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.
30 #include <linux/dma-mapping.h>
31 #include <linux/hdmi.h>
32 #include <linux/component.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_dp_helper.h>
36 #include <drm/drm_edid.h>
37 #include <drm/drm_fb_helper.h>
38 #include <drm/drm_plane_helper.h>
39 #include <drm/drm_probe_helper.h>
40 #include <drm/drm_scdc_helper.h>
41 #include <drm/drm_vblank.h>
43 #include <nvif/class.h>
44 #include <nvif/cl0002.h>
45 #include <nvif/cl5070.h>
46 #include <nvif/cl507d.h>
47 #include <nvif/event.h>
48 #include <nvif/timer.h>
50 #include "nouveau_drv.h"
51 #include "nouveau_dma.h"
52 #include "nouveau_gem.h"
53 #include "nouveau_connector.h"
54 #include "nouveau_encoder.h"
55 #include "nouveau_fence.h"
56 #include "nouveau_fbcon.h"
58 #include <subdev/bios/dp.h>
60 /******************************************************************************
62 *****************************************************************************/
64 struct nv50_outp_atom {
65 struct list_head head;
67 struct drm_encoder *encoder;
70 union nv50_outp_atom_mask {
78 /******************************************************************************
80 *****************************************************************************/
83 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
84 const s32 *oclass, u8 head, void *data, u32 size,
85 struct nv50_chan *chan)
87 struct nvif_sclass *sclass;
90 chan->device = device;
92 ret = n = nvif_object_sclass_get(disp, &sclass);
97 for (i = 0; i < n; i++) {
98 if (sclass[i].oclass == oclass[0]) {
99 ret = nvif_object_init(disp, 0, oclass[0],
100 data, size, &chan->user);
102 nvif_object_map(&chan->user, NULL, 0);
103 nvif_object_sclass_put(&sclass);
110 nvif_object_sclass_put(&sclass);
115 nv50_chan_destroy(struct nv50_chan *chan)
117 nvif_object_fini(&chan->user);
120 /******************************************************************************
122 *****************************************************************************/
125 nv50_dmac_destroy(struct nv50_dmac *dmac)
127 nvif_object_fini(&dmac->vram);
128 nvif_object_fini(&dmac->sync);
130 nv50_chan_destroy(&dmac->base);
132 nvif_mem_fini(&dmac->push);
136 nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
137 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
138 struct nv50_dmac *dmac)
140 struct nouveau_cli *cli = (void *)device->object.client;
141 struct nv50_disp_core_channel_dma_v0 *args = data;
142 u8 type = NVIF_MEM_COHERENT;
145 mutex_init(&dmac->lock);
147 /* Pascal added support for 47-bit physical addresses, but some
148 * parts of EVO still only accept 40-bit PAs.
150 * To avoid issues on systems with large amounts of RAM, and on
151 * systems where an IOMMU maps pages at a high address, we need
152 * to allocate push buffers in VRAM instead.
154 * This appears to match NVIDIA's behaviour on Pascal.
156 if (device->info.family == NV_DEVICE_INFO_V0_PASCAL)
157 type |= NVIF_MEM_VRAM;
159 ret = nvif_mem_init_map(&cli->mmu, type, 0x1000, &dmac->push);
163 dmac->ptr = dmac->push.object.map.ptr;
165 args->pushbuf = nvif_handle(&dmac->push.object);
167 ret = nv50_chan_create(device, disp, oclass, head, data, size,
175 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
176 &(struct nv_dma_v0) {
177 .target = NV_DMA_V0_TARGET_VRAM,
178 .access = NV_DMA_V0_ACCESS_RDWR,
179 .start = syncbuf + 0x0000,
180 .limit = syncbuf + 0x0fff,
181 }, sizeof(struct nv_dma_v0),
186 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
187 &(struct nv_dma_v0) {
188 .target = NV_DMA_V0_TARGET_VRAM,
189 .access = NV_DMA_V0_ACCESS_RDWR,
191 .limit = device->info.ram_user - 1,
192 }, sizeof(struct nv_dma_v0),
200 /******************************************************************************
201 * EVO channel helpers
202 *****************************************************************************/
204 evo_flush(struct nv50_dmac *dmac)
206 /* Push buffer fetches are not coherent with BAR1, we need to ensure
207 * writes have been flushed right through to VRAM before writing PUT.
209 if (dmac->push.type & NVIF_MEM_VRAM) {
210 struct nvif_device *device = dmac->base.device;
211 nvif_wr32(&device->object, 0x070000, 0x00000001);
212 nvif_msec(device, 2000,
213 if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
220 evo_wait(struct nv50_dmac *evoc, int nr)
222 struct nv50_dmac *dmac = evoc;
223 struct nvif_device *device = dmac->base.device;
224 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
226 mutex_lock(&dmac->lock);
227 if (put + nr >= (PAGE_SIZE / 4) - 8) {
228 dmac->ptr[put] = 0x20000000;
231 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
232 if (nvif_msec(device, 2000,
233 if (!nvif_rd32(&dmac->base.user, 0x0004))
236 mutex_unlock(&dmac->lock);
237 pr_err("nouveau: evo channel stalled\n");
244 return dmac->ptr + put;
248 evo_kick(u32 *push, struct nv50_dmac *evoc)
250 struct nv50_dmac *dmac = evoc;
254 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
255 mutex_unlock(&dmac->lock);
258 /******************************************************************************
259 * Output path helpers
260 *****************************************************************************/
262 nv50_outp_release(struct nouveau_encoder *nv_encoder)
264 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
266 struct nv50_disp_mthd_v1 base;
269 .base.method = NV50_DISP_MTHD_V1_RELEASE,
270 .base.hasht = nv_encoder->dcb->hasht,
271 .base.hashm = nv_encoder->dcb->hashm,
274 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
276 nv_encoder->link = 0;
280 nv50_outp_acquire(struct nouveau_encoder *nv_encoder, bool hda)
282 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
283 struct nv50_disp *disp = nv50_disp(drm->dev);
285 struct nv50_disp_mthd_v1 base;
286 struct nv50_disp_acquire_v0 info;
289 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
290 .base.hasht = nv_encoder->dcb->hasht,
291 .base.hashm = nv_encoder->dcb->hashm,
296 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
298 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
302 nv_encoder->or = args.info.or;
303 nv_encoder->link = args.info.link;
308 nv50_outp_atomic_check_view(struct drm_encoder *encoder,
309 struct drm_crtc_state *crtc_state,
310 struct drm_connector_state *conn_state,
311 struct drm_display_mode *native_mode)
313 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
314 struct drm_display_mode *mode = &crtc_state->mode;
315 struct drm_connector *connector = conn_state->connector;
316 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
317 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
319 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
320 asyc->scaler.full = false;
324 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
325 switch (connector->connector_type) {
326 case DRM_MODE_CONNECTOR_LVDS:
327 case DRM_MODE_CONNECTOR_eDP:
328 /* Don't force scaler for EDID modes with
329 * same size as the native one (e.g. different
332 if (mode->hdisplay == native_mode->hdisplay &&
333 mode->vdisplay == native_mode->vdisplay &&
334 mode->type & DRM_MODE_TYPE_DRIVER)
337 asyc->scaler.full = true;
346 if (!drm_mode_equal(adjusted_mode, mode)) {
347 drm_mode_copy(adjusted_mode, mode);
348 crtc_state->mode_changed = true;
355 nv50_outp_atomic_check(struct drm_encoder *encoder,
356 struct drm_crtc_state *crtc_state,
357 struct drm_connector_state *conn_state)
359 struct drm_connector *connector = conn_state->connector;
360 struct nouveau_connector *nv_connector = nouveau_connector(connector);
361 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
364 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
365 nv_connector->native_mode);
369 if (crtc_state->mode_changed || crtc_state->connectors_changed)
370 asyh->or.bpc = connector->display_info.bpc;
375 /******************************************************************************
377 *****************************************************************************/
379 nv50_dac_disable(struct drm_encoder *encoder)
381 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
382 struct nv50_core *core = nv50_disp(encoder->dev)->core;
383 if (nv_encoder->crtc)
384 core->func->dac->ctrl(core, nv_encoder->or, 0x00000000, NULL);
385 nv_encoder->crtc = NULL;
386 nv50_outp_release(nv_encoder);
390 nv50_dac_enable(struct drm_encoder *encoder)
392 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
393 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
394 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
395 struct nv50_core *core = nv50_disp(encoder->dev)->core;
397 nv50_outp_acquire(nv_encoder, false);
399 core->func->dac->ctrl(core, nv_encoder->or, 1 << nv_crtc->index, asyh);
402 nv_encoder->crtc = encoder->crtc;
405 static enum drm_connector_status
406 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
408 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
409 struct nv50_disp *disp = nv50_disp(encoder->dev);
411 struct nv50_disp_mthd_v1 base;
412 struct nv50_disp_dac_load_v0 load;
415 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
416 .base.hasht = nv_encoder->dcb->hasht,
417 .base.hashm = nv_encoder->dcb->hashm,
421 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
422 if (args.load.data == 0)
423 args.load.data = 340;
425 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
426 if (ret || !args.load.load)
427 return connector_status_disconnected;
429 return connector_status_connected;
432 static const struct drm_encoder_helper_funcs
434 .atomic_check = nv50_outp_atomic_check,
435 .enable = nv50_dac_enable,
436 .disable = nv50_dac_disable,
437 .detect = nv50_dac_detect
441 nv50_dac_destroy(struct drm_encoder *encoder)
443 drm_encoder_cleanup(encoder);
447 static const struct drm_encoder_funcs
449 .destroy = nv50_dac_destroy,
453 nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
455 struct nouveau_drm *drm = nouveau_drm(connector->dev);
456 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
457 struct nvkm_i2c_bus *bus;
458 struct nouveau_encoder *nv_encoder;
459 struct drm_encoder *encoder;
460 int type = DRM_MODE_ENCODER_DAC;
462 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
465 nv_encoder->dcb = dcbe;
467 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
469 nv_encoder->i2c = &bus->i2c;
471 encoder = to_drm_encoder(nv_encoder);
472 encoder->possible_crtcs = dcbe->heads;
473 encoder->possible_clones = 0;
474 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
475 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
476 drm_encoder_helper_add(encoder, &nv50_dac_help);
478 drm_connector_attach_encoder(connector, encoder);
483 * audio component binding for ELD notification
486 nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
489 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
490 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
495 nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
496 bool *enabled, unsigned char *buf, int max_bytes)
498 struct drm_device *drm_dev = dev_get_drvdata(kdev);
499 struct nouveau_drm *drm = nouveau_drm(drm_dev);
500 struct drm_encoder *encoder;
501 struct nouveau_encoder *nv_encoder;
502 struct nouveau_connector *nv_connector;
503 struct nouveau_crtc *nv_crtc;
507 drm_for_each_encoder(encoder, drm->dev) {
508 nv_encoder = nouveau_encoder(encoder);
509 nv_connector = nouveau_encoder_connector_get(nv_encoder);
510 nv_crtc = nouveau_crtc(encoder->crtc);
511 if (!nv_connector || !nv_crtc || nv_encoder->or != port ||
512 nv_crtc->index != dev_id)
514 *enabled = nv_encoder->audio;
516 ret = drm_eld_size(nv_connector->base.eld);
517 memcpy(buf, nv_connector->base.eld,
518 min(max_bytes, ret));
525 static const struct drm_audio_component_ops nv50_audio_component_ops = {
526 .get_eld = nv50_audio_component_get_eld,
530 nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
533 struct drm_device *drm_dev = dev_get_drvdata(kdev);
534 struct nouveau_drm *drm = nouveau_drm(drm_dev);
535 struct drm_audio_component *acomp = data;
537 if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
540 drm_modeset_lock_all(drm_dev);
541 acomp->ops = &nv50_audio_component_ops;
543 drm->audio.component = acomp;
544 drm_modeset_unlock_all(drm_dev);
549 nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
552 struct drm_device *drm_dev = dev_get_drvdata(kdev);
553 struct nouveau_drm *drm = nouveau_drm(drm_dev);
554 struct drm_audio_component *acomp = data;
556 drm_modeset_lock_all(drm_dev);
557 drm->audio.component = NULL;
560 drm_modeset_unlock_all(drm_dev);
563 static const struct component_ops nv50_audio_component_bind_ops = {
564 .bind = nv50_audio_component_bind,
565 .unbind = nv50_audio_component_unbind,
569 nv50_audio_component_init(struct nouveau_drm *drm)
571 if (!component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
572 drm->audio.component_registered = true;
576 nv50_audio_component_fini(struct nouveau_drm *drm)
578 if (drm->audio.component_registered) {
579 component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
580 drm->audio.component_registered = false;
584 /******************************************************************************
586 *****************************************************************************/
588 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
590 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
591 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
592 struct nv50_disp *disp = nv50_disp(encoder->dev);
594 struct nv50_disp_mthd_v1 base;
595 struct nv50_disp_sor_hda_eld_v0 eld;
598 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
599 .base.hasht = nv_encoder->dcb->hasht,
600 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
601 (0x0100 << nv_crtc->index),
604 if (!nv_encoder->audio)
607 nv_encoder->audio = false;
608 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
610 nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
615 nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
617 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
618 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
619 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
620 struct nouveau_connector *nv_connector;
621 struct nv50_disp *disp = nv50_disp(encoder->dev);
624 struct nv50_disp_mthd_v1 mthd;
625 struct nv50_disp_sor_hda_eld_v0 eld;
627 u8 data[sizeof(nv_connector->base.eld)];
629 .base.mthd.version = 1,
630 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
631 .base.mthd.hasht = nv_encoder->dcb->hasht,
632 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
633 (0x0100 << nv_crtc->index),
636 nv_connector = nouveau_encoder_connector_get(nv_encoder);
637 if (!drm_detect_monitor_audio(nv_connector->edid))
640 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
642 nvif_mthd(&disp->disp->object, 0, &args,
643 sizeof(args.base) + drm_eld_size(args.data));
644 nv_encoder->audio = true;
646 nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
650 /******************************************************************************
652 *****************************************************************************/
654 nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
656 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
657 struct nv50_disp *disp = nv50_disp(encoder->dev);
659 struct nv50_disp_mthd_v1 base;
660 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
663 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
664 .base.hasht = nv_encoder->dcb->hasht,
665 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
666 (0x0100 << nv_crtc->index),
669 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
673 nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
675 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
676 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
677 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
678 struct nv50_disp *disp = nv50_disp(encoder->dev);
680 struct nv50_disp_mthd_v1 base;
681 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
682 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
685 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
686 .base.hasht = nv_encoder->dcb->hasht,
687 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
688 (0x0100 << nv_crtc->index),
690 .pwr.rekey = 56, /* binary driver, and tegra, constant */
692 struct nouveau_connector *nv_connector;
693 struct drm_hdmi_info *hdmi;
695 union hdmi_infoframe avi_frame;
696 union hdmi_infoframe vendor_frame;
697 bool high_tmds_clock_ratio = false, scrambling = false;
702 nv_connector = nouveau_encoder_connector_get(nv_encoder);
703 if (!drm_detect_hdmi_monitor(nv_connector->edid))
706 hdmi = &nv_connector->base.display_info.hdmi;
708 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi,
709 &nv_connector->base, mode);
711 /* We have an AVI InfoFrame, populate it to the display */
712 args.pwr.avi_infoframe_length
713 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
716 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
717 &nv_connector->base, mode);
719 /* We have a Vendor InfoFrame, populate it to the display */
720 args.pwr.vendor_infoframe_length
721 = hdmi_infoframe_pack(&vendor_frame,
723 + args.pwr.avi_infoframe_length,
727 max_ac_packet = mode->htotal - mode->hdisplay;
728 max_ac_packet -= args.pwr.rekey;
729 max_ac_packet -= 18; /* constant from tegra */
730 args.pwr.max_ac_packet = max_ac_packet / 32;
732 if (hdmi->scdc.scrambling.supported) {
733 high_tmds_clock_ratio = mode->clock > 340000;
734 scrambling = high_tmds_clock_ratio ||
735 hdmi->scdc.scrambling.low_rates;
739 NV50_DISP_SOR_HDMI_PWR_V0_SCDC_SCRAMBLE * scrambling |
740 NV50_DISP_SOR_HDMI_PWR_V0_SCDC_DIV_BY_4 * high_tmds_clock_ratio;
742 size = sizeof(args.base)
744 + args.pwr.avi_infoframe_length
745 + args.pwr.vendor_infoframe_length;
746 nvif_mthd(&disp->disp->object, 0, &args, size);
748 nv50_audio_enable(encoder, mode);
750 /* If SCDC is supported by the downstream monitor, update
751 * divider / scrambling settings to what we programmed above.
753 if (!hdmi->scdc.scrambling.supported)
756 ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &config);
758 NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
761 config &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
762 config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 * high_tmds_clock_ratio;
763 config |= SCDC_SCRAMBLING_ENABLE * scrambling;
764 ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, config);
766 NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
770 /******************************************************************************
772 *****************************************************************************/
773 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
774 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
775 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
778 struct nouveau_encoder *outp;
780 struct drm_dp_mst_topology_mgr mgr;
788 struct nv50_mstm *mstm;
789 struct drm_dp_mst_port *port;
790 struct drm_connector connector;
792 struct drm_display_mode *native;
797 struct drm_encoder encoder;
799 struct nv50_head *head;
800 struct nv50_mstc *mstc;
804 static struct drm_dp_payload *
805 nv50_msto_payload(struct nv50_msto *msto)
807 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
808 struct nv50_mstc *mstc = msto->mstc;
809 struct nv50_mstm *mstm = mstc->mstm;
810 int vcpi = mstc->port->vcpi.vcpi, i;
812 WARN_ON(!mutex_is_locked(&mstm->mgr.payload_lock));
814 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
815 for (i = 0; i < mstm->mgr.max_payloads; i++) {
816 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
817 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
818 mstm->outp->base.base.name, i, payload->vcpi,
819 payload->start_slot, payload->num_slots);
822 for (i = 0; i < mstm->mgr.max_payloads; i++) {
823 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
824 if (payload->vcpi == vcpi)
832 nv50_msto_cleanup(struct nv50_msto *msto)
834 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
835 struct nv50_mstc *mstc = msto->mstc;
836 struct nv50_mstm *mstm = mstc->mstm;
841 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
843 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
846 msto->disabled = false;
850 nv50_msto_prepare(struct nv50_msto *msto)
852 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
853 struct nv50_mstc *mstc = msto->mstc;
854 struct nv50_mstm *mstm = mstc->mstm;
856 struct nv50_disp_mthd_v1 base;
857 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
860 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
861 .base.hasht = mstm->outp->dcb->hasht,
862 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
863 (0x0100 << msto->head->base.index),
866 mutex_lock(&mstm->mgr.payload_lock);
868 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
869 if (mstc->port->vcpi.vcpi > 0) {
870 struct drm_dp_payload *payload = nv50_msto_payload(msto);
872 args.vcpi.start_slot = payload->start_slot;
873 args.vcpi.num_slots = payload->num_slots;
874 args.vcpi.pbn = mstc->port->vcpi.pbn;
875 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
879 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
880 msto->encoder.name, msto->head->base.base.name,
881 args.vcpi.start_slot, args.vcpi.num_slots,
882 args.vcpi.pbn, args.vcpi.aligned_pbn);
884 nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
885 mutex_unlock(&mstm->mgr.payload_lock);
889 nv50_msto_atomic_check(struct drm_encoder *encoder,
890 struct drm_crtc_state *crtc_state,
891 struct drm_connector_state *conn_state)
893 struct drm_atomic_state *state = crtc_state->state;
894 struct drm_connector *connector = conn_state->connector;
895 struct nv50_mstc *mstc = nv50_mstc(connector);
896 struct nv50_mstm *mstm = mstc->mstm;
897 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
901 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
906 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
910 * When restoring duplicated states, we need to make sure that the bw
911 * remains the same and avoid recalculating it, as the connector's bpc
912 * may have changed after the state was duplicated
914 if (!state->duplicated) {
915 const int clock = crtc_state->adjusted_mode.clock;
917 asyh->or.bpc = connector->display_info.bpc;
918 asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3,
922 slots = drm_dp_atomic_find_vcpi_slots(state, &mstm->mgr, mstc->port,
933 nv50_dp_bpc_to_depth(unsigned int bpc)
938 case 10: /* fall-through */
944 nv50_msto_enable(struct drm_encoder *encoder)
946 struct nv50_head *head = nv50_head(encoder->crtc);
947 struct nv50_head_atom *armh = nv50_head_atom(head->base.base.state);
948 struct nv50_msto *msto = nv50_msto(encoder);
949 struct nv50_mstc *mstc = NULL;
950 struct nv50_mstm *mstm = NULL;
951 struct drm_connector *connector;
952 struct drm_connector_list_iter conn_iter;
956 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
957 drm_for_each_connector_iter(connector, &conn_iter) {
958 if (connector->state->best_encoder == &msto->encoder) {
959 mstc = nv50_mstc(connector);
964 drm_connector_list_iter_end(&conn_iter);
969 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, armh->dp.pbn,
972 DRM_DEBUG_KMS("Failed to allocate VCPI\n");
975 nv50_outp_acquire(mstm->outp, false /*XXX: MST audio.*/);
977 if (mstm->outp->link & 1)
982 mstm->outp->update(mstm->outp, head->base.index, armh, proto,
983 nv50_dp_bpc_to_depth(armh->or.bpc));
986 mstm->modified = true;
990 nv50_msto_disable(struct drm_encoder *encoder)
992 struct nv50_msto *msto = nv50_msto(encoder);
993 struct nv50_mstc *mstc = msto->mstc;
994 struct nv50_mstm *mstm = mstc->mstm;
996 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
998 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
999 mstm->modified = true;
1001 mstm->disabled = true;
1002 msto->disabled = true;
1005 static const struct drm_encoder_helper_funcs
1007 .disable = nv50_msto_disable,
1008 .enable = nv50_msto_enable,
1009 .atomic_check = nv50_msto_atomic_check,
1013 nv50_msto_destroy(struct drm_encoder *encoder)
1015 struct nv50_msto *msto = nv50_msto(encoder);
1016 drm_encoder_cleanup(&msto->encoder);
1020 static const struct drm_encoder_funcs
1022 .destroy = nv50_msto_destroy,
1025 static struct nv50_msto *
1026 nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1028 struct nv50_msto *msto;
1031 msto = kzalloc(sizeof(*msto), GFP_KERNEL);
1033 return ERR_PTR(-ENOMEM);
1035 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1036 DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1039 return ERR_PTR(ret);
1042 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1043 msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
1048 static struct drm_encoder *
1049 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1050 struct drm_connector_state *connector_state)
1052 struct nv50_mstc *mstc = nv50_mstc(connector);
1053 struct drm_crtc *crtc = connector_state->crtc;
1055 if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1058 return &nv50_head(crtc)->msto->encoder;
1061 static enum drm_mode_status
1062 nv50_mstc_mode_valid(struct drm_connector *connector,
1063 struct drm_display_mode *mode)
1065 struct nv50_mstc *mstc = nv50_mstc(connector);
1066 struct nouveau_encoder *outp = mstc->mstm->outp;
1068 /* TODO: calculate the PBN from the dotclock and validate against the
1069 * MSTB's max possible PBN
1072 return nv50_dp_mode_valid(connector, outp, mode, NULL);
1076 nv50_mstc_get_modes(struct drm_connector *connector)
1078 struct nv50_mstc *mstc = nv50_mstc(connector);
1081 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1082 drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1084 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
1087 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
1088 * to 8 to save bandwidth on the topology. In the future, we'll want
1089 * to properly fix this by dynamically selecting the highest possible
1090 * bpc that would fit in the topology
1092 if (connector->display_info.bpc)
1093 connector->display_info.bpc =
1094 clamp(connector->display_info.bpc, 6U, 8U);
1096 connector->display_info.bpc = 8;
1099 drm_mode_destroy(mstc->connector.dev, mstc->native);
1100 mstc->native = nouveau_conn_native_mode(&mstc->connector);
1105 nv50_mstc_atomic_check(struct drm_connector *connector,
1106 struct drm_atomic_state *state)
1108 struct nv50_mstc *mstc = nv50_mstc(connector);
1109 struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;
1110 struct drm_connector_state *new_conn_state =
1111 drm_atomic_get_new_connector_state(state, connector);
1112 struct drm_connector_state *old_conn_state =
1113 drm_atomic_get_old_connector_state(state, connector);
1114 struct drm_crtc_state *crtc_state;
1115 struct drm_crtc *new_crtc = new_conn_state->crtc;
1117 if (!old_conn_state->crtc)
1120 /* We only want to free VCPI if this state disables the CRTC on this
1124 crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
1127 !drm_atomic_crtc_needs_modeset(crtc_state) ||
1132 return drm_dp_atomic_release_vcpi_slots(state, mgr, mstc->port);
1136 nv50_mstc_detect(struct drm_connector *connector,
1137 struct drm_modeset_acquire_ctx *ctx, bool force)
1139 struct nv50_mstc *mstc = nv50_mstc(connector);
1142 if (drm_connector_is_unregistered(connector))
1143 return connector_status_disconnected;
1145 ret = pm_runtime_get_sync(connector->dev->dev);
1146 if (ret < 0 && ret != -EACCES) {
1147 pm_runtime_put_autosuspend(connector->dev->dev);
1148 return connector_status_disconnected;
1151 ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
1154 pm_runtime_mark_last_busy(connector->dev->dev);
1155 pm_runtime_put_autosuspend(connector->dev->dev);
1159 static const struct drm_connector_helper_funcs
1161 .get_modes = nv50_mstc_get_modes,
1162 .mode_valid = nv50_mstc_mode_valid,
1163 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
1164 .atomic_check = nv50_mstc_atomic_check,
1165 .detect_ctx = nv50_mstc_detect,
1169 nv50_mstc_destroy(struct drm_connector *connector)
1171 struct nv50_mstc *mstc = nv50_mstc(connector);
1173 drm_connector_cleanup(&mstc->connector);
1174 drm_dp_mst_put_port_malloc(mstc->port);
1179 static const struct drm_connector_funcs
1181 .reset = nouveau_conn_reset,
1182 .fill_modes = drm_helper_probe_single_connector_modes,
1183 .destroy = nv50_mstc_destroy,
1184 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1185 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1186 .atomic_set_property = nouveau_conn_atomic_set_property,
1187 .atomic_get_property = nouveau_conn_atomic_get_property,
1191 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
1192 const char *path, struct nv50_mstc **pmstc)
1194 struct drm_device *dev = mstm->outp->base.base.dev;
1195 struct drm_crtc *crtc;
1196 struct nv50_mstc *mstc;
1199 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
1204 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
1205 DRM_MODE_CONNECTOR_DisplayPort);
1212 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
1214 mstc->connector.funcs->reset(&mstc->connector);
1215 nouveau_conn_attach_properties(&mstc->connector);
1217 drm_for_each_crtc(crtc, dev) {
1218 if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1221 drm_connector_attach_encoder(&mstc->connector,
1222 &nv50_head(crtc)->msto->encoder);
1225 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
1226 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1227 drm_connector_set_path_property(&mstc->connector, path);
1228 drm_dp_mst_get_port_malloc(port);
1233 nv50_mstm_cleanup(struct nv50_mstm *mstm)
1235 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1236 struct drm_encoder *encoder;
1239 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1240 ret = drm_dp_check_act_status(&mstm->mgr);
1242 ret = drm_dp_update_payload_part2(&mstm->mgr);
1244 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1245 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1246 struct nv50_msto *msto = nv50_msto(encoder);
1247 struct nv50_mstc *mstc = msto->mstc;
1248 if (mstc && mstc->mstm == mstm)
1249 nv50_msto_cleanup(msto);
1253 mstm->modified = false;
1257 nv50_mstm_prepare(struct nv50_mstm *mstm)
1259 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1260 struct drm_encoder *encoder;
1263 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
1264 ret = drm_dp_update_payload_part1(&mstm->mgr);
1266 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1267 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1268 struct nv50_msto *msto = nv50_msto(encoder);
1269 struct nv50_mstc *mstc = msto->mstc;
1270 if (mstc && mstc->mstm == mstm)
1271 nv50_msto_prepare(msto);
1275 if (mstm->disabled) {
1277 nv50_outp_release(mstm->outp);
1278 mstm->disabled = false;
1282 static struct drm_connector *
1283 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1284 struct drm_dp_mst_port *port, const char *path)
1286 struct nv50_mstm *mstm = nv50_mstm(mgr);
1287 struct nv50_mstc *mstc;
1290 ret = nv50_mstc_new(mstm, port, path, &mstc);
1294 return &mstc->connector;
1297 static const struct drm_dp_mst_topology_cbs
1299 .add_connector = nv50_mstm_add_connector,
1303 nv50_mstm_service(struct nv50_mstm *mstm)
1305 struct drm_dp_aux *aux = mstm ? mstm->mgr.aux : NULL;
1306 bool handled = true;
1314 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1316 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1320 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1324 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
1329 nv50_mstm_remove(struct nv50_mstm *mstm)
1332 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1336 nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
1338 struct nouveau_encoder *outp = mstm->outp;
1340 struct nv50_disp_mthd_v1 base;
1341 struct nv50_disp_sor_dp_mst_link_v0 mst;
1344 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
1345 .base.hasht = outp->dcb->hasht,
1346 .base.hashm = outp->dcb->hashm,
1349 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
1350 struct nvif_object *disp = &drm->display->disp.object;
1354 /* Even if we're enabling MST, start with disabling the
1355 * branching unit to clear any sink-side MST topology state
1356 * that wasn't set by us
1358 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, 0);
1363 /* Now, start initializing */
1364 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL,
1371 return nvif_mthd(disp, 0, &args, sizeof(args));
1375 nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
1377 struct drm_dp_aux *aux;
1379 bool old_state, new_state;
1385 mutex_lock(&mstm->mgr.lock);
1387 old_state = mstm->mgr.mst_state;
1388 new_state = old_state;
1389 aux = mstm->mgr.aux;
1392 /* Just check that the MST hub is still as we expect it */
1393 ret = drm_dp_dpcd_readb(aux, DP_MSTM_CTRL, &mstm_ctrl);
1394 if (ret < 0 || !(mstm_ctrl & DP_MST_EN)) {
1395 DRM_DEBUG_KMS("Hub gone, disabling MST topology\n");
1398 } else if (dpcd[0] >= 0x12) {
1399 ret = drm_dp_dpcd_readb(aux, DP_MSTM_CAP, &dpcd[1]);
1403 if (!(dpcd[1] & DP_MST_CAP))
1409 if (new_state == old_state) {
1410 mutex_unlock(&mstm->mgr.lock);
1414 ret = nv50_mstm_enable(mstm, dpcd[0], new_state);
1418 mutex_unlock(&mstm->mgr.lock);
1420 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, new_state);
1422 return nv50_mstm_enable(mstm, dpcd[0], 0);
1427 mutex_unlock(&mstm->mgr.lock);
1432 nv50_mstm_fini(struct nv50_mstm *mstm)
1434 if (mstm && mstm->mgr.mst_state)
1435 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1439 nv50_mstm_init(struct nv50_mstm *mstm, bool runtime)
1443 if (!mstm || !mstm->mgr.mst_state)
1446 ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
1448 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1449 drm_kms_helper_hotplug_event(mstm->mgr.dev);
1454 nv50_mstm_del(struct nv50_mstm **pmstm)
1456 struct nv50_mstm *mstm = *pmstm;
1458 drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1465 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1466 int conn_base_id, struct nv50_mstm **pmstm)
1468 const int max_payloads = hweight8(outp->dcb->heads);
1469 struct drm_device *dev = outp->base.base.dev;
1470 struct nv50_mstm *mstm;
1474 /* This is a workaround for some monitors not functioning
1475 * correctly in MST mode on initial module load. I think
1476 * some bad interaction with the VBIOS may be responsible.
1478 * A good ol' off and on again seems to work here ;)
1480 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
1481 if (ret >= 0 && dpcd >= 0x12)
1482 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1484 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1487 mstm->mgr.cbs = &nv50_mstm;
1489 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1490 max_payloads, conn_base_id);
1497 /******************************************************************************
1499 *****************************************************************************/
1501 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1502 struct nv50_head_atom *asyh, u8 proto, u8 depth)
1504 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1505 struct nv50_core *core = disp->core;
1508 nv_encoder->ctrl &= ~BIT(head);
1509 if (!(nv_encoder->ctrl & 0x0000000f))
1510 nv_encoder->ctrl = 0;
1512 nv_encoder->ctrl |= proto << 8;
1513 nv_encoder->ctrl |= BIT(head);
1514 asyh->or.depth = depth;
1517 core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
1521 nv50_sor_disable(struct drm_encoder *encoder)
1523 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1524 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1526 nv_encoder->crtc = NULL;
1529 struct nvkm_i2c_aux *aux = nv_encoder->aux;
1533 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
1535 pwr &= ~DP_SET_POWER_MASK;
1536 pwr |= DP_SET_POWER_D3;
1537 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
1541 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
1542 nv50_audio_disable(encoder, nv_crtc);
1543 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
1544 nv50_outp_release(nv_encoder);
1549 nv50_sor_enable(struct drm_encoder *encoder)
1551 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1552 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1553 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1554 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1556 struct nv50_disp_mthd_v1 base;
1557 struct nv50_disp_sor_lvds_script_v0 lvds;
1560 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1561 .base.hasht = nv_encoder->dcb->hasht,
1562 .base.hashm = nv_encoder->dcb->hashm,
1564 struct nv50_disp *disp = nv50_disp(encoder->dev);
1565 struct drm_device *dev = encoder->dev;
1566 struct nouveau_drm *drm = nouveau_drm(dev);
1567 struct nouveau_connector *nv_connector;
1568 struct nvbios *bios = &drm->vbios;
1573 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1574 nv_encoder->crtc = encoder->crtc;
1576 if ((disp->disp->object.oclass == GT214_DISP ||
1577 disp->disp->object.oclass >= GF110_DISP) &&
1578 drm_detect_monitor_audio(nv_connector->edid))
1580 nv50_outp_acquire(nv_encoder, hda);
1582 switch (nv_encoder->dcb->type) {
1583 case DCB_OUTPUT_TMDS:
1584 if (nv_encoder->link & 1) {
1586 /* Only enable dual-link if:
1587 * - Need to (i.e. rate > 165MHz)
1589 * - Not an HDMI monitor, since there's no dual-link
1592 if (mode->clock >= 165000 &&
1593 nv_encoder->dcb->duallink_possible &&
1594 !drm_detect_hdmi_monitor(nv_connector->edid))
1600 nv50_hdmi_enable(&nv_encoder->base.base, mode);
1602 case DCB_OUTPUT_LVDS:
1605 if (bios->fp_no_ddc) {
1606 if (bios->fp.dual_link)
1607 lvds.lvds.script |= 0x0100;
1608 if (bios->fp.if_is_24bit)
1609 lvds.lvds.script |= 0x0200;
1611 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1612 if (((u8 *)nv_connector->edid)[121] == 2)
1613 lvds.lvds.script |= 0x0100;
1615 if (mode->clock >= bios->fp.duallink_transition_clk) {
1616 lvds.lvds.script |= 0x0100;
1619 if (lvds.lvds.script & 0x0100) {
1620 if (bios->fp.strapless_is_24bit & 2)
1621 lvds.lvds.script |= 0x0200;
1623 if (bios->fp.strapless_is_24bit & 1)
1624 lvds.lvds.script |= 0x0200;
1627 if (asyh->or.bpc == 8)
1628 lvds.lvds.script |= 0x0200;
1631 nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
1634 depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1636 if (nv_encoder->link & 1)
1641 nv50_audio_enable(encoder, mode);
1648 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1651 static const struct drm_encoder_helper_funcs
1653 .atomic_check = nv50_outp_atomic_check,
1654 .enable = nv50_sor_enable,
1655 .disable = nv50_sor_disable,
1659 nv50_sor_destroy(struct drm_encoder *encoder)
1661 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1662 nv50_mstm_del(&nv_encoder->dp.mstm);
1663 drm_encoder_cleanup(encoder);
1667 static const struct drm_encoder_funcs
1669 .destroy = nv50_sor_destroy,
1672 static bool nv50_has_mst(struct nouveau_drm *drm)
1674 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
1676 u8 ver, hdr, cnt, len;
1678 data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len);
1679 return data && ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04);
1683 nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
1685 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1686 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1687 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1688 struct nouveau_encoder *nv_encoder;
1689 struct drm_encoder *encoder;
1690 struct nv50_disp *disp = nv50_disp(connector->dev);
1693 switch (dcbe->type) {
1694 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1695 case DCB_OUTPUT_TMDS:
1698 type = DRM_MODE_ENCODER_TMDS;
1702 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1705 nv_encoder->dcb = dcbe;
1706 nv_encoder->update = nv50_sor_update;
1708 encoder = to_drm_encoder(nv_encoder);
1709 encoder->possible_crtcs = dcbe->heads;
1710 encoder->possible_clones = 0;
1711 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1712 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1713 drm_encoder_helper_add(encoder, &nv50_sor_help);
1715 drm_connector_attach_encoder(connector, encoder);
1717 disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1719 if (dcbe->type == DCB_OUTPUT_DP) {
1720 struct nvkm_i2c_aux *aux =
1721 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1724 if (disp->disp->object.oclass < GF110_DISP) {
1725 /* HW has no support for address-only
1726 * transactions, so we're required to
1727 * use custom I2C-over-AUX code.
1729 nv_encoder->i2c = &aux->i2c;
1731 nv_encoder->i2c = &nv_connector->aux.ddc;
1733 nv_encoder->aux = aux;
1736 if (nv_connector->type != DCB_CONNECTOR_eDP &&
1737 nv50_has_mst(drm)) {
1738 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
1739 16, nv_connector->base.base.id,
1740 &nv_encoder->dp.mstm);
1745 struct nvkm_i2c_bus *bus =
1746 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1748 nv_encoder->i2c = &bus->i2c;
1754 /******************************************************************************
1756 *****************************************************************************/
1758 nv50_pior_atomic_check(struct drm_encoder *encoder,
1759 struct drm_crtc_state *crtc_state,
1760 struct drm_connector_state *conn_state)
1762 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1765 crtc_state->adjusted_mode.clock *= 2;
1770 nv50_pior_disable(struct drm_encoder *encoder)
1772 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1773 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1774 if (nv_encoder->crtc)
1775 core->func->pior->ctrl(core, nv_encoder->or, 0x00000000, NULL);
1776 nv_encoder->crtc = NULL;
1777 nv50_outp_release(nv_encoder);
1781 nv50_pior_enable(struct drm_encoder *encoder)
1783 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1784 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1785 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1786 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1787 u8 owner = 1 << nv_crtc->index;
1790 nv50_outp_acquire(nv_encoder, false);
1792 switch (asyh->or.bpc) {
1793 case 10: asyh->or.depth = 0x6; break;
1794 case 8: asyh->or.depth = 0x5; break;
1795 case 6: asyh->or.depth = 0x2; break;
1796 default: asyh->or.depth = 0x0; break;
1799 switch (nv_encoder->dcb->type) {
1800 case DCB_OUTPUT_TMDS:
1809 core->func->pior->ctrl(core, nv_encoder->or, (proto << 8) | owner, asyh);
1810 nv_encoder->crtc = encoder->crtc;
1813 static const struct drm_encoder_helper_funcs
1815 .atomic_check = nv50_pior_atomic_check,
1816 .enable = nv50_pior_enable,
1817 .disable = nv50_pior_disable,
1821 nv50_pior_destroy(struct drm_encoder *encoder)
1823 drm_encoder_cleanup(encoder);
1827 static const struct drm_encoder_funcs
1829 .destroy = nv50_pior_destroy,
1833 nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1835 struct drm_device *dev = connector->dev;
1836 struct nouveau_drm *drm = nouveau_drm(dev);
1837 struct nv50_disp *disp = nv50_disp(dev);
1838 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1839 struct nvkm_i2c_bus *bus = NULL;
1840 struct nvkm_i2c_aux *aux = NULL;
1841 struct i2c_adapter *ddc;
1842 struct nouveau_encoder *nv_encoder;
1843 struct drm_encoder *encoder;
1846 switch (dcbe->type) {
1847 case DCB_OUTPUT_TMDS:
1848 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1849 ddc = bus ? &bus->i2c : NULL;
1850 type = DRM_MODE_ENCODER_TMDS;
1853 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
1854 ddc = aux ? &aux->i2c : NULL;
1855 type = DRM_MODE_ENCODER_TMDS;
1861 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1864 nv_encoder->dcb = dcbe;
1865 nv_encoder->i2c = ddc;
1866 nv_encoder->aux = aux;
1868 encoder = to_drm_encoder(nv_encoder);
1869 encoder->possible_crtcs = dcbe->heads;
1870 encoder->possible_clones = 0;
1871 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1872 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
1873 drm_encoder_helper_add(encoder, &nv50_pior_help);
1875 drm_connector_attach_encoder(connector, encoder);
1877 disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1882 /******************************************************************************
1884 *****************************************************************************/
1887 nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
1889 struct nouveau_drm *drm = nouveau_drm(state->dev);
1890 struct nv50_disp *disp = nv50_disp(drm->dev);
1891 struct nv50_core *core = disp->core;
1892 struct nv50_mstm *mstm;
1893 struct drm_encoder *encoder;
1895 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
1897 drm_for_each_encoder(encoder, drm->dev) {
1898 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1899 mstm = nouveau_encoder(encoder)->dp.mstm;
1900 if (mstm && mstm->modified)
1901 nv50_mstm_prepare(mstm);
1905 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1906 core->func->update(core, interlock, true);
1907 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1908 disp->core->chan.base.device))
1909 NV_ERROR(drm, "core notifier timeout\n");
1911 drm_for_each_encoder(encoder, drm->dev) {
1912 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1913 mstm = nouveau_encoder(encoder)->dp.mstm;
1914 if (mstm && mstm->modified)
1915 nv50_mstm_cleanup(mstm);
1921 nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
1923 struct drm_plane_state *new_plane_state;
1924 struct drm_plane *plane;
1927 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1928 struct nv50_wndw *wndw = nv50_wndw(plane);
1929 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1930 if (wndw->func->update)
1931 wndw->func->update(wndw, interlock);
1937 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1939 struct drm_device *dev = state->dev;
1940 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
1941 struct drm_crtc *crtc;
1942 struct drm_plane_state *new_plane_state;
1943 struct drm_plane *plane;
1944 struct nouveau_drm *drm = nouveau_drm(dev);
1945 struct nv50_disp *disp = nv50_disp(dev);
1946 struct nv50_atom *atom = nv50_atom(state);
1947 struct nv50_core *core = disp->core;
1948 struct nv50_outp_atom *outp, *outt;
1949 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
1952 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
1953 drm_atomic_helper_wait_for_fences(dev, state, false);
1954 drm_atomic_helper_wait_for_dependencies(state);
1955 drm_atomic_helper_update_legacy_modeset_state(dev, state);
1957 if (atom->lock_core)
1958 mutex_lock(&disp->mutex);
1960 /* Disable head(s). */
1961 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1962 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
1963 struct nv50_head *head = nv50_head(crtc);
1965 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
1966 asyh->clr.mask, asyh->set.mask);
1968 if (old_crtc_state->active && !new_crtc_state->active) {
1969 pm_runtime_put_noidle(dev->dev);
1970 drm_crtc_vblank_off(crtc);
1973 if (asyh->clr.mask) {
1974 nv50_head_flush_clr(head, asyh, atom->flush_disable);
1975 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
1979 /* Disable plane(s). */
1980 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1981 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
1982 struct nv50_wndw *wndw = nv50_wndw(plane);
1984 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
1985 asyw->clr.mask, asyw->set.mask);
1986 if (!asyw->clr.mask)
1989 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
1992 /* Disable output path(s). */
1993 list_for_each_entry(outp, &atom->outp, head) {
1994 const struct drm_encoder_helper_funcs *help;
1995 struct drm_encoder *encoder;
1997 encoder = outp->encoder;
1998 help = encoder->helper_private;
2000 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
2001 outp->clr.mask, outp->set.mask);
2003 if (outp->clr.mask) {
2004 help->disable(encoder);
2005 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2006 if (outp->flush_disable) {
2007 nv50_disp_atomic_commit_wndw(state, interlock);
2008 nv50_disp_atomic_commit_core(state, interlock);
2009 memset(interlock, 0x00, sizeof(interlock));
2014 /* Flush disable. */
2015 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2016 if (atom->flush_disable) {
2017 nv50_disp_atomic_commit_wndw(state, interlock);
2018 nv50_disp_atomic_commit_core(state, interlock);
2019 memset(interlock, 0x00, sizeof(interlock));
2023 /* Update output path(s). */
2024 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2025 const struct drm_encoder_helper_funcs *help;
2026 struct drm_encoder *encoder;
2028 encoder = outp->encoder;
2029 help = encoder->helper_private;
2031 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
2032 outp->set.mask, outp->clr.mask);
2034 if (outp->set.mask) {
2035 help->enable(encoder);
2036 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2039 list_del(&outp->head);
2043 /* Update head(s). */
2044 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2045 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2046 struct nv50_head *head = nv50_head(crtc);
2048 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2049 asyh->set.mask, asyh->clr.mask);
2051 if (asyh->set.mask) {
2052 nv50_head_flush_set(head, asyh);
2053 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2056 if (new_crtc_state->active) {
2057 if (!old_crtc_state->active) {
2058 drm_crtc_vblank_on(crtc);
2059 pm_runtime_get_noresume(dev->dev);
2061 if (new_crtc_state->event)
2062 drm_crtc_vblank_get(crtc);
2066 /* Update window->head assignment.
2068 * This has to happen in an update that's not interlocked with
2069 * any window channels to avoid hitting HW error checks.
2071 *TODO: Proper handling of window ownership (Turing apparently
2072 * supports non-fixed mappings).
2074 if (core->assign_windows) {
2075 core->func->wndw.owner(core);
2076 nv50_disp_atomic_commit_core(state, interlock);
2077 core->assign_windows = false;
2078 interlock[NV50_DISP_INTERLOCK_CORE] = 0;
2081 /* Update plane(s). */
2082 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2083 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2084 struct nv50_wndw *wndw = nv50_wndw(plane);
2086 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
2087 asyw->set.mask, asyw->clr.mask);
2088 if ( !asyw->set.mask &&
2089 (!asyw->clr.mask || atom->flush_disable))
2092 nv50_wndw_flush_set(wndw, interlock, asyw);
2096 nv50_disp_atomic_commit_wndw(state, interlock);
2098 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2099 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2100 interlock[NV50_DISP_INTERLOCK_OVLY] ||
2101 interlock[NV50_DISP_INTERLOCK_WNDW] ||
2102 !atom->state.legacy_cursor_update)
2103 nv50_disp_atomic_commit_core(state, interlock);
2105 disp->core->func->update(disp->core, interlock, false);
2108 if (atom->lock_core)
2109 mutex_unlock(&disp->mutex);
2111 /* Wait for HW to signal completion. */
2112 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2113 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2114 struct nv50_wndw *wndw = nv50_wndw(plane);
2115 int ret = nv50_wndw_wait_armed(wndw, asyw);
2117 NV_ERROR(drm, "%s: timeout\n", plane->name);
2120 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2121 if (new_crtc_state->event) {
2122 unsigned long flags;
2123 /* Get correct count/ts if racing with vblank irq */
2124 if (new_crtc_state->active)
2125 drm_crtc_accurate_vblank_count(crtc);
2126 spin_lock_irqsave(&crtc->dev->event_lock, flags);
2127 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2128 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2130 new_crtc_state->event = NULL;
2131 if (new_crtc_state->active)
2132 drm_crtc_vblank_put(crtc);
2136 drm_atomic_helper_commit_hw_done(state);
2137 drm_atomic_helper_cleanup_planes(dev, state);
2138 drm_atomic_helper_commit_cleanup_done(state);
2139 drm_atomic_state_put(state);
2141 /* Drop the RPM ref we got from nv50_disp_atomic_commit() */
2142 pm_runtime_mark_last_busy(dev->dev);
2143 pm_runtime_put_autosuspend(dev->dev);
2147 nv50_disp_atomic_commit_work(struct work_struct *work)
2149 struct drm_atomic_state *state =
2150 container_of(work, typeof(*state), commit_work);
2151 nv50_disp_atomic_commit_tail(state);
2155 nv50_disp_atomic_commit(struct drm_device *dev,
2156 struct drm_atomic_state *state, bool nonblock)
2158 struct drm_plane_state *new_plane_state;
2159 struct drm_plane *plane;
2162 ret = pm_runtime_get_sync(dev->dev);
2163 if (ret < 0 && ret != -EACCES)
2166 ret = drm_atomic_helper_setup_commit(state, nonblock);
2170 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
2172 ret = drm_atomic_helper_prepare_planes(dev, state);
2177 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2182 ret = drm_atomic_helper_swap_state(state, true);
2186 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2187 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2188 struct nv50_wndw *wndw = nv50_wndw(plane);
2190 if (asyw->set.image)
2191 nv50_wndw_ntfy_enable(wndw, asyw);
2194 drm_atomic_state_get(state);
2197 * Grab another RPM ref for the commit tail, which will release the
2198 * ref when it's finished
2200 pm_runtime_get_noresume(dev->dev);
2203 queue_work(system_unbound_wq, &state->commit_work);
2205 nv50_disp_atomic_commit_tail(state);
2209 drm_atomic_helper_cleanup_planes(dev, state);
2211 pm_runtime_put_autosuspend(dev->dev);
2215 static struct nv50_outp_atom *
2216 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
2218 struct nv50_outp_atom *outp;
2220 list_for_each_entry(outp, &atom->outp, head) {
2221 if (outp->encoder == encoder)
2225 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2227 return ERR_PTR(-ENOMEM);
2229 list_add(&outp->head, &atom->outp);
2230 outp->encoder = encoder;
2235 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2236 struct drm_connector_state *old_connector_state)
2238 struct drm_encoder *encoder = old_connector_state->best_encoder;
2239 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2240 struct drm_crtc *crtc;
2241 struct nv50_outp_atom *outp;
2243 if (!(crtc = old_connector_state->crtc))
2246 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
2247 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2248 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2249 outp = nv50_disp_outp_atomic_add(atom, encoder);
2251 return PTR_ERR(outp);
2253 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
2254 outp->flush_disable = true;
2255 atom->flush_disable = true;
2257 outp->clr.ctrl = true;
2258 atom->lock_core = true;
2265 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
2266 struct drm_connector_state *connector_state)
2268 struct drm_encoder *encoder = connector_state->best_encoder;
2269 struct drm_crtc_state *new_crtc_state;
2270 struct drm_crtc *crtc;
2271 struct nv50_outp_atom *outp;
2273 if (!(crtc = connector_state->crtc))
2276 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2277 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2278 outp = nv50_disp_outp_atomic_add(atom, encoder);
2280 return PTR_ERR(outp);
2282 outp->set.ctrl = true;
2283 atom->lock_core = true;
2290 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
2292 struct nv50_atom *atom = nv50_atom(state);
2293 struct drm_connector_state *old_connector_state, *new_connector_state;
2294 struct drm_connector *connector;
2295 struct drm_crtc_state *new_crtc_state;
2296 struct drm_crtc *crtc;
2299 /* We need to handle colour management on a per-plane basis. */
2300 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2301 if (new_crtc_state->color_mgmt_changed) {
2302 ret = drm_atomic_add_affected_planes(state, crtc);
2308 ret = drm_atomic_helper_check(dev, state);
2312 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
2313 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2317 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2322 ret = drm_dp_mst_atomic_check(state);
2330 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2332 struct nv50_atom *atom = nv50_atom(state);
2333 struct nv50_outp_atom *outp, *outt;
2335 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2336 list_del(&outp->head);
2340 drm_atomic_state_default_clear(state);
2344 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2346 struct nv50_atom *atom = nv50_atom(state);
2347 drm_atomic_state_default_release(&atom->state);
2351 static struct drm_atomic_state *
2352 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2354 struct nv50_atom *atom;
2355 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2356 drm_atomic_state_init(dev, &atom->state) < 0) {
2360 INIT_LIST_HEAD(&atom->outp);
2361 return &atom->state;
2364 static const struct drm_mode_config_funcs
2366 .fb_create = nouveau_user_framebuffer_create,
2367 .output_poll_changed = nouveau_fbcon_output_poll_changed,
2368 .atomic_check = nv50_disp_atomic_check,
2369 .atomic_commit = nv50_disp_atomic_commit,
2370 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2371 .atomic_state_clear = nv50_disp_atomic_state_clear,
2372 .atomic_state_free = nv50_disp_atomic_state_free,
2375 /******************************************************************************
2377 *****************************************************************************/
2380 nv50_display_fini(struct drm_device *dev, bool suspend)
2382 struct nouveau_encoder *nv_encoder;
2383 struct drm_encoder *encoder;
2384 struct drm_plane *plane;
2386 drm_for_each_plane(plane, dev) {
2387 struct nv50_wndw *wndw = nv50_wndw(plane);
2388 if (plane->funcs != &nv50_wndw)
2390 nv50_wndw_fini(wndw);
2393 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2394 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2395 nv_encoder = nouveau_encoder(encoder);
2396 nv50_mstm_fini(nv_encoder->dp.mstm);
2402 nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2404 struct nv50_core *core = nv50_disp(dev)->core;
2405 struct drm_encoder *encoder;
2406 struct drm_plane *plane;
2408 if (resume || runtime)
2409 core->func->init(core);
2411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2412 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2413 struct nouveau_encoder *nv_encoder =
2414 nouveau_encoder(encoder);
2415 nv50_mstm_init(nv_encoder->dp.mstm, runtime);
2419 drm_for_each_plane(plane, dev) {
2420 struct nv50_wndw *wndw = nv50_wndw(plane);
2421 if (plane->funcs != &nv50_wndw)
2423 nv50_wndw_init(wndw);
2430 nv50_display_destroy(struct drm_device *dev)
2432 struct nv50_disp *disp = nv50_disp(dev);
2434 nv50_audio_component_fini(nouveau_drm(dev));
2436 nvif_object_unmap(&disp->caps);
2437 nvif_object_fini(&disp->caps);
2438 nv50_core_del(&disp->core);
2440 nouveau_bo_unmap(disp->sync);
2442 nouveau_bo_unpin(disp->sync);
2443 nouveau_bo_ref(NULL, &disp->sync);
2445 nouveau_display(dev)->priv = NULL;
2450 nv50_display_create(struct drm_device *dev)
2452 struct nvif_device *device = &nouveau_drm(dev)->client.device;
2453 struct nouveau_drm *drm = nouveau_drm(dev);
2454 struct dcb_table *dcb = &drm->vbios.dcb;
2455 struct drm_connector *connector, *tmp;
2456 struct nv50_disp *disp;
2457 struct dcb_output *dcbe;
2459 bool has_mst = nv50_has_mst(drm);
2461 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2465 mutex_init(&disp->mutex);
2467 nouveau_display(dev)->priv = disp;
2468 nouveau_display(dev)->dtor = nv50_display_destroy;
2469 nouveau_display(dev)->init = nv50_display_init;
2470 nouveau_display(dev)->fini = nv50_display_fini;
2471 disp->disp = &nouveau_display(dev)->disp;
2472 dev->mode_config.funcs = &nv50_disp_func;
2473 dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2474 dev->mode_config.normalize_zpos = true;
2476 /* small shared memory area we use for notifiers and semaphores */
2477 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2478 0, 0x0000, NULL, NULL, &disp->sync);
2480 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
2482 ret = nouveau_bo_map(disp->sync);
2484 nouveau_bo_unpin(disp->sync);
2487 nouveau_bo_ref(NULL, &disp->sync);
2493 /* allocate master evo channel */
2494 ret = nv50_core_new(drm, &disp->core);
2498 disp->core->func->init(disp->core);
2499 if (disp->core->func->caps_init) {
2500 ret = disp->core->func->caps_init(drm, disp);
2505 /* Assign the correct format modifiers */
2506 if (disp->disp->object.oclass >= TU102_DISP)
2507 nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
2509 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
2510 nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
2512 nouveau_display(dev)->format_modifiers = disp50xx_modifiers;
2514 /* create crtc objects to represent the hw heads */
2515 if (disp->disp->object.oclass >= GV100_DISP)
2516 crtcs = nvif_rd32(&device->object, 0x610060) & 0xff;
2518 if (disp->disp->object.oclass >= GF110_DISP)
2519 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
2523 for (i = 0; i < fls(crtcs); i++) {
2524 struct nv50_head *head;
2526 if (!(crtcs & (1 << i)))
2529 head = nv50_head_create(dev, i);
2531 ret = PTR_ERR(head);
2536 head->msto = nv50_msto_new(dev, head, i);
2537 if (IS_ERR(head->msto)) {
2538 ret = PTR_ERR(head->msto);
2544 * FIXME: This is a hack to workaround the following
2547 * https://gitlab.gnome.org/GNOME/mutter/issues/759
2548 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
2550 * Once these issues are closed, this should be
2553 head->msto->encoder.possible_crtcs = crtcs;
2557 /* create encoder/connector objects based on VBIOS DCB table */
2558 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2559 connector = nouveau_connector_create(dev, dcbe);
2560 if (IS_ERR(connector))
2563 if (dcbe->location == DCB_LOC_ON_CHIP) {
2564 switch (dcbe->type) {
2565 case DCB_OUTPUT_TMDS:
2566 case DCB_OUTPUT_LVDS:
2568 ret = nv50_sor_create(connector, dcbe);
2570 case DCB_OUTPUT_ANALOG:
2571 ret = nv50_dac_create(connector, dcbe);
2578 ret = nv50_pior_create(connector, dcbe);
2582 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2583 dcbe->location, dcbe->type,
2584 ffs(dcbe->or) - 1, ret);
2589 /* cull any connectors we created that don't have an encoder */
2590 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2591 if (connector->possible_encoders)
2594 NV_WARN(drm, "%s has no encoders, removing\n",
2596 connector->funcs->destroy(connector);
2599 /* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
2600 dev->vblank_disable_immediate = true;
2602 nv50_audio_component_init(drm);
2606 nv50_display_destroy(dev);
2610 /******************************************************************************
2612 *****************************************************************************/
2614 /****************************************************************
2615 * Log2(block height) ----------------------------+ *
2616 * Page Kind ----------------------------------+ | *
2617 * Gob Height/Page Kind Generation ------+ | | *
2618 * Sector layout -------+ | | | *
2619 * Compression ------+ | | | | */
2620 const u64 disp50xx_modifiers[] = { /* | | | | | */
2621 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
2622 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
2623 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
2624 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
2625 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
2626 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
2627 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
2628 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
2629 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
2630 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
2631 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
2632 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
2633 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
2634 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
2635 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
2636 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
2637 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
2638 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
2639 DRM_FORMAT_MOD_LINEAR,
2640 DRM_FORMAT_MOD_INVALID
2643 /****************************************************************
2644 * Log2(block height) ----------------------------+ *
2645 * Page Kind ----------------------------------+ | *
2646 * Gob Height/Page Kind Generation ------+ | | *
2647 * Sector layout -------+ | | | *
2648 * Compression ------+ | | | | */
2649 const u64 disp90xx_modifiers[] = { /* | | | | | */
2650 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
2651 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
2652 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
2653 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
2654 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
2655 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
2656 DRM_FORMAT_MOD_LINEAR,
2657 DRM_FORMAT_MOD_INVALID