radeon_legacy_crtc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include <drm/radeon_drm.h>
  29. #include <drm/drm_fixed.h>
  30. #include "radeon.h"
  31. #include "atom.h"
  32. static void radeon_overscan_setup(struct drm_crtc *crtc,
  33. struct drm_display_mode *mode)
  34. {
  35. struct drm_device *dev = crtc->dev;
  36. struct radeon_device *rdev = dev->dev_private;
  37. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  38. WREG32(RADEON_OVR_CLR + radeon_crtc->crtc_offset, 0);
  39. WREG32(RADEON_OVR_WID_LEFT_RIGHT + radeon_crtc->crtc_offset, 0);
  40. WREG32(RADEON_OVR_WID_TOP_BOTTOM + radeon_crtc->crtc_offset, 0);
  41. }
  42. static void radeon_legacy_rmx_mode_set(struct drm_crtc *crtc,
  43. struct drm_display_mode *mode)
  44. {
  45. struct drm_device *dev = crtc->dev;
  46. struct radeon_device *rdev = dev->dev_private;
  47. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  48. int xres = mode->hdisplay;
  49. int yres = mode->vdisplay;
  50. bool hscale = true, vscale = true;
  51. int hsync_wid;
  52. int vsync_wid;
  53. int hsync_start;
  54. int blank_width;
  55. u32 scale, inc, crtc_more_cntl;
  56. u32 fp_horz_stretch, fp_vert_stretch, fp_horz_vert_active;
  57. u32 fp_h_sync_strt_wid, fp_crtc_h_total_disp;
  58. u32 fp_v_sync_strt_wid, fp_crtc_v_total_disp;
  59. struct drm_display_mode *native_mode = &radeon_crtc->native_mode;
  60. fp_vert_stretch = RREG32(RADEON_FP_VERT_STRETCH) &
  61. (RADEON_VERT_STRETCH_RESERVED |
  62. RADEON_VERT_AUTO_RATIO_INC);
  63. fp_horz_stretch = RREG32(RADEON_FP_HORZ_STRETCH) &
  64. (RADEON_HORZ_FP_LOOP_STRETCH |
  65. RADEON_HORZ_AUTO_RATIO_INC);
  66. crtc_more_cntl = 0;
  67. if ((rdev->family == CHIP_RS100) ||
  68. (rdev->family == CHIP_RS200)) {
  69. /* This is to workaround the asic bug for RMX, some versions
  70. of BIOS dosen't have this register initialized correctly. */
  71. crtc_more_cntl |= RADEON_CRTC_H_CUTOFF_ACTIVE_EN;
  72. }
  73. fp_crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
  74. | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
  75. hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
  76. if (!hsync_wid)
  77. hsync_wid = 1;
  78. hsync_start = mode->crtc_hsync_start - 8;
  79. fp_h_sync_strt_wid = ((hsync_start & 0x1fff)
  80. | ((hsync_wid & 0x3f) << 16)
  81. | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
  82. ? RADEON_CRTC_H_SYNC_POL
  83. : 0));
  84. fp_crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
  85. | ((mode->crtc_vdisplay - 1) << 16));
  86. vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
  87. if (!vsync_wid)
  88. vsync_wid = 1;
  89. fp_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
  90. | ((vsync_wid & 0x1f) << 16)
  91. | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
  92. ? RADEON_CRTC_V_SYNC_POL
  93. : 0));
  94. fp_horz_vert_active = 0;
  95. if (native_mode->hdisplay == 0 ||
  96. native_mode->vdisplay == 0) {
  97. hscale = false;
  98. vscale = false;
  99. } else {
  100. if (xres > native_mode->hdisplay)
  101. xres = native_mode->hdisplay;
  102. if (yres > native_mode->vdisplay)
  103. yres = native_mode->vdisplay;
  104. if (xres == native_mode->hdisplay)
  105. hscale = false;
  106. if (yres == native_mode->vdisplay)
  107. vscale = false;
  108. }
  109. switch (radeon_crtc->rmx_type) {
  110. case RMX_FULL:
  111. case RMX_ASPECT:
  112. if (!hscale)
  113. fp_horz_stretch |= ((xres/8-1) << 16);
  114. else {
  115. inc = (fp_horz_stretch & RADEON_HORZ_AUTO_RATIO_INC) ? 1 : 0;
  116. scale = ((xres + inc) * RADEON_HORZ_STRETCH_RATIO_MAX)
  117. / native_mode->hdisplay + 1;
  118. fp_horz_stretch |= (((scale) & RADEON_HORZ_STRETCH_RATIO_MASK) |
  119. RADEON_HORZ_STRETCH_BLEND |
  120. RADEON_HORZ_STRETCH_ENABLE |
  121. ((native_mode->hdisplay/8-1) << 16));
  122. }
  123. if (!vscale)
  124. fp_vert_stretch |= ((yres-1) << 12);
  125. else {
  126. inc = (fp_vert_stretch & RADEON_VERT_AUTO_RATIO_INC) ? 1 : 0;
  127. scale = ((yres + inc) * RADEON_VERT_STRETCH_RATIO_MAX)
  128. / native_mode->vdisplay + 1;
  129. fp_vert_stretch |= (((scale) & RADEON_VERT_STRETCH_RATIO_MASK) |
  130. RADEON_VERT_STRETCH_ENABLE |
  131. RADEON_VERT_STRETCH_BLEND |
  132. ((native_mode->vdisplay-1) << 12));
  133. }
  134. break;
  135. case RMX_CENTER:
  136. fp_horz_stretch |= ((xres/8-1) << 16);
  137. fp_vert_stretch |= ((yres-1) << 12);
  138. crtc_more_cntl |= (RADEON_CRTC_AUTO_HORZ_CENTER_EN |
  139. RADEON_CRTC_AUTO_VERT_CENTER_EN);
  140. blank_width = (mode->crtc_hblank_end - mode->crtc_hblank_start) / 8;
  141. if (blank_width > 110)
  142. blank_width = 110;
  143. fp_crtc_h_total_disp = (((blank_width) & 0x3ff)
  144. | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
  145. hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
  146. if (!hsync_wid)
  147. hsync_wid = 1;
  148. fp_h_sync_strt_wid = ((((mode->crtc_hsync_start - mode->crtc_hblank_start) / 8) & 0x1fff)
  149. | ((hsync_wid & 0x3f) << 16)
  150. | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
  151. ? RADEON_CRTC_H_SYNC_POL
  152. : 0));
  153. fp_crtc_v_total_disp = (((mode->crtc_vblank_end - mode->crtc_vblank_start) & 0xffff)
  154. | ((mode->crtc_vdisplay - 1) << 16));
  155. vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
  156. if (!vsync_wid)
  157. vsync_wid = 1;
  158. fp_v_sync_strt_wid = ((((mode->crtc_vsync_start - mode->crtc_vblank_start) & 0xfff)
  159. | ((vsync_wid & 0x1f) << 16)
  160. | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
  161. ? RADEON_CRTC_V_SYNC_POL
  162. : 0)));
  163. fp_horz_vert_active = (((native_mode->vdisplay) & 0xfff) |
  164. (((native_mode->hdisplay / 8) & 0x1ff) << 16));
  165. break;
  166. case RMX_OFF:
  167. default:
  168. fp_horz_stretch |= ((xres/8-1) << 16);
  169. fp_vert_stretch |= ((yres-1) << 12);
  170. break;
  171. }
  172. WREG32(RADEON_FP_HORZ_STRETCH, fp_horz_stretch);
  173. WREG32(RADEON_FP_VERT_STRETCH, fp_vert_stretch);
  174. WREG32(RADEON_CRTC_MORE_CNTL, crtc_more_cntl);
  175. WREG32(RADEON_FP_HORZ_VERT_ACTIVE, fp_horz_vert_active);
  176. WREG32(RADEON_FP_H_SYNC_STRT_WID, fp_h_sync_strt_wid);
  177. WREG32(RADEON_FP_V_SYNC_STRT_WID, fp_v_sync_strt_wid);
  178. WREG32(RADEON_FP_CRTC_H_TOTAL_DISP, fp_crtc_h_total_disp);
  179. WREG32(RADEON_FP_CRTC_V_TOTAL_DISP, fp_crtc_v_total_disp);
  180. }
  181. static void radeon_pll_wait_for_read_update_complete(struct drm_device *dev)
  182. {
  183. struct radeon_device *rdev = dev->dev_private;
  184. int i = 0;
  185. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  186. the cause yet, but this workaround will mask the problem for now.
  187. Other chips usually will pass at the very first test, so the
  188. workaround shouldn't have any effect on them. */
  189. for (i = 0;
  190. (i < 10000 &&
  191. RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
  192. i++);
  193. }
  194. static void radeon_pll_write_update(struct drm_device *dev)
  195. {
  196. struct radeon_device *rdev = dev->dev_private;
  197. while (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
  198. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  199. RADEON_PPLL_ATOMIC_UPDATE_W,
  200. ~(RADEON_PPLL_ATOMIC_UPDATE_W));
  201. }
  202. static void radeon_pll2_wait_for_read_update_complete(struct drm_device *dev)
  203. {
  204. struct radeon_device *rdev = dev->dev_private;
  205. int i = 0;
  206. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  207. the cause yet, but this workaround will mask the problem for now.
  208. Other chips usually will pass at the very first test, so the
  209. workaround shouldn't have any effect on them. */
  210. for (i = 0;
  211. (i < 10000 &&
  212. RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
  213. i++);
  214. }
  215. static void radeon_pll2_write_update(struct drm_device *dev)
  216. {
  217. struct radeon_device *rdev = dev->dev_private;
  218. while (RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
  219. WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
  220. RADEON_P2PLL_ATOMIC_UPDATE_W,
  221. ~(RADEON_P2PLL_ATOMIC_UPDATE_W));
  222. }
  223. static uint8_t radeon_compute_pll_gain(uint16_t ref_freq, uint16_t ref_div,
  224. uint16_t fb_div)
  225. {
  226. unsigned int vcoFreq;
  227. if (!ref_div)
  228. return 1;
  229. vcoFreq = ((unsigned)ref_freq * fb_div) / ref_div;
  230. /*
  231. * This is horribly crude: the VCO frequency range is divided into
  232. * 3 parts, each part having a fixed PLL gain value.
  233. */
  234. if (vcoFreq >= 30000)
  235. /*
  236. * [300..max] MHz : 7
  237. */
  238. return 7;
  239. else if (vcoFreq >= 18000)
  240. /*
  241. * [180..300) MHz : 4
  242. */
  243. return 4;
  244. else
  245. /*
  246. * [0..180) MHz : 1
  247. */
  248. return 1;
  249. }
  250. static void radeon_crtc_dpms(struct drm_crtc *crtc, int mode)
  251. {
  252. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  253. struct drm_device *dev = crtc->dev;
  254. struct radeon_device *rdev = dev->dev_private;
  255. uint32_t crtc_ext_cntl = 0;
  256. uint32_t mask;
  257. if (radeon_crtc->crtc_id)
  258. mask = (RADEON_CRTC2_DISP_DIS |
  259. RADEON_CRTC2_VSYNC_DIS |
  260. RADEON_CRTC2_HSYNC_DIS |
  261. RADEON_CRTC2_DISP_REQ_EN_B);
  262. else
  263. mask = (RADEON_CRTC_DISPLAY_DIS |
  264. RADEON_CRTC_VSYNC_DIS |
  265. RADEON_CRTC_HSYNC_DIS);
  266. /*
  267. * On all dual CRTC GPUs this bit controls the CRTC of the primary DAC.
  268. * Therefore it is set in the DAC DMPS function.
  269. * This is different for GPU's with a single CRTC but a primary and a
  270. * TV DAC: here it controls the single CRTC no matter where it is
  271. * routed. Therefore we set it here.
  272. */
  273. if (rdev->flags & RADEON_SINGLE_CRTC)
  274. crtc_ext_cntl = RADEON_CRTC_CRT_ON;
  275. switch (mode) {
  276. case DRM_MODE_DPMS_ON:
  277. radeon_crtc->enabled = true;
  278. /* adjust pm to dpms changes BEFORE enabling crtcs */
  279. radeon_pm_compute_clocks(rdev);
  280. if (radeon_crtc->crtc_id)
  281. WREG32_P(RADEON_CRTC2_GEN_CNTL, RADEON_CRTC2_EN, ~(RADEON_CRTC2_EN | mask));
  282. else {
  283. WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_EN, ~(RADEON_CRTC_EN |
  284. RADEON_CRTC_DISP_REQ_EN_B));
  285. WREG32_P(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl, ~(mask | crtc_ext_cntl));
  286. }
  287. drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
  288. /* Make sure vblank interrupt is still enabled if needed */
  289. radeon_irq_set(rdev);
  290. radeon_crtc_load_lut(crtc);
  291. break;
  292. case DRM_MODE_DPMS_STANDBY:
  293. case DRM_MODE_DPMS_SUSPEND:
  294. case DRM_MODE_DPMS_OFF:
  295. drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
  296. if (radeon_crtc->crtc_id)
  297. WREG32_P(RADEON_CRTC2_GEN_CNTL, mask, ~(RADEON_CRTC2_EN | mask));
  298. else {
  299. WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B, ~(RADEON_CRTC_EN |
  300. RADEON_CRTC_DISP_REQ_EN_B));
  301. WREG32_P(RADEON_CRTC_EXT_CNTL, mask, ~(mask | crtc_ext_cntl));
  302. }
  303. radeon_crtc->enabled = false;
  304. /* adjust pm to dpms changes AFTER disabling crtcs */
  305. radeon_pm_compute_clocks(rdev);
  306. break;
  307. }
  308. }
  309. int radeon_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  310. struct drm_framebuffer *old_fb)
  311. {
  312. return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
  313. }
  314. int radeon_crtc_set_base_atomic(struct drm_crtc *crtc,
  315. struct drm_framebuffer *fb,
  316. int x, int y, enum mode_set_atomic state)
  317. {
  318. return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
  319. }
  320. int radeon_crtc_do_set_base(struct drm_crtc *crtc,
  321. struct drm_framebuffer *fb,
  322. int x, int y, int atomic)
  323. {
  324. struct drm_device *dev = crtc->dev;
  325. struct radeon_device *rdev = dev->dev_private;
  326. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  327. struct radeon_framebuffer *radeon_fb;
  328. struct drm_framebuffer *target_fb;
  329. struct drm_gem_object *obj;
  330. struct radeon_bo *rbo;
  331. uint64_t base;
  332. uint32_t crtc_offset, crtc_offset_cntl, crtc_tile_x0_y0 = 0;
  333. uint32_t crtc_pitch, pitch_pixels;
  334. uint32_t tiling_flags;
  335. int format;
  336. uint32_t gen_cntl_reg, gen_cntl_val;
  337. int r;
  338. DRM_DEBUG_KMS("\n");
  339. /* no fb bound */
  340. if (!atomic && !crtc->primary->fb) {
  341. DRM_DEBUG_KMS("No FB bound\n");
  342. return 0;
  343. }
  344. if (atomic) {
  345. radeon_fb = to_radeon_framebuffer(fb);
  346. target_fb = fb;
  347. }
  348. else {
  349. radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  350. target_fb = crtc->primary->fb;
  351. }
  352. switch (target_fb->bits_per_pixel) {
  353. case 8:
  354. format = 2;
  355. break;
  356. case 15: /* 555 */
  357. format = 3;
  358. break;
  359. case 16: /* 565 */
  360. format = 4;
  361. break;
  362. case 24: /* RGB */
  363. format = 5;
  364. break;
  365. case 32: /* xRGB */
  366. format = 6;
  367. break;
  368. default:
  369. return false;
  370. }
  371. /* Pin framebuffer & get tilling informations */
  372. obj = radeon_fb->obj;
  373. rbo = gem_to_radeon_bo(obj);
  374. retry:
  375. r = radeon_bo_reserve(rbo, false);
  376. if (unlikely(r != 0))
  377. return r;
  378. /* Only 27 bit offset for legacy CRTC */
  379. r = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM, 1 << 27,
  380. &base);
  381. if (unlikely(r != 0)) {
  382. radeon_bo_unreserve(rbo);
  383. /* On old GPU like RN50 with little vram pining can fails because
  384. * current fb is taking all space needed. So instead of unpining
  385. * the old buffer after pining the new one, first unpin old one
  386. * and then retry pining new one.
  387. *
  388. * As only master can set mode only master can pin and it is
  389. * unlikely the master client will race with itself especialy
  390. * on those old gpu with single crtc.
  391. *
  392. * We don't shutdown the display controller because new buffer
  393. * will end up in same spot.
  394. */
  395. if (!atomic && fb && fb != crtc->primary->fb) {
  396. struct radeon_bo *old_rbo;
  397. unsigned long nsize, osize;
  398. old_rbo = gem_to_radeon_bo(to_radeon_framebuffer(fb)->obj);
  399. osize = radeon_bo_size(old_rbo);
  400. nsize = radeon_bo_size(rbo);
  401. if (nsize <= osize && !radeon_bo_reserve(old_rbo, false)) {
  402. radeon_bo_unpin(old_rbo);
  403. radeon_bo_unreserve(old_rbo);
  404. fb = NULL;
  405. goto retry;
  406. }
  407. }
  408. return -EINVAL;
  409. }
  410. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  411. radeon_bo_unreserve(rbo);
  412. if (tiling_flags & RADEON_TILING_MICRO)
  413. DRM_ERROR("trying to scanout microtiled buffer\n");
  414. /* if scanout was in GTT this really wouldn't work */
  415. /* crtc offset is from display base addr not FB location */
  416. radeon_crtc->legacy_display_base_addr = rdev->mc.vram_start;
  417. base -= radeon_crtc->legacy_display_base_addr;
  418. crtc_offset_cntl = 0;
  419. pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  420. crtc_pitch = (((pitch_pixels * target_fb->bits_per_pixel) +
  421. ((target_fb->bits_per_pixel * 8) - 1)) /
  422. (target_fb->bits_per_pixel * 8));
  423. crtc_pitch |= crtc_pitch << 16;
  424. crtc_offset_cntl |= RADEON_CRTC_GUI_TRIG_OFFSET_LEFT_EN;
  425. if (tiling_flags & RADEON_TILING_MACRO) {
  426. if (ASIC_IS_R300(rdev))
  427. crtc_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
  428. R300_CRTC_MICRO_TILE_BUFFER_DIS |
  429. R300_CRTC_MACRO_TILE_EN);
  430. else
  431. crtc_offset_cntl |= RADEON_CRTC_TILE_EN;
  432. } else {
  433. if (ASIC_IS_R300(rdev))
  434. crtc_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
  435. R300_CRTC_MICRO_TILE_BUFFER_DIS |
  436. R300_CRTC_MACRO_TILE_EN);
  437. else
  438. crtc_offset_cntl &= ~RADEON_CRTC_TILE_EN;
  439. }
  440. if (tiling_flags & RADEON_TILING_MACRO) {
  441. if (ASIC_IS_R300(rdev)) {
  442. crtc_tile_x0_y0 = x | (y << 16);
  443. base &= ~0x7ff;
  444. } else {
  445. int byteshift = target_fb->bits_per_pixel >> 4;
  446. int tile_addr = (((y >> 3) * pitch_pixels + x) >> (8 - byteshift)) << 11;
  447. base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
  448. crtc_offset_cntl |= (y % 16);
  449. }
  450. } else {
  451. int offset = y * pitch_pixels + x;
  452. switch (target_fb->bits_per_pixel) {
  453. case 8:
  454. offset *= 1;
  455. break;
  456. case 15:
  457. case 16:
  458. offset *= 2;
  459. break;
  460. case 24:
  461. offset *= 3;
  462. break;
  463. case 32:
  464. offset *= 4;
  465. break;
  466. default:
  467. return false;
  468. }
  469. base += offset;
  470. }
  471. base &= ~7;
  472. if (radeon_crtc->crtc_id == 1)
  473. gen_cntl_reg = RADEON_CRTC2_GEN_CNTL;
  474. else
  475. gen_cntl_reg = RADEON_CRTC_GEN_CNTL;
  476. gen_cntl_val = RREG32(gen_cntl_reg);
  477. gen_cntl_val &= ~(0xf << 8);
  478. gen_cntl_val |= (format << 8);
  479. gen_cntl_val &= ~RADEON_CRTC_VSTAT_MODE_MASK;
  480. WREG32(gen_cntl_reg, gen_cntl_val);
  481. crtc_offset = (u32)base;
  482. WREG32(RADEON_DISPLAY_BASE_ADDR + radeon_crtc->crtc_offset, radeon_crtc->legacy_display_base_addr);
  483. if (ASIC_IS_R300(rdev)) {
  484. if (radeon_crtc->crtc_id)
  485. WREG32(R300_CRTC2_TILE_X0_Y0, crtc_tile_x0_y0);
  486. else
  487. WREG32(R300_CRTC_TILE_X0_Y0, crtc_tile_x0_y0);
  488. }
  489. WREG32(RADEON_CRTC_OFFSET_CNTL + radeon_crtc->crtc_offset, crtc_offset_cntl);
  490. WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, crtc_offset);
  491. WREG32(RADEON_CRTC_PITCH + radeon_crtc->crtc_offset, crtc_pitch);
  492. if (!atomic && fb && fb != crtc->primary->fb) {
  493. radeon_fb = to_radeon_framebuffer(fb);
  494. rbo = gem_to_radeon_bo(radeon_fb->obj);
  495. r = radeon_bo_reserve(rbo, false);
  496. if (unlikely(r != 0))
  497. return r;
  498. radeon_bo_unpin(rbo);
  499. radeon_bo_unreserve(rbo);
  500. }
  501. /* Bytes per pixel may have changed */
  502. radeon_bandwidth_update(rdev);
  503. return 0;
  504. }
  505. static bool radeon_set_crtc_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
  506. {
  507. struct drm_device *dev = crtc->dev;
  508. struct radeon_device *rdev = dev->dev_private;
  509. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  510. struct drm_encoder *encoder;
  511. int format;
  512. int hsync_start;
  513. int hsync_wid;
  514. int vsync_wid;
  515. uint32_t crtc_h_total_disp;
  516. uint32_t crtc_h_sync_strt_wid;
  517. uint32_t crtc_v_total_disp;
  518. uint32_t crtc_v_sync_strt_wid;
  519. bool is_tv = false;
  520. DRM_DEBUG_KMS("\n");
  521. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  522. if (encoder->crtc == crtc) {
  523. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  524. if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
  525. is_tv = true;
  526. DRM_INFO("crtc %d is connected to a TV\n", radeon_crtc->crtc_id);
  527. break;
  528. }
  529. }
  530. }
  531. switch (crtc->primary->fb->bits_per_pixel) {
  532. case 8:
  533. format = 2;
  534. break;
  535. case 15: /* 555 */
  536. format = 3;
  537. break;
  538. case 16: /* 565 */
  539. format = 4;
  540. break;
  541. case 24: /* RGB */
  542. format = 5;
  543. break;
  544. case 32: /* xRGB */
  545. format = 6;
  546. break;
  547. default:
  548. return false;
  549. }
  550. crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
  551. | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
  552. hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
  553. if (!hsync_wid)
  554. hsync_wid = 1;
  555. hsync_start = mode->crtc_hsync_start - 8;
  556. crtc_h_sync_strt_wid = ((hsync_start & 0x1fff)
  557. | ((hsync_wid & 0x3f) << 16)
  558. | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
  559. ? RADEON_CRTC_H_SYNC_POL
  560. : 0));
  561. /* This works for double scan mode. */
  562. crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
  563. | ((mode->crtc_vdisplay - 1) << 16));
  564. vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
  565. if (!vsync_wid)
  566. vsync_wid = 1;
  567. crtc_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
  568. | ((vsync_wid & 0x1f) << 16)
  569. | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
  570. ? RADEON_CRTC_V_SYNC_POL
  571. : 0));
  572. if (radeon_crtc->crtc_id) {
  573. uint32_t crtc2_gen_cntl;
  574. uint32_t disp2_merge_cntl;
  575. /* if TV DAC is enabled for another crtc and keep it enabled */
  576. crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL) & 0x00718080;
  577. crtc2_gen_cntl |= ((format << 8)
  578. | RADEON_CRTC2_VSYNC_DIS
  579. | RADEON_CRTC2_HSYNC_DIS
  580. | RADEON_CRTC2_DISP_DIS
  581. | RADEON_CRTC2_DISP_REQ_EN_B
  582. | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
  583. ? RADEON_CRTC2_DBL_SCAN_EN
  584. : 0)
  585. | ((mode->flags & DRM_MODE_FLAG_CSYNC)
  586. ? RADEON_CRTC2_CSYNC_EN
  587. : 0)
  588. | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
  589. ? RADEON_CRTC2_INTERLACE_EN
  590. : 0));
  591. /* rs4xx chips seem to like to have the crtc enabled when the timing is set */
  592. if ((rdev->family == CHIP_RS400) || (rdev->family == CHIP_RS480))
  593. crtc2_gen_cntl |= RADEON_CRTC2_EN;
  594. disp2_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
  595. disp2_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
  596. WREG32(RADEON_DISP2_MERGE_CNTL, disp2_merge_cntl);
  597. WREG32(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
  598. WREG32(RADEON_FP_H2_SYNC_STRT_WID, crtc_h_sync_strt_wid);
  599. WREG32(RADEON_FP_V2_SYNC_STRT_WID, crtc_v_sync_strt_wid);
  600. } else {
  601. uint32_t crtc_gen_cntl;
  602. uint32_t crtc_ext_cntl;
  603. uint32_t disp_merge_cntl;
  604. crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL) & 0x00718000;
  605. crtc_gen_cntl |= (RADEON_CRTC_EXT_DISP_EN
  606. | (format << 8)
  607. | RADEON_CRTC_DISP_REQ_EN_B
  608. | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
  609. ? RADEON_CRTC_DBL_SCAN_EN
  610. : 0)
  611. | ((mode->flags & DRM_MODE_FLAG_CSYNC)
  612. ? RADEON_CRTC_CSYNC_EN
  613. : 0)
  614. | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
  615. ? RADEON_CRTC_INTERLACE_EN
  616. : 0));
  617. /* rs4xx chips seem to like to have the crtc enabled when the timing is set */
  618. if ((rdev->family == CHIP_RS400) || (rdev->family == CHIP_RS480))
  619. crtc_gen_cntl |= RADEON_CRTC_EN;
  620. crtc_ext_cntl = RREG32(RADEON_CRTC_EXT_CNTL);
  621. crtc_ext_cntl |= (RADEON_XCRT_CNT_EN |
  622. RADEON_CRTC_VSYNC_DIS |
  623. RADEON_CRTC_HSYNC_DIS |
  624. RADEON_CRTC_DISPLAY_DIS);
  625. disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
  626. disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
  627. WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
  628. WREG32(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
  629. WREG32(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl);
  630. }
  631. if (is_tv)
  632. radeon_legacy_tv_adjust_crtc_reg(encoder, &crtc_h_total_disp,
  633. &crtc_h_sync_strt_wid, &crtc_v_total_disp,
  634. &crtc_v_sync_strt_wid);
  635. WREG32(RADEON_CRTC_H_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_h_total_disp);
  636. WREG32(RADEON_CRTC_H_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_h_sync_strt_wid);
  637. WREG32(RADEON_CRTC_V_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_v_total_disp);
  638. WREG32(RADEON_CRTC_V_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_v_sync_strt_wid);
  639. return true;
  640. }
  641. static void radeon_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
  642. {
  643. struct drm_device *dev = crtc->dev;
  644. struct radeon_device *rdev = dev->dev_private;
  645. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  646. struct drm_encoder *encoder;
  647. uint32_t feedback_div = 0;
  648. uint32_t frac_fb_div = 0;
  649. uint32_t reference_div = 0;
  650. uint32_t post_divider = 0;
  651. uint32_t freq = 0;
  652. uint8_t pll_gain;
  653. bool use_bios_divs = false;
  654. /* PLL registers */
  655. uint32_t pll_ref_div = 0;
  656. uint32_t pll_fb_post_div = 0;
  657. uint32_t htotal_cntl = 0;
  658. bool is_tv = false;
  659. struct radeon_pll *pll;
  660. struct {
  661. int divider;
  662. int bitvalue;
  663. } *post_div, post_divs[] = {
  664. /* From RAGE 128 VR/RAGE 128 GL Register
  665. * Reference Manual (Technical Reference
  666. * Manual P/N RRG-G04100-C Rev. 0.04), page
  667. * 3-17 (PLL_DIV_[3:0]).
  668. */
  669. { 1, 0 }, /* VCLK_SRC */
  670. { 2, 1 }, /* VCLK_SRC/2 */
  671. { 4, 2 }, /* VCLK_SRC/4 */
  672. { 8, 3 }, /* VCLK_SRC/8 */
  673. { 3, 4 }, /* VCLK_SRC/3 */
  674. { 16, 5 }, /* VCLK_SRC/16 */
  675. { 6, 6 }, /* VCLK_SRC/6 */
  676. { 12, 7 }, /* VCLK_SRC/12 */
  677. { 0, 0 }
  678. };
  679. if (radeon_crtc->crtc_id)
  680. pll = &rdev->clock.p2pll;
  681. else
  682. pll = &rdev->clock.p1pll;
  683. pll->flags = RADEON_PLL_LEGACY;
  684. if (mode->clock > 200000) /* range limits??? */
  685. pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
  686. else
  687. pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
  688. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  689. if (encoder->crtc == crtc) {
  690. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  691. if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
  692. is_tv = true;
  693. break;
  694. }
  695. if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
  696. pll->flags |= RADEON_PLL_NO_ODD_POST_DIV;
  697. if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS) {
  698. if (!rdev->is_atom_bios) {
  699. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  700. struct radeon_encoder_lvds *lvds = (struct radeon_encoder_lvds *)radeon_encoder->enc_priv;
  701. if (lvds) {
  702. if (lvds->use_bios_dividers) {
  703. pll_ref_div = lvds->panel_ref_divider;
  704. pll_fb_post_div = (lvds->panel_fb_divider |
  705. (lvds->panel_post_divider << 16));
  706. htotal_cntl = 0;
  707. use_bios_divs = true;
  708. }
  709. }
  710. }
  711. pll->flags |= RADEON_PLL_USE_REF_DIV;
  712. }
  713. }
  714. }
  715. DRM_DEBUG_KMS("\n");
  716. if (!use_bios_divs) {
  717. radeon_compute_pll_legacy(pll, mode->clock,
  718. &freq, &feedback_div, &frac_fb_div,
  719. &reference_div, &post_divider);
  720. for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
  721. if (post_div->divider == post_divider)
  722. break;
  723. }
  724. if (!post_div->divider)
  725. post_div = &post_divs[0];
  726. DRM_DEBUG_KMS("dc=%u, fd=%d, rd=%d, pd=%d\n",
  727. (unsigned)freq,
  728. feedback_div,
  729. reference_div,
  730. post_divider);
  731. pll_ref_div = reference_div;
  732. #if defined(__powerpc__) && (0) /* TODO */
  733. /* apparently programming this otherwise causes a hang??? */
  734. if (info->MacModel == RADEON_MAC_IBOOK)
  735. pll_fb_post_div = 0x000600ad;
  736. else
  737. #endif
  738. pll_fb_post_div = (feedback_div | (post_div->bitvalue << 16));
  739. htotal_cntl = mode->htotal & 0x7;
  740. }
  741. pll_gain = radeon_compute_pll_gain(pll->reference_freq,
  742. pll_ref_div & 0x3ff,
  743. pll_fb_post_div & 0x7ff);
  744. if (radeon_crtc->crtc_id) {
  745. uint32_t pixclks_cntl = ((RREG32_PLL(RADEON_PIXCLKS_CNTL) &
  746. ~(RADEON_PIX2CLK_SRC_SEL_MASK)) |
  747. RADEON_PIX2CLK_SRC_SEL_P2PLLCLK);
  748. if (is_tv) {
  749. radeon_legacy_tv_adjust_pll2(encoder, &htotal_cntl,
  750. &pll_ref_div, &pll_fb_post_div,
  751. &pixclks_cntl);
  752. }
  753. WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
  754. RADEON_PIX2CLK_SRC_SEL_CPUCLK,
  755. ~(RADEON_PIX2CLK_SRC_SEL_MASK));
  756. WREG32_PLL_P(RADEON_P2PLL_CNTL,
  757. RADEON_P2PLL_RESET
  758. | RADEON_P2PLL_ATOMIC_UPDATE_EN
  759. | ((uint32_t)pll_gain << RADEON_P2PLL_PVG_SHIFT),
  760. ~(RADEON_P2PLL_RESET
  761. | RADEON_P2PLL_ATOMIC_UPDATE_EN
  762. | RADEON_P2PLL_PVG_MASK));
  763. WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
  764. pll_ref_div,
  765. ~RADEON_P2PLL_REF_DIV_MASK);
  766. WREG32_PLL_P(RADEON_P2PLL_DIV_0,
  767. pll_fb_post_div,
  768. ~RADEON_P2PLL_FB0_DIV_MASK);
  769. WREG32_PLL_P(RADEON_P2PLL_DIV_0,
  770. pll_fb_post_div,
  771. ~RADEON_P2PLL_POST0_DIV_MASK);
  772. radeon_pll2_write_update(dev);
  773. radeon_pll2_wait_for_read_update_complete(dev);
  774. WREG32_PLL(RADEON_HTOTAL2_CNTL, htotal_cntl);
  775. WREG32_PLL_P(RADEON_P2PLL_CNTL,
  776. 0,
  777. ~(RADEON_P2PLL_RESET
  778. | RADEON_P2PLL_SLEEP
  779. | RADEON_P2PLL_ATOMIC_UPDATE_EN));
  780. DRM_DEBUG_KMS("Wrote2: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
  781. (unsigned)pll_ref_div,
  782. (unsigned)pll_fb_post_div,
  783. (unsigned)htotal_cntl,
  784. RREG32_PLL(RADEON_P2PLL_CNTL));
  785. DRM_DEBUG_KMS("Wrote2: rd=%u, fd=%u, pd=%u\n",
  786. (unsigned)pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
  787. (unsigned)pll_fb_post_div & RADEON_P2PLL_FB0_DIV_MASK,
  788. (unsigned)((pll_fb_post_div &
  789. RADEON_P2PLL_POST0_DIV_MASK) >> 16));
  790. mdelay(50); /* Let the clock to lock */
  791. WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
  792. RADEON_PIX2CLK_SRC_SEL_P2PLLCLK,
  793. ~(RADEON_PIX2CLK_SRC_SEL_MASK));
  794. WREG32_PLL(RADEON_PIXCLKS_CNTL, pixclks_cntl);
  795. } else {
  796. uint32_t pixclks_cntl;
  797. if (is_tv) {
  798. pixclks_cntl = RREG32_PLL(RADEON_PIXCLKS_CNTL);
  799. radeon_legacy_tv_adjust_pll1(encoder, &htotal_cntl, &pll_ref_div,
  800. &pll_fb_post_div, &pixclks_cntl);
  801. }
  802. if (rdev->flags & RADEON_IS_MOBILITY) {
  803. /* A temporal workaround for the occasional blanking on certain laptop panels.
  804. This appears to related to the PLL divider registers (fail to lock?).
  805. It occurs even when all dividers are the same with their old settings.
  806. In this case we really don't need to fiddle with PLL registers.
  807. By doing this we can avoid the blanking problem with some panels.
  808. */
  809. if ((pll_ref_div == (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_REF_DIV_MASK)) &&
  810. (pll_fb_post_div == (RREG32_PLL(RADEON_PPLL_DIV_3) &
  811. (RADEON_PPLL_POST3_DIV_MASK | RADEON_PPLL_FB3_DIV_MASK)))) {
  812. WREG32_P(RADEON_CLOCK_CNTL_INDEX,
  813. RADEON_PLL_DIV_SEL,
  814. ~(RADEON_PLL_DIV_SEL));
  815. r100_pll_errata_after_index(rdev);
  816. return;
  817. }
  818. }
  819. WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
  820. RADEON_VCLK_SRC_SEL_CPUCLK,
  821. ~(RADEON_VCLK_SRC_SEL_MASK));
  822. WREG32_PLL_P(RADEON_PPLL_CNTL,
  823. RADEON_PPLL_RESET
  824. | RADEON_PPLL_ATOMIC_UPDATE_EN
  825. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
  826. | ((uint32_t)pll_gain << RADEON_PPLL_PVG_SHIFT),
  827. ~(RADEON_PPLL_RESET
  828. | RADEON_PPLL_ATOMIC_UPDATE_EN
  829. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
  830. | RADEON_PPLL_PVG_MASK));
  831. WREG32_P(RADEON_CLOCK_CNTL_INDEX,
  832. RADEON_PLL_DIV_SEL,
  833. ~(RADEON_PLL_DIV_SEL));
  834. r100_pll_errata_after_index(rdev);
  835. if (ASIC_IS_R300(rdev) ||
  836. (rdev->family == CHIP_RS300) ||
  837. (rdev->family == CHIP_RS400) ||
  838. (rdev->family == CHIP_RS480)) {
  839. if (pll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
  840. /* When restoring console mode, use saved PPLL_REF_DIV
  841. * setting.
  842. */
  843. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  844. pll_ref_div,
  845. 0);
  846. } else {
  847. /* R300 uses ref_div_acc field as real ref divider */
  848. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  849. (pll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
  850. ~R300_PPLL_REF_DIV_ACC_MASK);
  851. }
  852. } else
  853. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  854. pll_ref_div,
  855. ~RADEON_PPLL_REF_DIV_MASK);
  856. WREG32_PLL_P(RADEON_PPLL_DIV_3,
  857. pll_fb_post_div,
  858. ~RADEON_PPLL_FB3_DIV_MASK);
  859. WREG32_PLL_P(RADEON_PPLL_DIV_3,
  860. pll_fb_post_div,
  861. ~RADEON_PPLL_POST3_DIV_MASK);
  862. radeon_pll_write_update(dev);
  863. radeon_pll_wait_for_read_update_complete(dev);
  864. WREG32_PLL(RADEON_HTOTAL_CNTL, htotal_cntl);
  865. WREG32_PLL_P(RADEON_PPLL_CNTL,
  866. 0,
  867. ~(RADEON_PPLL_RESET
  868. | RADEON_PPLL_SLEEP
  869. | RADEON_PPLL_ATOMIC_UPDATE_EN
  870. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN));
  871. DRM_DEBUG_KMS("Wrote: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
  872. pll_ref_div,
  873. pll_fb_post_div,
  874. (unsigned)htotal_cntl,
  875. RREG32_PLL(RADEON_PPLL_CNTL));
  876. DRM_DEBUG_KMS("Wrote: rd=%d, fd=%d, pd=%d\n",
  877. pll_ref_div & RADEON_PPLL_REF_DIV_MASK,
  878. pll_fb_post_div & RADEON_PPLL_FB3_DIV_MASK,
  879. (pll_fb_post_div & RADEON_PPLL_POST3_DIV_MASK) >> 16);
  880. mdelay(50); /* Let the clock to lock */
  881. WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
  882. RADEON_VCLK_SRC_SEL_PPLLCLK,
  883. ~(RADEON_VCLK_SRC_SEL_MASK));
  884. if (is_tv)
  885. WREG32_PLL(RADEON_PIXCLKS_CNTL, pixclks_cntl);
  886. }
  887. }
  888. static bool radeon_crtc_mode_fixup(struct drm_crtc *crtc,
  889. const struct drm_display_mode *mode,
  890. struct drm_display_mode *adjusted_mode)
  891. {
  892. if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  893. return false;
  894. return true;
  895. }
  896. static int radeon_crtc_mode_set(struct drm_crtc *crtc,
  897. struct drm_display_mode *mode,
  898. struct drm_display_mode *adjusted_mode,
  899. int x, int y, struct drm_framebuffer *old_fb)
  900. {
  901. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  902. /* TODO TV */
  903. radeon_crtc_set_base(crtc, x, y, old_fb);
  904. radeon_set_crtc_timing(crtc, adjusted_mode);
  905. radeon_set_pll(crtc, adjusted_mode);
  906. radeon_overscan_setup(crtc, adjusted_mode);
  907. if (radeon_crtc->crtc_id == 0) {
  908. radeon_legacy_rmx_mode_set(crtc, adjusted_mode);
  909. } else {
  910. if (radeon_crtc->rmx_type != RMX_OFF) {
  911. /* FIXME: only first crtc has rmx what should we
  912. * do ?
  913. */
  914. DRM_ERROR("Mode need scaling but only first crtc can do that.\n");
  915. }
  916. }
  917. radeon_cursor_reset(crtc);
  918. return 0;
  919. }
  920. static void radeon_crtc_prepare(struct drm_crtc *crtc)
  921. {
  922. struct drm_device *dev = crtc->dev;
  923. struct drm_crtc *crtci;
  924. /*
  925. * The hardware wedges sometimes if you reconfigure one CRTC
  926. * whilst another is running (see fdo bug #24611).
  927. */
  928. list_for_each_entry(crtci, &dev->mode_config.crtc_list, head)
  929. radeon_crtc_dpms(crtci, DRM_MODE_DPMS_OFF);
  930. }
  931. static void radeon_crtc_commit(struct drm_crtc *crtc)
  932. {
  933. struct drm_device *dev = crtc->dev;
  934. struct drm_crtc *crtci;
  935. /*
  936. * Reenable the CRTCs that should be running.
  937. */
  938. list_for_each_entry(crtci, &dev->mode_config.crtc_list, head) {
  939. if (crtci->enabled)
  940. radeon_crtc_dpms(crtci, DRM_MODE_DPMS_ON);
  941. }
  942. }
  943. static void radeon_crtc_disable(struct drm_crtc *crtc)
  944. {
  945. radeon_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  946. if (crtc->primary->fb) {
  947. int r;
  948. struct radeon_framebuffer *radeon_fb;
  949. struct radeon_bo *rbo;
  950. radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  951. rbo = gem_to_radeon_bo(radeon_fb->obj);
  952. r = radeon_bo_reserve(rbo, false);
  953. if (unlikely(r))
  954. DRM_ERROR("failed to reserve rbo before unpin\n");
  955. else {
  956. radeon_bo_unpin(rbo);
  957. radeon_bo_unreserve(rbo);
  958. }
  959. }
  960. }
  961. static const struct drm_crtc_helper_funcs legacy_helper_funcs = {
  962. .dpms = radeon_crtc_dpms,
  963. .mode_fixup = radeon_crtc_mode_fixup,
  964. .mode_set = radeon_crtc_mode_set,
  965. .mode_set_base = radeon_crtc_set_base,
  966. .mode_set_base_atomic = radeon_crtc_set_base_atomic,
  967. .prepare = radeon_crtc_prepare,
  968. .commit = radeon_crtc_commit,
  969. .load_lut = radeon_crtc_load_lut,
  970. .disable = radeon_crtc_disable
  971. };
  972. void radeon_legacy_init_crtc(struct drm_device *dev,
  973. struct radeon_crtc *radeon_crtc)
  974. {
  975. if (radeon_crtc->crtc_id == 1)
  976. radeon_crtc->crtc_offset = RADEON_CRTC2_H_TOTAL_DISP - RADEON_CRTC_H_TOTAL_DISP;
  977. drm_crtc_helper_add(&radeon_crtc->base, &legacy_helper_funcs);
  978. }