blob: 8eef9a891261cd64b0365e51a768429c88a13df8 [file] [log] [blame]
Liviu Dudauad49f862016-03-07 10:00:53 +00001/*
2 * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
3 * Author: Liviu Dudau <Liviu.Dudau@arm.com>
4 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * ARM Mali DP plane manipulation routines.
11 */
12
13#include <drm/drmP.h>
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_fb_cma_helper.h>
16#include <drm/drm_gem_cma_helper.h>
17#include <drm/drm_plane_helper.h>
18
19#include "malidp_hw.h"
20#include "malidp_drv.h"
21
22/* Layer specific register offsets */
23#define MALIDP_LAYER_FORMAT 0x000
24#define MALIDP_LAYER_CONTROL 0x004
25#define LAYER_ENABLE (1 << 0)
26#define LAYER_ROT_OFFSET 8
27#define LAYER_H_FLIP (1 << 10)
28#define LAYER_V_FLIP (1 << 11)
29#define LAYER_ROT_MASK (0xf << 8)
Brian Starkeyc57eb712016-10-11 15:26:08 +010030#define LAYER_COMP_MASK (0x3 << 12)
31#define LAYER_COMP_PIXEL (0x3 << 12)
32#define LAYER_COMP_PLANE (0x2 << 12)
33#define MALIDP_LAYER_COMPOSE 0x008
Liviu Dudauad49f862016-03-07 10:00:53 +000034#define MALIDP_LAYER_SIZE 0x00c
35#define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
36#define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
37#define MALIDP_LAYER_COMP_SIZE 0x010
38#define MALIDP_LAYER_OFFSET 0x014
39#define MALIDP_LAYER_STRIDE 0x018
40
Brian Starkeyc57eb712016-10-11 15:26:08 +010041/*
42 * This 4-entry look-up-table is used to determine the full 8-bit alpha value
43 * for formats with 1- or 2-bit alpha channels.
44 * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
45 * opacity for 2-bit formats.
46 */
47#define MALIDP_ALPHA_LUT 0xffaa5500
48
Liviu Dudauad49f862016-03-07 10:00:53 +000049static void malidp_de_plane_destroy(struct drm_plane *plane)
50{
51 struct malidp_plane *mp = to_malidp_plane(plane);
52
53 if (mp->base.fb)
54 drm_framebuffer_unreference(mp->base.fb);
55
56 drm_plane_helper_disable(plane);
57 drm_plane_cleanup(plane);
58 devm_kfree(plane->dev->dev, mp);
59}
60
61struct drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
62{
63 struct malidp_plane_state *state, *m_state;
64
65 if (!plane->state)
66 return NULL;
67
68 state = kmalloc(sizeof(*state), GFP_KERNEL);
69 if (state) {
70 m_state = to_malidp_plane_state(plane->state);
71 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
72 state->rotmem_size = m_state->rotmem_size;
Brian Starkey70c94a32016-10-11 15:26:09 +010073 state->format = m_state->format;
74 state->n_planes = m_state->n_planes;
Liviu Dudauad49f862016-03-07 10:00:53 +000075 }
76
77 return &state->base;
78}
79
80void malidp_destroy_plane_state(struct drm_plane *plane,
81 struct drm_plane_state *state)
82{
83 struct malidp_plane_state *m_state = to_malidp_plane_state(state);
84
85 __drm_atomic_helper_plane_destroy_state(state);
86 kfree(m_state);
87}
88
89static const struct drm_plane_funcs malidp_de_plane_funcs = {
90 .update_plane = drm_atomic_helper_update_plane,
91 .disable_plane = drm_atomic_helper_disable_plane,
Liviu Dudau2fe1f082016-10-24 18:35:09 +010092 .set_property = drm_atomic_helper_plane_set_property,
Liviu Dudauad49f862016-03-07 10:00:53 +000093 .destroy = malidp_de_plane_destroy,
94 .reset = drm_atomic_helper_plane_reset,
95 .atomic_duplicate_state = malidp_duplicate_plane_state,
96 .atomic_destroy_state = malidp_destroy_plane_state,
97};
98
99static int malidp_de_plane_check(struct drm_plane *plane,
100 struct drm_plane_state *state)
101{
102 struct malidp_plane *mp = to_malidp_plane(plane);
103 struct malidp_plane_state *ms = to_malidp_plane_state(state);
Brian Starkeya46a0962016-10-11 15:26:05 +0100104 struct drm_framebuffer *fb;
Brian Starkey70c94a32016-10-11 15:26:09 +0100105 int i;
Liviu Dudauad49f862016-03-07 10:00:53 +0000106 u32 src_w, src_h;
107
108 if (!state->crtc || !state->fb)
109 return 0;
110
Brian Starkeya46a0962016-10-11 15:26:05 +0100111 fb = state->fb;
112
Brian Starkey70c94a32016-10-11 15:26:09 +0100113 ms->format = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id,
Brian Starkeya46a0962016-10-11 15:26:05 +0100114 fb->pixel_format);
Brian Starkey70c94a32016-10-11 15:26:09 +0100115 if (ms->format == MALIDP_INVALID_FORMAT_ID)
Liviu Dudauad49f862016-03-07 10:00:53 +0000116 return -EINVAL;
117
Brian Starkey70c94a32016-10-11 15:26:09 +0100118 ms->n_planes = drm_format_num_planes(fb->pixel_format);
119 for (i = 0; i < ms->n_planes; i++) {
Brian Starkeya46a0962016-10-11 15:26:05 +0100120 if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
121 DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
122 fb->pitches[i], i);
123 return -EINVAL;
124 }
125 }
126
Liviu Dudauad49f862016-03-07 10:00:53 +0000127 src_w = state->src_w >> 16;
128 src_h = state->src_h >> 16;
129
130 if ((state->crtc_w > mp->hwdev->max_line_size) ||
131 (state->crtc_h > mp->hwdev->max_line_size) ||
132 (state->crtc_w < mp->hwdev->min_line_size) ||
133 (state->crtc_h < mp->hwdev->min_line_size) ||
134 (state->crtc_w != src_w) || (state->crtc_h != src_h))
135 return -EINVAL;
136
137 /* packed RGB888 / BGR888 can't be rotated or flipped */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300138 if (state->rotation != DRM_ROTATE_0 &&
Liviu Dudauad49f862016-03-07 10:00:53 +0000139 (state->fb->pixel_format == DRM_FORMAT_RGB888 ||
140 state->fb->pixel_format == DRM_FORMAT_BGR888))
141 return -EINVAL;
142
143 ms->rotmem_size = 0;
144 if (state->rotation & MALIDP_ROTATED_MASK) {
145 int val;
146
147 val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
148 state->crtc_w,
149 state->fb->pixel_format);
150 if (val < 0)
151 return val;
152
153 ms->rotmem_size = val;
154 }
155
156 return 0;
157}
158
159static void malidp_de_plane_update(struct drm_plane *plane,
160 struct drm_plane_state *old_state)
161{
162 struct drm_gem_cma_object *obj;
163 struct malidp_plane *mp;
164 const struct malidp_hw_regmap *map;
Brian Starkey70c94a32016-10-11 15:26:09 +0100165 struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
Liviu Dudauad49f862016-03-07 10:00:53 +0000166 u16 ptr;
Brian Starkey70c94a32016-10-11 15:26:09 +0100167 u32 src_w, src_h, dest_w, dest_h, val;
168 int i;
Liviu Dudauad49f862016-03-07 10:00:53 +0000169
170 mp = to_malidp_plane(plane);
Liviu Dudauad49f862016-03-07 10:00:53 +0000171 map = &mp->hwdev->map;
Liviu Dudauad49f862016-03-07 10:00:53 +0000172
173 /* convert src values from Q16 fixed point to integer */
174 src_w = plane->state->src_w >> 16;
175 src_h = plane->state->src_h >> 16;
176 if (plane->state->rotation & MALIDP_ROTATED_MASK) {
177 dest_w = plane->state->crtc_h;
178 dest_h = plane->state->crtc_w;
179 } else {
180 dest_w = plane->state->crtc_w;
181 dest_h = plane->state->crtc_h;
182 }
183
Brian Starkey70c94a32016-10-11 15:26:09 +0100184 malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
Liviu Dudauad49f862016-03-07 10:00:53 +0000185
Brian Starkey70c94a32016-10-11 15:26:09 +0100186 for (i = 0; i < ms->n_planes; i++) {
Liviu Dudauad49f862016-03-07 10:00:53 +0000187 /* calculate the offset for the layer's plane registers */
188 ptr = mp->layer->ptr + (i << 4);
189
190 obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
191 malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
192 malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
193 malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i],
194 mp->layer->base + MALIDP_LAYER_STRIDE);
195 }
196
197 malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
198 mp->layer->base + MALIDP_LAYER_SIZE);
199
200 malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
201 mp->layer->base + MALIDP_LAYER_COMP_SIZE);
202
203 malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
204 LAYER_V_VAL(plane->state->crtc_y),
205 mp->layer->base + MALIDP_LAYER_OFFSET);
206
Brian Starkeyc57eb712016-10-11 15:26:08 +0100207 /* first clear the rotation bits */
208 val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
209 val &= ~LAYER_ROT_MASK;
Liviu Dudauad49f862016-03-07 10:00:53 +0000210
211 /* setup the rotation and axis flip bits */
212 if (plane->state->rotation & DRM_ROTATE_MASK)
213 val = ilog2(plane->state->rotation & DRM_ROTATE_MASK) << LAYER_ROT_OFFSET;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300214 if (plane->state->rotation & DRM_REFLECT_X)
Liviu Dudauad49f862016-03-07 10:00:53 +0000215 val |= LAYER_V_FLIP;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300216 if (plane->state->rotation & DRM_REFLECT_Y)
Liviu Dudauad49f862016-03-07 10:00:53 +0000217 val |= LAYER_H_FLIP;
218
Brian Starkeyc57eb712016-10-11 15:26:08 +0100219 /*
220 * always enable pixel alpha blending until we have a way to change
221 * blend modes
222 */
223 val &= ~LAYER_COMP_MASK;
224 val |= LAYER_COMP_PIXEL;
225
Liviu Dudauad49f862016-03-07 10:00:53 +0000226 /* set the 'enable layer' bit */
227 val |= LAYER_ENABLE;
228
Brian Starkeyc57eb712016-10-11 15:26:08 +0100229 malidp_hw_write(mp->hwdev, val,
230 mp->layer->base + MALIDP_LAYER_CONTROL);
Liviu Dudauad49f862016-03-07 10:00:53 +0000231}
232
233static void malidp_de_plane_disable(struct drm_plane *plane,
234 struct drm_plane_state *state)
235{
236 struct malidp_plane *mp = to_malidp_plane(plane);
237
238 malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
239 mp->layer->base + MALIDP_LAYER_CONTROL);
240}
241
242static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
243 .atomic_check = malidp_de_plane_check,
244 .atomic_update = malidp_de_plane_update,
245 .atomic_disable = malidp_de_plane_disable,
246};
247
248int malidp_de_planes_init(struct drm_device *drm)
249{
250 struct malidp_drm *malidp = drm->dev_private;
251 const struct malidp_hw_regmap *map = &malidp->dev->map;
252 struct malidp_plane *plane = NULL;
253 enum drm_plane_type plane_type;
254 unsigned long crtcs = 1 << drm->mode_config.num_crtc;
Brian Starkey15807782016-10-11 15:26:07 +0100255 unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 |
256 DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y;
Liviu Dudauad49f862016-03-07 10:00:53 +0000257 u32 *formats;
258 int ret, i, j, n;
259
260 formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL);
261 if (!formats) {
262 ret = -ENOMEM;
263 goto cleanup;
264 }
265
266 for (i = 0; i < map->n_layers; i++) {
267 u8 id = map->layers[i].id;
268
269 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
270 if (!plane) {
271 ret = -ENOMEM;
272 goto cleanup;
273 }
274
275 /* build the list of DRM supported formats based on the map */
276 for (n = 0, j = 0; j < map->n_input_formats; j++) {
277 if ((map->input_formats[j].layer & id) == id)
278 formats[n++] = map->input_formats[j].format;
279 }
280
281 plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
282 DRM_PLANE_TYPE_OVERLAY;
283 ret = drm_universal_plane_init(drm, &plane->base, crtcs,
284 &malidp_de_plane_funcs, formats,
285 n, plane_type, NULL);
286 if (ret < 0)
287 goto cleanup;
288
Liviu Dudauad49f862016-03-07 10:00:53 +0000289 drm_plane_helper_add(&plane->base,
290 &malidp_de_plane_helper_funcs);
291 plane->hwdev = malidp->dev;
292 plane->layer = &map->layers[i];
Brian Starkey15807782016-10-11 15:26:07 +0100293
294 /* Skip the features which the SMART layer doesn't have */
295 if (id == DE_SMART)
296 continue;
297
298 drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags);
Brian Starkeyc57eb712016-10-11 15:26:08 +0100299 malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
300 plane->layer->base + MALIDP_LAYER_COMPOSE);
Liviu Dudauad49f862016-03-07 10:00:53 +0000301 }
302
303 kfree(formats);
304
305 return 0;
306
307cleanup:
308 malidp_de_planes_destroy(drm);
309 kfree(formats);
310
311 return ret;
312}
313
314void malidp_de_planes_destroy(struct drm_device *drm)
315{
316 struct drm_plane *p, *pt;
317
318 list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
319 drm_plane_cleanup(p);
320 kfree(p);
321 }
322}