Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/mgag200: Hardware cursor support

G200 cards support, at best, 16 colour palleted images for the cursor
so we do a conversion in the cursor_set function, and reject cursors
with more than 16 colours, or cursors with partial transparency. Xorg
falls back gracefully to software cursors in this case.

We can't disable/enable the cursor hardware without causing momentary
corruption around the cursor. Instead, once the cursor is on we leave
it on, and simulate turning the cursor off by moving it
offscreen. This works well.

Since we can't disable -> update -> enable the cursors, we double
buffer cursor icons, then just move the base address that points to
the old cursor, to the new. This also works well, but uses an extra
page of memory.

The cursor buffers are lazily-allocated on first cursor_set. This is
to make sure they don't take priority over any framebuffers in case of
limited memory.

Here is a representation of how the bitmap for the cursor is mapped in G200 memory :

Each line of color cursor use 6 Slices of 8 bytes. Slices 0 to 3
are used for the 4bpp bitmap, slice 4 for XOR mask and slice 5 for
AND mask. Each line has the following format:

// Byte 0 Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6 Byte 7
//
// S0: P00-01 P02-03 P04-05 P06-07 P08-09 P10-11 P12-13 P14-15
// S1: P16-17 P18-19 P20-21 P22-23 P24-25 P26-27 P28-29 P30-31
// S2: P32-33 P34-35 P36-37 P38-39 P40-41 P42-43 P44-45 P46-47
// S3: P48-49 P50-51 P52-53 P54-55 P56-57 P58-59 P60-61 P62-63
// S4: X63-56 X55-48 X47-40 X39-32 X31-24 X23-16 X15-08 X07-00
// S5: A63-56 A55-48 A47-40 A39-32 A31-24 A23-16 A15-08 A07-00
//
// S0 to S5 = Slices 0 to 5
// P00 to P63 = Bitmap - pixels 0 to 63
// X00 to X63 = always 0 - pixels 0 to 63
// A00 to A63 = transparent markers - pixels 0 to 63
// 1 means colour, 0 means transparent

Signed-off-by: Christopher Harvey <charvey@matrox.com>
Signed-off-by: Mathieu Larouche <mathieu.larouche@matrox.com>
Acked-by: Julia Lemire <jlemire@matrox.com>
Tested-by: Julia Lemire <jlemire@matrox.com>
Signed-off-by: Dave Airlie <airlied@gmail.com>

authored by

Christopher Harvey and committed by
Dave Airlie
a080db9f fb85ac4d

