]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - drivers/gpu/drm/nouveau/dispnv50/disp.c
Merge tag 'drm-misc-next-2023-09-11-1' of git://anongit.freedesktop.org/drm/drm-misc...
[thirdparty/kernel/linux.git] / drivers / gpu / drm / nouveau / dispnv50 / disp.c
1 /*
2 * Copyright 2011 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 "disp.h"
25 #include "atom.h"
26 #include "core.h"
27 #include "head.h"
28 #include "wndw.h"
29 #include "handles.h"
30
31 #include <linux/dma-mapping.h>
32 #include <linux/hdmi.h>
33 #include <linux/component.h>
34 #include <linux/iopoll.h>
35
36 #include <drm/display/drm_dp_helper.h>
37 #include <drm/display/drm_scdc_helper.h>
38 #include <drm/drm_atomic.h>
39 #include <drm/drm_atomic_helper.h>
40 #include <drm/drm_edid.h>
41 #include <drm/drm_fb_helper.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/drm_vblank.h>
44
45 #include <nvif/push507c.h>
46
47 #include <nvif/class.h>
48 #include <nvif/cl0002.h>
49 #include <nvif/event.h>
50 #include <nvif/if0012.h>
51 #include <nvif/if0014.h>
52 #include <nvif/timer.h>
53
54 #include <nvhw/class/cl507c.h>
55 #include <nvhw/class/cl507d.h>
56 #include <nvhw/class/cl837d.h>
57 #include <nvhw/class/cl887d.h>
58 #include <nvhw/class/cl907d.h>
59 #include <nvhw/class/cl917d.h>
60
61 #include "nouveau_drv.h"
62 #include "nouveau_dma.h"
63 #include "nouveau_gem.h"
64 #include "nouveau_connector.h"
65 #include "nouveau_encoder.h"
66 #include "nouveau_fence.h"
67 #include "nv50_display.h"
68
69 #include <subdev/bios/dp.h>
70
71 /******************************************************************************
72 * EVO channel
73 *****************************************************************************/
74
75 static int
76 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
77 const s32 *oclass, u8 head, void *data, u32 size,
78 struct nv50_chan *chan)
79 {
80 struct nvif_sclass *sclass;
81 int ret, i, n;
82
83 chan->device = device;
84
85 ret = n = nvif_object_sclass_get(disp, &sclass);
86 if (ret < 0)
87 return ret;
88
89 while (oclass[0]) {
90 for (i = 0; i < n; i++) {
91 if (sclass[i].oclass == oclass[0]) {
92 ret = nvif_object_ctor(disp, "kmsChan", 0,
93 oclass[0], data, size,
94 &chan->user);
95 if (ret == 0)
96 nvif_object_map(&chan->user, NULL, 0);
97 nvif_object_sclass_put(&sclass);
98 return ret;
99 }
100 }
101 oclass++;
102 }
103
104 nvif_object_sclass_put(&sclass);
105 return -ENOSYS;
106 }
107
108 static void
109 nv50_chan_destroy(struct nv50_chan *chan)
110 {
111 nvif_object_dtor(&chan->user);
112 }
113
114 /******************************************************************************
115 * DMA EVO channel
116 *****************************************************************************/
117
118 void
119 nv50_dmac_destroy(struct nv50_dmac *dmac)
120 {
121 nvif_object_dtor(&dmac->vram);
122 nvif_object_dtor(&dmac->sync);
123
124 nv50_chan_destroy(&dmac->base);
125
126 nvif_mem_dtor(&dmac->_push.mem);
127 }
128
129 static void
130 nv50_dmac_kick(struct nvif_push *push)
131 {
132 struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
133
134 dmac->cur = push->cur - (u32 __iomem *)dmac->_push.mem.object.map.ptr;
135 if (dmac->put != dmac->cur) {
136 /* Push buffer fetches are not coherent with BAR1, we need to ensure
137 * writes have been flushed right through to VRAM before writing PUT.
138 */
139 if (dmac->push->mem.type & NVIF_MEM_VRAM) {
140 struct nvif_device *device = dmac->base.device;
141 nvif_wr32(&device->object, 0x070000, 0x00000001);
142 nvif_msec(device, 2000,
143 if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
144 break;
145 );
146 }
147
148 NVIF_WV32(&dmac->base.user, NV507C, PUT, PTR, dmac->cur);
149 dmac->put = dmac->cur;
150 }
151
152 push->bgn = push->cur;
153 }
154
155 static int
156 nv50_dmac_free(struct nv50_dmac *dmac)
157 {
158 u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
159 if (get > dmac->cur) /* NVIDIA stay 5 away from GET, do the same. */
160 return get - dmac->cur - 5;
161 return dmac->max - dmac->cur;
162 }
163
164 static int
165 nv50_dmac_wind(struct nv50_dmac *dmac)
166 {
167 /* Wait for GET to depart from the beginning of the push buffer to
168 * prevent writing PUT == GET, which would be ignored by HW.
169 */
170 u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
171 if (get == 0) {
172 /* Corner-case, HW idle, but non-committed work pending. */
173 if (dmac->put == 0)
174 nv50_dmac_kick(dmac->push);
175
176 if (nvif_msec(dmac->base.device, 2000,
177 if (NVIF_TV32(&dmac->base.user, NV507C, GET, PTR, >, 0))
178 break;
179 ) < 0)
180 return -ETIMEDOUT;
181 }
182
183 PUSH_RSVD(dmac->push, PUSH_JUMP(dmac->push, 0));
184 dmac->cur = 0;
185 return 0;
186 }
187
188 static int
189 nv50_dmac_wait(struct nvif_push *push, u32 size)
190 {
191 struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
192 int free;
193
194 if (WARN_ON(size > dmac->max))
195 return -EINVAL;
196
197 dmac->cur = push->cur - (u32 __iomem *)dmac->_push.mem.object.map.ptr;
198 if (dmac->cur + size >= dmac->max) {
199 int ret = nv50_dmac_wind(dmac);
200 if (ret)
201 return ret;
202
203 push->cur = dmac->_push.mem.object.map.ptr;
204 push->cur = push->cur + dmac->cur;
205 nv50_dmac_kick(push);
206 }
207
208 if (nvif_msec(dmac->base.device, 2000,
209 if ((free = nv50_dmac_free(dmac)) >= size)
210 break;
211 ) < 0) {
212 WARN_ON(1);
213 return -ETIMEDOUT;
214 }
215
216 push->bgn = dmac->_push.mem.object.map.ptr;
217 push->bgn = push->bgn + dmac->cur;
218 push->cur = push->bgn;
219 push->end = push->cur + free;
220 return 0;
221 }
222
223 MODULE_PARM_DESC(kms_vram_pushbuf, "Place EVO/NVD push buffers in VRAM (default: auto)");
224 static int nv50_dmac_vram_pushbuf = -1;
225 module_param_named(kms_vram_pushbuf, nv50_dmac_vram_pushbuf, int, 0400);
226
227 int
228 nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
229 const s32 *oclass, u8 head, void *data, u32 size, s64 syncbuf,
230 struct nv50_dmac *dmac)
231 {
232 struct nouveau_cli *cli = (void *)device->object.client;
233 struct nvif_disp_chan_v0 *args = data;
234 u8 type = NVIF_MEM_COHERENT;
235 int ret;
236
237 mutex_init(&dmac->lock);
238
239 /* Pascal added support for 47-bit physical addresses, but some
240 * parts of EVO still only accept 40-bit PAs.
241 *
242 * To avoid issues on systems with large amounts of RAM, and on
243 * systems where an IOMMU maps pages at a high address, we need
244 * to allocate push buffers in VRAM instead.
245 *
246 * This appears to match NVIDIA's behaviour on Pascal.
247 */
248 if ((nv50_dmac_vram_pushbuf > 0) ||
249 (nv50_dmac_vram_pushbuf < 0 && device->info.family == NV_DEVICE_INFO_V0_PASCAL))
250 type |= NVIF_MEM_VRAM;
251
252 ret = nvif_mem_ctor_map(&cli->mmu, "kmsChanPush", type, 0x1000,
253 &dmac->_push.mem);
254 if (ret)
255 return ret;
256
257 dmac->ptr = dmac->_push.mem.object.map.ptr;
258 dmac->_push.wait = nv50_dmac_wait;
259 dmac->_push.kick = nv50_dmac_kick;
260 dmac->push = &dmac->_push;
261 dmac->push->bgn = dmac->_push.mem.object.map.ptr;
262 dmac->push->cur = dmac->push->bgn;
263 dmac->push->end = dmac->push->bgn;
264 dmac->max = 0x1000/4 - 1;
265
266 /* EVO channels are affected by a HW bug where the last 12 DWORDs
267 * of the push buffer aren't able to be used safely.
268 */
269 if (disp->oclass < GV100_DISP)
270 dmac->max -= 12;
271
272 args->pushbuf = nvif_handle(&dmac->_push.mem.object);
273
274 ret = nv50_chan_create(device, disp, oclass, head, data, size,
275 &dmac->base);
276 if (ret)
277 return ret;
278
279 if (syncbuf < 0)
280 return 0;
281
282 ret = nvif_object_ctor(&dmac->base.user, "kmsSyncCtxDma", NV50_DISP_HANDLE_SYNCBUF,
283 NV_DMA_IN_MEMORY,
284 &(struct nv_dma_v0) {
285 .target = NV_DMA_V0_TARGET_VRAM,
286 .access = NV_DMA_V0_ACCESS_RDWR,
287 .start = syncbuf + 0x0000,
288 .limit = syncbuf + 0x0fff,
289 }, sizeof(struct nv_dma_v0),
290 &dmac->sync);
291 if (ret)
292 return ret;
293
294 ret = nvif_object_ctor(&dmac->base.user, "kmsVramCtxDma", NV50_DISP_HANDLE_VRAM,
295 NV_DMA_IN_MEMORY,
296 &(struct nv_dma_v0) {
297 .target = NV_DMA_V0_TARGET_VRAM,
298 .access = NV_DMA_V0_ACCESS_RDWR,
299 .start = 0,
300 .limit = device->info.ram_user - 1,
301 }, sizeof(struct nv_dma_v0),
302 &dmac->vram);
303 if (ret)
304 return ret;
305
306 return ret;
307 }
308
309 /******************************************************************************
310 * Output path helpers
311 *****************************************************************************/
312 static void
313 nv50_outp_dump_caps(struct nouveau_drm *drm,
314 struct nouveau_encoder *outp)
315 {
316 NV_DEBUG(drm, "%s caps: dp_interlace=%d\n",
317 outp->base.base.name, outp->caps.dp_interlace);
318 }
319
320 static int
321 nv50_outp_atomic_check_view(struct drm_encoder *encoder,
322 struct drm_crtc_state *crtc_state,
323 struct drm_connector_state *conn_state,
324 struct drm_display_mode *native_mode)
325 {
326 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
327 struct drm_display_mode *mode = &crtc_state->mode;
328 struct drm_connector *connector = conn_state->connector;
329 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
330 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
331
332 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
333 asyc->scaler.full = false;
334 if (!native_mode)
335 return 0;
336
337 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
338 switch (connector->connector_type) {
339 case DRM_MODE_CONNECTOR_LVDS:
340 case DRM_MODE_CONNECTOR_eDP:
341 /* Don't force scaler for EDID modes with
342 * same size as the native one (e.g. different
343 * refresh rate)
344 */
345 if (mode->hdisplay == native_mode->hdisplay &&
346 mode->vdisplay == native_mode->vdisplay &&
347 mode->type & DRM_MODE_TYPE_DRIVER)
348 break;
349 mode = native_mode;
350 asyc->scaler.full = true;
351 break;
352 default:
353 break;
354 }
355 } else {
356 mode = native_mode;
357 }
358
359 if (!drm_mode_equal(adjusted_mode, mode)) {
360 drm_mode_copy(adjusted_mode, mode);
361 crtc_state->mode_changed = true;
362 }
363
364 return 0;
365 }
366
367 static void
368 nv50_outp_atomic_fix_depth(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state)
369 {
370 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
371 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
372 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
373 unsigned int max_rate, mode_rate;
374
375 switch (nv_encoder->dcb->type) {
376 case DCB_OUTPUT_DP:
377 max_rate = nv_encoder->dp.link_nr * nv_encoder->dp.link_bw;
378
379 /* we don't support more than 10 anyway */
380 asyh->or.bpc = min_t(u8, asyh->or.bpc, 10);
381
382 /* reduce the bpc until it works out */
383 while (asyh->or.bpc > 6) {
384 mode_rate = DIV_ROUND_UP(mode->clock * asyh->or.bpc * 3, 8);
385 if (mode_rate <= max_rate)
386 break;
387
388 asyh->or.bpc -= 2;
389 }
390 break;
391 default:
392 break;
393 }
394 }
395
396 static int
397 nv50_outp_atomic_check(struct drm_encoder *encoder,
398 struct drm_crtc_state *crtc_state,
399 struct drm_connector_state *conn_state)
400 {
401 struct drm_connector *connector = conn_state->connector;
402 struct nouveau_connector *nv_connector = nouveau_connector(connector);
403 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
404 int ret;
405
406 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
407 nv_connector->native_mode);
408 if (ret)
409 return ret;
410
411 if (crtc_state->mode_changed || crtc_state->connectors_changed)
412 asyh->or.bpc = connector->display_info.bpc;
413
414 /* We might have to reduce the bpc */
415 nv50_outp_atomic_fix_depth(encoder, crtc_state);
416
417 return 0;
418 }
419
420 struct nouveau_connector *
421 nv50_outp_get_new_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
422 {
423 struct drm_connector *connector;
424 struct drm_connector_state *connector_state;
425 struct drm_encoder *encoder = to_drm_encoder(outp);
426 int i;
427
428 for_each_new_connector_in_state(state, connector, connector_state, i) {
429 if (connector_state->best_encoder == encoder)
430 return nouveau_connector(connector);
431 }
432
433 return NULL;
434 }
435
436 struct nouveau_connector *
437 nv50_outp_get_old_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
438 {
439 struct drm_connector *connector;
440 struct drm_connector_state *connector_state;
441 struct drm_encoder *encoder = to_drm_encoder(outp);
442 int i;
443
444 for_each_old_connector_in_state(state, connector, connector_state, i) {
445 if (connector_state->best_encoder == encoder)
446 return nouveau_connector(connector);
447 }
448
449 return NULL;
450 }
451
452 static struct nouveau_crtc *
453 nv50_outp_get_new_crtc(const struct drm_atomic_state *state, const struct nouveau_encoder *outp)
454 {
455 struct drm_crtc *crtc;
456 struct drm_crtc_state *crtc_state;
457 const u32 mask = drm_encoder_mask(&outp->base.base);
458 int i;
459
460 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
461 if (crtc_state->encoder_mask & mask)
462 return nouveau_crtc(crtc);
463 }
464
465 return NULL;
466 }
467
468 /******************************************************************************
469 * DAC
470 *****************************************************************************/
471 static void
472 nv50_dac_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
473 {
474 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
475 struct nv50_core *core = nv50_disp(encoder->dev)->core;
476 const u32 ctrl = NVDEF(NV507D, DAC_SET_CONTROL, OWNER, NONE);
477
478 core->func->dac->ctrl(core, nv_encoder->outp.or.id, ctrl, NULL);
479 nv_encoder->crtc = NULL;
480 nvif_outp_release(&nv_encoder->outp);
481 }
482
483 static void
484 nv50_dac_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
485 {
486 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
487 struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
488 struct nv50_head_atom *asyh =
489 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
490 struct nv50_core *core = nv50_disp(encoder->dev)->core;
491 u32 ctrl = 0;
492
493 switch (nv_crtc->index) {
494 case 0: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD0); break;
495 case 1: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD1); break;
496 case 2: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD2); break;
497 case 3: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD3); break;
498 default:
499 WARN_ON(1);
500 break;
501 }
502
503 ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, PROTOCOL, RGB_CRT);
504
505 nvif_outp_acquire_rgb_crt(&nv_encoder->outp);
506
507 core->func->dac->ctrl(core, nv_encoder->outp.or.id, ctrl, asyh);
508 asyh->or.depth = 0;
509
510 nv_encoder->crtc = &nv_crtc->base;
511 }
512
513 static enum drm_connector_status
514 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
515 {
516 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
517 u32 loadval;
518 int ret;
519
520 loadval = nouveau_drm(encoder->dev)->vbios.dactestval;
521 if (loadval == 0)
522 loadval = 340;
523
524 ret = nvif_outp_load_detect(&nv_encoder->outp, loadval);
525 if (ret <= 0)
526 return connector_status_disconnected;
527
528 return connector_status_connected;
529 }
530
531 static const struct drm_encoder_helper_funcs
532 nv50_dac_help = {
533 .atomic_check = nv50_outp_atomic_check,
534 .atomic_enable = nv50_dac_atomic_enable,
535 .atomic_disable = nv50_dac_atomic_disable,
536 .detect = nv50_dac_detect
537 };
538
539 static void
540 nv50_dac_destroy(struct drm_encoder *encoder)
541 {
542 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
543
544 nvif_outp_dtor(&nv_encoder->outp);
545
546 drm_encoder_cleanup(encoder);
547 kfree(encoder);
548 }
549
550 static const struct drm_encoder_funcs
551 nv50_dac_func = {
552 .destroy = nv50_dac_destroy,
553 };
554
555 static int
556 nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
557 {
558 struct nouveau_drm *drm = nouveau_drm(connector->dev);
559 struct nv50_disp *disp = nv50_disp(connector->dev);
560 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
561 struct nvkm_i2c_bus *bus;
562 struct nouveau_encoder *nv_encoder;
563 struct drm_encoder *encoder;
564 int type = DRM_MODE_ENCODER_DAC;
565
566 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
567 if (!nv_encoder)
568 return -ENOMEM;
569 nv_encoder->dcb = dcbe;
570
571 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
572 if (bus)
573 nv_encoder->i2c = &bus->i2c;
574
575 encoder = to_drm_encoder(nv_encoder);
576 encoder->possible_crtcs = dcbe->heads;
577 encoder->possible_clones = 0;
578 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
579 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
580 drm_encoder_helper_add(encoder, &nv50_dac_help);
581
582 drm_connector_attach_encoder(connector, encoder);
583 return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
584 }
585
586 /*
587 * audio component binding for ELD notification
588 */
589 static void
590 nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
591 int dev_id)
592 {
593 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
594 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
595 port, dev_id);
596 }
597
598 static int
599 nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
600 bool *enabled, unsigned char *buf, int max_bytes)
601 {
602 struct drm_device *drm_dev = dev_get_drvdata(kdev);
603 struct nouveau_drm *drm = nouveau_drm(drm_dev);
604 struct drm_encoder *encoder;
605 struct nouveau_encoder *nv_encoder;
606 struct nouveau_crtc *nv_crtc;
607 int ret = 0;
608
609 *enabled = false;
610
611 mutex_lock(&drm->audio.lock);
612
613 drm_for_each_encoder(encoder, drm->dev) {
614 struct nouveau_connector *nv_connector = NULL;
615
616 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST)
617 continue; /* TODO */
618
619 nv_encoder = nouveau_encoder(encoder);
620 nv_connector = nouveau_connector(nv_encoder->audio.connector);
621 nv_crtc = nouveau_crtc(nv_encoder->crtc);
622
623 if (!nv_crtc || nv_encoder->outp.or.id != port || nv_crtc->index != dev_id)
624 continue;
625
626 *enabled = nv_encoder->audio.enabled;
627 if (*enabled) {
628 ret = drm_eld_size(nv_connector->base.eld);
629 memcpy(buf, nv_connector->base.eld,
630 min(max_bytes, ret));
631 }
632 break;
633 }
634
635 mutex_unlock(&drm->audio.lock);
636
637 return ret;
638 }
639
640 static const struct drm_audio_component_ops nv50_audio_component_ops = {
641 .get_eld = nv50_audio_component_get_eld,
642 };
643
644 static int
645 nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
646 void *data)
647 {
648 struct drm_device *drm_dev = dev_get_drvdata(kdev);
649 struct nouveau_drm *drm = nouveau_drm(drm_dev);
650 struct drm_audio_component *acomp = data;
651
652 if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
653 return -ENOMEM;
654
655 drm_modeset_lock_all(drm_dev);
656 acomp->ops = &nv50_audio_component_ops;
657 acomp->dev = kdev;
658 drm->audio.component = acomp;
659 drm_modeset_unlock_all(drm_dev);
660 return 0;
661 }
662
663 static void
664 nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
665 void *data)
666 {
667 struct drm_device *drm_dev = dev_get_drvdata(kdev);
668 struct nouveau_drm *drm = nouveau_drm(drm_dev);
669 struct drm_audio_component *acomp = data;
670
671 drm_modeset_lock_all(drm_dev);
672 drm->audio.component = NULL;
673 acomp->ops = NULL;
674 acomp->dev = NULL;
675 drm_modeset_unlock_all(drm_dev);
676 }
677
678 static const struct component_ops nv50_audio_component_bind_ops = {
679 .bind = nv50_audio_component_bind,
680 .unbind = nv50_audio_component_unbind,
681 };
682
683 static void
684 nv50_audio_component_init(struct nouveau_drm *drm)
685 {
686 if (component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
687 return;
688
689 drm->audio.component_registered = true;
690 mutex_init(&drm->audio.lock);
691 }
692
693 static void
694 nv50_audio_component_fini(struct nouveau_drm *drm)
695 {
696 if (!drm->audio.component_registered)
697 return;
698
699 component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
700 drm->audio.component_registered = false;
701 mutex_destroy(&drm->audio.lock);
702 }
703
704 /******************************************************************************
705 * Audio
706 *****************************************************************************/
707 static bool
708 nv50_audio_supported(struct drm_encoder *encoder)
709 {
710 struct nv50_disp *disp = nv50_disp(encoder->dev);
711
712 if (disp->disp->object.oclass <= GT200_DISP ||
713 disp->disp->object.oclass == GT206_DISP)
714 return false;
715
716 return true;
717 }
718
719 static void
720 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
721 {
722 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
723 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
724 struct nvif_outp *outp = &nv_encoder->outp;
725
726 if (!nv50_audio_supported(encoder))
727 return;
728
729 mutex_lock(&drm->audio.lock);
730 if (nv_encoder->audio.enabled) {
731 nv_encoder->audio.enabled = false;
732 nv_encoder->audio.connector = NULL;
733 nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, NULL, 0);
734 }
735 mutex_unlock(&drm->audio.lock);
736
737 nv50_audio_component_eld_notify(drm->audio.component, outp->or.id, nv_crtc->index);
738 }
739
740 static void
741 nv50_audio_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
742 struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
743 struct drm_display_mode *mode)
744 {
745 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
746 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
747 struct nvif_outp *outp = &nv_encoder->outp;
748
749 if (!nv50_audio_supported(encoder) || !drm_detect_monitor_audio(nv_connector->edid))
750 return;
751
752 mutex_lock(&drm->audio.lock);
753
754 nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, nv_connector->base.eld,
755 drm_eld_size(nv_connector->base.eld));
756 nv_encoder->audio.enabled = true;
757 nv_encoder->audio.connector = &nv_connector->base;
758
759 mutex_unlock(&drm->audio.lock);
760
761 nv50_audio_component_eld_notify(drm->audio.component, outp->or.id, nv_crtc->index);
762 }
763
764 /******************************************************************************
765 * HDMI
766 *****************************************************************************/
767 static void
768 nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
769 struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
770 struct drm_display_mode *mode, bool hda)
771 {
772 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
773 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
774 struct drm_hdmi_info *hdmi = &nv_connector->base.display_info.hdmi;
775 union hdmi_infoframe infoframe = { 0 };
776 const u8 rekey = 56; /* binary driver, and tegra, constant */
777 u8 scdc = 0;
778 u32 max_ac_packet;
779 struct {
780 struct nvif_outp_infoframe_v0 infoframe;
781 u8 data[17];
782 } args = { 0 };
783 int ret, size;
784
785 max_ac_packet = mode->htotal - mode->hdisplay;
786 max_ac_packet -= rekey;
787 max_ac_packet -= 18; /* constant from tegra */
788 max_ac_packet /= 32;
789
790 if (hdmi->scdc.scrambling.supported) {
791 const bool high_tmds_clock_ratio = mode->clock > 340000;
792
793 ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &scdc);
794 if (ret < 0) {
795 NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
796 return;
797 }
798
799 scdc &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
800 if (high_tmds_clock_ratio || hdmi->scdc.scrambling.low_rates)
801 scdc |= SCDC_SCRAMBLING_ENABLE;
802 if (high_tmds_clock_ratio)
803 scdc |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
804
805 ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, scdc);
806 if (ret < 0)
807 NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
808 scdc, ret);
809 }
810
811 ret = nvif_outp_acquire_tmds(&nv_encoder->outp, nv_crtc->index, true,
812 max_ac_packet, rekey, scdc, hda);
813 if (ret)
814 return;
815
816 /* AVI InfoFrame. */
817 args.infoframe.version = 0;
818 args.infoframe.head = nv_crtc->index;
819
820 if (!drm_hdmi_avi_infoframe_from_display_mode(&infoframe.avi, &nv_connector->base, mode)) {
821 drm_hdmi_avi_infoframe_quant_range(&infoframe.avi, &nv_connector->base, mode,
822 HDMI_QUANTIZATION_RANGE_FULL);
823
824 size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
825 } else {
826 size = 0;
827 }
828
829 nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_AVI, &args.infoframe, size);
830
831 /* Vendor InfoFrame. */
832 memset(&args.data, 0, sizeof(args.data));
833 if (!drm_hdmi_vendor_infoframe_from_display_mode(&infoframe.vendor.hdmi,
834 &nv_connector->base, mode))
835 size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
836 else
837 size = 0;
838
839 nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_VSI, &args.infoframe, size);
840
841 nv50_audio_enable(encoder, nv_crtc, nv_connector, state, mode);
842 }
843
844 /******************************************************************************
845 * MST
846 *****************************************************************************/
847 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
848 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
849 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
850
851 struct nv50_mstc {
852 struct nv50_mstm *mstm;
853 struct drm_dp_mst_port *port;
854 struct drm_connector connector;
855
856 struct drm_display_mode *native;
857 struct edid *edid;
858 };
859
860 struct nv50_msto {
861 struct drm_encoder encoder;
862
863 /* head is statically assigned on msto creation */
864 struct nv50_head *head;
865 struct nv50_mstc *mstc;
866 bool disabled;
867 bool enabled;
868 };
869
870 struct nouveau_encoder *nv50_real_outp(struct drm_encoder *encoder)
871 {
872 struct nv50_msto *msto;
873
874 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
875 return nouveau_encoder(encoder);
876
877 msto = nv50_msto(encoder);
878 if (!msto->mstc)
879 return NULL;
880 return msto->mstc->mstm->outp;
881 }
882
883 static void
884 nv50_msto_cleanup(struct drm_atomic_state *state,
885 struct drm_dp_mst_topology_state *new_mst_state,
886 struct drm_dp_mst_topology_mgr *mgr,
887 struct nv50_msto *msto)
888 {
889 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
890 struct drm_dp_mst_atomic_payload *new_payload =
891 drm_atomic_get_mst_payload_state(new_mst_state, msto->mstc->port);
892 struct drm_dp_mst_topology_state *old_mst_state =
893 drm_atomic_get_old_mst_topology_state(state, mgr);
894 const struct drm_dp_mst_atomic_payload *old_payload =
895 drm_atomic_get_mst_payload_state(old_mst_state, msto->mstc->port);
896
897 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
898
899 if (msto->disabled) {
900 msto->mstc = NULL;
901 msto->disabled = false;
902 drm_dp_remove_payload_part2(mgr, new_mst_state, old_payload, new_payload);
903 } else if (msto->enabled) {
904 drm_dp_add_payload_part2(mgr, state, new_payload);
905 msto->enabled = false;
906 }
907 }
908
909 static void
910 nv50_msto_prepare(struct drm_atomic_state *state,
911 struct drm_dp_mst_topology_state *mst_state,
912 struct drm_dp_mst_topology_mgr *mgr,
913 struct nv50_msto *msto)
914 {
915 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
916 struct nv50_mstc *mstc = msto->mstc;
917 struct nv50_mstm *mstm = mstc->mstm;
918 struct drm_dp_mst_atomic_payload *payload;
919
920 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
921
922 payload = drm_atomic_get_mst_payload_state(mst_state, mstc->port);
923
924 // TODO: Figure out if we want to do a better job of handling VCPI allocation failures here?
925 if (msto->disabled) {
926 drm_dp_remove_payload_part1(mgr, mst_state, payload);
927
928 nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index, 0, 0, 0, 0);
929 } else {
930 if (msto->enabled)
931 drm_dp_add_payload_part1(mgr, mst_state, payload);
932
933 nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index,
934 payload->vc_start_slot, payload->time_slots,
935 payload->pbn, payload->time_slots * mst_state->pbn_div);
936 }
937 }
938
939 static int
940 nv50_msto_atomic_check(struct drm_encoder *encoder,
941 struct drm_crtc_state *crtc_state,
942 struct drm_connector_state *conn_state)
943 {
944 struct drm_atomic_state *state = crtc_state->state;
945 struct drm_connector *connector = conn_state->connector;
946 struct drm_dp_mst_topology_state *mst_state;
947 struct nv50_mstc *mstc = nv50_mstc(connector);
948 struct nv50_mstm *mstm = mstc->mstm;
949 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
950 int slots;
951 int ret;
952
953 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
954 mstc->native);
955 if (ret)
956 return ret;
957
958 if (!drm_atomic_crtc_needs_modeset(crtc_state))
959 return 0;
960
961 /*
962 * When restoring duplicated states, we need to make sure that the bw
963 * remains the same and avoid recalculating it, as the connector's bpc
964 * may have changed after the state was duplicated
965 */
966 if (!state->duplicated) {
967 const int clock = crtc_state->adjusted_mode.clock;
968
969 asyh->or.bpc = connector->display_info.bpc;
970 asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3,
971 false);
972 }
973
974 mst_state = drm_atomic_get_mst_topology_state(state, &mstm->mgr);
975 if (IS_ERR(mst_state))
976 return PTR_ERR(mst_state);
977
978 if (!mst_state->pbn_div) {
979 struct nouveau_encoder *outp = mstc->mstm->outp;
980
981 mst_state->pbn_div = drm_dp_get_vc_payload_bw(&mstm->mgr,
982 outp->dp.link_bw, outp->dp.link_nr);
983 }
984
985 slots = drm_dp_atomic_find_time_slots(state, &mstm->mgr, mstc->port, asyh->dp.pbn);
986 if (slots < 0)
987 return slots;
988
989 asyh->dp.tu = slots;
990
991 return 0;
992 }
993
994 static u8
995 nv50_dp_bpc_to_depth(unsigned int bpc)
996 {
997 switch (bpc) {
998 case 6: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444;
999 case 8: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444;
1000 case 10:
1001 default: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444;
1002 }
1003 }
1004
1005 static void
1006 nv50_msto_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1007 {
1008 struct nv50_msto *msto = nv50_msto(encoder);
1009 struct nv50_head *head = msto->head;
1010 struct nv50_head_atom *asyh =
1011 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &head->base.base));
1012 struct nv50_mstc *mstc = NULL;
1013 struct nv50_mstm *mstm = NULL;
1014 struct drm_connector *connector;
1015 struct drm_connector_list_iter conn_iter;
1016 u8 proto;
1017
1018 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
1019 drm_for_each_connector_iter(connector, &conn_iter) {
1020 if (connector->state->best_encoder == &msto->encoder) {
1021 mstc = nv50_mstc(connector);
1022 mstm = mstc->mstm;
1023 break;
1024 }
1025 }
1026 drm_connector_list_iter_end(&conn_iter);
1027
1028 if (WARN_ON(!mstc))
1029 return;
1030
1031 if (!mstm->links++) {
1032 /*XXX: MST audio. */
1033 nvif_outp_acquire_dp(&mstm->outp->outp, mstm->outp->dp.dpcd, 0, 0, false, true);
1034 }
1035
1036 if (mstm->outp->outp.or.link & 1)
1037 proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1038 else
1039 proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1040
1041 mstm->outp->update(mstm->outp, head->base.index, asyh, proto,
1042 nv50_dp_bpc_to_depth(asyh->or.bpc));
1043
1044 msto->mstc = mstc;
1045 msto->enabled = true;
1046 mstm->modified = true;
1047 }
1048
1049 static void
1050 nv50_msto_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1051 {
1052 struct nv50_msto *msto = nv50_msto(encoder);
1053 struct nv50_mstc *mstc = msto->mstc;
1054 struct nv50_mstm *mstm = mstc->mstm;
1055
1056 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
1057 mstm->modified = true;
1058 if (!--mstm->links)
1059 mstm->disabled = true;
1060 msto->disabled = true;
1061 }
1062
1063 static const struct drm_encoder_helper_funcs
1064 nv50_msto_help = {
1065 .atomic_disable = nv50_msto_atomic_disable,
1066 .atomic_enable = nv50_msto_atomic_enable,
1067 .atomic_check = nv50_msto_atomic_check,
1068 };
1069
1070 static void
1071 nv50_msto_destroy(struct drm_encoder *encoder)
1072 {
1073 struct nv50_msto *msto = nv50_msto(encoder);
1074 drm_encoder_cleanup(&msto->encoder);
1075 kfree(msto);
1076 }
1077
1078 static const struct drm_encoder_funcs
1079 nv50_msto = {
1080 .destroy = nv50_msto_destroy,
1081 };
1082
1083 static struct nv50_msto *
1084 nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1085 {
1086 struct nv50_msto *msto;
1087 int ret;
1088
1089 msto = kzalloc(sizeof(*msto), GFP_KERNEL);
1090 if (!msto)
1091 return ERR_PTR(-ENOMEM);
1092
1093 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1094 DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1095 if (ret) {
1096 kfree(msto);
1097 return ERR_PTR(ret);
1098 }
1099
1100 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1101 msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
1102 msto->head = head;
1103 return msto;
1104 }
1105
1106 static struct drm_encoder *
1107 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1108 struct drm_atomic_state *state)
1109 {
1110 struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
1111 connector);
1112 struct nv50_mstc *mstc = nv50_mstc(connector);
1113 struct drm_crtc *crtc = connector_state->crtc;
1114
1115 if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1116 return NULL;
1117
1118 return &nv50_head(crtc)->msto->encoder;
1119 }
1120
1121 static enum drm_mode_status
1122 nv50_mstc_mode_valid(struct drm_connector *connector,
1123 struct drm_display_mode *mode)
1124 {
1125 struct nv50_mstc *mstc = nv50_mstc(connector);
1126 struct nouveau_encoder *outp = mstc->mstm->outp;
1127
1128 /* TODO: calculate the PBN from the dotclock and validate against the
1129 * MSTB's max possible PBN
1130 */
1131
1132 return nv50_dp_mode_valid(outp, mode, NULL);
1133 }
1134
1135 static int
1136 nv50_mstc_get_modes(struct drm_connector *connector)
1137 {
1138 struct nv50_mstc *mstc = nv50_mstc(connector);
1139 int ret = 0;
1140
1141 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1142 drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1143 if (mstc->edid)
1144 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
1145
1146 /*
1147 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
1148 * to 8 to save bandwidth on the topology. In the future, we'll want
1149 * to properly fix this by dynamically selecting the highest possible
1150 * bpc that would fit in the topology
1151 */
1152 if (connector->display_info.bpc)
1153 connector->display_info.bpc =
1154 clamp(connector->display_info.bpc, 6U, 8U);
1155 else
1156 connector->display_info.bpc = 8;
1157
1158 if (mstc->native)
1159 drm_mode_destroy(mstc->connector.dev, mstc->native);
1160 mstc->native = nouveau_conn_native_mode(&mstc->connector);
1161 return ret;
1162 }
1163
1164 static int
1165 nv50_mstc_atomic_check(struct drm_connector *connector,
1166 struct drm_atomic_state *state)
1167 {
1168 struct nv50_mstc *mstc = nv50_mstc(connector);
1169 struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;
1170
1171 return drm_dp_atomic_release_time_slots(state, mgr, mstc->port);
1172 }
1173
1174 static int
1175 nv50_mstc_detect(struct drm_connector *connector,
1176 struct drm_modeset_acquire_ctx *ctx, bool force)
1177 {
1178 struct nv50_mstc *mstc = nv50_mstc(connector);
1179 int ret;
1180
1181 if (drm_connector_is_unregistered(connector))
1182 return connector_status_disconnected;
1183
1184 ret = pm_runtime_get_sync(connector->dev->dev);
1185 if (ret < 0 && ret != -EACCES) {
1186 pm_runtime_put_autosuspend(connector->dev->dev);
1187 return connector_status_disconnected;
1188 }
1189
1190 ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
1191 mstc->port);
1192 if (ret != connector_status_connected)
1193 goto out;
1194
1195 out:
1196 pm_runtime_mark_last_busy(connector->dev->dev);
1197 pm_runtime_put_autosuspend(connector->dev->dev);
1198 return ret;
1199 }
1200
1201 static const struct drm_connector_helper_funcs
1202 nv50_mstc_help = {
1203 .get_modes = nv50_mstc_get_modes,
1204 .mode_valid = nv50_mstc_mode_valid,
1205 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
1206 .atomic_check = nv50_mstc_atomic_check,
1207 .detect_ctx = nv50_mstc_detect,
1208 };
1209
1210 static void
1211 nv50_mstc_destroy(struct drm_connector *connector)
1212 {
1213 struct nv50_mstc *mstc = nv50_mstc(connector);
1214
1215 drm_connector_cleanup(&mstc->connector);
1216 drm_dp_mst_put_port_malloc(mstc->port);
1217
1218 kfree(mstc);
1219 }
1220
1221 static const struct drm_connector_funcs
1222 nv50_mstc = {
1223 .reset = nouveau_conn_reset,
1224 .fill_modes = drm_helper_probe_single_connector_modes,
1225 .destroy = nv50_mstc_destroy,
1226 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1227 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1228 .atomic_set_property = nouveau_conn_atomic_set_property,
1229 .atomic_get_property = nouveau_conn_atomic_get_property,
1230 };
1231
1232 static int
1233 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
1234 const char *path, struct nv50_mstc **pmstc)
1235 {
1236 struct drm_device *dev = mstm->outp->base.base.dev;
1237 struct drm_crtc *crtc;
1238 struct nv50_mstc *mstc;
1239 int ret;
1240
1241 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
1242 return -ENOMEM;
1243 mstc->mstm = mstm;
1244 mstc->port = port;
1245
1246 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
1247 DRM_MODE_CONNECTOR_DisplayPort);
1248 if (ret) {
1249 kfree(*pmstc);
1250 *pmstc = NULL;
1251 return ret;
1252 }
1253
1254 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
1255
1256 mstc->connector.funcs->reset(&mstc->connector);
1257 nouveau_conn_attach_properties(&mstc->connector);
1258
1259 drm_for_each_crtc(crtc, dev) {
1260 if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1261 continue;
1262
1263 drm_connector_attach_encoder(&mstc->connector,
1264 &nv50_head(crtc)->msto->encoder);
1265 }
1266
1267 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
1268 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1269 drm_connector_set_path_property(&mstc->connector, path);
1270 drm_dp_mst_get_port_malloc(port);
1271 return 0;
1272 }
1273
1274 static void
1275 nv50_mstm_cleanup(struct drm_atomic_state *state,
1276 struct drm_dp_mst_topology_state *mst_state,
1277 struct nv50_mstm *mstm)
1278 {
1279 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1280 struct drm_encoder *encoder;
1281
1282 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1283 drm_dp_check_act_status(&mstm->mgr);
1284
1285 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1286 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1287 struct nv50_msto *msto = nv50_msto(encoder);
1288 struct nv50_mstc *mstc = msto->mstc;
1289 if (mstc && mstc->mstm == mstm)
1290 nv50_msto_cleanup(state, mst_state, &mstm->mgr, msto);
1291 }
1292 }
1293
1294 mstm->modified = false;
1295 }
1296
1297 static void
1298 nv50_mstm_prepare(struct drm_atomic_state *state,
1299 struct drm_dp_mst_topology_state *mst_state,
1300 struct nv50_mstm *mstm)
1301 {
1302 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1303 struct drm_encoder *encoder;
1304
1305 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
1306
1307 /* Disable payloads first */
1308 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1309 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1310 struct nv50_msto *msto = nv50_msto(encoder);
1311 struct nv50_mstc *mstc = msto->mstc;
1312 if (mstc && mstc->mstm == mstm && msto->disabled)
1313 nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1314 }
1315 }
1316
1317 /* Add payloads for new heads, while also updating the start slots of any unmodified (but
1318 * active) heads that may have had their VC slots shifted left after the previous step
1319 */
1320 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1321 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1322 struct nv50_msto *msto = nv50_msto(encoder);
1323 struct nv50_mstc *mstc = msto->mstc;
1324 if (mstc && mstc->mstm == mstm && !msto->disabled)
1325 nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1326 }
1327 }
1328
1329 if (mstm->disabled) {
1330 if (!mstm->links)
1331 nvif_outp_release(&mstm->outp->outp);
1332 mstm->disabled = false;
1333 }
1334 }
1335
1336 static struct drm_connector *
1337 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1338 struct drm_dp_mst_port *port, const char *path)
1339 {
1340 struct nv50_mstm *mstm = nv50_mstm(mgr);
1341 struct nv50_mstc *mstc;
1342 int ret;
1343
1344 ret = nv50_mstc_new(mstm, port, path, &mstc);
1345 if (ret)
1346 return NULL;
1347
1348 return &mstc->connector;
1349 }
1350
1351 static const struct drm_dp_mst_topology_cbs
1352 nv50_mstm = {
1353 .add_connector = nv50_mstm_add_connector,
1354 };
1355
1356 bool
1357 nv50_mstm_service(struct nouveau_drm *drm,
1358 struct nouveau_connector *nv_connector,
1359 struct nv50_mstm *mstm)
1360 {
1361 struct drm_dp_aux *aux = &nv_connector->aux;
1362 bool handled = true, ret = true;
1363 int rc;
1364 u8 esi[8] = {};
1365
1366 while (handled) {
1367 u8 ack[8] = {};
1368
1369 rc = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1370 if (rc != 8) {
1371 ret = false;
1372 break;
1373 }
1374
1375 drm_dp_mst_hpd_irq_handle_event(&mstm->mgr, esi, ack, &handled);
1376 if (!handled)
1377 break;
1378
1379 rc = drm_dp_dpcd_writeb(aux, DP_SINK_COUNT_ESI + 1, ack[1]);
1380
1381 if (rc != 1) {
1382 ret = false;
1383 break;
1384 }
1385
1386 drm_dp_mst_hpd_irq_send_new_request(&mstm->mgr);
1387 }
1388
1389 if (!ret)
1390 NV_DEBUG(drm, "Failed to handle ESI on %s: %d\n",
1391 nv_connector->base.name, rc);
1392
1393 return ret;
1394 }
1395
1396 void
1397 nv50_mstm_remove(struct nv50_mstm *mstm)
1398 {
1399 mstm->is_mst = false;
1400 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1401 }
1402
1403 int
1404 nv50_mstm_detect(struct nouveau_encoder *outp)
1405 {
1406 struct nv50_mstm *mstm = outp->dp.mstm;
1407 struct drm_dp_aux *aux;
1408 int ret;
1409
1410 if (!mstm || !mstm->can_mst)
1411 return 0;
1412
1413 aux = mstm->mgr.aux;
1414
1415 /* Clear any leftover MST state we didn't set ourselves by first
1416 * disabling MST if it was already enabled
1417 */
1418 ret = drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1419 if (ret < 0)
1420 return ret;
1421
1422 /* And start enabling */
1423 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, true);
1424 if (ret)
1425 return ret;
1426
1427 mstm->is_mst = true;
1428 return 1;
1429 }
1430
1431 static void
1432 nv50_mstm_fini(struct nouveau_encoder *outp)
1433 {
1434 struct nv50_mstm *mstm = outp->dp.mstm;
1435
1436 if (!mstm)
1437 return;
1438
1439 /* Don't change the MST state of this connector until we've finished
1440 * resuming, since we can't safely grab hpd_irq_lock in our resume
1441 * path to protect mstm->is_mst without potentially deadlocking
1442 */
1443 mutex_lock(&outp->dp.hpd_irq_lock);
1444 mstm->suspended = true;
1445 mutex_unlock(&outp->dp.hpd_irq_lock);
1446
1447 if (mstm->is_mst)
1448 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1449 }
1450
1451 static void
1452 nv50_mstm_init(struct nouveau_encoder *outp, bool runtime)
1453 {
1454 struct nv50_mstm *mstm = outp->dp.mstm;
1455 int ret = 0;
1456
1457 if (!mstm)
1458 return;
1459
1460 if (mstm->is_mst) {
1461 ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
1462 if (ret == -1)
1463 nv50_mstm_remove(mstm);
1464 }
1465
1466 mutex_lock(&outp->dp.hpd_irq_lock);
1467 mstm->suspended = false;
1468 mutex_unlock(&outp->dp.hpd_irq_lock);
1469
1470 if (ret == -1)
1471 drm_kms_helper_hotplug_event(mstm->mgr.dev);
1472 }
1473
1474 static void
1475 nv50_mstm_del(struct nv50_mstm **pmstm)
1476 {
1477 struct nv50_mstm *mstm = *pmstm;
1478 if (mstm) {
1479 drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1480 kfree(*pmstm);
1481 *pmstm = NULL;
1482 }
1483 }
1484
1485 static int
1486 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1487 int conn_base_id, struct nv50_mstm **pmstm)
1488 {
1489 const int max_payloads = hweight8(outp->dcb->heads);
1490 struct drm_device *dev = outp->base.base.dev;
1491 struct nv50_mstm *mstm;
1492 int ret;
1493
1494 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1495 return -ENOMEM;
1496 mstm->outp = outp;
1497 mstm->mgr.cbs = &nv50_mstm;
1498
1499 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1500 max_payloads, conn_base_id);
1501 if (ret)
1502 return ret;
1503
1504 return 0;
1505 }
1506
1507 /******************************************************************************
1508 * SOR
1509 *****************************************************************************/
1510 static void
1511 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1512 struct nv50_head_atom *asyh, u8 proto, u8 depth)
1513 {
1514 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1515 struct nv50_core *core = disp->core;
1516
1517 if (!asyh) {
1518 nv_encoder->ctrl &= ~BIT(head);
1519 if (NVDEF_TEST(nv_encoder->ctrl, NV507D, SOR_SET_CONTROL, OWNER, ==, NONE))
1520 nv_encoder->ctrl = 0;
1521 } else {
1522 nv_encoder->ctrl |= NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto);
1523 nv_encoder->ctrl |= BIT(head);
1524 asyh->or.depth = depth;
1525 }
1526
1527 core->func->sor->ctrl(core, nv_encoder->outp.or.id, nv_encoder->ctrl, asyh);
1528 }
1529
1530 /* TODO: Should we extend this to PWM-only backlights?
1531 * As well, should we add a DRM helper for waiting for the backlight to acknowledge
1532 * the panel backlight has been shut off? Intel doesn't seem to do this, and uses a
1533 * fixed time delay from the vbios…
1534 */
1535 static void
1536 nv50_sor_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1537 {
1538 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1539 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1540 struct nouveau_connector *nv_connector = nv50_outp_get_old_connector(state, nv_encoder);
1541 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1542 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
1543 struct nouveau_backlight *backlight = nv_connector->backlight;
1544 #endif
1545 struct drm_dp_aux *aux = &nv_connector->aux;
1546 int ret;
1547 u8 pwr;
1548
1549 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1550 if (backlight && backlight->uses_dpcd) {
1551 ret = drm_edp_backlight_disable(aux, &backlight->edp_info);
1552 if (ret < 0)
1553 NV_ERROR(drm, "Failed to disable backlight on [CONNECTOR:%d:%s]: %d\n",
1554 nv_connector->base.base.id, nv_connector->base.name, ret);
1555 }
1556 #endif
1557
1558 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
1559 ret = drm_dp_dpcd_readb(aux, DP_SET_POWER, &pwr);
1560
1561 if (ret == 0) {
1562 pwr &= ~DP_SET_POWER_MASK;
1563 pwr |= DP_SET_POWER_D3;
1564 drm_dp_dpcd_writeb(aux, DP_SET_POWER, pwr);
1565 }
1566 }
1567
1568 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
1569 nv50_audio_disable(encoder, nv_crtc);
1570 nvif_outp_release(&nv_encoder->outp);
1571 nv_encoder->crtc = NULL;
1572 }
1573
1574 static void
1575 nv50_sor_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1576 {
1577 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1578 struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1579 struct nv50_head_atom *asyh =
1580 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1581 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1582 struct nv50_disp *disp = nv50_disp(encoder->dev);
1583 struct nvif_outp *outp = &nv_encoder->outp;
1584 struct drm_device *dev = encoder->dev;
1585 struct nouveau_drm *drm = nouveau_drm(dev);
1586 struct nouveau_connector *nv_connector;
1587 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1588 struct nouveau_backlight *backlight;
1589 #endif
1590 struct nvbios *bios = &drm->vbios;
1591 bool lvds_dual = false, lvds_8bpc = false, hda = false;
1592 u8 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_CUSTOM;
1593 u8 depth = NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT;
1594
1595 nv_connector = nv50_outp_get_new_connector(state, nv_encoder);
1596 nv_encoder->crtc = &nv_crtc->base;
1597
1598 if ((disp->disp->object.oclass == GT214_DISP ||
1599 disp->disp->object.oclass >= GF110_DISP) &&
1600 drm_detect_monitor_audio(nv_connector->edid))
1601 hda = true;
1602
1603 switch (nv_encoder->dcb->type) {
1604 case DCB_OUTPUT_TMDS:
1605 if (disp->disp->object.oclass == NV50_DISP ||
1606 !drm_detect_hdmi_monitor(nv_connector->edid))
1607 nvif_outp_acquire_tmds(outp, nv_crtc->index, false, 0, 0, 0, false);
1608 else
1609 nv50_hdmi_enable(encoder, nv_crtc, nv_connector, state, mode, hda);
1610
1611 if (nv_encoder->outp.or.link & 1) {
1612 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_A;
1613 /* Only enable dual-link if:
1614 * - Need to (i.e. rate > 165MHz)
1615 * - DCB says we can
1616 * - Not an HDMI monitor, since there's no dual-link
1617 * on HDMI.
1618 */
1619 if (mode->clock >= 165000 &&
1620 nv_encoder->dcb->duallink_possible &&
1621 !drm_detect_hdmi_monitor(nv_connector->edid))
1622 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_DUAL_TMDS;
1623 } else {
1624 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_B;
1625 }
1626 break;
1627 case DCB_OUTPUT_LVDS:
1628 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_LVDS_CUSTOM;
1629
1630 if (bios->fp_no_ddc) {
1631 lvds_dual = bios->fp.dual_link;
1632 lvds_8bpc = bios->fp.if_is_24bit;
1633 } else {
1634 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1635 if (((u8 *)nv_connector->edid)[121] == 2)
1636 lvds_dual = true;
1637 } else
1638 if (mode->clock >= bios->fp.duallink_transition_clk) {
1639 lvds_dual = true;
1640 }
1641
1642 if (lvds_dual) {
1643 if (bios->fp.strapless_is_24bit & 2)
1644 lvds_8bpc = true;
1645 } else {
1646 if (bios->fp.strapless_is_24bit & 1)
1647 lvds_8bpc = true;
1648 }
1649
1650 if (asyh->or.bpc == 8)
1651 lvds_8bpc = true;
1652 }
1653
1654 nvif_outp_acquire_lvds(&nv_encoder->outp, lvds_dual, lvds_8bpc);
1655 break;
1656 case DCB_OUTPUT_DP:
1657 nvif_outp_acquire_dp(&nv_encoder->outp, nv_encoder->dp.dpcd, 0, 0, hda, false);
1658 depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1659
1660 if (nv_encoder->outp.or.link & 1)
1661 proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1662 else
1663 proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1664
1665 nv50_audio_enable(encoder, nv_crtc, nv_connector, state, mode);
1666
1667 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1668 backlight = nv_connector->backlight;
1669 if (backlight && backlight->uses_dpcd)
1670 drm_edp_backlight_enable(&nv_connector->aux, &backlight->edp_info,
1671 (u16)backlight->dev->props.brightness);
1672 #endif
1673
1674 break;
1675 default:
1676 BUG();
1677 break;
1678 }
1679
1680 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1681 }
1682
1683 static const struct drm_encoder_helper_funcs
1684 nv50_sor_help = {
1685 .atomic_check = nv50_outp_atomic_check,
1686 .atomic_enable = nv50_sor_atomic_enable,
1687 .atomic_disable = nv50_sor_atomic_disable,
1688 };
1689
1690 static void
1691 nv50_sor_destroy(struct drm_encoder *encoder)
1692 {
1693 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1694
1695 nvif_outp_dtor(&nv_encoder->outp);
1696
1697 nv50_mstm_del(&nv_encoder->dp.mstm);
1698 drm_encoder_cleanup(encoder);
1699
1700 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1701 mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
1702
1703 kfree(encoder);
1704 }
1705
1706 static const struct drm_encoder_funcs
1707 nv50_sor_func = {
1708 .destroy = nv50_sor_destroy,
1709 };
1710
1711 bool nv50_has_mst(struct nouveau_drm *drm)
1712 {
1713 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
1714 u32 data;
1715 u8 ver, hdr, cnt, len;
1716
1717 data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len);
1718 return data && ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04);
1719 }
1720
1721 static int
1722 nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
1723 {
1724 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1725 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1726 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1727 struct nouveau_encoder *nv_encoder;
1728 struct drm_encoder *encoder;
1729 struct nv50_disp *disp = nv50_disp(connector->dev);
1730 int type, ret;
1731
1732 switch (dcbe->type) {
1733 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1734 case DCB_OUTPUT_TMDS:
1735 case DCB_OUTPUT_DP:
1736 default:
1737 type = DRM_MODE_ENCODER_TMDS;
1738 break;
1739 }
1740
1741 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1742 if (!nv_encoder)
1743 return -ENOMEM;
1744 nv_encoder->dcb = dcbe;
1745 nv_encoder->update = nv50_sor_update;
1746
1747 encoder = to_drm_encoder(nv_encoder);
1748 encoder->possible_crtcs = dcbe->heads;
1749 encoder->possible_clones = 0;
1750 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1751 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1752 drm_encoder_helper_add(encoder, &nv50_sor_help);
1753
1754 drm_connector_attach_encoder(connector, encoder);
1755
1756 disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1757 nv50_outp_dump_caps(drm, nv_encoder);
1758
1759 if (dcbe->type == DCB_OUTPUT_DP) {
1760 struct nvkm_i2c_aux *aux =
1761 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1762
1763 mutex_init(&nv_encoder->dp.hpd_irq_lock);
1764
1765 if (aux) {
1766 if (disp->disp->object.oclass < GF110_DISP) {
1767 /* HW has no support for address-only
1768 * transactions, so we're required to
1769 * use custom I2C-over-AUX code.
1770 */
1771 nv_encoder->i2c = &aux->i2c;
1772 } else {
1773 nv_encoder->i2c = &nv_connector->aux.ddc;
1774 }
1775 nv_encoder->aux = aux;
1776 }
1777
1778 if (nv_connector->type != DCB_CONNECTOR_eDP &&
1779 nv50_has_mst(drm)) {
1780 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
1781 16, nv_connector->base.base.id,
1782 &nv_encoder->dp.mstm);
1783 if (ret)
1784 return ret;
1785 }
1786 } else {
1787 struct nvkm_i2c_bus *bus =
1788 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1789 if (bus)
1790 nv_encoder->i2c = &bus->i2c;
1791 }
1792
1793 return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
1794 }
1795
1796 /******************************************************************************
1797 * PIOR
1798 *****************************************************************************/
1799 static int
1800 nv50_pior_atomic_check(struct drm_encoder *encoder,
1801 struct drm_crtc_state *crtc_state,
1802 struct drm_connector_state *conn_state)
1803 {
1804 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1805 if (ret)
1806 return ret;
1807 crtc_state->adjusted_mode.clock *= 2;
1808 return 0;
1809 }
1810
1811 static void
1812 nv50_pior_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1813 {
1814 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1815 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1816 const u32 ctrl = NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, NONE);
1817
1818 core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, NULL);
1819 nv_encoder->crtc = NULL;
1820 nvif_outp_release(&nv_encoder->outp);
1821 }
1822
1823 static void
1824 nv50_pior_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1825 {
1826 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1827 struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1828 struct nv50_head_atom *asyh =
1829 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1830 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1831 u32 ctrl = 0;
1832
1833 switch (nv_crtc->index) {
1834 case 0: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD0); break;
1835 case 1: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD1); break;
1836 default:
1837 WARN_ON(1);
1838 break;
1839 }
1840
1841 switch (asyh->or.bpc) {
1842 case 10: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444; break;
1843 case 8: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444; break;
1844 case 6: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444; break;
1845 default: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT; break;
1846 }
1847
1848 switch (nv_encoder->dcb->type) {
1849 case DCB_OUTPUT_TMDS:
1850 ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
1851 nvif_outp_acquire_tmds(&nv_encoder->outp, false, false, 0, 0, 0, false);
1852 break;
1853 case DCB_OUTPUT_DP:
1854 ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
1855 nvif_outp_acquire_dp(&nv_encoder->outp, nv_encoder->dp.dpcd, 0, 0, false, false);
1856 break;
1857 default:
1858 BUG();
1859 break;
1860 }
1861
1862 core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, asyh);
1863 nv_encoder->crtc = &nv_crtc->base;
1864 }
1865
1866 static const struct drm_encoder_helper_funcs
1867 nv50_pior_help = {
1868 .atomic_check = nv50_pior_atomic_check,
1869 .atomic_enable = nv50_pior_atomic_enable,
1870 .atomic_disable = nv50_pior_atomic_disable,
1871 };
1872
1873 static void
1874 nv50_pior_destroy(struct drm_encoder *encoder)
1875 {
1876 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1877
1878 nvif_outp_dtor(&nv_encoder->outp);
1879
1880 drm_encoder_cleanup(encoder);
1881
1882 mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
1883 kfree(encoder);
1884 }
1885
1886 static const struct drm_encoder_funcs
1887 nv50_pior_func = {
1888 .destroy = nv50_pior_destroy,
1889 };
1890
1891 static int
1892 nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1893 {
1894 struct drm_device *dev = connector->dev;
1895 struct nouveau_drm *drm = nouveau_drm(dev);
1896 struct nv50_disp *disp = nv50_disp(dev);
1897 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1898 struct nvkm_i2c_bus *bus = NULL;
1899 struct nvkm_i2c_aux *aux = NULL;
1900 struct i2c_adapter *ddc;
1901 struct nouveau_encoder *nv_encoder;
1902 struct drm_encoder *encoder;
1903 int type;
1904
1905 switch (dcbe->type) {
1906 case DCB_OUTPUT_TMDS:
1907 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1908 ddc = bus ? &bus->i2c : NULL;
1909 type = DRM_MODE_ENCODER_TMDS;
1910 break;
1911 case DCB_OUTPUT_DP:
1912 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
1913 ddc = aux ? &aux->i2c : NULL;
1914 type = DRM_MODE_ENCODER_TMDS;
1915 break;
1916 default:
1917 return -ENODEV;
1918 }
1919
1920 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1921 if (!nv_encoder)
1922 return -ENOMEM;
1923 nv_encoder->dcb = dcbe;
1924 nv_encoder->i2c = ddc;
1925 nv_encoder->aux = aux;
1926
1927 mutex_init(&nv_encoder->dp.hpd_irq_lock);
1928
1929 encoder = to_drm_encoder(nv_encoder);
1930 encoder->possible_crtcs = dcbe->heads;
1931 encoder->possible_clones = 0;
1932 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1933 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
1934 drm_encoder_helper_add(encoder, &nv50_pior_help);
1935
1936 drm_connector_attach_encoder(connector, encoder);
1937
1938 disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1939 nv50_outp_dump_caps(drm, nv_encoder);
1940
1941 return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
1942 }
1943
1944 /******************************************************************************
1945 * Atomic
1946 *****************************************************************************/
1947
1948 static void
1949 nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
1950 {
1951 struct drm_dp_mst_topology_mgr *mgr;
1952 struct drm_dp_mst_topology_state *mst_state;
1953 struct nouveau_drm *drm = nouveau_drm(state->dev);
1954 struct nv50_disp *disp = nv50_disp(drm->dev);
1955 struct nv50_core *core = disp->core;
1956 struct nv50_mstm *mstm;
1957 int i;
1958
1959 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
1960
1961 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
1962 mstm = nv50_mstm(mgr);
1963 if (mstm->modified)
1964 nv50_mstm_prepare(state, mst_state, mstm);
1965 }
1966
1967 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1968 core->func->update(core, interlock, true);
1969 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1970 disp->core->chan.base.device))
1971 NV_ERROR(drm, "core notifier timeout\n");
1972
1973 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
1974 mstm = nv50_mstm(mgr);
1975 if (mstm->modified)
1976 nv50_mstm_cleanup(state, mst_state, mstm);
1977 }
1978 }
1979
1980 static void
1981 nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
1982 {
1983 struct drm_plane_state *new_plane_state;
1984 struct drm_plane *plane;
1985 int i;
1986
1987 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1988 struct nv50_wndw *wndw = nv50_wndw(plane);
1989 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1990 if (wndw->func->update)
1991 wndw->func->update(wndw, interlock);
1992 }
1993 }
1994 }
1995
1996 static void
1997 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1998 {
1999 struct drm_device *dev = state->dev;
2000 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
2001 struct drm_crtc *crtc;
2002 struct drm_plane_state *new_plane_state;
2003 struct drm_plane *plane;
2004 struct nouveau_drm *drm = nouveau_drm(dev);
2005 struct nv50_disp *disp = nv50_disp(dev);
2006 struct nv50_atom *atom = nv50_atom(state);
2007 struct nv50_core *core = disp->core;
2008 struct nv50_outp_atom *outp, *outt;
2009 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
2010 int i;
2011 bool flushed = false;
2012
2013 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
2014 nv50_crc_atomic_stop_reporting(state);
2015 drm_atomic_helper_wait_for_fences(dev, state, false);
2016 drm_atomic_helper_wait_for_dependencies(state);
2017 drm_dp_mst_atomic_wait_for_dependencies(state);
2018 drm_atomic_helper_update_legacy_modeset_state(dev, state);
2019 drm_atomic_helper_calc_timestamping_constants(state);
2020
2021 if (atom->lock_core)
2022 mutex_lock(&disp->mutex);
2023
2024 /* Disable head(s). */
2025 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2026 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2027 struct nv50_head *head = nv50_head(crtc);
2028
2029 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
2030 asyh->clr.mask, asyh->set.mask);
2031
2032 if (old_crtc_state->active && !new_crtc_state->active) {
2033 pm_runtime_put_noidle(dev->dev);
2034 drm_crtc_vblank_off(crtc);
2035 }
2036
2037 if (asyh->clr.mask) {
2038 nv50_head_flush_clr(head, asyh, atom->flush_disable);
2039 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2040 }
2041 }
2042
2043 /* Disable plane(s). */
2044 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2045 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2046 struct nv50_wndw *wndw = nv50_wndw(plane);
2047
2048 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
2049 asyw->clr.mask, asyw->set.mask);
2050 if (!asyw->clr.mask)
2051 continue;
2052
2053 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
2054 }
2055
2056 /* Disable output path(s). */
2057 list_for_each_entry(outp, &atom->outp, head) {
2058 const struct drm_encoder_helper_funcs *help;
2059 struct drm_encoder *encoder;
2060
2061 encoder = outp->encoder;
2062 help = encoder->helper_private;
2063
2064 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
2065 outp->clr.mask, outp->set.mask);
2066
2067 if (outp->clr.mask) {
2068 help->atomic_disable(encoder, state);
2069 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2070 if (outp->flush_disable) {
2071 nv50_disp_atomic_commit_wndw(state, interlock);
2072 nv50_disp_atomic_commit_core(state, interlock);
2073 memset(interlock, 0x00, sizeof(interlock));
2074
2075 flushed = true;
2076 }
2077 }
2078 }
2079
2080 /* Flush disable. */
2081 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2082 if (atom->flush_disable) {
2083 nv50_disp_atomic_commit_wndw(state, interlock);
2084 nv50_disp_atomic_commit_core(state, interlock);
2085 memset(interlock, 0x00, sizeof(interlock));
2086
2087 flushed = true;
2088 }
2089 }
2090
2091 if (flushed)
2092 nv50_crc_atomic_release_notifier_contexts(state);
2093 nv50_crc_atomic_init_notifier_contexts(state);
2094
2095 /* Update output path(s). */
2096 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2097 const struct drm_encoder_helper_funcs *help;
2098 struct drm_encoder *encoder;
2099
2100 encoder = outp->encoder;
2101 help = encoder->helper_private;
2102
2103 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
2104 outp->set.mask, outp->clr.mask);
2105
2106 if (outp->set.mask) {
2107 help->atomic_enable(encoder, state);
2108 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2109 }
2110
2111 list_del(&outp->head);
2112 kfree(outp);
2113 }
2114
2115 /* Update head(s). */
2116 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2117 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2118 struct nv50_head *head = nv50_head(crtc);
2119
2120 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2121 asyh->set.mask, asyh->clr.mask);
2122
2123 if (asyh->set.mask) {
2124 nv50_head_flush_set(head, asyh);
2125 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2126 }
2127
2128 if (new_crtc_state->active) {
2129 if (!old_crtc_state->active) {
2130 drm_crtc_vblank_on(crtc);
2131 pm_runtime_get_noresume(dev->dev);
2132 }
2133 if (new_crtc_state->event)
2134 drm_crtc_vblank_get(crtc);
2135 }
2136 }
2137
2138 /* Update window->head assignment.
2139 *
2140 * This has to happen in an update that's not interlocked with
2141 * any window channels to avoid hitting HW error checks.
2142 *
2143 *TODO: Proper handling of window ownership (Turing apparently
2144 * supports non-fixed mappings).
2145 */
2146 if (core->assign_windows) {
2147 core->func->wndw.owner(core);
2148 nv50_disp_atomic_commit_core(state, interlock);
2149 core->assign_windows = false;
2150 interlock[NV50_DISP_INTERLOCK_CORE] = 0;
2151 }
2152
2153 /* Finish updating head(s)...
2154 *
2155 * NVD is rather picky about both where window assignments can change,
2156 * *and* about certain core and window channel states matching.
2157 *
2158 * The EFI GOP driver on newer GPUs configures window channels with a
2159 * different output format to what we do, and the core channel update
2160 * in the assign_windows case above would result in a state mismatch.
2161 *
2162 * Delay some of the head update until after that point to workaround
2163 * the issue. This only affects the initial modeset.
2164 *
2165 * TODO: handle this better when adding flexible window mapping
2166 */
2167 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2168 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2169 struct nv50_head *head = nv50_head(crtc);
2170
2171 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2172 asyh->set.mask, asyh->clr.mask);
2173
2174 if (asyh->set.mask) {
2175 nv50_head_flush_set_wndw(head, asyh);
2176 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2177 }
2178 }
2179
2180 /* Update plane(s). */
2181 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2182 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2183 struct nv50_wndw *wndw = nv50_wndw(plane);
2184
2185 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
2186 asyw->set.mask, asyw->clr.mask);
2187 if ( !asyw->set.mask &&
2188 (!asyw->clr.mask || atom->flush_disable))
2189 continue;
2190
2191 nv50_wndw_flush_set(wndw, interlock, asyw);
2192 }
2193
2194 /* Flush update. */
2195 nv50_disp_atomic_commit_wndw(state, interlock);
2196
2197 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2198 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2199 interlock[NV50_DISP_INTERLOCK_OVLY] ||
2200 interlock[NV50_DISP_INTERLOCK_WNDW] ||
2201 !atom->state.legacy_cursor_update)
2202 nv50_disp_atomic_commit_core(state, interlock);
2203 else
2204 disp->core->func->update(disp->core, interlock, false);
2205 }
2206
2207 if (atom->lock_core)
2208 mutex_unlock(&disp->mutex);
2209
2210 /* Wait for HW to signal completion. */
2211 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2212 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2213 struct nv50_wndw *wndw = nv50_wndw(plane);
2214 int ret = nv50_wndw_wait_armed(wndw, asyw);
2215 if (ret)
2216 NV_ERROR(drm, "%s: timeout\n", plane->name);
2217 }
2218
2219 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2220 if (new_crtc_state->event) {
2221 unsigned long flags;
2222 /* Get correct count/ts if racing with vblank irq */
2223 if (new_crtc_state->active)
2224 drm_crtc_accurate_vblank_count(crtc);
2225 spin_lock_irqsave(&crtc->dev->event_lock, flags);
2226 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2227 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2228
2229 new_crtc_state->event = NULL;
2230 if (new_crtc_state->active)
2231 drm_crtc_vblank_put(crtc);
2232 }
2233 }
2234
2235 nv50_crc_atomic_start_reporting(state);
2236 if (!flushed)
2237 nv50_crc_atomic_release_notifier_contexts(state);
2238
2239 drm_atomic_helper_commit_hw_done(state);
2240 drm_atomic_helper_cleanup_planes(dev, state);
2241 drm_atomic_helper_commit_cleanup_done(state);
2242 drm_atomic_state_put(state);
2243
2244 /* Drop the RPM ref we got from nv50_disp_atomic_commit() */
2245 pm_runtime_mark_last_busy(dev->dev);
2246 pm_runtime_put_autosuspend(dev->dev);
2247 }
2248
2249 static void
2250 nv50_disp_atomic_commit_work(struct work_struct *work)
2251 {
2252 struct drm_atomic_state *state =
2253 container_of(work, typeof(*state), commit_work);
2254 nv50_disp_atomic_commit_tail(state);
2255 }
2256
2257 static int
2258 nv50_disp_atomic_commit(struct drm_device *dev,
2259 struct drm_atomic_state *state, bool nonblock)
2260 {
2261 struct drm_plane_state *new_plane_state;
2262 struct drm_plane *plane;
2263 int ret, i;
2264
2265 ret = pm_runtime_get_sync(dev->dev);
2266 if (ret < 0 && ret != -EACCES) {
2267 pm_runtime_put_autosuspend(dev->dev);
2268 return ret;
2269 }
2270
2271 ret = drm_atomic_helper_setup_commit(state, nonblock);
2272 if (ret)
2273 goto done;
2274
2275 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
2276
2277 ret = drm_atomic_helper_prepare_planes(dev, state);
2278 if (ret)
2279 goto done;
2280
2281 if (!nonblock) {
2282 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2283 if (ret)
2284 goto err_cleanup;
2285 }
2286
2287 ret = drm_atomic_helper_swap_state(state, true);
2288 if (ret)
2289 goto err_cleanup;
2290
2291 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2292 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2293 struct nv50_wndw *wndw = nv50_wndw(plane);
2294
2295 if (asyw->set.image)
2296 nv50_wndw_ntfy_enable(wndw, asyw);
2297 }
2298
2299 drm_atomic_state_get(state);
2300
2301 /*
2302 * Grab another RPM ref for the commit tail, which will release the
2303 * ref when it's finished
2304 */
2305 pm_runtime_get_noresume(dev->dev);
2306
2307 if (nonblock)
2308 queue_work(system_unbound_wq, &state->commit_work);
2309 else
2310 nv50_disp_atomic_commit_tail(state);
2311
2312 err_cleanup:
2313 if (ret)
2314 drm_atomic_helper_cleanup_planes(dev, state);
2315 done:
2316 pm_runtime_put_autosuspend(dev->dev);
2317 return ret;
2318 }
2319
2320 static struct nv50_outp_atom *
2321 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
2322 {
2323 struct nv50_outp_atom *outp;
2324
2325 list_for_each_entry(outp, &atom->outp, head) {
2326 if (outp->encoder == encoder)
2327 return outp;
2328 }
2329
2330 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2331 if (!outp)
2332 return ERR_PTR(-ENOMEM);
2333
2334 list_add(&outp->head, &atom->outp);
2335 outp->encoder = encoder;
2336 return outp;
2337 }
2338
2339 static int
2340 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2341 struct drm_connector_state *old_connector_state)
2342 {
2343 struct drm_encoder *encoder = old_connector_state->best_encoder;
2344 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2345 struct drm_crtc *crtc;
2346 struct nv50_outp_atom *outp;
2347
2348 if (!(crtc = old_connector_state->crtc))
2349 return 0;
2350
2351 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
2352 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2353 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2354 outp = nv50_disp_outp_atomic_add(atom, encoder);
2355 if (IS_ERR(outp))
2356 return PTR_ERR(outp);
2357
2358 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
2359 outp->flush_disable = true;
2360 atom->flush_disable = true;
2361 }
2362 outp->clr.ctrl = true;
2363 atom->lock_core = true;
2364 }
2365
2366 return 0;
2367 }
2368
2369 static int
2370 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
2371 struct drm_connector_state *connector_state)
2372 {
2373 struct drm_encoder *encoder = connector_state->best_encoder;
2374 struct drm_crtc_state *new_crtc_state;
2375 struct drm_crtc *crtc;
2376 struct nv50_outp_atom *outp;
2377
2378 if (!(crtc = connector_state->crtc))
2379 return 0;
2380
2381 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2382 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2383 outp = nv50_disp_outp_atomic_add(atom, encoder);
2384 if (IS_ERR(outp))
2385 return PTR_ERR(outp);
2386
2387 outp->set.ctrl = true;
2388 atom->lock_core = true;
2389 }
2390
2391 return 0;
2392 }
2393
2394 static int
2395 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
2396 {
2397 struct nv50_atom *atom = nv50_atom(state);
2398 struct nv50_core *core = nv50_disp(dev)->core;
2399 struct drm_connector_state *old_connector_state, *new_connector_state;
2400 struct drm_connector *connector;
2401 struct drm_crtc_state *new_crtc_state;
2402 struct drm_crtc *crtc;
2403 struct nv50_head *head;
2404 struct nv50_head_atom *asyh;
2405 int ret, i;
2406
2407 if (core->assign_windows && core->func->head->static_wndw_map) {
2408 drm_for_each_crtc(crtc, dev) {
2409 new_crtc_state = drm_atomic_get_crtc_state(state,
2410 crtc);
2411 if (IS_ERR(new_crtc_state))
2412 return PTR_ERR(new_crtc_state);
2413
2414 head = nv50_head(crtc);
2415 asyh = nv50_head_atom(new_crtc_state);
2416 core->func->head->static_wndw_map(head, asyh);
2417 }
2418 }
2419
2420 /* We need to handle colour management on a per-plane basis. */
2421 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2422 if (new_crtc_state->color_mgmt_changed) {
2423 ret = drm_atomic_add_affected_planes(state, crtc);
2424 if (ret)
2425 return ret;
2426 }
2427 }
2428
2429 ret = drm_atomic_helper_check(dev, state);
2430 if (ret)
2431 return ret;
2432
2433 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
2434 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2435 if (ret)
2436 return ret;
2437
2438 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2439 if (ret)
2440 return ret;
2441 }
2442
2443 ret = drm_dp_mst_atomic_check(state);
2444 if (ret)
2445 return ret;
2446
2447 nv50_crc_atomic_check_outp(atom);
2448
2449 return 0;
2450 }
2451
2452 static void
2453 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2454 {
2455 struct nv50_atom *atom = nv50_atom(state);
2456 struct nv50_outp_atom *outp, *outt;
2457
2458 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2459 list_del(&outp->head);
2460 kfree(outp);
2461 }
2462
2463 drm_atomic_state_default_clear(state);
2464 }
2465
2466 static void
2467 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2468 {
2469 struct nv50_atom *atom = nv50_atom(state);
2470 drm_atomic_state_default_release(&atom->state);
2471 kfree(atom);
2472 }
2473
2474 static struct drm_atomic_state *
2475 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2476 {
2477 struct nv50_atom *atom;
2478 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2479 drm_atomic_state_init(dev, &atom->state) < 0) {
2480 kfree(atom);
2481 return NULL;
2482 }
2483 INIT_LIST_HEAD(&atom->outp);
2484 return &atom->state;
2485 }
2486
2487 static const struct drm_mode_config_funcs
2488 nv50_disp_func = {
2489 .fb_create = nouveau_user_framebuffer_create,
2490 .output_poll_changed = drm_fb_helper_output_poll_changed,
2491 .atomic_check = nv50_disp_atomic_check,
2492 .atomic_commit = nv50_disp_atomic_commit,
2493 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2494 .atomic_state_clear = nv50_disp_atomic_state_clear,
2495 .atomic_state_free = nv50_disp_atomic_state_free,
2496 };
2497
2498 static const struct drm_mode_config_helper_funcs
2499 nv50_disp_helper_func = {
2500 .atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
2501 };
2502
2503 /******************************************************************************
2504 * Init
2505 *****************************************************************************/
2506
2507 static void
2508 nv50_display_fini(struct drm_device *dev, bool runtime, bool suspend)
2509 {
2510 struct nouveau_drm *drm = nouveau_drm(dev);
2511 struct drm_encoder *encoder;
2512
2513 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2514 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
2515 nv50_mstm_fini(nouveau_encoder(encoder));
2516 }
2517
2518 if (!runtime)
2519 cancel_work_sync(&drm->hpd_work);
2520 }
2521
2522 static int
2523 nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2524 {
2525 struct nv50_core *core = nv50_disp(dev)->core;
2526 struct drm_encoder *encoder;
2527
2528 if (resume || runtime)
2529 core->func->init(core);
2530
2531 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2532 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2533 struct nouveau_encoder *nv_encoder =
2534 nouveau_encoder(encoder);
2535 nv50_mstm_init(nv_encoder, runtime);
2536 }
2537 }
2538
2539 return 0;
2540 }
2541
2542 static void
2543 nv50_display_destroy(struct drm_device *dev)
2544 {
2545 struct nv50_disp *disp = nv50_disp(dev);
2546
2547 nv50_audio_component_fini(nouveau_drm(dev));
2548
2549 nvif_object_unmap(&disp->caps);
2550 nvif_object_dtor(&disp->caps);
2551 nv50_core_del(&disp->core);
2552
2553 nouveau_bo_unmap(disp->sync);
2554 if (disp->sync)
2555 nouveau_bo_unpin(disp->sync);
2556 nouveau_bo_ref(NULL, &disp->sync);
2557
2558 nouveau_display(dev)->priv = NULL;
2559 kfree(disp);
2560 }
2561
2562 int
2563 nv50_display_create(struct drm_device *dev)
2564 {
2565 struct nvif_device *device = &nouveau_drm(dev)->client.device;
2566 struct nouveau_drm *drm = nouveau_drm(dev);
2567 struct dcb_table *dcb = &drm->vbios.dcb;
2568 struct drm_connector *connector, *tmp;
2569 struct nv50_disp *disp;
2570 struct dcb_output *dcbe;
2571 int crtcs, ret, i;
2572 bool has_mst = nv50_has_mst(drm);
2573
2574 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2575 if (!disp)
2576 return -ENOMEM;
2577
2578 mutex_init(&disp->mutex);
2579
2580 nouveau_display(dev)->priv = disp;
2581 nouveau_display(dev)->dtor = nv50_display_destroy;
2582 nouveau_display(dev)->init = nv50_display_init;
2583 nouveau_display(dev)->fini = nv50_display_fini;
2584 disp->disp = &nouveau_display(dev)->disp;
2585 dev->mode_config.funcs = &nv50_disp_func;
2586 dev->mode_config.helper_private = &nv50_disp_helper_func;
2587 dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2588 dev->mode_config.normalize_zpos = true;
2589
2590 /* small shared memory area we use for notifiers and semaphores */
2591 ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
2592 NOUVEAU_GEM_DOMAIN_VRAM,
2593 0, 0x0000, NULL, NULL, &disp->sync);
2594 if (!ret) {
2595 ret = nouveau_bo_pin(disp->sync, NOUVEAU_GEM_DOMAIN_VRAM, true);
2596 if (!ret) {
2597 ret = nouveau_bo_map(disp->sync);
2598 if (ret)
2599 nouveau_bo_unpin(disp->sync);
2600 }
2601 if (ret)
2602 nouveau_bo_ref(NULL, &disp->sync);
2603 }
2604
2605 if (ret)
2606 goto out;
2607
2608 /* allocate master evo channel */
2609 ret = nv50_core_new(drm, &disp->core);
2610 if (ret)
2611 goto out;
2612
2613 disp->core->func->init(disp->core);
2614 if (disp->core->func->caps_init) {
2615 ret = disp->core->func->caps_init(drm, disp);
2616 if (ret)
2617 goto out;
2618 }
2619
2620 /* Assign the correct format modifiers */
2621 if (disp->disp->object.oclass >= TU102_DISP)
2622 nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
2623 else
2624 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
2625 nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
2626 else
2627 nouveau_display(dev)->format_modifiers = disp50xx_modifiers;
2628
2629 /* FIXME: 256x256 cursors are supported on Kepler, however unlike Maxwell and later
2630 * generations Kepler requires that we use small pages (4K) for cursor scanout surfaces. The
2631 * proper fix for this is to teach nouveau to migrate fbs being used for the cursor plane to
2632 * small page allocations in prepare_fb(). When this is implemented, we should also force
2633 * large pages (128K) for ovly fbs in order to fix Kepler ovlys.
2634 * But until then, just limit cursors to 128x128 - which is small enough to avoid ever using
2635 * large pages.
2636 */
2637 if (disp->disp->object.oclass >= GM107_DISP) {
2638 dev->mode_config.cursor_width = 256;
2639 dev->mode_config.cursor_height = 256;
2640 } else if (disp->disp->object.oclass >= GK104_DISP) {
2641 dev->mode_config.cursor_width = 128;
2642 dev->mode_config.cursor_height = 128;
2643 } else {
2644 dev->mode_config.cursor_width = 64;
2645 dev->mode_config.cursor_height = 64;
2646 }
2647
2648 /* create crtc objects to represent the hw heads */
2649 if (disp->disp->object.oclass >= GV100_DISP)
2650 crtcs = nvif_rd32(&device->object, 0x610060) & 0xff;
2651 else
2652 if (disp->disp->object.oclass >= GF110_DISP)
2653 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
2654 else
2655 crtcs = 0x3;
2656
2657 for (i = 0; i < fls(crtcs); i++) {
2658 struct nv50_head *head;
2659
2660 if (!(crtcs & (1 << i)))
2661 continue;
2662
2663 head = nv50_head_create(dev, i);
2664 if (IS_ERR(head)) {
2665 ret = PTR_ERR(head);
2666 goto out;
2667 }
2668
2669 if (has_mst) {
2670 head->msto = nv50_msto_new(dev, head, i);
2671 if (IS_ERR(head->msto)) {
2672 ret = PTR_ERR(head->msto);
2673 head->msto = NULL;
2674 goto out;
2675 }
2676
2677 /*
2678 * FIXME: This is a hack to workaround the following
2679 * issues:
2680 *
2681 * https://gitlab.gnome.org/GNOME/mutter/issues/759
2682 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
2683 *
2684 * Once these issues are closed, this should be
2685 * removed
2686 */
2687 head->msto->encoder.possible_crtcs = crtcs;
2688 }
2689 }
2690
2691 /* create encoder/connector objects based on VBIOS DCB table */
2692 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2693 connector = nouveau_connector_create(dev, dcbe);
2694 if (IS_ERR(connector))
2695 continue;
2696
2697 if (dcbe->location == DCB_LOC_ON_CHIP) {
2698 switch (dcbe->type) {
2699 case DCB_OUTPUT_TMDS:
2700 case DCB_OUTPUT_LVDS:
2701 case DCB_OUTPUT_DP:
2702 ret = nv50_sor_create(connector, dcbe);
2703 break;
2704 case DCB_OUTPUT_ANALOG:
2705 ret = nv50_dac_create(connector, dcbe);
2706 break;
2707 default:
2708 ret = -ENODEV;
2709 break;
2710 }
2711 } else {
2712 ret = nv50_pior_create(connector, dcbe);
2713 }
2714
2715 if (ret) {
2716 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2717 dcbe->location, dcbe->type,
2718 ffs(dcbe->or) - 1, ret);
2719 ret = 0;
2720 }
2721 }
2722
2723 /* cull any connectors we created that don't have an encoder */
2724 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2725 if (connector->possible_encoders)
2726 continue;
2727
2728 NV_WARN(drm, "%s has no encoders, removing\n",
2729 connector->name);
2730 connector->funcs->destroy(connector);
2731 }
2732
2733 /* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
2734 dev->vblank_disable_immediate = true;
2735
2736 nv50_audio_component_init(drm);
2737
2738 out:
2739 if (ret)
2740 nv50_display_destroy(dev);
2741 return ret;
2742 }
2743
2744 /******************************************************************************
2745 * Format modifiers
2746 *****************************************************************************/
2747
2748 /****************************************************************
2749 * Log2(block height) ----------------------------+ *
2750 * Page Kind ----------------------------------+ | *
2751 * Gob Height/Page Kind Generation ------+ | | *
2752 * Sector layout -------+ | | | *
2753 * Compression ------+ | | | | */
2754 const u64 disp50xx_modifiers[] = { /* | | | | | */
2755 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
2756 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
2757 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
2758 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
2759 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
2760 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
2761 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
2762 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
2763 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
2764 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
2765 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
2766 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
2767 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
2768 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
2769 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
2770 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
2771 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
2772 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
2773 DRM_FORMAT_MOD_LINEAR,
2774 DRM_FORMAT_MOD_INVALID
2775 };
2776
2777 /****************************************************************
2778 * Log2(block height) ----------------------------+ *
2779 * Page Kind ----------------------------------+ | *
2780 * Gob Height/Page Kind Generation ------+ | | *
2781 * Sector layout -------+ | | | *
2782 * Compression ------+ | | | | */
2783 const u64 disp90xx_modifiers[] = { /* | | | | | */
2784 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
2785 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
2786 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
2787 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
2788 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
2789 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
2790 DRM_FORMAT_MOD_LINEAR,
2791 DRM_FORMAT_MOD_INVALID
2792 };