radeon_device.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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/efi.h>
  29. #include <linux/pci.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/slab.h>
  32. #include <linux/vga_switcheroo.h>
  33. #include <linux/vgaarb.h>
  34. #include <drm/drm_cache.h>
  35. #include <drm/drm_client_event.h>
  36. #include <drm/drm_crtc_helper.h>
  37. #include <drm/drm_device.h>
  38. #include <drm/drm_file.h>
  39. #include <drm/drm_framebuffer.h>
  40. #include <drm/drm_probe_helper.h>
  41. #include <drm/radeon_drm.h>
  42. #include "radeon_device.h"
  43. #include "radeon_reg.h"
  44. #include "radeon.h"
  45. #include "atom.h"
  46. static const char radeon_family_name[][16] = {
  47. "R100",
  48. "RV100",
  49. "RS100",
  50. "RV200",
  51. "RS200",
  52. "R200",
  53. "RV250",
  54. "RS300",
  55. "RV280",
  56. "R300",
  57. "R350",
  58. "RV350",
  59. "RV380",
  60. "R420",
  61. "R423",
  62. "RV410",
  63. "RS400",
  64. "RS480",
  65. "RS600",
  66. "RS690",
  67. "RS740",
  68. "RV515",
  69. "R520",
  70. "RV530",
  71. "RV560",
  72. "RV570",
  73. "R580",
  74. "R600",
  75. "RV610",
  76. "RV630",
  77. "RV670",
  78. "RV620",
  79. "RV635",
  80. "RS780",
  81. "RS880",
  82. "RV770",
  83. "RV730",
  84. "RV710",
  85. "RV740",
  86. "CEDAR",
  87. "REDWOOD",
  88. "JUNIPER",
  89. "CYPRESS",
  90. "HEMLOCK",
  91. "PALM",
  92. "SUMO",
  93. "SUMO2",
  94. "BARTS",
  95. "TURKS",
  96. "CAICOS",
  97. "CAYMAN",
  98. "ARUBA",
  99. "TAHITI",
  100. "PITCAIRN",
  101. "VERDE",
  102. "OLAND",
  103. "HAINAN",
  104. "BONAIRE",
  105. "KAVERI",
  106. "KABINI",
  107. "HAWAII",
  108. "MULLINS",
  109. "LAST",
  110. };
  111. #if defined(CONFIG_VGA_SWITCHEROO)
  112. bool radeon_has_atpx_dgpu_power_cntl(void);
  113. bool radeon_is_atpx_hybrid(void);
  114. #else
  115. static inline bool radeon_has_atpx_dgpu_power_cntl(void) { return false; }
  116. static inline bool radeon_is_atpx_hybrid(void) { return false; }
  117. #endif
  118. #define RADEON_PX_QUIRK_DISABLE_PX (1 << 0)
  119. struct radeon_px_quirk {
  120. u32 chip_vendor;
  121. u32 chip_device;
  122. u32 subsys_vendor;
  123. u32 subsys_device;
  124. u32 px_quirk_flags;
  125. };
  126. static struct radeon_px_quirk radeon_px_quirk_list[] = {
  127. /* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
  128. * https://bugzilla.kernel.org/show_bug.cgi?id=74551
  129. */
  130. { PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
  131. /* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
  132. * https://bugzilla.kernel.org/show_bug.cgi?id=51381
  133. */
  134. { PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
  135. /* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
  136. * https://bugzilla.kernel.org/show_bug.cgi?id=51381
  137. */
  138. { PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
  139. /* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
  140. * https://bugs.freedesktop.org/show_bug.cgi?id=101491
  141. */
  142. { PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
  143. /* Asus K73TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
  144. * https://bugzilla.kernel.org/show_bug.cgi?id=51381#c52
  145. */
  146. { PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2123, RADEON_PX_QUIRK_DISABLE_PX },
  147. { 0, 0, 0, 0, 0 },
  148. };
  149. bool radeon_is_px(struct drm_device *dev)
  150. {
  151. struct radeon_device *rdev = dev->dev_private;
  152. if (rdev->flags & RADEON_IS_PX)
  153. return true;
  154. return false;
  155. }
  156. static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
  157. {
  158. struct radeon_px_quirk *p = radeon_px_quirk_list;
  159. /* Apply PX quirks */
  160. while (p && p->chip_device != 0) {
  161. if (rdev->pdev->vendor == p->chip_vendor &&
  162. rdev->pdev->device == p->chip_device &&
  163. rdev->pdev->subsystem_vendor == p->subsys_vendor &&
  164. rdev->pdev->subsystem_device == p->subsys_device) {
  165. rdev->px_quirk_flags = p->px_quirk_flags;
  166. break;
  167. }
  168. ++p;
  169. }
  170. if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
  171. rdev->flags &= ~RADEON_IS_PX;
  172. /* disable PX is the system doesn't support dGPU power control or hybrid gfx */
  173. if (!radeon_is_atpx_hybrid() &&
  174. !radeon_has_atpx_dgpu_power_cntl())
  175. rdev->flags &= ~RADEON_IS_PX;
  176. }
  177. /**
  178. * radeon_program_register_sequence - program an array of registers.
  179. *
  180. * @rdev: radeon_device pointer
  181. * @registers: pointer to the register array
  182. * @array_size: size of the register array
  183. *
  184. * Programs an array or registers with and and or masks.
  185. * This is a helper for setting golden registers.
  186. */
  187. void radeon_program_register_sequence(struct radeon_device *rdev,
  188. const u32 *registers,
  189. const u32 array_size)
  190. {
  191. u32 tmp, reg, and_mask, or_mask;
  192. int i;
  193. if (array_size % 3)
  194. return;
  195. for (i = 0; i < array_size; i +=3) {
  196. reg = registers[i + 0];
  197. and_mask = registers[i + 1];
  198. or_mask = registers[i + 2];
  199. if (and_mask == 0xffffffff) {
  200. tmp = or_mask;
  201. } else {
  202. tmp = RREG32(reg);
  203. tmp &= ~and_mask;
  204. tmp |= or_mask;
  205. }
  206. WREG32(reg, tmp);
  207. }
  208. }
  209. void radeon_pci_config_reset(struct radeon_device *rdev)
  210. {
  211. pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
  212. }
  213. /**
  214. * radeon_surface_init - Clear GPU surface registers.
  215. *
  216. * @rdev: radeon_device pointer
  217. *
  218. * Clear GPU surface registers (r1xx-r5xx).
  219. */
  220. void radeon_surface_init(struct radeon_device *rdev)
  221. {
  222. /* FIXME: check this out */
  223. if (rdev->family < CHIP_R600) {
  224. int i;
  225. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  226. if (rdev->surface_regs[i].bo)
  227. radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
  228. else
  229. radeon_clear_surface_reg(rdev, i);
  230. }
  231. /* enable surfaces */
  232. WREG32(RADEON_SURFACE_CNTL, 0);
  233. }
  234. }
  235. /*
  236. * GPU scratch registers helpers function.
  237. */
  238. /**
  239. * radeon_scratch_init - Init scratch register driver information.
  240. *
  241. * @rdev: radeon_device pointer
  242. *
  243. * Init CP scratch register driver information (r1xx-r5xx)
  244. */
  245. void radeon_scratch_init(struct radeon_device *rdev)
  246. {
  247. int i;
  248. /* FIXME: check this out */
  249. if (rdev->family < CHIP_R300) {
  250. rdev->scratch.num_reg = 5;
  251. } else {
  252. rdev->scratch.num_reg = 7;
  253. }
  254. rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
  255. for (i = 0; i < rdev->scratch.num_reg; i++) {
  256. rdev->scratch.free[i] = true;
  257. rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
  258. }
  259. }
  260. /**
  261. * radeon_scratch_get - Allocate a scratch register
  262. *
  263. * @rdev: radeon_device pointer
  264. * @reg: scratch register mmio offset
  265. *
  266. * Allocate a CP scratch register for use by the driver (all asics).
  267. * Returns 0 on success or -EINVAL on failure.
  268. */
  269. int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
  270. {
  271. int i;
  272. for (i = 0; i < rdev->scratch.num_reg; i++) {
  273. if (rdev->scratch.free[i]) {
  274. rdev->scratch.free[i] = false;
  275. *reg = rdev->scratch.reg[i];
  276. return 0;
  277. }
  278. }
  279. return -EINVAL;
  280. }
  281. /**
  282. * radeon_scratch_free - Free a scratch register
  283. *
  284. * @rdev: radeon_device pointer
  285. * @reg: scratch register mmio offset
  286. *
  287. * Free a CP scratch register allocated for use by the driver (all asics)
  288. */
  289. void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
  290. {
  291. int i;
  292. for (i = 0; i < rdev->scratch.num_reg; i++) {
  293. if (rdev->scratch.reg[i] == reg) {
  294. rdev->scratch.free[i] = true;
  295. return;
  296. }
  297. }
  298. }
  299. /*
  300. * GPU doorbell aperture helpers function.
  301. */
  302. /**
  303. * radeon_doorbell_init - Init doorbell driver information.
  304. *
  305. * @rdev: radeon_device pointer
  306. *
  307. * Init doorbell driver information (CIK)
  308. * Returns 0 on success, error on failure.
  309. */
  310. static int radeon_doorbell_init(struct radeon_device *rdev)
  311. {
  312. /* doorbell bar mapping */
  313. rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
  314. rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
  315. rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
  316. if (rdev->doorbell.num_doorbells == 0)
  317. return -EINVAL;
  318. rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
  319. if (rdev->doorbell.ptr == NULL) {
  320. return -ENOMEM;
  321. }
  322. DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
  323. DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
  324. memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
  325. return 0;
  326. }
  327. /**
  328. * radeon_doorbell_fini - Tear down doorbell driver information.
  329. *
  330. * @rdev: radeon_device pointer
  331. *
  332. * Tear down doorbell driver information (CIK)
  333. */
  334. static void radeon_doorbell_fini(struct radeon_device *rdev)
  335. {
  336. iounmap(rdev->doorbell.ptr);
  337. rdev->doorbell.ptr = NULL;
  338. }
  339. /**
  340. * radeon_doorbell_get - Allocate a doorbell entry
  341. *
  342. * @rdev: radeon_device pointer
  343. * @doorbell: doorbell index
  344. *
  345. * Allocate a doorbell for use by the driver (all asics).
  346. * Returns 0 on success or -EINVAL on failure.
  347. */
  348. int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
  349. {
  350. unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
  351. if (offset < rdev->doorbell.num_doorbells) {
  352. __set_bit(offset, rdev->doorbell.used);
  353. *doorbell = offset;
  354. return 0;
  355. } else {
  356. return -EINVAL;
  357. }
  358. }
  359. /**
  360. * radeon_doorbell_free - Free a doorbell entry
  361. *
  362. * @rdev: radeon_device pointer
  363. * @doorbell: doorbell index
  364. *
  365. * Free a doorbell allocated for use by the driver (all asics)
  366. */
  367. void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
  368. {
  369. if (doorbell < rdev->doorbell.num_doorbells)
  370. __clear_bit(doorbell, rdev->doorbell.used);
  371. }
  372. /*
  373. * radeon_wb_*()
  374. * Writeback is the method by which the GPU updates special pages
  375. * in memory with the status of certain GPU events (fences, ring pointers,
  376. * etc.).
  377. */
  378. /**
  379. * radeon_wb_disable - Disable Writeback
  380. *
  381. * @rdev: radeon_device pointer
  382. *
  383. * Disables Writeback (all asics). Used for suspend.
  384. */
  385. void radeon_wb_disable(struct radeon_device *rdev)
  386. {
  387. rdev->wb.enabled = false;
  388. }
  389. /**
  390. * radeon_wb_fini - Disable Writeback and free memory
  391. *
  392. * @rdev: radeon_device pointer
  393. *
  394. * Disables Writeback and frees the Writeback memory (all asics).
  395. * Used at driver shutdown.
  396. */
  397. void radeon_wb_fini(struct radeon_device *rdev)
  398. {
  399. radeon_wb_disable(rdev);
  400. if (rdev->wb.wb_obj) {
  401. if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
  402. radeon_bo_kunmap(rdev->wb.wb_obj);
  403. radeon_bo_unpin(rdev->wb.wb_obj);
  404. radeon_bo_unreserve(rdev->wb.wb_obj);
  405. }
  406. radeon_bo_unref(&rdev->wb.wb_obj);
  407. rdev->wb.wb = NULL;
  408. rdev->wb.wb_obj = NULL;
  409. }
  410. }
  411. /**
  412. * radeon_wb_init- Init Writeback driver info and allocate memory
  413. *
  414. * @rdev: radeon_device pointer
  415. *
  416. * Disables Writeback and frees the Writeback memory (all asics).
  417. * Used at driver startup.
  418. * Returns 0 on success or an -error on failure.
  419. */
  420. int radeon_wb_init(struct radeon_device *rdev)
  421. {
  422. int r;
  423. if (rdev->wb.wb_obj == NULL) {
  424. r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
  425. RADEON_GEM_DOMAIN_GTT, 0, NULL, NULL,
  426. &rdev->wb.wb_obj);
  427. if (r) {
  428. dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
  429. return r;
  430. }
  431. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  432. if (unlikely(r != 0)) {
  433. radeon_wb_fini(rdev);
  434. return r;
  435. }
  436. r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
  437. &rdev->wb.gpu_addr);
  438. if (r) {
  439. radeon_bo_unreserve(rdev->wb.wb_obj);
  440. dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
  441. radeon_wb_fini(rdev);
  442. return r;
  443. }
  444. r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
  445. radeon_bo_unreserve(rdev->wb.wb_obj);
  446. if (r) {
  447. dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
  448. radeon_wb_fini(rdev);
  449. return r;
  450. }
  451. }
  452. /* clear wb memory */
  453. memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
  454. /* disable event_write fences */
  455. rdev->wb.use_event = false;
  456. /* disabled via module param */
  457. if (radeon_no_wb == 1) {
  458. rdev->wb.enabled = false;
  459. } else {
  460. if (rdev->flags & RADEON_IS_AGP) {
  461. /* often unreliable on AGP */
  462. rdev->wb.enabled = false;
  463. } else if (rdev->family < CHIP_R300) {
  464. /* often unreliable on pre-r300 */
  465. rdev->wb.enabled = false;
  466. } else {
  467. rdev->wb.enabled = true;
  468. /* event_write fences are only available on r600+ */
  469. if (rdev->family >= CHIP_R600) {
  470. rdev->wb.use_event = true;
  471. }
  472. }
  473. }
  474. /* always use writeback/events on NI, APUs */
  475. if (rdev->family >= CHIP_PALM) {
  476. rdev->wb.enabled = true;
  477. rdev->wb.use_event = true;
  478. }
  479. dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
  480. return 0;
  481. }
  482. /**
  483. * radeon_vram_location - try to find VRAM location
  484. * @rdev: radeon device structure holding all necessary informations
  485. * @mc: memory controller structure holding memory informations
  486. * @base: base address at which to put VRAM
  487. *
  488. * Function will try to place VRAM at base address provided
  489. * as parameter (which is so far either PCI aperture address or
  490. * for IGP TOM base address).
  491. *
  492. * If there is not enough space to fit the unvisible VRAM in the 32bits
  493. * address space then we limit the VRAM size to the aperture.
  494. *
  495. * If we are using AGP and if the AGP aperture doesn't allow us to have
  496. * room for all the VRAM than we restrict the VRAM to the PCI aperture
  497. * size and print a warning.
  498. *
  499. * This function will never fails, worst case are limiting VRAM.
  500. *
  501. * Note: GTT start, end, size should be initialized before calling this
  502. * function on AGP platform.
  503. *
  504. * Note 1: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  505. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  506. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  507. * not IGP.
  508. *
  509. * Note 2: we use mc_vram_size as on some board we need to program the mc to
  510. * cover the whole aperture even if VRAM size is inferior to aperture size
  511. * Novell bug 204882 + along with lots of ubuntu ones
  512. *
  513. * Note 3: when limiting vram it's safe to overwrite real_vram_size because
  514. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  515. * not affected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  516. * ones)
  517. *
  518. * Note 4: IGP TOM addr should be the same as the aperture addr, we don't
  519. * explicitly check for that thought.
  520. *
  521. * FIXME: when reducing VRAM size, align new size on power of 2.
  522. */
  523. void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
  524. {
  525. uint64_t limit = (uint64_t)radeon_vram_limit << 20;
  526. mc->vram_start = base;
  527. if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
  528. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  529. mc->real_vram_size = mc->aper_size;
  530. mc->mc_vram_size = mc->aper_size;
  531. }
  532. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  533. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  534. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  535. mc->real_vram_size = mc->aper_size;
  536. mc->mc_vram_size = mc->aper_size;
  537. }
  538. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  539. if (limit && limit < mc->real_vram_size)
  540. mc->real_vram_size = limit;
  541. dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  542. mc->mc_vram_size >> 20, mc->vram_start,
  543. mc->vram_end, mc->real_vram_size >> 20);
  544. }
  545. /**
  546. * radeon_gtt_location - try to find GTT location
  547. * @rdev: radeon device structure holding all necessary informations
  548. * @mc: memory controller structure holding memory informations
  549. *
  550. * Function will try to place GTT before or after VRAM.
  551. *
  552. * If GTT size is bigger than space left then we ajust GTT size.
  553. * Thus function will never fails.
  554. *
  555. * FIXME: when reducing GTT size align new size on power of 2.
  556. */
  557. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  558. {
  559. u64 size_af, size_bf;
  560. size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  561. size_bf = mc->vram_start & ~mc->gtt_base_align;
  562. if (size_bf > size_af) {
  563. if (mc->gtt_size > size_bf) {
  564. dev_warn(rdev->dev, "limiting GTT\n");
  565. mc->gtt_size = size_bf;
  566. }
  567. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  568. } else {
  569. if (mc->gtt_size > size_af) {
  570. dev_warn(rdev->dev, "limiting GTT\n");
  571. mc->gtt_size = size_af;
  572. }
  573. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  574. }
  575. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  576. dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  577. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  578. }
  579. /*
  580. * GPU helpers function.
  581. */
  582. /*
  583. * radeon_device_is_virtual - check if we are running is a virtual environment
  584. *
  585. * Check if the asic has been passed through to a VM (all asics).
  586. * Used at driver startup.
  587. * Returns true if virtual or false if not.
  588. */
  589. bool radeon_device_is_virtual(void)
  590. {
  591. #ifdef CONFIG_X86
  592. return boot_cpu_has(X86_FEATURE_HYPERVISOR);
  593. #else
  594. return false;
  595. #endif
  596. }
  597. /**
  598. * radeon_card_posted - check if the hw has already been initialized
  599. *
  600. * @rdev: radeon_device pointer
  601. *
  602. * Check if the asic has been initialized (all asics).
  603. * Used at driver startup.
  604. * Returns true if initialized or false if not.
  605. */
  606. bool radeon_card_posted(struct radeon_device *rdev)
  607. {
  608. uint32_t reg;
  609. /* for pass through, always force asic_init for CI */
  610. if (rdev->family >= CHIP_BONAIRE &&
  611. radeon_device_is_virtual())
  612. return false;
  613. /* required for EFI mode on macbook2,1 which uses an r5xx asic */
  614. if (efi_enabled(EFI_BOOT) &&
  615. (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
  616. (rdev->family < CHIP_R600))
  617. return false;
  618. if (ASIC_IS_NODCE(rdev))
  619. goto check_memsize;
  620. /* first check CRTCs */
  621. if (ASIC_IS_DCE4(rdev)) {
  622. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  623. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  624. if (rdev->num_crtc >= 4) {
  625. reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  626. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
  627. }
  628. if (rdev->num_crtc >= 6) {
  629. reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  630. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  631. }
  632. if (reg & EVERGREEN_CRTC_MASTER_EN)
  633. return true;
  634. } else if (ASIC_IS_AVIVO(rdev)) {
  635. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  636. RREG32(AVIVO_D2CRTC_CONTROL);
  637. if (reg & AVIVO_CRTC_EN) {
  638. return true;
  639. }
  640. } else {
  641. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  642. RREG32(RADEON_CRTC2_GEN_CNTL);
  643. if (reg & RADEON_CRTC_EN) {
  644. return true;
  645. }
  646. }
  647. check_memsize:
  648. /* then check MEM_SIZE, in case the crtcs are off */
  649. if (rdev->family >= CHIP_R600)
  650. reg = RREG32(R600_CONFIG_MEMSIZE);
  651. else
  652. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  653. if (reg)
  654. return true;
  655. return false;
  656. }
  657. /**
  658. * radeon_update_bandwidth_info - update display bandwidth params
  659. *
  660. * @rdev: radeon_device pointer
  661. *
  662. * Used when sclk/mclk are switched or display modes are set.
  663. * params are used to calculate display watermarks (all asics)
  664. */
  665. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  666. {
  667. fixed20_12 a;
  668. u32 sclk = rdev->pm.current_sclk;
  669. u32 mclk = rdev->pm.current_mclk;
  670. /* sclk/mclk in Mhz */
  671. a.full = dfixed_const(100);
  672. rdev->pm.sclk.full = dfixed_const(sclk);
  673. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  674. rdev->pm.mclk.full = dfixed_const(mclk);
  675. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  676. if (rdev->flags & RADEON_IS_IGP) {
  677. a.full = dfixed_const(16);
  678. /* core_bandwidth = sclk(Mhz) * 16 */
  679. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  680. }
  681. }
  682. /**
  683. * radeon_boot_test_post_card - check and possibly initialize the hw
  684. *
  685. * @rdev: radeon_device pointer
  686. *
  687. * Check if the asic is initialized and if not, attempt to initialize
  688. * it (all asics).
  689. * Returns true if initialized or false if not.
  690. */
  691. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  692. {
  693. if (radeon_card_posted(rdev))
  694. return true;
  695. if (rdev->bios) {
  696. DRM_INFO("GPU not posted. posting now...\n");
  697. if (rdev->is_atom_bios)
  698. atom_asic_init(rdev->mode_info.atom_context);
  699. else
  700. radeon_combios_asic_init(rdev_to_drm(rdev));
  701. return true;
  702. } else {
  703. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  704. return false;
  705. }
  706. }
  707. /**
  708. * radeon_dummy_page_init - init dummy page used by the driver
  709. *
  710. * @rdev: radeon_device pointer
  711. *
  712. * Allocate the dummy page used by the driver (all asics).
  713. * This dummy page is used by the driver as a filler for gart entries
  714. * when pages are taken out of the GART
  715. * Returns 0 on sucess, -ENOMEM on failure.
  716. */
  717. int radeon_dummy_page_init(struct radeon_device *rdev)
  718. {
  719. if (rdev->dummy_page.page)
  720. return 0;
  721. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  722. if (rdev->dummy_page.page == NULL)
  723. return -ENOMEM;
  724. rdev->dummy_page.addr = dma_map_page(&rdev->pdev->dev, rdev->dummy_page.page,
  725. 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
  726. if (dma_mapping_error(&rdev->pdev->dev, rdev->dummy_page.addr)) {
  727. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  728. __free_page(rdev->dummy_page.page);
  729. rdev->dummy_page.page = NULL;
  730. return -ENOMEM;
  731. }
  732. rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
  733. RADEON_GART_PAGE_DUMMY);
  734. return 0;
  735. }
  736. /**
  737. * radeon_dummy_page_fini - free dummy page used by the driver
  738. *
  739. * @rdev: radeon_device pointer
  740. *
  741. * Frees the dummy page used by the driver (all asics).
  742. */
  743. void radeon_dummy_page_fini(struct radeon_device *rdev)
  744. {
  745. if (rdev->dummy_page.page == NULL)
  746. return;
  747. dma_unmap_page(&rdev->pdev->dev, rdev->dummy_page.addr, PAGE_SIZE,
  748. DMA_BIDIRECTIONAL);
  749. __free_page(rdev->dummy_page.page);
  750. rdev->dummy_page.page = NULL;
  751. }
  752. /* ATOM accessor methods */
  753. /*
  754. * ATOM is an interpreted byte code stored in tables in the vbios. The
  755. * driver registers callbacks to access registers and the interpreter
  756. * in the driver parses the tables and executes then to program specific
  757. * actions (set display modes, asic init, etc.). See radeon_atombios.c,
  758. * atombios.h, and atom.c
  759. */
  760. /**
  761. * cail_pll_read - read PLL register
  762. *
  763. * @info: atom card_info pointer
  764. * @reg: PLL register offset
  765. *
  766. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  767. * Returns the value of the PLL register.
  768. */
  769. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  770. {
  771. struct radeon_device *rdev = info->dev->dev_private;
  772. uint32_t r;
  773. r = rdev->pll_rreg(rdev, reg);
  774. return r;
  775. }
  776. /**
  777. * cail_pll_write - write PLL register
  778. *
  779. * @info: atom card_info pointer
  780. * @reg: PLL register offset
  781. * @val: value to write to the pll register
  782. *
  783. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  784. */
  785. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  786. {
  787. struct radeon_device *rdev = info->dev->dev_private;
  788. rdev->pll_wreg(rdev, reg, val);
  789. }
  790. /**
  791. * cail_mc_read - read MC (Memory Controller) register
  792. *
  793. * @info: atom card_info pointer
  794. * @reg: MC register offset
  795. *
  796. * Provides an MC register accessor for the atom interpreter (r4xx+).
  797. * Returns the value of the MC register.
  798. */
  799. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  800. {
  801. struct radeon_device *rdev = info->dev->dev_private;
  802. uint32_t r;
  803. r = rdev->mc_rreg(rdev, reg);
  804. return r;
  805. }
  806. /**
  807. * cail_mc_write - write MC (Memory Controller) register
  808. *
  809. * @info: atom card_info pointer
  810. * @reg: MC register offset
  811. * @val: value to write to the pll register
  812. *
  813. * Provides a MC register accessor for the atom interpreter (r4xx+).
  814. */
  815. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  816. {
  817. struct radeon_device *rdev = info->dev->dev_private;
  818. rdev->mc_wreg(rdev, reg, val);
  819. }
  820. /**
  821. * cail_reg_write - write MMIO register
  822. *
  823. * @info: atom card_info pointer
  824. * @reg: MMIO register offset
  825. * @val: value to write to the pll register
  826. *
  827. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  828. */
  829. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  830. {
  831. struct radeon_device *rdev = info->dev->dev_private;
  832. WREG32(reg*4, val);
  833. }
  834. /**
  835. * cail_reg_read - read MMIO register
  836. *
  837. * @info: atom card_info pointer
  838. * @reg: MMIO register offset
  839. *
  840. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  841. * Returns the value of the MMIO register.
  842. */
  843. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  844. {
  845. struct radeon_device *rdev = info->dev->dev_private;
  846. uint32_t r;
  847. r = RREG32(reg*4);
  848. return r;
  849. }
  850. /**
  851. * cail_ioreg_write - write IO register
  852. *
  853. * @info: atom card_info pointer
  854. * @reg: IO register offset
  855. * @val: value to write to the pll register
  856. *
  857. * Provides a IO register accessor for the atom interpreter (r4xx+).
  858. */
  859. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  860. {
  861. struct radeon_device *rdev = info->dev->dev_private;
  862. WREG32_IO(reg*4, val);
  863. }
  864. /**
  865. * cail_ioreg_read - read IO register
  866. *
  867. * @info: atom card_info pointer
  868. * @reg: IO register offset
  869. *
  870. * Provides an IO register accessor for the atom interpreter (r4xx+).
  871. * Returns the value of the IO register.
  872. */
  873. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  874. {
  875. struct radeon_device *rdev = info->dev->dev_private;
  876. uint32_t r;
  877. r = RREG32_IO(reg*4);
  878. return r;
  879. }
  880. /**
  881. * radeon_atombios_init - init the driver info and callbacks for atombios
  882. *
  883. * @rdev: radeon_device pointer
  884. *
  885. * Initializes the driver info and register access callbacks for the
  886. * ATOM interpreter (r4xx+).
  887. * Returns 0 on sucess, -ENOMEM on failure.
  888. * Called at driver startup.
  889. */
  890. int radeon_atombios_init(struct radeon_device *rdev)
  891. {
  892. struct card_info *atom_card_info =
  893. kzalloc_obj(struct card_info);
  894. if (!atom_card_info)
  895. return -ENOMEM;
  896. rdev->mode_info.atom_card_info = atom_card_info;
  897. atom_card_info->dev = rdev_to_drm(rdev);
  898. atom_card_info->reg_read = cail_reg_read;
  899. atom_card_info->reg_write = cail_reg_write;
  900. /* needed for iio ops */
  901. if (rdev->rio_mem) {
  902. atom_card_info->ioreg_read = cail_ioreg_read;
  903. atom_card_info->ioreg_write = cail_ioreg_write;
  904. } else {
  905. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  906. atom_card_info->ioreg_read = cail_reg_read;
  907. atom_card_info->ioreg_write = cail_reg_write;
  908. }
  909. atom_card_info->mc_read = cail_mc_read;
  910. atom_card_info->mc_write = cail_mc_write;
  911. atom_card_info->pll_read = cail_pll_read;
  912. atom_card_info->pll_write = cail_pll_write;
  913. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  914. if (!rdev->mode_info.atom_context) {
  915. radeon_atombios_fini(rdev);
  916. return -ENOMEM;
  917. }
  918. mutex_init(&rdev->mode_info.atom_context->mutex);
  919. mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
  920. radeon_atom_initialize_bios_scratch_regs(rdev_to_drm(rdev));
  921. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  922. return 0;
  923. }
  924. /**
  925. * radeon_atombios_fini - free the driver info and callbacks for atombios
  926. *
  927. * @rdev: radeon_device pointer
  928. *
  929. * Frees the driver info and register access callbacks for the ATOM
  930. * interpreter (r4xx+).
  931. * Called at driver shutdown.
  932. */
  933. void radeon_atombios_fini(struct radeon_device *rdev)
  934. {
  935. if (rdev->mode_info.atom_context) {
  936. kfree(rdev->mode_info.atom_context->scratch);
  937. kfree(rdev->mode_info.atom_context->iio);
  938. }
  939. kfree(rdev->mode_info.atom_context);
  940. rdev->mode_info.atom_context = NULL;
  941. kfree(rdev->mode_info.atom_card_info);
  942. rdev->mode_info.atom_card_info = NULL;
  943. }
  944. /* COMBIOS */
  945. /*
  946. * COMBIOS is the bios format prior to ATOM. It provides
  947. * command tables similar to ATOM, but doesn't have a unified
  948. * parser. See radeon_combios.c
  949. */
  950. /**
  951. * radeon_combios_init - init the driver info for combios
  952. *
  953. * @rdev: radeon_device pointer
  954. *
  955. * Initializes the driver info for combios (r1xx-r3xx).
  956. * Returns 0 on sucess.
  957. * Called at driver startup.
  958. */
  959. int radeon_combios_init(struct radeon_device *rdev)
  960. {
  961. radeon_combios_initialize_bios_scratch_regs(rdev_to_drm(rdev));
  962. return 0;
  963. }
  964. /**
  965. * radeon_combios_fini - free the driver info for combios
  966. *
  967. * @rdev: radeon_device pointer
  968. *
  969. * Frees the driver info for combios (r1xx-r3xx).
  970. * Called at driver shutdown.
  971. */
  972. void radeon_combios_fini(struct radeon_device *rdev)
  973. {
  974. }
  975. /* if we get transitioned to only one device, take VGA back */
  976. /**
  977. * radeon_vga_set_decode - enable/disable vga decode
  978. *
  979. * @pdev: PCI device
  980. * @state: enable/disable vga decode
  981. *
  982. * Enable/disable vga decode (all asics).
  983. * Returns VGA resource flags.
  984. */
  985. static unsigned int radeon_vga_set_decode(struct pci_dev *pdev, bool state)
  986. {
  987. struct drm_device *dev = pci_get_drvdata(pdev);
  988. struct radeon_device *rdev = dev->dev_private;
  989. radeon_vga_set_state(rdev, state);
  990. if (state)
  991. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  992. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  993. else
  994. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  995. }
  996. /**
  997. * radeon_gart_size_auto - Determine a sensible default GART size
  998. * according to ASIC family.
  999. *
  1000. * @family: ASIC family name
  1001. */
  1002. static int radeon_gart_size_auto(enum radeon_family family)
  1003. {
  1004. /* default to a larger gart size on newer asics */
  1005. if (family >= CHIP_TAHITI)
  1006. return 2048;
  1007. else if (family >= CHIP_RV770)
  1008. return 1024;
  1009. else
  1010. return 512;
  1011. }
  1012. /**
  1013. * radeon_check_arguments - validate module params
  1014. *
  1015. * @rdev: radeon_device pointer
  1016. *
  1017. * Validates certain module parameters and updates
  1018. * the associated values used by the driver (all asics).
  1019. */
  1020. static void radeon_check_arguments(struct radeon_device *rdev)
  1021. {
  1022. /* vramlimit must be a power of two */
  1023. if (radeon_vram_limit != 0 && !is_power_of_2(radeon_vram_limit)) {
  1024. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  1025. radeon_vram_limit);
  1026. radeon_vram_limit = 0;
  1027. }
  1028. if (radeon_gart_size == -1) {
  1029. radeon_gart_size = radeon_gart_size_auto(rdev->family);
  1030. }
  1031. /* gtt size must be power of two and greater or equal to 32M */
  1032. if (radeon_gart_size < 32) {
  1033. dev_warn(rdev->dev, "gart size (%d) too small\n",
  1034. radeon_gart_size);
  1035. radeon_gart_size = radeon_gart_size_auto(rdev->family);
  1036. } else if (!is_power_of_2(radeon_gart_size)) {
  1037. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  1038. radeon_gart_size);
  1039. radeon_gart_size = radeon_gart_size_auto(rdev->family);
  1040. }
  1041. rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
  1042. /* AGP mode can only be -1, 1, 2, 4, 8 */
  1043. switch (radeon_agpmode) {
  1044. case -1:
  1045. case 0:
  1046. case 1:
  1047. case 2:
  1048. case 4:
  1049. case 8:
  1050. break;
  1051. default:
  1052. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  1053. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  1054. radeon_agpmode = 0;
  1055. break;
  1056. }
  1057. if (!is_power_of_2(radeon_vm_size)) {
  1058. dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
  1059. radeon_vm_size);
  1060. radeon_vm_size = 4;
  1061. }
  1062. if (radeon_vm_size < 1) {
  1063. dev_warn(rdev->dev, "VM size (%d) too small, min is 1GB\n",
  1064. radeon_vm_size);
  1065. radeon_vm_size = 4;
  1066. }
  1067. /*
  1068. * Max GPUVM size for Cayman, SI and CI are 40 bits.
  1069. */
  1070. if (radeon_vm_size > 1024) {
  1071. dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
  1072. radeon_vm_size);
  1073. radeon_vm_size = 4;
  1074. }
  1075. /* defines number of bits in page table versus page directory,
  1076. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  1077. * page table and the remaining bits are in the page directory */
  1078. if (radeon_vm_block_size == -1) {
  1079. /* Total bits covered by PD + PTs */
  1080. unsigned bits = ilog2(radeon_vm_size) + 18;
  1081. /* Make sure the PD is 4K in size up to 8GB address space.
  1082. Above that split equal between PD and PTs */
  1083. if (radeon_vm_size <= 8)
  1084. radeon_vm_block_size = bits - 9;
  1085. else
  1086. radeon_vm_block_size = (bits + 3) / 2;
  1087. } else if (radeon_vm_block_size < 9) {
  1088. dev_warn(rdev->dev, "VM page table size (%d) too small\n",
  1089. radeon_vm_block_size);
  1090. radeon_vm_block_size = 9;
  1091. }
  1092. if (radeon_vm_block_size > 24 ||
  1093. (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
  1094. dev_warn(rdev->dev, "VM page table size (%d) too large\n",
  1095. radeon_vm_block_size);
  1096. radeon_vm_block_size = 9;
  1097. }
  1098. }
  1099. /**
  1100. * radeon_switcheroo_set_state - set switcheroo state
  1101. *
  1102. * @pdev: pci dev pointer
  1103. * @state: vga_switcheroo state
  1104. *
  1105. * Callback for the switcheroo driver. Suspends or resumes
  1106. * the asics before or after it is powered up using ACPI methods.
  1107. */
  1108. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  1109. {
  1110. struct drm_device *dev = pci_get_drvdata(pdev);
  1111. if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  1112. return;
  1113. if (state == VGA_SWITCHEROO_ON) {
  1114. pr_info("radeon: switched on\n");
  1115. /* don't suspend or resume card normally */
  1116. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1117. radeon_resume_kms(dev, true, true);
  1118. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  1119. drm_kms_helper_poll_enable(dev);
  1120. } else {
  1121. pr_info("radeon: switched off\n");
  1122. drm_kms_helper_poll_disable(dev);
  1123. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1124. radeon_suspend_kms(dev, true, true, false);
  1125. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  1126. }
  1127. }
  1128. /**
  1129. * radeon_switcheroo_can_switch - see if switcheroo state can change
  1130. *
  1131. * @pdev: pci dev pointer
  1132. *
  1133. * Callback for the switcheroo driver. Check of the switcheroo
  1134. * state can be changed.
  1135. * Returns true if the state can be changed, false if not.
  1136. */
  1137. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  1138. {
  1139. struct drm_device *dev = pci_get_drvdata(pdev);
  1140. /*
  1141. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  1142. * locking inversion with the driver load path. And the access here is
  1143. * completely racy anyway. So don't bother with locking for now.
  1144. */
  1145. return atomic_read(&dev->open_count) == 0;
  1146. }
  1147. static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
  1148. .set_gpu_state = radeon_switcheroo_set_state,
  1149. .reprobe = NULL,
  1150. .can_switch = radeon_switcheroo_can_switch,
  1151. };
  1152. /**
  1153. * radeon_device_init - initialize the driver
  1154. *
  1155. * @rdev: radeon_device pointer
  1156. * @ddev: drm dev pointer
  1157. * @pdev: pci dev pointer
  1158. * @flags: driver flags
  1159. *
  1160. * Initializes the driver info and hw (all asics).
  1161. * Returns 0 for success or an error on failure.
  1162. * Called at driver startup.
  1163. */
  1164. int radeon_device_init(struct radeon_device *rdev,
  1165. struct drm_device *ddev,
  1166. struct pci_dev *pdev,
  1167. uint32_t flags)
  1168. {
  1169. int r, i;
  1170. int dma_bits;
  1171. bool runtime = false;
  1172. rdev->shutdown = false;
  1173. rdev->flags = flags;
  1174. rdev->family = flags & RADEON_FAMILY_MASK;
  1175. rdev->is_atom_bios = false;
  1176. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  1177. rdev->mc.gtt_size = 512 * 1024 * 1024;
  1178. rdev->accel_working = false;
  1179. /* set up ring ids */
  1180. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  1181. rdev->ring[i].idx = i;
  1182. }
  1183. rdev->fence_context = dma_fence_context_alloc(RADEON_NUM_RINGS);
  1184. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  1185. radeon_family_name[rdev->family], pdev->vendor, pdev->device,
  1186. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  1187. /* mutex initialization are all done here so we
  1188. * can recall function without having locking issues */
  1189. mutex_init(&rdev->ring_lock);
  1190. mutex_init(&rdev->dc_hw_i2c_mutex);
  1191. atomic_set(&rdev->ih.lock, 0);
  1192. mutex_init(&rdev->gem.mutex);
  1193. mutex_init(&rdev->pm.mutex);
  1194. mutex_init(&rdev->gpu_clock_mutex);
  1195. mutex_init(&rdev->srbm_mutex);
  1196. mutex_init(&rdev->audio.component_mutex);
  1197. init_rwsem(&rdev->pm.mclk_lock);
  1198. init_rwsem(&rdev->exclusive_lock);
  1199. init_waitqueue_head(&rdev->irq.vblank_queue);
  1200. r = radeon_gem_init(rdev);
  1201. if (r)
  1202. return r;
  1203. radeon_check_arguments(rdev);
  1204. /* Adjust VM size here.
  1205. * Max GPUVM size for cayman+ is 40 bits.
  1206. */
  1207. rdev->vm_manager.max_pfn = radeon_vm_size << 18;
  1208. /* Set asic functions */
  1209. r = radeon_asic_init(rdev);
  1210. if (r)
  1211. return r;
  1212. /* all of the newer IGP chips have an internal gart
  1213. * However some rs4xx report as AGP, so remove that here.
  1214. */
  1215. if ((rdev->family >= CHIP_RS400) &&
  1216. (rdev->flags & RADEON_IS_IGP)) {
  1217. rdev->flags &= ~RADEON_IS_AGP;
  1218. }
  1219. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  1220. radeon_agp_disable(rdev);
  1221. }
  1222. /* Set the internal MC address mask
  1223. * This is the max address of the GPU's
  1224. * internal address space.
  1225. */
  1226. if (rdev->family >= CHIP_CAYMAN)
  1227. rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
  1228. else if (rdev->family >= CHIP_CEDAR)
  1229. rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
  1230. else
  1231. rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
  1232. /* set DMA mask.
  1233. * PCIE - can handle 40-bits.
  1234. * IGP - can handle 40-bits
  1235. * AGP - generally dma32 is safest
  1236. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  1237. */
  1238. dma_bits = 40;
  1239. if (rdev->flags & RADEON_IS_AGP)
  1240. dma_bits = 32;
  1241. if ((rdev->flags & RADEON_IS_PCI) &&
  1242. (rdev->family <= CHIP_RS740))
  1243. dma_bits = 32;
  1244. #ifdef CONFIG_PPC64
  1245. if (rdev->family == CHIP_CEDAR)
  1246. dma_bits = 32;
  1247. #endif
  1248. r = dma_set_mask_and_coherent(&rdev->pdev->dev, DMA_BIT_MASK(dma_bits));
  1249. if (r) {
  1250. pr_warn("radeon: No suitable DMA available\n");
  1251. return r;
  1252. }
  1253. rdev->pdev->msi_addr_mask = DMA_BIT_MASK(dma_bits);
  1254. rdev->need_swiotlb = drm_need_swiotlb(dma_bits);
  1255. /* Registers mapping */
  1256. /* TODO: block userspace mapping of io register */
  1257. spin_lock_init(&rdev->mmio_idx_lock);
  1258. spin_lock_init(&rdev->smc_idx_lock);
  1259. spin_lock_init(&rdev->pll_idx_lock);
  1260. spin_lock_init(&rdev->mc_idx_lock);
  1261. spin_lock_init(&rdev->pcie_idx_lock);
  1262. spin_lock_init(&rdev->pciep_idx_lock);
  1263. spin_lock_init(&rdev->pif_idx_lock);
  1264. spin_lock_init(&rdev->cg_idx_lock);
  1265. spin_lock_init(&rdev->uvd_idx_lock);
  1266. spin_lock_init(&rdev->rcu_idx_lock);
  1267. spin_lock_init(&rdev->didt_idx_lock);
  1268. spin_lock_init(&rdev->end_idx_lock);
  1269. if (rdev->family >= CHIP_BONAIRE) {
  1270. rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
  1271. rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
  1272. } else {
  1273. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  1274. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  1275. }
  1276. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  1277. if (rdev->rmmio == NULL)
  1278. return -ENOMEM;
  1279. /* doorbell bar mapping */
  1280. if (rdev->family >= CHIP_BONAIRE)
  1281. radeon_doorbell_init(rdev);
  1282. /* io port mapping */
  1283. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1284. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  1285. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  1286. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  1287. break;
  1288. }
  1289. }
  1290. if (rdev->rio_mem == NULL)
  1291. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1292. if (rdev->flags & RADEON_IS_PX)
  1293. radeon_device_handle_px_quirks(rdev);
  1294. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  1295. /* this will fail for cards that aren't VGA class devices, just
  1296. * ignore it */
  1297. vga_client_register(rdev->pdev, radeon_vga_set_decode);
  1298. if (rdev->flags & RADEON_IS_PX)
  1299. runtime = true;
  1300. if (!pci_is_thunderbolt_attached(rdev->pdev))
  1301. vga_switcheroo_register_client(rdev->pdev,
  1302. &radeon_switcheroo_ops, runtime);
  1303. if (runtime)
  1304. vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
  1305. r = radeon_init(rdev);
  1306. if (r)
  1307. goto failed;
  1308. radeon_gem_debugfs_init(rdev);
  1309. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  1310. /* Acceleration not working on AGP card try again
  1311. * with fallback to PCI or PCIE GART
  1312. */
  1313. radeon_asic_reset(rdev);
  1314. radeon_fini(rdev);
  1315. radeon_agp_disable(rdev);
  1316. r = radeon_init(rdev);
  1317. if (r)
  1318. goto failed;
  1319. }
  1320. radeon_audio_component_init(rdev);
  1321. r = radeon_ib_ring_tests(rdev);
  1322. if (r)
  1323. DRM_ERROR("ib ring test failed (%d).\n", r);
  1324. /*
  1325. * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
  1326. * after the CP ring have chew one packet at least. Hence here we stop
  1327. * and restart DPM after the radeon_ib_ring_tests().
  1328. */
  1329. if (rdev->pm.dpm_enabled &&
  1330. (rdev->pm.pm_method == PM_METHOD_DPM) &&
  1331. (rdev->family == CHIP_TURKS) &&
  1332. (rdev->flags & RADEON_IS_MOBILITY)) {
  1333. mutex_lock(&rdev->pm.mutex);
  1334. radeon_dpm_disable(rdev);
  1335. radeon_dpm_enable(rdev);
  1336. mutex_unlock(&rdev->pm.mutex);
  1337. }
  1338. if ((radeon_testing & 1)) {
  1339. if (rdev->accel_working)
  1340. radeon_test_moves(rdev);
  1341. else
  1342. DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
  1343. }
  1344. if ((radeon_testing & 2)) {
  1345. if (rdev->accel_working)
  1346. radeon_test_syncing(rdev);
  1347. else
  1348. DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
  1349. }
  1350. if (radeon_benchmarking) {
  1351. if (rdev->accel_working)
  1352. radeon_benchmark(rdev, radeon_benchmarking);
  1353. else
  1354. DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
  1355. }
  1356. return 0;
  1357. failed:
  1358. /* balance pm_runtime_get_sync() in radeon_driver_unload_kms() */
  1359. if (radeon_is_px(ddev))
  1360. pm_runtime_put_noidle(ddev->dev);
  1361. if (runtime)
  1362. vga_switcheroo_fini_domain_pm_ops(rdev->dev);
  1363. return r;
  1364. }
  1365. /**
  1366. * radeon_device_fini - tear down the driver
  1367. *
  1368. * @rdev: radeon_device pointer
  1369. *
  1370. * Tear down the driver info (all asics).
  1371. * Called at driver shutdown.
  1372. */
  1373. void radeon_device_fini(struct radeon_device *rdev)
  1374. {
  1375. DRM_INFO("radeon: finishing device.\n");
  1376. rdev->shutdown = true;
  1377. /* evict vram memory */
  1378. radeon_bo_evict_vram(rdev);
  1379. radeon_audio_component_fini(rdev);
  1380. radeon_fini(rdev);
  1381. if (!pci_is_thunderbolt_attached(rdev->pdev))
  1382. vga_switcheroo_unregister_client(rdev->pdev);
  1383. if (rdev->flags & RADEON_IS_PX)
  1384. vga_switcheroo_fini_domain_pm_ops(rdev->dev);
  1385. vga_client_unregister(rdev->pdev);
  1386. if (rdev->rio_mem)
  1387. pci_iounmap(rdev->pdev, rdev->rio_mem);
  1388. rdev->rio_mem = NULL;
  1389. iounmap(rdev->rmmio);
  1390. rdev->rmmio = NULL;
  1391. if (rdev->family >= CHIP_BONAIRE)
  1392. radeon_doorbell_fini(rdev);
  1393. }
  1394. /*
  1395. * Suspend & resume.
  1396. */
  1397. /*
  1398. * radeon_suspend_kms - initiate device suspend
  1399. *
  1400. * Puts the hw in the suspend state (all asics).
  1401. * Returns 0 for success or an error on failure.
  1402. * Called at driver suspend.
  1403. */
  1404. int radeon_suspend_kms(struct drm_device *dev, bool suspend,
  1405. bool notify_clients, bool freeze)
  1406. {
  1407. struct radeon_device *rdev;
  1408. struct pci_dev *pdev;
  1409. struct drm_crtc *crtc;
  1410. struct drm_connector *connector;
  1411. int i, r;
  1412. if (dev == NULL || dev->dev_private == NULL) {
  1413. return -ENODEV;
  1414. }
  1415. rdev = dev->dev_private;
  1416. pdev = to_pci_dev(dev->dev);
  1417. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1418. return 0;
  1419. drm_kms_helper_poll_disable(dev);
  1420. drm_modeset_lock_all(dev);
  1421. /* turn off display hw */
  1422. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1423. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1424. }
  1425. drm_modeset_unlock_all(dev);
  1426. /* unpin the front buffers and cursors */
  1427. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1428. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1429. struct drm_framebuffer *fb = crtc->primary->fb;
  1430. struct radeon_bo *robj;
  1431. if (radeon_crtc->cursor_bo) {
  1432. struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
  1433. r = radeon_bo_reserve(robj, false);
  1434. if (r == 0) {
  1435. radeon_bo_unpin(robj);
  1436. radeon_bo_unreserve(robj);
  1437. }
  1438. }
  1439. if (fb == NULL || fb->obj[0] == NULL) {
  1440. continue;
  1441. }
  1442. robj = gem_to_radeon_bo(fb->obj[0]);
  1443. /* don't unpin kernel fb objects */
  1444. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  1445. r = radeon_bo_reserve(robj, false);
  1446. if (r == 0) {
  1447. radeon_bo_unpin(robj);
  1448. radeon_bo_unreserve(robj);
  1449. }
  1450. }
  1451. }
  1452. /* evict vram memory */
  1453. radeon_bo_evict_vram(rdev);
  1454. /* wait for gpu to finish processing current batch */
  1455. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  1456. r = radeon_fence_wait_empty(rdev, i);
  1457. if (r) {
  1458. /* delay GPU reset to resume */
  1459. radeon_fence_driver_force_completion(rdev, i);
  1460. } else {
  1461. /* finish executing delayed work */
  1462. flush_delayed_work(&rdev->fence_drv[i].lockup_work);
  1463. }
  1464. }
  1465. radeon_save_bios_scratch_regs(rdev);
  1466. radeon_suspend(rdev);
  1467. radeon_hpd_fini(rdev);
  1468. /* evict remaining vram memory
  1469. * This second call to evict vram is to evict the gart page table
  1470. * using the CPU.
  1471. */
  1472. radeon_bo_evict_vram(rdev);
  1473. radeon_agp_suspend(rdev);
  1474. pci_save_state(pdev);
  1475. if (freeze && rdev->family >= CHIP_CEDAR && !(rdev->flags & RADEON_IS_IGP)) {
  1476. rdev->asic->asic_reset(rdev, true);
  1477. pci_restore_state(pdev);
  1478. } else if (suspend) {
  1479. /* Shut down the device */
  1480. pci_disable_device(pdev);
  1481. pci_set_power_state(pdev, PCI_D3hot);
  1482. }
  1483. if (notify_clients)
  1484. drm_client_dev_suspend(dev);
  1485. return 0;
  1486. }
  1487. /*
  1488. * radeon_resume_kms - initiate device resume
  1489. *
  1490. * Bring the hw back to operating state (all asics).
  1491. * Returns 0 for success or an error on failure.
  1492. * Called at driver resume.
  1493. */
  1494. int radeon_resume_kms(struct drm_device *dev, bool resume, bool notify_clients)
  1495. {
  1496. struct drm_connector *connector;
  1497. struct radeon_device *rdev = dev->dev_private;
  1498. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1499. struct drm_crtc *crtc;
  1500. int r;
  1501. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1502. return 0;
  1503. if (resume) {
  1504. pci_set_power_state(pdev, PCI_D0);
  1505. pci_restore_state(pdev);
  1506. if (pci_enable_device(pdev))
  1507. return -1;
  1508. }
  1509. /* resume AGP if in use */
  1510. radeon_agp_resume(rdev);
  1511. radeon_resume(rdev);
  1512. r = radeon_ib_ring_tests(rdev);
  1513. if (r)
  1514. DRM_ERROR("ib ring test failed (%d).\n", r);
  1515. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  1516. /* do dpm late init */
  1517. r = radeon_pm_late_init(rdev);
  1518. if (r) {
  1519. rdev->pm.dpm_enabled = false;
  1520. DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
  1521. }
  1522. } else {
  1523. /* resume old pm late */
  1524. radeon_pm_resume(rdev);
  1525. }
  1526. radeon_restore_bios_scratch_regs(rdev);
  1527. /* pin cursors */
  1528. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1529. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1530. if (radeon_crtc->cursor_bo) {
  1531. struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
  1532. r = radeon_bo_reserve(robj, false);
  1533. if (r == 0) {
  1534. /* Only 27 bit offset for legacy cursor */
  1535. r = radeon_bo_pin_restricted(robj,
  1536. RADEON_GEM_DOMAIN_VRAM,
  1537. ASIC_IS_AVIVO(rdev) ?
  1538. 0 : 1 << 27,
  1539. &radeon_crtc->cursor_addr);
  1540. if (r != 0)
  1541. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  1542. radeon_bo_unreserve(robj);
  1543. }
  1544. }
  1545. }
  1546. /* init dig PHYs, disp eng pll */
  1547. if (rdev->is_atom_bios) {
  1548. radeon_atom_encoder_init(rdev);
  1549. radeon_atom_disp_eng_pll_init(rdev);
  1550. /* turn on the BL */
  1551. if (rdev->mode_info.bl_encoder) {
  1552. u8 bl_level = radeon_get_backlight_level(rdev,
  1553. rdev->mode_info.bl_encoder);
  1554. radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
  1555. bl_level);
  1556. }
  1557. }
  1558. /* reset hpd state */
  1559. radeon_hpd_init(rdev);
  1560. /* blat the mode back in */
  1561. if (notify_clients) {
  1562. drm_helper_resume_force_mode(dev);
  1563. /* turn on display hw */
  1564. drm_modeset_lock_all(dev);
  1565. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1566. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1567. }
  1568. drm_modeset_unlock_all(dev);
  1569. }
  1570. drm_kms_helper_poll_enable(dev);
  1571. /* set the power state here in case we are a PX system or headless */
  1572. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
  1573. radeon_pm_compute_clocks(rdev);
  1574. if (notify_clients)
  1575. drm_client_dev_resume(dev);
  1576. return 0;
  1577. }
  1578. /**
  1579. * radeon_gpu_reset - reset the asic
  1580. *
  1581. * @rdev: radeon device pointer
  1582. *
  1583. * Attempt the reset the GPU if it has hung (all asics).
  1584. * Returns 0 for success or an error on failure.
  1585. */
  1586. int radeon_gpu_reset(struct radeon_device *rdev)
  1587. {
  1588. unsigned ring_sizes[RADEON_NUM_RINGS];
  1589. uint32_t *ring_data[RADEON_NUM_RINGS];
  1590. bool saved = false;
  1591. int i, r;
  1592. down_write(&rdev->exclusive_lock);
  1593. if (!rdev->needs_reset) {
  1594. up_write(&rdev->exclusive_lock);
  1595. return 0;
  1596. }
  1597. atomic_inc(&rdev->gpu_reset_counter);
  1598. radeon_save_bios_scratch_regs(rdev);
  1599. radeon_suspend(rdev);
  1600. radeon_hpd_fini(rdev);
  1601. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1602. ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
  1603. &ring_data[i]);
  1604. if (ring_sizes[i]) {
  1605. saved = true;
  1606. dev_info(rdev->dev, "Saved %d dwords of commands "
  1607. "on ring %d.\n", ring_sizes[i], i);
  1608. }
  1609. }
  1610. r = radeon_asic_reset(rdev);
  1611. if (!r) {
  1612. dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
  1613. radeon_resume(rdev);
  1614. }
  1615. radeon_restore_bios_scratch_regs(rdev);
  1616. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1617. if (!r && ring_data[i]) {
  1618. radeon_ring_restore(rdev, &rdev->ring[i],
  1619. ring_sizes[i], ring_data[i]);
  1620. } else {
  1621. radeon_fence_driver_force_completion(rdev, i);
  1622. kfree(ring_data[i]);
  1623. }
  1624. }
  1625. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  1626. /* do dpm late init */
  1627. r = radeon_pm_late_init(rdev);
  1628. if (r) {
  1629. rdev->pm.dpm_enabled = false;
  1630. DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
  1631. }
  1632. } else {
  1633. /* resume old pm late */
  1634. radeon_pm_resume(rdev);
  1635. }
  1636. /* init dig PHYs, disp eng pll */
  1637. if (rdev->is_atom_bios) {
  1638. radeon_atom_encoder_init(rdev);
  1639. radeon_atom_disp_eng_pll_init(rdev);
  1640. /* turn on the BL */
  1641. if (rdev->mode_info.bl_encoder) {
  1642. u8 bl_level = radeon_get_backlight_level(rdev,
  1643. rdev->mode_info.bl_encoder);
  1644. radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
  1645. bl_level);
  1646. }
  1647. }
  1648. /* reset hpd state */
  1649. radeon_hpd_init(rdev);
  1650. rdev->in_reset = true;
  1651. rdev->needs_reset = false;
  1652. downgrade_write(&rdev->exclusive_lock);
  1653. drm_helper_resume_force_mode(rdev_to_drm(rdev));
  1654. /* set the power state here in case we are a PX system or headless */
  1655. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
  1656. radeon_pm_compute_clocks(rdev);
  1657. if (!r) {
  1658. r = radeon_ib_ring_tests(rdev);
  1659. if (r && saved)
  1660. r = -EAGAIN;
  1661. } else {
  1662. /* bad news, how to tell it to userspace ? */
  1663. dev_info(rdev->dev, "GPU reset failed\n");
  1664. }
  1665. rdev->needs_reset = r == -EAGAIN;
  1666. rdev->in_reset = false;
  1667. up_read(&rdev->exclusive_lock);
  1668. return r;
  1669. }