blob: 76b8c4f980ea0faa245cdebef6d4b9b0d66e3a97 [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>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010029#include <drm/drm_plane_helper.h>
Ben Skeggs48743222014-05-31 01:48:06 +100030#include <drm/drm_dp_helper.h>
Ben Skeggs26f6d882011-07-04 16:25:18 +100031
Ben Skeggsfdb751e2014-08-10 04:10:23 +100032#include <nvif/class.h>
33
Ben Skeggs77145f12012-07-31 16:16:21 +100034#include "nouveau_drm.h"
35#include "nouveau_dma.h"
36#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100037#include "nouveau_connector.h"
38#include "nouveau_encoder.h"
39#include "nouveau_crtc.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100040#include "nouveau_fence.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100041#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100042
Ben Skeggs8a464382011-11-12 23:52:07 +100043#define EVO_DMA_NR 9
44
Ben Skeggsbdb8c212011-11-12 01:30:24 +100045#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100046#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100047#define EVO_OVLY(c) (0x05 + (c))
48#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100049#define EVO_CURS(c) (0x0d + (c))
50
Ben Skeggs816af2f2011-11-16 15:48:48 +100051/* offsets in shared sync bo of various structures */
52#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100053#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
54#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
55#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs816af2f2011-11-16 15:48:48 +100056
Ben Skeggsb5a794b2012-10-16 14:18:32 +100057/******************************************************************************
58 * EVO channel
59 *****************************************************************************/
60
Ben Skeggse225f442012-11-21 14:40:21 +100061struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +100062 struct nvif_object user;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100063};
64
65static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +100066nv50_chan_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +100067 void *data, u32 size, struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100068{
Ben Skeggs410f3ec2014-08-10 04:10:25 +100069 while (oclass[0]) {
70 int ret = nvif_object_init(disp, NULL, (oclass[0] << 16) | head,
71 oclass[0], data, size,
72 &chan->user);
Ben Skeggsb76f1522014-08-10 04:10:28 +100073 if (oclass++, ret == 0) {
74 nvif_object_map(&chan->user);
Ben Skeggs410f3ec2014-08-10 04:10:25 +100075 return ret;
Ben Skeggsb76f1522014-08-10 04:10:28 +100076 }
Ben Skeggs410f3ec2014-08-10 04:10:25 +100077 }
78 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100079}
80
81static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100082nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100083{
Ben Skeggs0ad72862014-08-10 04:10:22 +100084 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100085}
86
87/******************************************************************************
88 * PIO EVO channel
89 *****************************************************************************/
90
Ben Skeggse225f442012-11-21 14:40:21 +100091struct nv50_pioc {
92 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100093};
94
95static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100096nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100097{
Ben Skeggs0ad72862014-08-10 04:10:22 +100098 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100099}
100
101static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000102nv50_pioc_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +1000103 void *data, u32 size, struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000104{
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000105 return nv50_chan_create(disp, oclass, head, data, size, &pioc->base);
106}
107
108/******************************************************************************
109 * Cursor Immediate
110 *****************************************************************************/
111
112struct nv50_curs {
113 struct nv50_pioc base;
114};
115
116static int
117nv50_curs_create(struct nvif_object *disp, int head, struct nv50_curs *curs)
118{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000119 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000120 .head = head,
121 };
122 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000123 GK104_DISP_CURSOR,
124 GF110_DISP_CURSOR,
125 GT214_DISP_CURSOR,
126 G82_DISP_CURSOR,
127 NV50_DISP_CURSOR,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000128 0
129 };
130
131 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
132 &curs->base);
133}
134
135/******************************************************************************
136 * Overlay Immediate
137 *****************************************************************************/
138
139struct nv50_oimm {
140 struct nv50_pioc base;
141};
142
143static int
144nv50_oimm_create(struct nvif_object *disp, int head, struct nv50_oimm *oimm)
145{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000146 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000147 .head = head,
148 };
149 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000150 GK104_DISP_OVERLAY,
151 GF110_DISP_OVERLAY,
152 GT214_DISP_OVERLAY,
153 G82_DISP_OVERLAY,
154 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000155 0
156 };
157
158 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
159 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000160}
161
162/******************************************************************************
163 * DMA EVO channel
164 *****************************************************************************/
165
Ben Skeggse225f442012-11-21 14:40:21 +1000166struct nv50_dmac {
167 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000168 dma_addr_t handle;
169 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100170
Ben Skeggs0ad72862014-08-10 04:10:22 +1000171 struct nvif_object sync;
172 struct nvif_object vram;
173
Daniel Vetter59ad1462012-12-02 14:49:44 +0100174 /* Protects against concurrent pushbuf access to this channel, lock is
175 * grabbed by evo_wait (if the pushbuf reservation is successful) and
176 * dropped again by evo_kick. */
177 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000178};
179
180static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000181nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000182{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000183 nvif_object_fini(&dmac->vram);
184 nvif_object_fini(&dmac->sync);
185
186 nv50_chan_destroy(&dmac->base);
187
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000188 if (dmac->ptr) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000189 struct pci_dev *pdev = nvkm_device(nvif_device(disp))->pdev;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000190 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
191 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000192}
193
194static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000195nv50_dmac_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000196 void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000197 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000198{
Ben Skeggsf392ec42014-08-10 04:10:28 +1000199 struct nvif_device *device = nvif_device(disp);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000200 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000201 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000202 int ret;
203
Daniel Vetter59ad1462012-12-02 14:49:44 +0100204 mutex_init(&dmac->lock);
205
Ben Skeggsf392ec42014-08-10 04:10:28 +1000206 dmac->ptr = pci_alloc_consistent(nvkm_device(device)->pdev,
Ben Skeggs0ad72862014-08-10 04:10:22 +1000207 PAGE_SIZE, &dmac->handle);
Ben Skeggs47057302012-11-16 13:58:48 +1000208 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000209 return -ENOMEM;
210
Ben Skeggsf392ec42014-08-10 04:10:28 +1000211 ret = nvif_object_init(nvif_object(device), NULL,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000212 args->pushbuf, NV_DMA_FROM_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000213 &(struct nv_dma_v0) {
214 .target = NV_DMA_V0_TARGET_PCI_US,
215 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000216 .start = dmac->handle + 0x0000,
217 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000218 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000219 if (ret)
220 return ret;
221
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000222 ret = nv50_chan_create(disp, oclass, head, data, size, &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000223 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000224 if (ret)
225 return ret;
226
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000227 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000000,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000228 NV_DMA_IN_MEMORY,
229 &(struct nv_dma_v0) {
230 .target = NV_DMA_V0_TARGET_VRAM,
231 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000232 .start = syncbuf + 0x0000,
233 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000234 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000235 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000236 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000237 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000238
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000239 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000001,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000240 NV_DMA_IN_MEMORY,
241 &(struct nv_dma_v0) {
242 .target = NV_DMA_V0_TARGET_VRAM,
243 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000244 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000245 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000246 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000247 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000248 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000249 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000250
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000251 return ret;
252}
253
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000254/******************************************************************************
255 * Core
256 *****************************************************************************/
257
Ben Skeggse225f442012-11-21 14:40:21 +1000258struct nv50_mast {
259 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000260};
261
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000262static int
263nv50_core_create(struct nvif_object *disp, u64 syncbuf, struct nv50_mast *core)
264{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000265 struct nv50_disp_core_channel_dma_v0 args = {
266 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000267 };
268 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000269 GM107_DISP_CORE_CHANNEL_DMA,
270 GK110_DISP_CORE_CHANNEL_DMA,
271 GK104_DISP_CORE_CHANNEL_DMA,
272 GF110_DISP_CORE_CHANNEL_DMA,
273 GT214_DISP_CORE_CHANNEL_DMA,
274 GT206_DISP_CORE_CHANNEL_DMA,
275 GT200_DISP_CORE_CHANNEL_DMA,
276 G82_DISP_CORE_CHANNEL_DMA,
277 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000278 0
279 };
280
281 return nv50_dmac_create(disp, oclass, 0, &args, sizeof(args), syncbuf,
282 &core->base);
283}
284
285/******************************************************************************
286 * Base
287 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000288
Ben Skeggse225f442012-11-21 14:40:21 +1000289struct nv50_sync {
290 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000291 u32 addr;
292 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000293};
294
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000295static int
296nv50_base_create(struct nvif_object *disp, int head, u64 syncbuf,
297 struct nv50_sync *base)
298{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000299 struct nv50_disp_base_channel_dma_v0 args = {
300 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000301 .head = head,
302 };
303 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000304 GK110_DISP_BASE_CHANNEL_DMA,
305 GK104_DISP_BASE_CHANNEL_DMA,
306 GF110_DISP_BASE_CHANNEL_DMA,
307 GT214_DISP_BASE_CHANNEL_DMA,
308 GT200_DISP_BASE_CHANNEL_DMA,
309 G82_DISP_BASE_CHANNEL_DMA,
310 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000311 0
312 };
313
314 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
315 syncbuf, &base->base);
316}
317
318/******************************************************************************
319 * Overlay
320 *****************************************************************************/
321
Ben Skeggse225f442012-11-21 14:40:21 +1000322struct nv50_ovly {
323 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000324};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000325
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000326static int
327nv50_ovly_create(struct nvif_object *disp, int head, u64 syncbuf,
328 struct nv50_ovly *ovly)
329{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000330 struct nv50_disp_overlay_channel_dma_v0 args = {
331 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000332 .head = head,
333 };
334 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000335 GK104_DISP_OVERLAY_CONTROL_DMA,
336 GF110_DISP_OVERLAY_CONTROL_DMA,
337 GT214_DISP_OVERLAY_CHANNEL_DMA,
338 GT200_DISP_OVERLAY_CHANNEL_DMA,
339 G82_DISP_OVERLAY_CHANNEL_DMA,
340 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000341 0
342 };
343
344 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
345 syncbuf, &ovly->base);
346}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000347
Ben Skeggse225f442012-11-21 14:40:21 +1000348struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000349 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000350 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000351 struct nv50_curs curs;
352 struct nv50_sync sync;
353 struct nv50_ovly ovly;
354 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000355};
356
Ben Skeggse225f442012-11-21 14:40:21 +1000357#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
358#define nv50_curs(c) (&nv50_head(c)->curs)
359#define nv50_sync(c) (&nv50_head(c)->sync)
360#define nv50_ovly(c) (&nv50_head(c)->ovly)
361#define nv50_oimm(c) (&nv50_head(c)->oimm)
362#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000363#define nv50_vers(c) nv50_chan(c)->user.oclass
364
365struct nv50_fbdma {
366 struct list_head head;
367 struct nvif_object core;
368 struct nvif_object base[4];
369};
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000370
Ben Skeggse225f442012-11-21 14:40:21 +1000371struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000372 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000373 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000374
Ben Skeggs8a423642014-08-10 04:10:19 +1000375 struct list_head fbdma;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000376
377 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000378};
379
Ben Skeggse225f442012-11-21 14:40:21 +1000380static struct nv50_disp *
381nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000382{
Ben Skeggs77145f12012-07-31 16:16:21 +1000383 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000384}
385
Ben Skeggse225f442012-11-21 14:40:21 +1000386#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000387
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000388static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000389nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000390{
391 return nouveau_encoder(encoder)->crtc;
392}
393
394/******************************************************************************
395 * EVO channel helpers
396 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000397static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000398evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000399{
Ben Skeggse225f442012-11-21 14:40:21 +1000400 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000401 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000402
Daniel Vetter59ad1462012-12-02 14:49:44 +0100403 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000404 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000405 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000406
Ben Skeggs0ad72862014-08-10 04:10:22 +1000407 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
408 if (!nvkm_wait(&dmac->base.user, 0x0004, ~0, 0x00000000)) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100409 mutex_unlock(&dmac->lock);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000410 nv_error(nvkm_object(&dmac->base.user), "channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000411 return NULL;
412 }
413
414 put = 0;
415 }
416
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000417 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000418}
419
420static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000421evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000422{
Ben Skeggse225f442012-11-21 14:40:21 +1000423 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000424 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100425 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000426}
427
428#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
429#define evo_data(p,d) *((p)++) = (d)
430
Ben Skeggs3376ee32011-11-12 14:28:12 +1000431static bool
432evo_sync_wait(void *data)
433{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500434 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
435 return true;
436 usleep_range(1, 2);
437 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000438}
439
440static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000441evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000442{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000443 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000444 struct nv50_disp *disp = nv50_disp(dev);
445 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000446 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000447 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000448 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000449 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000450 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000451 evo_mthd(push, 0x0080, 2);
452 evo_data(push, 0x00000000);
453 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000454 evo_kick(push, mast);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000455 if (nv_wait_cb(nvkm_device(device), evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000456 return 0;
457 }
458
459 return -EBUSY;
460}
461
462/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000463 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000464 *****************************************************************************/
465struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000466nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000467{
Ben Skeggse225f442012-11-21 14:40:21 +1000468 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000469}
470
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000471struct nv50_display_flip {
472 struct nv50_disp *disp;
473 struct nv50_sync *chan;
474};
475
476static bool
477nv50_display_flip_wait(void *data)
478{
479 struct nv50_display_flip *flip = data;
480 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500481 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000482 return true;
483 usleep_range(1, 2);
484 return false;
485}
486
Ben Skeggs3376ee32011-11-12 14:28:12 +1000487void
Ben Skeggse225f442012-11-21 14:40:21 +1000488nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000489{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000490 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000491 struct nv50_display_flip flip = {
492 .disp = nv50_disp(crtc->dev),
493 .chan = nv50_sync(crtc),
494 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000495 u32 *push;
496
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000497 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000498 if (push) {
499 evo_mthd(push, 0x0084, 1);
500 evo_data(push, 0x00000000);
501 evo_mthd(push, 0x0094, 1);
502 evo_data(push, 0x00000000);
503 evo_mthd(push, 0x00c0, 1);
504 evo_data(push, 0x00000000);
505 evo_mthd(push, 0x0080, 1);
506 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000507 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000508 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000509
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000510 nv_wait_cb(nvkm_device(device), nv50_display_flip_wait, &flip);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000511}
512
513int
Ben Skeggse225f442012-11-21 14:40:21 +1000514nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000515 struct nouveau_channel *chan, u32 swap_interval)
516{
517 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000518 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000519 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000520 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000521 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000522 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000523
524 swap_interval <<= 4;
525 if (swap_interval == 0)
526 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000527 if (chan == NULL)
528 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000529
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000530 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000531 if (unlikely(push == NULL))
532 return -EBUSY;
533
Ben Skeggsbbf89062014-08-10 04:10:25 +1000534 if (chan && chan->object->oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000535 ret = RING_SPACE(chan, 8);
536 if (ret)
537 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000538
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000539 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000540 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000541 OUT_RING (chan, sync->addr ^ 0x10);
542 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
543 OUT_RING (chan, sync->data + 1);
544 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
545 OUT_RING (chan, sync->addr);
546 OUT_RING (chan, sync->data);
547 } else
Ben Skeggsbbf89062014-08-10 04:10:25 +1000548 if (chan && chan->object->oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000549 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000550 ret = RING_SPACE(chan, 12);
551 if (ret)
552 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000553
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000554 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000555 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000556 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
557 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
558 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
559 OUT_RING (chan, sync->data + 1);
560 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
561 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
562 OUT_RING (chan, upper_32_bits(addr));
563 OUT_RING (chan, lower_32_bits(addr));
564 OUT_RING (chan, sync->data);
565 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
566 } else
567 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000568 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000569 ret = RING_SPACE(chan, 10);
570 if (ret)
571 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000572
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000573 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
574 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
575 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
576 OUT_RING (chan, sync->data + 1);
577 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
578 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
579 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
580 OUT_RING (chan, upper_32_bits(addr));
581 OUT_RING (chan, lower_32_bits(addr));
582 OUT_RING (chan, sync->data);
583 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
584 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
585 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500586
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000587 if (chan) {
588 sync->addr ^= 0x10;
589 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000590 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000591 }
592
593 /* queue the flip */
594 evo_mthd(push, 0x0100, 1);
595 evo_data(push, 0xfffe0000);
596 evo_mthd(push, 0x0084, 1);
597 evo_data(push, swap_interval);
598 if (!(swap_interval & 0x00000100)) {
599 evo_mthd(push, 0x00e0, 1);
600 evo_data(push, 0x40000000);
601 }
602 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000603 evo_data(push, sync->addr);
604 evo_data(push, sync->data++);
605 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000606 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000607 evo_mthd(push, 0x00a0, 2);
608 evo_data(push, 0x00000000);
609 evo_data(push, 0x00000000);
610 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000611 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000612 evo_mthd(push, 0x0110, 2);
613 evo_data(push, 0x00000000);
614 evo_data(push, 0x00000000);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000615 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000616 evo_mthd(push, 0x0800, 5);
617 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
618 evo_data(push, 0);
619 evo_data(push, (fb->height << 16) | fb->width);
620 evo_data(push, nv_fb->r_pitch);
621 evo_data(push, nv_fb->r_format);
622 } else {
623 evo_mthd(push, 0x0400, 5);
624 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
625 evo_data(push, 0);
626 evo_data(push, (fb->height << 16) | fb->width);
627 evo_data(push, nv_fb->r_pitch);
628 evo_data(push, nv_fb->r_format);
629 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000630 evo_mthd(push, 0x0080, 1);
631 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000632 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000633
634 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000635 return 0;
636}
637
Ben Skeggs26f6d882011-07-04 16:25:18 +1000638/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000639 * CRTC
640 *****************************************************************************/
641static int
Ben Skeggse225f442012-11-21 14:40:21 +1000642nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000643{
Ben Skeggse225f442012-11-21 14:40:21 +1000644 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000645 struct nouveau_connector *nv_connector;
646 struct drm_connector *connector;
647 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000648
Ben Skeggs488ff202011-10-17 10:38:10 +1000649 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000650 connector = &nv_connector->base;
651 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -0700652 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +1000653 mode = DITHERING_MODE_DYNAMIC2X2;
654 } else {
655 mode = nv_connector->dithering_mode;
656 }
657
658 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
659 if (connector->display_info.bpc >= 8)
660 mode |= DITHERING_DEPTH_8BPC;
661 } else {
662 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000663 }
664
Ben Skeggsde8268c2012-11-16 10:24:31 +1000665 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000666 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000667 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000668 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
669 evo_data(push, mode);
670 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000671 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000672 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
673 evo_data(push, mode);
674 } else {
675 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
676 evo_data(push, mode);
677 }
678
Ben Skeggs438d99e2011-07-05 16:48:06 +1000679 if (update) {
680 evo_mthd(push, 0x0080, 1);
681 evo_data(push, 0x00000000);
682 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000683 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000684 }
685
686 return 0;
687}
688
689static int
Ben Skeggse225f442012-11-21 14:40:21 +1000690nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000691{
Ben Skeggse225f442012-11-21 14:40:21 +1000692 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000693 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000694 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000695 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000696 int mode = DRM_MODE_SCALE_NONE;
697 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000698
Ben Skeggs92854622011-11-11 23:49:06 +1000699 /* start off at the resolution we programmed the crtc for, this
700 * effectively handles NONE/FULL scaling
701 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000702 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000703 if (nv_connector && nv_connector->native_mode)
704 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000705
Ben Skeggs92854622011-11-11 23:49:06 +1000706 if (mode != DRM_MODE_SCALE_NONE)
707 omode = nv_connector->native_mode;
708 else
709 omode = umode;
710
711 oX = omode->hdisplay;
712 oY = omode->vdisplay;
713 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
714 oY *= 2;
715
716 /* add overscan compensation if necessary, will keep the aspect
717 * ratio the same as the backend mode unless overridden by the
718 * user setting both hborder and vborder properties.
719 */
720 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
721 (nv_connector->underscan == UNDERSCAN_AUTO &&
722 nv_connector->edid &&
723 drm_detect_hdmi_monitor(nv_connector->edid)))) {
724 u32 bX = nv_connector->underscan_hborder;
725 u32 bY = nv_connector->underscan_vborder;
726 u32 aspect = (oY << 19) / oX;
727
728 if (bX) {
729 oX -= (bX * 2);
730 if (bY) oY -= (bY * 2);
731 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
732 } else {
733 oX -= (oX >> 4) + 32;
734 if (bY) oY -= (bY * 2);
735 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000736 }
737 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000738
Ben Skeggs92854622011-11-11 23:49:06 +1000739 /* handle CENTER/ASPECT scaling, taking into account the areas
740 * removed already for overscan compensation
741 */
742 switch (mode) {
743 case DRM_MODE_SCALE_CENTER:
744 oX = min((u32)umode->hdisplay, oX);
745 oY = min((u32)umode->vdisplay, oY);
746 /* fall-through */
747 case DRM_MODE_SCALE_ASPECT:
748 if (oY < oX) {
749 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
750 oX = ((oY * aspect) + (aspect / 2)) >> 19;
751 } else {
752 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
753 oY = ((oX * aspect) + (aspect / 2)) >> 19;
754 }
755 break;
756 default:
757 break;
758 }
759
Ben Skeggsde8268c2012-11-16 10:24:31 +1000760 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000761 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000762 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000763 /*XXX: SCALE_CTRL_ACTIVE??? */
764 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
765 evo_data(push, (oY << 16) | oX);
766 evo_data(push, (oY << 16) | oX);
767 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
768 evo_data(push, 0x00000000);
769 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
770 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
771 } else {
772 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
773 evo_data(push, (oY << 16) | oX);
774 evo_data(push, (oY << 16) | oX);
775 evo_data(push, (oY << 16) | oX);
776 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
777 evo_data(push, 0x00000000);
778 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
779 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
780 }
781
782 evo_kick(push, mast);
783
Ben Skeggs3376ee32011-11-12 14:28:12 +1000784 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000785 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700786 nv50_display_flip_next(crtc, crtc->primary->fb,
787 NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000788 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000789 }
790
791 return 0;
792}
793
794static int
Ben Skeggse225f442012-11-21 14:40:21 +1000795nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000796{
Ben Skeggse225f442012-11-21 14:40:21 +1000797 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000798 u32 *push, hue, vib;
799 int adj;
800
801 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
802 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
803 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
804
805 push = evo_wait(mast, 16);
806 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000807 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000808 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
809 evo_data(push, (hue << 20) | (vib << 8));
810 } else {
811 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
812 evo_data(push, (hue << 20) | (vib << 8));
813 }
814
815 if (update) {
816 evo_mthd(push, 0x0080, 1);
817 evo_data(push, 0x00000000);
818 }
819 evo_kick(push, mast);
820 }
821
822 return 0;
823}
824
825static int
Ben Skeggse225f442012-11-21 14:40:21 +1000826nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000827 int x, int y, bool update)
828{
829 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000830 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000831 u32 *push;
832
Ben Skeggsde8268c2012-11-16 10:24:31 +1000833 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000834 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000835 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000836 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
837 evo_data(push, nvfb->nvbo->bo.offset >> 8);
838 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
839 evo_data(push, (fb->height << 16) | fb->width);
840 evo_data(push, nvfb->r_pitch);
841 evo_data(push, nvfb->r_format);
842 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
843 evo_data(push, (y << 16) | x);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000844 if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000845 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000846 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000847 }
848 } else {
849 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
850 evo_data(push, nvfb->nvbo->bo.offset >> 8);
851 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
852 evo_data(push, (fb->height << 16) | fb->width);
853 evo_data(push, nvfb->r_pitch);
854 evo_data(push, nvfb->r_format);
Ben Skeggs8a423642014-08-10 04:10:19 +1000855 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000856 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
857 evo_data(push, (y << 16) | x);
858 }
859
Ben Skeggsa46232e2011-07-07 15:23:48 +1000860 if (update) {
861 evo_mthd(push, 0x0080, 1);
862 evo_data(push, 0x00000000);
863 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000864 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000865 }
866
Ben Skeggs8a423642014-08-10 04:10:19 +1000867 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000868 return 0;
869}
870
871static void
Ben Skeggse225f442012-11-21 14:40:21 +1000872nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000873{
Ben Skeggse225f442012-11-21 14:40:21 +1000874 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000875 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000876 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000877 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000878 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
879 evo_data(push, 0x85000000);
880 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
881 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000882 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000883 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
884 evo_data(push, 0x85000000);
885 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
886 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000887 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000888 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000889 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
890 evo_data(push, 0x85000000);
891 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
892 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000893 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000894 }
895 evo_kick(push, mast);
896 }
897}
898
899static void
Ben Skeggse225f442012-11-21 14:40:21 +1000900nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000901{
Ben Skeggse225f442012-11-21 14:40:21 +1000902 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000903 u32 *push = evo_wait(mast, 16);
904 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000905 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000906 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
907 evo_data(push, 0x05000000);
908 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000909 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000910 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
911 evo_data(push, 0x05000000);
912 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
913 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000914 } else {
915 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
916 evo_data(push, 0x05000000);
917 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
918 evo_data(push, 0x00000000);
919 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000920 evo_kick(push, mast);
921 }
922}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000923
Ben Skeggsde8268c2012-11-16 10:24:31 +1000924static void
Ben Skeggse225f442012-11-21 14:40:21 +1000925nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000926{
Ben Skeggse225f442012-11-21 14:40:21 +1000927 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000928
929 if (show)
Ben Skeggse225f442012-11-21 14:40:21 +1000930 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000931 else
Ben Skeggse225f442012-11-21 14:40:21 +1000932 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000933
934 if (update) {
935 u32 *push = evo_wait(mast, 2);
936 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000937 evo_mthd(push, 0x0080, 1);
938 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000939 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000940 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000941 }
942}
943
944static void
Ben Skeggse225f442012-11-21 14:40:21 +1000945nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000946{
947}
948
949static void
Ben Skeggse225f442012-11-21 14:40:21 +1000950nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000951{
952 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000953 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000954 u32 *push;
955
Ben Skeggse225f442012-11-21 14:40:21 +1000956 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000957
Ben Skeggs56d237d2014-05-19 14:54:33 +1000958 push = evo_wait(mast, 6);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000959 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000960 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000961 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
962 evo_data(push, 0x00000000);
963 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
964 evo_data(push, 0x40000000);
965 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000966 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000967 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
968 evo_data(push, 0x00000000);
969 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
970 evo_data(push, 0x40000000);
971 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
972 evo_data(push, 0x00000000);
973 } else {
974 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
975 evo_data(push, 0x00000000);
976 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
977 evo_data(push, 0x03000000);
978 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
979 evo_data(push, 0x00000000);
980 }
981
982 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000983 }
984
Ben Skeggse225f442012-11-21 14:40:21 +1000985 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000986}
987
988static void
Ben Skeggse225f442012-11-21 14:40:21 +1000989nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000990{
991 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000992 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000993 u32 *push;
994
Ben Skeggsde8268c2012-11-16 10:24:31 +1000995 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000996 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000997 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000998 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000999 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001000 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1001 evo_data(push, 0xc0000000);
1002 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1003 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001004 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001005 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001006 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001007 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1008 evo_data(push, 0xc0000000);
1009 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1010 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001011 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001012 } else {
1013 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001014 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001015 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1016 evo_data(push, 0x83000000);
1017 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1018 evo_data(push, 0x00000000);
1019 evo_data(push, 0x00000000);
1020 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001021 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001022 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1023 evo_data(push, 0xffffff00);
1024 }
1025
1026 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001027 }
1028
Ben Skeggse225f442012-11-21 14:40:21 +10001029 nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
Matt Roperf4510a22014-04-01 15:22:40 -07001030 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001031}
1032
1033static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001034nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001035 struct drm_display_mode *adjusted_mode)
1036{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001037 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001038 return true;
1039}
1040
1041static int
Ben Skeggse225f442012-11-21 14:40:21 +10001042nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001043{
Matt Roperf4510a22014-04-01 15:22:40 -07001044 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001045 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001046 int ret;
1047
1048 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001049 if (ret == 0) {
1050 if (head->image)
1051 nouveau_bo_unpin(head->image);
1052 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001053 }
1054
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001055 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001056}
1057
1058static int
Ben Skeggse225f442012-11-21 14:40:21 +10001059nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001060 struct drm_display_mode *mode, int x, int y,
1061 struct drm_framebuffer *old_fb)
1062{
Ben Skeggse225f442012-11-21 14:40:21 +10001063 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001064 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1065 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001066 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1067 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1068 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1069 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
Roy Spliet1dce6262014-09-12 18:00:13 +02001070 u32 vblan2e = 0, vblan2s = 1, vblankus = 0;
Ben Skeggs3488c572012-03-12 11:42:20 +10001071 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001072 int ret;
1073
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001074 hactive = mode->htotal;
1075 hsynce = mode->hsync_end - mode->hsync_start - 1;
1076 hbackp = mode->htotal - mode->hsync_end;
1077 hblanke = hsynce + hbackp;
1078 hfrontp = mode->hsync_start - mode->hdisplay;
1079 hblanks = mode->htotal - hfrontp - 1;
1080
1081 vactive = mode->vtotal * vscan / ilace;
1082 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1083 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1084 vblanke = vsynce + vbackp;
1085 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1086 vblanks = vactive - vfrontp - 1;
Roy Spliet1dce6262014-09-12 18:00:13 +02001087 /* XXX: Safe underestimate, even "0" works */
1088 vblankus = (vactive - mode->vdisplay - 2) * hactive;
1089 vblankus *= 1000;
1090 vblankus /= mode->clock;
1091
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001092 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1093 vblan2e = vactive + vsynce + vbackp;
1094 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1095 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001096 }
1097
Ben Skeggse225f442012-11-21 14:40:21 +10001098 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001099 if (ret)
1100 return ret;
1101
Ben Skeggsde8268c2012-11-16 10:24:31 +10001102 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001103 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001104 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001105 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1106 evo_data(push, 0x00800000 | mode->clock);
1107 evo_data(push, (ilace == 2) ? 2 : 0);
Roy Spliet1dce6262014-09-12 18:00:13 +02001108 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001109 evo_data(push, 0x00000000);
1110 evo_data(push, (vactive << 16) | hactive);
1111 evo_data(push, ( vsynce << 16) | hsynce);
1112 evo_data(push, (vblanke << 16) | hblanke);
1113 evo_data(push, (vblanks << 16) | hblanks);
1114 evo_data(push, (vblan2e << 16) | vblan2s);
Roy Spliet1dce6262014-09-12 18:00:13 +02001115 evo_data(push, vblankus);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001116 evo_data(push, 0x00000000);
1117 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1118 evo_data(push, 0x00000311);
1119 evo_data(push, 0x00000100);
1120 } else {
1121 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1122 evo_data(push, 0x00000000);
1123 evo_data(push, (vactive << 16) | hactive);
1124 evo_data(push, ( vsynce << 16) | hsynce);
1125 evo_data(push, (vblanke << 16) | hblanke);
1126 evo_data(push, (vblanks << 16) | hblanks);
1127 evo_data(push, (vblan2e << 16) | vblan2s);
1128 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1129 evo_data(push, 0x00000000); /* ??? */
1130 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1131 evo_data(push, mode->clock * 1000);
1132 evo_data(push, 0x00200000); /* ??? */
1133 evo_data(push, mode->clock * 1000);
1134 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1135 evo_data(push, 0x00000311);
1136 evo_data(push, 0x00000100);
1137 }
1138
1139 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001140 }
1141
1142 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001143 nv50_crtc_set_dither(nv_crtc, false);
1144 nv50_crtc_set_scale(nv_crtc, false);
1145 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001146 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001147 return 0;
1148}
1149
1150static int
Ben Skeggse225f442012-11-21 14:40:21 +10001151nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001152 struct drm_framebuffer *old_fb)
1153{
Ben Skeggs77145f12012-07-31 16:16:21 +10001154 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001155 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1156 int ret;
1157
Matt Roperf4510a22014-04-01 15:22:40 -07001158 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001159 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001160 return 0;
1161 }
1162
Ben Skeggse225f442012-11-21 14:40:21 +10001163 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001164 if (ret)
1165 return ret;
1166
Ben Skeggse225f442012-11-21 14:40:21 +10001167 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001168 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1169 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001170 return 0;
1171}
1172
1173static int
Ben Skeggse225f442012-11-21 14:40:21 +10001174nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001175 struct drm_framebuffer *fb, int x, int y,
1176 enum mode_set_atomic state)
1177{
1178 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001179 nv50_display_flip_stop(crtc);
1180 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001181 return 0;
1182}
1183
1184static void
Ben Skeggse225f442012-11-21 14:40:21 +10001185nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001186{
Ben Skeggse225f442012-11-21 14:40:21 +10001187 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001188 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1189 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1190 int i;
1191
1192 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001193 u16 r = nv_crtc->lut.r[i] >> 2;
1194 u16 g = nv_crtc->lut.g[i] >> 2;
1195 u16 b = nv_crtc->lut.b[i] >> 2;
1196
Ben Skeggs648d4df2014-08-10 04:10:27 +10001197 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001198 writew(r + 0x0000, lut + (i * 0x08) + 0);
1199 writew(g + 0x0000, lut + (i * 0x08) + 2);
1200 writew(b + 0x0000, lut + (i * 0x08) + 4);
1201 } else {
1202 writew(r + 0x6000, lut + (i * 0x20) + 0);
1203 writew(g + 0x6000, lut + (i * 0x20) + 2);
1204 writew(b + 0x6000, lut + (i * 0x20) + 4);
1205 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001206 }
1207}
1208
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001209static void
1210nv50_crtc_disable(struct drm_crtc *crtc)
1211{
1212 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001213 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001214 if (head->image)
1215 nouveau_bo_unpin(head->image);
1216 nouveau_bo_ref(NULL, &head->image);
1217}
1218
Ben Skeggs438d99e2011-07-05 16:48:06 +10001219static int
Ben Skeggse225f442012-11-21 14:40:21 +10001220nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001221 uint32_t handle, uint32_t width, uint32_t height)
1222{
1223 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1224 struct drm_device *dev = crtc->dev;
1225 struct drm_gem_object *gem;
1226 struct nouveau_bo *nvbo;
1227 bool visible = (handle != 0);
1228 int i, ret = 0;
1229
1230 if (visible) {
1231 if (width != 64 || height != 64)
1232 return -EINVAL;
1233
1234 gem = drm_gem_object_lookup(dev, file_priv, handle);
1235 if (unlikely(!gem))
1236 return -ENOENT;
1237 nvbo = nouveau_gem_object(gem);
1238
1239 ret = nouveau_bo_map(nvbo);
1240 if (ret == 0) {
1241 for (i = 0; i < 64 * 64; i++) {
1242 u32 v = nouveau_bo_rd32(nvbo, i);
1243 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1244 }
1245 nouveau_bo_unmap(nvbo);
1246 }
1247
1248 drm_gem_object_unreference_unlocked(gem);
1249 }
1250
1251 if (visible != nv_crtc->cursor.visible) {
Ben Skeggse225f442012-11-21 14:40:21 +10001252 nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001253 nv_crtc->cursor.visible = visible;
1254 }
1255
1256 return ret;
1257}
1258
1259static int
Ben Skeggse225f442012-11-21 14:40:21 +10001260nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001261{
Ben Skeggse225f442012-11-21 14:40:21 +10001262 struct nv50_curs *curs = nv50_curs(crtc);
1263 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001264 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1265 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001266 return 0;
1267}
1268
1269static void
Ben Skeggse225f442012-11-21 14:40:21 +10001270nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001271 uint32_t start, uint32_t size)
1272{
1273 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Dan Carpenterbdefc8c2013-11-28 01:18:47 +03001274 u32 end = min_t(u32, start + size, 256);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001275 u32 i;
1276
1277 for (i = start; i < end; i++) {
1278 nv_crtc->lut.r[i] = r[i];
1279 nv_crtc->lut.g[i] = g[i];
1280 nv_crtc->lut.b[i] = b[i];
1281 }
1282
Ben Skeggse225f442012-11-21 14:40:21 +10001283 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001284}
1285
1286static void
Ben Skeggse225f442012-11-21 14:40:21 +10001287nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001288{
1289 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001290 struct nv50_disp *disp = nv50_disp(crtc->dev);
1291 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001292 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001293
Ben Skeggs0ad72862014-08-10 04:10:22 +10001294 list_for_each_entry(fbdma, &disp->fbdma, head) {
1295 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1296 }
1297
1298 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1299 nv50_pioc_destroy(&head->oimm.base);
1300 nv50_dmac_destroy(&head->sync.base, disp->disp);
1301 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001302
1303 /*XXX: this shouldn't be necessary, but the core doesn't call
1304 * disconnect() during the cleanup paths
1305 */
1306 if (head->image)
1307 nouveau_bo_unpin(head->image);
1308 nouveau_bo_ref(NULL, &head->image);
1309
Ben Skeggs438d99e2011-07-05 16:48:06 +10001310 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001311 if (nv_crtc->cursor.nvbo)
1312 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001313 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001314
Ben Skeggs438d99e2011-07-05 16:48:06 +10001315 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001316 if (nv_crtc->lut.nvbo)
1317 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001318 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001319
Ben Skeggs438d99e2011-07-05 16:48:06 +10001320 drm_crtc_cleanup(crtc);
1321 kfree(crtc);
1322}
1323
Ben Skeggse225f442012-11-21 14:40:21 +10001324static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1325 .dpms = nv50_crtc_dpms,
1326 .prepare = nv50_crtc_prepare,
1327 .commit = nv50_crtc_commit,
1328 .mode_fixup = nv50_crtc_mode_fixup,
1329 .mode_set = nv50_crtc_mode_set,
1330 .mode_set_base = nv50_crtc_mode_set_base,
1331 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1332 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001333 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001334};
1335
Ben Skeggse225f442012-11-21 14:40:21 +10001336static const struct drm_crtc_funcs nv50_crtc_func = {
1337 .cursor_set = nv50_crtc_cursor_set,
1338 .cursor_move = nv50_crtc_cursor_move,
1339 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001340 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001341 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001342 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001343};
1344
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001345static void
Ben Skeggse225f442012-11-21 14:40:21 +10001346nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001347{
1348}
1349
1350static void
Ben Skeggse225f442012-11-21 14:40:21 +10001351nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001352{
1353}
1354
Ben Skeggs438d99e2011-07-05 16:48:06 +10001355static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001356nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001357{
Ben Skeggse225f442012-11-21 14:40:21 +10001358 struct nv50_disp *disp = nv50_disp(dev);
1359 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001360 struct drm_crtc *crtc;
1361 int ret, i;
1362
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001363 head = kzalloc(sizeof(*head), GFP_KERNEL);
1364 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001365 return -ENOMEM;
1366
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001367 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001368 head->base.set_dither = nv50_crtc_set_dither;
1369 head->base.set_scale = nv50_crtc_set_scale;
1370 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001371 head->base.color_vibrance = 50;
1372 head->base.vibrant_hue = 0;
Ben Skeggse225f442012-11-21 14:40:21 +10001373 head->base.cursor.set_offset = nv50_cursor_set_offset;
1374 head->base.cursor.set_pos = nv50_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001375 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001376 head->base.lut.r[i] = i << 8;
1377 head->base.lut.g[i] = i << 8;
1378 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001379 }
1380
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001381 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001382 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1383 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001384 drm_mode_crtc_set_gamma_size(crtc, 256);
1385
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001386 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01001387 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001388 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001389 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001390 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001391 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001392 if (ret)
1393 nouveau_bo_unpin(head->base.lut.nvbo);
1394 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001395 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001396 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001397 }
1398
1399 if (ret)
1400 goto out;
1401
Ben Skeggse225f442012-11-21 14:40:21 +10001402 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001403
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001404 /* allocate cursor resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001405 ret = nv50_curs_create(disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001406 if (ret)
1407 goto out;
1408
1409 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01001410 0, 0x0000, NULL, NULL, &head->base.cursor.nvbo);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001411 if (!ret) {
1412 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001413 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001414 ret = nouveau_bo_map(head->base.cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001415 if (ret)
1416 nouveau_bo_unpin(head->base.lut.nvbo);
1417 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001418 if (ret)
1419 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
1420 }
1421
1422 if (ret)
1423 goto out;
1424
1425 /* allocate page flip / sync resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001426 ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
1427 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001428 if (ret)
1429 goto out;
1430
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001431 head->sync.addr = EVO_FLIP_SEM0(index);
1432 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001433
1434 /* allocate overlay resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001435 ret = nv50_oimm_create(disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001436 if (ret)
1437 goto out;
1438
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001439 ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
1440 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001441 if (ret)
1442 goto out;
1443
Ben Skeggs438d99e2011-07-05 16:48:06 +10001444out:
1445 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001446 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001447 return ret;
1448}
1449
1450/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001451 * DAC
1452 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001453static void
Ben Skeggse225f442012-11-21 14:40:21 +10001454nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001455{
1456 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001457 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001458 struct {
1459 struct nv50_disp_mthd_v1 base;
1460 struct nv50_disp_dac_pwr_v0 pwr;
1461 } args = {
1462 .base.version = 1,
1463 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1464 .base.hasht = nv_encoder->dcb->hasht,
1465 .base.hashm = nv_encoder->dcb->hashm,
1466 .pwr.state = 1,
1467 .pwr.data = 1,
1468 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1469 mode != DRM_MODE_DPMS_OFF),
1470 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1471 mode != DRM_MODE_DPMS_OFF),
1472 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001473
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001474 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001475}
1476
1477static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001478nv50_dac_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001479 const struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001480 struct drm_display_mode *adjusted_mode)
1481{
1482 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1483 struct nouveau_connector *nv_connector;
1484
1485 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1486 if (nv_connector && nv_connector->native_mode) {
1487 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1488 int id = adjusted_mode->base.id;
1489 *adjusted_mode = *nv_connector->native_mode;
1490 adjusted_mode->base.id = id;
1491 }
1492 }
1493
1494 return true;
1495}
1496
1497static void
Ben Skeggse225f442012-11-21 14:40:21 +10001498nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001499{
1500}
1501
1502static void
Ben Skeggse225f442012-11-21 14:40:21 +10001503nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001504 struct drm_display_mode *adjusted_mode)
1505{
Ben Skeggse225f442012-11-21 14:40:21 +10001506 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001507 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1508 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001509 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001510
Ben Skeggse225f442012-11-21 14:40:21 +10001511 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001512
Ben Skeggs97b19b52012-11-16 11:21:37 +10001513 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001514 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001515 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001516 u32 syncs = 0x00000000;
1517
1518 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1519 syncs |= 0x00000001;
1520 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1521 syncs |= 0x00000002;
1522
1523 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1524 evo_data(push, 1 << nv_crtc->index);
1525 evo_data(push, syncs);
1526 } else {
1527 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1528 u32 syncs = 0x00000001;
1529
1530 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1531 syncs |= 0x00000008;
1532 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1533 syncs |= 0x00000010;
1534
1535 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1536 magic |= 0x00000001;
1537
1538 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1539 evo_data(push, syncs);
1540 evo_data(push, magic);
1541 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1542 evo_data(push, 1 << nv_crtc->index);
1543 }
1544
1545 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001546 }
1547
1548 nv_encoder->crtc = encoder->crtc;
1549}
1550
1551static void
Ben Skeggse225f442012-11-21 14:40:21 +10001552nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001553{
1554 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001555 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001556 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001557 u32 *push;
1558
1559 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001560 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001561
Ben Skeggs97b19b52012-11-16 11:21:37 +10001562 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001563 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001564 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001565 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1566 evo_data(push, 0x00000000);
1567 } else {
1568 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1569 evo_data(push, 0x00000000);
1570 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001571 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001572 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001573 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001574
1575 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001576}
1577
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001578static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001579nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001580{
Ben Skeggsc4abd312014-08-10 04:10:26 +10001581 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001582 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10001583 struct {
1584 struct nv50_disp_mthd_v1 base;
1585 struct nv50_disp_dac_load_v0 load;
1586 } args = {
1587 .base.version = 1,
1588 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
1589 .base.hasht = nv_encoder->dcb->hasht,
1590 .base.hashm = nv_encoder->dcb->hashm,
1591 };
1592 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10001593
Ben Skeggsc4abd312014-08-10 04:10:26 +10001594 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
1595 if (args.load.data == 0)
1596 args.load.data = 340;
1597
1598 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
1599 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001600 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001601
Ben Skeggs35b21d32012-11-08 12:08:55 +10001602 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001603}
1604
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001605static void
Ben Skeggse225f442012-11-21 14:40:21 +10001606nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001607{
1608 drm_encoder_cleanup(encoder);
1609 kfree(encoder);
1610}
1611
Ben Skeggse225f442012-11-21 14:40:21 +10001612static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1613 .dpms = nv50_dac_dpms,
1614 .mode_fixup = nv50_dac_mode_fixup,
1615 .prepare = nv50_dac_disconnect,
1616 .commit = nv50_dac_commit,
1617 .mode_set = nv50_dac_mode_set,
1618 .disable = nv50_dac_disconnect,
1619 .get_crtc = nv50_display_crtc_get,
1620 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001621};
1622
Ben Skeggse225f442012-11-21 14:40:21 +10001623static const struct drm_encoder_funcs nv50_dac_func = {
1624 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001625};
1626
1627static int
Ben Skeggse225f442012-11-21 14:40:21 +10001628nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001629{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001630 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001631 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001632 struct nouveau_encoder *nv_encoder;
1633 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001634 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001635
1636 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1637 if (!nv_encoder)
1638 return -ENOMEM;
1639 nv_encoder->dcb = dcbe;
1640 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001641 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001642
1643 encoder = to_drm_encoder(nv_encoder);
1644 encoder->possible_crtcs = dcbe->heads;
1645 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001646 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001647 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001648
1649 drm_mode_connector_attach_encoder(connector, encoder);
1650 return 0;
1651}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001652
1653/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001654 * Audio
1655 *****************************************************************************/
1656static void
Ben Skeggse225f442012-11-21 14:40:21 +10001657nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001658{
1659 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10001660 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10001661 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001662 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10001663 struct __packed {
1664 struct {
1665 struct nv50_disp_mthd_v1 mthd;
1666 struct nv50_disp_sor_hda_eld_v0 eld;
1667 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10001668 u8 data[sizeof(nv_connector->base.eld)];
1669 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10001670 .base.mthd.version = 1,
1671 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1672 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10001673 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1674 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10001675 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001676
1677 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1678 if (!drm_detect_monitor_audio(nv_connector->edid))
1679 return;
1680
Ben Skeggs78951d22011-11-11 18:13:13 +10001681 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001682 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001683
Ben Skeggsd889c522014-09-15 21:11:51 +10001684 nvif_mthd(disp->disp, 0, &args, sizeof(args.base) + args.data[2] * 4);
Ben Skeggs78951d22011-11-11 18:13:13 +10001685}
1686
1687static void
Ben Skeggscc2a9072014-09-15 21:29:05 +10001688nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001689{
1690 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001691 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001692 struct {
1693 struct nv50_disp_mthd_v1 base;
1694 struct nv50_disp_sor_hda_eld_v0 eld;
1695 } args = {
1696 .base.version = 1,
1697 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1698 .base.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10001699 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1700 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10001701 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001702
Ben Skeggs120b0c32014-08-10 04:10:26 +10001703 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001704}
1705
1706/******************************************************************************
1707 * HDMI
1708 *****************************************************************************/
1709static void
Ben Skeggse225f442012-11-21 14:40:21 +10001710nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001711{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001712 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1713 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001714 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001715 struct {
1716 struct nv50_disp_mthd_v1 base;
1717 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1718 } args = {
1719 .base.version = 1,
1720 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1721 .base.hasht = nv_encoder->dcb->hasht,
1722 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1723 (0x0100 << nv_crtc->index),
1724 .pwr.state = 1,
1725 .pwr.rekey = 56, /* binary driver, and tegra, constant */
1726 };
1727 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001728 u32 max_ac_packet;
1729
1730 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1731 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1732 return;
1733
1734 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10001735 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001736 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10001737 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001738
Ben Skeggse00f2232014-08-10 04:10:26 +10001739 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10001740 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001741}
1742
1743static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001744nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001745{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001746 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001747 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001748 struct {
1749 struct nv50_disp_mthd_v1 base;
1750 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1751 } args = {
1752 .base.version = 1,
1753 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1754 .base.hasht = nv_encoder->dcb->hasht,
1755 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1756 (0x0100 << nv_crtc->index),
1757 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001758
Ben Skeggse00f2232014-08-10 04:10:26 +10001759 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001760}
1761
1762/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001763 * SOR
1764 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001765static void
Ben Skeggse225f442012-11-21 14:40:21 +10001766nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001767{
1768 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001769 struct nv50_disp *disp = nv50_disp(encoder->dev);
1770 struct {
1771 struct nv50_disp_mthd_v1 base;
1772 struct nv50_disp_sor_pwr_v0 pwr;
1773 } args = {
1774 .base.version = 1,
1775 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
1776 .base.hasht = nv_encoder->dcb->hasht,
1777 .base.hashm = nv_encoder->dcb->hashm,
1778 .pwr.state = mode == DRM_MODE_DPMS_ON,
1779 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001780 struct {
1781 struct nv50_disp_mthd_v1 base;
1782 struct nv50_disp_sor_dp_pwr_v0 pwr;
1783 } link = {
1784 .base.version = 1,
1785 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
1786 .base.hasht = nv_encoder->dcb->hasht,
1787 .base.hashm = nv_encoder->dcb->hashm,
1788 .pwr.state = mode == DRM_MODE_DPMS_ON,
1789 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10001790 struct drm_device *dev = encoder->dev;
1791 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001792
1793 nv_encoder->last_dpms = mode;
1794
1795 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1796 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1797
1798 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1799 continue;
1800
1801 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001802 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001803 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1804 return;
1805 break;
1806 }
1807 }
1808
Ben Skeggs48743222014-05-31 01:48:06 +10001809 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001810 args.pwr.state = 1;
1811 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001812 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10001813 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001814 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10001815 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001816}
1817
1818static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001819nv50_sor_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001820 const struct drm_display_mode *mode,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001821 struct drm_display_mode *adjusted_mode)
1822{
1823 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1824 struct nouveau_connector *nv_connector;
1825
1826 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1827 if (nv_connector && nv_connector->native_mode) {
1828 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1829 int id = adjusted_mode->base.id;
1830 *adjusted_mode = *nv_connector->native_mode;
1831 adjusted_mode->base.id = id;
1832 }
1833 }
1834
1835 return true;
1836}
1837
1838static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001839nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
1840{
1841 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
1842 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
1843 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001844 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001845 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
1846 evo_data(push, (nv_encoder->ctrl = temp));
1847 } else {
1848 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1849 evo_data(push, (nv_encoder->ctrl = temp));
1850 }
1851 evo_kick(push, mast);
1852 }
1853}
1854
1855static void
Ben Skeggse225f442012-11-21 14:40:21 +10001856nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001857{
1858 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001859 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001860
1861 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1862 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001863
1864 if (nv_crtc) {
1865 nv50_crtc_prepare(&nv_crtc->base);
1866 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggscc2a9072014-09-15 21:29:05 +10001867 nv50_audio_disconnect(encoder, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001868 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
1869 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001870}
1871
1872static void
Ben Skeggse225f442012-11-21 14:40:21 +10001873nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001874{
1875}
1876
1877static void
Ben Skeggse225f442012-11-21 14:40:21 +10001878nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001879 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001880{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001881 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1882 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1883 struct {
1884 struct nv50_disp_mthd_v1 base;
1885 struct nv50_disp_sor_lvds_script_v0 lvds;
1886 } lvds = {
1887 .base.version = 1,
1888 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1889 .base.hasht = nv_encoder->dcb->hasht,
1890 .base.hashm = nv_encoder->dcb->hashm,
1891 };
Ben Skeggse225f442012-11-21 14:40:21 +10001892 struct nv50_disp *disp = nv50_disp(encoder->dev);
1893 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001894 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001895 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001896 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001897 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001898 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001899 u8 owner = 1 << nv_crtc->index;
1900 u8 proto = 0xf;
1901 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001902
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001903 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001904 nv_encoder->crtc = encoder->crtc;
1905
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001906 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001907 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001908 if (nv_encoder->dcb->sorconf.link & 1) {
1909 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001910 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001911 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001912 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001913 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001914 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001915 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001916
Ben Skeggse84a35a2014-06-05 10:59:55 +10001917 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001918 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001919 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001920 proto = 0x0;
1921
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001922 if (bios->fp_no_ddc) {
1923 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001924 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001925 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001926 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001927 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001928 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001929 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001930 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001931 } else
1932 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001933 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001934 }
1935
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001936 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001937 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001938 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001939 } else {
1940 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001941 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001942 }
1943
1944 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001945 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001946 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001947
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001948 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001949 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001950 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001951 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001952 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001953 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001954 } else
1955 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001956 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001957 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001958 } else {
1959 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1960 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001961 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001962
1963 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001964 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001965 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001966 proto = 0x9;
Ben Skeggs3eee8642014-09-15 15:20:47 +10001967 nv50_audio_mode_set(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001968 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001969 default:
1970 BUG_ON(1);
1971 break;
1972 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001973
Ben Skeggse84a35a2014-06-05 10:59:55 +10001974 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001975
Ben Skeggs648d4df2014-08-10 04:10:27 +10001976 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001977 u32 *push = evo_wait(mast, 3);
1978 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001979 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1980 u32 syncs = 0x00000001;
1981
1982 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1983 syncs |= 0x00000008;
1984 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1985 syncs |= 0x00000010;
1986
1987 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1988 magic |= 0x00000001;
1989
1990 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1991 evo_data(push, syncs | (depth << 6));
1992 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001993 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001994 }
1995
Ben Skeggse84a35a2014-06-05 10:59:55 +10001996 ctrl = proto << 8;
1997 mask = 0x00000f00;
1998 } else {
1999 ctrl = (depth << 16) | (proto << 8);
2000 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2001 ctrl |= 0x00001000;
2002 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2003 ctrl |= 0x00002000;
2004 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002005 }
2006
Ben Skeggse84a35a2014-06-05 10:59:55 +10002007 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002008}
2009
2010static void
Ben Skeggse225f442012-11-21 14:40:21 +10002011nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002012{
2013 drm_encoder_cleanup(encoder);
2014 kfree(encoder);
2015}
2016
Ben Skeggse225f442012-11-21 14:40:21 +10002017static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2018 .dpms = nv50_sor_dpms,
2019 .mode_fixup = nv50_sor_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10002020 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10002021 .commit = nv50_sor_commit,
2022 .mode_set = nv50_sor_mode_set,
2023 .disable = nv50_sor_disconnect,
2024 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002025};
2026
Ben Skeggse225f442012-11-21 14:40:21 +10002027static const struct drm_encoder_funcs nv50_sor_func = {
2028 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002029};
2030
2031static int
Ben Skeggse225f442012-11-21 14:40:21 +10002032nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002033{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002034 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002035 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002036 struct nouveau_encoder *nv_encoder;
2037 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002038 int type;
2039
2040 switch (dcbe->type) {
2041 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2042 case DCB_OUTPUT_TMDS:
2043 case DCB_OUTPUT_DP:
2044 default:
2045 type = DRM_MODE_ENCODER_TMDS;
2046 break;
2047 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002048
2049 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2050 if (!nv_encoder)
2051 return -ENOMEM;
2052 nv_encoder->dcb = dcbe;
2053 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002054 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002055 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2056
2057 encoder = to_drm_encoder(nv_encoder);
2058 encoder->possible_crtcs = dcbe->heads;
2059 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002060 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10002061 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002062
2063 drm_mode_connector_attach_encoder(connector, encoder);
2064 return 0;
2065}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002066
2067/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002068 * PIOR
2069 *****************************************************************************/
2070
2071static void
2072nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2073{
2074 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2075 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10002076 struct {
2077 struct nv50_disp_mthd_v1 base;
2078 struct nv50_disp_pior_pwr_v0 pwr;
2079 } args = {
2080 .base.version = 1,
2081 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2082 .base.hasht = nv_encoder->dcb->hasht,
2083 .base.hashm = nv_encoder->dcb->hashm,
2084 .pwr.state = mode == DRM_MODE_DPMS_ON,
2085 .pwr.type = nv_encoder->dcb->type,
2086 };
2087
2088 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002089}
2090
2091static bool
2092nv50_pior_mode_fixup(struct drm_encoder *encoder,
2093 const struct drm_display_mode *mode,
2094 struct drm_display_mode *adjusted_mode)
2095{
2096 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2097 struct nouveau_connector *nv_connector;
2098
2099 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2100 if (nv_connector && nv_connector->native_mode) {
2101 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
2102 int id = adjusted_mode->base.id;
2103 *adjusted_mode = *nv_connector->native_mode;
2104 adjusted_mode->base.id = id;
2105 }
2106 }
2107
2108 adjusted_mode->clock *= 2;
2109 return true;
2110}
2111
2112static void
2113nv50_pior_commit(struct drm_encoder *encoder)
2114{
2115}
2116
2117static void
2118nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2119 struct drm_display_mode *adjusted_mode)
2120{
2121 struct nv50_mast *mast = nv50_mast(encoder->dev);
2122 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2123 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2124 struct nouveau_connector *nv_connector;
2125 u8 owner = 1 << nv_crtc->index;
2126 u8 proto, depth;
2127 u32 *push;
2128
2129 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2130 switch (nv_connector->base.display_info.bpc) {
2131 case 10: depth = 0x6; break;
2132 case 8: depth = 0x5; break;
2133 case 6: depth = 0x2; break;
2134 default: depth = 0x0; break;
2135 }
2136
2137 switch (nv_encoder->dcb->type) {
2138 case DCB_OUTPUT_TMDS:
2139 case DCB_OUTPUT_DP:
2140 proto = 0x0;
2141 break;
2142 default:
2143 BUG_ON(1);
2144 break;
2145 }
2146
2147 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2148
2149 push = evo_wait(mast, 8);
2150 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002151 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002152 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2153 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2154 ctrl |= 0x00001000;
2155 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2156 ctrl |= 0x00002000;
2157 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2158 evo_data(push, ctrl);
2159 }
2160
2161 evo_kick(push, mast);
2162 }
2163
2164 nv_encoder->crtc = encoder->crtc;
2165}
2166
2167static void
2168nv50_pior_disconnect(struct drm_encoder *encoder)
2169{
2170 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2171 struct nv50_mast *mast = nv50_mast(encoder->dev);
2172 const int or = nv_encoder->or;
2173 u32 *push;
2174
2175 if (nv_encoder->crtc) {
2176 nv50_crtc_prepare(nv_encoder->crtc);
2177
2178 push = evo_wait(mast, 4);
2179 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002180 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002181 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2182 evo_data(push, 0x00000000);
2183 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002184 evo_kick(push, mast);
2185 }
2186 }
2187
2188 nv_encoder->crtc = NULL;
2189}
2190
2191static void
2192nv50_pior_destroy(struct drm_encoder *encoder)
2193{
2194 drm_encoder_cleanup(encoder);
2195 kfree(encoder);
2196}
2197
2198static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2199 .dpms = nv50_pior_dpms,
2200 .mode_fixup = nv50_pior_mode_fixup,
2201 .prepare = nv50_pior_disconnect,
2202 .commit = nv50_pior_commit,
2203 .mode_set = nv50_pior_mode_set,
2204 .disable = nv50_pior_disconnect,
2205 .get_crtc = nv50_display_crtc_get,
2206};
2207
2208static const struct drm_encoder_funcs nv50_pior_func = {
2209 .destroy = nv50_pior_destroy,
2210};
2211
2212static int
2213nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2214{
2215 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002216 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002217 struct nouveau_i2c_port *ddc = NULL;
2218 struct nouveau_encoder *nv_encoder;
2219 struct drm_encoder *encoder;
2220 int type;
2221
2222 switch (dcbe->type) {
2223 case DCB_OUTPUT_TMDS:
2224 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2225 type = DRM_MODE_ENCODER_TMDS;
2226 break;
2227 case DCB_OUTPUT_DP:
2228 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2229 type = DRM_MODE_ENCODER_TMDS;
2230 break;
2231 default:
2232 return -ENODEV;
2233 }
2234
2235 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2236 if (!nv_encoder)
2237 return -ENOMEM;
2238 nv_encoder->dcb = dcbe;
2239 nv_encoder->or = ffs(dcbe->or) - 1;
2240 nv_encoder->i2c = ddc;
2241
2242 encoder = to_drm_encoder(nv_encoder);
2243 encoder->possible_crtcs = dcbe->heads;
2244 encoder->possible_clones = 0;
2245 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2246 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2247
2248 drm_mode_connector_attach_encoder(connector, encoder);
2249 return 0;
2250}
2251
2252/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002253 * Framebuffer
2254 *****************************************************************************/
2255
Ben Skeggs8a423642014-08-10 04:10:19 +10002256static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002257nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002258{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002259 int i;
2260 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2261 nvif_object_fini(&fbdma->base[i]);
2262 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002263 list_del(&fbdma->head);
2264 kfree(fbdma);
2265}
2266
2267static int
2268nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2269{
2270 struct nouveau_drm *drm = nouveau_drm(dev);
2271 struct nv50_disp *disp = nv50_disp(dev);
2272 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002273 struct __attribute__ ((packed)) {
2274 struct nv_dma_v0 base;
2275 union {
2276 struct nv50_dma_v0 nv50;
2277 struct gf100_dma_v0 gf100;
2278 struct gf110_dma_v0 gf110;
2279 };
2280 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002281 struct nv50_fbdma *fbdma;
2282 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002283 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002284 int ret;
2285
2286 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002287 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002288 return 0;
2289 }
2290
2291 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2292 if (!fbdma)
2293 return -ENOMEM;
2294 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002295
Ben Skeggs4acfd702014-08-10 04:10:24 +10002296 args.base.target = NV_DMA_V0_TARGET_VRAM;
2297 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2298 args.base.start = offset;
2299 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002300
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002301 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002302 args.nv50.part = NV50_DMA_V0_PART_256;
2303 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002304 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002305 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002306 args.nv50.part = NV50_DMA_V0_PART_256;
2307 args.nv50.kind = kind;
2308 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002309 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002310 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002311 args.gf100.kind = kind;
2312 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002313 } else {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002314 args.gf110.page = GF110_DMA_V0_PAGE_LP;
2315 args.gf110.kind = kind;
2316 size += sizeof(args.gf110);
Ben Skeggs8a423642014-08-10 04:10:19 +10002317 }
2318
2319 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002320 struct nv50_head *head = nv50_head(crtc);
2321 int ret = nvif_object_init(&head->sync.base.base.user, NULL,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002322 name, NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002323 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002324 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002325 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002326 return ret;
2327 }
2328 }
2329
Ben Skeggs0ad72862014-08-10 04:10:22 +10002330 ret = nvif_object_init(&mast->base.base.user, NULL, name,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002331 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002332 &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002333 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002334 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002335 return ret;
2336 }
2337
2338 return 0;
2339}
2340
Ben Skeggsab0af552014-08-10 04:10:19 +10002341static void
2342nv50_fb_dtor(struct drm_framebuffer *fb)
2343{
2344}
2345
2346static int
2347nv50_fb_ctor(struct drm_framebuffer *fb)
2348{
2349 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2350 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2351 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002352 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002353 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2354 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002355
2356 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
2357 NV_ERROR(drm, "framebuffer requires contiguous bo\n");
2358 return -EINVAL;
2359 }
2360
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002361 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002362 tile >>= 4; /* yep.. */
2363
Ben Skeggsab0af552014-08-10 04:10:19 +10002364 switch (fb->depth) {
2365 case 8: nv_fb->r_format = 0x1e00; break;
2366 case 15: nv_fb->r_format = 0xe900; break;
2367 case 16: nv_fb->r_format = 0xe800; break;
2368 case 24:
2369 case 32: nv_fb->r_format = 0xcf00; break;
2370 case 30: nv_fb->r_format = 0xd100; break;
2371 default:
2372 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2373 return -EINVAL;
2374 }
2375
Ben Skeggs648d4df2014-08-10 04:10:27 +10002376 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002377 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2378 (fb->pitches[0] | 0x00100000);
2379 nv_fb->r_format |= kind << 16;
2380 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10002381 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002382 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2383 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002384 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002385 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2386 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002387 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002388 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002389
Ben Skeggsf392ec42014-08-10 04:10:28 +10002390 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
2391 drm->device.info.ram_user, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002392}
2393
2394/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002395 * Init
2396 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002397
Ben Skeggs2a44e492011-11-09 11:36:33 +10002398void
Ben Skeggse225f442012-11-21 14:40:21 +10002399nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002400{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002401}
2402
2403int
Ben Skeggse225f442012-11-21 14:40:21 +10002404nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002405{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002406 struct nv50_disp *disp = nv50_disp(dev);
2407 struct drm_crtc *crtc;
2408 u32 *push;
2409
2410 push = evo_wait(nv50_mast(dev), 32);
2411 if (!push)
2412 return -EBUSY;
2413
2414 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2415 struct nv50_sync *sync = nv50_sync(crtc);
2416 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002417 }
2418
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002419 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10002420 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002421 evo_kick(push, nv50_mast(dev));
2422 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002423}
2424
2425void
Ben Skeggse225f442012-11-21 14:40:21 +10002426nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002427{
Ben Skeggse225f442012-11-21 14:40:21 +10002428 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002429 struct nv50_fbdma *fbdma, *fbtmp;
2430
2431 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002432 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002433 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002434
Ben Skeggs0ad72862014-08-10 04:10:22 +10002435 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002436
Ben Skeggs816af2f2011-11-16 15:48:48 +10002437 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002438 if (disp->sync)
2439 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002440 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002441
Ben Skeggs77145f12012-07-31 16:16:21 +10002442 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002443 kfree(disp);
2444}
2445
2446int
Ben Skeggse225f442012-11-21 14:40:21 +10002447nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002448{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002449 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002450 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002451 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002452 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002453 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002454 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002455 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002456
2457 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2458 if (!disp)
2459 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10002460 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10002461
2462 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002463 nouveau_display(dev)->dtor = nv50_display_destroy;
2464 nouveau_display(dev)->init = nv50_display_init;
2465 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10002466 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2467 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002468 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002469
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002470 /* small shared memory area we use for notifiers and semaphores */
2471 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002472 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002473 if (!ret) {
2474 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002475 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002476 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002477 if (ret)
2478 nouveau_bo_unpin(disp->sync);
2479 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002480 if (ret)
2481 nouveau_bo_ref(NULL, &disp->sync);
2482 }
2483
2484 if (ret)
2485 goto out;
2486
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002487 /* allocate master evo channel */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10002488 ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
2489 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002490 if (ret)
2491 goto out;
2492
Ben Skeggs438d99e2011-07-05 16:48:06 +10002493 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10002494 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsdb2bec12014-08-10 04:10:22 +10002495 crtcs = nvif_rd32(device, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10002496 else
2497 crtcs = 2;
2498
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002499 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002500 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002501 if (ret)
2502 goto out;
2503 }
2504
Ben Skeggs83fc0832011-07-05 13:08:40 +10002505 /* create encoder/connector objects based on VBIOS DCB table */
2506 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2507 connector = nouveau_connector_create(dev, dcbe->connector);
2508 if (IS_ERR(connector))
2509 continue;
2510
Ben Skeggseb6313a2013-02-11 09:52:58 +10002511 if (dcbe->location == DCB_LOC_ON_CHIP) {
2512 switch (dcbe->type) {
2513 case DCB_OUTPUT_TMDS:
2514 case DCB_OUTPUT_LVDS:
2515 case DCB_OUTPUT_DP:
2516 ret = nv50_sor_create(connector, dcbe);
2517 break;
2518 case DCB_OUTPUT_ANALOG:
2519 ret = nv50_dac_create(connector, dcbe);
2520 break;
2521 default:
2522 ret = -ENODEV;
2523 break;
2524 }
2525 } else {
2526 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002527 }
2528
Ben Skeggseb6313a2013-02-11 09:52:58 +10002529 if (ret) {
2530 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2531 dcbe->location, dcbe->type,
2532 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002533 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002534 }
2535 }
2536
2537 /* cull any connectors we created that don't have an encoder */
2538 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2539 if (connector->encoder_ids[0])
2540 continue;
2541
Ben Skeggs77145f12012-07-31 16:16:21 +10002542 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002543 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002544 connector->funcs->destroy(connector);
2545 }
2546
Ben Skeggs26f6d882011-07-04 16:25:18 +10002547out:
2548 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002549 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002550 return ret;
2551}