aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/nouveau/nvkm/engine/disp/base.c
blob: 23d1b5c0dc1625aae2a5fbf0bb359b2c0a440a12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * 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
 */
#include "priv.h"
#include "conn.h"
#include "outp.h"

#include <core/notify.h>
#include <subdev/bios.h>
#include <subdev/bios/dcb.h>

#include <nvif/class.h>
#include <nvif/event.h>
#include <nvif/unpack.h>

int
nvkm_disp_vblank_ctor(struct nvkm_object *object, void *data, u32 size,
		      struct nvkm_notify *notify)
{
	struct nvkm_disp *disp =
		container_of(notify->event, typeof(*disp), vblank);
	union {
		struct nvif_notify_head_req_v0 v0;
	} *req = data;
	int ret;

	if (nvif_unpack(req->v0, 0, 0, false)) {
		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;
}

void
nvkm_disp_vblank(struct nvkm_disp *disp, int head)
{
	struct nvif_notify_head_rep_v0 rep = {};
	nvkm_event_send(&disp->vblank, 1, head, &rep, sizeof(rep));
}

static int
nvkm_disp_hpd_ctor(struct nvkm_object *object, void *data, u32 size,
		   struct nvkm_notify *notify)
{
	struct nvkm_disp *disp =
		container_of(notify->event, typeof(*disp), hpd);
	union {
		struct nvif_notify_conn_req_v0 v0;
	} *req = data;
	struct nvkm_output *outp;
	int ret;

	if (nvif_unpack(req->v0, 0, 0, false)) {
		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;
			}
		}
	}

	return ret;
}

static const struct nvkm_event_func
nvkm_disp_hpd_func = {
	.ctor = nvkm_disp_hpd_ctor
};

int
nvkm_disp_ntfy(struct nvkm_object *object, u32 type, struct nvkm_event **event)
{
	struct nvkm_disp *disp = (void *)object->engine;
	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;
}

int
_nvkm_disp_fini(struct nvkm_object *object, bool suspend)
{
	struct nvkm_disp *disp = (void *)object;
	struct nvkm_output *outp;
	int ret;

	list_for_each_entry(outp, &disp->outp, head) {
		ret = nv_ofuncs(outp)->fini(nv_object(outp), suspend);
		if (ret && suspend)
			goto fail_outp;
	}

	return nvkm_engine_fini(&disp->base, suspend);

fail_outp:
	list_for_each_entry_continue_reverse(outp, &disp->outp, head) {
		nv_ofuncs(outp)->init(nv_object(outp));
	}

	return ret;
}

int
_nvkm_disp_init(struct nvkm_object *object)
{
	struct nvkm_disp *disp = (void *)object;
	struct nvkm_output *outp;
	int ret;

	ret = nvkm_engine_init(&disp->base);
	if (ret)
		return ret;

	list_for_each_entry(outp, &disp->outp, head) {
		ret = nv_ofuncs(outp)->init(nv_object(outp));
		if (ret)
			goto fail_outp;
	}

	return ret;

fail_outp:
	list_for_each_entry_continue_reverse(outp, &disp->outp, head) {
		nv_ofuncs(outp)->fini(nv_object(outp), false);
	}

	return ret;
}

void
_nvkm_disp_dtor(struct nvkm_object *object)
{
	struct nvkm_disp *disp = (void *)object;
	struct nvkm_output *outp, *outt;

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

	if (disp->outp.next) {
		list_for_each_entry_safe(outp, outt, &disp->outp, head) {
			nvkm_object_ref(NULL, (struct nvkm_object **)&outp);
		}
	}

	nvkm_engine_destroy(&disp->base);
}

int
nvkm_disp_create_(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, int heads, const char *intname,
		  const char *extname, int length, void **pobject)
{
	struct nvkm_disp_impl *impl = (void *)oclass;
	struct nvkm_bios *bios = nvkm_bios(parent);
	struct nvkm_disp *disp;
	struct nvkm_oclass **sclass;
	struct nvkm_object *object;
	struct dcb_output dcbE;
	u8  hpd = 0, ver, hdr;
	u32 data;
	int ret, i;

	ret = nvkm_engine_create_(parent, engine, oclass, true, intname,
				  extname, length, pobject);
	disp = *pobject;
	if (ret)
		return ret;

	INIT_LIST_HEAD(&disp->outp);

	/* create output objects for each display path in the vbios */
	i = -1;
	while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE))) {
		if (dcbE.type == DCB_OUTPUT_UNUSED)
			continue;
		if (dcbE.type == DCB_OUTPUT_EOL)
			break;
		data = dcbE.location << 4 | dcbE.type;

		oclass = nvkm_output_oclass;
		sclass = impl->outp;
		while (sclass && sclass[0]) {
			if (sclass[0]->handle == data) {
				oclass = sclass[0];
				break;
			}
			sclass++;
		}

		nvkm_object_ctor(*pobject, NULL, oclass, &dcbE, i, &object);
		hpd = max(hpd, (u8)(dcbE.connector + 1));
	}

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

	ret = nvkm_event_init(impl->vblank, 1, heads, &disp->vblank);
	if (ret)
		return ret;

	return 0;
}