blob: 4b1cf74579833102a6ddb323b527abc5d6340444 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2008 Maarten Maathuis.
3 * All Rights Reserved.
4 *
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:
12 *
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.
16 *
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.
24 *
25 */
26
27#include "drmP.h"
28#include "drm_crtc_helper.h"
29#include "nouveau_drv.h"
30#include "nouveau_fb.h"
31#include "nouveau_fbcon.h"
Francisco Jerez042206c2010-10-21 18:19:29 +020032#include "nouveau_hw.h"
Francisco Jerez332b2422010-10-20 23:35:40 +020033#include "nouveau_crtc.h"
34#include "nouveau_dma.h"
Ben Skeggsde691852011-10-17 12:23:41 +100035#include "nouveau_connector.h"
Ben Skeggs52c4d762011-11-24 12:59:44 +100036#include "nouveau_gpio.h"
Ben Skeggs45c4e0a2011-02-09 11:57:45 +100037#include "nv50_display.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100038
39static void
40nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
41{
42 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
Ben Skeggs6ee73862009-12-11 19:24:15 +100043
Luca Barbieribc9025b2010-02-09 05:49:12 +000044 if (fb->nvbo)
45 drm_gem_object_unreference_unlocked(fb->nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +100046
47 drm_framebuffer_cleanup(drm_fb);
48 kfree(fb);
49}
50
51static int
52nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
53 struct drm_file *file_priv,
54 unsigned int *handle)
55{
56 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
57
58 return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
59}
60
61static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
62 .destroy = nouveau_user_framebuffer_destroy,
63 .create_handle = nouveau_user_framebuffer_create_handle,
64};
65
Dave Airlie38651672010-03-30 05:34:13 +000066int
Ben Skeggs45c4e0a2011-02-09 11:57:45 +100067nouveau_framebuffer_init(struct drm_device *dev,
68 struct nouveau_framebuffer *nv_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080069 struct drm_mode_fb_cmd2 *mode_cmd,
Ben Skeggs45c4e0a2011-02-09 11:57:45 +100070 struct nouveau_bo *nvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +100071{
Ben Skeggs45c4e0a2011-02-09 11:57:45 +100072 struct drm_nouveau_private *dev_priv = dev->dev_private;
73 struct drm_framebuffer *fb = &nv_fb->base;
Ben Skeggs6ee73862009-12-11 19:24:15 +100074 int ret;
75
Ben Skeggs45c4e0a2011-02-09 11:57:45 +100076 ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
Ben Skeggs6ee73862009-12-11 19:24:15 +100077 if (ret) {
Dave Airlie38651672010-03-30 05:34:13 +000078 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +100079 }
80
Ben Skeggs45c4e0a2011-02-09 11:57:45 +100081 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
82 nv_fb->nvbo = nvbo;
83
84 if (dev_priv->card_type >= NV_50) {
85 u32 tile_flags = nouveau_bo_tile_layout(nvbo);
86 if (tile_flags == 0x7a00 ||
87 tile_flags == 0xfe00)
88 nv_fb->r_dma = NvEvoFB32;
89 else
90 if (tile_flags == 0x7000)
91 nv_fb->r_dma = NvEvoFB16;
92 else
93 nv_fb->r_dma = NvEvoVRAM_LP;
94
95 switch (fb->depth) {
96 case 8: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_8; break;
97 case 15: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_15; break;
98 case 16: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_16; break;
99 case 24:
100 case 32: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_24; break;
101 case 30: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_30; break;
102 default:
103 NV_ERROR(dev, "unknown depth %d\n", fb->depth);
104 return -EINVAL;
105 }
106
107 if (dev_priv->chipset == 0x50)
108 nv_fb->r_format |= (tile_flags << 8);
109
Ben Skeggs2fad3d52011-07-06 09:59:40 +1000110 if (!tile_flags) {
111 if (dev_priv->card_type < NV_D0)
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200112 nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
Ben Skeggs2fad3d52011-07-06 09:59:40 +1000113 else
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200114 nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
Ben Skeggs2fad3d52011-07-06 09:59:40 +1000115 } else {
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000116 u32 mode = nvbo->tile_mode;
117 if (dev_priv->card_type >= NV_C0)
118 mode >>= 4;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200119 nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000120 }
121 }
122
Dave Airlie38651672010-03-30 05:34:13 +0000123 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000124}
125
126static struct drm_framebuffer *
127nouveau_user_framebuffer_create(struct drm_device *dev,
128 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800129 struct drm_mode_fb_cmd2 *mode_cmd)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000130{
Dave Airlie38651672010-03-30 05:34:13 +0000131 struct nouveau_framebuffer *nouveau_fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000132 struct drm_gem_object *gem;
Dave Airlie38651672010-03-30 05:34:13 +0000133 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000134
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800135 gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000136 if (!gem)
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100137 return ERR_PTR(-ENOENT);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000138
Dave Airlie38651672010-03-30 05:34:13 +0000139 nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
140 if (!nouveau_fb)
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100141 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +0000142
143 ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
144 if (ret) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000145 drm_gem_object_unreference(gem);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100146 return ERR_PTR(ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000147 }
148
Dave Airlie38651672010-03-30 05:34:13 +0000149 return &nouveau_fb->base;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000150}
151
Ben Skeggs27d50302011-10-06 12:46:40 +1000152static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000153 .fb_create = nouveau_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000154 .output_poll_changed = nouveau_fbcon_output_poll_changed,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000155};
156
Ben Skeggsb29caa52011-10-06 13:29:05 +1000157
Sascha Hauer4a67d392012-02-06 10:58:17 +0100158struct nouveau_drm_prop_enum_list {
Ben Skeggsde691852011-10-17 12:23:41 +1000159 u8 gen_mask;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000160 int type;
161 char *name;
162};
163
Sascha Hauer4a67d392012-02-06 10:58:17 +0100164static struct nouveau_drm_prop_enum_list underscan[] = {
Ben Skeggs92854622011-11-11 23:49:06 +1000165 { 6, UNDERSCAN_AUTO, "auto" },
166 { 6, UNDERSCAN_OFF, "off" },
167 { 6, UNDERSCAN_ON, "on" },
Ben Skeggsde691852011-10-17 12:23:41 +1000168 {}
Ben Skeggsb29caa52011-10-06 13:29:05 +1000169};
170
Sascha Hauer4a67d392012-02-06 10:58:17 +0100171static struct nouveau_drm_prop_enum_list dither_mode[] = {
Ben Skeggsde691852011-10-17 12:23:41 +1000172 { 7, DITHERING_MODE_AUTO, "auto" },
173 { 7, DITHERING_MODE_OFF, "off" },
174 { 1, DITHERING_MODE_ON, "on" },
175 { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
176 { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
177 { 4, DITHERING_MODE_TEMPORAL, "temporal" },
178 {}
179};
180
Sascha Hauer4a67d392012-02-06 10:58:17 +0100181static struct nouveau_drm_prop_enum_list dither_depth[] = {
Ben Skeggsde691852011-10-17 12:23:41 +1000182 { 6, DITHERING_DEPTH_AUTO, "auto" },
183 { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
184 { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
185 {}
186};
187
188#define PROP_ENUM(p,gen,n,list) do { \
Sascha Hauer4a67d392012-02-06 10:58:17 +0100189 struct nouveau_drm_prop_enum_list *l = (list); \
Ben Skeggsde691852011-10-17 12:23:41 +1000190 int c = 0; \
191 while (l->gen_mask) { \
192 if (l->gen_mask & (1 << (gen))) \
193 c++; \
194 l++; \
195 } \
196 if (c) { \
197 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
198 l = (list); \
199 c = 0; \
200 while (p && l->gen_mask) { \
201 if (l->gen_mask & (1 << (gen))) { \
202 drm_property_add_enum(p, c, l->type, l->name); \
203 c++; \
204 } \
205 l++; \
206 } \
207 } \
208} while(0)
209
Francisco Jerez042206c2010-10-21 18:19:29 +0200210int
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000211nouveau_display_init(struct drm_device *dev)
212{
213 struct drm_nouveau_private *dev_priv = dev->dev_private;
214 struct nouveau_display_engine *disp = &dev_priv->engine.display;
Ben Skeggs52c4d762011-11-24 12:59:44 +1000215 struct drm_connector *connector;
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000216 int ret;
217
218 ret = disp->init(dev);
Ben Skeggs52c4d762011-11-24 12:59:44 +1000219 if (ret)
220 return ret;
221
Ben Skeggs7df898b2012-01-31 09:16:59 +1000222 /* power on internal panel if it's not already. the init tables of
223 * some vbios default this to off for some reason, causing the
224 * panel to not work after resume
225 */
226 if (nouveau_gpio_func_get(dev, DCB_GPIO_PANEL_POWER) == 0) {
227 nouveau_gpio_func_set(dev, DCB_GPIO_PANEL_POWER, true);
228 msleep(300);
229 }
230
231 /* enable polling for external displays */
Ben Skeggs52c4d762011-11-24 12:59:44 +1000232 drm_kms_helper_poll_enable(dev);
233
234 /* enable hotplug interrupts */
235 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
236 struct nouveau_connector *conn = nouveau_connector(connector);
237 nouveau_gpio_irq(dev, 0, conn->hpd, 0xff, true);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000238 }
239
240 return ret;
241}
242
243void
244nouveau_display_fini(struct drm_device *dev)
245{
246 struct drm_nouveau_private *dev_priv = dev->dev_private;
247 struct nouveau_display_engine *disp = &dev_priv->engine.display;
Ben Skeggs52c4d762011-11-24 12:59:44 +1000248 struct drm_connector *connector;
249
250 /* disable hotplug interrupts */
251 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
252 struct nouveau_connector *conn = nouveau_connector(connector);
253 nouveau_gpio_irq(dev, 0, conn->hpd, 0xff, false);
254 }
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000255
256 drm_kms_helper_poll_disable(dev);
257 disp->fini(dev);
258}
259
260int
Ben Skeggs27d50302011-10-06 12:46:40 +1000261nouveau_display_create(struct drm_device *dev)
262{
263 struct drm_nouveau_private *dev_priv = dev->dev_private;
264 struct nouveau_display_engine *disp = &dev_priv->engine.display;
Ben Skeggsde691852011-10-17 12:23:41 +1000265 int ret, gen;
Ben Skeggs27d50302011-10-06 12:46:40 +1000266
267 drm_mode_config_init(dev);
268 drm_mode_create_scaling_mode_property(dev);
Ben Skeggs4ceca5f2011-10-14 23:55:47 +1000269 drm_mode_create_dvi_i_properties(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000270
Ben Skeggsde691852011-10-17 12:23:41 +1000271 if (dev_priv->card_type < NV_50)
272 gen = 0;
273 else
274 if (dev_priv->card_type < NV_D0)
275 gen = 1;
276 else
277 gen = 2;
278
279 PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
280 PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
281 PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000282
283 disp->underscan_hborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100284 drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000285
286 disp->underscan_vborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100287 drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000288
Christoph Bumillerdf26bc92012-01-21 23:13:26 +0100289 if (gen == 1) {
290 disp->vibrant_hue_property =
291 drm_property_create(dev, DRM_MODE_PROP_RANGE,
292 "vibrant hue", 2);
293 disp->vibrant_hue_property->values[0] = 0;
294 disp->vibrant_hue_property->values[1] = 180; /* -90..+90 */
295
296 disp->color_vibrance_property =
297 drm_property_create(dev, DRM_MODE_PROP_RANGE,
298 "color vibrance", 2);
299 disp->color_vibrance_property->values[0] = 0;
300 disp->color_vibrance_property->values[1] = 200; /* -100..+100 */
301 }
302
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200303 dev->mode_config.funcs = &nouveau_mode_config_funcs;
Ben Skeggs27d50302011-10-06 12:46:40 +1000304 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
305
306 dev->mode_config.min_width = 0;
307 dev->mode_config.min_height = 0;
308 if (dev_priv->card_type < NV_10) {
309 dev->mode_config.max_width = 2048;
310 dev->mode_config.max_height = 2048;
311 } else
312 if (dev_priv->card_type < NV_50) {
313 dev->mode_config.max_width = 4096;
314 dev->mode_config.max_height = 4096;
315 } else {
316 dev->mode_config.max_width = 8192;
317 dev->mode_config.max_height = 8192;
318 }
319
Dave Airlief1377992012-02-20 14:39:11 +0000320 dev->mode_config.preferred_depth = 24;
321 dev->mode_config.prefer_shadow = 1;
322
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000323 drm_kms_helper_poll_init(dev);
324 drm_kms_helper_poll_disable(dev);
325
Ben Skeggs27d50302011-10-06 12:46:40 +1000326 ret = disp->create(dev);
327 if (ret)
328 return ret;
329
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000330 if (dev->mode_config.num_crtc) {
331 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
332 if (ret)
333 return ret;
334 }
335
Ben Skeggs2a44e492011-11-09 11:36:33 +1000336 return ret;
Ben Skeggs27d50302011-10-06 12:46:40 +1000337}
338
339void
340nouveau_display_destroy(struct drm_device *dev)
341{
342 struct drm_nouveau_private *dev_priv = dev->dev_private;
343 struct nouveau_display_engine *disp = &dev_priv->engine.display;
344
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000345 drm_vblank_cleanup(dev);
346
Ben Skeggs27d50302011-10-06 12:46:40 +1000347 disp->destroy(dev);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000348
349 drm_kms_helper_poll_fini(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000350 drm_mode_config_cleanup(dev);
351}
352
353int
Francisco Jerez042206c2010-10-21 18:19:29 +0200354nouveau_vblank_enable(struct drm_device *dev, int crtc)
355{
356 struct drm_nouveau_private *dev_priv = dev->dev_private;
357
358 if (dev_priv->card_type >= NV_50)
359 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
360 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
361 else
362 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
363 NV_PCRTC_INTR_0_VBLANK);
364
365 return 0;
366}
367
368void
369nouveau_vblank_disable(struct drm_device *dev, int crtc)
370{
371 struct drm_nouveau_private *dev_priv = dev->dev_private;
372
373 if (dev_priv->card_type >= NV_50)
374 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
375 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
376 else
377 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
378}
Francisco Jerez332b2422010-10-20 23:35:40 +0200379
380static int
381nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
382 struct nouveau_bo *new_bo)
383{
384 int ret;
385
386 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
387 if (ret)
388 return ret;
389
390 ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
391 if (ret)
392 goto fail;
393
394 ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
395 if (ret)
396 goto fail_unreserve;
397
398 return 0;
399
400fail_unreserve:
401 ttm_bo_unreserve(&new_bo->bo);
402fail:
403 nouveau_bo_unpin(new_bo);
404 return ret;
405}
406
407static void
408nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
409 struct nouveau_bo *new_bo,
410 struct nouveau_fence *fence)
411{
412 nouveau_bo_fence(new_bo, fence);
413 ttm_bo_unreserve(&new_bo->bo);
414
415 nouveau_bo_fence(old_bo, fence);
416 ttm_bo_unreserve(&old_bo->bo);
417
418 nouveau_bo_unpin(old_bo);
419}
420
421static int
422nouveau_page_flip_emit(struct nouveau_channel *chan,
423 struct nouveau_bo *old_bo,
424 struct nouveau_bo *new_bo,
425 struct nouveau_page_flip_state *s,
426 struct nouveau_fence **pfence)
427{
Ben Skeggsbd2f2032011-02-08 15:16:23 +1000428 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
Francisco Jerez332b2422010-10-20 23:35:40 +0200429 struct drm_device *dev = chan->dev;
430 unsigned long flags;
431 int ret;
432
433 /* Queue it to the pending list */
434 spin_lock_irqsave(&dev->event_lock, flags);
435 list_add_tail(&s->head, &chan->nvsw.flip);
436 spin_unlock_irqrestore(&dev->event_lock, flags);
437
438 /* Synchronize with the old framebuffer */
439 ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
440 if (ret)
441 goto fail;
442
443 /* Emit the pageflip */
Ben Skeggsd5316e22012-03-21 13:53:49 +1000444 ret = RING_SPACE(chan, 3);
Francisco Jerez332b2422010-10-20 23:35:40 +0200445 if (ret)
446 goto fail;
447
Ben Skeggsd5316e22012-03-21 13:53:49 +1000448 if (dev_priv->card_type < NV_C0) {
Ben Skeggsbd2f2032011-02-08 15:16:23 +1000449 BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
Ben Skeggsd5316e22012-03-21 13:53:49 +1000450 OUT_RING (chan, 0x00000000);
451 OUT_RING (chan, 0x00000000);
452 } else {
453 BEGIN_NVC0(chan, 2, 0, NV10_SUBCHAN_REF_CNT, 1);
454 OUT_RING (chan, ++chan->fence.sequence);
455 BEGIN_NVC0(chan, 8, 0, NVSW_SUBCHAN_PAGE_FLIP, 0x0000);
456 }
Ben Skeggsbd2f2032011-02-08 15:16:23 +1000457 FIRE_RING (chan);
Francisco Jerez332b2422010-10-20 23:35:40 +0200458
459 ret = nouveau_fence_new(chan, pfence, true);
460 if (ret)
461 goto fail;
462
463 return 0;
464fail:
465 spin_lock_irqsave(&dev->event_lock, flags);
466 list_del(&s->head);
467 spin_unlock_irqrestore(&dev->event_lock, flags);
468 return ret;
469}
470
471int
472nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
473 struct drm_pending_vblank_event *event)
474{
475 struct drm_device *dev = crtc->dev;
476 struct drm_nouveau_private *dev_priv = dev->dev_private;
477 struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
478 struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
479 struct nouveau_page_flip_state *s;
480 struct nouveau_channel *chan;
481 struct nouveau_fence *fence;
482 int ret;
483
Ben Skeggsa82dd492011-04-01 13:56:05 +1000484 if (!dev_priv->channel)
Francisco Jerez332b2422010-10-20 23:35:40 +0200485 return -ENODEV;
486
487 s = kzalloc(sizeof(*s), GFP_KERNEL);
488 if (!s)
489 return -ENOMEM;
490
491 /* Don't let the buffers go away while we flip */
492 ret = nouveau_page_flip_reserve(old_bo, new_bo);
493 if (ret)
494 goto fail_free;
495
496 /* Initialize a page flip struct */
497 *s = (struct nouveau_page_flip_state)
Benjamin Franzke2503c6f2011-02-16 10:04:48 +0100498 { { }, event, nouveau_crtc(crtc)->index,
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200499 fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
Francisco Jerez332b2422010-10-20 23:35:40 +0200500 new_bo->bo.offset };
501
502 /* Choose the channel the flip will be handled in */
503 chan = nouveau_fence_channel(new_bo->bo.sync_obj);
504 if (!chan)
505 chan = nouveau_channel_get_unlocked(dev_priv->channel);
506 mutex_lock(&chan->mutex);
507
508 /* Emit a page flip */
Ben Skeggsd7117e02011-02-07 14:27:04 +1000509 if (dev_priv->card_type >= NV_50) {
Ben Skeggs3376ee32011-11-12 14:28:12 +1000510 if (dev_priv->card_type >= NV_D0)
511 ret = nvd0_display_flip_next(crtc, fb, chan, 0);
512 else
513 ret = nv50_display_flip_next(crtc, fb, chan);
Ben Skeggsd7117e02011-02-07 14:27:04 +1000514 if (ret) {
515 nouveau_channel_put(&chan);
516 goto fail_unreserve;
517 }
518 }
519
Francisco Jerez332b2422010-10-20 23:35:40 +0200520 ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
521 nouveau_channel_put(&chan);
522 if (ret)
523 goto fail_unreserve;
524
525 /* Update the crtc struct and cleanup */
526 crtc->fb = fb;
527
528 nouveau_page_flip_unreserve(old_bo, new_bo, fence);
529 nouveau_fence_unref(&fence);
530 return 0;
531
532fail_unreserve:
533 nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
534fail_free:
535 kfree(s);
536 return ret;
537}
538
539int
540nouveau_finish_page_flip(struct nouveau_channel *chan,
541 struct nouveau_page_flip_state *ps)
542{
543 struct drm_device *dev = chan->dev;
544 struct nouveau_page_flip_state *s;
545 unsigned long flags;
546
547 spin_lock_irqsave(&dev->event_lock, flags);
548
549 if (list_empty(&chan->nvsw.flip)) {
550 NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
551 spin_unlock_irqrestore(&dev->event_lock, flags);
552 return -EINVAL;
553 }
554
555 s = list_first_entry(&chan->nvsw.flip,
556 struct nouveau_page_flip_state, head);
557 if (s->event) {
558 struct drm_pending_vblank_event *e = s->event;
559 struct timeval now;
560
561 do_gettimeofday(&now);
562 e->event.sequence = 0;
563 e->event.tv_sec = now.tv_sec;
564 e->event.tv_usec = now.tv_usec;
565 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
566 wake_up_interruptible(&e->base.file_priv->event_wait);
567 }
568
569 list_del(&s->head);
Ben Skeggsd7117e02011-02-07 14:27:04 +1000570 if (ps)
571 *ps = *s;
Francisco Jerez332b2422010-10-20 23:35:40 +0200572 kfree(s);
573
574 spin_unlock_irqrestore(&dev->event_lock, flags);
575 return 0;
576}
Ben Skeggs33dbc272011-09-30 08:55:50 +1000577
578int
579nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
580 struct drm_mode_create_dumb *args)
581{
582 struct nouveau_bo *bo;
583 int ret;
584
585 args->pitch = roundup(args->width * (args->bpp / 8), 256);
586 args->size = args->pitch * args->height;
587 args->size = roundup(args->size, PAGE_SIZE);
588
589 ret = nouveau_gem_new(dev, args->size, 0, TTM_PL_FLAG_VRAM, 0, 0, &bo);
590 if (ret)
591 return ret;
592
593 ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle);
594 drm_gem_object_unreference_unlocked(bo->gem);
595 return ret;
596}
597
598int
599nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev,
600 uint32_t handle)
601{
602 return drm_gem_handle_delete(file_priv, handle);
603}
604
605int
606nouveau_display_dumb_map_offset(struct drm_file *file_priv,
607 struct drm_device *dev,
608 uint32_t handle, uint64_t *poffset)
609{
610 struct drm_gem_object *gem;
611
612 gem = drm_gem_object_lookup(dev, file_priv, handle);
613 if (gem) {
614 struct nouveau_bo *bo = gem->driver_private;
615 *poffset = bo->bo.addr_space_offset;
616 drm_gem_object_unreference_unlocked(gem);
617 return 0;
618 }
619
620 return -ENOENT;
621}