radeon_device.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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 <drm/drmP.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/radeon_drm.h>
  32. #include <linux/vgaarb.h>
  33. #include <linux/vga_switcheroo.h>
  34. #include "radeon_reg.h"
  35. #include "radeon.h"
  36. #include "atom.h"
  37. /*
  38. * Clear GPU surface registers.
  39. */
  40. void radeon_surface_init(struct radeon_device *rdev)
  41. {
  42. /* FIXME: check this out */
  43. if (rdev->family < CHIP_R600) {
  44. int i;
  45. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  46. if (rdev->surface_regs[i].bo)
  47. radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
  48. else
  49. radeon_clear_surface_reg(rdev, i);
  50. }
  51. /* enable surfaces */
  52. WREG32(RADEON_SURFACE_CNTL, 0);
  53. }
  54. }
  55. /*
  56. * GPU scratch registers helpers function.
  57. */
  58. void radeon_scratch_init(struct radeon_device *rdev)
  59. {
  60. int i;
  61. /* FIXME: check this out */
  62. if (rdev->family < CHIP_R300) {
  63. rdev->scratch.num_reg = 5;
  64. } else {
  65. rdev->scratch.num_reg = 7;
  66. }
  67. for (i = 0; i < rdev->scratch.num_reg; i++) {
  68. rdev->scratch.free[i] = true;
  69. rdev->scratch.reg[i] = RADEON_SCRATCH_REG0 + (i * 4);
  70. }
  71. }
  72. int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
  73. {
  74. int i;
  75. for (i = 0; i < rdev->scratch.num_reg; i++) {
  76. if (rdev->scratch.free[i]) {
  77. rdev->scratch.free[i] = false;
  78. *reg = rdev->scratch.reg[i];
  79. return 0;
  80. }
  81. }
  82. return -EINVAL;
  83. }
  84. void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
  85. {
  86. int i;
  87. for (i = 0; i < rdev->scratch.num_reg; i++) {
  88. if (rdev->scratch.reg[i] == reg) {
  89. rdev->scratch.free[i] = true;
  90. return;
  91. }
  92. }
  93. }
  94. /**
  95. * radeon_vram_location - try to find VRAM location
  96. * @rdev: radeon device structure holding all necessary informations
  97. * @mc: memory controller structure holding memory informations
  98. * @base: base address at which to put VRAM
  99. *
  100. * Function will place try to place VRAM at base address provided
  101. * as parameter (which is so far either PCI aperture address or
  102. * for IGP TOM base address).
  103. *
  104. * If there is not enough space to fit the unvisible VRAM in the 32bits
  105. * address space then we limit the VRAM size to the aperture.
  106. *
  107. * If we are using AGP and if the AGP aperture doesn't allow us to have
  108. * room for all the VRAM than we restrict the VRAM to the PCI aperture
  109. * size and print a warning.
  110. *
  111. * This function will never fails, worst case are limiting VRAM.
  112. *
  113. * Note: GTT start, end, size should be initialized before calling this
  114. * function on AGP platform.
  115. *
  116. * Note: We don't explictly enforce VRAM start to be aligned on VRAM size,
  117. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  118. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  119. * not IGP.
  120. *
  121. * Note: we use mc_vram_size as on some board we need to program the mc to
  122. * cover the whole aperture even if VRAM size is inferior to aperture size
  123. * Novell bug 204882 + along with lots of ubuntu ones
  124. *
  125. * Note: when limiting vram it's safe to overwritte real_vram_size because
  126. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  127. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  128. * ones)
  129. *
  130. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  131. * explicitly check for that thought.
  132. *
  133. * FIXME: when reducing VRAM size align new size on power of 2.
  134. */
  135. void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
  136. {
  137. mc->vram_start = base;
  138. if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
  139. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  140. mc->real_vram_size = mc->aper_size;
  141. mc->mc_vram_size = mc->aper_size;
  142. }
  143. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  144. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_end <= mc->gtt_end) {
  145. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  146. mc->real_vram_size = mc->aper_size;
  147. mc->mc_vram_size = mc->aper_size;
  148. }
  149. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  150. dev_info(rdev->dev, "VRAM: %lluM 0x%08llX - 0x%08llX (%lluM used)\n",
  151. mc->mc_vram_size >> 20, mc->vram_start,
  152. mc->vram_end, mc->real_vram_size >> 20);
  153. }
  154. /**
  155. * radeon_gtt_location - try to find GTT location
  156. * @rdev: radeon device structure holding all necessary informations
  157. * @mc: memory controller structure holding memory informations
  158. *
  159. * Function will place try to place GTT before or after VRAM.
  160. *
  161. * If GTT size is bigger than space left then we ajust GTT size.
  162. * Thus function will never fails.
  163. *
  164. * FIXME: when reducing GTT size align new size on power of 2.
  165. */
  166. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  167. {
  168. u64 size_af, size_bf;
  169. size_af = 0xFFFFFFFF - mc->vram_end;
  170. size_bf = mc->vram_start;
  171. if (size_bf > size_af) {
  172. if (mc->gtt_size > size_bf) {
  173. dev_warn(rdev->dev, "limiting GTT\n");
  174. mc->gtt_size = size_bf;
  175. }
  176. mc->gtt_start = mc->vram_start - mc->gtt_size;
  177. } else {
  178. if (mc->gtt_size > size_af) {
  179. dev_warn(rdev->dev, "limiting GTT\n");
  180. mc->gtt_size = size_af;
  181. }
  182. mc->gtt_start = mc->vram_end + 1;
  183. }
  184. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  185. dev_info(rdev->dev, "GTT: %lluM 0x%08llX - 0x%08llX\n",
  186. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  187. }
  188. /*
  189. * GPU helpers function.
  190. */
  191. bool radeon_card_posted(struct radeon_device *rdev)
  192. {
  193. uint32_t reg;
  194. /* first check CRTCs */
  195. if (ASIC_IS_DCE4(rdev)) {
  196. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  197. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
  198. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  199. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
  200. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  201. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  202. if (reg & EVERGREEN_CRTC_MASTER_EN)
  203. return true;
  204. } else if (ASIC_IS_AVIVO(rdev)) {
  205. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  206. RREG32(AVIVO_D2CRTC_CONTROL);
  207. if (reg & AVIVO_CRTC_EN) {
  208. return true;
  209. }
  210. } else {
  211. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  212. RREG32(RADEON_CRTC2_GEN_CNTL);
  213. if (reg & RADEON_CRTC_EN) {
  214. return true;
  215. }
  216. }
  217. /* then check MEM_SIZE, in case the crtcs are off */
  218. if (rdev->family >= CHIP_R600)
  219. reg = RREG32(R600_CONFIG_MEMSIZE);
  220. else
  221. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  222. if (reg)
  223. return true;
  224. return false;
  225. }
  226. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  227. {
  228. if (radeon_card_posted(rdev))
  229. return true;
  230. if (rdev->bios) {
  231. DRM_INFO("GPU not posted. posting now...\n");
  232. if (rdev->is_atom_bios)
  233. atom_asic_init(rdev->mode_info.atom_context);
  234. else
  235. radeon_combios_asic_init(rdev->ddev);
  236. return true;
  237. } else {
  238. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  239. return false;
  240. }
  241. }
  242. int radeon_dummy_page_init(struct radeon_device *rdev)
  243. {
  244. if (rdev->dummy_page.page)
  245. return 0;
  246. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  247. if (rdev->dummy_page.page == NULL)
  248. return -ENOMEM;
  249. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  250. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  251. if (!rdev->dummy_page.addr) {
  252. __free_page(rdev->dummy_page.page);
  253. rdev->dummy_page.page = NULL;
  254. return -ENOMEM;
  255. }
  256. return 0;
  257. }
  258. void radeon_dummy_page_fini(struct radeon_device *rdev)
  259. {
  260. if (rdev->dummy_page.page == NULL)
  261. return;
  262. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  263. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  264. __free_page(rdev->dummy_page.page);
  265. rdev->dummy_page.page = NULL;
  266. }
  267. /* ATOM accessor methods */
  268. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  269. {
  270. struct radeon_device *rdev = info->dev->dev_private;
  271. uint32_t r;
  272. r = rdev->pll_rreg(rdev, reg);
  273. return r;
  274. }
  275. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  276. {
  277. struct radeon_device *rdev = info->dev->dev_private;
  278. rdev->pll_wreg(rdev, reg, val);
  279. }
  280. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  281. {
  282. struct radeon_device *rdev = info->dev->dev_private;
  283. uint32_t r;
  284. r = rdev->mc_rreg(rdev, reg);
  285. return r;
  286. }
  287. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  288. {
  289. struct radeon_device *rdev = info->dev->dev_private;
  290. rdev->mc_wreg(rdev, reg, val);
  291. }
  292. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  293. {
  294. struct radeon_device *rdev = info->dev->dev_private;
  295. WREG32(reg*4, val);
  296. }
  297. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  298. {
  299. struct radeon_device *rdev = info->dev->dev_private;
  300. uint32_t r;
  301. r = RREG32(reg*4);
  302. return r;
  303. }
  304. int radeon_atombios_init(struct radeon_device *rdev)
  305. {
  306. struct card_info *atom_card_info =
  307. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  308. if (!atom_card_info)
  309. return -ENOMEM;
  310. rdev->mode_info.atom_card_info = atom_card_info;
  311. atom_card_info->dev = rdev->ddev;
  312. atom_card_info->reg_read = cail_reg_read;
  313. atom_card_info->reg_write = cail_reg_write;
  314. atom_card_info->mc_read = cail_mc_read;
  315. atom_card_info->mc_write = cail_mc_write;
  316. atom_card_info->pll_read = cail_pll_read;
  317. atom_card_info->pll_write = cail_pll_write;
  318. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  319. mutex_init(&rdev->mode_info.atom_context->mutex);
  320. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  321. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  322. return 0;
  323. }
  324. void radeon_atombios_fini(struct radeon_device *rdev)
  325. {
  326. if (rdev->mode_info.atom_context) {
  327. kfree(rdev->mode_info.atom_context->scratch);
  328. kfree(rdev->mode_info.atom_context);
  329. }
  330. kfree(rdev->mode_info.atom_card_info);
  331. }
  332. int radeon_combios_init(struct radeon_device *rdev)
  333. {
  334. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  335. return 0;
  336. }
  337. void radeon_combios_fini(struct radeon_device *rdev)
  338. {
  339. }
  340. /* if we get transitioned to only one device, tak VGA back */
  341. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  342. {
  343. struct radeon_device *rdev = cookie;
  344. radeon_vga_set_state(rdev, state);
  345. if (state)
  346. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  347. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  348. else
  349. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  350. }
  351. void radeon_check_arguments(struct radeon_device *rdev)
  352. {
  353. /* vramlimit must be a power of two */
  354. switch (radeon_vram_limit) {
  355. case 0:
  356. case 4:
  357. case 8:
  358. case 16:
  359. case 32:
  360. case 64:
  361. case 128:
  362. case 256:
  363. case 512:
  364. case 1024:
  365. case 2048:
  366. case 4096:
  367. break;
  368. default:
  369. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  370. radeon_vram_limit);
  371. radeon_vram_limit = 0;
  372. break;
  373. }
  374. radeon_vram_limit = radeon_vram_limit << 20;
  375. /* gtt size must be power of two and greater or equal to 32M */
  376. switch (radeon_gart_size) {
  377. case 4:
  378. case 8:
  379. case 16:
  380. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  381. radeon_gart_size);
  382. radeon_gart_size = 512;
  383. break;
  384. case 32:
  385. case 64:
  386. case 128:
  387. case 256:
  388. case 512:
  389. case 1024:
  390. case 2048:
  391. case 4096:
  392. break;
  393. default:
  394. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  395. radeon_gart_size);
  396. radeon_gart_size = 512;
  397. break;
  398. }
  399. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  400. /* AGP mode can only be -1, 1, 2, 4, 8 */
  401. switch (radeon_agpmode) {
  402. case -1:
  403. case 0:
  404. case 1:
  405. case 2:
  406. case 4:
  407. case 8:
  408. break;
  409. default:
  410. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  411. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  412. radeon_agpmode = 0;
  413. break;
  414. }
  415. }
  416. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  417. {
  418. struct drm_device *dev = pci_get_drvdata(pdev);
  419. struct radeon_device *rdev = dev->dev_private;
  420. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  421. if (state == VGA_SWITCHEROO_ON) {
  422. printk(KERN_INFO "radeon: switched on\n");
  423. /* don't suspend or resume card normally */
  424. rdev->powered_down = false;
  425. radeon_resume_kms(dev);
  426. } else {
  427. printk(KERN_INFO "radeon: switched off\n");
  428. radeon_suspend_kms(dev, pmm);
  429. /* don't suspend or resume card normally */
  430. rdev->powered_down = true;
  431. }
  432. }
  433. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  434. {
  435. struct drm_device *dev = pci_get_drvdata(pdev);
  436. bool can_switch;
  437. spin_lock(&dev->count_lock);
  438. can_switch = (dev->open_count == 0);
  439. spin_unlock(&dev->count_lock);
  440. return can_switch;
  441. }
  442. int radeon_device_init(struct radeon_device *rdev,
  443. struct drm_device *ddev,
  444. struct pci_dev *pdev,
  445. uint32_t flags)
  446. {
  447. int r;
  448. int dma_bits;
  449. DRM_INFO("radeon: Initializing kernel modesetting.\n");
  450. rdev->shutdown = false;
  451. rdev->dev = &pdev->dev;
  452. rdev->ddev = ddev;
  453. rdev->pdev = pdev;
  454. rdev->flags = flags;
  455. rdev->family = flags & RADEON_FAMILY_MASK;
  456. rdev->is_atom_bios = false;
  457. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  458. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  459. rdev->gpu_lockup = false;
  460. rdev->accel_working = false;
  461. /* mutex initialization are all done here so we
  462. * can recall function without having locking issues */
  463. mutex_init(&rdev->cs_mutex);
  464. mutex_init(&rdev->ib_pool.mutex);
  465. mutex_init(&rdev->cp.mutex);
  466. mutex_init(&rdev->dc_hw_i2c_mutex);
  467. if (rdev->family >= CHIP_R600)
  468. spin_lock_init(&rdev->ih.lock);
  469. mutex_init(&rdev->gem.mutex);
  470. mutex_init(&rdev->pm.mutex);
  471. rwlock_init(&rdev->fence_drv.lock);
  472. INIT_LIST_HEAD(&rdev->gem.objects);
  473. init_waitqueue_head(&rdev->irq.vblank_queue);
  474. /* setup workqueue */
  475. rdev->wq = create_workqueue("radeon");
  476. if (rdev->wq == NULL)
  477. return -ENOMEM;
  478. /* Set asic functions */
  479. r = radeon_asic_init(rdev);
  480. if (r)
  481. return r;
  482. radeon_check_arguments(rdev);
  483. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  484. radeon_agp_disable(rdev);
  485. }
  486. /* set DMA mask + need_dma32 flags.
  487. * PCIE - can handle 40-bits.
  488. * IGP - can handle 40-bits (in theory)
  489. * AGP - generally dma32 is safest
  490. * PCI - only dma32
  491. */
  492. rdev->need_dma32 = false;
  493. if (rdev->flags & RADEON_IS_AGP)
  494. rdev->need_dma32 = true;
  495. if (rdev->flags & RADEON_IS_PCI)
  496. rdev->need_dma32 = true;
  497. dma_bits = rdev->need_dma32 ? 32 : 40;
  498. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  499. if (r) {
  500. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  501. }
  502. /* Registers mapping */
  503. /* TODO: block userspace mapping of io register */
  504. rdev->rmmio_base = drm_get_resource_start(rdev->ddev, 2);
  505. rdev->rmmio_size = drm_get_resource_len(rdev->ddev, 2);
  506. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  507. if (rdev->rmmio == NULL) {
  508. return -ENOMEM;
  509. }
  510. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  511. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  512. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  513. /* this will fail for cards that aren't VGA class devices, just
  514. * ignore it */
  515. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  516. vga_switcheroo_register_client(rdev->pdev,
  517. radeon_switcheroo_set_state,
  518. radeon_switcheroo_can_switch);
  519. r = radeon_init(rdev);
  520. if (r)
  521. return r;
  522. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  523. /* Acceleration not working on AGP card try again
  524. * with fallback to PCI or PCIE GART
  525. */
  526. radeon_gpu_reset(rdev);
  527. radeon_fini(rdev);
  528. radeon_agp_disable(rdev);
  529. r = radeon_init(rdev);
  530. if (r)
  531. return r;
  532. }
  533. if (radeon_testing) {
  534. radeon_test_moves(rdev);
  535. }
  536. if (radeon_benchmarking) {
  537. radeon_benchmark(rdev);
  538. }
  539. return 0;
  540. }
  541. void radeon_device_fini(struct radeon_device *rdev)
  542. {
  543. DRM_INFO("radeon: finishing device.\n");
  544. rdev->shutdown = true;
  545. radeon_fini(rdev);
  546. destroy_workqueue(rdev->wq);
  547. vga_switcheroo_unregister_client(rdev->pdev);
  548. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  549. iounmap(rdev->rmmio);
  550. rdev->rmmio = NULL;
  551. }
  552. /*
  553. * Suspend & resume.
  554. */
  555. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  556. {
  557. struct radeon_device *rdev;
  558. struct drm_crtc *crtc;
  559. int r;
  560. if (dev == NULL || dev->dev_private == NULL) {
  561. return -ENODEV;
  562. }
  563. if (state.event == PM_EVENT_PRETHAW) {
  564. return 0;
  565. }
  566. rdev = dev->dev_private;
  567. if (rdev->powered_down)
  568. return 0;
  569. /* unpin the front buffers */
  570. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  571. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  572. struct radeon_bo *robj;
  573. if (rfb == NULL || rfb->obj == NULL) {
  574. continue;
  575. }
  576. robj = rfb->obj->driver_private;
  577. if (robj != rdev->fbdev_rbo) {
  578. r = radeon_bo_reserve(robj, false);
  579. if (unlikely(r == 0)) {
  580. radeon_bo_unpin(robj);
  581. radeon_bo_unreserve(robj);
  582. }
  583. }
  584. }
  585. /* evict vram memory */
  586. radeon_bo_evict_vram(rdev);
  587. /* wait for gpu to finish processing current batch */
  588. radeon_fence_wait_last(rdev);
  589. radeon_save_bios_scratch_regs(rdev);
  590. radeon_suspend(rdev);
  591. radeon_hpd_fini(rdev);
  592. /* evict remaining vram memory */
  593. radeon_bo_evict_vram(rdev);
  594. pci_save_state(dev->pdev);
  595. if (state.event == PM_EVENT_SUSPEND) {
  596. /* Shut down the device */
  597. pci_disable_device(dev->pdev);
  598. pci_set_power_state(dev->pdev, PCI_D3hot);
  599. }
  600. acquire_console_sem();
  601. fb_set_suspend(rdev->fbdev_info, 1);
  602. release_console_sem();
  603. return 0;
  604. }
  605. int radeon_resume_kms(struct drm_device *dev)
  606. {
  607. struct radeon_device *rdev = dev->dev_private;
  608. if (rdev->powered_down)
  609. return 0;
  610. acquire_console_sem();
  611. pci_set_power_state(dev->pdev, PCI_D0);
  612. pci_restore_state(dev->pdev);
  613. if (pci_enable_device(dev->pdev)) {
  614. release_console_sem();
  615. return -1;
  616. }
  617. pci_set_master(dev->pdev);
  618. /* resume AGP if in use */
  619. radeon_agp_resume(rdev);
  620. radeon_resume(rdev);
  621. radeon_restore_bios_scratch_regs(rdev);
  622. fb_set_suspend(rdev->fbdev_info, 0);
  623. release_console_sem();
  624. /* reset hpd state */
  625. radeon_hpd_init(rdev);
  626. /* blat the mode back in */
  627. drm_helper_resume_force_mode(dev);
  628. return 0;
  629. }
  630. /*
  631. * Debugfs
  632. */
  633. struct radeon_debugfs {
  634. struct drm_info_list *files;
  635. unsigned num_files;
  636. };
  637. static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES];
  638. static unsigned _radeon_debugfs_count = 0;
  639. int radeon_debugfs_add_files(struct radeon_device *rdev,
  640. struct drm_info_list *files,
  641. unsigned nfiles)
  642. {
  643. unsigned i;
  644. for (i = 0; i < _radeon_debugfs_count; i++) {
  645. if (_radeon_debugfs[i].files == files) {
  646. /* Already registered */
  647. return 0;
  648. }
  649. }
  650. if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) {
  651. DRM_ERROR("Reached maximum number of debugfs files.\n");
  652. DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n");
  653. return -EINVAL;
  654. }
  655. _radeon_debugfs[_radeon_debugfs_count].files = files;
  656. _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles;
  657. _radeon_debugfs_count++;
  658. #if defined(CONFIG_DEBUG_FS)
  659. drm_debugfs_create_files(files, nfiles,
  660. rdev->ddev->control->debugfs_root,
  661. rdev->ddev->control);
  662. drm_debugfs_create_files(files, nfiles,
  663. rdev->ddev->primary->debugfs_root,
  664. rdev->ddev->primary);
  665. #endif
  666. return 0;
  667. }
  668. #if defined(CONFIG_DEBUG_FS)
  669. int radeon_debugfs_init(struct drm_minor *minor)
  670. {
  671. return 0;
  672. }
  673. void radeon_debugfs_cleanup(struct drm_minor *minor)
  674. {
  675. unsigned i;
  676. for (i = 0; i < _radeon_debugfs_count; i++) {
  677. drm_debugfs_remove_files(_radeon_debugfs[i].files,
  678. _radeon_debugfs[i].num_files, minor);
  679. }
  680. }
  681. #endif