blob: 85cc667cfb0f0311143ad4cfff03b8e6fa6987e3 [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
553 swap_interval <<= 4;
554 if (swap_interval == 0)
555 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000556 if (chan == NULL)
557 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000558
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000559 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000560 if (unlikely(push == NULL))
561 return -EBUSY;
562
Ben Skeggsbbf89062014-08-10 04:10:25 +1000563 if (chan && chan->object->oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000564 ret = RING_SPACE(chan, 8);
565 if (ret)
566 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000567
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000568 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000569 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000570 OUT_RING (chan, sync->addr ^ 0x10);
571 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
572 OUT_RING (chan, sync->data + 1);
573 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
574 OUT_RING (chan, sync->addr);
575 OUT_RING (chan, sync->data);
576 } else
Ben Skeggsbbf89062014-08-10 04:10:25 +1000577 if (chan && chan->object->oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000578 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000579 ret = RING_SPACE(chan, 12);
580 if (ret)
581 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000582
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000583 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000584 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000585 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
586 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
587 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
588 OUT_RING (chan, sync->data + 1);
589 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
590 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
591 OUT_RING (chan, upper_32_bits(addr));
592 OUT_RING (chan, lower_32_bits(addr));
593 OUT_RING (chan, sync->data);
594 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
595 } else
596 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000597 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000598 ret = RING_SPACE(chan, 10);
599 if (ret)
600 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000601
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000602 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
603 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
604 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
605 OUT_RING (chan, sync->data + 1);
606 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
607 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
608 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
609 OUT_RING (chan, upper_32_bits(addr));
610 OUT_RING (chan, lower_32_bits(addr));
611 OUT_RING (chan, sync->data);
612 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
613 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
614 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500615
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000616 if (chan) {
617 sync->addr ^= 0x10;
618 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000619 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000620 }
621
622 /* queue the flip */
623 evo_mthd(push, 0x0100, 1);
624 evo_data(push, 0xfffe0000);
625 evo_mthd(push, 0x0084, 1);
626 evo_data(push, swap_interval);
627 if (!(swap_interval & 0x00000100)) {
628 evo_mthd(push, 0x00e0, 1);
629 evo_data(push, 0x40000000);
630 }
631 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000632 evo_data(push, sync->addr);
633 evo_data(push, sync->data++);
634 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000635 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000636 evo_mthd(push, 0x00a0, 2);
637 evo_data(push, 0x00000000);
638 evo_data(push, 0x00000000);
639 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000640 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000641 evo_mthd(push, 0x0110, 2);
642 evo_data(push, 0x00000000);
643 evo_data(push, 0x00000000);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000644 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000645 evo_mthd(push, 0x0800, 5);
646 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
647 evo_data(push, 0);
648 evo_data(push, (fb->height << 16) | fb->width);
649 evo_data(push, nv_fb->r_pitch);
650 evo_data(push, nv_fb->r_format);
651 } else {
652 evo_mthd(push, 0x0400, 5);
653 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
654 evo_data(push, 0);
655 evo_data(push, (fb->height << 16) | fb->width);
656 evo_data(push, nv_fb->r_pitch);
657 evo_data(push, nv_fb->r_format);
658 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000659 evo_mthd(push, 0x0080, 1);
660 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000661 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000662
663 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000664 return 0;
665}
666
Ben Skeggs26f6d882011-07-04 16:25:18 +1000667/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000668 * CRTC
669 *****************************************************************************/
670static int
Ben Skeggse225f442012-11-21 14:40:21 +1000671nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000672{
Ben Skeggse225f442012-11-21 14:40:21 +1000673 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000674 struct nouveau_connector *nv_connector;
675 struct drm_connector *connector;
676 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000677
Ben Skeggs488ff202011-10-17 10:38:10 +1000678 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000679 connector = &nv_connector->base;
680 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -0700681 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +1000682 mode = DITHERING_MODE_DYNAMIC2X2;
683 } else {
684 mode = nv_connector->dithering_mode;
685 }
686
687 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
688 if (connector->display_info.bpc >= 8)
689 mode |= DITHERING_DEPTH_8BPC;
690 } else {
691 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000692 }
693
Ben Skeggsde8268c2012-11-16 10:24:31 +1000694 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000695 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000696 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000697 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
698 evo_data(push, mode);
699 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000700 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000701 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
702 evo_data(push, mode);
703 } else {
704 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
705 evo_data(push, mode);
706 }
707
Ben Skeggs438d99e2011-07-05 16:48:06 +1000708 if (update) {
709 evo_mthd(push, 0x0080, 1);
710 evo_data(push, 0x00000000);
711 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000712 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000713 }
714
715 return 0;
716}
717
718static int
Ben Skeggse225f442012-11-21 14:40:21 +1000719nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000720{
Ben Skeggse225f442012-11-21 14:40:21 +1000721 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000722 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000723 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000724 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000725 int mode = DRM_MODE_SCALE_NONE;
726 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000727
Ben Skeggs92854622011-11-11 23:49:06 +1000728 /* start off at the resolution we programmed the crtc for, this
729 * effectively handles NONE/FULL scaling
730 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000731 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs576f7912014-12-22 17:19:26 +1000732 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs92854622011-11-11 23:49:06 +1000733 mode = nv_connector->scaling_mode;
Ben Skeggs576f7912014-12-22 17:19:26 +1000734 if (nv_connector->scaling_full) /* non-EDID LVDS/eDP mode */
735 mode = DRM_MODE_SCALE_FULLSCREEN;
736 }
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000737
Ben Skeggs92854622011-11-11 23:49:06 +1000738 if (mode != DRM_MODE_SCALE_NONE)
739 omode = nv_connector->native_mode;
740 else
741 omode = umode;
742
743 oX = omode->hdisplay;
744 oY = omode->vdisplay;
745 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
746 oY *= 2;
747
748 /* add overscan compensation if necessary, will keep the aspect
749 * ratio the same as the backend mode unless overridden by the
750 * user setting both hborder and vborder properties.
751 */
752 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
753 (nv_connector->underscan == UNDERSCAN_AUTO &&
754 nv_connector->edid &&
755 drm_detect_hdmi_monitor(nv_connector->edid)))) {
756 u32 bX = nv_connector->underscan_hborder;
757 u32 bY = nv_connector->underscan_vborder;
758 u32 aspect = (oY << 19) / oX;
759
760 if (bX) {
761 oX -= (bX * 2);
762 if (bY) oY -= (bY * 2);
763 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
764 } else {
765 oX -= (oX >> 4) + 32;
766 if (bY) oY -= (bY * 2);
767 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000768 }
769 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000770
Ben Skeggs92854622011-11-11 23:49:06 +1000771 /* handle CENTER/ASPECT scaling, taking into account the areas
772 * removed already for overscan compensation
773 */
774 switch (mode) {
775 case DRM_MODE_SCALE_CENTER:
776 oX = min((u32)umode->hdisplay, oX);
777 oY = min((u32)umode->vdisplay, oY);
778 /* fall-through */
779 case DRM_MODE_SCALE_ASPECT:
780 if (oY < oX) {
781 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
782 oX = ((oY * aspect) + (aspect / 2)) >> 19;
783 } else {
784 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
785 oY = ((oX * aspect) + (aspect / 2)) >> 19;
786 }
787 break;
788 default:
789 break;
790 }
791
Ben Skeggsde8268c2012-11-16 10:24:31 +1000792 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000793 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000794 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000795 /*XXX: SCALE_CTRL_ACTIVE??? */
796 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
797 evo_data(push, (oY << 16) | oX);
798 evo_data(push, (oY << 16) | oX);
799 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
800 evo_data(push, 0x00000000);
801 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
802 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
803 } else {
804 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
805 evo_data(push, (oY << 16) | oX);
806 evo_data(push, (oY << 16) | oX);
807 evo_data(push, (oY << 16) | oX);
808 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
809 evo_data(push, 0x00000000);
810 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
811 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
812 }
813
814 evo_kick(push, mast);
815
Ben Skeggs3376ee32011-11-12 14:28:12 +1000816 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000817 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700818 nv50_display_flip_next(crtc, crtc->primary->fb,
819 NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000820 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000821 }
822
823 return 0;
824}
825
826static int
Roy Splieteae73822014-10-30 22:57:45 +0100827nv50_crtc_set_raster_vblank_dmi(struct nouveau_crtc *nv_crtc, u32 usec)
828{
829 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
830 u32 *push;
831
832 push = evo_wait(mast, 8);
833 if (!push)
834 return -ENOMEM;
835
836 evo_mthd(push, 0x0828 + (nv_crtc->index * 0x400), 1);
837 evo_data(push, usec);
838 evo_kick(push, mast);
839 return 0;
840}
841
842static int
Ben Skeggse225f442012-11-21 14:40:21 +1000843nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000844{
Ben Skeggse225f442012-11-21 14:40:21 +1000845 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000846 u32 *push, hue, vib;
847 int adj;
848
849 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
850 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
851 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
852
853 push = evo_wait(mast, 16);
854 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000855 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000856 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
857 evo_data(push, (hue << 20) | (vib << 8));
858 } else {
859 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
860 evo_data(push, (hue << 20) | (vib << 8));
861 }
862
863 if (update) {
864 evo_mthd(push, 0x0080, 1);
865 evo_data(push, 0x00000000);
866 }
867 evo_kick(push, mast);
868 }
869
870 return 0;
871}
872
873static int
Ben Skeggse225f442012-11-21 14:40:21 +1000874nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000875 int x, int y, bool update)
876{
877 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000878 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000879 u32 *push;
880
Ben Skeggsde8268c2012-11-16 10:24:31 +1000881 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000882 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000883 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000884 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
885 evo_data(push, nvfb->nvbo->bo.offset >> 8);
886 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
887 evo_data(push, (fb->height << 16) | fb->width);
888 evo_data(push, nvfb->r_pitch);
889 evo_data(push, nvfb->r_format);
890 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
891 evo_data(push, (y << 16) | x);
Ben Skeggs648d4df2014-08-10 04:10:27 +1000892 if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000893 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000894 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000895 }
896 } else {
897 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
898 evo_data(push, nvfb->nvbo->bo.offset >> 8);
899 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
900 evo_data(push, (fb->height << 16) | fb->width);
901 evo_data(push, nvfb->r_pitch);
902 evo_data(push, nvfb->r_format);
Ben Skeggs8a423642014-08-10 04:10:19 +1000903 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000904 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
905 evo_data(push, (y << 16) | x);
906 }
907
Ben Skeggsa46232e2011-07-07 15:23:48 +1000908 if (update) {
909 evo_mthd(push, 0x0080, 1);
910 evo_data(push, 0x00000000);
911 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000912 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000913 }
914
Ben Skeggs8a423642014-08-10 04:10:19 +1000915 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000916 return 0;
917}
918
919static void
Ben Skeggse225f442012-11-21 14:40:21 +1000920nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000921{
Ben Skeggse225f442012-11-21 14:40:21 +1000922 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs5a560252014-11-10 15:52:02 +1000923 struct nv50_curs *curs = nv50_curs(&nv_crtc->base);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000924 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000925 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000926 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000927 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
928 evo_data(push, 0x85000000);
Ben Skeggs5a560252014-11-10 15:52:02 +1000929 evo_data(push, curs->image->bo.offset >> 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000930 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000931 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000932 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
933 evo_data(push, 0x85000000);
Ben Skeggs5a560252014-11-10 15:52:02 +1000934 evo_data(push, curs->image->bo.offset >> 8);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000935 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000936 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000937 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000938 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
939 evo_data(push, 0x85000000);
Ben Skeggs5a560252014-11-10 15:52:02 +1000940 evo_data(push, curs->image->bo.offset >> 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000941 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000942 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000943 }
944 evo_kick(push, mast);
945 }
946}
947
948static void
Ben Skeggse225f442012-11-21 14:40:21 +1000949nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000950{
Ben Skeggse225f442012-11-21 14:40:21 +1000951 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000952 u32 *push = evo_wait(mast, 16);
953 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +1000954 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000955 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
956 evo_data(push, 0x05000000);
957 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +1000958 if (nv50_vers(mast) < GF110_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 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
962 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000963 } else {
964 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
965 evo_data(push, 0x05000000);
966 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
967 evo_data(push, 0x00000000);
968 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000969 evo_kick(push, mast);
970 }
971}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000972
Ben Skeggsde8268c2012-11-16 10:24:31 +1000973static void
Ben Skeggse225f442012-11-21 14:40:21 +1000974nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000975{
Ben Skeggse225f442012-11-21 14:40:21 +1000976 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs5a560252014-11-10 15:52:02 +1000977 struct nv50_curs *curs = nv50_curs(&nv_crtc->base);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000978
Ben Skeggs5a560252014-11-10 15:52:02 +1000979 if (show && curs->image)
Ben Skeggse225f442012-11-21 14:40:21 +1000980 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000981 else
Ben Skeggse225f442012-11-21 14:40:21 +1000982 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000983
984 if (update) {
985 u32 *push = evo_wait(mast, 2);
986 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000987 evo_mthd(push, 0x0080, 1);
988 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000989 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000990 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000991 }
992}
993
994static void
Ben Skeggse225f442012-11-21 14:40:21 +1000995nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000996{
997}
998
999static void
Ben Skeggse225f442012-11-21 14:40:21 +10001000nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001001{
1002 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001003 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001004 u32 *push;
1005
Ben Skeggse225f442012-11-21 14:40:21 +10001006 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +10001007
Ben Skeggs56d237d2014-05-19 14:54:33 +10001008 push = evo_wait(mast, 6);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001009 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001010 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001011 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1012 evo_data(push, 0x00000000);
1013 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
1014 evo_data(push, 0x40000000);
1015 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001016 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001017 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1018 evo_data(push, 0x00000000);
1019 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
1020 evo_data(push, 0x40000000);
1021 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
1022 evo_data(push, 0x00000000);
1023 } else {
1024 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
1025 evo_data(push, 0x00000000);
1026 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
1027 evo_data(push, 0x03000000);
1028 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
1029 evo_data(push, 0x00000000);
1030 }
1031
1032 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001033 }
1034
Ben Skeggse225f442012-11-21 14:40:21 +10001035 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001036}
1037
1038static void
Ben Skeggse225f442012-11-21 14:40:21 +10001039nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001040{
1041 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001042 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001043 u32 *push;
1044
Ben Skeggsde8268c2012-11-16 10:24:31 +10001045 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001046 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001047 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001048 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001049 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001050 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1051 evo_data(push, 0xc0000000);
1052 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1053 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10001054 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001055 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001056 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001057 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1058 evo_data(push, 0xc0000000);
1059 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1060 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001061 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001062 } else {
1063 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001064 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001065 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1066 evo_data(push, 0x83000000);
1067 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1068 evo_data(push, 0x00000000);
1069 evo_data(push, 0x00000000);
1070 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001071 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001072 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1073 evo_data(push, 0xffffff00);
1074 }
1075
1076 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001077 }
1078
Ben Skeggs5a560252014-11-10 15:52:02 +10001079 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Matt Roperf4510a22014-04-01 15:22:40 -07001080 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001081}
1082
1083static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001084nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001085 struct drm_display_mode *adjusted_mode)
1086{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001087 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001088 return true;
1089}
1090
1091static int
Ben Skeggse225f442012-11-21 14:40:21 +10001092nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001093{
Matt Roperf4510a22014-04-01 15:22:40 -07001094 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001095 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001096 int ret;
1097
Ben Skeggs547ad072014-11-10 12:35:06 +10001098 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001099 if (ret == 0) {
1100 if (head->image)
1101 nouveau_bo_unpin(head->image);
1102 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001103 }
1104
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001105 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001106}
1107
1108static int
Ben Skeggse225f442012-11-21 14:40:21 +10001109nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001110 struct drm_display_mode *mode, int x, int y,
1111 struct drm_framebuffer *old_fb)
1112{
Ben Skeggse225f442012-11-21 14:40:21 +10001113 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001114 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1115 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001116 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1117 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1118 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1119 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
Roy Spliet1dce6262014-09-12 18:00:13 +02001120 u32 vblan2e = 0, vblan2s = 1, vblankus = 0;
Ben Skeggs3488c572012-03-12 11:42:20 +10001121 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001122 int ret;
1123
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001124 hactive = mode->htotal;
1125 hsynce = mode->hsync_end - mode->hsync_start - 1;
1126 hbackp = mode->htotal - mode->hsync_end;
1127 hblanke = hsynce + hbackp;
1128 hfrontp = mode->hsync_start - mode->hdisplay;
1129 hblanks = mode->htotal - hfrontp - 1;
1130
1131 vactive = mode->vtotal * vscan / ilace;
1132 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1133 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1134 vblanke = vsynce + vbackp;
1135 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1136 vblanks = vactive - vfrontp - 1;
Roy Spliet1dce6262014-09-12 18:00:13 +02001137 /* XXX: Safe underestimate, even "0" works */
1138 vblankus = (vactive - mode->vdisplay - 2) * hactive;
1139 vblankus *= 1000;
1140 vblankus /= mode->clock;
1141
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001142 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1143 vblan2e = vactive + vsynce + vbackp;
1144 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1145 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001146 }
1147
Ben Skeggse225f442012-11-21 14:40:21 +10001148 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001149 if (ret)
1150 return ret;
1151
Ben Skeggsde8268c2012-11-16 10:24:31 +10001152 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001153 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001154 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001155 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1156 evo_data(push, 0x00800000 | mode->clock);
1157 evo_data(push, (ilace == 2) ? 2 : 0);
Roy Splieteae73822014-10-30 22:57:45 +01001158 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001159 evo_data(push, 0x00000000);
1160 evo_data(push, (vactive << 16) | hactive);
1161 evo_data(push, ( vsynce << 16) | hsynce);
1162 evo_data(push, (vblanke << 16) | hblanke);
1163 evo_data(push, (vblanks << 16) | hblanks);
1164 evo_data(push, (vblan2e << 16) | vblan2s);
Roy Splieteae73822014-10-30 22:57:45 +01001165 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001166 evo_data(push, 0x00000000);
1167 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1168 evo_data(push, 0x00000311);
1169 evo_data(push, 0x00000100);
1170 } else {
1171 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1172 evo_data(push, 0x00000000);
1173 evo_data(push, (vactive << 16) | hactive);
1174 evo_data(push, ( vsynce << 16) | hsynce);
1175 evo_data(push, (vblanke << 16) | hblanke);
1176 evo_data(push, (vblanks << 16) | hblanks);
1177 evo_data(push, (vblan2e << 16) | vblan2s);
1178 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1179 evo_data(push, 0x00000000); /* ??? */
1180 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1181 evo_data(push, mode->clock * 1000);
1182 evo_data(push, 0x00200000); /* ??? */
1183 evo_data(push, mode->clock * 1000);
1184 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1185 evo_data(push, 0x00000311);
1186 evo_data(push, 0x00000100);
1187 }
1188
1189 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001190 }
1191
1192 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001193 nv50_crtc_set_dither(nv_crtc, false);
1194 nv50_crtc_set_scale(nv_crtc, false);
Roy Splieteae73822014-10-30 22:57:45 +01001195
1196 /* G94 only accepts this after setting scale */
1197 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA)
1198 nv50_crtc_set_raster_vblank_dmi(nv_crtc, vblankus);
1199
Ben Skeggse225f442012-11-21 14:40:21 +10001200 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001201 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001202 return 0;
1203}
1204
1205static int
Ben Skeggse225f442012-11-21 14:40:21 +10001206nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001207 struct drm_framebuffer *old_fb)
1208{
Ben Skeggs77145f12012-07-31 16:16:21 +10001209 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001210 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1211 int ret;
1212
Matt Roperf4510a22014-04-01 15:22:40 -07001213 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001214 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001215 return 0;
1216 }
1217
Ben Skeggse225f442012-11-21 14:40:21 +10001218 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001219 if (ret)
1220 return ret;
1221
Ben Skeggse225f442012-11-21 14:40:21 +10001222 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001223 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1224 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001225 return 0;
1226}
1227
1228static int
Ben Skeggse225f442012-11-21 14:40:21 +10001229nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001230 struct drm_framebuffer *fb, int x, int y,
1231 enum mode_set_atomic state)
1232{
1233 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001234 nv50_display_flip_stop(crtc);
1235 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001236 return 0;
1237}
1238
1239static void
Ben Skeggse225f442012-11-21 14:40:21 +10001240nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001241{
Ben Skeggse225f442012-11-21 14:40:21 +10001242 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001243 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1244 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1245 int i;
1246
1247 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001248 u16 r = nv_crtc->lut.r[i] >> 2;
1249 u16 g = nv_crtc->lut.g[i] >> 2;
1250 u16 b = nv_crtc->lut.b[i] >> 2;
1251
Ben Skeggs648d4df2014-08-10 04:10:27 +10001252 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001253 writew(r + 0x0000, lut + (i * 0x08) + 0);
1254 writew(g + 0x0000, lut + (i * 0x08) + 2);
1255 writew(b + 0x0000, lut + (i * 0x08) + 4);
1256 } else {
1257 writew(r + 0x6000, lut + (i * 0x20) + 0);
1258 writew(g + 0x6000, lut + (i * 0x20) + 2);
1259 writew(b + 0x6000, lut + (i * 0x20) + 4);
1260 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001261 }
1262}
1263
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001264static void
1265nv50_crtc_disable(struct drm_crtc *crtc)
1266{
1267 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001268 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001269 if (head->image)
1270 nouveau_bo_unpin(head->image);
1271 nouveau_bo_ref(NULL, &head->image);
1272}
1273
Ben Skeggs438d99e2011-07-05 16:48:06 +10001274static int
Ben Skeggse225f442012-11-21 14:40:21 +10001275nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001276 uint32_t handle, uint32_t width, uint32_t height)
1277{
1278 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs5a560252014-11-10 15:52:02 +10001279 struct nv50_curs *curs = nv50_curs(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001280 struct drm_device *dev = crtc->dev;
Ben Skeggs5a560252014-11-10 15:52:02 +10001281 struct drm_gem_object *gem = NULL;
1282 struct nouveau_bo *nvbo = NULL;
1283 int ret = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001284
Ben Skeggs5a560252014-11-10 15:52:02 +10001285 if (handle) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001286 if (width != 64 || height != 64)
1287 return -EINVAL;
1288
1289 gem = drm_gem_object_lookup(dev, file_priv, handle);
1290 if (unlikely(!gem))
1291 return -ENOENT;
1292 nvbo = nouveau_gem_object(gem);
1293
Ben Skeggs5a560252014-11-10 15:52:02 +10001294 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001295 }
1296
Ben Skeggs5a560252014-11-10 15:52:02 +10001297 if (ret == 0) {
1298 if (curs->image)
1299 nouveau_bo_unpin(curs->image);
1300 nouveau_bo_ref(nvbo, &curs->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001301 }
Ben Skeggs5a560252014-11-10 15:52:02 +10001302 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001303
Ben Skeggs5a560252014-11-10 15:52:02 +10001304 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001305 return ret;
1306}
1307
1308static int
Ben Skeggse225f442012-11-21 14:40:21 +10001309nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001310{
Ben Skeggse225f442012-11-21 14:40:21 +10001311 struct nv50_curs *curs = nv50_curs(crtc);
1312 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001313 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1314 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001315 return 0;
1316}
1317
1318static void
Ben Skeggse225f442012-11-21 14:40:21 +10001319nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001320 uint32_t start, uint32_t size)
1321{
1322 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Dan Carpenterbdefc8c2013-11-28 01:18:47 +03001323 u32 end = min_t(u32, start + size, 256);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001324 u32 i;
1325
1326 for (i = start; i < end; i++) {
1327 nv_crtc->lut.r[i] = r[i];
1328 nv_crtc->lut.g[i] = g[i];
1329 nv_crtc->lut.b[i] = b[i];
1330 }
1331
Ben Skeggse225f442012-11-21 14:40:21 +10001332 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001333}
1334
1335static void
Ben Skeggse225f442012-11-21 14:40:21 +10001336nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001337{
1338 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001339 struct nv50_disp *disp = nv50_disp(crtc->dev);
1340 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001341 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001342
Ben Skeggs0ad72862014-08-10 04:10:22 +10001343 list_for_each_entry(fbdma, &disp->fbdma, head) {
1344 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1345 }
1346
1347 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1348 nv50_pioc_destroy(&head->oimm.base);
1349 nv50_dmac_destroy(&head->sync.base, disp->disp);
1350 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001351
1352 /*XXX: this shouldn't be necessary, but the core doesn't call
1353 * disconnect() during the cleanup paths
1354 */
1355 if (head->image)
1356 nouveau_bo_unpin(head->image);
1357 nouveau_bo_ref(NULL, &head->image);
1358
Ben Skeggs5a560252014-11-10 15:52:02 +10001359 /*XXX: ditto */
1360 if (head->curs.image)
1361 nouveau_bo_unpin(head->curs.image);
1362 nouveau_bo_ref(NULL, &head->curs.image);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001363
Ben Skeggs438d99e2011-07-05 16:48:06 +10001364 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001365 if (nv_crtc->lut.nvbo)
1366 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001367 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001368
Ben Skeggs438d99e2011-07-05 16:48:06 +10001369 drm_crtc_cleanup(crtc);
1370 kfree(crtc);
1371}
1372
Ben Skeggse225f442012-11-21 14:40:21 +10001373static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1374 .dpms = nv50_crtc_dpms,
1375 .prepare = nv50_crtc_prepare,
1376 .commit = nv50_crtc_commit,
1377 .mode_fixup = nv50_crtc_mode_fixup,
1378 .mode_set = nv50_crtc_mode_set,
1379 .mode_set_base = nv50_crtc_mode_set_base,
1380 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1381 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001382 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001383};
1384
Ben Skeggse225f442012-11-21 14:40:21 +10001385static const struct drm_crtc_funcs nv50_crtc_func = {
1386 .cursor_set = nv50_crtc_cursor_set,
1387 .cursor_move = nv50_crtc_cursor_move,
1388 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001389 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001390 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001391 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001392};
1393
1394static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001395nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001396{
Ben Skeggse225f442012-11-21 14:40:21 +10001397 struct nv50_disp *disp = nv50_disp(dev);
1398 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001399 struct drm_crtc *crtc;
1400 int ret, i;
1401
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001402 head = kzalloc(sizeof(*head), GFP_KERNEL);
1403 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001404 return -ENOMEM;
1405
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001406 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001407 head->base.set_dither = nv50_crtc_set_dither;
1408 head->base.set_scale = nv50_crtc_set_scale;
1409 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001410 head->base.color_vibrance = 50;
1411 head->base.vibrant_hue = 0;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001412 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001413 head->base.lut.r[i] = i << 8;
1414 head->base.lut.g[i] = i << 8;
1415 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001416 }
1417
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001418 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001419 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1420 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001421 drm_mode_crtc_set_gamma_size(crtc, 256);
1422
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001423 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01001424 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001425 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10001426 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001427 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001428 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001429 if (ret)
1430 nouveau_bo_unpin(head->base.lut.nvbo);
1431 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001432 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001433 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001434 }
1435
1436 if (ret)
1437 goto out;
1438
Ben Skeggse225f442012-11-21 14:40:21 +10001439 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001440
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001441 /* allocate cursor resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001442 ret = nv50_curs_create(disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001443 if (ret)
1444 goto out;
1445
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001446 /* allocate page flip / sync resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001447 ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
1448 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001449 if (ret)
1450 goto out;
1451
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001452 head->sync.addr = EVO_FLIP_SEM0(index);
1453 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001454
1455 /* allocate overlay resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001456 ret = nv50_oimm_create(disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001457 if (ret)
1458 goto out;
1459
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001460 ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
1461 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001462 if (ret)
1463 goto out;
1464
Ben Skeggs438d99e2011-07-05 16:48:06 +10001465out:
1466 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001467 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001468 return ret;
1469}
1470
1471/******************************************************************************
Ben Skeggsa91d3222014-12-22 16:30:13 +10001472 * Encoder helpers
1473 *****************************************************************************/
1474static bool
1475nv50_encoder_mode_fixup(struct drm_encoder *encoder,
1476 const struct drm_display_mode *mode,
1477 struct drm_display_mode *adjusted_mode)
1478{
1479 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1480 struct nouveau_connector *nv_connector;
1481
1482 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1483 if (nv_connector && nv_connector->native_mode) {
Ben Skeggs576f7912014-12-22 17:19:26 +10001484 nv_connector->scaling_full = false;
1485 if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) {
1486 switch (nv_connector->type) {
1487 case DCB_CONNECTOR_LVDS:
1488 case DCB_CONNECTOR_LVDS_SPWG:
1489 case DCB_CONNECTOR_eDP:
1490 /* force use of scaler for non-edid modes */
1491 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
1492 return true;
1493 nv_connector->scaling_full = true;
1494 break;
1495 default:
1496 return true;
1497 }
1498 }
1499
1500 drm_mode_copy(adjusted_mode, nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +10001501 }
1502
1503 return true;
1504}
1505
1506/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001507 * DAC
1508 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001509static void
Ben Skeggse225f442012-11-21 14:40:21 +10001510nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001511{
1512 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001513 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001514 struct {
1515 struct nv50_disp_mthd_v1 base;
1516 struct nv50_disp_dac_pwr_v0 pwr;
1517 } args = {
1518 .base.version = 1,
1519 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1520 .base.hasht = nv_encoder->dcb->hasht,
1521 .base.hashm = nv_encoder->dcb->hashm,
1522 .pwr.state = 1,
1523 .pwr.data = 1,
1524 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1525 mode != DRM_MODE_DPMS_OFF),
1526 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1527 mode != DRM_MODE_DPMS_OFF),
1528 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001529
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001530 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001531}
1532
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001533static void
Ben Skeggse225f442012-11-21 14:40:21 +10001534nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001535{
1536}
1537
1538static void
Ben Skeggse225f442012-11-21 14:40:21 +10001539nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001540 struct drm_display_mode *adjusted_mode)
1541{
Ben Skeggse225f442012-11-21 14:40:21 +10001542 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001543 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1544 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001545 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001546
Ben Skeggse225f442012-11-21 14:40:21 +10001547 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001548
Ben Skeggs97b19b52012-11-16 11:21:37 +10001549 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001550 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001551 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001552 u32 syncs = 0x00000000;
1553
1554 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1555 syncs |= 0x00000001;
1556 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1557 syncs |= 0x00000002;
1558
1559 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1560 evo_data(push, 1 << nv_crtc->index);
1561 evo_data(push, syncs);
1562 } else {
1563 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1564 u32 syncs = 0x00000001;
1565
1566 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1567 syncs |= 0x00000008;
1568 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1569 syncs |= 0x00000010;
1570
1571 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1572 magic |= 0x00000001;
1573
1574 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1575 evo_data(push, syncs);
1576 evo_data(push, magic);
1577 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1578 evo_data(push, 1 << nv_crtc->index);
1579 }
1580
1581 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001582 }
1583
1584 nv_encoder->crtc = encoder->crtc;
1585}
1586
1587static void
Ben Skeggse225f442012-11-21 14:40:21 +10001588nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001589{
1590 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001591 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001592 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001593 u32 *push;
1594
1595 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001596 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001597
Ben Skeggs97b19b52012-11-16 11:21:37 +10001598 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001599 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001600 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001601 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1602 evo_data(push, 0x00000000);
1603 } else {
1604 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1605 evo_data(push, 0x00000000);
1606 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001607 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001608 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001609 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001610
1611 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001612}
1613
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001614static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001615nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001616{
Ben Skeggsc4abd312014-08-10 04:10:26 +10001617 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001618 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +10001619 struct {
1620 struct nv50_disp_mthd_v1 base;
1621 struct nv50_disp_dac_load_v0 load;
1622 } args = {
1623 .base.version = 1,
1624 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
1625 .base.hasht = nv_encoder->dcb->hasht,
1626 .base.hashm = nv_encoder->dcb->hashm,
1627 };
1628 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +10001629
Ben Skeggsc4abd312014-08-10 04:10:26 +10001630 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
1631 if (args.load.data == 0)
1632 args.load.data = 340;
1633
1634 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
1635 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001636 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001637
Ben Skeggs35b21d32012-11-08 12:08:55 +10001638 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001639}
1640
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001641static void
Ben Skeggse225f442012-11-21 14:40:21 +10001642nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001643{
1644 drm_encoder_cleanup(encoder);
1645 kfree(encoder);
1646}
1647
Ben Skeggse225f442012-11-21 14:40:21 +10001648static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1649 .dpms = nv50_dac_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10001650 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggse225f442012-11-21 14:40:21 +10001651 .prepare = nv50_dac_disconnect,
1652 .commit = nv50_dac_commit,
1653 .mode_set = nv50_dac_mode_set,
1654 .disable = nv50_dac_disconnect,
1655 .get_crtc = nv50_display_crtc_get,
1656 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001657};
1658
Ben Skeggse225f442012-11-21 14:40:21 +10001659static const struct drm_encoder_funcs nv50_dac_func = {
1660 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001661};
1662
1663static int
Ben Skeggse225f442012-11-21 14:40:21 +10001664nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001665{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001666 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001667 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001668 struct nouveau_encoder *nv_encoder;
1669 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001670 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001671
1672 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1673 if (!nv_encoder)
1674 return -ENOMEM;
1675 nv_encoder->dcb = dcbe;
1676 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001677 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001678
1679 encoder = to_drm_encoder(nv_encoder);
1680 encoder->possible_crtcs = dcbe->heads;
1681 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001682 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001683 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001684
1685 drm_mode_connector_attach_encoder(connector, encoder);
1686 return 0;
1687}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001688
1689/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001690 * Audio
1691 *****************************************************************************/
1692static void
Ben Skeggse225f442012-11-21 14:40:21 +10001693nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001694{
1695 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +10001696 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +10001697 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001698 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +10001699 struct __packed {
1700 struct {
1701 struct nv50_disp_mthd_v1 mthd;
1702 struct nv50_disp_sor_hda_eld_v0 eld;
1703 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +10001704 u8 data[sizeof(nv_connector->base.eld)];
1705 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +10001706 .base.mthd.version = 1,
1707 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1708 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10001709 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1710 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10001711 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001712
1713 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1714 if (!drm_detect_monitor_audio(nv_connector->edid))
1715 return;
1716
Ben Skeggs78951d22011-11-11 18:13:13 +10001717 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001718 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001719
Jani Nikula938fd8a2014-10-28 16:20:48 +02001720 nvif_mthd(disp->disp, 0, &args,
1721 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001722}
1723
1724static void
Ben Skeggscc2a9072014-09-15 21:29:05 +10001725nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001726{
1727 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001728 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +10001729 struct {
1730 struct nv50_disp_mthd_v1 base;
1731 struct nv50_disp_sor_hda_eld_v0 eld;
1732 } args = {
1733 .base.version = 1,
1734 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1735 .base.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +10001736 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1737 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +10001738 };
Ben Skeggs78951d22011-11-11 18:13:13 +10001739
Ben Skeggs120b0c32014-08-10 04:10:26 +10001740 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001741}
1742
1743/******************************************************************************
1744 * HDMI
1745 *****************************************************************************/
1746static void
Ben Skeggse225f442012-11-21 14:40:21 +10001747nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001748{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001749 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1750 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001751 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001752 struct {
1753 struct nv50_disp_mthd_v1 base;
1754 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1755 } args = {
1756 .base.version = 1,
1757 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1758 .base.hasht = nv_encoder->dcb->hasht,
1759 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1760 (0x0100 << nv_crtc->index),
1761 .pwr.state = 1,
1762 .pwr.rekey = 56, /* binary driver, and tegra, constant */
1763 };
1764 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001765 u32 max_ac_packet;
1766
1767 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1768 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1769 return;
1770
1771 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +10001772 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001773 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +10001774 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001775
Ben Skeggse00f2232014-08-10 04:10:26 +10001776 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggse225f442012-11-21 14:40:21 +10001777 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001778}
1779
1780static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001781nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001782{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001783 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001784 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +10001785 struct {
1786 struct nv50_disp_mthd_v1 base;
1787 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1788 } args = {
1789 .base.version = 1,
1790 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1791 .base.hasht = nv_encoder->dcb->hasht,
1792 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1793 (0x0100 << nv_crtc->index),
1794 };
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001795
Ben Skeggse00f2232014-08-10 04:10:26 +10001796 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +10001797}
1798
1799/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001800 * SOR
1801 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001802static void
Ben Skeggse225f442012-11-21 14:40:21 +10001803nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001804{
1805 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001806 struct nv50_disp *disp = nv50_disp(encoder->dev);
1807 struct {
1808 struct nv50_disp_mthd_v1 base;
1809 struct nv50_disp_sor_pwr_v0 pwr;
1810 } args = {
1811 .base.version = 1,
1812 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
1813 .base.hasht = nv_encoder->dcb->hasht,
1814 .base.hashm = nv_encoder->dcb->hashm,
1815 .pwr.state = mode == DRM_MODE_DPMS_ON,
1816 };
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001817 struct {
1818 struct nv50_disp_mthd_v1 base;
1819 struct nv50_disp_sor_dp_pwr_v0 pwr;
1820 } link = {
1821 .base.version = 1,
1822 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
1823 .base.hasht = nv_encoder->dcb->hasht,
1824 .base.hashm = nv_encoder->dcb->hashm,
1825 .pwr.state = mode == DRM_MODE_DPMS_ON,
1826 };
Ben Skeggs83fc0832011-07-05 13:08:40 +10001827 struct drm_device *dev = encoder->dev;
1828 struct drm_encoder *partner;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001829
1830 nv_encoder->last_dpms = mode;
1831
1832 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1833 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1834
1835 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1836 continue;
1837
1838 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001839 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001840 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1841 return;
1842 break;
1843 }
1844 }
1845
Ben Skeggs48743222014-05-31 01:48:06 +10001846 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001847 args.pwr.state = 1;
1848 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggsc02ed2b2014-08-10 04:10:27 +10001849 nvif_mthd(disp->disp, 0, &link, sizeof(link));
Ben Skeggs48743222014-05-31 01:48:06 +10001850 } else {
Ben Skeggsd55b4af2014-08-10 04:10:26 +10001851 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs48743222014-05-31 01:48:06 +10001852 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001853}
1854
Ben Skeggs83fc0832011-07-05 13:08:40 +10001855static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001856nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
1857{
1858 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
1859 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
1860 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10001861 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001862 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
1863 evo_data(push, (nv_encoder->ctrl = temp));
1864 } else {
1865 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1866 evo_data(push, (nv_encoder->ctrl = temp));
1867 }
1868 evo_kick(push, mast);
1869 }
1870}
1871
1872static void
Ben Skeggse225f442012-11-21 14:40:21 +10001873nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001874{
1875 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001876 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001877
1878 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1879 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001880
1881 if (nv_crtc) {
1882 nv50_crtc_prepare(&nv_crtc->base);
1883 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
Ben Skeggscc2a9072014-09-15 21:29:05 +10001884 nv50_audio_disconnect(encoder, nv_crtc);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001885 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
1886 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001887}
1888
1889static void
Ben Skeggse225f442012-11-21 14:40:21 +10001890nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001891{
1892}
1893
1894static void
Ben Skeggse225f442012-11-21 14:40:21 +10001895nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001896 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001897{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001898 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1899 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1900 struct {
1901 struct nv50_disp_mthd_v1 base;
1902 struct nv50_disp_sor_lvds_script_v0 lvds;
1903 } lvds = {
1904 .base.version = 1,
1905 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1906 .base.hasht = nv_encoder->dcb->hasht,
1907 .base.hashm = nv_encoder->dcb->hashm,
1908 };
Ben Skeggse225f442012-11-21 14:40:21 +10001909 struct nv50_disp *disp = nv50_disp(encoder->dev);
1910 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001911 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001912 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001913 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001914 struct nvbios *bios = &drm->vbios;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001915 u32 mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001916 u8 owner = 1 << nv_crtc->index;
1917 u8 proto = 0xf;
1918 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001919
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001920 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001921 nv_encoder->crtc = encoder->crtc;
1922
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001923 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001924 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001925 if (nv_encoder->dcb->sorconf.link & 1) {
1926 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001927 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001928 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001929 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001930 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001931 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001932 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001933
Ben Skeggse84a35a2014-06-05 10:59:55 +10001934 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001935 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001936 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001937 proto = 0x0;
1938
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001939 if (bios->fp_no_ddc) {
1940 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001941 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001942 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001943 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001944 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001945 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001946 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001947 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001948 } else
1949 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001950 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001951 }
1952
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001953 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001954 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001955 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001956 } else {
1957 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001958 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001959 }
1960
1961 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001962 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001963 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001964
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001965 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001966 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001967 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001968 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001969 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001970 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001971 } else
1972 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001973 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001974 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001975 } else {
1976 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1977 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001978 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001979
1980 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001981 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001982 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001983 proto = 0x9;
Ben Skeggs3eee8642014-09-15 15:20:47 +10001984 nv50_audio_mode_set(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001985 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001986 default:
1987 BUG_ON(1);
1988 break;
1989 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001990
Ben Skeggse84a35a2014-06-05 10:59:55 +10001991 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001992
Ben Skeggs648d4df2014-08-10 04:10:27 +10001993 if (nv50_vers(mast) >= GF110_DISP) {
Ben Skeggse84a35a2014-06-05 10:59:55 +10001994 u32 *push = evo_wait(mast, 3);
1995 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001996 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1997 u32 syncs = 0x00000001;
1998
1999 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2000 syncs |= 0x00000008;
2001 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2002 syncs |= 0x00000010;
2003
2004 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2005 magic |= 0x00000001;
2006
2007 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2008 evo_data(push, syncs | (depth << 6));
2009 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10002010 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10002011 }
2012
Ben Skeggse84a35a2014-06-05 10:59:55 +10002013 ctrl = proto << 8;
2014 mask = 0x00000f00;
2015 } else {
2016 ctrl = (depth << 16) | (proto << 8);
2017 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2018 ctrl |= 0x00001000;
2019 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2020 ctrl |= 0x00002000;
2021 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002022 }
2023
Ben Skeggse84a35a2014-06-05 10:59:55 +10002024 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002025}
2026
2027static void
Ben Skeggse225f442012-11-21 14:40:21 +10002028nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002029{
2030 drm_encoder_cleanup(encoder);
2031 kfree(encoder);
2032}
2033
Ben Skeggse225f442012-11-21 14:40:21 +10002034static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2035 .dpms = nv50_sor_dpms,
Ben Skeggsa91d3222014-12-22 16:30:13 +10002036 .mode_fixup = nv50_encoder_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10002037 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10002038 .commit = nv50_sor_commit,
2039 .mode_set = nv50_sor_mode_set,
2040 .disable = nv50_sor_disconnect,
2041 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002042};
2043
Ben Skeggse225f442012-11-21 14:40:21 +10002044static const struct drm_encoder_funcs nv50_sor_func = {
2045 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10002046};
2047
2048static int
Ben Skeggse225f442012-11-21 14:40:21 +10002049nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10002050{
Ben Skeggs5ed50202013-02-11 20:15:03 +10002051 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002052 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002053 struct nouveau_encoder *nv_encoder;
2054 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002055 int type;
2056
2057 switch (dcbe->type) {
2058 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2059 case DCB_OUTPUT_TMDS:
2060 case DCB_OUTPUT_DP:
2061 default:
2062 type = DRM_MODE_ENCODER_TMDS;
2063 break;
2064 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10002065
2066 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2067 if (!nv_encoder)
2068 return -ENOMEM;
2069 nv_encoder->dcb = dcbe;
2070 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002071 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002072 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2073
2074 encoder = to_drm_encoder(nv_encoder);
2075 encoder->possible_crtcs = dcbe->heads;
2076 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002077 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10002078 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002079
2080 drm_mode_connector_attach_encoder(connector, encoder);
2081 return 0;
2082}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002083
2084/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002085 * PIOR
2086 *****************************************************************************/
2087
2088static void
2089nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2090{
2091 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2092 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs67cb49c2014-08-10 04:10:27 +10002093 struct {
2094 struct nv50_disp_mthd_v1 base;
2095 struct nv50_disp_pior_pwr_v0 pwr;
2096 } args = {
2097 .base.version = 1,
2098 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2099 .base.hasht = nv_encoder->dcb->hasht,
2100 .base.hashm = nv_encoder->dcb->hashm,
2101 .pwr.state = mode == DRM_MODE_DPMS_ON,
2102 .pwr.type = nv_encoder->dcb->type,
2103 };
2104
2105 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002106}
2107
2108static bool
2109nv50_pior_mode_fixup(struct drm_encoder *encoder,
2110 const struct drm_display_mode *mode,
2111 struct drm_display_mode *adjusted_mode)
2112{
Ben Skeggsa91d3222014-12-22 16:30:13 +10002113 if (!nv50_encoder_mode_fixup(encoder, mode, adjusted_mode))
2114 return false;
Ben Skeggseb6313a2013-02-11 09:52:58 +10002115 adjusted_mode->clock *= 2;
2116 return true;
2117}
2118
2119static void
2120nv50_pior_commit(struct drm_encoder *encoder)
2121{
2122}
2123
2124static void
2125nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2126 struct drm_display_mode *adjusted_mode)
2127{
2128 struct nv50_mast *mast = nv50_mast(encoder->dev);
2129 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2130 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2131 struct nouveau_connector *nv_connector;
2132 u8 owner = 1 << nv_crtc->index;
2133 u8 proto, depth;
2134 u32 *push;
2135
2136 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2137 switch (nv_connector->base.display_info.bpc) {
2138 case 10: depth = 0x6; break;
2139 case 8: depth = 0x5; break;
2140 case 6: depth = 0x2; break;
2141 default: depth = 0x0; break;
2142 }
2143
2144 switch (nv_encoder->dcb->type) {
2145 case DCB_OUTPUT_TMDS:
2146 case DCB_OUTPUT_DP:
2147 proto = 0x0;
2148 break;
2149 default:
2150 BUG_ON(1);
2151 break;
2152 }
2153
2154 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2155
2156 push = evo_wait(mast, 8);
2157 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002158 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002159 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2160 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2161 ctrl |= 0x00001000;
2162 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2163 ctrl |= 0x00002000;
2164 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2165 evo_data(push, ctrl);
2166 }
2167
2168 evo_kick(push, mast);
2169 }
2170
2171 nv_encoder->crtc = encoder->crtc;
2172}
2173
2174static void
2175nv50_pior_disconnect(struct drm_encoder *encoder)
2176{
2177 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2178 struct nv50_mast *mast = nv50_mast(encoder->dev);
2179 const int or = nv_encoder->or;
2180 u32 *push;
2181
2182 if (nv_encoder->crtc) {
2183 nv50_crtc_prepare(nv_encoder->crtc);
2184
2185 push = evo_wait(mast, 4);
2186 if (push) {
Ben Skeggs648d4df2014-08-10 04:10:27 +10002187 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
Ben Skeggseb6313a2013-02-11 09:52:58 +10002188 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2189 evo_data(push, 0x00000000);
2190 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002191 evo_kick(push, mast);
2192 }
2193 }
2194
2195 nv_encoder->crtc = NULL;
2196}
2197
2198static void
2199nv50_pior_destroy(struct drm_encoder *encoder)
2200{
2201 drm_encoder_cleanup(encoder);
2202 kfree(encoder);
2203}
2204
2205static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2206 .dpms = nv50_pior_dpms,
2207 .mode_fixup = nv50_pior_mode_fixup,
2208 .prepare = nv50_pior_disconnect,
2209 .commit = nv50_pior_commit,
2210 .mode_set = nv50_pior_mode_set,
2211 .disable = nv50_pior_disconnect,
2212 .get_crtc = nv50_display_crtc_get,
2213};
2214
2215static const struct drm_encoder_funcs nv50_pior_func = {
2216 .destroy = nv50_pior_destroy,
2217};
2218
2219static int
2220nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2221{
2222 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002223 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002224 struct nouveau_i2c_port *ddc = NULL;
2225 struct nouveau_encoder *nv_encoder;
2226 struct drm_encoder *encoder;
2227 int type;
2228
2229 switch (dcbe->type) {
2230 case DCB_OUTPUT_TMDS:
2231 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2232 type = DRM_MODE_ENCODER_TMDS;
2233 break;
2234 case DCB_OUTPUT_DP:
2235 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2236 type = DRM_MODE_ENCODER_TMDS;
2237 break;
2238 default:
2239 return -ENODEV;
2240 }
2241
2242 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2243 if (!nv_encoder)
2244 return -ENOMEM;
2245 nv_encoder->dcb = dcbe;
2246 nv_encoder->or = ffs(dcbe->or) - 1;
2247 nv_encoder->i2c = ddc;
2248
2249 encoder = to_drm_encoder(nv_encoder);
2250 encoder->possible_crtcs = dcbe->heads;
2251 encoder->possible_clones = 0;
2252 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2253 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2254
2255 drm_mode_connector_attach_encoder(connector, encoder);
2256 return 0;
2257}
2258
2259/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002260 * Framebuffer
2261 *****************************************************************************/
2262
Ben Skeggs8a423642014-08-10 04:10:19 +10002263static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002264nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002265{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002266 int i;
2267 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2268 nvif_object_fini(&fbdma->base[i]);
2269 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002270 list_del(&fbdma->head);
2271 kfree(fbdma);
2272}
2273
2274static int
2275nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2276{
2277 struct nouveau_drm *drm = nouveau_drm(dev);
2278 struct nv50_disp *disp = nv50_disp(dev);
2279 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002280 struct __attribute__ ((packed)) {
2281 struct nv_dma_v0 base;
2282 union {
2283 struct nv50_dma_v0 nv50;
2284 struct gf100_dma_v0 gf100;
2285 struct gf110_dma_v0 gf110;
2286 };
2287 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002288 struct nv50_fbdma *fbdma;
2289 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002290 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002291 int ret;
2292
2293 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002294 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002295 return 0;
2296 }
2297
2298 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2299 if (!fbdma)
2300 return -ENOMEM;
2301 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002302
Ben Skeggs4acfd702014-08-10 04:10:24 +10002303 args.base.target = NV_DMA_V0_TARGET_VRAM;
2304 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2305 args.base.start = offset;
2306 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002307
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002308 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002309 args.nv50.part = NV50_DMA_V0_PART_256;
2310 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002311 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002312 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002313 args.nv50.part = NV50_DMA_V0_PART_256;
2314 args.nv50.kind = kind;
2315 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002316 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002317 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002318 args.gf100.kind = kind;
2319 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002320 } else {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002321 args.gf110.page = GF110_DMA_V0_PAGE_LP;
2322 args.gf110.kind = kind;
2323 size += sizeof(args.gf110);
Ben Skeggs8a423642014-08-10 04:10:19 +10002324 }
2325
2326 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002327 struct nv50_head *head = nv50_head(crtc);
2328 int ret = nvif_object_init(&head->sync.base.base.user, NULL,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002329 name, NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002330 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002331 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002332 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002333 return ret;
2334 }
2335 }
2336
Ben Skeggs0ad72862014-08-10 04:10:22 +10002337 ret = nvif_object_init(&mast->base.base.user, NULL, name,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002338 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002339 &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002340 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002341 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002342 return ret;
2343 }
2344
2345 return 0;
2346}
2347
Ben Skeggsab0af552014-08-10 04:10:19 +10002348static void
2349nv50_fb_dtor(struct drm_framebuffer *fb)
2350{
2351}
2352
2353static int
2354nv50_fb_ctor(struct drm_framebuffer *fb)
2355{
2356 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2357 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2358 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002359 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002360 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2361 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002362
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002363 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002364 tile >>= 4; /* yep.. */
2365
Ben Skeggsab0af552014-08-10 04:10:19 +10002366 switch (fb->depth) {
2367 case 8: nv_fb->r_format = 0x1e00; break;
2368 case 15: nv_fb->r_format = 0xe900; break;
2369 case 16: nv_fb->r_format = 0xe800; break;
2370 case 24:
2371 case 32: nv_fb->r_format = 0xcf00; break;
2372 case 30: nv_fb->r_format = 0xd100; break;
2373 default:
2374 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2375 return -EINVAL;
2376 }
2377
Ben Skeggs648d4df2014-08-10 04:10:27 +10002378 if (disp->disp->oclass < G82_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002379 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2380 (fb->pitches[0] | 0x00100000);
2381 nv_fb->r_format |= kind << 16;
2382 } else
Ben Skeggs648d4df2014-08-10 04:10:27 +10002383 if (disp->disp->oclass < GF110_DISP) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002384 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2385 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002386 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002387 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2388 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002389 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002390 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002391
Ben Skeggsf392ec42014-08-10 04:10:28 +10002392 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
2393 drm->device.info.ram_user, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002394}
2395
2396/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002397 * Init
2398 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002399
Ben Skeggs2a44e492011-11-09 11:36:33 +10002400void
Ben Skeggse225f442012-11-21 14:40:21 +10002401nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002402{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002403}
2404
2405int
Ben Skeggse225f442012-11-21 14:40:21 +10002406nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002407{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002408 struct nv50_disp *disp = nv50_disp(dev);
2409 struct drm_crtc *crtc;
2410 u32 *push;
2411
2412 push = evo_wait(nv50_mast(dev), 32);
2413 if (!push)
2414 return -EBUSY;
2415
2416 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2417 struct nv50_sync *sync = nv50_sync(crtc);
2418 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002419 }
2420
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002421 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10002422 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002423 evo_kick(push, nv50_mast(dev));
2424 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002425}
2426
2427void
Ben Skeggse225f442012-11-21 14:40:21 +10002428nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002429{
Ben Skeggse225f442012-11-21 14:40:21 +10002430 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002431 struct nv50_fbdma *fbdma, *fbtmp;
2432
2433 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002434 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002435 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002436
Ben Skeggs0ad72862014-08-10 04:10:22 +10002437 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002438
Ben Skeggs816af2f2011-11-16 15:48:48 +10002439 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002440 if (disp->sync)
2441 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002442 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002443
Ben Skeggs77145f12012-07-31 16:16:21 +10002444 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002445 kfree(disp);
2446}
2447
2448int
Ben Skeggse225f442012-11-21 14:40:21 +10002449nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002450{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002451 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002452 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002453 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002454 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002455 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002456 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002457 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002458
2459 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2460 if (!disp)
2461 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10002462 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10002463
2464 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002465 nouveau_display(dev)->dtor = nv50_display_destroy;
2466 nouveau_display(dev)->init = nv50_display_init;
2467 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10002468 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2469 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002470 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002471
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002472 /* small shared memory area we use for notifiers and semaphores */
2473 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002474 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002475 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002476 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002477 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002478 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002479 if (ret)
2480 nouveau_bo_unpin(disp->sync);
2481 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002482 if (ret)
2483 nouveau_bo_ref(NULL, &disp->sync);
2484 }
2485
2486 if (ret)
2487 goto out;
2488
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002489 /* allocate master evo channel */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10002490 ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
2491 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002492 if (ret)
2493 goto out;
2494
Ben Skeggs438d99e2011-07-05 16:48:06 +10002495 /* create crtc objects to represent the hw heads */
Ben Skeggs648d4df2014-08-10 04:10:27 +10002496 if (disp->disp->oclass >= GF110_DISP)
Ben Skeggsdb2bec12014-08-10 04:10:22 +10002497 crtcs = nvif_rd32(device, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10002498 else
2499 crtcs = 2;
2500
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002501 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002502 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002503 if (ret)
2504 goto out;
2505 }
2506
Ben Skeggs83fc0832011-07-05 13:08:40 +10002507 /* create encoder/connector objects based on VBIOS DCB table */
2508 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2509 connector = nouveau_connector_create(dev, dcbe->connector);
2510 if (IS_ERR(connector))
2511 continue;
2512
Ben Skeggseb6313a2013-02-11 09:52:58 +10002513 if (dcbe->location == DCB_LOC_ON_CHIP) {
2514 switch (dcbe->type) {
2515 case DCB_OUTPUT_TMDS:
2516 case DCB_OUTPUT_LVDS:
2517 case DCB_OUTPUT_DP:
2518 ret = nv50_sor_create(connector, dcbe);
2519 break;
2520 case DCB_OUTPUT_ANALOG:
2521 ret = nv50_dac_create(connector, dcbe);
2522 break;
2523 default:
2524 ret = -ENODEV;
2525 break;
2526 }
2527 } else {
2528 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002529 }
2530
Ben Skeggseb6313a2013-02-11 09:52:58 +10002531 if (ret) {
2532 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2533 dcbe->location, dcbe->type,
2534 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002535 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002536 }
2537 }
2538
2539 /* cull any connectors we created that don't have an encoder */
2540 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2541 if (connector->encoder_ids[0])
2542 continue;
2543
Ben Skeggs77145f12012-07-31 16:16:21 +10002544 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002545 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002546 connector->funcs->destroy(connector);
2547 }
2548
Ben Skeggs26f6d882011-07-04 16:25:18 +10002549out:
2550 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002551 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002552 return ret;
2553}