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>
36 /* IED scripts are no longer used by UEFI/RM from Ampere, but have been updated for
37 * the x86 option ROM. However, the relevant VBIOS table versions weren't modified,
38 * so we're unable to detect this in a nice way.
40 #define AMPERE_IED_HACK(disp) ((disp)->engine.subdev.device->card_type >= GA100)
52 nvkm_dp_train_sense(struct lt_state *lt, bool pc, u32 delay)
54 struct nvkm_dp *dp = lt->dp;
57 usleep_range(delay, delay * 2);
59 ret = nvkm_rdaux(dp->aux, DPCD_LS02, lt->stat, 6);
64 ret = nvkm_rdaux(dp->aux, DPCD_LS0C, <->pc2stat, 1);
67 OUTP_TRACE(&dp->outp, "status %6ph pc2 %02x",
68 lt->stat, lt->pc2stat);
70 OUTP_TRACE(&dp->outp, "status %6ph", lt->stat);
77 nvkm_dp_train_drive(struct lt_state *lt, bool pc)
79 struct nvkm_dp *dp = lt->dp;
80 struct nvkm_ior *ior = dp->outp.ior;
81 struct nvkm_bios *bios = ior->disp->engine.subdev.device->bios;
82 struct nvbios_dpout info;
83 struct nvbios_dpcfg ocfg;
84 u8 ver, hdr, cnt, len;
88 for (i = 0; i < ior->dp.nr; i++) {
89 u8 lane = (lt->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
90 u8 lpc2 = (lt->pc2stat >> (i * 2)) & 0x3;
91 u8 lpre = (lane & 0x0c) >> 2;
92 u8 lvsw = (lane & 0x03) >> 0;
98 lpc2 = hipc | DPCD_LC0F_LANE0_MAX_POST_CURSOR2_REACHED;
100 lpre = hipe | DPCD_LC03_MAX_SWING_REACHED; /* yes. */
101 lvsw = hivs = 3 - (lpre & 3);
104 lvsw = hivs | DPCD_LC03_MAX_SWING_REACHED;
107 lt->conf[i] = (lpre << 3) | lvsw;
108 lt->pc2conf[i >> 1] |= lpc2 << ((i & 1) * 4);
110 OUTP_TRACE(&dp->outp, "config lane %d %02x %02x",
111 i, lt->conf[i], lpc2);
113 data = nvbios_dpout_match(bios, dp->outp.info.hasht,
115 &ver, &hdr, &cnt, &len, &info);
119 data = nvbios_dpcfg_match(bios, data, lpc2 & 3, lvsw & 3,
120 lpre & 3, &ver, &hdr, &cnt, &len,
125 ior->func->dp.drive(ior, i, ocfg.pc, ocfg.dc,
126 ocfg.pe, ocfg.tx_pu);
129 ret = nvkm_wraux(dp->aux, DPCD_LC03(0), lt->conf, 4);
134 ret = nvkm_wraux(dp->aux, DPCD_LC0F, lt->pc2conf, 2);
143 nvkm_dp_train_pattern(struct lt_state *lt, u8 pattern)
145 struct nvkm_dp *dp = lt->dp;
148 OUTP_TRACE(&dp->outp, "training pattern %d", pattern);
149 dp->outp.ior->func->dp.pattern(dp->outp.ior, pattern);
151 nvkm_rdaux(dp->aux, DPCD_LC02, &sink_tp, 1);
152 sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
153 sink_tp |= (pattern != 4) ? pattern : 7;
156 sink_tp |= DPCD_LC02_SCRAMBLING_DISABLE;
158 sink_tp &= ~DPCD_LC02_SCRAMBLING_DISABLE;
159 nvkm_wraux(dp->aux, DPCD_LC02, &sink_tp, 1);
163 nvkm_dp_train_eq(struct lt_state *lt)
165 bool eq_done = false, cr_done = true;
166 int tries = 0, usec = 0, i;
169 if (lt->dp->dpcd[DPCD_RC00_DPCD_REV] >= 0x14 &&
170 lt->dp->dpcd[DPCD_RC03] & DPCD_RC03_TPS4_SUPPORTED)
171 nvkm_dp_train_pattern(lt, 4);
173 if (lt->dp->dpcd[DPCD_RC00_DPCD_REV] >= 0x12 &&
174 lt->dp->dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED)
175 nvkm_dp_train_pattern(lt, 3);
177 nvkm_dp_train_pattern(lt, 2);
179 usec = (lt->dp->dpcd[DPCD_RC0E] & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
184 nvkm_dp_train_drive(lt, lt->pc2)) ||
185 nvkm_dp_train_sense(lt, lt->pc2, usec ? usec : 400))
188 eq_done = !!(lt->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
189 for (i = 0; i < lt->dp->outp.ior->dp.nr && eq_done; i++) {
190 u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
191 if (!(lane & DPCD_LS02_LANE0_CR_DONE))
193 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
194 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
197 } while (!eq_done && cr_done && ++tries <= 5);
199 return eq_done ? 0 : -1;
203 nvkm_dp_train_cr(struct lt_state *lt)
205 bool cr_done = false, abort = false;
206 int voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
207 int tries = 0, usec = 0, i;
209 nvkm_dp_train_pattern(lt, 1);
211 if (lt->dp->dpcd[DPCD_RC00_DPCD_REV] < 0x14)
212 usec = (lt->dp->dpcd[DPCD_RC0E] & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
215 if (nvkm_dp_train_drive(lt, false) ||
216 nvkm_dp_train_sense(lt, false, usec ? usec : 100))
220 for (i = 0; i < lt->dp->outp.ior->dp.nr; i++) {
221 u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
222 if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
224 if (lt->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
230 if ((lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
231 voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
234 } while (!cr_done && !abort && ++tries < 5);
236 return cr_done ? 0 : -1;
240 nvkm_dp_train_links(struct nvkm_dp *dp)
242 struct nvkm_ior *ior = dp->outp.ior;
243 struct nvkm_disp *disp = dp->outp.disp;
244 struct nvkm_subdev *subdev = &disp->engine.subdev;
245 struct nvkm_bios *bios = subdev->device->bios;
246 struct lt_state lt = {
253 OUTP_DBG(&dp->outp, "training %d x %d MB/s",
254 ior->dp.nr, ior->dp.bw * 27);
256 /* Intersect misc. capabilities of the OR and sink. */
257 if (disp->engine.subdev.device->chipset < 0x110)
258 dp->dpcd[DPCD_RC03] &= ~DPCD_RC03_TPS4_SUPPORTED;
259 if (disp->engine.subdev.device->chipset < 0xd0)
260 dp->dpcd[DPCD_RC02] &= ~DPCD_RC02_TPS3_SUPPORTED;
261 lt.pc2 = dp->dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED;
263 if (AMPERE_IED_HACK(disp) && (lnkcmp = lt.dp->info.script[0])) {
264 /* Execute BeforeLinkTraining script from DP Info table. */
265 while (ior->dp.bw < nvbios_rd08(bios, lnkcmp))
267 lnkcmp = nvbios_rd16(bios, lnkcmp + 1);
269 nvbios_init(&dp->outp.disp->engine.subdev, lnkcmp,
270 init.outp = &dp->outp.info;
272 init.link = ior->asy.link;
276 /* Set desired link configuration on the source. */
277 if ((lnkcmp = lt.dp->info.lnkcmp)) {
278 if (dp->version < 0x30) {
279 while ((ior->dp.bw * 2700) < nvbios_rd16(bios, lnkcmp))
281 lnkcmp = nvbios_rd16(bios, lnkcmp + 2);
283 while (ior->dp.bw < nvbios_rd08(bios, lnkcmp))
285 lnkcmp = nvbios_rd16(bios, lnkcmp + 1);
288 nvbios_init(subdev, lnkcmp,
289 init.outp = &dp->outp.info;
291 init.link = ior->asy.link;
295 ret = ior->func->dp.links(ior, dp->aux);
298 OUTP_ERR(&dp->outp, "train failed with %d", ret);
304 ior->func->dp.power(ior, ior->dp.nr);
306 /* Set desired link configuration on the sink. */
307 sink[0] = ior->dp.bw;
308 sink[1] = ior->dp.nr;
310 sink[1] |= DPCD_LC01_ENHANCED_FRAME_EN;
312 ret = nvkm_wraux(dp->aux, DPCD_LC00_LINK_BW_SET, sink, 2);
316 /* Attempt to train the link in this configuration. */
317 memset(lt.stat, 0x00, sizeof(lt.stat));
318 ret = nvkm_dp_train_cr(<);
320 ret = nvkm_dp_train_eq(<);
321 nvkm_dp_train_pattern(<, 0);
326 nvkm_dp_train_fini(struct nvkm_dp *dp)
328 /* Execute AfterLinkTraining script from DP Info table. */
329 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[1],
330 init.outp = &dp->outp.info;
331 init.or = dp->outp.ior->id;
332 init.link = dp->outp.ior->asy.link;
337 nvkm_dp_train_init(struct nvkm_dp *dp)
339 /* Execute EnableSpread/DisableSpread script from DP Info table. */
340 if (dp->dpcd[DPCD_RC03] & DPCD_RC03_MAX_DOWNSPREAD) {
341 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[2],
342 init.outp = &dp->outp.info;
343 init.or = dp->outp.ior->id;
344 init.link = dp->outp.ior->asy.link;
347 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[3],
348 init.outp = &dp->outp.info;
349 init.or = dp->outp.ior->id;
350 init.link = dp->outp.ior->asy.link;
354 if (!AMPERE_IED_HACK(dp->outp.disp)) {
355 /* Execute BeforeLinkTraining script from DP Info table. */
356 nvbios_init(&dp->outp.disp->engine.subdev, dp->info.script[0],
357 init.outp = &dp->outp.info;
358 init.or = dp->outp.ior->id;
359 init.link = dp->outp.ior->asy.link;
365 nvkm_dp_train(struct nvkm_dp *dp, u32 dataKBps)
367 struct nvkm_ior *ior = dp->outp.ior;
368 int ret = -EINVAL, nr, rate;
371 /* Ensure sink is not in a low-power state. */
372 if (!nvkm_rdaux(dp->aux, DPCD_SC00, &pwr, 1)) {
373 if ((pwr & DPCD_SC00_SET_POWER) != DPCD_SC00_SET_POWER_D0) {
374 pwr &= ~DPCD_SC00_SET_POWER;
375 pwr |= DPCD_SC00_SET_POWER_D0;
376 nvkm_wraux(dp->aux, DPCD_SC00, &pwr, 1);
380 ior->dp.mst = dp->lt.mst;
381 ior->dp.ef = dp->dpcd[DPCD_RC02] & DPCD_RC02_ENHANCED_FRAME_CAP;
385 OUTP_DBG(&dp->outp, "training");
386 nvkm_dp_train_init(dp);
387 for (nr = dp->links; ret < 0 && nr; nr >>= 1) {
388 for (rate = 0; ret < 0 && rate < dp->rates; rate++) {
389 if (dp->rate[rate].rate * nr >= dataKBps || WARN_ON(!ior->dp.nr)) {
390 /* Program selected link configuration. */
391 ior->dp.bw = dp->rate[rate].rate / 27000;
393 ret = nvkm_dp_train_links(dp);
397 nvkm_dp_train_fini(dp);
399 OUTP_ERR(&dp->outp, "training failed");
401 OUTP_DBG(&dp->outp, "training done");
402 atomic_set(&dp->lt.done, 1);
407 nvkm_dp_disable(struct nvkm_outp *outp, struct nvkm_ior *ior)
409 struct nvkm_dp *dp = nvkm_dp(outp);
411 /* Execute DisableLT script from DP Info Table. */
412 nvbios_init(&ior->disp->engine.subdev, dp->info.script[4],
413 init.outp = &dp->outp.info;
415 init.link = ior->arm.link;
420 nvkm_dp_release(struct nvkm_outp *outp)
422 struct nvkm_dp *dp = nvkm_dp(outp);
424 /* Prevent link from being retrained if sink sends an IRQ. */
425 atomic_set(&dp->lt.done, 0);
426 dp->outp.ior->dp.nr = 0;
430 nvkm_dp_acquire(struct nvkm_outp *outp)
432 struct nvkm_dp *dp = nvkm_dp(outp);
433 struct nvkm_ior *ior = dp->outp.ior;
434 struct nvkm_head *head;
442 mutex_lock(&dp->mutex);
444 /* Check that link configuration meets current requirements. */
445 list_for_each_entry(head, &outp->disp->head, head) {
446 if (ior->asy.head & (1 << head->id)) {
447 u32 khz = (head->asy.hz >> ior->asy.rgdiv) / 1000;
448 datakbps += khz * head->asy.or.depth;
452 linkKBps = ior->dp.bw * 27000 * ior->dp.nr;
453 dataKBps = DIV_ROUND_UP(datakbps, 8);
454 OUTP_DBG(&dp->outp, "data %d KB/s link %d KB/s mst %d->%d",
455 dataKBps, linkKBps, ior->dp.mst, dp->lt.mst);
456 if (linkKBps < dataKBps || ior->dp.mst != dp->lt.mst) {
457 OUTP_DBG(&dp->outp, "link requirements changed");
461 /* Check that link is still trained. */
462 ret = nvkm_rdaux(dp->aux, DPCD_LS02, stat, 3);
465 "failed to read link status, assuming no sink");
469 if (stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE) {
470 for (i = 0; i < ior->dp.nr; i++) {
471 u8 lane = (stat[i >> 1] >> ((i & 1) * 4)) & 0x0f;
472 if (!(lane & DPCD_LS02_LANE0_CR_DONE) ||
473 !(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
474 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED)) {
476 "lane %d not equalised", lane);
482 OUTP_DBG(&dp->outp, "no inter-lane alignment");
486 if (retrain || !atomic_read(&dp->lt.done))
487 ret = nvkm_dp_train(dp, dataKBps);
488 mutex_unlock(&dp->mutex);
493 nvkm_dp_enable(struct nvkm_dp *dp, bool enable)
495 struct nvkm_i2c_aux *aux = dp->aux;
499 OUTP_DBG(&dp->outp, "aux power -> always");
500 nvkm_i2c_aux_monitor(aux, true);
504 if (!nvkm_rdaux(aux, DPCD_RC00_DPCD_REV, dp->dpcd, sizeof(dp->dpcd))) {
505 const u8 rates[] = { 0x14, 0x0a, 0x06, 0 };
510 dp->links = dp->dpcd[DPCD_RC02] & DPCD_RC02_MAX_LANE_COUNT;
511 dp->links = min(dp->links, dp->outp.info.dpconf.link_nr);
513 rate_max = dp->dpcd[DPCD_RC01_MAX_LINK_RATE];
514 rate_max = min(rate_max, dp->outp.info.dpconf.link_bw);
517 for (rate = rates; *rate; rate++) {
518 if (*rate <= rate_max) {
519 if (WARN_ON(dp->rates == ARRAY_SIZE(dp->rate)))
522 dp->rate[dp->rates].rate = *rate * 27000;
533 OUTP_DBG(&dp->outp, "aux power -> demand");
534 nvkm_i2c_aux_monitor(aux, false);
538 atomic_set(&dp->lt.done, 0);
543 nvkm_dp_hpd(struct nvkm_notify *notify)
545 const struct nvkm_i2c_ntfy_rep *line = notify->data;
546 struct nvkm_dp *dp = container_of(notify, typeof(*dp), hpd);
547 struct nvkm_conn *conn = dp->outp.conn;
548 struct nvkm_disp *disp = dp->outp.disp;
549 struct nvif_notify_conn_rep_v0 rep = {};
551 OUTP_DBG(&dp->outp, "HPD: %d", line->mask);
552 if (line->mask & NVKM_I2C_IRQ) {
553 if (atomic_read(&dp->lt.done))
554 dp->outp.func->acquire(&dp->outp);
555 rep.mask |= NVIF_NOTIFY_CONN_V0_IRQ;
557 nvkm_dp_enable(dp, true);
560 if (line->mask & NVKM_I2C_UNPLUG)
561 rep.mask |= NVIF_NOTIFY_CONN_V0_UNPLUG;
562 if (line->mask & NVKM_I2C_PLUG)
563 rep.mask |= NVIF_NOTIFY_CONN_V0_PLUG;
565 nvkm_event_send(&disp->hpd, rep.mask, conn->index, &rep, sizeof(rep));
566 return NVKM_NOTIFY_KEEP;
570 nvkm_dp_fini(struct nvkm_outp *outp)
572 struct nvkm_dp *dp = nvkm_dp(outp);
573 nvkm_notify_put(&dp->hpd);
574 nvkm_dp_enable(dp, false);
578 nvkm_dp_init(struct nvkm_outp *outp)
580 struct nvkm_gpio *gpio = outp->disp->engine.subdev.device->gpio;
581 struct nvkm_dp *dp = nvkm_dp(outp);
583 nvkm_notify_put(&dp->outp.conn->hpd);
585 /* eDP panels need powering on by us (if the VBIOS doesn't default it
586 * to on) before doing any AUX channel transactions. LVDS panel power
587 * is handled by the SOR itself, and not required for LVDS DDC.
589 if (dp->outp.conn->info.type == DCB_CONNECTOR_eDP) {
590 int power = nvkm_gpio_get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff);
592 nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1);
594 /* We delay here unconditionally, even if already powered,
595 * because some laptop panels having a significant resume
596 * delay before the panel begins responding.
598 * This is likely a bit of a hack, but no better idea for
599 * handling this at the moment.
603 /* If the eDP panel can't be detected, we need to restore
604 * the panel power GPIO to avoid breaking another output.
606 if (!nvkm_dp_enable(dp, true) && power == 0)
607 nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 0);
609 nvkm_dp_enable(dp, true);
612 nvkm_notify_get(&dp->hpd);
616 nvkm_dp_dtor(struct nvkm_outp *outp)
618 struct nvkm_dp *dp = nvkm_dp(outp);
619 nvkm_notify_fini(&dp->hpd);
623 static const struct nvkm_outp_func
625 .dtor = nvkm_dp_dtor,
626 .init = nvkm_dp_init,
627 .fini = nvkm_dp_fini,
628 .acquire = nvkm_dp_acquire,
629 .release = nvkm_dp_release,
630 .disable = nvkm_dp_disable,
634 nvkm_dp_ctor(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
635 struct nvkm_i2c_aux *aux, struct nvkm_dp *dp)
637 struct nvkm_device *device = disp->engine.subdev.device;
638 struct nvkm_bios *bios = device->bios;
639 struct nvkm_i2c *i2c = device->i2c;
644 ret = nvkm_outp_ctor(&nvkm_dp_func, disp, index, dcbE, &dp->outp);
650 OUTP_ERR(&dp->outp, "no aux");
654 /* bios data is not optional */
655 data = nvbios_dpout_match(bios, dp->outp.info.hasht,
656 dp->outp.info.hashm, &dp->version,
657 &hdr, &cnt, &len, &dp->info);
659 OUTP_ERR(&dp->outp, "no bios dp data");
663 OUTP_DBG(&dp->outp, "bios dp %02x %02x %02x %02x",
664 dp->version, hdr, cnt, len);
666 /* hotplug detect, replaces gpio-based mechanism with aux events */
667 ret = nvkm_notify_init(NULL, &i2c->event, nvkm_dp_hpd, true,
668 &(struct nvkm_i2c_ntfy_req) {
669 .mask = NVKM_I2C_PLUG | NVKM_I2C_UNPLUG |
673 sizeof(struct nvkm_i2c_ntfy_req),
674 sizeof(struct nvkm_i2c_ntfy_rep),
677 OUTP_ERR(&dp->outp, "error monitoring aux hpd: %d", ret);
681 mutex_init(&dp->mutex);
682 atomic_set(&dp->lt.done, 0);
687 nvkm_dp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
688 struct nvkm_outp **poutp)
690 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
691 struct nvkm_i2c_aux *aux;
694 if (dcbE->location == 0)
695 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_CCB(dcbE->i2c_index));
697 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbE->extdev));
699 if (!(dp = kzalloc(sizeof(*dp), GFP_KERNEL)))
703 return nvkm_dp_ctor(disp, index, dcbE, aux, dp);