2 * Copyright 2014 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.
29 #include <drm/display/drm_dp.h>
31 #include <subdev/bios.h>
32 #include <subdev/bios/init.h>
33 #include <subdev/gpio.h>
34 #include <subdev/i2c.h>
36 #include <nvif/event.h>
38 /* IED scripts are no longer used by UEFI/RM from Ampere, but have been updated for
39 * the x86 option ROM. However, the relevant VBIOS table versions weren't modified,
40 * so we're unable to detect this in a nice way.
42 #define AMPERE_IED_HACK(disp) ((disp)->engine.subdev.device->card_type >= GA100)
45 nvkm_dp_mst_id_put(struct nvkm_outp *outp, u32 id)
51 nvkm_dp_mst_id_get(struct nvkm_outp *outp, u32 *pid)
53 *pid = BIT(outp->index);
58 nvkm_dp_aux_xfer(struct nvkm_outp *outp, u8 type, u32 addr, u8 *data, u8 *size)
60 int ret = nvkm_i2c_aux_acquire(outp->dp.aux);
65 ret = nvkm_i2c_aux_xfer(outp->dp.aux, false, type, addr, data, size);
66 nvkm_i2c_aux_release(outp->dp.aux);
71 nvkm_dp_aux_pwr(struct nvkm_outp *outp, bool pu)
73 outp->dp.enabled = pu;
74 nvkm_dp_enable(outp, outp->dp.enabled);
79 struct nvkm_outp *outp;
92 nvkm_dp_train_sense(struct lt_state *lt, bool pc, u32 delay)
94 struct nvkm_outp *outp = lt->outp;
98 usleep_range(delay, delay * 2);
101 addr = DPCD_LTTPR_LANE0_1_STATUS(lt->repeater);
105 ret = nvkm_rdaux(outp->dp.aux, addr, <->stat[0], 3);
110 addr = DPCD_LTTPR_LANE0_1_ADJUST(lt->repeater);
114 ret = nvkm_rdaux(outp->dp.aux, addr, <->stat[4], 2);
119 ret = nvkm_rdaux(outp->dp.aux, DPCD_LS0C, <->pc2stat, 1);
123 OUTP_TRACE(outp, "status %6ph pc2 %02x", lt->stat, lt->pc2stat);
125 OUTP_TRACE(outp, "status %6ph", lt->stat);
132 nvkm_dp_train_drive(struct lt_state *lt, bool pc)
134 struct nvkm_outp *outp = lt->outp;
135 struct nvkm_ior *ior = outp->ior;
136 struct nvkm_bios *bios = ior->disp->engine.subdev.device->bios;
137 struct nvbios_dpout info;
138 struct nvbios_dpcfg ocfg;
139 u8 ver, hdr, cnt, len;
144 for (i = 0; i < ior->dp.nr; i++) {
145 u8 lane = (lt->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
146 u8 lpc2 = (lt->pc2stat >> (i * 2)) & 0x3;
147 u8 lpre = (lane & 0x0c) >> 2;
148 u8 lvsw = (lane & 0x03) >> 0;
154 lpc2 = hipc | DPCD_LC0F_LANE0_MAX_POST_CURSOR2_REACHED;
156 lpre = hipe | DPCD_LC03_MAX_SWING_REACHED; /* yes. */
157 lvsw = hivs = 3 - (lpre & 3);
160 lvsw = hivs | DPCD_LC03_MAX_SWING_REACHED;
163 lt->conf[i] = (lpre << 3) | lvsw;
164 lt->pc2conf[i >> 1] |= lpc2 << ((i & 1) * 4);
166 OUTP_TRACE(outp, "config lane %d %02x %02x", i, lt->conf[i], lpc2);
168 if (lt->repeater != lt->repeaters)
171 data = nvbios_dpout_match(bios, outp->info.hasht, outp->info.hashm,
172 &ver, &hdr, &cnt, &len, &info);
176 data = nvbios_dpcfg_match(bios, data, lpc2 & 3, lvsw & 3, lpre & 3,
177 &ver, &hdr, &cnt, &len, &ocfg);
181 ior->func->dp->drive(ior, i, ocfg.pc, ocfg.dc, ocfg.pe, ocfg.tx_pu);
185 addr = DPCD_LTTPR_LANE0_SET(lt->repeater);
189 ret = nvkm_wraux(outp->dp.aux, addr, lt->conf, 4);
194 ret = nvkm_wraux(outp->dp.aux, DPCD_LC0F, lt->pc2conf, 2);
203 nvkm_dp_train_pattern(struct lt_state *lt, u8 pattern)
205 struct nvkm_outp *outp = lt->outp;
209 OUTP_TRACE(outp, "training pattern %d", pattern);
210 outp->ior->func->dp->pattern(outp->ior, pattern);
213 addr = DPCD_LTTPR_PATTERN_SET(lt->repeater);
217 nvkm_rdaux(outp->dp.aux, addr, &sink_tp, 1);
218 sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
219 sink_tp |= (pattern != 4) ? pattern : 7;
222 sink_tp |= DPCD_LC02_SCRAMBLING_DISABLE;
224 sink_tp &= ~DPCD_LC02_SCRAMBLING_DISABLE;
225 nvkm_wraux(outp->dp.aux, addr, &sink_tp, 1);
229 nvkm_dp_train_eq(struct lt_state *lt)
231 struct nvkm_i2c_aux *aux = lt->outp->dp.aux;
232 bool eq_done = false, cr_done = true;
233 int tries = 0, usec = 0, i;
237 if (!nvkm_rdaux(aux, DPCD_LTTPR_AUX_RD_INTERVAL(lt->repeater), &data, sizeof(data)))
238 usec = (data & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
240 nvkm_dp_train_pattern(lt, 4);
242 if (lt->outp->dp.dpcd[DPCD_RC00_DPCD_REV] >= 0x14 &&
243 lt->outp->dp.dpcd[DPCD_RC03] & DPCD_RC03_TPS4_SUPPORTED)
244 nvkm_dp_train_pattern(lt, 4);
246 if (lt->outp->dp.dpcd[DPCD_RC00_DPCD_REV] >= 0x12 &&
247 lt->outp->dp.dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED)
248 nvkm_dp_train_pattern(lt, 3);
250 nvkm_dp_train_pattern(lt, 2);
252 usec = (lt->outp->dp.dpcd[DPCD_RC0E] & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
257 nvkm_dp_train_drive(lt, lt->pc2)) ||
258 nvkm_dp_train_sense(lt, lt->pc2, usec ? usec : 400))
261 eq_done = !!(lt->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
262 for (i = 0; i < lt->outp->ior->dp.nr && eq_done; i++) {
263 u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
264 if (!(lane & DPCD_LS02_LANE0_CR_DONE))
266 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
267 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
270 } while (!eq_done && cr_done && ++tries <= 5);
272 return eq_done ? 0 : -1;
276 nvkm_dp_train_cr(struct lt_state *lt)
278 bool cr_done = false, abort = false;
279 int voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
280 int tries = 0, usec = 0, i;
282 nvkm_dp_train_pattern(lt, 1);
284 if (lt->outp->dp.dpcd[DPCD_RC00_DPCD_REV] < 0x14 && !lt->repeater)
285 usec = (lt->outp->dp.dpcd[DPCD_RC0E] & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
288 if (nvkm_dp_train_drive(lt, false) ||
289 nvkm_dp_train_sense(lt, false, usec ? usec : 100))
293 for (i = 0; i < lt->outp->ior->dp.nr; i++) {
294 u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
295 if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
297 if (lt->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
303 if ((lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
304 voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
307 } while (!cr_done && !abort && ++tries < 5);
309 return cr_done ? 0 : -1;
313 nvkm_dp_train_link(struct nvkm_outp *outp, int rate)
315 struct nvkm_ior *ior = outp->ior;
316 struct lt_state lt = {
318 .pc2 = outp->dp.dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED,
319 .repeaters = outp->dp.lttprs,
324 OUTP_DBG(outp, "training %dx%02x", ior->dp.nr, ior->dp.bw);
326 /* Set desired link configuration on the sink. */
327 sink[0] = (outp->dp.rate[rate].dpcd < 0) ? ior->dp.bw : 0;
328 sink[1] = ior->dp.nr;
330 sink[1] |= DPCD_LC01_ENHANCED_FRAME_EN;
331 if (outp->dp.lt.post_adj)
334 ret = nvkm_wraux(outp->dp.aux, DPCD_LC00_LINK_BW_SET, sink, 2);
338 if (outp->dp.rate[rate].dpcd >= 0) {
339 ret = nvkm_rdaux(outp->dp.aux, DPCD_LC15_LINK_RATE_SET, &sink[0], sizeof(sink[0]));
343 sink[0] &= ~DPCD_LC15_LINK_RATE_SET_MASK;
344 sink[0] |= outp->dp.rate[rate].dpcd;
346 ret = nvkm_wraux(outp->dp.aux, DPCD_LC15_LINK_RATE_SET, &sink[0], sizeof(sink[0]));
351 /* Attempt to train the link in this configuration. */
352 for (lt.repeater = lt.repeaters; lt.repeater >= 0; lt.repeater--) {
354 OUTP_DBG(outp, "training LTTPR%d", lt.repeater);
356 OUTP_DBG(outp, "training sink");
358 memset(lt.stat, 0x00, sizeof(lt.stat));
359 ret = nvkm_dp_train_cr(<);
361 ret = nvkm_dp_train_eq(<);
362 nvkm_dp_train_pattern(<, 0);
369 nvkm_dp_train_links(struct nvkm_outp *outp, int rate)
371 struct nvkm_ior *ior = outp->ior;
372 struct nvkm_disp *disp = outp->disp;
373 struct nvkm_subdev *subdev = &disp->engine.subdev;
374 struct nvkm_bios *bios = subdev->device->bios;
378 OUTP_DBG(outp, "programming link for %dx%02x", ior->dp.nr, ior->dp.bw);
380 /* Intersect misc. capabilities of the OR and sink. */
381 if (disp->engine.subdev.device->chipset < 0x110)
382 outp->dp.dpcd[DPCD_RC03] &= ~DPCD_RC03_TPS4_SUPPORTED;
383 if (disp->engine.subdev.device->chipset < 0xd0)
384 outp->dp.dpcd[DPCD_RC02] &= ~DPCD_RC02_TPS3_SUPPORTED;
386 if (AMPERE_IED_HACK(disp) && (lnkcmp = outp->dp.info.script[0])) {
387 /* Execute BeforeLinkTraining script from DP Info table. */
388 while (ior->dp.bw < nvbios_rd08(bios, lnkcmp))
390 lnkcmp = nvbios_rd16(bios, lnkcmp + 1);
392 nvbios_init(&outp->disp->engine.subdev, lnkcmp,
393 init.outp = &outp->info;
395 init.link = ior->asy.link;
399 /* Set desired link configuration on the source. */
400 if ((lnkcmp = outp->dp.info.lnkcmp)) {
401 if (outp->dp.version < 0x30) {
402 while ((ior->dp.bw * 2700) < nvbios_rd16(bios, lnkcmp))
404 lnkcmp = nvbios_rd16(bios, lnkcmp + 2);
406 while (ior->dp.bw < nvbios_rd08(bios, lnkcmp))
408 lnkcmp = nvbios_rd16(bios, lnkcmp + 1);
411 nvbios_init(subdev, lnkcmp,
412 init.outp = &outp->info;
414 init.link = ior->asy.link;
418 ret = ior->func->dp->links(ior, outp->dp.aux);
421 OUTP_ERR(outp, "train failed with %d", ret);
427 ior->func->dp->power(ior, ior->dp.nr);
429 /* Attempt to train the link in this configuration. */
430 return nvkm_dp_train_link(outp, rate);
434 nvkm_dp_train_fini(struct nvkm_outp *outp)
436 /* Execute AfterLinkTraining script from DP Info table. */
437 nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[1],
438 init.outp = &outp->info;
439 init.or = outp->ior->id;
440 init.link = outp->ior->asy.link;
445 nvkm_dp_train_init(struct nvkm_outp *outp)
447 /* Execute EnableSpread/DisableSpread script from DP Info table. */
448 if (outp->dp.dpcd[DPCD_RC03] & DPCD_RC03_MAX_DOWNSPREAD) {
449 nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[2],
450 init.outp = &outp->info;
451 init.or = outp->ior->id;
452 init.link = outp->ior->asy.link;
455 nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[3],
456 init.outp = &outp->info;
457 init.or = outp->ior->id;
458 init.link = outp->ior->asy.link;
462 if (!AMPERE_IED_HACK(outp->disp)) {
463 /* Execute BeforeLinkTraining script from DP Info table. */
464 nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[0],
465 init.outp = &outp->info;
466 init.or = outp->ior->id;
467 init.link = outp->ior->asy.link;
473 nvkm_dp_drive(struct nvkm_outp *outp, u8 lanes, u8 pe[4], u8 vs[4])
475 struct lt_state lt = {
477 .stat[4] = (pe[0] << 2) | (vs[0] << 0) |
478 (pe[1] << 6) | (vs[1] << 4),
479 .stat[5] = (pe[2] << 2) | (vs[2] << 0) |
480 (pe[3] << 6) | (vs[3] << 4),
483 return nvkm_dp_train_drive(<, false);
487 nvkm_dp_train(struct nvkm_outp *outp, bool retrain)
489 struct nvkm_ior *ior = outp->ior;
492 for (rate = 0; rate < outp->dp.rates; rate++) {
493 if (outp->dp.rate[rate].rate == (retrain ? ior->dp.bw : outp->dp.lt.bw) * 27000)
497 if (WARN_ON(rate == outp->dp.rates))
500 /* Retraining link? Skip source configuration, it can mess up the active modeset. */
502 mutex_lock(&outp->dp.mutex);
503 ret = nvkm_dp_train_link(outp, rate);
504 mutex_unlock(&outp->dp.mutex);
508 mutex_lock(&outp->dp.mutex);
509 OUTP_DBG(outp, "training");
511 ior->dp.mst = outp->dp.lt.mst;
512 ior->dp.ef = outp->dp.dpcd[DPCD_RC02] & DPCD_RC02_ENHANCED_FRAME_CAP;
513 ior->dp.bw = outp->dp.lt.bw;
514 ior->dp.nr = outp->dp.lt.nr;
516 nvkm_dp_train_init(outp);
517 ret = nvkm_dp_train_links(outp, rate);
518 nvkm_dp_train_fini(outp);
520 OUTP_ERR(outp, "training failed");
522 OUTP_DBG(outp, "training done");
524 mutex_unlock(&outp->dp.mutex);
529 nvkm_dp_disable(struct nvkm_outp *outp, struct nvkm_ior *ior)
531 /* Execute DisableLT script from DP Info Table. */
532 nvbios_init(&ior->disp->engine.subdev, outp->dp.info.script[4],
533 init.outp = &outp->info;
535 init.link = ior->arm.link;
540 nvkm_dp_release(struct nvkm_outp *outp)
542 outp->ior->dp.nr = 0;
543 nvkm_dp_disable(outp, outp->ior);
545 nvkm_outp_release(outp);
549 nvkm_dp_enable(struct nvkm_outp *outp, bool auxpwr)
551 struct nvkm_gpio *gpio = outp->disp->engine.subdev.device->gpio;
552 struct nvkm_i2c_aux *aux = outp->dp.aux;
554 if (auxpwr && !outp->dp.aux_pwr) {
555 /* eDP panels need powering on by us (if the VBIOS doesn't default it
556 * to on) before doing any AUX channel transactions. LVDS panel power
557 * is handled by the SOR itself, and not required for LVDS DDC.
559 if (outp->conn->info.type == DCB_CONNECTOR_eDP) {
560 int power = nvkm_gpio_get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff);
562 nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1);
563 outp->dp.aux_pwr_pu = true;
566 /* We delay here unconditionally, even if already powered,
567 * because some laptop panels having a significant resume
568 * delay before the panel begins responding.
570 * This is likely a bit of a hack, but no better idea for
571 * handling this at the moment.
576 OUTP_DBG(outp, "aux power -> always");
577 nvkm_i2c_aux_monitor(aux, true);
578 outp->dp.aux_pwr = true;
580 if (!auxpwr && outp->dp.aux_pwr) {
581 OUTP_DBG(outp, "aux power -> demand");
582 nvkm_i2c_aux_monitor(aux, false);
583 outp->dp.aux_pwr = false;
585 /* Restore eDP panel GPIO to its prior state if we changed it, as
586 * it could potentially interfere with other outputs.
588 if (outp->conn->info.type == DCB_CONNECTOR_eDP) {
589 if (outp->dp.aux_pwr_pu) {
590 nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 0);
591 outp->dp.aux_pwr_pu = false;
598 nvkm_dp_fini(struct nvkm_outp *outp)
600 nvkm_dp_enable(outp, false);
604 nvkm_dp_init(struct nvkm_outp *outp)
606 nvkm_outp_init(outp);
607 nvkm_dp_enable(outp, outp->dp.enabled);
611 nvkm_dp_dtor(struct nvkm_outp *outp)
616 static const struct nvkm_outp_func
618 .dtor = nvkm_dp_dtor,
619 .init = nvkm_dp_init,
620 .fini = nvkm_dp_fini,
621 .detect = nvkm_outp_detect,
622 .inherit = nvkm_outp_inherit,
623 .acquire = nvkm_outp_acquire,
624 .release = nvkm_dp_release,
625 .bl.get = nvkm_outp_bl_get,
626 .bl.set = nvkm_outp_bl_set,
627 .dp.aux_pwr = nvkm_dp_aux_pwr,
628 .dp.aux_xfer = nvkm_dp_aux_xfer,
629 .dp.train = nvkm_dp_train,
630 .dp.drive = nvkm_dp_drive,
631 .dp.mst_id_get = nvkm_dp_mst_id_get,
632 .dp.mst_id_put = nvkm_dp_mst_id_put,
636 nvkm_dp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE, struct nvkm_outp **poutp)
638 struct nvkm_device *device = disp->engine.subdev.device;
639 struct nvkm_bios *bios = device->bios;
640 struct nvkm_i2c *i2c = device->i2c;
641 struct nvkm_outp *outp;
642 u8 ver, hdr, cnt, len;
646 ret = nvkm_outp_new_(&nvkm_dp_func, disp, index, dcbE, poutp);
651 if (dcbE->location == 0)
652 outp->dp.aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_CCB(dcbE->i2c_index));
654 outp->dp.aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbE->extdev));
656 OUTP_ERR(outp, "no aux");
660 /* bios data is not optional */
661 data = nvbios_dpout_match(bios, outp->info.hasht, outp->info.hashm,
662 &outp->dp.version, &hdr, &cnt, &len, &outp->dp.info);
664 OUTP_ERR(outp, "no bios dp data");
668 OUTP_DBG(outp, "bios dp %02x %02x %02x %02x", outp->dp.version, hdr, cnt, len);
670 data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len);
671 outp->dp.mst = data && ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04);
673 mutex_init(&outp->dp.mutex);