base.c 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2013 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
24 25
#include "priv.h"
#include "conn.h"
26
#include "head.h"
27 28
#include "outp.h"

29
#include <core/client.h>
30
#include <core/notify.h>
31
#include <core/oproxy.h>
32 33
#include <subdev/bios.h>
#include <subdev/bios/dcb.h>
34

35
#include <nvif/class.h>
36
#include <nvif/cl0046.h>
37
#include <nvif/event.h>
38
#include <nvif/unpack.h>
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static void
nvkm_disp_vblank_fini(struct nvkm_event *event, int type, int head)
{
	struct nvkm_disp *disp = container_of(event, typeof(*disp), vblank);
	disp->func->head.vblank_fini(disp, head);
}

static void
nvkm_disp_vblank_init(struct nvkm_event *event, int type, int head)
{
	struct nvkm_disp *disp = container_of(event, typeof(*disp), vblank);
	disp->func->head.vblank_init(disp, head);
}

static int
55 56
nvkm_disp_vblank_ctor(struct nvkm_object *object, void *data, u32 size,
		      struct nvkm_notify *notify)
57
{
58
	struct nvkm_disp *disp =
59 60 61 62
		container_of(notify->event, typeof(*disp), vblank);
	union {
		struct nvif_notify_head_req_v0 v0;
	} *req = data;
63
	int ret = -ENOSYS;
64

65
	if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, false))) {
66 67 68 69 70 71 72 73 74 75 76
		notify->size = sizeof(struct nvif_notify_head_rep_v0);
		if (ret = -ENXIO, req->v0.head <= disp->vblank.index_nr) {
			notify->types = 1;
			notify->index = req->v0.head;
			return 0;
		}
	}

	return ret;
}

77 78 79 80 81 82 83
static const struct nvkm_event_func
nvkm_disp_vblank_func = {
	.ctor = nvkm_disp_vblank_ctor,
	.init = nvkm_disp_vblank_init,
	.fini = nvkm_disp_vblank_fini,
};

84
void
85
nvkm_disp_vblank(struct nvkm_disp *disp, int head)
86 87 88 89 90
{
	struct nvif_notify_head_rep_v0 rep = {};
	nvkm_event_send(&disp->vblank, 1, head, &rep, sizeof(rep));
}

91
static int
92 93
nvkm_disp_hpd_ctor(struct nvkm_object *object, void *data, u32 size,
		   struct nvkm_notify *notify)
94
{
95
	struct nvkm_disp *disp =
96 97 98 99
		container_of(notify->event, typeof(*disp), hpd);
	union {
		struct nvif_notify_conn_req_v0 v0;
	} *req = data;
100
	struct nvkm_outp *outp;
101
	int ret = -ENOSYS;
102

103
	if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, false))) {
104 105 106 107 108 109 110 111 112 113
		notify->size = sizeof(struct nvif_notify_conn_rep_v0);
		list_for_each_entry(outp, &disp->outp, head) {
			if (ret = -ENXIO, outp->conn->index == req->v0.conn) {
				if (ret = -ENODEV, outp->conn->hpd.event) {
					notify->types = req->v0.mask;
					notify->index = req->v0.conn;
					ret = 0;
				}
				break;
			}
114 115
		}
	}
116 117

	return ret;
118
}
119

120
static const struct nvkm_event_func
121 122
nvkm_disp_hpd_func = {
	.ctor = nvkm_disp_hpd_ctor
123 124
};

