radeon_device.c 38 KB

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