radeon_device.c 45 KB

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