]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/mgag200/mgag200_cursor.c
drm/mgag200: Move cursor-image update to mgag200_show_cursor()
[linux.git] / drivers / gpu / drm / mgag200 / mgag200_cursor.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2013 Matrox Graphics
4  *
5  * Author: Christopher Harvey <charvey@matrox.com>
6  */
7
8 #include <drm/drm_pci.h>
9
10 #include "mgag200_drv.h"
11
12 static bool warn_transparent = true;
13 static bool warn_palette = true;
14
15 static int mgag200_cursor_update(struct mga_device *mdev, void *dst, void *src,
16                                  unsigned int width, unsigned int height)
17 {
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;
23         uint32_t this_colour;
24         bool found = false;
25         int colour_count = 0;
26         u8 reg_index;
27         u8 this_row[48];
28
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);
33                 /* No transparency */
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. */
40                         }
41                         return -EINVAL;
42                 }
43                 /* Don't need to store transparent pixels as colours */
44                 if (this_colour>>24 == 0x0)
45                         continue;
46                 found = false;
47                 for (palette_iter = &colour_set[0]; palette_iter != next_space; palette_iter++) {
48                         if (*palette_iter == this_colour) {
49                                 found = true;
50                                 break;
51                         }
52                 }
53                 if (found)
54                         continue;
55                 /* We only support 4bit paletted cursors */
56                 if (colour_count >= 16) {
57                         if (warn_palette) {
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. */
61                         }
62                         return -EINVAL;
63                 }
64                 *next_space = this_colour;
65                 next_space++;
66                 colour_count++;
67         }
68
69         /* Program colours from cursor icon into palette */
70         for (i = 0; i < colour_count; i++) {
71                 if (i <= 2)
72                         reg_index = 0x8 + i*0x4;
73                 else
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);
79         }
80
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);
89                                 continue;
90                         }
91
92                         /* write colour index here */
93                         for (i = 0; i < colour_count; i++) {
94                                 if (colour_set[i] == this_colour) {
95                                         if (col % 2)
96                                                 this_row[col/2] |= i<<4;
97                                         else
98                                                 this_row[col/2] |= i;
99                                         break;
100                                 }
101                         }
102                 }
103                 memcpy_toio(dst + row*48, &this_row[0], 48);
104         }
105
106         return 0;
107 }
108
109 static void mgag200_cursor_set_base(struct mga_device *mdev, u64 address)
110 {
111         u8 addrl = (address >> 10) & 0xff;
112         u8 addrh = (address >> 18) & 0x3f;
113
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);
117 }
118
119 static int mgag200_show_cursor(struct mga_device *mdev, void *dst, void *src,
120                                unsigned int width, unsigned int height,
121                                u64 dst_gpu)
122 {
123         int ret;
124
125         ret = mgag200_cursor_update(mdev, dst, src, width, height);
126         if (ret)
127                 return ret;
128         mgag200_cursor_set_base(mdev, dst_gpu);
129
130         /* Adjust cursor control register to turn on the cursor */
131         WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */
132
133         return 0;
134 }
135
136 /*
137  * Hide the cursor off screen. We can't disable the cursor hardware because
138  * it takes too long to re-activate and causes momentary corruption.
139  */
140 static void mgag200_hide_cursor(struct mga_device *mdev)
141 {
142         WREG8(MGA_CURPOSXL, 0);
143         WREG8(MGA_CURPOSXH, 0);
144         if (mdev->cursor.pixels_current)
145                 drm_gem_vram_unpin(mdev->cursor.pixels_current);
146         mdev->cursor.pixels_current = NULL;
147 }
148
149 static void mgag200_move_cursor(struct mga_device *mdev, int x, int y)
150 {
151         if (WARN_ON(x <= 0))
152                 return;
153         if (WARN_ON(y <= 0))
154                 return;
155         if (WARN_ON(x & ~0xffff))
156                 return;
157         if (WARN_ON(y & ~0xffff))
158                 return;
159
160         WREG8(MGA_CURPOSXL, x & 0xff);
161         WREG8(MGA_CURPOSXH, (x>>8) & 0xff);
162
163         WREG8(MGA_CURPOSYL, y & 0xff);
164         WREG8(MGA_CURPOSYH, (y>>8) & 0xff);
165 }
166
167 int mgag200_cursor_init(struct mga_device *mdev)
168 {
169         struct drm_device *dev = mdev->dev;
170
171         /*
172          * Make small buffers to store a hardware cursor (double
173          * buffered icon updates)
174          */
175         mdev->cursor.pixels_1 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
176                                                     roundup(48*64, PAGE_SIZE),
177                                                     0, 0);
178         mdev->cursor.pixels_2 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
179                                                     roundup(48*64, PAGE_SIZE),
180                                                     0, 0);
181         if (IS_ERR(mdev->cursor.pixels_2) || IS_ERR(mdev->cursor.pixels_1)) {
182                 mdev->cursor.pixels_1 = NULL;
183                 mdev->cursor.pixels_2 = NULL;
184                 dev_warn(&dev->pdev->dev,
185                         "Could not allocate space for cursors. Not doing hardware cursors.\n");
186         }
187         mdev->cursor.pixels_current = NULL;
188
189         return 0;
190 }
191
192 void mgag200_cursor_fini(struct mga_device *mdev)
193 { }
194
195 int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
196                             uint32_t handle, uint32_t width, uint32_t height)
197 {
198         struct drm_device *dev = crtc->dev;
199         struct mga_device *mdev = (struct mga_device *)dev->dev_private;
200         struct drm_gem_vram_object *pixels_1 = mdev->cursor.pixels_1;
201         struct drm_gem_vram_object *pixels_2 = mdev->cursor.pixels_2;
202         struct drm_gem_vram_object *pixels_current = mdev->cursor.pixels_current;
203         struct drm_gem_vram_object *pixels_next;
204         struct drm_gem_object *obj;
205         struct drm_gem_vram_object *gbo = NULL;
206         int ret;
207         u8 *src, *dst;
208         s64 gpu_addr;
209         u64 dst_gpu;
210
211         if (!pixels_1 || !pixels_2) {
212                 WREG8(MGA_CURPOSXL, 0);
213                 WREG8(MGA_CURPOSXH, 0);
214                 return -ENOTSUPP; /* Didn't allocate space for cursors */
215         }
216
217         if (WARN_ON(pixels_current &&
218                     pixels_1 != pixels_current &&
219                     pixels_2 != pixels_current)) {
220                 return -ENOTSUPP; /* inconsistent state */
221         }
222
223         if (!handle || !file_priv) {
224                 mgag200_hide_cursor(mdev);
225                 return 0;
226         }
227
228         if (width != 64 || height != 64) {
229                 WREG8(MGA_CURPOSXL, 0);
230                 WREG8(MGA_CURPOSXH, 0);
231                 return -EINVAL;
232         }
233
234         if (pixels_current == pixels_1)
235                 pixels_next = pixels_2;
236         else
237                 pixels_next = pixels_1;
238
239         obj = drm_gem_object_lookup(file_priv, handle);
240         if (!obj)
241                 return -ENOENT;
242         gbo = drm_gem_vram_of_gem(obj);
243         src = drm_gem_vram_vmap(gbo);
244         if (IS_ERR(src)) {
245                 ret = PTR_ERR(src);
246                 dev_err(&dev->pdev->dev,
247                         "failed to map user buffer updates\n");
248                 goto err_drm_gem_object_put_unlocked;
249         }
250
251         /* Pin and map up-coming buffer to write colour indices */
252         ret = drm_gem_vram_pin(pixels_next, DRM_GEM_VRAM_PL_FLAG_VRAM);
253         if (ret) {
254                 dev_err(&dev->pdev->dev,
255                         "failed to pin cursor buffer: %d\n", ret);
256                 goto err_drm_gem_vram_vunmap;
257         }
258         dst = drm_gem_vram_kmap(pixels_next, true, NULL);
259         if (IS_ERR(dst)) {
260                 ret = PTR_ERR(dst);
261                 dev_err(&dev->pdev->dev,
262                         "failed to kmap cursor updates: %d\n", ret);
263                 goto err_drm_gem_vram_unpin_dst;
264         }
265         gpu_addr = drm_gem_vram_offset(pixels_next);
266         if (gpu_addr < 0) {
267                 ret = (int)gpu_addr;
268                 dev_err(&dev->pdev->dev,
269                         "failed to get cursor scanout address: %d\n", ret);
270                 goto err_drm_gem_vram_kunmap_dst;
271         }
272         dst_gpu = (u64)gpu_addr;
273
274         ret = mgag200_show_cursor(mdev, dst, src, width, height, dst_gpu);
275         if (ret)
276                 goto err_drm_gem_vram_kunmap_dst;
277
278         /* Now update internal buffer pointers */
279         if (pixels_current)
280                 drm_gem_vram_unpin(pixels_current);
281         mdev->cursor.pixels_current = pixels_next;
282
283         drm_gem_vram_kunmap(pixels_next);
284         drm_gem_vram_vunmap(gbo, src);
285         drm_gem_object_put_unlocked(obj);
286
287         return 0;
288
289 err_drm_gem_vram_kunmap_dst:
290         drm_gem_vram_kunmap(pixels_next);
291 err_drm_gem_vram_unpin_dst:
292         drm_gem_vram_unpin(pixels_next);
293 err_drm_gem_vram_vunmap:
294         drm_gem_vram_vunmap(gbo, src);
295 err_drm_gem_object_put_unlocked:
296         drm_gem_object_put_unlocked(obj);
297         return ret;
298 }
299
300 int mgag200_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
301 {
302         struct mga_device *mdev = (struct mga_device *)crtc->dev->dev_private;
303
304         /* Our origin is at (64,64) */
305         x += 64;
306         y += 64;
307
308         mgag200_move_cursor(mdev, x, y);
309
310         return 0;
311 }