radeon_device.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  227. {
  228. fixed20_12 a;
  229. u32 sclk, mclk;
  230. if (rdev->flags & RADEON_IS_IGP) {
  231. sclk = radeon_get_engine_clock(rdev);
  232. mclk = rdev->clock.default_mclk;
  233. a.full = rfixed_const(100);
  234. rdev->pm.sclk.full = rfixed_const(sclk);
  235. rdev->pm.sclk.full = rfixed_div(rdev->pm.sclk, a);
  236. rdev->pm.mclk.full = rfixed_const(mclk);
  237. rdev->pm.mclk.full = rfixed_div(rdev->pm.mclk, a);
  238. a.full = rfixed_const(16);
  239. /* core_bandwidth = sclk(Mhz) * 16 */
  240. rdev->pm.core_bandwidth.full = rfixed_div(rdev->pm.sclk, a);
  241. } else {
  242. sclk = radeon_get_engine_clock(rdev);
  243. mclk = radeon_get_memory_clock(rdev);
  244. a.full = rfixed_const(100);
  245. rdev->pm.sclk.full = rfixed_const(sclk);
  246. rdev->pm.sclk.full = rfixed_div(rdev->pm.sclk, a);
  247. rdev->pm.mclk.full = rfixed_const(mclk);
  248. rdev->pm.mclk.full = rfixed_div(rdev->pm.mclk, a);
  249. }
  250. }
  251. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  252. {
  253. if (radeon_card_posted(rdev))
  254. return true;
  255. if (rdev->bios) {
  256. DRM_INFO("GPU not posted. posting now...\n");
  257. if (rdev->is_atom_bios)
  258. atom_asic_init(rdev->mode_info.atom_context);
  259. else
  260. radeon_combios_asic_init(rdev->ddev);
  261. return true;
  262. } else {
  263. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  264. return false;
  265. }
  266. }
  267. int radeon_dummy_page_init(struct radeon_device *rdev)
  268. {
  269. if (rdev->dummy_page.page)
  270. return 0;
  271. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  272. if (rdev->dummy_page.page == NULL)
  273. return -ENOMEM;
  274. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  275. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  276. if (!rdev->dummy_page.addr) {
  277. __free_page(rdev->dummy_page.page);
  278. rdev->dummy_page.page = NULL;
  279. return -ENOMEM;
  280. }
  281. return 0;
  282. }
  283. void radeon_dummy_page_fini(struct radeon_device *rdev)
  284. {
  285. if (rdev->dummy_page.page == NULL)
  286. return;
  287. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  288. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  289. __free_page(rdev->dummy_page.page);
  290. rdev->dummy_page.page = NULL;
  291. }
  292. /* ATOM accessor methods */
  293. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  294. {
  295. struct radeon_device *rdev = info->dev->dev_private;
  296. uint32_t r;
  297. r = rdev->pll_rreg(rdev, reg);
  298. return r;
  299. }
  300. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  301. {
  302. struct radeon_device *rdev = info->dev->dev_private;
  303. rdev->pll_wreg(rdev, reg, val);
  304. }
  305. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  306. {
  307. struct radeon_device *rdev = info->dev->dev_private;
  308. uint32_t r;
  309. r = rdev->mc_rreg(rdev, reg);
  310. return r;
  311. }
  312. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  313. {
  314. struct radeon_device *rdev = info->dev->dev_private;
  315. rdev->mc_wreg(rdev, reg, val);
  316. }
  317. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  318. {
  319. struct radeon_device *rdev = info->dev->dev_private;
  320. WREG32(reg*4, val);
  321. }
  322. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  323. {
  324. struct radeon_device *rdev = info->dev->dev_private;
  325. uint32_t r;
  326. r = RREG32(reg*4);
  327. return r;
  328. }
  329. int radeon_atombios_init(struct radeon_device *rdev)
  330. {
  331. struct card_info *atom_card_info =
  332. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  333. if (!atom_card_info)
  334. return -ENOMEM;
  335. rdev->mode_info.atom_card_info = atom_card_info;
  336. atom_card_info->dev = rdev->ddev;
  337. atom_card_info->reg_read = cail_reg_read;
  338. atom_card_info->reg_write = cail_reg_write;
  339. atom_card_info->mc_read = cail_mc_read;
  340. atom_card_info->mc_write = cail_mc_write;
  341. atom_card_info->pll_read = cail_pll_read;
  342. atom_card_info->pll_write = cail_pll_write;
  343. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  344. mutex_init(&rdev->mode_info.atom_context->mutex);
  345. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  346. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  347. return 0;
  348. }
  349. void radeon_atombios_fini(struct radeon_device *rdev)
  350. {
  351. if (rdev->mode_info.atom_context) {
  352. kfree(rdev->mode_info.atom_context->scratch);
  353. kfree(rdev->mode_info.atom_context);
  354. }
  355. kfree(rdev->mode_info.atom_card_info);
  356. }
  357. int radeon_combios_init(struct radeon_device *rdev)
  358. {
  359. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  360. return 0;
  361. }
  362. void radeon_combios_fini(struct radeon_device *rdev)
  363. {
  364. }
  365. /* if we get transitioned to only one device, tak VGA back */
  366. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  367. {
  368. struct radeon_device *rdev = cookie;
  369. radeon_vga_set_state(rdev, state);
  370. if (state)
  371. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  372. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  373. else
  374. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  375. }
  376. void radeon_check_arguments(struct radeon_device *rdev)
  377. {
  378. /* vramlimit must be a power of two */
  379. switch (radeon_vram_limit) {
  380. case 0:
  381. case 4:
  382. case 8:
  383. case 16:
  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, "vram limit (%d) must be a power of 2\n",
  395. radeon_vram_limit);
  396. radeon_vram_limit = 0;
  397. break;
  398. }
  399. radeon_vram_limit = radeon_vram_limit << 20;
  400. /* gtt size must be power of two and greater or equal to 32M */
  401. switch (radeon_gart_size) {
  402. case 4:
  403. case 8:
  404. case 16:
  405. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  406. radeon_gart_size);
  407. radeon_gart_size = 512;
  408. break;
  409. case 32:
  410. case 64:
  411. case 128:
  412. case 256:
  413. case 512:
  414. case 1024:
  415. case 2048:
  416. case 4096:
  417. break;
  418. default:
  419. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  420. radeon_gart_size);
  421. radeon_gart_size = 512;
  422. break;
  423. }
  424. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  425. /* AGP mode can only be -1, 1, 2, 4, 8 */
  426. switch (radeon_agpmode) {
  427. case -1:
  428. case 0:
  429. case 1:
  430. case 2:
  431. case 4:
  432. case 8:
  433. break;
  434. default:
  435. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  436. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  437. radeon_agpmode = 0;
  438. break;
  439. }
  440. }
  441. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  442. {
  443. struct drm_device *dev = pci_get_drvdata(pdev);
  444. struct radeon_device *rdev = dev->dev_private;
  445. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  446. if (state == VGA_SWITCHEROO_ON) {
  447. printk(KERN_INFO "radeon: switched on\n");
  448. /* don't suspend or resume card normally */
  449. rdev->powered_down = false;
  450. radeon_resume_kms(dev);
  451. } else {
  452. printk(KERN_INFO "radeon: switched off\n");
  453. radeon_suspend_kms(dev, pmm);
  454. /* don't suspend or resume card normally */
  455. rdev->powered_down = true;
  456. }
  457. }
  458. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  459. {
  460. struct drm_device *dev = pci_get_drvdata(pdev);
  461. bool can_switch;
  462. spin_lock(&dev->count_lock);
  463. can_switch = (dev->open_count == 0);
  464. spin_unlock(&dev->count_lock);
  465. return can_switch;
  466. }
  467. int radeon_device_init(struct radeon_device *rdev,
  468. struct drm_device *ddev,
  469. struct pci_dev *pdev,
  470. uint32_t flags)
  471. {
  472. int r;
  473. int dma_bits;
  474. DRM_INFO("radeon: Initializing kernel modesetting.\n");
  475. rdev->shutdown = false;
  476. rdev->dev = &pdev->dev;
  477. rdev->ddev = ddev;
  478. rdev->pdev = pdev;
  479. rdev->flags = flags;
  480. rdev->family = flags & RADEON_FAMILY_MASK;
  481. rdev->is_atom_bios = false;
  482. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  483. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  484. rdev->gpu_lockup = false;
  485. rdev->accel_working = false;
  486. /* mutex initialization are all done here so we
  487. * can recall function without having locking issues */
  488. mutex_init(&rdev->cs_mutex);
  489. mutex_init(&rdev->ib_pool.mutex);
  490. mutex_init(&rdev->cp.mutex);
  491. mutex_init(&rdev->dc_hw_i2c_mutex);
  492. if (rdev->family >= CHIP_R600)
  493. spin_lock_init(&rdev->ih.lock);
  494. mutex_init(&rdev->gem.mutex);
  495. mutex_init(&rdev->pm.mutex);
  496. rwlock_init(&rdev->fence_drv.lock);
  497. INIT_LIST_HEAD(&rdev->gem.objects);
  498. init_waitqueue_head(&rdev->irq.vblank_queue);
  499. /* setup workqueue */
  500. rdev->wq = create_workqueue("radeon");
  501. if (rdev->wq == NULL)
  502. return -ENOMEM;
  503. /* Set asic functions */
  504. r = radeon_asic_init(rdev);
  505. if (r)
  506. return r;
  507. radeon_check_arguments(rdev);
  508. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  509. radeon_agp_disable(rdev);
  510. }
  511. /* set DMA mask + need_dma32 flags.
  512. * PCIE - can handle 40-bits.
  513. * IGP - can handle 40-bits (in theory)
  514. * AGP - generally dma32 is safest
  515. * PCI - only dma32
  516. */
  517. rdev->need_dma32 = false;
  518. if (rdev->flags & RADEON_IS_AGP)
  519. rdev->need_dma32 = true;
  520. if (rdev->flags & RADEON_IS_PCI)
  521. rdev->need_dma32 = true;
  522. dma_bits = rdev->need_dma32 ? 32 : 40;
  523. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  524. if (r) {
  525. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  526. }
  527. /* Registers mapping */
  528. /* TODO: block userspace mapping of io register */
  529. rdev->rmmio_base = drm_get_resource_start(rdev->ddev, 2);
  530. rdev->rmmio_size = drm_get_resource_len(rdev->ddev, 2);
  531. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  532. if (rdev->rmmio == NULL) {
  533. return -ENOMEM;
  534. }
  535. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  536. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  537. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  538. /* this will fail for cards that aren't VGA class devices, just
  539. * ignore it */
  540. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  541. vga_switcheroo_register_client(rdev->pdev,
  542. radeon_switcheroo_set_state,
  543. radeon_switcheroo_can_switch);
  544. r = radeon_init(rdev);
  545. if (r)
  546. return r;
  547. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  548. /* Acceleration not working on AGP card try again
  549. * with fallback to PCI or PCIE GART
  550. */
  551. radeon_gpu_reset(rdev);
  552. radeon_fini(rdev);
  553. radeon_agp_disable(rdev);
  554. r = radeon_init(rdev);
  555. if (r)
  556. return r;
  557. }
  558. if (radeon_testing) {
  559. radeon_test_moves(rdev);
  560. }
  561. if (radeon_benchmarking) {
  562. radeon_benchmark(rdev);
  563. }
  564. return 0;
  565. }
  566. void radeon_device_fini(struct radeon_device *rdev)
  567. {
  568. DRM_INFO("radeon: finishing device.\n");
  569. rdev->shutdown = true;
  570. radeon_fini(rdev);
  571. destroy_workqueue(rdev->wq);
  572. vga_switcheroo_unregister_client(rdev->pdev);
  573. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  574. iounmap(rdev->rmmio);
  575. rdev->rmmio = NULL;
  576. }
  577. /*
  578. * Suspend & resume.
  579. */
  580. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  581. {
  582. struct radeon_device *rdev;
  583. struct drm_crtc *crtc;
  584. int r;
  585. if (dev == NULL || dev->dev_private == NULL) {
  586. return -ENODEV;
  587. }
  588. if (state.event == PM_EVENT_PRETHAW) {
  589. return 0;
  590. }
  591. rdev = dev->dev_private;
  592. if (rdev->powered_down)
  593. return 0;
  594. /* unpin the front buffers */
  595. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  596. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  597. struct radeon_bo *robj;
  598. if (rfb == NULL || rfb->obj == NULL) {
  599. continue;
  600. }
  601. robj = rfb->obj->driver_private;
  602. if (robj != rdev->fbdev_rbo) {
  603. r = radeon_bo_reserve(robj, false);
  604. if (unlikely(r == 0)) {
  605. radeon_bo_unpin(robj);
  606. radeon_bo_unreserve(robj);
  607. }
  608. }
  609. }
  610. /* evict vram memory */
  611. radeon_bo_evict_vram(rdev);
  612. /* wait for gpu to finish processing current batch */
  613. radeon_fence_wait_last(rdev);
  614. radeon_save_bios_scratch_regs(rdev);
  615. radeon_suspend(rdev);
  616. radeon_hpd_fini(rdev);
  617. /* evict remaining vram memory */
  618. radeon_bo_evict_vram(rdev);
  619. pci_save_state(dev->pdev);
  620. if (state.event == PM_EVENT_SUSPEND) {
  621. /* Shut down the device */
  622. pci_disable_device(dev->pdev);
  623. pci_set_power_state(dev->pdev, PCI_D3hot);
  624. }
  625. acquire_console_sem();
  626. fb_set_suspend(rdev->fbdev_info, 1);
  627. release_console_sem();
  628. return 0;
  629. }
  630. int radeon_resume_kms(struct drm_device *dev)
  631. {
  632. struct radeon_device *rdev = dev->dev_private;
  633. if (rdev->powered_down)
  634. return 0;
  635. acquire_console_sem();
  636. pci_set_power_state(dev->pdev, PCI_D0);
  637. pci_restore_state(dev->pdev);
  638. if (pci_enable_device(dev->pdev)) {
  639. release_console_sem();
  640. return -1;
  641. }
  642. pci_set_master(dev->pdev);
  643. /* resume AGP if in use */
  644. radeon_agp_resume(rdev);
  645. radeon_resume(rdev);
  646. radeon_restore_bios_scratch_regs(rdev);
  647. fb_set_suspend(rdev->fbdev_info, 0);
  648. release_console_sem();
  649. /* reset hpd state */
  650. radeon_hpd_init(rdev);
  651. /* blat the mode back in */
  652. drm_helper_resume_force_mode(dev);
  653. return 0;
  654. }
  655. /*
  656. * Debugfs
  657. */
  658. struct radeon_debugfs {
  659. struct drm_info_list *files;
  660. unsigned num_files;
  661. };
  662. static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES];
  663. static unsigned _radeon_debugfs_count = 0;
  664. int radeon_debugfs_add_files(struct radeon_device *rdev,
  665. struct drm_info_list *files,
  666. unsigned nfiles)
  667. {
  668. unsigned i;
  669. for (i = 0; i < _radeon_debugfs_count; i++) {
  670. if (_radeon_debugfs[i].files == files) {
  671. /* Already registered */
  672. return 0;
  673. }
  674. }
  675. if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) {
  676. DRM_ERROR("Reached maximum number of debugfs files.\n");
  677. DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n");
  678. return -EINVAL;
  679. }
  680. _radeon_debugfs[_radeon_debugfs_count].files = files;
  681. _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles;
  682. _radeon_debugfs_count++;
  683. #if defined(CONFIG_DEBUG_FS)
  684. drm_debugfs_create_files(files, nfiles,
  685. rdev->ddev->control->debugfs_root,
  686. rdev->ddev->control);
  687. drm_debugfs_create_files(files, nfiles,
  688. rdev->ddev->primary->debugfs_root,
  689. rdev->ddev->primary);
  690. #endif
  691. return 0;
  692. }
  693. #if defined(CONFIG_DEBUG_FS)
  694. int radeon_debugfs_init(struct drm_minor *minor)
  695. {
  696. return 0;
  697. }
  698. void radeon_debugfs_cleanup(struct drm_minor *minor)
  699. {
  700. unsigned i;
  701. for (i = 0; i < _radeon_debugfs_count; i++) {
  702. drm_debugfs_remove_files(_radeon_debugfs[i].files,
  703. _radeon_debugfs[i].num_files, minor);
  704. }
  705. }
  706. #endif