125
int
126
nvkm_disp_ntfy(struct nvkm_object *object, u32 type, struct nvkm_event **event)
127
{
128
	struct nvkm_disp *disp = nvkm_disp(object->engine);
129 130 131 132 133 134 135 136 137 138 139 140 141
	switch (type) {
	case NV04_DISP_NTFY_VBLANK:
		*event = &disp->vblank;
		return 0;
	case NV04_DISP_NTFY_CONN:
		*event = &disp->hpd;
		return 0;
	default:
		break;
	}
	return -EINVAL;
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static void
nvkm_disp_class_del(struct nvkm_oproxy *oproxy)
{
	struct nvkm_disp *disp = nvkm_disp(oproxy->base.engine);
	mutex_lock(&disp->engine.subdev.mutex);
	if (disp->client == oproxy)
		disp->client = NULL;
	mutex_unlock(&disp->engine.subdev.mutex);
}

static const struct nvkm_oproxy_func
nvkm_disp_class = {
	.dtor[1] = nvkm_disp_class_del,
};

static int
nvkm_disp_class_new(struct nvkm_device *device,
		    const struct nvkm_oclass *oclass, void *data, u32 size,
		    struct nvkm_object **pobject)
{
	const struct nvkm_disp_oclass *sclass = oclass->engn;
	struct nvkm_disp *disp = nvkm_disp(oclass->engine);
	struct nvkm_oproxy *oproxy;
	int ret;

	ret = nvkm_oproxy_new_(&nvkm_disp_class, oclass, &oproxy);
	if (ret)
		return ret;
	*pobject = &oproxy->base;

	mutex_lock(&disp->engine.subdev.mutex);
	if (disp->client) {
		mutex_unlock(&disp->engine.subdev.mutex);
		return -EBUSY;
	}
	disp->client = oproxy;
	mutex_unlock(&disp->engine.subdev.mutex);

	return sclass->ctor(disp, oclass, data, size, &oproxy->object);
}

static const struct nvkm_device_oclass
nvkm_disp_sclass = {
	.ctor = nvkm_disp_class_new,
};

static int
nvkm_disp_class_get(struct nvkm_oclass *oclass, int index,
		    const struct nvkm_device_oclass **class)
{
	struct nvkm_disp *disp = nvkm_disp(oclass->engine);
	if (index == 0) {
194 195 196
		const struct nvkm_disp_oclass *root = disp->func->root(disp);
		oclass->base = root->base;
		oclass->engn = root;
197 198 199 200 201 202
		*class = &nvkm_disp_sclass;
		return 0;
	}
	return 1;
}

203 204 205 206 207 208 209 210 211
static void
nvkm_disp_intr(struct nvkm_engine *engine)
{
	struct nvkm_disp *disp = nvkm_disp(engine);
	disp->func->intr(disp);
}

static int
nvkm_disp_fini(struct nvkm_engine *engine, bool suspend)
212
{
213
	struct nvkm_disp *disp = nvkm_disp(engine);
214
	struct nvkm_conn *conn;
215
	struct nvkm_outp *outp;
216 217

	list_for_each_entry(outp, &disp->outp, head) {
218
		nvkm_outp_fini(outp);
219 220
	}

221
	list_for_each_entry(conn, &disp->conn, head) {
222
		nvkm_conn_fini(conn);
223 224
	}

225
	return 0;
226 227
}

228 229
static int
nvkm_disp_init(struct nvkm_engine *engine)
230
{
231
	struct nvkm_disp *disp = nvkm_disp(engine);
232
	struct nvkm_conn *conn;
233
	struct nvkm_outp *outp;
234

235
	list_for_each_entry(conn, &disp->conn, head) {
236
		nvkm_conn_init(conn);
237 238
	}

239
	list_for_each_entry(outp, &disp->outp, head) {
240
		nvkm_outp_init(outp);
241 242
	}

243
	return 0;
244
}
245

246 247
static int
nvkm_disp_oneinit(struct nvkm_engine *engine)
248
{
249
	struct nvkm_disp *disp = nvkm_disp(engine);
250
	struct nvkm_bios *bios = disp->engine.subdev.device->bios;
251
	struct nvkm_outp *outp, *outt, *pair;
252
	struct nvkm_conn *conn;
253
	struct nvkm_head *head;
254
	struct nvbios_connE connE;
255 256 257 258
	struct dcb_output dcbE;
	u8  hpd = 0, ver, hdr;
	u32 data;
	int ret, i;
259

260
	/* Create output path objects for each VBIOS display path. */
261 262
	i = -1;
	while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE))) {
263 264
		const struct nvkm_disp_func_outp *outps;
		int (*ctor)(struct nvkm_disp *, int, struct dcb_output *,
265
			    struct nvkm_outp **);
266

267 268 269 270
		if (dcbE.type == DCB_OUTPUT_UNUSED)
			continue;
		if (dcbE.type == DCB_OUTPUT_EOL)
			break;
271 272 273
		outp = NULL;

		switch (dcbE.location) {
274 275
		case 0: outps = &disp->func->outp.internal; break;
		case 1: outps = &disp->func->outp.external; break;
276 277 278 279 280
		default:
			nvkm_warn(&disp->engine.subdev,
				  "dcb %d locn %d unknown\n", i, dcbE.location);
			continue;
		}
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
		switch (dcbE.type) {
		case DCB_OUTPUT_ANALOG: ctor = outps->crt ; break;
		case DCB_OUTPUT_TV    : ctor = outps->tv  ; break;
		case DCB_OUTPUT_TMDS  : ctor = outps->tmds; break;
		case DCB_OUTPUT_LVDS  : ctor = outps->lvds; break;
		case DCB_OUTPUT_DP    : ctor = outps->dp  ; break;
		default:
			nvkm_warn(&disp->engine.subdev,
				  "dcb %d type %d unknown\n", i, dcbE.type);
			continue;
		}

		if (ctor)
			ret = ctor(disp, i, &dcbE, &outp);
		else
			ret = -ENODEV;

		if (ret) {
			if (ret == -ENODEV) {
				nvkm_debug(&disp->engine.subdev,
					   "dcb %d %d/%d not supported\n",
					   i, dcbE.location, dcbE.type);
				continue;
305
			}
306
			nvkm_error(&disp->engine.subdev,
307
				   "failed to create outp %d\n", i);
308
			nvkm_outp_del(&outp);
309
			continue;
310 311
		}

312
		list_add_tail(&outp->head, &disp->outp);
313 314 315
		hpd = max(hpd, (u8)(dcbE.connector + 1));
	}

