blob: 69eba71253c9fce1a738e546dd2c7f1e082e583a [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) ||
Brian Starkeyb2a2ddb2016-12-07 13:14:51 +0000138 (state->crtc_h < mp->hwdev->min_line_size))
Liviu Dudauad49f862016-03-07 10:00:53 +0000139 return -EINVAL;
140
141 /* packed RGB888 / BGR888 can't be rotated or flipped */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300142 if (state->rotation != DRM_ROTATE_0 &&
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200143 (fb->format->format == DRM_FORMAT_RGB888 ||
144 fb->format->format == DRM_FORMAT_BGR888))
Liviu Dudauad49f862016-03-07 10:00:53 +0000145 return -EINVAL;
146
Liviu Dudaub9c33152016-11-25 14:28:54 +0000147 crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
148 clip.x2 = crtc_state->adjusted_mode.hdisplay;
149 clip.y2 = crtc_state->adjusted_mode.vdisplay;
150 ret = drm_plane_helper_check_state(state, &clip,
151 DRM_PLANE_HELPER_NO_SCALING,
152 DRM_PLANE_HELPER_NO_SCALING,
153 true, true);
154 if (ret)
155 return ret;
156
Liviu Dudauad49f862016-03-07 10:00:53 +0000157 ms->rotmem_size = 0;
158 if (state->rotation & MALIDP_ROTATED_MASK) {
159 int val;
160
161 val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
162 state->crtc_w,
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200163 fb->format->format);
Liviu Dudauad49f862016-03-07 10:00:53 +0000164 if (val < 0)
165 return val;
166
167 ms->rotmem_size = val;
168 }
169
170 return 0;
171}
172
173static void malidp_de_plane_update(struct drm_plane *plane,
174 struct drm_plane_state *old_state)
175{
176 struct drm_gem_cma_object *obj;
177 struct malidp_plane *mp;
178 const struct malidp_hw_regmap *map;
Brian Starkey70c94a32016-10-11 15:26:09 +0100179 struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
Liviu Dudauad49f862016-03-07 10:00:53 +0000180 u16 ptr;
Brian Starkey70c94a32016-10-11 15:26:09 +0100181 u32 src_w, src_h, dest_w, dest_h, val;
182 int i;
Liviu Dudauad49f862016-03-07 10:00:53 +0000183
184 mp = to_malidp_plane(plane);
Liviu Dudauad49f862016-03-07 10:00:53 +0000185 map = &mp->hwdev->map;
Liviu Dudauad49f862016-03-07 10:00:53 +0000186
187 /* convert src values from Q16 fixed point to integer */
188 src_w = plane->state->src_w >> 16;
189 src_h = plane->state->src_h >> 16;
190 if (plane->state->rotation & MALIDP_ROTATED_MASK) {
191 dest_w = plane->state->crtc_h;
192 dest_h = plane->state->crtc_w;
193 } else {
194 dest_w = plane->state->crtc_w;
195 dest_h = plane->state->crtc_h;
196 }
197
Brian Starkey70c94a32016-10-11 15:26:09 +0100198 malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
Liviu Dudauad49f862016-03-07 10:00:53 +0000199
Brian Starkey70c94a32016-10-11 15:26:09 +0100200 for (i = 0; i < ms->n_planes; i++) {
Liviu Dudauad49f862016-03-07 10:00:53 +0000201 /* calculate the offset for the layer's plane registers */
202 ptr = mp->layer->ptr + (i << 4);
203
204 obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
205 malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
206 malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
207 malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i],
208 mp->layer->base + MALIDP_LAYER_STRIDE);
209 }
210
211 malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
212 mp->layer->base + MALIDP_LAYER_SIZE);
213
214 malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
215 mp->layer->base + MALIDP_LAYER_COMP_SIZE);
216
217 malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
218 LAYER_V_VAL(plane->state->crtc_y),
219 mp->layer->base + MALIDP_LAYER_OFFSET);
220
Brian Starkeyc57eb712016-10-11 15:26:08 +0100221 /* first clear the rotation bits */
222 val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
223 val &= ~LAYER_ROT_MASK;
Liviu Dudauad49f862016-03-07 10:00:53 +0000224
225 /* setup the rotation and axis flip bits */
226 if (plane->state->rotation & DRM_ROTATE_MASK)
227 val = ilog2(plane->state->rotation & DRM_ROTATE_MASK) << LAYER_ROT_OFFSET;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300228 if (plane->state->rotation & DRM_REFLECT_X)
Liviu Dudauad49f862016-03-07 10:00:53 +0000229 val |= LAYER_V_FLIP;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300230 if (plane->state->rotation & DRM_REFLECT_Y)
Liviu Dudauad49f862016-03-07 10:00:53 +0000231 val |= LAYER_H_FLIP;
232
Brian Starkeyc57eb712016-10-11 15:26:08 +0100233 /*
234 * always enable pixel alpha blending until we have a way to change
235 * blend modes
236 */
237 val &= ~LAYER_COMP_MASK;
238 val |= LAYER_COMP_PIXEL;
239
Liviu Dudauad49f862016-03-07 10:00:53 +0000240 /* set the 'enable layer' bit */
241 val |= LAYER_ENABLE;
242
Brian Starkeyc57eb712016-10-11 15:26:08 +0100243 malidp_hw_write(mp->hwdev, val,
244 mp->layer->base + MALIDP_LAYER_CONTROL);
Liviu Dudauad49f862016-03-07 10:00:53 +0000245}
246
247static void malidp_de_plane_disable(struct drm_plane *plane,
248 struct drm_plane_state *state)
249{
250 struct malidp_plane *mp = to_malidp_plane(plane);
251
252 malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
253 mp->layer->base + MALIDP_LAYER_CONTROL);
254}
255
256static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
257 .atomic_check = malidp_de_plane_check,
258 .atomic_update = malidp_de_plane_update,
259 .atomic_disable = malidp_de_plane_disable,
260};
261
262int malidp_de_planes_init(struct drm_device *drm)
263{
264 struct malidp_drm *malidp = drm->dev_private;
265 const struct malidp_hw_regmap *map = &malidp->dev->map;
266 struct malidp_plane *plane = NULL;
267 enum drm_plane_type plane_type;
268 unsigned long crtcs = 1 << drm->mode_config.num_crtc;
Brian Starkey15807782016-10-11 15:26:07 +0100269 unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 |
270 DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y;
Liviu Dudauad49f862016-03-07 10:00:53 +0000271 u32 *formats;
272 int ret, i, j, n;
273
274 formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL);
275 if (!formats) {
276 ret = -ENOMEM;
277 goto cleanup;
278 }
279
280 for (i = 0; i < map->n_layers; i++) {
281 u8 id = map->layers[i].id;
282
283 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
284 if (!plane) {
285 ret = -ENOMEM;
286 goto cleanup;
287 }
288
289 /* build the list of DRM supported formats based on the map */
290 for (n = 0, j = 0; j < map->n_input_formats; j++) {
291 if ((map->input_formats[j].layer & id) == id)
292 formats[n++] = map->input_formats[j].format;
293 }
294
295 plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
296 DRM_PLANE_TYPE_OVERLAY;
297 ret = drm_universal_plane_init(drm, &plane->base, crtcs,
298 &malidp_de_plane_funcs, formats,
299 n, plane_type, NULL);
300 if (ret < 0)
301 goto cleanup;
302
Liviu Dudauad49f862016-03-07 10:00:53 +0000303 drm_plane_helper_add(&plane->base,
304 &malidp_de_plane_helper_funcs);
305 plane->hwdev = malidp->dev;
306 plane->layer = &map->layers[i];
Brian Starkey15807782016-10-11 15:26:07 +0100307
308 /* Skip the features which the SMART layer doesn't have */
309 if (id == DE_SMART)
310 continue;
311
312 drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags);
Brian Starkeyc57eb712016-10-11 15:26:08 +0100313 malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
314 plane->layer->base + MALIDP_LAYER_COMPOSE);
Liviu Dudauad49f862016-03-07 10:00:53 +0000315 }
316
317 kfree(formats);
318
319 return 0;
320
321cleanup:
322 malidp_de_planes_destroy(drm);
323 kfree(formats);
324
325 return ret;
326}
327
328void malidp_de_planes_destroy(struct drm_device *drm)
329{
330 struct drm_plane *p, *pt;
331
332 list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
333 drm_plane_cleanup(p);
334 kfree(p);
335 }
336}