radeon_device.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. * Clear GPU surface registers.
  99. */
  100. void radeon_surface_init(struct radeon_device *rdev)
  101. {
  102. /* FIXME: check this out */
  103. if (rdev->family < CHIP_R600) {
  104. int i;
  105. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  106. if (rdev->surface_regs[i].bo)
  107. radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
  108. else
  109. radeon_clear_surface_reg(rdev, i);
  110. }
  111. /* enable surfaces */
  112. WREG32(RADEON_SURFACE_CNTL, 0);
  113. }
  114. }
  115. /*
  116. * GPU scratch registers helpers function.
  117. */
  118. void radeon_scratch_init(struct radeon_device *rdev)
  119. {
  120. int i;
  121. /* FIXME: check this out */
  122. if (rdev->family < CHIP_R300) {
  123. rdev->scratch.num_reg = 5;
  124. } else {
  125. rdev->scratch.num_reg = 7;
  126. }
  127. rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
  128. for (i = 0; i < rdev->scratch.num_reg; i++) {
  129. rdev->scratch.free[i] = true;
  130. rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
  131. }
  132. }
  133. int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
  134. {
  135. int i;
  136. for (i = 0; i < rdev->scratch.num_reg; i++) {
  137. if (rdev->scratch.free[i]) {
  138. rdev->scratch.free[i] = false;
  139. *reg = rdev->scratch.reg[i];
  140. return 0;
  141. }
  142. }
  143. return -EINVAL;
  144. }
  145. void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
  146. {
  147. int i;
  148. for (i = 0; i < rdev->scratch.num_reg; i++) {
  149. if (rdev->scratch.reg[i] == reg) {
  150. rdev->scratch.free[i] = true;
  151. return;
  152. }
  153. }
  154. }
  155. void radeon_wb_disable(struct radeon_device *rdev)
  156. {
  157. int r;
  158. if (rdev->wb.wb_obj) {
  159. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  160. if (unlikely(r != 0))
  161. return;
  162. radeon_bo_kunmap(rdev->wb.wb_obj);
  163. radeon_bo_unpin(rdev->wb.wb_obj);
  164. radeon_bo_unreserve(rdev->wb.wb_obj);
  165. }
  166. rdev->wb.enabled = false;
  167. }
  168. void radeon_wb_fini(struct radeon_device *rdev)
  169. {
  170. radeon_wb_disable(rdev);
  171. if (rdev->wb.wb_obj) {
  172. radeon_bo_unref(&rdev->wb.wb_obj);
  173. rdev->wb.wb = NULL;
  174. rdev->wb.wb_obj = NULL;
  175. }
  176. }
  177. int radeon_wb_init(struct radeon_device *rdev)
  178. {
  179. int r;
  180. if (rdev->wb.wb_obj == NULL) {
  181. r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
  182. RADEON_GEM_DOMAIN_GTT, &rdev->wb.wb_obj);
  183. if (r) {
  184. dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
  185. return r;
  186. }
  187. }
  188. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  189. if (unlikely(r != 0)) {
  190. radeon_wb_fini(rdev);
  191. return r;
  192. }
  193. r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
  194. &rdev->wb.gpu_addr);
  195. if (r) {
  196. radeon_bo_unreserve(rdev->wb.wb_obj);
  197. dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
  198. radeon_wb_fini(rdev);
  199. return r;
  200. }
  201. r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
  202. radeon_bo_unreserve(rdev->wb.wb_obj);
  203. if (r) {
  204. dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
  205. radeon_wb_fini(rdev);
  206. return r;
  207. }
  208. /* clear wb memory */
  209. memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
  210. /* disable event_write fences */
  211. rdev->wb.use_event = false;
  212. /* disabled via module param */
  213. if (radeon_no_wb == 1) {
  214. rdev->wb.enabled = false;
  215. } else {
  216. if (rdev->flags & RADEON_IS_AGP) {
  217. /* often unreliable on AGP */
  218. rdev->wb.enabled = false;
  219. } else if (rdev->family < CHIP_R300) {
  220. /* often unreliable on pre-r300 */
  221. rdev->wb.enabled = false;
  222. } else {
  223. rdev->wb.enabled = true;
  224. /* event_write fences are only available on r600+ */
  225. if (rdev->family >= CHIP_R600) {
  226. rdev->wb.use_event = true;
  227. }
  228. }
  229. }
  230. /* always use writeback/events on NI, APUs */
  231. if (rdev->family >= CHIP_PALM) {
  232. rdev->wb.enabled = true;
  233. rdev->wb.use_event = true;
  234. }
  235. dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
  236. return 0;
  237. }
  238. /**
  239. * radeon_vram_location - try to find VRAM location
  240. * @rdev: radeon device structure holding all necessary informations
  241. * @mc: memory controller structure holding memory informations
  242. * @base: base address at which to put VRAM
  243. *
  244. * Function will place try to place VRAM at base address provided
  245. * as parameter (which is so far either PCI aperture address or
  246. * for IGP TOM base address).
  247. *
  248. * If there is not enough space to fit the unvisible VRAM in the 32bits
  249. * address space then we limit the VRAM size to the aperture.
  250. *
  251. * If we are using AGP and if the AGP aperture doesn't allow us to have
  252. * room for all the VRAM than we restrict the VRAM to the PCI aperture
  253. * size and print a warning.
  254. *
  255. * This function will never fails, worst case are limiting VRAM.
  256. *
  257. * Note: GTT start, end, size should be initialized before calling this
  258. * function on AGP platform.
  259. *
  260. * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  261. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  262. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  263. * not IGP.
  264. *
  265. * Note: we use mc_vram_size as on some board we need to program the mc to
  266. * cover the whole aperture even if VRAM size is inferior to aperture size
  267. * Novell bug 204882 + along with lots of ubuntu ones
  268. *
  269. * Note: when limiting vram it's safe to overwritte real_vram_size because
  270. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  271. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  272. * ones)
  273. *
  274. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  275. * explicitly check for that thought.
  276. *
  277. * FIXME: when reducing VRAM size align new size on power of 2.
  278. */
  279. void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
  280. {
  281. mc->vram_start = base;
  282. if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
  283. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  284. mc->real_vram_size = mc->aper_size;
  285. mc->mc_vram_size = mc->aper_size;
  286. }
  287. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  288. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  289. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  290. mc->real_vram_size = mc->aper_size;
  291. mc->mc_vram_size = mc->aper_size;
  292. }
  293. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  294. if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size)
  295. mc->real_vram_size = radeon_vram_limit;
  296. dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  297. mc->mc_vram_size >> 20, mc->vram_start,
  298. mc->vram_end, mc->real_vram_size >> 20);
  299. }
  300. /**
  301. * radeon_gtt_location - try to find GTT location
  302. * @rdev: radeon device structure holding all necessary informations
  303. * @mc: memory controller structure holding memory informations
  304. *
  305. * Function will place try to place GTT before or after VRAM.
  306. *
  307. * If GTT size is bigger than space left then we ajust GTT size.
  308. * Thus function will never fails.
  309. *
  310. * FIXME: when reducing GTT size align new size on power of 2.
  311. */
  312. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  313. {
  314. u64 size_af, size_bf;
  315. size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  316. size_bf = mc->vram_start & ~mc->gtt_base_align;
  317. if (size_bf > size_af) {
  318. if (mc->gtt_size > size_bf) {
  319. dev_warn(rdev->dev, "limiting GTT\n");
  320. mc->gtt_size = size_bf;
  321. }
  322. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  323. } else {
  324. if (mc->gtt_size > size_af) {
  325. dev_warn(rdev->dev, "limiting GTT\n");
  326. mc->gtt_size = size_af;
  327. }
  328. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  329. }
  330. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  331. dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  332. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  333. }
  334. /*
  335. * GPU helpers function.
  336. */
  337. bool radeon_card_posted(struct radeon_device *rdev)
  338. {
  339. uint32_t reg;
  340. if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE)
  341. return false;
  342. /* first check CRTCs */
  343. if (ASIC_IS_DCE41(rdev)) {
  344. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  345. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  346. if (reg & EVERGREEN_CRTC_MASTER_EN)
  347. return true;
  348. } else if (ASIC_IS_DCE4(rdev)) {
  349. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  350. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
  351. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  352. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
  353. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  354. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  355. if (reg & EVERGREEN_CRTC_MASTER_EN)
  356. return true;
  357. } else if (ASIC_IS_AVIVO(rdev)) {
  358. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  359. RREG32(AVIVO_D2CRTC_CONTROL);
  360. if (reg & AVIVO_CRTC_EN) {
  361. return true;
  362. }
  363. } else {
  364. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  365. RREG32(RADEON_CRTC2_GEN_CNTL);
  366. if (reg & RADEON_CRTC_EN) {
  367. return true;
  368. }
  369. }
  370. /* then check MEM_SIZE, in case the crtcs are off */
  371. if (rdev->family >= CHIP_R600)
  372. reg = RREG32(R600_CONFIG_MEMSIZE);
  373. else
  374. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  375. if (reg)
  376. return true;
  377. return false;
  378. }
  379. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  380. {
  381. fixed20_12 a;
  382. u32 sclk = rdev->pm.current_sclk;
  383. u32 mclk = rdev->pm.current_mclk;
  384. /* sclk/mclk in Mhz */
  385. a.full = dfixed_const(100);
  386. rdev->pm.sclk.full = dfixed_const(sclk);
  387. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  388. rdev->pm.mclk.full = dfixed_const(mclk);
  389. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  390. if (rdev->flags & RADEON_IS_IGP) {
  391. a.full = dfixed_const(16);
  392. /* core_bandwidth = sclk(Mhz) * 16 */
  393. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  394. }
  395. }
  396. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  397. {
  398. if (radeon_card_posted(rdev))
  399. return true;
  400. if (rdev->bios) {
  401. DRM_INFO("GPU not posted. posting now...\n");
  402. if (rdev->is_atom_bios)
  403. atom_asic_init(rdev->mode_info.atom_context);
  404. else
  405. radeon_combios_asic_init(rdev->ddev);
  406. return true;
  407. } else {
  408. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  409. return false;
  410. }
  411. }
  412. int radeon_dummy_page_init(struct radeon_device *rdev)
  413. {
  414. if (rdev->dummy_page.page)
  415. return 0;
  416. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  417. if (rdev->dummy_page.page == NULL)
  418. return -ENOMEM;
  419. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  420. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  421. if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
  422. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  423. __free_page(rdev->dummy_page.page);
  424. rdev->dummy_page.page = NULL;
  425. return -ENOMEM;
  426. }
  427. return 0;
  428. }
  429. void radeon_dummy_page_fini(struct radeon_device *rdev)
  430. {
  431. if (rdev->dummy_page.page == NULL)
  432. return;
  433. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  434. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  435. __free_page(rdev->dummy_page.page);
  436. rdev->dummy_page.page = NULL;
  437. }
  438. /* ATOM accessor methods */
  439. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  440. {
  441. struct radeon_device *rdev = info->dev->dev_private;
  442. uint32_t r;
  443. r = rdev->pll_rreg(rdev, reg);
  444. return r;
  445. }
  446. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  447. {
  448. struct radeon_device *rdev = info->dev->dev_private;
  449. rdev->pll_wreg(rdev, reg, val);
  450. }
  451. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  452. {
  453. struct radeon_device *rdev = info->dev->dev_private;
  454. uint32_t r;
  455. r = rdev->mc_rreg(rdev, reg);
  456. return r;
  457. }
  458. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  459. {
  460. struct radeon_device *rdev = info->dev->dev_private;
  461. rdev->mc_wreg(rdev, reg, val);
  462. }
  463. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  464. {
  465. struct radeon_device *rdev = info->dev->dev_private;
  466. WREG32(reg*4, val);
  467. }
  468. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  469. {
  470. struct radeon_device *rdev = info->dev->dev_private;
  471. uint32_t r;
  472. r = RREG32(reg*4);
  473. return r;
  474. }
  475. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  476. {
  477. struct radeon_device *rdev = info->dev->dev_private;
  478. WREG32_IO(reg*4, val);
  479. }
  480. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  481. {
  482. struct radeon_device *rdev = info->dev->dev_private;
  483. uint32_t r;
  484. r = RREG32_IO(reg*4);
  485. return r;
  486. }
  487. int radeon_atombios_init(struct radeon_device *rdev)
  488. {
  489. struct card_info *atom_card_info =
  490. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  491. if (!atom_card_info)
  492. return -ENOMEM;
  493. rdev->mode_info.atom_card_info = atom_card_info;
  494. atom_card_info->dev = rdev->ddev;
  495. atom_card_info->reg_read = cail_reg_read;
  496. atom_card_info->reg_write = cail_reg_write;
  497. /* needed for iio ops */
  498. if (rdev->rio_mem) {
  499. atom_card_info->ioreg_read = cail_ioreg_read;
  500. atom_card_info->ioreg_write = cail_ioreg_write;
  501. } else {
  502. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  503. atom_card_info->ioreg_read = cail_reg_read;
  504. atom_card_info->ioreg_write = cail_reg_write;
  505. }
  506. atom_card_info->mc_read = cail_mc_read;
  507. atom_card_info->mc_write = cail_mc_write;
  508. atom_card_info->pll_read = cail_pll_read;
  509. atom_card_info->pll_write = cail_pll_write;
  510. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  511. mutex_init(&rdev->mode_info.atom_context->mutex);
  512. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  513. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  514. return 0;
  515. }
  516. void radeon_atombios_fini(struct radeon_device *rdev)
  517. {
  518. if (rdev->mode_info.atom_context) {
  519. kfree(rdev->mode_info.atom_context->scratch);
  520. kfree(rdev->mode_info.atom_context);
  521. }
  522. kfree(rdev->mode_info.atom_card_info);
  523. }
  524. int radeon_combios_init(struct radeon_device *rdev)
  525. {
  526. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  527. return 0;
  528. }
  529. void radeon_combios_fini(struct radeon_device *rdev)
  530. {
  531. }
  532. /* if we get transitioned to only one device, tak VGA back */
  533. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  534. {
  535. struct radeon_device *rdev = cookie;
  536. radeon_vga_set_state(rdev, state);
  537. if (state)
  538. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  539. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  540. else
  541. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  542. }
  543. void radeon_check_arguments(struct radeon_device *rdev)
  544. {
  545. /* vramlimit must be a power of two */
  546. switch (radeon_vram_limit) {
  547. case 0:
  548. case 4:
  549. case 8:
  550. case 16:
  551. case 32:
  552. case 64:
  553. case 128:
  554. case 256:
  555. case 512:
  556. case 1024:
  557. case 2048:
  558. case 4096:
  559. break;
  560. default:
  561. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  562. radeon_vram_limit);
  563. radeon_vram_limit = 0;
  564. break;
  565. }
  566. radeon_vram_limit = radeon_vram_limit << 20;
  567. /* gtt size must be power of two and greater or equal to 32M */
  568. switch (radeon_gart_size) {
  569. case 4:
  570. case 8:
  571. case 16:
  572. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  573. radeon_gart_size);
  574. radeon_gart_size = 512;
  575. break;
  576. case 32:
  577. case 64:
  578. case 128:
  579. case 256:
  580. case 512:
  581. case 1024:
  582. case 2048:
  583. case 4096:
  584. break;
  585. default:
  586. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  587. radeon_gart_size);
  588. radeon_gart_size = 512;
  589. break;
  590. }
  591. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  592. /* AGP mode can only be -1, 1, 2, 4, 8 */
  593. switch (radeon_agpmode) {
  594. case -1:
  595. case 0:
  596. case 1:
  597. case 2:
  598. case 4:
  599. case 8:
  600. break;
  601. default:
  602. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  603. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  604. radeon_agpmode = 0;
  605. break;
  606. }
  607. }
  608. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  609. {
  610. struct drm_device *dev = pci_get_drvdata(pdev);
  611. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  612. if (state == VGA_SWITCHEROO_ON) {
  613. printk(KERN_INFO "radeon: switched on\n");
  614. /* don't suspend or resume card normally */
  615. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  616. radeon_resume_kms(dev);
  617. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  618. drm_kms_helper_poll_enable(dev);
  619. } else {
  620. printk(KERN_INFO "radeon: switched off\n");
  621. drm_kms_helper_poll_disable(dev);
  622. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  623. radeon_suspend_kms(dev, pmm);
  624. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  625. }
  626. }
  627. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  628. {
  629. struct drm_device *dev = pci_get_drvdata(pdev);
  630. bool can_switch;
  631. spin_lock(&dev->count_lock);
  632. can_switch = (dev->open_count == 0);
  633. spin_unlock(&dev->count_lock);
  634. return can_switch;
  635. }
  636. int radeon_device_init(struct radeon_device *rdev,
  637. struct drm_device *ddev,
  638. struct pci_dev *pdev,
  639. uint32_t flags)
  640. {
  641. int r, i;
  642. int dma_bits;
  643. rdev->shutdown = false;
  644. rdev->dev = &pdev->dev;
  645. rdev->ddev = ddev;
  646. rdev->pdev = pdev;
  647. rdev->flags = flags;
  648. rdev->family = flags & RADEON_FAMILY_MASK;
  649. rdev->is_atom_bios = false;
  650. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  651. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  652. rdev->accel_working = false;
  653. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
  654. radeon_family_name[rdev->family], pdev->vendor, pdev->device,
  655. pdev->subsystem_vendor, pdev->subsystem_device);
  656. /* mutex initialization are all done here so we
  657. * can recall function without having locking issues */
  658. radeon_mutex_init(&rdev->cs_mutex);
  659. mutex_init(&rdev->ring_lock);
  660. mutex_init(&rdev->dc_hw_i2c_mutex);
  661. if (rdev->family >= CHIP_R600)
  662. spin_lock_init(&rdev->ih.lock);
  663. mutex_init(&rdev->gem.mutex);
  664. mutex_init(&rdev->pm.mutex);
  665. mutex_init(&rdev->vram_mutex);
  666. INIT_LIST_HEAD(&rdev->gem.objects);
  667. init_waitqueue_head(&rdev->irq.vblank_queue);
  668. init_waitqueue_head(&rdev->irq.idle_queue);
  669. /* initialize vm here */
  670. rdev->vm_manager.use_bitmap = 1;
  671. rdev->vm_manager.max_pfn = 1 << 20;
  672. INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
  673. /* Set asic functions */
  674. r = radeon_asic_init(rdev);
  675. if (r)
  676. return r;
  677. radeon_check_arguments(rdev);
  678. /* all of the newer IGP chips have an internal gart
  679. * However some rs4xx report as AGP, so remove that here.
  680. */
  681. if ((rdev->family >= CHIP_RS400) &&
  682. (rdev->flags & RADEON_IS_IGP)) {
  683. rdev->flags &= ~RADEON_IS_AGP;
  684. }
  685. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  686. radeon_agp_disable(rdev);
  687. }
  688. /* set DMA mask + need_dma32 flags.
  689. * PCIE - can handle 40-bits.
  690. * IGP - can handle 40-bits
  691. * AGP - generally dma32 is safest
  692. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  693. */
  694. rdev->need_dma32 = false;
  695. if (rdev->flags & RADEON_IS_AGP)
  696. rdev->need_dma32 = true;
  697. if ((rdev->flags & RADEON_IS_PCI) &&
  698. (rdev->family < CHIP_RS400))
  699. rdev->need_dma32 = true;
  700. dma_bits = rdev->need_dma32 ? 32 : 40;
  701. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  702. if (r) {
  703. rdev->need_dma32 = true;
  704. dma_bits = 32;
  705. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  706. }
  707. r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  708. if (r) {
  709. pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
  710. printk(KERN_WARNING "radeon: No coherent DMA available.\n");
  711. }
  712. /* Registers mapping */
  713. /* TODO: block userspace mapping of io register */
  714. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  715. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  716. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  717. if (rdev->rmmio == NULL) {
  718. return -ENOMEM;
  719. }
  720. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  721. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  722. /* io port mapping */
  723. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  724. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  725. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  726. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  727. break;
  728. }
  729. }
  730. if (rdev->rio_mem == NULL)
  731. DRM_ERROR("Unable to find PCI I/O BAR\n");
  732. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  733. /* this will fail for cards that aren't VGA class devices, just
  734. * ignore it */
  735. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  736. vga_switcheroo_register_client(rdev->pdev,
  737. radeon_switcheroo_set_state,
  738. NULL,
  739. radeon_switcheroo_can_switch);
  740. r = radeon_init(rdev);
  741. if (r)
  742. return r;
  743. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  744. /* Acceleration not working on AGP card try again
  745. * with fallback to PCI or PCIE GART
  746. */
  747. radeon_asic_reset(rdev);
  748. radeon_fini(rdev);
  749. radeon_agp_disable(rdev);
  750. r = radeon_init(rdev);
  751. if (r)
  752. return r;
  753. }
  754. if ((radeon_testing & 1)) {
  755. radeon_test_moves(rdev);
  756. }
  757. if ((radeon_testing & 2)) {
  758. radeon_test_syncing(rdev);
  759. }
  760. if (radeon_benchmarking) {
  761. radeon_benchmark(rdev, radeon_benchmarking);
  762. }
  763. return 0;
  764. }
  765. static void radeon_debugfs_remove_files(struct radeon_device *rdev);
  766. void radeon_device_fini(struct radeon_device *rdev)
  767. {
  768. DRM_INFO("radeon: finishing device.\n");
  769. rdev->shutdown = true;
  770. /* evict vram memory */
  771. radeon_bo_evict_vram(rdev);
  772. radeon_fini(rdev);
  773. vga_switcheroo_unregister_client(rdev->pdev);
  774. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  775. if (rdev->rio_mem)
  776. pci_iounmap(rdev->pdev, rdev->rio_mem);
  777. rdev->rio_mem = NULL;
  778. iounmap(rdev->rmmio);
  779. rdev->rmmio = NULL;
  780. radeon_debugfs_remove_files(rdev);
  781. }
  782. /*
  783. * Suspend & resume.
  784. */
  785. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  786. {
  787. struct radeon_device *rdev;
  788. struct drm_crtc *crtc;
  789. struct drm_connector *connector;
  790. int i, r;
  791. if (dev == NULL || dev->dev_private == NULL) {
  792. return -ENODEV;
  793. }
  794. if (state.event == PM_EVENT_PRETHAW) {
  795. return 0;
  796. }
  797. rdev = dev->dev_private;
  798. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  799. return 0;
  800. drm_kms_helper_poll_disable(dev);
  801. /* turn off display hw */
  802. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  803. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  804. }
  805. /* unpin the front buffers */
  806. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  807. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  808. struct radeon_bo *robj;
  809. if (rfb == NULL || rfb->obj == NULL) {
  810. continue;
  811. }
  812. robj = gem_to_radeon_bo(rfb->obj);
  813. /* don't unpin kernel fb objects */
  814. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  815. r = radeon_bo_reserve(robj, false);
  816. if (r == 0) {
  817. radeon_bo_unpin(robj);
  818. radeon_bo_unreserve(robj);
  819. }
  820. }
  821. }
  822. /* evict vram memory */
  823. radeon_bo_evict_vram(rdev);
  824. mutex_lock(&rdev->ring_lock);
  825. /* wait for gpu to finish processing current batch */
  826. for (i = 0; i < RADEON_NUM_RINGS; i++)
  827. radeon_fence_wait_empty_locked(rdev, i);
  828. mutex_unlock(&rdev->ring_lock);
  829. radeon_save_bios_scratch_regs(rdev);
  830. radeon_pm_suspend(rdev);
  831. radeon_suspend(rdev);
  832. radeon_hpd_fini(rdev);
  833. /* evict remaining vram memory */
  834. radeon_bo_evict_vram(rdev);
  835. radeon_agp_suspend(rdev);
  836. pci_save_state(dev->pdev);
  837. if (state.event == PM_EVENT_SUSPEND) {
  838. /* Shut down the device */
  839. pci_disable_device(dev->pdev);
  840. pci_set_power_state(dev->pdev, PCI_D3hot);
  841. }
  842. console_lock();
  843. radeon_fbdev_set_suspend(rdev, 1);
  844. console_unlock();
  845. return 0;
  846. }
  847. int radeon_resume_kms(struct drm_device *dev)
  848. {
  849. struct drm_connector *connector;
  850. struct radeon_device *rdev = dev->dev_private;
  851. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  852. return 0;
  853. console_lock();
  854. pci_set_power_state(dev->pdev, PCI_D0);
  855. pci_restore_state(dev->pdev);
  856. if (pci_enable_device(dev->pdev)) {
  857. console_unlock();
  858. return -1;
  859. }
  860. /* resume AGP if in use */
  861. radeon_agp_resume(rdev);
  862. radeon_resume(rdev);
  863. radeon_pm_resume(rdev);
  864. radeon_restore_bios_scratch_regs(rdev);
  865. radeon_fbdev_set_suspend(rdev, 0);
  866. console_unlock();
  867. /* init dig PHYs, disp eng pll */
  868. if (rdev->is_atom_bios) {
  869. radeon_atom_encoder_init(rdev);
  870. radeon_atom_disp_eng_pll_init(rdev);
  871. }
  872. /* reset hpd state */
  873. radeon_hpd_init(rdev);
  874. /* blat the mode back in */
  875. drm_helper_resume_force_mode(dev);
  876. /* turn on display hw */
  877. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  878. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  879. }
  880. drm_kms_helper_poll_enable(dev);
  881. return 0;
  882. }
  883. int radeon_gpu_reset(struct radeon_device *rdev)
  884. {
  885. int r;
  886. int resched;
  887. radeon_save_bios_scratch_regs(rdev);
  888. /* block TTM */
  889. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  890. radeon_suspend(rdev);
  891. r = radeon_asic_reset(rdev);
  892. if (!r) {
  893. dev_info(rdev->dev, "GPU reset succeed\n");
  894. radeon_resume(rdev);
  895. radeon_restore_bios_scratch_regs(rdev);
  896. drm_helper_resume_force_mode(rdev->ddev);
  897. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  898. }
  899. if (r) {
  900. /* bad news, how to tell it to userspace ? */
  901. dev_info(rdev->dev, "GPU reset failed\n");
  902. }
  903. return r;
  904. }
  905. /*
  906. * Debugfs
  907. */
  908. int radeon_debugfs_add_files(struct radeon_device *rdev,
  909. struct drm_info_list *files,
  910. unsigned nfiles)
  911. {
  912. unsigned i;
  913. for (i = 0; i < rdev->debugfs_count; i++) {
  914. if (rdev->debugfs[i].files == files) {
  915. /* Already registered */
  916. return 0;
  917. }
  918. }
  919. i = rdev->debugfs_count + 1;
  920. if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
  921. DRM_ERROR("Reached maximum number of debugfs components.\n");
  922. DRM_ERROR("Report so we increase "
  923. "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
  924. return -EINVAL;
  925. }
  926. rdev->debugfs[rdev->debugfs_count].files = files;
  927. rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
  928. rdev->debugfs_count = i;
  929. #if defined(CONFIG_DEBUG_FS)
  930. drm_debugfs_create_files(files, nfiles,
  931. rdev->ddev->control->debugfs_root,
  932. rdev->ddev->control);
  933. drm_debugfs_create_files(files, nfiles,
  934. rdev->ddev->primary->debugfs_root,
  935. rdev->ddev->primary);
  936. #endif
  937. return 0;
  938. }
  939. static void radeon_debugfs_remove_files(struct radeon_device *rdev)
  940. {
  941. #if defined(CONFIG_DEBUG_FS)
  942. unsigned i;
  943. for (i = 0; i < rdev->debugfs_count; i++) {
  944. drm_debugfs_remove_files(rdev->debugfs[i].files,
  945. rdev->debugfs[i].num_files,
  946. rdev->ddev->control);
  947. drm_debugfs_remove_files(rdev->debugfs[i].files,
  948. rdev->debugfs[i].num_files,
  949. rdev->ddev->primary);
  950. }
  951. #endif
  952. }
  953. #if defined(CONFIG_DEBUG_FS)
  954. int radeon_debugfs_init(struct drm_minor *minor)
  955. {
  956. return 0;
  957. }
  958. void radeon_debugfs_cleanup(struct drm_minor *minor)
  959. {
  960. }
  961. #endif