2 * Copyright 2011 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
31 #include <linux/dma-mapping.h>
32 #include <linux/hdmi.h>
33 #include <linux/component.h>
35 #include <drm/drm_atomic_helper.h>
36 #include <drm/drm_dp_helper.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_fb_helper.h>
39 #include <drm/drm_plane_helper.h>
40 #include <drm/drm_probe_helper.h>
41 #include <drm/drm_scdc_helper.h>
42 #include <drm/drm_vblank.h>
44 #include <nvif/push507c.h>
46 #include <nvif/class.h>
47 #include <nvif/cl0002.h>
48 #include <nvif/cl5070.h>
49 #include <nvif/cl507d.h>
50 #include <nvif/event.h>
51 #include <nvif/timer.h>
53 #include <nvhw/class/cl507c.h>
54 #include <nvhw/class/cl507d.h>
55 #include <nvhw/class/cl837d.h>
56 #include <nvhw/class/cl887d.h>
57 #include <nvhw/class/cl907d.h>
58 #include <nvhw/class/cl917d.h>
60 #include "nouveau_drv.h"
61 #include "nouveau_dma.h"
62 #include "nouveau_gem.h"
63 #include "nouveau_connector.h"
64 #include "nouveau_encoder.h"
65 #include "nouveau_fence.h"
66 #include "nouveau_fbcon.h"
68 #include <subdev/bios/dp.h>
70 /******************************************************************************
72 *****************************************************************************/
75 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
76 const s32 *oclass, u8 head, void *data, u32 size,
77 struct nv50_chan *chan)
79 struct nvif_sclass *sclass;
82 chan->device = device;
84 ret = n = nvif_object_sclass_get(disp, &sclass);
89 for (i = 0; i < n; i++) {
90 if (sclass[i].oclass == oclass[0]) {
91 ret = nvif_object_ctor(disp, "kmsChan", 0,
92 oclass[0], data, size,
95 nvif_object_map(&chan->user, NULL, 0);
96 nvif_object_sclass_put(&sclass);
103 nvif_object_sclass_put(&sclass);
108 nv50_chan_destroy(struct nv50_chan *chan)
110 nvif_object_dtor(&chan->user);
113 /******************************************************************************
115 *****************************************************************************/
118 nv50_dmac_destroy(struct nv50_dmac *dmac)
120 nvif_object_dtor(&dmac->vram);
121 nvif_object_dtor(&dmac->sync);
123 nv50_chan_destroy(&dmac->base);
125 nvif_mem_dtor(&dmac->_push.mem);
129 nv50_dmac_kick(struct nvif_push *push)
131 struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
133 dmac->cur = push->cur - (u32 *)dmac->_push.mem.object.map.ptr;
134 if (dmac->put != dmac->cur) {
135 /* Push buffer fetches are not coherent with BAR1, we need to ensure
136 * writes have been flushed right through to VRAM before writing PUT.
138 if (dmac->push->mem.type & NVIF_MEM_VRAM) {
139 struct nvif_device *device = dmac->base.device;
140 nvif_wr32(&device->object, 0x070000, 0x00000001);
141 nvif_msec(device, 2000,
142 if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
147 NVIF_WV32(&dmac->base.user, NV507C, PUT, PTR, dmac->cur);
148 dmac->put = dmac->cur;
151 push->bgn = push->cur;
155 nv50_dmac_free(struct nv50_dmac *dmac)
157 u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
158 if (get > dmac->cur) /* NVIDIA stay 5 away from GET, do the same. */
159 return get - dmac->cur - 5;
160 return dmac->max - dmac->cur;
164 nv50_dmac_wind(struct nv50_dmac *dmac)
166 /* Wait for GET to depart from the beginning of the push buffer to
167 * prevent writing PUT == GET, which would be ignored by HW.
169 u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
171 /* Corner-case, HW idle, but non-committed work pending. */
173 nv50_dmac_kick(dmac->push);
175 if (nvif_msec(dmac->base.device, 2000,
176 if (NVIF_TV32(&dmac->base.user, NV507C, GET, PTR, >, 0))
182 PUSH_RSVD(dmac->push, PUSH_JUMP(dmac->push, 0));
188 nv50_dmac_wait(struct nvif_push *push, u32 size)
190 struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
193 if (WARN_ON(size > dmac->max))
196 dmac->cur = push->cur - (u32 *)dmac->_push.mem.object.map.ptr;
197 if (dmac->cur + size >= dmac->max) {
198 int ret = nv50_dmac_wind(dmac);
202 push->cur = dmac->_push.mem.object.map.ptr;
203 push->cur = push->cur + dmac->cur;
204 nv50_dmac_kick(push);
207 if (nvif_msec(dmac->base.device, 2000,
208 if ((free = nv50_dmac_free(dmac)) >= size)
215 push->bgn = dmac->_push.mem.object.map.ptr;
216 push->bgn = push->bgn + dmac->cur;
217 push->cur = push->bgn;
218 push->end = push->cur + free;
223 nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
224 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
225 struct nv50_dmac *dmac)
227 struct nouveau_cli *cli = (void *)device->object.client;
228 struct nv50_disp_core_channel_dma_v0 *args = data;
229 u8 type = NVIF_MEM_COHERENT;
232 mutex_init(&dmac->lock);
234 /* Pascal added support for 47-bit physical addresses, but some
235 * parts of EVO still only accept 40-bit PAs.
237 * To avoid issues on systems with large amounts of RAM, and on
238 * systems where an IOMMU maps pages at a high address, we need
239 * to allocate push buffers in VRAM instead.
241 * This appears to match NVIDIA's behaviour on Pascal.
243 if (device->info.family == NV_DEVICE_INFO_V0_PASCAL)
244 type |= NVIF_MEM_VRAM;
246 ret = nvif_mem_ctor_map(&cli->mmu, "kmsChanPush", type, 0x1000,
251 dmac->ptr = dmac->_push.mem.object.map.ptr;
252 dmac->_push.wait = nv50_dmac_wait;
253 dmac->_push.kick = nv50_dmac_kick;
254 dmac->push = &dmac->_push;
255 dmac->push->bgn = dmac->_push.mem.object.map.ptr;
256 dmac->push->cur = dmac->push->bgn;
257 dmac->push->end = dmac->push->bgn;
258 dmac->max = 0x1000/4 - 1;
260 args->pushbuf = nvif_handle(&dmac->_push.mem.object);
262 ret = nv50_chan_create(device, disp, oclass, head, data, size,
270 ret = nvif_object_ctor(&dmac->base.user, "kmsSyncCtxDma", NV50_DISP_HANDLE_SYNCBUF,
272 &(struct nv_dma_v0) {
273 .target = NV_DMA_V0_TARGET_VRAM,
274 .access = NV_DMA_V0_ACCESS_RDWR,
275 .start = syncbuf + 0x0000,
276 .limit = syncbuf + 0x0fff,
277 }, sizeof(struct nv_dma_v0),
282 ret = nvif_object_ctor(&dmac->base.user, "kmsVramCtxDma", NV50_DISP_HANDLE_VRAM,
284 &(struct nv_dma_v0) {
285 .target = NV_DMA_V0_TARGET_VRAM,
286 .access = NV_DMA_V0_ACCESS_RDWR,
288 .limit = device->info.ram_user - 1,
289 }, sizeof(struct nv_dma_v0),
297 /******************************************************************************
298 * Output path helpers
299 *****************************************************************************/
301 nv50_outp_release(struct nouveau_encoder *nv_encoder)
303 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
305 struct nv50_disp_mthd_v1 base;
308 .base.method = NV50_DISP_MTHD_V1_RELEASE,
309 .base.hasht = nv_encoder->dcb->hasht,
310 .base.hashm = nv_encoder->dcb->hashm,
313 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
315 nv_encoder->link = 0;
319 nv50_outp_acquire(struct nouveau_encoder *nv_encoder, bool hda)
321 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
322 struct nv50_disp *disp = nv50_disp(drm->dev);
324 struct nv50_disp_mthd_v1 base;
325 struct nv50_disp_acquire_v0 info;
328 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
329 .base.hasht = nv_encoder->dcb->hasht,
330 .base.hashm = nv_encoder->dcb->hashm,
335 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
337 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
341 nv_encoder->or = args.info.or;
342 nv_encoder->link = args.info.link;
347 nv50_outp_atomic_check_view(struct drm_encoder *encoder,
348 struct drm_crtc_state *crtc_state,
349 struct drm_connector_state *conn_state,
350 struct drm_display_mode *native_mode)
352 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
353 struct drm_display_mode *mode = &crtc_state->mode;
354 struct drm_connector *connector = conn_state->connector;
355 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
356 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
358 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
359 asyc->scaler.full = false;
363 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
364 switch (connector->connector_type) {
365 case DRM_MODE_CONNECTOR_LVDS:
366 case DRM_MODE_CONNECTOR_eDP:
367 /* Don't force scaler for EDID modes with
368 * same size as the native one (e.g. different
371 if (mode->hdisplay == native_mode->hdisplay &&
372 mode->vdisplay == native_mode->vdisplay &&
373 mode->type & DRM_MODE_TYPE_DRIVER)
376 asyc->scaler.full = true;
385 if (!drm_mode_equal(adjusted_mode, mode)) {
386 drm_mode_copy(adjusted_mode, mode);
387 crtc_state->mode_changed = true;
394 nv50_outp_atomic_check(struct drm_encoder *encoder,
395 struct drm_crtc_state *crtc_state,
396 struct drm_connector_state *conn_state)
398 struct drm_connector *connector = conn_state->connector;
399 struct nouveau_connector *nv_connector = nouveau_connector(connector);
400 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
403 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
404 nv_connector->native_mode);
408 if (crtc_state->mode_changed || crtc_state->connectors_changed)
409 asyh->or.bpc = connector->display_info.bpc;
414 struct nouveau_connector *
415 nv50_outp_get_new_connector(struct nouveau_encoder *outp,
416 struct drm_atomic_state *state)
418 struct drm_connector *connector;
419 struct drm_connector_state *connector_state;
420 struct drm_encoder *encoder = to_drm_encoder(outp);
423 for_each_new_connector_in_state(state, connector, connector_state, i) {
424 if (connector_state->best_encoder == encoder)
425 return nouveau_connector(connector);
431 struct nouveau_connector *
432 nv50_outp_get_old_connector(struct nouveau_encoder *outp,
433 struct drm_atomic_state *state)
435 struct drm_connector *connector;
436 struct drm_connector_state *connector_state;
437 struct drm_encoder *encoder = to_drm_encoder(outp);
440 for_each_old_connector_in_state(state, connector, connector_state, i) {
441 if (connector_state->best_encoder == encoder)
442 return nouveau_connector(connector);
448 /******************************************************************************
450 *****************************************************************************/
452 nv50_dac_disable(struct drm_encoder *encoder)
454 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
455 struct nv50_core *core = nv50_disp(encoder->dev)->core;
456 const u32 ctrl = NVDEF(NV507D, DAC_SET_CONTROL, OWNER, NONE);
457 if (nv_encoder->crtc)
458 core->func->dac->ctrl(core, nv_encoder->or, ctrl, NULL);
459 nv_encoder->crtc = NULL;
460 nv50_outp_release(nv_encoder);
464 nv50_dac_enable(struct drm_encoder *encoder)
466 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
467 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
468 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
469 struct nv50_core *core = nv50_disp(encoder->dev)->core;
472 switch (nv_crtc->index) {
473 case 0: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD0); break;
474 case 1: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD1); break;
475 case 2: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD2); break;
476 case 3: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD3); break;
482 ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, PROTOCOL, RGB_CRT);
484 nv50_outp_acquire(nv_encoder, false);
486 core->func->dac->ctrl(core, nv_encoder->or, ctrl, asyh);
489 nv_encoder->crtc = encoder->crtc;
492 static enum drm_connector_status
493 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
495 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
496 struct nv50_disp *disp = nv50_disp(encoder->dev);
498 struct nv50_disp_mthd_v1 base;
499 struct nv50_disp_dac_load_v0 load;
502 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
503 .base.hasht = nv_encoder->dcb->hasht,
504 .base.hashm = nv_encoder->dcb->hashm,
508 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
509 if (args.load.data == 0)
510 args.load.data = 340;
512 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
513 if (ret || !args.load.load)
514 return connector_status_disconnected;
516 return connector_status_connected;
519 static const struct drm_encoder_helper_funcs
521 .atomic_check = nv50_outp_atomic_check,
522 .enable = nv50_dac_enable,
523 .disable = nv50_dac_disable,
524 .detect = nv50_dac_detect
528 nv50_dac_destroy(struct drm_encoder *encoder)
530 drm_encoder_cleanup(encoder);
534 static const struct drm_encoder_funcs
536 .destroy = nv50_dac_destroy,
540 nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
542 struct nouveau_drm *drm = nouveau_drm(connector->dev);
543 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
544 struct nvkm_i2c_bus *bus;
545 struct nouveau_encoder *nv_encoder;
546 struct drm_encoder *encoder;
547 int type = DRM_MODE_ENCODER_DAC;
549 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
552 nv_encoder->dcb = dcbe;
554 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
556 nv_encoder->i2c = &bus->i2c;
558 encoder = to_drm_encoder(nv_encoder);
559 encoder->possible_crtcs = dcbe->heads;
560 encoder->possible_clones = 0;
561 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
562 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
563 drm_encoder_helper_add(encoder, &nv50_dac_help);
565 drm_connector_attach_encoder(connector, encoder);
570 * audio component binding for ELD notification
573 nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
576 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
577 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
582 nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
583 bool *enabled, unsigned char *buf, int max_bytes)
585 struct drm_device *drm_dev = dev_get_drvdata(kdev);
586 struct nouveau_drm *drm = nouveau_drm(drm_dev);
587 struct drm_encoder *encoder;
588 struct nouveau_encoder *nv_encoder;
589 struct drm_connector *connector;
590 struct nouveau_crtc *nv_crtc;
591 struct drm_connector_list_iter conn_iter;
596 drm_for_each_encoder(encoder, drm->dev) {
597 struct nouveau_connector *nv_connector = NULL;
599 nv_encoder = nouveau_encoder(encoder);
601 drm_connector_list_iter_begin(drm_dev, &conn_iter);
602 drm_for_each_connector_iter(connector, &conn_iter) {
603 if (connector->state->best_encoder == encoder) {
604 nv_connector = nouveau_connector(connector);
608 drm_connector_list_iter_end(&conn_iter);
612 nv_crtc = nouveau_crtc(encoder->crtc);
613 if (!nv_crtc || nv_encoder->or != port ||
614 nv_crtc->index != dev_id)
616 *enabled = nv_encoder->audio;
618 ret = drm_eld_size(nv_connector->base.eld);
619 memcpy(buf, nv_connector->base.eld,
620 min(max_bytes, ret));
628 static const struct drm_audio_component_ops nv50_audio_component_ops = {
629 .get_eld = nv50_audio_component_get_eld,
633 nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
636 struct drm_device *drm_dev = dev_get_drvdata(kdev);
637 struct nouveau_drm *drm = nouveau_drm(drm_dev);
638 struct drm_audio_component *acomp = data;
640 if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
643 drm_modeset_lock_all(drm_dev);
644 acomp->ops = &nv50_audio_component_ops;
646 drm->audio.component = acomp;
647 drm_modeset_unlock_all(drm_dev);
652 nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
655 struct drm_device *drm_dev = dev_get_drvdata(kdev);
656 struct nouveau_drm *drm = nouveau_drm(drm_dev);
657 struct drm_audio_component *acomp = data;
659 drm_modeset_lock_all(drm_dev);
660 drm->audio.component = NULL;
663 drm_modeset_unlock_all(drm_dev);
666 static const struct component_ops nv50_audio_component_bind_ops = {
667 .bind = nv50_audio_component_bind,
668 .unbind = nv50_audio_component_unbind,
672 nv50_audio_component_init(struct nouveau_drm *drm)
674 if (!component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
675 drm->audio.component_registered = true;
679 nv50_audio_component_fini(struct nouveau_drm *drm)
681 if (drm->audio.component_registered) {
682 component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
683 drm->audio.component_registered = false;
687 /******************************************************************************
689 *****************************************************************************/
691 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
693 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
694 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
695 struct nv50_disp *disp = nv50_disp(encoder->dev);
697 struct nv50_disp_mthd_v1 base;
698 struct nv50_disp_sor_hda_eld_v0 eld;
701 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
702 .base.hasht = nv_encoder->dcb->hasht,
703 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
704 (0x0100 << nv_crtc->index),
707 if (!nv_encoder->audio)
710 nv_encoder->audio = false;
711 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
713 nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
718 nv50_audio_enable(struct drm_encoder *encoder, struct drm_atomic_state *state,
719 struct drm_display_mode *mode)
721 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
722 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
723 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
724 struct nouveau_connector *nv_connector;
725 struct nv50_disp *disp = nv50_disp(encoder->dev);
728 struct nv50_disp_mthd_v1 mthd;
729 struct nv50_disp_sor_hda_eld_v0 eld;
731 u8 data[sizeof(nv_connector->base.eld)];
733 .base.mthd.version = 1,
734 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
735 .base.mthd.hasht = nv_encoder->dcb->hasht,
736 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
737 (0x0100 << nv_crtc->index),
740 nv_connector = nv50_outp_get_new_connector(nv_encoder, state);
741 if (!drm_detect_monitor_audio(nv_connector->edid))
744 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
746 nvif_mthd(&disp->disp->object, 0, &args,
747 sizeof(args.base) + drm_eld_size(args.data));
748 nv_encoder->audio = true;
750 nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
754 /******************************************************************************
756 *****************************************************************************/
758 nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
760 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
761 struct nv50_disp *disp = nv50_disp(encoder->dev);
763 struct nv50_disp_mthd_v1 base;
764 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
767 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
768 .base.hasht = nv_encoder->dcb->hasht,
769 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
770 (0x0100 << nv_crtc->index),
773 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
777 nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_atomic_state *state,
778 struct drm_display_mode *mode)
780 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
781 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
782 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
783 struct nv50_disp *disp = nv50_disp(encoder->dev);
785 struct nv50_disp_mthd_v1 base;
786 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
787 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
790 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
791 .base.hasht = nv_encoder->dcb->hasht,
792 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
793 (0x0100 << nv_crtc->index),
795 .pwr.rekey = 56, /* binary driver, and tegra, constant */
797 struct nouveau_connector *nv_connector;
798 struct drm_hdmi_info *hdmi;
800 union hdmi_infoframe avi_frame;
801 union hdmi_infoframe vendor_frame;
802 bool high_tmds_clock_ratio = false, scrambling = false;
807 nv_connector = nv50_outp_get_new_connector(nv_encoder, state);
808 if (!drm_detect_hdmi_monitor(nv_connector->edid))
811 hdmi = &nv_connector->base.display_info.hdmi;
813 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi,
814 &nv_connector->base, mode);
816 /* We have an AVI InfoFrame, populate it to the display */
817 args.pwr.avi_infoframe_length
818 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
821 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
822 &nv_connector->base, mode);
824 /* We have a Vendor InfoFrame, populate it to the display */
825 args.pwr.vendor_infoframe_length
826 = hdmi_infoframe_pack(&vendor_frame,
828 + args.pwr.avi_infoframe_length,
832 max_ac_packet = mode->htotal - mode->hdisplay;
833 max_ac_packet -= args.pwr.rekey;
834 max_ac_packet -= 18; /* constant from tegra */
835 args.pwr.max_ac_packet = max_ac_packet / 32;
837 if (hdmi->scdc.scrambling.supported) {
838 high_tmds_clock_ratio = mode->clock > 340000;
839 scrambling = high_tmds_clock_ratio ||
840 hdmi->scdc.scrambling.low_rates;
844 NV50_DISP_SOR_HDMI_PWR_V0_SCDC_SCRAMBLE * scrambling |
845 NV50_DISP_SOR_HDMI_PWR_V0_SCDC_DIV_BY_4 * high_tmds_clock_ratio;
847 size = sizeof(args.base)
849 + args.pwr.avi_infoframe_length
850 + args.pwr.vendor_infoframe_length;
851 nvif_mthd(&disp->disp->object, 0, &args, size);
853 nv50_audio_enable(encoder, state, mode);
855 /* If SCDC is supported by the downstream monitor, update
856 * divider / scrambling settings to what we programmed above.
858 if (!hdmi->scdc.scrambling.supported)
861 ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &config);
863 NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
866 config &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
867 config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 * high_tmds_clock_ratio;
868 config |= SCDC_SCRAMBLING_ENABLE * scrambling;
869 ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, config);
871 NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
875 /******************************************************************************
877 *****************************************************************************/
878 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
879 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
880 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
883 struct nv50_mstm *mstm;
884 struct drm_dp_mst_port *port;
885 struct drm_connector connector;
887 struct drm_display_mode *native;
892 struct drm_encoder encoder;
894 struct nv50_head *head;
895 struct nv50_mstc *mstc;
899 struct nouveau_encoder *nv50_real_outp(struct drm_encoder *encoder)
901 struct nv50_msto *msto;
903 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
904 return nouveau_encoder(encoder);
906 msto = nv50_msto(encoder);
909 return msto->mstc->mstm->outp;
912 static struct drm_dp_payload *
913 nv50_msto_payload(struct nv50_msto *msto)
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 int vcpi = mstc->port->vcpi.vcpi, i;
920 WARN_ON(!mutex_is_locked(&mstm->mgr.payload_lock));
922 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
923 for (i = 0; i < mstm->mgr.max_payloads; i++) {
924 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
925 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
926 mstm->outp->base.base.name, i, payload->vcpi,
927 payload->start_slot, payload->num_slots);
930 for (i = 0; i < mstm->mgr.max_payloads; i++) {
931 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
932 if (payload->vcpi == vcpi)
940 nv50_msto_cleanup(struct nv50_msto *msto)
942 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
943 struct nv50_mstc *mstc = msto->mstc;
944 struct nv50_mstm *mstm = mstc->mstm;
949 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
951 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
954 msto->disabled = false;
958 nv50_msto_prepare(struct nv50_msto *msto)
960 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
961 struct nv50_mstc *mstc = msto->mstc;
962 struct nv50_mstm *mstm = mstc->mstm;
964 struct nv50_disp_mthd_v1 base;
965 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
968 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
969 .base.hasht = mstm->outp->dcb->hasht,
970 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
971 (0x0100 << msto->head->base.index),
974 mutex_lock(&mstm->mgr.payload_lock);
976 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
977 if (mstc->port->vcpi.vcpi > 0) {
978 struct drm_dp_payload *payload = nv50_msto_payload(msto);
980 args.vcpi.start_slot = payload->start_slot;
981 args.vcpi.num_slots = payload->num_slots;
982 args.vcpi.pbn = mstc->port->vcpi.pbn;
983 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
987 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
988 msto->encoder.name, msto->head->base.base.name,
989 args.vcpi.start_slot, args.vcpi.num_slots,
990 args.vcpi.pbn, args.vcpi.aligned_pbn);
992 nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
993 mutex_unlock(&mstm->mgr.payload_lock);
997 nv50_msto_atomic_check(struct drm_encoder *encoder,
998 struct drm_crtc_state *crtc_state,
999 struct drm_connector_state *conn_state)
1001 struct drm_atomic_state *state = crtc_state->state;
1002 struct drm_connector *connector = conn_state->connector;
1003 struct nv50_mstc *mstc = nv50_mstc(connector);
1004 struct nv50_mstm *mstm = mstc->mstm;
1005 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
1009 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
1014 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
1018 * When restoring duplicated states, we need to make sure that the bw
1019 * remains the same and avoid recalculating it, as the connector's bpc
1020 * may have changed after the state was duplicated
1022 if (!state->duplicated) {
1023 const int clock = crtc_state->adjusted_mode.clock;
1025 asyh->or.bpc = connector->display_info.bpc;
1026 asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3,
1030 slots = drm_dp_atomic_find_vcpi_slots(state, &mstm->mgr, mstc->port,
1035 asyh->dp.tu = slots;
1041 nv50_dp_bpc_to_depth(unsigned int bpc)
1044 case 6: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444;
1045 case 8: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444;
1047 default: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444;
1052 nv50_msto_enable(struct drm_encoder *encoder)
1054 struct nv50_head *head = nv50_head(encoder->crtc);
1055 struct nv50_head_atom *armh = nv50_head_atom(head->base.base.state);
1056 struct nv50_msto *msto = nv50_msto(encoder);
1057 struct nv50_mstc *mstc = NULL;
1058 struct nv50_mstm *mstm = NULL;
1059 struct drm_connector *connector;
1060 struct drm_connector_list_iter conn_iter;
1064 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
1065 drm_for_each_connector_iter(connector, &conn_iter) {
1066 if (connector->state->best_encoder == &msto->encoder) {
1067 mstc = nv50_mstc(connector);
1072 drm_connector_list_iter_end(&conn_iter);
1077 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, armh->dp.pbn,
1080 DRM_DEBUG_KMS("Failed to allocate VCPI\n");
1083 nv50_outp_acquire(mstm->outp, false /*XXX: MST audio.*/);
1085 if (mstm->outp->link & 1)
1086 proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1088 proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1090 mstm->outp->update(mstm->outp, head->base.index, armh, proto,
1091 nv50_dp_bpc_to_depth(armh->or.bpc));
1094 mstm->modified = true;
1098 nv50_msto_disable(struct drm_encoder *encoder)
1100 struct nv50_msto *msto = nv50_msto(encoder);
1101 struct nv50_mstc *mstc = msto->mstc;
1102 struct nv50_mstm *mstm = mstc->mstm;
1104 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
1106 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
1107 mstm->modified = true;
1109 mstm->disabled = true;
1110 msto->disabled = true;
1113 static const struct drm_encoder_helper_funcs
1115 .disable = nv50_msto_disable,
1116 .enable = nv50_msto_enable,
1117 .atomic_check = nv50_msto_atomic_check,
1121 nv50_msto_destroy(struct drm_encoder *encoder)
1123 struct nv50_msto *msto = nv50_msto(encoder);
1124 drm_encoder_cleanup(&msto->encoder);
1128 static const struct drm_encoder_funcs
1130 .destroy = nv50_msto_destroy,
1133 static struct nv50_msto *
1134 nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1136 struct nv50_msto *msto;
1139 msto = kzalloc(sizeof(*msto), GFP_KERNEL);
1141 return ERR_PTR(-ENOMEM);
1143 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1144 DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1147 return ERR_PTR(ret);
1150 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1151 msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
1156 static struct drm_encoder *
1157 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1158 struct drm_connector_state *connector_state)
1160 struct nv50_mstc *mstc = nv50_mstc(connector);
1161 struct drm_crtc *crtc = connector_state->crtc;
1163 if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1166 return &nv50_head(crtc)->msto->encoder;
1169 static enum drm_mode_status
1170 nv50_mstc_mode_valid(struct drm_connector *connector,
1171 struct drm_display_mode *mode)
1173 struct nv50_mstc *mstc = nv50_mstc(connector);
1174 struct nouveau_encoder *outp = mstc->mstm->outp;
1176 /* TODO: calculate the PBN from the dotclock and validate against the
1177 * MSTB's max possible PBN
1180 return nv50_dp_mode_valid(connector, outp, mode, NULL);
1184 nv50_mstc_get_modes(struct drm_connector *connector)
1186 struct nv50_mstc *mstc = nv50_mstc(connector);
1189 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1190 drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1192 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
1195 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
1196 * to 8 to save bandwidth on the topology. In the future, we'll want
1197 * to properly fix this by dynamically selecting the highest possible
1198 * bpc that would fit in the topology
1200 if (connector->display_info.bpc)
1201 connector->display_info.bpc =
1202 clamp(connector->display_info.bpc, 6U, 8U);
1204 connector->display_info.bpc = 8;
1207 drm_mode_destroy(mstc->connector.dev, mstc->native);
1208 mstc->native = nouveau_conn_native_mode(&mstc->connector);
1213 nv50_mstc_atomic_check(struct drm_connector *connector,
1214 struct drm_atomic_state *state)
1216 struct nv50_mstc *mstc = nv50_mstc(connector);
1217 struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;
1218 struct drm_connector_state *new_conn_state =
1219 drm_atomic_get_new_connector_state(state, connector);
1220 struct drm_connector_state *old_conn_state =
1221 drm_atomic_get_old_connector_state(state, connector);
1222 struct drm_crtc_state *crtc_state;
1223 struct drm_crtc *new_crtc = new_conn_state->crtc;
1225 if (!old_conn_state->crtc)
1228 /* We only want to free VCPI if this state disables the CRTC on this
1232 crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
1235 !drm_atomic_crtc_needs_modeset(crtc_state) ||
1240 return drm_dp_atomic_release_vcpi_slots(state, mgr, mstc->port);
1244 nv50_mstc_detect(struct drm_connector *connector,
1245 struct drm_modeset_acquire_ctx *ctx, bool force)
1247 struct nv50_mstc *mstc = nv50_mstc(connector);
1250 if (drm_connector_is_unregistered(connector))
1251 return connector_status_disconnected;
1253 ret = pm_runtime_get_sync(connector->dev->dev);
1254 if (ret < 0 && ret != -EACCES) {
1255 pm_runtime_put_autosuspend(connector->dev->dev);
1256 return connector_status_disconnected;
1259 ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
1262 pm_runtime_mark_last_busy(connector->dev->dev);
1263 pm_runtime_put_autosuspend(connector->dev->dev);
1267 static const struct drm_connector_helper_funcs
1269 .get_modes = nv50_mstc_get_modes,
1270 .mode_valid = nv50_mstc_mode_valid,
1271 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
1272 .atomic_check = nv50_mstc_atomic_check,
1273 .detect_ctx = nv50_mstc_detect,
1277 nv50_mstc_destroy(struct drm_connector *connector)
1279 struct nv50_mstc *mstc = nv50_mstc(connector);
1281 drm_connector_cleanup(&mstc->connector);
1282 drm_dp_mst_put_port_malloc(mstc->port);
1287 static const struct drm_connector_funcs
1289 .reset = nouveau_conn_reset,
1290 .fill_modes = drm_helper_probe_single_connector_modes,
1291 .destroy = nv50_mstc_destroy,
1292 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1293 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1294 .atomic_set_property = nouveau_conn_atomic_set_property,
1295 .atomic_get_property = nouveau_conn_atomic_get_property,
1299 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
1300 const char *path, struct nv50_mstc **pmstc)
1302 struct drm_device *dev = mstm->outp->base.base.dev;
1303 struct drm_crtc *crtc;
1304 struct nv50_mstc *mstc;
1307 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
1312 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
1313 DRM_MODE_CONNECTOR_DisplayPort);
1320 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
1322 mstc->connector.funcs->reset(&mstc->connector);
1323 nouveau_conn_attach_properties(&mstc->connector);
1325 drm_for_each_crtc(crtc, dev) {
1326 if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1329 drm_connector_attach_encoder(&mstc->connector,
1330 &nv50_head(crtc)->msto->encoder);
1333 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
1334 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1335 drm_connector_set_path_property(&mstc->connector, path);
1336 drm_dp_mst_get_port_malloc(port);
1341 nv50_mstm_cleanup(struct nv50_mstm *mstm)
1343 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1344 struct drm_encoder *encoder;
1347 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1348 ret = drm_dp_check_act_status(&mstm->mgr);
1350 ret = drm_dp_update_payload_part2(&mstm->mgr);
1352 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1353 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1354 struct nv50_msto *msto = nv50_msto(encoder);
1355 struct nv50_mstc *mstc = msto->mstc;
1356 if (mstc && mstc->mstm == mstm)
1357 nv50_msto_cleanup(msto);
1361 mstm->modified = false;
1365 nv50_mstm_prepare(struct nv50_mstm *mstm)
1367 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1368 struct drm_encoder *encoder;
1371 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
1372 ret = drm_dp_update_payload_part1(&mstm->mgr);
1374 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1375 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1376 struct nv50_msto *msto = nv50_msto(encoder);
1377 struct nv50_mstc *mstc = msto->mstc;
1378 if (mstc && mstc->mstm == mstm)
1379 nv50_msto_prepare(msto);
1383 if (mstm->disabled) {
1385 nv50_outp_release(mstm->outp);
1386 mstm->disabled = false;
1390 static struct drm_connector *
1391 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1392 struct drm_dp_mst_port *port, const char *path)
1394 struct nv50_mstm *mstm = nv50_mstm(mgr);
1395 struct nv50_mstc *mstc;
1398 ret = nv50_mstc_new(mstm, port, path, &mstc);
1402 return &mstc->connector;
1405 static const struct drm_dp_mst_topology_cbs
1407 .add_connector = nv50_mstm_add_connector,
1411 nv50_mstm_service(struct nouveau_drm *drm,
1412 struct nouveau_connector *nv_connector,
1413 struct nv50_mstm *mstm)
1415 struct drm_dp_aux *aux = &nv_connector->aux;
1416 bool handled = true, ret = true;
1421 rc = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1427 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1431 rc = drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1],
1440 NV_DEBUG(drm, "Failed to handle ESI on %s: %d\n",
1441 nv_connector->base.name, rc);
1447 nv50_mstm_remove(struct nv50_mstm *mstm)
1449 mstm->is_mst = false;
1450 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1454 nv50_mstm_enable(struct nv50_mstm *mstm, int state)
1456 struct nouveau_encoder *outp = mstm->outp;
1458 struct nv50_disp_mthd_v1 base;
1459 struct nv50_disp_sor_dp_mst_link_v0 mst;
1462 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
1463 .base.hasht = outp->dcb->hasht,
1464 .base.hashm = outp->dcb->hashm,
1467 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
1468 struct nvif_object *disp = &drm->display->disp.object;
1470 return nvif_mthd(disp, 0, &args, sizeof(args));
1474 nv50_mstm_detect(struct nouveau_encoder *outp)
1476 struct nv50_mstm *mstm = outp->dp.mstm;
1477 struct drm_dp_aux *aux;
1480 if (!mstm || !mstm->can_mst)
1483 aux = mstm->mgr.aux;
1485 /* Clear any leftover MST state we didn't set ourselves by first
1486 * disabling MST if it was already enabled
1488 ret = drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1492 /* And start enabling */
1493 ret = nv50_mstm_enable(mstm, true);
1497 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, true);
1499 nv50_mstm_enable(mstm, false);
1503 mstm->is_mst = true;
1508 nv50_mstm_fini(struct nouveau_encoder *outp)
1510 struct nv50_mstm *mstm = outp->dp.mstm;
1515 /* Don't change the MST state of this connector until we've finished
1516 * resuming, since we can't safely grab hpd_irq_lock in our resume
1517 * path to protect mstm->is_mst without potentially deadlocking
1519 mutex_lock(&outp->dp.hpd_irq_lock);
1520 mstm->suspended = true;
1521 mutex_unlock(&outp->dp.hpd_irq_lock);
1524 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1528 nv50_mstm_init(struct nouveau_encoder *outp, bool runtime)
1530 struct nv50_mstm *mstm = outp->dp.mstm;
1537 ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
1539 nv50_mstm_remove(mstm);
1542 mutex_lock(&outp->dp.hpd_irq_lock);
1543 mstm->suspended = false;
1544 mutex_unlock(&outp->dp.hpd_irq_lock);
1547 drm_kms_helper_hotplug_event(mstm->mgr.dev);
1551 nv50_mstm_del(struct nv50_mstm **pmstm)
1553 struct nv50_mstm *mstm = *pmstm;
1555 drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1562 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1563 int conn_base_id, struct nv50_mstm **pmstm)
1565 const int max_payloads = hweight8(outp->dcb->heads);
1566 struct drm_device *dev = outp->base.base.dev;
1567 struct nv50_mstm *mstm;
1570 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1573 mstm->mgr.cbs = &nv50_mstm;
1575 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1576 max_payloads, conn_base_id);
1583 /******************************************************************************
1585 *****************************************************************************/
1587 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1588 struct nv50_head_atom *asyh, u8 proto, u8 depth)
1590 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1591 struct nv50_core *core = disp->core;
1594 nv_encoder->ctrl &= ~BIT(head);
1595 if (NVDEF_TEST(nv_encoder->ctrl, NV507D, SOR_SET_CONTROL, OWNER, ==, NONE))
1596 nv_encoder->ctrl = 0;
1598 nv_encoder->ctrl |= NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto);
1599 nv_encoder->ctrl |= BIT(head);
1600 asyh->or.depth = depth;
1603 core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
1607 nv50_sor_disable(struct drm_encoder *encoder,
1608 struct drm_atomic_state *state)
1610 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1611 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1612 struct nouveau_connector *nv_connector =
1613 nv50_outp_get_old_connector(nv_encoder, state);
1615 nv_encoder->crtc = NULL;
1618 struct drm_dp_aux *aux = &nv_connector->aux;
1621 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
1622 int ret = drm_dp_dpcd_readb(aux, DP_SET_POWER, &pwr);
1625 pwr &= ~DP_SET_POWER_MASK;
1626 pwr |= DP_SET_POWER_D3;
1627 drm_dp_dpcd_writeb(aux, DP_SET_POWER, pwr);
1631 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
1632 nv50_audio_disable(encoder, nv_crtc);
1633 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
1634 nv50_outp_release(nv_encoder);
1639 nv50_sor_enable(struct drm_encoder *encoder,
1640 struct drm_atomic_state *state)
1642 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1643 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1644 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1645 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1647 struct nv50_disp_mthd_v1 base;
1648 struct nv50_disp_sor_lvds_script_v0 lvds;
1651 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1652 .base.hasht = nv_encoder->dcb->hasht,
1653 .base.hashm = nv_encoder->dcb->hashm,
1655 struct nv50_disp *disp = nv50_disp(encoder->dev);
1656 struct drm_device *dev = encoder->dev;
1657 struct nouveau_drm *drm = nouveau_drm(dev);
1658 struct nouveau_connector *nv_connector;
1659 struct nvbios *bios = &drm->vbios;
1661 u8 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_CUSTOM;
1662 u8 depth = NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT;
1664 nv_connector = nv50_outp_get_new_connector(nv_encoder, state);
1665 nv_encoder->crtc = encoder->crtc;
1667 if ((disp->disp->object.oclass == GT214_DISP ||
1668 disp->disp->object.oclass >= GF110_DISP) &&
1669 drm_detect_monitor_audio(nv_connector->edid))
1671 nv50_outp_acquire(nv_encoder, hda);
1673 switch (nv_encoder->dcb->type) {
1674 case DCB_OUTPUT_TMDS:
1675 if (nv_encoder->link & 1) {
1676 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_A;
1677 /* Only enable dual-link if:
1678 * - Need to (i.e. rate > 165MHz)
1680 * - Not an HDMI monitor, since there's no dual-link
1683 if (mode->clock >= 165000 &&
1684 nv_encoder->dcb->duallink_possible &&
1685 !drm_detect_hdmi_monitor(nv_connector->edid))
1686 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_DUAL_TMDS;
1688 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_B;
1691 nv50_hdmi_enable(&nv_encoder->base.base, state, mode);
1693 case DCB_OUTPUT_LVDS:
1694 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_LVDS_CUSTOM;
1696 if (bios->fp_no_ddc) {
1697 if (bios->fp.dual_link)
1698 lvds.lvds.script |= 0x0100;
1699 if (bios->fp.if_is_24bit)
1700 lvds.lvds.script |= 0x0200;
1702 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1703 if (((u8 *)nv_connector->edid)[121] == 2)
1704 lvds.lvds.script |= 0x0100;
1706 if (mode->clock >= bios->fp.duallink_transition_clk) {
1707 lvds.lvds.script |= 0x0100;
1710 if (lvds.lvds.script & 0x0100) {
1711 if (bios->fp.strapless_is_24bit & 2)
1712 lvds.lvds.script |= 0x0200;
1714 if (bios->fp.strapless_is_24bit & 1)
1715 lvds.lvds.script |= 0x0200;
1718 if (asyh->or.bpc == 8)
1719 lvds.lvds.script |= 0x0200;
1722 nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
1725 depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1727 if (nv_encoder->link & 1)
1728 proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1730 proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1732 nv50_audio_enable(encoder, state, mode);
1739 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1742 static const struct drm_encoder_helper_funcs
1744 .atomic_check = nv50_outp_atomic_check,
1745 .atomic_enable = nv50_sor_enable,
1746 .atomic_disable = nv50_sor_disable,
1750 nv50_sor_destroy(struct drm_encoder *encoder)
1752 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1753 nv50_mstm_del(&nv_encoder->dp.mstm);
1754 drm_encoder_cleanup(encoder);
1756 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1757 mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
1762 static const struct drm_encoder_funcs
1764 .destroy = nv50_sor_destroy,
1767 static bool nv50_has_mst(struct nouveau_drm *drm)
1769 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
1771 u8 ver, hdr, cnt, len;
1773 data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len);
1774 return data && ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04);
1778 nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
1780 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1781 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1782 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1783 struct nouveau_encoder *nv_encoder;
1784 struct drm_encoder *encoder;
1785 struct nv50_disp *disp = nv50_disp(connector->dev);
1788 switch (dcbe->type) {
1789 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1790 case DCB_OUTPUT_TMDS:
1793 type = DRM_MODE_ENCODER_TMDS;
1797 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1800 nv_encoder->dcb = dcbe;
1801 nv_encoder->update = nv50_sor_update;
1803 encoder = to_drm_encoder(nv_encoder);
1804 encoder->possible_crtcs = dcbe->heads;
1805 encoder->possible_clones = 0;
1806 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1807 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1808 drm_encoder_helper_add(encoder, &nv50_sor_help);
1810 drm_connector_attach_encoder(connector, encoder);
1812 disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1814 if (dcbe->type == DCB_OUTPUT_DP) {
1815 struct nvkm_i2c_aux *aux =
1816 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1818 mutex_init(&nv_encoder->dp.hpd_irq_lock);
1821 if (disp->disp->object.oclass < GF110_DISP) {
1822 /* HW has no support for address-only
1823 * transactions, so we're required to
1824 * use custom I2C-over-AUX code.
1826 nv_encoder->i2c = &aux->i2c;
1828 nv_encoder->i2c = &nv_connector->aux.ddc;
1830 nv_encoder->aux = aux;
1833 if (nv_connector->type != DCB_CONNECTOR_eDP &&
1834 nv50_has_mst(drm)) {
1835 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
1836 16, nv_connector->base.base.id,
1837 &nv_encoder->dp.mstm);
1842 struct nvkm_i2c_bus *bus =
1843 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1845 nv_encoder->i2c = &bus->i2c;
1851 /******************************************************************************
1853 *****************************************************************************/
1855 nv50_pior_atomic_check(struct drm_encoder *encoder,
1856 struct drm_crtc_state *crtc_state,
1857 struct drm_connector_state *conn_state)
1859 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1862 crtc_state->adjusted_mode.clock *= 2;
1867 nv50_pior_disable(struct drm_encoder *encoder)
1869 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1870 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1871 const u32 ctrl = NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, NONE);
1872 if (nv_encoder->crtc)
1873 core->func->pior->ctrl(core, nv_encoder->or, ctrl, NULL);
1874 nv_encoder->crtc = NULL;
1875 nv50_outp_release(nv_encoder);
1879 nv50_pior_enable(struct drm_encoder *encoder)
1881 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1882 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1883 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1884 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1887 switch (nv_crtc->index) {
1888 case 0: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD0); break;
1889 case 1: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD1); break;
1895 nv50_outp_acquire(nv_encoder, false);
1897 switch (asyh->or.bpc) {
1898 case 10: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444; break;
1899 case 8: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444; break;
1900 case 6: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444; break;
1901 default: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT; break;
1904 switch (nv_encoder->dcb->type) {
1905 case DCB_OUTPUT_TMDS:
1907 ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
1914 core->func->pior->ctrl(core, nv_encoder->or, ctrl, asyh);
1915 nv_encoder->crtc = encoder->crtc;
1918 static const struct drm_encoder_helper_funcs
1920 .atomic_check = nv50_pior_atomic_check,
1921 .enable = nv50_pior_enable,
1922 .disable = nv50_pior_disable,
1926 nv50_pior_destroy(struct drm_encoder *encoder)
1928 drm_encoder_cleanup(encoder);
1932 static const struct drm_encoder_funcs
1934 .destroy = nv50_pior_destroy,
1938 nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1940 struct drm_device *dev = connector->dev;
1941 struct nouveau_drm *drm = nouveau_drm(dev);
1942 struct nv50_disp *disp = nv50_disp(dev);
1943 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1944 struct nvkm_i2c_bus *bus = NULL;
1945 struct nvkm_i2c_aux *aux = NULL;
1946 struct i2c_adapter *ddc;
1947 struct nouveau_encoder *nv_encoder;
1948 struct drm_encoder *encoder;
1951 switch (dcbe->type) {
1952 case DCB_OUTPUT_TMDS:
1953 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1954 ddc = bus ? &bus->i2c : NULL;
1955 type = DRM_MODE_ENCODER_TMDS;
1958 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
1959 ddc = aux ? &aux->i2c : NULL;
1960 type = DRM_MODE_ENCODER_TMDS;
1966 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1969 nv_encoder->dcb = dcbe;
1970 nv_encoder->i2c = ddc;
1971 nv_encoder->aux = aux;
1973 encoder = to_drm_encoder(nv_encoder);
1974 encoder->possible_crtcs = dcbe->heads;
1975 encoder->possible_clones = 0;
1976 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1977 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
1978 drm_encoder_helper_add(encoder, &nv50_pior_help);
1980 drm_connector_attach_encoder(connector, encoder);
1982 disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1987 /******************************************************************************
1989 *****************************************************************************/
1992 nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
1994 struct nouveau_drm *drm = nouveau_drm(state->dev);
1995 struct nv50_disp *disp = nv50_disp(drm->dev);
1996 struct nv50_core *core = disp->core;
1997 struct nv50_mstm *mstm;
1998 struct drm_encoder *encoder;
2000 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
2002 drm_for_each_encoder(encoder, drm->dev) {
2003 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2004 mstm = nouveau_encoder(encoder)->dp.mstm;
2005 if (mstm && mstm->modified)
2006 nv50_mstm_prepare(mstm);
2010 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
2011 core->func->update(core, interlock, true);
2012 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
2013 disp->core->chan.base.device))
2014 NV_ERROR(drm, "core notifier timeout\n");
2016 drm_for_each_encoder(encoder, drm->dev) {
2017 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2018 mstm = nouveau_encoder(encoder)->dp.mstm;
2019 if (mstm && mstm->modified)
2020 nv50_mstm_cleanup(mstm);
2026 nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
2028 struct drm_plane_state *new_plane_state;
2029 struct drm_plane *plane;
2032 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2033 struct nv50_wndw *wndw = nv50_wndw(plane);
2034 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
2035 if (wndw->func->update)
2036 wndw->func->update(wndw, interlock);
2042 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
2044 struct drm_device *dev = state->dev;
2045 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
2046 struct drm_crtc *crtc;
2047 struct drm_plane_state *new_plane_state;
2048 struct drm_plane *plane;
2049 struct nouveau_drm *drm = nouveau_drm(dev);
2050 struct nv50_disp *disp = nv50_disp(dev);
2051 struct nv50_atom *atom = nv50_atom(state);
2052 struct nv50_core *core = disp->core;
2053 struct nv50_outp_atom *outp, *outt;
2054 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
2056 bool flushed = false;
2058 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
2059 nv50_crc_atomic_stop_reporting(state);
2060 drm_atomic_helper_wait_for_fences(dev, state, false);
2061 drm_atomic_helper_wait_for_dependencies(state);
2062 drm_atomic_helper_update_legacy_modeset_state(dev, state);
2064 if (atom->lock_core)
2065 mutex_lock(&disp->mutex);
2067 /* Disable head(s). */
2068 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2069 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2070 struct nv50_head *head = nv50_head(crtc);
2072 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
2073 asyh->clr.mask, asyh->set.mask);
2075 if (old_crtc_state->active && !new_crtc_state->active) {
2076 pm_runtime_put_noidle(dev->dev);
2077 drm_crtc_vblank_off(crtc);
2080 if (asyh->clr.mask) {
2081 nv50_head_flush_clr(head, asyh, atom->flush_disable);
2082 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2086 /* Disable plane(s). */
2087 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2088 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2089 struct nv50_wndw *wndw = nv50_wndw(plane);
2091 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
2092 asyw->clr.mask, asyw->set.mask);
2093 if (!asyw->clr.mask)
2096 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
2099 /* Disable output path(s). */
2100 list_for_each_entry(outp, &atom->outp, head) {
2101 const struct drm_encoder_helper_funcs *help;
2102 struct drm_encoder *encoder;
2104 encoder = outp->encoder;
2105 help = encoder->helper_private;
2107 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
2108 outp->clr.mask, outp->set.mask);
2110 if (outp->clr.mask) {
2111 help->atomic_disable(encoder, state);
2112 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2113 if (outp->flush_disable) {
2114 nv50_disp_atomic_commit_wndw(state, interlock);
2115 nv50_disp_atomic_commit_core(state, interlock);
2116 memset(interlock, 0x00, sizeof(interlock));
2123 /* Flush disable. */
2124 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2125 if (atom->flush_disable) {
2126 nv50_disp_atomic_commit_wndw(state, interlock);
2127 nv50_disp_atomic_commit_core(state, interlock);
2128 memset(interlock, 0x00, sizeof(interlock));
2135 nv50_crc_atomic_release_notifier_contexts(state);
2136 nv50_crc_atomic_init_notifier_contexts(state);
2138 /* Update output path(s). */
2139 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2140 const struct drm_encoder_helper_funcs *help;
2141 struct drm_encoder *encoder;
2143 encoder = outp->encoder;
2144 help = encoder->helper_private;
2146 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
2147 outp->set.mask, outp->clr.mask);
2149 if (outp->set.mask) {
2150 help->atomic_enable(encoder, state);
2151 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2154 list_del(&outp->head);
2158 /* Update head(s). */
2159 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2160 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2161 struct nv50_head *head = nv50_head(crtc);
2163 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2164 asyh->set.mask, asyh->clr.mask);
2166 if (asyh->set.mask) {
2167 nv50_head_flush_set(head, asyh);
2168 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2171 if (new_crtc_state->active) {
2172 if (!old_crtc_state->active) {
2173 drm_crtc_vblank_on(crtc);
2174 pm_runtime_get_noresume(dev->dev);
2176 if (new_crtc_state->event)
2177 drm_crtc_vblank_get(crtc);
2181 /* Update window->head assignment.
2183 * This has to happen in an update that's not interlocked with
2184 * any window channels to avoid hitting HW error checks.
2186 *TODO: Proper handling of window ownership (Turing apparently
2187 * supports non-fixed mappings).
2189 if (core->assign_windows) {
2190 core->func->wndw.owner(core);
2191 nv50_disp_atomic_commit_core(state, interlock);
2192 core->assign_windows = false;
2193 interlock[NV50_DISP_INTERLOCK_CORE] = 0;
2196 /* Update plane(s). */
2197 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2198 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2199 struct nv50_wndw *wndw = nv50_wndw(plane);
2201 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
2202 asyw->set.mask, asyw->clr.mask);
2203 if ( !asyw->set.mask &&
2204 (!asyw->clr.mask || atom->flush_disable))
2207 nv50_wndw_flush_set(wndw, interlock, asyw);
2211 nv50_disp_atomic_commit_wndw(state, interlock);
2213 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2214 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2215 interlock[NV50_DISP_INTERLOCK_OVLY] ||
2216 interlock[NV50_DISP_INTERLOCK_WNDW] ||
2217 !atom->state.legacy_cursor_update)
2218 nv50_disp_atomic_commit_core(state, interlock);
2220 disp->core->func->update(disp->core, interlock, false);
2223 if (atom->lock_core)
2224 mutex_unlock(&disp->mutex);
2226 /* Wait for HW to signal completion. */
2227 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2228 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2229 struct nv50_wndw *wndw = nv50_wndw(plane);
2230 int ret = nv50_wndw_wait_armed(wndw, asyw);
2232 NV_ERROR(drm, "%s: timeout\n", plane->name);
2235 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2236 if (new_crtc_state->event) {
2237 unsigned long flags;
2238 /* Get correct count/ts if racing with vblank irq */
2239 if (new_crtc_state->active)
2240 drm_crtc_accurate_vblank_count(crtc);
2241 spin_lock_irqsave(&crtc->dev->event_lock, flags);
2242 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2243 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2245 new_crtc_state->event = NULL;
2246 if (new_crtc_state->active)
2247 drm_crtc_vblank_put(crtc);
2251 nv50_crc_atomic_start_reporting(state);
2253 nv50_crc_atomic_release_notifier_contexts(state);
2254 drm_atomic_helper_commit_hw_done(state);
2255 drm_atomic_helper_cleanup_planes(dev, state);
2256 drm_atomic_helper_commit_cleanup_done(state);
2257 drm_atomic_state_put(state);
2259 /* Drop the RPM ref we got from nv50_disp_atomic_commit() */
2260 pm_runtime_mark_last_busy(dev->dev);
2261 pm_runtime_put_autosuspend(dev->dev);
2265 nv50_disp_atomic_commit_work(struct work_struct *work)
2267 struct drm_atomic_state *state =
2268 container_of(work, typeof(*state), commit_work);
2269 nv50_disp_atomic_commit_tail(state);
2273 nv50_disp_atomic_commit(struct drm_device *dev,
2274 struct drm_atomic_state *state, bool nonblock)
2276 struct drm_plane_state *new_plane_state;
2277 struct drm_plane *plane;
2280 ret = pm_runtime_get_sync(dev->dev);
2281 if (ret < 0 && ret != -EACCES) {
2282 pm_runtime_put_autosuspend(dev->dev);
2286 ret = drm_atomic_helper_setup_commit(state, nonblock);
2290 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
2292 ret = drm_atomic_helper_prepare_planes(dev, state);
2297 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2302 ret = drm_atomic_helper_swap_state(state, true);
2306 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2307 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2308 struct nv50_wndw *wndw = nv50_wndw(plane);
2310 if (asyw->set.image)
2311 nv50_wndw_ntfy_enable(wndw, asyw);
2314 drm_atomic_state_get(state);
2317 * Grab another RPM ref for the commit tail, which will release the
2318 * ref when it's finished
2320 pm_runtime_get_noresume(dev->dev);
2323 queue_work(system_unbound_wq, &state->commit_work);
2325 nv50_disp_atomic_commit_tail(state);
2329 drm_atomic_helper_cleanup_planes(dev, state);
2331 pm_runtime_put_autosuspend(dev->dev);
2335 static struct nv50_outp_atom *
2336 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
2338 struct nv50_outp_atom *outp;
2340 list_for_each_entry(outp, &atom->outp, head) {
2341 if (outp->encoder == encoder)
2345 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2347 return ERR_PTR(-ENOMEM);
2349 list_add(&outp->head, &atom->outp);
2350 outp->encoder = encoder;
2355 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2356 struct drm_connector_state *old_connector_state)
2358 struct drm_encoder *encoder = old_connector_state->best_encoder;
2359 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2360 struct drm_crtc *crtc;
2361 struct nv50_outp_atom *outp;
2363 if (!(crtc = old_connector_state->crtc))
2366 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
2367 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2368 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2369 outp = nv50_disp_outp_atomic_add(atom, encoder);
2371 return PTR_ERR(outp);
2373 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
2374 outp->flush_disable = true;
2375 atom->flush_disable = true;
2377 outp->clr.ctrl = true;
2378 atom->lock_core = true;
2385 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
2386 struct drm_connector_state *connector_state)
2388 struct drm_encoder *encoder = connector_state->best_encoder;
2389 struct drm_crtc_state *new_crtc_state;
2390 struct drm_crtc *crtc;
2391 struct nv50_outp_atom *outp;
2393 if (!(crtc = connector_state->crtc))
2396 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2397 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2398 outp = nv50_disp_outp_atomic_add(atom, encoder);
2400 return PTR_ERR(outp);
2402 outp->set.ctrl = true;
2403 atom->lock_core = true;
2410 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
2412 struct nv50_atom *atom = nv50_atom(state);
2413 struct nv50_core *core = nv50_disp(dev)->core;
2414 struct drm_connector_state *old_connector_state, *new_connector_state;
2415 struct drm_connector *connector;
2416 struct drm_crtc_state *new_crtc_state;
2417 struct drm_crtc *crtc;
2418 struct nv50_head *head;
2419 struct nv50_head_atom *asyh;
2422 if (core->assign_windows && core->func->head->static_wndw_map) {
2423 drm_for_each_crtc(crtc, dev) {
2424 new_crtc_state = drm_atomic_get_crtc_state(state,
2426 if (IS_ERR(new_crtc_state))
2427 return PTR_ERR(new_crtc_state);
2429 head = nv50_head(crtc);
2430 asyh = nv50_head_atom(new_crtc_state);
2431 core->func->head->static_wndw_map(head, asyh);
2435 /* We need to handle colour management on a per-plane basis. */
2436 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2437 if (new_crtc_state->color_mgmt_changed) {
2438 ret = drm_atomic_add_affected_planes(state, crtc);
2444 ret = drm_atomic_helper_check(dev, state);
2448 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
2449 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2453 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2458 ret = drm_dp_mst_atomic_check(state);
2462 nv50_crc_atomic_check_outp(atom);
2468 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2470 struct nv50_atom *atom = nv50_atom(state);
2471 struct nv50_outp_atom *outp, *outt;
2473 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2474 list_del(&outp->head);
2478 drm_atomic_state_default_clear(state);
2482 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2484 struct nv50_atom *atom = nv50_atom(state);
2485 drm_atomic_state_default_release(&atom->state);
2489 static struct drm_atomic_state *
2490 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2492 struct nv50_atom *atom;
2493 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2494 drm_atomic_state_init(dev, &atom->state) < 0) {
2498 INIT_LIST_HEAD(&atom->outp);
2499 return &atom->state;
2502 static const struct drm_mode_config_funcs
2504 .fb_create = nouveau_user_framebuffer_create,
2505 .output_poll_changed = nouveau_fbcon_output_poll_changed,
2506 .atomic_check = nv50_disp_atomic_check,
2507 .atomic_commit = nv50_disp_atomic_commit,
2508 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2509 .atomic_state_clear = nv50_disp_atomic_state_clear,
2510 .atomic_state_free = nv50_disp_atomic_state_free,
2513 /******************************************************************************
2515 *****************************************************************************/
2518 nv50_display_fini(struct drm_device *dev, bool runtime, bool suspend)
2520 struct nouveau_drm *drm = nouveau_drm(dev);
2521 struct drm_encoder *encoder;
2522 struct drm_plane *plane;
2524 drm_for_each_plane(plane, dev) {
2525 struct nv50_wndw *wndw = nv50_wndw(plane);
2526 if (plane->funcs != &nv50_wndw)
2528 nv50_wndw_fini(wndw);
2531 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2532 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
2533 nv50_mstm_fini(nouveau_encoder(encoder));
2537 cancel_work_sync(&drm->hpd_work);
2541 nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2543 struct nv50_core *core = nv50_disp(dev)->core;
2544 struct drm_encoder *encoder;
2545 struct drm_plane *plane;
2547 if (resume || runtime)
2548 core->func->init(core);
2550 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2551 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2552 struct nouveau_encoder *nv_encoder =
2553 nouveau_encoder(encoder);
2554 nv50_mstm_init(nv_encoder, runtime);
2558 drm_for_each_plane(plane, dev) {
2559 struct nv50_wndw *wndw = nv50_wndw(plane);
2560 if (plane->funcs != &nv50_wndw)
2562 nv50_wndw_init(wndw);
2569 nv50_display_destroy(struct drm_device *dev)
2571 struct nv50_disp *disp = nv50_disp(dev);
2573 nv50_audio_component_fini(nouveau_drm(dev));
2575 nvif_object_unmap(&disp->caps);
2576 nvif_object_dtor(&disp->caps);
2577 nv50_core_del(&disp->core);
2579 nouveau_bo_unmap(disp->sync);
2581 nouveau_bo_unpin(disp->sync);
2582 nouveau_bo_ref(NULL, &disp->sync);
2584 nouveau_display(dev)->priv = NULL;
2589 nv50_display_create(struct drm_device *dev)
2591 struct nvif_device *device = &nouveau_drm(dev)->client.device;
2592 struct nouveau_drm *drm = nouveau_drm(dev);
2593 struct dcb_table *dcb = &drm->vbios.dcb;
2594 struct drm_connector *connector, *tmp;
2595 struct nv50_disp *disp;
2596 struct dcb_output *dcbe;
2598 bool has_mst = nv50_has_mst(drm);
2600 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2604 mutex_init(&disp->mutex);
2606 nouveau_display(dev)->priv = disp;
2607 nouveau_display(dev)->dtor = nv50_display_destroy;
2608 nouveau_display(dev)->init = nv50_display_init;
2609 nouveau_display(dev)->fini = nv50_display_fini;
2610 disp->disp = &nouveau_display(dev)->disp;
2611 dev->mode_config.funcs = &nv50_disp_func;
2612 dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2613 dev->mode_config.normalize_zpos = true;
2615 /* small shared memory area we use for notifiers and semaphores */
2616 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2617 0, 0x0000, NULL, NULL, &disp->sync);
2619 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
2621 ret = nouveau_bo_map(disp->sync);
2623 nouveau_bo_unpin(disp->sync);
2626 nouveau_bo_ref(NULL, &disp->sync);
2632 /* allocate master evo channel */
2633 ret = nv50_core_new(drm, &disp->core);
2637 disp->core->func->init(disp->core);
2638 if (disp->core->func->caps_init) {
2639 ret = disp->core->func->caps_init(drm, disp);
2644 /* Assign the correct format modifiers */
2645 if (disp->disp->object.oclass >= TU102_DISP)
2646 nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
2648 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
2649 nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
2651 nouveau_display(dev)->format_modifiers = disp50xx_modifiers;
2653 /* create crtc objects to represent the hw heads */
2654 if (disp->disp->object.oclass >= GV100_DISP)
2655 crtcs = nvif_rd32(&device->object, 0x610060) & 0xff;
2657 if (disp->disp->object.oclass >= GF110_DISP)
2658 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
2662 for (i = 0; i < fls(crtcs); i++) {
2663 struct nv50_head *head;
2665 if (!(crtcs & (1 << i)))
2668 head = nv50_head_create(dev, i);
2670 ret = PTR_ERR(head);
2675 head->msto = nv50_msto_new(dev, head, i);
2676 if (IS_ERR(head->msto)) {
2677 ret = PTR_ERR(head->msto);
2683 * FIXME: This is a hack to workaround the following
2686 * https://gitlab.gnome.org/GNOME/mutter/issues/759
2687 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
2689 * Once these issues are closed, this should be
2692 head->msto->encoder.possible_crtcs = crtcs;
2696 /* create encoder/connector objects based on VBIOS DCB table */
2697 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2698 connector = nouveau_connector_create(dev, dcbe);
2699 if (IS_ERR(connector))
2702 if (dcbe->location == DCB_LOC_ON_CHIP) {
2703 switch (dcbe->type) {
2704 case DCB_OUTPUT_TMDS:
2705 case DCB_OUTPUT_LVDS:
2707 ret = nv50_sor_create(connector, dcbe);
2709 case DCB_OUTPUT_ANALOG:
2710 ret = nv50_dac_create(connector, dcbe);
2717 ret = nv50_pior_create(connector, dcbe);
2721 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2722 dcbe->location, dcbe->type,
2723 ffs(dcbe->or) - 1, ret);
2728 /* cull any connectors we created that don't have an encoder */
2729 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2730 if (connector->possible_encoders)
2733 NV_WARN(drm, "%s has no encoders, removing\n",
2735 connector->funcs->destroy(connector);
2738 /* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
2739 dev->vblank_disable_immediate = true;
2741 nv50_audio_component_init(drm);
2745 nv50_display_destroy(dev);
2749 /******************************************************************************
2751 *****************************************************************************/
2753 /****************************************************************
2754 * Log2(block height) ----------------------------+ *
2755 * Page Kind ----------------------------------+ | *
2756 * Gob Height/Page Kind Generation ------+ | | *
2757 * Sector layout -------+ | | | *
2758 * Compression ------+ | | | | */
2759 const u64 disp50xx_modifiers[] = { /* | | | | | */
2760 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
2761 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
2762 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
2763 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
2764 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
2765 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
2766 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
2767 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
2768 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
2769 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
2770 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
2771 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
2772 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
2773 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
2774 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
2775 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
2776 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
2777 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
2778 DRM_FORMAT_MOD_LINEAR,
2779 DRM_FORMAT_MOD_INVALID
2782 /****************************************************************
2783 * Log2(block height) ----------------------------+ *
2784 * Page Kind ----------------------------------+ | *
2785 * Gob Height/Page Kind Generation ------+ | | *
2786 * Sector layout -------+ | | | *
2787 * Compression ------+ | | | | */
2788 const u64 disp90xx_modifiers[] = { /* | | | | | */
2789 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
2790 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
2791 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
2792 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
2793 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
2794 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
2795 DRM_FORMAT_MOD_LINEAR,
2796 DRM_FORMAT_MOD_INVALID