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 <subdev/bios/gpio.h>
41 #include <subdev/gpio.h>
42 #include <engine/disp.h>
44 #include <core/class.h>
47 nouveau_user_framebuffer_destroy(struct drm_framebuffer
*drm_fb
)
49 struct nouveau_framebuffer
*fb
= nouveau_framebuffer(drm_fb
);
52 drm_gem_object_unreference_unlocked(fb
->nvbo
->gem
);
54 drm_framebuffer_cleanup(drm_fb
);
59 nouveau_user_framebuffer_create_handle(struct drm_framebuffer
*drm_fb
,
60 struct drm_file
*file_priv
,
63 struct nouveau_framebuffer
*fb
= nouveau_framebuffer(drm_fb
);
65 return drm_gem_handle_create(file_priv
, fb
->nvbo
->gem
, handle
);
68 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs
= {
69 .destroy
= nouveau_user_framebuffer_destroy
,
70 .create_handle
= nouveau_user_framebuffer_create_handle
,
74 nouveau_framebuffer_init(struct drm_device
*dev
,
75 struct nouveau_framebuffer
*nv_fb
,
76 struct drm_mode_fb_cmd2
*mode_cmd
,
77 struct nouveau_bo
*nvbo
)
79 struct nouveau_drm
*drm
= nouveau_drm(dev
);
80 struct drm_framebuffer
*fb
= &nv_fb
->base
;
83 drm_helper_mode_fill_fb_struct(fb
, mode_cmd
);
86 if (nv_device(drm
->device
)->card_type
>= NV_50
) {
87 u32 tile_flags
= nouveau_bo_tile_layout(nvbo
);
88 if (tile_flags
== 0x7a00 ||
90 nv_fb
->r_dma
= NvEvoFB32
;
92 if (tile_flags
== 0x7000)
93 nv_fb
->r_dma
= NvEvoFB16
;
95 nv_fb
->r_dma
= NvEvoVRAM_LP
;
98 case 8: nv_fb
->r_format
= 0x1e00; break;
99 case 15: nv_fb
->r_format
= 0xe900; break;
100 case 16: nv_fb
->r_format
= 0xe800; break;
102 case 32: nv_fb
->r_format
= 0xcf00; break;
103 case 30: nv_fb
->r_format
= 0xd100; break;
105 NV_ERROR(drm
, "unknown depth %d\n", fb
->depth
);
109 if (nv_device(drm
->device
)->chipset
== 0x50)
110 nv_fb
->r_format
|= (tile_flags
<< 8);
113 if (nv_device(drm
->device
)->card_type
< NV_D0
)
114 nv_fb
->r_pitch
= 0x00100000 | fb
->pitches
[0];
116 nv_fb
->r_pitch
= 0x01000000 | fb
->pitches
[0];
118 u32 mode
= nvbo
->tile_mode
;
119 if (nv_device(drm
->device
)->card_type
>= NV_C0
)
121 nv_fb
->r_pitch
= ((fb
->pitches
[0] / 4) << 4) | mode
;
125 ret
= drm_framebuffer_init(dev
, fb
, &nouveau_framebuffer_funcs
);
133 static struct drm_framebuffer
*
134 nouveau_user_framebuffer_create(struct drm_device
*dev
,
135 struct drm_file
*file_priv
,
136 struct drm_mode_fb_cmd2
*mode_cmd
)
138 struct nouveau_framebuffer
*nouveau_fb
;
139 struct drm_gem_object
*gem
;
142 gem
= drm_gem_object_lookup(dev
, file_priv
, mode_cmd
->handles
[0]);
144 return ERR_PTR(-ENOENT
);
146 nouveau_fb
= kzalloc(sizeof(struct nouveau_framebuffer
), GFP_KERNEL
);
148 return ERR_PTR(-ENOMEM
);
150 ret
= nouveau_framebuffer_init(dev
, nouveau_fb
, mode_cmd
, nouveau_gem_object(gem
));
152 drm_gem_object_unreference(gem
);
156 return &nouveau_fb
->base
;
159 static const struct drm_mode_config_funcs nouveau_mode_config_funcs
= {
160 .fb_create
= nouveau_user_framebuffer_create
,
161 .output_poll_changed
= nouveau_fbcon_output_poll_changed
,
165 struct nouveau_drm_prop_enum_list
{
171 static struct nouveau_drm_prop_enum_list underscan
[] = {
172 { 6, UNDERSCAN_AUTO
, "auto" },
173 { 6, UNDERSCAN_OFF
, "off" },
174 { 6, UNDERSCAN_ON
, "on" },
178 static struct nouveau_drm_prop_enum_list dither_mode
[] = {
179 { 7, DITHERING_MODE_AUTO
, "auto" },
180 { 7, DITHERING_MODE_OFF
, "off" },
181 { 1, DITHERING_MODE_ON
, "on" },
182 { 6, DITHERING_MODE_STATIC2X2
, "static 2x2" },
183 { 6, DITHERING_MODE_DYNAMIC2X2
, "dynamic 2x2" },
184 { 4, DITHERING_MODE_TEMPORAL
, "temporal" },
188 static struct nouveau_drm_prop_enum_list dither_depth
[] = {
189 { 6, DITHERING_DEPTH_AUTO
, "auto" },
190 { 6, DITHERING_DEPTH_6BPC
, "6 bpc" },
191 { 6, DITHERING_DEPTH_8BPC
, "8 bpc" },
195 #define PROP_ENUM(p,gen,n,list) do { \
196 struct nouveau_drm_prop_enum_list *l = (list); \
198 while (l->gen_mask) { \
199 if (l->gen_mask & (1 << (gen))) \
204 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
207 while (p && l->gen_mask) { \
208 if (l->gen_mask & (1 << (gen))) { \
209 drm_property_add_enum(p, c, l->type, l->name); \
218 nouveau_display_init(struct drm_device
*dev
)
220 struct nouveau_drm
*drm
= nouveau_drm(dev
);
221 struct nouveau_display
*disp
= nouveau_display(dev
);
222 struct nouveau_gpio
*gpio
= nouveau_gpio(drm
->device
);
223 struct drm_connector
*connector
;
226 ret
= disp
->init(dev
);
230 /* enable polling for external displays */
231 drm_kms_helper_poll_enable(dev
);
233 /* enable hotplug interrupts */
234 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
235 struct nouveau_connector
*conn
= nouveau_connector(connector
);
236 if (gpio
&& conn
->hpd
.func
!= DCB_GPIO_UNUSED
) {
237 nouveau_event_get(gpio
->events
, conn
->hpd
.line
,
246 nouveau_display_fini(struct drm_device
*dev
)
248 struct nouveau_drm
*drm
= nouveau_drm(dev
);
249 struct nouveau_display
*disp
= nouveau_display(dev
);
250 struct nouveau_gpio
*gpio
= nouveau_gpio(drm
->device
);
251 struct drm_connector
*connector
;
253 /* disable hotplug interrupts */
254 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
255 struct nouveau_connector
*conn
= nouveau_connector(connector
);
256 if (gpio
&& conn
->hpd
.func
!= DCB_GPIO_UNUSED
) {
257 nouveau_event_put(gpio
->events
, conn
->hpd
.line
,
262 drm_kms_helper_poll_disable(dev
);
267 nouveau_display_create(struct drm_device
*dev
)
269 struct nouveau_drm
*drm
= nouveau_drm(dev
);
270 struct nouveau_display
*disp
;
271 u32 pclass
= dev
->pdev
->class >> 8;
274 disp
= drm
->display
= kzalloc(sizeof(*disp
), GFP_KERNEL
);
278 drm_mode_config_init(dev
);
279 drm_mode_create_scaling_mode_property(dev
);
280 drm_mode_create_dvi_i_properties(dev
);
282 if (nv_device(drm
->device
)->card_type
< NV_50
)
285 if (nv_device(drm
->device
)->card_type
< NV_D0
)
290 PROP_ENUM(disp
->dithering_mode
, gen
, "dithering mode", dither_mode
);
291 PROP_ENUM(disp
->dithering_depth
, gen
, "dithering depth", dither_depth
);
292 PROP_ENUM(disp
->underscan_property
, gen
, "underscan", underscan
);
294 disp
->underscan_hborder_property
=
295 drm_property_create_range(dev
, 0, "underscan hborder", 0, 128);
297 disp
->underscan_vborder_property
=
298 drm_property_create_range(dev
, 0, "underscan vborder", 0, 128);
302 disp
->vibrant_hue_property
=
303 drm_property_create_range(dev
, 0, "vibrant hue", 0, 180);
306 disp
->color_vibrance_property
=
307 drm_property_create_range(dev
, 0, "color vibrance", 0, 200);
310 dev
->mode_config
.funcs
= &nouveau_mode_config_funcs
;
311 dev
->mode_config
.fb_base
= pci_resource_start(dev
->pdev
, 1);
313 dev
->mode_config
.min_width
= 0;
314 dev
->mode_config
.min_height
= 0;
315 if (nv_device(drm
->device
)->card_type
< NV_10
) {
316 dev
->mode_config
.max_width
= 2048;
317 dev
->mode_config
.max_height
= 2048;
319 if (nv_device(drm
->device
)->card_type
< NV_50
) {
320 dev
->mode_config
.max_width
= 4096;
321 dev
->mode_config
.max_height
= 4096;
323 dev
->mode_config
.max_width
= 8192;
324 dev
->mode_config
.max_height
= 8192;
327 dev
->mode_config
.preferred_depth
= 24;
328 dev
->mode_config
.prefer_shadow
= 1;
330 drm_kms_helper_poll_init(dev
);
331 drm_kms_helper_poll_disable(dev
);
333 if (nouveau_modeset
== 1 ||
334 (nouveau_modeset
< 0 && pclass
== PCI_CLASS_DISPLAY_VGA
)) {
335 if (nv_device(drm
->device
)->card_type
< NV_50
)
336 ret
= nv04_display_create(dev
);
338 ret
= nv50_display_create(dev
);
340 goto disp_create_err
;
342 if (dev
->mode_config
.num_crtc
) {
343 ret
= drm_vblank_init(dev
, dev
->mode_config
.num_crtc
);
348 nouveau_backlight_init(dev
);
356 drm_kms_helper_poll_fini(dev
);
357 drm_mode_config_cleanup(dev
);
362 nouveau_display_destroy(struct drm_device
*dev
)
364 struct nouveau_display
*disp
= nouveau_display(dev
);
366 nouveau_backlight_exit(dev
);
367 drm_vblank_cleanup(dev
);
369 drm_kms_helper_poll_fini(dev
);
370 drm_mode_config_cleanup(dev
);
375 nouveau_drm(dev
)->display
= NULL
;
380 nouveau_display_suspend(struct drm_device
*dev
)
382 struct nouveau_drm
*drm
= nouveau_drm(dev
);
383 struct drm_crtc
*crtc
;
385 nouveau_display_fini(dev
);
387 NV_INFO(drm
, "unpinning framebuffer(s)...\n");
388 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
389 struct nouveau_framebuffer
*nouveau_fb
;
391 nouveau_fb
= nouveau_framebuffer(crtc
->fb
);
392 if (!nouveau_fb
|| !nouveau_fb
->nvbo
)
395 nouveau_bo_unpin(nouveau_fb
->nvbo
);
398 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
399 struct nouveau_crtc
*nv_crtc
= nouveau_crtc(crtc
);
401 nouveau_bo_unmap(nv_crtc
->cursor
.nvbo
);
402 nouveau_bo_unpin(nv_crtc
->cursor
.nvbo
);
409 nouveau_display_resume(struct drm_device
*dev
)
411 struct nouveau_drm
*drm
= nouveau_drm(dev
);
412 struct drm_crtc
*crtc
;
415 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
416 struct nouveau_framebuffer
*nouveau_fb
;
418 nouveau_fb
= nouveau_framebuffer(crtc
->fb
);
419 if (!nouveau_fb
|| !nouveau_fb
->nvbo
)
422 nouveau_bo_pin(nouveau_fb
->nvbo
, TTM_PL_FLAG_VRAM
);
425 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
426 struct nouveau_crtc
*nv_crtc
= nouveau_crtc(crtc
);
428 ret
= nouveau_bo_pin(nv_crtc
->cursor
.nvbo
, TTM_PL_FLAG_VRAM
);
430 ret
= nouveau_bo_map(nv_crtc
->cursor
.nvbo
);
432 NV_ERROR(drm
, "Could not pin/map cursor.\n");
435 nouveau_fbcon_set_suspend(dev
, 0);
436 nouveau_fbcon_zfill_all(dev
);
438 nouveau_display_init(dev
);
440 /* Force CLUT to get re-loaded during modeset */
441 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
442 struct nouveau_crtc
*nv_crtc
= nouveau_crtc(crtc
);
444 nv_crtc
->lut
.depth
= 0;
447 drm_helper_resume_force_mode(dev
);
449 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
450 struct nouveau_crtc
*nv_crtc
= nouveau_crtc(crtc
);
451 u32 offset
= nv_crtc
->cursor
.nvbo
->bo
.offset
;
453 nv_crtc
->cursor
.set_offset(nv_crtc
, offset
);
454 nv_crtc
->cursor
.set_pos(nv_crtc
, nv_crtc
->cursor_saved_x
,
455 nv_crtc
->cursor_saved_y
);
460 nouveau_page_flip_reserve(struct nouveau_bo
*old_bo
,
461 struct nouveau_bo
*new_bo
)
465 ret
= nouveau_bo_pin(new_bo
, TTM_PL_FLAG_VRAM
);
469 ret
= ttm_bo_reserve(&new_bo
->bo
, false, false, false, 0);
473 if (likely(old_bo
!= new_bo
)) {
474 ret
= ttm_bo_reserve(&old_bo
->bo
, false, false, false, 0);
482 ttm_bo_unreserve(&new_bo
->bo
);
484 nouveau_bo_unpin(new_bo
);
489 nouveau_page_flip_unreserve(struct nouveau_bo
*old_bo
,
490 struct nouveau_bo
*new_bo
,
491 struct nouveau_fence
*fence
)
493 nouveau_bo_fence(new_bo
, fence
);
494 ttm_bo_unreserve(&new_bo
->bo
);
496 if (likely(old_bo
!= new_bo
)) {
497 nouveau_bo_fence(old_bo
, fence
);
498 ttm_bo_unreserve(&old_bo
->bo
);
501 nouveau_bo_unpin(old_bo
);
505 nouveau_page_flip_emit(struct nouveau_channel
*chan
,
506 struct nouveau_bo
*old_bo
,
507 struct nouveau_bo
*new_bo
,
508 struct nouveau_page_flip_state
*s
,
509 struct nouveau_fence
**pfence
)
511 struct nouveau_fence_chan
*fctx
= chan
->fence
;
512 struct nouveau_drm
*drm
= chan
->drm
;
513 struct drm_device
*dev
= drm
->dev
;
517 /* Queue it to the pending list */
518 spin_lock_irqsave(&dev
->event_lock
, flags
);
519 list_add_tail(&s
->head
, &fctx
->flip
);
520 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
522 /* Synchronize with the old framebuffer */
523 ret
= nouveau_fence_sync(old_bo
->bo
.sync_obj
, chan
);
527 /* Emit the pageflip */
528 ret
= RING_SPACE(chan
, 3);
532 if (nv_device(drm
->device
)->card_type
< NV_C0
) {
533 BEGIN_NV04(chan
, NvSubSw
, NV_SW_PAGE_FLIP
, 1);
534 OUT_RING (chan
, 0x00000000);
535 OUT_RING (chan
, 0x00000000);
537 BEGIN_NVC0(chan
, 0, NV10_SUBCHAN_REF_CNT
, 1);
539 BEGIN_IMC0(chan
, 0, NVSW_SUBCHAN_PAGE_FLIP
, 0x0000);
543 ret
= nouveau_fence_new(chan
, false, pfence
);
549 spin_lock_irqsave(&dev
->event_lock
, flags
);
551 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
556 nouveau_crtc_page_flip(struct drm_crtc
*crtc
, struct drm_framebuffer
*fb
,
557 struct drm_pending_vblank_event
*event
)
559 struct drm_device
*dev
= crtc
->dev
;
560 struct nouveau_drm
*drm
= nouveau_drm(dev
);
561 struct nouveau_bo
*old_bo
= nouveau_framebuffer(crtc
->fb
)->nvbo
;
562 struct nouveau_bo
*new_bo
= nouveau_framebuffer(fb
)->nvbo
;
563 struct nouveau_page_flip_state
*s
;
564 struct nouveau_channel
*chan
= NULL
;
565 struct nouveau_fence
*fence
;
571 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
575 /* Don't let the buffers go away while we flip */
576 ret
= nouveau_page_flip_reserve(old_bo
, new_bo
);
580 /* Initialize a page flip struct */
581 *s
= (struct nouveau_page_flip_state
)
582 { { }, event
, nouveau_crtc(crtc
)->index
,
583 fb
->bits_per_pixel
, fb
->pitches
[0], crtc
->x
, crtc
->y
,
586 /* Choose the channel the flip will be handled in */
587 fence
= new_bo
->bo
.sync_obj
;
589 chan
= fence
->channel
;
592 mutex_lock(&chan
->cli
->mutex
);
594 /* Emit a page flip */
595 if (nv_device(drm
->device
)->card_type
>= NV_50
) {
596 ret
= nv50_display_flip_next(crtc
, fb
, chan
, 0);
598 mutex_unlock(&chan
->cli
->mutex
);
603 ret
= nouveau_page_flip_emit(chan
, old_bo
, new_bo
, s
, &fence
);
604 mutex_unlock(&chan
->cli
->mutex
);
608 /* Update the crtc struct and cleanup */
611 nouveau_page_flip_unreserve(old_bo
, new_bo
, fence
);
612 nouveau_fence_unref(&fence
);
616 nouveau_page_flip_unreserve(old_bo
, new_bo
, NULL
);
623 nouveau_finish_page_flip(struct nouveau_channel
*chan
,
624 struct nouveau_page_flip_state
*ps
)
626 struct nouveau_fence_chan
*fctx
= chan
->fence
;
627 struct nouveau_drm
*drm
= chan
->drm
;
628 struct drm_device
*dev
= drm
->dev
;
629 struct nouveau_page_flip_state
*s
;
632 spin_lock_irqsave(&dev
->event_lock
, flags
);
634 if (list_empty(&fctx
->flip
)) {
635 NV_ERROR(drm
, "unexpected pageflip\n");
636 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
640 s
= list_first_entry(&fctx
->flip
, struct nouveau_page_flip_state
, head
);
642 drm_send_vblank_event(dev
, -1, s
->event
);
649 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
654 nouveau_flip_complete(void *data
)
656 struct nouveau_channel
*chan
= data
;
657 struct nouveau_drm
*drm
= chan
->drm
;
658 struct nouveau_page_flip_state state
;
660 if (!nouveau_finish_page_flip(chan
, &state
)) {
661 if (nv_device(drm
->device
)->card_type
< NV_50
) {
662 nv_set_crtc_base(drm
->dev
, state
.crtc
, state
.offset
+
663 state
.y
* state
.pitch
+
664 state
.x
* state
.bpp
/ 8);
672 nouveau_display_dumb_create(struct drm_file
*file_priv
, struct drm_device
*dev
,
673 struct drm_mode_create_dumb
*args
)
675 struct nouveau_bo
*bo
;
678 args
->pitch
= roundup(args
->width
* (args
->bpp
/ 8), 256);
679 args
->size
= args
->pitch
* args
->height
;
680 args
->size
= roundup(args
->size
, PAGE_SIZE
);
682 ret
= nouveau_gem_new(dev
, args
->size
, 0, NOUVEAU_GEM_DOMAIN_VRAM
, 0, 0, &bo
);
686 ret
= drm_gem_handle_create(file_priv
, bo
->gem
, &args
->handle
);
687 drm_gem_object_unreference_unlocked(bo
->gem
);
692 nouveau_display_dumb_destroy(struct drm_file
*file_priv
, struct drm_device
*dev
,
695 return drm_gem_handle_delete(file_priv
, handle
);
699 nouveau_display_dumb_map_offset(struct drm_file
*file_priv
,
700 struct drm_device
*dev
,
701 uint32_t handle
, uint64_t *poffset
)
703 struct drm_gem_object
*gem
;
705 gem
= drm_gem_object_lookup(dev
, file_priv
, handle
);
707 struct nouveau_bo
*bo
= gem
->driver_private
;
708 *poffset
= bo
->bo
.addr_space_offset
;
709 drm_gem_object_unreference_unlocked(gem
);