1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2013 Matrox Graphics
5 * Author: Christopher Harvey <charvey@matrox.com>
8 #include <drm/drm_pci.h>
10 #include "mgag200_drv.h"
12 static bool warn_transparent = true;
13 static bool warn_palette = true;
15 static int mgag200_cursor_update(struct mga_device *mdev, void *dst, void *src,
16 unsigned int width, unsigned int height)
18 struct drm_device *dev = mdev->dev;
19 unsigned int i, row, col;
20 uint32_t colour_set[16];
21 uint32_t *next_space = &colour_set[0];
22 uint32_t *palette_iter;
29 memset(&colour_set[0], 0, sizeof(uint32_t)*16);
30 /* width*height*4 = 16384 */
31 for (i = 0; i < 16384; i += 4) {
32 this_colour = ioread32(src + i);
34 if (this_colour>>24 != 0xff &&
35 this_colour>>24 != 0x0) {
36 if (warn_transparent) {
37 dev_info(&dev->pdev->dev, "Video card doesn't support cursors with partial transparency.\n");
38 dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
39 warn_transparent = false; /* Only tell the user once. */
43 /* Don't need to store transparent pixels as colours */
44 if (this_colour>>24 == 0x0)
47 for (palette_iter = &colour_set[0]; palette_iter != next_space; palette_iter++) {
48 if (*palette_iter == this_colour) {
55 /* We only support 4bit paletted cursors */
56 if (colour_count >= 16) {
58 dev_info(&dev->pdev->dev, "Video card only supports cursors with up to 16 colours.\n");
59 dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
60 warn_palette = false; /* Only tell the user once. */
64 *next_space = this_colour;
69 /* Program colours from cursor icon into palette */
70 for (i = 0; i < colour_count; i++) {
72 reg_index = 0x8 + i*0x4;
74 reg_index = 0x60 + i*0x3;
75 WREG_DAC(reg_index, colour_set[i] & 0xff);
76 WREG_DAC(reg_index+1, colour_set[i]>>8 & 0xff);
77 WREG_DAC(reg_index+2, colour_set[i]>>16 & 0xff);
78 BUG_ON((colour_set[i]>>24 & 0xff) != 0xff);
81 /* now write colour indices into hardware cursor buffer */
82 for (row = 0; row < 64; row++) {
83 memset(&this_row[0], 0, 48);
84 for (col = 0; col < 64; col++) {
85 this_colour = ioread32(src + 4*(col + 64*row));
86 /* write transparent pixels */
87 if (this_colour>>24 == 0x0) {
88 this_row[47 - col/8] |= 0x80>>(col%8);
92 /* write colour index here */
93 for (i = 0; i < colour_count; i++) {
94 if (colour_set[i] == this_colour) {
96 this_row[col/2] |= i<<4;
103 memcpy_toio(dst + row*48, &this_row[0], 48);
109 static void mgag200_cursor_set_base(struct mga_device *mdev, u64 address)
111 u8 addrl = (address >> 10) & 0xff;
112 u8 addrh = (address >> 18) & 0x3f;
114 /* Program gpu address of cursor buffer */
115 WREG_DAC(MGA1064_CURSOR_BASE_ADR_LOW, addrl);
116 WREG_DAC(MGA1064_CURSOR_BASE_ADR_HI, addrh);
119 static int mgag200_show_cursor(struct mga_device *mdev, void *src,
120 unsigned int width, unsigned int height)
122 struct drm_device *dev = mdev->dev;
123 struct drm_gem_vram_object *pixels_1 = mdev->cursor.pixels_1;
124 struct drm_gem_vram_object *pixels_2 = mdev->cursor.pixels_2;
125 struct drm_gem_vram_object *pixels_current = mdev->cursor.pixels_current;
126 struct drm_gem_vram_object *pixels_next;
131 if (!pixels_1 || !pixels_2) {
132 WREG8(MGA_CURPOSXL, 0);
133 WREG8(MGA_CURPOSXH, 0);
134 return -ENOTSUPP; /* Didn't allocate space for cursors */
137 if (WARN_ON(pixels_current &&
138 pixels_1 != pixels_current &&
139 pixels_2 != pixels_current)) {
140 return -ENOTSUPP; /* inconsistent state */
143 if (pixels_current == pixels_1)
144 pixels_next = pixels_2;
146 pixels_next = pixels_1;
148 dst = drm_gem_vram_vmap(pixels_next);
151 dev_err(&dev->pdev->dev,
152 "failed to map cursor updates: %d\n", ret);
155 off = drm_gem_vram_offset(pixels_next);
158 dev_err(&dev->pdev->dev,
159 "failed to get cursor scanout address: %d\n", ret);
160 goto err_drm_gem_vram_vunmap;
163 ret = mgag200_cursor_update(mdev, dst, src, width, height);
165 goto err_drm_gem_vram_vunmap;
166 mgag200_cursor_set_base(mdev, off);
168 /* Adjust cursor control register to turn on the cursor */
169 WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */
172 drm_gem_vram_unpin(pixels_current);
173 mdev->cursor.pixels_current = pixels_next;
175 drm_gem_vram_vunmap(pixels_next, dst);
179 err_drm_gem_vram_vunmap:
180 drm_gem_vram_vunmap(pixels_next, dst);
185 * Hide the cursor off screen. We can't disable the cursor hardware because
186 * it takes too long to re-activate and causes momentary corruption.
188 static void mgag200_hide_cursor(struct mga_device *mdev)
190 WREG8(MGA_CURPOSXL, 0);
191 WREG8(MGA_CURPOSXH, 0);
192 if (mdev->cursor.pixels_current)
193 drm_gem_vram_unpin(mdev->cursor.pixels_current);
194 mdev->cursor.pixels_current = NULL;
197 static void mgag200_move_cursor(struct mga_device *mdev, int x, int y)
203 if (WARN_ON(x & ~0xffff))
205 if (WARN_ON(y & ~0xffff))
208 WREG8(MGA_CURPOSXL, x & 0xff);
209 WREG8(MGA_CURPOSXH, (x>>8) & 0xff);
211 WREG8(MGA_CURPOSYL, y & 0xff);
212 WREG8(MGA_CURPOSYH, (y>>8) & 0xff);
215 int mgag200_cursor_init(struct mga_device *mdev)
217 struct drm_device *dev = mdev->dev;
220 * Make small buffers to store a hardware cursor (double
221 * buffered icon updates)
223 mdev->cursor.pixels_1 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
224 roundup(48*64, PAGE_SIZE),
226 mdev->cursor.pixels_2 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
227 roundup(48*64, PAGE_SIZE),
229 if (IS_ERR(mdev->cursor.pixels_2) || IS_ERR(mdev->cursor.pixels_1)) {
230 mdev->cursor.pixels_1 = NULL;
231 mdev->cursor.pixels_2 = NULL;
232 dev_warn(&dev->pdev->dev,
233 "Could not allocate space for cursors. Not doing hardware cursors.\n");
235 mdev->cursor.pixels_current = NULL;
240 void mgag200_cursor_fini(struct mga_device *mdev)
243 int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
244 uint32_t handle, uint32_t width, uint32_t height)
246 struct drm_device *dev = crtc->dev;
247 struct mga_device *mdev = (struct mga_device *)dev->dev_private;
248 struct drm_gem_object *obj;
249 struct drm_gem_vram_object *gbo = NULL;
253 if (!handle || !file_priv) {
254 mgag200_hide_cursor(mdev);
258 if (width != 64 || height != 64) {
259 WREG8(MGA_CURPOSXL, 0);
260 WREG8(MGA_CURPOSXH, 0);
264 obj = drm_gem_object_lookup(file_priv, handle);
267 gbo = drm_gem_vram_of_gem(obj);
268 src = drm_gem_vram_vmap(gbo);
271 dev_err(&dev->pdev->dev,
272 "failed to map user buffer updates\n");
273 goto err_drm_gem_object_put_unlocked;
276 ret = mgag200_show_cursor(mdev, src, width, height);
278 goto err_drm_gem_vram_vunmap;
280 /* Now update internal buffer pointers */
281 drm_gem_vram_vunmap(gbo, src);
282 drm_gem_object_put_unlocked(obj);
285 err_drm_gem_vram_vunmap:
286 drm_gem_vram_vunmap(gbo, src);
287 err_drm_gem_object_put_unlocked:
288 drm_gem_object_put_unlocked(obj);
292 int mgag200_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
294 struct mga_device *mdev = (struct mga_device *)crtc->dev->dev_private;
296 /* Our origin is at (64,64) */
300 mgag200_move_cursor(mdev, x, y);