radeon_device.c 26 KB

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