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