blob: 8f62c2a811ffb2abdb87832d14f9fb6b532c2b66 [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;
122 if (flush)
123 wndw->func->update(wndw, interlock);
Ben Skeggs15907002018-05-08 20:39:47 +1000124}
125
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000126void
127nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 *interlock,
Ben Skeggs15907002018-05-08 20:39:47 +1000128 struct nv50_wndw_atom *asyw)
129{
130 if (interlock) {
131 asyw->image.mode = 0;
132 asyw->image.interval = 1;
133 }
134
135 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
136 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
137 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
138 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
139 if (asyw->set.point) {
140 wndw->immd->point(wndw, asyw);
141 wndw->immd->update(wndw, interlock);
142 }
143
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000144 interlock[wndw->interlock.type] |= wndw->interlock.data;
145 if (wndw->func->update)
146 wndw->func->update(wndw, interlock);
Ben Skeggs15907002018-05-08 20:39:47 +1000147}
148
Ben Skeggsccd27db2018-05-08 20:39:47 +1000149void
150nv50_wndw_ntfy_enable(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
151{
152 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
153
154 asyw->ntfy.handle = wndw->wndw.sync.handle;
155 asyw->ntfy.offset = wndw->ntfy;
156 asyw->ntfy.awaken = false;
157 asyw->set.ntfy = true;
158
159 wndw->func->ntfy_reset(disp->sync, wndw->ntfy);
160 wndw->ntfy ^= 0x10;
161}
162
Ben Skeggs15907002018-05-08 20:39:47 +1000163static void
164nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
165 struct nv50_wndw_atom *asyw,
166 struct nv50_head_atom *asyh)
167{
168 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
169 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
170 wndw->func->release(wndw, asyw, asyh);
171 asyw->ntfy.handle = 0;
172 asyw->sema.handle = 0;
173}
174
175static int
Ben Skeggs43c181e2018-05-08 20:39:47 +1000176nv50_wndw_atomic_check_acquire_rgb(struct nv50_wndw_atom *asyw)
177{
178 switch (asyw->state.fb->format->format) {
179 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
180 case DRM_FORMAT_XRGB8888 :
181 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
182 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
183 case DRM_FORMAT_XRGB1555 :
184 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
185 case DRM_FORMAT_XBGR2101010:
186 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
187 case DRM_FORMAT_XBGR8888 :
188 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
189 default:
190 WARN_ON(1);
191 return -EINVAL;
192 }
193 return 0;
194}
195
196static int
Ben Skeggs15907002018-05-08 20:39:47 +1000197nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
198 struct nv50_wndw_atom *asyw,
199 struct nv50_head_atom *asyh)
200{
201 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
202 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
203 int ret;
204
205 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
206
207 asyw->image.w = fb->base.width;
208 asyw->image.h = fb->base.height;
209 asyw->image.kind = fb->nvbo->kind;
210
Ben Skeggs43c181e2018-05-08 20:39:47 +1000211 ret = nv50_wndw_atomic_check_acquire_rgb(asyw);
212 if (ret)
213 return ret;
214
Ben Skeggs15907002018-05-08 20:39:47 +1000215 if (asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC)
216 asyw->interval = 0;
217 else
218 asyw->interval = 1;
219
220 if (asyw->image.kind) {
221 asyw->image.layout = 0;
222 if (drm->client.device.info.chipset >= 0xc0)
223 asyw->image.block = fb->nvbo->mode >> 4;
224 else
225 asyw->image.block = fb->nvbo->mode;
Ben Skeggs261fcfa2018-05-08 20:39:47 +1000226 asyw->image.pitch[0] = (fb->base.pitches[0] / 4) << 4;
Ben Skeggs15907002018-05-08 20:39:47 +1000227 } else {
228 asyw->image.layout = 1;
229 asyw->image.block = 0;
Ben Skeggs261fcfa2018-05-08 20:39:47 +1000230 asyw->image.pitch[0] = fb->base.pitches[0];
Ben Skeggs15907002018-05-08 20:39:47 +1000231 }
232
233 ret = wndw->func->acquire(wndw, asyw, asyh);
234 if (ret)
235 return ret;
236
237 if (asyw->set.image) {
238 if (!(asyw->image.mode = asyw->interval ? 0 : 1))
239 asyw->image.interval = asyw->interval;
240 else
241 asyw->image.interval = 0;
242 }
243
244 return 0;
245}
246
247int
248nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
249{
250 struct nouveau_drm *drm = nouveau_drm(plane->dev);
251 struct nv50_wndw *wndw = nv50_wndw(plane);
252 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
253 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
254 struct nv50_head_atom *harm = NULL, *asyh = NULL;
255 bool varm = false, asyv = false, asym = false;
256 int ret;
257
258 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
259 if (asyw->state.crtc) {
260 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
261 if (IS_ERR(asyh))
262 return PTR_ERR(asyh);
263 asym = drm_atomic_crtc_needs_modeset(&asyh->state);
264 asyv = asyh->state.active;
265 }
266
267 if (armw->state.crtc) {
268 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
269 if (IS_ERR(harm))
270 return PTR_ERR(harm);
271 varm = harm->state.crtc->state->active;
272 }
273
274 if (asyv) {
275 asyw->point.x = asyw->state.crtc_x;
276 asyw->point.y = asyw->state.crtc_y;
277 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
278 asyw->set.point = true;
279
280 ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh);
281 if (ret)
282 return ret;
283 } else
284 if (varm) {
285 nv50_wndw_atomic_check_release(wndw, asyw, harm);
286 } else {
287 return 0;
288 }
289
290 if (!asyv || asym) {
291 asyw->clr.ntfy = armw->ntfy.handle != 0;
292 asyw->clr.sema = armw->sema.handle != 0;
293 if (wndw->func->image_clr)
Ben Skeggs261fcfa2018-05-08 20:39:47 +1000294 asyw->clr.image = armw->image.handle[0] != 0;
Ben Skeggs15907002018-05-08 20:39:47 +1000295 asyw->set.lut = wndw->func->lut && asyv;
296 }
297
298 return 0;
299}
300
301static void
302nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
303{
304 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
305 struct nouveau_drm *drm = nouveau_drm(plane->dev);
306
307 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
308 if (!old_state->fb)
309 return;
310
311 nouveau_bo_unpin(fb->nvbo);
312}
313
314static int
315nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
316{
317 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
318 struct nouveau_drm *drm = nouveau_drm(plane->dev);
319 struct nv50_wndw *wndw = nv50_wndw(plane);
320 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
321 struct nv50_head_atom *asyh;
322 struct nv50_wndw_ctxdma *ctxdma;
323 int ret;
324
325 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
326 if (!asyw->state.fb)
327 return 0;
328
329 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
330 if (ret)
331 return ret;
332
333 ctxdma = nv50_wndw_ctxdma_new(wndw, fb);
334 if (IS_ERR(ctxdma)) {
335 nouveau_bo_unpin(fb->nvbo);
336 return PTR_ERR(ctxdma);
337 }
338
339 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
Ben Skeggs261fcfa2018-05-08 20:39:47 +1000340 asyw->image.handle[0] = ctxdma->object.handle;
341 asyw->image.offset[0] = fb->nvbo->bo.offset;
Ben Skeggs15907002018-05-08 20:39:47 +1000342
343 if (wndw->func->prepare) {
344 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
345 if (IS_ERR(asyh))
346 return PTR_ERR(asyh);
347
348 wndw->func->prepare(wndw, asyh, asyw);
349 }
350
351 return 0;
352}
353
354static const struct drm_plane_helper_funcs
355nv50_wndw_helper = {
356 .prepare_fb = nv50_wndw_prepare_fb,
357 .cleanup_fb = nv50_wndw_cleanup_fb,
358 .atomic_check = nv50_wndw_atomic_check,
359};
360
361static void
362nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
363 struct drm_plane_state *state)
364{
365 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
366 __drm_atomic_helper_plane_destroy_state(&asyw->state);
367 kfree(asyw);
368}
369
370static struct drm_plane_state *
371nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
372{
373 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
374 struct nv50_wndw_atom *asyw;
375 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
376 return NULL;
377 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
378 asyw->interval = 1;
379 asyw->sema = armw->sema;
380 asyw->ntfy = armw->ntfy;
381 asyw->image = armw->image;
382 asyw->point = armw->point;
383 asyw->lut = armw->lut;
384 asyw->clr.mask = 0;
385 asyw->set.mask = 0;
386 return &asyw->state;
387}
388
389static void
390nv50_wndw_reset(struct drm_plane *plane)
391{
392 struct nv50_wndw_atom *asyw;
393
394 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
395 return;
396
397 if (plane->state)
398 plane->funcs->atomic_destroy_state(plane, plane->state);
399 plane->state = &asyw->state;
400 plane->state->plane = plane;
401 plane->state->rotation = DRM_MODE_ROTATE_0;
402}
403
404static void
405nv50_wndw_destroy(struct drm_plane *plane)
406{
407 struct nv50_wndw *wndw = nv50_wndw(plane);
408 struct nv50_wndw_ctxdma *ctxdma, *ctxtmp;
409
410 list_for_each_entry_safe(ctxdma, ctxtmp, &wndw->ctxdma.list, head) {
411 nv50_wndw_ctxdma_del(ctxdma);
412 }
413
414 nvif_notify_fini(&wndw->notify);
415 nv50_dmac_destroy(&wndw->wimm);
416 nv50_dmac_destroy(&wndw->wndw);
417 drm_plane_cleanup(&wndw->plane);
418 kfree(wndw);
419}
420
421const struct drm_plane_funcs
422nv50_wndw = {
423 .update_plane = drm_atomic_helper_update_plane,
424 .disable_plane = drm_atomic_helper_disable_plane,
425 .destroy = nv50_wndw_destroy,
426 .reset = nv50_wndw_reset,
427 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
428 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
429};
430
431static int
432nv50_wndw_notify(struct nvif_notify *notify)
433{
434 return NVIF_NOTIFY_KEEP;
435}
436
437void
438nv50_wndw_fini(struct nv50_wndw *wndw)
439{
440 nvif_notify_put(&wndw->notify);
441}
442
443void
444nv50_wndw_init(struct nv50_wndw *wndw)
445{
446 nvif_notify_get(&wndw->notify);
447}
448
449int
450nv50_wndw_new_(const struct nv50_wndw_func *func, struct drm_device *dev,
451 enum drm_plane_type type, const char *name, int index,
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000452 const u32 *format, u32 heads,
453 enum nv50_disp_interlock_type interlock_type, u32 interlock_data,
454 struct nv50_wndw **pwndw)
Ben Skeggs15907002018-05-08 20:39:47 +1000455{
456 struct nv50_wndw *wndw;
457 int nformat;
458 int ret;
459
460 if (!(wndw = *pwndw = kzalloc(sizeof(*wndw), GFP_KERNEL)))
461 return -ENOMEM;
462 wndw->func = func;
463 wndw->id = index;
Ben Skeggs53e0a3e2018-05-08 20:39:47 +1000464 wndw->interlock.type = interlock_type;
465 wndw->interlock.data = interlock_data;
466 wndw->ctxdma.parent = &wndw->wndw.base.user;
Ben Skeggs15907002018-05-08 20:39:47 +1000467
468 wndw->ctxdma.parent = &wndw->wndw.base.user;
469 INIT_LIST_HEAD(&wndw->ctxdma.list);
470
471 for (nformat = 0; format[nformat]; nformat++);
472
Ben Skeggs9d6c2fe2018-05-08 20:39:47 +1000473 ret = drm_universal_plane_init(dev, &wndw->plane, heads, &nv50_wndw,
Ben Skeggs15907002018-05-08 20:39:47 +1000474 format, nformat, NULL,
475 type, "%s-%d", name, index);
476 if (ret) {
477 kfree(*pwndw);
478 *pwndw = NULL;
479 return ret;
480 }
481
482 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
483
484 wndw->notify.func = nv50_wndw_notify;
485 return 0;
486}