1 /*
2  * Copyright 2012 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  * Authors: Ben Skeggs
23  */
24 #include "chan.h"
25 #include "head.h"
26 #include "ior.h"
27 #include "outp.h"
28 
29 #include <core/client.h>
30 
31 #include <nvif/class.h>
32 #include <nvif/cl5070.h>
33 #include <nvif/unpack.h>
34 
35 int
nv50_disp_root_mthd_(struct nvkm_object * object,u32 mthd,void * data,u32 size)36 nv50_disp_root_mthd_(struct nvkm_object *object, u32 mthd, void *data, u32 size)
37 {
38 	union {
39 		struct nv50_disp_mthd_v0 v0;
40 		struct nv50_disp_mthd_v1 v1;
41 	} *args = data;
42 	struct nvkm_disp *disp = nvkm_udisp(object);
43 	struct nvkm_outp *temp, *outp = NULL;
44 	struct nvkm_head *head;
45 	u16 type, mask = 0;
46 	int hidx, ret = -ENOSYS;
47 
48 	if (mthd != NV50_DISP_MTHD)
49 		return -EINVAL;
50 
51 	nvif_ioctl(object, "disp mthd size %d\n", size);
52 	if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
53 		nvif_ioctl(object, "disp mthd vers %d mthd %02x head %d\n",
54 			   args->v0.version, args->v0.method, args->v0.head);
55 		mthd = args->v0.method;
56 		hidx = args->v0.head;
57 	} else
58 	if (!(ret = nvif_unpack(ret, &data, &size, args->v1, 1, 1, true))) {
59 		nvif_ioctl(object, "disp mthd vers %d mthd %02x "
60 				   "type %04x mask %04x\n",
61 			   args->v1.version, args->v1.method,
62 			   args->v1.hasht, args->v1.hashm);
63 		mthd = args->v1.method;
64 		type = args->v1.hasht;
65 		mask = args->v1.hashm;
66 		hidx = ffs((mask >> 8) & 0x0f) - 1;
67 	} else
68 		return ret;
69 
70 	if (!(head = nvkm_head_find(disp, hidx)))
71 		return -ENXIO;
72 
73 	if (mask) {
74 		list_for_each_entry(temp, &disp->outps, head) {
75 			if ((temp->info.hasht         == type) &&
76 			    (temp->info.hashm & mask) == mask) {
77 				outp = temp;
78 				break;
79 			}
80 		}
81 		if (outp == NULL)
82 			return -ENXIO;
83 	}
84 
85 	switch (mthd) {
86 	case NV50_DISP_SCANOUTPOS: {
87 		return nvkm_head_mthd_scanoutpos(object, head, data, size);
88 	}
89 	default:
90 		break;
91 	}
92 
93 	switch (mthd * !!outp) {
94 	case NV50_DISP_MTHD_V1_ACQUIRE: {
95 		union {
96 			struct nv50_disp_acquire_v0 v0;
97 		} *args = data;
98 		int ret = -ENOSYS;
99 		if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
100 			ret = nvkm_outp_acquire(outp, NVKM_OUTP_USER, args->v0.hda);
101 			if (ret == 0) {
102 				args->v0.or = outp->ior->id;
103 				args->v0.link = outp->ior->asy.link;
104 			}
105 		}
106 		return ret;
107 	}
108 		break;
109 	case NV50_DISP_MTHD_V1_RELEASE:
110 		nvkm_outp_release(outp, NVKM_OUTP_USER);
111 		return 0;
112 	case NV50_DISP_MTHD_V1_SOR_HDA_ELD: {
113 		union {
114 			struct nv50_disp_sor_hda_eld_v0 v0;
115 		} *args = data;
116 		struct nvkm_ior *ior = outp->ior;
117 		int ret = -ENOSYS;
118 
119 		nvif_ioctl(object, "disp sor hda eld size %d\n", size);
120 		if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
121 			nvif_ioctl(object, "disp sor hda eld vers %d\n",
122 				   args->v0.version);
123 			if (size > 0x60)
124 				return -E2BIG;
125 		} else
126 			return ret;
127 
128 		if (!ior->hda)
129 			return -ENODEV;
130 
131 		if (size && args->v0.data[0]) {
132 			if (outp->info.type == DCB_OUTPUT_DP)
133 				ior->func->dp->audio(ior, hidx, true);
134 			ior->func->hda->hpd(ior, hidx, true);
135 			ior->func->hda->eld(ior, hidx, data, size);
136 		} else {
137 			if (outp->info.type == DCB_OUTPUT_DP)
138 				ior->func->dp->audio(ior, hidx, false);
139 			ior->func->hda->hpd(ior, hidx, false);
140 		}
141 
142 		return 0;
143 	}
144 		break;
145 	case NV50_DISP_MTHD_V1_SOR_HDMI_PWR: {
146 		union {
147 			struct nv50_disp_sor_hdmi_pwr_v0 v0;
148 		} *args = data;
149 		u8 *vendor, vendor_size;
150 		u8 *avi, avi_size;
151 		int ret = -ENOSYS;
152 
153 		nvif_ioctl(object, "disp sor hdmi ctrl size %d\n", size);
154 		if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
155 			nvif_ioctl(object, "disp sor hdmi ctrl vers %d state %d "
156 					   "max_ac_packet %d rekey %d scdc %d\n",
157 				   args->v0.version, args->v0.state,
158 				   args->v0.max_ac_packet, args->v0.rekey,
159 				   args->v0.scdc);
160 			if (args->v0.max_ac_packet > 0x1f || args->v0.rekey > 0x7f)
161 				return -EINVAL;
162 			if ((args->v0.avi_infoframe_length
163 			     + args->v0.vendor_infoframe_length) > size)
164 				return -EINVAL;
165 			else
166 			if ((args->v0.avi_infoframe_length
167 			     + args->v0.vendor_infoframe_length) < size)
168 				return -E2BIG;
169 			avi = data;
170 			avi_size = args->v0.avi_infoframe_length;
171 			vendor = avi + avi_size;
172 			vendor_size = args->v0.vendor_infoframe_length;
173 		} else
174 			return ret;
175 
176 		if (!outp->ior->func->hdmi.ctrl)
177 			return -ENODEV;
178 
179 		outp->ior->func->hdmi.ctrl(outp->ior, hidx, args->v0.state,
180 					   args->v0.max_ac_packet,
181 					   args->v0.rekey, avi, avi_size,
182 					   vendor, vendor_size);
183 
184 		if (outp->ior->func->hdmi.scdc)
185 			outp->ior->func->hdmi.scdc(outp->ior, args->v0.scdc);
186 
187 		return 0;
188 	}
189 		break;
190 	case NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT: {
191 		union {
192 			struct nv50_disp_sor_lvds_script_v0 v0;
193 		} *args = data;
194 		int ret = -ENOSYS;
195 		nvif_ioctl(object, "disp sor lvds script size %d\n", size);
196 		if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
197 			nvif_ioctl(object, "disp sor lvds script "
198 					   "vers %d name %04x\n",
199 				   args->v0.version, args->v0.script);
200 			disp->sor.lvdsconf = args->v0.script;
201 			return 0;
202 		} else
203 			return ret;
204 	}
205 		break;
206 	case NV50_DISP_MTHD_V1_SOR_DP_MST_LINK: {
207 		union {
208 			struct nv50_disp_sor_dp_mst_link_v0 v0;
209 		} *args = data;
210 		int ret = -ENOSYS;
211 		nvif_ioctl(object, "disp sor dp mst link size %d\n", size);
212 		if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
213 			nvif_ioctl(object, "disp sor dp mst link vers %d state %d\n",
214 				   args->v0.version, args->v0.state);
215 			outp->dp.lt.mst = !!args->v0.state;
216 			return 0;
217 		} else
218 			return ret;
219 	}
220 		break;
221 	case NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI: {
222 		union {
223 			struct nv50_disp_sor_dp_mst_vcpi_v0 v0;
224 		} *args = data;
225 		int ret = -ENOSYS;
226 		nvif_ioctl(object, "disp sor dp mst vcpi size %d\n", size);
227 		if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
228 			nvif_ioctl(object, "disp sor dp mst vcpi vers %d "
229 					   "slot %02x/%02x pbn %04x/%04x\n",
230 				   args->v0.version, args->v0.start_slot,
231 				   args->v0.num_slots, args->v0.pbn,
232 				   args->v0.aligned_pbn);
233 			if (!outp->ior->func->dp->vcpi)
234 				return -ENODEV;
235 			outp->ior->func->dp->vcpi(outp->ior, hidx,
236 						 args->v0.start_slot,
237 						 args->v0.num_slots,
238 						 args->v0.pbn,
239 						 args->v0.aligned_pbn);
240 			return 0;
241 		} else
242 			return ret;
243 	}
244 		break;
245 	default:
246 		break;
247 	}
248 
249 	return -EINVAL;
250 }
251