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"
37 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
39 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
42 drm_gem_object_unreference_unlocked(fb->nvbo->gem);
44 drm_framebuffer_cleanup(drm_fb);
49 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
50 struct drm_file *file_priv,
53 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
55 return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
58 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
59 .destroy = nouveau_user_framebuffer_destroy,
60 .create_handle = nouveau_user_framebuffer_create_handle,
64 nouveau_framebuffer_init(struct drm_device *dev, struct nouveau_framebuffer *nouveau_fb,
65 struct drm_mode_fb_cmd *mode_cmd, struct nouveau_bo *nvbo)
69 ret = drm_framebuffer_init(dev, &nouveau_fb->base, &nouveau_framebuffer_funcs);
74 drm_helper_mode_fill_fb_struct(&nouveau_fb->base, mode_cmd);
75 nouveau_fb->nvbo = nvbo;
79 static struct drm_framebuffer *
80 nouveau_user_framebuffer_create(struct drm_device *dev,
81 struct drm_file *file_priv,
82 struct drm_mode_fb_cmd *mode_cmd)
84 struct nouveau_framebuffer *nouveau_fb;
85 struct drm_gem_object *gem;
88 gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
90 return ERR_PTR(-ENOENT);
92 nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
94 return ERR_PTR(-ENOMEM);
96 ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
98 drm_gem_object_unreference(gem);
102 return &nouveau_fb->base;
105 const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
106 .fb_create = nouveau_user_framebuffer_create,
107 .output_poll_changed = nouveau_fbcon_output_poll_changed,
111 nouveau_vblank_enable(struct drm_device *dev, int crtc)
113 struct drm_nouveau_private *dev_priv = dev->dev_private;
115 if (dev_priv->card_type >= NV_50)
116 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
117 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
119 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
120 NV_PCRTC_INTR_0_VBLANK);
126 nouveau_vblank_disable(struct drm_device *dev, int crtc)
128 struct drm_nouveau_private *dev_priv = dev->dev_private;
130 if (dev_priv->card_type >= NV_50)
131 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
132 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
134 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
138 nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
139 struct nouveau_bo *new_bo)
143 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
147 ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
151 ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
158 ttm_bo_unreserve(&new_bo->bo);
160 nouveau_bo_unpin(new_bo);
165 nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
166 struct nouveau_bo *new_bo,
167 struct nouveau_fence *fence)
169 nouveau_bo_fence(new_bo, fence);
170 ttm_bo_unreserve(&new_bo->bo);
172 nouveau_bo_fence(old_bo, fence);
173 ttm_bo_unreserve(&old_bo->bo);
175 nouveau_bo_unpin(old_bo);
179 nouveau_page_flip_emit(struct nouveau_channel *chan,
180 struct nouveau_bo *old_bo,
181 struct nouveau_bo *new_bo,
182 struct nouveau_page_flip_state *s,
183 struct nouveau_fence **pfence)
185 struct drm_device *dev = chan->dev;
189 /* Queue it to the pending list */
190 spin_lock_irqsave(&dev->event_lock, flags);
191 list_add_tail(&s->head, &chan->nvsw.flip);
192 spin_unlock_irqrestore(&dev->event_lock, flags);
194 /* Synchronize with the old framebuffer */
195 ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
199 /* Emit the pageflip */
200 ret = RING_SPACE(chan, 2);
204 BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
208 ret = nouveau_fence_new(chan, pfence, true);
214 spin_lock_irqsave(&dev->event_lock, flags);
216 spin_unlock_irqrestore(&dev->event_lock, flags);
221 nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
222 struct drm_pending_vblank_event *event)
224 struct drm_device *dev = crtc->dev;
225 struct drm_nouveau_private *dev_priv = dev->dev_private;
226 struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
227 struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
228 struct nouveau_page_flip_state *s;
229 struct nouveau_channel *chan;
230 struct nouveau_fence *fence;
233 if (dev_priv->engine.graph.accel_blocked)
236 s = kzalloc(sizeof(*s), GFP_KERNEL);
240 /* Don't let the buffers go away while we flip */
241 ret = nouveau_page_flip_reserve(old_bo, new_bo);
245 /* Initialize a page flip struct */
246 *s = (struct nouveau_page_flip_state)
247 { { }, s->event, nouveau_crtc(crtc)->index,
248 fb->bits_per_pixel, fb->pitch, crtc->x, crtc->y,
251 /* Choose the channel the flip will be handled in */
252 chan = nouveau_fence_channel(new_bo->bo.sync_obj);
254 chan = nouveau_channel_get_unlocked(dev_priv->channel);
255 mutex_lock(&chan->mutex);
257 /* Emit a page flip */
258 ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
259 nouveau_channel_put(&chan);
263 /* Update the crtc struct and cleanup */
266 nouveau_page_flip_unreserve(old_bo, new_bo, fence);
267 nouveau_fence_unref(&fence);
271 nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
278 nouveau_finish_page_flip(struct nouveau_channel *chan,
279 struct nouveau_page_flip_state *ps)
281 struct drm_device *dev = chan->dev;
282 struct nouveau_page_flip_state *s;
285 spin_lock_irqsave(&dev->event_lock, flags);
287 if (list_empty(&chan->nvsw.flip)) {
288 NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
289 spin_unlock_irqrestore(&dev->event_lock, flags);
293 s = list_first_entry(&chan->nvsw.flip,
294 struct nouveau_page_flip_state, head);
296 struct drm_pending_vblank_event *e = s->event;
299 do_gettimeofday(&now);
300 e->event.sequence = 0;
301 e->event.tv_sec = now.tv_sec;
302 e->event.tv_usec = now.tv_usec;
303 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
304 wake_up_interruptible(&e->base.file_priv->event_wait);
311 spin_unlock_irqrestore(&dev->event_lock, flags);