rcar_du_plane.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * rcar_du_plane.c -- R-Car Display Unit Planes
  4. *
  5. * Copyright (C) 2013-2015 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <drm/drmP.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_crtc.h>
  13. #include <drm/drm_crtc_helper.h>
  14. #include <drm/drm_fb_cma_helper.h>
  15. #include <drm/drm_gem_cma_helper.h>
  16. #include <drm/drm_plane_helper.h>
  17. #include "rcar_du_drv.h"
  18. #include "rcar_du_group.h"
  19. #include "rcar_du_kms.h"
  20. #include "rcar_du_plane.h"
  21. #include "rcar_du_regs.h"
  22. /* -----------------------------------------------------------------------------
  23. * Atomic hardware plane allocator
  24. *
  25. * The hardware plane allocator is solely based on the atomic plane states
  26. * without keeping any external state to avoid races between .atomic_check()
  27. * and .atomic_commit().
  28. *
  29. * The core idea is to avoid using a free planes bitmask that would need to be
  30. * shared between check and commit handlers with a collective knowledge based on
  31. * the allocated hardware plane(s) for each KMS plane. The allocator then loops
  32. * over all plane states to compute the free planes bitmask, allocates hardware
  33. * planes based on that bitmask, and stores the result back in the plane states.
  34. *
  35. * For this to work we need to access the current state of planes not touched by
  36. * the atomic update. To ensure that it won't be modified, we need to lock all
  37. * planes using drm_atomic_get_plane_state(). This effectively serializes atomic
  38. * updates from .atomic_check() up to completion (when swapping the states if
  39. * the check step has succeeded) or rollback (when freeing the states if the
  40. * check step has failed).
  41. *
  42. * Allocation is performed in the .atomic_check() handler and applied
  43. * automatically when the core swaps the old and new states.
  44. */
  45. static bool rcar_du_plane_needs_realloc(
  46. const struct rcar_du_plane_state *old_state,
  47. const struct rcar_du_plane_state *new_state)
  48. {
  49. /*
  50. * Lowering the number of planes doesn't strictly require reallocation
  51. * as the extra hardware plane will be freed when committing, but doing
  52. * so could lead to more fragmentation.
  53. */
  54. if (!old_state->format ||
  55. old_state->format->planes != new_state->format->planes)
  56. return true;
  57. /* Reallocate hardware planes if the source has changed. */
  58. if (old_state->source != new_state->source)
  59. return true;
  60. return false;
  61. }
  62. static unsigned int rcar_du_plane_hwmask(struct rcar_du_plane_state *state)
  63. {
  64. unsigned int mask;
  65. if (state->hwindex == -1)
  66. return 0;
  67. mask = 1 << state->hwindex;
  68. if (state->format->planes == 2)
  69. mask |= 1 << ((state->hwindex + 1) % 8);
  70. return mask;
  71. }
  72. /*
  73. * The R8A7790 DU can source frames directly from the VSP1 devices VSPD0 and
  74. * VSPD1. VSPD0 feeds DU0/1 plane 0, and VSPD1 feeds either DU2 plane 0 or
  75. * DU0/1 plane 1.
  76. *
  77. * Allocate the correct fixed plane when sourcing frames from VSPD0 or VSPD1,
  78. * and allocate planes in reverse index order otherwise to ensure maximum
  79. * availability of planes 0 and 1.
  80. *
  81. * The caller is responsible for ensuring that the requested source is
  82. * compatible with the DU revision.
  83. */
  84. static int rcar_du_plane_hwalloc(struct rcar_du_plane *plane,
  85. struct rcar_du_plane_state *state,
  86. unsigned int free)
  87. {
  88. unsigned int num_planes = state->format->planes;
  89. int fixed = -1;
  90. int i;
  91. if (state->source == RCAR_DU_PLANE_VSPD0) {
  92. /* VSPD0 feeds plane 0 on DU0/1. */
  93. if (plane->group->index != 0)
  94. return -EINVAL;
  95. fixed = 0;
  96. } else if (state->source == RCAR_DU_PLANE_VSPD1) {
  97. /* VSPD1 feeds plane 1 on DU0/1 or plane 0 on DU2. */
  98. fixed = plane->group->index == 0 ? 1 : 0;
  99. }
  100. if (fixed >= 0)
  101. return free & (1 << fixed) ? fixed : -EBUSY;
  102. for (i = RCAR_DU_NUM_HW_PLANES - 1; i >= 0; --i) {
  103. if (!(free & (1 << i)))
  104. continue;
  105. if (num_planes == 1 || free & (1 << ((i + 1) % 8)))
  106. break;
  107. }
  108. return i < 0 ? -EBUSY : i;
  109. }
  110. int rcar_du_atomic_check_planes(struct drm_device *dev,
  111. struct drm_atomic_state *state)
  112. {
  113. struct rcar_du_device *rcdu = dev->dev_private;
  114. unsigned int group_freed_planes[RCAR_DU_MAX_GROUPS] = { 0, };
  115. unsigned int group_free_planes[RCAR_DU_MAX_GROUPS] = { 0, };
  116. bool needs_realloc = false;
  117. unsigned int groups = 0;
  118. unsigned int i;
  119. struct drm_plane *drm_plane;
  120. struct drm_plane_state *old_drm_plane_state;
  121. struct drm_plane_state *new_drm_plane_state;
  122. /* Check if hardware planes need to be reallocated. */
  123. for_each_oldnew_plane_in_state(state, drm_plane, old_drm_plane_state,
  124. new_drm_plane_state, i) {
  125. struct rcar_du_plane_state *old_plane_state;
  126. struct rcar_du_plane_state *new_plane_state;
  127. struct rcar_du_plane *plane;
  128. unsigned int index;
  129. plane = to_rcar_plane(drm_plane);
  130. old_plane_state = to_rcar_plane_state(old_drm_plane_state);
  131. new_plane_state = to_rcar_plane_state(new_drm_plane_state);
  132. dev_dbg(rcdu->dev, "%s: checking plane (%u,%tu)\n", __func__,
  133. plane->group->index, plane - plane->group->planes);
  134. /*
  135. * If the plane is being disabled we don't need to go through
  136. * the full reallocation procedure. Just mark the hardware
  137. * plane(s) as freed.
  138. */
  139. if (!new_plane_state->format) {
  140. dev_dbg(rcdu->dev, "%s: plane is being disabled\n",
  141. __func__);
  142. index = plane - plane->group->planes;
  143. group_freed_planes[plane->group->index] |= 1 << index;
  144. new_plane_state->hwindex = -1;
  145. continue;
  146. }
  147. /*
  148. * If the plane needs to be reallocated mark it as such, and
  149. * mark the hardware plane(s) as free.
  150. */
  151. if (rcar_du_plane_needs_realloc(old_plane_state, new_plane_state)) {
  152. dev_dbg(rcdu->dev, "%s: plane needs reallocation\n",
  153. __func__);
  154. groups |= 1 << plane->group->index;
  155. needs_realloc = true;
  156. index = plane - plane->group->planes;
  157. group_freed_planes[plane->group->index] |= 1 << index;
  158. new_plane_state->hwindex = -1;
  159. }
  160. }
  161. if (!needs_realloc)
  162. return 0;
  163. /*
  164. * Grab all plane states for the groups that need reallocation to ensure
  165. * locking and avoid racy updates. This serializes the update operation,
  166. * but there's not much we can do about it as that's the hardware
  167. * design.
  168. *
  169. * Compute the used planes mask for each group at the same time to avoid
  170. * looping over the planes separately later.
  171. */
  172. while (groups) {
  173. unsigned int index = ffs(groups) - 1;
  174. struct rcar_du_group *group = &rcdu->groups[index];
  175. unsigned int used_planes = 0;
  176. dev_dbg(rcdu->dev, "%s: finding free planes for group %u\n",
  177. __func__, index);
  178. for (i = 0; i < group->num_planes; ++i) {
  179. struct rcar_du_plane *plane = &group->planes[i];
  180. struct rcar_du_plane_state *new_plane_state;
  181. struct drm_plane_state *s;
  182. s = drm_atomic_get_plane_state(state, &plane->plane);
  183. if (IS_ERR(s))
  184. return PTR_ERR(s);
  185. /*
  186. * If the plane has been freed in the above loop its
  187. * hardware planes must not be added to the used planes
  188. * bitmask. However, the current state doesn't reflect
  189. * the free state yet, as we've modified the new state
  190. * above. Use the local freed planes list to check for
  191. * that condition instead.
  192. */
  193. if (group_freed_planes[index] & (1 << i)) {
  194. dev_dbg(rcdu->dev,
  195. "%s: plane (%u,%tu) has been freed, skipping\n",
  196. __func__, plane->group->index,
  197. plane - plane->group->planes);
  198. continue;
  199. }
  200. new_plane_state = to_rcar_plane_state(s);
  201. used_planes |= rcar_du_plane_hwmask(new_plane_state);
  202. dev_dbg(rcdu->dev,
  203. "%s: plane (%u,%tu) uses %u hwplanes (index %d)\n",
  204. __func__, plane->group->index,
  205. plane - plane->group->planes,
  206. new_plane_state->format ?
  207. new_plane_state->format->planes : 0,
  208. new_plane_state->hwindex);
  209. }
  210. group_free_planes[index] = 0xff & ~used_planes;
  211. groups &= ~(1 << index);
  212. dev_dbg(rcdu->dev, "%s: group %u free planes mask 0x%02x\n",
  213. __func__, index, group_free_planes[index]);
  214. }
  215. /* Reallocate hardware planes for each plane that needs it. */
  216. for_each_oldnew_plane_in_state(state, drm_plane, old_drm_plane_state,
  217. new_drm_plane_state, i) {
  218. struct rcar_du_plane_state *old_plane_state;
  219. struct rcar_du_plane_state *new_plane_state;
  220. struct rcar_du_plane *plane;
  221. unsigned int crtc_planes;
  222. unsigned int free;
  223. int idx;
  224. plane = to_rcar_plane(drm_plane);
  225. old_plane_state = to_rcar_plane_state(old_drm_plane_state);
  226. new_plane_state = to_rcar_plane_state(new_drm_plane_state);
  227. dev_dbg(rcdu->dev, "%s: allocating plane (%u,%tu)\n", __func__,
  228. plane->group->index, plane - plane->group->planes);
  229. /*
  230. * Skip planes that are being disabled or don't need to be
  231. * reallocated.
  232. */
  233. if (!new_plane_state->format ||
  234. !rcar_du_plane_needs_realloc(old_plane_state, new_plane_state))
  235. continue;
  236. /*
  237. * Try to allocate the plane from the free planes currently
  238. * associated with the target CRTC to avoid restarting the CRTC
  239. * group and thus minimize flicker. If it fails fall back to
  240. * allocating from all free planes.
  241. */
  242. crtc_planes = to_rcar_crtc(new_plane_state->state.crtc)->index % 2
  243. ? plane->group->dptsr_planes
  244. : ~plane->group->dptsr_planes;
  245. free = group_free_planes[plane->group->index];
  246. idx = rcar_du_plane_hwalloc(plane, new_plane_state,
  247. free & crtc_planes);
  248. if (idx < 0)
  249. idx = rcar_du_plane_hwalloc(plane, new_plane_state,
  250. free);
  251. if (idx < 0) {
  252. dev_dbg(rcdu->dev, "%s: no available hardware plane\n",
  253. __func__);
  254. return idx;
  255. }
  256. dev_dbg(rcdu->dev, "%s: allocated %u hwplanes (index %u)\n",
  257. __func__, new_plane_state->format->planes, idx);
  258. new_plane_state->hwindex = idx;
  259. group_free_planes[plane->group->index] &=
  260. ~rcar_du_plane_hwmask(new_plane_state);
  261. dev_dbg(rcdu->dev, "%s: group %u free planes mask 0x%02x\n",
  262. __func__, plane->group->index,
  263. group_free_planes[plane->group->index]);
  264. }
  265. return 0;
  266. }
  267. /* -----------------------------------------------------------------------------
  268. * Plane Setup
  269. */
  270. #define RCAR_DU_COLORKEY_NONE (0 << 24)
  271. #define RCAR_DU_COLORKEY_SOURCE (1 << 24)
  272. #define RCAR_DU_COLORKEY_MASK (1 << 24)
  273. static void rcar_du_plane_write(struct rcar_du_group *rgrp,
  274. unsigned int index, u32 reg, u32 data)
  275. {
  276. rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
  277. data);
  278. }
  279. static void rcar_du_plane_setup_scanout(struct rcar_du_group *rgrp,
  280. const struct rcar_du_plane_state *state)
  281. {
  282. unsigned int src_x = state->state.src.x1 >> 16;
  283. unsigned int src_y = state->state.src.y1 >> 16;
  284. unsigned int index = state->hwindex;
  285. unsigned int pitch;
  286. bool interlaced;
  287. u32 dma[2];
  288. interlaced = state->state.crtc->state->adjusted_mode.flags
  289. & DRM_MODE_FLAG_INTERLACE;
  290. if (state->source == RCAR_DU_PLANE_MEMORY) {
  291. struct drm_framebuffer *fb = state->state.fb;
  292. struct drm_gem_cma_object *gem;
  293. unsigned int i;
  294. if (state->format->planes == 2)
  295. pitch = fb->pitches[0];
  296. else
  297. pitch = fb->pitches[0] * 8 / state->format->bpp;
  298. for (i = 0; i < state->format->planes; ++i) {
  299. gem = drm_fb_cma_get_gem_obj(fb, i);
  300. dma[i] = gem->paddr + fb->offsets[i];
  301. }
  302. } else {
  303. pitch = drm_rect_width(&state->state.src) >> 16;
  304. dma[0] = 0;
  305. dma[1] = 0;
  306. }
  307. /*
  308. * Memory pitch (expressed in pixels). Must be doubled for interlaced
  309. * operation with 32bpp formats.
  310. */
  311. rcar_du_plane_write(rgrp, index, PnMWR,
  312. (interlaced && state->format->bpp == 32) ?
  313. pitch * 2 : pitch);
  314. /*
  315. * The Y position is expressed in raster line units and must be doubled
  316. * for 32bpp formats, according to the R8A7790 datasheet. No mention of
  317. * doubling the Y position is found in the R8A7779 datasheet, but the
  318. * rule seems to apply there as well.
  319. *
  320. * Despite not being documented, doubling seem not to be needed when
  321. * operating in interlaced mode.
  322. *
  323. * Similarly, for the second plane, NV12 and NV21 formats seem to
  324. * require a halved Y position value, in both progressive and interlaced
  325. * modes.
  326. */
  327. rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
  328. rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
  329. (!interlaced && state->format->bpp == 32 ? 2 : 1));
  330. rcar_du_plane_write(rgrp, index, PnDSA0R, dma[0]);
  331. if (state->format->planes == 2) {
  332. index = (index + 1) % 8;
  333. rcar_du_plane_write(rgrp, index, PnMWR, pitch);
  334. rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
  335. rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
  336. (state->format->bpp == 16 ? 2 : 1) / 2);
  337. rcar_du_plane_write(rgrp, index, PnDSA0R, dma[1]);
  338. }
  339. }
  340. static void rcar_du_plane_setup_mode(struct rcar_du_group *rgrp,
  341. unsigned int index,
  342. const struct rcar_du_plane_state *state)
  343. {
  344. u32 colorkey;
  345. u32 pnmr;
  346. /*
  347. * The PnALPHAR register controls alpha-blending in 16bpp formats
  348. * (ARGB1555 and XRGB1555).
  349. *
  350. * For ARGB, set the alpha value to 0, and enable alpha-blending when
  351. * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
  352. *
  353. * For XRGB, set the alpha value to the plane-wide alpha value and
  354. * enable alpha-blending regardless of the X bit value.
  355. */
  356. if (state->format->fourcc != DRM_FORMAT_XRGB1555)
  357. rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
  358. else
  359. rcar_du_plane_write(rgrp, index, PnALPHAR,
  360. PnALPHAR_ABIT_X | state->state.alpha >> 8);
  361. pnmr = PnMR_BM_MD | state->format->pnmr;
  362. /*
  363. * Disable color keying when requested. YUV formats have the
  364. * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
  365. * automatically.
  366. */
  367. if ((state->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
  368. pnmr |= PnMR_SPIM_TP_OFF;
  369. /* For packed YUV formats we need to select the U/V order. */
  370. if (state->format->fourcc == DRM_FORMAT_YUYV)
  371. pnmr |= PnMR_YCDF_YUYV;
  372. rcar_du_plane_write(rgrp, index, PnMR, pnmr);
  373. switch (state->format->fourcc) {
  374. case DRM_FORMAT_RGB565:
  375. colorkey = ((state->colorkey & 0xf80000) >> 8)
  376. | ((state->colorkey & 0x00fc00) >> 5)
  377. | ((state->colorkey & 0x0000f8) >> 3);
  378. rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
  379. break;
  380. case DRM_FORMAT_ARGB1555:
  381. case DRM_FORMAT_XRGB1555:
  382. colorkey = ((state->colorkey & 0xf80000) >> 9)
  383. | ((state->colorkey & 0x00f800) >> 6)
  384. | ((state->colorkey & 0x0000f8) >> 3);
  385. rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
  386. break;
  387. case DRM_FORMAT_XRGB8888:
  388. case DRM_FORMAT_ARGB8888:
  389. rcar_du_plane_write(rgrp, index, PnTC3R,
  390. PnTC3R_CODE | (state->colorkey & 0xffffff));
  391. break;
  392. }
  393. }
  394. static void rcar_du_plane_setup_format_gen2(struct rcar_du_group *rgrp,
  395. unsigned int index,
  396. const struct rcar_du_plane_state *state)
  397. {
  398. u32 ddcr2 = PnDDCR2_CODE;
  399. u32 ddcr4;
  400. /*
  401. * Data format
  402. *
  403. * The data format is selected by the DDDF field in PnMR and the EDF
  404. * field in DDCR4.
  405. */
  406. rcar_du_plane_setup_mode(rgrp, index, state);
  407. if (state->format->planes == 2) {
  408. if (state->hwindex != index) {
  409. if (state->format->fourcc == DRM_FORMAT_NV12 ||
  410. state->format->fourcc == DRM_FORMAT_NV21)
  411. ddcr2 |= PnDDCR2_Y420;
  412. if (state->format->fourcc == DRM_FORMAT_NV21)
  413. ddcr2 |= PnDDCR2_NV21;
  414. ddcr2 |= PnDDCR2_DIVU;
  415. } else {
  416. ddcr2 |= PnDDCR2_DIVY;
  417. }
  418. }
  419. rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
  420. ddcr4 = state->format->edf | PnDDCR4_CODE;
  421. if (state->source != RCAR_DU_PLANE_MEMORY)
  422. ddcr4 |= PnDDCR4_VSPS;
  423. rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
  424. }
  425. static void rcar_du_plane_setup_format_gen3(struct rcar_du_group *rgrp,
  426. unsigned int index,
  427. const struct rcar_du_plane_state *state)
  428. {
  429. rcar_du_plane_write(rgrp, index, PnMR,
  430. PnMR_SPIM_TP_OFF | state->format->pnmr);
  431. rcar_du_plane_write(rgrp, index, PnDDCR4,
  432. state->format->edf | PnDDCR4_CODE);
  433. }
  434. static void rcar_du_plane_setup_format(struct rcar_du_group *rgrp,
  435. unsigned int index,
  436. const struct rcar_du_plane_state *state)
  437. {
  438. struct rcar_du_device *rcdu = rgrp->dev;
  439. const struct drm_rect *dst = &state->state.dst;
  440. if (rcdu->info->gen < 3)
  441. rcar_du_plane_setup_format_gen2(rgrp, index, state);
  442. else
  443. rcar_du_plane_setup_format_gen3(rgrp, index, state);
  444. /* Destination position and size */
  445. rcar_du_plane_write(rgrp, index, PnDSXR, drm_rect_width(dst));
  446. rcar_du_plane_write(rgrp, index, PnDSYR, drm_rect_height(dst));
  447. rcar_du_plane_write(rgrp, index, PnDPXR, dst->x1);
  448. rcar_du_plane_write(rgrp, index, PnDPYR, dst->y1);
  449. if (rcdu->info->gen < 3) {
  450. /* Wrap-around and blinking, disabled */
  451. rcar_du_plane_write(rgrp, index, PnWASPR, 0);
  452. rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
  453. rcar_du_plane_write(rgrp, index, PnBTR, 0);
  454. rcar_du_plane_write(rgrp, index, PnMLR, 0);
  455. }
  456. }
  457. void __rcar_du_plane_setup(struct rcar_du_group *rgrp,
  458. const struct rcar_du_plane_state *state)
  459. {
  460. struct rcar_du_device *rcdu = rgrp->dev;
  461. rcar_du_plane_setup_format(rgrp, state->hwindex, state);
  462. if (state->format->planes == 2)
  463. rcar_du_plane_setup_format(rgrp, (state->hwindex + 1) % 8,
  464. state);
  465. if (rcdu->info->gen < 3)
  466. rcar_du_plane_setup_scanout(rgrp, state);
  467. if (state->source == RCAR_DU_PLANE_VSPD1) {
  468. unsigned int vspd1_sink = rgrp->index ? 2 : 0;
  469. if (rcdu->vspd1_sink != vspd1_sink) {
  470. rcdu->vspd1_sink = vspd1_sink;
  471. rcar_du_set_dpad0_vsp1_routing(rcdu);
  472. }
  473. }
  474. }
  475. int __rcar_du_plane_atomic_check(struct drm_plane *plane,
  476. struct drm_plane_state *state,
  477. const struct rcar_du_format_info **format)
  478. {
  479. struct drm_device *dev = plane->dev;
  480. struct drm_crtc_state *crtc_state;
  481. int ret;
  482. if (!state->crtc) {
  483. /*
  484. * The visible field is not reset by the DRM core but only
  485. * updated by drm_plane_helper_check_state(), set it manually.
  486. */
  487. state->visible = false;
  488. *format = NULL;
  489. return 0;
  490. }
  491. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  492. if (IS_ERR(crtc_state))
  493. return PTR_ERR(crtc_state);
  494. ret = drm_atomic_helper_check_plane_state(state, crtc_state,
  495. DRM_PLANE_HELPER_NO_SCALING,
  496. DRM_PLANE_HELPER_NO_SCALING,
  497. true, true);
  498. if (ret < 0)
  499. return ret;
  500. if (!state->visible) {
  501. *format = NULL;
  502. return 0;
  503. }
  504. *format = rcar_du_format_info(state->fb->format->format);
  505. if (*format == NULL) {
  506. dev_dbg(dev->dev, "%s: unsupported format %08x\n", __func__,
  507. state->fb->format->format);
  508. return -EINVAL;
  509. }
  510. return 0;
  511. }
  512. static int rcar_du_plane_atomic_check(struct drm_plane *plane,
  513. struct drm_plane_state *state)
  514. {
  515. struct rcar_du_plane_state *rstate = to_rcar_plane_state(state);
  516. return __rcar_du_plane_atomic_check(plane, state, &rstate->format);
  517. }
  518. static void rcar_du_plane_atomic_update(struct drm_plane *plane,
  519. struct drm_plane_state *old_state)
  520. {
  521. struct rcar_du_plane *rplane = to_rcar_plane(plane);
  522. struct rcar_du_plane_state *old_rstate;
  523. struct rcar_du_plane_state *new_rstate;
  524. if (!plane->state->visible)
  525. return;
  526. rcar_du_plane_setup(rplane);
  527. /*
  528. * Check whether the source has changed from memory to live source or
  529. * from live source to memory. The source has been configured by the
  530. * VSPS bit in the PnDDCR4 register. Although the datasheet states that
  531. * the bit is updated during vertical blanking, it seems that updates
  532. * only occur when the DU group is held in reset through the DSYSR.DRES
  533. * bit. We thus need to restart the group if the source changes.
  534. */
  535. old_rstate = to_rcar_plane_state(old_state);
  536. new_rstate = to_rcar_plane_state(plane->state);
  537. if ((old_rstate->source == RCAR_DU_PLANE_MEMORY) !=
  538. (new_rstate->source == RCAR_DU_PLANE_MEMORY))
  539. rplane->group->need_restart = true;
  540. }
  541. static const struct drm_plane_helper_funcs rcar_du_plane_helper_funcs = {
  542. .atomic_check = rcar_du_plane_atomic_check,
  543. .atomic_update = rcar_du_plane_atomic_update,
  544. };
  545. static struct drm_plane_state *
  546. rcar_du_plane_atomic_duplicate_state(struct drm_plane *plane)
  547. {
  548. struct rcar_du_plane_state *state;
  549. struct rcar_du_plane_state *copy;
  550. if (WARN_ON(!plane->state))
  551. return NULL;
  552. state = to_rcar_plane_state(plane->state);
  553. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  554. if (copy == NULL)
  555. return NULL;
  556. __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
  557. return &copy->state;
  558. }
  559. static void rcar_du_plane_atomic_destroy_state(struct drm_plane *plane,
  560. struct drm_plane_state *state)
  561. {
  562. __drm_atomic_helper_plane_destroy_state(state);
  563. kfree(to_rcar_plane_state(state));
  564. }
  565. static void rcar_du_plane_reset(struct drm_plane *plane)
  566. {
  567. struct rcar_du_plane_state *state;
  568. if (plane->state) {
  569. rcar_du_plane_atomic_destroy_state(plane, plane->state);
  570. plane->state = NULL;
  571. }
  572. state = kzalloc(sizeof(*state), GFP_KERNEL);
  573. if (state == NULL)
  574. return;
  575. __drm_atomic_helper_plane_reset(plane, &state->state);
  576. state->hwindex = -1;
  577. state->source = RCAR_DU_PLANE_MEMORY;
  578. state->colorkey = RCAR_DU_COLORKEY_NONE;
  579. state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
  580. }
  581. static int rcar_du_plane_atomic_set_property(struct drm_plane *plane,
  582. struct drm_plane_state *state,
  583. struct drm_property *property,
  584. uint64_t val)
  585. {
  586. struct rcar_du_plane_state *rstate = to_rcar_plane_state(state);
  587. struct rcar_du_device *rcdu = to_rcar_plane(plane)->group->dev;
  588. if (property == rcdu->props.colorkey)
  589. rstate->colorkey = val;
  590. else
  591. return -EINVAL;
  592. return 0;
  593. }
  594. static int rcar_du_plane_atomic_get_property(struct drm_plane *plane,
  595. const struct drm_plane_state *state, struct drm_property *property,
  596. uint64_t *val)
  597. {
  598. const struct rcar_du_plane_state *rstate =
  599. container_of(state, const struct rcar_du_plane_state, state);
  600. struct rcar_du_device *rcdu = to_rcar_plane(plane)->group->dev;
  601. if (property == rcdu->props.colorkey)
  602. *val = rstate->colorkey;
  603. else
  604. return -EINVAL;
  605. return 0;
  606. }
  607. static const struct drm_plane_funcs rcar_du_plane_funcs = {
  608. .update_plane = drm_atomic_helper_update_plane,
  609. .disable_plane = drm_atomic_helper_disable_plane,
  610. .reset = rcar_du_plane_reset,
  611. .destroy = drm_plane_cleanup,
  612. .atomic_duplicate_state = rcar_du_plane_atomic_duplicate_state,
  613. .atomic_destroy_state = rcar_du_plane_atomic_destroy_state,
  614. .atomic_set_property = rcar_du_plane_atomic_set_property,
  615. .atomic_get_property = rcar_du_plane_atomic_get_property,
  616. };
  617. static const uint32_t formats[] = {
  618. DRM_FORMAT_RGB565,
  619. DRM_FORMAT_ARGB1555,
  620. DRM_FORMAT_XRGB1555,
  621. DRM_FORMAT_XRGB8888,
  622. DRM_FORMAT_ARGB8888,
  623. DRM_FORMAT_UYVY,
  624. DRM_FORMAT_YUYV,
  625. DRM_FORMAT_NV12,
  626. DRM_FORMAT_NV21,
  627. DRM_FORMAT_NV16,
  628. };
  629. int rcar_du_planes_init(struct rcar_du_group *rgrp)
  630. {
  631. struct rcar_du_device *rcdu = rgrp->dev;
  632. unsigned int crtcs;
  633. unsigned int i;
  634. int ret;
  635. /*
  636. * Create one primary plane per CRTC in this group and seven overlay
  637. * planes.
  638. */
  639. rgrp->num_planes = rgrp->num_crtcs + 7;
  640. crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
  641. for (i = 0; i < rgrp->num_planes; ++i) {
  642. enum drm_plane_type type = i < rgrp->num_crtcs
  643. ? DRM_PLANE_TYPE_PRIMARY
  644. : DRM_PLANE_TYPE_OVERLAY;
  645. struct rcar_du_plane *plane = &rgrp->planes[i];
  646. plane->group = rgrp;
  647. ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
  648. &rcar_du_plane_funcs, formats,
  649. ARRAY_SIZE(formats),
  650. NULL, type, NULL);
  651. if (ret < 0)
  652. return ret;
  653. drm_plane_helper_add(&plane->plane,
  654. &rcar_du_plane_helper_funcs);
  655. if (type == DRM_PLANE_TYPE_PRIMARY)
  656. continue;
  657. drm_object_attach_property(&plane->plane.base,
  658. rcdu->props.colorkey,
  659. RCAR_DU_COLORKEY_NONE);
  660. drm_plane_create_alpha_property(&plane->plane);
  661. drm_plane_create_zpos_property(&plane->plane, 1, 1, 7);
  662. }
  663. return 0;
  664. }