radeon_device.c 26 KB

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