blob: 6718c84fb86250f2bb68a4612a5209ed5725cee4 [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
Hans de Goede3a6536c2016-11-09 18:17:44 +010027#include <acpi/video.h>
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
Ben Skeggsb167db02016-11-04 17:20:36 +100029#include <drm/drm_atomic.h>
30#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drm_crtc_helper.h>
Ben Skeggs77145f12012-07-31 16:16:21 +100032
Ben Skeggsfdb751e2014-08-10 04:10:23 +100033#include <nvif/class.h>
34
Ben Skeggs6ee73862009-12-11 19:24:15 +100035#include "nouveau_fbcon.h"
Ben Skeggs1a646342013-03-21 15:45:11 +100036#include "dispnv04/hw.h"
Francisco Jerez332b2422010-10-20 23:35:40 +020037#include "nouveau_crtc.h"
38#include "nouveau_dma.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100039#include "nouveau_gem.h"
Ben Skeggsde691852011-10-17 12:23:41 +100040#include "nouveau_connector.h"
Ben Skeggs45c4e0a2011-02-09 11:57:45 +100041#include "nv50_display.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100042
Ben Skeggsebb945a2012-07-20 08:17:34 +100043#include "nouveau_fence.h"
44
Ben Skeggs7568b102015-11-08 10:44:19 +100045#include <nvif/cl0046.h>
Ben Skeggs79ca2772014-08-10 04:10:20 +100046#include <nvif/event.h>
Ben Skeggs1d7c71a2013-01-31 09:23:34 +100047
Ben Skeggs51cb4b32013-10-03 07:02:29 +100048static int
Ben Skeggs80bc3402014-08-10 04:10:28 +100049nouveau_display_vblank_handler(struct nvif_notify *notify)
Ben Skeggs51cb4b32013-10-03 07:02:29 +100050{
Ben Skeggs79ca2772014-08-10 04:10:20 +100051 struct nouveau_crtc *nv_crtc =
52 container_of(notify, typeof(*nv_crtc), vblank);
Gustavo Padovand297b022016-07-04 21:04:50 -030053 drm_crtc_handle_vblank(&nv_crtc->base);
Ben Skeggs80bc3402014-08-10 04:10:28 +100054 return NVIF_NOTIFY_KEEP;
Ben Skeggs51cb4b32013-10-03 07:02:29 +100055}
56
57int
Thierry Reding88e72712015-09-24 18:35:31 +020058nouveau_display_vblank_enable(struct drm_device *dev, unsigned int pipe)
Ben Skeggs51cb4b32013-10-03 07:02:29 +100059{
Ben Skeggsb12f0ae2014-08-10 04:10:19 +100060 struct drm_crtc *crtc;
Shawn Guoea441bd2017-01-09 19:25:43 +080061 struct nouveau_crtc *nv_crtc;
62
63 crtc = drm_crtc_from_index(dev, pipe);
64 if (!crtc)
65 return -EINVAL;
66
67 nv_crtc = nouveau_crtc(crtc);
68 nvif_notify_get(&nv_crtc->vblank);
69
70 return 0;
Ben Skeggs51cb4b32013-10-03 07:02:29 +100071}
72
73void
Thierry Reding88e72712015-09-24 18:35:31 +020074nouveau_display_vblank_disable(struct drm_device *dev, unsigned int pipe)
Ben Skeggs51cb4b32013-10-03 07:02:29 +100075{
Ben Skeggsb12f0ae2014-08-10 04:10:19 +100076 struct drm_crtc *crtc;
Shawn Guoea441bd2017-01-09 19:25:43 +080077 struct nouveau_crtc *nv_crtc;
78
79 crtc = drm_crtc_from_index(dev, pipe);
80 if (!crtc)
81 return;
82
83 nv_crtc = nouveau_crtc(crtc);
84 nvif_notify_put(&nv_crtc->vblank);
Ben Skeggs51cb4b32013-10-03 07:02:29 +100085}
86
Ben Skeggsd83ef852013-11-14 13:37:49 +100087static inline int
88calc(int blanks, int blanke, int total, int line)
89{
90 if (blanke >= blanks) {
91 if (line >= blanks)
92 line -= total;
93 } else {
94 if (line >= blanks)
95 line -= total;
96 line -= blanke + 1;
97 }
98 return line;
99}
100
Daniel Vetter1bf6ad62017-05-09 16:03:28 +0200101static bool
Ben Skeggsd83ef852013-11-14 13:37:49 +1000102nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
103 ktime_t *stime, ktime_t *etime)
104{
Ben Skeggs4952b4d2014-08-10 04:10:27 +1000105 struct {
106 struct nv04_disp_mthd_v0 base;
107 struct nv04_disp_scanoutpos_v0 scan;
108 } args = {
109 .base.method = NV04_DISP_SCANOUTPOS,
110 .base.head = nouveau_crtc(crtc)->index,
111 };
Ben Skeggsd83ef852013-11-14 13:37:49 +1000112 struct nouveau_display *disp = nouveau_display(crtc->dev);
Ville Syrjäläeba1f352015-09-14 22:43:43 +0300113 struct drm_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
Daniel Vetter1bf6ad62017-05-09 16:03:28 +0200114 int retry = 20;
115 bool ret = false;
Ben Skeggsd83ef852013-11-14 13:37:49 +1000116
117 do {
Ben Skeggs4952b4d2014-08-10 04:10:27 +1000118 ret = nvif_mthd(&disp->disp, 0, &args, sizeof(args));
Ben Skeggsd83ef852013-11-14 13:37:49 +1000119 if (ret != 0)
Daniel Vetter1bf6ad62017-05-09 16:03:28 +0200120 return false;
Ben Skeggsd83ef852013-11-14 13:37:49 +1000121
Ben Skeggs4952b4d2014-08-10 04:10:27 +1000122 if (args.scan.vline) {
Daniel Vetter1bf6ad62017-05-09 16:03:28 +0200123 ret = true;
Ben Skeggsd83ef852013-11-14 13:37:49 +1000124 break;
125 }
126
Ville Syrjäläeba1f352015-09-14 22:43:43 +0300127 if (retry) ndelay(vblank->linedur_ns);
Ben Skeggsd83ef852013-11-14 13:37:49 +1000128 } while (retry--);
129
Ben Skeggs4952b4d2014-08-10 04:10:27 +1000130 *hpos = args.scan.hline;
131 *vpos = calc(args.scan.vblanks, args.scan.vblanke,
132 args.scan.vtotal, args.scan.vline);
133 if (stime) *stime = ns_to_ktime(args.scan.time[0]);
134 if (etime) *etime = ns_to_ktime(args.scan.time[1]);
Ben Skeggsd83ef852013-11-14 13:37:49 +1000135
Ben Skeggsd83ef852013-11-14 13:37:49 +1000136 return ret;
137}
138
Daniel Vetter1bf6ad62017-05-09 16:03:28 +0200139bool
Thierry Reding88e72712015-09-24 18:35:31 +0200140nouveau_display_scanoutpos(struct drm_device *dev, unsigned int pipe,
Daniel Vetter1bf6ad62017-05-09 16:03:28 +0200141 bool in_vblank_irq, int *vpos, int *hpos,
Thierry Reding88e72712015-09-24 18:35:31 +0200142 ktime_t *stime, ktime_t *etime,
Ville Syrjälä3bb403b2015-09-14 22:43:44 +0300143 const struct drm_display_mode *mode)
Ben Skeggsd83ef852013-11-14 13:37:49 +1000144{
145 struct drm_crtc *crtc;
146
147 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Thierry Reding88e72712015-09-24 18:35:31 +0200148 if (nouveau_crtc(crtc)->index == pipe) {
Ben Skeggsd83ef852013-11-14 13:37:49 +1000149 return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
150 stime, etime);
151 }
152 }
153
Daniel Vetterd673c022017-05-09 16:03:25 +0200154 return false;
Ben Skeggsd83ef852013-11-14 13:37:49 +1000155}
156
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000157static void
158nouveau_display_vblank_fini(struct drm_device *dev)
159{
Ben Skeggsb12f0ae2014-08-10 04:10:19 +1000160 struct drm_crtc *crtc;
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000161
Ben Skeggs1139ffb2014-01-23 10:49:47 +1000162 drm_vblank_cleanup(dev);
163
Ben Skeggsb12f0ae2014-08-10 04:10:19 +1000164 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
165 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs80bc3402014-08-10 04:10:28 +1000166 nvif_notify_fini(&nv_crtc->vblank);
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000167 }
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000168}
169
170static int
171nouveau_display_vblank_init(struct drm_device *dev)
172{
Ben Skeggs80bc3402014-08-10 04:10:28 +1000173 struct nouveau_display *disp = nouveau_display(dev);
Ben Skeggsb12f0ae2014-08-10 04:10:19 +1000174 struct drm_crtc *crtc;
175 int ret;
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000176
Ben Skeggsb12f0ae2014-08-10 04:10:19 +1000177 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
178 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggsa01ca782015-08-20 14:54:15 +1000179 ret = nvif_notify_init(&disp->disp,
Ben Skeggs79ca2772014-08-10 04:10:20 +1000180 nouveau_display_vblank_handler, false,
Ben Skeggs80bc3402014-08-10 04:10:28 +1000181 NV04_DISP_NTFY_VBLANK,
Ben Skeggs79ca2772014-08-10 04:10:20 +1000182 &(struct nvif_notify_head_req_v0) {
183 .head = nv_crtc->index,
184 },
185 sizeof(struct nvif_notify_head_req_v0),
186 sizeof(struct nvif_notify_head_rep_v0),
187 &nv_crtc->vblank);
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000188 if (ret) {
189 nouveau_display_vblank_fini(dev);
190 return ret;
191 }
192 }
193
194 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
195 if (ret) {
196 nouveau_display_vblank_fini(dev);
197 return ret;
198 }
199
200 return 0;
201}
202
Ben Skeggs6ee73862009-12-11 19:24:15 +1000203static void
204nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
205{
206 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000207
Luca Barbieribc9025b2010-02-09 05:49:12 +0000208 if (fb->nvbo)
David Herrmann55fb74a2013-10-02 10:15:17 +0200209 drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210
211 drm_framebuffer_cleanup(drm_fb);
212 kfree(fb);
213}
214
215static int
216nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
217 struct drm_file *file_priv,
218 unsigned int *handle)
219{
220 struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
221
David Herrmann55fb74a2013-10-02 10:15:17 +0200222 return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223}
224
225static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
226 .destroy = nouveau_user_framebuffer_destroy,
227 .create_handle = nouveau_user_framebuffer_create_handle,
228};
229
Dave Airlie38651672010-03-30 05:34:13 +0000230int
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000231nouveau_framebuffer_new(struct drm_device *dev,
232 const struct drm_mode_fb_cmd2 *mode_cmd,
233 struct nouveau_bo *nvbo,
234 struct nouveau_framebuffer **pfb)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235{
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000236 struct nouveau_framebuffer *fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000237 int ret;
238
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000239 if (!(fb = *pfb = kzalloc(sizeof(*fb), GFP_KERNEL)))
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000240 return -ENOMEM;
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000241
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200242 drm_helper_mode_fill_fb_struct(dev, &fb->base, mode_cmd);
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000243 fb->nvbo = nvbo;
Ben Skeggsab0af552014-08-10 04:10:19 +1000244
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000245 ret = drm_framebuffer_init(dev, &fb->base, &nouveau_framebuffer_funcs);
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000246 if (ret)
247 kfree(fb);
Ben Skeggsab0af552014-08-10 04:10:19 +1000248 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000249}
250
Ben Skeggs839ca902016-11-04 17:20:36 +1000251struct drm_framebuffer *
Ben Skeggs6ee73862009-12-11 19:24:15 +1000252nouveau_user_framebuffer_create(struct drm_device *dev,
253 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200254 const struct drm_mode_fb_cmd2 *mode_cmd)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000255{
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000256 struct nouveau_framebuffer *fb;
257 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000258 struct drm_gem_object *gem;
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000259 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000260
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100261 gem = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000262 if (!gem)
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100263 return ERR_PTR(-ENOENT);
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000264 nvbo = nouveau_gem_object(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265
Ben Skeggs1608a0f2016-11-04 17:20:35 +1000266 ret = nouveau_framebuffer_new(dev, mode_cmd, nvbo, &fb);
267 if (ret == 0)
268 return &fb->base;
Dave Airlie38651672010-03-30 05:34:13 +0000269
Daniel Vettere889c242016-03-30 11:40:40 +0200270 drm_gem_object_unreference_unlocked(gem);
Maarten Lankhorstfdfb8332013-07-08 14:50:54 +1000271 return ERR_PTR(ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000272}
273
Ben Skeggs27d50302011-10-06 12:46:40 +1000274static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000275 .fb_create = nouveau_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000276 .output_poll_changed = nouveau_fbcon_output_poll_changed,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000277};
278
Ben Skeggsb29caa52011-10-06 13:29:05 +1000279
Sascha Hauer4a67d392012-02-06 10:58:17 +0100280struct nouveau_drm_prop_enum_list {
Ben Skeggsde691852011-10-17 12:23:41 +1000281 u8 gen_mask;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000282 int type;
283 char *name;
284};
285
Sascha Hauer4a67d392012-02-06 10:58:17 +0100286static struct nouveau_drm_prop_enum_list underscan[] = {
Ben Skeggs92854622011-11-11 23:49:06 +1000287 { 6, UNDERSCAN_AUTO, "auto" },
288 { 6, UNDERSCAN_OFF, "off" },
289 { 6, UNDERSCAN_ON, "on" },
Ben Skeggsde691852011-10-17 12:23:41 +1000290 {}
Ben Skeggsb29caa52011-10-06 13:29:05 +1000291};
292
Sascha Hauer4a67d392012-02-06 10:58:17 +0100293static struct nouveau_drm_prop_enum_list dither_mode[] = {
Ben Skeggsde691852011-10-17 12:23:41 +1000294 { 7, DITHERING_MODE_AUTO, "auto" },
295 { 7, DITHERING_MODE_OFF, "off" },
296 { 1, DITHERING_MODE_ON, "on" },
297 { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
298 { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
299 { 4, DITHERING_MODE_TEMPORAL, "temporal" },
300 {}
301};
302
Sascha Hauer4a67d392012-02-06 10:58:17 +0100303static struct nouveau_drm_prop_enum_list dither_depth[] = {
Ben Skeggsde691852011-10-17 12:23:41 +1000304 { 6, DITHERING_DEPTH_AUTO, "auto" },
305 { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
306 { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
307 {}
308};
309
310#define PROP_ENUM(p,gen,n,list) do { \
Sascha Hauer4a67d392012-02-06 10:58:17 +0100311 struct nouveau_drm_prop_enum_list *l = (list); \
Ben Skeggsde691852011-10-17 12:23:41 +1000312 int c = 0; \
313 while (l->gen_mask) { \
314 if (l->gen_mask & (1 << (gen))) \
315 c++; \
316 l++; \
317 } \
318 if (c) { \
319 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
320 l = (list); \
321 c = 0; \
322 while (p && l->gen_mask) { \
323 if (l->gen_mask & (1 << (gen))) { \
324 drm_property_add_enum(p, c, l->type, l->name); \
325 c++; \
326 } \
327 l++; \
328 } \
329 } \
330} while(0)
331
Hans de Goede81280d02016-11-21 17:50:54 +0100332static void
333nouveau_display_hpd_work(struct work_struct *work)
334{
335 struct nouveau_drm *drm = container_of(work, typeof(*drm), hpd_work);
336
337 pm_runtime_get_sync(drm->dev->dev);
338
339 drm_helper_hpd_irq_event(drm->dev);
340
341 pm_runtime_mark_last_busy(drm->dev->dev);
342 pm_runtime_put_sync(drm->dev->dev);
343}
344
Hans de Goede3a6536c2016-11-09 18:17:44 +0100345#ifdef CONFIG_ACPI
346
347/*
348 * Hans de Goede: This define belongs in acpi/video.h, I've submitted a patch
349 * to the acpi subsys to move it there from drivers/acpi/acpi_video.c .
350 * This should be dropped once that is merged.
351 */
352#ifndef ACPI_VIDEO_NOTIFY_PROBE
353#define ACPI_VIDEO_NOTIFY_PROBE 0x81
354#endif
355
Hans de Goede3a6536c2016-11-09 18:17:44 +0100356static int
357nouveau_display_acpi_ntfy(struct notifier_block *nb, unsigned long val,
358 void *data)
359{
360 struct nouveau_drm *drm = container_of(nb, typeof(*drm), acpi_nb);
361 struct acpi_bus_event *info = data;
362
363 if (!strcmp(info->device_class, ACPI_VIDEO_CLASS)) {
364 if (info->type == ACPI_VIDEO_NOTIFY_PROBE) {
365 /*
366 * This may be the only indication we receive of a
367 * connector hotplug on a runtime suspended GPU,
Hans de Goede81280d02016-11-21 17:50:54 +0100368 * schedule hpd_work to check.
Hans de Goede3a6536c2016-11-09 18:17:44 +0100369 */
Hans de Goede81280d02016-11-21 17:50:54 +0100370 schedule_work(&drm->hpd_work);
Hans de Goede3a6536c2016-11-09 18:17:44 +0100371
372 /* acpi-video should not generate keypresses for this */
373 return NOTIFY_BAD;
374 }
375 }
376
377 return NOTIFY_DONE;
378}
379#endif
380
Francisco Jerez042206c2010-10-21 18:19:29 +0200381int
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000382nouveau_display_init(struct drm_device *dev)
383{
Ben Skeggs77145f12012-07-31 16:16:21 +1000384 struct nouveau_display *disp = nouveau_display(dev);
Ben Skeggs898a2b32015-08-20 14:54:18 +1000385 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs52c4d762011-11-24 12:59:44 +1000386 struct drm_connector *connector;
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000387 int ret;
388
389 ret = disp->init(dev);
Ben Skeggs52c4d762011-11-24 12:59:44 +1000390 if (ret)
391 return ret;
392
Ben Skeggs7df898b2012-01-31 09:16:59 +1000393 /* enable polling for external displays */
Lyude Paulcae9ff02017-01-11 21:25:23 -0500394 if (!dev->mode_config.poll_enabled)
395 drm_kms_helper_poll_enable(dev);
Ben Skeggs52c4d762011-11-24 12:59:44 +1000396
397 /* enable hotplug interrupts */
398 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
399 struct nouveau_connector *conn = nouveau_connector(connector);
Ben Skeggs80bc3402014-08-10 04:10:28 +1000400 nvif_notify_get(&conn->hpd);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000401 }
402
Ben Skeggs898a2b32015-08-20 14:54:18 +1000403 /* enable flip completion events */
404 nvif_notify_get(&drm->flip);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000405 return ret;
406}
407
408void
Ben Skeggs3b4c0ab2016-11-04 17:20:35 +1000409nouveau_display_fini(struct drm_device *dev, bool suspend)
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000410{
Ben Skeggs77145f12012-07-31 16:16:21 +1000411 struct nouveau_display *disp = nouveau_display(dev);
Ben Skeggs898a2b32015-08-20 14:54:18 +1000412 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs52c4d762011-11-24 12:59:44 +1000413 struct drm_connector *connector;
Daniel Vetter9bc6db0d2016-11-15 22:58:19 +0100414 struct drm_crtc *crtc;
Mario Kleiner9cba5ef2014-07-29 02:36:44 +0200415
Daniel Vetter18dddad2017-03-21 17:41:49 +0100416 if (!suspend) {
417 if (drm_drv_uses_atomic_modeset(dev))
418 drm_atomic_helper_shutdown(dev);
419 else
420 drm_crtc_force_disable_all(dev);
421 }
Ben Skeggs3b4c0ab2016-11-04 17:20:35 +1000422
Mario Kleiner9cba5ef2014-07-29 02:36:44 +0200423 /* Make sure that drm and hw vblank irqs get properly disabled. */
Daniel Vetter9bc6db0d2016-11-15 22:58:19 +0100424 drm_for_each_crtc(crtc, dev)
425 drm_crtc_vblank_off(crtc);
Ben Skeggs52c4d762011-11-24 12:59:44 +1000426
Ben Skeggs898a2b32015-08-20 14:54:18 +1000427 /* disable flip completion events */
428 nvif_notify_put(&drm->flip);
429
Ben Skeggs52c4d762011-11-24 12:59:44 +1000430 /* disable hotplug interrupts */
431 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
432 struct nouveau_connector *conn = nouveau_connector(connector);
Ben Skeggs80bc3402014-08-10 04:10:28 +1000433 nvif_notify_put(&conn->hpd);
Ben Skeggs52c4d762011-11-24 12:59:44 +1000434 }
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000435
436 drm_kms_helper_poll_disable(dev);
437 disp->fini(dev);
438}
439
Ben Skeggs9c210f32014-08-10 04:10:21 +1000440static void
441nouveau_display_create_properties(struct drm_device *dev)
Ben Skeggs27d50302011-10-06 12:46:40 +1000442{
Ben Skeggs9c210f32014-08-10 04:10:21 +1000443 struct nouveau_display *disp = nouveau_display(dev);
444 int gen;
Ben Skeggs27d50302011-10-06 12:46:40 +1000445
Ben Skeggs648d4df2014-08-10 04:10:27 +1000446 if (disp->disp.oclass < NV50_DISP)
Ben Skeggsde691852011-10-17 12:23:41 +1000447 gen = 0;
448 else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000449 if (disp->disp.oclass < GF110_DISP)
Ben Skeggsde691852011-10-17 12:23:41 +1000450 gen = 1;
451 else
452 gen = 2;
453
454 PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
455 PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
456 PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000457
458 disp->underscan_hborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100459 drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000460
461 disp->underscan_vborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100462 drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000463
Ben Skeggs9c210f32014-08-10 04:10:21 +1000464 if (gen < 1)
465 return;
Christoph Bumillerdf26bc92012-01-21 23:13:26 +0100466
Ben Skeggs9c210f32014-08-10 04:10:21 +1000467 /* -90..+90 */
468 disp->vibrant_hue_property =
469 drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
470
471 /* -100..+100 */
472 disp->color_vibrance_property =
473 drm_property_create_range(dev, 0, "color vibrance", 0, 200);
474}
475
476int
477nouveau_display_create(struct drm_device *dev)
478{
479 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000480 struct nvkm_device *device = nvxx_device(&drm->client.device);
Ben Skeggs9c210f32014-08-10 04:10:21 +1000481 struct nouveau_display *disp;
482 int ret;
483
484 disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
485 if (!disp)
486 return -ENOMEM;
487
488 drm_mode_config_init(dev);
489 drm_mode_create_scaling_mode_property(dev);
490 drm_mode_create_dvi_i_properties(dev);
Christoph Bumillerdf26bc92012-01-21 23:13:26 +0100491
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200492 dev->mode_config.funcs = &nouveau_mode_config_funcs;
Ben Skeggs7e8820f2015-08-20 14:54:23 +1000493 dev->mode_config.fb_base = device->func->resource_addr(device, 1);
Ben Skeggs27d50302011-10-06 12:46:40 +1000494
495 dev->mode_config.min_width = 0;
496 dev->mode_config.min_height = 0;
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000497 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
Ben Skeggs27d50302011-10-06 12:46:40 +1000498 dev->mode_config.max_width = 2048;
499 dev->mode_config.max_height = 2048;
500 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000501 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
Ben Skeggs27d50302011-10-06 12:46:40 +1000502 dev->mode_config.max_width = 4096;
503 dev->mode_config.max_height = 4096;
Ilia Mirkin5102ec32015-09-17 22:31:20 -0400504 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000505 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI) {
Ben Skeggs27d50302011-10-06 12:46:40 +1000506 dev->mode_config.max_width = 8192;
507 dev->mode_config.max_height = 8192;
Ilia Mirkin5102ec32015-09-17 22:31:20 -0400508 } else {
509 dev->mode_config.max_width = 16384;
510 dev->mode_config.max_height = 16384;
Ben Skeggs27d50302011-10-06 12:46:40 +1000511 }
512
Dave Airlief1377992012-02-20 14:39:11 +0000513 dev->mode_config.preferred_depth = 24;
514 dev->mode_config.prefer_shadow = 1;
515
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000516 if (drm->client.device.info.chipset < 0x11)
Ben Skeggsb9d9dcd2013-11-11 13:59:40 +1000517 dev->mode_config.async_page_flip = false;
518 else
519 dev->mode_config.async_page_flip = true;
520
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000521 drm_kms_helper_poll_init(dev);
522 drm_kms_helper_poll_disable(dev);
523
Ben Skeggs771fa0e2014-08-10 04:10:31 +1000524 if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
Ben Skeggs2332b312014-01-22 12:58:12 +1000525 static const u16 oclass[] = {
Ben Skeggsed828662016-11-16 15:03:07 +1000526 GP102_DISP,
Ben Skeggsf9d5cbb2016-07-09 10:41:01 +1000527 GP100_DISP,
Ben Skeggsdb1eb522016-02-11 08:35:32 +1000528 GM200_DISP,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000529 GM107_DISP,
530 GK110_DISP,
531 GK104_DISP,
532 GF110_DISP,
533 GT214_DISP,
534 GT206_DISP,
535 GT200_DISP,
536 G82_DISP,
537 NV50_DISP,
538 NV04_DISP,
Ben Skeggs2332b312014-01-22 12:58:12 +1000539 };
540 int i;
541
542 for (i = 0, ret = -ENODEV; ret && i < ARRAY_SIZE(oclass); i++) {
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000543 ret = nvif_object_init(&drm->client.device.object, 0,
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000544 oclass[i], NULL, 0, &disp->disp);
Ben Skeggs2332b312014-01-22 12:58:12 +1000545 }
546
547 if (ret == 0) {
Ben Skeggs9c210f32014-08-10 04:10:21 +1000548 nouveau_display_create_properties(dev);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000549 if (disp->disp.oclass < NV50_DISP)
Ben Skeggs2332b312014-01-22 12:58:12 +1000550 ret = nv04_display_create(dev);
551 else
552 ret = nv50_display_create(dev);
553 }
Ben Skeggsfc162082013-09-10 13:20:34 +1000554 } else {
555 ret = 0;
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000556 }
557
Ben Skeggsfc162082013-09-10 13:20:34 +1000558 if (ret)
559 goto disp_create_err;
560
Ben Skeggs07193f72016-11-04 17:20:35 +1000561 drm_mode_config_reset(dev);
562
Ben Skeggsfc162082013-09-10 13:20:34 +1000563 if (dev->mode_config.num_crtc) {
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000564 ret = nouveau_display_vblank_init(dev);
Ben Skeggsfc162082013-09-10 13:20:34 +1000565 if (ret)
566 goto vblank_err;
567 }
568
569 nouveau_backlight_init(dev);
Hans de Goede81280d02016-11-21 17:50:54 +0100570 INIT_WORK(&drm->hpd_work, nouveau_display_hpd_work);
Hans de Goede3a6536c2016-11-09 18:17:44 +0100571#ifdef CONFIG_ACPI
Hans de Goede3a6536c2016-11-09 18:17:44 +0100572 drm->acpi_nb.notifier_call = nouveau_display_acpi_ntfy;
573 register_acpi_notifier(&drm->acpi_nb);
574#endif
575
Marcin Slusarz5ace2c92012-04-15 14:36:08 +0200576 return 0;
577
578vblank_err:
Ben Skeggs77145f12012-07-31 16:16:21 +1000579 disp->dtor(dev);
Marcin Slusarz5ace2c92012-04-15 14:36:08 +0200580disp_create_err:
581 drm_kms_helper_poll_fini(dev);
582 drm_mode_config_cleanup(dev);
Ben Skeggs2a44e492011-11-09 11:36:33 +1000583 return ret;
Ben Skeggs27d50302011-10-06 12:46:40 +1000584}
585
586void
587nouveau_display_destroy(struct drm_device *dev)
588{
Ben Skeggs77145f12012-07-31 16:16:21 +1000589 struct nouveau_display *disp = nouveau_display(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000590
Hans de Goede3a6536c2016-11-09 18:17:44 +0100591#ifdef CONFIG_ACPI
592 unregister_acpi_notifier(&nouveau_drm(dev)->acpi_nb);
593#endif
Ben Skeggs77145f12012-07-31 16:16:21 +1000594 nouveau_backlight_exit(dev);
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000595 nouveau_display_vblank_fini(dev);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000596
Ben Skeggsd6bf2f32012-10-30 10:59:12 +1000597 drm_kms_helper_poll_fini(dev);
598 drm_mode_config_cleanup(dev);
599
Ben Skeggs94307382012-10-31 12:11:15 +1000600 if (disp->dtor)
601 disp->dtor(dev);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000602
Ben Skeggs0ad72862014-08-10 04:10:22 +1000603 nvif_object_fini(&disp->disp);
Ben Skeggs2332b312014-01-22 12:58:12 +1000604
Ben Skeggs77145f12012-07-31 16:16:21 +1000605 nouveau_drm(dev)->display = NULL;
606 kfree(disp);
607}
608
609int
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000610nouveau_display_suspend(struct drm_device *dev, bool runtime)
Ben Skeggs77145f12012-07-31 16:16:21 +1000611{
Ben Skeggsb167db02016-11-04 17:20:36 +1000612 struct nouveau_display *disp = nouveau_display(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000613 struct drm_crtc *crtc;
614
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -0800615 if (drm_drv_uses_atomic_modeset(dev)) {
Ben Skeggsb167db02016-11-04 17:20:36 +1000616 if (!runtime) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +0100617 disp->suspend = drm_atomic_helper_suspend(dev);
Ben Skeggsb167db02016-11-04 17:20:36 +1000618 if (IS_ERR(disp->suspend)) {
619 int ret = PTR_ERR(disp->suspend);
620 disp->suspend = NULL;
621 return ret;
622 }
623 }
624
625 nouveau_display_fini(dev, true);
626 return 0;
627 }
628
Ben Skeggs3b4c0ab2016-11-04 17:20:35 +1000629 nouveau_display_fini(dev, true);
Ben Skeggs77145f12012-07-31 16:16:21 +1000630
Ben Skeggs77145f12012-07-31 16:16:21 +1000631 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
632 struct nouveau_framebuffer *nouveau_fb;
633
Matt Roperf4510a22014-04-01 15:22:40 -0700634 nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs77145f12012-07-31 16:16:21 +1000635 if (!nouveau_fb || !nouveau_fb->nvbo)
636 continue;
637
638 nouveau_bo_unpin(nouveau_fb->nvbo);
639 }
640
641 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
642 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs5a560252014-11-10 15:52:02 +1000643 if (nv_crtc->cursor.nvbo) {
Maarten Lankhorst4dc63932015-01-13 09:18:49 +0100644 if (nv_crtc->cursor.set_offset)
645 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Ben Skeggs5a560252014-11-10 15:52:02 +1000646 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
647 }
Ben Skeggs77145f12012-07-31 16:16:21 +1000648 }
649
650 return 0;
651}
652
653void
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000654nouveau_display_resume(struct drm_device *dev, bool runtime)
Ben Skeggs77145f12012-07-31 16:16:21 +1000655{
Ben Skeggsb167db02016-11-04 17:20:36 +1000656 struct nouveau_display *disp = nouveau_display(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000657 struct nouveau_drm *drm = nouveau_drm(dev);
658 struct drm_crtc *crtc;
Daniel Vetter9bc6db0d2016-11-15 22:58:19 +0100659 int ret;
Ben Skeggs77145f12012-07-31 16:16:21 +1000660
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -0800661 if (drm_drv_uses_atomic_modeset(dev)) {
Ben Skeggsb167db02016-11-04 17:20:36 +1000662 nouveau_display_init(dev);
663 if (disp->suspend) {
664 drm_atomic_helper_resume(dev, disp->suspend);
665 disp->suspend = NULL;
666 }
667 return;
668 }
669
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000670 /* re-pin fb/cursors */
Ben Skeggs77145f12012-07-31 16:16:21 +1000671 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
672 struct nouveau_framebuffer *nouveau_fb;
673
Matt Roperf4510a22014-04-01 15:22:40 -0700674 nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs77145f12012-07-31 16:16:21 +1000675 if (!nouveau_fb || !nouveau_fb->nvbo)
676 continue;
677
Ben Skeggs547ad072014-11-10 12:35:06 +1000678 ret = nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggsc9a6fd92014-08-12 23:35:35 +1000679 if (ret)
680 NV_ERROR(drm, "Could not pin framebuffer\n");
Ben Skeggs77145f12012-07-31 16:16:21 +1000681 }
682
683 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
684 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs5a560252014-11-10 15:52:02 +1000685 if (!nv_crtc->cursor.nvbo)
686 continue;
Ben Skeggs77145f12012-07-31 16:16:21 +1000687
Ben Skeggs547ad072014-11-10 12:35:06 +1000688 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +0100689 if (!ret && nv_crtc->cursor.set_offset)
Ben Skeggs77145f12012-07-31 16:16:21 +1000690 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
691 if (ret)
692 NV_ERROR(drm, "Could not pin/map cursor.\n");
693 }
Mario Kleiner9cba5ef2014-07-29 02:36:44 +0200694
Ben Skeggs77145f12012-07-31 16:16:21 +1000695 nouveau_display_init(dev);
696
697 /* Force CLUT to get re-loaded during modeset */
698 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
699 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
700
701 nv_crtc->lut.depth = 0;
702 }
703
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000704 /* This should ensure we don't hit a locking problem when someone
705 * wakes us up via a connector. We should never go into suspend
706 * while the display is on anyways.
707 */
708 if (runtime)
709 return;
710
Ben Skeggs77145f12012-07-31 16:16:21 +1000711 drm_helper_resume_force_mode(dev);
712
713 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
714 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs77145f12012-07-31 16:16:21 +1000715
Ben Skeggs5a560252014-11-10 15:52:02 +1000716 if (!nv_crtc->cursor.nvbo)
Ben Skeggs036a12b2014-11-03 14:33:01 +1000717 continue;
Maarten Lankhorst4dc63932015-01-13 09:18:49 +0100718
719 if (nv_crtc->cursor.set_offset)
720 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
Ben Skeggs77145f12012-07-31 16:16:21 +1000721 nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
722 nv_crtc->cursor_saved_y);
723 }
Ben Skeggs27d50302011-10-06 12:46:40 +1000724}
725
Francisco Jerez332b2422010-10-20 23:35:40 +0200726static int
Francisco Jerez332b2422010-10-20 23:35:40 +0200727nouveau_page_flip_emit(struct nouveau_channel *chan,
728 struct nouveau_bo *old_bo,
729 struct nouveau_bo *new_bo,
730 struct nouveau_page_flip_state *s,
731 struct nouveau_fence **pfence)
732{
Ben Skeggsf589be82012-07-22 11:55:54 +1000733 struct nouveau_fence_chan *fctx = chan->fence;
Ben Skeggs77145f12012-07-31 16:16:21 +1000734 struct nouveau_drm *drm = chan->drm;
735 struct drm_device *dev = drm->dev;
Francisco Jerez332b2422010-10-20 23:35:40 +0200736 unsigned long flags;
737 int ret;
738
739 /* Queue it to the pending list */
740 spin_lock_irqsave(&dev->event_lock, flags);
Ben Skeggsf589be82012-07-22 11:55:54 +1000741 list_add_tail(&s->head, &fctx->flip);
Francisco Jerez332b2422010-10-20 23:35:40 +0200742 spin_unlock_irqrestore(&dev->event_lock, flags);
743
744 /* Synchronize with the old framebuffer */
Maarten Lankhorste3be4c22014-09-16 11:15:07 +0200745 ret = nouveau_fence_sync(old_bo, chan, false, false);
Francisco Jerez332b2422010-10-20 23:35:40 +0200746 if (ret)
747 goto fail;
748
749 /* Emit the pageflip */
Ben Skeggs1e303c02013-11-13 10:49:46 +1000750 ret = RING_SPACE(chan, 2);
Francisco Jerez332b2422010-10-20 23:35:40 +0200751 if (ret)
752 goto fail;
753
Ben Skeggse1ef6b42016-11-04 17:20:36 +1000754 BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
Ben Skeggs1e303c02013-11-13 10:49:46 +1000755 OUT_RING (chan, 0x00000000);
Ben Skeggsbd2f2032011-02-08 15:16:23 +1000756 FIRE_RING (chan);
Francisco Jerez332b2422010-10-20 23:35:40 +0200757
Ben Skeggs264ce192013-02-14 13:43:21 +1000758 ret = nouveau_fence_new(chan, false, pfence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200759 if (ret)
760 goto fail;
761
762 return 0;
763fail:
764 spin_lock_irqsave(&dev->event_lock, flags);
765 list_del(&s->head);
766 spin_unlock_irqrestore(&dev->event_lock, flags);
767 return ret;
768}
769
770int
771nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Daniel Vetter41292b1f2017-03-22 22:50:50 +0100772 struct drm_pending_vblank_event *event, u32 flags,
773 struct drm_modeset_acquire_ctx *ctx)
Francisco Jerez332b2422010-10-20 23:35:40 +0200774{
Ben Skeggsb9d9dcd2013-11-11 13:59:40 +1000775 const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
Francisco Jerez332b2422010-10-20 23:35:40 +0200776 struct drm_device *dev = crtc->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000777 struct nouveau_drm *drm = nouveau_drm(dev);
Matt Roperf4510a22014-04-01 15:22:40 -0700778 struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->primary->fb)->nvbo;
Francisco Jerez332b2422010-10-20 23:35:40 +0200779 struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
780 struct nouveau_page_flip_state *s;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000781 struct nouveau_channel *chan;
782 struct nouveau_cli *cli;
Francisco Jerez332b2422010-10-20 23:35:40 +0200783 struct nouveau_fence *fence;
Ben Skeggse1ef6b42016-11-04 17:20:36 +1000784 struct nv04_display *dispnv04 = nv04_display(dev);
785 int head = nouveau_crtc(crtc)->index;
Francisco Jerez332b2422010-10-20 23:35:40 +0200786 int ret;
787
Ben Skeggs0ad72862014-08-10 04:10:22 +1000788 chan = drm->channel;
789 if (!chan)
Francisco Jerez332b2422010-10-20 23:35:40 +0200790 return -ENODEV;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000791 cli = (void *)chan->user.client;
Francisco Jerez332b2422010-10-20 23:35:40 +0200792
793 s = kzalloc(sizeof(*s), GFP_KERNEL);
794 if (!s)
795 return -ENOMEM;
796
Maarten Lankhorstd5c1e842014-01-14 16:48:58 +0100797 if (new_bo != old_bo) {
Ben Skeggs547ad072014-11-10 12:35:06 +1000798 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM, true);
Maarten Lankhorstd5c1e842014-01-14 16:48:58 +0100799 if (ret)
800 goto fail_free;
801 }
802
Ben Skeggs0ad72862014-08-10 04:10:22 +1000803 mutex_lock(&cli->mutex);
Christian Königdfd5e502016-04-06 11:12:03 +0200804 ret = ttm_bo_reserve(&new_bo->bo, true, false, NULL);
Maarten Lankhorstbdaf7dd2014-01-21 13:02:14 +0100805 if (ret)
806 goto fail_unpin;
Maarten Lankhorstd5c1e842014-01-14 16:48:58 +0100807
Ben Skeggseae389f2013-11-13 10:17:17 +1000808 /* synchronise rendering channel with the kernel's channel */
Maarten Lankhorste3be4c22014-09-16 11:15:07 +0200809 ret = nouveau_fence_sync(new_bo, chan, false, true);
Maarten Lankhorstbdaf7dd2014-01-21 13:02:14 +0100810 if (ret) {
811 ttm_bo_unreserve(&new_bo->bo);
Maarten Lankhorst09c3de12014-01-29 11:05:09 +0100812 goto fail_unpin;
Maarten Lankhorstbdaf7dd2014-01-21 13:02:14 +0100813 }
Maarten Lankhorstb580c9e2013-06-27 13:48:18 +0200814
Maarten Lankhorstbdaf7dd2014-01-21 13:02:14 +0100815 if (new_bo != old_bo) {
816 ttm_bo_unreserve(&new_bo->bo);
817
Christian Königdfd5e502016-04-06 11:12:03 +0200818 ret = ttm_bo_reserve(&old_bo->bo, true, false, NULL);
Maarten Lankhorstbdaf7dd2014-01-21 13:02:14 +0100819 if (ret)
820 goto fail_unpin;
821 }
Francisco Jerez332b2422010-10-20 23:35:40 +0200822
823 /* Initialize a page flip struct */
824 *s = (struct nouveau_page_flip_state)
Ville Syrjälä272725c2016-12-14 23:32:20 +0200825 { { }, event, crtc, fb->format->cpp[0] * 8, fb->pitches[0],
Francisco Jerez332b2422010-10-20 23:35:40 +0200826 new_bo->bo.offset };
827
Mario Kleinerba124a412014-06-11 09:51:23 +0200828 /* Keep vblanks on during flip, for the target crtc of this flip */
Gustavo Padovandc4ff112016-06-07 11:07:53 -0300829 drm_crtc_vblank_get(crtc);
Mario Kleinerba124a412014-06-11 09:51:23 +0200830
Francisco Jerez332b2422010-10-20 23:35:40 +0200831 /* Emit a page flip */
Ben Skeggse1ef6b42016-11-04 17:20:36 +1000832 if (swap_interval) {
833 ret = RING_SPACE(chan, 8);
Ben Skeggs060810d2013-07-08 14:15:51 +1000834 if (ret)
Ben Skeggsd7117e02011-02-07 14:27:04 +1000835 goto fail_unreserve;
Ben Skeggsb9d9dcd2013-11-11 13:59:40 +1000836
Ben Skeggse1ef6b42016-11-04 17:20:36 +1000837 BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
838 OUT_RING (chan, 0);
839 BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
840 OUT_RING (chan, head);
841 BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
842 OUT_RING (chan, 0);
843 BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
844 OUT_RING (chan, 0);
Ben Skeggsd7117e02011-02-07 14:27:04 +1000845 }
846
Ben Skeggse1ef6b42016-11-04 17:20:36 +1000847 nouveau_bo_ref(new_bo, &dispnv04->image[head]);
848
Francisco Jerez332b2422010-10-20 23:35:40 +0200849 ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200850 if (ret)
851 goto fail_unreserve;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000852 mutex_unlock(&cli->mutex);
Francisco Jerez332b2422010-10-20 23:35:40 +0200853
854 /* Update the crtc struct and cleanup */
Matt Roperf4510a22014-04-01 15:22:40 -0700855 crtc->primary->fb = fb;
Francisco Jerez332b2422010-10-20 23:35:40 +0200856
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200857 nouveau_bo_fence(old_bo, fence, false);
Ben Skeggs07ad6ca2013-11-13 10:00:32 +1000858 ttm_bo_unreserve(&old_bo->bo);
Ben Skeggs060810d2013-07-08 14:15:51 +1000859 if (old_bo != new_bo)
Maarten Lankhorstb580c9e2013-06-27 13:48:18 +0200860 nouveau_bo_unpin(old_bo);
Francisco Jerez332b2422010-10-20 23:35:40 +0200861 nouveau_fence_unref(&fence);
862 return 0;
863
864fail_unreserve:
Gustavo Padovandc4ff112016-06-07 11:07:53 -0300865 drm_crtc_vblank_put(crtc);
Ben Skeggs07ad6ca2013-11-13 10:00:32 +1000866 ttm_bo_unreserve(&old_bo->bo);
Ben Skeggs060810d2013-07-08 14:15:51 +1000867fail_unpin:
Ben Skeggs0ad72862014-08-10 04:10:22 +1000868 mutex_unlock(&cli->mutex);
Ben Skeggs060810d2013-07-08 14:15:51 +1000869 if (old_bo != new_bo)
Maarten Lankhorstb580c9e2013-06-27 13:48:18 +0200870 nouveau_bo_unpin(new_bo);
Francisco Jerez332b2422010-10-20 23:35:40 +0200871fail_free:
872 kfree(s);
873 return ret;
874}
875
876int
877nouveau_finish_page_flip(struct nouveau_channel *chan,
878 struct nouveau_page_flip_state *ps)
879{
Ben Skeggsf589be82012-07-22 11:55:54 +1000880 struct nouveau_fence_chan *fctx = chan->fence;
Ben Skeggs77145f12012-07-31 16:16:21 +1000881 struct nouveau_drm *drm = chan->drm;
882 struct drm_device *dev = drm->dev;
Francisco Jerez332b2422010-10-20 23:35:40 +0200883 struct nouveau_page_flip_state *s;
884 unsigned long flags;
885
886 spin_lock_irqsave(&dev->event_lock, flags);
887
Ben Skeggsf589be82012-07-22 11:55:54 +1000888 if (list_empty(&fctx->flip)) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000889 NV_ERROR(drm, "unexpected pageflip\n");
Francisco Jerez332b2422010-10-20 23:35:40 +0200890 spin_unlock_irqrestore(&dev->event_lock, flags);
891 return -EINVAL;
892 }
893
Ben Skeggsf589be82012-07-22 11:55:54 +1000894 s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
Mario Kleineraf4870e2014-05-13 00:42:08 +0200895 if (s->event) {
Ben Skeggse1ef6b42016-11-04 17:20:36 +1000896 drm_crtc_arm_vblank_event(s->crtc, s->event);
897 } else {
Daniel Vetterbbc87642015-11-10 17:37:31 +0100898 /* Give up ownership of vblank for page-flipped crtc */
Gustavo Padovandc4ff112016-06-07 11:07:53 -0300899 drm_crtc_vblank_put(s->crtc);
Daniel Vetterbbc87642015-11-10 17:37:31 +0100900 }
Mario Kleinerba124a412014-06-11 09:51:23 +0200901
Francisco Jerez332b2422010-10-20 23:35:40 +0200902 list_del(&s->head);
Ben Skeggsd7117e02011-02-07 14:27:04 +1000903 if (ps)
904 *ps = *s;
Francisco Jerez332b2422010-10-20 23:35:40 +0200905 kfree(s);
906
907 spin_unlock_irqrestore(&dev->event_lock, flags);
908 return 0;
909}
Ben Skeggs33dbc272011-09-30 08:55:50 +1000910
911int
Ben Skeggs898a2b32015-08-20 14:54:18 +1000912nouveau_flip_complete(struct nvif_notify *notify)
Ben Skeggsf589be82012-07-22 11:55:54 +1000913{
Ben Skeggs898a2b32015-08-20 14:54:18 +1000914 struct nouveau_drm *drm = container_of(notify, typeof(*drm), flip);
915 struct nouveau_channel *chan = drm->channel;
Ben Skeggsf589be82012-07-22 11:55:54 +1000916 struct nouveau_page_flip_state state;
917
918 if (!nouveau_finish_page_flip(chan, &state)) {
Ben Skeggse1ef6b42016-11-04 17:20:36 +1000919 nv_set_crtc_base(drm->dev, drm_crtc_index(state.crtc),
920 state.offset + state.crtc->y *
921 state.pitch + state.crtc->x *
922 state.bpp / 8);
Ben Skeggsf589be82012-07-22 11:55:54 +1000923 }
924
Ben Skeggs898a2b32015-08-20 14:54:18 +1000925 return NVIF_NOTIFY_KEEP;
Ben Skeggsf589be82012-07-22 11:55:54 +1000926}
927
928int
Ben Skeggs33dbc272011-09-30 08:55:50 +1000929nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
930 struct drm_mode_create_dumb *args)
931{
Ben Skeggsfc1b0a02016-05-24 17:29:55 +1000932 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs33dbc272011-09-30 08:55:50 +1000933 struct nouveau_bo *bo;
Alexandre Courboteaecf032015-02-20 18:22:59 +0900934 uint32_t domain;
Ben Skeggs33dbc272011-09-30 08:55:50 +1000935 int ret;
936
937 args->pitch = roundup(args->width * (args->bpp / 8), 256);
938 args->size = args->pitch * args->height;
939 args->size = roundup(args->size, PAGE_SIZE);
940
Alexandre Courboteaecf032015-02-20 18:22:59 +0900941 /* Use VRAM if there is any ; otherwise fallback to system memory */
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000942 if (nouveau_drm(dev)->client.device.info.ram_size != 0)
Alexandre Courboteaecf032015-02-20 18:22:59 +0900943 domain = NOUVEAU_GEM_DOMAIN_VRAM;
944 else
945 domain = NOUVEAU_GEM_DOMAIN_GART;
946
Ben Skeggsfc1b0a02016-05-24 17:29:55 +1000947 ret = nouveau_gem_new(cli, args->size, 0, domain, 0, 0, &bo);
Ben Skeggs33dbc272011-09-30 08:55:50 +1000948 if (ret)
949 return ret;
950
David Herrmann55fb74a2013-10-02 10:15:17 +0200951 ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
952 drm_gem_object_unreference_unlocked(&bo->gem);
Ben Skeggs33dbc272011-09-30 08:55:50 +1000953 return ret;
954}
955
956int
Ben Skeggs33dbc272011-09-30 08:55:50 +1000957nouveau_display_dumb_map_offset(struct drm_file *file_priv,
958 struct drm_device *dev,
959 uint32_t handle, uint64_t *poffset)
960{
961 struct drm_gem_object *gem;
962
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100963 gem = drm_gem_object_lookup(file_priv, handle);
Ben Skeggs33dbc272011-09-30 08:55:50 +1000964 if (gem) {
David Herrmann55fb74a2013-10-02 10:15:17 +0200965 struct nouveau_bo *bo = nouveau_gem_object(gem);
David Herrmann72525b32013-07-24 21:08:53 +0200966 *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
Ben Skeggs33dbc272011-09-30 08:55:50 +1000967 drm_gem_object_unreference_unlocked(gem);
968 return 0;
969 }
970
971 return -ENOENT;
972}