1 /*
2  * Copyright 2021 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 #define nvkm_uoutp(p) container_of((p), struct nvkm_outp, object)
23 #include "outp.h"
24 #include "dp.h"
25 #include "head.h"
26 #include "ior.h"
27 
28 #include <nvif/if0012.h>
29 
30 static int
31 nvkm_uoutp_mthd_dp_mst_vcpi(struct nvkm_outp *outp, void *argv, u32 argc)
32 {
33 	struct nvkm_ior *ior = outp->ior;
34 	union nvif_outp_dp_mst_vcpi_args *args = argv;
35 
36 	if (argc != sizeof(args->v0) || args->v0.version != 0)
37 		return -ENOSYS;
38 	if (!ior->func->dp || !ior->func->dp->vcpi || !nvkm_head_find(outp->disp, args->v0.head))
39 		return -EINVAL;
40 
41 	ior->func->dp->vcpi(ior, args->v0.head, args->v0.start_slot, args->v0.num_slots,
42 				 args->v0.pbn, args->v0.aligned_pbn);
43 	return 0;
44 }
45 
46 static int
47 nvkm_uoutp_mthd_dp_retrain(struct nvkm_outp *outp, void *argv, u32 argc)
48 {
49 	union nvif_outp_dp_retrain_args *args = argv;
50 
51 	if (argc != sizeof(args->vn))
52 		return -ENOSYS;
53 
54 	if (!atomic_read(&outp->dp.lt.done))
55 		return 0;
56 
57 	return outp->func->acquire(outp);
58 }
59 
60 static int
61 nvkm_uoutp_mthd_dp_aux_pwr(struct nvkm_outp *outp, void *argv, u32 argc)
62 {
63 	union nvif_outp_dp_aux_pwr_args *args = argv;
64 
65 	if (argc != sizeof(args->v0) || args->v0.version != 0)
66 		return -ENOSYS;
67 
68 	outp->dp.enabled = !!args->v0.state;
69 	nvkm_dp_enable(outp, outp->dp.enabled);
70 	return 0;
71 }
72 
73 static int
74 nvkm_uoutp_mthd_hda_eld(struct nvkm_outp *outp, void *argv, u32 argc)
75 {
76 	struct nvkm_ior *ior = outp->ior;
77 	union nvif_outp_hda_eld_args *args = argv;
78 
79 	if (argc < sizeof(args->v0) || args->v0.version != 0)
80 		return -ENOSYS;
81 	argc -= sizeof(args->v0);
82 
83 	if (!ior->hda || !nvkm_head_find(outp->disp, args->v0.head))
84 		return -EINVAL;
85 	if (argc > 0x60)
86 		return -E2BIG;
87 
88 	if (argc && args->v0.data[0]) {
89 		if (outp->info.type == DCB_OUTPUT_DP)
90 			ior->func->dp->audio(ior, args->v0.head, true);
91 		ior->func->hda->hpd(ior, args->v0.head, true);
92 		ior->func->hda->eld(ior, args->v0.head, args->v0.data, argc);
93 	} else {
94 		if (outp->info.type == DCB_OUTPUT_DP)
95 			ior->func->dp->audio(ior, args->v0.head, false);
96 		ior->func->hda->hpd(ior, args->v0.head, false);
97 	}
98 
99 	return 0;
100 }
101 
102 static int
103 nvkm_uoutp_mthd_infoframe(struct nvkm_outp *outp, void *argv, u32 argc)
104 {
105 	struct nvkm_ior *ior = outp->ior;
106 	union nvif_outp_infoframe_args *args = argv;
107 	ssize_t size = argc - sizeof(*args);
108 
109 	if (argc < sizeof(args->v0) || args->v0.version != 0)
110 		return -ENOSYS;
111 	if (!nvkm_head_find(outp->disp, args->v0.head))
112 		return -EINVAL;
113 
114 	switch (ior->func->hdmi ? args->v0.type : 0xff) {
115 	case NVIF_OUTP_INFOFRAME_V0_AVI:
116 		ior->func->hdmi->infoframe_avi(ior, args->v0.head, &args->v0.data, size);
117 		return 0;
118 	case NVIF_OUTP_INFOFRAME_V0_VSI:
119 		ior->func->hdmi->infoframe_vsi(ior, args->v0.head, &args->v0.data, size);
120 		return 0;
121 	default:
122 		break;
123 	}
124 
125 	return -EINVAL;
126 }
127 
128 static int
129 nvkm_uoutp_mthd_release(struct nvkm_outp *outp, void *argv, u32 argc)
130 {
131 	struct nvkm_head *head = outp->asy.head;
132 	struct nvkm_ior *ior = outp->ior;
133 	union nvif_outp_release_args *args = argv;
134 
135 	if (argc != sizeof(args->vn))
136 		return -ENOSYS;
137 
138 	if (ior->func->hdmi && head) {
139 		ior->func->hdmi->infoframe_avi(ior, head->id, NULL, 0);
140 		ior->func->hdmi->infoframe_vsi(ior, head->id, NULL, 0);
141 		ior->func->hdmi->ctrl(ior, head->id, false, 0, 0);
142 	}
143 
144 	nvkm_outp_release(outp, NVKM_OUTP_USER);
145 	return 0;
146 }
147 
148 static int
149 nvkm_uoutp_mthd_acquire_dp(struct nvkm_outp *outp, u8 dpcd[16],
150 			   u8 link_nr, u8 link_bw, bool hda, bool mst)
151 {
152 	int ret;
153 
154 	ret = nvkm_outp_acquire(outp, NVKM_OUTP_USER, hda);
155 	if (ret)
156 		return ret;
157 
158 	memcpy(outp->dp.dpcd, dpcd, sizeof(outp->dp.dpcd));
159 	outp->dp.lt.nr = link_nr;
160 	outp->dp.lt.bw = link_bw;
161 	outp->dp.lt.mst = mst;
162 	return 0;
163 }
164 
165 static int
166 nvkm_uoutp_mthd_acquire_tmds(struct nvkm_outp *outp, u8 head, u8 hdmi, u8 hdmi_max_ac_packet,
167 			     u8 hdmi_rekey, u8 hdmi_scdc, u8 hdmi_hda)
168 {
169 	struct nvkm_ior *ior;
170 	int ret;
171 
172 	if (!(outp->asy.head = nvkm_head_find(outp->disp, head)))
173 		return -EINVAL;
174 
175 	ret = nvkm_outp_acquire(outp, NVKM_OUTP_USER, hdmi && hdmi_hda);
176 	if (ret)
177 		return ret;
178 
179 	ior = outp->ior;
180 
181 	if (hdmi) {
182 		if (!ior->func->hdmi ||
183 		    hdmi_max_ac_packet > 0x1f || hdmi_rekey > 0x7f ||
184 		    (hdmi_scdc && !ior->func->hdmi->scdc)) {
185 			nvkm_outp_release(outp, NVKM_OUTP_USER);
186 			return -EINVAL;
187 		}
188 
189 		ior->func->hdmi->ctrl(ior, head, hdmi, hdmi_max_ac_packet, hdmi_rekey);
190 		if (ior->func->hdmi->scdc)
191 			ior->func->hdmi->scdc(ior, hdmi_scdc);
192 	}
193 
194 	return 0;
195 }
196 
197 static int
198 nvkm_uoutp_mthd_acquire_lvds(struct nvkm_outp *outp, bool dual, bool bpc8)
199 {
200 	if (outp->info.type != DCB_OUTPUT_LVDS)
201 		return -EINVAL;
202 
203 	outp->lvds.dual = dual;
204 	outp->lvds.bpc8 = bpc8;
205 
206 	return nvkm_outp_acquire(outp, NVKM_OUTP_USER, false);
207 }
208 
209 static int
210 nvkm_uoutp_mthd_acquire(struct nvkm_outp *outp, void *argv, u32 argc)
211 {
212 	union nvif_outp_acquire_args *args = argv;
213 	int ret;
214 
215 	if (argc != sizeof(args->v0) || args->v0.version != 0)
216 		return -ENOSYS;
217 	if (outp->ior)
218 		return -EBUSY;
219 
220 	switch (args->v0.proto) {
221 	case NVIF_OUTP_ACQUIRE_V0_RGB_CRT:
222 		ret = nvkm_outp_acquire(outp, NVKM_OUTP_USER, false);
223 		break;
224 	case NVIF_OUTP_ACQUIRE_V0_TMDS:
225 		ret = nvkm_uoutp_mthd_acquire_tmds(outp, args->v0.tmds.head,
226 							 args->v0.tmds.hdmi,
227 							 args->v0.tmds.hdmi_max_ac_packet,
228 							 args->v0.tmds.hdmi_rekey,
229 							 args->v0.tmds.hdmi_scdc,
230 							 args->v0.tmds.hdmi_hda);
231 		break;
232 	case NVIF_OUTP_ACQUIRE_V0_LVDS:
233 		ret = nvkm_uoutp_mthd_acquire_lvds(outp, args->v0.lvds.dual, args->v0.lvds.bpc8);
234 		break;
235 	case NVIF_OUTP_ACQUIRE_V0_DP:
236 		ret = nvkm_uoutp_mthd_acquire_dp(outp, args->v0.dp.dpcd,
237 						       args->v0.dp.link_nr,
238 						       args->v0.dp.link_bw,
239 						       args->v0.dp.hda != 0,
240 						       args->v0.dp.mst != 0);
241 		break;
242 	default:
243 		ret = -EINVAL;
244 		break;
245 	}
246 
247 	if (ret)
248 		return ret;
249 
250 	args->v0.or = outp->ior->id;
251 	args->v0.link = outp->ior->asy.link;
252 	return 0;
253 }
254 
255 static int
256 nvkm_uoutp_mthd_load_detect(struct nvkm_outp *outp, void *argv, u32 argc)
257 {
258 	union nvif_outp_load_detect_args *args = argv;
259 	int ret;
260 
261 	if (argc != sizeof(args->v0) || args->v0.version != 0)
262 		return -ENOSYS;
263 
264 	ret = nvkm_outp_acquire(outp, NVKM_OUTP_PRIV, false);
265 	if (ret == 0) {
266 		if (outp->ior->func->sense) {
267 			ret = outp->ior->func->sense(outp->ior, args->v0.data);
268 			args->v0.load = ret < 0 ? 0 : ret;
269 		} else {
270 			ret = -EINVAL;
271 		}
272 		nvkm_outp_release(outp, NVKM_OUTP_PRIV);
273 	}
274 
275 	return ret;
276 }
277 
278 static int
279 nvkm_uoutp_mthd_acquired(struct nvkm_outp *outp, u32 mthd, void *argv, u32 argc)
280 {
281 	switch (mthd) {
282 	case NVIF_OUTP_V0_RELEASE    : return nvkm_uoutp_mthd_release    (outp, argv, argc);
283 	case NVIF_OUTP_V0_INFOFRAME  : return nvkm_uoutp_mthd_infoframe  (outp, argv, argc);
284 	case NVIF_OUTP_V0_HDA_ELD    : return nvkm_uoutp_mthd_hda_eld    (outp, argv, argc);
285 	case NVIF_OUTP_V0_DP_RETRAIN : return nvkm_uoutp_mthd_dp_retrain (outp, argv, argc);
286 	case NVIF_OUTP_V0_DP_MST_VCPI: return nvkm_uoutp_mthd_dp_mst_vcpi(outp, argv, argc);
287 	default:
288 		break;
289 	}
290 
291 	return -EINVAL;
292 }
293 
294 static int
295 nvkm_uoutp_mthd_noacquire(struct nvkm_outp *outp, u32 mthd, void *argv, u32 argc)
296 {
297 	switch (mthd) {
298 	case NVIF_OUTP_V0_LOAD_DETECT: return nvkm_uoutp_mthd_load_detect(outp, argv, argc);
299 	case NVIF_OUTP_V0_ACQUIRE    : return nvkm_uoutp_mthd_acquire    (outp, argv, argc);
300 	case NVIF_OUTP_V0_DP_AUX_PWR : return nvkm_uoutp_mthd_dp_aux_pwr (outp, argv, argc);
301 	default:
302 		break;
303 	}
304 
305 	return 1;
306 }
307 
308 static int
309 nvkm_uoutp_mthd(struct nvkm_object *object, u32 mthd, void *argv, u32 argc)
310 {
311 	struct nvkm_outp *outp = nvkm_uoutp(object);
312 	struct nvkm_disp *disp = outp->disp;
313 	int ret;
314 
315 	mutex_lock(&disp->super.mutex);
316 
317 	ret = nvkm_uoutp_mthd_noacquire(outp, mthd, argv, argc);
318 	if (ret <= 0)
319 		goto done;
320 
321 	if (outp->ior)
322 		ret = nvkm_uoutp_mthd_acquired(outp, mthd, argv, argc);
323 	else
324 		ret = -EIO;
325 
326 done:
327 	mutex_unlock(&disp->super.mutex);
328 	return ret;
329 }
330 
331 static void *
332 nvkm_uoutp_dtor(struct nvkm_object *object)
333 {
334 	struct nvkm_outp *outp = nvkm_uoutp(object);
335 	struct nvkm_disp *disp = outp->disp;
336 
337 	spin_lock(&disp->client.lock);
338 	outp->object.func = NULL;
339 	spin_unlock(&disp->client.lock);
340 	return NULL;
341 }
342 
343 static const struct nvkm_object_func
344 nvkm_uoutp = {
345 	.dtor = nvkm_uoutp_dtor,
346 	.mthd = nvkm_uoutp_mthd,
347 };
348 
349 int
350 nvkm_uoutp_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, struct nvkm_object **pobject)
351 {
352 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
353 	struct nvkm_outp *outt, *outp = NULL;
354 	union nvif_outp_args *args = argv;
355 	int ret;
356 
357 	if (argc != sizeof(args->v0) || args->v0.version != 0)
358 		return -ENOSYS;
359 
360 	list_for_each_entry(outt, &disp->outps, head) {
361 		if (outt->index == args->v0.id) {
362 			outp = outt;
363 			break;
364 		}
365 	}
366 
367 	if (!outp)
368 		return -EINVAL;
369 
370 	ret = -EBUSY;
371 	spin_lock(&disp->client.lock);
372 	if (!outp->object.func) {
373 		nvkm_object_ctor(&nvkm_uoutp, oclass, &outp->object);
374 		*pobject = &outp->object;
375 		ret = 0;
376 	}
377 	spin_unlock(&disp->client.lock);
378 	return ret;
379 }
380