radeon_device.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. "ARUBA",
  92. "TAHITI",
  93. "PITCAIRN",
  94. "VERDE",
  95. "LAST",
  96. };
  97. /**
  98. * radeon_surface_init - Clear GPU surface registers.
  99. *
  100. * @rdev: radeon_device pointer
  101. *
  102. * Clear GPU surface registers (r1xx-r5xx).
  103. */
  104. void radeon_surface_init(struct radeon_device *rdev)
  105. {
  106. /* FIXME: check this out */
  107. if (rdev->family < CHIP_R600) {
  108. int i;
  109. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  110. if (rdev->surface_regs[i].bo)
  111. radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
  112. else
  113. radeon_clear_surface_reg(rdev, i);
  114. }
  115. /* enable surfaces */
  116. WREG32(RADEON_SURFACE_CNTL, 0);
  117. }
  118. }
  119. /*
  120. * GPU scratch registers helpers function.
  121. */
  122. /**
  123. * radeon_scratch_init - Init scratch register driver information.
  124. *
  125. * @rdev: radeon_device pointer
  126. *
  127. * Init CP scratch register driver information (r1xx-r5xx)
  128. */
  129. void radeon_scratch_init(struct radeon_device *rdev)
  130. {
  131. int i;
  132. /* FIXME: check this out */
  133. if (rdev->family < CHIP_R300) {
  134. rdev->scratch.num_reg = 5;
  135. } else {
  136. rdev->scratch.num_reg = 7;
  137. }
  138. rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
  139. for (i = 0; i < rdev->scratch.num_reg; i++) {
  140. rdev->scratch.free[i] = true;
  141. rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
  142. }
  143. }
  144. /**
  145. * radeon_scratch_get - Allocate a scratch register
  146. *
  147. * @rdev: radeon_device pointer
  148. * @reg: scratch register mmio offset
  149. *
  150. * Allocate a CP scratch register for use by the driver (all asics).
  151. * Returns 0 on success or -EINVAL on failure.
  152. */
  153. int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
  154. {
  155. int i;
  156. for (i = 0; i < rdev->scratch.num_reg; i++) {
  157. if (rdev->scratch.free[i]) {
  158. rdev->scratch.free[i] = false;
  159. *reg = rdev->scratch.reg[i];
  160. return 0;
  161. }
  162. }
  163. return -EINVAL;
  164. }
  165. /**
  166. * radeon_scratch_free - Free a scratch register
  167. *
  168. * @rdev: radeon_device pointer
  169. * @reg: scratch register mmio offset
  170. *
  171. * Free a CP scratch register allocated for use by the driver (all asics)
  172. */
  173. void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
  174. {
  175. int i;
  176. for (i = 0; i < rdev->scratch.num_reg; i++) {
  177. if (rdev->scratch.reg[i] == reg) {
  178. rdev->scratch.free[i] = true;
  179. return;
  180. }
  181. }
  182. }
  183. /*
  184. * radeon_wb_*()
  185. * Writeback is the the method by which the the GPU updates special pages
  186. * in memory with the status of certain GPU events (fences, ring pointers,
  187. * etc.).
  188. */
  189. /**
  190. * radeon_wb_disable - Disable Writeback
  191. *
  192. * @rdev: radeon_device pointer
  193. *
  194. * Disables Writeback (all asics). Used for suspend.
  195. */
  196. void radeon_wb_disable(struct radeon_device *rdev)
  197. {
  198. int r;
  199. if (rdev->wb.wb_obj) {
  200. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  201. if (unlikely(r != 0))
  202. return;
  203. radeon_bo_kunmap(rdev->wb.wb_obj);
  204. radeon_bo_unpin(rdev->wb.wb_obj);
  205. radeon_bo_unreserve(rdev->wb.wb_obj);
  206. }
  207. rdev->wb.enabled = false;
  208. }
  209. /**
  210. * radeon_wb_fini - Disable Writeback and free memory
  211. *
  212. * @rdev: radeon_device pointer
  213. *
  214. * Disables Writeback and frees the Writeback memory (all asics).
  215. * Used at driver shutdown.
  216. */
  217. void radeon_wb_fini(struct radeon_device *rdev)
  218. {
  219. radeon_wb_disable(rdev);
  220. if (rdev->wb.wb_obj) {
  221. radeon_bo_unref(&rdev->wb.wb_obj);
  222. rdev->wb.wb = NULL;
  223. rdev->wb.wb_obj = NULL;
  224. }
  225. }
  226. /**
  227. * radeon_wb_init- Init Writeback driver info and allocate memory
  228. *
  229. * @rdev: radeon_device pointer
  230. *
  231. * Disables Writeback and frees the Writeback memory (all asics).
  232. * Used at driver startup.
  233. * Returns 0 on success or an -error on failure.
  234. */
  235. int radeon_wb_init(struct radeon_device *rdev)
  236. {
  237. int r;
  238. if (rdev->wb.wb_obj == NULL) {
  239. r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
  240. RADEON_GEM_DOMAIN_GTT, NULL, &rdev->wb.wb_obj);
  241. if (r) {
  242. dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
  243. return r;
  244. }
  245. }
  246. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  247. if (unlikely(r != 0)) {
  248. radeon_wb_fini(rdev);
  249. return r;
  250. }
  251. r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
  252. &rdev->wb.gpu_addr);
  253. if (r) {
  254. radeon_bo_unreserve(rdev->wb.wb_obj);
  255. dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
  256. radeon_wb_fini(rdev);
  257. return r;
  258. }
  259. r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
  260. radeon_bo_unreserve(rdev->wb.wb_obj);
  261. if (r) {
  262. dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
  263. radeon_wb_fini(rdev);
  264. return r;
  265. }
  266. /* clear wb memory */
  267. memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
  268. /* disable event_write fences */
  269. rdev->wb.use_event = false;
  270. /* disabled via module param */
  271. if (radeon_no_wb == 1) {
  272. rdev->wb.enabled = false;
  273. } else {
  274. if (rdev->flags & RADEON_IS_AGP) {
  275. /* often unreliable on AGP */
  276. rdev->wb.enabled = false;
  277. } else if (rdev->family < CHIP_R300) {
  278. /* often unreliable on pre-r300 */
  279. rdev->wb.enabled = false;
  280. } else {
  281. rdev->wb.enabled = true;
  282. /* event_write fences are only available on r600+ */
  283. if (rdev->family >= CHIP_R600) {
  284. rdev->wb.use_event = true;
  285. }
  286. }
  287. }
  288. /* always use writeback/events on NI, APUs */
  289. if (rdev->family >= CHIP_PALM) {
  290. rdev->wb.enabled = true;
  291. rdev->wb.use_event = true;
  292. }
  293. dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
  294. return 0;
  295. }
  296. /**
  297. * radeon_vram_location - try to find VRAM location
  298. * @rdev: radeon device structure holding all necessary informations
  299. * @mc: memory controller structure holding memory informations
  300. * @base: base address at which to put VRAM
  301. *
  302. * Function will place try to place VRAM at base address provided
  303. * as parameter (which is so far either PCI aperture address or
  304. * for IGP TOM base address).
  305. *
  306. * If there is not enough space to fit the unvisible VRAM in the 32bits
  307. * address space then we limit the VRAM size to the aperture.
  308. *
  309. * If we are using AGP and if the AGP aperture doesn't allow us to have
  310. * room for all the VRAM than we restrict the VRAM to the PCI aperture
  311. * size and print a warning.
  312. *
  313. * This function will never fails, worst case are limiting VRAM.
  314. *
  315. * Note: GTT start, end, size should be initialized before calling this
  316. * function on AGP platform.
  317. *
  318. * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  319. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  320. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  321. * not IGP.
  322. *
  323. * Note: we use mc_vram_size as on some board we need to program the mc to
  324. * cover the whole aperture even if VRAM size is inferior to aperture size
  325. * Novell bug 204882 + along with lots of ubuntu ones
  326. *
  327. * Note: when limiting vram it's safe to overwritte real_vram_size because
  328. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  329. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  330. * ones)
  331. *
  332. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  333. * explicitly check for that thought.
  334. *
  335. * FIXME: when reducing VRAM size align new size on power of 2.
  336. */
  337. void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
  338. {
  339. mc->vram_start = base;
  340. if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
  341. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  342. mc->real_vram_size = mc->aper_size;
  343. mc->mc_vram_size = mc->aper_size;
  344. }
  345. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  346. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  347. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  348. mc->real_vram_size = mc->aper_size;
  349. mc->mc_vram_size = mc->aper_size;
  350. }
  351. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  352. if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size)
  353. mc->real_vram_size = radeon_vram_limit;
  354. dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  355. mc->mc_vram_size >> 20, mc->vram_start,
  356. mc->vram_end, mc->real_vram_size >> 20);
  357. }
  358. /**
  359. * radeon_gtt_location - try to find GTT location
  360. * @rdev: radeon device structure holding all necessary informations
  361. * @mc: memory controller structure holding memory informations
  362. *
  363. * Function will place try to place GTT before or after VRAM.
  364. *
  365. * If GTT size is bigger than space left then we ajust GTT size.
  366. * Thus function will never fails.
  367. *
  368. * FIXME: when reducing GTT size align new size on power of 2.
  369. */
  370. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  371. {
  372. u64 size_af, size_bf;
  373. size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  374. size_bf = mc->vram_start & ~mc->gtt_base_align;
  375. if (size_bf > size_af) {
  376. if (mc->gtt_size > size_bf) {
  377. dev_warn(rdev->dev, "limiting GTT\n");
  378. mc->gtt_size = size_bf;
  379. }
  380. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  381. } else {
  382. if (mc->gtt_size > size_af) {
  383. dev_warn(rdev->dev, "limiting GTT\n");
  384. mc->gtt_size = size_af;
  385. }
  386. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  387. }
  388. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  389. dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  390. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  391. }
  392. /*
  393. * GPU helpers function.
  394. */
  395. /**
  396. * radeon_card_posted - check if the hw has already been initialized
  397. *
  398. * @rdev: radeon_device pointer
  399. *
  400. * Check if the asic has been initialized (all asics).
  401. * Used at driver startup.
  402. * Returns true if initialized or false if not.
  403. */
  404. bool radeon_card_posted(struct radeon_device *rdev)
  405. {
  406. uint32_t reg;
  407. if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE)
  408. return false;
  409. /* first check CRTCs */
  410. if (ASIC_IS_DCE41(rdev)) {
  411. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  412. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  413. if (reg & EVERGREEN_CRTC_MASTER_EN)
  414. return true;
  415. } else if (ASIC_IS_DCE4(rdev)) {
  416. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  417. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
  418. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  419. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
  420. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  421. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  422. if (reg & EVERGREEN_CRTC_MASTER_EN)
  423. return true;
  424. } else if (ASIC_IS_AVIVO(rdev)) {
  425. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  426. RREG32(AVIVO_D2CRTC_CONTROL);
  427. if (reg & AVIVO_CRTC_EN) {
  428. return true;
  429. }
  430. } else {
  431. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  432. RREG32(RADEON_CRTC2_GEN_CNTL);
  433. if (reg & RADEON_CRTC_EN) {
  434. return true;
  435. }
  436. }
  437. /* then check MEM_SIZE, in case the crtcs are off */
  438. if (rdev->family >= CHIP_R600)
  439. reg = RREG32(R600_CONFIG_MEMSIZE);
  440. else
  441. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  442. if (reg)
  443. return true;
  444. return false;
  445. }
  446. /**
  447. * radeon_update_bandwidth_info - update display bandwidth params
  448. *
  449. * @rdev: radeon_device pointer
  450. *
  451. * Used when sclk/mclk are switched or display modes are set.
  452. * params are used to calculate display watermarks (all asics)
  453. */
  454. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  455. {
  456. fixed20_12 a;
  457. u32 sclk = rdev->pm.current_sclk;
  458. u32 mclk = rdev->pm.current_mclk;
  459. /* sclk/mclk in Mhz */
  460. a.full = dfixed_const(100);
  461. rdev->pm.sclk.full = dfixed_const(sclk);
  462. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  463. rdev->pm.mclk.full = dfixed_const(mclk);
  464. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  465. if (rdev->flags & RADEON_IS_IGP) {
  466. a.full = dfixed_const(16);
  467. /* core_bandwidth = sclk(Mhz) * 16 */
  468. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  469. }
  470. }
  471. /**
  472. * radeon_boot_test_post_card - check and possibly initialize the hw
  473. *
  474. * @rdev: radeon_device pointer
  475. *
  476. * Check if the asic is initialized and if not, attempt to initialize
  477. * it (all asics).
  478. * Returns true if initialized or false if not.
  479. */
  480. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  481. {
  482. if (radeon_card_posted(rdev))
  483. return true;
  484. if (rdev->bios) {
  485. DRM_INFO("GPU not posted. posting now...\n");
  486. if (rdev->is_atom_bios)
  487. atom_asic_init(rdev->mode_info.atom_context);
  488. else
  489. radeon_combios_asic_init(rdev->ddev);
  490. return true;
  491. } else {
  492. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  493. return false;
  494. }
  495. }
  496. /**
  497. * radeon_dummy_page_init - init dummy page used by the driver
  498. *
  499. * @rdev: radeon_device pointer
  500. *
  501. * Allocate the dummy page used by the driver (all asics).
  502. * This dummy page is used by the driver as a filler for gart entries
  503. * when pages are taken out of the GART
  504. * Returns 0 on sucess, -ENOMEM on failure.
  505. */
  506. int radeon_dummy_page_init(struct radeon_device *rdev)
  507. {
  508. if (rdev->dummy_page.page)
  509. return 0;
  510. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  511. if (rdev->dummy_page.page == NULL)
  512. return -ENOMEM;
  513. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  514. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  515. if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
  516. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  517. __free_page(rdev->dummy_page.page);
  518. rdev->dummy_page.page = NULL;
  519. return -ENOMEM;
  520. }
  521. return 0;
  522. }
  523. /**
  524. * radeon_dummy_page_fini - free dummy page used by the driver
  525. *
  526. * @rdev: radeon_device pointer
  527. *
  528. * Frees the dummy page used by the driver (all asics).
  529. */
  530. void radeon_dummy_page_fini(struct radeon_device *rdev)
  531. {
  532. if (rdev->dummy_page.page == NULL)
  533. return;
  534. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  535. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  536. __free_page(rdev->dummy_page.page);
  537. rdev->dummy_page.page = NULL;
  538. }
  539. /* ATOM accessor methods */
  540. /*
  541. * ATOM is an interpreted byte code stored in tables in the vbios. The
  542. * driver registers callbacks to access registers and the interpreter
  543. * in the driver parses the tables and executes then to program specific
  544. * actions (set display modes, asic init, etc.). See radeon_atombios.c,
  545. * atombios.h, and atom.c
  546. */
  547. /**
  548. * cail_pll_read - read PLL register
  549. *
  550. * @info: atom card_info pointer
  551. * @reg: PLL register offset
  552. *
  553. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  554. * Returns the value of the PLL register.
  555. */
  556. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  557. {
  558. struct radeon_device *rdev = info->dev->dev_private;
  559. uint32_t r;
  560. r = rdev->pll_rreg(rdev, reg);
  561. return r;
  562. }
  563. /**
  564. * cail_pll_write - write PLL register
  565. *
  566. * @info: atom card_info pointer
  567. * @reg: PLL register offset
  568. * @val: value to write to the pll register
  569. *
  570. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  571. */
  572. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  573. {
  574. struct radeon_device *rdev = info->dev->dev_private;
  575. rdev->pll_wreg(rdev, reg, val);
  576. }
  577. /**
  578. * cail_mc_read - read MC (Memory Controller) register
  579. *
  580. * @info: atom card_info pointer
  581. * @reg: MC register offset
  582. *
  583. * Provides an MC register accessor for the atom interpreter (r4xx+).
  584. * Returns the value of the MC register.
  585. */
  586. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  587. {
  588. struct radeon_device *rdev = info->dev->dev_private;
  589. uint32_t r;
  590. r = rdev->mc_rreg(rdev, reg);
  591. return r;
  592. }
  593. /**
  594. * cail_mc_write - write MC (Memory Controller) register
  595. *
  596. * @info: atom card_info pointer
  597. * @reg: MC register offset
  598. * @val: value to write to the pll register
  599. *
  600. * Provides a MC register accessor for the atom interpreter (r4xx+).
  601. */
  602. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  603. {
  604. struct radeon_device *rdev = info->dev->dev_private;
  605. rdev->mc_wreg(rdev, reg, val);
  606. }
  607. /**
  608. * cail_reg_write - write MMIO register
  609. *
  610. * @info: atom card_info pointer
  611. * @reg: MMIO register offset
  612. * @val: value to write to the pll register
  613. *
  614. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  615. */
  616. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  617. {
  618. struct radeon_device *rdev = info->dev->dev_private;
  619. WREG32(reg*4, val);
  620. }
  621. /**
  622. * cail_reg_read - read MMIO register
  623. *
  624. * @info: atom card_info pointer
  625. * @reg: MMIO register offset
  626. *
  627. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  628. * Returns the value of the MMIO register.
  629. */
  630. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  631. {
  632. struct radeon_device *rdev = info->dev->dev_private;
  633. uint32_t r;
  634. r = RREG32(reg*4);
  635. return r;
  636. }
  637. /**
  638. * cail_ioreg_write - write IO register
  639. *
  640. * @info: atom card_info pointer
  641. * @reg: IO register offset
  642. * @val: value to write to the pll register
  643. *
  644. * Provides a IO register accessor for the atom interpreter (r4xx+).
  645. */
  646. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  647. {
  648. struct radeon_device *rdev = info->dev->dev_private;
  649. WREG32_IO(reg*4, val);
  650. }
  651. /**
  652. * cail_ioreg_read - read IO register
  653. *
  654. * @info: atom card_info pointer
  655. * @reg: IO register offset
  656. *
  657. * Provides an IO register accessor for the atom interpreter (r4xx+).
  658. * Returns the value of the IO register.
  659. */
  660. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  661. {
  662. struct radeon_device *rdev = info->dev->dev_private;
  663. uint32_t r;
  664. r = RREG32_IO(reg*4);
  665. return r;
  666. }
  667. /**
  668. * radeon_atombios_init - init the driver info and callbacks for atombios
  669. *
  670. * @rdev: radeon_device pointer
  671. *
  672. * Initializes the driver info and register access callbacks for the
  673. * ATOM interpreter (r4xx+).
  674. * Returns 0 on sucess, -ENOMEM on failure.
  675. * Called at driver startup.
  676. */
  677. int radeon_atombios_init(struct radeon_device *rdev)
  678. {
  679. struct card_info *atom_card_info =
  680. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  681. if (!atom_card_info)
  682. return -ENOMEM;
  683. rdev->mode_info.atom_card_info = atom_card_info;
  684. atom_card_info->dev = rdev->ddev;
  685. atom_card_info->reg_read = cail_reg_read;
  686. atom_card_info->reg_write = cail_reg_write;
  687. /* needed for iio ops */
  688. if (rdev->rio_mem) {
  689. atom_card_info->ioreg_read = cail_ioreg_read;
  690. atom_card_info->ioreg_write = cail_ioreg_write;
  691. } else {
  692. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  693. atom_card_info->ioreg_read = cail_reg_read;
  694. atom_card_info->ioreg_write = cail_reg_write;
  695. }
  696. atom_card_info->mc_read = cail_mc_read;
  697. atom_card_info->mc_write = cail_mc_write;
  698. atom_card_info->pll_read = cail_pll_read;
  699. atom_card_info->pll_write = cail_pll_write;
  700. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  701. mutex_init(&rdev->mode_info.atom_context->mutex);
  702. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  703. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  704. return 0;
  705. }
  706. /**
  707. * radeon_atombios_fini - free the driver info and callbacks for atombios
  708. *
  709. * @rdev: radeon_device pointer
  710. *
  711. * Frees the driver info and register access callbacks for the ATOM
  712. * interpreter (r4xx+).
  713. * Called at driver shutdown.
  714. */
  715. void radeon_atombios_fini(struct radeon_device *rdev)
  716. {
  717. if (rdev->mode_info.atom_context) {
  718. kfree(rdev->mode_info.atom_context->scratch);
  719. kfree(rdev->mode_info.atom_context);
  720. }
  721. kfree(rdev->mode_info.atom_card_info);
  722. }
  723. /* COMBIOS */
  724. /*
  725. * COMBIOS is the bios format prior to ATOM. It provides
  726. * command tables similar to ATOM, but doesn't have a unified
  727. * parser. See radeon_combios.c
  728. */
  729. /**
  730. * radeon_combios_init - init the driver info for combios
  731. *
  732. * @rdev: radeon_device pointer
  733. *
  734. * Initializes the driver info for combios (r1xx-r3xx).
  735. * Returns 0 on sucess.
  736. * Called at driver startup.
  737. */
  738. int radeon_combios_init(struct radeon_device *rdev)
  739. {
  740. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  741. return 0;
  742. }
  743. /**
  744. * radeon_combios_fini - free the driver info for combios
  745. *
  746. * @rdev: radeon_device pointer
  747. *
  748. * Frees the driver info for combios (r1xx-r3xx).
  749. * Called at driver shutdown.
  750. */
  751. void radeon_combios_fini(struct radeon_device *rdev)
  752. {
  753. }
  754. /* if we get transitioned to only one device, take VGA back */
  755. /**
  756. * radeon_vga_set_decode - enable/disable vga decode
  757. *
  758. * @cookie: radeon_device pointer
  759. * @state: enable/disable vga decode
  760. *
  761. * Enable/disable vga decode (all asics).
  762. * Returns VGA resource flags.
  763. */
  764. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  765. {
  766. struct radeon_device *rdev = cookie;
  767. radeon_vga_set_state(rdev, state);
  768. if (state)
  769. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  770. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  771. else
  772. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  773. }
  774. /**
  775. * radeon_check_arguments - validate module params
  776. *
  777. * @rdev: radeon_device pointer
  778. *
  779. * Validates certain module parameters and updates
  780. * the associated values used by the driver (all asics).
  781. */
  782. static void radeon_check_arguments(struct radeon_device *rdev)
  783. {
  784. /* vramlimit must be a power of two */
  785. switch (radeon_vram_limit) {
  786. case 0:
  787. case 4:
  788. case 8:
  789. case 16:
  790. case 32:
  791. case 64:
  792. case 128:
  793. case 256:
  794. case 512:
  795. case 1024:
  796. case 2048:
  797. case 4096:
  798. break;
  799. default:
  800. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  801. radeon_vram_limit);
  802. radeon_vram_limit = 0;
  803. break;
  804. }
  805. radeon_vram_limit = radeon_vram_limit << 20;
  806. /* gtt size must be power of two and greater or equal to 32M */
  807. switch (radeon_gart_size) {
  808. case 4:
  809. case 8:
  810. case 16:
  811. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  812. radeon_gart_size);
  813. radeon_gart_size = 512;
  814. break;
  815. case 32:
  816. case 64:
  817. case 128:
  818. case 256:
  819. case 512:
  820. case 1024:
  821. case 2048:
  822. case 4096:
  823. break;
  824. default:
  825. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  826. radeon_gart_size);
  827. radeon_gart_size = 512;
  828. break;
  829. }
  830. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  831. /* AGP mode can only be -1, 1, 2, 4, 8 */
  832. switch (radeon_agpmode) {
  833. case -1:
  834. case 0:
  835. case 1:
  836. case 2:
  837. case 4:
  838. case 8:
  839. break;
  840. default:
  841. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  842. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  843. radeon_agpmode = 0;
  844. break;
  845. }
  846. }
  847. /**
  848. * radeon_switcheroo_set_state - set switcheroo state
  849. *
  850. * @pdev: pci dev pointer
  851. * @state: vga switcheroo state
  852. *
  853. * Callback for the switcheroo driver. Suspends or resumes the
  854. * the asics before or after it is powered up using ACPI methods.
  855. */
  856. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  857. {
  858. struct drm_device *dev = pci_get_drvdata(pdev);
  859. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  860. if (state == VGA_SWITCHEROO_ON) {
  861. printk(KERN_INFO "radeon: switched on\n");
  862. /* don't suspend or resume card normally */
  863. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  864. radeon_resume_kms(dev);
  865. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  866. drm_kms_helper_poll_enable(dev);
  867. } else {
  868. printk(KERN_INFO "radeon: switched off\n");
  869. drm_kms_helper_poll_disable(dev);
  870. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  871. radeon_suspend_kms(dev, pmm);
  872. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  873. }
  874. }
  875. /**
  876. * radeon_switcheroo_can_switch - see if switcheroo state can change
  877. *
  878. * @pdev: pci dev pointer
  879. *
  880. * Callback for the switcheroo driver. Check of the switcheroo
  881. * state can be changed.
  882. * Returns true if the state can be changed, false if not.
  883. */
  884. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  885. {
  886. struct drm_device *dev = pci_get_drvdata(pdev);
  887. bool can_switch;
  888. spin_lock(&dev->count_lock);
  889. can_switch = (dev->open_count == 0);
  890. spin_unlock(&dev->count_lock);
  891. return can_switch;
  892. }
  893. static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
  894. .set_gpu_state = radeon_switcheroo_set_state,
  895. .reprobe = NULL,
  896. .can_switch = radeon_switcheroo_can_switch,
  897. };
  898. /**
  899. * radeon_device_init - initialize the driver
  900. *
  901. * @rdev: radeon_device pointer
  902. * @pdev: drm dev pointer
  903. * @pdev: pci dev pointer
  904. * @flags: driver flags
  905. *
  906. * Initializes the driver info and hw (all asics).
  907. * Returns 0 for success or an error on failure.
  908. * Called at driver startup.
  909. */
  910. int radeon_device_init(struct radeon_device *rdev,
  911. struct drm_device *ddev,
  912. struct pci_dev *pdev,
  913. uint32_t flags)
  914. {
  915. int r, i;
  916. int dma_bits;
  917. rdev->shutdown = false;
  918. rdev->dev = &pdev->dev;
  919. rdev->ddev = ddev;
  920. rdev->pdev = pdev;
  921. rdev->flags = flags;
  922. rdev->family = flags & RADEON_FAMILY_MASK;
  923. rdev->is_atom_bios = false;
  924. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  925. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  926. rdev->accel_working = false;
  927. /* set up ring ids */
  928. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  929. rdev->ring[i].idx = i;
  930. }
  931. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
  932. radeon_family_name[rdev->family], pdev->vendor, pdev->device,
  933. pdev->subsystem_vendor, pdev->subsystem_device);
  934. /* mutex initialization are all done here so we
  935. * can recall function without having locking issues */
  936. mutex_init(&rdev->ring_lock);
  937. mutex_init(&rdev->dc_hw_i2c_mutex);
  938. atomic_set(&rdev->ih.lock, 0);
  939. mutex_init(&rdev->gem.mutex);
  940. mutex_init(&rdev->pm.mutex);
  941. mutex_init(&rdev->gpu_clock_mutex);
  942. init_rwsem(&rdev->pm.mclk_lock);
  943. init_rwsem(&rdev->exclusive_lock);
  944. init_waitqueue_head(&rdev->irq.vblank_queue);
  945. r = radeon_gem_init(rdev);
  946. if (r)
  947. return r;
  948. /* initialize vm here */
  949. mutex_init(&rdev->vm_manager.lock);
  950. /* Adjust VM size here.
  951. * Currently set to 4GB ((1 << 20) 4k pages).
  952. * Max GPUVM size for cayman and SI is 40 bits.
  953. */
  954. rdev->vm_manager.max_pfn = 1 << 20;
  955. INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
  956. /* Set asic functions */
  957. r = radeon_asic_init(rdev);
  958. if (r)
  959. return r;
  960. radeon_check_arguments(rdev);
  961. /* all of the newer IGP chips have an internal gart
  962. * However some rs4xx report as AGP, so remove that here.
  963. */
  964. if ((rdev->family >= CHIP_RS400) &&
  965. (rdev->flags & RADEON_IS_IGP)) {
  966. rdev->flags &= ~RADEON_IS_AGP;
  967. }
  968. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  969. radeon_agp_disable(rdev);
  970. }
  971. /* set DMA mask + need_dma32 flags.
  972. * PCIE - can handle 40-bits.
  973. * IGP - can handle 40-bits
  974. * AGP - generally dma32 is safest
  975. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  976. */
  977. rdev->need_dma32 = false;
  978. if (rdev->flags & RADEON_IS_AGP)
  979. rdev->need_dma32 = true;
  980. if ((rdev->flags & RADEON_IS_PCI) &&
  981. (rdev->family <= CHIP_RS740))
  982. rdev->need_dma32 = true;
  983. dma_bits = rdev->need_dma32 ? 32 : 40;
  984. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  985. if (r) {
  986. rdev->need_dma32 = true;
  987. dma_bits = 32;
  988. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  989. }
  990. r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  991. if (r) {
  992. pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
  993. printk(KERN_WARNING "radeon: No coherent DMA available.\n");
  994. }
  995. /* Registers mapping */
  996. /* TODO: block userspace mapping of io register */
  997. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  998. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  999. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  1000. if (rdev->rmmio == NULL) {
  1001. return -ENOMEM;
  1002. }
  1003. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  1004. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  1005. /* io port mapping */
  1006. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1007. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  1008. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  1009. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  1010. break;
  1011. }
  1012. }
  1013. if (rdev->rio_mem == NULL)
  1014. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1015. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  1016. /* this will fail for cards that aren't VGA class devices, just
  1017. * ignore it */
  1018. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  1019. vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops);
  1020. r = radeon_init(rdev);
  1021. if (r)
  1022. return r;
  1023. r = radeon_ib_ring_tests(rdev);
  1024. if (r)
  1025. DRM_ERROR("ib ring test failed (%d).\n", r);
  1026. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  1027. /* Acceleration not working on AGP card try again
  1028. * with fallback to PCI or PCIE GART
  1029. */
  1030. radeon_asic_reset(rdev);
  1031. radeon_fini(rdev);
  1032. radeon_agp_disable(rdev);
  1033. r = radeon_init(rdev);
  1034. if (r)
  1035. return r;
  1036. }
  1037. if ((radeon_testing & 1)) {
  1038. radeon_test_moves(rdev);
  1039. }
  1040. if ((radeon_testing & 2)) {
  1041. radeon_test_syncing(rdev);
  1042. }
  1043. if (radeon_benchmarking) {
  1044. radeon_benchmark(rdev, radeon_benchmarking);
  1045. }
  1046. return 0;
  1047. }
  1048. static void radeon_debugfs_remove_files(struct radeon_device *rdev);
  1049. /**
  1050. * radeon_device_fini - tear down the driver
  1051. *
  1052. * @rdev: radeon_device pointer
  1053. *
  1054. * Tear down the driver info (all asics).
  1055. * Called at driver shutdown.
  1056. */
  1057. void radeon_device_fini(struct radeon_device *rdev)
  1058. {
  1059. DRM_INFO("radeon: finishing device.\n");
  1060. rdev->shutdown = true;
  1061. /* evict vram memory */
  1062. radeon_bo_evict_vram(rdev);
  1063. radeon_fini(rdev);
  1064. vga_switcheroo_unregister_client(rdev->pdev);
  1065. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  1066. if (rdev->rio_mem)
  1067. pci_iounmap(rdev->pdev, rdev->rio_mem);
  1068. rdev->rio_mem = NULL;
  1069. iounmap(rdev->rmmio);
  1070. rdev->rmmio = NULL;
  1071. radeon_debugfs_remove_files(rdev);
  1072. }
  1073. /*
  1074. * Suspend & resume.
  1075. */
  1076. /**
  1077. * radeon_suspend_kms - initiate device suspend
  1078. *
  1079. * @pdev: drm dev pointer
  1080. * @state: suspend state
  1081. *
  1082. * Puts the hw in the suspend state (all asics).
  1083. * Returns 0 for success or an error on failure.
  1084. * Called at driver suspend.
  1085. */
  1086. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  1087. {
  1088. struct radeon_device *rdev;
  1089. struct drm_crtc *crtc;
  1090. struct drm_connector *connector;
  1091. int i, r;
  1092. if (dev == NULL || dev->dev_private == NULL) {
  1093. return -ENODEV;
  1094. }
  1095. if (state.event == PM_EVENT_PRETHAW) {
  1096. return 0;
  1097. }
  1098. rdev = dev->dev_private;
  1099. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1100. return 0;
  1101. drm_kms_helper_poll_disable(dev);
  1102. /* turn off display hw */
  1103. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1104. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1105. }
  1106. /* unpin the front buffers */
  1107. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1108. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  1109. struct radeon_bo *robj;
  1110. if (rfb == NULL || rfb->obj == NULL) {
  1111. continue;
  1112. }
  1113. robj = gem_to_radeon_bo(rfb->obj);
  1114. /* don't unpin kernel fb objects */
  1115. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  1116. r = radeon_bo_reserve(robj, false);
  1117. if (r == 0) {
  1118. radeon_bo_unpin(robj);
  1119. radeon_bo_unreserve(robj);
  1120. }
  1121. }
  1122. }
  1123. /* evict vram memory */
  1124. radeon_bo_evict_vram(rdev);
  1125. mutex_lock(&rdev->ring_lock);
  1126. /* wait for gpu to finish processing current batch */
  1127. for (i = 0; i < RADEON_NUM_RINGS; i++)
  1128. radeon_fence_wait_empty_locked(rdev, i);
  1129. mutex_unlock(&rdev->ring_lock);
  1130. radeon_save_bios_scratch_regs(rdev);
  1131. radeon_pm_suspend(rdev);
  1132. radeon_suspend(rdev);
  1133. radeon_hpd_fini(rdev);
  1134. /* evict remaining vram memory */
  1135. radeon_bo_evict_vram(rdev);
  1136. radeon_agp_suspend(rdev);
  1137. pci_save_state(dev->pdev);
  1138. if (state.event == PM_EVENT_SUSPEND) {
  1139. /* Shut down the device */
  1140. pci_disable_device(dev->pdev);
  1141. pci_set_power_state(dev->pdev, PCI_D3hot);
  1142. }
  1143. console_lock();
  1144. radeon_fbdev_set_suspend(rdev, 1);
  1145. console_unlock();
  1146. return 0;
  1147. }
  1148. /**
  1149. * radeon_resume_kms - initiate device resume
  1150. *
  1151. * @pdev: drm dev pointer
  1152. *
  1153. * Bring the hw back to operating state (all asics).
  1154. * Returns 0 for success or an error on failure.
  1155. * Called at driver resume.
  1156. */
  1157. int radeon_resume_kms(struct drm_device *dev)
  1158. {
  1159. struct drm_connector *connector;
  1160. struct radeon_device *rdev = dev->dev_private;
  1161. int r;
  1162. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1163. return 0;
  1164. console_lock();
  1165. pci_set_power_state(dev->pdev, PCI_D0);
  1166. pci_restore_state(dev->pdev);
  1167. if (pci_enable_device(dev->pdev)) {
  1168. console_unlock();
  1169. return -1;
  1170. }
  1171. /* resume AGP if in use */
  1172. radeon_agp_resume(rdev);
  1173. radeon_resume(rdev);
  1174. r = radeon_ib_ring_tests(rdev);
  1175. if (r)
  1176. DRM_ERROR("ib ring test failed (%d).\n", r);
  1177. radeon_pm_resume(rdev);
  1178. radeon_restore_bios_scratch_regs(rdev);
  1179. radeon_fbdev_set_suspend(rdev, 0);
  1180. console_unlock();
  1181. /* init dig PHYs, disp eng pll */
  1182. if (rdev->is_atom_bios) {
  1183. radeon_atom_encoder_init(rdev);
  1184. radeon_atom_disp_eng_pll_init(rdev);
  1185. /* turn on the BL */
  1186. if (rdev->mode_info.bl_encoder) {
  1187. u8 bl_level = radeon_get_backlight_level(rdev,
  1188. rdev->mode_info.bl_encoder);
  1189. radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
  1190. bl_level);
  1191. }
  1192. }
  1193. /* reset hpd state */
  1194. radeon_hpd_init(rdev);
  1195. /* blat the mode back in */
  1196. drm_helper_resume_force_mode(dev);
  1197. /* turn on display hw */
  1198. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1199. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1200. }
  1201. drm_kms_helper_poll_enable(dev);
  1202. return 0;
  1203. }
  1204. /**
  1205. * radeon_gpu_reset - reset the asic
  1206. *
  1207. * @rdev: radeon device pointer
  1208. *
  1209. * Attempt the reset the GPU if it has hung (all asics).
  1210. * Returns 0 for success or an error on failure.
  1211. */
  1212. int radeon_gpu_reset(struct radeon_device *rdev)
  1213. {
  1214. unsigned ring_sizes[RADEON_NUM_RINGS];
  1215. uint32_t *ring_data[RADEON_NUM_RINGS];
  1216. bool saved = false;
  1217. int i, r;
  1218. int resched;
  1219. down_write(&rdev->exclusive_lock);
  1220. radeon_save_bios_scratch_regs(rdev);
  1221. /* block TTM */
  1222. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  1223. radeon_suspend(rdev);
  1224. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1225. ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
  1226. &ring_data[i]);
  1227. if (ring_sizes[i]) {
  1228. saved = true;
  1229. dev_info(rdev->dev, "Saved %d dwords of commands "
  1230. "on ring %d.\n", ring_sizes[i], i);
  1231. }
  1232. }
  1233. retry:
  1234. r = radeon_asic_reset(rdev);
  1235. if (!r) {
  1236. dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
  1237. radeon_resume(rdev);
  1238. }
  1239. radeon_restore_bios_scratch_regs(rdev);
  1240. drm_helper_resume_force_mode(rdev->ddev);
  1241. if (!r) {
  1242. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1243. radeon_ring_restore(rdev, &rdev->ring[i],
  1244. ring_sizes[i], ring_data[i]);
  1245. ring_sizes[i] = 0;
  1246. ring_data[i] = NULL;
  1247. }
  1248. r = radeon_ib_ring_tests(rdev);
  1249. if (r) {
  1250. dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
  1251. if (saved) {
  1252. saved = false;
  1253. radeon_suspend(rdev);
  1254. goto retry;
  1255. }
  1256. }
  1257. } else {
  1258. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1259. kfree(ring_data[i]);
  1260. }
  1261. }
  1262. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  1263. if (r) {
  1264. /* bad news, how to tell it to userspace ? */
  1265. dev_info(rdev->dev, "GPU reset failed\n");
  1266. }
  1267. up_write(&rdev->exclusive_lock);
  1268. return r;
  1269. }
  1270. /*
  1271. * Debugfs
  1272. */
  1273. int radeon_debugfs_add_files(struct radeon_device *rdev,
  1274. struct drm_info_list *files,
  1275. unsigned nfiles)
  1276. {
  1277. unsigned i;
  1278. for (i = 0; i < rdev->debugfs_count; i++) {
  1279. if (rdev->debugfs[i].files == files) {
  1280. /* Already registered */
  1281. return 0;
  1282. }
  1283. }
  1284. i = rdev->debugfs_count + 1;
  1285. if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
  1286. DRM_ERROR("Reached maximum number of debugfs components.\n");
  1287. DRM_ERROR("Report so we increase "
  1288. "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
  1289. return -EINVAL;
  1290. }
  1291. rdev->debugfs[rdev->debugfs_count].files = files;
  1292. rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
  1293. rdev->debugfs_count = i;
  1294. #if defined(CONFIG_DEBUG_FS)
  1295. drm_debugfs_create_files(files, nfiles,
  1296. rdev->ddev->control->debugfs_root,
  1297. rdev->ddev->control);
  1298. drm_debugfs_create_files(files, nfiles,
  1299. rdev->ddev->primary->debugfs_root,
  1300. rdev->ddev->primary);
  1301. #endif
  1302. return 0;
  1303. }
  1304. static void radeon_debugfs_remove_files(struct radeon_device *rdev)
  1305. {
  1306. #if defined(CONFIG_DEBUG_FS)
  1307. unsigned i;
  1308. for (i = 0; i < rdev->debugfs_count; i++) {
  1309. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1310. rdev->debugfs[i].num_files,
  1311. rdev->ddev->control);
  1312. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1313. rdev->debugfs[i].num_files,
  1314. rdev->ddev->primary);
  1315. }
  1316. #endif
  1317. }
  1318. #if defined(CONFIG_DEBUG_FS)
  1319. int radeon_debugfs_init(struct drm_minor *minor)
  1320. {
  1321. return 0;
  1322. }
  1323. void radeon_debugfs_cleanup(struct drm_minor *minor)
  1324. {
  1325. }
  1326. #endif