blob: b22c37bde13ffb97aa9d5a1ca557c93ef4294d1f [file] [log] [blame]
Ben Skeggs56d237d2014-05-19 14:54:33 +10001/*
Ben Skeggs26f6d882011-07-04 16:25:18 +10002 * 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
Ben Skeggs51beb422011-07-05 10:33:08 +100025#include <linux/dma-mapping.h>
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -040026#include <linux/hdmi.h>
Ben Skeggs83fc0832011-07-05 13:08:40 +100027
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
Ben Skeggsad633612016-11-04 17:20:36 +100029#include <drm/drm_atomic.h>
Ben Skeggs973f10c2016-11-04 17:20:36 +100030#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drm_crtc_helper.h>
Ben Skeggs48743222014-05-31 01:48:06 +100032#include <drm/drm_dp_helper.h>
Daniel Vetterb516a9e2015-12-04 09:45:43 +010033#include <drm/drm_fb_helper.h>
Ben Skeggsad633612016-11-04 17:20:36 +100034#include <drm/drm_plane_helper.h>
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -040035#include <drm/drm_edid.h>
Ben Skeggs26f6d882011-07-04 16:25:18 +100036
Ben Skeggsfdb751e2014-08-10 04:10:23 +100037#include <nvif/class.h>
Ben Skeggs845f2722015-11-08 12:16:40 +100038#include <nvif/cl0002.h>
Ben Skeggs7568b102015-11-08 10:44:19 +100039#include <nvif/cl5070.h>
40#include <nvif/cl507a.h>
41#include <nvif/cl507b.h>
42#include <nvif/cl507c.h>
43#include <nvif/cl507d.h>
44#include <nvif/cl507e.h>
Ben Skeggs973f10c2016-11-04 17:20:36 +100045#include <nvif/event.h>
Ben Skeggsfdb751e2014-08-10 04:10:23 +100046
Ben Skeggs4dc28132016-05-20 09:22:55 +100047#include "nouveau_drv.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100048#include "nouveau_dma.h"
49#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100050#include "nouveau_connector.h"
51#include "nouveau_encoder.h"
52#include "nouveau_crtc.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100053#include "nouveau_fence.h"
Ben Skeggs839ca902016-11-04 17:20:36 +100054#include "nouveau_fbcon.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100055#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100056
Ben Skeggs8a464382011-11-12 23:52:07 +100057#define EVO_DMA_NR 9
58
Ben Skeggsbdb8c212011-11-12 01:30:24 +100059#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100060#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100061#define EVO_OVLY(c) (0x05 + (c))
62#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100063#define EVO_CURS(c) (0x0d + (c))
64
Ben Skeggs816af2f2011-11-16 15:48:48 +100065/* offsets in shared sync bo of various structures */
66#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100067#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
68#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
69#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs973f10c2016-11-04 17:20:36 +100070#define EVO_FLIP_NTFY0(c) EVO_SYNC((c) + 1, 0x20)
71#define EVO_FLIP_NTFY1(c) EVO_SYNC((c) + 1, 0x30)
Ben Skeggs816af2f2011-11-16 15:48:48 +100072
Ben Skeggsb5a794b2012-10-16 14:18:32 +100073/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +100074 * Atomic state
75 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +100076#define nv50_atom(p) container_of((p), struct nv50_atom, state)
77
78struct nv50_atom {
79 struct drm_atomic_state state;
80
81 struct list_head outp;
82 bool lock_core;
83 bool flush_disable;
84};
85
86struct nv50_outp_atom {
87 struct list_head head;
88
89 struct drm_encoder *encoder;
90 bool flush_disable;
91
92 union {
93 struct {
94 bool ctrl:1;
95 };
96 u8 mask;
97 } clr;
98
99 union {
100 struct {
101 bool ctrl:1;
102 };
103 u8 mask;
104 } set;
105};
106
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000107#define nv50_head_atom(p) container_of((p), struct nv50_head_atom, state)
108
109struct nv50_head_atom {
110 struct drm_crtc_state state;
111
Ben Skeggsc4e68122016-11-04 17:20:36 +1000112 struct {
113 u16 iW;
114 u16 iH;
115 u16 oW;
116 u16 oH;
117 } view;
118
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000119 struct nv50_head_mode {
120 bool interlace;
121 u32 clock;
122 struct {
123 u16 active;
124 u16 synce;
125 u16 blanke;
126 u16 blanks;
127 } h;
128 struct {
129 u32 active;
130 u16 synce;
131 u16 blanke;
132 u16 blanks;
133 u16 blank2s;
134 u16 blank2e;
135 u16 blankus;
136 } v;
137 } mode;
138
Ben Skeggsad633612016-11-04 17:20:36 +1000139 struct {
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000140 u32 handle;
141 u64 offset:40;
142 } lut;
143
144 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000145 bool visible;
146 u32 handle;
147 u64 offset:40;
148 u8 format;
149 u8 kind:7;
150 u8 layout:1;
151 u8 block:4;
152 u32 pitch:20;
153 u16 x;
154 u16 y;
155 u16 w;
156 u16 h;
157 } core;
158
159 struct {
Ben Skeggsea8ee392016-11-04 17:20:36 +1000160 bool visible;
161 u32 handle;
162 u64 offset:40;
163 u8 layout:1;
164 u8 format:1;
165 } curs;
166
167 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000168 u8 depth;
169 u8 cpp;
170 u16 x;
171 u16 y;
172 u16 w;
173 u16 h;
174 } base;
175
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000176 struct {
177 u8 cpp;
178 } ovly;
179
Ben Skeggs7e918332016-11-04 17:20:36 +1000180 struct {
181 bool enable:1;
182 u8 bits:2;
183 u8 mode:4;
184 } dither;
185
Ben Skeggs7e08d672016-11-04 17:20:36 +1000186 struct {
187 struct {
188 u16 cos:12;
189 u16 sin:12;
190 } sat;
191 } procamp;
192
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000193 union {
194 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000195 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000196 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000197 };
198 u8 mask;
199 } clr;
200
201 union {
202 struct {
203 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000204 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000205 bool view:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000206 bool mode:1;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000207 bool base:1;
208 bool ovly:1;
Ben Skeggs7e918332016-11-04 17:20:36 +1000209 bool dither:1;
Ben Skeggs7e08d672016-11-04 17:20:36 +1000210 bool procamp:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000211 };
212 u16 mask;
213 } set;
214};
215
Ben Skeggs839ca902016-11-04 17:20:36 +1000216static inline struct nv50_head_atom *
217nv50_head_atom_get(struct drm_atomic_state *state, struct drm_crtc *crtc)
218{
219 struct drm_crtc_state *statec = drm_atomic_get_crtc_state(state, crtc);
220 if (IS_ERR(statec))
221 return (void *)statec;
222 return nv50_head_atom(statec);
223}
224
Ben Skeggs973f10c2016-11-04 17:20:36 +1000225#define nv50_wndw_atom(p) container_of((p), struct nv50_wndw_atom, state)
226
227struct nv50_wndw_atom {
228 struct drm_plane_state state;
229 u8 interval;
230
231 struct drm_rect clip;
232
233 struct {
234 u32 handle;
235 u16 offset:12;
236 bool awaken:1;
237 } ntfy;
238
239 struct {
240 u32 handle;
241 u16 offset:12;
242 u32 acquire;
243 u32 release;
244 } sema;
245
246 struct {
247 u8 enable:2;
248 } lut;
249
250 struct {
251 u8 mode:2;
252 u8 interval:4;
253
254 u8 format;
255 u8 kind:7;
256 u8 layout:1;
257 u8 block:4;
258 u32 pitch:20;
259 u16 w;
260 u16 h;
261
262 u32 handle;
263 u64 offset;
264 } image;
265
266 struct {
267 u16 x;
268 u16 y;
269 } point;
270
271 union {
272 struct {
273 bool ntfy:1;
274 bool sema:1;
275 bool image:1;
276 };
277 u8 mask;
278 } clr;
279
280 union {
281 struct {
282 bool ntfy:1;
283 bool sema:1;
284 bool image:1;
285 bool lut:1;
286 bool point:1;
287 };
288 u8 mask;
289 } set;
290};
291
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000292/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000293 * EVO channel
294 *****************************************************************************/
295
Ben Skeggse225f442012-11-21 14:40:21 +1000296struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000297 struct nvif_object user;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000298 struct nvif_device *device;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000299};
300
301static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000302nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000303 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000304 struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000305{
Ben Skeggs41a63402015-08-20 14:54:16 +1000306 struct nvif_sclass *sclass;
307 int ret, i, n;
Ben Skeggs6af52892014-11-03 15:01:33 +1000308
Ben Skeggsa01ca782015-08-20 14:54:15 +1000309 chan->device = device;
310
Ben Skeggs41a63402015-08-20 14:54:16 +1000311 ret = n = nvif_object_sclass_get(disp, &sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000312 if (ret < 0)
313 return ret;
314
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000315 while (oclass[0]) {
Ben Skeggs41a63402015-08-20 14:54:16 +1000316 for (i = 0; i < n; i++) {
317 if (sclass[i].oclass == oclass[0]) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000318 ret = nvif_object_init(disp, 0, oclass[0],
Ben Skeggsa01ca782015-08-20 14:54:15 +1000319 data, size, &chan->user);
Ben Skeggs6af52892014-11-03 15:01:33 +1000320 if (ret == 0)
Ben Skeggs01326052017-11-01 03:56:19 +1000321 nvif_object_map(&chan->user, NULL, 0);
Ben Skeggs41a63402015-08-20 14:54:16 +1000322 nvif_object_sclass_put(&sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000323 return ret;
324 }
Ben Skeggsb76f1522014-08-10 04:10:28 +1000325 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000326 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000327 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000328
Ben Skeggs41a63402015-08-20 14:54:16 +1000329 nvif_object_sclass_put(&sclass);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000330 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000331}
332
333static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000334nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000335{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000336 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000337}
338
339/******************************************************************************
340 * PIO EVO channel
341 *****************************************************************************/
342
Ben Skeggse225f442012-11-21 14:40:21 +1000343struct nv50_pioc {
344 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000345};
346
347static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000348nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000349{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000350 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000351}
352
353static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000354nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000355 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000356 struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000357{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000358 return nv50_chan_create(device, disp, oclass, head, data, size,
359 &pioc->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000360}
361
362/******************************************************************************
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000363 * Overlay Immediate
364 *****************************************************************************/
365
366struct nv50_oimm {
367 struct nv50_pioc base;
368};
369
370static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000371nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
372 int head, struct nv50_oimm *oimm)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000373{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000374 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000375 .head = head,
376 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000377 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000378 GK104_DISP_OVERLAY,
379 GF110_DISP_OVERLAY,
380 GT214_DISP_OVERLAY,
381 G82_DISP_OVERLAY,
382 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000383 0
384 };
385
Ben Skeggsa01ca782015-08-20 14:54:15 +1000386 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
387 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000388}
389
390/******************************************************************************
391 * DMA EVO channel
392 *****************************************************************************/
393
Ben Skeggsaccdea22016-11-04 17:20:36 +1000394struct nv50_dmac_ctxdma {
395 struct list_head head;
396 struct nvif_object object;
397};
398
Ben Skeggse225f442012-11-21 14:40:21 +1000399struct nv50_dmac {
400 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000401 dma_addr_t handle;
402 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100403
Ben Skeggs0ad72862014-08-10 04:10:22 +1000404 struct nvif_object sync;
405 struct nvif_object vram;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000406 struct list_head ctxdma;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000407
Daniel Vetter59ad1462012-12-02 14:49:44 +0100408 /* Protects against concurrent pushbuf access to this channel, lock is
409 * grabbed by evo_wait (if the pushbuf reservation is successful) and
410 * dropped again by evo_kick. */
411 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000412};
413
414static void
Ben Skeggsaccdea22016-11-04 17:20:36 +1000415nv50_dmac_ctxdma_del(struct nv50_dmac_ctxdma *ctxdma)
416{
417 nvif_object_fini(&ctxdma->object);
418 list_del(&ctxdma->head);
419 kfree(ctxdma);
420}
421
422static struct nv50_dmac_ctxdma *
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000423nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, struct nouveau_framebuffer *fb)
Ben Skeggsaccdea22016-11-04 17:20:36 +1000424{
425 struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
426 struct nv50_dmac_ctxdma *ctxdma;
Ben Skeggs7760a2e2017-11-01 03:56:19 +1000427 const u8 kind = fb->nvbo->kind;
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000428 const u32 handle = 0xfb000000 | kind;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000429 struct {
430 struct nv_dma_v0 base;
431 union {
432 struct nv50_dma_v0 nv50;
433 struct gf100_dma_v0 gf100;
434 struct gf119_dma_v0 gf119;
435 };
436 } args = {};
437 u32 argc = sizeof(args.base);
438 int ret;
439
440 list_for_each_entry(ctxdma, &dmac->ctxdma, head) {
441 if (ctxdma->object.handle == handle)
442 return ctxdma;
443 }
444
445 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
446 return ERR_PTR(-ENOMEM);
447 list_add(&ctxdma->head, &dmac->ctxdma);
448
449 args.base.target = NV_DMA_V0_TARGET_VRAM;
450 args.base.access = NV_DMA_V0_ACCESS_RDWR;
451 args.base.start = 0;
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000452 args.base.limit = drm->client.device.info.ram_user - 1;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000453
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000454 if (drm->client.device.info.chipset < 0x80) {
Ben Skeggsaccdea22016-11-04 17:20:36 +1000455 args.nv50.part = NV50_DMA_V0_PART_256;
456 argc += sizeof(args.nv50);
457 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000458 if (drm->client.device.info.chipset < 0xc0) {
Ben Skeggsaccdea22016-11-04 17:20:36 +1000459 args.nv50.part = NV50_DMA_V0_PART_256;
460 args.nv50.kind = kind;
461 argc += sizeof(args.nv50);
462 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000463 if (drm->client.device.info.chipset < 0xd0) {
Ben Skeggsaccdea22016-11-04 17:20:36 +1000464 args.gf100.kind = kind;
465 argc += sizeof(args.gf100);
466 } else {
467 args.gf119.page = GF119_DMA_V0_PAGE_LP;
468 args.gf119.kind = kind;
469 argc += sizeof(args.gf119);
470 }
471
472 ret = nvif_object_init(&dmac->base.user, handle, NV_DMA_IN_MEMORY,
473 &args, argc, &ctxdma->object);
474 if (ret) {
475 nv50_dmac_ctxdma_del(ctxdma);
476 return ERR_PTR(ret);
477 }
478
479 return ctxdma;
480}
481
482static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000483nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000484{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000485 struct nvif_device *device = dmac->base.device;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000486 struct nv50_dmac_ctxdma *ctxdma, *ctxtmp;
487
488 list_for_each_entry_safe(ctxdma, ctxtmp, &dmac->ctxdma, head) {
489 nv50_dmac_ctxdma_del(ctxdma);
490 }
Ben Skeggsa01ca782015-08-20 14:54:15 +1000491
Ben Skeggs0ad72862014-08-10 04:10:22 +1000492 nvif_object_fini(&dmac->vram);
493 nvif_object_fini(&dmac->sync);
494
495 nv50_chan_destroy(&dmac->base);
496
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000497 if (dmac->ptr) {
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000498 struct device *dev = nvxx_device(device)->dev;
499 dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000500 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000501}
502
503static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000504nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000505 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000506 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000507{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000508 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000509 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000510 int ret;
511
Daniel Vetter59ad1462012-12-02 14:49:44 +0100512 mutex_init(&dmac->lock);
Ben Skeggs9551efc2017-11-01 03:56:19 +1000513 INIT_LIST_HEAD(&dmac->ctxdma);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100514
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000515 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
516 &dmac->handle, GFP_KERNEL);
Ben Skeggs47057302012-11-16 13:58:48 +1000517 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000518 return -ENOMEM;
519
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000520 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
521 &(struct nv_dma_v0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +1000522 .target = NV_DMA_V0_TARGET_PCI_US,
523 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000524 .start = dmac->handle + 0x0000,
525 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000526 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000527 if (ret)
528 return ret;
529
Ben Skeggsbf81df92015-08-20 14:54:16 +1000530 args->pushbuf = nvif_handle(&pushbuf);
531
Ben Skeggsa01ca782015-08-20 14:54:15 +1000532 ret = nv50_chan_create(device, disp, oclass, head, data, size,
533 &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000534 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000535 if (ret)
536 return ret;
537
Ben Skeggsa01ca782015-08-20 14:54:15 +1000538 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000539 &(struct nv_dma_v0) {
540 .target = NV_DMA_V0_TARGET_VRAM,
541 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000542 .start = syncbuf + 0x0000,
543 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000544 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000545 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000546 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000547 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000548
Ben Skeggsa01ca782015-08-20 14:54:15 +1000549 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000550 &(struct nv_dma_v0) {
551 .target = NV_DMA_V0_TARGET_VRAM,
552 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000553 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000554 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000555 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000556 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000557 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000558 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000559
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000560 return ret;
561}
562
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000563/******************************************************************************
564 * Core
565 *****************************************************************************/
566
Ben Skeggse225f442012-11-21 14:40:21 +1000567struct nv50_mast {
568 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000569};
570
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000571static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000572nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
573 u64 syncbuf, struct nv50_mast *core)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000574{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000575 struct nv50_disp_core_channel_dma_v0 args = {
576 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000577 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000578 static const s32 oclass[] = {
Ben Skeggsed828662016-11-16 15:03:07 +1000579 GP102_DISP_CORE_CHANNEL_DMA,
Ben Skeggsf9d5cbb2016-07-09 10:41:01 +1000580 GP100_DISP_CORE_CHANNEL_DMA,
Ben Skeggsdb1eb522016-02-11 08:35:32 +1000581 GM200_DISP_CORE_CHANNEL_DMA,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000582 GM107_DISP_CORE_CHANNEL_DMA,
583 GK110_DISP_CORE_CHANNEL_DMA,
584 GK104_DISP_CORE_CHANNEL_DMA,
585 GF110_DISP_CORE_CHANNEL_DMA,
586 GT214_DISP_CORE_CHANNEL_DMA,
587 GT206_DISP_CORE_CHANNEL_DMA,
588 GT200_DISP_CORE_CHANNEL_DMA,
589 G82_DISP_CORE_CHANNEL_DMA,
590 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000591 0
592 };
593
Ben Skeggsa01ca782015-08-20 14:54:15 +1000594 return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
595 syncbuf, &core->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000596}
597
598/******************************************************************************
599 * Base
600 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000601
Ben Skeggse225f442012-11-21 14:40:21 +1000602struct nv50_sync {
603 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000604 u32 addr;
605 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000606};
607
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000608static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000609nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
610 int head, u64 syncbuf, struct nv50_sync *base)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000611{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000612 struct nv50_disp_base_channel_dma_v0 args = {
613 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000614 .head = head,
615 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000616 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000617 GK110_DISP_BASE_CHANNEL_DMA,
618 GK104_DISP_BASE_CHANNEL_DMA,
619 GF110_DISP_BASE_CHANNEL_DMA,
620 GT214_DISP_BASE_CHANNEL_DMA,
621 GT200_DISP_BASE_CHANNEL_DMA,
622 G82_DISP_BASE_CHANNEL_DMA,
623 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000624 0
625 };
626
Ben Skeggsa01ca782015-08-20 14:54:15 +1000627 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000628 syncbuf, &base->base);
629}
630
631/******************************************************************************
632 * Overlay
633 *****************************************************************************/
634
Ben Skeggse225f442012-11-21 14:40:21 +1000635struct nv50_ovly {
636 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000637};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000638
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000639static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000640nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
641 int head, u64 syncbuf, struct nv50_ovly *ovly)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000642{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000643 struct nv50_disp_overlay_channel_dma_v0 args = {
644 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000645 .head = head,
646 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000647 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000648 GK104_DISP_OVERLAY_CONTROL_DMA,
649 GF110_DISP_OVERLAY_CONTROL_DMA,
650 GT214_DISP_OVERLAY_CHANNEL_DMA,
651 GT200_DISP_OVERLAY_CHANNEL_DMA,
652 G82_DISP_OVERLAY_CHANNEL_DMA,
653 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000654 0
655 };
656
Ben Skeggsa01ca782015-08-20 14:54:15 +1000657 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000658 syncbuf, &ovly->base);
659}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000660
Ben Skeggse225f442012-11-21 14:40:21 +1000661struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000662 struct nouveau_crtc base;
Ben Skeggse225f442012-11-21 14:40:21 +1000663 struct nv50_ovly ovly;
664 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000665};
666
Ben Skeggse225f442012-11-21 14:40:21 +1000667#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
Ben Skeggse225f442012-11-21 14:40:21 +1000668#define nv50_ovly(c) (&nv50_head(c)->ovly)
669#define nv50_oimm(c) (&nv50_head(c)->oimm)
670#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000671#define nv50_vers(c) nv50_chan(c)->user.oclass
672
Ben Skeggse225f442012-11-21 14:40:21 +1000673struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000674 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000675 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000676
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000677 struct nouveau_bo *sync;
Ben Skeggs839ca902016-11-04 17:20:36 +1000678
679 struct mutex mutex;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000680};
681
Ben Skeggse225f442012-11-21 14:40:21 +1000682static struct nv50_disp *
683nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000684{
Ben Skeggs77145f12012-07-31 16:16:21 +1000685 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000686}
687
Ben Skeggse225f442012-11-21 14:40:21 +1000688#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000689
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000690/******************************************************************************
691 * EVO channel helpers
692 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000693static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000694evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000695{
Ben Skeggse225f442012-11-21 14:40:21 +1000696 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000697 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000698 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000699
Daniel Vetter59ad1462012-12-02 14:49:44 +0100700 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000701 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000702 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000703
Ben Skeggs0ad72862014-08-10 04:10:22 +1000704 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000705 if (nvif_msec(device, 2000,
706 if (!nvif_rd32(&dmac->base.user, 0x0004))
707 break;
708 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100709 mutex_unlock(&dmac->lock);
Joe Perches8dfe1622017-02-28 04:55:54 -0800710 pr_err("nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000711 return NULL;
712 }
713
714 put = 0;
715 }
716
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000717 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000718}
719
720static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000721evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000722{
Ben Skeggse225f442012-11-21 14:40:21 +1000723 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000724 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100725 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000726}
727
Joe Perches8dfe1622017-02-28 04:55:54 -0800728#define evo_mthd(p, m, s) do { \
729 const u32 _m = (m), _s = (s); \
730 if (drm_debug & DRM_UT_KMS) \
731 pr_err("%04x %d %s\n", _m, _s, __func__); \
732 *((p)++) = ((_s << 18) | _m); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000733} while(0)
Ben Skeggs7f55a072016-11-04 17:20:36 +1000734
Joe Perches8dfe1622017-02-28 04:55:54 -0800735#define evo_data(p, d) do { \
736 const u32 _d = (d); \
737 if (drm_debug & DRM_UT_KMS) \
738 pr_err("\t%08x\n", _d); \
739 *((p)++) = _d; \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000740} while(0)
Ben Skeggs51beb422011-07-05 10:33:08 +1000741
Ben Skeggs3376ee32011-11-12 14:28:12 +1000742/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +1000743 * Plane
744 *****************************************************************************/
745#define nv50_wndw(p) container_of((p), struct nv50_wndw, plane)
746
747struct nv50_wndw {
748 const struct nv50_wndw_func *func;
749 struct nv50_dmac *dmac;
750
751 struct drm_plane plane;
752
753 struct nvif_notify notify;
754 u16 ntfy;
755 u16 sema;
756 u32 data;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000757};
758
759struct nv50_wndw_func {
760 void *(*dtor)(struct nv50_wndw *);
761 int (*acquire)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
762 struct nv50_head_atom *asyh);
763 void (*release)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
764 struct nv50_head_atom *asyh);
765 void (*prepare)(struct nv50_wndw *, struct nv50_head_atom *asyh,
766 struct nv50_wndw_atom *asyw);
767
768 void (*sema_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
769 void (*sema_clr)(struct nv50_wndw *);
770 void (*ntfy_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
771 void (*ntfy_clr)(struct nv50_wndw *);
772 int (*ntfy_wait_begun)(struct nv50_wndw *, struct nv50_wndw_atom *);
773 void (*image_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
774 void (*image_clr)(struct nv50_wndw *);
775 void (*lut)(struct nv50_wndw *, struct nv50_wndw_atom *);
776 void (*point)(struct nv50_wndw *, struct nv50_wndw_atom *);
777
778 u32 (*update)(struct nv50_wndw *, u32 interlock);
779};
780
781static int
782nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
783{
784 if (asyw->set.ntfy)
785 return wndw->func->ntfy_wait_begun(wndw, asyw);
786 return 0;
787}
788
789static u32
790nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 interlock, bool flush,
791 struct nv50_wndw_atom *asyw)
792{
793 if (asyw->clr.sema && (!asyw->set.sema || flush))
794 wndw->func->sema_clr(wndw);
795 if (asyw->clr.ntfy && (!asyw->set.ntfy || flush))
796 wndw->func->ntfy_clr(wndw);
797 if (asyw->clr.image && (!asyw->set.image || flush))
798 wndw->func->image_clr(wndw);
799
800 return flush ? wndw->func->update(wndw, interlock) : 0;
801}
802
803static u32
804nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 interlock,
805 struct nv50_wndw_atom *asyw)
806{
807 if (interlock) {
808 asyw->image.mode = 0;
809 asyw->image.interval = 1;
810 }
811
812 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
813 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
814 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
815 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
816 if (asyw->set.point) wndw->func->point (wndw, asyw);
817
818 return wndw->func->update(wndw, interlock);
819}
820
821static void
822nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
823 struct nv50_wndw_atom *asyw,
824 struct nv50_head_atom *asyh)
825{
826 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
827 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
828 wndw->func->release(wndw, asyw, asyh);
829 asyw->ntfy.handle = 0;
830 asyw->sema.handle = 0;
831}
832
833static int
834nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
835 struct nv50_wndw_atom *asyw,
Ben Skeggsf42c5702017-05-01 16:59:29 +1000836 struct nv50_head_atom *asyh)
Ben Skeggs973f10c2016-11-04 17:20:36 +1000837{
838 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
839 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
840 int ret;
841
842 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
843 asyw->clip.x1 = 0;
844 asyw->clip.y1 = 0;
845 asyw->clip.x2 = asyh->state.mode.hdisplay;
846 asyw->clip.y2 = asyh->state.mode.vdisplay;
847
848 asyw->image.w = fb->base.width;
849 asyw->image.h = fb->base.height;
Ben Skeggs7760a2e2017-11-01 03:56:19 +1000850 asyw->image.kind = fb->nvbo->kind;
Andrey Grodzovsky612fb5d2017-02-02 16:56:30 -0500851
Ben Skeggsf42c5702017-05-01 16:59:29 +1000852 if (asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC)
853 asyw->interval = 0;
854 else
855 asyw->interval = 1;
Andrey Grodzovsky612fb5d2017-02-02 16:56:30 -0500856
Ben Skeggs973f10c2016-11-04 17:20:36 +1000857 if (asyw->image.kind) {
858 asyw->image.layout = 0;
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000859 if (drm->client.device.info.chipset >= 0xc0)
Ben Skeggs7760a2e2017-11-01 03:56:19 +1000860 asyw->image.block = fb->nvbo->mode >> 4;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000861 else
Ben Skeggs7760a2e2017-11-01 03:56:19 +1000862 asyw->image.block = fb->nvbo->mode;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000863 asyw->image.pitch = (fb->base.pitches[0] / 4) << 4;
864 } else {
865 asyw->image.layout = 1;
866 asyw->image.block = 0;
867 asyw->image.pitch = fb->base.pitches[0];
868 }
869
870 ret = wndw->func->acquire(wndw, asyw, asyh);
871 if (ret)
872 return ret;
873
874 if (asyw->set.image) {
875 if (!(asyw->image.mode = asyw->interval ? 0 : 1))
876 asyw->image.interval = asyw->interval;
877 else
878 asyw->image.interval = 0;
879 }
880
881 return 0;
882}
883
884static int
885nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
886{
887 struct nouveau_drm *drm = nouveau_drm(plane->dev);
888 struct nv50_wndw *wndw = nv50_wndw(plane);
Ben Skeggs839ca902016-11-04 17:20:36 +1000889 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
890 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000891 struct nv50_head_atom *harm = NULL, *asyh = NULL;
892 bool varm = false, asyv = false, asym = false;
893 int ret;
894
Ben Skeggs973f10c2016-11-04 17:20:36 +1000895 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
896 if (asyw->state.crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +1000897 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000898 if (IS_ERR(asyh))
899 return PTR_ERR(asyh);
900 asym = drm_atomic_crtc_needs_modeset(&asyh->state);
901 asyv = asyh->state.active;
902 }
903
904 if (armw->state.crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +1000905 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000906 if (IS_ERR(harm))
907 return PTR_ERR(harm);
Ben Skeggs839ca902016-11-04 17:20:36 +1000908 varm = harm->state.crtc->state->active;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000909 }
910
911 if (asyv) {
912 asyw->point.x = asyw->state.crtc_x;
913 asyw->point.y = asyw->state.crtc_y;
914 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
915 asyw->set.point = true;
916
Ben Skeggs36601c22017-05-01 16:52:03 +1000917 ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh);
918 if (ret)
919 return ret;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000920 } else
921 if (varm) {
922 nv50_wndw_atomic_check_release(wndw, asyw, harm);
923 } else {
924 return 0;
925 }
926
927 if (!asyv || asym) {
928 asyw->clr.ntfy = armw->ntfy.handle != 0;
929 asyw->clr.sema = armw->sema.handle != 0;
930 if (wndw->func->image_clr)
931 asyw->clr.image = armw->image.handle != 0;
932 asyw->set.lut = wndw->func->lut && asyv;
933 }
934
Ben Skeggs973f10c2016-11-04 17:20:36 +1000935 return 0;
936}
937
938static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000939nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
940{
941 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
942 struct nouveau_drm *drm = nouveau_drm(plane->dev);
943
944 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
945 if (!old_state->fb)
946 return;
947
948 nouveau_bo_unpin(fb->nvbo);
949}
950
951static int
952nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
953{
954 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
955 struct nouveau_drm *drm = nouveau_drm(plane->dev);
956 struct nv50_wndw *wndw = nv50_wndw(plane);
957 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
958 struct nv50_head_atom *asyh;
959 struct nv50_dmac_ctxdma *ctxdma;
Ben Skeggs839ca902016-11-04 17:20:36 +1000960 int ret;
961
962 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
963 if (!asyw->state.fb)
964 return 0;
Ben Skeggs839ca902016-11-04 17:20:36 +1000965
966 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
967 if (ret)
968 return ret;
969
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000970 ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, fb);
Ben Skeggs839ca902016-11-04 17:20:36 +1000971 if (IS_ERR(ctxdma)) {
972 nouveau_bo_unpin(fb->nvbo);
973 return PTR_ERR(ctxdma);
974 }
975
976 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
977 asyw->image.handle = ctxdma->object.handle;
978 asyw->image.offset = fb->nvbo->bo.offset;
979
980 if (wndw->func->prepare) {
981 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
982 if (IS_ERR(asyh))
983 return PTR_ERR(asyh);
984
985 wndw->func->prepare(wndw, asyh, asyw);
986 }
987
988 return 0;
989}
990
991static const struct drm_plane_helper_funcs
992nv50_wndw_helper = {
993 .prepare_fb = nv50_wndw_prepare_fb,
994 .cleanup_fb = nv50_wndw_cleanup_fb,
995 .atomic_check = nv50_wndw_atomic_check,
996};
997
998static void
Ben Skeggs973f10c2016-11-04 17:20:36 +1000999nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
1000 struct drm_plane_state *state)
1001{
1002 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
1003 __drm_atomic_helper_plane_destroy_state(&asyw->state);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001004 kfree(asyw);
1005}
1006
1007static struct drm_plane_state *
1008nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
1009{
1010 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
1011 struct nv50_wndw_atom *asyw;
1012 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
1013 return NULL;
1014 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001015 asyw->interval = 1;
1016 asyw->sema = armw->sema;
1017 asyw->ntfy = armw->ntfy;
1018 asyw->image = armw->image;
1019 asyw->point = armw->point;
1020 asyw->lut = armw->lut;
1021 asyw->clr.mask = 0;
1022 asyw->set.mask = 0;
1023 return &asyw->state;
1024}
1025
1026static void
1027nv50_wndw_reset(struct drm_plane *plane)
1028{
1029 struct nv50_wndw_atom *asyw;
1030
1031 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
1032 return;
1033
1034 if (plane->state)
1035 plane->funcs->atomic_destroy_state(plane, plane->state);
1036 plane->state = &asyw->state;
1037 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -04001038 plane->state->rotation = DRM_MODE_ROTATE_0;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001039}
1040
1041static void
1042nv50_wndw_destroy(struct drm_plane *plane)
1043{
1044 struct nv50_wndw *wndw = nv50_wndw(plane);
1045 void *data;
1046 nvif_notify_fini(&wndw->notify);
1047 data = wndw->func->dtor(wndw);
1048 drm_plane_cleanup(&wndw->plane);
1049 kfree(data);
1050}
1051
1052static const struct drm_plane_funcs
1053nv50_wndw = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001054 .update_plane = drm_atomic_helper_update_plane,
1055 .disable_plane = drm_atomic_helper_disable_plane,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001056 .destroy = nv50_wndw_destroy,
1057 .reset = nv50_wndw_reset,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001058 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
1059 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
1060};
1061
1062static void
1063nv50_wndw_fini(struct nv50_wndw *wndw)
1064{
1065 nvif_notify_put(&wndw->notify);
1066}
1067
1068static void
1069nv50_wndw_init(struct nv50_wndw *wndw)
1070{
1071 nvif_notify_get(&wndw->notify);
1072}
1073
1074static int
1075nv50_wndw_ctor(const struct nv50_wndw_func *func, struct drm_device *dev,
1076 enum drm_plane_type type, const char *name, int index,
1077 struct nv50_dmac *dmac, const u32 *format, int nformat,
1078 struct nv50_wndw *wndw)
1079{
1080 int ret;
1081
1082 wndw->func = func;
1083 wndw->dmac = dmac;
1084
Ben Widawskye6fc3b62017-07-23 20:46:38 -07001085 ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw,
1086 format, nformat, NULL,
1087 type, "%s-%d", name, index);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001088 if (ret)
1089 return ret;
1090
Ben Skeggs839ca902016-11-04 17:20:36 +10001091 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001092 return 0;
1093}
1094
1095/******************************************************************************
Ben Skeggs22e927d2016-11-04 17:20:36 +10001096 * Cursor plane
1097 *****************************************************************************/
1098#define nv50_curs(p) container_of((p), struct nv50_curs, wndw)
1099
1100struct nv50_curs {
1101 struct nv50_wndw wndw;
1102 struct nvif_object chan;
1103};
1104
1105static u32
1106nv50_curs_update(struct nv50_wndw *wndw, u32 interlock)
1107{
1108 struct nv50_curs *curs = nv50_curs(wndw);
1109 nvif_wr32(&curs->chan, 0x0080, 0x00000000);
1110 return 0;
1111}
1112
1113static void
1114nv50_curs_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1115{
1116 struct nv50_curs *curs = nv50_curs(wndw);
1117 nvif_wr32(&curs->chan, 0x0084, (asyw->point.y << 16) | asyw->point.x);
1118}
1119
1120static void
1121nv50_curs_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
1122 struct nv50_wndw_atom *asyw)
1123{
Ben Skeggse6db9572017-05-01 16:53:40 +10001124 u32 handle = nv50_disp(wndw->plane.dev)->mast.base.vram.handle;
1125 u32 offset = asyw->image.offset;
1126 if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
1127 asyh->curs.handle = handle;
1128 asyh->curs.offset = offset;
1129 asyh->set.curs = asyh->curs.visible;
1130 }
Ben Skeggs22e927d2016-11-04 17:20:36 +10001131}
1132
1133static void
1134nv50_curs_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1135 struct nv50_head_atom *asyh)
1136{
1137 asyh->curs.visible = false;
1138}
1139
1140static int
1141nv50_curs_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1142 struct nv50_head_atom *asyh)
1143{
1144 int ret;
1145
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02001146 ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
1147 &asyw->clip,
1148 DRM_PLANE_HELPER_NO_SCALING,
1149 DRM_PLANE_HELPER_NO_SCALING,
1150 true, true);
Ben Skeggs22e927d2016-11-04 17:20:36 +10001151 asyh->curs.visible = asyw->state.visible;
1152 if (ret || !asyh->curs.visible)
1153 return ret;
1154
1155 switch (asyw->state.fb->width) {
1156 case 32: asyh->curs.layout = 0; break;
1157 case 64: asyh->curs.layout = 1; break;
1158 default:
1159 return -EINVAL;
1160 }
1161
1162 if (asyw->state.fb->width != asyw->state.fb->height)
1163 return -EINVAL;
1164
Ville Syrjälä438b74a2016-12-14 23:32:55 +02001165 switch (asyw->state.fb->format->format) {
Ben Skeggs22e927d2016-11-04 17:20:36 +10001166 case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
1167 default:
1168 WARN_ON(1);
1169 return -EINVAL;
1170 }
1171
1172 return 0;
1173}
1174
1175static void *
1176nv50_curs_dtor(struct nv50_wndw *wndw)
1177{
1178 struct nv50_curs *curs = nv50_curs(wndw);
1179 nvif_object_fini(&curs->chan);
1180 return curs;
1181}
1182
1183static const u32
1184nv50_curs_format[] = {
1185 DRM_FORMAT_ARGB8888,
1186};
1187
1188static const struct nv50_wndw_func
1189nv50_curs = {
1190 .dtor = nv50_curs_dtor,
1191 .acquire = nv50_curs_acquire,
1192 .release = nv50_curs_release,
1193 .prepare = nv50_curs_prepare,
1194 .point = nv50_curs_point,
1195 .update = nv50_curs_update,
1196};
1197
1198static int
1199nv50_curs_new(struct nouveau_drm *drm, struct nv50_head *head,
1200 struct nv50_curs **pcurs)
1201{
1202 static const struct nvif_mclass curses[] = {
1203 { GK104_DISP_CURSOR, 0 },
1204 { GF110_DISP_CURSOR, 0 },
1205 { GT214_DISP_CURSOR, 0 },
1206 { G82_DISP_CURSOR, 0 },
1207 { NV50_DISP_CURSOR, 0 },
1208 {}
1209 };
1210 struct nv50_disp_cursor_v0 args = {
1211 .head = head->base.index,
1212 };
1213 struct nv50_disp *disp = nv50_disp(drm->dev);
1214 struct nv50_curs *curs;
1215 int cid, ret;
1216
1217 cid = nvif_mclass(disp->disp, curses);
1218 if (cid < 0) {
1219 NV_ERROR(drm, "No supported cursor immediate class\n");
1220 return cid;
1221 }
1222
1223 if (!(curs = *pcurs = kzalloc(sizeof(*curs), GFP_KERNEL)))
1224 return -ENOMEM;
1225
1226 ret = nv50_wndw_ctor(&nv50_curs, drm->dev, DRM_PLANE_TYPE_CURSOR,
1227 "curs", head->base.index, &disp->mast.base,
1228 nv50_curs_format, ARRAY_SIZE(nv50_curs_format),
1229 &curs->wndw);
1230 if (ret) {
1231 kfree(curs);
1232 return ret;
1233 }
1234
1235 ret = nvif_object_init(disp->disp, 0, curses[cid].oclass, &args,
1236 sizeof(args), &curs->chan);
1237 if (ret) {
1238 NV_ERROR(drm, "curs%04x allocation failed: %d\n",
1239 curses[cid].oclass, ret);
1240 return ret;
1241 }
1242
1243 return 0;
1244}
1245
1246/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +10001247 * Primary plane
1248 *****************************************************************************/
1249#define nv50_base(p) container_of((p), struct nv50_base, wndw)
1250
1251struct nv50_base {
1252 struct nv50_wndw wndw;
1253 struct nv50_sync chan;
1254 int id;
1255};
1256
1257static int
1258nv50_base_notify(struct nvif_notify *notify)
1259{
1260 return NVIF_NOTIFY_KEEP;
1261}
1262
1263static void
1264nv50_base_lut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1265{
1266 struct nv50_base *base = nv50_base(wndw);
1267 u32 *push;
1268 if ((push = evo_wait(&base->chan, 2))) {
1269 evo_mthd(push, 0x00e0, 1);
1270 evo_data(push, asyw->lut.enable << 30);
1271 evo_kick(push, &base->chan);
1272 }
1273}
1274
1275static void
1276nv50_base_image_clr(struct nv50_wndw *wndw)
1277{
1278 struct nv50_base *base = nv50_base(wndw);
1279 u32 *push;
1280 if ((push = evo_wait(&base->chan, 4))) {
1281 evo_mthd(push, 0x0084, 1);
1282 evo_data(push, 0x00000000);
1283 evo_mthd(push, 0x00c0, 1);
1284 evo_data(push, 0x00000000);
1285 evo_kick(push, &base->chan);
1286 }
1287}
1288
1289static void
1290nv50_base_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1291{
1292 struct nv50_base *base = nv50_base(wndw);
1293 const s32 oclass = base->chan.base.base.user.oclass;
1294 u32 *push;
1295 if ((push = evo_wait(&base->chan, 10))) {
1296 evo_mthd(push, 0x0084, 1);
1297 evo_data(push, (asyw->image.mode << 8) |
1298 (asyw->image.interval << 4));
1299 evo_mthd(push, 0x00c0, 1);
1300 evo_data(push, asyw->image.handle);
1301 if (oclass < G82_DISP_BASE_CHANNEL_DMA) {
1302 evo_mthd(push, 0x0800, 5);
1303 evo_data(push, asyw->image.offset >> 8);
1304 evo_data(push, 0x00000000);
1305 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1306 evo_data(push, (asyw->image.layout << 20) |
1307 asyw->image.pitch |
1308 asyw->image.block);
1309 evo_data(push, (asyw->image.kind << 16) |
1310 (asyw->image.format << 8));
1311 } else
1312 if (oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1313 evo_mthd(push, 0x0800, 5);
1314 evo_data(push, asyw->image.offset >> 8);
1315 evo_data(push, 0x00000000);
1316 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1317 evo_data(push, (asyw->image.layout << 20) |
1318 asyw->image.pitch |
1319 asyw->image.block);
1320 evo_data(push, asyw->image.format << 8);
1321 } else {
1322 evo_mthd(push, 0x0400, 5);
1323 evo_data(push, asyw->image.offset >> 8);
1324 evo_data(push, 0x00000000);
1325 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1326 evo_data(push, (asyw->image.layout << 24) |
1327 asyw->image.pitch |
1328 asyw->image.block);
1329 evo_data(push, asyw->image.format << 8);
1330 }
1331 evo_kick(push, &base->chan);
1332 }
1333}
1334
1335static void
1336nv50_base_ntfy_clr(struct nv50_wndw *wndw)
1337{
1338 struct nv50_base *base = nv50_base(wndw);
1339 u32 *push;
1340 if ((push = evo_wait(&base->chan, 2))) {
1341 evo_mthd(push, 0x00a4, 1);
1342 evo_data(push, 0x00000000);
1343 evo_kick(push, &base->chan);
1344 }
1345}
1346
1347static void
1348nv50_base_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1349{
1350 struct nv50_base *base = nv50_base(wndw);
1351 u32 *push;
1352 if ((push = evo_wait(&base->chan, 3))) {
1353 evo_mthd(push, 0x00a0, 2);
1354 evo_data(push, (asyw->ntfy.awaken << 30) | asyw->ntfy.offset);
1355 evo_data(push, asyw->ntfy.handle);
1356 evo_kick(push, &base->chan);
1357 }
1358}
1359
1360static void
1361nv50_base_sema_clr(struct nv50_wndw *wndw)
1362{
1363 struct nv50_base *base = nv50_base(wndw);
1364 u32 *push;
1365 if ((push = evo_wait(&base->chan, 2))) {
1366 evo_mthd(push, 0x0094, 1);
1367 evo_data(push, 0x00000000);
1368 evo_kick(push, &base->chan);
1369 }
1370}
1371
1372static void
1373nv50_base_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1374{
1375 struct nv50_base *base = nv50_base(wndw);
1376 u32 *push;
1377 if ((push = evo_wait(&base->chan, 5))) {
1378 evo_mthd(push, 0x0088, 4);
1379 evo_data(push, asyw->sema.offset);
1380 evo_data(push, asyw->sema.acquire);
1381 evo_data(push, asyw->sema.release);
1382 evo_data(push, asyw->sema.handle);
1383 evo_kick(push, &base->chan);
1384 }
1385}
1386
1387static u32
1388nv50_base_update(struct nv50_wndw *wndw, u32 interlock)
1389{
1390 struct nv50_base *base = nv50_base(wndw);
1391 u32 *push;
1392
1393 if (!(push = evo_wait(&base->chan, 2)))
1394 return 0;
1395 evo_mthd(push, 0x0080, 1);
1396 evo_data(push, interlock);
1397 evo_kick(push, &base->chan);
1398
1399 if (base->chan.base.base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA)
1400 return interlock ? 2 << (base->id * 8) : 0;
1401 return interlock ? 2 << (base->id * 4) : 0;
1402}
1403
1404static int
1405nv50_base_ntfy_wait_begun(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1406{
1407 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
1408 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001409 if (nvif_msec(&drm->client.device, 2000ULL,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001410 u32 data = nouveau_bo_rd32(disp->sync, asyw->ntfy.offset / 4);
1411 if ((data & 0xc0000000) == 0x40000000)
1412 break;
1413 usleep_range(1, 2);
1414 ) < 0)
1415 return -ETIMEDOUT;
1416 return 0;
1417}
1418
1419static void
1420nv50_base_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1421 struct nv50_head_atom *asyh)
1422{
1423 asyh->base.cpp = 0;
1424}
1425
1426static int
1427nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1428 struct nv50_head_atom *asyh)
1429{
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001430 const struct drm_framebuffer *fb = asyw->state.fb;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001431 int ret;
1432
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001433 if (!fb->format->depth)
Ben Skeggs973f10c2016-11-04 17:20:36 +10001434 return -EINVAL;
1435
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02001436 ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
1437 &asyw->clip,
1438 DRM_PLANE_HELPER_NO_SCALING,
1439 DRM_PLANE_HELPER_NO_SCALING,
1440 false, true);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001441 if (ret)
1442 return ret;
1443
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001444 asyh->base.depth = fb->format->depth;
1445 asyh->base.cpp = fb->format->cpp[0];
Ben Skeggs973f10c2016-11-04 17:20:36 +10001446 asyh->base.x = asyw->state.src.x1 >> 16;
1447 asyh->base.y = asyw->state.src.y1 >> 16;
1448 asyh->base.w = asyw->state.fb->width;
1449 asyh->base.h = asyw->state.fb->height;
1450
Ville Syrjälä438b74a2016-12-14 23:32:55 +02001451 switch (fb->format->format) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001452 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
1453 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
1454 case DRM_FORMAT_XRGB1555 :
1455 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
1456 case DRM_FORMAT_XRGB8888 :
1457 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
1458 case DRM_FORMAT_XBGR2101010:
1459 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
1460 case DRM_FORMAT_XBGR8888 :
1461 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
1462 default:
1463 WARN_ON(1);
1464 return -EINVAL;
1465 }
1466
1467 asyw->lut.enable = 1;
1468 asyw->set.image = true;
1469 return 0;
1470}
1471
1472static void *
1473nv50_base_dtor(struct nv50_wndw *wndw)
1474{
1475 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1476 struct nv50_base *base = nv50_base(wndw);
1477 nv50_dmac_destroy(&base->chan.base, disp->disp);
1478 return base;
1479}
1480
1481static const u32
1482nv50_base_format[] = {
1483 DRM_FORMAT_C8,
1484 DRM_FORMAT_RGB565,
1485 DRM_FORMAT_XRGB1555,
1486 DRM_FORMAT_ARGB1555,
1487 DRM_FORMAT_XRGB8888,
1488 DRM_FORMAT_ARGB8888,
1489 DRM_FORMAT_XBGR2101010,
1490 DRM_FORMAT_ABGR2101010,
1491 DRM_FORMAT_XBGR8888,
1492 DRM_FORMAT_ABGR8888,
1493};
1494
1495static const struct nv50_wndw_func
1496nv50_base = {
1497 .dtor = nv50_base_dtor,
1498 .acquire = nv50_base_acquire,
1499 .release = nv50_base_release,
1500 .sema_set = nv50_base_sema_set,
1501 .sema_clr = nv50_base_sema_clr,
1502 .ntfy_set = nv50_base_ntfy_set,
1503 .ntfy_clr = nv50_base_ntfy_clr,
1504 .ntfy_wait_begun = nv50_base_ntfy_wait_begun,
1505 .image_set = nv50_base_image_set,
1506 .image_clr = nv50_base_image_clr,
1507 .lut = nv50_base_lut,
1508 .update = nv50_base_update,
1509};
1510
1511static int
1512nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
1513 struct nv50_base **pbase)
1514{
1515 struct nv50_disp *disp = nv50_disp(drm->dev);
1516 struct nv50_base *base;
1517 int ret;
1518
1519 if (!(base = *pbase = kzalloc(sizeof(*base), GFP_KERNEL)))
1520 return -ENOMEM;
1521 base->id = head->base.index;
1522 base->wndw.ntfy = EVO_FLIP_NTFY0(base->id);
1523 base->wndw.sema = EVO_FLIP_SEM0(base->id);
1524 base->wndw.data = 0x00000000;
1525
1526 ret = nv50_wndw_ctor(&nv50_base, drm->dev, DRM_PLANE_TYPE_PRIMARY,
1527 "base", base->id, &base->chan.base,
1528 nv50_base_format, ARRAY_SIZE(nv50_base_format),
1529 &base->wndw);
1530 if (ret) {
1531 kfree(base);
1532 return ret;
1533 }
1534
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001535 ret = nv50_base_create(&drm->client.device, disp->disp, base->id,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001536 disp->sync->bo.offset, &base->chan);
1537 if (ret)
1538 return ret;
1539
1540 return nvif_notify_init(&base->chan.base.base.user, nv50_base_notify,
1541 false,
1542 NV50_DISP_BASE_CHANNEL_DMA_V0_NTFY_UEVENT,
1543 &(struct nvif_notify_uevent_req) {},
1544 sizeof(struct nvif_notify_uevent_req),
1545 sizeof(struct nvif_notify_uevent_rep),
1546 &base->wndw.notify);
1547}
1548
1549/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001550 * Head
1551 *****************************************************************************/
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001552static void
Ben Skeggs7e08d672016-11-04 17:20:36 +10001553nv50_head_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
1554{
1555 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1556 u32 *push;
1557 if ((push = evo_wait(core, 2))) {
1558 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1559 evo_mthd(push, 0x08a8 + (head->base.index * 0x400), 1);
1560 else
1561 evo_mthd(push, 0x0498 + (head->base.index * 0x300), 1);
1562 evo_data(push, (asyh->procamp.sat.sin << 20) |
1563 (asyh->procamp.sat.cos << 8));
1564 evo_kick(push, core);
1565 }
1566}
1567
1568static void
Ben Skeggs7e918332016-11-04 17:20:36 +10001569nv50_head_dither(struct nv50_head *head, struct nv50_head_atom *asyh)
1570{
1571 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1572 u32 *push;
1573 if ((push = evo_wait(core, 2))) {
1574 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1575 evo_mthd(push, 0x08a0 + (head->base.index * 0x0400), 1);
1576 else
1577 if (core->base.user.oclass < GK104_DISP_CORE_CHANNEL_DMA)
1578 evo_mthd(push, 0x0490 + (head->base.index * 0x0300), 1);
1579 else
1580 evo_mthd(push, 0x04a0 + (head->base.index * 0x0300), 1);
1581 evo_data(push, (asyh->dither.mode << 3) |
1582 (asyh->dither.bits << 1) |
1583 asyh->dither.enable);
1584 evo_kick(push, core);
1585 }
1586}
1587
1588static void
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001589nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
1590{
1591 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1592 u32 bounds = 0;
1593 u32 *push;
1594
1595 if (asyh->base.cpp) {
1596 switch (asyh->base.cpp) {
1597 case 8: bounds |= 0x00000500; break;
1598 case 4: bounds |= 0x00000300; break;
1599 case 2: bounds |= 0x00000100; break;
1600 default:
1601 WARN_ON(1);
1602 break;
1603 }
1604 bounds |= 0x00000001;
1605 }
1606
1607 if ((push = evo_wait(core, 2))) {
1608 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1609 evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
1610 else
1611 evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
1612 evo_data(push, bounds);
1613 evo_kick(push, core);
1614 }
1615}
1616
1617static void
1618nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
1619{
1620 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1621 u32 bounds = 0;
1622 u32 *push;
1623
1624 if (asyh->base.cpp) {
1625 switch (asyh->base.cpp) {
1626 case 8: bounds |= 0x00000500; break;
1627 case 4: bounds |= 0x00000300; break;
1628 case 2: bounds |= 0x00000100; break;
1629 case 1: bounds |= 0x00000000; break;
1630 default:
1631 WARN_ON(1);
1632 break;
1633 }
1634 bounds |= 0x00000001;
1635 }
1636
1637 if ((push = evo_wait(core, 2))) {
1638 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1639 evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
1640 else
1641 evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
1642 evo_data(push, bounds);
1643 evo_kick(push, core);
1644 }
1645}
1646
1647static void
Ben Skeggsea8ee392016-11-04 17:20:36 +10001648nv50_head_curs_clr(struct nv50_head *head)
1649{
1650 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1651 u32 *push;
1652 if ((push = evo_wait(core, 4))) {
1653 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1654 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1655 evo_data(push, 0x05000000);
1656 } else
1657 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1658 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1659 evo_data(push, 0x05000000);
1660 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1661 evo_data(push, 0x00000000);
1662 } else {
1663 evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
1664 evo_data(push, 0x05000000);
1665 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1666 evo_data(push, 0x00000000);
1667 }
1668 evo_kick(push, core);
1669 }
1670}
1671
1672static void
1673nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1674{
1675 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1676 u32 *push;
1677 if ((push = evo_wait(core, 5))) {
1678 if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
1679 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1680 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1681 (asyh->curs.format << 24));
1682 evo_data(push, asyh->curs.offset >> 8);
1683 } else
1684 if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1685 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1686 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1687 (asyh->curs.format << 24));
1688 evo_data(push, asyh->curs.offset >> 8);
1689 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1690 evo_data(push, asyh->curs.handle);
1691 } else {
1692 evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
1693 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1694 (asyh->curs.format << 24));
1695 evo_data(push, asyh->curs.offset >> 8);
1696 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1697 evo_data(push, asyh->curs.handle);
1698 }
1699 evo_kick(push, core);
1700 }
1701}
1702
1703static void
Ben Skeggsad633612016-11-04 17:20:36 +10001704nv50_head_core_clr(struct nv50_head *head)
1705{
1706 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1707 u32 *push;
1708 if ((push = evo_wait(core, 2))) {
1709 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1710 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
1711 else
1712 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
1713 evo_data(push, 0x00000000);
1714 evo_kick(push, core);
1715 }
1716}
1717
1718static void
1719nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1720{
1721 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1722 u32 *push;
1723 if ((push = evo_wait(core, 9))) {
1724 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1725 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1726 evo_data(push, asyh->core.offset >> 8);
1727 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1728 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1729 evo_data(push, asyh->core.layout << 20 |
1730 (asyh->core.pitch >> 8) << 8 |
1731 asyh->core.block);
1732 evo_data(push, asyh->core.kind << 16 |
1733 asyh->core.format << 8);
1734 evo_data(push, asyh->core.handle);
1735 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1736 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
Ben Skeggs19d53d02016-12-13 11:18:46 +10001737 /* EVO will complain with INVALID_STATE if we have an
1738 * active cursor and (re)specify HeadSetContextDmaIso
1739 * without also updating HeadSetOffsetCursor.
1740 */
1741 asyh->set.curs = asyh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10001742 } else
1743 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1744 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1745 evo_data(push, asyh->core.offset >> 8);
1746 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1747 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1748 evo_data(push, asyh->core.layout << 20 |
1749 (asyh->core.pitch >> 8) << 8 |
1750 asyh->core.block);
1751 evo_data(push, asyh->core.format << 8);
1752 evo_data(push, asyh->core.handle);
1753 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1754 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1755 } else {
1756 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
1757 evo_data(push, asyh->core.offset >> 8);
1758 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
1759 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1760 evo_data(push, asyh->core.layout << 24 |
1761 (asyh->core.pitch >> 8) << 8 |
1762 asyh->core.block);
1763 evo_data(push, asyh->core.format << 8);
1764 evo_data(push, asyh->core.handle);
1765 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
1766 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1767 }
1768 evo_kick(push, core);
1769 }
1770}
1771
1772static void
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001773nv50_head_lut_clr(struct nv50_head *head)
1774{
1775 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1776 u32 *push;
1777 if ((push = evo_wait(core, 4))) {
1778 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1779 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1780 evo_data(push, 0x40000000);
1781 } else
1782 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1783 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1784 evo_data(push, 0x40000000);
1785 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1786 evo_data(push, 0x00000000);
1787 } else {
1788 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
1789 evo_data(push, 0x03000000);
1790 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1791 evo_data(push, 0x00000000);
1792 }
1793 evo_kick(push, core);
1794 }
1795}
1796
1797static void
1798nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1799{
1800 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1801 u32 *push;
1802 if ((push = evo_wait(core, 7))) {
1803 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1804 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1805 evo_data(push, 0xc0000000);
1806 evo_data(push, asyh->lut.offset >> 8);
1807 } else
1808 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1809 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1810 evo_data(push, 0xc0000000);
1811 evo_data(push, asyh->lut.offset >> 8);
1812 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1813 evo_data(push, asyh->lut.handle);
1814 } else {
1815 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
1816 evo_data(push, 0x83000000);
1817 evo_data(push, asyh->lut.offset >> 8);
1818 evo_data(push, 0x00000000);
1819 evo_data(push, 0x00000000);
1820 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1821 evo_data(push, asyh->lut.handle);
1822 }
1823 evo_kick(push, core);
1824 }
1825}
1826
1827static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001828nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1829{
1830 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1831 struct nv50_head_mode *m = &asyh->mode;
1832 u32 *push;
1833 if ((push = evo_wait(core, 14))) {
1834 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1835 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
1836 evo_data(push, 0x00800000 | m->clock);
1837 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001838 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 7);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001839 evo_data(push, 0x00000000);
1840 evo_data(push, (m->v.active << 16) | m->h.active );
1841 evo_data(push, (m->v.synce << 16) | m->h.synce );
1842 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1843 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1844 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001845 evo_data(push, asyh->mode.v.blankus);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001846 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
1847 evo_data(push, 0x00000000);
1848 } else {
1849 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
1850 evo_data(push, 0x00000000);
1851 evo_data(push, (m->v.active << 16) | m->h.active );
1852 evo_data(push, (m->v.synce << 16) | m->h.synce );
1853 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1854 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1855 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
1856 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
1857 evo_data(push, 0x00000000); /* ??? */
1858 evo_data(push, 0xffffff00);
1859 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
1860 evo_data(push, m->clock * 1000);
1861 evo_data(push, 0x00200000); /* ??? */
1862 evo_data(push, m->clock * 1000);
1863 }
1864 evo_kick(push, core);
1865 }
1866}
1867
1868static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001869nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
1870{
1871 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1872 u32 *push;
1873 if ((push = evo_wait(core, 10))) {
1874 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1875 evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
1876 evo_data(push, 0x00000000);
1877 evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
1878 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1879 evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
1880 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1881 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1882 } else {
1883 evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
1884 evo_data(push, 0x00000000);
1885 evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
1886 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1887 evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
1888 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1889 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1890 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1891 }
1892 evo_kick(push, core);
1893 }
1894}
1895
1896static void
Ben Skeggsad633612016-11-04 17:20:36 +10001897nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
1898{
1899 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001900 nv50_head_lut_clr(head);
1901 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsad633612016-11-04 17:20:36 +10001902 nv50_head_core_clr(head);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001903 if (asyh->clr.curs && (!asyh->set.curs || y))
1904 nv50_head_curs_clr(head);
Ben Skeggsad633612016-11-04 17:20:36 +10001905}
1906
1907static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001908nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1909{
Ben Skeggsc4e68122016-11-04 17:20:36 +10001910 if (asyh->set.view ) nv50_head_view (head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001911 if (asyh->set.mode ) nv50_head_mode (head, asyh);
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001912 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10001913 if (asyh->set.core ) nv50_head_core_set(head, asyh);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001914 if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001915 if (asyh->set.base ) nv50_head_base (head, asyh);
1916 if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
Ben Skeggs7e918332016-11-04 17:20:36 +10001917 if (asyh->set.dither ) nv50_head_dither (head, asyh);
Ben Skeggs7e08d672016-11-04 17:20:36 +10001918 if (asyh->set.procamp) nv50_head_procamp (head, asyh);
1919}
1920
1921static void
1922nv50_head_atomic_check_procamp(struct nv50_head_atom *armh,
1923 struct nv50_head_atom *asyh,
1924 struct nouveau_conn_atom *asyc)
1925{
1926 const int vib = asyc->procamp.color_vibrance - 100;
1927 const int hue = asyc->procamp.vibrant_hue - 90;
1928 const int adj = (vib > 0) ? 50 : 0;
1929 asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff;
1930 asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff;
1931 asyh->set.procamp = true;
Ben Skeggs7e918332016-11-04 17:20:36 +10001932}
1933
1934static void
1935nv50_head_atomic_check_dither(struct nv50_head_atom *armh,
1936 struct nv50_head_atom *asyh,
1937 struct nouveau_conn_atom *asyc)
1938{
1939 struct drm_connector *connector = asyc->state.connector;
1940 u32 mode = 0x00;
1941
1942 if (asyc->dither.mode == DITHERING_MODE_AUTO) {
1943 if (asyh->base.depth > connector->display_info.bpc * 3)
1944 mode = DITHERING_MODE_DYNAMIC2X2;
1945 } else {
1946 mode = asyc->dither.mode;
1947 }
1948
1949 if (asyc->dither.depth == DITHERING_DEPTH_AUTO) {
1950 if (connector->display_info.bpc >= 8)
1951 mode |= DITHERING_DEPTH_8BPC;
1952 } else {
1953 mode |= asyc->dither.depth;
1954 }
1955
1956 asyh->dither.enable = mode;
1957 asyh->dither.bits = mode >> 1;
1958 asyh->dither.mode = mode >> 3;
1959 asyh->set.dither = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001960}
1961
1962static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001963nv50_head_atomic_check_view(struct nv50_head_atom *armh,
1964 struct nv50_head_atom *asyh,
1965 struct nouveau_conn_atom *asyc)
1966{
1967 struct drm_connector *connector = asyc->state.connector;
1968 struct drm_display_mode *omode = &asyh->state.adjusted_mode;
1969 struct drm_display_mode *umode = &asyh->state.mode;
1970 int mode = asyc->scaler.mode;
1971 struct edid *edid;
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04001972 int umode_vdisplay, omode_hdisplay, omode_vdisplay;
Ben Skeggsc4e68122016-11-04 17:20:36 +10001973
1974 if (connector->edid_blob_ptr)
1975 edid = (struct edid *)connector->edid_blob_ptr->data;
1976 else
1977 edid = NULL;
1978
1979 if (!asyc->scaler.full) {
1980 if (mode == DRM_MODE_SCALE_NONE)
1981 omode = umode;
1982 } else {
1983 /* Non-EDID LVDS/eDP mode. */
1984 mode = DRM_MODE_SCALE_FULLSCREEN;
1985 }
1986
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04001987 /* For the user-specified mode, we must ignore doublescan and
1988 * the like, but honor frame packing.
1989 */
1990 umode_vdisplay = umode->vdisplay;
1991 if ((umode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1992 umode_vdisplay += umode->vtotal;
Ben Skeggsc4e68122016-11-04 17:20:36 +10001993 asyh->view.iW = umode->hdisplay;
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04001994 asyh->view.iH = umode_vdisplay;
1995 /* For the output mode, we can just use the stock helper. */
1996 drm_mode_get_hv_timing(omode, &omode_hdisplay, &omode_vdisplay);
1997 asyh->view.oW = omode_hdisplay;
1998 asyh->view.oH = omode_vdisplay;
Ben Skeggsc4e68122016-11-04 17:20:36 +10001999
2000 /* Add overscan compensation if necessary, will keep the aspect
2001 * ratio the same as the backend mode unless overridden by the
2002 * user setting both hborder and vborder properties.
2003 */
2004 if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
2005 (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
2006 drm_detect_hdmi_monitor(edid)))) {
2007 u32 bX = asyc->scaler.underscan.hborder;
2008 u32 bY = asyc->scaler.underscan.vborder;
2009 u32 r = (asyh->view.oH << 19) / asyh->view.oW;
2010
2011 if (bX) {
2012 asyh->view.oW -= (bX * 2);
2013 if (bY) asyh->view.oH -= (bY * 2);
2014 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2015 } else {
2016 asyh->view.oW -= (asyh->view.oW >> 4) + 32;
2017 if (bY) asyh->view.oH -= (bY * 2);
2018 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2019 }
2020 }
2021
2022 /* Handle CENTER/ASPECT scaling, taking into account the areas
2023 * removed already for overscan compensation.
2024 */
2025 switch (mode) {
2026 case DRM_MODE_SCALE_CENTER:
2027 asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04002028 asyh->view.oH = min((u16)umode_vdisplay, asyh->view.oH);
Ben Skeggsc4e68122016-11-04 17:20:36 +10002029 /* fall-through */
2030 case DRM_MODE_SCALE_ASPECT:
2031 if (asyh->view.oH < asyh->view.oW) {
2032 u32 r = (asyh->view.iW << 19) / asyh->view.iH;
2033 asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
2034 } else {
2035 u32 r = (asyh->view.iH << 19) / asyh->view.iW;
2036 asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2037 }
2038 break;
2039 default:
2040 break;
2041 }
2042
2043 asyh->set.view = true;
2044}
2045
2046static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002047nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
2048{
2049 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002050 struct nv50_head_mode *m = &asyh->mode;
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002051 u32 blankus;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002052
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04002053 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V | CRTC_STEREO_DOUBLE);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002054
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002055 /*
2056 * DRM modes are defined in terms of a repeating interval
2057 * starting with the active display area. The hardware modes
2058 * are defined in terms of a repeating interval starting one
2059 * unit (pixel or line) into the sync pulse. So, add bias.
2060 */
2061
2062 m->h.active = mode->crtc_htotal;
2063 m->h.synce = mode->crtc_hsync_end - mode->crtc_hsync_start - 1;
2064 m->h.blanke = mode->crtc_hblank_end - mode->crtc_hsync_start - 1;
2065 m->h.blanks = m->h.blanke + mode->crtc_hdisplay;
2066
2067 m->v.active = mode->crtc_vtotal;
2068 m->v.synce = mode->crtc_vsync_end - mode->crtc_vsync_start - 1;
2069 m->v.blanke = mode->crtc_vblank_end - mode->crtc_vsync_start - 1;
2070 m->v.blanks = m->v.blanke + mode->crtc_vdisplay;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002071
2072 /*XXX: Safe underestimate, even "0" works */
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002073 blankus = (m->v.active - mode->crtc_vdisplay - 2) * m->h.active;
Ben Skeggsaeecfcd2017-04-05 09:12:54 +10002074 blankus *= 1000;
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002075 blankus /= mode->crtc_clock;
Ben Skeggsaeecfcd2017-04-05 09:12:54 +10002076 m->v.blankus = blankus;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002077
2078 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002079 m->v.blank2e = m->v.active + m->v.blanke;
2080 m->v.blank2s = m->v.blank2e + mode->crtc_vdisplay;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002081 m->v.active = (m->v.active * 2) + 1;
2082 m->interlace = true;
2083 } else {
2084 m->v.blank2e = 0;
2085 m->v.blank2s = 1;
2086 m->interlace = false;
2087 }
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002088 m->clock = mode->crtc_clock;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002089
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002090 asyh->set.mode = true;
2091}
2092
2093static int
2094nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
2095{
2096 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggsad633612016-11-04 17:20:36 +10002097 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002098 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002099 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002100 struct nv50_head_atom *asyh = nv50_head_atom(state);
Ben Skeggs839ca902016-11-04 17:20:36 +10002101 struct nouveau_conn_atom *asyc = NULL;
2102 struct drm_connector_state *conns;
2103 struct drm_connector *conn;
2104 int i;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002105
2106 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002107 if (asyh->state.active) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02002108 for_each_new_connector_in_state(asyh->state.state, conn, conns, i) {
Ben Skeggs839ca902016-11-04 17:20:36 +10002109 if (conns->crtc == crtc) {
2110 asyc = nouveau_conn_atom(conns);
2111 break;
2112 }
2113 }
2114
2115 if (armh->state.active) {
2116 if (asyc) {
2117 if (asyh->state.mode_changed)
2118 asyc->set.scaler = true;
2119 if (armh->base.depth != asyh->base.depth)
2120 asyc->set.dither = true;
2121 }
2122 } else {
Gustavo A. R. Silva86276922017-05-22 14:12:37 -05002123 if (asyc)
2124 asyc->set.mask = ~0;
Ben Skeggs839ca902016-11-04 17:20:36 +10002125 asyh->set.mask = ~0;
2126 }
2127
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002128 if (asyh->state.mode_changed)
2129 nv50_head_atomic_check_mode(head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10002130
Ben Skeggs839ca902016-11-04 17:20:36 +10002131 if (asyc) {
2132 if (asyc->set.scaler)
2133 nv50_head_atomic_check_view(armh, asyh, asyc);
2134 if (asyc->set.dither)
2135 nv50_head_atomic_check_dither(armh, asyh, asyc);
2136 if (asyc->set.procamp)
2137 nv50_head_atomic_check_procamp(armh, asyh, asyc);
2138 }
2139
Ben Skeggsad633612016-11-04 17:20:36 +10002140 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
2141 asyh->core.x = asyh->base.x;
2142 asyh->core.y = asyh->base.y;
2143 asyh->core.w = asyh->base.w;
2144 asyh->core.h = asyh->base.h;
2145 } else
Ben Skeggsea8ee392016-11-04 17:20:36 +10002146 if ((asyh->core.visible = asyh->curs.visible)) {
Ben Skeggsad633612016-11-04 17:20:36 +10002147 /*XXX: We need to either find some way of having the
2148 * primary base layer appear black, while still
2149 * being able to display the other layers, or we
2150 * need to allocate a dummy black surface here.
2151 */
2152 asyh->core.x = 0;
2153 asyh->core.y = 0;
2154 asyh->core.w = asyh->state.mode.hdisplay;
2155 asyh->core.h = asyh->state.mode.vdisplay;
2156 }
2157 asyh->core.handle = disp->mast.base.vram.handle;
2158 asyh->core.offset = 0;
2159 asyh->core.format = 0xcf;
2160 asyh->core.kind = 0;
2161 asyh->core.layout = 1;
2162 asyh->core.block = 0;
2163 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002164 asyh->lut.handle = disp->mast.base.vram.handle;
2165 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002166 asyh->set.base = armh->base.cpp != asyh->base.cpp;
2167 asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
Ben Skeggsad633612016-11-04 17:20:36 +10002168 } else {
2169 asyh->core.visible = false;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002170 asyh->curs.visible = false;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002171 asyh->base.cpp = 0;
2172 asyh->ovly.cpp = 0;
Ben Skeggsad633612016-11-04 17:20:36 +10002173 }
2174
2175 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
2176 if (asyh->core.visible) {
2177 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
2178 asyh->set.core = true;
2179 } else
2180 if (armh->core.visible) {
2181 asyh->clr.core = true;
2182 }
Ben Skeggsea8ee392016-11-04 17:20:36 +10002183
2184 if (asyh->curs.visible) {
2185 if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
2186 asyh->set.curs = true;
2187 } else
2188 if (armh->curs.visible) {
2189 asyh->clr.curs = true;
2190 }
Ben Skeggsad633612016-11-04 17:20:36 +10002191 } else {
2192 asyh->clr.core = armh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002193 asyh->clr.curs = armh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10002194 asyh->set.core = asyh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002195 asyh->set.curs = asyh->curs.visible;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002196 }
2197
Ben Skeggs839ca902016-11-04 17:20:36 +10002198 if (asyh->clr.mask || asyh->set.mask)
2199 nv50_atom(asyh->state.state)->lock_core = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002200 return 0;
2201}
2202
Ben Skeggs438d99e2011-07-05 16:48:06 +10002203static void
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002204nv50_head_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002205{
Ben Skeggse225f442012-11-21 14:40:21 +10002206 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002207 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2208 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
Peter Rosin804ea3e2017-07-13 18:25:35 +02002209 u16 *r, *g, *b;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002210 int i;
2211
Peter Rosin804ea3e2017-07-13 18:25:35 +02002212 r = crtc->gamma_store;
2213 g = r + crtc->gamma_size;
2214 b = g + crtc->gamma_size;
Ben Skeggsde8268c2012-11-16 10:24:31 +10002215
Peter Rosin804ea3e2017-07-13 18:25:35 +02002216 for (i = 0; i < 256; i++) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002217 if (disp->disp->oclass < GF110_DISP) {
Peter Rosin804ea3e2017-07-13 18:25:35 +02002218 writew((*r++ >> 2) + 0x0000, lut + (i * 0x08) + 0);
2219 writew((*g++ >> 2) + 0x0000, lut + (i * 0x08) + 2);
2220 writew((*b++ >> 2) + 0x0000, lut + (i * 0x08) + 4);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002221 } else {
Peter Rosin804ea3e2017-07-13 18:25:35 +02002222 /* 0x6000 interferes with the 14-bit color??? */
2223 writew((*r++ >> 2) + 0x6000, lut + (i * 0x20) + 0);
2224 writew((*g++ >> 2) + 0x6000, lut + (i * 0x20) + 2);
2225 writew((*b++ >> 2) + 0x6000, lut + (i * 0x20) + 4);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002226 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002227 }
2228}
2229
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002230static const struct drm_crtc_helper_funcs
2231nv50_head_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10002232 .atomic_check = nv50_head_atomic_check,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002233};
2234
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002235static int
2236nv50_head_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02002237 uint32_t size,
2238 struct drm_modeset_acquire_ctx *ctx)
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002239{
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002240 nv50_head_lut_load(crtc);
2241 return 0;
2242}
2243
Ben Skeggs839ca902016-11-04 17:20:36 +10002244static void
2245nv50_head_atomic_destroy_state(struct drm_crtc *crtc,
2246 struct drm_crtc_state *state)
2247{
2248 struct nv50_head_atom *asyh = nv50_head_atom(state);
2249 __drm_atomic_helper_crtc_destroy_state(&asyh->state);
2250 kfree(asyh);
2251}
2252
2253static struct drm_crtc_state *
2254nv50_head_atomic_duplicate_state(struct drm_crtc *crtc)
2255{
2256 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
2257 struct nv50_head_atom *asyh;
2258 if (!(asyh = kmalloc(sizeof(*asyh), GFP_KERNEL)))
2259 return NULL;
2260 __drm_atomic_helper_crtc_duplicate_state(crtc, &asyh->state);
2261 asyh->view = armh->view;
2262 asyh->mode = armh->mode;
2263 asyh->lut = armh->lut;
2264 asyh->core = armh->core;
2265 asyh->curs = armh->curs;
2266 asyh->base = armh->base;
2267 asyh->ovly = armh->ovly;
2268 asyh->dither = armh->dither;
2269 asyh->procamp = armh->procamp;
2270 asyh->clr.mask = 0;
2271 asyh->set.mask = 0;
2272 return &asyh->state;
2273}
2274
2275static void
2276__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
2277 struct drm_crtc_state *state)
2278{
2279 if (crtc->state)
2280 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
2281 crtc->state = state;
2282 crtc->state->crtc = crtc;
2283}
2284
2285static void
2286nv50_head_reset(struct drm_crtc *crtc)
2287{
2288 struct nv50_head_atom *asyh;
2289
2290 if (WARN_ON(!(asyh = kzalloc(sizeof(*asyh), GFP_KERNEL))))
2291 return;
2292
2293 __drm_atomic_helper_crtc_reset(crtc, &asyh->state);
2294}
2295
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002296static void
2297nv50_head_destroy(struct drm_crtc *crtc)
2298{
2299 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2300 struct nv50_disp *disp = nv50_disp(crtc->dev);
2301 struct nv50_head *head = nv50_head(crtc);
2302
2303 nv50_dmac_destroy(&head->ovly.base, disp->disp);
2304 nv50_pioc_destroy(&head->oimm.base);
2305
2306 nouveau_bo_unmap(nv_crtc->lut.nvbo);
2307 if (nv_crtc->lut.nvbo)
2308 nouveau_bo_unpin(nv_crtc->lut.nvbo);
2309 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
2310
2311 drm_crtc_cleanup(crtc);
2312 kfree(crtc);
2313}
2314
2315static const struct drm_crtc_funcs
2316nv50_head_func = {
Ben Skeggs839ca902016-11-04 17:20:36 +10002317 .reset = nv50_head_reset,
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002318 .gamma_set = nv50_head_gamma_set,
2319 .destroy = nv50_head_destroy,
Ben Skeggs839ca902016-11-04 17:20:36 +10002320 .set_config = drm_atomic_helper_set_config,
Andrey Grodzovsky612fb5d2017-02-02 16:56:30 -05002321 .page_flip = drm_atomic_helper_page_flip,
Ben Skeggs839ca902016-11-04 17:20:36 +10002322 .atomic_duplicate_state = nv50_head_atomic_duplicate_state,
2323 .atomic_destroy_state = nv50_head_atomic_destroy_state,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002324};
2325
2326static int
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002327nv50_head_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002328{
Ben Skeggsa01ca782015-08-20 14:54:15 +10002329 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002330 struct nvif_device *device = &drm->client.device;
Ben Skeggse225f442012-11-21 14:40:21 +10002331 struct nv50_disp *disp = nv50_disp(dev);
2332 struct nv50_head *head;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002333 struct nv50_base *base;
Ben Skeggs22e927d2016-11-04 17:20:36 +10002334 struct nv50_curs *curs;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002335 struct drm_crtc *crtc;
Peter Rosin804ea3e2017-07-13 18:25:35 +02002336 int ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002337
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002338 head = kzalloc(sizeof(*head), GFP_KERNEL);
2339 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002340 return -ENOMEM;
2341
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002342 head->base.index = index;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002343 ret = nv50_base_new(drm, head, &base);
Ben Skeggs22e927d2016-11-04 17:20:36 +10002344 if (ret == 0)
2345 ret = nv50_curs_new(drm, head, &curs);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002346 if (ret) {
2347 kfree(head);
2348 return ret;
2349 }
2350
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002351 crtc = &head->base.base;
Ben Skeggs839ca902016-11-04 17:20:36 +10002352 drm_crtc_init_with_planes(dev, crtc, &base->wndw.plane,
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002353 &curs->wndw.plane, &nv50_head_func,
Ben Skeggs839ca902016-11-04 17:20:36 +10002354 "head-%d", head->base.index);
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002355 drm_crtc_helper_add(crtc, &nv50_head_help);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002356 drm_mode_crtc_set_gamma_size(crtc, 256);
2357
Ben Skeggsbab7cc12016-05-24 17:26:48 +10002358 ret = nouveau_bo_new(&drm->client, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002359 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002360 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002361 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002362 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002363 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002364 if (ret)
2365 nouveau_bo_unpin(head->base.lut.nvbo);
2366 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002367 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002368 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002369 }
2370
2371 if (ret)
2372 goto out;
2373
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002374 /* allocate overlay resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10002375 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002376 if (ret)
2377 goto out;
2378
Ben Skeggsa01ca782015-08-20 14:54:15 +10002379 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
2380 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002381 if (ret)
2382 goto out;
2383
Ben Skeggs438d99e2011-07-05 16:48:06 +10002384out:
2385 if (ret)
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002386 nv50_head_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002387 return ret;
2388}
2389
2390/******************************************************************************
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002391 * Output path helpers
Ben Skeggsa91d3222014-12-22 16:30:13 +10002392 *****************************************************************************/
Ben Skeggs6c22ea32017-05-19 23:59:35 +10002393static void
2394nv50_outp_release(struct nouveau_encoder *nv_encoder)
2395{
2396 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
2397 struct {
2398 struct nv50_disp_mthd_v1 base;
2399 } args = {
2400 .base.version = 1,
2401 .base.method = NV50_DISP_MTHD_V1_RELEASE,
2402 .base.hasht = nv_encoder->dcb->hasht,
2403 .base.hashm = nv_encoder->dcb->hashm,
2404 };
2405
2406 nvif_mthd(disp->disp, 0, &args, sizeof(args));
2407 nv_encoder->or = -1;
2408 nv_encoder->link = 0;
2409}
2410
2411static int
2412nv50_outp_acquire(struct nouveau_encoder *nv_encoder)
2413{
2414 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
2415 struct nv50_disp *disp = nv50_disp(drm->dev);
2416 struct {
2417 struct nv50_disp_mthd_v1 base;
2418 struct nv50_disp_acquire_v0 info;
2419 } args = {
2420 .base.version = 1,
2421 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
2422 .base.hasht = nv_encoder->dcb->hasht,
2423 .base.hashm = nv_encoder->dcb->hashm,
2424 };
2425 int ret;
2426
2427 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2428 if (ret) {
2429 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
2430 return ret;
2431 }
2432
2433 nv_encoder->or = args.info.or;
2434 nv_encoder->link = args.info.link;
2435 return 0;
2436}
2437
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002438static int
2439nv50_outp_atomic_check_view(struct drm_encoder *encoder,
2440 struct drm_crtc_state *crtc_state,
2441 struct drm_connector_state *conn_state,
2442 struct drm_display_mode *native_mode)
2443{
2444 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
2445 struct drm_display_mode *mode = &crtc_state->mode;
2446 struct drm_connector *connector = conn_state->connector;
2447 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
2448 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
2449
2450 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
2451 asyc->scaler.full = false;
2452 if (!native_mode)
2453 return 0;
2454
2455 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
2456 switch (connector->connector_type) {
2457 case DRM_MODE_CONNECTOR_LVDS:
2458 case DRM_MODE_CONNECTOR_eDP:
2459 /* Force use of scaler for non-EDID modes. */
2460 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
2461 break;
2462 mode = native_mode;
2463 asyc->scaler.full = true;
2464 break;
2465 default:
2466 break;
2467 }
2468 } else {
2469 mode = native_mode;
2470 }
2471
2472 if (!drm_mode_equal(adjusted_mode, mode)) {
2473 drm_mode_copy(adjusted_mode, mode);
2474 crtc_state->mode_changed = true;
2475 }
2476
2477 return 0;
2478}
2479
Ben Skeggs839ca902016-11-04 17:20:36 +10002480static int
2481nv50_outp_atomic_check(struct drm_encoder *encoder,
2482 struct drm_crtc_state *crtc_state,
2483 struct drm_connector_state *conn_state)
Ben Skeggsa91d3222014-12-22 16:30:13 +10002484{
Ben Skeggs839ca902016-11-04 17:20:36 +10002485 struct nouveau_connector *nv_connector =
2486 nouveau_connector(conn_state->connector);
2487 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2488 nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10002489}
2490
2491/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002492 * DAC
2493 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002494static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002495nv50_dac_disable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002496{
Ben Skeggsf20c6652016-11-04 17:20:36 +10002497 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2498 struct nv50_mast *mast = nv50_mast(encoder->dev);
2499 const int or = nv_encoder->or;
2500 u32 *push;
2501
2502 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10002503 push = evo_wait(mast, 4);
2504 if (push) {
2505 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2506 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2507 evo_data(push, 0x00000000);
2508 } else {
2509 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2510 evo_data(push, 0x00000000);
2511 }
2512 evo_kick(push, mast);
2513 }
2514 }
2515
2516 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10002517 nv50_outp_release(nv_encoder);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002518}
2519
2520static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002521nv50_dac_enable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002522{
Ben Skeggse225f442012-11-21 14:40:21 +10002523 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002524 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2525 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002526 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggs97b19b52012-11-16 11:21:37 +10002527 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002528
Ben Skeggs6c22ea32017-05-19 23:59:35 +10002529 nv50_outp_acquire(nv_encoder);
2530
Ben Skeggs97b19b52012-11-16 11:21:37 +10002531 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002532 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002533 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002534 u32 syncs = 0x00000000;
2535
2536 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2537 syncs |= 0x00000001;
2538 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2539 syncs |= 0x00000002;
2540
2541 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
2542 evo_data(push, 1 << nv_crtc->index);
2543 evo_data(push, syncs);
2544 } else {
2545 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2546 u32 syncs = 0x00000001;
2547
2548 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2549 syncs |= 0x00000008;
2550 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2551 syncs |= 0x00000010;
2552
2553 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2554 magic |= 0x00000001;
2555
2556 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2557 evo_data(push, syncs);
2558 evo_data(push, magic);
2559 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2560 evo_data(push, 1 << nv_crtc->index);
2561 }
2562
2563 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002564 }
2565
2566 nv_encoder->crtc = encoder->crtc;
2567}
2568
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002569static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10002570nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002571{
Ben Skeggsc4abd312014-08-10 04:10:26 +10002572 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002573 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10002574 struct {
2575 struct nv50_disp_mthd_v1 base;
2576 struct nv50_disp_dac_load_v0 load;
2577 } args = {
2578 .base.version = 1,
2579 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2580 .base.hasht = nv_encoder->dcb->hasht,
2581 .base.hashm = nv_encoder->dcb->hashm,
2582 };
2583 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10002584
Ben Skeggsc4abd312014-08-10 04:10:26 +10002585 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2586 if (args.load.data == 0)
2587 args.load.data = 340;
2588
2589 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2590 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10002591 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10002592
Ben Skeggs35b21d32012-11-08 12:08:55 +10002593 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002594}
2595
Ben Skeggsf20c6652016-11-04 17:20:36 +10002596static const struct drm_encoder_helper_funcs
2597nv50_dac_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10002598 .atomic_check = nv50_outp_atomic_check,
2599 .enable = nv50_dac_enable,
2600 .disable = nv50_dac_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002601 .detect = nv50_dac_detect
2602};
2603
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002604static void
Ben Skeggse225f442012-11-21 14:40:21 +10002605nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002606{
2607 drm_encoder_cleanup(encoder);
2608 kfree(encoder);
2609}
2610
Ben Skeggsf20c6652016-11-04 17:20:36 +10002611static const struct drm_encoder_funcs
2612nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10002613 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002614};
2615
2616static int
Ben Skeggse225f442012-11-21 14:40:21 +10002617nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002618{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002619 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002620 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002621 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002622 struct nouveau_encoder *nv_encoder;
2623 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002624 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002625
2626 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2627 if (!nv_encoder)
2628 return -ENOMEM;
2629 nv_encoder->dcb = dcbe;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002630
2631 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2632 if (bus)
2633 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002634
2635 encoder = to_drm_encoder(nv_encoder);
2636 encoder->possible_crtcs = dcbe->heads;
2637 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002638 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2639 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10002640 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002641
2642 drm_mode_connector_attach_encoder(connector, encoder);
2643 return 0;
2644}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002645
2646/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10002647 * Audio
2648 *****************************************************************************/
2649static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002650nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
2651{
2652 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2653 struct nv50_disp *disp = nv50_disp(encoder->dev);
2654 struct {
2655 struct nv50_disp_mthd_v1 base;
2656 struct nv50_disp_sor_hda_eld_v0 eld;
2657 } args = {
2658 .base.version = 1,
2659 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2660 .base.hasht = nv_encoder->dcb->hasht,
2661 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2662 (0x0100 << nv_crtc->index),
2663 };
2664
2665 nvif_mthd(disp->disp, 0, &args, sizeof(args));
2666}
2667
2668static void
2669nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002670{
2671 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10002672 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10002673 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10002674 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10002675 struct __packed {
2676 struct {
2677 struct nv50_disp_mthd_v1 mthd;
2678 struct nv50_disp_sor_hda_eld_v0 eld;
2679 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002680 u8 data[sizeof(nv_connector->base.eld)];
2681 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10002682 .base.mthd.version = 1,
2683 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2684 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002685 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2686 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002687 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002688
2689 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2690 if (!drm_detect_monitor_audio(nv_connector->edid))
2691 return;
2692
Ben Skeggs120b0c32014-08-10 04:10:26 +10002693 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002694
Jani Nikula938fd8a2014-10-28 16:20:48 +02002695 nvif_mthd(disp->disp, 0, &args,
2696 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002697}
2698
Ben Skeggsf20c6652016-11-04 17:20:36 +10002699/******************************************************************************
2700 * HDMI
2701 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +10002702static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002703nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002704{
2705 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002706 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002707 struct {
2708 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +10002709 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002710 } args = {
2711 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002712 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2713 .base.hasht = nv_encoder->dcb->hasht,
2714 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2715 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002716 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002717
Ben Skeggs120b0c32014-08-10 04:10:26 +10002718 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002719}
2720
Ben Skeggs78951d22011-11-11 18:13:13 +10002721static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002722nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002723{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002724 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2725 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002726 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002727 struct {
2728 struct nv50_disp_mthd_v1 base;
2729 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002730 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
Ben Skeggse00f2232014-08-10 04:10:26 +10002731 } args = {
2732 .base.version = 1,
2733 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2734 .base.hasht = nv_encoder->dcb->hasht,
2735 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2736 (0x0100 << nv_crtc->index),
2737 .pwr.state = 1,
2738 .pwr.rekey = 56, /* binary driver, and tegra, constant */
2739 };
2740 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002741 u32 max_ac_packet;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002742 union hdmi_infoframe avi_frame;
2743 union hdmi_infoframe vendor_frame;
2744 int ret;
2745 int size;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002746
2747 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2748 if (!drm_detect_hdmi_monitor(nv_connector->edid))
2749 return;
2750
Shashank Sharma0c1f5282017-07-13 21:03:07 +05302751 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi, mode,
2752 false);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002753 if (!ret) {
2754 /* We have an AVI InfoFrame, populate it to the display */
2755 args.pwr.avi_infoframe_length
2756 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
2757 }
2758
Ville Syrjäläf1781e92017-11-13 19:04:19 +02002759 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
2760 &nv_connector->base, mode);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002761 if (!ret) {
2762 /* We have a Vendor InfoFrame, populate it to the display */
2763 args.pwr.vendor_infoframe_length
2764 = hdmi_infoframe_pack(&vendor_frame,
2765 args.infoframes
2766 + args.pwr.avi_infoframe_length,
2767 17);
2768 }
2769
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002770 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10002771 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002772 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10002773 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002774
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002775 size = sizeof(args.base)
2776 + sizeof(args.pwr)
2777 + args.pwr.avi_infoframe_length
2778 + args.pwr.vendor_infoframe_length;
2779 nvif_mthd(disp->disp, 0, &args, size);
Ben Skeggsf20c6652016-11-04 17:20:36 +10002780 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10002781}
2782
2783/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002784 * MST
2785 *****************************************************************************/
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002786#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
2787#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
2788#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
2789
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002790struct nv50_mstm {
2791 struct nouveau_encoder *outp;
2792
2793 struct drm_dp_mst_topology_mgr mgr;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002794 struct nv50_msto *msto[4];
2795
2796 bool modified;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10002797 bool disabled;
2798 int links;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002799};
2800
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002801struct nv50_mstc {
2802 struct nv50_mstm *mstm;
2803 struct drm_dp_mst_port *port;
2804 struct drm_connector connector;
2805
2806 struct drm_display_mode *native;
2807 struct edid *edid;
2808
2809 int pbn;
2810};
2811
2812struct nv50_msto {
2813 struct drm_encoder encoder;
2814
2815 struct nv50_head *head;
2816 struct nv50_mstc *mstc;
2817 bool disabled;
2818};
2819
2820static struct drm_dp_payload *
2821nv50_msto_payload(struct nv50_msto *msto)
2822{
2823 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2824 struct nv50_mstc *mstc = msto->mstc;
2825 struct nv50_mstm *mstm = mstc->mstm;
2826 int vcpi = mstc->port->vcpi.vcpi, i;
2827
2828 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
2829 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2830 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2831 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
2832 mstm->outp->base.base.name, i, payload->vcpi,
2833 payload->start_slot, payload->num_slots);
2834 }
2835
2836 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2837 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2838 if (payload->vcpi == vcpi)
2839 return payload;
2840 }
2841
2842 return NULL;
2843}
2844
2845static void
2846nv50_msto_cleanup(struct nv50_msto *msto)
2847{
2848 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2849 struct nv50_mstc *mstc = msto->mstc;
2850 struct nv50_mstm *mstm = mstc->mstm;
2851
2852 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
2853 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
2854 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
2855 if (msto->disabled) {
2856 msto->mstc = NULL;
2857 msto->head = NULL;
2858 msto->disabled = false;
2859 }
2860}
2861
2862static void
2863nv50_msto_prepare(struct nv50_msto *msto)
2864{
2865 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2866 struct nv50_mstc *mstc = msto->mstc;
2867 struct nv50_mstm *mstm = mstc->mstm;
2868 struct {
2869 struct nv50_disp_mthd_v1 base;
2870 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
2871 } args = {
2872 .base.version = 1,
2873 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
2874 .base.hasht = mstm->outp->dcb->hasht,
2875 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
2876 (0x0100 << msto->head->base.index),
2877 };
2878
2879 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
2880 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
2881 struct drm_dp_payload *payload = nv50_msto_payload(msto);
2882 if (payload) {
2883 args.vcpi.start_slot = payload->start_slot;
2884 args.vcpi.num_slots = payload->num_slots;
2885 args.vcpi.pbn = mstc->port->vcpi.pbn;
2886 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
2887 }
2888 }
2889
2890 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
2891 msto->encoder.name, msto->head->base.base.name,
2892 args.vcpi.start_slot, args.vcpi.num_slots,
2893 args.vcpi.pbn, args.vcpi.aligned_pbn);
2894 nvif_mthd(&drm->display->disp, 0, &args, sizeof(args));
2895}
2896
2897static int
2898nv50_msto_atomic_check(struct drm_encoder *encoder,
2899 struct drm_crtc_state *crtc_state,
2900 struct drm_connector_state *conn_state)
2901{
2902 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
2903 struct nv50_mstm *mstm = mstc->mstm;
2904 int bpp = conn_state->connector->display_info.bpc * 3;
2905 int slots;
2906
2907 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
2908
2909 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2910 if (slots < 0)
2911 return slots;
2912
2913 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2914 mstc->native);
2915}
2916
2917static void
2918nv50_msto_enable(struct drm_encoder *encoder)
2919{
2920 struct nv50_head *head = nv50_head(encoder->crtc);
2921 struct nv50_msto *msto = nv50_msto(encoder);
2922 struct nv50_mstc *mstc = NULL;
2923 struct nv50_mstm *mstm = NULL;
2924 struct drm_connector *connector;
Gustavo Padovan875dd622017-05-11 16:10:46 -03002925 struct drm_connector_list_iter conn_iter;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002926 u8 proto, depth;
2927 int slots;
2928 bool r;
2929
Gustavo Padovan875dd622017-05-11 16:10:46 -03002930 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
2931 drm_for_each_connector_iter(connector, &conn_iter) {
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002932 if (connector->state->best_encoder == &msto->encoder) {
2933 mstc = nv50_mstc(connector);
2934 mstm = mstc->mstm;
2935 break;
2936 }
2937 }
Gustavo Padovan875dd622017-05-11 16:10:46 -03002938 drm_connector_list_iter_end(&conn_iter);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002939
2940 if (WARN_ON(!mstc))
2941 return;
2942
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002943 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2944 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002945 WARN_ON(!r);
2946
Ben Skeggs6c22ea32017-05-19 23:59:35 +10002947 if (!mstm->links++)
2948 nv50_outp_acquire(mstm->outp);
2949
2950 if (mstm->outp->link & 1)
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002951 proto = 0x8;
2952 else
2953 proto = 0x9;
2954
2955 switch (mstc->connector.display_info.bpc) {
2956 case 6: depth = 0x2; break;
2957 case 8: depth = 0x5; break;
2958 case 10:
2959 default: depth = 0x6; break;
2960 }
2961
2962 mstm->outp->update(mstm->outp, head->base.index,
2963 &head->base.base.state->adjusted_mode, proto, depth);
2964
2965 msto->head = head;
2966 msto->mstc = mstc;
2967 mstm->modified = true;
2968}
2969
2970static void
2971nv50_msto_disable(struct drm_encoder *encoder)
2972{
2973 struct nv50_msto *msto = nv50_msto(encoder);
2974 struct nv50_mstc *mstc = msto->mstc;
2975 struct nv50_mstm *mstm = mstc->mstm;
2976
2977 if (mstc->port)
2978 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
2979
2980 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
2981 mstm->modified = true;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10002982 if (!--mstm->links)
2983 mstm->disabled = true;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002984 msto->disabled = true;
2985}
2986
2987static const struct drm_encoder_helper_funcs
2988nv50_msto_help = {
2989 .disable = nv50_msto_disable,
2990 .enable = nv50_msto_enable,
2991 .atomic_check = nv50_msto_atomic_check,
2992};
2993
2994static void
2995nv50_msto_destroy(struct drm_encoder *encoder)
2996{
2997 struct nv50_msto *msto = nv50_msto(encoder);
2998 drm_encoder_cleanup(&msto->encoder);
2999 kfree(msto);
3000}
3001
3002static const struct drm_encoder_funcs
3003nv50_msto = {
3004 .destroy = nv50_msto_destroy,
3005};
3006
3007static int
3008nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
3009 struct nv50_msto **pmsto)
3010{
3011 struct nv50_msto *msto;
3012 int ret;
3013
3014 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
3015 return -ENOMEM;
3016
3017 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
3018 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
3019 if (ret) {
3020 kfree(*pmsto);
3021 *pmsto = NULL;
3022 return ret;
3023 }
3024
3025 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
3026 msto->encoder.possible_crtcs = heads;
3027 return 0;
3028}
3029
3030static struct drm_encoder *
3031nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
3032 struct drm_connector_state *connector_state)
3033{
3034 struct nv50_head *head = nv50_head(connector_state->crtc);
3035 struct nv50_mstc *mstc = nv50_mstc(connector);
3036 if (mstc->port) {
3037 struct nv50_mstm *mstm = mstc->mstm;
3038 return &mstm->msto[head->base.index]->encoder;
3039 }
3040 return NULL;
3041}
3042
3043static struct drm_encoder *
3044nv50_mstc_best_encoder(struct drm_connector *connector)
3045{
3046 struct nv50_mstc *mstc = nv50_mstc(connector);
3047 if (mstc->port) {
3048 struct nv50_mstm *mstm = mstc->mstm;
3049 return &mstm->msto[0]->encoder;
3050 }
3051 return NULL;
3052}
3053
3054static enum drm_mode_status
3055nv50_mstc_mode_valid(struct drm_connector *connector,
3056 struct drm_display_mode *mode)
3057{
3058 return MODE_OK;
3059}
3060
3061static int
3062nv50_mstc_get_modes(struct drm_connector *connector)
3063{
3064 struct nv50_mstc *mstc = nv50_mstc(connector);
3065 int ret = 0;
3066
3067 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
3068 drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
Jani Nikulad471ed02017-11-01 16:21:02 +02003069 if (mstc->edid)
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003070 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003071
3072 if (!mstc->connector.display_info.bpc)
3073 mstc->connector.display_info.bpc = 8;
3074
3075 if (mstc->native)
3076 drm_mode_destroy(mstc->connector.dev, mstc->native);
3077 mstc->native = nouveau_conn_native_mode(&mstc->connector);
3078 return ret;
3079}
3080
3081static const struct drm_connector_helper_funcs
3082nv50_mstc_help = {
3083 .get_modes = nv50_mstc_get_modes,
3084 .mode_valid = nv50_mstc_mode_valid,
3085 .best_encoder = nv50_mstc_best_encoder,
3086 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
3087};
3088
3089static enum drm_connector_status
3090nv50_mstc_detect(struct drm_connector *connector, bool force)
3091{
3092 struct nv50_mstc *mstc = nv50_mstc(connector);
3093 if (!mstc->port)
3094 return connector_status_disconnected;
3095 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
3096}
3097
3098static void
3099nv50_mstc_destroy(struct drm_connector *connector)
3100{
3101 struct nv50_mstc *mstc = nv50_mstc(connector);
3102 drm_connector_cleanup(&mstc->connector);
3103 kfree(mstc);
3104}
3105
3106static const struct drm_connector_funcs
3107nv50_mstc = {
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003108 .reset = nouveau_conn_reset,
3109 .detect = nv50_mstc_detect,
3110 .fill_modes = drm_helper_probe_single_connector_modes,
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003111 .destroy = nv50_mstc_destroy,
3112 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
3113 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
3114 .atomic_set_property = nouveau_conn_atomic_set_property,
3115 .atomic_get_property = nouveau_conn_atomic_get_property,
3116};
3117
3118static int
3119nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
3120 const char *path, struct nv50_mstc **pmstc)
3121{
3122 struct drm_device *dev = mstm->outp->base.base.dev;
3123 struct nv50_mstc *mstc;
3124 int ret, i;
3125
3126 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
3127 return -ENOMEM;
3128 mstc->mstm = mstm;
3129 mstc->port = port;
3130
3131 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
3132 DRM_MODE_CONNECTOR_DisplayPort);
3133 if (ret) {
3134 kfree(*pmstc);
3135 *pmstc = NULL;
3136 return ret;
3137 }
3138
3139 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
3140
3141 mstc->connector.funcs->reset(&mstc->connector);
3142 nouveau_conn_attach_properties(&mstc->connector);
3143
Colin Ian King27a451e2017-08-17 23:03:23 +01003144 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto[i]; i++)
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003145 drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
3146
3147 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
3148 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
3149 drm_mode_connector_set_path_property(&mstc->connector, path);
3150 return 0;
3151}
3152
3153static void
3154nv50_mstm_cleanup(struct nv50_mstm *mstm)
3155{
3156 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3157 struct drm_encoder *encoder;
3158 int ret;
3159
3160 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
3161 ret = drm_dp_check_act_status(&mstm->mgr);
3162
3163 ret = drm_dp_update_payload_part2(&mstm->mgr);
3164
3165 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3166 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3167 struct nv50_msto *msto = nv50_msto(encoder);
3168 struct nv50_mstc *mstc = msto->mstc;
3169 if (mstc && mstc->mstm == mstm)
3170 nv50_msto_cleanup(msto);
3171 }
3172 }
3173
3174 mstm->modified = false;
3175}
3176
3177static void
3178nv50_mstm_prepare(struct nv50_mstm *mstm)
3179{
3180 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3181 struct drm_encoder *encoder;
3182 int ret;
3183
3184 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
3185 ret = drm_dp_update_payload_part1(&mstm->mgr);
3186
3187 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3188 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3189 struct nv50_msto *msto = nv50_msto(encoder);
3190 struct nv50_mstc *mstc = msto->mstc;
3191 if (mstc && mstc->mstm == mstm)
3192 nv50_msto_prepare(msto);
3193 }
3194 }
Ben Skeggs6c22ea32017-05-19 23:59:35 +10003195
3196 if (mstm->disabled) {
3197 if (!mstm->links)
3198 nv50_outp_release(mstm->outp);
3199 mstm->disabled = false;
3200 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003201}
3202
3203static void
3204nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
3205{
3206 struct nv50_mstm *mstm = nv50_mstm(mgr);
3207 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
3208}
3209
3210static void
3211nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
3212 struct drm_connector *connector)
3213{
3214 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3215 struct nv50_mstc *mstc = nv50_mstc(connector);
3216
3217 drm_connector_unregister(&mstc->connector);
3218
3219 drm_modeset_lock_all(drm->dev);
3220 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
3221 mstc->port = NULL;
3222 drm_modeset_unlock_all(drm->dev);
3223
3224 drm_connector_unreference(&mstc->connector);
3225}
3226
3227static void
3228nv50_mstm_register_connector(struct drm_connector *connector)
3229{
3230 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3231
3232 drm_modeset_lock_all(drm->dev);
3233 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
3234 drm_modeset_unlock_all(drm->dev);
3235
3236 drm_connector_register(connector);
3237}
3238
3239static struct drm_connector *
3240nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
3241 struct drm_dp_mst_port *port, const char *path)
3242{
3243 struct nv50_mstm *mstm = nv50_mstm(mgr);
3244 struct nv50_mstc *mstc;
3245 int ret;
3246
3247 ret = nv50_mstc_new(mstm, port, path, &mstc);
3248 if (ret) {
3249 if (mstc)
3250 mstc->connector.funcs->destroy(&mstc->connector);
3251 return NULL;
3252 }
3253
3254 return &mstc->connector;
3255}
3256
3257static const struct drm_dp_mst_topology_cbs
3258nv50_mstm = {
3259 .add_connector = nv50_mstm_add_connector,
3260 .register_connector = nv50_mstm_register_connector,
3261 .destroy_connector = nv50_mstm_destroy_connector,
3262 .hotplug = nv50_mstm_hotplug,
3263};
3264
3265void
3266nv50_mstm_service(struct nv50_mstm *mstm)
3267{
Ben Skeggs227f66d2017-10-03 16:24:28 +10003268 struct drm_dp_aux *aux = mstm ? mstm->mgr.aux : NULL;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003269 bool handled = true;
3270 int ret;
3271 u8 esi[8] = {};
3272
Ben Skeggs227f66d2017-10-03 16:24:28 +10003273 if (!aux)
3274 return;
3275
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003276 while (handled) {
3277 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
3278 if (ret != 8) {
3279 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3280 return;
3281 }
3282
3283 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
3284 if (!handled)
3285 break;
3286
3287 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
3288 }
3289}
3290
3291void
3292nv50_mstm_remove(struct nv50_mstm *mstm)
3293{
3294 if (mstm)
3295 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3296}
3297
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003298static int
3299nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
3300{
3301 struct nouveau_encoder *outp = mstm->outp;
3302 struct {
3303 struct nv50_disp_mthd_v1 base;
3304 struct nv50_disp_sor_dp_mst_link_v0 mst;
3305 } args = {
3306 .base.version = 1,
3307 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
3308 .base.hasht = outp->dcb->hasht,
3309 .base.hashm = outp->dcb->hashm,
3310 .mst.state = state,
3311 };
3312 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
3313 struct nvif_object *disp = &drm->display->disp;
3314 int ret;
3315
3316 if (dpcd >= 0x12) {
3317 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
3318 if (ret < 0)
3319 return ret;
3320
3321 dpcd &= ~DP_MST_EN;
3322 if (state)
3323 dpcd |= DP_MST_EN;
3324
3325 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
3326 if (ret < 0)
3327 return ret;
3328 }
3329
3330 return nvif_mthd(disp, 0, &args, sizeof(args));
3331}
3332
3333int
3334nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
3335{
3336 int ret, state = 0;
3337
3338 if (!mstm)
3339 return 0;
3340
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10003341 if (dpcd[0] >= 0x12) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003342 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
3343 if (ret < 0)
3344 return ret;
3345
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10003346 if (!(dpcd[1] & DP_MST_CAP))
3347 dpcd[0] = 0x11;
3348 else
3349 state = allow;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003350 }
3351
3352 ret = nv50_mstm_enable(mstm, dpcd[0], state);
3353 if (ret)
3354 return ret;
3355
3356 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
3357 if (ret)
3358 return nv50_mstm_enable(mstm, dpcd[0], 0);
3359
3360 return mstm->mgr.mst_state;
3361}
3362
3363static void
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003364nv50_mstm_fini(struct nv50_mstm *mstm)
3365{
3366 if (mstm && mstm->mgr.mst_state)
3367 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
3368}
3369
3370static void
3371nv50_mstm_init(struct nv50_mstm *mstm)
3372{
3373 if (mstm && mstm->mgr.mst_state)
3374 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
3375}
3376
3377static void
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003378nv50_mstm_del(struct nv50_mstm **pmstm)
3379{
3380 struct nv50_mstm *mstm = *pmstm;
3381 if (mstm) {
3382 kfree(*pmstm);
3383 *pmstm = NULL;
3384 }
3385}
3386
3387static int
3388nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
3389 int conn_base_id, struct nv50_mstm **pmstm)
3390{
3391 const int max_payloads = hweight8(outp->dcb->heads);
3392 struct drm_device *dev = outp->base.base.dev;
3393 struct nv50_mstm *mstm;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003394 int ret, i;
3395 u8 dpcd;
3396
3397 /* This is a workaround for some monitors not functioning
3398 * correctly in MST mode on initial module load. I think
3399 * some bad interaction with the VBIOS may be responsible.
3400 *
3401 * A good ol' off and on again seems to work here ;)
3402 */
3403 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
3404 if (ret >= 0 && dpcd >= 0x12)
3405 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003406
3407 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
3408 return -ENOMEM;
3409 mstm->outp = outp;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003410 mstm->mgr.cbs = &nv50_mstm;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003411
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08003412 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003413 max_payloads, conn_base_id);
3414 if (ret)
3415 return ret;
3416
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003417 for (i = 0; i < max_payloads; i++) {
3418 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
3419 i, &mstm->msto[i]);
3420 if (ret)
3421 return ret;
3422 }
3423
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003424 return 0;
3425}
3426
3427/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003428 * SOR
3429 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003430static void
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003431nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
3432 struct drm_display_mode *mode, u8 proto, u8 depth)
Ben Skeggse84a35a2014-06-05 10:59:55 +10003433{
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003434 struct nv50_dmac *core = &nv50_mast(nv_encoder->base.base.dev)->base;
3435 u32 *push;
3436
3437 if (!mode) {
3438 nv_encoder->ctrl &= ~BIT(head);
3439 if (!(nv_encoder->ctrl & 0x0000000f))
3440 nv_encoder->ctrl = 0;
3441 } else {
3442 nv_encoder->ctrl |= proto << 8;
3443 nv_encoder->ctrl |= BIT(head);
3444 }
3445
3446 if ((push = evo_wait(core, 6))) {
3447 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
3448 if (mode) {
3449 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3450 nv_encoder->ctrl |= 0x00001000;
3451 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3452 nv_encoder->ctrl |= 0x00002000;
3453 nv_encoder->ctrl |= depth << 16;
3454 }
Ben Skeggse84a35a2014-06-05 10:59:55 +10003455 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003456 } else {
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003457 if (mode) {
3458 u32 magic = 0x31ec6000 | (head << 25);
3459 u32 syncs = 0x00000001;
3460 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3461 syncs |= 0x00000008;
3462 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3463 syncs |= 0x00000010;
3464 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3465 magic |= 0x00000001;
3466
3467 evo_mthd(push, 0x0404 + (head * 0x300), 2);
3468 evo_data(push, syncs | (depth << 6));
3469 evo_data(push, magic);
3470 }
Ben Skeggse84a35a2014-06-05 10:59:55 +10003471 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003472 }
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003473 evo_data(push, nv_encoder->ctrl);
3474 evo_kick(push, core);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003475 }
3476}
3477
3478static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003479nv50_sor_disable(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003480{
3481 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003482 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003483
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003484 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10003485
3486 if (nv_crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +10003487 struct nvkm_i2c_aux *aux = nv_encoder->aux;
3488 u8 pwr;
3489
3490 if (aux) {
3491 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
3492 if (ret == 0) {
3493 pwr &= ~DP_SET_POWER_MASK;
3494 pwr |= DP_SET_POWER_D3;
3495 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
3496 }
3497 }
3498
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003499 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003500 nv50_audio_disable(encoder, nv_crtc);
3501 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggs6c22ea32017-05-19 23:59:35 +10003502 nv50_outp_release(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003503 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003504}
3505
3506static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003507nv50_sor_enable(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003508{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003509 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3510 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10003511 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003512 struct {
3513 struct nv50_disp_mthd_v1 base;
3514 struct nv50_disp_sor_lvds_script_v0 lvds;
3515 } lvds = {
3516 .base.version = 1,
3517 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
3518 .base.hasht = nv_encoder->dcb->hasht,
3519 .base.hashm = nv_encoder->dcb->hashm,
3520 };
Ben Skeggse225f442012-11-21 14:40:21 +10003521 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10003522 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10003523 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003524 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10003525 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003526 u8 proto = 0xf;
3527 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003528
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003529 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003530 nv_encoder->crtc = encoder->crtc;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10003531 nv50_outp_acquire(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003532
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003533 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10003534 case DCB_OUTPUT_TMDS:
Ben Skeggs6c22ea32017-05-19 23:59:35 +10003535 if (nv_encoder->link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05003536 proto = 0x1;
3537 /* Only enable dual-link if:
3538 * - Need to (i.e. rate > 165MHz)
3539 * - DCB says we can
3540 * - Not an HDMI monitor, since there's no dual-link
3541 * on HDMI.
3542 */
3543 if (mode->clock >= 165000 &&
3544 nv_encoder->dcb->duallink_possible &&
3545 !drm_detect_hdmi_monitor(nv_connector->edid))
3546 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003547 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003548 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003549 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003550
Ben Skeggsf20c6652016-11-04 17:20:36 +10003551 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003552 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003553 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003554 proto = 0x0;
3555
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003556 if (bios->fp_no_ddc) {
3557 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003558 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003559 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003560 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003561 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10003562 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003563 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003564 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003565 } else
3566 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003567 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003568 }
3569
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003570 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003571 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003572 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003573 } else {
3574 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003575 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003576 }
3577
3578 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003579 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003580 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10003581
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003582 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003583 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003584 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10003585 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003586 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003587 else
3588 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003589 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003590 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10003591 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003592
Ben Skeggs6c22ea32017-05-19 23:59:35 +10003593 if (nv_encoder->link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003594 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003595 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003596 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003597
3598 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003599 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003600 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10003601 BUG();
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003602 break;
3603 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10003604
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003605 nv_encoder->update(nv_encoder, nv_crtc->index, mode, proto, depth);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003606}
3607
Ben Skeggsf20c6652016-11-04 17:20:36 +10003608static const struct drm_encoder_helper_funcs
3609nv50_sor_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10003610 .atomic_check = nv50_outp_atomic_check,
3611 .enable = nv50_sor_enable,
3612 .disable = nv50_sor_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003613};
3614
Ben Skeggs83fc0832011-07-05 13:08:40 +10003615static void
Ben Skeggse225f442012-11-21 14:40:21 +10003616nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003617{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003618 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3619 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003620 drm_encoder_cleanup(encoder);
3621 kfree(encoder);
3622}
3623
Ben Skeggsf20c6652016-11-04 17:20:36 +10003624static const struct drm_encoder_funcs
3625nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10003626 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10003627};
3628
3629static int
Ben Skeggse225f442012-11-21 14:40:21 +10003630nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003631{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003632 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10003633 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003634 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003635 struct nouveau_encoder *nv_encoder;
3636 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003637 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10003638
3639 switch (dcbe->type) {
3640 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
3641 case DCB_OUTPUT_TMDS:
3642 case DCB_OUTPUT_DP:
3643 default:
3644 type = DRM_MODE_ENCODER_TMDS;
3645 break;
3646 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003647
3648 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3649 if (!nv_encoder)
3650 return -ENOMEM;
3651 nv_encoder->dcb = dcbe;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003652 nv_encoder->update = nv50_sor_update;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003653
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003654 encoder = to_drm_encoder(nv_encoder);
3655 encoder->possible_crtcs = dcbe->heads;
3656 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003657 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
3658 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003659 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003660
3661 drm_mode_connector_attach_encoder(connector, encoder);
3662
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003663 if (dcbe->type == DCB_OUTPUT_DP) {
Ben Skeggs13a86512017-07-19 16:49:59 +10003664 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003665 struct nvkm_i2c_aux *aux =
3666 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
3667 if (aux) {
Ben Skeggs13a86512017-07-19 16:49:59 +10003668 if (disp->disp->oclass < GF110_DISP) {
3669 /* HW has no support for address-only
3670 * transactions, so we're required to
3671 * use custom I2C-over-AUX code.
3672 */
3673 nv_encoder->i2c = &aux->i2c;
3674 } else {
3675 nv_encoder->i2c = &nv_connector->aux.ddc;
3676 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003677 nv_encoder->aux = aux;
3678 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003679
3680 /*TODO: Use DP Info Table to check for support. */
Ben Skeggs13a86512017-07-19 16:49:59 +10003681 if (disp->disp->oclass >= GF110_DISP) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003682 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
3683 nv_connector->base.base.id,
3684 &nv_encoder->dp.mstm);
3685 if (ret)
3686 return ret;
3687 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003688 } else {
3689 struct nvkm_i2c_bus *bus =
3690 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
3691 if (bus)
3692 nv_encoder->i2c = &bus->i2c;
3693 }
3694
Ben Skeggs83fc0832011-07-05 13:08:40 +10003695 return 0;
3696}
Ben Skeggs26f6d882011-07-04 16:25:18 +10003697
3698/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10003699 * PIOR
3700 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +10003701static int
3702nv50_pior_atomic_check(struct drm_encoder *encoder,
3703 struct drm_crtc_state *crtc_state,
3704 struct drm_connector_state *conn_state)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003705{
Ben Skeggs839ca902016-11-04 17:20:36 +10003706 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
3707 if (ret)
3708 return ret;
3709 crtc_state->adjusted_mode.clock *= 2;
3710 return 0;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003711}
3712
3713static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003714nv50_pior_disable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003715{
Ben Skeggsf20c6652016-11-04 17:20:36 +10003716 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3717 struct nv50_mast *mast = nv50_mast(encoder->dev);
3718 const int or = nv_encoder->or;
3719 u32 *push;
3720
3721 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10003722 push = evo_wait(mast, 4);
3723 if (push) {
3724 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
3725 evo_mthd(push, 0x0700 + (or * 0x040), 1);
3726 evo_data(push, 0x00000000);
3727 }
3728 evo_kick(push, mast);
3729 }
3730 }
3731
3732 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10003733 nv50_outp_release(nv_encoder);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003734}
3735
3736static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003737nv50_pior_enable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003738{
3739 struct nv50_mast *mast = nv50_mast(encoder->dev);
3740 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3741 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3742 struct nouveau_connector *nv_connector;
Ben Skeggs839ca902016-11-04 17:20:36 +10003743 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003744 u8 owner = 1 << nv_crtc->index;
3745 u8 proto, depth;
3746 u32 *push;
3747
Ben Skeggs6c22ea32017-05-19 23:59:35 +10003748 nv50_outp_acquire(nv_encoder);
3749
Ben Skeggseb6313a2013-02-11 09:52:58 +10003750 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3751 switch (nv_connector->base.display_info.bpc) {
3752 case 10: depth = 0x6; break;
3753 case 8: depth = 0x5; break;
3754 case 6: depth = 0x2; break;
3755 default: depth = 0x0; break;
3756 }
3757
3758 switch (nv_encoder->dcb->type) {
3759 case DCB_OUTPUT_TMDS:
3760 case DCB_OUTPUT_DP:
3761 proto = 0x0;
3762 break;
3763 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10003764 BUG();
Ben Skeggseb6313a2013-02-11 09:52:58 +10003765 break;
3766 }
3767
Ben Skeggseb6313a2013-02-11 09:52:58 +10003768 push = evo_wait(mast, 8);
3769 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003770 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003771 u32 ctrl = (depth << 16) | (proto << 8) | owner;
3772 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3773 ctrl |= 0x00001000;
3774 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3775 ctrl |= 0x00002000;
3776 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
3777 evo_data(push, ctrl);
3778 }
3779
3780 evo_kick(push, mast);
3781 }
3782
3783 nv_encoder->crtc = encoder->crtc;
3784}
3785
Ben Skeggsf20c6652016-11-04 17:20:36 +10003786static const struct drm_encoder_helper_funcs
3787nv50_pior_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10003788 .atomic_check = nv50_pior_atomic_check,
3789 .enable = nv50_pior_enable,
3790 .disable = nv50_pior_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003791};
Ben Skeggseb6313a2013-02-11 09:52:58 +10003792
3793static void
3794nv50_pior_destroy(struct drm_encoder *encoder)
3795{
3796 drm_encoder_cleanup(encoder);
3797 kfree(encoder);
3798}
3799
Ben Skeggsf20c6652016-11-04 17:20:36 +10003800static const struct drm_encoder_funcs
3801nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003802 .destroy = nv50_pior_destroy,
3803};
3804
3805static int
3806nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
3807{
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003808 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003809 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003810 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003811 struct nvkm_i2c_bus *bus = NULL;
3812 struct nvkm_i2c_aux *aux = NULL;
3813 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003814 struct nouveau_encoder *nv_encoder;
3815 struct drm_encoder *encoder;
3816 int type;
3817
3818 switch (dcbe->type) {
3819 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003820 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
3821 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003822 type = DRM_MODE_ENCODER_TMDS;
3823 break;
3824 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003825 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003826 ddc = aux ? &nv_connector->aux.ddc : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003827 type = DRM_MODE_ENCODER_TMDS;
3828 break;
3829 default:
3830 return -ENODEV;
3831 }
3832
3833 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3834 if (!nv_encoder)
3835 return -ENOMEM;
3836 nv_encoder->dcb = dcbe;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003837 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003838 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003839
3840 encoder = to_drm_encoder(nv_encoder);
3841 encoder->possible_crtcs = dcbe->heads;
3842 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003843 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
3844 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003845 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003846
3847 drm_mode_connector_attach_encoder(connector, encoder);
3848 return 0;
3849}
3850
3851/******************************************************************************
Ben Skeggs839ca902016-11-04 17:20:36 +10003852 * Atomic
3853 *****************************************************************************/
3854
3855static void
3856nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
3857{
3858 struct nv50_disp *disp = nv50_disp(drm->dev);
3859 struct nv50_dmac *core = &disp->mast.base;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003860 struct nv50_mstm *mstm;
3861 struct drm_encoder *encoder;
Ben Skeggs839ca902016-11-04 17:20:36 +10003862 u32 *push;
3863
3864 NV_ATOMIC(drm, "commit core %08x\n", interlock);
3865
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003866 drm_for_each_encoder(encoder, drm->dev) {
3867 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3868 mstm = nouveau_encoder(encoder)->dp.mstm;
3869 if (mstm && mstm->modified)
3870 nv50_mstm_prepare(mstm);
3871 }
3872 }
3873
Ben Skeggs839ca902016-11-04 17:20:36 +10003874 if ((push = evo_wait(core, 5))) {
3875 evo_mthd(push, 0x0084, 1);
3876 evo_data(push, 0x80000000);
3877 evo_mthd(push, 0x0080, 2);
3878 evo_data(push, interlock);
3879 evo_data(push, 0x00000000);
3880 nouveau_bo_wr32(disp->sync, 0, 0x00000000);
3881 evo_kick(push, core);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003882 if (nvif_msec(&drm->client.device, 2000ULL,
Ben Skeggs839ca902016-11-04 17:20:36 +10003883 if (nouveau_bo_rd32(disp->sync, 0))
3884 break;
3885 usleep_range(1, 2);
3886 ) < 0)
3887 NV_ERROR(drm, "EVO timeout\n");
3888 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003889
3890 drm_for_each_encoder(encoder, drm->dev) {
3891 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3892 mstm = nouveau_encoder(encoder)->dp.mstm;
3893 if (mstm && mstm->modified)
3894 nv50_mstm_cleanup(mstm);
3895 }
3896 }
Ben Skeggs839ca902016-11-04 17:20:36 +10003897}
3898
3899static void
3900nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
3901{
3902 struct drm_device *dev = state->dev;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02003903 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10003904 struct drm_crtc *crtc;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02003905 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10003906 struct drm_plane *plane;
3907 struct nouveau_drm *drm = nouveau_drm(dev);
3908 struct nv50_disp *disp = nv50_disp(dev);
3909 struct nv50_atom *atom = nv50_atom(state);
3910 struct nv50_outp_atom *outp, *outt;
3911 u32 interlock_core = 0;
3912 u32 interlock_chan = 0;
3913 int i;
3914
3915 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
3916 drm_atomic_helper_wait_for_fences(dev, state, false);
3917 drm_atomic_helper_wait_for_dependencies(state);
3918 drm_atomic_helper_update_legacy_modeset_state(dev, state);
3919
3920 if (atom->lock_core)
3921 mutex_lock(&disp->mutex);
3922
3923 /* Disable head(s). */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02003924 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02003925 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10003926 struct nv50_head *head = nv50_head(crtc);
3927
3928 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
3929 asyh->clr.mask, asyh->set.mask);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02003930 if (old_crtc_state->active && !new_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10003931 drm_crtc_vblank_off(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10003932
3933 if (asyh->clr.mask) {
3934 nv50_head_flush_clr(head, asyh, atom->flush_disable);
3935 interlock_core |= 1;
3936 }
3937 }
3938
3939 /* Disable plane(s). */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02003940 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
3941 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10003942 struct nv50_wndw *wndw = nv50_wndw(plane);
3943
3944 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
3945 asyw->clr.mask, asyw->set.mask);
3946 if (!asyw->clr.mask)
3947 continue;
3948
3949 interlock_chan |= nv50_wndw_flush_clr(wndw, interlock_core,
3950 atom->flush_disable,
3951 asyw);
3952 }
3953
3954 /* Disable output path(s). */
3955 list_for_each_entry(outp, &atom->outp, head) {
3956 const struct drm_encoder_helper_funcs *help;
3957 struct drm_encoder *encoder;
3958
3959 encoder = outp->encoder;
3960 help = encoder->helper_private;
3961
3962 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
3963 outp->clr.mask, outp->set.mask);
3964
3965 if (outp->clr.mask) {
3966 help->disable(encoder);
3967 interlock_core |= 1;
3968 if (outp->flush_disable) {
3969 nv50_disp_atomic_commit_core(drm, interlock_chan);
3970 interlock_core = 0;
3971 interlock_chan = 0;
3972 }
3973 }
3974 }
3975
3976 /* Flush disable. */
3977 if (interlock_core) {
3978 if (atom->flush_disable) {
3979 nv50_disp_atomic_commit_core(drm, interlock_chan);
3980 interlock_core = 0;
3981 interlock_chan = 0;
3982 }
3983 }
3984
3985 /* Update output path(s). */
3986 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
3987 const struct drm_encoder_helper_funcs *help;
3988 struct drm_encoder *encoder;
3989
3990 encoder = outp->encoder;
3991 help = encoder->helper_private;
3992
3993 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
3994 outp->set.mask, outp->clr.mask);
3995
3996 if (outp->set.mask) {
3997 help->enable(encoder);
3998 interlock_core = 1;
3999 }
4000
4001 list_del(&outp->head);
4002 kfree(outp);
4003 }
4004
4005 /* Update head(s). */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02004006 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004007 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10004008 struct nv50_head *head = nv50_head(crtc);
4009
4010 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
4011 asyh->set.mask, asyh->clr.mask);
4012
4013 if (asyh->set.mask) {
4014 nv50_head_flush_set(head, asyh);
4015 interlock_core = 1;
4016 }
Ben Skeggs839ca902016-11-04 17:20:36 +10004017
Maarten Lankhorstefa47932017-08-15 10:52:50 +02004018 if (new_crtc_state->active) {
4019 if (!old_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10004020 drm_crtc_vblank_on(crtc);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02004021 if (new_crtc_state->event)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10004022 drm_crtc_vblank_get(crtc);
4023 }
Ben Skeggs2b507892017-01-24 09:32:26 +10004024 }
4025
Ben Skeggs839ca902016-11-04 17:20:36 +10004026 /* Update plane(s). */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004027 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
4028 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10004029 struct nv50_wndw *wndw = nv50_wndw(plane);
4030
4031 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
4032 asyw->set.mask, asyw->clr.mask);
4033 if ( !asyw->set.mask &&
4034 (!asyw->clr.mask || atom->flush_disable))
4035 continue;
4036
4037 interlock_chan |= nv50_wndw_flush_set(wndw, interlock_core, asyw);
4038 }
4039
4040 /* Flush update. */
4041 if (interlock_core) {
4042 if (!interlock_chan && atom->state.legacy_cursor_update) {
4043 u32 *push = evo_wait(&disp->mast, 2);
4044 if (push) {
4045 evo_mthd(push, 0x0080, 1);
4046 evo_data(push, 0x00000000);
4047 evo_kick(push, &disp->mast);
4048 }
4049 } else {
4050 nv50_disp_atomic_commit_core(drm, interlock_chan);
4051 }
4052 }
4053
4054 if (atom->lock_core)
4055 mutex_unlock(&disp->mutex);
4056
4057 /* Wait for HW to signal completion. */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004058 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
4059 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10004060 struct nv50_wndw *wndw = nv50_wndw(plane);
4061 int ret = nv50_wndw_wait_armed(wndw, asyw);
4062 if (ret)
4063 NV_ERROR(drm, "%s: timeout\n", plane->name);
4064 }
4065
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004066 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4067 if (new_crtc_state->event) {
Ben Skeggs839ca902016-11-04 17:20:36 +10004068 unsigned long flags;
Mario Kleinerbd9f6602016-11-23 07:58:54 +01004069 /* Get correct count/ts if racing with vblank irq */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02004070 if (new_crtc_state->active)
Dave Airlie0c697fa2017-08-15 16:16:58 +10004071 drm_crtc_accurate_vblank_count(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10004072 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004073 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Ben Skeggs839ca902016-11-04 17:20:36 +10004074 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02004075
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004076 new_crtc_state->event = NULL;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02004077 if (new_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10004078 drm_crtc_vblank_put(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10004079 }
4080 }
4081
4082 drm_atomic_helper_commit_hw_done(state);
4083 drm_atomic_helper_cleanup_planes(dev, state);
4084 drm_atomic_helper_commit_cleanup_done(state);
4085 drm_atomic_state_put(state);
4086}
4087
4088static void
4089nv50_disp_atomic_commit_work(struct work_struct *work)
4090{
4091 struct drm_atomic_state *state =
4092 container_of(work, typeof(*state), commit_work);
4093 nv50_disp_atomic_commit_tail(state);
4094}
4095
4096static int
4097nv50_disp_atomic_commit(struct drm_device *dev,
4098 struct drm_atomic_state *state, bool nonblock)
4099{
4100 struct nouveau_drm *drm = nouveau_drm(dev);
4101 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggsd324c5b2017-11-01 09:12:25 +10004102 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10004103 struct drm_plane *plane;
4104 struct drm_crtc *crtc;
4105 bool active = false;
4106 int ret, i;
4107
4108 ret = pm_runtime_get_sync(dev->dev);
4109 if (ret < 0 && ret != -EACCES)
4110 return ret;
4111
4112 ret = drm_atomic_helper_setup_commit(state, nonblock);
4113 if (ret)
4114 goto done;
4115
4116 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
4117
4118 ret = drm_atomic_helper_prepare_planes(dev, state);
4119 if (ret)
4120 goto done;
4121
4122 if (!nonblock) {
4123 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
4124 if (ret)
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02004125 goto err_cleanup;
Ben Skeggs839ca902016-11-04 17:20:36 +10004126 }
4127
Maarten Lankhorst85726362017-07-11 16:33:05 +02004128 ret = drm_atomic_helper_swap_state(state, true);
4129 if (ret)
4130 goto err_cleanup;
4131
Ben Skeggsd324c5b2017-11-01 09:12:25 +10004132 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
4133 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10004134 struct nv50_wndw *wndw = nv50_wndw(plane);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004135
Ben Skeggs839ca902016-11-04 17:20:36 +10004136 if (asyw->set.image) {
4137 asyw->ntfy.handle = wndw->dmac->sync.handle;
4138 asyw->ntfy.offset = wndw->ntfy;
4139 asyw->ntfy.awaken = false;
4140 asyw->set.ntfy = true;
4141 nouveau_bo_wr32(disp->sync, wndw->ntfy / 4, 0x00000000);
4142 wndw->ntfy ^= 0x10;
4143 }
4144 }
4145
Ben Skeggs839ca902016-11-04 17:20:36 +10004146 drm_atomic_state_get(state);
4147
4148 if (nonblock)
4149 queue_work(system_unbound_wq, &state->commit_work);
4150 else
4151 nv50_disp_atomic_commit_tail(state);
4152
4153 drm_for_each_crtc(crtc, dev) {
4154 if (crtc->state->enable) {
4155 if (!drm->have_disp_power_ref) {
4156 drm->have_disp_power_ref = true;
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02004157 return 0;
Ben Skeggs839ca902016-11-04 17:20:36 +10004158 }
4159 active = true;
4160 break;
4161 }
4162 }
4163
4164 if (!active && drm->have_disp_power_ref) {
4165 pm_runtime_put_autosuspend(dev->dev);
4166 drm->have_disp_power_ref = false;
4167 }
4168
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02004169err_cleanup:
4170 if (ret)
4171 drm_atomic_helper_cleanup_planes(dev, state);
Ben Skeggs839ca902016-11-04 17:20:36 +10004172done:
4173 pm_runtime_put_autosuspend(dev->dev);
4174 return ret;
4175}
4176
4177static struct nv50_outp_atom *
4178nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
4179{
4180 struct nv50_outp_atom *outp;
4181
4182 list_for_each_entry(outp, &atom->outp, head) {
4183 if (outp->encoder == encoder)
4184 return outp;
4185 }
4186
4187 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
4188 if (!outp)
4189 return ERR_PTR(-ENOMEM);
4190
4191 list_add(&outp->head, &atom->outp);
4192 outp->encoder = encoder;
4193 return outp;
4194}
4195
4196static int
4197nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004198 struct drm_connector_state *old_connector_state)
Ben Skeggs839ca902016-11-04 17:20:36 +10004199{
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004200 struct drm_encoder *encoder = old_connector_state->best_encoder;
4201 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10004202 struct drm_crtc *crtc;
4203 struct nv50_outp_atom *outp;
4204
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004205 if (!(crtc = old_connector_state->crtc))
Ben Skeggs839ca902016-11-04 17:20:36 +10004206 return 0;
4207
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004208 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
4209 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
4210 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10004211 outp = nv50_disp_outp_atomic_add(atom, encoder);
4212 if (IS_ERR(outp))
4213 return PTR_ERR(outp);
4214
4215 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
4216 outp->flush_disable = true;
4217 atom->flush_disable = true;
4218 }
4219 outp->clr.ctrl = true;
4220 atom->lock_core = true;
4221 }
4222
4223 return 0;
4224}
4225
4226static int
4227nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
4228 struct drm_connector_state *connector_state)
4229{
4230 struct drm_encoder *encoder = connector_state->best_encoder;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004231 struct drm_crtc_state *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10004232 struct drm_crtc *crtc;
4233 struct nv50_outp_atom *outp;
4234
4235 if (!(crtc = connector_state->crtc))
4236 return 0;
4237
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004238 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
4239 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10004240 outp = nv50_disp_outp_atomic_add(atom, encoder);
4241 if (IS_ERR(outp))
4242 return PTR_ERR(outp);
4243
4244 outp->set.ctrl = true;
4245 atom->lock_core = true;
4246 }
4247
4248 return 0;
4249}
4250
4251static int
4252nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
4253{
4254 struct nv50_atom *atom = nv50_atom(state);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004255 struct drm_connector_state *old_connector_state, *new_connector_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10004256 struct drm_connector *connector;
4257 int ret, i;
4258
4259 ret = drm_atomic_helper_check(dev, state);
4260 if (ret)
4261 return ret;
4262
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004263 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
4264 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10004265 if (ret)
4266 return ret;
4267
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02004268 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10004269 if (ret)
4270 return ret;
4271 }
4272
4273 return 0;
4274}
4275
4276static void
4277nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
4278{
4279 struct nv50_atom *atom = nv50_atom(state);
4280 struct nv50_outp_atom *outp, *outt;
4281
4282 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
4283 list_del(&outp->head);
4284 kfree(outp);
4285 }
4286
4287 drm_atomic_state_default_clear(state);
4288}
4289
4290static void
4291nv50_disp_atomic_state_free(struct drm_atomic_state *state)
4292{
4293 struct nv50_atom *atom = nv50_atom(state);
4294 drm_atomic_state_default_release(&atom->state);
4295 kfree(atom);
4296}
4297
4298static struct drm_atomic_state *
4299nv50_disp_atomic_state_alloc(struct drm_device *dev)
4300{
4301 struct nv50_atom *atom;
4302 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
4303 drm_atomic_state_init(dev, &atom->state) < 0) {
4304 kfree(atom);
4305 return NULL;
4306 }
4307 INIT_LIST_HEAD(&atom->outp);
4308 return &atom->state;
4309}
4310
4311static const struct drm_mode_config_funcs
4312nv50_disp_func = {
4313 .fb_create = nouveau_user_framebuffer_create,
Noralf Trønnesd0f54f52017-12-05 19:25:00 +01004314 .output_poll_changed = drm_fb_helper_output_poll_changed,
Ben Skeggs839ca902016-11-04 17:20:36 +10004315 .atomic_check = nv50_disp_atomic_check,
4316 .atomic_commit = nv50_disp_atomic_commit,
4317 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
4318 .atomic_state_clear = nv50_disp_atomic_state_clear,
4319 .atomic_state_free = nv50_disp_atomic_state_free,
4320};
4321
4322/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10004323 * Init
4324 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10004325
Ben Skeggs2a44e492011-11-09 11:36:33 +10004326void
Ben Skeggse225f442012-11-21 14:40:21 +10004327nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004328{
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004329 struct nouveau_encoder *nv_encoder;
4330 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10004331 struct drm_plane *plane;
4332
4333 drm_for_each_plane(plane, dev) {
4334 struct nv50_wndw *wndw = nv50_wndw(plane);
4335 if (plane->funcs != &nv50_wndw)
4336 continue;
4337 nv50_wndw_fini(wndw);
4338 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004339
4340 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4341 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
4342 nv_encoder = nouveau_encoder(encoder);
4343 nv50_mstm_fini(nv_encoder->dp.mstm);
4344 }
4345 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10004346}
4347
4348int
Ben Skeggse225f442012-11-21 14:40:21 +10004349nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004350{
Ben Skeggs354d3502016-11-04 17:20:36 +10004351 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10004352 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004353 struct drm_crtc *crtc;
4354 u32 *push;
4355
4356 push = evo_wait(nv50_mast(dev), 32);
4357 if (!push)
4358 return -EBUSY;
4359
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004360 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10004361 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004362 evo_kick(push, nv50_mast(dev));
Ben Skeggs973f10c2016-11-04 17:20:36 +10004363
Ben Skeggs354d3502016-11-04 17:20:36 +10004364 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4365 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
Ben Skeggs9c5753b2017-05-19 23:59:35 +10004366 struct nouveau_encoder *nv_encoder =
4367 nouveau_encoder(encoder);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004368 nv50_mstm_init(nv_encoder->dp.mstm);
Ben Skeggs354d3502016-11-04 17:20:36 +10004369 }
4370 }
4371
Ben Skeggse1ef6b42016-11-04 17:20:36 +10004372 drm_for_each_crtc(crtc, dev) {
Ben Skeggs9bfdee92016-11-04 17:20:36 +10004373 nv50_head_lut_load(crtc);
Ben Skeggse1ef6b42016-11-04 17:20:36 +10004374 }
4375
Ben Skeggs973f10c2016-11-04 17:20:36 +10004376 drm_for_each_plane(plane, dev) {
4377 struct nv50_wndw *wndw = nv50_wndw(plane);
4378 if (plane->funcs != &nv50_wndw)
4379 continue;
4380 nv50_wndw_init(wndw);
4381 }
4382
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004383 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004384}
4385
4386void
Ben Skeggse225f442012-11-21 14:40:21 +10004387nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004388{
Ben Skeggse225f442012-11-21 14:40:21 +10004389 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004390
Ben Skeggs0ad72862014-08-10 04:10:22 +10004391 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10004392
Ben Skeggs816af2f2011-11-16 15:48:48 +10004393 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004394 if (disp->sync)
4395 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10004396 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10004397
Ben Skeggs77145f12012-07-31 16:16:21 +10004398 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004399 kfree(disp);
4400}
4401
Ben Skeggs839ca902016-11-04 17:20:36 +10004402MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
4403static int nouveau_atomic = 0;
4404module_param_named(atomic, nouveau_atomic, int, 0400);
4405
Ben Skeggs26f6d882011-07-04 16:25:18 +10004406int
Ben Skeggse225f442012-11-21 14:40:21 +10004407nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004408{
Ben Skeggs1167c6b2016-05-18 13:57:42 +10004409 struct nvif_device *device = &nouveau_drm(dev)->client.device;
Ben Skeggs77145f12012-07-31 16:16:21 +10004410 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10004411 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004412 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10004413 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10004414 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10004415 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004416
4417 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
4418 if (!disp)
4419 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10004420
Ben Skeggs839ca902016-11-04 17:20:36 +10004421 mutex_init(&disp->mutex);
4422
Ben Skeggs77145f12012-07-31 16:16:21 +10004423 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10004424 nouveau_display(dev)->dtor = nv50_display_destroy;
4425 nouveau_display(dev)->init = nv50_display_init;
4426 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs0ad72862014-08-10 04:10:22 +10004427 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs839ca902016-11-04 17:20:36 +10004428 dev->mode_config.funcs = &nv50_disp_func;
4429 if (nouveau_atomic)
4430 dev->driver->driver_features |= DRIVER_ATOMIC;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004431
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004432 /* small shared memory area we use for notifiers and semaphores */
Ben Skeggsbab7cc12016-05-24 17:26:48 +10004433 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01004434 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004435 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10004436 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004437 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004438 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004439 if (ret)
4440 nouveau_bo_unpin(disp->sync);
4441 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004442 if (ret)
4443 nouveau_bo_ref(NULL, &disp->sync);
4444 }
4445
4446 if (ret)
4447 goto out;
4448
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004449 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10004450 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10004451 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004452 if (ret)
4453 goto out;
4454
Ben Skeggs438d99e2011-07-05 16:48:06 +10004455 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10004456 if (disp->disp->oclass >= GF110_DISP)
Ilia Mirkineba5e562017-07-03 13:06:26 -04004457 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
Ben Skeggs63718a02012-11-16 11:44:14 +10004458 else
Ilia Mirkineba5e562017-07-03 13:06:26 -04004459 crtcs = 0x3;
Ben Skeggs63718a02012-11-16 11:44:14 +10004460
Ilia Mirkineba5e562017-07-03 13:06:26 -04004461 for (i = 0; i < fls(crtcs); i++) {
4462 if (!(crtcs & (1 << i)))
4463 continue;
Ben Skeggs9bfdee92016-11-04 17:20:36 +10004464 ret = nv50_head_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10004465 if (ret)
4466 goto out;
4467 }
4468
Ben Skeggs83fc0832011-07-05 13:08:40 +10004469 /* create encoder/connector objects based on VBIOS DCB table */
4470 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
4471 connector = nouveau_connector_create(dev, dcbe->connector);
4472 if (IS_ERR(connector))
4473 continue;
4474
Ben Skeggseb6313a2013-02-11 09:52:58 +10004475 if (dcbe->location == DCB_LOC_ON_CHIP) {
4476 switch (dcbe->type) {
4477 case DCB_OUTPUT_TMDS:
4478 case DCB_OUTPUT_LVDS:
4479 case DCB_OUTPUT_DP:
4480 ret = nv50_sor_create(connector, dcbe);
4481 break;
4482 case DCB_OUTPUT_ANALOG:
4483 ret = nv50_dac_create(connector, dcbe);
4484 break;
4485 default:
4486 ret = -ENODEV;
4487 break;
4488 }
4489 } else {
4490 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004491 }
4492
Ben Skeggseb6313a2013-02-11 09:52:58 +10004493 if (ret) {
4494 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
4495 dcbe->location, dcbe->type,
4496 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10004497 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004498 }
4499 }
4500
4501 /* cull any connectors we created that don't have an encoder */
4502 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
4503 if (connector->encoder_ids[0])
4504 continue;
4505
Ben Skeggs77145f12012-07-31 16:16:21 +10004506 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03004507 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004508 connector->funcs->destroy(connector);
4509 }
4510
Ben Skeggs26f6d882011-07-04 16:25:18 +10004511out:
4512 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10004513 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004514 return ret;
4515}