radeon_device.c 43 KB

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