blob: 018f33601e1e58f958bd9645b1c1362f3d1e26f6 [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
74 struct nv50_head_mode {
75 bool interlace;
76 u32 clock;
77 struct {
78 u16 active;
79 u16 synce;
80 u16 blanke;
81 u16 blanks;
82 } h;
83 struct {
84 u32 active;
85 u16 synce;
86 u16 blanke;
87 u16 blanks;
88 u16 blank2s;
89 u16 blank2e;
90 u16 blankus;
91 } v;
92 } mode;
93
Ben Skeggsad633612016-11-04 17:20:36 +100094 struct {
Ben Skeggsa7ae1562016-11-04 17:20:36 +100095 u32 handle;
96 u64 offset:40;
97 } lut;
98
99 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000100 bool visible;
101 u32 handle;
102 u64 offset:40;
103 u8 format;
104 u8 kind:7;
105 u8 layout:1;
106 u8 block:4;
107 u32 pitch:20;
108 u16 x;
109 u16 y;
110 u16 w;
111 u16 h;
112 } core;
113
114 struct {
115 u8 depth;
116 u8 cpp;
117 u16 x;
118 u16 y;
119 u16 w;
120 u16 h;
121 } base;
122
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000123 union {
124 struct {
Ben Skeggsad633612016-11-04 17:20:36 +1000125 bool core:1;
126 };
127 u8 mask;
128 } clr;
129
130 union {
131 struct {
132 bool core:1;
133 bool view:1;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000134 bool mode:1;
135 };
136 u16 mask;
137 } set;
138};
139
140/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000141 * EVO channel
142 *****************************************************************************/
143
Ben Skeggse225f442012-11-21 14:40:21 +1000144struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000145 struct nvif_object user;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000146 struct nvif_device *device;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000147};
148
149static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000150nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000151 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000152 struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000153{
Ben Skeggs41a63402015-08-20 14:54:16 +1000154 struct nvif_sclass *sclass;
155 int ret, i, n;
Ben Skeggs6af52892014-11-03 15:01:33 +1000156
Ben Skeggsa01ca782015-08-20 14:54:15 +1000157 chan->device = device;
158
Ben Skeggs41a63402015-08-20 14:54:16 +1000159 ret = n = nvif_object_sclass_get(disp, &sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000160 if (ret < 0)
161 return ret;
162
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000163 while (oclass[0]) {
Ben Skeggs41a63402015-08-20 14:54:16 +1000164 for (i = 0; i < n; i++) {
165 if (sclass[i].oclass == oclass[0]) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000166 ret = nvif_object_init(disp, 0, oclass[0],
Ben Skeggsa01ca782015-08-20 14:54:15 +1000167 data, size, &chan->user);
Ben Skeggs6af52892014-11-03 15:01:33 +1000168 if (ret == 0)
169 nvif_object_map(&chan->user);
Ben Skeggs41a63402015-08-20 14:54:16 +1000170 nvif_object_sclass_put(&sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000171 return ret;
172 }
Ben Skeggsb76f1522014-08-10 04:10:28 +1000173 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000174 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000175 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000176
Ben Skeggs41a63402015-08-20 14:54:16 +1000177 nvif_object_sclass_put(&sclass);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000178 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000179}
180
181static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000182nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000183{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000184 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000185}
186
187/******************************************************************************
188 * PIO EVO channel
189 *****************************************************************************/
190
Ben Skeggse225f442012-11-21 14:40:21 +1000191struct nv50_pioc {
192 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000193};
194
195static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000196nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000197{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000198 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000199}
200
201static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000202nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000203 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +1000204 struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000205{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000206 return nv50_chan_create(device, disp, oclass, head, data, size,
207 &pioc->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000208}
209
210/******************************************************************************
211 * Cursor Immediate
212 *****************************************************************************/
213
214struct nv50_curs {
215 struct nv50_pioc base;
216};
217
218static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000219nv50_curs_create(struct nvif_device *device, struct nvif_object *disp,
220 int head, struct nv50_curs *curs)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000221{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000222 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000223 .head = head,
224 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000225 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000226 GK104_DISP_CURSOR,
227 GF110_DISP_CURSOR,
228 GT214_DISP_CURSOR,
229 G82_DISP_CURSOR,
230 NV50_DISP_CURSOR,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000231 0
232 };
233
Ben Skeggsa01ca782015-08-20 14:54:15 +1000234 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
235 &curs->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000236}
237
238/******************************************************************************
239 * Overlay Immediate
240 *****************************************************************************/
241
242struct nv50_oimm {
243 struct nv50_pioc base;
244};
245
246static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000247nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
248 int head, struct nv50_oimm *oimm)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000249{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000250 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000251 .head = head,
252 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000253 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000254 GK104_DISP_OVERLAY,
255 GF110_DISP_OVERLAY,
256 GT214_DISP_OVERLAY,
257 G82_DISP_OVERLAY,
258 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000259 0
260 };
261
Ben Skeggsa01ca782015-08-20 14:54:15 +1000262 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
263 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000264}
265
266/******************************************************************************
267 * DMA EVO channel
268 *****************************************************************************/
269
Ben Skeggse225f442012-11-21 14:40:21 +1000270struct nv50_dmac {
271 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000272 dma_addr_t handle;
273 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100274
Ben Skeggs0ad72862014-08-10 04:10:22 +1000275 struct nvif_object sync;
276 struct nvif_object vram;
277
Daniel Vetter59ad1462012-12-02 14:49:44 +0100278 /* Protects against concurrent pushbuf access to this channel, lock is
279 * grabbed by evo_wait (if the pushbuf reservation is successful) and
280 * dropped again by evo_kick. */
281 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000282};
283
284static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000285nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000286{
Ben Skeggsa01ca782015-08-20 14:54:15 +1000287 struct nvif_device *device = dmac->base.device;
288
Ben Skeggs0ad72862014-08-10 04:10:22 +1000289 nvif_object_fini(&dmac->vram);
290 nvif_object_fini(&dmac->sync);
291
292 nv50_chan_destroy(&dmac->base);
293
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000294 if (dmac->ptr) {
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000295 struct device *dev = nvxx_device(device)->dev;
296 dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000297 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000298}
299
300static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000301nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000302 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000303 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000304{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000305 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000306 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000307 int ret;
308
Daniel Vetter59ad1462012-12-02 14:49:44 +0100309 mutex_init(&dmac->lock);
310
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000311 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
312 &dmac->handle, GFP_KERNEL);
Ben Skeggs47057302012-11-16 13:58:48 +1000313 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000314 return -ENOMEM;
315
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000316 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
317 &(struct nv_dma_v0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +1000318 .target = NV_DMA_V0_TARGET_PCI_US,
319 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000320 .start = dmac->handle + 0x0000,
321 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000322 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000323 if (ret)
324 return ret;
325
Ben Skeggsbf81df92015-08-20 14:54:16 +1000326 args->pushbuf = nvif_handle(&pushbuf);
327
Ben Skeggsa01ca782015-08-20 14:54:15 +1000328 ret = nv50_chan_create(device, disp, oclass, head, data, size,
329 &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000330 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000331 if (ret)
332 return ret;
333
Ben Skeggsa01ca782015-08-20 14:54:15 +1000334 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000335 &(struct nv_dma_v0) {
336 .target = NV_DMA_V0_TARGET_VRAM,
337 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000338 .start = syncbuf + 0x0000,
339 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000340 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000341 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000342 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000343 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000344
Ben Skeggsa01ca782015-08-20 14:54:15 +1000345 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000346 &(struct nv_dma_v0) {
347 .target = NV_DMA_V0_TARGET_VRAM,
348 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000349 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000350 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000351 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000352 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000353 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000354 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000355
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000356 return ret;
357}
358
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000359/******************************************************************************
360 * Core
361 *****************************************************************************/
362
Ben Skeggse225f442012-11-21 14:40:21 +1000363struct nv50_mast {
364 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000365};
366
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000367static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000368nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
369 u64 syncbuf, struct nv50_mast *core)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000370{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000371 struct nv50_disp_core_channel_dma_v0 args = {
372 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000373 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000374 static const s32 oclass[] = {
Ben Skeggsfd478772016-07-09 10:41:01 +1000375 GP104_DISP_CORE_CHANNEL_DMA,
Ben Skeggsf9d5cbb2016-07-09 10:41:01 +1000376 GP100_DISP_CORE_CHANNEL_DMA,
Ben Skeggsdb1eb522016-02-11 08:35:32 +1000377 GM200_DISP_CORE_CHANNEL_DMA,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000378 GM107_DISP_CORE_CHANNEL_DMA,
379 GK110_DISP_CORE_CHANNEL_DMA,
380 GK104_DISP_CORE_CHANNEL_DMA,
381 GF110_DISP_CORE_CHANNEL_DMA,
382 GT214_DISP_CORE_CHANNEL_DMA,
383 GT206_DISP_CORE_CHANNEL_DMA,
384 GT200_DISP_CORE_CHANNEL_DMA,
385 G82_DISP_CORE_CHANNEL_DMA,
386 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000387 0
388 };
389
Ben Skeggsa01ca782015-08-20 14:54:15 +1000390 return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
391 syncbuf, &core->base);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000392}
393
394/******************************************************************************
395 * Base
396 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000397
Ben Skeggse225f442012-11-21 14:40:21 +1000398struct nv50_sync {
399 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000400 u32 addr;
401 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000402};
403
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000404static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000405nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
406 int head, u64 syncbuf, struct nv50_sync *base)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000407{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000408 struct nv50_disp_base_channel_dma_v0 args = {
409 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000410 .head = head,
411 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000412 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000413 GK110_DISP_BASE_CHANNEL_DMA,
414 GK104_DISP_BASE_CHANNEL_DMA,
415 GF110_DISP_BASE_CHANNEL_DMA,
416 GT214_DISP_BASE_CHANNEL_DMA,
417 GT200_DISP_BASE_CHANNEL_DMA,
418 G82_DISP_BASE_CHANNEL_DMA,
419 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000420 0
421 };
422
Ben Skeggsa01ca782015-08-20 14:54:15 +1000423 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000424 syncbuf, &base->base);
425}
426
427/******************************************************************************
428 * Overlay
429 *****************************************************************************/
430
Ben Skeggse225f442012-11-21 14:40:21 +1000431struct nv50_ovly {
432 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000433};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000434
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000435static int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000436nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
437 int head, u64 syncbuf, struct nv50_ovly *ovly)
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000438{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000439 struct nv50_disp_overlay_channel_dma_v0 args = {
440 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000441 .head = head,
442 };
Ben Skeggs315a8b22015-08-20 14:54:16 +1000443 static const s32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000444 GK104_DISP_OVERLAY_CONTROL_DMA,
445 GF110_DISP_OVERLAY_CONTROL_DMA,
446 GT214_DISP_OVERLAY_CHANNEL_DMA,
447 GT200_DISP_OVERLAY_CHANNEL_DMA,
448 G82_DISP_OVERLAY_CHANNEL_DMA,
449 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000450 0
451 };
452
Ben Skeggsa01ca782015-08-20 14:54:15 +1000453 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000454 syncbuf, &ovly->base);
455}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000456
Ben Skeggse225f442012-11-21 14:40:21 +1000457struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000458 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000459 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000460 struct nv50_curs curs;
461 struct nv50_sync sync;
462 struct nv50_ovly ovly;
463 struct nv50_oimm oimm;
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000464
465 struct nv50_head_atom arm;
466 struct nv50_head_atom asy;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000467};
468
Ben Skeggse225f442012-11-21 14:40:21 +1000469#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
470#define nv50_curs(c) (&nv50_head(c)->curs)
471#define nv50_sync(c) (&nv50_head(c)->sync)
472#define nv50_ovly(c) (&nv50_head(c)->ovly)
473#define nv50_oimm(c) (&nv50_head(c)->oimm)
474#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000475#define nv50_vers(c) nv50_chan(c)->user.oclass
476
477struct nv50_fbdma {
478 struct list_head head;
479 struct nvif_object core;
480 struct nvif_object base[4];
481};
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000482
Ben Skeggse225f442012-11-21 14:40:21 +1000483struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000484 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000485 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000486
Ben Skeggs8a423642014-08-10 04:10:19 +1000487 struct list_head fbdma;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000488
489 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000490};
491
Ben Skeggse225f442012-11-21 14:40:21 +1000492static struct nv50_disp *
493nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000494{
Ben Skeggs77145f12012-07-31 16:16:21 +1000495 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000496}
497
Ben Skeggse225f442012-11-21 14:40:21 +1000498#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000499
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000500static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000501nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000502{
503 return nouveau_encoder(encoder)->crtc;
504}
505
506/******************************************************************************
507 * EVO channel helpers
508 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000509static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000510evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000511{
Ben Skeggse225f442012-11-21 14:40:21 +1000512 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000513 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000514 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000515
Daniel Vetter59ad1462012-12-02 14:49:44 +0100516 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000517 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000518 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000519
Ben Skeggs0ad72862014-08-10 04:10:22 +1000520 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000521 if (nvif_msec(device, 2000,
522 if (!nvif_rd32(&dmac->base.user, 0x0004))
523 break;
524 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100525 mutex_unlock(&dmac->lock);
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000526 printk(KERN_ERR "nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000527 return NULL;
528 }
529
530 put = 0;
531 }
532
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000533 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000534}
535
536static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000537evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000538{
Ben Skeggse225f442012-11-21 14:40:21 +1000539 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000540 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100541 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000542}
543
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000544#define evo_mthd(p,m,s) do { \
545 const u32 _m = (m), _s = (s); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000546 if (drm_debug & DRM_UT_KMS) \
547 printk(KERN_ERR "%04x %d %s\n", _m, _s, __func__); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000548 *((p)++) = ((_s << 18) | _m); \
549} while(0)
Ben Skeggs7f55a072016-11-04 17:20:36 +1000550
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000551#define evo_data(p,d) do { \
552 const u32 _d = (d); \
Ben Skeggs7f55a072016-11-04 17:20:36 +1000553 if (drm_debug & DRM_UT_KMS) \
554 printk(KERN_ERR "\t%08x\n", _d); \
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000555 *((p)++) = _d; \
556} while(0)
Ben Skeggs51beb422011-07-05 10:33:08 +1000557
Ben Skeggs3376ee32011-11-12 14:28:12 +1000558static bool
559evo_sync_wait(void *data)
560{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500561 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
562 return true;
563 usleep_range(1, 2);
564 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000565}
566
567static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000568evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000569{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000570 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000571 struct nv50_disp *disp = nv50_disp(dev);
572 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000573 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000574 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000575 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000576 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000577 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000578 evo_mthd(push, 0x0080, 2);
579 evo_data(push, 0x00000000);
580 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000581 evo_kick(push, mast);
Ben Skeggs54442042015-08-20 14:54:11 +1000582 if (nvif_msec(device, 2000,
583 if (evo_sync_wait(disp->sync))
584 break;
585 ) >= 0)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000586 return 0;
587 }
588
589 return -EBUSY;
590}
591
592/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000593 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000594 *****************************************************************************/
595struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000596nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000597{
Ben Skeggse225f442012-11-21 14:40:21 +1000598 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000599}
600
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000601struct nv50_display_flip {
602 struct nv50_disp *disp;
603 struct nv50_sync *chan;
604};
605
606static bool
607nv50_display_flip_wait(void *data)
608{
609 struct nv50_display_flip *flip = data;
610 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500611 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000612 return true;
613 usleep_range(1, 2);
614 return false;
615}
616
Ben Skeggs3376ee32011-11-12 14:28:12 +1000617void
Ben Skeggse225f442012-11-21 14:40:21 +1000618nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000619{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000620 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000621 struct nv50_display_flip flip = {
622 .disp = nv50_disp(crtc->dev),
623 .chan = nv50_sync(crtc),
624 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000625 u32 *push;
626
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000627 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000628 if (push) {
629 evo_mthd(push, 0x0084, 1);
630 evo_data(push, 0x00000000);
631 evo_mthd(push, 0x0094, 1);
632 evo_data(push, 0x00000000);
633 evo_mthd(push, 0x00c0, 1);
634 evo_data(push, 0x00000000);
635 evo_mthd(push, 0x0080, 1);
636 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000637 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000638 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000639
Ben Skeggs54442042015-08-20 14:54:11 +1000640 nvif_msec(device, 2000,
641 if (nv50_display_flip_wait(&flip))
642 break;
643 );
Ben Skeggs3376ee32011-11-12 14:28:12 +1000644}
645
646int
Ben Skeggse225f442012-11-21 14:40:21 +1000647nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000648 struct nouveau_channel *chan, u32 swap_interval)
649{
650 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000651 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000652 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000653 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000654 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000655 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000656
Ben Skeggs9ba83102014-12-22 19:50:23 +1000657 if (crtc->primary->fb->width != fb->width ||
658 crtc->primary->fb->height != fb->height)
659 return -EINVAL;
660
Ben Skeggs3376ee32011-11-12 14:28:12 +1000661 swap_interval <<= 4;
662 if (swap_interval == 0)
663 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000664 if (chan == NULL)
665 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000666
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000667 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000668 if (unlikely(push == NULL))
669 return -EBUSY;
670
Ben Skeggsa01ca782015-08-20 14:54:15 +1000671 if (chan && chan->user.oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000672 ret = RING_SPACE(chan, 8);
673 if (ret)
674 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000675
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000676 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000677 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000678 OUT_RING (chan, sync->addr ^ 0x10);
679 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
680 OUT_RING (chan, sync->data + 1);
681 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
682 OUT_RING (chan, sync->addr);
683 OUT_RING (chan, sync->data);
684 } else
Ben Skeggsa01ca782015-08-20 14:54:15 +1000685 if (chan && chan->user.oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000686 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000687 ret = RING_SPACE(chan, 12);
688 if (ret)
689 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000690
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000691 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000692 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000693 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
694 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
695 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
696 OUT_RING (chan, sync->data + 1);
697 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
698 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
699 OUT_RING (chan, upper_32_bits(addr));
700 OUT_RING (chan, lower_32_bits(addr));
701 OUT_RING (chan, sync->data);
702 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
703 } else
704 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000705 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000706 ret = RING_SPACE(chan, 10);
707 if (ret)
708 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000709
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000710 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
711 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
712 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
713 OUT_RING (chan, sync->data + 1);
714 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
715 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
716 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
717 OUT_RING (chan, upper_32_bits(addr));
718 OUT_RING (chan, lower_32_bits(addr));
719 OUT_RING (chan, sync->data);
720 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
721 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
722 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500723
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000724 if (chan) {
725 sync->addr ^= 0x10;
726 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000727 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000728 }
729
730 /* queue the flip */
731 evo_mthd(push, 0x0100, 1);
732 evo_data(push, 0xfffe0000);
733 evo_mthd(push, 0x0084, 1);
734 evo_data(push, swap_interval);
735 if (!(swap_interval & 0x00000100)) {
736 evo_mthd(push, 0x00e0, 1);
737 evo_data(push, 0x40000000);
738 }
739 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000740 evo_data(push, sync->addr);
741 evo_data(push, sync->data++);
742 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000743 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000744 evo_mthd(push, 0x00a0, 2);
745 evo_data(push, 0x00000000);
746 evo_data(push, 0x00000000);
747 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000748 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000749 evo_mthd(push, 0x0110, 2);
750 evo_data(push, 0x00000000);
751 evo_data(push, 0x00000000);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000752 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000753 evo_mthd(push, 0x0800, 5);
754 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
755 evo_data(push, 0);
756 evo_data(push, (fb->height << 16) | fb->width);
757 evo_data(push, nv_fb->r_pitch);
758 evo_data(push, nv_fb->r_format);
759 } else {
760 evo_mthd(push, 0x0400, 5);
761 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
762 evo_data(push, 0);
763 evo_data(push, (fb->height << 16) | fb->width);
764 evo_data(push, nv_fb->r_pitch);
765 evo_data(push, nv_fb->r_format);
766 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000767 evo_mthd(push, 0x0080, 1);
768 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000769 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000770
771 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000772 return 0;
773}
774
Ben Skeggs26f6d882011-07-04 16:25:18 +1000775/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000776 * Head
777 *****************************************************************************/
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000778static void
Ben Skeggsad633612016-11-04 17:20:36 +1000779nv50_head_core_clr(struct nv50_head *head)
780{
781 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
782 u32 *push;
783 if ((push = evo_wait(core, 2))) {
784 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
785 evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
786 else
787 evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
788 evo_data(push, 0x00000000);
789 evo_kick(push, core);
790 }
791}
792
793static void
794nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
795{
796 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
797 u32 *push;
798 if ((push = evo_wait(core, 9))) {
799 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
800 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
801 evo_data(push, asyh->core.offset >> 8);
802 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
803 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
804 evo_data(push, asyh->core.layout << 20 |
805 (asyh->core.pitch >> 8) << 8 |
806 asyh->core.block);
807 evo_data(push, asyh->core.kind << 16 |
808 asyh->core.format << 8);
809 evo_data(push, asyh->core.handle);
810 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
811 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
812 } else
813 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
814 evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
815 evo_data(push, asyh->core.offset >> 8);
816 evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
817 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
818 evo_data(push, asyh->core.layout << 20 |
819 (asyh->core.pitch >> 8) << 8 |
820 asyh->core.block);
821 evo_data(push, asyh->core.format << 8);
822 evo_data(push, asyh->core.handle);
823 evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
824 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
825 } else {
826 evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
827 evo_data(push, asyh->core.offset >> 8);
828 evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
829 evo_data(push, (asyh->core.h << 16) | asyh->core.w);
830 evo_data(push, asyh->core.layout << 24 |
831 (asyh->core.pitch >> 8) << 8 |
832 asyh->core.block);
833 evo_data(push, asyh->core.format << 8);
834 evo_data(push, asyh->core.handle);
835 evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
836 evo_data(push, (asyh->core.y << 16) | asyh->core.x);
837 }
838 evo_kick(push, core);
839 }
840}
841
842static void
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000843nv50_head_lut_clr(struct nv50_head *head)
844{
845 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
846 u32 *push;
847 if ((push = evo_wait(core, 4))) {
848 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
849 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
850 evo_data(push, 0x40000000);
851 } else
852 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
853 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
854 evo_data(push, 0x40000000);
855 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
856 evo_data(push, 0x00000000);
857 } else {
858 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
859 evo_data(push, 0x03000000);
860 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
861 evo_data(push, 0x00000000);
862 }
863 evo_kick(push, core);
864 }
865}
866
867static void
868nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
869{
870 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
871 u32 *push;
872 if ((push = evo_wait(core, 7))) {
873 if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
874 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
875 evo_data(push, 0xc0000000);
876 evo_data(push, asyh->lut.offset >> 8);
877 } else
878 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
879 evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
880 evo_data(push, 0xc0000000);
881 evo_data(push, asyh->lut.offset >> 8);
882 evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
883 evo_data(push, asyh->lut.handle);
884 } else {
885 evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
886 evo_data(push, 0x83000000);
887 evo_data(push, asyh->lut.offset >> 8);
888 evo_data(push, 0x00000000);
889 evo_data(push, 0x00000000);
890 evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
891 evo_data(push, asyh->lut.handle);
892 }
893 evo_kick(push, core);
894 }
895}
896
897static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000898nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
899{
900 struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
901 struct nv50_head_mode *m = &asyh->mode;
902 u32 *push;
903 if ((push = evo_wait(core, 14))) {
904 if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
905 evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
906 evo_data(push, 0x00800000 | m->clock);
907 evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
908 evo_mthd(push, 0x0810 + (head->base.index * 0x400), 6);
909 evo_data(push, 0x00000000);
910 evo_data(push, (m->v.active << 16) | m->h.active );
911 evo_data(push, (m->v.synce << 16) | m->h.synce );
912 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
913 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
914 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
915 evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
916 evo_data(push, 0x00000000);
917 } else {
918 evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
919 evo_data(push, 0x00000000);
920 evo_data(push, (m->v.active << 16) | m->h.active );
921 evo_data(push, (m->v.synce << 16) | m->h.synce );
922 evo_data(push, (m->v.blanke << 16) | m->h.blanke );
923 evo_data(push, (m->v.blanks << 16) | m->h.blanks );
924 evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
925 evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
926 evo_data(push, 0x00000000); /* ??? */
927 evo_data(push, 0xffffff00);
928 evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
929 evo_data(push, m->clock * 1000);
930 evo_data(push, 0x00200000); /* ??? */
931 evo_data(push, m->clock * 1000);
932 }
933 evo_kick(push, core);
934 }
935}
936
937static void
Ben Skeggsad633612016-11-04 17:20:36 +1000938nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
939{
940 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000941 nv50_head_lut_clr(head);
942 if (asyh->clr.core && (!asyh->set.core || y))
Ben Skeggsad633612016-11-04 17:20:36 +1000943 nv50_head_core_clr(head);
944}
945
946static void
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000947nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
948{
949 if (asyh->set.mode ) nv50_head_mode (head, asyh);
Ben Skeggsa7ae1562016-11-04 17:20:36 +1000950 if (asyh->set.core ) nv50_head_lut_set (head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +1000951 if (asyh->set.core ) nv50_head_core_set(head, asyh);
Ben Skeggs3dbd0362016-11-04 17:20:36 +1000952}
953
954static void
955nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
956{
957 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
958 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
959 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
960 u32 hbackp = mode->htotal - mode->hsync_end;
961 u32 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
962 u32 hfrontp = mode->hsync_start - mode->hdisplay;
963 u32 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
964 struct nv50_head_mode *m = &asyh->mode;
965
966 m->h.active = mode->htotal;
967 m->h.synce = mode->hsync_end - mode->hsync_start - 1;
968 m->h.blanke = m->h.synce + hbackp;
969 m->h.blanks = mode->htotal - hfrontp - 1;
970
971 m->v.active = mode->vtotal * vscan / ilace;
972 m->v.synce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
973 m->v.blanke = m->v.synce + vbackp;
974 m->v.blanks = m->v.active - vfrontp - 1;
975
976 /*XXX: Safe underestimate, even "0" works */
977 m->v.blankus = (m->v.active - mode->vdisplay - 2) * m->h.active;
978 m->v.blankus *= 1000;
979 m->v.blankus /= mode->clock;
980
981 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
982 m->v.blank2e = m->v.active + m->v.synce + vbackp;
983 m->v.blank2s = m->v.blank2e + (mode->vdisplay * vscan / ilace);
984 m->v.active = (m->v.active * 2) + 1;
985 m->interlace = true;
986 } else {
987 m->v.blank2e = 0;
988 m->v.blank2s = 1;
989 m->interlace = false;
990 }
991 m->clock = mode->clock;
992
993 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
994 asyh->set.mode = true;
995}
996
997static int
998nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
999{
1000 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggsad633612016-11-04 17:20:36 +10001001 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001002 struct nv50_head *head = nv50_head(crtc);
1003 struct nv50_head_atom *armh = &head->arm;
1004 struct nv50_head_atom *asyh = nv50_head_atom(state);
1005
1006 NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
Ben Skeggsad633612016-11-04 17:20:36 +10001007 asyh->clr.mask = 0;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001008 asyh->set.mask = 0;
1009
1010 if (asyh->state.active) {
1011 if (asyh->state.mode_changed)
1012 nv50_head_atomic_check_mode(head, asyh);
Ben Skeggsad633612016-11-04 17:20:36 +10001013
1014 if ((asyh->core.visible = (asyh->base.cpp != 0))) {
1015 asyh->core.x = asyh->base.x;
1016 asyh->core.y = asyh->base.y;
1017 asyh->core.w = asyh->base.w;
1018 asyh->core.h = asyh->base.h;
1019 } else
1020 if ((asyh->core.visible = true)) {
1021 /*XXX: We need to either find some way of having the
1022 * primary base layer appear black, while still
1023 * being able to display the other layers, or we
1024 * need to allocate a dummy black surface here.
1025 */
1026 asyh->core.x = 0;
1027 asyh->core.y = 0;
1028 asyh->core.w = asyh->state.mode.hdisplay;
1029 asyh->core.h = asyh->state.mode.vdisplay;
1030 }
1031 asyh->core.handle = disp->mast.base.vram.handle;
1032 asyh->core.offset = 0;
1033 asyh->core.format = 0xcf;
1034 asyh->core.kind = 0;
1035 asyh->core.layout = 1;
1036 asyh->core.block = 0;
1037 asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001038 asyh->lut.handle = disp->mast.base.vram.handle;
1039 asyh->lut.offset = head->base.lut.nvbo->bo.offset;
Ben Skeggsad633612016-11-04 17:20:36 +10001040 } else {
1041 asyh->core.visible = false;
1042 }
1043
1044 if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
1045 if (asyh->core.visible) {
1046 if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
1047 asyh->set.core = true;
1048 } else
1049 if (armh->core.visible) {
1050 asyh->clr.core = true;
1051 }
1052 } else {
1053 asyh->clr.core = armh->core.visible;
1054 asyh->set.core = asyh->core.visible;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001055 }
1056
1057 memcpy(armh, asyh, sizeof(*asyh));
1058 asyh->state.mode_changed = 0;
1059 return 0;
1060}
1061
1062/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +10001063 * CRTC
1064 *****************************************************************************/
1065static int
Ben Skeggse225f442012-11-21 14:40:21 +10001066nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001067{
Ben Skeggse225f442012-11-21 14:40:21 +10001068 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +10001069 struct nouveau_connector *nv_connector;
1070 struct drm_connector *connector;
1071 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001072
Ben Skeggs488ff202011-10-17 10:38:10 +10001073 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +10001074 connector = &nv_connector->base;
1075 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -07001076 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +10001077 mode = DITHERING_MODE_DYNAMIC2X2;
1078 } else {
1079 mode = nv_connector->dithering_mode;
1080 }
1081
1082 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
1083 if (connector->display_info.bpc >= 8)
1084 mode |= DITHERING_DEPTH_8BPC;
1085 } else {
1086 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001087 }
1088
Ben Skeggsde8268c2012-11-16 10:24:31 +10001089 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001090 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001091 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001092 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
1093 evo_data(push, mode);
1094 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001095 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001096 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
1097 evo_data(push, mode);
1098 } else {
1099 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
1100 evo_data(push, mode);
1101 }
1102
Ben Skeggs438d99e2011-07-05 16:48:06 +10001103 if (update) {
1104 evo_mthd(push, 0x0080, 1);
1105 evo_data(push, 0x00000000);
1106 }
Ben Skeggsde8268c2012-11-16 10:24:31 +10001107 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001108 }
1109
1110 return 0;
1111}
1112
1113static int
Ben Skeggse225f442012-11-21 14:40:21 +10001114nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001115{
Ben Skeggse225f442012-11-21 14:40:21 +10001116 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +10001117 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +10001118 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +10001119 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +10001120 int mode = DRM_MODE_SCALE_NONE;
1121 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001122
Ben Skeggs92854622011-11-11 23:49:06 +10001123 /* start off at the resolution we programmed the crtc for, this
1124 * effectively handles NONE/FULL scaling
1125 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +10001126 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs576f7912014-12-22 17:19:26 +10001127 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs92854622011-11-11 23:49:06 +10001128 mode = nv_connector->scaling_mode;
Ben Skeggs576f7912014-12-22 17:19:26 +10001129 if (nv_connector->scaling_full) /* non-EDID LVDS/eDP mode */
1130 mode = DRM_MODE_SCALE_FULLSCREEN;
1131 }
Ben Skeggsf3fdc522011-07-07 16:01:57 +10001132
Ben Skeggs92854622011-11-11 23:49:06 +10001133 if (mode != DRM_MODE_SCALE_NONE)
1134 omode = nv_connector->native_mode;
1135 else
1136 omode = umode;
1137
1138 oX = omode->hdisplay;
1139 oY = omode->vdisplay;
1140 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
1141 oY *= 2;
1142
1143 /* add overscan compensation if necessary, will keep the aspect
1144 * ratio the same as the backend mode unless overridden by the
1145 * user setting both hborder and vborder properties.
1146 */
1147 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
1148 (nv_connector->underscan == UNDERSCAN_AUTO &&
Ben Skeggs92854622011-11-11 23:49:06 +10001149 drm_detect_hdmi_monitor(nv_connector->edid)))) {
1150 u32 bX = nv_connector->underscan_hborder;
1151 u32 bY = nv_connector->underscan_vborder;
1152 u32 aspect = (oY << 19) / oX;
1153
1154 if (bX) {
1155 oX -= (bX * 2);
1156 if (bY) oY -= (bY * 2);
1157 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
1158 } else {
1159 oX -= (oX >> 4) + 32;
1160 if (bY) oY -= (bY * 2);
1161 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +10001162 }
1163 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001164
Ben Skeggs92854622011-11-11 23:49:06 +10001165 /* handle CENTER/ASPECT scaling, taking into account the areas
1166 * removed already for overscan compensation
1167 */
1168 switch (mode) {
1169 case DRM_MODE_SCALE_CENTER:
1170 oX = min((u32)umode->hdisplay, oX);
1171 oY = min((u32)umode->vdisplay, oY);
1172 /* fall-through */
1173 case DRM_MODE_SCALE_ASPECT:
1174 if (oY < oX) {
1175 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
1176 oX = ((oY * aspect) + (aspect / 2)) >> 19;
1177 } else {
1178 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
1179 oY = ((oX * aspect) + (aspect / 2)) >> 19;
1180 }
1181 break;
1182 default:
1183 break;
1184 }
1185
Ben Skeggsde8268c2012-11-16 10:24:31 +10001186 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001187 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001188 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001189 /*XXX: SCALE_CTRL_ACTIVE??? */
1190 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
1191 evo_data(push, (oY << 16) | oX);
1192 evo_data(push, (oY << 16) | oX);
1193 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
1194 evo_data(push, 0x00000000);
1195 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
1196 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
1197 } else {
1198 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
1199 evo_data(push, (oY << 16) | oX);
1200 evo_data(push, (oY << 16) | oX);
1201 evo_data(push, (oY << 16) | oX);
1202 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
1203 evo_data(push, 0x00000000);
1204 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
1205 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
1206 }
1207
1208 evo_kick(push, mast);
1209
Ben Skeggs3376ee32011-11-12 14:28:12 +10001210 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +10001211 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001212 nv50_display_flip_next(crtc, crtc->primary->fb,
1213 NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001214 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001215 }
1216
1217 return 0;
1218}
1219
1220static int
Roy Splieteae73822014-10-30 22:57:45 +01001221nv50_crtc_set_raster_vblank_dmi(struct nouveau_crtc *nv_crtc, u32 usec)
1222{
1223 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
1224 u32 *push;
1225
1226 push = evo_wait(mast, 8);
1227 if (!push)
1228 return -ENOMEM;
1229
1230 evo_mthd(push, 0x0828 + (nv_crtc->index * 0x400), 1);
1231 evo_data(push, usec);
1232 evo_kick(push, mast);
1233 return 0;
1234}
1235
1236static int
Ben Skeggse225f442012-11-21 14:40:21 +10001237nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +10001238{
Ben Skeggse225f442012-11-21 14:40:21 +10001239 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +10001240 u32 *push, hue, vib;
1241 int adj;
1242
1243 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
1244 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
1245 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
1246
1247 push = evo_wait(mast, 16);
1248 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001249 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsf9887d02012-11-21 13:03:42 +10001250 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
1251 evo_data(push, (hue << 20) | (vib << 8));
1252 } else {
1253 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
1254 evo_data(push, (hue << 20) | (vib << 8));
1255 }
1256
1257 if (update) {
1258 evo_mthd(push, 0x0080, 1);
1259 evo_data(push, 0x00000000);
1260 }
1261 evo_kick(push, mast);
1262 }
1263
1264 return 0;
1265}
1266
1267static int
Ben Skeggse225f442012-11-21 14:40:21 +10001268nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001269 int x, int y, bool update)
1270{
1271 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggsad633612016-11-04 17:20:36 +10001272 struct nv50_head *head = nv50_head(&nv_crtc->base);
1273 struct nv50_head_atom *asyh = &head->asy;
1274 const struct drm_format_info *info;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001275
Ben Skeggsad633612016-11-04 17:20:36 +10001276 info = drm_format_info(nvfb->base.pixel_format);
1277 if (!info || !info->depth)
1278 return -EINVAL;
Ben Skeggsde8268c2012-11-16 10:24:31 +10001279
Ben Skeggsad633612016-11-04 17:20:36 +10001280 asyh->base.depth = info->depth;
1281 asyh->base.cpp = info->cpp[0];
1282 asyh->base.x = x;
1283 asyh->base.y = y;
1284 asyh->base.w = nvfb->base.width;
1285 asyh->base.h = nvfb->base.height;
1286 nv50_head_atomic_check(&head->base.base, &asyh->state);
1287 nv50_head_flush_set(head, asyh);
1288
1289 if (update) {
1290 struct nv50_mast *core = nv50_mast(nv_crtc->base.dev);
1291 u32 *push = evo_wait(core, 2);
1292 if (push) {
Ben Skeggsa46232e2011-07-07 15:23:48 +10001293 evo_mthd(push, 0x0080, 1);
1294 evo_data(push, 0x00000000);
Ben Skeggsad633612016-11-04 17:20:36 +10001295 evo_kick(push, core);
Ben Skeggsa46232e2011-07-07 15:23:48 +10001296 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001297 }
1298
Ben Skeggs8a423642014-08-10 04:10:19 +10001299 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001300 return 0;
1301}
1302
1303static void
Ben Skeggse225f442012-11-21 14:40:21 +10001304nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001305{
Ben Skeggse225f442012-11-21 14:40:21 +10001306 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001307 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001308 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001309 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001310 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
1311 evo_data(push, 0x85000000);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001312 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001313 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001314 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001315 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
1316 evo_data(push, 0x85000000);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001317 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001318 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001319 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001320 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001321 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
1322 evo_data(push, 0x85000000);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001323 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001324 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001325 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001326 }
1327 evo_kick(push, mast);
1328 }
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001329 nv_crtc->cursor.visible = true;
Ben Skeggsde8268c2012-11-16 10:24:31 +10001330}
1331
1332static void
Ben Skeggse225f442012-11-21 14:40:21 +10001333nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +10001334{
Ben Skeggse225f442012-11-21 14:40:21 +10001335 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001336 u32 *push = evo_wait(mast, 16);
1337 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001338 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001339 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
1340 evo_data(push, 0x05000000);
1341 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001342 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001343 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
1344 evo_data(push, 0x05000000);
1345 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
1346 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001347 } else {
1348 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
1349 evo_data(push, 0x05000000);
1350 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
1351 evo_data(push, 0x00000000);
1352 }
Ben Skeggsde8268c2012-11-16 10:24:31 +10001353 evo_kick(push, mast);
1354 }
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001355 nv_crtc->cursor.visible = false;
Ben Skeggsde8268c2012-11-16 10:24:31 +10001356}
Ben Skeggs438d99e2011-07-05 16:48:06 +10001357
Ben Skeggsde8268c2012-11-16 10:24:31 +10001358static void
Ben Skeggse225f442012-11-21 14:40:21 +10001359nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +10001360{
Ben Skeggse225f442012-11-21 14:40:21 +10001361 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001362
Ben Skeggs697bb722015-07-28 17:20:57 +10001363 if (show && nv_crtc->cursor.nvbo && nv_crtc->base.enabled)
Ben Skeggse225f442012-11-21 14:40:21 +10001364 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001365 else
Ben Skeggse225f442012-11-21 14:40:21 +10001366 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001367
1368 if (update) {
1369 u32 *push = evo_wait(mast, 2);
1370 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001371 evo_mthd(push, 0x0080, 1);
1372 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001373 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001374 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001375 }
1376}
1377
1378static void
Ben Skeggse225f442012-11-21 14:40:21 +10001379nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001380{
1381}
1382
1383static void
Ben Skeggse225f442012-11-21 14:40:21 +10001384nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001385{
1386 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggsad633612016-11-04 17:20:36 +10001387 struct nv50_head *head = nv50_head(crtc);
1388 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001389
Ben Skeggse225f442012-11-21 14:40:21 +10001390 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001391
Ben Skeggsad633612016-11-04 17:20:36 +10001392 asyh->state.active = false;
1393 nv50_head_atomic_check(&head->base.base, &asyh->state);
1394 nv50_head_flush_clr(head, asyh, false);
1395
Ben Skeggse225f442012-11-21 14:40:21 +10001396 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001397}
1398
1399static void
Ben Skeggse225f442012-11-21 14:40:21 +10001400nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001401{
1402 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001403 struct nv50_head *head = nv50_head(crtc);
1404 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001405
Ben Skeggsa7ae1562016-11-04 17:20:36 +10001406 asyh->state.active = true;
1407 nv50_head_atomic_check(&head->base.base, &asyh->state);
1408 nv50_head_flush_set(head, asyh);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001409
Ben Skeggs5a560252014-11-10 15:52:02 +10001410 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Matt Roperf4510a22014-04-01 15:22:40 -07001411 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001412}
1413
1414static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001415nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001416 struct drm_display_mode *adjusted_mode)
1417{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001418 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001419 return true;
1420}
1421
1422static int
Ben Skeggse225f442012-11-21 14:40:21 +10001423nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001424{
Matt Roperf4510a22014-04-01 15:22:40 -07001425 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001426 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001427 int ret;
1428
Ben Skeggs547ad072014-11-10 12:35:06 +10001429 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001430 if (ret == 0) {
1431 if (head->image)
1432 nouveau_bo_unpin(head->image);
1433 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001434 }
1435
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001436 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001437}
1438
1439static int
Ben Skeggse225f442012-11-21 14:40:21 +10001440nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001441 struct drm_display_mode *mode, int x, int y,
1442 struct drm_framebuffer *old_fb)
1443{
Ben Skeggse225f442012-11-21 14:40:21 +10001444 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001445 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1446 struct nouveau_connector *nv_connector;
Ben Skeggs3488c572012-03-12 11:42:20 +10001447 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001448 int ret;
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001449 struct nv50_head *head = nv50_head(crtc);
1450 struct nv50_head_atom *asyh = &head->asy;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001451
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001452 memcpy(&asyh->state.mode, umode, sizeof(*umode));
1453 memcpy(&asyh->state.adjusted_mode, mode, sizeof(*mode));
1454 asyh->state.active = true;
1455 asyh->state.mode_changed = true;
1456 nv50_head_atomic_check(&head->base.base, &asyh->state);
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001457
Ben Skeggse225f442012-11-21 14:40:21 +10001458 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001459 if (ret)
1460 return ret;
1461
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001462 nv50_head_flush_set(head, asyh);
1463
Ben Skeggsde8268c2012-11-16 10:24:31 +10001464 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001465 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001466 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001467 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1468 evo_data(push, 0x00000311);
1469 evo_data(push, 0x00000100);
1470 } else {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001471 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1472 evo_data(push, 0x00000311);
1473 evo_data(push, 0x00000100);
1474 }
Ben Skeggsde8268c2012-11-16 10:24:31 +10001475 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001476 }
1477
1478 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001479 nv50_crtc_set_dither(nv_crtc, false);
1480 nv50_crtc_set_scale(nv_crtc, false);
Roy Splieteae73822014-10-30 22:57:45 +01001481
1482 /* G94 only accepts this after setting scale */
1483 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA)
Ben Skeggs3dbd0362016-11-04 17:20:36 +10001484 nv50_crtc_set_raster_vblank_dmi(nv_crtc, asyh->mode.v.blankus);
Roy Splieteae73822014-10-30 22:57:45 +01001485
Ben Skeggse225f442012-11-21 14:40:21 +10001486 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001487 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001488 return 0;
1489}
1490
1491static int
Ben Skeggse225f442012-11-21 14:40:21 +10001492nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001493 struct drm_framebuffer *old_fb)
1494{
Ben Skeggs77145f12012-07-31 16:16:21 +10001495 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001496 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1497 int ret;
1498
Matt Roperf4510a22014-04-01 15:22:40 -07001499 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001500 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001501 return 0;
1502 }
1503
Ben Skeggse225f442012-11-21 14:40:21 +10001504 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001505 if (ret)
1506 return ret;
1507
Ben Skeggse225f442012-11-21 14:40:21 +10001508 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001509 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1510 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001511 return 0;
1512}
1513
1514static int
Ben Skeggse225f442012-11-21 14:40:21 +10001515nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001516 struct drm_framebuffer *fb, int x, int y,
1517 enum mode_set_atomic state)
1518{
1519 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001520 nv50_display_flip_stop(crtc);
1521 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001522 return 0;
1523}
1524
1525static void
Ben Skeggse225f442012-11-21 14:40:21 +10001526nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001527{
Ben Skeggse225f442012-11-21 14:40:21 +10001528 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001529 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1530 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1531 int i;
1532
1533 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001534 u16 r = nv_crtc->lut.r[i] >> 2;
1535 u16 g = nv_crtc->lut.g[i] >> 2;
1536 u16 b = nv_crtc->lut.b[i] >> 2;
1537
Ben Skeggs648d4df2014-08-10 04:10:27 +10001538 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001539 writew(r + 0x0000, lut + (i * 0x08) + 0);
1540 writew(g + 0x0000, lut + (i * 0x08) + 2);
1541 writew(b + 0x0000, lut + (i * 0x08) + 4);
1542 } else {
1543 writew(r + 0x6000, lut + (i * 0x20) + 0);
1544 writew(g + 0x6000, lut + (i * 0x20) + 2);
1545 writew(b + 0x6000, lut + (i * 0x20) + 4);
1546 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001547 }
1548}
1549
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001550static void
1551nv50_crtc_disable(struct drm_crtc *crtc)
1552{
1553 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001554 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001555 if (head->image)
1556 nouveau_bo_unpin(head->image);
1557 nouveau_bo_ref(NULL, &head->image);
1558}
1559
Ben Skeggs438d99e2011-07-05 16:48:06 +10001560static int
Ben Skeggse225f442012-11-21 14:40:21 +10001561nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001562 uint32_t handle, uint32_t width, uint32_t height)
1563{
1564 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs5a560252014-11-10 15:52:02 +10001565 struct drm_gem_object *gem = NULL;
1566 struct nouveau_bo *nvbo = NULL;
1567 int ret = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001568
Ben Skeggs5a560252014-11-10 15:52:02 +10001569 if (handle) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001570 if (width != 64 || height != 64)
1571 return -EINVAL;
1572
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001573 gem = drm_gem_object_lookup(file_priv, handle);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001574 if (unlikely(!gem))
1575 return -ENOENT;
1576 nvbo = nouveau_gem_object(gem);
1577
Ben Skeggs5a560252014-11-10 15:52:02 +10001578 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001579 }
1580
Ben Skeggs5a560252014-11-10 15:52:02 +10001581 if (ret == 0) {
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001582 if (nv_crtc->cursor.nvbo)
1583 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1584 nouveau_bo_ref(nvbo, &nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001585 }
Ben Skeggs5a560252014-11-10 15:52:02 +10001586 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001587
Ben Skeggs5a560252014-11-10 15:52:02 +10001588 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001589 return ret;
1590}
1591
1592static int
Ben Skeggse225f442012-11-21 14:40:21 +10001593nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001594{
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001595 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001596 struct nv50_curs *curs = nv50_curs(crtc);
1597 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001598 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1599 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001600
1601 nv_crtc->cursor_saved_x = x;
1602 nv_crtc->cursor_saved_y = y;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001603 return 0;
1604}
1605
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001606static int
Ben Skeggse225f442012-11-21 14:40:21 +10001607nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001608 uint32_t size)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001609{
1610 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001611 u32 i;
1612
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001613 for (i = 0; i < size; i++) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001614 nv_crtc->lut.r[i] = r[i];
1615 nv_crtc->lut.g[i] = g[i];
1616 nv_crtc->lut.b[i] = b[i];
1617 }
1618
Ben Skeggse225f442012-11-21 14:40:21 +10001619 nv50_crtc_lut_load(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001620
1621 return 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001622}
1623
1624static void
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001625nv50_crtc_cursor_restore(struct nouveau_crtc *nv_crtc, int x, int y)
1626{
1627 nv50_crtc_cursor_move(&nv_crtc->base, x, y);
1628
1629 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
1630}
1631
1632static void
Ben Skeggse225f442012-11-21 14:40:21 +10001633nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001634{
1635 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001636 struct nv50_disp *disp = nv50_disp(crtc->dev);
1637 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001638 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001639
Ben Skeggs0ad72862014-08-10 04:10:22 +10001640 list_for_each_entry(fbdma, &disp->fbdma, head) {
1641 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1642 }
1643
1644 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1645 nv50_pioc_destroy(&head->oimm.base);
1646 nv50_dmac_destroy(&head->sync.base, disp->disp);
1647 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001648
1649 /*XXX: this shouldn't be necessary, but the core doesn't call
1650 * disconnect() during the cleanup paths
1651 */
1652 if (head->image)
1653 nouveau_bo_unpin(head->image);
1654 nouveau_bo_ref(NULL, &head->image);
1655
Ben Skeggs5a560252014-11-10 15:52:02 +10001656 /*XXX: ditto */
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001657 if (nv_crtc->cursor.nvbo)
1658 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1659 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001660
Ben Skeggs438d99e2011-07-05 16:48:06 +10001661 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001662 if (nv_crtc->lut.nvbo)
1663 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001664 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001665
Ben Skeggs438d99e2011-07-05 16:48:06 +10001666 drm_crtc_cleanup(crtc);
1667 kfree(crtc);
1668}
1669
Ben Skeggse225f442012-11-21 14:40:21 +10001670static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1671 .dpms = nv50_crtc_dpms,
1672 .prepare = nv50_crtc_prepare,
1673 .commit = nv50_crtc_commit,
1674 .mode_fixup = nv50_crtc_mode_fixup,
1675 .mode_set = nv50_crtc_mode_set,
1676 .mode_set_base = nv50_crtc_mode_set_base,
1677 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1678 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001679 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001680};
1681
Ben Skeggse225f442012-11-21 14:40:21 +10001682static const struct drm_crtc_funcs nv50_crtc_func = {
1683 .cursor_set = nv50_crtc_cursor_set,
1684 .cursor_move = nv50_crtc_cursor_move,
1685 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001686 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001687 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001688 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001689};
1690
1691static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001692nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001693{
Ben Skeggsa01ca782015-08-20 14:54:15 +10001694 struct nouveau_drm *drm = nouveau_drm(dev);
1695 struct nvif_device *device = &drm->device;
Ben Skeggse225f442012-11-21 14:40:21 +10001696 struct nv50_disp *disp = nv50_disp(dev);
1697 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001698 struct drm_crtc *crtc;
1699 int ret, i;
1700
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001701 head = kzalloc(sizeof(*head), GFP_KERNEL);
1702 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001703 return -ENOMEM;
1704
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001705 head->base.index = index;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001706 head->base.color_vibrance = 50;
1707 head->base.vibrant_hue = 0;
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01001708 head->base.cursor.set_pos = nv50_crtc_cursor_restore;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001709 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001710 head->base.lut.r[i] = i << 8;
1711 head->base.lut.g[i] = i << 8;
1712 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001713 }
1714
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001715 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001716 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1717 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001718 drm_mode_crtc_set_gamma_size(crtc, 256);
1719
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001720 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01001721 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001722 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10001723 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001724 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001725 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001726 if (ret)
1727 nouveau_bo_unpin(head->base.lut.nvbo);
1728 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001729 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001730 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001731 }
1732
1733 if (ret)
1734 goto out;
1735
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001736 /* allocate cursor resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10001737 ret = nv50_curs_create(device, disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001738 if (ret)
1739 goto out;
1740
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001741 /* allocate page flip / sync resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10001742 ret = nv50_base_create(device, disp->disp, index, disp->sync->bo.offset,
1743 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001744 if (ret)
1745 goto out;
1746
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001747 head->sync.addr = EVO_FLIP_SEM0(index);
1748 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001749
1750 /* allocate overlay resources */
Ben Skeggsa01ca782015-08-20 14:54:15 +10001751 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001752 if (ret)
1753 goto out;
1754
Ben Skeggsa01ca782015-08-20 14:54:15 +10001755 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
1756 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001757 if (ret)
1758 goto out;
1759
Ben Skeggs438d99e2011-07-05 16:48:06 +10001760out:
1761 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001762 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001763 return ret;
1764}
1765
1766/******************************************************************************
Ben Skeggsa91d3222014-12-22 16:30:13 +10001767 * Encoder helpers
1768 *****************************************************************************/
1769static bool
1770nv50_encoder_mode_fixup(struct drm_encoder *encoder,
1771 const struct drm_display_mode *mode,
1772 struct drm_display_mode *adjusted_mode)
1773{
1774 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1775 struct nouveau_connector *nv_connector;
1776
1777 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1778 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs576f7912014-12-22 17:19:26 +10001779 nv_connector->scaling_full = false;
1780 if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) {
1781 switch (nv_connector->type) {
1782 case DCB_CONNECTOR_LVDS:
1783 case DCB_CONNECTOR_LVDS_SPWG:
1784 case DCB_CONNECTOR_eDP:
1785 /* force use of scaler for non-edid modes */
1786 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
1787 return true;
1788 nv_connector->scaling_full = true;
1789 break;
1790 default:
1791 return true;
1792 }
1793 }
1794
1795 drm_mode_copy(adjusted_mode, nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10001796 }
1797
1798 return true;
1799}
1800
1801/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001802 * DAC
1803 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001804static void
Ben Skeggse225f442012-11-21 14:40:21 +10001805nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001806{
1807 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001808 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001809 struct {
1810 struct nv50_disp_mthd_v1 base;
1811 struct nv50_disp_dac_pwr_v0 pwr;
1812 } args = {
1813 .base.version = 1,
1814 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1815 .base.hasht = nv_encoder->dcb->hasht,
1816 .base.hashm = nv_encoder->dcb->hashm,
1817 .pwr.state = 1,
1818 .pwr.data = 1,
1819 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1820 mode != DRM_MODE_DPMS_OFF),
1821 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1822 mode != DRM_MODE_DPMS_OFF),
1823 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001824
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001825 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001826}
1827
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001828static void
Ben Skeggse225f442012-11-21 14:40:21 +10001829nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001830{
1831}
1832
1833static void
Ben Skeggse225f442012-11-21 14:40:21 +10001834nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001835 struct drm_display_mode *adjusted_mode)
1836{
Ben Skeggse225f442012-11-21 14:40:21 +10001837 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001838 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1839 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001840 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001841
Ben Skeggse225f442012-11-21 14:40:21 +10001842 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001843
Ben Skeggs97b19b52012-11-16 11:21:37 +10001844 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001845 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001846 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001847 u32 syncs = 0x00000000;
1848
1849 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1850 syncs |= 0x00000001;
1851 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1852 syncs |= 0x00000002;
1853
1854 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1855 evo_data(push, 1 << nv_crtc->index);
1856 evo_data(push, syncs);
1857 } else {
1858 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1859 u32 syncs = 0x00000001;
1860
1861 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1862 syncs |= 0x00000008;
1863 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1864 syncs |= 0x00000010;
1865
1866 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1867 magic |= 0x00000001;
1868
1869 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1870 evo_data(push, syncs);
1871 evo_data(push, magic);
1872 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1873 evo_data(push, 1 << nv_crtc->index);
1874 }
1875
1876 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001877 }
1878
1879 nv_encoder->crtc = encoder->crtc;
1880}
1881
1882static void
Ben Skeggse225f442012-11-21 14:40:21 +10001883nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001884{
1885 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001886 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001887 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001888 u32 *push;
1889
1890 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001891 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001892
Ben Skeggs97b19b52012-11-16 11:21:37 +10001893 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001894 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001895 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001896 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1897 evo_data(push, 0x00000000);
1898 } else {
1899 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1900 evo_data(push, 0x00000000);
1901 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001902 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001903 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001904 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001905
1906 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001907}
1908
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001909static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001910nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001911{
Ben Skeggsc4abd312014-08-10 04:10:26 +10001912 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001913 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10001914 struct {
1915 struct nv50_disp_mthd_v1 base;
1916 struct nv50_disp_dac_load_v0 load;
1917 } args = {
1918 .base.version = 1,
1919 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
1920 .base.hasht = nv_encoder->dcb->hasht,
1921 .base.hashm = nv_encoder->dcb->hashm,
1922 };
1923 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10001924
Ben Skeggsc4abd312014-08-10 04:10:26 +10001925 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
1926 if (args.load.data == 0)
1927 args.load.data = 340;
1928
1929 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
1930 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001931 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001932
Ben Skeggs35b21d32012-11-08 12:08:55 +10001933 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001934}
1935
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001936static void
Ben Skeggse225f442012-11-21 14:40:21 +10001937nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001938{
1939 drm_encoder_cleanup(encoder);
1940 kfree(encoder);
1941}
1942
Ben Skeggse225f442012-11-21 14:40:21 +10001943static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1944 .dpms = nv50_dac_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10001945 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggse225f442012-11-21 14:40:21 +10001946 .prepare = nv50_dac_disconnect,
1947 .commit = nv50_dac_commit,
1948 .mode_set = nv50_dac_mode_set,
1949 .disable = nv50_dac_disconnect,
1950 .get_crtc = nv50_display_crtc_get,
1951 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001952};
1953
Ben Skeggse225f442012-11-21 14:40:21 +10001954static const struct drm_encoder_funcs nv50_dac_func = {
1955 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001956};
1957
1958static int
Ben Skeggse225f442012-11-21 14:40:21 +10001959nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001960{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001961 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10001962 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001963 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001964 struct nouveau_encoder *nv_encoder;
1965 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001966 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001967
1968 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1969 if (!nv_encoder)
1970 return -ENOMEM;
1971 nv_encoder->dcb = dcbe;
1972 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001973
1974 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1975 if (bus)
1976 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001977
1978 encoder = to_drm_encoder(nv_encoder);
1979 encoder->possible_crtcs = dcbe->heads;
1980 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10001981 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
1982 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggse225f442012-11-21 14:40:21 +10001983 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001984
1985 drm_mode_connector_attach_encoder(connector, encoder);
1986 return 0;
1987}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001988
1989/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001990 * Audio
1991 *****************************************************************************/
1992static void
Ben Skeggse225f442012-11-21 14:40:21 +10001993nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001994{
1995 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10001996 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10001997 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001998 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10001999 struct __packed {
2000 struct {
2001 struct nv50_disp_mthd_v1 mthd;
2002 struct nv50_disp_sor_hda_eld_v0 eld;
2003 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10002004 u8 data[sizeof(nv_connector->base.eld)];
2005 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10002006 .base.mthd.version = 1,
2007 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2008 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002009 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2010 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002011 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002012
2013 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2014 if (!drm_detect_monitor_audio(nv_connector->edid))
2015 return;
2016
Ben Skeggs78951d22011-11-11 18:13:13 +10002017 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002018 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002019
Jani Nikula938fd8a2014-10-28 16:20:48 +02002020 nvif_mthd(disp->disp, 0, &args,
2021 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10002022}
2023
2024static void
Ben Skeggscc2a9072014-09-15 21:29:05 +10002025nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002026{
2027 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002028 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10002029 struct {
2030 struct nv50_disp_mthd_v1 base;
2031 struct nv50_disp_sor_hda_eld_v0 eld;
2032 } args = {
2033 .base.version = 1,
2034 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
2035 .base.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10002036 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2037 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10002038 };
Ben Skeggs78951d22011-11-11 18:13:13 +10002039
Ben Skeggs120b0c32014-08-10 04:10:26 +10002040 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002041}
2042
2043/******************************************************************************
2044 * HDMI
2045 *****************************************************************************/
2046static void
Ben Skeggse225f442012-11-21 14:40:21 +10002047nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10002048{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002049 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2050 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10002051 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002052 struct {
2053 struct nv50_disp_mthd_v1 base;
2054 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
2055 } args = {
2056 .base.version = 1,
2057 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2058 .base.hasht = nv_encoder->dcb->hasht,
2059 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2060 (0x0100 << nv_crtc->index),
2061 .pwr.state = 1,
2062 .pwr.rekey = 56, /* binary driver, and tegra, constant */
2063 };
2064 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002065 u32 max_ac_packet;
2066
2067 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2068 if (!drm_detect_hdmi_monitor(nv_connector->edid))
2069 return;
2070
2071 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10002072 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002073 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10002074 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002075
Ben Skeggse00f2232014-08-10 04:10:26 +10002076 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10002077 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10002078}
2079
2080static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10002081nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10002082{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002083 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10002084 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10002085 struct {
2086 struct nv50_disp_mthd_v1 base;
2087 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
2088 } args = {
2089 .base.version = 1,
2090 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
2091 .base.hasht = nv_encoder->dcb->hasht,
2092 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
2093 (0x0100 << nv_crtc->index),
2094 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10002095
Ben Skeggse00f2232014-08-10 04:10:26 +10002096 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10002097}
2098
2099/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002100 * MST
2101 *****************************************************************************/
2102struct nv50_mstm {
2103 struct nouveau_encoder *outp;
2104
2105 struct drm_dp_mst_topology_mgr mgr;
2106};
2107
2108static int
2109nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
2110{
2111 struct nouveau_encoder *outp = mstm->outp;
2112 struct {
2113 struct nv50_disp_mthd_v1 base;
2114 struct nv50_disp_sor_dp_mst_link_v0 mst;
2115 } args = {
2116 .base.version = 1,
2117 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
2118 .base.hasht = outp->dcb->hasht,
2119 .base.hashm = outp->dcb->hashm,
2120 .mst.state = state,
2121 };
2122 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
2123 struct nvif_object *disp = &drm->display->disp;
2124 int ret;
2125
2126 if (dpcd >= 0x12) {
2127 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
2128 if (ret < 0)
2129 return ret;
2130
2131 dpcd &= ~DP_MST_EN;
2132 if (state)
2133 dpcd |= DP_MST_EN;
2134
2135 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
2136 if (ret < 0)
2137 return ret;
2138 }
2139
2140 return nvif_mthd(disp, 0, &args, sizeof(args));
2141}
2142
2143int
2144nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
2145{
2146 int ret, state = 0;
2147
2148 if (!mstm)
2149 return 0;
2150
2151 if (dpcd[0] >= 0x12 && allow) {
2152 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
2153 if (ret < 0)
2154 return ret;
2155
2156 state = dpcd[1] & DP_MST_CAP;
2157 }
2158
2159 ret = nv50_mstm_enable(mstm, dpcd[0], state);
2160 if (ret)
2161 return ret;
2162
2163 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
2164 if (ret)
2165 return nv50_mstm_enable(mstm, dpcd[0], 0);
2166
2167 return mstm->mgr.mst_state;
2168}
2169
2170static void
2171nv50_mstm_del(struct nv50_mstm **pmstm)
2172{
2173 struct nv50_mstm *mstm = *pmstm;
2174 if (mstm) {
2175 kfree(*pmstm);
2176 *pmstm = NULL;
2177 }
2178}
2179
2180static int
2181nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
2182 int conn_base_id, struct nv50_mstm **pmstm)
2183{
2184 const int max_payloads = hweight8(outp->dcb->heads);
2185 struct drm_device *dev = outp->base.base.dev;
2186 struct nv50_mstm *mstm;
2187 int ret;
2188
2189 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
2190 return -ENOMEM;
2191 mstm->outp = outp;
2192
2193 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev->dev, aux, aux_max,
2194 max_payloads, conn_base_id);
2195 if (ret)
2196 return ret;
2197
2198 return 0;
2199}
2200
2201/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002202 * SOR
2203 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002204static void
Ben Skeggse225f442012-11-21 14:40:21 +10002205nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002206{
2207 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10002208 struct nv50_disp *disp = nv50_disp(encoder->dev);
2209 struct {
2210 struct nv50_disp_mthd_v1 base;
2211 struct nv50_disp_sor_pwr_v0 pwr;
2212 } args = {
2213 .base.version = 1,
2214 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
2215 .base.hasht = nv_encoder->dcb->hasht,
2216 .base.hashm = nv_encoder->dcb->hashm,
2217 .pwr.state = mode == DRM_MODE_DPMS_ON,
2218 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10002219 struct {
2220 struct nv50_disp_mthd_v1 base;
2221 struct nv50_disp_sor_dp_pwr_v0 pwr;
2222 } link = {
2223 .base.version = 1,
2224 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
2225 .base.hasht = nv_encoder->dcb->hasht,
2226 .base.hashm = nv_encoder->dcb->hashm,
2227 .pwr.state = mode == DRM_MODE_DPMS_ON,
2228 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10002229 struct drm_device *dev = encoder->dev;
2230 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002231
2232 nv_encoder->last_dpms = mode;
2233
2234 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
2235 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
2236
2237 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
2238 continue;
2239
2240 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10002241 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10002242 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
2243 return;
2244 break;
2245 }
2246 }
2247
Ben Skeggs48743222014-05-31 01:48:06 +10002248 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10002249 args.pwr.state = 1;
2250 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10002251 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10002252 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10002253 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10002254 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002255}
2256
Ben Skeggs83fc0832011-07-05 13:08:40 +10002257static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10002258nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
2259{
2260 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
2261 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
2262 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002263 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10002264 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
2265 evo_data(push, (nv_encoder->ctrl = temp));
2266 } else {
2267 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
2268 evo_data(push, (nv_encoder->ctrl = temp));
2269 }
2270 evo_kick(push, mast);
2271 }
2272}
2273
2274static void
Ben Skeggse225f442012-11-21 14:40:21 +10002275nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10002276{
2277 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002278 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002279
2280 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2281 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10002282
2283 if (nv_crtc) {
2284 nv50_crtc_prepare(&nv_crtc->base);
2285 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggscc2a9072014-09-15 21:29:05 +10002286 nv50_audio_disconnect(encoder, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002287 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
2288 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10002289}
2290
2291static void
Ben Skeggse225f442012-11-21 14:40:21 +10002292nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002293{
2294}
2295
2296static void
Ben Skeggse225f442012-11-21 14:40:21 +10002297nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002298 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002299{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002300 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2301 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2302 struct {
2303 struct nv50_disp_mthd_v1 base;
2304 struct nv50_disp_sor_lvds_script_v0 lvds;
2305 } lvds = {
2306 .base.version = 1,
2307 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
2308 .base.hasht = nv_encoder->dcb->hasht,
2309 .base.hashm = nv_encoder->dcb->hashm,
2310 };
Ben Skeggse225f442012-11-21 14:40:21 +10002311 struct nv50_disp *disp = nv50_disp(encoder->dev);
2312 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10002313 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10002314 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002315 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10002316 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002317 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002318 u8 owner = 1 << nv_crtc->index;
2319 u8 proto = 0xf;
2320 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002321
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002322 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002323 nv_encoder->crtc = encoder->crtc;
2324
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002325 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10002326 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002327 if (nv_encoder->dcb->sorconf.link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05002328 proto = 0x1;
2329 /* Only enable dual-link if:
2330 * - Need to (i.e. rate > 165MHz)
2331 * - DCB says we can
2332 * - Not an HDMI monitor, since there's no dual-link
2333 * on HDMI.
2334 */
2335 if (mode->clock >= 165000 &&
2336 nv_encoder->dcb->duallink_possible &&
2337 !drm_detect_hdmi_monitor(nv_connector->edid))
2338 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002339 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002340 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002341 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002342
Ben Skeggse84a35a2014-06-05 10:59:55 +10002343 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002344 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10002345 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002346 proto = 0x0;
2347
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002348 if (bios->fp_no_ddc) {
2349 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002350 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002351 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002352 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002353 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10002354 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002355 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002356 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002357 } else
2358 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002359 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002360 }
2361
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002362 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002363 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002364 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002365 } else {
2366 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002367 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002368 }
2369
2370 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002371 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002372 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10002373
Ben Skeggsa3761fa2014-08-10 04:10:27 +10002374 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002375 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10002376 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10002377 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002378 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002379 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10002380 } else
2381 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002382 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002383 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10002384 } else {
2385 nv_encoder->dp.datarate = mode->clock * 30 / 8;
2386 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10002387 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002388
2389 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002390 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002391 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002392 proto = 0x9;
Ben Skeggs3eee8642014-09-15 15:20:47 +10002393 nv50_audio_mode_set(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10002394 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10002395 default:
2396 BUG_ON(1);
2397 break;
2398 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10002399
Ben Skeggse84a35a2014-06-05 10:59:55 +10002400 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002401
Ben Skeggs648d4df2014-08-10 04:10:27 +10002402 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10002403 u32 *push = evo_wait(mast, 3);
2404 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002405 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2406 u32 syncs = 0x00000001;
2407
2408 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2409 syncs |= 0x00000008;
2410 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2411 syncs |= 0x00000010;
2412
2413 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2414 magic |= 0x00000001;
2415
2416 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2417 evo_data(push, syncs | (depth << 6));
2418 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002419 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002420 }
2421
Ben Skeggse84a35a2014-06-05 10:59:55 +10002422 ctrl = proto << 8;
2423 mask = 0x00000f00;
2424 } else {
2425 ctrl = (depth << 16) | (proto << 8);
2426 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2427 ctrl |= 0x00001000;
2428 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2429 ctrl |= 0x00002000;
2430 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002431 }
2432
Ben Skeggse84a35a2014-06-05 10:59:55 +10002433 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002434}
2435
2436static void
Ben Skeggse225f442012-11-21 14:40:21 +10002437nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002438{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002439 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2440 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002441 drm_encoder_cleanup(encoder);
2442 kfree(encoder);
2443}
2444
Ben Skeggse225f442012-11-21 14:40:21 +10002445static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2446 .dpms = nv50_sor_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10002447 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10002448 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10002449 .commit = nv50_sor_commit,
2450 .mode_set = nv50_sor_mode_set,
2451 .disable = nv50_sor_disconnect,
2452 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002453};
2454
Ben Skeggse225f442012-11-21 14:40:21 +10002455static const struct drm_encoder_funcs nv50_sor_func = {
2456 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002457};
2458
2459static int
Ben Skeggse225f442012-11-21 14:40:21 +10002460nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002461{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002462 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10002463 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002464 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002465 struct nouveau_encoder *nv_encoder;
2466 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002467 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002468
2469 switch (dcbe->type) {
2470 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2471 case DCB_OUTPUT_TMDS:
2472 case DCB_OUTPUT_DP:
2473 default:
2474 type = DRM_MODE_ENCODER_TMDS;
2475 break;
2476 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002477
2478 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2479 if (!nv_encoder)
2480 return -ENOMEM;
2481 nv_encoder->dcb = dcbe;
2482 nv_encoder->or = ffs(dcbe->or) - 1;
2483 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2484
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002485 encoder = to_drm_encoder(nv_encoder);
2486 encoder->possible_crtcs = dcbe->heads;
2487 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002488 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
2489 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002490 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
2491
2492 drm_mode_connector_attach_encoder(connector, encoder);
2493
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002494 if (dcbe->type == DCB_OUTPUT_DP) {
2495 struct nvkm_i2c_aux *aux =
2496 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
2497 if (aux) {
2498 nv_encoder->i2c = &aux->i2c;
2499 nv_encoder->aux = aux;
2500 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10002501
2502 /*TODO: Use DP Info Table to check for support. */
2503 if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
2504 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
2505 nv_connector->base.base.id,
2506 &nv_encoder->dp.mstm);
2507 if (ret)
2508 return ret;
2509 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002510 } else {
2511 struct nvkm_i2c_bus *bus =
2512 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2513 if (bus)
2514 nv_encoder->i2c = &bus->i2c;
2515 }
2516
Ben Skeggs83fc0832011-07-05 13:08:40 +10002517 return 0;
2518}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002519
2520/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002521 * PIOR
2522 *****************************************************************************/
2523
2524static void
2525nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2526{
2527 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2528 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10002529 struct {
2530 struct nv50_disp_mthd_v1 base;
2531 struct nv50_disp_pior_pwr_v0 pwr;
2532 } args = {
2533 .base.version = 1,
2534 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2535 .base.hasht = nv_encoder->dcb->hasht,
2536 .base.hashm = nv_encoder->dcb->hashm,
2537 .pwr.state = mode == DRM_MODE_DPMS_ON,
2538 .pwr.type = nv_encoder->dcb->type,
2539 };
2540
2541 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002542}
2543
2544static bool
2545nv50_pior_mode_fixup(struct drm_encoder *encoder,
2546 const struct drm_display_mode *mode,
2547 struct drm_display_mode *adjusted_mode)
2548{
Ben Skeggsa91d3222014-12-22 16:30:13 +10002549 if (!nv50_encoder_mode_fixup(encoder, mode, adjusted_mode))
2550 return false;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002551 adjusted_mode->clock *= 2;
2552 return true;
2553}
2554
2555static void
2556nv50_pior_commit(struct drm_encoder *encoder)
2557{
2558}
2559
2560static void
2561nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2562 struct drm_display_mode *adjusted_mode)
2563{
2564 struct nv50_mast *mast = nv50_mast(encoder->dev);
2565 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2566 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2567 struct nouveau_connector *nv_connector;
2568 u8 owner = 1 << nv_crtc->index;
2569 u8 proto, depth;
2570 u32 *push;
2571
2572 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2573 switch (nv_connector->base.display_info.bpc) {
2574 case 10: depth = 0x6; break;
2575 case 8: depth = 0x5; break;
2576 case 6: depth = 0x2; break;
2577 default: depth = 0x0; break;
2578 }
2579
2580 switch (nv_encoder->dcb->type) {
2581 case DCB_OUTPUT_TMDS:
2582 case DCB_OUTPUT_DP:
2583 proto = 0x0;
2584 break;
2585 default:
2586 BUG_ON(1);
2587 break;
2588 }
2589
2590 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2591
2592 push = evo_wait(mast, 8);
2593 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002594 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002595 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2596 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2597 ctrl |= 0x00001000;
2598 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2599 ctrl |= 0x00002000;
2600 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2601 evo_data(push, ctrl);
2602 }
2603
2604 evo_kick(push, mast);
2605 }
2606
2607 nv_encoder->crtc = encoder->crtc;
2608}
2609
2610static void
2611nv50_pior_disconnect(struct drm_encoder *encoder)
2612{
2613 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2614 struct nv50_mast *mast = nv50_mast(encoder->dev);
2615 const int or = nv_encoder->or;
2616 u32 *push;
2617
2618 if (nv_encoder->crtc) {
2619 nv50_crtc_prepare(nv_encoder->crtc);
2620
2621 push = evo_wait(mast, 4);
2622 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002623 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002624 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2625 evo_data(push, 0x00000000);
2626 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002627 evo_kick(push, mast);
2628 }
2629 }
2630
2631 nv_encoder->crtc = NULL;
2632}
2633
2634static void
2635nv50_pior_destroy(struct drm_encoder *encoder)
2636{
2637 drm_encoder_cleanup(encoder);
2638 kfree(encoder);
2639}
2640
2641static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2642 .dpms = nv50_pior_dpms,
2643 .mode_fixup = nv50_pior_mode_fixup,
2644 .prepare = nv50_pior_disconnect,
2645 .commit = nv50_pior_commit,
2646 .mode_set = nv50_pior_mode_set,
2647 .disable = nv50_pior_disconnect,
2648 .get_crtc = nv50_display_crtc_get,
2649};
2650
2651static const struct drm_encoder_funcs nv50_pior_func = {
2652 .destroy = nv50_pior_destroy,
2653};
2654
2655static int
2656nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2657{
2658 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggsbe83cd42015-01-14 15:36:34 +10002659 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002660 struct nvkm_i2c_bus *bus = NULL;
2661 struct nvkm_i2c_aux *aux = NULL;
2662 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002663 struct nouveau_encoder *nv_encoder;
2664 struct drm_encoder *encoder;
2665 int type;
2666
2667 switch (dcbe->type) {
2668 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002669 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
2670 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002671 type = DRM_MODE_ENCODER_TMDS;
2672 break;
2673 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002674 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
2675 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002676 type = DRM_MODE_ENCODER_TMDS;
2677 break;
2678 default:
2679 return -ENODEV;
2680 }
2681
2682 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2683 if (!nv_encoder)
2684 return -ENOMEM;
2685 nv_encoder->dcb = dcbe;
2686 nv_encoder->or = ffs(dcbe->or) - 1;
2687 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10002688 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002689
2690 encoder = to_drm_encoder(nv_encoder);
2691 encoder->possible_crtcs = dcbe->heads;
2692 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10002693 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
2694 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002695 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2696
2697 drm_mode_connector_attach_encoder(connector, encoder);
2698 return 0;
2699}
2700
2701/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002702 * Framebuffer
2703 *****************************************************************************/
2704
Ben Skeggs8a423642014-08-10 04:10:19 +10002705static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002706nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002707{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002708 int i;
2709 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2710 nvif_object_fini(&fbdma->base[i]);
2711 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002712 list_del(&fbdma->head);
2713 kfree(fbdma);
2714}
2715
2716static int
2717nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2718{
2719 struct nouveau_drm *drm = nouveau_drm(dev);
2720 struct nv50_disp *disp = nv50_disp(dev);
2721 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002722 struct __attribute__ ((packed)) {
2723 struct nv_dma_v0 base;
2724 union {
2725 struct nv50_dma_v0 nv50;
2726 struct gf100_dma_v0 gf100;
Ben Skeggsbd70563f2015-08-20 14:54:21 +10002727 struct gf119_dma_v0 gf119;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002728 };
2729 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002730 struct nv50_fbdma *fbdma;
2731 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002732 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002733 int ret;
2734
2735 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002736 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002737 return 0;
2738 }
2739
2740 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2741 if (!fbdma)
2742 return -ENOMEM;
2743 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002744
Ben Skeggs4acfd702014-08-10 04:10:24 +10002745 args.base.target = NV_DMA_V0_TARGET_VRAM;
2746 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2747 args.base.start = offset;
2748 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002749
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002750 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002751 args.nv50.part = NV50_DMA_V0_PART_256;
2752 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002753 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002754 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002755 args.nv50.part = NV50_DMA_V0_PART_256;
2756 args.nv50.kind = kind;
2757 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002758 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002759 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002760 args.gf100.kind = kind;
2761 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002762 } else {
Ben Skeggsbd70563f2015-08-20 14:54:21 +10002763 args.gf119.page = GF119_DMA_V0_PAGE_LP;
2764 args.gf119.kind = kind;
2765 size += sizeof(args.gf119);
Ben Skeggs8a423642014-08-10 04:10:19 +10002766 }
2767
2768 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002769 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsa01ca782015-08-20 14:54:15 +10002770 int ret = nvif_object_init(&head->sync.base.base.user, name,
2771 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002772 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002773 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002774 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002775 return ret;
2776 }
2777 }
2778
Ben Skeggsa01ca782015-08-20 14:54:15 +10002779 ret = nvif_object_init(&mast->base.base.user, name, NV_DMA_IN_MEMORY,
2780 &args, size, &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002781 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002782 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002783 return ret;
2784 }
2785
2786 return 0;
2787}
2788
Ben Skeggsab0af552014-08-10 04:10:19 +10002789static void
2790nv50_fb_dtor(struct drm_framebuffer *fb)
2791{
2792}
2793
2794static int
2795nv50_fb_ctor(struct drm_framebuffer *fb)
2796{
2797 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2798 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2799 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002800 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002801 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2802 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002803
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002804 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002805 tile >>= 4; /* yep.. */
2806
Ben Skeggsab0af552014-08-10 04:10:19 +10002807 switch (fb->depth) {
2808 case 8: nv_fb->r_format = 0x1e00; break;
2809 case 15: nv_fb->r_format = 0xe900; break;
2810 case 16: nv_fb->r_format = 0xe800; break;
2811 case 24:
2812 case 32: nv_fb->r_format = 0xcf00; break;
2813 case 30: nv_fb->r_format = 0xd100; break;
2814 default:
2815 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2816 return -EINVAL;
2817 }
2818
Ben Skeggs648d4df2014-08-10 04:10:27 +10002819 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002820 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2821 (fb->pitches[0] | 0x00100000);
2822 nv_fb->r_format |= kind << 16;
2823 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10002824 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002825 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2826 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002827 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002828 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2829 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002830 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002831 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002832
Ben Skeggsf392ec42014-08-10 04:10:28 +10002833 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
2834 drm->device.info.ram_user, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002835}
2836
2837/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002838 * Init
2839 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002840
Ben Skeggs2a44e492011-11-09 11:36:33 +10002841void
Ben Skeggse225f442012-11-21 14:40:21 +10002842nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002843{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002844}
2845
2846int
Ben Skeggse225f442012-11-21 14:40:21 +10002847nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002848{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002849 struct nv50_disp *disp = nv50_disp(dev);
2850 struct drm_crtc *crtc;
2851 u32 *push;
2852
2853 push = evo_wait(nv50_mast(dev), 32);
2854 if (!push)
2855 return -EBUSY;
2856
2857 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2858 struct nv50_sync *sync = nv50_sync(crtc);
Maarten Lankhorst4dc63932015-01-13 09:18:49 +01002859
2860 nv50_crtc_lut_load(crtc);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002861 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002862 }
2863
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002864 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10002865 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002866 evo_kick(push, nv50_mast(dev));
2867 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002868}
2869
2870void
Ben Skeggse225f442012-11-21 14:40:21 +10002871nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002872{
Ben Skeggse225f442012-11-21 14:40:21 +10002873 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002874 struct nv50_fbdma *fbdma, *fbtmp;
2875
2876 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002877 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002878 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002879
Ben Skeggs0ad72862014-08-10 04:10:22 +10002880 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002881
Ben Skeggs816af2f2011-11-16 15:48:48 +10002882 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002883 if (disp->sync)
2884 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002885 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002886
Ben Skeggs77145f12012-07-31 16:16:21 +10002887 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002888 kfree(disp);
2889}
2890
2891int
Ben Skeggse225f442012-11-21 14:40:21 +10002892nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002893{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002894 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002895 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002896 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002897 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002898 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002899 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002900 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002901
2902 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2903 if (!disp)
2904 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10002905 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10002906
2907 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002908 nouveau_display(dev)->dtor = nv50_display_destroy;
2909 nouveau_display(dev)->init = nv50_display_init;
2910 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10002911 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2912 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002913 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002914
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002915 /* small shared memory area we use for notifiers and semaphores */
2916 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002917 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002918 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002919 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002920 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002921 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002922 if (ret)
2923 nouveau_bo_unpin(disp->sync);
2924 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002925 if (ret)
2926 nouveau_bo_ref(NULL, &disp->sync);
2927 }
2928
2929 if (ret)
2930 goto out;
2931
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002932 /* allocate master evo channel */
Ben Skeggsa01ca782015-08-20 14:54:15 +10002933 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
Ben Skeggs410f3ec2014-08-10 04:10:25 +10002934 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002935 if (ret)
2936 goto out;
2937
Ben Skeggs438d99e2011-07-05 16:48:06 +10002938 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10002939 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsa01ca782015-08-20 14:54:15 +10002940 crtcs = nvif_rd32(&device->object, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10002941 else
2942 crtcs = 2;
2943
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002944 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002945 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002946 if (ret)
2947 goto out;
2948 }
2949
Ben Skeggs83fc0832011-07-05 13:08:40 +10002950 /* create encoder/connector objects based on VBIOS DCB table */
2951 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2952 connector = nouveau_connector_create(dev, dcbe->connector);
2953 if (IS_ERR(connector))
2954 continue;
2955
Ben Skeggseb6313a2013-02-11 09:52:58 +10002956 if (dcbe->location == DCB_LOC_ON_CHIP) {
2957 switch (dcbe->type) {
2958 case DCB_OUTPUT_TMDS:
2959 case DCB_OUTPUT_LVDS:
2960 case DCB_OUTPUT_DP:
2961 ret = nv50_sor_create(connector, dcbe);
2962 break;
2963 case DCB_OUTPUT_ANALOG:
2964 ret = nv50_dac_create(connector, dcbe);
2965 break;
2966 default:
2967 ret = -ENODEV;
2968 break;
2969 }
2970 } else {
2971 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002972 }
2973
Ben Skeggseb6313a2013-02-11 09:52:58 +10002974 if (ret) {
2975 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2976 dcbe->location, dcbe->type,
2977 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002978 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002979 }
2980 }
2981
2982 /* cull any connectors we created that don't have an encoder */
2983 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2984 if (connector->encoder_ids[0])
2985 continue;
2986
Ben Skeggs77145f12012-07-31 16:16:21 +10002987 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002988 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002989 connector->funcs->destroy(connector);
2990 }
2991
Ben Skeggs26f6d882011-07-04 16:25:18 +10002992out:
2993 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002994 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002995 return ret;
2996}