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 #include "handles.h"
25 
26 #include <nvif/class.h>
27 #include <nvif/cl0002.h>
28 
29 #include <nvhw/class/cl507c.h>
30 #include <nvhw/class/cl507e.h>
31 #include <nvhw/class/clc37e.h>
32 
33 #include <drm/drm_atomic.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_fourcc.h>
36 
37 #include "nouveau_bo.h"
38 #include "nouveau_gem.h"
39 
40 static void
41 nv50_wndw_ctxdma_del(struct nv50_wndw_ctxdma *ctxdma)
42 {
43 	nvif_object_dtor(&ctxdma->object);
44 	list_del(&ctxdma->head);
45 	kfree(ctxdma);
46 }
47 
48 static struct nv50_wndw_ctxdma *
49 nv50_wndw_ctxdma_new(struct nv50_wndw *wndw, struct drm_framebuffer *fb)
50 {
51 	struct nouveau_drm *drm = nouveau_drm(fb->dev);
52 	struct nv50_wndw_ctxdma *ctxdma;
53 	u32 handle;
54 	u32 unused;
55 	u8  kind;
56 	struct {
57 		struct nv_dma_v0 base;
58 		union {
59 			struct nv50_dma_v0 nv50;
60 			struct gf100_dma_v0 gf100;
61 			struct gf119_dma_v0 gf119;
62 		};
63 	} args = {};
64 	u32 argc = sizeof(args.base);
65 	int ret;
66 
67 	nouveau_framebuffer_get_layout(fb, &unused, &kind);
68 	handle = NV50_DISP_HANDLE_WNDW_CTX(kind);
69 
70 	list_for_each_entry(ctxdma, &wndw->ctxdma.list, head) {
71 		if (ctxdma->object.handle == handle)
72 			return ctxdma;
73 	}
74 
75 	if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
76 		return ERR_PTR(-ENOMEM);
77 	list_add(&ctxdma->head, &wndw->ctxdma.list);
78 
79 	args.base.target = NV_DMA_V0_TARGET_VRAM;
80 	args.base.access = NV_DMA_V0_ACCESS_RDWR;
81 	args.base.start  = 0;
82 	args.base.limit  = drm->client.device.info.ram_user - 1;
83 
84 	if (drm->client.device.info.chipset < 0x80) {
85 		args.nv50.part = NV50_DMA_V0_PART_256;
86 		argc += sizeof(args.nv50);
87 	} else
88 	if (drm->client.device.info.chipset < 0xc0) {
89 		args.nv50.part = NV50_DMA_V0_PART_256;
90 		args.nv50.kind = kind;
91 		argc += sizeof(args.nv50);
92 	} else
93 	if (drm->client.device.info.chipset < 0xd0) {
94 		args.gf100.kind = kind;
95 		argc += sizeof(args.gf100);
96 	} else {
97 		args.gf119.page = GF119_DMA_V0_PAGE_LP;
98 		args.gf119.kind = kind;
99 		argc += sizeof(args.gf119);
100 	}
101 
102 	ret = nvif_object_ctor(wndw->ctxdma.parent, "kmsFbCtxDma", handle,
103 			       NV_DMA_IN_MEMORY, &args, argc, &ctxdma->object);
104 	if (ret) {
105 		nv50_wndw_ctxdma_del(ctxdma);
106 		return ERR_PTR(ret);
107 	}
108 
109 	return ctxdma;
110 }
111 
112 int
113 nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
114 {
115 	struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
116 	if (asyw->set.ntfy) {
117 		return wndw->func->ntfy_wait_begun(disp->sync,
118 						   asyw->ntfy.offset,
119 						   wndw->wndw.base.device);
120 	}
121 	return 0;
122 }
123 
124 void
125 nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 *interlock, bool flush,
126 		    struct nv50_wndw_atom *asyw)
127 {
128 	union nv50_wndw_atom_mask clr = {
129 		.mask = asyw->clr.mask & ~(flush ? 0 : asyw->set.mask),
130 	};
131 	if (clr.sema ) wndw->func-> sema_clr(wndw);
132 	if (clr.ntfy ) wndw->func-> ntfy_clr(wndw);
133 	if (clr.xlut ) wndw->func-> xlut_clr(wndw);
134 	if (clr.csc  ) wndw->func->  csc_clr(wndw);
135 	if (clr.image) wndw->func->image_clr(wndw);
136 
137 	interlock[wndw->interlock.type] |= wndw->interlock.data;
138 }
139 
140 void
141 nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 *interlock,
142 		    struct nv50_wndw_atom *asyw)
143 {
144 	if (interlock[NV50_DISP_INTERLOCK_CORE]) {
145 		asyw->image.mode = NV507C_SET_PRESENT_CONTROL_BEGIN_MODE_NON_TEARING;
146 		asyw->image.interval = 1;
147 	}
148 
149 	if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
150 	if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
151 	if (asyw->set.image) wndw->func->image_set(wndw, asyw);
152 
153 	if (asyw->set.xlut ) {
154 		if (asyw->ilut) {
155 			asyw->xlut.i.offset =
156 				nv50_lut_load(&wndw->ilut, asyw->xlut.i.buffer,
157 					      asyw->ilut, asyw->xlut.i.load);
158 		}
159 		wndw->func->xlut_set(wndw, asyw);
160 	}
161 
162 	if (asyw->set.csc  ) wndw->func->csc_set  (wndw, asyw);
163 	if (asyw->set.scale) wndw->func->scale_set(wndw, asyw);
164 	if (asyw->set.blend) wndw->func->blend_set(wndw, asyw);
165 	if (asyw->set.point) {
166 		if (asyw->set.point = false, asyw->set.mask)
167 			interlock[wndw->interlock.type] |= wndw->interlock.data;
168 		interlock[NV50_DISP_INTERLOCK_WIMM] |= wndw->interlock.wimm;
169 
170 		wndw->immd->point(wndw, asyw);
171 		wndw->immd->update(wndw, interlock);
172 	} else {
173 		interlock[wndw->interlock.type] |= wndw->interlock.data;
174 	}
175 }
176 
177 void
178 nv50_wndw_ntfy_enable(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
179 {
180 	struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
181 
182 	asyw->ntfy.handle = wndw->wndw.sync.handle;
183 	asyw->ntfy.offset = wndw->ntfy;
184 	asyw->ntfy.awaken = false;
185 	asyw->set.ntfy = true;
186 
187 	wndw->func->ntfy_reset(disp->sync, wndw->ntfy);
188 	wndw->ntfy ^= 0x10;
189 }
190 
191 static void
192 nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
193 			       struct nv50_wndw_atom *asyw,
194 			       struct nv50_head_atom *asyh)
195 {
196 	struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
197 	NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
198 	wndw->func->release(wndw, asyw, asyh);
199 	asyw->ntfy.handle = 0;
200 	asyw->sema.handle = 0;
201 	asyw->xlut.handle = 0;
202 	memset(asyw->image.handle, 0x00, sizeof(asyw->image.handle));
203 }
204 
205 static int
206 nv50_wndw_atomic_check_acquire_yuv(struct nv50_wndw_atom *asyw)
207 {
208 	switch (asyw->state.fb->format->format) {
209 	case DRM_FORMAT_YUYV:
210 		asyw->image.format = NV507E_SURFACE_SET_PARAMS_FORMAT_VE8YO8UE8YE8;
211 		break;
212 	case DRM_FORMAT_UYVY:
213 		asyw->image.format = NV507E_SURFACE_SET_PARAMS_FORMAT_YO8VE8YE8UE8;
214 		break;
215 	default:
216 		WARN_ON(1);
217 		return -EINVAL;
218 	}
219 
220 	asyw->image.colorspace = NV507E_SURFACE_SET_PARAMS_COLOR_SPACE_YUV_601;
221 	return 0;
222 }
223 
224 static int
225 nv50_wndw_atomic_check_acquire_rgb(struct nv50_wndw_atom *asyw)
226 {
227 	switch (asyw->state.fb->format->format) {
228 	case DRM_FORMAT_C8:
229 		asyw->image.format = NV507C_SURFACE_SET_PARAMS_FORMAT_I8;
230 		break;
231 	case DRM_FORMAT_XRGB8888:
232 	case DRM_FORMAT_ARGB8888:
233 		asyw->image.format = NV507C_SURFACE_SET_PARAMS_FORMAT_A8R8G8B8;
234 		break;
235 	case DRM_FORMAT_RGB565:
236 		asyw->image.format = NV507C_SURFACE_SET_PARAMS_FORMAT_R5G6B5;
237 		break;
238 	case DRM_FORMAT_XRGB1555:
239 	case DRM_FORMAT_ARGB1555:
240 		asyw->image.format = NV507C_SURFACE_SET_PARAMS_FORMAT_A1R5G5B5;
241 		break;
242 	case DRM_FORMAT_XBGR2101010:
243 	case DRM_FORMAT_ABGR2101010:
244 		asyw->image.format = NV507C_SURFACE_SET_PARAMS_FORMAT_A2B10G10R10;
245 		break;
246 	case DRM_FORMAT_XBGR8888:
247 	case DRM_FORMAT_ABGR8888:
248 		asyw->image.format = NV507C_SURFACE_SET_PARAMS_FORMAT_A8B8G8R8;
249 		break;
250 	case DRM_FORMAT_XRGB2101010:
251 	case DRM_FORMAT_ARGB2101010:
252 		asyw->image.format = NVC37E_SET_PARAMS_FORMAT_A2R10G10B10;
253 		break;
254 	case DRM_FORMAT_XBGR16161616F:
255 	case DRM_FORMAT_ABGR16161616F:
256 		asyw->image.format = NV507C_SURFACE_SET_PARAMS_FORMAT_RF16_GF16_BF16_AF16;
257 		break;
258 	default:
259 		return -EINVAL;
260 	}
261 
262 	asyw->image.colorspace = NV507E_SURFACE_SET_PARAMS_COLOR_SPACE_RGB;
263 	return 0;
264 }
265 
266 static int
267 nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw, bool modeset,
268 			       struct nv50_wndw_atom *armw,
269 			       struct nv50_wndw_atom *asyw,
270 			       struct nv50_head_atom *asyh)
271 {
272 	struct drm_framebuffer *fb = asyw->state.fb;
273 	struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
274 	uint8_t kind;
275 	uint32_t tile_mode;
276 	int ret;
277 
278 	NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
279 
280 	if (fb != armw->state.fb || !armw->visible || modeset) {
281 		nouveau_framebuffer_get_layout(fb, &tile_mode, &kind);
282 
283 		asyw->image.w = fb->width;
284 		asyw->image.h = fb->height;
285 		asyw->image.kind = kind;
286 
287 		ret = nv50_wndw_atomic_check_acquire_rgb(asyw);
288 		if (ret) {
289 			ret = nv50_wndw_atomic_check_acquire_yuv(asyw);
290 			if (ret)
291 				return ret;
292 		}
293 
294 		if (asyw->image.kind) {
295 			asyw->image.layout = NV507C_SURFACE_SET_STORAGE_MEMORY_LAYOUT_BLOCKLINEAR;
296 			if (drm->client.device.info.chipset >= 0xc0)
297 				asyw->image.blockh = tile_mode >> 4;
298 			else
299 				asyw->image.blockh = tile_mode;
300 			asyw->image.blocks[0] = fb->pitches[0] / 64;
301 			asyw->image.pitch[0] = 0;
302 		} else {
303 			asyw->image.layout = NV507C_SURFACE_SET_STORAGE_MEMORY_LAYOUT_PITCH;
304 			asyw->image.blockh = NV507C_SURFACE_SET_STORAGE_BLOCK_HEIGHT_ONE_GOB;
305 			asyw->image.blocks[0] = 0;
306 			asyw->image.pitch[0] = fb->pitches[0];
307 		}
308 
309 		if (!asyh->state.async_flip)
310 			asyw->image.interval = 1;
311 		else
312 			asyw->image.interval = 0;
313 
314 		if (asyw->image.interval)
315 			asyw->image.mode = NV507C_SET_PRESENT_CONTROL_BEGIN_MODE_NON_TEARING;
316 		else
317 			asyw->image.mode = NV507C_SET_PRESENT_CONTROL_BEGIN_MODE_IMMEDIATE;
318 
319 		asyw->set.image = wndw->func->image_set != NULL;
320 	}
321 
322 	if (wndw->func->scale_set) {
323 		asyw->scale.sx = asyw->state.src_x >> 16;
324 		asyw->scale.sy = asyw->state.src_y >> 16;
325 		asyw->scale.sw = asyw->state.src_w >> 16;
326 		asyw->scale.sh = asyw->state.src_h >> 16;
327 		asyw->scale.dw = asyw->state.crtc_w;
328 		asyw->scale.dh = asyw->state.crtc_h;
329 		if (memcmp(&armw->scale, &asyw->scale, sizeof(asyw->scale)))
330 			asyw->set.scale = true;
331 	}
332 
333 	if (wndw->func->blend_set) {
334 		asyw->blend.depth = 255 - asyw->state.normalized_zpos;
335 		asyw->blend.k1 = asyw->state.alpha >> 8;
336 		switch (asyw->state.pixel_blend_mode) {
337 		case DRM_MODE_BLEND_PREMULTI:
338 			asyw->blend.src_color = NVC37E_SET_COMPOSITION_FACTOR_SELECT_SRC_COLOR_FACTOR_MATCH_SELECT_K1;
339 			asyw->blend.dst_color = NVC37E_SET_COMPOSITION_FACTOR_SELECT_DST_COLOR_FACTOR_MATCH_SELECT_NEG_K1_TIMES_SRC;
340 			break;
341 		case DRM_MODE_BLEND_COVERAGE:
342 			asyw->blend.src_color = NVC37E_SET_COMPOSITION_FACTOR_SELECT_SRC_COLOR_FACTOR_MATCH_SELECT_K1_TIMES_SRC;
343 			asyw->blend.dst_color = NVC37E_SET_COMPOSITION_FACTOR_SELECT_DST_COLOR_FACTOR_MATCH_SELECT_NEG_K1_TIMES_SRC;
344 			break;
345 		case DRM_MODE_BLEND_PIXEL_NONE:
346 		default:
347 			asyw->blend.src_color = NVC37E_SET_COMPOSITION_FACTOR_SELECT_SRC_COLOR_FACTOR_MATCH_SELECT_K1;
348 			asyw->blend.dst_color = NVC37E_SET_COMPOSITION_FACTOR_SELECT_DST_COLOR_FACTOR_MATCH_SELECT_NEG_K1;
349 			break;
350 		}
351 		if (memcmp(&armw->blend, &asyw->blend, sizeof(asyw->blend)))
352 			asyw->set.blend = true;
353 	}
354 
355 	if (wndw->immd) {
356 		asyw->point.x = asyw->state.crtc_x;
357 		asyw->point.y = asyw->state.crtc_y;
358 		if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
359 			asyw->set.point = true;
360 	}
361 
362 	return wndw->func->acquire(wndw, asyw, asyh);
363 }
364 
365 static int
366 nv50_wndw_atomic_check_lut(struct nv50_wndw *wndw,
367 			   struct nv50_wndw_atom *armw,
368 			   struct nv50_wndw_atom *asyw,
369 			   struct nv50_head_atom *asyh)
370 {
371 	struct drm_property_blob *ilut = asyh->state.degamma_lut;
372 
373 	/* I8 format without an input LUT makes no sense, and the
374 	 * HW error-checks for this.
375 	 *
376 	 * In order to handle legacy gamma, when there's no input
377 	 * LUT we need to steal the output LUT and use it instead.
378 	 */
379 	if (!ilut && asyw->state.fb->format->format == DRM_FORMAT_C8) {
380 		/* This should be an error, but there's legacy clients
381 		 * that do a modeset before providing a gamma table.
382 		 *
383 		 * We keep the window disabled to avoid angering HW.
384 		 */
385 		if (!(ilut = asyh->state.gamma_lut)) {
386 			asyw->visible = false;
387 			return 0;
388 		}
389 
390 		if (wndw->func->ilut)
391 			asyh->wndw.olut |= BIT(wndw->id);
392 	} else {
393 		asyh->wndw.olut &= ~BIT(wndw->id);
394 	}
395 
396 	if (!ilut && wndw->func->ilut_identity &&
397 	    asyw->state.fb->format->format != DRM_FORMAT_XBGR16161616F &&
398 	    asyw->state.fb->format->format != DRM_FORMAT_ABGR16161616F) {
399 		static struct drm_property_blob dummy = {};
400 		ilut = &dummy;
401 	}
402 
403 	/* Recalculate LUT state. */
404 	memset(&asyw->xlut, 0x00, sizeof(asyw->xlut));
405 	if ((asyw->ilut = wndw->func->ilut ? ilut : NULL)) {
406 		wndw->func->ilut(wndw, asyw, drm_color_lut_size(ilut));
407 		asyw->xlut.handle = wndw->wndw.vram.handle;
408 		asyw->xlut.i.buffer = !asyw->xlut.i.buffer;
409 		asyw->set.xlut = true;
410 	} else {
411 		asyw->clr.xlut = armw->xlut.handle != 0;
412 	}
413 
414 	/* Handle setting base SET_OUTPUT_LUT_LO_ENABLE_USE_CORE_LUT. */
415 	if (wndw->func->olut_core &&
416 	    (!armw->visible || (armw->xlut.handle && !asyw->xlut.handle)))
417 		asyw->set.xlut = true;
418 
419 	if (wndw->func->csc && asyh->state.ctm) {
420 		const struct drm_color_ctm *ctm = asyh->state.ctm->data;
421 		wndw->func->csc(wndw, asyw, ctm);
422 		asyw->csc.valid = true;
423 		asyw->set.csc = true;
424 	} else {
425 		asyw->csc.valid = false;
426 		asyw->clr.csc = armw->csc.valid;
427 	}
428 
429 	/* Can't do an immediate flip while changing the LUT. */
430 	asyh->state.async_flip = false;
431 	return 0;
432 }
433 
434 static int
435 nv50_wndw_atomic_check(struct drm_plane *plane,
436 		       struct drm_atomic_state *state)
437 {
438 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
439 										 plane);
440 	struct nouveau_drm *drm = nouveau_drm(plane->dev);
441 	struct nv50_wndw *wndw = nv50_wndw(plane);
442 	struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
443 	struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
444 	struct nv50_head_atom *harm = NULL, *asyh = NULL;
445 	bool modeset = false;
446 	int ret;
447 
448 	NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
449 
450 	/* Fetch the assembly state for the head the window will belong to,
451 	 * and determine whether the window will be visible.
452 	 */
453 	if (asyw->state.crtc) {
454 		asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
455 		if (IS_ERR(asyh))
456 			return PTR_ERR(asyh);
457 		modeset = drm_atomic_crtc_needs_modeset(&asyh->state);
458 		asyw->visible = asyh->state.active;
459 	} else {
460 		asyw->visible = false;
461 	}
462 
463 	/* Fetch assembly state for the head the window used to belong to. */
464 	if (armw->state.crtc) {
465 		harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
466 		if (IS_ERR(harm))
467 			return PTR_ERR(harm);
468 	}
469 
470 	/* LUT configuration can potentially cause the window to be disabled. */
471 	if (asyw->visible && wndw->func->xlut_set &&
472 	    (!armw->visible ||
473 	     asyh->state.color_mgmt_changed ||
474 	     asyw->state.fb->format->format !=
475 	     armw->state.fb->format->format)) {
476 		ret = nv50_wndw_atomic_check_lut(wndw, armw, asyw, asyh);
477 		if (ret)
478 			return ret;
479 	}
480 
481 	/* Calculate new window state. */
482 	if (asyw->visible) {
483 		ret = nv50_wndw_atomic_check_acquire(wndw, modeset,
484 						     armw, asyw, asyh);
485 		if (ret)
486 			return ret;
487 
488 		asyh->wndw.mask |= BIT(wndw->id);
489 	} else
490 	if (armw->visible) {
491 		nv50_wndw_atomic_check_release(wndw, asyw, harm);
492 		harm->wndw.mask &= ~BIT(wndw->id);
493 	} else {
494 		return 0;
495 	}
496 
497 	/* Aside from the obvious case where the window is actively being
498 	 * disabled, we might also need to temporarily disable the window
499 	 * when performing certain modeset operations.
500 	 */
501 	if (!asyw->visible || modeset) {
502 		asyw->clr.ntfy = armw->ntfy.handle != 0;
503 		asyw->clr.sema = armw->sema.handle != 0;
504 		asyw->clr.xlut = armw->xlut.handle != 0;
505 		if (asyw->clr.xlut && asyw->visible)
506 			asyw->set.xlut = asyw->xlut.handle != 0;
507 		asyw->clr.csc  = armw->csc.valid;
508 		if (wndw->func->image_clr)
509 			asyw->clr.image = armw->image.handle[0] != 0;
510 	}
511 
512 	return 0;
513 }
514 
515 static void
516 nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
517 {
518 	struct nouveau_drm *drm = nouveau_drm(plane->dev);
519 	struct nouveau_bo *nvbo;
520 
521 	NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
522 	if (!old_state->fb)
523 		return;
524 
525 	nvbo = nouveau_gem_object(old_state->fb->obj[0]);
526 	nouveau_bo_unpin(nvbo);
527 }
528 
529 static int
530 nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
531 {
532 	struct drm_framebuffer *fb = state->fb;
533 	struct nouveau_drm *drm = nouveau_drm(plane->dev);
534 	struct nv50_wndw *wndw = nv50_wndw(plane);
535 	struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
536 	struct nouveau_bo *nvbo;
537 	struct nv50_head_atom *asyh;
538 	struct nv50_wndw_ctxdma *ctxdma;
539 	struct dma_resv_iter cursor;
540 	struct dma_fence *fence;
541 	int ret;
542 
543 	NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, fb);
544 	if (!asyw->state.fb)
545 		return 0;
546 
547 	nvbo = nouveau_gem_object(fb->obj[0]);
548 	ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, true);
549 	if (ret)
550 		return ret;
551 
552 	if (wndw->ctxdma.parent) {
553 		ctxdma = nv50_wndw_ctxdma_new(wndw, fb);
554 		if (IS_ERR(ctxdma)) {
555 			nouveau_bo_unpin(nvbo);
556 			return PTR_ERR(ctxdma);
557 		}
558 
559 		if (asyw->visible)
560 			asyw->image.handle[0] = ctxdma->object.handle;
561 	}
562 
563 	dma_resv_iter_begin(&cursor, nvbo->bo.base.resv, false);
564 	dma_resv_for_each_fence_unlocked(&cursor, fence) {
565 		/* TODO: We only use the first writer here */
566 		asyw->state.fence = dma_fence_get(fence);
567 		break;
568 	}
569 	dma_resv_iter_end(&cursor);
570 	asyw->image.offset[0] = nvbo->offset;
571 
572 	if (wndw->func->prepare) {
573 		asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
574 		if (IS_ERR(asyh))
575 			return PTR_ERR(asyh);
576 
577 		wndw->func->prepare(wndw, asyh, asyw);
578 	}
579 
580 	return 0;
581 }
582 
583 static const struct drm_plane_helper_funcs
584 nv50_wndw_helper = {
585 	.prepare_fb = nv50_wndw_prepare_fb,
586 	.cleanup_fb = nv50_wndw_cleanup_fb,
587 	.atomic_check = nv50_wndw_atomic_check,
588 };
589 
590 static void
591 nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
592 			       struct drm_plane_state *state)
593 {
594 	struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
595 	__drm_atomic_helper_plane_destroy_state(&asyw->state);
596 	kfree(asyw);
597 }
598 
599 static struct drm_plane_state *
600 nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
601 {
602 	struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
603 	struct nv50_wndw_atom *asyw;
604 	if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
605 		return NULL;
606 	__drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
607 	asyw->sema = armw->sema;
608 	asyw->ntfy = armw->ntfy;
609 	asyw->ilut = NULL;
610 	asyw->xlut = armw->xlut;
611 	asyw->csc  = armw->csc;
612 	asyw->image = armw->image;
613 	asyw->point = armw->point;
614 	asyw->clr.mask = 0;
615 	asyw->set.mask = 0;
616 	return &asyw->state;
617 }
618 
619 static int
620 nv50_wndw_zpos_default(struct drm_plane *plane)
621 {
622 	return (plane->type == DRM_PLANE_TYPE_PRIMARY) ? 0 :
623 	       (plane->type == DRM_PLANE_TYPE_OVERLAY) ? 1 : 255;
624 }
625 
626 static void
627 nv50_wndw_reset(struct drm_plane *plane)
628 {
629 	struct nv50_wndw_atom *asyw;
630 
631 	if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
632 		return;
633 
634 	if (plane->state)
635 		plane->funcs->atomic_destroy_state(plane, plane->state);
636 
637 	__drm_atomic_helper_plane_reset(plane, &asyw->state);
638 	plane->state->zpos = nv50_wndw_zpos_default(plane);
639 	plane->state->normalized_zpos = nv50_wndw_zpos_default(plane);
640 }
641 
642 static void
643 nv50_wndw_destroy(struct drm_plane *plane)
644 {
645 	struct nv50_wndw *wndw = nv50_wndw(plane);
646 	struct nv50_wndw_ctxdma *ctxdma, *ctxtmp;
647 
648 	list_for_each_entry_safe(ctxdma, ctxtmp, &wndw->ctxdma.list, head) {
649 		nv50_wndw_ctxdma_del(ctxdma);
650 	}
651 
652 	nvif_notify_dtor(&wndw->notify);
653 	nv50_dmac_destroy(&wndw->wimm);
654 	nv50_dmac_destroy(&wndw->wndw);
655 
656 	nv50_lut_fini(&wndw->ilut);
657 
658 	drm_plane_cleanup(&wndw->plane);
659 	kfree(wndw);
660 }
661 
662 /* This function assumes the format has already been validated against the plane
663  * and the modifier was validated against the device-wides modifier list at FB
664  * creation time.
665  */
666 static bool nv50_plane_format_mod_supported(struct drm_plane *plane,
667 					    u32 format, u64 modifier)
668 {
669 	struct nouveau_drm *drm = nouveau_drm(plane->dev);
670 	uint8_t i;
671 
672 	if (drm->client.device.info.chipset < 0xc0) {
673 		const struct drm_format_info *info = drm_format_info(format);
674 		const uint8_t kind = (modifier >> 12) & 0xff;
675 
676 		if (!format) return false;
677 
678 		for (i = 0; i < info->num_planes; i++)
679 			if ((info->cpp[i] != 4) && kind != 0x70) return false;
680 	}
681 
682 	return true;
683 }
684 
685 const struct drm_plane_funcs
686 nv50_wndw = {
687 	.update_plane = drm_atomic_helper_update_plane,
688 	.disable_plane = drm_atomic_helper_disable_plane,
689 	.destroy = nv50_wndw_destroy,
690 	.reset = nv50_wndw_reset,
691 	.atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
692 	.atomic_destroy_state = nv50_wndw_atomic_destroy_state,
693 	.format_mod_supported = nv50_plane_format_mod_supported,
694 };
695 
696 static int
697 nv50_wndw_notify(struct nvif_notify *notify)
698 {
699 	return NVIF_NOTIFY_KEEP;
700 }
701 
702 void
703 nv50_wndw_fini(struct nv50_wndw *wndw)
704 {
705 	nvif_notify_put(&wndw->notify);
706 }
707 
708 void
709 nv50_wndw_init(struct nv50_wndw *wndw)
710 {
711 	nvif_notify_get(&wndw->notify);
712 }
713 
714 static const u64 nv50_cursor_format_modifiers[] = {
715 	DRM_FORMAT_MOD_LINEAR,
716 	DRM_FORMAT_MOD_INVALID,
717 };
718 
719 int
720 nv50_wndw_new_(const struct nv50_wndw_func *func, struct drm_device *dev,
721 	       enum drm_plane_type type, const char *name, int index,
722 	       const u32 *format, u32 heads,
723 	       enum nv50_disp_interlock_type interlock_type, u32 interlock_data,
724 	       struct nv50_wndw **pwndw)
725 {
726 	struct nouveau_drm *drm = nouveau_drm(dev);
727 	struct nvif_mmu *mmu = &drm->client.mmu;
728 	struct nv50_disp *disp = nv50_disp(dev);
729 	struct nv50_wndw *wndw;
730 	const u64 *format_modifiers;
731 	int nformat;
732 	int ret;
733 
734 	if (!(wndw = *pwndw = kzalloc(sizeof(*wndw), GFP_KERNEL)))
735 		return -ENOMEM;
736 	wndw->func = func;
737 	wndw->id = index;
738 	wndw->interlock.type = interlock_type;
739 	wndw->interlock.data = interlock_data;
740 
741 	wndw->ctxdma.parent = &wndw->wndw.base.user;
742 	INIT_LIST_HEAD(&wndw->ctxdma.list);
743 
744 	for (nformat = 0; format[nformat]; nformat++);
745 
746 	if (type == DRM_PLANE_TYPE_CURSOR)
747 		format_modifiers = nv50_cursor_format_modifiers;
748 	else
749 		format_modifiers = nouveau_display(dev)->format_modifiers;
750 
751 	ret = drm_universal_plane_init(dev, &wndw->plane, heads, &nv50_wndw, format, nformat,
752 				       format_modifiers, type, "%s-%d", name, index);
753 	if (ret) {
754 		kfree(*pwndw);
755 		*pwndw = NULL;
756 		return ret;
757 	}
758 
759 	drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
760 
761 	if (wndw->func->ilut) {
762 		ret = nv50_lut_init(disp, mmu, &wndw->ilut);
763 		if (ret)
764 			return ret;
765 	}
766 
767 	wndw->notify.func = nv50_wndw_notify;
768 
769 	if (wndw->func->blend_set) {
770 		ret = drm_plane_create_zpos_property(&wndw->plane,
771 				nv50_wndw_zpos_default(&wndw->plane), 0, 254);
772 		if (ret)
773 			return ret;
774 
775 		ret = drm_plane_create_alpha_property(&wndw->plane);
776 		if (ret)
777 			return ret;
778 
779 		ret = drm_plane_create_blend_mode_property(&wndw->plane,
780 				BIT(DRM_MODE_BLEND_PIXEL_NONE) |
781 				BIT(DRM_MODE_BLEND_PREMULTI) |
782 				BIT(DRM_MODE_BLEND_COVERAGE));
783 		if (ret)
784 			return ret;
785 	} else {
786 		ret = drm_plane_create_zpos_immutable_property(&wndw->plane,
787 				nv50_wndw_zpos_default(&wndw->plane));
788 		if (ret)
789 			return ret;
790 	}
791 
792 	return 0;
793 }
794 
795 int
796 nv50_wndw_new(struct nouveau_drm *drm, enum drm_plane_type type, int index,
797 	      struct nv50_wndw **pwndw)
798 {
799 	struct {
800 		s32 oclass;
801 		int version;
802 		int (*new)(struct nouveau_drm *, enum drm_plane_type,
803 			   int, s32, struct nv50_wndw **);
804 	} wndws[] = {
805 		{ GA102_DISP_WINDOW_CHANNEL_DMA, 0, wndwc67e_new },
806 		{ TU102_DISP_WINDOW_CHANNEL_DMA, 0, wndwc57e_new },
807 		{ GV100_DISP_WINDOW_CHANNEL_DMA, 0, wndwc37e_new },
808 		{}
809 	};
810 	struct nv50_disp *disp = nv50_disp(drm->dev);
811 	int cid, ret;
812 
813 	cid = nvif_mclass(&disp->disp->object, wndws);
814 	if (cid < 0) {
815 		NV_ERROR(drm, "No supported window class\n");
816 		return cid;
817 	}
818 
819 	ret = wndws[cid].new(drm, type, index, wndws[cid].oclass, pwndw);
820 	if (ret)
821 		return ret;
822 
823 	return nv50_wimm_init(drm, *pwndw);
824 }
825