2 * Copyright (C) 2008 Maarten Maathuis.
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:
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.
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.
28 #include <drm/drm_crtc_helper.h>
30 #include "nouveau_fbcon.h"
31 #include "dispnv04/hw.h"
32 #include "nouveau_crtc.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_gem.h"
35 #include "nouveau_connector.h"
36 #include "nv50_display.h"
38 #include "nouveau_fence.h"
40 #include <engine/disp.h>
42 #include <core/class.h>
45 nouveau_display_vblank_handler(void *data, int head)
47 struct nouveau_drm *drm = data;
48 drm_handle_vblank(drm->dev, head);
49 return NVKM_EVENT_KEEP;
53 nouveau_display_vblank_enable(struct drm_device *dev, int head)
55 struct nouveau_display *disp = nouveau_display(dev);
57 nouveau_event_get(disp->vblank[head]);
64 nouveau_display_vblank_disable(struct drm_device *dev, int head)
66 struct nouveau_display *disp = nouveau_display(dev);
68 nouveau_event_put(disp->vblank[head]);
72 nouveau_display_vblank_fini(struct drm_device *dev)
74 struct nouveau_display *disp = nouveau_display(dev);
78 for (i = 0; i < dev->mode_config.num_crtc; i++)
79 nouveau_event_ref(NULL, &disp->vblank[i]);
84 drm_vblank_cleanup(dev);
88 nouveau_display_vblank_init(struct drm_device *dev)
90 struct nouveau_display *disp = nouveau_display(dev);
91 struct nouveau_drm *drm = nouveau_drm(dev);
92 struct nouveau_disp *pdisp = nouveau_disp(drm->device);
95 disp->vblank = kzalloc(dev->mode_config.num_crtc *
96 sizeof(*disp->vblank), GFP_KERNEL);
100 for (i = 0; i < dev->mode_config.num_crtc; i++) {
101 ret = nouveau_event_new(pdisp->vblank, i,
102 nouveau_display_vblank_handler,
103 drm, &disp->vblank[i]);
105 nouveau_display_vblank_fini(dev);
110 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
112 nouveau_display_vblank_fini(dev);
120 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
122 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
125 drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
127 drm_framebuffer_cleanup(drm_fb);
132 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
133 struct drm_file *file_priv,
134 unsigned int *handle)
136 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
138 return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
141 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
142 .destroy = nouveau_user_framebuffer_destroy,
143 .create_handle = nouveau_user_framebuffer_create_handle,
147 nouveau_framebuffer_init(struct drm_device *dev,
148 struct nouveau_framebuffer *nv_fb,
149 struct drm_mode_fb_cmd2 *mode_cmd,
150 struct nouveau_bo *nvbo)
152 struct nouveau_drm *drm = nouveau_drm(dev);
153 struct drm_framebuffer *fb = &nv_fb->base;
156 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
159 if (nv_device(drm->device)->card_type >= NV_50) {
160 u32 tile_flags = nouveau_bo_tile_layout(nvbo);
161 if (tile_flags == 0x7a00 ||
162 tile_flags == 0xfe00)
163 nv_fb->r_dma = NvEvoFB32;
165 if (tile_flags == 0x7000)
166 nv_fb->r_dma = NvEvoFB16;
168 nv_fb->r_dma = NvEvoVRAM_LP;
171 case 8: nv_fb->r_format = 0x1e00; break;
172 case 15: nv_fb->r_format = 0xe900; break;
173 case 16: nv_fb->r_format = 0xe800; break;
175 case 32: nv_fb->r_format = 0xcf00; break;
176 case 30: nv_fb->r_format = 0xd100; break;
178 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
182 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
183 NV_ERROR(drm, "framebuffer requires contiguous bo\n");
187 if (nv_device(drm->device)->chipset == 0x50)
188 nv_fb->r_format |= (tile_flags << 8);
191 if (nv_device(drm->device)->card_type < NV_D0)
192 nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
194 nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
196 u32 mode = nvbo->tile_mode;
197 if (nv_device(drm->device)->card_type >= NV_C0)
199 nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
203 ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
211 static struct drm_framebuffer *
212 nouveau_user_framebuffer_create(struct drm_device *dev,
213 struct drm_file *file_priv,
214 struct drm_mode_fb_cmd2 *mode_cmd)
216 struct nouveau_framebuffer *nouveau_fb;
217 struct drm_gem_object *gem;
220 gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
222 return ERR_PTR(-ENOENT);
224 nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
228 ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
232 return &nouveau_fb->base;
237 drm_gem_object_unreference(gem);
241 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
242 .fb_create = nouveau_user_framebuffer_create,
243 .output_poll_changed = nouveau_fbcon_output_poll_changed,
247 struct nouveau_drm_prop_enum_list {
253 static struct nouveau_drm_prop_enum_list underscan[] = {
254 { 6, UNDERSCAN_AUTO, "auto" },
255 { 6, UNDERSCAN_OFF, "off" },
256 { 6, UNDERSCAN_ON, "on" },
260 static struct nouveau_drm_prop_enum_list dither_mode[] = {
261 { 7, DITHERING_MODE_AUTO, "auto" },
262 { 7, DITHERING_MODE_OFF, "off" },
263 { 1, DITHERING_MODE_ON, "on" },
264 { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
265 { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
266 { 4, DITHERING_MODE_TEMPORAL, "temporal" },
270 static struct nouveau_drm_prop_enum_list dither_depth[] = {
271 { 6, DITHERING_DEPTH_AUTO, "auto" },
272 { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
273 { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
277 #define PROP_ENUM(p,gen,n,list) do { \
278 struct nouveau_drm_prop_enum_list *l = (list); \
280 while (l->gen_mask) { \
281 if (l->gen_mask & (1 << (gen))) \
286 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
289 while (p && l->gen_mask) { \
290 if (l->gen_mask & (1 << (gen))) { \
291 drm_property_add_enum(p, c, l->type, l->name); \
300 nouveau_display_init(struct drm_device *dev)
302 struct nouveau_display *disp = nouveau_display(dev);
303 struct drm_connector *connector;
306 ret = disp->init(dev);
310 /* enable polling for external displays */
311 drm_kms_helper_poll_enable(dev);
313 /* enable hotplug interrupts */
314 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
315 struct nouveau_connector *conn = nouveau_connector(connector);
316 if (conn->hpd_func) nouveau_event_get(conn->hpd_func);
323 nouveau_display_fini(struct drm_device *dev)
325 struct nouveau_display *disp = nouveau_display(dev);
326 struct drm_connector *connector;
328 /* disable hotplug interrupts */
329 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
330 struct nouveau_connector *conn = nouveau_connector(connector);
331 if (conn->hpd_func) nouveau_event_put(conn->hpd_func);
334 drm_kms_helper_poll_disable(dev);
339 nouveau_display_create(struct drm_device *dev)
341 struct nouveau_drm *drm = nouveau_drm(dev);
342 struct nouveau_display *disp;
345 disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
349 drm_mode_config_init(dev);
350 drm_mode_create_scaling_mode_property(dev);
351 drm_mode_create_dvi_i_properties(dev);
353 if (nv_device(drm->device)->card_type < NV_50)
356 if (nv_device(drm->device)->card_type < NV_D0)
361 PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
362 PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
363 PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
365 disp->underscan_hborder_property =
366 drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
368 disp->underscan_vborder_property =
369 drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
373 disp->vibrant_hue_property =
374 drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
377 disp->color_vibrance_property =
378 drm_property_create_range(dev, 0, "color vibrance", 0, 200);
381 dev->mode_config.funcs = &nouveau_mode_config_funcs;
382 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
384 dev->mode_config.min_width = 0;
385 dev->mode_config.min_height = 0;
386 if (nv_device(drm->device)->card_type < NV_10) {
387 dev->mode_config.max_width = 2048;
388 dev->mode_config.max_height = 2048;
390 if (nv_device(drm->device)->card_type < NV_50) {
391 dev->mode_config.max_width = 4096;
392 dev->mode_config.max_height = 4096;
394 dev->mode_config.max_width = 8192;
395 dev->mode_config.max_height = 8192;
398 dev->mode_config.preferred_depth = 24;
399 dev->mode_config.prefer_shadow = 1;
401 if (nv_device(drm->device)->chipset < 0x11)
402 dev->mode_config.async_page_flip = false;
404 dev->mode_config.async_page_flip = true;
406 drm_kms_helper_poll_init(dev);
407 drm_kms_helper_poll_disable(dev);
409 if (drm->vbios.dcb.entries) {
410 if (nv_device(drm->device)->card_type < NV_50)
411 ret = nv04_display_create(dev);
413 ret = nv50_display_create(dev);
419 goto disp_create_err;
421 if (dev->mode_config.num_crtc) {
422 ret = nouveau_display_vblank_init(dev);
427 nouveau_backlight_init(dev);
433 drm_kms_helper_poll_fini(dev);
434 drm_mode_config_cleanup(dev);
439 nouveau_display_destroy(struct drm_device *dev)
441 struct nouveau_display *disp = nouveau_display(dev);
443 nouveau_backlight_exit(dev);
444 nouveau_display_vblank_fini(dev);
446 drm_kms_helper_poll_fini(dev);
447 drm_mode_config_cleanup(dev);
452 nouveau_drm(dev)->display = NULL;
457 nouveau_display_suspend(struct drm_device *dev)
459 struct nouveau_drm *drm = nouveau_drm(dev);
460 struct drm_crtc *crtc;
462 nouveau_display_fini(dev);
464 NV_INFO(drm, "unpinning framebuffer(s)...\n");
465 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
466 struct nouveau_framebuffer *nouveau_fb;
468 nouveau_fb = nouveau_framebuffer(crtc->fb);
469 if (!nouveau_fb || !nouveau_fb->nvbo)
472 nouveau_bo_unpin(nouveau_fb->nvbo);
475 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
476 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
478 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
479 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
486 nouveau_display_repin(struct drm_device *dev)
488 struct nouveau_drm *drm = nouveau_drm(dev);
489 struct drm_crtc *crtc;
492 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
493 struct nouveau_framebuffer *nouveau_fb;
495 nouveau_fb = nouveau_framebuffer(crtc->fb);
496 if (!nouveau_fb || !nouveau_fb->nvbo)
499 nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM);
502 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
503 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
505 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
507 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
509 NV_ERROR(drm, "Could not pin/map cursor.\n");
514 nouveau_display_resume(struct drm_device *dev)
516 struct drm_crtc *crtc;
517 nouveau_display_init(dev);
519 /* Force CLUT to get re-loaded during modeset */
520 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
521 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
523 nv_crtc->lut.depth = 0;
526 drm_helper_resume_force_mode(dev);
528 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
529 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
530 u32 offset = nv_crtc->cursor.nvbo->bo.offset;
532 nv_crtc->cursor.set_offset(nv_crtc, offset);
533 nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
534 nv_crtc->cursor_saved_y);
539 nouveau_page_flip_emit(struct nouveau_channel *chan,
540 struct nouveau_bo *old_bo,
541 struct nouveau_bo *new_bo,
542 struct nouveau_page_flip_state *s,
543 struct nouveau_fence **pfence)
545 struct nouveau_fence_chan *fctx = chan->fence;
546 struct nouveau_drm *drm = chan->drm;
547 struct drm_device *dev = drm->dev;
551 /* Queue it to the pending list */
552 spin_lock_irqsave(&dev->event_lock, flags);
553 list_add_tail(&s->head, &fctx->flip);
554 spin_unlock_irqrestore(&dev->event_lock, flags);
556 /* Synchronize with the old framebuffer */
557 ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
561 /* Emit the pageflip */
562 ret = RING_SPACE(chan, 2);
566 if (nv_device(drm->device)->card_type < NV_C0)
567 BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
569 BEGIN_NVC0(chan, FermiSw, NV_SW_PAGE_FLIP, 1);
570 OUT_RING (chan, 0x00000000);
573 ret = nouveau_fence_new(chan, false, pfence);
579 spin_lock_irqsave(&dev->event_lock, flags);
581 spin_unlock_irqrestore(&dev->event_lock, flags);
586 nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
587 struct drm_pending_vblank_event *event, u32 flags)
589 const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
590 struct drm_device *dev = crtc->dev;
591 struct nouveau_drm *drm = nouveau_drm(dev);
592 struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
593 struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
594 struct nouveau_page_flip_state *s;
595 struct nouveau_channel *chan = drm->channel;
596 struct nouveau_fence *fence;
602 s = kzalloc(sizeof(*s), GFP_KERNEL);
606 /* synchronise rendering channel with the kernel's channel */
607 spin_lock(&new_bo->bo.bdev->fence_lock);
608 fence = nouveau_fence_ref(new_bo->bo.sync_obj);
609 spin_unlock(&new_bo->bo.bdev->fence_lock);
610 ret = nouveau_fence_sync(fence, chan);
611 nouveau_fence_unref(&fence);
615 if (new_bo != old_bo) {
616 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
621 mutex_lock(&chan->cli->mutex);
622 ret = ttm_bo_reserve(&old_bo->bo, true, false, false, NULL);
626 /* Initialize a page flip struct */
627 *s = (struct nouveau_page_flip_state)
628 { { }, event, nouveau_crtc(crtc)->index,
629 fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
632 /* Emit a page flip */
633 if (nv_device(drm->device)->card_type >= NV_50) {
634 ret = nv50_display_flip_next(crtc, fb, chan, swap_interval);
638 struct nv04_display *dispnv04 = nv04_display(dev);
639 int head = nouveau_crtc(crtc)->index;
642 ret = RING_SPACE(chan, 8);
646 BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
648 BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
649 OUT_RING (chan, head);
650 BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
652 BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
656 nouveau_bo_ref(new_bo, &dispnv04->image[head]);
659 ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
660 mutex_unlock(&chan->cli->mutex);
664 /* Update the crtc struct and cleanup */
667 nouveau_bo_fence(old_bo, fence);
668 ttm_bo_unreserve(&old_bo->bo);
669 if (old_bo != new_bo)
670 nouveau_bo_unpin(old_bo);
671 nouveau_fence_unref(&fence);
675 ttm_bo_unreserve(&old_bo->bo);
677 mutex_unlock(&chan->cli->mutex);
678 if (old_bo != new_bo)
679 nouveau_bo_unpin(new_bo);
686 nouveau_finish_page_flip(struct nouveau_channel *chan,
687 struct nouveau_page_flip_state *ps)
689 struct nouveau_fence_chan *fctx = chan->fence;
690 struct nouveau_drm *drm = chan->drm;
691 struct drm_device *dev = drm->dev;
692 struct nouveau_page_flip_state *s;
695 spin_lock_irqsave(&dev->event_lock, flags);
697 if (list_empty(&fctx->flip)) {
698 NV_ERROR(drm, "unexpected pageflip\n");
699 spin_unlock_irqrestore(&dev->event_lock, flags);
703 s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
705 drm_send_vblank_event(dev, s->crtc, s->event);
712 spin_unlock_irqrestore(&dev->event_lock, flags);
717 nouveau_flip_complete(void *data)
719 struct nouveau_channel *chan = data;
720 struct nouveau_drm *drm = chan->drm;
721 struct nouveau_page_flip_state state;
723 if (!nouveau_finish_page_flip(chan, &state)) {
724 if (nv_device(drm->device)->card_type < NV_50) {
725 nv_set_crtc_base(drm->dev, state.crtc, state.offset +
726 state.y * state.pitch +
727 state.x * state.bpp / 8);
735 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
736 struct drm_mode_create_dumb *args)
738 struct nouveau_bo *bo;
741 args->pitch = roundup(args->width * (args->bpp / 8), 256);
742 args->size = args->pitch * args->height;
743 args->size = roundup(args->size, PAGE_SIZE);
745 ret = nouveau_gem_new(dev, args->size, 0, NOUVEAU_GEM_DOMAIN_VRAM, 0, 0, &bo);
749 ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
750 drm_gem_object_unreference_unlocked(&bo->gem);
755 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
756 struct drm_device *dev,
757 uint32_t handle, uint64_t *poffset)
759 struct drm_gem_object *gem;
761 gem = drm_gem_object_lookup(dev, file_priv, handle);
763 struct nouveau_bo *bo = nouveau_gem_object(gem);
764 *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
765 drm_gem_object_unreference_unlocked(gem);