blob: d1cc15724dc337f22b84e41fa169d3d7a2833997 [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
Baoyou Xieed8b0c02016-10-22 17:13:01 +080061static struct
62drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
Liviu Dudauad49f862016-03-07 10:00:53 +000063{
64 struct malidp_plane_state *state, *m_state;
65
66 if (!plane->state)
67 return NULL;
68
69 state = kmalloc(sizeof(*state), GFP_KERNEL);
Shailendra Verma94d8b9b2016-11-11 13:35:00 +000070 if (!state)
71 return NULL;
72
73 m_state = to_malidp_plane_state(plane->state);
74 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
75 state->rotmem_size = m_state->rotmem_size;
76 state->format = m_state->format;
77 state->n_planes = m_state->n_planes;
Liviu Dudauad49f862016-03-07 10:00:53 +000078
79 return &state->base;
80}
81
Baoyou Xieed8b0c02016-10-22 17:13:01 +080082static void malidp_destroy_plane_state(struct drm_plane *plane,
83 struct drm_plane_state *state)
Liviu Dudauad49f862016-03-07 10:00:53 +000084{
85 struct malidp_plane_state *m_state = to_malidp_plane_state(state);
86
87 __drm_atomic_helper_plane_destroy_state(state);
88 kfree(m_state);
89}
90
91static const struct drm_plane_funcs malidp_de_plane_funcs = {
92 .update_plane = drm_atomic_helper_update_plane,
93 .disable_plane = drm_atomic_helper_disable_plane,
Liviu Dudau2fe1f082016-10-24 18:35:09 +010094 .set_property = drm_atomic_helper_plane_set_property,
Liviu Dudauad49f862016-03-07 10:00:53 +000095 .destroy = malidp_de_plane_destroy,
96 .reset = drm_atomic_helper_plane_reset,
97 .atomic_duplicate_state = malidp_duplicate_plane_state,
98 .atomic_destroy_state = malidp_destroy_plane_state,
99};
100
101static int malidp_de_plane_check(struct drm_plane *plane,
102 struct drm_plane_state *state)
103{
104 struct malidp_plane *mp = to_malidp_plane(plane);
105 struct malidp_plane_state *ms = to_malidp_plane_state(state);
Brian Starkeya46a0962016-10-11 15:26:05 +0100106 struct drm_framebuffer *fb;
Brian Starkey70c94a32016-10-11 15:26:09 +0100107 int i;
Liviu Dudauad49f862016-03-07 10:00:53 +0000108 u32 src_w, src_h;
109
110 if (!state->crtc || !state->fb)
111 return 0;
112
Brian Starkeya46a0962016-10-11 15:26:05 +0100113 fb = state->fb;
114
Brian Starkey70c94a32016-10-11 15:26:09 +0100115 ms->format = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id,
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200116 fb->format->format);
Brian Starkey70c94a32016-10-11 15:26:09 +0100117 if (ms->format == MALIDP_INVALID_FORMAT_ID)
Liviu Dudauad49f862016-03-07 10:00:53 +0000118 return -EINVAL;
119
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200120 ms->n_planes = fb->format->num_planes;
Brian Starkey70c94a32016-10-11 15:26:09 +0100121 for (i = 0; i < ms->n_planes; i++) {
Brian Starkeya46a0962016-10-11 15:26:05 +0100122 if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
123 DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
124 fb->pitches[i], i);
125 return -EINVAL;
126 }
127 }
128
Liviu Dudauad49f862016-03-07 10:00:53 +0000129 src_w = state->src_w >> 16;
130 src_h = state->src_h >> 16;
131
132 if ((state->crtc_w > mp->hwdev->max_line_size) ||
133 (state->crtc_h > mp->hwdev->max_line_size) ||
134 (state->crtc_w < mp->hwdev->min_line_size) ||
135 (state->crtc_h < mp->hwdev->min_line_size) ||
136 (state->crtc_w != src_w) || (state->crtc_h != src_h))
137 return -EINVAL;
138
139 /* packed RGB888 / BGR888 can't be rotated or flipped */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300140 if (state->rotation != DRM_ROTATE_0 &&
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200141 (fb->format->format == DRM_FORMAT_RGB888 ||
142 fb->format->format == DRM_FORMAT_BGR888))
Liviu Dudauad49f862016-03-07 10:00:53 +0000143 return -EINVAL;
144
145 ms->rotmem_size = 0;
146 if (state->rotation & MALIDP_ROTATED_MASK) {
147 int val;
148
149 val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
150 state->crtc_w,
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200151 fb->format->format);
Liviu Dudauad49f862016-03-07 10:00:53 +0000152 if (val < 0)
153 return val;
154
155 ms->rotmem_size = val;
156 }
157
158 return 0;
159}
160
161static void malidp_de_plane_update(struct drm_plane *plane,
162 struct drm_plane_state *old_state)
163{
164 struct drm_gem_cma_object *obj;
165 struct malidp_plane *mp;
166 const struct malidp_hw_regmap *map;
Brian Starkey70c94a32016-10-11 15:26:09 +0100167 struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
Liviu Dudauad49f862016-03-07 10:00:53 +0000168 u16 ptr;
Brian Starkey70c94a32016-10-11 15:26:09 +0100169 u32 src_w, src_h, dest_w, dest_h, val;
170 int i;
Liviu Dudauad49f862016-03-07 10:00:53 +0000171
172 mp = to_malidp_plane(plane);
Liviu Dudauad49f862016-03-07 10:00:53 +0000173 map = &mp->hwdev->map;
Liviu Dudauad49f862016-03-07 10:00:53 +0000174
175 /* convert src values from Q16 fixed point to integer */
176 src_w = plane->state->src_w >> 16;
177 src_h = plane->state->src_h >> 16;
178 if (plane->state->rotation & MALIDP_ROTATED_MASK) {
179 dest_w = plane->state->crtc_h;
180 dest_h = plane->state->crtc_w;
181 } else {
182 dest_w = plane->state->crtc_w;
183 dest_h = plane->state->crtc_h;
184 }
185
Brian Starkey70c94a32016-10-11 15:26:09 +0100186 malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
Liviu Dudauad49f862016-03-07 10:00:53 +0000187
Brian Starkey70c94a32016-10-11 15:26:09 +0100188 for (i = 0; i < ms->n_planes; i++) {
Liviu Dudauad49f862016-03-07 10:00:53 +0000189 /* calculate the offset for the layer's plane registers */
190 ptr = mp->layer->ptr + (i << 4);
191
192 obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
193 malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
194 malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
195 malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i],
196 mp->layer->base + MALIDP_LAYER_STRIDE);
197 }
198
199 malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
200 mp->layer->base + MALIDP_LAYER_SIZE);
201
202 malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
203 mp->layer->base + MALIDP_LAYER_COMP_SIZE);
204
205 malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
206 LAYER_V_VAL(plane->state->crtc_y),
207 mp->layer->base + MALIDP_LAYER_OFFSET);
208
Brian Starkeyc57eb712016-10-11 15:26:08 +0100209 /* first clear the rotation bits */
210 val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
211 val &= ~LAYER_ROT_MASK;
Liviu Dudauad49f862016-03-07 10:00:53 +0000212
213 /* setup the rotation and axis flip bits */
214 if (plane->state->rotation & DRM_ROTATE_MASK)
215 val = ilog2(plane->state->rotation & DRM_ROTATE_MASK) << LAYER_ROT_OFFSET;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300216 if (plane->state->rotation & DRM_REFLECT_X)
Liviu Dudauad49f862016-03-07 10:00:53 +0000217 val |= LAYER_V_FLIP;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300218 if (plane->state->rotation & DRM_REFLECT_Y)
Liviu Dudauad49f862016-03-07 10:00:53 +0000219 val |= LAYER_H_FLIP;
220
Brian Starkeyc57eb712016-10-11 15:26:08 +0100221 /*
222 * always enable pixel alpha blending until we have a way to change
223 * blend modes
224 */
225 val &= ~LAYER_COMP_MASK;
226 val |= LAYER_COMP_PIXEL;
227
Liviu Dudauad49f862016-03-07 10:00:53 +0000228 /* set the 'enable layer' bit */
229 val |= LAYER_ENABLE;
230
Brian Starkeyc57eb712016-10-11 15:26:08 +0100231 malidp_hw_write(mp->hwdev, val,
232 mp->layer->base + MALIDP_LAYER_CONTROL);
Liviu Dudauad49f862016-03-07 10:00:53 +0000233}
234
235static void malidp_de_plane_disable(struct drm_plane *plane,
236 struct drm_plane_state *state)
237{
238 struct malidp_plane *mp = to_malidp_plane(plane);
239
240 malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
241 mp->layer->base + MALIDP_LAYER_CONTROL);
242}
243
244static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
245 .atomic_check = malidp_de_plane_check,
246 .atomic_update = malidp_de_plane_update,
247 .atomic_disable = malidp_de_plane_disable,
248};
249
250int malidp_de_planes_init(struct drm_device *drm)
251{
252 struct malidp_drm *malidp = drm->dev_private;
253 const struct malidp_hw_regmap *map = &malidp->dev->map;
254 struct malidp_plane *plane = NULL;
255 enum drm_plane_type plane_type;
256 unsigned long crtcs = 1 << drm->mode_config.num_crtc;
Brian Starkey15807782016-10-11 15:26:07 +0100257 unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 |
258 DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y;
Liviu Dudauad49f862016-03-07 10:00:53 +0000259 u32 *formats;
260 int ret, i, j, n;
261
262 formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL);
263 if (!formats) {
264 ret = -ENOMEM;
265 goto cleanup;
266 }
267
268 for (i = 0; i < map->n_layers; i++) {
269 u8 id = map->layers[i].id;
270
271 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
272 if (!plane) {
273 ret = -ENOMEM;
274 goto cleanup;
275 }
276
277 /* build the list of DRM supported formats based on the map */
278 for (n = 0, j = 0; j < map->n_input_formats; j++) {
279 if ((map->input_formats[j].layer & id) == id)
280 formats[n++] = map->input_formats[j].format;
281 }
282
283 plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
284 DRM_PLANE_TYPE_OVERLAY;
285 ret = drm_universal_plane_init(drm, &plane->base, crtcs,
286 &malidp_de_plane_funcs, formats,
287 n, plane_type, NULL);
288 if (ret < 0)
289 goto cleanup;
290
Liviu Dudauad49f862016-03-07 10:00:53 +0000291 drm_plane_helper_add(&plane->base,
292 &malidp_de_plane_helper_funcs);
293 plane->hwdev = malidp->dev;
294 plane->layer = &map->layers[i];
Brian Starkey15807782016-10-11 15:26:07 +0100295
296 /* Skip the features which the SMART layer doesn't have */
297 if (id == DE_SMART)
298 continue;
299
300 drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags);
Brian Starkeyc57eb712016-10-11 15:26:08 +0100301 malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
302 plane->layer->base + MALIDP_LAYER_COMPOSE);
Liviu Dudauad49f862016-03-07 10:00:53 +0000303 }
304
305 kfree(formats);
306
307 return 0;
308
309cleanup:
310 malidp_de_planes_destroy(drm);
311 kfree(formats);
312
313 return ret;
314}
315
316void malidp_de_planes_destroy(struct drm_device *drm)
317{
318 struct drm_plane *p, *pt;
319
320 list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
321 drm_plane_cleanup(p);
322 kfree(p);
323 }
324}