summaryrefslogtreecommitdiff
path: root/driver/product/kernel/drivers/gpu/drm/pl111/pl111_drm_cursor.c
blob: 4bf20fec2464045d3b2968bbe3765364c889af31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 *
 * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * A copy of the licence is included with the program, and can also be obtained
 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */



/**
 * pl111_drm_cursor.c
 * Implementation of cursor functions for PL111 DRM
 */
#include <linux/amba/bus.h>
#include <linux/amba/clcd.h>
#include <linux/version.h>
#include <linux/shmem_fs.h>
#include <linux/dma-buf.h>
#include <linux/module.h>

#include <drm/drmP.h>
#include <drm/drm_crtc_helper.h>
#include "pl111_clcd_ext.h"
#include "pl111_drm.h"

#define PL111_MAX_CURSOR_WIDTH (64)
#define PL111_MAX_CURSOR_HEIGHT (64)

#define ARGB_2_LBBP_BINARY_THRESHOLD	(1 << 7)
#define ARGB_ALPHA_SHIFT		24
#define ARGB_ALPHA_MASK			(0xff << ARGB_ALPHA_SHIFT)
#define ARGB_RED_SHIFT			16
#define ARGB_RED_MASK			(0xff << ARGB_RED_SHIFT)
#define ARGB_GREEN_SHIFT		8
#define ARGB_GREEN_MASK			(0xff << ARGB_GREEN_SHIFT)
#define ARGB_BLUE_SHIFT			0
#define ARGB_BLUE_MASK			(0xff << ARGB_BLUE_SHIFT)


void pl111_set_cursor_size(enum pl111_cursor_size size)
{
	u32 reg_data = readl(priv.regs + CLCD_CRSR_CONFIG);

	if (size == CURSOR_64X64)
		reg_data |= CRSR_CONFIG_CRSR_SIZE;
	else
		reg_data &= ~CRSR_CONFIG_CRSR_SIZE;

	writel(reg_data, priv.regs + CLCD_CRSR_CONFIG);
}

void pl111_set_cursor_sync(enum pl111_cursor_sync sync)
{
	u32 reg_data = readl(priv.regs + CLCD_CRSR_CONFIG);

	if (sync == CURSOR_SYNC_VSYNC)
		reg_data |= CRSR_CONFIG_CRSR_FRAME_SYNC;
	else
		reg_data &= ~CRSR_CONFIG_CRSR_FRAME_SYNC;

	writel(reg_data, priv.regs + CLCD_CRSR_CONFIG);
}

void pl111_set_cursor(u32 cursor)
{
	u32 reg_data = readl(priv.regs + CLCD_CRSR_CTRL);

	reg_data &= ~(CRSR_CTRL_CRSR_MAX << CRSR_CTRL_CRSR_NUM_SHIFT);
	reg_data |= (cursor & CRSR_CTRL_CRSR_MAX) << CRSR_CTRL_CRSR_NUM_SHIFT;

	writel(reg_data, priv.regs + CLCD_CRSR_CTRL);
}

void pl111_set_cursor_enable(bool enable)
{
	u32 reg_data = readl(priv.regs + CLCD_CRSR_CTRL);

	if (enable)
		reg_data |= CRSR_CTRL_CRSR_ON;
	else
		reg_data &= ~CRSR_CTRL_CRSR_ON;

	writel(reg_data, priv.regs + CLCD_CRSR_CTRL);
}

void pl111_set_cursor_position(u32 x, u32 y)
{
	u32 reg_data = (x & CRSR_XY_MASK) |
			((y & CRSR_XY_MASK) << CRSR_XY_Y_SHIFT);

	writel(reg_data, priv.regs + CLCD_CRSR_XY);
}

void pl111_set_cursor_clipping(u32 x, u32 y)
{
	u32 reg_data;

	/* 
	 * Do not allow setting clipping values larger than
	 * the cursor size since the cursor is already fully hidden
	 * when x,y = PL111_MAX_CURSOR_WIDTH.
	 */
	if (x > PL111_MAX_CURSOR_WIDTH)
		x = PL111_MAX_CURSOR_WIDTH;
	if (y > PL111_MAX_CURSOR_WIDTH)
		y = PL111_MAX_CURSOR_WIDTH;

	reg_data = (x & CRSR_CLIP_MASK) |
			((y & CRSR_CLIP_MASK) << CRSR_CLIP_Y_SHIFT);

	writel(reg_data, priv.regs + CLCD_CRSR_CLIP);
}

