drm/nouveau: don't pretend to support the DVI-I 'select subconnector' prop
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / nouveau / nouveau_display.c
1 /*
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"
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"
37
38 static void
39 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
40 {
41         struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
42
43         if (fb->nvbo)
44                 drm_gem_object_unreference_unlocked(fb->nvbo->gem);
45
46         drm_framebuffer_cleanup(drm_fb);
47         kfree(fb);
48 }
49
50 static int
51 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
52                                        struct drm_file *file_priv,
53                                        unsigned int *handle)
54 {
55         struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
56
57         return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
58 }
59
60 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
61         .destroy = nouveau_user_framebuffer_destroy,
62         .create_handle = nouveau_user_framebuffer_create_handle,
63 };
64
65 int
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)
70 {
71         struct drm_nouveau_private *dev_priv = dev->dev_private;
72         struct drm_framebuffer *fb = &nv_fb->base;
73         int ret;
74
75         ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
76         if (ret) {
77                 return ret;
78         }
79
80         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
81         nv_fb->nvbo = nvbo;
82
83         if (dev_priv->card_type >= NV_50) {
84                 u32 tile_flags = nouveau_bo_tile_layout(nvbo);
85                 if (tile_flags == 0x7a00 ||
86                     tile_flags == 0xfe00)
87                         nv_fb->r_dma = NvEvoFB32;
88                 else
89                 if (tile_flags == 0x7000)
90                         nv_fb->r_dma = NvEvoFB16;
91                 else
92                         nv_fb->r_dma = NvEvoVRAM_LP;
93
94                 switch (fb->depth) {
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;
98                 case 24:
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;
101                 default:
102                          NV_ERROR(dev, "unknown depth %d\n", fb->depth);
103                          return -EINVAL;
104                 }
105
106                 if (dev_priv->chipset == 0x50)
107                         nv_fb->r_format |= (tile_flags << 8);
108
109                 if (!tile_flags) {
110                         if (dev_priv->card_type < NV_D0)
111                                 nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
112                         else
113                                 nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
114                 } else {
115                         u32 mode = nvbo->tile_mode;
116                         if (dev_priv->card_type >= NV_C0)
117                                 mode >>= 4;
118                         nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
119                 }
120         }
121
122         return 0;
123 }
124
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)
129 {
130         struct nouveau_framebuffer *nouveau_fb;
131         struct drm_gem_object *gem;
132         int ret;
133
134         gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
135         if (!gem)
136                 return ERR_PTR(-ENOENT);
137
138         nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
139         if (!nouveau_fb)
140                 return ERR_PTR(-ENOMEM);
141
142         ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
143         if (ret) {
144                 drm_gem_object_unreference(gem);
145                 return ERR_PTR(ret);
146         }
147
148         return &nouveau_fb->base;
149 }
150
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,
154 };
155
156
157 struct drm_prop_enum_list {
158         u8 gen_mask;
159         int type;
160         char *name;
161 };
162
163 static struct drm_prop_enum_list underscan[] = {
164         { 2, UNDERSCAN_AUTO, "auto" },
165         { 2, UNDERSCAN_OFF, "off" },
166         { 2, UNDERSCAN_ON, "on" },
167         {}
168 };
169
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" },
177         {}
178 };
179
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" },
184         {}
185 };
186
187 #define PROP_ENUM(p,gen,n,list) do {                                           \
188         struct drm_prop_enum_list *l = (list);                                 \
189         int c = 0;                                                             \
190         while (l->gen_mask) {                                                  \
191                 if (l->gen_mask & (1 << (gen)))                                \
192                         c++;                                                   \
193                 l++;                                                           \
194         }                                                                      \
195         if (c) {                                                               \
196                 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c);        \
197                 l = (list);                                                    \
198                 c = 0;                                                         \
199                 while (p && l->gen_mask) {                                     \
200                         if (l->gen_mask & (1 << (gen))) {                      \
201                                 drm_property_add_enum(p, c, l->type, l->name); \
202                                 c++;                                           \
203                         }                                                      \
204                         l++;                                                   \
205                 }                                                              \
206         }                                                                      \
207 } while(0)
208
209 int
210 nouveau_display_create(struct drm_device *dev)
211 {
212         struct drm_nouveau_private *dev_priv = dev->dev_private;
213         struct nouveau_display_engine *disp = &dev_priv->engine.display;
214         int ret, gen;
215
216         drm_mode_config_init(dev);
217         drm_mode_create_scaling_mode_property(dev);
218         drm_mode_create_dvi_i_properties(dev);
219
220         if (dev_priv->card_type < NV_50)
221                 gen = 0;
222         else
223         if (dev_priv->card_type < NV_D0)
224                 gen = 1;
225         else
226                 gen = 2;
227
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);
231
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;
237
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;
243
244         dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
245         dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
246
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;
252         } else
253         if (dev_priv->card_type < NV_50) {
254                 dev->mode_config.max_width = 4096;
255                 dev->mode_config.max_height = 4096;
256         } else {
257                 dev->mode_config.max_width = 8192;
258                 dev->mode_config.max_height = 8192;
259         }
260
261         ret = disp->create(dev);
262         if (ret)
263                 return ret;
264
265         return 0;
266 }
267
268 void
269 nouveau_display_destroy(struct drm_device *dev)
270 {
271         struct drm_nouveau_private *dev_priv = dev->dev_private;
272         struct nouveau_display_engine *disp = &dev_priv->engine.display;
273
274         disp->destroy(dev);
275         drm_mode_config_cleanup(dev);
276 }
277
278 int
279 nouveau_vblank_enable(struct drm_device *dev, int crtc)
280 {
281         struct drm_nouveau_private *dev_priv = dev->dev_private;
282
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));
286         else
287                 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
288                             NV_PCRTC_INTR_0_VBLANK);
289
290         return 0;
291 }
292
293 void
294 nouveau_vblank_disable(struct drm_device *dev, int crtc)
295 {
296         struct drm_nouveau_private *dev_priv = dev->dev_private;
297
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);
301         else
302                 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
303 }
304
305 static int
306 nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
307                           struct nouveau_bo *new_bo)
308 {
309         int ret;
310
311         ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
312         if (ret)
313                 return ret;
314
315         ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
316         if (ret)
317                 goto fail;
318
319         ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
320         if (ret)
321                 goto fail_unreserve;
322
323         return 0;
324
325 fail_unreserve:
326         ttm_bo_unreserve(&new_bo->bo);
327 fail:
328         nouveau_bo_unpin(new_bo);
329         return ret;
330 }
331
332 static void
333 nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
334                             struct nouveau_bo *new_bo,
335                             struct nouveau_fence *fence)
336 {
337         nouveau_bo_fence(new_bo, fence);
338         ttm_bo_unreserve(&new_bo->bo);
339
340         nouveau_bo_fence(old_bo, fence);
341         ttm_bo_unreserve(&old_bo->bo);
342
343         nouveau_bo_unpin(old_bo);
344 }
345
346 static int
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)
352 {
353         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
354         struct drm_device *dev = chan->dev;
355         unsigned long flags;
356         int ret;
357
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);
362
363         /* Synchronize with the old framebuffer */
364         ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
365         if (ret)
366                 goto fail;
367
368         /* Emit the pageflip */
369         ret = RING_SPACE(chan, 2);
370         if (ret)
371                 goto fail;
372
373         if (dev_priv->card_type < NV_C0)
374                 BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
375         else
376                 BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0500, 1);
377         OUT_RING  (chan, 0);
378         FIRE_RING (chan);
379
380         ret = nouveau_fence_new(chan, pfence, true);
381         if (ret)
382                 goto fail;
383
384         return 0;
385 fail:
386         spin_lock_irqsave(&dev->event_lock, flags);
387         list_del(&s->head);
388         spin_unlock_irqrestore(&dev->event_lock, flags);
389         return ret;
390 }
391
392 int
393 nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
394                        struct drm_pending_vblank_event *event)
395 {
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;
403         int ret;
404
405         if (!dev_priv->channel)
406                 return -ENODEV;
407
408         s = kzalloc(sizeof(*s), GFP_KERNEL);
409         if (!s)
410                 return -ENOMEM;
411
412         /* Don't let the buffers go away while we flip */
413         ret = nouveau_page_flip_reserve(old_bo, new_bo);
414         if (ret)
415                 goto fail_free;
416
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,
421                   new_bo->bo.offset };
422
423         /* Choose the channel the flip will be handled in */
424         chan = nouveau_fence_channel(new_bo->bo.sync_obj);
425         if (!chan)
426                 chan = nouveau_channel_get_unlocked(dev_priv->channel);
427         mutex_lock(&chan->mutex);
428
429         /* Emit a page flip */
430         if (dev_priv->card_type >= NV_50) {
431                 ret = nv50_display_flip_next(crtc, fb, chan);
432                 if (ret) {
433                         nouveau_channel_put(&chan);
434                         goto fail_unreserve;
435                 }
436         }
437
438         ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
439         nouveau_channel_put(&chan);
440         if (ret)
441                 goto fail_unreserve;
442
443         /* Update the crtc struct and cleanup */
444         crtc->fb = fb;
445
446         nouveau_page_flip_unreserve(old_bo, new_bo, fence);
447         nouveau_fence_unref(&fence);
448         return 0;
449
450 fail_unreserve:
451         nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
452 fail_free:
453         kfree(s);
454         return ret;
455 }
456
457 int
458 nouveau_finish_page_flip(struct nouveau_channel *chan,
459                          struct nouveau_page_flip_state *ps)
460 {
461         struct drm_device *dev = chan->dev;
462         struct nouveau_page_flip_state *s;
463         unsigned long flags;
464
465         spin_lock_irqsave(&dev->event_lock, flags);
466
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);
470                 return -EINVAL;
471         }
472
473         s = list_first_entry(&chan->nvsw.flip,
474                              struct nouveau_page_flip_state, head);
475         if (s->event) {
476                 struct drm_pending_vblank_event *e = s->event;
477                 struct timeval now;
478
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);
485         }
486
487         list_del(&s->head);
488         if (ps)
489                 *ps = *s;
490         kfree(s);
491
492         spin_unlock_irqrestore(&dev->event_lock, flags);
493         return 0;
494 }
495
496 int
497 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
498                             struct drm_mode_create_dumb *args)
499 {
500         struct nouveau_bo *bo;
501         int ret;
502
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);
506
507         ret = nouveau_gem_new(dev, args->size, 0, TTM_PL_FLAG_VRAM, 0, 0, &bo);
508         if (ret)
509                 return ret;
510
511         ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle);
512         drm_gem_object_unreference_unlocked(bo->gem);
513         return ret;
514 }
515
516 int
517 nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev,
518                              uint32_t handle)
519 {
520         return drm_gem_handle_delete(file_priv, handle);
521 }
522
523 int
524 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
525                                 struct drm_device *dev,
526                                 uint32_t handle, uint64_t *poffset)
527 {
528         struct drm_gem_object *gem;
529
530         gem = drm_gem_object_lookup(dev, file_priv, handle);
531         if (gem) {
532                 struct nouveau_bo *bo = gem->driver_private;
533                 *poffset = bo->bo.addr_space_offset;
534                 drm_gem_object_unreference_unlocked(gem);
535                 return 0;
536         }
537
538         return -ENOENT;
539 }