radeon_device.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/console.h>
  29. #include <linux/slab.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include <drm/radeon_drm.h>
  33. #include <linux/vgaarb.h>
  34. #include <linux/vga_switcheroo.h>
  35. #include "radeon_reg.h"
  36. #include "radeon.h"
  37. #include "atom.h"
  38. static const char radeon_family_name[][16] = {
  39. "R100",
  40. "RV100",
  41. "RS100",
  42. "RV200",
  43. "RS200",
  44. "R200",
  45. "RV250",
  46. "RS300",
  47. "RV280",
  48. "R300",
  49. "R350",
  50. "RV350",
  51. "RV380",
  52. "R420",
  53. "R423",
  54. "RV410",
  55. "RS400",
  56. "RS480",
  57. "RS600",
  58. "RS690",
  59. "RS740",
  60. "RV515",
  61. "R520",
  62. "RV530",
  63. "RV560",
  64. "RV570",
  65. "R580",
  66. "R600",
  67. "RV610",
  68. "RV630",
  69. "RV670",
  70. "RV620",
  71. "RV635",
  72. "RS780",
  73. "RS880",
  74. "RV770",
  75. "RV730",
  76. "RV710",
  77. "RV740",
  78. "CEDAR",
  79. "REDWOOD",
  80. "JUNIPER",
  81. "CYPRESS",
  82. "HEMLOCK",
  83. "LAST",
  84. };
  85. /*
  86. * Clear GPU surface registers.
  87. */
  88. void radeon_surface_init(struct radeon_device *rdev)
  89. {
  90. /* FIXME: check this out */
  91. if (rdev->family < CHIP_R600) {
  92. int i;
  93. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  94. if (rdev->surface_regs[i].bo)
  95. radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
  96. else
  97. radeon_clear_surface_reg(rdev, i);
  98. }
  99. /* enable surfaces */
  100. WREG32(RADEON_SURFACE_CNTL, 0);
  101. }
  102. }
  103. /*
  104. * GPU scratch registers helpers function.
  105. */
  106. void radeon_scratch_init(struct radeon_device *rdev)
  107. {
  108. int i;
  109. /* FIXME: check this out */
  110. if (rdev->family < CHIP_R300) {
  111. rdev->scratch.num_reg = 5;
  112. } else {
  113. rdev->scratch.num_reg = 7;
  114. }
  115. rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
  116. for (i = 0; i < rdev->scratch.num_reg; i++) {
  117. rdev->scratch.free[i] = true;
  118. rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
  119. }
  120. }
  121. int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
  122. {
  123. int i;
  124. for (i = 0; i < rdev->scratch.num_reg; i++) {
  125. if (rdev->scratch.free[i]) {
  126. rdev->scratch.free[i] = false;
  127. *reg = rdev->scratch.reg[i];
  128. return 0;
  129. }
  130. }
  131. return -EINVAL;
  132. }
  133. void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
  134. {
  135. int i;
  136. for (i = 0; i < rdev->scratch.num_reg; i++) {
  137. if (rdev->scratch.reg[i] == reg) {
  138. rdev->scratch.free[i] = true;
  139. return;
  140. }
  141. }
  142. }
  143. void radeon_wb_disable(struct radeon_device *rdev)
  144. {
  145. int r;
  146. if (rdev->wb.wb_obj) {
  147. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  148. if (unlikely(r != 0))
  149. return;
  150. radeon_bo_kunmap(rdev->wb.wb_obj);
  151. radeon_bo_unpin(rdev->wb.wb_obj);
  152. radeon_bo_unreserve(rdev->wb.wb_obj);
  153. }
  154. rdev->wb.enabled = false;
  155. }
  156. void radeon_wb_fini(struct radeon_device *rdev)
  157. {
  158. radeon_wb_disable(rdev);
  159. if (rdev->wb.wb_obj) {
  160. radeon_bo_unref(&rdev->wb.wb_obj);
  161. rdev->wb.wb = NULL;
  162. rdev->wb.wb_obj = NULL;
  163. }
  164. }
  165. int radeon_wb_init(struct radeon_device *rdev)
  166. {
  167. int r;
  168. if (rdev->wb.wb_obj == NULL) {
  169. r = radeon_bo_create(rdev, NULL, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
  170. RADEON_GEM_DOMAIN_GTT, &rdev->wb.wb_obj);
  171. if (r) {
  172. dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
  173. return r;
  174. }
  175. }
  176. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  177. if (unlikely(r != 0)) {
  178. radeon_wb_fini(rdev);
  179. return r;
  180. }
  181. r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
  182. &rdev->wb.gpu_addr);
  183. if (r) {
  184. radeon_bo_unreserve(rdev->wb.wb_obj);
  185. dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
  186. radeon_wb_fini(rdev);
  187. return r;
  188. }
  189. r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
  190. radeon_bo_unreserve(rdev->wb.wb_obj);
  191. if (r) {
  192. dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
  193. radeon_wb_fini(rdev);
  194. return r;
  195. }
  196. /* disable event_write fences */
  197. rdev->wb.use_event = false;
  198. /* disabled via module param */
  199. if (radeon_no_wb == 1)
  200. rdev->wb.enabled = false;
  201. else {
  202. /* often unreliable on AGP */
  203. if (rdev->flags & RADEON_IS_AGP) {
  204. rdev->wb.enabled = false;
  205. } else {
  206. rdev->wb.enabled = true;
  207. /* event_write fences are only available on r600+ */
  208. if (rdev->family >= CHIP_R600)
  209. rdev->wb.use_event = true;
  210. }
  211. }
  212. dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
  213. return 0;
  214. }
  215. /**
  216. * radeon_vram_location - try to find VRAM location
  217. * @rdev: radeon device structure holding all necessary informations
  218. * @mc: memory controller structure holding memory informations
  219. * @base: base address at which to put VRAM
  220. *
  221. * Function will place try to place VRAM at base address provided
  222. * as parameter (which is so far either PCI aperture address or
  223. * for IGP TOM base address).
  224. *
  225. * If there is not enough space to fit the unvisible VRAM in the 32bits
  226. * address space then we limit the VRAM size to the aperture.
  227. *
  228. * If we are using AGP and if the AGP aperture doesn't allow us to have
  229. * room for all the VRAM than we restrict the VRAM to the PCI aperture
  230. * size and print a warning.
  231. *
  232. * This function will never fails, worst case are limiting VRAM.
  233. *
  234. * Note: GTT start, end, size should be initialized before calling this
  235. * function on AGP platform.
  236. *
  237. * Note: We don't explictly enforce VRAM start to be aligned on VRAM size,
  238. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  239. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  240. * not IGP.
  241. *
  242. * Note: we use mc_vram_size as on some board we need to program the mc to
  243. * cover the whole aperture even if VRAM size is inferior to aperture size
  244. * Novell bug 204882 + along with lots of ubuntu ones
  245. *
  246. * Note: when limiting vram it's safe to overwritte real_vram_size because
  247. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  248. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  249. * ones)
  250. *
  251. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  252. * explicitly check for that thought.
  253. *
  254. * FIXME: when reducing VRAM size align new size on power of 2.
  255. */
  256. void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
  257. {
  258. mc->vram_start = base;
  259. if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
  260. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  261. mc->real_vram_size = mc->aper_size;
  262. mc->mc_vram_size = mc->aper_size;
  263. }
  264. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  265. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  266. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  267. mc->real_vram_size = mc->aper_size;
  268. mc->mc_vram_size = mc->aper_size;
  269. }
  270. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  271. dev_info(rdev->dev, "VRAM: %lluM 0x%08llX - 0x%08llX (%lluM used)\n",
  272. mc->mc_vram_size >> 20, mc->vram_start,
  273. mc->vram_end, mc->real_vram_size >> 20);
  274. }
  275. /**
  276. * radeon_gtt_location - try to find GTT location
  277. * @rdev: radeon device structure holding all necessary informations
  278. * @mc: memory controller structure holding memory informations
  279. *
  280. * Function will place try to place GTT before or after VRAM.
  281. *
  282. * If GTT size is bigger than space left then we ajust GTT size.
  283. * Thus function will never fails.
  284. *
  285. * FIXME: when reducing GTT size align new size on power of 2.
  286. */
  287. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  288. {
  289. u64 size_af, size_bf;
  290. size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  291. size_bf = mc->vram_start & ~mc->gtt_base_align;
  292. if (size_bf > size_af) {
  293. if (mc->gtt_size > size_bf) {
  294. dev_warn(rdev->dev, "limiting GTT\n");
  295. mc->gtt_size = size_bf;
  296. }
  297. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  298. } else {
  299. if (mc->gtt_size > size_af) {
  300. dev_warn(rdev->dev, "limiting GTT\n");
  301. mc->gtt_size = size_af;
  302. }
  303. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  304. }
  305. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  306. dev_info(rdev->dev, "GTT: %lluM 0x%08llX - 0x%08llX\n",
  307. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  308. }
  309. /*
  310. * GPU helpers function.
  311. */
  312. bool radeon_card_posted(struct radeon_device *rdev)
  313. {
  314. uint32_t reg;
  315. /* first check CRTCs */
  316. if (ASIC_IS_DCE41(rdev)) {
  317. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  318. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  319. if (reg & EVERGREEN_CRTC_MASTER_EN)
  320. return true;
  321. } else if (ASIC_IS_DCE4(rdev)) {
  322. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  323. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
  324. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  325. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
  326. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  327. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  328. if (reg & EVERGREEN_CRTC_MASTER_EN)
  329. return true;
  330. } else if (ASIC_IS_AVIVO(rdev)) {
  331. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  332. RREG32(AVIVO_D2CRTC_CONTROL);
  333. if (reg & AVIVO_CRTC_EN) {
  334. return true;
  335. }
  336. } else {
  337. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  338. RREG32(RADEON_CRTC2_GEN_CNTL);
  339. if (reg & RADEON_CRTC_EN) {
  340. return true;
  341. }
  342. }
  343. /* then check MEM_SIZE, in case the crtcs are off */
  344. if (rdev->family >= CHIP_R600)
  345. reg = RREG32(R600_CONFIG_MEMSIZE);
  346. else
  347. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  348. if (reg)
  349. return true;
  350. return false;
  351. }
  352. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  353. {
  354. fixed20_12 a;
  355. u32 sclk = rdev->pm.current_sclk;
  356. u32 mclk = rdev->pm.current_mclk;
  357. /* sclk/mclk in Mhz */
  358. a.full = dfixed_const(100);
  359. rdev->pm.sclk.full = dfixed_const(sclk);
  360. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  361. rdev->pm.mclk.full = dfixed_const(mclk);
  362. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  363. if (rdev->flags & RADEON_IS_IGP) {
  364. a.full = dfixed_const(16);
  365. /* core_bandwidth = sclk(Mhz) * 16 */
  366. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  367. }
  368. }
  369. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  370. {
  371. if (radeon_card_posted(rdev))
  372. return true;
  373. if (rdev->bios) {
  374. DRM_INFO("GPU not posted. posting now...\n");
  375. if (rdev->is_atom_bios)
  376. atom_asic_init(rdev->mode_info.atom_context);
  377. else
  378. radeon_combios_asic_init(rdev->ddev);
  379. return true;
  380. } else {
  381. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  382. return false;
  383. }
  384. }
  385. int radeon_dummy_page_init(struct radeon_device *rdev)
  386. {
  387. if (rdev->dummy_page.page)
  388. return 0;
  389. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  390. if (rdev->dummy_page.page == NULL)
  391. return -ENOMEM;
  392. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  393. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  394. if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
  395. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  396. __free_page(rdev->dummy_page.page);
  397. rdev->dummy_page.page = NULL;
  398. return -ENOMEM;
  399. }
  400. return 0;
  401. }
  402. void radeon_dummy_page_fini(struct radeon_device *rdev)
  403. {
  404. if (rdev->dummy_page.page == NULL)
  405. return;
  406. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  407. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  408. __free_page(rdev->dummy_page.page);
  409. rdev->dummy_page.page = NULL;
  410. }
  411. /* ATOM accessor methods */
  412. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  413. {
  414. struct radeon_device *rdev = info->dev->dev_private;
  415. uint32_t r;
  416. r = rdev->pll_rreg(rdev, reg);
  417. return r;
  418. }
  419. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  420. {
  421. struct radeon_device *rdev = info->dev->dev_private;
  422. rdev->pll_wreg(rdev, reg, val);
  423. }
  424. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  425. {
  426. struct radeon_device *rdev = info->dev->dev_private;
  427. uint32_t r;
  428. r = rdev->mc_rreg(rdev, reg);
  429. return r;
  430. }
  431. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  432. {
  433. struct radeon_device *rdev = info->dev->dev_private;
  434. rdev->mc_wreg(rdev, reg, val);
  435. }
  436. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  437. {
  438. struct radeon_device *rdev = info->dev->dev_private;
  439. WREG32(reg*4, val);
  440. }
  441. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  442. {
  443. struct radeon_device *rdev = info->dev->dev_private;
  444. uint32_t r;
  445. r = RREG32(reg*4);
  446. return r;
  447. }
  448. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  449. {
  450. struct radeon_device *rdev = info->dev->dev_private;
  451. WREG32_IO(reg*4, val);
  452. }
  453. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  454. {
  455. struct radeon_device *rdev = info->dev->dev_private;
  456. uint32_t r;
  457. r = RREG32_IO(reg*4);
  458. return r;
  459. }
  460. int radeon_atombios_init(struct radeon_device *rdev)
  461. {
  462. struct card_info *atom_card_info =
  463. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  464. if (!atom_card_info)
  465. return -ENOMEM;
  466. rdev->mode_info.atom_card_info = atom_card_info;
  467. atom_card_info->dev = rdev->ddev;
  468. atom_card_info->reg_read = cail_reg_read;
  469. atom_card_info->reg_write = cail_reg_write;
  470. /* needed for iio ops */
  471. if (rdev->rio_mem) {
  472. atom_card_info->ioreg_read = cail_ioreg_read;
  473. atom_card_info->ioreg_write = cail_ioreg_write;
  474. } else {
  475. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  476. atom_card_info->ioreg_read = cail_reg_read;
  477. atom_card_info->ioreg_write = cail_reg_write;
  478. }
  479. atom_card_info->mc_read = cail_mc_read;
  480. atom_card_info->mc_write = cail_mc_write;
  481. atom_card_info->pll_read = cail_pll_read;
  482. atom_card_info->pll_write = cail_pll_write;
  483. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  484. mutex_init(&rdev->mode_info.atom_context->mutex);
  485. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  486. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  487. return 0;
  488. }
  489. void radeon_atombios_fini(struct radeon_device *rdev)
  490. {
  491. if (rdev->mode_info.atom_context) {
  492. kfree(rdev->mode_info.atom_context->scratch);
  493. kfree(rdev->mode_info.atom_context);
  494. }
  495. kfree(rdev->mode_info.atom_card_info);
  496. }
  497. int radeon_combios_init(struct radeon_device *rdev)
  498. {
  499. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  500. return 0;
  501. }
  502. void radeon_combios_fini(struct radeon_device *rdev)
  503. {
  504. }
  505. /* if we get transitioned to only one device, tak VGA back */
  506. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  507. {
  508. struct radeon_device *rdev = cookie;
  509. radeon_vga_set_state(rdev, state);
  510. if (state)
  511. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  512. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  513. else
  514. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  515. }
  516. void radeon_check_arguments(struct radeon_device *rdev)
  517. {
  518. /* vramlimit must be a power of two */
  519. switch (radeon_vram_limit) {
  520. case 0:
  521. case 4:
  522. case 8:
  523. case 16:
  524. case 32:
  525. case 64:
  526. case 128:
  527. case 256:
  528. case 512:
  529. case 1024:
  530. case 2048:
  531. case 4096:
  532. break;
  533. default:
  534. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  535. radeon_vram_limit);
  536. radeon_vram_limit = 0;
  537. break;
  538. }
  539. radeon_vram_limit = radeon_vram_limit << 20;
  540. /* gtt size must be power of two and greater or equal to 32M */
  541. switch (radeon_gart_size) {
  542. case 4:
  543. case 8:
  544. case 16:
  545. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  546. radeon_gart_size);
  547. radeon_gart_size = 512;
  548. break;
  549. case 32:
  550. case 64:
  551. case 128:
  552. case 256:
  553. case 512:
  554. case 1024:
  555. case 2048:
  556. case 4096:
  557. break;
  558. default:
  559. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  560. radeon_gart_size);
  561. radeon_gart_size = 512;
  562. break;
  563. }
  564. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  565. /* AGP mode can only be -1, 1, 2, 4, 8 */
  566. switch (radeon_agpmode) {
  567. case -1:
  568. case 0:
  569. case 1:
  570. case 2:
  571. case 4:
  572. case 8:
  573. break;
  574. default:
  575. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  576. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  577. radeon_agpmode = 0;
  578. break;
  579. }
  580. }
  581. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  582. {
  583. struct drm_device *dev = pci_get_drvdata(pdev);
  584. struct radeon_device *rdev = dev->dev_private;
  585. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  586. if (state == VGA_SWITCHEROO_ON) {
  587. printk(KERN_INFO "radeon: switched on\n");
  588. /* don't suspend or resume card normally */
  589. rdev->powered_down = false;
  590. radeon_resume_kms(dev);
  591. drm_kms_helper_poll_enable(dev);
  592. } else {
  593. printk(KERN_INFO "radeon: switched off\n");
  594. drm_kms_helper_poll_disable(dev);
  595. radeon_suspend_kms(dev, pmm);
  596. /* don't suspend or resume card normally */
  597. rdev->powered_down = true;
  598. }
  599. }
  600. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  601. {
  602. struct drm_device *dev = pci_get_drvdata(pdev);
  603. bool can_switch;
  604. spin_lock(&dev->count_lock);
  605. can_switch = (dev->open_count == 0);
  606. spin_unlock(&dev->count_lock);
  607. return can_switch;
  608. }
  609. int radeon_device_init(struct radeon_device *rdev,
  610. struct drm_device *ddev,
  611. struct pci_dev *pdev,
  612. uint32_t flags)
  613. {
  614. int r, i;
  615. int dma_bits;
  616. rdev->shutdown = false;
  617. rdev->dev = &pdev->dev;
  618. rdev->ddev = ddev;
  619. rdev->pdev = pdev;
  620. rdev->flags = flags;
  621. rdev->family = flags & RADEON_FAMILY_MASK;
  622. rdev->is_atom_bios = false;
  623. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  624. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  625. rdev->gpu_lockup = false;
  626. rdev->accel_working = false;
  627. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X).\n",
  628. radeon_family_name[rdev->family], pdev->vendor, pdev->device);
  629. /* mutex initialization are all done here so we
  630. * can recall function without having locking issues */
  631. mutex_init(&rdev->cs_mutex);
  632. mutex_init(&rdev->ib_pool.mutex);
  633. mutex_init(&rdev->cp.mutex);
  634. mutex_init(&rdev->dc_hw_i2c_mutex);
  635. if (rdev->family >= CHIP_R600)
  636. spin_lock_init(&rdev->ih.lock);
  637. mutex_init(&rdev->gem.mutex);
  638. mutex_init(&rdev->pm.mutex);
  639. mutex_init(&rdev->vram_mutex);
  640. rwlock_init(&rdev->fence_drv.lock);
  641. INIT_LIST_HEAD(&rdev->gem.objects);
  642. init_waitqueue_head(&rdev->irq.vblank_queue);
  643. init_waitqueue_head(&rdev->irq.idle_queue);
  644. /* setup workqueue */
  645. rdev->wq = create_workqueue("radeon");
  646. if (rdev->wq == NULL)
  647. return -ENOMEM;
  648. /* Set asic functions */
  649. r = radeon_asic_init(rdev);
  650. if (r)
  651. return r;
  652. radeon_check_arguments(rdev);
  653. /* all of the newer IGP chips have an internal gart
  654. * However some rs4xx report as AGP, so remove that here.
  655. */
  656. if ((rdev->family >= CHIP_RS400) &&
  657. (rdev->flags & RADEON_IS_IGP)) {
  658. rdev->flags &= ~RADEON_IS_AGP;
  659. }
  660. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  661. radeon_agp_disable(rdev);
  662. }
  663. /* set DMA mask + need_dma32 flags.
  664. * PCIE - can handle 40-bits.
  665. * IGP - can handle 40-bits (in theory)
  666. * AGP - generally dma32 is safest
  667. * PCI - only dma32
  668. */
  669. rdev->need_dma32 = false;
  670. if (rdev->flags & RADEON_IS_AGP)
  671. rdev->need_dma32 = true;
  672. if (rdev->flags & RADEON_IS_PCI)
  673. rdev->need_dma32 = true;
  674. dma_bits = rdev->need_dma32 ? 32 : 40;
  675. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  676. if (r) {
  677. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  678. }
  679. /* Registers mapping */
  680. /* TODO: block userspace mapping of io register */
  681. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  682. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  683. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  684. if (rdev->rmmio == NULL) {
  685. return -ENOMEM;
  686. }
  687. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  688. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  689. /* io port mapping */
  690. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  691. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  692. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  693. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  694. break;
  695. }
  696. }
  697. if (rdev->rio_mem == NULL)
  698. DRM_ERROR("Unable to find PCI I/O BAR\n");
  699. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  700. /* this will fail for cards that aren't VGA class devices, just
  701. * ignore it */
  702. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  703. vga_switcheroo_register_client(rdev->pdev,
  704. radeon_switcheroo_set_state,
  705. radeon_switcheroo_can_switch);
  706. r = radeon_init(rdev);
  707. if (r)
  708. return r;
  709. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  710. /* Acceleration not working on AGP card try again
  711. * with fallback to PCI or PCIE GART
  712. */
  713. radeon_asic_reset(rdev);
  714. radeon_fini(rdev);
  715. radeon_agp_disable(rdev);
  716. r = radeon_init(rdev);
  717. if (r)
  718. return r;
  719. }
  720. if (radeon_testing) {
  721. radeon_test_moves(rdev);
  722. }
  723. if (radeon_benchmarking) {
  724. radeon_benchmark(rdev);
  725. }
  726. return 0;
  727. }
  728. void radeon_device_fini(struct radeon_device *rdev)
  729. {
  730. DRM_INFO("radeon: finishing device.\n");
  731. rdev->shutdown = true;
  732. /* evict vram memory */
  733. radeon_bo_evict_vram(rdev);
  734. radeon_fini(rdev);
  735. destroy_workqueue(rdev->wq);
  736. vga_switcheroo_unregister_client(rdev->pdev);
  737. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  738. if (rdev->rio_mem)
  739. pci_iounmap(rdev->pdev, rdev->rio_mem);
  740. rdev->rio_mem = NULL;
  741. iounmap(rdev->rmmio);
  742. rdev->rmmio = NULL;
  743. }
  744. /*
  745. * Suspend & resume.
  746. */
  747. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  748. {
  749. struct radeon_device *rdev;
  750. struct drm_crtc *crtc;
  751. struct drm_connector *connector;
  752. int r;
  753. if (dev == NULL || dev->dev_private == NULL) {
  754. return -ENODEV;
  755. }
  756. if (state.event == PM_EVENT_PRETHAW) {
  757. return 0;
  758. }
  759. rdev = dev->dev_private;
  760. if (rdev->powered_down)
  761. return 0;
  762. /* turn off display hw */
  763. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  764. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  765. }
  766. /* unpin the front buffers */
  767. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  768. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  769. struct radeon_bo *robj;
  770. if (rfb == NULL || rfb->obj == NULL) {
  771. continue;
  772. }
  773. robj = rfb->obj->driver_private;
  774. /* don't unpin kernel fb objects */
  775. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  776. r = radeon_bo_reserve(robj, false);
  777. if (r == 0) {
  778. radeon_bo_unpin(robj);
  779. radeon_bo_unreserve(robj);
  780. }
  781. }
  782. }
  783. /* evict vram memory */
  784. radeon_bo_evict_vram(rdev);
  785. /* wait for gpu to finish processing current batch */
  786. radeon_fence_wait_last(rdev);
  787. radeon_save_bios_scratch_regs(rdev);
  788. radeon_pm_suspend(rdev);
  789. radeon_suspend(rdev);
  790. radeon_hpd_fini(rdev);
  791. /* evict remaining vram memory */
  792. radeon_bo_evict_vram(rdev);
  793. radeon_agp_suspend(rdev);
  794. pci_save_state(dev->pdev);
  795. if (state.event == PM_EVENT_SUSPEND) {
  796. /* Shut down the device */
  797. pci_disable_device(dev->pdev);
  798. pci_set_power_state(dev->pdev, PCI_D3hot);
  799. }
  800. acquire_console_sem();
  801. radeon_fbdev_set_suspend(rdev, 1);
  802. release_console_sem();
  803. return 0;
  804. }
  805. int radeon_resume_kms(struct drm_device *dev)
  806. {
  807. struct drm_connector *connector;
  808. struct radeon_device *rdev = dev->dev_private;
  809. if (rdev->powered_down)
  810. return 0;
  811. acquire_console_sem();
  812. pci_set_power_state(dev->pdev, PCI_D0);
  813. pci_restore_state(dev->pdev);
  814. if (pci_enable_device(dev->pdev)) {
  815. release_console_sem();
  816. return -1;
  817. }
  818. pci_set_master(dev->pdev);
  819. /* resume AGP if in use */
  820. radeon_agp_resume(rdev);
  821. radeon_resume(rdev);
  822. radeon_pm_resume(rdev);
  823. radeon_restore_bios_scratch_regs(rdev);
  824. /* turn on display hw */
  825. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  826. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  827. }
  828. radeon_fbdev_set_suspend(rdev, 0);
  829. release_console_sem();
  830. /* reset hpd state */
  831. radeon_hpd_init(rdev);
  832. /* blat the mode back in */
  833. drm_helper_resume_force_mode(dev);
  834. return 0;
  835. }
  836. int radeon_gpu_reset(struct radeon_device *rdev)
  837. {
  838. int r;
  839. radeon_save_bios_scratch_regs(rdev);
  840. radeon_suspend(rdev);
  841. r = radeon_asic_reset(rdev);
  842. if (!r) {
  843. dev_info(rdev->dev, "GPU reset succeed\n");
  844. radeon_resume(rdev);
  845. radeon_restore_bios_scratch_regs(rdev);
  846. drm_helper_resume_force_mode(rdev->ddev);
  847. return 0;
  848. }
  849. /* bad news, how to tell it to userspace ? */
  850. dev_info(rdev->dev, "GPU reset failed\n");
  851. return r;
  852. }
  853. /*
  854. * Debugfs
  855. */
  856. struct radeon_debugfs {
  857. struct drm_info_list *files;
  858. unsigned num_files;
  859. };
  860. static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES];
  861. static unsigned _radeon_debugfs_count = 0;
  862. int radeon_debugfs_add_files(struct radeon_device *rdev,
  863. struct drm_info_list *files,
  864. unsigned nfiles)
  865. {
  866. unsigned i;
  867. for (i = 0; i < _radeon_debugfs_count; i++) {
  868. if (_radeon_debugfs[i].files == files) {
  869. /* Already registered */
  870. return 0;
  871. }
  872. }
  873. if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) {
  874. DRM_ERROR("Reached maximum number of debugfs files.\n");
  875. DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n");
  876. return -EINVAL;
  877. }
  878. _radeon_debugfs[_radeon_debugfs_count].files = files;
  879. _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles;
  880. _radeon_debugfs_count++;
  881. #if defined(CONFIG_DEBUG_FS)
  882. drm_debugfs_create_files(files, nfiles,
  883. rdev->ddev->control->debugfs_root,
  884. rdev->ddev->control);
  885. drm_debugfs_create_files(files, nfiles,
  886. rdev->ddev->primary->debugfs_root,
  887. rdev->ddev->primary);
  888. #endif
  889. return 0;
  890. }
  891. #if defined(CONFIG_DEBUG_FS)
  892. int radeon_debugfs_init(struct drm_minor *minor)
  893. {
  894. return 0;
  895. }
  896. void radeon_debugfs_cleanup(struct drm_minor *minor)
  897. {
  898. unsigned i;
  899. for (i = 0; i < _radeon_debugfs_count; i++) {
  900. drm_debugfs_remove_files(_radeon_debugfs[i].files,
  901. _radeon_debugfs[i].num_files, minor);
  902. }
  903. }
  904. #endif