mgag200_mode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2010 Matt Turner.
  4. * Copyright 2012 Red Hat
  5. *
  6. * Authors: Matthew Garrett
  7. * Matt Turner
  8. * Dave Airlie
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/iosys-map.h>
  12. #include <drm/drm_atomic.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_damage_helper.h>
  15. #include <drm/drm_edid.h>
  16. #include <drm/drm_format_helper.h>
  17. #include <drm/drm_fourcc.h>
  18. #include <drm/drm_framebuffer.h>
  19. #include <drm/drm_gem_atomic_helper.h>
  20. #include <drm/drm_gem_framebuffer_helper.h>
  21. #include <drm/drm_panic.h>
  22. #include <drm/drm_print.h>
  23. #include "mgag200_ddc.h"
  24. #include "mgag200_drv.h"
  25. /*
  26. * This file contains setup code for the CRTC.
  27. */
  28. void mgag200_crtc_set_gamma_linear(struct mga_device *mdev,
  29. const struct drm_format_info *format)
  30. {
  31. int i;
  32. WREG8(DAC_INDEX + MGA1064_INDEX, 0);
  33. switch (format->format) {
  34. case DRM_FORMAT_RGB565:
  35. /* Use better interpolation, to take 32 values from 0 to 255 */
  36. for (i = 0; i < MGAG200_LUT_SIZE / 8; i++) {
  37. WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 8 + i / 4);
  38. WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 4 + i / 16);
  39. WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 8 + i / 4);
  40. }
  41. /* Green has one more bit, so add padding with 0 for red and blue. */
  42. for (i = MGAG200_LUT_SIZE / 8; i < MGAG200_LUT_SIZE / 4; i++) {
  43. WREG8(DAC_INDEX + MGA1064_COL_PAL, 0);
  44. WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 4 + i / 16);
  45. WREG8(DAC_INDEX + MGA1064_COL_PAL, 0);
  46. }
  47. break;
  48. case DRM_FORMAT_RGB888:
  49. case DRM_FORMAT_XRGB8888:
  50. for (i = 0; i < MGAG200_LUT_SIZE; i++) {
  51. WREG8(DAC_INDEX + MGA1064_COL_PAL, i);
  52. WREG8(DAC_INDEX + MGA1064_COL_PAL, i);
  53. WREG8(DAC_INDEX + MGA1064_COL_PAL, i);
  54. }
  55. break;
  56. default:
  57. drm_warn_once(&mdev->base, "Unsupported format %p4cc for gamma correction\n",
  58. &format->format);
  59. break;
  60. }
  61. }
  62. void mgag200_crtc_set_gamma(struct mga_device *mdev,
  63. const struct drm_format_info *format,
  64. struct drm_color_lut *lut)
  65. {
  66. int i;
  67. WREG8(DAC_INDEX + MGA1064_INDEX, 0);
  68. switch (format->format) {
  69. case DRM_FORMAT_RGB565:
  70. /* Use better interpolation, to take 32 values from lut[0] to lut[255] */
  71. for (i = 0; i < MGAG200_LUT_SIZE / 8; i++) {
  72. WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 8 + i / 4].red >> 8);
  73. WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 4 + i / 16].green >> 8);
  74. WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 8 + i / 4].blue >> 8);
  75. }
  76. /* Green has one more bit, so add padding with 0 for red and blue. */
  77. for (i = MGAG200_LUT_SIZE / 8; i < MGAG200_LUT_SIZE / 4; i++) {
  78. WREG8(DAC_INDEX + MGA1064_COL_PAL, 0);
  79. WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 4 + i / 16].green >> 8);
  80. WREG8(DAC_INDEX + MGA1064_COL_PAL, 0);
  81. }
  82. break;
  83. case DRM_FORMAT_RGB888:
  84. case DRM_FORMAT_XRGB8888:
  85. for (i = 0; i < MGAG200_LUT_SIZE; i++) {
  86. WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i].red >> 8);
  87. WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i].green >> 8);
  88. WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i].blue >> 8);
  89. }
  90. break;
  91. default:
  92. drm_warn_once(&mdev->base, "Unsupported format %p4cc for gamma correction\n",
  93. &format->format);
  94. break;
  95. }
  96. }
  97. static inline void mga_wait_vsync(struct mga_device *mdev)
  98. {
  99. unsigned long timeout = jiffies + HZ/10;
  100. unsigned int status = 0;
  101. do {
  102. status = RREG32(MGAREG_STATUS);
  103. } while ((status & 0x08) && time_before(jiffies, timeout));
  104. timeout = jiffies + HZ/10;
  105. status = 0;
  106. do {
  107. status = RREG32(MGAREG_STATUS);
  108. } while (!(status & 0x08) && time_before(jiffies, timeout));
  109. }
  110. static inline void mga_wait_busy(struct mga_device *mdev)
  111. {
  112. unsigned long timeout = jiffies + HZ;
  113. unsigned int status = 0;
  114. do {
  115. status = RREG8(MGAREG_STATUS + 2);
  116. } while ((status & 0x01) && time_before(jiffies, timeout));
  117. }
  118. /*
  119. * This is how the framebuffer base address is stored in g200 cards:
  120. * * Assume @offset is the gpu_addr variable of the framebuffer object
  121. * * Then addr is the number of _pixels_ (not bytes) from the start of
  122. * VRAM to the first pixel we want to display. (divided by 2 for 32bit
  123. * framebuffers)
  124. * * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
  125. * addr<20> -> CRTCEXT0<6>
  126. * addr<19-16> -> CRTCEXT0<3-0>
  127. * addr<15-8> -> CRTCC<7-0>
  128. * addr<7-0> -> CRTCD<7-0>
  129. *
  130. * CRTCEXT0 has to be programmed last to trigger an update and make the
  131. * new addr variable take effect.
  132. */
  133. static void mgag200_set_startadd(struct mga_device *mdev,
  134. unsigned long offset)
  135. {
  136. struct drm_device *dev = &mdev->base;
  137. u32 startadd;
  138. u8 crtcc, crtcd, crtcext0;
  139. startadd = offset / 8;
  140. if (startadd > 0)
  141. drm_WARN_ON_ONCE(dev, mdev->info->bug_no_startadd);
  142. /*
  143. * Can't store addresses any higher than that, but we also
  144. * don't have more than 16 MiB of memory, so it should be fine.
  145. */
  146. drm_WARN_ON(dev, startadd > 0x1fffff);
  147. RREG_ECRT(0x00, crtcext0);
  148. crtcc = (startadd >> 8) & 0xff;
  149. crtcd = startadd & 0xff;
  150. crtcext0 &= 0xb0;
  151. crtcext0 |= ((startadd >> 14) & BIT(6)) |
  152. ((startadd >> 16) & 0x0f);
  153. WREG_CRT(0x0c, crtcc);
  154. WREG_CRT(0x0d, crtcd);
  155. WREG_ECRT(0x00, crtcext0);
  156. }
  157. void mgag200_init_registers(struct mga_device *mdev)
  158. {
  159. u8 crtc11, misc;
  160. WREG_SEQ(2, 0x0f);
  161. WREG_SEQ(3, 0x00);
  162. WREG_SEQ(4, 0x0e);
  163. WREG_CRT(10, 0);
  164. WREG_CRT(11, 0);
  165. WREG_CRT(12, 0);
  166. WREG_CRT(13, 0);
  167. WREG_CRT(14, 0);
  168. WREG_CRT(15, 0);
  169. RREG_CRT(0x11, crtc11);
  170. crtc11 &= ~(MGAREG_CRTC11_CRTCPROTECT |
  171. MGAREG_CRTC11_VINTEN |
  172. MGAREG_CRTC11_VINTCLR);
  173. WREG_CRT(0x11, crtc11);
  174. misc = RREG8(MGA_MISC_IN);
  175. misc |= MGAREG_MISC_IOADSEL;
  176. WREG8(MGA_MISC_OUT, misc);
  177. }
  178. void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode,
  179. bool set_vidrst)
  180. {
  181. unsigned int hdispend, hsyncstr, hsyncend, htotal, hblkstr, hblkend;
  182. unsigned int vdispend, vsyncstr, vsyncend, vtotal, vblkstr, vblkend;
  183. unsigned int linecomp;
  184. u8 misc, crtcext1, crtcext2, crtcext5;
  185. hdispend = mode->crtc_hdisplay / 8 - 1;
  186. hsyncstr = mode->crtc_hsync_start / 8 - 1;
  187. hsyncend = mode->crtc_hsync_end / 8 - 1;
  188. htotal = mode->crtc_htotal / 8 - 1;
  189. /* Work around hardware quirk */
  190. if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  191. htotal++;
  192. hblkstr = mode->crtc_hblank_start / 8 - 1;
  193. hblkend = htotal;
  194. vdispend = mode->crtc_vdisplay - 1;
  195. vsyncstr = mode->crtc_vsync_start - 1;
  196. vsyncend = mode->crtc_vsync_end - 1;
  197. vtotal = mode->crtc_vtotal - 2;
  198. vblkstr = mode->crtc_vblank_start - 1;
  199. vblkend = vtotal + 1;
  200. linecomp = vdispend;
  201. misc = RREG8(MGA_MISC_IN);
  202. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  203. misc |= MGAREG_MISC_HSYNCPOL;
  204. else
  205. misc &= ~MGAREG_MISC_HSYNCPOL;
  206. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  207. misc |= MGAREG_MISC_VSYNCPOL;
  208. else
  209. misc &= ~MGAREG_MISC_VSYNCPOL;
  210. crtcext1 = (((htotal - 4) & 0x100) >> 8) |
  211. ((hblkstr & 0x100) >> 7) |
  212. ((hsyncstr & 0x100) >> 6) |
  213. (hblkend & 0x40);
  214. if (set_vidrst)
  215. crtcext1 |= MGAREG_CRTCEXT1_VRSTEN |
  216. MGAREG_CRTCEXT1_HRSTEN;
  217. crtcext2 = ((vtotal & 0xc00) >> 10) |
  218. ((vdispend & 0x400) >> 8) |
  219. ((vblkstr & 0xc00) >> 7) |
  220. ((vsyncstr & 0xc00) >> 5) |
  221. ((linecomp & 0x400) >> 3);
  222. crtcext5 = 0x00;
  223. WREG_CRT(0x00, htotal - 4);
  224. WREG_CRT(0x01, hdispend);
  225. WREG_CRT(0x02, hblkstr);
  226. WREG_CRT(0x03, (hblkend & 0x1f) | 0x80);
  227. WREG_CRT(0x04, hsyncstr);
  228. WREG_CRT(0x05, ((hblkend & 0x20) << 2) | (hsyncend & 0x1f));
  229. WREG_CRT(0x06, vtotal & 0xff);
  230. WREG_CRT(0x07, ((vtotal & 0x100) >> 8) |
  231. ((vdispend & 0x100) >> 7) |
  232. ((vsyncstr & 0x100) >> 6) |
  233. ((vblkstr & 0x100) >> 5) |
  234. ((linecomp & 0x100) >> 4) |
  235. ((vtotal & 0x200) >> 4) |
  236. ((vdispend & 0x200) >> 3) |
  237. ((vsyncstr & 0x200) >> 2));
  238. WREG_CRT(0x09, ((vblkstr & 0x200) >> 4) |
  239. ((linecomp & 0x200) >> 3));
  240. WREG_CRT(0x10, vsyncstr & 0xff);
  241. WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
  242. WREG_CRT(0x12, vdispend & 0xff);
  243. WREG_CRT(0x14, 0);
  244. WREG_CRT(0x15, vblkstr & 0xff);
  245. WREG_CRT(0x16, vblkend & 0xff);
  246. WREG_CRT(0x17, 0xc3);
  247. WREG_CRT(0x18, linecomp & 0xff);
  248. WREG_ECRT(0x01, crtcext1);
  249. WREG_ECRT(0x02, crtcext2);
  250. WREG_ECRT(0x05, crtcext5);
  251. WREG8(MGA_MISC_OUT, misc);
  252. }
  253. static u8 mgag200_get_bpp_shift(const struct drm_format_info *format)
  254. {
  255. static const u8 bpp_shift[] = {0, 1, 0, 2};
  256. return bpp_shift[format->cpp[0] - 1];
  257. }
  258. /*
  259. * Calculates the HW offset value from the framebuffer's pitch. The
  260. * offset is a multiple of the pixel size and depends on the display
  261. * format.
  262. */
  263. static u32 mgag200_calculate_offset(struct mga_device *mdev,
  264. const struct drm_framebuffer *fb)
  265. {
  266. u32 offset = fb->pitches[0] / fb->format->cpp[0];
  267. u8 bppshift = mgag200_get_bpp_shift(fb->format);
  268. if (fb->format->cpp[0] * 8 == 24)
  269. offset = (offset * 3) >> (4 - bppshift);
  270. else
  271. offset = offset >> (4 - bppshift);
  272. return offset;
  273. }
  274. static void mgag200_set_offset(struct mga_device *mdev,
  275. const struct drm_framebuffer *fb)
  276. {
  277. u8 crtc13, crtcext0;
  278. u32 offset = mgag200_calculate_offset(mdev, fb);
  279. RREG_ECRT(0, crtcext0);
  280. crtc13 = offset & 0xff;
  281. crtcext0 &= ~MGAREG_CRTCEXT0_OFFSET_MASK;
  282. crtcext0 |= (offset >> 4) & MGAREG_CRTCEXT0_OFFSET_MASK;
  283. WREG_CRT(0x13, crtc13);
  284. WREG_ECRT(0x00, crtcext0);
  285. }
  286. void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format)
  287. {
  288. struct drm_device *dev = &mdev->base;
  289. unsigned int bpp, bppshift, scale;
  290. u8 crtcext3, xmulctrl;
  291. bpp = format->cpp[0] * 8;
  292. bppshift = mgag200_get_bpp_shift(format);
  293. switch (bpp) {
  294. case 24:
  295. scale = ((1 << bppshift) * 3) - 1;
  296. break;
  297. default:
  298. scale = (1 << bppshift) - 1;
  299. break;
  300. }
  301. RREG_ECRT(3, crtcext3);
  302. switch (bpp) {
  303. case 8:
  304. xmulctrl = MGA1064_MUL_CTL_8bits;
  305. break;
  306. case 16:
  307. if (format->depth == 15)
  308. xmulctrl = MGA1064_MUL_CTL_15bits;
  309. else
  310. xmulctrl = MGA1064_MUL_CTL_16bits;
  311. break;
  312. case 24:
  313. xmulctrl = MGA1064_MUL_CTL_24bits;
  314. break;
  315. case 32:
  316. xmulctrl = MGA1064_MUL_CTL_32_24bits;
  317. break;
  318. default:
  319. /* BUG: We should have caught this problem already. */
  320. drm_WARN_ON(dev, "invalid format depth\n");
  321. return;
  322. }
  323. crtcext3 &= ~GENMASK(2, 0);
  324. crtcext3 |= scale;
  325. WREG_DAC(MGA1064_MUL_CTL, xmulctrl);
  326. WREG_GFX(0, 0x00);
  327. WREG_GFX(1, 0x00);
  328. WREG_GFX(2, 0x00);
  329. WREG_GFX(3, 0x00);
  330. WREG_GFX(4, 0x00);
  331. WREG_GFX(5, 0x40);
  332. /* GCTL6 should be 0x05, but we configure memmapsl to 0xb8000 (text mode),
  333. * so that it doesn't hang when running kexec/kdump on G200_SE rev42.
  334. */
  335. WREG_GFX(6, 0x0d);
  336. WREG_GFX(7, 0x0f);
  337. WREG_GFX(8, 0x0f);
  338. WREG_ECRT(3, crtcext3);
  339. }
  340. void mgag200_enable_display(struct mga_device *mdev)
  341. {
  342. u8 seq0, crtcext1;
  343. RREG_SEQ(0x00, seq0);
  344. seq0 |= MGAREG_SEQ0_SYNCRST |
  345. MGAREG_SEQ0_ASYNCRST;
  346. WREG_SEQ(0x00, seq0);
  347. /*
  348. * TODO: replace busy waiting with vblank IRQ; put
  349. * msleep(50) before changing SCROFF
  350. */
  351. mga_wait_vsync(mdev);
  352. mga_wait_busy(mdev);
  353. RREG_ECRT(0x01, crtcext1);
  354. crtcext1 &= ~MGAREG_CRTCEXT1_VSYNCOFF;
  355. crtcext1 &= ~MGAREG_CRTCEXT1_HSYNCOFF;
  356. WREG_ECRT(0x01, crtcext1);
  357. }
  358. static void mgag200_disable_display(struct mga_device *mdev)
  359. {
  360. u8 seq0, crtcext1;
  361. RREG_SEQ(0x00, seq0);
  362. seq0 &= ~MGAREG_SEQ0_SYNCRST;
  363. WREG_SEQ(0x00, seq0);
  364. /*
  365. * TODO: replace busy waiting with vblank IRQ; put
  366. * msleep(50) before changing SCROFF
  367. */
  368. mga_wait_vsync(mdev);
  369. mga_wait_busy(mdev);
  370. RREG_ECRT(0x01, crtcext1);
  371. crtcext1 |= MGAREG_CRTCEXT1_VSYNCOFF |
  372. MGAREG_CRTCEXT1_HSYNCOFF;
  373. WREG_ECRT(0x01, crtcext1);
  374. }
  375. static void mgag200_handle_damage(struct mga_device *mdev, const struct iosys_map *vmap,
  376. struct drm_framebuffer *fb, struct drm_rect *clip)
  377. {
  378. struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(mdev->vram);
  379. iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip));
  380. drm_fb_memcpy(&dst, fb->pitches, vmap, fb, clip);
  381. }
  382. /*
  383. * Primary plane
  384. */
  385. const uint32_t mgag200_primary_plane_formats[] = {
  386. DRM_FORMAT_XRGB8888,
  387. DRM_FORMAT_RGB565,
  388. DRM_FORMAT_RGB888,
  389. };
  390. const size_t mgag200_primary_plane_formats_size = ARRAY_SIZE(mgag200_primary_plane_formats);
  391. const uint64_t mgag200_primary_plane_fmtmods[] = {
  392. DRM_FORMAT_MOD_LINEAR,
  393. DRM_FORMAT_MOD_INVALID
  394. };
  395. int mgag200_primary_plane_helper_atomic_check(struct drm_plane *plane,
  396. struct drm_atomic_state *new_state)
  397. {
  398. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(new_state, plane);
  399. struct drm_framebuffer *new_fb = new_plane_state->fb;
  400. struct drm_framebuffer *fb = NULL;
  401. struct drm_crtc *new_crtc = new_plane_state->crtc;
  402. struct drm_crtc_state *new_crtc_state = NULL;
  403. struct mgag200_crtc_state *new_mgag200_crtc_state;
  404. int ret;
  405. if (new_crtc)
  406. new_crtc_state = drm_atomic_get_new_crtc_state(new_state, new_crtc);
  407. ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
  408. DRM_PLANE_NO_SCALING,
  409. DRM_PLANE_NO_SCALING,
  410. false, true);
  411. if (ret)
  412. return ret;
  413. else if (!new_plane_state->visible)
  414. return 0;
  415. if (plane->state)
  416. fb = plane->state->fb;
  417. if (!fb || (fb->format != new_fb->format))
  418. new_crtc_state->mode_changed = true; /* update PLL settings */
  419. new_mgag200_crtc_state = to_mgag200_crtc_state(new_crtc_state);
  420. new_mgag200_crtc_state->format = new_fb->format;
  421. return 0;
  422. }
  423. void mgag200_primary_plane_helper_atomic_update(struct drm_plane *plane,
  424. struct drm_atomic_state *old_state)
  425. {
  426. struct drm_device *dev = plane->dev;
  427. struct mga_device *mdev = to_mga_device(dev);
  428. struct drm_plane_state *plane_state = plane->state;
  429. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(old_state, plane);
  430. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  431. struct drm_framebuffer *fb = plane_state->fb;
  432. struct drm_atomic_helper_damage_iter iter;
  433. struct drm_rect damage;
  434. drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
  435. drm_atomic_for_each_plane_damage(&iter, &damage) {
  436. mgag200_handle_damage(mdev, shadow_plane_state->data, fb, &damage);
  437. }
  438. /* Always scanout image at VRAM offset 0 */
  439. mgag200_set_startadd(mdev, (u32)0);
  440. mgag200_set_offset(mdev, fb);
  441. }
  442. void mgag200_primary_plane_helper_atomic_enable(struct drm_plane *plane,
  443. struct drm_atomic_state *state)
  444. {
  445. struct drm_device *dev = plane->dev;
  446. struct mga_device *mdev = to_mga_device(dev);
  447. u8 seq1;
  448. RREG_SEQ(0x01, seq1);
  449. seq1 &= ~MGAREG_SEQ1_SCROFF;
  450. WREG_SEQ(0x01, seq1);
  451. msleep(20);
  452. }
  453. void mgag200_primary_plane_helper_atomic_disable(struct drm_plane *plane,
  454. struct drm_atomic_state *old_state)
  455. {
  456. struct drm_device *dev = plane->dev;
  457. struct mga_device *mdev = to_mga_device(dev);
  458. u8 seq1;
  459. RREG_SEQ(0x01, seq1);
  460. seq1 |= MGAREG_SEQ1_SCROFF;
  461. WREG_SEQ(0x01, seq1);
  462. msleep(20);
  463. }
  464. int mgag200_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
  465. struct drm_scanout_buffer *sb)
  466. {
  467. struct mga_device *mdev = to_mga_device(plane->dev);
  468. struct iosys_map map = IOSYS_MAP_INIT_VADDR_IOMEM(mdev->vram);
  469. if (plane->state && plane->state->fb) {
  470. sb->format = plane->state->fb->format;
  471. sb->width = plane->state->fb->width;
  472. sb->height = plane->state->fb->height;
  473. sb->pitch[0] = plane->state->fb->pitches[0];
  474. sb->map[0] = map;
  475. return 0;
  476. }
  477. return -ENODEV;
  478. }
  479. /*
  480. * CRTC
  481. */
  482. enum drm_mode_status mgag200_crtc_helper_mode_valid(struct drm_crtc *crtc,
  483. const struct drm_display_mode *mode)
  484. {
  485. struct mga_device *mdev = to_mga_device(crtc->dev);
  486. const struct mgag200_device_info *info = mdev->info;
  487. /*
  488. * Some devices have additional limits on the size of the
  489. * display mode.
  490. */
  491. if (mode->hdisplay > info->max_hdisplay)
  492. return MODE_VIRTUAL_X;
  493. if (mode->vdisplay > info->max_vdisplay)
  494. return MODE_VIRTUAL_Y;
  495. if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 ||
  496. (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) {
  497. return MODE_H_ILLEGAL;
  498. }
  499. if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
  500. mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
  501. mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
  502. mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
  503. return MODE_BAD;
  504. }
  505. return MODE_OK;
  506. }
  507. int mgag200_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *new_state)
  508. {
  509. struct drm_device *dev = crtc->dev;
  510. struct mga_device *mdev = to_mga_device(dev);
  511. const struct mgag200_device_funcs *funcs = mdev->funcs;
  512. struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(new_state, crtc);
  513. struct drm_property_blob *new_gamma_lut = new_crtc_state->gamma_lut;
  514. int ret;
  515. if (!new_crtc_state->enable)
  516. return 0;
  517. ret = drm_atomic_helper_check_crtc_primary_plane(new_crtc_state);
  518. if (ret)
  519. return ret;
  520. if (new_crtc_state->mode_changed) {
  521. if (funcs->pixpllc_atomic_check) {
  522. ret = funcs->pixpllc_atomic_check(crtc, new_state);
  523. if (ret)
  524. return ret;
  525. }
  526. }
  527. if (new_crtc_state->color_mgmt_changed && new_gamma_lut) {
  528. if (new_gamma_lut->length != MGAG200_LUT_SIZE * sizeof(struct drm_color_lut)) {
  529. drm_dbg(dev, "Wrong size for gamma_lut %zu\n", new_gamma_lut->length);
  530. return -EINVAL;
  531. }
  532. }
  533. return 0;
  534. }
  535. void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *old_state)
  536. {
  537. struct drm_crtc_state *crtc_state = crtc->state;
  538. struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state);
  539. struct drm_device *dev = crtc->dev;
  540. struct mga_device *mdev = to_mga_device(dev);
  541. if (crtc_state->enable && crtc_state->color_mgmt_changed) {
  542. const struct drm_format_info *format = mgag200_crtc_state->format;
  543. if (crtc_state->gamma_lut)
  544. mgag200_crtc_set_gamma(mdev, format, crtc_state->gamma_lut->data);
  545. else
  546. mgag200_crtc_set_gamma_linear(mdev, format);
  547. }
  548. }
  549. void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)
  550. {
  551. struct drm_device *dev = crtc->dev;
  552. struct mga_device *mdev = to_mga_device(dev);
  553. const struct mgag200_device_funcs *funcs = mdev->funcs;
  554. struct drm_crtc_state *crtc_state = crtc->state;
  555. struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
  556. struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state);
  557. const struct drm_format_info *format = mgag200_crtc_state->format;
  558. mgag200_set_format_regs(mdev, format);
  559. mgag200_set_mode_regs(mdev, adjusted_mode, mgag200_crtc_state->set_vidrst);
  560. if (funcs->pixpllc_atomic_update)
  561. funcs->pixpllc_atomic_update(crtc, old_state);
  562. if (crtc_state->gamma_lut)
  563. mgag200_crtc_set_gamma(mdev, format, crtc_state->gamma_lut->data);
  564. else
  565. mgag200_crtc_set_gamma_linear(mdev, format);
  566. mgag200_enable_display(mdev);
  567. }
  568. void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)
  569. {
  570. struct mga_device *mdev = to_mga_device(crtc->dev);
  571. mgag200_disable_display(mdev);
  572. }
  573. void mgag200_crtc_reset(struct drm_crtc *crtc)
  574. {
  575. struct mgag200_crtc_state *mgag200_crtc_state;
  576. if (crtc->state)
  577. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  578. mgag200_crtc_state = kzalloc(sizeof(*mgag200_crtc_state), GFP_KERNEL);
  579. if (mgag200_crtc_state)
  580. __drm_atomic_helper_crtc_reset(crtc, &mgag200_crtc_state->base);
  581. else
  582. __drm_atomic_helper_crtc_reset(crtc, NULL);
  583. }
  584. struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  585. {
  586. struct drm_crtc_state *crtc_state = crtc->state;
  587. struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state);
  588. struct mgag200_crtc_state *new_mgag200_crtc_state;
  589. if (!crtc_state)
  590. return NULL;
  591. new_mgag200_crtc_state = kzalloc(sizeof(*new_mgag200_crtc_state), GFP_KERNEL);
  592. if (!new_mgag200_crtc_state)
  593. return NULL;
  594. __drm_atomic_helper_crtc_duplicate_state(crtc, &new_mgag200_crtc_state->base);
  595. new_mgag200_crtc_state->format = mgag200_crtc_state->format;
  596. memcpy(&new_mgag200_crtc_state->pixpllc, &mgag200_crtc_state->pixpllc,
  597. sizeof(new_mgag200_crtc_state->pixpllc));
  598. new_mgag200_crtc_state->set_vidrst = mgag200_crtc_state->set_vidrst;
  599. return &new_mgag200_crtc_state->base;
  600. }
  601. void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state)
  602. {
  603. struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state);
  604. __drm_atomic_helper_crtc_destroy_state(&mgag200_crtc_state->base);
  605. kfree(mgag200_crtc_state);
  606. }
  607. /*
  608. * Mode config
  609. */
  610. static void mgag200_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state)
  611. {
  612. struct mga_device *mdev = to_mga_device(state->dev);
  613. /*
  614. * Concurrent operations could possibly trigger a call to
  615. * drm_connector_helper_funcs.get_modes by trying to read the
  616. * display modes. Protect access to I/O registers by acquiring
  617. * the I/O-register lock.
  618. */
  619. mutex_lock(&mdev->rmmio_lock);
  620. drm_atomic_helper_commit_tail(state);
  621. mutex_unlock(&mdev->rmmio_lock);
  622. }
  623. static const struct drm_mode_config_helper_funcs mgag200_mode_config_helper_funcs = {
  624. .atomic_commit_tail = mgag200_mode_config_helper_atomic_commit_tail,
  625. };
  626. /* Calculates a mode's required memory bandwidth (in KiB/sec). */
  627. static uint32_t mgag200_calculate_mode_bandwidth(const struct drm_display_mode *mode,
  628. unsigned int bits_per_pixel)
  629. {
  630. uint32_t total_area, divisor;
  631. uint64_t active_area, pixels_per_second, bandwidth;
  632. uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
  633. divisor = 1024;
  634. if (!mode->htotal || !mode->vtotal || !mode->clock)
  635. return 0;
  636. active_area = mode->hdisplay * mode->vdisplay;
  637. total_area = mode->htotal * mode->vtotal;
  638. pixels_per_second = active_area * mode->clock * 1000;
  639. do_div(pixels_per_second, total_area);
  640. bandwidth = pixels_per_second * bytes_per_pixel * 100;
  641. do_div(bandwidth, divisor);
  642. return (uint32_t)bandwidth;
  643. }
  644. static enum drm_mode_status mgag200_mode_config_mode_valid(struct drm_device *dev,
  645. const struct drm_display_mode *mode)
  646. {
  647. static const unsigned int max_bpp = 4; // DRM_FORMAT_XRGB8888
  648. struct mga_device *mdev = to_mga_device(dev);
  649. unsigned long fbsize, fbpages, max_fbpages;
  650. const struct mgag200_device_info *info = mdev->info;
  651. max_fbpages = mdev->vram_available >> PAGE_SHIFT;
  652. fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
  653. fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
  654. if (fbpages > max_fbpages)
  655. return MODE_MEM;
  656. /*
  657. * Test the mode's required memory bandwidth if the device
  658. * specifies a maximum. Not all devices do though.
  659. */
  660. if (info->max_mem_bandwidth) {
  661. uint32_t mode_bandwidth = mgag200_calculate_mode_bandwidth(mode, max_bpp * 8);
  662. if (mode_bandwidth > (info->max_mem_bandwidth * 1024))
  663. return MODE_BAD;
  664. }
  665. return MODE_OK;
  666. }
  667. static const struct drm_mode_config_funcs mgag200_mode_config_funcs = {
  668. .fb_create = drm_gem_fb_create_with_dirty,
  669. .mode_valid = mgag200_mode_config_mode_valid,
  670. .atomic_check = drm_atomic_helper_check,
  671. .atomic_commit = drm_atomic_helper_commit,
  672. };
  673. int mgag200_mode_config_init(struct mga_device *mdev, resource_size_t vram_available)
  674. {
  675. struct drm_device *dev = &mdev->base;
  676. int ret;
  677. mdev->vram_available = vram_available;
  678. ret = drmm_mode_config_init(dev);
  679. if (ret) {
  680. drm_err(dev, "drmm_mode_config_init() failed: %d\n", ret);
  681. return ret;
  682. }
  683. dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
  684. dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
  685. dev->mode_config.preferred_depth = 24;
  686. dev->mode_config.funcs = &mgag200_mode_config_funcs;
  687. dev->mode_config.helper_private = &mgag200_mode_config_helper_funcs;
  688. return 0;
  689. }