1 /*
2  * Copyright 2013 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 "priv.h"
25 #include "conn.h"
26 #include "dp.h"
27 #include "head.h"
28 #include "ior.h"
29 #include "outp.h"
30 
31 #include <core/client.h>
32 #include <core/notify.h>
33 #include <core/oproxy.h>
34 #include <subdev/bios.h>
35 #include <subdev/bios/dcb.h>
36 
37 #include <nvif/class.h>
38 #include <nvif/cl0046.h>
39 #include <nvif/event.h>
40 #include <nvif/unpack.h>
41 
42 static void
43 nvkm_disp_vblank_fini(struct nvkm_event *event, int type, int id)
44 {
45 	struct nvkm_disp *disp = container_of(event, typeof(*disp), vblank);
46 	struct nvkm_head *head = nvkm_head_find(disp, id);
47 	if (head)
48 		head->func->vblank_put(head);
49 }
50 
51 static void
52 nvkm_disp_vblank_init(struct nvkm_event *event, int type, int id)
53 {
54 	struct nvkm_disp *disp = container_of(event, typeof(*disp), vblank);
55 	struct nvkm_head *head = nvkm_head_find(disp, id);
56 	if (head)
57 		head->func->vblank_get(head);
58 }
59 
60 static int
61 nvkm_disp_vblank_ctor(struct nvkm_object *object, void *data, u32 size,
62 		      struct nvkm_notify *notify)
63 {
64 	struct nvkm_disp *disp =
65 		container_of(notify->event, typeof(*disp), vblank);
66 	union {
67 		struct nvif_notify_head_req_v0 v0;
68 	} *req = data;
69 	int ret = -ENOSYS;
70 
71 	if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, false))) {
72 		notify->size = sizeof(struct nvif_notify_head_rep_v0);
73 		if (ret = -ENXIO, req->v0.head <= disp->vblank.index_nr) {
74 			notify->types = 1;
75 			notify->index = req->v0.head;
76 			return 0;
77 		}
78 	}
79 
80 	return ret;
81 }
82 
83 static const struct nvkm_event_func
84 nvkm_disp_vblank_func = {
85 	.ctor = nvkm_disp_vblank_ctor,
86 	.init = nvkm_disp_vblank_init,
87 	.fini = nvkm_disp_vblank_fini,
88 };
89 
90 void
91 nvkm_disp_vblank(struct nvkm_disp *disp, int head)
92 {
93 	struct nvif_notify_head_rep_v0 rep = {};
94 	nvkm_event_send(&disp->vblank, 1, head, &rep, sizeof(rep));
95 }
96 
97 static int
98 nvkm_disp_hpd_ctor(struct nvkm_object *object, void *data, u32 size,
99 		   struct nvkm_notify *notify)
100 {
101 	struct nvkm_disp *disp =
102 		container_of(notify->event, typeof(*disp), hpd);
103 	union {
104 		struct nvif_notify_conn_req_v0 v0;
105 	} *req = data;
106 	struct nvkm_outp *outp;
107 	int ret = -ENOSYS;
108 
109 	if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, false))) {
110 		notify->size = sizeof(struct nvif_notify_conn_rep_v0);
111 		list_for_each_entry(outp, &disp->outp, head) {
112 			if (ret = -ENXIO, outp->conn->index == req->v0.conn) {
113 				if (ret = -ENODEV, outp->conn->hpd.event) {
114 					notify->types = req->v0.mask;
115 					notify->index = req->v0.conn;
116 					ret = 0;
117 				}
118 				break;
119 			}
120 		}
121 	}
122 
123 	return ret;
124 }
125 
126 static const struct nvkm_event_func
127 nvkm_disp_hpd_func = {
128 	.ctor = nvkm_disp_hpd_ctor
129 };
130 
131 int
132 nvkm_disp_ntfy(struct nvkm_object *object, u32 type, struct nvkm_event **event)
133 {
134 	struct nvkm_disp *disp = nvkm_disp(object->engine);
135 	switch (type) {
136 	case NV04_DISP_NTFY_VBLANK:
137 		*event = &disp->vblank;
138 		return 0;
139 	case NV04_DISP_NTFY_CONN:
140 		*event = &disp->hpd;
141 		return 0;
142 	default:
143 		break;
144 	}
145 	return -EINVAL;
146 }
147 
148 static void
149 nvkm_disp_class_del(struct nvkm_oproxy *oproxy)
150 {
151 	struct nvkm_disp *disp = nvkm_disp(oproxy->base.engine);
152 	mutex_lock(&disp->engine.subdev.mutex);
153 	if (disp->client == oproxy)
154 		disp->client = NULL;
155 	mutex_unlock(&disp->engine.subdev.mutex);
156 }
157 
158 static const struct nvkm_oproxy_func
159 nvkm_disp_class = {
160 	.dtor[1] = nvkm_disp_class_del,
161 };
162 
163 static int
164 nvkm_disp_class_new(struct nvkm_device *device,
165 		    const struct nvkm_oclass *oclass, void *data, u32 size,
166 		    struct nvkm_object **pobject)
167 {
168 	const struct nvkm_disp_oclass *sclass = oclass->engn;
169 	struct nvkm_disp *disp = nvkm_disp(oclass->engine);
170 	struct nvkm_oproxy *oproxy;
171 	int ret;
172 
173 	ret = nvkm_oproxy_new_(&nvkm_disp_class, oclass, &oproxy);
174 	if (ret)
175 		return ret;
176 	*pobject = &oproxy->base;
177 
178 	mutex_lock(&disp->engine.subdev.mutex);
179 	if (disp->client) {
180 		mutex_unlock(&disp->engine.subdev.mutex);
181 		return -EBUSY;
182 	}
183 	disp->client = oproxy;
184 	mutex_unlock(&disp->engine.subdev.mutex);
185 
186 	return sclass->ctor(disp, oclass, data, size, &oproxy->object);
187 }
188 
189 static const struct nvkm_device_oclass
190 nvkm_disp_sclass = {
191 	.ctor = nvkm_disp_class_new,
192 };
193 
194 static int
195 nvkm_disp_class_get(struct nvkm_oclass *oclass, int index,
196 		    const struct nvkm_device_oclass **class)
197 {
198 	struct nvkm_disp *disp = nvkm_disp(oclass->engine);
199 	if (index == 0) {
200 		const struct nvkm_disp_oclass *root = disp->func->root(disp);
201 		oclass->base = root->base;
202 		oclass->engn = root;
203 		*class = &nvkm_disp_sclass;
204 		return 0;
205 	}
206 	return 1;
207 }
208 
209 static void
210 nvkm_disp_intr(struct nvkm_engine *engine)
211 {
212 	struct nvkm_disp *disp = nvkm_disp(engine);
213 	disp->func->intr(disp);
214 }
215 
216 static int
217 nvkm_disp_fini(struct nvkm_engine *engine, bool suspend)
218 {
219 	struct nvkm_disp *disp = nvkm_disp(engine);
220 	struct nvkm_conn *conn;
221 	struct nvkm_outp *outp;
222 
223 	if (disp->func->fini)
224 		disp->func->fini(disp);
225 
226 	list_for_each_entry(outp, &disp->outp, head) {
227 		nvkm_outp_fini(outp);
228 	}
229 
230 	list_for_each_entry(conn, &disp->conn, head) {
231 		nvkm_conn_fini(conn);
232 	}
233 
234 	return 0;
235 }
236 
237 static int
238 nvkm_disp_init(struct nvkm_engine *engine)
239 {
240 	struct nvkm_disp *disp = nvkm_disp(engine);
241 	struct nvkm_conn *conn;
242 	struct nvkm_outp *outp;
243 	struct nvkm_ior *ior;
244 
245 	list_for_each_entry(conn, &disp->conn, head) {
246 		nvkm_conn_init(conn);
247 	}
248 
249 	list_for_each_entry(outp, &disp->outp, head) {
250 		nvkm_outp_init(outp);
251 	}
252 
253 	if (disp->func->init) {
254 		int ret = disp->func->init(disp);
255 		if (ret)
256 			return ret;
257 	}
258 
259 	/* Set 'normal' (ie. when it's attached to a head) state for
260 	 * each output resource to 'fully enabled'.
261 	 */
262 	list_for_each_entry(ior, &disp->ior, head) {
263 		ior->func->power(ior, true, true, true, true, true);
264 	}
265 
266 	return 0;
267 }
268 
269 static int
270 nvkm_disp_oneinit(struct nvkm_engine *engine)
271 {
272 	struct nvkm_disp *disp = nvkm_disp(engine);
273 	struct nvkm_subdev *subdev = &disp->engine.subdev;
274 	struct nvkm_bios *bios = subdev->device->bios;
275 	struct nvkm_outp *outp, *outt, *pair;
276 	struct nvkm_conn *conn;
277 	struct nvkm_head *head;
278 	struct nvbios_connE connE;
279 	struct dcb_output dcbE;
280 	u8  hpd = 0, ver, hdr;
281 	u32 data;
282 	int ret, i;
283 
284 	/* Create output path objects for each VBIOS display path. */
285 	i = -1;
286 	while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE))) {
287 		if (ver < 0x40) /* No support for chipsets prior to NV50. */
288 			break;
289 		if (dcbE.type == DCB_OUTPUT_UNUSED)
290 			continue;
291 		if (dcbE.type == DCB_OUTPUT_EOL)
292 			break;
293 		outp = NULL;
294 
295 		switch (dcbE.type) {
296 		case DCB_OUTPUT_ANALOG:
297 		case DCB_OUTPUT_TV:
298 		case DCB_OUTPUT_TMDS:
299 		case DCB_OUTPUT_LVDS:
300 			ret = nvkm_outp_new(disp, i, &dcbE, &outp);
301 			break;
302 		case DCB_OUTPUT_DP:
303 			ret = nvkm_dp_new(disp, i, &dcbE, &outp);
304 			break;
305 		case DCB_OUTPUT_WFD:
306 			/* No support for WFD yet. */
307 			ret = -ENODEV;
308 			continue;
309 		default:
310 			nvkm_warn(subdev, "dcb %d type %d unknown\n",
311 				  i, dcbE.type);
312 			continue;
313 		}
314 
315 		if (ret) {
316 			if (outp) {
317 				if (ret != -ENODEV)
318 					OUTP_ERR(outp, "ctor failed: %d", ret);
319 				else
320 					OUTP_DBG(outp, "not supported");
321 				nvkm_outp_del(&outp);
322 				continue;
323 			}
324 			nvkm_error(subdev, "failed to create outp %d\n", i);
325 			continue;
326 		}
327 
328 		list_add_tail(&outp->head, &disp->outp);
329 		hpd = max(hpd, (u8)(dcbE.connector + 1));
330 	}
331 
332 	/* Create connector objects based on available output paths. */
333 	list_for_each_entry_safe(outp, outt, &disp->outp, head) {
334 		/* VBIOS data *should* give us the most useful information. */
335 		data = nvbios_connEp(bios, outp->info.connector, &ver, &hdr,
336 				     &connE);
337 
338 		/* No bios connector data... */
339 		if (!data) {
340 			/* Heuristic: anything with the same ccb index is
341 			 * considered to be on the same connector, any
342 			 * output path without an associated ccb entry will
343 			 * be put on its own connector.
344 			 */
345 			int ccb_index = outp->info.i2c_index;
346 			if (ccb_index != 0xf) {
347 				list_for_each_entry(pair, &disp->outp, head) {
348 					if (pair->info.i2c_index == ccb_index) {
349 						outp->conn = pair->conn;
350 						break;
351 					}
352 				}
353 			}
354 
355 			/* Connector shared with another output path. */
356 			if (outp->conn)
357 				continue;
358 
359 			memset(&connE, 0x00, sizeof(connE));
360 			connE.type = DCB_CONNECTOR_NONE;
361 			i = -1;
362 		} else {
363 			i = outp->info.connector;
364 		}
365 
366 		/* Check that we haven't already created this connector. */
367 		list_for_each_entry(conn, &disp->conn, head) {
368 			if (conn->index == outp->info.connector) {
369 				outp->conn = conn;
370 				break;
371 			}
372 		}
373 
374 		if (outp->conn)
375 			continue;
376 
377 		/* Apparently we need to create a new one! */
378 		ret = nvkm_conn_new(disp, i, &connE, &outp->conn);
379 		if (ret) {
380 			nvkm_error(&disp->engine.subdev,
381 				   "failed to create outp %d conn: %d\n",
382 				   outp->index, ret);
383 			nvkm_conn_del(&outp->conn);
384 			list_del(&outp->head);
385 			nvkm_outp_del(&outp);
386 			continue;
387 		}
388 
389 		list_add_tail(&outp->conn->head, &disp->conn);
390 	}
391 
392 	ret = nvkm_event_init(&nvkm_disp_hpd_func, 3, hpd, &disp->hpd);
393 	if (ret)
394 		return ret;
395 
396 	if (disp->func->oneinit) {
397 		ret = disp->func->oneinit(disp);
398 		if (ret)
399 			return ret;
400 	}
401 
402 	i = 0;
403 	list_for_each_entry(head, &disp->head, head)
404 		i = max(i, head->id + 1);
405 
406 	return nvkm_event_init(&nvkm_disp_vblank_func, 1, i, &disp->vblank);
407 }
408 
409 static void *
410 nvkm_disp_dtor(struct nvkm_engine *engine)
411 {
412 	struct nvkm_disp *disp = nvkm_disp(engine);
413 	struct nvkm_conn *conn;
414 	struct nvkm_outp *outp;
415 	void *data = disp;
416 
417 	if (disp->func->dtor)
418 		data = disp->func->dtor(disp);
419 
420 	nvkm_event_fini(&disp->vblank);
421 	nvkm_event_fini(&disp->hpd);
422 
423 	while (!list_empty(&disp->conn)) {
424 		conn = list_first_entry(&disp->conn, typeof(*conn), head);
425 		list_del(&conn->head);
426 		nvkm_conn_del(&conn);
427 	}
428 
429 	while (!list_empty(&disp->outp)) {
430 		outp = list_first_entry(&disp->outp, typeof(*outp), head);
431 		list_del(&outp->head);
432 		nvkm_outp_del(&outp);
433 	}
434 
435 	while (!list_empty(&disp->ior)) {
436 		struct nvkm_ior *ior =
437 			list_first_entry(&disp->ior, typeof(*ior), head);
438 		nvkm_ior_del(&ior);
439 	}
440 
441 	while (!list_empty(&disp->head)) {
442 		struct nvkm_head *head =
443 			list_first_entry(&disp->head, typeof(*head), head);
444 		nvkm_head_del(&head);
445 	}
446 
447 	return data;
448 }
449 
450 static const struct nvkm_engine_func
451 nvkm_disp = {
452 	.dtor = nvkm_disp_dtor,
453 	.oneinit = nvkm_disp_oneinit,
454 	.init = nvkm_disp_init,
455 	.fini = nvkm_disp_fini,
456 	.intr = nvkm_disp_intr,
457 	.base.sclass = nvkm_disp_class_get,
458 };
459 
460 int
461 nvkm_disp_ctor(const struct nvkm_disp_func *func, struct nvkm_device *device,
462 	       int index, struct nvkm_disp *disp)
463 {
464 	disp->func = func;
465 	INIT_LIST_HEAD(&disp->head);
466 	INIT_LIST_HEAD(&disp->ior);
467 	INIT_LIST_HEAD(&disp->outp);
468 	INIT_LIST_HEAD(&disp->conn);
469 	return nvkm_engine_ctor(&nvkm_disp, device, index, true, &disp->engine);
470 }
471 
472 int
473 nvkm_disp_new_(const struct nvkm_disp_func *func, struct nvkm_device *device,
474 	       int index, struct nvkm_disp **pdisp)
475 {
476 	if (!(*pdisp = kzalloc(sizeof(**pdisp), GFP_KERNEL)))
477 		return -ENOMEM;
478 	return nvkm_disp_ctor(func, device, index, *pdisp);
479 }
480