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 #include <nvif/outp.h>
23 #include <nvif/disp.h>
24 #include <nvif/printf.h>
25 
26 #include <nvif/class.h>
27 
28 int
nvif_outp_dp_mst_vcpi(struct nvif_outp * outp,int head,u8 start_slot,u8 num_slots,u16 pbn,u16 aligned_pbn)29 nvif_outp_dp_mst_vcpi(struct nvif_outp *outp, int head,
30 		      u8 start_slot, u8 num_slots, u16 pbn, u16 aligned_pbn)
31 {
32 	struct nvif_outp_dp_mst_vcpi_v0 args;
33 	int ret;
34 
35 	args.version = 0;
36 	args.head = head;
37 	args.start_slot = start_slot;
38 	args.num_slots = num_slots;
39 	args.pbn = pbn;
40 	args.aligned_pbn = aligned_pbn;
41 
42 	ret = nvif_object_mthd(&outp->object, NVIF_OUTP_V0_DP_MST_VCPI, &args, sizeof(args));
43 	NVIF_ERRON(ret, &outp->object,
44 		   "[DP_MST_VCPI head:%d start_slot:%02x num_slots:%02x pbn:%04x aligned_pbn:%04x]",
45 		   args.head, args.start_slot, args.num_slots, args.pbn, args.aligned_pbn);
46 	return ret;
47 }
48 
49 int
nvif_outp_dp_retrain(struct nvif_outp * outp)50 nvif_outp_dp_retrain(struct nvif_outp *outp)
51 {
52 	int ret = nvif_object_mthd(&outp->object, NVIF_OUTP_V0_DP_RETRAIN, NULL, 0);
53 	NVIF_ERRON(ret, &outp->object, "[DP_RETRAIN]");
54 	return ret;
55 }
56 
57 int
nvif_outp_dp_aux_pwr(struct nvif_outp * outp,bool enable)58 nvif_outp_dp_aux_pwr(struct nvif_outp *outp, bool enable)
59 {
60 	struct nvif_outp_dp_aux_pwr_v0 args;
61 	int ret;
62 
63 	args.version = 0;
64 	args.state = enable;
65 
66 	ret = nvif_object_mthd(&outp->object, NVIF_OUTP_V0_DP_AUX_PWR, &args, sizeof(args));
67 	NVIF_ERRON(ret, &outp->object, "[DP_AUX_PWR state:%d]", args.state);
68 	return ret;
69 }
70 
71 int
nvif_outp_hda_eld(struct nvif_outp * outp,int head,void * data,u32 size)72 nvif_outp_hda_eld(struct nvif_outp *outp, int head, void *data, u32 size)
73 {
74 	struct {
75 		struct nvif_outp_hda_eld_v0 mthd;
76 		u8 data[128];
77 	} args;
78 	int ret;
79 
80 	if (WARN_ON(size > ARRAY_SIZE(args.data)))
81 		return -EINVAL;
82 
83 	args.mthd.version = 0;
84 	args.mthd.head = head;
85 
86 	memcpy(args.data, data, size);
87 	ret = nvif_mthd(&outp->object, NVIF_OUTP_V0_HDA_ELD, &args, sizeof(args.mthd) + size);
88 	NVIF_ERRON(ret, &outp->object, "[HDA_ELD head:%d size:%d]", head, size);
89 	return ret;
90 }
91 
92 int
nvif_outp_infoframe(struct nvif_outp * outp,u8 type,struct nvif_outp_infoframe_v0 * args,u32 size)93 nvif_outp_infoframe(struct nvif_outp *outp, u8 type, struct nvif_outp_infoframe_v0 *args, u32 size)
94 {
95 	int ret;
96 
97 	args->type = type;
98 
99 	ret = nvif_mthd(&outp->object, NVIF_OUTP_V0_INFOFRAME, args, sizeof(*args) + size);
100 	NVIF_ERRON(ret, &outp->object, "[INFOFRAME type:%d size:%d]", type, size);
101 	return ret;
102 }
103 
104 void
nvif_outp_release(struct nvif_outp * outp)105 nvif_outp_release(struct nvif_outp *outp)
106 {
107 	int ret = nvif_mthd(&outp->object, NVIF_OUTP_V0_RELEASE, NULL, 0);
108 	NVIF_ERRON(ret, &outp->object, "[RELEASE]");
109 	outp->or.id = -1;
110 }
111 
112 static inline int
nvif_outp_acquire(struct nvif_outp * outp,u8 proto,struct nvif_outp_acquire_v0 * args)113 nvif_outp_acquire(struct nvif_outp *outp, u8 proto, struct nvif_outp_acquire_v0 *args)
114 {
115 	int ret;
116 
117 	args->version = 0;
118 	args->proto = proto;
119 
120 	ret = nvif_mthd(&outp->object, NVIF_OUTP_V0_ACQUIRE, args, sizeof(*args));
121 	if (ret)
122 		return ret;
123 
124 	outp->or.id = args->or;
125 	outp->or.link = args->link;
126 	return 0;
127 }
128 
129 int
nvif_outp_acquire_dp(struct nvif_outp * outp,u8 dpcd[DP_RECEIVER_CAP_SIZE],int link_nr,int link_bw,bool hda,bool mst)130 nvif_outp_acquire_dp(struct nvif_outp *outp, u8 dpcd[DP_RECEIVER_CAP_SIZE],
131 		     int link_nr, int link_bw, bool hda, bool mst)
132 {
133 	struct nvif_outp_acquire_v0 args;
134 	int ret;
135 
136 	args.dp.link_nr = link_nr;
137 	args.dp.link_bw = link_bw;
138 	args.dp.hda = hda;
139 	args.dp.mst = mst;
140 	memcpy(args.dp.dpcd, dpcd, sizeof(args.dp.dpcd));
141 
142 	ret = nvif_outp_acquire(outp, NVIF_OUTP_ACQUIRE_V0_DP, &args);
143 	NVIF_ERRON(ret, &outp->object,
144 		   "[ACQUIRE proto:DP link_nr:%d link_bw:%02x hda:%d mst:%d] or:%d link:%d",
145 		   args.dp.link_nr, args.dp.link_bw, args.dp.hda, args.dp.mst, args.or, args.link);
146 	return ret;
147 }
148 
149 int
nvif_outp_acquire_lvds(struct nvif_outp * outp,bool dual,bool bpc8)150 nvif_outp_acquire_lvds(struct nvif_outp *outp, bool dual, bool bpc8)
151 {
152 	struct nvif_outp_acquire_v0 args;
153 	int ret;
154 
155 	args.lvds.dual = dual;
156 	args.lvds.bpc8 = bpc8;
157 
158 	ret = nvif_outp_acquire(outp, NVIF_OUTP_ACQUIRE_V0_LVDS, &args);
159 	NVIF_ERRON(ret, &outp->object,
160 		   "[ACQUIRE proto:LVDS dual:%d 8bpc:%d] or:%d link:%d",
161 		   args.lvds.dual, args.lvds.bpc8, args.or, args.link);
162 	return ret;
163 }
164 
165 int
nvif_outp_acquire_tmds(struct nvif_outp * outp,int head,bool hdmi,u8 max_ac_packet,u8 rekey,u8 scdc,bool hda)166 nvif_outp_acquire_tmds(struct nvif_outp *outp, int head,
167 		       bool hdmi, u8 max_ac_packet, u8 rekey, u8 scdc, bool hda)
168 {
169 	struct nvif_outp_acquire_v0 args;
170 	int ret;
171 
172 	args.tmds.head = head;
173 	args.tmds.hdmi = hdmi;
174 	args.tmds.hdmi_max_ac_packet = max_ac_packet;
175 	args.tmds.hdmi_rekey = rekey;
176 	args.tmds.hdmi_scdc = scdc;
177 	args.tmds.hdmi_hda = hda;
178 
179 	ret = nvif_outp_acquire(outp, NVIF_OUTP_ACQUIRE_V0_TMDS, &args);
180 	NVIF_ERRON(ret, &outp->object,
181 		   "[ACQUIRE proto:TMDS head:%d hdmi:%d max_ac_packet:%d rekey:%d scdc:%d hda:%d]"
182 		   " or:%d link:%d", args.tmds.head, args.tmds.hdmi, args.tmds.hdmi_max_ac_packet,
183 		   args.tmds.hdmi_rekey, args.tmds.hdmi_scdc, args.tmds.hdmi_hda,
184 		   args.or, args.link);
185 	return ret;
186 }
187 
188 int
nvif_outp_acquire_rgb_crt(struct nvif_outp * outp)189 nvif_outp_acquire_rgb_crt(struct nvif_outp *outp)
190 {
191 	struct nvif_outp_acquire_v0 args;
192 	int ret;
193 
194 	ret = nvif_outp_acquire(outp, NVIF_OUTP_ACQUIRE_V0_RGB_CRT, &args);
195 	NVIF_ERRON(ret, &outp->object, "[ACQUIRE proto:RGB_CRT] or:%d", args.or);
196 	return ret;
197 }
198 
199 int
nvif_outp_load_detect(struct nvif_outp * outp,u32 loadval)200 nvif_outp_load_detect(struct nvif_outp *outp, u32 loadval)
201 {
202 	struct nvif_outp_load_detect_v0 args;
203 	int ret;
204 
205 	args.version = 0;
206 	args.data = loadval;
207 
208 	ret = nvif_mthd(&outp->object, NVIF_OUTP_V0_LOAD_DETECT, &args, sizeof(args));
209 	NVIF_ERRON(ret, &outp->object, "[LOAD_DETECT data:%08x] load:%02x", args.data, args.load);
210 	return ret < 0 ? ret : args.load;
211 }
212 
213 void
nvif_outp_dtor(struct nvif_outp * outp)214 nvif_outp_dtor(struct nvif_outp *outp)
215 {
216 	nvif_object_dtor(&outp->object);
217 }
218 
219 int
nvif_outp_ctor(struct nvif_disp * disp,const char * name,int id,struct nvif_outp * outp)220 nvif_outp_ctor(struct nvif_disp *disp, const char *name, int id, struct nvif_outp *outp)
221 {
222 	struct nvif_outp_v0 args;
223 	int ret;
224 
225 	args.version = 0;
226 	args.id = id;
227 
228 	ret = nvif_object_ctor(&disp->object, name ?: "nvifOutp", id, NVIF_CLASS_OUTP,
229 			       &args, sizeof(args), &outp->object);
230 	NVIF_ERRON(ret, &disp->object, "[NEW outp id:%d]", id);
231 	if (ret)
232 		return ret;
233 
234 	outp->or.id = -1;
235 	return 0;
236 }
237