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 "atom.h"
24 
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_plane_helper.h>
27 #include <nouveau_bo.h>
28 
29 #include <nvif/clc37e.h>
30 
31 static void
32 wndwc37e_csc_clr(struct nv50_wndw *wndw)
33 {
34 }
35 
36 static void
37 wndwc37e_csc_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
38 {
39 	u32 *push, i;
40 	if ((push = evo_wait(&wndw->wndw, 13))) {
41 		 evo_mthd(push, 0x02bc, 12);
42 		 for (i = 0; i < 12; i++)
43 			  evo_data(push, asyw->csc.matrix[i]);
44 		 evo_kick(push, &wndw->wndw);
45 	}
46 }
47 
48 static void
49 wndwc37e_ilut_clr(struct nv50_wndw *wndw)
50 {
51 	u32 *push;
52 	if ((push = evo_wait(&wndw->wndw, 2))) {
53 		evo_mthd(push, 0x02b8, 1);
54 		evo_data(push, 0x00000000);
55 		evo_kick(push, &wndw->wndw);
56 	}
57 }
58 
59 static void
60 wndwc37e_ilut_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
61 {
62 	u32 *push;
63 	if ((push = evo_wait(&wndw->wndw, 4))) {
64 		evo_mthd(push, 0x02b0, 3);
65 		evo_data(push, asyw->xlut.i.output_mode << 8 |
66 			       asyw->xlut.i.range << 4 |
67 			       asyw->xlut.i.size);
68 		evo_data(push, asyw->xlut.i.offset >> 8);
69 		evo_data(push, asyw->xlut.handle);
70 		evo_kick(push, &wndw->wndw);
71 	}
72 }
73 
74 static bool
75 wndwc37e_ilut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw, int size)
76 {
77 	if (size != 256 && size != 1024)
78 		return false;
79 
80 	asyw->xlut.i.mode = 2;
81 	asyw->xlut.i.size = size == 1024 ? 2 : 0;
82 	asyw->xlut.i.range = 0;
83 	asyw->xlut.i.output_mode = 1;
84 	asyw->xlut.i.load = head907d_olut_load;
85 	return true;
86 }
87 
88 void
89 wndwc37e_blend_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
90 {
91 	u32 *push;
92 	if ((push = evo_wait(&wndw->wndw, 8))) {
93 		evo_mthd(push, 0x02ec, 7);
94 		evo_data(push, asyw->blend.depth << 4);
95 		evo_data(push, asyw->blend.k1);
96 		evo_data(push, asyw->blend.dst_color << 12 |
97 			       asyw->blend.dst_color << 8 |
98 			       asyw->blend.src_color << 4 |
99 			       asyw->blend.src_color);
100 		evo_data(push, 0xffff0000);
101 		evo_data(push, 0xffff0000);
102 		evo_data(push, 0xffff0000);
103 		evo_data(push, 0xffff0000);
104 		evo_kick(push, &wndw->wndw);
105 	}
106 }
107 
108 void
109 wndwc37e_image_clr(struct nv50_wndw *wndw)
110 {
111 	u32 *push;
112 	if ((push = evo_wait(&wndw->wndw, 4))) {
113 		evo_mthd(push, 0x0308, 1);
114 		evo_data(push, 0x00000000);
115 		evo_mthd(push, 0x0240, 1);
116 		evo_data(push, 0x00000000);
117 		evo_kick(push, &wndw->wndw);
118 	}
119 }
120 
121 static void
122 wndwc37e_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
123 {
124 	u32 *push;
125 
126 	if (!(push = evo_wait(&wndw->wndw, 17)))
127 		return;
128 
129 	evo_mthd(push, 0x0308, 1);
130 	evo_data(push, asyw->image.mode << 4 | asyw->image.interval);
131 	evo_mthd(push, 0x0224, 4);
132 	evo_data(push, asyw->image.h << 16 | asyw->image.w);
133 	evo_data(push, asyw->image.layout << 4 | asyw->image.blockh);
134 	evo_data(push, asyw->csc.valid << 17 |
135 		       asyw->image.colorspace << 8 |
136 		       asyw->image.format);
137 	evo_data(push, asyw->image.blocks[0] | (asyw->image.pitch[0] >> 6));
138 	evo_mthd(push, 0x0240, 1);
139 	evo_data(push, asyw->image.handle[0]);
140 	evo_mthd(push, 0x0260, 1);
141 	evo_data(push, asyw->image.offset[0] >> 8);
142 	evo_mthd(push, 0x0290, 1);
143 	evo_data(push, (asyw->state.src_y >> 16) << 16 |
144 		       (asyw->state.src_x >> 16));
145 	evo_mthd(push, 0x0298, 1);
146 	evo_data(push, (asyw->state.src_h >> 16) << 16 |
147 		       (asyw->state.src_w >> 16));
148 	evo_mthd(push, 0x02a4, 1);
149 	evo_data(push, asyw->state.crtc_h << 16 |
150 		       asyw->state.crtc_w);
151 	evo_kick(push, &wndw->wndw);
152 }
153 
154 void
155 wndwc37e_ntfy_clr(struct nv50_wndw *wndw)
156 {
157 	u32 *push;
158 	if ((push = evo_wait(&wndw->wndw, 2))) {
159 		evo_mthd(push, 0x021c, 1);
160 		evo_data(push, 0x00000000);
161 		evo_kick(push, &wndw->wndw);
162 	}
163 }
164 
165 void
166 wndwc37e_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
167 {
168 	u32 *push;
169 	if ((push = evo_wait(&wndw->wndw, 3))) {
170 		evo_mthd(push, 0x021c, 2);
171 		evo_data(push, asyw->ntfy.handle);
172 		evo_data(push, asyw->ntfy.offset | asyw->ntfy.awaken);
173 		evo_kick(push, &wndw->wndw);
174 	}
175 }
176 
177 void
178 wndwc37e_sema_clr(struct nv50_wndw *wndw)
179 {
180 	u32 *push;
181 	if ((push = evo_wait(&wndw->wndw, 2))) {
182 		evo_mthd(push, 0x0218, 1);
183 		evo_data(push, 0x00000000);
184 		evo_kick(push, &wndw->wndw);
185 	}
186 }
187 
188 void
189 wndwc37e_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
190 {
191 	u32 *push;
192 	if ((push = evo_wait(&wndw->wndw, 5))) {
193 		evo_mthd(push, 0x020c, 4);
194 		evo_data(push, asyw->sema.offset);
195 		evo_data(push, asyw->sema.acquire);
196 		evo_data(push, asyw->sema.release);
197 		evo_data(push, asyw->sema.handle);
198 		evo_kick(push, &wndw->wndw);
199 	}
200 }
201 
202 void
203 wndwc37e_update(struct nv50_wndw *wndw, u32 *interlock)
204 {
205 	u32 *push;
206 	if ((push = evo_wait(&wndw->wndw, 5))) {
207 		evo_mthd(push, 0x0370, 2);
208 		evo_data(push, interlock[NV50_DISP_INTERLOCK_CURS] << 1 |
209 			       interlock[NV50_DISP_INTERLOCK_CORE]);
210 		evo_data(push, interlock[NV50_DISP_INTERLOCK_WNDW]);
211 		evo_mthd(push, 0x0200, 1);
212 		if (interlock[NV50_DISP_INTERLOCK_WIMM] & wndw->interlock.data)
213 			evo_data(push, 0x00001001);
214 		else
215 			evo_data(push, 0x00000001);
216 		evo_kick(push, &wndw->wndw);
217 	}
218 }
219 
220 void
221 wndwc37e_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
222 		 struct nv50_head_atom *asyh)
223 {
224 }
225 
226 int
227 wndwc37e_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
228 		 struct nv50_head_atom *asyh)
229 {
230 	return drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
231 						   DRM_PLANE_HELPER_NO_SCALING,
232 						   DRM_PLANE_HELPER_NO_SCALING,
233 						   true, true);
234 }
235 
236 static const u32
237 wndwc37e_format[] = {
238 	DRM_FORMAT_C8,
239 	DRM_FORMAT_YUYV,
240 	DRM_FORMAT_UYVY,
241 	DRM_FORMAT_XRGB8888,
242 	DRM_FORMAT_ARGB8888,
243 	DRM_FORMAT_RGB565,
244 	DRM_FORMAT_XRGB1555,
245 	DRM_FORMAT_ARGB1555,
246 	DRM_FORMAT_XBGR2101010,
247 	DRM_FORMAT_ABGR2101010,
248 	DRM_FORMAT_XBGR8888,
249 	DRM_FORMAT_ABGR8888,
250 	DRM_FORMAT_XRGB2101010,
251 	DRM_FORMAT_ARGB2101010,
252 	DRM_FORMAT_XBGR16161616F,
253 	DRM_FORMAT_ABGR16161616F,
254 	0
255 };
256 
257 static const struct nv50_wndw_func
258 wndwc37e = {
259 	.acquire = wndwc37e_acquire,
260 	.release = wndwc37e_release,
261 	.sema_set = wndwc37e_sema_set,
262 	.sema_clr = wndwc37e_sema_clr,
263 	.ntfy_set = wndwc37e_ntfy_set,
264 	.ntfy_clr = wndwc37e_ntfy_clr,
265 	.ntfy_reset = corec37d_ntfy_init,
266 	.ntfy_wait_begun = base507c_ntfy_wait_begun,
267 	.ilut = wndwc37e_ilut,
268 	.ilut_size = 1024,
269 	.xlut_set = wndwc37e_ilut_set,
270 	.xlut_clr = wndwc37e_ilut_clr,
271 	.csc = base907c_csc,
272 	.csc_set = wndwc37e_csc_set,
273 	.csc_clr = wndwc37e_csc_clr,
274 	.image_set = wndwc37e_image_set,
275 	.image_clr = wndwc37e_image_clr,
276 	.blend_set = wndwc37e_blend_set,
277 	.update = wndwc37e_update,
278 };
279 
280 int
281 wndwc37e_new_(const struct nv50_wndw_func *func, struct nouveau_drm *drm,
282 	      enum drm_plane_type type, int index, s32 oclass, u32 heads,
283 	      struct nv50_wndw **pwndw)
284 {
285 	struct nvc37e_window_channel_dma_v0 args = {
286 		.pushbuf = 0xb0007e00 | index,
287 		.index = index,
288 	};
289 	struct nv50_disp *disp = nv50_disp(drm->dev);
290 	struct nv50_wndw *wndw;
291 	int ret;
292 
293 	ret = nv50_wndw_new_(func, drm->dev, type, "wndw", index,
294 			     wndwc37e_format, heads, NV50_DISP_INTERLOCK_WNDW,
295 			     BIT(index), &wndw);
296 	if (*pwndw = wndw, ret)
297 		return ret;
298 
299 	ret = nv50_dmac_create(&drm->client.device, &disp->disp->object,
300 			       &oclass, 0, &args, sizeof(args),
301 			       disp->sync->bo.offset, &wndw->wndw);
302 	if (ret) {
303 		NV_ERROR(drm, "qndw%04x allocation failed: %d\n", oclass, ret);
304 		return ret;
305 	}
306 
307 	wndw->ntfy = NV50_DISP_WNDW_NTFY(wndw->id);
308 	wndw->sema = NV50_DISP_WNDW_SEM0(wndw->id);
309 	wndw->data = 0x00000000;
310 	return 0;
311 }
312 
313 int
314 wndwc37e_new(struct nouveau_drm *drm, enum drm_plane_type type, int index,
315 	     s32 oclass, struct nv50_wndw **pwndw)
316 {
317 	return wndwc37e_new_(&wndwc37e, drm, type, index, oclass,
318 			     BIT(index >> 1), pwndw);
319 }
320