blob: f684fe4a80d2b419fef82b60d12743e6122e7f5b [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>
Liviu Dudaub9c33152016-11-25 14:28:54 +000014#include <drm/drm_atomic.h>
Liviu Dudauad49f862016-03-07 10:00:53 +000015#include <drm/drm_atomic_helper.h>
16#include <drm/drm_fb_cma_helper.h>
17#include <drm/drm_gem_cma_helper.h>
18#include <drm/drm_plane_helper.h>
19
20#include "malidp_hw.h"
21#include "malidp_drv.h"
22
23/* Layer specific register offsets */
24#define MALIDP_LAYER_FORMAT 0x000
25#define MALIDP_LAYER_CONTROL 0x004
26#define LAYER_ENABLE (1 << 0)
27#define LAYER_ROT_OFFSET 8
28#define LAYER_H_FLIP (1 << 10)
29#define LAYER_V_FLIP (1 << 11)
30#define LAYER_ROT_MASK (0xf << 8)
Brian Starkeyc57eb712016-10-11 15:26:08 +010031#define LAYER_COMP_MASK (0x3 << 12)
32#define LAYER_COMP_PIXEL (0x3 << 12)
33#define LAYER_COMP_PLANE (0x2 << 12)
34#define MALIDP_LAYER_COMPOSE 0x008
Liviu Dudauad49f862016-03-07 10:00:53 +000035#define MALIDP_LAYER_SIZE 0x00c
36#define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
37#define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
38#define MALIDP_LAYER_COMP_SIZE 0x010
39#define MALIDP_LAYER_OFFSET 0x014
40#define MALIDP_LAYER_STRIDE 0x018
41
Brian Starkeyc57eb712016-10-11 15:26:08 +010042/*
43 * This 4-entry look-up-table is used to determine the full 8-bit alpha value
44 * for formats with 1- or 2-bit alpha channels.
45 * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
46 * opacity for 2-bit formats.
47 */
48#define MALIDP_ALPHA_LUT 0xffaa5500
49
Liviu Dudauad49f862016-03-07 10:00:53 +000050static void malidp_de_plane_destroy(struct drm_plane *plane)
51{
52 struct malidp_plane *mp = to_malidp_plane(plane);
53
54 if (mp->base.fb)
55 drm_framebuffer_unreference(mp->base.fb);
56
57 drm_plane_helper_disable(plane);
58 drm_plane_cleanup(plane);
59 devm_kfree(plane->dev->dev, mp);
60}
61
Baoyou Xieed8b0c02016-10-22 17:13:01 +080062static struct
63drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
Liviu Dudauad49f862016-03-07 10:00:53 +000064{
65 struct malidp_plane_state *state, *m_state;
66
67 if (!plane->state)
68 return NULL;
69
70 state = kmalloc(sizeof(*state), GFP_KERNEL);
Shailendra Verma94d8b9b2016-11-11 13:35:00 +000071 if (!state)
72 return NULL;
73
74 m_state = to_malidp_plane_state(plane->state);
75 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
76 state->rotmem_size = m_state->rotmem_size;
77 state->format = m_state->format;
78 state->n_planes = m_state->n_planes;
Liviu Dudauad49f862016-03-07 10:00:53 +000079
80 return &state->base;
81}
82
Baoyou Xieed8b0c02016-10-22 17:13:01 +080083static void malidp_destroy_plane_state(struct drm_plane *plane,
84 struct drm_plane_state *state)
Liviu Dudauad49f862016-03-07 10:00:53 +000085{
86 struct malidp_plane_state *m_state = to_malidp_plane_state(state);
87
88 __drm_atomic_helper_plane_destroy_state(state);
89 kfree(m_state);
90}
91
92static const struct drm_plane_funcs malidp_de_plane_funcs = {
93 .update_plane = drm_atomic_helper_update_plane,
94 .disable_plane = drm_atomic_helper_disable_plane,
Liviu Dudau2fe1f082016-10-24 18:35:09 +010095 .set_property = drm_atomic_helper_plane_set_property,
Liviu Dudauad49f862016-03-07 10:00:53 +000096 .destroy = malidp_de_plane_destroy,
97 .reset = drm_atomic_helper_plane_reset,
98 .atomic_duplicate_state = malidp_duplicate_plane_state,
99 .atomic_destroy_state = malidp_destroy_plane_state,
100};
101
102static int malidp_de_plane_check(struct drm_plane *plane,
103 struct drm_plane_state *state)
104{
105 struct malidp_plane *mp = to_malidp_plane(plane);
106 struct malidp_plane_state *ms = to_malidp_plane_state(state);
Liviu Dudaub9c33152016-11-25 14:28:54 +0000107 struct drm_crtc_state *crtc_state;
Brian Starkeya46a0962016-10-11 15:26:05 +0100108 struct drm_framebuffer *fb;
Liviu Dudaub9c33152016-11-25 14:28:54 +0000109 struct drm_rect clip = { 0 };
110 int i, ret;
Liviu Dudauad49f862016-03-07 10:00:53 +0000111 u32 src_w, src_h;
112
113 if (!state->crtc || !state->fb)
114 return 0;
115
Brian Starkeya46a0962016-10-11 15:26:05 +0100116 fb = state->fb;
117
Brian Starkey70c94a32016-10-11 15:26:09 +0100118 ms->format = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id,
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200119 fb->format->format);
Brian Starkey70c94a32016-10-11 15:26:09 +0100120 if (ms->format == MALIDP_INVALID_FORMAT_ID)
Liviu Dudauad49f862016-03-07 10:00:53 +0000121 return -EINVAL;
122
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200123 ms->n_planes = fb->format->num_planes;
Brian Starkey70c94a32016-10-11 15:26:09 +0100124 for (i = 0; i < ms->n_planes; i++) {
Brian Starkeya46a0962016-10-11 15:26:05 +0100125 if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
126 DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
127 fb->pitches[i], i);
128 return -EINVAL;
129 }
130 }
131
Liviu Dudauad49f862016-03-07 10:00:53 +0000132 src_w = state->src_w >> 16;
133 src_h = state->src_h >> 16;
134
135 if ((state->crtc_w > mp->hwdev->max_line_size) ||
136 (state->crtc_h > mp->hwdev->max_line_size) ||
137 (state->crtc_w < mp->hwdev->min_line_size) ||
138 (state->crtc_h < mp->hwdev->min_line_size) ||
139 (state->crtc_w != src_w) || (state->crtc_h != src_h))
140 return -EINVAL;
141
142 /* packed RGB888 / BGR888 can't be rotated or flipped */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300143 if (state->rotation != DRM_ROTATE_0 &&
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200144 (fb->format->format == DRM_FORMAT_RGB888 ||
145 fb->format->format == DRM_FORMAT_BGR888))
Liviu Dudauad49f862016-03-07 10:00:53 +0000146 return -EINVAL;
147
Liviu Dudaub9c33152016-11-25 14:28:54 +0000148 crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
149 clip.x2 = crtc_state->adjusted_mode.hdisplay;
150 clip.y2 = crtc_state->adjusted_mode.vdisplay;
151 ret = drm_plane_helper_check_state(state, &clip,
152 DRM_PLANE_HELPER_NO_SCALING,
153 DRM_PLANE_HELPER_NO_SCALING,
154 true, true);
155 if (ret)
156 return ret;
157
Liviu Dudauad49f862016-03-07 10:00:53 +0000158 ms->rotmem_size = 0;
159 if (state->rotation & MALIDP_ROTATED_MASK) {
160 int val;
161
162 val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
163 state->crtc_w,
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200164 fb->format->format);
Liviu Dudauad49f862016-03-07 10:00:53 +0000165 if (val < 0)
166 return val;
167
168 ms->rotmem_size = val;
169 }
170
171 return 0;
172}
173
174static void malidp_de_plane_update(struct drm_plane *plane,
175 struct drm_plane_state *old_state)
176{
177 struct drm_gem_cma_object *obj;
178 struct malidp_plane *mp;
179 const struct malidp_hw_regmap *map;
Brian Starkey70c94a32016-10-11 15:26:09 +0100180 struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
Liviu Dudauad49f862016-03-07 10:00:53 +0000181 u16 ptr;
Brian Starkey70c94a32016-10-11 15:26:09 +0100182 u32 src_w, src_h, dest_w, dest_h, val;
183 int i;
Liviu Dudauad49f862016-03-07 10:00:53 +0000184
185 mp = to_malidp_plane(plane);
Liviu Dudauad49f862016-03-07 10:00:53 +0000186 map = &mp->hwdev->map;
Liviu Dudauad49f862016-03-07 10:00:53 +0000187
188 /* convert src values from Q16 fixed point to integer */
189 src_w = plane->state->src_w >> 16;
190 src_h = plane->state->src_h >> 16;
191 if (plane->state->rotation & MALIDP_ROTATED_MASK) {
192 dest_w = plane->state->crtc_h;
193 dest_h = plane->state->crtc_w;
194 } else {
195 dest_w = plane->state->crtc_w;
196 dest_h = plane->state->crtc_h;
197 }
198
Brian Starkey70c94a32016-10-11 15:26:09 +0100199 malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
Liviu Dudauad49f862016-03-07 10:00:53 +0000200
Brian Starkey70c94a32016-10-11 15:26:09 +0100201 for (i = 0; i < ms->n_planes; i++) {
Liviu Dudauad49f862016-03-07 10:00:53 +0000202 /* calculate the offset for the layer's plane registers */
203 ptr = mp->layer->ptr + (i << 4);
204
205 obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
206 malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
207 malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
208 malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i],
209 mp->layer->base + MALIDP_LAYER_STRIDE);
210 }
211
212 malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
213 mp->layer->base + MALIDP_LAYER_SIZE);
214
215 malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
216 mp->layer->base + MALIDP_LAYER_COMP_SIZE);
217
218 malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
219 LAYER_V_VAL(plane->state->crtc_y),
220 mp->layer->base + MALIDP_LAYER_OFFSET);
221
Brian Starkeyc57eb712016-10-11 15:26:08 +0100222 /* first clear the rotation bits */
223 val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
224 val &= ~LAYER_ROT_MASK;
Liviu Dudauad49f862016-03-07 10:00:53 +0000225
226 /* setup the rotation and axis flip bits */
227 if (plane->state->rotation & DRM_ROTATE_MASK)
228 val = ilog2(plane->state->rotation & DRM_ROTATE_MASK) << LAYER_ROT_OFFSET;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300229 if (plane->state->rotation & DRM_REFLECT_X)
Liviu Dudauad49f862016-03-07 10:00:53 +0000230 val |= LAYER_V_FLIP;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300231 if (plane->state->rotation & DRM_REFLECT_Y)
Liviu Dudauad49f862016-03-07 10:00:53 +0000232 val |= LAYER_H_FLIP;
233
Brian Starkeyc57eb712016-10-11 15:26:08 +0100234 /*
235 * always enable pixel alpha blending until we have a way to change
236 * blend modes
237 */
238 val &= ~LAYER_COMP_MASK;
239 val |= LAYER_COMP_PIXEL;
240
Liviu Dudauad49f862016-03-07 10:00:53 +0000241 /* set the 'enable layer' bit */
242 val |= LAYER_ENABLE;
243
Brian Starkeyc57eb712016-10-11 15:26:08 +0100244 malidp_hw_write(mp->hwdev, val,
245 mp->layer->base + MALIDP_LAYER_CONTROL);
Liviu Dudauad49f862016-03-07 10:00:53 +0000246}
247
248static void malidp_de_plane_disable(struct drm_plane *plane,
249 struct drm_plane_state *state)
250{
251 struct malidp_plane *mp = to_malidp_plane(plane);
252
253 malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
254 mp->layer->base + MALIDP_LAYER_CONTROL);
255}
256
257static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
258 .atomic_check = malidp_de_plane_check,
259 .atomic_update = malidp_de_plane_update,
260 .atomic_disable = malidp_de_plane_disable,
261};
262
263int malidp_de_planes_init(struct drm_device *drm)
264{
265 struct malidp_drm *malidp = drm->dev_private;
266 const struct malidp_hw_regmap *map = &malidp->dev->map;
267 struct malidp_plane *plane = NULL;
268 enum drm_plane_type plane_type;
269 unsigned long crtcs = 1 << drm->mode_config.num_crtc;
Brian Starkey15807782016-10-11 15:26:07 +0100270 unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 |
271 DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y;
Liviu Dudauad49f862016-03-07 10:00:53 +0000272 u32 *formats;
273 int ret, i, j, n;
274
275 formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL);
276 if (!formats) {
277 ret = -ENOMEM;
278 goto cleanup;
279 }
280
281 for (i = 0; i < map->n_layers; i++) {
282 u8 id = map->layers[i].id;
283
284 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
285 if (!plane) {
286 ret = -ENOMEM;
287 goto cleanup;
288 }
289
290 /* build the list of DRM supported formats based on the map */
291 for (n = 0, j = 0; j < map->n_input_formats; j++) {
292 if ((map->input_formats[j].layer & id) == id)
293 formats[n++] = map->input_formats[j].format;
294 }
295
296 plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
297 DRM_PLANE_TYPE_OVERLAY;
298 ret = drm_universal_plane_init(drm, &plane->base, crtcs,
299 &malidp_de_plane_funcs, formats,
300 n, plane_type, NULL);
301 if (ret < 0)
302 goto cleanup;
303
Liviu Dudauad49f862016-03-07 10:00:53 +0000304 drm_plane_helper_add(&plane->base,
305 &malidp_de_plane_helper_funcs);
306 plane->hwdev = malidp->dev;
307 plane->layer = &map->layers[i];
Brian Starkey15807782016-10-11 15:26:07 +0100308
309 /* Skip the features which the SMART layer doesn't have */
310 if (id == DE_SMART)
311 continue;
312
313 drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags);
Brian Starkeyc57eb712016-10-11 15:26:08 +0100314 malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
315 plane->layer->base + MALIDP_LAYER_COMPOSE);
Liviu Dudauad49f862016-03-07 10:00:53 +0000316 }
317
318 kfree(formats);
319
320 return 0;
321
322cleanup:
323 malidp_de_planes_destroy(drm);
324 kfree(formats);
325
326 return ret;
327}
328
329void malidp_de_planes_destroy(struct drm_device *drm)
330{
331 struct drm_plane *p, *pt;
332
333 list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
334 drm_plane_cleanup(p);
335 kfree(p);
336 }
337}