316
	/* Create connector objects based on available output paths. */
317
	list_for_each_entry_safe(outp, outt, &disp->outp, head) {
318
		/* VBIOS data *should* give us the most useful information. */
319 320 321
		data = nvbios_connEp(bios, outp->info.connector, &ver, &hdr,
				     &connE);

322
		/* No bios connector data... */
323
		if (!data) {
324
			/* Heuristic: anything with the same ccb index is
325 326
			 * considered to be on the same connector, any
			 * output path without an associated ccb entry will
327
			 * be put on its own connector.
328 329 330 331 332 333 334 335 336 337 338
			 */
			int ccb_index = outp->info.i2c_index;
			if (ccb_index != 0xf) {
				list_for_each_entry(pair, &disp->outp, head) {
					if (pair->info.i2c_index == ccb_index) {
						outp->conn = pair->conn;
						break;
					}
				}
			}

339
			/* Connector shared with another output path. */
340 341 342 343 344 345 346 347 348 349
			if (outp->conn)
				continue;

			memset(&connE, 0x00, sizeof(connE));
			connE.type = DCB_CONNECTOR_NONE;
			i = -1;
		} else {
			i = outp->info.connector;
		}

350
		/* Check that we haven't already created this connector. */
351 352 353 354 355 356 357 358 359 360
		list_for_each_entry(conn, &disp->conn, head) {
			if (conn->index == outp->info.connector) {
				outp->conn = conn;
				break;
			}
		}

		if (outp->conn)
			continue;

361
		/* Apparently we need to create a new one! */
362
		ret = nvkm_conn_new(disp, i, &connE, &outp->conn);
363 364
		if (ret) {
			nvkm_error(&disp->engine.subdev,
365
				   "failed to create outp %d conn: %d\n",
366
				   outp->index, ret);
367
			nvkm_conn_del(&outp->conn);
368
			list_del(&outp->head);
369
			nvkm_outp_del(&outp);
370 371 372 373 374 375
			continue;
		}

		list_add_tail(&outp->conn->head, &disp->conn);
	}

376
	ret = nvkm_event_init(&nvkm_disp_hpd_func, 3, hpd, &disp->hpd);
377 378 379
	if (ret)
		return ret;

380 381 382 383 384
	i = 0;
	list_for_each_entry(head, &disp->head, head)
		i = max(i, head->id + 1);

	return nvkm_event_init(&nvkm_disp_vblank_func, 1, i, &disp->vblank);
385
}
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static void *
nvkm_disp_dtor(struct nvkm_engine *engine)
{
	struct nvkm_disp *disp = nvkm_disp(engine);
	struct nvkm_conn *conn;
	struct nvkm_outp *outp;
	void *data = disp;

	if (disp->func->dtor)
		data = disp->func->dtor(disp);

	nvkm_event_fini(&disp->vblank);
	nvkm_event_fini(&disp->hpd);

	while (!list_empty(&disp->conn)) {
		conn = list_first_entry(&disp->conn, typeof(*conn), head);
		list_del(&conn->head);
		nvkm_conn_del(&conn);
	}

	while (!list_empty(&disp->outp)) {
		outp = list_first_entry(&disp->outp, typeof(*outp), head);
		list_del(&outp->head);
		nvkm_outp_del(&outp);
	}

413 414 415 416 417 418
	while (!list_empty(&disp->head)) {
		struct nvkm_head *head =
			list_first_entry(&disp->head, typeof(*head), head);
		nvkm_head_del(&head);
	}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	return data;
}

static const struct nvkm_engine_func
nvkm_disp = {
	.dtor = nvkm_disp_dtor,
	.oneinit = nvkm_disp_oneinit,
	.init = nvkm_disp_init,
	.fini = nvkm_disp_fini,
	.intr = nvkm_disp_intr,
	.base.sclass = nvkm_disp_class_get,
};

int
nvkm_disp_ctor(const struct nvkm_disp_func *func, struct nvkm_device *device,
434
	       int index, struct nvkm_disp *disp)
435 436
{
	disp->func = func;
437
	INIT_LIST_HEAD(&disp->head);
438 439 440
	INIT_LIST_HEAD(&disp->outp);
	INIT_LIST_HEAD(&disp->conn);
	return nvkm_engine_ctor(&nvkm_disp, device, index, true, &disp->engine);
441
}
442 443 444

int
nvkm_disp_new_(const struct nvkm_disp_func *func, struct nvkm_device *device,
445
	       int index, struct nvkm_disp **pdisp)
446 447 448
{
	if (!(*pdisp = kzalloc(sizeof(**pdisp), GFP_KERNEL)))
		return -ENOMEM;
449
	return nvkm_disp_ctor(func, device, index, *pdisp);
450
}