void pl111_set_cursor_palette(u32 color0, u32 color1)
{
	writel(color0 & CRSR_PALETTE_MASK, priv.regs + CLCD_CRSR_PALETTE_0);
	writel(color1 & CRSR_PALETTE_MASK, priv.regs + CLCD_CRSR_PALETTE_1);
}

void pl111_cursor_enable(void)
{
	pl111_set_cursor_sync(CURSOR_SYNC_VSYNC);
	pl111_set_cursor_size(CURSOR_64X64);
	pl111_set_cursor_palette(0x0, 0x00ffffff);
	pl111_set_cursor_enable(true);
}

void pl111_cursor_disable(void)
{
	pl111_set_cursor_enable(false);
}

/* shift required to locate pixel into the correct position in
 * a cursor LBBP word, indexed by x mod 16.
 */
static const unsigned char
x_mod_16_to_value_shift[CLCD_CRSR_IMAGE_PIXELS_PER_WORD] = {
	6, 4, 2, 0, 14, 12, 10, 8, 22, 20, 18, 16, 30, 28, 26, 24
};

/* Pack the pixel value into its correct position in the buffer as specified
 * for LBBP */
static inline void
set_lbbp_pixel(uint32_t *buffer, unsigned int x, unsigned int y,
				uint32_t value)
{
	u32 *cursor_ram = priv.regs + CLCD_CRSR_IMAGE;
	uint32_t shift;
	uint32_t data;

	shift = x_mod_16_to_value_shift[x % CLCD_CRSR_IMAGE_PIXELS_PER_WORD];

	/* Get the word containing this pixel */
	cursor_ram = cursor_ram + (x >> CLCD_CRSR_IMAGE_WORDS_PER_LINE) + (y << 2);

	/* Update pixel in cursor RAM */
	data = readl(cursor_ram);
	data &= ~(CLCD_CRSR_LBBP_COLOR_MASK << shift);
	data |= value << shift;
	writel(data, cursor_ram);
}

static u32 pl111_argb_to_lbbp(u32 argb_pix)
{
	u32 lbbp_pix = CLCD_CRSR_LBBP_TRANSPARENT;
	u32 alpha = (argb_pix & ARGB_ALPHA_MASK) >> ARGB_ALPHA_SHIFT;
	u32 red = (argb_pix & ARGB_RED_MASK) >> ARGB_RED_SHIFT;
	u32 green = (argb_pix & ARGB_GREEN_MASK) >> ARGB_GREEN_SHIFT;
	u32 blue =  (argb_pix & ARGB_BLUE_MASK) >> ARGB_BLUE_SHIFT;

	/*
	 * Converting from 8 pixel transparency to binary transparency
	 * it's the best we can achieve.
	 */
	if (alpha & ARGB_2_LBBP_BINARY_THRESHOLD) {
		u32 gray, max, min;

		/*
		 * Convert to gray using the lightness method:
		 * gray = [max(R,G,B) + min(R,G,B)]/2
		 */
		min = min(red, green);
		min = min(min, blue);
		max = max(red, green);
		max = max(max, blue);
		gray = (min + max) >> 1; /* divide by 2 */
		/* Apply binary threshold to the gray value calculated */
		if (gray & ARGB_2_LBBP_BINARY_THRESHOLD)
			lbbp_pix = CLCD_CRSR_LBBP_FOREGROUND;
		else
			lbbp_pix = CLCD_CRSR_LBBP_BACKGROUND;
	}

	return lbbp_pix;
}

/*
 * The PL111 hardware cursor supports only LBBP which is a 2bpp format but
 * the cursor format from userspace is ARGB8888 so we need to convert
 *  to LBBP here.
 */
