radeon_device.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. mc->vram_start = base;
  340. if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
  341. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  342. mc->real_vram_size = mc->aper_size;
  343. mc->mc_vram_size = mc->aper_size;
  344. }
  345. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  346. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  347. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  348. mc->real_vram_size = mc->aper_size;
  349. mc->mc_vram_size = mc->aper_size;
  350. }
  351. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  352. if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size)
  353. mc->real_vram_size = radeon_vram_limit;
  354. dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  355. mc->mc_vram_size >> 20, mc->vram_start,
  356. mc->vram_end, mc->real_vram_size >> 20);
  357. }
  358. /**
  359. * radeon_gtt_location - try to find GTT location
  360. * @rdev: radeon device structure holding all necessary informations
  361. * @mc: memory controller structure holding memory informations
  362. *
  363. * Function will place try to place GTT before or after VRAM.
  364. *
  365. * If GTT size is bigger than space left then we ajust GTT size.
  366. * Thus function will never fails.
  367. *
  368. * FIXME: when reducing GTT size align new size on power of 2.
  369. */
  370. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  371. {
  372. u64 size_af, size_bf;
  373. size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  374. size_bf = mc->vram_start & ~mc->gtt_base_align;
  375. if (size_bf > size_af) {
  376. if (mc->gtt_size > size_bf) {
  377. dev_warn(rdev->dev, "limiting GTT\n");
  378. mc->gtt_size = size_bf;
  379. }
  380. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  381. } else {
  382. if (mc->gtt_size > size_af) {
  383. dev_warn(rdev->dev, "limiting GTT\n");
  384. mc->gtt_size = size_af;
  385. }
  386. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  387. }
  388. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  389. dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  390. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  391. }
  392. /*
  393. * GPU helpers function.
  394. */
  395. /**
  396. * radeon_card_posted - check if the hw has already been initialized
  397. *
  398. * @rdev: radeon_device pointer
  399. *
  400. * Check if the asic has been initialized (all asics).
  401. * Used at driver startup.
  402. * Returns true if initialized or false if not.
  403. */
  404. bool radeon_card_posted(struct radeon_device *rdev)
  405. {
  406. uint32_t reg;
  407. if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE)
  408. return false;
  409. /* first check CRTCs */
  410. if (ASIC_IS_DCE41(rdev)) {
  411. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  412. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  413. if (reg & EVERGREEN_CRTC_MASTER_EN)
  414. return true;
  415. } else if (ASIC_IS_DCE4(rdev)) {
  416. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  417. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
  418. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  419. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
  420. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  421. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  422. if (reg & EVERGREEN_CRTC_MASTER_EN)
  423. return true;
  424. } else if (ASIC_IS_AVIVO(rdev)) {
  425. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  426. RREG32(AVIVO_D2CRTC_CONTROL);
  427. if (reg & AVIVO_CRTC_EN) {
  428. return true;
  429. }
  430. } else {
  431. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  432. RREG32(RADEON_CRTC2_GEN_CNTL);
  433. if (reg & RADEON_CRTC_EN) {
  434. return true;
  435. }
  436. }
  437. /* then check MEM_SIZE, in case the crtcs are off */
  438. if (rdev->family >= CHIP_R600)
  439. reg = RREG32(R600_CONFIG_MEMSIZE);
  440. else
  441. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  442. if (reg)
  443. return true;
  444. return false;
  445. }
  446. /**
  447. * radeon_update_bandwidth_info - update display bandwidth params
  448. *
  449. * @rdev: radeon_device pointer
  450. *
  451. * Used when sclk/mclk are switched or display modes are set.
  452. * params are used to calculate display watermarks (all asics)
  453. */
  454. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  455. {
  456. fixed20_12 a;
  457. u32 sclk = rdev->pm.current_sclk;
  458. u32 mclk = rdev->pm.current_mclk;
  459. /* sclk/mclk in Mhz */
  460. a.full = dfixed_const(100);
  461. rdev->pm.sclk.full = dfixed_const(sclk);
  462. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  463. rdev->pm.mclk.full = dfixed_const(mclk);
  464. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  465. if (rdev->flags & RADEON_IS_IGP) {
  466. a.full = dfixed_const(16);
  467. /* core_bandwidth = sclk(Mhz) * 16 */
  468. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  469. }
  470. }
  471. /**
  472. * radeon_boot_test_post_card - check and possibly initialize the hw
  473. *
  474. * @rdev: radeon_device pointer
  475. *
  476. * Check if the asic is initialized and if not, attempt to initialize
  477. * it (all asics).
  478. * Returns true if initialized or false if not.
  479. */
  480. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  481. {
  482. if (radeon_card_posted(rdev))
  483. return true;
  484. if (rdev->bios) {
  485. DRM_INFO("GPU not posted. posting now...\n");
  486. if (rdev->is_atom_bios)
  487. atom_asic_init(rdev->mode_info.atom_context);
  488. else
  489. radeon_combios_asic_init(rdev->ddev);
  490. return true;
  491. } else {
  492. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  493. return false;
  494. }
  495. }
  496. /**
  497. * radeon_dummy_page_init - init dummy page used by the driver
  498. *
  499. * @rdev: radeon_device pointer
  500. *
  501. * Allocate the dummy page used by the driver (all asics).
  502. * This dummy page is used by the driver as a filler for gart entries
  503. * when pages are taken out of the GART
  504. * Returns 0 on sucess, -ENOMEM on failure.
  505. */
  506. int radeon_dummy_page_init(struct radeon_device *rdev)
  507. {
  508. if (rdev->dummy_page.page)
  509. return 0;
  510. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  511. if (rdev->dummy_page.page == NULL)
  512. return -ENOMEM;
  513. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  514. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  515. if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
  516. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  517. __free_page(rdev->dummy_page.page);
  518. rdev->dummy_page.page = NULL;
  519. return -ENOMEM;
  520. }
  521. return 0;
  522. }
  523. /**
  524. * radeon_dummy_page_fini - free dummy page used by the driver
  525. *
  526. * @rdev: radeon_device pointer
  527. *
  528. * Frees the dummy page used by the driver (all asics).
  529. */
  530. void radeon_dummy_page_fini(struct radeon_device *rdev)
  531. {
  532. if (rdev->dummy_page.page == NULL)
  533. return;
  534. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  535. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  536. __free_page(rdev->dummy_page.page);
  537. rdev->dummy_page.page = NULL;
  538. }
  539. /* ATOM accessor methods */
  540. /*
  541. * ATOM is an interpreted byte code stored in tables in the vbios. The
  542. * driver registers callbacks to access registers and the interpreter
  543. * in the driver parses the tables and executes then to program specific
  544. * actions (set display modes, asic init, etc.). See radeon_atombios.c,
  545. * atombios.h, and atom.c
  546. */
  547. /**
  548. * cail_pll_read - read PLL register
  549. *
  550. * @info: atom card_info pointer
  551. * @reg: PLL register offset
  552. *
  553. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  554. * Returns the value of the PLL register.
  555. */
  556. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  557. {
  558. struct radeon_device *rdev = info->dev->dev_private;
  559. uint32_t r;
  560. r = rdev->pll_rreg(rdev, reg);
  561. return r;
  562. }
  563. /**
  564. * cail_pll_write - write PLL register
  565. *
  566. * @info: atom card_info pointer
  567. * @reg: PLL register offset
  568. * @val: value to write to the pll register
  569. *
  570. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  571. */
  572. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  573. {
  574. struct radeon_device *rdev = info->dev->dev_private;
  575. rdev->pll_wreg(rdev, reg, val);
  576. }
  577. /**
  578. * cail_mc_read - read MC (Memory Controller) register
  579. *
  580. * @info: atom card_info pointer
  581. * @reg: MC register offset
  582. *
  583. * Provides an MC register accessor for the atom interpreter (r4xx+).
  584. * Returns the value of the MC register.
  585. */
  586. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  587. {
  588. struct radeon_device *rdev = info->dev->dev_private;
  589. uint32_t r;
  590. r = rdev->mc_rreg(rdev, reg);
  591. return r;
  592. }
  593. /**
  594. * cail_mc_write - write MC (Memory Controller) register
  595. *
  596. * @info: atom card_info pointer
  597. * @reg: MC register offset
  598. * @val: value to write to the pll register
  599. *
  600. * Provides a MC register accessor for the atom interpreter (r4xx+).
  601. */
  602. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  603. {
  604. struct radeon_device *rdev = info->dev->dev_private;
  605. rdev->mc_wreg(rdev, reg, val);
  606. }
  607. /**
  608. * cail_reg_write - write MMIO register
  609. *
  610. * @info: atom card_info pointer
  611. * @reg: MMIO register offset
  612. * @val: value to write to the pll register
  613. *
  614. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  615. */
  616. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  617. {
  618. struct radeon_device *rdev = info->dev->dev_private;
  619. WREG32(reg*4, val);
  620. }
  621. /**
  622. * cail_reg_read - read MMIO register
  623. *
  624. * @info: atom card_info pointer
  625. * @reg: MMIO register offset
  626. *
  627. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  628. * Returns the value of the MMIO register.
  629. */
  630. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  631. {
  632. struct radeon_device *rdev = info->dev->dev_private;
  633. uint32_t r;
  634. r = RREG32(reg*4);
  635. return r;
  636. }
  637. /**
  638. * cail_ioreg_write - write IO register
  639. *
  640. * @info: atom card_info pointer
  641. * @reg: IO register offset
  642. * @val: value to write to the pll register
  643. *
  644. * Provides a IO register accessor for the atom interpreter (r4xx+).
  645. */
  646. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  647. {
  648. struct radeon_device *rdev = info->dev->dev_private;
  649. WREG32_IO(reg*4, val);
  650. }
  651. /**
  652. * cail_ioreg_read - read IO register
  653. *
  654. * @info: atom card_info pointer
  655. * @reg: IO register offset
  656. *
  657. * Provides an IO register accessor for the atom interpreter (r4xx+).
  658. * Returns the value of the IO register.
  659. */
  660. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  661. {
  662. struct radeon_device *rdev = info->dev->dev_private;
  663. uint32_t r;
  664. r = RREG32_IO(reg*4);
  665. return r;
  666. }
  667. /**
  668. * radeon_atombios_init - init the driver info and callbacks for atombios
  669. *
  670. * @rdev: radeon_device pointer
  671. *
  672. * Initializes the driver info and register access callbacks for the
  673. * ATOM interpreter (r4xx+).
  674. * Returns 0 on sucess, -ENOMEM on failure.
  675. * Called at driver startup.
  676. */
  677. int radeon_atombios_init(struct radeon_device *rdev)
  678. {
  679. struct card_info *atom_card_info =
  680. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  681. if (!atom_card_info)
  682. return -ENOMEM;
  683. rdev->mode_info.atom_card_info = atom_card_info;
  684. atom_card_info->dev = rdev->ddev;
  685. atom_card_info->reg_read = cail_reg_read;
  686. atom_card_info->reg_write = cail_reg_write;
  687. /* needed for iio ops */
  688. if (rdev->rio_mem) {
  689. atom_card_info->ioreg_read = cail_ioreg_read;
  690. atom_card_info->ioreg_write = cail_ioreg_write;
  691. } else {
  692. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  693. atom_card_info->ioreg_read = cail_reg_read;
  694. atom_card_info->ioreg_write = cail_reg_write;
  695. }
  696. atom_card_info->mc_read = cail_mc_read;
  697. atom_card_info->mc_write = cail_mc_write;
  698. atom_card_info->pll_read = cail_pll_read;
  699. atom_card_info->pll_write = cail_pll_write;
  700. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  701. mutex_init(&rdev->mode_info.atom_context->mutex);
  702. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  703. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  704. return 0;
  705. }
  706. /**
  707. * radeon_atombios_fini - free the driver info and callbacks for atombios
  708. *
  709. * @rdev: radeon_device pointer
  710. *
  711. * Frees the driver info and register access callbacks for the ATOM
  712. * interpreter (r4xx+).
  713. * Called at driver shutdown.
  714. */
  715. void radeon_atombios_fini(struct radeon_device *rdev)
  716. {
  717. if (rdev->mode_info.atom_context) {
  718. kfree(rdev->mode_info.atom_context->scratch);
  719. kfree(rdev->mode_info.atom_context);
  720. }
  721. kfree(rdev->mode_info.atom_card_info);
  722. }
  723. /* COMBIOS */
  724. /*
  725. * COMBIOS is the bios format prior to ATOM. It provides
  726. * command tables similar to ATOM, but doesn't have a unified
  727. * parser. See radeon_combios.c
  728. */
  729. /**
  730. * radeon_combios_init - init the driver info for combios
  731. *
  732. * @rdev: radeon_device pointer
  733. *
  734. * Initializes the driver info for combios (r1xx-r3xx).
  735. * Returns 0 on sucess.
  736. * Called at driver startup.
  737. */
  738. int radeon_combios_init(struct radeon_device *rdev)
  739. {
  740. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  741. return 0;
  742. }
  743. /**
  744. * radeon_combios_fini - free the driver info for combios
  745. *
  746. * @rdev: radeon_device pointer
  747. *
  748. * Frees the driver info for combios (r1xx-r3xx).
  749. * Called at driver shutdown.
  750. */
  751. void radeon_combios_fini(struct radeon_device *rdev)
  752. {
  753. }
  754. /* if we get transitioned to only one device, take VGA back */
  755. /**
  756. * radeon_vga_set_decode - enable/disable vga decode
  757. *
  758. * @cookie: radeon_device pointer
  759. * @state: enable/disable vga decode
  760. *
  761. * Enable/disable vga decode (all asics).
  762. * Returns VGA resource flags.
  763. */
  764. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  765. {
  766. struct radeon_device *rdev = cookie;
  767. radeon_vga_set_state(rdev, state);
  768. if (state)
  769. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  770. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  771. else
  772. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  773. }
  774. /**
  775. * radeon_check_arguments - validate module params
  776. *
  777. * @rdev: radeon_device pointer
  778. *
  779. * Validates certain module parameters and updates
  780. * the associated values used by the driver (all asics).
  781. */
  782. void radeon_check_arguments(struct radeon_device *rdev)
  783. {
  784. /* vramlimit must be a power of two */
  785. switch (radeon_vram_limit) {
  786. case 0:
  787. case 4:
  788. case 8:
  789. case 16:
  790. case 32:
  791. case 64:
  792. case 128:
  793. case 256:
  794. case 512:
  795. case 1024:
  796. case 2048:
  797. case 4096:
  798. break;
  799. default:
  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. break;
  804. }
  805. radeon_vram_limit = radeon_vram_limit << 20;
  806. /* gtt size must be power of two and greater or equal to 32M */
  807. switch (radeon_gart_size) {
  808. case 4:
  809. case 8:
  810. case 16:
  811. dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
  812. radeon_gart_size);
  813. radeon_gart_size = 512;
  814. break;
  815. case 32:
  816. case 64:
  817. case 128:
  818. case 256:
  819. case 512:
  820. case 1024:
  821. case 2048:
  822. case 4096:
  823. break;
  824. default:
  825. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  826. radeon_gart_size);
  827. radeon_gart_size = 512;
  828. break;
  829. }
  830. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  831. /* AGP mode can only be -1, 1, 2, 4, 8 */
  832. switch (radeon_agpmode) {
  833. case -1:
  834. case 0:
  835. case 1:
  836. case 2:
  837. case 4:
  838. case 8:
  839. break;
  840. default:
  841. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  842. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  843. radeon_agpmode = 0;
  844. break;
  845. }
  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. printk(KERN_INFO "radeon: switched on\n");
  862. /* don't suspend or resume card normally */
  863. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  864. radeon_resume_kms(dev);
  865. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  866. drm_kms_helper_poll_enable(dev);
  867. } else {
  868. printk(KERN_INFO "radeon: switched off\n");
  869. drm_kms_helper_poll_disable(dev);
  870. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  871. radeon_suspend_kms(dev, pmm);
  872. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  873. }
  874. }
  875. /**
  876. * radeon_switcheroo_can_switch - see if switcheroo state can change
  877. *
  878. * @pdev: pci dev pointer
  879. *
  880. * Callback for the switcheroo driver. Check of the switcheroo
  881. * state can be changed.
  882. * Returns true if the state can be changed, false if not.
  883. */
  884. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  885. {
  886. struct drm_device *dev = pci_get_drvdata(pdev);
  887. bool can_switch;
  888. spin_lock(&dev->count_lock);
  889. can_switch = (dev->open_count == 0);
  890. spin_unlock(&dev->count_lock);
  891. return can_switch;
  892. }
  893. static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
  894. .set_gpu_state = radeon_switcheroo_set_state,
  895. .reprobe = NULL,
  896. .can_switch = radeon_switcheroo_can_switch,
  897. };
  898. /**
  899. * radeon_device_init - initialize the driver
  900. *
  901. * @rdev: radeon_device pointer
  902. * @pdev: drm dev pointer
  903. * @pdev: pci dev pointer
  904. * @flags: driver flags
  905. *
  906. * Initializes the driver info and hw (all asics).
  907. * Returns 0 for success or an error on failure.
  908. * Called at driver startup.
  909. */
  910. int radeon_device_init(struct radeon_device *rdev,
  911. struct drm_device *ddev,
  912. struct pci_dev *pdev,
  913. uint32_t flags)
  914. {
  915. int r, i;
  916. int dma_bits;
  917. rdev->shutdown = false;
  918. rdev->dev = &pdev->dev;
  919. rdev->ddev = ddev;
  920. rdev->pdev = pdev;
  921. rdev->flags = flags;
  922. rdev->family = flags & RADEON_FAMILY_MASK;
  923. rdev->is_atom_bios = false;
  924. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  925. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  926. rdev->accel_working = false;
  927. /* set up ring ids */
  928. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  929. rdev->ring[i].idx = i;
  930. }
  931. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
  932. radeon_family_name[rdev->family], pdev->vendor, pdev->device,
  933. pdev->subsystem_vendor, pdev->subsystem_device);
  934. /* mutex initialization are all done here so we
  935. * can recall function without having locking issues */
  936. mutex_init(&rdev->ring_lock);
  937. mutex_init(&rdev->dc_hw_i2c_mutex);
  938. atomic_set(&rdev->ih.lock, 0);
  939. mutex_init(&rdev->gem.mutex);
  940. mutex_init(&rdev->pm.mutex);
  941. init_rwsem(&rdev->pm.mclk_lock);
  942. init_rwsem(&rdev->exclusive_lock);
  943. init_waitqueue_head(&rdev->irq.vblank_queue);
  944. init_waitqueue_head(&rdev->irq.idle_queue);
  945. r = radeon_gem_init(rdev);
  946. if (r)
  947. return r;
  948. /* initialize vm here */
  949. mutex_init(&rdev->vm_manager.lock);
  950. rdev->vm_manager.use_bitmap = 1;
  951. rdev->vm_manager.max_pfn = 1 << 20;
  952. INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
  953. /* Set asic functions */
  954. r = radeon_asic_init(rdev);
  955. if (r)
  956. return r;
  957. radeon_check_arguments(rdev);
  958. /* all of the newer IGP chips have an internal gart
  959. * However some rs4xx report as AGP, so remove that here.
  960. */
  961. if ((rdev->family >= CHIP_RS400) &&
  962. (rdev->flags & RADEON_IS_IGP)) {
  963. rdev->flags &= ~RADEON_IS_AGP;
  964. }
  965. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  966. radeon_agp_disable(rdev);
  967. }
  968. /* set DMA mask + need_dma32 flags.
  969. * PCIE - can handle 40-bits.
  970. * IGP - can handle 40-bits
  971. * AGP - generally dma32 is safest
  972. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  973. */
  974. rdev->need_dma32 = false;
  975. if (rdev->flags & RADEON_IS_AGP)
  976. rdev->need_dma32 = true;
  977. if ((rdev->flags & RADEON_IS_PCI) &&
  978. (rdev->family < CHIP_RS400))
  979. rdev->need_dma32 = true;
  980. dma_bits = rdev->need_dma32 ? 32 : 40;
  981. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  982. if (r) {
  983. rdev->need_dma32 = true;
  984. dma_bits = 32;
  985. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  986. }
  987. r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  988. if (r) {
  989. pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
  990. printk(KERN_WARNING "radeon: No coherent DMA available.\n");
  991. }
  992. /* Registers mapping */
  993. /* TODO: block userspace mapping of io register */
  994. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  995. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  996. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  997. if (rdev->rmmio == NULL) {
  998. return -ENOMEM;
  999. }
  1000. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  1001. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  1002. /* io port mapping */
  1003. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1004. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  1005. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  1006. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  1007. break;
  1008. }
  1009. }
  1010. if (rdev->rio_mem == NULL)
  1011. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1012. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  1013. /* this will fail for cards that aren't VGA class devices, just
  1014. * ignore it */
  1015. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  1016. vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops);
  1017. r = radeon_init(rdev);
  1018. if (r)
  1019. return r;
  1020. r = radeon_ib_ring_tests(rdev);
  1021. if (r)
  1022. DRM_ERROR("ib ring test failed (%d).\n", r);
  1023. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  1024. /* Acceleration not working on AGP card try again
  1025. * with fallback to PCI or PCIE GART
  1026. */
  1027. radeon_asic_reset(rdev);
  1028. radeon_fini(rdev);
  1029. radeon_agp_disable(rdev);
  1030. r = radeon_init(rdev);
  1031. if (r)
  1032. return r;
  1033. }
  1034. if ((radeon_testing & 1)) {
  1035. radeon_test_moves(rdev);
  1036. }
  1037. if ((radeon_testing & 2)) {
  1038. radeon_test_syncing(rdev);
  1039. }
  1040. if (radeon_benchmarking) {
  1041. radeon_benchmark(rdev, radeon_benchmarking);
  1042. }
  1043. return 0;
  1044. }
  1045. static void radeon_debugfs_remove_files(struct radeon_device *rdev);
  1046. /**
  1047. * radeon_device_fini - tear down the driver
  1048. *
  1049. * @rdev: radeon_device pointer
  1050. *
  1051. * Tear down the driver info (all asics).
  1052. * Called at driver shutdown.
  1053. */
  1054. void radeon_device_fini(struct radeon_device *rdev)
  1055. {
  1056. DRM_INFO("radeon: finishing device.\n");
  1057. rdev->shutdown = true;
  1058. /* evict vram memory */
  1059. radeon_bo_evict_vram(rdev);
  1060. radeon_fini(rdev);
  1061. vga_switcheroo_unregister_client(rdev->pdev);
  1062. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  1063. if (rdev->rio_mem)
  1064. pci_iounmap(rdev->pdev, rdev->rio_mem);
  1065. rdev->rio_mem = NULL;
  1066. iounmap(rdev->rmmio);
  1067. rdev->rmmio = NULL;
  1068. radeon_debugfs_remove_files(rdev);
  1069. }
  1070. /*
  1071. * Suspend & resume.
  1072. */
  1073. /**
  1074. * radeon_suspend_kms - initiate device suspend
  1075. *
  1076. * @pdev: drm dev pointer
  1077. * @state: suspend state
  1078. *
  1079. * Puts the hw in the suspend state (all asics).
  1080. * Returns 0 for success or an error on failure.
  1081. * Called at driver suspend.
  1082. */
  1083. int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
  1084. {
  1085. struct radeon_device *rdev;
  1086. struct drm_crtc *crtc;
  1087. struct drm_connector *connector;
  1088. int i, r;
  1089. if (dev == NULL || dev->dev_private == NULL) {
  1090. return -ENODEV;
  1091. }
  1092. if (state.event == PM_EVENT_PRETHAW) {
  1093. return 0;
  1094. }
  1095. rdev = dev->dev_private;
  1096. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1097. return 0;
  1098. drm_kms_helper_poll_disable(dev);
  1099. /* turn off display hw */
  1100. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1101. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1102. }
  1103. /* unpin the front buffers */
  1104. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1105. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  1106. struct radeon_bo *robj;
  1107. if (rfb == NULL || rfb->obj == NULL) {
  1108. continue;
  1109. }
  1110. robj = gem_to_radeon_bo(rfb->obj);
  1111. /* don't unpin kernel fb objects */
  1112. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  1113. r = radeon_bo_reserve(robj, false);
  1114. if (r == 0) {
  1115. radeon_bo_unpin(robj);
  1116. radeon_bo_unreserve(robj);
  1117. }
  1118. }
  1119. }
  1120. /* evict vram memory */
  1121. radeon_bo_evict_vram(rdev);
  1122. mutex_lock(&rdev->ring_lock);
  1123. /* wait for gpu to finish processing current batch */
  1124. for (i = 0; i < RADEON_NUM_RINGS; i++)
  1125. radeon_fence_wait_empty_locked(rdev, i);
  1126. mutex_unlock(&rdev->ring_lock);
  1127. radeon_save_bios_scratch_regs(rdev);
  1128. radeon_pm_suspend(rdev);
  1129. radeon_suspend(rdev);
  1130. radeon_hpd_fini(rdev);
  1131. /* evict remaining vram memory */
  1132. radeon_bo_evict_vram(rdev);
  1133. radeon_agp_suspend(rdev);
  1134. pci_save_state(dev->pdev);
  1135. if (state.event == PM_EVENT_SUSPEND) {
  1136. /* Shut down the device */
  1137. pci_disable_device(dev->pdev);
  1138. pci_set_power_state(dev->pdev, PCI_D3hot);
  1139. }
  1140. console_lock();
  1141. radeon_fbdev_set_suspend(rdev, 1);
  1142. console_unlock();
  1143. return 0;
  1144. }
  1145. /**
  1146. * radeon_resume_kms - initiate device resume
  1147. *
  1148. * @pdev: drm dev pointer
  1149. *
  1150. * Bring the hw back to operating state (all asics).
  1151. * Returns 0 for success or an error on failure.
  1152. * Called at driver resume.
  1153. */
  1154. int radeon_resume_kms(struct drm_device *dev)
  1155. {
  1156. struct drm_connector *connector;
  1157. struct radeon_device *rdev = dev->dev_private;
  1158. int r;
  1159. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1160. return 0;
  1161. console_lock();
  1162. pci_set_power_state(dev->pdev, PCI_D0);
  1163. pci_restore_state(dev->pdev);
  1164. if (pci_enable_device(dev->pdev)) {
  1165. console_unlock();
  1166. return -1;
  1167. }
  1168. /* resume AGP if in use */
  1169. radeon_agp_resume(rdev);
  1170. radeon_resume(rdev);
  1171. r = radeon_ib_ring_tests(rdev);
  1172. if (r)
  1173. DRM_ERROR("ib ring test failed (%d).\n", r);
  1174. radeon_pm_resume(rdev);
  1175. radeon_restore_bios_scratch_regs(rdev);
  1176. radeon_fbdev_set_suspend(rdev, 0);
  1177. console_unlock();
  1178. /* init dig PHYs, disp eng pll */
  1179. if (rdev->is_atom_bios) {
  1180. radeon_atom_encoder_init(rdev);
  1181. radeon_atom_disp_eng_pll_init(rdev);
  1182. }
  1183. /* reset hpd state */
  1184. radeon_hpd_init(rdev);
  1185. /* blat the mode back in */
  1186. drm_helper_resume_force_mode(dev);
  1187. /* turn on display hw */
  1188. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1189. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1190. }
  1191. drm_kms_helper_poll_enable(dev);
  1192. return 0;
  1193. }
  1194. /**
  1195. * radeon_gpu_reset - reset the asic
  1196. *
  1197. * @rdev: radeon device pointer
  1198. *
  1199. * Attempt the reset the GPU if it has hung (all asics).
  1200. * Returns 0 for success or an error on failure.
  1201. */
  1202. int radeon_gpu_reset(struct radeon_device *rdev)
  1203. {
  1204. unsigned ring_sizes[RADEON_NUM_RINGS];
  1205. uint32_t *ring_data[RADEON_NUM_RINGS];
  1206. bool saved = false;
  1207. int i, r;
  1208. int resched;
  1209. down_write(&rdev->exclusive_lock);
  1210. radeon_save_bios_scratch_regs(rdev);
  1211. /* block TTM */
  1212. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  1213. radeon_suspend(rdev);
  1214. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1215. ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
  1216. &ring_data[i]);
  1217. if (ring_sizes[i]) {
  1218. saved = true;
  1219. dev_info(rdev->dev, "Saved %d dwords of commands "
  1220. "on ring %d.\n", ring_sizes[i], i);
  1221. }
  1222. }
  1223. retry:
  1224. r = radeon_asic_reset(rdev);
  1225. if (!r) {
  1226. dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
  1227. radeon_resume(rdev);
  1228. }
  1229. radeon_restore_bios_scratch_regs(rdev);
  1230. drm_helper_resume_force_mode(rdev->ddev);
  1231. if (!r) {
  1232. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1233. radeon_ring_restore(rdev, &rdev->ring[i],
  1234. ring_sizes[i], ring_data[i]);
  1235. }
  1236. r = radeon_ib_ring_tests(rdev);
  1237. if (r) {
  1238. dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
  1239. if (saved) {
  1240. radeon_suspend(rdev);
  1241. goto retry;
  1242. }
  1243. }
  1244. } else {
  1245. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1246. kfree(ring_data[i]);
  1247. }
  1248. }
  1249. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  1250. if (r) {
  1251. /* bad news, how to tell it to userspace ? */
  1252. dev_info(rdev->dev, "GPU reset failed\n");
  1253. }
  1254. up_write(&rdev->exclusive_lock);
  1255. return r;
  1256. }
  1257. /*
  1258. * Debugfs
  1259. */
  1260. int radeon_debugfs_add_files(struct radeon_device *rdev,
  1261. struct drm_info_list *files,
  1262. unsigned nfiles)
  1263. {
  1264. unsigned i;
  1265. for (i = 0; i < rdev->debugfs_count; i++) {
  1266. if (rdev->debugfs[i].files == files) {
  1267. /* Already registered */
  1268. return 0;
  1269. }
  1270. }
  1271. i = rdev->debugfs_count + 1;
  1272. if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
  1273. DRM_ERROR("Reached maximum number of debugfs components.\n");
  1274. DRM_ERROR("Report so we increase "
  1275. "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
  1276. return -EINVAL;
  1277. }
  1278. rdev->debugfs[rdev->debugfs_count].files = files;
  1279. rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
  1280. rdev->debugfs_count = i;
  1281. #if defined(CONFIG_DEBUG_FS)
  1282. drm_debugfs_create_files(files, nfiles,
  1283. rdev->ddev->control->debugfs_root,
  1284. rdev->ddev->control);
  1285. drm_debugfs_create_files(files, nfiles,
  1286. rdev->ddev->primary->debugfs_root,
  1287. rdev->ddev->primary);
  1288. #endif
  1289. return 0;
  1290. }
  1291. static void radeon_debugfs_remove_files(struct radeon_device *rdev)
  1292. {
  1293. #if defined(CONFIG_DEBUG_FS)
  1294. unsigned i;
  1295. for (i = 0; i < rdev->debugfs_count; i++) {
  1296. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1297. rdev->debugfs[i].num_files,
  1298. rdev->ddev->control);
  1299. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1300. rdev->debugfs[i].num_files,
  1301. rdev->ddev->primary);
  1302. }
  1303. #endif
  1304. }
  1305. #if defined(CONFIG_DEBUG_FS)
  1306. int radeon_debugfs_init(struct drm_minor *minor)
  1307. {
  1308. return 0;
  1309. }
  1310. void radeon_debugfs_cleanup(struct drm_minor *minor)
  1311. {
  1312. }
  1313. #endif