blob: 742ac7cbff55f71b62a3ecf6a44cbc431105ffd1 [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 Skeggs3a89cd02011-07-07 10:47:10 +100052#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100053
Ben Skeggs8a464382011-11-12 23:52:07 +100054#define EVO_DMA_NR 9
55
Ben Skeggsbdb8c212011-11-12 01:30:24 +100056#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100057#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100058#define EVO_OVLY(c) (0x05 + (c))
59#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100060#define EVO_CURS(c) (0x0d + (c))
61
Ben Skeggs816af2f2011-11-16 15:48:48 +100062/* offsets in shared sync bo of various structures */
63#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100064#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
65#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
66#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs973f10c2016-11-04 17:20:36 +100067#define EVO_FLIP_NTFY0(c) EVO_SYNC((c) + 1, 0x20)
68#define EVO_FLIP_NTFY1(c) EVO_SYNC((c) + 1, 0x30)
Ben Skeggs816af2f2011-11-16 15:48:48 +100069
Ben Skeggsb5a794b2012-10-16 14:18:32 +100070/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +100071 * Atomic state
72 *****************************************************************************/
73#define nv50_head_atom(p) container_of((p), struct nv50_head_atom, state)
74
75struct nv50_head_atom {
76 struct drm_crtc_state state;
77
Ben Skeggsc4e68122016-11-04 17:20:36 +100078 struct {
79 u16 iW;
80 u16 iH;
81 u16 oW;
82 u16 oH;
83 } view;
84
Ben Skeggs3dbd0362016-11-04 17:20:36 +100085 struct nv50_head_mode {
86 bool interlace;
87 u32 clock;
88 struct {
89 u16 active;
90 u16 synce;
91 u16 blanke;
92 u16 blanks;
93 } h;
94 struct {
95 u32 active;
96 u16 synce;
97 u16 blanke;
98 u16 blanks;
99 u16 blank2s;
100 u16 blank2e;
101 u16 blankus;
102 } v;
103 } mode;
104
Ben Skeggsad633612016-11-04 17:20:36 +1000105 struct {
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000106 u32 handle;
107 u64 offset:40;
108 } lut;
109
110 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000111 bool visible;
112 u32 handle;
113 u64 offset:40;
114 u8 format;
115 u8 kind:7;
116 u8 layout:1;
117 u8 block:4;
118 u32 pitch:20;
119 u16 x;
120 u16 y;
121 u16 w;
122 u16 h;
123 } core;
124
125 struct {
Ben Skeggsea8ee392016-11-04 17:20:36 +1000126 bool visible;
127 u32 handle;
128 u64 offset:40;
129 u8 layout:1;
130 u8 format:1;
131 } curs;
132
133 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000134 u8 depth;
135 u8 cpp;
136 u16 x;
137 u16 y;
138 u16 w;
139 u16 h;
140 } base;
141
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000142 struct {
143 u8 cpp;
144 } ovly;
145
Ben Skeggs7e918332016-11-04 17:20:36 +1000146 struct {
147 bool enable:1;
148 u8 bits:2;
149 u8 mode:4;
150 } dither;
151
Ben Skeggs7e08d672016-11-04 17:20:36 +1000152 struct {
153 struct {
154 u16 cos:12;
155 u16 sin:12;
156 } sat;
157 } procamp;
158
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000159 union {
160 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000161 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000162 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000163 };
164 u8 mask;
165 } clr;
166
167 union {
168 struct {
169 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000170 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000171 bool view:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000172 bool mode:1;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000173 bool base:1;
174 bool ovly:1;
Ben Skeggs7e918332016-11-04 17:20:36 +1000175 bool dither:1;
Ben Skeggs7e08d672016-11-04 17:20:36 +1000176 bool procamp:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000177 };
178 u16 mask;
179 } set;
180};
181
Ben Skeggs973f10c2016-11-04 17:20:36 +1000182#define nv50_wndw_atom(p) container_of((p), struct nv50_wndw_atom, state)
183
184struct nv50_wndw_atom {
185 struct drm_plane_state state;
186 u8 interval;
187
188 struct drm_rect clip;
189
190 struct {
191 u32 handle;
192 u16 offset:12;
193 bool awaken:1;
194 } ntfy;
195
196 struct {
197 u32 handle;
198 u16 offset:12;
199 u32 acquire;
200 u32 release;
201 } sema;
202
203 struct {
204 u8 enable:2;
205 } lut;
206
207 struct {
208 u8 mode:2;
209 u8 interval:4;
210
211 u8 format;
212 u8 kind:7;
213 u8 layout:1;
214 u8 block:4;
215 u32 pitch:20;
216 u16 w;
217 u16 h;
218
219 u32 handle;
220 u64 offset;
221 } image;
222
223 struct {
224 u16 x;
225 u16 y;
226 } point;
227
228 union {
229 struct {
230 bool ntfy:1;
231 bool sema:1;
232 bool image:1;
233 };
234 u8 mask;
235 } clr;
236
237 union {
238 struct {
239 bool ntfy:1;
240 bool sema:1;
241 bool image:1;
242 bool lut:1;
243 bool point:1;
244 };
245 u8 mask;
246 } set;
247};
248
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000249/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000250 * EVO channel
251 *****************************************************************************/
252
Ben Skeggse225f442012-11-21 14:40:21 +1000253struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000254 struct nvif_object user;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000255 struct nvif_device *device;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000256};
257
258static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000259nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000260 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000261 struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000262{
Ben Skeggs41a63402015-08-20 14:54:16 +1000263 struct nvif_sclass *sclass;
264 int ret, i, n;
Ben Skeggs6af52892014-11-03 15:01:33 +1000265
Ben Skeggsa01ca782015-08-20 14:54:15 +1000266 chan->device = device;
267
Ben Skeggs41a63402015-08-20 14:54:16 +1000268 ret = n = nvif_object_sclass_get(disp, &sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000269 if (ret < 0)
270 return ret;
271
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000272 while (oclass[0]) {
Ben Skeggs41a63402015-08-20 14:54:16 +1000273 for (i = 0; i < n; i++) {
274 if (sclass[i].oclass == oclass[0]) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000275 ret = nvif_object_init(disp, 0, oclass[0],
Ben Skeggsa01ca782015-08-20 14:54:15 +1000276 data, size, &chan->user);
Ben Skeggs6af52892014-11-03 15:01:33 +1000277 if (ret == 0)
278 nvif_object_map(&chan->user);
Ben Skeggs41a63402015-08-20 14:54:16 +1000279 nvif_object_sclass_put(&sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000280 return ret;
281 }
Ben Skeggsb76f1522014-08-10 04:10:28 +1000282 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000283 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000284 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000285
Ben Skeggs41a63402015-08-20 14:54:16 +1000286 nvif_object_sclass_put(&sclass);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000287 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000288}
289
290static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000291nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000292{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000293 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000294}
295
296/******************************************************************************
297 * PIO EVO channel
298 *****************************************************************************/
299
Ben Skeggse225f442012-11-21 14:40:21 +1000300struct nv50_pioc {
301 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000302};
303
304static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000305nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000306{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000307 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000308}
309
310static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000311nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000312 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000313 struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000314{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000315 return nv50_chan_create(device, disp, oclass, head, data, size,
316 &pioc->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000317}
318
319/******************************************************************************
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000320 * Overlay Immediate
321 *****************************************************************************/
322
323struct nv50_oimm {
324 struct nv50_pioc base;
325};
326
327static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000328nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
329 int head, struct nv50_oimm *oimm)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000330{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000331 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000332 .head = head,
333 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000334 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000335 GK104_DISP_OVERLAY,
336 GF110_DISP_OVERLAY,
337 GT214_DISP_OVERLAY,
338 G82_DISP_OVERLAY,
339 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000340 0
341 };
342
Ben Skeggsa01ca782015-08-20 14:54:15 +1000343 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
344 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000345}
346
347/******************************************************************************
348 * DMA EVO channel
349 *****************************************************************************/
350
Ben Skeggsaccdea22016-11-04 17:20:36 +1000351struct nv50_dmac_ctxdma {
352 struct list_head head;
353 struct nvif_object object;
354};
355
Ben Skeggse225f442012-11-21 14:40:21 +1000356struct nv50_dmac {
357 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000358 dma_addr_t handle;
359 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100360
Ben Skeggs0ad72862014-08-10 04:10:22 +1000361 struct nvif_object sync;
362 struct nvif_object vram;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000363 struct list_head ctxdma;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000364
Daniel Vetter59ad1462012-12-02 14:49:44 +0100365 /* Protects against concurrent pushbuf access to this channel, lock is
366 * grabbed by evo_wait (if the pushbuf reservation is successful) and
367 * dropped again by evo_kick. */
368 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000369};
370
371static void
Ben Skeggsaccdea22016-11-04 17:20:36 +1000372nv50_dmac_ctxdma_del(struct nv50_dmac_ctxdma *ctxdma)
373{
374 nvif_object_fini(&ctxdma->object);
375 list_del(&ctxdma->head);
376 kfree(ctxdma);
377}
378
379static struct nv50_dmac_ctxdma *
380nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, u32 handle,
381 struct nouveau_framebuffer *fb)
382{
383 struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
384 struct nv50_dmac_ctxdma *ctxdma;
385 const u8 kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
386 struct {
387 struct nv_dma_v0 base;
388 union {
389 struct nv50_dma_v0 nv50;
390 struct gf100_dma_v0 gf100;
391 struct gf119_dma_v0 gf119;
392 };
393 } args = {};
394 u32 argc = sizeof(args.base);
395 int ret;
396
397 list_for_each_entry(ctxdma, &dmac->ctxdma, head) {
398 if (ctxdma->object.handle == handle)
399 return ctxdma;
400 }
401
402 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
403 return ERR_PTR(-ENOMEM);
404 list_add(&ctxdma->head, &dmac->ctxdma);
405
406 args.base.target = NV_DMA_V0_TARGET_VRAM;
407 args.base.access = NV_DMA_V0_ACCESS_RDWR;
408 args.base.start = 0;
409 args.base.limit = drm->device.info.ram_user - 1;
410
411 if (drm->device.info.chipset < 0x80) {
412 args.nv50.part = NV50_DMA_V0_PART_256;
413 argc += sizeof(args.nv50);
414 } else
415 if (drm->device.info.chipset < 0xc0) {
416 args.nv50.part = NV50_DMA_V0_PART_256;
417 args.nv50.kind = kind;
418 argc += sizeof(args.nv50);
419 } else
420 if (drm->device.info.chipset < 0xd0) {
421 args.gf100.kind = kind;
422 argc += sizeof(args.gf100);
423 } else {
424 args.gf119.page = GF119_DMA_V0_PAGE_LP;
425 args.gf119.kind = kind;
426 argc += sizeof(args.gf119);
427 }
428
429 ret = nvif_object_init(&dmac->base.user, handle, NV_DMA_IN_MEMORY,
430 &args, argc, &ctxdma->object);
431 if (ret) {
432 nv50_dmac_ctxdma_del(ctxdma);
433 return ERR_PTR(ret);
434 }
435
436 return ctxdma;
437}
438
439static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000440nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000441{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000442 struct nvif_device *device = dmac->base.device;
Ben Skeggsaccdea22016-11-04 17:20:36 +1000443 struct nv50_dmac_ctxdma *ctxdma, *ctxtmp;
444
445 list_for_each_entry_safe(ctxdma, ctxtmp, &dmac->ctxdma, head) {
446 nv50_dmac_ctxdma_del(ctxdma);
447 }
Ben Skeggsa01ca782015-08-20 14:54:15 +1000448
Ben Skeggs0ad72862014-08-10 04:10:22 +1000449 nvif_object_fini(&dmac->vram);
450 nvif_object_fini(&dmac->sync);
451
452 nv50_chan_destroy(&dmac->base);
453
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000454 if (dmac->ptr) {
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000455 struct device *dev = nvxx_device(device)->dev;
456 dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000457 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000458}
459
460static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000461nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000462 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000463 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000464{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000465 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000466 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000467 int ret;
468
Daniel Vetter59ad1462012-12-02 14:49:44 +0100469 mutex_init(&dmac->lock);
470
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000471 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
472 &dmac->handle, GFP_KERNEL);
Ben Skeggs47057302012-11-16 13:58:48 +1000473 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000474 return -ENOMEM;
475
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000476 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
477 &(struct nv_dma_v0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +1000478 .target = NV_DMA_V0_TARGET_PCI_US,
479 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000480 .start = dmac->handle + 0x0000,
481 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000482 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000483 if (ret)
484 return ret;
485
Ben Skeggsbf81df92015-08-20 14:54:16 +1000486 args->pushbuf = nvif_handle(&pushbuf);
487
Ben Skeggsa01ca782015-08-20 14:54:15 +1000488 ret = nv50_chan_create(device, disp, oclass, head, data, size,
489 &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000490 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000491 if (ret)
492 return ret;
493
Ben Skeggsa01ca782015-08-20 14:54:15 +1000494 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000495 &(struct nv_dma_v0) {
496 .target = NV_DMA_V0_TARGET_VRAM,
497 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000498 .start = syncbuf + 0x0000,
499 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000500 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000501 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000502 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000503 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000504
Ben Skeggsa01ca782015-08-20 14:54:15 +1000505 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000506 &(struct nv_dma_v0) {
507 .target = NV_DMA_V0_TARGET_VRAM,
508 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000509 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000510 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000511 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000512 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000513 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000514 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000515
Ben Skeggsaccdea22016-11-04 17:20:36 +1000516 INIT_LIST_HEAD(&dmac->ctxdma);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000517 return ret;
518}
519
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000520/******************************************************************************
521 * Core
522 *****************************************************************************/
523
Ben Skeggse225f442012-11-21 14:40:21 +1000524struct nv50_mast {
525 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000526};
527
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000528static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000529nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
530 u64 syncbuf, struct nv50_mast *core)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000531{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000532 struct nv50_disp_core_channel_dma_v0 args = {
533 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000534 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000535 static const s32 oclass[] = {
Ben Skeggsfd478772016-07-09 10:41:01 +1000536 GP104_DISP_CORE_CHANNEL_DMA,
Ben Skeggsf9d5cbb2016-07-09 10:41:01 +1000537 GP100_DISP_CORE_CHANNEL_DMA,
Ben Skeggsdb1eb522016-02-11 08:35:32 +1000538 GM200_DISP_CORE_CHANNEL_DMA,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000539 GM107_DISP_CORE_CHANNEL_DMA,
540 GK110_DISP_CORE_CHANNEL_DMA,
541 GK104_DISP_CORE_CHANNEL_DMA,
542 GF110_DISP_CORE_CHANNEL_DMA,
543 GT214_DISP_CORE_CHANNEL_DMA,
544 GT206_DISP_CORE_CHANNEL_DMA,
545 GT200_DISP_CORE_CHANNEL_DMA,
546 G82_DISP_CORE_CHANNEL_DMA,
547 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000548 0
549 };
550
Ben Skeggsa01ca782015-08-20 14:54:15 +1000551 return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
552 syncbuf, &core->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000553}
554
555/******************************************************************************
556 * Base
557 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000558
Ben Skeggse225f442012-11-21 14:40:21 +1000559struct nv50_sync {
560 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000561 u32 addr;
562 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000563};
564
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000565static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000566nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
567 int head, u64 syncbuf, struct nv50_sync *base)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000568{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000569 struct nv50_disp_base_channel_dma_v0 args = {
570 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000571 .head = head,
572 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000573 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000574 GK110_DISP_BASE_CHANNEL_DMA,
575 GK104_DISP_BASE_CHANNEL_DMA,
576 GF110_DISP_BASE_CHANNEL_DMA,
577 GT214_DISP_BASE_CHANNEL_DMA,
578 GT200_DISP_BASE_CHANNEL_DMA,
579 G82_DISP_BASE_CHANNEL_DMA,
580 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000581 0
582 };
583
Ben Skeggsa01ca782015-08-20 14:54:15 +1000584 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000585 syncbuf, &base->base);
586}
587
588/******************************************************************************
589 * Overlay
590 *****************************************************************************/
591
Ben Skeggse225f442012-11-21 14:40:21 +1000592struct nv50_ovly {
593 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000594};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000595
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000596static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000597nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
598 int head, u64 syncbuf, struct nv50_ovly *ovly)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000599{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000600 struct nv50_disp_overlay_channel_dma_v0 args = {
601 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000602 .head = head,
603 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000604 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000605 GK104_DISP_OVERLAY_CONTROL_DMA,
606 GF110_DISP_OVERLAY_CONTROL_DMA,
607 GT214_DISP_OVERLAY_CHANNEL_DMA,
608 GT200_DISP_OVERLAY_CHANNEL_DMA,
609 G82_DISP_OVERLAY_CHANNEL_DMA,
610 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000611 0
612 };
613
Ben Skeggsa01ca782015-08-20 14:54:15 +1000614 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000615 syncbuf, &ovly->base);
616}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000617
Ben Skeggse225f442012-11-21 14:40:21 +1000618struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000619 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000620 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000621 struct nv50_ovly ovly;
622 struct nv50_oimm oimm;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000623
624 struct nv50_head_atom arm;
625 struct nv50_head_atom asy;
Ben Skeggs973f10c2016-11-04 17:20:36 +1000626
627 struct nv50_base *_base;
Ben Skeggs22e927d2016-11-04 17:20:36 +1000628 struct nv50_curs *_curs;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000629};
630
Ben Skeggse225f442012-11-21 14:40:21 +1000631#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
Ben Skeggse225f442012-11-21 14:40:21 +1000632#define nv50_ovly(c) (&nv50_head(c)->ovly)
633#define nv50_oimm(c) (&nv50_head(c)->oimm)
634#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000635#define nv50_vers(c) nv50_chan(c)->user.oclass
636
Ben Skeggse225f442012-11-21 14:40:21 +1000637struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000638 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000639 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000640
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000641 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000642};
643
Ben Skeggse225f442012-11-21 14:40:21 +1000644static struct nv50_disp *
645nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000646{
Ben Skeggs77145f12012-07-31 16:16:21 +1000647 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000648}
649
Ben Skeggse225f442012-11-21 14:40:21 +1000650#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000651
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000652static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000653nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000654{
655 return nouveau_encoder(encoder)->crtc;
656}
657
658/******************************************************************************
659 * EVO channel helpers
660 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000661static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000662evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000663{
Ben Skeggse225f442012-11-21 14:40:21 +1000664 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000665 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000666 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000667
Daniel Vetter59ad1462012-12-02 14:49:44 +0100668 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000669 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000670 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000671
Ben Skeggs0ad72862014-08-10 04:10:22 +1000672 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000673 if (nvif_msec(device, 2000,
674 if (!nvif_rd32(&dmac->base.user, 0x0004))
675 break;
676 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100677 mutex_unlock(&dmac->lock);
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000678 printk(KERN_ERR "nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000679 return NULL;
680 }
681
682 put = 0;
683 }
684
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000685 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000686}
687
688static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000689evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000690{
Ben Skeggse225f442012-11-21 14:40:21 +1000691 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000692 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100693 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000694}
695
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000696#define evo_mthd(p,m,s) do { \
697 const u32 _m = (m), _s = (s); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000698 if (drm_debug & DRM_UT_KMS) \
699 printk(KERN_ERR "%04x %d %s\n", _m, _s, __func__); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000700 *((p)++) = ((_s << 18) | _m); \
701} while(0)
Ben Skeggs7f55a072016-11-04 17:20:36 +1000702
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000703#define evo_data(p,d) do { \
704 const u32 _d = (d); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000705 if (drm_debug & DRM_UT_KMS) \
706 printk(KERN_ERR "\t%08x\n", _d); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000707 *((p)++) = _d; \
708} while(0)
Ben Skeggs51beb422011-07-05 10:33:08 +1000709
Ben Skeggs3376ee32011-11-12 14:28:12 +1000710static bool
711evo_sync_wait(void *data)
712{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500713 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
714 return true;
715 usleep_range(1, 2);
716 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000717}
718
719static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000720evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000721{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000722 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000723 struct nv50_disp *disp = nv50_disp(dev);
724 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000725 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000726 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000727 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000728 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000729 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000730 evo_mthd(push, 0x0080, 2);
731 evo_data(push, 0x00000000);
732 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000733 evo_kick(push, mast);
Ben Skeggs54442042015-08-20 14:54:11 +1000734 if (nvif_msec(device, 2000,
735 if (evo_sync_wait(disp->sync))
736 break;
737 ) >= 0)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000738 return 0;
739 }
740
741 return -EBUSY;
742}
743
744/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +1000745 * Plane
746 *****************************************************************************/
747#define nv50_wndw(p) container_of((p), struct nv50_wndw, plane)
748
749struct nv50_wndw {
750 const struct nv50_wndw_func *func;
751 struct nv50_dmac *dmac;
752
753 struct drm_plane plane;
754
755 struct nvif_notify notify;
756 u16 ntfy;
757 u16 sema;
758 u32 data;
759
760 struct nv50_wndw_atom arm;
761 struct nv50_wndw_atom asy;
762};
763
764struct nv50_wndw_func {
765 void *(*dtor)(struct nv50_wndw *);
766 int (*acquire)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
767 struct nv50_head_atom *asyh);
768 void (*release)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
769 struct nv50_head_atom *asyh);
770 void (*prepare)(struct nv50_wndw *, struct nv50_head_atom *asyh,
771 struct nv50_wndw_atom *asyw);
772
773 void (*sema_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
774 void (*sema_clr)(struct nv50_wndw *);
775 void (*ntfy_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
776 void (*ntfy_clr)(struct nv50_wndw *);
777 int (*ntfy_wait_begun)(struct nv50_wndw *, struct nv50_wndw_atom *);
778 void (*image_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
779 void (*image_clr)(struct nv50_wndw *);
780 void (*lut)(struct nv50_wndw *, struct nv50_wndw_atom *);
781 void (*point)(struct nv50_wndw *, struct nv50_wndw_atom *);
782
783 u32 (*update)(struct nv50_wndw *, u32 interlock);
784};
785
786static int
787nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
788{
789 if (asyw->set.ntfy)
790 return wndw->func->ntfy_wait_begun(wndw, asyw);
791 return 0;
792}
793
794static u32
795nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 interlock, bool flush,
796 struct nv50_wndw_atom *asyw)
797{
798 if (asyw->clr.sema && (!asyw->set.sema || flush))
799 wndw->func->sema_clr(wndw);
800 if (asyw->clr.ntfy && (!asyw->set.ntfy || flush))
801 wndw->func->ntfy_clr(wndw);
802 if (asyw->clr.image && (!asyw->set.image || flush))
803 wndw->func->image_clr(wndw);
804
805 return flush ? wndw->func->update(wndw, interlock) : 0;
806}
807
808static u32
809nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 interlock,
810 struct nv50_wndw_atom *asyw)
811{
812 if (interlock) {
813 asyw->image.mode = 0;
814 asyw->image.interval = 1;
815 }
816
817 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
818 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
819 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
820 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
821 if (asyw->set.point) wndw->func->point (wndw, asyw);
822
823 return wndw->func->update(wndw, interlock);
824}
825
826static void
827nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
828 struct nv50_wndw_atom *asyw,
829 struct nv50_head_atom *asyh)
830{
831 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
832 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
833 wndw->func->release(wndw, asyw, asyh);
834 asyw->ntfy.handle = 0;
835 asyw->sema.handle = 0;
836}
837
838static int
839nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
840 struct nv50_wndw_atom *asyw,
841 struct nv50_head_atom *asyh)
842{
843 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
844 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
845 int ret;
846
847 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
848 asyw->clip.x1 = 0;
849 asyw->clip.y1 = 0;
850 asyw->clip.x2 = asyh->state.mode.hdisplay;
851 asyw->clip.y2 = asyh->state.mode.vdisplay;
852
853 asyw->image.w = fb->base.width;
854 asyw->image.h = fb->base.height;
855 asyw->image.kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
856 if (asyw->image.kind) {
857 asyw->image.layout = 0;
858 if (drm->device.info.chipset >= 0xc0)
859 asyw->image.block = fb->nvbo->tile_mode >> 4;
860 else
861 asyw->image.block = fb->nvbo->tile_mode;
862 asyw->image.pitch = (fb->base.pitches[0] / 4) << 4;
863 } else {
864 asyw->image.layout = 1;
865 asyw->image.block = 0;
866 asyw->image.pitch = fb->base.pitches[0];
867 }
868
869 ret = wndw->func->acquire(wndw, asyw, asyh);
870 if (ret)
871 return ret;
872
873 if (asyw->set.image) {
874 if (!(asyw->image.mode = asyw->interval ? 0 : 1))
875 asyw->image.interval = asyw->interval;
876 else
877 asyw->image.interval = 0;
878 }
879
880 return 0;
881}
882
883static int
884nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
885{
886 struct nouveau_drm *drm = nouveau_drm(plane->dev);
887 struct nv50_wndw *wndw = nv50_wndw(plane);
888 struct nv50_wndw_atom *armw = &wndw->arm;
889 struct nv50_wndw_atom *asyw = &wndw->asy;
890 struct nv50_head_atom *harm = NULL, *asyh = NULL;
891 bool varm = false, asyv = false, asym = false;
892 int ret;
893
894 asyw->clr.mask = 0;
895 asyw->set.mask = 0;
896
897 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
898 if (asyw->state.crtc) {
899 asyh = &nv50_head(asyw->state.crtc)->asy;
900 if (IS_ERR(asyh))
901 return PTR_ERR(asyh);
902 asym = drm_atomic_crtc_needs_modeset(&asyh->state);
903 asyv = asyh->state.active;
904 }
905
906 if (armw->state.crtc) {
907 harm = &nv50_head(armw->state.crtc)->asy;
908 if (IS_ERR(harm))
909 return PTR_ERR(harm);
910 varm = nv50_head(harm->state.crtc)->arm.state.active;
911 }
912
913 if (asyv) {
914 asyw->point.x = asyw->state.crtc_x;
915 asyw->point.y = asyw->state.crtc_y;
916 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
917 asyw->set.point = true;
918
919 if (!varm || asym || armw->state.fb != asyw->state.fb) {
920 ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh);
921 if (ret)
922 return ret;
923 }
924 } else
925 if (varm) {
926 nv50_wndw_atomic_check_release(wndw, asyw, harm);
927 } else {
928 return 0;
929 }
930
931 if (!asyv || asym) {
932 asyw->clr.ntfy = armw->ntfy.handle != 0;
933 asyw->clr.sema = armw->sema.handle != 0;
934 if (wndw->func->image_clr)
935 asyw->clr.image = armw->image.handle != 0;
936 asyw->set.lut = wndw->func->lut && asyv;
937 }
938
939 memcpy(armw, asyw, sizeof(*asyw));
940 return 0;
941}
942
943static void
944nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
945 struct drm_plane_state *state)
946{
947 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
948 __drm_atomic_helper_plane_destroy_state(&asyw->state);
949 dma_fence_put(asyw->state.fence);
950 kfree(asyw);
951}
952
953static struct drm_plane_state *
954nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
955{
956 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
957 struct nv50_wndw_atom *asyw;
958 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
959 return NULL;
960 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
961 asyw->state.fence = NULL;
962 asyw->interval = 1;
963 asyw->sema = armw->sema;
964 asyw->ntfy = armw->ntfy;
965 asyw->image = armw->image;
966 asyw->point = armw->point;
967 asyw->lut = armw->lut;
968 asyw->clr.mask = 0;
969 asyw->set.mask = 0;
970 return &asyw->state;
971}
972
973static void
974nv50_wndw_reset(struct drm_plane *plane)
975{
976 struct nv50_wndw_atom *asyw;
977
978 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
979 return;
980
981 if (plane->state)
982 plane->funcs->atomic_destroy_state(plane, plane->state);
983 plane->state = &asyw->state;
984 plane->state->plane = plane;
985 plane->state->rotation = DRM_ROTATE_0;
986}
987
988static void
989nv50_wndw_destroy(struct drm_plane *plane)
990{
991 struct nv50_wndw *wndw = nv50_wndw(plane);
992 void *data;
993 nvif_notify_fini(&wndw->notify);
994 data = wndw->func->dtor(wndw);
995 drm_plane_cleanup(&wndw->plane);
996 kfree(data);
997}
998
999static const struct drm_plane_funcs
1000nv50_wndw = {
1001 .destroy = nv50_wndw_destroy,
1002 .reset = nv50_wndw_reset,
1003 .set_property = drm_atomic_helper_plane_set_property,
1004 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
1005 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
1006};
1007
1008static void
1009nv50_wndw_fini(struct nv50_wndw *wndw)
1010{
1011 nvif_notify_put(&wndw->notify);
1012}
1013
1014static void
1015nv50_wndw_init(struct nv50_wndw *wndw)
1016{
1017 nvif_notify_get(&wndw->notify);
1018}
1019
1020static int
1021nv50_wndw_ctor(const struct nv50_wndw_func *func, struct drm_device *dev,
1022 enum drm_plane_type type, const char *name, int index,
1023 struct nv50_dmac *dmac, const u32 *format, int nformat,
1024 struct nv50_wndw *wndw)
1025{
1026 int ret;
1027
1028 wndw->func = func;
1029 wndw->dmac = dmac;
1030
1031 ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw, format,
1032 nformat, type, "%s-%d", name, index);
1033 if (ret)
1034 return ret;
1035
1036 return 0;
1037}
1038
1039/******************************************************************************
Ben Skeggs22e927d2016-11-04 17:20:36 +10001040 * Cursor plane
1041 *****************************************************************************/
1042#define nv50_curs(p) container_of((p), struct nv50_curs, wndw)
1043
1044struct nv50_curs {
1045 struct nv50_wndw wndw;
1046 struct nvif_object chan;
1047};
1048
1049static u32
1050nv50_curs_update(struct nv50_wndw *wndw, u32 interlock)
1051{
1052 struct nv50_curs *curs = nv50_curs(wndw);
1053 nvif_wr32(&curs->chan, 0x0080, 0x00000000);
1054 return 0;
1055}
1056
1057static void
1058nv50_curs_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1059{
1060 struct nv50_curs *curs = nv50_curs(wndw);
1061 nvif_wr32(&curs->chan, 0x0084, (asyw->point.y << 16) | asyw->point.x);
1062}
1063
1064static void
1065nv50_curs_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
1066 struct nv50_wndw_atom *asyw)
1067{
1068 asyh->curs.handle = nv50_disp(wndw->plane.dev)->mast.base.vram.handle;
1069 asyh->curs.offset = asyw->image.offset;
1070 asyh->set.curs = asyh->curs.visible;
1071}
1072
1073static void
1074nv50_curs_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1075 struct nv50_head_atom *asyh)
1076{
1077 asyh->curs.visible = false;
1078}
1079
1080static int
1081nv50_curs_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1082 struct nv50_head_atom *asyh)
1083{
1084 int ret;
1085
1086 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1087 DRM_PLANE_HELPER_NO_SCALING,
1088 DRM_PLANE_HELPER_NO_SCALING,
1089 true, true);
1090 asyh->curs.visible = asyw->state.visible;
1091 if (ret || !asyh->curs.visible)
1092 return ret;
1093
1094 switch (asyw->state.fb->width) {
1095 case 32: asyh->curs.layout = 0; break;
1096 case 64: asyh->curs.layout = 1; break;
1097 default:
1098 return -EINVAL;
1099 }
1100
1101 if (asyw->state.fb->width != asyw->state.fb->height)
1102 return -EINVAL;
1103
1104 switch (asyw->state.fb->pixel_format) {
1105 case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
1106 default:
1107 WARN_ON(1);
1108 return -EINVAL;
1109 }
1110
1111 return 0;
1112}
1113
1114static void *
1115nv50_curs_dtor(struct nv50_wndw *wndw)
1116{
1117 struct nv50_curs *curs = nv50_curs(wndw);
1118 nvif_object_fini(&curs->chan);
1119 return curs;
1120}
1121
1122static const u32
1123nv50_curs_format[] = {
1124 DRM_FORMAT_ARGB8888,
1125};
1126
1127static const struct nv50_wndw_func
1128nv50_curs = {
1129 .dtor = nv50_curs_dtor,
1130 .acquire = nv50_curs_acquire,
1131 .release = nv50_curs_release,
1132 .prepare = nv50_curs_prepare,
1133 .point = nv50_curs_point,
1134 .update = nv50_curs_update,
1135};
1136
1137static int
1138nv50_curs_new(struct nouveau_drm *drm, struct nv50_head *head,
1139 struct nv50_curs **pcurs)
1140{
1141 static const struct nvif_mclass curses[] = {
1142 { GK104_DISP_CURSOR, 0 },
1143 { GF110_DISP_CURSOR, 0 },
1144 { GT214_DISP_CURSOR, 0 },
1145 { G82_DISP_CURSOR, 0 },
1146 { NV50_DISP_CURSOR, 0 },
1147 {}
1148 };
1149 struct nv50_disp_cursor_v0 args = {
1150 .head = head->base.index,
1151 };
1152 struct nv50_disp *disp = nv50_disp(drm->dev);
1153 struct nv50_curs *curs;
1154 int cid, ret;
1155
1156 cid = nvif_mclass(disp->disp, curses);
1157 if (cid < 0) {
1158 NV_ERROR(drm, "No supported cursor immediate class\n");
1159 return cid;
1160 }
1161
1162 if (!(curs = *pcurs = kzalloc(sizeof(*curs), GFP_KERNEL)))
1163 return -ENOMEM;
1164
1165 ret = nv50_wndw_ctor(&nv50_curs, drm->dev, DRM_PLANE_TYPE_CURSOR,
1166 "curs", head->base.index, &disp->mast.base,
1167 nv50_curs_format, ARRAY_SIZE(nv50_curs_format),
1168 &curs->wndw);
1169 if (ret) {
1170 kfree(curs);
1171 return ret;
1172 }
1173
1174 ret = nvif_object_init(disp->disp, 0, curses[cid].oclass, &args,
1175 sizeof(args), &curs->chan);
1176 if (ret) {
1177 NV_ERROR(drm, "curs%04x allocation failed: %d\n",
1178 curses[cid].oclass, ret);
1179 return ret;
1180 }
1181
1182 return 0;
1183}
1184
1185/******************************************************************************
Ben Skeggs973f10c2016-11-04 17:20:36 +10001186 * Primary plane
1187 *****************************************************************************/
1188#define nv50_base(p) container_of((p), struct nv50_base, wndw)
1189
1190struct nv50_base {
1191 struct nv50_wndw wndw;
1192 struct nv50_sync chan;
1193 int id;
1194};
1195
1196static int
1197nv50_base_notify(struct nvif_notify *notify)
1198{
1199 return NVIF_NOTIFY_KEEP;
1200}
1201
1202static void
1203nv50_base_lut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1204{
1205 struct nv50_base *base = nv50_base(wndw);
1206 u32 *push;
1207 if ((push = evo_wait(&base->chan, 2))) {
1208 evo_mthd(push, 0x00e0, 1);
1209 evo_data(push, asyw->lut.enable << 30);
1210 evo_kick(push, &base->chan);
1211 }
1212}
1213
1214static void
1215nv50_base_image_clr(struct nv50_wndw *wndw)
1216{
1217 struct nv50_base *base = nv50_base(wndw);
1218 u32 *push;
1219 if ((push = evo_wait(&base->chan, 4))) {
1220 evo_mthd(push, 0x0084, 1);
1221 evo_data(push, 0x00000000);
1222 evo_mthd(push, 0x00c0, 1);
1223 evo_data(push, 0x00000000);
1224 evo_kick(push, &base->chan);
1225 }
1226}
1227
1228static void
1229nv50_base_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1230{
1231 struct nv50_base *base = nv50_base(wndw);
1232 const s32 oclass = base->chan.base.base.user.oclass;
1233 u32 *push;
1234 if ((push = evo_wait(&base->chan, 10))) {
1235 evo_mthd(push, 0x0084, 1);
1236 evo_data(push, (asyw->image.mode << 8) |
1237 (asyw->image.interval << 4));
1238 evo_mthd(push, 0x00c0, 1);
1239 evo_data(push, asyw->image.handle);
1240 if (oclass < G82_DISP_BASE_CHANNEL_DMA) {
1241 evo_mthd(push, 0x0800, 5);
1242 evo_data(push, asyw->image.offset >> 8);
1243 evo_data(push, 0x00000000);
1244 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1245 evo_data(push, (asyw->image.layout << 20) |
1246 asyw->image.pitch |
1247 asyw->image.block);
1248 evo_data(push, (asyw->image.kind << 16) |
1249 (asyw->image.format << 8));
1250 } else
1251 if (oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1252 evo_mthd(push, 0x0800, 5);
1253 evo_data(push, asyw->image.offset >> 8);
1254 evo_data(push, 0x00000000);
1255 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1256 evo_data(push, (asyw->image.layout << 20) |
1257 asyw->image.pitch |
1258 asyw->image.block);
1259 evo_data(push, asyw->image.format << 8);
1260 } else {
1261 evo_mthd(push, 0x0400, 5);
1262 evo_data(push, asyw->image.offset >> 8);
1263 evo_data(push, 0x00000000);
1264 evo_data(push, (asyw->image.h << 16) | asyw->image.w);
1265 evo_data(push, (asyw->image.layout << 24) |
1266 asyw->image.pitch |
1267 asyw->image.block);
1268 evo_data(push, asyw->image.format << 8);
1269 }
1270 evo_kick(push, &base->chan);
1271 }
1272}
1273
1274static void
1275nv50_base_ntfy_clr(struct nv50_wndw *wndw)
1276{
1277 struct nv50_base *base = nv50_base(wndw);
1278 u32 *push;
1279 if ((push = evo_wait(&base->chan, 2))) {
1280 evo_mthd(push, 0x00a4, 1);
1281 evo_data(push, 0x00000000);
1282 evo_kick(push, &base->chan);
1283 }
1284}
1285
1286static void
1287nv50_base_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1288{
1289 struct nv50_base *base = nv50_base(wndw);
1290 u32 *push;
1291 if ((push = evo_wait(&base->chan, 3))) {
1292 evo_mthd(push, 0x00a0, 2);
1293 evo_data(push, (asyw->ntfy.awaken << 30) | asyw->ntfy.offset);
1294 evo_data(push, asyw->ntfy.handle);
1295 evo_kick(push, &base->chan);
1296 }
1297}
1298
1299static void
1300nv50_base_sema_clr(struct nv50_wndw *wndw)
1301{
1302 struct nv50_base *base = nv50_base(wndw);
1303 u32 *push;
1304 if ((push = evo_wait(&base->chan, 2))) {
1305 evo_mthd(push, 0x0094, 1);
1306 evo_data(push, 0x00000000);
1307 evo_kick(push, &base->chan);
1308 }
1309}
1310
1311static void
1312nv50_base_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1313{
1314 struct nv50_base *base = nv50_base(wndw);
1315 u32 *push;
1316 if ((push = evo_wait(&base->chan, 5))) {
1317 evo_mthd(push, 0x0088, 4);
1318 evo_data(push, asyw->sema.offset);
1319 evo_data(push, asyw->sema.acquire);
1320 evo_data(push, asyw->sema.release);
1321 evo_data(push, asyw->sema.handle);
1322 evo_kick(push, &base->chan);
1323 }
1324}
1325
1326static u32
1327nv50_base_update(struct nv50_wndw *wndw, u32 interlock)
1328{
1329 struct nv50_base *base = nv50_base(wndw);
1330 u32 *push;
1331
1332 if (!(push = evo_wait(&base->chan, 2)))
1333 return 0;
1334 evo_mthd(push, 0x0080, 1);
1335 evo_data(push, interlock);
1336 evo_kick(push, &base->chan);
1337
1338 if (base->chan.base.base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA)
1339 return interlock ? 2 << (base->id * 8) : 0;
1340 return interlock ? 2 << (base->id * 4) : 0;
1341}
1342
1343static int
1344nv50_base_ntfy_wait_begun(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1345{
1346 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
1347 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1348 if (nvif_msec(&drm->device, 2000ULL,
1349 u32 data = nouveau_bo_rd32(disp->sync, asyw->ntfy.offset / 4);
1350 if ((data & 0xc0000000) == 0x40000000)
1351 break;
1352 usleep_range(1, 2);
1353 ) < 0)
1354 return -ETIMEDOUT;
1355 return 0;
1356}
1357
1358static void
1359nv50_base_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1360 struct nv50_head_atom *asyh)
1361{
1362 asyh->base.cpp = 0;
1363}
1364
1365static int
1366nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1367 struct nv50_head_atom *asyh)
1368{
1369 const u32 format = asyw->state.fb->pixel_format;
1370 const struct drm_format_info *info;
1371 int ret;
1372
1373 info = drm_format_info(format);
1374 if (!info || !info->depth)
1375 return -EINVAL;
1376
1377 ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
1378 DRM_PLANE_HELPER_NO_SCALING,
1379 DRM_PLANE_HELPER_NO_SCALING,
1380 false, true);
1381 if (ret)
1382 return ret;
1383
1384 asyh->base.depth = info->depth;
1385 asyh->base.cpp = info->cpp[0];
1386 asyh->base.x = asyw->state.src.x1 >> 16;
1387 asyh->base.y = asyw->state.src.y1 >> 16;
1388 asyh->base.w = asyw->state.fb->width;
1389 asyh->base.h = asyw->state.fb->height;
1390
1391 switch (format) {
1392 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
1393 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
1394 case DRM_FORMAT_XRGB1555 :
1395 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
1396 case DRM_FORMAT_XRGB8888 :
1397 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
1398 case DRM_FORMAT_XBGR2101010:
1399 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
1400 case DRM_FORMAT_XBGR8888 :
1401 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
1402 default:
1403 WARN_ON(1);
1404 return -EINVAL;
1405 }
1406
1407 asyw->lut.enable = 1;
1408 asyw->set.image = true;
1409 return 0;
1410}
1411
1412static void *
1413nv50_base_dtor(struct nv50_wndw *wndw)
1414{
1415 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
1416 struct nv50_base *base = nv50_base(wndw);
1417 nv50_dmac_destroy(&base->chan.base, disp->disp);
1418 return base;
1419}
1420
1421static const u32
1422nv50_base_format[] = {
1423 DRM_FORMAT_C8,
1424 DRM_FORMAT_RGB565,
1425 DRM_FORMAT_XRGB1555,
1426 DRM_FORMAT_ARGB1555,
1427 DRM_FORMAT_XRGB8888,
1428 DRM_FORMAT_ARGB8888,
1429 DRM_FORMAT_XBGR2101010,
1430 DRM_FORMAT_ABGR2101010,
1431 DRM_FORMAT_XBGR8888,
1432 DRM_FORMAT_ABGR8888,
1433};
1434
1435static const struct nv50_wndw_func
1436nv50_base = {
1437 .dtor = nv50_base_dtor,
1438 .acquire = nv50_base_acquire,
1439 .release = nv50_base_release,
1440 .sema_set = nv50_base_sema_set,
1441 .sema_clr = nv50_base_sema_clr,
1442 .ntfy_set = nv50_base_ntfy_set,
1443 .ntfy_clr = nv50_base_ntfy_clr,
1444 .ntfy_wait_begun = nv50_base_ntfy_wait_begun,
1445 .image_set = nv50_base_image_set,
1446 .image_clr = nv50_base_image_clr,
1447 .lut = nv50_base_lut,
1448 .update = nv50_base_update,
1449};
1450
1451static int
1452nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
1453 struct nv50_base **pbase)
1454{
1455 struct nv50_disp *disp = nv50_disp(drm->dev);
1456 struct nv50_base *base;
1457 int ret;
1458
1459 if (!(base = *pbase = kzalloc(sizeof(*base), GFP_KERNEL)))
1460 return -ENOMEM;
1461 base->id = head->base.index;
1462 base->wndw.ntfy = EVO_FLIP_NTFY0(base->id);
1463 base->wndw.sema = EVO_FLIP_SEM0(base->id);
1464 base->wndw.data = 0x00000000;
1465
1466 ret = nv50_wndw_ctor(&nv50_base, drm->dev, DRM_PLANE_TYPE_PRIMARY,
1467 "base", base->id, &base->chan.base,
1468 nv50_base_format, ARRAY_SIZE(nv50_base_format),
1469 &base->wndw);
1470 if (ret) {
1471 kfree(base);
1472 return ret;
1473 }
1474
1475 ret = nv50_base_create(&drm->device, disp->disp, base->id,
1476 disp->sync->bo.offset, &base->chan);
1477 if (ret)
1478 return ret;
1479
1480 return nvif_notify_init(&base->chan.base.base.user, nv50_base_notify,
1481 false,
1482 NV50_DISP_BASE_CHANNEL_DMA_V0_NTFY_UEVENT,
1483 &(struct nvif_notify_uevent_req) {},
1484 sizeof(struct nvif_notify_uevent_req),
1485 sizeof(struct nvif_notify_uevent_rep),
1486 &base->wndw.notify);
1487}
1488
1489/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001490 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +10001491 *****************************************************************************/
1492struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +10001493nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +10001494{
Ben Skeggse225f442012-11-21 14:40:21 +10001495 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001496}
1497
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001498struct nv50_display_flip {
1499 struct nv50_disp *disp;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001500 struct nv50_base *base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001501};
1502
1503static bool
1504nv50_display_flip_wait(void *data)
1505{
1506 struct nv50_display_flip *flip = data;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001507 if (nouveau_bo_rd32(flip->disp->sync, flip->base->wndw.sema / 4) ==
1508 flip->base->wndw.data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001509 return true;
1510 usleep_range(1, 2);
1511 return false;
1512}
1513
Ben Skeggs3376ee32011-11-12 14:28:12 +10001514void
Ben Skeggse225f442012-11-21 14:40:21 +10001515nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +10001516{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001517 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs973f10c2016-11-04 17:20:36 +10001518 struct nv50_base *base = nv50_head(crtc)->_base;
1519 struct nv50_wndw *wndw = &base->wndw;
1520 struct nv50_wndw_atom *asyw = &wndw->asy;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001521 struct nv50_display_flip flip = {
1522 .disp = nv50_disp(crtc->dev),
Ben Skeggs973f10c2016-11-04 17:20:36 +10001523 .base = base,
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001524 };
Ben Skeggs3376ee32011-11-12 14:28:12 +10001525
Ben Skeggs973f10c2016-11-04 17:20:36 +10001526 asyw->state.crtc = NULL;
1527 asyw->state.fb = NULL;
1528 nv50_wndw_atomic_check(&wndw->plane, &asyw->state);
1529 nv50_wndw_flush_clr(wndw, 0, true, asyw);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001530
Ben Skeggs54442042015-08-20 14:54:11 +10001531 nvif_msec(device, 2000,
1532 if (nv50_display_flip_wait(&flip))
1533 break;
1534 );
Ben Skeggs3376ee32011-11-12 14:28:12 +10001535}
1536
1537int
Ben Skeggse225f442012-11-21 14:40:21 +10001538nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001539 struct nouveau_channel *chan, u32 swap_interval)
1540{
1541 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001542 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001543 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001544 struct nv50_base *base = nv50_head(crtc)->_base;
1545 struct nv50_wndw *wndw = &base->wndw;
1546 struct nv50_wndw_atom *asyw = &wndw->asy;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001547 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001548
Ben Skeggs9ba83102014-12-22 19:50:23 +10001549 if (crtc->primary->fb->width != fb->width ||
1550 crtc->primary->fb->height != fb->height)
1551 return -EINVAL;
1552
Ben Skeggsf60b6e72013-03-19 15:20:00 +10001553 if (chan == NULL)
1554 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001555
Ben Skeggsa01ca782015-08-20 14:54:15 +10001556 if (chan && chan->user.oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001557 ret = RING_SPACE(chan, 8);
1558 if (ret)
1559 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +10001560
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001561 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001562 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001563 OUT_RING (chan, base->wndw.sema ^ 0x10);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001564 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001565 OUT_RING (chan, base->wndw.data + 1);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001566 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
Ben Skeggs973f10c2016-11-04 17:20:36 +10001567 OUT_RING (chan, base->wndw.sema);
1568 OUT_RING (chan, base->wndw.data);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001569 } else
Ben Skeggsa01ca782015-08-20 14:54:15 +10001570 if (chan && chan->user.oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001571 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + base->wndw.sema;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001572 ret = RING_SPACE(chan, 12);
1573 if (ret)
1574 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +10001575
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001576 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001577 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001578 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1579 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
1580 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001581 OUT_RING (chan, base->wndw.data + 1);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001582 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
1583 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1584 OUT_RING (chan, upper_32_bits(addr));
1585 OUT_RING (chan, lower_32_bits(addr));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001586 OUT_RING (chan, base->wndw.data);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001587 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
1588 } else
1589 if (chan) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001590 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + base->wndw.sema;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001591 ret = RING_SPACE(chan, 10);
1592 if (ret)
1593 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +10001594
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001595 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1596 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
1597 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001598 OUT_RING (chan, base->wndw.data + 1);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001599 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
1600 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
1601 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
1602 OUT_RING (chan, upper_32_bits(addr));
1603 OUT_RING (chan, lower_32_bits(addr));
Ben Skeggs973f10c2016-11-04 17:20:36 +10001604 OUT_RING (chan, base->wndw.data);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001605 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
1606 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
1607 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -05001608
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001609 if (chan) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10001610 base->wndw.sema ^= 0x10;
1611 base->wndw.data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001612 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001613 }
1614
1615 /* queue the flip */
Ben Skeggs973f10c2016-11-04 17:20:36 +10001616 asyw->state.crtc = &head->base.base;
1617 asyw->state.fb = fb;
1618 asyw->interval = swap_interval;
1619 asyw->image.handle = nv_fb->r_handle;
1620 asyw->image.offset = nv_fb->nvbo->bo.offset;
1621 asyw->sema.handle = base->chan.base.sync.handle;
1622 asyw->sema.offset = base->wndw.sema;
1623 asyw->sema.acquire = base->wndw.data++;
1624 asyw->sema.release = base->wndw.data;
1625 nv50_wndw_atomic_check(&wndw->plane, &asyw->state);
1626 asyw->set.sema = true;
1627 nv50_wndw_flush_set(wndw, 0, asyw);
1628 nv50_wndw_wait_armed(wndw, asyw);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001629
1630 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001631 return 0;
1632}
1633
Ben Skeggs26f6d882011-07-04 16:25:18 +10001634/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001635 * Head
1636 *****************************************************************************/
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001637static void
Ben Skeggs7e08d672016-11-04 17:20:36 +10001638nv50_head_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
1639{
1640 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1641 u32 *push;
1642 if ((push = evo_wait(core, 2))) {
1643 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1644 evo_mthd(push, 0x08a8 + (head->base.index * 0x400), 1);
1645 else
1646 evo_mthd(push, 0x0498 + (head->base.index * 0x300), 1);
1647 evo_data(push, (asyh->procamp.sat.sin << 20) |
1648 (asyh->procamp.sat.cos << 8));
1649 evo_kick(push, core);
1650 }
1651}
1652
1653static void
Ben Skeggs7e918332016-11-04 17:20:36 +10001654nv50_head_dither(struct nv50_head *head, struct nv50_head_atom *asyh)
1655{
1656 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1657 u32 *push;
1658 if ((push = evo_wait(core, 2))) {
1659 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1660 evo_mthd(push, 0x08a0 + (head->base.index * 0x0400), 1);
1661 else
1662 if (core->base.user.oclass < GK104_DISP_CORE_CHANNEL_DMA)
1663 evo_mthd(push, 0x0490 + (head->base.index * 0x0300), 1);
1664 else
1665 evo_mthd(push, 0x04a0 + (head->base.index * 0x0300), 1);
1666 evo_data(push, (asyh->dither.mode << 3) |
1667 (asyh->dither.bits << 1) |
1668 asyh->dither.enable);
1669 evo_kick(push, core);
1670 }
1671}
1672
1673static void
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001674nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
1675{
1676 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1677 u32 bounds = 0;
1678 u32 *push;
1679
1680 if (asyh->base.cpp) {
1681 switch (asyh->base.cpp) {
1682 case 8: bounds |= 0x00000500; break;
1683 case 4: bounds |= 0x00000300; break;
1684 case 2: bounds |= 0x00000100; break;
1685 default:
1686 WARN_ON(1);
1687 break;
1688 }
1689 bounds |= 0x00000001;
1690 }
1691
1692 if ((push = evo_wait(core, 2))) {
1693 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1694 evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
1695 else
1696 evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
1697 evo_data(push, bounds);
1698 evo_kick(push, core);
1699 }
1700}
1701
1702static void
1703nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
1704{
1705 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1706 u32 bounds = 0;
1707 u32 *push;
1708
1709 if (asyh->base.cpp) {
1710 switch (asyh->base.cpp) {
1711 case 8: bounds |= 0x00000500; break;
1712 case 4: bounds |= 0x00000300; break;
1713 case 2: bounds |= 0x00000100; break;
1714 case 1: bounds |= 0x00000000; break;
1715 default:
1716 WARN_ON(1);
1717 break;
1718 }
1719 bounds |= 0x00000001;
1720 }
1721
1722 if ((push = evo_wait(core, 2))) {
1723 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1724 evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
1725 else
1726 evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
1727 evo_data(push, bounds);
1728 evo_kick(push, core);
1729 }
1730}
1731
1732static void
Ben Skeggsea8ee392016-11-04 17:20:36 +10001733nv50_head_curs_clr(struct nv50_head *head)
1734{
1735 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1736 u32 *push;
1737 if ((push = evo_wait(core, 4))) {
1738 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1739 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1740 evo_data(push, 0x05000000);
1741 } else
1742 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1743 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
1744 evo_data(push, 0x05000000);
1745 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1746 evo_data(push, 0x00000000);
1747 } else {
1748 evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
1749 evo_data(push, 0x05000000);
1750 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1751 evo_data(push, 0x00000000);
1752 }
1753 evo_kick(push, core);
1754 }
1755}
1756
1757static void
1758nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1759{
1760 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1761 u32 *push;
1762 if ((push = evo_wait(core, 5))) {
1763 if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
1764 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1765 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1766 (asyh->curs.format << 24));
1767 evo_data(push, asyh->curs.offset >> 8);
1768 } else
1769 if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
1770 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
1771 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1772 (asyh->curs.format << 24));
1773 evo_data(push, asyh->curs.offset >> 8);
1774 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
1775 evo_data(push, asyh->curs.handle);
1776 } else {
1777 evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
1778 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
1779 (asyh->curs.format << 24));
1780 evo_data(push, asyh->curs.offset >> 8);
1781 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
1782 evo_data(push, asyh->curs.handle);
1783 }
1784 evo_kick(push, core);
1785 }
1786}
1787
1788static void
Ben Skeggsad633612016-11-04 17:20:36 +10001789nv50_head_core_clr(struct nv50_head *head)
1790{
1791 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1792 u32 *push;
1793 if ((push = evo_wait(core, 2))) {
1794 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
1795 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
1796 else
1797 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
1798 evo_data(push, 0x00000000);
1799 evo_kick(push, core);
1800 }
1801}
1802
1803static void
1804nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1805{
1806 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1807 u32 *push;
1808 if ((push = evo_wait(core, 9))) {
1809 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1810 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1811 evo_data(push, asyh->core.offset >> 8);
1812 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1813 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1814 evo_data(push, asyh->core.layout << 20 |
1815 (asyh->core.pitch >> 8) << 8 |
1816 asyh->core.block);
1817 evo_data(push, asyh->core.kind << 16 |
1818 asyh->core.format << 8);
1819 evo_data(push, asyh->core.handle);
1820 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1821 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1822 } else
1823 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1824 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
1825 evo_data(push, asyh->core.offset >> 8);
1826 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
1827 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1828 evo_data(push, asyh->core.layout << 20 |
1829 (asyh->core.pitch >> 8) << 8 |
1830 asyh->core.block);
1831 evo_data(push, asyh->core.format << 8);
1832 evo_data(push, asyh->core.handle);
1833 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
1834 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1835 } else {
1836 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
1837 evo_data(push, asyh->core.offset >> 8);
1838 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
1839 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
1840 evo_data(push, asyh->core.layout << 24 |
1841 (asyh->core.pitch >> 8) << 8 |
1842 asyh->core.block);
1843 evo_data(push, asyh->core.format << 8);
1844 evo_data(push, asyh->core.handle);
1845 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
1846 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
1847 }
1848 evo_kick(push, core);
1849 }
1850}
1851
1852static void
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001853nv50_head_lut_clr(struct nv50_head *head)
1854{
1855 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1856 u32 *push;
1857 if ((push = evo_wait(core, 4))) {
1858 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1859 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1860 evo_data(push, 0x40000000);
1861 } else
1862 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1863 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
1864 evo_data(push, 0x40000000);
1865 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1866 evo_data(push, 0x00000000);
1867 } else {
1868 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
1869 evo_data(push, 0x03000000);
1870 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1871 evo_data(push, 0x00000000);
1872 }
1873 evo_kick(push, core);
1874 }
1875}
1876
1877static void
1878nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1879{
1880 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1881 u32 *push;
1882 if ((push = evo_wait(core, 7))) {
1883 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1884 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1885 evo_data(push, 0xc0000000);
1886 evo_data(push, asyh->lut.offset >> 8);
1887 } else
1888 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1889 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1890 evo_data(push, 0xc0000000);
1891 evo_data(push, asyh->lut.offset >> 8);
1892 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1893 evo_data(push, asyh->lut.handle);
1894 } else {
1895 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
1896 evo_data(push, 0x83000000);
1897 evo_data(push, asyh->lut.offset >> 8);
1898 evo_data(push, 0x00000000);
1899 evo_data(push, 0x00000000);
1900 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1901 evo_data(push, asyh->lut.handle);
1902 }
1903 evo_kick(push, core);
1904 }
1905}
1906
1907static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001908nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1909{
1910 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1911 struct nv50_head_mode *m = &asyh->mode;
1912 u32 *push;
1913 if ((push = evo_wait(core, 14))) {
1914 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1915 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
1916 evo_data(push, 0x00800000 | m->clock);
1917 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001918 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 7);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001919 evo_data(push, 0x00000000);
1920 evo_data(push, (m->v.active << 16) | m->h.active );
1921 evo_data(push, (m->v.synce << 16) | m->h.synce );
1922 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1923 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1924 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
Ben Skeggs06ab2822016-11-04 17:20:36 +10001925 evo_data(push, asyh->mode.v.blankus);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001926 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
1927 evo_data(push, 0x00000000);
1928 } else {
1929 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
1930 evo_data(push, 0x00000000);
1931 evo_data(push, (m->v.active << 16) | m->h.active );
1932 evo_data(push, (m->v.synce << 16) | m->h.synce );
1933 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1934 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1935 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
1936 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
1937 evo_data(push, 0x00000000); /* ??? */
1938 evo_data(push, 0xffffff00);
1939 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
1940 evo_data(push, m->clock * 1000);
1941 evo_data(push, 0x00200000); /* ??? */
1942 evo_data(push, m->clock * 1000);
1943 }
1944 evo_kick(push, core);
1945 }
1946}
1947
1948static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001949nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
1950{
1951 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1952 u32 *push;
1953 if ((push = evo_wait(core, 10))) {
1954 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1955 evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
1956 evo_data(push, 0x00000000);
1957 evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
1958 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1959 evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
1960 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1961 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1962 } else {
1963 evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
1964 evo_data(push, 0x00000000);
1965 evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
1966 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1967 evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
1968 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1969 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1970 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1971 }
1972 evo_kick(push, core);
1973 }
1974}
1975
1976static void
Ben Skeggsad633612016-11-04 17:20:36 +10001977nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
1978{
1979 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001980 nv50_head_lut_clr(head);
1981 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsad633612016-11-04 17:20:36 +10001982 nv50_head_core_clr(head);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001983 if (asyh->clr.curs && (!asyh->set.curs || y))
1984 nv50_head_curs_clr(head);
Ben Skeggsad633612016-11-04 17:20:36 +10001985}
1986
1987static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001988nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1989{
Ben Skeggsc4e68122016-11-04 17:20:36 +10001990 if (asyh->set.view ) nv50_head_view (head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001991 if (asyh->set.mode ) nv50_head_mode (head, asyh);
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001992 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10001993 if (asyh->set.core ) nv50_head_core_set(head, asyh);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001994 if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001995 if (asyh->set.base ) nv50_head_base (head, asyh);
1996 if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
Ben Skeggs7e918332016-11-04 17:20:36 +10001997 if (asyh->set.dither ) nv50_head_dither (head, asyh);
Ben Skeggs7e08d672016-11-04 17:20:36 +10001998 if (asyh->set.procamp) nv50_head_procamp (head, asyh);
1999}
2000
2001static void
2002nv50_head_atomic_check_procamp(struct nv50_head_atom *armh,
2003 struct nv50_head_atom *asyh,
2004 struct nouveau_conn_atom *asyc)
2005{
2006 const int vib = asyc->procamp.color_vibrance - 100;
2007 const int hue = asyc->procamp.vibrant_hue - 90;
2008 const int adj = (vib > 0) ? 50 : 0;
2009 asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff;
2010 asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff;
2011 asyh->set.procamp = true;
Ben Skeggs7e918332016-11-04 17:20:36 +10002012}
2013
2014static void
2015nv50_head_atomic_check_dither(struct nv50_head_atom *armh,
2016 struct nv50_head_atom *asyh,
2017 struct nouveau_conn_atom *asyc)
2018{
2019 struct drm_connector *connector = asyc->state.connector;
2020 u32 mode = 0x00;
2021
2022 if (asyc->dither.mode == DITHERING_MODE_AUTO) {
2023 if (asyh->base.depth > connector->display_info.bpc * 3)
2024 mode = DITHERING_MODE_DYNAMIC2X2;
2025 } else {
2026 mode = asyc->dither.mode;
2027 }
2028
2029 if (asyc->dither.depth == DITHERING_DEPTH_AUTO) {
2030 if (connector->display_info.bpc >= 8)
2031 mode |= DITHERING_DEPTH_8BPC;
2032 } else {
2033 mode |= asyc->dither.depth;
2034 }
2035
2036 asyh->dither.enable = mode;
2037 asyh->dither.bits = mode >> 1;
2038 asyh->dither.mode = mode >> 3;
2039 asyh->set.dither = true;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002040}
2041
2042static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10002043nv50_head_atomic_check_view(struct nv50_head_atom *armh,
2044 struct nv50_head_atom *asyh,
2045 struct nouveau_conn_atom *asyc)
2046{
2047 struct drm_connector *connector = asyc->state.connector;
2048 struct drm_display_mode *omode = &asyh->state.adjusted_mode;
2049 struct drm_display_mode *umode = &asyh->state.mode;
2050 int mode = asyc->scaler.mode;
2051 struct edid *edid;
2052
2053 if (connector->edid_blob_ptr)
2054 edid = (struct edid *)connector->edid_blob_ptr->data;
2055 else
2056 edid = NULL;
2057
2058 if (!asyc->scaler.full) {
2059 if (mode == DRM_MODE_SCALE_NONE)
2060 omode = umode;
2061 } else {
2062 /* Non-EDID LVDS/eDP mode. */
2063 mode = DRM_MODE_SCALE_FULLSCREEN;
2064 }
2065
2066 asyh->view.iW = umode->hdisplay;
2067 asyh->view.iH = umode->vdisplay;
2068 asyh->view.oW = omode->hdisplay;
2069 asyh->view.oH = omode->vdisplay;
2070 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
2071 asyh->view.oH *= 2;
2072
2073 /* Add overscan compensation if necessary, will keep the aspect
2074 * ratio the same as the backend mode unless overridden by the
2075 * user setting both hborder and vborder properties.
2076 */
2077 if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
2078 (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
2079 drm_detect_hdmi_monitor(edid)))) {
2080 u32 bX = asyc->scaler.underscan.hborder;
2081 u32 bY = asyc->scaler.underscan.vborder;
2082 u32 r = (asyh->view.oH << 19) / asyh->view.oW;
2083
2084 if (bX) {
2085 asyh->view.oW -= (bX * 2);
2086 if (bY) asyh->view.oH -= (bY * 2);
2087 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2088 } else {
2089 asyh->view.oW -= (asyh->view.oW >> 4) + 32;
2090 if (bY) asyh->view.oH -= (bY * 2);
2091 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2092 }
2093 }
2094
2095 /* Handle CENTER/ASPECT scaling, taking into account the areas
2096 * removed already for overscan compensation.
2097 */
2098 switch (mode) {
2099 case DRM_MODE_SCALE_CENTER:
2100 asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
2101 asyh->view.oH = min((u16)umode->vdisplay, asyh->view.oH);
2102 /* fall-through */
2103 case DRM_MODE_SCALE_ASPECT:
2104 if (asyh->view.oH < asyh->view.oW) {
2105 u32 r = (asyh->view.iW << 19) / asyh->view.iH;
2106 asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
2107 } else {
2108 u32 r = (asyh->view.iH << 19) / asyh->view.iW;
2109 asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
2110 }
2111 break;
2112 default:
2113 break;
2114 }
2115
2116 asyh->set.view = true;
2117}
2118
2119static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002120nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
2121{
2122 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
2123 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
2124 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
2125 u32 hbackp = mode->htotal - mode->hsync_end;
2126 u32 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
2127 u32 hfrontp = mode->hsync_start - mode->hdisplay;
2128 u32 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
2129 struct nv50_head_mode *m = &asyh->mode;
2130
2131 m->h.active = mode->htotal;
2132 m->h.synce = mode->hsync_end - mode->hsync_start - 1;
2133 m->h.blanke = m->h.synce + hbackp;
2134 m->h.blanks = mode->htotal - hfrontp - 1;
2135
2136 m->v.active = mode->vtotal * vscan / ilace;
2137 m->v.synce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
2138 m->v.blanke = m->v.synce + vbackp;
2139 m->v.blanks = m->v.active - vfrontp - 1;
2140
2141 /*XXX: Safe underestimate, even "0" works */
2142 m->v.blankus = (m->v.active - mode->vdisplay - 2) * m->h.active;
2143 m->v.blankus *= 1000;
2144 m->v.blankus /= mode->clock;
2145
2146 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
2147 m->v.blank2e = m->v.active + m->v.synce + vbackp;
2148 m->v.blank2s = m->v.blank2e + (mode->vdisplay * vscan / ilace);
2149 m->v.active = (m->v.active * 2) + 1;
2150 m->interlace = true;
2151 } else {
2152 m->v.blank2e = 0;
2153 m->v.blank2s = 1;
2154 m->interlace = false;
2155 }
2156 m->clock = mode->clock;
2157
2158 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2159 asyh->set.mode = true;
2160}
2161
2162static int
2163nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
2164{
2165 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggsad633612016-11-04 17:20:36 +10002166 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002167 struct nv50_head *head = nv50_head(crtc);
2168 struct nv50_head_atom *armh = &head->arm;
2169 struct nv50_head_atom *asyh = nv50_head_atom(state);
2170
2171 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
Ben Skeggsad633612016-11-04 17:20:36 +10002172 asyh->clr.mask = 0;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002173 asyh->set.mask = 0;
2174
2175 if (asyh->state.active) {
2176 if (asyh->state.mode_changed)
2177 nv50_head_atomic_check_mode(head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10002178
2179 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
2180 asyh->core.x = asyh->base.x;
2181 asyh->core.y = asyh->base.y;
2182 asyh->core.w = asyh->base.w;
2183 asyh->core.h = asyh->base.h;
2184 } else
Ben Skeggsea8ee392016-11-04 17:20:36 +10002185 if ((asyh->core.visible = asyh->curs.visible)) {
Ben Skeggsad633612016-11-04 17:20:36 +10002186 /*XXX: We need to either find some way of having the
2187 * primary base layer appear black, while still
2188 * being able to display the other layers, or we
2189 * need to allocate a dummy black surface here.
2190 */
2191 asyh->core.x = 0;
2192 asyh->core.y = 0;
2193 asyh->core.w = asyh->state.mode.hdisplay;
2194 asyh->core.h = asyh->state.mode.vdisplay;
2195 }
2196 asyh->core.handle = disp->mast.base.vram.handle;
2197 asyh->core.offset = 0;
2198 asyh->core.format = 0xcf;
2199 asyh->core.kind = 0;
2200 asyh->core.layout = 1;
2201 asyh->core.block = 0;
2202 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002203 asyh->lut.handle = disp->mast.base.vram.handle;
2204 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002205 asyh->set.base = armh->base.cpp != asyh->base.cpp;
2206 asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
Ben Skeggsad633612016-11-04 17:20:36 +10002207 } else {
2208 asyh->core.visible = false;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002209 asyh->curs.visible = false;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10002210 asyh->base.cpp = 0;
2211 asyh->ovly.cpp = 0;
Ben Skeggsad633612016-11-04 17:20:36 +10002212 }
2213
2214 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
2215 if (asyh->core.visible) {
2216 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
2217 asyh->set.core = true;
2218 } else
2219 if (armh->core.visible) {
2220 asyh->clr.core = true;
2221 }
Ben Skeggsea8ee392016-11-04 17:20:36 +10002222
2223 if (asyh->curs.visible) {
2224 if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
2225 asyh->set.curs = true;
2226 } else
2227 if (armh->curs.visible) {
2228 asyh->clr.curs = true;
2229 }
Ben Skeggsad633612016-11-04 17:20:36 +10002230 } else {
2231 asyh->clr.core = armh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002232 asyh->clr.curs = armh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10002233 asyh->set.core = asyh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10002234 asyh->set.curs = asyh->curs.visible;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002235 }
2236
2237 memcpy(armh, asyh, sizeof(*asyh));
2238 asyh->state.mode_changed = 0;
2239 return 0;
2240}
2241
2242/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +10002243 * CRTC
2244 *****************************************************************************/
2245static int
Ben Skeggse225f442012-11-21 14:40:21 +10002246nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002247{
Ben Skeggse225f442012-11-21 14:40:21 +10002248 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs7e918332016-11-04 17:20:36 +10002249 struct nv50_head *head = nv50_head(&nv_crtc->base);
2250 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggsde691852011-10-17 12:23:41 +10002251 struct nouveau_connector *nv_connector;
Ben Skeggs7e918332016-11-04 17:20:36 +10002252 struct nouveau_conn_atom asyc;
2253 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002254
Ben Skeggs488ff202011-10-17 10:38:10 +10002255 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +10002256
Ben Skeggs7e918332016-11-04 17:20:36 +10002257 asyc.state.connector = &nv_connector->base;
2258 asyc.dither.mode = nv_connector->dithering_mode;
2259 asyc.dither.depth = nv_connector->dithering_depth;
2260 asyh->state.crtc = &nv_crtc->base;
2261 nv50_head_atomic_check(&head->base.base, &asyh->state);
2262 nv50_head_atomic_check_dither(&head->arm, asyh, &asyc);
2263 nv50_head_flush_set(head, asyh);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002264
Ben Skeggs7e918332016-11-04 17:20:36 +10002265 if (update) {
2266 if ((push = evo_wait(mast, 2))) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10002267 evo_mthd(push, 0x0080, 1);
2268 evo_data(push, 0x00000000);
Ben Skeggs7e918332016-11-04 17:20:36 +10002269 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002270 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002271 }
2272
2273 return 0;
2274}
2275
2276static int
Ben Skeggse225f442012-11-21 14:40:21 +10002277nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002278{
Ben Skeggsc4e68122016-11-04 17:20:36 +10002279 struct nv50_head *head = nv50_head(&nv_crtc->base);
2280 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs3376ee32011-11-12 14:28:12 +10002281 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +10002282 struct nouveau_connector *nv_connector;
Ben Skeggsc4e68122016-11-04 17:20:36 +10002283 struct nouveau_conn_atom asyc;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002284
Ben Skeggsf3fdc522011-07-07 16:01:57 +10002285 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsf3fdc522011-07-07 16:01:57 +10002286
Ben Skeggsc4e68122016-11-04 17:20:36 +10002287 asyc.state.connector = &nv_connector->base;
2288 asyc.scaler.mode = nv_connector->scaling_mode;
2289 asyc.scaler.full = nv_connector->scaling_full;
2290 asyc.scaler.underscan.mode = nv_connector->underscan;
2291 asyc.scaler.underscan.hborder = nv_connector->underscan_hborder;
2292 asyc.scaler.underscan.vborder = nv_connector->underscan_vborder;
2293 nv50_head_atomic_check(&head->base.base, &asyh->state);
2294 nv50_head_atomic_check_view(&head->arm, asyh, &asyc);
2295 nv50_head_flush_set(head, asyh);
Ben Skeggs92854622011-11-11 23:49:06 +10002296
Ben Skeggsc4e68122016-11-04 17:20:36 +10002297 if (update) {
2298 nv50_display_flip_stop(crtc);
2299 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002300 }
2301
2302 return 0;
2303}
2304
2305static int
Ben Skeggse225f442012-11-21 14:40:21 +10002306nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +10002307{
Ben Skeggse225f442012-11-21 14:40:21 +10002308 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs7e08d672016-11-04 17:20:36 +10002309 struct nv50_head *head = nv50_head(&nv_crtc->base);
2310 struct nv50_head_atom *asyh = &head->asy;
2311 struct nouveau_conn_atom asyc;
2312 u32 *push;
Ben Skeggsf9887d02012-11-21 13:03:42 +10002313
Ben Skeggs7e08d672016-11-04 17:20:36 +10002314 asyc.procamp.color_vibrance = nv_crtc->color_vibrance + 100;
2315 asyc.procamp.vibrant_hue = nv_crtc->vibrant_hue + 90;
2316 nv50_head_atomic_check(&head->base.base, &asyh->state);
2317 nv50_head_atomic_check_procamp(&head->arm, asyh, &asyc);
2318 nv50_head_flush_set(head, asyh);
Ben Skeggsf9887d02012-11-21 13:03:42 +10002319
Ben Skeggs7e08d672016-11-04 17:20:36 +10002320 if (update) {
2321 if ((push = evo_wait(mast, 2))) {
Ben Skeggsf9887d02012-11-21 13:03:42 +10002322 evo_mthd(push, 0x0080, 1);
2323 evo_data(push, 0x00000000);
Ben Skeggs7e08d672016-11-04 17:20:36 +10002324 evo_kick(push, mast);
Ben Skeggsf9887d02012-11-21 13:03:42 +10002325 }
Ben Skeggsf9887d02012-11-21 13:03:42 +10002326 }
2327
2328 return 0;
2329}
2330
2331static int
Ben Skeggse225f442012-11-21 14:40:21 +10002332nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002333 int x, int y, bool update)
2334{
2335 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggsad633612016-11-04 17:20:36 +10002336 struct nv50_head *head = nv50_head(&nv_crtc->base);
2337 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002338 struct nv50_wndw_atom *asyw = &head->_base->wndw.asy;
Ben Skeggsad633612016-11-04 17:20:36 +10002339 const struct drm_format_info *info;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002340
Ben Skeggsad633612016-11-04 17:20:36 +10002341 info = drm_format_info(nvfb->base.pixel_format);
2342 if (!info || !info->depth)
2343 return -EINVAL;
Ben Skeggsde8268c2012-11-16 10:24:31 +10002344
Ben Skeggsad633612016-11-04 17:20:36 +10002345 asyh->base.depth = info->depth;
2346 asyh->base.cpp = info->cpp[0];
2347 asyh->base.x = x;
2348 asyh->base.y = y;
2349 asyh->base.w = nvfb->base.width;
2350 asyh->base.h = nvfb->base.height;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002351 asyw->state.src_x = x << 16;
2352 asyw->state.src_y = y << 16;
Ben Skeggsad633612016-11-04 17:20:36 +10002353 nv50_head_atomic_check(&head->base.base, &asyh->state);
2354 nv50_head_flush_set(head, asyh);
2355
2356 if (update) {
2357 struct nv50_mast *core = nv50_mast(nv_crtc->base.dev);
2358 u32 *push = evo_wait(core, 2);
2359 if (push) {
Ben Skeggsa46232e2011-07-07 15:23:48 +10002360 evo_mthd(push, 0x0080, 1);
2361 evo_data(push, 0x00000000);
Ben Skeggsad633612016-11-04 17:20:36 +10002362 evo_kick(push, core);
Ben Skeggsa46232e2011-07-07 15:23:48 +10002363 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002364 }
2365
Ben Skeggs8a423642014-08-10 04:10:19 +10002366 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002367 return 0;
2368}
2369
2370static void
Ben Skeggse225f442012-11-21 14:40:21 +10002371nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002372{
Ben Skeggse225f442012-11-21 14:40:21 +10002373 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsea8ee392016-11-04 17:20:36 +10002374 struct nv50_head *head = nv50_head(&nv_crtc->base);
2375 struct nv50_head_atom *asyh = &head->asy;
2376
2377 asyh->curs.visible = true;
2378 asyh->curs.handle = mast->base.vram.handle;
2379 asyh->curs.offset = nv_crtc->cursor.nvbo->bo.offset;
2380 asyh->curs.layout = 1;
2381 asyh->curs.format = 1;
2382 nv50_head_atomic_check(&head->base.base, &asyh->state);
2383 nv50_head_flush_set(head, asyh);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002384}
2385
2386static void
Ben Skeggse225f442012-11-21 14:40:21 +10002387nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +10002388{
Ben Skeggsea8ee392016-11-04 17:20:36 +10002389 struct nv50_head *head = nv50_head(&nv_crtc->base);
2390 struct nv50_head_atom *asyh = &head->asy;
2391
2392 asyh->curs.visible = false;
2393 nv50_head_atomic_check(&head->base.base, &asyh->state);
2394 nv50_head_flush_clr(head, asyh, false);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002395}
Ben Skeggs438d99e2011-07-05 16:48:06 +10002396
Ben Skeggsde8268c2012-11-16 10:24:31 +10002397static void
Ben Skeggse225f442012-11-21 14:40:21 +10002398nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +10002399{
Ben Skeggse225f442012-11-21 14:40:21 +10002400 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002401
Ben Skeggs697bb722015-07-28 17:20:57 +10002402 if (show && nv_crtc->cursor.nvbo && nv_crtc->base.enabled)
Ben Skeggse225f442012-11-21 14:40:21 +10002403 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002404 else
Ben Skeggse225f442012-11-21 14:40:21 +10002405 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002406
2407 if (update) {
2408 u32 *push = evo_wait(mast, 2);
2409 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10002410 evo_mthd(push, 0x0080, 1);
2411 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +10002412 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002413 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002414 }
2415}
2416
2417static void
Ben Skeggse225f442012-11-21 14:40:21 +10002418nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002419{
2420}
2421
2422static void
Ben Skeggse225f442012-11-21 14:40:21 +10002423nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002424{
Ben Skeggsad633612016-11-04 17:20:36 +10002425 struct nv50_head *head = nv50_head(crtc);
2426 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002427
Ben Skeggse225f442012-11-21 14:40:21 +10002428 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +10002429
Ben Skeggsad633612016-11-04 17:20:36 +10002430 asyh->state.active = false;
2431 nv50_head_atomic_check(&head->base.base, &asyh->state);
2432 nv50_head_flush_clr(head, asyh, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002433}
2434
2435static void
Ben Skeggse225f442012-11-21 14:40:21 +10002436nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002437{
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002438 struct nv50_head *head = nv50_head(crtc);
2439 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002440
Ben Skeggsa7ae1562016-11-04 17:20:36 +10002441 asyh->state.active = true;
2442 nv50_head_atomic_check(&head->base.base, &asyh->state);
2443 nv50_head_flush_set(head, asyh);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002444
Matt Roperf4510a22014-04-01 15:22:40 -07002445 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002446}
2447
2448static bool
Ben Skeggse225f442012-11-21 14:40:21 +10002449nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002450 struct drm_display_mode *adjusted_mode)
2451{
Ben Skeggseb2e9682014-01-24 10:13:23 +10002452 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002453 return true;
2454}
2455
2456static int
Ben Skeggse225f442012-11-21 14:40:21 +10002457nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002458{
Matt Roperf4510a22014-04-01 15:22:40 -07002459 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002460 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002461 int ret;
2462
Ben Skeggs547ad072014-11-10 12:35:06 +10002463 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002464 if (ret == 0) {
2465 if (head->image)
2466 nouveau_bo_unpin(head->image);
2467 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002468 }
2469
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002470 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002471}
2472
2473static int
Ben Skeggse225f442012-11-21 14:40:21 +10002474nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002475 struct drm_display_mode *mode, int x, int y,
2476 struct drm_framebuffer *old_fb)
2477{
2478 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2479 struct nouveau_connector *nv_connector;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002480 int ret;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002481 struct nv50_head *head = nv50_head(crtc);
2482 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002483
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002484 memcpy(&asyh->state.mode, umode, sizeof(*umode));
2485 memcpy(&asyh->state.adjusted_mode, mode, sizeof(*mode));
2486 asyh->state.active = true;
2487 asyh->state.mode_changed = true;
2488 nv50_head_atomic_check(&head->base.base, &asyh->state);
Ben Skeggs2d1d8982011-11-11 23:39:22 +10002489
Ben Skeggse225f442012-11-21 14:40:21 +10002490 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002491 if (ret)
2492 return ret;
2493
Ben Skeggs3dbd0362016-11-04 17:20:36 +10002494 nv50_head_flush_set(head, asyh);
2495
Ben Skeggs438d99e2011-07-05 16:48:06 +10002496 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002497 nv50_crtc_set_dither(nv_crtc, false);
2498 nv50_crtc_set_scale(nv_crtc, false);
Roy Splieteae73822014-10-30 22:57:45 +01002499
Ben Skeggse225f442012-11-21 14:40:21 +10002500 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07002501 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002502 return 0;
2503}
2504
2505static int
Ben Skeggse225f442012-11-21 14:40:21 +10002506nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002507 struct drm_framebuffer *old_fb)
2508{
Ben Skeggs77145f12012-07-31 16:16:21 +10002509 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002510 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2511 int ret;
2512
Matt Roperf4510a22014-04-01 15:22:40 -07002513 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10002514 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10002515 return 0;
2516 }
2517
Ben Skeggse225f442012-11-21 14:40:21 +10002518 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002519 if (ret)
2520 return ret;
2521
Ben Skeggse225f442012-11-21 14:40:21 +10002522 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07002523 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
2524 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002525 return 0;
2526}
2527
2528static int
Ben Skeggse225f442012-11-21 14:40:21 +10002529nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002530 struct drm_framebuffer *fb, int x, int y,
2531 enum mode_set_atomic state)
2532{
2533 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002534 nv50_display_flip_stop(crtc);
2535 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002536 return 0;
2537}
2538
2539static void
Ben Skeggse225f442012-11-21 14:40:21 +10002540nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002541{
Ben Skeggse225f442012-11-21 14:40:21 +10002542 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002543 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
2544 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
2545 int i;
2546
2547 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002548 u16 r = nv_crtc->lut.r[i] >> 2;
2549 u16 g = nv_crtc->lut.g[i] >> 2;
2550 u16 b = nv_crtc->lut.b[i] >> 2;
2551
Ben Skeggs648d4df2014-08-10 04:10:27 +10002552 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10002553 writew(r + 0x0000, lut + (i * 0x08) + 0);
2554 writew(g + 0x0000, lut + (i * 0x08) + 2);
2555 writew(b + 0x0000, lut + (i * 0x08) + 4);
2556 } else {
2557 writew(r + 0x6000, lut + (i * 0x20) + 0);
2558 writew(g + 0x6000, lut + (i * 0x20) + 2);
2559 writew(b + 0x6000, lut + (i * 0x20) + 4);
2560 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002561 }
2562}
2563
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002564static void
2565nv50_crtc_disable(struct drm_crtc *crtc)
2566{
2567 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10002568 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002569 if (head->image)
2570 nouveau_bo_unpin(head->image);
2571 nouveau_bo_ref(NULL, &head->image);
2572}
2573
Ben Skeggs438d99e2011-07-05 16:48:06 +10002574static int
Ben Skeggse225f442012-11-21 14:40:21 +10002575nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002576 uint32_t handle, uint32_t width, uint32_t height)
2577{
2578 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs5a560252014-11-10 15:52:02 +10002579 struct drm_gem_object *gem = NULL;
2580 struct nouveau_bo *nvbo = NULL;
2581 int ret = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002582
Ben Skeggs5a560252014-11-10 15:52:02 +10002583 if (handle) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10002584 if (width != 64 || height != 64)
2585 return -EINVAL;
2586
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01002587 gem = drm_gem_object_lookup(file_priv, handle);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002588 if (unlikely(!gem))
2589 return -ENOENT;
2590 nvbo = nouveau_gem_object(gem);
2591
Ben Skeggs5a560252014-11-10 15:52:02 +10002592 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002593 }
2594
Ben Skeggs5a560252014-11-10 15:52:02 +10002595 if (ret == 0) {
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002596 if (nv_crtc->cursor.nvbo)
2597 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
2598 nouveau_bo_ref(nvbo, &nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002599 }
Ben Skeggs5a560252014-11-10 15:52:02 +10002600 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002601
Ben Skeggs5a560252014-11-10 15:52:02 +10002602 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002603 return ret;
2604}
2605
2606static int
Ben Skeggse225f442012-11-21 14:40:21 +10002607nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002608{
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002609 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs22e927d2016-11-04 17:20:36 +10002610 struct nv50_wndw *wndw = &nv50_head(crtc)->_curs->wndw;
2611 struct nv50_wndw_atom *asyw = &wndw->asy;
2612
2613 asyw->point.x = x;
2614 asyw->point.y = y;
2615 asyw->set.point = true;
2616 nv50_wndw_flush_set(wndw, 0, asyw);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002617
2618 nv_crtc->cursor_saved_x = x;
2619 nv_crtc->cursor_saved_y = y;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002620 return 0;
2621}
2622
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002623static int
Ben Skeggse225f442012-11-21 14:40:21 +10002624nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002625 uint32_t size)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002626{
2627 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002628 u32 i;
2629
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002630 for (i = 0; i < size; i++) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10002631 nv_crtc->lut.r[i] = r[i];
2632 nv_crtc->lut.g[i] = g[i];
2633 nv_crtc->lut.b[i] = b[i];
2634 }
2635
Ben Skeggse225f442012-11-21 14:40:21 +10002636 nv50_crtc_lut_load(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002637
2638 return 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002639}
2640
2641static void
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002642nv50_crtc_cursor_restore(struct nouveau_crtc *nv_crtc, int x, int y)
2643{
2644 nv50_crtc_cursor_move(&nv_crtc->base, x, y);
2645
2646 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
2647}
2648
2649static void
Ben Skeggse225f442012-11-21 14:40:21 +10002650nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002651{
2652 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002653 struct nv50_disp *disp = nv50_disp(crtc->dev);
2654 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10002655
2656 nv50_dmac_destroy(&head->ovly.base, disp->disp);
2657 nv50_pioc_destroy(&head->oimm.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002658
2659 /*XXX: this shouldn't be necessary, but the core doesn't call
2660 * disconnect() during the cleanup paths
2661 */
2662 if (head->image)
2663 nouveau_bo_unpin(head->image);
2664 nouveau_bo_ref(NULL, &head->image);
2665
Ben Skeggs5a560252014-11-10 15:52:02 +10002666 /*XXX: ditto */
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002667 if (nv_crtc->cursor.nvbo)
2668 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
2669 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002670
Ben Skeggs438d99e2011-07-05 16:48:06 +10002671 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002672 if (nv_crtc->lut.nvbo)
2673 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002674 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002675
Ben Skeggs438d99e2011-07-05 16:48:06 +10002676 drm_crtc_cleanup(crtc);
2677 kfree(crtc);
2678}
2679
Ben Skeggse225f442012-11-21 14:40:21 +10002680static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
2681 .dpms = nv50_crtc_dpms,
2682 .prepare = nv50_crtc_prepare,
2683 .commit = nv50_crtc_commit,
2684 .mode_fixup = nv50_crtc_mode_fixup,
2685 .mode_set = nv50_crtc_mode_set,
2686 .mode_set_base = nv50_crtc_mode_set_base,
2687 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
2688 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10002689 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002690};
2691
Ben Skeggse225f442012-11-21 14:40:21 +10002692static const struct drm_crtc_funcs nv50_crtc_func = {
2693 .cursor_set = nv50_crtc_cursor_set,
2694 .cursor_move = nv50_crtc_cursor_move,
2695 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10002696 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10002697 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10002698 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10002699};
2700
2701static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10002702nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002703{
Ben Skeggsa01ca782015-08-20 14:54:15 +10002704 struct nouveau_drm *drm = nouveau_drm(dev);
2705 struct nvif_device *device = &drm->device;
Ben Skeggse225f442012-11-21 14:40:21 +10002706 struct nv50_disp *disp = nv50_disp(dev);
2707 struct nv50_head *head;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002708 struct nv50_base *base;
Ben Skeggs22e927d2016-11-04 17:20:36 +10002709 struct nv50_curs *curs;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002710 struct drm_crtc *crtc;
2711 int ret, i;
2712
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002713 head = kzalloc(sizeof(*head), GFP_KERNEL);
2714 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10002715 return -ENOMEM;
2716
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002717 head->base.index = index;
Ben Skeggsf9887d02012-11-21 13:03:42 +10002718 head->base.color_vibrance = 50;
2719 head->base.vibrant_hue = 0;
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002720 head->base.cursor.set_pos = nv50_crtc_cursor_restore;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002721 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002722 head->base.lut.r[i] = i << 8;
2723 head->base.lut.g[i] = i << 8;
2724 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10002725 }
2726
Ben Skeggs973f10c2016-11-04 17:20:36 +10002727 ret = nv50_base_new(drm, head, &base);
Ben Skeggs22e927d2016-11-04 17:20:36 +10002728 if (ret == 0)
2729 ret = nv50_curs_new(drm, head, &curs);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002730 if (ret) {
2731 kfree(head);
2732 return ret;
2733 }
2734
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002735 crtc = &head->base.base;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002736 head->_base = base;
Ben Skeggs22e927d2016-11-04 17:20:36 +10002737 head->_curs = curs;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002738
Ben Skeggse225f442012-11-21 14:40:21 +10002739 drm_crtc_init(dev, crtc, &nv50_crtc_func);
2740 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002741 drm_mode_crtc_set_gamma_size(crtc, 256);
2742
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10002743 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002744 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002745 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002746 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002747 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002748 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002749 if (ret)
2750 nouveau_bo_unpin(head->base.lut.nvbo);
2751 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10002752 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10002753 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002754 }
2755
2756 if (ret)
2757 goto out;
2758
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002759 /* allocate overlay resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10002760 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002761 if (ret)
2762 goto out;
2763
Ben Skeggsa01ca782015-08-20 14:54:15 +10002764 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
2765 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002766 if (ret)
2767 goto out;
2768
Ben Skeggs438d99e2011-07-05 16:48:06 +10002769out:
2770 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002771 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002772 return ret;
2773}
2774
2775/******************************************************************************
Ben Skeggsa91d3222014-12-22 16:30:13 +10002776 * Encoder helpers
2777 *****************************************************************************/
2778static bool
2779nv50_encoder_mode_fixup(struct drm_encoder *encoder,
2780 const struct drm_display_mode *mode,
2781 struct drm_display_mode *adjusted_mode)
2782{
2783 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2784 struct nouveau_connector *nv_connector;
2785
2786 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2787 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs576f7912014-12-22 17:19:26 +10002788 nv_connector->scaling_full = false;
2789 if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) {
2790 switch (nv_connector->type) {
2791 case DCB_CONNECTOR_LVDS:
2792 case DCB_CONNECTOR_LVDS_SPWG:
2793 case DCB_CONNECTOR_eDP:
2794 /* force use of scaler for non-edid modes */
2795 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
2796 return true;
2797 nv_connector->scaling_full = true;
2798 break;
2799 default:
2800 return true;
2801 }
2802 }
2803
2804 drm_mode_copy(adjusted_mode, nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10002805 }
2806
2807 return true;
2808}
2809
2810/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002811 * DAC
2812 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002813static void
Ben Skeggse225f442012-11-21 14:40:21 +10002814nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002815{
2816 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002817 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10002818 struct {
2819 struct nv50_disp_mthd_v1 base;
2820 struct nv50_disp_dac_pwr_v0 pwr;
2821 } args = {
2822 .base.version = 1,
2823 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
2824 .base.hasht = nv_encoder->dcb->hasht,
2825 .base.hashm = nv_encoder->dcb->hashm,
2826 .pwr.state = 1,
2827 .pwr.data = 1,
2828 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
2829 mode != DRM_MODE_DPMS_OFF),
2830 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
2831 mode != DRM_MODE_DPMS_OFF),
2832 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002833
Ben Skeggsbf0eb892014-08-10 04:10:26 +10002834 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002835}
2836
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002837static void
Ben Skeggse225f442012-11-21 14:40:21 +10002838nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002839{
2840}
2841
2842static void
Ben Skeggse225f442012-11-21 14:40:21 +10002843nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002844 struct drm_display_mode *adjusted_mode)
2845{
Ben Skeggse225f442012-11-21 14:40:21 +10002846 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002847 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2848 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10002849 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002850
Ben Skeggse225f442012-11-21 14:40:21 +10002851 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002852
Ben Skeggs97b19b52012-11-16 11:21:37 +10002853 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002854 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002855 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002856 u32 syncs = 0x00000000;
2857
2858 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2859 syncs |= 0x00000001;
2860 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2861 syncs |= 0x00000002;
2862
2863 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
2864 evo_data(push, 1 << nv_crtc->index);
2865 evo_data(push, syncs);
2866 } else {
2867 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2868 u32 syncs = 0x00000001;
2869
2870 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2871 syncs |= 0x00000008;
2872 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2873 syncs |= 0x00000010;
2874
2875 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2876 magic |= 0x00000001;
2877
2878 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2879 evo_data(push, syncs);
2880 evo_data(push, magic);
2881 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2882 evo_data(push, 1 << nv_crtc->index);
2883 }
2884
2885 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002886 }
2887
2888 nv_encoder->crtc = encoder->crtc;
2889}
2890
2891static void
Ben Skeggse225f442012-11-21 14:40:21 +10002892nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002893{
2894 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002895 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10002896 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002897 u32 *push;
2898
2899 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10002900 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002901
Ben Skeggs97b19b52012-11-16 11:21:37 +10002902 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002903 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002904 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002905 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2906 evo_data(push, 0x00000000);
2907 } else {
2908 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2909 evo_data(push, 0x00000000);
2910 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10002911 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002912 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002913 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10002914
2915 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002916}
2917
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002918static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10002919nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002920{
Ben Skeggsc4abd312014-08-10 04:10:26 +10002921 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002922 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10002923 struct {
2924 struct nv50_disp_mthd_v1 base;
2925 struct nv50_disp_dac_load_v0 load;
2926 } args = {
2927 .base.version = 1,
2928 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2929 .base.hasht = nv_encoder->dcb->hasht,
2930 .base.hashm = nv_encoder->dcb->hashm,
2931 };
2932 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10002933
Ben Skeggsc4abd312014-08-10 04:10:26 +10002934 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2935 if (args.load.data == 0)
2936 args.load.data = 340;
2937
2938 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2939 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10002940 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10002941
Ben Skeggs35b21d32012-11-08 12:08:55 +10002942 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002943}
2944
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002945static void
Ben Skeggse225f442012-11-21 14:40:21 +10002946nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002947{
2948 drm_encoder_cleanup(encoder);
2949 kfree(encoder);
2950}
2951
Ben Skeggse225f442012-11-21 14:40:21 +10002952static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
2953 .dpms = nv50_dac_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10002954 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggse225f442012-11-21 14:40:21 +10002955 .prepare = nv50_dac_disconnect,
2956 .commit = nv50_dac_commit,
2957 .mode_set = nv50_dac_mode_set,
2958 .disable = nv50_dac_disconnect,
2959 .get_crtc = nv50_display_crtc_get,
2960 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002961};
2962
Ben Skeggse225f442012-11-21 14:40:21 +10002963static const struct drm_encoder_funcs nv50_dac_func = {
2964 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002965};
2966
2967static int
Ben Skeggse225f442012-11-21 14:40:21 +10002968nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002969{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002970 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002971 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002972 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002973 struct nouveau_encoder *nv_encoder;
2974 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002975 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002976
2977 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2978 if (!nv_encoder)
2979 return -ENOMEM;
2980 nv_encoder->dcb = dcbe;
2981 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002982
2983 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2984 if (bus)
2985 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002986
2987 encoder = to_drm_encoder(nv_encoder);
2988 encoder->possible_crtcs = dcbe->heads;
2989 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002990 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2991 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggse225f442012-11-21 14:40:21 +10002992 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002993
2994 drm_mode_connector_attach_encoder(connector, encoder);
2995 return 0;
2996}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002997
2998/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10002999 * Audio
3000 *****************************************************************************/
3001static void
Ben Skeggse225f442012-11-21 14:40:21 +10003002nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10003003{
3004 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10003005 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10003006 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10003007 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10003008 struct __packed {
3009 struct {
3010 struct nv50_disp_mthd_v1 mthd;
3011 struct nv50_disp_sor_hda_eld_v0 eld;
3012 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10003013 u8 data[sizeof(nv_connector->base.eld)];
3014 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10003015 .base.mthd.version = 1,
3016 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
3017 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10003018 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3019 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10003020 };
Ben Skeggs78951d22011-11-11 18:13:13 +10003021
3022 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3023 if (!drm_detect_monitor_audio(nv_connector->edid))
3024 return;
3025
Ben Skeggs78951d22011-11-11 18:13:13 +10003026 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10003027 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10003028
Jani Nikula938fd8a2014-10-28 16:20:48 +02003029 nvif_mthd(disp->disp, 0, &args,
3030 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10003031}
3032
3033static void
Ben Skeggscc2a9072014-09-15 21:29:05 +10003034nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10003035{
3036 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10003037 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10003038 struct {
3039 struct nv50_disp_mthd_v1 base;
3040 struct nv50_disp_sor_hda_eld_v0 eld;
3041 } args = {
3042 .base.version = 1,
3043 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
3044 .base.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10003045 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3046 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10003047 };
Ben Skeggs78951d22011-11-11 18:13:13 +10003048
Ben Skeggs120b0c32014-08-10 04:10:26 +10003049 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10003050}
3051
3052/******************************************************************************
3053 * HDMI
3054 *****************************************************************************/
3055static void
Ben Skeggse225f442012-11-21 14:40:21 +10003056nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10003057{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003058 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3059 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10003060 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10003061 struct {
3062 struct nv50_disp_mthd_v1 base;
3063 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
3064 } args = {
3065 .base.version = 1,
3066 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
3067 .base.hasht = nv_encoder->dcb->hasht,
3068 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3069 (0x0100 << nv_crtc->index),
3070 .pwr.state = 1,
3071 .pwr.rekey = 56, /* binary driver, and tegra, constant */
3072 };
3073 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003074 u32 max_ac_packet;
3075
3076 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3077 if (!drm_detect_hdmi_monitor(nv_connector->edid))
3078 return;
3079
3080 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10003081 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003082 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10003083 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003084
Ben Skeggse00f2232014-08-10 04:10:26 +10003085 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10003086 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10003087}
3088
3089static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10003090nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10003091{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003092 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10003093 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10003094 struct {
3095 struct nv50_disp_mthd_v1 base;
3096 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
3097 } args = {
3098 .base.version = 1,
3099 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
3100 .base.hasht = nv_encoder->dcb->hasht,
3101 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3102 (0x0100 << nv_crtc->index),
3103 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003104
Ben Skeggse00f2232014-08-10 04:10:26 +10003105 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10003106}
3107
3108/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003109 * MST
3110 *****************************************************************************/
3111struct nv50_mstm {
3112 struct nouveau_encoder *outp;
3113
3114 struct drm_dp_mst_topology_mgr mgr;
3115};
3116
3117static int
3118nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
3119{
3120 struct nouveau_encoder *outp = mstm->outp;
3121 struct {
3122 struct nv50_disp_mthd_v1 base;
3123 struct nv50_disp_sor_dp_mst_link_v0 mst;
3124 } args = {
3125 .base.version = 1,
3126 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
3127 .base.hasht = outp->dcb->hasht,
3128 .base.hashm = outp->dcb->hashm,
3129 .mst.state = state,
3130 };
3131 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
3132 struct nvif_object *disp = &drm->display->disp;
3133 int ret;
3134
3135 if (dpcd >= 0x12) {
3136 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
3137 if (ret < 0)
3138 return ret;
3139
3140 dpcd &= ~DP_MST_EN;
3141 if (state)
3142 dpcd |= DP_MST_EN;
3143
3144 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
3145 if (ret < 0)
3146 return ret;
3147 }
3148
3149 return nvif_mthd(disp, 0, &args, sizeof(args));
3150}
3151
3152int
3153nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
3154{
3155 int ret, state = 0;
3156
3157 if (!mstm)
3158 return 0;
3159
3160 if (dpcd[0] >= 0x12 && allow) {
3161 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
3162 if (ret < 0)
3163 return ret;
3164
3165 state = dpcd[1] & DP_MST_CAP;
3166 }
3167
3168 ret = nv50_mstm_enable(mstm, dpcd[0], state);
3169 if (ret)
3170 return ret;
3171
3172 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
3173 if (ret)
3174 return nv50_mstm_enable(mstm, dpcd[0], 0);
3175
3176 return mstm->mgr.mst_state;
3177}
3178
3179static void
3180nv50_mstm_del(struct nv50_mstm **pmstm)
3181{
3182 struct nv50_mstm *mstm = *pmstm;
3183 if (mstm) {
3184 kfree(*pmstm);
3185 *pmstm = NULL;
3186 }
3187}
3188
3189static int
3190nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
3191 int conn_base_id, struct nv50_mstm **pmstm)
3192{
3193 const int max_payloads = hweight8(outp->dcb->heads);
3194 struct drm_device *dev = outp->base.base.dev;
3195 struct nv50_mstm *mstm;
3196 int ret;
3197
3198 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
3199 return -ENOMEM;
3200 mstm->outp = outp;
3201
3202 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev->dev, aux, aux_max,
3203 max_payloads, conn_base_id);
3204 if (ret)
3205 return ret;
3206
3207 return 0;
3208}
3209
3210/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003211 * SOR
3212 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003213static void
Ben Skeggse225f442012-11-21 14:40:21 +10003214nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003215{
3216 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003217 struct nv50_disp *disp = nv50_disp(encoder->dev);
3218 struct {
3219 struct nv50_disp_mthd_v1 base;
3220 struct nv50_disp_sor_pwr_v0 pwr;
3221 } args = {
3222 .base.version = 1,
3223 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
3224 .base.hasht = nv_encoder->dcb->hasht,
3225 .base.hashm = nv_encoder->dcb->hashm,
3226 .pwr.state = mode == DRM_MODE_DPMS_ON,
3227 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10003228 struct {
3229 struct nv50_disp_mthd_v1 base;
3230 struct nv50_disp_sor_dp_pwr_v0 pwr;
3231 } link = {
3232 .base.version = 1,
3233 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
3234 .base.hasht = nv_encoder->dcb->hasht,
3235 .base.hashm = nv_encoder->dcb->hashm,
3236 .pwr.state = mode == DRM_MODE_DPMS_ON,
3237 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10003238 struct drm_device *dev = encoder->dev;
3239 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003240
3241 nv_encoder->last_dpms = mode;
3242
3243 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
3244 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
3245
3246 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
3247 continue;
3248
3249 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10003250 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10003251 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
3252 return;
3253 break;
3254 }
3255 }
3256
Ben Skeggs48743222014-05-31 01:48:06 +10003257 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003258 args.pwr.state = 1;
3259 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10003260 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10003261 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003262 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10003263 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003264}
3265
Ben Skeggs83fc0832011-07-05 13:08:40 +10003266static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10003267nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
3268{
3269 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
3270 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
3271 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003272 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10003273 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
3274 evo_data(push, (nv_encoder->ctrl = temp));
3275 } else {
3276 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
3277 evo_data(push, (nv_encoder->ctrl = temp));
3278 }
3279 evo_kick(push, mast);
3280 }
3281}
3282
3283static void
Ben Skeggse225f442012-11-21 14:40:21 +10003284nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003285{
3286 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003287 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003288
3289 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
3290 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10003291
3292 if (nv_crtc) {
3293 nv50_crtc_prepare(&nv_crtc->base);
3294 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggscc2a9072014-09-15 21:29:05 +10003295 nv50_audio_disconnect(encoder, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003296 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
3297 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003298}
3299
3300static void
Ben Skeggse225f442012-11-21 14:40:21 +10003301nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003302{
3303}
3304
3305static void
Ben Skeggse225f442012-11-21 14:40:21 +10003306nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003307 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003308{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003309 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3310 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3311 struct {
3312 struct nv50_disp_mthd_v1 base;
3313 struct nv50_disp_sor_lvds_script_v0 lvds;
3314 } lvds = {
3315 .base.version = 1,
3316 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
3317 .base.hasht = nv_encoder->dcb->hasht,
3318 .base.hashm = nv_encoder->dcb->hashm,
3319 };
Ben Skeggse225f442012-11-21 14:40:21 +10003320 struct nv50_disp *disp = nv50_disp(encoder->dev);
3321 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10003322 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10003323 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003324 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10003325 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003326 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003327 u8 owner = 1 << nv_crtc->index;
3328 u8 proto = 0xf;
3329 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003330
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003331 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003332 nv_encoder->crtc = encoder->crtc;
3333
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003334 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10003335 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003336 if (nv_encoder->dcb->sorconf.link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05003337 proto = 0x1;
3338 /* Only enable dual-link if:
3339 * - Need to (i.e. rate > 165MHz)
3340 * - DCB says we can
3341 * - Not an HDMI monitor, since there's no dual-link
3342 * on HDMI.
3343 */
3344 if (mode->clock >= 165000 &&
3345 nv_encoder->dcb->duallink_possible &&
3346 !drm_detect_hdmi_monitor(nv_connector->edid))
3347 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003348 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003349 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003350 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003351
Ben Skeggse84a35a2014-06-05 10:59:55 +10003352 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003353 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003354 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003355 proto = 0x0;
3356
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003357 if (bios->fp_no_ddc) {
3358 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003359 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003360 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003361 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003362 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10003363 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003364 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003365 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003366 } else
3367 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003368 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003369 }
3370
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003371 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003372 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003373 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003374 } else {
3375 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003376 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003377 }
3378
3379 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003380 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003381 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10003382
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003383 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003384 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003385 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10003386 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003387 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003388 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10003389 } else
3390 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003391 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003392 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10003393 } else {
3394 nv_encoder->dp.datarate = mode->clock * 30 / 8;
3395 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10003396 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003397
3398 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003399 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003400 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003401 proto = 0x9;
Ben Skeggs3eee8642014-09-15 15:20:47 +10003402 nv50_audio_mode_set(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003403 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003404 default:
3405 BUG_ON(1);
3406 break;
3407 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10003408
Ben Skeggse84a35a2014-06-05 10:59:55 +10003409 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003410
Ben Skeggs648d4df2014-08-10 04:10:27 +10003411 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10003412 u32 *push = evo_wait(mast, 3);
3413 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003414 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
3415 u32 syncs = 0x00000001;
3416
3417 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3418 syncs |= 0x00000008;
3419 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3420 syncs |= 0x00000010;
3421
3422 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3423 magic |= 0x00000001;
3424
3425 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
3426 evo_data(push, syncs | (depth << 6));
3427 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003428 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003429 }
3430
Ben Skeggse84a35a2014-06-05 10:59:55 +10003431 ctrl = proto << 8;
3432 mask = 0x00000f00;
3433 } else {
3434 ctrl = (depth << 16) | (proto << 8);
3435 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3436 ctrl |= 0x00001000;
3437 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3438 ctrl |= 0x00002000;
3439 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003440 }
3441
Ben Skeggse84a35a2014-06-05 10:59:55 +10003442 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003443}
3444
3445static void
Ben Skeggse225f442012-11-21 14:40:21 +10003446nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003447{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003448 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3449 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003450 drm_encoder_cleanup(encoder);
3451 kfree(encoder);
3452}
3453
Ben Skeggse225f442012-11-21 14:40:21 +10003454static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
3455 .dpms = nv50_sor_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10003456 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10003457 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10003458 .commit = nv50_sor_commit,
3459 .mode_set = nv50_sor_mode_set,
3460 .disable = nv50_sor_disconnect,
3461 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10003462};
3463
Ben Skeggse225f442012-11-21 14:40:21 +10003464static const struct drm_encoder_funcs nv50_sor_func = {
3465 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10003466};
3467
3468static int
Ben Skeggse225f442012-11-21 14:40:21 +10003469nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003470{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003471 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10003472 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10003473 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003474 struct nouveau_encoder *nv_encoder;
3475 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003476 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10003477
3478 switch (dcbe->type) {
3479 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
3480 case DCB_OUTPUT_TMDS:
3481 case DCB_OUTPUT_DP:
3482 default:
3483 type = DRM_MODE_ENCODER_TMDS;
3484 break;
3485 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003486
3487 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3488 if (!nv_encoder)
3489 return -ENOMEM;
3490 nv_encoder->dcb = dcbe;
3491 nv_encoder->or = ffs(dcbe->or) - 1;
3492 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
3493
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003494 encoder = to_drm_encoder(nv_encoder);
3495 encoder->possible_crtcs = dcbe->heads;
3496 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003497 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
3498 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003499 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
3500
3501 drm_mode_connector_attach_encoder(connector, encoder);
3502
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003503 if (dcbe->type == DCB_OUTPUT_DP) {
3504 struct nvkm_i2c_aux *aux =
3505 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
3506 if (aux) {
3507 nv_encoder->i2c = &aux->i2c;
3508 nv_encoder->aux = aux;
3509 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003510
3511 /*TODO: Use DP Info Table to check for support. */
3512 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
3513 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
3514 nv_connector->base.base.id,
3515 &nv_encoder->dp.mstm);
3516 if (ret)
3517 return ret;
3518 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003519 } else {
3520 struct nvkm_i2c_bus *bus =
3521 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
3522 if (bus)
3523 nv_encoder->i2c = &bus->i2c;
3524 }
3525
Ben Skeggs83fc0832011-07-05 13:08:40 +10003526 return 0;
3527}
Ben Skeggs26f6d882011-07-04 16:25:18 +10003528
3529/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10003530 * PIOR
3531 *****************************************************************************/
3532
3533static void
3534nv50_pior_dpms(struct drm_encoder *encoder, int mode)
3535{
3536 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3537 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10003538 struct {
3539 struct nv50_disp_mthd_v1 base;
3540 struct nv50_disp_pior_pwr_v0 pwr;
3541 } args = {
3542 .base.version = 1,
3543 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
3544 .base.hasht = nv_encoder->dcb->hasht,
3545 .base.hashm = nv_encoder->dcb->hashm,
3546 .pwr.state = mode == DRM_MODE_DPMS_ON,
3547 .pwr.type = nv_encoder->dcb->type,
3548 };
3549
3550 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10003551}
3552
3553static bool
3554nv50_pior_mode_fixup(struct drm_encoder *encoder,
3555 const struct drm_display_mode *mode,
3556 struct drm_display_mode *adjusted_mode)
3557{
Ben Skeggsa91d3222014-12-22 16:30:13 +10003558 if (!nv50_encoder_mode_fixup(encoder, mode, adjusted_mode))
3559 return false;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003560 adjusted_mode->clock *= 2;
3561 return true;
3562}
3563
3564static void
3565nv50_pior_commit(struct drm_encoder *encoder)
3566{
3567}
3568
3569static void
3570nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
3571 struct drm_display_mode *adjusted_mode)
3572{
3573 struct nv50_mast *mast = nv50_mast(encoder->dev);
3574 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3575 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3576 struct nouveau_connector *nv_connector;
3577 u8 owner = 1 << nv_crtc->index;
3578 u8 proto, depth;
3579 u32 *push;
3580
3581 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3582 switch (nv_connector->base.display_info.bpc) {
3583 case 10: depth = 0x6; break;
3584 case 8: depth = 0x5; break;
3585 case 6: depth = 0x2; break;
3586 default: depth = 0x0; break;
3587 }
3588
3589 switch (nv_encoder->dcb->type) {
3590 case DCB_OUTPUT_TMDS:
3591 case DCB_OUTPUT_DP:
3592 proto = 0x0;
3593 break;
3594 default:
3595 BUG_ON(1);
3596 break;
3597 }
3598
3599 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
3600
3601 push = evo_wait(mast, 8);
3602 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003603 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003604 u32 ctrl = (depth << 16) | (proto << 8) | owner;
3605 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3606 ctrl |= 0x00001000;
3607 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3608 ctrl |= 0x00002000;
3609 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
3610 evo_data(push, ctrl);
3611 }
3612
3613 evo_kick(push, mast);
3614 }
3615
3616 nv_encoder->crtc = encoder->crtc;
3617}
3618
3619static void
3620nv50_pior_disconnect(struct drm_encoder *encoder)
3621{
3622 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3623 struct nv50_mast *mast = nv50_mast(encoder->dev);
3624 const int or = nv_encoder->or;
3625 u32 *push;
3626
3627 if (nv_encoder->crtc) {
3628 nv50_crtc_prepare(nv_encoder->crtc);
3629
3630 push = evo_wait(mast, 4);
3631 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003632 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003633 evo_mthd(push, 0x0700 + (or * 0x040), 1);
3634 evo_data(push, 0x00000000);
3635 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10003636 evo_kick(push, mast);
3637 }
3638 }
3639
3640 nv_encoder->crtc = NULL;
3641}
3642
3643static void
3644nv50_pior_destroy(struct drm_encoder *encoder)
3645{
3646 drm_encoder_cleanup(encoder);
3647 kfree(encoder);
3648}
3649
3650static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
3651 .dpms = nv50_pior_dpms,
3652 .mode_fixup = nv50_pior_mode_fixup,
3653 .prepare = nv50_pior_disconnect,
3654 .commit = nv50_pior_commit,
3655 .mode_set = nv50_pior_mode_set,
3656 .disable = nv50_pior_disconnect,
3657 .get_crtc = nv50_display_crtc_get,
3658};
3659
3660static const struct drm_encoder_funcs nv50_pior_func = {
3661 .destroy = nv50_pior_destroy,
3662};
3663
3664static int
3665nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
3666{
3667 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10003668 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003669 struct nvkm_i2c_bus *bus = NULL;
3670 struct nvkm_i2c_aux *aux = NULL;
3671 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003672 struct nouveau_encoder *nv_encoder;
3673 struct drm_encoder *encoder;
3674 int type;
3675
3676 switch (dcbe->type) {
3677 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003678 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
3679 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003680 type = DRM_MODE_ENCODER_TMDS;
3681 break;
3682 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003683 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
3684 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003685 type = DRM_MODE_ENCODER_TMDS;
3686 break;
3687 default:
3688 return -ENODEV;
3689 }
3690
3691 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3692 if (!nv_encoder)
3693 return -ENOMEM;
3694 nv_encoder->dcb = dcbe;
3695 nv_encoder->or = ffs(dcbe->or) - 1;
3696 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003697 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003698
3699 encoder = to_drm_encoder(nv_encoder);
3700 encoder->possible_crtcs = dcbe->heads;
3701 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003702 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
3703 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003704 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
3705
3706 drm_mode_connector_attach_encoder(connector, encoder);
3707 return 0;
3708}
3709
3710/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10003711 * Framebuffer
3712 *****************************************************************************/
3713
Ben Skeggs8a423642014-08-10 04:10:19 +10003714static void
Ben Skeggsab0af552014-08-10 04:10:19 +10003715nv50_fb_dtor(struct drm_framebuffer *fb)
3716{
3717}
3718
3719static int
3720nv50_fb_ctor(struct drm_framebuffer *fb)
3721{
3722 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
3723 struct nouveau_drm *drm = nouveau_drm(fb->dev);
3724 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10003725 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10003726 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
3727 u8 tile = nvbo->tile_mode;
Ben Skeggsaccdea22016-11-04 17:20:36 +10003728 struct drm_crtc *crtc;
Ben Skeggsab0af552014-08-10 04:10:19 +10003729
Ben Skeggs967e7bd2014-08-10 04:10:22 +10003730 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10003731 tile >>= 4; /* yep.. */
3732
Ben Skeggsab0af552014-08-10 04:10:19 +10003733 switch (fb->depth) {
3734 case 8: nv_fb->r_format = 0x1e00; break;
3735 case 15: nv_fb->r_format = 0xe900; break;
3736 case 16: nv_fb->r_format = 0xe800; break;
3737 case 24:
3738 case 32: nv_fb->r_format = 0xcf00; break;
3739 case 30: nv_fb->r_format = 0xd100; break;
3740 default:
3741 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
3742 return -EINVAL;
3743 }
3744
Ben Skeggs648d4df2014-08-10 04:10:27 +10003745 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10003746 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3747 (fb->pitches[0] | 0x00100000);
3748 nv_fb->r_format |= kind << 16;
3749 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10003750 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10003751 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3752 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10003753 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10003754 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3755 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10003756 }
Ben Skeggs8a423642014-08-10 04:10:19 +10003757 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10003758
Ben Skeggsaccdea22016-11-04 17:20:36 +10003759 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
3760 struct nv50_head *head = nv50_head(crtc);
3761 struct nv50_dmac_ctxdma *ctxdma;
3762
3763 ctxdma = nv50_dmac_ctxdma_new(&head->_base->chan.base,
3764 nv_fb->r_handle, nv_fb);
3765 if (IS_ERR(ctxdma))
3766 return PTR_ERR(ctxdma);
3767 }
3768
3769 return 0;
Ben Skeggsab0af552014-08-10 04:10:19 +10003770}
3771
3772/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003773 * Init
3774 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10003775
Ben Skeggs2a44e492011-11-09 11:36:33 +10003776void
Ben Skeggse225f442012-11-21 14:40:21 +10003777nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003778{
Ben Skeggs973f10c2016-11-04 17:20:36 +10003779 struct drm_plane *plane;
3780
3781 drm_for_each_plane(plane, dev) {
3782 struct nv50_wndw *wndw = nv50_wndw(plane);
3783 if (plane->funcs != &nv50_wndw)
3784 continue;
3785 nv50_wndw_fini(wndw);
3786 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10003787}
3788
3789int
Ben Skeggse225f442012-11-21 14:40:21 +10003790nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003791{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003792 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs354d3502016-11-04 17:20:36 +10003793 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10003794 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003795 struct drm_crtc *crtc;
3796 u32 *push;
3797
3798 push = evo_wait(nv50_mast(dev), 32);
3799 if (!push)
3800 return -EBUSY;
3801
3802 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10003803 struct nv50_wndw *wndw = &nv50_head(crtc)->_base->wndw;
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01003804
3805 nv50_crtc_lut_load(crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +10003806 nouveau_bo_wr32(disp->sync, wndw->sema / 4, wndw->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003807 }
3808
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003809 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10003810 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003811 evo_kick(push, nv50_mast(dev));
Ben Skeggs973f10c2016-11-04 17:20:36 +10003812
Ben Skeggs354d3502016-11-04 17:20:36 +10003813 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3814 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3815 const struct drm_encoder_helper_funcs *help;
3816 struct nouveau_encoder *nv_encoder;
3817
3818 nv_encoder = nouveau_encoder(encoder);
3819 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
3820 nv_encoder->dcb->type = DCB_OUTPUT_EOL;
3821
3822 help = encoder->helper_private;
3823 if (help && help->dpms)
3824 help->dpms(encoder, DRM_MODE_DPMS_ON);
3825
3826 if (nv_encoder->dcb->type == DCB_OUTPUT_EOL)
3827 nv_encoder->dcb->type = DCB_OUTPUT_DP;
3828 }
3829 }
3830
Ben Skeggs973f10c2016-11-04 17:20:36 +10003831 drm_for_each_plane(plane, dev) {
3832 struct nv50_wndw *wndw = nv50_wndw(plane);
3833 if (plane->funcs != &nv50_wndw)
3834 continue;
3835 nv50_wndw_init(wndw);
3836 }
3837
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003838 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003839}
3840
3841void
Ben Skeggse225f442012-11-21 14:40:21 +10003842nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003843{
Ben Skeggse225f442012-11-21 14:40:21 +10003844 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003845
Ben Skeggs0ad72862014-08-10 04:10:22 +10003846 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10003847
Ben Skeggs816af2f2011-11-16 15:48:48 +10003848 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003849 if (disp->sync)
3850 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10003851 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10003852
Ben Skeggs77145f12012-07-31 16:16:21 +10003853 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003854 kfree(disp);
3855}
3856
3857int
Ben Skeggse225f442012-11-21 14:40:21 +10003858nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003859{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10003860 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10003861 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10003862 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003863 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10003864 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10003865 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10003866 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003867
3868 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
3869 if (!disp)
3870 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10003871
3872 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10003873 nouveau_display(dev)->dtor = nv50_display_destroy;
3874 nouveau_display(dev)->init = nv50_display_init;
3875 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10003876 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
3877 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10003878 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003879
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003880 /* small shared memory area we use for notifiers and semaphores */
3881 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01003882 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003883 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10003884 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003885 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003886 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003887 if (ret)
3888 nouveau_bo_unpin(disp->sync);
3889 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003890 if (ret)
3891 nouveau_bo_ref(NULL, &disp->sync);
3892 }
3893
3894 if (ret)
3895 goto out;
3896
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003897 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10003898 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10003899 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003900 if (ret)
3901 goto out;
3902
Ben Skeggs438d99e2011-07-05 16:48:06 +10003903 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10003904 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsa01ca782015-08-20 14:54:15 +10003905 crtcs = nvif_rd32(&device->object, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10003906 else
3907 crtcs = 2;
3908
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10003909 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10003910 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10003911 if (ret)
3912 goto out;
3913 }
3914
Ben Skeggs83fc0832011-07-05 13:08:40 +10003915 /* create encoder/connector objects based on VBIOS DCB table */
3916 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
3917 connector = nouveau_connector_create(dev, dcbe->connector);
3918 if (IS_ERR(connector))
3919 continue;
3920
Ben Skeggseb6313a2013-02-11 09:52:58 +10003921 if (dcbe->location == DCB_LOC_ON_CHIP) {
3922 switch (dcbe->type) {
3923 case DCB_OUTPUT_TMDS:
3924 case DCB_OUTPUT_LVDS:
3925 case DCB_OUTPUT_DP:
3926 ret = nv50_sor_create(connector, dcbe);
3927 break;
3928 case DCB_OUTPUT_ANALOG:
3929 ret = nv50_dac_create(connector, dcbe);
3930 break;
3931 default:
3932 ret = -ENODEV;
3933 break;
3934 }
3935 } else {
3936 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003937 }
3938
Ben Skeggseb6313a2013-02-11 09:52:58 +10003939 if (ret) {
3940 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
3941 dcbe->location, dcbe->type,
3942 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10003943 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003944 }
3945 }
3946
3947 /* cull any connectors we created that don't have an encoder */
3948 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
3949 if (connector->encoder_ids[0])
3950 continue;
3951
Ben Skeggs77145f12012-07-31 16:16:21 +10003952 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03003953 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003954 connector->funcs->destroy(connector);
3955 }
3956
Ben Skeggs26f6d882011-07-04 16:25:18 +10003957out:
3958 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10003959 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003960 return ret;
3961}