2 * Copyright 2018 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #include <nvif/class.h>
26 #include <nvif/cl0002.h>
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_fourcc.h>
31 #include "nouveau_bo.h"
34 nv50_wndw_ctxdma_del(struct nv50_wndw_ctxdma *ctxdma)
36 nvif_object_fini(&ctxdma->object);
37 list_del(&ctxdma->head);
41 static struct nv50_wndw_ctxdma *
42 nv50_wndw_ctxdma_new(struct nv50_wndw *wndw, struct nouveau_framebuffer *fb)
44 struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
45 struct nv50_wndw_ctxdma *ctxdma;
46 const u8 kind = fb->nvbo->kind;
47 const u32 handle = 0xfb000000 | kind;
49 struct nv_dma_v0 base;
51 struct nv50_dma_v0 nv50;
52 struct gf100_dma_v0 gf100;
53 struct gf119_dma_v0 gf119;
56 u32 argc = sizeof(args.base);
59 list_for_each_entry(ctxdma, &wndw->ctxdma.list, head) {
60 if (ctxdma->object.handle == handle)
64 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
65 return ERR_PTR(-ENOMEM);
66 list_add(&ctxdma->head, &wndw->ctxdma.list);
68 args.base.target = NV_DMA_V0_TARGET_VRAM;
69 args.base.access = NV_DMA_V0_ACCESS_RDWR;
71 args.base.limit = drm->client.device.info.ram_user - 1;
73 if (drm->client.device.info.chipset < 0x80) {
74 args.nv50.part = NV50_DMA_V0_PART_256;
75 argc += sizeof(args.nv50);
77 if (drm->client.device.info.chipset < 0xc0) {
78 args.nv50.part = NV50_DMA_V0_PART_256;
79 args.nv50.kind = kind;
80 argc += sizeof(args.nv50);
82 if (drm->client.device.info.chipset < 0xd0) {
83 args.gf100.kind = kind;
84 argc += sizeof(args.gf100);
86 args.gf119.page = GF119_DMA_V0_PAGE_LP;
87 args.gf119.kind = kind;
88 argc += sizeof(args.gf119);
91 ret = nvif_object_init(wndw->ctxdma.parent, handle, NV_DMA_IN_MEMORY,
92 &args, argc, &ctxdma->object);
94 nv50_wndw_ctxdma_del(ctxdma);
102 nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
104 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
105 if (asyw->set.ntfy) {
106 return wndw->func->ntfy_wait_begun(disp->sync,
108 wndw->wndw.base.device);
114 nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 *interlock, bool flush,
115 struct nv50_wndw_atom *asyw)
117 union nv50_wndw_atom_mask clr = {
118 .mask = asyw->clr.mask & ~(flush ? 0 : asyw->set.mask),
120 if (clr.sema ) wndw->func-> sema_clr(wndw);
121 if (clr.ntfy ) wndw->func-> ntfy_clr(wndw);
122 if (clr.xlut ) wndw->func-> xlut_clr(wndw);
123 if (clr.csc ) wndw->func-> csc_clr(wndw);
124 if (clr.image) wndw->func->image_clr(wndw);
126 interlock[wndw->interlock.type] |= wndw->interlock.data;
130 nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 *interlock,
131 struct nv50_wndw_atom *asyw)
133 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
134 asyw->image.mode = 0;
135 asyw->image.interval = 1;
138 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
139 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
140 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
142 if (asyw->set.xlut ) {
144 asyw->xlut.i.offset =
145 nv50_lut_load(&wndw->ilut, asyw->xlut.i.buffer,
146 asyw->ilut, asyw->xlut.i.load);
148 wndw->func->xlut_set(wndw, asyw);
151 if (asyw->set.csc ) wndw->func->csc_set (wndw, asyw);
152 if (asyw->set.scale) wndw->func->scale_set(wndw, asyw);
153 if (asyw->set.point) {
154 if (asyw->set.point = false, asyw->set.mask)
155 interlock[wndw->interlock.type] |= wndw->interlock.data;
156 interlock[NV50_DISP_INTERLOCK_WIMM] |= wndw->interlock.wimm;
158 wndw->immd->point(wndw, asyw);
159 wndw->immd->update(wndw, interlock);
161 interlock[wndw->interlock.type] |= wndw->interlock.data;
166 nv50_wndw_ntfy_enable(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
168 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
170 asyw->ntfy.handle = wndw->wndw.sync.handle;
171 asyw->ntfy.offset = wndw->ntfy;
172 asyw->ntfy.awaken = false;
173 asyw->set.ntfy = true;
175 wndw->func->ntfy_reset(disp->sync, wndw->ntfy);
180 nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
181 struct nv50_wndw_atom *asyw,
182 struct nv50_head_atom *asyh)
184 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
185 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
186 wndw->func->release(wndw, asyw, asyh);
187 asyw->ntfy.handle = 0;
188 asyw->sema.handle = 0;
192 nv50_wndw_atomic_check_acquire_yuv(struct nv50_wndw_atom *asyw)
194 switch (asyw->state.fb->format->format) {
195 case DRM_FORMAT_YUYV: asyw->image.format = 0x28; break;
196 case DRM_FORMAT_UYVY: asyw->image.format = 0x29; break;
201 asyw->image.colorspace = 1;
206 nv50_wndw_atomic_check_acquire_rgb(struct nv50_wndw_atom *asyw)
208 switch (asyw->state.fb->format->format) {
209 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
210 case DRM_FORMAT_XRGB8888 :
211 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
212 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
213 case DRM_FORMAT_XRGB1555 :
214 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
215 case DRM_FORMAT_XBGR2101010 :
216 case DRM_FORMAT_ABGR2101010 : asyw->image.format = 0xd1; break;
217 case DRM_FORMAT_XBGR8888 :
218 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
219 case DRM_FORMAT_XRGB2101010 :
220 case DRM_FORMAT_ARGB2101010 : asyw->image.format = 0xdf; break;
221 case DRM_FORMAT_XBGR16161616F:
222 case DRM_FORMAT_ABGR16161616F: asyw->image.format = 0xca; break;
226 asyw->image.colorspace = 0;
231 nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw, bool modeset,
232 struct nv50_wndw_atom *armw,
233 struct nv50_wndw_atom *asyw,
234 struct nv50_head_atom *asyh)
236 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
237 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
240 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
242 if (asyw->state.fb != armw->state.fb || !armw->visible || modeset) {
243 asyw->image.w = fb->base.width;
244 asyw->image.h = fb->base.height;
245 asyw->image.kind = fb->nvbo->kind;
247 ret = nv50_wndw_atomic_check_acquire_rgb(asyw);
249 ret = nv50_wndw_atomic_check_acquire_yuv(asyw);
254 if (asyw->image.kind) {
255 asyw->image.layout = 0;
256 if (drm->client.device.info.chipset >= 0xc0)
257 asyw->image.blockh = fb->nvbo->mode >> 4;
259 asyw->image.blockh = fb->nvbo->mode;
260 asyw->image.blocks[0] = fb->base.pitches[0] / 64;
261 asyw->image.pitch[0] = 0;
263 asyw->image.layout = 1;
264 asyw->image.blockh = 0;
265 asyw->image.blocks[0] = 0;
266 asyw->image.pitch[0] = fb->base.pitches[0];
269 if (!(asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC))
270 asyw->image.interval = 1;
272 asyw->image.interval = 0;
273 asyw->image.mode = asyw->image.interval ? 0 : 1;
274 asyw->set.image = wndw->func->image_set != NULL;
277 if (wndw->func->scale_set) {
278 asyw->scale.sx = asyw->state.src_x >> 16;
279 asyw->scale.sy = asyw->state.src_y >> 16;
280 asyw->scale.sw = asyw->state.src_w >> 16;
281 asyw->scale.sh = asyw->state.src_h >> 16;
282 asyw->scale.dw = asyw->state.crtc_w;
283 asyw->scale.dh = asyw->state.crtc_h;
284 if (memcmp(&armw->scale, &asyw->scale, sizeof(asyw->scale)))
285 asyw->set.scale = true;
289 asyw->point.x = asyw->state.crtc_x;
290 asyw->point.y = asyw->state.crtc_y;
291 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
292 asyw->set.point = true;
295 return wndw->func->acquire(wndw, asyw, asyh);
299 nv50_wndw_atomic_check_lut(struct nv50_wndw *wndw,
300 struct nv50_wndw_atom *armw,
301 struct nv50_wndw_atom *asyw,
302 struct nv50_head_atom *asyh)
304 struct drm_property_blob *ilut = asyh->state.degamma_lut;
306 /* I8 format without an input LUT makes no sense, and the
307 * HW error-checks for this.
309 * In order to handle legacy gamma, when there's no input
310 * LUT we need to steal the output LUT and use it instead.
312 if (!ilut && asyw->state.fb->format->format == DRM_FORMAT_C8) {
313 /* This should be an error, but there's legacy clients
314 * that do a modeset before providing a gamma table.
316 * We keep the window disabled to avoid angering HW.
318 if (!(ilut = asyh->state.gamma_lut)) {
319 asyw->visible = false;
323 if (wndw->func->ilut)
324 asyh->wndw.olut |= BIT(wndw->id);
326 asyh->wndw.olut &= ~BIT(wndw->id);
329 if (!ilut && wndw->func->ilut_identity &&
330 asyw->state.fb->format->format != DRM_FORMAT_XBGR16161616F &&
331 asyw->state.fb->format->format != DRM_FORMAT_ABGR16161616F) {
332 static struct drm_property_blob dummy = {};
336 /* Recalculate LUT state. */
337 memset(&asyw->xlut, 0x00, sizeof(asyw->xlut));
338 if ((asyw->ilut = wndw->func->ilut ? ilut : NULL)) {
339 wndw->func->ilut(wndw, asyw);
340 asyw->xlut.handle = wndw->wndw.vram.handle;
341 asyw->xlut.i.buffer = !asyw->xlut.i.buffer;
342 asyw->set.xlut = true;
344 asyw->clr.xlut = armw->xlut.handle != 0;
347 /* Handle setting base SET_OUTPUT_LUT_LO_ENABLE_USE_CORE_LUT. */
348 if (wndw->func->olut_core &&
349 (!armw->visible || (armw->xlut.handle && !asyw->xlut.handle)))
350 asyw->set.xlut = true;
352 if (wndw->func->csc && asyh->state.ctm) {
353 const struct drm_color_ctm *ctm = asyh->state.ctm->data;
354 wndw->func->csc(wndw, asyw, ctm);
355 asyw->csc.valid = true;
356 asyw->set.csc = true;
358 asyw->csc.valid = false;
359 asyw->clr.csc = armw->csc.valid;
362 /* Can't do an immediate flip while changing the LUT. */
363 asyh->state.pageflip_flags &= ~DRM_MODE_PAGE_FLIP_ASYNC;
367 nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
369 struct nouveau_drm *drm = nouveau_drm(plane->dev);
370 struct nv50_wndw *wndw = nv50_wndw(plane);
371 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
372 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
373 struct nv50_head_atom *harm = NULL, *asyh = NULL;
374 bool modeset = false;
377 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
379 /* Fetch the assembly state for the head the window will belong to,
380 * and determine whether the window will be visible.
382 if (asyw->state.crtc) {
383 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
385 return PTR_ERR(asyh);
386 modeset = drm_atomic_crtc_needs_modeset(&asyh->state);
387 asyw->visible = asyh->state.active;
389 asyw->visible = false;
392 /* Fetch assembly state for the head the window used to belong to. */
393 if (armw->state.crtc) {
394 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
396 return PTR_ERR(harm);
399 /* LUT configuration can potentially cause the window to be disabled. */
400 if (asyw->visible && wndw->func->xlut_set &&
402 asyh->state.color_mgmt_changed ||
403 asyw->state.fb->format->format !=
404 armw->state.fb->format->format))
405 nv50_wndw_atomic_check_lut(wndw, armw, asyw, asyh);
407 /* Calculate new window state. */
409 ret = nv50_wndw_atomic_check_acquire(wndw, modeset,
414 asyh->wndw.mask |= BIT(wndw->id);
417 nv50_wndw_atomic_check_release(wndw, asyw, harm);
418 harm->wndw.mask &= ~BIT(wndw->id);
423 /* Aside from the obvious case where the window is actively being
424 * disabled, we might also need to temporarily disable the window
425 * when performing certain modeset operations.
427 if (!asyw->visible || modeset) {
428 asyw->clr.ntfy = armw->ntfy.handle != 0;
429 asyw->clr.sema = armw->sema.handle != 0;
430 asyw->clr.xlut = armw->xlut.handle != 0;
431 asyw->clr.csc = armw->csc.valid;
432 if (wndw->func->image_clr)
433 asyw->clr.image = armw->image.handle[0] != 0;
440 nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
442 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
443 struct nouveau_drm *drm = nouveau_drm(plane->dev);
445 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
449 nouveau_bo_unpin(fb->nvbo);
453 nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
455 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
456 struct nouveau_drm *drm = nouveau_drm(plane->dev);
457 struct nv50_wndw *wndw = nv50_wndw(plane);
458 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
459 struct nv50_head_atom *asyh;
460 struct nv50_wndw_ctxdma *ctxdma;
463 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
467 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
471 if (wndw->ctxdma.parent) {
472 ctxdma = nv50_wndw_ctxdma_new(wndw, fb);
473 if (IS_ERR(ctxdma)) {
474 nouveau_bo_unpin(fb->nvbo);
475 return PTR_ERR(ctxdma);
478 asyw->image.handle[0] = ctxdma->object.handle;
481 asyw->state.fence = dma_resv_get_excl_rcu(fb->nvbo->bo.base.resv);
482 asyw->image.offset[0] = fb->nvbo->bo.offset;
484 if (wndw->func->prepare) {
485 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
487 return PTR_ERR(asyh);
489 wndw->func->prepare(wndw, asyh, asyw);
495 static const struct drm_plane_helper_funcs
497 .prepare_fb = nv50_wndw_prepare_fb,
498 .cleanup_fb = nv50_wndw_cleanup_fb,
499 .atomic_check = nv50_wndw_atomic_check,
503 nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
504 struct drm_plane_state *state)
506 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
507 __drm_atomic_helper_plane_destroy_state(&asyw->state);
511 static struct drm_plane_state *
512 nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
514 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
515 struct nv50_wndw_atom *asyw;
516 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
518 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
519 asyw->sema = armw->sema;
520 asyw->ntfy = armw->ntfy;
522 asyw->xlut = armw->xlut;
523 asyw->csc = armw->csc;
524 asyw->image = armw->image;
525 asyw->point = armw->point;
532 nv50_wndw_reset(struct drm_plane *plane)
534 struct nv50_wndw_atom *asyw;
536 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
540 plane->funcs->atomic_destroy_state(plane, plane->state);
541 plane->state = &asyw->state;
542 plane->state->plane = plane;
543 plane->state->rotation = DRM_MODE_ROTATE_0;
547 nv50_wndw_destroy(struct drm_plane *plane)
549 struct nv50_wndw *wndw = nv50_wndw(plane);
550 struct nv50_wndw_ctxdma *ctxdma, *ctxtmp;
552 list_for_each_entry_safe(ctxdma, ctxtmp, &wndw->ctxdma.list, head) {
553 nv50_wndw_ctxdma_del(ctxdma);
556 nvif_notify_fini(&wndw->notify);
557 nv50_dmac_destroy(&wndw->wimm);
558 nv50_dmac_destroy(&wndw->wndw);
560 nv50_lut_fini(&wndw->ilut);
562 drm_plane_cleanup(&wndw->plane);
566 const struct drm_plane_funcs
568 .update_plane = drm_atomic_helper_update_plane,
569 .disable_plane = drm_atomic_helper_disable_plane,
570 .destroy = nv50_wndw_destroy,
571 .reset = nv50_wndw_reset,
572 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
573 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
577 nv50_wndw_notify(struct nvif_notify *notify)
579 return NVIF_NOTIFY_KEEP;
583 nv50_wndw_fini(struct nv50_wndw *wndw)
585 nvif_notify_put(&wndw->notify);
589 nv50_wndw_init(struct nv50_wndw *wndw)
591 nvif_notify_get(&wndw->notify);
595 nv50_wndw_new_(const struct nv50_wndw_func *func, struct drm_device *dev,
596 enum drm_plane_type type, const char *name, int index,
597 const u32 *format, u32 heads,
598 enum nv50_disp_interlock_type interlock_type, u32 interlock_data,
599 struct nv50_wndw **pwndw)
601 struct nouveau_drm *drm = nouveau_drm(dev);
602 struct nvif_mmu *mmu = &drm->client.mmu;
603 struct nv50_disp *disp = nv50_disp(dev);
604 struct nv50_wndw *wndw;
608 if (!(wndw = *pwndw = kzalloc(sizeof(*wndw), GFP_KERNEL)))
612 wndw->interlock.type = interlock_type;
613 wndw->interlock.data = interlock_data;
615 wndw->ctxdma.parent = &wndw->wndw.base.user;
616 INIT_LIST_HEAD(&wndw->ctxdma.list);
618 for (nformat = 0; format[nformat]; nformat++);
620 ret = drm_universal_plane_init(dev, &wndw->plane, heads, &nv50_wndw,
621 format, nformat, NULL,
622 type, "%s-%d", name, index);
629 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
631 if (wndw->func->ilut) {
632 ret = nv50_lut_init(disp, mmu, &wndw->ilut);
637 wndw->notify.func = nv50_wndw_notify;
642 nv50_wndw_new(struct nouveau_drm *drm, enum drm_plane_type type, int index,
643 struct nv50_wndw **pwndw)
648 int (*new)(struct nouveau_drm *, enum drm_plane_type,
649 int, s32, struct nv50_wndw **);
651 { TU102_DISP_WINDOW_CHANNEL_DMA, 0, wndwc57e_new },
652 { GV100_DISP_WINDOW_CHANNEL_DMA, 0, wndwc37e_new },
655 struct nv50_disp *disp = nv50_disp(drm->dev);
658 cid = nvif_mclass(&disp->disp->object, wndws);
660 NV_ERROR(drm, "No supported window class\n");
664 ret = wndws[cid].new(drm, type, index, wndws[cid].oclass, pwndw);
668 return nv50_wimm_init(drm, *pwndw);