radeon_device.c 38 KB

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