]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_display.c
drm/nv50/disp: fix evo for create/init + destroy/fini split
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nouveau_display.c
CommitLineData
6ee73861
BS
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"
042206c0 32#include "nouveau_hw.h"
332b242f
FJ
33#include "nouveau_crtc.h"
34#include "nouveau_dma.h"
de691855 35#include "nouveau_connector.h"
45c4e0aa 36#include "nv50_display.h"
6ee73861
BS
37
38static void
39nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
40{
41 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
6ee73861 42
bc9025bd
LB
43 if (fb->nvbo)
44 drm_gem_object_unreference_unlocked(fb->nvbo->gem);
6ee73861
BS
45
46 drm_framebuffer_cleanup(drm_fb);
47 kfree(fb);
48}
49
50static int
51nouveau_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
60static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
61 .destroy = nouveau_user_framebuffer_destroy,
62 .create_handle = nouveau_user_framebuffer_create_handle,
63};
64
38651674 65int
45c4e0aa
BS
66nouveau_framebuffer_init(struct drm_device *dev,
67 struct nouveau_framebuffer *nv_fb,
308e5bcb 68 struct drm_mode_fb_cmd2 *mode_cmd,
45c4e0aa 69 struct nouveau_bo *nvbo)
6ee73861 70{
45c4e0aa
BS
71 struct drm_nouveau_private *dev_priv = dev->dev_private;
72 struct drm_framebuffer *fb = &nv_fb->base;
6ee73861
BS
73 int ret;
74
45c4e0aa 75 ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
6ee73861 76 if (ret) {
38651674 77 return ret;
6ee73861
BS
78 }
79
45c4e0aa
BS
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
2fad3d5e
BS
109 if (!tile_flags) {
110 if (dev_priv->card_type < NV_D0)
01f2c773 111 nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
2fad3d5e 112 else
01f2c773 113 nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
2fad3d5e 114 } else {
45c4e0aa
BS
115 u32 mode = nvbo->tile_mode;
116 if (dev_priv->card_type >= NV_C0)
117 mode >>= 4;
01f2c773 118 nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
45c4e0aa
BS
119 }
120 }
121
38651674 122 return 0;
6ee73861
BS
123}
124
125static struct drm_framebuffer *
126nouveau_user_framebuffer_create(struct drm_device *dev,
127 struct drm_file *file_priv,
308e5bcb 128 struct drm_mode_fb_cmd2 *mode_cmd)
6ee73861 129{
38651674 130 struct nouveau_framebuffer *nouveau_fb;
6ee73861 131 struct drm_gem_object *gem;
38651674 132 int ret;
6ee73861 133
308e5bcb 134 gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
6ee73861 135 if (!gem)
cce13ff7 136 return ERR_PTR(-ENOENT);
6ee73861 137
38651674
DA
138 nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
139 if (!nouveau_fb)
cce13ff7 140 return ERR_PTR(-ENOMEM);
38651674
DA
141
142 ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
143 if (ret) {
6ee73861 144 drm_gem_object_unreference(gem);
cce13ff7 145 return ERR_PTR(ret);
6ee73861
BS
146 }
147
38651674 148 return &nouveau_fb->base;
6ee73861
BS
149}
150
27d5030a 151static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
6ee73861 152 .fb_create = nouveau_user_framebuffer_create,
eb1f8e4f 153 .output_poll_changed = nouveau_fbcon_output_poll_changed,
6ee73861
BS
154};
155
b29caa58
BS
156
157struct drm_prop_enum_list {
de691855 158 u8 gen_mask;
b29caa58
BS
159 int type;
160 char *name;
161};
162
de691855
BS
163static struct drm_prop_enum_list underscan[] = {
164 { 2, UNDERSCAN_AUTO, "auto" },
165 { 2, UNDERSCAN_OFF, "off" },
166 { 2, UNDERSCAN_ON, "on" },
167 {}
b29caa58
BS
168};
169
de691855
BS
170static 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
180static 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
27d5030a
BS
209int
210nouveau_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;
de691855 214 int ret, gen;
27d5030a
BS
215
216 drm_mode_config_init(dev);
217 drm_mode_create_scaling_mode_property(dev);
4ceca5f8 218 drm_mode_create_dvi_i_properties(dev);
de691855
BS
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);
b29caa58
BS
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
27d5030a
BS
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
2a44e499
BS
265 ret = disp->init(dev);
266 if (ret)
267 disp->destroy(dev);
268 return ret;
27d5030a
BS
269}
270
271void
272nouveau_display_destroy(struct drm_device *dev)
273{
274 struct drm_nouveau_private *dev_priv = dev->dev_private;
275 struct nouveau_display_engine *disp = &dev_priv->engine.display;
276
2a44e499 277 disp->fini(dev);
27d5030a
BS
278 disp->destroy(dev);
279 drm_mode_config_cleanup(dev);
280}
281
042206c0
FJ
282int
283nouveau_vblank_enable(struct drm_device *dev, int crtc)
284{
285 struct drm_nouveau_private *dev_priv = dev->dev_private;
286
287 if (dev_priv->card_type >= NV_50)
288 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
289 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
290 else
291 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
292 NV_PCRTC_INTR_0_VBLANK);
293
294 return 0;
295}
296
297void
298nouveau_vblank_disable(struct drm_device *dev, int crtc)
299{
300 struct drm_nouveau_private *dev_priv = dev->dev_private;
301
302 if (dev_priv->card_type >= NV_50)
303 nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
304 NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
305 else
306 NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
307}
332b242f
FJ
308
309static int
310nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
311 struct nouveau_bo *new_bo)
312{
313 int ret;
314
315 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
316 if (ret)
317 return ret;
318
319 ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
320 if (ret)
321 goto fail;
322
323 ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
324 if (ret)
325 goto fail_unreserve;
326
327 return 0;
328
329fail_unreserve:
330 ttm_bo_unreserve(&new_bo->bo);
331fail:
332 nouveau_bo_unpin(new_bo);
333 return ret;
334}
335
336static void
337nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
338 struct nouveau_bo *new_bo,
339 struct nouveau_fence *fence)
340{
341 nouveau_bo_fence(new_bo, fence);
342 ttm_bo_unreserve(&new_bo->bo);
343
344 nouveau_bo_fence(old_bo, fence);
345 ttm_bo_unreserve(&old_bo->bo);
346
347 nouveau_bo_unpin(old_bo);
348}
349
350static int
351nouveau_page_flip_emit(struct nouveau_channel *chan,
352 struct nouveau_bo *old_bo,
353 struct nouveau_bo *new_bo,
354 struct nouveau_page_flip_state *s,
355 struct nouveau_fence **pfence)
356{
bd2f2037 357 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
332b242f
FJ
358 struct drm_device *dev = chan->dev;
359 unsigned long flags;
360 int ret;
361
362 /* Queue it to the pending list */
363 spin_lock_irqsave(&dev->event_lock, flags);
364 list_add_tail(&s->head, &chan->nvsw.flip);
365 spin_unlock_irqrestore(&dev->event_lock, flags);
366
367 /* Synchronize with the old framebuffer */
368 ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
369 if (ret)
370 goto fail;
371
372 /* Emit the pageflip */
373 ret = RING_SPACE(chan, 2);
374 if (ret)
375 goto fail;
376
bd2f2037
BS
377 if (dev_priv->card_type < NV_C0)
378 BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
379 else
380 BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0500, 1);
381 OUT_RING (chan, 0);
382 FIRE_RING (chan);
332b242f
FJ
383
384 ret = nouveau_fence_new(chan, pfence, true);
385 if (ret)
386 goto fail;
387
388 return 0;
389fail:
390 spin_lock_irqsave(&dev->event_lock, flags);
391 list_del(&s->head);
392 spin_unlock_irqrestore(&dev->event_lock, flags);
393 return ret;
394}
395
396int
397nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
398 struct drm_pending_vblank_event *event)
399{
400 struct drm_device *dev = crtc->dev;
401 struct drm_nouveau_private *dev_priv = dev->dev_private;
402 struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
403 struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
404 struct nouveau_page_flip_state *s;
405 struct nouveau_channel *chan;
406 struct nouveau_fence *fence;
407 int ret;
408
a82dd49f 409 if (!dev_priv->channel)
332b242f
FJ
410 return -ENODEV;
411
412 s = kzalloc(sizeof(*s), GFP_KERNEL);
413 if (!s)
414 return -ENOMEM;
415
416 /* Don't let the buffers go away while we flip */
417 ret = nouveau_page_flip_reserve(old_bo, new_bo);
418 if (ret)
419 goto fail_free;
420
421 /* Initialize a page flip struct */
422 *s = (struct nouveau_page_flip_state)
2503c6fa 423 { { }, event, nouveau_crtc(crtc)->index,
01f2c773 424 fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
332b242f
FJ
425 new_bo->bo.offset };
426
427 /* Choose the channel the flip will be handled in */
428 chan = nouveau_fence_channel(new_bo->bo.sync_obj);
429 if (!chan)
430 chan = nouveau_channel_get_unlocked(dev_priv->channel);
431 mutex_lock(&chan->mutex);
432
433 /* Emit a page flip */
d7117e0d
BS
434 if (dev_priv->card_type >= NV_50) {
435 ret = nv50_display_flip_next(crtc, fb, chan);
436 if (ret) {
437 nouveau_channel_put(&chan);
438 goto fail_unreserve;
439 }
440 }
441
332b242f
FJ
442 ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
443 nouveau_channel_put(&chan);
444 if (ret)
445 goto fail_unreserve;
446
447 /* Update the crtc struct and cleanup */
448 crtc->fb = fb;
449
450 nouveau_page_flip_unreserve(old_bo, new_bo, fence);
451 nouveau_fence_unref(&fence);
452 return 0;
453
454fail_unreserve:
455 nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
456fail_free:
457 kfree(s);
458 return ret;
459}
460
461int
462nouveau_finish_page_flip(struct nouveau_channel *chan,
463 struct nouveau_page_flip_state *ps)
464{
465 struct drm_device *dev = chan->dev;
466 struct nouveau_page_flip_state *s;
467 unsigned long flags;
468
469 spin_lock_irqsave(&dev->event_lock, flags);
470
471 if (list_empty(&chan->nvsw.flip)) {
472 NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
473 spin_unlock_irqrestore(&dev->event_lock, flags);
474 return -EINVAL;
475 }
476
477 s = list_first_entry(&chan->nvsw.flip,
478 struct nouveau_page_flip_state, head);
479 if (s->event) {
480 struct drm_pending_vblank_event *e = s->event;
481 struct timeval now;
482
483 do_gettimeofday(&now);
484 e->event.sequence = 0;
485 e->event.tv_sec = now.tv_sec;
486 e->event.tv_usec = now.tv_usec;
487 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
488 wake_up_interruptible(&e->base.file_priv->event_wait);
489 }
490
491 list_del(&s->head);
d7117e0d
BS
492 if (ps)
493 *ps = *s;
332b242f
FJ
494 kfree(s);
495
496 spin_unlock_irqrestore(&dev->event_lock, flags);
497 return 0;
498}
33dbc27f
BS
499
500int
501nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
502 struct drm_mode_create_dumb *args)
503{
504 struct nouveau_bo *bo;
505 int ret;
506
507 args->pitch = roundup(args->width * (args->bpp / 8), 256);
508 args->size = args->pitch * args->height;
509 args->size = roundup(args->size, PAGE_SIZE);
510
511 ret = nouveau_gem_new(dev, args->size, 0, TTM_PL_FLAG_VRAM, 0, 0, &bo);
512 if (ret)
513 return ret;
514
515 ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle);
516 drm_gem_object_unreference_unlocked(bo->gem);
517 return ret;
518}
519
520int
521nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev,
522 uint32_t handle)
523{
524 return drm_gem_handle_delete(file_priv, handle);
525}
526
527int
528nouveau_display_dumb_map_offset(struct drm_file *file_priv,
529 struct drm_device *dev,
530 uint32_t handle, uint64_t *poffset)
531{
532 struct drm_gem_object *gem;
533
534 gem = drm_gem_object_lookup(dev, file_priv, handle);
535 if (gem) {
536 struct nouveau_bo *bo = gem->driver_private;
537 *poffset = bo->bo.addr_space_offset;
538 drm_gem_object_unreference_unlocked(gem);
539 return 0;
540 }
541
542 return -ENOENT;
543}