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