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 *****************************************************************************/
65 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
66 const s32 *oclass, u8 head, void *data, u32 size,
67 struct nv50_chan *chan)
69 struct nvif_sclass *sclass;
72 chan->device = device;
74 ret = n = nvif_object_sclass_get(disp, &sclass);
79 for (i = 0; i < n; i++) {
80 if (sclass[i].oclass == oclass[0]) {
81 ret = nvif_object_init(disp, 0, oclass[0],
82 data, size, &chan->user);
84 nvif_object_map(&chan->user, NULL, 0);
85 nvif_object_sclass_put(&sclass);
92 nvif_object_sclass_put(&sclass);
97 nv50_chan_destroy(struct nv50_chan *chan)
99 nvif_object_fini(&chan->user);
102 /******************************************************************************
104 *****************************************************************************/
107 nv50_dmac_destroy(struct nv50_dmac *dmac)
109 nvif_object_fini(&dmac->vram);
110 nvif_object_fini(&dmac->sync);
112 nv50_chan_destroy(&dmac->base);
114 nvif_mem_fini(&dmac->push);
118 nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
119 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
120 struct nv50_dmac *dmac)
122 struct nouveau_cli *cli = (void *)device->object.client;
123 struct nv50_disp_core_channel_dma_v0 *args = data;
124 u8 type = NVIF_MEM_COHERENT;
127 mutex_init(&dmac->lock);
129 /* Pascal added support for 47-bit physical addresses, but some
130 * parts of EVO still only accept 40-bit PAs.
132 * To avoid issues on systems with large amounts of RAM, and on
133 * systems where an IOMMU maps pages at a high address, we need
134 * to allocate push buffers in VRAM instead.
136 * This appears to match NVIDIA's behaviour on Pascal.
138 if (device->info.family == NV_DEVICE_INFO_V0_PASCAL)
139 type |= NVIF_MEM_VRAM;
141 ret = nvif_mem_init_map(&cli->mmu, type, 0x1000, &dmac->push);
145 dmac->ptr = dmac->push.object.map.ptr;
147 args->pushbuf = nvif_handle(&dmac->push.object);
149 ret = nv50_chan_create(device, disp, oclass, head, data, size,
157 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
158 &(struct nv_dma_v0) {
159 .target = NV_DMA_V0_TARGET_VRAM,
160 .access = NV_DMA_V0_ACCESS_RDWR,
161 .start = syncbuf + 0x0000,
162 .limit = syncbuf + 0x0fff,
163 }, sizeof(struct nv_dma_v0),
168 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
169 &(struct nv_dma_v0) {
170 .target = NV_DMA_V0_TARGET_VRAM,
171 .access = NV_DMA_V0_ACCESS_RDWR,
173 .limit = device->info.ram_user - 1,
174 }, sizeof(struct nv_dma_v0),
182 /******************************************************************************
183 * EVO channel helpers
184 *****************************************************************************/
186 evo_flush(struct nv50_dmac *dmac)
188 /* Push buffer fetches are not coherent with BAR1, we need to ensure
189 * writes have been flushed right through to VRAM before writing PUT.
191 if (dmac->push.type & NVIF_MEM_VRAM) {
192 struct nvif_device *device = dmac->base.device;
193 nvif_wr32(&device->object, 0x070000, 0x00000001);
194 nvif_msec(device, 2000,
195 if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
202 evo_wait(struct nv50_dmac *evoc, int nr)
204 struct nv50_dmac *dmac = evoc;
205 struct nvif_device *device = dmac->base.device;
206 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
208 mutex_lock(&dmac->lock);
209 if (put + nr >= (PAGE_SIZE / 4) - 8) {
210 dmac->ptr[put] = 0x20000000;
213 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
214 if (nvif_msec(device, 2000,
215 if (!nvif_rd32(&dmac->base.user, 0x0004))
218 mutex_unlock(&dmac->lock);
219 pr_err("nouveau: evo channel stalled\n");
226 return dmac->ptr + put;
230 evo_kick(u32 *push, struct nv50_dmac *evoc)
232 struct nv50_dmac *dmac = evoc;
236 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
237 mutex_unlock(&dmac->lock);
240 /******************************************************************************
241 * Output path helpers
242 *****************************************************************************/
244 nv50_outp_release(struct nouveau_encoder *nv_encoder)
246 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
248 struct nv50_disp_mthd_v1 base;
251 .base.method = NV50_DISP_MTHD_V1_RELEASE,
252 .base.hasht = nv_encoder->dcb->hasht,
253 .base.hashm = nv_encoder->dcb->hashm,
256 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
258 nv_encoder->link = 0;
262 nv50_outp_acquire(struct nouveau_encoder *nv_encoder, bool hda)
264 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
265 struct nv50_disp *disp = nv50_disp(drm->dev);
267 struct nv50_disp_mthd_v1 base;
268 struct nv50_disp_acquire_v0 info;
271 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
272 .base.hasht = nv_encoder->dcb->hasht,
273 .base.hashm = nv_encoder->dcb->hashm,
278 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
280 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
284 nv_encoder->or = args.info.or;
285 nv_encoder->link = args.info.link;
290 nv50_outp_atomic_check_view(struct drm_encoder *encoder,
291 struct drm_crtc_state *crtc_state,
292 struct drm_connector_state *conn_state,
293 struct drm_display_mode *native_mode)
295 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
296 struct drm_display_mode *mode = &crtc_state->mode;
297 struct drm_connector *connector = conn_state->connector;
298 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
299 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
301 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
302 asyc->scaler.full = false;
306 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
307 switch (connector->connector_type) {
308 case DRM_MODE_CONNECTOR_LVDS:
309 case DRM_MODE_CONNECTOR_eDP:
310 /* Don't force scaler for EDID modes with
311 * same size as the native one (e.g. different
314 if (mode->hdisplay == native_mode->hdisplay &&
315 mode->vdisplay == native_mode->vdisplay &&
316 mode->type & DRM_MODE_TYPE_DRIVER)
319 asyc->scaler.full = true;
328 if (!drm_mode_equal(adjusted_mode, mode)) {
329 drm_mode_copy(adjusted_mode, mode);
330 crtc_state->mode_changed = true;
337 nv50_outp_atomic_check(struct drm_encoder *encoder,
338 struct drm_crtc_state *crtc_state,
339 struct drm_connector_state *conn_state)
341 struct drm_connector *connector = conn_state->connector;
342 struct nouveau_connector *nv_connector = nouveau_connector(connector);
343 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
346 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
347 nv_connector->native_mode);
351 if (crtc_state->mode_changed || crtc_state->connectors_changed)
352 asyh->or.bpc = connector->display_info.bpc;
357 /******************************************************************************
359 *****************************************************************************/
361 nv50_dac_disable(struct drm_encoder *encoder)
363 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
364 struct nv50_core *core = nv50_disp(encoder->dev)->core;
365 if (nv_encoder->crtc)
366 core->func->dac->ctrl(core, nv_encoder->or, 0x00000000, NULL);
367 nv_encoder->crtc = NULL;
368 nv50_outp_release(nv_encoder);
372 nv50_dac_enable(struct drm_encoder *encoder)
374 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
375 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
376 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
377 struct nv50_core *core = nv50_disp(encoder->dev)->core;
379 nv50_outp_acquire(nv_encoder, false);
381 core->func->dac->ctrl(core, nv_encoder->or, 1 << nv_crtc->index, asyh);
384 nv_encoder->crtc = encoder->crtc;
387 static enum drm_connector_status
388 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
390 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
391 struct nv50_disp *disp = nv50_disp(encoder->dev);
393 struct nv50_disp_mthd_v1 base;
394 struct nv50_disp_dac_load_v0 load;
397 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
398 .base.hasht = nv_encoder->dcb->hasht,
399 .base.hashm = nv_encoder->dcb->hashm,
403 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
404 if (args.load.data == 0)
405 args.load.data = 340;
407 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
408 if (ret || !args.load.load)
409 return connector_status_disconnected;
411 return connector_status_connected;
414 static const struct drm_encoder_helper_funcs
416 .atomic_check = nv50_outp_atomic_check,
417 .enable = nv50_dac_enable,
418 .disable = nv50_dac_disable,
419 .detect = nv50_dac_detect
423 nv50_dac_destroy(struct drm_encoder *encoder)
425 drm_encoder_cleanup(encoder);
429 static const struct drm_encoder_funcs
431 .destroy = nv50_dac_destroy,
435 nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
437 struct nouveau_drm *drm = nouveau_drm(connector->dev);
438 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
439 struct nvkm_i2c_bus *bus;
440 struct nouveau_encoder *nv_encoder;
441 struct drm_encoder *encoder;
442 int type = DRM_MODE_ENCODER_DAC;
444 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
447 nv_encoder->dcb = dcbe;
449 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
451 nv_encoder->i2c = &bus->i2c;
453 encoder = to_drm_encoder(nv_encoder);
454 encoder->possible_crtcs = dcbe->heads;
455 encoder->possible_clones = 0;
456 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
457 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
458 drm_encoder_helper_add(encoder, &nv50_dac_help);
460 drm_connector_attach_encoder(connector, encoder);
465 * audio component binding for ELD notification
468 nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
471 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
472 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
477 nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
478 bool *enabled, unsigned char *buf, int max_bytes)
480 struct drm_device *drm_dev = dev_get_drvdata(kdev);
481 struct nouveau_drm *drm = nouveau_drm(drm_dev);
482 struct drm_encoder *encoder;
483 struct nouveau_encoder *nv_encoder;
484 struct nouveau_connector *nv_connector;
485 struct nouveau_crtc *nv_crtc;
489 drm_for_each_encoder(encoder, drm->dev) {
490 nv_encoder = nouveau_encoder(encoder);
491 nv_connector = nouveau_encoder_connector_get(nv_encoder);
492 nv_crtc = nouveau_crtc(encoder->crtc);
493 if (!nv_connector || !nv_crtc || nv_encoder->or != port ||
494 nv_crtc->index != dev_id)
496 *enabled = nv_encoder->audio;
498 ret = drm_eld_size(nv_connector->base.eld);
499 memcpy(buf, nv_connector->base.eld,
500 min(max_bytes, ret));
507 static const struct drm_audio_component_ops nv50_audio_component_ops = {
508 .get_eld = nv50_audio_component_get_eld,
512 nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
515 struct drm_device *drm_dev = dev_get_drvdata(kdev);
516 struct nouveau_drm *drm = nouveau_drm(drm_dev);
517 struct drm_audio_component *acomp = data;
519 if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
522 drm_modeset_lock_all(drm_dev);
523 acomp->ops = &nv50_audio_component_ops;
525 drm->audio.component = acomp;
526 drm_modeset_unlock_all(drm_dev);
531 nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
534 struct drm_device *drm_dev = dev_get_drvdata(kdev);
535 struct nouveau_drm *drm = nouveau_drm(drm_dev);
536 struct drm_audio_component *acomp = data;
538 drm_modeset_lock_all(drm_dev);
539 drm->audio.component = NULL;
542 drm_modeset_unlock_all(drm_dev);
545 static const struct component_ops nv50_audio_component_bind_ops = {
546 .bind = nv50_audio_component_bind,
547 .unbind = nv50_audio_component_unbind,
551 nv50_audio_component_init(struct nouveau_drm *drm)
553 if (!component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
554 drm->audio.component_registered = true;
558 nv50_audio_component_fini(struct nouveau_drm *drm)
560 if (drm->audio.component_registered) {
561 component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
562 drm->audio.component_registered = false;
566 /******************************************************************************
568 *****************************************************************************/
570 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
572 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
573 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
574 struct nv50_disp *disp = nv50_disp(encoder->dev);
576 struct nv50_disp_mthd_v1 base;
577 struct nv50_disp_sor_hda_eld_v0 eld;
580 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
581 .base.hasht = nv_encoder->dcb->hasht,
582 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
583 (0x0100 << nv_crtc->index),
586 nv_encoder->audio = false;
587 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
589 nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
594 nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
596 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
597 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
598 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
599 struct nouveau_connector *nv_connector;
600 struct nv50_disp *disp = nv50_disp(encoder->dev);
603 struct nv50_disp_mthd_v1 mthd;
604 struct nv50_disp_sor_hda_eld_v0 eld;
606 u8 data[sizeof(nv_connector->base.eld)];
608 .base.mthd.version = 1,
609 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
610 .base.mthd.hasht = nv_encoder->dcb->hasht,
611 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
612 (0x0100 << nv_crtc->index),
615 nv_connector = nouveau_encoder_connector_get(nv_encoder);
616 if (!drm_detect_monitor_audio(nv_connector->edid))
619 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
621 nvif_mthd(&disp->disp->object, 0, &args,
622 sizeof(args.base) + drm_eld_size(args.data));
623 nv_encoder->audio = true;
625 nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
629 /******************************************************************************
631 *****************************************************************************/
633 nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
635 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
636 struct nv50_disp *disp = nv50_disp(encoder->dev);
638 struct nv50_disp_mthd_v1 base;
639 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
642 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
643 .base.hasht = nv_encoder->dcb->hasht,
644 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
645 (0x0100 << nv_crtc->index),
648 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
652 nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
654 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
655 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
656 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
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;
661 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
664 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
665 .base.hasht = nv_encoder->dcb->hasht,
666 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
667 (0x0100 << nv_crtc->index),
669 .pwr.rekey = 56, /* binary driver, and tegra, constant */
671 struct nouveau_connector *nv_connector;
672 struct drm_hdmi_info *hdmi;
674 union hdmi_infoframe avi_frame;
675 union hdmi_infoframe vendor_frame;
676 bool high_tmds_clock_ratio = false, scrambling = false;
681 nv_connector = nouveau_encoder_connector_get(nv_encoder);
682 if (!drm_detect_hdmi_monitor(nv_connector->edid))
685 hdmi = &nv_connector->base.display_info.hdmi;
687 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi,
688 &nv_connector->base, mode);
690 /* We have an AVI InfoFrame, populate it to the display */
691 args.pwr.avi_infoframe_length
692 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
695 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
696 &nv_connector->base, mode);
698 /* We have a Vendor InfoFrame, populate it to the display */
699 args.pwr.vendor_infoframe_length
700 = hdmi_infoframe_pack(&vendor_frame,
702 + args.pwr.avi_infoframe_length,
706 max_ac_packet = mode->htotal - mode->hdisplay;
707 max_ac_packet -= args.pwr.rekey;
708 max_ac_packet -= 18; /* constant from tegra */
709 args.pwr.max_ac_packet = max_ac_packet / 32;
711 if (hdmi->scdc.scrambling.supported) {
712 high_tmds_clock_ratio = mode->clock > 340000;
713 scrambling = high_tmds_clock_ratio ||
714 hdmi->scdc.scrambling.low_rates;
718 NV50_DISP_SOR_HDMI_PWR_V0_SCDC_SCRAMBLE * scrambling |
719 NV50_DISP_SOR_HDMI_PWR_V0_SCDC_DIV_BY_4 * high_tmds_clock_ratio;
721 size = sizeof(args.base)
723 + args.pwr.avi_infoframe_length
724 + args.pwr.vendor_infoframe_length;
725 nvif_mthd(&disp->disp->object, 0, &args, size);
727 nv50_audio_enable(encoder, mode);
729 /* If SCDC is supported by the downstream monitor, update
730 * divider / scrambling settings to what we programmed above.
732 if (!hdmi->scdc.scrambling.supported)
735 ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &config);
737 NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
740 config &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
741 config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 * high_tmds_clock_ratio;
742 config |= SCDC_SCRAMBLING_ENABLE * scrambling;
743 ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, config);
745 NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
749 /******************************************************************************
751 *****************************************************************************/
752 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
753 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
754 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
757 struct nouveau_encoder *outp;
759 struct drm_dp_mst_topology_mgr mgr;
767 struct nv50_mstm *mstm;
768 struct drm_dp_mst_port *port;
769 struct drm_connector connector;
771 struct drm_display_mode *native;
776 struct drm_encoder encoder;
778 struct nv50_head *head;
779 struct nv50_mstc *mstc;
783 static struct drm_dp_payload *
784 nv50_msto_payload(struct nv50_msto *msto)
786 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
787 struct nv50_mstc *mstc = msto->mstc;
788 struct nv50_mstm *mstm = mstc->mstm;
789 int vcpi = mstc->port->vcpi.vcpi, i;
791 WARN_ON(!mutex_is_locked(&mstm->mgr.payload_lock));
793 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
794 for (i = 0; i < mstm->mgr.max_payloads; i++) {
795 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
796 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
797 mstm->outp->base.base.name, i, payload->vcpi,
798 payload->start_slot, payload->num_slots);
801 for (i = 0; i < mstm->mgr.max_payloads; i++) {
802 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
803 if (payload->vcpi == vcpi)
811 nv50_msto_cleanup(struct nv50_msto *msto)
813 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
814 struct nv50_mstc *mstc = msto->mstc;
815 struct nv50_mstm *mstm = mstc->mstm;
820 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
822 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
825 msto->disabled = false;
829 nv50_msto_prepare(struct nv50_msto *msto)
831 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
832 struct nv50_mstc *mstc = msto->mstc;
833 struct nv50_mstm *mstm = mstc->mstm;
835 struct nv50_disp_mthd_v1 base;
836 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
839 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
840 .base.hasht = mstm->outp->dcb->hasht,
841 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
842 (0x0100 << msto->head->base.index),
845 mutex_lock(&mstm->mgr.payload_lock);
847 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
848 if (mstc->port->vcpi.vcpi > 0) {
849 struct drm_dp_payload *payload = nv50_msto_payload(msto);
851 args.vcpi.start_slot = payload->start_slot;
852 args.vcpi.num_slots = payload->num_slots;
853 args.vcpi.pbn = mstc->port->vcpi.pbn;
854 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
858 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
859 msto->encoder.name, msto->head->base.base.name,
860 args.vcpi.start_slot, args.vcpi.num_slots,
861 args.vcpi.pbn, args.vcpi.aligned_pbn);
863 nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
864 mutex_unlock(&mstm->mgr.payload_lock);
868 nv50_msto_atomic_check(struct drm_encoder *encoder,
869 struct drm_crtc_state *crtc_state,
870 struct drm_connector_state *conn_state)
872 struct drm_atomic_state *state = crtc_state->state;
873 struct drm_connector *connector = conn_state->connector;
874 struct nv50_mstc *mstc = nv50_mstc(connector);
875 struct nv50_mstm *mstm = mstc->mstm;
876 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
880 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
885 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
889 * When restoring duplicated states, we need to make sure that the bw
890 * remains the same and avoid recalculating it, as the connector's bpc
891 * may have changed after the state was duplicated
893 if (!state->duplicated) {
894 const int clock = crtc_state->adjusted_mode.clock;
896 asyh->or.bpc = connector->display_info.bpc;
897 asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3,
901 slots = drm_dp_atomic_find_vcpi_slots(state, &mstm->mgr, mstc->port,
912 nv50_dp_bpc_to_depth(unsigned int bpc)
917 case 10: /* fall-through */
923 nv50_msto_enable(struct drm_encoder *encoder)
925 struct nv50_head *head = nv50_head(encoder->crtc);
926 struct nv50_head_atom *armh = nv50_head_atom(head->base.base.state);
927 struct nv50_msto *msto = nv50_msto(encoder);
928 struct nv50_mstc *mstc = NULL;
929 struct nv50_mstm *mstm = NULL;
930 struct drm_connector *connector;
931 struct drm_connector_list_iter conn_iter;
935 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
936 drm_for_each_connector_iter(connector, &conn_iter) {
937 if (connector->state->best_encoder == &msto->encoder) {
938 mstc = nv50_mstc(connector);
943 drm_connector_list_iter_end(&conn_iter);
948 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, armh->dp.pbn,
951 DRM_DEBUG_KMS("Failed to allocate VCPI\n");
954 nv50_outp_acquire(mstm->outp, false /*XXX: MST audio.*/);
956 if (mstm->outp->link & 1)
961 mstm->outp->update(mstm->outp, head->base.index, armh, proto,
962 nv50_dp_bpc_to_depth(armh->or.bpc));
965 mstm->modified = true;
969 nv50_msto_disable(struct drm_encoder *encoder)
971 struct nv50_msto *msto = nv50_msto(encoder);
972 struct nv50_mstc *mstc = msto->mstc;
973 struct nv50_mstm *mstm = mstc->mstm;
975 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
977 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
978 mstm->modified = true;
980 mstm->disabled = true;
981 msto->disabled = true;
984 static const struct drm_encoder_helper_funcs
986 .disable = nv50_msto_disable,
987 .enable = nv50_msto_enable,
988 .atomic_check = nv50_msto_atomic_check,
992 nv50_msto_destroy(struct drm_encoder *encoder)
994 struct nv50_msto *msto = nv50_msto(encoder);
995 drm_encoder_cleanup(&msto->encoder);
999 static const struct drm_encoder_funcs
1001 .destroy = nv50_msto_destroy,
1004 static struct nv50_msto *
1005 nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1007 struct nv50_msto *msto;
1010 msto = kzalloc(sizeof(*msto), GFP_KERNEL);
1012 return ERR_PTR(-ENOMEM);
1014 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1015 DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1018 return ERR_PTR(ret);
1021 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1022 msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
1027 static struct drm_encoder *
1028 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1029 struct drm_connector_state *connector_state)
1031 struct nv50_mstc *mstc = nv50_mstc(connector);
1032 struct drm_crtc *crtc = connector_state->crtc;
1034 if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1037 return &nv50_head(crtc)->msto->encoder;
1040 static enum drm_mode_status
1041 nv50_mstc_mode_valid(struct drm_connector *connector,
1042 struct drm_display_mode *mode)
1044 struct nv50_mstc *mstc = nv50_mstc(connector);
1045 struct nouveau_encoder *outp = mstc->mstm->outp;
1047 /* TODO: calculate the PBN from the dotclock and validate against the
1048 * MSTB's max possible PBN
1051 return nv50_dp_mode_valid(connector, outp, mode, NULL);
1055 nv50_mstc_get_modes(struct drm_connector *connector)
1057 struct nv50_mstc *mstc = nv50_mstc(connector);
1060 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1061 drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1063 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
1066 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
1067 * to 8 to save bandwidth on the topology. In the future, we'll want
1068 * to properly fix this by dynamically selecting the highest possible
1069 * bpc that would fit in the topology
1071 if (connector->display_info.bpc)
1072 connector->display_info.bpc =
1073 clamp(connector->display_info.bpc, 6U, 8U);
1075 connector->display_info.bpc = 8;
1078 drm_mode_destroy(mstc->connector.dev, mstc->native);
1079 mstc->native = nouveau_conn_native_mode(&mstc->connector);
1084 nv50_mstc_atomic_check(struct drm_connector *connector,
1085 struct drm_atomic_state *state)
1087 struct nv50_mstc *mstc = nv50_mstc(connector);
1088 struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;
1089 struct drm_connector_state *new_conn_state =
1090 drm_atomic_get_new_connector_state(state, connector);
1091 struct drm_connector_state *old_conn_state =
1092 drm_atomic_get_old_connector_state(state, connector);
1093 struct drm_crtc_state *crtc_state;
1094 struct drm_crtc *new_crtc = new_conn_state->crtc;
1096 if (!old_conn_state->crtc)
1099 /* We only want to free VCPI if this state disables the CRTC on this
1103 crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
1106 !drm_atomic_crtc_needs_modeset(crtc_state) ||
1111 return drm_dp_atomic_release_vcpi_slots(state, mgr, mstc->port);
1115 nv50_mstc_detect(struct drm_connector *connector,
1116 struct drm_modeset_acquire_ctx *ctx, bool force)
1118 struct nv50_mstc *mstc = nv50_mstc(connector);
1121 if (drm_connector_is_unregistered(connector))
1122 return connector_status_disconnected;
1124 ret = pm_runtime_get_sync(connector->dev->dev);
1125 if (ret < 0 && ret != -EACCES) {
1126 pm_runtime_put_autosuspend(connector->dev->dev);
1127 return connector_status_disconnected;
1130 ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
1133 pm_runtime_mark_last_busy(connector->dev->dev);
1134 pm_runtime_put_autosuspend(connector->dev->dev);
1138 static const struct drm_connector_helper_funcs
1140 .get_modes = nv50_mstc_get_modes,
1141 .mode_valid = nv50_mstc_mode_valid,
1142 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
1143 .atomic_check = nv50_mstc_atomic_check,
1144 .detect_ctx = nv50_mstc_detect,
1148 nv50_mstc_destroy(struct drm_connector *connector)
1150 struct nv50_mstc *mstc = nv50_mstc(connector);
1152 drm_connector_cleanup(&mstc->connector);
1153 drm_dp_mst_put_port_malloc(mstc->port);
1158 static const struct drm_connector_funcs
1160 .reset = nouveau_conn_reset,
1161 .fill_modes = drm_helper_probe_single_connector_modes,
1162 .destroy = nv50_mstc_destroy,
1163 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1164 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1165 .atomic_set_property = nouveau_conn_atomic_set_property,
1166 .atomic_get_property = nouveau_conn_atomic_get_property,
1170 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
1171 const char *path, struct nv50_mstc **pmstc)
1173 struct drm_device *dev = mstm->outp->base.base.dev;
1174 struct drm_crtc *crtc;
1175 struct nv50_mstc *mstc;
1178 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
1183 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
1184 DRM_MODE_CONNECTOR_DisplayPort);
1191 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
1193 mstc->connector.funcs->reset(&mstc->connector);
1194 nouveau_conn_attach_properties(&mstc->connector);
1196 drm_for_each_crtc(crtc, dev) {
1197 if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1200 drm_connector_attach_encoder(&mstc->connector,
1201 &nv50_head(crtc)->msto->encoder);
1204 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
1205 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1206 drm_connector_set_path_property(&mstc->connector, path);
1207 drm_dp_mst_get_port_malloc(port);
1212 nv50_mstm_cleanup(struct nv50_mstm *mstm)
1214 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1215 struct drm_encoder *encoder;
1218 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1219 ret = drm_dp_check_act_status(&mstm->mgr);
1221 ret = drm_dp_update_payload_part2(&mstm->mgr);
1223 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1224 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1225 struct nv50_msto *msto = nv50_msto(encoder);
1226 struct nv50_mstc *mstc = msto->mstc;
1227 if (mstc && mstc->mstm == mstm)
1228 nv50_msto_cleanup(msto);
1232 mstm->modified = false;
1236 nv50_mstm_prepare(struct nv50_mstm *mstm)
1238 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1239 struct drm_encoder *encoder;
1242 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
1243 ret = drm_dp_update_payload_part1(&mstm->mgr);
1245 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1246 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1247 struct nv50_msto *msto = nv50_msto(encoder);
1248 struct nv50_mstc *mstc = msto->mstc;
1249 if (mstc && mstc->mstm == mstm)
1250 nv50_msto_prepare(msto);
1254 if (mstm->disabled) {
1256 nv50_outp_release(mstm->outp);
1257 mstm->disabled = false;
1261 static struct drm_connector *
1262 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1263 struct drm_dp_mst_port *port, const char *path)
1265 struct nv50_mstm *mstm = nv50_mstm(mgr);
1266 struct nv50_mstc *mstc;
1269 ret = nv50_mstc_new(mstm, port, path, &mstc);
1273 return &mstc->connector;
1276 static const struct drm_dp_mst_topology_cbs
1278 .add_connector = nv50_mstm_add_connector,
1282 nv50_mstm_service(struct nv50_mstm *mstm)
1284 struct drm_dp_aux *aux = mstm ? mstm->mgr.aux : NULL;
1285 bool handled = true;
1293 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1295 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1299 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1303 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
1308 nv50_mstm_remove(struct nv50_mstm *mstm)
1311 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1315 nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
1317 struct nouveau_encoder *outp = mstm->outp;
1319 struct nv50_disp_mthd_v1 base;
1320 struct nv50_disp_sor_dp_mst_link_v0 mst;
1323 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
1324 .base.hasht = outp->dcb->hasht,
1325 .base.hashm = outp->dcb->hashm,
1328 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
1329 struct nvif_object *disp = &drm->display->disp.object;
1333 /* Even if we're enabling MST, start with disabling the
1334 * branching unit to clear any sink-side MST topology state
1335 * that wasn't set by us
1337 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, 0);
1342 /* Now, start initializing */
1343 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL,
1350 return nvif_mthd(disp, 0, &args, sizeof(args));
1354 nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
1356 struct drm_dp_aux *aux;
1358 bool old_state, new_state;
1364 mutex_lock(&mstm->mgr.lock);
1366 old_state = mstm->mgr.mst_state;
1367 new_state = old_state;
1368 aux = mstm->mgr.aux;
1371 /* Just check that the MST hub is still as we expect it */
1372 ret = drm_dp_dpcd_readb(aux, DP_MSTM_CTRL, &mstm_ctrl);
1373 if (ret < 0 || !(mstm_ctrl & DP_MST_EN)) {
1374 DRM_DEBUG_KMS("Hub gone, disabling MST topology\n");
1377 } else if (dpcd[0] >= 0x12) {
1378 ret = drm_dp_dpcd_readb(aux, DP_MSTM_CAP, &dpcd[1]);
1382 if (!(dpcd[1] & DP_MST_CAP))
1388 if (new_state == old_state) {
1389 mutex_unlock(&mstm->mgr.lock);
1393 ret = nv50_mstm_enable(mstm, dpcd[0], new_state);
1397 mutex_unlock(&mstm->mgr.lock);
1399 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, new_state);
1401 return nv50_mstm_enable(mstm, dpcd[0], 0);
1406 mutex_unlock(&mstm->mgr.lock);
1411 nv50_mstm_fini(struct nv50_mstm *mstm)
1413 if (mstm && mstm->mgr.mst_state)
1414 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1418 nv50_mstm_init(struct nv50_mstm *mstm, bool runtime)
1422 if (!mstm || !mstm->mgr.mst_state)
1425 ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
1427 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1428 drm_kms_helper_hotplug_event(mstm->mgr.dev);
1433 nv50_mstm_del(struct nv50_mstm **pmstm)
1435 struct nv50_mstm *mstm = *pmstm;
1437 drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1444 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1445 int conn_base_id, struct nv50_mstm **pmstm)
1447 const int max_payloads = hweight8(outp->dcb->heads);
1448 struct drm_device *dev = outp->base.base.dev;
1449 struct nv50_mstm *mstm;
1453 /* This is a workaround for some monitors not functioning
1454 * correctly in MST mode on initial module load. I think
1455 * some bad interaction with the VBIOS may be responsible.
1457 * A good ol' off and on again seems to work here ;)
1459 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
1460 if (ret >= 0 && dpcd >= 0x12)
1461 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1463 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1466 mstm->mgr.cbs = &nv50_mstm;
1468 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1469 max_payloads, conn_base_id);
1476 /******************************************************************************
1478 *****************************************************************************/
1480 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1481 struct nv50_head_atom *asyh, u8 proto, u8 depth)
1483 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1484 struct nv50_core *core = disp->core;
1487 nv_encoder->ctrl &= ~BIT(head);
1488 if (!(nv_encoder->ctrl & 0x0000000f))
1489 nv_encoder->ctrl = 0;
1491 nv_encoder->ctrl |= proto << 8;
1492 nv_encoder->ctrl |= BIT(head);
1493 asyh->or.depth = depth;
1496 core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
1500 nv50_sor_disable(struct drm_encoder *encoder)
1502 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1503 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1505 nv_encoder->crtc = NULL;
1508 struct nvkm_i2c_aux *aux = nv_encoder->aux;
1512 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
1514 pwr &= ~DP_SET_POWER_MASK;
1515 pwr |= DP_SET_POWER_D3;
1516 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
1520 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
1521 nv50_audio_disable(encoder, nv_crtc);
1522 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
1523 nv50_outp_release(nv_encoder);
1528 nv50_sor_enable(struct drm_encoder *encoder)
1530 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1531 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1532 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1533 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1535 struct nv50_disp_mthd_v1 base;
1536 struct nv50_disp_sor_lvds_script_v0 lvds;
1539 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1540 .base.hasht = nv_encoder->dcb->hasht,
1541 .base.hashm = nv_encoder->dcb->hashm,
1543 struct nv50_disp *disp = nv50_disp(encoder->dev);
1544 struct drm_device *dev = encoder->dev;
1545 struct nouveau_drm *drm = nouveau_drm(dev);
1546 struct nouveau_connector *nv_connector;
1547 struct nvbios *bios = &drm->vbios;
1552 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1553 nv_encoder->crtc = encoder->crtc;
1555 if ((disp->disp->object.oclass == GT214_DISP ||
1556 disp->disp->object.oclass >= GF110_DISP) &&
1557 drm_detect_monitor_audio(nv_connector->edid))
1559 nv50_outp_acquire(nv_encoder, hda);
1561 switch (nv_encoder->dcb->type) {
1562 case DCB_OUTPUT_TMDS:
1563 if (nv_encoder->link & 1) {
1565 /* Only enable dual-link if:
1566 * - Need to (i.e. rate > 165MHz)
1568 * - Not an HDMI monitor, since there's no dual-link
1571 if (mode->clock >= 165000 &&
1572 nv_encoder->dcb->duallink_possible &&
1573 !drm_detect_hdmi_monitor(nv_connector->edid))
1579 nv50_hdmi_enable(&nv_encoder->base.base, mode);
1581 case DCB_OUTPUT_LVDS:
1584 if (bios->fp_no_ddc) {
1585 if (bios->fp.dual_link)
1586 lvds.lvds.script |= 0x0100;
1587 if (bios->fp.if_is_24bit)
1588 lvds.lvds.script |= 0x0200;
1590 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1591 if (((u8 *)nv_connector->edid)[121] == 2)
1592 lvds.lvds.script |= 0x0100;
1594 if (mode->clock >= bios->fp.duallink_transition_clk) {
1595 lvds.lvds.script |= 0x0100;
1598 if (lvds.lvds.script & 0x0100) {
1599 if (bios->fp.strapless_is_24bit & 2)
1600 lvds.lvds.script |= 0x0200;
1602 if (bios->fp.strapless_is_24bit & 1)
1603 lvds.lvds.script |= 0x0200;
1606 if (asyh->or.bpc == 8)
1607 lvds.lvds.script |= 0x0200;
1610 nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
1613 depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1615 if (nv_encoder->link & 1)
1620 nv50_audio_enable(encoder, mode);
1627 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1630 static const struct drm_encoder_helper_funcs
1632 .atomic_check = nv50_outp_atomic_check,
1633 .enable = nv50_sor_enable,
1634 .disable = nv50_sor_disable,
1638 nv50_sor_destroy(struct drm_encoder *encoder)
1640 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1641 nv50_mstm_del(&nv_encoder->dp.mstm);
1642 drm_encoder_cleanup(encoder);
1646 static const struct drm_encoder_funcs
1648 .destroy = nv50_sor_destroy,
1651 static bool nv50_has_mst(struct nouveau_drm *drm)
1653 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
1655 u8 ver, hdr, cnt, len;
1657 data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len);
1658 return data && ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04);
1662 nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
1664 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1665 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1666 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1667 struct nouveau_encoder *nv_encoder;
1668 struct drm_encoder *encoder;
1669 struct nv50_disp *disp = nv50_disp(connector->dev);
1672 switch (dcbe->type) {
1673 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1674 case DCB_OUTPUT_TMDS:
1677 type = DRM_MODE_ENCODER_TMDS;
1681 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1684 nv_encoder->dcb = dcbe;
1685 nv_encoder->update = nv50_sor_update;
1687 encoder = to_drm_encoder(nv_encoder);
1688 encoder->possible_crtcs = dcbe->heads;
1689 encoder->possible_clones = 0;
1690 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1691 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1692 drm_encoder_helper_add(encoder, &nv50_sor_help);
1694 drm_connector_attach_encoder(connector, encoder);
1696 disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1698 if (dcbe->type == DCB_OUTPUT_DP) {
1699 struct nvkm_i2c_aux *aux =
1700 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1703 if (disp->disp->object.oclass < GF110_DISP) {
1704 /* HW has no support for address-only
1705 * transactions, so we're required to
1706 * use custom I2C-over-AUX code.
1708 nv_encoder->i2c = &aux->i2c;
1710 nv_encoder->i2c = &nv_connector->aux.ddc;
1712 nv_encoder->aux = aux;
1715 if (nv_connector->type != DCB_CONNECTOR_eDP &&
1716 nv50_has_mst(drm)) {
1717 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
1718 16, nv_connector->base.base.id,
1719 &nv_encoder->dp.mstm);
1724 struct nvkm_i2c_bus *bus =
1725 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1727 nv_encoder->i2c = &bus->i2c;
1733 /******************************************************************************
1735 *****************************************************************************/
1737 nv50_pior_atomic_check(struct drm_encoder *encoder,
1738 struct drm_crtc_state *crtc_state,
1739 struct drm_connector_state *conn_state)
1741 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1744 crtc_state->adjusted_mode.clock *= 2;
1749 nv50_pior_disable(struct drm_encoder *encoder)
1751 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1752 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1753 if (nv_encoder->crtc)
1754 core->func->pior->ctrl(core, nv_encoder->or, 0x00000000, NULL);
1755 nv_encoder->crtc = NULL;
1756 nv50_outp_release(nv_encoder);
1760 nv50_pior_enable(struct drm_encoder *encoder)
1762 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1763 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1764 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1765 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1766 u8 owner = 1 << nv_crtc->index;
1769 nv50_outp_acquire(nv_encoder, false);
1771 switch (asyh->or.bpc) {
1772 case 10: asyh->or.depth = 0x6; break;
1773 case 8: asyh->or.depth = 0x5; break;
1774 case 6: asyh->or.depth = 0x2; break;
1775 default: asyh->or.depth = 0x0; break;
1778 switch (nv_encoder->dcb->type) {
1779 case DCB_OUTPUT_TMDS:
1788 core->func->pior->ctrl(core, nv_encoder->or, (proto << 8) | owner, asyh);
1789 nv_encoder->crtc = encoder->crtc;
1792 static const struct drm_encoder_helper_funcs
1794 .atomic_check = nv50_pior_atomic_check,
1795 .enable = nv50_pior_enable,
1796 .disable = nv50_pior_disable,
1800 nv50_pior_destroy(struct drm_encoder *encoder)
1802 drm_encoder_cleanup(encoder);
1806 static const struct drm_encoder_funcs
1808 .destroy = nv50_pior_destroy,
1812 nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1814 struct drm_device *dev = connector->dev;
1815 struct nouveau_drm *drm = nouveau_drm(dev);
1816 struct nv50_disp *disp = nv50_disp(dev);
1817 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1818 struct nvkm_i2c_bus *bus = NULL;
1819 struct nvkm_i2c_aux *aux = NULL;
1820 struct i2c_adapter *ddc;
1821 struct nouveau_encoder *nv_encoder;
1822 struct drm_encoder *encoder;
1825 switch (dcbe->type) {
1826 case DCB_OUTPUT_TMDS:
1827 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1828 ddc = bus ? &bus->i2c : NULL;
1829 type = DRM_MODE_ENCODER_TMDS;
1832 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
1833 ddc = aux ? &aux->i2c : NULL;
1834 type = DRM_MODE_ENCODER_TMDS;
1840 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1843 nv_encoder->dcb = dcbe;
1844 nv_encoder->i2c = ddc;
1845 nv_encoder->aux = aux;
1847 encoder = to_drm_encoder(nv_encoder);
1848 encoder->possible_crtcs = dcbe->heads;
1849 encoder->possible_clones = 0;
1850 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1851 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
1852 drm_encoder_helper_add(encoder, &nv50_pior_help);
1854 drm_connector_attach_encoder(connector, encoder);
1856 disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1861 /******************************************************************************
1863 *****************************************************************************/
1866 nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
1868 struct nouveau_drm *drm = nouveau_drm(state->dev);
1869 struct nv50_disp *disp = nv50_disp(drm->dev);
1870 struct nv50_core *core = disp->core;
1871 struct nv50_mstm *mstm;
1872 struct drm_encoder *encoder;
1874 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
1876 drm_for_each_encoder(encoder, drm->dev) {
1877 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1878 mstm = nouveau_encoder(encoder)->dp.mstm;
1879 if (mstm && mstm->modified)
1880 nv50_mstm_prepare(mstm);
1884 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1885 core->func->update(core, interlock, true);
1886 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1887 disp->core->chan.base.device))
1888 NV_ERROR(drm, "core notifier timeout\n");
1890 drm_for_each_encoder(encoder, drm->dev) {
1891 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1892 mstm = nouveau_encoder(encoder)->dp.mstm;
1893 if (mstm && mstm->modified)
1894 nv50_mstm_cleanup(mstm);
1900 nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
1902 struct drm_plane_state *new_plane_state;
1903 struct drm_plane *plane;
1906 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1907 struct nv50_wndw *wndw = nv50_wndw(plane);
1908 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1909 if (wndw->func->update)
1910 wndw->func->update(wndw, interlock);
1916 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1918 struct drm_device *dev = state->dev;
1919 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
1920 struct drm_crtc *crtc;
1921 struct drm_plane_state *new_plane_state;
1922 struct drm_plane *plane;
1923 struct nouveau_drm *drm = nouveau_drm(dev);
1924 struct nv50_disp *disp = nv50_disp(dev);
1925 struct nv50_atom *atom = nv50_atom(state);
1926 struct nv50_core *core = disp->core;
1927 struct nv50_outp_atom *outp, *outt;
1928 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
1931 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
1932 drm_atomic_helper_wait_for_fences(dev, state, false);
1933 drm_atomic_helper_wait_for_dependencies(state);
1934 drm_atomic_helper_update_legacy_modeset_state(dev, state);
1936 if (atom->lock_core)
1937 mutex_lock(&disp->mutex);
1939 /* Disable head(s). */
1940 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1941 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
1942 struct nv50_head *head = nv50_head(crtc);
1944 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
1945 asyh->clr.mask, asyh->set.mask);
1947 if (old_crtc_state->active && !new_crtc_state->active) {
1948 pm_runtime_put_noidle(dev->dev);
1949 drm_crtc_vblank_off(crtc);
1952 if (asyh->clr.mask) {
1953 nv50_head_flush_clr(head, asyh, atom->flush_disable);
1954 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
1958 /* Disable plane(s). */
1959 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1960 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
1961 struct nv50_wndw *wndw = nv50_wndw(plane);
1963 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
1964 asyw->clr.mask, asyw->set.mask);
1965 if (!asyw->clr.mask)
1968 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
1971 /* Disable output path(s). */
1972 list_for_each_entry(outp, &atom->outp, head) {
1973 const struct drm_encoder_helper_funcs *help;
1974 struct drm_encoder *encoder;
1976 encoder = outp->encoder;
1977 help = encoder->helper_private;
1979 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
1980 outp->clr.mask, outp->set.mask);
1982 if (outp->clr.mask) {
1983 help->disable(encoder);
1984 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
1985 if (outp->flush_disable) {
1986 nv50_disp_atomic_commit_wndw(state, interlock);
1987 nv50_disp_atomic_commit_core(state, interlock);
1988 memset(interlock, 0x00, sizeof(interlock));
1993 /* Flush disable. */
1994 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
1995 if (atom->flush_disable) {
1996 nv50_disp_atomic_commit_wndw(state, interlock);
1997 nv50_disp_atomic_commit_core(state, interlock);
1998 memset(interlock, 0x00, sizeof(interlock));
2002 /* Update output path(s). */
2003 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2004 const struct drm_encoder_helper_funcs *help;
2005 struct drm_encoder *encoder;
2007 encoder = outp->encoder;
2008 help = encoder->helper_private;
2010 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
2011 outp->set.mask, outp->clr.mask);
2013 if (outp->set.mask) {
2014 help->enable(encoder);
2015 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2018 list_del(&outp->head);
2022 /* Update head(s). */
2023 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2024 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2025 struct nv50_head *head = nv50_head(crtc);
2027 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2028 asyh->set.mask, asyh->clr.mask);
2030 if (asyh->set.mask) {
2031 nv50_head_flush_set(head, asyh);
2032 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2035 if (new_crtc_state->active) {
2036 if (!old_crtc_state->active) {
2037 drm_crtc_vblank_on(crtc);
2038 pm_runtime_get_noresume(dev->dev);
2040 if (new_crtc_state->event)
2041 drm_crtc_vblank_get(crtc);
2045 /* Update window->head assignment.
2047 * This has to happen in an update that's not interlocked with
2048 * any window channels to avoid hitting HW error checks.
2050 *TODO: Proper handling of window ownership (Turing apparently
2051 * supports non-fixed mappings).
2053 if (core->assign_windows) {
2054 core->func->wndw.owner(core);
2055 core->func->update(core, interlock, false);
2056 core->assign_windows = false;
2057 interlock[NV50_DISP_INTERLOCK_CORE] = 0;
2060 /* Update plane(s). */
2061 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2062 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2063 struct nv50_wndw *wndw = nv50_wndw(plane);
2065 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
2066 asyw->set.mask, asyw->clr.mask);
2067 if ( !asyw->set.mask &&
2068 (!asyw->clr.mask || atom->flush_disable))
2071 nv50_wndw_flush_set(wndw, interlock, asyw);
2075 nv50_disp_atomic_commit_wndw(state, interlock);
2077 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2078 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2079 interlock[NV50_DISP_INTERLOCK_OVLY] ||
2080 interlock[NV50_DISP_INTERLOCK_WNDW] ||
2081 !atom->state.legacy_cursor_update)
2082 nv50_disp_atomic_commit_core(state, interlock);
2084 disp->core->func->update(disp->core, interlock, false);
2087 if (atom->lock_core)
2088 mutex_unlock(&disp->mutex);
2090 /* Wait for HW to signal completion. */
2091 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2092 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2093 struct nv50_wndw *wndw = nv50_wndw(plane);
2094 int ret = nv50_wndw_wait_armed(wndw, asyw);
2096 NV_ERROR(drm, "%s: timeout\n", plane->name);
2099 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2100 if (new_crtc_state->event) {
2101 unsigned long flags;
2102 /* Get correct count/ts if racing with vblank irq */
2103 if (new_crtc_state->active)
2104 drm_crtc_accurate_vblank_count(crtc);
2105 spin_lock_irqsave(&crtc->dev->event_lock, flags);
2106 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2107 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2109 new_crtc_state->event = NULL;
2110 if (new_crtc_state->active)
2111 drm_crtc_vblank_put(crtc);
2115 drm_atomic_helper_commit_hw_done(state);
2116 drm_atomic_helper_cleanup_planes(dev, state);
2117 drm_atomic_helper_commit_cleanup_done(state);
2118 drm_atomic_state_put(state);
2120 /* Drop the RPM ref we got from nv50_disp_atomic_commit() */
2121 pm_runtime_mark_last_busy(dev->dev);
2122 pm_runtime_put_autosuspend(dev->dev);
2126 nv50_disp_atomic_commit_work(struct work_struct *work)
2128 struct drm_atomic_state *state =
2129 container_of(work, typeof(*state), commit_work);
2130 nv50_disp_atomic_commit_tail(state);
2134 nv50_disp_atomic_commit(struct drm_device *dev,
2135 struct drm_atomic_state *state, bool nonblock)
2137 struct drm_plane_state *new_plane_state;
2138 struct drm_plane *plane;
2141 ret = pm_runtime_get_sync(dev->dev);
2142 if (ret < 0 && ret != -EACCES)
2145 ret = drm_atomic_helper_setup_commit(state, nonblock);
2149 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
2151 ret = drm_atomic_helper_prepare_planes(dev, state);
2156 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2161 ret = drm_atomic_helper_swap_state(state, true);
2165 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2166 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2167 struct nv50_wndw *wndw = nv50_wndw(plane);
2169 if (asyw->set.image)
2170 nv50_wndw_ntfy_enable(wndw, asyw);
2173 drm_atomic_state_get(state);
2176 * Grab another RPM ref for the commit tail, which will release the
2177 * ref when it's finished
2179 pm_runtime_get_noresume(dev->dev);
2182 queue_work(system_unbound_wq, &state->commit_work);
2184 nv50_disp_atomic_commit_tail(state);
2188 drm_atomic_helper_cleanup_planes(dev, state);
2190 pm_runtime_put_autosuspend(dev->dev);
2194 static struct nv50_outp_atom *
2195 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
2197 struct nv50_outp_atom *outp;
2199 list_for_each_entry(outp, &atom->outp, head) {
2200 if (outp->encoder == encoder)
2204 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2206 return ERR_PTR(-ENOMEM);
2208 list_add(&outp->head, &atom->outp);
2209 outp->encoder = encoder;
2214 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2215 struct drm_connector_state *old_connector_state)
2217 struct drm_encoder *encoder = old_connector_state->best_encoder;
2218 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2219 struct drm_crtc *crtc;
2220 struct nv50_outp_atom *outp;
2222 if (!(crtc = old_connector_state->crtc))
2225 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
2226 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2227 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2228 outp = nv50_disp_outp_atomic_add(atom, encoder);
2230 return PTR_ERR(outp);
2232 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
2233 outp->flush_disable = true;
2234 atom->flush_disable = true;
2236 outp->clr.ctrl = true;
2237 atom->lock_core = true;
2244 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
2245 struct drm_connector_state *connector_state)
2247 struct drm_encoder *encoder = connector_state->best_encoder;
2248 struct drm_crtc_state *new_crtc_state;
2249 struct drm_crtc *crtc;
2250 struct nv50_outp_atom *outp;
2252 if (!(crtc = connector_state->crtc))
2255 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2256 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2257 outp = nv50_disp_outp_atomic_add(atom, encoder);
2259 return PTR_ERR(outp);
2261 outp->set.ctrl = true;
2262 atom->lock_core = true;
2269 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
2271 struct nv50_atom *atom = nv50_atom(state);
2272 struct nv50_core *core = nv50_disp(dev)->core;
2273 struct drm_connector_state *old_connector_state, *new_connector_state;
2274 struct drm_connector *connector;
2275 struct drm_crtc_state *new_crtc_state;
2276 struct drm_crtc *crtc;
2277 struct nv50_head *head;
2278 struct nv50_head_atom *asyh;
2281 if (core->assign_windows && core->func->head->static_wndw_map) {
2282 drm_for_each_crtc(crtc, dev) {
2283 new_crtc_state = drm_atomic_get_crtc_state(state,
2285 if (IS_ERR(new_crtc_state))
2286 return PTR_ERR(new_crtc_state);
2288 head = nv50_head(crtc);
2289 asyh = nv50_head_atom(new_crtc_state);
2290 core->func->head->static_wndw_map(head, asyh);
2294 /* We need to handle colour management on a per-plane basis. */
2295 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2296 if (new_crtc_state->color_mgmt_changed) {
2297 ret = drm_atomic_add_affected_planes(state, crtc);
2303 ret = drm_atomic_helper_check(dev, state);
2307 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
2308 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2312 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2317 ret = drm_dp_mst_atomic_check(state);
2325 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2327 struct nv50_atom *atom = nv50_atom(state);
2328 struct nv50_outp_atom *outp, *outt;
2330 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2331 list_del(&outp->head);
2335 drm_atomic_state_default_clear(state);
2339 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2341 struct nv50_atom *atom = nv50_atom(state);
2342 drm_atomic_state_default_release(&atom->state);
2346 static struct drm_atomic_state *
2347 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2349 struct nv50_atom *atom;
2350 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2351 drm_atomic_state_init(dev, &atom->state) < 0) {
2355 INIT_LIST_HEAD(&atom->outp);
2356 return &atom->state;
2359 static const struct drm_mode_config_funcs
2361 .fb_create = nouveau_user_framebuffer_create,
2362 .output_poll_changed = nouveau_fbcon_output_poll_changed,
2363 .atomic_check = nv50_disp_atomic_check,
2364 .atomic_commit = nv50_disp_atomic_commit,
2365 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2366 .atomic_state_clear = nv50_disp_atomic_state_clear,
2367 .atomic_state_free = nv50_disp_atomic_state_free,
2370 /******************************************************************************
2372 *****************************************************************************/
2375 nv50_display_fini(struct drm_device *dev, bool suspend)
2377 struct nouveau_encoder *nv_encoder;
2378 struct drm_encoder *encoder;
2379 struct drm_plane *plane;
2381 drm_for_each_plane(plane, dev) {
2382 struct nv50_wndw *wndw = nv50_wndw(plane);
2383 if (plane->funcs != &nv50_wndw)
2385 nv50_wndw_fini(wndw);
2388 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2389 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2390 nv_encoder = nouveau_encoder(encoder);
2391 nv50_mstm_fini(nv_encoder->dp.mstm);
2397 nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2399 struct nv50_core *core = nv50_disp(dev)->core;
2400 struct drm_encoder *encoder;
2401 struct drm_plane *plane;
2403 if (resume || runtime)
2404 core->func->init(core);
2406 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2407 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2408 struct nouveau_encoder *nv_encoder =
2409 nouveau_encoder(encoder);
2410 nv50_mstm_init(nv_encoder->dp.mstm, runtime);
2414 drm_for_each_plane(plane, dev) {
2415 struct nv50_wndw *wndw = nv50_wndw(plane);
2416 if (plane->funcs != &nv50_wndw)
2418 nv50_wndw_init(wndw);
2425 nv50_display_destroy(struct drm_device *dev)
2427 struct nv50_disp *disp = nv50_disp(dev);
2429 nv50_audio_component_fini(nouveau_drm(dev));
2431 nvif_object_unmap(&disp->caps);
2432 nvif_object_fini(&disp->caps);
2433 nv50_core_del(&disp->core);
2435 nouveau_bo_unmap(disp->sync);
2437 nouveau_bo_unpin(disp->sync);
2438 nouveau_bo_ref(NULL, &disp->sync);
2440 nouveau_display(dev)->priv = NULL;
2445 nv50_display_create(struct drm_device *dev)
2447 struct nvif_device *device = &nouveau_drm(dev)->client.device;
2448 struct nouveau_drm *drm = nouveau_drm(dev);
2449 struct dcb_table *dcb = &drm->vbios.dcb;
2450 struct drm_connector *connector, *tmp;
2451 struct nv50_disp *disp;
2452 struct dcb_output *dcbe;
2454 bool has_mst = nv50_has_mst(drm);
2456 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2460 mutex_init(&disp->mutex);
2462 nouveau_display(dev)->priv = disp;
2463 nouveau_display(dev)->dtor = nv50_display_destroy;
2464 nouveau_display(dev)->init = nv50_display_init;
2465 nouveau_display(dev)->fini = nv50_display_fini;
2466 disp->disp = &nouveau_display(dev)->disp;
2467 dev->mode_config.funcs = &nv50_disp_func;
2468 dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2469 dev->mode_config.normalize_zpos = true;
2471 /* small shared memory area we use for notifiers and semaphores */
2472 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2473 0, 0x0000, NULL, NULL, &disp->sync);
2475 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
2477 ret = nouveau_bo_map(disp->sync);
2479 nouveau_bo_unpin(disp->sync);
2482 nouveau_bo_ref(NULL, &disp->sync);
2488 /* allocate master evo channel */
2489 ret = nv50_core_new(drm, &disp->core);
2493 disp->core->func->init(disp->core);
2494 if (disp->core->func->caps_init) {
2495 ret = disp->core->func->caps_init(drm, disp);
2500 /* Assign the correct format modifiers */
2501 if (disp->disp->object.oclass >= TU102_DISP)
2502 nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
2504 if (disp->disp->object.oclass >= GF110_DISP)
2505 nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
2507 nouveau_display(dev)->format_modifiers = disp50xx_modifiers;
2509 /* create crtc objects to represent the hw heads */
2510 if (disp->disp->object.oclass >= GV100_DISP)
2511 crtcs = nvif_rd32(&device->object, 0x610060) & 0xff;
2513 if (disp->disp->object.oclass >= GF110_DISP)
2514 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
2518 for (i = 0; i < fls(crtcs); i++) {
2519 struct nv50_head *head;
2521 if (!(crtcs & (1 << i)))
2524 head = nv50_head_create(dev, i);
2526 ret = PTR_ERR(head);
2531 head->msto = nv50_msto_new(dev, head, i);
2532 if (IS_ERR(head->msto)) {
2533 ret = PTR_ERR(head->msto);
2539 * FIXME: This is a hack to workaround the following
2542 * https://gitlab.gnome.org/GNOME/mutter/issues/759
2543 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
2545 * Once these issues are closed, this should be
2548 head->msto->encoder.possible_crtcs = crtcs;
2552 /* create encoder/connector objects based on VBIOS DCB table */
2553 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2554 connector = nouveau_connector_create(dev, dcbe);
2555 if (IS_ERR(connector))
2558 if (dcbe->location == DCB_LOC_ON_CHIP) {
2559 switch (dcbe->type) {
2560 case DCB_OUTPUT_TMDS:
2561 case DCB_OUTPUT_LVDS:
2563 ret = nv50_sor_create(connector, dcbe);
2565 case DCB_OUTPUT_ANALOG:
2566 ret = nv50_dac_create(connector, dcbe);
2573 ret = nv50_pior_create(connector, dcbe);
2577 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2578 dcbe->location, dcbe->type,
2579 ffs(dcbe->or) - 1, ret);
2584 /* cull any connectors we created that don't have an encoder */
2585 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2586 if (connector->possible_encoders)
2589 NV_WARN(drm, "%s has no encoders, removing\n",
2591 connector->funcs->destroy(connector);
2594 /* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
2595 dev->vblank_disable_immediate = true;
2597 nv50_audio_component_init(drm);
2601 nv50_display_destroy(dev);
2605 /******************************************************************************
2607 *****************************************************************************/
2609 /****************************************************************
2610 * Log2(block height) ----------------------------+ *
2611 * Page Kind ----------------------------------+ | *
2612 * Gob Height/Page Kind Generation ------+ | | *
2613 * Sector layout -------+ | | | *
2614 * Compression ------+ | | | | */
2615 const u64 disp50xx_modifiers[] = { /* | | | | | */
2616 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
2617 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
2618 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
2619 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
2620 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
2621 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
2622 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
2623 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
2624 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
2625 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
2626 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
2627 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
2628 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
2629 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
2630 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
2631 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
2632 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
2633 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
2634 DRM_FORMAT_MOD_LINEAR,
2635 DRM_FORMAT_MOD_INVALID
2638 /****************************************************************
2639 * Log2(block height) ----------------------------+ *
2640 * Page Kind ----------------------------------+ | *
2641 * Gob Height/Page Kind Generation ------+ | | *
2642 * Sector layout -------+ | | | *
2643 * Compression ------+ | | | | */
2644 const u64 disp90xx_modifiers[] = { /* | | | | | */
2645 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
2646 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
2647 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
2648 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
2649 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
2650 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
2651 DRM_FORMAT_MOD_LINEAR,
2652 DRM_FORMAT_MOD_INVALID