blob: fcbbf3884781d79338970fdd52150fa083372688 [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 Skeggs6af52892014-11-03 15:01:33 +100069 const u32 handle = (oclass[0] << 16) | head;
70 u32 sclass[8];
71 int ret, i;
72
73 ret = nvif_object_sclass(disp, sclass, ARRAY_SIZE(sclass));
74 WARN_ON(ret > ARRAY_SIZE(sclass));
75 if (ret < 0)
76 return ret;
77
Ben Skeggs410f3ec2014-08-10 04:10:25 +100078 while (oclass[0]) {
Ben Skeggs6af52892014-11-03 15:01:33 +100079 for (i = 0; i < ARRAY_SIZE(sclass); i++) {
80 if (sclass[i] == oclass[0]) {
81 ret = nvif_object_init(disp, NULL, handle,
82 oclass[0], data, size,
83 &chan->user);
84 if (ret == 0)
85 nvif_object_map(&chan->user);
86 return ret;
87 }
Ben Skeggsb76f1522014-08-10 04:10:28 +100088 }
Ben Skeggs6af52892014-11-03 15:01:33 +100089 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +100090 }
Ben Skeggs6af52892014-11-03 15:01:33 +100091
Ben Skeggs410f3ec2014-08-10 04:10:25 +100092 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100093}
94
95static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100096nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100097{
Ben Skeggs0ad72862014-08-10 04:10:22 +100098 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100099}
100
101/******************************************************************************
102 * PIO EVO channel
103 *****************************************************************************/
104
Ben Skeggse225f442012-11-21 14:40:21 +1000105struct nv50_pioc {
106 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000107};
108
109static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000110nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000111{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000112 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000113}
114
115static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000116nv50_pioc_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +1000117 void *data, u32 size, struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000118{
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000119 return nv50_chan_create(disp, oclass, head, data, size, &pioc->base);
120}
121
122/******************************************************************************
123 * Cursor Immediate
124 *****************************************************************************/
125
126struct nv50_curs {
127 struct nv50_pioc base;
Ben Skeggs5a560252014-11-10 15:52:02 +1000128 struct nouveau_bo *image;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000129};
130
131static int
132nv50_curs_create(struct nvif_object *disp, int head, struct nv50_curs *curs)
133{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000134 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000135 .head = head,
136 };
137 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000138 GK104_DISP_CURSOR,
139 GF110_DISP_CURSOR,
140 GT214_DISP_CURSOR,
141 G82_DISP_CURSOR,
142 NV50_DISP_CURSOR,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000143 0
144 };
145
146 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
147 &curs->base);
148}
149
150/******************************************************************************
151 * Overlay Immediate
152 *****************************************************************************/
153
154struct nv50_oimm {
155 struct nv50_pioc base;
156};
157
158static int
159nv50_oimm_create(struct nvif_object *disp, int head, struct nv50_oimm *oimm)
160{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000161 struct nv50_disp_cursor_v0 args = {
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000162 .head = head,
163 };
164 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000165 GK104_DISP_OVERLAY,
166 GF110_DISP_OVERLAY,
167 GT214_DISP_OVERLAY,
168 G82_DISP_OVERLAY,
169 NV50_DISP_OVERLAY,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000170 0
171 };
172
173 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
174 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000175}
176
177/******************************************************************************
178 * DMA EVO channel
179 *****************************************************************************/
180
Ben Skeggse225f442012-11-21 14:40:21 +1000181struct nv50_dmac {
182 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000183 dma_addr_t handle;
184 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100185
Ben Skeggs0ad72862014-08-10 04:10:22 +1000186 struct nvif_object sync;
187 struct nvif_object vram;
188
Daniel Vetter59ad1462012-12-02 14:49:44 +0100189 /* Protects against concurrent pushbuf access to this channel, lock is
190 * grabbed by evo_wait (if the pushbuf reservation is successful) and
191 * dropped again by evo_kick. */
192 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000193};
194
195static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000196nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000197{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000198 nvif_object_fini(&dmac->vram);
199 nvif_object_fini(&dmac->sync);
200
201 nv50_chan_destroy(&dmac->base);
202
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000203 if (dmac->ptr) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000204 struct pci_dev *pdev = nvkm_device(nvif_device(disp))->pdev;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000205 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
206 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000207}
208
209static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000210nv50_dmac_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000211 void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000212 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000213{
Ben Skeggsf392ec42014-08-10 04:10:28 +1000214 struct nvif_device *device = nvif_device(disp);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000215 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000216 struct nvif_object pushbuf;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000217 int ret;
218
Daniel Vetter59ad1462012-12-02 14:49:44 +0100219 mutex_init(&dmac->lock);
220
Ben Skeggsf392ec42014-08-10 04:10:28 +1000221 dmac->ptr = pci_alloc_consistent(nvkm_device(device)->pdev,
Ben Skeggs0ad72862014-08-10 04:10:22 +1000222 PAGE_SIZE, &dmac->handle);
Ben Skeggs47057302012-11-16 13:58:48 +1000223 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000224 return -ENOMEM;
225
Ben Skeggsf392ec42014-08-10 04:10:28 +1000226 ret = nvif_object_init(nvif_object(device), NULL,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000227 args->pushbuf, NV_DMA_FROM_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000228 &(struct nv_dma_v0) {
229 .target = NV_DMA_V0_TARGET_PCI_US,
230 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000231 .start = dmac->handle + 0x0000,
232 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000233 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000234 if (ret)
235 return ret;
236
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000237 ret = nv50_chan_create(disp, oclass, head, data, size, &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000238 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000239 if (ret)
240 return ret;
241
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000242 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000000,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000243 NV_DMA_IN_MEMORY,
244 &(struct nv_dma_v0) {
245 .target = NV_DMA_V0_TARGET_VRAM,
246 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000247 .start = syncbuf + 0x0000,
248 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000249 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000250 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000251 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000252 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000253
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000254 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000001,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000255 NV_DMA_IN_MEMORY,
256 &(struct nv_dma_v0) {
257 .target = NV_DMA_V0_TARGET_VRAM,
258 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000259 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000260 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000261 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000262 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000263 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000264 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000265
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000266 return ret;
267}
268
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000269/******************************************************************************
270 * Core
271 *****************************************************************************/
272
Ben Skeggse225f442012-11-21 14:40:21 +1000273struct nv50_mast {
274 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000275};
276
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000277static int
278nv50_core_create(struct nvif_object *disp, u64 syncbuf, struct nv50_mast *core)
279{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000280 struct nv50_disp_core_channel_dma_v0 args = {
281 .pushbuf = 0xb0007d00,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000282 };
283 static const u32 oclass[] = {
Ben Skeggsdbbd6bc2014-08-19 10:23:47 +1000284 GM204_DISP_CORE_CHANNEL_DMA,
Ben Skeggs648d4df2014-08-10 04:10:27 +1000285 GM107_DISP_CORE_CHANNEL_DMA,
286 GK110_DISP_CORE_CHANNEL_DMA,
287 GK104_DISP_CORE_CHANNEL_DMA,
288 GF110_DISP_CORE_CHANNEL_DMA,
289 GT214_DISP_CORE_CHANNEL_DMA,
290 GT206_DISP_CORE_CHANNEL_DMA,
291 GT200_DISP_CORE_CHANNEL_DMA,
292 G82_DISP_CORE_CHANNEL_DMA,
293 NV50_DISP_CORE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000294 0
295 };
296
297 return nv50_dmac_create(disp, oclass, 0, &args, sizeof(args), syncbuf,
298 &core->base);
299}
300
301/******************************************************************************
302 * Base
303 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000304
Ben Skeggse225f442012-11-21 14:40:21 +1000305struct nv50_sync {
306 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000307 u32 addr;
308 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000309};
310
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000311static int
312nv50_base_create(struct nvif_object *disp, int head, u64 syncbuf,
313 struct nv50_sync *base)
314{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000315 struct nv50_disp_base_channel_dma_v0 args = {
316 .pushbuf = 0xb0007c00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000317 .head = head,
318 };
319 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000320 GK110_DISP_BASE_CHANNEL_DMA,
321 GK104_DISP_BASE_CHANNEL_DMA,
322 GF110_DISP_BASE_CHANNEL_DMA,
323 GT214_DISP_BASE_CHANNEL_DMA,
324 GT200_DISP_BASE_CHANNEL_DMA,
325 G82_DISP_BASE_CHANNEL_DMA,
326 NV50_DISP_BASE_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000327 0
328 };
329
330 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
331 syncbuf, &base->base);
332}
333
334/******************************************************************************
335 * Overlay
336 *****************************************************************************/
337
Ben Skeggse225f442012-11-21 14:40:21 +1000338struct nv50_ovly {
339 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000340};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000341
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000342static int
343nv50_ovly_create(struct nvif_object *disp, int head, u64 syncbuf,
344 struct nv50_ovly *ovly)
345{
Ben Skeggs648d4df2014-08-10 04:10:27 +1000346 struct nv50_disp_overlay_channel_dma_v0 args = {
347 .pushbuf = 0xb0007e00 | head,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000348 .head = head,
349 };
350 static const u32 oclass[] = {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000351 GK104_DISP_OVERLAY_CONTROL_DMA,
352 GF110_DISP_OVERLAY_CONTROL_DMA,
353 GT214_DISP_OVERLAY_CHANNEL_DMA,
354 GT200_DISP_OVERLAY_CHANNEL_DMA,
355 G82_DISP_OVERLAY_CHANNEL_DMA,
356 NV50_DISP_OVERLAY_CHANNEL_DMA,
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000357 0
358 };
359
360 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
361 syncbuf, &ovly->base);
362}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000363
Ben Skeggse225f442012-11-21 14:40:21 +1000364struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000365 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000366 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000367 struct nv50_curs curs;
368 struct nv50_sync sync;
369 struct nv50_ovly ovly;
370 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000371};
372
Ben Skeggse225f442012-11-21 14:40:21 +1000373#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
374#define nv50_curs(c) (&nv50_head(c)->curs)
375#define nv50_sync(c) (&nv50_head(c)->sync)
376#define nv50_ovly(c) (&nv50_head(c)->ovly)
377#define nv50_oimm(c) (&nv50_head(c)->oimm)
378#define nv50_chan(c) (&(c)->base.base)
Ben Skeggs0ad72862014-08-10 04:10:22 +1000379#define nv50_vers(c) nv50_chan(c)->user.oclass
380
381struct nv50_fbdma {
382 struct list_head head;
383 struct nvif_object core;
384 struct nvif_object base[4];
385};
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000386
Ben Skeggse225f442012-11-21 14:40:21 +1000387struct nv50_disp {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000388 struct nvif_object *disp;
Ben Skeggse225f442012-11-21 14:40:21 +1000389 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000390
Ben Skeggs8a423642014-08-10 04:10:19 +1000391 struct list_head fbdma;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000392
393 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000394};
395
Ben Skeggse225f442012-11-21 14:40:21 +1000396static struct nv50_disp *
397nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000398{
Ben Skeggs77145f12012-07-31 16:16:21 +1000399 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000400}
401
Ben Skeggse225f442012-11-21 14:40:21 +1000402#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000403
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000404static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000405nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000406{
407 return nouveau_encoder(encoder)->crtc;
408}
409
410/******************************************************************************
411 * EVO channel helpers
412 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000413static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000414evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000415{
Ben Skeggse225f442012-11-21 14:40:21 +1000416 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000417 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000418
Daniel Vetter59ad1462012-12-02 14:49:44 +0100419 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000420 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000421 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000422
Ben Skeggs0ad72862014-08-10 04:10:22 +1000423 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
424 if (!nvkm_wait(&dmac->base.user, 0x0004, ~0, 0x00000000)) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100425 mutex_unlock(&dmac->lock);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000426 nv_error(nvkm_object(&dmac->base.user), "channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000427 return NULL;
428 }
429
430 put = 0;
431 }
432
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000433 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000434}
435
436static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000437evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000438{
Ben Skeggse225f442012-11-21 14:40:21 +1000439 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000440 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100441 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000442}
443
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000444#if 1
Ben Skeggs51beb422011-07-05 10:33:08 +1000445#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
446#define evo_data(p,d) *((p)++) = (d)
Ben Skeggs2b1930c2014-11-03 16:43:59 +1000447#else
448#define evo_mthd(p,m,s) do { \
449 const u32 _m = (m), _s = (s); \
450 printk(KERN_ERR "%04x %d %s\n", _m, _s, __func__); \
451 *((p)++) = ((_s << 18) | _m); \
452} while(0)
453#define evo_data(p,d) do { \
454 const u32 _d = (d); \
455 printk(KERN_ERR "\t%08x\n", _d); \
456 *((p)++) = _d; \
457} while(0)
458#endif
Ben Skeggs51beb422011-07-05 10:33:08 +1000459
Ben Skeggs3376ee32011-11-12 14:28:12 +1000460static bool
461evo_sync_wait(void *data)
462{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500463 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
464 return true;
465 usleep_range(1, 2);
466 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000467}
468
469static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000470evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000471{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000472 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000473 struct nv50_disp *disp = nv50_disp(dev);
474 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000475 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000476 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000477 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000478 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000479 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000480 evo_mthd(push, 0x0080, 2);
481 evo_data(push, 0x00000000);
482 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000483 evo_kick(push, mast);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000484 if (nv_wait_cb(nvkm_device(device), evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000485 return 0;
486 }
487
488 return -EBUSY;
489}
490
491/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000492 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000493 *****************************************************************************/
494struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000495nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000496{
Ben Skeggse225f442012-11-21 14:40:21 +1000497 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000498}
499
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000500struct nv50_display_flip {
501 struct nv50_disp *disp;
502 struct nv50_sync *chan;
503};
504
505static bool
506nv50_display_flip_wait(void *data)
507{
508 struct nv50_display_flip *flip = data;
509 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500510 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000511 return true;
512 usleep_range(1, 2);
513 return false;
514}
515
Ben Skeggs3376ee32011-11-12 14:28:12 +1000516void
Ben Skeggse225f442012-11-21 14:40:21 +1000517nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000518{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000519 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000520 struct nv50_display_flip flip = {
521 .disp = nv50_disp(crtc->dev),
522 .chan = nv50_sync(crtc),
523 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000524 u32 *push;
525
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000526 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000527 if (push) {
528 evo_mthd(push, 0x0084, 1);
529 evo_data(push, 0x00000000);
530 evo_mthd(push, 0x0094, 1);
531 evo_data(push, 0x00000000);
532 evo_mthd(push, 0x00c0, 1);
533 evo_data(push, 0x00000000);
534 evo_mthd(push, 0x0080, 1);
535 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000536 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000537 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000538
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000539 nv_wait_cb(nvkm_device(device), nv50_display_flip_wait, &flip);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000540}
541
542int
Ben Skeggse225f442012-11-21 14:40:21 +1000543nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000544 struct nouveau_channel *chan, u32 swap_interval)
545{
546 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000547 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000548 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000549 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000550 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000551 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000552
Ben Skeggs9ba83102014-12-22 19:50:23 +1000553 if (crtc->primary->fb->width != fb->width ||
554 crtc->primary->fb->height != fb->height)
555 return -EINVAL;
556
Ben Skeggs3376ee32011-11-12 14:28:12 +1000557 swap_interval <<= 4;
558 if (swap_interval == 0)
559 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000560 if (chan == NULL)
561 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000562
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000563 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000564 if (unlikely(push == NULL))
565 return -EBUSY;
566
Ben Skeggsbbf89062014-08-10 04:10:25 +1000567 if (chan && chan->object->oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000568 ret = RING_SPACE(chan, 8);
569 if (ret)
570 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000571
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000572 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000573 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000574 OUT_RING (chan, sync->addr ^ 0x10);
575 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
576 OUT_RING (chan, sync->data + 1);
577 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
578 OUT_RING (chan, sync->addr);
579 OUT_RING (chan, sync->data);
580 } else
Ben Skeggsbbf89062014-08-10 04:10:25 +1000581 if (chan && chan->object->oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000582 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000583 ret = RING_SPACE(chan, 12);
584 if (ret)
585 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000586
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000587 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000588 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000589 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
590 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
591 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
592 OUT_RING (chan, sync->data + 1);
593 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
594 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
595 OUT_RING (chan, upper_32_bits(addr));
596 OUT_RING (chan, lower_32_bits(addr));
597 OUT_RING (chan, sync->data);
598 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
599 } else
600 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000601 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000602 ret = RING_SPACE(chan, 10);
603 if (ret)
604 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000605
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000606 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
607 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
608 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
609 OUT_RING (chan, sync->data + 1);
610 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
611 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
612 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
613 OUT_RING (chan, upper_32_bits(addr));
614 OUT_RING (chan, lower_32_bits(addr));
615 OUT_RING (chan, sync->data);
616 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
617 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
618 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500619
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000620 if (chan) {
621 sync->addr ^= 0x10;
622 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000623 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000624 }
625
626 /* queue the flip */
627 evo_mthd(push, 0x0100, 1);
628 evo_data(push, 0xfffe0000);
629 evo_mthd(push, 0x0084, 1);
630 evo_data(push, swap_interval);
631 if (!(swap_interval & 0x00000100)) {
632 evo_mthd(push, 0x00e0, 1);
633 evo_data(push, 0x40000000);
634 }
635 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000636 evo_data(push, sync->addr);
637 evo_data(push, sync->data++);
638 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000639 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000640 evo_mthd(push, 0x00a0, 2);
641 evo_data(push, 0x00000000);
642 evo_data(push, 0x00000000);
643 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000644 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000645 evo_mthd(push, 0x0110, 2);
646 evo_data(push, 0x00000000);
647 evo_data(push, 0x00000000);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000648 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000649 evo_mthd(push, 0x0800, 5);
650 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
651 evo_data(push, 0);
652 evo_data(push, (fb->height << 16) | fb->width);
653 evo_data(push, nv_fb->r_pitch);
654 evo_data(push, nv_fb->r_format);
655 } else {
656 evo_mthd(push, 0x0400, 5);
657 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
658 evo_data(push, 0);
659 evo_data(push, (fb->height << 16) | fb->width);
660 evo_data(push, nv_fb->r_pitch);
661 evo_data(push, nv_fb->r_format);
662 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000663 evo_mthd(push, 0x0080, 1);
664 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000665 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000666
667 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000668 return 0;
669}
670
Ben Skeggs26f6d882011-07-04 16:25:18 +1000671/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000672 * CRTC
673 *****************************************************************************/
674static int
Ben Skeggse225f442012-11-21 14:40:21 +1000675nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000676{
Ben Skeggse225f442012-11-21 14:40:21 +1000677 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000678 struct nouveau_connector *nv_connector;
679 struct drm_connector *connector;
680 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000681
Ben Skeggs488ff202011-10-17 10:38:10 +1000682 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000683 connector = &nv_connector->base;
684 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -0700685 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +1000686 mode = DITHERING_MODE_DYNAMIC2X2;
687 } else {
688 mode = nv_connector->dithering_mode;
689 }
690
691 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
692 if (connector->display_info.bpc >= 8)
693 mode |= DITHERING_DEPTH_8BPC;
694 } else {
695 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000696 }
697
Ben Skeggsde8268c2012-11-16 10:24:31 +1000698 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000699 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000700 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000701 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
702 evo_data(push, mode);
703 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000704 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000705 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
706 evo_data(push, mode);
707 } else {
708 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
709 evo_data(push, mode);
710 }
711
Ben Skeggs438d99e2011-07-05 16:48:06 +1000712 if (update) {
713 evo_mthd(push, 0x0080, 1);
714 evo_data(push, 0x00000000);
715 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000716 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000717 }
718
719 return 0;
720}
721
722static int
Ben Skeggse225f442012-11-21 14:40:21 +1000723nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000724{
Ben Skeggse225f442012-11-21 14:40:21 +1000725 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000726 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000727 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000728 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000729 int mode = DRM_MODE_SCALE_NONE;
730 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000731
Ben Skeggs92854622011-11-11 23:49:06 +1000732 /* start off at the resolution we programmed the crtc for, this
733 * effectively handles NONE/FULL scaling
734 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000735 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs576f7912014-12-22 17:19:26 +1000736 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs92854622011-11-11 23:49:06 +1000737 mode = nv_connector->scaling_mode;
Ben Skeggs576f7912014-12-22 17:19:26 +1000738 if (nv_connector->scaling_full) /* non-EDID LVDS/eDP mode */
739 mode = DRM_MODE_SCALE_FULLSCREEN;
740 }
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000741
Ben Skeggs92854622011-11-11 23:49:06 +1000742 if (mode != DRM_MODE_SCALE_NONE)
743 omode = nv_connector->native_mode;
744 else
745 omode = umode;
746
747 oX = omode->hdisplay;
748 oY = omode->vdisplay;
749 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
750 oY *= 2;
751
752 /* add overscan compensation if necessary, will keep the aspect
753 * ratio the same as the backend mode unless overridden by the
754 * user setting both hborder and vborder properties.
755 */
756 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
757 (nv_connector->underscan == UNDERSCAN_AUTO &&
758 nv_connector->edid &&
759 drm_detect_hdmi_monitor(nv_connector->edid)))) {
760 u32 bX = nv_connector->underscan_hborder;
761 u32 bY = nv_connector->underscan_vborder;
762 u32 aspect = (oY << 19) / oX;
763
764 if (bX) {
765 oX -= (bX * 2);
766 if (bY) oY -= (bY * 2);
767 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
768 } else {
769 oX -= (oX >> 4) + 32;
770 if (bY) oY -= (bY * 2);
771 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000772 }
773 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000774
Ben Skeggs92854622011-11-11 23:49:06 +1000775 /* handle CENTER/ASPECT scaling, taking into account the areas
776 * removed already for overscan compensation
777 */
778 switch (mode) {
779 case DRM_MODE_SCALE_CENTER:
780 oX = min((u32)umode->hdisplay, oX);
781 oY = min((u32)umode->vdisplay, oY);
782 /* fall-through */
783 case DRM_MODE_SCALE_ASPECT:
784 if (oY < oX) {
785 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
786 oX = ((oY * aspect) + (aspect / 2)) >> 19;
787 } else {
788 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
789 oY = ((oX * aspect) + (aspect / 2)) >> 19;
790 }
791 break;
792 default:
793 break;
794 }
795
Ben Skeggsde8268c2012-11-16 10:24:31 +1000796 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000797 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000798 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000799 /*XXX: SCALE_CTRL_ACTIVE??? */
800 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
801 evo_data(push, (oY << 16) | oX);
802 evo_data(push, (oY << 16) | oX);
803 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
804 evo_data(push, 0x00000000);
805 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
806 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
807 } else {
808 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
809 evo_data(push, (oY << 16) | oX);
810 evo_data(push, (oY << 16) | oX);
811 evo_data(push, (oY << 16) | oX);
812 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
813 evo_data(push, 0x00000000);
814 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
815 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
816 }
817
818 evo_kick(push, mast);
819
Ben Skeggs3376ee32011-11-12 14:28:12 +1000820 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000821 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700822 nv50_display_flip_next(crtc, crtc->primary->fb,
823 NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000824 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000825 }
826
827 return 0;
828}
829
830static int
Roy Splieteae73822014-10-30 22:57:45 +0100831nv50_crtc_set_raster_vblank_dmi(struct nouveau_crtc *nv_crtc, u32 usec)
832{
833 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
834 u32 *push;
835
836 push = evo_wait(mast, 8);
837 if (!push)
838 return -ENOMEM;
839
840 evo_mthd(push, 0x0828 + (nv_crtc->index * 0x400), 1);
841 evo_data(push, usec);
842 evo_kick(push, mast);
843 return 0;
844}
845
846static int
Ben Skeggse225f442012-11-21 14:40:21 +1000847nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000848{
Ben Skeggse225f442012-11-21 14:40:21 +1000849 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000850 u32 *push, hue, vib;
851 int adj;
852
853 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
854 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
855 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
856
857 push = evo_wait(mast, 16);
858 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000859 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000860 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
861 evo_data(push, (hue << 20) | (vib << 8));
862 } else {
863 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
864 evo_data(push, (hue << 20) | (vib << 8));
865 }
866
867 if (update) {
868 evo_mthd(push, 0x0080, 1);
869 evo_data(push, 0x00000000);
870 }
871 evo_kick(push, mast);
872 }
873
874 return 0;
875}
876
877static int
Ben Skeggse225f442012-11-21 14:40:21 +1000878nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000879 int x, int y, bool update)
880{
881 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000882 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000883 u32 *push;
884
Ben Skeggsde8268c2012-11-16 10:24:31 +1000885 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000886 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000887 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000888 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
889 evo_data(push, nvfb->nvbo->bo.offset >> 8);
890 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
891 evo_data(push, (fb->height << 16) | fb->width);
892 evo_data(push, nvfb->r_pitch);
893 evo_data(push, nvfb->r_format);
894 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
895 evo_data(push, (y << 16) | x);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000896 if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000897 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000898 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000899 }
900 } else {
901 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
902 evo_data(push, nvfb->nvbo->bo.offset >> 8);
903 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
904 evo_data(push, (fb->height << 16) | fb->width);
905 evo_data(push, nvfb->r_pitch);
906 evo_data(push, nvfb->r_format);
Ben Skeggs8a423642014-08-10 04:10:19 +1000907 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000908 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
909 evo_data(push, (y << 16) | x);
910 }
911
Ben Skeggsa46232e2011-07-07 15:23:48 +1000912 if (update) {
913 evo_mthd(push, 0x0080, 1);
914 evo_data(push, 0x00000000);
915 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000916 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000917 }
918
Ben Skeggs8a423642014-08-10 04:10:19 +1000919 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000920 return 0;
921}
922
923static void
Ben Skeggse225f442012-11-21 14:40:21 +1000924nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000925{
Ben Skeggse225f442012-11-21 14:40:21 +1000926 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs5a560252014-11-10 15:52:02 +1000927 struct nv50_curs *curs = nv50_curs(&nv_crtc->base);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000928 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000929 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000930 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000931 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
932 evo_data(push, 0x85000000);
Ben Skeggs5a560252014-11-10 15:52:02 +1000933 evo_data(push, curs->image->bo.offset >> 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000934 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000935 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000936 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
937 evo_data(push, 0x85000000);
Ben Skeggs5a560252014-11-10 15:52:02 +1000938 evo_data(push, curs->image->bo.offset >> 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000939 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000940 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000941 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000942 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
943 evo_data(push, 0x85000000);
Ben Skeggs5a560252014-11-10 15:52:02 +1000944 evo_data(push, curs->image->bo.offset >> 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000945 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000946 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000947 }
948 evo_kick(push, mast);
949 }
950}
951
952static void
Ben Skeggse225f442012-11-21 14:40:21 +1000953nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000954{
Ben Skeggse225f442012-11-21 14:40:21 +1000955 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000956 u32 *push = evo_wait(mast, 16);
957 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000958 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000959 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
960 evo_data(push, 0x05000000);
961 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000962 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000963 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
964 evo_data(push, 0x05000000);
965 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
966 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000967 } else {
968 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
969 evo_data(push, 0x05000000);
970 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
971 evo_data(push, 0x00000000);
972 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000973 evo_kick(push, mast);
974 }
975}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000976
Ben Skeggsde8268c2012-11-16 10:24:31 +1000977static void
Ben Skeggse225f442012-11-21 14:40:21 +1000978nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000979{
Ben Skeggse225f442012-11-21 14:40:21 +1000980 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs5a560252014-11-10 15:52:02 +1000981 struct nv50_curs *curs = nv50_curs(&nv_crtc->base);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000982
Ben Skeggs5a560252014-11-10 15:52:02 +1000983 if (show && curs->image)
Ben Skeggse225f442012-11-21 14:40:21 +1000984 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000985 else
Ben Skeggse225f442012-11-21 14:40:21 +1000986 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000987
988 if (update) {
989 u32 *push = evo_wait(mast, 2);
990 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000991 evo_mthd(push, 0x0080, 1);
992 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000993 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000994 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000995 }
996}
997
998static void
Ben Skeggse225f442012-11-21 14:40:21 +1000999nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001000{
1001}
1002
1003static void
Ben Skeggse225f442012-11-21 14:40:21 +10001004nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001005{
1006 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001007 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001008 u32 *push;
1009
Ben Skeggse225f442012-11-21 14:40:21 +10001010 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001011
Ben Skeggs56d237d2014-05-19 14:54:33 +10001012 push = evo_wait(mast, 6);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001013 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001014 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001015 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1016 evo_data(push, 0x00000000);
1017 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
1018 evo_data(push, 0x40000000);
1019 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001020 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001021 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1022 evo_data(push, 0x00000000);
1023 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
1024 evo_data(push, 0x40000000);
1025 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
1026 evo_data(push, 0x00000000);
1027 } else {
1028 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
1029 evo_data(push, 0x00000000);
1030 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
1031 evo_data(push, 0x03000000);
1032 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
1033 evo_data(push, 0x00000000);
1034 }
1035
1036 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001037 }
1038
Ben Skeggse225f442012-11-21 14:40:21 +10001039 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001040}
1041
1042static void
Ben Skeggse225f442012-11-21 14:40:21 +10001043nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001044{
1045 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001046 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001047 u32 *push;
1048
Ben Skeggsde8268c2012-11-16 10:24:31 +10001049 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001050 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001051 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001052 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001053 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001054 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1055 evo_data(push, 0xc0000000);
1056 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1057 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001058 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001059 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001060 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001061 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1062 evo_data(push, 0xc0000000);
1063 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1064 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001065 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001066 } else {
1067 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001068 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001069 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1070 evo_data(push, 0x83000000);
1071 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1072 evo_data(push, 0x00000000);
1073 evo_data(push, 0x00000000);
1074 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001075 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001076 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1077 evo_data(push, 0xffffff00);
1078 }
1079
1080 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001081 }
1082
Ben Skeggs5a560252014-11-10 15:52:02 +10001083 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Matt Roperf4510a22014-04-01 15:22:40 -07001084 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001085}
1086
1087static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001088nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001089 struct drm_display_mode *adjusted_mode)
1090{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001091 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001092 return true;
1093}
1094
1095static int
Ben Skeggse225f442012-11-21 14:40:21 +10001096nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001097{
Matt Roperf4510a22014-04-01 15:22:40 -07001098 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001099 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001100 int ret;
1101
Ben Skeggs547ad072014-11-10 12:35:06 +10001102 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001103 if (ret == 0) {
1104 if (head->image)
1105 nouveau_bo_unpin(head->image);
1106 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001107 }
1108
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001109 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001110}
1111
1112static int
Ben Skeggse225f442012-11-21 14:40:21 +10001113nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001114 struct drm_display_mode *mode, int x, int y,
1115 struct drm_framebuffer *old_fb)
1116{
Ben Skeggse225f442012-11-21 14:40:21 +10001117 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001118 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1119 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001120 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1121 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1122 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1123 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
Roy Spliet1dce6262014-09-12 18:00:13 +02001124 u32 vblan2e = 0, vblan2s = 1, vblankus = 0;
Ben Skeggs3488c572012-03-12 11:42:20 +10001125 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001126 int ret;
1127
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001128 hactive = mode->htotal;
1129 hsynce = mode->hsync_end - mode->hsync_start - 1;
1130 hbackp = mode->htotal - mode->hsync_end;
1131 hblanke = hsynce + hbackp;
1132 hfrontp = mode->hsync_start - mode->hdisplay;
1133 hblanks = mode->htotal - hfrontp - 1;
1134
1135 vactive = mode->vtotal * vscan / ilace;
1136 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1137 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1138 vblanke = vsynce + vbackp;
1139 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1140 vblanks = vactive - vfrontp - 1;
Roy Spliet1dce6262014-09-12 18:00:13 +02001141 /* XXX: Safe underestimate, even "0" works */
1142 vblankus = (vactive - mode->vdisplay - 2) * hactive;
1143 vblankus *= 1000;
1144 vblankus /= mode->clock;
1145
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001146 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1147 vblan2e = vactive + vsynce + vbackp;
1148 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1149 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001150 }
1151
Ben Skeggse225f442012-11-21 14:40:21 +10001152 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001153 if (ret)
1154 return ret;
1155
Ben Skeggsde8268c2012-11-16 10:24:31 +10001156 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001157 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001158 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001159 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1160 evo_data(push, 0x00800000 | mode->clock);
1161 evo_data(push, (ilace == 2) ? 2 : 0);
Roy Splieteae73822014-10-30 22:57:45 +01001162 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001163 evo_data(push, 0x00000000);
1164 evo_data(push, (vactive << 16) | hactive);
1165 evo_data(push, ( vsynce << 16) | hsynce);
1166 evo_data(push, (vblanke << 16) | hblanke);
1167 evo_data(push, (vblanks << 16) | hblanks);
1168 evo_data(push, (vblan2e << 16) | vblan2s);
Roy Splieteae73822014-10-30 22:57:45 +01001169 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001170 evo_data(push, 0x00000000);
1171 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1172 evo_data(push, 0x00000311);
1173 evo_data(push, 0x00000100);
1174 } else {
1175 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1176 evo_data(push, 0x00000000);
1177 evo_data(push, (vactive << 16) | hactive);
1178 evo_data(push, ( vsynce << 16) | hsynce);
1179 evo_data(push, (vblanke << 16) | hblanke);
1180 evo_data(push, (vblanks << 16) | hblanks);
1181 evo_data(push, (vblan2e << 16) | vblan2s);
1182 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1183 evo_data(push, 0x00000000); /* ??? */
1184 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1185 evo_data(push, mode->clock * 1000);
1186 evo_data(push, 0x00200000); /* ??? */
1187 evo_data(push, mode->clock * 1000);
1188 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1189 evo_data(push, 0x00000311);
1190 evo_data(push, 0x00000100);
1191 }
1192
1193 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001194 }
1195
1196 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001197 nv50_crtc_set_dither(nv_crtc, false);
1198 nv50_crtc_set_scale(nv_crtc, false);
Roy Splieteae73822014-10-30 22:57:45 +01001199
1200 /* G94 only accepts this after setting scale */
1201 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA)
1202 nv50_crtc_set_raster_vblank_dmi(nv_crtc, vblankus);
1203
Ben Skeggse225f442012-11-21 14:40:21 +10001204 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001205 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001206 return 0;
1207}
1208
1209static int
Ben Skeggse225f442012-11-21 14:40:21 +10001210nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001211 struct drm_framebuffer *old_fb)
1212{
Ben Skeggs77145f12012-07-31 16:16:21 +10001213 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001214 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1215 int ret;
1216
Matt Roperf4510a22014-04-01 15:22:40 -07001217 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001218 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001219 return 0;
1220 }
1221
Ben Skeggse225f442012-11-21 14:40:21 +10001222 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001223 if (ret)
1224 return ret;
1225
Ben Skeggse225f442012-11-21 14:40:21 +10001226 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001227 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1228 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001229 return 0;
1230}
1231
1232static int
Ben Skeggse225f442012-11-21 14:40:21 +10001233nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001234 struct drm_framebuffer *fb, int x, int y,
1235 enum mode_set_atomic state)
1236{
1237 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001238 nv50_display_flip_stop(crtc);
1239 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001240 return 0;
1241}
1242
1243static void
Ben Skeggse225f442012-11-21 14:40:21 +10001244nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001245{
Ben Skeggse225f442012-11-21 14:40:21 +10001246 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001247 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1248 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1249 int i;
1250
1251 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001252 u16 r = nv_crtc->lut.r[i] >> 2;
1253 u16 g = nv_crtc->lut.g[i] >> 2;
1254 u16 b = nv_crtc->lut.b[i] >> 2;
1255
Ben Skeggs648d4df2014-08-10 04:10:27 +10001256 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001257 writew(r + 0x0000, lut + (i * 0x08) + 0);
1258 writew(g + 0x0000, lut + (i * 0x08) + 2);
1259 writew(b + 0x0000, lut + (i * 0x08) + 4);
1260 } else {
1261 writew(r + 0x6000, lut + (i * 0x20) + 0);
1262 writew(g + 0x6000, lut + (i * 0x20) + 2);
1263 writew(b + 0x6000, lut + (i * 0x20) + 4);
1264 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001265 }
1266}
1267
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001268static void
1269nv50_crtc_disable(struct drm_crtc *crtc)
1270{
1271 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001272 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001273 if (head->image)
1274 nouveau_bo_unpin(head->image);
1275 nouveau_bo_ref(NULL, &head->image);
1276}
1277
Ben Skeggs438d99e2011-07-05 16:48:06 +10001278static int
Ben Skeggse225f442012-11-21 14:40:21 +10001279nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001280 uint32_t handle, uint32_t width, uint32_t height)
1281{
1282 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs5a560252014-11-10 15:52:02 +10001283 struct nv50_curs *curs = nv50_curs(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001284 struct drm_device *dev = crtc->dev;
Ben Skeggs5a560252014-11-10 15:52:02 +10001285 struct drm_gem_object *gem = NULL;
1286 struct nouveau_bo *nvbo = NULL;
1287 int ret = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001288
Ben Skeggs5a560252014-11-10 15:52:02 +10001289 if (handle) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001290 if (width != 64 || height != 64)
1291 return -EINVAL;
1292
1293 gem = drm_gem_object_lookup(dev, file_priv, handle);
1294 if (unlikely(!gem))
1295 return -ENOENT;
1296 nvbo = nouveau_gem_object(gem);
1297
Ben Skeggs5a560252014-11-10 15:52:02 +10001298 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001299 }
1300
Ben Skeggs5a560252014-11-10 15:52:02 +10001301 if (ret == 0) {
1302 if (curs->image)
1303 nouveau_bo_unpin(curs->image);
1304 nouveau_bo_ref(nvbo, &curs->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001305 }
Ben Skeggs5a560252014-11-10 15:52:02 +10001306 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001307
Ben Skeggs5a560252014-11-10 15:52:02 +10001308 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001309 return ret;
1310}
1311
1312static int
Ben Skeggse225f442012-11-21 14:40:21 +10001313nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001314{
Ben Skeggse225f442012-11-21 14:40:21 +10001315 struct nv50_curs *curs = nv50_curs(crtc);
1316 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001317 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1318 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001319 return 0;
1320}
1321
1322static void
Ben Skeggse225f442012-11-21 14:40:21 +10001323nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001324 uint32_t start, uint32_t size)
1325{
1326 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Dan Carpenterbdefc8c2013-11-28 01:18:47 +03001327 u32 end = min_t(u32, start + size, 256);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001328 u32 i;
1329
1330 for (i = start; i < end; i++) {
1331 nv_crtc->lut.r[i] = r[i];
1332 nv_crtc->lut.g[i] = g[i];
1333 nv_crtc->lut.b[i] = b[i];
1334 }
1335
Ben Skeggse225f442012-11-21 14:40:21 +10001336 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001337}
1338
1339static void
Ben Skeggse225f442012-11-21 14:40:21 +10001340nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001341{
1342 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001343 struct nv50_disp *disp = nv50_disp(crtc->dev);
1344 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001345 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001346
Ben Skeggs0ad72862014-08-10 04:10:22 +10001347 list_for_each_entry(fbdma, &disp->fbdma, head) {
1348 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1349 }
1350
1351 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1352 nv50_pioc_destroy(&head->oimm.base);
1353 nv50_dmac_destroy(&head->sync.base, disp->disp);
1354 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001355
1356 /*XXX: this shouldn't be necessary, but the core doesn't call
1357 * disconnect() during the cleanup paths
1358 */
1359 if (head->image)
1360 nouveau_bo_unpin(head->image);
1361 nouveau_bo_ref(NULL, &head->image);
1362
Ben Skeggs5a560252014-11-10 15:52:02 +10001363 /*XXX: ditto */
1364 if (head->curs.image)
1365 nouveau_bo_unpin(head->curs.image);
1366 nouveau_bo_ref(NULL, &head->curs.image);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001367
Ben Skeggs438d99e2011-07-05 16:48:06 +10001368 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001369 if (nv_crtc->lut.nvbo)
1370 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001371 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001372
Ben Skeggs438d99e2011-07-05 16:48:06 +10001373 drm_crtc_cleanup(crtc);
1374 kfree(crtc);
1375}
1376
Ben Skeggse225f442012-11-21 14:40:21 +10001377static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1378 .dpms = nv50_crtc_dpms,
1379 .prepare = nv50_crtc_prepare,
1380 .commit = nv50_crtc_commit,
1381 .mode_fixup = nv50_crtc_mode_fixup,
1382 .mode_set = nv50_crtc_mode_set,
1383 .mode_set_base = nv50_crtc_mode_set_base,
1384 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1385 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001386 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001387};
1388
Ben Skeggse225f442012-11-21 14:40:21 +10001389static const struct drm_crtc_funcs nv50_crtc_func = {
1390 .cursor_set = nv50_crtc_cursor_set,
1391 .cursor_move = nv50_crtc_cursor_move,
1392 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001393 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001394 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001395 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001396};
1397
1398static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001399nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001400{
Ben Skeggse225f442012-11-21 14:40:21 +10001401 struct nv50_disp *disp = nv50_disp(dev);
1402 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001403 struct drm_crtc *crtc;
1404 int ret, i;
1405
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001406 head = kzalloc(sizeof(*head), GFP_KERNEL);
1407 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001408 return -ENOMEM;
1409
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001410 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001411 head->base.set_dither = nv50_crtc_set_dither;
1412 head->base.set_scale = nv50_crtc_set_scale;
1413 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001414 head->base.color_vibrance = 50;
1415 head->base.vibrant_hue = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001416 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001417 head->base.lut.r[i] = i << 8;
1418 head->base.lut.g[i] = i << 8;
1419 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001420 }
1421
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001422 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001423 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1424 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001425 drm_mode_crtc_set_gamma_size(crtc, 256);
1426
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001427 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01001428 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001429 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10001430 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001431 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001432 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001433 if (ret)
1434 nouveau_bo_unpin(head->base.lut.nvbo);
1435 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001436 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001437 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001438 }
1439
1440 if (ret)
1441 goto out;
1442
Ben Skeggse225f442012-11-21 14:40:21 +10001443 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001444
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001445 /* allocate cursor resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001446 ret = nv50_curs_create(disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001447 if (ret)
1448 goto out;
1449
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001450 /* allocate page flip / sync resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001451 ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
1452 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001453 if (ret)
1454 goto out;
1455
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001456 head->sync.addr = EVO_FLIP_SEM0(index);
1457 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001458
1459 /* allocate overlay resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001460 ret = nv50_oimm_create(disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001461 if (ret)
1462 goto out;
1463
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001464 ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
1465 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001466 if (ret)
1467 goto out;
1468
Ben Skeggs438d99e2011-07-05 16:48:06 +10001469out:
1470 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001471 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001472 return ret;
1473}
1474
1475/******************************************************************************
Ben Skeggsa91d3222014-12-22 16:30:13 +10001476 * Encoder helpers
1477 *****************************************************************************/
1478static bool
1479nv50_encoder_mode_fixup(struct drm_encoder *encoder,
1480 const struct drm_display_mode *mode,
1481 struct drm_display_mode *adjusted_mode)
1482{
1483 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1484 struct nouveau_connector *nv_connector;
1485
1486 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1487 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs576f7912014-12-22 17:19:26 +10001488 nv_connector->scaling_full = false;
1489 if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) {
1490 switch (nv_connector->type) {
1491 case DCB_CONNECTOR_LVDS:
1492 case DCB_CONNECTOR_LVDS_SPWG:
1493 case DCB_CONNECTOR_eDP:
1494 /* force use of scaler for non-edid modes */
1495 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
1496 return true;
1497 nv_connector->scaling_full = true;
1498 break;
1499 default:
1500 return true;
1501 }
1502 }
1503
1504 drm_mode_copy(adjusted_mode, nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10001505 }
1506
1507 return true;
1508}
1509
1510/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001511 * DAC
1512 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001513static void
Ben Skeggse225f442012-11-21 14:40:21 +10001514nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001515{
1516 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001517 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001518 struct {
1519 struct nv50_disp_mthd_v1 base;
1520 struct nv50_disp_dac_pwr_v0 pwr;
1521 } args = {
1522 .base.version = 1,
1523 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1524 .base.hasht = nv_encoder->dcb->hasht,
1525 .base.hashm = nv_encoder->dcb->hashm,
1526 .pwr.state = 1,
1527 .pwr.data = 1,
1528 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1529 mode != DRM_MODE_DPMS_OFF),
1530 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1531 mode != DRM_MODE_DPMS_OFF),
1532 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001533
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001534 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001535}
1536
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001537static void
Ben Skeggse225f442012-11-21 14:40:21 +10001538nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001539{
1540}
1541
1542static void
Ben Skeggse225f442012-11-21 14:40:21 +10001543nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001544 struct drm_display_mode *adjusted_mode)
1545{
Ben Skeggse225f442012-11-21 14:40:21 +10001546 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001547 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1548 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001549 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001550
Ben Skeggse225f442012-11-21 14:40:21 +10001551 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001552
Ben Skeggs97b19b52012-11-16 11:21:37 +10001553 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001554 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001555 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001556 u32 syncs = 0x00000000;
1557
1558 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1559 syncs |= 0x00000001;
1560 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1561 syncs |= 0x00000002;
1562
1563 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1564 evo_data(push, 1 << nv_crtc->index);
1565 evo_data(push, syncs);
1566 } else {
1567 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1568 u32 syncs = 0x00000001;
1569
1570 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1571 syncs |= 0x00000008;
1572 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1573 syncs |= 0x00000010;
1574
1575 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1576 magic |= 0x00000001;
1577
1578 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1579 evo_data(push, syncs);
1580 evo_data(push, magic);
1581 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1582 evo_data(push, 1 << nv_crtc->index);
1583 }
1584
1585 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001586 }
1587
1588 nv_encoder->crtc = encoder->crtc;
1589}
1590
1591static void
Ben Skeggse225f442012-11-21 14:40:21 +10001592nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001593{
1594 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001595 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001596 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001597 u32 *push;
1598
1599 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001600 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001601
Ben Skeggs97b19b52012-11-16 11:21:37 +10001602 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001603 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001604 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001605 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1606 evo_data(push, 0x00000000);
1607 } else {
1608 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1609 evo_data(push, 0x00000000);
1610 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001611 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001612 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001613 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001614
1615 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001616}
1617
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001618static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001619nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001620{
Ben Skeggsc4abd312014-08-10 04:10:26 +10001621 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001622 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10001623 struct {
1624 struct nv50_disp_mthd_v1 base;
1625 struct nv50_disp_dac_load_v0 load;
1626 } args = {
1627 .base.version = 1,
1628 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
1629 .base.hasht = nv_encoder->dcb->hasht,
1630 .base.hashm = nv_encoder->dcb->hashm,
1631 };
1632 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10001633
Ben Skeggsc4abd312014-08-10 04:10:26 +10001634 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
1635 if (args.load.data == 0)
1636 args.load.data = 340;
1637
1638 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
1639 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001640 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001641
Ben Skeggs35b21d32012-11-08 12:08:55 +10001642 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001643}
1644
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001645static void
Ben Skeggse225f442012-11-21 14:40:21 +10001646nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001647{
1648 drm_encoder_cleanup(encoder);
1649 kfree(encoder);
1650}
1651
Ben Skeggse225f442012-11-21 14:40:21 +10001652static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1653 .dpms = nv50_dac_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10001654 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggse225f442012-11-21 14:40:21 +10001655 .prepare = nv50_dac_disconnect,
1656 .commit = nv50_dac_commit,
1657 .mode_set = nv50_dac_mode_set,
1658 .disable = nv50_dac_disconnect,
1659 .get_crtc = nv50_display_crtc_get,
1660 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001661};
1662
Ben Skeggse225f442012-11-21 14:40:21 +10001663static const struct drm_encoder_funcs nv50_dac_func = {
1664 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001665};
1666
1667static int
Ben Skeggse225f442012-11-21 14:40:21 +10001668nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001669{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001670 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001671 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001672 struct nouveau_encoder *nv_encoder;
1673 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001674 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001675
1676 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1677 if (!nv_encoder)
1678 return -ENOMEM;
1679 nv_encoder->dcb = dcbe;
1680 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001681 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001682
1683 encoder = to_drm_encoder(nv_encoder);
1684 encoder->possible_crtcs = dcbe->heads;
1685 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001686 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001687 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001688
1689 drm_mode_connector_attach_encoder(connector, encoder);
1690 return 0;
1691}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001692
1693/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001694 * Audio
1695 *****************************************************************************/
1696static void
Ben Skeggse225f442012-11-21 14:40:21 +10001697nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001698{
1699 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10001700 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10001701 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001702 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10001703 struct __packed {
1704 struct {
1705 struct nv50_disp_mthd_v1 mthd;
1706 struct nv50_disp_sor_hda_eld_v0 eld;
1707 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10001708 u8 data[sizeof(nv_connector->base.eld)];
1709 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10001710 .base.mthd.version = 1,
1711 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1712 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10001713 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1714 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10001715 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001716
1717 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1718 if (!drm_detect_monitor_audio(nv_connector->edid))
1719 return;
1720
Ben Skeggs78951d22011-11-11 18:13:13 +10001721 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001722 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001723
Jani Nikula938fd8a2014-10-28 16:20:48 +02001724 nvif_mthd(disp->disp, 0, &args,
1725 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001726}
1727
1728static void
Ben Skeggscc2a9072014-09-15 21:29:05 +10001729nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001730{
1731 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001732 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001733 struct {
1734 struct nv50_disp_mthd_v1 base;
1735 struct nv50_disp_sor_hda_eld_v0 eld;
1736 } args = {
1737 .base.version = 1,
1738 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1739 .base.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10001740 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1741 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10001742 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001743
Ben Skeggs120b0c32014-08-10 04:10:26 +10001744 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001745}
1746
1747/******************************************************************************
1748 * HDMI
1749 *****************************************************************************/
1750static void
Ben Skeggse225f442012-11-21 14:40:21 +10001751nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001752{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001753 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1754 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001755 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001756 struct {
1757 struct nv50_disp_mthd_v1 base;
1758 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1759 } args = {
1760 .base.version = 1,
1761 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1762 .base.hasht = nv_encoder->dcb->hasht,
1763 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1764 (0x0100 << nv_crtc->index),
1765 .pwr.state = 1,
1766 .pwr.rekey = 56, /* binary driver, and tegra, constant */
1767 };
1768 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001769 u32 max_ac_packet;
1770
1771 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1772 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1773 return;
1774
1775 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10001776 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001777 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10001778 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001779
Ben Skeggse00f2232014-08-10 04:10:26 +10001780 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10001781 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001782}
1783
1784static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001785nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001786{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001787 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001788 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001789 struct {
1790 struct nv50_disp_mthd_v1 base;
1791 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1792 } args = {
1793 .base.version = 1,
1794 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1795 .base.hasht = nv_encoder->dcb->hasht,
1796 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1797 (0x0100 << nv_crtc->index),
1798 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001799
Ben Skeggse00f2232014-08-10 04:10:26 +10001800 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001801}
1802
1803/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001804 * SOR
1805 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001806static void
Ben Skeggse225f442012-11-21 14:40:21 +10001807nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001808{
1809 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001810 struct nv50_disp *disp = nv50_disp(encoder->dev);
1811 struct {
1812 struct nv50_disp_mthd_v1 base;
1813 struct nv50_disp_sor_pwr_v0 pwr;
1814 } args = {
1815 .base.version = 1,
1816 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
1817 .base.hasht = nv_encoder->dcb->hasht,
1818 .base.hashm = nv_encoder->dcb->hashm,
1819 .pwr.state = mode == DRM_MODE_DPMS_ON,
1820 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001821 struct {
1822 struct nv50_disp_mthd_v1 base;
1823 struct nv50_disp_sor_dp_pwr_v0 pwr;
1824 } link = {
1825 .base.version = 1,
1826 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
1827 .base.hasht = nv_encoder->dcb->hasht,
1828 .base.hashm = nv_encoder->dcb->hashm,
1829 .pwr.state = mode == DRM_MODE_DPMS_ON,
1830 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10001831 struct drm_device *dev = encoder->dev;
1832 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001833
1834 nv_encoder->last_dpms = mode;
1835
1836 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1837 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1838
1839 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1840 continue;
1841
1842 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001843 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001844 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1845 return;
1846 break;
1847 }
1848 }
1849
Ben Skeggs48743222014-05-31 01:48:06 +10001850 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001851 args.pwr.state = 1;
1852 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001853 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10001854 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001855 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10001856 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001857}
1858
Ben Skeggs83fc0832011-07-05 13:08:40 +10001859static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001860nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
1861{
1862 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
1863 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
1864 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001865 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001866 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
1867 evo_data(push, (nv_encoder->ctrl = temp));
1868 } else {
1869 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1870 evo_data(push, (nv_encoder->ctrl = temp));
1871 }
1872 evo_kick(push, mast);
1873 }
1874}
1875
1876static void
Ben Skeggse225f442012-11-21 14:40:21 +10001877nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001878{
1879 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001880 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001881
1882 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1883 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001884
1885 if (nv_crtc) {
1886 nv50_crtc_prepare(&nv_crtc->base);
1887 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggscc2a9072014-09-15 21:29:05 +10001888 nv50_audio_disconnect(encoder, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001889 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
1890 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001891}
1892
1893static void
Ben Skeggse225f442012-11-21 14:40:21 +10001894nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001895{
1896}
1897
1898static void
Ben Skeggse225f442012-11-21 14:40:21 +10001899nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001900 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001901{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001902 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1903 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1904 struct {
1905 struct nv50_disp_mthd_v1 base;
1906 struct nv50_disp_sor_lvds_script_v0 lvds;
1907 } lvds = {
1908 .base.version = 1,
1909 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1910 .base.hasht = nv_encoder->dcb->hasht,
1911 .base.hashm = nv_encoder->dcb->hashm,
1912 };
Ben Skeggse225f442012-11-21 14:40:21 +10001913 struct nv50_disp *disp = nv50_disp(encoder->dev);
1914 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001915 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001916 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001917 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001918 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001919 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001920 u8 owner = 1 << nv_crtc->index;
1921 u8 proto = 0xf;
1922 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001923
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001924 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001925 nv_encoder->crtc = encoder->crtc;
1926
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001927 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001928 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001929 if (nv_encoder->dcb->sorconf.link & 1) {
1930 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001931 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001932 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001933 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001934 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001935 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001936 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001937
Ben Skeggse84a35a2014-06-05 10:59:55 +10001938 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001939 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001940 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001941 proto = 0x0;
1942
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001943 if (bios->fp_no_ddc) {
1944 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001945 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001946 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001947 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001948 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001949 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001950 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001951 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001952 } else
1953 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001954 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001955 }
1956
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001957 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001958 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001959 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001960 } else {
1961 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001962 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001963 }
1964
1965 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001966 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001967 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001968
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001969 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001970 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001971 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001972 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001973 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001974 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001975 } else
1976 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001977 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001978 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001979 } else {
1980 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1981 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001982 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001983
1984 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001985 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001986 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001987 proto = 0x9;
Ben Skeggs3eee8642014-09-15 15:20:47 +10001988 nv50_audio_mode_set(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001989 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001990 default:
1991 BUG_ON(1);
1992 break;
1993 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001994
Ben Skeggse84a35a2014-06-05 10:59:55 +10001995 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001996
Ben Skeggs648d4df2014-08-10 04:10:27 +10001997 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001998 u32 *push = evo_wait(mast, 3);
1999 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002000 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2001 u32 syncs = 0x00000001;
2002
2003 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2004 syncs |= 0x00000008;
2005 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2006 syncs |= 0x00000010;
2007
2008 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2009 magic |= 0x00000001;
2010
2011 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2012 evo_data(push, syncs | (depth << 6));
2013 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002014 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002015 }
2016
Ben Skeggse84a35a2014-06-05 10:59:55 +10002017 ctrl = proto << 8;
2018 mask = 0x00000f00;
2019 } else {
2020 ctrl = (depth << 16) | (proto << 8);
2021 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2022 ctrl |= 0x00001000;
2023 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2024 ctrl |= 0x00002000;
2025 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002026 }
2027
Ben Skeggse84a35a2014-06-05 10:59:55 +10002028 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002029}
2030
2031static void
Ben Skeggse225f442012-11-21 14:40:21 +10002032nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002033{
2034 drm_encoder_cleanup(encoder);
2035 kfree(encoder);
2036}
2037
Ben Skeggse225f442012-11-21 14:40:21 +10002038static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2039 .dpms = nv50_sor_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10002040 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10002041 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10002042 .commit = nv50_sor_commit,
2043 .mode_set = nv50_sor_mode_set,
2044 .disable = nv50_sor_disconnect,
2045 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002046};
2047
Ben Skeggse225f442012-11-21 14:40:21 +10002048static const struct drm_encoder_funcs nv50_sor_func = {
2049 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002050};
2051
2052static int
Ben Skeggse225f442012-11-21 14:40:21 +10002053nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002054{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002055 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002056 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002057 struct nouveau_encoder *nv_encoder;
2058 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002059 int type;
2060
2061 switch (dcbe->type) {
2062 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2063 case DCB_OUTPUT_TMDS:
2064 case DCB_OUTPUT_DP:
2065 default:
2066 type = DRM_MODE_ENCODER_TMDS;
2067 break;
2068 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002069
2070 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2071 if (!nv_encoder)
2072 return -ENOMEM;
2073 nv_encoder->dcb = dcbe;
2074 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002075 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002076 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2077
2078 encoder = to_drm_encoder(nv_encoder);
2079 encoder->possible_crtcs = dcbe->heads;
2080 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002081 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10002082 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002083
2084 drm_mode_connector_attach_encoder(connector, encoder);
2085 return 0;
2086}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002087
2088/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002089 * PIOR
2090 *****************************************************************************/
2091
2092static void
2093nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2094{
2095 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2096 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10002097 struct {
2098 struct nv50_disp_mthd_v1 base;
2099 struct nv50_disp_pior_pwr_v0 pwr;
2100 } args = {
2101 .base.version = 1,
2102 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2103 .base.hasht = nv_encoder->dcb->hasht,
2104 .base.hashm = nv_encoder->dcb->hashm,
2105 .pwr.state = mode == DRM_MODE_DPMS_ON,
2106 .pwr.type = nv_encoder->dcb->type,
2107 };
2108
2109 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002110}
2111
2112static bool
2113nv50_pior_mode_fixup(struct drm_encoder *encoder,
2114 const struct drm_display_mode *mode,
2115 struct drm_display_mode *adjusted_mode)
2116{
Ben Skeggsa91d3222014-12-22 16:30:13 +10002117 if (!nv50_encoder_mode_fixup(encoder, mode, adjusted_mode))
2118 return false;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002119 adjusted_mode->clock *= 2;
2120 return true;
2121}
2122
2123static void
2124nv50_pior_commit(struct drm_encoder *encoder)
2125{
2126}
2127
2128static void
2129nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2130 struct drm_display_mode *adjusted_mode)
2131{
2132 struct nv50_mast *mast = nv50_mast(encoder->dev);
2133 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2134 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2135 struct nouveau_connector *nv_connector;
2136 u8 owner = 1 << nv_crtc->index;
2137 u8 proto, depth;
2138 u32 *push;
2139
2140 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2141 switch (nv_connector->base.display_info.bpc) {
2142 case 10: depth = 0x6; break;
2143 case 8: depth = 0x5; break;
2144 case 6: depth = 0x2; break;
2145 default: depth = 0x0; break;
2146 }
2147
2148 switch (nv_encoder->dcb->type) {
2149 case DCB_OUTPUT_TMDS:
2150 case DCB_OUTPUT_DP:
2151 proto = 0x0;
2152 break;
2153 default:
2154 BUG_ON(1);
2155 break;
2156 }
2157
2158 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2159
2160 push = evo_wait(mast, 8);
2161 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002162 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002163 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2164 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2165 ctrl |= 0x00001000;
2166 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2167 ctrl |= 0x00002000;
2168 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2169 evo_data(push, ctrl);
2170 }
2171
2172 evo_kick(push, mast);
2173 }
2174
2175 nv_encoder->crtc = encoder->crtc;
2176}
2177
2178static void
2179nv50_pior_disconnect(struct drm_encoder *encoder)
2180{
2181 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2182 struct nv50_mast *mast = nv50_mast(encoder->dev);
2183 const int or = nv_encoder->or;
2184 u32 *push;
2185
2186 if (nv_encoder->crtc) {
2187 nv50_crtc_prepare(nv_encoder->crtc);
2188
2189 push = evo_wait(mast, 4);
2190 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002191 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002192 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2193 evo_data(push, 0x00000000);
2194 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002195 evo_kick(push, mast);
2196 }
2197 }
2198
2199 nv_encoder->crtc = NULL;
2200}
2201
2202static void
2203nv50_pior_destroy(struct drm_encoder *encoder)
2204{
2205 drm_encoder_cleanup(encoder);
2206 kfree(encoder);
2207}
2208
2209static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2210 .dpms = nv50_pior_dpms,
2211 .mode_fixup = nv50_pior_mode_fixup,
2212 .prepare = nv50_pior_disconnect,
2213 .commit = nv50_pior_commit,
2214 .mode_set = nv50_pior_mode_set,
2215 .disable = nv50_pior_disconnect,
2216 .get_crtc = nv50_display_crtc_get,
2217};
2218
2219static const struct drm_encoder_funcs nv50_pior_func = {
2220 .destroy = nv50_pior_destroy,
2221};
2222
2223static int
2224nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2225{
2226 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002227 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002228 struct nouveau_i2c_port *ddc = NULL;
2229 struct nouveau_encoder *nv_encoder;
2230 struct drm_encoder *encoder;
2231 int type;
2232
2233 switch (dcbe->type) {
2234 case DCB_OUTPUT_TMDS:
2235 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2236 type = DRM_MODE_ENCODER_TMDS;
2237 break;
2238 case DCB_OUTPUT_DP:
2239 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2240 type = DRM_MODE_ENCODER_TMDS;
2241 break;
2242 default:
2243 return -ENODEV;
2244 }
2245
2246 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2247 if (!nv_encoder)
2248 return -ENOMEM;
2249 nv_encoder->dcb = dcbe;
2250 nv_encoder->or = ffs(dcbe->or) - 1;
2251 nv_encoder->i2c = ddc;
2252
2253 encoder = to_drm_encoder(nv_encoder);
2254 encoder->possible_crtcs = dcbe->heads;
2255 encoder->possible_clones = 0;
2256 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2257 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2258
2259 drm_mode_connector_attach_encoder(connector, encoder);
2260 return 0;
2261}
2262
2263/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002264 * Framebuffer
2265 *****************************************************************************/
2266
Ben Skeggs8a423642014-08-10 04:10:19 +10002267static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002268nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002269{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002270 int i;
2271 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2272 nvif_object_fini(&fbdma->base[i]);
2273 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002274 list_del(&fbdma->head);
2275 kfree(fbdma);
2276}
2277
2278static int
2279nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2280{
2281 struct nouveau_drm *drm = nouveau_drm(dev);
2282 struct nv50_disp *disp = nv50_disp(dev);
2283 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002284 struct __attribute__ ((packed)) {
2285 struct nv_dma_v0 base;
2286 union {
2287 struct nv50_dma_v0 nv50;
2288 struct gf100_dma_v0 gf100;
2289 struct gf110_dma_v0 gf110;
2290 };
2291 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002292 struct nv50_fbdma *fbdma;
2293 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002294 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002295 int ret;
2296
2297 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002298 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002299 return 0;
2300 }
2301
2302 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2303 if (!fbdma)
2304 return -ENOMEM;
2305 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002306
Ben Skeggs4acfd702014-08-10 04:10:24 +10002307 args.base.target = NV_DMA_V0_TARGET_VRAM;
2308 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2309 args.base.start = offset;
2310 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002311
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002312 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002313 args.nv50.part = NV50_DMA_V0_PART_256;
2314 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002315 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002316 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002317 args.nv50.part = NV50_DMA_V0_PART_256;
2318 args.nv50.kind = kind;
2319 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002320 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002321 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002322 args.gf100.kind = kind;
2323 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002324 } else {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002325 args.gf110.page = GF110_DMA_V0_PAGE_LP;
2326 args.gf110.kind = kind;
2327 size += sizeof(args.gf110);
Ben Skeggs8a423642014-08-10 04:10:19 +10002328 }
2329
2330 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002331 struct nv50_head *head = nv50_head(crtc);
2332 int ret = nvif_object_init(&head->sync.base.base.user, NULL,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002333 name, NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002334 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002335 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002336 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002337 return ret;
2338 }
2339 }
2340
Ben Skeggs0ad72862014-08-10 04:10:22 +10002341 ret = nvif_object_init(&mast->base.base.user, NULL, name,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002342 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002343 &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002344 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002345 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002346 return ret;
2347 }
2348
2349 return 0;
2350}
2351
Ben Skeggsab0af552014-08-10 04:10:19 +10002352static void
2353nv50_fb_dtor(struct drm_framebuffer *fb)
2354{
2355}
2356
2357static int
2358nv50_fb_ctor(struct drm_framebuffer *fb)
2359{
2360 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2361 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2362 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002363 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002364 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2365 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002366
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002367 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002368 tile >>= 4; /* yep.. */
2369
Ben Skeggsab0af552014-08-10 04:10:19 +10002370 switch (fb->depth) {
2371 case 8: nv_fb->r_format = 0x1e00; break;
2372 case 15: nv_fb->r_format = 0xe900; break;
2373 case 16: nv_fb->r_format = 0xe800; break;
2374 case 24:
2375 case 32: nv_fb->r_format = 0xcf00; break;
2376 case 30: nv_fb->r_format = 0xd100; break;
2377 default:
2378 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2379 return -EINVAL;
2380 }
2381
Ben Skeggs648d4df2014-08-10 04:10:27 +10002382 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002383 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2384 (fb->pitches[0] | 0x00100000);
2385 nv_fb->r_format |= kind << 16;
2386 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10002387 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002388 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2389 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002390 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002391 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2392 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002393 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002394 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002395
Ben Skeggsf392ec42014-08-10 04:10:28 +10002396 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
2397 drm->device.info.ram_user, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002398}
2399
2400/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002401 * Init
2402 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002403
Ben Skeggs2a44e492011-11-09 11:36:33 +10002404void
Ben Skeggse225f442012-11-21 14:40:21 +10002405nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002406{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002407}
2408
2409int
Ben Skeggse225f442012-11-21 14:40:21 +10002410nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002411{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002412 struct nv50_disp *disp = nv50_disp(dev);
2413 struct drm_crtc *crtc;
2414 u32 *push;
2415
2416 push = evo_wait(nv50_mast(dev), 32);
2417 if (!push)
2418 return -EBUSY;
2419
2420 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2421 struct nv50_sync *sync = nv50_sync(crtc);
2422 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002423 }
2424
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002425 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10002426 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002427 evo_kick(push, nv50_mast(dev));
2428 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002429}
2430
2431void
Ben Skeggse225f442012-11-21 14:40:21 +10002432nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002433{
Ben Skeggse225f442012-11-21 14:40:21 +10002434 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002435 struct nv50_fbdma *fbdma, *fbtmp;
2436
2437 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002438 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002439 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002440
Ben Skeggs0ad72862014-08-10 04:10:22 +10002441 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002442
Ben Skeggs816af2f2011-11-16 15:48:48 +10002443 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002444 if (disp->sync)
2445 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002446 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002447
Ben Skeggs77145f12012-07-31 16:16:21 +10002448 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002449 kfree(disp);
2450}
2451
2452int
Ben Skeggse225f442012-11-21 14:40:21 +10002453nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002454{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002455 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002456 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002457 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002458 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002459 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002460 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002461 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002462
2463 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2464 if (!disp)
2465 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10002466 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10002467
2468 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002469 nouveau_display(dev)->dtor = nv50_display_destroy;
2470 nouveau_display(dev)->init = nv50_display_init;
2471 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10002472 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2473 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002474 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002475
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002476 /* small shared memory area we use for notifiers and semaphores */
2477 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002478 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002479 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002480 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002481 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002482 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002483 if (ret)
2484 nouveau_bo_unpin(disp->sync);
2485 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002486 if (ret)
2487 nouveau_bo_ref(NULL, &disp->sync);
2488 }
2489
2490 if (ret)
2491 goto out;
2492
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002493 /* allocate master evo channel */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10002494 ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
2495 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002496 if (ret)
2497 goto out;
2498
Ben Skeggs438d99e2011-07-05 16:48:06 +10002499 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10002500 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsdb2bec12014-08-10 04:10:22 +10002501 crtcs = nvif_rd32(device, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10002502 else
2503 crtcs = 2;
2504
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002505 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002506 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002507 if (ret)
2508 goto out;
2509 }
2510
Ben Skeggs83fc0832011-07-05 13:08:40 +10002511 /* create encoder/connector objects based on VBIOS DCB table */
2512 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2513 connector = nouveau_connector_create(dev, dcbe->connector);
2514 if (IS_ERR(connector))
2515 continue;
2516
Ben Skeggseb6313a2013-02-11 09:52:58 +10002517 if (dcbe->location == DCB_LOC_ON_CHIP) {
2518 switch (dcbe->type) {
2519 case DCB_OUTPUT_TMDS:
2520 case DCB_OUTPUT_LVDS:
2521 case DCB_OUTPUT_DP:
2522 ret = nv50_sor_create(connector, dcbe);
2523 break;
2524 case DCB_OUTPUT_ANALOG:
2525 ret = nv50_dac_create(connector, dcbe);
2526 break;
2527 default:
2528 ret = -ENODEV;
2529 break;
2530 }
2531 } else {
2532 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002533 }
2534
Ben Skeggseb6313a2013-02-11 09:52:58 +10002535 if (ret) {
2536 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2537 dcbe->location, dcbe->type,
2538 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002539 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002540 }
2541 }
2542
2543 /* cull any connectors we created that don't have an encoder */
2544 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2545 if (connector->encoder_ids[0])
2546 continue;
2547
Ben Skeggs77145f12012-07-31 16:16:21 +10002548 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002549 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002550 connector->funcs->destroy(connector);
2551 }
2552
Ben Skeggs26f6d882011-07-04 16:25:18 +10002553out:
2554 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002555 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002556 return ret;
2557}