base.c 11.0 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 "dp.h"
27
#include "head.h"
28
#include "ior.h"
29 30
#include "outp.h"

31
#include <core/client.h>
32
#include <core/notify.h>
33
#include <core/oproxy.h>
34 35
#include <subdev/bios.h>
#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 47 48
	struct nvkm_head *head = nvkm_head_find(disp, id);
	if (head)
		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 56 57
	struct nvkm_head *head = nvkm_head_find(disp, id);
	if (head)
		head->func->vblank_get(head);
58 59 60
}

static int
61 62
nvkm_disp_vblank_ctor(struct nvkm_object *object, void *data, u32 size,
		      struct nvkm_notify *notify)
63
{
64
	struct nvkm_disp *disp =
65 66 67 68
		container_of(notify->event, typeof(*disp), vblank);
	union {
		struct nvif_notify_head_req_v0 v0;
	} *req = data;
69
	int ret = -ENOSYS;
70

71
	if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, false))) {
72 73 74 75 76 77 78 79 80 81 82
		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;
}

83 84 85 86 87 88 89
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,
};

90
void
91
nvkm_disp_vblank(struct nvkm_disp *disp, int head)
92 93 94 95 96
{
	struct nvif_notify_head_rep_v0 rep = {};
	nvkm_event_send(&disp->vblank, 1, head, &rep, sizeof(rep));
}

97
static int
98 99
nvkm_disp_hpd_ctor(struct nvkm_object *object, void *data, u32 size,
		   struct nvkm_notify *notify)
100
{
101
	struct nvkm_disp *disp =
102 103 104 105
		container_of(notify->event, typeof(*disp), hpd);
	union {
		struct nvif_notify_conn_req_v0 v0;
	} *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 111 112 113 114 115 116 117 118 119
		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;
			}
120 121
		}
	}
122 123

	return ret;
124
}
125

126
static const struct nvkm_event_func
127 128
nvkm_disp_hpd_func = {
	.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 136 137 138 139 140 141 142 143 144 145 146 147
	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;
}

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 194 195 196 197 198 199
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) {
200 201 202
		const struct nvkm_disp_oclass *root = disp->func->root(disp);
		oclass->base = root->base;
		oclass->engn = root;
203 204 205 206 207 208
		*class = &nvkm_disp_sclass;
		return 0;
	}
	return 1;
}

209 210 211 212 213 214 215 216 217
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)
218
{
219
	struct nvkm_disp *disp = nvkm_disp(engine);
220
	struct nvkm_conn *conn;
221
	struct nvkm_outp *outp;
222 223

	list_for_each_entry(outp, &disp->outp, head) {
224
		nvkm_outp_fini(outp);
225 226
	}

227
	list_for_each_entry(conn, &disp->conn, head) {
228
		nvkm_conn_fini(conn);
229 230
	}

231
	return 0;
232 233
}

234 235
static int
nvkm_disp_init(struct nvkm_engine *engine)
236
{
237
	struct nvkm_disp *disp = nvkm_disp(engine);
238
	struct nvkm_conn *conn;
239
	struct nvkm_outp *outp;
240

241
	list_for_each_entry(conn, &disp->conn, head) {
242
		nvkm_conn_init(conn);
243 244
	}

245
	list_for_each_entry(outp, &disp->outp, head) {
246
		nvkm_outp_init(outp);
247 248
	}

249
	return 0;
250
}
251

252 253
static int
nvkm_disp_oneinit(struct nvkm_engine *engine)
254
{
255
	struct nvkm_disp *disp = nvkm_disp(engine);
256 257
	struct nvkm_subdev *subdev = &disp->engine.subdev;
	struct nvkm_bios *bios = subdev->device->bios;
258
	struct nvkm_outp *outp, *outt, *pair;
259
	struct nvkm_conn *conn;
260
	struct nvkm_head *head;
261
	struct nvbios_connE connE;
262 263 264 265
	struct dcb_output dcbE;
	u8  hpd = 0, ver, hdr;
	u32 data;
	int ret, i;
266

267
	/* Create output path objects for each VBIOS display path. */
268 269
	i = -1;
	while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE))) {
270 271
		if (ver < 0x40) /* No support for chipsets prior to NV50. */
			break;
272 273 274 275
		if (dcbE.type == DCB_OUTPUT_UNUSED)
			continue;
		if (dcbE.type == DCB_OUTPUT_EOL)
			break;
276 277 278
		outp = NULL;

		switch (dcbE.type) {
279 280 281 282 283 284 285 286 287
		case DCB_OUTPUT_ANALOG:
		case DCB_OUTPUT_TV:
		case DCB_OUTPUT_TMDS:
		case DCB_OUTPUT_LVDS:
			ret = nvkm_outp_new(disp, i, &dcbE, &outp);
			break;
		case DCB_OUTPUT_DP:
			ret = nvkm_dp_new(disp, i, &dcbE, &outp);
			break;
288
		default:
289 290
			nvkm_warn(subdev, "dcb %d type %d unknown\n",
				  i, dcbE.type);
291 292 293 294
			continue;
		}

		if (ret) {
295 296 297 298 299 300
			if (outp) {
				if (ret != -ENODEV)
					OUTP_ERR(outp, "ctor failed: %d", ret);
				else
					OUTP_DBG(outp, "not supported");
				nvkm_outp_del(&outp);
301
				continue;
302
			}
303
			nvkm_error(subdev, "failed to create outp %d\n", i);
304
			continue;
305 306
		}

307
		list_add_tail(&outp->head, &disp->outp);
308 309 310
		hpd = max(hpd, (u8)(dcbE.connector + 1));
	}

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

317
		/* No bios connector data... */
318
		if (!data) {
319
			/* Heuristic: anything with the same ccb index is
320 321
			 * considered to be on the same connector, any
			 * output path without an associated ccb entry will
322
			 * be put on its own connector.
323 324 325 326 327 328 329 330 331 332 333
			 */
			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;
					}
				}
			}

334
			/* Connector shared with another output path. */
335 336 337 338 339 340 341 342 343 344
			if (outp->conn)
				continue;

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

345
		/* Check that we haven't already created this connector. */
346 347 348 349 350 351 352 353 354 355
		list_for_each_entry(conn, &disp->conn, head) {
			if (conn->index == outp->info.connector) {
				outp->conn = conn;
				break;
			}
		}

		if (outp->conn)
			continue;

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

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

371
	ret = nvkm_event_init(&nvkm_disp_hpd_func, 3, hpd, &disp->hpd);
372 373 374
	if (ret)
		return ret;

375 376 377 378 379
	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);
380
}
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
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);
	}

408 409 410 411 412 413
	while (!list_empty(&disp->ior)) {
		struct nvkm_ior *ior =
			list_first_entry(&disp->ior, typeof(*ior), head);
		nvkm_ior_del(&ior);
	}

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

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	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,
435
	       int index, struct nvkm_disp *disp)
436 437
{
	disp->func = func;
438
	INIT_LIST_HEAD(&disp->head);
439
	INIT_LIST_HEAD(&disp->ior);
440 441 442
	INIT_LIST_HEAD(&disp->outp);
	INIT_LIST_HEAD(&disp->conn);
	return nvkm_engine_ctor(&nvkm_disp, device, index, true, &disp->engine);
443
}
444 445 446

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