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/ramht.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
nvkm_disp_vblank_fini(struct nvkm_event * event,int type,int id)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
nvkm_disp_vblank_init(struct nvkm_event * event,int type,int id)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
nvkm_disp_vblank_ctor(struct nvkm_object * object,void * data,u32 size,struct nvkm_notify * notify)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
nvkm_disp_vblank(struct nvkm_disp * disp,int head)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
nvkm_disp_hpd_ctor(struct nvkm_object * object,void * data,u32 size,struct nvkm_notify * notify)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->outps, 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
nvkm_disp_ntfy(struct nvkm_object * object,u32 type,struct nvkm_event ** event)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 int
nvkm_disp_class_new(struct nvkm_device * device,const struct nvkm_oclass * oclass,void * data,u32 size,struct nvkm_object ** pobject)149 nvkm_disp_class_new(struct nvkm_device *device,
150 const struct nvkm_oclass *oclass, void *data, u32 size,
151 struct nvkm_object **pobject)
152 {
153 return nvkm_udisp_new(oclass, data, size, pobject);
154 }
155
156 static const struct nvkm_device_oclass
157 nvkm_disp_sclass = {
158 .ctor = nvkm_disp_class_new,
159 };
160
161 static int
nvkm_disp_class_get(struct nvkm_oclass * oclass,int index,const struct nvkm_device_oclass ** class)162 nvkm_disp_class_get(struct nvkm_oclass *oclass, int index,
163 const struct nvkm_device_oclass **class)
164 {
165 struct nvkm_disp *disp = nvkm_disp(oclass->engine);
166 if (index == 0) {
167 oclass->base = disp->func->root;
168 *class = &nvkm_disp_sclass;
169 return 0;
170 }
171 return 1;
172 }
173
174 static void
nvkm_disp_intr(struct nvkm_engine * engine)175 nvkm_disp_intr(struct nvkm_engine *engine)
176 {
177 struct nvkm_disp *disp = nvkm_disp(engine);
178 disp->func->intr(disp);
179 }
180
181 static int
nvkm_disp_fini(struct nvkm_engine * engine,bool suspend)182 nvkm_disp_fini(struct nvkm_engine *engine, bool suspend)
183 {
184 struct nvkm_disp *disp = nvkm_disp(engine);
185 struct nvkm_conn *conn;
186 struct nvkm_outp *outp;
187
188 if (disp->func->fini)
189 disp->func->fini(disp);
190
191 list_for_each_entry(outp, &disp->outps, head) {
192 nvkm_outp_fini(outp);
193 }
194
195 list_for_each_entry(conn, &disp->conns, head) {
196 nvkm_conn_fini(conn);
197 }
198
199 return 0;
200 }
201
202 static int
nvkm_disp_init(struct nvkm_engine * engine)203 nvkm_disp_init(struct nvkm_engine *engine)
204 {
205 struct nvkm_disp *disp = nvkm_disp(engine);
206 struct nvkm_conn *conn;
207 struct nvkm_outp *outp;
208 struct nvkm_ior *ior;
209
210 list_for_each_entry(conn, &disp->conns, head) {
211 nvkm_conn_init(conn);
212 }
213
214 list_for_each_entry(outp, &disp->outps, head) {
215 nvkm_outp_init(outp);
216 }
217
218 if (disp->func->init) {
219 int ret = disp->func->init(disp);
220 if (ret)
221 return ret;
222 }
223
224 /* Set 'normal' (ie. when it's attached to a head) state for
225 * each output resource to 'fully enabled'.
226 */
227 list_for_each_entry(ior, &disp->iors, head) {
228 ior->func->power(ior, true, true, true, true, true);
229 }
230
231 return 0;
232 }
233
234 static int
nvkm_disp_oneinit(struct nvkm_engine * engine)235 nvkm_disp_oneinit(struct nvkm_engine *engine)
236 {
237 struct nvkm_disp *disp = nvkm_disp(engine);
238 struct nvkm_subdev *subdev = &disp->engine.subdev;
239 struct nvkm_bios *bios = subdev->device->bios;
240 struct nvkm_outp *outp, *outt, *pair;
241 struct nvkm_conn *conn;
242 struct nvkm_head *head;
243 struct nvkm_ior *ior;
244 struct nvbios_connE connE;
245 struct dcb_output dcbE;
246 u8 hpd = 0, ver, hdr;
247 u32 data;
248 int ret, i;
249
250 /* Create output path objects for each VBIOS display path. */
251 i = -1;
252 while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE))) {
253 if (ver < 0x40) /* No support for chipsets prior to NV50. */
254 break;
255 if (dcbE.type == DCB_OUTPUT_UNUSED)
256 continue;
257 if (dcbE.type == DCB_OUTPUT_EOL)
258 break;
259 outp = NULL;
260
261 switch (dcbE.type) {
262 case DCB_OUTPUT_ANALOG:
263 case DCB_OUTPUT_TV:
264 case DCB_OUTPUT_TMDS:
265 case DCB_OUTPUT_LVDS:
266 ret = nvkm_outp_new(disp, i, &dcbE, &outp);
267 break;
268 case DCB_OUTPUT_DP:
269 ret = nvkm_dp_new(disp, i, &dcbE, &outp);
270 break;
271 case DCB_OUTPUT_WFD:
272 /* No support for WFD yet. */
273 ret = -ENODEV;
274 continue;
275 default:
276 nvkm_warn(subdev, "dcb %d type %d unknown\n",
277 i, dcbE.type);
278 continue;
279 }
280
281 if (ret) {
282 if (outp) {
283 if (ret != -ENODEV)
284 OUTP_ERR(outp, "ctor failed: %d", ret);
285 else
286 OUTP_DBG(outp, "not supported");
287 nvkm_outp_del(&outp);
288 continue;
289 }
290 nvkm_error(subdev, "failed to create outp %d\n", i);
291 continue;
292 }
293
294 list_add_tail(&outp->head, &disp->outps);
295 hpd = max(hpd, (u8)(dcbE.connector + 1));
296 }
297
298 /* Create connector objects based on available output paths. */
299 list_for_each_entry_safe(outp, outt, &disp->outps, head) {
300 /* VBIOS data *should* give us the most useful information. */
301 data = nvbios_connEp(bios, outp->info.connector, &ver, &hdr,
302 &connE);
303
304 /* No bios connector data... */
305 if (!data) {
306 /* Heuristic: anything with the same ccb index is
307 * considered to be on the same connector, any
308 * output path without an associated ccb entry will
309 * be put on its own connector.
310 */
311 int ccb_index = outp->info.i2c_index;
312 if (ccb_index != 0xf) {
313 list_for_each_entry(pair, &disp->outps, head) {
314 if (pair->info.i2c_index == ccb_index) {
315 outp->conn = pair->conn;
316 break;
317 }
318 }
319 }
320
321 /* Connector shared with another output path. */
322 if (outp->conn)
323 continue;
324
325 memset(&connE, 0x00, sizeof(connE));
326 connE.type = DCB_CONNECTOR_NONE;
327 i = -1;
328 } else {
329 i = outp->info.connector;
330 }
331
332 /* Check that we haven't already created this connector. */
333 list_for_each_entry(conn, &disp->conns, head) {
334 if (conn->index == outp->info.connector) {
335 outp->conn = conn;
336 break;
337 }
338 }
339
340 if (outp->conn)
341 continue;
342
343 /* Apparently we need to create a new one! */
344 ret = nvkm_conn_new(disp, i, &connE, &outp->conn);
345 if (ret) {
346 nvkm_error(&disp->engine.subdev,
347 "failed to create outp %d conn: %d\n",
348 outp->index, ret);
349 nvkm_conn_del(&outp->conn);
350 list_del(&outp->head);
351 nvkm_outp_del(&outp);
352 continue;
353 }
354
355 list_add_tail(&outp->conn->head, &disp->conns);
356 }
357
358 ret = nvkm_event_init(&nvkm_disp_hpd_func, 3, hpd, &disp->hpd);
359 if (ret)
360 return ret;
361
362 if (disp->func->oneinit) {
363 ret = disp->func->oneinit(disp);
364 if (ret)
365 return ret;
366 }
367
368 /* Enforce identity-mapped SOR assignment for panels, which have
369 * certain bits (ie. backlight controls) wired to a specific SOR.
370 */
371 list_for_each_entry(outp, &disp->outps, head) {
372 if (outp->conn->info.type == DCB_CONNECTOR_LVDS ||
373 outp->conn->info.type == DCB_CONNECTOR_eDP) {
374 ior = nvkm_ior_find(disp, SOR, ffs(outp->info.or) - 1);
375 if (!WARN_ON(!ior))
376 ior->identity = true;
377 outp->identity = true;
378 }
379 }
380
381 i = 0;
382 list_for_each_entry(head, &disp->heads, head)
383 i = max(i, head->id + 1);
384
385 return nvkm_event_init(&nvkm_disp_vblank_func, 1, i, &disp->vblank);
386 }
387
388 static void *
nvkm_disp_dtor(struct nvkm_engine * engine)389 nvkm_disp_dtor(struct nvkm_engine *engine)
390 {
391 struct nvkm_disp *disp = nvkm_disp(engine);
392 struct nvkm_conn *conn;
393 struct nvkm_outp *outp;
394 struct nvkm_ior *ior;
395 struct nvkm_head *head;
396 void *data = disp;
397
398 nvkm_ramht_del(&disp->ramht);
399 nvkm_gpuobj_del(&disp->inst);
400
401 nvkm_event_fini(&disp->uevent);
402
403 if (disp->super.wq) {
404 destroy_workqueue(disp->super.wq);
405 mutex_destroy(&disp->super.mutex);
406 }
407
408 nvkm_event_fini(&disp->vblank);
409 nvkm_event_fini(&disp->hpd);
410
411 while (!list_empty(&disp->conns)) {
412 conn = list_first_entry(&disp->conns, typeof(*conn), head);
413 list_del(&conn->head);
414 nvkm_conn_del(&conn);
415 }
416
417 while (!list_empty(&disp->outps)) {
418 outp = list_first_entry(&disp->outps, typeof(*outp), head);
419 list_del(&outp->head);
420 nvkm_outp_del(&outp);
421 }
422
423 while (!list_empty(&disp->iors)) {
424 ior = list_first_entry(&disp->iors, typeof(*ior), head);
425 nvkm_ior_del(&ior);
426 }
427
428 while (!list_empty(&disp->heads)) {
429 head = list_first_entry(&disp->heads, typeof(*head), head);
430 nvkm_head_del(&head);
431 }
432
433 return data;
434 }
435
436 static const struct nvkm_engine_func
437 nvkm_disp = {
438 .dtor = nvkm_disp_dtor,
439 .oneinit = nvkm_disp_oneinit,
440 .init = nvkm_disp_init,
441 .fini = nvkm_disp_fini,
442 .intr = nvkm_disp_intr,
443 .base.sclass = nvkm_disp_class_get,
444 };
445
446 int
nvkm_disp_new_(const struct nvkm_disp_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_disp ** pdisp)447 nvkm_disp_new_(const struct nvkm_disp_func *func, struct nvkm_device *device,
448 enum nvkm_subdev_type type, int inst, struct nvkm_disp **pdisp)
449 {
450 struct nvkm_disp *disp;
451 int ret;
452
453 if (!(disp = *pdisp = kzalloc(sizeof(**pdisp), GFP_KERNEL)))
454 return -ENOMEM;
455
456 disp->func = func;
457 INIT_LIST_HEAD(&disp->heads);
458 INIT_LIST_HEAD(&disp->iors);
459 INIT_LIST_HEAD(&disp->outps);
460 INIT_LIST_HEAD(&disp->conns);
461 spin_lock_init(&disp->client.lock);
462
463 ret = nvkm_engine_ctor(&nvkm_disp, device, type, inst, true, &disp->engine);
464 if (ret)
465 return ret;
466
467 if (func->super) {
468 disp->super.wq = create_singlethread_workqueue("nvkm-disp");
469 if (!disp->super.wq)
470 return -ENOMEM;
471
472 INIT_WORK(&disp->super.work, func->super);
473 mutex_init(&disp->super.mutex);
474 }
475
476 return nvkm_event_init(func->uevent, 1, ARRAY_SIZE(disp->chan), &disp->uevent);
477 }
478