]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/nouveau/dispnv50/wndw.c
c8d0786297222e1eec86962dcfb34bec8f0f561f
[linux.git] / drivers / gpu / drm / nouveau / dispnv50 / wndw.c
1 /*
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 #include "wimm.h"
24
25 #include <nvif/class.h>
26 #include <nvif/cl0002.h>
27
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_fourcc.h>
30
31 #include "nouveau_bo.h"
32
33 static void
34 nv50_wndw_ctxdma_del(struct nv50_wndw_ctxdma *ctxdma)
35 {
36         nvif_object_fini(&ctxdma->object);
37         list_del(&ctxdma->head);
38         kfree(ctxdma);
39 }
40
41 static struct nv50_wndw_ctxdma *
42 nv50_wndw_ctxdma_new(struct nv50_wndw *wndw, struct nouveau_framebuffer *fb)
43 {
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;
48         struct {
49                 struct nv_dma_v0 base;
50                 union {
51                         struct nv50_dma_v0 nv50;
52                         struct gf100_dma_v0 gf100;
53                         struct gf119_dma_v0 gf119;
54                 };
55         } args = {};
56         u32 argc = sizeof(args.base);
57         int ret;
58
59         list_for_each_entry(ctxdma, &wndw->ctxdma.list, head) {
60                 if (ctxdma->object.handle == handle)
61                         return ctxdma;
62         }
63
64         if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
65                 return ERR_PTR(-ENOMEM);
66         list_add(&ctxdma->head, &wndw->ctxdma.list);
67
68         args.base.target = NV_DMA_V0_TARGET_VRAM;
69         args.base.access = NV_DMA_V0_ACCESS_RDWR;
70         args.base.start  = 0;
71         args.base.limit  = drm->client.device.info.ram_user - 1;
72
73         if (drm->client.device.info.chipset < 0x80) {
74                 args.nv50.part = NV50_DMA_V0_PART_256;
75                 argc += sizeof(args.nv50);
76         } else
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);
81         } else
82         if (drm->client.device.info.chipset < 0xd0) {
83                 args.gf100.kind = kind;
84                 argc += sizeof(args.gf100);
85         } else {
86                 args.gf119.page = GF119_DMA_V0_PAGE_LP;
87                 args.gf119.kind = kind;
88                 argc += sizeof(args.gf119);
89         }
90
91         ret = nvif_object_init(wndw->ctxdma.parent, handle, NV_DMA_IN_MEMORY,
92                                &args, argc, &ctxdma->object);
93         if (ret) {
94                 nv50_wndw_ctxdma_del(ctxdma);
95                 return ERR_PTR(ret);
96         }
97
98         return ctxdma;
99 }
100
101 int
102 nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
103 {
104         struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
105         if (asyw->set.ntfy) {
106                 return wndw->func->ntfy_wait_begun(disp->sync,
107                                                    asyw->ntfy.offset,
108                                                    wndw->wndw.base.device);
109         }
110         return 0;
111 }
112
113 void
114 nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 *interlock, bool flush,
115                     struct nv50_wndw_atom *asyw)
116 {
117         union nv50_wndw_atom_mask clr = {
118                 .mask = asyw->clr.mask & ~(flush ? 0 : asyw->set.mask),
119         };
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);
125
126         interlock[wndw->interlock.type] |= wndw->interlock.data;
127 }
128
129 void
130 nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 *interlock,
131                     struct nv50_wndw_atom *asyw)
132 {
133         if (interlock[NV50_DISP_INTERLOCK_CORE]) {
134                 asyw->image.mode = 0;
135                 asyw->image.interval = 1;
136         }
137
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);
141
142         if (asyw->set.xlut ) {
143                 if (asyw->ilut) {
144                         asyw->xlut.i.offset =
145                                 nv50_lut_load(&wndw->ilut, asyw->xlut.i.buffer,
146                                               asyw->ilut, asyw->xlut.i.load);
147                 }
148                 wndw->func->xlut_set(wndw, asyw);
149         }
150
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;
157
158                 wndw->immd->point(wndw, asyw);
159                 wndw->immd->update(wndw, interlock);
160         } else {
161                 interlock[wndw->interlock.type] |= wndw->interlock.data;
162         }
163 }
164
165 void
166 nv50_wndw_ntfy_enable(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
167 {
168         struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
169
170         asyw->ntfy.handle = wndw->wndw.sync.handle;
171         asyw->ntfy.offset = wndw->ntfy;
172         asyw->ntfy.awaken = false;
173         asyw->set.ntfy = true;
174
175         wndw->func->ntfy_reset(disp->sync, wndw->ntfy);
176         wndw->ntfy ^= 0x10;
177 }
178
179 static void
180 nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
181                                struct nv50_wndw_atom *asyw,
182                                struct nv50_head_atom *asyh)
183 {
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;
189 }
190
191 static int
192 nv50_wndw_atomic_check_acquire_yuv(struct nv50_wndw_atom *asyw)
193 {
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;
197         default:
198                 WARN_ON(1);
199                 return -EINVAL;
200         }
201         asyw->image.colorspace = 1;
202         return 0;
203 }
204
205 static int
206 nv50_wndw_atomic_check_acquire_rgb(struct nv50_wndw_atom *asyw)
207 {
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;
223         default:
224                 return -EINVAL;
225         }
226         asyw->image.colorspace = 0;
227         return 0;
228 }
229
230 static int
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)
235 {
236         struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
237         struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
238         int ret;
239
240         NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
241
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;
246
247                 ret = nv50_wndw_atomic_check_acquire_rgb(asyw);
248                 if (ret) {
249                         ret = nv50_wndw_atomic_check_acquire_yuv(asyw);
250                         if (ret)
251                                 return ret;
252                 }
253
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;
258                         else
259                                 asyw->image.blockh = fb->nvbo->mode;
260                         asyw->image.blocks[0] = fb->base.pitches[0] / 64;
261                         asyw->image.pitch[0] = 0;
262                 } else {
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];
267                 }
268
269                 if (!(asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC))
270                         asyw->image.interval = 1;
271                 else
272                         asyw->image.interval = 0;
273                 asyw->image.mode = asyw->image.interval ? 0 : 1;
274                 asyw->set.image = wndw->func->image_set != NULL;
275         }
276
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;
286         }
287
288         if (wndw->immd) {
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;
293         }
294
295         return wndw->func->acquire(wndw, asyw, asyh);
296 }
297
298 static void
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)
303 {
304         struct drm_property_blob *ilut = asyh->state.degamma_lut;
305
306         /* I8 format without an input LUT makes no sense, and the
307          * HW error-checks for this.
308          *
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.
311          */
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.
315                  *
316                  * We keep the window disabled to avoid angering HW.
317                  */
318                 if (!(ilut = asyh->state.gamma_lut)) {
319                         asyw->visible = false;
320                         return;
321                 }
322
323                 if (wndw->func->ilut)
324                         asyh->wndw.olut |= BIT(wndw->id);
325         } else {
326                 asyh->wndw.olut &= ~BIT(wndw->id);
327         }
328
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 = {};
333                 ilut = &dummy;
334         }
335
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;
343         } else {
344                 asyw->clr.xlut = armw->xlut.handle != 0;
345         }
346
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;
351
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;
357         } else {
358                 asyw->csc.valid = false;
359                 asyw->clr.csc = armw->csc.valid;
360         }
361
362         /* Can't do an immediate flip while changing the LUT. */
363         asyh->state.pageflip_flags &= ~DRM_MODE_PAGE_FLIP_ASYNC;
364 }
365
366 static int
367 nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
368 {
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;
375         int ret;
376
377         NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
378
379         /* Fetch the assembly state for the head the window will belong to,
380          * and determine whether the window will be visible.
381          */
382         if (asyw->state.crtc) {
383                 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
384                 if (IS_ERR(asyh))
385                         return PTR_ERR(asyh);
386                 modeset = drm_atomic_crtc_needs_modeset(&asyh->state);
387                 asyw->visible = asyh->state.active;
388         } else {
389                 asyw->visible = false;
390         }
391
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);
395                 if (IS_ERR(harm))
396                         return PTR_ERR(harm);
397         }
398
399         /* LUT configuration can potentially cause the window to be disabled. */
400         if (asyw->visible && wndw->func->xlut_set &&
401             (!armw->visible ||
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);
406
407         /* Calculate new window state. */
408         if (asyw->visible) {
409                 ret = nv50_wndw_atomic_check_acquire(wndw, modeset,
410                                                      armw, asyw, asyh);
411                 if (ret)
412                         return ret;
413
414                 asyh->wndw.mask |= BIT(wndw->id);
415         } else
416         if (armw->visible) {
417                 nv50_wndw_atomic_check_release(wndw, asyw, harm);
418                 harm->wndw.mask &= ~BIT(wndw->id);
419         } else {
420                 return 0;
421         }
422
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.
426          */
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;
434         }
435
436         return 0;
437 }
438
439 static void
440 nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
441 {
442         struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
443         struct nouveau_drm *drm = nouveau_drm(plane->dev);
444
445         NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
446         if (!old_state->fb)
447                 return;
448
449         nouveau_bo_unpin(fb->nvbo);
450 }
451
452 static int
453 nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
454 {
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;
461         int ret;
462
463         NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
464         if (!asyw->state.fb)
465                 return 0;
466
467         ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
468         if (ret)
469                 return ret;
470
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);
476                 }
477
478                 asyw->image.handle[0] = ctxdma->object.handle;
479         }
480
481         asyw->state.fence = dma_resv_get_excl_rcu(fb->nvbo->bo.base.resv);
482         asyw->image.offset[0] = fb->nvbo->bo.offset;
483
484         if (wndw->func->prepare) {
485                 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
486                 if (IS_ERR(asyh))
487                         return PTR_ERR(asyh);
488
489                 wndw->func->prepare(wndw, asyh, asyw);
490         }
491
492         return 0;
493 }
494
495 static const struct drm_plane_helper_funcs
496 nv50_wndw_helper = {
497         .prepare_fb = nv50_wndw_prepare_fb,
498         .cleanup_fb = nv50_wndw_cleanup_fb,
499         .atomic_check = nv50_wndw_atomic_check,
500 };
501
502 static void
503 nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
504                                struct drm_plane_state *state)
505 {
506         struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
507         __drm_atomic_helper_plane_destroy_state(&asyw->state);
508         kfree(asyw);
509 }
510
511 static struct drm_plane_state *
512 nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
513 {
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)))
517                 return NULL;
518         __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
519         asyw->sema = armw->sema;
520         asyw->ntfy = armw->ntfy;
521         asyw->ilut = NULL;
522         asyw->xlut = armw->xlut;
523         asyw->csc  = armw->csc;
524         asyw->image = armw->image;
525         asyw->point = armw->point;
526         asyw->clr.mask = 0;
527         asyw->set.mask = 0;
528         return &asyw->state;
529 }
530
531 static void
532 nv50_wndw_reset(struct drm_plane *plane)
533 {
534         struct nv50_wndw_atom *asyw;
535
536         if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
537                 return;
538
539         if (plane->state)
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;
544 }
545
546 static void
547 nv50_wndw_destroy(struct drm_plane *plane)
548 {
549         struct nv50_wndw *wndw = nv50_wndw(plane);
550         struct nv50_wndw_ctxdma *ctxdma, *ctxtmp;
551
552         list_for_each_entry_safe(ctxdma, ctxtmp, &wndw->ctxdma.list, head) {
553                 nv50_wndw_ctxdma_del(ctxdma);
554         }
555
556         nvif_notify_fini(&wndw->notify);
557         nv50_dmac_destroy(&wndw->wimm);
558         nv50_dmac_destroy(&wndw->wndw);
559
560         nv50_lut_fini(&wndw->ilut);
561
562         drm_plane_cleanup(&wndw->plane);
563         kfree(wndw);
564 }
565
566 const struct drm_plane_funcs
567 nv50_wndw = {
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,
574 };
575
576 static int
577 nv50_wndw_notify(struct nvif_notify *notify)
578 {
579         return NVIF_NOTIFY_KEEP;
580 }
581
582 void
583 nv50_wndw_fini(struct nv50_wndw *wndw)
584 {
585         nvif_notify_put(&wndw->notify);
586 }
587
588 void
589 nv50_wndw_init(struct nv50_wndw *wndw)
590 {
591         nvif_notify_get(&wndw->notify);
592 }
593
594 int
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)
600 {
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;
605         int nformat;
606         int ret;
607
608         if (!(wndw = *pwndw = kzalloc(sizeof(*wndw), GFP_KERNEL)))
609                 return -ENOMEM;
610         wndw->func = func;
611         wndw->id = index;
612         wndw->interlock.type = interlock_type;
613         wndw->interlock.data = interlock_data;
614
615         wndw->ctxdma.parent = &wndw->wndw.base.user;
616         INIT_LIST_HEAD(&wndw->ctxdma.list);
617
618         for (nformat = 0; format[nformat]; nformat++);
619
620         ret = drm_universal_plane_init(dev, &wndw->plane, heads, &nv50_wndw,
621                                        format, nformat, NULL,
622                                        type, "%s-%d", name, index);
623         if (ret) {
624                 kfree(*pwndw);
625                 *pwndw = NULL;
626                 return ret;
627         }
628
629         drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
630
631         if (wndw->func->ilut) {
632                 ret = nv50_lut_init(disp, mmu, &wndw->ilut);
633                 if (ret)
634                         return ret;
635         }
636
637         wndw->notify.func = nv50_wndw_notify;
638         return 0;
639 }
640
641 int
642 nv50_wndw_new(struct nouveau_drm *drm, enum drm_plane_type type, int index,
643               struct nv50_wndw **pwndw)
644 {
645         struct {
646                 s32 oclass;
647                 int version;
648                 int (*new)(struct nouveau_drm *, enum drm_plane_type,
649                            int, s32, struct nv50_wndw **);
650         } wndws[] = {
651                 { TU102_DISP_WINDOW_CHANNEL_DMA, 0, wndwc57e_new },
652                 { GV100_DISP_WINDOW_CHANNEL_DMA, 0, wndwc37e_new },
653                 {}
654         };
655         struct nv50_disp *disp = nv50_disp(drm->dev);
656         int cid, ret;
657
658         cid = nvif_mclass(&disp->disp->object, wndws);
659         if (cid < 0) {
660                 NV_ERROR(drm, "No supported window class\n");
661                 return cid;
662         }
663
664         ret = wndws[cid].new(drm, type, index, wndws[cid].oclass, pwndw);
665         if (ret)
666                 return ret;
667
668         return nv50_wimm_init(drm, *pwndw);
669 }