rcar_du_plane.c 23 KB

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