1/*	$NetBSD: nouveau_dispnv50_ovly507e.c,v 1.2 2021/12/18 23:45:32 riastradh Exp $	*/
2
3/*
4 * Copyright 2018 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <sys/cdefs.h>
25__KERNEL_RCSID(0, "$NetBSD: nouveau_dispnv50_ovly507e.c,v 1.2 2021/12/18 23:45:32 riastradh Exp $");
26
27#include "ovly.h"
28#include "atom.h"
29
30#include <drm/drm_atomic_helper.h>
31#include <drm/drm_fourcc.h>
32#include <drm/drm_plane_helper.h>
33
34#include <nvif/cl507e.h>
35#include <nvif/event.h>
36
37void
38ovly507e_update(struct nv50_wndw *wndw, u32 *interlock)
39{
40	u32 *push;
41	if ((push = evo_wait(&wndw->wndw, 2))) {
42		evo_mthd(push, 0x0080, 1);
43		evo_data(push, interlock[NV50_DISP_INTERLOCK_CORE]);
44		evo_kick(push, &wndw->wndw);
45	}
46}
47
48void
49ovly507e_scale_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
50{
51	u32 *push;
52	if ((push = evo_wait(&wndw->wndw, 4))) {
53		evo_mthd(push, 0x00e0, 3);
54		evo_data(push, asyw->scale.sy << 16 | asyw->scale.sx);
55		evo_data(push, asyw->scale.sh << 16 | asyw->scale.sw);
56		evo_data(push, asyw->scale.dw);
57		evo_kick(push, &wndw->wndw);
58	}
59}
60
61void
62ovly507e_image_clr(struct nv50_wndw *wndw)
63{
64	u32 *push;
65	if ((push = evo_wait(&wndw->wndw, 4))) {
66		evo_mthd(push, 0x0084, 1);
67		evo_data(push, 0x00000000);
68		evo_mthd(push, 0x00c0, 1);
69		evo_data(push, 0x00000000);
70		evo_kick(push, &wndw->wndw);
71	}
72}
73
74static void
75ovly507e_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
76{
77	u32 *push;
78	if ((push = evo_wait(&wndw->wndw, 12))) {
79		evo_mthd(push, 0x0084, 1);
80		evo_data(push, asyw->image.interval << 4);
81		evo_mthd(push, 0x00c0, 1);
82		evo_data(push, asyw->image.handle[0]);
83		evo_mthd(push, 0x0100, 1);
84		evo_data(push, 0x00000002);
85		evo_mthd(push, 0x0800, 1);
86		evo_data(push, asyw->image.offset[0] >> 8);
87		evo_mthd(push, 0x0808, 3);
88		evo_data(push, asyw->image.h << 16 | asyw->image.w);
89		evo_data(push, asyw->image.layout << 20 |
90			       (asyw->image.pitch[0] >> 8) << 8 |
91			       asyw->image.blocks[0] << 8 |
92			       asyw->image.blockh);
93		evo_data(push, asyw->image.kind << 16 |
94			       asyw->image.format << 8 |
95			       asyw->image.colorspace);
96		evo_kick(push, &wndw->wndw);
97	}
98}
99
100void
101ovly507e_ntfy_clr(struct nv50_wndw *wndw)
102{
103	u32 *push;
104	if ((push = evo_wait(&wndw->wndw, 2))) {
105		evo_mthd(push, 0x00a4, 1);
106		evo_data(push, 0x00000000);
107		evo_kick(push, &wndw->wndw);
108	}
109}
110
111void
112ovly507e_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
113{
114	u32 *push;
115	if ((push = evo_wait(&wndw->wndw, 3))) {
116		evo_mthd(push, 0x00a0, 2);
117		evo_data(push, asyw->ntfy.awaken << 30 | asyw->ntfy.offset);
118		evo_data(push, asyw->ntfy.handle);
119		evo_kick(push, &wndw->wndw);
120	}
121}
122
123void
124ovly507e_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
125		 struct nv50_head_atom *asyh)
126{
127	asyh->ovly.cpp = 0;
128}
129
130int
131ovly507e_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
132		 struct nv50_head_atom *asyh)
133{
134	const struct drm_framebuffer *fb = asyw->state.fb;
135	int ret;
136
137	ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
138						  DRM_PLANE_HELPER_NO_SCALING,
139						  DRM_PLANE_HELPER_NO_SCALING,
140						  true, true);
141	if (ret)
142		return ret;
143
144	asyh->ovly.cpp = fb->format->cpp[0];
145	return 0;
146}
147
148#include "nouveau_bo.h"
149
150static const struct nv50_wndw_func
151ovly507e = {
152	.acquire = ovly507e_acquire,
153	.release = ovly507e_release,
154	.ntfy_set = ovly507e_ntfy_set,
155	.ntfy_clr = ovly507e_ntfy_clr,
156	.ntfy_reset = base507c_ntfy_reset,
157	.ntfy_wait_begun = base507c_ntfy_wait_begun,
158	.image_set = ovly507e_image_set,
159	.image_clr = ovly507e_image_clr,
160	.scale_set = ovly507e_scale_set,
161	.update = ovly507e_update,
162};
163
164static const u32
165ovly507e_format[] = {
166	DRM_FORMAT_YUYV,
167	DRM_FORMAT_UYVY,
168	DRM_FORMAT_XRGB8888,
169	DRM_FORMAT_XRGB1555,
170	0
171};
172
173int
174ovly507e_new_(const struct nv50_wndw_func *func, const u32 *format,
175	      struct nouveau_drm *drm, int head, s32 oclass, u32 interlock_data,
176	      struct nv50_wndw **pwndw)
177{
178	struct nv50_disp_overlay_channel_dma_v0 args = {
179		.head = head,
180	};
181	struct nv50_disp *disp = nv50_disp(drm->dev);
182	struct nv50_wndw *wndw;
183	int ret;
184
185	ret = nv50_wndw_new_(func, drm->dev, DRM_PLANE_TYPE_OVERLAY,
186			     "ovly", head, format, BIT(head),
187			     NV50_DISP_INTERLOCK_OVLY, interlock_data,
188			     &wndw);
189	if (*pwndw = wndw, ret)
190		return ret;
191
192	ret = nv50_dmac_create(&drm->client.device, &disp->disp->object,
193			       &oclass, 0, &args, sizeof(args),
194			       disp->sync->bo.offset, &wndw->wndw);
195	if (ret) {
196		NV_ERROR(drm, "ovly%04x allocation failed: %d\n", oclass, ret);
197		return ret;
198	}
199
200	ret = nvif_notify_init(&wndw->wndw.base.user, wndw->notify.func, false,
201			       NV50_DISP_OVERLAY_CHANNEL_DMA_V0_NTFY_UEVENT,
202			       &(struct nvif_notify_uevent_req) {},
203			       sizeof(struct nvif_notify_uevent_req),
204			       sizeof(struct nvif_notify_uevent_rep),
205			       &wndw->notify);
206	if (ret)
207		return ret;
208
209	wndw->ntfy = NV50_DISP_OVLY_NTFY(wndw->id);
210	wndw->sema = NV50_DISP_OVLY_SEM0(wndw->id);
211	wndw->data = 0x00000000;
212	return 0;
213}
214
215int
216ovly507e_new(struct nouveau_drm *drm, int head, s32 oclass,
217	     struct nv50_wndw **pwndw)
218{
219	return ovly507e_new_(&ovly507e, ovly507e_format, drm, head, oclass,
220			     0x00000004 << (head * 8), pwndw);
221}
222