Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2007 David Airlie |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * David Airlie |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/tty.h> |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 33 | #include <linux/sysrq.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/fb.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/screen_info.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 38 | #include <linux/vga_switcheroo.h> |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 39 | |
| 40 | #include "drmP.h" |
| 41 | #include "drm.h" |
| 42 | #include "drm_crtc.h" |
| 43 | #include "drm_crtc_helper.h" |
| 44 | #include "drm_fb_helper.h" |
| 45 | #include "nouveau_drv.h" |
| 46 | #include "nouveau_drm.h" |
| 47 | #include "nouveau_crtc.h" |
| 48 | #include "nouveau_fb.h" |
| 49 | #include "nouveau_fbcon.h" |
| 50 | #include "nouveau_dma.h" |
| 51 | |
| 52 | static int |
| 53 | nouveau_fbcon_sync(struct fb_info *info) |
| 54 | { |
| 55 | struct nouveau_fbcon_par *par = info->par; |
| 56 | struct drm_device *dev = par->dev; |
| 57 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 58 | struct nouveau_channel *chan = dev_priv->channel; |
| 59 | int ret, i; |
| 60 | |
Ben Skeggs | 0735f62 | 2009-12-16 14:28:55 +1000 | [diff] [blame] | 61 | if (!chan || !chan->accel_done || |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 62 | info->state != FBINFO_STATE_RUNNING || |
| 63 | info->flags & FBINFO_HWACCEL_DISABLED) |
| 64 | return 0; |
| 65 | |
| 66 | if (RING_SPACE(chan, 4)) { |
Marcin Slusarz | 846975a | 2010-01-04 19:25:09 +0100 | [diff] [blame] | 67 | nouveau_fbcon_gpu_lockup(info); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | BEGIN_RING(chan, 0, 0x0104, 1); |
| 72 | OUT_RING(chan, 0); |
| 73 | BEGIN_RING(chan, 0, 0x0100, 1); |
| 74 | OUT_RING(chan, 0); |
| 75 | nouveau_bo_wr32(chan->notifier_bo, chan->m2mf_ntfy + 3, 0xffffffff); |
| 76 | FIRE_RING(chan); |
| 77 | |
| 78 | ret = -EBUSY; |
| 79 | for (i = 0; i < 100000; i++) { |
| 80 | if (!nouveau_bo_rd32(chan->notifier_bo, chan->m2mf_ntfy + 3)) { |
| 81 | ret = 0; |
| 82 | break; |
| 83 | } |
| 84 | DRM_UDELAY(1); |
| 85 | } |
| 86 | |
| 87 | if (ret) { |
Marcin Slusarz | 846975a | 2010-01-04 19:25:09 +0100 | [diff] [blame] | 88 | nouveau_fbcon_gpu_lockup(info); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | chan->accel_done = false; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static struct fb_ops nouveau_fbcon_ops = { |
| 97 | .owner = THIS_MODULE, |
| 98 | .fb_check_var = drm_fb_helper_check_var, |
| 99 | .fb_set_par = drm_fb_helper_set_par, |
| 100 | .fb_setcolreg = drm_fb_helper_setcolreg, |
| 101 | .fb_fillrect = cfb_fillrect, |
| 102 | .fb_copyarea = cfb_copyarea, |
| 103 | .fb_imageblit = cfb_imageblit, |
| 104 | .fb_sync = nouveau_fbcon_sync, |
| 105 | .fb_pan_display = drm_fb_helper_pan_display, |
| 106 | .fb_blank = drm_fb_helper_blank, |
| 107 | .fb_setcmap = drm_fb_helper_setcmap, |
| 108 | }; |
| 109 | |
Marcin Kościelnicki | 126b544 | 2010-01-27 14:03:18 +0000 | [diff] [blame] | 110 | static struct fb_ops nv04_fbcon_ops = { |
| 111 | .owner = THIS_MODULE, |
| 112 | .fb_check_var = drm_fb_helper_check_var, |
| 113 | .fb_set_par = drm_fb_helper_set_par, |
| 114 | .fb_setcolreg = drm_fb_helper_setcolreg, |
| 115 | .fb_fillrect = nv04_fbcon_fillrect, |
| 116 | .fb_copyarea = nv04_fbcon_copyarea, |
| 117 | .fb_imageblit = nv04_fbcon_imageblit, |
| 118 | .fb_sync = nouveau_fbcon_sync, |
| 119 | .fb_pan_display = drm_fb_helper_pan_display, |
| 120 | .fb_blank = drm_fb_helper_blank, |
| 121 | .fb_setcmap = drm_fb_helper_setcmap, |
| 122 | }; |
| 123 | |
| 124 | static struct fb_ops nv50_fbcon_ops = { |
| 125 | .owner = THIS_MODULE, |
| 126 | .fb_check_var = drm_fb_helper_check_var, |
| 127 | .fb_set_par = drm_fb_helper_set_par, |
| 128 | .fb_setcolreg = drm_fb_helper_setcolreg, |
| 129 | .fb_fillrect = nv50_fbcon_fillrect, |
| 130 | .fb_copyarea = nv50_fbcon_copyarea, |
| 131 | .fb_imageblit = nv50_fbcon_imageblit, |
| 132 | .fb_sync = nouveau_fbcon_sync, |
| 133 | .fb_pan_display = drm_fb_helper_pan_display, |
| 134 | .fb_blank = drm_fb_helper_blank, |
| 135 | .fb_setcmap = drm_fb_helper_setcmap, |
| 136 | }; |
| 137 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 138 | static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, |
| 139 | u16 blue, int regno) |
| 140 | { |
| 141 | struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc); |
| 142 | |
| 143 | nv_crtc->lut.r[regno] = red; |
| 144 | nv_crtc->lut.g[regno] = green; |
| 145 | nv_crtc->lut.b[regno] = blue; |
| 146 | } |
| 147 | |
| 148 | static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green, |
| 149 | u16 *blue, int regno) |
| 150 | { |
| 151 | struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc); |
| 152 | |
| 153 | *red = nv_crtc->lut.r[regno]; |
| 154 | *green = nv_crtc->lut.g[regno]; |
| 155 | *blue = nv_crtc->lut.b[regno]; |
| 156 | } |
| 157 | |
| 158 | static struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = { |
| 159 | .gamma_set = nouveau_fbcon_gamma_set, |
| 160 | .gamma_get = nouveau_fbcon_gamma_get |
| 161 | }; |
| 162 | |
| 163 | #if defined(__i386__) || defined(__x86_64__) |
| 164 | static bool |
| 165 | nouveau_fbcon_has_vesafb_or_efifb(struct drm_device *dev) |
| 166 | { |
| 167 | struct pci_dev *pdev = dev->pdev; |
| 168 | int ramin; |
| 169 | |
| 170 | if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB && |
| 171 | screen_info.orig_video_isVGA != VIDEO_TYPE_EFI) |
| 172 | return false; |
| 173 | |
| 174 | if (screen_info.lfb_base < pci_resource_start(pdev, 1)) |
| 175 | goto not_fb; |
| 176 | |
| 177 | if (screen_info.lfb_base + screen_info.lfb_size >= |
| 178 | pci_resource_start(pdev, 1) + pci_resource_len(pdev, 1)) |
| 179 | goto not_fb; |
| 180 | |
| 181 | return true; |
| 182 | not_fb: |
| 183 | ramin = 2; |
| 184 | if (pci_resource_len(pdev, ramin) == 0) { |
| 185 | ramin = 3; |
| 186 | if (pci_resource_len(pdev, ramin) == 0) |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if (screen_info.lfb_base < pci_resource_start(pdev, ramin)) |
| 191 | return false; |
| 192 | |
| 193 | if (screen_info.lfb_base + screen_info.lfb_size >= |
| 194 | pci_resource_start(pdev, ramin) + pci_resource_len(pdev, ramin)) |
| 195 | return false; |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | void |
| 202 | nouveau_fbcon_zfill(struct drm_device *dev) |
| 203 | { |
| 204 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 205 | struct fb_info *info = dev_priv->fbdev_info; |
| 206 | struct fb_fillrect rect; |
| 207 | |
| 208 | /* Clear the entire fbcon. The drm will program every connector |
| 209 | * with it's preferred mode. If the sizes differ, one display will |
| 210 | * quite likely have garbage around the console. |
| 211 | */ |
| 212 | rect.dx = rect.dy = 0; |
| 213 | rect.width = info->var.xres_virtual; |
| 214 | rect.height = info->var.yres_virtual; |
| 215 | rect.color = 0; |
| 216 | rect.rop = ROP_COPY; |
| 217 | info->fbops->fb_fillrect(info, &rect); |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | nouveau_fbcon_create(struct drm_device *dev, uint32_t fb_width, |
| 222 | uint32_t fb_height, uint32_t surface_width, |
| 223 | uint32_t surface_height, uint32_t surface_depth, |
| 224 | uint32_t surface_bpp, struct drm_framebuffer **pfb) |
| 225 | { |
| 226 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 227 | struct fb_info *info; |
| 228 | struct nouveau_fbcon_par *par; |
| 229 | struct drm_framebuffer *fb; |
| 230 | struct nouveau_framebuffer *nouveau_fb; |
| 231 | struct nouveau_bo *nvbo; |
| 232 | struct drm_mode_fb_cmd mode_cmd; |
| 233 | struct device *device = &dev->pdev->dev; |
| 234 | int size, ret; |
| 235 | |
| 236 | mode_cmd.width = surface_width; |
| 237 | mode_cmd.height = surface_height; |
| 238 | |
| 239 | mode_cmd.bpp = surface_bpp; |
| 240 | mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3); |
Maarten Maathuis | 1c7059e | 2009-12-25 18:51:17 +0100 | [diff] [blame] | 241 | mode_cmd.pitch = roundup(mode_cmd.pitch, 256); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 242 | mode_cmd.depth = surface_depth; |
| 243 | |
| 244 | size = mode_cmd.pitch * mode_cmd.height; |
Maarten Maathuis | 1c7059e | 2009-12-25 18:51:17 +0100 | [diff] [blame] | 245 | size = roundup(size, PAGE_SIZE); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 246 | |
| 247 | ret = nouveau_gem_new(dev, dev_priv->channel, size, 0, TTM_PL_FLAG_VRAM, |
| 248 | 0, 0x0000, false, true, &nvbo); |
| 249 | if (ret) { |
| 250 | NV_ERROR(dev, "failed to allocate framebuffer\n"); |
| 251 | goto out; |
| 252 | } |
| 253 | |
| 254 | ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM); |
| 255 | if (ret) { |
| 256 | NV_ERROR(dev, "failed to pin fb: %d\n", ret); |
| 257 | nouveau_bo_ref(NULL, &nvbo); |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | ret = nouveau_bo_map(nvbo); |
| 262 | if (ret) { |
| 263 | NV_ERROR(dev, "failed to map fb: %d\n", ret); |
| 264 | nouveau_bo_unpin(nvbo); |
| 265 | nouveau_bo_ref(NULL, &nvbo); |
| 266 | goto out; |
| 267 | } |
| 268 | |
| 269 | mutex_lock(&dev->struct_mutex); |
| 270 | |
| 271 | fb = nouveau_framebuffer_create(dev, nvbo, &mode_cmd); |
| 272 | if (!fb) { |
| 273 | ret = -ENOMEM; |
| 274 | NV_ERROR(dev, "failed to allocate fb.\n"); |
| 275 | goto out_unref; |
| 276 | } |
| 277 | |
| 278 | list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list); |
| 279 | |
| 280 | nouveau_fb = nouveau_framebuffer(fb); |
| 281 | *pfb = fb; |
| 282 | |
| 283 | info = framebuffer_alloc(sizeof(struct nouveau_fbcon_par), device); |
| 284 | if (!info) { |
| 285 | ret = -ENOMEM; |
| 286 | goto out_unref; |
| 287 | } |
| 288 | |
| 289 | par = info->par; |
| 290 | par->helper.funcs = &nouveau_fbcon_helper_funcs; |
| 291 | par->helper.dev = dev; |
| 292 | ret = drm_fb_helper_init_crtc_count(&par->helper, 2, 4); |
| 293 | if (ret) |
| 294 | goto out_unref; |
| 295 | dev_priv->fbdev_info = info; |
| 296 | |
| 297 | strcpy(info->fix.id, "nouveaufb"); |
Marcin Kościelnicki | a32ed69 | 2010-01-26 14:00:42 +0000 | [diff] [blame] | 298 | if (nouveau_nofbaccel) |
| 299 | info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED; |
| 300 | else |
| 301 | info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | |
| 302 | FBINFO_HWACCEL_FILLRECT | |
| 303 | FBINFO_HWACCEL_IMAGEBLIT; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 304 | info->fbops = &nouveau_fbcon_ops; |
| 305 | info->fix.smem_start = dev->mode_config.fb_base + nvbo->bo.offset - |
| 306 | dev_priv->vm_vram_base; |
| 307 | info->fix.smem_len = size; |
| 308 | |
| 309 | info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo); |
| 310 | info->screen_size = size; |
| 311 | |
| 312 | drm_fb_helper_fill_fix(info, fb->pitch, fb->depth); |
| 313 | drm_fb_helper_fill_var(info, fb, fb_width, fb_height); |
| 314 | |
| 315 | /* FIXME: we really shouldn't expose mmio space at all */ |
| 316 | info->fix.mmio_start = pci_resource_start(dev->pdev, 1); |
| 317 | info->fix.mmio_len = pci_resource_len(dev->pdev, 1); |
| 318 | |
| 319 | /* Set aperture base/size for vesafb takeover */ |
| 320 | #if defined(__i386__) || defined(__x86_64__) |
| 321 | if (nouveau_fbcon_has_vesafb_or_efifb(dev)) { |
| 322 | /* Some NVIDIA VBIOS' are stupid and decide to put the |
| 323 | * framebuffer in the middle of the PRAMIN BAR for |
| 324 | * whatever reason. We need to know the exact lfb_base |
| 325 | * to get vesafb kicked off, and the only reliable way |
| 326 | * we have left is to find out lfb_base the same way |
| 327 | * vesafb did. |
| 328 | */ |
| 329 | info->aperture_base = screen_info.lfb_base; |
| 330 | info->aperture_size = screen_info.lfb_size; |
| 331 | if (screen_info.orig_video_isVGA == VIDEO_TYPE_VLFB) |
| 332 | info->aperture_size *= 65536; |
| 333 | } else |
| 334 | #endif |
| 335 | { |
| 336 | info->aperture_base = info->fix.mmio_start; |
| 337 | info->aperture_size = info->fix.mmio_len; |
| 338 | } |
| 339 | |
| 340 | info->pixmap.size = 64*1024; |
| 341 | info->pixmap.buf_align = 8; |
| 342 | info->pixmap.access_align = 32; |
| 343 | info->pixmap.flags = FB_PIXMAP_SYSTEM; |
| 344 | info->pixmap.scan_align = 1; |
| 345 | |
| 346 | fb->fbdev = info; |
| 347 | |
| 348 | par->nouveau_fb = nouveau_fb; |
| 349 | par->dev = dev; |
| 350 | |
Marcin Kościelnicki | a32ed69 | 2010-01-26 14:00:42 +0000 | [diff] [blame] | 351 | if (dev_priv->channel && !nouveau_nofbaccel) { |
Ben Skeggs | 0735f62 | 2009-12-16 14:28:55 +1000 | [diff] [blame] | 352 | switch (dev_priv->card_type) { |
| 353 | case NV_50: |
| 354 | nv50_fbcon_accel_init(info); |
Marcin Kościelnicki | 126b544 | 2010-01-27 14:03:18 +0000 | [diff] [blame] | 355 | info->fbops = &nv50_fbcon_ops; |
Ben Skeggs | 0735f62 | 2009-12-16 14:28:55 +1000 | [diff] [blame] | 356 | break; |
| 357 | default: |
| 358 | nv04_fbcon_accel_init(info); |
Marcin Kościelnicki | 126b544 | 2010-01-27 14:03:18 +0000 | [diff] [blame] | 359 | info->fbops = &nv04_fbcon_ops; |
Ben Skeggs | 0735f62 | 2009-12-16 14:28:55 +1000 | [diff] [blame] | 360 | break; |
| 361 | }; |
| 362 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 363 | |
| 364 | nouveau_fbcon_zfill(dev); |
| 365 | |
| 366 | /* To allow resizeing without swapping buffers */ |
| 367 | NV_INFO(dev, "allocated %dx%d fb: 0x%lx, bo %p\n", |
| 368 | nouveau_fb->base.width, |
| 369 | nouveau_fb->base.height, |
| 370 | nvbo->bo.offset, nvbo); |
| 371 | |
| 372 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 373 | vga_switcheroo_client_fb_set(dev->pdev, info); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 374 | return 0; |
| 375 | |
| 376 | out_unref: |
| 377 | mutex_unlock(&dev->struct_mutex); |
| 378 | out: |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | int |
| 383 | nouveau_fbcon_probe(struct drm_device *dev) |
| 384 | { |
Maarten Maathuis | ef2bb50 | 2009-12-13 16:53:12 +0100 | [diff] [blame] | 385 | NV_DEBUG_KMS(dev, "\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 386 | |
| 387 | return drm_fb_helper_single_fb_probe(dev, 32, nouveau_fbcon_create); |
| 388 | } |
| 389 | |
| 390 | int |
| 391 | nouveau_fbcon_remove(struct drm_device *dev, struct drm_framebuffer *fb) |
| 392 | { |
| 393 | struct nouveau_framebuffer *nouveau_fb = nouveau_framebuffer(fb); |
| 394 | struct fb_info *info; |
| 395 | |
| 396 | if (!fb) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | info = fb->fbdev; |
| 400 | if (info) { |
| 401 | struct nouveau_fbcon_par *par = info->par; |
| 402 | |
| 403 | unregister_framebuffer(info); |
| 404 | nouveau_bo_unmap(nouveau_fb->nvbo); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 405 | drm_gem_object_unreference_unlocked(nouveau_fb->nvbo->gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 406 | nouveau_fb->nvbo = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 407 | if (par) |
| 408 | drm_fb_helper_free(&par->helper); |
| 409 | framebuffer_release(info); |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
Marcin Slusarz | 846975a | 2010-01-04 19:25:09 +0100 | [diff] [blame] | 414 | |
| 415 | void nouveau_fbcon_gpu_lockup(struct fb_info *info) |
| 416 | { |
| 417 | struct nouveau_fbcon_par *par = info->par; |
| 418 | struct drm_device *dev = par->dev; |
| 419 | |
| 420 | NV_ERROR(dev, "GPU lockup - switching to software fbcon\n"); |
| 421 | info->flags |= FBINFO_HWACCEL_DISABLED; |
| 422 | } |