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_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 "nv50_display.h"
38 nouveau_user_framebuffer_destroy(struct drm_framebuffer
*drm_fb
)
40 struct nouveau_framebuffer
*fb
= nouveau_framebuffer(drm_fb
);
43 drm_gem_object_unreference_unlocked(fb
->nvbo
->gem
);
45 drm_framebuffer_cleanup(drm_fb
);
50 nouveau_user_framebuffer_create_handle(struct drm_framebuffer
*drm_fb
,
51 struct drm_file
*file_priv
,
54 struct nouveau_framebuffer
*fb
= nouveau_framebuffer(drm_fb
);
56 return drm_gem_handle_create(file_priv
, fb
->nvbo
->gem
, handle
);
59 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs
= {
60 .destroy
= nouveau_user_framebuffer_destroy
,
61 .create_handle
= nouveau_user_framebuffer_create_handle
,
65 nouveau_framebuffer_init(struct drm_device
*dev
,
66 struct nouveau_framebuffer
*nv_fb
,
67 struct drm_mode_fb_cmd
*mode_cmd
,
68 struct nouveau_bo
*nvbo
)
70 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
71 struct drm_framebuffer
*fb
= &nv_fb
->base
;
74 ret
= drm_framebuffer_init(dev
, fb
, &nouveau_framebuffer_funcs
);
79 drm_helper_mode_fill_fb_struct(fb
, mode_cmd
);
82 if (dev_priv
->card_type
>= NV_50
) {
83 u32 tile_flags
= nouveau_bo_tile_layout(nvbo
);
84 if (tile_flags
== 0x7a00 ||
86 nv_fb
->r_dma
= NvEvoFB32
;
88 if (tile_flags
== 0x7000)
89 nv_fb
->r_dma
= NvEvoFB16
;
91 nv_fb
->r_dma
= NvEvoVRAM_LP
;
94 case 8: nv_fb
->r_format
= NV50_EVO_CRTC_FB_DEPTH_8
; break;
95 case 15: nv_fb
->r_format
= NV50_EVO_CRTC_FB_DEPTH_15
; break;
96 case 16: nv_fb
->r_format
= NV50_EVO_CRTC_FB_DEPTH_16
; break;
98 case 32: nv_fb
->r_format
= NV50_EVO_CRTC_FB_DEPTH_24
; break;
99 case 30: nv_fb
->r_format
= NV50_EVO_CRTC_FB_DEPTH_30
; break;
101 NV_ERROR(dev
, "unknown depth %d\n", fb
->depth
);
105 if (dev_priv
->chipset
== 0x50)
106 nv_fb
->r_format
|= (tile_flags
<< 8);
109 nv_fb
->r_pitch
= 0x00100000 | fb
->pitch
;
111 u32 mode
= nvbo
->tile_mode
;
112 if (dev_priv
->card_type
>= NV_C0
)
114 nv_fb
->r_pitch
= ((fb
->pitch
/ 4) << 4) | mode
;
121 static struct drm_framebuffer
*
122 nouveau_user_framebuffer_create(struct drm_device
*dev
,
123 struct drm_file
*file_priv
,
124 struct drm_mode_fb_cmd
*mode_cmd
)
126 struct nouveau_framebuffer
*nouveau_fb
;
127 struct drm_gem_object
*gem
;
130 gem
= drm_gem_object_lookup(dev
, file_priv
, mode_cmd
->handle
);
132 return ERR_PTR(-ENOENT
);
134 nouveau_fb
= kzalloc(sizeof(struct nouveau_framebuffer
), GFP_KERNEL
);
136 return ERR_PTR(-ENOMEM
);
138 ret
= nouveau_framebuffer_init(dev
, nouveau_fb
, mode_cmd
, nouveau_gem_object(gem
));
140 drm_gem_object_unreference(gem
);
144 return &nouveau_fb
->base
;
147 const struct drm_mode_config_funcs nouveau_mode_config_funcs
= {
148 .fb_create
= nouveau_user_framebuffer_create
,
149 .output_poll_changed
= nouveau_fbcon_output_poll_changed
,
153 nouveau_vblank_enable(struct drm_device
*dev
, int crtc
)
155 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
157 if (dev_priv
->card_type
>= NV_50
)
158 nv_mask(dev
, NV50_PDISPLAY_INTR_EN_1
, 0,
159 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc
));
161 NVWriteCRTC(dev
, crtc
, NV_PCRTC_INTR_EN_0
,
162 NV_PCRTC_INTR_0_VBLANK
);
168 nouveau_vblank_disable(struct drm_device
*dev
, int crtc
)
170 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
172 if (dev_priv
->card_type
>= NV_50
)
173 nv_mask(dev
, NV50_PDISPLAY_INTR_EN_1
,
174 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc
), 0);
176 NVWriteCRTC(dev
, crtc
, NV_PCRTC_INTR_EN_0
, 0);
180 nouveau_page_flip_reserve(struct nouveau_bo
*old_bo
,
181 struct nouveau_bo
*new_bo
)
185 ret
= nouveau_bo_pin(new_bo
, TTM_PL_FLAG_VRAM
);
189 ret
= ttm_bo_reserve(&new_bo
->bo
, false, false, false, 0);
193 ret
= ttm_bo_reserve(&old_bo
->bo
, false, false, false, 0);
200 ttm_bo_unreserve(&new_bo
->bo
);
202 nouveau_bo_unpin(new_bo
);
207 nouveau_page_flip_unreserve(struct nouveau_bo
*old_bo
,
208 struct nouveau_bo
*new_bo
,
209 struct nouveau_fence
*fence
)
211 nouveau_bo_fence(new_bo
, fence
);
212 ttm_bo_unreserve(&new_bo
->bo
);
214 nouveau_bo_fence(old_bo
, fence
);
215 ttm_bo_unreserve(&old_bo
->bo
);
217 nouveau_bo_unpin(old_bo
);
221 nouveau_page_flip_emit(struct nouveau_channel
*chan
,
222 struct nouveau_bo
*old_bo
,
223 struct nouveau_bo
*new_bo
,
224 struct nouveau_page_flip_state
*s
,
225 struct nouveau_fence
**pfence
)
227 struct drm_nouveau_private
*dev_priv
= chan
->dev
->dev_private
;
228 struct drm_device
*dev
= chan
->dev
;
232 /* Queue it to the pending list */
233 spin_lock_irqsave(&dev
->event_lock
, flags
);
234 list_add_tail(&s
->head
, &chan
->nvsw
.flip
);
235 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
237 /* Synchronize with the old framebuffer */
238 ret
= nouveau_fence_sync(old_bo
->bo
.sync_obj
, chan
);
242 /* Emit the pageflip */
243 ret
= RING_SPACE(chan
, 2);
247 if (dev_priv
->card_type
< NV_C0
)
248 BEGIN_RING(chan
, NvSubSw
, NV_SW_PAGE_FLIP
, 1);
250 BEGIN_NVC0(chan
, 2, NvSubM2MF
, 0x0500, 1);
254 ret
= nouveau_fence_new(chan
, pfence
, true);
260 spin_lock_irqsave(&dev
->event_lock
, flags
);
262 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
267 nouveau_crtc_page_flip(struct drm_crtc
*crtc
, struct drm_framebuffer
*fb
,
268 struct drm_pending_vblank_event
*event
)
270 struct drm_device
*dev
= crtc
->dev
;
271 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
272 struct nouveau_bo
*old_bo
= nouveau_framebuffer(crtc
->fb
)->nvbo
;
273 struct nouveau_bo
*new_bo
= nouveau_framebuffer(fb
)->nvbo
;
274 struct nouveau_page_flip_state
*s
;
275 struct nouveau_channel
*chan
;
276 struct nouveau_fence
*fence
;
279 if (!dev_priv
->channel
)
282 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
286 /* Don't let the buffers go away while we flip */
287 ret
= nouveau_page_flip_reserve(old_bo
, new_bo
);
291 /* Initialize a page flip struct */
292 *s
= (struct nouveau_page_flip_state
)
293 { { }, event
, nouveau_crtc(crtc
)->index
,
294 fb
->bits_per_pixel
, fb
->pitch
, crtc
->x
, crtc
->y
,
297 /* Choose the channel the flip will be handled in */
298 chan
= nouveau_fence_channel(new_bo
->bo
.sync_obj
);
300 chan
= nouveau_channel_get_unlocked(dev_priv
->channel
);
301 mutex_lock(&chan
->mutex
);
303 /* Emit a page flip */
304 if (dev_priv
->card_type
>= NV_50
) {
305 ret
= nv50_display_flip_next(crtc
, fb
, chan
);
307 nouveau_channel_put(&chan
);
312 ret
= nouveau_page_flip_emit(chan
, old_bo
, new_bo
, s
, &fence
);
313 nouveau_channel_put(&chan
);
317 /* Update the crtc struct and cleanup */
320 nouveau_page_flip_unreserve(old_bo
, new_bo
, fence
);
321 nouveau_fence_unref(&fence
);
325 nouveau_page_flip_unreserve(old_bo
, new_bo
, NULL
);
332 nouveau_finish_page_flip(struct nouveau_channel
*chan
,
333 struct nouveau_page_flip_state
*ps
)
335 struct drm_device
*dev
= chan
->dev
;
336 struct nouveau_page_flip_state
*s
;
339 spin_lock_irqsave(&dev
->event_lock
, flags
);
341 if (list_empty(&chan
->nvsw
.flip
)) {
342 NV_ERROR(dev
, "Unexpected pageflip in channel %d.\n", chan
->id
);
343 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
347 s
= list_first_entry(&chan
->nvsw
.flip
,
348 struct nouveau_page_flip_state
, head
);
350 struct drm_pending_vblank_event
*e
= s
->event
;
353 do_gettimeofday(&now
);
354 e
->event
.sequence
= 0;
355 e
->event
.tv_sec
= now
.tv_sec
;
356 e
->event
.tv_usec
= now
.tv_usec
;
357 list_add_tail(&e
->base
.link
, &e
->base
.file_priv
->event_list
);
358 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
366 spin_unlock_irqrestore(&dev
->event_lock
, flags
);