1 /* 2 * Copyright (C) 2008 Maarten Maathuis. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 */ 26 27 #include "drmP.h" 28 #include "drm_crtc_helper.h" 29 #include "nouveau_drv.h" 30 #include "nouveau_fb.h" 31 #include "nouveau_fbcon.h" 32 #include "nouveau_hw.h" 33 #include "nouveau_crtc.h" 34 #include "nouveau_dma.h" 35 #include "nouveau_connector.h" 36 #include "nouveau_gpio.h" 37 #include "nv50_display.h" 38 39 static void 40 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb) 41 { 42 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb); 43 44 if (fb->nvbo) 45 drm_gem_object_unreference_unlocked(fb->nvbo->gem); 46 47 drm_framebuffer_cleanup(drm_fb); 48 kfree(fb); 49 } 50 51 static int 52 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb, 53 struct drm_file *file_priv, 54 unsigned int *handle) 55 { 56 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb); 57 58 return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle); 59 } 60 61 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = { 62 .destroy = nouveau_user_framebuffer_destroy, 63 .create_handle = nouveau_user_framebuffer_create_handle, 64 }; 65 66 int 67 nouveau_framebuffer_init(struct drm_device *dev, 68 struct nouveau_framebuffer *nv_fb, 69 struct drm_mode_fb_cmd2 *mode_cmd, 70 struct nouveau_bo *nvbo) 71 { 72 struct drm_nouveau_private *dev_priv = dev->dev_private; 73 struct drm_framebuffer *fb = &nv_fb->base; 74 int ret; 75 76 ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs); 77 if (ret) { 78 return ret; 79 } 80 81 drm_helper_mode_fill_fb_struct(fb, mode_cmd); 82 nv_fb->nvbo = nvbo; 83 84 if (dev_priv->card_type >= NV_50) { 85 u32 tile_flags = nouveau_bo_tile_layout(nvbo); 86 if (tile_flags == 0x7a00 || 87 tile_flags == 0xfe00) 88 nv_fb->r_dma = NvEvoFB32; 89 else 90 if (tile_flags == 0x7000) 91 nv_fb->r_dma = NvEvoFB16; 92 else 93 nv_fb->r_dma = NvEvoVRAM_LP; 94 95 switch (fb->depth) { 96 case 8: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_8; break; 97 case 15: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_15; break; 98 case 16: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_16; break; 99 case 24: 100 case 32: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_24; break; 101 case 30: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_30; break; 102 default: 103 NV_ERROR(dev, "unknown depth %d\n", fb->depth); 104 return -EINVAL; 105 } 106 107 if (dev_priv->chipset == 0x50) 108 nv_fb->r_format |= (tile_flags << 8); 109 110 if (!tile_flags) { 111 if (dev_priv->card_type < NV_D0) 112 nv_fb->r_pitch = 0x00100000 | fb->pitches[0]; 113 else 114 nv_fb->r_pitch = 0x01000000 | fb->pitches[0]; 115 } else { 116 u32 mode = nvbo->tile_mode; 117 if (dev_priv->card_type >= NV_C0) 118 mode >>= 4; 119 nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode; 120 } 121 } 122 123 return 0; 124 } 125 126 static struct drm_framebuffer * 127 nouveau_user_framebuffer_create(struct drm_device *dev, 128 struct drm_file *file_priv, 129 struct drm_mode_fb_cmd2 *mode_cmd) 130 { 131 struct nouveau_framebuffer *nouveau_fb; 132 struct drm_gem_object *gem; 133 int ret; 134 135 gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]); 136 if (!gem) 137 return ERR_PTR(-ENOENT); 138 139 nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL); 140 if (!nouveau_fb) 141 return ERR_PTR(-ENOMEM); 142 143 ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem)); 144 if (ret) { 145 drm_gem_object_unreference(gem); 146 return ERR_PTR(ret); 147 } 148 149 return &nouveau_fb->base; 150 } 151 152 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = { 153 .fb_create = nouveau_user_framebuffer_create, 154 .output_poll_changed = nouveau_fbcon_output_poll_changed, 155 }; 156 157 158 struct drm_prop_enum_list { 159 u8 gen_mask; 160 int type; 161 char *name; 162 }; 163 164 static struct drm_prop_enum_list underscan[] = { 165 { 6, UNDERSCAN_AUTO, "auto" }, 166 { 6, UNDERSCAN_OFF, "off" }, 167 { 6, UNDERSCAN_ON, "on" }, 168 {} 169 }; 170 171 static struct drm_prop_enum_list dither_mode[] = { 172 { 7, DITHERING_MODE_AUTO, "auto" }, 173 { 7, DITHERING_MODE_OFF, "off" }, 174 { 1, DITHERING_MODE_ON, "on" }, 175 { 6, DITHERING_MODE_STATIC2X2, "static 2x2" }, 176 { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" }, 177 { 4, DITHERING_MODE_TEMPORAL, "temporal" }, 178 {} 179 }; 180 181 static struct drm_prop_enum_list dither_depth[] = { 182 { 6, DITHERING_DEPTH_AUTO, "auto" }, 183 { 6, DITHERING_DEPTH_6BPC, "6 bpc" }, 184 { 6, DITHERING_DEPTH_8BPC, "8 bpc" }, 185 {} 186 }; 187 188 #define PROP_ENUM(p,gen,n,list) do { \ 189 struct drm_prop_enum_list *l = (list); \ 190 int c = 0; \ 191 while (l->gen_mask) { \ 192 if (l->gen_mask & (1 << (gen))) \ 193 c++; \ 194 l++; \ 195 } \ 196 if (c) { \ 197 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \ 198 l = (list); \ 199 c = 0; \ 200 while (p && l->gen_mask) { \ 201 if (l->gen_mask & (1 << (gen))) { \ 202 drm_property_add_enum(p, c, l->type, l->name); \ 203 c++; \ 204 } \ 205 l++; \ 206 } \ 207 } \ 208 } while(0) 209 210 int 211 nouveau_display_init(struct drm_device *dev) 212 { 213 struct drm_nouveau_private *dev_priv = dev->dev_private; 214 struct nouveau_display_engine *disp = &dev_priv->engine.display; 215 struct drm_connector *connector; 216 int ret; 217 218 ret = disp->init(dev); 219 if (ret) 220 return ret; 221 222 drm_kms_helper_poll_enable(dev); 223 224 /* enable hotplug interrupts */ 225 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 226 struct nouveau_connector *conn = nouveau_connector(connector); 227 nouveau_gpio_irq(dev, 0, conn->hpd, 0xff, true); 228 } 229 230 return ret; 231 } 232 233 void 234 nouveau_display_fini(struct drm_device *dev) 235 { 236 struct drm_nouveau_private *dev_priv = dev->dev_private; 237 struct nouveau_display_engine *disp = &dev_priv->engine.display; 238 struct drm_connector *connector; 239 240 /* disable hotplug interrupts */ 241 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 242 struct nouveau_connector *conn = nouveau_connector(connector); 243 nouveau_gpio_irq(dev, 0, conn->hpd, 0xff, false); 244 } 245 246 drm_kms_helper_poll_disable(dev); 247 disp->fini(dev); 248 } 249 250 int 251 nouveau_display_create(struct drm_device *dev) 252 { 253 struct drm_nouveau_private *dev_priv = dev->dev_private; 254 struct nouveau_display_engine *disp = &dev_priv->engine.display; 255 int ret, gen; 256 257 drm_mode_config_init(dev); 258 drm_mode_create_scaling_mode_property(dev); 259 drm_mode_create_dvi_i_properties(dev); 260 261 if (dev_priv->card_type < NV_50) 262 gen = 0; 263 else 264 if (dev_priv->card_type < NV_D0) 265 gen = 1; 266 else 267 gen = 2; 268 269 PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode); 270 PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth); 271 PROP_ENUM(disp->underscan_property, gen, "underscan", underscan); 272 273 disp->underscan_hborder_property = 274 drm_property_create(dev, DRM_MODE_PROP_RANGE, 275 "underscan hborder", 2); 276 disp->underscan_hborder_property->values[0] = 0; 277 disp->underscan_hborder_property->values[1] = 128; 278 279 disp->underscan_vborder_property = 280 drm_property_create(dev, DRM_MODE_PROP_RANGE, 281 "underscan vborder", 2); 282 disp->underscan_vborder_property->values[0] = 0; 283 disp->underscan_vborder_property->values[1] = 128; 284 285 dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs; 286 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1); 287 288 dev->mode_config.min_width = 0; 289 dev->mode_config.min_height = 0; 290 if (dev_priv->card_type < NV_10) { 291 dev->mode_config.max_width = 2048; 292 dev->mode_config.max_height = 2048; 293 } else 294 if (dev_priv->card_type < NV_50) { 295 dev->mode_config.max_width = 4096; 296 dev->mode_config.max_height = 4096; 297 } else { 298 dev->mode_config.max_width = 8192; 299 dev->mode_config.max_height = 8192; 300 } 301 302 drm_kms_helper_poll_init(dev); 303 drm_kms_helper_poll_disable(dev); 304 305 ret = disp->create(dev); 306 if (ret) 307 return ret; 308 309 if (dev->mode_config.num_crtc) { 310 ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 311 if (ret) 312 return ret; 313 } 314 315 return ret; 316 } 317 318 void 319 nouveau_display_destroy(struct drm_device *dev) 320 { 321 struct drm_nouveau_private *dev_priv = dev->dev_private; 322 struct nouveau_display_engine *disp = &dev_priv->engine.display; 323 324 drm_vblank_cleanup(dev); 325 326 disp->destroy(dev); 327 328 drm_kms_helper_poll_fini(dev); 329 drm_mode_config_cleanup(dev); 330 } 331 332 int 333 nouveau_vblank_enable(struct drm_device *dev, int crtc) 334 { 335 struct drm_nouveau_private *dev_priv = dev->dev_private; 336 337 if (dev_priv->card_type >= NV_50) 338 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0, 339 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc)); 340 else 341 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 342 NV_PCRTC_INTR_0_VBLANK); 343 344 return 0; 345 } 346 347 void 348 nouveau_vblank_disable(struct drm_device *dev, int crtc) 349 { 350 struct drm_nouveau_private *dev_priv = dev->dev_private; 351 352 if (dev_priv->card_type >= NV_50) 353 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 354 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0); 355 else 356 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0); 357 } 358 359 static int 360 nouveau_page_flip_reserve(struct nouveau_bo *old_bo, 361 struct nouveau_bo *new_bo) 362 { 363 int ret; 364 365 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM); 366 if (ret) 367 return ret; 368 369 ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0); 370 if (ret) 371 goto fail; 372 373 ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0); 374 if (ret) 375 goto fail_unreserve; 376 377 return 0; 378 379 fail_unreserve: 380 ttm_bo_unreserve(&new_bo->bo); 381 fail: 382 nouveau_bo_unpin(new_bo); 383 return ret; 384 } 385 386 static void 387 nouveau_page_flip_unreserve(struct nouveau_bo *old_bo, 388 struct nouveau_bo *new_bo, 389 struct nouveau_fence *fence) 390 { 391 nouveau_bo_fence(new_bo, fence); 392 ttm_bo_unreserve(&new_bo->bo); 393 394 nouveau_bo_fence(old_bo, fence); 395 ttm_bo_unreserve(&old_bo->bo); 396 397 nouveau_bo_unpin(old_bo); 398 } 399 400 static int 401 nouveau_page_flip_emit(struct nouveau_channel *chan, 402 struct nouveau_bo *old_bo, 403 struct nouveau_bo *new_bo, 404 struct nouveau_page_flip_state *s, 405 struct nouveau_fence **pfence) 406 { 407 struct drm_nouveau_private *dev_priv = chan->dev->dev_private; 408 struct drm_device *dev = chan->dev; 409 unsigned long flags; 410 int ret; 411 412 /* Queue it to the pending list */ 413 spin_lock_irqsave(&dev->event_lock, flags); 414 list_add_tail(&s->head, &chan->nvsw.flip); 415 spin_unlock_irqrestore(&dev->event_lock, flags); 416 417 /* Synchronize with the old framebuffer */ 418 ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan); 419 if (ret) 420 goto fail; 421 422 /* Emit the pageflip */ 423 ret = RING_SPACE(chan, 2); 424 if (ret) 425 goto fail; 426 427 if (dev_priv->card_type < NV_C0) 428 BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1); 429 else 430 BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0500, 1); 431 OUT_RING (chan, 0); 432 FIRE_RING (chan); 433 434 ret = nouveau_fence_new(chan, pfence, true); 435 if (ret) 436 goto fail; 437 438 return 0; 439 fail: 440 spin_lock_irqsave(&dev->event_lock, flags); 441 list_del(&s->head); 442 spin_unlock_irqrestore(&dev->event_lock, flags); 443 return ret; 444 } 445 446 int 447 nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb, 448 struct drm_pending_vblank_event *event) 449 { 450 struct drm_device *dev = crtc->dev; 451 struct drm_nouveau_private *dev_priv = dev->dev_private; 452 struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo; 453 struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo; 454 struct nouveau_page_flip_state *s; 455 struct nouveau_channel *chan; 456 struct nouveau_fence *fence; 457 int ret; 458 459 if (!dev_priv->channel) 460 return -ENODEV; 461 462 s = kzalloc(sizeof(*s), GFP_KERNEL); 463 if (!s) 464 return -ENOMEM; 465 466 /* Don't let the buffers go away while we flip */ 467 ret = nouveau_page_flip_reserve(old_bo, new_bo); 468 if (ret) 469 goto fail_free; 470 471 /* Initialize a page flip struct */ 472 *s = (struct nouveau_page_flip_state) 473 { { }, event, nouveau_crtc(crtc)->index, 474 fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y, 475 new_bo->bo.offset }; 476 477 /* Choose the channel the flip will be handled in */ 478 chan = nouveau_fence_channel(new_bo->bo.sync_obj); 479 if (!chan) 480 chan = nouveau_channel_get_unlocked(dev_priv->channel); 481 mutex_lock(&chan->mutex); 482 483 /* Emit a page flip */ 484 if (dev_priv->card_type >= NV_50) { 485 if (dev_priv->card_type >= NV_D0) 486 ret = nvd0_display_flip_next(crtc, fb, chan, 0); 487 else 488 ret = nv50_display_flip_next(crtc, fb, chan); 489 if (ret) { 490 nouveau_channel_put(&chan); 491 goto fail_unreserve; 492 } 493 } 494 495 ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence); 496 nouveau_channel_put(&chan); 497 if (ret) 498 goto fail_unreserve; 499 500 /* Update the crtc struct and cleanup */ 501 crtc->fb = fb; 502 503 nouveau_page_flip_unreserve(old_bo, new_bo, fence); 504 nouveau_fence_unref(&fence); 505 return 0; 506 507 fail_unreserve: 508 nouveau_page_flip_unreserve(old_bo, new_bo, NULL); 509 fail_free: 510 kfree(s); 511 return ret; 512 } 513 514 int 515 nouveau_finish_page_flip(struct nouveau_channel *chan, 516 struct nouveau_page_flip_state *ps) 517 { 518 struct drm_device *dev = chan->dev; 519 struct nouveau_page_flip_state *s; 520 unsigned long flags; 521 522 spin_lock_irqsave(&dev->event_lock, flags); 523 524 if (list_empty(&chan->nvsw.flip)) { 525 NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id); 526 spin_unlock_irqrestore(&dev->event_lock, flags); 527 return -EINVAL; 528 } 529 530 s = list_first_entry(&chan->nvsw.flip, 531 struct nouveau_page_flip_state, head); 532 if (s->event) { 533 struct drm_pending_vblank_event *e = s->event; 534 struct timeval now; 535 536 do_gettimeofday(&now); 537 e->event.sequence = 0; 538 e->event.tv_sec = now.tv_sec; 539 e->event.tv_usec = now.tv_usec; 540 list_add_tail(&e->base.link, &e->base.file_priv->event_list); 541 wake_up_interruptible(&e->base.file_priv->event_wait); 542 } 543 544 list_del(&s->head); 545 if (ps) 546 *ps = *s; 547 kfree(s); 548 549 spin_unlock_irqrestore(&dev->event_lock, flags); 550 return 0; 551 } 552 553 int 554 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev, 555 struct drm_mode_create_dumb *args) 556 { 557 struct nouveau_bo *bo; 558 int ret; 559 560 args->pitch = roundup(args->width * (args->bpp / 8), 256); 561 args->size = args->pitch * args->height; 562 args->size = roundup(args->size, PAGE_SIZE); 563 564 ret = nouveau_gem_new(dev, args->size, 0, TTM_PL_FLAG_VRAM, 0, 0, &bo); 565 if (ret) 566 return ret; 567 568 ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle); 569 drm_gem_object_unreference_unlocked(bo->gem); 570 return ret; 571 } 572 573 int 574 nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev, 575 uint32_t handle) 576 { 577 return drm_gem_handle_delete(file_priv, handle); 578 } 579 580 int 581 nouveau_display_dumb_map_offset(struct drm_file *file_priv, 582 struct drm_device *dev, 583 uint32_t handle, uint64_t *poffset) 584 { 585 struct drm_gem_object *gem; 586 587 gem = drm_gem_object_lookup(dev, file_priv, handle); 588 if (gem) { 589 struct nouveau_bo *bo = gem->driver_private; 590 *poffset = bo->bo.addr_space_offset; 591 drm_gem_object_unreference_unlocked(gem); 592 return 0; 593 } 594 595 return -ENOENT; 596 } 597