radeon_device.c 27 KB

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