radeon_device.c 26 KB

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