blob: 83eee1f003365d60cf21099a6bdcc0bf83229b8a [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>
Ben Skeggs83fc0832011-07-05 13:08:40 +100026
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
Ben Skeggsad633612016-11-04 17:20:36 +100028#include <drm/drm_atomic.h>
Ben Skeggs973f10c2016-11-04 17:20:36 +100029#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drm_crtc_helper.h>
Ben Skeggs48743222014-05-31 01:48:06 +100031#include <drm/drm_dp_helper.h>
Daniel Vetterb516a9e2015-12-04 09:45:43 +010032#include <drm/drm_fb_helper.h>
Ben Skeggsad633612016-11-04 17:20:36 +100033#include <drm/drm_plane_helper.h>
Ben Skeggs26f6d882011-07-04 16:25:18 +100034
Ben Skeggsfdb751e2014-08-10 04:10:23 +100035#include <nvif/class.h>
Ben Skeggs845f2722015-11-08 12:16:40 +100036#include <nvif/cl0002.h>
Ben Skeggs7568b102015-11-08 10:44:19 +100037#include <nvif/cl5070.h>
38#include <nvif/cl507a.h>
39#include <nvif/cl507b.h>
40#include <nvif/cl507c.h>
41#include <nvif/cl507d.h>
42#include <nvif/cl507e.h>
Ben Skeggs973f10c2016-11-04 17:20:36 +100043#include <nvif/event.h>
Ben Skeggsfdb751e2014-08-10 04:10:23 +100044
Ben Skeggs4dc28132016-05-20 09:22:55 +100045#include "nouveau_drv.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100046#include "nouveau_dma.h"
47#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100048#include "nouveau_connector.h"
49#include "nouveau_encoder.h"
50#include "nouveau_crtc.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100051#include "nouveau_fence.h"
Ben Skeggs839ca902016-11-04 17:20:36 +100052#include "nouveau_fbcon.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100053#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100054
Ben Skeggs8a464382011-11-12 23:52:07 +100055#define EVO_DMA_NR 9
56
Ben Skeggsbdb8c212011-11-12 01:30:24 +100057#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100058#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100059#define EVO_OVLY(c) (0x05 + (c))
60#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100061#define EVO_CURS(c) (0x0d + (c))
62
Ben Skeggs816af2f2011-11-16 15:48:48 +100063/* offsets in shared sync bo of various structures */
64#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100065#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
66#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
67#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs973f10c2016-11-04 17:20:36 +100068#define EVO_FLIP_NTFY0(c) EVO_SYNC((c) + 1, 0x20)
69#define EVO_FLIP_NTFY1(c) EVO_SYNC((c) + 1, 0x30)
Ben Skeggs816af2f2011-11-16 15:48:48 +100070
Ben Skeggsb5a794b2012-10-16 14:18:32 +100071/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +100072 * Atomic state
73 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +100074#define nv50_atom(p) container_of((p), struct nv50_atom, state)
75
76struct nv50_atom {
77 struct drm_atomic_state state;
78
79 struct list_head outp;
80 bool lock_core;
81 bool flush_disable;
82};
83
84struct nv50_outp_atom {
85 struct list_head head;
86
87 struct drm_encoder *encoder;
88 bool flush_disable;
89
90 union {
91 struct {
92 bool ctrl:1;
93 };
94 u8 mask;
95 } clr;
96
97 union {
98 struct {
99 bool ctrl:1;
100 };
101 u8 mask;
102 } set;
103};
104
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000105#define nv50_head_atom(p) container_of((p), struct nv50_head_atom, state)
106
107struct nv50_head_atom {
108 struct drm_crtc_state state;
109
Ben Skeggsc4e68122016-11-04 17:20:36 +1000110 struct {
111 u16 iW;
112 u16 iH;
113 u16 oW;
114 u16 oH;
115 } view;
116
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000117 struct nv50_head_mode {
118 bool interlace;
119 u32 clock;
120 struct {
121 u16 active;
122 u16 synce;
123 u16 blanke;
124 u16 blanks;
125 } h;
126 struct {
127 u32 active;
128 u16 synce;
129 u16 blanke;
130 u16 blanks;
131 u16 blank2s;
132 u16 blank2e;
133 u16 blankus;
134 } v;
135 } mode;
136
Ben Skeggsad633612016-11-04 17:20:36 +1000137 struct {
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000138 u32 handle;
139 u64 offset:40;
140 } lut;
141
142 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000143 bool visible;
144 u32 handle;
145 u64 offset:40;
146 u8 format;
147 u8 kind:7;
148 u8 layout:1;
149 u8 block:4;
150 u32 pitch:20;
151 u16 x;
152 u16 y;
153 u16 w;
154 u16 h;
155 } core;
156
157 struct {
Ben Skeggsea8ee392016-11-04 17:20:36 +1000158 bool visible;
159 u32 handle;
160 u64 offset:40;
161 u8 layout:1;
162 u8 format:1;
163 } curs;
164
165 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000166 u8 depth;
167 u8 cpp;
168 u16 x;
169 u16 y;
170 u16 w;
171 u16 h;
172 } base;
173
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000174 struct {
175 u8 cpp;
176 } ovly;
177
Ben Skeggs7e918332016-11-04 17:20:36 +1000178 struct {
179 bool enable:1;
180 u8 bits:2;
181 u8 mode:4;
182 } dither;
183
Ben Skeggs7e08d672016-11-04 17:20:36 +1000184 struct {
185 struct {
186 u16 cos:12;
187 u16 sin:12;
188 } sat;
189 } procamp;
190
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000191 union {
192 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000193 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000194 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000195 };
196 u8 mask;
197 } clr;
198
199 union {
200 struct {
201 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000202 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000203 bool view:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000204 bool mode:1;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000205 bool base:1;
206 bool ovly:1;
Ben Skeggs7e918332016-11-04 17:20:36 +1000207 bool dither:1;
Ben Skeggs7e08d672016-11-04 17:20:36 +1000208 bool procamp:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000209 };
210 u16 mask;
211 } set;
212};
213
Ben Skeggs839ca902016-11-04 17:20:36 +1000214static inline struct nv50_head_atom *
215nv50_head_atom_get(struct drm_atomic_state *state, struct drm_crtc *crtc)
216{
217 struct drm_crtc_state *statec = drm_atomic_get_crtc_state(state, crtc);
218 if (IS_ERR(statec))
219 return (void *)statec;
220 return nv50_head_atom(statec);
221}
222
Ben Skeggs973f10c2016-11-04 17:20:36 +1000223#define nv50_wndw_atom(p) container_of((p), struct nv50_wndw_atom, state)
224
225struct nv50_wndw_atom {
226 struct drm_plane_state state;
227 u8 interval;
228
229 struct drm_rect clip;
230
231 struct {
232 u32 handle;
233 u16 offset:12;
234 bool awaken:1;
235 } ntfy;
236
237 struct {
238 u32 handle;
239 u16 offset:12;
240 u32 acquire;
241 u32 release;
242 } sema;
243
244 struct {
245 u8 enable:2;
246 } lut;
247
248 struct {
249 u8 mode:2;
250 u8 interval:4;
251
252 u8 format;
253 u8 kind:7;
254 u8 layout:1;
255 u8 block:4;
256 u32 pitch:20;
257 u16 w;
258 u16 h;
259
260 u32 handle;
261 u64 offset;
262 } image;
263
264 struct {
265 u16 x;
266 u16 y;
267 } point;
268
269 union {
270 struct {
271 bool ntfy:1;
272 bool sema:1;
273 bool image:1;
274 };
275 u8 mask;
276 } clr;
277
278 union {
279 struct {
280 bool ntfy:1;
281 bool sema:1;
282 bool image:1;
283 bool lut:1;
284 bool point:1;
285 };
286 u8 mask;
287 } set;
288};
289
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000290/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000291 * EVO channel
292 *****************************************************************************/
293
Ben Skeggse225f442012-11-21 14:40:21 +1000294struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000295 struct nvif_object user;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000296 struct nvif_device *device;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000297};
298
299static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000300nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000301 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000302 struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000303{
Ben Skeggs41a63402015-08-20 14:54:16 +1000304 struct nvif_sclass *sclass;
305 int ret, i, n;
Ben Skeggs6af52892014-11-03 15:01:33 +1000306
Ben Skeggsa01ca782015-08-20 14:54:15 +1000307 chan->device = device;
308
Ben Skeggs41a63402015-08-20 14:54:16 +1000309 ret = n = nvif_object_sclass_get(disp, &sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000310 if (ret < 0)
311 return ret;
312
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000313 while (oclass[0]) {
Ben Skeggs41a63402015-08-20 14:54:16 +1000314 for (i = 0; i < n; i++) {
315 if (sclass[i].oclass == oclass[0]) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000316 ret = nvif_object_init(disp, 0, oclass[0],
Ben Skeggsa01ca782015-08-20 14:54:15 +1000317 data, size, &chan->user);
Ben Skeggs6af52892014-11-03 15:01:33 +1000318 if (ret == 0)
319 nvif_object_map(&chan->user);
Ben Skeggs41a63402015-08-20 14:54:16 +1000320 nvif_object_sclass_put(&sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000321 return ret;
322 }
Ben Skeggsb76f1522014-08-10 04:10:28 +1000323 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000324 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000325 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000326
Ben Skeggs41a63402015-08-20 14:54:16 +1000327 nvif_object_sclass_put(&sclass);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000328 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000329}
330
331static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000332nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000333{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000334 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000335}
336
337/******************************************************************************
338 * PIO EVO channel
339 *****************************************************************************/
340
Ben Skeggse225f442012-11-21 14:40:21 +1000341struct nv50_pioc {
342 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000343};
344
345static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000346nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000347{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000348 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000349}
350
351static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000352nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000353 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000354 struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000355{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000356 return nv50_chan_create(device, disp, oclass, head, data, size,
357 &pioc->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000358}
359
360/******************************************************************************
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000361 * Overlay Immediate
362 *****************************************************************************/
363
364struct nv50_oimm {
365 struct nv50_pioc base;
366};
367
368static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000369nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
370 int head, struct nv50_oimm *oimm)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000371{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000372 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000373 .head = head,
374 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000375 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000376 GK104_DISP_OVERLAY,
377 GF110_DISP_OVERLAY,
378 GT214_DISP_OVERLAY,
379 G82_DISP_OVERLAY,
380 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000381 0
382 };
383
Ben Skeggsa01ca782015-08-20 14:54:15 +1000384 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
385 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000386}
387
388/******************************************************************************
389 * DMA EVO channel
390 *****************************************************************************/
391
Ben Skeggsaccdea22016-11-04 17:20:36 +1000392struct nv50_dmac_ctxdma {
393 struct list_head head;
394 struct nvif_object object;
395};
396
Ben Skeggse225f442012-11-21 14:40:21 +1000397struct nv50_dmac {
398 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000399 dma_addr_t handle;
400 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100401
Ben Skeggs0ad72862014-08-10 04:10:22 +1000402 struct nvif_object sync;
403 struct nvif_object vram;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000404 struct list_head ctxdma;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000405
Daniel Vetter59ad1462012-12-02 14:49:44 +0100406 /* Protects against concurrent pushbuf access to this channel, lock is
407 * grabbed by evo_wait (if the pushbuf reservation is successful) and
408 * dropped again by evo_kick. */
409 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000410};
411
412static void
Ben Skeggsaccdea22016-11-04 17:20:36 +1000413nv50_dmac_ctxdma_del(struct nv50_dmac_ctxdma *ctxdma)
414{
415 nvif_object_fini(&ctxdma->object);
416 list_del(&ctxdma->head);
417 kfree(ctxdma);
418}
419
420static struct nv50_dmac_ctxdma *
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000421nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, struct nouveau_framebuffer *fb)
Ben Skeggsaccdea22016-11-04 17:20:36 +1000422{
423 struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
424 struct nv50_dmac_ctxdma *ctxdma;
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000425 const u8 kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
426 const u32 handle = 0xfb000000 | kind;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000427 struct {
428 struct nv_dma_v0 base;
429 union {
430 struct nv50_dma_v0 nv50;
431 struct gf100_dma_v0 gf100;
432 struct gf119_dma_v0 gf119;
433 };
434 } args = {};
435 u32 argc = sizeof(args.base);
436 int ret;
437
438 list_for_each_entry(ctxdma, &dmac->ctxdma, head) {
439 if (ctxdma->object.handle == handle)
440 return ctxdma;
441 }
442
443 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
444 return ERR_PTR(-ENOMEM);
445 list_add(&ctxdma->head, &dmac->ctxdma);
446
447 args.base.target = NV_DMA_V0_TARGET_VRAM;
448 args.base.access = NV_DMA_V0_ACCESS_RDWR;
449 args.base.start = 0;
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000450 args.base.limit = drm->client.device.info.ram_user - 1;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000451
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000452 if (drm->client.device.info.chipset < 0x80) {
Ben Skeggsaccdea22016-11-04 17:20:36 +1000453 args.nv50.part = NV50_DMA_V0_PART_256;
454 argc += sizeof(args.nv50);
455 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000456 if (drm->client.device.info.chipset < 0xc0) {
Ben Skeggsaccdea22016-11-04 17:20:36 +1000457 args.nv50.part = NV50_DMA_V0_PART_256;
458 args.nv50.kind = kind;
459 argc += sizeof(args.nv50);
460 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000461 if (drm->client.device.info.chipset < 0xd0) {
Ben Skeggsaccdea22016-11-04 17:20:36 +1000462 args.gf100.kind = kind;
463 argc += sizeof(args.gf100);
464 } else {
465 args.gf119.page = GF119_DMA_V0_PAGE_LP;
466 args.gf119.kind = kind;
467 argc += sizeof(args.gf119);
468 }
469
470 ret = nvif_object_init(&dmac->base.user, handle, NV_DMA_IN_MEMORY,
471 &args, argc, &ctxdma->object);
472 if (ret) {
473 nv50_dmac_ctxdma_del(ctxdma);
474 return ERR_PTR(ret);
475 }
476
477 return ctxdma;
478}
479
480static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000481nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000482{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000483 struct nvif_device *device = dmac->base.device;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000484 struct nv50_dmac_ctxdma *ctxdma, *ctxtmp;
485
486 list_for_each_entry_safe(ctxdma, ctxtmp, &dmac->ctxdma, head) {
487 nv50_dmac_ctxdma_del(ctxdma);
488 }
Ben Skeggsa01ca782015-08-20 14:54:15 +1000489
Ben Skeggs0ad72862014-08-10 04:10:22 +1000490 nvif_object_fini(&dmac->vram);
491 nvif_object_fini(&dmac->sync);
492
493 nv50_chan_destroy(&dmac->base);
494
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000495 if (dmac->ptr) {
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000496 struct device *dev = nvxx_device(device)->dev;
497 dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000498 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000499}
500
501static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000502nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000503 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000504 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000505{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000506 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000507 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000508 int ret;
509
Daniel Vetter59ad1462012-12-02 14:49:44 +0100510 mutex_init(&dmac->lock);
511
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000512 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
513 &dmac->handle, GFP_KERNEL);
Ben Skeggs47057302012-11-16 13:58:48 +1000514 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000515 return -ENOMEM;
516
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000517 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
518 &(struct nv_dma_v0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +1000519 .target = NV_DMA_V0_TARGET_PCI_US,
520 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000521 .start = dmac->handle + 0x0000,
522 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000523 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000524 if (ret)
525 return ret;
526
Ben Skeggsbf81df92015-08-20 14:54:16 +1000527 args->pushbuf = nvif_handle(&pushbuf);
528
Ben Skeggsa01ca782015-08-20 14:54:15 +1000529 ret = nv50_chan_create(device, disp, oclass, head, data, size,
530 &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000531 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000532 if (ret)
533 return ret;
534
Ben Skeggsa01ca782015-08-20 14:54:15 +1000535 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000536 &(struct nv_dma_v0) {
537 .target = NV_DMA_V0_TARGET_VRAM,
538 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000539 .start = syncbuf + 0x0000,
540 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000541 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000542 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000543 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000544 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000545
Ben Skeggsa01ca782015-08-20 14:54:15 +1000546 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000547 &(struct nv_dma_v0) {
548 .target = NV_DMA_V0_TARGET_VRAM,
549 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000550 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000551 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000552 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000553 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000554 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000555 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000556
Ben Skeggsaccdea22016-11-04 17:20:36 +1000557 INIT_LIST_HEAD(&dmac->ctxdma);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000558 return ret;
559}
560
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000561/******************************************************************************
562 * Core
563 *****************************************************************************/
564
Ben Skeggse225f442012-11-21 14:40:21 +1000565struct nv50_mast {
566 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000567};
568
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000569static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000570nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
571 u64 syncbuf, struct nv50_mast *core)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000572{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000573 struct nv50_disp_core_channel_dma_v0 args = {
574 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000575 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000576 static const s32 oclass[] = {
Ben Skeggsed828662016-11-16 15:03:07 +1000577 GP102_DISP_CORE_CHANNEL_DMA,
Ben Skeggsf9d5cbb2016-07-09 10:41:01 +1000578 GP100_DISP_CORE_CHANNEL_DMA,
Ben Skeggsdb1eb522016-02-11 08:35:32 +1000579 GM200_DISP_CORE_CHANNEL_DMA,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000580 GM107_DISP_CORE_CHANNEL_DMA,
581 GK110_DISP_CORE_CHANNEL_DMA,
582 GK104_DISP_CORE_CHANNEL_DMA,
583 GF110_DISP_CORE_CHANNEL_DMA,
584 GT214_DISP_CORE_CHANNEL_DMA,
585 GT206_DISP_CORE_CHANNEL_DMA,
586 GT200_DISP_CORE_CHANNEL_DMA,
587 G82_DISP_CORE_CHANNEL_DMA,
588 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000589 0
590 };
591
Ben Skeggsa01ca782015-08-20 14:54:15 +1000592 return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
593 syncbuf, &core->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000594}
595
596/******************************************************************************
597 * Base
598 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000599
Ben Skeggse225f442012-11-21 14:40:21 +1000600struct nv50_sync {
601 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000602 u32 addr;
603 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000604};
605
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000606static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000607nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
608 int head, u64 syncbuf, struct nv50_sync *base)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000609{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000610 struct nv50_disp_base_channel_dma_v0 args = {
611 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000612 .head = head,
613 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000614 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000615 GK110_DISP_BASE_CHANNEL_DMA,
616 GK104_DISP_BASE_CHANNEL_DMA,
617 GF110_DISP_BASE_CHANNEL_DMA,
618 GT214_DISP_BASE_CHANNEL_DMA,
619 GT200_DISP_BASE_CHANNEL_DMA,
620 G82_DISP_BASE_CHANNEL_DMA,
621 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000622 0
623 };
624
Ben Skeggsa01ca782015-08-20 14:54:15 +1000625 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000626 syncbuf, &base->base);
627}
628
629/******************************************************************************
630 * Overlay
631 *****************************************************************************/
632
Ben Skeggse225f442012-11-21 14:40:21 +1000633struct nv50_ovly {
634 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000635};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000636
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000637static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000638nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
639 int head, u64 syncbuf, struct nv50_ovly *ovly)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000640{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000641 struct nv50_disp_overlay_channel_dma_v0 args = {
642 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000643 .head = head,
644 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000645 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000646 GK104_DISP_OVERLAY_CONTROL_DMA,
647 GF110_DISP_OVERLAY_CONTROL_DMA,
648 GT214_DISP_OVERLAY_CHANNEL_DMA,
649 GT200_DISP_OVERLAY_CHANNEL_DMA,
650 G82_DISP_OVERLAY_CHANNEL_DMA,
651 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000652 0
653 };
654
Ben Skeggsa01ca782015-08-20 14:54:15 +1000655 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000656 syncbuf, &ovly->base);
657}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000658
Ben Skeggse225f442012-11-21 14:40:21 +1000659struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000660 struct nouveau_crtc base;
Ben Skeggse225f442012-11-21 14:40:21 +1000661 struct nv50_ovly ovly;
662 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000663};
664
Ben Skeggse225f442012-11-21 14:40:21 +1000665#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
Ben Skeggse225f442012-11-21 14:40:21 +1000666#define nv50_ovly(c) (&nv50_head(c)->ovly)
667#define nv50_oimm(c) (&nv50_head(c)->oimm)
668#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000669#define nv50_vers(c) nv50_chan(c)->user.oclass
670
Ben Skeggse225f442012-11-21 14:40:21 +1000671struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000672 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000673 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000674
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000675 struct nouveau_bo *sync;
Ben Skeggs839ca902016-11-04 17:20:36 +1000676
677 struct mutex mutex;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000678};
679
Ben Skeggse225f442012-11-21 14:40:21 +1000680static struct nv50_disp *
681nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000682{
Ben Skeggs77145f12012-07-31 16:16:21 +1000683 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000684}
685
Ben Skeggse225f442012-11-21 14:40:21 +1000686#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000687
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000688/******************************************************************************
689 * EVO channel helpers
690 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000691static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000692evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000693{
Ben Skeggse225f442012-11-21 14:40:21 +1000694 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000695 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000696 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000697
Daniel Vetter59ad1462012-12-02 14:49:44 +0100698 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000699 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000700 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000701
Ben Skeggs0ad72862014-08-10 04:10:22 +1000702 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000703 if (nvif_msec(device, 2000,
704 if (!nvif_rd32(&dmac->base.user, 0x0004))
705 break;
706 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100707 mutex_unlock(&dmac->lock);
Joe Perches8dfe1622017-02-28 04:55:54 -0800708 pr_err("nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000709 return NULL;
710 }
711
712 put = 0;
713 }
714
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000715 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000716}
717
718static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000719evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000720{
Ben Skeggse225f442012-11-21 14:40:21 +1000721 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000722 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100723 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000724}
725
Joe Perches8dfe1622017-02-28 04:55:54 -0800726#define evo_mthd(p, m, s) do { \
727 const u32 _m = (m), _s = (s); \
728 if (drm_debug & DRM_UT_KMS) \
729 pr_err("%04x %d %s\n", _m, _s, __func__); \
730 *((p)++) = ((_s << 18) | _m); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000731} while(0)
Ben Skeggs7f55a072016-11-04 17:20:36 +1000732
Joe Perches8dfe1622017-02-28 04:55:54 -0800733#define evo_data(p, d) do { \
734 const u32 _d = (d); \
735 if (drm_debug & DRM_UT_KMS) \
736 pr_err("\t%08x\n", _d); \
737 *((p)++) = _d; \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000738} while(0)
Ben Skeggs51beb422011-07-05 10:33:08 +1000739
Ben Skeggs3376ee32011-11-12 14:28:12 +1000740/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +1000741 * Plane
742 *****************************************************************************/
743#define nv50_wndw(p) container_of((p), struct nv50_wndw, plane)
744
745struct nv50_wndw {
746 const struct nv50_wndw_func *func;
747 struct nv50_dmac *dmac;
748
749 struct drm_plane plane;
750
751 struct nvif_notify notify;
752 u16 ntfy;
753 u16 sema;
754 u32 data;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000755};
756
757struct nv50_wndw_func {
758 void *(*dtor)(struct nv50_wndw *);
759 int (*acquire)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
760 struct nv50_head_atom *asyh);
761 void (*release)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
762 struct nv50_head_atom *asyh);
763 void (*prepare)(struct nv50_wndw *, struct nv50_head_atom *asyh,
764 struct nv50_wndw_atom *asyw);
765
766 void (*sema_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
767 void (*sema_clr)(struct nv50_wndw *);
768 void (*ntfy_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
769 void (*ntfy_clr)(struct nv50_wndw *);
770 int (*ntfy_wait_begun)(struct nv50_wndw *, struct nv50_wndw_atom *);
771 void (*image_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
772 void (*image_clr)(struct nv50_wndw *);
773 void (*lut)(struct nv50_wndw *, struct nv50_wndw_atom *);
774 void (*point)(struct nv50_wndw *, struct nv50_wndw_atom *);
775
776 u32 (*update)(struct nv50_wndw *, u32 interlock);
777};
778
779static int
780nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
781{
782 if (asyw->set.ntfy)
783 return wndw->func->ntfy_wait_begun(wndw, asyw);
784 return 0;
785}
786
787static u32
788nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 interlock, bool flush,
789 struct nv50_wndw_atom *asyw)
790{
791 if (asyw->clr.sema && (!asyw->set.sema || flush))
792 wndw->func->sema_clr(wndw);
793 if (asyw->clr.ntfy && (!asyw->set.ntfy || flush))
794 wndw->func->ntfy_clr(wndw);
795 if (asyw->clr.image && (!asyw->set.image || flush))
796 wndw->func->image_clr(wndw);
797
798 return flush ? wndw->func->update(wndw, interlock) : 0;
799}
800
801static u32
802nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 interlock,
803 struct nv50_wndw_atom *asyw)
804{
805 if (interlock) {
806 asyw->image.mode = 0;
807 asyw->image.interval = 1;
808 }
809
810 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
811 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
812 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
813 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
814 if (asyw->set.point) wndw->func->point (wndw, asyw);
815
816 return wndw->func->update(wndw, interlock);
817}
818
819static void
820nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
821 struct nv50_wndw_atom *asyw,
822 struct nv50_head_atom *asyh)
823{
824 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
825 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
826 wndw->func->release(wndw, asyw, asyh);
827 asyw->ntfy.handle = 0;
828 asyw->sema.handle = 0;
829}
830
831static int
832nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
833 struct nv50_wndw_atom *asyw,
Ben Skeggsf42c5702017-05-01 16:59:29 +1000834 struct nv50_head_atom *asyh)
Ben Skeggs973f10c2016-11-04 17:20:36 +1000835{
836 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
837 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
838 int ret;
839
840 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
841 asyw->clip.x1 = 0;
842 asyw->clip.y1 = 0;
843 asyw->clip.x2 = asyh->state.mode.hdisplay;
844 asyw->clip.y2 = asyh->state.mode.vdisplay;
845
846 asyw->image.w = fb->base.width;
847 asyw->image.h = fb->base.height;
848 asyw->image.kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
Andrey Grodzovsky612fb5d2017-02-02 16:56:30 -0500849
Ben Skeggsf42c5702017-05-01 16:59:29 +1000850 if (asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC)
851 asyw->interval = 0;
852 else
853 asyw->interval = 1;
Andrey Grodzovsky612fb5d2017-02-02 16:56:30 -0500854
Ben Skeggs973f10c2016-11-04 17:20:36 +1000855 if (asyw->image.kind) {
856 asyw->image.layout = 0;
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000857 if (drm->client.device.info.chipset >= 0xc0)
Ben Skeggs973f10c2016-11-04 17:20:36 +1000858 asyw->image.block = fb->nvbo->tile_mode >> 4;
859 else
860 asyw->image.block = fb->nvbo->tile_mode;
861 asyw->image.pitch = (fb->base.pitches[0] / 4) << 4;
862 } else {
863 asyw->image.layout = 1;
864 asyw->image.block = 0;
865 asyw->image.pitch = fb->base.pitches[0];
866 }
867
868 ret = wndw->func->acquire(wndw, asyw, asyh);
869 if (ret)
870 return ret;
871
872 if (asyw->set.image) {
873 if (!(asyw->image.mode = asyw->interval ? 0 : 1))
874 asyw->image.interval = asyw->interval;
875 else
876 asyw->image.interval = 0;
877 }
878
879 return 0;
880}
881
882static int
883nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
884{
885 struct nouveau_drm *drm = nouveau_drm(plane->dev);
886 struct nv50_wndw *wndw = nv50_wndw(plane);
Ben Skeggs839ca902016-11-04 17:20:36 +1000887 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
888 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000889 struct nv50_head_atom *harm = NULL, *asyh = NULL;
890 bool varm = false, asyv = false, asym = false;
891 int ret;
892
Ben Skeggs973f10c2016-11-04 17:20:36 +1000893 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
894 if (asyw->state.crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +1000895 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000896 if (IS_ERR(asyh))
897 return PTR_ERR(asyh);
898 asym = drm_atomic_crtc_needs_modeset(&asyh->state);
899 asyv = asyh->state.active;
900 }
901
902 if (armw->state.crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +1000903 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000904 if (IS_ERR(harm))
905 return PTR_ERR(harm);
Ben Skeggs839ca902016-11-04 17:20:36 +1000906 varm = harm->state.crtc->state->active;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000907 }
908
909 if (asyv) {
910 asyw->point.x = asyw->state.crtc_x;
911 asyw->point.y = asyw->state.crtc_y;
912 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
913 asyw->set.point = true;
914
Ben Skeggs36601c22017-05-01 16:52:03 +1000915 ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh);
916 if (ret)
917 return ret;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000918 } else
919 if (varm) {
920 nv50_wndw_atomic_check_release(wndw, asyw, harm);
921 } else {
922 return 0;
923 }
924
925 if (!asyv || asym) {
926 asyw->clr.ntfy = armw->ntfy.handle != 0;
927 asyw->clr.sema = armw->sema.handle != 0;
928 if (wndw->func->image_clr)
929 asyw->clr.image = armw->image.handle != 0;
930 asyw->set.lut = wndw->func->lut && asyv;
931 }
932
Ben Skeggs973f10c2016-11-04 17:20:36 +1000933 return 0;
934}
935
936static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000937nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
938{
939 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
940 struct nouveau_drm *drm = nouveau_drm(plane->dev);
941
942 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
943 if (!old_state->fb)
944 return;
945
946 nouveau_bo_unpin(fb->nvbo);
947}
948
949static int
950nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
951{
952 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
953 struct nouveau_drm *drm = nouveau_drm(plane->dev);
954 struct nv50_wndw *wndw = nv50_wndw(plane);
955 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
956 struct nv50_head_atom *asyh;
957 struct nv50_dmac_ctxdma *ctxdma;
Ben Skeggs839ca902016-11-04 17:20:36 +1000958 int ret;
959
960 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
961 if (!asyw->state.fb)
962 return 0;
Ben Skeggs839ca902016-11-04 17:20:36 +1000963
964 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
965 if (ret)
966 return ret;
967
Ben Skeggsf00f0e22016-11-04 17:20:36 +1000968 ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, fb);
Ben Skeggs839ca902016-11-04 17:20:36 +1000969 if (IS_ERR(ctxdma)) {
970 nouveau_bo_unpin(fb->nvbo);
971 return PTR_ERR(ctxdma);
972 }
973
974 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
975 asyw->image.handle = ctxdma->object.handle;
976 asyw->image.offset = fb->nvbo->bo.offset;
977
978 if (wndw->func->prepare) {
979 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
980 if (IS_ERR(asyh))
981 return PTR_ERR(asyh);
982
983 wndw->func->prepare(wndw, asyh, asyw);
984 }
985
986 return 0;
987}
988
989static const struct drm_plane_helper_funcs
990nv50_wndw_helper = {
991 .prepare_fb = nv50_wndw_prepare_fb,
992 .cleanup_fb = nv50_wndw_cleanup_fb,
993 .atomic_check = nv50_wndw_atomic_check,
994};
995
996static void
Ben Skeggs973f10c2016-11-04 17:20:36 +1000997nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
998 struct drm_plane_state *state)
999{
1000 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
1001 __drm_atomic_helper_plane_destroy_state(&asyw->state);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001002 kfree(asyw);
1003}
1004
1005static struct drm_plane_state *
1006nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
1007{
1008 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
1009 struct nv50_wndw_atom *asyw;
1010 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
1011 return NULL;
1012 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001013 asyw->interval = 1;
1014 asyw->sema = armw->sema;
1015 asyw->ntfy = armw->ntfy;
1016 asyw->image = armw->image;
1017 asyw->point = armw->point;
1018 asyw->lut = armw->lut;
1019 asyw->clr.mask = 0;
1020 asyw->set.mask = 0;
1021 return &asyw->state;
1022}
1023
1024static void
1025nv50_wndw_reset(struct drm_plane *plane)
1026{
1027 struct nv50_wndw_atom *asyw;
1028
1029 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
1030 return;
1031
1032 if (plane->state)
1033 plane->funcs->atomic_destroy_state(plane, plane->state);
1034 plane->state = &asyw->state;
1035 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -04001036 plane->state->rotation = DRM_MODE_ROTATE_0;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001037}
1038
1039static void
1040nv50_wndw_destroy(struct drm_plane *plane)
1041{
1042 struct nv50_wndw *wndw = nv50_wndw(plane);
1043 void *data;
1044 nvif_notify_fini(&wndw->notify);
1045 data = wndw->func->dtor(wndw);
1046 drm_plane_cleanup(&wndw->plane);
1047 kfree(data);
1048}
1049
1050static const struct drm_plane_funcs
1051nv50_wndw = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001052 .update_plane = drm_atomic_helper_update_plane,
1053 .disable_plane = drm_atomic_helper_disable_plane,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001054 .destroy = nv50_wndw_destroy,
1055 .reset = nv50_wndw_reset,
1056 .set_property = drm_atomic_helper_plane_set_property,
1057 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
1058 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
1059};
1060
1061static void
1062nv50_wndw_fini(struct nv50_wndw *wndw)
1063{
1064 nvif_notify_put(&wndw->notify);
1065}
1066
1067static void
1068nv50_wndw_init(struct nv50_wndw *wndw)
1069{
1070 nvif_notify_get(&wndw->notify);
1071}
1072
1073static int
1074nv50_wndw_ctor(const struct nv50_wndw_func *func, struct drm_device *dev,
1075 enum drm_plane_type type, const char *name, int index,
1076 struct nv50_dmac *dmac, const u32 *format, int nformat,
1077 struct nv50_wndw *wndw)
1078{
1079 int ret;
1080
1081 wndw->func = func;
1082 wndw->dmac = dmac;
1083
1084 ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw, format,
1085 nformat, type, "%s-%d", name, index);
1086 if (ret)
1087 return ret;
1088
Ben Skeggs839ca902016-11-04 17:20:36 +10001089 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001090 return 0;
1091}
1092
1093/******************************************************************************
Ben Skeggs22e927d2016-11-04 17:20:36 +10001094 * Cursor plane
1095 *****************************************************************************/
1096#define nv50_curs(p) container_of((p), struct nv50_curs, wndw)
1097
1098struct nv50_curs {
1099 struct nv50_wndw wndw;
1100 struct nvif_object chan;
1101};
1102
1103static u32
1104nv50_curs_update(struct nv50_wndw *wndw, u32 interlock)
1105{
1106 struct nv50_curs *curs = nv50_curs(wndw);
1107 nvif_wr32(&curs->chan, 0x0080, 0x00000000);
1108 return 0;
1109}
1110
1111static void
1112nv50_curs_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1113{
1114 struct nv50_curs *curs = nv50_curs(wndw);
1115 nvif_wr32(&curs->chan, 0x0084, (asyw->point.y << 16) | asyw->point.x);
1116}
1117
1118static void
1119nv50_curs_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
1120 struct nv50_wndw_atom *asyw)
1121{
Ben Skeggse6db9572017-05-01 16:53:40 +10001122 u32 handle = nv50_disp(wndw->plane.dev)->mast.base.vram.handle;
1123 u32 offset = asyw->image.offset;
1124 if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
1125 asyh->curs.handle = handle;
1126 asyh->curs.offset = offset;
1127 asyh->set.curs = asyh->curs.visible;
1128 }
Ben Skeggs22e927d2016-11-04 17:20:36 +10001129}
1130
1131static void
1132nv50_curs_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1133 struct nv50_head_atom *asyh)
1134{
1135 asyh->curs.visible = false;
1136}
1137
1138static int
1139nv50_curs_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1140 struct nv50_head_atom *asyh)
1141{
1142 int ret;
1143
1144 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1145 DRM_PLANE_HELPER_NO_SCALING,
1146 DRM_PLANE_HELPER_NO_SCALING,
1147 true, true);
1148 asyh->curs.visible = asyw->state.visible;
1149 if (ret || !asyh->curs.visible)
1150 return ret;
1151
1152 switch (asyw->state.fb->width) {
1153 case 32: asyh->curs.layout = 0; break;
1154 case 64: asyh->curs.layout = 1; break;
1155 default:
1156 return -EINVAL;
1157 }
1158
1159 if (asyw->state.fb->width != asyw->state.fb->height)
1160 return -EINVAL;
1161
Ville Syrjälä438b74a2016-12-14 23:32:55 +02001162 switch (asyw->state.fb->format->format) {
Ben Skeggs22e927d2016-11-04 17:20:36 +10001163 case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
1164 default:
1165 WARN_ON(1);
1166 return -EINVAL;
1167 }
1168
1169 return 0;
1170}
1171
1172static void *
1173nv50_curs_dtor(struct nv50_wndw *wndw)
1174{
1175 struct nv50_curs *curs = nv50_curs(wndw);
1176 nvif_object_fini(&curs->chan);
1177 return curs;
1178}
1179
1180static const u32
1181nv50_curs_format[] = {
1182 DRM_FORMAT_ARGB8888,
1183};
1184
1185static const struct nv50_wndw_func
1186nv50_curs = {
1187 .dtor = nv50_curs_dtor,
1188 .acquire = nv50_curs_acquire,
1189 .release = nv50_curs_release,
1190 .prepare = nv50_curs_prepare,
1191 .point = nv50_curs_point,
1192 .update = nv50_curs_update,
1193};
1194
1195static int
1196nv50_curs_new(struct nouveau_drm *drm, struct nv50_head *head,
1197 struct nv50_curs **pcurs)
1198{
1199 static const struct nvif_mclass curses[] = {
1200 { GK104_DISP_CURSOR, 0 },
1201 { GF110_DISP_CURSOR, 0 },
1202 { GT214_DISP_CURSOR, 0 },
1203 { G82_DISP_CURSOR, 0 },
1204 { NV50_DISP_CURSOR, 0 },
1205 {}
1206 };
1207 struct nv50_disp_cursor_v0 args = {
1208 .head = head->base.index,
1209 };
1210 struct nv50_disp *disp = nv50_disp(drm->dev);
1211 struct nv50_curs *curs;
1212 int cid, ret;
1213
1214 cid = nvif_mclass(disp->disp, curses);
1215 if (cid < 0) {
1216 NV_ERROR(drm, "No supported cursor immediate class\n");
1217 return cid;
1218 }
1219
1220 if (!(curs = *pcurs = kzalloc(sizeof(*curs), GFP_KERNEL)))
1221 return -ENOMEM;
1222
1223 ret = nv50_wndw_ctor(&nv50_curs, drm->dev, DRM_PLANE_TYPE_CURSOR,
1224 "curs", head->base.index, &disp->mast.base,
1225 nv50_curs_format, ARRAY_SIZE(nv50_curs_format),
1226 &curs->wndw);
1227 if (ret) {
1228 kfree(curs);
1229 return ret;
1230 }
1231
1232 ret = nvif_object_init(disp->disp, 0, curses[cid].oclass, &args,
1233 sizeof(args), &curs->chan);
1234 if (ret) {
1235 NV_ERROR(drm, "curs%04x allocation failed: %d\n",
1236 curses[cid].oclass, ret);
1237 return ret;
1238 }
1239
1240 return 0;
1241}
1242
1243/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +10001244 * Primary plane
1245 *****************************************************************************/
1246#define nv50_base(p) container_of((p), struct nv50_base, wndw)
1247
1248struct nv50_base {
1249 struct nv50_wndw wndw;
1250 struct nv50_sync chan;
1251 int id;
1252};
1253
1254static int
1255nv50_base_notify(struct nvif_notify *notify)
1256{
1257 return NVIF_NOTIFY_KEEP;
1258}
1259
1260static void
1261nv50_base_lut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1262{
1263 struct nv50_base *base = nv50_base(wndw);
1264 u32 *push;
1265 if ((push = evo_wait(&base->chan, 2))) {
1266 evo_mthd(push, 0x00e0, 1);
1267 evo_data(push, asyw->lut.enable << 30);
1268 evo_kick(push, &base->chan);
1269 }
1270}
1271
1272static void
1273nv50_base_image_clr(struct nv50_wndw *wndw)
1274{
1275 struct nv50_base *base = nv50_base(wndw);
1276 u32 *push;
1277 if ((push = evo_wait(&base->chan, 4))) {
1278 evo_mthd(push, 0x0084, 1);
1279 evo_data(push, 0x00000000);
1280 evo_mthd(push, 0x00c0, 1);
1281 evo_data(push, 0x00000000);
1282 evo_kick(push, &base->chan);
1283 }
1284}
1285
1286static void
1287nv50_base_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1288{
1289 struct nv50_base *base = nv50_base(wndw);
1290 const s32 oclass = base->chan.base.base.user.oclass;
1291 u32 *push;
1292 if ((push = evo_wait(&base->chan, 10))) {
1293 evo_mthd(push, 0x0084, 1);
1294 evo_data(push, (asyw->image.mode << 8) |
1295 (asyw->image.interval << 4));
1296 evo_mthd(push, 0x00c0, 1);
1297 evo_data(push, asyw->image.handle);
1298 if (oclass < G82_DISP_BASE_CHANNEL_DMA) {
1299 evo_mthd(push, 0x0800, 5);
1300 evo_data(push, asyw->image.offset >> 8);
1301 evo_data(push, 0x00000000);
1302 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1303 evo_data(push, (asyw->image.layout << 20) |
1304 asyw->image.pitch |
1305 asyw->image.block);
1306 evo_data(push, (asyw->image.kind << 16) |
1307 (asyw->image.format << 8));
1308 } else
1309 if (oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1310 evo_mthd(push, 0x0800, 5);
1311 evo_data(push, asyw->image.offset >> 8);
1312 evo_data(push, 0x00000000);
1313 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1314 evo_data(push, (asyw->image.layout << 20) |
1315 asyw->image.pitch |
1316 asyw->image.block);
1317 evo_data(push, asyw->image.format << 8);
1318 } else {
1319 evo_mthd(push, 0x0400, 5);
1320 evo_data(push, asyw->image.offset >> 8);
1321 evo_data(push, 0x00000000);
1322 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1323 evo_data(push, (asyw->image.layout << 24) |
1324 asyw->image.pitch |
1325 asyw->image.block);
1326 evo_data(push, asyw->image.format << 8);
1327 }
1328 evo_kick(push, &base->chan);
1329 }
1330}
1331
1332static void
1333nv50_base_ntfy_clr(struct nv50_wndw *wndw)
1334{
1335 struct nv50_base *base = nv50_base(wndw);
1336 u32 *push;
1337 if ((push = evo_wait(&base->chan, 2))) {
1338 evo_mthd(push, 0x00a4, 1);
1339 evo_data(push, 0x00000000);
1340 evo_kick(push, &base->chan);
1341 }
1342}
1343
1344static void
1345nv50_base_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1346{
1347 struct nv50_base *base = nv50_base(wndw);
1348 u32 *push;
1349 if ((push = evo_wait(&base->chan, 3))) {
1350 evo_mthd(push, 0x00a0, 2);
1351 evo_data(push, (asyw->ntfy.awaken << 30) | asyw->ntfy.offset);
1352 evo_data(push, asyw->ntfy.handle);
1353 evo_kick(push, &base->chan);
1354 }
1355}
1356
1357static void
1358nv50_base_sema_clr(struct nv50_wndw *wndw)
1359{
1360 struct nv50_base *base = nv50_base(wndw);
1361 u32 *push;
1362 if ((push = evo_wait(&base->chan, 2))) {
1363 evo_mthd(push, 0x0094, 1);
1364 evo_data(push, 0x00000000);
1365 evo_kick(push, &base->chan);
1366 }
1367}
1368
1369static void
1370nv50_base_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1371{
1372 struct nv50_base *base = nv50_base(wndw);
1373 u32 *push;
1374 if ((push = evo_wait(&base->chan, 5))) {
1375 evo_mthd(push, 0x0088, 4);
1376 evo_data(push, asyw->sema.offset);
1377 evo_data(push, asyw->sema.acquire);
1378 evo_data(push, asyw->sema.release);
1379 evo_data(push, asyw->sema.handle);
1380 evo_kick(push, &base->chan);
1381 }
1382}
1383
1384static u32
1385nv50_base_update(struct nv50_wndw *wndw, u32 interlock)
1386{
1387 struct nv50_base *base = nv50_base(wndw);
1388 u32 *push;
1389
1390 if (!(push = evo_wait(&base->chan, 2)))
1391 return 0;
1392 evo_mthd(push, 0x0080, 1);
1393 evo_data(push, interlock);
1394 evo_kick(push, &base->chan);
1395
1396 if (base->chan.base.base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA)
1397 return interlock ? 2 << (base->id * 8) : 0;
1398 return interlock ? 2 << (base->id * 4) : 0;
1399}
1400
1401static int
1402nv50_base_ntfy_wait_begun(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1403{
1404 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
1405 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001406 if (nvif_msec(&drm->client.device, 2000ULL,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001407 u32 data = nouveau_bo_rd32(disp->sync, asyw->ntfy.offset / 4);
1408 if ((data & 0xc0000000) == 0x40000000)
1409 break;
1410 usleep_range(1, 2);
1411 ) < 0)
1412 return -ETIMEDOUT;
1413 return 0;
1414}
1415
1416static void
1417nv50_base_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1418 struct nv50_head_atom *asyh)
1419{
1420 asyh->base.cpp = 0;
1421}
1422
1423static int
1424nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1425 struct nv50_head_atom *asyh)
1426{
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001427 const struct drm_framebuffer *fb = asyw->state.fb;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001428 int ret;
1429
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001430 if (!fb->format->depth)
Ben Skeggs973f10c2016-11-04 17:20:36 +10001431 return -EINVAL;
1432
1433 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1434 DRM_PLANE_HELPER_NO_SCALING,
1435 DRM_PLANE_HELPER_NO_SCALING,
1436 false, true);
1437 if (ret)
1438 return ret;
1439
Ville Syrjälä9857ecb2016-11-18 21:53:03 +02001440 asyh->base.depth = fb->format->depth;
1441 asyh->base.cpp = fb->format->cpp[0];
Ben Skeggs973f10c2016-11-04 17:20:36 +10001442 asyh->base.x = asyw->state.src.x1 >> 16;
1443 asyh->base.y = asyw->state.src.y1 >> 16;
1444 asyh->base.w = asyw->state.fb->width;
1445 asyh->base.h = asyw->state.fb->height;
1446
Ville Syrjälä438b74a2016-12-14 23:32:55 +02001447 switch (fb->format->format) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001448 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
1449 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
1450 case DRM_FORMAT_XRGB1555 :
1451 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
1452 case DRM_FORMAT_XRGB8888 :
1453 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
1454 case DRM_FORMAT_XBGR2101010:
1455 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
1456 case DRM_FORMAT_XBGR8888 :
1457 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
1458 default:
1459 WARN_ON(1);
1460 return -EINVAL;
1461 }
1462
1463 asyw->lut.enable = 1;
1464 asyw->set.image = true;
1465 return 0;
1466}
1467
1468static void *
1469nv50_base_dtor(struct nv50_wndw *wndw)
1470{
1471 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1472 struct nv50_base *base = nv50_base(wndw);
1473 nv50_dmac_destroy(&base->chan.base, disp->disp);
1474 return base;
1475}
1476
1477static const u32
1478nv50_base_format[] = {
1479 DRM_FORMAT_C8,
1480 DRM_FORMAT_RGB565,
1481 DRM_FORMAT_XRGB1555,
1482 DRM_FORMAT_ARGB1555,
1483 DRM_FORMAT_XRGB8888,
1484 DRM_FORMAT_ARGB8888,
1485 DRM_FORMAT_XBGR2101010,
1486 DRM_FORMAT_ABGR2101010,
1487 DRM_FORMAT_XBGR8888,
1488 DRM_FORMAT_ABGR8888,
1489};
1490
1491static const struct nv50_wndw_func
1492nv50_base = {
1493 .dtor = nv50_base_dtor,
1494 .acquire = nv50_base_acquire,
1495 .release = nv50_base_release,
1496 .sema_set = nv50_base_sema_set,
1497 .sema_clr = nv50_base_sema_clr,
1498 .ntfy_set = nv50_base_ntfy_set,
1499 .ntfy_clr = nv50_base_ntfy_clr,
1500 .ntfy_wait_begun = nv50_base_ntfy_wait_begun,
1501 .image_set = nv50_base_image_set,
1502 .image_clr = nv50_base_image_clr,
1503 .lut = nv50_base_lut,
1504 .update = nv50_base_update,
1505};
1506
1507static int
1508nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
1509 struct nv50_base **pbase)
1510{
1511 struct nv50_disp *disp = nv50_disp(drm->dev);
1512 struct nv50_base *base;
1513 int ret;
1514
1515 if (!(base = *pbase = kzalloc(sizeof(*base), GFP_KERNEL)))
1516 return -ENOMEM;
1517 base->id = head->base.index;
1518 base->wndw.ntfy = EVO_FLIP_NTFY0(base->id);
1519 base->wndw.sema = EVO_FLIP_SEM0(base->id);
1520 base->wndw.data = 0x00000000;
1521
1522 ret = nv50_wndw_ctor(&nv50_base, drm->dev, DRM_PLANE_TYPE_PRIMARY,
1523 "base", base->id, &base->chan.base,
1524 nv50_base_format, ARRAY_SIZE(nv50_base_format),
1525 &base->wndw);
1526 if (ret) {
1527 kfree(base);
1528 return ret;
1529 }
1530
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001531 ret = nv50_base_create(&drm->client.device, disp->disp, base->id,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001532 disp->sync->bo.offset, &base->chan);
1533 if (ret)
1534 return ret;
1535
1536 return nvif_notify_init(&base->chan.base.base.user, nv50_base_notify,
1537 false,
1538 NV50_DISP_BASE_CHANNEL_DMA_V0_NTFY_UEVENT,
1539 &(struct nvif_notify_uevent_req) {},
1540 sizeof(struct nvif_notify_uevent_req),
1541 sizeof(struct nvif_notify_uevent_rep),
1542 &base->wndw.notify);
1543}
1544
1545/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001546 * Head
1547 *****************************************************************************/
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001548static void
Ben Skeggs7e08d672016-11-04 17:20:36 +10001549nv50_head_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
1550{
1551 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1552 u32 *push;
1553 if ((push = evo_wait(core, 2))) {
1554 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1555 evo_mthd(push, 0x08a8 + (head->base.index * 0x400), 1);
1556 else
1557 evo_mthd(push, 0x0498 + (head->base.index * 0x300), 1);
1558 evo_data(push, (asyh->procamp.sat.sin << 20) |
1559 (asyh->procamp.sat.cos << 8));
1560 evo_kick(push, core);
1561 }
1562}
1563
1564static void
Ben Skeggs7e918332016-11-04 17:20:36 +10001565nv50_head_dither(struct nv50_head *head, struct nv50_head_atom *asyh)
1566{
1567 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1568 u32 *push;
1569 if ((push = evo_wait(core, 2))) {
1570 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1571 evo_mthd(push, 0x08a0 + (head->base.index * 0x0400), 1);
1572 else
1573 if (core->base.user.oclass < GK104_DISP_CORE_CHANNEL_DMA)
1574 evo_mthd(push, 0x0490 + (head->base.index * 0x0300), 1);
1575 else
1576 evo_mthd(push, 0x04a0 + (head->base.index * 0x0300), 1);
1577 evo_data(push, (asyh->dither.mode << 3) |
1578 (asyh->dither.bits << 1) |
1579 asyh->dither.enable);
1580 evo_kick(push, core);
1581 }
1582}
1583
1584static void
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001585nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
1586{
1587 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1588 u32 bounds = 0;
1589 u32 *push;
1590
1591 if (asyh->base.cpp) {
1592 switch (asyh->base.cpp) {
1593 case 8: bounds |= 0x00000500; break;
1594 case 4: bounds |= 0x00000300; break;
1595 case 2: bounds |= 0x00000100; break;
1596 default:
1597 WARN_ON(1);
1598 break;
1599 }
1600 bounds |= 0x00000001;
1601 }
1602
1603 if ((push = evo_wait(core, 2))) {
1604 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1605 evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
1606 else
1607 evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
1608 evo_data(push, bounds);
1609 evo_kick(push, core);
1610 }
1611}
1612
1613static void
1614nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
1615{
1616 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1617 u32 bounds = 0;
1618 u32 *push;
1619
1620 if (asyh->base.cpp) {
1621 switch (asyh->base.cpp) {
1622 case 8: bounds |= 0x00000500; break;
1623 case 4: bounds |= 0x00000300; break;
1624 case 2: bounds |= 0x00000100; break;
1625 case 1: bounds |= 0x00000000; break;
1626 default:
1627 WARN_ON(1);
1628 break;
1629 }
1630 bounds |= 0x00000001;
1631 }
1632
1633 if ((push = evo_wait(core, 2))) {
1634 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1635 evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
1636 else
1637 evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
1638 evo_data(push, bounds);
1639 evo_kick(push, core);
1640 }
1641}
1642
1643static void
Ben Skeggsea8ee392016-11-04 17:20:36 +10001644nv50_head_curs_clr(struct nv50_head *head)
1645{
1646 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1647 u32 *push;
1648 if ((push = evo_wait(core, 4))) {
1649 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1650 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1651 evo_data(push, 0x05000000);
1652 } else
1653 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1654 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1655 evo_data(push, 0x05000000);
1656 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1657 evo_data(push, 0x00000000);
1658 } else {
1659 evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
1660 evo_data(push, 0x05000000);
1661 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1662 evo_data(push, 0x00000000);
1663 }
1664 evo_kick(push, core);
1665 }
1666}
1667
1668static void
1669nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1670{
1671 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1672 u32 *push;
1673 if ((push = evo_wait(core, 5))) {
1674 if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
1675 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1676 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1677 (asyh->curs.format << 24));
1678 evo_data(push, asyh->curs.offset >> 8);
1679 } else
1680 if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1681 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1682 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1683 (asyh->curs.format << 24));
1684 evo_data(push, asyh->curs.offset >> 8);
1685 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1686 evo_data(push, asyh->curs.handle);
1687 } else {
1688 evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
1689 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1690 (asyh->curs.format << 24));
1691 evo_data(push, asyh->curs.offset >> 8);
1692 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1693 evo_data(push, asyh->curs.handle);
1694 }
1695 evo_kick(push, core);
1696 }
1697}
1698
1699static void
Ben Skeggsad633612016-11-04 17:20:36 +10001700nv50_head_core_clr(struct nv50_head *head)
1701{
1702 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1703 u32 *push;
1704 if ((push = evo_wait(core, 2))) {
1705 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1706 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
1707 else
1708 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
1709 evo_data(push, 0x00000000);
1710 evo_kick(push, core);
1711 }
1712}
1713
1714static void
1715nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1716{
1717 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1718 u32 *push;
1719 if ((push = evo_wait(core, 9))) {
1720 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1721 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1722 evo_data(push, asyh->core.offset >> 8);
1723 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1724 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1725 evo_data(push, asyh->core.layout << 20 |
1726 (asyh->core.pitch >> 8) << 8 |
1727 asyh->core.block);
1728 evo_data(push, asyh->core.kind << 16 |
1729 asyh->core.format << 8);
1730 evo_data(push, asyh->core.handle);
1731 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1732 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
Ben Skeggs19d53d02016-12-13 11:18:46 +10001733 /* EVO will complain with INVALID_STATE if we have an
1734 * active cursor and (re)specify HeadSetContextDmaIso
1735 * without also updating HeadSetOffsetCursor.
1736 */
1737 asyh->set.curs = asyh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10001738 } else
1739 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1740 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1741 evo_data(push, asyh->core.offset >> 8);
1742 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1743 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1744 evo_data(push, asyh->core.layout << 20 |
1745 (asyh->core.pitch >> 8) << 8 |
1746 asyh->core.block);
1747 evo_data(push, asyh->core.format << 8);
1748 evo_data(push, asyh->core.handle);
1749 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1750 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1751 } else {
1752 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
1753 evo_data(push, asyh->core.offset >> 8);
1754 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
1755 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1756 evo_data(push, asyh->core.layout << 24 |
1757 (asyh->core.pitch >> 8) << 8 |
1758 asyh->core.block);
1759 evo_data(push, asyh->core.format << 8);
1760 evo_data(push, asyh->core.handle);
1761 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
1762 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1763 }
1764 evo_kick(push, core);
1765 }
1766}
1767
1768static void
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001769nv50_head_lut_clr(struct nv50_head *head)
1770{
1771 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1772 u32 *push;
1773 if ((push = evo_wait(core, 4))) {
1774 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1775 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1776 evo_data(push, 0x40000000);
1777 } else
1778 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1779 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1780 evo_data(push, 0x40000000);
1781 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1782 evo_data(push, 0x00000000);
1783 } else {
1784 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
1785 evo_data(push, 0x03000000);
1786 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1787 evo_data(push, 0x00000000);
1788 }
1789 evo_kick(push, core);
1790 }
1791}
1792
1793static void
1794nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1795{
1796 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1797 u32 *push;
1798 if ((push = evo_wait(core, 7))) {
1799 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1800 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1801 evo_data(push, 0xc0000000);
1802 evo_data(push, asyh->lut.offset >> 8);
1803 } else
1804 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1805 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1806 evo_data(push, 0xc0000000);
1807 evo_data(push, asyh->lut.offset >> 8);
1808 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1809 evo_data(push, asyh->lut.handle);
1810 } else {
1811 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
1812 evo_data(push, 0x83000000);
1813 evo_data(push, asyh->lut.offset >> 8);
1814 evo_data(push, 0x00000000);
1815 evo_data(push, 0x00000000);
1816 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1817 evo_data(push, asyh->lut.handle);
1818 }
1819 evo_kick(push, core);
1820 }
1821}
1822
1823static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001824nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1825{
1826 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1827 struct nv50_head_mode *m = &asyh->mode;
1828 u32 *push;
1829 if ((push = evo_wait(core, 14))) {
1830 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1831 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
1832 evo_data(push, 0x00800000 | m->clock);
1833 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001834 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 7);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001835 evo_data(push, 0x00000000);
1836 evo_data(push, (m->v.active << 16) | m->h.active );
1837 evo_data(push, (m->v.synce << 16) | m->h.synce );
1838 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1839 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1840 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001841 evo_data(push, asyh->mode.v.blankus);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001842 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
1843 evo_data(push, 0x00000000);
1844 } else {
1845 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
1846 evo_data(push, 0x00000000);
1847 evo_data(push, (m->v.active << 16) | m->h.active );
1848 evo_data(push, (m->v.synce << 16) | m->h.synce );
1849 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1850 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1851 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
1852 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
1853 evo_data(push, 0x00000000); /* ??? */
1854 evo_data(push, 0xffffff00);
1855 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
1856 evo_data(push, m->clock * 1000);
1857 evo_data(push, 0x00200000); /* ??? */
1858 evo_data(push, m->clock * 1000);
1859 }
1860 evo_kick(push, core);
1861 }
1862}
1863
1864static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001865nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
1866{
1867 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1868 u32 *push;
1869 if ((push = evo_wait(core, 10))) {
1870 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1871 evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
1872 evo_data(push, 0x00000000);
1873 evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
1874 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1875 evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
1876 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1877 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1878 } else {
1879 evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
1880 evo_data(push, 0x00000000);
1881 evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
1882 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1883 evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
1884 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1885 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1886 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1887 }
1888 evo_kick(push, core);
1889 }
1890}
1891
1892static void
Ben Skeggsad633612016-11-04 17:20:36 +10001893nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
1894{
1895 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001896 nv50_head_lut_clr(head);
1897 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsad633612016-11-04 17:20:36 +10001898 nv50_head_core_clr(head);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001899 if (asyh->clr.curs && (!asyh->set.curs || y))
1900 nv50_head_curs_clr(head);
Ben Skeggsad633612016-11-04 17:20:36 +10001901}
1902
1903static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001904nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1905{
Ben Skeggsc4e68122016-11-04 17:20:36 +10001906 if (asyh->set.view ) nv50_head_view (head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001907 if (asyh->set.mode ) nv50_head_mode (head, asyh);
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001908 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10001909 if (asyh->set.core ) nv50_head_core_set(head, asyh);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001910 if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001911 if (asyh->set.base ) nv50_head_base (head, asyh);
1912 if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
Ben Skeggs7e918332016-11-04 17:20:36 +10001913 if (asyh->set.dither ) nv50_head_dither (head, asyh);
Ben Skeggs7e08d672016-11-04 17:20:36 +10001914 if (asyh->set.procamp) nv50_head_procamp (head, asyh);
1915}
1916
1917static void
1918nv50_head_atomic_check_procamp(struct nv50_head_atom *armh,
1919 struct nv50_head_atom *asyh,
1920 struct nouveau_conn_atom *asyc)
1921{
1922 const int vib = asyc->procamp.color_vibrance - 100;
1923 const int hue = asyc->procamp.vibrant_hue - 90;
1924 const int adj = (vib > 0) ? 50 : 0;
1925 asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff;
1926 asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff;
1927 asyh->set.procamp = true;
Ben Skeggs7e918332016-11-04 17:20:36 +10001928}
1929
1930static void
1931nv50_head_atomic_check_dither(struct nv50_head_atom *armh,
1932 struct nv50_head_atom *asyh,
1933 struct nouveau_conn_atom *asyc)
1934{
1935 struct drm_connector *connector = asyc->state.connector;
1936 u32 mode = 0x00;
1937
1938 if (asyc->dither.mode == DITHERING_MODE_AUTO) {
1939 if (asyh->base.depth > connector->display_info.bpc * 3)
1940 mode = DITHERING_MODE_DYNAMIC2X2;
1941 } else {
1942 mode = asyc->dither.mode;
1943 }
1944
1945 if (asyc->dither.depth == DITHERING_DEPTH_AUTO) {
1946 if (connector->display_info.bpc >= 8)
1947 mode |= DITHERING_DEPTH_8BPC;
1948 } else {
1949 mode |= asyc->dither.depth;
1950 }
1951
1952 asyh->dither.enable = mode;
1953 asyh->dither.bits = mode >> 1;
1954 asyh->dither.mode = mode >> 3;
1955 asyh->set.dither = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001956}
1957
1958static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001959nv50_head_atomic_check_view(struct nv50_head_atom *armh,
1960 struct nv50_head_atom *asyh,
1961 struct nouveau_conn_atom *asyc)
1962{
1963 struct drm_connector *connector = asyc->state.connector;
1964 struct drm_display_mode *omode = &asyh->state.adjusted_mode;
1965 struct drm_display_mode *umode = &asyh->state.mode;
1966 int mode = asyc->scaler.mode;
1967 struct edid *edid;
1968
1969 if (connector->edid_blob_ptr)
1970 edid = (struct edid *)connector->edid_blob_ptr->data;
1971 else
1972 edid = NULL;
1973
1974 if (!asyc->scaler.full) {
1975 if (mode == DRM_MODE_SCALE_NONE)
1976 omode = umode;
1977 } else {
1978 /* Non-EDID LVDS/eDP mode. */
1979 mode = DRM_MODE_SCALE_FULLSCREEN;
1980 }
1981
1982 asyh->view.iW = umode->hdisplay;
1983 asyh->view.iH = umode->vdisplay;
1984 asyh->view.oW = omode->hdisplay;
1985 asyh->view.oH = omode->vdisplay;
1986 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
1987 asyh->view.oH *= 2;
1988
1989 /* Add overscan compensation if necessary, will keep the aspect
1990 * ratio the same as the backend mode unless overridden by the
1991 * user setting both hborder and vborder properties.
1992 */
1993 if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
1994 (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
1995 drm_detect_hdmi_monitor(edid)))) {
1996 u32 bX = asyc->scaler.underscan.hborder;
1997 u32 bY = asyc->scaler.underscan.vborder;
1998 u32 r = (asyh->view.oH << 19) / asyh->view.oW;
1999
2000 if (bX) {
2001 asyh->view.oW -= (bX * 2);
2002 if (bY) asyh->view.oH -= (bY * 2);
2003 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2004 } else {
2005 asyh->view.oW -= (asyh->view.oW >> 4) + 32;
2006 if (bY) asyh->view.oH -= (bY * 2);
2007 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2008 }
2009 }
2010
2011 /* Handle CENTER/ASPECT scaling, taking into account the areas
2012 * removed already for overscan compensation.
2013 */
2014 switch (mode) {
2015 case DRM_MODE_SCALE_CENTER:
2016 asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
2017 asyh->view.oH = min((u16)umode->vdisplay, asyh->view.oH);
2018 /* fall-through */
2019 case DRM_MODE_SCALE_ASPECT:
2020 if (asyh->view.oH < asyh->view.oW) {
2021 u32 r = (asyh->view.iW << 19) / asyh->view.iH;
2022 asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
2023 } else {
2024 u32 r = (asyh->view.iH << 19) / asyh->view.iW;
2025 asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2026 }
2027 break;
2028 default:
2029 break;
2030 }
2031
2032 asyh->set.view = true;
2033}
2034
2035static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002036nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
2037{
2038 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002039 struct nv50_head_mode *m = &asyh->mode;
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002040 u32 blankus;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002041
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002042 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002043
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002044 /*
2045 * DRM modes are defined in terms of a repeating interval
2046 * starting with the active display area. The hardware modes
2047 * are defined in terms of a repeating interval starting one
2048 * unit (pixel or line) into the sync pulse. So, add bias.
2049 */
2050
2051 m->h.active = mode->crtc_htotal;
2052 m->h.synce = mode->crtc_hsync_end - mode->crtc_hsync_start - 1;
2053 m->h.blanke = mode->crtc_hblank_end - mode->crtc_hsync_start - 1;
2054 m->h.blanks = m->h.blanke + mode->crtc_hdisplay;
2055
2056 m->v.active = mode->crtc_vtotal;
2057 m->v.synce = mode->crtc_vsync_end - mode->crtc_vsync_start - 1;
2058 m->v.blanke = mode->crtc_vblank_end - mode->crtc_vsync_start - 1;
2059 m->v.blanks = m->v.blanke + mode->crtc_vdisplay;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002060
2061 /*XXX: Safe underestimate, even "0" works */
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002062 blankus = (m->v.active - mode->crtc_vdisplay - 2) * m->h.active;
Ben Skeggsaeecfcd2017-04-05 09:12:54 +10002063 blankus *= 1000;
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002064 blankus /= mode->crtc_clock;
Ben Skeggsaeecfcd2017-04-05 09:12:54 +10002065 m->v.blankus = blankus;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002066
2067 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002068 m->v.blank2e = m->v.active + m->v.blanke;
2069 m->v.blank2s = m->v.blank2e + mode->crtc_vdisplay;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002070 m->v.active = (m->v.active * 2) + 1;
2071 m->interlace = true;
2072 } else {
2073 m->v.blank2e = 0;
2074 m->v.blank2s = 1;
2075 m->interlace = false;
2076 }
Alastair Bridgewater35dd9872017-04-11 13:11:16 -04002077 m->clock = mode->crtc_clock;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002078
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002079 asyh->set.mode = true;
2080}
2081
2082static int
2083nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
2084{
2085 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggsad633612016-11-04 17:20:36 +10002086 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002087 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002088 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002089 struct nv50_head_atom *asyh = nv50_head_atom(state);
Ben Skeggs839ca902016-11-04 17:20:36 +10002090 struct nouveau_conn_atom *asyc = NULL;
2091 struct drm_connector_state *conns;
2092 struct drm_connector *conn;
2093 int i;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002094
2095 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002096 if (asyh->state.active) {
Ben Skeggs839ca902016-11-04 17:20:36 +10002097 for_each_connector_in_state(asyh->state.state, conn, conns, i) {
2098 if (conns->crtc == crtc) {
2099 asyc = nouveau_conn_atom(conns);
2100 break;
2101 }
2102 }
2103
2104 if (armh->state.active) {
2105 if (asyc) {
2106 if (asyh->state.mode_changed)
2107 asyc->set.scaler = true;
2108 if (armh->base.depth != asyh->base.depth)
2109 asyc->set.dither = true;
2110 }
2111 } else {
Gustavo A. R. Silva86276922017-05-22 14:12:37 -05002112 if (asyc)
2113 asyc->set.mask = ~0;
Ben Skeggs839ca902016-11-04 17:20:36 +10002114 asyh->set.mask = ~0;
2115 }
2116
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002117 if (asyh->state.mode_changed)
2118 nv50_head_atomic_check_mode(head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10002119
Ben Skeggs839ca902016-11-04 17:20:36 +10002120 if (asyc) {
2121 if (asyc->set.scaler)
2122 nv50_head_atomic_check_view(armh, asyh, asyc);
2123 if (asyc->set.dither)
2124 nv50_head_atomic_check_dither(armh, asyh, asyc);
2125 if (asyc->set.procamp)
2126 nv50_head_atomic_check_procamp(armh, asyh, asyc);
2127 }
2128
Ben Skeggsad633612016-11-04 17:20:36 +10002129 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
2130 asyh->core.x = asyh->base.x;
2131 asyh->core.y = asyh->base.y;
2132 asyh->core.w = asyh->base.w;
2133 asyh->core.h = asyh->base.h;
2134 } else
Ben Skeggsea8ee392016-11-04 17:20:36 +10002135 if ((asyh->core.visible = asyh->curs.visible)) {
Ben Skeggsad633612016-11-04 17:20:36 +10002136 /*XXX: We need to either find some way of having the
2137 * primary base layer appear black, while still
2138 * being able to display the other layers, or we
2139 * need to allocate a dummy black surface here.
2140 */
2141 asyh->core.x = 0;
2142 asyh->core.y = 0;
2143 asyh->core.w = asyh->state.mode.hdisplay;
2144 asyh->core.h = asyh->state.mode.vdisplay;
2145 }
2146 asyh->core.handle = disp->mast.base.vram.handle;
2147 asyh->core.offset = 0;
2148 asyh->core.format = 0xcf;
2149 asyh->core.kind = 0;
2150 asyh->core.layout = 1;
2151 asyh->core.block = 0;
2152 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002153 asyh->lut.handle = disp->mast.base.vram.handle;
2154 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002155 asyh->set.base = armh->base.cpp != asyh->base.cpp;
2156 asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
Ben Skeggsad633612016-11-04 17:20:36 +10002157 } else {
2158 asyh->core.visible = false;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002159 asyh->curs.visible = false;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002160 asyh->base.cpp = 0;
2161 asyh->ovly.cpp = 0;
Ben Skeggsad633612016-11-04 17:20:36 +10002162 }
2163
2164 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
2165 if (asyh->core.visible) {
2166 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
2167 asyh->set.core = true;
2168 } else
2169 if (armh->core.visible) {
2170 asyh->clr.core = true;
2171 }
Ben Skeggsea8ee392016-11-04 17:20:36 +10002172
2173 if (asyh->curs.visible) {
2174 if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
2175 asyh->set.curs = true;
2176 } else
2177 if (armh->curs.visible) {
2178 asyh->clr.curs = true;
2179 }
Ben Skeggsad633612016-11-04 17:20:36 +10002180 } else {
2181 asyh->clr.core = armh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002182 asyh->clr.curs = armh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10002183 asyh->set.core = asyh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002184 asyh->set.curs = asyh->curs.visible;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002185 }
2186
Ben Skeggs839ca902016-11-04 17:20:36 +10002187 if (asyh->clr.mask || asyh->set.mask)
2188 nv50_atom(asyh->state.state)->lock_core = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002189 return 0;
2190}
2191
Ben Skeggs438d99e2011-07-05 16:48:06 +10002192static void
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002193nv50_head_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002194{
Ben Skeggse225f442012-11-21 14:40:21 +10002195 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002196 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2197 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
2198 int i;
2199
2200 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002201 u16 r = nv_crtc->lut.r[i] >> 2;
2202 u16 g = nv_crtc->lut.g[i] >> 2;
2203 u16 b = nv_crtc->lut.b[i] >> 2;
2204
Ben Skeggs648d4df2014-08-10 04:10:27 +10002205 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002206 writew(r + 0x0000, lut + (i * 0x08) + 0);
2207 writew(g + 0x0000, lut + (i * 0x08) + 2);
2208 writew(b + 0x0000, lut + (i * 0x08) + 4);
2209 } else {
2210 writew(r + 0x6000, lut + (i * 0x20) + 0);
2211 writew(g + 0x6000, lut + (i * 0x20) + 2);
2212 writew(b + 0x6000, lut + (i * 0x20) + 4);
2213 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002214 }
2215}
2216
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002217static const struct drm_crtc_helper_funcs
2218nv50_head_help = {
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002219 .load_lut = nv50_head_lut_load,
Ben Skeggs839ca902016-11-04 17:20:36 +10002220 .atomic_check = nv50_head_atomic_check,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002221};
2222
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002223static int
2224nv50_head_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02002225 uint32_t size,
2226 struct drm_modeset_acquire_ctx *ctx)
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002227{
2228 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2229 u32 i;
2230
2231 for (i = 0; i < size; i++) {
2232 nv_crtc->lut.r[i] = r[i];
2233 nv_crtc->lut.g[i] = g[i];
2234 nv_crtc->lut.b[i] = b[i];
2235 }
2236
2237 nv50_head_lut_load(crtc);
2238 return 0;
2239}
2240
Ben Skeggs839ca902016-11-04 17:20:36 +10002241static void
2242nv50_head_atomic_destroy_state(struct drm_crtc *crtc,
2243 struct drm_crtc_state *state)
2244{
2245 struct nv50_head_atom *asyh = nv50_head_atom(state);
2246 __drm_atomic_helper_crtc_destroy_state(&asyh->state);
2247 kfree(asyh);
2248}
2249
2250static struct drm_crtc_state *
2251nv50_head_atomic_duplicate_state(struct drm_crtc *crtc)
2252{
2253 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
2254 struct nv50_head_atom *asyh;
2255 if (!(asyh = kmalloc(sizeof(*asyh), GFP_KERNEL)))
2256 return NULL;
2257 __drm_atomic_helper_crtc_duplicate_state(crtc, &asyh->state);
2258 asyh->view = armh->view;
2259 asyh->mode = armh->mode;
2260 asyh->lut = armh->lut;
2261 asyh->core = armh->core;
2262 asyh->curs = armh->curs;
2263 asyh->base = armh->base;
2264 asyh->ovly = armh->ovly;
2265 asyh->dither = armh->dither;
2266 asyh->procamp = armh->procamp;
2267 asyh->clr.mask = 0;
2268 asyh->set.mask = 0;
2269 return &asyh->state;
2270}
2271
2272static void
2273__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
2274 struct drm_crtc_state *state)
2275{
2276 if (crtc->state)
2277 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
2278 crtc->state = state;
2279 crtc->state->crtc = crtc;
2280}
2281
2282static void
2283nv50_head_reset(struct drm_crtc *crtc)
2284{
2285 struct nv50_head_atom *asyh;
2286
2287 if (WARN_ON(!(asyh = kzalloc(sizeof(*asyh), GFP_KERNEL))))
2288 return;
2289
2290 __drm_atomic_helper_crtc_reset(crtc, &asyh->state);
2291}
2292
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002293static void
2294nv50_head_destroy(struct drm_crtc *crtc)
2295{
2296 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2297 struct nv50_disp *disp = nv50_disp(crtc->dev);
2298 struct nv50_head *head = nv50_head(crtc);
2299
2300 nv50_dmac_destroy(&head->ovly.base, disp->disp);
2301 nv50_pioc_destroy(&head->oimm.base);
2302
2303 nouveau_bo_unmap(nv_crtc->lut.nvbo);
2304 if (nv_crtc->lut.nvbo)
2305 nouveau_bo_unpin(nv_crtc->lut.nvbo);
2306 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
2307
2308 drm_crtc_cleanup(crtc);
2309 kfree(crtc);
2310}
2311
2312static const struct drm_crtc_funcs
2313nv50_head_func = {
Ben Skeggs839ca902016-11-04 17:20:36 +10002314 .reset = nv50_head_reset,
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002315 .gamma_set = nv50_head_gamma_set,
2316 .destroy = nv50_head_destroy,
Ben Skeggs839ca902016-11-04 17:20:36 +10002317 .set_config = drm_atomic_helper_set_config,
Andrey Grodzovsky612fb5d2017-02-02 16:56:30 -05002318 .page_flip = drm_atomic_helper_page_flip,
Ben Skeggs839ca902016-11-04 17:20:36 +10002319 .set_property = drm_atomic_helper_crtc_set_property,
2320 .atomic_duplicate_state = nv50_head_atomic_duplicate_state,
2321 .atomic_destroy_state = nv50_head_atomic_destroy_state,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002322};
2323
2324static int
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002325nv50_head_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002326{
Ben Skeggsa01ca782015-08-20 14:54:15 +10002327 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002328 struct nvif_device *device = &drm->client.device;
Ben Skeggse225f442012-11-21 14:40:21 +10002329 struct nv50_disp *disp = nv50_disp(dev);
2330 struct nv50_head *head;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002331 struct nv50_base *base;
Ben Skeggs22e927d2016-11-04 17:20:36 +10002332 struct nv50_curs *curs;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002333 struct drm_crtc *crtc;
2334 int ret, i;
2335
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002336 head = kzalloc(sizeof(*head), GFP_KERNEL);
2337 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002338 return -ENOMEM;
2339
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002340 head->base.index = index;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002341 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002342 head->base.lut.r[i] = i << 8;
2343 head->base.lut.g[i] = i << 8;
2344 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002345 }
2346
Ben Skeggs973f10c2016-11-04 17:20:36 +10002347 ret = nv50_base_new(drm, head, &base);
Ben Skeggs22e927d2016-11-04 17:20:36 +10002348 if (ret == 0)
2349 ret = nv50_curs_new(drm, head, &curs);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002350 if (ret) {
2351 kfree(head);
2352 return ret;
2353 }
2354
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002355 crtc = &head->base.base;
Ben Skeggs839ca902016-11-04 17:20:36 +10002356 drm_crtc_init_with_planes(dev, crtc, &base->wndw.plane,
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002357 &curs->wndw.plane, &nv50_head_func,
Ben Skeggs839ca902016-11-04 17:20:36 +10002358 "head-%d", head->base.index);
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002359 drm_crtc_helper_add(crtc, &nv50_head_help);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002360 drm_mode_crtc_set_gamma_size(crtc, 256);
2361
Ben Skeggsbab7cc12016-05-24 17:26:48 +10002362 ret = nouveau_bo_new(&drm->client, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002363 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002364 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002365 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002366 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002367 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002368 if (ret)
2369 nouveau_bo_unpin(head->base.lut.nvbo);
2370 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002371 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002372 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002373 }
2374
2375 if (ret)
2376 goto out;
2377
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002378 /* allocate overlay resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10002379 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002380 if (ret)
2381 goto out;
2382
Ben Skeggsa01ca782015-08-20 14:54:15 +10002383 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
2384 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002385 if (ret)
2386 goto out;
2387
Ben Skeggs438d99e2011-07-05 16:48:06 +10002388out:
2389 if (ret)
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002390 nv50_head_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002391 return ret;
2392}
2393
2394/******************************************************************************
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002395 * Output path helpers
Ben Skeggsa91d3222014-12-22 16:30:13 +10002396 *****************************************************************************/
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002397static int
2398nv50_outp_atomic_check_view(struct drm_encoder *encoder,
2399 struct drm_crtc_state *crtc_state,
2400 struct drm_connector_state *conn_state,
2401 struct drm_display_mode *native_mode)
2402{
2403 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
2404 struct drm_display_mode *mode = &crtc_state->mode;
2405 struct drm_connector *connector = conn_state->connector;
2406 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
2407 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
2408
2409 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
2410 asyc->scaler.full = false;
2411 if (!native_mode)
2412 return 0;
2413
2414 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
2415 switch (connector->connector_type) {
2416 case DRM_MODE_CONNECTOR_LVDS:
2417 case DRM_MODE_CONNECTOR_eDP:
2418 /* Force use of scaler for non-EDID modes. */
2419 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
2420 break;
2421 mode = native_mode;
2422 asyc->scaler.full = true;
2423 break;
2424 default:
2425 break;
2426 }
2427 } else {
2428 mode = native_mode;
2429 }
2430
2431 if (!drm_mode_equal(adjusted_mode, mode)) {
2432 drm_mode_copy(adjusted_mode, mode);
2433 crtc_state->mode_changed = true;
2434 }
2435
2436 return 0;
2437}
2438
Ben Skeggs839ca902016-11-04 17:20:36 +10002439static int
2440nv50_outp_atomic_check(struct drm_encoder *encoder,
2441 struct drm_crtc_state *crtc_state,
2442 struct drm_connector_state *conn_state)
Ben Skeggsa91d3222014-12-22 16:30:13 +10002443{
Ben Skeggs839ca902016-11-04 17:20:36 +10002444 struct nouveau_connector *nv_connector =
2445 nouveau_connector(conn_state->connector);
2446 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2447 nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10002448}
2449
2450/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002451 * DAC
2452 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002453static void
Ben Skeggse225f442012-11-21 14:40:21 +10002454nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002455{
2456 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002457 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10002458 struct {
2459 struct nv50_disp_mthd_v1 base;
2460 struct nv50_disp_dac_pwr_v0 pwr;
2461 } args = {
2462 .base.version = 1,
2463 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
2464 .base.hasht = nv_encoder->dcb->hasht,
2465 .base.hashm = nv_encoder->dcb->hashm,
2466 .pwr.state = 1,
2467 .pwr.data = 1,
2468 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
2469 mode != DRM_MODE_DPMS_OFF),
2470 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
2471 mode != DRM_MODE_DPMS_OFF),
2472 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002473
Ben Skeggsbf0eb892014-08-10 04:10:26 +10002474 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002475}
2476
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002477static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002478nv50_dac_disable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002479{
Ben Skeggsf20c6652016-11-04 17:20:36 +10002480 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2481 struct nv50_mast *mast = nv50_mast(encoder->dev);
2482 const int or = nv_encoder->or;
2483 u32 *push;
2484
2485 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10002486 push = evo_wait(mast, 4);
2487 if (push) {
2488 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2489 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2490 evo_data(push, 0x00000000);
2491 } else {
2492 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2493 evo_data(push, 0x00000000);
2494 }
2495 evo_kick(push, mast);
2496 }
2497 }
2498
2499 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002500}
2501
2502static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002503nv50_dac_enable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002504{
Ben Skeggse225f442012-11-21 14:40:21 +10002505 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002506 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2507 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002508 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggs97b19b52012-11-16 11:21:37 +10002509 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002510
Ben Skeggs97b19b52012-11-16 11:21:37 +10002511 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002512 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002513 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002514 u32 syncs = 0x00000000;
2515
2516 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2517 syncs |= 0x00000001;
2518 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2519 syncs |= 0x00000002;
2520
2521 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
2522 evo_data(push, 1 << nv_crtc->index);
2523 evo_data(push, syncs);
2524 } else {
2525 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2526 u32 syncs = 0x00000001;
2527
2528 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2529 syncs |= 0x00000008;
2530 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2531 syncs |= 0x00000010;
2532
2533 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2534 magic |= 0x00000001;
2535
2536 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2537 evo_data(push, syncs);
2538 evo_data(push, magic);
2539 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2540 evo_data(push, 1 << nv_crtc->index);
2541 }
2542
2543 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002544 }
2545
2546 nv_encoder->crtc = encoder->crtc;
2547}
2548
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002549static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10002550nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002551{
Ben Skeggsc4abd312014-08-10 04:10:26 +10002552 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002553 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10002554 struct {
2555 struct nv50_disp_mthd_v1 base;
2556 struct nv50_disp_dac_load_v0 load;
2557 } args = {
2558 .base.version = 1,
2559 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2560 .base.hasht = nv_encoder->dcb->hasht,
2561 .base.hashm = nv_encoder->dcb->hashm,
2562 };
2563 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10002564
Ben Skeggsc4abd312014-08-10 04:10:26 +10002565 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2566 if (args.load.data == 0)
2567 args.load.data = 340;
2568
2569 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2570 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10002571 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10002572
Ben Skeggs35b21d32012-11-08 12:08:55 +10002573 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002574}
2575
Ben Skeggsf20c6652016-11-04 17:20:36 +10002576static const struct drm_encoder_helper_funcs
2577nv50_dac_help = {
2578 .dpms = nv50_dac_dpms,
Ben Skeggs839ca902016-11-04 17:20:36 +10002579 .atomic_check = nv50_outp_atomic_check,
2580 .enable = nv50_dac_enable,
2581 .disable = nv50_dac_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002582 .detect = nv50_dac_detect
2583};
2584
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002585static void
Ben Skeggse225f442012-11-21 14:40:21 +10002586nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002587{
2588 drm_encoder_cleanup(encoder);
2589 kfree(encoder);
2590}
2591
Ben Skeggsf20c6652016-11-04 17:20:36 +10002592static const struct drm_encoder_funcs
2593nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10002594 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002595};
2596
2597static int
Ben Skeggse225f442012-11-21 14:40:21 +10002598nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002599{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002600 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002601 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002602 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002603 struct nouveau_encoder *nv_encoder;
2604 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002605 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002606
2607 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2608 if (!nv_encoder)
2609 return -ENOMEM;
2610 nv_encoder->dcb = dcbe;
2611 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002612
2613 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2614 if (bus)
2615 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002616
2617 encoder = to_drm_encoder(nv_encoder);
2618 encoder->possible_crtcs = dcbe->heads;
2619 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002620 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2621 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10002622 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002623
2624 drm_mode_connector_attach_encoder(connector, encoder);
2625 return 0;
2626}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002627
2628/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10002629 * Audio
2630 *****************************************************************************/
2631static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002632nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
2633{
2634 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2635 struct nv50_disp *disp = nv50_disp(encoder->dev);
2636 struct {
2637 struct nv50_disp_mthd_v1 base;
2638 struct nv50_disp_sor_hda_eld_v0 eld;
2639 } args = {
2640 .base.version = 1,
2641 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2642 .base.hasht = nv_encoder->dcb->hasht,
2643 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2644 (0x0100 << nv_crtc->index),
2645 };
2646
2647 nvif_mthd(disp->disp, 0, &args, sizeof(args));
2648}
2649
2650static void
2651nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002652{
2653 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10002654 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10002655 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10002656 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10002657 struct __packed {
2658 struct {
2659 struct nv50_disp_mthd_v1 mthd;
2660 struct nv50_disp_sor_hda_eld_v0 eld;
2661 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002662 u8 data[sizeof(nv_connector->base.eld)];
2663 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10002664 .base.mthd.version = 1,
2665 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2666 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002667 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2668 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002669 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002670
2671 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2672 if (!drm_detect_monitor_audio(nv_connector->edid))
2673 return;
2674
Ben Skeggs78951d22011-11-11 18:13:13 +10002675 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002676 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002677
Jani Nikula938fd8a2014-10-28 16:20:48 +02002678 nvif_mthd(disp->disp, 0, &args,
2679 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002680}
2681
Ben Skeggsf20c6652016-11-04 17:20:36 +10002682/******************************************************************************
2683 * HDMI
2684 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +10002685static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002686nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002687{
2688 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002689 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002690 struct {
2691 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +10002692 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002693 } args = {
2694 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002695 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2696 .base.hasht = nv_encoder->dcb->hasht,
2697 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2698 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002699 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002700
Ben Skeggs120b0c32014-08-10 04:10:26 +10002701 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002702}
2703
Ben Skeggs78951d22011-11-11 18:13:13 +10002704static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002705nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002706{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002707 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2708 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002709 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002710 struct {
2711 struct nv50_disp_mthd_v1 base;
2712 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
2713 } args = {
2714 .base.version = 1,
2715 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2716 .base.hasht = nv_encoder->dcb->hasht,
2717 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2718 (0x0100 << nv_crtc->index),
2719 .pwr.state = 1,
2720 .pwr.rekey = 56, /* binary driver, and tegra, constant */
2721 };
2722 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002723 u32 max_ac_packet;
2724
2725 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2726 if (!drm_detect_hdmi_monitor(nv_connector->edid))
2727 return;
2728
2729 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10002730 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002731 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10002732 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002733
Ben Skeggse00f2232014-08-10 04:10:26 +10002734 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsf20c6652016-11-04 17:20:36 +10002735 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10002736}
2737
2738/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002739 * MST
2740 *****************************************************************************/
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002741#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
2742#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
2743#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
2744
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002745struct nv50_mstm {
2746 struct nouveau_encoder *outp;
2747
2748 struct drm_dp_mst_topology_mgr mgr;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002749 struct nv50_msto *msto[4];
2750
2751 bool modified;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002752};
2753
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002754struct nv50_mstc {
2755 struct nv50_mstm *mstm;
2756 struct drm_dp_mst_port *port;
2757 struct drm_connector connector;
2758
2759 struct drm_display_mode *native;
2760 struct edid *edid;
2761
2762 int pbn;
2763};
2764
2765struct nv50_msto {
2766 struct drm_encoder encoder;
2767
2768 struct nv50_head *head;
2769 struct nv50_mstc *mstc;
2770 bool disabled;
2771};
2772
2773static struct drm_dp_payload *
2774nv50_msto_payload(struct nv50_msto *msto)
2775{
2776 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2777 struct nv50_mstc *mstc = msto->mstc;
2778 struct nv50_mstm *mstm = mstc->mstm;
2779 int vcpi = mstc->port->vcpi.vcpi, i;
2780
2781 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
2782 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2783 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2784 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
2785 mstm->outp->base.base.name, i, payload->vcpi,
2786 payload->start_slot, payload->num_slots);
2787 }
2788
2789 for (i = 0; i < mstm->mgr.max_payloads; i++) {
2790 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
2791 if (payload->vcpi == vcpi)
2792 return payload;
2793 }
2794
2795 return NULL;
2796}
2797
2798static void
2799nv50_msto_cleanup(struct nv50_msto *msto)
2800{
2801 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2802 struct nv50_mstc *mstc = msto->mstc;
2803 struct nv50_mstm *mstm = mstc->mstm;
2804
2805 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
2806 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
2807 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
2808 if (msto->disabled) {
2809 msto->mstc = NULL;
2810 msto->head = NULL;
2811 msto->disabled = false;
2812 }
2813}
2814
2815static void
2816nv50_msto_prepare(struct nv50_msto *msto)
2817{
2818 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
2819 struct nv50_mstc *mstc = msto->mstc;
2820 struct nv50_mstm *mstm = mstc->mstm;
2821 struct {
2822 struct nv50_disp_mthd_v1 base;
2823 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
2824 } args = {
2825 .base.version = 1,
2826 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
2827 .base.hasht = mstm->outp->dcb->hasht,
2828 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
2829 (0x0100 << msto->head->base.index),
2830 };
2831
2832 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
2833 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
2834 struct drm_dp_payload *payload = nv50_msto_payload(msto);
2835 if (payload) {
2836 args.vcpi.start_slot = payload->start_slot;
2837 args.vcpi.num_slots = payload->num_slots;
2838 args.vcpi.pbn = mstc->port->vcpi.pbn;
2839 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
2840 }
2841 }
2842
2843 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
2844 msto->encoder.name, msto->head->base.base.name,
2845 args.vcpi.start_slot, args.vcpi.num_slots,
2846 args.vcpi.pbn, args.vcpi.aligned_pbn);
2847 nvif_mthd(&drm->display->disp, 0, &args, sizeof(args));
2848}
2849
2850static int
2851nv50_msto_atomic_check(struct drm_encoder *encoder,
2852 struct drm_crtc_state *crtc_state,
2853 struct drm_connector_state *conn_state)
2854{
2855 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
2856 struct nv50_mstm *mstm = mstc->mstm;
2857 int bpp = conn_state->connector->display_info.bpc * 3;
2858 int slots;
2859
2860 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
2861
2862 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2863 if (slots < 0)
2864 return slots;
2865
2866 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2867 mstc->native);
2868}
2869
2870static void
2871nv50_msto_enable(struct drm_encoder *encoder)
2872{
2873 struct nv50_head *head = nv50_head(encoder->crtc);
2874 struct nv50_msto *msto = nv50_msto(encoder);
2875 struct nv50_mstc *mstc = NULL;
2876 struct nv50_mstm *mstm = NULL;
2877 struct drm_connector *connector;
Gustavo Padovan875dd622017-05-11 16:10:46 -03002878 struct drm_connector_list_iter conn_iter;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002879 u8 proto, depth;
2880 int slots;
2881 bool r;
2882
Gustavo Padovan875dd622017-05-11 16:10:46 -03002883 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
2884 drm_for_each_connector_iter(connector, &conn_iter) {
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002885 if (connector->state->best_encoder == &msto->encoder) {
2886 mstc = nv50_mstc(connector);
2887 mstm = mstc->mstm;
2888 break;
2889 }
2890 }
Gustavo Padovan875dd622017-05-11 16:10:46 -03002891 drm_connector_list_iter_end(&conn_iter);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002892
2893 if (WARN_ON(!mstc))
2894 return;
2895
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002896 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
2897 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002898 WARN_ON(!r);
2899
2900 if (mstm->outp->dcb->sorconf.link & 1)
2901 proto = 0x8;
2902 else
2903 proto = 0x9;
2904
2905 switch (mstc->connector.display_info.bpc) {
2906 case 6: depth = 0x2; break;
2907 case 8: depth = 0x5; break;
2908 case 10:
2909 default: depth = 0x6; break;
2910 }
2911
2912 mstm->outp->update(mstm->outp, head->base.index,
2913 &head->base.base.state->adjusted_mode, proto, depth);
2914
2915 msto->head = head;
2916 msto->mstc = mstc;
2917 mstm->modified = true;
2918}
2919
2920static void
2921nv50_msto_disable(struct drm_encoder *encoder)
2922{
2923 struct nv50_msto *msto = nv50_msto(encoder);
2924 struct nv50_mstc *mstc = msto->mstc;
2925 struct nv50_mstm *mstm = mstc->mstm;
2926
2927 if (mstc->port)
2928 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
2929
2930 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
2931 mstm->modified = true;
2932 msto->disabled = true;
2933}
2934
2935static const struct drm_encoder_helper_funcs
2936nv50_msto_help = {
2937 .disable = nv50_msto_disable,
2938 .enable = nv50_msto_enable,
2939 .atomic_check = nv50_msto_atomic_check,
2940};
2941
2942static void
2943nv50_msto_destroy(struct drm_encoder *encoder)
2944{
2945 struct nv50_msto *msto = nv50_msto(encoder);
2946 drm_encoder_cleanup(&msto->encoder);
2947 kfree(msto);
2948}
2949
2950static const struct drm_encoder_funcs
2951nv50_msto = {
2952 .destroy = nv50_msto_destroy,
2953};
2954
2955static int
2956nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
2957 struct nv50_msto **pmsto)
2958{
2959 struct nv50_msto *msto;
2960 int ret;
2961
2962 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
2963 return -ENOMEM;
2964
2965 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
2966 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
2967 if (ret) {
2968 kfree(*pmsto);
2969 *pmsto = NULL;
2970 return ret;
2971 }
2972
2973 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
2974 msto->encoder.possible_crtcs = heads;
2975 return 0;
2976}
2977
2978static struct drm_encoder *
2979nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
2980 struct drm_connector_state *connector_state)
2981{
2982 struct nv50_head *head = nv50_head(connector_state->crtc);
2983 struct nv50_mstc *mstc = nv50_mstc(connector);
2984 if (mstc->port) {
2985 struct nv50_mstm *mstm = mstc->mstm;
2986 return &mstm->msto[head->base.index]->encoder;
2987 }
2988 return NULL;
2989}
2990
2991static struct drm_encoder *
2992nv50_mstc_best_encoder(struct drm_connector *connector)
2993{
2994 struct nv50_mstc *mstc = nv50_mstc(connector);
2995 if (mstc->port) {
2996 struct nv50_mstm *mstm = mstc->mstm;
2997 return &mstm->msto[0]->encoder;
2998 }
2999 return NULL;
3000}
3001
3002static enum drm_mode_status
3003nv50_mstc_mode_valid(struct drm_connector *connector,
3004 struct drm_display_mode *mode)
3005{
3006 return MODE_OK;
3007}
3008
3009static int
3010nv50_mstc_get_modes(struct drm_connector *connector)
3011{
3012 struct nv50_mstc *mstc = nv50_mstc(connector);
3013 int ret = 0;
3014
3015 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
3016 drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
3017 if (mstc->edid) {
3018 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
3019 drm_edid_to_eld(&mstc->connector, mstc->edid);
3020 }
3021
3022 if (!mstc->connector.display_info.bpc)
3023 mstc->connector.display_info.bpc = 8;
3024
3025 if (mstc->native)
3026 drm_mode_destroy(mstc->connector.dev, mstc->native);
3027 mstc->native = nouveau_conn_native_mode(&mstc->connector);
3028 return ret;
3029}
3030
3031static const struct drm_connector_helper_funcs
3032nv50_mstc_help = {
3033 .get_modes = nv50_mstc_get_modes,
3034 .mode_valid = nv50_mstc_mode_valid,
3035 .best_encoder = nv50_mstc_best_encoder,
3036 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
3037};
3038
3039static enum drm_connector_status
3040nv50_mstc_detect(struct drm_connector *connector, bool force)
3041{
3042 struct nv50_mstc *mstc = nv50_mstc(connector);
3043 if (!mstc->port)
3044 return connector_status_disconnected;
3045 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
3046}
3047
3048static void
3049nv50_mstc_destroy(struct drm_connector *connector)
3050{
3051 struct nv50_mstc *mstc = nv50_mstc(connector);
3052 drm_connector_cleanup(&mstc->connector);
3053 kfree(mstc);
3054}
3055
3056static const struct drm_connector_funcs
3057nv50_mstc = {
3058 .dpms = drm_atomic_helper_connector_dpms,
3059 .reset = nouveau_conn_reset,
3060 .detect = nv50_mstc_detect,
3061 .fill_modes = drm_helper_probe_single_connector_modes,
3062 .set_property = drm_atomic_helper_connector_set_property,
3063 .destroy = nv50_mstc_destroy,
3064 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
3065 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
3066 .atomic_set_property = nouveau_conn_atomic_set_property,
3067 .atomic_get_property = nouveau_conn_atomic_get_property,
3068};
3069
3070static int
3071nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
3072 const char *path, struct nv50_mstc **pmstc)
3073{
3074 struct drm_device *dev = mstm->outp->base.base.dev;
3075 struct nv50_mstc *mstc;
3076 int ret, i;
3077
3078 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
3079 return -ENOMEM;
3080 mstc->mstm = mstm;
3081 mstc->port = port;
3082
3083 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
3084 DRM_MODE_CONNECTOR_DisplayPort);
3085 if (ret) {
3086 kfree(*pmstc);
3087 *pmstc = NULL;
3088 return ret;
3089 }
3090
3091 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
3092
3093 mstc->connector.funcs->reset(&mstc->connector);
3094 nouveau_conn_attach_properties(&mstc->connector);
3095
3096 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto; i++)
3097 drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
3098
3099 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
3100 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
3101 drm_mode_connector_set_path_property(&mstc->connector, path);
3102 return 0;
3103}
3104
3105static void
3106nv50_mstm_cleanup(struct nv50_mstm *mstm)
3107{
3108 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3109 struct drm_encoder *encoder;
3110 int ret;
3111
3112 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
3113 ret = drm_dp_check_act_status(&mstm->mgr);
3114
3115 ret = drm_dp_update_payload_part2(&mstm->mgr);
3116
3117 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3118 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3119 struct nv50_msto *msto = nv50_msto(encoder);
3120 struct nv50_mstc *mstc = msto->mstc;
3121 if (mstc && mstc->mstm == mstm)
3122 nv50_msto_cleanup(msto);
3123 }
3124 }
3125
3126 mstm->modified = false;
3127}
3128
3129static void
3130nv50_mstm_prepare(struct nv50_mstm *mstm)
3131{
3132 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
3133 struct drm_encoder *encoder;
3134 int ret;
3135
3136 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
3137 ret = drm_dp_update_payload_part1(&mstm->mgr);
3138
3139 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
3140 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3141 struct nv50_msto *msto = nv50_msto(encoder);
3142 struct nv50_mstc *mstc = msto->mstc;
3143 if (mstc && mstc->mstm == mstm)
3144 nv50_msto_prepare(msto);
3145 }
3146 }
3147}
3148
3149static void
3150nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
3151{
3152 struct nv50_mstm *mstm = nv50_mstm(mgr);
3153 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
3154}
3155
3156static void
3157nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
3158 struct drm_connector *connector)
3159{
3160 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3161 struct nv50_mstc *mstc = nv50_mstc(connector);
3162
3163 drm_connector_unregister(&mstc->connector);
3164
3165 drm_modeset_lock_all(drm->dev);
3166 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
3167 mstc->port = NULL;
3168 drm_modeset_unlock_all(drm->dev);
3169
3170 drm_connector_unreference(&mstc->connector);
3171}
3172
3173static void
3174nv50_mstm_register_connector(struct drm_connector *connector)
3175{
3176 struct nouveau_drm *drm = nouveau_drm(connector->dev);
3177
3178 drm_modeset_lock_all(drm->dev);
3179 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
3180 drm_modeset_unlock_all(drm->dev);
3181
3182 drm_connector_register(connector);
3183}
3184
3185static struct drm_connector *
3186nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
3187 struct drm_dp_mst_port *port, const char *path)
3188{
3189 struct nv50_mstm *mstm = nv50_mstm(mgr);
3190 struct nv50_mstc *mstc;
3191 int ret;
3192
3193 ret = nv50_mstc_new(mstm, port, path, &mstc);
3194 if (ret) {
3195 if (mstc)
3196 mstc->connector.funcs->destroy(&mstc->connector);
3197 return NULL;
3198 }
3199
3200 return &mstc->connector;
3201}
3202
3203static const struct drm_dp_mst_topology_cbs
3204nv50_mstm = {
3205 .add_connector = nv50_mstm_add_connector,
3206 .register_connector = nv50_mstm_register_connector,
3207 .destroy_connector = nv50_mstm_destroy_connector,
3208 .hotplug = nv50_mstm_hotplug,
3209};
3210
3211void
3212nv50_mstm_service(struct nv50_mstm *mstm)
3213{
3214 struct drm_dp_aux *aux = mstm->mgr.aux;
3215 bool handled = true;
3216 int ret;
3217 u8 esi[8] = {};
3218
3219 while (handled) {
3220 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
3221 if (ret != 8) {
3222 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3223 return;
3224 }
3225
3226 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
3227 if (!handled)
3228 break;
3229
3230 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
3231 }
3232}
3233
3234void
3235nv50_mstm_remove(struct nv50_mstm *mstm)
3236{
3237 if (mstm)
3238 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
3239}
3240
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003241static int
3242nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
3243{
3244 struct nouveau_encoder *outp = mstm->outp;
3245 struct {
3246 struct nv50_disp_mthd_v1 base;
3247 struct nv50_disp_sor_dp_mst_link_v0 mst;
3248 } args = {
3249 .base.version = 1,
3250 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
3251 .base.hasht = outp->dcb->hasht,
3252 .base.hashm = outp->dcb->hashm,
3253 .mst.state = state,
3254 };
3255 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
3256 struct nvif_object *disp = &drm->display->disp;
3257 int ret;
3258
3259 if (dpcd >= 0x12) {
3260 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
3261 if (ret < 0)
3262 return ret;
3263
3264 dpcd &= ~DP_MST_EN;
3265 if (state)
3266 dpcd |= DP_MST_EN;
3267
3268 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
3269 if (ret < 0)
3270 return ret;
3271 }
3272
3273 return nvif_mthd(disp, 0, &args, sizeof(args));
3274}
3275
3276int
3277nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
3278{
3279 int ret, state = 0;
3280
3281 if (!mstm)
3282 return 0;
3283
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10003284 if (dpcd[0] >= 0x12) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003285 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
3286 if (ret < 0)
3287 return ret;
3288
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10003289 if (!(dpcd[1] & DP_MST_CAP))
3290 dpcd[0] = 0x11;
3291 else
3292 state = allow;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003293 }
3294
3295 ret = nv50_mstm_enable(mstm, dpcd[0], state);
3296 if (ret)
3297 return ret;
3298
3299 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
3300 if (ret)
3301 return nv50_mstm_enable(mstm, dpcd[0], 0);
3302
3303 return mstm->mgr.mst_state;
3304}
3305
3306static void
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003307nv50_mstm_fini(struct nv50_mstm *mstm)
3308{
3309 if (mstm && mstm->mgr.mst_state)
3310 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
3311}
3312
3313static void
3314nv50_mstm_init(struct nv50_mstm *mstm)
3315{
3316 if (mstm && mstm->mgr.mst_state)
3317 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
3318}
3319
3320static void
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003321nv50_mstm_del(struct nv50_mstm **pmstm)
3322{
3323 struct nv50_mstm *mstm = *pmstm;
3324 if (mstm) {
3325 kfree(*pmstm);
3326 *pmstm = NULL;
3327 }
3328}
3329
3330static int
3331nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
3332 int conn_base_id, struct nv50_mstm **pmstm)
3333{
3334 const int max_payloads = hweight8(outp->dcb->heads);
3335 struct drm_device *dev = outp->base.base.dev;
3336 struct nv50_mstm *mstm;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003337 int ret, i;
3338 u8 dpcd;
3339
3340 /* This is a workaround for some monitors not functioning
3341 * correctly in MST mode on initial module load. I think
3342 * some bad interaction with the VBIOS may be responsible.
3343 *
3344 * A good ol' off and on again seems to work here ;)
3345 */
3346 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
3347 if (ret >= 0 && dpcd >= 0x12)
3348 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003349
3350 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
3351 return -ENOMEM;
3352 mstm->outp = outp;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003353 mstm->mgr.cbs = &nv50_mstm;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003354
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08003355 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003356 max_payloads, conn_base_id);
3357 if (ret)
3358 return ret;
3359
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003360 for (i = 0; i < max_payloads; i++) {
3361 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
3362 i, &mstm->msto[i]);
3363 if (ret)
3364 return ret;
3365 }
3366
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003367 return 0;
3368}
3369
3370/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003371 * SOR
3372 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003373static void
Ben Skeggse225f442012-11-21 14:40:21 +10003374nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003375{
3376 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003377 struct nv50_disp *disp = nv50_disp(encoder->dev);
3378 struct {
3379 struct nv50_disp_mthd_v1 base;
3380 struct nv50_disp_sor_pwr_v0 pwr;
3381 } args = {
3382 .base.version = 1,
3383 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
3384 .base.hasht = nv_encoder->dcb->hasht,
3385 .base.hashm = nv_encoder->dcb->hashm,
3386 .pwr.state = mode == DRM_MODE_DPMS_ON,
3387 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10003388
Ben Skeggs8896cee2016-11-04 17:20:36 +10003389 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs83fc0832011-07-05 13:08:40 +10003390}
3391
Ben Skeggs83fc0832011-07-05 13:08:40 +10003392static void
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003393nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
3394 struct drm_display_mode *mode, u8 proto, u8 depth)
Ben Skeggse84a35a2014-06-05 10:59:55 +10003395{
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003396 struct nv50_dmac *core = &nv50_mast(nv_encoder->base.base.dev)->base;
3397 u32 *push;
3398
3399 if (!mode) {
3400 nv_encoder->ctrl &= ~BIT(head);
3401 if (!(nv_encoder->ctrl & 0x0000000f))
3402 nv_encoder->ctrl = 0;
3403 } else {
3404 nv_encoder->ctrl |= proto << 8;
3405 nv_encoder->ctrl |= BIT(head);
3406 }
3407
3408 if ((push = evo_wait(core, 6))) {
3409 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
3410 if (mode) {
3411 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3412 nv_encoder->ctrl |= 0x00001000;
3413 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3414 nv_encoder->ctrl |= 0x00002000;
3415 nv_encoder->ctrl |= depth << 16;
3416 }
Ben Skeggse84a35a2014-06-05 10:59:55 +10003417 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003418 } else {
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003419 if (mode) {
3420 u32 magic = 0x31ec6000 | (head << 25);
3421 u32 syncs = 0x00000001;
3422 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3423 syncs |= 0x00000008;
3424 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3425 syncs |= 0x00000010;
3426 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3427 magic |= 0x00000001;
3428
3429 evo_mthd(push, 0x0404 + (head * 0x300), 2);
3430 evo_data(push, syncs | (depth << 6));
3431 evo_data(push, magic);
3432 }
Ben Skeggse84a35a2014-06-05 10:59:55 +10003433 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003434 }
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003435 evo_data(push, nv_encoder->ctrl);
3436 evo_kick(push, core);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003437 }
3438}
3439
3440static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003441nv50_sor_disable(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003442{
3443 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003444 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003445
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003446 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10003447
3448 if (nv_crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +10003449 struct nvkm_i2c_aux *aux = nv_encoder->aux;
3450 u8 pwr;
3451
3452 if (aux) {
3453 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
3454 if (ret == 0) {
3455 pwr &= ~DP_SET_POWER_MASK;
3456 pwr |= DP_SET_POWER_D3;
3457 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
3458 }
3459 }
3460
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003461 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003462 nv50_audio_disable(encoder, nv_crtc);
3463 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003464 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003465}
3466
3467static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003468nv50_sor_enable(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003469{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003470 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3471 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10003472 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003473 struct {
3474 struct nv50_disp_mthd_v1 base;
3475 struct nv50_disp_sor_lvds_script_v0 lvds;
3476 } lvds = {
3477 .base.version = 1,
3478 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
3479 .base.hasht = nv_encoder->dcb->hasht,
3480 .base.hashm = nv_encoder->dcb->hashm,
3481 };
Ben Skeggse225f442012-11-21 14:40:21 +10003482 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10003483 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10003484 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003485 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10003486 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003487 u8 proto = 0xf;
3488 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003489
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003490 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003491 nv_encoder->crtc = encoder->crtc;
3492
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003493 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10003494 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003495 if (nv_encoder->dcb->sorconf.link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05003496 proto = 0x1;
3497 /* Only enable dual-link if:
3498 * - Need to (i.e. rate > 165MHz)
3499 * - DCB says we can
3500 * - Not an HDMI monitor, since there's no dual-link
3501 * on HDMI.
3502 */
3503 if (mode->clock >= 165000 &&
3504 nv_encoder->dcb->duallink_possible &&
3505 !drm_detect_hdmi_monitor(nv_connector->edid))
3506 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003507 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003508 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003509 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003510
Ben Skeggsf20c6652016-11-04 17:20:36 +10003511 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003512 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003513 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003514 proto = 0x0;
3515
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003516 if (bios->fp_no_ddc) {
3517 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003518 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003519 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003520 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003521 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10003522 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003523 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003524 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003525 } else
3526 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003527 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003528 }
3529
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003530 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003531 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003532 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003533 } else {
3534 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003535 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003536 }
3537
3538 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003539 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003540 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10003541
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003542 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003543 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003544 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10003545 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003546 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003547 else
3548 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003549 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003550 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10003551 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003552
3553 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003554 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003555 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003556 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003557
3558 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003559 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003560 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10003561 BUG();
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003562 break;
3563 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10003564
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003565 nv_encoder->update(nv_encoder, nv_crtc->index, mode, proto, depth);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003566}
3567
Ben Skeggsf20c6652016-11-04 17:20:36 +10003568static const struct drm_encoder_helper_funcs
3569nv50_sor_help = {
3570 .dpms = nv50_sor_dpms,
Ben Skeggs839ca902016-11-04 17:20:36 +10003571 .atomic_check = nv50_outp_atomic_check,
3572 .enable = nv50_sor_enable,
3573 .disable = nv50_sor_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003574};
3575
Ben Skeggs83fc0832011-07-05 13:08:40 +10003576static void
Ben Skeggse225f442012-11-21 14:40:21 +10003577nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003578{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003579 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3580 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003581 drm_encoder_cleanup(encoder);
3582 kfree(encoder);
3583}
3584
Ben Skeggsf20c6652016-11-04 17:20:36 +10003585static const struct drm_encoder_funcs
3586nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10003587 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10003588};
3589
3590static int
Ben Skeggse225f442012-11-21 14:40:21 +10003591nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003592{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003593 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10003594 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003595 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003596 struct nouveau_encoder *nv_encoder;
3597 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003598 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10003599
3600 switch (dcbe->type) {
3601 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
3602 case DCB_OUTPUT_TMDS:
3603 case DCB_OUTPUT_DP:
3604 default:
3605 type = DRM_MODE_ENCODER_TMDS;
3606 break;
3607 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003608
3609 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3610 if (!nv_encoder)
3611 return -ENOMEM;
3612 nv_encoder->dcb = dcbe;
3613 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10003614 nv_encoder->update = nv50_sor_update;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003615
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003616 encoder = to_drm_encoder(nv_encoder);
3617 encoder->possible_crtcs = dcbe->heads;
3618 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003619 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
3620 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003621 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003622
3623 drm_mode_connector_attach_encoder(connector, encoder);
3624
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003625 if (dcbe->type == DCB_OUTPUT_DP) {
3626 struct nvkm_i2c_aux *aux =
3627 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
3628 if (aux) {
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003629 nv_encoder->i2c = &nv_connector->aux.ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003630 nv_encoder->aux = aux;
3631 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003632
3633 /*TODO: Use DP Info Table to check for support. */
3634 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
3635 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
3636 nv_connector->base.base.id,
3637 &nv_encoder->dp.mstm);
3638 if (ret)
3639 return ret;
3640 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003641 } else {
3642 struct nvkm_i2c_bus *bus =
3643 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
3644 if (bus)
3645 nv_encoder->i2c = &bus->i2c;
3646 }
3647
Ben Skeggs83fc0832011-07-05 13:08:40 +10003648 return 0;
3649}
Ben Skeggs26f6d882011-07-04 16:25:18 +10003650
3651/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10003652 * PIOR
3653 *****************************************************************************/
Ben Skeggseb6313a2013-02-11 09:52:58 +10003654static void
3655nv50_pior_dpms(struct drm_encoder *encoder, int mode)
3656{
3657 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3658 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10003659 struct {
3660 struct nv50_disp_mthd_v1 base;
3661 struct nv50_disp_pior_pwr_v0 pwr;
3662 } args = {
3663 .base.version = 1,
3664 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
3665 .base.hasht = nv_encoder->dcb->hasht,
3666 .base.hashm = nv_encoder->dcb->hashm,
3667 .pwr.state = mode == DRM_MODE_DPMS_ON,
3668 .pwr.type = nv_encoder->dcb->type,
3669 };
3670
3671 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10003672}
3673
Ben Skeggs839ca902016-11-04 17:20:36 +10003674static int
3675nv50_pior_atomic_check(struct drm_encoder *encoder,
3676 struct drm_crtc_state *crtc_state,
3677 struct drm_connector_state *conn_state)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003678{
Ben Skeggs839ca902016-11-04 17:20:36 +10003679 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
3680 if (ret)
3681 return ret;
3682 crtc_state->adjusted_mode.clock *= 2;
3683 return 0;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003684}
3685
3686static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003687nv50_pior_disable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003688{
Ben Skeggsf20c6652016-11-04 17:20:36 +10003689 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3690 struct nv50_mast *mast = nv50_mast(encoder->dev);
3691 const int or = nv_encoder->or;
3692 u32 *push;
3693
3694 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10003695 push = evo_wait(mast, 4);
3696 if (push) {
3697 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
3698 evo_mthd(push, 0x0700 + (or * 0x040), 1);
3699 evo_data(push, 0x00000000);
3700 }
3701 evo_kick(push, mast);
3702 }
3703 }
3704
3705 nv_encoder->crtc = NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003706}
3707
3708static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003709nv50_pior_enable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003710{
3711 struct nv50_mast *mast = nv50_mast(encoder->dev);
3712 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3713 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3714 struct nouveau_connector *nv_connector;
Ben Skeggs839ca902016-11-04 17:20:36 +10003715 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003716 u8 owner = 1 << nv_crtc->index;
3717 u8 proto, depth;
3718 u32 *push;
3719
3720 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3721 switch (nv_connector->base.display_info.bpc) {
3722 case 10: depth = 0x6; break;
3723 case 8: depth = 0x5; break;
3724 case 6: depth = 0x2; break;
3725 default: depth = 0x0; break;
3726 }
3727
3728 switch (nv_encoder->dcb->type) {
3729 case DCB_OUTPUT_TMDS:
3730 case DCB_OUTPUT_DP:
3731 proto = 0x0;
3732 break;
3733 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10003734 BUG();
Ben Skeggseb6313a2013-02-11 09:52:58 +10003735 break;
3736 }
3737
Ben Skeggseb6313a2013-02-11 09:52:58 +10003738 push = evo_wait(mast, 8);
3739 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003740 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003741 u32 ctrl = (depth << 16) | (proto << 8) | owner;
3742 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3743 ctrl |= 0x00001000;
3744 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3745 ctrl |= 0x00002000;
3746 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
3747 evo_data(push, ctrl);
3748 }
3749
3750 evo_kick(push, mast);
3751 }
3752
3753 nv_encoder->crtc = encoder->crtc;
3754}
3755
Ben Skeggsf20c6652016-11-04 17:20:36 +10003756static const struct drm_encoder_helper_funcs
3757nv50_pior_help = {
3758 .dpms = nv50_pior_dpms,
Ben Skeggs839ca902016-11-04 17:20:36 +10003759 .atomic_check = nv50_pior_atomic_check,
3760 .enable = nv50_pior_enable,
3761 .disable = nv50_pior_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003762};
Ben Skeggseb6313a2013-02-11 09:52:58 +10003763
3764static void
3765nv50_pior_destroy(struct drm_encoder *encoder)
3766{
3767 drm_encoder_cleanup(encoder);
3768 kfree(encoder);
3769}
3770
Ben Skeggsf20c6652016-11-04 17:20:36 +10003771static const struct drm_encoder_funcs
3772nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003773 .destroy = nv50_pior_destroy,
3774};
3775
3776static int
3777nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
3778{
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003779 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003780 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003781 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003782 struct nvkm_i2c_bus *bus = NULL;
3783 struct nvkm_i2c_aux *aux = NULL;
3784 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003785 struct nouveau_encoder *nv_encoder;
3786 struct drm_encoder *encoder;
3787 int type;
3788
3789 switch (dcbe->type) {
3790 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003791 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
3792 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003793 type = DRM_MODE_ENCODER_TMDS;
3794 break;
3795 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003796 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
Ben Skeggsdf8dc972017-03-01 09:42:04 +10003797 ddc = aux ? &nv_connector->aux.ddc : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003798 type = DRM_MODE_ENCODER_TMDS;
3799 break;
3800 default:
3801 return -ENODEV;
3802 }
3803
3804 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3805 if (!nv_encoder)
3806 return -ENOMEM;
3807 nv_encoder->dcb = dcbe;
3808 nv_encoder->or = ffs(dcbe->or) - 1;
3809 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003810 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003811
3812 encoder = to_drm_encoder(nv_encoder);
3813 encoder->possible_crtcs = dcbe->heads;
3814 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003815 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
3816 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003817 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003818
3819 drm_mode_connector_attach_encoder(connector, encoder);
3820 return 0;
3821}
3822
3823/******************************************************************************
Ben Skeggs839ca902016-11-04 17:20:36 +10003824 * Atomic
3825 *****************************************************************************/
3826
3827static void
3828nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
3829{
3830 struct nv50_disp *disp = nv50_disp(drm->dev);
3831 struct nv50_dmac *core = &disp->mast.base;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003832 struct nv50_mstm *mstm;
3833 struct drm_encoder *encoder;
Ben Skeggs839ca902016-11-04 17:20:36 +10003834 u32 *push;
3835
3836 NV_ATOMIC(drm, "commit core %08x\n", interlock);
3837
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003838 drm_for_each_encoder(encoder, drm->dev) {
3839 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3840 mstm = nouveau_encoder(encoder)->dp.mstm;
3841 if (mstm && mstm->modified)
3842 nv50_mstm_prepare(mstm);
3843 }
3844 }
3845
Ben Skeggs839ca902016-11-04 17:20:36 +10003846 if ((push = evo_wait(core, 5))) {
3847 evo_mthd(push, 0x0084, 1);
3848 evo_data(push, 0x80000000);
3849 evo_mthd(push, 0x0080, 2);
3850 evo_data(push, interlock);
3851 evo_data(push, 0x00000000);
3852 nouveau_bo_wr32(disp->sync, 0, 0x00000000);
3853 evo_kick(push, core);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10003854 if (nvif_msec(&drm->client.device, 2000ULL,
Ben Skeggs839ca902016-11-04 17:20:36 +10003855 if (nouveau_bo_rd32(disp->sync, 0))
3856 break;
3857 usleep_range(1, 2);
3858 ) < 0)
3859 NV_ERROR(drm, "EVO timeout\n");
3860 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10003861
3862 drm_for_each_encoder(encoder, drm->dev) {
3863 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3864 mstm = nouveau_encoder(encoder)->dp.mstm;
3865 if (mstm && mstm->modified)
3866 nv50_mstm_cleanup(mstm);
3867 }
3868 }
Ben Skeggs839ca902016-11-04 17:20:36 +10003869}
3870
3871static void
3872nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
3873{
3874 struct drm_device *dev = state->dev;
3875 struct drm_crtc_state *crtc_state;
3876 struct drm_crtc *crtc;
3877 struct drm_plane_state *plane_state;
3878 struct drm_plane *plane;
3879 struct nouveau_drm *drm = nouveau_drm(dev);
3880 struct nv50_disp *disp = nv50_disp(dev);
3881 struct nv50_atom *atom = nv50_atom(state);
3882 struct nv50_outp_atom *outp, *outt;
3883 u32 interlock_core = 0;
3884 u32 interlock_chan = 0;
3885 int i;
3886
3887 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
3888 drm_atomic_helper_wait_for_fences(dev, state, false);
3889 drm_atomic_helper_wait_for_dependencies(state);
3890 drm_atomic_helper_update_legacy_modeset_state(dev, state);
3891
3892 if (atom->lock_core)
3893 mutex_lock(&disp->mutex);
3894
3895 /* Disable head(s). */
3896 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3897 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3898 struct nv50_head *head = nv50_head(crtc);
3899
3900 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
3901 asyh->clr.mask, asyh->set.mask);
3902
3903 if (asyh->clr.mask) {
3904 nv50_head_flush_clr(head, asyh, atom->flush_disable);
3905 interlock_core |= 1;
3906 }
3907 }
3908
3909 /* Disable plane(s). */
3910 for_each_plane_in_state(state, plane, plane_state, i) {
3911 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3912 struct nv50_wndw *wndw = nv50_wndw(plane);
3913
3914 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
3915 asyw->clr.mask, asyw->set.mask);
3916 if (!asyw->clr.mask)
3917 continue;
3918
3919 interlock_chan |= nv50_wndw_flush_clr(wndw, interlock_core,
3920 atom->flush_disable,
3921 asyw);
3922 }
3923
3924 /* Disable output path(s). */
3925 list_for_each_entry(outp, &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: clr %02x (set %02x)\n", encoder->name,
3933 outp->clr.mask, outp->set.mask);
3934
3935 if (outp->clr.mask) {
3936 help->disable(encoder);
3937 interlock_core |= 1;
3938 if (outp->flush_disable) {
3939 nv50_disp_atomic_commit_core(drm, interlock_chan);
3940 interlock_core = 0;
3941 interlock_chan = 0;
3942 }
3943 }
3944 }
3945
3946 /* Flush disable. */
3947 if (interlock_core) {
3948 if (atom->flush_disable) {
3949 nv50_disp_atomic_commit_core(drm, interlock_chan);
3950 interlock_core = 0;
3951 interlock_chan = 0;
3952 }
3953 }
3954
3955 /* Update output path(s). */
3956 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
3957 const struct drm_encoder_helper_funcs *help;
3958 struct drm_encoder *encoder;
3959
3960 encoder = outp->encoder;
3961 help = encoder->helper_private;
3962
3963 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
3964 outp->set.mask, outp->clr.mask);
3965
3966 if (outp->set.mask) {
3967 help->enable(encoder);
3968 interlock_core = 1;
3969 }
3970
3971 list_del(&outp->head);
3972 kfree(outp);
3973 }
3974
3975 /* Update head(s). */
3976 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3977 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3978 struct nv50_head *head = nv50_head(crtc);
3979
3980 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
3981 asyh->set.mask, asyh->clr.mask);
3982
3983 if (asyh->set.mask) {
3984 nv50_head_flush_set(head, asyh);
3985 interlock_core = 1;
3986 }
3987 }
3988
Ben Skeggs2b507892017-01-24 09:32:26 +10003989 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3990 if (crtc->state->event)
3991 drm_crtc_vblank_get(crtc);
3992 }
3993
Ben Skeggs839ca902016-11-04 17:20:36 +10003994 /* Update plane(s). */
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
3999 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
4000 asyw->set.mask, asyw->clr.mask);
4001 if ( !asyw->set.mask &&
4002 (!asyw->clr.mask || atom->flush_disable))
4003 continue;
4004
4005 interlock_chan |= nv50_wndw_flush_set(wndw, interlock_core, asyw);
4006 }
4007
4008 /* Flush update. */
4009 if (interlock_core) {
4010 if (!interlock_chan && atom->state.legacy_cursor_update) {
4011 u32 *push = evo_wait(&disp->mast, 2);
4012 if (push) {
4013 evo_mthd(push, 0x0080, 1);
4014 evo_data(push, 0x00000000);
4015 evo_kick(push, &disp->mast);
4016 }
4017 } else {
4018 nv50_disp_atomic_commit_core(drm, interlock_chan);
4019 }
4020 }
4021
4022 if (atom->lock_core)
4023 mutex_unlock(&disp->mutex);
4024
4025 /* Wait for HW to signal completion. */
4026 for_each_plane_in_state(state, plane, plane_state, i) {
4027 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
4028 struct nv50_wndw *wndw = nv50_wndw(plane);
4029 int ret = nv50_wndw_wait_armed(wndw, asyw);
4030 if (ret)
4031 NV_ERROR(drm, "%s: timeout\n", plane->name);
4032 }
4033
4034 for_each_crtc_in_state(state, crtc, crtc_state, i) {
4035 if (crtc->state->event) {
4036 unsigned long flags;
Mario Kleinerbd9f6602016-11-23 07:58:54 +01004037 /* Get correct count/ts if racing with vblank irq */
4038 drm_accurate_vblank_count(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10004039 spin_lock_irqsave(&crtc->dev->event_lock, flags);
4040 drm_crtc_send_vblank_event(crtc, crtc->state->event);
4041 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4042 crtc->state->event = NULL;
Ben Skeggs2b507892017-01-24 09:32:26 +10004043 drm_crtc_vblank_put(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10004044 }
4045 }
4046
4047 drm_atomic_helper_commit_hw_done(state);
4048 drm_atomic_helper_cleanup_planes(dev, state);
4049 drm_atomic_helper_commit_cleanup_done(state);
4050 drm_atomic_state_put(state);
4051}
4052
4053static void
4054nv50_disp_atomic_commit_work(struct work_struct *work)
4055{
4056 struct drm_atomic_state *state =
4057 container_of(work, typeof(*state), commit_work);
4058 nv50_disp_atomic_commit_tail(state);
4059}
4060
4061static int
4062nv50_disp_atomic_commit(struct drm_device *dev,
4063 struct drm_atomic_state *state, bool nonblock)
4064{
4065 struct nouveau_drm *drm = nouveau_drm(dev);
4066 struct nv50_disp *disp = nv50_disp(dev);
4067 struct drm_plane_state *plane_state;
4068 struct drm_plane *plane;
4069 struct drm_crtc *crtc;
4070 bool active = false;
4071 int ret, i;
4072
4073 ret = pm_runtime_get_sync(dev->dev);
4074 if (ret < 0 && ret != -EACCES)
4075 return ret;
4076
4077 ret = drm_atomic_helper_setup_commit(state, nonblock);
4078 if (ret)
4079 goto done;
4080
4081 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
4082
4083 ret = drm_atomic_helper_prepare_planes(dev, state);
4084 if (ret)
4085 goto done;
4086
4087 if (!nonblock) {
4088 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
4089 if (ret)
4090 goto done;
4091 }
4092
4093 for_each_plane_in_state(state, plane, plane_state, i) {
4094 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane_state);
4095 struct nv50_wndw *wndw = nv50_wndw(plane);
4096 if (asyw->set.image) {
4097 asyw->ntfy.handle = wndw->dmac->sync.handle;
4098 asyw->ntfy.offset = wndw->ntfy;
4099 asyw->ntfy.awaken = false;
4100 asyw->set.ntfy = true;
4101 nouveau_bo_wr32(disp->sync, wndw->ntfy / 4, 0x00000000);
4102 wndw->ntfy ^= 0x10;
4103 }
4104 }
4105
4106 drm_atomic_helper_swap_state(state, true);
4107 drm_atomic_state_get(state);
4108
4109 if (nonblock)
4110 queue_work(system_unbound_wq, &state->commit_work);
4111 else
4112 nv50_disp_atomic_commit_tail(state);
4113
4114 drm_for_each_crtc(crtc, dev) {
4115 if (crtc->state->enable) {
4116 if (!drm->have_disp_power_ref) {
4117 drm->have_disp_power_ref = true;
4118 return ret;
4119 }
4120 active = true;
4121 break;
4122 }
4123 }
4124
4125 if (!active && drm->have_disp_power_ref) {
4126 pm_runtime_put_autosuspend(dev->dev);
4127 drm->have_disp_power_ref = false;
4128 }
4129
4130done:
4131 pm_runtime_put_autosuspend(dev->dev);
4132 return ret;
4133}
4134
4135static struct nv50_outp_atom *
4136nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
4137{
4138 struct nv50_outp_atom *outp;
4139
4140 list_for_each_entry(outp, &atom->outp, head) {
4141 if (outp->encoder == encoder)
4142 return outp;
4143 }
4144
4145 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
4146 if (!outp)
4147 return ERR_PTR(-ENOMEM);
4148
4149 list_add(&outp->head, &atom->outp);
4150 outp->encoder = encoder;
4151 return outp;
4152}
4153
4154static int
4155nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
4156 struct drm_connector *connector)
4157{
4158 struct drm_encoder *encoder = connector->state->best_encoder;
4159 struct drm_crtc_state *crtc_state;
4160 struct drm_crtc *crtc;
4161 struct nv50_outp_atom *outp;
4162
4163 if (!(crtc = connector->state->crtc))
4164 return 0;
4165
4166 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
4167 if (crtc->state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
4168 outp = nv50_disp_outp_atomic_add(atom, encoder);
4169 if (IS_ERR(outp))
4170 return PTR_ERR(outp);
4171
4172 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
4173 outp->flush_disable = true;
4174 atom->flush_disable = true;
4175 }
4176 outp->clr.ctrl = true;
4177 atom->lock_core = true;
4178 }
4179
4180 return 0;
4181}
4182
4183static int
4184nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
4185 struct drm_connector_state *connector_state)
4186{
4187 struct drm_encoder *encoder = connector_state->best_encoder;
4188 struct drm_crtc_state *crtc_state;
4189 struct drm_crtc *crtc;
4190 struct nv50_outp_atom *outp;
4191
4192 if (!(crtc = connector_state->crtc))
4193 return 0;
4194
4195 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
4196 if (crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
4197 outp = nv50_disp_outp_atomic_add(atom, encoder);
4198 if (IS_ERR(outp))
4199 return PTR_ERR(outp);
4200
4201 outp->set.ctrl = true;
4202 atom->lock_core = true;
4203 }
4204
4205 return 0;
4206}
4207
4208static int
4209nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
4210{
4211 struct nv50_atom *atom = nv50_atom(state);
4212 struct drm_connector_state *connector_state;
4213 struct drm_connector *connector;
4214 int ret, i;
4215
4216 ret = drm_atomic_helper_check(dev, state);
4217 if (ret)
4218 return ret;
4219
4220 for_each_connector_in_state(state, connector, connector_state, i) {
4221 ret = nv50_disp_outp_atomic_check_clr(atom, connector);
4222 if (ret)
4223 return ret;
4224
4225 ret = nv50_disp_outp_atomic_check_set(atom, connector_state);
4226 if (ret)
4227 return ret;
4228 }
4229
4230 return 0;
4231}
4232
4233static void
4234nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
4235{
4236 struct nv50_atom *atom = nv50_atom(state);
4237 struct nv50_outp_atom *outp, *outt;
4238
4239 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
4240 list_del(&outp->head);
4241 kfree(outp);
4242 }
4243
4244 drm_atomic_state_default_clear(state);
4245}
4246
4247static void
4248nv50_disp_atomic_state_free(struct drm_atomic_state *state)
4249{
4250 struct nv50_atom *atom = nv50_atom(state);
4251 drm_atomic_state_default_release(&atom->state);
4252 kfree(atom);
4253}
4254
4255static struct drm_atomic_state *
4256nv50_disp_atomic_state_alloc(struct drm_device *dev)
4257{
4258 struct nv50_atom *atom;
4259 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
4260 drm_atomic_state_init(dev, &atom->state) < 0) {
4261 kfree(atom);
4262 return NULL;
4263 }
4264 INIT_LIST_HEAD(&atom->outp);
4265 return &atom->state;
4266}
4267
4268static const struct drm_mode_config_funcs
4269nv50_disp_func = {
4270 .fb_create = nouveau_user_framebuffer_create,
4271 .output_poll_changed = nouveau_fbcon_output_poll_changed,
4272 .atomic_check = nv50_disp_atomic_check,
4273 .atomic_commit = nv50_disp_atomic_commit,
4274 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
4275 .atomic_state_clear = nv50_disp_atomic_state_clear,
4276 .atomic_state_free = nv50_disp_atomic_state_free,
4277};
4278
4279/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10004280 * Init
4281 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10004282
Ben Skeggs2a44e492011-11-09 11:36:33 +10004283void
Ben Skeggse225f442012-11-21 14:40:21 +10004284nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004285{
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004286 struct nouveau_encoder *nv_encoder;
4287 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10004288 struct drm_plane *plane;
4289
4290 drm_for_each_plane(plane, dev) {
4291 struct nv50_wndw *wndw = nv50_wndw(plane);
4292 if (plane->funcs != &nv50_wndw)
4293 continue;
4294 nv50_wndw_fini(wndw);
4295 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004296
4297 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4298 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
4299 nv_encoder = nouveau_encoder(encoder);
4300 nv50_mstm_fini(nv_encoder->dp.mstm);
4301 }
4302 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10004303}
4304
4305int
Ben Skeggse225f442012-11-21 14:40:21 +10004306nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004307{
Ben Skeggs354d3502016-11-04 17:20:36 +10004308 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10004309 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004310 struct drm_crtc *crtc;
4311 u32 *push;
4312
4313 push = evo_wait(nv50_mast(dev), 32);
4314 if (!push)
4315 return -EBUSY;
4316
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004317 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10004318 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004319 evo_kick(push, nv50_mast(dev));
Ben Skeggs973f10c2016-11-04 17:20:36 +10004320
Ben Skeggs354d3502016-11-04 17:20:36 +10004321 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4322 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
4323 const struct drm_encoder_helper_funcs *help;
4324 struct nouveau_encoder *nv_encoder;
4325
4326 nv_encoder = nouveau_encoder(encoder);
Ben Skeggs354d3502016-11-04 17:20:36 +10004327 help = encoder->helper_private;
4328 if (help && help->dpms)
4329 help->dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10004330
4331 nv50_mstm_init(nv_encoder->dp.mstm);
Ben Skeggs354d3502016-11-04 17:20:36 +10004332 }
4333 }
4334
Ben Skeggse1ef6b42016-11-04 17:20:36 +10004335 drm_for_each_crtc(crtc, dev) {
Ben Skeggs9bfdee92016-11-04 17:20:36 +10004336 nv50_head_lut_load(crtc);
Ben Skeggse1ef6b42016-11-04 17:20:36 +10004337 }
4338
Ben Skeggs973f10c2016-11-04 17:20:36 +10004339 drm_for_each_plane(plane, dev) {
4340 struct nv50_wndw *wndw = nv50_wndw(plane);
4341 if (plane->funcs != &nv50_wndw)
4342 continue;
4343 nv50_wndw_init(wndw);
4344 }
4345
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004346 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004347}
4348
4349void
Ben Skeggse225f442012-11-21 14:40:21 +10004350nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004351{
Ben Skeggse225f442012-11-21 14:40:21 +10004352 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004353
Ben Skeggs0ad72862014-08-10 04:10:22 +10004354 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10004355
Ben Skeggs816af2f2011-11-16 15:48:48 +10004356 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004357 if (disp->sync)
4358 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10004359 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10004360
Ben Skeggs77145f12012-07-31 16:16:21 +10004361 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004362 kfree(disp);
4363}
4364
Ben Skeggs839ca902016-11-04 17:20:36 +10004365MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
4366static int nouveau_atomic = 0;
4367module_param_named(atomic, nouveau_atomic, int, 0400);
4368
Ben Skeggs26f6d882011-07-04 16:25:18 +10004369int
Ben Skeggse225f442012-11-21 14:40:21 +10004370nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004371{
Ben Skeggs1167c6b2016-05-18 13:57:42 +10004372 struct nvif_device *device = &nouveau_drm(dev)->client.device;
Ben Skeggs77145f12012-07-31 16:16:21 +10004373 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10004374 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004375 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10004376 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10004377 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10004378 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004379
4380 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
4381 if (!disp)
4382 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10004383
Ben Skeggs839ca902016-11-04 17:20:36 +10004384 mutex_init(&disp->mutex);
4385
Ben Skeggs77145f12012-07-31 16:16:21 +10004386 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10004387 nouveau_display(dev)->dtor = nv50_display_destroy;
4388 nouveau_display(dev)->init = nv50_display_init;
4389 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs0ad72862014-08-10 04:10:22 +10004390 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs839ca902016-11-04 17:20:36 +10004391 dev->mode_config.funcs = &nv50_disp_func;
4392 if (nouveau_atomic)
4393 dev->driver->driver_features |= DRIVER_ATOMIC;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004394
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004395 /* small shared memory area we use for notifiers and semaphores */
Ben Skeggsbab7cc12016-05-24 17:26:48 +10004396 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01004397 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004398 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10004399 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004400 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004401 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004402 if (ret)
4403 nouveau_bo_unpin(disp->sync);
4404 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004405 if (ret)
4406 nouveau_bo_ref(NULL, &disp->sync);
4407 }
4408
4409 if (ret)
4410 goto out;
4411
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004412 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10004413 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10004414 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004415 if (ret)
4416 goto out;
4417
Ben Skeggs438d99e2011-07-05 16:48:06 +10004418 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10004419 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsa01ca782015-08-20 14:54:15 +10004420 crtcs = nvif_rd32(&device->object, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10004421 else
4422 crtcs = 2;
4423
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10004424 for (i = 0; i < crtcs; i++) {
Ben Skeggs9bfdee92016-11-04 17:20:36 +10004425 ret = nv50_head_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10004426 if (ret)
4427 goto out;
4428 }
4429
Ben Skeggs83fc0832011-07-05 13:08:40 +10004430 /* create encoder/connector objects based on VBIOS DCB table */
4431 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
4432 connector = nouveau_connector_create(dev, dcbe->connector);
4433 if (IS_ERR(connector))
4434 continue;
4435
Ben Skeggseb6313a2013-02-11 09:52:58 +10004436 if (dcbe->location == DCB_LOC_ON_CHIP) {
4437 switch (dcbe->type) {
4438 case DCB_OUTPUT_TMDS:
4439 case DCB_OUTPUT_LVDS:
4440 case DCB_OUTPUT_DP:
4441 ret = nv50_sor_create(connector, dcbe);
4442 break;
4443 case DCB_OUTPUT_ANALOG:
4444 ret = nv50_dac_create(connector, dcbe);
4445 break;
4446 default:
4447 ret = -ENODEV;
4448 break;
4449 }
4450 } else {
4451 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004452 }
4453
Ben Skeggseb6313a2013-02-11 09:52:58 +10004454 if (ret) {
4455 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
4456 dcbe->location, dcbe->type,
4457 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10004458 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004459 }
4460 }
4461
4462 /* cull any connectors we created that don't have an encoder */
4463 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
4464 if (connector->encoder_ids[0])
4465 continue;
4466
Ben Skeggs77145f12012-07-31 16:16:21 +10004467 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03004468 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004469 connector->funcs->destroy(connector);
4470 }
4471
Ben Skeggs26f6d882011-07-04 16:25:18 +10004472out:
4473 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10004474 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004475 return ret;
4476}