summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/openchrome/openchrome_cursor.c
blob: 796955dcd209e142c7ee2c681563b664ba706b6c (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Copyright © 2019 Kevin Brace
 * Copyright 2012 James Simmons <jsimmons@infradead.org>. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including
 * the next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
/*
 * Author(s):
 *
 * Kevin Brace <kevinbrace@gmx.com>
 * James Simmons <jsimmons@infradead.org>
 */

#include <linux/pci_ids.h>

#include <drm/drm_crtc.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_mode.h>
#include <drm/drm_modeset_helper_vtables.h>
#include <drm/drm_plane.h>
#include <drm/drm_plane_helper.h>

#include <drm/ttm/ttm_bo_api.h>
#include <drm/ttm/ttm_bo_driver.h>

#include "openchrome_drv.h"


static void openchrome_hide_cursor(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct via_crtc *iga = container_of(crtc,
					struct via_crtc, base);
	struct openchrome_drm_private *dev_private =
					crtc->dev->dev_private;
	uint32_t temp;

	switch (dev->pdev->device) {
	case PCI_DEVICE_ID_VIA_PM800:
	case PCI_DEVICE_ID_VIA_VT3157:
	case PCI_DEVICE_ID_VIA_VT3343:
	case PCI_DEVICE_ID_VIA_P4M900:
	case PCI_DEVICE_ID_VIA_VT1122:
	case PCI_DEVICE_ID_VIA_VX875:
	case PCI_DEVICE_ID_VIA_VX900_VGA:
		if (iga->index) {
			temp = VIA_READ(HI_CONTROL);
			VIA_WRITE(HI_CONTROL, temp & 0xFFFFFFFA);
		} else {
			temp = VIA_READ(PRIM_HI_CTRL);
			VIA_WRITE(PRIM_HI_CTRL, temp & 0xFFFFFFFA);
		}

		break;
	default:
		temp = VIA_READ(HI_CONTROL);
		VIA_WRITE(HI_CONTROL, temp & 0xFFFFFFFA);
		break;
	}
}

static void openchrome_show_cursor(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct via_crtc *iga = container_of(crtc,
					struct via_crtc, base);
	struct openchrome_drm_private *dev_private =
					crtc->dev->dev_private;

	switch (dev->pdev->device) {
	case PCI_DEVICE_ID_VIA_PM800:
	case PCI_DEVICE_ID_VIA_VT3157:
	case PCI_DEVICE_ID_VIA_VT3343:
	case PCI_DEVICE_ID_VIA_P4M900:
	case PCI_DEVICE_ID_VIA_VT1122:
	case PCI_DEVICE_ID_VIA_VX875:
	case PCI_DEVICE_ID_VIA_VX900_VGA:
		/* Program Hardware Icon (HI) FIFO, foreground, and
		 * background colors. */
		if (iga->index) {
			VIA_WRITE(HI_TRANSPARENT_COLOR, 0x00000000);
			VIA_WRITE(HI_INVTCOLOR, 0x00FFFFFF);
			VIA_WRITE(ALPHA_V3_PREFIFO_CONTROL,
							0x000E0000);
			VIA_WRITE(ALPHA_V3_FIFO_CONTROL, 0x0E0F0000);
		} else {
			VIA_WRITE(PRIM_HI_TRANSCOLOR, 0x00000000);
			VIA_WRITE(PRIM_HI_INVTCOLOR, 0x00FFFFFF);
			VIA_WRITE(V327_HI_INVTCOLOR, 0x00FFFFFF);
			VIA_WRITE(PRIM_HI_FIFO, 0x0D000D0F);
		}

		break;
	default:
		VIA_WRITE(HI_TRANSPARENT_COLOR, 0x00000000);
		VIA_WRITE(HI_INVTCOLOR, 0x00FFFFFF);
		VIA_WRITE(ALPHA_V3_PREFIFO_CONTROL, 0x000E0000);
		VIA_WRITE(ALPHA_V3_FIFO_CONTROL, 0xE0F0000);
		break;
	}

	switch (dev->pdev->device) {
	case PCI_DEVICE_ID_VIA_PM800:
	case PCI_DEVICE_ID_VIA_VT3157:
	case PCI_DEVICE_ID_VIA_VT3343:
	case PCI_DEVICE_ID_VIA_P4M900:
	case PCI_DEVICE_ID_VIA_VT1122:
	case PCI_DEVICE_ID_VIA_VX875:
	case PCI_DEVICE_ID_VIA_VX900_VGA:
		/* Turn on Hardware icon Cursor */
		if (iga->index) {
			VIA_WRITE(HI_CONTROL, 0xB6000005);
		} else {
			VIA_WRITE(PRIM_HI_CTRL, 0x36000005);
		}

		break;
	default:
		if (iga->index) {
			VIA_WRITE(HI_CONTROL, 0xB6000005);
		} else {
			VIA_WRITE(HI_CONTROL, 0x36000005);
		}

		break;
	}
}

static void openchrome_cursor_address(struct drm_crtc *crtc,
					struct openchrome_bo *ttm_bo)
{
	struct drm_device *dev = crtc->dev;
	struct via_crtc *iga = container_of(crtc,
					struct via_crtc, base);
	struct openchrome_drm_private *dev_private =
					crtc->dev->dev_private;
	int ret;

	ret = ttm_bo_kmap(&ttm_bo->ttm_bo, 0,
				ttm_bo->ttm_bo.num_pages,
				&ttm_bo->kmap);
	if (ret) {
		goto exit;
	}

	switch (dev->pdev->device) {
	case PCI_DEVICE_ID_VIA_PM800:
	case PCI_DEVICE_ID_VIA_VT3157:
	case PCI_DEVICE_ID_VIA_VT3343:
	case PCI_DEVICE_ID_VIA_P4M900:
	case PCI_DEVICE_ID_VIA_VT1122:
	case PCI_DEVICE_ID_VIA_VX875:
	case PCI_DEVICE_ID_VIA_VX900_VGA:
		/* Program the HI offset. */
		if (iga->index) {
			VIA_WRITE(HI_FBOFFSET,
					ttm_bo->kmap.bo->offset);
		} else {
			VIA_WRITE(PRIM_HI_FBOFFSET,
					ttm_bo->kmap.bo->offset);
		}
		break;
	default:
		VIA_WRITE(HI_FBOFFSET,
				ttm_bo->kmap.bo->offset);
		break;
	}

	ttm_bo_kunmap(&ttm_bo->kmap);
exit:
	return;
}

static void openchrome_set_hi_location(struct drm_crtc *crtc,
					int crtc_x,
					int crtc_y)
{
	struct drm_device *dev = crtc->dev;
	struct via_crtc *iga = container_of(crtc,
					struct via_crtc, base);
	struct openchrome_drm_private *dev_private =
					crtc->dev->dev_private;
	uint32_t location_x = 0, location_y = 0;
	uint32_t offset_x = 0, offset_y = 0;

	if (crtc_x < 0) {
		offset_x = -crtc_x;
	} else {
		location_x = crtc_x;
	}

	if (crtc_y < 0) {
		offset_y = -crtc_y;
	} else {
		location_y = crtc_y;
	}

	switch (dev->pdev->device) {
	case PCI_DEVICE_ID_VIA_PM800:
	case PCI_DEVICE_ID_VIA_VT3157:
	case PCI_DEVICE_ID_VIA_VT3343:
	case PCI_DEVICE_ID_VIA_P4M900:
	case PCI_DEVICE_ID_VIA_VT1122:
	case PCI_DEVICE_ID_VIA_VX875:
	case PCI_DEVICE_ID_VIA_VX900_VGA:
		if (iga->index) {
			VIA_WRITE(HI_POSSTART,
				(((location_x & 0x07ff) << 16) |
				(location_y & 0x07ff)));
			VIA_WRITE(HI_CENTEROFFSET,
				(((offset_x & 0x07ff) << 16) |
				(offset_y & 0x07ff)));
		} else {
			VIA_WRITE(PRIM_HI_POSSTART,
				(((location_x & 0x07ff) << 16) |
				(location_y & 0x07ff)));
			VIA_WRITE(PRIM_HI_CENTEROFFSET,
				(((offset_x & 0x07ff) << 16) |
				(offset_y & 0x07ff)));
		}

		break;
	default:
		VIA_WRITE(HI_POSSTART,
				(((location_x & 0x07ff) << 16) |
				(location_y & 0x07ff)));
		VIA_WRITE(HI_CENTEROFFSET,
				(((offset_x & 0x07ff) << 16) |
				(offset_y & 0x07ff)));
		break;
	}
}

static int openchrome_cursor_update_plane(struct drm_plane *plane,
				struct drm_crtc *crtc,
				struct drm_framebuffer *fb,
				int crtc_x, int crtc_y,
				unsigned int crtc_w,
				unsigned int crtc_h,
				uint32_t src_x, uint32_t src_y,
				uint32_t src_w, uint32_t src_h,
				struct drm_modeset_acquire_ctx *ctx)
{
	struct drm_device *dev = plane->dev;
	struct via_framebuffer *via_fb;
	struct openchrome_bo *ttm_bo;
	struct drm_gem_object *gem;
	int ret = 0;

	if (!crtc) {
		DRM_ERROR("Invalid CRTC!\n");
		ret = -EINVAL;
		goto exit;
	}

	if (!crtc->enabled) {
		DRM_ERROR("CRTC is currently disabled!\n");
		ret = -EINVAL;
		goto exit;
	}

	if (!fb) {
		DRM_ERROR("Invalid frame buffer!\n");
		ret = -EINVAL;
		goto exit;
	}

	if ((dev->pdev->device == PCI_DEVICE_ID_VIA_CLE266) ||
		(dev->pdev->device == PCI_DEVICE_ID_VIA_KM400)) {
		if ((fb->width != OPENCHROME_UNICHROME_CURSOR_SIZE) ||
		(fb->height != OPENCHROME_UNICHROME_CURSOR_SIZE)) {
			DRM_ERROR("Invalid cursor dimensions.\n");
			ret = -EINVAL;
			goto exit;
		}

	} else {
		if ((fb->width != OPENCHROME_UNICHROME_PRO_CURSOR_SIZE) ||
		(fb->height != OPENCHROME_UNICHROME_PRO_CURSOR_SIZE)) {
			DRM_ERROR("Invalid cursor dimensions.\n");
			ret = -EINVAL;
			goto exit;
		}
	}

	if (fb->width != fb->height) {
		DRM_ERROR("Hardware cursor is expected to have "
				"square dimensions.\n");
		ret = -EINVAL;
		goto exit;
	}

	if (fb != crtc->cursor->fb) {
		via_fb = container_of(fb, struct via_framebuffer, fb);
		gem = via_fb->gem;
		ttm_bo = container_of(gem, struct openchrome_bo, gem);
		openchrome_cursor_address(crtc, ttm_bo);
	} else {
		crtc->cursor_x = crtc_x;
		crtc->cursor_y = crtc_y;
		openchrome_set_hi_location(crtc, crtc_x, crtc_y);
	}

	openchrome_show_cursor(crtc);
exit:
	return ret;
}

static int openchrome_cursor_disable_plane(struct drm_plane *plane,
				struct drm_modeset_acquire_ctx *ctx)
{
	if (plane->crtc) {
		openchrome_hide_cursor(plane->crtc);
	}

	if (plane->fb) {
		drm_framebuffer_put(plane->fb);
		plane->fb = NULL;
	}

	return 0;
}

static void openchrome_cursor_destroy(struct drm_plane *plane)
{
	if (plane->crtc) {
		openchrome_hide_cursor(plane->crtc);
	}

	drm_plane_cleanup(plane);
}

const struct drm_plane_funcs openchrome_cursor_drm_plane_funcs = {
	.update_plane	= openchrome_cursor_update_plane,
	.disable_plane	= openchrome_cursor_disable_plane,
	.destroy	= openchrome_cursor_destroy,
};

const uint32_t openchrome_cursor_formats[] = {
	DRM_FORMAT_ARGB8888,
};

const unsigned int openchrome_cursor_formats_size =
				ARRAY_SIZE(openchrome_cursor_formats);