radeon_device.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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 <linux/efi.h>
  36. #include "radeon_reg.h"
  37. #include "radeon.h"
  38. #include "atom.h"
  39. static const char radeon_family_name[][16] = {
  40. "R100",
  41. "RV100",
  42. "RS100",
  43. "RV200",
  44. "RS200",
  45. "R200",
  46. "RV250",
  47. "RS300",
  48. "RV280",
  49. "R300",
  50. "R350",
  51. "RV350",
  52. "RV380",
  53. "R420",
  54. "R423",
  55. "RV410",
  56. "RS400",
  57. "RS480",
  58. "RS600",
  59. "RS690",
  60. "RS740",
  61. "RV515",
  62. "R520",
  63. "RV530",
  64. "RV560",
  65. "RV570",
  66. "R580",
  67. "R600",
  68. "RV610",
  69. "RV630",
  70. "RV670",
  71. "RV620",
  72. "RV635",
  73. "RS780",
  74. "RS880",
  75. "RV770",
  76. "RV730",
  77. "RV710",
  78. "RV740",
  79. "CEDAR",
  80. "REDWOOD",
  81. "JUNIPER",
  82. "CYPRESS",
  83. "HEMLOCK",
  84. "PALM",
  85. "SUMO",
  86. "SUMO2",
  87. "BARTS",
  88. "TURKS",
  89. "CAICOS",
  90. "CAYMAN",
  91. "ARUBA",
  92. "TAHITI",
  93. "PITCAIRN",
  94. "VERDE",
  95. "LAST",
  96. };
  97. /**
  98. * radeon_surface_init - Clear GPU surface registers.
  99. *
  100. * @rdev: radeon_device pointer
  101. *
  102. * Clear GPU surface registers (r1xx-r5xx).
  103. */
  104. void radeon_surface_init(struct radeon_device *rdev)
  105. {
  106. /* FIXME: check this out */
  107. if (rdev->family < CHIP_R600) {
  108. int i;
  109. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  110. if (rdev->surface_regs[i].bo)
  111. radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
  112. else
  113. radeon_clear_surface_reg(rdev, i);
  114. }
  115. /* enable surfaces */
  116. WREG32(RADEON_SURFACE_CNTL, 0);
  117. }
  118. }
  119. /*
  120. * GPU scratch registers helpers function.
  121. */
  122. /**
  123. * radeon_scratch_init - Init scratch register driver information.
  124. *
  125. * @rdev: radeon_device pointer
  126. *
  127. * Init CP scratch register driver information (r1xx-r5xx)
  128. */
  129. void radeon_scratch_init(struct radeon_device *rdev)
  130. {
  131. int i;
  132. /* FIXME: check this out */
  133. if (rdev->family < CHIP_R300) {
  134. rdev->scratch.num_reg = 5;
  135. } else {
  136. rdev->scratch.num_reg = 7;
  137. }
  138. rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
  139. for (i = 0; i < rdev->scratch.num_reg; i++) {
  140. rdev->scratch.free[i] = true;
  141. rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
  142. }
  143. }
  144. /**
  145. * radeon_scratch_get - Allocate a scratch register
  146. *
  147. * @rdev: radeon_device pointer
  148. * @reg: scratch register mmio offset
  149. *
  150. * Allocate a CP scratch register for use by the driver (all asics).
  151. * Returns 0 on success or -EINVAL on failure.
  152. */
  153. int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
  154. {
  155. int i;
  156. for (i = 0; i < rdev->scratch.num_reg; i++) {
  157. if (rdev->scratch.free[i]) {
  158. rdev->scratch.free[i] = false;
  159. *reg = rdev->scratch.reg[i];
  160. return 0;
  161. }
  162. }
  163. return -EINVAL;
  164. }
  165. /**
  166. * radeon_scratch_free - Free a scratch register
  167. *
  168. * @rdev: radeon_device pointer
  169. * @reg: scratch register mmio offset
  170. *
  171. * Free a CP scratch register allocated for use by the driver (all asics)
  172. */
  173. void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
  174. {
  175. int i;
  176. for (i = 0; i < rdev->scratch.num_reg; i++) {
  177. if (rdev->scratch.reg[i] == reg) {
  178. rdev->scratch.free[i] = true;
  179. return;
  180. }
  181. }
  182. }
  183. /*
  184. * radeon_wb_*()
  185. * Writeback is the the method by which the the GPU updates special pages
  186. * in memory with the status of certain GPU events (fences, ring pointers,
  187. * etc.).
  188. */
  189. /**
  190. * radeon_wb_disable - Disable Writeback
  191. *
  192. * @rdev: radeon_device pointer
  193. *
  194. * Disables Writeback (all asics). Used for suspend.
  195. */
  196. void radeon_wb_disable(struct radeon_device *rdev)
  197. {
  198. int r;
  199. if (rdev->wb.wb_obj) {
  200. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  201. if (unlikely(r != 0))
  202. return;
  203. radeon_bo_kunmap(rdev->wb.wb_obj);
  204. radeon_bo_unpin(rdev->wb.wb_obj);
  205. radeon_bo_unreserve(rdev->wb.wb_obj);
  206. }
  207. rdev->wb.enabled = false;
  208. }
  209. /**
  210. * radeon_wb_fini - Disable Writeback and free memory
  211. *
  212. * @rdev: radeon_device pointer
  213. *
  214. * Disables Writeback and frees the Writeback memory (all asics).
  215. * Used at driver shutdown.
  216. */
  217. void radeon_wb_fini(struct radeon_device *rdev)
  218. {
  219. radeon_wb_disable(rdev);
  220. if (rdev->wb.wb_obj) {
  221. radeon_bo_unref(&rdev->wb.wb_obj);
  222. rdev->wb.wb = NULL;
  223. rdev->wb.wb_obj = NULL;
  224. }
  225. }
  226. /**
  227. * radeon_wb_init- Init Writeback driver info and allocate memory
  228. *
  229. * @rdev: radeon_device pointer
  230. *
  231. * Disables Writeback and frees the Writeback memory (all asics).
  232. * Used at driver startup.
  233. * Returns 0 on success or an -error on failure.
  234. */
  235. int radeon_wb_init(struct radeon_device *rdev)
  236. {
  237. int r;
  238. if (rdev->wb.wb_obj == NULL) {
  239. r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
  240. RADEON_GEM_DOMAIN_GTT, NULL, &rdev->wb.wb_obj);
  241. if (r) {
  242. dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
  243. return r;
  244. }
  245. }
  246. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  247. if (unlikely(r != 0)) {
  248. radeon_wb_fini(rdev);
  249. return r;
  250. }
  251. r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
  252. &rdev->wb.gpu_addr);
  253. if (r) {
  254. radeon_bo_unreserve(rdev->wb.wb_obj);
  255. dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
  256. radeon_wb_fini(rdev);
  257. return r;
  258. }
  259. r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
  260. radeon_bo_unreserve(rdev->wb.wb_obj);
  261. if (r) {
  262. dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
  263. radeon_wb_fini(rdev);
  264. return r;
  265. }
  266. /* clear wb memory */
  267. memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
  268. /* disable event_write fences */
  269. rdev->wb.use_event = false;
  270. /* disabled via module param */
  271. if (radeon_no_wb == 1) {
  272. rdev->wb.enabled = false;
  273. } else {
  274. if (rdev->flags & RADEON_IS_AGP) {
  275. /* often unreliable on AGP */
  276. rdev->wb.enabled = false;
  277. } else if (rdev->family < CHIP_R300) {
  278. /* often unreliable on pre-r300 */
  279. rdev->wb.enabled = false;
  280. } else {
  281. rdev->wb.enabled = true;
  282. /* event_write fences are only available on r600+ */
  283. if (rdev->family >= CHIP_R600) {
  284. rdev->wb.use_event = true;
  285. }
  286. }
  287. }
  288. /* always use writeback/events on NI, APUs */
  289. if (rdev->family >= CHIP_PALM) {
  290. rdev->wb.enabled = true;
  291. rdev->wb.use_event = true;
  292. }
  293. dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
  294. return 0;
  295. }
  296. /**
  297. * radeon_vram_location - try to find VRAM location
  298. * @rdev: radeon device structure holding all necessary informations
  299. * @mc: memory controller structure holding memory informations
  300. * @base: base address at which to put VRAM
  301. *
  302. * Function will place try to place VRAM at base address provided
  303. * as parameter (which is so far either PCI aperture address or
  304. * for IGP TOM base address).
  305. *
  306. * If there is not enough space to fit the unvisible VRAM in the 32bits
  307. * address space then we limit the VRAM size to the aperture.
  308. *
  309. * If we are using AGP and if the AGP aperture doesn't allow us to have
  310. * room for all the VRAM than we restrict the VRAM to the PCI aperture
  311. * size and print a warning.
  312. *
  313. * This function will never fails, worst case are limiting VRAM.
  314. *
  315. * Note: GTT start, end, size should be initialized before calling this
  316. * function on AGP platform.
  317. *
  318. * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  319. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  320. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  321. * not IGP.
  322. *
  323. * Note: we use mc_vram_size as on some board we need to program the mc to
  324. * cover the whole aperture even if VRAM size is inferior to aperture size
  325. * Novell bug 204882 + along with lots of ubuntu ones
  326. *
  327. * Note: when limiting vram it's safe to overwritte real_vram_size because
  328. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  329. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  330. * ones)
  331. *
  332. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  333. * explicitly check for that thought.
  334. *
  335. * FIXME: when reducing VRAM size align new size on power of 2.
  336. */
  337. void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
  338. {
  339. uint64_t limit = (uint64_t)radeon_vram_limit << 20;
  340. mc->vram_start = base;
  341. if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
  342. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  343. mc->real_vram_size = mc->aper_size;
  344. mc->mc_vram_size = mc->aper_size;
  345. }
  346. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  347. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  348. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  349. mc->real_vram_size = mc->aper_size;
  350. mc->mc_vram_size = mc->aper_size;
  351. }
  352. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  353. if (limit && limit < mc->real_vram_size)
  354. mc->real_vram_size = limit;
  355. dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  356. mc->mc_vram_size >> 20, mc->vram_start,
  357. mc->vram_end, mc->real_vram_size >> 20);
  358. }
  359. /**
  360. * radeon_gtt_location - try to find GTT location
  361. * @rdev: radeon device structure holding all necessary informations
  362. * @mc: memory controller structure holding memory informations
  363. *
  364. * Function will place try to place GTT before or after VRAM.
  365. *
  366. * If GTT size is bigger than space left then we ajust GTT size.
  367. * Thus function will never fails.
  368. *
  369. * FIXME: when reducing GTT size align new size on power of 2.
  370. */
  371. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  372. {
  373. u64 size_af, size_bf;
  374. size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  375. size_bf = mc->vram_start & ~mc->gtt_base_align;
  376. if (size_bf > size_af) {
  377. if (mc->gtt_size > size_bf) {
  378. dev_warn(rdev->dev, "limiting GTT\n");
  379. mc->gtt_size = size_bf;
  380. }
  381. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  382. } else {
  383. if (mc->gtt_size > size_af) {
  384. dev_warn(rdev->dev, "limiting GTT\n");
  385. mc->gtt_size = size_af;
  386. }
  387. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  388. }
  389. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  390. dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  391. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  392. }
  393. /*
  394. * GPU helpers function.
  395. */
  396. /**
  397. * radeon_card_posted - check if the hw has already been initialized
  398. *
  399. * @rdev: radeon_device pointer
  400. *
  401. * Check if the asic has been initialized (all asics).
  402. * Used at driver startup.
  403. * Returns true if initialized or false if not.
  404. */
  405. bool radeon_card_posted(struct radeon_device *rdev)
  406. {
  407. uint32_t reg;
  408. if (efi_enabled(EFI_BOOT) &&
  409. rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE)
  410. return false;
  411. /* first check CRTCs */
  412. if (ASIC_IS_DCE41(rdev)) {
  413. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  414. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  415. if (reg & EVERGREEN_CRTC_MASTER_EN)
  416. return true;
  417. } else if (ASIC_IS_DCE4(rdev)) {
  418. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  419. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
  420. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  421. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
  422. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  423. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  424. if (reg & EVERGREEN_CRTC_MASTER_EN)
  425. return true;
  426. } else if (ASIC_IS_AVIVO(rdev)) {
  427. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  428. RREG32(AVIVO_D2CRTC_CONTROL);
  429. if (reg & AVIVO_CRTC_EN) {
  430. return true;
  431. }
  432. } else {
  433. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  434. RREG32(RADEON_CRTC2_GEN_CNTL);
  435. if (reg & RADEON_CRTC_EN) {
  436. return true;
  437. }
  438. }
  439. /* then check MEM_SIZE, in case the crtcs are off */
  440. if (rdev->family >= CHIP_R600)
  441. reg = RREG32(R600_CONFIG_MEMSIZE);
  442. else
  443. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  444. if (reg)
  445. return true;
  446. return false;
  447. }
  448. /**
  449. * radeon_update_bandwidth_info - update display bandwidth params
  450. *
  451. * @rdev: radeon_device pointer
  452. *
  453. * Used when sclk/mclk are switched or display modes are set.
  454. * params are used to calculate display watermarks (all asics)
  455. */
  456. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  457. {
  458. fixed20_12 a;
  459. u32 sclk = rdev->pm.current_sclk;
  460. u32 mclk = rdev->pm.current_mclk;
  461. /* sclk/mclk in Mhz */
  462. a.full = dfixed_const(100);
  463. rdev->pm.sclk.full = dfixed_const(sclk);
  464. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  465. rdev->pm.mclk.full = dfixed_const(mclk);
  466. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  467. if (rdev->flags & RADEON_IS_IGP) {
  468. a.full = dfixed_const(16);
  469. /* core_bandwidth = sclk(Mhz) * 16 */
  470. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  471. }
  472. }
  473. /**
  474. * radeon_boot_test_post_card - check and possibly initialize the hw
  475. *
  476. * @rdev: radeon_device pointer
  477. *
  478. * Check if the asic is initialized and if not, attempt to initialize
  479. * it (all asics).
  480. * Returns true if initialized or false if not.
  481. */
  482. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  483. {
  484. if (radeon_card_posted(rdev))
  485. return true;
  486. if (rdev->bios) {
  487. DRM_INFO("GPU not posted. posting now...\n");
  488. if (rdev->is_atom_bios)
  489. atom_asic_init(rdev->mode_info.atom_context);
  490. else
  491. radeon_combios_asic_init(rdev->ddev);
  492. return true;
  493. } else {
  494. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  495. return false;
  496. }
  497. }
  498. /**
  499. * radeon_dummy_page_init - init dummy page used by the driver
  500. *
  501. * @rdev: radeon_device pointer
  502. *
  503. * Allocate the dummy page used by the driver (all asics).
  504. * This dummy page is used by the driver as a filler for gart entries
  505. * when pages are taken out of the GART
  506. * Returns 0 on sucess, -ENOMEM on failure.
  507. */
  508. int radeon_dummy_page_init(struct radeon_device *rdev)
  509. {
  510. if (rdev->dummy_page.page)
  511. return 0;
  512. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  513. if (rdev->dummy_page.page == NULL)
  514. return -ENOMEM;
  515. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  516. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  517. if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
  518. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  519. __free_page(rdev->dummy_page.page);
  520. rdev->dummy_page.page = NULL;
  521. return -ENOMEM;
  522. }
  523. return 0;
  524. }
  525. /**
  526. * radeon_dummy_page_fini - free dummy page used by the driver
  527. *
  528. * @rdev: radeon_device pointer
  529. *
  530. * Frees the dummy page used by the driver (all asics).
  531. */
  532. void radeon_dummy_page_fini(struct radeon_device *rdev)
  533. {
  534. if (rdev->dummy_page.page == NULL)
  535. return;
  536. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  537. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  538. __free_page(rdev->dummy_page.page);
  539. rdev->dummy_page.page = NULL;
  540. }
  541. /* ATOM accessor methods */
  542. /*
  543. * ATOM is an interpreted byte code stored in tables in the vbios. The
  544. * driver registers callbacks to access registers and the interpreter
  545. * in the driver parses the tables and executes then to program specific
  546. * actions (set display modes, asic init, etc.). See radeon_atombios.c,
  547. * atombios.h, and atom.c
  548. */
  549. /**
  550. * cail_pll_read - read PLL register
  551. *
  552. * @info: atom card_info pointer
  553. * @reg: PLL register offset
  554. *
  555. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  556. * Returns the value of the PLL register.
  557. */
  558. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  559. {
  560. struct radeon_device *rdev = info->dev->dev_private;
  561. uint32_t r;
  562. r = rdev->pll_rreg(rdev, reg);
  563. return r;
  564. }
  565. /**
  566. * cail_pll_write - write PLL register
  567. *
  568. * @info: atom card_info pointer
  569. * @reg: PLL register offset
  570. * @val: value to write to the pll register
  571. *
  572. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  573. */
  574. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  575. {
  576. struct radeon_device *rdev = info->dev->dev_private;
  577. rdev->pll_wreg(rdev, reg, val);
  578. }
  579. /**
  580. * cail_mc_read - read MC (Memory Controller) register
  581. *
  582. * @info: atom card_info pointer
  583. * @reg: MC register offset
  584. *
  585. * Provides an MC register accessor for the atom interpreter (r4xx+).
  586. * Returns the value of the MC register.
  587. */
  588. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  589. {
  590. struct radeon_device *rdev = info->dev->dev_private;
  591. uint32_t r;
  592. r = rdev->mc_rreg(rdev, reg);
  593. return r;
  594. }
  595. /**
  596. * cail_mc_write - write MC (Memory Controller) register
  597. *
  598. * @info: atom card_info pointer
  599. * @reg: MC register offset
  600. * @val: value to write to the pll register
  601. *
  602. * Provides a MC register accessor for the atom interpreter (r4xx+).
  603. */
  604. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  605. {
  606. struct radeon_device *rdev = info->dev->dev_private;
  607. rdev->mc_wreg(rdev, reg, val);
  608. }
  609. /**
  610. * cail_reg_write - write MMIO register
  611. *
  612. * @info: atom card_info pointer
  613. * @reg: MMIO register offset
  614. * @val: value to write to the pll register
  615. *
  616. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  617. */
  618. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  619. {
  620. struct radeon_device *rdev = info->dev->dev_private;
  621. WREG32(reg*4, val);
  622. }
  623. /**
  624. * cail_reg_read - read MMIO register
  625. *
  626. * @info: atom card_info pointer
  627. * @reg: MMIO register offset
  628. *
  629. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  630. * Returns the value of the MMIO register.
  631. */
  632. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  633. {
  634. struct radeon_device *rdev = info->dev->dev_private;
  635. uint32_t r;
  636. r = RREG32(reg*4);
  637. return r;
  638. }
  639. /**
  640. * cail_ioreg_write - write IO register
  641. *
  642. * @info: atom card_info pointer
  643. * @reg: IO register offset
  644. * @val: value to write to the pll register
  645. *
  646. * Provides a IO register accessor for the atom interpreter (r4xx+).
  647. */
  648. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  649. {
  650. struct radeon_device *rdev = info->dev->dev_private;
  651. WREG32_IO(reg*4, val);
  652. }
  653. /**
  654. * cail_ioreg_read - read IO register
  655. *
  656. * @info: atom card_info pointer
  657. * @reg: IO register offset
  658. *
  659. * Provides an IO register accessor for the atom interpreter (r4xx+).
  660. * Returns the value of the IO register.
  661. */
  662. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  663. {
  664. struct radeon_device *rdev = info->dev->dev_private;
  665. uint32_t r;
  666. r = RREG32_IO(reg*4);
  667. return r;
  668. }
  669. /**
  670. * radeon_atombios_init - init the driver info and callbacks for atombios
  671. *
  672. * @rdev: radeon_device pointer
  673. *
  674. * Initializes the driver info and register access callbacks for the
  675. * ATOM interpreter (r4xx+).
  676. * Returns 0 on sucess, -ENOMEM on failure.
  677. * Called at driver startup.
  678. */
  679. int radeon_atombios_init(struct radeon_device *rdev)
  680. {
  681. struct card_info *atom_card_info =
  682. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  683. if (!atom_card_info)
  684. return -ENOMEM;
  685. rdev->mode_info.atom_card_info = atom_card_info;
  686. atom_card_info->dev = rdev->ddev;
  687. atom_card_info->reg_read = cail_reg_read;
  688. atom_card_info->reg_write = cail_reg_write;
  689. /* needed for iio ops */
  690. if (rdev->rio_mem) {
  691. atom_card_info->ioreg_read = cail_ioreg_read;
  692. atom_card_info->ioreg_write = cail_ioreg_write;
  693. } else {
  694. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  695. atom_card_info->ioreg_read = cail_reg_read;
  696. atom_card_info->ioreg_write = cail_reg_write;
  697. }
  698. atom_card_info->mc_read = cail_mc_read;
  699. atom_card_info->mc_write = cail_mc_write;
  700. atom_card_info->pll_read = cail_pll_read;
  701. atom_card_info->pll_write = cail_pll_write;
  702. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  703. mutex_init(&rdev->mode_info.atom_context->mutex);
  704. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  705. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  706. return 0;
  707. }
  708. /**
  709. * radeon_atombios_fini - free the driver info and callbacks for atombios
  710. *
  711. * @rdev: radeon_device pointer
  712. *
  713. * Frees the driver info and register access callbacks for the ATOM
  714. * interpreter (r4xx+).
  715. * Called at driver shutdown.
  716. */
  717. void radeon_atombios_fini(struct radeon_device *rdev)
  718. {
  719. if (rdev->mode_info.atom_context) {
  720. kfree(rdev->mode_info.atom_context->scratch);
  721. kfree(rdev->mode_info.atom_context);
  722. }
  723. kfree(rdev->mode_info.atom_card_info);
  724. }
  725. /* COMBIOS */
  726. /*
  727. * COMBIOS is the bios format prior to ATOM. It provides
  728. * command tables similar to ATOM, but doesn't have a unified
  729. * parser. See radeon_combios.c
  730. */
  731. /**
  732. * radeon_combios_init - init the driver info for combios
  733. *
  734. * @rdev: radeon_device pointer
  735. *
  736. * Initializes the driver info for combios (r1xx-r3xx).
  737. * Returns 0 on sucess.
  738. * Called at driver startup.
  739. */
  740. int radeon_combios_init(struct radeon_device *rdev)
  741. {
  742. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  743. return 0;
  744. }
  745. /**
  746. * radeon_combios_fini - free the driver info for combios
  747. *
  748. * @rdev: radeon_device pointer
  749. *
  750. * Frees the driver info for combios (r1xx-r3xx).
  751. * Called at driver shutdown.
  752. */
  753. void radeon_combios_fini(struct radeon_device *rdev)
  754. {
  755. }
  756. /* if we get transitioned to only one device, take VGA back */
  757. /**
  758. * radeon_vga_set_decode - enable/disable vga decode
  759. *
  760. * @cookie: radeon_device pointer
  761. * @state: enable/disable vga decode
  762. *
  763. * Enable/disable vga decode (all asics).
  764. * Returns VGA resource flags.
  765. */
  766. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  767. {
  768. struct radeon_device *rdev = cookie;
  769. radeon_vga_set_state(rdev, state);
  770. if (state)
  771. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  772. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  773. else
  774. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  775. }
  776. /**
  777. * radeon_check_pot_argument - check that argument is a power of two
  778. *
  779. * @arg: value to check
  780. *
  781. * Validates that a certain argument is a power of two (all asics).
  782. * Returns true if argument is valid.
  783. */
  784. static bool radeon_check_pot_argument(int arg)
  785. {
  786. return (arg & (arg - 1)) == 0;
  787. }
  788. /**
  789. * radeon_check_arguments - validate module params
  790. *
  791. * @rdev: radeon_device pointer
  792. *
  793. * Validates certain module parameters and updates
  794. * the associated values used by the driver (all asics).
  795. */
  796. static void radeon_check_arguments(struct radeon_device *rdev)
  797. {
  798. /* vramlimit must be a power of two */
  799. if (!radeon_check_pot_argument(radeon_vram_limit)) {
  800. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  801. radeon_vram_limit);
  802. radeon_vram_limit = 0;
  803. }
  804. /* gtt size must be power of two and greater or equal to 32M */
  805. if (radeon_gart_size < 32) {
  806. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  807. radeon_gart_size);
  808. radeon_gart_size = 512;
  809. } else if (!radeon_check_pot_argument(radeon_gart_size)) {
  810. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  811. radeon_gart_size);
  812. radeon_gart_size = 512;
  813. }
  814. rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
  815. /* AGP mode can only be -1, 1, 2, 4, 8 */
  816. switch (radeon_agpmode) {
  817. case -1:
  818. case 0:
  819. case 1:
  820. case 2:
  821. case 4:
  822. case 8:
  823. break;
  824. default:
  825. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  826. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  827. radeon_agpmode = 0;
  828. break;
  829. }
  830. }
  831. /**
  832. * radeon_switcheroo_quirk_long_wakeup - return true if longer d3 delay is
  833. * needed for waking up.
  834. *
  835. * @pdev: pci dev pointer
  836. */
  837. static bool radeon_switcheroo_quirk_long_wakeup(struct pci_dev *pdev)
  838. {
  839. /* 6600m in a macbook pro */
  840. if (pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
  841. pdev->subsystem_device == 0x00e2) {
  842. printk(KERN_INFO "radeon: quirking longer d3 wakeup delay\n");
  843. return true;
  844. }
  845. return false;
  846. }
  847. /**
  848. * radeon_switcheroo_set_state - set switcheroo state
  849. *
  850. * @pdev: pci dev pointer
  851. * @state: vga switcheroo state
  852. *
  853. * Callback for the switcheroo driver. Suspends or resumes the
  854. * the asics before or after it is powered up using ACPI methods.
  855. */
  856. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  857. {
  858. struct drm_device *dev = pci_get_drvdata(pdev);
  859. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  860. if (state == VGA_SWITCHEROO_ON) {
  861. unsigned d3_delay = dev->pdev->d3_delay;
  862. printk(KERN_INFO "radeon: switched on\n");
  863. /* don't suspend or resume card normally */
  864. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  865. if (d3_delay < 20 && radeon_switcheroo_quirk_long_wakeup(pdev))
  866. dev->pdev->d3_delay = 20;
  867. radeon_resume_kms(dev);
  868. dev->pdev->d3_delay = d3_delay;
  869. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  870. drm_kms_helper_poll_enable(dev);
  871. } else {
  872. printk(KERN_INFO "radeon: switched off\n");
  873. drm_kms_helper_poll_disable(dev);
  874. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  875. radeon_suspend_kms(dev, pmm);
  876. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  877. }
  878. }
  879. /**
  880. * radeon_switcheroo_can_switch - see if switcheroo state can change
  881. *
  882. * @pdev: pci dev pointer
  883. *
  884. * Callback for the switcheroo driver. Check of the switcheroo
  885. * state can be changed.
  886. * Returns true if the state can be changed, false if not.
  887. */
  888. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  889. {
  890. struct drm_device *dev = pci_get_drvdata(pdev);
  891. bool can_switch;
  892. spin_lock(&dev->count_lock);
  893. can_switch = (dev->open_count == 0);
  894. spin_unlock(&dev->count_lock);
  895. return can_switch;
  896. }
  897. static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
  898. .set_gpu_state = radeon_switcheroo_set_state,
  899. .reprobe = NULL,
  900. .can_switch = radeon_switcheroo_can_switch,
  901. };
  902. /**
  903. * radeon_device_init - initialize the driver
  904. *
  905. * @rdev: radeon_device pointer
  906. * @pdev: drm dev pointer
  907. * @pdev: pci dev pointer
  908. * @flags: driver flags
  909. *
  910. * Initializes the driver info and hw (all asics).
  911. * Returns 0 for success or an error on failure.
  912. * Called at driver startup.
  913. */
  914. int radeon_device_init(struct radeon_device *rdev,
  915. struct drm_device *ddev,
  916. struct pci_dev *pdev,
  917. uint32_t flags)
  918. {
  919. int r, i;
  920. int dma_bits;
  921. rdev->shutdown = false;
  922. rdev->dev = &pdev->dev;
  923. rdev->ddev = ddev;
  924. rdev->pdev = pdev;
  925. rdev->flags = flags;
  926. rdev->family = flags & RADEON_FAMILY_MASK;
  927. rdev->is_atom_bios = false;
  928. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  929. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  930. rdev->accel_working = false;
  931. /* set up ring ids */
  932. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  933. rdev->ring[i].idx = i;
  934. }
  935. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
  936. radeon_family_name[rdev->family], pdev->vendor, pdev->device,
  937. pdev->subsystem_vendor, pdev->subsystem_device);
  938. /* mutex initialization are all done here so we
  939. * can recall function without having locking issues */
  940. mutex_init(&rdev->ring_lock);
  941. mutex_init(&rdev->dc_hw_i2c_mutex);
  942. atomic_set(&rdev->ih.lock, 0);
  943. mutex_init(&rdev->gem.mutex);
  944. mutex_init(&rdev->pm.mutex);
  945. mutex_init(&rdev->gpu_clock_mutex);
  946. init_rwsem(&rdev->pm.mclk_lock);
  947. init_rwsem(&rdev->exclusive_lock);
  948. init_waitqueue_head(&rdev->irq.vblank_queue);
  949. r = radeon_gem_init(rdev);
  950. if (r)
  951. return r;
  952. /* initialize vm here */
  953. mutex_init(&rdev->vm_manager.lock);
  954. /* Adjust VM size here.
  955. * Currently set to 4GB ((1 << 20) 4k pages).
  956. * Max GPUVM size for cayman and SI is 40 bits.
  957. */
  958. rdev->vm_manager.max_pfn = 1 << 20;
  959. INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
  960. /* Set asic functions */
  961. r = radeon_asic_init(rdev);
  962. if (r)
  963. return r;
  964. radeon_check_arguments(rdev);
  965. /* all of the newer IGP chips have an internal gart
  966. * However some rs4xx report as AGP, so remove that here.
  967. */
  968. if ((rdev->family >= CHIP_RS400) &&
  969. (rdev->flags & RADEON_IS_IGP)) {
  970. rdev->flags &= ~RADEON_IS_AGP;
  971. }
  972. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  973. radeon_agp_disable(rdev);
  974. }
  975. /* set DMA mask + need_dma32 flags.
  976. * PCIE - can handle 40-bits.
  977. * IGP - can handle 40-bits
  978. * AGP - generally dma32 is safest
  979. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  980. */
  981. rdev->need_dma32 = false;
  982. if (rdev->flags & RADEON_IS_AGP)
  983. rdev->need_dma32 = true;
  984. if ((rdev->flags & RADEON_IS_PCI) &&
  985. (rdev->family <= CHIP_RS740))
  986. rdev->need_dma32 = true;
  987. dma_bits = rdev->need_dma32 ? 32 : 40;
  988. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  989. if (r) {
  990. rdev->need_dma32 = true;
  991. dma_bits = 32;
  992. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  993. }
  994. r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  995. if (r) {
  996. pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
  997. printk(KERN_WARNING "radeon: No coherent DMA available.\n");
  998. }
  999. /* Registers mapping */
  1000. /* TODO: block userspace mapping of io register */
  1001. spin_lock_init(&rdev->mmio_idx_lock);
  1002. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  1003. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  1004. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  1005. if (rdev->rmmio == NULL) {
  1006. return -ENOMEM;
  1007. }
  1008. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  1009. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  1010. /* io port mapping */
  1011. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1012. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  1013. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  1014. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  1015. break;
  1016. }
  1017. }
  1018. if (rdev->rio_mem == NULL)
  1019. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1020. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  1021. /* this will fail for cards that aren't VGA class devices, just
  1022. * ignore it */
  1023. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  1024. vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops);
  1025. r = radeon_init(rdev);
  1026. if (r)
  1027. return r;
  1028. r = radeon_ib_ring_tests(rdev);
  1029. if (r)
  1030. DRM_ERROR("ib ring test failed (%d).\n", r);
  1031. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  1032. /* Acceleration not working on AGP card try again
  1033. * with fallback to PCI or PCIE GART
  1034. */
  1035. radeon_asic_reset(rdev);
  1036. radeon_fini(rdev);
  1037. radeon_agp_disable(rdev);
  1038. r = radeon_init(rdev);
  1039. if (r)
  1040. return r;
  1041. }
  1042. if ((radeon_testing & 1)) {
  1043. radeon_test_moves(rdev);
  1044. }
  1045. if ((radeon_testing & 2)) {
  1046. radeon_test_syncing(rdev);
  1047. }
  1048. if (radeon_benchmarking) {
  1049. radeon_benchmark(rdev, radeon_benchmarking);
  1050. }
  1051. return 0;
  1052. }
  1053. static void radeon_debugfs_remove_files(struct radeon_device *rdev);
  1054. /**
  1055. * radeon_device_fini - tear down the driver
  1056. *
  1057. * @rdev: radeon_device pointer
  1058. *
  1059. * Tear down the driver info (all asics).
  1060. * Called at driver shutdown.
  1061. */
  1062. void radeon_device_fini(struct radeon_device *rdev)
  1063. {
  1064. DRM_INFO("radeon: finishing device.\n");
  1065. rdev->shutdown = true;
  1066. /* evict vram memory */
  1067. radeon_bo_evict_vram(rdev);
  1068. radeon_fini(rdev);
  1069. vga_switcheroo_unregister_client(rdev->pdev);
  1070. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  1071. if (rdev->rio_mem)
  1072. pci_iounmap(rdev->pdev, rdev->rio_mem);
  1073. rdev->rio_mem = NULL;
  1074. iounmap(rdev->rmmio);
  1075. rdev->rmmio = NULL;
  1076. radeon_debugfs_remove_files(rdev);
  1077. }
  1078. /*
  1079. * Suspend & resume.
  1080. */
  1081. /**
  1082. * radeon_suspend_kms - initiate device suspend
  1083. *
  1084. * @pdev: drm dev pointer
  1085. * @state: suspend state
  1086. *
  1087. * Puts the hw in the suspend state (all asics).
  1088. * Returns 0 for success or an error on failure.
  1089. * Called at driver suspend.
  1090. */
  1091. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  1092. {
  1093. struct radeon_device *rdev;
  1094. struct drm_crtc *crtc;
  1095. struct drm_connector *connector;
  1096. int i, r;
  1097. bool force_completion = false;
  1098. if (dev == NULL || dev->dev_private == NULL) {
  1099. return -ENODEV;
  1100. }
  1101. if (state.event == PM_EVENT_PRETHAW) {
  1102. return 0;
  1103. }
  1104. rdev = dev->dev_private;
  1105. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1106. return 0;
  1107. drm_kms_helper_poll_disable(dev);
  1108. /* turn off display hw */
  1109. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1110. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1111. }
  1112. /* unpin the front buffers */
  1113. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1114. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  1115. struct radeon_bo *robj;
  1116. if (rfb == NULL || rfb->obj == NULL) {
  1117. continue;
  1118. }
  1119. robj = gem_to_radeon_bo(rfb->obj);
  1120. /* don't unpin kernel fb objects */
  1121. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  1122. r = radeon_bo_reserve(robj, false);
  1123. if (r == 0) {
  1124. radeon_bo_unpin(robj);
  1125. radeon_bo_unreserve(robj);
  1126. }
  1127. }
  1128. }
  1129. /* evict vram memory */
  1130. radeon_bo_evict_vram(rdev);
  1131. mutex_lock(&rdev->ring_lock);
  1132. /* wait for gpu to finish processing current batch */
  1133. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  1134. r = radeon_fence_wait_empty_locked(rdev, i);
  1135. if (r) {
  1136. /* delay GPU reset to resume */
  1137. force_completion = true;
  1138. }
  1139. }
  1140. if (force_completion) {
  1141. radeon_fence_driver_force_completion(rdev);
  1142. }
  1143. mutex_unlock(&rdev->ring_lock);
  1144. radeon_save_bios_scratch_regs(rdev);
  1145. radeon_pm_suspend(rdev);
  1146. radeon_suspend(rdev);
  1147. radeon_hpd_fini(rdev);
  1148. /* evict remaining vram memory */
  1149. radeon_bo_evict_vram(rdev);
  1150. radeon_agp_suspend(rdev);
  1151. pci_save_state(dev->pdev);
  1152. if (state.event == PM_EVENT_SUSPEND) {
  1153. /* Shut down the device */
  1154. pci_disable_device(dev->pdev);
  1155. pci_set_power_state(dev->pdev, PCI_D3hot);
  1156. }
  1157. console_lock();
  1158. radeon_fbdev_set_suspend(rdev, 1);
  1159. console_unlock();
  1160. return 0;
  1161. }
  1162. /**
  1163. * radeon_resume_kms - initiate device resume
  1164. *
  1165. * @pdev: drm dev pointer
  1166. *
  1167. * Bring the hw back to operating state (all asics).
  1168. * Returns 0 for success or an error on failure.
  1169. * Called at driver resume.
  1170. */
  1171. int radeon_resume_kms(struct drm_device *dev)
  1172. {
  1173. struct drm_connector *connector;
  1174. struct radeon_device *rdev = dev->dev_private;
  1175. int r;
  1176. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1177. return 0;
  1178. console_lock();
  1179. pci_set_power_state(dev->pdev, PCI_D0);
  1180. pci_restore_state(dev->pdev);
  1181. if (pci_enable_device(dev->pdev)) {
  1182. console_unlock();
  1183. return -1;
  1184. }
  1185. /* resume AGP if in use */
  1186. radeon_agp_resume(rdev);
  1187. radeon_resume(rdev);
  1188. r = radeon_ib_ring_tests(rdev);
  1189. if (r)
  1190. DRM_ERROR("ib ring test failed (%d).\n", r);
  1191. radeon_pm_resume(rdev);
  1192. radeon_restore_bios_scratch_regs(rdev);
  1193. radeon_fbdev_set_suspend(rdev, 0);
  1194. console_unlock();
  1195. /* init dig PHYs, disp eng pll */
  1196. if (rdev->is_atom_bios) {
  1197. radeon_atom_encoder_init(rdev);
  1198. radeon_atom_disp_eng_pll_init(rdev);
  1199. /* turn on the BL */
  1200. if (rdev->mode_info.bl_encoder) {
  1201. u8 bl_level = radeon_get_backlight_level(rdev,
  1202. rdev->mode_info.bl_encoder);
  1203. radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
  1204. bl_level);
  1205. }
  1206. }
  1207. /* reset hpd state */
  1208. radeon_hpd_init(rdev);
  1209. /* blat the mode back in */
  1210. drm_helper_resume_force_mode(dev);
  1211. /* turn on display hw */
  1212. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1213. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1214. }
  1215. drm_kms_helper_poll_enable(dev);
  1216. return 0;
  1217. }
  1218. /**
  1219. * radeon_gpu_reset - reset the asic
  1220. *
  1221. * @rdev: radeon device pointer
  1222. *
  1223. * Attempt the reset the GPU if it has hung (all asics).
  1224. * Returns 0 for success or an error on failure.
  1225. */
  1226. int radeon_gpu_reset(struct radeon_device *rdev)
  1227. {
  1228. unsigned ring_sizes[RADEON_NUM_RINGS];
  1229. uint32_t *ring_data[RADEON_NUM_RINGS];
  1230. bool saved = false;
  1231. int i, r;
  1232. int resched;
  1233. down_write(&rdev->exclusive_lock);
  1234. radeon_save_bios_scratch_regs(rdev);
  1235. /* block TTM */
  1236. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  1237. radeon_suspend(rdev);
  1238. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1239. ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
  1240. &ring_data[i]);
  1241. if (ring_sizes[i]) {
  1242. saved = true;
  1243. dev_info(rdev->dev, "Saved %d dwords of commands "
  1244. "on ring %d.\n", ring_sizes[i], i);
  1245. }
  1246. }
  1247. retry:
  1248. r = radeon_asic_reset(rdev);
  1249. if (!r) {
  1250. dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
  1251. radeon_resume(rdev);
  1252. }
  1253. radeon_restore_bios_scratch_regs(rdev);
  1254. if (!r) {
  1255. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1256. radeon_ring_restore(rdev, &rdev->ring[i],
  1257. ring_sizes[i], ring_data[i]);
  1258. ring_sizes[i] = 0;
  1259. ring_data[i] = NULL;
  1260. }
  1261. r = radeon_ib_ring_tests(rdev);
  1262. if (r) {
  1263. dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
  1264. if (saved) {
  1265. saved = false;
  1266. radeon_suspend(rdev);
  1267. goto retry;
  1268. }
  1269. }
  1270. } else {
  1271. radeon_fence_driver_force_completion(rdev);
  1272. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1273. kfree(ring_data[i]);
  1274. }
  1275. }
  1276. drm_helper_resume_force_mode(rdev->ddev);
  1277. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  1278. if (r) {
  1279. /* bad news, how to tell it to userspace ? */
  1280. dev_info(rdev->dev, "GPU reset failed\n");
  1281. }
  1282. up_write(&rdev->exclusive_lock);
  1283. return r;
  1284. }
  1285. /*
  1286. * Debugfs
  1287. */
  1288. int radeon_debugfs_add_files(struct radeon_device *rdev,
  1289. struct drm_info_list *files,
  1290. unsigned nfiles)
  1291. {
  1292. unsigned i;
  1293. for (i = 0; i < rdev->debugfs_count; i++) {
  1294. if (rdev->debugfs[i].files == files) {
  1295. /* Already registered */
  1296. return 0;
  1297. }
  1298. }
  1299. i = rdev->debugfs_count + 1;
  1300. if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
  1301. DRM_ERROR("Reached maximum number of debugfs components.\n");
  1302. DRM_ERROR("Report so we increase "
  1303. "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
  1304. return -EINVAL;
  1305. }
  1306. rdev->debugfs[rdev->debugfs_count].files = files;
  1307. rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
  1308. rdev->debugfs_count = i;
  1309. #if defined(CONFIG_DEBUG_FS)
  1310. drm_debugfs_create_files(files, nfiles,
  1311. rdev->ddev->control->debugfs_root,
  1312. rdev->ddev->control);
  1313. drm_debugfs_create_files(files, nfiles,
  1314. rdev->ddev->primary->debugfs_root,
  1315. rdev->ddev->primary);
  1316. #endif
  1317. return 0;
  1318. }
  1319. static void radeon_debugfs_remove_files(struct radeon_device *rdev)
  1320. {
  1321. #if defined(CONFIG_DEBUG_FS)
  1322. unsigned i;
  1323. for (i = 0; i < rdev->debugfs_count; i++) {
  1324. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1325. rdev->debugfs[i].num_files,
  1326. rdev->ddev->control);
  1327. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1328. rdev->debugfs[i].num_files,
  1329. rdev->ddev->primary);
  1330. }
  1331. #endif
  1332. }
  1333. #if defined(CONFIG_DEBUG_FS)
  1334. int radeon_debugfs_init(struct drm_minor *minor)
  1335. {
  1336. return 0;
  1337. }
  1338. void radeon_debugfs_cleanup(struct drm_minor *minor)
  1339. {
  1340. }
  1341. #endif