+324 -3
+1 -1
drivers/gpu/drm/mgag200/Makefile
··· 1 1 ccflags-y := -Iinclude/drm 2 - mgag200-y := mgag200_main.o mgag200_mode.o \ 2 + mgag200-y := mgag200_main.o mgag200_mode.o mgag200_cursor.o \ 3 3 mgag200_drv.o mgag200_fb.o mgag200_i2c.o mgag200_ttm.o 4 4 5 5 obj-$(CONFIG_DRM_MGAG200) += mgag200.o
+275
drivers/gpu/drm/mgag200/mgag200_cursor.c
··· 1 + /* 2 + * Copyright 2013 Matrox Graphics 3 + * 4 + * This file is subject to the terms and conditions of the GNU General 5 + * Public License version 2. See the file COPYING in the main 6 + * directory of this archive for more details. 7 + * 8 + * Author: Christopher Harvey <charvey@matrox.com> 9 + */ 10 + 11 + #include <drm/drmP.h> 12 + #include "mgag200_drv.h" 13 + 14 + static bool warn_transparent = true; 15 + static bool warn_palette = true; 16 + 17 + /* 18 + Hide the cursor off screen. We can't disable the cursor hardware because it 19 + takes too long to re-activate and causes momentary corruption 20 + */ 21 + static void mga_hide_cursor(struct mga_device *mdev) 22 + { 23 + WREG8(MGA_CURPOSXL, 0); 24 + WREG8(MGA_CURPOSXH, 0); 25 + mgag200_bo_unpin(mdev->cursor.pixels_1); 26 + mgag200_bo_unpin(mdev->cursor.pixels_2); 27 + } 28 + 29 + int mga_crtc_cursor_set(struct drm_crtc *crtc, 30 + struct drm_file *file_priv, 31 + uint32_t handle, 32 + uint32_t width, 33 + uint32_t height) 34 + { 35 + struct drm_device *dev = (struct drm_device *)file_priv->minor->dev; 36 + struct mga_device *mdev = (struct mga_device *)dev->dev_private; 37 + struct mgag200_bo *pixels_1 = mdev->cursor.pixels_1; 38 + struct mgag200_bo *pixels_2 = mdev->cursor.pixels_2; 39 + struct mgag200_bo *pixels_current = mdev->cursor.pixels_current; 40 + struct mgag200_bo *pixels_prev = mdev->cursor.pixels_prev; 41 + struct drm_gem_object *obj; 42 + struct mgag200_bo *bo = NULL; 43 + int ret = 0; 44 + unsigned int i, row, col; 45 + uint32_t colour_set[16]; 46 + uint32_t *next_space = &colour_set[0]; 47 + uint32_t *palette_iter; 48 + uint32_t this_colour; 49 + bool found = false; 50 + int colour_count = 0; 51 + u64 gpu_addr; 52 + u8 reg_index; 53 + u8 this_row[48]; 54 + 55 + if (!pixels_1 || !pixels_2) { 56 + WREG8(MGA_CURPOSXL, 0); 57 + WREG8(MGA_CURPOSXH, 0); 58 + return -ENOTSUPP; /* Didn't allocate space for cursors */ 59 + } 60 + 61 + if ((width != 64 || height != 64) && handle) { 62 + WREG8(MGA_CURPOSXL, 0); 63 + WREG8(MGA_CURPOSXH, 0); 64 + return -EINVAL; 65 + } 66 + 67 + BUG_ON(pixels_1 != pixels_current && pixels_1 != pixels_prev); 68 + BUG_ON(pixels_2 != pixels_current && pixels_2 != pixels_prev); 69 + BUG_ON(pixels_current == pixels_prev); 70 + 71 + ret = mgag200_bo_reserve(pixels_1, true); 72 + if (ret) { 73 + WREG8(MGA_CURPOSXL, 0); 74 + WREG8(MGA_CURPOSXH, 0); 75 + return ret; 76 + } 77 + ret = mgag200_bo_reserve(pixels_2, true); 78 + if (ret) { 79 + WREG8(MGA_CURPOSXL, 0); 80 + WREG8(MGA_CURPOSXH, 0); 81 + mgag200_bo_unreserve(pixels_1); 82 + return ret; 83 + } 84 + 85 + if (!handle) { 86 + mga_hide_cursor(mdev); 87 + ret = 0; 88 + goto out1; 89 + } 90 + 91 + /* Move cursor buffers into VRAM if they aren't already */ 92 + if (!pixels_1->pin_count) { 93 + ret = mgag200_bo_pin(pixels_1, TTM_PL_FLAG_VRAM, 94 + &mdev->cursor.pixels_1_gpu_addr); 95 + if (ret) 96 + goto out1; 97 + } 98 + if (!pixels_2->pin_count) { 99 + ret = mgag200_bo_pin(pixels_2, TTM_PL_FLAG_VRAM, 100 + &mdev->cursor.pixels_2_gpu_addr); 101 + if (ret) { 102 + mgag200_bo_unpin(pixels_1); 103 + goto out1; 104 + } 105 + } 106 + 107 + mutex_lock(&dev->struct_mutex); 108 + obj = drm_gem_object_lookup(dev, file_priv, handle); 109 + if (!obj) { 110 + mutex_unlock(&dev->struct_mutex); 111 + ret = -ENOENT; 112 + goto out1; 113 + } 114 + drm_gem_object_unreference(obj); 115 + mutex_unlock(&dev->struct_mutex); 116 + 117 + bo = gem_to_mga_bo(obj); 118 + ret = mgag200_bo_reserve(bo, true); 119 + if (ret) { 120 + dev_err(&dev->pdev->dev, "failed to reserve user bo\n"); 121 + goto out1; 122 + } 123 + if (!bo->kmap.virtual) { 124 + ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap); 125 + if (ret) { 126 + dev_err(&dev->pdev->dev, "failed to kmap user buffer updates\n"); 127 + goto out2; 128 + } 129 + } 130 + 131 + memset(&colour_set[0], 0, sizeof(uint32_t)*16); 132 + /* width*height*4 = 16384 */ 133 + for (i = 0; i < 16384; i += 4) { 134 + this_colour = ioread32(bo->kmap.virtual + i); 135 + /* No transparency */ 136 + if (this_colour>>24 != 0xff && 137 + this_colour>>24 != 0x0) { 138 + if (warn_transparent) { 139 + dev_info(&dev->pdev->dev, "Video card doesn't support cursors with partial transparency.\n"); 140 + dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n"); 141 + warn_transparent = false; /* Only tell the user once. */ 142 + } 143 + ret = -EINVAL; 144 + goto out3; 145 + } 146 + /* Don't need to store transparent pixels as colours */ 147 + if (this_colour>>24 == 0x0) 148 + continue; 149 + found = false; 150 + for (palette_iter = &colour_set[0]; palette_iter != next_space; palette_iter++) { 151 + if (*palette_iter == this_colour) { 152 + found = true; 153 + break; 154 + } 155 + } 156 + if (found) 157 + continue; 158 + /* We only support 4bit paletted cursors */ 159 + if (colour_count >= 16) { 160 + if (warn_palette) { 161 + dev_info(&dev->pdev->dev, "Video card only supports cursors with up to 16 colours.\n"); 162 + dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n"); 163 + warn_palette = false; /* Only tell the user once. */ 164 + } 165 + ret = -EINVAL; 166 + goto out3; 167 + } 168 + *next_space = this_colour; 169 + next_space++; 170 + colour_count++; 171 + } 172 + 173 + /* Program colours from cursor icon into palette */ 174 + for (i = 0; i < colour_count; i++) { 175 + if (i <= 2) 176 + reg_index = 0x8 + i*0x4; 177 + else 178 + reg_index = 0x60 + i*0x3; 179 + WREG_DAC(reg_index, colour_set[i] & 0xff); 180 + WREG_DAC(reg_index+1, colour_set[i]>>8 & 0xff); 181 + WREG_DAC(reg_index+2, colour_set[i]>>16 & 0xff); 182 + BUG_ON((colour_set[i]>>24 & 0xff) != 0xff); 183 + } 184 + 185 + /* Map up-coming buffer to write colour indices */ 186 + if (!pixels_prev->kmap.virtual) { 187 + ret = ttm_bo_kmap(&pixels_prev->bo, 0, 188 + pixels_prev->bo.num_pages, 189 + &pixels_prev->kmap); 190 + if (ret) { 191 + dev_err(&dev->pdev->dev, "failed to kmap cursor updates\n"); 192 + goto out3; 193 + } 194 + } 195 + 196 + /* now write colour indices into hardware cursor buffer */ 197 + for (row = 0; row < 64; row++) { 198 + memset(&this_row[0], 0, 48); 199 + for (col = 0; col < 64; col++) { 200 + this_colour = ioread32(bo->kmap.virtual + 4*(col + 64*row)); 201 + /* write transparent pixels */ 202 + if (this_colour>>24 == 0x0) { 203 + this_row[47 - col/8] |= 0x80>>(col%8); 204 + continue; 205 + } 206 + 207 + /* write colour index here */ 208 + for (i = 0; i < colour_count; i++) { 209 + if (colour_set[i] == this_colour) { 210 + if (col % 2) 211 + this_row[col/2] |= i<<4; 212 + else 213 + this_row[col/2] |= i; 214 + break; 215 + } 216 + } 217 + } 218 + memcpy_toio(pixels_prev->kmap.virtual + row*48, &this_row[0], 48); 219 + } 220 + 221 + /* Program gpu address of cursor buffer */ 222 + if (pixels_prev == pixels_1) 223 + gpu_addr = mdev->cursor.pixels_1_gpu_addr; 224 + else 225 + gpu_addr = mdev->cursor.pixels_2_gpu_addr; 226 + WREG_DAC(MGA1064_CURSOR_BASE_ADR_LOW, (u8)((gpu_addr>>10) & 0xff)); 227 + WREG_DAC(MGA1064_CURSOR_BASE_ADR_HI, (u8)((gpu_addr>>18) & 0x3f)); 228 + 229 + /* Adjust cursor control register to turn on the cursor */ 230 + WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */ 231 + 232 + /* Now swap internal buffer pointers */ 233 + if (mdev->cursor.pixels_1 == mdev->cursor.pixels_prev) { 234 + mdev->cursor.pixels_prev = mdev->cursor.pixels_2; 235 + mdev->cursor.pixels_current = mdev->cursor.pixels_1; 236 + } else if (mdev->cursor.pixels_1 == mdev->cursor.pixels_current) { 237 + mdev->cursor.pixels_prev = mdev->cursor.pixels_1; 238 + mdev->cursor.pixels_current = mdev->cursor.pixels_2; 239 + } else { 240 + BUG(); 241 + } 242 + ret = 0; 243 + 244 + ttm_bo_kunmap(&pixels_prev->kmap); 245 + out3: 246 + ttm_bo_kunmap(&bo->kmap); 247 + out2: 248 + mgag200_bo_unreserve(bo); 249 + out1: 250 + if (ret) 251 + mga_hide_cursor(mdev); 252 + mgag200_bo_unreserve(pixels_1); 253 + mgag200_bo_unreserve(pixels_2); 254 + return ret; 255 + } 256 + 257 + int mga_crtc_cursor_move(struct drm_crtc *crtc, int x, int y) 258 + { 259 + struct mga_device *mdev = (struct mga_device *)crtc->dev->dev_private; 260 + /* Our origin is at (64,64) */ 261 + x += 64; 262 + y += 64; 263 + 264 + BUG_ON(x <= 0); 265 + BUG_ON(y <= 0); 266 + BUG_ON(x & ~0xffff); 267 + BUG_ON(y & ~0xffff); 268 + 269 + WREG8(MGA_CURPOSXL, x & 0xff); 270 + WREG8(MGA_CURPOSXH, (x>>8) & 0xff); 271 + 272 + WREG8(MGA_CURPOSYL, y & 0xff); 273 + WREG8(MGA_CURPOSYH, (y>>8) & 0xff); 274 + return 0; 275 + }
+21
drivers/gpu/drm/mgag200/mgag200_drv.h
··· 149 149 struct mga_i2c_chan *i2c; 150 150 }; 151 151 152 + struct mga_cursor { 153 + /* 154 + We have to have 2 buffers for the cursor to avoid occasional 155 + corruption while switching cursor icons. 156 + If either of these is NULL, then don't do hardware cursors, and 157 + fall back to software. 158 + */ 159 + struct mgag200_bo *pixels_1; 160 + struct mgag200_bo *pixels_2; 161 + u64 pixels_1_gpu_addr, pixels_2_gpu_addr; 162 + /* The currently displayed icon, this points to one of pixels_1, or pixels_2 */ 163 + struct mgag200_bo *pixels_current; 164 + /* The previously displayed icon */ 165 + struct mgag200_bo *pixels_prev; 166 + }; 152 167 153 168 struct mga_mc { 154 169 resource_size_t vram_size; ··· 196 181 struct mga_mode_info mode_info; 197 182 198 183 struct mga_fbdev *mfbdev; 184 + struct mga_cursor cursor; 199 185 200 186 bool suspended; 201 187 int num_crtc; ··· 289 273 int mgag200_bo_pin(struct mgag200_bo *bo, u32 pl_flag, u64 *gpu_addr); 290 274 int mgag200_bo_unpin(struct mgag200_bo *bo); 291 275 int mgag200_bo_push_sysram(struct mgag200_bo *bo); 276 + /* mgag200_cursor.c */ 277 + int mga_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv, 278 + uint32_t handle, uint32_t width, uint32_t height); 279 + int mga_crtc_cursor_move(struct drm_crtc *crtc, int x, int y); 280 + 292 281 #endif /* __MGAG200_DRV_H__ */
+20 -1
drivers/gpu/drm/mgag200/mgag200_main.c
··· 221 221 dev->mode_config.prefer_shadow = 1; 222 222 223 223 r = mgag200_modeset_init(mdev); 224 - if (r) 224 + if (r) { 225 225 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r); 226 + goto out; 227 + } 228 + 229 + /* Make small buffers to store a hardware cursor (double buffered icon updates) */ 230 + mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0, 231 + &mdev->cursor.pixels_1); 232 + mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0, 233 + &mdev->cursor.pixels_2); 234 + if (!mdev->cursor.pixels_2 || !mdev->cursor.pixels_1) 235 + goto cursor_nospace; 236 + mdev->cursor.pixels_current = mdev->cursor.pixels_1; 237 + mdev->cursor.pixels_prev = mdev->cursor.pixels_2; 238 + goto cursor_done; 239 + cursor_nospace: 240 + mdev->cursor.pixels_1 = NULL; 241 + mdev->cursor.pixels_2 = NULL; 242 + dev_warn(&dev->pdev->dev, "Could not allocate space for cursors. Not doing hardware cursors.\n"); 243 + cursor_done: 244 + 226 245 out: 227 246 if (r) 228 247 mgag200_driver_unload(dev);
+2
drivers/gpu/drm/mgag200/mgag200_mode.c
··· 1252 1252 1253 1253 /* These provide the minimum set of functions required to handle a CRTC */ 1254 1254 static const struct drm_crtc_funcs mga_crtc_funcs = { 1255 + .cursor_set = mga_crtc_cursor_set, 1256 + .cursor_move = mga_crtc_cursor_move, 1255 1257 .gamma_set = mga_crtc_gamma_set, 1256 1258 .set_config = drm_crtc_helper_set_config, 1257 1259 .destroy = mga_crtc_destroy,
+5 -1
drivers/gpu/drm/mgag200/mgag200_reg.h
··· 235 235 #define MGAREG_CRTCEXT_INDEX 0x1fde 236 236 #define MGAREG_CRTCEXT_DATA 0x1fdf 237 237 238 - 238 + /* Cursor X and Y position */ 239 + #define MGA_CURPOSXL 0x3c0c 240 + #define MGA_CURPOSXH 0x3c0d 241 + #define MGA_CURPOSYL 0x3c0e 242 + #define MGA_CURPOSYH 0x3c0f 239 243 240 244 /* MGA bits for registers PCI_OPTION_REG */ 241 245 #define MGA1064_OPT_SYS_CLK_PCI ( 0x00 << 0 )