blob: 0f6de6049be4412f9accf9eb1f637d22589ffde3 [file] [log] [blame]
Ben Skeggs15907002018-05-08 20:39:47 +10001/*
2 * Copyright 2018 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#include "wndw.h"
23
24#include <nvif/class.h>
25#include <nvif/cl0002.h>
26
27#include <drm/drm_atomic_helper.h>
28#include "nouveau_bo.h"
29
30static void
31nv50_wndw_ctxdma_del(struct nv50_wndw_ctxdma *ctxdma)
32{
33 nvif_object_fini(&ctxdma->object);
34 list_del(&ctxdma->head);
35 kfree(ctxdma);
36}
37
38static struct nv50_wndw_ctxdma *
39nv50_wndw_ctxdma_new(struct nv50_wndw *wndw, struct nouveau_framebuffer *fb)
40{
41 struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
42 struct nv50_wndw_ctxdma *ctxdma;
43 const u8 kind = fb->nvbo->kind;
44 const u32 handle = 0xfb000000 | kind;
45 struct {
46 struct nv_dma_v0 base;
47 union {
48 struct nv50_dma_v0 nv50;
49 struct gf100_dma_v0 gf100;
50 struct gf119_dma_v0 gf119;
51 };
52 } args = {};
53 u32 argc = sizeof(args.base);
54 int ret;
55
56 list_for_each_entry(ctxdma, &wndw->ctxdma.list, head) {
57 if (ctxdma->object.handle == handle)
58 return ctxdma;
59 }
60
61 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
62 return ERR_PTR(-ENOMEM);
63 list_add(&ctxdma->head, &wndw->ctxdma.list);
64
65 args.base.target = NV_DMA_V0_TARGET_VRAM;
66 args.base.access = NV_DMA_V0_ACCESS_RDWR;
67 args.base.start = 0;
68 args.base.limit = drm->client.device.info.ram_user - 1;
69
70 if (drm->client.device.info.chipset < 0x80) {
71 args.nv50.part = NV50_DMA_V0_PART_256;
72 argc += sizeof(args.nv50);
73 } else
74 if (drm->client.device.info.chipset < 0xc0) {
75 args.nv50.part = NV50_DMA_V0_PART_256;
76 args.nv50.kind = kind;
77 argc += sizeof(args.nv50);
78 } else
79 if (drm->client.device.info.chipset < 0xd0) {
80 args.gf100.kind = kind;
81 argc += sizeof(args.gf100);
82 } else {
83 args.gf119.page = GF119_DMA_V0_PAGE_LP;
84 args.gf119.kind = kind;
85 argc += sizeof(args.gf119);
86 }
87
88 ret = nvif_object_init(wndw->ctxdma.parent, handle, NV_DMA_IN_MEMORY,
89 &args, argc, &ctxdma->object);
90 if (ret) {
91 nv50_wndw_ctxdma_del(ctxdma);
92 return ERR_PTR(ret);
93 }
94
95 return ctxdma;
96}
97
98int
99nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
100{
Ben Skeggsccd27db2018-05-08 20:39:47 +1000101 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
102 if (asyw->set.ntfy) {
103 return wndw->func->ntfy_wait_begun(disp->sync,
104 asyw->ntfy.offset,
105 wndw->wndw.base.device);
106 }
Ben Skeggs15907002018-05-08 20:39:47 +1000107 return 0;
108}
109
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000110void
111nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 *interlock, bool flush,
Ben Skeggs15907002018-05-08 20:39:47 +1000112 struct nv50_wndw_atom *asyw)
113{
Ben Skeggsf88bc9d32018-05-08 20:39:47 +1000114 union nv50_wndw_atom_mask clr = {
115 .mask = asyw->clr.mask & ~(flush ? 0 : asyw->set.mask),
116 };
117 if (clr.sema ) wndw->func-> sema_clr(wndw);
118 if (clr.ntfy ) wndw->func-> ntfy_clr(wndw);
119 if (clr.image) wndw->func->image_clr(wndw);
Ben Skeggs15907002018-05-08 20:39:47 +1000120
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000121 interlock[wndw->interlock.type] |= wndw->interlock.data;
Ben Skeggs15907002018-05-08 20:39:47 +1000122}
123
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000124void
125nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 *interlock,
Ben Skeggs15907002018-05-08 20:39:47 +1000126 struct nv50_wndw_atom *asyw)
127{
128 if (interlock) {
129 asyw->image.mode = 0;
130 asyw->image.interval = 1;
131 }
132
133 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
134 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
135 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
136 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
137 if (asyw->set.point) {
138 wndw->immd->point(wndw, asyw);
139 wndw->immd->update(wndw, interlock);
140 }
141
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000142 interlock[wndw->interlock.type] |= wndw->interlock.data;
Ben Skeggs15907002018-05-08 20:39:47 +1000143}
144
Ben Skeggsccd27db2018-05-08 20:39:47 +1000145void
146nv50_wndw_ntfy_enable(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
147{
148 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
149
150 asyw->ntfy.handle = wndw->wndw.sync.handle;
151 asyw->ntfy.offset = wndw->ntfy;
152 asyw->ntfy.awaken = false;
153 asyw->set.ntfy = true;
154
155 wndw->func->ntfy_reset(disp->sync, wndw->ntfy);
156 wndw->ntfy ^= 0x10;
157}
158
Ben Skeggs15907002018-05-08 20:39:47 +1000159static void
160nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
161 struct nv50_wndw_atom *asyw,
162 struct nv50_head_atom *asyh)
163{
164 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
165 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
166 wndw->func->release(wndw, asyw, asyh);
167 asyw->ntfy.handle = 0;
168 asyw->sema.handle = 0;
169}
170
171static int
Ben Skeggs43c181e2018-05-08 20:39:47 +1000172nv50_wndw_atomic_check_acquire_rgb(struct nv50_wndw_atom *asyw)
173{
174 switch (asyw->state.fb->format->format) {
175 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
176 case DRM_FORMAT_XRGB8888 :
177 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
178 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
179 case DRM_FORMAT_XRGB1555 :
180 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
181 case DRM_FORMAT_XBGR2101010:
182 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
183 case DRM_FORMAT_XBGR8888 :
184 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
185 default:
186 WARN_ON(1);
187 return -EINVAL;
188 }
189 return 0;
190}
191
192static int
Ben Skeggse349a052018-05-08 20:39:47 +1000193nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw, bool modeset,
194 struct nv50_wndw_atom *armw,
Ben Skeggs15907002018-05-08 20:39:47 +1000195 struct nv50_wndw_atom *asyw,
196 struct nv50_head_atom *asyh)
197{
198 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
199 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
200 int ret;
201
202 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
203
Ben Skeggse349a052018-05-08 20:39:47 +1000204 if (asyw->state.fb != armw->state.fb || !armw->visible || modeset) {
205 asyw->image.w = fb->base.width;
206 asyw->image.h = fb->base.height;
207 asyw->image.kind = fb->nvbo->kind;
Ben Skeggs15907002018-05-08 20:39:47 +1000208
Ben Skeggse349a052018-05-08 20:39:47 +1000209 ret = nv50_wndw_atomic_check_acquire_rgb(asyw);
210 if (ret)
211 return ret;
Ben Skeggs43c181e2018-05-08 20:39:47 +1000212
Ben Skeggse349a052018-05-08 20:39:47 +1000213 if (asyw->image.kind) {
214 asyw->image.layout = 0;
215 if (drm->client.device.info.chipset >= 0xc0)
216 asyw->image.block = fb->nvbo->mode >> 4;
217 else
218 asyw->image.block = fb->nvbo->mode;
219 asyw->image.pitch[0] = (fb->base.pitches[0] / 4) << 4;
220 } else {
221 asyw->image.layout = 1;
222 asyw->image.block = 0;
223 asyw->image.pitch[0] = fb->base.pitches[0];
224 }
Ben Skeggs15907002018-05-08 20:39:47 +1000225
Ben Skeggs45a29452018-05-08 20:39:47 +1000226 if (!(asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC))
227 asyw->image.interval = 1;
Ben Skeggs15907002018-05-08 20:39:47 +1000228 else
229 asyw->image.interval = 0;
Ben Skeggs45a29452018-05-08 20:39:47 +1000230 asyw->image.mode = asyw->image.interval ? 0 : 1;
Ben Skeggse349a052018-05-08 20:39:47 +1000231 asyw->set.image = wndw->func->image_set != NULL;
Ben Skeggs15907002018-05-08 20:39:47 +1000232 }
233
Ben Skeggse349a052018-05-08 20:39:47 +1000234 if (wndw->immd) {
235 asyw->point.x = asyw->state.crtc_x;
236 asyw->point.y = asyw->state.crtc_y;
237 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
238 asyw->set.point = true;
239 }
240
241 return wndw->func->acquire(wndw, asyw, asyh);
Ben Skeggs15907002018-05-08 20:39:47 +1000242}
243
244int
245nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
246{
247 struct nouveau_drm *drm = nouveau_drm(plane->dev);
248 struct nv50_wndw *wndw = nv50_wndw(plane);
249 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
250 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
251 struct nv50_head_atom *harm = NULL, *asyh = NULL;
Ben Skeggs859b4562018-05-08 20:39:47 +1000252 bool modeset = false;
Ben Skeggs15907002018-05-08 20:39:47 +1000253 int ret;
254
255 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
Ben Skeggs859b4562018-05-08 20:39:47 +1000256
257 /* Fetch the assembly state for the head the window will belong to,
258 * and determine whether the window will be visible.
259 */
Ben Skeggs15907002018-05-08 20:39:47 +1000260 if (asyw->state.crtc) {
261 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
262 if (IS_ERR(asyh))
263 return PTR_ERR(asyh);
Ben Skeggs859b4562018-05-08 20:39:47 +1000264 modeset = drm_atomic_crtc_needs_modeset(&asyh->state);
265 asyw->visible = asyh->state.active;
266 } else {
267 asyw->visible = false;
Ben Skeggs15907002018-05-08 20:39:47 +1000268 }
269
Ben Skeggs859b4562018-05-08 20:39:47 +1000270 /* Fetch assembly state for the head the window used to belong to. */
Ben Skeggs15907002018-05-08 20:39:47 +1000271 if (armw->state.crtc) {
272 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
273 if (IS_ERR(harm))
274 return PTR_ERR(harm);
Ben Skeggs15907002018-05-08 20:39:47 +1000275 }
276
Ben Skeggs859b4562018-05-08 20:39:47 +1000277 /* Calculate new window state. */
278 if (asyw->visible) {
Ben Skeggse349a052018-05-08 20:39:47 +1000279 ret = nv50_wndw_atomic_check_acquire(wndw, modeset,
280 armw, asyw, asyh);
Ben Skeggs15907002018-05-08 20:39:47 +1000281 if (ret)
282 return ret;
283 } else
Ben Skeggs859b4562018-05-08 20:39:47 +1000284 if (armw->visible) {
Ben Skeggs15907002018-05-08 20:39:47 +1000285 nv50_wndw_atomic_check_release(wndw, asyw, harm);
286 } else {
287 return 0;
288 }
289
Ben Skeggs859b4562018-05-08 20:39:47 +1000290 /* Aside from the obvious case where the window is actively being
291 * disabled, we might also need to temporarily disable the window
292 * when performing certain modeset operations.
293 */
294 if (!asyw->visible || modeset) {
Ben Skeggs15907002018-05-08 20:39:47 +1000295 asyw->clr.ntfy = armw->ntfy.handle != 0;
296 asyw->clr.sema = armw->sema.handle != 0;
297 if (wndw->func->image_clr)
Ben Skeggs261fcfa2018-05-08 20:39:47 +1000298 asyw->clr.image = armw->image.handle[0] != 0;
Ben Skeggs859b4562018-05-08 20:39:47 +1000299 asyw->set.lut = wndw->func->lut && asyw->visible;
Ben Skeggs15907002018-05-08 20:39:47 +1000300 }
301
302 return 0;
303}
304
305static void
306nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
307{
308 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
309 struct nouveau_drm *drm = nouveau_drm(plane->dev);
310
311 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
312 if (!old_state->fb)
313 return;
314
315 nouveau_bo_unpin(fb->nvbo);
316}
317
318static int
319nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
320{
321 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
322 struct nouveau_drm *drm = nouveau_drm(plane->dev);
323 struct nv50_wndw *wndw = nv50_wndw(plane);
324 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
325 struct nv50_head_atom *asyh;
326 struct nv50_wndw_ctxdma *ctxdma;
327 int ret;
328
329 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
330 if (!asyw->state.fb)
331 return 0;
332
333 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
334 if (ret)
335 return ret;
336
337 ctxdma = nv50_wndw_ctxdma_new(wndw, fb);
338 if (IS_ERR(ctxdma)) {
339 nouveau_bo_unpin(fb->nvbo);
340 return PTR_ERR(ctxdma);
341 }
342
343 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
Ben Skeggs261fcfa2018-05-08 20:39:47 +1000344 asyw->image.handle[0] = ctxdma->object.handle;
345 asyw->image.offset[0] = fb->nvbo->bo.offset;
Ben Skeggs15907002018-05-08 20:39:47 +1000346
347 if (wndw->func->prepare) {
348 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
349 if (IS_ERR(asyh))
350 return PTR_ERR(asyh);
351
352 wndw->func->prepare(wndw, asyh, asyw);
353 }
354
355 return 0;
356}
357
358static const struct drm_plane_helper_funcs
359nv50_wndw_helper = {
360 .prepare_fb = nv50_wndw_prepare_fb,
361 .cleanup_fb = nv50_wndw_cleanup_fb,
362 .atomic_check = nv50_wndw_atomic_check,
363};
364
365static void
366nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
367 struct drm_plane_state *state)
368{
369 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
370 __drm_atomic_helper_plane_destroy_state(&asyw->state);
371 kfree(asyw);
372}
373
374static struct drm_plane_state *
375nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
376{
377 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
378 struct nv50_wndw_atom *asyw;
379 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
380 return NULL;
381 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
Ben Skeggs15907002018-05-08 20:39:47 +1000382 asyw->sema = armw->sema;
383 asyw->ntfy = armw->ntfy;
384 asyw->image = armw->image;
385 asyw->point = armw->point;
386 asyw->lut = armw->lut;
387 asyw->clr.mask = 0;
388 asyw->set.mask = 0;
389 return &asyw->state;
390}
391
392static void
393nv50_wndw_reset(struct drm_plane *plane)
394{
395 struct nv50_wndw_atom *asyw;
396
397 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
398 return;
399
400 if (plane->state)
401 plane->funcs->atomic_destroy_state(plane, plane->state);
402 plane->state = &asyw->state;
403 plane->state->plane = plane;
404 plane->state->rotation = DRM_MODE_ROTATE_0;
405}
406
407static void
408nv50_wndw_destroy(struct drm_plane *plane)
409{
410 struct nv50_wndw *wndw = nv50_wndw(plane);
411 struct nv50_wndw_ctxdma *ctxdma, *ctxtmp;
412
413 list_for_each_entry_safe(ctxdma, ctxtmp, &wndw->ctxdma.list, head) {
414 nv50_wndw_ctxdma_del(ctxdma);
415 }
416
417 nvif_notify_fini(&wndw->notify);
418 nv50_dmac_destroy(&wndw->wimm);
419 nv50_dmac_destroy(&wndw->wndw);
420 drm_plane_cleanup(&wndw->plane);
421 kfree(wndw);
422}
423
424const struct drm_plane_funcs
425nv50_wndw = {
426 .update_plane = drm_atomic_helper_update_plane,
427 .disable_plane = drm_atomic_helper_disable_plane,
428 .destroy = nv50_wndw_destroy,
429 .reset = nv50_wndw_reset,
430 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
431 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
432};
433
434static int
435nv50_wndw_notify(struct nvif_notify *notify)
436{
437 return NVIF_NOTIFY_KEEP;
438}
439
440void
441nv50_wndw_fini(struct nv50_wndw *wndw)
442{
443 nvif_notify_put(&wndw->notify);
444}
445
446void
447nv50_wndw_init(struct nv50_wndw *wndw)
448{
449 nvif_notify_get(&wndw->notify);
450}
451
452int
453nv50_wndw_new_(const struct nv50_wndw_func *func, struct drm_device *dev,
454 enum drm_plane_type type, const char *name, int index,
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000455 const u32 *format, u32 heads,
456 enum nv50_disp_interlock_type interlock_type, u32 interlock_data,
457 struct nv50_wndw **pwndw)
Ben Skeggs15907002018-05-08 20:39:47 +1000458{
459 struct nv50_wndw *wndw;
460 int nformat;
461 int ret;
462
463 if (!(wndw = *pwndw = kzalloc(sizeof(*wndw), GFP_KERNEL)))
464 return -ENOMEM;
465 wndw->func = func;
466 wndw->id = index;
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000467 wndw->interlock.type = interlock_type;
468 wndw->interlock.data = interlock_data;
469 wndw->ctxdma.parent = &wndw->wndw.base.user;
Ben Skeggs15907002018-05-08 20:39:47 +1000470
471 wndw->ctxdma.parent = &wndw->wndw.base.user;
472 INIT_LIST_HEAD(&wndw->ctxdma.list);
473
474 for (nformat = 0; format[nformat]; nformat++);
475
Ben Skeggs9d6c2fe2018-05-08 20:39:47 +1000476 ret = drm_universal_plane_init(dev, &wndw->plane, heads, &nv50_wndw,
Ben Skeggs15907002018-05-08 20:39:47 +1000477 format, nformat, NULL,
478 type, "%s-%d", name, index);
479 if (ret) {
480 kfree(*pwndw);
481 *pwndw = NULL;
482 return ret;
483 }
484
485 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
486
487 wndw->notify.func = nv50_wndw_notify;
488 return 0;
489}