radeon_device.c 23 KB

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