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 <subdev/bios.h>
30 #include <subdev/bios/init.h>
31 #include <subdev/gpio.h>
32 #include <subdev/i2c.h>
34 #include <nvif/event.h>
46 nvkm_dp_train_sense(struct lt_state *lt, bool pc, u32 delay)
48 struct nvkm_dp *dp = lt->dp;
51 if (dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL])
52 mdelay(dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL] * 4);
56 ret = nvkm_rdaux(dp->aux, DPCD_LS02, lt->stat, 6);
61 ret = nvkm_rdaux(dp->aux, DPCD_LS0C, <->pc2stat, 1);
64 OUTP_TRACE(&dp->outp, "status %6ph pc2 %02x",
65 lt->stat, lt->pc2stat);
67 OUTP_TRACE(&dp->outp, "status %6ph", lt->stat);
74 nvkm_dp_train_drive(struct lt_state *lt, bool pc)
76 struct nvkm_dp *dp = lt->dp;
77 struct nvkm_ior *ior = dp->outp.ior;
78 struct nvkm_bios *bios = ior->disp->engine.subdev.device->bios;
79 struct nvbios_dpout info;
80 struct nvbios_dpcfg ocfg;
81 u8 ver, hdr, cnt, len;
85 for (i = 0; i < ior->dp.nr; i++) {
86 u8 lane = (lt->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
87 u8 lpc2 = (lt->pc2stat >> (i * 2)) & 0x3;
88 u8 lpre = (lane & 0x0c) >> 2;
89 u8 lvsw = (lane & 0x03) >> 0;
95 lpc2 = hipc | DPCD_LC0F_LANE0_MAX_POST_CURSOR2_REACHED;
97 lpre = hipe | DPCD_LC03_MAX_SWING_REACHED; /* yes. */
98 lvsw = hivs = 3 - (lpre & 3);
101 lvsw = hivs | DPCD_LC03_MAX_SWING_REACHED;
104 lt->conf[i] = (lpre << 3) | lvsw;
105 lt->pc2conf[i >> 1] |= lpc2 << ((i & 1) * 4);
107 OUTP_TRACE(&dp->outp, "config lane %d %02x %02x",
108 i, lt->conf[i], lpc2);
110 data = nvbios_dpout_match(bios, dp->outp.info.hasht,
112 &ver, &hdr, &cnt, &len, &info);
116 data = nvbios_dpcfg_match(bios, data, lpc2 & 3, lvsw & 3,
117 lpre & 3, &ver, &hdr, &cnt, &len,
122 ior->func->dp.drive(ior, i, ocfg.pc, ocfg.dc,
123 ocfg.pe, ocfg.tx_pu);
126 ret = nvkm_wraux(dp->aux, DPCD_LC03(0), lt->conf, 4);
131 ret = nvkm_wraux(dp->aux, DPCD_LC0F, lt->pc2conf, 2);
140 nvkm_dp_train_pattern(struct lt_state *lt, u8 pattern)
142 struct nvkm_dp *dp = lt->dp;
145 OUTP_TRACE(&dp->outp, "training pattern %d", pattern);
146 dp->outp.ior->func->dp.pattern(dp->outp.ior, pattern);
148 nvkm_rdaux(dp->aux, DPCD_LC02, &sink_tp, 1);
149 sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
151 nvkm_wraux(dp->aux, DPCD_LC02, &sink_tp, 1);
155 nvkm_dp_train_eq(struct lt_state *lt)
157 bool eq_done = false, cr_done = true;
160 if (lt->dp->dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED)
161 nvkm_dp_train_pattern(lt, 3);
163 nvkm_dp_train_pattern(lt, 2);
167 nvkm_dp_train_drive(lt, lt->pc2)) ||
168 nvkm_dp_train_sense(lt, lt->pc2, 400))
171 eq_done = !!(lt->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
172 for (i = 0; i < lt->dp->outp.ior->dp.nr && eq_done; i++) {
173 u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
174 if (!(lane & DPCD_LS02_LANE0_CR_DONE))
176 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
177 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
180 } while (!eq_done && cr_done && ++tries <= 5);
182 return eq_done ? 0 : -1;
186 nvkm_dp_train_cr(struct lt_state *lt)
188 bool cr_done = false, abort = false;
189 int voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
192 nvkm_dp_train_pattern(lt, 1);
195 if (nvkm_dp_train_drive(lt, false) ||
196 nvkm_dp_train_sense(lt, false, 100))
200 for (i = 0; i < lt->dp->outp.ior->dp.nr; i++) {
201 u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
202 if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
204 if (lt->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
210 if ((lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
211 voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
214 } while (!cr_done && !abort && ++tries < 5);
216 return cr_done ? 0 : -1;
220 nvkm_dp_train_links(struct nvkm_dp *dp)
222 struct nvkm_ior *ior = dp->outp.ior;
223 struct nvkm_disp *disp = dp->outp.disp;
224 struct nvkm_subdev *subdev = &disp->engine.subdev;
225 struct nvkm_bios *bios = subdev->device->bios;
226 struct lt_state lt = {
233 OUTP_DBG(&dp->outp, "training %d x %d MB/s",
234 ior->dp.nr, ior->dp.bw * 27);
236 /* Intersect misc. capabilities of the OR and sink. */
237 if (disp->engine.subdev.device->chipset < 0xd0)
238 dp->dpcd[DPCD_RC02] &= ~DPCD_RC02_TPS3_SUPPORTED;
239 lt.pc2 = dp->dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED;
241 /* Set desired link configuration on the source. */
242 if ((lnkcmp = lt.dp->info.lnkcmp)) {
243 if (dp->version < 0x30) {
244 while ((ior->dp.bw * 2700) < nvbios_rd16(bios, lnkcmp))
246 lnkcmp = nvbios_rd16(bios, lnkcmp + 2);
248 while (ior->dp.bw < nvbios_rd08(bios, lnkcmp))
250 lnkcmp = nvbios_rd16(bios, lnkcmp + 1);
253 nvbios_init(subdev, lnkcmp,
254 init.outp = &dp->outp.info;
256 init.link = ior->asy.link;
260 ret = ior->func->dp.links(ior, dp->aux);
263 OUTP_ERR(&dp->outp, "train failed with %d", ret);
269 ior->func->dp.power(ior, ior->dp.nr);
271 /* Set desired link configuration on the sink. */
272 sink[0] = ior->dp.bw;
273 sink[1] = ior->dp.nr;
275 sink[1] |= DPCD_LC01_ENHANCED_FRAME_EN;
277 ret = nvkm_wraux(dp->aux, DPCD_LC00_LINK_BW_SET, sink, 2);
281 /* Attempt to train the link in this configuration. */
282 memset(lt.stat, 0x00, sizeof(lt.stat));
283 ret = nvkm_dp_train_cr(<);
285 ret = nvkm_dp_train_eq(<);
286 nvkm_dp_train_pattern(<, 0);
291 nvkm_dp_train_fini(struct nvkm_dp *dp)
293 /* Execute AfterLinkTraining script from DP Info table. */
294 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[1],
295 init.outp = &dp->outp.info;
296 init.or = dp->outp.ior->id;
297 init.link = dp->outp.ior->asy.link;
302 nvkm_dp_train_init(struct nvkm_dp *dp)
304 /* Execute EnableSpread/DisableSpread script from DP Info table. */
305 if (dp->dpcd[DPCD_RC03] & DPCD_RC03_MAX_DOWNSPREAD) {
306 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[2],
307 init.outp = &dp->outp.info;
308 init.or = dp->outp.ior->id;
309 init.link = dp->outp.ior->asy.link;
312 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[3],
313 init.outp = &dp->outp.info;
314 init.or = dp->outp.ior->id;
315 init.link = dp->outp.ior->asy.link;
319 /* Execute BeforeLinkTraining script from DP Info table. */
320 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[0],
321 init.outp = &dp->outp.info;
322 init.or = dp->outp.ior->id;
323 init.link = dp->outp.ior->asy.link;
327 static const struct dp_rates {
331 } nvkm_dp_rates[] = {
332 { 2160000, 0x14, 4 },
333 { 1080000, 0x0a, 4 },
334 { 1080000, 0x14, 2 },
345 nvkm_dp_train(struct nvkm_dp *dp, u32 dataKBps)
347 struct nvkm_ior *ior = dp->outp.ior;
348 const u8 sink_nr = dp->dpcd[DPCD_RC02] & DPCD_RC02_MAX_LANE_COUNT;
349 const u8 sink_bw = dp->dpcd[DPCD_RC01_MAX_LINK_RATE];
350 const u8 outp_nr = dp->outp.info.dpconf.link_nr;
351 const u8 outp_bw = dp->outp.info.dpconf.link_bw;
352 const struct dp_rates *failsafe = NULL, *cfg;
356 /* Find the lowest configuration of the OR that can support
357 * the required link rate.
359 * We will refuse to program the OR to lower rates, even if
360 * link training fails at higher rates (or even if the sink
361 * can't support the rate at all, though the DD is supposed
362 * to prevent such situations from happening).
364 * Attempting to do so can cause the entire display to hang,
365 * and it's better to have a failed modeset than that.
367 for (cfg = nvkm_dp_rates; cfg->rate; cfg++) {
368 if (cfg->nr <= outp_nr && cfg->nr <= outp_bw)
370 if (failsafe && cfg[1].rate < dataKBps)
374 if (WARN_ON(!failsafe))
377 /* Ensure sink is not in a low-power state. */
378 if (!nvkm_rdaux(dp->aux, DPCD_SC00, &pwr, 1)) {
379 if ((pwr & DPCD_SC00_SET_POWER) != DPCD_SC00_SET_POWER_D0) {
380 pwr &= ~DPCD_SC00_SET_POWER;
381 pwr |= DPCD_SC00_SET_POWER_D0;
382 nvkm_wraux(dp->aux, DPCD_SC00, &pwr, 1);
387 OUTP_DBG(&dp->outp, "training (min: %d x %d MB/s)",
388 failsafe->nr, failsafe->bw * 27);
389 nvkm_dp_train_init(dp);
390 for (cfg = nvkm_dp_rates; ret < 0 && cfg <= failsafe; cfg++) {
391 /* Skip configurations not supported by both OR and sink. */
392 if ((cfg->nr > outp_nr || cfg->bw > outp_bw ||
393 cfg->nr > sink_nr || cfg->bw > sink_bw)) {
396 OUTP_ERR(&dp->outp, "link rate unsupported by sink");
398 ior->dp.mst = dp->lt.mst;
399 ior->dp.ef = dp->dpcd[DPCD_RC02] & DPCD_RC02_ENHANCED_FRAME_CAP;
400 ior->dp.bw = cfg->bw;
401 ior->dp.nr = cfg->nr;
403 /* Program selected link configuration. */
404 ret = nvkm_dp_train_links(dp);
406 nvkm_dp_train_fini(dp);
408 OUTP_ERR(&dp->outp, "training failed");
410 OUTP_DBG(&dp->outp, "training done");
411 atomic_set(&dp->lt.done, 1);
416 nvkm_dp_disable(struct nvkm_outp *outp, struct nvkm_ior *ior)
418 struct nvkm_dp *dp = nvkm_dp(outp);
420 /* Execute DisableLT script from DP Info Table. */
421 nvbios_init(&ior->disp->engine.subdev, dp->info.script[4],
422 init.outp = &dp->outp.info;
424 init.link = ior->arm.link;
429 nvkm_dp_release(struct nvkm_outp *outp)
431 struct nvkm_dp *dp = nvkm_dp(outp);
433 /* Prevent link from being retrained if sink sends an IRQ. */
434 atomic_set(&dp->lt.done, 0);
435 dp->outp.ior->dp.nr = 0;
439 nvkm_dp_acquire(struct nvkm_outp *outp)
441 struct nvkm_dp *dp = nvkm_dp(outp);
442 struct nvkm_ior *ior = dp->outp.ior;
443 struct nvkm_head *head;
451 mutex_lock(&dp->mutex);
453 /* Check that link configuration meets current requirements. */
454 list_for_each_entry(head, &outp->disp->head, head) {
455 if (ior->asy.head & (1 << head->id)) {
456 u32 khz = (head->asy.hz >> ior->asy.rgdiv) / 1000;
457 datakbps += khz * head->asy.or.depth;
461 linkKBps = ior->dp.bw * 27000 * ior->dp.nr;
462 dataKBps = DIV_ROUND_UP(datakbps, 8);
463 OUTP_DBG(&dp->outp, "data %d KB/s link %d KB/s mst %d->%d",
464 dataKBps, linkKBps, ior->dp.mst, dp->lt.mst);
465 if (linkKBps < dataKBps || ior->dp.mst != dp->lt.mst) {
466 OUTP_DBG(&dp->outp, "link requirements changed");
470 /* Check that link is still trained. */
471 ret = nvkm_rdaux(dp->aux, DPCD_LS02, stat, 3);
474 "failed to read link status, assuming no sink");
478 if (stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE) {
479 for (i = 0; i < ior->dp.nr; i++) {
480 u8 lane = (stat[i >> 1] >> ((i & 1) * 4)) & 0x0f;
481 if (!(lane & DPCD_LS02_LANE0_CR_DONE) ||
482 !(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
483 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED)) {
485 "lane %d not equalised", lane);
491 OUTP_DBG(&dp->outp, "no inter-lane alignment");
495 if (retrain || !atomic_read(&dp->lt.done))
496 ret = nvkm_dp_train(dp, dataKBps);
497 mutex_unlock(&dp->mutex);
502 nvkm_dp_enable(struct nvkm_dp *dp, bool enable)
504 struct nvkm_i2c_aux *aux = dp->aux;
508 OUTP_DBG(&dp->outp, "aux power -> always");
509 nvkm_i2c_aux_monitor(aux, true);
513 if (!nvkm_rdaux(aux, DPCD_RC00_DPCD_REV, dp->dpcd,
519 OUTP_DBG(&dp->outp, "aux power -> demand");
520 nvkm_i2c_aux_monitor(aux, false);
524 atomic_set(&dp->lt.done, 0);
529 nvkm_dp_hpd(struct nvkm_notify *notify)
531 const struct nvkm_i2c_ntfy_rep *line = notify->data;
532 struct nvkm_dp *dp = container_of(notify, typeof(*dp), hpd);
533 struct nvkm_conn *conn = dp->outp.conn;
534 struct nvkm_disp *disp = dp->outp.disp;
535 struct nvif_notify_conn_rep_v0 rep = {};
537 OUTP_DBG(&dp->outp, "HPD: %d", line->mask);
538 if (line->mask & NVKM_I2C_IRQ) {
539 if (atomic_read(&dp->lt.done))
540 dp->outp.func->acquire(&dp->outp);
541 rep.mask |= NVIF_NOTIFY_CONN_V0_IRQ;
543 nvkm_dp_enable(dp, true);
546 if (line->mask & NVKM_I2C_UNPLUG)
547 rep.mask |= NVIF_NOTIFY_CONN_V0_UNPLUG;
548 if (line->mask & NVKM_I2C_PLUG)
549 rep.mask |= NVIF_NOTIFY_CONN_V0_PLUG;
551 nvkm_event_send(&disp->hpd, rep.mask, conn->index, &rep, sizeof(rep));
552 return NVKM_NOTIFY_KEEP;
556 nvkm_dp_fini(struct nvkm_outp *outp)
558 struct nvkm_dp *dp = nvkm_dp(outp);
559 nvkm_notify_put(&dp->hpd);
560 nvkm_dp_enable(dp, false);
564 nvkm_dp_init(struct nvkm_outp *outp)
566 struct nvkm_gpio *gpio = outp->disp->engine.subdev.device->gpio;
567 struct nvkm_dp *dp = nvkm_dp(outp);
569 nvkm_notify_put(&dp->outp.conn->hpd);
571 /* eDP panels need powering on by us (if the VBIOS doesn't default it
572 * to on) before doing any AUX channel transactions. LVDS panel power
573 * is handled by the SOR itself, and not required for LVDS DDC.
575 if (dp->outp.conn->info.type == DCB_CONNECTOR_eDP) {
576 int power = nvkm_gpio_get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff);
578 nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1);
580 /* We delay here unconditionally, even if already powered,
581 * because some laptop panels having a significant resume
582 * delay before the panel begins responding.
584 * This is likely a bit of a hack, but no better idea for
585 * handling this at the moment.
589 /* If the eDP panel can't be detected, we need to restore
590 * the panel power GPIO to avoid breaking another output.
592 if (!nvkm_dp_enable(dp, true) && power == 0)
593 nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 0);
595 nvkm_dp_enable(dp, true);
598 nvkm_notify_get(&dp->hpd);
602 nvkm_dp_dtor(struct nvkm_outp *outp)
604 struct nvkm_dp *dp = nvkm_dp(outp);
605 nvkm_notify_fini(&dp->hpd);
609 static const struct nvkm_outp_func
611 .dtor = nvkm_dp_dtor,
612 .init = nvkm_dp_init,
613 .fini = nvkm_dp_fini,
614 .acquire = nvkm_dp_acquire,
615 .release = nvkm_dp_release,
616 .disable = nvkm_dp_disable,
620 nvkm_dp_ctor(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
621 struct nvkm_i2c_aux *aux, struct nvkm_dp *dp)
623 struct nvkm_device *device = disp->engine.subdev.device;
624 struct nvkm_bios *bios = device->bios;
625 struct nvkm_i2c *i2c = device->i2c;
630 ret = nvkm_outp_ctor(&nvkm_dp_func, disp, index, dcbE, &dp->outp);
636 OUTP_ERR(&dp->outp, "no aux");
640 /* bios data is not optional */
641 data = nvbios_dpout_match(bios, dp->outp.info.hasht,
642 dp->outp.info.hashm, &dp->version,
643 &hdr, &cnt, &len, &dp->info);
645 OUTP_ERR(&dp->outp, "no bios dp data");
649 OUTP_DBG(&dp->outp, "bios dp %02x %02x %02x %02x",
650 dp->version, hdr, cnt, len);
652 /* hotplug detect, replaces gpio-based mechanism with aux events */
653 ret = nvkm_notify_init(NULL, &i2c->event, nvkm_dp_hpd, true,
654 &(struct nvkm_i2c_ntfy_req) {
655 .mask = NVKM_I2C_PLUG | NVKM_I2C_UNPLUG |
659 sizeof(struct nvkm_i2c_ntfy_req),
660 sizeof(struct nvkm_i2c_ntfy_rep),
663 OUTP_ERR(&dp->outp, "error monitoring aux hpd: %d", ret);
667 mutex_init(&dp->mutex);
668 atomic_set(&dp->lt.done, 0);
673 nvkm_dp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
674 struct nvkm_outp **poutp)
676 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
677 struct nvkm_i2c_aux *aux;
680 if (dcbE->location == 0)
681 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_CCB(dcbE->i2c_index));
683 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbE->extdev));
685 if (!(dp = kzalloc(sizeof(*dp), GFP_KERNEL)))
689 return nvkm_dp_ctor(disp, index, dcbE, aux, dp);