blob: c4ad9910c162a27c7cdf270f118036eff45fa81d [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 *
421nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, u32 handle,
422 struct nouveau_framebuffer *fb)
423{
424 struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
425 struct nv50_dmac_ctxdma *ctxdma;
426 const u8 kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
427 struct {
428 struct nv_dma_v0 base;
429 union {
430 struct nv50_dma_v0 nv50;
431 struct gf100_dma_v0 gf100;
432 struct gf119_dma_v0 gf119;
433 };
434 } args = {};
435 u32 argc = sizeof(args.base);
436 int ret;
437
438 list_for_each_entry(ctxdma, &dmac->ctxdma, head) {
439 if (ctxdma->object.handle == handle)
440 return ctxdma;
441 }
442
443 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
444 return ERR_PTR(-ENOMEM);
445 list_add(&ctxdma->head, &dmac->ctxdma);
446
447 args.base.target = NV_DMA_V0_TARGET_VRAM;
448 args.base.access = NV_DMA_V0_ACCESS_RDWR;
449 args.base.start = 0;
450 args.base.limit = drm->device.info.ram_user - 1;
451
452 if (drm->device.info.chipset < 0x80) {
453 args.nv50.part = NV50_DMA_V0_PART_256;
454 argc += sizeof(args.nv50);
455 } else
456 if (drm->device.info.chipset < 0xc0) {
457 args.nv50.part = NV50_DMA_V0_PART_256;
458 args.nv50.kind = kind;
459 argc += sizeof(args.nv50);
460 } else
461 if (drm->device.info.chipset < 0xd0) {
462 args.gf100.kind = kind;
463 argc += sizeof(args.gf100);
464 } else {
465 args.gf119.page = GF119_DMA_V0_PAGE_LP;
466 args.gf119.kind = kind;
467 argc += sizeof(args.gf119);
468 }
469
470 ret = nvif_object_init(&dmac->base.user, handle, NV_DMA_IN_MEMORY,
471 &args, argc, &ctxdma->object);
472 if (ret) {
473 nv50_dmac_ctxdma_del(ctxdma);
474 return ERR_PTR(ret);
475 }
476
477 return ctxdma;
478}
479
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 Skeggsfd478772016-07-09 10:41:01 +1000577 GP104_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 Skeggs8dda53f2013-07-09 12:35:55 +1000661 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000662 struct nv50_ovly ovly;
663 struct nv50_oimm oimm;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000664
Ben Skeggs973f10c2016-11-04 17:20:36 +1000665 struct nv50_base *_base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000666};
667
Ben Skeggse225f442012-11-21 14:40:21 +1000668#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
Ben Skeggse225f442012-11-21 14:40:21 +1000669#define nv50_ovly(c) (&nv50_head(c)->ovly)
670#define nv50_oimm(c) (&nv50_head(c)->oimm)
671#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000672#define nv50_vers(c) nv50_chan(c)->user.oclass
673
Ben Skeggse225f442012-11-21 14:40:21 +1000674struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000675 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000676 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000677
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000678 struct nouveau_bo *sync;
Ben Skeggs839ca902016-11-04 17:20:36 +1000679
680 struct mutex mutex;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000681};
682
Ben Skeggse225f442012-11-21 14:40:21 +1000683static struct nv50_disp *
684nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000685{
Ben Skeggs77145f12012-07-31 16:16:21 +1000686 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000687}
688
Ben Skeggse225f442012-11-21 14:40:21 +1000689#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000690
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000691/******************************************************************************
692 * EVO channel helpers
693 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000694static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000695evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000696{
Ben Skeggse225f442012-11-21 14:40:21 +1000697 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000698 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000699 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000700
Daniel Vetter59ad1462012-12-02 14:49:44 +0100701 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000702 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000703 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000704
Ben Skeggs0ad72862014-08-10 04:10:22 +1000705 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000706 if (nvif_msec(device, 2000,
707 if (!nvif_rd32(&dmac->base.user, 0x0004))
708 break;
709 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100710 mutex_unlock(&dmac->lock);
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000711 printk(KERN_ERR "nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000712 return NULL;
713 }
714
715 put = 0;
716 }
717
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000718 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000719}
720
721static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000722evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000723{
Ben Skeggse225f442012-11-21 14:40:21 +1000724 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000725 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100726 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000727}
728
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000729#define evo_mthd(p,m,s) do { \
730 const u32 _m = (m), _s = (s); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000731 if (drm_debug & DRM_UT_KMS) \
732 printk(KERN_ERR "%04x %d %s\n", _m, _s, __func__); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000733 *((p)++) = ((_s << 18) | _m); \
734} while(0)
Ben Skeggs7f55a072016-11-04 17:20:36 +1000735
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000736#define evo_data(p,d) do { \
737 const u32 _d = (d); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000738 if (drm_debug & DRM_UT_KMS) \
739 printk(KERN_ERR "\t%08x\n", _d); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000740 *((p)++) = _d; \
741} while(0)
Ben Skeggs51beb422011-07-05 10:33:08 +1000742
Ben Skeggs3376ee32011-11-12 14:28:12 +1000743static bool
744evo_sync_wait(void *data)
745{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500746 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
747 return true;
748 usleep_range(1, 2);
749 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000750}
751
752static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000753evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000754{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000755 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000756 struct nv50_disp *disp = nv50_disp(dev);
757 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000758 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000759 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000760 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000761 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000762 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000763 evo_mthd(push, 0x0080, 2);
764 evo_data(push, 0x00000000);
765 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000766 evo_kick(push, mast);
Ben Skeggs54442042015-08-20 14:54:11 +1000767 if (nvif_msec(device, 2000,
768 if (evo_sync_wait(disp->sync))
769 break;
770 ) >= 0)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000771 return 0;
772 }
773
774 return -EBUSY;
775}
776
777/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +1000778 * Plane
779 *****************************************************************************/
780#define nv50_wndw(p) container_of((p), struct nv50_wndw, plane)
781
782struct nv50_wndw {
783 const struct nv50_wndw_func *func;
784 struct nv50_dmac *dmac;
785
786 struct drm_plane plane;
787
788 struct nvif_notify notify;
789 u16 ntfy;
790 u16 sema;
791 u32 data;
792
Ben Skeggs973f10c2016-11-04 17:20:36 +1000793 struct nv50_wndw_atom asy;
794};
795
796struct nv50_wndw_func {
797 void *(*dtor)(struct nv50_wndw *);
798 int (*acquire)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
799 struct nv50_head_atom *asyh);
800 void (*release)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
801 struct nv50_head_atom *asyh);
802 void (*prepare)(struct nv50_wndw *, struct nv50_head_atom *asyh,
803 struct nv50_wndw_atom *asyw);
804
805 void (*sema_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
806 void (*sema_clr)(struct nv50_wndw *);
807 void (*ntfy_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
808 void (*ntfy_clr)(struct nv50_wndw *);
809 int (*ntfy_wait_begun)(struct nv50_wndw *, struct nv50_wndw_atom *);
810 void (*image_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
811 void (*image_clr)(struct nv50_wndw *);
812 void (*lut)(struct nv50_wndw *, struct nv50_wndw_atom *);
813 void (*point)(struct nv50_wndw *, struct nv50_wndw_atom *);
814
815 u32 (*update)(struct nv50_wndw *, u32 interlock);
816};
817
818static int
819nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
820{
821 if (asyw->set.ntfy)
822 return wndw->func->ntfy_wait_begun(wndw, asyw);
823 return 0;
824}
825
826static u32
827nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 interlock, bool flush,
828 struct nv50_wndw_atom *asyw)
829{
830 if (asyw->clr.sema && (!asyw->set.sema || flush))
831 wndw->func->sema_clr(wndw);
832 if (asyw->clr.ntfy && (!asyw->set.ntfy || flush))
833 wndw->func->ntfy_clr(wndw);
834 if (asyw->clr.image && (!asyw->set.image || flush))
835 wndw->func->image_clr(wndw);
836
837 return flush ? wndw->func->update(wndw, interlock) : 0;
838}
839
840static u32
841nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 interlock,
842 struct nv50_wndw_atom *asyw)
843{
844 if (interlock) {
845 asyw->image.mode = 0;
846 asyw->image.interval = 1;
847 }
848
849 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
850 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
851 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
852 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
853 if (asyw->set.point) wndw->func->point (wndw, asyw);
854
855 return wndw->func->update(wndw, interlock);
856}
857
858static void
859nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
860 struct nv50_wndw_atom *asyw,
861 struct nv50_head_atom *asyh)
862{
863 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
864 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
865 wndw->func->release(wndw, asyw, asyh);
866 asyw->ntfy.handle = 0;
867 asyw->sema.handle = 0;
868}
869
870static int
871nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
872 struct nv50_wndw_atom *asyw,
873 struct nv50_head_atom *asyh)
874{
875 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
876 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
877 int ret;
878
879 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
880 asyw->clip.x1 = 0;
881 asyw->clip.y1 = 0;
882 asyw->clip.x2 = asyh->state.mode.hdisplay;
883 asyw->clip.y2 = asyh->state.mode.vdisplay;
884
885 asyw->image.w = fb->base.width;
886 asyw->image.h = fb->base.height;
887 asyw->image.kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
888 if (asyw->image.kind) {
889 asyw->image.layout = 0;
890 if (drm->device.info.chipset >= 0xc0)
891 asyw->image.block = fb->nvbo->tile_mode >> 4;
892 else
893 asyw->image.block = fb->nvbo->tile_mode;
894 asyw->image.pitch = (fb->base.pitches[0] / 4) << 4;
895 } else {
896 asyw->image.layout = 1;
897 asyw->image.block = 0;
898 asyw->image.pitch = fb->base.pitches[0];
899 }
900
901 ret = wndw->func->acquire(wndw, asyw, asyh);
902 if (ret)
903 return ret;
904
905 if (asyw->set.image) {
906 if (!(asyw->image.mode = asyw->interval ? 0 : 1))
907 asyw->image.interval = asyw->interval;
908 else
909 asyw->image.interval = 0;
910 }
911
912 return 0;
913}
914
915static int
916nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
917{
918 struct nouveau_drm *drm = nouveau_drm(plane->dev);
919 struct nv50_wndw *wndw = nv50_wndw(plane);
Ben Skeggs839ca902016-11-04 17:20:36 +1000920 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
921 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000922 struct nv50_head_atom *harm = NULL, *asyh = NULL;
923 bool varm = false, asyv = false, asym = false;
924 int ret;
925
Ben Skeggs973f10c2016-11-04 17:20:36 +1000926 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
927 if (asyw->state.crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +1000928 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000929 if (IS_ERR(asyh))
930 return PTR_ERR(asyh);
931 asym = drm_atomic_crtc_needs_modeset(&asyh->state);
932 asyv = asyh->state.active;
933 }
934
935 if (armw->state.crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +1000936 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +1000937 if (IS_ERR(harm))
938 return PTR_ERR(harm);
Ben Skeggs839ca902016-11-04 17:20:36 +1000939 varm = harm->state.crtc->state->active;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000940 }
941
942 if (asyv) {
943 asyw->point.x = asyw->state.crtc_x;
944 asyw->point.y = asyw->state.crtc_y;
945 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
946 asyw->set.point = true;
947
948 if (!varm || asym || armw->state.fb != asyw->state.fb) {
949 ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh);
950 if (ret)
951 return ret;
952 }
953 } else
954 if (varm) {
955 nv50_wndw_atomic_check_release(wndw, asyw, harm);
956 } else {
957 return 0;
958 }
959
960 if (!asyv || asym) {
961 asyw->clr.ntfy = armw->ntfy.handle != 0;
962 asyw->clr.sema = armw->sema.handle != 0;
963 if (wndw->func->image_clr)
964 asyw->clr.image = armw->image.handle != 0;
965 asyw->set.lut = wndw->func->lut && asyv;
966 }
967
Ben Skeggs973f10c2016-11-04 17:20:36 +1000968 return 0;
969}
970
971static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000972nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
973{
974 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
975 struct nouveau_drm *drm = nouveau_drm(plane->dev);
976
977 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
978 if (!old_state->fb)
979 return;
980
981 nouveau_bo_unpin(fb->nvbo);
982}
983
984static int
985nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
986{
987 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
988 struct nouveau_drm *drm = nouveau_drm(plane->dev);
989 struct nv50_wndw *wndw = nv50_wndw(plane);
990 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
991 struct nv50_head_atom *asyh;
992 struct nv50_dmac_ctxdma *ctxdma;
993 u32 name;
994 u8 kind;
995 int ret;
996
997 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
998 if (!asyw->state.fb)
999 return 0;
1000 kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
1001 name = 0xfb000000 | kind;
1002
1003 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
1004 if (ret)
1005 return ret;
1006
1007 ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, name, fb);
1008 if (IS_ERR(ctxdma)) {
1009 nouveau_bo_unpin(fb->nvbo);
1010 return PTR_ERR(ctxdma);
1011 }
1012
1013 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
1014 asyw->image.handle = ctxdma->object.handle;
1015 asyw->image.offset = fb->nvbo->bo.offset;
1016
1017 if (wndw->func->prepare) {
1018 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
1019 if (IS_ERR(asyh))
1020 return PTR_ERR(asyh);
1021
1022 wndw->func->prepare(wndw, asyh, asyw);
1023 }
1024
1025 return 0;
1026}
1027
1028static const struct drm_plane_helper_funcs
1029nv50_wndw_helper = {
1030 .prepare_fb = nv50_wndw_prepare_fb,
1031 .cleanup_fb = nv50_wndw_cleanup_fb,
1032 .atomic_check = nv50_wndw_atomic_check,
1033};
1034
1035static void
Ben Skeggs973f10c2016-11-04 17:20:36 +10001036nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
1037 struct drm_plane_state *state)
1038{
1039 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
1040 __drm_atomic_helper_plane_destroy_state(&asyw->state);
1041 dma_fence_put(asyw->state.fence);
1042 kfree(asyw);
1043}
1044
1045static struct drm_plane_state *
1046nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
1047{
1048 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
1049 struct nv50_wndw_atom *asyw;
1050 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
1051 return NULL;
1052 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
1053 asyw->state.fence = NULL;
1054 asyw->interval = 1;
1055 asyw->sema = armw->sema;
1056 asyw->ntfy = armw->ntfy;
1057 asyw->image = armw->image;
1058 asyw->point = armw->point;
1059 asyw->lut = armw->lut;
1060 asyw->clr.mask = 0;
1061 asyw->set.mask = 0;
1062 return &asyw->state;
1063}
1064
1065static void
1066nv50_wndw_reset(struct drm_plane *plane)
1067{
1068 struct nv50_wndw_atom *asyw;
1069
1070 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
1071 return;
1072
1073 if (plane->state)
1074 plane->funcs->atomic_destroy_state(plane, plane->state);
1075 plane->state = &asyw->state;
1076 plane->state->plane = plane;
1077 plane->state->rotation = DRM_ROTATE_0;
1078}
1079
1080static void
1081nv50_wndw_destroy(struct drm_plane *plane)
1082{
1083 struct nv50_wndw *wndw = nv50_wndw(plane);
1084 void *data;
1085 nvif_notify_fini(&wndw->notify);
1086 data = wndw->func->dtor(wndw);
1087 drm_plane_cleanup(&wndw->plane);
1088 kfree(data);
1089}
1090
1091static const struct drm_plane_funcs
1092nv50_wndw = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001093 .update_plane = drm_atomic_helper_update_plane,
1094 .disable_plane = drm_atomic_helper_disable_plane,
Ben Skeggs973f10c2016-11-04 17:20:36 +10001095 .destroy = nv50_wndw_destroy,
1096 .reset = nv50_wndw_reset,
1097 .set_property = drm_atomic_helper_plane_set_property,
1098 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
1099 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
1100};
1101
1102static void
1103nv50_wndw_fini(struct nv50_wndw *wndw)
1104{
1105 nvif_notify_put(&wndw->notify);
1106}
1107
1108static void
1109nv50_wndw_init(struct nv50_wndw *wndw)
1110{
1111 nvif_notify_get(&wndw->notify);
1112}
1113
1114static int
1115nv50_wndw_ctor(const struct nv50_wndw_func *func, struct drm_device *dev,
1116 enum drm_plane_type type, const char *name, int index,
1117 struct nv50_dmac *dmac, const u32 *format, int nformat,
1118 struct nv50_wndw *wndw)
1119{
1120 int ret;
1121
1122 wndw->func = func;
1123 wndw->dmac = dmac;
1124
1125 ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw, format,
1126 nformat, type, "%s-%d", name, index);
1127 if (ret)
1128 return ret;
1129
Ben Skeggs839ca902016-11-04 17:20:36 +10001130 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001131 return 0;
1132}
1133
1134/******************************************************************************
Ben Skeggs22e927d2016-11-04 17:20:36 +10001135 * Cursor plane
1136 *****************************************************************************/
1137#define nv50_curs(p) container_of((p), struct nv50_curs, wndw)
1138
1139struct nv50_curs {
1140 struct nv50_wndw wndw;
1141 struct nvif_object chan;
1142};
1143
1144static u32
1145nv50_curs_update(struct nv50_wndw *wndw, u32 interlock)
1146{
1147 struct nv50_curs *curs = nv50_curs(wndw);
1148 nvif_wr32(&curs->chan, 0x0080, 0x00000000);
1149 return 0;
1150}
1151
1152static void
1153nv50_curs_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1154{
1155 struct nv50_curs *curs = nv50_curs(wndw);
1156 nvif_wr32(&curs->chan, 0x0084, (asyw->point.y << 16) | asyw->point.x);
1157}
1158
1159static void
1160nv50_curs_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
1161 struct nv50_wndw_atom *asyw)
1162{
1163 asyh->curs.handle = nv50_disp(wndw->plane.dev)->mast.base.vram.handle;
1164 asyh->curs.offset = asyw->image.offset;
1165 asyh->set.curs = asyh->curs.visible;
1166}
1167
1168static void
1169nv50_curs_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1170 struct nv50_head_atom *asyh)
1171{
1172 asyh->curs.visible = false;
1173}
1174
1175static int
1176nv50_curs_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1177 struct nv50_head_atom *asyh)
1178{
1179 int ret;
1180
1181 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1182 DRM_PLANE_HELPER_NO_SCALING,
1183 DRM_PLANE_HELPER_NO_SCALING,
1184 true, true);
1185 asyh->curs.visible = asyw->state.visible;
1186 if (ret || !asyh->curs.visible)
1187 return ret;
1188
1189 switch (asyw->state.fb->width) {
1190 case 32: asyh->curs.layout = 0; break;
1191 case 64: asyh->curs.layout = 1; break;
1192 default:
1193 return -EINVAL;
1194 }
1195
1196 if (asyw->state.fb->width != asyw->state.fb->height)
1197 return -EINVAL;
1198
1199 switch (asyw->state.fb->pixel_format) {
1200 case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
1201 default:
1202 WARN_ON(1);
1203 return -EINVAL;
1204 }
1205
1206 return 0;
1207}
1208
1209static void *
1210nv50_curs_dtor(struct nv50_wndw *wndw)
1211{
1212 struct nv50_curs *curs = nv50_curs(wndw);
1213 nvif_object_fini(&curs->chan);
1214 return curs;
1215}
1216
1217static const u32
1218nv50_curs_format[] = {
1219 DRM_FORMAT_ARGB8888,
1220};
1221
1222static const struct nv50_wndw_func
1223nv50_curs = {
1224 .dtor = nv50_curs_dtor,
1225 .acquire = nv50_curs_acquire,
1226 .release = nv50_curs_release,
1227 .prepare = nv50_curs_prepare,
1228 .point = nv50_curs_point,
1229 .update = nv50_curs_update,
1230};
1231
1232static int
1233nv50_curs_new(struct nouveau_drm *drm, struct nv50_head *head,
1234 struct nv50_curs **pcurs)
1235{
1236 static const struct nvif_mclass curses[] = {
1237 { GK104_DISP_CURSOR, 0 },
1238 { GF110_DISP_CURSOR, 0 },
1239 { GT214_DISP_CURSOR, 0 },
1240 { G82_DISP_CURSOR, 0 },
1241 { NV50_DISP_CURSOR, 0 },
1242 {}
1243 };
1244 struct nv50_disp_cursor_v0 args = {
1245 .head = head->base.index,
1246 };
1247 struct nv50_disp *disp = nv50_disp(drm->dev);
1248 struct nv50_curs *curs;
1249 int cid, ret;
1250
1251 cid = nvif_mclass(disp->disp, curses);
1252 if (cid < 0) {
1253 NV_ERROR(drm, "No supported cursor immediate class\n");
1254 return cid;
1255 }
1256
1257 if (!(curs = *pcurs = kzalloc(sizeof(*curs), GFP_KERNEL)))
1258 return -ENOMEM;
1259
1260 ret = nv50_wndw_ctor(&nv50_curs, drm->dev, DRM_PLANE_TYPE_CURSOR,
1261 "curs", head->base.index, &disp->mast.base,
1262 nv50_curs_format, ARRAY_SIZE(nv50_curs_format),
1263 &curs->wndw);
1264 if (ret) {
1265 kfree(curs);
1266 return ret;
1267 }
1268
1269 ret = nvif_object_init(disp->disp, 0, curses[cid].oclass, &args,
1270 sizeof(args), &curs->chan);
1271 if (ret) {
1272 NV_ERROR(drm, "curs%04x allocation failed: %d\n",
1273 curses[cid].oclass, ret);
1274 return ret;
1275 }
1276
1277 return 0;
1278}
1279
1280/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +10001281 * Primary plane
1282 *****************************************************************************/
1283#define nv50_base(p) container_of((p), struct nv50_base, wndw)
1284
1285struct nv50_base {
1286 struct nv50_wndw wndw;
1287 struct nv50_sync chan;
1288 int id;
1289};
1290
1291static int
1292nv50_base_notify(struct nvif_notify *notify)
1293{
1294 return NVIF_NOTIFY_KEEP;
1295}
1296
1297static void
1298nv50_base_lut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1299{
1300 struct nv50_base *base = nv50_base(wndw);
1301 u32 *push;
1302 if ((push = evo_wait(&base->chan, 2))) {
1303 evo_mthd(push, 0x00e0, 1);
1304 evo_data(push, asyw->lut.enable << 30);
1305 evo_kick(push, &base->chan);
1306 }
1307}
1308
1309static void
1310nv50_base_image_clr(struct nv50_wndw *wndw)
1311{
1312 struct nv50_base *base = nv50_base(wndw);
1313 u32 *push;
1314 if ((push = evo_wait(&base->chan, 4))) {
1315 evo_mthd(push, 0x0084, 1);
1316 evo_data(push, 0x00000000);
1317 evo_mthd(push, 0x00c0, 1);
1318 evo_data(push, 0x00000000);
1319 evo_kick(push, &base->chan);
1320 }
1321}
1322
1323static void
1324nv50_base_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1325{
1326 struct nv50_base *base = nv50_base(wndw);
1327 const s32 oclass = base->chan.base.base.user.oclass;
1328 u32 *push;
1329 if ((push = evo_wait(&base->chan, 10))) {
1330 evo_mthd(push, 0x0084, 1);
1331 evo_data(push, (asyw->image.mode << 8) |
1332 (asyw->image.interval << 4));
1333 evo_mthd(push, 0x00c0, 1);
1334 evo_data(push, asyw->image.handle);
1335 if (oclass < G82_DISP_BASE_CHANNEL_DMA) {
1336 evo_mthd(push, 0x0800, 5);
1337 evo_data(push, asyw->image.offset >> 8);
1338 evo_data(push, 0x00000000);
1339 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1340 evo_data(push, (asyw->image.layout << 20) |
1341 asyw->image.pitch |
1342 asyw->image.block);
1343 evo_data(push, (asyw->image.kind << 16) |
1344 (asyw->image.format << 8));
1345 } else
1346 if (oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1347 evo_mthd(push, 0x0800, 5);
1348 evo_data(push, asyw->image.offset >> 8);
1349 evo_data(push, 0x00000000);
1350 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1351 evo_data(push, (asyw->image.layout << 20) |
1352 asyw->image.pitch |
1353 asyw->image.block);
1354 evo_data(push, asyw->image.format << 8);
1355 } else {
1356 evo_mthd(push, 0x0400, 5);
1357 evo_data(push, asyw->image.offset >> 8);
1358 evo_data(push, 0x00000000);
1359 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1360 evo_data(push, (asyw->image.layout << 24) |
1361 asyw->image.pitch |
1362 asyw->image.block);
1363 evo_data(push, asyw->image.format << 8);
1364 }
1365 evo_kick(push, &base->chan);
1366 }
1367}
1368
1369static void
1370nv50_base_ntfy_clr(struct nv50_wndw *wndw)
1371{
1372 struct nv50_base *base = nv50_base(wndw);
1373 u32 *push;
1374 if ((push = evo_wait(&base->chan, 2))) {
1375 evo_mthd(push, 0x00a4, 1);
1376 evo_data(push, 0x00000000);
1377 evo_kick(push, &base->chan);
1378 }
1379}
1380
1381static void
1382nv50_base_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1383{
1384 struct nv50_base *base = nv50_base(wndw);
1385 u32 *push;
1386 if ((push = evo_wait(&base->chan, 3))) {
1387 evo_mthd(push, 0x00a0, 2);
1388 evo_data(push, (asyw->ntfy.awaken << 30) | asyw->ntfy.offset);
1389 evo_data(push, asyw->ntfy.handle);
1390 evo_kick(push, &base->chan);
1391 }
1392}
1393
1394static void
1395nv50_base_sema_clr(struct nv50_wndw *wndw)
1396{
1397 struct nv50_base *base = nv50_base(wndw);
1398 u32 *push;
1399 if ((push = evo_wait(&base->chan, 2))) {
1400 evo_mthd(push, 0x0094, 1);
1401 evo_data(push, 0x00000000);
1402 evo_kick(push, &base->chan);
1403 }
1404}
1405
1406static void
1407nv50_base_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1408{
1409 struct nv50_base *base = nv50_base(wndw);
1410 u32 *push;
1411 if ((push = evo_wait(&base->chan, 5))) {
1412 evo_mthd(push, 0x0088, 4);
1413 evo_data(push, asyw->sema.offset);
1414 evo_data(push, asyw->sema.acquire);
1415 evo_data(push, asyw->sema.release);
1416 evo_data(push, asyw->sema.handle);
1417 evo_kick(push, &base->chan);
1418 }
1419}
1420
1421static u32
1422nv50_base_update(struct nv50_wndw *wndw, u32 interlock)
1423{
1424 struct nv50_base *base = nv50_base(wndw);
1425 u32 *push;
1426
1427 if (!(push = evo_wait(&base->chan, 2)))
1428 return 0;
1429 evo_mthd(push, 0x0080, 1);
1430 evo_data(push, interlock);
1431 evo_kick(push, &base->chan);
1432
1433 if (base->chan.base.base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA)
1434 return interlock ? 2 << (base->id * 8) : 0;
1435 return interlock ? 2 << (base->id * 4) : 0;
1436}
1437
1438static int
1439nv50_base_ntfy_wait_begun(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1440{
1441 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
1442 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1443 if (nvif_msec(&drm->device, 2000ULL,
1444 u32 data = nouveau_bo_rd32(disp->sync, asyw->ntfy.offset / 4);
1445 if ((data & 0xc0000000) == 0x40000000)
1446 break;
1447 usleep_range(1, 2);
1448 ) < 0)
1449 return -ETIMEDOUT;
1450 return 0;
1451}
1452
1453static void
1454nv50_base_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1455 struct nv50_head_atom *asyh)
1456{
1457 asyh->base.cpp = 0;
1458}
1459
1460static int
1461nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1462 struct nv50_head_atom *asyh)
1463{
1464 const u32 format = asyw->state.fb->pixel_format;
1465 const struct drm_format_info *info;
1466 int ret;
1467
1468 info = drm_format_info(format);
1469 if (!info || !info->depth)
1470 return -EINVAL;
1471
1472 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1473 DRM_PLANE_HELPER_NO_SCALING,
1474 DRM_PLANE_HELPER_NO_SCALING,
1475 false, true);
1476 if (ret)
1477 return ret;
1478
1479 asyh->base.depth = info->depth;
1480 asyh->base.cpp = info->cpp[0];
1481 asyh->base.x = asyw->state.src.x1 >> 16;
1482 asyh->base.y = asyw->state.src.y1 >> 16;
1483 asyh->base.w = asyw->state.fb->width;
1484 asyh->base.h = asyw->state.fb->height;
1485
1486 switch (format) {
1487 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
1488 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
1489 case DRM_FORMAT_XRGB1555 :
1490 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
1491 case DRM_FORMAT_XRGB8888 :
1492 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
1493 case DRM_FORMAT_XBGR2101010:
1494 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
1495 case DRM_FORMAT_XBGR8888 :
1496 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
1497 default:
1498 WARN_ON(1);
1499 return -EINVAL;
1500 }
1501
1502 asyw->lut.enable = 1;
1503 asyw->set.image = true;
1504 return 0;
1505}
1506
1507static void *
1508nv50_base_dtor(struct nv50_wndw *wndw)
1509{
1510 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1511 struct nv50_base *base = nv50_base(wndw);
1512 nv50_dmac_destroy(&base->chan.base, disp->disp);
1513 return base;
1514}
1515
1516static const u32
1517nv50_base_format[] = {
1518 DRM_FORMAT_C8,
1519 DRM_FORMAT_RGB565,
1520 DRM_FORMAT_XRGB1555,
1521 DRM_FORMAT_ARGB1555,
1522 DRM_FORMAT_XRGB8888,
1523 DRM_FORMAT_ARGB8888,
1524 DRM_FORMAT_XBGR2101010,
1525 DRM_FORMAT_ABGR2101010,
1526 DRM_FORMAT_XBGR8888,
1527 DRM_FORMAT_ABGR8888,
1528};
1529
1530static const struct nv50_wndw_func
1531nv50_base = {
1532 .dtor = nv50_base_dtor,
1533 .acquire = nv50_base_acquire,
1534 .release = nv50_base_release,
1535 .sema_set = nv50_base_sema_set,
1536 .sema_clr = nv50_base_sema_clr,
1537 .ntfy_set = nv50_base_ntfy_set,
1538 .ntfy_clr = nv50_base_ntfy_clr,
1539 .ntfy_wait_begun = nv50_base_ntfy_wait_begun,
1540 .image_set = nv50_base_image_set,
1541 .image_clr = nv50_base_image_clr,
1542 .lut = nv50_base_lut,
1543 .update = nv50_base_update,
1544};
1545
1546static int
1547nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
1548 struct nv50_base **pbase)
1549{
1550 struct nv50_disp *disp = nv50_disp(drm->dev);
1551 struct nv50_base *base;
1552 int ret;
1553
1554 if (!(base = *pbase = kzalloc(sizeof(*base), GFP_KERNEL)))
1555 return -ENOMEM;
1556 base->id = head->base.index;
1557 base->wndw.ntfy = EVO_FLIP_NTFY0(base->id);
1558 base->wndw.sema = EVO_FLIP_SEM0(base->id);
1559 base->wndw.data = 0x00000000;
1560
1561 ret = nv50_wndw_ctor(&nv50_base, drm->dev, DRM_PLANE_TYPE_PRIMARY,
1562 "base", base->id, &base->chan.base,
1563 nv50_base_format, ARRAY_SIZE(nv50_base_format),
1564 &base->wndw);
1565 if (ret) {
1566 kfree(base);
1567 return ret;
1568 }
1569
1570 ret = nv50_base_create(&drm->device, disp->disp, base->id,
1571 disp->sync->bo.offset, &base->chan);
1572 if (ret)
1573 return ret;
1574
1575 return nvif_notify_init(&base->chan.base.base.user, nv50_base_notify,
1576 false,
1577 NV50_DISP_BASE_CHANNEL_DMA_V0_NTFY_UEVENT,
1578 &(struct nvif_notify_uevent_req) {},
1579 sizeof(struct nvif_notify_uevent_req),
1580 sizeof(struct nvif_notify_uevent_rep),
1581 &base->wndw.notify);
1582}
1583
1584/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001585 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +10001586 *****************************************************************************/
1587struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +10001588nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +10001589{
Ben Skeggse225f442012-11-21 14:40:21 +10001590 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001591}
1592
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001593struct nv50_display_flip {
1594 struct nv50_disp *disp;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001595 struct nv50_base *base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001596};
1597
1598static bool
1599nv50_display_flip_wait(void *data)
1600{
1601 struct nv50_display_flip *flip = data;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001602 if (nouveau_bo_rd32(flip->disp->sync, flip->base->wndw.sema / 4) ==
1603 flip->base->wndw.data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001604 return true;
1605 usleep_range(1, 2);
1606 return false;
1607}
1608
Ben Skeggs3376ee32011-11-12 14:28:12 +10001609void
Ben Skeggse225f442012-11-21 14:40:21 +10001610nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +10001611{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001612 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001613 struct nv50_base *base = nv50_head(crtc)->_base;
1614 struct nv50_wndw *wndw = &base->wndw;
1615 struct nv50_wndw_atom *asyw = &wndw->asy;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001616 struct nv50_display_flip flip = {
1617 .disp = nv50_disp(crtc->dev),
Ben Skeggs973f10c2016-11-04 17:20:36 +10001618 .base = base,
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001619 };
Ben Skeggs3376ee32011-11-12 14:28:12 +10001620
Ben Skeggs973f10c2016-11-04 17:20:36 +10001621 asyw->state.crtc = NULL;
1622 asyw->state.fb = NULL;
1623 nv50_wndw_atomic_check(&wndw->plane, &asyw->state);
1624 nv50_wndw_flush_clr(wndw, 0, true, asyw);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001625
Ben Skeggs54442042015-08-20 14:54:11 +10001626 nvif_msec(device, 2000,
1627 if (nv50_display_flip_wait(&flip))
1628 break;
1629 );
Ben Skeggs3376ee32011-11-12 14:28:12 +10001630}
1631
1632int
Ben Skeggse225f442012-11-21 14:40:21 +10001633nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001634 struct nouveau_channel *chan, u32 swap_interval)
1635{
1636 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001637 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001638 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001639 struct nv50_base *base = nv50_head(crtc)->_base;
1640 struct nv50_wndw *wndw = &base->wndw;
1641 struct nv50_wndw_atom *asyw = &wndw->asy;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001642 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001643
Ben Skeggs9ba83102014-12-22 19:50:23 +10001644 if (crtc->primary->fb->width != fb->width ||
1645 crtc->primary->fb->height != fb->height)
1646 return -EINVAL;
1647
Ben Skeggsf60b6e72013-03-19 15:20:00 +10001648 if (chan == NULL)
1649 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001650
Ben Skeggsa01ca782015-08-20 14:54:15 +10001651 if (chan && chan->user.oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001652 ret = RING_SPACE(chan, 8);
1653 if (ret)
1654 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +10001655
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001656 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001657 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001658 OUT_RING (chan, base->wndw.sema ^ 0x10);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001659 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001660 OUT_RING (chan, base->wndw.data + 1);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001661 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001662 OUT_RING (chan, base->wndw.sema);
1663 OUT_RING (chan, base->wndw.data);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001664 } else
Ben Skeggsa01ca782015-08-20 14:54:15 +10001665 if (chan && chan->user.oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001666 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + base->wndw.sema;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001667 ret = RING_SPACE(chan, 12);
1668 if (ret)
1669 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +10001670
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001671 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001672 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001673 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1674 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
1675 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001676 OUT_RING (chan, base->wndw.data + 1);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001677 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
1678 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1679 OUT_RING (chan, upper_32_bits(addr));
1680 OUT_RING (chan, lower_32_bits(addr));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001681 OUT_RING (chan, base->wndw.data);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001682 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
1683 } else
1684 if (chan) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001685 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + base->wndw.sema;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001686 ret = RING_SPACE(chan, 10);
1687 if (ret)
1688 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +10001689
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001690 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1691 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
1692 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001693 OUT_RING (chan, base->wndw.data + 1);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001694 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
1695 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
1696 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1697 OUT_RING (chan, upper_32_bits(addr));
1698 OUT_RING (chan, lower_32_bits(addr));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001699 OUT_RING (chan, base->wndw.data);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001700 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
1701 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
1702 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -05001703
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001704 if (chan) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001705 base->wndw.sema ^= 0x10;
1706 base->wndw.data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001707 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001708 }
1709
1710 /* queue the flip */
Ben Skeggs973f10c2016-11-04 17:20:36 +10001711 asyw->state.crtc = &head->base.base;
1712 asyw->state.fb = fb;
1713 asyw->interval = swap_interval;
1714 asyw->image.handle = nv_fb->r_handle;
1715 asyw->image.offset = nv_fb->nvbo->bo.offset;
1716 asyw->sema.handle = base->chan.base.sync.handle;
1717 asyw->sema.offset = base->wndw.sema;
1718 asyw->sema.acquire = base->wndw.data++;
1719 asyw->sema.release = base->wndw.data;
1720 nv50_wndw_atomic_check(&wndw->plane, &asyw->state);
1721 asyw->set.sema = true;
1722 nv50_wndw_flush_set(wndw, 0, asyw);
1723 nv50_wndw_wait_armed(wndw, asyw);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001724
1725 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001726 return 0;
1727}
1728
Ben Skeggs26f6d882011-07-04 16:25:18 +10001729/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001730 * Head
1731 *****************************************************************************/
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001732static void
Ben Skeggs7e08d672016-11-04 17:20:36 +10001733nv50_head_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
1734{
1735 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1736 u32 *push;
1737 if ((push = evo_wait(core, 2))) {
1738 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1739 evo_mthd(push, 0x08a8 + (head->base.index * 0x400), 1);
1740 else
1741 evo_mthd(push, 0x0498 + (head->base.index * 0x300), 1);
1742 evo_data(push, (asyh->procamp.sat.sin << 20) |
1743 (asyh->procamp.sat.cos << 8));
1744 evo_kick(push, core);
1745 }
1746}
1747
1748static void
Ben Skeggs7e918332016-11-04 17:20:36 +10001749nv50_head_dither(struct nv50_head *head, struct nv50_head_atom *asyh)
1750{
1751 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1752 u32 *push;
1753 if ((push = evo_wait(core, 2))) {
1754 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1755 evo_mthd(push, 0x08a0 + (head->base.index * 0x0400), 1);
1756 else
1757 if (core->base.user.oclass < GK104_DISP_CORE_CHANNEL_DMA)
1758 evo_mthd(push, 0x0490 + (head->base.index * 0x0300), 1);
1759 else
1760 evo_mthd(push, 0x04a0 + (head->base.index * 0x0300), 1);
1761 evo_data(push, (asyh->dither.mode << 3) |
1762 (asyh->dither.bits << 1) |
1763 asyh->dither.enable);
1764 evo_kick(push, core);
1765 }
1766}
1767
1768static void
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001769nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
1770{
1771 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1772 u32 bounds = 0;
1773 u32 *push;
1774
1775 if (asyh->base.cpp) {
1776 switch (asyh->base.cpp) {
1777 case 8: bounds |= 0x00000500; break;
1778 case 4: bounds |= 0x00000300; break;
1779 case 2: bounds |= 0x00000100; break;
1780 default:
1781 WARN_ON(1);
1782 break;
1783 }
1784 bounds |= 0x00000001;
1785 }
1786
1787 if ((push = evo_wait(core, 2))) {
1788 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1789 evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
1790 else
1791 evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
1792 evo_data(push, bounds);
1793 evo_kick(push, core);
1794 }
1795}
1796
1797static void
1798nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
1799{
1800 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1801 u32 bounds = 0;
1802 u32 *push;
1803
1804 if (asyh->base.cpp) {
1805 switch (asyh->base.cpp) {
1806 case 8: bounds |= 0x00000500; break;
1807 case 4: bounds |= 0x00000300; break;
1808 case 2: bounds |= 0x00000100; break;
1809 case 1: bounds |= 0x00000000; break;
1810 default:
1811 WARN_ON(1);
1812 break;
1813 }
1814 bounds |= 0x00000001;
1815 }
1816
1817 if ((push = evo_wait(core, 2))) {
1818 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1819 evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
1820 else
1821 evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
1822 evo_data(push, bounds);
1823 evo_kick(push, core);
1824 }
1825}
1826
1827static void
Ben Skeggsea8ee392016-11-04 17:20:36 +10001828nv50_head_curs_clr(struct nv50_head *head)
1829{
1830 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1831 u32 *push;
1832 if ((push = evo_wait(core, 4))) {
1833 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1834 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1835 evo_data(push, 0x05000000);
1836 } else
1837 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1838 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1839 evo_data(push, 0x05000000);
1840 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1841 evo_data(push, 0x00000000);
1842 } else {
1843 evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
1844 evo_data(push, 0x05000000);
1845 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1846 evo_data(push, 0x00000000);
1847 }
1848 evo_kick(push, core);
1849 }
1850}
1851
1852static void
1853nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1854{
1855 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1856 u32 *push;
1857 if ((push = evo_wait(core, 5))) {
1858 if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
1859 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1860 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1861 (asyh->curs.format << 24));
1862 evo_data(push, asyh->curs.offset >> 8);
1863 } else
1864 if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1865 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1866 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1867 (asyh->curs.format << 24));
1868 evo_data(push, asyh->curs.offset >> 8);
1869 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1870 evo_data(push, asyh->curs.handle);
1871 } else {
1872 evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
1873 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1874 (asyh->curs.format << 24));
1875 evo_data(push, asyh->curs.offset >> 8);
1876 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1877 evo_data(push, asyh->curs.handle);
1878 }
1879 evo_kick(push, core);
1880 }
1881}
1882
1883static void
Ben Skeggsad633612016-11-04 17:20:36 +10001884nv50_head_core_clr(struct nv50_head *head)
1885{
1886 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1887 u32 *push;
1888 if ((push = evo_wait(core, 2))) {
1889 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1890 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
1891 else
1892 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
1893 evo_data(push, 0x00000000);
1894 evo_kick(push, core);
1895 }
1896}
1897
1898static void
1899nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1900{
1901 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1902 u32 *push;
1903 if ((push = evo_wait(core, 9))) {
1904 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1905 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1906 evo_data(push, asyh->core.offset >> 8);
1907 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1908 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1909 evo_data(push, asyh->core.layout << 20 |
1910 (asyh->core.pitch >> 8) << 8 |
1911 asyh->core.block);
1912 evo_data(push, asyh->core.kind << 16 |
1913 asyh->core.format << 8);
1914 evo_data(push, asyh->core.handle);
1915 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1916 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1917 } else
1918 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1919 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1920 evo_data(push, asyh->core.offset >> 8);
1921 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1922 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1923 evo_data(push, asyh->core.layout << 20 |
1924 (asyh->core.pitch >> 8) << 8 |
1925 asyh->core.block);
1926 evo_data(push, asyh->core.format << 8);
1927 evo_data(push, asyh->core.handle);
1928 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1929 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1930 } else {
1931 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
1932 evo_data(push, asyh->core.offset >> 8);
1933 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
1934 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1935 evo_data(push, asyh->core.layout << 24 |
1936 (asyh->core.pitch >> 8) << 8 |
1937 asyh->core.block);
1938 evo_data(push, asyh->core.format << 8);
1939 evo_data(push, asyh->core.handle);
1940 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
1941 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1942 }
1943 evo_kick(push, core);
1944 }
1945}
1946
1947static void
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001948nv50_head_lut_clr(struct nv50_head *head)
1949{
1950 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1951 u32 *push;
1952 if ((push = evo_wait(core, 4))) {
1953 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1954 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1955 evo_data(push, 0x40000000);
1956 } else
1957 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1958 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1959 evo_data(push, 0x40000000);
1960 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1961 evo_data(push, 0x00000000);
1962 } else {
1963 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
1964 evo_data(push, 0x03000000);
1965 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1966 evo_data(push, 0x00000000);
1967 }
1968 evo_kick(push, core);
1969 }
1970}
1971
1972static void
1973nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1974{
1975 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1976 u32 *push;
1977 if ((push = evo_wait(core, 7))) {
1978 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1979 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1980 evo_data(push, 0xc0000000);
1981 evo_data(push, asyh->lut.offset >> 8);
1982 } else
1983 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1984 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1985 evo_data(push, 0xc0000000);
1986 evo_data(push, asyh->lut.offset >> 8);
1987 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1988 evo_data(push, asyh->lut.handle);
1989 } else {
1990 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
1991 evo_data(push, 0x83000000);
1992 evo_data(push, asyh->lut.offset >> 8);
1993 evo_data(push, 0x00000000);
1994 evo_data(push, 0x00000000);
1995 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1996 evo_data(push, asyh->lut.handle);
1997 }
1998 evo_kick(push, core);
1999 }
2000}
2001
2002static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002003nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
2004{
2005 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
2006 struct nv50_head_mode *m = &asyh->mode;
2007 u32 *push;
2008 if ((push = evo_wait(core, 14))) {
2009 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
2010 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
2011 evo_data(push, 0x00800000 | m->clock);
2012 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
Ben Skeggs06ab2822016-11-04 17:20:36 +10002013 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 7);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002014 evo_data(push, 0x00000000);
2015 evo_data(push, (m->v.active << 16) | m->h.active );
2016 evo_data(push, (m->v.synce << 16) | m->h.synce );
2017 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
2018 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
2019 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
Ben Skeggs06ab2822016-11-04 17:20:36 +10002020 evo_data(push, asyh->mode.v.blankus);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002021 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
2022 evo_data(push, 0x00000000);
2023 } else {
2024 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
2025 evo_data(push, 0x00000000);
2026 evo_data(push, (m->v.active << 16) | m->h.active );
2027 evo_data(push, (m->v.synce << 16) | m->h.synce );
2028 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
2029 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
2030 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
2031 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
2032 evo_data(push, 0x00000000); /* ??? */
2033 evo_data(push, 0xffffff00);
2034 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
2035 evo_data(push, m->clock * 1000);
2036 evo_data(push, 0x00200000); /* ??? */
2037 evo_data(push, m->clock * 1000);
2038 }
2039 evo_kick(push, core);
2040 }
2041}
2042
2043static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10002044nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
2045{
2046 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
2047 u32 *push;
2048 if ((push = evo_wait(core, 10))) {
2049 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
2050 evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
2051 evo_data(push, 0x00000000);
2052 evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
2053 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
2054 evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
2055 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
2056 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
2057 } else {
2058 evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
2059 evo_data(push, 0x00000000);
2060 evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
2061 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
2062 evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
2063 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
2064 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
2065 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
2066 }
2067 evo_kick(push, core);
2068 }
2069}
2070
2071static void
Ben Skeggsad633612016-11-04 17:20:36 +10002072nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
2073{
2074 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002075 nv50_head_lut_clr(head);
2076 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsad633612016-11-04 17:20:36 +10002077 nv50_head_core_clr(head);
Ben Skeggsea8ee392016-11-04 17:20:36 +10002078 if (asyh->clr.curs && (!asyh->set.curs || y))
2079 nv50_head_curs_clr(head);
Ben Skeggsad633612016-11-04 17:20:36 +10002080}
2081
2082static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002083nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
2084{
Ben Skeggsc4e68122016-11-04 17:20:36 +10002085 if (asyh->set.view ) nv50_head_view (head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002086 if (asyh->set.mode ) nv50_head_mode (head, asyh);
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002087 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10002088 if (asyh->set.core ) nv50_head_core_set(head, asyh);
Ben Skeggsea8ee392016-11-04 17:20:36 +10002089 if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002090 if (asyh->set.base ) nv50_head_base (head, asyh);
2091 if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
Ben Skeggs7e918332016-11-04 17:20:36 +10002092 if (asyh->set.dither ) nv50_head_dither (head, asyh);
Ben Skeggs7e08d672016-11-04 17:20:36 +10002093 if (asyh->set.procamp) nv50_head_procamp (head, asyh);
2094}
2095
2096static void
2097nv50_head_atomic_check_procamp(struct nv50_head_atom *armh,
2098 struct nv50_head_atom *asyh,
2099 struct nouveau_conn_atom *asyc)
2100{
2101 const int vib = asyc->procamp.color_vibrance - 100;
2102 const int hue = asyc->procamp.vibrant_hue - 90;
2103 const int adj = (vib > 0) ? 50 : 0;
2104 asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff;
2105 asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff;
2106 asyh->set.procamp = true;
Ben Skeggs7e918332016-11-04 17:20:36 +10002107}
2108
2109static void
2110nv50_head_atomic_check_dither(struct nv50_head_atom *armh,
2111 struct nv50_head_atom *asyh,
2112 struct nouveau_conn_atom *asyc)
2113{
2114 struct drm_connector *connector = asyc->state.connector;
2115 u32 mode = 0x00;
2116
2117 if (asyc->dither.mode == DITHERING_MODE_AUTO) {
2118 if (asyh->base.depth > connector->display_info.bpc * 3)
2119 mode = DITHERING_MODE_DYNAMIC2X2;
2120 } else {
2121 mode = asyc->dither.mode;
2122 }
2123
2124 if (asyc->dither.depth == DITHERING_DEPTH_AUTO) {
2125 if (connector->display_info.bpc >= 8)
2126 mode |= DITHERING_DEPTH_8BPC;
2127 } else {
2128 mode |= asyc->dither.depth;
2129 }
2130
2131 asyh->dither.enable = mode;
2132 asyh->dither.bits = mode >> 1;
2133 asyh->dither.mode = mode >> 3;
2134 asyh->set.dither = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002135}
2136
2137static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10002138nv50_head_atomic_check_view(struct nv50_head_atom *armh,
2139 struct nv50_head_atom *asyh,
2140 struct nouveau_conn_atom *asyc)
2141{
2142 struct drm_connector *connector = asyc->state.connector;
2143 struct drm_display_mode *omode = &asyh->state.adjusted_mode;
2144 struct drm_display_mode *umode = &asyh->state.mode;
2145 int mode = asyc->scaler.mode;
2146 struct edid *edid;
2147
2148 if (connector->edid_blob_ptr)
2149 edid = (struct edid *)connector->edid_blob_ptr->data;
2150 else
2151 edid = NULL;
2152
2153 if (!asyc->scaler.full) {
2154 if (mode == DRM_MODE_SCALE_NONE)
2155 omode = umode;
2156 } else {
2157 /* Non-EDID LVDS/eDP mode. */
2158 mode = DRM_MODE_SCALE_FULLSCREEN;
2159 }
2160
2161 asyh->view.iW = umode->hdisplay;
2162 asyh->view.iH = umode->vdisplay;
2163 asyh->view.oW = omode->hdisplay;
2164 asyh->view.oH = omode->vdisplay;
2165 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
2166 asyh->view.oH *= 2;
2167
2168 /* Add overscan compensation if necessary, will keep the aspect
2169 * ratio the same as the backend mode unless overridden by the
2170 * user setting both hborder and vborder properties.
2171 */
2172 if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
2173 (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
2174 drm_detect_hdmi_monitor(edid)))) {
2175 u32 bX = asyc->scaler.underscan.hborder;
2176 u32 bY = asyc->scaler.underscan.vborder;
2177 u32 r = (asyh->view.oH << 19) / asyh->view.oW;
2178
2179 if (bX) {
2180 asyh->view.oW -= (bX * 2);
2181 if (bY) asyh->view.oH -= (bY * 2);
2182 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2183 } else {
2184 asyh->view.oW -= (asyh->view.oW >> 4) + 32;
2185 if (bY) asyh->view.oH -= (bY * 2);
2186 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2187 }
2188 }
2189
2190 /* Handle CENTER/ASPECT scaling, taking into account the areas
2191 * removed already for overscan compensation.
2192 */
2193 switch (mode) {
2194 case DRM_MODE_SCALE_CENTER:
2195 asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
2196 asyh->view.oH = min((u16)umode->vdisplay, asyh->view.oH);
2197 /* fall-through */
2198 case DRM_MODE_SCALE_ASPECT:
2199 if (asyh->view.oH < asyh->view.oW) {
2200 u32 r = (asyh->view.iW << 19) / asyh->view.iH;
2201 asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
2202 } else {
2203 u32 r = (asyh->view.iH << 19) / asyh->view.iW;
2204 asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2205 }
2206 break;
2207 default:
2208 break;
2209 }
2210
2211 asyh->set.view = true;
2212}
2213
2214static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002215nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
2216{
2217 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
2218 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
2219 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
2220 u32 hbackp = mode->htotal - mode->hsync_end;
2221 u32 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
2222 u32 hfrontp = mode->hsync_start - mode->hdisplay;
2223 u32 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
2224 struct nv50_head_mode *m = &asyh->mode;
2225
2226 m->h.active = mode->htotal;
2227 m->h.synce = mode->hsync_end - mode->hsync_start - 1;
2228 m->h.blanke = m->h.synce + hbackp;
2229 m->h.blanks = mode->htotal - hfrontp - 1;
2230
2231 m->v.active = mode->vtotal * vscan / ilace;
2232 m->v.synce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
2233 m->v.blanke = m->v.synce + vbackp;
2234 m->v.blanks = m->v.active - vfrontp - 1;
2235
2236 /*XXX: Safe underestimate, even "0" works */
2237 m->v.blankus = (m->v.active - mode->vdisplay - 2) * m->h.active;
2238 m->v.blankus *= 1000;
2239 m->v.blankus /= mode->clock;
2240
2241 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
2242 m->v.blank2e = m->v.active + m->v.synce + vbackp;
2243 m->v.blank2s = m->v.blank2e + (mode->vdisplay * vscan / ilace);
2244 m->v.active = (m->v.active * 2) + 1;
2245 m->interlace = true;
2246 } else {
2247 m->v.blank2e = 0;
2248 m->v.blank2s = 1;
2249 m->interlace = false;
2250 }
2251 m->clock = mode->clock;
2252
2253 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2254 asyh->set.mode = true;
2255}
2256
2257static int
2258nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
2259{
2260 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggsad633612016-11-04 17:20:36 +10002261 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002262 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002263 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002264 struct nv50_head_atom *asyh = nv50_head_atom(state);
Ben Skeggs839ca902016-11-04 17:20:36 +10002265 struct nouveau_conn_atom *asyc = NULL;
2266 struct drm_connector_state *conns;
2267 struct drm_connector *conn;
2268 int i;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002269
2270 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002271 if (asyh->state.active) {
Ben Skeggs839ca902016-11-04 17:20:36 +10002272 for_each_connector_in_state(asyh->state.state, conn, conns, i) {
2273 if (conns->crtc == crtc) {
2274 asyc = nouveau_conn_atom(conns);
2275 break;
2276 }
2277 }
2278
2279 if (armh->state.active) {
2280 if (asyc) {
2281 if (asyh->state.mode_changed)
2282 asyc->set.scaler = true;
2283 if (armh->base.depth != asyh->base.depth)
2284 asyc->set.dither = true;
2285 }
2286 } else {
2287 asyc->set.mask = ~0;
2288 asyh->set.mask = ~0;
2289 }
2290
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002291 if (asyh->state.mode_changed)
2292 nv50_head_atomic_check_mode(head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10002293
Ben Skeggs839ca902016-11-04 17:20:36 +10002294 if (asyc) {
2295 if (asyc->set.scaler)
2296 nv50_head_atomic_check_view(armh, asyh, asyc);
2297 if (asyc->set.dither)
2298 nv50_head_atomic_check_dither(armh, asyh, asyc);
2299 if (asyc->set.procamp)
2300 nv50_head_atomic_check_procamp(armh, asyh, asyc);
2301 }
2302
Ben Skeggsad633612016-11-04 17:20:36 +10002303 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
2304 asyh->core.x = asyh->base.x;
2305 asyh->core.y = asyh->base.y;
2306 asyh->core.w = asyh->base.w;
2307 asyh->core.h = asyh->base.h;
2308 } else
Ben Skeggsea8ee392016-11-04 17:20:36 +10002309 if ((asyh->core.visible = asyh->curs.visible)) {
Ben Skeggsad633612016-11-04 17:20:36 +10002310 /*XXX: We need to either find some way of having the
2311 * primary base layer appear black, while still
2312 * being able to display the other layers, or we
2313 * need to allocate a dummy black surface here.
2314 */
2315 asyh->core.x = 0;
2316 asyh->core.y = 0;
2317 asyh->core.w = asyh->state.mode.hdisplay;
2318 asyh->core.h = asyh->state.mode.vdisplay;
2319 }
2320 asyh->core.handle = disp->mast.base.vram.handle;
2321 asyh->core.offset = 0;
2322 asyh->core.format = 0xcf;
2323 asyh->core.kind = 0;
2324 asyh->core.layout = 1;
2325 asyh->core.block = 0;
2326 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002327 asyh->lut.handle = disp->mast.base.vram.handle;
2328 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002329 asyh->set.base = armh->base.cpp != asyh->base.cpp;
2330 asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
Ben Skeggsad633612016-11-04 17:20:36 +10002331 } else {
2332 asyh->core.visible = false;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002333 asyh->curs.visible = false;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002334 asyh->base.cpp = 0;
2335 asyh->ovly.cpp = 0;
Ben Skeggsad633612016-11-04 17:20:36 +10002336 }
2337
2338 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
2339 if (asyh->core.visible) {
2340 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
2341 asyh->set.core = true;
2342 } else
2343 if (armh->core.visible) {
2344 asyh->clr.core = true;
2345 }
Ben Skeggsea8ee392016-11-04 17:20:36 +10002346
2347 if (asyh->curs.visible) {
2348 if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
2349 asyh->set.curs = true;
2350 } else
2351 if (armh->curs.visible) {
2352 asyh->clr.curs = true;
2353 }
Ben Skeggsad633612016-11-04 17:20:36 +10002354 } else {
2355 asyh->clr.core = armh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002356 asyh->clr.curs = armh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10002357 asyh->set.core = asyh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002358 asyh->set.curs = asyh->curs.visible;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002359 }
2360
Ben Skeggs839ca902016-11-04 17:20:36 +10002361 if (asyh->clr.mask || asyh->set.mask)
2362 nv50_atom(asyh->state.state)->lock_core = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002363 return 0;
2364}
2365
2366/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +10002367 * CRTC
2368 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +10002369
Ben Skeggs438d99e2011-07-05 16:48:06 +10002370static int
Ben Skeggse225f442012-11-21 14:40:21 +10002371nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002372 struct drm_framebuffer *fb, int x, int y,
2373 enum mode_set_atomic state)
2374{
Ben Skeggs5f674a52016-11-04 17:20:36 +10002375 WARN_ON(1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002376 return 0;
2377}
2378
2379static void
Ben Skeggse225f442012-11-21 14:40:21 +10002380nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002381{
Ben Skeggse225f442012-11-21 14:40:21 +10002382 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002383 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2384 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
2385 int i;
2386
2387 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002388 u16 r = nv_crtc->lut.r[i] >> 2;
2389 u16 g = nv_crtc->lut.g[i] >> 2;
2390 u16 b = nv_crtc->lut.b[i] >> 2;
2391
Ben Skeggs648d4df2014-08-10 04:10:27 +10002392 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002393 writew(r + 0x0000, lut + (i * 0x08) + 0);
2394 writew(g + 0x0000, lut + (i * 0x08) + 2);
2395 writew(b + 0x0000, lut + (i * 0x08) + 4);
2396 } else {
2397 writew(r + 0x6000, lut + (i * 0x20) + 0);
2398 writew(g + 0x6000, lut + (i * 0x20) + 2);
2399 writew(b + 0x6000, lut + (i * 0x20) + 4);
2400 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002401 }
2402}
2403
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002404static int
Ben Skeggse225f442012-11-21 14:40:21 +10002405nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002406 uint32_t size)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002407{
2408 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002409 u32 i;
2410
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002411 for (i = 0; i < size; i++) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10002412 nv_crtc->lut.r[i] = r[i];
2413 nv_crtc->lut.g[i] = g[i];
2414 nv_crtc->lut.b[i] = b[i];
2415 }
2416
Ben Skeggse225f442012-11-21 14:40:21 +10002417 nv50_crtc_lut_load(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002418
2419 return 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002420}
2421
2422static void
Ben Skeggse225f442012-11-21 14:40:21 +10002423nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002424{
2425 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002426 struct nv50_disp *disp = nv50_disp(crtc->dev);
2427 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10002428
2429 nv50_dmac_destroy(&head->ovly.base, disp->disp);
2430 nv50_pioc_destroy(&head->oimm.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002431
Ben Skeggs438d99e2011-07-05 16:48:06 +10002432 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002433 if (nv_crtc->lut.nvbo)
2434 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002435 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002436
Ben Skeggs438d99e2011-07-05 16:48:06 +10002437 drm_crtc_cleanup(crtc);
2438 kfree(crtc);
2439}
2440
Ben Skeggse225f442012-11-21 14:40:21 +10002441static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
Ben Skeggse225f442012-11-21 14:40:21 +10002442 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
2443 .load_lut = nv50_crtc_lut_load,
Ben Skeggs839ca902016-11-04 17:20:36 +10002444 .atomic_check = nv50_head_atomic_check,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002445};
2446
Ben Skeggs839ca902016-11-04 17:20:36 +10002447/* This is identical to the version in the atomic helpers, except that
2448 * it supports non-vblanked ("async") page flips.
2449 */
2450static int
2451nv50_head_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2452 struct drm_pending_vblank_event *event, u32 flags)
2453{
2454 struct drm_plane *plane = crtc->primary;
2455 struct drm_atomic_state *state;
2456 struct drm_plane_state *plane_state;
2457 struct drm_crtc_state *crtc_state;
2458 int ret = 0;
2459
2460 state = drm_atomic_state_alloc(plane->dev);
2461 if (!state)
2462 return -ENOMEM;
2463
2464 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2465retry:
2466 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2467 if (IS_ERR(crtc_state)) {
2468 ret = PTR_ERR(crtc_state);
2469 goto fail;
2470 }
2471 crtc_state->event = event;
2472
2473 plane_state = drm_atomic_get_plane_state(state, plane);
2474 if (IS_ERR(plane_state)) {
2475 ret = PTR_ERR(plane_state);
2476 goto fail;
2477 }
2478
2479 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2480 if (ret != 0)
2481 goto fail;
2482 drm_atomic_set_fb_for_plane(plane_state, fb);
2483
2484 /* Make sure we don't accidentally do a full modeset. */
2485 state->allow_modeset = false;
2486 if (!crtc_state->active) {
2487 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2488 crtc->base.id);
2489 ret = -EINVAL;
2490 goto fail;
2491 }
2492
2493 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2494 nv50_wndw_atom(plane_state)->interval = 0;
2495
2496 ret = drm_atomic_nonblocking_commit(state);
2497fail:
2498 if (ret == -EDEADLK)
2499 goto backoff;
2500
2501 drm_atomic_state_put(state);
2502 return ret;
2503
2504backoff:
2505 drm_atomic_state_clear(state);
2506 drm_atomic_legacy_backoff(state);
2507
2508 /*
2509 * Someone might have exchanged the framebuffer while we dropped locks
2510 * in the backoff code. We need to fix up the fb refcount tracking the
2511 * core does for us.
2512 */
2513 plane->old_fb = plane->fb;
2514
2515 goto retry;
2516}
2517
2518static void
2519nv50_head_atomic_destroy_state(struct drm_crtc *crtc,
2520 struct drm_crtc_state *state)
2521{
2522 struct nv50_head_atom *asyh = nv50_head_atom(state);
2523 __drm_atomic_helper_crtc_destroy_state(&asyh->state);
2524 kfree(asyh);
2525}
2526
2527static struct drm_crtc_state *
2528nv50_head_atomic_duplicate_state(struct drm_crtc *crtc)
2529{
2530 struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
2531 struct nv50_head_atom *asyh;
2532 if (!(asyh = kmalloc(sizeof(*asyh), GFP_KERNEL)))
2533 return NULL;
2534 __drm_atomic_helper_crtc_duplicate_state(crtc, &asyh->state);
2535 asyh->view = armh->view;
2536 asyh->mode = armh->mode;
2537 asyh->lut = armh->lut;
2538 asyh->core = armh->core;
2539 asyh->curs = armh->curs;
2540 asyh->base = armh->base;
2541 asyh->ovly = armh->ovly;
2542 asyh->dither = armh->dither;
2543 asyh->procamp = armh->procamp;
2544 asyh->clr.mask = 0;
2545 asyh->set.mask = 0;
2546 return &asyh->state;
2547}
2548
2549static void
2550__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
2551 struct drm_crtc_state *state)
2552{
2553 if (crtc->state)
2554 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
2555 crtc->state = state;
2556 crtc->state->crtc = crtc;
2557}
2558
2559static void
2560nv50_head_reset(struct drm_crtc *crtc)
2561{
2562 struct nv50_head_atom *asyh;
2563
2564 if (WARN_ON(!(asyh = kzalloc(sizeof(*asyh), GFP_KERNEL))))
2565 return;
2566
2567 __drm_atomic_helper_crtc_reset(crtc, &asyh->state);
2568}
2569
Ben Skeggse225f442012-11-21 14:40:21 +10002570static const struct drm_crtc_funcs nv50_crtc_func = {
Ben Skeggs839ca902016-11-04 17:20:36 +10002571 .reset = nv50_head_reset,
Ben Skeggse225f442012-11-21 14:40:21 +10002572 .gamma_set = nv50_crtc_gamma_set,
Ben Skeggse225f442012-11-21 14:40:21 +10002573 .destroy = nv50_crtc_destroy,
Ben Skeggs839ca902016-11-04 17:20:36 +10002574 .set_config = drm_atomic_helper_set_config,
2575 .page_flip = nv50_head_page_flip,
2576 .set_property = drm_atomic_helper_crtc_set_property,
2577 .atomic_duplicate_state = nv50_head_atomic_duplicate_state,
2578 .atomic_destroy_state = nv50_head_atomic_destroy_state,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002579};
2580
2581static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10002582nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002583{
Ben Skeggsa01ca782015-08-20 14:54:15 +10002584 struct nouveau_drm *drm = nouveau_drm(dev);
2585 struct nvif_device *device = &drm->device;
Ben Skeggse225f442012-11-21 14:40:21 +10002586 struct nv50_disp *disp = nv50_disp(dev);
2587 struct nv50_head *head;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002588 struct nv50_base *base;
Ben Skeggs22e927d2016-11-04 17:20:36 +10002589 struct nv50_curs *curs;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002590 struct drm_crtc *crtc;
2591 int ret, i;
2592
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002593 head = kzalloc(sizeof(*head), GFP_KERNEL);
2594 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002595 return -ENOMEM;
2596
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002597 head->base.index = index;
Ben Skeggsf9887d02012-11-21 13:03:42 +10002598 head->base.color_vibrance = 50;
2599 head->base.vibrant_hue = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002600 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002601 head->base.lut.r[i] = i << 8;
2602 head->base.lut.g[i] = i << 8;
2603 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002604 }
2605
Ben Skeggs973f10c2016-11-04 17:20:36 +10002606 ret = nv50_base_new(drm, head, &base);
Ben Skeggs22e927d2016-11-04 17:20:36 +10002607 if (ret == 0)
2608 ret = nv50_curs_new(drm, head, &curs);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002609 if (ret) {
2610 kfree(head);
2611 return ret;
2612 }
2613
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002614 crtc = &head->base.base;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002615 head->_base = base;
2616
Ben Skeggs839ca902016-11-04 17:20:36 +10002617 drm_crtc_init_with_planes(dev, crtc, &base->wndw.plane,
2618 &curs->wndw.plane, &nv50_crtc_func,
2619 "head-%d", head->base.index);
Ben Skeggse225f442012-11-21 14:40:21 +10002620 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002621 drm_mode_crtc_set_gamma_size(crtc, 256);
2622
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10002623 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002624 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002625 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002626 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002627 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002628 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002629 if (ret)
2630 nouveau_bo_unpin(head->base.lut.nvbo);
2631 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002632 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002633 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002634 }
2635
2636 if (ret)
2637 goto out;
2638
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002639 /* allocate overlay resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10002640 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002641 if (ret)
2642 goto out;
2643
Ben Skeggsa01ca782015-08-20 14:54:15 +10002644 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
2645 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002646 if (ret)
2647 goto out;
2648
Ben Skeggs438d99e2011-07-05 16:48:06 +10002649out:
2650 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002651 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002652 return ret;
2653}
2654
2655/******************************************************************************
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002656 * Output path helpers
Ben Skeggsa91d3222014-12-22 16:30:13 +10002657 *****************************************************************************/
Ben Skeggsd92c8ad2016-11-04 17:20:36 +10002658static int
2659nv50_outp_atomic_check_view(struct drm_encoder *encoder,
2660 struct drm_crtc_state *crtc_state,
2661 struct drm_connector_state *conn_state,
2662 struct drm_display_mode *native_mode)
2663{
2664 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
2665 struct drm_display_mode *mode = &crtc_state->mode;
2666 struct drm_connector *connector = conn_state->connector;
2667 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
2668 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
2669
2670 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
2671 asyc->scaler.full = false;
2672 if (!native_mode)
2673 return 0;
2674
2675 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
2676 switch (connector->connector_type) {
2677 case DRM_MODE_CONNECTOR_LVDS:
2678 case DRM_MODE_CONNECTOR_eDP:
2679 /* Force use of scaler for non-EDID modes. */
2680 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
2681 break;
2682 mode = native_mode;
2683 asyc->scaler.full = true;
2684 break;
2685 default:
2686 break;
2687 }
2688 } else {
2689 mode = native_mode;
2690 }
2691
2692 if (!drm_mode_equal(adjusted_mode, mode)) {
2693 drm_mode_copy(adjusted_mode, mode);
2694 crtc_state->mode_changed = true;
2695 }
2696
2697 return 0;
2698}
2699
Ben Skeggs839ca902016-11-04 17:20:36 +10002700static int
2701nv50_outp_atomic_check(struct drm_encoder *encoder,
2702 struct drm_crtc_state *crtc_state,
2703 struct drm_connector_state *conn_state)
Ben Skeggsa91d3222014-12-22 16:30:13 +10002704{
Ben Skeggs839ca902016-11-04 17:20:36 +10002705 struct nouveau_connector *nv_connector =
2706 nouveau_connector(conn_state->connector);
2707 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
2708 nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10002709}
2710
2711/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002712 * DAC
2713 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002714static void
Ben Skeggse225f442012-11-21 14:40:21 +10002715nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002716{
2717 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002718 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10002719 struct {
2720 struct nv50_disp_mthd_v1 base;
2721 struct nv50_disp_dac_pwr_v0 pwr;
2722 } args = {
2723 .base.version = 1,
2724 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
2725 .base.hasht = nv_encoder->dcb->hasht,
2726 .base.hashm = nv_encoder->dcb->hashm,
2727 .pwr.state = 1,
2728 .pwr.data = 1,
2729 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
2730 mode != DRM_MODE_DPMS_OFF),
2731 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
2732 mode != DRM_MODE_DPMS_OFF),
2733 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002734
Ben Skeggsbf0eb892014-08-10 04:10:26 +10002735 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002736}
2737
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002738static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002739nv50_dac_disable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002740{
Ben Skeggsf20c6652016-11-04 17:20:36 +10002741 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2742 struct nv50_mast *mast = nv50_mast(encoder->dev);
2743 const int or = nv_encoder->or;
2744 u32 *push;
2745
2746 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10002747 push = evo_wait(mast, 4);
2748 if (push) {
2749 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2750 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2751 evo_data(push, 0x00000000);
2752 } else {
2753 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2754 evo_data(push, 0x00000000);
2755 }
2756 evo_kick(push, mast);
2757 }
2758 }
2759
2760 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002761}
2762
2763static void
Ben Skeggs839ca902016-11-04 17:20:36 +10002764nv50_dac_enable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002765{
Ben Skeggse225f442012-11-21 14:40:21 +10002766 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002767 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2768 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10002769 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggs97b19b52012-11-16 11:21:37 +10002770 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002771
Ben Skeggs97b19b52012-11-16 11:21:37 +10002772 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002773 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002774 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002775 u32 syncs = 0x00000000;
2776
2777 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2778 syncs |= 0x00000001;
2779 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2780 syncs |= 0x00000002;
2781
2782 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
2783 evo_data(push, 1 << nv_crtc->index);
2784 evo_data(push, syncs);
2785 } else {
2786 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2787 u32 syncs = 0x00000001;
2788
2789 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2790 syncs |= 0x00000008;
2791 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2792 syncs |= 0x00000010;
2793
2794 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2795 magic |= 0x00000001;
2796
2797 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2798 evo_data(push, syncs);
2799 evo_data(push, magic);
2800 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2801 evo_data(push, 1 << nv_crtc->index);
2802 }
2803
2804 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002805 }
2806
2807 nv_encoder->crtc = encoder->crtc;
2808}
2809
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002810static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10002811nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002812{
Ben Skeggsc4abd312014-08-10 04:10:26 +10002813 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002814 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10002815 struct {
2816 struct nv50_disp_mthd_v1 base;
2817 struct nv50_disp_dac_load_v0 load;
2818 } args = {
2819 .base.version = 1,
2820 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2821 .base.hasht = nv_encoder->dcb->hasht,
2822 .base.hashm = nv_encoder->dcb->hashm,
2823 };
2824 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10002825
Ben Skeggsc4abd312014-08-10 04:10:26 +10002826 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2827 if (args.load.data == 0)
2828 args.load.data = 340;
2829
2830 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2831 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10002832 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10002833
Ben Skeggs35b21d32012-11-08 12:08:55 +10002834 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002835}
2836
Ben Skeggsf20c6652016-11-04 17:20:36 +10002837static const struct drm_encoder_helper_funcs
2838nv50_dac_help = {
2839 .dpms = nv50_dac_dpms,
Ben Skeggs839ca902016-11-04 17:20:36 +10002840 .atomic_check = nv50_outp_atomic_check,
2841 .enable = nv50_dac_enable,
2842 .disable = nv50_dac_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002843 .detect = nv50_dac_detect
2844};
2845
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002846static void
Ben Skeggse225f442012-11-21 14:40:21 +10002847nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002848{
2849 drm_encoder_cleanup(encoder);
2850 kfree(encoder);
2851}
2852
Ben Skeggsf20c6652016-11-04 17:20:36 +10002853static const struct drm_encoder_funcs
2854nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10002855 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002856};
2857
2858static int
Ben Skeggse225f442012-11-21 14:40:21 +10002859nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002860{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002861 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002862 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002863 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002864 struct nouveau_encoder *nv_encoder;
2865 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002866 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002867
2868 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2869 if (!nv_encoder)
2870 return -ENOMEM;
2871 nv_encoder->dcb = dcbe;
2872 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002873
2874 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2875 if (bus)
2876 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002877
2878 encoder = to_drm_encoder(nv_encoder);
2879 encoder->possible_crtcs = dcbe->heads;
2880 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002881 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2882 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10002883 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002884
2885 drm_mode_connector_attach_encoder(connector, encoder);
2886 return 0;
2887}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002888
2889/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10002890 * Audio
2891 *****************************************************************************/
2892static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002893nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
2894{
2895 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2896 struct nv50_disp *disp = nv50_disp(encoder->dev);
2897 struct {
2898 struct nv50_disp_mthd_v1 base;
2899 struct nv50_disp_sor_hda_eld_v0 eld;
2900 } args = {
2901 .base.version = 1,
2902 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2903 .base.hasht = nv_encoder->dcb->hasht,
2904 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2905 (0x0100 << nv_crtc->index),
2906 };
2907
2908 nvif_mthd(disp->disp, 0, &args, sizeof(args));
2909}
2910
2911static void
2912nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002913{
2914 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10002915 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10002916 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10002917 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10002918 struct __packed {
2919 struct {
2920 struct nv50_disp_mthd_v1 mthd;
2921 struct nv50_disp_sor_hda_eld_v0 eld;
2922 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002923 u8 data[sizeof(nv_connector->base.eld)];
2924 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10002925 .base.mthd.version = 1,
2926 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2927 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002928 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2929 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002930 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002931
2932 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2933 if (!drm_detect_monitor_audio(nv_connector->edid))
2934 return;
2935
Ben Skeggs78951d22011-11-11 18:13:13 +10002936 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002937 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002938
Jani Nikula938fd8a2014-10-28 16:20:48 +02002939 nvif_mthd(disp->disp, 0, &args,
2940 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002941}
2942
Ben Skeggsf20c6652016-11-04 17:20:36 +10002943/******************************************************************************
2944 * HDMI
2945 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +10002946static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002947nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002948{
2949 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002950 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002951 struct {
2952 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +10002953 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002954 } args = {
2955 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +10002956 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2957 .base.hasht = nv_encoder->dcb->hasht,
2958 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2959 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002960 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002961
Ben Skeggs120b0c32014-08-10 04:10:26 +10002962 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002963}
2964
Ben Skeggs78951d22011-11-11 18:13:13 +10002965static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002966nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002967{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002968 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2969 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002970 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002971 struct {
2972 struct nv50_disp_mthd_v1 base;
2973 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
2974 } args = {
2975 .base.version = 1,
2976 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2977 .base.hasht = nv_encoder->dcb->hasht,
2978 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2979 (0x0100 << nv_crtc->index),
2980 .pwr.state = 1,
2981 .pwr.rekey = 56, /* binary driver, and tegra, constant */
2982 };
2983 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002984 u32 max_ac_packet;
2985
2986 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2987 if (!drm_detect_hdmi_monitor(nv_connector->edid))
2988 return;
2989
2990 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10002991 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002992 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10002993 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002994
Ben Skeggse00f2232014-08-10 04:10:26 +10002995 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsf20c6652016-11-04 17:20:36 +10002996 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10002997}
2998
2999/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003000 * MST
3001 *****************************************************************************/
3002struct nv50_mstm {
3003 struct nouveau_encoder *outp;
3004
3005 struct drm_dp_mst_topology_mgr mgr;
3006};
3007
3008static int
3009nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
3010{
3011 struct nouveau_encoder *outp = mstm->outp;
3012 struct {
3013 struct nv50_disp_mthd_v1 base;
3014 struct nv50_disp_sor_dp_mst_link_v0 mst;
3015 } args = {
3016 .base.version = 1,
3017 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
3018 .base.hasht = outp->dcb->hasht,
3019 .base.hashm = outp->dcb->hashm,
3020 .mst.state = state,
3021 };
3022 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
3023 struct nvif_object *disp = &drm->display->disp;
3024 int ret;
3025
3026 if (dpcd >= 0x12) {
3027 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
3028 if (ret < 0)
3029 return ret;
3030
3031 dpcd &= ~DP_MST_EN;
3032 if (state)
3033 dpcd |= DP_MST_EN;
3034
3035 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
3036 if (ret < 0)
3037 return ret;
3038 }
3039
3040 return nvif_mthd(disp, 0, &args, sizeof(args));
3041}
3042
3043int
3044nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
3045{
3046 int ret, state = 0;
3047
3048 if (!mstm)
3049 return 0;
3050
3051 if (dpcd[0] >= 0x12 && allow) {
3052 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
3053 if (ret < 0)
3054 return ret;
3055
3056 state = dpcd[1] & DP_MST_CAP;
3057 }
3058
3059 ret = nv50_mstm_enable(mstm, dpcd[0], state);
3060 if (ret)
3061 return ret;
3062
3063 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
3064 if (ret)
3065 return nv50_mstm_enable(mstm, dpcd[0], 0);
3066
3067 return mstm->mgr.mst_state;
3068}
3069
3070static void
3071nv50_mstm_del(struct nv50_mstm **pmstm)
3072{
3073 struct nv50_mstm *mstm = *pmstm;
3074 if (mstm) {
3075 kfree(*pmstm);
3076 *pmstm = NULL;
3077 }
3078}
3079
3080static int
3081nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
3082 int conn_base_id, struct nv50_mstm **pmstm)
3083{
3084 const int max_payloads = hweight8(outp->dcb->heads);
3085 struct drm_device *dev = outp->base.base.dev;
3086 struct nv50_mstm *mstm;
3087 int ret;
3088
3089 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
3090 return -ENOMEM;
3091 mstm->outp = outp;
3092
3093 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev->dev, aux, aux_max,
3094 max_payloads, conn_base_id);
3095 if (ret)
3096 return ret;
3097
3098 return 0;
3099}
3100
3101/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003102 * SOR
3103 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003104static void
Ben Skeggse225f442012-11-21 14:40:21 +10003105nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003106{
3107 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003108 struct nv50_disp *disp = nv50_disp(encoder->dev);
3109 struct {
3110 struct nv50_disp_mthd_v1 base;
3111 struct nv50_disp_sor_pwr_v0 pwr;
3112 } args = {
3113 .base.version = 1,
3114 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
3115 .base.hasht = nv_encoder->dcb->hasht,
3116 .base.hashm = nv_encoder->dcb->hashm,
3117 .pwr.state = mode == DRM_MODE_DPMS_ON,
3118 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10003119 struct {
3120 struct nv50_disp_mthd_v1 base;
3121 struct nv50_disp_sor_dp_pwr_v0 pwr;
3122 } link = {
3123 .base.version = 1,
3124 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
3125 .base.hasht = nv_encoder->dcb->hasht,
3126 .base.hashm = nv_encoder->dcb->hashm,
3127 .pwr.state = mode == DRM_MODE_DPMS_ON,
3128 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10003129 struct drm_device *dev = encoder->dev;
3130 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003131
3132 nv_encoder->last_dpms = mode;
3133
3134 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
3135 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
3136
3137 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
3138 continue;
3139
3140 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10003141 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10003142 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
3143 return;
3144 break;
3145 }
3146 }
3147
Ben Skeggs48743222014-05-31 01:48:06 +10003148 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003149 args.pwr.state = 1;
3150 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10003151 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10003152 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003153 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10003154 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003155}
3156
Ben Skeggs83fc0832011-07-05 13:08:40 +10003157static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10003158nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
3159{
3160 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
3161 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
3162 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003163 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10003164 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
3165 evo_data(push, (nv_encoder->ctrl = temp));
3166 } else {
3167 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
3168 evo_data(push, (nv_encoder->ctrl = temp));
3169 }
3170 evo_kick(push, mast);
3171 }
3172}
3173
3174static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003175nv50_sor_disable(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003176{
3177 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003178 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003179
3180 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
3181 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10003182
3183 if (nv_crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +10003184 struct nvkm_i2c_aux *aux = nv_encoder->aux;
3185 u8 pwr;
3186
3187 if (aux) {
3188 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
3189 if (ret == 0) {
3190 pwr &= ~DP_SET_POWER_MASK;
3191 pwr |= DP_SET_POWER_D3;
3192 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
3193 }
3194 }
3195
Ben Skeggse84a35a2014-06-05 10:59:55 +10003196 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003197 nv50_audio_disable(encoder, nv_crtc);
3198 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003199 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003200}
3201
3202static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003203nv50_sor_enable(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003204{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003205 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3206 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10003207 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003208 struct {
3209 struct nv50_disp_mthd_v1 base;
3210 struct nv50_disp_sor_lvds_script_v0 lvds;
3211 } lvds = {
3212 .base.version = 1,
3213 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
3214 .base.hasht = nv_encoder->dcb->hasht,
3215 .base.hashm = nv_encoder->dcb->hashm,
3216 };
Ben Skeggse225f442012-11-21 14:40:21 +10003217 struct nv50_disp *disp = nv50_disp(encoder->dev);
3218 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10003219 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10003220 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003221 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10003222 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003223 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003224 u8 owner = 1 << nv_crtc->index;
3225 u8 proto = 0xf;
3226 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003227
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003228 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003229 nv_encoder->crtc = encoder->crtc;
3230
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003231 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10003232 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003233 if (nv_encoder->dcb->sorconf.link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05003234 proto = 0x1;
3235 /* Only enable dual-link if:
3236 * - Need to (i.e. rate > 165MHz)
3237 * - DCB says we can
3238 * - Not an HDMI monitor, since there's no dual-link
3239 * on HDMI.
3240 */
3241 if (mode->clock >= 165000 &&
3242 nv_encoder->dcb->duallink_possible &&
3243 !drm_detect_hdmi_monitor(nv_connector->edid))
3244 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003245 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003246 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003247 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003248
Ben Skeggsf20c6652016-11-04 17:20:36 +10003249 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003250 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003251 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003252 proto = 0x0;
3253
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003254 if (bios->fp_no_ddc) {
3255 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003256 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003257 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003258 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003259 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10003260 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003261 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003262 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003263 } else
3264 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003265 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003266 }
3267
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003268 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003269 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003270 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003271 } else {
3272 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003273 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003274 }
3275
3276 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003277 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003278 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10003279
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003280 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003281 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003282 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10003283 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003284 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003285 else
3286 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003287 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003288 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10003289 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003290
3291 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003292 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003293 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003294 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003295
3296 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003297 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003298 default:
3299 BUG_ON(1);
3300 break;
3301 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10003302
Ben Skeggs648d4df2014-08-10 04:10:27 +10003303 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10003304 u32 *push = evo_wait(mast, 3);
3305 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003306 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
3307 u32 syncs = 0x00000001;
3308
3309 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3310 syncs |= 0x00000008;
3311 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3312 syncs |= 0x00000010;
3313
3314 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3315 magic |= 0x00000001;
3316
3317 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
3318 evo_data(push, syncs | (depth << 6));
3319 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003320 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003321 }
3322
Ben Skeggse84a35a2014-06-05 10:59:55 +10003323 ctrl = proto << 8;
3324 mask = 0x00000f00;
3325 } else {
3326 ctrl = (depth << 16) | (proto << 8);
3327 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3328 ctrl |= 0x00001000;
3329 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3330 ctrl |= 0x00002000;
3331 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003332 }
3333
Ben Skeggse84a35a2014-06-05 10:59:55 +10003334 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003335}
3336
Ben Skeggsf20c6652016-11-04 17:20:36 +10003337static const struct drm_encoder_helper_funcs
3338nv50_sor_help = {
3339 .dpms = nv50_sor_dpms,
Ben Skeggs839ca902016-11-04 17:20:36 +10003340 .atomic_check = nv50_outp_atomic_check,
3341 .enable = nv50_sor_enable,
3342 .disable = nv50_sor_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003343};
3344
Ben Skeggs83fc0832011-07-05 13:08:40 +10003345static void
Ben Skeggse225f442012-11-21 14:40:21 +10003346nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003347{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003348 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3349 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003350 drm_encoder_cleanup(encoder);
3351 kfree(encoder);
3352}
3353
Ben Skeggsf20c6652016-11-04 17:20:36 +10003354static const struct drm_encoder_funcs
3355nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10003356 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10003357};
3358
3359static int
Ben Skeggse225f442012-11-21 14:40:21 +10003360nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003361{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003362 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10003363 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10003364 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003365 struct nouveau_encoder *nv_encoder;
3366 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003367 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10003368
3369 switch (dcbe->type) {
3370 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
3371 case DCB_OUTPUT_TMDS:
3372 case DCB_OUTPUT_DP:
3373 default:
3374 type = DRM_MODE_ENCODER_TMDS;
3375 break;
3376 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003377
3378 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3379 if (!nv_encoder)
3380 return -ENOMEM;
3381 nv_encoder->dcb = dcbe;
3382 nv_encoder->or = ffs(dcbe->or) - 1;
3383 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
3384
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003385 encoder = to_drm_encoder(nv_encoder);
3386 encoder->possible_crtcs = dcbe->heads;
3387 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003388 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
3389 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003390 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003391
3392 drm_mode_connector_attach_encoder(connector, encoder);
3393
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003394 if (dcbe->type == DCB_OUTPUT_DP) {
3395 struct nvkm_i2c_aux *aux =
3396 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
3397 if (aux) {
3398 nv_encoder->i2c = &aux->i2c;
3399 nv_encoder->aux = aux;
3400 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003401
3402 /*TODO: Use DP Info Table to check for support. */
3403 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
3404 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
3405 nv_connector->base.base.id,
3406 &nv_encoder->dp.mstm);
3407 if (ret)
3408 return ret;
3409 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003410 } else {
3411 struct nvkm_i2c_bus *bus =
3412 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
3413 if (bus)
3414 nv_encoder->i2c = &bus->i2c;
3415 }
3416
Ben Skeggs83fc0832011-07-05 13:08:40 +10003417 return 0;
3418}
Ben Skeggs26f6d882011-07-04 16:25:18 +10003419
3420/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10003421 * PIOR
3422 *****************************************************************************/
Ben Skeggseb6313a2013-02-11 09:52:58 +10003423static void
3424nv50_pior_dpms(struct drm_encoder *encoder, int mode)
3425{
3426 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3427 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10003428 struct {
3429 struct nv50_disp_mthd_v1 base;
3430 struct nv50_disp_pior_pwr_v0 pwr;
3431 } args = {
3432 .base.version = 1,
3433 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
3434 .base.hasht = nv_encoder->dcb->hasht,
3435 .base.hashm = nv_encoder->dcb->hashm,
3436 .pwr.state = mode == DRM_MODE_DPMS_ON,
3437 .pwr.type = nv_encoder->dcb->type,
3438 };
3439
3440 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10003441}
3442
Ben Skeggs839ca902016-11-04 17:20:36 +10003443static int
3444nv50_pior_atomic_check(struct drm_encoder *encoder,
3445 struct drm_crtc_state *crtc_state,
3446 struct drm_connector_state *conn_state)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003447{
Ben Skeggs839ca902016-11-04 17:20:36 +10003448 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
3449 if (ret)
3450 return ret;
3451 crtc_state->adjusted_mode.clock *= 2;
3452 return 0;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003453}
3454
3455static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003456nv50_pior_disable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003457{
Ben Skeggsf20c6652016-11-04 17:20:36 +10003458 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3459 struct nv50_mast *mast = nv50_mast(encoder->dev);
3460 const int or = nv_encoder->or;
3461 u32 *push;
3462
3463 if (nv_encoder->crtc) {
Ben Skeggsf20c6652016-11-04 17:20:36 +10003464 push = evo_wait(mast, 4);
3465 if (push) {
3466 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
3467 evo_mthd(push, 0x0700 + (or * 0x040), 1);
3468 evo_data(push, 0x00000000);
3469 }
3470 evo_kick(push, mast);
3471 }
3472 }
3473
3474 nv_encoder->crtc = NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003475}
3476
3477static void
Ben Skeggs839ca902016-11-04 17:20:36 +10003478nv50_pior_enable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003479{
3480 struct nv50_mast *mast = nv50_mast(encoder->dev);
3481 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3482 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3483 struct nouveau_connector *nv_connector;
Ben Skeggs839ca902016-11-04 17:20:36 +10003484 struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003485 u8 owner = 1 << nv_crtc->index;
3486 u8 proto, depth;
3487 u32 *push;
3488
3489 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3490 switch (nv_connector->base.display_info.bpc) {
3491 case 10: depth = 0x6; break;
3492 case 8: depth = 0x5; break;
3493 case 6: depth = 0x2; break;
3494 default: depth = 0x0; break;
3495 }
3496
3497 switch (nv_encoder->dcb->type) {
3498 case DCB_OUTPUT_TMDS:
3499 case DCB_OUTPUT_DP:
3500 proto = 0x0;
3501 break;
3502 default:
3503 BUG_ON(1);
3504 break;
3505 }
3506
Ben Skeggseb6313a2013-02-11 09:52:58 +10003507 push = evo_wait(mast, 8);
3508 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003509 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003510 u32 ctrl = (depth << 16) | (proto << 8) | owner;
3511 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3512 ctrl |= 0x00001000;
3513 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3514 ctrl |= 0x00002000;
3515 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
3516 evo_data(push, ctrl);
3517 }
3518
3519 evo_kick(push, mast);
3520 }
3521
3522 nv_encoder->crtc = encoder->crtc;
3523}
3524
Ben Skeggsf20c6652016-11-04 17:20:36 +10003525static const struct drm_encoder_helper_funcs
3526nv50_pior_help = {
3527 .dpms = nv50_pior_dpms,
Ben Skeggs839ca902016-11-04 17:20:36 +10003528 .atomic_check = nv50_pior_atomic_check,
3529 .enable = nv50_pior_enable,
3530 .disable = nv50_pior_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003531};
Ben Skeggseb6313a2013-02-11 09:52:58 +10003532
3533static void
3534nv50_pior_destroy(struct drm_encoder *encoder)
3535{
3536 drm_encoder_cleanup(encoder);
3537 kfree(encoder);
3538}
3539
Ben Skeggsf20c6652016-11-04 17:20:36 +10003540static const struct drm_encoder_funcs
3541nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003542 .destroy = nv50_pior_destroy,
3543};
3544
3545static int
3546nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
3547{
3548 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10003549 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003550 struct nvkm_i2c_bus *bus = NULL;
3551 struct nvkm_i2c_aux *aux = NULL;
3552 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003553 struct nouveau_encoder *nv_encoder;
3554 struct drm_encoder *encoder;
3555 int type;
3556
3557 switch (dcbe->type) {
3558 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003559 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
3560 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003561 type = DRM_MODE_ENCODER_TMDS;
3562 break;
3563 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003564 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
3565 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003566 type = DRM_MODE_ENCODER_TMDS;
3567 break;
3568 default:
3569 return -ENODEV;
3570 }
3571
3572 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3573 if (!nv_encoder)
3574 return -ENOMEM;
3575 nv_encoder->dcb = dcbe;
3576 nv_encoder->or = ffs(dcbe->or) - 1;
3577 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003578 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003579
3580 encoder = to_drm_encoder(nv_encoder);
3581 encoder->possible_crtcs = dcbe->heads;
3582 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003583 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
3584 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003585 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003586
3587 drm_mode_connector_attach_encoder(connector, encoder);
3588 return 0;
3589}
3590
3591/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10003592 * Framebuffer
3593 *****************************************************************************/
3594
Ben Skeggs8a423642014-08-10 04:10:19 +10003595static void
Ben Skeggsab0af552014-08-10 04:10:19 +10003596nv50_fb_dtor(struct drm_framebuffer *fb)
3597{
3598}
3599
3600static int
3601nv50_fb_ctor(struct drm_framebuffer *fb)
3602{
3603 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
3604 struct nouveau_drm *drm = nouveau_drm(fb->dev);
3605 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10003606 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10003607 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
3608 u8 tile = nvbo->tile_mode;
Ben Skeggsaccdea22016-11-04 17:20:36 +10003609 struct drm_crtc *crtc;
Ben Skeggsab0af552014-08-10 04:10:19 +10003610
Ben Skeggs967e7bd2014-08-10 04:10:22 +10003611 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10003612 tile >>= 4; /* yep.. */
3613
Ben Skeggsab0af552014-08-10 04:10:19 +10003614 switch (fb->depth) {
3615 case 8: nv_fb->r_format = 0x1e00; break;
3616 case 15: nv_fb->r_format = 0xe900; break;
3617 case 16: nv_fb->r_format = 0xe800; break;
3618 case 24:
3619 case 32: nv_fb->r_format = 0xcf00; break;
3620 case 30: nv_fb->r_format = 0xd100; break;
3621 default:
3622 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
3623 return -EINVAL;
3624 }
3625
Ben Skeggs648d4df2014-08-10 04:10:27 +10003626 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10003627 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3628 (fb->pitches[0] | 0x00100000);
3629 nv_fb->r_format |= kind << 16;
3630 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10003631 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10003632 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3633 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10003634 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10003635 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3636 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10003637 }
Ben Skeggs8a423642014-08-10 04:10:19 +10003638 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10003639
Ben Skeggsaccdea22016-11-04 17:20:36 +10003640 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
Ben Skeggs839ca902016-11-04 17:20:36 +10003641 struct nv50_wndw *wndw = nv50_wndw(crtc->primary);
Ben Skeggsaccdea22016-11-04 17:20:36 +10003642 struct nv50_dmac_ctxdma *ctxdma;
3643
Ben Skeggs839ca902016-11-04 17:20:36 +10003644 ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, nv_fb->r_handle, nv_fb);
Ben Skeggsaccdea22016-11-04 17:20:36 +10003645 if (IS_ERR(ctxdma))
3646 return PTR_ERR(ctxdma);
3647 }
3648
3649 return 0;
Ben Skeggsab0af552014-08-10 04:10:19 +10003650}
3651
3652/******************************************************************************
Ben Skeggs839ca902016-11-04 17:20:36 +10003653 * Atomic
3654 *****************************************************************************/
3655
3656static void
3657nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
3658{
3659 struct nv50_disp *disp = nv50_disp(drm->dev);
3660 struct nv50_dmac *core = &disp->mast.base;
3661 u32 *push;
3662
3663 NV_ATOMIC(drm, "commit core %08x\n", interlock);
3664
3665 if ((push = evo_wait(core, 5))) {
3666 evo_mthd(push, 0x0084, 1);
3667 evo_data(push, 0x80000000);
3668 evo_mthd(push, 0x0080, 2);
3669 evo_data(push, interlock);
3670 evo_data(push, 0x00000000);
3671 nouveau_bo_wr32(disp->sync, 0, 0x00000000);
3672 evo_kick(push, core);
3673 if (nvif_msec(&drm->device, 2000ULL,
3674 if (nouveau_bo_rd32(disp->sync, 0))
3675 break;
3676 usleep_range(1, 2);
3677 ) < 0)
3678 NV_ERROR(drm, "EVO timeout\n");
3679 }
3680}
3681
3682static void
3683nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
3684{
3685 struct drm_device *dev = state->dev;
3686 struct drm_crtc_state *crtc_state;
3687 struct drm_crtc *crtc;
3688 struct drm_plane_state *plane_state;
3689 struct drm_plane *plane;
3690 struct nouveau_drm *drm = nouveau_drm(dev);
3691 struct nv50_disp *disp = nv50_disp(dev);
3692 struct nv50_atom *atom = nv50_atom(state);
3693 struct nv50_outp_atom *outp, *outt;
3694 u32 interlock_core = 0;
3695 u32 interlock_chan = 0;
3696 int i;
3697
3698 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
3699 drm_atomic_helper_wait_for_fences(dev, state, false);
3700 drm_atomic_helper_wait_for_dependencies(state);
3701 drm_atomic_helper_update_legacy_modeset_state(dev, state);
3702
3703 if (atom->lock_core)
3704 mutex_lock(&disp->mutex);
3705
3706 /* Disable head(s). */
3707 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3708 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3709 struct nv50_head *head = nv50_head(crtc);
3710
3711 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
3712 asyh->clr.mask, asyh->set.mask);
3713
3714 if (asyh->clr.mask) {
3715 nv50_head_flush_clr(head, asyh, atom->flush_disable);
3716 interlock_core |= 1;
3717 }
3718 }
3719
3720 /* Disable plane(s). */
3721 for_each_plane_in_state(state, plane, plane_state, i) {
3722 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3723 struct nv50_wndw *wndw = nv50_wndw(plane);
3724
3725 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
3726 asyw->clr.mask, asyw->set.mask);
3727 if (!asyw->clr.mask)
3728 continue;
3729
3730 interlock_chan |= nv50_wndw_flush_clr(wndw, interlock_core,
3731 atom->flush_disable,
3732 asyw);
3733 }
3734
3735 /* Disable output path(s). */
3736 list_for_each_entry(outp, &atom->outp, head) {
3737 const struct drm_encoder_helper_funcs *help;
3738 struct drm_encoder *encoder;
3739
3740 encoder = outp->encoder;
3741 help = encoder->helper_private;
3742
3743 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
3744 outp->clr.mask, outp->set.mask);
3745
3746 if (outp->clr.mask) {
3747 help->disable(encoder);
3748 interlock_core |= 1;
3749 if (outp->flush_disable) {
3750 nv50_disp_atomic_commit_core(drm, interlock_chan);
3751 interlock_core = 0;
3752 interlock_chan = 0;
3753 }
3754 }
3755 }
3756
3757 /* Flush disable. */
3758 if (interlock_core) {
3759 if (atom->flush_disable) {
3760 nv50_disp_atomic_commit_core(drm, interlock_chan);
3761 interlock_core = 0;
3762 interlock_chan = 0;
3763 }
3764 }
3765
3766 /* Update output path(s). */
3767 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
3768 const struct drm_encoder_helper_funcs *help;
3769 struct drm_encoder *encoder;
3770
3771 encoder = outp->encoder;
3772 help = encoder->helper_private;
3773
3774 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
3775 outp->set.mask, outp->clr.mask);
3776
3777 if (outp->set.mask) {
3778 help->enable(encoder);
3779 interlock_core = 1;
3780 }
3781
3782 list_del(&outp->head);
3783 kfree(outp);
3784 }
3785
3786 /* Update head(s). */
3787 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3788 struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
3789 struct nv50_head *head = nv50_head(crtc);
3790
3791 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
3792 asyh->set.mask, asyh->clr.mask);
3793
3794 if (asyh->set.mask) {
3795 nv50_head_flush_set(head, asyh);
3796 interlock_core = 1;
3797 }
3798 }
3799
3800 /* Update plane(s). */
3801 for_each_plane_in_state(state, plane, plane_state, i) {
3802 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3803 struct nv50_wndw *wndw = nv50_wndw(plane);
3804
3805 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
3806 asyw->set.mask, asyw->clr.mask);
3807 if ( !asyw->set.mask &&
3808 (!asyw->clr.mask || atom->flush_disable))
3809 continue;
3810
3811 interlock_chan |= nv50_wndw_flush_set(wndw, interlock_core, asyw);
3812 }
3813
3814 /* Flush update. */
3815 if (interlock_core) {
3816 if (!interlock_chan && atom->state.legacy_cursor_update) {
3817 u32 *push = evo_wait(&disp->mast, 2);
3818 if (push) {
3819 evo_mthd(push, 0x0080, 1);
3820 evo_data(push, 0x00000000);
3821 evo_kick(push, &disp->mast);
3822 }
3823 } else {
3824 nv50_disp_atomic_commit_core(drm, interlock_chan);
3825 }
3826 }
3827
3828 if (atom->lock_core)
3829 mutex_unlock(&disp->mutex);
3830
3831 /* Wait for HW to signal completion. */
3832 for_each_plane_in_state(state, plane, plane_state, i) {
3833 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
3834 struct nv50_wndw *wndw = nv50_wndw(plane);
3835 int ret = nv50_wndw_wait_armed(wndw, asyw);
3836 if (ret)
3837 NV_ERROR(drm, "%s: timeout\n", plane->name);
3838 }
3839
3840 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3841 if (crtc->state->event) {
3842 unsigned long flags;
3843 spin_lock_irqsave(&crtc->dev->event_lock, flags);
3844 drm_crtc_send_vblank_event(crtc, crtc->state->event);
3845 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
3846 crtc->state->event = NULL;
3847 }
3848 }
3849
3850 drm_atomic_helper_commit_hw_done(state);
3851 drm_atomic_helper_cleanup_planes(dev, state);
3852 drm_atomic_helper_commit_cleanup_done(state);
3853 drm_atomic_state_put(state);
3854}
3855
3856static void
3857nv50_disp_atomic_commit_work(struct work_struct *work)
3858{
3859 struct drm_atomic_state *state =
3860 container_of(work, typeof(*state), commit_work);
3861 nv50_disp_atomic_commit_tail(state);
3862}
3863
3864static int
3865nv50_disp_atomic_commit(struct drm_device *dev,
3866 struct drm_atomic_state *state, bool nonblock)
3867{
3868 struct nouveau_drm *drm = nouveau_drm(dev);
3869 struct nv50_disp *disp = nv50_disp(dev);
3870 struct drm_plane_state *plane_state;
3871 struct drm_plane *plane;
3872 struct drm_crtc *crtc;
3873 bool active = false;
3874 int ret, i;
3875
3876 ret = pm_runtime_get_sync(dev->dev);
3877 if (ret < 0 && ret != -EACCES)
3878 return ret;
3879
3880 ret = drm_atomic_helper_setup_commit(state, nonblock);
3881 if (ret)
3882 goto done;
3883
3884 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
3885
3886 ret = drm_atomic_helper_prepare_planes(dev, state);
3887 if (ret)
3888 goto done;
3889
3890 if (!nonblock) {
3891 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
3892 if (ret)
3893 goto done;
3894 }
3895
3896 for_each_plane_in_state(state, plane, plane_state, i) {
3897 struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane_state);
3898 struct nv50_wndw *wndw = nv50_wndw(plane);
3899 if (asyw->set.image) {
3900 asyw->ntfy.handle = wndw->dmac->sync.handle;
3901 asyw->ntfy.offset = wndw->ntfy;
3902 asyw->ntfy.awaken = false;
3903 asyw->set.ntfy = true;
3904 nouveau_bo_wr32(disp->sync, wndw->ntfy / 4, 0x00000000);
3905 wndw->ntfy ^= 0x10;
3906 }
3907 }
3908
3909 drm_atomic_helper_swap_state(state, true);
3910 drm_atomic_state_get(state);
3911
3912 if (nonblock)
3913 queue_work(system_unbound_wq, &state->commit_work);
3914 else
3915 nv50_disp_atomic_commit_tail(state);
3916
3917 drm_for_each_crtc(crtc, dev) {
3918 if (crtc->state->enable) {
3919 if (!drm->have_disp_power_ref) {
3920 drm->have_disp_power_ref = true;
3921 return ret;
3922 }
3923 active = true;
3924 break;
3925 }
3926 }
3927
3928 if (!active && drm->have_disp_power_ref) {
3929 pm_runtime_put_autosuspend(dev->dev);
3930 drm->have_disp_power_ref = false;
3931 }
3932
3933done:
3934 pm_runtime_put_autosuspend(dev->dev);
3935 return ret;
3936}
3937
3938static struct nv50_outp_atom *
3939nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
3940{
3941 struct nv50_outp_atom *outp;
3942
3943 list_for_each_entry(outp, &atom->outp, head) {
3944 if (outp->encoder == encoder)
3945 return outp;
3946 }
3947
3948 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
3949 if (!outp)
3950 return ERR_PTR(-ENOMEM);
3951
3952 list_add(&outp->head, &atom->outp);
3953 outp->encoder = encoder;
3954 return outp;
3955}
3956
3957static int
3958nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
3959 struct drm_connector *connector)
3960{
3961 struct drm_encoder *encoder = connector->state->best_encoder;
3962 struct drm_crtc_state *crtc_state;
3963 struct drm_crtc *crtc;
3964 struct nv50_outp_atom *outp;
3965
3966 if (!(crtc = connector->state->crtc))
3967 return 0;
3968
3969 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
3970 if (crtc->state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
3971 outp = nv50_disp_outp_atomic_add(atom, encoder);
3972 if (IS_ERR(outp))
3973 return PTR_ERR(outp);
3974
3975 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
3976 outp->flush_disable = true;
3977 atom->flush_disable = true;
3978 }
3979 outp->clr.ctrl = true;
3980 atom->lock_core = true;
3981 }
3982
3983 return 0;
3984}
3985
3986static int
3987nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
3988 struct drm_connector_state *connector_state)
3989{
3990 struct drm_encoder *encoder = connector_state->best_encoder;
3991 struct drm_crtc_state *crtc_state;
3992 struct drm_crtc *crtc;
3993 struct nv50_outp_atom *outp;
3994
3995 if (!(crtc = connector_state->crtc))
3996 return 0;
3997
3998 crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
3999 if (crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
4000 outp = nv50_disp_outp_atomic_add(atom, encoder);
4001 if (IS_ERR(outp))
4002 return PTR_ERR(outp);
4003
4004 outp->set.ctrl = true;
4005 atom->lock_core = true;
4006 }
4007
4008 return 0;
4009}
4010
4011static int
4012nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
4013{
4014 struct nv50_atom *atom = nv50_atom(state);
4015 struct drm_connector_state *connector_state;
4016 struct drm_connector *connector;
4017 int ret, i;
4018
4019 ret = drm_atomic_helper_check(dev, state);
4020 if (ret)
4021 return ret;
4022
4023 for_each_connector_in_state(state, connector, connector_state, i) {
4024 ret = nv50_disp_outp_atomic_check_clr(atom, connector);
4025 if (ret)
4026 return ret;
4027
4028 ret = nv50_disp_outp_atomic_check_set(atom, connector_state);
4029 if (ret)
4030 return ret;
4031 }
4032
4033 return 0;
4034}
4035
4036static void
4037nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
4038{
4039 struct nv50_atom *atom = nv50_atom(state);
4040 struct nv50_outp_atom *outp, *outt;
4041
4042 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
4043 list_del(&outp->head);
4044 kfree(outp);
4045 }
4046
4047 drm_atomic_state_default_clear(state);
4048}
4049
4050static void
4051nv50_disp_atomic_state_free(struct drm_atomic_state *state)
4052{
4053 struct nv50_atom *atom = nv50_atom(state);
4054 drm_atomic_state_default_release(&atom->state);
4055 kfree(atom);
4056}
4057
4058static struct drm_atomic_state *
4059nv50_disp_atomic_state_alloc(struct drm_device *dev)
4060{
4061 struct nv50_atom *atom;
4062 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
4063 drm_atomic_state_init(dev, &atom->state) < 0) {
4064 kfree(atom);
4065 return NULL;
4066 }
4067 INIT_LIST_HEAD(&atom->outp);
4068 return &atom->state;
4069}
4070
4071static const struct drm_mode_config_funcs
4072nv50_disp_func = {
4073 .fb_create = nouveau_user_framebuffer_create,
4074 .output_poll_changed = nouveau_fbcon_output_poll_changed,
4075 .atomic_check = nv50_disp_atomic_check,
4076 .atomic_commit = nv50_disp_atomic_commit,
4077 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
4078 .atomic_state_clear = nv50_disp_atomic_state_clear,
4079 .atomic_state_free = nv50_disp_atomic_state_free,
4080};
4081
4082/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10004083 * Init
4084 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10004085
Ben Skeggs2a44e492011-11-09 11:36:33 +10004086void
Ben Skeggse225f442012-11-21 14:40:21 +10004087nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004088{
Ben Skeggs973f10c2016-11-04 17:20:36 +10004089 struct drm_plane *plane;
4090
4091 drm_for_each_plane(plane, dev) {
4092 struct nv50_wndw *wndw = nv50_wndw(plane);
4093 if (plane->funcs != &nv50_wndw)
4094 continue;
4095 nv50_wndw_fini(wndw);
4096 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10004097}
4098
4099int
Ben Skeggse225f442012-11-21 14:40:21 +10004100nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004101{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004102 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs354d3502016-11-04 17:20:36 +10004103 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10004104 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004105 struct drm_crtc *crtc;
4106 u32 *push;
4107
4108 push = evo_wait(nv50_mast(dev), 32);
4109 if (!push)
4110 return -EBUSY;
4111
4112 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10004113 struct nv50_wndw *wndw = &nv50_head(crtc)->_base->wndw;
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01004114
4115 nv50_crtc_lut_load(crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +10004116 nouveau_bo_wr32(disp->sync, wndw->sema / 4, wndw->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004117 }
4118
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004119 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10004120 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004121 evo_kick(push, nv50_mast(dev));
Ben Skeggs973f10c2016-11-04 17:20:36 +10004122
Ben Skeggs354d3502016-11-04 17:20:36 +10004123 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4124 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
4125 const struct drm_encoder_helper_funcs *help;
4126 struct nouveau_encoder *nv_encoder;
4127
4128 nv_encoder = nouveau_encoder(encoder);
4129 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
4130 nv_encoder->dcb->type = DCB_OUTPUT_EOL;
4131
4132 help = encoder->helper_private;
4133 if (help && help->dpms)
4134 help->dpms(encoder, DRM_MODE_DPMS_ON);
4135
4136 if (nv_encoder->dcb->type == DCB_OUTPUT_EOL)
4137 nv_encoder->dcb->type = DCB_OUTPUT_DP;
4138 }
4139 }
4140
Ben Skeggs973f10c2016-11-04 17:20:36 +10004141 drm_for_each_plane(plane, dev) {
4142 struct nv50_wndw *wndw = nv50_wndw(plane);
4143 if (plane->funcs != &nv50_wndw)
4144 continue;
4145 nv50_wndw_init(wndw);
4146 }
4147
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10004148 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004149}
4150
4151void
Ben Skeggse225f442012-11-21 14:40:21 +10004152nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004153{
Ben Skeggse225f442012-11-21 14:40:21 +10004154 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004155
Ben Skeggs0ad72862014-08-10 04:10:22 +10004156 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10004157
Ben Skeggs816af2f2011-11-16 15:48:48 +10004158 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004159 if (disp->sync)
4160 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10004161 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10004162
Ben Skeggs77145f12012-07-31 16:16:21 +10004163 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004164 kfree(disp);
4165}
4166
Ben Skeggs839ca902016-11-04 17:20:36 +10004167MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
4168static int nouveau_atomic = 0;
4169module_param_named(atomic, nouveau_atomic, int, 0400);
4170
Ben Skeggs26f6d882011-07-04 16:25:18 +10004171int
Ben Skeggse225f442012-11-21 14:40:21 +10004172nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10004173{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10004174 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10004175 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10004176 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004177 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10004178 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10004179 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10004180 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004181
4182 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
4183 if (!disp)
4184 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10004185
Ben Skeggs839ca902016-11-04 17:20:36 +10004186 mutex_init(&disp->mutex);
4187
Ben Skeggs77145f12012-07-31 16:16:21 +10004188 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10004189 nouveau_display(dev)->dtor = nv50_display_destroy;
4190 nouveau_display(dev)->init = nv50_display_init;
4191 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10004192 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
4193 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10004194 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs839ca902016-11-04 17:20:36 +10004195 dev->mode_config.funcs = &nv50_disp_func;
4196 if (nouveau_atomic)
4197 dev->driver->driver_features |= DRIVER_ATOMIC;
Ben Skeggs26f6d882011-07-04 16:25:18 +10004198
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004199 /* small shared memory area we use for notifiers and semaphores */
4200 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01004201 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004202 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10004203 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004204 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004205 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01004206 if (ret)
4207 nouveau_bo_unpin(disp->sync);
4208 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004209 if (ret)
4210 nouveau_bo_ref(NULL, &disp->sync);
4211 }
4212
4213 if (ret)
4214 goto out;
4215
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004216 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10004217 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10004218 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10004219 if (ret)
4220 goto out;
4221
Ben Skeggs438d99e2011-07-05 16:48:06 +10004222 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10004223 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsa01ca782015-08-20 14:54:15 +10004224 crtcs = nvif_rd32(&device->object, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10004225 else
4226 crtcs = 2;
4227
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10004228 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10004229 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10004230 if (ret)
4231 goto out;
4232 }
4233
Ben Skeggs83fc0832011-07-05 13:08:40 +10004234 /* create encoder/connector objects based on VBIOS DCB table */
4235 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
4236 connector = nouveau_connector_create(dev, dcbe->connector);
4237 if (IS_ERR(connector))
4238 continue;
4239
Ben Skeggseb6313a2013-02-11 09:52:58 +10004240 if (dcbe->location == DCB_LOC_ON_CHIP) {
4241 switch (dcbe->type) {
4242 case DCB_OUTPUT_TMDS:
4243 case DCB_OUTPUT_LVDS:
4244 case DCB_OUTPUT_DP:
4245 ret = nv50_sor_create(connector, dcbe);
4246 break;
4247 case DCB_OUTPUT_ANALOG:
4248 ret = nv50_dac_create(connector, dcbe);
4249 break;
4250 default:
4251 ret = -ENODEV;
4252 break;
4253 }
4254 } else {
4255 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004256 }
4257
Ben Skeggseb6313a2013-02-11 09:52:58 +10004258 if (ret) {
4259 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
4260 dcbe->location, dcbe->type,
4261 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10004262 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10004263 }
4264 }
4265
4266 /* cull any connectors we created that don't have an encoder */
4267 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
4268 if (connector->encoder_ids[0])
4269 continue;
4270
Ben Skeggs77145f12012-07-31 16:16:21 +10004271 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03004272 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10004273 connector->funcs->destroy(connector);
4274 }
4275
Ben Skeggs26f6d882011-07-04 16:25:18 +10004276out:
4277 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10004278 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10004279 return ret;
4280}