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>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_crtc_helper.h>
36 #include <drm/drm_dp_helper.h>
37 #include <drm/drm_fb_helper.h>
38 #include <drm/drm_plane_helper.h>
39 #include <drm/drm_edid.h>
41 #include <nvif/class.h>
42 #include <nvif/cl0002.h>
43 #include <nvif/cl5070.h>
44 #include <nvif/cl507d.h>
45 #include <nvif/event.h>
47 #include "nouveau_drv.h"
48 #include "nouveau_dma.h"
49 #include "nouveau_gem.h"
50 #include "nouveau_connector.h"
51 #include "nouveau_encoder.h"
52 #include "nouveau_fence.h"
53 #include "nouveau_fbcon.h"
55 #include <subdev/bios/dp.h>
57 /******************************************************************************
59 *****************************************************************************/
61 struct nv50_outp_atom {
62 struct list_head head;
64 struct drm_encoder *encoder;
67 union nv50_outp_atom_mask {
75 /******************************************************************************
77 *****************************************************************************/
80 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
81 const s32 *oclass, u8 head, void *data, u32 size,
82 struct nv50_chan *chan)
84 struct nvif_sclass *sclass;
87 chan->device = device;
89 ret = n = nvif_object_sclass_get(disp, &sclass);
94 for (i = 0; i < n; i++) {
95 if (sclass[i].oclass == oclass[0]) {
96 ret = nvif_object_init(disp, 0, oclass[0],
97 data, size, &chan->user);
99 nvif_object_map(&chan->user, NULL, 0);
100 nvif_object_sclass_put(&sclass);
107 nvif_object_sclass_put(&sclass);
112 nv50_chan_destroy(struct nv50_chan *chan)
114 nvif_object_fini(&chan->user);
117 /******************************************************************************
119 *****************************************************************************/
122 nv50_dmac_destroy(struct nv50_dmac *dmac)
124 nvif_object_fini(&dmac->vram);
125 nvif_object_fini(&dmac->sync);
127 nv50_chan_destroy(&dmac->base);
129 nvif_mem_fini(&dmac->push);
133 nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
134 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
135 struct nv50_dmac *dmac)
137 struct nouveau_cli *cli = (void *)device->object.client;
138 struct nv50_disp_core_channel_dma_v0 *args = data;
141 mutex_init(&dmac->lock);
143 ret = nvif_mem_init_map(&cli->mmu, NVIF_MEM_COHERENT, 0x1000,
148 dmac->ptr = dmac->push.object.map.ptr;
150 args->pushbuf = nvif_handle(&dmac->push.object);
152 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_wait(struct nv50_dmac *evoc, int nr)
188 struct nv50_dmac *dmac = evoc;
189 struct nvif_device *device = dmac->base.device;
190 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
192 mutex_lock(&dmac->lock);
193 if (put + nr >= (PAGE_SIZE / 4) - 8) {
194 dmac->ptr[put] = 0x20000000;
196 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
197 if (nvif_msec(device, 2000,
198 if (!nvif_rd32(&dmac->base.user, 0x0004))
201 mutex_unlock(&dmac->lock);
202 pr_err("nouveau: evo channel stalled\n");
209 return dmac->ptr + put;
213 evo_kick(u32 *push, struct nv50_dmac *evoc)
215 struct nv50_dmac *dmac = evoc;
216 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
217 mutex_unlock(&dmac->lock);
220 /******************************************************************************
221 * Output path helpers
222 *****************************************************************************/
224 nv50_outp_release(struct nouveau_encoder *nv_encoder)
226 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
228 struct nv50_disp_mthd_v1 base;
231 .base.method = NV50_DISP_MTHD_V1_RELEASE,
232 .base.hasht = nv_encoder->dcb->hasht,
233 .base.hashm = nv_encoder->dcb->hashm,
236 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
238 nv_encoder->link = 0;
242 nv50_outp_acquire(struct nouveau_encoder *nv_encoder)
244 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
245 struct nv50_disp *disp = nv50_disp(drm->dev);
247 struct nv50_disp_mthd_v1 base;
248 struct nv50_disp_acquire_v0 info;
251 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
252 .base.hasht = nv_encoder->dcb->hasht,
253 .base.hashm = nv_encoder->dcb->hashm,
257 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
259 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
263 nv_encoder->or = args.info.or;
264 nv_encoder->link = args.info.link;
269 nv50_outp_atomic_check_view(struct drm_encoder *encoder,
270 struct drm_crtc_state *crtc_state,
271 struct drm_connector_state *conn_state,
272 struct drm_display_mode *native_mode)
274 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
275 struct drm_display_mode *mode = &crtc_state->mode;
276 struct drm_connector *connector = conn_state->connector;
277 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
278 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
280 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
281 asyc->scaler.full = false;
285 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
286 switch (connector->connector_type) {
287 case DRM_MODE_CONNECTOR_LVDS:
288 case DRM_MODE_CONNECTOR_eDP:
289 /* Force use of scaler for non-EDID modes. */
290 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
293 asyc->scaler.full = true;
302 if (!drm_mode_equal(adjusted_mode, mode)) {
303 drm_mode_copy(adjusted_mode, mode);
304 crtc_state->mode_changed = true;
311 nv50_outp_atomic_check(struct drm_encoder *encoder,
312 struct drm_crtc_state *crtc_state,
313 struct drm_connector_state *conn_state)
315 struct nouveau_connector *nv_connector =
316 nouveau_connector(conn_state->connector);
317 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
318 nv_connector->native_mode);
321 /******************************************************************************
323 *****************************************************************************/
325 nv50_dac_disable(struct drm_encoder *encoder)
327 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
328 struct nv50_core *core = nv50_disp(encoder->dev)->core;
329 if (nv_encoder->crtc)
330 core->func->dac->ctrl(core, nv_encoder->or, 0x00000000, NULL);
331 nv_encoder->crtc = NULL;
332 nv50_outp_release(nv_encoder);
336 nv50_dac_enable(struct drm_encoder *encoder)
338 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
339 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
340 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
341 struct nv50_core *core = nv50_disp(encoder->dev)->core;
343 nv50_outp_acquire(nv_encoder);
345 core->func->dac->ctrl(core, nv_encoder->or, 1 << nv_crtc->index, asyh);
348 nv_encoder->crtc = encoder->crtc;
351 static enum drm_connector_status
352 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
354 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
355 struct nv50_disp *disp = nv50_disp(encoder->dev);
357 struct nv50_disp_mthd_v1 base;
358 struct nv50_disp_dac_load_v0 load;
361 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
362 .base.hasht = nv_encoder->dcb->hasht,
363 .base.hashm = nv_encoder->dcb->hashm,
367 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
368 if (args.load.data == 0)
369 args.load.data = 340;
371 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
372 if (ret || !args.load.load)
373 return connector_status_disconnected;
375 return connector_status_connected;
378 static const struct drm_encoder_helper_funcs
380 .atomic_check = nv50_outp_atomic_check,
381 .enable = nv50_dac_enable,
382 .disable = nv50_dac_disable,
383 .detect = nv50_dac_detect
387 nv50_dac_destroy(struct drm_encoder *encoder)
389 drm_encoder_cleanup(encoder);
393 static const struct drm_encoder_funcs
395 .destroy = nv50_dac_destroy,
399 nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
401 struct nouveau_drm *drm = nouveau_drm(connector->dev);
402 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
403 struct nvkm_i2c_bus *bus;
404 struct nouveau_encoder *nv_encoder;
405 struct drm_encoder *encoder;
406 int type = DRM_MODE_ENCODER_DAC;
408 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
411 nv_encoder->dcb = dcbe;
413 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
415 nv_encoder->i2c = &bus->i2c;
417 encoder = to_drm_encoder(nv_encoder);
418 encoder->possible_crtcs = dcbe->heads;
419 encoder->possible_clones = 0;
420 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
421 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
422 drm_encoder_helper_add(encoder, &nv50_dac_help);
424 drm_mode_connector_attach_encoder(connector, encoder);
428 /******************************************************************************
430 *****************************************************************************/
432 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
434 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
435 struct nv50_disp *disp = nv50_disp(encoder->dev);
437 struct nv50_disp_mthd_v1 base;
438 struct nv50_disp_sor_hda_eld_v0 eld;
441 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
442 .base.hasht = nv_encoder->dcb->hasht,
443 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
444 (0x0100 << nv_crtc->index),
447 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
451 nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
453 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
454 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
455 struct nouveau_connector *nv_connector;
456 struct nv50_disp *disp = nv50_disp(encoder->dev);
459 struct nv50_disp_mthd_v1 mthd;
460 struct nv50_disp_sor_hda_eld_v0 eld;
462 u8 data[sizeof(nv_connector->base.eld)];
464 .base.mthd.version = 1,
465 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
466 .base.mthd.hasht = nv_encoder->dcb->hasht,
467 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
468 (0x0100 << nv_crtc->index),
471 nv_connector = nouveau_encoder_connector_get(nv_encoder);
472 if (!drm_detect_monitor_audio(nv_connector->edid))
475 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
477 nvif_mthd(&disp->disp->object, 0, &args,
478 sizeof(args.base) + drm_eld_size(args.data));
481 /******************************************************************************
483 *****************************************************************************/
485 nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
487 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
488 struct nv50_disp *disp = nv50_disp(encoder->dev);
490 struct nv50_disp_mthd_v1 base;
491 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
494 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
495 .base.hasht = nv_encoder->dcb->hasht,
496 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
497 (0x0100 << nv_crtc->index),
500 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
504 nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
506 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
507 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
508 struct nv50_disp *disp = nv50_disp(encoder->dev);
510 struct nv50_disp_mthd_v1 base;
511 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
512 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
515 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
516 .base.hasht = nv_encoder->dcb->hasht,
517 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
518 (0x0100 << nv_crtc->index),
520 .pwr.rekey = 56, /* binary driver, and tegra, constant */
522 struct nouveau_connector *nv_connector;
524 union hdmi_infoframe avi_frame;
525 union hdmi_infoframe vendor_frame;
529 nv_connector = nouveau_encoder_connector_get(nv_encoder);
530 if (!drm_detect_hdmi_monitor(nv_connector->edid))
533 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi, mode,
536 /* We have an AVI InfoFrame, populate it to the display */
537 args.pwr.avi_infoframe_length
538 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
541 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
542 &nv_connector->base, mode);
544 /* We have a Vendor InfoFrame, populate it to the display */
545 args.pwr.vendor_infoframe_length
546 = hdmi_infoframe_pack(&vendor_frame,
548 + args.pwr.avi_infoframe_length,
552 max_ac_packet = mode->htotal - mode->hdisplay;
553 max_ac_packet -= args.pwr.rekey;
554 max_ac_packet -= 18; /* constant from tegra */
555 args.pwr.max_ac_packet = max_ac_packet / 32;
557 size = sizeof(args.base)
559 + args.pwr.avi_infoframe_length
560 + args.pwr.vendor_infoframe_length;
561 nvif_mthd(&disp->disp->object, 0, &args, size);
562 nv50_audio_enable(encoder, mode);
565 /******************************************************************************
567 *****************************************************************************/
568 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
569 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
570 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
573 struct nouveau_encoder *outp;
575 struct drm_dp_mst_topology_mgr mgr;
576 struct nv50_msto *msto[4];
584 struct nv50_mstm *mstm;
585 struct drm_dp_mst_port *port;
586 struct drm_connector connector;
588 struct drm_display_mode *native;
595 struct drm_encoder encoder;
597 struct nv50_head *head;
598 struct nv50_mstc *mstc;
602 static struct drm_dp_payload *
603 nv50_msto_payload(struct nv50_msto *msto)
605 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
606 struct nv50_mstc *mstc = msto->mstc;
607 struct nv50_mstm *mstm = mstc->mstm;
608 int vcpi = mstc->port->vcpi.vcpi, i;
610 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
611 for (i = 0; i < mstm->mgr.max_payloads; i++) {
612 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
613 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
614 mstm->outp->base.base.name, i, payload->vcpi,
615 payload->start_slot, payload->num_slots);
618 for (i = 0; i < mstm->mgr.max_payloads; i++) {
619 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
620 if (payload->vcpi == vcpi)
628 nv50_msto_cleanup(struct nv50_msto *msto)
630 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
631 struct nv50_mstc *mstc = msto->mstc;
632 struct nv50_mstm *mstm = mstc->mstm;
634 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
635 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
636 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
637 if (msto->disabled) {
640 msto->disabled = false;
645 nv50_msto_prepare(struct nv50_msto *msto)
647 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
648 struct nv50_mstc *mstc = msto->mstc;
649 struct nv50_mstm *mstm = mstc->mstm;
651 struct nv50_disp_mthd_v1 base;
652 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
655 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
656 .base.hasht = mstm->outp->dcb->hasht,
657 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
658 (0x0100 << msto->head->base.index),
661 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
662 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
663 struct drm_dp_payload *payload = nv50_msto_payload(msto);
665 args.vcpi.start_slot = payload->start_slot;
666 args.vcpi.num_slots = payload->num_slots;
667 args.vcpi.pbn = mstc->port->vcpi.pbn;
668 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
672 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
673 msto->encoder.name, msto->head->base.base.name,
674 args.vcpi.start_slot, args.vcpi.num_slots,
675 args.vcpi.pbn, args.vcpi.aligned_pbn);
676 nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
680 nv50_msto_atomic_check(struct drm_encoder *encoder,
681 struct drm_crtc_state *crtc_state,
682 struct drm_connector_state *conn_state)
684 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
685 struct nv50_mstm *mstm = mstc->mstm;
686 int bpp = conn_state->connector->display_info.bpc * 3;
689 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
691 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
695 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
700 nv50_msto_enable(struct drm_encoder *encoder)
702 struct nv50_head *head = nv50_head(encoder->crtc);
703 struct nv50_msto *msto = nv50_msto(encoder);
704 struct nv50_mstc *mstc = NULL;
705 struct nv50_mstm *mstm = NULL;
706 struct drm_connector *connector;
707 struct drm_connector_list_iter conn_iter;
712 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
713 drm_for_each_connector_iter(connector, &conn_iter) {
714 if (connector->state->best_encoder == &msto->encoder) {
715 mstc = nv50_mstc(connector);
720 drm_connector_list_iter_end(&conn_iter);
725 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
726 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
730 nv50_outp_acquire(mstm->outp);
732 if (mstm->outp->link & 1)
737 switch (mstc->connector.display_info.bpc) {
738 case 6: depth = 0x2; break;
739 case 8: depth = 0x5; break;
741 default: depth = 0x6; break;
744 mstm->outp->update(mstm->outp, head->base.index,
745 nv50_head_atom(head->base.base.state), proto, depth);
749 mstm->modified = true;
753 nv50_msto_disable(struct drm_encoder *encoder)
755 struct nv50_msto *msto = nv50_msto(encoder);
756 struct nv50_mstc *mstc = msto->mstc;
757 struct nv50_mstm *mstm = mstc->mstm;
760 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
762 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
763 mstm->modified = true;
765 mstm->disabled = true;
766 msto->disabled = true;
769 static const struct drm_encoder_helper_funcs
771 .disable = nv50_msto_disable,
772 .enable = nv50_msto_enable,
773 .atomic_check = nv50_msto_atomic_check,
777 nv50_msto_destroy(struct drm_encoder *encoder)
779 struct nv50_msto *msto = nv50_msto(encoder);
780 drm_encoder_cleanup(&msto->encoder);
784 static const struct drm_encoder_funcs
786 .destroy = nv50_msto_destroy,
790 nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
791 struct nv50_msto **pmsto)
793 struct nv50_msto *msto;
796 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
799 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
800 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
807 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
808 msto->encoder.possible_crtcs = heads;
812 static struct drm_encoder *
813 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
814 struct drm_connector_state *connector_state)
816 struct nv50_head *head = nv50_head(connector_state->crtc);
817 struct nv50_mstc *mstc = nv50_mstc(connector);
819 struct nv50_mstm *mstm = mstc->mstm;
820 return &mstm->msto[head->base.index]->encoder;
825 static struct drm_encoder *
826 nv50_mstc_best_encoder(struct drm_connector *connector)
828 struct nv50_mstc *mstc = nv50_mstc(connector);
830 struct nv50_mstm *mstm = mstc->mstm;
831 return &mstm->msto[0]->encoder;
836 static enum drm_mode_status
837 nv50_mstc_mode_valid(struct drm_connector *connector,
838 struct drm_display_mode *mode)
844 nv50_mstc_get_modes(struct drm_connector *connector)
846 struct nv50_mstc *mstc = nv50_mstc(connector);
849 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
850 drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
852 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
854 if (!mstc->connector.display_info.bpc)
855 mstc->connector.display_info.bpc = 8;
858 drm_mode_destroy(mstc->connector.dev, mstc->native);
859 mstc->native = nouveau_conn_native_mode(&mstc->connector);
863 static const struct drm_connector_helper_funcs
865 .get_modes = nv50_mstc_get_modes,
866 .mode_valid = nv50_mstc_mode_valid,
867 .best_encoder = nv50_mstc_best_encoder,
868 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
871 static enum drm_connector_status
872 nv50_mstc_detect(struct drm_connector *connector, bool force)
874 struct nv50_mstc *mstc = nv50_mstc(connector);
876 return connector_status_disconnected;
877 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
881 nv50_mstc_destroy(struct drm_connector *connector)
883 struct nv50_mstc *mstc = nv50_mstc(connector);
884 drm_connector_cleanup(&mstc->connector);
888 static const struct drm_connector_funcs
890 .reset = nouveau_conn_reset,
891 .detect = nv50_mstc_detect,
892 .fill_modes = drm_helper_probe_single_connector_modes,
893 .destroy = nv50_mstc_destroy,
894 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
895 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
896 .atomic_set_property = nouveau_conn_atomic_set_property,
897 .atomic_get_property = nouveau_conn_atomic_get_property,
901 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
902 const char *path, struct nv50_mstc **pmstc)
904 struct drm_device *dev = mstm->outp->base.base.dev;
905 struct nv50_mstc *mstc;
908 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
913 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
914 DRM_MODE_CONNECTOR_DisplayPort);
921 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
923 mstc->connector.funcs->reset(&mstc->connector);
924 nouveau_conn_attach_properties(&mstc->connector);
926 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto[i]; i++)
927 drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
929 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
930 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
931 drm_mode_connector_set_path_property(&mstc->connector, path);
936 nv50_mstm_cleanup(struct nv50_mstm *mstm)
938 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
939 struct drm_encoder *encoder;
942 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
943 ret = drm_dp_check_act_status(&mstm->mgr);
945 ret = drm_dp_update_payload_part2(&mstm->mgr);
947 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
948 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
949 struct nv50_msto *msto = nv50_msto(encoder);
950 struct nv50_mstc *mstc = msto->mstc;
951 if (mstc && mstc->mstm == mstm)
952 nv50_msto_cleanup(msto);
956 mstm->modified = false;
960 nv50_mstm_prepare(struct nv50_mstm *mstm)
962 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
963 struct drm_encoder *encoder;
966 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
967 ret = drm_dp_update_payload_part1(&mstm->mgr);
969 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
970 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
971 struct nv50_msto *msto = nv50_msto(encoder);
972 struct nv50_mstc *mstc = msto->mstc;
973 if (mstc && mstc->mstm == mstm)
974 nv50_msto_prepare(msto);
978 if (mstm->disabled) {
980 nv50_outp_release(mstm->outp);
981 mstm->disabled = false;
986 nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
988 struct nv50_mstm *mstm = nv50_mstm(mgr);
989 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
993 nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
994 struct drm_connector *connector)
996 struct nouveau_drm *drm = nouveau_drm(connector->dev);
997 struct nv50_mstc *mstc = nv50_mstc(connector);
999 drm_connector_unregister(&mstc->connector);
1001 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
1003 drm_modeset_lock(&drm->dev->mode_config.connection_mutex, NULL);
1005 drm_modeset_unlock(&drm->dev->mode_config.connection_mutex);
1007 drm_connector_unreference(&mstc->connector);
1011 nv50_mstm_register_connector(struct drm_connector *connector)
1013 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1015 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
1017 drm_connector_register(connector);
1020 static struct drm_connector *
1021 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1022 struct drm_dp_mst_port *port, const char *path)
1024 struct nv50_mstm *mstm = nv50_mstm(mgr);
1025 struct nv50_mstc *mstc;
1028 ret = nv50_mstc_new(mstm, port, path, &mstc);
1031 mstc->connector.funcs->destroy(&mstc->connector);
1035 return &mstc->connector;
1038 static const struct drm_dp_mst_topology_cbs
1040 .add_connector = nv50_mstm_add_connector,
1041 .register_connector = nv50_mstm_register_connector,
1042 .destroy_connector = nv50_mstm_destroy_connector,
1043 .hotplug = nv50_mstm_hotplug,
1047 nv50_mstm_service(struct nv50_mstm *mstm)
1049 struct drm_dp_aux *aux = mstm ? mstm->mgr.aux : NULL;
1050 bool handled = true;
1058 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1060 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1064 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1068 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
1073 nv50_mstm_remove(struct nv50_mstm *mstm)
1076 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1080 nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
1082 struct nouveau_encoder *outp = mstm->outp;
1084 struct nv50_disp_mthd_v1 base;
1085 struct nv50_disp_sor_dp_mst_link_v0 mst;
1088 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
1089 .base.hasht = outp->dcb->hasht,
1090 .base.hashm = outp->dcb->hashm,
1093 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
1094 struct nvif_object *disp = &drm->display->disp.object;
1098 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
1106 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
1111 return nvif_mthd(disp, 0, &args, sizeof(args));
1115 nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
1122 if (dpcd[0] >= 0x12) {
1123 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
1127 if (!(dpcd[1] & DP_MST_CAP))
1133 ret = nv50_mstm_enable(mstm, dpcd[0], state);
1137 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
1139 return nv50_mstm_enable(mstm, dpcd[0], 0);
1141 return mstm->mgr.mst_state;
1145 nv50_mstm_fini(struct nv50_mstm *mstm)
1147 if (mstm && mstm->mgr.mst_state)
1148 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1152 nv50_mstm_init(struct nv50_mstm *mstm)
1154 if (mstm && mstm->mgr.mst_state)
1155 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
1159 nv50_mstm_del(struct nv50_mstm **pmstm)
1161 struct nv50_mstm *mstm = *pmstm;
1169 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1170 int conn_base_id, struct nv50_mstm **pmstm)
1172 const int max_payloads = hweight8(outp->dcb->heads);
1173 struct drm_device *dev = outp->base.base.dev;
1174 struct nv50_mstm *mstm;
1178 /* This is a workaround for some monitors not functioning
1179 * correctly in MST mode on initial module load. I think
1180 * some bad interaction with the VBIOS may be responsible.
1182 * A good ol' off and on again seems to work here ;)
1184 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
1185 if (ret >= 0 && dpcd >= 0x12)
1186 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1188 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1191 mstm->mgr.cbs = &nv50_mstm;
1193 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1194 max_payloads, conn_base_id);
1198 for (i = 0; i < max_payloads; i++) {
1199 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
1208 /******************************************************************************
1210 *****************************************************************************/
1212 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1213 struct nv50_head_atom *asyh, u8 proto, u8 depth)
1215 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1216 struct nv50_core *core = disp->core;
1219 nv_encoder->ctrl &= ~BIT(head);
1220 if (!(nv_encoder->ctrl & 0x0000000f))
1221 nv_encoder->ctrl = 0;
1223 nv_encoder->ctrl |= proto << 8;
1224 nv_encoder->ctrl |= BIT(head);
1225 asyh->or.depth = depth;
1228 core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
1232 nv50_sor_disable(struct drm_encoder *encoder)
1234 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1235 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1237 nv_encoder->crtc = NULL;
1240 struct nvkm_i2c_aux *aux = nv_encoder->aux;
1244 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
1246 pwr &= ~DP_SET_POWER_MASK;
1247 pwr |= DP_SET_POWER_D3;
1248 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
1252 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
1253 nv50_audio_disable(encoder, nv_crtc);
1254 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
1255 nv50_outp_release(nv_encoder);
1260 nv50_sor_enable(struct drm_encoder *encoder)
1262 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1263 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1264 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1265 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1267 struct nv50_disp_mthd_v1 base;
1268 struct nv50_disp_sor_lvds_script_v0 lvds;
1271 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1272 .base.hasht = nv_encoder->dcb->hasht,
1273 .base.hashm = nv_encoder->dcb->hashm,
1275 struct nv50_disp *disp = nv50_disp(encoder->dev);
1276 struct drm_device *dev = encoder->dev;
1277 struct nouveau_drm *drm = nouveau_drm(dev);
1278 struct nouveau_connector *nv_connector;
1279 struct nvbios *bios = &drm->vbios;
1283 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1284 nv_encoder->crtc = encoder->crtc;
1285 nv50_outp_acquire(nv_encoder);
1287 switch (nv_encoder->dcb->type) {
1288 case DCB_OUTPUT_TMDS:
1289 if (nv_encoder->link & 1) {
1291 /* Only enable dual-link if:
1292 * - Need to (i.e. rate > 165MHz)
1294 * - Not an HDMI monitor, since there's no dual-link
1297 if (mode->clock >= 165000 &&
1298 nv_encoder->dcb->duallink_possible &&
1299 !drm_detect_hdmi_monitor(nv_connector->edid))
1305 nv50_hdmi_enable(&nv_encoder->base.base, mode);
1307 case DCB_OUTPUT_LVDS:
1310 if (bios->fp_no_ddc) {
1311 if (bios->fp.dual_link)
1312 lvds.lvds.script |= 0x0100;
1313 if (bios->fp.if_is_24bit)
1314 lvds.lvds.script |= 0x0200;
1316 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1317 if (((u8 *)nv_connector->edid)[121] == 2)
1318 lvds.lvds.script |= 0x0100;
1320 if (mode->clock >= bios->fp.duallink_transition_clk) {
1321 lvds.lvds.script |= 0x0100;
1324 if (lvds.lvds.script & 0x0100) {
1325 if (bios->fp.strapless_is_24bit & 2)
1326 lvds.lvds.script |= 0x0200;
1328 if (bios->fp.strapless_is_24bit & 1)
1329 lvds.lvds.script |= 0x0200;
1332 if (nv_connector->base.display_info.bpc == 8)
1333 lvds.lvds.script |= 0x0200;
1336 nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
1339 if (nv_connector->base.display_info.bpc == 6)
1342 if (nv_connector->base.display_info.bpc == 8)
1347 if (nv_encoder->link & 1)
1352 nv50_audio_enable(encoder, mode);
1359 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1362 static const struct drm_encoder_helper_funcs
1364 .atomic_check = nv50_outp_atomic_check,
1365 .enable = nv50_sor_enable,
1366 .disable = nv50_sor_disable,
1370 nv50_sor_destroy(struct drm_encoder *encoder)
1372 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1373 nv50_mstm_del(&nv_encoder->dp.mstm);
1374 drm_encoder_cleanup(encoder);
1378 static const struct drm_encoder_funcs
1380 .destroy = nv50_sor_destroy,
1384 nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
1386 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1387 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1388 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
1389 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1390 struct nouveau_encoder *nv_encoder;
1391 struct drm_encoder *encoder;
1392 u8 ver, hdr, cnt, len;
1396 switch (dcbe->type) {
1397 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1398 case DCB_OUTPUT_TMDS:
1401 type = DRM_MODE_ENCODER_TMDS;
1405 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1408 nv_encoder->dcb = dcbe;
1409 nv_encoder->update = nv50_sor_update;
1411 encoder = to_drm_encoder(nv_encoder);
1412 encoder->possible_crtcs = dcbe->heads;
1413 encoder->possible_clones = 0;
1414 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1415 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1416 drm_encoder_helper_add(encoder, &nv50_sor_help);
1418 drm_mode_connector_attach_encoder(connector, encoder);
1420 if (dcbe->type == DCB_OUTPUT_DP) {
1421 struct nv50_disp *disp = nv50_disp(encoder->dev);
1422 struct nvkm_i2c_aux *aux =
1423 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1425 if (disp->disp->object.oclass < GF110_DISP) {
1426 /* HW has no support for address-only
1427 * transactions, so we're required to
1428 * use custom I2C-over-AUX code.
1430 nv_encoder->i2c = &aux->i2c;
1432 nv_encoder->i2c = &nv_connector->aux.ddc;
1434 nv_encoder->aux = aux;
1437 if ((data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len)) &&
1438 ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04)) {
1439 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
1440 nv_connector->base.base.id,
1441 &nv_encoder->dp.mstm);
1446 struct nvkm_i2c_bus *bus =
1447 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1449 nv_encoder->i2c = &bus->i2c;
1455 /******************************************************************************
1457 *****************************************************************************/
1459 nv50_pior_atomic_check(struct drm_encoder *encoder,
1460 struct drm_crtc_state *crtc_state,
1461 struct drm_connector_state *conn_state)
1463 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1466 crtc_state->adjusted_mode.clock *= 2;
1471 nv50_pior_disable(struct drm_encoder *encoder)
1473 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1474 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1475 if (nv_encoder->crtc)
1476 core->func->pior->ctrl(core, nv_encoder->or, 0x00000000, NULL);
1477 nv_encoder->crtc = NULL;
1478 nv50_outp_release(nv_encoder);
1482 nv50_pior_enable(struct drm_encoder *encoder)
1484 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1485 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1486 struct nouveau_connector *nv_connector;
1487 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1488 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1489 u8 owner = 1 << nv_crtc->index;
1492 nv50_outp_acquire(nv_encoder);
1494 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1495 switch (nv_connector->base.display_info.bpc) {
1496 case 10: asyh->or.depth = 0x6; break;
1497 case 8: asyh->or.depth = 0x5; break;
1498 case 6: asyh->or.depth = 0x2; break;
1499 default: asyh->or.depth = 0x0; break;
1502 switch (nv_encoder->dcb->type) {
1503 case DCB_OUTPUT_TMDS:
1512 core->func->pior->ctrl(core, nv_encoder->or, (proto << 8) | owner, asyh);
1513 nv_encoder->crtc = encoder->crtc;
1516 static const struct drm_encoder_helper_funcs
1518 .atomic_check = nv50_pior_atomic_check,
1519 .enable = nv50_pior_enable,
1520 .disable = nv50_pior_disable,
1524 nv50_pior_destroy(struct drm_encoder *encoder)
1526 drm_encoder_cleanup(encoder);
1530 static const struct drm_encoder_funcs
1532 .destroy = nv50_pior_destroy,
1536 nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1538 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1539 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1540 struct nvkm_i2c_bus *bus = NULL;
1541 struct nvkm_i2c_aux *aux = NULL;
1542 struct i2c_adapter *ddc;
1543 struct nouveau_encoder *nv_encoder;
1544 struct drm_encoder *encoder;
1547 switch (dcbe->type) {
1548 case DCB_OUTPUT_TMDS:
1549 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1550 ddc = bus ? &bus->i2c : NULL;
1551 type = DRM_MODE_ENCODER_TMDS;
1554 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
1555 ddc = aux ? &aux->i2c : NULL;
1556 type = DRM_MODE_ENCODER_TMDS;
1562 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1565 nv_encoder->dcb = dcbe;
1566 nv_encoder->i2c = ddc;
1567 nv_encoder->aux = aux;
1569 encoder = to_drm_encoder(nv_encoder);
1570 encoder->possible_crtcs = dcbe->heads;
1571 encoder->possible_clones = 0;
1572 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1573 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
1574 drm_encoder_helper_add(encoder, &nv50_pior_help);
1576 drm_mode_connector_attach_encoder(connector, encoder);
1580 /******************************************************************************
1582 *****************************************************************************/
1585 nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 *interlock)
1587 struct nv50_disp *disp = nv50_disp(drm->dev);
1588 struct nv50_core *core = disp->core;
1589 struct nv50_mstm *mstm;
1590 struct drm_encoder *encoder;
1592 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
1594 drm_for_each_encoder(encoder, drm->dev) {
1595 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1596 mstm = nouveau_encoder(encoder)->dp.mstm;
1597 if (mstm && mstm->modified)
1598 nv50_mstm_prepare(mstm);
1602 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1603 core->func->update(core, interlock, true);
1604 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1605 disp->core->chan.base.device))
1606 NV_ERROR(drm, "core notifier timeout\n");
1608 drm_for_each_encoder(encoder, drm->dev) {
1609 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1610 mstm = nouveau_encoder(encoder)->dp.mstm;
1611 if (mstm && mstm->modified)
1612 nv50_mstm_cleanup(mstm);
1618 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1620 struct drm_device *dev = state->dev;
1621 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
1622 struct drm_crtc *crtc;
1623 struct drm_plane_state *new_plane_state;
1624 struct drm_plane *plane;
1625 struct nouveau_drm *drm = nouveau_drm(dev);
1626 struct nv50_disp *disp = nv50_disp(dev);
1627 struct nv50_atom *atom = nv50_atom(state);
1628 struct nv50_outp_atom *outp, *outt;
1629 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
1632 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
1633 drm_atomic_helper_wait_for_fences(dev, state, false);
1634 drm_atomic_helper_wait_for_dependencies(state);
1635 drm_atomic_helper_update_legacy_modeset_state(dev, state);
1637 if (atom->lock_core)
1638 mutex_lock(&disp->mutex);
1640 /* Disable head(s). */
1641 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1642 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
1643 struct nv50_head *head = nv50_head(crtc);
1645 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
1646 asyh->clr.mask, asyh->set.mask);
1647 if (old_crtc_state->active && !new_crtc_state->active)
1648 drm_crtc_vblank_off(crtc);
1650 if (asyh->clr.mask) {
1651 nv50_head_flush_clr(head, asyh, atom->flush_disable);
1652 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
1656 /* Disable plane(s). */
1657 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1658 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
1659 struct nv50_wndw *wndw = nv50_wndw(plane);
1661 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
1662 asyw->clr.mask, asyw->set.mask);
1663 if (!asyw->clr.mask)
1666 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
1669 /* Disable output path(s). */
1670 list_for_each_entry(outp, &atom->outp, head) {
1671 const struct drm_encoder_helper_funcs *help;
1672 struct drm_encoder *encoder;
1674 encoder = outp->encoder;
1675 help = encoder->helper_private;
1677 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
1678 outp->clr.mask, outp->set.mask);
1680 if (outp->clr.mask) {
1681 help->disable(encoder);
1682 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
1683 if (outp->flush_disable) {
1684 nv50_disp_atomic_commit_core(drm, interlock);
1685 memset(interlock, 0x00, sizeof(interlock));
1690 /* Flush disable. */
1691 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
1692 if (atom->flush_disable) {
1693 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1694 struct nv50_wndw *wndw = nv50_wndw(plane);
1695 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1696 if (wndw->func->update)
1697 wndw->func->update(wndw, interlock);
1701 nv50_disp_atomic_commit_core(drm, interlock);
1702 memset(interlock, 0x00, sizeof(interlock));
1706 /* Update output path(s). */
1707 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
1708 const struct drm_encoder_helper_funcs *help;
1709 struct drm_encoder *encoder;
1711 encoder = outp->encoder;
1712 help = encoder->helper_private;
1714 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
1715 outp->set.mask, outp->clr.mask);
1717 if (outp->set.mask) {
1718 help->enable(encoder);
1719 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
1722 list_del(&outp->head);
1726 /* Update head(s). */
1727 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1728 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
1729 struct nv50_head *head = nv50_head(crtc);
1731 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
1732 asyh->set.mask, asyh->clr.mask);
1734 if (asyh->set.mask) {
1735 nv50_head_flush_set(head, asyh);
1736 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
1739 if (new_crtc_state->active) {
1740 if (!old_crtc_state->active)
1741 drm_crtc_vblank_on(crtc);
1742 if (new_crtc_state->event)
1743 drm_crtc_vblank_get(crtc);
1747 /* Update plane(s). */
1748 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1749 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
1750 struct nv50_wndw *wndw = nv50_wndw(plane);
1752 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
1753 asyw->set.mask, asyw->clr.mask);
1754 if ( !asyw->set.mask &&
1755 (!asyw->clr.mask || atom->flush_disable))
1758 nv50_wndw_flush_set(wndw, interlock, asyw);
1762 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1763 struct nv50_wndw *wndw = nv50_wndw(plane);
1764 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1765 if (wndw->func->update)
1766 wndw->func->update(wndw, interlock);
1770 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
1771 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
1772 !atom->state.legacy_cursor_update)
1773 nv50_disp_atomic_commit_core(drm, interlock);
1775 disp->core->func->update(disp->core, interlock, false);
1778 if (atom->lock_core)
1779 mutex_unlock(&disp->mutex);
1781 /* Wait for HW to signal completion. */
1782 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1783 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
1784 struct nv50_wndw *wndw = nv50_wndw(plane);
1785 int ret = nv50_wndw_wait_armed(wndw, asyw);
1787 NV_ERROR(drm, "%s: timeout\n", plane->name);
1790 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1791 if (new_crtc_state->event) {
1792 unsigned long flags;
1793 /* Get correct count/ts if racing with vblank irq */
1794 if (new_crtc_state->active)
1795 drm_crtc_accurate_vblank_count(crtc);
1796 spin_lock_irqsave(&crtc->dev->event_lock, flags);
1797 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
1798 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
1800 new_crtc_state->event = NULL;
1801 if (new_crtc_state->active)
1802 drm_crtc_vblank_put(crtc);
1806 drm_atomic_helper_commit_hw_done(state);
1807 drm_atomic_helper_cleanup_planes(dev, state);
1808 drm_atomic_helper_commit_cleanup_done(state);
1809 drm_atomic_state_put(state);
1813 nv50_disp_atomic_commit_work(struct work_struct *work)
1815 struct drm_atomic_state *state =
1816 container_of(work, typeof(*state), commit_work);
1817 nv50_disp_atomic_commit_tail(state);
1821 nv50_disp_atomic_commit(struct drm_device *dev,
1822 struct drm_atomic_state *state, bool nonblock)
1824 struct nouveau_drm *drm = nouveau_drm(dev);
1825 struct drm_plane_state *new_plane_state;
1826 struct drm_plane *plane;
1827 struct drm_crtc *crtc;
1828 bool active = false;
1831 ret = pm_runtime_get_sync(dev->dev);
1832 if (ret < 0 && ret != -EACCES)
1835 ret = drm_atomic_helper_setup_commit(state, nonblock);
1839 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
1841 ret = drm_atomic_helper_prepare_planes(dev, state);
1846 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1851 ret = drm_atomic_helper_swap_state(state, true);
1855 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1856 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
1857 struct nv50_wndw *wndw = nv50_wndw(plane);
1859 if (asyw->set.image)
1860 nv50_wndw_ntfy_enable(wndw, asyw);
1863 drm_atomic_state_get(state);
1866 queue_work(system_unbound_wq, &state->commit_work);
1868 nv50_disp_atomic_commit_tail(state);
1870 drm_for_each_crtc(crtc, dev) {
1871 if (crtc->state->enable) {
1872 if (!drm->have_disp_power_ref) {
1873 drm->have_disp_power_ref = true;
1881 if (!active && drm->have_disp_power_ref) {
1882 pm_runtime_put_autosuspend(dev->dev);
1883 drm->have_disp_power_ref = false;
1888 drm_atomic_helper_cleanup_planes(dev, state);
1890 pm_runtime_put_autosuspend(dev->dev);
1894 static struct nv50_outp_atom *
1895 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
1897 struct nv50_outp_atom *outp;
1899 list_for_each_entry(outp, &atom->outp, head) {
1900 if (outp->encoder == encoder)
1904 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
1906 return ERR_PTR(-ENOMEM);
1908 list_add(&outp->head, &atom->outp);
1909 outp->encoder = encoder;
1914 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
1915 struct drm_connector_state *old_connector_state)
1917 struct drm_encoder *encoder = old_connector_state->best_encoder;
1918 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1919 struct drm_crtc *crtc;
1920 struct nv50_outp_atom *outp;
1922 if (!(crtc = old_connector_state->crtc))
1925 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
1926 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1927 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1928 outp = nv50_disp_outp_atomic_add(atom, encoder);
1930 return PTR_ERR(outp);
1932 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1933 outp->flush_disable = true;
1934 atom->flush_disable = true;
1936 outp->clr.ctrl = true;
1937 atom->lock_core = true;
1944 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
1945 struct drm_connector_state *connector_state)
1947 struct drm_encoder *encoder = connector_state->best_encoder;
1948 struct drm_crtc_state *new_crtc_state;
1949 struct drm_crtc *crtc;
1950 struct nv50_outp_atom *outp;
1952 if (!(crtc = connector_state->crtc))
1955 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1956 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1957 outp = nv50_disp_outp_atomic_add(atom, encoder);
1959 return PTR_ERR(outp);
1961 outp->set.ctrl = true;
1962 atom->lock_core = true;
1969 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
1971 struct nv50_atom *atom = nv50_atom(state);
1972 struct drm_connector_state *old_connector_state, *new_connector_state;
1973 struct drm_connector *connector;
1976 ret = drm_atomic_helper_check(dev, state);
1980 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
1981 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
1985 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
1994 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
1996 struct nv50_atom *atom = nv50_atom(state);
1997 struct nv50_outp_atom *outp, *outt;
1999 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2000 list_del(&outp->head);
2004 drm_atomic_state_default_clear(state);
2008 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2010 struct nv50_atom *atom = nv50_atom(state);
2011 drm_atomic_state_default_release(&atom->state);
2015 static struct drm_atomic_state *
2016 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2018 struct nv50_atom *atom;
2019 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2020 drm_atomic_state_init(dev, &atom->state) < 0) {
2024 INIT_LIST_HEAD(&atom->outp);
2025 return &atom->state;
2028 static const struct drm_mode_config_funcs
2030 .fb_create = nouveau_user_framebuffer_create,
2031 .output_poll_changed = drm_fb_helper_output_poll_changed,
2032 .atomic_check = nv50_disp_atomic_check,
2033 .atomic_commit = nv50_disp_atomic_commit,
2034 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2035 .atomic_state_clear = nv50_disp_atomic_state_clear,
2036 .atomic_state_free = nv50_disp_atomic_state_free,
2039 /******************************************************************************
2041 *****************************************************************************/
2044 nv50_display_fini(struct drm_device *dev)
2046 struct nouveau_encoder *nv_encoder;
2047 struct drm_encoder *encoder;
2048 struct drm_plane *plane;
2050 drm_for_each_plane(plane, dev) {
2051 struct nv50_wndw *wndw = nv50_wndw(plane);
2052 if (plane->funcs != &nv50_wndw)
2054 nv50_wndw_fini(wndw);
2057 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2058 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2059 nv_encoder = nouveau_encoder(encoder);
2060 nv50_mstm_fini(nv_encoder->dp.mstm);
2066 nv50_display_init(struct drm_device *dev)
2068 struct nv50_core *core = nv50_disp(dev)->core;
2069 struct drm_encoder *encoder;
2070 struct drm_plane *plane;
2072 core->func->init(core);
2074 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2075 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2076 struct nouveau_encoder *nv_encoder =
2077 nouveau_encoder(encoder);
2078 nv50_mstm_init(nv_encoder->dp.mstm);
2082 drm_for_each_plane(plane, dev) {
2083 struct nv50_wndw *wndw = nv50_wndw(plane);
2084 if (plane->funcs != &nv50_wndw)
2086 nv50_wndw_init(wndw);
2093 nv50_display_destroy(struct drm_device *dev)
2095 struct nv50_disp *disp = nv50_disp(dev);
2097 nv50_core_del(&disp->core);
2099 nouveau_bo_unmap(disp->sync);
2101 nouveau_bo_unpin(disp->sync);
2102 nouveau_bo_ref(NULL, &disp->sync);
2104 nouveau_display(dev)->priv = NULL;
2108 MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
2109 static int nouveau_atomic = 0;
2110 module_param_named(atomic, nouveau_atomic, int, 0400);
2113 nv50_display_create(struct drm_device *dev)
2115 struct nvif_device *device = &nouveau_drm(dev)->client.device;
2116 struct nouveau_drm *drm = nouveau_drm(dev);
2117 struct dcb_table *dcb = &drm->vbios.dcb;
2118 struct drm_connector *connector, *tmp;
2119 struct nv50_disp *disp;
2120 struct dcb_output *dcbe;
2123 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2127 mutex_init(&disp->mutex);
2129 nouveau_display(dev)->priv = disp;
2130 nouveau_display(dev)->dtor = nv50_display_destroy;
2131 nouveau_display(dev)->init = nv50_display_init;
2132 nouveau_display(dev)->fini = nv50_display_fini;
2133 disp->disp = &nouveau_display(dev)->disp;
2134 dev->mode_config.funcs = &nv50_disp_func;
2135 dev->driver->driver_features |= DRIVER_PREFER_XBGR_30BPP;
2137 dev->driver->driver_features |= DRIVER_ATOMIC;
2139 /* small shared memory area we use for notifiers and semaphores */
2140 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2141 0, 0x0000, NULL, NULL, &disp->sync);
2143 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
2145 ret = nouveau_bo_map(disp->sync);
2147 nouveau_bo_unpin(disp->sync);
2150 nouveau_bo_ref(NULL, &disp->sync);
2156 /* allocate master evo channel */
2157 ret = nv50_core_new(drm, &disp->core);
2161 /* create crtc objects to represent the hw heads */
2162 if (disp->disp->object.oclass >= GF110_DISP)
2163 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
2167 for (i = 0; i < fls(crtcs); i++) {
2168 if (!(crtcs & (1 << i)))
2170 ret = nv50_head_create(dev, i);
2175 /* create encoder/connector objects based on VBIOS DCB table */
2176 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2177 connector = nouveau_connector_create(dev, dcbe->connector);
2178 if (IS_ERR(connector))
2181 if (dcbe->location == DCB_LOC_ON_CHIP) {
2182 switch (dcbe->type) {
2183 case DCB_OUTPUT_TMDS:
2184 case DCB_OUTPUT_LVDS:
2186 ret = nv50_sor_create(connector, dcbe);
2188 case DCB_OUTPUT_ANALOG:
2189 ret = nv50_dac_create(connector, dcbe);
2196 ret = nv50_pior_create(connector, dcbe);
2200 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2201 dcbe->location, dcbe->type,
2202 ffs(dcbe->or) - 1, ret);
2207 /* cull any connectors we created that don't have an encoder */
2208 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2209 if (connector->encoder_ids[0])
2212 NV_WARN(drm, "%s has no encoders, removing\n",
2214 connector->funcs->destroy(connector);
2219 nv50_display_destroy(dev);