radeon_device.c 24 KB

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