Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Matt Turner. |
| 3 | * Copyright 2012 Red Hat |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General |
| 6 | * Public License version 2. See the file COPYING in the main |
| 7 | * directory of this archive for more details. |
| 8 | * |
| 9 | * Authors: Matthew Garrett |
| 10 | * Matt Turner |
| 11 | * Dave Airlie |
| 12 | */ |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 13 | #include <drm/drmP.h> |
| 14 | #include <drm/drm_crtc_helper.h> |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 15 | #include "mgag200_drv.h" |
| 16 | |
| 17 | static void mga_user_framebuffer_destroy(struct drm_framebuffer *fb) |
| 18 | { |
| 19 | struct mga_framebuffer *mga_fb = to_mga_framebuffer(fb); |
| 20 | if (mga_fb->obj) |
| 21 | drm_gem_object_unreference_unlocked(mga_fb->obj); |
| 22 | drm_framebuffer_cleanup(fb); |
| 23 | kfree(fb); |
| 24 | } |
| 25 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 26 | static const struct drm_framebuffer_funcs mga_fb_funcs = { |
| 27 | .destroy = mga_user_framebuffer_destroy, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | int mgag200_framebuffer_init(struct drm_device *dev, |
| 31 | struct mga_framebuffer *gfb, |
| 32 | struct drm_mode_fb_cmd2 *mode_cmd, |
| 33 | struct drm_gem_object *obj) |
| 34 | { |
Daniel Vetter | c7d73f6 | 2012-12-13 23:38:38 +0100 | [diff] [blame] | 35 | int ret; |
| 36 | |
| 37 | drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd); |
| 38 | gfb->obj = obj; |
| 39 | ret = drm_framebuffer_init(dev, &gfb->base, &mga_fb_funcs); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 40 | if (ret) { |
| 41 | DRM_ERROR("drm_framebuffer_init failed: %d\n", ret); |
| 42 | return ret; |
| 43 | } |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static struct drm_framebuffer * |
| 48 | mgag200_user_framebuffer_create(struct drm_device *dev, |
| 49 | struct drm_file *filp, |
| 50 | struct drm_mode_fb_cmd2 *mode_cmd) |
| 51 | { |
| 52 | struct drm_gem_object *obj; |
| 53 | struct mga_framebuffer *mga_fb; |
| 54 | int ret; |
| 55 | |
| 56 | obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]); |
| 57 | if (obj == NULL) |
| 58 | return ERR_PTR(-ENOENT); |
| 59 | |
| 60 | mga_fb = kzalloc(sizeof(*mga_fb), GFP_KERNEL); |
| 61 | if (!mga_fb) { |
| 62 | drm_gem_object_unreference_unlocked(obj); |
| 63 | return ERR_PTR(-ENOMEM); |
| 64 | } |
| 65 | |
| 66 | ret = mgag200_framebuffer_init(dev, mga_fb, mode_cmd, obj); |
| 67 | if (ret) { |
| 68 | drm_gem_object_unreference_unlocked(obj); |
| 69 | kfree(mga_fb); |
| 70 | return ERR_PTR(ret); |
| 71 | } |
| 72 | return &mga_fb->base; |
| 73 | } |
| 74 | |
| 75 | static const struct drm_mode_config_funcs mga_mode_funcs = { |
| 76 | .fb_create = mgag200_user_framebuffer_create, |
| 77 | }; |
| 78 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 79 | static int mga_probe_vram(struct mga_device *mdev, void __iomem *mem) |
| 80 | { |
| 81 | int offset; |
| 82 | int orig; |
| 83 | int test1, test2; |
| 84 | int orig1, orig2; |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 85 | unsigned int vram_size; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 86 | |
| 87 | /* Probe */ |
| 88 | orig = ioread16(mem); |
| 89 | iowrite16(0, mem); |
| 90 | |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 91 | vram_size = mdev->mc.vram_window; |
| 92 | |
| 93 | if ((mdev->type == G200_EW3) && (vram_size >= 0x1000000)) { |
| 94 | vram_size = vram_size - 0x400000; |
| 95 | } |
| 96 | |
| 97 | for (offset = 0x100000; offset < vram_size; offset += 0x4000) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 98 | orig1 = ioread8(mem + offset); |
| 99 | orig2 = ioread8(mem + offset + 0x100); |
| 100 | |
| 101 | iowrite16(0xaa55, mem + offset); |
| 102 | iowrite16(0xaa55, mem + offset + 0x100); |
| 103 | |
| 104 | test1 = ioread16(mem + offset); |
| 105 | test2 = ioread16(mem); |
| 106 | |
| 107 | iowrite16(orig1, mem + offset); |
| 108 | iowrite16(orig2, mem + offset + 0x100); |
| 109 | |
| 110 | if (test1 != 0xaa55) { |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | if (test2) { |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | iowrite16(orig, mem); |
| 120 | return offset - 65536; |
| 121 | } |
| 122 | |
| 123 | /* Map the framebuffer from the card and configure the core */ |
| 124 | static int mga_vram_init(struct mga_device *mdev) |
| 125 | { |
| 126 | void __iomem *mem; |
| 127 | struct apertures_struct *aper = alloc_apertures(1); |
Tommi Rantala | 0a7fdc5 | 2012-11-09 09:19:36 +0000 | [diff] [blame] | 128 | if (!aper) |
| 129 | return -ENOMEM; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 130 | |
| 131 | /* BAR 0 is VRAM */ |
| 132 | mdev->mc.vram_base = pci_resource_start(mdev->dev->pdev, 0); |
| 133 | mdev->mc.vram_window = pci_resource_len(mdev->dev->pdev, 0); |
| 134 | |
| 135 | aper->ranges[0].base = mdev->mc.vram_base; |
| 136 | aper->ranges[0].size = mdev->mc.vram_window; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 137 | |
| 138 | remove_conflicting_framebuffers(aper, "mgafb", true); |
Tommi Rantala | 6022864 | 2012-11-09 09:19:37 +0000 | [diff] [blame] | 139 | kfree(aper); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 140 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 141 | if (!devm_request_mem_region(mdev->dev->dev, mdev->mc.vram_base, mdev->mc.vram_window, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 142 | "mgadrmfb_vram")) { |
| 143 | DRM_ERROR("can't reserve VRAM\n"); |
| 144 | return -ENXIO; |
| 145 | } |
| 146 | |
| 147 | mem = pci_iomap(mdev->dev->pdev, 0, 0); |
| 148 | |
| 149 | mdev->mc.vram_size = mga_probe_vram(mdev, mem); |
| 150 | |
| 151 | pci_iounmap(mdev->dev->pdev, mem); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int mgag200_device_init(struct drm_device *dev, |
| 157 | uint32_t flags) |
| 158 | { |
| 159 | struct mga_device *mdev = dev->dev_private; |
| 160 | int ret, option; |
| 161 | |
| 162 | mdev->type = flags; |
| 163 | |
| 164 | /* Hardcode the number of CRTCs to 1 */ |
| 165 | mdev->num_crtc = 1; |
| 166 | |
| 167 | pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option); |
| 168 | mdev->has_sdram = !(option & (1 << 14)); |
| 169 | |
| 170 | /* BAR 0 is the framebuffer, BAR 1 contains registers */ |
| 171 | mdev->rmmio_base = pci_resource_start(mdev->dev->pdev, 1); |
| 172 | mdev->rmmio_size = pci_resource_len(mdev->dev->pdev, 1); |
| 173 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 174 | if (!devm_request_mem_region(mdev->dev->dev, mdev->rmmio_base, mdev->rmmio_size, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 175 | "mgadrmfb_mmio")) { |
| 176 | DRM_ERROR("can't reserve mmio registers\n"); |
| 177 | return -ENOMEM; |
| 178 | } |
| 179 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 180 | mdev->rmmio = pcim_iomap(dev->pdev, 1, 0); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 181 | if (mdev->rmmio == NULL) |
| 182 | return -ENOMEM; |
| 183 | |
| 184 | /* stash G200 SE model number for later use */ |
| 185 | if (IS_G200_SE(mdev)) |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 186 | mdev->unique_rev_id = RREG32(0x1e24); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 187 | |
| 188 | ret = mga_vram_init(mdev); |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 189 | if (ret) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 190 | return ret; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 191 | |
| 192 | mdev->bpp_shifts[0] = 0; |
| 193 | mdev->bpp_shifts[1] = 1; |
| 194 | mdev->bpp_shifts[2] = 0; |
| 195 | mdev->bpp_shifts[3] = 2; |
| 196 | return 0; |
| 197 | } |
| 198 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 199 | /* |
| 200 | * Functions here will be called by the core once it's bound the driver to |
| 201 | * a PCI device |
| 202 | */ |
| 203 | |
| 204 | |
| 205 | int mgag200_driver_load(struct drm_device *dev, unsigned long flags) |
| 206 | { |
| 207 | struct mga_device *mdev; |
| 208 | int r; |
| 209 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 210 | mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 211 | if (mdev == NULL) |
| 212 | return -ENOMEM; |
| 213 | dev->dev_private = (void *)mdev; |
| 214 | mdev->dev = dev; |
| 215 | |
| 216 | r = mgag200_device_init(dev, flags); |
| 217 | if (r) { |
| 218 | dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r); |
Christopher Harvey | 2791197 | 2013-06-05 11:29:57 -0400 | [diff] [blame] | 219 | return r; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 220 | } |
| 221 | r = mgag200_mm_init(mdev); |
| 222 | if (r) |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 223 | goto err_mm; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 224 | |
| 225 | drm_mode_config_init(dev); |
| 226 | dev->mode_config.funcs = (void *)&mga_mode_funcs; |
Dave Airlie | 918be88 | 2014-01-21 01:47:46 -0500 | [diff] [blame] | 227 | if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024)) |
| 228 | dev->mode_config.preferred_depth = 16; |
| 229 | else |
| 230 | dev->mode_config.preferred_depth = 24; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 231 | dev->mode_config.prefer_shadow = 1; |
| 232 | |
| 233 | r = mgag200_modeset_init(mdev); |
Christopher Harvey | a080db9 | 2013-06-05 15:24:26 -0400 | [diff] [blame] | 234 | if (r) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 235 | dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r); |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 236 | goto err_modeset; |
Christopher Harvey | a080db9 | 2013-06-05 15:24:26 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* Make small buffers to store a hardware cursor (double buffered icon updates) */ |
| 240 | mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0, |
| 241 | &mdev->cursor.pixels_1); |
| 242 | mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0, |
| 243 | &mdev->cursor.pixels_2); |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 244 | if (!mdev->cursor.pixels_2 || !mdev->cursor.pixels_1) { |
| 245 | mdev->cursor.pixels_1 = NULL; |
| 246 | mdev->cursor.pixels_2 = NULL; |
| 247 | dev_warn(&dev->pdev->dev, |
| 248 | "Could not allocate space for cursors. Not doing hardware cursors.\n"); |
| 249 | } else { |
| 250 | mdev->cursor.pixels_current = mdev->cursor.pixels_1; |
| 251 | mdev->cursor.pixels_prev = mdev->cursor.pixels_2; |
| 252 | } |
Christopher Harvey | a080db9 | 2013-06-05 15:24:26 -0400 | [diff] [blame] | 253 | |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 254 | return 0; |
| 255 | |
| 256 | err_modeset: |
| 257 | drm_mode_config_cleanup(dev); |
| 258 | mgag200_mm_fini(mdev); |
| 259 | err_mm: |
| 260 | dev->dev_private = NULL; |
| 261 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 262 | return r; |
| 263 | } |
| 264 | |
| 265 | int mgag200_driver_unload(struct drm_device *dev) |
| 266 | { |
| 267 | struct mga_device *mdev = dev->dev_private; |
| 268 | |
| 269 | if (mdev == NULL) |
| 270 | return 0; |
| 271 | mgag200_modeset_fini(mdev); |
| 272 | mgag200_fbdev_fini(mdev); |
| 273 | drm_mode_config_cleanup(dev); |
| 274 | mgag200_mm_fini(mdev); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 275 | dev->dev_private = NULL; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int mgag200_gem_create(struct drm_device *dev, |
| 280 | u32 size, bool iskernel, |
| 281 | struct drm_gem_object **obj) |
| 282 | { |
| 283 | struct mgag200_bo *astbo; |
| 284 | int ret; |
| 285 | |
| 286 | *obj = NULL; |
| 287 | |
| 288 | size = roundup(size, PAGE_SIZE); |
| 289 | if (size == 0) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | ret = mgag200_bo_create(dev, size, 0, 0, &astbo); |
| 293 | if (ret) { |
| 294 | if (ret != -ERESTARTSYS) |
| 295 | DRM_ERROR("failed to allocate GEM object\n"); |
| 296 | return ret; |
| 297 | } |
| 298 | *obj = &astbo->gem; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | int mgag200_dumb_create(struct drm_file *file, |
| 303 | struct drm_device *dev, |
| 304 | struct drm_mode_create_dumb *args) |
| 305 | { |
| 306 | int ret; |
| 307 | struct drm_gem_object *gobj; |
| 308 | u32 handle; |
| 309 | |
| 310 | args->pitch = args->width * ((args->bpp + 7) / 8); |
| 311 | args->size = args->pitch * args->height; |
| 312 | |
| 313 | ret = mgag200_gem_create(dev, args->size, false, |
| 314 | &gobj); |
| 315 | if (ret) |
| 316 | return ret; |
| 317 | |
| 318 | ret = drm_gem_handle_create(file, gobj, &handle); |
| 319 | drm_gem_object_unreference_unlocked(gobj); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | |
| 323 | args->handle = handle; |
| 324 | return 0; |
| 325 | } |
| 326 | |
Rashika | b2890a7 | 2014-01-06 20:36:15 +0530 | [diff] [blame] | 327 | static void mgag200_bo_unref(struct mgag200_bo **bo) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 328 | { |
| 329 | struct ttm_buffer_object *tbo; |
| 330 | |
| 331 | if ((*bo) == NULL) |
| 332 | return; |
| 333 | |
| 334 | tbo = &((*bo)->bo); |
| 335 | ttm_bo_unref(&tbo); |
Daniel Vetter | 36b347f | 2014-04-05 10:03:22 +0200 | [diff] [blame] | 336 | *bo = NULL; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void mgag200_gem_free_object(struct drm_gem_object *obj) |
| 340 | { |
| 341 | struct mgag200_bo *mgag200_bo = gem_to_mga_bo(obj); |
| 342 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 343 | mgag200_bo_unref(&mgag200_bo); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | static inline u64 mgag200_bo_mmap_offset(struct mgag200_bo *bo) |
| 348 | { |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 349 | return drm_vma_node_offset_addr(&bo->bo.vma_node); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | int |
| 353 | mgag200_dumb_mmap_offset(struct drm_file *file, |
| 354 | struct drm_device *dev, |
| 355 | uint32_t handle, |
| 356 | uint64_t *offset) |
| 357 | { |
| 358 | struct drm_gem_object *obj; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 359 | struct mgag200_bo *bo; |
| 360 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 361 | obj = drm_gem_object_lookup(dev, file, handle); |
Daniel Vetter | 34c294f | 2015-07-09 23:32:38 +0200 | [diff] [blame] | 362 | if (obj == NULL) |
| 363 | return -ENOENT; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 364 | |
| 365 | bo = gem_to_mga_bo(obj); |
| 366 | *offset = mgag200_bo_mmap_offset(bo); |
| 367 | |
Daniel Vetter | 34c294f | 2015-07-09 23:32:38 +0200 | [diff] [blame] | 368 | drm_gem_object_unreference_unlocked(obj); |
| 369 | return 0; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 370 | } |