mgag200_mode.c 22 KB

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