1 /*
2 * Copyright 2011 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Ben Skeggs
23 */
24 #include "disp.h"
25 #include "atom.h"
26 #include "core.h"
27 #include "head.h"
28 #include "wndw.h"
29 #include "handles.h"
30
31 #include <linux/backlight.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/hdmi.h>
34 #include <linux/component.h>
35 #include <linux/iopoll.h>
36
37 #include <drm/display/drm_dp_helper.h>
38 #include <drm/display/drm_scdc_helper.h>
39 #include <drm/drm_atomic.h>
40 #include <drm/drm_atomic_helper.h>
41 #include <drm/drm_edid.h>
42 #include <drm/drm_eld.h>
43 #include <drm/drm_fb_helper.h>
44 #include <drm/drm_fixed.h>
45 #include <drm/drm_probe_helper.h>
46 #include <drm/drm_vblank.h>
47
48 #include <nvif/push507c.h>
49
50 #include <nvif/class.h>
51 #include <nvif/cl0002.h>
52 #include <nvif/event.h>
53 #include <nvif/if0012.h>
54 #include <nvif/if0014.h>
55 #include <nvif/timer.h>
56
57 #include <nvhw/class/cl507c.h>
58 #include <nvhw/class/cl507d.h>
59 #include <nvhw/class/cl837d.h>
60 #include <nvhw/class/cl887d.h>
61 #include <nvhw/class/cl907d.h>
62 #include <nvhw/class/cl917d.h>
63
64 #include "nouveau_drv.h"
65 #include "nouveau_dma.h"
66 #include "nouveau_gem.h"
67 #include "nouveau_connector.h"
68 #include "nouveau_encoder.h"
69 #include "nouveau_fence.h"
70 #include "nv50_display.h"
71
72 /******************************************************************************
73 * EVO channel
74 *****************************************************************************/
75
76 static int
nv50_chan_create(struct nvif_device * device,struct nvif_object * disp,const s32 * oclass,u8 head,void * data,u32 size,struct nv50_chan * chan)77 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
78 const s32 *oclass, u8 head, void *data, u32 size,
79 struct nv50_chan *chan)
80 {
81 struct nvif_sclass *sclass;
82 int ret, i, n;
83
84 chan->device = device;
85
86 ret = n = nvif_object_sclass_get(disp, &sclass);
87 if (ret < 0)
88 return ret;
89
90 while (oclass[0]) {
91 for (i = 0; i < n; i++) {
92 if (sclass[i].oclass == oclass[0]) {
93 ret = nvif_object_ctor(disp, "kmsChan", 0,
94 oclass[0], data, size,
95 &chan->user);
96 if (ret == 0) {
97 ret = nvif_object_map(&chan->user, NULL, 0);
98 if (ret)
99 nvif_object_dtor(&chan->user);
100 }
101 nvif_object_sclass_put(&sclass);
102 return ret;
103 }
104 }
105 oclass++;
106 }
107
108 nvif_object_sclass_put(&sclass);
109 return -ENOSYS;
110 }
111
112 static void
nv50_chan_destroy(struct nv50_chan * chan)113 nv50_chan_destroy(struct nv50_chan *chan)
114 {
115 nvif_object_dtor(&chan->user);
116 }
117
118 /******************************************************************************
119 * DMA EVO channel
120 *****************************************************************************/
121
122 void
nv50_dmac_destroy(struct nv50_dmac * dmac)123 nv50_dmac_destroy(struct nv50_dmac *dmac)
124 {
125 nvif_object_dtor(&dmac->vram);
126 nvif_object_dtor(&dmac->sync);
127
128 nv50_chan_destroy(&dmac->base);
129
130 nvif_mem_dtor(&dmac->push.mem);
131 }
132
133 static void
nv50_dmac_kick(struct nvif_push * push)134 nv50_dmac_kick(struct nvif_push *push)
135 {
136 struct nv50_dmac *dmac = container_of(push, typeof(*dmac), push);
137
138 dmac->cur = push->cur - (u32 __iomem *)dmac->push.mem.object.map.ptr;
139 if (dmac->put != dmac->cur) {
140 /* Push buffer fetches are not coherent with BAR1, we need to ensure
141 * writes have been flushed right through to VRAM before writing PUT.
142 */
143 if (dmac->push.mem.type & NVIF_MEM_VRAM) {
144 struct nvif_device *device = dmac->base.device;
145 nvif_wr32(&device->object, 0x070000, 0x00000001);
146 nvif_msec(device, 2000,
147 if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
148 break;
149 );
150 }
151
152 NVIF_WV32(&dmac->base.user, NV507C, PUT, PTR, dmac->cur);
153 dmac->put = dmac->cur;
154 }
155
156 push->bgn = push->cur;
157 }
158
159 static int
nv50_dmac_free(struct nv50_dmac * dmac)160 nv50_dmac_free(struct nv50_dmac *dmac)
161 {
162 u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
163 if (get > dmac->cur) /* NVIDIA stay 5 away from GET, do the same. */
164 return get - dmac->cur - 5;
165 return dmac->max - dmac->cur;
166 }
167
168 static int
nv50_dmac_wind(struct nv50_dmac * dmac)169 nv50_dmac_wind(struct nv50_dmac *dmac)
170 {
171 /* Wait for GET to depart from the beginning of the push buffer to
172 * prevent writing PUT == GET, which would be ignored by HW.
173 */
174 u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
175 if (get == 0) {
176 /* Corner-case, HW idle, but non-committed work pending. */
177 if (dmac->put == 0)
178 nv50_dmac_kick(&dmac->push);
179
180 if (nvif_msec(dmac->base.device, 2000,
181 if (NVIF_TV32(&dmac->base.user, NV507C, GET, PTR, >, 0))
182 break;
183 ) < 0)
184 return -ETIMEDOUT;
185 }
186
187 PUSH_RSVD(&dmac->push, PUSH_JUMP(&dmac->push, 0));
188 dmac->cur = 0;
189 return 0;
190 }
191
192 static int
nv50_dmac_wait(struct nvif_push * push,u32 size)193 nv50_dmac_wait(struct nvif_push *push, u32 size)
194 {
195 struct nv50_dmac *dmac = container_of(push, typeof(*dmac), push);
196 int free;
197
198 if (WARN_ON(size > dmac->max))
199 return -EINVAL;
200
201 dmac->cur = push->cur - (u32 __iomem *)dmac->push.mem.object.map.ptr;
202 if (dmac->cur + size >= dmac->max) {
203 int ret = nv50_dmac_wind(dmac);
204 if (ret)
205 return ret;
206
207 push->cur = dmac->push.mem.object.map.ptr;
208 push->cur = push->cur + dmac->cur;
209 nv50_dmac_kick(push);
210 }
211
212 if (nvif_msec(dmac->base.device, 2000,
213 if ((free = nv50_dmac_free(dmac)) >= size)
214 break;
215 ) < 0) {
216 WARN_ON(1);
217 return -ETIMEDOUT;
218 }
219
220 push->bgn = dmac->push.mem.object.map.ptr;
221 push->bgn = push->bgn + dmac->cur;
222 push->cur = push->bgn;
223 push->end = push->cur + free;
224 return 0;
225 }
226
227 MODULE_PARM_DESC(kms_vram_pushbuf, "Place EVO/NVD push buffers in VRAM (default: auto)");
228 static int nv50_dmac_vram_pushbuf = -1;
229 module_param_named(kms_vram_pushbuf, nv50_dmac_vram_pushbuf, int, 0400);
230
231 int
nv50_dmac_create(struct nouveau_drm * drm,const s32 * oclass,u8 head,void * data,u32 size,s64 syncbuf,struct nv50_dmac * dmac)232 nv50_dmac_create(struct nouveau_drm *drm,
233 const s32 *oclass, u8 head, void *data, u32 size, s64 syncbuf,
234 struct nv50_dmac *dmac)
235 {
236 struct nvif_device *device = &drm->device;
237 struct nvif_object *disp = &drm->display->disp.object;
238 struct nvif_disp_chan_v0 *args = data;
239 u8 type = NVIF_MEM_COHERENT;
240 int ret;
241
242 /* Pascal added support for 47-bit physical addresses, but some
243 * parts of EVO still only accept 40-bit PAs.
244 *
245 * To avoid issues on systems with large amounts of RAM, and on
246 * systems where an IOMMU maps pages at a high address, we need
247 * to allocate push buffers in VRAM instead.
248 *
249 * This appears to match NVIDIA's behaviour on Pascal.
250 */
251 if ((nv50_dmac_vram_pushbuf > 0) ||
252 (nv50_dmac_vram_pushbuf < 0 && device->info.family == NV_DEVICE_INFO_V0_PASCAL))
253 type |= NVIF_MEM_VRAM;
254
255 ret = nvif_mem_ctor_map(&drm->mmu, "kmsChanPush", type, 0x1000, &dmac->push.mem);
256 if (ret)
257 return ret;
258
259 dmac->push.wait = nv50_dmac_wait;
260 dmac->push.kick = nv50_dmac_kick;
261 dmac->push.bgn = dmac->push.mem.object.map.ptr;
262 dmac->push.cur = dmac->push.bgn;
263 dmac->push.end = dmac->push.bgn;
264 dmac->max = 0x1000/4 - 1;
265
266 /* EVO channels are affected by a HW bug where the last 12 DWORDs
267 * of the push buffer aren't able to be used safely.
268 */
269 if (disp->oclass < GV100_DISP)
270 dmac->max -= 12;
271
272 args->pushbuf = nvif_handle(&dmac->push.mem.object);
273
274 ret = nv50_chan_create(device, disp, oclass, head, data, size,
275 &dmac->base);
276 if (ret)
277 return ret;
278
279 if (syncbuf < 0)
280 return 0;
281
282 ret = nvif_object_ctor(&dmac->base.user, "kmsSyncCtxDma", NV50_DISP_HANDLE_SYNCBUF,
283 NV_DMA_IN_MEMORY,
284 &(struct nv_dma_v0) {
285 .target = NV_DMA_V0_TARGET_VRAM,
286 .access = NV_DMA_V0_ACCESS_RDWR,
287 .start = syncbuf + 0x0000,
288 .limit = syncbuf + 0x0fff,
289 }, sizeof(struct nv_dma_v0),
290 &dmac->sync);
291 if (ret)
292 return ret;
293
294 ret = nvif_object_ctor(&dmac->base.user, "kmsVramCtxDma", NV50_DISP_HANDLE_VRAM,
295 NV_DMA_IN_MEMORY,
296 &(struct nv_dma_v0) {
297 .target = NV_DMA_V0_TARGET_VRAM,
298 .access = NV_DMA_V0_ACCESS_RDWR,
299 .start = 0,
300 .limit = device->info.ram_user - 1,
301 }, sizeof(struct nv_dma_v0),
302 &dmac->vram);
303 if (ret)
304 return ret;
305
306 return ret;
307 }
308
309 /******************************************************************************
310 * Output path helpers
311 *****************************************************************************/
312 static void
nv50_outp_dump_caps(struct nouveau_drm * drm,struct nouveau_encoder * outp)313 nv50_outp_dump_caps(struct nouveau_drm *drm,
314 struct nouveau_encoder *outp)
315 {
316 NV_DEBUG(drm, "%s caps: dp_interlace=%d\n",
317 outp->base.base.name, outp->caps.dp_interlace);
318 }
319
320 static int
nv50_outp_atomic_check_view(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,struct drm_display_mode * native_mode)321 nv50_outp_atomic_check_view(struct drm_encoder *encoder,
322 struct drm_crtc_state *crtc_state,
323 struct drm_connector_state *conn_state,
324 struct drm_display_mode *native_mode)
325 {
326 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
327 struct drm_display_mode *mode = &crtc_state->mode;
328 struct drm_connector *connector = conn_state->connector;
329 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
330 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
331
332 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
333 asyc->scaler.full = false;
334 if (!native_mode)
335 return 0;
336
337 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
338 switch (connector->connector_type) {
339 case DRM_MODE_CONNECTOR_LVDS:
340 case DRM_MODE_CONNECTOR_eDP:
341 /* Don't force scaler for EDID modes with
342 * same size as the native one (e.g. different
343 * refresh rate)
344 */
345 if (mode->hdisplay == native_mode->hdisplay &&
346 mode->vdisplay == native_mode->vdisplay &&
347 mode->type & DRM_MODE_TYPE_DRIVER)
348 break;
349 mode = native_mode;
350 asyc->scaler.full = true;
351 break;
352 default:
353 break;
354 }
355 } else {
356 mode = native_mode;
357 }
358
359 if (!drm_mode_equal(adjusted_mode, mode)) {
360 drm_mode_copy(adjusted_mode, mode);
361 crtc_state->mode_changed = true;
362 }
363
364 return 0;
365 }
366
367 static void
nv50_outp_atomic_fix_depth(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state)368 nv50_outp_atomic_fix_depth(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state)
369 {
370 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
371 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
372 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
373 unsigned int max_rate, mode_rate;
374
375 switch (nv_encoder->dcb->type) {
376 case DCB_OUTPUT_DP:
377 max_rate = nv_encoder->dp.link_nr * nv_encoder->dp.link_bw;
378
379 /* we don't support more than 10 anyway */
380 asyh->or.bpc = min_t(u8, asyh->or.bpc, 10);
381
382 /* reduce the bpc until it works out */
383 while (asyh->or.bpc > 6) {
384 mode_rate = DIV_ROUND_UP(mode->clock * asyh->or.bpc * 3, 8);
385 if (mode_rate <= max_rate)
386 break;
387
388 asyh->or.bpc -= 2;
389 }
390 break;
391 default:
392 break;
393 }
394 }
395
396 static int
nv50_outp_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)397 nv50_outp_atomic_check(struct drm_encoder *encoder,
398 struct drm_crtc_state *crtc_state,
399 struct drm_connector_state *conn_state)
400 {
401 struct drm_connector *connector = conn_state->connector;
402 struct nouveau_connector *nv_connector = nouveau_connector(connector);
403 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
404 int ret;
405
406 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
407 nv_connector->native_mode);
408 if (ret)
409 return ret;
410
411 if (crtc_state->mode_changed || crtc_state->connectors_changed)
412 asyh->or.bpc = connector->display_info.bpc;
413
414 /* We might have to reduce the bpc */
415 nv50_outp_atomic_fix_depth(encoder, crtc_state);
416
417 return 0;
418 }
419
420 struct nouveau_connector *
nv50_outp_get_new_connector(struct drm_atomic_state * state,struct nouveau_encoder * outp)421 nv50_outp_get_new_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
422 {
423 struct drm_connector *connector;
424 struct drm_connector_state *connector_state;
425 struct drm_encoder *encoder = to_drm_encoder(outp);
426 int i;
427
428 for_each_new_connector_in_state(state, connector, connector_state, i) {
429 if (connector_state->best_encoder == encoder)
430 return nouveau_connector(connector);
431 }
432
433 return NULL;
434 }
435
436 struct nouveau_connector *
nv50_outp_get_old_connector(struct drm_atomic_state * state,struct nouveau_encoder * outp)437 nv50_outp_get_old_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
438 {
439 struct drm_connector *connector;
440 struct drm_connector_state *connector_state;
441 struct drm_encoder *encoder = to_drm_encoder(outp);
442 int i;
443
444 for_each_old_connector_in_state(state, connector, connector_state, i) {
445 if (connector_state->best_encoder == encoder)
446 return nouveau_connector(connector);
447 }
448
449 return NULL;
450 }
451
452 static struct nouveau_crtc *
nv50_outp_get_new_crtc(const struct drm_atomic_state * state,const struct nouveau_encoder * outp)453 nv50_outp_get_new_crtc(const struct drm_atomic_state *state, const struct nouveau_encoder *outp)
454 {
455 struct drm_crtc *crtc;
456 struct drm_crtc_state *crtc_state;
457 const u32 mask = drm_encoder_mask(&outp->base.base);
458 int i;
459
460 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
461 if (crtc_state->encoder_mask & mask)
462 return nouveau_crtc(crtc);
463 }
464
465 return NULL;
466 }
467
468 /******************************************************************************
469 * DAC
470 *****************************************************************************/
471 static void
nv50_dac_atomic_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)472 nv50_dac_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
473 {
474 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
475 struct nv50_core *core = nv50_disp(encoder->dev)->core;
476 const u32 ctrl = NVDEF(NV507D, DAC_SET_CONTROL, OWNER, NONE);
477
478 core->func->dac->ctrl(core, nv_encoder->outp.or.id, ctrl, NULL);
479 nv_encoder->crtc = NULL;
480 }
481
482 static void
nv50_dac_atomic_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)483 nv50_dac_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
484 {
485 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
486 struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
487 struct nv50_head_atom *asyh =
488 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
489 struct nv50_core *core = nv50_disp(encoder->dev)->core;
490 u32 ctrl = 0;
491
492 switch (nv_crtc->index) {
493 case 0: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD0); break;
494 case 1: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD1); break;
495 case 2: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD2); break;
496 case 3: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD3); break;
497 default:
498 WARN_ON(1);
499 break;
500 }
501
502 ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, PROTOCOL, RGB_CRT);
503
504 if (!nvif_outp_acquired(&nv_encoder->outp))
505 nvif_outp_acquire_dac(&nv_encoder->outp);
506
507 core->func->dac->ctrl(core, nv_encoder->outp.or.id, ctrl, asyh);
508 asyh->or.depth = 0;
509
510 nv_encoder->crtc = &nv_crtc->base;
511 }
512
513 static enum drm_connector_status
nv50_dac_detect(struct drm_encoder * encoder,struct drm_connector * connector)514 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
515 {
516 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
517 u32 loadval;
518 int ret;
519
520 loadval = nouveau_drm(encoder->dev)->vbios.dactestval;
521 if (loadval == 0)
522 loadval = 340;
523
524 ret = nvif_outp_load_detect(&nv_encoder->outp, loadval);
525 if (ret <= 0)
526 return connector_status_disconnected;
527
528 return connector_status_connected;
529 }
530
531 static const struct drm_encoder_helper_funcs
532 nv50_dac_help = {
533 .atomic_check = nv50_outp_atomic_check,
534 .atomic_enable = nv50_dac_atomic_enable,
535 .atomic_disable = nv50_dac_atomic_disable,
536 .detect = nv50_dac_detect
537 };
538
539 static void
nv50_dac_destroy(struct drm_encoder * encoder)540 nv50_dac_destroy(struct drm_encoder *encoder)
541 {
542 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
543
544 nvif_outp_dtor(&nv_encoder->outp);
545
546 drm_encoder_cleanup(encoder);
547 kfree(encoder);
548 }
549
550 static const struct drm_encoder_funcs
551 nv50_dac_func = {
552 .destroy = nv50_dac_destroy,
553 };
554
555 static int
nv50_dac_create(struct nouveau_encoder * nv_encoder)556 nv50_dac_create(struct nouveau_encoder *nv_encoder)
557 {
558 struct drm_connector *connector = &nv_encoder->conn->base;
559 struct nouveau_drm *drm = nouveau_drm(connector->dev);
560 struct nvkm_i2c *i2c = nvxx_i2c(drm);
561 struct nvkm_i2c_bus *bus;
562 struct drm_encoder *encoder;
563 struct dcb_output *dcbe = nv_encoder->dcb;
564 int type = DRM_MODE_ENCODER_DAC;
565
566 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
567 if (bus)
568 nv_encoder->i2c = &bus->i2c;
569
570 encoder = to_drm_encoder(nv_encoder);
571 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
572 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
573 drm_encoder_helper_add(encoder, &nv50_dac_help);
574
575 drm_connector_attach_encoder(connector, encoder);
576 return 0;
577 }
578
579 /*
580 * audio component binding for ELD notification
581 */
582 static void
nv50_audio_component_eld_notify(struct drm_audio_component * acomp,int port,int dev_id)583 nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
584 int dev_id)
585 {
586 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
587 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
588 port, dev_id);
589 }
590
591 static int
nv50_audio_component_get_eld(struct device * kdev,int port,int dev_id,bool * enabled,unsigned char * buf,int max_bytes)592 nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
593 bool *enabled, unsigned char *buf, int max_bytes)
594 {
595 struct nouveau_drm *drm = dev_get_drvdata(kdev);
596 struct drm_encoder *encoder;
597 struct nouveau_encoder *nv_encoder;
598 struct nouveau_crtc *nv_crtc;
599 int ret = 0;
600
601 *enabled = false;
602
603 mutex_lock(&drm->audio.lock);
604
605 drm_for_each_encoder(encoder, drm->dev) {
606 struct nouveau_connector *nv_connector = NULL;
607
608 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST)
609 continue; /* TODO */
610
611 nv_encoder = nouveau_encoder(encoder);
612 nv_connector = nv_encoder->conn;
613 nv_crtc = nouveau_crtc(nv_encoder->crtc);
614
615 if (!nv_crtc || nv_encoder->outp.or.id != port || nv_crtc->index != dev_id)
616 continue;
617
618 *enabled = nv_encoder->audio.enabled;
619 if (*enabled) {
620 ret = drm_eld_size(nv_connector->base.eld);
621 memcpy(buf, nv_connector->base.eld,
622 min(max_bytes, ret));
623 }
624 break;
625 }
626
627 mutex_unlock(&drm->audio.lock);
628
629 return ret;
630 }
631
632 static const struct drm_audio_component_ops nv50_audio_component_ops = {
633 .get_eld = nv50_audio_component_get_eld,
634 };
635
636 static int
nv50_audio_component_bind(struct device * kdev,struct device * hda_kdev,void * data)637 nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
638 void *data)
639 {
640 struct nouveau_drm *drm = dev_get_drvdata(kdev);
641 struct drm_audio_component *acomp = data;
642
643 if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
644 return -ENOMEM;
645
646 drm_modeset_lock_all(drm->dev);
647 acomp->ops = &nv50_audio_component_ops;
648 acomp->dev = kdev;
649 drm->audio.component = acomp;
650 drm_modeset_unlock_all(drm->dev);
651 return 0;
652 }
653
654 static void
nv50_audio_component_unbind(struct device * kdev,struct device * hda_kdev,void * data)655 nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
656 void *data)
657 {
658 struct nouveau_drm *drm = dev_get_drvdata(kdev);
659 struct drm_audio_component *acomp = data;
660
661 drm_modeset_lock_all(drm->dev);
662 drm->audio.component = NULL;
663 acomp->ops = NULL;
664 acomp->dev = NULL;
665 drm_modeset_unlock_all(drm->dev);
666 }
667
668 static const struct component_ops nv50_audio_component_bind_ops = {
669 .bind = nv50_audio_component_bind,
670 .unbind = nv50_audio_component_unbind,
671 };
672
673 static void
nv50_audio_component_init(struct nouveau_drm * drm)674 nv50_audio_component_init(struct nouveau_drm *drm)
675 {
676 if (component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
677 return;
678
679 drm->audio.component_registered = true;
680 mutex_init(&drm->audio.lock);
681 }
682
683 static void
nv50_audio_component_fini(struct nouveau_drm * drm)684 nv50_audio_component_fini(struct nouveau_drm *drm)
685 {
686 if (!drm->audio.component_registered)
687 return;
688
689 component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
690 drm->audio.component_registered = false;
691 mutex_destroy(&drm->audio.lock);
692 }
693
694 /******************************************************************************
695 * Audio
696 *****************************************************************************/
697 static bool
nv50_audio_supported(struct drm_encoder * encoder)698 nv50_audio_supported(struct drm_encoder *encoder)
699 {
700 struct nv50_disp *disp = nv50_disp(encoder->dev);
701
702 if (disp->disp->object.oclass <= GT200_DISP ||
703 disp->disp->object.oclass == GT206_DISP)
704 return false;
705
706 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
707 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
708
709 switch (nv_encoder->dcb->type) {
710 case DCB_OUTPUT_TMDS:
711 case DCB_OUTPUT_DP:
712 break;
713 default:
714 return false;
715 }
716 }
717
718 return true;
719 }
720
721 static void
nv50_audio_disable(struct drm_encoder * encoder,struct nouveau_crtc * nv_crtc)722 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
723 {
724 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
725 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
726 struct nvif_outp *outp = &nv_encoder->outp;
727
728 if (!nv50_audio_supported(encoder))
729 return;
730
731 mutex_lock(&drm->audio.lock);
732 if (nv_encoder->audio.enabled) {
733 nv_encoder->audio.enabled = false;
734 nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, NULL, 0);
735 }
736 mutex_unlock(&drm->audio.lock);
737
738 nv50_audio_component_eld_notify(drm->audio.component, outp->or.id, nv_crtc->index);
739 }
740
741 static void
nv50_audio_enable(struct drm_encoder * encoder,struct nouveau_crtc * nv_crtc,struct nouveau_connector * nv_connector,struct drm_atomic_state * state,struct drm_display_mode * mode)742 nv50_audio_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
743 struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
744 struct drm_display_mode *mode)
745 {
746 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
747 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
748 struct nvif_outp *outp = &nv_encoder->outp;
749
750 if (!nv50_audio_supported(encoder) || !nv_connector->base.display_info.has_audio)
751 return;
752
753 mutex_lock(&drm->audio.lock);
754
755 nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, nv_connector->base.eld,
756 drm_eld_size(nv_connector->base.eld));
757 nv_encoder->audio.enabled = true;
758
759 mutex_unlock(&drm->audio.lock);
760
761 nv50_audio_component_eld_notify(drm->audio.component, outp->or.id, nv_crtc->index);
762 }
763
764 /******************************************************************************
765 * HDMI
766 *****************************************************************************/
767 static void
nv50_hdmi_enable(struct drm_encoder * encoder,struct nouveau_crtc * nv_crtc,struct nouveau_connector * nv_connector,struct drm_atomic_state * state,struct drm_display_mode * mode,bool hda)768 nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
769 struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
770 struct drm_display_mode *mode, bool hda)
771 {
772 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
773 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
774 struct drm_hdmi_info *hdmi = &nv_connector->base.display_info.hdmi;
775 union hdmi_infoframe infoframe = { 0 };
776 const u8 rekey = 56; /* binary driver, and tegra, constant */
777 u32 max_ac_packet;
778 struct {
779 struct nvif_outp_infoframe_v0 infoframe;
780 u8 data[17];
781 } args = { 0 };
782 int ret, size;
783
784 max_ac_packet = mode->htotal - mode->hdisplay;
785 max_ac_packet -= rekey;
786 max_ac_packet -= 18; /* constant from tegra */
787 max_ac_packet /= 32;
788
789 if (nv_encoder->i2c && hdmi->scdc.scrambling.supported) {
790 const bool high_tmds_clock_ratio = mode->clock > 340000;
791 u8 scdc;
792
793 ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &scdc);
794 if (ret < 0) {
795 NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
796 return;
797 }
798
799 scdc &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
800 if (high_tmds_clock_ratio || hdmi->scdc.scrambling.low_rates)
801 scdc |= SCDC_SCRAMBLING_ENABLE;
802 if (high_tmds_clock_ratio)
803 scdc |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
804
805 ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, scdc);
806 if (ret < 0)
807 NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
808 scdc, ret);
809 }
810
811 ret = nvif_outp_hdmi(&nv_encoder->outp, nv_crtc->index, true, max_ac_packet, rekey,
812 mode->clock, hdmi->scdc.supported, hdmi->scdc.scrambling.supported,
813 hdmi->scdc.scrambling.low_rates);
814 if (ret)
815 return;
816
817 /* AVI InfoFrame. */
818 args.infoframe.version = 0;
819 args.infoframe.head = nv_crtc->index;
820
821 if (!drm_hdmi_avi_infoframe_from_display_mode(&infoframe.avi, &nv_connector->base, mode)) {
822 drm_hdmi_avi_infoframe_quant_range(&infoframe.avi, &nv_connector->base, mode,
823 HDMI_QUANTIZATION_RANGE_FULL);
824
825 size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
826 } else {
827 size = 0;
828 }
829
830 nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_AVI, &args.infoframe, size);
831
832 /* Vendor InfoFrame. */
833 memset(&args.data, 0, sizeof(args.data));
834 if (!drm_hdmi_vendor_infoframe_from_display_mode(&infoframe.vendor.hdmi,
835 &nv_connector->base, mode))
836 size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
837 else
838 size = 0;
839
840 nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_VSI, &args.infoframe, size);
841
842 nv_encoder->hdmi.enabled = true;
843 }
844
845 /******************************************************************************
846 * MST
847 *****************************************************************************/
848 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
849 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
850 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
851
852 struct nv50_mstc {
853 struct nv50_mstm *mstm;
854 struct drm_dp_mst_port *port;
855 struct drm_connector connector;
856
857 struct drm_display_mode *native;
858 struct edid *edid;
859 };
860
861 struct nv50_msto {
862 struct drm_encoder encoder;
863
864 /* head is statically assigned on msto creation */
865 struct nv50_head *head;
866 struct nv50_mstc *mstc;
867 bool disabled;
868 bool enabled;
869
870 u32 display_id;
871 };
872
nv50_real_outp(struct drm_encoder * encoder)873 struct nouveau_encoder *nv50_real_outp(struct drm_encoder *encoder)
874 {
875 struct nv50_msto *msto;
876
877 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
878 return nouveau_encoder(encoder);
879
880 msto = nv50_msto(encoder);
881 if (!msto->mstc)
882 return NULL;
883 return msto->mstc->mstm->outp;
884 }
885
886 static void
nv50_msto_cleanup(struct drm_atomic_state * state,struct drm_dp_mst_topology_state * new_mst_state,struct drm_dp_mst_topology_mgr * mgr,struct nv50_msto * msto)887 nv50_msto_cleanup(struct drm_atomic_state *state,
888 struct drm_dp_mst_topology_state *new_mst_state,
889 struct drm_dp_mst_topology_mgr *mgr,
890 struct nv50_msto *msto)
891 {
892 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
893 struct drm_dp_mst_atomic_payload *new_payload =
894 drm_atomic_get_mst_payload_state(new_mst_state, msto->mstc->port);
895 struct drm_dp_mst_topology_state *old_mst_state =
896 drm_atomic_get_old_mst_topology_state(state, mgr);
897 const struct drm_dp_mst_atomic_payload *old_payload =
898 drm_atomic_get_mst_payload_state(old_mst_state, msto->mstc->port);
899 struct nv50_mstc *mstc = msto->mstc;
900 struct nv50_mstm *mstm = mstc->mstm;
901
902 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
903
904 if (msto->disabled) {
905 if (msto->head->func->display_id) {
906 nvif_outp_dp_mst_id_put(&mstm->outp->outp, msto->display_id);
907 msto->display_id = 0;
908 }
909
910 msto->mstc = NULL;
911 msto->disabled = false;
912 drm_dp_remove_payload_part2(mgr, new_mst_state, old_payload, new_payload);
913 } else if (msto->enabled) {
914 drm_dp_add_payload_part2(mgr, new_payload);
915 msto->enabled = false;
916 }
917 }
918
919 static void
nv50_msto_prepare(struct drm_atomic_state * state,struct drm_dp_mst_topology_state * mst_state,struct drm_dp_mst_topology_mgr * mgr,struct nv50_msto * msto)920 nv50_msto_prepare(struct drm_atomic_state *state,
921 struct drm_dp_mst_topology_state *mst_state,
922 struct drm_dp_mst_topology_mgr *mgr,
923 struct nv50_msto *msto)
924 {
925 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
926 struct nv50_mstc *mstc = msto->mstc;
927 struct nv50_mstm *mstm = mstc->mstm;
928 struct drm_dp_mst_atomic_payload *payload;
929 int ret = 0;
930
931 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
932
933 payload = drm_atomic_get_mst_payload_state(mst_state, mstc->port);
934
935 if (msto->disabled) {
936 drm_dp_remove_payload_part1(mgr, mst_state, payload);
937 nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index, 0, 0, 0, 0);
938 ret = 1;
939 } else {
940 if (msto->enabled)
941 ret = drm_dp_add_payload_part1(mgr, mst_state, payload);
942 }
943
944 if (ret == 0) {
945 nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index,
946 payload->vc_start_slot, payload->time_slots,
947 payload->pbn,
948 payload->time_slots * dfixed_trunc(mst_state->pbn_div));
949 } else {
950 nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index, 0, 0, 0, 0);
951 }
952 }
953
954 static int
nv50_msto_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)955 nv50_msto_atomic_check(struct drm_encoder *encoder,
956 struct drm_crtc_state *crtc_state,
957 struct drm_connector_state *conn_state)
958 {
959 struct drm_atomic_state *state = crtc_state->state;
960 struct drm_connector *connector = conn_state->connector;
961 struct drm_dp_mst_topology_state *mst_state;
962 struct nv50_mstc *mstc = nv50_mstc(connector);
963 struct nv50_mstm *mstm = mstc->mstm;
964 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
965 int slots;
966 int ret;
967
968 ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
969 mstc->native);
970 if (ret)
971 return ret;
972
973 if (!drm_atomic_crtc_needs_modeset(crtc_state))
974 return 0;
975
976 /*
977 * When restoring duplicated states, we need to make sure that the bw
978 * remains the same and avoid recalculating it, as the connector's bpc
979 * may have changed after the state was duplicated
980 */
981 if (!state->duplicated) {
982 const int clock = crtc_state->adjusted_mode.clock;
983
984 asyh->or.bpc = connector->display_info.bpc;
985 asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3 << 4);
986 }
987
988 mst_state = drm_atomic_get_mst_topology_state(state, &mstm->mgr);
989 if (IS_ERR(mst_state))
990 return PTR_ERR(mst_state);
991
992 if (!mst_state->pbn_div.full) {
993 struct nouveau_encoder *outp = mstc->mstm->outp;
994
995 mst_state->pbn_div = drm_dp_get_vc_payload_bw(outp->dp.link_bw, outp->dp.link_nr);
996 }
997
998 slots = drm_dp_atomic_find_time_slots(state, &mstm->mgr, mstc->port, asyh->dp.pbn);
999 if (slots < 0)
1000 return slots;
1001
1002 asyh->dp.tu = slots;
1003
1004 return 0;
1005 }
1006
1007 static u8
nv50_dp_bpc_to_depth(unsigned int bpc)1008 nv50_dp_bpc_to_depth(unsigned int bpc)
1009 {
1010 switch (bpc) {
1011 case 6: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444;
1012 case 8: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444;
1013 case 10:
1014 default: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444;
1015 }
1016 }
1017
1018 static void
nv50_msto_atomic_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)1019 nv50_msto_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1020 {
1021 struct nv50_msto *msto = nv50_msto(encoder);
1022 struct nv50_head *head = msto->head;
1023 struct nv50_head_atom *asyh =
1024 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &head->base.base));
1025 struct nv50_mstc *mstc = NULL;
1026 struct nv50_mstm *mstm = NULL;
1027 struct drm_connector *connector;
1028 struct drm_connector_list_iter conn_iter;
1029 u8 proto;
1030
1031 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
1032 drm_for_each_connector_iter(connector, &conn_iter) {
1033 if (connector->state->best_encoder == &msto->encoder) {
1034 mstc = nv50_mstc(connector);
1035 mstm = mstc->mstm;
1036 break;
1037 }
1038 }
1039 drm_connector_list_iter_end(&conn_iter);
1040
1041 if (WARN_ON(!mstc))
1042 return;
1043
1044 if (!mstm->links++) {
1045 nvif_outp_acquire_sor(&mstm->outp->outp, false /*TODO: MST audio... */);
1046 nouveau_dp_train(mstm->outp, true, 0, 0);
1047 }
1048
1049 if (head->func->display_id) {
1050 if (!WARN_ON(nvif_outp_dp_mst_id_get(&mstm->outp->outp, &msto->display_id)))
1051 head->func->display_id(head, msto->display_id);
1052 }
1053
1054 if (mstm->outp->outp.or.link & 1)
1055 proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1056 else
1057 proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1058
1059 mstm->outp->update(mstm->outp, head->base.index, asyh, proto,
1060 nv50_dp_bpc_to_depth(asyh->or.bpc));
1061
1062 msto->mstc = mstc;
1063 msto->enabled = true;
1064 mstm->modified = true;
1065 }
1066
1067 static void
nv50_msto_atomic_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)1068 nv50_msto_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1069 {
1070 struct nv50_msto *msto = nv50_msto(encoder);
1071 struct nv50_mstc *mstc = msto->mstc;
1072 struct nv50_mstm *mstm = mstc->mstm;
1073
1074 if (msto->head->func->display_id)
1075 msto->head->func->display_id(msto->head, 0);
1076
1077 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
1078 mstm->modified = true;
1079 if (!--mstm->links)
1080 mstm->disabled = true;
1081 msto->disabled = true;
1082 }
1083
1084 static const struct drm_encoder_helper_funcs
1085 nv50_msto_help = {
1086 .atomic_disable = nv50_msto_atomic_disable,
1087 .atomic_enable = nv50_msto_atomic_enable,
1088 .atomic_check = nv50_msto_atomic_check,
1089 };
1090
1091 static void
nv50_msto_destroy(struct drm_encoder * encoder)1092 nv50_msto_destroy(struct drm_encoder *encoder)
1093 {
1094 struct nv50_msto *msto = nv50_msto(encoder);
1095 drm_encoder_cleanup(&msto->encoder);
1096 kfree(msto);
1097 }
1098
1099 static const struct drm_encoder_funcs
1100 nv50_msto = {
1101 .destroy = nv50_msto_destroy,
1102 };
1103
1104 static struct nv50_msto *
nv50_msto_new(struct drm_device * dev,struct nv50_head * head,int id)1105 nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1106 {
1107 struct nv50_msto *msto;
1108 int ret;
1109
1110 msto = kzalloc(sizeof(*msto), GFP_KERNEL);
1111 if (!msto)
1112 return ERR_PTR(-ENOMEM);
1113
1114 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1115 DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1116 if (ret) {
1117 kfree(msto);
1118 return ERR_PTR(ret);
1119 }
1120
1121 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1122 msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
1123 msto->head = head;
1124 return msto;
1125 }
1126
1127 static struct drm_encoder *
nv50_mstc_atomic_best_encoder(struct drm_connector * connector,struct drm_atomic_state * state)1128 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1129 struct drm_atomic_state *state)
1130 {
1131 struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
1132 connector);
1133 struct nv50_mstc *mstc = nv50_mstc(connector);
1134 struct drm_crtc *crtc = connector_state->crtc;
1135
1136 if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1137 return NULL;
1138
1139 return &nv50_head(crtc)->msto->encoder;
1140 }
1141
1142 static enum drm_mode_status
nv50_mstc_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)1143 nv50_mstc_mode_valid(struct drm_connector *connector,
1144 struct drm_display_mode *mode)
1145 {
1146 struct nv50_mstc *mstc = nv50_mstc(connector);
1147 struct nouveau_encoder *outp = mstc->mstm->outp;
1148
1149 /* TODO: calculate the PBN from the dotclock and validate against the
1150 * MSTB's max possible PBN
1151 */
1152
1153 return nv50_dp_mode_valid(outp, mode, NULL);
1154 }
1155
1156 static int
nv50_mstc_get_modes(struct drm_connector * connector)1157 nv50_mstc_get_modes(struct drm_connector *connector)
1158 {
1159 struct nv50_mstc *mstc = nv50_mstc(connector);
1160 int ret = 0;
1161
1162 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1163 drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1164 if (mstc->edid)
1165 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
1166
1167 /*
1168 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
1169 * to 8 to save bandwidth on the topology. In the future, we'll want
1170 * to properly fix this by dynamically selecting the highest possible
1171 * bpc that would fit in the topology
1172 */
1173 if (connector->display_info.bpc)
1174 connector->display_info.bpc =
1175 clamp(connector->display_info.bpc, 6U, 8U);
1176 else
1177 connector->display_info.bpc = 8;
1178
1179 if (mstc->native)
1180 drm_mode_destroy(mstc->connector.dev, mstc->native);
1181 mstc->native = nouveau_conn_native_mode(&mstc->connector);
1182 return ret;
1183 }
1184
1185 static int
nv50_mstc_atomic_check(struct drm_connector * connector,struct drm_atomic_state * state)1186 nv50_mstc_atomic_check(struct drm_connector *connector,
1187 struct drm_atomic_state *state)
1188 {
1189 struct nv50_mstc *mstc = nv50_mstc(connector);
1190 struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;
1191
1192 return drm_dp_atomic_release_time_slots(state, mgr, mstc->port);
1193 }
1194
1195 static int
nv50_mstc_detect(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)1196 nv50_mstc_detect(struct drm_connector *connector,
1197 struct drm_modeset_acquire_ctx *ctx, bool force)
1198 {
1199 struct nv50_mstc *mstc = nv50_mstc(connector);
1200 int ret;
1201
1202 if (drm_connector_is_unregistered(connector))
1203 return connector_status_disconnected;
1204
1205 ret = pm_runtime_get_sync(connector->dev->dev);
1206 if (ret < 0 && ret != -EACCES) {
1207 pm_runtime_put_autosuspend(connector->dev->dev);
1208 return connector_status_disconnected;
1209 }
1210
1211 ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
1212 mstc->port);
1213 if (ret != connector_status_connected)
1214 goto out;
1215
1216 out:
1217 pm_runtime_mark_last_busy(connector->dev->dev);
1218 pm_runtime_put_autosuspend(connector->dev->dev);
1219 return ret;
1220 }
1221
1222 static const struct drm_connector_helper_funcs
1223 nv50_mstc_help = {
1224 .get_modes = nv50_mstc_get_modes,
1225 .mode_valid = nv50_mstc_mode_valid,
1226 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
1227 .atomic_check = nv50_mstc_atomic_check,
1228 .detect_ctx = nv50_mstc_detect,
1229 };
1230
1231 static void
nv50_mstc_destroy(struct drm_connector * connector)1232 nv50_mstc_destroy(struct drm_connector *connector)
1233 {
1234 struct nv50_mstc *mstc = nv50_mstc(connector);
1235
1236 drm_connector_cleanup(&mstc->connector);
1237 drm_dp_mst_put_port_malloc(mstc->port);
1238
1239 kfree(mstc);
1240 }
1241
1242 static const struct drm_connector_funcs
1243 nv50_mstc = {
1244 .reset = nouveau_conn_reset,
1245 .fill_modes = drm_helper_probe_single_connector_modes,
1246 .destroy = nv50_mstc_destroy,
1247 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1248 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1249 .atomic_set_property = nouveau_conn_atomic_set_property,
1250 .atomic_get_property = nouveau_conn_atomic_get_property,
1251 };
1252
1253 static int
nv50_mstc_new(struct nv50_mstm * mstm,struct drm_dp_mst_port * port,const char * path,struct nv50_mstc ** pmstc)1254 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
1255 const char *path, struct nv50_mstc **pmstc)
1256 {
1257 struct drm_device *dev = mstm->outp->base.base.dev;
1258 struct drm_crtc *crtc;
1259 struct nv50_mstc *mstc;
1260 int ret;
1261
1262 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
1263 return -ENOMEM;
1264 mstc->mstm = mstm;
1265 mstc->port = port;
1266
1267 ret = drm_connector_dynamic_init(dev, &mstc->connector, &nv50_mstc,
1268 DRM_MODE_CONNECTOR_DisplayPort, NULL);
1269 if (ret) {
1270 kfree(*pmstc);
1271 *pmstc = NULL;
1272 return ret;
1273 }
1274
1275 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
1276
1277 mstc->connector.funcs->reset(&mstc->connector);
1278 nouveau_conn_attach_properties(&mstc->connector);
1279
1280 drm_for_each_crtc(crtc, dev) {
1281 if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1282 continue;
1283
1284 drm_connector_attach_encoder(&mstc->connector,
1285 &nv50_head(crtc)->msto->encoder);
1286 }
1287
1288 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
1289 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1290 drm_connector_set_path_property(&mstc->connector, path);
1291 drm_dp_mst_get_port_malloc(port);
1292 return 0;
1293 }
1294
1295 static void
nv50_mstm_cleanup(struct drm_atomic_state * state,struct drm_dp_mst_topology_state * mst_state,struct nv50_mstm * mstm)1296 nv50_mstm_cleanup(struct drm_atomic_state *state,
1297 struct drm_dp_mst_topology_state *mst_state,
1298 struct nv50_mstm *mstm)
1299 {
1300 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1301 struct drm_encoder *encoder;
1302
1303 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1304 drm_dp_check_act_status(&mstm->mgr);
1305
1306 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1307 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1308 struct nv50_msto *msto = nv50_msto(encoder);
1309 struct nv50_mstc *mstc = msto->mstc;
1310 if (mstc && mstc->mstm == mstm)
1311 nv50_msto_cleanup(state, mst_state, &mstm->mgr, msto);
1312 }
1313 }
1314
1315 if (mstm->disabled) {
1316 nouveau_dp_power_down(mstm->outp);
1317 nvif_outp_release(&mstm->outp->outp);
1318 mstm->disabled = false;
1319 }
1320
1321 mstm->modified = false;
1322 }
1323
1324 static void
nv50_mstm_prepare(struct drm_atomic_state * state,struct drm_dp_mst_topology_state * mst_state,struct nv50_mstm * mstm)1325 nv50_mstm_prepare(struct drm_atomic_state *state,
1326 struct drm_dp_mst_topology_state *mst_state,
1327 struct nv50_mstm *mstm)
1328 {
1329 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1330 struct drm_encoder *encoder;
1331
1332 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
1333
1334 /* Disable payloads first */
1335 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1336 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1337 struct nv50_msto *msto = nv50_msto(encoder);
1338 struct nv50_mstc *mstc = msto->mstc;
1339 if (mstc && mstc->mstm == mstm && msto->disabled)
1340 nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1341 }
1342 }
1343
1344 /* Add payloads for new heads, while also updating the start slots of any unmodified (but
1345 * active) heads that may have had their VC slots shifted left after the previous step
1346 */
1347 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1348 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1349 struct nv50_msto *msto = nv50_msto(encoder);
1350 struct nv50_mstc *mstc = msto->mstc;
1351 if (mstc && mstc->mstm == mstm && !msto->disabled)
1352 nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1353 }
1354 }
1355 }
1356
1357 static struct drm_connector *
nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,const char * path)1358 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1359 struct drm_dp_mst_port *port, const char *path)
1360 {
1361 struct nv50_mstm *mstm = nv50_mstm(mgr);
1362 struct nv50_mstc *mstc;
1363 int ret;
1364
1365 ret = nv50_mstc_new(mstm, port, path, &mstc);
1366 if (ret)
1367 return NULL;
1368
1369 return &mstc->connector;
1370 }
1371
1372 static const struct drm_dp_mst_topology_cbs
1373 nv50_mstm = {
1374 .add_connector = nv50_mstm_add_connector,
1375 };
1376
1377 bool
nv50_mstm_service(struct nouveau_drm * drm,struct nouveau_connector * nv_connector,struct nv50_mstm * mstm)1378 nv50_mstm_service(struct nouveau_drm *drm,
1379 struct nouveau_connector *nv_connector,
1380 struct nv50_mstm *mstm)
1381 {
1382 struct drm_dp_aux *aux = &nv_connector->aux;
1383 bool handled = true, ret = true;
1384 int rc;
1385 u8 esi[8] = {};
1386
1387 while (handled) {
1388 u8 ack[8] = {};
1389
1390 rc = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1391 if (rc != 8) {
1392 ret = false;
1393 break;
1394 }
1395
1396 drm_dp_mst_hpd_irq_handle_event(&mstm->mgr, esi, ack, &handled);
1397 if (!handled)
1398 break;
1399
1400 rc = drm_dp_dpcd_writeb(aux, DP_SINK_COUNT_ESI + 1, ack[1]);
1401
1402 if (rc != 1) {
1403 ret = false;
1404 break;
1405 }
1406
1407 drm_dp_mst_hpd_irq_send_new_request(&mstm->mgr);
1408 }
1409
1410 if (!ret)
1411 NV_DEBUG(drm, "Failed to handle ESI on %s: %d\n",
1412 nv_connector->base.name, rc);
1413
1414 return ret;
1415 }
1416
1417 void
nv50_mstm_remove(struct nv50_mstm * mstm)1418 nv50_mstm_remove(struct nv50_mstm *mstm)
1419 {
1420 mstm->is_mst = false;
1421 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1422 }
1423
1424 int
nv50_mstm_detect(struct nouveau_encoder * outp)1425 nv50_mstm_detect(struct nouveau_encoder *outp)
1426 {
1427 struct nv50_mstm *mstm = outp->dp.mstm;
1428 struct drm_dp_aux *aux;
1429 int ret;
1430
1431 if (!mstm || !mstm->can_mst)
1432 return 0;
1433
1434 aux = mstm->mgr.aux;
1435
1436 /* Clear any leftover MST state we didn't set ourselves by first
1437 * disabling MST if it was already enabled
1438 */
1439 ret = drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1440 if (ret < 0)
1441 return ret;
1442
1443 /* And start enabling */
1444 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, true);
1445 if (ret)
1446 return ret;
1447
1448 mstm->is_mst = true;
1449 return 1;
1450 }
1451
1452 static void
nv50_mstm_fini(struct nouveau_encoder * outp)1453 nv50_mstm_fini(struct nouveau_encoder *outp)
1454 {
1455 struct nv50_mstm *mstm = outp->dp.mstm;
1456
1457 if (!mstm)
1458 return;
1459
1460 /* Don't change the MST state of this connector until we've finished
1461 * resuming, since we can't safely grab hpd_irq_lock in our resume
1462 * path to protect mstm->is_mst without potentially deadlocking
1463 */
1464 mutex_lock(&outp->dp.hpd_irq_lock);
1465 mstm->suspended = true;
1466 mutex_unlock(&outp->dp.hpd_irq_lock);
1467
1468 if (mstm->is_mst)
1469 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1470 }
1471
1472 static void
nv50_mstm_init(struct nouveau_encoder * outp,bool runtime)1473 nv50_mstm_init(struct nouveau_encoder *outp, bool runtime)
1474 {
1475 struct nv50_mstm *mstm = outp->dp.mstm;
1476 int ret = 0;
1477
1478 if (!mstm)
1479 return;
1480
1481 if (mstm->is_mst) {
1482 ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
1483 if (ret == -1)
1484 nv50_mstm_remove(mstm);
1485 }
1486
1487 mutex_lock(&outp->dp.hpd_irq_lock);
1488 mstm->suspended = false;
1489 mutex_unlock(&outp->dp.hpd_irq_lock);
1490
1491 if (ret == -1)
1492 drm_kms_helper_hotplug_event(mstm->mgr.dev);
1493 }
1494
1495 static void
nv50_mstm_del(struct nv50_mstm ** pmstm)1496 nv50_mstm_del(struct nv50_mstm **pmstm)
1497 {
1498 struct nv50_mstm *mstm = *pmstm;
1499 if (mstm) {
1500 drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1501 kfree(*pmstm);
1502 *pmstm = NULL;
1503 }
1504 }
1505
1506 static int
nv50_mstm_new(struct nouveau_encoder * outp,struct drm_dp_aux * aux,int aux_max,int conn_base_id,struct nv50_mstm ** pmstm)1507 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1508 int conn_base_id, struct nv50_mstm **pmstm)
1509 {
1510 const int max_payloads = hweight8(outp->dcb->heads);
1511 struct drm_device *dev = outp->base.base.dev;
1512 struct nv50_mstm *mstm;
1513 int ret;
1514
1515 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1516 return -ENOMEM;
1517 mstm->outp = outp;
1518 mstm->mgr.cbs = &nv50_mstm;
1519
1520 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1521 max_payloads, conn_base_id);
1522 if (ret)
1523 return ret;
1524
1525 return 0;
1526 }
1527
1528 /******************************************************************************
1529 * SOR
1530 *****************************************************************************/
1531 static void
nv50_sor_update(struct nouveau_encoder * nv_encoder,u8 head,struct nv50_head_atom * asyh,u8 proto,u8 depth)1532 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1533 struct nv50_head_atom *asyh, u8 proto, u8 depth)
1534 {
1535 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1536 struct nv50_core *core = disp->core;
1537
1538 if (!asyh) {
1539 nv_encoder->ctrl &= ~BIT(head);
1540 if (NVDEF_TEST(nv_encoder->ctrl, NV507D, SOR_SET_CONTROL, OWNER, ==, NONE))
1541 nv_encoder->ctrl = 0;
1542 } else {
1543 nv_encoder->ctrl |= NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto);
1544 nv_encoder->ctrl |= BIT(head);
1545 asyh->or.depth = depth;
1546 }
1547
1548 core->func->sor->ctrl(core, nv_encoder->outp.or.id, nv_encoder->ctrl, asyh);
1549 }
1550
1551 /* TODO: Should we extend this to PWM-only backlights?
1552 * As well, should we add a DRM helper for waiting for the backlight to acknowledge
1553 * the panel backlight has been shut off? Intel doesn't seem to do this, and uses a
1554 * fixed time delay from the vbios…
1555 */
1556 static void
nv50_sor_atomic_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)1557 nv50_sor_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1558 {
1559 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1560 struct nv50_head *head = nv50_head(nv_encoder->crtc);
1561 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1562 struct nouveau_connector *nv_connector = nv50_outp_get_old_connector(state, nv_encoder);
1563 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
1564 struct nouveau_backlight *backlight = nv_connector->backlight;
1565 struct drm_dp_aux *aux = &nv_connector->aux;
1566 int ret;
1567
1568 if (backlight && backlight->uses_dpcd) {
1569 ret = drm_edp_backlight_disable(aux, &backlight->edp_info);
1570 if (ret < 0)
1571 NV_ERROR(drm, "Failed to disable backlight on [CONNECTOR:%d:%s]: %d\n",
1572 nv_connector->base.base.id, nv_connector->base.name, ret);
1573 }
1574 #endif
1575
1576 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS && nv_encoder->hdmi.enabled) {
1577 nvif_outp_hdmi(&nv_encoder->outp, head->base.index,
1578 false, 0, 0, 0, false, false, false);
1579 nv_encoder->hdmi.enabled = false;
1580 }
1581
1582 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1583 nouveau_dp_power_down(nv_encoder);
1584
1585 if (head->func->display_id)
1586 head->func->display_id(head, 0);
1587
1588 nv_encoder->update(nv_encoder, head->base.index, NULL, 0, 0);
1589 nv50_audio_disable(encoder, &head->base);
1590 nv_encoder->crtc = NULL;
1591 }
1592
1593 // common/inc/displayport/displayport.h
1594 #define DP_CONFIG_WATERMARK_ADJUST 2
1595 #define DP_CONFIG_WATERMARK_LIMIT 20
1596 #define DP_CONFIG_INCREASED_WATERMARK_ADJUST 8
1597 #define DP_CONFIG_INCREASED_WATERMARK_LIMIT 22
1598
1599 static bool
nv50_sor_dp_watermark_sst(struct nouveau_encoder * outp,struct nv50_head * head,struct nv50_head_atom * asyh)1600 nv50_sor_dp_watermark_sst(struct nouveau_encoder *outp,
1601 struct nv50_head *head, struct nv50_head_atom *asyh)
1602 {
1603 bool enhancedFraming = outp->dp.dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP;
1604 u64 minRate = outp->dp.link_bw * 1000;
1605 unsigned tuSize = 64;
1606 unsigned waterMark;
1607 unsigned hBlankSym;
1608 unsigned vBlankSym;
1609 unsigned watermarkAdjust = DP_CONFIG_WATERMARK_ADJUST;
1610 unsigned watermarkMinimum = DP_CONFIG_WATERMARK_LIMIT;
1611 // depth is multiplied by 16 in case of DSC enable
1612 s32 hblank_symbols;
1613 // number of link clocks per line.
1614 int vblank_symbols = 0;
1615 bool bEnableDsc = false;
1616 unsigned surfaceWidth = asyh->mode.h.blanks - asyh->mode.h.blanke;
1617 unsigned rasterWidth = asyh->mode.h.active;
1618 unsigned depth = asyh->or.bpc * 3;
1619 unsigned DSC_FACTOR = bEnableDsc ? 16 : 1;
1620 u64 pixelClockHz = asyh->mode.clock * 1000;
1621 u64 PrecisionFactor = 100000, ratioF, watermarkF;
1622 u32 numLanesPerLink = outp->dp.link_nr;
1623 u32 numSymbolsPerLine;
1624 u32 BlankingBits;
1625 u32 surfaceWidthPerLink;
1626 u32 PixelSteeringBits;
1627 u64 NumBlankingLinkClocks;
1628 u32 MinHBlank;
1629
1630 if (outp->outp.info.dp.increased_wm) {
1631 watermarkAdjust = DP_CONFIG_INCREASED_WATERMARK_ADJUST;
1632 watermarkMinimum = DP_CONFIG_INCREASED_WATERMARK_LIMIT;
1633 }
1634
1635 if ((pixelClockHz * depth) >= (8 * minRate * outp->dp.link_nr * DSC_FACTOR))
1636 {
1637 return false;
1638 }
1639
1640 //
1641 // For DSC, if (pclk * bpp) < (1/64 * orclk * 8 * lanes) then some TU may end up with
1642 // 0 active symbols. This may cause HW hang. Bug 200379426
1643 //
1644 if ((bEnableDsc) &&
1645 ((pixelClockHz * depth) < div_u64(8 * minRate * outp->dp.link_nr * DSC_FACTOR, 64)))
1646 {
1647 return false;
1648 }
1649
1650 //
1651 // Perform the SST calculation.
1652 // For auto mode the watermark calculation does not need to track accumulated error the
1653 // formulas for manual mode will not work. So below calculation was extracted from the DTB.
1654 //
1655 ratioF = div_u64((u64)pixelClockHz * depth * PrecisionFactor, DSC_FACTOR);
1656
1657 ratioF = div_u64(ratioF, 8 * (u64) minRate * outp->dp.link_nr);
1658
1659 if (PrecisionFactor < ratioF) // Assert if we will end up with a negative number in below
1660 return false;
1661
1662 watermarkF = div_u64(ratioF * tuSize * (PrecisionFactor - ratioF), PrecisionFactor);
1663 waterMark = (unsigned)(watermarkAdjust + (div_u64(2 * div_u64(depth * PrecisionFactor, 8 * numLanesPerLink * DSC_FACTOR) + watermarkF, PrecisionFactor)));
1664
1665 //
1666 // Bounds check the watermark
1667 //
1668 numSymbolsPerLine = div_u64(surfaceWidth * depth, 8 * outp->dp.link_nr * DSC_FACTOR);
1669
1670 if (WARN_ON(waterMark > 39 || waterMark > numSymbolsPerLine))
1671 return false;
1672
1673 //
1674 // Clamp the low side
1675 //
1676 if (waterMark < watermarkMinimum)
1677 waterMark = watermarkMinimum;
1678
1679 //Bits to send BS/BE/Extra symbols due to pixel padding
1680 //Also accounts for enhanced framing.
1681 BlankingBits = 3*8*numLanesPerLink + (enhancedFraming ? 3*8*numLanesPerLink : 0);
1682
1683 //VBID/MVID/MAUD sent 4 times all the time
1684 BlankingBits += 3*8*4;
1685
1686 surfaceWidthPerLink = surfaceWidth;
1687
1688 //Extra bits sent due to pixel steering
1689 u32 remain;
1690 div_u64_rem(surfaceWidthPerLink, numLanesPerLink, &remain);
1691 PixelSteeringBits = remain ? div_u64((numLanesPerLink - remain) * depth, DSC_FACTOR) : 0;
1692
1693 BlankingBits += PixelSteeringBits;
1694 NumBlankingLinkClocks = div_u64((u64)BlankingBits * PrecisionFactor, (8 * numLanesPerLink));
1695 MinHBlank = (u32)(div_u64(div_u64(NumBlankingLinkClocks * pixelClockHz, minRate), PrecisionFactor));
1696 MinHBlank += 12;
1697
1698 if (WARN_ON(MinHBlank > rasterWidth - surfaceWidth))
1699 return false;
1700
1701 // Bug 702290 - Active Width should be greater than 60
1702 if (WARN_ON(surfaceWidth <= 60))
1703 return false;
1704
1705
1706 hblank_symbols = (s32)(div_u64((u64)(rasterWidth - surfaceWidth - MinHBlank) * minRate, pixelClockHz));
1707
1708 //reduce HBlank Symbols to account for secondary data packet
1709 hblank_symbols -= 1; //Stuffer latency to send BS
1710 hblank_symbols -= 3; //SPKT latency to send data to stuffer
1711
1712 hblank_symbols -= numLanesPerLink == 1 ? 9 : numLanesPerLink == 2 ? 6 : 3;
1713
1714 hBlankSym = (hblank_symbols < 0) ? 0 : hblank_symbols;
1715
1716 // Refer to dev_disp.ref for more information.
1717 // # symbols/vblank = ((SetRasterBlankEnd.X + SetRasterSize.Width - SetRasterBlankStart.X - 40) * link_clk / pclk) - Y - 1;
1718 // where Y = (# lanes == 4) 12 : (# lanes == 2) ? 21 : 39
1719 if (surfaceWidth < 40)
1720 {
1721 vblank_symbols = 0;
1722 }
1723 else
1724 {
1725 vblank_symbols = (s32)((div_u64((u64)(surfaceWidth - 40) * minRate, pixelClockHz))) - 1;
1726
1727 vblank_symbols -= numLanesPerLink == 1 ? 39 : numLanesPerLink == 2 ? 21 : 12;
1728 }
1729
1730 vBlankSym = (vblank_symbols < 0) ? 0 : vblank_symbols;
1731
1732 return nvif_outp_dp_sst(&outp->outp, head->base.index, waterMark, hBlankSym, vBlankSym);
1733 }
1734
1735 static void
nv50_sor_atomic_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)1736 nv50_sor_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1737 {
1738 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1739 struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1740 struct nv50_head_atom *asyh =
1741 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1742 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1743 struct nv50_disp *disp = nv50_disp(encoder->dev);
1744 struct nv50_head *head = nv50_head(&nv_crtc->base);
1745 struct nvif_outp *outp = &nv_encoder->outp;
1746 struct drm_device *dev = encoder->dev;
1747 struct nouveau_drm *drm = nouveau_drm(dev);
1748 struct nouveau_connector *nv_connector;
1749 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1750 struct nouveau_backlight *backlight;
1751 #endif
1752 struct nvbios *bios = &drm->vbios;
1753 bool lvds_dual = false, lvds_8bpc = false, hda = false;
1754 u8 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_CUSTOM;
1755 u8 depth = NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT;
1756
1757 nv_connector = nv50_outp_get_new_connector(state, nv_encoder);
1758 nv_encoder->crtc = &nv_crtc->base;
1759
1760 if ((disp->disp->object.oclass == GT214_DISP ||
1761 disp->disp->object.oclass >= GF110_DISP) &&
1762 nv_encoder->dcb->type != DCB_OUTPUT_LVDS &&
1763 nv_connector->base.display_info.has_audio)
1764 hda = true;
1765
1766 if (!nvif_outp_acquired(outp))
1767 nvif_outp_acquire_sor(outp, hda);
1768
1769 switch (nv_encoder->dcb->type) {
1770 case DCB_OUTPUT_TMDS:
1771 if (disp->disp->object.oclass != NV50_DISP &&
1772 nv_connector->base.display_info.is_hdmi)
1773 nv50_hdmi_enable(encoder, nv_crtc, nv_connector, state, mode, hda);
1774
1775 if (nv_encoder->outp.or.link & 1) {
1776 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_A;
1777 /* Only enable dual-link if:
1778 * - Need to (i.e. rate > 165MHz)
1779 * - DCB says we can
1780 * - Not an HDMI monitor, since there's no dual-link
1781 * on HDMI.
1782 */
1783 if (mode->clock >= 165000 &&
1784 nv_encoder->dcb->duallink_possible &&
1785 !nv_connector->base.display_info.is_hdmi)
1786 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_DUAL_TMDS;
1787 } else {
1788 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_B;
1789 }
1790 break;
1791 case DCB_OUTPUT_LVDS:
1792 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_LVDS_CUSTOM;
1793
1794 if (bios->fp_no_ddc) {
1795 lvds_dual = bios->fp.dual_link;
1796 lvds_8bpc = bios->fp.if_is_24bit;
1797 } else {
1798 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1799 if (((u8 *)nv_connector->edid)[121] == 2)
1800 lvds_dual = true;
1801 } else
1802 if (mode->clock >= bios->fp.duallink_transition_clk) {
1803 lvds_dual = true;
1804 }
1805
1806 if (lvds_dual) {
1807 if (bios->fp.strapless_is_24bit & 2)
1808 lvds_8bpc = true;
1809 } else {
1810 if (bios->fp.strapless_is_24bit & 1)
1811 lvds_8bpc = true;
1812 }
1813
1814 if (asyh->or.bpc == 8)
1815 lvds_8bpc = true;
1816 }
1817
1818 nvif_outp_lvds(&nv_encoder->outp, lvds_dual, lvds_8bpc);
1819 break;
1820 case DCB_OUTPUT_DP:
1821 nouveau_dp_train(nv_encoder, false, mode->clock, asyh->or.bpc);
1822 nv50_sor_dp_watermark_sst(nv_encoder, head, asyh);
1823 depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1824
1825 if (nv_encoder->outp.or.link & 1)
1826 proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1827 else
1828 proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1829
1830 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1831 backlight = nv_connector->backlight;
1832 if (backlight && backlight->uses_dpcd)
1833 drm_edp_backlight_enable(&nv_connector->aux, &backlight->edp_info,
1834 (u16)backlight->dev->props.brightness);
1835 #endif
1836
1837 break;
1838 default:
1839 BUG();
1840 break;
1841 }
1842
1843 if (head->func->display_id)
1844 head->func->display_id(head, BIT(nv_encoder->outp.id));
1845
1846 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1847 }
1848
1849 static const struct drm_encoder_helper_funcs
1850 nv50_sor_help = {
1851 .atomic_check = nv50_outp_atomic_check,
1852 .atomic_enable = nv50_sor_atomic_enable,
1853 .atomic_disable = nv50_sor_atomic_disable,
1854 };
1855
1856 static void
nv50_sor_destroy(struct drm_encoder * encoder)1857 nv50_sor_destroy(struct drm_encoder *encoder)
1858 {
1859 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1860
1861 nv50_mstm_del(&nv_encoder->dp.mstm);
1862 drm_encoder_cleanup(encoder);
1863
1864 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1865 mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
1866
1867 nvif_outp_dtor(&nv_encoder->outp);
1868 kfree(encoder);
1869 }
1870
1871 static const struct drm_encoder_funcs
1872 nv50_sor_func = {
1873 .destroy = nv50_sor_destroy,
1874 };
1875
1876 static int
nv50_sor_create(struct nouveau_encoder * nv_encoder)1877 nv50_sor_create(struct nouveau_encoder *nv_encoder)
1878 {
1879 struct drm_connector *connector = &nv_encoder->conn->base;
1880 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1881 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1882 struct nvkm_i2c *i2c = nvxx_i2c(drm);
1883 struct drm_encoder *encoder;
1884 struct dcb_output *dcbe = nv_encoder->dcb;
1885 struct nv50_disp *disp = nv50_disp(connector->dev);
1886 int type, ret;
1887
1888 switch (dcbe->type) {
1889 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1890 case DCB_OUTPUT_TMDS:
1891 case DCB_OUTPUT_DP:
1892 default:
1893 type = DRM_MODE_ENCODER_TMDS;
1894 break;
1895 }
1896
1897 nv_encoder->update = nv50_sor_update;
1898
1899 encoder = to_drm_encoder(nv_encoder);
1900 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1901 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1902 drm_encoder_helper_add(encoder, &nv50_sor_help);
1903
1904 drm_connector_attach_encoder(connector, encoder);
1905
1906 disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1907 nv50_outp_dump_caps(drm, nv_encoder);
1908
1909 if (dcbe->type == DCB_OUTPUT_DP) {
1910 mutex_init(&nv_encoder->dp.hpd_irq_lock);
1911
1912 if (disp->disp->object.oclass < GF110_DISP) {
1913 /* HW has no support for address-only
1914 * transactions, so we're required to
1915 * use custom I2C-over-AUX code.
1916 */
1917 struct nvkm_i2c_aux *aux;
1918
1919 aux = nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1920 if (!aux)
1921 return -EINVAL;
1922
1923 nv_encoder->i2c = &aux->i2c;
1924 } else {
1925 nv_encoder->i2c = &nv_connector->aux.ddc;
1926 }
1927
1928 if (nv_connector->type != DCB_CONNECTOR_eDP && nv_encoder->outp.info.dp.mst) {
1929 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
1930 16, nv_connector->base.base.id,
1931 &nv_encoder->dp.mstm);
1932 if (ret)
1933 return ret;
1934 }
1935 } else
1936 if (nv_encoder->outp.info.ddc != NVIF_OUTP_DDC_INVALID) {
1937 struct nvkm_i2c_bus *bus =
1938 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1939 if (bus)
1940 nv_encoder->i2c = &bus->i2c;
1941 }
1942
1943 return 0;
1944 }
1945
1946 /******************************************************************************
1947 * PIOR
1948 *****************************************************************************/
1949 static int
nv50_pior_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)1950 nv50_pior_atomic_check(struct drm_encoder *encoder,
1951 struct drm_crtc_state *crtc_state,
1952 struct drm_connector_state *conn_state)
1953 {
1954 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1955 if (ret)
1956 return ret;
1957 crtc_state->adjusted_mode.clock *= 2;
1958 return 0;
1959 }
1960
1961 static void
nv50_pior_atomic_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)1962 nv50_pior_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1963 {
1964 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1965 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1966 const u32 ctrl = NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, NONE);
1967
1968 core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, NULL);
1969 nv_encoder->crtc = NULL;
1970 }
1971
1972 static void
nv50_pior_atomic_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)1973 nv50_pior_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1974 {
1975 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1976 struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1977 struct nv50_head_atom *asyh =
1978 nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1979 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1980 u32 ctrl = 0;
1981
1982 switch (nv_crtc->index) {
1983 case 0: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD0); break;
1984 case 1: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD1); break;
1985 default:
1986 WARN_ON(1);
1987 break;
1988 }
1989
1990 switch (asyh->or.bpc) {
1991 case 10: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444; break;
1992 case 8: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444; break;
1993 case 6: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444; break;
1994 default: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT; break;
1995 }
1996
1997 if (!nvif_outp_acquired(&nv_encoder->outp))
1998 nvif_outp_acquire_pior(&nv_encoder->outp);
1999
2000 switch (nv_encoder->dcb->type) {
2001 case DCB_OUTPUT_TMDS:
2002 ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
2003 break;
2004 case DCB_OUTPUT_DP:
2005 ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
2006 nouveau_dp_train(nv_encoder, false, asyh->state.adjusted_mode.clock, 6);
2007 break;
2008 default:
2009 BUG();
2010 break;
2011 }
2012
2013 core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, asyh);
2014 nv_encoder->crtc = &nv_crtc->base;
2015 }
2016
2017 static const struct drm_encoder_helper_funcs
2018 nv50_pior_help = {
2019 .atomic_check = nv50_pior_atomic_check,
2020 .atomic_enable = nv50_pior_atomic_enable,
2021 .atomic_disable = nv50_pior_atomic_disable,
2022 };
2023
2024 static void
nv50_pior_destroy(struct drm_encoder * encoder)2025 nv50_pior_destroy(struct drm_encoder *encoder)
2026 {
2027 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2028
2029 nvif_outp_dtor(&nv_encoder->outp);
2030
2031 drm_encoder_cleanup(encoder);
2032
2033 mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
2034 kfree(encoder);
2035 }
2036
2037 static const struct drm_encoder_funcs
2038 nv50_pior_func = {
2039 .destroy = nv50_pior_destroy,
2040 };
2041
2042 static int
nv50_pior_create(struct nouveau_encoder * nv_encoder)2043 nv50_pior_create(struct nouveau_encoder *nv_encoder)
2044 {
2045 struct drm_connector *connector = &nv_encoder->conn->base;
2046 struct drm_device *dev = connector->dev;
2047 struct nouveau_drm *drm = nouveau_drm(dev);
2048 struct nv50_disp *disp = nv50_disp(dev);
2049 struct nvkm_i2c *i2c = nvxx_i2c(drm);
2050 struct nvkm_i2c_bus *bus = NULL;
2051 struct nvkm_i2c_aux *aux = NULL;
2052 struct i2c_adapter *ddc;
2053 struct drm_encoder *encoder;
2054 struct dcb_output *dcbe = nv_encoder->dcb;
2055 int type;
2056
2057 switch (dcbe->type) {
2058 case DCB_OUTPUT_TMDS:
2059 bus = nvkm_i2c_bus_find(i2c, nv_encoder->outp.info.ddc);
2060 ddc = bus ? &bus->i2c : NULL;
2061 type = DRM_MODE_ENCODER_TMDS;
2062 break;
2063 case DCB_OUTPUT_DP:
2064 aux = nvkm_i2c_aux_find(i2c, nv_encoder->outp.info.dp.aux);
2065 ddc = aux ? &aux->i2c : NULL;
2066 type = DRM_MODE_ENCODER_TMDS;
2067 break;
2068 default:
2069 return -ENODEV;
2070 }
2071
2072 nv_encoder->i2c = ddc;
2073
2074 mutex_init(&nv_encoder->dp.hpd_irq_lock);
2075
2076 encoder = to_drm_encoder(nv_encoder);
2077 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
2078 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
2079 drm_encoder_helper_add(encoder, &nv50_pior_help);
2080
2081 drm_connector_attach_encoder(connector, encoder);
2082
2083 disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
2084 nv50_outp_dump_caps(drm, nv_encoder);
2085
2086 return 0;
2087 }
2088
2089 /******************************************************************************
2090 * Atomic
2091 *****************************************************************************/
2092
2093 static void
nv50_disp_atomic_commit_core(struct drm_atomic_state * state,u32 * interlock)2094 nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
2095 {
2096 struct drm_dp_mst_topology_mgr *mgr;
2097 struct drm_dp_mst_topology_state *mst_state;
2098 struct nouveau_drm *drm = nouveau_drm(state->dev);
2099 struct nv50_disp *disp = nv50_disp(drm->dev);
2100 struct nv50_atom *atom = nv50_atom(state);
2101 struct nv50_core *core = disp->core;
2102 struct nv50_outp_atom *outp;
2103 struct nv50_mstm *mstm;
2104 int i;
2105
2106 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
2107
2108 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
2109 mstm = nv50_mstm(mgr);
2110 if (mstm->modified)
2111 nv50_mstm_prepare(state, mst_state, mstm);
2112 }
2113
2114 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
2115 core->func->update(core, interlock, true);
2116 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
2117 disp->core->chan.base.device))
2118 NV_ERROR(drm, "core notifier timeout\n");
2119
2120 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
2121 mstm = nv50_mstm(mgr);
2122 if (mstm->modified)
2123 nv50_mstm_cleanup(state, mst_state, mstm);
2124 }
2125
2126 list_for_each_entry(outp, &atom->outp, head) {
2127 if (outp->encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2128 struct nouveau_encoder *nv_encoder = nouveau_encoder(outp->encoder);
2129
2130 if (outp->enabled) {
2131 nv50_audio_enable(outp->encoder, nouveau_crtc(nv_encoder->crtc),
2132 nv_encoder->conn, NULL, NULL);
2133 outp->enabled = outp->disabled = false;
2134 } else {
2135 if (outp->disabled) {
2136 nvif_outp_release(&nv_encoder->outp);
2137 outp->disabled = false;
2138 }
2139 }
2140 }
2141 }
2142 }
2143
2144 static void
nv50_disp_atomic_commit_wndw(struct drm_atomic_state * state,u32 * interlock)2145 nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
2146 {
2147 struct drm_plane_state *new_plane_state;
2148 struct drm_plane *plane;
2149 int i;
2150
2151 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2152 struct nv50_wndw *wndw = nv50_wndw(plane);
2153 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
2154 if (wndw->func->update)
2155 wndw->func->update(wndw, interlock);
2156 }
2157 }
2158 }
2159
2160 static void
nv50_disp_atomic_commit_tail(struct drm_atomic_state * state)2161 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
2162 {
2163 struct drm_device *dev = state->dev;
2164 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
2165 struct drm_crtc *crtc;
2166 struct drm_plane_state *new_plane_state;
2167 struct drm_plane *plane;
2168 struct nouveau_drm *drm = nouveau_drm(dev);
2169 struct nv50_disp *disp = nv50_disp(dev);
2170 struct nv50_atom *atom = nv50_atom(state);
2171 struct nv50_core *core = disp->core;
2172 struct nv50_outp_atom *outp, *outt;
2173 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
2174 int i;
2175 bool flushed = false;
2176
2177 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
2178 nv50_crc_atomic_stop_reporting(state);
2179 drm_atomic_helper_wait_for_fences(dev, state, false);
2180 drm_atomic_helper_wait_for_dependencies(state);
2181 drm_dp_mst_atomic_wait_for_dependencies(state);
2182 drm_atomic_helper_update_legacy_modeset_state(dev, state);
2183 drm_atomic_helper_calc_timestamping_constants(state);
2184
2185 if (atom->lock_core)
2186 mutex_lock(&disp->mutex);
2187
2188 /* Disable head(s). */
2189 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2190 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2191 struct nv50_head *head = nv50_head(crtc);
2192
2193 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
2194 asyh->clr.mask, asyh->set.mask);
2195
2196 if (old_crtc_state->active && !new_crtc_state->active) {
2197 pm_runtime_put_noidle(dev->dev);
2198 drm_crtc_vblank_off(crtc);
2199 }
2200
2201 if (asyh->clr.mask) {
2202 nv50_head_flush_clr(head, asyh, atom->flush_disable);
2203 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2204 }
2205 }
2206
2207 /* Disable plane(s). */
2208 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2209 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2210 struct nv50_wndw *wndw = nv50_wndw(plane);
2211
2212 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
2213 asyw->clr.mask, asyw->set.mask);
2214 if (!asyw->clr.mask)
2215 continue;
2216
2217 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
2218 }
2219
2220 /* Disable output path(s). */
2221 list_for_each_entry(outp, &atom->outp, head) {
2222 const struct drm_encoder_helper_funcs *help;
2223 struct drm_encoder *encoder;
2224
2225 encoder = outp->encoder;
2226 help = encoder->helper_private;
2227
2228 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
2229 outp->clr.mask, outp->set.mask);
2230
2231 if (outp->clr.mask) {
2232 help->atomic_disable(encoder, state);
2233 outp->disabled = true;
2234 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2235 }
2236 }
2237
2238 /* Flush disable. */
2239 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2240 if (atom->flush_disable) {
2241 nv50_disp_atomic_commit_wndw(state, interlock);
2242 nv50_disp_atomic_commit_core(state, interlock);
2243 memset(interlock, 0x00, sizeof(interlock));
2244
2245 flushed = true;
2246 }
2247 }
2248
2249 if (flushed)
2250 nv50_crc_atomic_release_notifier_contexts(state);
2251 nv50_crc_atomic_init_notifier_contexts(state);
2252
2253 /* Update output path(s). */
2254 list_for_each_entry(outp, &atom->outp, head) {
2255 const struct drm_encoder_helper_funcs *help;
2256 struct drm_encoder *encoder;
2257
2258 encoder = outp->encoder;
2259 help = encoder->helper_private;
2260
2261 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
2262 outp->set.mask, outp->clr.mask);
2263
2264 if (outp->set.mask) {
2265 help->atomic_enable(encoder, state);
2266 outp->enabled = true;
2267 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2268 }
2269 }
2270
2271 /* Update head(s). */
2272 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2273 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2274 struct nv50_head *head = nv50_head(crtc);
2275
2276 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2277 asyh->set.mask, asyh->clr.mask);
2278
2279 if (asyh->set.mask) {
2280 nv50_head_flush_set(head, asyh);
2281 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2282 }
2283
2284 if (new_crtc_state->active) {
2285 if (!old_crtc_state->active) {
2286 drm_crtc_vblank_on(crtc);
2287 pm_runtime_get_noresume(dev->dev);
2288 }
2289 if (new_crtc_state->event)
2290 drm_crtc_vblank_get(crtc);
2291 }
2292 }
2293
2294 /* Update window->head assignment.
2295 *
2296 * This has to happen in an update that's not interlocked with
2297 * any window channels to avoid hitting HW error checks.
2298 *
2299 *TODO: Proper handling of window ownership (Turing apparently
2300 * supports non-fixed mappings).
2301 */
2302 if (core->assign_windows) {
2303 core->func->wndw.owner(core);
2304 nv50_disp_atomic_commit_core(state, interlock);
2305 core->assign_windows = false;
2306 interlock[NV50_DISP_INTERLOCK_CORE] = 0;
2307 }
2308
2309 /* Finish updating head(s)...
2310 *
2311 * NVD is rather picky about both where window assignments can change,
2312 * *and* about certain core and window channel states matching.
2313 *
2314 * The EFI GOP driver on newer GPUs configures window channels with a
2315 * different output format to what we do, and the core channel update
2316 * in the assign_windows case above would result in a state mismatch.
2317 *
2318 * Delay some of the head update until after that point to workaround
2319 * the issue. This only affects the initial modeset.
2320 *
2321 * TODO: handle this better when adding flexible window mapping
2322 */
2323 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2324 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2325 struct nv50_head *head = nv50_head(crtc);
2326
2327 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2328 asyh->set.mask, asyh->clr.mask);
2329
2330 if (asyh->set.mask) {
2331 nv50_head_flush_set_wndw(head, asyh);
2332 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2333 }
2334 }
2335
2336 /* Update plane(s). */
2337 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2338 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2339 struct nv50_wndw *wndw = nv50_wndw(plane);
2340
2341 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
2342 asyw->set.mask, asyw->clr.mask);
2343 if ( !asyw->set.mask &&
2344 (!asyw->clr.mask || atom->flush_disable))
2345 continue;
2346
2347 nv50_wndw_flush_set(wndw, interlock, asyw);
2348 }
2349
2350 /* Flush update. */
2351 nv50_disp_atomic_commit_wndw(state, interlock);
2352
2353 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2354 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2355 interlock[NV50_DISP_INTERLOCK_OVLY] ||
2356 interlock[NV50_DISP_INTERLOCK_WNDW] ||
2357 !atom->state.legacy_cursor_update)
2358 nv50_disp_atomic_commit_core(state, interlock);
2359 else
2360 disp->core->func->update(disp->core, interlock, false);
2361 }
2362
2363 if (atom->lock_core)
2364 mutex_unlock(&disp->mutex);
2365
2366 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2367 list_del(&outp->head);
2368 kfree(outp);
2369 }
2370
2371 /* Wait for HW to signal completion. */
2372 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2373 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2374 struct nv50_wndw *wndw = nv50_wndw(plane);
2375 int ret = nv50_wndw_wait_armed(wndw, asyw);
2376 if (ret)
2377 NV_ERROR(drm, "%s: timeout\n", plane->name);
2378 }
2379
2380 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2381 if (new_crtc_state->event) {
2382 unsigned long flags;
2383 /* Get correct count/ts if racing with vblank irq */
2384 if (new_crtc_state->active)
2385 drm_crtc_accurate_vblank_count(crtc);
2386 spin_lock_irqsave(&crtc->dev->event_lock, flags);
2387 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2388 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2389
2390 new_crtc_state->event = NULL;
2391 if (new_crtc_state->active)
2392 drm_crtc_vblank_put(crtc);
2393 }
2394 }
2395
2396 nv50_crc_atomic_start_reporting(state);
2397 if (!flushed)
2398 nv50_crc_atomic_release_notifier_contexts(state);
2399
2400 drm_atomic_helper_commit_hw_done(state);
2401 drm_atomic_helper_cleanup_planes(dev, state);
2402 drm_atomic_helper_commit_cleanup_done(state);
2403 drm_atomic_state_put(state);
2404
2405 /* Drop the RPM ref we got from nv50_disp_atomic_commit() */
2406 pm_runtime_mark_last_busy(dev->dev);
2407 pm_runtime_put_autosuspend(dev->dev);
2408 }
2409
2410 static void
nv50_disp_atomic_commit_work(struct work_struct * work)2411 nv50_disp_atomic_commit_work(struct work_struct *work)
2412 {
2413 struct drm_atomic_state *state =
2414 container_of(work, typeof(*state), commit_work);
2415 nv50_disp_atomic_commit_tail(state);
2416 }
2417
2418 static int
nv50_disp_atomic_commit(struct drm_device * dev,struct drm_atomic_state * state,bool nonblock)2419 nv50_disp_atomic_commit(struct drm_device *dev,
2420 struct drm_atomic_state *state, bool nonblock)
2421 {
2422 struct drm_plane_state *new_plane_state;
2423 struct drm_plane *plane;
2424 int ret, i;
2425
2426 ret = pm_runtime_get_sync(dev->dev);
2427 if (ret < 0 && ret != -EACCES) {
2428 pm_runtime_put_autosuspend(dev->dev);
2429 return ret;
2430 }
2431
2432 ret = drm_atomic_helper_setup_commit(state, nonblock);
2433 if (ret)
2434 goto done;
2435
2436 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
2437
2438 ret = drm_atomic_helper_prepare_planes(dev, state);
2439 if (ret)
2440 goto done;
2441
2442 if (!nonblock) {
2443 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2444 if (ret)
2445 goto err_cleanup;
2446 }
2447
2448 ret = drm_atomic_helper_swap_state(state, true);
2449 if (ret)
2450 goto err_cleanup;
2451
2452 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2453 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2454 struct nv50_wndw *wndw = nv50_wndw(plane);
2455
2456 if (asyw->set.image)
2457 nv50_wndw_ntfy_enable(wndw, asyw);
2458 }
2459
2460 drm_atomic_state_get(state);
2461
2462 /*
2463 * Grab another RPM ref for the commit tail, which will release the
2464 * ref when it's finished
2465 */
2466 pm_runtime_get_noresume(dev->dev);
2467
2468 if (nonblock)
2469 queue_work(system_unbound_wq, &state->commit_work);
2470 else
2471 nv50_disp_atomic_commit_tail(state);
2472
2473 err_cleanup:
2474 if (ret)
2475 drm_atomic_helper_unprepare_planes(dev, state);
2476 done:
2477 pm_runtime_put_autosuspend(dev->dev);
2478 return ret;
2479 }
2480
2481 static struct nv50_outp_atom *
nv50_disp_outp_atomic_add(struct nv50_atom * atom,struct drm_encoder * encoder)2482 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
2483 {
2484 struct nv50_outp_atom *outp;
2485
2486 list_for_each_entry(outp, &atom->outp, head) {
2487 if (outp->encoder == encoder)
2488 return outp;
2489 }
2490
2491 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2492 if (!outp)
2493 return ERR_PTR(-ENOMEM);
2494
2495 list_add(&outp->head, &atom->outp);
2496 outp->encoder = encoder;
2497 return outp;
2498 }
2499
2500 static int
nv50_disp_outp_atomic_check_clr(struct nv50_atom * atom,struct drm_connector_state * old_connector_state)2501 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2502 struct drm_connector_state *old_connector_state)
2503 {
2504 struct drm_encoder *encoder = old_connector_state->best_encoder;
2505 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2506 struct drm_crtc *crtc;
2507 struct nv50_outp_atom *outp;
2508
2509 if (!(crtc = old_connector_state->crtc))
2510 return 0;
2511
2512 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
2513 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2514 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2515 outp = nv50_disp_outp_atomic_add(atom, encoder);
2516 if (IS_ERR(outp))
2517 return PTR_ERR(outp);
2518
2519 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST ||
2520 nouveau_encoder(outp->encoder)->dcb->type == DCB_OUTPUT_DP)
2521 atom->flush_disable = true;
2522 outp->clr.ctrl = true;
2523 atom->lock_core = true;
2524 }
2525
2526 return 0;
2527 }
2528
2529 static int
nv50_disp_outp_atomic_check_set(struct nv50_atom * atom,struct drm_connector_state * connector_state)2530 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
2531 struct drm_connector_state *connector_state)
2532 {
2533 struct drm_encoder *encoder = connector_state->best_encoder;
2534 struct drm_crtc_state *new_crtc_state;
2535 struct drm_crtc *crtc;
2536 struct nv50_outp_atom *outp;
2537
2538 if (!(crtc = connector_state->crtc))
2539 return 0;
2540
2541 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2542 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2543 outp = nv50_disp_outp_atomic_add(atom, encoder);
2544 if (IS_ERR(outp))
2545 return PTR_ERR(outp);
2546
2547 outp->set.ctrl = true;
2548 atom->lock_core = true;
2549 }
2550
2551 return 0;
2552 }
2553
2554 static int
nv50_disp_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)2555 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
2556 {
2557 struct nv50_atom *atom = nv50_atom(state);
2558 struct nv50_core *core = nv50_disp(dev)->core;
2559 struct drm_connector_state *old_connector_state, *new_connector_state;
2560 struct drm_connector *connector;
2561 struct drm_crtc_state *new_crtc_state;
2562 struct drm_crtc *crtc;
2563 struct nv50_head *head;
2564 struct nv50_head_atom *asyh;
2565 int ret, i;
2566
2567 if (core->assign_windows && core->func->head->static_wndw_map) {
2568 drm_for_each_crtc(crtc, dev) {
2569 new_crtc_state = drm_atomic_get_crtc_state(state,
2570 crtc);
2571 if (IS_ERR(new_crtc_state))
2572 return PTR_ERR(new_crtc_state);
2573
2574 head = nv50_head(crtc);
2575 asyh = nv50_head_atom(new_crtc_state);
2576 core->func->head->static_wndw_map(head, asyh);
2577 }
2578 }
2579
2580 /* We need to handle colour management on a per-plane basis. */
2581 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2582 if (new_crtc_state->color_mgmt_changed) {
2583 ret = drm_atomic_add_affected_planes(state, crtc);
2584 if (ret)
2585 return ret;
2586 }
2587 }
2588
2589 ret = drm_atomic_helper_check(dev, state);
2590 if (ret)
2591 return ret;
2592
2593 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
2594 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2595 if (ret)
2596 return ret;
2597
2598 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2599 if (ret)
2600 return ret;
2601 }
2602
2603 ret = drm_dp_mst_atomic_check(state);
2604 if (ret)
2605 return ret;
2606
2607 nv50_crc_atomic_check_outp(atom);
2608
2609 return 0;
2610 }
2611
2612 static void
nv50_disp_atomic_state_clear(struct drm_atomic_state * state)2613 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2614 {
2615 struct nv50_atom *atom = nv50_atom(state);
2616 struct nv50_outp_atom *outp, *outt;
2617
2618 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2619 list_del(&outp->head);
2620 kfree(outp);
2621 }
2622
2623 drm_atomic_state_default_clear(state);
2624 }
2625
2626 static void
nv50_disp_atomic_state_free(struct drm_atomic_state * state)2627 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2628 {
2629 struct nv50_atom *atom = nv50_atom(state);
2630 drm_atomic_state_default_release(&atom->state);
2631 kfree(atom);
2632 }
2633
2634 static struct drm_atomic_state *
nv50_disp_atomic_state_alloc(struct drm_device * dev)2635 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2636 {
2637 struct nv50_atom *atom;
2638 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2639 drm_atomic_state_init(dev, &atom->state) < 0) {
2640 kfree(atom);
2641 return NULL;
2642 }
2643 INIT_LIST_HEAD(&atom->outp);
2644 return &atom->state;
2645 }
2646
2647 static const struct drm_mode_config_funcs
2648 nv50_disp_func = {
2649 .fb_create = nouveau_user_framebuffer_create,
2650 .atomic_check = nv50_disp_atomic_check,
2651 .atomic_commit = nv50_disp_atomic_commit,
2652 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2653 .atomic_state_clear = nv50_disp_atomic_state_clear,
2654 .atomic_state_free = nv50_disp_atomic_state_free,
2655 };
2656
2657 static const struct drm_mode_config_helper_funcs
2658 nv50_disp_helper_func = {
2659 .atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
2660 };
2661
2662 /******************************************************************************
2663 * Init
2664 *****************************************************************************/
2665
2666 static void
nv50_display_fini(struct drm_device * dev,bool runtime,bool suspend)2667 nv50_display_fini(struct drm_device *dev, bool runtime, bool suspend)
2668 {
2669 struct nouveau_drm *drm = nouveau_drm(dev);
2670 struct drm_encoder *encoder;
2671
2672 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2673 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
2674 nv50_mstm_fini(nouveau_encoder(encoder));
2675 }
2676
2677 if (!runtime && !drm->headless)
2678 cancel_work_sync(&drm->hpd_work);
2679 }
2680
2681 static inline void
nv50_display_read_hw_or_state(struct drm_device * dev,struct nv50_disp * disp,struct nouveau_encoder * outp)2682 nv50_display_read_hw_or_state(struct drm_device *dev, struct nv50_disp *disp,
2683 struct nouveau_encoder *outp)
2684 {
2685 struct drm_crtc *crtc;
2686 struct drm_connector_list_iter conn_iter;
2687 struct drm_connector *conn;
2688 struct nv50_head_atom *armh;
2689 const u32 encoder_mask = drm_encoder_mask(&outp->base.base);
2690 bool found_conn = false, found_head = false;
2691 u8 proto;
2692 int head_idx;
2693 int ret;
2694
2695 switch (outp->dcb->type) {
2696 case DCB_OUTPUT_TMDS:
2697 ret = nvif_outp_inherit_tmds(&outp->outp, &proto);
2698 break;
2699 case DCB_OUTPUT_DP:
2700 ret = nvif_outp_inherit_dp(&outp->outp, &proto);
2701 break;
2702 case DCB_OUTPUT_LVDS:
2703 ret = nvif_outp_inherit_lvds(&outp->outp, &proto);
2704 break;
2705 case DCB_OUTPUT_ANALOG:
2706 ret = nvif_outp_inherit_rgb_crt(&outp->outp, &proto);
2707 break;
2708 default:
2709 drm_dbg_kms(dev, "Readback for %s not implemented yet, skipping\n",
2710 outp->base.base.name);
2711 drm_WARN_ON(dev, true);
2712 return;
2713 }
2714
2715 if (ret < 0)
2716 return;
2717
2718 head_idx = ret;
2719
2720 drm_for_each_crtc(crtc, dev) {
2721 if (crtc->index != head_idx)
2722 continue;
2723
2724 armh = nv50_head_atom(crtc->state);
2725 found_head = true;
2726 break;
2727 }
2728 if (drm_WARN_ON(dev, !found_head))
2729 return;
2730
2731 /* Figure out which connector is being used by this encoder */
2732 drm_connector_list_iter_begin(dev, &conn_iter);
2733 nouveau_for_each_non_mst_connector_iter(conn, &conn_iter) {
2734 if (nouveau_connector(conn)->index == outp->dcb->connector) {
2735 found_conn = true;
2736 break;
2737 }
2738 }
2739 drm_connector_list_iter_end(&conn_iter);
2740 if (drm_WARN_ON(dev, !found_conn))
2741 return;
2742
2743 armh->state.encoder_mask = encoder_mask;
2744 armh->state.connector_mask = drm_connector_mask(conn);
2745 armh->state.active = true;
2746 armh->state.enable = true;
2747 pm_runtime_get_noresume(dev->dev);
2748
2749 outp->crtc = crtc;
2750 outp->ctrl = NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto) | BIT(crtc->index);
2751
2752 drm_connector_get(conn);
2753 conn->state->crtc = crtc;
2754 conn->state->best_encoder = &outp->base.base;
2755 }
2756
2757 /* Read back the currently programmed display state */
2758 static void
nv50_display_read_hw_state(struct nouveau_drm * drm)2759 nv50_display_read_hw_state(struct nouveau_drm *drm)
2760 {
2761 struct drm_device *dev = drm->dev;
2762 struct drm_encoder *encoder;
2763 struct drm_modeset_acquire_ctx ctx;
2764 struct nv50_disp *disp = nv50_disp(dev);
2765 int ret;
2766
2767 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
2768
2769 drm_for_each_encoder(encoder, dev) {
2770 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST)
2771 continue;
2772
2773 nv50_display_read_hw_or_state(dev, disp, nouveau_encoder(encoder));
2774 }
2775
2776 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
2777 }
2778
2779 static int
nv50_display_init(struct drm_device * dev,bool resume,bool runtime)2780 nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2781 {
2782 struct nv50_core *core = nv50_disp(dev)->core;
2783 struct drm_encoder *encoder;
2784
2785 if (resume || runtime)
2786 core->func->init(core);
2787
2788 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2789 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2790 struct nouveau_encoder *nv_encoder =
2791 nouveau_encoder(encoder);
2792 nv50_mstm_init(nv_encoder, runtime);
2793 }
2794 }
2795
2796 if (!resume)
2797 nv50_display_read_hw_state(nouveau_drm(dev));
2798
2799 return 0;
2800 }
2801
2802 static void
nv50_display_destroy(struct drm_device * dev)2803 nv50_display_destroy(struct drm_device *dev)
2804 {
2805 struct nv50_disp *disp = nv50_disp(dev);
2806
2807 nv50_audio_component_fini(nouveau_drm(dev));
2808
2809 nvif_object_unmap(&disp->caps);
2810 nvif_object_dtor(&disp->caps);
2811 nv50_core_del(&disp->core);
2812
2813 nouveau_bo_unmap(disp->sync);
2814 if (disp->sync)
2815 nouveau_bo_unpin(disp->sync);
2816 nouveau_bo_fini(disp->sync);
2817
2818 nouveau_display(dev)->priv = NULL;
2819 kfree(disp);
2820 }
2821
2822 int
nv50_display_create(struct drm_device * dev)2823 nv50_display_create(struct drm_device *dev)
2824 {
2825 struct nouveau_drm *drm = nouveau_drm(dev);
2826 struct drm_connector *connector, *tmp;
2827 struct nv50_disp *disp;
2828 int ret, i;
2829 bool has_mst = false;
2830
2831 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2832 if (!disp)
2833 return -ENOMEM;
2834
2835 mutex_init(&disp->mutex);
2836
2837 nouveau_display(dev)->priv = disp;
2838 nouveau_display(dev)->dtor = nv50_display_destroy;
2839 nouveau_display(dev)->init = nv50_display_init;
2840 nouveau_display(dev)->fini = nv50_display_fini;
2841 disp->disp = &nouveau_display(dev)->disp;
2842 dev->mode_config.funcs = &nv50_disp_func;
2843 dev->mode_config.helper_private = &nv50_disp_helper_func;
2844 dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2845 dev->mode_config.normalize_zpos = true;
2846
2847 /* small shared memory area we use for notifiers and semaphores */
2848 ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
2849 NOUVEAU_GEM_DOMAIN_VRAM,
2850 0, 0x0000, NULL, NULL, &disp->sync);
2851 if (!ret) {
2852 ret = nouveau_bo_pin(disp->sync, NOUVEAU_GEM_DOMAIN_VRAM, true);
2853 if (!ret) {
2854 ret = nouveau_bo_map(disp->sync);
2855 if (ret)
2856 nouveau_bo_unpin(disp->sync);
2857 }
2858 if (ret)
2859 nouveau_bo_fini(disp->sync);
2860 }
2861
2862 if (ret)
2863 goto out;
2864
2865 /* allocate master evo channel */
2866 ret = nv50_core_new(drm, &disp->core);
2867 if (ret)
2868 goto out;
2869
2870 disp->core->func->init(disp->core);
2871 if (disp->core->func->caps_init) {
2872 ret = disp->core->func->caps_init(drm, disp);
2873 if (ret)
2874 goto out;
2875 }
2876
2877 /* Assign the correct format modifiers */
2878 if (disp->disp->object.oclass >= TU102_DISP)
2879 nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
2880 else
2881 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
2882 nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
2883 else
2884 nouveau_display(dev)->format_modifiers = disp50xx_modifiers;
2885
2886 /* FIXME: 256x256 cursors are supported on Kepler, however unlike Maxwell and later
2887 * generations Kepler requires that we use small pages (4K) for cursor scanout surfaces. The
2888 * proper fix for this is to teach nouveau to migrate fbs being used for the cursor plane to
2889 * small page allocations in prepare_fb(). When this is implemented, we should also force
2890 * large pages (128K) for ovly fbs in order to fix Kepler ovlys.
2891 * But until then, just limit cursors to 128x128 - which is small enough to avoid ever using
2892 * large pages.
2893 */
2894 if (disp->disp->object.oclass >= GM107_DISP) {
2895 dev->mode_config.cursor_width = 256;
2896 dev->mode_config.cursor_height = 256;
2897 } else if (disp->disp->object.oclass >= GK104_DISP) {
2898 dev->mode_config.cursor_width = 128;
2899 dev->mode_config.cursor_height = 128;
2900 } else {
2901 dev->mode_config.cursor_width = 64;
2902 dev->mode_config.cursor_height = 64;
2903 }
2904
2905 /* create encoder/connector objects based on VBIOS DCB table */
2906 for_each_set_bit(i, &disp->disp->outp_mask, sizeof(disp->disp->outp_mask) * 8) {
2907 struct nouveau_encoder *outp;
2908
2909 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2910 if (!outp)
2911 break;
2912
2913 ret = nvif_outp_ctor(disp->disp, "kmsOutp", i, &outp->outp);
2914 if (ret) {
2915 kfree(outp);
2916 continue;
2917 }
2918
2919 connector = nouveau_connector_create(dev, outp->outp.info.conn);
2920 if (IS_ERR(connector)) {
2921 nvif_outp_dtor(&outp->outp);
2922 kfree(outp);
2923 continue;
2924 }
2925
2926 outp->base.base.possible_crtcs = outp->outp.info.heads;
2927 outp->base.base.possible_clones = 0;
2928 outp->conn = nouveau_connector(connector);
2929
2930 outp->dcb = kzalloc(sizeof(*outp->dcb), GFP_KERNEL);
2931 if (!outp->dcb)
2932 break;
2933
2934 switch (outp->outp.info.proto) {
2935 case NVIF_OUTP_RGB_CRT:
2936 outp->dcb->type = DCB_OUTPUT_ANALOG;
2937 outp->dcb->crtconf.maxfreq = outp->outp.info.rgb_crt.freq_max;
2938 break;
2939 case NVIF_OUTP_TMDS:
2940 outp->dcb->type = DCB_OUTPUT_TMDS;
2941 outp->dcb->duallink_possible = outp->outp.info.tmds.dual;
2942 break;
2943 case NVIF_OUTP_LVDS:
2944 outp->dcb->type = DCB_OUTPUT_LVDS;
2945 outp->dcb->lvdsconf.use_acpi_for_edid = outp->outp.info.lvds.acpi_edid;
2946 break;
2947 case NVIF_OUTP_DP:
2948 outp->dcb->type = DCB_OUTPUT_DP;
2949 outp->dcb->dpconf.link_nr = outp->outp.info.dp.link_nr;
2950 outp->dcb->dpconf.link_bw = outp->outp.info.dp.link_bw;
2951 if (outp->outp.info.dp.mst)
2952 has_mst = true;
2953 break;
2954 default:
2955 WARN_ON(1);
2956 continue;
2957 }
2958
2959 outp->dcb->heads = outp->outp.info.heads;
2960 outp->dcb->connector = outp->outp.info.conn;
2961 outp->dcb->i2c_index = outp->outp.info.ddc;
2962
2963 switch (outp->outp.info.type) {
2964 case NVIF_OUTP_DAC : ret = nv50_dac_create(outp); break;
2965 case NVIF_OUTP_SOR : ret = nv50_sor_create(outp); break;
2966 case NVIF_OUTP_PIOR: ret = nv50_pior_create(outp); break;
2967 default:
2968 WARN_ON(1);
2969 continue;
2970 }
2971
2972 if (ret) {
2973 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2974 i, outp->outp.info.type, outp->outp.info.proto, ret);
2975 }
2976 }
2977
2978 /* cull any connectors we created that don't have an encoder */
2979 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2980 if (connector->possible_encoders)
2981 continue;
2982
2983 NV_WARN(drm, "%s has no encoders, removing\n",
2984 connector->name);
2985 connector->funcs->destroy(connector);
2986 }
2987
2988 /* create crtc objects to represent the hw heads */
2989 for_each_set_bit(i, &disp->disp->head_mask, sizeof(disp->disp->head_mask) * 8) {
2990 struct nv50_head *head;
2991
2992 head = nv50_head_create(dev, i);
2993 if (IS_ERR(head)) {
2994 ret = PTR_ERR(head);
2995 goto out;
2996 }
2997
2998 if (has_mst) {
2999 head->msto = nv50_msto_new(dev, head, i);
3000 if (IS_ERR(head->msto)) {
3001 ret = PTR_ERR(head->msto);
3002 head->msto = NULL;
3003 goto out;
3004 }
3005
3006 /*
3007 * FIXME: This is a hack to workaround the following
3008 * issues:
3009 *
3010 * https://gitlab.gnome.org/GNOME/mutter/issues/759
3011 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
3012 *
3013 * Once these issues are closed, this should be
3014 * removed
3015 */
3016 head->msto->encoder.possible_crtcs = disp->disp->head_mask;
3017 }
3018 }
3019
3020 /* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
3021 dev->vblank_disable_immediate = true;
3022
3023 nv50_audio_component_init(drm);
3024
3025 out:
3026 if (ret)
3027 nv50_display_destroy(dev);
3028 return ret;
3029 }
3030
3031 /******************************************************************************
3032 * Format modifiers
3033 *****************************************************************************/
3034
3035 /****************************************************************
3036 * Log2(block height) ----------------------------+ *
3037 * Page Kind ----------------------------------+ | *
3038 * Gob Height/Page Kind Generation ------+ | | *
3039 * Sector layout -------+ | | | *
3040 * Compression ------+ | | | | */
3041 const u64 disp50xx_modifiers[] = { /* | | | | | */
3042 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
3043 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
3044 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
3045 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
3046 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
3047 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
3048 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
3049 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
3050 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
3051 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
3052 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
3053 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
3054 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
3055 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
3056 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
3057 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
3058 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
3059 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
3060 DRM_FORMAT_MOD_LINEAR,
3061 DRM_FORMAT_MOD_INVALID
3062 };
3063
3064 /****************************************************************
3065 * Log2(block height) ----------------------------+ *
3066 * Page Kind ----------------------------------+ | *
3067 * Gob Height/Page Kind Generation ------+ | | *
3068 * Sector layout -------+ | | | *
3069 * Compression ------+ | | | | */
3070 const u64 disp90xx_modifiers[] = { /* | | | | | */
3071 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
3072 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
3073 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
3074 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
3075 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
3076 DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
3077 DRM_FORMAT_MOD_LINEAR,
3078 DRM_FORMAT_MOD_INVALID
3079 };
3080