blob: a5e9599e183fe307a65d027e02950c8951ca563e [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>
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drm_crtc_helper.h>
Ben Skeggs48743222014-05-31 01:48:06 +100030#include <drm/drm_dp_helper.h>
Daniel Vetterb516a9e2015-12-04 09:45:43 +010031#include <drm/drm_fb_helper.h>
Ben Skeggsad633612016-11-04 17:20:36 +100032#include <drm/drm_plane_helper.h>
Ben Skeggs26f6d882011-07-04 16:25:18 +100033
Ben Skeggsfdb751e2014-08-10 04:10:23 +100034#include <nvif/class.h>
Ben Skeggs845f2722015-11-08 12:16:40 +100035#include <nvif/cl0002.h>
Ben Skeggs7568b102015-11-08 10:44:19 +100036#include <nvif/cl5070.h>
37#include <nvif/cl507a.h>
38#include <nvif/cl507b.h>
39#include <nvif/cl507c.h>
40#include <nvif/cl507d.h>
41#include <nvif/cl507e.h>
Ben Skeggsfdb751e2014-08-10 04:10:23 +100042
Ben Skeggs4dc28132016-05-20 09:22:55 +100043#include "nouveau_drv.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100044#include "nouveau_dma.h"
45#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100046#include "nouveau_connector.h"
47#include "nouveau_encoder.h"
48#include "nouveau_crtc.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100049#include "nouveau_fence.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100050#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100051
Ben Skeggs8a464382011-11-12 23:52:07 +100052#define EVO_DMA_NR 9
53
Ben Skeggsbdb8c212011-11-12 01:30:24 +100054#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100055#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100056#define EVO_OVLY(c) (0x05 + (c))
57#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100058#define EVO_CURS(c) (0x0d + (c))
59
Ben Skeggs816af2f2011-11-16 15:48:48 +100060/* offsets in shared sync bo of various structures */
61#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100062#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
63#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
64#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs816af2f2011-11-16 15:48:48 +100065
Ben Skeggsb5a794b2012-10-16 14:18:32 +100066/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +100067 * Atomic state
68 *****************************************************************************/
69#define nv50_head_atom(p) container_of((p), struct nv50_head_atom, state)
70
71struct nv50_head_atom {
72 struct drm_crtc_state state;
73
Ben Skeggsc4e68122016-11-04 17:20:36 +100074 struct {
75 u16 iW;
76 u16 iH;
77 u16 oW;
78 u16 oH;
79 } view;
80
Ben Skeggs3dbd0362016-11-04 17:20:36 +100081 struct nv50_head_mode {
82 bool interlace;
83 u32 clock;
84 struct {
85 u16 active;
86 u16 synce;
87 u16 blanke;
88 u16 blanks;
89 } h;
90 struct {
91 u32 active;
92 u16 synce;
93 u16 blanke;
94 u16 blanks;
95 u16 blank2s;
96 u16 blank2e;
97 u16 blankus;
98 } v;
99 } mode;
100
Ben Skeggsad633612016-11-04 17:20:36 +1000101 struct {
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000102 u32 handle;
103 u64 offset:40;
104 } lut;
105
106 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000107 bool visible;
108 u32 handle;
109 u64 offset:40;
110 u8 format;
111 u8 kind:7;
112 u8 layout:1;
113 u8 block:4;
114 u32 pitch:20;
115 u16 x;
116 u16 y;
117 u16 w;
118 u16 h;
119 } core;
120
121 struct {
Ben Skeggsea8ee392016-11-04 17:20:36 +1000122 bool visible;
123 u32 handle;
124 u64 offset:40;
125 u8 layout:1;
126 u8 format:1;
127 } curs;
128
129 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000130 u8 depth;
131 u8 cpp;
132 u16 x;
133 u16 y;
134 u16 w;
135 u16 h;
136 } base;
137
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000138 struct {
139 u8 cpp;
140 } ovly;
141
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000142 union {
143 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000144 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000145 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000146 };
147 u8 mask;
148 } clr;
149
150 union {
151 struct {
152 bool core:1;
Ben Skeggsea8ee392016-11-04 17:20:36 +1000153 bool curs:1;
Ben Skeggsad633612016-11-04 17:20:36 +1000154 bool view:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000155 bool mode:1;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000156 bool base:1;
157 bool ovly:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000158 };
159 u16 mask;
160 } set;
161};
162
163/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000164 * EVO channel
165 *****************************************************************************/
166
Ben Skeggse225f442012-11-21 14:40:21 +1000167struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000168 struct nvif_object user;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000169 struct nvif_device *device;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000170};
171
172static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000173nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000174 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000175 struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000176{
Ben Skeggs41a63402015-08-20 14:54:16 +1000177 struct nvif_sclass *sclass;
178 int ret, i, n;
Ben Skeggs6af52892014-11-03 15:01:33 +1000179
Ben Skeggsa01ca782015-08-20 14:54:15 +1000180 chan->device = device;
181
Ben Skeggs41a63402015-08-20 14:54:16 +1000182 ret = n = nvif_object_sclass_get(disp, &sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000183 if (ret < 0)
184 return ret;
185
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000186 while (oclass[0]) {
Ben Skeggs41a63402015-08-20 14:54:16 +1000187 for (i = 0; i < n; i++) {
188 if (sclass[i].oclass == oclass[0]) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000189 ret = nvif_object_init(disp, 0, oclass[0],
Ben Skeggsa01ca782015-08-20 14:54:15 +1000190 data, size, &chan->user);
Ben Skeggs6af52892014-11-03 15:01:33 +1000191 if (ret == 0)
192 nvif_object_map(&chan->user);
Ben Skeggs41a63402015-08-20 14:54:16 +1000193 nvif_object_sclass_put(&sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000194 return ret;
195 }
Ben Skeggsb76f1522014-08-10 04:10:28 +1000196 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000197 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000198 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000199
Ben Skeggs41a63402015-08-20 14:54:16 +1000200 nvif_object_sclass_put(&sclass);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000201 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000202}
203
204static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000205nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000206{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000207 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000208}
209
210/******************************************************************************
211 * PIO EVO channel
212 *****************************************************************************/
213
Ben Skeggse225f442012-11-21 14:40:21 +1000214struct nv50_pioc {
215 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000216};
217
218static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000219nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000220{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000221 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000222}
223
224static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000225nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000226 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000227 struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000228{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000229 return nv50_chan_create(device, disp, oclass, head, data, size,
230 &pioc->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000231}
232
233/******************************************************************************
234 * Cursor Immediate
235 *****************************************************************************/
236
237struct nv50_curs {
238 struct nv50_pioc base;
239};
240
241static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000242nv50_curs_create(struct nvif_device *device, struct nvif_object *disp,
243 int head, struct nv50_curs *curs)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000244{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000245 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000246 .head = head,
247 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000248 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000249 GK104_DISP_CURSOR,
250 GF110_DISP_CURSOR,
251 GT214_DISP_CURSOR,
252 G82_DISP_CURSOR,
253 NV50_DISP_CURSOR,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000254 0
255 };
256
Ben Skeggsa01ca782015-08-20 14:54:15 +1000257 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
258 &curs->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000259}
260
261/******************************************************************************
262 * Overlay Immediate
263 *****************************************************************************/
264
265struct nv50_oimm {
266 struct nv50_pioc base;
267};
268
269static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000270nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
271 int head, struct nv50_oimm *oimm)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000272{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000273 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000274 .head = head,
275 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000276 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000277 GK104_DISP_OVERLAY,
278 GF110_DISP_OVERLAY,
279 GT214_DISP_OVERLAY,
280 G82_DISP_OVERLAY,
281 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000282 0
283 };
284
Ben Skeggsa01ca782015-08-20 14:54:15 +1000285 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
286 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000287}
288
289/******************************************************************************
290 * DMA EVO channel
291 *****************************************************************************/
292
Ben Skeggse225f442012-11-21 14:40:21 +1000293struct nv50_dmac {
294 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000295 dma_addr_t handle;
296 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100297
Ben Skeggs0ad72862014-08-10 04:10:22 +1000298 struct nvif_object sync;
299 struct nvif_object vram;
300
Daniel Vetter59ad1462012-12-02 14:49:44 +0100301 /* Protects against concurrent pushbuf access to this channel, lock is
302 * grabbed by evo_wait (if the pushbuf reservation is successful) and
303 * dropped again by evo_kick. */
304 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000305};
306
307static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000308nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000309{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000310 struct nvif_device *device = dmac->base.device;
311
Ben Skeggs0ad72862014-08-10 04:10:22 +1000312 nvif_object_fini(&dmac->vram);
313 nvif_object_fini(&dmac->sync);
314
315 nv50_chan_destroy(&dmac->base);
316
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000317 if (dmac->ptr) {
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000318 struct device *dev = nvxx_device(device)->dev;
319 dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000320 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000321}
322
323static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000324nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000325 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000326 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000327{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000328 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000329 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000330 int ret;
331
Daniel Vetter59ad1462012-12-02 14:49:44 +0100332 mutex_init(&dmac->lock);
333
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000334 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
335 &dmac->handle, GFP_KERNEL);
Ben Skeggs47057302012-11-16 13:58:48 +1000336 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000337 return -ENOMEM;
338
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000339 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
340 &(struct nv_dma_v0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +1000341 .target = NV_DMA_V0_TARGET_PCI_US,
342 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000343 .start = dmac->handle + 0x0000,
344 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000345 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000346 if (ret)
347 return ret;
348
Ben Skeggsbf81df92015-08-20 14:54:16 +1000349 args->pushbuf = nvif_handle(&pushbuf);
350
Ben Skeggsa01ca782015-08-20 14:54:15 +1000351 ret = nv50_chan_create(device, disp, oclass, head, data, size,
352 &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000353 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000354 if (ret)
355 return ret;
356
Ben Skeggsa01ca782015-08-20 14:54:15 +1000357 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000358 &(struct nv_dma_v0) {
359 .target = NV_DMA_V0_TARGET_VRAM,
360 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000361 .start = syncbuf + 0x0000,
362 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000363 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000364 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000365 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000366 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000367
Ben Skeggsa01ca782015-08-20 14:54:15 +1000368 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000369 &(struct nv_dma_v0) {
370 .target = NV_DMA_V0_TARGET_VRAM,
371 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000372 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000373 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000374 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000375 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000376 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000377 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000378
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000379 return ret;
380}
381
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000382/******************************************************************************
383 * Core
384 *****************************************************************************/
385
Ben Skeggse225f442012-11-21 14:40:21 +1000386struct nv50_mast {
387 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000388};
389
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000390static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000391nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
392 u64 syncbuf, struct nv50_mast *core)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000393{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000394 struct nv50_disp_core_channel_dma_v0 args = {
395 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000396 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000397 static const s32 oclass[] = {
Ben Skeggsfd478772016-07-09 10:41:01 +1000398 GP104_DISP_CORE_CHANNEL_DMA,
Ben Skeggsf9d5cbb2016-07-09 10:41:01 +1000399 GP100_DISP_CORE_CHANNEL_DMA,
Ben Skeggsdb1eb522016-02-11 08:35:32 +1000400 GM200_DISP_CORE_CHANNEL_DMA,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000401 GM107_DISP_CORE_CHANNEL_DMA,
402 GK110_DISP_CORE_CHANNEL_DMA,
403 GK104_DISP_CORE_CHANNEL_DMA,
404 GF110_DISP_CORE_CHANNEL_DMA,
405 GT214_DISP_CORE_CHANNEL_DMA,
406 GT206_DISP_CORE_CHANNEL_DMA,
407 GT200_DISP_CORE_CHANNEL_DMA,
408 G82_DISP_CORE_CHANNEL_DMA,
409 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000410 0
411 };
412
Ben Skeggsa01ca782015-08-20 14:54:15 +1000413 return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
414 syncbuf, &core->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000415}
416
417/******************************************************************************
418 * Base
419 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000420
Ben Skeggse225f442012-11-21 14:40:21 +1000421struct nv50_sync {
422 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000423 u32 addr;
424 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000425};
426
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000427static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000428nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
429 int head, u64 syncbuf, struct nv50_sync *base)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000430{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000431 struct nv50_disp_base_channel_dma_v0 args = {
432 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000433 .head = head,
434 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000435 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000436 GK110_DISP_BASE_CHANNEL_DMA,
437 GK104_DISP_BASE_CHANNEL_DMA,
438 GF110_DISP_BASE_CHANNEL_DMA,
439 GT214_DISP_BASE_CHANNEL_DMA,
440 GT200_DISP_BASE_CHANNEL_DMA,
441 G82_DISP_BASE_CHANNEL_DMA,
442 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000443 0
444 };
445
Ben Skeggsa01ca782015-08-20 14:54:15 +1000446 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000447 syncbuf, &base->base);
448}
449
450/******************************************************************************
451 * Overlay
452 *****************************************************************************/
453
Ben Skeggse225f442012-11-21 14:40:21 +1000454struct nv50_ovly {
455 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000456};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000457
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000458static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000459nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
460 int head, u64 syncbuf, struct nv50_ovly *ovly)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000461{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000462 struct nv50_disp_overlay_channel_dma_v0 args = {
463 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000464 .head = head,
465 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000466 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000467 GK104_DISP_OVERLAY_CONTROL_DMA,
468 GF110_DISP_OVERLAY_CONTROL_DMA,
469 GT214_DISP_OVERLAY_CHANNEL_DMA,
470 GT200_DISP_OVERLAY_CHANNEL_DMA,
471 G82_DISP_OVERLAY_CHANNEL_DMA,
472 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000473 0
474 };
475
Ben Skeggsa01ca782015-08-20 14:54:15 +1000476 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000477 syncbuf, &ovly->base);
478}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000479
Ben Skeggse225f442012-11-21 14:40:21 +1000480struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000481 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000482 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000483 struct nv50_curs curs;
484 struct nv50_sync sync;
485 struct nv50_ovly ovly;
486 struct nv50_oimm oimm;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000487
488 struct nv50_head_atom arm;
489 struct nv50_head_atom asy;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000490};
491
Ben Skeggse225f442012-11-21 14:40:21 +1000492#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
493#define nv50_curs(c) (&nv50_head(c)->curs)
494#define nv50_sync(c) (&nv50_head(c)->sync)
495#define nv50_ovly(c) (&nv50_head(c)->ovly)
496#define nv50_oimm(c) (&nv50_head(c)->oimm)
497#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000498#define nv50_vers(c) nv50_chan(c)->user.oclass
499
500struct nv50_fbdma {
501 struct list_head head;
502 struct nvif_object core;
503 struct nvif_object base[4];
504};
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000505
Ben Skeggse225f442012-11-21 14:40:21 +1000506struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000507 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000508 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000509
Ben Skeggs8a423642014-08-10 04:10:19 +1000510 struct list_head fbdma;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000511
512 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000513};
514
Ben Skeggse225f442012-11-21 14:40:21 +1000515static struct nv50_disp *
516nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000517{
Ben Skeggs77145f12012-07-31 16:16:21 +1000518 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000519}
520
Ben Skeggse225f442012-11-21 14:40:21 +1000521#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000522
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000523static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000524nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000525{
526 return nouveau_encoder(encoder)->crtc;
527}
528
529/******************************************************************************
530 * EVO channel helpers
531 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000532static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000533evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000534{
Ben Skeggse225f442012-11-21 14:40:21 +1000535 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000536 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000537 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000538
Daniel Vetter59ad1462012-12-02 14:49:44 +0100539 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000540 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000541 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000542
Ben Skeggs0ad72862014-08-10 04:10:22 +1000543 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000544 if (nvif_msec(device, 2000,
545 if (!nvif_rd32(&dmac->base.user, 0x0004))
546 break;
547 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100548 mutex_unlock(&dmac->lock);
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000549 printk(KERN_ERR "nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000550 return NULL;
551 }
552
553 put = 0;
554 }
555
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000556 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000557}
558
559static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000560evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000561{
Ben Skeggse225f442012-11-21 14:40:21 +1000562 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000563 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100564 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000565}
566
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000567#define evo_mthd(p,m,s) do { \
568 const u32 _m = (m), _s = (s); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000569 if (drm_debug & DRM_UT_KMS) \
570 printk(KERN_ERR "%04x %d %s\n", _m, _s, __func__); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000571 *((p)++) = ((_s << 18) | _m); \
572} while(0)
Ben Skeggs7f55a072016-11-04 17:20:36 +1000573
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000574#define evo_data(p,d) do { \
575 const u32 _d = (d); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000576 if (drm_debug & DRM_UT_KMS) \
577 printk(KERN_ERR "\t%08x\n", _d); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000578 *((p)++) = _d; \
579} while(0)
Ben Skeggs51beb422011-07-05 10:33:08 +1000580
Ben Skeggs3376ee32011-11-12 14:28:12 +1000581static bool
582evo_sync_wait(void *data)
583{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500584 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
585 return true;
586 usleep_range(1, 2);
587 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000588}
589
590static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000591evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000592{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000593 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000594 struct nv50_disp *disp = nv50_disp(dev);
595 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000596 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000597 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000598 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000599 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000600 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000601 evo_mthd(push, 0x0080, 2);
602 evo_data(push, 0x00000000);
603 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000604 evo_kick(push, mast);
Ben Skeggs54442042015-08-20 14:54:11 +1000605 if (nvif_msec(device, 2000,
606 if (evo_sync_wait(disp->sync))
607 break;
608 ) >= 0)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000609 return 0;
610 }
611
612 return -EBUSY;
613}
614
615/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000616 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000617 *****************************************************************************/
618struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000619nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000620{
Ben Skeggse225f442012-11-21 14:40:21 +1000621 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000622}
623
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000624struct nv50_display_flip {
625 struct nv50_disp *disp;
626 struct nv50_sync *chan;
627};
628
629static bool
630nv50_display_flip_wait(void *data)
631{
632 struct nv50_display_flip *flip = data;
633 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500634 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000635 return true;
636 usleep_range(1, 2);
637 return false;
638}
639
Ben Skeggs3376ee32011-11-12 14:28:12 +1000640void
Ben Skeggse225f442012-11-21 14:40:21 +1000641nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000642{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000643 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000644 struct nv50_display_flip flip = {
645 .disp = nv50_disp(crtc->dev),
646 .chan = nv50_sync(crtc),
647 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000648 u32 *push;
649
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000650 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000651 if (push) {
652 evo_mthd(push, 0x0084, 1);
653 evo_data(push, 0x00000000);
654 evo_mthd(push, 0x0094, 1);
655 evo_data(push, 0x00000000);
656 evo_mthd(push, 0x00c0, 1);
657 evo_data(push, 0x00000000);
658 evo_mthd(push, 0x0080, 1);
659 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000660 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000661 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000662
Ben Skeggs54442042015-08-20 14:54:11 +1000663 nvif_msec(device, 2000,
664 if (nv50_display_flip_wait(&flip))
665 break;
666 );
Ben Skeggs3376ee32011-11-12 14:28:12 +1000667}
668
669int
Ben Skeggse225f442012-11-21 14:40:21 +1000670nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000671 struct nouveau_channel *chan, u32 swap_interval)
672{
673 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000674 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000675 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000676 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000677 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000678 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000679
Ben Skeggs9ba83102014-12-22 19:50:23 +1000680 if (crtc->primary->fb->width != fb->width ||
681 crtc->primary->fb->height != fb->height)
682 return -EINVAL;
683
Ben Skeggs3376ee32011-11-12 14:28:12 +1000684 swap_interval <<= 4;
685 if (swap_interval == 0)
686 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000687 if (chan == NULL)
688 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000689
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000690 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000691 if (unlikely(push == NULL))
692 return -EBUSY;
693
Ben Skeggsa01ca782015-08-20 14:54:15 +1000694 if (chan && chan->user.oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000695 ret = RING_SPACE(chan, 8);
696 if (ret)
697 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000698
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000699 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000700 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000701 OUT_RING (chan, sync->addr ^ 0x10);
702 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
703 OUT_RING (chan, sync->data + 1);
704 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
705 OUT_RING (chan, sync->addr);
706 OUT_RING (chan, sync->data);
707 } else
Ben Skeggsa01ca782015-08-20 14:54:15 +1000708 if (chan && chan->user.oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000709 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000710 ret = RING_SPACE(chan, 12);
711 if (ret)
712 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000713
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000714 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000715 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000716 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
717 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
718 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
719 OUT_RING (chan, sync->data + 1);
720 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
721 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
722 OUT_RING (chan, upper_32_bits(addr));
723 OUT_RING (chan, lower_32_bits(addr));
724 OUT_RING (chan, sync->data);
725 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
726 } else
727 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000728 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000729 ret = RING_SPACE(chan, 10);
730 if (ret)
731 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000732
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000733 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
734 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
735 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
736 OUT_RING (chan, sync->data + 1);
737 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
738 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
739 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
740 OUT_RING (chan, upper_32_bits(addr));
741 OUT_RING (chan, lower_32_bits(addr));
742 OUT_RING (chan, sync->data);
743 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
744 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
745 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500746
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000747 if (chan) {
748 sync->addr ^= 0x10;
749 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000750 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000751 }
752
753 /* queue the flip */
754 evo_mthd(push, 0x0100, 1);
755 evo_data(push, 0xfffe0000);
756 evo_mthd(push, 0x0084, 1);
757 evo_data(push, swap_interval);
758 if (!(swap_interval & 0x00000100)) {
759 evo_mthd(push, 0x00e0, 1);
760 evo_data(push, 0x40000000);
761 }
762 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000763 evo_data(push, sync->addr);
764 evo_data(push, sync->data++);
765 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000766 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000767 evo_mthd(push, 0x00a0, 2);
768 evo_data(push, 0x00000000);
769 evo_data(push, 0x00000000);
770 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000771 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000772 evo_mthd(push, 0x0110, 2);
773 evo_data(push, 0x00000000);
774 evo_data(push, 0x00000000);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000775 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000776 evo_mthd(push, 0x0800, 5);
777 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
778 evo_data(push, 0);
779 evo_data(push, (fb->height << 16) | fb->width);
780 evo_data(push, nv_fb->r_pitch);
781 evo_data(push, nv_fb->r_format);
782 } else {
783 evo_mthd(push, 0x0400, 5);
784 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
785 evo_data(push, 0);
786 evo_data(push, (fb->height << 16) | fb->width);
787 evo_data(push, nv_fb->r_pitch);
788 evo_data(push, nv_fb->r_format);
789 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000790 evo_mthd(push, 0x0080, 1);
791 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000792 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000793
794 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000795 return 0;
796}
797
Ben Skeggs26f6d882011-07-04 16:25:18 +1000798/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000799 * Head
800 *****************************************************************************/
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000801static void
Ben Skeggs6bbab3b2016-11-04 17:20:36 +1000802nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
803{
804 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
805 u32 bounds = 0;
806 u32 *push;
807
808 if (asyh->base.cpp) {
809 switch (asyh->base.cpp) {
810 case 8: bounds |= 0x00000500; break;
811 case 4: bounds |= 0x00000300; break;
812 case 2: bounds |= 0x00000100; break;
813 default:
814 WARN_ON(1);
815 break;
816 }
817 bounds |= 0x00000001;
818 }
819
820 if ((push = evo_wait(core, 2))) {
821 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
822 evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
823 else
824 evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
825 evo_data(push, bounds);
826 evo_kick(push, core);
827 }
828}
829
830static void
831nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
832{
833 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
834 u32 bounds = 0;
835 u32 *push;
836
837 if (asyh->base.cpp) {
838 switch (asyh->base.cpp) {
839 case 8: bounds |= 0x00000500; break;
840 case 4: bounds |= 0x00000300; break;
841 case 2: bounds |= 0x00000100; break;
842 case 1: bounds |= 0x00000000; break;
843 default:
844 WARN_ON(1);
845 break;
846 }
847 bounds |= 0x00000001;
848 }
849
850 if ((push = evo_wait(core, 2))) {
851 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
852 evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
853 else
854 evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
855 evo_data(push, bounds);
856 evo_kick(push, core);
857 }
858}
859
860static void
Ben Skeggsea8ee392016-11-04 17:20:36 +1000861nv50_head_curs_clr(struct nv50_head *head)
862{
863 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
864 u32 *push;
865 if ((push = evo_wait(core, 4))) {
866 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
867 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
868 evo_data(push, 0x05000000);
869 } else
870 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
871 evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
872 evo_data(push, 0x05000000);
873 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
874 evo_data(push, 0x00000000);
875 } else {
876 evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
877 evo_data(push, 0x05000000);
878 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
879 evo_data(push, 0x00000000);
880 }
881 evo_kick(push, core);
882 }
883}
884
885static void
886nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
887{
888 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
889 u32 *push;
890 if ((push = evo_wait(core, 5))) {
891 if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
892 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
893 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
894 (asyh->curs.format << 24));
895 evo_data(push, asyh->curs.offset >> 8);
896 } else
897 if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
898 evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
899 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
900 (asyh->curs.format << 24));
901 evo_data(push, asyh->curs.offset >> 8);
902 evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
903 evo_data(push, asyh->curs.handle);
904 } else {
905 evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
906 evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
907 (asyh->curs.format << 24));
908 evo_data(push, asyh->curs.offset >> 8);
909 evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
910 evo_data(push, asyh->curs.handle);
911 }
912 evo_kick(push, core);
913 }
914}
915
916static void
Ben Skeggsad633612016-11-04 17:20:36 +1000917nv50_head_core_clr(struct nv50_head *head)
918{
919 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
920 u32 *push;
921 if ((push = evo_wait(core, 2))) {
922 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
923 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
924 else
925 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
926 evo_data(push, 0x00000000);
927 evo_kick(push, core);
928 }
929}
930
931static void
932nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
933{
934 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
935 u32 *push;
936 if ((push = evo_wait(core, 9))) {
937 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
938 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
939 evo_data(push, asyh->core.offset >> 8);
940 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
941 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
942 evo_data(push, asyh->core.layout << 20 |
943 (asyh->core.pitch >> 8) << 8 |
944 asyh->core.block);
945 evo_data(push, asyh->core.kind << 16 |
946 asyh->core.format << 8);
947 evo_data(push, asyh->core.handle);
948 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
949 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
950 } else
951 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
952 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
953 evo_data(push, asyh->core.offset >> 8);
954 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
955 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
956 evo_data(push, asyh->core.layout << 20 |
957 (asyh->core.pitch >> 8) << 8 |
958 asyh->core.block);
959 evo_data(push, asyh->core.format << 8);
960 evo_data(push, asyh->core.handle);
961 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
962 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
963 } else {
964 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
965 evo_data(push, asyh->core.offset >> 8);
966 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
967 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
968 evo_data(push, asyh->core.layout << 24 |
969 (asyh->core.pitch >> 8) << 8 |
970 asyh->core.block);
971 evo_data(push, asyh->core.format << 8);
972 evo_data(push, asyh->core.handle);
973 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
974 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
975 }
976 evo_kick(push, core);
977 }
978}
979
980static void
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000981nv50_head_lut_clr(struct nv50_head *head)
982{
983 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
984 u32 *push;
985 if ((push = evo_wait(core, 4))) {
986 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
987 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
988 evo_data(push, 0x40000000);
989 } else
990 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
991 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
992 evo_data(push, 0x40000000);
993 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
994 evo_data(push, 0x00000000);
995 } else {
996 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
997 evo_data(push, 0x03000000);
998 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
999 evo_data(push, 0x00000000);
1000 }
1001 evo_kick(push, core);
1002 }
1003}
1004
1005static void
1006nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1007{
1008 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1009 u32 *push;
1010 if ((push = evo_wait(core, 7))) {
1011 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
1012 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1013 evo_data(push, 0xc0000000);
1014 evo_data(push, asyh->lut.offset >> 8);
1015 } else
1016 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1017 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
1018 evo_data(push, 0xc0000000);
1019 evo_data(push, asyh->lut.offset >> 8);
1020 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
1021 evo_data(push, asyh->lut.handle);
1022 } else {
1023 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
1024 evo_data(push, 0x83000000);
1025 evo_data(push, asyh->lut.offset >> 8);
1026 evo_data(push, 0x00000000);
1027 evo_data(push, 0x00000000);
1028 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
1029 evo_data(push, asyh->lut.handle);
1030 }
1031 evo_kick(push, core);
1032 }
1033}
1034
1035static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001036nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1037{
1038 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1039 struct nv50_head_mode *m = &asyh->mode;
1040 u32 *push;
1041 if ((push = evo_wait(core, 14))) {
1042 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1043 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
1044 evo_data(push, 0x00800000 | m->clock);
1045 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
1046 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 6);
1047 evo_data(push, 0x00000000);
1048 evo_data(push, (m->v.active << 16) | m->h.active );
1049 evo_data(push, (m->v.synce << 16) | m->h.synce );
1050 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1051 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1052 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
1053 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
1054 evo_data(push, 0x00000000);
1055 } else {
1056 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
1057 evo_data(push, 0x00000000);
1058 evo_data(push, (m->v.active << 16) | m->h.active );
1059 evo_data(push, (m->v.synce << 16) | m->h.synce );
1060 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
1061 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
1062 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
1063 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
1064 evo_data(push, 0x00000000); /* ??? */
1065 evo_data(push, 0xffffff00);
1066 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
1067 evo_data(push, m->clock * 1000);
1068 evo_data(push, 0x00200000); /* ??? */
1069 evo_data(push, m->clock * 1000);
1070 }
1071 evo_kick(push, core);
1072 }
1073}
1074
1075static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001076nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
1077{
1078 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
1079 u32 *push;
1080 if ((push = evo_wait(core, 10))) {
1081 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
1082 evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
1083 evo_data(push, 0x00000000);
1084 evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
1085 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1086 evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
1087 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1088 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1089 } else {
1090 evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
1091 evo_data(push, 0x00000000);
1092 evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
1093 evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
1094 evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
1095 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1096 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1097 evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
1098 }
1099 evo_kick(push, core);
1100 }
1101}
1102
1103static void
Ben Skeggsad633612016-11-04 17:20:36 +10001104nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
1105{
1106 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001107 nv50_head_lut_clr(head);
1108 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsad633612016-11-04 17:20:36 +10001109 nv50_head_core_clr(head);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001110 if (asyh->clr.curs && (!asyh->set.curs || y))
1111 nv50_head_curs_clr(head);
Ben Skeggsad633612016-11-04 17:20:36 +10001112}
1113
1114static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001115nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
1116{
Ben Skeggsc4e68122016-11-04 17:20:36 +10001117 if (asyh->set.view ) nv50_head_view (head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001118 if (asyh->set.mode ) nv50_head_mode (head, asyh);
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001119 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10001120 if (asyh->set.core ) nv50_head_core_set(head, asyh);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001121 if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001122 if (asyh->set.base ) nv50_head_base (head, asyh);
1123 if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001124}
1125
1126static void
Ben Skeggsc4e68122016-11-04 17:20:36 +10001127nv50_head_atomic_check_view(struct nv50_head_atom *armh,
1128 struct nv50_head_atom *asyh,
1129 struct nouveau_conn_atom *asyc)
1130{
1131 struct drm_connector *connector = asyc->state.connector;
1132 struct drm_display_mode *omode = &asyh->state.adjusted_mode;
1133 struct drm_display_mode *umode = &asyh->state.mode;
1134 int mode = asyc->scaler.mode;
1135 struct edid *edid;
1136
1137 if (connector->edid_blob_ptr)
1138 edid = (struct edid *)connector->edid_blob_ptr->data;
1139 else
1140 edid = NULL;
1141
1142 if (!asyc->scaler.full) {
1143 if (mode == DRM_MODE_SCALE_NONE)
1144 omode = umode;
1145 } else {
1146 /* Non-EDID LVDS/eDP mode. */
1147 mode = DRM_MODE_SCALE_FULLSCREEN;
1148 }
1149
1150 asyh->view.iW = umode->hdisplay;
1151 asyh->view.iH = umode->vdisplay;
1152 asyh->view.oW = omode->hdisplay;
1153 asyh->view.oH = omode->vdisplay;
1154 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
1155 asyh->view.oH *= 2;
1156
1157 /* Add overscan compensation if necessary, will keep the aspect
1158 * ratio the same as the backend mode unless overridden by the
1159 * user setting both hborder and vborder properties.
1160 */
1161 if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
1162 (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
1163 drm_detect_hdmi_monitor(edid)))) {
1164 u32 bX = asyc->scaler.underscan.hborder;
1165 u32 bY = asyc->scaler.underscan.vborder;
1166 u32 r = (asyh->view.oH << 19) / asyh->view.oW;
1167
1168 if (bX) {
1169 asyh->view.oW -= (bX * 2);
1170 if (bY) asyh->view.oH -= (bY * 2);
1171 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
1172 } else {
1173 asyh->view.oW -= (asyh->view.oW >> 4) + 32;
1174 if (bY) asyh->view.oH -= (bY * 2);
1175 else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
1176 }
1177 }
1178
1179 /* Handle CENTER/ASPECT scaling, taking into account the areas
1180 * removed already for overscan compensation.
1181 */
1182 switch (mode) {
1183 case DRM_MODE_SCALE_CENTER:
1184 asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
1185 asyh->view.oH = min((u16)umode->vdisplay, asyh->view.oH);
1186 /* fall-through */
1187 case DRM_MODE_SCALE_ASPECT:
1188 if (asyh->view.oH < asyh->view.oW) {
1189 u32 r = (asyh->view.iW << 19) / asyh->view.iH;
1190 asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
1191 } else {
1192 u32 r = (asyh->view.iH << 19) / asyh->view.iW;
1193 asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
1194 }
1195 break;
1196 default:
1197 break;
1198 }
1199
1200 asyh->set.view = true;
1201}
1202
1203static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001204nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1205{
1206 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1207 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1208 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1209 u32 hbackp = mode->htotal - mode->hsync_end;
1210 u32 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1211 u32 hfrontp = mode->hsync_start - mode->hdisplay;
1212 u32 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1213 struct nv50_head_mode *m = &asyh->mode;
1214
1215 m->h.active = mode->htotal;
1216 m->h.synce = mode->hsync_end - mode->hsync_start - 1;
1217 m->h.blanke = m->h.synce + hbackp;
1218 m->h.blanks = mode->htotal - hfrontp - 1;
1219
1220 m->v.active = mode->vtotal * vscan / ilace;
1221 m->v.synce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1222 m->v.blanke = m->v.synce + vbackp;
1223 m->v.blanks = m->v.active - vfrontp - 1;
1224
1225 /*XXX: Safe underestimate, even "0" works */
1226 m->v.blankus = (m->v.active - mode->vdisplay - 2) * m->h.active;
1227 m->v.blankus *= 1000;
1228 m->v.blankus /= mode->clock;
1229
1230 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1231 m->v.blank2e = m->v.active + m->v.synce + vbackp;
1232 m->v.blank2s = m->v.blank2e + (mode->vdisplay * vscan / ilace);
1233 m->v.active = (m->v.active * 2) + 1;
1234 m->interlace = true;
1235 } else {
1236 m->v.blank2e = 0;
1237 m->v.blank2s = 1;
1238 m->interlace = false;
1239 }
1240 m->clock = mode->clock;
1241
1242 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1243 asyh->set.mode = true;
1244}
1245
1246static int
1247nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
1248{
1249 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggsad633612016-11-04 17:20:36 +10001250 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001251 struct nv50_head *head = nv50_head(crtc);
1252 struct nv50_head_atom *armh = &head->arm;
1253 struct nv50_head_atom *asyh = nv50_head_atom(state);
1254
1255 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
Ben Skeggsad633612016-11-04 17:20:36 +10001256 asyh->clr.mask = 0;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001257 asyh->set.mask = 0;
1258
1259 if (asyh->state.active) {
1260 if (asyh->state.mode_changed)
1261 nv50_head_atomic_check_mode(head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10001262
1263 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
1264 asyh->core.x = asyh->base.x;
1265 asyh->core.y = asyh->base.y;
1266 asyh->core.w = asyh->base.w;
1267 asyh->core.h = asyh->base.h;
1268 } else
Ben Skeggsea8ee392016-11-04 17:20:36 +10001269 if ((asyh->core.visible = asyh->curs.visible)) {
Ben Skeggsad633612016-11-04 17:20:36 +10001270 /*XXX: We need to either find some way of having the
1271 * primary base layer appear black, while still
1272 * being able to display the other layers, or we
1273 * need to allocate a dummy black surface here.
1274 */
1275 asyh->core.x = 0;
1276 asyh->core.y = 0;
1277 asyh->core.w = asyh->state.mode.hdisplay;
1278 asyh->core.h = asyh->state.mode.vdisplay;
1279 }
1280 asyh->core.handle = disp->mast.base.vram.handle;
1281 asyh->core.offset = 0;
1282 asyh->core.format = 0xcf;
1283 asyh->core.kind = 0;
1284 asyh->core.layout = 1;
1285 asyh->core.block = 0;
1286 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001287 asyh->lut.handle = disp->mast.base.vram.handle;
1288 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001289 asyh->set.base = armh->base.cpp != asyh->base.cpp;
1290 asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
Ben Skeggsad633612016-11-04 17:20:36 +10001291 } else {
1292 asyh->core.visible = false;
Ben Skeggsea8ee392016-11-04 17:20:36 +10001293 asyh->curs.visible = false;
Ben Skeggs6bbab3b2016-11-04 17:20:36 +10001294 asyh->base.cpp = 0;
1295 asyh->ovly.cpp = 0;
Ben Skeggsad633612016-11-04 17:20:36 +10001296 }
1297
1298 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
1299 if (asyh->core.visible) {
1300 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
1301 asyh->set.core = true;
1302 } else
1303 if (armh->core.visible) {
1304 asyh->clr.core = true;
1305 }
Ben Skeggsea8ee392016-11-04 17:20:36 +10001306
1307 if (asyh->curs.visible) {
1308 if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
1309 asyh->set.curs = true;
1310 } else
1311 if (armh->curs.visible) {
1312 asyh->clr.curs = true;
1313 }
Ben Skeggsad633612016-11-04 17:20:36 +10001314 } else {
1315 asyh->clr.core = armh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10001316 asyh->clr.curs = armh->curs.visible;
Ben Skeggsad633612016-11-04 17:20:36 +10001317 asyh->set.core = asyh->core.visible;
Ben Skeggsea8ee392016-11-04 17:20:36 +10001318 asyh->set.curs = asyh->curs.visible;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001319 }
1320
1321 memcpy(armh, asyh, sizeof(*asyh));
1322 asyh->state.mode_changed = 0;
1323 return 0;
1324}
1325
1326/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +10001327 * CRTC
1328 *****************************************************************************/
1329static int
Ben Skeggse225f442012-11-21 14:40:21 +10001330nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001331{
Ben Skeggse225f442012-11-21 14:40:21 +10001332 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +10001333 struct nouveau_connector *nv_connector;
1334 struct drm_connector *connector;
1335 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001336
Ben Skeggs488ff202011-10-17 10:38:10 +10001337 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +10001338 connector = &nv_connector->base;
1339 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -07001340 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +10001341 mode = DITHERING_MODE_DYNAMIC2X2;
1342 } else {
1343 mode = nv_connector->dithering_mode;
1344 }
1345
1346 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
1347 if (connector->display_info.bpc >= 8)
1348 mode |= DITHERING_DEPTH_8BPC;
1349 } else {
1350 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001351 }
1352
Ben Skeggsde8268c2012-11-16 10:24:31 +10001353 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001354 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001355 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001356 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
1357 evo_data(push, mode);
1358 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001359 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001360 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
1361 evo_data(push, mode);
1362 } else {
1363 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
1364 evo_data(push, mode);
1365 }
1366
Ben Skeggs438d99e2011-07-05 16:48:06 +10001367 if (update) {
1368 evo_mthd(push, 0x0080, 1);
1369 evo_data(push, 0x00000000);
1370 }
Ben Skeggsde8268c2012-11-16 10:24:31 +10001371 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001372 }
1373
1374 return 0;
1375}
1376
1377static int
Ben Skeggse225f442012-11-21 14:40:21 +10001378nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001379{
Ben Skeggsc4e68122016-11-04 17:20:36 +10001380 struct nv50_head *head = nv50_head(&nv_crtc->base);
1381 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001382 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +10001383 struct nouveau_connector *nv_connector;
Ben Skeggsc4e68122016-11-04 17:20:36 +10001384 struct nouveau_conn_atom asyc;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001385
Ben Skeggsf3fdc522011-07-07 16:01:57 +10001386 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsf3fdc522011-07-07 16:01:57 +10001387
Ben Skeggsc4e68122016-11-04 17:20:36 +10001388 asyc.state.connector = &nv_connector->base;
1389 asyc.scaler.mode = nv_connector->scaling_mode;
1390 asyc.scaler.full = nv_connector->scaling_full;
1391 asyc.scaler.underscan.mode = nv_connector->underscan;
1392 asyc.scaler.underscan.hborder = nv_connector->underscan_hborder;
1393 asyc.scaler.underscan.vborder = nv_connector->underscan_vborder;
1394 nv50_head_atomic_check(&head->base.base, &asyh->state);
1395 nv50_head_atomic_check_view(&head->arm, asyh, &asyc);
1396 nv50_head_flush_set(head, asyh);
Ben Skeggs92854622011-11-11 23:49:06 +10001397
Ben Skeggsc4e68122016-11-04 17:20:36 +10001398 if (update) {
1399 nv50_display_flip_stop(crtc);
1400 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001401 }
1402
1403 return 0;
1404}
1405
1406static int
Roy Splieteae73822014-10-30 22:57:45 +01001407nv50_crtc_set_raster_vblank_dmi(struct nouveau_crtc *nv_crtc, u32 usec)
1408{
1409 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
1410 u32 *push;
1411
1412 push = evo_wait(mast, 8);
1413 if (!push)
1414 return -ENOMEM;
1415
1416 evo_mthd(push, 0x0828 + (nv_crtc->index * 0x400), 1);
1417 evo_data(push, usec);
1418 evo_kick(push, mast);
1419 return 0;
1420}
1421
1422static int
Ben Skeggse225f442012-11-21 14:40:21 +10001423nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +10001424{
Ben Skeggse225f442012-11-21 14:40:21 +10001425 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +10001426 u32 *push, hue, vib;
1427 int adj;
1428
1429 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
1430 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
1431 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
1432
1433 push = evo_wait(mast, 16);
1434 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001435 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsf9887d02012-11-21 13:03:42 +10001436 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
1437 evo_data(push, (hue << 20) | (vib << 8));
1438 } else {
1439 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
1440 evo_data(push, (hue << 20) | (vib << 8));
1441 }
1442
1443 if (update) {
1444 evo_mthd(push, 0x0080, 1);
1445 evo_data(push, 0x00000000);
1446 }
1447 evo_kick(push, mast);
1448 }
1449
1450 return 0;
1451}
1452
1453static int
Ben Skeggse225f442012-11-21 14:40:21 +10001454nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001455 int x, int y, bool update)
1456{
1457 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggsad633612016-11-04 17:20:36 +10001458 struct nv50_head *head = nv50_head(&nv_crtc->base);
1459 struct nv50_head_atom *asyh = &head->asy;
1460 const struct drm_format_info *info;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001461
Ben Skeggsad633612016-11-04 17:20:36 +10001462 info = drm_format_info(nvfb->base.pixel_format);
1463 if (!info || !info->depth)
1464 return -EINVAL;
Ben Skeggsde8268c2012-11-16 10:24:31 +10001465
Ben Skeggsad633612016-11-04 17:20:36 +10001466 asyh->base.depth = info->depth;
1467 asyh->base.cpp = info->cpp[0];
1468 asyh->base.x = x;
1469 asyh->base.y = y;
1470 asyh->base.w = nvfb->base.width;
1471 asyh->base.h = nvfb->base.height;
1472 nv50_head_atomic_check(&head->base.base, &asyh->state);
1473 nv50_head_flush_set(head, asyh);
1474
1475 if (update) {
1476 struct nv50_mast *core = nv50_mast(nv_crtc->base.dev);
1477 u32 *push = evo_wait(core, 2);
1478 if (push) {
Ben Skeggsa46232e2011-07-07 15:23:48 +10001479 evo_mthd(push, 0x0080, 1);
1480 evo_data(push, 0x00000000);
Ben Skeggsad633612016-11-04 17:20:36 +10001481 evo_kick(push, core);
Ben Skeggsa46232e2011-07-07 15:23:48 +10001482 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001483 }
1484
Ben Skeggs8a423642014-08-10 04:10:19 +10001485 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001486 return 0;
1487}
1488
1489static void
Ben Skeggse225f442012-11-21 14:40:21 +10001490nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001491{
Ben Skeggse225f442012-11-21 14:40:21 +10001492 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsea8ee392016-11-04 17:20:36 +10001493 struct nv50_head *head = nv50_head(&nv_crtc->base);
1494 struct nv50_head_atom *asyh = &head->asy;
1495
1496 asyh->curs.visible = true;
1497 asyh->curs.handle = mast->base.vram.handle;
1498 asyh->curs.offset = nv_crtc->cursor.nvbo->bo.offset;
1499 asyh->curs.layout = 1;
1500 asyh->curs.format = 1;
1501 nv50_head_atomic_check(&head->base.base, &asyh->state);
1502 nv50_head_flush_set(head, asyh);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001503}
1504
1505static void
Ben Skeggse225f442012-11-21 14:40:21 +10001506nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +10001507{
Ben Skeggsea8ee392016-11-04 17:20:36 +10001508 struct nv50_head *head = nv50_head(&nv_crtc->base);
1509 struct nv50_head_atom *asyh = &head->asy;
1510
1511 asyh->curs.visible = false;
1512 nv50_head_atomic_check(&head->base.base, &asyh->state);
1513 nv50_head_flush_clr(head, asyh, false);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001514}
Ben Skeggs438d99e2011-07-05 16:48:06 +10001515
Ben Skeggsde8268c2012-11-16 10:24:31 +10001516static void
Ben Skeggse225f442012-11-21 14:40:21 +10001517nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +10001518{
Ben Skeggse225f442012-11-21 14:40:21 +10001519 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001520
Ben Skeggs697bb722015-07-28 17:20:57 +10001521 if (show && nv_crtc->cursor.nvbo && nv_crtc->base.enabled)
Ben Skeggse225f442012-11-21 14:40:21 +10001522 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001523 else
Ben Skeggse225f442012-11-21 14:40:21 +10001524 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001525
1526 if (update) {
1527 u32 *push = evo_wait(mast, 2);
1528 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001529 evo_mthd(push, 0x0080, 1);
1530 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001531 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001532 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001533 }
1534}
1535
1536static void
Ben Skeggse225f442012-11-21 14:40:21 +10001537nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001538{
1539}
1540
1541static void
Ben Skeggse225f442012-11-21 14:40:21 +10001542nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001543{
Ben Skeggsad633612016-11-04 17:20:36 +10001544 struct nv50_head *head = nv50_head(crtc);
1545 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001546
Ben Skeggse225f442012-11-21 14:40:21 +10001547 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001548
Ben Skeggsad633612016-11-04 17:20:36 +10001549 asyh->state.active = false;
1550 nv50_head_atomic_check(&head->base.base, &asyh->state);
1551 nv50_head_flush_clr(head, asyh, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001552}
1553
1554static void
Ben Skeggse225f442012-11-21 14:40:21 +10001555nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001556{
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001557 struct nv50_head *head = nv50_head(crtc);
1558 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001559
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001560 asyh->state.active = true;
1561 nv50_head_atomic_check(&head->base.base, &asyh->state);
1562 nv50_head_flush_set(head, asyh);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001563
Matt Roperf4510a22014-04-01 15:22:40 -07001564 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001565}
1566
1567static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001568nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001569 struct drm_display_mode *adjusted_mode)
1570{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001571 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001572 return true;
1573}
1574
1575static int
Ben Skeggse225f442012-11-21 14:40:21 +10001576nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001577{
Matt Roperf4510a22014-04-01 15:22:40 -07001578 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001579 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001580 int ret;
1581
Ben Skeggs547ad072014-11-10 12:35:06 +10001582 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001583 if (ret == 0) {
1584 if (head->image)
1585 nouveau_bo_unpin(head->image);
1586 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001587 }
1588
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001589 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001590}
1591
1592static int
Ben Skeggse225f442012-11-21 14:40:21 +10001593nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001594 struct drm_display_mode *mode, int x, int y,
1595 struct drm_framebuffer *old_fb)
1596{
Ben Skeggse225f442012-11-21 14:40:21 +10001597 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001598 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1599 struct nouveau_connector *nv_connector;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001600 int ret;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001601 struct nv50_head *head = nv50_head(crtc);
1602 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001603
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001604 memcpy(&asyh->state.mode, umode, sizeof(*umode));
1605 memcpy(&asyh->state.adjusted_mode, mode, sizeof(*mode));
1606 asyh->state.active = true;
1607 asyh->state.mode_changed = true;
1608 nv50_head_atomic_check(&head->base.base, &asyh->state);
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001609
Ben Skeggse225f442012-11-21 14:40:21 +10001610 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001611 if (ret)
1612 return ret;
1613
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001614 nv50_head_flush_set(head, asyh);
1615
Ben Skeggs438d99e2011-07-05 16:48:06 +10001616 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001617 nv50_crtc_set_dither(nv_crtc, false);
1618 nv50_crtc_set_scale(nv_crtc, false);
Roy Splieteae73822014-10-30 22:57:45 +01001619
1620 /* G94 only accepts this after setting scale */
1621 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA)
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001622 nv50_crtc_set_raster_vblank_dmi(nv_crtc, asyh->mode.v.blankus);
Roy Splieteae73822014-10-30 22:57:45 +01001623
Ben Skeggse225f442012-11-21 14:40:21 +10001624 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001625 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001626 return 0;
1627}
1628
1629static int
Ben Skeggse225f442012-11-21 14:40:21 +10001630nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001631 struct drm_framebuffer *old_fb)
1632{
Ben Skeggs77145f12012-07-31 16:16:21 +10001633 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001634 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1635 int ret;
1636
Matt Roperf4510a22014-04-01 15:22:40 -07001637 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001638 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001639 return 0;
1640 }
1641
Ben Skeggse225f442012-11-21 14:40:21 +10001642 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001643 if (ret)
1644 return ret;
1645
Ben Skeggse225f442012-11-21 14:40:21 +10001646 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001647 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1648 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001649 return 0;
1650}
1651
1652static int
Ben Skeggse225f442012-11-21 14:40:21 +10001653nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001654 struct drm_framebuffer *fb, int x, int y,
1655 enum mode_set_atomic state)
1656{
1657 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001658 nv50_display_flip_stop(crtc);
1659 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001660 return 0;
1661}
1662
1663static void
Ben Skeggse225f442012-11-21 14:40:21 +10001664nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001665{
Ben Skeggse225f442012-11-21 14:40:21 +10001666 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001667 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1668 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1669 int i;
1670
1671 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001672 u16 r = nv_crtc->lut.r[i] >> 2;
1673 u16 g = nv_crtc->lut.g[i] >> 2;
1674 u16 b = nv_crtc->lut.b[i] >> 2;
1675
Ben Skeggs648d4df2014-08-10 04:10:27 +10001676 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001677 writew(r + 0x0000, lut + (i * 0x08) + 0);
1678 writew(g + 0x0000, lut + (i * 0x08) + 2);
1679 writew(b + 0x0000, lut + (i * 0x08) + 4);
1680 } else {
1681 writew(r + 0x6000, lut + (i * 0x20) + 0);
1682 writew(g + 0x6000, lut + (i * 0x20) + 2);
1683 writew(b + 0x6000, lut + (i * 0x20) + 4);
1684 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001685 }
1686}
1687
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001688static void
1689nv50_crtc_disable(struct drm_crtc *crtc)
1690{
1691 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001692 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001693 if (head->image)
1694 nouveau_bo_unpin(head->image);
1695 nouveau_bo_ref(NULL, &head->image);
1696}
1697
Ben Skeggs438d99e2011-07-05 16:48:06 +10001698static int
Ben Skeggse225f442012-11-21 14:40:21 +10001699nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001700 uint32_t handle, uint32_t width, uint32_t height)
1701{
1702 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs5a560252014-11-10 15:52:02 +10001703 struct drm_gem_object *gem = NULL;
1704 struct nouveau_bo *nvbo = NULL;
1705 int ret = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001706
Ben Skeggs5a560252014-11-10 15:52:02 +10001707 if (handle) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001708 if (width != 64 || height != 64)
1709 return -EINVAL;
1710
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001711 gem = drm_gem_object_lookup(file_priv, handle);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001712 if (unlikely(!gem))
1713 return -ENOENT;
1714 nvbo = nouveau_gem_object(gem);
1715
Ben Skeggs5a560252014-11-10 15:52:02 +10001716 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001717 }
1718
Ben Skeggs5a560252014-11-10 15:52:02 +10001719 if (ret == 0) {
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001720 if (nv_crtc->cursor.nvbo)
1721 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1722 nouveau_bo_ref(nvbo, &nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001723 }
Ben Skeggs5a560252014-11-10 15:52:02 +10001724 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001725
Ben Skeggs5a560252014-11-10 15:52:02 +10001726 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001727 return ret;
1728}
1729
1730static int
Ben Skeggse225f442012-11-21 14:40:21 +10001731nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001732{
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001733 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001734 struct nv50_curs *curs = nv50_curs(crtc);
1735 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001736 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1737 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001738
1739 nv_crtc->cursor_saved_x = x;
1740 nv_crtc->cursor_saved_y = y;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001741 return 0;
1742}
1743
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001744static int
Ben Skeggse225f442012-11-21 14:40:21 +10001745nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001746 uint32_t size)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001747{
1748 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001749 u32 i;
1750
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001751 for (i = 0; i < size; i++) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001752 nv_crtc->lut.r[i] = r[i];
1753 nv_crtc->lut.g[i] = g[i];
1754 nv_crtc->lut.b[i] = b[i];
1755 }
1756
Ben Skeggse225f442012-11-21 14:40:21 +10001757 nv50_crtc_lut_load(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001758
1759 return 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001760}
1761
1762static void
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001763nv50_crtc_cursor_restore(struct nouveau_crtc *nv_crtc, int x, int y)
1764{
1765 nv50_crtc_cursor_move(&nv_crtc->base, x, y);
1766
1767 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
1768}
1769
1770static void
Ben Skeggse225f442012-11-21 14:40:21 +10001771nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001772{
1773 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001774 struct nv50_disp *disp = nv50_disp(crtc->dev);
1775 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001776 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001777
Ben Skeggs0ad72862014-08-10 04:10:22 +10001778 list_for_each_entry(fbdma, &disp->fbdma, head) {
1779 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1780 }
1781
1782 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1783 nv50_pioc_destroy(&head->oimm.base);
1784 nv50_dmac_destroy(&head->sync.base, disp->disp);
1785 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001786
1787 /*XXX: this shouldn't be necessary, but the core doesn't call
1788 * disconnect() during the cleanup paths
1789 */
1790 if (head->image)
1791 nouveau_bo_unpin(head->image);
1792 nouveau_bo_ref(NULL, &head->image);
1793
Ben Skeggs5a560252014-11-10 15:52:02 +10001794 /*XXX: ditto */
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001795 if (nv_crtc->cursor.nvbo)
1796 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1797 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001798
Ben Skeggs438d99e2011-07-05 16:48:06 +10001799 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001800 if (nv_crtc->lut.nvbo)
1801 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001802 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001803
Ben Skeggs438d99e2011-07-05 16:48:06 +10001804 drm_crtc_cleanup(crtc);
1805 kfree(crtc);
1806}
1807
Ben Skeggse225f442012-11-21 14:40:21 +10001808static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1809 .dpms = nv50_crtc_dpms,
1810 .prepare = nv50_crtc_prepare,
1811 .commit = nv50_crtc_commit,
1812 .mode_fixup = nv50_crtc_mode_fixup,
1813 .mode_set = nv50_crtc_mode_set,
1814 .mode_set_base = nv50_crtc_mode_set_base,
1815 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1816 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001817 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001818};
1819
Ben Skeggse225f442012-11-21 14:40:21 +10001820static const struct drm_crtc_funcs nv50_crtc_func = {
1821 .cursor_set = nv50_crtc_cursor_set,
1822 .cursor_move = nv50_crtc_cursor_move,
1823 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001824 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001825 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001826 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001827};
1828
1829static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001830nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001831{
Ben Skeggsa01ca782015-08-20 14:54:15 +10001832 struct nouveau_drm *drm = nouveau_drm(dev);
1833 struct nvif_device *device = &drm->device;
Ben Skeggse225f442012-11-21 14:40:21 +10001834 struct nv50_disp *disp = nv50_disp(dev);
1835 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001836 struct drm_crtc *crtc;
1837 int ret, i;
1838
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001839 head = kzalloc(sizeof(*head), GFP_KERNEL);
1840 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001841 return -ENOMEM;
1842
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001843 head->base.index = index;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001844 head->base.color_vibrance = 50;
1845 head->base.vibrant_hue = 0;
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001846 head->base.cursor.set_pos = nv50_crtc_cursor_restore;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001847 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001848 head->base.lut.r[i] = i << 8;
1849 head->base.lut.g[i] = i << 8;
1850 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001851 }
1852
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001853 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001854 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1855 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001856 drm_mode_crtc_set_gamma_size(crtc, 256);
1857
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001858 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01001859 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001860 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10001861 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001862 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001863 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001864 if (ret)
1865 nouveau_bo_unpin(head->base.lut.nvbo);
1866 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001867 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001868 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001869 }
1870
1871 if (ret)
1872 goto out;
1873
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001874 /* allocate cursor resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10001875 ret = nv50_curs_create(device, disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001876 if (ret)
1877 goto out;
1878
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001879 /* allocate page flip / sync resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10001880 ret = nv50_base_create(device, disp->disp, index, disp->sync->bo.offset,
1881 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001882 if (ret)
1883 goto out;
1884
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001885 head->sync.addr = EVO_FLIP_SEM0(index);
1886 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001887
1888 /* allocate overlay resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10001889 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001890 if (ret)
1891 goto out;
1892
Ben Skeggsa01ca782015-08-20 14:54:15 +10001893 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
1894 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001895 if (ret)
1896 goto out;
1897
Ben Skeggs438d99e2011-07-05 16:48:06 +10001898out:
1899 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001900 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001901 return ret;
1902}
1903
1904/******************************************************************************
Ben Skeggsa91d3222014-12-22 16:30:13 +10001905 * Encoder helpers
1906 *****************************************************************************/
1907static bool
1908nv50_encoder_mode_fixup(struct drm_encoder *encoder,
1909 const struct drm_display_mode *mode,
1910 struct drm_display_mode *adjusted_mode)
1911{
1912 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1913 struct nouveau_connector *nv_connector;
1914
1915 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1916 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs576f7912014-12-22 17:19:26 +10001917 nv_connector->scaling_full = false;
1918 if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) {
1919 switch (nv_connector->type) {
1920 case DCB_CONNECTOR_LVDS:
1921 case DCB_CONNECTOR_LVDS_SPWG:
1922 case DCB_CONNECTOR_eDP:
1923 /* force use of scaler for non-edid modes */
1924 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
1925 return true;
1926 nv_connector->scaling_full = true;
1927 break;
1928 default:
1929 return true;
1930 }
1931 }
1932
1933 drm_mode_copy(adjusted_mode, nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10001934 }
1935
1936 return true;
1937}
1938
1939/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001940 * DAC
1941 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001942static void
Ben Skeggse225f442012-11-21 14:40:21 +10001943nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001944{
1945 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001946 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001947 struct {
1948 struct nv50_disp_mthd_v1 base;
1949 struct nv50_disp_dac_pwr_v0 pwr;
1950 } args = {
1951 .base.version = 1,
1952 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1953 .base.hasht = nv_encoder->dcb->hasht,
1954 .base.hashm = nv_encoder->dcb->hashm,
1955 .pwr.state = 1,
1956 .pwr.data = 1,
1957 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1958 mode != DRM_MODE_DPMS_OFF),
1959 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1960 mode != DRM_MODE_DPMS_OFF),
1961 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001962
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001963 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001964}
1965
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001966static void
Ben Skeggse225f442012-11-21 14:40:21 +10001967nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001968{
1969}
1970
1971static void
Ben Skeggse225f442012-11-21 14:40:21 +10001972nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001973 struct drm_display_mode *adjusted_mode)
1974{
Ben Skeggse225f442012-11-21 14:40:21 +10001975 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001976 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1977 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001978 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001979
Ben Skeggse225f442012-11-21 14:40:21 +10001980 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001981
Ben Skeggs97b19b52012-11-16 11:21:37 +10001982 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001983 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001984 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001985 u32 syncs = 0x00000000;
1986
1987 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1988 syncs |= 0x00000001;
1989 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1990 syncs |= 0x00000002;
1991
1992 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1993 evo_data(push, 1 << nv_crtc->index);
1994 evo_data(push, syncs);
1995 } else {
1996 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1997 u32 syncs = 0x00000001;
1998
1999 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2000 syncs |= 0x00000008;
2001 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2002 syncs |= 0x00000010;
2003
2004 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2005 magic |= 0x00000001;
2006
2007 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2008 evo_data(push, syncs);
2009 evo_data(push, magic);
2010 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
2011 evo_data(push, 1 << nv_crtc->index);
2012 }
2013
2014 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002015 }
2016
2017 nv_encoder->crtc = encoder->crtc;
2018}
2019
2020static void
Ben Skeggse225f442012-11-21 14:40:21 +10002021nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002022{
2023 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002024 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10002025 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002026 u32 *push;
2027
2028 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10002029 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002030
Ben Skeggs97b19b52012-11-16 11:21:37 +10002031 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002032 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002033 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10002034 evo_mthd(push, 0x0400 + (or * 0x080), 1);
2035 evo_data(push, 0x00000000);
2036 } else {
2037 evo_mthd(push, 0x0180 + (or * 0x020), 1);
2038 evo_data(push, 0x00000000);
2039 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10002040 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002041 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002042 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10002043
2044 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002045}
2046
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002047static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10002048nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002049{
Ben Skeggsc4abd312014-08-10 04:10:26 +10002050 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002051 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10002052 struct {
2053 struct nv50_disp_mthd_v1 base;
2054 struct nv50_disp_dac_load_v0 load;
2055 } args = {
2056 .base.version = 1,
2057 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
2058 .base.hasht = nv_encoder->dcb->hasht,
2059 .base.hashm = nv_encoder->dcb->hashm,
2060 };
2061 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10002062
Ben Skeggsc4abd312014-08-10 04:10:26 +10002063 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
2064 if (args.load.data == 0)
2065 args.load.data = 340;
2066
2067 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
2068 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10002069 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10002070
Ben Skeggs35b21d32012-11-08 12:08:55 +10002071 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10002072}
2073
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002074static void
Ben Skeggse225f442012-11-21 14:40:21 +10002075nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002076{
2077 drm_encoder_cleanup(encoder);
2078 kfree(encoder);
2079}
2080
Ben Skeggse225f442012-11-21 14:40:21 +10002081static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
2082 .dpms = nv50_dac_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10002083 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggse225f442012-11-21 14:40:21 +10002084 .prepare = nv50_dac_disconnect,
2085 .commit = nv50_dac_commit,
2086 .mode_set = nv50_dac_mode_set,
2087 .disable = nv50_dac_disconnect,
2088 .get_crtc = nv50_display_crtc_get,
2089 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002090};
2091
Ben Skeggse225f442012-11-21 14:40:21 +10002092static const struct drm_encoder_funcs nv50_dac_func = {
2093 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002094};
2095
2096static int
Ben Skeggse225f442012-11-21 14:40:21 +10002097nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002098{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002099 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002100 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002101 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002102 struct nouveau_encoder *nv_encoder;
2103 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002104 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002105
2106 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2107 if (!nv_encoder)
2108 return -ENOMEM;
2109 nv_encoder->dcb = dcbe;
2110 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002111
2112 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2113 if (bus)
2114 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002115
2116 encoder = to_drm_encoder(nv_encoder);
2117 encoder->possible_crtcs = dcbe->heads;
2118 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002119 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
2120 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggse225f442012-11-21 14:40:21 +10002121 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002122
2123 drm_mode_connector_attach_encoder(connector, encoder);
2124 return 0;
2125}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002126
2127/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10002128 * Audio
2129 *****************************************************************************/
2130static void
Ben Skeggse225f442012-11-21 14:40:21 +10002131nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002132{
2133 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10002134 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10002135 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10002136 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10002137 struct __packed {
2138 struct {
2139 struct nv50_disp_mthd_v1 mthd;
2140 struct nv50_disp_sor_hda_eld_v0 eld;
2141 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002142 u8 data[sizeof(nv_connector->base.eld)];
2143 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10002144 .base.mthd.version = 1,
2145 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2146 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002147 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2148 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002149 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002150
2151 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2152 if (!drm_detect_monitor_audio(nv_connector->edid))
2153 return;
2154
Ben Skeggs78951d22011-11-11 18:13:13 +10002155 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002156 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002157
Jani Nikula938fd8a2014-10-28 16:20:48 +02002158 nvif_mthd(disp->disp, 0, &args,
2159 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002160}
2161
2162static void
Ben Skeggscc2a9072014-09-15 21:29:05 +10002163nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002164{
2165 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002166 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002167 struct {
2168 struct nv50_disp_mthd_v1 base;
2169 struct nv50_disp_sor_hda_eld_v0 eld;
2170 } args = {
2171 .base.version = 1,
2172 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2173 .base.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002174 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2175 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002176 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002177
Ben Skeggs120b0c32014-08-10 04:10:26 +10002178 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002179}
2180
2181/******************************************************************************
2182 * HDMI
2183 *****************************************************************************/
2184static void
Ben Skeggse225f442012-11-21 14:40:21 +10002185nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002186{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002187 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2188 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002189 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002190 struct {
2191 struct nv50_disp_mthd_v1 base;
2192 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
2193 } args = {
2194 .base.version = 1,
2195 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2196 .base.hasht = nv_encoder->dcb->hasht,
2197 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2198 (0x0100 << nv_crtc->index),
2199 .pwr.state = 1,
2200 .pwr.rekey = 56, /* binary driver, and tegra, constant */
2201 };
2202 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002203 u32 max_ac_packet;
2204
2205 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2206 if (!drm_detect_hdmi_monitor(nv_connector->edid))
2207 return;
2208
2209 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10002210 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002211 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10002212 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002213
Ben Skeggse00f2232014-08-10 04:10:26 +10002214 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10002215 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10002216}
2217
2218static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10002219nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002220{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002221 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002222 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002223 struct {
2224 struct nv50_disp_mthd_v1 base;
2225 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
2226 } args = {
2227 .base.version = 1,
2228 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2229 .base.hasht = nv_encoder->dcb->hasht,
2230 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2231 (0x0100 << nv_crtc->index),
2232 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002233
Ben Skeggse00f2232014-08-10 04:10:26 +10002234 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002235}
2236
2237/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002238 * MST
2239 *****************************************************************************/
2240struct nv50_mstm {
2241 struct nouveau_encoder *outp;
2242
2243 struct drm_dp_mst_topology_mgr mgr;
2244};
2245
2246static int
2247nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
2248{
2249 struct nouveau_encoder *outp = mstm->outp;
2250 struct {
2251 struct nv50_disp_mthd_v1 base;
2252 struct nv50_disp_sor_dp_mst_link_v0 mst;
2253 } args = {
2254 .base.version = 1,
2255 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
2256 .base.hasht = outp->dcb->hasht,
2257 .base.hashm = outp->dcb->hashm,
2258 .mst.state = state,
2259 };
2260 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
2261 struct nvif_object *disp = &drm->display->disp;
2262 int ret;
2263
2264 if (dpcd >= 0x12) {
2265 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
2266 if (ret < 0)
2267 return ret;
2268
2269 dpcd &= ~DP_MST_EN;
2270 if (state)
2271 dpcd |= DP_MST_EN;
2272
2273 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
2274 if (ret < 0)
2275 return ret;
2276 }
2277
2278 return nvif_mthd(disp, 0, &args, sizeof(args));
2279}
2280
2281int
2282nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
2283{
2284 int ret, state = 0;
2285
2286 if (!mstm)
2287 return 0;
2288
2289 if (dpcd[0] >= 0x12 && allow) {
2290 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
2291 if (ret < 0)
2292 return ret;
2293
2294 state = dpcd[1] & DP_MST_CAP;
2295 }
2296
2297 ret = nv50_mstm_enable(mstm, dpcd[0], state);
2298 if (ret)
2299 return ret;
2300
2301 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
2302 if (ret)
2303 return nv50_mstm_enable(mstm, dpcd[0], 0);
2304
2305 return mstm->mgr.mst_state;
2306}
2307
2308static void
2309nv50_mstm_del(struct nv50_mstm **pmstm)
2310{
2311 struct nv50_mstm *mstm = *pmstm;
2312 if (mstm) {
2313 kfree(*pmstm);
2314 *pmstm = NULL;
2315 }
2316}
2317
2318static int
2319nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
2320 int conn_base_id, struct nv50_mstm **pmstm)
2321{
2322 const int max_payloads = hweight8(outp->dcb->heads);
2323 struct drm_device *dev = outp->base.base.dev;
2324 struct nv50_mstm *mstm;
2325 int ret;
2326
2327 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
2328 return -ENOMEM;
2329 mstm->outp = outp;
2330
2331 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev->dev, aux, aux_max,
2332 max_payloads, conn_base_id);
2333 if (ret)
2334 return ret;
2335
2336 return 0;
2337}
2338
2339/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002340 * SOR
2341 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002342static void
Ben Skeggse225f442012-11-21 14:40:21 +10002343nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002344{
2345 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10002346 struct nv50_disp *disp = nv50_disp(encoder->dev);
2347 struct {
2348 struct nv50_disp_mthd_v1 base;
2349 struct nv50_disp_sor_pwr_v0 pwr;
2350 } args = {
2351 .base.version = 1,
2352 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
2353 .base.hasht = nv_encoder->dcb->hasht,
2354 .base.hashm = nv_encoder->dcb->hashm,
2355 .pwr.state = mode == DRM_MODE_DPMS_ON,
2356 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10002357 struct {
2358 struct nv50_disp_mthd_v1 base;
2359 struct nv50_disp_sor_dp_pwr_v0 pwr;
2360 } link = {
2361 .base.version = 1,
2362 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
2363 .base.hasht = nv_encoder->dcb->hasht,
2364 .base.hashm = nv_encoder->dcb->hashm,
2365 .pwr.state = mode == DRM_MODE_DPMS_ON,
2366 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10002367 struct drm_device *dev = encoder->dev;
2368 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002369
2370 nv_encoder->last_dpms = mode;
2371
2372 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
2373 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
2374
2375 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
2376 continue;
2377
2378 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10002379 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10002380 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
2381 return;
2382 break;
2383 }
2384 }
2385
Ben Skeggs48743222014-05-31 01:48:06 +10002386 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10002387 args.pwr.state = 1;
2388 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10002389 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10002390 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10002391 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10002392 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002393}
2394
Ben Skeggs83fc0832011-07-05 13:08:40 +10002395static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10002396nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
2397{
2398 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
2399 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
2400 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002401 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10002402 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
2403 evo_data(push, (nv_encoder->ctrl = temp));
2404 } else {
2405 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
2406 evo_data(push, (nv_encoder->ctrl = temp));
2407 }
2408 evo_kick(push, mast);
2409 }
2410}
2411
2412static void
Ben Skeggse225f442012-11-21 14:40:21 +10002413nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10002414{
2415 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002416 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002417
2418 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2419 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10002420
2421 if (nv_crtc) {
2422 nv50_crtc_prepare(&nv_crtc->base);
2423 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggscc2a9072014-09-15 21:29:05 +10002424 nv50_audio_disconnect(encoder, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002425 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
2426 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10002427}
2428
2429static void
Ben Skeggse225f442012-11-21 14:40:21 +10002430nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002431{
2432}
2433
2434static void
Ben Skeggse225f442012-11-21 14:40:21 +10002435nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002436 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002437{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002438 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2439 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2440 struct {
2441 struct nv50_disp_mthd_v1 base;
2442 struct nv50_disp_sor_lvds_script_v0 lvds;
2443 } lvds = {
2444 .base.version = 1,
2445 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
2446 .base.hasht = nv_encoder->dcb->hasht,
2447 .base.hashm = nv_encoder->dcb->hashm,
2448 };
Ben Skeggse225f442012-11-21 14:40:21 +10002449 struct nv50_disp *disp = nv50_disp(encoder->dev);
2450 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10002451 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10002452 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002453 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10002454 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002455 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002456 u8 owner = 1 << nv_crtc->index;
2457 u8 proto = 0xf;
2458 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002459
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002460 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002461 nv_encoder->crtc = encoder->crtc;
2462
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002463 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10002464 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002465 if (nv_encoder->dcb->sorconf.link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05002466 proto = 0x1;
2467 /* Only enable dual-link if:
2468 * - Need to (i.e. rate > 165MHz)
2469 * - DCB says we can
2470 * - Not an HDMI monitor, since there's no dual-link
2471 * on HDMI.
2472 */
2473 if (mode->clock >= 165000 &&
2474 nv_encoder->dcb->duallink_possible &&
2475 !drm_detect_hdmi_monitor(nv_connector->edid))
2476 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002477 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002478 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002479 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002480
Ben Skeggse84a35a2014-06-05 10:59:55 +10002481 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002482 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10002483 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002484 proto = 0x0;
2485
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002486 if (bios->fp_no_ddc) {
2487 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002488 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002489 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002490 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002491 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10002492 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002493 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002494 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002495 } else
2496 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002497 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002498 }
2499
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002500 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002501 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002502 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002503 } else {
2504 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002505 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002506 }
2507
2508 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002509 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002510 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10002511
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002512 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002513 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10002514 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10002515 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002516 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002517 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10002518 } else
2519 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002520 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002521 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10002522 } else {
2523 nv_encoder->dp.datarate = mode->clock * 30 / 8;
2524 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10002525 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002526
2527 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002528 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002529 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002530 proto = 0x9;
Ben Skeggs3eee8642014-09-15 15:20:47 +10002531 nv50_audio_mode_set(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002532 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002533 default:
2534 BUG_ON(1);
2535 break;
2536 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10002537
Ben Skeggse84a35a2014-06-05 10:59:55 +10002538 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002539
Ben Skeggs648d4df2014-08-10 04:10:27 +10002540 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10002541 u32 *push = evo_wait(mast, 3);
2542 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002543 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2544 u32 syncs = 0x00000001;
2545
2546 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2547 syncs |= 0x00000008;
2548 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2549 syncs |= 0x00000010;
2550
2551 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2552 magic |= 0x00000001;
2553
2554 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2555 evo_data(push, syncs | (depth << 6));
2556 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002557 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002558 }
2559
Ben Skeggse84a35a2014-06-05 10:59:55 +10002560 ctrl = proto << 8;
2561 mask = 0x00000f00;
2562 } else {
2563 ctrl = (depth << 16) | (proto << 8);
2564 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2565 ctrl |= 0x00001000;
2566 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2567 ctrl |= 0x00002000;
2568 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002569 }
2570
Ben Skeggse84a35a2014-06-05 10:59:55 +10002571 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002572}
2573
2574static void
Ben Skeggse225f442012-11-21 14:40:21 +10002575nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002576{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002577 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2578 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002579 drm_encoder_cleanup(encoder);
2580 kfree(encoder);
2581}
2582
Ben Skeggse225f442012-11-21 14:40:21 +10002583static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2584 .dpms = nv50_sor_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10002585 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10002586 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10002587 .commit = nv50_sor_commit,
2588 .mode_set = nv50_sor_mode_set,
2589 .disable = nv50_sor_disconnect,
2590 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002591};
2592
Ben Skeggse225f442012-11-21 14:40:21 +10002593static const struct drm_encoder_funcs nv50_sor_func = {
2594 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002595};
2596
2597static int
Ben Skeggse225f442012-11-21 14:40:21 +10002598nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002599{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002600 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10002601 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002602 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002603 struct nouveau_encoder *nv_encoder;
2604 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002605 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002606
2607 switch (dcbe->type) {
2608 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2609 case DCB_OUTPUT_TMDS:
2610 case DCB_OUTPUT_DP:
2611 default:
2612 type = DRM_MODE_ENCODER_TMDS;
2613 break;
2614 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002615
2616 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2617 if (!nv_encoder)
2618 return -ENOMEM;
2619 nv_encoder->dcb = dcbe;
2620 nv_encoder->or = ffs(dcbe->or) - 1;
2621 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2622
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002623 encoder = to_drm_encoder(nv_encoder);
2624 encoder->possible_crtcs = dcbe->heads;
2625 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002626 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
2627 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002628 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
2629
2630 drm_mode_connector_attach_encoder(connector, encoder);
2631
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002632 if (dcbe->type == DCB_OUTPUT_DP) {
2633 struct nvkm_i2c_aux *aux =
2634 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
2635 if (aux) {
2636 nv_encoder->i2c = &aux->i2c;
2637 nv_encoder->aux = aux;
2638 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002639
2640 /*TODO: Use DP Info Table to check for support. */
2641 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
2642 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
2643 nv_connector->base.base.id,
2644 &nv_encoder->dp.mstm);
2645 if (ret)
2646 return ret;
2647 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002648 } else {
2649 struct nvkm_i2c_bus *bus =
2650 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2651 if (bus)
2652 nv_encoder->i2c = &bus->i2c;
2653 }
2654
Ben Skeggs83fc0832011-07-05 13:08:40 +10002655 return 0;
2656}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002657
2658/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002659 * PIOR
2660 *****************************************************************************/
2661
2662static void
2663nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2664{
2665 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2666 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10002667 struct {
2668 struct nv50_disp_mthd_v1 base;
2669 struct nv50_disp_pior_pwr_v0 pwr;
2670 } args = {
2671 .base.version = 1,
2672 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2673 .base.hasht = nv_encoder->dcb->hasht,
2674 .base.hashm = nv_encoder->dcb->hashm,
2675 .pwr.state = mode == DRM_MODE_DPMS_ON,
2676 .pwr.type = nv_encoder->dcb->type,
2677 };
2678
2679 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002680}
2681
2682static bool
2683nv50_pior_mode_fixup(struct drm_encoder *encoder,
2684 const struct drm_display_mode *mode,
2685 struct drm_display_mode *adjusted_mode)
2686{
Ben Skeggsa91d3222014-12-22 16:30:13 +10002687 if (!nv50_encoder_mode_fixup(encoder, mode, adjusted_mode))
2688 return false;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002689 adjusted_mode->clock *= 2;
2690 return true;
2691}
2692
2693static void
2694nv50_pior_commit(struct drm_encoder *encoder)
2695{
2696}
2697
2698static void
2699nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2700 struct drm_display_mode *adjusted_mode)
2701{
2702 struct nv50_mast *mast = nv50_mast(encoder->dev);
2703 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2704 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2705 struct nouveau_connector *nv_connector;
2706 u8 owner = 1 << nv_crtc->index;
2707 u8 proto, depth;
2708 u32 *push;
2709
2710 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2711 switch (nv_connector->base.display_info.bpc) {
2712 case 10: depth = 0x6; break;
2713 case 8: depth = 0x5; break;
2714 case 6: depth = 0x2; break;
2715 default: depth = 0x0; break;
2716 }
2717
2718 switch (nv_encoder->dcb->type) {
2719 case DCB_OUTPUT_TMDS:
2720 case DCB_OUTPUT_DP:
2721 proto = 0x0;
2722 break;
2723 default:
2724 BUG_ON(1);
2725 break;
2726 }
2727
2728 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2729
2730 push = evo_wait(mast, 8);
2731 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002732 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002733 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2734 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2735 ctrl |= 0x00001000;
2736 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2737 ctrl |= 0x00002000;
2738 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2739 evo_data(push, ctrl);
2740 }
2741
2742 evo_kick(push, mast);
2743 }
2744
2745 nv_encoder->crtc = encoder->crtc;
2746}
2747
2748static void
2749nv50_pior_disconnect(struct drm_encoder *encoder)
2750{
2751 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2752 struct nv50_mast *mast = nv50_mast(encoder->dev);
2753 const int or = nv_encoder->or;
2754 u32 *push;
2755
2756 if (nv_encoder->crtc) {
2757 nv50_crtc_prepare(nv_encoder->crtc);
2758
2759 push = evo_wait(mast, 4);
2760 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002761 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002762 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2763 evo_data(push, 0x00000000);
2764 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002765 evo_kick(push, mast);
2766 }
2767 }
2768
2769 nv_encoder->crtc = NULL;
2770}
2771
2772static void
2773nv50_pior_destroy(struct drm_encoder *encoder)
2774{
2775 drm_encoder_cleanup(encoder);
2776 kfree(encoder);
2777}
2778
2779static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2780 .dpms = nv50_pior_dpms,
2781 .mode_fixup = nv50_pior_mode_fixup,
2782 .prepare = nv50_pior_disconnect,
2783 .commit = nv50_pior_commit,
2784 .mode_set = nv50_pior_mode_set,
2785 .disable = nv50_pior_disconnect,
2786 .get_crtc = nv50_display_crtc_get,
2787};
2788
2789static const struct drm_encoder_funcs nv50_pior_func = {
2790 .destroy = nv50_pior_destroy,
2791};
2792
2793static int
2794nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2795{
2796 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002797 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002798 struct nvkm_i2c_bus *bus = NULL;
2799 struct nvkm_i2c_aux *aux = NULL;
2800 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002801 struct nouveau_encoder *nv_encoder;
2802 struct drm_encoder *encoder;
2803 int type;
2804
2805 switch (dcbe->type) {
2806 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002807 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
2808 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002809 type = DRM_MODE_ENCODER_TMDS;
2810 break;
2811 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002812 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
2813 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002814 type = DRM_MODE_ENCODER_TMDS;
2815 break;
2816 default:
2817 return -ENODEV;
2818 }
2819
2820 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2821 if (!nv_encoder)
2822 return -ENOMEM;
2823 nv_encoder->dcb = dcbe;
2824 nv_encoder->or = ffs(dcbe->or) - 1;
2825 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002826 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002827
2828 encoder = to_drm_encoder(nv_encoder);
2829 encoder->possible_crtcs = dcbe->heads;
2830 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002831 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
2832 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002833 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2834
2835 drm_mode_connector_attach_encoder(connector, encoder);
2836 return 0;
2837}
2838
2839/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002840 * Framebuffer
2841 *****************************************************************************/
2842
Ben Skeggs8a423642014-08-10 04:10:19 +10002843static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002844nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002845{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002846 int i;
2847 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2848 nvif_object_fini(&fbdma->base[i]);
2849 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002850 list_del(&fbdma->head);
2851 kfree(fbdma);
2852}
2853
2854static int
2855nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2856{
2857 struct nouveau_drm *drm = nouveau_drm(dev);
2858 struct nv50_disp *disp = nv50_disp(dev);
2859 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002860 struct __attribute__ ((packed)) {
2861 struct nv_dma_v0 base;
2862 union {
2863 struct nv50_dma_v0 nv50;
2864 struct gf100_dma_v0 gf100;
Ben Skeggsbd70563f2015-08-20 14:54:21 +10002865 struct gf119_dma_v0 gf119;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002866 };
2867 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002868 struct nv50_fbdma *fbdma;
2869 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002870 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002871 int ret;
2872
2873 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002874 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002875 return 0;
2876 }
2877
2878 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2879 if (!fbdma)
2880 return -ENOMEM;
2881 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002882
Ben Skeggs4acfd702014-08-10 04:10:24 +10002883 args.base.target = NV_DMA_V0_TARGET_VRAM;
2884 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2885 args.base.start = offset;
2886 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002887
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002888 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002889 args.nv50.part = NV50_DMA_V0_PART_256;
2890 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002891 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002892 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002893 args.nv50.part = NV50_DMA_V0_PART_256;
2894 args.nv50.kind = kind;
2895 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002896 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002897 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002898 args.gf100.kind = kind;
2899 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002900 } else {
Ben Skeggsbd70563f2015-08-20 14:54:21 +10002901 args.gf119.page = GF119_DMA_V0_PAGE_LP;
2902 args.gf119.kind = kind;
2903 size += sizeof(args.gf119);
Ben Skeggs8a423642014-08-10 04:10:19 +10002904 }
2905
2906 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002907 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsa01ca782015-08-20 14:54:15 +10002908 int ret = nvif_object_init(&head->sync.base.base.user, name,
2909 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002910 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002911 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002912 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002913 return ret;
2914 }
2915 }
2916
Ben Skeggsa01ca782015-08-20 14:54:15 +10002917 ret = nvif_object_init(&mast->base.base.user, name, NV_DMA_IN_MEMORY,
2918 &args, size, &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002919 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002920 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002921 return ret;
2922 }
2923
2924 return 0;
2925}
2926
Ben Skeggsab0af552014-08-10 04:10:19 +10002927static void
2928nv50_fb_dtor(struct drm_framebuffer *fb)
2929{
2930}
2931
2932static int
2933nv50_fb_ctor(struct drm_framebuffer *fb)
2934{
2935 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2936 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2937 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002938 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002939 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2940 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002941
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002942 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002943 tile >>= 4; /* yep.. */
2944
Ben Skeggsab0af552014-08-10 04:10:19 +10002945 switch (fb->depth) {
2946 case 8: nv_fb->r_format = 0x1e00; break;
2947 case 15: nv_fb->r_format = 0xe900; break;
2948 case 16: nv_fb->r_format = 0xe800; break;
2949 case 24:
2950 case 32: nv_fb->r_format = 0xcf00; break;
2951 case 30: nv_fb->r_format = 0xd100; break;
2952 default:
2953 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2954 return -EINVAL;
2955 }
2956
Ben Skeggs648d4df2014-08-10 04:10:27 +10002957 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002958 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2959 (fb->pitches[0] | 0x00100000);
2960 nv_fb->r_format |= kind << 16;
2961 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10002962 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002963 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2964 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002965 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002966 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2967 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002968 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002969 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002970
Ben Skeggsf392ec42014-08-10 04:10:28 +10002971 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
2972 drm->device.info.ram_user, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002973}
2974
2975/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002976 * Init
2977 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002978
Ben Skeggs2a44e492011-11-09 11:36:33 +10002979void
Ben Skeggse225f442012-11-21 14:40:21 +10002980nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002981{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002982}
2983
2984int
Ben Skeggse225f442012-11-21 14:40:21 +10002985nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002986{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002987 struct nv50_disp *disp = nv50_disp(dev);
2988 struct drm_crtc *crtc;
2989 u32 *push;
2990
2991 push = evo_wait(nv50_mast(dev), 32);
2992 if (!push)
2993 return -EBUSY;
2994
2995 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2996 struct nv50_sync *sync = nv50_sync(crtc);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002997
2998 nv50_crtc_lut_load(crtc);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002999 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003000 }
3001
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003002 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10003003 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10003004 evo_kick(push, nv50_mast(dev));
3005 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003006}
3007
3008void
Ben Skeggse225f442012-11-21 14:40:21 +10003009nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003010{
Ben Skeggse225f442012-11-21 14:40:21 +10003011 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10003012 struct nv50_fbdma *fbdma, *fbtmp;
3013
3014 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10003015 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10003016 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10003017
Ben Skeggs0ad72862014-08-10 04:10:22 +10003018 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10003019
Ben Skeggs816af2f2011-11-16 15:48:48 +10003020 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003021 if (disp->sync)
3022 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10003023 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10003024
Ben Skeggs77145f12012-07-31 16:16:21 +10003025 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003026 kfree(disp);
3027}
3028
3029int
Ben Skeggse225f442012-11-21 14:40:21 +10003030nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10003031{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10003032 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10003033 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10003034 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003035 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10003036 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10003037 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10003038 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003039
3040 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
3041 if (!disp)
3042 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10003043 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10003044
3045 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10003046 nouveau_display(dev)->dtor = nv50_display_destroy;
3047 nouveau_display(dev)->init = nv50_display_init;
3048 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10003049 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
3050 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10003051 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10003052
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003053 /* small shared memory area we use for notifiers and semaphores */
3054 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01003055 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003056 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10003057 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003058 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003059 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01003060 if (ret)
3061 nouveau_bo_unpin(disp->sync);
3062 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003063 if (ret)
3064 nouveau_bo_ref(NULL, &disp->sync);
3065 }
3066
3067 if (ret)
3068 goto out;
3069
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003070 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10003071 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10003072 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10003073 if (ret)
3074 goto out;
3075
Ben Skeggs438d99e2011-07-05 16:48:06 +10003076 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10003077 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsa01ca782015-08-20 14:54:15 +10003078 crtcs = nvif_rd32(&device->object, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10003079 else
3080 crtcs = 2;
3081
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10003082 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10003083 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10003084 if (ret)
3085 goto out;
3086 }
3087
Ben Skeggs83fc0832011-07-05 13:08:40 +10003088 /* create encoder/connector objects based on VBIOS DCB table */
3089 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
3090 connector = nouveau_connector_create(dev, dcbe->connector);
3091 if (IS_ERR(connector))
3092 continue;
3093
Ben Skeggseb6313a2013-02-11 09:52:58 +10003094 if (dcbe->location == DCB_LOC_ON_CHIP) {
3095 switch (dcbe->type) {
3096 case DCB_OUTPUT_TMDS:
3097 case DCB_OUTPUT_LVDS:
3098 case DCB_OUTPUT_DP:
3099 ret = nv50_sor_create(connector, dcbe);
3100 break;
3101 case DCB_OUTPUT_ANALOG:
3102 ret = nv50_dac_create(connector, dcbe);
3103 break;
3104 default:
3105 ret = -ENODEV;
3106 break;
3107 }
3108 } else {
3109 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003110 }
3111
Ben Skeggseb6313a2013-02-11 09:52:58 +10003112 if (ret) {
3113 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
3114 dcbe->location, dcbe->type,
3115 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10003116 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10003117 }
3118 }
3119
3120 /* cull any connectors we created that don't have an encoder */
3121 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
3122 if (connector->encoder_ids[0])
3123 continue;
3124
Ben Skeggs77145f12012-07-31 16:16:21 +10003125 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03003126 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10003127 connector->funcs->destroy(connector);
3128 }
3129
Ben Skeggs26f6d882011-07-04 16:25:18 +10003130out:
3131 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10003132 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10003133 return ret;
3134}