radeon_device.c 27 KB

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