blob: c15060e7336c5ee6eca37efb43ee8cd26274eabd [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>
28#include <drm/drm_crtc_helper.h>
Ben Skeggs48743222014-05-31 01:48:06 +100029#include <drm/drm_dp_helper.h>
Ben Skeggs26f6d882011-07-04 16:25:18 +100030
Ben Skeggsfdb751e2014-08-10 04:10:23 +100031#include <nvif/class.h>
32
Ben Skeggs77145f12012-07-31 16:16:21 +100033#include "nouveau_drm.h"
34#include "nouveau_dma.h"
35#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100036#include "nouveau_connector.h"
37#include "nouveau_encoder.h"
38#include "nouveau_crtc.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100039#include "nouveau_fence.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100040#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100041
Ben Skeggs8a464382011-11-12 23:52:07 +100042#define EVO_DMA_NR 9
43
Ben Skeggsbdb8c212011-11-12 01:30:24 +100044#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100045#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100046#define EVO_OVLY(c) (0x05 + (c))
47#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100048#define EVO_CURS(c) (0x0d + (c))
49
Ben Skeggs816af2f2011-11-16 15:48:48 +100050/* offsets in shared sync bo of various structures */
51#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100052#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
53#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
54#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs816af2f2011-11-16 15:48:48 +100055
Ben Skeggsb5a794b2012-10-16 14:18:32 +100056/******************************************************************************
57 * EVO channel
58 *****************************************************************************/
59
Ben Skeggse225f442012-11-21 14:40:21 +100060struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +100061 struct nvif_object user;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100062};
63
64static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +100065nv50_chan_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +100066 void *data, u32 size, struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100067{
Ben Skeggs410f3ec2014-08-10 04:10:25 +100068 while (oclass[0]) {
69 int ret = nvif_object_init(disp, NULL, (oclass[0] << 16) | head,
70 oclass[0], data, size,
71 &chan->user);
Ben Skeggsb76f1522014-08-10 04:10:28 +100072 if (oclass++, ret == 0) {
73 nvif_object_map(&chan->user);
Ben Skeggs410f3ec2014-08-10 04:10:25 +100074 return ret;
Ben Skeggsb76f1522014-08-10 04:10:28 +100075 }
Ben Skeggs410f3ec2014-08-10 04:10:25 +100076 }
77 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100078}
79
80static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100081nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100082{
Ben Skeggs0ad72862014-08-10 04:10:22 +100083 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100084}
85
86/******************************************************************************
87 * PIO EVO channel
88 *****************************************************************************/
89
Ben Skeggse225f442012-11-21 14:40:21 +100090struct nv50_pioc {
91 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100092};
93
94static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100095nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100096{
Ben Skeggs0ad72862014-08-10 04:10:22 +100097 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100098}
99
100static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000101nv50_pioc_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +1000102 void *data, u32 size, struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000103{
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000104 return nv50_chan_create(disp, oclass, head, data, size, &pioc->base);
105}
106
107/******************************************************************************
108 * Cursor Immediate
109 *****************************************************************************/
110
111struct nv50_curs {
112 struct nv50_pioc base;
113};
114
115static int
116nv50_curs_create(struct nvif_object *disp, int head, struct nv50_curs *curs)
117{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000118 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000119 .head = head,
120 };
121 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000122 GK104_DISP_CURSOR,
123 GF110_DISP_CURSOR,
124 GT214_DISP_CURSOR,
125 G82_DISP_CURSOR,
126 NV50_DISP_CURSOR,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000127 0
128 };
129
130 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
131 &curs->base);
132}
133
134/******************************************************************************
135 * Overlay Immediate
136 *****************************************************************************/
137
138struct nv50_oimm {
139 struct nv50_pioc base;
140};
141
142static int
143nv50_oimm_create(struct nvif_object *disp, int head, struct nv50_oimm *oimm)
144{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000145 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000146 .head = head,
147 };
148 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000149 GK104_DISP_OVERLAY,
150 GF110_DISP_OVERLAY,
151 GT214_DISP_OVERLAY,
152 G82_DISP_OVERLAY,
153 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000154 0
155 };
156
157 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
158 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000159}
160
161/******************************************************************************
162 * DMA EVO channel
163 *****************************************************************************/
164
Ben Skeggse225f442012-11-21 14:40:21 +1000165struct nv50_dmac {
166 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000167 dma_addr_t handle;
168 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100169
Ben Skeggs0ad72862014-08-10 04:10:22 +1000170 struct nvif_object sync;
171 struct nvif_object vram;
172
Daniel Vetter59ad1462012-12-02 14:49:44 +0100173 /* Protects against concurrent pushbuf access to this channel, lock is
174 * grabbed by evo_wait (if the pushbuf reservation is successful) and
175 * dropped again by evo_kick. */
176 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000177};
178
179static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000180nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000181{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000182 nvif_object_fini(&dmac->vram);
183 nvif_object_fini(&dmac->sync);
184
185 nv50_chan_destroy(&dmac->base);
186
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000187 if (dmac->ptr) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000188 struct pci_dev *pdev = nvkm_device(nvif_device(disp))->pdev;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000189 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
190 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000191}
192
193static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000194nv50_dmac_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000195 void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000196 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000197{
Ben Skeggsf392ec42014-08-10 04:10:28 +1000198 struct nvif_device *device = nvif_device(disp);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000199 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000200 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000201 int ret;
202
Daniel Vetter59ad1462012-12-02 14:49:44 +0100203 mutex_init(&dmac->lock);
204
Ben Skeggsf392ec42014-08-10 04:10:28 +1000205 dmac->ptr = pci_alloc_consistent(nvkm_device(device)->pdev,
Ben Skeggs0ad72862014-08-10 04:10:22 +1000206 PAGE_SIZE, &dmac->handle);
Ben Skeggs47057302012-11-16 13:58:48 +1000207 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000208 return -ENOMEM;
209
Ben Skeggsf392ec42014-08-10 04:10:28 +1000210 ret = nvif_object_init(nvif_object(device), NULL,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000211 args->pushbuf, NV_DMA_FROM_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000212 &(struct nv_dma_v0) {
213 .target = NV_DMA_V0_TARGET_PCI_US,
214 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000215 .start = dmac->handle + 0x0000,
216 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000217 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000218 if (ret)
219 return ret;
220
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000221 ret = nv50_chan_create(disp, oclass, head, data, size, &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000222 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000223 if (ret)
224 return ret;
225
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000226 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000000,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000227 NV_DMA_IN_MEMORY,
228 &(struct nv_dma_v0) {
229 .target = NV_DMA_V0_TARGET_VRAM,
230 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000231 .start = syncbuf + 0x0000,
232 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000233 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000234 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000235 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000236 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000237
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000238 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000001,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000239 NV_DMA_IN_MEMORY,
240 &(struct nv_dma_v0) {
241 .target = NV_DMA_V0_TARGET_VRAM,
242 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000243 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000244 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000245 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000246 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000247 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000248 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000249
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000250 return ret;
251}
252
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000253/******************************************************************************
254 * Core
255 *****************************************************************************/
256
Ben Skeggse225f442012-11-21 14:40:21 +1000257struct nv50_mast {
258 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000259};
260
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000261static int
262nv50_core_create(struct nvif_object *disp, u64 syncbuf, struct nv50_mast *core)
263{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000264 struct nv50_disp_core_channel_dma_v0 args = {
265 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000266 };
267 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000268 GM107_DISP_CORE_CHANNEL_DMA,
269 GK110_DISP_CORE_CHANNEL_DMA,
270 GK104_DISP_CORE_CHANNEL_DMA,
271 GF110_DISP_CORE_CHANNEL_DMA,
272 GT214_DISP_CORE_CHANNEL_DMA,
273 GT206_DISP_CORE_CHANNEL_DMA,
274 GT200_DISP_CORE_CHANNEL_DMA,
275 G82_DISP_CORE_CHANNEL_DMA,
276 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000277 0
278 };
279
280 return nv50_dmac_create(disp, oclass, 0, &args, sizeof(args), syncbuf,
281 &core->base);
282}
283
284/******************************************************************************
285 * Base
286 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000287
Ben Skeggse225f442012-11-21 14:40:21 +1000288struct nv50_sync {
289 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000290 u32 addr;
291 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000292};
293
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000294static int
295nv50_base_create(struct nvif_object *disp, int head, u64 syncbuf,
296 struct nv50_sync *base)
297{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000298 struct nv50_disp_base_channel_dma_v0 args = {
299 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000300 .head = head,
301 };
302 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000303 GK110_DISP_BASE_CHANNEL_DMA,
304 GK104_DISP_BASE_CHANNEL_DMA,
305 GF110_DISP_BASE_CHANNEL_DMA,
306 GT214_DISP_BASE_CHANNEL_DMA,
307 GT200_DISP_BASE_CHANNEL_DMA,
308 G82_DISP_BASE_CHANNEL_DMA,
309 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000310 0
311 };
312
313 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
314 syncbuf, &base->base);
315}
316
317/******************************************************************************
318 * Overlay
319 *****************************************************************************/
320
Ben Skeggse225f442012-11-21 14:40:21 +1000321struct nv50_ovly {
322 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000323};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000324
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000325static int
326nv50_ovly_create(struct nvif_object *disp, int head, u64 syncbuf,
327 struct nv50_ovly *ovly)
328{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000329 struct nv50_disp_overlay_channel_dma_v0 args = {
330 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000331 .head = head,
332 };
333 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000334 GK104_DISP_OVERLAY_CONTROL_DMA,
335 GF110_DISP_OVERLAY_CONTROL_DMA,
336 GT214_DISP_OVERLAY_CHANNEL_DMA,
337 GT200_DISP_OVERLAY_CHANNEL_DMA,
338 G82_DISP_OVERLAY_CHANNEL_DMA,
339 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000340 0
341 };
342
343 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
344 syncbuf, &ovly->base);
345}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000346
Ben Skeggse225f442012-11-21 14:40:21 +1000347struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000348 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000349 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000350 struct nv50_curs curs;
351 struct nv50_sync sync;
352 struct nv50_ovly ovly;
353 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000354};
355
Ben Skeggse225f442012-11-21 14:40:21 +1000356#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
357#define nv50_curs(c) (&nv50_head(c)->curs)
358#define nv50_sync(c) (&nv50_head(c)->sync)
359#define nv50_ovly(c) (&nv50_head(c)->ovly)
360#define nv50_oimm(c) (&nv50_head(c)->oimm)
361#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000362#define nv50_vers(c) nv50_chan(c)->user.oclass
363
364struct nv50_fbdma {
365 struct list_head head;
366 struct nvif_object core;
367 struct nvif_object base[4];
368};
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000369
Ben Skeggse225f442012-11-21 14:40:21 +1000370struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000371 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000372 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000373
Ben Skeggs8a423642014-08-10 04:10:19 +1000374 struct list_head fbdma;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000375
376 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000377};
378
Ben Skeggse225f442012-11-21 14:40:21 +1000379static struct nv50_disp *
380nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000381{
Ben Skeggs77145f12012-07-31 16:16:21 +1000382 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000383}
384
Ben Skeggse225f442012-11-21 14:40:21 +1000385#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000386
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000387static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000388nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000389{
390 return nouveau_encoder(encoder)->crtc;
391}
392
393/******************************************************************************
394 * EVO channel helpers
395 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000396static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000397evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000398{
Ben Skeggse225f442012-11-21 14:40:21 +1000399 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000400 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000401
Daniel Vetter59ad1462012-12-02 14:49:44 +0100402 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000403 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000404 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000405
Ben Skeggs0ad72862014-08-10 04:10:22 +1000406 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
407 if (!nvkm_wait(&dmac->base.user, 0x0004, ~0, 0x00000000)) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100408 mutex_unlock(&dmac->lock);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000409 nv_error(nvkm_object(&dmac->base.user), "channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000410 return NULL;
411 }
412
413 put = 0;
414 }
415
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000416 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000417}
418
419static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000420evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000421{
Ben Skeggse225f442012-11-21 14:40:21 +1000422 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000423 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100424 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000425}
426
427#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
428#define evo_data(p,d) *((p)++) = (d)
429
Ben Skeggs3376ee32011-11-12 14:28:12 +1000430static bool
431evo_sync_wait(void *data)
432{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500433 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
434 return true;
435 usleep_range(1, 2);
436 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000437}
438
439static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000440evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000441{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000442 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000443 struct nv50_disp *disp = nv50_disp(dev);
444 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000445 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000446 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000447 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000448 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000449 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000450 evo_mthd(push, 0x0080, 2);
451 evo_data(push, 0x00000000);
452 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000453 evo_kick(push, mast);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000454 if (nv_wait_cb(nvkm_device(device), evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000455 return 0;
456 }
457
458 return -EBUSY;
459}
460
461/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000462 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000463 *****************************************************************************/
464struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000465nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000466{
Ben Skeggse225f442012-11-21 14:40:21 +1000467 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000468}
469
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000470struct nv50_display_flip {
471 struct nv50_disp *disp;
472 struct nv50_sync *chan;
473};
474
475static bool
476nv50_display_flip_wait(void *data)
477{
478 struct nv50_display_flip *flip = data;
479 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500480 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000481 return true;
482 usleep_range(1, 2);
483 return false;
484}
485
Ben Skeggs3376ee32011-11-12 14:28:12 +1000486void
Ben Skeggse225f442012-11-21 14:40:21 +1000487nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000488{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000489 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000490 struct nv50_display_flip flip = {
491 .disp = nv50_disp(crtc->dev),
492 .chan = nv50_sync(crtc),
493 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000494 u32 *push;
495
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000496 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000497 if (push) {
498 evo_mthd(push, 0x0084, 1);
499 evo_data(push, 0x00000000);
500 evo_mthd(push, 0x0094, 1);
501 evo_data(push, 0x00000000);
502 evo_mthd(push, 0x00c0, 1);
503 evo_data(push, 0x00000000);
504 evo_mthd(push, 0x0080, 1);
505 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000506 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000507 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000508
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000509 nv_wait_cb(nvkm_device(device), nv50_display_flip_wait, &flip);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000510}
511
512int
Ben Skeggse225f442012-11-21 14:40:21 +1000513nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000514 struct nouveau_channel *chan, u32 swap_interval)
515{
516 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000517 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000518 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000519 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000520 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000521 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000522
523 swap_interval <<= 4;
524 if (swap_interval == 0)
525 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000526 if (chan == NULL)
527 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000528
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000529 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000530 if (unlikely(push == NULL))
531 return -EBUSY;
532
Ben Skeggsbbf89062014-08-10 04:10:25 +1000533 if (chan && chan->object->oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000534 ret = RING_SPACE(chan, 8);
535 if (ret)
536 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000537
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000538 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000539 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000540 OUT_RING (chan, sync->addr ^ 0x10);
541 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
542 OUT_RING (chan, sync->data + 1);
543 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
544 OUT_RING (chan, sync->addr);
545 OUT_RING (chan, sync->data);
546 } else
Ben Skeggsbbf89062014-08-10 04:10:25 +1000547 if (chan && chan->object->oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000548 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000549 ret = RING_SPACE(chan, 12);
550 if (ret)
551 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000552
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000553 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000554 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000555 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
556 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
557 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
558 OUT_RING (chan, sync->data + 1);
559 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
560 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
561 OUT_RING (chan, upper_32_bits(addr));
562 OUT_RING (chan, lower_32_bits(addr));
563 OUT_RING (chan, sync->data);
564 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
565 } else
566 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000567 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000568 ret = RING_SPACE(chan, 10);
569 if (ret)
570 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000571
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000572 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
573 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
574 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
575 OUT_RING (chan, sync->data + 1);
576 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
577 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
578 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
579 OUT_RING (chan, upper_32_bits(addr));
580 OUT_RING (chan, lower_32_bits(addr));
581 OUT_RING (chan, sync->data);
582 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
583 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
584 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500585
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000586 if (chan) {
587 sync->addr ^= 0x10;
588 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000589 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000590 }
591
592 /* queue the flip */
593 evo_mthd(push, 0x0100, 1);
594 evo_data(push, 0xfffe0000);
595 evo_mthd(push, 0x0084, 1);
596 evo_data(push, swap_interval);
597 if (!(swap_interval & 0x00000100)) {
598 evo_mthd(push, 0x00e0, 1);
599 evo_data(push, 0x40000000);
600 }
601 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000602 evo_data(push, sync->addr);
603 evo_data(push, sync->data++);
604 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000605 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000606 evo_mthd(push, 0x00a0, 2);
607 evo_data(push, 0x00000000);
608 evo_data(push, 0x00000000);
609 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000610 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000611 evo_mthd(push, 0x0110, 2);
612 evo_data(push, 0x00000000);
613 evo_data(push, 0x00000000);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000614 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000615 evo_mthd(push, 0x0800, 5);
616 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
617 evo_data(push, 0);
618 evo_data(push, (fb->height << 16) | fb->width);
619 evo_data(push, nv_fb->r_pitch);
620 evo_data(push, nv_fb->r_format);
621 } else {
622 evo_mthd(push, 0x0400, 5);
623 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
624 evo_data(push, 0);
625 evo_data(push, (fb->height << 16) | fb->width);
626 evo_data(push, nv_fb->r_pitch);
627 evo_data(push, nv_fb->r_format);
628 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000629 evo_mthd(push, 0x0080, 1);
630 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000631 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000632
633 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000634 return 0;
635}
636
Ben Skeggs26f6d882011-07-04 16:25:18 +1000637/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000638 * CRTC
639 *****************************************************************************/
640static int
Ben Skeggse225f442012-11-21 14:40:21 +1000641nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000642{
Ben Skeggse225f442012-11-21 14:40:21 +1000643 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000644 struct nouveau_connector *nv_connector;
645 struct drm_connector *connector;
646 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000647
Ben Skeggs488ff202011-10-17 10:38:10 +1000648 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000649 connector = &nv_connector->base;
650 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -0700651 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +1000652 mode = DITHERING_MODE_DYNAMIC2X2;
653 } else {
654 mode = nv_connector->dithering_mode;
655 }
656
657 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
658 if (connector->display_info.bpc >= 8)
659 mode |= DITHERING_DEPTH_8BPC;
660 } else {
661 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000662 }
663
Ben Skeggsde8268c2012-11-16 10:24:31 +1000664 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000665 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000666 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000667 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
668 evo_data(push, mode);
669 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000670 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000671 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
672 evo_data(push, mode);
673 } else {
674 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
675 evo_data(push, mode);
676 }
677
Ben Skeggs438d99e2011-07-05 16:48:06 +1000678 if (update) {
679 evo_mthd(push, 0x0080, 1);
680 evo_data(push, 0x00000000);
681 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000682 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000683 }
684
685 return 0;
686}
687
688static int
Ben Skeggse225f442012-11-21 14:40:21 +1000689nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000690{
Ben Skeggse225f442012-11-21 14:40:21 +1000691 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000692 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000693 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000694 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000695 int mode = DRM_MODE_SCALE_NONE;
696 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000697
Ben Skeggs92854622011-11-11 23:49:06 +1000698 /* start off at the resolution we programmed the crtc for, this
699 * effectively handles NONE/FULL scaling
700 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000701 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000702 if (nv_connector && nv_connector->native_mode)
703 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000704
Ben Skeggs92854622011-11-11 23:49:06 +1000705 if (mode != DRM_MODE_SCALE_NONE)
706 omode = nv_connector->native_mode;
707 else
708 omode = umode;
709
710 oX = omode->hdisplay;
711 oY = omode->vdisplay;
712 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
713 oY *= 2;
714
715 /* add overscan compensation if necessary, will keep the aspect
716 * ratio the same as the backend mode unless overridden by the
717 * user setting both hborder and vborder properties.
718 */
719 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
720 (nv_connector->underscan == UNDERSCAN_AUTO &&
721 nv_connector->edid &&
722 drm_detect_hdmi_monitor(nv_connector->edid)))) {
723 u32 bX = nv_connector->underscan_hborder;
724 u32 bY = nv_connector->underscan_vborder;
725 u32 aspect = (oY << 19) / oX;
726
727 if (bX) {
728 oX -= (bX * 2);
729 if (bY) oY -= (bY * 2);
730 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
731 } else {
732 oX -= (oX >> 4) + 32;
733 if (bY) oY -= (bY * 2);
734 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000735 }
736 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000737
Ben Skeggs92854622011-11-11 23:49:06 +1000738 /* handle CENTER/ASPECT scaling, taking into account the areas
739 * removed already for overscan compensation
740 */
741 switch (mode) {
742 case DRM_MODE_SCALE_CENTER:
743 oX = min((u32)umode->hdisplay, oX);
744 oY = min((u32)umode->vdisplay, oY);
745 /* fall-through */
746 case DRM_MODE_SCALE_ASPECT:
747 if (oY < oX) {
748 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
749 oX = ((oY * aspect) + (aspect / 2)) >> 19;
750 } else {
751 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
752 oY = ((oX * aspect) + (aspect / 2)) >> 19;
753 }
754 break;
755 default:
756 break;
757 }
758
Ben Skeggsde8268c2012-11-16 10:24:31 +1000759 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000760 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000761 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000762 /*XXX: SCALE_CTRL_ACTIVE??? */
763 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
764 evo_data(push, (oY << 16) | oX);
765 evo_data(push, (oY << 16) | oX);
766 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
767 evo_data(push, 0x00000000);
768 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
769 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
770 } else {
771 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
772 evo_data(push, (oY << 16) | oX);
773 evo_data(push, (oY << 16) | oX);
774 evo_data(push, (oY << 16) | oX);
775 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
776 evo_data(push, 0x00000000);
777 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
778 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
779 }
780
781 evo_kick(push, mast);
782
Ben Skeggs3376ee32011-11-12 14:28:12 +1000783 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000784 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700785 nv50_display_flip_next(crtc, crtc->primary->fb,
786 NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000787 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000788 }
789
790 return 0;
791}
792
793static int
Ben Skeggse225f442012-11-21 14:40:21 +1000794nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000795{
Ben Skeggse225f442012-11-21 14:40:21 +1000796 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000797 u32 *push, hue, vib;
798 int adj;
799
800 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
801 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
802 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
803
804 push = evo_wait(mast, 16);
805 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000806 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000807 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
808 evo_data(push, (hue << 20) | (vib << 8));
809 } else {
810 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
811 evo_data(push, (hue << 20) | (vib << 8));
812 }
813
814 if (update) {
815 evo_mthd(push, 0x0080, 1);
816 evo_data(push, 0x00000000);
817 }
818 evo_kick(push, mast);
819 }
820
821 return 0;
822}
823
824static int
Ben Skeggse225f442012-11-21 14:40:21 +1000825nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000826 int x, int y, bool update)
827{
828 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000829 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000830 u32 *push;
831
Ben Skeggsde8268c2012-11-16 10:24:31 +1000832 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000833 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000834 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000835 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
836 evo_data(push, nvfb->nvbo->bo.offset >> 8);
837 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
838 evo_data(push, (fb->height << 16) | fb->width);
839 evo_data(push, nvfb->r_pitch);
840 evo_data(push, nvfb->r_format);
841 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
842 evo_data(push, (y << 16) | x);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000843 if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000844 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000845 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000846 }
847 } else {
848 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
849 evo_data(push, nvfb->nvbo->bo.offset >> 8);
850 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
851 evo_data(push, (fb->height << 16) | fb->width);
852 evo_data(push, nvfb->r_pitch);
853 evo_data(push, nvfb->r_format);
Ben Skeggs8a423642014-08-10 04:10:19 +1000854 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000855 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
856 evo_data(push, (y << 16) | x);
857 }
858
Ben Skeggsa46232e2011-07-07 15:23:48 +1000859 if (update) {
860 evo_mthd(push, 0x0080, 1);
861 evo_data(push, 0x00000000);
862 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000863 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000864 }
865
Ben Skeggs8a423642014-08-10 04:10:19 +1000866 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000867 return 0;
868}
869
870static void
Ben Skeggse225f442012-11-21 14:40:21 +1000871nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000872{
Ben Skeggse225f442012-11-21 14:40:21 +1000873 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000874 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000875 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000876 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000877 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
878 evo_data(push, 0x85000000);
879 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
880 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000881 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000882 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
883 evo_data(push, 0x85000000);
884 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
885 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000886 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000887 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000888 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
889 evo_data(push, 0x85000000);
890 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
891 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000892 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000893 }
894 evo_kick(push, mast);
895 }
896}
897
898static void
Ben Skeggse225f442012-11-21 14:40:21 +1000899nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000900{
Ben Skeggse225f442012-11-21 14:40:21 +1000901 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000902 u32 *push = evo_wait(mast, 16);
903 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000904 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000905 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
906 evo_data(push, 0x05000000);
907 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000908 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000909 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
910 evo_data(push, 0x05000000);
911 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
912 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000913 } else {
914 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
915 evo_data(push, 0x05000000);
916 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
917 evo_data(push, 0x00000000);
918 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000919 evo_kick(push, mast);
920 }
921}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000922
Ben Skeggsde8268c2012-11-16 10:24:31 +1000923static void
Ben Skeggse225f442012-11-21 14:40:21 +1000924nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000925{
Ben Skeggse225f442012-11-21 14:40:21 +1000926 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000927
928 if (show)
Ben Skeggse225f442012-11-21 14:40:21 +1000929 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000930 else
Ben Skeggse225f442012-11-21 14:40:21 +1000931 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000932
933 if (update) {
934 u32 *push = evo_wait(mast, 2);
935 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000936 evo_mthd(push, 0x0080, 1);
937 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000938 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000939 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000940 }
941}
942
943static void
Ben Skeggse225f442012-11-21 14:40:21 +1000944nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000945{
946}
947
948static void
Ben Skeggse225f442012-11-21 14:40:21 +1000949nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000950{
951 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000952 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000953 u32 *push;
954
Ben Skeggse225f442012-11-21 14:40:21 +1000955 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000956
Ben Skeggs56d237d2014-05-19 14:54:33 +1000957 push = evo_wait(mast, 6);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000958 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000959 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000960 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
961 evo_data(push, 0x00000000);
962 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
963 evo_data(push, 0x40000000);
964 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000965 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000966 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
967 evo_data(push, 0x00000000);
968 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
969 evo_data(push, 0x40000000);
970 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
971 evo_data(push, 0x00000000);
972 } else {
973 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
974 evo_data(push, 0x00000000);
975 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
976 evo_data(push, 0x03000000);
977 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
978 evo_data(push, 0x00000000);
979 }
980
981 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000982 }
983
Ben Skeggse225f442012-11-21 14:40:21 +1000984 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000985}
986
987static void
Ben Skeggse225f442012-11-21 14:40:21 +1000988nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000989{
990 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000991 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000992 u32 *push;
993
Ben Skeggsde8268c2012-11-16 10:24:31 +1000994 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000995 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000996 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000997 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000998 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000999 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1000 evo_data(push, 0xc0000000);
1001 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1002 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001003 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001004 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001005 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001006 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1007 evo_data(push, 0xc0000000);
1008 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1009 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001010 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001011 } else {
1012 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001013 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001014 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1015 evo_data(push, 0x83000000);
1016 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1017 evo_data(push, 0x00000000);
1018 evo_data(push, 0x00000000);
1019 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001020 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001021 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1022 evo_data(push, 0xffffff00);
1023 }
1024
1025 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001026 }
1027
Ben Skeggse225f442012-11-21 14:40:21 +10001028 nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
Matt Roperf4510a22014-04-01 15:22:40 -07001029 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001030}
1031
1032static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001033nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001034 struct drm_display_mode *adjusted_mode)
1035{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001036 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001037 return true;
1038}
1039
1040static int
Ben Skeggse225f442012-11-21 14:40:21 +10001041nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001042{
Matt Roperf4510a22014-04-01 15:22:40 -07001043 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001044 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001045 int ret;
1046
1047 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001048 if (ret == 0) {
1049 if (head->image)
1050 nouveau_bo_unpin(head->image);
1051 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001052 }
1053
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001054 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001055}
1056
1057static int
Ben Skeggse225f442012-11-21 14:40:21 +10001058nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001059 struct drm_display_mode *mode, int x, int y,
1060 struct drm_framebuffer *old_fb)
1061{
Ben Skeggse225f442012-11-21 14:40:21 +10001062 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001063 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1064 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001065 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1066 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1067 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1068 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
Roy Spliet1dce6262014-09-12 18:00:13 +02001069 u32 vblan2e = 0, vblan2s = 1, vblankus = 0;
Ben Skeggs3488c572012-03-12 11:42:20 +10001070 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001071 int ret;
1072
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001073 hactive = mode->htotal;
1074 hsynce = mode->hsync_end - mode->hsync_start - 1;
1075 hbackp = mode->htotal - mode->hsync_end;
1076 hblanke = hsynce + hbackp;
1077 hfrontp = mode->hsync_start - mode->hdisplay;
1078 hblanks = mode->htotal - hfrontp - 1;
1079
1080 vactive = mode->vtotal * vscan / ilace;
1081 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1082 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1083 vblanke = vsynce + vbackp;
1084 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1085 vblanks = vactive - vfrontp - 1;
Roy Spliet1dce6262014-09-12 18:00:13 +02001086 /* XXX: Safe underestimate, even "0" works */
1087 vblankus = (vactive - mode->vdisplay - 2) * hactive;
1088 vblankus *= 1000;
1089 vblankus /= mode->clock;
1090
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001091 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1092 vblan2e = vactive + vsynce + vbackp;
1093 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1094 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001095 }
1096
Ben Skeggse225f442012-11-21 14:40:21 +10001097 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001098 if (ret)
1099 return ret;
1100
Ben Skeggsde8268c2012-11-16 10:24:31 +10001101 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001102 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001103 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001104 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1105 evo_data(push, 0x00800000 | mode->clock);
1106 evo_data(push, (ilace == 2) ? 2 : 0);
Roy Spliet1dce6262014-09-12 18:00:13 +02001107 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001108 evo_data(push, 0x00000000);
1109 evo_data(push, (vactive << 16) | hactive);
1110 evo_data(push, ( vsynce << 16) | hsynce);
1111 evo_data(push, (vblanke << 16) | hblanke);
1112 evo_data(push, (vblanks << 16) | hblanks);
1113 evo_data(push, (vblan2e << 16) | vblan2s);
Roy Spliet1dce6262014-09-12 18:00:13 +02001114 evo_data(push, vblankus);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001115 evo_data(push, 0x00000000);
1116 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1117 evo_data(push, 0x00000311);
1118 evo_data(push, 0x00000100);
1119 } else {
1120 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1121 evo_data(push, 0x00000000);
1122 evo_data(push, (vactive << 16) | hactive);
1123 evo_data(push, ( vsynce << 16) | hsynce);
1124 evo_data(push, (vblanke << 16) | hblanke);
1125 evo_data(push, (vblanks << 16) | hblanks);
1126 evo_data(push, (vblan2e << 16) | vblan2s);
1127 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1128 evo_data(push, 0x00000000); /* ??? */
1129 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1130 evo_data(push, mode->clock * 1000);
1131 evo_data(push, 0x00200000); /* ??? */
1132 evo_data(push, mode->clock * 1000);
1133 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1134 evo_data(push, 0x00000311);
1135 evo_data(push, 0x00000100);
1136 }
1137
1138 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001139 }
1140
1141 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001142 nv50_crtc_set_dither(nv_crtc, false);
1143 nv50_crtc_set_scale(nv_crtc, false);
1144 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001145 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001146 return 0;
1147}
1148
1149static int
Ben Skeggse225f442012-11-21 14:40:21 +10001150nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001151 struct drm_framebuffer *old_fb)
1152{
Ben Skeggs77145f12012-07-31 16:16:21 +10001153 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001154 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1155 int ret;
1156
Matt Roperf4510a22014-04-01 15:22:40 -07001157 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001158 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001159 return 0;
1160 }
1161
Ben Skeggse225f442012-11-21 14:40:21 +10001162 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001163 if (ret)
1164 return ret;
1165
Ben Skeggse225f442012-11-21 14:40:21 +10001166 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001167 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1168 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001169 return 0;
1170}
1171
1172static int
Ben Skeggse225f442012-11-21 14:40:21 +10001173nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001174 struct drm_framebuffer *fb, int x, int y,
1175 enum mode_set_atomic state)
1176{
1177 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001178 nv50_display_flip_stop(crtc);
1179 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001180 return 0;
1181}
1182
1183static void
Ben Skeggse225f442012-11-21 14:40:21 +10001184nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001185{
Ben Skeggse225f442012-11-21 14:40:21 +10001186 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001187 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1188 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1189 int i;
1190
1191 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001192 u16 r = nv_crtc->lut.r[i] >> 2;
1193 u16 g = nv_crtc->lut.g[i] >> 2;
1194 u16 b = nv_crtc->lut.b[i] >> 2;
1195
Ben Skeggs648d4df2014-08-10 04:10:27 +10001196 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001197 writew(r + 0x0000, lut + (i * 0x08) + 0);
1198 writew(g + 0x0000, lut + (i * 0x08) + 2);
1199 writew(b + 0x0000, lut + (i * 0x08) + 4);
1200 } else {
1201 writew(r + 0x6000, lut + (i * 0x20) + 0);
1202 writew(g + 0x6000, lut + (i * 0x20) + 2);
1203 writew(b + 0x6000, lut + (i * 0x20) + 4);
1204 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001205 }
1206}
1207
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001208static void
1209nv50_crtc_disable(struct drm_crtc *crtc)
1210{
1211 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001212 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001213 if (head->image)
1214 nouveau_bo_unpin(head->image);
1215 nouveau_bo_ref(NULL, &head->image);
1216}
1217
Ben Skeggs438d99e2011-07-05 16:48:06 +10001218static int
Ben Skeggse225f442012-11-21 14:40:21 +10001219nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001220 uint32_t handle, uint32_t width, uint32_t height)
1221{
1222 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1223 struct drm_device *dev = crtc->dev;
1224 struct drm_gem_object *gem;
1225 struct nouveau_bo *nvbo;
1226 bool visible = (handle != 0);
1227 int i, ret = 0;
1228
1229 if (visible) {
1230 if (width != 64 || height != 64)
1231 return -EINVAL;
1232
1233 gem = drm_gem_object_lookup(dev, file_priv, handle);
1234 if (unlikely(!gem))
1235 return -ENOENT;
1236 nvbo = nouveau_gem_object(gem);
1237
1238 ret = nouveau_bo_map(nvbo);
1239 if (ret == 0) {
1240 for (i = 0; i < 64 * 64; i++) {
1241 u32 v = nouveau_bo_rd32(nvbo, i);
1242 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1243 }
1244 nouveau_bo_unmap(nvbo);
1245 }
1246
1247 drm_gem_object_unreference_unlocked(gem);
1248 }
1249
1250 if (visible != nv_crtc->cursor.visible) {
Ben Skeggse225f442012-11-21 14:40:21 +10001251 nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001252 nv_crtc->cursor.visible = visible;
1253 }
1254
1255 return ret;
1256}
1257
1258static int
Ben Skeggse225f442012-11-21 14:40:21 +10001259nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001260{
Ben Skeggse225f442012-11-21 14:40:21 +10001261 struct nv50_curs *curs = nv50_curs(crtc);
1262 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001263 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1264 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001265 return 0;
1266}
1267
1268static void
Ben Skeggse225f442012-11-21 14:40:21 +10001269nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001270 uint32_t start, uint32_t size)
1271{
1272 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Dan Carpenterbdefc8c2013-11-28 01:18:47 +03001273 u32 end = min_t(u32, start + size, 256);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001274 u32 i;
1275
1276 for (i = start; i < end; i++) {
1277 nv_crtc->lut.r[i] = r[i];
1278 nv_crtc->lut.g[i] = g[i];
1279 nv_crtc->lut.b[i] = b[i];
1280 }
1281
Ben Skeggse225f442012-11-21 14:40:21 +10001282 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001283}
1284
1285static void
Ben Skeggse225f442012-11-21 14:40:21 +10001286nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001287{
1288 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001289 struct nv50_disp *disp = nv50_disp(crtc->dev);
1290 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001291 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001292
Ben Skeggs0ad72862014-08-10 04:10:22 +10001293 list_for_each_entry(fbdma, &disp->fbdma, head) {
1294 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1295 }
1296
1297 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1298 nv50_pioc_destroy(&head->oimm.base);
1299 nv50_dmac_destroy(&head->sync.base, disp->disp);
1300 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001301
1302 /*XXX: this shouldn't be necessary, but the core doesn't call
1303 * disconnect() during the cleanup paths
1304 */
1305 if (head->image)
1306 nouveau_bo_unpin(head->image);
1307 nouveau_bo_ref(NULL, &head->image);
1308
Ben Skeggs438d99e2011-07-05 16:48:06 +10001309 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001310 if (nv_crtc->cursor.nvbo)
1311 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001312 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001313
Ben Skeggs438d99e2011-07-05 16:48:06 +10001314 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001315 if (nv_crtc->lut.nvbo)
1316 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001317 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001318
Ben Skeggs438d99e2011-07-05 16:48:06 +10001319 drm_crtc_cleanup(crtc);
1320 kfree(crtc);
1321}
1322
Ben Skeggse225f442012-11-21 14:40:21 +10001323static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1324 .dpms = nv50_crtc_dpms,
1325 .prepare = nv50_crtc_prepare,
1326 .commit = nv50_crtc_commit,
1327 .mode_fixup = nv50_crtc_mode_fixup,
1328 .mode_set = nv50_crtc_mode_set,
1329 .mode_set_base = nv50_crtc_mode_set_base,
1330 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1331 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001332 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001333};
1334
Ben Skeggse225f442012-11-21 14:40:21 +10001335static const struct drm_crtc_funcs nv50_crtc_func = {
1336 .cursor_set = nv50_crtc_cursor_set,
1337 .cursor_move = nv50_crtc_cursor_move,
1338 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001339 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001340 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001341 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001342};
1343
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001344static void
Ben Skeggse225f442012-11-21 14:40:21 +10001345nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001346{
1347}
1348
1349static void
Ben Skeggse225f442012-11-21 14:40:21 +10001350nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001351{
1352}
1353
Ben Skeggs438d99e2011-07-05 16:48:06 +10001354static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001355nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001356{
Ben Skeggse225f442012-11-21 14:40:21 +10001357 struct nv50_disp *disp = nv50_disp(dev);
1358 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001359 struct drm_crtc *crtc;
1360 int ret, i;
1361
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001362 head = kzalloc(sizeof(*head), GFP_KERNEL);
1363 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001364 return -ENOMEM;
1365
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001366 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001367 head->base.set_dither = nv50_crtc_set_dither;
1368 head->base.set_scale = nv50_crtc_set_scale;
1369 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001370 head->base.color_vibrance = 50;
1371 head->base.vibrant_hue = 0;
Ben Skeggse225f442012-11-21 14:40:21 +10001372 head->base.cursor.set_offset = nv50_cursor_set_offset;
1373 head->base.cursor.set_pos = nv50_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001374 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001375 head->base.lut.r[i] = i << 8;
1376 head->base.lut.g[i] = i << 8;
1377 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001378 }
1379
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001380 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001381 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1382 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001383 drm_mode_crtc_set_gamma_size(crtc, 256);
1384
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001385 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001386 0, 0x0000, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001387 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001388 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001389 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001390 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001391 if (ret)
1392 nouveau_bo_unpin(head->base.lut.nvbo);
1393 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001394 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001395 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001396 }
1397
1398 if (ret)
1399 goto out;
1400
Ben Skeggse225f442012-11-21 14:40:21 +10001401 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001402
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001403 /* allocate cursor resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001404 ret = nv50_curs_create(disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001405 if (ret)
1406 goto out;
1407
1408 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
1409 0, 0x0000, NULL, &head->base.cursor.nvbo);
1410 if (!ret) {
1411 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001412 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001413 ret = nouveau_bo_map(head->base.cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001414 if (ret)
1415 nouveau_bo_unpin(head->base.lut.nvbo);
1416 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001417 if (ret)
1418 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
1419 }
1420
1421 if (ret)
1422 goto out;
1423
1424 /* allocate page flip / sync resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001425 ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
1426 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001427 if (ret)
1428 goto out;
1429
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001430 head->sync.addr = EVO_FLIP_SEM0(index);
1431 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001432
1433 /* allocate overlay resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001434 ret = nv50_oimm_create(disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001435 if (ret)
1436 goto out;
1437
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001438 ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
1439 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001440 if (ret)
1441 goto out;
1442
Ben Skeggs438d99e2011-07-05 16:48:06 +10001443out:
1444 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001445 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001446 return ret;
1447}
1448
1449/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001450 * DAC
1451 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001452static void
Ben Skeggse225f442012-11-21 14:40:21 +10001453nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001454{
1455 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001456 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001457 struct {
1458 struct nv50_disp_mthd_v1 base;
1459 struct nv50_disp_dac_pwr_v0 pwr;
1460 } args = {
1461 .base.version = 1,
1462 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1463 .base.hasht = nv_encoder->dcb->hasht,
1464 .base.hashm = nv_encoder->dcb->hashm,
1465 .pwr.state = 1,
1466 .pwr.data = 1,
1467 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1468 mode != DRM_MODE_DPMS_OFF),
1469 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1470 mode != DRM_MODE_DPMS_OFF),
1471 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001472
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001473 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001474}
1475
1476static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001477nv50_dac_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001478 const struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001479 struct drm_display_mode *adjusted_mode)
1480{
1481 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1482 struct nouveau_connector *nv_connector;
1483
1484 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1485 if (nv_connector && nv_connector->native_mode) {
1486 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1487 int id = adjusted_mode->base.id;
1488 *adjusted_mode = *nv_connector->native_mode;
1489 adjusted_mode->base.id = id;
1490 }
1491 }
1492
1493 return true;
1494}
1495
1496static void
Ben Skeggse225f442012-11-21 14:40:21 +10001497nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001498{
1499}
1500
1501static void
Ben Skeggse225f442012-11-21 14:40:21 +10001502nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001503 struct drm_display_mode *adjusted_mode)
1504{
Ben Skeggse225f442012-11-21 14:40:21 +10001505 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001506 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1507 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001508 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001509
Ben Skeggse225f442012-11-21 14:40:21 +10001510 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001511
Ben Skeggs97b19b52012-11-16 11:21:37 +10001512 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001513 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001514 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001515 u32 syncs = 0x00000000;
1516
1517 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1518 syncs |= 0x00000001;
1519 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1520 syncs |= 0x00000002;
1521
1522 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1523 evo_data(push, 1 << nv_crtc->index);
1524 evo_data(push, syncs);
1525 } else {
1526 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1527 u32 syncs = 0x00000001;
1528
1529 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1530 syncs |= 0x00000008;
1531 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1532 syncs |= 0x00000010;
1533
1534 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1535 magic |= 0x00000001;
1536
1537 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1538 evo_data(push, syncs);
1539 evo_data(push, magic);
1540 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1541 evo_data(push, 1 << nv_crtc->index);
1542 }
1543
1544 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001545 }
1546
1547 nv_encoder->crtc = encoder->crtc;
1548}
1549
1550static void
Ben Skeggse225f442012-11-21 14:40:21 +10001551nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001552{
1553 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001554 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001555 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001556 u32 *push;
1557
1558 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001559 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001560
Ben Skeggs97b19b52012-11-16 11:21:37 +10001561 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001562 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001563 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001564 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1565 evo_data(push, 0x00000000);
1566 } else {
1567 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1568 evo_data(push, 0x00000000);
1569 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001570 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001571 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001572 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001573
1574 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001575}
1576
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001577static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001578nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001579{
Ben Skeggsc4abd312014-08-10 04:10:26 +10001580 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001581 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10001582 struct {
1583 struct nv50_disp_mthd_v1 base;
1584 struct nv50_disp_dac_load_v0 load;
1585 } args = {
1586 .base.version = 1,
1587 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
1588 .base.hasht = nv_encoder->dcb->hasht,
1589 .base.hashm = nv_encoder->dcb->hashm,
1590 };
1591 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10001592
Ben Skeggsc4abd312014-08-10 04:10:26 +10001593 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
1594 if (args.load.data == 0)
1595 args.load.data = 340;
1596
1597 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
1598 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001599 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001600
Ben Skeggs35b21d32012-11-08 12:08:55 +10001601 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001602}
1603
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001604static void
Ben Skeggse225f442012-11-21 14:40:21 +10001605nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001606{
1607 drm_encoder_cleanup(encoder);
1608 kfree(encoder);
1609}
1610
Ben Skeggse225f442012-11-21 14:40:21 +10001611static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1612 .dpms = nv50_dac_dpms,
1613 .mode_fixup = nv50_dac_mode_fixup,
1614 .prepare = nv50_dac_disconnect,
1615 .commit = nv50_dac_commit,
1616 .mode_set = nv50_dac_mode_set,
1617 .disable = nv50_dac_disconnect,
1618 .get_crtc = nv50_display_crtc_get,
1619 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001620};
1621
Ben Skeggse225f442012-11-21 14:40:21 +10001622static const struct drm_encoder_funcs nv50_dac_func = {
1623 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001624};
1625
1626static int
Ben Skeggse225f442012-11-21 14:40:21 +10001627nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001628{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001629 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001630 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001631 struct nouveau_encoder *nv_encoder;
1632 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001633 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001634
1635 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1636 if (!nv_encoder)
1637 return -ENOMEM;
1638 nv_encoder->dcb = dcbe;
1639 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001640 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001641
1642 encoder = to_drm_encoder(nv_encoder);
1643 encoder->possible_crtcs = dcbe->heads;
1644 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001645 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001646 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001647
1648 drm_mode_connector_attach_encoder(connector, encoder);
1649 return 0;
1650}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001651
1652/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001653 * Audio
1654 *****************************************************************************/
1655static void
Ben Skeggse225f442012-11-21 14:40:21 +10001656nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001657{
1658 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1659 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001660 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001661 struct {
1662 struct nv50_disp_mthd_v1 base;
1663 struct nv50_disp_sor_hda_eld_v0 eld;
1664 u8 data[sizeof(nv_connector->base.eld)];
1665 } args = {
1666 .base.version = 1,
1667 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1668 .base.hasht = nv_encoder->dcb->hasht,
1669 .base.hashm = nv_encoder->dcb->hashm,
1670 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001671
1672 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1673 if (!drm_detect_monitor_audio(nv_connector->edid))
1674 return;
1675
Ben Skeggs78951d22011-11-11 18:13:13 +10001676 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001677 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001678
Ben Skeggs120b0c32014-08-10 04:10:26 +10001679 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001680}
1681
1682static void
Ben Skeggse225f442012-11-21 14:40:21 +10001683nv50_audio_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001684{
1685 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001686 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001687 struct {
1688 struct nv50_disp_mthd_v1 base;
1689 struct nv50_disp_sor_hda_eld_v0 eld;
1690 } args = {
1691 .base.version = 1,
1692 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1693 .base.hasht = nv_encoder->dcb->hasht,
1694 .base.hashm = nv_encoder->dcb->hashm,
1695 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001696
Ben Skeggs120b0c32014-08-10 04:10:26 +10001697 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001698}
1699
1700/******************************************************************************
1701 * HDMI
1702 *****************************************************************************/
1703static void
Ben Skeggse225f442012-11-21 14:40:21 +10001704nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001705{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001706 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1707 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001708 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001709 struct {
1710 struct nv50_disp_mthd_v1 base;
1711 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1712 } args = {
1713 .base.version = 1,
1714 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1715 .base.hasht = nv_encoder->dcb->hasht,
1716 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1717 (0x0100 << nv_crtc->index),
1718 .pwr.state = 1,
1719 .pwr.rekey = 56, /* binary driver, and tegra, constant */
1720 };
1721 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001722 u32 max_ac_packet;
1723
1724 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1725 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1726 return;
1727
1728 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10001729 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001730 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10001731 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001732
Ben Skeggse00f2232014-08-10 04:10:26 +10001733 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10001734 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001735}
1736
1737static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001738nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001739{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001740 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001741 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001742 struct {
1743 struct nv50_disp_mthd_v1 base;
1744 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1745 } args = {
1746 .base.version = 1,
1747 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1748 .base.hasht = nv_encoder->dcb->hasht,
1749 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1750 (0x0100 << nv_crtc->index),
1751 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001752
Ben Skeggse225f442012-11-21 14:40:21 +10001753 nv50_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001754
Ben Skeggse00f2232014-08-10 04:10:26 +10001755 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001756}
1757
1758/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001759 * SOR
1760 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001761static void
Ben Skeggse225f442012-11-21 14:40:21 +10001762nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001763{
1764 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001765 struct nv50_disp *disp = nv50_disp(encoder->dev);
1766 struct {
1767 struct nv50_disp_mthd_v1 base;
1768 struct nv50_disp_sor_pwr_v0 pwr;
1769 } args = {
1770 .base.version = 1,
1771 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
1772 .base.hasht = nv_encoder->dcb->hasht,
1773 .base.hashm = nv_encoder->dcb->hashm,
1774 .pwr.state = mode == DRM_MODE_DPMS_ON,
1775 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001776 struct {
1777 struct nv50_disp_mthd_v1 base;
1778 struct nv50_disp_sor_dp_pwr_v0 pwr;
1779 } link = {
1780 .base.version = 1,
1781 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
1782 .base.hasht = nv_encoder->dcb->hasht,
1783 .base.hashm = nv_encoder->dcb->hashm,
1784 .pwr.state = mode == DRM_MODE_DPMS_ON,
1785 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10001786 struct drm_device *dev = encoder->dev;
1787 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001788
1789 nv_encoder->last_dpms = mode;
1790
1791 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1792 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1793
1794 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1795 continue;
1796
1797 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001798 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001799 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1800 return;
1801 break;
1802 }
1803 }
1804
Ben Skeggs48743222014-05-31 01:48:06 +10001805 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001806 args.pwr.state = 1;
1807 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001808 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10001809 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001810 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10001811 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001812}
1813
1814static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001815nv50_sor_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001816 const struct drm_display_mode *mode,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001817 struct drm_display_mode *adjusted_mode)
1818{
1819 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1820 struct nouveau_connector *nv_connector;
1821
1822 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1823 if (nv_connector && nv_connector->native_mode) {
1824 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1825 int id = adjusted_mode->base.id;
1826 *adjusted_mode = *nv_connector->native_mode;
1827 adjusted_mode->base.id = id;
1828 }
1829 }
1830
1831 return true;
1832}
1833
1834static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001835nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
1836{
1837 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
1838 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
1839 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001840 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001841 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
1842 evo_data(push, (nv_encoder->ctrl = temp));
1843 } else {
1844 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1845 evo_data(push, (nv_encoder->ctrl = temp));
1846 }
1847 evo_kick(push, mast);
1848 }
1849}
1850
1851static void
Ben Skeggse225f442012-11-21 14:40:21 +10001852nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001853{
1854 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001855 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001856
1857 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1858 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001859
1860 if (nv_crtc) {
1861 nv50_crtc_prepare(&nv_crtc->base);
1862 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
1863 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
1864 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001865}
1866
1867static void
Ben Skeggse225f442012-11-21 14:40:21 +10001868nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001869{
1870}
1871
1872static void
Ben Skeggse225f442012-11-21 14:40:21 +10001873nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001874 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001875{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001876 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1877 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1878 struct {
1879 struct nv50_disp_mthd_v1 base;
1880 struct nv50_disp_sor_lvds_script_v0 lvds;
1881 } lvds = {
1882 .base.version = 1,
1883 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1884 .base.hasht = nv_encoder->dcb->hasht,
1885 .base.hashm = nv_encoder->dcb->hashm,
1886 };
Ben Skeggse225f442012-11-21 14:40:21 +10001887 struct nv50_disp *disp = nv50_disp(encoder->dev);
1888 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001889 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001890 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001891 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001892 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001893 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001894 u8 owner = 1 << nv_crtc->index;
1895 u8 proto = 0xf;
1896 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001897
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001898 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001899 nv_encoder->crtc = encoder->crtc;
1900
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001901 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001902 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001903 if (nv_encoder->dcb->sorconf.link & 1) {
1904 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001905 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001906 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001907 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001908 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001909 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001910 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001911
Ben Skeggse84a35a2014-06-05 10:59:55 +10001912 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001913 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001914 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001915 proto = 0x0;
1916
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001917 if (bios->fp_no_ddc) {
1918 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001919 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001920 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001921 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001922 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001923 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001924 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001925 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001926 } else
1927 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001928 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001929 }
1930
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001931 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001932 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001933 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001934 } else {
1935 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001936 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001937 }
1938
1939 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001940 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001941 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001942
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001943 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001944 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001945 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001946 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001947 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001948 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001949 } else
1950 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001951 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001952 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001953 } else {
1954 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1955 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001956 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001957
1958 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001959 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001960 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001961 proto = 0x9;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001962 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001963 default:
1964 BUG_ON(1);
1965 break;
1966 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001967
Ben Skeggse84a35a2014-06-05 10:59:55 +10001968 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001969
Ben Skeggs648d4df2014-08-10 04:10:27 +10001970 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001971 u32 *push = evo_wait(mast, 3);
1972 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001973 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1974 u32 syncs = 0x00000001;
1975
1976 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1977 syncs |= 0x00000008;
1978 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1979 syncs |= 0x00000010;
1980
1981 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1982 magic |= 0x00000001;
1983
1984 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1985 evo_data(push, syncs | (depth << 6));
1986 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001987 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001988 }
1989
Ben Skeggse84a35a2014-06-05 10:59:55 +10001990 ctrl = proto << 8;
1991 mask = 0x00000f00;
1992 } else {
1993 ctrl = (depth << 16) | (proto << 8);
1994 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1995 ctrl |= 0x00001000;
1996 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1997 ctrl |= 0x00002000;
1998 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001999 }
2000
Ben Skeggse84a35a2014-06-05 10:59:55 +10002001 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002002}
2003
2004static void
Ben Skeggse225f442012-11-21 14:40:21 +10002005nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002006{
2007 drm_encoder_cleanup(encoder);
2008 kfree(encoder);
2009}
2010
Ben Skeggse225f442012-11-21 14:40:21 +10002011static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2012 .dpms = nv50_sor_dpms,
2013 .mode_fixup = nv50_sor_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10002014 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10002015 .commit = nv50_sor_commit,
2016 .mode_set = nv50_sor_mode_set,
2017 .disable = nv50_sor_disconnect,
2018 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002019};
2020
Ben Skeggse225f442012-11-21 14:40:21 +10002021static const struct drm_encoder_funcs nv50_sor_func = {
2022 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002023};
2024
2025static int
Ben Skeggse225f442012-11-21 14:40:21 +10002026nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002027{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002028 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002029 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002030 struct nouveau_encoder *nv_encoder;
2031 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002032 int type;
2033
2034 switch (dcbe->type) {
2035 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2036 case DCB_OUTPUT_TMDS:
2037 case DCB_OUTPUT_DP:
2038 default:
2039 type = DRM_MODE_ENCODER_TMDS;
2040 break;
2041 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002042
2043 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2044 if (!nv_encoder)
2045 return -ENOMEM;
2046 nv_encoder->dcb = dcbe;
2047 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002048 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002049 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2050
2051 encoder = to_drm_encoder(nv_encoder);
2052 encoder->possible_crtcs = dcbe->heads;
2053 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002054 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10002055 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002056
2057 drm_mode_connector_attach_encoder(connector, encoder);
2058 return 0;
2059}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002060
2061/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002062 * PIOR
2063 *****************************************************************************/
2064
2065static void
2066nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2067{
2068 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2069 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10002070 struct {
2071 struct nv50_disp_mthd_v1 base;
2072 struct nv50_disp_pior_pwr_v0 pwr;
2073 } args = {
2074 .base.version = 1,
2075 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2076 .base.hasht = nv_encoder->dcb->hasht,
2077 .base.hashm = nv_encoder->dcb->hashm,
2078 .pwr.state = mode == DRM_MODE_DPMS_ON,
2079 .pwr.type = nv_encoder->dcb->type,
2080 };
2081
2082 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002083}
2084
2085static bool
2086nv50_pior_mode_fixup(struct drm_encoder *encoder,
2087 const struct drm_display_mode *mode,
2088 struct drm_display_mode *adjusted_mode)
2089{
2090 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2091 struct nouveau_connector *nv_connector;
2092
2093 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2094 if (nv_connector && nv_connector->native_mode) {
2095 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
2096 int id = adjusted_mode->base.id;
2097 *adjusted_mode = *nv_connector->native_mode;
2098 adjusted_mode->base.id = id;
2099 }
2100 }
2101
2102 adjusted_mode->clock *= 2;
2103 return true;
2104}
2105
2106static void
2107nv50_pior_commit(struct drm_encoder *encoder)
2108{
2109}
2110
2111static void
2112nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2113 struct drm_display_mode *adjusted_mode)
2114{
2115 struct nv50_mast *mast = nv50_mast(encoder->dev);
2116 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2117 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2118 struct nouveau_connector *nv_connector;
2119 u8 owner = 1 << nv_crtc->index;
2120 u8 proto, depth;
2121 u32 *push;
2122
2123 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2124 switch (nv_connector->base.display_info.bpc) {
2125 case 10: depth = 0x6; break;
2126 case 8: depth = 0x5; break;
2127 case 6: depth = 0x2; break;
2128 default: depth = 0x0; break;
2129 }
2130
2131 switch (nv_encoder->dcb->type) {
2132 case DCB_OUTPUT_TMDS:
2133 case DCB_OUTPUT_DP:
2134 proto = 0x0;
2135 break;
2136 default:
2137 BUG_ON(1);
2138 break;
2139 }
2140
2141 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2142
2143 push = evo_wait(mast, 8);
2144 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002145 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002146 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2147 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2148 ctrl |= 0x00001000;
2149 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2150 ctrl |= 0x00002000;
2151 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2152 evo_data(push, ctrl);
2153 }
2154
2155 evo_kick(push, mast);
2156 }
2157
2158 nv_encoder->crtc = encoder->crtc;
2159}
2160
2161static void
2162nv50_pior_disconnect(struct drm_encoder *encoder)
2163{
2164 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2165 struct nv50_mast *mast = nv50_mast(encoder->dev);
2166 const int or = nv_encoder->or;
2167 u32 *push;
2168
2169 if (nv_encoder->crtc) {
2170 nv50_crtc_prepare(nv_encoder->crtc);
2171
2172 push = evo_wait(mast, 4);
2173 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002174 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002175 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2176 evo_data(push, 0x00000000);
2177 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002178 evo_kick(push, mast);
2179 }
2180 }
2181
2182 nv_encoder->crtc = NULL;
2183}
2184
2185static void
2186nv50_pior_destroy(struct drm_encoder *encoder)
2187{
2188 drm_encoder_cleanup(encoder);
2189 kfree(encoder);
2190}
2191
2192static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2193 .dpms = nv50_pior_dpms,
2194 .mode_fixup = nv50_pior_mode_fixup,
2195 .prepare = nv50_pior_disconnect,
2196 .commit = nv50_pior_commit,
2197 .mode_set = nv50_pior_mode_set,
2198 .disable = nv50_pior_disconnect,
2199 .get_crtc = nv50_display_crtc_get,
2200};
2201
2202static const struct drm_encoder_funcs nv50_pior_func = {
2203 .destroy = nv50_pior_destroy,
2204};
2205
2206static int
2207nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2208{
2209 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002210 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002211 struct nouveau_i2c_port *ddc = NULL;
2212 struct nouveau_encoder *nv_encoder;
2213 struct drm_encoder *encoder;
2214 int type;
2215
2216 switch (dcbe->type) {
2217 case DCB_OUTPUT_TMDS:
2218 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2219 type = DRM_MODE_ENCODER_TMDS;
2220 break;
2221 case DCB_OUTPUT_DP:
2222 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2223 type = DRM_MODE_ENCODER_TMDS;
2224 break;
2225 default:
2226 return -ENODEV;
2227 }
2228
2229 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2230 if (!nv_encoder)
2231 return -ENOMEM;
2232 nv_encoder->dcb = dcbe;
2233 nv_encoder->or = ffs(dcbe->or) - 1;
2234 nv_encoder->i2c = ddc;
2235
2236 encoder = to_drm_encoder(nv_encoder);
2237 encoder->possible_crtcs = dcbe->heads;
2238 encoder->possible_clones = 0;
2239 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2240 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2241
2242 drm_mode_connector_attach_encoder(connector, encoder);
2243 return 0;
2244}
2245
2246/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002247 * Framebuffer
2248 *****************************************************************************/
2249
Ben Skeggs8a423642014-08-10 04:10:19 +10002250static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002251nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002252{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002253 int i;
2254 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2255 nvif_object_fini(&fbdma->base[i]);
2256 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002257 list_del(&fbdma->head);
2258 kfree(fbdma);
2259}
2260
2261static int
2262nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2263{
2264 struct nouveau_drm *drm = nouveau_drm(dev);
2265 struct nv50_disp *disp = nv50_disp(dev);
2266 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002267 struct __attribute__ ((packed)) {
2268 struct nv_dma_v0 base;
2269 union {
2270 struct nv50_dma_v0 nv50;
2271 struct gf100_dma_v0 gf100;
2272 struct gf110_dma_v0 gf110;
2273 };
2274 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002275 struct nv50_fbdma *fbdma;
2276 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002277 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002278 int ret;
2279
2280 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002281 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002282 return 0;
2283 }
2284
2285 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2286 if (!fbdma)
2287 return -ENOMEM;
2288 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002289
Ben Skeggs4acfd702014-08-10 04:10:24 +10002290 args.base.target = NV_DMA_V0_TARGET_VRAM;
2291 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2292 args.base.start = offset;
2293 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002294
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002295 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002296 args.nv50.part = NV50_DMA_V0_PART_256;
2297 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002298 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002299 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002300 args.nv50.part = NV50_DMA_V0_PART_256;
2301 args.nv50.kind = kind;
2302 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002303 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002304 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002305 args.gf100.kind = kind;
2306 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002307 } else {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002308 args.gf110.page = GF110_DMA_V0_PAGE_LP;
2309 args.gf110.kind = kind;
2310 size += sizeof(args.gf110);
Ben Skeggs8a423642014-08-10 04:10:19 +10002311 }
2312
2313 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002314 struct nv50_head *head = nv50_head(crtc);
2315 int ret = nvif_object_init(&head->sync.base.base.user, NULL,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002316 name, NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002317 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002318 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002319 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002320 return ret;
2321 }
2322 }
2323
Ben Skeggs0ad72862014-08-10 04:10:22 +10002324 ret = nvif_object_init(&mast->base.base.user, NULL, name,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002325 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002326 &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002327 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002328 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002329 return ret;
2330 }
2331
2332 return 0;
2333}
2334
Ben Skeggsab0af552014-08-10 04:10:19 +10002335static void
2336nv50_fb_dtor(struct drm_framebuffer *fb)
2337{
2338}
2339
2340static int
2341nv50_fb_ctor(struct drm_framebuffer *fb)
2342{
2343 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2344 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2345 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002346 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002347 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2348 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002349
2350 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
2351 NV_ERROR(drm, "framebuffer requires contiguous bo\n");
2352 return -EINVAL;
2353 }
2354
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002355 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002356 tile >>= 4; /* yep.. */
2357
Ben Skeggsab0af552014-08-10 04:10:19 +10002358 switch (fb->depth) {
2359 case 8: nv_fb->r_format = 0x1e00; break;
2360 case 15: nv_fb->r_format = 0xe900; break;
2361 case 16: nv_fb->r_format = 0xe800; break;
2362 case 24:
2363 case 32: nv_fb->r_format = 0xcf00; break;
2364 case 30: nv_fb->r_format = 0xd100; break;
2365 default:
2366 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2367 return -EINVAL;
2368 }
2369
Ben Skeggs648d4df2014-08-10 04:10:27 +10002370 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002371 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2372 (fb->pitches[0] | 0x00100000);
2373 nv_fb->r_format |= kind << 16;
2374 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10002375 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002376 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2377 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002378 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002379 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2380 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002381 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002382 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002383
Ben Skeggsf392ec42014-08-10 04:10:28 +10002384 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
2385 drm->device.info.ram_user, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002386}
2387
2388/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002389 * Init
2390 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002391
Ben Skeggs2a44e492011-11-09 11:36:33 +10002392void
Ben Skeggse225f442012-11-21 14:40:21 +10002393nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002394{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002395}
2396
2397int
Ben Skeggse225f442012-11-21 14:40:21 +10002398nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002399{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002400 struct nv50_disp *disp = nv50_disp(dev);
2401 struct drm_crtc *crtc;
2402 u32 *push;
2403
2404 push = evo_wait(nv50_mast(dev), 32);
2405 if (!push)
2406 return -EBUSY;
2407
2408 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2409 struct nv50_sync *sync = nv50_sync(crtc);
2410 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002411 }
2412
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002413 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10002414 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002415 evo_kick(push, nv50_mast(dev));
2416 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002417}
2418
2419void
Ben Skeggse225f442012-11-21 14:40:21 +10002420nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002421{
Ben Skeggse225f442012-11-21 14:40:21 +10002422 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002423 struct nv50_fbdma *fbdma, *fbtmp;
2424
2425 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002426 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002427 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002428
Ben Skeggs0ad72862014-08-10 04:10:22 +10002429 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002430
Ben Skeggs816af2f2011-11-16 15:48:48 +10002431 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002432 if (disp->sync)
2433 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002434 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002435
Ben Skeggs77145f12012-07-31 16:16:21 +10002436 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002437 kfree(disp);
2438}
2439
2440int
Ben Skeggse225f442012-11-21 14:40:21 +10002441nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002442{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002443 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002444 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002445 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002446 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002447 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002448 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002449 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002450
2451 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2452 if (!disp)
2453 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10002454 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10002455
2456 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002457 nouveau_display(dev)->dtor = nv50_display_destroy;
2458 nouveau_display(dev)->init = nv50_display_init;
2459 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10002460 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2461 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002462 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002463
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002464 /* small shared memory area we use for notifiers and semaphores */
2465 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2466 0, 0x0000, NULL, &disp->sync);
2467 if (!ret) {
2468 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002469 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002470 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002471 if (ret)
2472 nouveau_bo_unpin(disp->sync);
2473 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002474 if (ret)
2475 nouveau_bo_ref(NULL, &disp->sync);
2476 }
2477
2478 if (ret)
2479 goto out;
2480
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002481 /* allocate master evo channel */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10002482 ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
2483 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002484 if (ret)
2485 goto out;
2486
Ben Skeggs438d99e2011-07-05 16:48:06 +10002487 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10002488 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsdb2bec12014-08-10 04:10:22 +10002489 crtcs = nvif_rd32(device, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10002490 else
2491 crtcs = 2;
2492
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002493 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002494 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002495 if (ret)
2496 goto out;
2497 }
2498
Ben Skeggs83fc0832011-07-05 13:08:40 +10002499 /* create encoder/connector objects based on VBIOS DCB table */
2500 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2501 connector = nouveau_connector_create(dev, dcbe->connector);
2502 if (IS_ERR(connector))
2503 continue;
2504
Ben Skeggseb6313a2013-02-11 09:52:58 +10002505 if (dcbe->location == DCB_LOC_ON_CHIP) {
2506 switch (dcbe->type) {
2507 case DCB_OUTPUT_TMDS:
2508 case DCB_OUTPUT_LVDS:
2509 case DCB_OUTPUT_DP:
2510 ret = nv50_sor_create(connector, dcbe);
2511 break;
2512 case DCB_OUTPUT_ANALOG:
2513 ret = nv50_dac_create(connector, dcbe);
2514 break;
2515 default:
2516 ret = -ENODEV;
2517 break;
2518 }
2519 } else {
2520 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002521 }
2522
Ben Skeggseb6313a2013-02-11 09:52:58 +10002523 if (ret) {
2524 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2525 dcbe->location, dcbe->type,
2526 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002527 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002528 }
2529 }
2530
2531 /* cull any connectors we created that don't have an encoder */
2532 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2533 if (connector->encoder_ids[0])
2534 continue;
2535
Ben Skeggs77145f12012-07-31 16:16:21 +10002536 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002537 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002538 connector->funcs->destroy(connector);
2539 }
2540
Ben Skeggs26f6d882011-07-04 16:25:18 +10002541out:
2542 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002543 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002544 return ret;
2545}