blob: 82d6b4f6a5c2d3451396017b83148582baecd236 [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);
72 if (oclass++, ret == 0)
73 return ret;
74 }
75 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100076}
77
78static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100079nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100080{
Ben Skeggs0ad72862014-08-10 04:10:22 +100081 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100082}
83
84/******************************************************************************
85 * PIO EVO channel
86 *****************************************************************************/
87
Ben Skeggse225f442012-11-21 14:40:21 +100088struct nv50_pioc {
89 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100090};
91
92static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100093nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100094{
Ben Skeggs0ad72862014-08-10 04:10:22 +100095 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100096}
97
98static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +100099nv50_pioc_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +1000100 void *data, u32 size, struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000101{
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000102 return nv50_chan_create(disp, oclass, head, data, size, &pioc->base);
103}
104
105/******************************************************************************
106 * Cursor Immediate
107 *****************************************************************************/
108
109struct nv50_curs {
110 struct nv50_pioc base;
111};
112
113static int
114nv50_curs_create(struct nvif_object *disp, int head, struct nv50_curs *curs)
115{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000116 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000117 .head = head,
118 };
119 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000120 GK104_DISP_CURSOR,
121 GF110_DISP_CURSOR,
122 GT214_DISP_CURSOR,
123 G82_DISP_CURSOR,
124 NV50_DISP_CURSOR,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000125 0
126 };
127
128 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
129 &curs->base);
130}
131
132/******************************************************************************
133 * Overlay Immediate
134 *****************************************************************************/
135
136struct nv50_oimm {
137 struct nv50_pioc base;
138};
139
140static int
141nv50_oimm_create(struct nvif_object *disp, int head, struct nv50_oimm *oimm)
142{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000143 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000144 .head = head,
145 };
146 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000147 GK104_DISP_OVERLAY,
148 GF110_DISP_OVERLAY,
149 GT214_DISP_OVERLAY,
150 G82_DISP_OVERLAY,
151 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000152 0
153 };
154
155 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
156 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000157}
158
159/******************************************************************************
160 * DMA EVO channel
161 *****************************************************************************/
162
Ben Skeggse225f442012-11-21 14:40:21 +1000163struct nv50_dmac {
164 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000165 dma_addr_t handle;
166 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100167
Ben Skeggs0ad72862014-08-10 04:10:22 +1000168 struct nvif_object sync;
169 struct nvif_object vram;
170
Daniel Vetter59ad1462012-12-02 14:49:44 +0100171 /* Protects against concurrent pushbuf access to this channel, lock is
172 * grabbed by evo_wait (if the pushbuf reservation is successful) and
173 * dropped again by evo_kick. */
174 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000175};
176
177static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000178nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000179{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000180 nvif_object_fini(&dmac->vram);
181 nvif_object_fini(&dmac->sync);
182
183 nv50_chan_destroy(&dmac->base);
184
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000185 if (dmac->ptr) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000186 struct pci_dev *pdev = nvkm_device(nvif_device(disp))->pdev;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000187 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
188 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000189}
190
191static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000192nv50_dmac_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000193 void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000194 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000195{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000196 struct nouveau_fb *pfb = nvkm_fb(nvif_device(disp));
Ben Skeggs648d4df2014-08-10 04:10:27 +1000197 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000198 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000199 int ret;
200
Daniel Vetter59ad1462012-12-02 14:49:44 +0100201 mutex_init(&dmac->lock);
202
Ben Skeggs0ad72862014-08-10 04:10:22 +1000203 dmac->ptr = pci_alloc_consistent(nvkm_device(nvif_device(disp))->pdev,
204 PAGE_SIZE, &dmac->handle);
Ben Skeggs47057302012-11-16 13:58:48 +1000205 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000206 return -ENOMEM;
207
Ben Skeggs648d4df2014-08-10 04:10:27 +1000208 ret = nvif_object_init(nvif_object(nvif_device(disp)), NULL,
209 args->pushbuf, NV_DMA_FROM_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000210 &(struct nv_dma_v0) {
211 .target = NV_DMA_V0_TARGET_PCI_US,
212 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000213 .start = dmac->handle + 0x0000,
214 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000215 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000216 if (ret)
217 return ret;
218
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000219 ret = nv50_chan_create(disp, oclass, head, data, size, &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000220 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000221 if (ret)
222 return ret;
223
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000224 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000000,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000225 NV_DMA_IN_MEMORY,
226 &(struct nv_dma_v0) {
227 .target = NV_DMA_V0_TARGET_VRAM,
228 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000229 .start = syncbuf + 0x0000,
230 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000231 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000232 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000233 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000234 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000235
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000236 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000001,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000237 NV_DMA_IN_MEMORY,
238 &(struct nv_dma_v0) {
239 .target = NV_DMA_V0_TARGET_VRAM,
240 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000241 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000242 .limit = pfb->ram->size - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000243 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000244 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000245 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000246 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000247
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000248 return ret;
249}
250
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000251/******************************************************************************
252 * Core
253 *****************************************************************************/
254
Ben Skeggse225f442012-11-21 14:40:21 +1000255struct nv50_mast {
256 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000257};
258
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000259static int
260nv50_core_create(struct nvif_object *disp, u64 syncbuf, struct nv50_mast *core)
261{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000262 struct nv50_disp_core_channel_dma_v0 args = {
263 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000264 };
265 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000266 GM107_DISP_CORE_CHANNEL_DMA,
267 GK110_DISP_CORE_CHANNEL_DMA,
268 GK104_DISP_CORE_CHANNEL_DMA,
269 GF110_DISP_CORE_CHANNEL_DMA,
270 GT214_DISP_CORE_CHANNEL_DMA,
271 GT206_DISP_CORE_CHANNEL_DMA,
272 GT200_DISP_CORE_CHANNEL_DMA,
273 G82_DISP_CORE_CHANNEL_DMA,
274 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000275 0
276 };
277
278 return nv50_dmac_create(disp, oclass, 0, &args, sizeof(args), syncbuf,
279 &core->base);
280}
281
282/******************************************************************************
283 * Base
284 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000285
Ben Skeggse225f442012-11-21 14:40:21 +1000286struct nv50_sync {
287 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000288 u32 addr;
289 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000290};
291
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000292static int
293nv50_base_create(struct nvif_object *disp, int head, u64 syncbuf,
294 struct nv50_sync *base)
295{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000296 struct nv50_disp_base_channel_dma_v0 args = {
297 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000298 .head = head,
299 };
300 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000301 GK110_DISP_BASE_CHANNEL_DMA,
302 GK104_DISP_BASE_CHANNEL_DMA,
303 GF110_DISP_BASE_CHANNEL_DMA,
304 GT214_DISP_BASE_CHANNEL_DMA,
305 GT200_DISP_BASE_CHANNEL_DMA,
306 G82_DISP_BASE_CHANNEL_DMA,
307 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000308 0
309 };
310
311 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
312 syncbuf, &base->base);
313}
314
315/******************************************************************************
316 * Overlay
317 *****************************************************************************/
318
Ben Skeggse225f442012-11-21 14:40:21 +1000319struct nv50_ovly {
320 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000321};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000322
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000323static int
324nv50_ovly_create(struct nvif_object *disp, int head, u64 syncbuf,
325 struct nv50_ovly *ovly)
326{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000327 struct nv50_disp_overlay_channel_dma_v0 args = {
328 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000329 .head = head,
330 };
331 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000332 GK104_DISP_OVERLAY_CONTROL_DMA,
333 GF110_DISP_OVERLAY_CONTROL_DMA,
334 GT214_DISP_OVERLAY_CHANNEL_DMA,
335 GT200_DISP_OVERLAY_CHANNEL_DMA,
336 G82_DISP_OVERLAY_CHANNEL_DMA,
337 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000338 0
339 };
340
341 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
342 syncbuf, &ovly->base);
343}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000344
Ben Skeggse225f442012-11-21 14:40:21 +1000345struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000346 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000347 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000348 struct nv50_curs curs;
349 struct nv50_sync sync;
350 struct nv50_ovly ovly;
351 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000352};
353
Ben Skeggse225f442012-11-21 14:40:21 +1000354#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
355#define nv50_curs(c) (&nv50_head(c)->curs)
356#define nv50_sync(c) (&nv50_head(c)->sync)
357#define nv50_ovly(c) (&nv50_head(c)->ovly)
358#define nv50_oimm(c) (&nv50_head(c)->oimm)
359#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000360#define nv50_vers(c) nv50_chan(c)->user.oclass
361
362struct nv50_fbdma {
363 struct list_head head;
364 struct nvif_object core;
365 struct nvif_object base[4];
366};
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000367
Ben Skeggse225f442012-11-21 14:40:21 +1000368struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000369 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000370 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000371
Ben Skeggs8a423642014-08-10 04:10:19 +1000372 struct list_head fbdma;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000373
374 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000375};
376
Ben Skeggse225f442012-11-21 14:40:21 +1000377static struct nv50_disp *
378nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000379{
Ben Skeggs77145f12012-07-31 16:16:21 +1000380 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000381}
382
Ben Skeggse225f442012-11-21 14:40:21 +1000383#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000384
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000385static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000386nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000387{
388 return nouveau_encoder(encoder)->crtc;
389}
390
391/******************************************************************************
392 * EVO channel helpers
393 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000394static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000395evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000396{
Ben Skeggse225f442012-11-21 14:40:21 +1000397 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000398 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000399
Daniel Vetter59ad1462012-12-02 14:49:44 +0100400 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000401 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000402 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000403
Ben Skeggs0ad72862014-08-10 04:10:22 +1000404 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
405 if (!nvkm_wait(&dmac->base.user, 0x0004, ~0, 0x00000000)) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100406 mutex_unlock(&dmac->lock);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000407 nv_error(nvkm_object(&dmac->base.user), "channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000408 return NULL;
409 }
410
411 put = 0;
412 }
413
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000414 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000415}
416
417static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000418evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000419{
Ben Skeggse225f442012-11-21 14:40:21 +1000420 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000421 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100422 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000423}
424
425#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
426#define evo_data(p,d) *((p)++) = (d)
427
Ben Skeggs3376ee32011-11-12 14:28:12 +1000428static bool
429evo_sync_wait(void *data)
430{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500431 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
432 return true;
433 usleep_range(1, 2);
434 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000435}
436
437static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000438evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000439{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000440 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000441 struct nv50_disp *disp = nv50_disp(dev);
442 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000443 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000444 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000445 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000446 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000447 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000448 evo_mthd(push, 0x0080, 2);
449 evo_data(push, 0x00000000);
450 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000451 evo_kick(push, mast);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000452 if (nv_wait_cb(nvkm_device(device), evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000453 return 0;
454 }
455
456 return -EBUSY;
457}
458
459/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000460 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000461 *****************************************************************************/
462struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000463nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000464{
Ben Skeggse225f442012-11-21 14:40:21 +1000465 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000466}
467
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000468struct nv50_display_flip {
469 struct nv50_disp *disp;
470 struct nv50_sync *chan;
471};
472
473static bool
474nv50_display_flip_wait(void *data)
475{
476 struct nv50_display_flip *flip = data;
477 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500478 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000479 return true;
480 usleep_range(1, 2);
481 return false;
482}
483
Ben Skeggs3376ee32011-11-12 14:28:12 +1000484void
Ben Skeggse225f442012-11-21 14:40:21 +1000485nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000486{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000487 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000488 struct nv50_display_flip flip = {
489 .disp = nv50_disp(crtc->dev),
490 .chan = nv50_sync(crtc),
491 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000492 u32 *push;
493
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000494 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000495 if (push) {
496 evo_mthd(push, 0x0084, 1);
497 evo_data(push, 0x00000000);
498 evo_mthd(push, 0x0094, 1);
499 evo_data(push, 0x00000000);
500 evo_mthd(push, 0x00c0, 1);
501 evo_data(push, 0x00000000);
502 evo_mthd(push, 0x0080, 1);
503 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000504 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000505 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000506
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000507 nv_wait_cb(nvkm_device(device), nv50_display_flip_wait, &flip);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000508}
509
510int
Ben Skeggse225f442012-11-21 14:40:21 +1000511nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000512 struct nouveau_channel *chan, u32 swap_interval)
513{
514 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000515 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000516 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000517 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000518 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000519 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000520
521 swap_interval <<= 4;
522 if (swap_interval == 0)
523 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000524 if (chan == NULL)
525 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000526
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000527 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000528 if (unlikely(push == NULL))
529 return -EBUSY;
530
Ben Skeggsbbf89062014-08-10 04:10:25 +1000531 if (chan && chan->object->oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000532 ret = RING_SPACE(chan, 8);
533 if (ret)
534 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000535
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000536 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000537 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000538 OUT_RING (chan, sync->addr ^ 0x10);
539 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
540 OUT_RING (chan, sync->data + 1);
541 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
542 OUT_RING (chan, sync->addr);
543 OUT_RING (chan, sync->data);
544 } else
Ben Skeggsbbf89062014-08-10 04:10:25 +1000545 if (chan && chan->object->oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000546 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000547 ret = RING_SPACE(chan, 12);
548 if (ret)
549 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000550
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000551 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000552 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000553 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
554 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
555 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
556 OUT_RING (chan, sync->data + 1);
557 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
558 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
559 OUT_RING (chan, upper_32_bits(addr));
560 OUT_RING (chan, lower_32_bits(addr));
561 OUT_RING (chan, sync->data);
562 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
563 } else
564 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000565 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000566 ret = RING_SPACE(chan, 10);
567 if (ret)
568 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000569
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000570 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
571 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
572 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
573 OUT_RING (chan, sync->data + 1);
574 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
575 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
576 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
577 OUT_RING (chan, upper_32_bits(addr));
578 OUT_RING (chan, lower_32_bits(addr));
579 OUT_RING (chan, sync->data);
580 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
581 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
582 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500583
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000584 if (chan) {
585 sync->addr ^= 0x10;
586 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000587 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000588 }
589
590 /* queue the flip */
591 evo_mthd(push, 0x0100, 1);
592 evo_data(push, 0xfffe0000);
593 evo_mthd(push, 0x0084, 1);
594 evo_data(push, swap_interval);
595 if (!(swap_interval & 0x00000100)) {
596 evo_mthd(push, 0x00e0, 1);
597 evo_data(push, 0x40000000);
598 }
599 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000600 evo_data(push, sync->addr);
601 evo_data(push, sync->data++);
602 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000603 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000604 evo_mthd(push, 0x00a0, 2);
605 evo_data(push, 0x00000000);
606 evo_data(push, 0x00000000);
607 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000608 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000609 evo_mthd(push, 0x0110, 2);
610 evo_data(push, 0x00000000);
611 evo_data(push, 0x00000000);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000612 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000613 evo_mthd(push, 0x0800, 5);
614 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
615 evo_data(push, 0);
616 evo_data(push, (fb->height << 16) | fb->width);
617 evo_data(push, nv_fb->r_pitch);
618 evo_data(push, nv_fb->r_format);
619 } else {
620 evo_mthd(push, 0x0400, 5);
621 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
622 evo_data(push, 0);
623 evo_data(push, (fb->height << 16) | fb->width);
624 evo_data(push, nv_fb->r_pitch);
625 evo_data(push, nv_fb->r_format);
626 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000627 evo_mthd(push, 0x0080, 1);
628 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000629 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000630
631 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000632 return 0;
633}
634
Ben Skeggs26f6d882011-07-04 16:25:18 +1000635/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000636 * CRTC
637 *****************************************************************************/
638static int
Ben Skeggse225f442012-11-21 14:40:21 +1000639nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000640{
Ben Skeggse225f442012-11-21 14:40:21 +1000641 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000642 struct nouveau_connector *nv_connector;
643 struct drm_connector *connector;
644 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000645
Ben Skeggs488ff202011-10-17 10:38:10 +1000646 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000647 connector = &nv_connector->base;
648 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -0700649 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +1000650 mode = DITHERING_MODE_DYNAMIC2X2;
651 } else {
652 mode = nv_connector->dithering_mode;
653 }
654
655 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
656 if (connector->display_info.bpc >= 8)
657 mode |= DITHERING_DEPTH_8BPC;
658 } else {
659 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000660 }
661
Ben Skeggsde8268c2012-11-16 10:24:31 +1000662 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000663 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000664 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000665 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
666 evo_data(push, mode);
667 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000668 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000669 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
670 evo_data(push, mode);
671 } else {
672 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
673 evo_data(push, mode);
674 }
675
Ben Skeggs438d99e2011-07-05 16:48:06 +1000676 if (update) {
677 evo_mthd(push, 0x0080, 1);
678 evo_data(push, 0x00000000);
679 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000680 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000681 }
682
683 return 0;
684}
685
686static int
Ben Skeggse225f442012-11-21 14:40:21 +1000687nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000688{
Ben Skeggse225f442012-11-21 14:40:21 +1000689 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000690 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000691 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000692 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000693 int mode = DRM_MODE_SCALE_NONE;
694 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000695
Ben Skeggs92854622011-11-11 23:49:06 +1000696 /* start off at the resolution we programmed the crtc for, this
697 * effectively handles NONE/FULL scaling
698 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000699 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000700 if (nv_connector && nv_connector->native_mode)
701 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000702
Ben Skeggs92854622011-11-11 23:49:06 +1000703 if (mode != DRM_MODE_SCALE_NONE)
704 omode = nv_connector->native_mode;
705 else
706 omode = umode;
707
708 oX = omode->hdisplay;
709 oY = omode->vdisplay;
710 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
711 oY *= 2;
712
713 /* add overscan compensation if necessary, will keep the aspect
714 * ratio the same as the backend mode unless overridden by the
715 * user setting both hborder and vborder properties.
716 */
717 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
718 (nv_connector->underscan == UNDERSCAN_AUTO &&
719 nv_connector->edid &&
720 drm_detect_hdmi_monitor(nv_connector->edid)))) {
721 u32 bX = nv_connector->underscan_hborder;
722 u32 bY = nv_connector->underscan_vborder;
723 u32 aspect = (oY << 19) / oX;
724
725 if (bX) {
726 oX -= (bX * 2);
727 if (bY) oY -= (bY * 2);
728 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
729 } else {
730 oX -= (oX >> 4) + 32;
731 if (bY) oY -= (bY * 2);
732 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000733 }
734 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000735
Ben Skeggs92854622011-11-11 23:49:06 +1000736 /* handle CENTER/ASPECT scaling, taking into account the areas
737 * removed already for overscan compensation
738 */
739 switch (mode) {
740 case DRM_MODE_SCALE_CENTER:
741 oX = min((u32)umode->hdisplay, oX);
742 oY = min((u32)umode->vdisplay, oY);
743 /* fall-through */
744 case DRM_MODE_SCALE_ASPECT:
745 if (oY < oX) {
746 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
747 oX = ((oY * aspect) + (aspect / 2)) >> 19;
748 } else {
749 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
750 oY = ((oX * aspect) + (aspect / 2)) >> 19;
751 }
752 break;
753 default:
754 break;
755 }
756
Ben Skeggsde8268c2012-11-16 10:24:31 +1000757 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000758 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000759 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000760 /*XXX: SCALE_CTRL_ACTIVE??? */
761 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
762 evo_data(push, (oY << 16) | oX);
763 evo_data(push, (oY << 16) | oX);
764 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
765 evo_data(push, 0x00000000);
766 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
767 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
768 } else {
769 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
770 evo_data(push, (oY << 16) | oX);
771 evo_data(push, (oY << 16) | oX);
772 evo_data(push, (oY << 16) | oX);
773 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
774 evo_data(push, 0x00000000);
775 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
776 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
777 }
778
779 evo_kick(push, mast);
780
Ben Skeggs3376ee32011-11-12 14:28:12 +1000781 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000782 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700783 nv50_display_flip_next(crtc, crtc->primary->fb,
784 NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000785 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000786 }
787
788 return 0;
789}
790
791static int
Ben Skeggse225f442012-11-21 14:40:21 +1000792nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000793{
Ben Skeggse225f442012-11-21 14:40:21 +1000794 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000795 u32 *push, hue, vib;
796 int adj;
797
798 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
799 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
800 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
801
802 push = evo_wait(mast, 16);
803 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000804 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000805 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
806 evo_data(push, (hue << 20) | (vib << 8));
807 } else {
808 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
809 evo_data(push, (hue << 20) | (vib << 8));
810 }
811
812 if (update) {
813 evo_mthd(push, 0x0080, 1);
814 evo_data(push, 0x00000000);
815 }
816 evo_kick(push, mast);
817 }
818
819 return 0;
820}
821
822static int
Ben Skeggse225f442012-11-21 14:40:21 +1000823nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000824 int x, int y, bool update)
825{
826 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000827 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000828 u32 *push;
829
Ben Skeggsde8268c2012-11-16 10:24:31 +1000830 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000831 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000832 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000833 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
834 evo_data(push, nvfb->nvbo->bo.offset >> 8);
835 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
836 evo_data(push, (fb->height << 16) | fb->width);
837 evo_data(push, nvfb->r_pitch);
838 evo_data(push, nvfb->r_format);
839 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
840 evo_data(push, (y << 16) | x);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000841 if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000842 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000843 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000844 }
845 } else {
846 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
847 evo_data(push, nvfb->nvbo->bo.offset >> 8);
848 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
849 evo_data(push, (fb->height << 16) | fb->width);
850 evo_data(push, nvfb->r_pitch);
851 evo_data(push, nvfb->r_format);
Ben Skeggs8a423642014-08-10 04:10:19 +1000852 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000853 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
854 evo_data(push, (y << 16) | x);
855 }
856
Ben Skeggsa46232e2011-07-07 15:23:48 +1000857 if (update) {
858 evo_mthd(push, 0x0080, 1);
859 evo_data(push, 0x00000000);
860 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000861 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000862 }
863
Ben Skeggs8a423642014-08-10 04:10:19 +1000864 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000865 return 0;
866}
867
868static void
Ben Skeggse225f442012-11-21 14:40:21 +1000869nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000870{
Ben Skeggse225f442012-11-21 14:40:21 +1000871 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000872 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000873 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000874 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000875 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
876 evo_data(push, 0x85000000);
877 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
878 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000879 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000880 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
881 evo_data(push, 0x85000000);
882 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
883 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000884 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000885 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000886 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
887 evo_data(push, 0x85000000);
888 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
889 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000890 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000891 }
892 evo_kick(push, mast);
893 }
894}
895
896static void
Ben Skeggse225f442012-11-21 14:40:21 +1000897nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000898{
Ben Skeggse225f442012-11-21 14:40:21 +1000899 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000900 u32 *push = evo_wait(mast, 16);
901 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000902 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000903 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
904 evo_data(push, 0x05000000);
905 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000906 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000907 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
908 evo_data(push, 0x05000000);
909 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
910 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000911 } else {
912 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
913 evo_data(push, 0x05000000);
914 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
915 evo_data(push, 0x00000000);
916 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000917 evo_kick(push, mast);
918 }
919}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000920
Ben Skeggsde8268c2012-11-16 10:24:31 +1000921static void
Ben Skeggse225f442012-11-21 14:40:21 +1000922nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000923{
Ben Skeggse225f442012-11-21 14:40:21 +1000924 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000925
926 if (show)
Ben Skeggse225f442012-11-21 14:40:21 +1000927 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000928 else
Ben Skeggse225f442012-11-21 14:40:21 +1000929 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000930
931 if (update) {
932 u32 *push = evo_wait(mast, 2);
933 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000934 evo_mthd(push, 0x0080, 1);
935 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000936 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000937 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000938 }
939}
940
941static void
Ben Skeggse225f442012-11-21 14:40:21 +1000942nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000943{
944}
945
946static void
Ben Skeggse225f442012-11-21 14:40:21 +1000947nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000948{
949 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000950 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000951 u32 *push;
952
Ben Skeggse225f442012-11-21 14:40:21 +1000953 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000954
Ben Skeggs56d237d2014-05-19 14:54:33 +1000955 push = evo_wait(mast, 6);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000956 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000957 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000958 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
959 evo_data(push, 0x00000000);
960 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
961 evo_data(push, 0x40000000);
962 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000963 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000964 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
965 evo_data(push, 0x00000000);
966 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
967 evo_data(push, 0x40000000);
968 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
969 evo_data(push, 0x00000000);
970 } else {
971 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
972 evo_data(push, 0x00000000);
973 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
974 evo_data(push, 0x03000000);
975 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
976 evo_data(push, 0x00000000);
977 }
978
979 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000980 }
981
Ben Skeggse225f442012-11-21 14:40:21 +1000982 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000983}
984
985static void
Ben Skeggse225f442012-11-21 14:40:21 +1000986nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000987{
988 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000989 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000990 u32 *push;
991
Ben Skeggsde8268c2012-11-16 10:24:31 +1000992 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000993 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000994 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000995 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000996 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000997 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
998 evo_data(push, 0xc0000000);
999 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1000 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001001 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001002 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001003 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001004 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1005 evo_data(push, 0xc0000000);
1006 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1007 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001008 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001009 } else {
1010 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001011 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001012 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1013 evo_data(push, 0x83000000);
1014 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1015 evo_data(push, 0x00000000);
1016 evo_data(push, 0x00000000);
1017 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001018 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001019 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1020 evo_data(push, 0xffffff00);
1021 }
1022
1023 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001024 }
1025
Ben Skeggse225f442012-11-21 14:40:21 +10001026 nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
Matt Roperf4510a22014-04-01 15:22:40 -07001027 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001028}
1029
1030static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001031nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001032 struct drm_display_mode *adjusted_mode)
1033{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001034 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001035 return true;
1036}
1037
1038static int
Ben Skeggse225f442012-11-21 14:40:21 +10001039nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001040{
Matt Roperf4510a22014-04-01 15:22:40 -07001041 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001042 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001043 int ret;
1044
1045 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001046 if (ret == 0) {
1047 if (head->image)
1048 nouveau_bo_unpin(head->image);
1049 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001050 }
1051
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001052 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001053}
1054
1055static int
Ben Skeggse225f442012-11-21 14:40:21 +10001056nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001057 struct drm_display_mode *mode, int x, int y,
1058 struct drm_framebuffer *old_fb)
1059{
Ben Skeggse225f442012-11-21 14:40:21 +10001060 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001061 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1062 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001063 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1064 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1065 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1066 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
1067 u32 vblan2e = 0, vblan2s = 1;
Ben Skeggs3488c572012-03-12 11:42:20 +10001068 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001069 int ret;
1070
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001071 hactive = mode->htotal;
1072 hsynce = mode->hsync_end - mode->hsync_start - 1;
1073 hbackp = mode->htotal - mode->hsync_end;
1074 hblanke = hsynce + hbackp;
1075 hfrontp = mode->hsync_start - mode->hdisplay;
1076 hblanks = mode->htotal - hfrontp - 1;
1077
1078 vactive = mode->vtotal * vscan / ilace;
1079 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1080 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1081 vblanke = vsynce + vbackp;
1082 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1083 vblanks = vactive - vfrontp - 1;
1084 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1085 vblan2e = vactive + vsynce + vbackp;
1086 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1087 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001088 }
1089
Ben Skeggse225f442012-11-21 14:40:21 +10001090 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001091 if (ret)
1092 return ret;
1093
Ben Skeggsde8268c2012-11-16 10:24:31 +10001094 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001095 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001096 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001097 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1098 evo_data(push, 0x00800000 | mode->clock);
1099 evo_data(push, (ilace == 2) ? 2 : 0);
1100 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
1101 evo_data(push, 0x00000000);
1102 evo_data(push, (vactive << 16) | hactive);
1103 evo_data(push, ( vsynce << 16) | hsynce);
1104 evo_data(push, (vblanke << 16) | hblanke);
1105 evo_data(push, (vblanks << 16) | hblanks);
1106 evo_data(push, (vblan2e << 16) | vblan2s);
1107 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
1108 evo_data(push, 0x00000000);
1109 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1110 evo_data(push, 0x00000311);
1111 evo_data(push, 0x00000100);
1112 } else {
1113 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1114 evo_data(push, 0x00000000);
1115 evo_data(push, (vactive << 16) | hactive);
1116 evo_data(push, ( vsynce << 16) | hsynce);
1117 evo_data(push, (vblanke << 16) | hblanke);
1118 evo_data(push, (vblanks << 16) | hblanks);
1119 evo_data(push, (vblan2e << 16) | vblan2s);
1120 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1121 evo_data(push, 0x00000000); /* ??? */
1122 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1123 evo_data(push, mode->clock * 1000);
1124 evo_data(push, 0x00200000); /* ??? */
1125 evo_data(push, mode->clock * 1000);
1126 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1127 evo_data(push, 0x00000311);
1128 evo_data(push, 0x00000100);
1129 }
1130
1131 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001132 }
1133
1134 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001135 nv50_crtc_set_dither(nv_crtc, false);
1136 nv50_crtc_set_scale(nv_crtc, false);
1137 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001138 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001139 return 0;
1140}
1141
1142static int
Ben Skeggse225f442012-11-21 14:40:21 +10001143nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001144 struct drm_framebuffer *old_fb)
1145{
Ben Skeggs77145f12012-07-31 16:16:21 +10001146 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001147 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1148 int ret;
1149
Matt Roperf4510a22014-04-01 15:22:40 -07001150 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001151 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001152 return 0;
1153 }
1154
Ben Skeggse225f442012-11-21 14:40:21 +10001155 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001156 if (ret)
1157 return ret;
1158
Ben Skeggse225f442012-11-21 14:40:21 +10001159 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001160 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1161 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001162 return 0;
1163}
1164
1165static int
Ben Skeggse225f442012-11-21 14:40:21 +10001166nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001167 struct drm_framebuffer *fb, int x, int y,
1168 enum mode_set_atomic state)
1169{
1170 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001171 nv50_display_flip_stop(crtc);
1172 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001173 return 0;
1174}
1175
1176static void
Ben Skeggse225f442012-11-21 14:40:21 +10001177nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001178{
Ben Skeggse225f442012-11-21 14:40:21 +10001179 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001180 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1181 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1182 int i;
1183
1184 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001185 u16 r = nv_crtc->lut.r[i] >> 2;
1186 u16 g = nv_crtc->lut.g[i] >> 2;
1187 u16 b = nv_crtc->lut.b[i] >> 2;
1188
Ben Skeggs648d4df2014-08-10 04:10:27 +10001189 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001190 writew(r + 0x0000, lut + (i * 0x08) + 0);
1191 writew(g + 0x0000, lut + (i * 0x08) + 2);
1192 writew(b + 0x0000, lut + (i * 0x08) + 4);
1193 } else {
1194 writew(r + 0x6000, lut + (i * 0x20) + 0);
1195 writew(g + 0x6000, lut + (i * 0x20) + 2);
1196 writew(b + 0x6000, lut + (i * 0x20) + 4);
1197 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001198 }
1199}
1200
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001201static void
1202nv50_crtc_disable(struct drm_crtc *crtc)
1203{
1204 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001205 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001206 if (head->image)
1207 nouveau_bo_unpin(head->image);
1208 nouveau_bo_ref(NULL, &head->image);
1209}
1210
Ben Skeggs438d99e2011-07-05 16:48:06 +10001211static int
Ben Skeggse225f442012-11-21 14:40:21 +10001212nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001213 uint32_t handle, uint32_t width, uint32_t height)
1214{
1215 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1216 struct drm_device *dev = crtc->dev;
1217 struct drm_gem_object *gem;
1218 struct nouveau_bo *nvbo;
1219 bool visible = (handle != 0);
1220 int i, ret = 0;
1221
1222 if (visible) {
1223 if (width != 64 || height != 64)
1224 return -EINVAL;
1225
1226 gem = drm_gem_object_lookup(dev, file_priv, handle);
1227 if (unlikely(!gem))
1228 return -ENOENT;
1229 nvbo = nouveau_gem_object(gem);
1230
1231 ret = nouveau_bo_map(nvbo);
1232 if (ret == 0) {
1233 for (i = 0; i < 64 * 64; i++) {
1234 u32 v = nouveau_bo_rd32(nvbo, i);
1235 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1236 }
1237 nouveau_bo_unmap(nvbo);
1238 }
1239
1240 drm_gem_object_unreference_unlocked(gem);
1241 }
1242
1243 if (visible != nv_crtc->cursor.visible) {
Ben Skeggse225f442012-11-21 14:40:21 +10001244 nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001245 nv_crtc->cursor.visible = visible;
1246 }
1247
1248 return ret;
1249}
1250
1251static int
Ben Skeggse225f442012-11-21 14:40:21 +10001252nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001253{
Ben Skeggse225f442012-11-21 14:40:21 +10001254 struct nv50_curs *curs = nv50_curs(crtc);
1255 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001256 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1257 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001258 return 0;
1259}
1260
1261static void
Ben Skeggse225f442012-11-21 14:40:21 +10001262nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001263 uint32_t start, uint32_t size)
1264{
1265 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Dan Carpenterbdefc8c2013-11-28 01:18:47 +03001266 u32 end = min_t(u32, start + size, 256);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001267 u32 i;
1268
1269 for (i = start; i < end; i++) {
1270 nv_crtc->lut.r[i] = r[i];
1271 nv_crtc->lut.g[i] = g[i];
1272 nv_crtc->lut.b[i] = b[i];
1273 }
1274
Ben Skeggse225f442012-11-21 14:40:21 +10001275 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001276}
1277
1278static void
Ben Skeggse225f442012-11-21 14:40:21 +10001279nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001280{
1281 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001282 struct nv50_disp *disp = nv50_disp(crtc->dev);
1283 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001284 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001285
Ben Skeggs0ad72862014-08-10 04:10:22 +10001286 list_for_each_entry(fbdma, &disp->fbdma, head) {
1287 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1288 }
1289
1290 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1291 nv50_pioc_destroy(&head->oimm.base);
1292 nv50_dmac_destroy(&head->sync.base, disp->disp);
1293 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001294
1295 /*XXX: this shouldn't be necessary, but the core doesn't call
1296 * disconnect() during the cleanup paths
1297 */
1298 if (head->image)
1299 nouveau_bo_unpin(head->image);
1300 nouveau_bo_ref(NULL, &head->image);
1301
Ben Skeggs438d99e2011-07-05 16:48:06 +10001302 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001303 if (nv_crtc->cursor.nvbo)
1304 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001305 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001306
Ben Skeggs438d99e2011-07-05 16:48:06 +10001307 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001308 if (nv_crtc->lut.nvbo)
1309 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001310 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001311
Ben Skeggs438d99e2011-07-05 16:48:06 +10001312 drm_crtc_cleanup(crtc);
1313 kfree(crtc);
1314}
1315
Ben Skeggse225f442012-11-21 14:40:21 +10001316static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1317 .dpms = nv50_crtc_dpms,
1318 .prepare = nv50_crtc_prepare,
1319 .commit = nv50_crtc_commit,
1320 .mode_fixup = nv50_crtc_mode_fixup,
1321 .mode_set = nv50_crtc_mode_set,
1322 .mode_set_base = nv50_crtc_mode_set_base,
1323 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1324 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001325 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001326};
1327
Ben Skeggse225f442012-11-21 14:40:21 +10001328static const struct drm_crtc_funcs nv50_crtc_func = {
1329 .cursor_set = nv50_crtc_cursor_set,
1330 .cursor_move = nv50_crtc_cursor_move,
1331 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001332 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001333 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001334 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001335};
1336
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001337static void
Ben Skeggse225f442012-11-21 14:40:21 +10001338nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001339{
1340}
1341
1342static void
Ben Skeggse225f442012-11-21 14:40:21 +10001343nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001344{
1345}
1346
Ben Skeggs438d99e2011-07-05 16:48:06 +10001347static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001348nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001349{
Ben Skeggse225f442012-11-21 14:40:21 +10001350 struct nv50_disp *disp = nv50_disp(dev);
1351 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001352 struct drm_crtc *crtc;
1353 int ret, i;
1354
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001355 head = kzalloc(sizeof(*head), GFP_KERNEL);
1356 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001357 return -ENOMEM;
1358
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001359 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001360 head->base.set_dither = nv50_crtc_set_dither;
1361 head->base.set_scale = nv50_crtc_set_scale;
1362 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001363 head->base.color_vibrance = 50;
1364 head->base.vibrant_hue = 0;
Ben Skeggse225f442012-11-21 14:40:21 +10001365 head->base.cursor.set_offset = nv50_cursor_set_offset;
1366 head->base.cursor.set_pos = nv50_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001367 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001368 head->base.lut.r[i] = i << 8;
1369 head->base.lut.g[i] = i << 8;
1370 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001371 }
1372
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001373 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001374 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1375 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001376 drm_mode_crtc_set_gamma_size(crtc, 256);
1377
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001378 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001379 0, 0x0000, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001380 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001381 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001382 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001383 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001384 if (ret)
1385 nouveau_bo_unpin(head->base.lut.nvbo);
1386 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001387 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001388 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001389 }
1390
1391 if (ret)
1392 goto out;
1393
Ben Skeggse225f442012-11-21 14:40:21 +10001394 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001395
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001396 /* allocate cursor resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001397 ret = nv50_curs_create(disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001398 if (ret)
1399 goto out;
1400
1401 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
1402 0, 0x0000, NULL, &head->base.cursor.nvbo);
1403 if (!ret) {
1404 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001405 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001406 ret = nouveau_bo_map(head->base.cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001407 if (ret)
1408 nouveau_bo_unpin(head->base.lut.nvbo);
1409 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001410 if (ret)
1411 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
1412 }
1413
1414 if (ret)
1415 goto out;
1416
1417 /* allocate page flip / sync resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001418 ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
1419 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001420 if (ret)
1421 goto out;
1422
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001423 head->sync.addr = EVO_FLIP_SEM0(index);
1424 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001425
1426 /* allocate overlay resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001427 ret = nv50_oimm_create(disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001428 if (ret)
1429 goto out;
1430
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001431 ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
1432 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001433 if (ret)
1434 goto out;
1435
Ben Skeggs438d99e2011-07-05 16:48:06 +10001436out:
1437 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001438 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001439 return ret;
1440}
1441
1442/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001443 * DAC
1444 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001445static void
Ben Skeggse225f442012-11-21 14:40:21 +10001446nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001447{
1448 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001449 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001450 struct {
1451 struct nv50_disp_mthd_v1 base;
1452 struct nv50_disp_dac_pwr_v0 pwr;
1453 } args = {
1454 .base.version = 1,
1455 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1456 .base.hasht = nv_encoder->dcb->hasht,
1457 .base.hashm = nv_encoder->dcb->hashm,
1458 .pwr.state = 1,
1459 .pwr.data = 1,
1460 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1461 mode != DRM_MODE_DPMS_OFF),
1462 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1463 mode != DRM_MODE_DPMS_OFF),
1464 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001465
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001466 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001467}
1468
1469static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001470nv50_dac_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001471 const struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001472 struct drm_display_mode *adjusted_mode)
1473{
1474 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1475 struct nouveau_connector *nv_connector;
1476
1477 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1478 if (nv_connector && nv_connector->native_mode) {
1479 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1480 int id = adjusted_mode->base.id;
1481 *adjusted_mode = *nv_connector->native_mode;
1482 adjusted_mode->base.id = id;
1483 }
1484 }
1485
1486 return true;
1487}
1488
1489static void
Ben Skeggse225f442012-11-21 14:40:21 +10001490nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001491{
1492}
1493
1494static void
Ben Skeggse225f442012-11-21 14:40:21 +10001495nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001496 struct drm_display_mode *adjusted_mode)
1497{
Ben Skeggse225f442012-11-21 14:40:21 +10001498 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001499 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1500 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001501 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001502
Ben Skeggse225f442012-11-21 14:40:21 +10001503 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001504
Ben Skeggs97b19b52012-11-16 11:21:37 +10001505 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001506 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001507 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001508 u32 syncs = 0x00000000;
1509
1510 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1511 syncs |= 0x00000001;
1512 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1513 syncs |= 0x00000002;
1514
1515 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1516 evo_data(push, 1 << nv_crtc->index);
1517 evo_data(push, syncs);
1518 } else {
1519 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1520 u32 syncs = 0x00000001;
1521
1522 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1523 syncs |= 0x00000008;
1524 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1525 syncs |= 0x00000010;
1526
1527 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1528 magic |= 0x00000001;
1529
1530 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1531 evo_data(push, syncs);
1532 evo_data(push, magic);
1533 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1534 evo_data(push, 1 << nv_crtc->index);
1535 }
1536
1537 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001538 }
1539
1540 nv_encoder->crtc = encoder->crtc;
1541}
1542
1543static void
Ben Skeggse225f442012-11-21 14:40:21 +10001544nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001545{
1546 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001547 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001548 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001549 u32 *push;
1550
1551 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001552 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001553
Ben Skeggs97b19b52012-11-16 11:21:37 +10001554 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001555 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001556 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001557 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1558 evo_data(push, 0x00000000);
1559 } else {
1560 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1561 evo_data(push, 0x00000000);
1562 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001563 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001564 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001565 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001566
1567 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001568}
1569
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001570static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001571nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001572{
Ben Skeggsc4abd312014-08-10 04:10:26 +10001573 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001574 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10001575 struct {
1576 struct nv50_disp_mthd_v1 base;
1577 struct nv50_disp_dac_load_v0 load;
1578 } args = {
1579 .base.version = 1,
1580 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
1581 .base.hasht = nv_encoder->dcb->hasht,
1582 .base.hashm = nv_encoder->dcb->hashm,
1583 };
1584 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10001585
Ben Skeggsc4abd312014-08-10 04:10:26 +10001586 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
1587 if (args.load.data == 0)
1588 args.load.data = 340;
1589
1590 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
1591 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001592 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001593
Ben Skeggs35b21d32012-11-08 12:08:55 +10001594 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001595}
1596
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001597static void
Ben Skeggse225f442012-11-21 14:40:21 +10001598nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001599{
1600 drm_encoder_cleanup(encoder);
1601 kfree(encoder);
1602}
1603
Ben Skeggse225f442012-11-21 14:40:21 +10001604static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1605 .dpms = nv50_dac_dpms,
1606 .mode_fixup = nv50_dac_mode_fixup,
1607 .prepare = nv50_dac_disconnect,
1608 .commit = nv50_dac_commit,
1609 .mode_set = nv50_dac_mode_set,
1610 .disable = nv50_dac_disconnect,
1611 .get_crtc = nv50_display_crtc_get,
1612 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001613};
1614
Ben Skeggse225f442012-11-21 14:40:21 +10001615static const struct drm_encoder_funcs nv50_dac_func = {
1616 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001617};
1618
1619static int
Ben Skeggse225f442012-11-21 14:40:21 +10001620nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001621{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001622 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001623 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001624 struct nouveau_encoder *nv_encoder;
1625 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001626 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001627
1628 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1629 if (!nv_encoder)
1630 return -ENOMEM;
1631 nv_encoder->dcb = dcbe;
1632 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001633 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001634
1635 encoder = to_drm_encoder(nv_encoder);
1636 encoder->possible_crtcs = dcbe->heads;
1637 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001638 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001639 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001640
1641 drm_mode_connector_attach_encoder(connector, encoder);
1642 return 0;
1643}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001644
1645/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001646 * Audio
1647 *****************************************************************************/
1648static void
Ben Skeggse225f442012-11-21 14:40:21 +10001649nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001650{
1651 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1652 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001653 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001654 struct {
1655 struct nv50_disp_mthd_v1 base;
1656 struct nv50_disp_sor_hda_eld_v0 eld;
1657 u8 data[sizeof(nv_connector->base.eld)];
1658 } args = {
1659 .base.version = 1,
1660 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1661 .base.hasht = nv_encoder->dcb->hasht,
1662 .base.hashm = nv_encoder->dcb->hashm,
1663 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001664
1665 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1666 if (!drm_detect_monitor_audio(nv_connector->edid))
1667 return;
1668
Ben Skeggs78951d22011-11-11 18:13:13 +10001669 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001670 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001671
Ben Skeggs120b0c32014-08-10 04:10:26 +10001672 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001673}
1674
1675static void
Ben Skeggse225f442012-11-21 14:40:21 +10001676nv50_audio_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001677{
1678 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001679 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001680 struct {
1681 struct nv50_disp_mthd_v1 base;
1682 struct nv50_disp_sor_hda_eld_v0 eld;
1683 } args = {
1684 .base.version = 1,
1685 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1686 .base.hasht = nv_encoder->dcb->hasht,
1687 .base.hashm = nv_encoder->dcb->hashm,
1688 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001689
Ben Skeggs120b0c32014-08-10 04:10:26 +10001690 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001691}
1692
1693/******************************************************************************
1694 * HDMI
1695 *****************************************************************************/
1696static void
Ben Skeggse225f442012-11-21 14:40:21 +10001697nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001698{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001699 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1700 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001701 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001702 struct {
1703 struct nv50_disp_mthd_v1 base;
1704 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1705 } args = {
1706 .base.version = 1,
1707 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1708 .base.hasht = nv_encoder->dcb->hasht,
1709 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1710 (0x0100 << nv_crtc->index),
1711 .pwr.state = 1,
1712 .pwr.rekey = 56, /* binary driver, and tegra, constant */
1713 };
1714 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001715 u32 max_ac_packet;
1716
1717 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1718 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1719 return;
1720
1721 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10001722 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001723 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10001724 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001725
Ben Skeggse00f2232014-08-10 04:10:26 +10001726 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10001727 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001728}
1729
1730static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001731nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001732{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001733 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001734 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001735 struct {
1736 struct nv50_disp_mthd_v1 base;
1737 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1738 } args = {
1739 .base.version = 1,
1740 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1741 .base.hasht = nv_encoder->dcb->hasht,
1742 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1743 (0x0100 << nv_crtc->index),
1744 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001745
Ben Skeggse225f442012-11-21 14:40:21 +10001746 nv50_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001747
Ben Skeggse00f2232014-08-10 04:10:26 +10001748 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001749}
1750
1751/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001752 * SOR
1753 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001754static void
Ben Skeggse225f442012-11-21 14:40:21 +10001755nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001756{
1757 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001758 struct nv50_disp *disp = nv50_disp(encoder->dev);
1759 struct {
1760 struct nv50_disp_mthd_v1 base;
1761 struct nv50_disp_sor_pwr_v0 pwr;
1762 } args = {
1763 .base.version = 1,
1764 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
1765 .base.hasht = nv_encoder->dcb->hasht,
1766 .base.hashm = nv_encoder->dcb->hashm,
1767 .pwr.state = mode == DRM_MODE_DPMS_ON,
1768 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001769 struct {
1770 struct nv50_disp_mthd_v1 base;
1771 struct nv50_disp_sor_dp_pwr_v0 pwr;
1772 } link = {
1773 .base.version = 1,
1774 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
1775 .base.hasht = nv_encoder->dcb->hasht,
1776 .base.hashm = nv_encoder->dcb->hashm,
1777 .pwr.state = mode == DRM_MODE_DPMS_ON,
1778 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10001779 struct drm_device *dev = encoder->dev;
1780 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001781
1782 nv_encoder->last_dpms = mode;
1783
1784 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1785 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1786
1787 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1788 continue;
1789
1790 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001791 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001792 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1793 return;
1794 break;
1795 }
1796 }
1797
Ben Skeggs48743222014-05-31 01:48:06 +10001798 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001799 args.pwr.state = 1;
1800 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001801 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10001802 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001803 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10001804 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001805}
1806
1807static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001808nv50_sor_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001809 const struct drm_display_mode *mode,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001810 struct drm_display_mode *adjusted_mode)
1811{
1812 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1813 struct nouveau_connector *nv_connector;
1814
1815 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1816 if (nv_connector && nv_connector->native_mode) {
1817 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1818 int id = adjusted_mode->base.id;
1819 *adjusted_mode = *nv_connector->native_mode;
1820 adjusted_mode->base.id = id;
1821 }
1822 }
1823
1824 return true;
1825}
1826
1827static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001828nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
1829{
1830 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
1831 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
1832 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001833 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001834 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
1835 evo_data(push, (nv_encoder->ctrl = temp));
1836 } else {
1837 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1838 evo_data(push, (nv_encoder->ctrl = temp));
1839 }
1840 evo_kick(push, mast);
1841 }
1842}
1843
1844static void
Ben Skeggse225f442012-11-21 14:40:21 +10001845nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001846{
1847 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001848 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001849
1850 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1851 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001852
1853 if (nv_crtc) {
1854 nv50_crtc_prepare(&nv_crtc->base);
1855 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
1856 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
1857 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001858}
1859
1860static void
Ben Skeggse225f442012-11-21 14:40:21 +10001861nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001862{
1863}
1864
1865static void
Ben Skeggse225f442012-11-21 14:40:21 +10001866nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001867 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001868{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001869 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1870 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1871 struct {
1872 struct nv50_disp_mthd_v1 base;
1873 struct nv50_disp_sor_lvds_script_v0 lvds;
1874 } lvds = {
1875 .base.version = 1,
1876 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1877 .base.hasht = nv_encoder->dcb->hasht,
1878 .base.hashm = nv_encoder->dcb->hashm,
1879 };
Ben Skeggse225f442012-11-21 14:40:21 +10001880 struct nv50_disp *disp = nv50_disp(encoder->dev);
1881 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001882 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001883 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001884 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001885 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001886 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001887 u8 owner = 1 << nv_crtc->index;
1888 u8 proto = 0xf;
1889 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001890
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001891 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001892 nv_encoder->crtc = encoder->crtc;
1893
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001894 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001895 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001896 if (nv_encoder->dcb->sorconf.link & 1) {
1897 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001898 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001899 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001900 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001901 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001902 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001903 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001904
Ben Skeggse84a35a2014-06-05 10:59:55 +10001905 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001906 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001907 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001908 proto = 0x0;
1909
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001910 if (bios->fp_no_ddc) {
1911 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001912 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001913 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001914 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001915 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001916 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001917 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001918 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001919 } else
1920 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001921 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001922 }
1923
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001924 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001925 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001926 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001927 } else {
1928 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001929 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001930 }
1931
1932 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001933 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001934 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001935
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001936 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001937 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001938 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001939 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001940 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001941 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001942 } else
1943 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001944 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001945 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001946 } else {
1947 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1948 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001949 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001950
1951 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001952 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001953 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001954 proto = 0x9;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001955 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001956 default:
1957 BUG_ON(1);
1958 break;
1959 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001960
Ben Skeggse84a35a2014-06-05 10:59:55 +10001961 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001962
Ben Skeggs648d4df2014-08-10 04:10:27 +10001963 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001964 u32 *push = evo_wait(mast, 3);
1965 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001966 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1967 u32 syncs = 0x00000001;
1968
1969 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1970 syncs |= 0x00000008;
1971 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1972 syncs |= 0x00000010;
1973
1974 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1975 magic |= 0x00000001;
1976
1977 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1978 evo_data(push, syncs | (depth << 6));
1979 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001980 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001981 }
1982
Ben Skeggse84a35a2014-06-05 10:59:55 +10001983 ctrl = proto << 8;
1984 mask = 0x00000f00;
1985 } else {
1986 ctrl = (depth << 16) | (proto << 8);
1987 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1988 ctrl |= 0x00001000;
1989 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1990 ctrl |= 0x00002000;
1991 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001992 }
1993
Ben Skeggse84a35a2014-06-05 10:59:55 +10001994 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001995}
1996
1997static void
Ben Skeggse225f442012-11-21 14:40:21 +10001998nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001999{
2000 drm_encoder_cleanup(encoder);
2001 kfree(encoder);
2002}
2003
Ben Skeggse225f442012-11-21 14:40:21 +10002004static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2005 .dpms = nv50_sor_dpms,
2006 .mode_fixup = nv50_sor_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10002007 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10002008 .commit = nv50_sor_commit,
2009 .mode_set = nv50_sor_mode_set,
2010 .disable = nv50_sor_disconnect,
2011 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002012};
2013
Ben Skeggse225f442012-11-21 14:40:21 +10002014static const struct drm_encoder_funcs nv50_sor_func = {
2015 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002016};
2017
2018static int
Ben Skeggse225f442012-11-21 14:40:21 +10002019nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002020{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002021 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002022 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002023 struct nouveau_encoder *nv_encoder;
2024 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002025 int type;
2026
2027 switch (dcbe->type) {
2028 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2029 case DCB_OUTPUT_TMDS:
2030 case DCB_OUTPUT_DP:
2031 default:
2032 type = DRM_MODE_ENCODER_TMDS;
2033 break;
2034 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002035
2036 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2037 if (!nv_encoder)
2038 return -ENOMEM;
2039 nv_encoder->dcb = dcbe;
2040 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002041 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002042 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2043
2044 encoder = to_drm_encoder(nv_encoder);
2045 encoder->possible_crtcs = dcbe->heads;
2046 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002047 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10002048 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002049
2050 drm_mode_connector_attach_encoder(connector, encoder);
2051 return 0;
2052}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002053
2054/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002055 * PIOR
2056 *****************************************************************************/
2057
2058static void
2059nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2060{
2061 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2062 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10002063 struct {
2064 struct nv50_disp_mthd_v1 base;
2065 struct nv50_disp_pior_pwr_v0 pwr;
2066 } args = {
2067 .base.version = 1,
2068 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2069 .base.hasht = nv_encoder->dcb->hasht,
2070 .base.hashm = nv_encoder->dcb->hashm,
2071 .pwr.state = mode == DRM_MODE_DPMS_ON,
2072 .pwr.type = nv_encoder->dcb->type,
2073 };
2074
2075 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002076}
2077
2078static bool
2079nv50_pior_mode_fixup(struct drm_encoder *encoder,
2080 const struct drm_display_mode *mode,
2081 struct drm_display_mode *adjusted_mode)
2082{
2083 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2084 struct nouveau_connector *nv_connector;
2085
2086 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2087 if (nv_connector && nv_connector->native_mode) {
2088 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
2089 int id = adjusted_mode->base.id;
2090 *adjusted_mode = *nv_connector->native_mode;
2091 adjusted_mode->base.id = id;
2092 }
2093 }
2094
2095 adjusted_mode->clock *= 2;
2096 return true;
2097}
2098
2099static void
2100nv50_pior_commit(struct drm_encoder *encoder)
2101{
2102}
2103
2104static void
2105nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2106 struct drm_display_mode *adjusted_mode)
2107{
2108 struct nv50_mast *mast = nv50_mast(encoder->dev);
2109 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2110 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2111 struct nouveau_connector *nv_connector;
2112 u8 owner = 1 << nv_crtc->index;
2113 u8 proto, depth;
2114 u32 *push;
2115
2116 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2117 switch (nv_connector->base.display_info.bpc) {
2118 case 10: depth = 0x6; break;
2119 case 8: depth = 0x5; break;
2120 case 6: depth = 0x2; break;
2121 default: depth = 0x0; break;
2122 }
2123
2124 switch (nv_encoder->dcb->type) {
2125 case DCB_OUTPUT_TMDS:
2126 case DCB_OUTPUT_DP:
2127 proto = 0x0;
2128 break;
2129 default:
2130 BUG_ON(1);
2131 break;
2132 }
2133
2134 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2135
2136 push = evo_wait(mast, 8);
2137 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002138 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002139 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2140 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2141 ctrl |= 0x00001000;
2142 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2143 ctrl |= 0x00002000;
2144 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2145 evo_data(push, ctrl);
2146 }
2147
2148 evo_kick(push, mast);
2149 }
2150
2151 nv_encoder->crtc = encoder->crtc;
2152}
2153
2154static void
2155nv50_pior_disconnect(struct drm_encoder *encoder)
2156{
2157 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2158 struct nv50_mast *mast = nv50_mast(encoder->dev);
2159 const int or = nv_encoder->or;
2160 u32 *push;
2161
2162 if (nv_encoder->crtc) {
2163 nv50_crtc_prepare(nv_encoder->crtc);
2164
2165 push = evo_wait(mast, 4);
2166 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002167 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002168 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2169 evo_data(push, 0x00000000);
2170 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002171 evo_kick(push, mast);
2172 }
2173 }
2174
2175 nv_encoder->crtc = NULL;
2176}
2177
2178static void
2179nv50_pior_destroy(struct drm_encoder *encoder)
2180{
2181 drm_encoder_cleanup(encoder);
2182 kfree(encoder);
2183}
2184
2185static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2186 .dpms = nv50_pior_dpms,
2187 .mode_fixup = nv50_pior_mode_fixup,
2188 .prepare = nv50_pior_disconnect,
2189 .commit = nv50_pior_commit,
2190 .mode_set = nv50_pior_mode_set,
2191 .disable = nv50_pior_disconnect,
2192 .get_crtc = nv50_display_crtc_get,
2193};
2194
2195static const struct drm_encoder_funcs nv50_pior_func = {
2196 .destroy = nv50_pior_destroy,
2197};
2198
2199static int
2200nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2201{
2202 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002203 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002204 struct nouveau_i2c_port *ddc = NULL;
2205 struct nouveau_encoder *nv_encoder;
2206 struct drm_encoder *encoder;
2207 int type;
2208
2209 switch (dcbe->type) {
2210 case DCB_OUTPUT_TMDS:
2211 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2212 type = DRM_MODE_ENCODER_TMDS;
2213 break;
2214 case DCB_OUTPUT_DP:
2215 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2216 type = DRM_MODE_ENCODER_TMDS;
2217 break;
2218 default:
2219 return -ENODEV;
2220 }
2221
2222 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2223 if (!nv_encoder)
2224 return -ENOMEM;
2225 nv_encoder->dcb = dcbe;
2226 nv_encoder->or = ffs(dcbe->or) - 1;
2227 nv_encoder->i2c = ddc;
2228
2229 encoder = to_drm_encoder(nv_encoder);
2230 encoder->possible_crtcs = dcbe->heads;
2231 encoder->possible_clones = 0;
2232 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2233 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2234
2235 drm_mode_connector_attach_encoder(connector, encoder);
2236 return 0;
2237}
2238
2239/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002240 * Framebuffer
2241 *****************************************************************************/
2242
Ben Skeggs8a423642014-08-10 04:10:19 +10002243static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002244nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002245{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002246 int i;
2247 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2248 nvif_object_fini(&fbdma->base[i]);
2249 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002250 list_del(&fbdma->head);
2251 kfree(fbdma);
2252}
2253
2254static int
2255nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2256{
2257 struct nouveau_drm *drm = nouveau_drm(dev);
2258 struct nv50_disp *disp = nv50_disp(dev);
2259 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002260 struct __attribute__ ((packed)) {
2261 struct nv_dma_v0 base;
2262 union {
2263 struct nv50_dma_v0 nv50;
2264 struct gf100_dma_v0 gf100;
2265 struct gf110_dma_v0 gf110;
2266 };
2267 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002268 struct nv50_fbdma *fbdma;
2269 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002270 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002271 int ret;
2272
2273 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002274 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002275 return 0;
2276 }
2277
2278 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2279 if (!fbdma)
2280 return -ENOMEM;
2281 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002282
Ben Skeggs4acfd702014-08-10 04:10:24 +10002283 args.base.target = NV_DMA_V0_TARGET_VRAM;
2284 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2285 args.base.start = offset;
2286 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002287
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002288 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002289 args.nv50.part = NV50_DMA_V0_PART_256;
2290 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002291 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002292 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002293 args.nv50.part = NV50_DMA_V0_PART_256;
2294 args.nv50.kind = kind;
2295 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002296 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002297 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002298 args.gf100.kind = kind;
2299 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002300 } else {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002301 args.gf110.page = GF110_DMA_V0_PAGE_LP;
2302 args.gf110.kind = kind;
2303 size += sizeof(args.gf110);
Ben Skeggs8a423642014-08-10 04:10:19 +10002304 }
2305
2306 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002307 struct nv50_head *head = nv50_head(crtc);
2308 int ret = nvif_object_init(&head->sync.base.base.user, NULL,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002309 name, NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002310 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002311 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002312 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002313 return ret;
2314 }
2315 }
2316
Ben Skeggs0ad72862014-08-10 04:10:22 +10002317 ret = nvif_object_init(&mast->base.base.user, NULL, name,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002318 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002319 &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002320 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002321 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002322 return ret;
2323 }
2324
2325 return 0;
2326}
2327
Ben Skeggsab0af552014-08-10 04:10:19 +10002328static void
2329nv50_fb_dtor(struct drm_framebuffer *fb)
2330{
2331}
2332
2333static int
2334nv50_fb_ctor(struct drm_framebuffer *fb)
2335{
2336 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2337 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2338 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002339 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002340 struct nouveau_fb *pfb = nvkm_fb(&drm->device);
Ben Skeggs8a423642014-08-10 04:10:19 +10002341 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2342 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002343
2344 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
2345 NV_ERROR(drm, "framebuffer requires contiguous bo\n");
2346 return -EINVAL;
2347 }
2348
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002349 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002350 tile >>= 4; /* yep.. */
2351
Ben Skeggsab0af552014-08-10 04:10:19 +10002352 switch (fb->depth) {
2353 case 8: nv_fb->r_format = 0x1e00; break;
2354 case 15: nv_fb->r_format = 0xe900; break;
2355 case 16: nv_fb->r_format = 0xe800; break;
2356 case 24:
2357 case 32: nv_fb->r_format = 0xcf00; break;
2358 case 30: nv_fb->r_format = 0xd100; break;
2359 default:
2360 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2361 return -EINVAL;
2362 }
2363
Ben Skeggs648d4df2014-08-10 04:10:27 +10002364 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002365 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2366 (fb->pitches[0] | 0x00100000);
2367 nv_fb->r_format |= kind << 16;
2368 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10002369 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002370 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2371 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002372 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002373 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2374 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002375 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002376 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002377
Ben Skeggs8a423642014-08-10 04:10:19 +10002378 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0, pfb->ram->size, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002379}
2380
2381/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002382 * Init
2383 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002384
Ben Skeggs2a44e492011-11-09 11:36:33 +10002385void
Ben Skeggse225f442012-11-21 14:40:21 +10002386nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002387{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002388}
2389
2390int
Ben Skeggse225f442012-11-21 14:40:21 +10002391nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002392{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002393 struct nv50_disp *disp = nv50_disp(dev);
2394 struct drm_crtc *crtc;
2395 u32 *push;
2396
2397 push = evo_wait(nv50_mast(dev), 32);
2398 if (!push)
2399 return -EBUSY;
2400
2401 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2402 struct nv50_sync *sync = nv50_sync(crtc);
2403 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002404 }
2405
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002406 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10002407 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002408 evo_kick(push, nv50_mast(dev));
2409 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002410}
2411
2412void
Ben Skeggse225f442012-11-21 14:40:21 +10002413nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002414{
Ben Skeggse225f442012-11-21 14:40:21 +10002415 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002416 struct nv50_fbdma *fbdma, *fbtmp;
2417
2418 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002419 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002420 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002421
Ben Skeggs0ad72862014-08-10 04:10:22 +10002422 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002423
Ben Skeggs816af2f2011-11-16 15:48:48 +10002424 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002425 if (disp->sync)
2426 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002427 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002428
Ben Skeggs77145f12012-07-31 16:16:21 +10002429 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002430 kfree(disp);
2431}
2432
2433int
Ben Skeggse225f442012-11-21 14:40:21 +10002434nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002435{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002436 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002437 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002438 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002439 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002440 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002441 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002442 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002443
2444 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2445 if (!disp)
2446 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10002447 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10002448
2449 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002450 nouveau_display(dev)->dtor = nv50_display_destroy;
2451 nouveau_display(dev)->init = nv50_display_init;
2452 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10002453 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2454 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002455 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002456
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002457 /* small shared memory area we use for notifiers and semaphores */
2458 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2459 0, 0x0000, NULL, &disp->sync);
2460 if (!ret) {
2461 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002462 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002463 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002464 if (ret)
2465 nouveau_bo_unpin(disp->sync);
2466 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002467 if (ret)
2468 nouveau_bo_ref(NULL, &disp->sync);
2469 }
2470
2471 if (ret)
2472 goto out;
2473
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002474 /* allocate master evo channel */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10002475 ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
2476 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002477 if (ret)
2478 goto out;
2479
Ben Skeggs438d99e2011-07-05 16:48:06 +10002480 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10002481 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsdb2bec12014-08-10 04:10:22 +10002482 crtcs = nvif_rd32(device, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10002483 else
2484 crtcs = 2;
2485
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002486 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002487 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002488 if (ret)
2489 goto out;
2490 }
2491
Ben Skeggs83fc0832011-07-05 13:08:40 +10002492 /* create encoder/connector objects based on VBIOS DCB table */
2493 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2494 connector = nouveau_connector_create(dev, dcbe->connector);
2495 if (IS_ERR(connector))
2496 continue;
2497
Ben Skeggseb6313a2013-02-11 09:52:58 +10002498 if (dcbe->location == DCB_LOC_ON_CHIP) {
2499 switch (dcbe->type) {
2500 case DCB_OUTPUT_TMDS:
2501 case DCB_OUTPUT_LVDS:
2502 case DCB_OUTPUT_DP:
2503 ret = nv50_sor_create(connector, dcbe);
2504 break;
2505 case DCB_OUTPUT_ANALOG:
2506 ret = nv50_dac_create(connector, dcbe);
2507 break;
2508 default:
2509 ret = -ENODEV;
2510 break;
2511 }
2512 } else {
2513 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002514 }
2515
Ben Skeggseb6313a2013-02-11 09:52:58 +10002516 if (ret) {
2517 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2518 dcbe->location, dcbe->type,
2519 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002520 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002521 }
2522 }
2523
2524 /* cull any connectors we created that don't have an encoder */
2525 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2526 if (connector->encoder_ids[0])
2527 continue;
2528
Ben Skeggs77145f12012-07-31 16:16:21 +10002529 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002530 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002531 connector->funcs->destroy(connector);
2532 }
2533
Ben Skeggs26f6d882011-07-04 16:25:18 +10002534out:
2535 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002536 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002537 return ret;
2538}