drm: rcar-du: Replace plane crtc and enabled fields by plane state
[deliverable/linux.git] / drivers / gpu / drm / rcar-du / rcar_du_plane.c
1 /*
2 * rcar_du_plane.c -- R-Car Display Unit Planes
3 *
4 * Copyright (C) 2013-2014 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
14 #include <drm/drmP.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
22 #include "rcar_du_drv.h"
23 #include "rcar_du_kms.h"
24 #include "rcar_du_plane.h"
25 #include "rcar_du_regs.h"
26
27 #define RCAR_DU_COLORKEY_NONE (0 << 24)
28 #define RCAR_DU_COLORKEY_SOURCE (1 << 24)
29 #define RCAR_DU_COLORKEY_MASK (1 << 24)
30
31 static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
32 {
33 return container_of(plane, struct rcar_du_plane, plane);
34 }
35
36 static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
37 unsigned int index, u32 reg)
38 {
39 return rcar_du_read(rgrp->dev,
40 rgrp->mmio_offset + index * PLANE_OFF + reg);
41 }
42
43 static void rcar_du_plane_write(struct rcar_du_group *rgrp,
44 unsigned int index, u32 reg, u32 data)
45 {
46 rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
47 data);
48 }
49
50 static int rcar_du_plane_reserve_check(struct rcar_du_plane *plane,
51 const struct rcar_du_format_info *format)
52 {
53 struct rcar_du_group *rgrp = plane->group;
54 unsigned int free;
55 unsigned int i;
56 int ret;
57
58 mutex_lock(&rgrp->planes.lock);
59
60 free = rgrp->planes.free;
61
62 if (plane->hwindex != -1) {
63 free |= 1 << plane->hwindex;
64 if (plane->format->planes == 2)
65 free |= 1 << ((plane->hwindex + 1) % 8);
66 }
67
68 for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
69 if (!(free & (1 << i)))
70 continue;
71
72 if (format->planes == 1 || free & (1 << ((i + 1) % 8)))
73 break;
74 }
75
76 ret = i == ARRAY_SIZE(rgrp->planes.planes) ? -EBUSY : 0;
77
78 mutex_unlock(&rgrp->planes.lock);
79 return ret;
80 }
81
82 static int rcar_du_plane_reserve(struct rcar_du_plane *plane,
83 const struct rcar_du_format_info *format)
84 {
85 struct rcar_du_group *rgrp = plane->group;
86 unsigned int i;
87 int ret = -EBUSY;
88
89 mutex_lock(&rgrp->planes.lock);
90
91 for (i = 0; i < RCAR_DU_NUM_HW_PLANES; ++i) {
92 if (!(rgrp->planes.free & (1 << i)))
93 continue;
94
95 if (format->planes == 1 ||
96 rgrp->planes.free & (1 << ((i + 1) % 8)))
97 break;
98 }
99
100 if (i == RCAR_DU_NUM_HW_PLANES)
101 goto done;
102
103 rgrp->planes.free &= ~(1 << i);
104 if (format->planes == 2)
105 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
106
107 plane->hwindex = i;
108
109 ret = 0;
110
111 done:
112 mutex_unlock(&rgrp->planes.lock);
113 return ret;
114 }
115
116 static void rcar_du_plane_release(struct rcar_du_plane *plane)
117 {
118 struct rcar_du_group *rgrp = plane->group;
119
120 if (plane->hwindex == -1)
121 return;
122
123 mutex_lock(&rgrp->planes.lock);
124 rgrp->planes.free |= 1 << plane->hwindex;
125 if (plane->format->planes == 2)
126 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
127 mutex_unlock(&rgrp->planes.lock);
128
129 plane->hwindex = -1;
130 }
131
132 static void rcar_du_plane_setup_fb(struct rcar_du_plane *plane)
133 {
134 struct drm_framebuffer *fb = plane->plane.state->fb;
135 struct rcar_du_group *rgrp = plane->group;
136 unsigned int src_x = plane->plane.state->src_x >> 16;
137 unsigned int src_y = plane->plane.state->src_y >> 16;
138 unsigned int index = plane->hwindex;
139 struct drm_gem_cma_object *gem;
140 bool interlaced;
141 u32 mwr;
142
143 interlaced = plane->plane.state->crtc->state->adjusted_mode.flags
144 & DRM_MODE_FLAG_INTERLACE;
145
146 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
147 * operation with 32bpp formats.
148 */
149 if (plane->format->planes == 2)
150 mwr = fb->pitches[0];
151 else
152 mwr = fb->pitches[0] * 8 / plane->format->bpp;
153
154 if (interlaced && plane->format->bpp == 32)
155 mwr *= 2;
156
157 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
158
159 /* The Y position is expressed in raster line units and must be doubled
160 * for 32bpp formats, according to the R8A7790 datasheet. No mention of
161 * doubling the Y position is found in the R8A7779 datasheet, but the
162 * rule seems to apply there as well.
163 *
164 * Despite not being documented, doubling seem not to be needed when
165 * operating in interlaced mode.
166 *
167 * Similarly, for the second plane, NV12 and NV21 formats seem to
168 * require a halved Y position value, in both progressive and interlaced
169 * modes.
170 */
171 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
172 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
173 (!interlaced && plane->format->bpp == 32 ? 2 : 1));
174
175 gem = drm_fb_cma_get_gem_obj(fb, 0);
176 rcar_du_plane_write(rgrp, index, PnDSA0R, gem->paddr + fb->offsets[0]);
177
178 if (plane->format->planes == 2) {
179 index = (index + 1) % 8;
180
181 rcar_du_plane_write(rgrp, index, PnMWR, fb->pitches[0]);
182
183 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
184 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
185 (plane->format->bpp == 16 ? 2 : 1) / 2);
186
187 gem = drm_fb_cma_get_gem_obj(fb, 1);
188 rcar_du_plane_write(rgrp, index, PnDSA0R,
189 gem->paddr + fb->offsets[1]);
190 }
191 }
192
193 static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
194 unsigned int index)
195 {
196 struct rcar_du_plane_state *state =
197 to_rcar_du_plane_state(plane->plane.state);
198 struct rcar_du_group *rgrp = plane->group;
199 u32 colorkey;
200 u32 pnmr;
201
202 /* The PnALPHAR register controls alpha-blending in 16bpp formats
203 * (ARGB1555 and XRGB1555).
204 *
205 * For ARGB, set the alpha value to 0, and enable alpha-blending when
206 * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
207 *
208 * For XRGB, set the alpha value to the plane-wide alpha value and
209 * enable alpha-blending regardless of the X bit value.
210 */
211 if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
212 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
213 else
214 rcar_du_plane_write(rgrp, index, PnALPHAR,
215 PnALPHAR_ABIT_X | state->alpha);
216
217 pnmr = PnMR_BM_MD | plane->format->pnmr;
218
219 /* Disable color keying when requested. YUV formats have the
220 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
221 * automatically.
222 */
223 if ((state->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
224 pnmr |= PnMR_SPIM_TP_OFF;
225
226 /* For packed YUV formats we need to select the U/V order. */
227 if (plane->format->fourcc == DRM_FORMAT_YUYV)
228 pnmr |= PnMR_YCDF_YUYV;
229
230 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
231
232 switch (plane->format->fourcc) {
233 case DRM_FORMAT_RGB565:
234 colorkey = ((state->colorkey & 0xf80000) >> 8)
235 | ((state->colorkey & 0x00fc00) >> 5)
236 | ((state->colorkey & 0x0000f8) >> 3);
237 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
238 break;
239
240 case DRM_FORMAT_ARGB1555:
241 case DRM_FORMAT_XRGB1555:
242 colorkey = ((state->colorkey & 0xf80000) >> 9)
243 | ((state->colorkey & 0x00f800) >> 6)
244 | ((state->colorkey & 0x0000f8) >> 3);
245 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
246 break;
247
248 case DRM_FORMAT_XRGB8888:
249 case DRM_FORMAT_ARGB8888:
250 rcar_du_plane_write(rgrp, index, PnTC3R,
251 PnTC3R_CODE | (state->colorkey & 0xffffff));
252 break;
253 }
254 }
255
256 static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
257 unsigned int index)
258 {
259 struct rcar_du_group *rgrp = plane->group;
260 u32 ddcr2 = PnDDCR2_CODE;
261 u32 ddcr4;
262
263 /* Data format
264 *
265 * The data format is selected by the DDDF field in PnMR and the EDF
266 * field in DDCR4.
267 */
268 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
269 ddcr4 &= ~PnDDCR4_EDF_MASK;
270 ddcr4 |= plane->format->edf | PnDDCR4_CODE;
271
272 rcar_du_plane_setup_mode(plane, index);
273
274 if (plane->format->planes == 2) {
275 if (plane->hwindex != index) {
276 if (plane->format->fourcc == DRM_FORMAT_NV12 ||
277 plane->format->fourcc == DRM_FORMAT_NV21)
278 ddcr2 |= PnDDCR2_Y420;
279
280 if (plane->format->fourcc == DRM_FORMAT_NV21)
281 ddcr2 |= PnDDCR2_NV21;
282
283 ddcr2 |= PnDDCR2_DIVU;
284 } else {
285 ddcr2 |= PnDDCR2_DIVY;
286 }
287 }
288
289 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
290 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
291
292 /* Destination position and size */
293 rcar_du_plane_write(rgrp, index, PnDSXR, plane->plane.state->crtc_w);
294 rcar_du_plane_write(rgrp, index, PnDSYR, plane->plane.state->crtc_h);
295 rcar_du_plane_write(rgrp, index, PnDPXR, plane->plane.state->crtc_x);
296 rcar_du_plane_write(rgrp, index, PnDPYR, plane->plane.state->crtc_y);
297
298 /* Wrap-around and blinking, disabled */
299 rcar_du_plane_write(rgrp, index, PnWASPR, 0);
300 rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
301 rcar_du_plane_write(rgrp, index, PnBTR, 0);
302 rcar_du_plane_write(rgrp, index, PnMLR, 0);
303 }
304
305 void rcar_du_plane_setup(struct rcar_du_plane *plane)
306 {
307 __rcar_du_plane_setup(plane, plane->hwindex);
308 if (plane->format->planes == 2)
309 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
310
311 rcar_du_plane_setup_fb(plane);
312 }
313
314 static int rcar_du_plane_atomic_check(struct drm_plane *plane,
315 struct drm_plane_state *state)
316 {
317 struct rcar_du_plane *rplane = to_rcar_plane(plane);
318 struct rcar_du_device *rcdu = rplane->group->dev;
319 const struct rcar_du_format_info *format;
320 unsigned int nplanes;
321 int ret;
322
323 if (!state->fb || !state->crtc)
324 return 0;
325
326 if (state->src_w >> 16 != state->crtc_w ||
327 state->src_h >> 16 != state->crtc_h) {
328 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
329 return -EINVAL;
330 }
331
332 format = rcar_du_format_info(state->fb->pixel_format);
333 if (format == NULL) {
334 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
335 state->fb->pixel_format);
336 return -EINVAL;
337 }
338
339 nplanes = rplane->format ? rplane->format->planes : 0;
340
341 /* If the number of required planes has changed we will need to
342 * reallocate hardware planes. Ensure free planes are available.
343 */
344 if (format->planes != nplanes) {
345 ret = rcar_du_plane_reserve_check(rplane, format);
346 if (ret < 0) {
347 dev_dbg(rcdu->dev, "%s: no available hardware plane\n",
348 __func__);
349 return ret;
350 }
351 }
352
353 return 0;
354 }
355
356 static void rcar_du_plane_disable(struct rcar_du_plane *rplane)
357 {
358 if (!rplane->plane.state->crtc)
359 return;
360
361 rcar_du_plane_release(rplane);
362
363 rplane->format = NULL;
364 }
365
366 static void rcar_du_plane_atomic_update(struct drm_plane *plane,
367 struct drm_plane_state *old_state)
368 {
369 struct rcar_du_plane *rplane = to_rcar_plane(plane);
370 struct drm_plane_state *state = plane->state;
371 const struct rcar_du_format_info *format;
372 unsigned int nplanes;
373
374 if (!state->crtc) {
375 rcar_du_plane_disable(rplane);
376 return;
377 }
378
379 format = rcar_du_format_info(state->fb->pixel_format);
380 nplanes = rplane->format ? rplane->format->planes : 0;
381
382 /* Reallocate hardware planes if the number of required planes has
383 * changed.
384 */
385 if (format->planes != nplanes) {
386 rcar_du_plane_release(rplane);
387 rcar_du_plane_reserve(rplane, format);
388 }
389
390 rplane->format = format;
391
392 rcar_du_plane_setup(rplane);
393 }
394
395 static const struct drm_plane_helper_funcs rcar_du_plane_helper_funcs = {
396 .atomic_check = rcar_du_plane_atomic_check,
397 .atomic_update = rcar_du_plane_atomic_update,
398 };
399
400 static void rcar_du_plane_reset(struct drm_plane *plane)
401 {
402 struct rcar_du_plane_state *state;
403
404 if (plane->state && plane->state->fb)
405 drm_framebuffer_unreference(plane->state->fb);
406
407 kfree(plane->state);
408 plane->state = NULL;
409
410 state = kzalloc(sizeof(*state), GFP_KERNEL);
411 if (state == NULL)
412 return;
413
414 state->alpha = 255;
415 state->colorkey = RCAR_DU_COLORKEY_NONE;
416 state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
417
418 plane->state = &state->state;
419 plane->state->plane = plane;
420 }
421
422 static struct drm_plane_state *
423 rcar_du_plane_atomic_duplicate_state(struct drm_plane *plane)
424 {
425 struct rcar_du_plane_state *state;
426 struct rcar_du_plane_state *copy;
427
428 state = to_rcar_du_plane_state(plane->state);
429 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
430 if (copy == NULL)
431 return NULL;
432
433 if (copy->state.fb)
434 drm_framebuffer_reference(copy->state.fb);
435
436 return &copy->state;
437 }
438
439 static void rcar_du_plane_atomic_destroy_state(struct drm_plane *plane,
440 struct drm_plane_state *state)
441 {
442 kfree(to_rcar_du_plane_state(state));
443 }
444
445 static int rcar_du_plane_atomic_set_property(struct drm_plane *plane,
446 struct drm_plane_state *state,
447 struct drm_property *property,
448 uint64_t val)
449 {
450 struct rcar_du_plane_state *rstate = to_rcar_du_plane_state(state);
451 struct rcar_du_plane *rplane = to_rcar_plane(plane);
452 struct rcar_du_group *rgrp = rplane->group;
453
454 if (property == rgrp->planes.alpha)
455 rstate->alpha = val;
456 else if (property == rgrp->planes.colorkey)
457 rstate->colorkey = val;
458 else if (property == rgrp->planes.zpos)
459 rstate->zpos = val;
460 else
461 return -EINVAL;
462
463 return 0;
464 }
465
466 static int rcar_du_plane_atomic_get_property(struct drm_plane *plane,
467 const struct drm_plane_state *state, struct drm_property *property,
468 uint64_t *val)
469 {
470 const struct rcar_du_plane_state *rstate =
471 container_of(state, const struct rcar_du_plane_state, state);
472 struct rcar_du_plane *rplane = to_rcar_plane(plane);
473 struct rcar_du_group *rgrp = rplane->group;
474
475 if (property == rgrp->planes.alpha)
476 *val = rstate->alpha;
477 else if (property == rgrp->planes.colorkey)
478 *val = rstate->colorkey;
479 else if (property == rgrp->planes.zpos)
480 *val = rstate->zpos;
481 else
482 return -EINVAL;
483
484 return 0;
485 }
486
487 static const struct drm_plane_funcs rcar_du_plane_funcs = {
488 .update_plane = drm_atomic_helper_update_plane,
489 .disable_plane = drm_atomic_helper_disable_plane,
490 .reset = rcar_du_plane_reset,
491 .set_property = drm_atomic_helper_plane_set_property,
492 .destroy = drm_plane_cleanup,
493 .atomic_duplicate_state = rcar_du_plane_atomic_duplicate_state,
494 .atomic_destroy_state = rcar_du_plane_atomic_destroy_state,
495 .atomic_set_property = rcar_du_plane_atomic_set_property,
496 .atomic_get_property = rcar_du_plane_atomic_get_property,
497 };
498
499 static const uint32_t formats[] = {
500 DRM_FORMAT_RGB565,
501 DRM_FORMAT_ARGB1555,
502 DRM_FORMAT_XRGB1555,
503 DRM_FORMAT_XRGB8888,
504 DRM_FORMAT_ARGB8888,
505 DRM_FORMAT_UYVY,
506 DRM_FORMAT_YUYV,
507 DRM_FORMAT_NV12,
508 DRM_FORMAT_NV21,
509 DRM_FORMAT_NV16,
510 };
511
512 int rcar_du_planes_init(struct rcar_du_group *rgrp)
513 {
514 struct rcar_du_planes *planes = &rgrp->planes;
515 struct rcar_du_device *rcdu = rgrp->dev;
516 unsigned int num_planes;
517 unsigned int num_crtcs;
518 unsigned int crtcs;
519 unsigned int i;
520 int ret;
521
522 mutex_init(&planes->lock);
523 planes->free = 0xff;
524
525 planes->alpha =
526 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
527 if (planes->alpha == NULL)
528 return -ENOMEM;
529
530 /* The color key is expressed as an RGB888 triplet stored in a 32-bit
531 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
532 * or enable source color keying (1).
533 */
534 planes->colorkey =
535 drm_property_create_range(rcdu->ddev, 0, "colorkey",
536 0, 0x01ffffff);
537 if (planes->colorkey == NULL)
538 return -ENOMEM;
539
540 planes->zpos =
541 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
542 if (planes->zpos == NULL)
543 return -ENOMEM;
544
545 /* Create one primary plane per in this group CRTC and seven overlay
546 * planes.
547 */
548 num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
549 num_planes = num_crtcs + 7;
550
551 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
552
553 for (i = 0; i < num_planes; ++i) {
554 enum drm_plane_type type = i < num_crtcs
555 ? DRM_PLANE_TYPE_PRIMARY
556 : DRM_PLANE_TYPE_OVERLAY;
557 struct rcar_du_plane *plane = &planes->planes[i];
558
559 plane->group = rgrp;
560 plane->hwindex = -1;
561
562 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
563 &rcar_du_plane_funcs, formats,
564 ARRAY_SIZE(formats), type);
565 if (ret < 0)
566 return ret;
567
568 drm_plane_helper_add(&plane->plane,
569 &rcar_du_plane_helper_funcs);
570
571 if (type == DRM_PLANE_TYPE_PRIMARY)
572 continue;
573
574 drm_object_attach_property(&plane->plane.base,
575 planes->alpha, 255);
576 drm_object_attach_property(&plane->plane.base,
577 planes->colorkey,
578 RCAR_DU_COLORKEY_NONE);
579 drm_object_attach_property(&plane->plane.base,
580 planes->zpos, 1);
581 }
582
583 return 0;
584 }
This page took 0.047385 seconds and 5 git commands to generate.