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 "nouveau_connector.h"
36 #include "nv50_display.h"
39 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
41 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
44 drm_gem_object_unreference_unlocked(fb->nvbo->gem);
46 drm_framebuffer_cleanup(drm_fb);
51 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
52 struct drm_file *file_priv,
55 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
57 return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
60 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
61 .destroy = nouveau_user_framebuffer_destroy,
62 .create_handle = nouveau_user_framebuffer_create_handle,
66 nouveau_framebuffer_init(struct drm_device *dev,
67 struct nouveau_framebuffer *nv_fb,
68 struct drm_mode_fb_cmd2 *mode_cmd,
69 struct nouveau_bo *nvbo)
71 struct drm_nouveau_private *dev_priv = dev->dev_private;
72 struct drm_framebuffer *fb = &nv_fb->base;
75 ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
80 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
83 if (dev_priv->card_type >= NV_50) {
84 u32 tile_flags = nouveau_bo_tile_layout(nvbo);
85 if (tile_flags == 0x7a00 ||
87 nv_fb->r_dma = NvEvoFB32;
89 if (tile_flags == 0x7000)
90 nv_fb->r_dma = NvEvoFB16;
92 nv_fb->r_dma = NvEvoVRAM_LP;
95 case 8: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_8; break;
96 case 15: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_15; break;
97 case 16: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_16; break;
99 case 32: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_24; break;
100 case 30: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_30; break;
102 NV_ERROR(dev, "unknown depth %d\n", fb->depth);
106 if (dev_priv->chipset == 0x50)
107 nv_fb->r_format |= (tile_flags << 8);
110 if (dev_priv->card_type < NV_D0)
111 nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
113 nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
115 u32 mode = nvbo->tile_mode;
116 if (dev_priv->card_type >= NV_C0)
118 nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
125 static struct drm_framebuffer *
126 nouveau_user_framebuffer_create(struct drm_device *dev,
127 struct drm_file *file_priv,
128 struct drm_mode_fb_cmd2 *mode_cmd)
130 struct nouveau_framebuffer *nouveau_fb;
131 struct drm_gem_object *gem;
134 gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
136 return ERR_PTR(-ENOENT);
138 nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
140 return ERR_PTR(-ENOMEM);
142 ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
144 drm_gem_object_unreference(gem);
148 return &nouveau_fb->base;
151 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
152 .fb_create = nouveau_user_framebuffer_create,
153 .output_poll_changed = nouveau_fbcon_output_poll_changed,
157 struct drm_prop_enum_list {
163 static struct drm_prop_enum_list underscan[] = {
164 { 2, UNDERSCAN_AUTO, "auto" },
165 { 2, UNDERSCAN_OFF, "off" },
166 { 2, UNDERSCAN_ON, "on" },
170 static struct drm_prop_enum_list dither_mode[] = {
171 { 7, DITHERING_MODE_AUTO, "auto" },
172 { 7, DITHERING_MODE_OFF, "off" },
173 { 1, DITHERING_MODE_ON, "on" },
174 { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
175 { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
176 { 4, DITHERING_MODE_TEMPORAL, "temporal" },
180 static struct drm_prop_enum_list dither_depth[] = {
181 { 6, DITHERING_DEPTH_AUTO, "auto" },
182 { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
183 { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
187 #define PROP_ENUM(p,gen,n,list) do { \
188 struct drm_prop_enum_list *l = (list); \
190 while (l->gen_mask) { \
191 if (l->gen_mask & (1 << (gen))) \
196 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
199 while (p && l->gen_mask) { \
200 if (l->gen_mask & (1 << (gen))) { \
201 drm_property_add_enum(p, c, l->type, l->name); \
210 nouveau_display_create(struct drm_device *dev)
212 struct drm_nouveau_private *dev_priv = dev->dev_private;
213 struct nouveau_display_engine *disp = &dev_priv->engine.display;
216 drm_mode_config_init(dev);
217 drm_mode_create_scaling_mode_property(dev);
218 drm_mode_create_dvi_i_properties(dev);
220 if (dev_priv->card_type < NV_50)
223 if (dev_priv->card_type < NV_D0)
228 PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
229 PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
230 PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
232 disp->underscan_hborder_property =
233 drm_property_create(dev, DRM_MODE_PROP_RANGE,
234 "underscan hborder", 2);
235 disp->underscan_hborder_property->values[0] = 0;
236 disp->underscan_hborder_property->values[1] = 128;
238 disp->underscan_vborder_property =
239 drm_property_create(dev, DRM_MODE_PROP_RANGE,
240 "underscan vborder", 2);
241 disp->underscan_vborder_property->values[0] = 0;
242 disp->underscan_vborder_property->values[1] = 128;
244 dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
245 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
247 dev->mode_config.min_width = 0;
248 dev->mode_config.min_height = 0;
249 if (dev_priv->card_type < NV_10) {
250 dev->mode_config.max_width = 2048;
251 dev->mode_config.max_height = 2048;
253 if (dev_priv->card_type < NV_50) {
254 dev->mode_config.max_width = 4096;
255 dev->mode_config.max_height = 4096;
257 dev->mode_config.max_width = 8192;
258 dev->mode_config.max_height = 8192;
261 ret = disp->create(dev);
269 nouveau_display_destroy(struct drm_device *dev)
271 struct drm_nouveau_private *dev_priv = dev->dev_private;
272 struct nouveau_display_engine *disp = &dev_priv->engine.display;
275 drm_mode_config_cleanup(dev);
279 nouveau_vblank_enable(struct drm_device *dev, int crtc)
281 struct drm_nouveau_private *dev_priv = dev->dev_private;
283 if (dev_priv->card_type >= NV_50)
284 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
285 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
287 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
288 NV_PCRTC_INTR_0_VBLANK);
294 nouveau_vblank_disable(struct drm_device *dev, int crtc)
296 struct drm_nouveau_private *dev_priv = dev->dev_private;
298 if (dev_priv->card_type >= NV_50)
299 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
300 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
302 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
306 nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
307 struct nouveau_bo *new_bo)
311 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
315 ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
319 ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
326 ttm_bo_unreserve(&new_bo->bo);
328 nouveau_bo_unpin(new_bo);
333 nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
334 struct nouveau_bo *new_bo,
335 struct nouveau_fence *fence)
337 nouveau_bo_fence(new_bo, fence);
338 ttm_bo_unreserve(&new_bo->bo);
340 nouveau_bo_fence(old_bo, fence);
341 ttm_bo_unreserve(&old_bo->bo);
343 nouveau_bo_unpin(old_bo);
347 nouveau_page_flip_emit(struct nouveau_channel *chan,
348 struct nouveau_bo *old_bo,
349 struct nouveau_bo *new_bo,
350 struct nouveau_page_flip_state *s,
351 struct nouveau_fence **pfence)
353 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
354 struct drm_device *dev = chan->dev;
358 /* Queue it to the pending list */
359 spin_lock_irqsave(&dev->event_lock, flags);
360 list_add_tail(&s->head, &chan->nvsw.flip);
361 spin_unlock_irqrestore(&dev->event_lock, flags);
363 /* Synchronize with the old framebuffer */
364 ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
368 /* Emit the pageflip */
369 ret = RING_SPACE(chan, 2);
373 if (dev_priv->card_type < NV_C0)
374 BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
376 BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0500, 1);
380 ret = nouveau_fence_new(chan, pfence, true);
386 spin_lock_irqsave(&dev->event_lock, flags);
388 spin_unlock_irqrestore(&dev->event_lock, flags);
393 nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
394 struct drm_pending_vblank_event *event)
396 struct drm_device *dev = crtc->dev;
397 struct drm_nouveau_private *dev_priv = dev->dev_private;
398 struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
399 struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
400 struct nouveau_page_flip_state *s;
401 struct nouveau_channel *chan;
402 struct nouveau_fence *fence;
405 if (!dev_priv->channel)
408 s = kzalloc(sizeof(*s), GFP_KERNEL);
412 /* Don't let the buffers go away while we flip */
413 ret = nouveau_page_flip_reserve(old_bo, new_bo);
417 /* Initialize a page flip struct */
418 *s = (struct nouveau_page_flip_state)
419 { { }, event, nouveau_crtc(crtc)->index,
420 fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
423 /* Choose the channel the flip will be handled in */
424 chan = nouveau_fence_channel(new_bo->bo.sync_obj);
426 chan = nouveau_channel_get_unlocked(dev_priv->channel);
427 mutex_lock(&chan->mutex);
429 /* Emit a page flip */
430 if (dev_priv->card_type >= NV_50) {
431 ret = nv50_display_flip_next(crtc, fb, chan);
433 nouveau_channel_put(&chan);
438 ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
439 nouveau_channel_put(&chan);
443 /* Update the crtc struct and cleanup */
446 nouveau_page_flip_unreserve(old_bo, new_bo, fence);
447 nouveau_fence_unref(&fence);
451 nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
458 nouveau_finish_page_flip(struct nouveau_channel *chan,
459 struct nouveau_page_flip_state *ps)
461 struct drm_device *dev = chan->dev;
462 struct nouveau_page_flip_state *s;
465 spin_lock_irqsave(&dev->event_lock, flags);
467 if (list_empty(&chan->nvsw.flip)) {
468 NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
469 spin_unlock_irqrestore(&dev->event_lock, flags);
473 s = list_first_entry(&chan->nvsw.flip,
474 struct nouveau_page_flip_state, head);
476 struct drm_pending_vblank_event *e = s->event;
479 do_gettimeofday(&now);
480 e->event.sequence = 0;
481 e->event.tv_sec = now.tv_sec;
482 e->event.tv_usec = now.tv_usec;
483 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
484 wake_up_interruptible(&e->base.file_priv->event_wait);
492 spin_unlock_irqrestore(&dev->event_lock, flags);
497 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
498 struct drm_mode_create_dumb *args)
500 struct nouveau_bo *bo;
503 args->pitch = roundup(args->width * (args->bpp / 8), 256);
504 args->size = args->pitch * args->height;
505 args->size = roundup(args->size, PAGE_SIZE);
507 ret = nouveau_gem_new(dev, args->size, 0, TTM_PL_FLAG_VRAM, 0, 0, &bo);
511 ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle);
512 drm_gem_object_unreference_unlocked(bo->gem);
517 nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev,
520 return drm_gem_handle_delete(file_priv, handle);
524 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
525 struct drm_device *dev,
526 uint32_t handle, uint64_t *poffset)
528 struct drm_gem_object *gem;
530 gem = drm_gem_object_lookup(dev, file_priv, handle);
532 struct nouveau_bo *bo = gem->driver_private;
533 *poffset = bo->bo.addr_space_offset;
534 drm_gem_object_unreference_unlocked(gem);