blob: 617c57daad21d2069ab173f05591c08885f20183 [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 Skeggsf20c6652016-11-04 17:20:36 +10002838nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002839{
Ben Skeggsf20c6652016-11-04 17:20:36 +10002840 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2841 struct nv50_mast *mast = nv50_mast(encoder->dev);
2842 const int or = nv_encoder->or;
2843 u32 *push;
2844
2845 if (nv_encoder->crtc) {
2846 nv50_crtc_prepare(nv_encoder->crtc);
2847
2848 push = evo_wait(mast, 4);
2849 if (push) {
2850 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2851 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2852 evo_data(push, 0x00000000);
2853 } else {
2854 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2855 evo_data(push, 0x00000000);
2856 }
2857 evo_kick(push, mast);
2858 }
2859 }
2860
2861 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002862}
2863
2864static void
Ben Skeggse225f442012-11-21 14:40:21 +10002865nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002866 struct drm_display_mode *adjusted_mode)
2867{
Ben Skeggse225f442012-11-21 14:40:21 +10002868 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002869 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2870 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10002871 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002872
Ben Skeggse225f442012-11-21 14:40:21 +10002873 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002874
Ben Skeggs97b19b52012-11-16 11:21:37 +10002875 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002876 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002877 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002878 u32 syncs = 0x00000000;
2879
2880 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2881 syncs |= 0x00000001;
2882 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2883 syncs |= 0x00000002;
2884
2885 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
2886 evo_data(push, 1 << nv_crtc->index);
2887 evo_data(push, syncs);
2888 } else {
2889 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2890 u32 syncs = 0x00000001;
2891
2892 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2893 syncs |= 0x00000008;
2894 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2895 syncs |= 0x00000010;
2896
2897 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2898 magic |= 0x00000001;
2899
2900 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2901 evo_data(push, syncs);
2902 evo_data(push, magic);
2903 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2904 evo_data(push, 1 << nv_crtc->index);
2905 }
2906
2907 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002908 }
2909
2910 nv_encoder->crtc = encoder->crtc;
2911}
2912
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002913static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10002914nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002915{
Ben Skeggsc4abd312014-08-10 04:10:26 +10002916 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002917 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10002918 struct {
2919 struct nv50_disp_mthd_v1 base;
2920 struct nv50_disp_dac_load_v0 load;
2921 } args = {
2922 .base.version = 1,
2923 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2924 .base.hasht = nv_encoder->dcb->hasht,
2925 .base.hashm = nv_encoder->dcb->hashm,
2926 };
2927 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10002928
Ben Skeggsc4abd312014-08-10 04:10:26 +10002929 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2930 if (args.load.data == 0)
2931 args.load.data = 340;
2932
2933 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2934 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10002935 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10002936
Ben Skeggs35b21d32012-11-08 12:08:55 +10002937 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002938}
2939
Ben Skeggsf20c6652016-11-04 17:20:36 +10002940static const struct drm_encoder_helper_funcs
2941nv50_dac_help = {
2942 .dpms = nv50_dac_dpms,
2943 .mode_fixup = nv50_encoder_mode_fixup,
2944 .prepare = nv50_dac_disconnect,
2945 .mode_set = nv50_dac_mode_set,
2946 .disable = nv50_dac_disconnect,
2947 .get_crtc = nv50_display_crtc_get,
2948 .detect = nv50_dac_detect
2949};
2950
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002951static void
Ben Skeggse225f442012-11-21 14:40:21 +10002952nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002953{
2954 drm_encoder_cleanup(encoder);
2955 kfree(encoder);
2956}
2957
Ben Skeggsf20c6652016-11-04 17:20:36 +10002958static const struct drm_encoder_funcs
2959nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10002960 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002961};
2962
2963static int
Ben Skeggse225f442012-11-21 14:40:21 +10002964nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002965{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002966 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002967 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002968 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002969 struct nouveau_encoder *nv_encoder;
2970 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002971 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002972
2973 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2974 if (!nv_encoder)
2975 return -ENOMEM;
2976 nv_encoder->dcb = dcbe;
2977 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002978
2979 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2980 if (bus)
2981 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002982
2983 encoder = to_drm_encoder(nv_encoder);
2984 encoder->possible_crtcs = dcbe->heads;
2985 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002986 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2987 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10002988 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002989
2990 drm_mode_connector_attach_encoder(connector, encoder);
2991 return 0;
2992}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002993
2994/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10002995 * Audio
2996 *****************************************************************************/
2997static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10002998nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
2999{
3000 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3001 struct nv50_disp *disp = nv50_disp(encoder->dev);
3002 struct {
3003 struct nv50_disp_mthd_v1 base;
3004 struct nv50_disp_sor_hda_eld_v0 eld;
3005 } args = {
3006 .base.version = 1,
3007 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
3008 .base.hasht = nv_encoder->dcb->hasht,
3009 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3010 (0x0100 << nv_crtc->index),
3011 };
3012
3013 nvif_mthd(disp->disp, 0, &args, sizeof(args));
3014}
3015
3016static void
3017nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10003018{
3019 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10003020 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10003021 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10003022 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10003023 struct __packed {
3024 struct {
3025 struct nv50_disp_mthd_v1 mthd;
3026 struct nv50_disp_sor_hda_eld_v0 eld;
3027 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10003028 u8 data[sizeof(nv_connector->base.eld)];
3029 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10003030 .base.mthd.version = 1,
3031 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
3032 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10003033 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3034 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10003035 };
Ben Skeggs78951d22011-11-11 18:13:13 +10003036
3037 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3038 if (!drm_detect_monitor_audio(nv_connector->edid))
3039 return;
3040
Ben Skeggs78951d22011-11-11 18:13:13 +10003041 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10003042 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10003043
Jani Nikula938fd8a2014-10-28 16:20:48 +02003044 nvif_mthd(disp->disp, 0, &args,
3045 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10003046}
3047
Ben Skeggsf20c6652016-11-04 17:20:36 +10003048/******************************************************************************
3049 * HDMI
3050 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +10003051static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10003052nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10003053{
3054 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10003055 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10003056 struct {
3057 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003058 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +10003059 } args = {
3060 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +10003061 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
3062 .base.hasht = nv_encoder->dcb->hasht,
3063 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3064 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10003065 };
Ben Skeggs78951d22011-11-11 18:13:13 +10003066
Ben Skeggs120b0c32014-08-10 04:10:26 +10003067 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10003068}
3069
Ben Skeggs78951d22011-11-11 18:13:13 +10003070static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10003071nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10003072{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003073 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3074 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10003075 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10003076 struct {
3077 struct nv50_disp_mthd_v1 base;
3078 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
3079 } args = {
3080 .base.version = 1,
3081 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
3082 .base.hasht = nv_encoder->dcb->hasht,
3083 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
3084 (0x0100 << nv_crtc->index),
3085 .pwr.state = 1,
3086 .pwr.rekey = 56, /* binary driver, and tegra, constant */
3087 };
3088 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003089 u32 max_ac_packet;
3090
3091 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3092 if (!drm_detect_hdmi_monitor(nv_connector->edid))
3093 return;
3094
3095 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10003096 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003097 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10003098 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10003099
Ben Skeggse00f2232014-08-10 04:10:26 +10003100 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsf20c6652016-11-04 17:20:36 +10003101 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10003102}
3103
3104/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003105 * MST
3106 *****************************************************************************/
3107struct nv50_mstm {
3108 struct nouveau_encoder *outp;
3109
3110 struct drm_dp_mst_topology_mgr mgr;
3111};
3112
3113static int
3114nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
3115{
3116 struct nouveau_encoder *outp = mstm->outp;
3117 struct {
3118 struct nv50_disp_mthd_v1 base;
3119 struct nv50_disp_sor_dp_mst_link_v0 mst;
3120 } args = {
3121 .base.version = 1,
3122 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
3123 .base.hasht = outp->dcb->hasht,
3124 .base.hashm = outp->dcb->hashm,
3125 .mst.state = state,
3126 };
3127 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
3128 struct nvif_object *disp = &drm->display->disp;
3129 int ret;
3130
3131 if (dpcd >= 0x12) {
3132 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
3133 if (ret < 0)
3134 return ret;
3135
3136 dpcd &= ~DP_MST_EN;
3137 if (state)
3138 dpcd |= DP_MST_EN;
3139
3140 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
3141 if (ret < 0)
3142 return ret;
3143 }
3144
3145 return nvif_mthd(disp, 0, &args, sizeof(args));
3146}
3147
3148int
3149nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
3150{
3151 int ret, state = 0;
3152
3153 if (!mstm)
3154 return 0;
3155
3156 if (dpcd[0] >= 0x12 && allow) {
3157 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
3158 if (ret < 0)
3159 return ret;
3160
3161 state = dpcd[1] & DP_MST_CAP;
3162 }
3163
3164 ret = nv50_mstm_enable(mstm, dpcd[0], state);
3165 if (ret)
3166 return ret;
3167
3168 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
3169 if (ret)
3170 return nv50_mstm_enable(mstm, dpcd[0], 0);
3171
3172 return mstm->mgr.mst_state;
3173}
3174
3175static void
3176nv50_mstm_del(struct nv50_mstm **pmstm)
3177{
3178 struct nv50_mstm *mstm = *pmstm;
3179 if (mstm) {
3180 kfree(*pmstm);
3181 *pmstm = NULL;
3182 }
3183}
3184
3185static int
3186nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
3187 int conn_base_id, struct nv50_mstm **pmstm)
3188{
3189 const int max_payloads = hweight8(outp->dcb->heads);
3190 struct drm_device *dev = outp->base.base.dev;
3191 struct nv50_mstm *mstm;
3192 int ret;
3193
3194 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
3195 return -ENOMEM;
3196 mstm->outp = outp;
3197
3198 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev->dev, aux, aux_max,
3199 max_payloads, conn_base_id);
3200 if (ret)
3201 return ret;
3202
3203 return 0;
3204}
3205
3206/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003207 * SOR
3208 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003209static void
Ben Skeggse225f442012-11-21 14:40:21 +10003210nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003211{
3212 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003213 struct nv50_disp *disp = nv50_disp(encoder->dev);
3214 struct {
3215 struct nv50_disp_mthd_v1 base;
3216 struct nv50_disp_sor_pwr_v0 pwr;
3217 } args = {
3218 .base.version = 1,
3219 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
3220 .base.hasht = nv_encoder->dcb->hasht,
3221 .base.hashm = nv_encoder->dcb->hashm,
3222 .pwr.state = mode == DRM_MODE_DPMS_ON,
3223 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10003224 struct {
3225 struct nv50_disp_mthd_v1 base;
3226 struct nv50_disp_sor_dp_pwr_v0 pwr;
3227 } link = {
3228 .base.version = 1,
3229 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
3230 .base.hasht = nv_encoder->dcb->hasht,
3231 .base.hashm = nv_encoder->dcb->hashm,
3232 .pwr.state = mode == DRM_MODE_DPMS_ON,
3233 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10003234 struct drm_device *dev = encoder->dev;
3235 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003236
3237 nv_encoder->last_dpms = mode;
3238
3239 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
3240 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
3241
3242 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
3243 continue;
3244
3245 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10003246 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10003247 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
3248 return;
3249 break;
3250 }
3251 }
3252
Ben Skeggs48743222014-05-31 01:48:06 +10003253 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003254 args.pwr.state = 1;
3255 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10003256 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10003257 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10003258 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10003259 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003260}
3261
Ben Skeggs83fc0832011-07-05 13:08:40 +10003262static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10003263nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
3264{
3265 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
3266 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
3267 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003268 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10003269 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
3270 evo_data(push, (nv_encoder->ctrl = temp));
3271 } else {
3272 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
3273 evo_data(push, (nv_encoder->ctrl = temp));
3274 }
3275 evo_kick(push, mast);
3276 }
3277}
3278
3279static void
Ben Skeggse225f442012-11-21 14:40:21 +10003280nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003281{
3282 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003283 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003284
3285 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
3286 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10003287
3288 if (nv_crtc) {
3289 nv50_crtc_prepare(&nv_crtc->base);
3290 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003291 nv50_audio_disable(encoder, nv_crtc);
3292 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003293 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10003294}
3295
3296static void
Ben Skeggse225f442012-11-21 14:40:21 +10003297nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003298 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003299{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003300 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3301 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3302 struct {
3303 struct nv50_disp_mthd_v1 base;
3304 struct nv50_disp_sor_lvds_script_v0 lvds;
3305 } lvds = {
3306 .base.version = 1,
3307 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
3308 .base.hasht = nv_encoder->dcb->hasht,
3309 .base.hashm = nv_encoder->dcb->hashm,
3310 };
Ben Skeggse225f442012-11-21 14:40:21 +10003311 struct nv50_disp *disp = nv50_disp(encoder->dev);
3312 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10003313 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10003314 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003315 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10003316 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003317 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003318 u8 owner = 1 << nv_crtc->index;
3319 u8 proto = 0xf;
3320 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003321
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003322 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003323 nv_encoder->crtc = encoder->crtc;
3324
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003325 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10003326 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003327 if (nv_encoder->dcb->sorconf.link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05003328 proto = 0x1;
3329 /* Only enable dual-link if:
3330 * - Need to (i.e. rate > 165MHz)
3331 * - DCB says we can
3332 * - Not an HDMI monitor, since there's no dual-link
3333 * on HDMI.
3334 */
3335 if (mode->clock >= 165000 &&
3336 nv_encoder->dcb->duallink_possible &&
3337 !drm_detect_hdmi_monitor(nv_connector->edid))
3338 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003339 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003340 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003341 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003342
Ben Skeggsf20c6652016-11-04 17:20:36 +10003343 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003344 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003345 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003346 proto = 0x0;
3347
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003348 if (bios->fp_no_ddc) {
3349 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003350 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003351 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003352 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003353 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10003354 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003355 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003356 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003357 } else
3358 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003359 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003360 }
3361
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003362 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003363 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003364 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003365 } else {
3366 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003367 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003368 }
3369
3370 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003371 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003372 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10003373
Ben Skeggsa3761fa2014-08-10 04:10:27 +10003374 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003375 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10003376 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10003377 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003378 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003379 else
3380 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003381 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003382 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10003383 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003384
3385 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003386 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003387 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003388 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10003389
3390 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10003391 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10003392 default:
3393 BUG_ON(1);
3394 break;
3395 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10003396
Ben Skeggse84a35a2014-06-05 10:59:55 +10003397 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003398
Ben Skeggs648d4df2014-08-10 04:10:27 +10003399 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10003400 u32 *push = evo_wait(mast, 3);
3401 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003402 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
3403 u32 syncs = 0x00000001;
3404
3405 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3406 syncs |= 0x00000008;
3407 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3408 syncs |= 0x00000010;
3409
3410 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3411 magic |= 0x00000001;
3412
3413 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
3414 evo_data(push, syncs | (depth << 6));
3415 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10003416 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10003417 }
3418
Ben Skeggse84a35a2014-06-05 10:59:55 +10003419 ctrl = proto << 8;
3420 mask = 0x00000f00;
3421 } else {
3422 ctrl = (depth << 16) | (proto << 8);
3423 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3424 ctrl |= 0x00001000;
3425 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3426 ctrl |= 0x00002000;
3427 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003428 }
3429
Ben Skeggse84a35a2014-06-05 10:59:55 +10003430 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003431}
3432
Ben Skeggsf20c6652016-11-04 17:20:36 +10003433static const struct drm_encoder_helper_funcs
3434nv50_sor_help = {
3435 .dpms = nv50_sor_dpms,
3436 .mode_fixup = nv50_encoder_mode_fixup,
3437 .prepare = nv50_sor_disconnect,
3438 .mode_set = nv50_sor_mode_set,
3439 .disable = nv50_sor_disconnect,
3440 .get_crtc = nv50_display_crtc_get,
3441};
3442
Ben Skeggs83fc0832011-07-05 13:08:40 +10003443static void
Ben Skeggse225f442012-11-21 14:40:21 +10003444nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003445{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003446 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3447 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003448 drm_encoder_cleanup(encoder);
3449 kfree(encoder);
3450}
3451
Ben Skeggsf20c6652016-11-04 17:20:36 +10003452static const struct drm_encoder_funcs
3453nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10003454 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10003455};
3456
3457static int
Ben Skeggse225f442012-11-21 14:40:21 +10003458nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10003459{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003460 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10003461 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10003462 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003463 struct nouveau_encoder *nv_encoder;
3464 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003465 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10003466
3467 switch (dcbe->type) {
3468 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
3469 case DCB_OUTPUT_TMDS:
3470 case DCB_OUTPUT_DP:
3471 default:
3472 type = DRM_MODE_ENCODER_TMDS;
3473 break;
3474 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10003475
3476 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3477 if (!nv_encoder)
3478 return -ENOMEM;
3479 nv_encoder->dcb = dcbe;
3480 nv_encoder->or = ffs(dcbe->or) - 1;
3481 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
3482
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003483 encoder = to_drm_encoder(nv_encoder);
3484 encoder->possible_crtcs = dcbe->heads;
3485 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003486 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
3487 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003488 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003489
3490 drm_mode_connector_attach_encoder(connector, encoder);
3491
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003492 if (dcbe->type == DCB_OUTPUT_DP) {
3493 struct nvkm_i2c_aux *aux =
3494 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
3495 if (aux) {
3496 nv_encoder->i2c = &aux->i2c;
3497 nv_encoder->aux = aux;
3498 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10003499
3500 /*TODO: Use DP Info Table to check for support. */
3501 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
3502 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
3503 nv_connector->base.base.id,
3504 &nv_encoder->dp.mstm);
3505 if (ret)
3506 return ret;
3507 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003508 } else {
3509 struct nvkm_i2c_bus *bus =
3510 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
3511 if (bus)
3512 nv_encoder->i2c = &bus->i2c;
3513 }
3514
Ben Skeggs83fc0832011-07-05 13:08:40 +10003515 return 0;
3516}
Ben Skeggs26f6d882011-07-04 16:25:18 +10003517
3518/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10003519 * PIOR
3520 *****************************************************************************/
Ben Skeggseb6313a2013-02-11 09:52:58 +10003521static void
3522nv50_pior_dpms(struct drm_encoder *encoder, int mode)
3523{
3524 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3525 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10003526 struct {
3527 struct nv50_disp_mthd_v1 base;
3528 struct nv50_disp_pior_pwr_v0 pwr;
3529 } args = {
3530 .base.version = 1,
3531 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
3532 .base.hasht = nv_encoder->dcb->hasht,
3533 .base.hashm = nv_encoder->dcb->hashm,
3534 .pwr.state = mode == DRM_MODE_DPMS_ON,
3535 .pwr.type = nv_encoder->dcb->type,
3536 };
3537
3538 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10003539}
3540
3541static bool
3542nv50_pior_mode_fixup(struct drm_encoder *encoder,
3543 const struct drm_display_mode *mode,
3544 struct drm_display_mode *adjusted_mode)
3545{
Ben Skeggsa91d3222014-12-22 16:30:13 +10003546 if (!nv50_encoder_mode_fixup(encoder, mode, adjusted_mode))
3547 return false;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003548 adjusted_mode->clock *= 2;
3549 return true;
3550}
3551
3552static void
Ben Skeggsf20c6652016-11-04 17:20:36 +10003553nv50_pior_disconnect(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10003554{
Ben Skeggsf20c6652016-11-04 17:20:36 +10003555 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3556 struct nv50_mast *mast = nv50_mast(encoder->dev);
3557 const int or = nv_encoder->or;
3558 u32 *push;
3559
3560 if (nv_encoder->crtc) {
3561 nv50_crtc_prepare(nv_encoder->crtc);
3562
3563 push = evo_wait(mast, 4);
3564 if (push) {
3565 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
3566 evo_mthd(push, 0x0700 + (or * 0x040), 1);
3567 evo_data(push, 0x00000000);
3568 }
3569 evo_kick(push, mast);
3570 }
3571 }
3572
3573 nv_encoder->crtc = NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003574}
3575
3576static void
3577nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
3578 struct drm_display_mode *adjusted_mode)
3579{
3580 struct nv50_mast *mast = nv50_mast(encoder->dev);
3581 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
3582 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3583 struct nouveau_connector *nv_connector;
3584 u8 owner = 1 << nv_crtc->index;
3585 u8 proto, depth;
3586 u32 *push;
3587
3588 nv_connector = nouveau_encoder_connector_get(nv_encoder);
3589 switch (nv_connector->base.display_info.bpc) {
3590 case 10: depth = 0x6; break;
3591 case 8: depth = 0x5; break;
3592 case 6: depth = 0x2; break;
3593 default: depth = 0x0; break;
3594 }
3595
3596 switch (nv_encoder->dcb->type) {
3597 case DCB_OUTPUT_TMDS:
3598 case DCB_OUTPUT_DP:
3599 proto = 0x0;
3600 break;
3601 default:
3602 BUG_ON(1);
3603 break;
3604 }
3605
3606 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
3607
3608 push = evo_wait(mast, 8);
3609 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10003610 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003611 u32 ctrl = (depth << 16) | (proto << 8) | owner;
3612 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
3613 ctrl |= 0x00001000;
3614 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
3615 ctrl |= 0x00002000;
3616 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
3617 evo_data(push, ctrl);
3618 }
3619
3620 evo_kick(push, mast);
3621 }
3622
3623 nv_encoder->crtc = encoder->crtc;
3624}
3625
Ben Skeggsf20c6652016-11-04 17:20:36 +10003626static const struct drm_encoder_helper_funcs
3627nv50_pior_help = {
3628 .dpms = nv50_pior_dpms,
3629 .mode_fixup = nv50_pior_mode_fixup,
3630 .prepare = nv50_pior_disconnect,
3631 .mode_set = nv50_pior_mode_set,
3632 .disable = nv50_pior_disconnect,
3633 .get_crtc = nv50_display_crtc_get,
3634};
Ben Skeggseb6313a2013-02-11 09:52:58 +10003635
3636static void
3637nv50_pior_destroy(struct drm_encoder *encoder)
3638{
3639 drm_encoder_cleanup(encoder);
3640 kfree(encoder);
3641}
3642
Ben Skeggsf20c6652016-11-04 17:20:36 +10003643static const struct drm_encoder_funcs
3644nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10003645 .destroy = nv50_pior_destroy,
3646};
3647
3648static int
3649nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
3650{
3651 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10003652 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003653 struct nvkm_i2c_bus *bus = NULL;
3654 struct nvkm_i2c_aux *aux = NULL;
3655 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003656 struct nouveau_encoder *nv_encoder;
3657 struct drm_encoder *encoder;
3658 int type;
3659
3660 switch (dcbe->type) {
3661 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003662 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
3663 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003664 type = DRM_MODE_ENCODER_TMDS;
3665 break;
3666 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003667 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
3668 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003669 type = DRM_MODE_ENCODER_TMDS;
3670 break;
3671 default:
3672 return -ENODEV;
3673 }
3674
3675 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
3676 if (!nv_encoder)
3677 return -ENOMEM;
3678 nv_encoder->dcb = dcbe;
3679 nv_encoder->or = ffs(dcbe->or) - 1;
3680 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10003681 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10003682
3683 encoder = to_drm_encoder(nv_encoder);
3684 encoder->possible_crtcs = dcbe->heads;
3685 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10003686 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
3687 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10003688 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10003689
3690 drm_mode_connector_attach_encoder(connector, encoder);
3691 return 0;
3692}
3693
3694/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10003695 * Framebuffer
3696 *****************************************************************************/
3697
Ben Skeggs8a423642014-08-10 04:10:19 +10003698static void
Ben Skeggsab0af552014-08-10 04:10:19 +10003699nv50_fb_dtor(struct drm_framebuffer *fb)
3700{
3701}
3702
3703static int
3704nv50_fb_ctor(struct drm_framebuffer *fb)
3705{
3706 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
3707 struct nouveau_drm *drm = nouveau_drm(fb->dev);
3708 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10003709 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10003710 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
3711 u8 tile = nvbo->tile_mode;
Ben Skeggsaccdea22016-11-04 17:20:36 +10003712 struct drm_crtc *crtc;
Ben Skeggsab0af552014-08-10 04:10:19 +10003713
Ben Skeggs967e7bd2014-08-10 04:10:22 +10003714 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10003715 tile >>= 4; /* yep.. */
3716
Ben Skeggsab0af552014-08-10 04:10:19 +10003717 switch (fb->depth) {
3718 case 8: nv_fb->r_format = 0x1e00; break;
3719 case 15: nv_fb->r_format = 0xe900; break;
3720 case 16: nv_fb->r_format = 0xe800; break;
3721 case 24:
3722 case 32: nv_fb->r_format = 0xcf00; break;
3723 case 30: nv_fb->r_format = 0xd100; break;
3724 default:
3725 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
3726 return -EINVAL;
3727 }
3728
Ben Skeggs648d4df2014-08-10 04:10:27 +10003729 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10003730 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3731 (fb->pitches[0] | 0x00100000);
3732 nv_fb->r_format |= kind << 16;
3733 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10003734 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10003735 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3736 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10003737 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10003738 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
3739 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10003740 }
Ben Skeggs8a423642014-08-10 04:10:19 +10003741 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10003742
Ben Skeggsaccdea22016-11-04 17:20:36 +10003743 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
3744 struct nv50_head *head = nv50_head(crtc);
3745 struct nv50_dmac_ctxdma *ctxdma;
3746
3747 ctxdma = nv50_dmac_ctxdma_new(&head->_base->chan.base,
3748 nv_fb->r_handle, nv_fb);
3749 if (IS_ERR(ctxdma))
3750 return PTR_ERR(ctxdma);
3751 }
3752
3753 return 0;
Ben Skeggsab0af552014-08-10 04:10:19 +10003754}
3755
3756/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10003757 * Init
3758 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10003759
Ben Skeggs2a44e492011-11-09 11:36:33 +10003760void
Ben Skeggse225f442012-11-21 14:40:21 +10003761nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003762{
Ben Skeggs973f10c2016-11-04 17:20:36 +10003763 struct drm_plane *plane;
3764
3765 drm_for_each_plane(plane, dev) {
3766 struct nv50_wndw *wndw = nv50_wndw(plane);
3767 if (plane->funcs != &nv50_wndw)
3768 continue;
3769 nv50_wndw_fini(wndw);
3770 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10003771}
3772
3773int
Ben Skeggse225f442012-11-21 14:40:21 +10003774nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003775{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003776 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs354d3502016-11-04 17:20:36 +10003777 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10003778 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003779 struct drm_crtc *crtc;
3780 u32 *push;
3781
3782 push = evo_wait(nv50_mast(dev), 32);
3783 if (!push)
3784 return -EBUSY;
3785
3786 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs973f10c2016-11-04 17:20:36 +10003787 struct nv50_wndw *wndw = &nv50_head(crtc)->_base->wndw;
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01003788
3789 nv50_crtc_lut_load(crtc);
Ben Skeggs973f10c2016-11-04 17:20:36 +10003790 nouveau_bo_wr32(disp->sync, wndw->sema / 4, wndw->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003791 }
3792
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003793 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10003794 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003795 evo_kick(push, nv50_mast(dev));
Ben Skeggs973f10c2016-11-04 17:20:36 +10003796
Ben Skeggs354d3502016-11-04 17:20:36 +10003797 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3798 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
3799 const struct drm_encoder_helper_funcs *help;
3800 struct nouveau_encoder *nv_encoder;
3801
3802 nv_encoder = nouveau_encoder(encoder);
3803 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
3804 nv_encoder->dcb->type = DCB_OUTPUT_EOL;
3805
3806 help = encoder->helper_private;
3807 if (help && help->dpms)
3808 help->dpms(encoder, DRM_MODE_DPMS_ON);
3809
3810 if (nv_encoder->dcb->type == DCB_OUTPUT_EOL)
3811 nv_encoder->dcb->type = DCB_OUTPUT_DP;
3812 }
3813 }
3814
Ben Skeggs973f10c2016-11-04 17:20:36 +10003815 drm_for_each_plane(plane, dev) {
3816 struct nv50_wndw *wndw = nv50_wndw(plane);
3817 if (plane->funcs != &nv50_wndw)
3818 continue;
3819 nv50_wndw_init(wndw);
3820 }
3821
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003822 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003823}
3824
3825void
Ben Skeggse225f442012-11-21 14:40:21 +10003826nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003827{
Ben Skeggse225f442012-11-21 14:40:21 +10003828 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003829
Ben Skeggs0ad72862014-08-10 04:10:22 +10003830 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10003831
Ben Skeggs816af2f2011-11-16 15:48:48 +10003832 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003833 if (disp->sync)
3834 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10003835 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10003836
Ben Skeggs77145f12012-07-31 16:16:21 +10003837 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003838 kfree(disp);
3839}
3840
3841int
Ben Skeggse225f442012-11-21 14:40:21 +10003842nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003843{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10003844 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10003845 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10003846 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003847 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10003848 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10003849 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10003850 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003851
3852 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
3853 if (!disp)
3854 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10003855
3856 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10003857 nouveau_display(dev)->dtor = nv50_display_destroy;
3858 nouveau_display(dev)->init = nv50_display_init;
3859 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10003860 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
3861 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10003862 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003863
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003864 /* small shared memory area we use for notifiers and semaphores */
3865 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01003866 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003867 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10003868 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003869 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003870 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003871 if (ret)
3872 nouveau_bo_unpin(disp->sync);
3873 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003874 if (ret)
3875 nouveau_bo_ref(NULL, &disp->sync);
3876 }
3877
3878 if (ret)
3879 goto out;
3880
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003881 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10003882 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10003883 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003884 if (ret)
3885 goto out;
3886
Ben Skeggs438d99e2011-07-05 16:48:06 +10003887 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10003888 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsa01ca782015-08-20 14:54:15 +10003889 crtcs = nvif_rd32(&device->object, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10003890 else
3891 crtcs = 2;
3892
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10003893 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10003894 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10003895 if (ret)
3896 goto out;
3897 }
3898
Ben Skeggs83fc0832011-07-05 13:08:40 +10003899 /* create encoder/connector objects based on VBIOS DCB table */
3900 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
3901 connector = nouveau_connector_create(dev, dcbe->connector);
3902 if (IS_ERR(connector))
3903 continue;
3904
Ben Skeggseb6313a2013-02-11 09:52:58 +10003905 if (dcbe->location == DCB_LOC_ON_CHIP) {
3906 switch (dcbe->type) {
3907 case DCB_OUTPUT_TMDS:
3908 case DCB_OUTPUT_LVDS:
3909 case DCB_OUTPUT_DP:
3910 ret = nv50_sor_create(connector, dcbe);
3911 break;
3912 case DCB_OUTPUT_ANALOG:
3913 ret = nv50_dac_create(connector, dcbe);
3914 break;
3915 default:
3916 ret = -ENODEV;
3917 break;
3918 }
3919 } else {
3920 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003921 }
3922
Ben Skeggseb6313a2013-02-11 09:52:58 +10003923 if (ret) {
3924 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
3925 dcbe->location, dcbe->type,
3926 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10003927 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003928 }
3929 }
3930
3931 /* cull any connectors we created that don't have an encoder */
3932 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
3933 if (connector->encoder_ids[0])
3934 continue;
3935
Ben Skeggs77145f12012-07-31 16:16:21 +10003936 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03003937 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003938 connector->funcs->destroy(connector);
3939 }
3940
Ben Skeggs26f6d882011-07-04 16:25:18 +10003941out:
3942 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10003943 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003944 return ret;
3945}