]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/nouveau/nv50_display.c
drm/fb-helper: Give up on kgdb for atomic drivers
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nv50_display.c
CommitLineData
56d237d2 1/*
26f6d88b
BS
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
51beb428 25#include <linux/dma-mapping.h>
83fc083c 26
760285e7 27#include <drm/drmP.h>
ad633619 28#include <drm/drm_atomic.h>
973f10c2 29#include <drm/drm_atomic_helper.h>
760285e7 30#include <drm/drm_crtc_helper.h>
4874322e 31#include <drm/drm_dp_helper.h>
b516a9ef 32#include <drm/drm_fb_helper.h>
ad633619 33#include <drm/drm_plane_helper.h>
26f6d88b 34
fdb751ef 35#include <nvif/class.h>
845f2725 36#include <nvif/cl0002.h>
7568b106
BS
37#include <nvif/cl5070.h>
38#include <nvif/cl507a.h>
39#include <nvif/cl507b.h>
40#include <nvif/cl507c.h>
41#include <nvif/cl507d.h>
42#include <nvif/cl507e.h>
973f10c2 43#include <nvif/event.h>
fdb751ef 44
4dc28134 45#include "nouveau_drv.h"
77145f1c
BS
46#include "nouveau_dma.h"
47#include "nouveau_gem.h"
26f6d88b
BS
48#include "nouveau_connector.h"
49#include "nouveau_encoder.h"
50#include "nouveau_crtc.h"
f589be88 51#include "nouveau_fence.h"
839ca903 52#include "nouveau_fbcon.h"
3a89cd02 53#include "nv50_display.h"
26f6d88b 54
8a46438a
BS
55#define EVO_DMA_NR 9
56
bdb8c212 57#define EVO_MASTER (0x00)
a63a97eb 58#define EVO_FLIP(c) (0x01 + (c))
8a46438a
BS
59#define EVO_OVLY(c) (0x05 + (c))
60#define EVO_OIMM(c) (0x09 + (c))
bdb8c212
BS
61#define EVO_CURS(c) (0x0d + (c))
62
816af2f2
BS
63/* offsets in shared sync bo of various structures */
64#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
9f9bdaaf
BS
65#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
66#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
67#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
973f10c2
BS
68#define EVO_FLIP_NTFY0(c) EVO_SYNC((c) + 1, 0x20)
69#define EVO_FLIP_NTFY1(c) EVO_SYNC((c) + 1, 0x30)
816af2f2 70
3dbd036b
BS
71/******************************************************************************
72 * Atomic state
73 *****************************************************************************/
839ca903
BS
74#define nv50_atom(p) container_of((p), struct nv50_atom, state)
75
76struct nv50_atom {
77 struct drm_atomic_state state;
78
79 struct list_head outp;
80 bool lock_core;
81 bool flush_disable;
82};
83
84struct nv50_outp_atom {
85 struct list_head head;
86
87 struct drm_encoder *encoder;
88 bool flush_disable;
89
90 union {
91 struct {
92 bool ctrl:1;
93 };
94 u8 mask;
95 } clr;
96
97 union {
98 struct {
99 bool ctrl:1;
100 };
101 u8 mask;
102 } set;
103};
104
3dbd036b
BS
105#define nv50_head_atom(p) container_of((p), struct nv50_head_atom, state)
106
107struct nv50_head_atom {
108 struct drm_crtc_state state;
109
c4e6812c
BS
110 struct {
111 u16 iW;
112 u16 iH;
113 u16 oW;
114 u16 oH;
115 } view;
116
3dbd036b
BS
117 struct nv50_head_mode {
118 bool interlace;
119 u32 clock;
120 struct {
121 u16 active;
122 u16 synce;
123 u16 blanke;
124 u16 blanks;
125 } h;
126 struct {
127 u32 active;
128 u16 synce;
129 u16 blanke;
130 u16 blanks;
131 u16 blank2s;
132 u16 blank2e;
133 u16 blankus;
134 } v;
135 } mode;
136
a7ae1561
BS
137 struct {
138 u32 handle;
139 u64 offset:40;
140 } lut;
141
ad633619
BS
142 struct {
143 bool visible;
144 u32 handle;
145 u64 offset:40;
146 u8 format;
147 u8 kind:7;
148 u8 layout:1;
149 u8 block:4;
150 u32 pitch:20;
151 u16 x;
152 u16 y;
153 u16 w;
154 u16 h;
155 } core;
156
ea8ee390
BS
157 struct {
158 bool visible;
159 u32 handle;
160 u64 offset:40;
161 u8 layout:1;
162 u8 format:1;
163 } curs;
164
ad633619
BS
165 struct {
166 u8 depth;
167 u8 cpp;
168 u16 x;
169 u16 y;
170 u16 w;
171 u16 h;
172 } base;
173
6bbab3b6
BS
174 struct {
175 u8 cpp;
176 } ovly;
177
7e91833d
BS
178 struct {
179 bool enable:1;
180 u8 bits:2;
181 u8 mode:4;
182 } dither;
183
7e08d67c
BS
184 struct {
185 struct {
186 u16 cos:12;
187 u16 sin:12;
188 } sat;
189 } procamp;
190
ad633619
BS
191 union {
192 struct {
193 bool core:1;
ea8ee390 194 bool curs:1;
ad633619
BS
195 };
196 u8 mask;
197 } clr;
198
3dbd036b
BS
199 union {
200 struct {
ad633619 201 bool core:1;
ea8ee390 202 bool curs:1;
ad633619 203 bool view:1;
3dbd036b 204 bool mode:1;
6bbab3b6
BS
205 bool base:1;
206 bool ovly:1;
7e91833d 207 bool dither:1;
7e08d67c 208 bool procamp:1;
3dbd036b
BS
209 };
210 u16 mask;
211 } set;
212};
213
839ca903
BS
214static inline struct nv50_head_atom *
215nv50_head_atom_get(struct drm_atomic_state *state, struct drm_crtc *crtc)
216{
217 struct drm_crtc_state *statec = drm_atomic_get_crtc_state(state, crtc);
218 if (IS_ERR(statec))
219 return (void *)statec;
220 return nv50_head_atom(statec);
221}
222
973f10c2
BS
223#define nv50_wndw_atom(p) container_of((p), struct nv50_wndw_atom, state)
224
225struct nv50_wndw_atom {
226 struct drm_plane_state state;
227 u8 interval;
228
229 struct drm_rect clip;
230
231 struct {
232 u32 handle;
233 u16 offset:12;
234 bool awaken:1;
235 } ntfy;
236
237 struct {
238 u32 handle;
239 u16 offset:12;
240 u32 acquire;
241 u32 release;
242 } sema;
243
244 struct {
245 u8 enable:2;
246 } lut;
247
248 struct {
249 u8 mode:2;
250 u8 interval:4;
251
252 u8 format;
253 u8 kind:7;
254 u8 layout:1;
255 u8 block:4;
256 u32 pitch:20;
257 u16 w;
258 u16 h;
259
260 u32 handle;
261 u64 offset;
262 } image;
263
264 struct {
265 u16 x;
266 u16 y;
267 } point;
268
269 union {
270 struct {
271 bool ntfy:1;
272 bool sema:1;
273 bool image:1;
274 };
275 u8 mask;
276 } clr;
277
278 union {
279 struct {
280 bool ntfy:1;
281 bool sema:1;
282 bool image:1;
283 bool lut:1;
284 bool point:1;
285 };
286 u8 mask;
287 } set;
288};
289
b5a794b0
BS
290/******************************************************************************
291 * EVO channel
292 *****************************************************************************/
293
e225f446 294struct nv50_chan {
0ad72863 295 struct nvif_object user;
a01ca78c 296 struct nvif_device *device;
b5a794b0
BS
297};
298
299static int
a01ca78c 300nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
315a8b2e 301 const s32 *oclass, u8 head, void *data, u32 size,
a01ca78c 302 struct nv50_chan *chan)
b5a794b0 303{
41a63406
BS
304 struct nvif_sclass *sclass;
305 int ret, i, n;
6af5289e 306
a01ca78c
BS
307 chan->device = device;
308
41a63406 309 ret = n = nvif_object_sclass_get(disp, &sclass);
6af5289e
BS
310 if (ret < 0)
311 return ret;
312
410f3ec6 313 while (oclass[0]) {
41a63406
BS
314 for (i = 0; i < n; i++) {
315 if (sclass[i].oclass == oclass[0]) {
fcf3f91c 316 ret = nvif_object_init(disp, 0, oclass[0],
a01ca78c 317 data, size, &chan->user);
6af5289e
BS
318 if (ret == 0)
319 nvif_object_map(&chan->user);
41a63406 320 nvif_object_sclass_put(&sclass);
6af5289e
BS
321 return ret;
322 }
b76f1529 323 }
6af5289e 324 oclass++;
410f3ec6 325 }
6af5289e 326
41a63406 327 nvif_object_sclass_put(&sclass);
410f3ec6 328 return -ENOSYS;
b5a794b0
BS
329}
330
331static void
0ad72863 332nv50_chan_destroy(struct nv50_chan *chan)
b5a794b0 333{
0ad72863 334 nvif_object_fini(&chan->user);
b5a794b0
BS
335}
336
337/******************************************************************************
338 * PIO EVO channel
339 *****************************************************************************/
340
e225f446
BS
341struct nv50_pioc {
342 struct nv50_chan base;
b5a794b0
BS
343};
344
345static void
0ad72863 346nv50_pioc_destroy(struct nv50_pioc *pioc)
b5a794b0 347{
0ad72863 348 nv50_chan_destroy(&pioc->base);
b5a794b0
BS
349}
350
351static int
a01ca78c 352nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
315a8b2e 353 const s32 *oclass, u8 head, void *data, u32 size,
a01ca78c 354 struct nv50_pioc *pioc)
b5a794b0 355{
a01ca78c
BS
356 return nv50_chan_create(device, disp, oclass, head, data, size,
357 &pioc->base);
410f3ec6
BS
358}
359
410f3ec6
BS
360/******************************************************************************
361 * Overlay Immediate
362 *****************************************************************************/
363
364struct nv50_oimm {
365 struct nv50_pioc base;
366};
367
368static int
a01ca78c
BS
369nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
370 int head, struct nv50_oimm *oimm)
410f3ec6 371{
648d4dfd 372 struct nv50_disp_cursor_v0 args = {
410f3ec6
BS
373 .head = head,
374 };
315a8b2e 375 static const s32 oclass[] = {
648d4dfd
BS
376 GK104_DISP_OVERLAY,
377 GF110_DISP_OVERLAY,
378 GT214_DISP_OVERLAY,
379 G82_DISP_OVERLAY,
380 NV50_DISP_OVERLAY,
410f3ec6
BS
381 0
382 };
383
a01ca78c
BS
384 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
385 &oimm->base);
b5a794b0
BS
386}
387
388/******************************************************************************
389 * DMA EVO channel
390 *****************************************************************************/
391
accdea2e
BS
392struct nv50_dmac_ctxdma {
393 struct list_head head;
394 struct nvif_object object;
395};
396
e225f446
BS
397struct nv50_dmac {
398 struct nv50_chan base;
3376ee37
BS
399 dma_addr_t handle;
400 u32 *ptr;
59ad1465 401
0ad72863
BS
402 struct nvif_object sync;
403 struct nvif_object vram;
accdea2e 404 struct list_head ctxdma;
0ad72863 405
59ad1465
DV
406 /* Protects against concurrent pushbuf access to this channel, lock is
407 * grabbed by evo_wait (if the pushbuf reservation is successful) and
408 * dropped again by evo_kick. */
409 struct mutex lock;
b5a794b0
BS
410};
411
accdea2e
BS
412static void
413nv50_dmac_ctxdma_del(struct nv50_dmac_ctxdma *ctxdma)
414{
415 nvif_object_fini(&ctxdma->object);
416 list_del(&ctxdma->head);
417 kfree(ctxdma);
418}
419
420static struct nv50_dmac_ctxdma *
f00f0e21 421nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, struct nouveau_framebuffer *fb)
accdea2e
BS
422{
423 struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
424 struct nv50_dmac_ctxdma *ctxdma;
f00f0e21
BS
425 const u8 kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
426 const u32 handle = 0xfb000000 | kind;
accdea2e
BS
427 struct {
428 struct nv_dma_v0 base;
429 union {
430 struct nv50_dma_v0 nv50;
431 struct gf100_dma_v0 gf100;
432 struct gf119_dma_v0 gf119;
433 };
434 } args = {};
435 u32 argc = sizeof(args.base);
436 int ret;
437
438 list_for_each_entry(ctxdma, &dmac->ctxdma, head) {
439 if (ctxdma->object.handle == handle)
440 return ctxdma;
441 }
442
443 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
444 return ERR_PTR(-ENOMEM);
445 list_add(&ctxdma->head, &dmac->ctxdma);
446
447 args.base.target = NV_DMA_V0_TARGET_VRAM;
448 args.base.access = NV_DMA_V0_ACCESS_RDWR;
449 args.base.start = 0;
1167c6bc 450 args.base.limit = drm->client.device.info.ram_user - 1;
accdea2e 451
1167c6bc 452 if (drm->client.device.info.chipset < 0x80) {
accdea2e
BS
453 args.nv50.part = NV50_DMA_V0_PART_256;
454 argc += sizeof(args.nv50);
455 } else
1167c6bc 456 if (drm->client.device.info.chipset < 0xc0) {
accdea2e
BS
457 args.nv50.part = NV50_DMA_V0_PART_256;
458 args.nv50.kind = kind;
459 argc += sizeof(args.nv50);
460 } else
1167c6bc 461 if (drm->client.device.info.chipset < 0xd0) {
accdea2e
BS
462 args.gf100.kind = kind;
463 argc += sizeof(args.gf100);
464 } else {
465 args.gf119.page = GF119_DMA_V0_PAGE_LP;
466 args.gf119.kind = kind;
467 argc += sizeof(args.gf119);
468 }
469
470 ret = nvif_object_init(&dmac->base.user, handle, NV_DMA_IN_MEMORY,
471 &args, argc, &ctxdma->object);
472 if (ret) {
473 nv50_dmac_ctxdma_del(ctxdma);
474 return ERR_PTR(ret);
475 }
476
477 return ctxdma;
478}
479
b5a794b0 480static void
0ad72863 481nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
b5a794b0 482{
a01ca78c 483 struct nvif_device *device = dmac->base.device;
accdea2e
BS
484 struct nv50_dmac_ctxdma *ctxdma, *ctxtmp;
485
486 list_for_each_entry_safe(ctxdma, ctxtmp, &dmac->ctxdma, head) {
487 nv50_dmac_ctxdma_del(ctxdma);
488 }
a01ca78c 489
0ad72863
BS
490 nvif_object_fini(&dmac->vram);
491 nvif_object_fini(&dmac->sync);
492
493 nv50_chan_destroy(&dmac->base);
494
b5a794b0 495 if (dmac->ptr) {
26c9e8ef
BS
496 struct device *dev = nvxx_device(device)->dev;
497 dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
b5a794b0 498 }
b5a794b0
BS
499}
500
47057302 501static int
a01ca78c 502nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
315a8b2e 503 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
e225f446 504 struct nv50_dmac *dmac)
47057302 505{
648d4dfd 506 struct nv50_disp_core_channel_dma_v0 *args = data;
0ad72863 507 struct nvif_object pushbuf;
47057302
BS
508 int ret;
509
59ad1465
DV
510 mutex_init(&dmac->lock);
511
26c9e8ef
BS
512 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
513 &dmac->handle, GFP_KERNEL);
47057302
BS
514 if (!dmac->ptr)
515 return -ENOMEM;
516
fcf3f91c
BS
517 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
518 &(struct nv_dma_v0) {
4acfd707
BS
519 .target = NV_DMA_V0_TARGET_PCI_US,
520 .access = NV_DMA_V0_ACCESS_RD,
47057302
BS
521 .start = dmac->handle + 0x0000,
522 .limit = dmac->handle + 0x0fff,
4acfd707 523 }, sizeof(struct nv_dma_v0), &pushbuf);
b5a794b0 524 if (ret)
47057302 525 return ret;
b5a794b0 526
bf81df9b
BS
527 args->pushbuf = nvif_handle(&pushbuf);
528
a01ca78c
BS
529 ret = nv50_chan_create(device, disp, oclass, head, data, size,
530 &dmac->base);
0ad72863 531 nvif_object_fini(&pushbuf);
47057302
BS
532 if (ret)
533 return ret;
534
a01ca78c 535 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
4acfd707
BS
536 &(struct nv_dma_v0) {
537 .target = NV_DMA_V0_TARGET_VRAM,
538 .access = NV_DMA_V0_ACCESS_RDWR,
47057302
BS
539 .start = syncbuf + 0x0000,
540 .limit = syncbuf + 0x0fff,
4acfd707 541 }, sizeof(struct nv_dma_v0),
0ad72863 542 &dmac->sync);
47057302
BS
543 if (ret)
544 return ret;
545
a01ca78c 546 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
4acfd707
BS
547 &(struct nv_dma_v0) {
548 .target = NV_DMA_V0_TARGET_VRAM,
549 .access = NV_DMA_V0_ACCESS_RDWR,
b5a794b0 550 .start = 0,
f392ec4b 551 .limit = device->info.ram_user - 1,
4acfd707 552 }, sizeof(struct nv_dma_v0),
0ad72863 553 &dmac->vram);
b5a794b0 554 if (ret)
47057302
BS
555 return ret;
556
accdea2e 557 INIT_LIST_HEAD(&dmac->ctxdma);
b5a794b0
BS
558 return ret;
559}
560
410f3ec6
BS
561/******************************************************************************
562 * Core
563 *****************************************************************************/
564
e225f446
BS
565struct nv50_mast {
566 struct nv50_dmac base;
b5a794b0
BS
567};
568
410f3ec6 569static int
a01ca78c
BS
570nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
571 u64 syncbuf, struct nv50_mast *core)
410f3ec6 572{
648d4dfd
BS
573 struct nv50_disp_core_channel_dma_v0 args = {
574 .pushbuf = 0xb0007d00,
410f3ec6 575 };
315a8b2e 576 static const s32 oclass[] = {
ed828666 577 GP102_DISP_CORE_CHANNEL_DMA,
f9d5cbb3 578 GP100_DISP_CORE_CHANNEL_DMA,
db1eb528 579 GM200_DISP_CORE_CHANNEL_DMA,
648d4dfd
BS
580 GM107_DISP_CORE_CHANNEL_DMA,
581 GK110_DISP_CORE_CHANNEL_DMA,
582 GK104_DISP_CORE_CHANNEL_DMA,
583 GF110_DISP_CORE_CHANNEL_DMA,
584 GT214_DISP_CORE_CHANNEL_DMA,
585 GT206_DISP_CORE_CHANNEL_DMA,
586 GT200_DISP_CORE_CHANNEL_DMA,
587 G82_DISP_CORE_CHANNEL_DMA,
588 NV50_DISP_CORE_CHANNEL_DMA,
410f3ec6
BS
589 0
590 };
591
a01ca78c
BS
592 return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
593 syncbuf, &core->base);
410f3ec6
BS
594}
595
596/******************************************************************************
597 * Base
598 *****************************************************************************/
b5a794b0 599
e225f446
BS
600struct nv50_sync {
601 struct nv50_dmac base;
9f9bdaaf
BS
602 u32 addr;
603 u32 data;
3376ee37
BS
604};
605
410f3ec6 606static int
a01ca78c
BS
607nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
608 int head, u64 syncbuf, struct nv50_sync *base)
410f3ec6 609{
648d4dfd
BS
610 struct nv50_disp_base_channel_dma_v0 args = {
611 .pushbuf = 0xb0007c00 | head,
410f3ec6
BS
612 .head = head,
613 };
315a8b2e 614 static const s32 oclass[] = {
648d4dfd
BS
615 GK110_DISP_BASE_CHANNEL_DMA,
616 GK104_DISP_BASE_CHANNEL_DMA,
617 GF110_DISP_BASE_CHANNEL_DMA,
618 GT214_DISP_BASE_CHANNEL_DMA,
619 GT200_DISP_BASE_CHANNEL_DMA,
620 G82_DISP_BASE_CHANNEL_DMA,
621 NV50_DISP_BASE_CHANNEL_DMA,
410f3ec6
BS
622 0
623 };
624
a01ca78c 625 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
410f3ec6
BS
626 syncbuf, &base->base);
627}
628
629/******************************************************************************
630 * Overlay
631 *****************************************************************************/
632
e225f446
BS
633struct nv50_ovly {
634 struct nv50_dmac base;
b5a794b0 635};
f20ce962 636
410f3ec6 637static int
a01ca78c
BS
638nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
639 int head, u64 syncbuf, struct nv50_ovly *ovly)
410f3ec6 640{
648d4dfd
BS
641 struct nv50_disp_overlay_channel_dma_v0 args = {
642 .pushbuf = 0xb0007e00 | head,
410f3ec6
BS
643 .head = head,
644 };
315a8b2e 645 static const s32 oclass[] = {
648d4dfd
BS
646 GK104_DISP_OVERLAY_CONTROL_DMA,
647 GF110_DISP_OVERLAY_CONTROL_DMA,
648 GT214_DISP_OVERLAY_CHANNEL_DMA,
649 GT200_DISP_OVERLAY_CHANNEL_DMA,
650 G82_DISP_OVERLAY_CHANNEL_DMA,
651 NV50_DISP_OVERLAY_CHANNEL_DMA,
410f3ec6
BS
652 0
653 };
654
a01ca78c 655 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
410f3ec6
BS
656 syncbuf, &ovly->base);
657}
26f6d88b 658
e225f446 659struct nv50_head {
dd0e3d53 660 struct nouveau_crtc base;
e225f446
BS
661 struct nv50_ovly ovly;
662 struct nv50_oimm oimm;
b5a794b0
BS
663};
664
e225f446 665#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
e225f446
BS
666#define nv50_ovly(c) (&nv50_head(c)->ovly)
667#define nv50_oimm(c) (&nv50_head(c)->oimm)
668#define nv50_chan(c) (&(c)->base.base)
0ad72863
BS
669#define nv50_vers(c) nv50_chan(c)->user.oclass
670
e225f446 671struct nv50_disp {
0ad72863 672 struct nvif_object *disp;
e225f446 673 struct nv50_mast mast;
b5a794b0 674
b5a794b0 675 struct nouveau_bo *sync;
839ca903
BS
676
677 struct mutex mutex;
dd0e3d53
BS
678};
679
e225f446
BS
680static struct nv50_disp *
681nv50_disp(struct drm_device *dev)
26f6d88b 682{
77145f1c 683 return nouveau_display(dev)->priv;
26f6d88b
BS
684}
685
e225f446 686#define nv50_mast(d) (&nv50_disp(d)->mast)
b5a794b0 687
bdb8c212
BS
688/******************************************************************************
689 * EVO channel helpers
690 *****************************************************************************/
51beb428 691static u32 *
b5a794b0 692evo_wait(void *evoc, int nr)
51beb428 693{
e225f446 694 struct nv50_dmac *dmac = evoc;
a01ca78c 695 struct nvif_device *device = dmac->base.device;
0ad72863 696 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
51beb428 697
59ad1465 698 mutex_lock(&dmac->lock);
de8268c5 699 if (put + nr >= (PAGE_SIZE / 4) - 8) {
b5a794b0 700 dmac->ptr[put] = 0x20000000;
51beb428 701
0ad72863 702 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
54442040
BS
703 if (nvif_msec(device, 2000,
704 if (!nvif_rd32(&dmac->base.user, 0x0004))
705 break;
706 ) < 0) {
59ad1465 707 mutex_unlock(&dmac->lock);
8dfe162a 708 pr_err("nouveau: evo channel stalled\n");
51beb428
BS
709 return NULL;
710 }
711
712 put = 0;
713 }
714
b5a794b0 715 return dmac->ptr + put;
51beb428
BS
716}
717
718static void
b5a794b0 719evo_kick(u32 *push, void *evoc)
51beb428 720{
e225f446 721 struct nv50_dmac *dmac = evoc;
0ad72863 722 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
59ad1465 723 mutex_unlock(&dmac->lock);
51beb428
BS
724}
725
8dfe162a
JP
726#define evo_mthd(p, m, s) do { \
727 const u32 _m = (m), _s = (s); \
728 if (drm_debug & DRM_UT_KMS) \
729 pr_err("%04x %d %s\n", _m, _s, __func__); \
730 *((p)++) = ((_s << 18) | _m); \
2b1930c3 731} while(0)
7f55a072 732
8dfe162a
JP
733#define evo_data(p, d) do { \
734 const u32 _d = (d); \
735 if (drm_debug & DRM_UT_KMS) \
736 pr_err("\t%08x\n", _d); \
737 *((p)++) = _d; \
2b1930c3 738} while(0)
51beb428 739
973f10c2
BS
740/******************************************************************************
741 * Plane
742 *****************************************************************************/
743#define nv50_wndw(p) container_of((p), struct nv50_wndw, plane)
744
745struct nv50_wndw {
746 const struct nv50_wndw_func *func;
747 struct nv50_dmac *dmac;
748
749 struct drm_plane plane;
750
751 struct nvif_notify notify;
752 u16 ntfy;
753 u16 sema;
754 u32 data;
973f10c2
BS
755};
756
757struct nv50_wndw_func {
758 void *(*dtor)(struct nv50_wndw *);
759 int (*acquire)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
760 struct nv50_head_atom *asyh);
761 void (*release)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
762 struct nv50_head_atom *asyh);
763 void (*prepare)(struct nv50_wndw *, struct nv50_head_atom *asyh,
764 struct nv50_wndw_atom *asyw);
765
766 void (*sema_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
767 void (*sema_clr)(struct nv50_wndw *);
768 void (*ntfy_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
769 void (*ntfy_clr)(struct nv50_wndw *);
770 int (*ntfy_wait_begun)(struct nv50_wndw *, struct nv50_wndw_atom *);
771 void (*image_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
772 void (*image_clr)(struct nv50_wndw *);
773 void (*lut)(struct nv50_wndw *, struct nv50_wndw_atom *);
774 void (*point)(struct nv50_wndw *, struct nv50_wndw_atom *);
775
776 u32 (*update)(struct nv50_wndw *, u32 interlock);
777};
778
779static int
780nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
781{
782 if (asyw->set.ntfy)
783 return wndw->func->ntfy_wait_begun(wndw, asyw);
784 return 0;
785}
786
787static u32
788nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 interlock, bool flush,
789 struct nv50_wndw_atom *asyw)
790{
791 if (asyw->clr.sema && (!asyw->set.sema || flush))
792 wndw->func->sema_clr(wndw);
793 if (asyw->clr.ntfy && (!asyw->set.ntfy || flush))
794 wndw->func->ntfy_clr(wndw);
795 if (asyw->clr.image && (!asyw->set.image || flush))
796 wndw->func->image_clr(wndw);
797
798 return flush ? wndw->func->update(wndw, interlock) : 0;
799}
800
801static u32
802nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 interlock,
803 struct nv50_wndw_atom *asyw)
804{
805 if (interlock) {
806 asyw->image.mode = 0;
807 asyw->image.interval = 1;
808 }
809
810 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
811 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
812 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
813 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
814 if (asyw->set.point) wndw->func->point (wndw, asyw);
815
816 return wndw->func->update(wndw, interlock);
817}
818
819static void
820nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
821 struct nv50_wndw_atom *asyw,
822 struct nv50_head_atom *asyh)
823{
824 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
825 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
826 wndw->func->release(wndw, asyw, asyh);
827 asyw->ntfy.handle = 0;
828 asyw->sema.handle = 0;
829}
830
831static int
832nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
833 struct nv50_wndw_atom *asyw,
612fb5d9
AG
834 struct nv50_head_atom *asyh,
835 u32 pflip_flags)
973f10c2
BS
836{
837 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
838 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
839 int ret;
840
841 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
842 asyw->clip.x1 = 0;
843 asyw->clip.y1 = 0;
844 asyw->clip.x2 = asyh->state.mode.hdisplay;
845 asyw->clip.y2 = asyh->state.mode.vdisplay;
846
847 asyw->image.w = fb->base.width;
848 asyw->image.h = fb->base.height;
849 asyw->image.kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
612fb5d9
AG
850
851 asyw->interval = pflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ? 0 : 1;
852
973f10c2
BS
853 if (asyw->image.kind) {
854 asyw->image.layout = 0;
1167c6bc 855 if (drm->client.device.info.chipset >= 0xc0)
973f10c2
BS
856 asyw->image.block = fb->nvbo->tile_mode >> 4;
857 else
858 asyw->image.block = fb->nvbo->tile_mode;
859 asyw->image.pitch = (fb->base.pitches[0] / 4) << 4;
860 } else {
861 asyw->image.layout = 1;
862 asyw->image.block = 0;
863 asyw->image.pitch = fb->base.pitches[0];
864 }
865
866 ret = wndw->func->acquire(wndw, asyw, asyh);
867 if (ret)
868 return ret;
869
870 if (asyw->set.image) {
871 if (!(asyw->image.mode = asyw->interval ? 0 : 1))
872 asyw->image.interval = asyw->interval;
873 else
874 asyw->image.interval = 0;
875 }
876
877 return 0;
878}
879
880static int
881nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
882{
883 struct nouveau_drm *drm = nouveau_drm(plane->dev);
884 struct nv50_wndw *wndw = nv50_wndw(plane);
839ca903
BS
885 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
886 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
973f10c2
BS
887 struct nv50_head_atom *harm = NULL, *asyh = NULL;
888 bool varm = false, asyv = false, asym = false;
889 int ret;
612fb5d9 890 u32 pflip_flags = 0;
973f10c2 891
973f10c2
BS
892 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
893 if (asyw->state.crtc) {
839ca903 894 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
973f10c2
BS
895 if (IS_ERR(asyh))
896 return PTR_ERR(asyh);
897 asym = drm_atomic_crtc_needs_modeset(&asyh->state);
898 asyv = asyh->state.active;
612fb5d9 899 pflip_flags = asyh->state.pageflip_flags;
973f10c2
BS
900 }
901
902 if (armw->state.crtc) {
839ca903 903 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
973f10c2
BS
904 if (IS_ERR(harm))
905 return PTR_ERR(harm);
839ca903 906 varm = harm->state.crtc->state->active;
973f10c2
BS
907 }
908
909 if (asyv) {
910 asyw->point.x = asyw->state.crtc_x;
911 asyw->point.y = asyw->state.crtc_y;
912 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
913 asyw->set.point = true;
914
915 if (!varm || asym || armw->state.fb != asyw->state.fb) {
612fb5d9
AG
916 ret = nv50_wndw_atomic_check_acquire(
917 wndw, asyw, asyh, pflip_flags);
973f10c2
BS
918 if (ret)
919 return ret;
920 }
921 } else
922 if (varm) {
923 nv50_wndw_atomic_check_release(wndw, asyw, harm);
924 } else {
925 return 0;
926 }
927
928 if (!asyv || asym) {
929 asyw->clr.ntfy = armw->ntfy.handle != 0;
930 asyw->clr.sema = armw->sema.handle != 0;
931 if (wndw->func->image_clr)
932 asyw->clr.image = armw->image.handle != 0;
933 asyw->set.lut = wndw->func->lut && asyv;
934 }
935
973f10c2
BS
936 return 0;
937}
938
839ca903
BS
939static void
940nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
941{
942 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
943 struct nouveau_drm *drm = nouveau_drm(plane->dev);
944
945 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
946 if (!old_state->fb)
947 return;
948
949 nouveau_bo_unpin(fb->nvbo);
950}
951
952static int
953nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
954{
955 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
956 struct nouveau_drm *drm = nouveau_drm(plane->dev);
957 struct nv50_wndw *wndw = nv50_wndw(plane);
958 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
959 struct nv50_head_atom *asyh;
960 struct nv50_dmac_ctxdma *ctxdma;
839ca903
BS
961 int ret;
962
963 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
964 if (!asyw->state.fb)
965 return 0;
839ca903
BS
966
967 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
968 if (ret)
969 return ret;
970
f00f0e21 971 ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, fb);
839ca903
BS
972 if (IS_ERR(ctxdma)) {
973 nouveau_bo_unpin(fb->nvbo);
974 return PTR_ERR(ctxdma);
975 }
976
977 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
978 asyw->image.handle = ctxdma->object.handle;
979 asyw->image.offset = fb->nvbo->bo.offset;
980
981 if (wndw->func->prepare) {
982 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
983 if (IS_ERR(asyh))
984 return PTR_ERR(asyh);
985
986 wndw->func->prepare(wndw, asyh, asyw);
987 }
988
989 return 0;
990}
991
992static const struct drm_plane_helper_funcs
993nv50_wndw_helper = {
994 .prepare_fb = nv50_wndw_prepare_fb,
995 .cleanup_fb = nv50_wndw_cleanup_fb,
996 .atomic_check = nv50_wndw_atomic_check,
997};
998
973f10c2
BS
999static void
1000nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
1001 struct drm_plane_state *state)
1002{
1003 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
1004 __drm_atomic_helper_plane_destroy_state(&asyw->state);
1005 dma_fence_put(asyw->state.fence);
1006 kfree(asyw);
1007}
1008
1009static struct drm_plane_state *
1010nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
1011{
1012 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
1013 struct nv50_wndw_atom *asyw;
1014 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
1015 return NULL;
1016 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
1017 asyw->state.fence = NULL;
1018 asyw->interval = 1;
1019 asyw->sema = armw->sema;
1020 asyw->ntfy = armw->ntfy;
1021 asyw->image = armw->image;
1022 asyw->point = armw->point;
1023 asyw->lut = armw->lut;
1024 asyw->clr.mask = 0;
1025 asyw->set.mask = 0;
1026 return &asyw->state;
1027}
1028
1029static void
1030nv50_wndw_reset(struct drm_plane *plane)
1031{
1032 struct nv50_wndw_atom *asyw;
1033
1034 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
1035 return;
1036
1037 if (plane->state)
1038 plane->funcs->atomic_destroy_state(plane, plane->state);
1039 plane->state = &asyw->state;
1040 plane->state->plane = plane;
1041 plane->state->rotation = DRM_ROTATE_0;
1042}
1043
1044static void
1045nv50_wndw_destroy(struct drm_plane *plane)
1046{
1047 struct nv50_wndw *wndw = nv50_wndw(plane);
1048 void *data;
1049 nvif_notify_fini(&wndw->notify);
1050 data = wndw->func->dtor(wndw);
1051 drm_plane_cleanup(&wndw->plane);
1052 kfree(data);
1053}
1054
1055static const struct drm_plane_funcs
1056nv50_wndw = {
839ca903
BS
1057 .update_plane = drm_atomic_helper_update_plane,
1058 .disable_plane = drm_atomic_helper_disable_plane,
973f10c2
BS
1059 .destroy = nv50_wndw_destroy,
1060 .reset = nv50_wndw_reset,
1061 .set_property = drm_atomic_helper_plane_set_property,
1062 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
1063 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
1064};
1065
1066static void
1067nv50_wndw_fini(struct nv50_wndw *wndw)
1068{
1069 nvif_notify_put(&wndw->notify);
1070}
1071
1072static void
1073nv50_wndw_init(struct nv50_wndw *wndw)
1074{
1075 nvif_notify_get(&wndw->notify);
1076}
1077
1078static int
1079nv50_wndw_ctor(const struct nv50_wndw_func *func, struct drm_device *dev,
1080 enum drm_plane_type type, const char *name, int index,
1081 struct nv50_dmac *dmac, const u32 *format, int nformat,
1082 struct nv50_wndw *wndw)
1083{
1084 int ret;
1085
1086 wndw->func = func;
1087 wndw->dmac = dmac;
1088
1089 ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw, format,
1090 nformat, type, "%s-%d", name, index);
1091 if (ret)
1092 return ret;
1093
839ca903 1094 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
973f10c2
BS
1095 return 0;
1096}
1097
22e927d2
BS
1098/******************************************************************************
1099 * Cursor plane
1100 *****************************************************************************/
1101#define nv50_curs(p) container_of((p), struct nv50_curs, wndw)
1102
1103struct nv50_curs {
1104 struct nv50_wndw wndw;
1105 struct nvif_object chan;
1106};
1107
1108static u32
1109nv50_curs_update(struct nv50_wndw *wndw, u32 interlock)
1110{
1111 struct nv50_curs *curs = nv50_curs(wndw);
1112 nvif_wr32(&curs->chan, 0x0080, 0x00000000);
1113 return 0;
1114}
1115
1116static void
1117nv50_curs_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1118{
1119 struct nv50_curs *curs = nv50_curs(wndw);
1120 nvif_wr32(&curs->chan, 0x0084, (asyw->point.y << 16) | asyw->point.x);
1121}
1122
1123static void
1124nv50_curs_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
1125 struct nv50_wndw_atom *asyw)
1126{
1127 asyh->curs.handle = nv50_disp(wndw->plane.dev)->mast.base.vram.handle;
1128 asyh->curs.offset = asyw->image.offset;
1129 asyh->set.curs = asyh->curs.visible;
1130}
1131
1132static void
1133nv50_curs_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1134 struct nv50_head_atom *asyh)
1135{
1136 asyh->curs.visible = false;
1137}
1138
1139static int
1140nv50_curs_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1141 struct nv50_head_atom *asyh)
1142{
1143 int ret;
1144
1145 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1146 DRM_PLANE_HELPER_NO_SCALING,
1147 DRM_PLANE_HELPER_NO_SCALING,
1148 true, true);
1149 asyh->curs.visible = asyw->state.visible;
1150 if (ret || !asyh->curs.visible)
1151 return ret;
1152
1153 switch (asyw->state.fb->width) {
1154 case 32: asyh->curs.layout = 0; break;
1155 case 64: asyh->curs.layout = 1; break;
1156 default:
1157 return -EINVAL;
1158 }
1159
1160 if (asyw->state.fb->width != asyw->state.fb->height)
1161 return -EINVAL;
1162
438b74a5 1163 switch (asyw->state.fb->format->format) {
22e927d2
BS
1164 case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
1165 default:
1166 WARN_ON(1);
1167 return -EINVAL;
1168 }
1169
1170 return 0;
1171}
1172
1173static void *
1174nv50_curs_dtor(struct nv50_wndw *wndw)
1175{
1176 struct nv50_curs *curs = nv50_curs(wndw);
1177 nvif_object_fini(&curs->chan);
1178 return curs;
1179}
1180
1181static const u32
1182nv50_curs_format[] = {
1183 DRM_FORMAT_ARGB8888,
1184};
1185
1186static const struct nv50_wndw_func
1187nv50_curs = {
1188 .dtor = nv50_curs_dtor,
1189 .acquire = nv50_curs_acquire,
1190 .release = nv50_curs_release,
1191 .prepare = nv50_curs_prepare,
1192 .point = nv50_curs_point,
1193 .update = nv50_curs_update,
1194};
1195
1196static int
1197nv50_curs_new(struct nouveau_drm *drm, struct nv50_head *head,
1198 struct nv50_curs **pcurs)
1199{
1200 static const struct nvif_mclass curses[] = {
1201 { GK104_DISP_CURSOR, 0 },
1202 { GF110_DISP_CURSOR, 0 },
1203 { GT214_DISP_CURSOR, 0 },
1204 { G82_DISP_CURSOR, 0 },
1205 { NV50_DISP_CURSOR, 0 },
1206 {}
1207 };
1208 struct nv50_disp_cursor_v0 args = {
1209 .head = head->base.index,
1210 };
1211 struct nv50_disp *disp = nv50_disp(drm->dev);
1212 struct nv50_curs *curs;
1213 int cid, ret;
1214
1215 cid = nvif_mclass(disp->disp, curses);
1216 if (cid < 0) {
1217 NV_ERROR(drm, "No supported cursor immediate class\n");
1218 return cid;
1219 }
1220
1221 if (!(curs = *pcurs = kzalloc(sizeof(*curs), GFP_KERNEL)))
1222 return -ENOMEM;
1223
1224 ret = nv50_wndw_ctor(&nv50_curs, drm->dev, DRM_PLANE_TYPE_CURSOR,
1225 "curs", head->base.index, &disp->mast.base,
1226 nv50_curs_format, ARRAY_SIZE(nv50_curs_format),
1227 &curs->wndw);
1228 if (ret) {
1229 kfree(curs);
1230 return ret;
1231 }
1232
1233 ret = nvif_object_init(disp->disp, 0, curses[cid].oclass, &args,
1234 sizeof(args), &curs->chan);
1235 if (ret) {
1236 NV_ERROR(drm, "curs%04x allocation failed: %d\n",
1237 curses[cid].oclass, ret);
1238 return ret;
1239 }
1240
1241 return 0;
1242}
1243
973f10c2
BS
1244/******************************************************************************
1245 * Primary plane
1246 *****************************************************************************/
1247#define nv50_base(p) container_of((p), struct nv50_base, wndw)
1248
1249struct nv50_base {
1250 struct nv50_wndw wndw;
1251 struct nv50_sync chan;
1252 int id;
1253};
1254
1255static int
1256nv50_base_notify(struct nvif_notify *notify)
1257{
1258 return NVIF_NOTIFY_KEEP;
1259}
1260
1261static void
1262nv50_base_lut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1263{
1264 struct nv50_base *base = nv50_base(wndw);
1265 u32 *push;
1266 if ((push = evo_wait(&base->chan, 2))) {
1267 evo_mthd(push, 0x00e0, 1);
1268 evo_data(push, asyw->lut.enable << 30);
1269 evo_kick(push, &base->chan);
1270 }
1271}
1272
1273static void
1274nv50_base_image_clr(struct nv50_wndw *wndw)
1275{
1276 struct nv50_base *base = nv50_base(wndw);
1277 u32 *push;
1278 if ((push = evo_wait(&base->chan, 4))) {
1279 evo_mthd(push, 0x0084, 1);
1280 evo_data(push, 0x00000000);
1281 evo_mthd(push, 0x00c0, 1);
1282 evo_data(push, 0x00000000);
1283 evo_kick(push, &base->chan);
1284 }
1285}
1286
1287static void
1288nv50_base_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1289{
1290 struct nv50_base *base = nv50_base(wndw);
1291 const s32 oclass = base->chan.base.base.user.oclass;
1292 u32 *push;
1293 if ((push = evo_wait(&base->chan, 10))) {
1294 evo_mthd(push, 0x0084, 1);
1295 evo_data(push, (asyw->image.mode << 8) |
1296 (asyw->image.interval << 4));
1297 evo_mthd(push, 0x00c0, 1);
1298 evo_data(push, asyw->image.handle);
1299 if (oclass < G82_DISP_BASE_CHANNEL_DMA) {
1300 evo_mthd(push, 0x0800, 5);
1301 evo_data(push, asyw->image.offset >> 8);
1302 evo_data(push, 0x00000000);
1303 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1304 evo_data(push, (asyw->image.layout << 20) |
1305 asyw->image.pitch |
1306 asyw->image.block);
1307 evo_data(push, (asyw->image.kind << 16) |
1308 (asyw->image.format << 8));
1309 } else
1310 if (oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1311 evo_mthd(push, 0x0800, 5);
1312 evo_data(push, asyw->image.offset >> 8);
1313 evo_data(push, 0x00000000);
1314 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1315 evo_data(push, (asyw->image.layout << 20) |
1316 asyw->image.pitch |
1317 asyw->image.block);
1318 evo_data(push, asyw->image.format << 8);
1319 } else {
1320 evo_mthd(push, 0x0400, 5);
1321 evo_data(push, asyw->image.offset >> 8);
1322 evo_data(push, 0x00000000);
1323 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1324 evo_data(push, (asyw->image.layout << 24) |
1325 asyw->image.pitch |
1326 asyw->image.block);
1327 evo_data(push, asyw->image.format << 8);
1328 }
1329 evo_kick(push, &base->chan);
1330 }
1331}
1332
1333static void
1334nv50_base_ntfy_clr(struct nv50_wndw *wndw)
1335{
1336 struct nv50_base *base = nv50_base(wndw);
1337 u32 *push;
1338 if ((push = evo_wait(&base->chan, 2))) {
1339 evo_mthd(push, 0x00a4, 1);
1340 evo_data(push, 0x00000000);
1341 evo_kick(push, &base->chan);
1342 }
1343}
1344
1345static void
1346nv50_base_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1347{
1348 struct nv50_base *base = nv50_base(wndw);
1349 u32 *push;
1350 if ((push = evo_wait(&base->chan, 3))) {
1351 evo_mthd(push, 0x00a0, 2);
1352 evo_data(push, (asyw->ntfy.awaken << 30) | asyw->ntfy.offset);
1353 evo_data(push, asyw->ntfy.handle);
1354 evo_kick(push, &base->chan);
1355 }
1356}
1357
1358static void
1359nv50_base_sema_clr(struct nv50_wndw *wndw)
1360{
1361 struct nv50_base *base = nv50_base(wndw);
1362 u32 *push;
1363 if ((push = evo_wait(&base->chan, 2))) {
1364 evo_mthd(push, 0x0094, 1);
1365 evo_data(push, 0x00000000);
1366 evo_kick(push, &base->chan);
1367 }
1368}
1369
1370static void
1371nv50_base_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1372{
1373 struct nv50_base *base = nv50_base(wndw);
1374 u32 *push;
1375 if ((push = evo_wait(&base->chan, 5))) {
1376 evo_mthd(push, 0x0088, 4);
1377 evo_data(push, asyw->sema.offset);
1378 evo_data(push, asyw->sema.acquire);
1379 evo_data(push, asyw->sema.release);
1380 evo_data(push, asyw->sema.handle);
1381 evo_kick(push, &base->chan);
1382 }
1383}
1384
1385static u32
1386nv50_base_update(struct nv50_wndw *wndw, u32 interlock)
1387{
1388 struct nv50_base *base = nv50_base(wndw);
1389 u32 *push;
1390
1391 if (!(push = evo_wait(&base->chan, 2)))
1392 return 0;
1393 evo_mthd(push, 0x0080, 1);
1394 evo_data(push, interlock);
1395 evo_kick(push, &base->chan);
1396
1397 if (base->chan.base.base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA)
1398 return interlock ? 2 << (base->id * 8) : 0;
1399 return interlock ? 2 << (base->id * 4) : 0;
1400}
1401
1402static int
1403nv50_base_ntfy_wait_begun(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1404{
1405 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
1406 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1167c6bc 1407 if (nvif_msec(&drm->client.device, 2000ULL,
973f10c2
BS
1408 u32 data = nouveau_bo_rd32(disp->sync, asyw->ntfy.offset / 4);
1409 if ((data & 0xc0000000) == 0x40000000)
1410 break;
1411 usleep_range(1, 2);
1412 ) < 0)
1413 return -ETIMEDOUT;
1414 return 0;
1415}
1416
1417static void
1418nv50_base_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1419 struct nv50_head_atom *asyh)
1420{
1421 asyh->base.cpp = 0;
1422}
1423
1424static int
1425nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1426 struct nv50_head_atom *asyh)
1427{
9857ecbe 1428 const struct drm_framebuffer *fb = asyw->state.fb;
973f10c2
BS
1429 int ret;
1430
9857ecbe 1431 if (!fb->format->depth)
973f10c2
BS
1432 return -EINVAL;
1433
1434 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1435 DRM_PLANE_HELPER_NO_SCALING,
1436 DRM_PLANE_HELPER_NO_SCALING,
1437 false, true);
1438 if (ret)
1439 return ret;
1440
9857ecbe
VS
1441 asyh->base.depth = fb->format->depth;
1442 asyh->base.cpp = fb->format->cpp[0];
973f10c2
BS
1443 asyh->base.x = asyw->state.src.x1 >> 16;
1444 asyh->base.y = asyw->state.src.y1 >> 16;
1445 asyh->base.w = asyw->state.fb->width;
1446 asyh->base.h = asyw->state.fb->height;
1447
438b74a5 1448 switch (fb->format->format) {
973f10c2
BS
1449 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
1450 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
1451 case DRM_FORMAT_XRGB1555 :
1452 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
1453 case DRM_FORMAT_XRGB8888 :
1454 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
1455 case DRM_FORMAT_XBGR2101010:
1456 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
1457 case DRM_FORMAT_XBGR8888 :
1458 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
1459 default:
1460 WARN_ON(1);
1461 return -EINVAL;
1462 }
1463
1464 asyw->lut.enable = 1;
1465 asyw->set.image = true;
1466 return 0;
1467}
1468
1469static void *
1470nv50_base_dtor(struct nv50_wndw *wndw)
1471{
1472 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1473 struct nv50_base *base = nv50_base(wndw);
1474 nv50_dmac_destroy(&base->chan.base, disp->disp);
1475 return base;
1476}
1477
1478static const u32
1479nv50_base_format[] = {
1480 DRM_FORMAT_C8,
1481 DRM_FORMAT_RGB565,
1482 DRM_FORMAT_XRGB1555,
1483 DRM_FORMAT_ARGB1555,
1484 DRM_FORMAT_XRGB8888,
1485 DRM_FORMAT_ARGB8888,
1486 DRM_FORMAT_XBGR2101010,
1487 DRM_FORMAT_ABGR2101010,
1488 DRM_FORMAT_XBGR8888,
1489 DRM_FORMAT_ABGR8888,
1490};
1491
1492static const struct nv50_wndw_func
1493nv50_base = {
1494 .dtor = nv50_base_dtor,
1495 .acquire = nv50_base_acquire,
1496 .release = nv50_base_release,
1497 .sema_set = nv50_base_sema_set,
1498 .sema_clr = nv50_base_sema_clr,
1499 .ntfy_set = nv50_base_ntfy_set,
1500 .ntfy_clr = nv50_base_ntfy_clr,
1501 .ntfy_wait_begun = nv50_base_ntfy_wait_begun,
1502 .image_set = nv50_base_image_set,
1503 .image_clr = nv50_base_image_clr,
1504 .lut = nv50_base_lut,
1505 .update = nv50_base_update,
1506};
1507
1508static int
1509nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
1510 struct nv50_base **pbase)
1511{
1512 struct nv50_disp *disp = nv50_disp(drm->dev);
1513 struct nv50_base *base;
1514 int ret;
1515
1516 if (!(base = *pbase = kzalloc(sizeof(*base), GFP_KERNEL)))
1517 return -ENOMEM;
1518 base->id = head->base.index;
1519 base->wndw.ntfy = EVO_FLIP_NTFY0(base->id);
1520 base->wndw.sema = EVO_FLIP_SEM0(base->id);
1521 base->wndw.data = 0x00000000;
1522
1523 ret = nv50_wndw_ctor(&nv50_base, drm->dev, DRM_PLANE_TYPE_PRIMARY,
1524 "base", base->id, &base->chan.base,
1525 nv50_base_format, ARRAY_SIZE(nv50_base_format),
1526 &base->wndw);
1527 if (ret) {
1528 kfree(base);
1529 return ret;
1530 }
1531
1167c6bc 1532 ret = nv50_base_create(&drm->client.device, disp->disp, base->id,
973f10c2
BS
1533 disp->sync->bo.offset, &base->chan);
1534 if (ret)
1535 return ret;
1536
1537 return nvif_notify_init(&base->chan.base.base.user, nv50_base_notify,
1538 false,
1539 NV50_DISP_BASE_CHANNEL_DMA_V0_NTFY_UEVENT,
1540 &(struct nvif_notify_uevent_req) {},
1541 sizeof(struct nvif_notify_uevent_req),
1542 sizeof(struct nvif_notify_uevent_rep),
1543 &base->wndw.notify);
1544}
1545
3dbd036b
BS
1546/******************************************************************************
1547 * Head
1548 *****************************************************************************/
7e08d67c
BS
1549static void
1550nv50_head_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
1551{
1552 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1553 u32 *push;
1554 if ((push = evo_wait(core, 2))) {
1555 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1556 evo_mthd(push, 0x08a8 + (head->base.index * 0x400), 1);
1557 else
1558 evo_mthd(push, 0x0498 + (head->base.index * 0x300), 1);
1559 evo_data(push, (asyh->procamp.sat.sin << 20) |
1560 (asyh->procamp.sat.cos << 8));
1561 evo_kick(push, core);
1562 }
1563}
1564
7e91833d
BS
1565static void
1566nv50_head_dither(struct nv50_head *head, struct nv50_head_atom *asyh)
1567{
1568 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1569 u32 *push;
1570 if ((push = evo_wait(core, 2))) {
1571 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1572 evo_mthd(push, 0x08a0 + (head->base.index * 0x0400), 1);
1573 else
1574 if (core->base.user.oclass < GK104_DISP_CORE_CHANNEL_DMA)
1575 evo_mthd(push, 0x0490 + (head->base.index * 0x0300), 1);
1576 else
1577 evo_mthd(push, 0x04a0 + (head->base.index * 0x0300), 1);
1578 evo_data(push, (asyh->dither.mode << 3) |
1579 (asyh->dither.bits << 1) |
1580 asyh->dither.enable);
1581 evo_kick(push, core);
1582 }
1583}
1584
6bbab3b6
BS
1585static void
1586nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
1587{
1588 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1589 u32 bounds = 0;
1590 u32 *push;
1591
1592 if (asyh->base.cpp) {
1593 switch (asyh->base.cpp) {
1594 case 8: bounds |= 0x00000500; break;
1595 case 4: bounds |= 0x00000300; break;
1596 case 2: bounds |= 0x00000100; break;
1597 default:
1598 WARN_ON(1);
1599 break;
1600 }
1601 bounds |= 0x00000001;
1602 }
1603
1604 if ((push = evo_wait(core, 2))) {
1605 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1606 evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
1607 else
1608 evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
1609 evo_data(push, bounds);
1610 evo_kick(push, core);
1611 }
1612}
1613
1614static void
1615nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
1616{
1617 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1618 u32 bounds = 0;
1619 u32 *push;
1620
1621 if (asyh->base.cpp) {
1622 switch (asyh->base.cpp) {
1623 case 8: bounds |= 0x00000500; break;
1624 case 4: bounds |= 0x00000300; break;
1625 case 2: bounds |= 0x00000100; break;
1626 case 1: bounds |= 0x00000000; break;
1627 default:
1628 WARN_ON(1);
1629 break;
1630 }
1631 bounds |= 0x00000001;
1632 }
1633
1634 if ((push = evo_wait(core, 2))) {
1635 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1636 evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
1637 else
1638 evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
1639 evo_data(push, bounds);
1640 evo_kick(push, core);
1641 }
1642}
1643
ea8ee390
BS
1644static void
1645nv50_head_curs_clr(struct nv50_head *head)
1646{
1647 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1648 u32 *push;
1649 if ((push = evo_wait(core, 4))) {
1650 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1651 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1652 evo_data(push, 0x05000000);
1653 } else
1654 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1655 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1656 evo_data(push, 0x05000000);
1657 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1658 evo_data(push, 0x00000000);
1659 } else {
1660 evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
1661 evo_data(push, 0x05000000);
1662 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1663 evo_data(push, 0x00000000);
1664 }
1665 evo_kick(push, core);
1666 }
1667}
1668
1669static void
1670nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1671{
1672 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1673 u32 *push;
1674 if ((push = evo_wait(core, 5))) {
1675 if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
1676 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1677 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1678 (asyh->curs.format << 24));
1679 evo_data(push, asyh->curs.offset >> 8);
1680 } else
1681 if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1682 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1683 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1684 (asyh->curs.format << 24));
1685 evo_data(push, asyh->curs.offset >> 8);
1686 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1687 evo_data(push, asyh->curs.handle);
1688 } else {
1689 evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
1690 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1691 (asyh->curs.format << 24));
1692 evo_data(push, asyh->curs.offset >> 8);
1693 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1694 evo_data(push, asyh->curs.handle);
1695 }
1696 evo_kick(push, core);
1697 }
1698}
1699
ad633619
BS
1700static void
1701nv50_head_core_clr(struct nv50_head *head)
1702{
1703 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1704 u32 *push;
1705 if ((push = evo_wait(core, 2))) {
1706 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1707 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
1708 else
1709 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
1710 evo_data(push, 0x00000000);
1711 evo_kick(push, core);
1712 }
1713}
1714
1715static void
1716nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1717{
1718 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1719 u32 *push;
1720 if ((push = evo_wait(core, 9))) {
1721 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1722 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1723 evo_data(push, asyh->core.offset >> 8);
1724 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1725 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1726 evo_data(push, asyh->core.layout << 20 |
1727 (asyh->core.pitch >> 8) << 8 |
1728 asyh->core.block);
1729 evo_data(push, asyh->core.kind << 16 |
1730 asyh->core.format << 8);
1731 evo_data(push, asyh->core.handle);
1732 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1733 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
19d53d01
BS
1734 /* EVO will complain with INVALID_STATE if we have an
1735 * active cursor and (re)specify HeadSetContextDmaIso
1736 * without also updating HeadSetOffsetCursor.
1737 */
1738 asyh->set.curs = asyh->curs.visible;
ad633619
BS
1739 } else
1740 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1741 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1742 evo_data(push, asyh->core.offset >> 8);
1743 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1744 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1745 evo_data(push, asyh->core.layout << 20 |
1746 (asyh->core.pitch >> 8) << 8 |
1747 asyh->core.block);
1748 evo_data(push, asyh->core.format << 8);
1749 evo_data(push, asyh->core.handle);
1750 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1751 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1752 } else {
1753 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
1754 evo_data(push, asyh->core.offset >> 8);
1755 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
1756 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1757 evo_data(push, asyh->core.layout << 24 |
1758 (asyh->core.pitch >> 8) << 8 |
1759 asyh->core.block);
1760 evo_data(push, asyh->core.format << 8);
1761 evo_data(push, asyh->core.handle);
1762 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
1763 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1764 }
1765 evo_kick(push, core);
1766 }
1767}
1768
a7ae1561
BS
1769static void
1770nv50_head_lut_clr(struct nv50_head *head)
1771{
1772 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1773 u32 *push;
1774 if ((push = evo_wait(core, 4))) {
1775 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1776 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1777 evo_data(push, 0x40000000);
1778 } else
1779 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1780 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1781 evo_data(push, 0x40000000);
1782 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1783 evo_data(push, 0x00000000);
1784 } else {
1785 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
1786 evo_data(push, 0x03000000);
1787 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1788 evo_data(push, 0x00000000);
1789 }
1790 evo_kick(push, core);
1791 }
1792}
1793
1794static void
1795nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1796{
1797 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1798 u32 *push;
1799 if ((push = evo_wait(core, 7))) {
1800 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1801 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1802 evo_data(push, 0xc0000000);
1803 evo_data(push, asyh->lut.offset >> 8);
1804 } else
1805 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1806 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1807 evo_data(push, 0xc0000000);
1808 evo_data(push, asyh->lut.offset >> 8);
1809 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1810 evo_data(push, asyh->lut.handle);
1811 } else {
1812 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
1813 evo_data(push, 0x83000000);
1814 evo_data(push, asyh->lut.offset >> 8);
1815 evo_data(push, 0x00000000);
1816 evo_data(push, 0x00000000);
1817 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1818 evo_data(push, asyh->lut.handle);
1819 }
1820 evo_kick(push, core);
1821 }
1822}
1823
3dbd036b
BS
1824static void
1825nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1826{
1827 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1828 struct nv50_head_mode *m = &asyh->mode;
1829 u32 *push;
1830 if ((push = evo_wait(core, 14))) {
1831 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1832 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
1833 evo_data(push, 0x00800000 | m->clock);
1834 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
06ab282f 1835 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 7);
3dbd036b
BS
1836 evo_data(push, 0x00000000);
1837 evo_data(push, (m->v.active << 16) | m->h.active );
1838 evo_data(push, (m->v.synce << 16) | m->h.synce );
1839 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1840 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1841 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
06ab282f 1842 evo_data(push, asyh->mode.v.blankus);
3dbd036b
BS
1843 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
1844 evo_data(push, 0x00000000);
1845 } else {
1846 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
1847 evo_data(push, 0x00000000);
1848 evo_data(push, (m->v.active << 16) | m->h.active );
1849 evo_data(push, (m->v.synce << 16) | m->h.synce );
1850 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1851 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1852 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
1853 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
1854 evo_data(push, 0x00000000); /* ??? */
1855 evo_data(push, 0xffffff00);
1856 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
1857 evo_data(push, m->clock * 1000);
1858 evo_data(push, 0x00200000); /* ??? */
1859 evo_data(push, m->clock * 1000);
1860 }
1861 evo_kick(push, core);
1862 }
1863}
1864
c4e6812c
BS
1865static void
1866nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
1867{
1868 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1869 u32 *push;
1870 if ((push = evo_wait(core, 10))) {
1871 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1872 evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
1873 evo_data(push, 0x00000000);
1874 evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
1875 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1876 evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
1877 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1878 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1879 } else {
1880 evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
1881 evo_data(push, 0x00000000);
1882 evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
1883 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1884 evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
1885 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1886 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1887 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1888 }
1889 evo_kick(push, core);
1890 }
1891}
1892
ad633619
BS
1893static void
1894nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
1895{
a7ae1561
BS
1896 if (asyh->clr.core && (!asyh->set.core || y))
1897 nv50_head_lut_clr(head);
ad633619
BS
1898 if (asyh->clr.core && (!asyh->set.core || y))
1899 nv50_head_core_clr(head);
ea8ee390
BS
1900 if (asyh->clr.curs && (!asyh->set.curs || y))
1901 nv50_head_curs_clr(head);
ad633619
BS
1902}
1903
3dbd036b
BS
1904static void
1905nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1906{
c4e6812c 1907 if (asyh->set.view ) nv50_head_view (head, asyh);
3dbd036b 1908 if (asyh->set.mode ) nv50_head_mode (head, asyh);
a7ae1561 1909 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
ad633619 1910 if (asyh->set.core ) nv50_head_core_set(head, asyh);
ea8ee390 1911 if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
6bbab3b6
BS
1912 if (asyh->set.base ) nv50_head_base (head, asyh);
1913 if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
7e91833d 1914 if (asyh->set.dither ) nv50_head_dither (head, asyh);
7e08d67c
BS
1915 if (asyh->set.procamp) nv50_head_procamp (head, asyh);
1916}
1917
1918static void
1919nv50_head_atomic_check_procamp(struct nv50_head_atom *armh,
1920 struct nv50_head_atom *asyh,
1921 struct nouveau_conn_atom *asyc)
1922{
1923 const int vib = asyc->procamp.color_vibrance - 100;
1924 const int hue = asyc->procamp.vibrant_hue - 90;
1925 const int adj = (vib > 0) ? 50 : 0;
1926 asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff;
1927 asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff;
1928 asyh->set.procamp = true;
7e91833d
BS
1929}
1930
1931static void
1932nv50_head_atomic_check_dither(struct nv50_head_atom *armh,
1933 struct nv50_head_atom *asyh,
1934 struct nouveau_conn_atom *asyc)
1935{
1936 struct drm_connector *connector = asyc->state.connector;
1937 u32 mode = 0x00;
1938
1939 if (asyc->dither.mode == DITHERING_MODE_AUTO) {
1940 if (asyh->base.depth > connector->display_info.bpc * 3)
1941 mode = DITHERING_MODE_DYNAMIC2X2;
1942 } else {
1943 mode = asyc->dither.mode;
1944 }
1945
1946 if (asyc->dither.depth == DITHERING_DEPTH_AUTO) {
1947 if (connector->display_info.bpc >= 8)
1948 mode |= DITHERING_DEPTH_8BPC;
1949 } else {
1950 mode |= asyc->dither.depth;
1951 }
1952
1953 asyh->dither.enable = mode;
1954 asyh->dither.bits = mode >> 1;
1955 asyh->dither.mode = mode >> 3;
1956 asyh->set.dither = true;
3dbd036b
BS
1957}
1958
c4e6812c
BS
1959static void
1960nv50_head_atomic_check_view(struct nv50_head_atom *armh,
1961 struct nv50_head_atom *asyh,
1962 struct nouveau_conn_atom *asyc)
1963{
1964 struct drm_connector *connector = asyc->state.connector;
1965 struct drm_display_mode *omode = &asyh->state.adjusted_mode;
1966 struct drm_display_mode *umode = &asyh->state.mode;
1967 int mode = asyc->scaler.mode;
1968 struct edid *edid;
1969
1970 if (connector->edid_blob_ptr)
1971 edid = (struct edid *)connector->edid_blob_ptr->data;
1972 else
1973 edid = NULL;
1974
1975 if (!asyc->scaler.full) {
1976 if (mode == DRM_MODE_SCALE_NONE)
1977 omode = umode;
1978 } else {
1979 /* Non-EDID LVDS/eDP mode. */
1980 mode = DRM_MODE_SCALE_FULLSCREEN;
1981 }
1982
1983 asyh->view.iW = umode->hdisplay;
1984 asyh->view.iH = umode->vdisplay;
1985 asyh->view.oW = omode->hdisplay;
1986 asyh->view.oH = omode->vdisplay;
1987 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
1988 asyh->view.oH *= 2;
1989
1990 /* Add overscan compensation if necessary, will keep the aspect
1991 * ratio the same as the backend mode unless overridden by the
1992 * user setting both hborder and vborder properties.
1993 */
1994 if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
1995 (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
1996 drm_detect_hdmi_monitor(edid)))) {
1997 u32 bX = asyc->scaler.underscan.hborder;
1998 u32 bY = asyc->scaler.underscan.vborder;
1999 u32 r = (asyh->view.oH << 19) / asyh->view.oW;
2000
2001 if (bX) {
2002 asyh->view.oW -= (bX * 2);
2003 if (bY) asyh->view.oH -= (bY * 2);
2004 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2005 } else {
2006 asyh->view.oW -= (asyh->view.oW >> 4) + 32;
2007 if (bY) asyh->view.oH -= (bY * 2);
2008 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2009 }
2010 }
2011
2012 /* Handle CENTER/ASPECT scaling, taking into account the areas
2013 * removed already for overscan compensation.
2014 */
2015 switch (mode) {
2016 case DRM_MODE_SCALE_CENTER:
2017 asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
2018 asyh->view.oH = min((u16)umode->vdisplay, asyh->view.oH);
2019 /* fall-through */
2020 case DRM_MODE_SCALE_ASPECT:
2021 if (asyh->view.oH < asyh->view.oW) {
2022 u32 r = (asyh->view.iW << 19) / asyh->view.iH;
2023 asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
2024 } else {
2025 u32 r = (asyh->view.iH << 19) / asyh->view.iW;
2026 asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2027 }
2028 break;
2029 default:
2030 break;
2031 }
2032
2033 asyh->set.view = true;
2034}
2035
3dbd036b
BS
2036static void
2037nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
2038{
2039 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
2040 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
2041 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
2042 u32 hbackp = mode->htotal - mode->hsync_end;
2043 u32 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
2044 u32 hfrontp = mode->hsync_start - mode->hdisplay;
2045 u32 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
2046 struct nv50_head_mode *m = &asyh->mode;
2047
2048 m->h.active = mode->htotal;
2049 m->h.synce = mode->hsync_end - mode->hsync_start - 1;
2050 m->h.blanke = m->h.synce + hbackp;
2051 m->h.blanks = mode->htotal - hfrontp - 1;
2052
2053 m->v.active = mode->vtotal * vscan / ilace;
2054 m->v.synce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
2055 m->v.blanke = m->v.synce + vbackp;
2056 m->v.blanks = m->v.active - vfrontp - 1;
2057
2058 /*XXX: Safe underestimate, even "0" works */
2059 m->v.blankus = (m->v.active - mode->vdisplay - 2) * m->h.active;
2060 m->v.blankus *= 1000;
2061 m->v.blankus /= mode->clock;
2062
2063 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
2064 m->v.blank2e = m->v.active + m->v.synce + vbackp;
2065 m->v.blank2s = m->v.blank2e + (mode->vdisplay * vscan / ilace);
2066 m->v.active = (m->v.active * 2) + 1;
2067 m->interlace = true;
2068 } else {
2069 m->v.blank2e = 0;
2070 m->v.blank2s = 1;
2071 m->interlace = false;
2072 }
2073 m->clock = mode->clock;
2074
2075 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2076 asyh->set.mode = true;
2077}
2078
2079static int
2080nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
2081{
2082 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
ad633619 2083 struct nv50_disp *disp = nv50_disp(crtc->dev);
3dbd036b 2084 struct nv50_head *head = nv50_head(crtc);
839ca903 2085 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
3dbd036b 2086 struct nv50_head_atom *asyh = nv50_head_atom(state);
839ca903
BS
2087 struct nouveau_conn_atom *asyc = NULL;
2088 struct drm_connector_state *conns;
2089 struct drm_connector *conn;
2090 int i;
3dbd036b
BS
2091
2092 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
3dbd036b 2093 if (asyh->state.active) {
839ca903
BS
2094 for_each_connector_in_state(asyh->state.state, conn, conns, i) {
2095 if (conns->crtc == crtc) {
2096 asyc = nouveau_conn_atom(conns);
2097 break;
2098 }
2099 }
2100
2101 if (armh->state.active) {
2102 if (asyc) {
2103 if (asyh->state.mode_changed)
2104 asyc->set.scaler = true;
2105 if (armh->base.depth != asyh->base.depth)
2106 asyc->set.dither = true;
2107 }
2108 } else {
2109 asyc->set.mask = ~0;
2110 asyh->set.mask = ~0;
2111 }
2112
3dbd036b
BS
2113 if (asyh->state.mode_changed)
2114 nv50_head_atomic_check_mode(head, asyh);
ad633619 2115
839ca903
BS
2116 if (asyc) {
2117 if (asyc->set.scaler)
2118 nv50_head_atomic_check_view(armh, asyh, asyc);
2119 if (asyc->set.dither)
2120 nv50_head_atomic_check_dither(armh, asyh, asyc);
2121 if (asyc->set.procamp)
2122 nv50_head_atomic_check_procamp(armh, asyh, asyc);
2123 }
2124
ad633619
BS
2125 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
2126 asyh->core.x = asyh->base.x;
2127 asyh->core.y = asyh->base.y;
2128 asyh->core.w = asyh->base.w;
2129 asyh->core.h = asyh->base.h;
2130 } else
ea8ee390 2131 if ((asyh->core.visible = asyh->curs.visible)) {
ad633619
BS
2132 /*XXX: We need to either find some way of having the
2133 * primary base layer appear black, while still
2134 * being able to display the other layers, or we
2135 * need to allocate a dummy black surface here.
2136 */
2137 asyh->core.x = 0;
2138 asyh->core.y = 0;
2139 asyh->core.w = asyh->state.mode.hdisplay;
2140 asyh->core.h = asyh->state.mode.vdisplay;
2141 }
2142 asyh->core.handle = disp->mast.base.vram.handle;
2143 asyh->core.offset = 0;
2144 asyh->core.format = 0xcf;
2145 asyh->core.kind = 0;
2146 asyh->core.layout = 1;
2147 asyh->core.block = 0;
2148 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
a7ae1561
BS
2149 asyh->lut.handle = disp->mast.base.vram.handle;
2150 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
6bbab3b6
BS
2151 asyh->set.base = armh->base.cpp != asyh->base.cpp;
2152 asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
ad633619
BS
2153 } else {
2154 asyh->core.visible = false;
ea8ee390 2155 asyh->curs.visible = false;
6bbab3b6
BS
2156 asyh->base.cpp = 0;
2157 asyh->ovly.cpp = 0;
ad633619
BS
2158 }
2159
2160 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
2161 if (asyh->core.visible) {
2162 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
2163 asyh->set.core = true;
2164 } else
2165 if (armh->core.visible) {
2166 asyh->clr.core = true;
2167 }
ea8ee390
BS
2168
2169 if (asyh->curs.visible) {
2170 if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
2171 asyh->set.curs = true;
2172 } else
2173 if (armh->curs.visible) {
2174 asyh->clr.curs = true;
2175 }
ad633619
BS
2176 } else {
2177 asyh->clr.core = armh->core.visible;
ea8ee390 2178 asyh->clr.curs = armh->curs.visible;
ad633619 2179 asyh->set.core = asyh->core.visible;
ea8ee390 2180 asyh->set.curs = asyh->curs.visible;
3dbd036b
BS
2181 }
2182
839ca903
BS
2183 if (asyh->clr.mask || asyh->set.mask)
2184 nv50_atom(asyh->state.state)->lock_core = true;
3dbd036b
BS
2185 return 0;
2186}
2187
438d99e3 2188static void
9bfdee9a 2189nv50_head_lut_load(struct drm_crtc *crtc)
438d99e3 2190{
e225f446 2191 struct nv50_disp *disp = nv50_disp(crtc->dev);
438d99e3
BS
2192 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2193 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
2194 int i;
2195
2196 for (i = 0; i < 256; i++) {
de8268c5
BS
2197 u16 r = nv_crtc->lut.r[i] >> 2;
2198 u16 g = nv_crtc->lut.g[i] >> 2;
2199 u16 b = nv_crtc->lut.b[i] >> 2;
2200
648d4dfd 2201 if (disp->disp->oclass < GF110_DISP) {
de8268c5
BS
2202 writew(r + 0x0000, lut + (i * 0x08) + 0);
2203 writew(g + 0x0000, lut + (i * 0x08) + 2);
2204 writew(b + 0x0000, lut + (i * 0x08) + 4);
2205 } else {
2206 writew(r + 0x6000, lut + (i * 0x20) + 0);
2207 writew(g + 0x6000, lut + (i * 0x20) + 2);
2208 writew(b + 0x6000, lut + (i * 0x20) + 4);
2209 }
438d99e3
BS
2210 }
2211}
2212
9bfdee9a
BS
2213static const struct drm_crtc_helper_funcs
2214nv50_head_help = {
9bfdee9a 2215 .load_lut = nv50_head_lut_load,
839ca903 2216 .atomic_check = nv50_head_atomic_check,
438d99e3
BS
2217};
2218
9bfdee9a
BS
2219static int
2220nv50_head_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
2221 uint32_t size)
2222{
2223 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2224 u32 i;
2225
2226 for (i = 0; i < size; i++) {
2227 nv_crtc->lut.r[i] = r[i];
2228 nv_crtc->lut.g[i] = g[i];
2229 nv_crtc->lut.b[i] = b[i];
2230 }
2231
2232 nv50_head_lut_load(crtc);
2233 return 0;
2234}
2235
839ca903
BS
2236static void
2237nv50_head_atomic_destroy_state(struct drm_crtc *crtc,
2238 struct drm_crtc_state *state)
2239{
2240 struct nv50_head_atom *asyh = nv50_head_atom(state);
2241 __drm_atomic_helper_crtc_destroy_state(&asyh->state);
2242 kfree(asyh);
2243}
2244
2245static struct drm_crtc_state *
2246nv50_head_atomic_duplicate_state(struct drm_crtc *crtc)
2247{
2248 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
2249 struct nv50_head_atom *asyh;
2250 if (!(asyh = kmalloc(sizeof(*asyh), GFP_KERNEL)))
2251 return NULL;
2252 __drm_atomic_helper_crtc_duplicate_state(crtc, &asyh->state);
2253 asyh->view = armh->view;
2254 asyh->mode = armh->mode;
2255 asyh->lut = armh->lut;
2256 asyh->core = armh->core;
2257 asyh->curs = armh->curs;
2258 asyh->base = armh->base;
2259 asyh->ovly = armh->ovly;
2260 asyh->dither = armh->dither;
2261 asyh->procamp = armh->procamp;
2262 asyh->clr.mask = 0;
2263 asyh->set.mask = 0;
2264 return &asyh->state;
2265}
2266
2267static void
2268__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
2269 struct drm_crtc_state *state)
2270{
2271 if (crtc->state)
2272 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
2273 crtc->state = state;
2274 crtc->state->crtc = crtc;
2275}
2276
2277static void
2278nv50_head_reset(struct drm_crtc *crtc)
2279{
2280 struct nv50_head_atom *asyh;
2281
2282 if (WARN_ON(!(asyh = kzalloc(sizeof(*asyh), GFP_KERNEL))))
2283 return;
2284
2285 __drm_atomic_helper_crtc_reset(crtc, &asyh->state);
2286}
2287
9bfdee9a
BS
2288static void
2289nv50_head_destroy(struct drm_crtc *crtc)
2290{
2291 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2292 struct nv50_disp *disp = nv50_disp(crtc->dev);
2293 struct nv50_head *head = nv50_head(crtc);
2294
2295 nv50_dmac_destroy(&head->ovly.base, disp->disp);
2296 nv50_pioc_destroy(&head->oimm.base);
2297
2298 nouveau_bo_unmap(nv_crtc->lut.nvbo);
2299 if (nv_crtc->lut.nvbo)
2300 nouveau_bo_unpin(nv_crtc->lut.nvbo);
2301 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
2302
2303 drm_crtc_cleanup(crtc);
2304 kfree(crtc);
2305}
2306
2307static const struct drm_crtc_funcs
2308nv50_head_func = {
839ca903 2309 .reset = nv50_head_reset,
9bfdee9a
BS
2310 .gamma_set = nv50_head_gamma_set,
2311 .destroy = nv50_head_destroy,
839ca903 2312 .set_config = drm_atomic_helper_set_config,
612fb5d9 2313 .page_flip = drm_atomic_helper_page_flip,
839ca903
BS
2314 .set_property = drm_atomic_helper_crtc_set_property,
2315 .atomic_duplicate_state = nv50_head_atomic_duplicate_state,
2316 .atomic_destroy_state = nv50_head_atomic_destroy_state,
438d99e3
BS
2317};
2318
2319static int
9bfdee9a 2320nv50_head_create(struct drm_device *dev, int index)
438d99e3 2321{
a01ca78c 2322 struct nouveau_drm *drm = nouveau_drm(dev);
1167c6bc 2323 struct nvif_device *device = &drm->client.device;
e225f446
BS
2324 struct nv50_disp *disp = nv50_disp(dev);
2325 struct nv50_head *head;
973f10c2 2326 struct nv50_base *base;
22e927d2 2327 struct nv50_curs *curs;
438d99e3
BS
2328 struct drm_crtc *crtc;
2329 int ret, i;
2330
dd0e3d53
BS
2331 head = kzalloc(sizeof(*head), GFP_KERNEL);
2332 if (!head)
438d99e3
BS
2333 return -ENOMEM;
2334
dd0e3d53 2335 head->base.index = index;
438d99e3 2336 for (i = 0; i < 256; i++) {
dd0e3d53
BS
2337 head->base.lut.r[i] = i << 8;
2338 head->base.lut.g[i] = i << 8;
2339 head->base.lut.b[i] = i << 8;
438d99e3
BS
2340 }
2341
973f10c2 2342 ret = nv50_base_new(drm, head, &base);
22e927d2
BS
2343 if (ret == 0)
2344 ret = nv50_curs_new(drm, head, &curs);
973f10c2
BS
2345 if (ret) {
2346 kfree(head);
2347 return ret;
2348 }
2349
dd0e3d53 2350 crtc = &head->base.base;
839ca903 2351 drm_crtc_init_with_planes(dev, crtc, &base->wndw.plane,
9bfdee9a 2352 &curs->wndw.plane, &nv50_head_func,
839ca903 2353 "head-%d", head->base.index);
9bfdee9a 2354 drm_crtc_helper_add(crtc, &nv50_head_help);
438d99e3
BS
2355 drm_mode_crtc_set_gamma_size(crtc, 256);
2356
bab7cc18 2357 ret = nouveau_bo_new(&drm->client, 8192, 0x100, TTM_PL_FLAG_VRAM,
bb6178b0 2358 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
b5a794b0 2359 if (!ret) {
547ad072 2360 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
04c8c210 2361 if (!ret) {
b5a794b0 2362 ret = nouveau_bo_map(head->base.lut.nvbo);
04c8c210
MS
2363 if (ret)
2364 nouveau_bo_unpin(head->base.lut.nvbo);
2365 }
b5a794b0
BS
2366 if (ret)
2367 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
2368 }
2369
438d99e3
BS
2370 if (ret)
2371 goto out;
2372
b5a794b0 2373 /* allocate overlay resources */
a01ca78c 2374 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
438d99e3
BS
2375 if (ret)
2376 goto out;
2377
a01ca78c
BS
2378 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
2379 &head->ovly);
b5a794b0
BS
2380 if (ret)
2381 goto out;
438d99e3
BS
2382
2383out:
2384 if (ret)
9bfdee9a 2385 nv50_head_destroy(crtc);
438d99e3
BS
2386 return ret;
2387}
2388
a91d3221 2389/******************************************************************************
d92c8adf 2390 * Output path helpers
a91d3221 2391 *****************************************************************************/
d92c8adf
BS
2392static int
2393nv50_outp_atomic_check_view(struct drm_encoder *encoder,
2394 struct drm_crtc_state *crtc_state,
2395 struct drm_connector_state *conn_state,
2396 struct drm_display_mode *native_mode)
2397{
2398 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
2399 struct drm_display_mode *mode = &crtc_state->mode;
2400 struct drm_connector *connector = conn_state->connector;
2401 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
2402 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
2403
2404 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
2405 asyc->scaler.full = false;
2406 if (!native_mode)
2407 return 0;
2408
2409 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
2410 switch (connector->connector_type) {
2411 case DRM_MODE_CONNECTOR_LVDS:
2412 case DRM_MODE_CONNECTOR_eDP:
2413 /* Force use of scaler for non-EDID modes. */
2414 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
2415 break;
2416 mode = native_mode;
2417 asyc->scaler.full = true;
2418 break;
2419 default:
2420 break;
2421 }
2422 } else {
2423 mode = native_mode;
2424 }
2425
2426 if (!drm_mode_equal(adjusted_mode, mode)) {
2427 drm_mode_copy(adjusted_mode, mode);
2428 crtc_state->mode_changed = true;
2429 }
2430
2431 return 0;
2432}
2433
839ca903
BS
2434static int
2435nv50_outp_atomic_check(struct drm_encoder *encoder,
2436 struct drm_crtc_state *crtc_state,
2437 struct drm_connector_state *conn_state)
a91d3221 2438{
839ca903
BS
2439 struct nouveau_connector *nv_connector =
2440 nouveau_connector(conn_state->connector);
2441 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2442 nv_connector->native_mode);
a91d3221
BS
2443}
2444
26f6d88b
BS
2445/******************************************************************************
2446 * DAC
2447 *****************************************************************************/
8eaa9669 2448static void
e225f446 2449nv50_dac_dpms(struct drm_encoder *encoder, int mode)
8eaa9669
BS
2450{
2451 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
e225f446 2452 struct nv50_disp *disp = nv50_disp(encoder->dev);
bf0eb898
BS
2453 struct {
2454 struct nv50_disp_mthd_v1 base;
2455 struct nv50_disp_dac_pwr_v0 pwr;
2456 } args = {
2457 .base.version = 1,
2458 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
2459 .base.hasht = nv_encoder->dcb->hasht,
2460 .base.hashm = nv_encoder->dcb->hashm,
2461 .pwr.state = 1,
2462 .pwr.data = 1,
2463 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
2464 mode != DRM_MODE_DPMS_OFF),
2465 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
2466 mode != DRM_MODE_DPMS_OFF),
2467 };
8eaa9669 2468
bf0eb898 2469 nvif_mthd(disp->disp, 0, &args, sizeof(args));
8eaa9669
BS
2470}
2471
8eaa9669 2472static void
839ca903 2473nv50_dac_disable(struct drm_encoder *encoder)
8eaa9669 2474{
f20c665c
BS
2475 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2476 struct nv50_mast *mast = nv50_mast(encoder->dev);
2477 const int or = nv_encoder->or;
2478 u32 *push;
2479
2480 if (nv_encoder->crtc) {
f20c665c
BS
2481 push = evo_wait(mast, 4);
2482 if (push) {
2483 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2484 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2485 evo_data(push, 0x00000000);
2486 } else {
2487 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2488 evo_data(push, 0x00000000);
2489 }
2490 evo_kick(push, mast);
2491 }
2492 }
2493
2494 nv_encoder->crtc = NULL;
8eaa9669
BS
2495}
2496
2497static void
839ca903 2498nv50_dac_enable(struct drm_encoder *encoder)
8eaa9669 2499{
e225f446 2500 struct nv50_mast *mast = nv50_mast(encoder->dev);
8eaa9669
BS
2501 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2502 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
839ca903 2503 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
97b19b5c 2504 u32 *push;
8eaa9669 2505
97b19b5c 2506 push = evo_wait(mast, 8);
8eaa9669 2507 if (push) {
648d4dfd 2508 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
97b19b5c
BS
2509 u32 syncs = 0x00000000;
2510
2511 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2512 syncs |= 0x00000001;
2513 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2514 syncs |= 0x00000002;
2515
2516 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
2517 evo_data(push, 1 << nv_crtc->index);
2518 evo_data(push, syncs);
2519 } else {
2520 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2521 u32 syncs = 0x00000001;
2522
2523 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2524 syncs |= 0x00000008;
2525 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2526 syncs |= 0x00000010;
2527
2528 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2529 magic |= 0x00000001;
2530
2531 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2532 evo_data(push, syncs);
2533 evo_data(push, magic);
2534 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2535 evo_data(push, 1 << nv_crtc->index);
2536 }
2537
2538 evo_kick(push, mast);
8eaa9669
BS
2539 }
2540
2541 nv_encoder->crtc = encoder->crtc;
2542}
2543
b6d8e7ec 2544static enum drm_connector_status
e225f446 2545nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
b6d8e7ec 2546{
c4abd317 2547 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
e225f446 2548 struct nv50_disp *disp = nv50_disp(encoder->dev);
c4abd317
BS
2549 struct {
2550 struct nv50_disp_mthd_v1 base;
2551 struct nv50_disp_dac_load_v0 load;
2552 } args = {
2553 .base.version = 1,
2554 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2555 .base.hasht = nv_encoder->dcb->hasht,
2556 .base.hashm = nv_encoder->dcb->hashm,
2557 };
2558 int ret;
2559
2560 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2561 if (args.load.data == 0)
2562 args.load.data = 340;
b681993f 2563
c4abd317
BS
2564 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2565 if (ret || !args.load.load)
35b21d39 2566 return connector_status_disconnected;
b681993f 2567
35b21d39 2568 return connector_status_connected;
b6d8e7ec
BS
2569}
2570
f20c665c
BS
2571static const struct drm_encoder_helper_funcs
2572nv50_dac_help = {
e225f446 2573 .dpms = nv50_dac_dpms,
839ca903
BS
2574 .atomic_check = nv50_outp_atomic_check,
2575 .enable = nv50_dac_enable,
2576 .disable = nv50_dac_disable,
e225f446 2577 .detect = nv50_dac_detect
8eaa9669
BS
2578};
2579
f20c665c
BS
2580static void
2581nv50_dac_destroy(struct drm_encoder *encoder)
2582{
2583 drm_encoder_cleanup(encoder);
2584 kfree(encoder);
2585}
2586
2587static const struct drm_encoder_funcs
2588nv50_dac_func = {
e225f446 2589 .destroy = nv50_dac_destroy,
8eaa9669
BS
2590};
2591
2592static int
e225f446 2593nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
8eaa9669 2594{
5ed50209 2595 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1167c6bc 2596 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
2aa5eac5 2597 struct nvkm_i2c_bus *bus;
8eaa9669
BS
2598 struct nouveau_encoder *nv_encoder;
2599 struct drm_encoder *encoder;
5ed50209 2600 int type = DRM_MODE_ENCODER_DAC;
8eaa9669
BS
2601
2602 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2603 if (!nv_encoder)
2604 return -ENOMEM;
2605 nv_encoder->dcb = dcbe;
2606 nv_encoder->or = ffs(dcbe->or) - 1;
2aa5eac5
BS
2607
2608 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2609 if (bus)
2610 nv_encoder->i2c = &bus->i2c;
8eaa9669
BS
2611
2612 encoder = to_drm_encoder(nv_encoder);
2613 encoder->possible_crtcs = dcbe->heads;
2614 encoder->possible_clones = 0;
5a223dac
BS
2615 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2616 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
f20c665c 2617 drm_encoder_helper_add(encoder, &nv50_dac_help);
8eaa9669
BS
2618
2619 drm_mode_connector_attach_encoder(connector, encoder);
2620 return 0;
2621}
26f6d88b 2622
78951d22
BS
2623/******************************************************************************
2624 * Audio
2625 *****************************************************************************/
2626static void
f20c665c
BS
2627nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
2628{
2629 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2630 struct nv50_disp *disp = nv50_disp(encoder->dev);
2631 struct {
2632 struct nv50_disp_mthd_v1 base;
2633 struct nv50_disp_sor_hda_eld_v0 eld;
2634 } args = {
2635 .base.version = 1,
2636 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2637 .base.hasht = nv_encoder->dcb->hasht,
2638 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2639 (0x0100 << nv_crtc->index),
2640 };
2641
2642 nvif_mthd(disp->disp, 0, &args, sizeof(args));
2643}
2644
2645static void
2646nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
78951d22
BS
2647{
2648 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
cc2a9071 2649 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
78951d22 2650 struct nouveau_connector *nv_connector;
e225f446 2651 struct nv50_disp *disp = nv50_disp(encoder->dev);
d889c524
BS
2652 struct __packed {
2653 struct {
2654 struct nv50_disp_mthd_v1 mthd;
2655 struct nv50_disp_sor_hda_eld_v0 eld;
2656 } base;
120b0c39
BS
2657 u8 data[sizeof(nv_connector->base.eld)];
2658 } args = {
d889c524
BS
2659 .base.mthd.version = 1,
2660 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2661 .base.mthd.hasht = nv_encoder->dcb->hasht,
cc2a9071
BS
2662 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2663 (0x0100 << nv_crtc->index),
120b0c39 2664 };
78951d22
BS
2665
2666 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2667 if (!drm_detect_monitor_audio(nv_connector->edid))
2668 return;
2669
78951d22 2670 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
120b0c39 2671 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
78951d22 2672
938fd8aa
JN
2673 nvif_mthd(disp->disp, 0, &args,
2674 sizeof(args.base) + drm_eld_size(args.data));
78951d22
BS
2675}
2676
f20c665c
BS
2677/******************************************************************************
2678 * HDMI
2679 *****************************************************************************/
78951d22 2680static void
f20c665c 2681nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
78951d22
BS
2682{
2683 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
e225f446 2684 struct nv50_disp *disp = nv50_disp(encoder->dev);
120b0c39
BS
2685 struct {
2686 struct nv50_disp_mthd_v1 base;
f20c665c 2687 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
120b0c39
BS
2688 } args = {
2689 .base.version = 1,
f20c665c
BS
2690 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2691 .base.hasht = nv_encoder->dcb->hasht,
2692 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2693 (0x0100 << nv_crtc->index),
120b0c39 2694 };
78951d22 2695
120b0c39 2696 nvif_mthd(disp->disp, 0, &args, sizeof(args));
78951d22
BS
2697}
2698
78951d22 2699static void
f20c665c 2700nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
78951d22 2701{
64d9cc04
BS
2702 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2703 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
e225f446 2704 struct nv50_disp *disp = nv50_disp(encoder->dev);
e00f2235
BS
2705 struct {
2706 struct nv50_disp_mthd_v1 base;
2707 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
2708 } args = {
2709 .base.version = 1,
2710 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2711 .base.hasht = nv_encoder->dcb->hasht,
2712 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2713 (0x0100 << nv_crtc->index),
2714 .pwr.state = 1,
2715 .pwr.rekey = 56, /* binary driver, and tegra, constant */
2716 };
2717 struct nouveau_connector *nv_connector;
64d9cc04
BS
2718 u32 max_ac_packet;
2719
2720 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2721 if (!drm_detect_hdmi_monitor(nv_connector->edid))
2722 return;
2723
2724 max_ac_packet = mode->htotal - mode->hdisplay;
e00f2235 2725 max_ac_packet -= args.pwr.rekey;
64d9cc04 2726 max_ac_packet -= 18; /* constant from tegra */
e00f2235 2727 args.pwr.max_ac_packet = max_ac_packet / 32;
091e40cd 2728
e00f2235 2729 nvif_mthd(disp->disp, 0, &args, sizeof(args));
f20c665c 2730 nv50_audio_enable(encoder, mode);
78951d22
BS
2731}
2732
52aa30f2
BS
2733/******************************************************************************
2734 * MST
2735 *****************************************************************************/
f479c0ba
BS
2736#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
2737#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
2738#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
2739
52aa30f2
BS
2740struct nv50_mstm {
2741 struct nouveau_encoder *outp;
2742
2743 struct drm_dp_mst_topology_mgr mgr;
f479c0ba
BS
2744 struct nv50_msto *msto[4];
2745
2746 bool modified;
2747};
2748
2749struct nv50_mstc {
2750 struct nv50_mstm *mstm;
2751 struct drm_dp_mst_port *port;
2752 struct drm_connector connector;
2753
2754 struct drm_display_mode *native;
2755 struct edid *edid;
2756
2757 int pbn;
52aa30f2
BS
2758};
2759
f479c0ba
BS
2760struct nv50_msto {
2761 struct drm_encoder encoder;
2762
2763 struct nv50_head *head;
2764 struct nv50_mstc *mstc;
2765 bool disabled;
2766};
2767
2768static struct drm_dp_payload *
2769nv50_msto_payload(struct nv50_msto *msto)
2770{
2771 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2772 struct nv50_mstc *mstc = msto->mstc;
2773 struct nv50_mstm *mstm = mstc->mstm;
2774 int vcpi = mstc->port->vcpi.vcpi, i;
2775
2776 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
2777 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2778 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2779 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
2780 mstm->outp->base.base.name, i, payload->vcpi,
2781 payload->start_slot, payload->num_slots);
2782 }
2783
2784 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2785 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2786 if (payload->vcpi == vcpi)
2787 return payload;
2788 }
2789
2790 return NULL;
2791}
2792
2793static void
2794nv50_msto_cleanup(struct nv50_msto *msto)
2795{
2796 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2797 struct nv50_mstc *mstc = msto->mstc;
2798 struct nv50_mstm *mstm = mstc->mstm;
2799
2800 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
2801 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
2802 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
2803 if (msto->disabled) {
2804 msto->mstc = NULL;
2805 msto->head = NULL;
2806 msto->disabled = false;
2807 }
2808}
2809
2810static void
2811nv50_msto_prepare(struct nv50_msto *msto)
2812{
2813 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2814 struct nv50_mstc *mstc = msto->mstc;
2815 struct nv50_mstm *mstm = mstc->mstm;
2816 struct {
2817 struct nv50_disp_mthd_v1 base;
2818 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
2819 } args = {
2820 .base.version = 1,
2821 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
2822 .base.hasht = mstm->outp->dcb->hasht,
2823 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
2824 (0x0100 << msto->head->base.index),
2825 };
2826
2827 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
2828 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
2829 struct drm_dp_payload *payload = nv50_msto_payload(msto);
2830 if (payload) {
2831 args.vcpi.start_slot = payload->start_slot;
2832 args.vcpi.num_slots = payload->num_slots;
2833 args.vcpi.pbn = mstc->port->vcpi.pbn;
2834 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
2835 }
2836 }
2837
2838 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
2839 msto->encoder.name, msto->head->base.base.name,
2840 args.vcpi.start_slot, args.vcpi.num_slots,
2841 args.vcpi.pbn, args.vcpi.aligned_pbn);
2842 nvif_mthd(&drm->display->disp, 0, &args, sizeof(args));
2843}
2844
2845static int
2846nv50_msto_atomic_check(struct drm_encoder *encoder,
2847 struct drm_crtc_state *crtc_state,
2848 struct drm_connector_state *conn_state)
2849{
2850 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
2851 struct nv50_mstm *mstm = mstc->mstm;
2852 int bpp = conn_state->connector->display_info.bpc * 3;
2853 int slots;
2854
2855 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
2856
2857 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2858 if (slots < 0)
2859 return slots;
2860
2861 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2862 mstc->native);
2863}
2864
2865static void
2866nv50_msto_enable(struct drm_encoder *encoder)
2867{
2868 struct nv50_head *head = nv50_head(encoder->crtc);
2869 struct nv50_msto *msto = nv50_msto(encoder);
2870 struct nv50_mstc *mstc = NULL;
2871 struct nv50_mstm *mstm = NULL;
2872 struct drm_connector *connector;
2873 u8 proto, depth;
2874 int slots;
2875 bool r;
2876
2877 drm_for_each_connector(connector, encoder->dev) {
2878 if (connector->state->best_encoder == &msto->encoder) {
2879 mstc = nv50_mstc(connector);
2880 mstm = mstc->mstm;
2881 break;
2882 }
2883 }
2884
2885 if (WARN_ON(!mstc))
2886 return;
2887
1e797f55
PD
2888 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2889 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
f479c0ba
BS
2890 WARN_ON(!r);
2891
2892 if (mstm->outp->dcb->sorconf.link & 1)
2893 proto = 0x8;
2894 else
2895 proto = 0x9;
2896
2897 switch (mstc->connector.display_info.bpc) {
2898 case 6: depth = 0x2; break;
2899 case 8: depth = 0x5; break;
2900 case 10:
2901 default: depth = 0x6; break;
2902 }
2903
2904 mstm->outp->update(mstm->outp, head->base.index,
2905 &head->base.base.state->adjusted_mode, proto, depth);
2906
2907 msto->head = head;
2908 msto->mstc = mstc;
2909 mstm->modified = true;
2910}
2911
2912static void
2913nv50_msto_disable(struct drm_encoder *encoder)
2914{
2915 struct nv50_msto *msto = nv50_msto(encoder);
2916 struct nv50_mstc *mstc = msto->mstc;
2917 struct nv50_mstm *mstm = mstc->mstm;
2918
2919 if (mstc->port)
2920 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
2921
2922 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
2923 mstm->modified = true;
2924 msto->disabled = true;
2925}
2926
2927static const struct drm_encoder_helper_funcs
2928nv50_msto_help = {
2929 .disable = nv50_msto_disable,
2930 .enable = nv50_msto_enable,
2931 .atomic_check = nv50_msto_atomic_check,
2932};
2933
2934static void
2935nv50_msto_destroy(struct drm_encoder *encoder)
2936{
2937 struct nv50_msto *msto = nv50_msto(encoder);
2938 drm_encoder_cleanup(&msto->encoder);
2939 kfree(msto);
2940}
2941
2942static const struct drm_encoder_funcs
2943nv50_msto = {
2944 .destroy = nv50_msto_destroy,
2945};
2946
2947static int
2948nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
2949 struct nv50_msto **pmsto)
2950{
2951 struct nv50_msto *msto;
2952 int ret;
2953
2954 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
2955 return -ENOMEM;
2956
2957 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
2958 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
2959 if (ret) {
2960 kfree(*pmsto);
2961 *pmsto = NULL;
2962 return ret;
2963 }
2964
2965 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
2966 msto->encoder.possible_crtcs = heads;
2967 return 0;
2968}
2969
2970static struct drm_encoder *
2971nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
2972 struct drm_connector_state *connector_state)
2973{
2974 struct nv50_head *head = nv50_head(connector_state->crtc);
2975 struct nv50_mstc *mstc = nv50_mstc(connector);
2976 if (mstc->port) {
2977 struct nv50_mstm *mstm = mstc->mstm;
2978 return &mstm->msto[head->base.index]->encoder;
2979 }
2980 return NULL;
2981}
2982
2983static struct drm_encoder *
2984nv50_mstc_best_encoder(struct drm_connector *connector)
2985{
2986 struct nv50_mstc *mstc = nv50_mstc(connector);
2987 if (mstc->port) {
2988 struct nv50_mstm *mstm = mstc->mstm;
2989 return &mstm->msto[0]->encoder;
2990 }
2991 return NULL;
2992}
2993
2994static enum drm_mode_status
2995nv50_mstc_mode_valid(struct drm_connector *connector,
2996 struct drm_display_mode *mode)
2997{
2998 return MODE_OK;
2999}
3000
3001static int
3002nv50_mstc_get_modes(struct drm_connector *connector)
3003{
3004 struct nv50_mstc *mstc = nv50_mstc(connector);
3005 int ret = 0;
3006
3007 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
3008 drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
3009 if (mstc->edid) {
3010 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
3011 drm_edid_to_eld(&mstc->connector, mstc->edid);
3012 }
3013
3014 if (!mstc->connector.display_info.bpc)
3015 mstc->connector.display_info.bpc = 8;
3016
3017 if (mstc->native)
3018 drm_mode_destroy(mstc->connector.dev, mstc->native);
3019 mstc->native = nouveau_conn_native_mode(&mstc->connector);
3020 return ret;
3021}
3022
3023static const struct drm_connector_helper_funcs
3024nv50_mstc_help = {
3025 .get_modes = nv50_mstc_get_modes,
3026 .mode_valid = nv50_mstc_mode_valid,
3027 .best_encoder = nv50_mstc_best_encoder,
3028 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
3029};
3030
3031static enum drm_connector_status
3032nv50_mstc_detect(struct drm_connector *connector, bool force)
3033{
3034 struct nv50_mstc *mstc = nv50_mstc(connector);
3035 if (!mstc->port)
3036 return connector_status_disconnected;
3037 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
3038}
3039
3040static void
3041nv50_mstc_destroy(struct drm_connector *connector)
3042{
3043 struct nv50_mstc *mstc = nv50_mstc(connector);
3044 drm_connector_cleanup(&mstc->connector);
3045 kfree(mstc);
3046}
3047
3048static const struct drm_connector_funcs
3049nv50_mstc = {
3050 .dpms = drm_atomic_helper_connector_dpms,
3051 .reset = nouveau_conn_reset,
3052 .detect = nv50_mstc_detect,
3053 .fill_modes = drm_helper_probe_single_connector_modes,
3054 .set_property = drm_atomic_helper_connector_set_property,
3055 .destroy = nv50_mstc_destroy,
3056 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
3057 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
3058 .atomic_set_property = nouveau_conn_atomic_set_property,
3059 .atomic_get_property = nouveau_conn_atomic_get_property,
3060};
3061
3062static int
3063nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
3064 const char *path, struct nv50_mstc **pmstc)
3065{
3066 struct drm_device *dev = mstm->outp->base.base.dev;
3067 struct nv50_mstc *mstc;
3068 int ret, i;
3069
3070 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
3071 return -ENOMEM;
3072 mstc->mstm = mstm;
3073 mstc->port = port;
3074
3075 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
3076 DRM_MODE_CONNECTOR_DisplayPort);
3077 if (ret) {
3078 kfree(*pmstc);
3079 *pmstc = NULL;
3080 return ret;
3081 }
3082
3083 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
3084
3085 mstc->connector.funcs->reset(&mstc->connector);
3086 nouveau_conn_attach_properties(&mstc->connector);
3087
3088 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto; i++)
3089 drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
3090
3091 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
3092 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
3093 drm_mode_connector_set_path_property(&mstc->connector, path);
3094 return 0;
3095}
3096
3097static void
3098nv50_mstm_cleanup(struct nv50_mstm *mstm)
3099{
3100 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3101 struct drm_encoder *encoder;
3102 int ret;
3103
3104 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
3105 ret = drm_dp_check_act_status(&mstm->mgr);
3106
3107 ret = drm_dp_update_payload_part2(&mstm->mgr);
3108
3109 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3110 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3111 struct nv50_msto *msto = nv50_msto(encoder);
3112 struct nv50_mstc *mstc = msto->mstc;
3113 if (mstc && mstc->mstm == mstm)
3114 nv50_msto_cleanup(msto);
3115 }
3116 }
3117
3118 mstm->modified = false;
3119}
3120
3121static void
3122nv50_mstm_prepare(struct nv50_mstm *mstm)
3123{
3124 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3125 struct drm_encoder *encoder;
3126 int ret;
3127
3128 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
3129 ret = drm_dp_update_payload_part1(&mstm->mgr);
3130
3131 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3132 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3133 struct nv50_msto *msto = nv50_msto(encoder);
3134 struct nv50_mstc *mstc = msto->mstc;
3135 if (mstc && mstc->mstm == mstm)
3136 nv50_msto_prepare(msto);
3137 }
3138 }
3139}
3140
3141static void
3142nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
3143{
3144 struct nv50_mstm *mstm = nv50_mstm(mgr);
3145 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
3146}
3147
3148static void
3149nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
3150 struct drm_connector *connector)
3151{
3152 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3153 struct nv50_mstc *mstc = nv50_mstc(connector);
3154
3155 drm_connector_unregister(&mstc->connector);
3156
3157 drm_modeset_lock_all(drm->dev);
3158 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
3159 mstc->port = NULL;
3160 drm_modeset_unlock_all(drm->dev);
3161
3162 drm_connector_unreference(&mstc->connector);
3163}
3164
3165static void
3166nv50_mstm_register_connector(struct drm_connector *connector)
3167{
3168 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3169
3170 drm_modeset_lock_all(drm->dev);
3171 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
3172 drm_modeset_unlock_all(drm->dev);
3173
3174 drm_connector_register(connector);
3175}
3176
3177static struct drm_connector *
3178nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
3179 struct drm_dp_mst_port *port, const char *path)
3180{
3181 struct nv50_mstm *mstm = nv50_mstm(mgr);
3182 struct nv50_mstc *mstc;
3183 int ret;
3184
3185 ret = nv50_mstc_new(mstm, port, path, &mstc);
3186 if (ret) {
3187 if (mstc)
3188 mstc->connector.funcs->destroy(&mstc->connector);
3189 return NULL;
3190 }
3191
3192 return &mstc->connector;
3193}
3194
3195static const struct drm_dp_mst_topology_cbs
3196nv50_mstm = {
3197 .add_connector = nv50_mstm_add_connector,
3198 .register_connector = nv50_mstm_register_connector,
3199 .destroy_connector = nv50_mstm_destroy_connector,
3200 .hotplug = nv50_mstm_hotplug,
3201};
3202
3203void
3204nv50_mstm_service(struct nv50_mstm *mstm)
3205{
3206 struct drm_dp_aux *aux = mstm->mgr.aux;
3207 bool handled = true;
3208 int ret;
3209 u8 esi[8] = {};
3210
3211 while (handled) {
3212 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
3213 if (ret != 8) {
3214 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3215 return;
3216 }
3217
3218 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
3219 if (!handled)
3220 break;
3221
3222 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
3223 }
3224}
3225
3226void
3227nv50_mstm_remove(struct nv50_mstm *mstm)
3228{
3229 if (mstm)
3230 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3231}
3232
52aa30f2
BS
3233static int
3234nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
3235{
3236 struct nouveau_encoder *outp = mstm->outp;
3237 struct {
3238 struct nv50_disp_mthd_v1 base;
3239 struct nv50_disp_sor_dp_mst_link_v0 mst;
3240 } args = {
3241 .base.version = 1,
3242 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
3243 .base.hasht = outp->dcb->hasht,
3244 .base.hashm = outp->dcb->hashm,
3245 .mst.state = state,
3246 };
3247 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
3248 struct nvif_object *disp = &drm->display->disp;
3249 int ret;
3250
3251 if (dpcd >= 0x12) {
3252 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
3253 if (ret < 0)
3254 return ret;
3255
3256 dpcd &= ~DP_MST_EN;
3257 if (state)
3258 dpcd |= DP_MST_EN;
3259
3260 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
3261 if (ret < 0)
3262 return ret;
3263 }
3264
3265 return nvif_mthd(disp, 0, &args, sizeof(args));
3266}
3267
3268int
3269nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
3270{
3271 int ret, state = 0;
3272
3273 if (!mstm)
3274 return 0;
3275
3ca03cac 3276 if (dpcd[0] >= 0x12) {
52aa30f2
BS
3277 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
3278 if (ret < 0)
3279 return ret;
3280
3ca03cac
BS
3281 if (!(dpcd[1] & DP_MST_CAP))
3282 dpcd[0] = 0x11;
3283 else
3284 state = allow;
52aa30f2
BS
3285 }
3286
3287 ret = nv50_mstm_enable(mstm, dpcd[0], state);
3288 if (ret)
3289 return ret;
3290
3291 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
3292 if (ret)
3293 return nv50_mstm_enable(mstm, dpcd[0], 0);
3294
3295 return mstm->mgr.mst_state;
3296}
3297
f479c0ba
BS
3298static void
3299nv50_mstm_fini(struct nv50_mstm *mstm)
3300{
3301 if (mstm && mstm->mgr.mst_state)
3302 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
3303}
3304
3305static void
3306nv50_mstm_init(struct nv50_mstm *mstm)
3307{
3308 if (mstm && mstm->mgr.mst_state)
3309 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
3310}
3311
52aa30f2
BS
3312static void
3313nv50_mstm_del(struct nv50_mstm **pmstm)
3314{
3315 struct nv50_mstm *mstm = *pmstm;
3316 if (mstm) {
3317 kfree(*pmstm);
3318 *pmstm = NULL;
3319 }
3320}
3321
3322static int
3323nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
3324 int conn_base_id, struct nv50_mstm **pmstm)
3325{
3326 const int max_payloads = hweight8(outp->dcb->heads);
3327 struct drm_device *dev = outp->base.base.dev;
3328 struct nv50_mstm *mstm;
f479c0ba
BS
3329 int ret, i;
3330 u8 dpcd;
3331
3332 /* This is a workaround for some monitors not functioning
3333 * correctly in MST mode on initial module load. I think
3334 * some bad interaction with the VBIOS may be responsible.
3335 *
3336 * A good ol' off and on again seems to work here ;)
3337 */
3338 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
3339 if (ret >= 0 && dpcd >= 0x12)
3340 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
52aa30f2
BS
3341
3342 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
3343 return -ENOMEM;
3344 mstm->outp = outp;
f479c0ba 3345 mstm->mgr.cbs = &nv50_mstm;
52aa30f2 3346
7b0a89a6 3347 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
52aa30f2
BS
3348 max_payloads, conn_base_id);
3349 if (ret)
3350 return ret;
3351
f479c0ba
BS
3352 for (i = 0; i < max_payloads; i++) {
3353 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
3354 i, &mstm->msto[i]);
3355 if (ret)
3356 return ret;
3357 }
3358
52aa30f2
BS
3359 return 0;
3360}
3361
26f6d88b
BS
3362/******************************************************************************
3363 * SOR
3364 *****************************************************************************/
83fc083c 3365static void
e225f446 3366nv50_sor_dpms(struct drm_encoder *encoder, int mode)
83fc083c
BS
3367{
3368 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
d55b4af9
BS
3369 struct nv50_disp *disp = nv50_disp(encoder->dev);
3370 struct {
3371 struct nv50_disp_mthd_v1 base;
3372 struct nv50_disp_sor_pwr_v0 pwr;
3373 } args = {
3374 .base.version = 1,
3375 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
3376 .base.hasht = nv_encoder->dcb->hasht,
3377 .base.hashm = nv_encoder->dcb->hashm,
3378 .pwr.state = mode == DRM_MODE_DPMS_ON,
3379 };
83fc083c 3380
8896ceef 3381 nvif_mthd(disp->disp, 0, &args, sizeof(args));
83fc083c
BS
3382}
3383
4cbb0f8d 3384static void
d665c7e9
BS
3385nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
3386 struct drm_display_mode *mode, u8 proto, u8 depth)
4cbb0f8d 3387{
d665c7e9
BS
3388 struct nv50_dmac *core = &nv50_mast(nv_encoder->base.base.dev)->base;
3389 u32 *push;
3390
3391 if (!mode) {
3392 nv_encoder->ctrl &= ~BIT(head);
3393 if (!(nv_encoder->ctrl & 0x0000000f))
3394 nv_encoder->ctrl = 0;
3395 } else {
3396 nv_encoder->ctrl |= proto << 8;
3397 nv_encoder->ctrl |= BIT(head);
3398 }
3399
3400 if ((push = evo_wait(core, 6))) {
3401 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
3402 if (mode) {
3403 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3404 nv_encoder->ctrl |= 0x00001000;
3405 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3406 nv_encoder->ctrl |= 0x00002000;
3407 nv_encoder->ctrl |= depth << 16;
3408 }
e84a35a8 3409 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
e84a35a8 3410 } else {
d665c7e9
BS
3411 if (mode) {
3412 u32 magic = 0x31ec6000 | (head << 25);
3413 u32 syncs = 0x00000001;
3414 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3415 syncs |= 0x00000008;
3416 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3417 syncs |= 0x00000010;
3418 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3419 magic |= 0x00000001;
3420
3421 evo_mthd(push, 0x0404 + (head * 0x300), 2);
3422 evo_data(push, syncs | (depth << 6));
3423 evo_data(push, magic);
3424 }
e84a35a8 3425 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
4cbb0f8d 3426 }
d665c7e9
BS
3427 evo_data(push, nv_encoder->ctrl);
3428 evo_kick(push, core);
4cbb0f8d 3429 }
e84a35a8
BS
3430}
3431
3432static void
839ca903 3433nv50_sor_disable(struct drm_encoder *encoder)
e84a35a8
BS
3434{
3435 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3436 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
419e8dc0 3437
419e8dc0 3438 nv_encoder->crtc = NULL;
e84a35a8
BS
3439
3440 if (nv_crtc) {
839ca903
BS
3441 struct nvkm_i2c_aux *aux = nv_encoder->aux;
3442 u8 pwr;
3443
3444 if (aux) {
3445 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
3446 if (ret == 0) {
3447 pwr &= ~DP_SET_POWER_MASK;
3448 pwr |= DP_SET_POWER_D3;
3449 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
3450 }
3451 }
3452
d665c7e9 3453 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
f20c665c
BS
3454 nv50_audio_disable(encoder, nv_crtc);
3455 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
e84a35a8 3456 }
4cbb0f8d
BS
3457}
3458
83fc083c 3459static void
839ca903 3460nv50_sor_enable(struct drm_encoder *encoder)
83fc083c 3461{
a3761fa2
BS
3462 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3463 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
839ca903 3464 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
a3761fa2
BS
3465 struct {
3466 struct nv50_disp_mthd_v1 base;
3467 struct nv50_disp_sor_lvds_script_v0 lvds;
3468 } lvds = {
3469 .base.version = 1,
3470 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
3471 .base.hasht = nv_encoder->dcb->hasht,
3472 .base.hashm = nv_encoder->dcb->hashm,
3473 };
e225f446 3474 struct nv50_disp *disp = nv50_disp(encoder->dev);
78951d22 3475 struct drm_device *dev = encoder->dev;
77145f1c 3476 struct nouveau_drm *drm = nouveau_drm(dev);
3b6d83d1 3477 struct nouveau_connector *nv_connector;
77145f1c 3478 struct nvbios *bios = &drm->vbios;
419e8dc0
BS
3479 u8 proto = 0xf;
3480 u8 depth = 0x0;
83fc083c 3481
3b6d83d1 3482 nv_connector = nouveau_encoder_connector_get(nv_encoder);
e84a35a8
BS
3483 nv_encoder->crtc = encoder->crtc;
3484
3b6d83d1 3485 switch (nv_encoder->dcb->type) {
cb75d97e 3486 case DCB_OUTPUT_TMDS:
3b6d83d1 3487 if (nv_encoder->dcb->sorconf.link & 1) {
16ef53a9
HM
3488 proto = 0x1;
3489 /* Only enable dual-link if:
3490 * - Need to (i.e. rate > 165MHz)
3491 * - DCB says we can
3492 * - Not an HDMI monitor, since there's no dual-link
3493 * on HDMI.
3494 */
3495 if (mode->clock >= 165000 &&
3496 nv_encoder->dcb->duallink_possible &&
3497 !drm_detect_hdmi_monitor(nv_connector->edid))
3498 proto |= 0x4;
3b6d83d1 3499 } else {
419e8dc0 3500 proto = 0x2;
3b6d83d1
BS
3501 }
3502
f20c665c 3503 nv50_hdmi_enable(&nv_encoder->base.base, mode);
3b6d83d1 3504 break;
cb75d97e 3505 case DCB_OUTPUT_LVDS:
419e8dc0
BS
3506 proto = 0x0;
3507
3b6d83d1
BS
3508 if (bios->fp_no_ddc) {
3509 if (bios->fp.dual_link)
a3761fa2 3510 lvds.lvds.script |= 0x0100;
3b6d83d1 3511 if (bios->fp.if_is_24bit)
a3761fa2 3512 lvds.lvds.script |= 0x0200;
3b6d83d1 3513 } else {
befb51e9 3514 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
3b6d83d1 3515 if (((u8 *)nv_connector->edid)[121] == 2)
a3761fa2 3516 lvds.lvds.script |= 0x0100;
3b6d83d1
BS
3517 } else
3518 if (mode->clock >= bios->fp.duallink_transition_clk) {
a3761fa2 3519 lvds.lvds.script |= 0x0100;
3b6d83d1 3520 }
83fc083c 3521
a3761fa2 3522 if (lvds.lvds.script & 0x0100) {
3b6d83d1 3523 if (bios->fp.strapless_is_24bit & 2)
a3761fa2 3524 lvds.lvds.script |= 0x0200;
3b6d83d1
BS
3525 } else {
3526 if (bios->fp.strapless_is_24bit & 1)
a3761fa2 3527 lvds.lvds.script |= 0x0200;
3b6d83d1
BS
3528 }
3529
3530 if (nv_connector->base.display_info.bpc == 8)
a3761fa2 3531 lvds.lvds.script |= 0x0200;
3b6d83d1 3532 }
4a230fa6 3533
a3761fa2 3534 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
3b6d83d1 3535 break;
cb75d97e 3536 case DCB_OUTPUT_DP:
f20c665c 3537 if (nv_connector->base.display_info.bpc == 6)
419e8dc0 3538 depth = 0x2;
f20c665c
BS
3539 else
3540 if (nv_connector->base.display_info.bpc == 8)
419e8dc0 3541 depth = 0x5;
f20c665c 3542 else
bf2c886a 3543 depth = 0x6;
6e83fda2
BS
3544
3545 if (nv_encoder->dcb->sorconf.link & 1)
419e8dc0 3546 proto = 0x8;
6e83fda2 3547 else
419e8dc0 3548 proto = 0x9;
f20c665c
BS
3549
3550 nv50_audio_enable(encoder, mode);
6e83fda2 3551 break;
3b6d83d1 3552 default:
af7db03e 3553 BUG();
3b6d83d1
BS
3554 break;
3555 }
ff8ff503 3556
d665c7e9 3557 nv_encoder->update(nv_encoder, nv_crtc->index, mode, proto, depth);
83fc083c
BS
3558}
3559
f20c665c
BS
3560static const struct drm_encoder_helper_funcs
3561nv50_sor_help = {
3562 .dpms = nv50_sor_dpms,
839ca903
BS
3563 .atomic_check = nv50_outp_atomic_check,
3564 .enable = nv50_sor_enable,
3565 .disable = nv50_sor_disable,
f20c665c
BS
3566};
3567
83fc083c 3568static void
e225f446 3569nv50_sor_destroy(struct drm_encoder *encoder)
83fc083c 3570{
52aa30f2
BS
3571 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3572 nv50_mstm_del(&nv_encoder->dp.mstm);
83fc083c
BS
3573 drm_encoder_cleanup(encoder);
3574 kfree(encoder);
3575}
3576
f20c665c
BS
3577static const struct drm_encoder_funcs
3578nv50_sor_func = {
e225f446 3579 .destroy = nv50_sor_destroy,
83fc083c
BS
3580};
3581
3582static int
e225f446 3583nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
83fc083c 3584{
52aa30f2 3585 struct nouveau_connector *nv_connector = nouveau_connector(connector);
5ed50209 3586 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1167c6bc 3587 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
83fc083c
BS
3588 struct nouveau_encoder *nv_encoder;
3589 struct drm_encoder *encoder;
52aa30f2 3590 int type, ret;
5ed50209
BS
3591
3592 switch (dcbe->type) {
3593 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
3594 case DCB_OUTPUT_TMDS:
3595 case DCB_OUTPUT_DP:
3596 default:
3597 type = DRM_MODE_ENCODER_TMDS;
3598 break;
3599 }
83fc083c
BS
3600
3601 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3602 if (!nv_encoder)
3603 return -ENOMEM;
3604 nv_encoder->dcb = dcbe;
3605 nv_encoder->or = ffs(dcbe->or) - 1;
d665c7e9 3606 nv_encoder->update = nv50_sor_update;
83fc083c 3607
52aa30f2
BS
3608 encoder = to_drm_encoder(nv_encoder);
3609 encoder->possible_crtcs = dcbe->heads;
3610 encoder->possible_clones = 0;
5a223dac
BS
3611 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
3612 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
f20c665c 3613 drm_encoder_helper_add(encoder, &nv50_sor_help);
52aa30f2
BS
3614
3615 drm_mode_connector_attach_encoder(connector, encoder);
3616
2aa5eac5
BS
3617 if (dcbe->type == DCB_OUTPUT_DP) {
3618 struct nvkm_i2c_aux *aux =
3619 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
3620 if (aux) {
df8dc97c 3621 nv_encoder->i2c = &nv_connector->aux.ddc;
2aa5eac5
BS
3622 nv_encoder->aux = aux;
3623 }
52aa30f2
BS
3624
3625 /*TODO: Use DP Info Table to check for support. */
3626 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
3627 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
3628 nv_connector->base.base.id,
3629 &nv_encoder->dp.mstm);
3630 if (ret)
3631 return ret;
3632 }
2aa5eac5
BS
3633 } else {
3634 struct nvkm_i2c_bus *bus =
3635 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
3636 if (bus)
3637 nv_encoder->i2c = &bus->i2c;
3638 }
3639
83fc083c
BS
3640 return 0;
3641}
26f6d88b 3642
eb6313ad
BS
3643/******************************************************************************
3644 * PIOR
3645 *****************************************************************************/
eb6313ad
BS
3646static void
3647nv50_pior_dpms(struct drm_encoder *encoder, int mode)
3648{
3649 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3650 struct nv50_disp *disp = nv50_disp(encoder->dev);
67cb49c4
BS
3651 struct {
3652 struct nv50_disp_mthd_v1 base;
3653 struct nv50_disp_pior_pwr_v0 pwr;
3654 } args = {
3655 .base.version = 1,
3656 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
3657 .base.hasht = nv_encoder->dcb->hasht,
3658 .base.hashm = nv_encoder->dcb->hashm,
3659 .pwr.state = mode == DRM_MODE_DPMS_ON,
3660 .pwr.type = nv_encoder->dcb->type,
3661 };
3662
3663 nvif_mthd(disp->disp, 0, &args, sizeof(args));
eb6313ad
BS
3664}
3665
839ca903
BS
3666static int
3667nv50_pior_atomic_check(struct drm_encoder *encoder,
3668 struct drm_crtc_state *crtc_state,
3669 struct drm_connector_state *conn_state)
eb6313ad 3670{
839ca903
BS
3671 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
3672 if (ret)
3673 return ret;
3674 crtc_state->adjusted_mode.clock *= 2;
3675 return 0;
eb6313ad
BS
3676}
3677
3678static void
839ca903 3679nv50_pior_disable(struct drm_encoder *encoder)
eb6313ad 3680{
f20c665c
BS
3681 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3682 struct nv50_mast *mast = nv50_mast(encoder->dev);
3683 const int or = nv_encoder->or;
3684 u32 *push;
3685
3686 if (nv_encoder->crtc) {
f20c665c
BS
3687 push = evo_wait(mast, 4);
3688 if (push) {
3689 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
3690 evo_mthd(push, 0x0700 + (or * 0x040), 1);
3691 evo_data(push, 0x00000000);
3692 }
3693 evo_kick(push, mast);
3694 }
3695 }
3696
3697 nv_encoder->crtc = NULL;
eb6313ad
BS
3698}
3699
3700static void
839ca903 3701nv50_pior_enable(struct drm_encoder *encoder)
eb6313ad
BS
3702{
3703 struct nv50_mast *mast = nv50_mast(encoder->dev);
3704 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3705 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3706 struct nouveau_connector *nv_connector;
839ca903 3707 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
eb6313ad
BS
3708 u8 owner = 1 << nv_crtc->index;
3709 u8 proto, depth;
3710 u32 *push;
3711
3712 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3713 switch (nv_connector->base.display_info.bpc) {
3714 case 10: depth = 0x6; break;
3715 case 8: depth = 0x5; break;
3716 case 6: depth = 0x2; break;
3717 default: depth = 0x0; break;
3718 }
3719
3720 switch (nv_encoder->dcb->type) {
3721 case DCB_OUTPUT_TMDS:
3722 case DCB_OUTPUT_DP:
3723 proto = 0x0;
3724 break;
3725 default:
af7db03e 3726 BUG();
eb6313ad
BS
3727 break;
3728 }
3729
eb6313ad
BS
3730 push = evo_wait(mast, 8);
3731 if (push) {
648d4dfd 3732 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
eb6313ad
BS
3733 u32 ctrl = (depth << 16) | (proto << 8) | owner;
3734 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3735 ctrl |= 0x00001000;
3736 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3737 ctrl |= 0x00002000;
3738 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
3739 evo_data(push, ctrl);
3740 }
3741
3742 evo_kick(push, mast);
3743 }
3744
3745 nv_encoder->crtc = encoder->crtc;
3746}
3747
f20c665c
BS
3748static const struct drm_encoder_helper_funcs
3749nv50_pior_help = {
eb6313ad 3750 .dpms = nv50_pior_dpms,
839ca903
BS
3751 .atomic_check = nv50_pior_atomic_check,
3752 .enable = nv50_pior_enable,
3753 .disable = nv50_pior_disable,
eb6313ad
BS
3754};
3755
f20c665c
BS
3756static void
3757nv50_pior_destroy(struct drm_encoder *encoder)
3758{
3759 drm_encoder_cleanup(encoder);
3760 kfree(encoder);
3761}
3762
3763static const struct drm_encoder_funcs
3764nv50_pior_func = {
eb6313ad
BS
3765 .destroy = nv50_pior_destroy,
3766};
3767
3768static int
3769nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
3770{
df8dc97c 3771 struct nouveau_connector *nv_connector = nouveau_connector(connector);
eb6313ad 3772 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1167c6bc 3773 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
2aa5eac5
BS
3774 struct nvkm_i2c_bus *bus = NULL;
3775 struct nvkm_i2c_aux *aux = NULL;
3776 struct i2c_adapter *ddc;
eb6313ad
BS
3777 struct nouveau_encoder *nv_encoder;
3778 struct drm_encoder *encoder;
3779 int type;
3780
3781 switch (dcbe->type) {
3782 case DCB_OUTPUT_TMDS:
2aa5eac5
BS
3783 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
3784 ddc = bus ? &bus->i2c : NULL;
eb6313ad
BS
3785 type = DRM_MODE_ENCODER_TMDS;
3786 break;
3787 case DCB_OUTPUT_DP:
2aa5eac5 3788 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
df8dc97c 3789 ddc = aux ? &nv_connector->aux.ddc : NULL;
eb6313ad
BS
3790 type = DRM_MODE_ENCODER_TMDS;
3791 break;
3792 default:
3793 return -ENODEV;
3794 }
3795
3796 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3797 if (!nv_encoder)
3798 return -ENOMEM;
3799 nv_encoder->dcb = dcbe;
3800 nv_encoder->or = ffs(dcbe->or) - 1;
3801 nv_encoder->i2c = ddc;
2aa5eac5 3802 nv_encoder->aux = aux;
eb6313ad
BS
3803
3804 encoder = to_drm_encoder(nv_encoder);
3805 encoder->possible_crtcs = dcbe->heads;
3806 encoder->possible_clones = 0;
5a223dac
BS
3807 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
3808 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
f20c665c 3809 drm_encoder_helper_add(encoder, &nv50_pior_help);
eb6313ad
BS
3810
3811 drm_mode_connector_attach_encoder(connector, encoder);
3812 return 0;
3813}
3814
839ca903
BS
3815/******************************************************************************
3816 * Atomic
3817 *****************************************************************************/
3818
3819static void
3820nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
3821{
3822 struct nv50_disp *disp = nv50_disp(drm->dev);
3823 struct nv50_dmac *core = &disp->mast.base;
f479c0ba
BS
3824 struct nv50_mstm *mstm;
3825 struct drm_encoder *encoder;
839ca903
BS
3826 u32 *push;
3827
3828 NV_ATOMIC(drm, "commit core %08x\n", interlock);
3829
f479c0ba
BS
3830 drm_for_each_encoder(encoder, drm->dev) {
3831 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3832 mstm = nouveau_encoder(encoder)->dp.mstm;
3833 if (mstm && mstm->modified)
3834 nv50_mstm_prepare(mstm);
3835 }
3836 }
3837
839ca903
BS
3838 if ((push = evo_wait(core, 5))) {
3839 evo_mthd(push, 0x0084, 1);
3840 evo_data(push, 0x80000000);
3841 evo_mthd(push, 0x0080, 2);
3842 evo_data(push, interlock);
3843 evo_data(push, 0x00000000);
3844 nouveau_bo_wr32(disp->sync, 0, 0x00000000);
3845 evo_kick(push, core);
1167c6bc 3846 if (nvif_msec(&drm->client.device, 2000ULL,
839ca903
BS
3847 if (nouveau_bo_rd32(disp->sync, 0))
3848 break;
3849 usleep_range(1, 2);
3850 ) < 0)
3851 NV_ERROR(drm, "EVO timeout\n");
3852 }
f479c0ba
BS
3853
3854 drm_for_each_encoder(encoder, drm->dev) {
3855 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3856 mstm = nouveau_encoder(encoder)->dp.mstm;
3857 if (mstm && mstm->modified)
3858 nv50_mstm_cleanup(mstm);
3859 }
3860 }
839ca903
BS
3861}
3862
3863static void
3864nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
3865{
3866 struct drm_device *dev = state->dev;
3867 struct drm_crtc_state *crtc_state;
3868 struct drm_crtc *crtc;
3869 struct drm_plane_state *plane_state;
3870 struct drm_plane *plane;
3871 struct nouveau_drm *drm = nouveau_drm(dev);
3872 struct nv50_disp *disp = nv50_disp(dev);
3873 struct nv50_atom *atom = nv50_atom(state);
3874 struct nv50_outp_atom *outp, *outt;
3875 u32 interlock_core = 0;
3876 u32 interlock_chan = 0;
3877 int i;
3878
3879 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
3880 drm_atomic_helper_wait_for_fences(dev, state, false);
3881 drm_atomic_helper_wait_for_dependencies(state);
3882 drm_atomic_helper_update_legacy_modeset_state(dev, state);
3883
3884 if (atom->lock_core)
3885 mutex_lock(&disp->mutex);
3886
3887 /* Disable head(s). */
3888 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3889 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3890 struct nv50_head *head = nv50_head(crtc);
3891
3892 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
3893 asyh->clr.mask, asyh->set.mask);
3894
3895 if (asyh->clr.mask) {
3896 nv50_head_flush_clr(head, asyh, atom->flush_disable);
3897 interlock_core |= 1;
3898 }
3899 }
3900
3901 /* Disable plane(s). */
3902 for_each_plane_in_state(state, plane, plane_state, i) {
3903 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3904 struct nv50_wndw *wndw = nv50_wndw(plane);
3905
3906 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
3907 asyw->clr.mask, asyw->set.mask);
3908 if (!asyw->clr.mask)
3909 continue;
3910
3911 interlock_chan |= nv50_wndw_flush_clr(wndw, interlock_core,
3912 atom->flush_disable,
3913 asyw);
3914 }
3915
3916 /* Disable output path(s). */
3917 list_for_each_entry(outp, &atom->outp, head) {
3918 const struct drm_encoder_helper_funcs *help;
3919 struct drm_encoder *encoder;
3920
3921 encoder = outp->encoder;
3922 help = encoder->helper_private;
3923
3924 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
3925 outp->clr.mask, outp->set.mask);
3926
3927 if (outp->clr.mask) {
3928 help->disable(encoder);
3929 interlock_core |= 1;
3930 if (outp->flush_disable) {
3931 nv50_disp_atomic_commit_core(drm, interlock_chan);
3932 interlock_core = 0;
3933 interlock_chan = 0;
3934 }
3935 }
3936 }
3937
3938 /* Flush disable. */
3939 if (interlock_core) {
3940 if (atom->flush_disable) {
3941 nv50_disp_atomic_commit_core(drm, interlock_chan);
3942 interlock_core = 0;
3943 interlock_chan = 0;
3944 }
3945 }
3946
3947 /* Update output path(s). */
3948 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
3949 const struct drm_encoder_helper_funcs *help;
3950 struct drm_encoder *encoder;
3951
3952 encoder = outp->encoder;
3953 help = encoder->helper_private;
3954
3955 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
3956 outp->set.mask, outp->clr.mask);
3957
3958 if (outp->set.mask) {
3959 help->enable(encoder);
3960 interlock_core = 1;
3961 }
3962
3963 list_del(&outp->head);
3964 kfree(outp);
3965 }
3966
3967 /* Update head(s). */
3968 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3969 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3970 struct nv50_head *head = nv50_head(crtc);
3971
3972 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
3973 asyh->set.mask, asyh->clr.mask);
3974
3975 if (asyh->set.mask) {
3976 nv50_head_flush_set(head, asyh);
3977 interlock_core = 1;
3978 }
3979 }
3980
2b507893
BS
3981 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3982 if (crtc->state->event)
3983 drm_crtc_vblank_get(crtc);
3984 }
3985
839ca903
BS
3986 /* Update plane(s). */
3987 for_each_plane_in_state(state, plane, plane_state, i) {
3988 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3989 struct nv50_wndw *wndw = nv50_wndw(plane);
3990
3991 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
3992 asyw->set.mask, asyw->clr.mask);
3993 if ( !asyw->set.mask &&
3994 (!asyw->clr.mask || atom->flush_disable))
3995 continue;
3996
3997 interlock_chan |= nv50_wndw_flush_set(wndw, interlock_core, asyw);
3998 }
3999
4000 /* Flush update. */
4001 if (interlock_core) {
4002 if (!interlock_chan && atom->state.legacy_cursor_update) {
4003 u32 *push = evo_wait(&disp->mast, 2);
4004 if (push) {
4005 evo_mthd(push, 0x0080, 1);
4006 evo_data(push, 0x00000000);
4007 evo_kick(push, &disp->mast);
4008 }
4009 } else {
4010 nv50_disp_atomic_commit_core(drm, interlock_chan);
4011 }
4012 }
4013
4014 if (atom->lock_core)
4015 mutex_unlock(&disp->mutex);
4016
4017 /* Wait for HW to signal completion. */
4018 for_each_plane_in_state(state, plane, plane_state, i) {
4019 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
4020 struct nv50_wndw *wndw = nv50_wndw(plane);
4021 int ret = nv50_wndw_wait_armed(wndw, asyw);
4022 if (ret)
4023 NV_ERROR(drm, "%s: timeout\n", plane->name);
4024 }
4025
4026 for_each_crtc_in_state(state, crtc, crtc_state, i) {
4027 if (crtc->state->event) {
4028 unsigned long flags;
bd9f6605
MK
4029 /* Get correct count/ts if racing with vblank irq */
4030 drm_accurate_vblank_count(crtc);
839ca903
BS
4031 spin_lock_irqsave(&crtc->dev->event_lock, flags);
4032 drm_crtc_send_vblank_event(crtc, crtc->state->event);
4033 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4034 crtc->state->event = NULL;
2b507893 4035 drm_crtc_vblank_put(crtc);
839ca903
BS
4036 }
4037 }
4038
4039 drm_atomic_helper_commit_hw_done(state);
4040 drm_atomic_helper_cleanup_planes(dev, state);
4041 drm_atomic_helper_commit_cleanup_done(state);
4042 drm_atomic_state_put(state);
4043}
4044
4045static void
4046nv50_disp_atomic_commit_work(struct work_struct *work)
4047{
4048 struct drm_atomic_state *state =
4049 container_of(work, typeof(*state), commit_work);
4050 nv50_disp_atomic_commit_tail(state);
4051}
4052
4053static int
4054nv50_disp_atomic_commit(struct drm_device *dev,
4055 struct drm_atomic_state *state, bool nonblock)
4056{
4057 struct nouveau_drm *drm = nouveau_drm(dev);
4058 struct nv50_disp *disp = nv50_disp(dev);
4059 struct drm_plane_state *plane_state;
4060 struct drm_plane *plane;
4061 struct drm_crtc *crtc;
4062 bool active = false;
4063 int ret, i;
4064
4065 ret = pm_runtime_get_sync(dev->dev);
4066 if (ret < 0 && ret != -EACCES)
4067 return ret;
4068
4069 ret = drm_atomic_helper_setup_commit(state, nonblock);
4070 if (ret)
4071 goto done;
4072
4073 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
4074
4075 ret = drm_atomic_helper_prepare_planes(dev, state);
4076 if (ret)
4077 goto done;
4078
4079 if (!nonblock) {
4080 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
4081 if (ret)
4082 goto done;
4083 }
4084
4085 for_each_plane_in_state(state, plane, plane_state, i) {
4086 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane_state);
4087 struct nv50_wndw *wndw = nv50_wndw(plane);
4088 if (asyw->set.image) {
4089 asyw->ntfy.handle = wndw->dmac->sync.handle;
4090 asyw->ntfy.offset = wndw->ntfy;
4091 asyw->ntfy.awaken = false;
4092 asyw->set.ntfy = true;
4093 nouveau_bo_wr32(disp->sync, wndw->ntfy / 4, 0x00000000);
4094 wndw->ntfy ^= 0x10;
4095 }
4096 }
4097
4098 drm_atomic_helper_swap_state(state, true);
4099 drm_atomic_state_get(state);
4100
4101 if (nonblock)
4102 queue_work(system_unbound_wq, &state->commit_work);
4103 else
4104 nv50_disp_atomic_commit_tail(state);
4105
4106 drm_for_each_crtc(crtc, dev) {
4107 if (crtc->state->enable) {
4108 if (!drm->have_disp_power_ref) {
4109 drm->have_disp_power_ref = true;
4110 return ret;
4111 }
4112 active = true;
4113 break;
4114 }
4115 }
4116
4117 if (!active && drm->have_disp_power_ref) {
4118 pm_runtime_put_autosuspend(dev->dev);
4119 drm->have_disp_power_ref = false;
4120 }
4121
4122done:
4123 pm_runtime_put_autosuspend(dev->dev);
4124 return ret;
4125}
4126
4127static struct nv50_outp_atom *
4128nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
4129{
4130 struct nv50_outp_atom *outp;
4131
4132 list_for_each_entry(outp, &atom->outp, head) {
4133 if (outp->encoder == encoder)
4134 return outp;
4135 }
4136
4137 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
4138 if (!outp)
4139 return ERR_PTR(-ENOMEM);
4140
4141 list_add(&outp->head, &atom->outp);
4142 outp->encoder = encoder;
4143 return outp;
4144}
4145
4146static int
4147nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
4148 struct drm_connector *connector)
4149{
4150 struct drm_encoder *encoder = connector->state->best_encoder;
4151 struct drm_crtc_state *crtc_state;
4152 struct drm_crtc *crtc;
4153 struct nv50_outp_atom *outp;
4154
4155 if (!(crtc = connector->state->crtc))
4156 return 0;
4157
4158 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
4159 if (crtc->state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
4160 outp = nv50_disp_outp_atomic_add(atom, encoder);
4161 if (IS_ERR(outp))
4162 return PTR_ERR(outp);
4163
4164 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
4165 outp->flush_disable = true;
4166 atom->flush_disable = true;
4167 }
4168 outp->clr.ctrl = true;
4169 atom->lock_core = true;
4170 }
4171
4172 return 0;
4173}
4174
4175static int
4176nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
4177 struct drm_connector_state *connector_state)
4178{
4179 struct drm_encoder *encoder = connector_state->best_encoder;
4180 struct drm_crtc_state *crtc_state;
4181 struct drm_crtc *crtc;
4182 struct nv50_outp_atom *outp;
4183
4184 if (!(crtc = connector_state->crtc))
4185 return 0;
4186
4187 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
4188 if (crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
4189 outp = nv50_disp_outp_atomic_add(atom, encoder);
4190 if (IS_ERR(outp))
4191 return PTR_ERR(outp);
4192
4193 outp->set.ctrl = true;
4194 atom->lock_core = true;
4195 }
4196
4197 return 0;
4198}
4199
4200static int
4201nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
4202{
4203 struct nv50_atom *atom = nv50_atom(state);
4204 struct drm_connector_state *connector_state;
4205 struct drm_connector *connector;
4206 int ret, i;
4207
4208 ret = drm_atomic_helper_check(dev, state);
4209 if (ret)
4210 return ret;
4211
4212 for_each_connector_in_state(state, connector, connector_state, i) {
4213 ret = nv50_disp_outp_atomic_check_clr(atom, connector);
4214 if (ret)
4215 return ret;
4216
4217 ret = nv50_disp_outp_atomic_check_set(atom, connector_state);
4218 if (ret)
4219 return ret;
4220 }
4221
4222 return 0;
4223}
4224
4225static void
4226nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
4227{
4228 struct nv50_atom *atom = nv50_atom(state);
4229 struct nv50_outp_atom *outp, *outt;
4230
4231 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
4232 list_del(&outp->head);
4233 kfree(outp);
4234 }
4235
4236 drm_atomic_state_default_clear(state);
4237}
4238
4239static void
4240nv50_disp_atomic_state_free(struct drm_atomic_state *state)
4241{
4242 struct nv50_atom *atom = nv50_atom(state);
4243 drm_atomic_state_default_release(&atom->state);
4244 kfree(atom);
4245}
4246
4247static struct drm_atomic_state *
4248nv50_disp_atomic_state_alloc(struct drm_device *dev)
4249{
4250 struct nv50_atom *atom;
4251 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
4252 drm_atomic_state_init(dev, &atom->state) < 0) {
4253 kfree(atom);
4254 return NULL;
4255 }
4256 INIT_LIST_HEAD(&atom->outp);
4257 return &atom->state;
4258}
4259
4260static const struct drm_mode_config_funcs
4261nv50_disp_func = {
4262 .fb_create = nouveau_user_framebuffer_create,
4263 .output_poll_changed = nouveau_fbcon_output_poll_changed,
4264 .atomic_check = nv50_disp_atomic_check,
4265 .atomic_commit = nv50_disp_atomic_commit,
4266 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
4267 .atomic_state_clear = nv50_disp_atomic_state_clear,
4268 .atomic_state_free = nv50_disp_atomic_state_free,
4269};
4270
26f6d88b
BS
4271/******************************************************************************
4272 * Init
4273 *****************************************************************************/
ab0af559 4274
2a44e499 4275void
e225f446 4276nv50_display_fini(struct drm_device *dev)
26f6d88b 4277{
f479c0ba
BS
4278 struct nouveau_encoder *nv_encoder;
4279 struct drm_encoder *encoder;
973f10c2
BS
4280 struct drm_plane *plane;
4281
4282 drm_for_each_plane(plane, dev) {
4283 struct nv50_wndw *wndw = nv50_wndw(plane);
4284 if (plane->funcs != &nv50_wndw)
4285 continue;
4286 nv50_wndw_fini(wndw);
4287 }
f479c0ba
BS
4288
4289 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4290 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
4291 nv_encoder = nouveau_encoder(encoder);
4292 nv50_mstm_fini(nv_encoder->dp.mstm);
4293 }
4294 }
26f6d88b
BS
4295}
4296
4297int
e225f446 4298nv50_display_init(struct drm_device *dev)
26f6d88b 4299{
354d3508 4300 struct drm_encoder *encoder;
973f10c2 4301 struct drm_plane *plane;
9f9bdaaf
BS
4302 struct drm_crtc *crtc;
4303 u32 *push;
4304
4305 push = evo_wait(nv50_mast(dev), 32);
4306 if (!push)
4307 return -EBUSY;
4308
9f9bdaaf 4309 evo_mthd(push, 0x0088, 1);
f45f55c4 4310 evo_data(push, nv50_mast(dev)->base.sync.handle);
9f9bdaaf 4311 evo_kick(push, nv50_mast(dev));
973f10c2 4312
354d3508
BS
4313 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4314 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
4315 const struct drm_encoder_helper_funcs *help;
4316 struct nouveau_encoder *nv_encoder;
4317
4318 nv_encoder = nouveau_encoder(encoder);
354d3508
BS
4319 help = encoder->helper_private;
4320 if (help && help->dpms)
4321 help->dpms(encoder, DRM_MODE_DPMS_ON);
f479c0ba
BS
4322
4323 nv50_mstm_init(nv_encoder->dp.mstm);
354d3508
BS
4324 }
4325 }
4326
e1ef6b42 4327 drm_for_each_crtc(crtc, dev) {
9bfdee9a 4328 nv50_head_lut_load(crtc);
e1ef6b42
BS
4329 }
4330
973f10c2
BS
4331 drm_for_each_plane(plane, dev) {
4332 struct nv50_wndw *wndw = nv50_wndw(plane);
4333 if (plane->funcs != &nv50_wndw)
4334 continue;
4335 nv50_wndw_init(wndw);
4336 }
4337
9f9bdaaf 4338 return 0;
26f6d88b
BS
4339}
4340
4341void
e225f446 4342nv50_display_destroy(struct drm_device *dev)
26f6d88b 4343{
e225f446 4344 struct nv50_disp *disp = nv50_disp(dev);
bdb8c212 4345
0ad72863 4346 nv50_dmac_destroy(&disp->mast.base, disp->disp);
26f6d88b 4347
816af2f2 4348 nouveau_bo_unmap(disp->sync);
04c8c210
MS
4349 if (disp->sync)
4350 nouveau_bo_unpin(disp->sync);
816af2f2 4351 nouveau_bo_ref(NULL, &disp->sync);
51beb428 4352
77145f1c 4353 nouveau_display(dev)->priv = NULL;
26f6d88b
BS
4354 kfree(disp);
4355}
4356
839ca903
BS
4357MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
4358static int nouveau_atomic = 0;
4359module_param_named(atomic, nouveau_atomic, int, 0400);
4360
26f6d88b 4361int
e225f446 4362nv50_display_create(struct drm_device *dev)
26f6d88b 4363{
1167c6bc 4364 struct nvif_device *device = &nouveau_drm(dev)->client.device;
77145f1c 4365 struct nouveau_drm *drm = nouveau_drm(dev);
77145f1c 4366 struct dcb_table *dcb = &drm->vbios.dcb;
83fc083c 4367 struct drm_connector *connector, *tmp;
e225f446 4368 struct nv50_disp *disp;
cb75d97e 4369 struct dcb_output *dcbe;
7c5f6a87 4370 int crtcs, ret, i;
26f6d88b
BS
4371
4372 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
4373 if (!disp)
4374 return -ENOMEM;
77145f1c 4375
839ca903
BS
4376 mutex_init(&disp->mutex);
4377
77145f1c 4378 nouveau_display(dev)->priv = disp;
e225f446
BS
4379 nouveau_display(dev)->dtor = nv50_display_destroy;
4380 nouveau_display(dev)->init = nv50_display_init;
4381 nouveau_display(dev)->fini = nv50_display_fini;
0ad72863 4382 disp->disp = &nouveau_display(dev)->disp;
839ca903
BS
4383 dev->mode_config.funcs = &nv50_disp_func;
4384 if (nouveau_atomic)
4385 dev->driver->driver_features |= DRIVER_ATOMIC;
26f6d88b 4386
b5a794b0 4387 /* small shared memory area we use for notifiers and semaphores */
bab7cc18 4388 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
bb6178b0 4389 0, 0x0000, NULL, NULL, &disp->sync);
b5a794b0 4390 if (!ret) {
547ad072 4391 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
04c8c210 4392 if (!ret) {
b5a794b0 4393 ret = nouveau_bo_map(disp->sync);
04c8c210
MS
4394 if (ret)
4395 nouveau_bo_unpin(disp->sync);
4396 }
b5a794b0
BS
4397 if (ret)
4398 nouveau_bo_ref(NULL, &disp->sync);
4399 }
4400
b5a794b0
BS
4401 if (ret)
4402 goto out;
4403
4404 /* allocate master evo channel */
a01ca78c 4405 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
410f3ec6 4406 &disp->mast);
b5a794b0
BS
4407 if (ret)
4408 goto out;
4409
438d99e3 4410 /* create crtc objects to represent the hw heads */
648d4dfd 4411 if (disp->disp->oclass >= GF110_DISP)
a01ca78c 4412 crtcs = nvif_rd32(&device->object, 0x022448);
63718a07
BS
4413 else
4414 crtcs = 2;
4415
7c5f6a87 4416 for (i = 0; i < crtcs; i++) {
9bfdee9a 4417 ret = nv50_head_create(dev, i);
438d99e3
BS
4418 if (ret)
4419 goto out;
4420 }
4421
83fc083c
BS
4422 /* create encoder/connector objects based on VBIOS DCB table */
4423 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
4424 connector = nouveau_connector_create(dev, dcbe->connector);
4425 if (IS_ERR(connector))
4426 continue;
4427
eb6313ad
BS
4428 if (dcbe->location == DCB_LOC_ON_CHIP) {
4429 switch (dcbe->type) {
4430 case DCB_OUTPUT_TMDS:
4431 case DCB_OUTPUT_LVDS:
4432 case DCB_OUTPUT_DP:
4433 ret = nv50_sor_create(connector, dcbe);
4434 break;
4435 case DCB_OUTPUT_ANALOG:
4436 ret = nv50_dac_create(connector, dcbe);
4437 break;
4438 default:
4439 ret = -ENODEV;
4440 break;
4441 }
4442 } else {
4443 ret = nv50_pior_create(connector, dcbe);
83fc083c
BS
4444 }
4445
eb6313ad
BS
4446 if (ret) {
4447 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
4448 dcbe->location, dcbe->type,
4449 ffs(dcbe->or) - 1, ret);
94f54f53 4450 ret = 0;
83fc083c
BS
4451 }
4452 }
4453
4454 /* cull any connectors we created that don't have an encoder */
4455 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
4456 if (connector->encoder_ids[0])
4457 continue;
4458
77145f1c 4459 NV_WARN(drm, "%s has no encoders, removing\n",
8c6c361a 4460 connector->name);
83fc083c
BS
4461 connector->funcs->destroy(connector);
4462 }
4463
26f6d88b
BS
4464out:
4465 if (ret)
e225f446 4466 nv50_display_destroy(dev);
26f6d88b
BS
4467 return ret;
4468}