1 /*
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.h>
15 #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 #include <drm/drm_print.h>
20 
21 #include "malidp_hw.h"
22 #include "malidp_drv.h"
23 
24 /* Layer specific register offsets */
25 #define MALIDP_LAYER_FORMAT		0x000
26 #define MALIDP_LAYER_CONTROL		0x004
27 #define   LAYER_ENABLE			(1 << 0)
28 #define   LAYER_FLOWCFG_MASK		7
29 #define   LAYER_FLOWCFG(x)		(((x) & LAYER_FLOWCFG_MASK) << 1)
30 #define     LAYER_FLOWCFG_SCALE_SE	3
31 #define   LAYER_ROT_OFFSET		8
32 #define   LAYER_H_FLIP			(1 << 10)
33 #define   LAYER_V_FLIP			(1 << 11)
34 #define   LAYER_ROT_MASK		(0xf << 8)
35 #define   LAYER_COMP_MASK		(0x3 << 12)
36 #define   LAYER_COMP_PIXEL		(0x3 << 12)
37 #define   LAYER_COMP_PLANE		(0x2 << 12)
38 #define MALIDP_LAYER_COMPOSE		0x008
39 #define MALIDP_LAYER_SIZE		0x00c
40 #define   LAYER_H_VAL(x)		(((x) & 0x1fff) << 0)
41 #define   LAYER_V_VAL(x)		(((x) & 0x1fff) << 16)
42 #define MALIDP_LAYER_COMP_SIZE		0x010
43 #define MALIDP_LAYER_OFFSET		0x014
44 #define MALIDP550_LS_ENABLE		0x01c
45 #define MALIDP550_LS_R1_IN_SIZE		0x020
46 
47 /*
48  * This 4-entry look-up-table is used to determine the full 8-bit alpha value
49  * for formats with 1- or 2-bit alpha channels.
50  * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
51  * opacity for 2-bit formats.
52  */
53 #define MALIDP_ALPHA_LUT 0xffaa5500
54 
55 static void malidp_de_plane_destroy(struct drm_plane *plane)
56 {
57 	struct malidp_plane *mp = to_malidp_plane(plane);
58 
59 	if (mp->base.fb)
60 		drm_framebuffer_put(mp->base.fb);
61 
62 	drm_plane_helper_disable(plane);
63 	drm_plane_cleanup(plane);
64 	devm_kfree(plane->dev->dev, mp);
65 }
66 
67 /*
68  * Replicate what the default ->reset hook does: free the state pointer and
69  * allocate a new empty object. We just need enough space to store
70  * a malidp_plane_state instead of a drm_plane_state.
71  */
72 static void malidp_plane_reset(struct drm_plane *plane)
73 {
74 	struct malidp_plane_state *state = to_malidp_plane_state(plane->state);
75 
76 	if (state)
77 		__drm_atomic_helper_plane_destroy_state(&state->base);
78 	kfree(state);
79 	plane->state = NULL;
80 	state = kzalloc(sizeof(*state), GFP_KERNEL);
81 	if (state) {
82 		state->base.plane = plane;
83 		state->base.rotation = DRM_MODE_ROTATE_0;
84 		plane->state = &state->base;
85 	}
86 }
87 
88 static struct
89 drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
90 {
91 	struct malidp_plane_state *state, *m_state;
92 
93 	if (!plane->state)
94 		return NULL;
95 
96 	state = kmalloc(sizeof(*state), GFP_KERNEL);
97 	if (!state)
98 		return NULL;
99 
100 	m_state = to_malidp_plane_state(plane->state);
101 	__drm_atomic_helper_plane_duplicate_state(plane, &state->base);
102 	state->rotmem_size = m_state->rotmem_size;
103 	state->format = m_state->format;
104 	state->n_planes = m_state->n_planes;
105 
106 	return &state->base;
107 }
108 
109 static void malidp_destroy_plane_state(struct drm_plane *plane,
110 				       struct drm_plane_state *state)
111 {
112 	struct malidp_plane_state *m_state = to_malidp_plane_state(state);
113 
114 	__drm_atomic_helper_plane_destroy_state(state);
115 	kfree(m_state);
116 }
117 
118 static void malidp_plane_atomic_print_state(struct drm_printer *p,
119 					    const struct drm_plane_state *state)
120 {
121 	struct malidp_plane_state *ms = to_malidp_plane_state(state);
122 
123 	drm_printf(p, "\trotmem_size=%u\n", ms->rotmem_size);
124 	drm_printf(p, "\tformat_id=%u\n", ms->format);
125 	drm_printf(p, "\tn_planes=%u\n", ms->n_planes);
126 }
127 
128 static const struct drm_plane_funcs malidp_de_plane_funcs = {
129 	.update_plane = drm_atomic_helper_update_plane,
130 	.disable_plane = drm_atomic_helper_disable_plane,
131 	.destroy = malidp_de_plane_destroy,
132 	.reset = malidp_plane_reset,
133 	.atomic_duplicate_state = malidp_duplicate_plane_state,
134 	.atomic_destroy_state = malidp_destroy_plane_state,
135 	.atomic_print_state = malidp_plane_atomic_print_state,
136 };
137 
138 static int malidp_se_check_scaling(struct malidp_plane *mp,
139 				   struct drm_plane_state *state)
140 {
141 	struct drm_crtc_state *crtc_state =
142 		drm_atomic_get_existing_crtc_state(state->state, state->crtc);
143 	struct malidp_crtc_state *mc;
144 	u32 src_w, src_h;
145 	int ret;
146 
147 	if (!crtc_state)
148 		return -EINVAL;
149 
150 	ret = drm_atomic_helper_check_plane_state(state, crtc_state,
151 						  0, INT_MAX, true, true);
152 	if (ret)
153 		return ret;
154 
155 	src_w = state->src_w >> 16;
156 	src_h = state->src_h >> 16;
157 	if ((state->crtc_w == src_w) && (state->crtc_h == src_h)) {
158 		/* Scaling not necessary for this plane. */
159 		mc->scaled_planes_mask &= ~(mp->layer->id);
160 		return 0;
161 	}
162 
163 	if (mp->layer->id & (DE_SMART | DE_GRAPHICS2))
164 		return -EINVAL;
165 
166 	mc = to_malidp_crtc_state(crtc_state);
167 
168 	mc->scaled_planes_mask |= mp->layer->id;
169 	/* Defer scaling requirements calculation to the crtc check. */
170 	return 0;
171 }
172 
173 static int malidp_de_plane_check(struct drm_plane *plane,
174 				 struct drm_plane_state *state)
175 {
176 	struct malidp_plane *mp = to_malidp_plane(plane);
177 	struct malidp_plane_state *ms = to_malidp_plane_state(state);
178 	struct drm_framebuffer *fb;
179 	int i, ret;
180 
181 	if (!state->crtc || !state->fb)
182 		return 0;
183 
184 	fb = state->fb;
185 
186 	ms->format = malidp_hw_get_format_id(&mp->hwdev->hw->map,
187 					     mp->layer->id,
188 					     fb->format->format);
189 	if (ms->format == MALIDP_INVALID_FORMAT_ID)
190 		return -EINVAL;
191 
192 	ms->n_planes = fb->format->num_planes;
193 	for (i = 0; i < ms->n_planes; i++) {
194 		if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
195 			DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
196 				      fb->pitches[i], i);
197 			return -EINVAL;
198 		}
199 	}
200 
201 	if ((state->crtc_w > mp->hwdev->max_line_size) ||
202 	    (state->crtc_h > mp->hwdev->max_line_size) ||
203 	    (state->crtc_w < mp->hwdev->min_line_size) ||
204 	    (state->crtc_h < mp->hwdev->min_line_size))
205 		return -EINVAL;
206 
207 	/*
208 	 * DP550/650 video layers can accept 3 plane formats only if
209 	 * fb->pitches[1] == fb->pitches[2] since they don't have a
210 	 * third plane stride register.
211 	 */
212 	if (ms->n_planes == 3 &&
213 	    !(mp->hwdev->hw->features & MALIDP_DEVICE_LV_HAS_3_STRIDES) &&
214 	    (state->fb->pitches[1] != state->fb->pitches[2]))
215 		return -EINVAL;
216 
217 	ret = malidp_se_check_scaling(mp, state);
218 	if (ret)
219 		return ret;
220 
221 	/* packed RGB888 / BGR888 can't be rotated or flipped */
222 	if (state->rotation != DRM_MODE_ROTATE_0 &&
223 	    (fb->format->format == DRM_FORMAT_RGB888 ||
224 	     fb->format->format == DRM_FORMAT_BGR888))
225 		return -EINVAL;
226 
227 	ms->rotmem_size = 0;
228 	if (state->rotation & MALIDP_ROTATED_MASK) {
229 		int val;
230 
231 		val = mp->hwdev->hw->rotmem_required(mp->hwdev, state->crtc_h,
232 						     state->crtc_w,
233 						     fb->format->format);
234 		if (val < 0)
235 			return val;
236 
237 		ms->rotmem_size = val;
238 	}
239 
240 	return 0;
241 }
242 
243 static void malidp_de_set_plane_pitches(struct malidp_plane *mp,
244 					int num_planes, unsigned int pitches[3])
245 {
246 	int i;
247 	int num_strides = num_planes;
248 
249 	if (!mp->layer->stride_offset)
250 		return;
251 
252 	if (num_planes == 3)
253 		num_strides = (mp->hwdev->hw->features &
254 			       MALIDP_DEVICE_LV_HAS_3_STRIDES) ? 3 : 2;
255 
256 	for (i = 0; i < num_strides; ++i)
257 		malidp_hw_write(mp->hwdev, pitches[i],
258 				mp->layer->base +
259 				mp->layer->stride_offset + i * 4);
260 }
261 
262 static void malidp_de_plane_update(struct drm_plane *plane,
263 				   struct drm_plane_state *old_state)
264 {
265 	struct malidp_plane *mp;
266 	struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
267 	u32 src_w, src_h, dest_w, dest_h, val;
268 	int i;
269 
270 	mp = to_malidp_plane(plane);
271 
272 	/* convert src values from Q16 fixed point to integer */
273 	src_w = plane->state->src_w >> 16;
274 	src_h = plane->state->src_h >> 16;
275 	dest_w = plane->state->crtc_w;
276 	dest_h = plane->state->crtc_h;
277 
278 	malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
279 
280 	for (i = 0; i < ms->n_planes; i++) {
281 		/* calculate the offset for the layer's plane registers */
282 		u16 ptr = mp->layer->ptr + (i << 4);
283 		dma_addr_t fb_addr = drm_fb_cma_get_gem_addr(plane->state->fb,
284 							     plane->state, i);
285 
286 		malidp_hw_write(mp->hwdev, lower_32_bits(fb_addr), ptr);
287 		malidp_hw_write(mp->hwdev, upper_32_bits(fb_addr), ptr + 4);
288 	}
289 	malidp_de_set_plane_pitches(mp, ms->n_planes,
290 				    plane->state->fb->pitches);
291 
292 	malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
293 			mp->layer->base + MALIDP_LAYER_SIZE);
294 
295 	malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
296 			mp->layer->base + MALIDP_LAYER_COMP_SIZE);
297 
298 	malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
299 			LAYER_V_VAL(plane->state->crtc_y),
300 			mp->layer->base + MALIDP_LAYER_OFFSET);
301 
302 	if (mp->layer->id == DE_SMART)
303 		malidp_hw_write(mp->hwdev,
304 				LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
305 				mp->layer->base + MALIDP550_LS_R1_IN_SIZE);
306 
307 	/* first clear the rotation bits */
308 	val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
309 	val &= ~LAYER_ROT_MASK;
310 
311 	/* setup the rotation and axis flip bits */
312 	if (plane->state->rotation & DRM_MODE_ROTATE_MASK)
313 		val |= ilog2(plane->state->rotation & DRM_MODE_ROTATE_MASK) <<
314 		       LAYER_ROT_OFFSET;
315 	if (plane->state->rotation & DRM_MODE_REFLECT_X)
316 		val |= LAYER_H_FLIP;
317 	if (plane->state->rotation & DRM_MODE_REFLECT_Y)
318 		val |= LAYER_V_FLIP;
319 
320 	/*
321 	 * always enable pixel alpha blending until we have a way to change
322 	 * blend modes
323 	 */
324 	val &= ~LAYER_COMP_MASK;
325 	val |= LAYER_COMP_PIXEL;
326 
327 	val &= ~LAYER_FLOWCFG(LAYER_FLOWCFG_MASK);
328 	if (plane->state->crtc) {
329 		struct malidp_crtc_state *m =
330 			to_malidp_crtc_state(plane->state->crtc->state);
331 
332 		if (m->scaler_config.scale_enable &&
333 		    m->scaler_config.plane_src_id == mp->layer->id)
334 			val |= LAYER_FLOWCFG(LAYER_FLOWCFG_SCALE_SE);
335 	}
336 
337 	/* set the 'enable layer' bit */
338 	val |= LAYER_ENABLE;
339 
340 	malidp_hw_write(mp->hwdev, val,
341 			mp->layer->base + MALIDP_LAYER_CONTROL);
342 }
343 
344 static void malidp_de_plane_disable(struct drm_plane *plane,
345 				    struct drm_plane_state *state)
346 {
347 	struct malidp_plane *mp = to_malidp_plane(plane);
348 
349 	malidp_hw_clearbits(mp->hwdev,
350 			    LAYER_ENABLE | LAYER_FLOWCFG(LAYER_FLOWCFG_MASK),
351 			    mp->layer->base + MALIDP_LAYER_CONTROL);
352 }
353 
354 static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
355 	.atomic_check = malidp_de_plane_check,
356 	.atomic_update = malidp_de_plane_update,
357 	.atomic_disable = malidp_de_plane_disable,
358 };
359 
360 int malidp_de_planes_init(struct drm_device *drm)
361 {
362 	struct malidp_drm *malidp = drm->dev_private;
363 	const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
364 	struct malidp_plane *plane = NULL;
365 	enum drm_plane_type plane_type;
366 	unsigned long crtcs = 1 << drm->mode_config.num_crtc;
367 	unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
368 			      DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
369 	u32 *formats;
370 	int ret, i, j, n;
371 
372 	formats = kcalloc(map->n_pixel_formats, sizeof(*formats), GFP_KERNEL);
373 	if (!formats) {
374 		ret = -ENOMEM;
375 		goto cleanup;
376 	}
377 
378 	for (i = 0; i < map->n_layers; i++) {
379 		u8 id = map->layers[i].id;
380 
381 		plane = kzalloc(sizeof(*plane), GFP_KERNEL);
382 		if (!plane) {
383 			ret = -ENOMEM;
384 			goto cleanup;
385 		}
386 
387 		/* build the list of DRM supported formats based on the map */
388 		for (n = 0, j = 0;  j < map->n_pixel_formats; j++) {
389 			if ((map->pixel_formats[j].layer & id) == id)
390 				formats[n++] = map->pixel_formats[j].format;
391 		}
392 
393 		plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
394 					DRM_PLANE_TYPE_OVERLAY;
395 		ret = drm_universal_plane_init(drm, &plane->base, crtcs,
396 					       &malidp_de_plane_funcs, formats,
397 					       n, NULL, plane_type, NULL);
398 		if (ret < 0)
399 			goto cleanup;
400 
401 		drm_plane_helper_add(&plane->base,
402 				     &malidp_de_plane_helper_funcs);
403 		plane->hwdev = malidp->dev;
404 		plane->layer = &map->layers[i];
405 
406 		if (id == DE_SMART) {
407 			/*
408 			 * Enable the first rectangle in the SMART layer to be
409 			 * able to use it as a drm plane.
410 			 */
411 			malidp_hw_write(malidp->dev, 1,
412 					plane->layer->base + MALIDP550_LS_ENABLE);
413 			/* Skip the features which the SMART layer doesn't have. */
414 			continue;
415 		}
416 
417 		drm_plane_create_rotation_property(&plane->base, DRM_MODE_ROTATE_0, flags);
418 		malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
419 				plane->layer->base + MALIDP_LAYER_COMPOSE);
420 	}
421 
422 	kfree(formats);
423 
424 	return 0;
425 
426 cleanup:
427 	malidp_de_planes_destroy(drm);
428 	kfree(formats);
429 
430 	return ret;
431 }
432 
433 void malidp_de_planes_destroy(struct drm_device *drm)
434 {
435 	struct drm_plane *p, *pt;
436 
437 	list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
438 		drm_plane_cleanup(p);
439 		kfree(p);
440 	}
441 }
442