static void pl111_set_cursor_image(u32 *data)
{
#ifdef ARGB_LBBP_CONVERSION_DEBUG
	/* Add 1 on width to insert trailing NULL */
	char string_cursor[PL111_MAX_CURSOR_WIDTH + 1];
#endif /* ARGB_LBBP_CONVERSION_DEBUG */
	unsigned int x;
	unsigned int y;

	for (y = 0; y < PL111_MAX_CURSOR_HEIGHT; y++) {
		for (x = 0; x < PL111_MAX_CURSOR_WIDTH; x++) {
			u32 value = pl111_argb_to_lbbp(*data);

#ifdef ARGB_LBBP_CONVERSION_DEBUG
			if (value == CLCD_CRSR_LBBP_TRANSPARENT)
				string_cursor[x] = 'T';
			else if (value == CLCD_CRSR_LBBP_FOREGROUND)
				string_cursor[x] = 'F';
			else if (value == CLCD_CRSR_LBBP_INVERSE)
				string_cursor[x] = 'I';
			else
				string_cursor[x] = 'B';

#endif /* ARGB_LBBP_CONVERSION_DEBUG */
			set_lbbp_pixel(data, x, y, value);
			++data;
		}
#ifdef ARGB_LBBP_CONVERSION_DEBUG
		string_cursor[PL111_MAX_CURSOR_WIDTH] = '\0';
		DRM_INFO("%s\n", string_cursor);
#endif /* ARGB_LBBP_CONVERSION_DEBUG */
	}
}

int pl111_crtc_cursor_set(struct drm_crtc *crtc,
			   struct drm_file *file_priv,
			   uint32_t handle,
			   uint32_t width,
			   uint32_t height)
{
	struct drm_gem_object *obj;
	struct pl111_gem_bo *bo;

	DRM_DEBUG_KMS("handle = %u, width = %u, height = %u\n",
		handle, width, height);

	if (!handle) {
		pl111_cursor_disable();
		return 0;
	}

	if ((width != PL111_MAX_CURSOR_WIDTH) ||
	    (height != PL111_MAX_CURSOR_HEIGHT))
		return -EINVAL;

	obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
	if (!obj) {
		DRM_ERROR("Cannot find cursor object for handle = %d\n",
			  handle);
		return -ENOENT;
	}

	/*
	 * We expect a PL111_MAX_CURSOR_WIDTH x PL111_MAX_CURSOR_HEIGHT
	 * ARGB888 buffer object in the input.
	 *
	 */
	if (obj->size < (PL111_MAX_CURSOR_WIDTH * PL111_MAX_CURSOR_HEIGHT * 4)) {
		DRM_ERROR("Cannot set cursor with an obj size = %d\n",
			  obj->size);
		drm_gem_object_unreference_unlocked(obj);
		return -EINVAL;
	}

	bo = PL111_BO_FROM_GEM(obj);
	if (!(bo->type & PL111_BOT_DMA)) {
		DRM_ERROR("Tried to set cursor with non DMA backed obj = %p\n",
			  obj);
		drm_gem_object_unreference_unlocked(obj);
		return -EINVAL;
	}

	pl111_set_cursor_image(bo->backing_data.dma.fb_cpu_addr);

	/*
	 * Since we copy the contents of the buffer to the HW cursor internal
	 * memory this GEM object is not needed anymore.
	 */
	drm_gem_object_unreference_unlocked(obj);

	pl111_cursor_enable();

	return 0;
}

int pl111_crtc_cursor_move(struct drm_crtc *crtc,
			    int x, int y)
{
	int x_clip = 0;
	int y_clip = 0;

	DRM_DEBUG("x %d y %d\n", x, y);

	/*
	 * The cursor image is clipped automatically at the screen limits when
	 * it extends beyond the screen image to the right or bottom but
	 * we must clip it using pl111 HW features for negative values.
	 */
	if (x < 0) {
		x_clip = -x;
		x = 0;
	}
	if (y < 0) {
		y_clip = -y;
		y = 0;
	}

	pl111_set_cursor_clipping(x_clip, y_clip);
	pl111_set_cursor_position(x, y);

	return 0;
}