blob: e6cc1d843c92da0649af52a455dda09f786bb646 [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)
321 nvif_object_map(&chan->user);
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 Skeggsf00f0e22016-11-04 17:20:36 +1000427 const u8 kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
428 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);
513
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000514 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
515 &dmac->handle, GFP_KERNEL);
Ben Skeggs47057302012-11-16 13:58:48 +1000516 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000517 return -ENOMEM;
518
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000519 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
520 &(struct nv_dma_v0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +1000521 .target = NV_DMA_V0_TARGET_PCI_US,
522 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000523 .start = dmac->handle + 0x0000,
524 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000525 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000526 if (ret)
527 return ret;
528
Ben Skeggsbf81df92015-08-20 14:54:16 +1000529 args->pushbuf = nvif_handle(&pushbuf);
530
Ben Skeggsa01ca782015-08-20 14:54:15 +1000531 ret = nv50_chan_create(device, disp, oclass, head, data, size,
532 &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000533 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000534 if (ret)
535 return ret;
536
Ben Skeggsa01ca782015-08-20 14:54:15 +1000537 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000538 &(struct nv_dma_v0) {
539 .target = NV_DMA_V0_TARGET_VRAM,
540 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000541 .start = syncbuf + 0x0000,
542 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000543 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000544 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000545 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000546 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000547
Ben Skeggsa01ca782015-08-20 14:54:15 +1000548 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000549 &(struct nv_dma_v0) {
550 .target = NV_DMA_V0_TARGET_VRAM,
551 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000552 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000553 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000554 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000555 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000556 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000557 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000558
Ben Skeggsaccdea22016-11-04 17:20:36 +1000559 INIT_LIST_HEAD(&dmac->ctxdma);
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;
850 asyw->image.kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
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 Skeggs973f10c2016-11-04 17:20:36 +1000860 asyw->image.block = fb->nvbo->tile_mode >> 4;
861 else
862 asyw->image.block = fb->nvbo->tile_mode;
863 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,
1058 .set_property = drm_atomic_helper_plane_set_property,
1059 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
1060 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
1061};
1062
1063static void
1064nv50_wndw_fini(struct nv50_wndw *wndw)
1065{
1066 nvif_notify_put(&wndw->notify);
1067}
1068
1069static void
1070nv50_wndw_init(struct nv50_wndw *wndw)
1071{
1072 nvif_notify_get(&wndw->notify);
1073}
1074
1075static int
1076nv50_wndw_ctor(const struct nv50_wndw_func *func, struct drm_device *dev,
1077 enum drm_plane_type type, const char *name, int index,
1078 struct nv50_dmac *dmac, const u32 *format, int nformat,
1079 struct nv50_wndw *wndw)
1080{
1081 int ret;
1082
1083 wndw->func = func;
1084 wndw->dmac = dmac;
1085
1086 ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw, format,
1087 nformat, type, "%s-%d", name, index);
1088 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
1146 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1147 DRM_PLANE_HELPER_NO_SCALING,
1148 DRM_PLANE_HELPER_NO_SCALING,
1149 true, true);
1150 asyh->curs.visible = asyw->state.visible;
1151 if (ret || !asyh->curs.visible)
1152 return ret;
1153
1154 switch (asyw->state.fb->width) {
1155 case 32: asyh->curs.layout = 0; break;
1156 case 64: asyh->curs.layout = 1; break;
1157 default:
1158 return -EINVAL;
1159 }
1160
1161 if (asyw->state.fb->width != asyw->state.fb->height)
1162 return -EINVAL;
1163
Ville Syrjälä438b74a2016-12-14 23:32:55 +02001164 switch (asyw->state.fb->format->format) {
Ben Skeggs22e927d2016-11-04 17:20:36 +10001165 case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
1166 default:
1167 WARN_ON(1);
1168 return -EINVAL;
1169 }
1170
1171 return 0;
1172}
1173
1174static void *
1175nv50_curs_dtor(struct nv50_wndw *wndw)
1176{
1177 struct nv50_curs *curs = nv50_curs(wndw);
1178 nvif_object_fini(&curs->chan);
1179 return curs;
1180}
1181
1182static const u32
1183nv50_curs_format[] = {
1184 DRM_FORMAT_ARGB8888,
1185};
1186
1187static const struct nv50_wndw_func
1188nv50_curs = {
1189 .dtor = nv50_curs_dtor,
1190 .acquire = nv50_curs_acquire,
1191 .release = nv50_curs_release,
1192 .prepare = nv50_curs_prepare,
1193 .point = nv50_curs_point,
1194 .update = nv50_curs_update,
1195};
1196
1197static int
1198nv50_curs_new(struct nouveau_drm *drm, struct nv50_head *head,
1199 struct nv50_curs **pcurs)
1200{
1201 static const struct nvif_mclass curses[] = {
1202 { GK104_DISP_CURSOR, 0 },
1203 { GF110_DISP_CURSOR, 0 },
1204 { GT214_DISP_CURSOR, 0 },
1205 { G82_DISP_CURSOR, 0 },
1206 { NV50_DISP_CURSOR, 0 },
1207 {}
1208 };
1209 struct nv50_disp_cursor_v0 args = {
1210 .head = head->base.index,
1211 };
1212 struct nv50_disp *disp = nv50_disp(drm->dev);
1213 struct nv50_curs *curs;
1214 int cid, ret;
1215
1216 cid = nvif_mclass(disp->disp, curses);
1217 if (cid < 0) {
1218 NV_ERROR(drm, "No supported cursor immediate class\n");
1219 return cid;
1220 }
1221
1222 if (!(curs = *pcurs = kzalloc(sizeof(*curs), GFP_KERNEL)))
1223 return -ENOMEM;
1224
1225 ret = nv50_wndw_ctor(&nv50_curs, drm->dev, DRM_PLANE_TYPE_CURSOR,
1226 "curs", head->base.index, &disp->mast.base,
1227 nv50_curs_format, ARRAY_SIZE(nv50_curs_format),
1228 &curs->wndw);
1229 if (ret) {
1230 kfree(curs);
1231 return ret;
1232 }
1233
1234 ret = nvif_object_init(disp->disp, 0, curses[cid].oclass, &args,
1235 sizeof(args), &curs->chan);
1236 if (ret) {
1237 NV_ERROR(drm, "curs%04x allocation failed: %d\n",
1238 curses[cid].oclass, ret);
1239 return ret;
1240 }
1241
1242 return 0;
1243}
1244
1245/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +10001246 * Primary plane
1247 *****************************************************************************/
1248#define nv50_base(p) container_of((p), struct nv50_base, wndw)
1249
1250struct nv50_base {
1251 struct nv50_wndw wndw;
1252 struct nv50_sync chan;
1253 int id;
1254};
1255
1256static int
1257nv50_base_notify(struct nvif_notify *notify)
1258{
1259 return NVIF_NOTIFY_KEEP;
1260}
1261
1262static void
1263nv50_base_lut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1264{
1265 struct nv50_base *base = nv50_base(wndw);
1266 u32 *push;
1267 if ((push = evo_wait(&base->chan, 2))) {
1268 evo_mthd(push, 0x00e0, 1);
1269 evo_data(push, asyw->lut.enable << 30);
1270 evo_kick(push, &base->chan);
1271 }
1272}
1273
1274static void
1275nv50_base_image_clr(struct nv50_wndw *wndw)
1276{
1277 struct nv50_base *base = nv50_base(wndw);
1278 u32 *push;
1279 if ((push = evo_wait(&base->chan, 4))) {
1280 evo_mthd(push, 0x0084, 1);
1281 evo_data(push, 0x00000000);
1282 evo_mthd(push, 0x00c0, 1);
1283 evo_data(push, 0x00000000);
1284 evo_kick(push, &base->chan);
1285 }
1286}
1287
1288static void
1289nv50_base_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1290{
1291 struct nv50_base *base = nv50_base(wndw);
1292 const s32 oclass = base->chan.base.base.user.oclass;
1293 u32 *push;
1294 if ((push = evo_wait(&base->chan, 10))) {
1295 evo_mthd(push, 0x0084, 1);
1296 evo_data(push, (asyw->image.mode << 8) |
1297 (asyw->image.interval << 4));
1298 evo_mthd(push, 0x00c0, 1);
1299 evo_data(push, asyw->image.handle);
1300 if (oclass < G82_DISP_BASE_CHANNEL_DMA) {
1301 evo_mthd(push, 0x0800, 5);
1302 evo_data(push, asyw->image.offset >> 8);
1303 evo_data(push, 0x00000000);
1304 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1305 evo_data(push, (asyw->image.layout << 20) |
1306 asyw->image.pitch |
1307 asyw->image.block);
1308 evo_data(push, (asyw->image.kind << 16) |
1309 (asyw->image.format << 8));
1310 } else
1311 if (oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1312 evo_mthd(push, 0x0800, 5);
1313 evo_data(push, asyw->image.offset >> 8);
1314 evo_data(push, 0x00000000);
1315 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1316 evo_data(push, (asyw->image.layout << 20) |
1317 asyw->image.pitch |
1318 asyw->image.block);
1319 evo_data(push, asyw->image.format << 8);
1320 } else {
1321 evo_mthd(push, 0x0400, 5);
1322 evo_data(push, asyw->image.offset >> 8);
1323 evo_data(push, 0x00000000);
1324 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1325 evo_data(push, (asyw->image.layout << 24) |
1326 asyw->image.pitch |
1327 asyw->image.block);
1328 evo_data(push, asyw->image.format << 8);
1329 }
1330 evo_kick(push, &base->chan);
1331 }
1332}
1333
1334static void
1335nv50_base_ntfy_clr(struct nv50_wndw *wndw)
1336{
1337 struct nv50_base *base = nv50_base(wndw);
1338 u32 *push;
1339 if ((push = evo_wait(&base->chan, 2))) {
1340 evo_mthd(push, 0x00a4, 1);
1341 evo_data(push, 0x00000000);
1342 evo_kick(push, &base->chan);
1343 }
1344}
1345
1346static void
1347nv50_base_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1348{
1349 struct nv50_base *base = nv50_base(wndw);
1350 u32 *push;
1351 if ((push = evo_wait(&base->chan, 3))) {
1352 evo_mthd(push, 0x00a0, 2);
1353 evo_data(push, (asyw->ntfy.awaken << 30) | asyw->ntfy.offset);
1354 evo_data(push, asyw->ntfy.handle);
1355 evo_kick(push, &base->chan);
1356 }
1357}
1358
1359static void
1360nv50_base_sema_clr(struct nv50_wndw *wndw)
1361{
1362 struct nv50_base *base = nv50_base(wndw);
1363 u32 *push;
1364 if ((push = evo_wait(&base->chan, 2))) {
1365 evo_mthd(push, 0x0094, 1);
1366 evo_data(push, 0x00000000);
1367 evo_kick(push, &base->chan);
1368 }
1369}
1370
1371static void
1372nv50_base_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1373{
1374 struct nv50_base *base = nv50_base(wndw);
1375 u32 *push;
1376 if ((push = evo_wait(&base->chan, 5))) {
1377 evo_mthd(push, 0x0088, 4);
1378 evo_data(push, asyw->sema.offset);
1379 evo_data(push, asyw->sema.acquire);
1380 evo_data(push, asyw->sema.release);
1381 evo_data(push, asyw->sema.handle);
1382 evo_kick(push, &base->chan);
1383 }
1384}
1385
1386static u32
1387nv50_base_update(struct nv50_wndw *wndw, u32 interlock)
1388{
1389 struct nv50_base *base = nv50_base(wndw);
1390 u32 *push;
1391
1392 if (!(push = evo_wait(&base->chan, 2)))
1393 return 0;
1394 evo_mthd(push, 0x0080, 1);
1395 evo_data(push, interlock);
1396 evo_kick(push, &base->chan);
1397
1398 if (base->chan.base.base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA)
1399 return interlock ? 2 << (base->id * 8) : 0;
1400 return interlock ? 2 << (base->id * 4) : 0;
1401}
1402
1403static int
1404nv50_base_ntfy_wait_begun(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1405{
1406 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
1407 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001408 if (nvif_msec(&drm->client.device, 2000ULL,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001409 u32 data = nouveau_bo_rd32(disp->sync, asyw->ntfy.offset / 4);
1410 if ((data & 0xc0000000) == 0x40000000)
1411 break;
1412 usleep_range(1, 2);
1413 ) < 0)
1414 return -ETIMEDOUT;
1415 return 0;
1416}
1417
1418static void
1419nv50_base_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1420 struct nv50_head_atom *asyh)
1421{
1422 asyh->base.cpp = 0;
1423}
1424
1425static int
1426nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1427 struct nv50_head_atom *asyh)
1428{
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001429 const struct drm_framebuffer *fb = asyw->state.fb;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001430 int ret;
1431
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001432 if (!fb->format->depth)
Ben Skeggs973f10c2016-11-04 17:20:36 +10001433 return -EINVAL;
1434
1435 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1436 DRM_PLANE_HELPER_NO_SCALING,
1437 DRM_PLANE_HELPER_NO_SCALING,
1438 false, true);
1439 if (ret)
1440 return ret;
1441
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001442 asyh->base.depth = fb->format->depth;
1443 asyh->base.cpp = fb->format->cpp[0];
Ben Skeggs973f10c2016-11-04 17:20:36 +10001444 asyh->base.x = asyw->state.src.x1 >> 16;
1445 asyh->base.y = asyw->state.src.y1 >> 16;
1446 asyh->base.w = asyw->state.fb->width;
1447 asyh->base.h = asyw->state.fb->height;
1448
Ville Syrjälä438b74a2016-12-14 23:32:55 +02001449 switch (fb->format->format) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001450 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
1451 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
1452 case DRM_FORMAT_XRGB1555 :
1453 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
1454 case DRM_FORMAT_XRGB8888 :
1455 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
1456 case DRM_FORMAT_XBGR2101010:
1457 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
1458 case DRM_FORMAT_XBGR8888 :
1459 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
1460 default:
1461 WARN_ON(1);
1462 return -EINVAL;
1463 }
1464
1465 asyw->lut.enable = 1;
1466 asyw->set.image = true;
1467 return 0;
1468}
1469
1470static void *
1471nv50_base_dtor(struct nv50_wndw *wndw)
1472{
1473 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1474 struct nv50_base *base = nv50_base(wndw);
1475 nv50_dmac_destroy(&base->chan.base, disp->disp);
1476 return base;
1477}
1478
1479static const u32
1480nv50_base_format[] = {
1481 DRM_FORMAT_C8,
1482 DRM_FORMAT_RGB565,
1483 DRM_FORMAT_XRGB1555,
1484 DRM_FORMAT_ARGB1555,
1485 DRM_FORMAT_XRGB8888,
1486 DRM_FORMAT_ARGB8888,
1487 DRM_FORMAT_XBGR2101010,
1488 DRM_FORMAT_ABGR2101010,
1489 DRM_FORMAT_XBGR8888,
1490 DRM_FORMAT_ABGR8888,
1491};
1492
1493static const struct nv50_wndw_func
1494nv50_base = {
1495 .dtor = nv50_base_dtor,
1496 .acquire = nv50_base_acquire,
1497 .release = nv50_base_release,
1498 .sema_set = nv50_base_sema_set,
1499 .sema_clr = nv50_base_sema_clr,
1500 .ntfy_set = nv50_base_ntfy_set,
1501 .ntfy_clr = nv50_base_ntfy_clr,
1502 .ntfy_wait_begun = nv50_base_ntfy_wait_begun,
1503 .image_set = nv50_base_image_set,
1504 .image_clr = nv50_base_image_clr,
1505 .lut = nv50_base_lut,
1506 .update = nv50_base_update,
1507};
1508
1509static int
1510nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
1511 struct nv50_base **pbase)
1512{
1513 struct nv50_disp *disp = nv50_disp(drm->dev);
1514 struct nv50_base *base;
1515 int ret;
1516
1517 if (!(base = *pbase = kzalloc(sizeof(*base), GFP_KERNEL)))
1518 return -ENOMEM;
1519 base->id = head->base.index;
1520 base->wndw.ntfy = EVO_FLIP_NTFY0(base->id);
1521 base->wndw.sema = EVO_FLIP_SEM0(base->id);
1522 base->wndw.data = 0x00000000;
1523
1524 ret = nv50_wndw_ctor(&nv50_base, drm->dev, DRM_PLANE_TYPE_PRIMARY,
1525 "base", base->id, &base->chan.base,
1526 nv50_base_format, ARRAY_SIZE(nv50_base_format),
1527 &base->wndw);
1528 if (ret) {
1529 kfree(base);
1530 return ret;
1531 }
1532
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001533 ret = nv50_base_create(&drm->client.device, disp->disp, base->id,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001534 disp->sync->bo.offset, &base->chan);
1535 if (ret)
1536 return ret;
1537
1538 return nvif_notify_init(&base->chan.base.base.user, nv50_base_notify,
1539 false,
1540 NV50_DISP_BASE_CHANNEL_DMA_V0_NTFY_UEVENT,
1541 &(struct nvif_notify_uevent_req) {},
1542 sizeof(struct nvif_notify_uevent_req),
1543 sizeof(struct nvif_notify_uevent_rep),
1544 &base->wndw.notify);
1545}
1546
1547/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001548 * Head
1549 *****************************************************************************/
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001550static void
Ben Skeggs7e08d672016-11-04 17:20:36 +10001551nv50_head_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
1552{
1553 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1554 u32 *push;
1555 if ((push = evo_wait(core, 2))) {
1556 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1557 evo_mthd(push, 0x08a8 + (head->base.index * 0x400), 1);
1558 else
1559 evo_mthd(push, 0x0498 + (head->base.index * 0x300), 1);
1560 evo_data(push, (asyh->procamp.sat.sin << 20) |
1561 (asyh->procamp.sat.cos << 8));
1562 evo_kick(push, core);
1563 }
1564}
1565
1566static void
Ben Skeggs7e918332016-11-04 17:20:36 +10001567nv50_head_dither(struct nv50_head *head, struct nv50_head_atom *asyh)
1568{
1569 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1570 u32 *push;
1571 if ((push = evo_wait(core, 2))) {
1572 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1573 evo_mthd(push, 0x08a0 + (head->base.index * 0x0400), 1);
1574 else
1575 if (core->base.user.oclass < GK104_DISP_CORE_CHANNEL_DMA)
1576 evo_mthd(push, 0x0490 + (head->base.index * 0x0300), 1);
1577 else
1578 evo_mthd(push, 0x04a0 + (head->base.index * 0x0300), 1);
1579 evo_data(push, (asyh->dither.mode << 3) |
1580 (asyh->dither.bits << 1) |
1581 asyh->dither.enable);
1582 evo_kick(push, core);
1583 }
1584}
1585
1586static void
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001587nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
1588{
1589 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1590 u32 bounds = 0;
1591 u32 *push;
1592
1593 if (asyh->base.cpp) {
1594 switch (asyh->base.cpp) {
1595 case 8: bounds |= 0x00000500; break;
1596 case 4: bounds |= 0x00000300; break;
1597 case 2: bounds |= 0x00000100; break;
1598 default:
1599 WARN_ON(1);
1600 break;
1601 }
1602 bounds |= 0x00000001;
1603 }
1604
1605 if ((push = evo_wait(core, 2))) {
1606 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1607 evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
1608 else
1609 evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
1610 evo_data(push, bounds);
1611 evo_kick(push, core);
1612 }
1613}
1614
1615static void
1616nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
1617{
1618 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1619 u32 bounds = 0;
1620 u32 *push;
1621
1622 if (asyh->base.cpp) {
1623 switch (asyh->base.cpp) {
1624 case 8: bounds |= 0x00000500; break;
1625 case 4: bounds |= 0x00000300; break;
1626 case 2: bounds |= 0x00000100; break;
1627 case 1: bounds |= 0x00000000; break;
1628 default:
1629 WARN_ON(1);
1630 break;
1631 }
1632 bounds |= 0x00000001;
1633 }
1634
1635 if ((push = evo_wait(core, 2))) {
1636 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1637 evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
1638 else
1639 evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
1640 evo_data(push, bounds);
1641 evo_kick(push, core);
1642 }
1643}
1644
1645static void
Ben Skeggsea8ee392016-11-04 17:20:36 +10001646nv50_head_curs_clr(struct nv50_head *head)
1647{
1648 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1649 u32 *push;
1650 if ((push = evo_wait(core, 4))) {
1651 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1652 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1653 evo_data(push, 0x05000000);
1654 } else
1655 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1656 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1657 evo_data(push, 0x05000000);
1658 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1659 evo_data(push, 0x00000000);
1660 } else {
1661 evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
1662 evo_data(push, 0x05000000);
1663 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1664 evo_data(push, 0x00000000);
1665 }
1666 evo_kick(push, core);
1667 }
1668}
1669
1670static void
1671nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1672{
1673 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1674 u32 *push;
1675 if ((push = evo_wait(core, 5))) {
1676 if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
1677 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1678 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1679 (asyh->curs.format << 24));
1680 evo_data(push, asyh->curs.offset >> 8);
1681 } else
1682 if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1683 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1684 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1685 (asyh->curs.format << 24));
1686 evo_data(push, asyh->curs.offset >> 8);
1687 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1688 evo_data(push, asyh->curs.handle);
1689 } else {
1690 evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
1691 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1692 (asyh->curs.format << 24));
1693 evo_data(push, asyh->curs.offset >> 8);
1694 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1695 evo_data(push, asyh->curs.handle);
1696 }
1697 evo_kick(push, core);
1698 }
1699}
1700
1701static void
Ben Skeggsad633612016-11-04 17:20:36 +10001702nv50_head_core_clr(struct nv50_head *head)
1703{
1704 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1705 u32 *push;
1706 if ((push = evo_wait(core, 2))) {
1707 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1708 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
1709 else
1710 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
1711 evo_data(push, 0x00000000);
1712 evo_kick(push, core);
1713 }
1714}
1715
1716static void
1717nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1718{
1719 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1720 u32 *push;
1721 if ((push = evo_wait(core, 9))) {
1722 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1723 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1724 evo_data(push, asyh->core.offset >> 8);
1725 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1726 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1727 evo_data(push, asyh->core.layout << 20 |
1728 (asyh->core.pitch >> 8) << 8 |
1729 asyh->core.block);
1730 evo_data(push, asyh->core.kind << 16 |
1731 asyh->core.format << 8);
1732 evo_data(push, asyh->core.handle);
1733 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1734 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
Ben Skeggs19d53d02016-12-13 11:18:46 +10001735 /* EVO will complain with INVALID_STATE if we have an
1736 * active cursor and (re)specify HeadSetContextDmaIso
1737 * without also updating HeadSetOffsetCursor.
1738 */
1739 asyh->set.curs = asyh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10001740 } else
1741 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1742 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1743 evo_data(push, asyh->core.offset >> 8);
1744 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1745 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1746 evo_data(push, asyh->core.layout << 20 |
1747 (asyh->core.pitch >> 8) << 8 |
1748 asyh->core.block);
1749 evo_data(push, asyh->core.format << 8);
1750 evo_data(push, asyh->core.handle);
1751 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1752 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1753 } else {
1754 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
1755 evo_data(push, asyh->core.offset >> 8);
1756 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
1757 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1758 evo_data(push, asyh->core.layout << 24 |
1759 (asyh->core.pitch >> 8) << 8 |
1760 asyh->core.block);
1761 evo_data(push, asyh->core.format << 8);
1762 evo_data(push, asyh->core.handle);
1763 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
1764 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1765 }
1766 evo_kick(push, core);
1767 }
1768}
1769
1770static void
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001771nv50_head_lut_clr(struct nv50_head *head)
1772{
1773 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1774 u32 *push;
1775 if ((push = evo_wait(core, 4))) {
1776 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1777 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1778 evo_data(push, 0x40000000);
1779 } else
1780 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1781 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1782 evo_data(push, 0x40000000);
1783 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1784 evo_data(push, 0x00000000);
1785 } else {
1786 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
1787 evo_data(push, 0x03000000);
1788 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1789 evo_data(push, 0x00000000);
1790 }
1791 evo_kick(push, core);
1792 }
1793}
1794
1795static void
1796nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1797{
1798 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1799 u32 *push;
1800 if ((push = evo_wait(core, 7))) {
1801 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1802 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1803 evo_data(push, 0xc0000000);
1804 evo_data(push, asyh->lut.offset >> 8);
1805 } else
1806 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1807 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1808 evo_data(push, 0xc0000000);
1809 evo_data(push, asyh->lut.offset >> 8);
1810 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1811 evo_data(push, asyh->lut.handle);
1812 } else {
1813 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
1814 evo_data(push, 0x83000000);
1815 evo_data(push, asyh->lut.offset >> 8);
1816 evo_data(push, 0x00000000);
1817 evo_data(push, 0x00000000);
1818 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1819 evo_data(push, asyh->lut.handle);
1820 }
1821 evo_kick(push, core);
1822 }
1823}
1824
1825static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001826nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1827{
1828 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1829 struct nv50_head_mode *m = &asyh->mode;
1830 u32 *push;
1831 if ((push = evo_wait(core, 14))) {
1832 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1833 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
1834 evo_data(push, 0x00800000 | m->clock);
1835 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001836 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 7);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001837 evo_data(push, 0x00000000);
1838 evo_data(push, (m->v.active << 16) | m->h.active );
1839 evo_data(push, (m->v.synce << 16) | m->h.synce );
1840 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1841 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1842 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001843 evo_data(push, asyh->mode.v.blankus);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001844 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
1845 evo_data(push, 0x00000000);
1846 } else {
1847 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
1848 evo_data(push, 0x00000000);
1849 evo_data(push, (m->v.active << 16) | m->h.active );
1850 evo_data(push, (m->v.synce << 16) | m->h.synce );
1851 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1852 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1853 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
1854 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
1855 evo_data(push, 0x00000000); /* ??? */
1856 evo_data(push, 0xffffff00);
1857 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
1858 evo_data(push, m->clock * 1000);
1859 evo_data(push, 0x00200000); /* ??? */
1860 evo_data(push, m->clock * 1000);
1861 }
1862 evo_kick(push, core);
1863 }
1864}
1865
1866static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001867nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
1868{
1869 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1870 u32 *push;
1871 if ((push = evo_wait(core, 10))) {
1872 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1873 evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
1874 evo_data(push, 0x00000000);
1875 evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
1876 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1877 evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
1878 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1879 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1880 } else {
1881 evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
1882 evo_data(push, 0x00000000);
1883 evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
1884 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1885 evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
1886 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1887 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1888 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1889 }
1890 evo_kick(push, core);
1891 }
1892}
1893
1894static void
Ben Skeggsad633612016-11-04 17:20:36 +10001895nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
1896{
1897 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001898 nv50_head_lut_clr(head);
1899 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsad633612016-11-04 17:20:36 +10001900 nv50_head_core_clr(head);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001901 if (asyh->clr.curs && (!asyh->set.curs || y))
1902 nv50_head_curs_clr(head);
Ben Skeggsad633612016-11-04 17:20:36 +10001903}
1904
1905static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001906nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1907{
Ben Skeggsc4e68122016-11-04 17:20:36 +10001908 if (asyh->set.view ) nv50_head_view (head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001909 if (asyh->set.mode ) nv50_head_mode (head, asyh);
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001910 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10001911 if (asyh->set.core ) nv50_head_core_set(head, asyh);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001912 if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001913 if (asyh->set.base ) nv50_head_base (head, asyh);
1914 if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
Ben Skeggs7e918332016-11-04 17:20:36 +10001915 if (asyh->set.dither ) nv50_head_dither (head, asyh);
Ben Skeggs7e08d672016-11-04 17:20:36 +10001916 if (asyh->set.procamp) nv50_head_procamp (head, asyh);
1917}
1918
1919static void
1920nv50_head_atomic_check_procamp(struct nv50_head_atom *armh,
1921 struct nv50_head_atom *asyh,
1922 struct nouveau_conn_atom *asyc)
1923{
1924 const int vib = asyc->procamp.color_vibrance - 100;
1925 const int hue = asyc->procamp.vibrant_hue - 90;
1926 const int adj = (vib > 0) ? 50 : 0;
1927 asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff;
1928 asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff;
1929 asyh->set.procamp = true;
Ben Skeggs7e918332016-11-04 17:20:36 +10001930}
1931
1932static void
1933nv50_head_atomic_check_dither(struct nv50_head_atom *armh,
1934 struct nv50_head_atom *asyh,
1935 struct nouveau_conn_atom *asyc)
1936{
1937 struct drm_connector *connector = asyc->state.connector;
1938 u32 mode = 0x00;
1939
1940 if (asyc->dither.mode == DITHERING_MODE_AUTO) {
1941 if (asyh->base.depth > connector->display_info.bpc * 3)
1942 mode = DITHERING_MODE_DYNAMIC2X2;
1943 } else {
1944 mode = asyc->dither.mode;
1945 }
1946
1947 if (asyc->dither.depth == DITHERING_DEPTH_AUTO) {
1948 if (connector->display_info.bpc >= 8)
1949 mode |= DITHERING_DEPTH_8BPC;
1950 } else {
1951 mode |= asyc->dither.depth;
1952 }
1953
1954 asyh->dither.enable = mode;
1955 asyh->dither.bits = mode >> 1;
1956 asyh->dither.mode = mode >> 3;
1957 asyh->set.dither = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001958}
1959
1960static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001961nv50_head_atomic_check_view(struct nv50_head_atom *armh,
1962 struct nv50_head_atom *asyh,
1963 struct nouveau_conn_atom *asyc)
1964{
1965 struct drm_connector *connector = asyc->state.connector;
1966 struct drm_display_mode *omode = &asyh->state.adjusted_mode;
1967 struct drm_display_mode *umode = &asyh->state.mode;
1968 int mode = asyc->scaler.mode;
1969 struct edid *edid;
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04001970 int umode_vdisplay, omode_hdisplay, omode_vdisplay;
Ben Skeggsc4e68122016-11-04 17:20:36 +10001971
1972 if (connector->edid_blob_ptr)
1973 edid = (struct edid *)connector->edid_blob_ptr->data;
1974 else
1975 edid = NULL;
1976
1977 if (!asyc->scaler.full) {
1978 if (mode == DRM_MODE_SCALE_NONE)
1979 omode = umode;
1980 } else {
1981 /* Non-EDID LVDS/eDP mode. */
1982 mode = DRM_MODE_SCALE_FULLSCREEN;
1983 }
1984
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04001985 /* For the user-specified mode, we must ignore doublescan and
1986 * the like, but honor frame packing.
1987 */
1988 umode_vdisplay = umode->vdisplay;
1989 if ((umode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1990 umode_vdisplay += umode->vtotal;
Ben Skeggsc4e68122016-11-04 17:20:36 +10001991 asyh->view.iW = umode->hdisplay;
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04001992 asyh->view.iH = umode_vdisplay;
1993 /* For the output mode, we can just use the stock helper. */
1994 drm_mode_get_hv_timing(omode, &omode_hdisplay, &omode_vdisplay);
1995 asyh->view.oW = omode_hdisplay;
1996 asyh->view.oH = omode_vdisplay;
Ben Skeggsc4e68122016-11-04 17:20:36 +10001997
1998 /* Add overscan compensation if necessary, will keep the aspect
1999 * ratio the same as the backend mode unless overridden by the
2000 * user setting both hborder and vborder properties.
2001 */
2002 if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
2003 (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
2004 drm_detect_hdmi_monitor(edid)))) {
2005 u32 bX = asyc->scaler.underscan.hborder;
2006 u32 bY = asyc->scaler.underscan.vborder;
2007 u32 r = (asyh->view.oH << 19) / asyh->view.oW;
2008
2009 if (bX) {
2010 asyh->view.oW -= (bX * 2);
2011 if (bY) asyh->view.oH -= (bY * 2);
2012 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2013 } else {
2014 asyh->view.oW -= (asyh->view.oW >> 4) + 32;
2015 if (bY) asyh->view.oH -= (bY * 2);
2016 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2017 }
2018 }
2019
2020 /* Handle CENTER/ASPECT scaling, taking into account the areas
2021 * removed already for overscan compensation.
2022 */
2023 switch (mode) {
2024 case DRM_MODE_SCALE_CENTER:
2025 asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04002026 asyh->view.oH = min((u16)umode_vdisplay, asyh->view.oH);
Ben Skeggsc4e68122016-11-04 17:20:36 +10002027 /* fall-through */
2028 case DRM_MODE_SCALE_ASPECT:
2029 if (asyh->view.oH < asyh->view.oW) {
2030 u32 r = (asyh->view.iW << 19) / asyh->view.iH;
2031 asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
2032 } else {
2033 u32 r = (asyh->view.iH << 19) / asyh->view.iW;
2034 asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2035 }
2036 break;
2037 default:
2038 break;
2039 }
2040
2041 asyh->set.view = true;
2042}
2043
2044static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002045nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
2046{
2047 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002048 struct nv50_head_mode *m = &asyh->mode;
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002049 u32 blankus;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002050
Alastair Bridgewater37aa2242017-04-11 13:11:24 -04002051 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V | CRTC_STEREO_DOUBLE);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002052
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002053 /*
2054 * DRM modes are defined in terms of a repeating interval
2055 * starting with the active display area. The hardware modes
2056 * are defined in terms of a repeating interval starting one
2057 * unit (pixel or line) into the sync pulse. So, add bias.
2058 */
2059
2060 m->h.active = mode->crtc_htotal;
2061 m->h.synce = mode->crtc_hsync_end - mode->crtc_hsync_start - 1;
2062 m->h.blanke = mode->crtc_hblank_end - mode->crtc_hsync_start - 1;
2063 m->h.blanks = m->h.blanke + mode->crtc_hdisplay;
2064
2065 m->v.active = mode->crtc_vtotal;
2066 m->v.synce = mode->crtc_vsync_end - mode->crtc_vsync_start - 1;
2067 m->v.blanke = mode->crtc_vblank_end - mode->crtc_vsync_start - 1;
2068 m->v.blanks = m->v.blanke + mode->crtc_vdisplay;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002069
2070 /*XXX: Safe underestimate, even "0" works */
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002071 blankus = (m->v.active - mode->crtc_vdisplay - 2) * m->h.active;
Ben Skeggsaeecfcd2017-04-05 09:12:54 +10002072 blankus *= 1000;
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002073 blankus /= mode->crtc_clock;
Ben Skeggsaeecfcd2017-04-05 09:12:54 +10002074 m->v.blankus = blankus;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002075
2076 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002077 m->v.blank2e = m->v.active + m->v.blanke;
2078 m->v.blank2s = m->v.blank2e + mode->crtc_vdisplay;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002079 m->v.active = (m->v.active * 2) + 1;
2080 m->interlace = true;
2081 } else {
2082 m->v.blank2e = 0;
2083 m->v.blank2s = 1;
2084 m->interlace = false;
2085 }
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002086 m->clock = mode->crtc_clock;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002087
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002088 asyh->set.mode = true;
2089}
2090
2091static int
2092nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
2093{
2094 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggsad633612016-11-04 17:20:36 +10002095 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002096 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002097 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002098 struct nv50_head_atom *asyh = nv50_head_atom(state);
Ben Skeggs839ca902016-11-04 17:20:36 +10002099 struct nouveau_conn_atom *asyc = NULL;
2100 struct drm_connector_state *conns;
2101 struct drm_connector *conn;
2102 int i;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002103
2104 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002105 if (asyh->state.active) {
Ben Skeggs839ca902016-11-04 17:20:36 +10002106 for_each_connector_in_state(asyh->state.state, conn, conns, i) {
2107 if (conns->crtc == crtc) {
2108 asyc = nouveau_conn_atom(conns);
2109 break;
2110 }
2111 }
2112
2113 if (armh->state.active) {
2114 if (asyc) {
2115 if (asyh->state.mode_changed)
2116 asyc->set.scaler = true;
2117 if (armh->base.depth != asyh->base.depth)
2118 asyc->set.dither = true;
2119 }
2120 } else {
Gustavo A. R. Silva86276922017-05-22 14:12:37 -05002121 if (asyc)
2122 asyc->set.mask = ~0;
Ben Skeggs839ca902016-11-04 17:20:36 +10002123 asyh->set.mask = ~0;
2124 }
2125
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002126 if (asyh->state.mode_changed)
2127 nv50_head_atomic_check_mode(head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10002128
Ben Skeggs839ca902016-11-04 17:20:36 +10002129 if (asyc) {
2130 if (asyc->set.scaler)
2131 nv50_head_atomic_check_view(armh, asyh, asyc);
2132 if (asyc->set.dither)
2133 nv50_head_atomic_check_dither(armh, asyh, asyc);
2134 if (asyc->set.procamp)
2135 nv50_head_atomic_check_procamp(armh, asyh, asyc);
2136 }
2137
Ben Skeggsad633612016-11-04 17:20:36 +10002138 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
2139 asyh->core.x = asyh->base.x;
2140 asyh->core.y = asyh->base.y;
2141 asyh->core.w = asyh->base.w;
2142 asyh->core.h = asyh->base.h;
2143 } else
Ben Skeggsea8ee392016-11-04 17:20:36 +10002144 if ((asyh->core.visible = asyh->curs.visible)) {
Ben Skeggsad633612016-11-04 17:20:36 +10002145 /*XXX: We need to either find some way of having the
2146 * primary base layer appear black, while still
2147 * being able to display the other layers, or we
2148 * need to allocate a dummy black surface here.
2149 */
2150 asyh->core.x = 0;
2151 asyh->core.y = 0;
2152 asyh->core.w = asyh->state.mode.hdisplay;
2153 asyh->core.h = asyh->state.mode.vdisplay;
2154 }
2155 asyh->core.handle = disp->mast.base.vram.handle;
2156 asyh->core.offset = 0;
2157 asyh->core.format = 0xcf;
2158 asyh->core.kind = 0;
2159 asyh->core.layout = 1;
2160 asyh->core.block = 0;
2161 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002162 asyh->lut.handle = disp->mast.base.vram.handle;
2163 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002164 asyh->set.base = armh->base.cpp != asyh->base.cpp;
2165 asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
Ben Skeggsad633612016-11-04 17:20:36 +10002166 } else {
2167 asyh->core.visible = false;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002168 asyh->curs.visible = false;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002169 asyh->base.cpp = 0;
2170 asyh->ovly.cpp = 0;
Ben Skeggsad633612016-11-04 17:20:36 +10002171 }
2172
2173 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
2174 if (asyh->core.visible) {
2175 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
2176 asyh->set.core = true;
2177 } else
2178 if (armh->core.visible) {
2179 asyh->clr.core = true;
2180 }
Ben Skeggsea8ee392016-11-04 17:20:36 +10002181
2182 if (asyh->curs.visible) {
2183 if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
2184 asyh->set.curs = true;
2185 } else
2186 if (armh->curs.visible) {
2187 asyh->clr.curs = true;
2188 }
Ben Skeggsad633612016-11-04 17:20:36 +10002189 } else {
2190 asyh->clr.core = armh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002191 asyh->clr.curs = armh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10002192 asyh->set.core = asyh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002193 asyh->set.curs = asyh->curs.visible;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002194 }
2195
Ben Skeggs839ca902016-11-04 17:20:36 +10002196 if (asyh->clr.mask || asyh->set.mask)
2197 nv50_atom(asyh->state.state)->lock_core = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002198 return 0;
2199}
2200
Ben Skeggs438d99e2011-07-05 16:48:06 +10002201static void
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002202nv50_head_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002203{
Ben Skeggse225f442012-11-21 14:40:21 +10002204 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002205 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2206 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
2207 int i;
2208
2209 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002210 u16 r = nv_crtc->lut.r[i] >> 2;
2211 u16 g = nv_crtc->lut.g[i] >> 2;
2212 u16 b = nv_crtc->lut.b[i] >> 2;
2213
Ben Skeggs648d4df2014-08-10 04:10:27 +10002214 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002215 writew(r + 0x0000, lut + (i * 0x08) + 0);
2216 writew(g + 0x0000, lut + (i * 0x08) + 2);
2217 writew(b + 0x0000, lut + (i * 0x08) + 4);
2218 } else {
2219 writew(r + 0x6000, lut + (i * 0x20) + 0);
2220 writew(g + 0x6000, lut + (i * 0x20) + 2);
2221 writew(b + 0x6000, lut + (i * 0x20) + 4);
2222 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002223 }
2224}
2225
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002226static const struct drm_crtc_helper_funcs
2227nv50_head_help = {
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002228 .load_lut = nv50_head_lut_load,
Ben Skeggs839ca902016-11-04 17:20:36 +10002229 .atomic_check = nv50_head_atomic_check,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002230};
2231
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002232static int
2233nv50_head_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02002234 uint32_t size,
2235 struct drm_modeset_acquire_ctx *ctx)
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002236{
2237 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2238 u32 i;
2239
2240 for (i = 0; i < size; i++) {
2241 nv_crtc->lut.r[i] = r[i];
2242 nv_crtc->lut.g[i] = g[i];
2243 nv_crtc->lut.b[i] = b[i];
2244 }
2245
2246 nv50_head_lut_load(crtc);
2247 return 0;
2248}
2249
Ben Skeggs839ca902016-11-04 17:20:36 +10002250static void
2251nv50_head_atomic_destroy_state(struct drm_crtc *crtc,
2252 struct drm_crtc_state *state)
2253{
2254 struct nv50_head_atom *asyh = nv50_head_atom(state);
2255 __drm_atomic_helper_crtc_destroy_state(&asyh->state);
2256 kfree(asyh);
2257}
2258
2259static struct drm_crtc_state *
2260nv50_head_atomic_duplicate_state(struct drm_crtc *crtc)
2261{
2262 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
2263 struct nv50_head_atom *asyh;
2264 if (!(asyh = kmalloc(sizeof(*asyh), GFP_KERNEL)))
2265 return NULL;
2266 __drm_atomic_helper_crtc_duplicate_state(crtc, &asyh->state);
2267 asyh->view = armh->view;
2268 asyh->mode = armh->mode;
2269 asyh->lut = armh->lut;
2270 asyh->core = armh->core;
2271 asyh->curs = armh->curs;
2272 asyh->base = armh->base;
2273 asyh->ovly = armh->ovly;
2274 asyh->dither = armh->dither;
2275 asyh->procamp = armh->procamp;
2276 asyh->clr.mask = 0;
2277 asyh->set.mask = 0;
2278 return &asyh->state;
2279}
2280
2281static void
2282__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
2283 struct drm_crtc_state *state)
2284{
2285 if (crtc->state)
2286 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
2287 crtc->state = state;
2288 crtc->state->crtc = crtc;
2289}
2290
2291static void
2292nv50_head_reset(struct drm_crtc *crtc)
2293{
2294 struct nv50_head_atom *asyh;
2295
2296 if (WARN_ON(!(asyh = kzalloc(sizeof(*asyh), GFP_KERNEL))))
2297 return;
2298
2299 __drm_atomic_helper_crtc_reset(crtc, &asyh->state);
2300}
2301
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002302static void
2303nv50_head_destroy(struct drm_crtc *crtc)
2304{
2305 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2306 struct nv50_disp *disp = nv50_disp(crtc->dev);
2307 struct nv50_head *head = nv50_head(crtc);
2308
2309 nv50_dmac_destroy(&head->ovly.base, disp->disp);
2310 nv50_pioc_destroy(&head->oimm.base);
2311
2312 nouveau_bo_unmap(nv_crtc->lut.nvbo);
2313 if (nv_crtc->lut.nvbo)
2314 nouveau_bo_unpin(nv_crtc->lut.nvbo);
2315 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
2316
2317 drm_crtc_cleanup(crtc);
2318 kfree(crtc);
2319}
2320
2321static const struct drm_crtc_funcs
2322nv50_head_func = {
Ben Skeggs839ca902016-11-04 17:20:36 +10002323 .reset = nv50_head_reset,
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002324 .gamma_set = nv50_head_gamma_set,
2325 .destroy = nv50_head_destroy,
Ben Skeggs839ca902016-11-04 17:20:36 +10002326 .set_config = drm_atomic_helper_set_config,
Andrey Grodzovsky612fb5d2017-02-02 16:56:30 -05002327 .page_flip = drm_atomic_helper_page_flip,
Ben Skeggs839ca902016-11-04 17:20:36 +10002328 .set_property = drm_atomic_helper_crtc_set_property,
2329 .atomic_duplicate_state = nv50_head_atomic_duplicate_state,
2330 .atomic_destroy_state = nv50_head_atomic_destroy_state,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002331};
2332
2333static int
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002334nv50_head_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002335{
Ben Skeggsa01ca782015-08-20 14:54:15 +10002336 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002337 struct nvif_device *device = &drm->client.device;
Ben Skeggse225f442012-11-21 14:40:21 +10002338 struct nv50_disp *disp = nv50_disp(dev);
2339 struct nv50_head *head;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002340 struct nv50_base *base;
Ben Skeggs22e927d2016-11-04 17:20:36 +10002341 struct nv50_curs *curs;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002342 struct drm_crtc *crtc;
2343 int ret, i;
2344
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002345 head = kzalloc(sizeof(*head), GFP_KERNEL);
2346 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002347 return -ENOMEM;
2348
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002349 head->base.index = index;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002350 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002351 head->base.lut.r[i] = i << 8;
2352 head->base.lut.g[i] = i << 8;
2353 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002354 }
2355
Ben Skeggs973f10c2016-11-04 17:20:36 +10002356 ret = nv50_base_new(drm, head, &base);
Ben Skeggs22e927d2016-11-04 17:20:36 +10002357 if (ret == 0)
2358 ret = nv50_curs_new(drm, head, &curs);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002359 if (ret) {
2360 kfree(head);
2361 return ret;
2362 }
2363
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002364 crtc = &head->base.base;
Ben Skeggs839ca902016-11-04 17:20:36 +10002365 drm_crtc_init_with_planes(dev, crtc, &base->wndw.plane,
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002366 &curs->wndw.plane, &nv50_head_func,
Ben Skeggs839ca902016-11-04 17:20:36 +10002367 "head-%d", head->base.index);
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002368 drm_crtc_helper_add(crtc, &nv50_head_help);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002369 drm_mode_crtc_set_gamma_size(crtc, 256);
2370
Ben Skeggsbab7cc12016-05-24 17:26:48 +10002371 ret = nouveau_bo_new(&drm->client, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002372 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002373 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002374 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002375 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002376 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002377 if (ret)
2378 nouveau_bo_unpin(head->base.lut.nvbo);
2379 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002380 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002381 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002382 }
2383
2384 if (ret)
2385 goto out;
2386
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002387 /* allocate overlay resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10002388 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002389 if (ret)
2390 goto out;
2391
Ben Skeggsa01ca782015-08-20 14:54:15 +10002392 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
2393 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002394 if (ret)
2395 goto out;
2396
Ben Skeggs438d99e2011-07-05 16:48:06 +10002397out:
2398 if (ret)
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002399 nv50_head_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002400 return ret;
2401}
2402
2403/******************************************************************************
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002404 * Output path helpers
Ben Skeggsa91d3222014-12-22 16:30:13 +10002405 *****************************************************************************/
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002406static int
2407nv50_outp_atomic_check_view(struct drm_encoder *encoder,
2408 struct drm_crtc_state *crtc_state,
2409 struct drm_connector_state *conn_state,
2410 struct drm_display_mode *native_mode)
2411{
2412 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
2413 struct drm_display_mode *mode = &crtc_state->mode;
2414 struct drm_connector *connector = conn_state->connector;
2415 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
2416 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
2417
2418 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
2419 asyc->scaler.full = false;
2420 if (!native_mode)
2421 return 0;
2422
2423 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
2424 switch (connector->connector_type) {
2425 case DRM_MODE_CONNECTOR_LVDS:
2426 case DRM_MODE_CONNECTOR_eDP:
2427 /* Force use of scaler for non-EDID modes. */
2428 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
2429 break;
2430 mode = native_mode;
2431 asyc->scaler.full = true;
2432 break;
2433 default:
2434 break;
2435 }
2436 } else {
2437 mode = native_mode;
2438 }
2439
2440 if (!drm_mode_equal(adjusted_mode, mode)) {
2441 drm_mode_copy(adjusted_mode, mode);
2442 crtc_state->mode_changed = true;
2443 }
2444
2445 return 0;
2446}
2447
Ben Skeggs839ca902016-11-04 17:20:36 +10002448static int
2449nv50_outp_atomic_check(struct drm_encoder *encoder,
2450 struct drm_crtc_state *crtc_state,
2451 struct drm_connector_state *conn_state)
Ben Skeggsa91d3222014-12-22 16:30:13 +10002452{
Ben Skeggs839ca902016-11-04 17:20:36 +10002453 struct nouveau_connector *nv_connector =
2454 nouveau_connector(conn_state->connector);
2455 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2456 nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10002457}
2458
2459/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002460 * DAC
2461 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002462static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002463nv50_dac_disable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002464{
Ben Skeggsf20c6652016-11-04 17:20:36 +10002465 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2466 struct nv50_mast *mast = nv50_mast(encoder->dev);
2467 const int or = nv_encoder->or;
2468 u32 *push;
2469
2470 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10002471 push = evo_wait(mast, 4);
2472 if (push) {
2473 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2474 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2475 evo_data(push, 0x00000000);
2476 } else {
2477 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2478 evo_data(push, 0x00000000);
2479 }
2480 evo_kick(push, mast);
2481 }
2482 }
2483
2484 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002485}
2486
2487static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002488nv50_dac_enable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002489{
Ben Skeggse225f442012-11-21 14:40:21 +10002490 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002491 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2492 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002493 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggs97b19b52012-11-16 11:21:37 +10002494 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002495
Ben Skeggs97b19b52012-11-16 11:21:37 +10002496 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002497 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002498 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002499 u32 syncs = 0x00000000;
2500
2501 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2502 syncs |= 0x00000001;
2503 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2504 syncs |= 0x00000002;
2505
2506 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
2507 evo_data(push, 1 << nv_crtc->index);
2508 evo_data(push, syncs);
2509 } else {
2510 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2511 u32 syncs = 0x00000001;
2512
2513 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2514 syncs |= 0x00000008;
2515 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2516 syncs |= 0x00000010;
2517
2518 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2519 magic |= 0x00000001;
2520
2521 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2522 evo_data(push, syncs);
2523 evo_data(push, magic);
2524 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2525 evo_data(push, 1 << nv_crtc->index);
2526 }
2527
2528 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002529 }
2530
2531 nv_encoder->crtc = encoder->crtc;
2532}
2533
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002534static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10002535nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002536{
Ben Skeggsc4abd312014-08-10 04:10:26 +10002537 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002538 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10002539 struct {
2540 struct nv50_disp_mthd_v1 base;
2541 struct nv50_disp_dac_load_v0 load;
2542 } args = {
2543 .base.version = 1,
2544 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2545 .base.hasht = nv_encoder->dcb->hasht,
2546 .base.hashm = nv_encoder->dcb->hashm,
2547 };
2548 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10002549
Ben Skeggsc4abd312014-08-10 04:10:26 +10002550 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2551 if (args.load.data == 0)
2552 args.load.data = 340;
2553
2554 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2555 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10002556 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10002557
Ben Skeggs35b21d32012-11-08 12:08:55 +10002558 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002559}
2560
Ben Skeggsf20c6652016-11-04 17:20:36 +10002561static const struct drm_encoder_helper_funcs
2562nv50_dac_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10002563 .atomic_check = nv50_outp_atomic_check,
2564 .enable = nv50_dac_enable,
2565 .disable = nv50_dac_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002566 .detect = nv50_dac_detect
2567};
2568
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002569static void
Ben Skeggse225f442012-11-21 14:40:21 +10002570nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002571{
2572 drm_encoder_cleanup(encoder);
2573 kfree(encoder);
2574}
2575
Ben Skeggsf20c6652016-11-04 17:20:36 +10002576static const struct drm_encoder_funcs
2577nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10002578 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002579};
2580
2581static int
Ben Skeggse225f442012-11-21 14:40:21 +10002582nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002583{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002584 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002585 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002586 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002587 struct nouveau_encoder *nv_encoder;
2588 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002589 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002590
2591 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2592 if (!nv_encoder)
2593 return -ENOMEM;
2594 nv_encoder->dcb = dcbe;
2595 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002596
2597 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2598 if (bus)
2599 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002600
2601 encoder = to_drm_encoder(nv_encoder);
2602 encoder->possible_crtcs = dcbe->heads;
2603 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002604 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2605 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10002606 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002607
2608 drm_mode_connector_attach_encoder(connector, encoder);
2609 return 0;
2610}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002611
2612/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10002613 * Audio
2614 *****************************************************************************/
2615static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002616nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
2617{
2618 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2619 struct nv50_disp *disp = nv50_disp(encoder->dev);
2620 struct {
2621 struct nv50_disp_mthd_v1 base;
2622 struct nv50_disp_sor_hda_eld_v0 eld;
2623 } args = {
2624 .base.version = 1,
2625 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2626 .base.hasht = nv_encoder->dcb->hasht,
2627 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2628 (0x0100 << nv_crtc->index),
2629 };
2630
2631 nvif_mthd(disp->disp, 0, &args, sizeof(args));
2632}
2633
2634static void
2635nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002636{
2637 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10002638 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10002639 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10002640 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10002641 struct __packed {
2642 struct {
2643 struct nv50_disp_mthd_v1 mthd;
2644 struct nv50_disp_sor_hda_eld_v0 eld;
2645 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002646 u8 data[sizeof(nv_connector->base.eld)];
2647 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10002648 .base.mthd.version = 1,
2649 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2650 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002651 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2652 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002653 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002654
2655 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2656 if (!drm_detect_monitor_audio(nv_connector->edid))
2657 return;
2658
Ben Skeggs78951d22011-11-11 18:13:13 +10002659 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002660 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002661
Jani Nikula938fd8a2014-10-28 16:20:48 +02002662 nvif_mthd(disp->disp, 0, &args,
2663 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002664}
2665
Ben Skeggsf20c6652016-11-04 17:20:36 +10002666/******************************************************************************
2667 * HDMI
2668 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +10002669static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002670nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002671{
2672 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002673 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002674 struct {
2675 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +10002676 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002677 } args = {
2678 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002679 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2680 .base.hasht = nv_encoder->dcb->hasht,
2681 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2682 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002683 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002684
Ben Skeggs120b0c32014-08-10 04:10:26 +10002685 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002686}
2687
Ben Skeggs78951d22011-11-11 18:13:13 +10002688static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002689nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002690{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002691 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2692 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002693 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002694 struct {
2695 struct nv50_disp_mthd_v1 base;
2696 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002697 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
Ben Skeggse00f2232014-08-10 04:10:26 +10002698 } args = {
2699 .base.version = 1,
2700 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2701 .base.hasht = nv_encoder->dcb->hasht,
2702 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2703 (0x0100 << nv_crtc->index),
2704 .pwr.state = 1,
2705 .pwr.rekey = 56, /* binary driver, and tegra, constant */
2706 };
2707 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002708 u32 max_ac_packet;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002709 union hdmi_infoframe avi_frame;
2710 union hdmi_infoframe vendor_frame;
2711 int ret;
2712 int size;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002713
2714 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2715 if (!drm_detect_hdmi_monitor(nv_connector->edid))
2716 return;
2717
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002718 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi, mode);
2719 if (!ret) {
2720 /* We have an AVI InfoFrame, populate it to the display */
2721 args.pwr.avi_infoframe_length
2722 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
2723 }
2724
2725 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi, mode);
2726 if (!ret) {
2727 /* We have a Vendor InfoFrame, populate it to the display */
2728 args.pwr.vendor_infoframe_length
2729 = hdmi_infoframe_pack(&vendor_frame,
2730 args.infoframes
2731 + args.pwr.avi_infoframe_length,
2732 17);
2733 }
2734
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002735 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10002736 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002737 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10002738 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002739
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -04002740 size = sizeof(args.base)
2741 + sizeof(args.pwr)
2742 + args.pwr.avi_infoframe_length
2743 + args.pwr.vendor_infoframe_length;
2744 nvif_mthd(disp->disp, 0, &args, size);
Ben Skeggsf20c6652016-11-04 17:20:36 +10002745 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10002746}
2747
2748/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002749 * MST
2750 *****************************************************************************/
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002751#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
2752#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
2753#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
2754
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002755struct nv50_mstm {
2756 struct nouveau_encoder *outp;
2757
2758 struct drm_dp_mst_topology_mgr mgr;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002759 struct nv50_msto *msto[4];
2760
2761 bool modified;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002762};
2763
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002764struct nv50_mstc {
2765 struct nv50_mstm *mstm;
2766 struct drm_dp_mst_port *port;
2767 struct drm_connector connector;
2768
2769 struct drm_display_mode *native;
2770 struct edid *edid;
2771
2772 int pbn;
2773};
2774
2775struct nv50_msto {
2776 struct drm_encoder encoder;
2777
2778 struct nv50_head *head;
2779 struct nv50_mstc *mstc;
2780 bool disabled;
2781};
2782
2783static struct drm_dp_payload *
2784nv50_msto_payload(struct nv50_msto *msto)
2785{
2786 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2787 struct nv50_mstc *mstc = msto->mstc;
2788 struct nv50_mstm *mstm = mstc->mstm;
2789 int vcpi = mstc->port->vcpi.vcpi, i;
2790
2791 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
2792 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2793 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2794 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
2795 mstm->outp->base.base.name, i, payload->vcpi,
2796 payload->start_slot, payload->num_slots);
2797 }
2798
2799 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2800 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2801 if (payload->vcpi == vcpi)
2802 return payload;
2803 }
2804
2805 return NULL;
2806}
2807
2808static void
2809nv50_msto_cleanup(struct nv50_msto *msto)
2810{
2811 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2812 struct nv50_mstc *mstc = msto->mstc;
2813 struct nv50_mstm *mstm = mstc->mstm;
2814
2815 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
2816 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
2817 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
2818 if (msto->disabled) {
2819 msto->mstc = NULL;
2820 msto->head = NULL;
2821 msto->disabled = false;
2822 }
2823}
2824
2825static void
2826nv50_msto_prepare(struct nv50_msto *msto)
2827{
2828 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2829 struct nv50_mstc *mstc = msto->mstc;
2830 struct nv50_mstm *mstm = mstc->mstm;
2831 struct {
2832 struct nv50_disp_mthd_v1 base;
2833 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
2834 } args = {
2835 .base.version = 1,
2836 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
2837 .base.hasht = mstm->outp->dcb->hasht,
2838 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
2839 (0x0100 << msto->head->base.index),
2840 };
2841
2842 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
2843 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
2844 struct drm_dp_payload *payload = nv50_msto_payload(msto);
2845 if (payload) {
2846 args.vcpi.start_slot = payload->start_slot;
2847 args.vcpi.num_slots = payload->num_slots;
2848 args.vcpi.pbn = mstc->port->vcpi.pbn;
2849 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
2850 }
2851 }
2852
2853 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
2854 msto->encoder.name, msto->head->base.base.name,
2855 args.vcpi.start_slot, args.vcpi.num_slots,
2856 args.vcpi.pbn, args.vcpi.aligned_pbn);
2857 nvif_mthd(&drm->display->disp, 0, &args, sizeof(args));
2858}
2859
2860static int
2861nv50_msto_atomic_check(struct drm_encoder *encoder,
2862 struct drm_crtc_state *crtc_state,
2863 struct drm_connector_state *conn_state)
2864{
2865 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
2866 struct nv50_mstm *mstm = mstc->mstm;
2867 int bpp = conn_state->connector->display_info.bpc * 3;
2868 int slots;
2869
2870 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
2871
2872 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2873 if (slots < 0)
2874 return slots;
2875
2876 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2877 mstc->native);
2878}
2879
2880static void
2881nv50_msto_enable(struct drm_encoder *encoder)
2882{
2883 struct nv50_head *head = nv50_head(encoder->crtc);
2884 struct nv50_msto *msto = nv50_msto(encoder);
2885 struct nv50_mstc *mstc = NULL;
2886 struct nv50_mstm *mstm = NULL;
2887 struct drm_connector *connector;
Gustavo Padovan875dd622017-05-11 16:10:46 -03002888 struct drm_connector_list_iter conn_iter;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002889 u8 proto, depth;
2890 int slots;
2891 bool r;
2892
Gustavo Padovan875dd622017-05-11 16:10:46 -03002893 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
2894 drm_for_each_connector_iter(connector, &conn_iter) {
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002895 if (connector->state->best_encoder == &msto->encoder) {
2896 mstc = nv50_mstc(connector);
2897 mstm = mstc->mstm;
2898 break;
2899 }
2900 }
Gustavo Padovan875dd622017-05-11 16:10:46 -03002901 drm_connector_list_iter_end(&conn_iter);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002902
2903 if (WARN_ON(!mstc))
2904 return;
2905
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002906 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2907 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002908 WARN_ON(!r);
2909
2910 if (mstm->outp->dcb->sorconf.link & 1)
2911 proto = 0x8;
2912 else
2913 proto = 0x9;
2914
2915 switch (mstc->connector.display_info.bpc) {
2916 case 6: depth = 0x2; break;
2917 case 8: depth = 0x5; break;
2918 case 10:
2919 default: depth = 0x6; break;
2920 }
2921
2922 mstm->outp->update(mstm->outp, head->base.index,
2923 &head->base.base.state->adjusted_mode, proto, depth);
2924
2925 msto->head = head;
2926 msto->mstc = mstc;
2927 mstm->modified = true;
2928}
2929
2930static void
2931nv50_msto_disable(struct drm_encoder *encoder)
2932{
2933 struct nv50_msto *msto = nv50_msto(encoder);
2934 struct nv50_mstc *mstc = msto->mstc;
2935 struct nv50_mstm *mstm = mstc->mstm;
2936
2937 if (mstc->port)
2938 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
2939
2940 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
2941 mstm->modified = true;
2942 msto->disabled = true;
2943}
2944
2945static const struct drm_encoder_helper_funcs
2946nv50_msto_help = {
2947 .disable = nv50_msto_disable,
2948 .enable = nv50_msto_enable,
2949 .atomic_check = nv50_msto_atomic_check,
2950};
2951
2952static void
2953nv50_msto_destroy(struct drm_encoder *encoder)
2954{
2955 struct nv50_msto *msto = nv50_msto(encoder);
2956 drm_encoder_cleanup(&msto->encoder);
2957 kfree(msto);
2958}
2959
2960static const struct drm_encoder_funcs
2961nv50_msto = {
2962 .destroy = nv50_msto_destroy,
2963};
2964
2965static int
2966nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
2967 struct nv50_msto **pmsto)
2968{
2969 struct nv50_msto *msto;
2970 int ret;
2971
2972 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
2973 return -ENOMEM;
2974
2975 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
2976 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
2977 if (ret) {
2978 kfree(*pmsto);
2979 *pmsto = NULL;
2980 return ret;
2981 }
2982
2983 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
2984 msto->encoder.possible_crtcs = heads;
2985 return 0;
2986}
2987
2988static struct drm_encoder *
2989nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
2990 struct drm_connector_state *connector_state)
2991{
2992 struct nv50_head *head = nv50_head(connector_state->crtc);
2993 struct nv50_mstc *mstc = nv50_mstc(connector);
2994 if (mstc->port) {
2995 struct nv50_mstm *mstm = mstc->mstm;
2996 return &mstm->msto[head->base.index]->encoder;
2997 }
2998 return NULL;
2999}
3000
3001static struct drm_encoder *
3002nv50_mstc_best_encoder(struct drm_connector *connector)
3003{
3004 struct nv50_mstc *mstc = nv50_mstc(connector);
3005 if (mstc->port) {
3006 struct nv50_mstm *mstm = mstc->mstm;
3007 return &mstm->msto[0]->encoder;
3008 }
3009 return NULL;
3010}
3011
3012static enum drm_mode_status
3013nv50_mstc_mode_valid(struct drm_connector *connector,
3014 struct drm_display_mode *mode)
3015{
3016 return MODE_OK;
3017}
3018
3019static int
3020nv50_mstc_get_modes(struct drm_connector *connector)
3021{
3022 struct nv50_mstc *mstc = nv50_mstc(connector);
3023 int ret = 0;
3024
3025 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
3026 drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
3027 if (mstc->edid) {
3028 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
3029 drm_edid_to_eld(&mstc->connector, mstc->edid);
3030 }
3031
3032 if (!mstc->connector.display_info.bpc)
3033 mstc->connector.display_info.bpc = 8;
3034
3035 if (mstc->native)
3036 drm_mode_destroy(mstc->connector.dev, mstc->native);
3037 mstc->native = nouveau_conn_native_mode(&mstc->connector);
3038 return ret;
3039}
3040
3041static const struct drm_connector_helper_funcs
3042nv50_mstc_help = {
3043 .get_modes = nv50_mstc_get_modes,
3044 .mode_valid = nv50_mstc_mode_valid,
3045 .best_encoder = nv50_mstc_best_encoder,
3046 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
3047};
3048
3049static enum drm_connector_status
3050nv50_mstc_detect(struct drm_connector *connector, bool force)
3051{
3052 struct nv50_mstc *mstc = nv50_mstc(connector);
3053 if (!mstc->port)
3054 return connector_status_disconnected;
3055 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
3056}
3057
3058static void
3059nv50_mstc_destroy(struct drm_connector *connector)
3060{
3061 struct nv50_mstc *mstc = nv50_mstc(connector);
3062 drm_connector_cleanup(&mstc->connector);
3063 kfree(mstc);
3064}
3065
3066static const struct drm_connector_funcs
3067nv50_mstc = {
3068 .dpms = drm_atomic_helper_connector_dpms,
3069 .reset = nouveau_conn_reset,
3070 .detect = nv50_mstc_detect,
3071 .fill_modes = drm_helper_probe_single_connector_modes,
3072 .set_property = drm_atomic_helper_connector_set_property,
3073 .destroy = nv50_mstc_destroy,
3074 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
3075 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
3076 .atomic_set_property = nouveau_conn_atomic_set_property,
3077 .atomic_get_property = nouveau_conn_atomic_get_property,
3078};
3079
3080static int
3081nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
3082 const char *path, struct nv50_mstc **pmstc)
3083{
3084 struct drm_device *dev = mstm->outp->base.base.dev;
3085 struct nv50_mstc *mstc;
3086 int ret, i;
3087
3088 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
3089 return -ENOMEM;
3090 mstc->mstm = mstm;
3091 mstc->port = port;
3092
3093 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
3094 DRM_MODE_CONNECTOR_DisplayPort);
3095 if (ret) {
3096 kfree(*pmstc);
3097 *pmstc = NULL;
3098 return ret;
3099 }
3100
3101 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
3102
3103 mstc->connector.funcs->reset(&mstc->connector);
3104 nouveau_conn_attach_properties(&mstc->connector);
3105
3106 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto; i++)
3107 drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
3108
3109 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
3110 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
3111 drm_mode_connector_set_path_property(&mstc->connector, path);
3112 return 0;
3113}
3114
3115static void
3116nv50_mstm_cleanup(struct nv50_mstm *mstm)
3117{
3118 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3119 struct drm_encoder *encoder;
3120 int ret;
3121
3122 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
3123 ret = drm_dp_check_act_status(&mstm->mgr);
3124
3125 ret = drm_dp_update_payload_part2(&mstm->mgr);
3126
3127 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3128 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3129 struct nv50_msto *msto = nv50_msto(encoder);
3130 struct nv50_mstc *mstc = msto->mstc;
3131 if (mstc && mstc->mstm == mstm)
3132 nv50_msto_cleanup(msto);
3133 }
3134 }
3135
3136 mstm->modified = false;
3137}
3138
3139static void
3140nv50_mstm_prepare(struct nv50_mstm *mstm)
3141{
3142 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3143 struct drm_encoder *encoder;
3144 int ret;
3145
3146 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
3147 ret = drm_dp_update_payload_part1(&mstm->mgr);
3148
3149 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3150 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3151 struct nv50_msto *msto = nv50_msto(encoder);
3152 struct nv50_mstc *mstc = msto->mstc;
3153 if (mstc && mstc->mstm == mstm)
3154 nv50_msto_prepare(msto);
3155 }
3156 }
3157}
3158
3159static void
3160nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
3161{
3162 struct nv50_mstm *mstm = nv50_mstm(mgr);
3163 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
3164}
3165
3166static void
3167nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
3168 struct drm_connector *connector)
3169{
3170 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3171 struct nv50_mstc *mstc = nv50_mstc(connector);
3172
3173 drm_connector_unregister(&mstc->connector);
3174
3175 drm_modeset_lock_all(drm->dev);
3176 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
3177 mstc->port = NULL;
3178 drm_modeset_unlock_all(drm->dev);
3179
3180 drm_connector_unreference(&mstc->connector);
3181}
3182
3183static void
3184nv50_mstm_register_connector(struct drm_connector *connector)
3185{
3186 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3187
3188 drm_modeset_lock_all(drm->dev);
3189 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
3190 drm_modeset_unlock_all(drm->dev);
3191
3192 drm_connector_register(connector);
3193}
3194
3195static struct drm_connector *
3196nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
3197 struct drm_dp_mst_port *port, const char *path)
3198{
3199 struct nv50_mstm *mstm = nv50_mstm(mgr);
3200 struct nv50_mstc *mstc;
3201 int ret;
3202
3203 ret = nv50_mstc_new(mstm, port, path, &mstc);
3204 if (ret) {
3205 if (mstc)
3206 mstc->connector.funcs->destroy(&mstc->connector);
3207 return NULL;
3208 }
3209
3210 return &mstc->connector;
3211}
3212
3213static const struct drm_dp_mst_topology_cbs
3214nv50_mstm = {
3215 .add_connector = nv50_mstm_add_connector,
3216 .register_connector = nv50_mstm_register_connector,
3217 .destroy_connector = nv50_mstm_destroy_connector,
3218 .hotplug = nv50_mstm_hotplug,
3219};
3220
3221void
3222nv50_mstm_service(struct nv50_mstm *mstm)
3223{
3224 struct drm_dp_aux *aux = mstm->mgr.aux;
3225 bool handled = true;
3226 int ret;
3227 u8 esi[8] = {};
3228
3229 while (handled) {
3230 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
3231 if (ret != 8) {
3232 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3233 return;
3234 }
3235
3236 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
3237 if (!handled)
3238 break;
3239
3240 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
3241 }
3242}
3243
3244void
3245nv50_mstm_remove(struct nv50_mstm *mstm)
3246{
3247 if (mstm)
3248 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3249}
3250
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003251static int
3252nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
3253{
3254 struct nouveau_encoder *outp = mstm->outp;
3255 struct {
3256 struct nv50_disp_mthd_v1 base;
3257 struct nv50_disp_sor_dp_mst_link_v0 mst;
3258 } args = {
3259 .base.version = 1,
3260 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
3261 .base.hasht = outp->dcb->hasht,
3262 .base.hashm = outp->dcb->hashm,
3263 .mst.state = state,
3264 };
3265 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
3266 struct nvif_object *disp = &drm->display->disp;
3267 int ret;
3268
3269 if (dpcd >= 0x12) {
3270 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
3271 if (ret < 0)
3272 return ret;
3273
3274 dpcd &= ~DP_MST_EN;
3275 if (state)
3276 dpcd |= DP_MST_EN;
3277
3278 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
3279 if (ret < 0)
3280 return ret;
3281 }
3282
3283 return nvif_mthd(disp, 0, &args, sizeof(args));
3284}
3285
3286int
3287nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
3288{
3289 int ret, state = 0;
3290
3291 if (!mstm)
3292 return 0;
3293
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10003294 if (dpcd[0] >= 0x12) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003295 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
3296 if (ret < 0)
3297 return ret;
3298
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10003299 if (!(dpcd[1] & DP_MST_CAP))
3300 dpcd[0] = 0x11;
3301 else
3302 state = allow;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003303 }
3304
3305 ret = nv50_mstm_enable(mstm, dpcd[0], state);
3306 if (ret)
3307 return ret;
3308
3309 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
3310 if (ret)
3311 return nv50_mstm_enable(mstm, dpcd[0], 0);
3312
3313 return mstm->mgr.mst_state;
3314}
3315
3316static void
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003317nv50_mstm_fini(struct nv50_mstm *mstm)
3318{
3319 if (mstm && mstm->mgr.mst_state)
3320 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
3321}
3322
3323static void
3324nv50_mstm_init(struct nv50_mstm *mstm)
3325{
3326 if (mstm && mstm->mgr.mst_state)
3327 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
3328}
3329
3330static void
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003331nv50_mstm_del(struct nv50_mstm **pmstm)
3332{
3333 struct nv50_mstm *mstm = *pmstm;
3334 if (mstm) {
3335 kfree(*pmstm);
3336 *pmstm = NULL;
3337 }
3338}
3339
3340static int
3341nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
3342 int conn_base_id, struct nv50_mstm **pmstm)
3343{
3344 const int max_payloads = hweight8(outp->dcb->heads);
3345 struct drm_device *dev = outp->base.base.dev;
3346 struct nv50_mstm *mstm;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003347 int ret, i;
3348 u8 dpcd;
3349
3350 /* This is a workaround for some monitors not functioning
3351 * correctly in MST mode on initial module load. I think
3352 * some bad interaction with the VBIOS may be responsible.
3353 *
3354 * A good ol' off and on again seems to work here ;)
3355 */
3356 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
3357 if (ret >= 0 && dpcd >= 0x12)
3358 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003359
3360 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
3361 return -ENOMEM;
3362 mstm->outp = outp;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003363 mstm->mgr.cbs = &nv50_mstm;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003364
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08003365 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003366 max_payloads, conn_base_id);
3367 if (ret)
3368 return ret;
3369
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003370 for (i = 0; i < max_payloads; i++) {
3371 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
3372 i, &mstm->msto[i]);
3373 if (ret)
3374 return ret;
3375 }
3376
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003377 return 0;
3378}
3379
3380/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003381 * SOR
3382 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003383static void
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003384nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
3385 struct drm_display_mode *mode, u8 proto, u8 depth)
Ben Skeggse84a35a2014-06-05 10:59:55 +10003386{
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003387 struct nv50_dmac *core = &nv50_mast(nv_encoder->base.base.dev)->base;
3388 u32 *push;
3389
3390 if (!mode) {
3391 nv_encoder->ctrl &= ~BIT(head);
3392 if (!(nv_encoder->ctrl & 0x0000000f))
3393 nv_encoder->ctrl = 0;
3394 } else {
3395 nv_encoder->ctrl |= proto << 8;
3396 nv_encoder->ctrl |= BIT(head);
3397 }
3398
3399 if ((push = evo_wait(core, 6))) {
3400 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
3401 if (mode) {
3402 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3403 nv_encoder->ctrl |= 0x00001000;
3404 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3405 nv_encoder->ctrl |= 0x00002000;
3406 nv_encoder->ctrl |= depth << 16;
3407 }
Ben Skeggse84a35a2014-06-05 10:59:55 +10003408 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003409 } else {
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003410 if (mode) {
3411 u32 magic = 0x31ec6000 | (head << 25);
3412 u32 syncs = 0x00000001;
3413 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3414 syncs |= 0x00000008;
3415 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3416 syncs |= 0x00000010;
3417 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3418 magic |= 0x00000001;
3419
3420 evo_mthd(push, 0x0404 + (head * 0x300), 2);
3421 evo_data(push, syncs | (depth << 6));
3422 evo_data(push, magic);
3423 }
Ben Skeggse84a35a2014-06-05 10:59:55 +10003424 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003425 }
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003426 evo_data(push, nv_encoder->ctrl);
3427 evo_kick(push, core);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003428 }
3429}
3430
3431static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003432nv50_sor_disable(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003433{
3434 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003435 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003436
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003437 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10003438
3439 if (nv_crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +10003440 struct nvkm_i2c_aux *aux = nv_encoder->aux;
3441 u8 pwr;
3442
3443 if (aux) {
3444 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
3445 if (ret == 0) {
3446 pwr &= ~DP_SET_POWER_MASK;
3447 pwr |= DP_SET_POWER_D3;
3448 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
3449 }
3450 }
3451
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003452 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003453 nv50_audio_disable(encoder, nv_crtc);
3454 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003455 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003456}
3457
3458static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003459nv50_sor_enable(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003460{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003461 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3462 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10003463 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003464 struct {
3465 struct nv50_disp_mthd_v1 base;
3466 struct nv50_disp_sor_lvds_script_v0 lvds;
3467 } lvds = {
3468 .base.version = 1,
3469 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
3470 .base.hasht = nv_encoder->dcb->hasht,
3471 .base.hashm = nv_encoder->dcb->hashm,
3472 };
Ben Skeggse225f442012-11-21 14:40:21 +10003473 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10003474 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10003475 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003476 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10003477 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003478 u8 proto = 0xf;
3479 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003480
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003481 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003482 nv_encoder->crtc = encoder->crtc;
3483
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003484 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10003485 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003486 if (nv_encoder->dcb->sorconf.link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05003487 proto = 0x1;
3488 /* Only enable dual-link if:
3489 * - Need to (i.e. rate > 165MHz)
3490 * - DCB says we can
3491 * - Not an HDMI monitor, since there's no dual-link
3492 * on HDMI.
3493 */
3494 if (mode->clock >= 165000 &&
3495 nv_encoder->dcb->duallink_possible &&
3496 !drm_detect_hdmi_monitor(nv_connector->edid))
3497 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003498 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003499 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003500 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003501
Ben Skeggsf20c6652016-11-04 17:20:36 +10003502 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003503 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003504 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003505 proto = 0x0;
3506
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003507 if (bios->fp_no_ddc) {
3508 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003509 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003510 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003511 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003512 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10003513 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003514 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003515 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003516 } else
3517 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003518 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003519 }
3520
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003521 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003522 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003523 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003524 } else {
3525 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003526 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003527 }
3528
3529 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003530 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003531 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10003532
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003533 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003534 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003535 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10003536 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003537 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003538 else
3539 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003540 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003541 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10003542 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003543
3544 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003545 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003546 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003547 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003548
3549 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003550 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003551 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10003552 BUG();
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003553 break;
3554 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10003555
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003556 nv_encoder->update(nv_encoder, nv_crtc->index, mode, proto, depth);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003557}
3558
Ben Skeggsf20c6652016-11-04 17:20:36 +10003559static const struct drm_encoder_helper_funcs
3560nv50_sor_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10003561 .atomic_check = nv50_outp_atomic_check,
3562 .enable = nv50_sor_enable,
3563 .disable = nv50_sor_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003564};
3565
Ben Skeggs83fc0832011-07-05 13:08:40 +10003566static void
Ben Skeggse225f442012-11-21 14:40:21 +10003567nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003568{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003569 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3570 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003571 drm_encoder_cleanup(encoder);
3572 kfree(encoder);
3573}
3574
Ben Skeggsf20c6652016-11-04 17:20:36 +10003575static const struct drm_encoder_funcs
3576nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10003577 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10003578};
3579
3580static int
Ben Skeggse225f442012-11-21 14:40:21 +10003581nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003582{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003583 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10003584 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003585 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003586 struct nouveau_encoder *nv_encoder;
3587 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003588 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10003589
3590 switch (dcbe->type) {
3591 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
3592 case DCB_OUTPUT_TMDS:
3593 case DCB_OUTPUT_DP:
3594 default:
3595 type = DRM_MODE_ENCODER_TMDS;
3596 break;
3597 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003598
3599 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3600 if (!nv_encoder)
3601 return -ENOMEM;
3602 nv_encoder->dcb = dcbe;
3603 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003604 nv_encoder->update = nv50_sor_update;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003605
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003606 encoder = to_drm_encoder(nv_encoder);
3607 encoder->possible_crtcs = dcbe->heads;
3608 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003609 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
3610 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003611 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003612
3613 drm_mode_connector_attach_encoder(connector, encoder);
3614
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003615 if (dcbe->type == DCB_OUTPUT_DP) {
3616 struct nvkm_i2c_aux *aux =
3617 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
3618 if (aux) {
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003619 nv_encoder->i2c = &nv_connector->aux.ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003620 nv_encoder->aux = aux;
3621 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003622
3623 /*TODO: Use DP Info Table to check for support. */
3624 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
3625 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
3626 nv_connector->base.base.id,
3627 &nv_encoder->dp.mstm);
3628 if (ret)
3629 return ret;
3630 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003631 } else {
3632 struct nvkm_i2c_bus *bus =
3633 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
3634 if (bus)
3635 nv_encoder->i2c = &bus->i2c;
3636 }
3637
Ben Skeggs83fc0832011-07-05 13:08:40 +10003638 return 0;
3639}
Ben Skeggs26f6d882011-07-04 16:25:18 +10003640
3641/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10003642 * PIOR
3643 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +10003644static int
3645nv50_pior_atomic_check(struct drm_encoder *encoder,
3646 struct drm_crtc_state *crtc_state,
3647 struct drm_connector_state *conn_state)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003648{
Ben Skeggs839ca902016-11-04 17:20:36 +10003649 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
3650 if (ret)
3651 return ret;
3652 crtc_state->adjusted_mode.clock *= 2;
3653 return 0;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003654}
3655
3656static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003657nv50_pior_disable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003658{
Ben Skeggsf20c6652016-11-04 17:20:36 +10003659 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3660 struct nv50_mast *mast = nv50_mast(encoder->dev);
3661 const int or = nv_encoder->or;
3662 u32 *push;
3663
3664 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10003665 push = evo_wait(mast, 4);
3666 if (push) {
3667 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
3668 evo_mthd(push, 0x0700 + (or * 0x040), 1);
3669 evo_data(push, 0x00000000);
3670 }
3671 evo_kick(push, mast);
3672 }
3673 }
3674
3675 nv_encoder->crtc = NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003676}
3677
3678static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003679nv50_pior_enable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003680{
3681 struct nv50_mast *mast = nv50_mast(encoder->dev);
3682 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3683 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3684 struct nouveau_connector *nv_connector;
Ben Skeggs839ca902016-11-04 17:20:36 +10003685 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003686 u8 owner = 1 << nv_crtc->index;
3687 u8 proto, depth;
3688 u32 *push;
3689
3690 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3691 switch (nv_connector->base.display_info.bpc) {
3692 case 10: depth = 0x6; break;
3693 case 8: depth = 0x5; break;
3694 case 6: depth = 0x2; break;
3695 default: depth = 0x0; break;
3696 }
3697
3698 switch (nv_encoder->dcb->type) {
3699 case DCB_OUTPUT_TMDS:
3700 case DCB_OUTPUT_DP:
3701 proto = 0x0;
3702 break;
3703 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10003704 BUG();
Ben Skeggseb6313a2013-02-11 09:52:58 +10003705 break;
3706 }
3707
Ben Skeggseb6313a2013-02-11 09:52:58 +10003708 push = evo_wait(mast, 8);
3709 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003710 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003711 u32 ctrl = (depth << 16) | (proto << 8) | owner;
3712 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3713 ctrl |= 0x00001000;
3714 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3715 ctrl |= 0x00002000;
3716 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
3717 evo_data(push, ctrl);
3718 }
3719
3720 evo_kick(push, mast);
3721 }
3722
3723 nv_encoder->crtc = encoder->crtc;
3724}
3725
Ben Skeggsf20c6652016-11-04 17:20:36 +10003726static const struct drm_encoder_helper_funcs
3727nv50_pior_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10003728 .atomic_check = nv50_pior_atomic_check,
3729 .enable = nv50_pior_enable,
3730 .disable = nv50_pior_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003731};
Ben Skeggseb6313a2013-02-11 09:52:58 +10003732
3733static void
3734nv50_pior_destroy(struct drm_encoder *encoder)
3735{
3736 drm_encoder_cleanup(encoder);
3737 kfree(encoder);
3738}
3739
Ben Skeggsf20c6652016-11-04 17:20:36 +10003740static const struct drm_encoder_funcs
3741nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003742 .destroy = nv50_pior_destroy,
3743};
3744
3745static int
3746nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
3747{
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003748 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003749 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003750 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003751 struct nvkm_i2c_bus *bus = NULL;
3752 struct nvkm_i2c_aux *aux = NULL;
3753 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003754 struct nouveau_encoder *nv_encoder;
3755 struct drm_encoder *encoder;
3756 int type;
3757
3758 switch (dcbe->type) {
3759 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003760 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
3761 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003762 type = DRM_MODE_ENCODER_TMDS;
3763 break;
3764 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003765 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003766 ddc = aux ? &nv_connector->aux.ddc : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003767 type = DRM_MODE_ENCODER_TMDS;
3768 break;
3769 default:
3770 return -ENODEV;
3771 }
3772
3773 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3774 if (!nv_encoder)
3775 return -ENOMEM;
3776 nv_encoder->dcb = dcbe;
3777 nv_encoder->or = ffs(dcbe->or) - 1;
3778 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003779 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003780
3781 encoder = to_drm_encoder(nv_encoder);
3782 encoder->possible_crtcs = dcbe->heads;
3783 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003784 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
3785 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003786 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003787
3788 drm_mode_connector_attach_encoder(connector, encoder);
3789 return 0;
3790}
3791
3792/******************************************************************************
Ben Skeggs839ca902016-11-04 17:20:36 +10003793 * Atomic
3794 *****************************************************************************/
3795
3796static void
3797nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
3798{
3799 struct nv50_disp *disp = nv50_disp(drm->dev);
3800 struct nv50_dmac *core = &disp->mast.base;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003801 struct nv50_mstm *mstm;
3802 struct drm_encoder *encoder;
Ben Skeggs839ca902016-11-04 17:20:36 +10003803 u32 *push;
3804
3805 NV_ATOMIC(drm, "commit core %08x\n", interlock);
3806
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003807 drm_for_each_encoder(encoder, drm->dev) {
3808 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3809 mstm = nouveau_encoder(encoder)->dp.mstm;
3810 if (mstm && mstm->modified)
3811 nv50_mstm_prepare(mstm);
3812 }
3813 }
3814
Ben Skeggs839ca902016-11-04 17:20:36 +10003815 if ((push = evo_wait(core, 5))) {
3816 evo_mthd(push, 0x0084, 1);
3817 evo_data(push, 0x80000000);
3818 evo_mthd(push, 0x0080, 2);
3819 evo_data(push, interlock);
3820 evo_data(push, 0x00000000);
3821 nouveau_bo_wr32(disp->sync, 0, 0x00000000);
3822 evo_kick(push, core);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003823 if (nvif_msec(&drm->client.device, 2000ULL,
Ben Skeggs839ca902016-11-04 17:20:36 +10003824 if (nouveau_bo_rd32(disp->sync, 0))
3825 break;
3826 usleep_range(1, 2);
3827 ) < 0)
3828 NV_ERROR(drm, "EVO timeout\n");
3829 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003830
3831 drm_for_each_encoder(encoder, drm->dev) {
3832 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3833 mstm = nouveau_encoder(encoder)->dp.mstm;
3834 if (mstm && mstm->modified)
3835 nv50_mstm_cleanup(mstm);
3836 }
3837 }
Ben Skeggs839ca902016-11-04 17:20:36 +10003838}
3839
3840static void
3841nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
3842{
3843 struct drm_device *dev = state->dev;
3844 struct drm_crtc_state *crtc_state;
3845 struct drm_crtc *crtc;
3846 struct drm_plane_state *plane_state;
3847 struct drm_plane *plane;
3848 struct nouveau_drm *drm = nouveau_drm(dev);
3849 struct nv50_disp *disp = nv50_disp(dev);
3850 struct nv50_atom *atom = nv50_atom(state);
3851 struct nv50_outp_atom *outp, *outt;
3852 u32 interlock_core = 0;
3853 u32 interlock_chan = 0;
3854 int i;
3855
3856 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
3857 drm_atomic_helper_wait_for_fences(dev, state, false);
3858 drm_atomic_helper_wait_for_dependencies(state);
3859 drm_atomic_helper_update_legacy_modeset_state(dev, state);
3860
3861 if (atom->lock_core)
3862 mutex_lock(&disp->mutex);
3863
3864 /* Disable head(s). */
3865 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3866 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3867 struct nv50_head *head = nv50_head(crtc);
3868
3869 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
3870 asyh->clr.mask, asyh->set.mask);
3871
3872 if (asyh->clr.mask) {
3873 nv50_head_flush_clr(head, asyh, atom->flush_disable);
3874 interlock_core |= 1;
3875 }
3876 }
3877
3878 /* Disable plane(s). */
3879 for_each_plane_in_state(state, plane, plane_state, i) {
3880 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3881 struct nv50_wndw *wndw = nv50_wndw(plane);
3882
3883 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
3884 asyw->clr.mask, asyw->set.mask);
3885 if (!asyw->clr.mask)
3886 continue;
3887
3888 interlock_chan |= nv50_wndw_flush_clr(wndw, interlock_core,
3889 atom->flush_disable,
3890 asyw);
3891 }
3892
3893 /* Disable output path(s). */
3894 list_for_each_entry(outp, &atom->outp, head) {
3895 const struct drm_encoder_helper_funcs *help;
3896 struct drm_encoder *encoder;
3897
3898 encoder = outp->encoder;
3899 help = encoder->helper_private;
3900
3901 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
3902 outp->clr.mask, outp->set.mask);
3903
3904 if (outp->clr.mask) {
3905 help->disable(encoder);
3906 interlock_core |= 1;
3907 if (outp->flush_disable) {
3908 nv50_disp_atomic_commit_core(drm, interlock_chan);
3909 interlock_core = 0;
3910 interlock_chan = 0;
3911 }
3912 }
3913 }
3914
3915 /* Flush disable. */
3916 if (interlock_core) {
3917 if (atom->flush_disable) {
3918 nv50_disp_atomic_commit_core(drm, interlock_chan);
3919 interlock_core = 0;
3920 interlock_chan = 0;
3921 }
3922 }
3923
3924 /* Update output path(s). */
3925 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
3926 const struct drm_encoder_helper_funcs *help;
3927 struct drm_encoder *encoder;
3928
3929 encoder = outp->encoder;
3930 help = encoder->helper_private;
3931
3932 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
3933 outp->set.mask, outp->clr.mask);
3934
3935 if (outp->set.mask) {
3936 help->enable(encoder);
3937 interlock_core = 1;
3938 }
3939
3940 list_del(&outp->head);
3941 kfree(outp);
3942 }
3943
3944 /* Update head(s). */
3945 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3946 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3947 struct nv50_head *head = nv50_head(crtc);
3948
3949 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
3950 asyh->set.mask, asyh->clr.mask);
3951
3952 if (asyh->set.mask) {
3953 nv50_head_flush_set(head, asyh);
3954 interlock_core = 1;
3955 }
3956 }
3957
Ben Skeggs2b507892017-01-24 09:32:26 +10003958 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3959 if (crtc->state->event)
3960 drm_crtc_vblank_get(crtc);
3961 }
3962
Ben Skeggs839ca902016-11-04 17:20:36 +10003963 /* Update plane(s). */
3964 for_each_plane_in_state(state, plane, plane_state, i) {
3965 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3966 struct nv50_wndw *wndw = nv50_wndw(plane);
3967
3968 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
3969 asyw->set.mask, asyw->clr.mask);
3970 if ( !asyw->set.mask &&
3971 (!asyw->clr.mask || atom->flush_disable))
3972 continue;
3973
3974 interlock_chan |= nv50_wndw_flush_set(wndw, interlock_core, asyw);
3975 }
3976
3977 /* Flush update. */
3978 if (interlock_core) {
3979 if (!interlock_chan && atom->state.legacy_cursor_update) {
3980 u32 *push = evo_wait(&disp->mast, 2);
3981 if (push) {
3982 evo_mthd(push, 0x0080, 1);
3983 evo_data(push, 0x00000000);
3984 evo_kick(push, &disp->mast);
3985 }
3986 } else {
3987 nv50_disp_atomic_commit_core(drm, interlock_chan);
3988 }
3989 }
3990
3991 if (atom->lock_core)
3992 mutex_unlock(&disp->mutex);
3993
3994 /* Wait for HW to signal completion. */
3995 for_each_plane_in_state(state, plane, plane_state, i) {
3996 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3997 struct nv50_wndw *wndw = nv50_wndw(plane);
3998 int ret = nv50_wndw_wait_armed(wndw, asyw);
3999 if (ret)
4000 NV_ERROR(drm, "%s: timeout\n", plane->name);
4001 }
4002
4003 for_each_crtc_in_state(state, crtc, crtc_state, i) {
4004 if (crtc->state->event) {
4005 unsigned long flags;
Mario Kleinerbd9f6602016-11-23 07:58:54 +01004006 /* Get correct count/ts if racing with vblank irq */
4007 drm_accurate_vblank_count(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10004008 spin_lock_irqsave(&crtc->dev->event_lock, flags);
4009 drm_crtc_send_vblank_event(crtc, crtc->state->event);
4010 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4011 crtc->state->event = NULL;
Ben Skeggs2b507892017-01-24 09:32:26 +10004012 drm_crtc_vblank_put(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10004013 }
4014 }
4015
4016 drm_atomic_helper_commit_hw_done(state);
4017 drm_atomic_helper_cleanup_planes(dev, state);
4018 drm_atomic_helper_commit_cleanup_done(state);
4019 drm_atomic_state_put(state);
4020}
4021
4022static void
4023nv50_disp_atomic_commit_work(struct work_struct *work)
4024{
4025 struct drm_atomic_state *state =
4026 container_of(work, typeof(*state), commit_work);
4027 nv50_disp_atomic_commit_tail(state);
4028}
4029
4030static int
4031nv50_disp_atomic_commit(struct drm_device *dev,
4032 struct drm_atomic_state *state, bool nonblock)
4033{
4034 struct nouveau_drm *drm = nouveau_drm(dev);
4035 struct nv50_disp *disp = nv50_disp(dev);
4036 struct drm_plane_state *plane_state;
4037 struct drm_plane *plane;
4038 struct drm_crtc *crtc;
4039 bool active = false;
4040 int ret, i;
4041
4042 ret = pm_runtime_get_sync(dev->dev);
4043 if (ret < 0 && ret != -EACCES)
4044 return ret;
4045
4046 ret = drm_atomic_helper_setup_commit(state, nonblock);
4047 if (ret)
4048 goto done;
4049
4050 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
4051
4052 ret = drm_atomic_helper_prepare_planes(dev, state);
4053 if (ret)
4054 goto done;
4055
4056 if (!nonblock) {
4057 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
4058 if (ret)
4059 goto done;
4060 }
4061
4062 for_each_plane_in_state(state, plane, plane_state, i) {
4063 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane_state);
4064 struct nv50_wndw *wndw = nv50_wndw(plane);
4065 if (asyw->set.image) {
4066 asyw->ntfy.handle = wndw->dmac->sync.handle;
4067 asyw->ntfy.offset = wndw->ntfy;
4068 asyw->ntfy.awaken = false;
4069 asyw->set.ntfy = true;
4070 nouveau_bo_wr32(disp->sync, wndw->ntfy / 4, 0x00000000);
4071 wndw->ntfy ^= 0x10;
4072 }
4073 }
4074
4075 drm_atomic_helper_swap_state(state, true);
4076 drm_atomic_state_get(state);
4077
4078 if (nonblock)
4079 queue_work(system_unbound_wq, &state->commit_work);
4080 else
4081 nv50_disp_atomic_commit_tail(state);
4082
4083 drm_for_each_crtc(crtc, dev) {
4084 if (crtc->state->enable) {
4085 if (!drm->have_disp_power_ref) {
4086 drm->have_disp_power_ref = true;
4087 return ret;
4088 }
4089 active = true;
4090 break;
4091 }
4092 }
4093
4094 if (!active && drm->have_disp_power_ref) {
4095 pm_runtime_put_autosuspend(dev->dev);
4096 drm->have_disp_power_ref = false;
4097 }
4098
4099done:
4100 pm_runtime_put_autosuspend(dev->dev);
4101 return ret;
4102}
4103
4104static struct nv50_outp_atom *
4105nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
4106{
4107 struct nv50_outp_atom *outp;
4108
4109 list_for_each_entry(outp, &atom->outp, head) {
4110 if (outp->encoder == encoder)
4111 return outp;
4112 }
4113
4114 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
4115 if (!outp)
4116 return ERR_PTR(-ENOMEM);
4117
4118 list_add(&outp->head, &atom->outp);
4119 outp->encoder = encoder;
4120 return outp;
4121}
4122
4123static int
4124nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
4125 struct drm_connector *connector)
4126{
4127 struct drm_encoder *encoder = connector->state->best_encoder;
4128 struct drm_crtc_state *crtc_state;
4129 struct drm_crtc *crtc;
4130 struct nv50_outp_atom *outp;
4131
4132 if (!(crtc = connector->state->crtc))
4133 return 0;
4134
4135 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
4136 if (crtc->state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
4137 outp = nv50_disp_outp_atomic_add(atom, encoder);
4138 if (IS_ERR(outp))
4139 return PTR_ERR(outp);
4140
4141 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
4142 outp->flush_disable = true;
4143 atom->flush_disable = true;
4144 }
4145 outp->clr.ctrl = true;
4146 atom->lock_core = true;
4147 }
4148
4149 return 0;
4150}
4151
4152static int
4153nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
4154 struct drm_connector_state *connector_state)
4155{
4156 struct drm_encoder *encoder = connector_state->best_encoder;
4157 struct drm_crtc_state *crtc_state;
4158 struct drm_crtc *crtc;
4159 struct nv50_outp_atom *outp;
4160
4161 if (!(crtc = connector_state->crtc))
4162 return 0;
4163
4164 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
4165 if (crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
4166 outp = nv50_disp_outp_atomic_add(atom, encoder);
4167 if (IS_ERR(outp))
4168 return PTR_ERR(outp);
4169
4170 outp->set.ctrl = true;
4171 atom->lock_core = true;
4172 }
4173
4174 return 0;
4175}
4176
4177static int
4178nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
4179{
4180 struct nv50_atom *atom = nv50_atom(state);
4181 struct drm_connector_state *connector_state;
4182 struct drm_connector *connector;
4183 int ret, i;
4184
4185 ret = drm_atomic_helper_check(dev, state);
4186 if (ret)
4187 return ret;
4188
4189 for_each_connector_in_state(state, connector, connector_state, i) {
4190 ret = nv50_disp_outp_atomic_check_clr(atom, connector);
4191 if (ret)
4192 return ret;
4193
4194 ret = nv50_disp_outp_atomic_check_set(atom, connector_state);
4195 if (ret)
4196 return ret;
4197 }
4198
4199 return 0;
4200}
4201
4202static void
4203nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
4204{
4205 struct nv50_atom *atom = nv50_atom(state);
4206 struct nv50_outp_atom *outp, *outt;
4207
4208 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
4209 list_del(&outp->head);
4210 kfree(outp);
4211 }
4212
4213 drm_atomic_state_default_clear(state);
4214}
4215
4216static void
4217nv50_disp_atomic_state_free(struct drm_atomic_state *state)
4218{
4219 struct nv50_atom *atom = nv50_atom(state);
4220 drm_atomic_state_default_release(&atom->state);
4221 kfree(atom);
4222}
4223
4224static struct drm_atomic_state *
4225nv50_disp_atomic_state_alloc(struct drm_device *dev)
4226{
4227 struct nv50_atom *atom;
4228 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
4229 drm_atomic_state_init(dev, &atom->state) < 0) {
4230 kfree(atom);
4231 return NULL;
4232 }
4233 INIT_LIST_HEAD(&atom->outp);
4234 return &atom->state;
4235}
4236
4237static const struct drm_mode_config_funcs
4238nv50_disp_func = {
4239 .fb_create = nouveau_user_framebuffer_create,
4240 .output_poll_changed = nouveau_fbcon_output_poll_changed,
4241 .atomic_check = nv50_disp_atomic_check,
4242 .atomic_commit = nv50_disp_atomic_commit,
4243 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
4244 .atomic_state_clear = nv50_disp_atomic_state_clear,
4245 .atomic_state_free = nv50_disp_atomic_state_free,
4246};
4247
4248/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10004249 * Init
4250 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10004251
Ben Skeggs2a44e492011-11-09 11:36:33 +10004252void
Ben Skeggse225f442012-11-21 14:40:21 +10004253nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004254{
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004255 struct nouveau_encoder *nv_encoder;
4256 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10004257 struct drm_plane *plane;
4258
4259 drm_for_each_plane(plane, dev) {
4260 struct nv50_wndw *wndw = nv50_wndw(plane);
4261 if (plane->funcs != &nv50_wndw)
4262 continue;
4263 nv50_wndw_fini(wndw);
4264 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004265
4266 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4267 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
4268 nv_encoder = nouveau_encoder(encoder);
4269 nv50_mstm_fini(nv_encoder->dp.mstm);
4270 }
4271 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10004272}
4273
4274int
Ben Skeggse225f442012-11-21 14:40:21 +10004275nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004276{
Ben Skeggs354d3502016-11-04 17:20:36 +10004277 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10004278 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004279 struct drm_crtc *crtc;
4280 u32 *push;
4281
4282 push = evo_wait(nv50_mast(dev), 32);
4283 if (!push)
4284 return -EBUSY;
4285
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004286 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10004287 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004288 evo_kick(push, nv50_mast(dev));
Ben Skeggs973f10c2016-11-04 17:20:36 +10004289
Ben Skeggs354d3502016-11-04 17:20:36 +10004290 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4291 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
Ben Skeggs9c5753b2017-05-19 23:59:35 +10004292 struct nouveau_encoder *nv_encoder =
4293 nouveau_encoder(encoder);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004294 nv50_mstm_init(nv_encoder->dp.mstm);
Ben Skeggs354d3502016-11-04 17:20:36 +10004295 }
4296 }
4297
Ben Skeggse1ef6b42016-11-04 17:20:36 +10004298 drm_for_each_crtc(crtc, dev) {
Ben Skeggs9bfdee92016-11-04 17:20:36 +10004299 nv50_head_lut_load(crtc);
Ben Skeggse1ef6b42016-11-04 17:20:36 +10004300 }
4301
Ben Skeggs973f10c2016-11-04 17:20:36 +10004302 drm_for_each_plane(plane, dev) {
4303 struct nv50_wndw *wndw = nv50_wndw(plane);
4304 if (plane->funcs != &nv50_wndw)
4305 continue;
4306 nv50_wndw_init(wndw);
4307 }
4308
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004309 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004310}
4311
4312void
Ben Skeggse225f442012-11-21 14:40:21 +10004313nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004314{
Ben Skeggse225f442012-11-21 14:40:21 +10004315 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004316
Ben Skeggs0ad72862014-08-10 04:10:22 +10004317 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10004318
Ben Skeggs816af2f2011-11-16 15:48:48 +10004319 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004320 if (disp->sync)
4321 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10004322 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10004323
Ben Skeggs77145f12012-07-31 16:16:21 +10004324 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004325 kfree(disp);
4326}
4327
Ben Skeggs839ca902016-11-04 17:20:36 +10004328MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
4329static int nouveau_atomic = 0;
4330module_param_named(atomic, nouveau_atomic, int, 0400);
4331
Ben Skeggs26f6d882011-07-04 16:25:18 +10004332int
Ben Skeggse225f442012-11-21 14:40:21 +10004333nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004334{
Ben Skeggs1167c6b2016-05-18 13:57:42 +10004335 struct nvif_device *device = &nouveau_drm(dev)->client.device;
Ben Skeggs77145f12012-07-31 16:16:21 +10004336 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10004337 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004338 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10004339 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10004340 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10004341 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004342
4343 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
4344 if (!disp)
4345 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10004346
Ben Skeggs839ca902016-11-04 17:20:36 +10004347 mutex_init(&disp->mutex);
4348
Ben Skeggs77145f12012-07-31 16:16:21 +10004349 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10004350 nouveau_display(dev)->dtor = nv50_display_destroy;
4351 nouveau_display(dev)->init = nv50_display_init;
4352 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs0ad72862014-08-10 04:10:22 +10004353 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs839ca902016-11-04 17:20:36 +10004354 dev->mode_config.funcs = &nv50_disp_func;
4355 if (nouveau_atomic)
4356 dev->driver->driver_features |= DRIVER_ATOMIC;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004357
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004358 /* small shared memory area we use for notifiers and semaphores */
Ben Skeggsbab7cc12016-05-24 17:26:48 +10004359 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01004360 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004361 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10004362 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004363 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004364 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004365 if (ret)
4366 nouveau_bo_unpin(disp->sync);
4367 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004368 if (ret)
4369 nouveau_bo_ref(NULL, &disp->sync);
4370 }
4371
4372 if (ret)
4373 goto out;
4374
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004375 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10004376 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10004377 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004378 if (ret)
4379 goto out;
4380
Ben Skeggs438d99e2011-07-05 16:48:06 +10004381 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10004382 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsa01ca782015-08-20 14:54:15 +10004383 crtcs = nvif_rd32(&device->object, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10004384 else
4385 crtcs = 2;
4386
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10004387 for (i = 0; i < crtcs; i++) {
Ben Skeggs9bfdee92016-11-04 17:20:36 +10004388 ret = nv50_head_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10004389 if (ret)
4390 goto out;
4391 }
4392
Ben Skeggs83fc0832011-07-05 13:08:40 +10004393 /* create encoder/connector objects based on VBIOS DCB table */
4394 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
4395 connector = nouveau_connector_create(dev, dcbe->connector);
4396 if (IS_ERR(connector))
4397 continue;
4398
Ben Skeggseb6313a2013-02-11 09:52:58 +10004399 if (dcbe->location == DCB_LOC_ON_CHIP) {
4400 switch (dcbe->type) {
4401 case DCB_OUTPUT_TMDS:
4402 case DCB_OUTPUT_LVDS:
4403 case DCB_OUTPUT_DP:
4404 ret = nv50_sor_create(connector, dcbe);
4405 break;
4406 case DCB_OUTPUT_ANALOG:
4407 ret = nv50_dac_create(connector, dcbe);
4408 break;
4409 default:
4410 ret = -ENODEV;
4411 break;
4412 }
4413 } else {
4414 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004415 }
4416
Ben Skeggseb6313a2013-02-11 09:52:58 +10004417 if (ret) {
4418 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
4419 dcbe->location, dcbe->type,
4420 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10004421 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004422 }
4423 }
4424
4425 /* cull any connectors we created that don't have an encoder */
4426 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
4427 if (connector->encoder_ids[0])
4428 continue;
4429
Ben Skeggs77145f12012-07-31 16:16:21 +10004430 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03004431 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004432 connector->funcs->destroy(connector);
4433 }
4434
Ben Skeggs26f6d882011-07-04 16:25:18 +10004435out:
4436 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10004437 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004438 return ret;
4439}