radeon_device.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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. mc->vram_start = base;
  340. if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
  341. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  342. mc->real_vram_size = mc->aper_size;
  343. mc->mc_vram_size = mc->aper_size;
  344. }
  345. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  346. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  347. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  348. mc->real_vram_size = mc->aper_size;
  349. mc->mc_vram_size = mc->aper_size;
  350. }
  351. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  352. if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size)
  353. mc->real_vram_size = radeon_vram_limit;
  354. dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  355. mc->mc_vram_size >> 20, mc->vram_start,
  356. mc->vram_end, mc->real_vram_size >> 20);
  357. }
  358. /**
  359. * radeon_gtt_location - try to find GTT location
  360. * @rdev: radeon device structure holding all necessary informations
  361. * @mc: memory controller structure holding memory informations
  362. *
  363. * Function will place try to place GTT before or after VRAM.
  364. *
  365. * If GTT size is bigger than space left then we ajust GTT size.
  366. * Thus function will never fails.
  367. *
  368. * FIXME: when reducing GTT size align new size on power of 2.
  369. */
  370. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  371. {
  372. u64 size_af, size_bf;
  373. size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  374. size_bf = mc->vram_start & ~mc->gtt_base_align;
  375. if (size_bf > size_af) {
  376. if (mc->gtt_size > size_bf) {
  377. dev_warn(rdev->dev, "limiting GTT\n");
  378. mc->gtt_size = size_bf;
  379. }
  380. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  381. } else {
  382. if (mc->gtt_size > size_af) {
  383. dev_warn(rdev->dev, "limiting GTT\n");
  384. mc->gtt_size = size_af;
  385. }
  386. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  387. }
  388. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  389. dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  390. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  391. }
  392. /*
  393. * GPU helpers function.
  394. */
  395. /**
  396. * radeon_card_posted - check if the hw has already been initialized
  397. *
  398. * @rdev: radeon_device pointer
  399. *
  400. * Check if the asic has been initialized (all asics).
  401. * Used at driver startup.
  402. * Returns true if initialized or false if not.
  403. */
  404. bool radeon_card_posted(struct radeon_device *rdev)
  405. {
  406. uint32_t reg;
  407. if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE)
  408. return false;
  409. /* first check CRTCs */
  410. if (ASIC_IS_DCE41(rdev)) {
  411. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  412. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  413. if (reg & EVERGREEN_CRTC_MASTER_EN)
  414. return true;
  415. } else if (ASIC_IS_DCE4(rdev)) {
  416. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  417. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
  418. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  419. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
  420. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  421. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  422. if (reg & EVERGREEN_CRTC_MASTER_EN)
  423. return true;
  424. } else if (ASIC_IS_AVIVO(rdev)) {
  425. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  426. RREG32(AVIVO_D2CRTC_CONTROL);
  427. if (reg & AVIVO_CRTC_EN) {
  428. return true;
  429. }
  430. } else {
  431. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  432. RREG32(RADEON_CRTC2_GEN_CNTL);
  433. if (reg & RADEON_CRTC_EN) {
  434. return true;
  435. }
  436. }
  437. /* then check MEM_SIZE, in case the crtcs are off */
  438. if (rdev->family >= CHIP_R600)
  439. reg = RREG32(R600_CONFIG_MEMSIZE);
  440. else
  441. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  442. if (reg)
  443. return true;
  444. return false;
  445. }
  446. /**
  447. * radeon_update_bandwidth_info - update display bandwidth params
  448. *
  449. * @rdev: radeon_device pointer
  450. *
  451. * Used when sclk/mclk are switched or display modes are set.
  452. * params are used to calculate display watermarks (all asics)
  453. */
  454. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  455. {
  456. fixed20_12 a;
  457. u32 sclk = rdev->pm.current_sclk;
  458. u32 mclk = rdev->pm.current_mclk;
  459. /* sclk/mclk in Mhz */
  460. a.full = dfixed_const(100);
  461. rdev->pm.sclk.full = dfixed_const(sclk);
  462. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  463. rdev->pm.mclk.full = dfixed_const(mclk);
  464. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  465. if (rdev->flags & RADEON_IS_IGP) {
  466. a.full = dfixed_const(16);
  467. /* core_bandwidth = sclk(Mhz) * 16 */
  468. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  469. }
  470. }
  471. /**
  472. * radeon_boot_test_post_card - check and possibly initialize the hw
  473. *
  474. * @rdev: radeon_device pointer
  475. *
  476. * Check if the asic is initialized and if not, attempt to initialize
  477. * it (all asics).
  478. * Returns true if initialized or false if not.
  479. */
  480. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  481. {
  482. if (radeon_card_posted(rdev))
  483. return true;
  484. if (rdev->bios) {
  485. DRM_INFO("GPU not posted. posting now...\n");
  486. if (rdev->is_atom_bios)
  487. atom_asic_init(rdev->mode_info.atom_context);
  488. else
  489. radeon_combios_asic_init(rdev->ddev);
  490. return true;
  491. } else {
  492. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  493. return false;
  494. }
  495. }
  496. /**
  497. * radeon_dummy_page_init - init dummy page used by the driver
  498. *
  499. * @rdev: radeon_device pointer
  500. *
  501. * Allocate the dummy page used by the driver (all asics).
  502. * This dummy page is used by the driver as a filler for gart entries
  503. * when pages are taken out of the GART
  504. * Returns 0 on sucess, -ENOMEM on failure.
  505. */
  506. int radeon_dummy_page_init(struct radeon_device *rdev)
  507. {
  508. if (rdev->dummy_page.page)
  509. return 0;
  510. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  511. if (rdev->dummy_page.page == NULL)
  512. return -ENOMEM;
  513. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  514. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  515. if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
  516. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  517. __free_page(rdev->dummy_page.page);
  518. rdev->dummy_page.page = NULL;
  519. return -ENOMEM;
  520. }
  521. return 0;
  522. }
  523. /**
  524. * radeon_dummy_page_fini - free dummy page used by the driver
  525. *
  526. * @rdev: radeon_device pointer
  527. *
  528. * Frees the dummy page used by the driver (all asics).
  529. */
  530. void radeon_dummy_page_fini(struct radeon_device *rdev)
  531. {
  532. if (rdev->dummy_page.page == NULL)
  533. return;
  534. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  535. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  536. __free_page(rdev->dummy_page.page);
  537. rdev->dummy_page.page = NULL;
  538. }
  539. /* ATOM accessor methods */
  540. /*
  541. * ATOM is an interpreted byte code stored in tables in the vbios. The
  542. * driver registers callbacks to access registers and the interpreter
  543. * in the driver parses the tables and executes then to program specific
  544. * actions (set display modes, asic init, etc.). See radeon_atombios.c,
  545. * atombios.h, and atom.c
  546. */
  547. /**
  548. * cail_pll_read - read PLL register
  549. *
  550. * @info: atom card_info pointer
  551. * @reg: PLL register offset
  552. *
  553. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  554. * Returns the value of the PLL register.
  555. */
  556. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  557. {
  558. struct radeon_device *rdev = info->dev->dev_private;
  559. uint32_t r;
  560. r = rdev->pll_rreg(rdev, reg);
  561. return r;
  562. }
  563. /**
  564. * cail_pll_write - write PLL register
  565. *
  566. * @info: atom card_info pointer
  567. * @reg: PLL register offset
  568. * @val: value to write to the pll register
  569. *
  570. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  571. */
  572. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  573. {
  574. struct radeon_device *rdev = info->dev->dev_private;
  575. rdev->pll_wreg(rdev, reg, val);
  576. }
  577. /**
  578. * cail_mc_read - read MC (Memory Controller) register
  579. *
  580. * @info: atom card_info pointer
  581. * @reg: MC register offset
  582. *
  583. * Provides an MC register accessor for the atom interpreter (r4xx+).
  584. * Returns the value of the MC register.
  585. */
  586. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  587. {
  588. struct radeon_device *rdev = info->dev->dev_private;
  589. uint32_t r;
  590. r = rdev->mc_rreg(rdev, reg);
  591. return r;
  592. }
  593. /**
  594. * cail_mc_write - write MC (Memory Controller) register
  595. *
  596. * @info: atom card_info pointer
  597. * @reg: MC register offset
  598. * @val: value to write to the pll register
  599. *
  600. * Provides a MC register accessor for the atom interpreter (r4xx+).
  601. */
  602. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  603. {
  604. struct radeon_device *rdev = info->dev->dev_private;
  605. rdev->mc_wreg(rdev, reg, val);
  606. }
  607. /**
  608. * cail_reg_write - write MMIO register
  609. *
  610. * @info: atom card_info pointer
  611. * @reg: MMIO register offset
  612. * @val: value to write to the pll register
  613. *
  614. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  615. */
  616. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  617. {
  618. struct radeon_device *rdev = info->dev->dev_private;
  619. WREG32(reg*4, val);
  620. }
  621. /**
  622. * cail_reg_read - read MMIO register
  623. *
  624. * @info: atom card_info pointer
  625. * @reg: MMIO register offset
  626. *
  627. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  628. * Returns the value of the MMIO register.
  629. */
  630. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  631. {
  632. struct radeon_device *rdev = info->dev->dev_private;
  633. uint32_t r;
  634. r = RREG32(reg*4);
  635. return r;
  636. }
  637. /**
  638. * cail_ioreg_write - write IO register
  639. *
  640. * @info: atom card_info pointer
  641. * @reg: IO register offset
  642. * @val: value to write to the pll register
  643. *
  644. * Provides a IO register accessor for the atom interpreter (r4xx+).
  645. */
  646. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  647. {
  648. struct radeon_device *rdev = info->dev->dev_private;
  649. WREG32_IO(reg*4, val);
  650. }
  651. /**
  652. * cail_ioreg_read - read IO register
  653. *
  654. * @info: atom card_info pointer
  655. * @reg: IO register offset
  656. *
  657. * Provides an IO register accessor for the atom interpreter (r4xx+).
  658. * Returns the value of the IO register.
  659. */
  660. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  661. {
  662. struct radeon_device *rdev = info->dev->dev_private;
  663. uint32_t r;
  664. r = RREG32_IO(reg*4);
  665. return r;
  666. }
  667. /**
  668. * radeon_atombios_init - init the driver info and callbacks for atombios
  669. *
  670. * @rdev: radeon_device pointer
  671. *
  672. * Initializes the driver info and register access callbacks for the
  673. * ATOM interpreter (r4xx+).
  674. * Returns 0 on sucess, -ENOMEM on failure.
  675. * Called at driver startup.
  676. */
  677. int radeon_atombios_init(struct radeon_device *rdev)
  678. {
  679. struct card_info *atom_card_info =
  680. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  681. if (!atom_card_info)
  682. return -ENOMEM;
  683. rdev->mode_info.atom_card_info = atom_card_info;
  684. atom_card_info->dev = rdev->ddev;
  685. atom_card_info->reg_read = cail_reg_read;
  686. atom_card_info->reg_write = cail_reg_write;
  687. /* needed for iio ops */
  688. if (rdev->rio_mem) {
  689. atom_card_info->ioreg_read = cail_ioreg_read;
  690. atom_card_info->ioreg_write = cail_ioreg_write;
  691. } else {
  692. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  693. atom_card_info->ioreg_read = cail_reg_read;
  694. atom_card_info->ioreg_write = cail_reg_write;
  695. }
  696. atom_card_info->mc_read = cail_mc_read;
  697. atom_card_info->mc_write = cail_mc_write;
  698. atom_card_info->pll_read = cail_pll_read;
  699. atom_card_info->pll_write = cail_pll_write;
  700. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  701. mutex_init(&rdev->mode_info.atom_context->mutex);
  702. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  703. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  704. return 0;
  705. }
  706. /**
  707. * radeon_atombios_fini - free the driver info and callbacks for atombios
  708. *
  709. * @rdev: radeon_device pointer
  710. *
  711. * Frees the driver info and register access callbacks for the ATOM
  712. * interpreter (r4xx+).
  713. * Called at driver shutdown.
  714. */
  715. void radeon_atombios_fini(struct radeon_device *rdev)
  716. {
  717. if (rdev->mode_info.atom_context) {
  718. kfree(rdev->mode_info.atom_context->scratch);
  719. kfree(rdev->mode_info.atom_context);
  720. }
  721. kfree(rdev->mode_info.atom_card_info);
  722. }
  723. /* COMBIOS */
  724. /*
  725. * COMBIOS is the bios format prior to ATOM. It provides
  726. * command tables similar to ATOM, but doesn't have a unified
  727. * parser. See radeon_combios.c
  728. */
  729. /**
  730. * radeon_combios_init - init the driver info for combios
  731. *
  732. * @rdev: radeon_device pointer
  733. *
  734. * Initializes the driver info for combios (r1xx-r3xx).
  735. * Returns 0 on sucess.
  736. * Called at driver startup.
  737. */
  738. int radeon_combios_init(struct radeon_device *rdev)
  739. {
  740. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  741. return 0;
  742. }
  743. /**
  744. * radeon_combios_fini - free the driver info for combios
  745. *
  746. * @rdev: radeon_device pointer
  747. *
  748. * Frees the driver info for combios (r1xx-r3xx).
  749. * Called at driver shutdown.
  750. */
  751. void radeon_combios_fini(struct radeon_device *rdev)
  752. {
  753. }
  754. /* if we get transitioned to only one device, take VGA back */
  755. /**
  756. * radeon_vga_set_decode - enable/disable vga decode
  757. *
  758. * @cookie: radeon_device pointer
  759. * @state: enable/disable vga decode
  760. *
  761. * Enable/disable vga decode (all asics).
  762. * Returns VGA resource flags.
  763. */
  764. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  765. {
  766. struct radeon_device *rdev = cookie;
  767. radeon_vga_set_state(rdev, state);
  768. if (state)
  769. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  770. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  771. else
  772. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  773. }
  774. /**
  775. * radeon_check_arguments - validate module params
  776. *
  777. * @rdev: radeon_device pointer
  778. *
  779. * Validates certain module parameters and updates
  780. * the associated values used by the driver (all asics).
  781. */
  782. void radeon_check_arguments(struct radeon_device *rdev)
  783. {
  784. /* vramlimit must be a power of two */
  785. switch (radeon_vram_limit) {
  786. case 0:
  787. case 4:
  788. case 8:
  789. case 16:
  790. case 32:
  791. case 64:
  792. case 128:
  793. case 256:
  794. case 512:
  795. case 1024:
  796. case 2048:
  797. case 4096:
  798. break;
  799. default:
  800. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  801. radeon_vram_limit);
  802. radeon_vram_limit = 0;
  803. break;
  804. }
  805. radeon_vram_limit = radeon_vram_limit << 20;
  806. /* gtt size must be power of two and greater or equal to 32M */
  807. switch (radeon_gart_size) {
  808. case 4:
  809. case 8:
  810. case 16:
  811. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  812. radeon_gart_size);
  813. radeon_gart_size = 512;
  814. break;
  815. case 32:
  816. case 64:
  817. case 128:
  818. case 256:
  819. case 512:
  820. case 1024:
  821. case 2048:
  822. case 4096:
  823. break;
  824. default:
  825. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  826. radeon_gart_size);
  827. radeon_gart_size = 512;
  828. break;
  829. }
  830. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  831. /* AGP mode can only be -1, 1, 2, 4, 8 */
  832. switch (radeon_agpmode) {
  833. case -1:
  834. case 0:
  835. case 1:
  836. case 2:
  837. case 4:
  838. case 8:
  839. break;
  840. default:
  841. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  842. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  843. radeon_agpmode = 0;
  844. break;
  845. }
  846. }
  847. /**
  848. * radeon_switcheroo_set_state - set switcheroo state
  849. *
  850. * @pdev: pci dev pointer
  851. * @state: vga switcheroo state
  852. *
  853. * Callback for the switcheroo driver. Suspends or resumes the
  854. * the asics before or after it is powered up using ACPI methods.
  855. */
  856. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  857. {
  858. struct drm_device *dev = pci_get_drvdata(pdev);
  859. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  860. if (state == VGA_SWITCHEROO_ON) {
  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. radeon_resume_kms(dev);
  865. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  866. drm_kms_helper_poll_enable(dev);
  867. } else {
  868. printk(KERN_INFO "radeon: switched off\n");
  869. drm_kms_helper_poll_disable(dev);
  870. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  871. radeon_suspend_kms(dev, pmm);
  872. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  873. }
  874. }
  875. /**
  876. * radeon_switcheroo_can_switch - see if switcheroo state can change
  877. *
  878. * @pdev: pci dev pointer
  879. *
  880. * Callback for the switcheroo driver. Check of the switcheroo
  881. * state can be changed.
  882. * Returns true if the state can be changed, false if not.
  883. */
  884. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  885. {
  886. struct drm_device *dev = pci_get_drvdata(pdev);
  887. bool can_switch;
  888. spin_lock(&dev->count_lock);
  889. can_switch = (dev->open_count == 0);
  890. spin_unlock(&dev->count_lock);
  891. return can_switch;
  892. }
  893. static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
  894. .set_gpu_state = radeon_switcheroo_set_state,
  895. .reprobe = NULL,
  896. .can_switch = radeon_switcheroo_can_switch,
  897. };
  898. /**
  899. * radeon_device_init - initialize the driver
  900. *
  901. * @rdev: radeon_device pointer
  902. * @pdev: drm dev pointer
  903. * @pdev: pci dev pointer
  904. * @flags: driver flags
  905. *
  906. * Initializes the driver info and hw (all asics).
  907. * Returns 0 for success or an error on failure.
  908. * Called at driver startup.
  909. */
  910. int radeon_device_init(struct radeon_device *rdev,
  911. struct drm_device *ddev,
  912. struct pci_dev *pdev,
  913. uint32_t flags)
  914. {
  915. int r, i;
  916. int dma_bits;
  917. rdev->shutdown = false;
  918. rdev->dev = &pdev->dev;
  919. rdev->ddev = ddev;
  920. rdev->pdev = pdev;
  921. rdev->flags = flags;
  922. rdev->family = flags & RADEON_FAMILY_MASK;
  923. rdev->is_atom_bios = false;
  924. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  925. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  926. rdev->accel_working = false;
  927. /* set up ring ids */
  928. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  929. rdev->ring[i].idx = i;
  930. }
  931. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
  932. radeon_family_name[rdev->family], pdev->vendor, pdev->device,
  933. pdev->subsystem_vendor, pdev->subsystem_device);
  934. /* mutex initialization are all done here so we
  935. * can recall function without having locking issues */
  936. mutex_init(&rdev->ring_lock);
  937. mutex_init(&rdev->dc_hw_i2c_mutex);
  938. atomic_set(&rdev->ih.lock, 0);
  939. mutex_init(&rdev->gem.mutex);
  940. mutex_init(&rdev->pm.mutex);
  941. mutex_init(&rdev->gpu_clock_mutex);
  942. init_rwsem(&rdev->pm.mclk_lock);
  943. init_rwsem(&rdev->exclusive_lock);
  944. init_waitqueue_head(&rdev->irq.vblank_queue);
  945. r = radeon_gem_init(rdev);
  946. if (r)
  947. return r;
  948. /* initialize vm here */
  949. mutex_init(&rdev->vm_manager.lock);
  950. rdev->vm_manager.max_pfn = 1 << 20;
  951. INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
  952. /* Set asic functions */
  953. r = radeon_asic_init(rdev);
  954. if (r)
  955. return r;
  956. radeon_check_arguments(rdev);
  957. /* all of the newer IGP chips have an internal gart
  958. * However some rs4xx report as AGP, so remove that here.
  959. */
  960. if ((rdev->family >= CHIP_RS400) &&
  961. (rdev->flags & RADEON_IS_IGP)) {
  962. rdev->flags &= ~RADEON_IS_AGP;
  963. }
  964. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  965. radeon_agp_disable(rdev);
  966. }
  967. /* set DMA mask + need_dma32 flags.
  968. * PCIE - can handle 40-bits.
  969. * IGP - can handle 40-bits
  970. * AGP - generally dma32 is safest
  971. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  972. */
  973. rdev->need_dma32 = false;
  974. if (rdev->flags & RADEON_IS_AGP)
  975. rdev->need_dma32 = true;
  976. if ((rdev->flags & RADEON_IS_PCI) &&
  977. (rdev->family <= CHIP_RS740))
  978. rdev->need_dma32 = true;
  979. dma_bits = rdev->need_dma32 ? 32 : 40;
  980. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  981. if (r) {
  982. rdev->need_dma32 = true;
  983. dma_bits = 32;
  984. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  985. }
  986. r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  987. if (r) {
  988. pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
  989. printk(KERN_WARNING "radeon: No coherent DMA available.\n");
  990. }
  991. /* Registers mapping */
  992. /* TODO: block userspace mapping of io register */
  993. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  994. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  995. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  996. if (rdev->rmmio == NULL) {
  997. return -ENOMEM;
  998. }
  999. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  1000. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  1001. /* io port mapping */
  1002. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1003. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  1004. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  1005. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  1006. break;
  1007. }
  1008. }
  1009. if (rdev->rio_mem == NULL)
  1010. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1011. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  1012. /* this will fail for cards that aren't VGA class devices, just
  1013. * ignore it */
  1014. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  1015. vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops);
  1016. r = radeon_init(rdev);
  1017. if (r)
  1018. return r;
  1019. r = radeon_ib_ring_tests(rdev);
  1020. if (r)
  1021. DRM_ERROR("ib ring test failed (%d).\n", r);
  1022. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  1023. /* Acceleration not working on AGP card try again
  1024. * with fallback to PCI or PCIE GART
  1025. */
  1026. radeon_asic_reset(rdev);
  1027. radeon_fini(rdev);
  1028. radeon_agp_disable(rdev);
  1029. r = radeon_init(rdev);
  1030. if (r)
  1031. return r;
  1032. }
  1033. if ((radeon_testing & 1)) {
  1034. radeon_test_moves(rdev);
  1035. }
  1036. if ((radeon_testing & 2)) {
  1037. radeon_test_syncing(rdev);
  1038. }
  1039. if (radeon_benchmarking) {
  1040. radeon_benchmark(rdev, radeon_benchmarking);
  1041. }
  1042. return 0;
  1043. }
  1044. static void radeon_debugfs_remove_files(struct radeon_device *rdev);
  1045. /**
  1046. * radeon_device_fini - tear down the driver
  1047. *
  1048. * @rdev: radeon_device pointer
  1049. *
  1050. * Tear down the driver info (all asics).
  1051. * Called at driver shutdown.
  1052. */
  1053. void radeon_device_fini(struct radeon_device *rdev)
  1054. {
  1055. DRM_INFO("radeon: finishing device.\n");
  1056. rdev->shutdown = true;
  1057. /* evict vram memory */
  1058. radeon_bo_evict_vram(rdev);
  1059. radeon_fini(rdev);
  1060. vga_switcheroo_unregister_client(rdev->pdev);
  1061. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  1062. if (rdev->rio_mem)
  1063. pci_iounmap(rdev->pdev, rdev->rio_mem);
  1064. rdev->rio_mem = NULL;
  1065. iounmap(rdev->rmmio);
  1066. rdev->rmmio = NULL;
  1067. radeon_debugfs_remove_files(rdev);
  1068. }
  1069. /*
  1070. * Suspend & resume.
  1071. */
  1072. /**
  1073. * radeon_suspend_kms - initiate device suspend
  1074. *
  1075. * @pdev: drm dev pointer
  1076. * @state: suspend state
  1077. *
  1078. * Puts the hw in the suspend state (all asics).
  1079. * Returns 0 for success or an error on failure.
  1080. * Called at driver suspend.
  1081. */
  1082. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  1083. {
  1084. struct radeon_device *rdev;
  1085. struct drm_crtc *crtc;
  1086. struct drm_connector *connector;
  1087. int i, r;
  1088. if (dev == NULL || dev->dev_private == NULL) {
  1089. return -ENODEV;
  1090. }
  1091. if (state.event == PM_EVENT_PRETHAW) {
  1092. return 0;
  1093. }
  1094. rdev = dev->dev_private;
  1095. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1096. return 0;
  1097. drm_kms_helper_poll_disable(dev);
  1098. /* turn off display hw */
  1099. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1100. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1101. }
  1102. /* unpin the front buffers */
  1103. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1104. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  1105. struct radeon_bo *robj;
  1106. if (rfb == NULL || rfb->obj == NULL) {
  1107. continue;
  1108. }
  1109. robj = gem_to_radeon_bo(rfb->obj);
  1110. /* don't unpin kernel fb objects */
  1111. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  1112. r = radeon_bo_reserve(robj, false);
  1113. if (r == 0) {
  1114. radeon_bo_unpin(robj);
  1115. radeon_bo_unreserve(robj);
  1116. }
  1117. }
  1118. }
  1119. /* evict vram memory */
  1120. radeon_bo_evict_vram(rdev);
  1121. mutex_lock(&rdev->ring_lock);
  1122. /* wait for gpu to finish processing current batch */
  1123. for (i = 0; i < RADEON_NUM_RINGS; i++)
  1124. radeon_fence_wait_empty_locked(rdev, i);
  1125. mutex_unlock(&rdev->ring_lock);
  1126. radeon_save_bios_scratch_regs(rdev);
  1127. radeon_pm_suspend(rdev);
  1128. radeon_suspend(rdev);
  1129. radeon_hpd_fini(rdev);
  1130. /* evict remaining vram memory */
  1131. radeon_bo_evict_vram(rdev);
  1132. radeon_agp_suspend(rdev);
  1133. pci_save_state(dev->pdev);
  1134. if (state.event == PM_EVENT_SUSPEND) {
  1135. /* Shut down the device */
  1136. pci_disable_device(dev->pdev);
  1137. pci_set_power_state(dev->pdev, PCI_D3hot);
  1138. }
  1139. console_lock();
  1140. radeon_fbdev_set_suspend(rdev, 1);
  1141. console_unlock();
  1142. return 0;
  1143. }
  1144. /**
  1145. * radeon_resume_kms - initiate device resume
  1146. *
  1147. * @pdev: drm dev pointer
  1148. *
  1149. * Bring the hw back to operating state (all asics).
  1150. * Returns 0 for success or an error on failure.
  1151. * Called at driver resume.
  1152. */
  1153. int radeon_resume_kms(struct drm_device *dev)
  1154. {
  1155. struct drm_connector *connector;
  1156. struct radeon_device *rdev = dev->dev_private;
  1157. int r;
  1158. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1159. return 0;
  1160. console_lock();
  1161. pci_set_power_state(dev->pdev, PCI_D0);
  1162. pci_restore_state(dev->pdev);
  1163. if (pci_enable_device(dev->pdev)) {
  1164. console_unlock();
  1165. return -1;
  1166. }
  1167. /* resume AGP if in use */
  1168. radeon_agp_resume(rdev);
  1169. radeon_resume(rdev);
  1170. r = radeon_ib_ring_tests(rdev);
  1171. if (r)
  1172. DRM_ERROR("ib ring test failed (%d).\n", r);
  1173. radeon_pm_resume(rdev);
  1174. radeon_restore_bios_scratch_regs(rdev);
  1175. radeon_fbdev_set_suspend(rdev, 0);
  1176. console_unlock();
  1177. /* init dig PHYs, disp eng pll */
  1178. if (rdev->is_atom_bios) {
  1179. radeon_atom_encoder_init(rdev);
  1180. radeon_atom_disp_eng_pll_init(rdev);
  1181. }
  1182. /* reset hpd state */
  1183. radeon_hpd_init(rdev);
  1184. /* blat the mode back in */
  1185. drm_helper_resume_force_mode(dev);
  1186. /* turn on display hw */
  1187. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1188. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1189. }
  1190. drm_kms_helper_poll_enable(dev);
  1191. return 0;
  1192. }
  1193. /**
  1194. * radeon_gpu_reset - reset the asic
  1195. *
  1196. * @rdev: radeon device pointer
  1197. *
  1198. * Attempt the reset the GPU if it has hung (all asics).
  1199. * Returns 0 for success or an error on failure.
  1200. */
  1201. int radeon_gpu_reset(struct radeon_device *rdev)
  1202. {
  1203. unsigned ring_sizes[RADEON_NUM_RINGS];
  1204. uint32_t *ring_data[RADEON_NUM_RINGS];
  1205. bool saved = false;
  1206. int i, r;
  1207. int resched;
  1208. down_write(&rdev->exclusive_lock);
  1209. radeon_save_bios_scratch_regs(rdev);
  1210. /* block TTM */
  1211. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  1212. radeon_suspend(rdev);
  1213. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1214. ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
  1215. &ring_data[i]);
  1216. if (ring_sizes[i]) {
  1217. saved = true;
  1218. dev_info(rdev->dev, "Saved %d dwords of commands "
  1219. "on ring %d.\n", ring_sizes[i], i);
  1220. }
  1221. }
  1222. retry:
  1223. r = radeon_asic_reset(rdev);
  1224. if (!r) {
  1225. dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
  1226. radeon_resume(rdev);
  1227. }
  1228. radeon_restore_bios_scratch_regs(rdev);
  1229. drm_helper_resume_force_mode(rdev->ddev);
  1230. if (!r) {
  1231. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1232. radeon_ring_restore(rdev, &rdev->ring[i],
  1233. ring_sizes[i], ring_data[i]);
  1234. ring_sizes[i] = 0;
  1235. ring_data[i] = NULL;
  1236. }
  1237. r = radeon_ib_ring_tests(rdev);
  1238. if (r) {
  1239. dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
  1240. if (saved) {
  1241. saved = false;
  1242. radeon_suspend(rdev);
  1243. goto retry;
  1244. }
  1245. }
  1246. } else {
  1247. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1248. kfree(ring_data[i]);
  1249. }
  1250. }
  1251. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  1252. if (r) {
  1253. /* bad news, how to tell it to userspace ? */
  1254. dev_info(rdev->dev, "GPU reset failed\n");
  1255. }
  1256. up_write(&rdev->exclusive_lock);
  1257. return r;
  1258. }
  1259. /*
  1260. * Debugfs
  1261. */
  1262. int radeon_debugfs_add_files(struct radeon_device *rdev,
  1263. struct drm_info_list *files,
  1264. unsigned nfiles)
  1265. {
  1266. unsigned i;
  1267. for (i = 0; i < rdev->debugfs_count; i++) {
  1268. if (rdev->debugfs[i].files == files) {
  1269. /* Already registered */
  1270. return 0;
  1271. }
  1272. }
  1273. i = rdev->debugfs_count + 1;
  1274. if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
  1275. DRM_ERROR("Reached maximum number of debugfs components.\n");
  1276. DRM_ERROR("Report so we increase "
  1277. "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
  1278. return -EINVAL;
  1279. }
  1280. rdev->debugfs[rdev->debugfs_count].files = files;
  1281. rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
  1282. rdev->debugfs_count = i;
  1283. #if defined(CONFIG_DEBUG_FS)
  1284. drm_debugfs_create_files(files, nfiles,
  1285. rdev->ddev->control->debugfs_root,
  1286. rdev->ddev->control);
  1287. drm_debugfs_create_files(files, nfiles,
  1288. rdev->ddev->primary->debugfs_root,
  1289. rdev->ddev->primary);
  1290. #endif
  1291. return 0;
  1292. }
  1293. static void radeon_debugfs_remove_files(struct radeon_device *rdev)
  1294. {
  1295. #if defined(CONFIG_DEBUG_FS)
  1296. unsigned i;
  1297. for (i = 0; i < rdev->debugfs_count; i++) {
  1298. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1299. rdev->debugfs[i].num_files,
  1300. rdev->ddev->control);
  1301. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1302. rdev->debugfs[i].num_files,
  1303. rdev->ddev->primary);
  1304. }
  1305. #endif
  1306. }
  1307. #if defined(CONFIG_DEBUG_FS)
  1308. int radeon_debugfs_init(struct drm_minor *minor)
  1309. {
  1310. return 0;
  1311. }
  1312. void radeon_debugfs_cleanup(struct drm_minor *minor)
  1313. {
  1314. }
  1315. #endif