blob: d3e6bd33670ee8acdbe42b0fd64d099585e25ccc [file] [log] [blame]
Ben Skeggs56d237d2014-05-19 14:54:33 +10001/*
Ben Skeggs26f6d882011-07-04 16:25:18 +10002 * Copyright 2011 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
Ben Skeggs51beb422011-07-05 10:33:08 +100025#include <linux/dma-mapping.h>
Ben Skeggs83fc0832011-07-05 13:08:40 +100026
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/drm_crtc_helper.h>
Ben Skeggs48743222014-05-31 01:48:06 +100029#include <drm/drm_dp_helper.h>
Ben Skeggs26f6d882011-07-04 16:25:18 +100030
Ben Skeggsfdb751e2014-08-10 04:10:23 +100031#include <nvif/class.h>
32
Ben Skeggs77145f12012-07-31 16:16:21 +100033#include "nouveau_drm.h"
34#include "nouveau_dma.h"
35#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100036#include "nouveau_connector.h"
37#include "nouveau_encoder.h"
38#include "nouveau_crtc.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100039#include "nouveau_fence.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100040#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100041
Ben Skeggs8a464382011-11-12 23:52:07 +100042#define EVO_DMA_NR 9
43
Ben Skeggsbdb8c212011-11-12 01:30:24 +100044#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100045#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100046#define EVO_OVLY(c) (0x05 + (c))
47#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100048#define EVO_CURS(c) (0x0d + (c))
49
Ben Skeggs816af2f2011-11-16 15:48:48 +100050/* offsets in shared sync bo of various structures */
51#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100052#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
53#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
54#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs816af2f2011-11-16 15:48:48 +100055
Ben Skeggsb5a794b2012-10-16 14:18:32 +100056#define EVO_CORE_HANDLE (0xd1500000)
57#define EVO_CHAN_HANDLE(t,i) (0xd15c0000 | (((t) & 0x00ff) << 8) | (i))
Ben Skeggs0ad72862014-08-10 04:10:22 +100058#define EVO_CHAN_OCLASS(t,c) (((c)->oclass & 0xff00) | ((t) & 0x00ff))
Ben Skeggsb5a794b2012-10-16 14:18:32 +100059#define EVO_PUSH_HANDLE(t,i) (0xd15b0000 | (i) | \
60 (((NV50_DISP_##t##_CLASS) & 0x00ff) << 8))
61
62/******************************************************************************
63 * EVO channel
64 *****************************************************************************/
65
Ben Skeggse225f442012-11-21 14:40:21 +100066struct nv50_chan {
Ben Skeggs0ad72862014-08-10 04:10:22 +100067 struct nvif_object user;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100068};
69
70static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +100071nv50_chan_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +100072 void *data, u32 size, struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100073{
Ben Skeggs410f3ec2014-08-10 04:10:25 +100074 while (oclass[0]) {
75 int ret = nvif_object_init(disp, NULL, (oclass[0] << 16) | head,
76 oclass[0], data, size,
77 &chan->user);
78 if (oclass++, ret == 0)
79 return ret;
80 }
81 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100082}
83
84static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100085nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100086{
Ben Skeggs0ad72862014-08-10 04:10:22 +100087 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +100088}
89
90/******************************************************************************
91 * PIO EVO channel
92 *****************************************************************************/
93
Ben Skeggse225f442012-11-21 14:40:21 +100094struct nv50_pioc {
95 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +100096};
97
98static void
Ben Skeggs0ad72862014-08-10 04:10:22 +100099nv50_pioc_destroy(struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000100{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000101 nv50_chan_destroy(&pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000102}
103
104static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000105nv50_pioc_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggse225f442012-11-21 14:40:21 +1000106 void *data, u32 size, struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000107{
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000108 return nv50_chan_create(disp, oclass, head, data, size, &pioc->base);
109}
110
111/******************************************************************************
112 * Cursor Immediate
113 *****************************************************************************/
114
115struct nv50_curs {
116 struct nv50_pioc base;
117};
118
119static int
120nv50_curs_create(struct nvif_object *disp, int head, struct nv50_curs *curs)
121{
122 struct nv50_display_curs_class args = {
123 .head = head,
124 };
125 static const u32 oclass[] = {
126 GM107_DISP_CURS_CLASS,
127 NVF0_DISP_CURS_CLASS,
128 NVE0_DISP_CURS_CLASS,
129 NVD0_DISP_CURS_CLASS,
130 NVA3_DISP_CURS_CLASS,
131 NV94_DISP_CURS_CLASS,
132 NVA0_DISP_CURS_CLASS,
133 NV84_DISP_CURS_CLASS,
134 NV50_DISP_CURS_CLASS,
135 0
136 };
137
138 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
139 &curs->base);
140}
141
142/******************************************************************************
143 * Overlay Immediate
144 *****************************************************************************/
145
146struct nv50_oimm {
147 struct nv50_pioc base;
148};
149
150static int
151nv50_oimm_create(struct nvif_object *disp, int head, struct nv50_oimm *oimm)
152{
153 struct nv50_display_oimm_class args = {
154 .head = head,
155 };
156 static const u32 oclass[] = {
157 GM107_DISP_OIMM_CLASS,
158 NVF0_DISP_OIMM_CLASS,
159 NVE0_DISP_OIMM_CLASS,
160 NVD0_DISP_OIMM_CLASS,
161 NVA3_DISP_OIMM_CLASS,
162 NV94_DISP_OIMM_CLASS,
163 NVA0_DISP_OIMM_CLASS,
164 NV84_DISP_OIMM_CLASS,
165 NV50_DISP_OIMM_CLASS,
166 0
167 };
168
169 return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
170 &oimm->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000171}
172
173/******************************************************************************
174 * DMA EVO channel
175 *****************************************************************************/
176
Ben Skeggse225f442012-11-21 14:40:21 +1000177struct nv50_dmac {
178 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000179 dma_addr_t handle;
180 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100181
Ben Skeggs0ad72862014-08-10 04:10:22 +1000182 struct nvif_object sync;
183 struct nvif_object vram;
184
Daniel Vetter59ad1462012-12-02 14:49:44 +0100185 /* Protects against concurrent pushbuf access to this channel, lock is
186 * grabbed by evo_wait (if the pushbuf reservation is successful) and
187 * dropped again by evo_kick. */
188 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000189};
190
191static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000192nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000193{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000194 nvif_object_fini(&dmac->vram);
195 nvif_object_fini(&dmac->sync);
196
197 nv50_chan_destroy(&dmac->base);
198
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000199 if (dmac->ptr) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000200 struct pci_dev *pdev = nvkm_device(nvif_device(disp))->pdev;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000201 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
202 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000203}
204
205static int
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000206nv50_dmac_create(struct nvif_object *disp, const u32 *oclass, u8 head,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000207 void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000208 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000209{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000210 struct nouveau_fb *pfb = nvkm_fb(nvif_device(disp));
211 struct nvif_object pushbuf;
212 u32 handle = *(u32 *)data;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000213 int ret;
214
Daniel Vetter59ad1462012-12-02 14:49:44 +0100215 mutex_init(&dmac->lock);
216
Ben Skeggs0ad72862014-08-10 04:10:22 +1000217 dmac->ptr = pci_alloc_consistent(nvkm_device(nvif_device(disp))->pdev,
218 PAGE_SIZE, &dmac->handle);
Ben Skeggs47057302012-11-16 13:58:48 +1000219 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000220 return -ENOMEM;
221
Ben Skeggs0ad72862014-08-10 04:10:22 +1000222 ret = nvif_object_init(nvif_object(nvif_device(disp)), NULL, handle,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000223 NV_DMA_FROM_MEMORY,
224 &(struct nv_dma_v0) {
225 .target = NV_DMA_V0_TARGET_PCI_US,
226 .access = NV_DMA_V0_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000227 .start = dmac->handle + 0x0000,
228 .limit = dmac->handle + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000229 }, sizeof(struct nv_dma_v0), &pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000230 if (ret)
231 return ret;
232
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000233 ret = nv50_chan_create(disp, oclass, head, data, size, &dmac->base);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000234 nvif_object_fini(&pushbuf);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000235 if (ret)
236 return ret;
237
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000238 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000000,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000239 NV_DMA_IN_MEMORY,
240 &(struct nv_dma_v0) {
241 .target = NV_DMA_V0_TARGET_VRAM,
242 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000243 .start = syncbuf + 0x0000,
244 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000245 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000246 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000247 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000248 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000249
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000250 ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000001,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000251 NV_DMA_IN_MEMORY,
252 &(struct nv_dma_v0) {
253 .target = NV_DMA_V0_TARGET_VRAM,
254 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000255 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000256 .limit = pfb->ram->size - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000257 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000258 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000259 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000260 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000261
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000262 return ret;
263}
264
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000265/******************************************************************************
266 * Core
267 *****************************************************************************/
268
Ben Skeggse225f442012-11-21 14:40:21 +1000269struct nv50_mast {
270 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000271};
272
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000273static int
274nv50_core_create(struct nvif_object *disp, u64 syncbuf, struct nv50_mast *core)
275{
276 struct nv50_display_mast_class args = {
277 .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
278 };
279 static const u32 oclass[] = {
280 GM107_DISP_MAST_CLASS,
281 NVF0_DISP_MAST_CLASS,
282 NVE0_DISP_MAST_CLASS,
283 NVD0_DISP_MAST_CLASS,
284 NVA3_DISP_MAST_CLASS,
285 NV94_DISP_MAST_CLASS,
286 NVA0_DISP_MAST_CLASS,
287 NV84_DISP_MAST_CLASS,
288 NV50_DISP_MAST_CLASS,
289 0
290 };
291
292 return nv50_dmac_create(disp, oclass, 0, &args, sizeof(args), syncbuf,
293 &core->base);
294}
295
296/******************************************************************************
297 * Base
298 *****************************************************************************/
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000299
Ben Skeggse225f442012-11-21 14:40:21 +1000300struct nv50_sync {
301 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000302 u32 addr;
303 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000304};
305
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000306static int
307nv50_base_create(struct nvif_object *disp, int head, u64 syncbuf,
308 struct nv50_sync *base)
309{
310 struct nv50_display_sync_class args = {
311 .pushbuf = EVO_PUSH_HANDLE(SYNC, head),
312 .head = head,
313 };
314 static const u32 oclass[] = {
315 GM107_DISP_SYNC_CLASS,
316 NVF0_DISP_SYNC_CLASS,
317 NVE0_DISP_SYNC_CLASS,
318 NVD0_DISP_SYNC_CLASS,
319 NVA3_DISP_SYNC_CLASS,
320 NV94_DISP_SYNC_CLASS,
321 NVA0_DISP_SYNC_CLASS,
322 NV84_DISP_SYNC_CLASS,
323 NV50_DISP_SYNC_CLASS,
324 0
325 };
326
327 return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
328 syncbuf, &base->base);
329}
330
331/******************************************************************************
332 * Overlay
333 *****************************************************************************/
334
Ben Skeggse225f442012-11-21 14:40:21 +1000335struct nv50_ovly {
336 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000337};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000338
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000339static int
340nv50_ovly_create(struct nvif_object *disp, int head, u64 syncbuf,
341 struct nv50_ovly *ovly)
342{
343 struct nv50_display_ovly_class args = {
344 .pushbuf = EVO_PUSH_HANDLE(OVLY, head),
345 .head = head,
346 };
347 static const u32 oclass[] = {
348 GM107_DISP_OVLY_CLASS,
349 NVF0_DISP_OVLY_CLASS,
350 NVE0_DISP_OVLY_CLASS,
351 NVD0_DISP_OVLY_CLASS,
352 NVA3_DISP_OVLY_CLASS,
353 NV94_DISP_OVLY_CLASS,
354 NVA0_DISP_OVLY_CLASS,
355 NV84_DISP_OVLY_CLASS,
356 NV50_DISP_OVLY_CLASS,
357 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
444#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
445#define evo_data(p,d) *((p)++) = (d)
446
Ben Skeggs3376ee32011-11-12 14:28:12 +1000447static bool
448evo_sync_wait(void *data)
449{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500450 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
451 return true;
452 usleep_range(1, 2);
453 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000454}
455
456static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000457evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000458{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000459 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggse225f442012-11-21 14:40:21 +1000460 struct nv50_disp *disp = nv50_disp(dev);
461 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000462 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000463 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000464 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000465 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000466 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000467 evo_mthd(push, 0x0080, 2);
468 evo_data(push, 0x00000000);
469 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000470 evo_kick(push, mast);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000471 if (nv_wait_cb(nvkm_device(device), evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000472 return 0;
473 }
474
475 return -EBUSY;
476}
477
478/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000479 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000480 *****************************************************************************/
481struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000482nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000483{
Ben Skeggse225f442012-11-21 14:40:21 +1000484 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000485}
486
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000487struct nv50_display_flip {
488 struct nv50_disp *disp;
489 struct nv50_sync *chan;
490};
491
492static bool
493nv50_display_flip_wait(void *data)
494{
495 struct nv50_display_flip *flip = data;
496 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500497 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000498 return true;
499 usleep_range(1, 2);
500 return false;
501}
502
Ben Skeggs3376ee32011-11-12 14:28:12 +1000503void
Ben Skeggse225f442012-11-21 14:40:21 +1000504nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000505{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000506 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000507 struct nv50_display_flip flip = {
508 .disp = nv50_disp(crtc->dev),
509 .chan = nv50_sync(crtc),
510 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000511 u32 *push;
512
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000513 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000514 if (push) {
515 evo_mthd(push, 0x0084, 1);
516 evo_data(push, 0x00000000);
517 evo_mthd(push, 0x0094, 1);
518 evo_data(push, 0x00000000);
519 evo_mthd(push, 0x00c0, 1);
520 evo_data(push, 0x00000000);
521 evo_mthd(push, 0x0080, 1);
522 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000523 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000524 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000525
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000526 nv_wait_cb(nvkm_device(device), nv50_display_flip_wait, &flip);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000527}
528
529int
Ben Skeggse225f442012-11-21 14:40:21 +1000530nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000531 struct nouveau_channel *chan, u32 swap_interval)
532{
533 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000534 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000535 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000536 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000537 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000538 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000539
540 swap_interval <<= 4;
541 if (swap_interval == 0)
542 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000543 if (chan == NULL)
544 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000545
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000546 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000547 if (unlikely(push == NULL))
548 return -EBUSY;
549
Ben Skeggsbbf89062014-08-10 04:10:25 +1000550 if (chan && chan->object->oclass < G82_CHANNEL_GPFIFO) {
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000551 ret = RING_SPACE(chan, 8);
552 if (ret)
553 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000554
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000555 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000556 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000557 OUT_RING (chan, sync->addr ^ 0x10);
558 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
559 OUT_RING (chan, sync->data + 1);
560 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
561 OUT_RING (chan, sync->addr);
562 OUT_RING (chan, sync->data);
563 } else
Ben Skeggsbbf89062014-08-10 04:10:25 +1000564 if (chan && chan->object->oclass < FERMI_CHANNEL_GPFIFO) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000565 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000566 ret = RING_SPACE(chan, 12);
567 if (ret)
568 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000569
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000570 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000571 OUT_RING (chan, chan->vram.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000572 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
573 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
574 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
575 OUT_RING (chan, sync->data + 1);
576 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
577 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
578 OUT_RING (chan, upper_32_bits(addr));
579 OUT_RING (chan, lower_32_bits(addr));
580 OUT_RING (chan, sync->data);
581 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
582 } else
583 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000584 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000585 ret = RING_SPACE(chan, 10);
586 if (ret)
587 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000588
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000589 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
590 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
591 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
592 OUT_RING (chan, sync->data + 1);
593 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
594 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
595 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
596 OUT_RING (chan, upper_32_bits(addr));
597 OUT_RING (chan, lower_32_bits(addr));
598 OUT_RING (chan, sync->data);
599 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
600 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
601 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500602
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000603 if (chan) {
604 sync->addr ^= 0x10;
605 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000606 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000607 }
608
609 /* queue the flip */
610 evo_mthd(push, 0x0100, 1);
611 evo_data(push, 0xfffe0000);
612 evo_mthd(push, 0x0084, 1);
613 evo_data(push, swap_interval);
614 if (!(swap_interval & 0x00000100)) {
615 evo_mthd(push, 0x00e0, 1);
616 evo_data(push, 0x40000000);
617 }
618 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000619 evo_data(push, sync->addr);
620 evo_data(push, sync->data++);
621 evo_data(push, sync->data);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000622 evo_data(push, sync->base.sync.handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000623 evo_mthd(push, 0x00a0, 2);
624 evo_data(push, 0x00000000);
625 evo_data(push, 0x00000000);
626 evo_mthd(push, 0x00c0, 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000627 evo_data(push, nv_fb->r_handle);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000628 evo_mthd(push, 0x0110, 2);
629 evo_data(push, 0x00000000);
630 evo_data(push, 0x00000000);
Ben Skeggse225f442012-11-21 14:40:21 +1000631 if (nv50_vers(sync) < NVD0_DISP_SYNC_CLASS) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000632 evo_mthd(push, 0x0800, 5);
633 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
634 evo_data(push, 0);
635 evo_data(push, (fb->height << 16) | fb->width);
636 evo_data(push, nv_fb->r_pitch);
637 evo_data(push, nv_fb->r_format);
638 } else {
639 evo_mthd(push, 0x0400, 5);
640 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
641 evo_data(push, 0);
642 evo_data(push, (fb->height << 16) | fb->width);
643 evo_data(push, nv_fb->r_pitch);
644 evo_data(push, nv_fb->r_format);
645 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000646 evo_mthd(push, 0x0080, 1);
647 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000648 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000649
650 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000651 return 0;
652}
653
Ben Skeggs26f6d882011-07-04 16:25:18 +1000654/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000655 * CRTC
656 *****************************************************************************/
657static int
Ben Skeggse225f442012-11-21 14:40:21 +1000658nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000659{
Ben Skeggse225f442012-11-21 14:40:21 +1000660 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000661 struct nouveau_connector *nv_connector;
662 struct drm_connector *connector;
663 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000664
Ben Skeggs488ff202011-10-17 10:38:10 +1000665 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000666 connector = &nv_connector->base;
667 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
Matt Roperf4510a22014-04-01 15:22:40 -0700668 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
Ben Skeggsde691852011-10-17 12:23:41 +1000669 mode = DITHERING_MODE_DYNAMIC2X2;
670 } else {
671 mode = nv_connector->dithering_mode;
672 }
673
674 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
675 if (connector->display_info.bpc >= 8)
676 mode |= DITHERING_DEPTH_8BPC;
677 } else {
678 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000679 }
680
Ben Skeggsde8268c2012-11-16 10:24:31 +1000681 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000682 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000683 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000684 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
685 evo_data(push, mode);
686 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000687 if (nv50_vers(mast) < NVE0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000688 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
689 evo_data(push, mode);
690 } else {
691 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
692 evo_data(push, mode);
693 }
694
Ben Skeggs438d99e2011-07-05 16:48:06 +1000695 if (update) {
696 evo_mthd(push, 0x0080, 1);
697 evo_data(push, 0x00000000);
698 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000699 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000700 }
701
702 return 0;
703}
704
705static int
Ben Skeggse225f442012-11-21 14:40:21 +1000706nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000707{
Ben Skeggse225f442012-11-21 14:40:21 +1000708 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000709 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000710 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000711 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000712 int mode = DRM_MODE_SCALE_NONE;
713 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000714
Ben Skeggs92854622011-11-11 23:49:06 +1000715 /* start off at the resolution we programmed the crtc for, this
716 * effectively handles NONE/FULL scaling
717 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000718 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000719 if (nv_connector && nv_connector->native_mode)
720 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000721
Ben Skeggs92854622011-11-11 23:49:06 +1000722 if (mode != DRM_MODE_SCALE_NONE)
723 omode = nv_connector->native_mode;
724 else
725 omode = umode;
726
727 oX = omode->hdisplay;
728 oY = omode->vdisplay;
729 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
730 oY *= 2;
731
732 /* add overscan compensation if necessary, will keep the aspect
733 * ratio the same as the backend mode unless overridden by the
734 * user setting both hborder and vborder properties.
735 */
736 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
737 (nv_connector->underscan == UNDERSCAN_AUTO &&
738 nv_connector->edid &&
739 drm_detect_hdmi_monitor(nv_connector->edid)))) {
740 u32 bX = nv_connector->underscan_hborder;
741 u32 bY = nv_connector->underscan_vborder;
742 u32 aspect = (oY << 19) / oX;
743
744 if (bX) {
745 oX -= (bX * 2);
746 if (bY) oY -= (bY * 2);
747 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
748 } else {
749 oX -= (oX >> 4) + 32;
750 if (bY) oY -= (bY * 2);
751 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000752 }
753 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000754
Ben Skeggs92854622011-11-11 23:49:06 +1000755 /* handle CENTER/ASPECT scaling, taking into account the areas
756 * removed already for overscan compensation
757 */
758 switch (mode) {
759 case DRM_MODE_SCALE_CENTER:
760 oX = min((u32)umode->hdisplay, oX);
761 oY = min((u32)umode->vdisplay, oY);
762 /* fall-through */
763 case DRM_MODE_SCALE_ASPECT:
764 if (oY < oX) {
765 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
766 oX = ((oY * aspect) + (aspect / 2)) >> 19;
767 } else {
768 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
769 oY = ((oX * aspect) + (aspect / 2)) >> 19;
770 }
771 break;
772 default:
773 break;
774 }
775
Ben Skeggsde8268c2012-11-16 10:24:31 +1000776 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000777 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000778 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000779 /*XXX: SCALE_CTRL_ACTIVE??? */
780 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
781 evo_data(push, (oY << 16) | oX);
782 evo_data(push, (oY << 16) | oX);
783 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
784 evo_data(push, 0x00000000);
785 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
786 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
787 } else {
788 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
789 evo_data(push, (oY << 16) | oX);
790 evo_data(push, (oY << 16) | oX);
791 evo_data(push, (oY << 16) | oX);
792 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
793 evo_data(push, 0x00000000);
794 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
795 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
796 }
797
798 evo_kick(push, mast);
799
Ben Skeggs3376ee32011-11-12 14:28:12 +1000800 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000801 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700802 nv50_display_flip_next(crtc, crtc->primary->fb,
803 NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000804 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000805 }
806
807 return 0;
808}
809
810static int
Ben Skeggse225f442012-11-21 14:40:21 +1000811nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000812{
Ben Skeggse225f442012-11-21 14:40:21 +1000813 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000814 u32 *push, hue, vib;
815 int adj;
816
817 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
818 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
819 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
820
821 push = evo_wait(mast, 16);
822 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000823 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000824 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
825 evo_data(push, (hue << 20) | (vib << 8));
826 } else {
827 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
828 evo_data(push, (hue << 20) | (vib << 8));
829 }
830
831 if (update) {
832 evo_mthd(push, 0x0080, 1);
833 evo_data(push, 0x00000000);
834 }
835 evo_kick(push, mast);
836 }
837
838 return 0;
839}
840
841static int
Ben Skeggse225f442012-11-21 14:40:21 +1000842nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000843 int x, int y, bool update)
844{
845 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000846 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000847 u32 *push;
848
Ben Skeggsde8268c2012-11-16 10:24:31 +1000849 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000850 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000851 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000852 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
853 evo_data(push, nvfb->nvbo->bo.offset >> 8);
854 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
855 evo_data(push, (fb->height << 16) | fb->width);
856 evo_data(push, nvfb->r_pitch);
857 evo_data(push, nvfb->r_format);
858 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
859 evo_data(push, (y << 16) | x);
Ben Skeggse225f442012-11-21 14:40:21 +1000860 if (nv50_vers(mast) > NV50_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000861 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +1000862 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000863 }
864 } else {
865 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
866 evo_data(push, nvfb->nvbo->bo.offset >> 8);
867 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
868 evo_data(push, (fb->height << 16) | fb->width);
869 evo_data(push, nvfb->r_pitch);
870 evo_data(push, nvfb->r_format);
Ben Skeggs8a423642014-08-10 04:10:19 +1000871 evo_data(push, nvfb->r_handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000872 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
873 evo_data(push, (y << 16) | x);
874 }
875
Ben Skeggsa46232e2011-07-07 15:23:48 +1000876 if (update) {
877 evo_mthd(push, 0x0080, 1);
878 evo_data(push, 0x00000000);
879 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000880 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000881 }
882
Ben Skeggs8a423642014-08-10 04:10:19 +1000883 nv_crtc->fb.handle = nvfb->r_handle;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000884 return 0;
885}
886
887static void
Ben Skeggse225f442012-11-21 14:40:21 +1000888nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000889{
Ben Skeggse225f442012-11-21 14:40:21 +1000890 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000891 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000892 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000893 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000894 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
895 evo_data(push, 0x85000000);
896 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
897 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000898 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000899 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
900 evo_data(push, 0x85000000);
901 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
902 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000903 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000904 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000905 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
906 evo_data(push, 0x85000000);
907 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
908 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000909 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000910 }
911 evo_kick(push, mast);
912 }
913}
914
915static void
Ben Skeggse225f442012-11-21 14:40:21 +1000916nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000917{
Ben Skeggse225f442012-11-21 14:40:21 +1000918 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000919 u32 *push = evo_wait(mast, 16);
920 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000921 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000922 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
923 evo_data(push, 0x05000000);
924 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000925 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000926 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
927 evo_data(push, 0x05000000);
928 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
929 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000930 } else {
931 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
932 evo_data(push, 0x05000000);
933 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
934 evo_data(push, 0x00000000);
935 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000936 evo_kick(push, mast);
937 }
938}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000939
Ben Skeggsde8268c2012-11-16 10:24:31 +1000940static void
Ben Skeggse225f442012-11-21 14:40:21 +1000941nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000942{
Ben Skeggse225f442012-11-21 14:40:21 +1000943 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000944
945 if (show)
Ben Skeggse225f442012-11-21 14:40:21 +1000946 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000947 else
Ben Skeggse225f442012-11-21 14:40:21 +1000948 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000949
950 if (update) {
951 u32 *push = evo_wait(mast, 2);
952 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000953 evo_mthd(push, 0x0080, 1);
954 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000955 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000956 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000957 }
958}
959
960static void
Ben Skeggse225f442012-11-21 14:40:21 +1000961nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000962{
963}
964
965static void
Ben Skeggse225f442012-11-21 14:40:21 +1000966nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000967{
968 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000969 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000970 u32 *push;
971
Ben Skeggse225f442012-11-21 14:40:21 +1000972 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000973
Ben Skeggs56d237d2014-05-19 14:54:33 +1000974 push = evo_wait(mast, 6);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000975 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000976 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000977 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
978 evo_data(push, 0x00000000);
979 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
980 evo_data(push, 0x40000000);
981 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000982 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000983 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
984 evo_data(push, 0x00000000);
985 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
986 evo_data(push, 0x40000000);
987 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
988 evo_data(push, 0x00000000);
989 } else {
990 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
991 evo_data(push, 0x00000000);
992 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
993 evo_data(push, 0x03000000);
994 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
995 evo_data(push, 0x00000000);
996 }
997
998 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000999 }
1000
Ben Skeggse225f442012-11-21 14:40:21 +10001001 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001002}
1003
1004static void
Ben Skeggse225f442012-11-21 14:40:21 +10001005nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001006{
1007 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001008 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001009 u32 *push;
1010
Ben Skeggsde8268c2012-11-16 10:24:31 +10001011 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001012 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001013 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001014 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001015 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001016 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1017 evo_data(push, 0xc0000000);
1018 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1019 } else
Ben Skeggse225f442012-11-21 14:40:21 +10001020 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001021 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001022 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001023 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1024 evo_data(push, 0xc0000000);
1025 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1026 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001027 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001028 } else {
1029 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
Ben Skeggs8a423642014-08-10 04:10:19 +10001030 evo_data(push, nv_crtc->fb.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001031 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1032 evo_data(push, 0x83000000);
1033 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1034 evo_data(push, 0x00000000);
1035 evo_data(push, 0x00000000);
1036 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10001037 evo_data(push, mast->base.vram.handle);
Ben Skeggsde8268c2012-11-16 10:24:31 +10001038 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1039 evo_data(push, 0xffffff00);
1040 }
1041
1042 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001043 }
1044
Ben Skeggse225f442012-11-21 14:40:21 +10001045 nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
Matt Roperf4510a22014-04-01 15:22:40 -07001046 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001047}
1048
1049static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001050nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001051 struct drm_display_mode *adjusted_mode)
1052{
Ben Skeggseb2e9682014-01-24 10:13:23 +10001053 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001054 return true;
1055}
1056
1057static int
Ben Skeggse225f442012-11-21 14:40:21 +10001058nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001059{
Matt Roperf4510a22014-04-01 15:22:40 -07001060 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001061 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001062 int ret;
1063
1064 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001065 if (ret == 0) {
1066 if (head->image)
1067 nouveau_bo_unpin(head->image);
1068 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001069 }
1070
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001071 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001072}
1073
1074static int
Ben Skeggse225f442012-11-21 14:40:21 +10001075nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001076 struct drm_display_mode *mode, int x, int y,
1077 struct drm_framebuffer *old_fb)
1078{
Ben Skeggse225f442012-11-21 14:40:21 +10001079 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001080 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1081 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001082 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1083 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1084 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1085 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
1086 u32 vblan2e = 0, vblan2s = 1;
Ben Skeggs3488c572012-03-12 11:42:20 +10001087 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001088 int ret;
1089
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001090 hactive = mode->htotal;
1091 hsynce = mode->hsync_end - mode->hsync_start - 1;
1092 hbackp = mode->htotal - mode->hsync_end;
1093 hblanke = hsynce + hbackp;
1094 hfrontp = mode->hsync_start - mode->hdisplay;
1095 hblanks = mode->htotal - hfrontp - 1;
1096
1097 vactive = mode->vtotal * vscan / ilace;
1098 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1099 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1100 vblanke = vsynce + vbackp;
1101 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1102 vblanks = vactive - vfrontp - 1;
1103 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1104 vblan2e = vactive + vsynce + vbackp;
1105 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1106 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001107 }
1108
Ben Skeggse225f442012-11-21 14:40:21 +10001109 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001110 if (ret)
1111 return ret;
1112
Ben Skeggsde8268c2012-11-16 10:24:31 +10001113 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001114 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001115 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001116 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1117 evo_data(push, 0x00800000 | mode->clock);
1118 evo_data(push, (ilace == 2) ? 2 : 0);
1119 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
1120 evo_data(push, 0x00000000);
1121 evo_data(push, (vactive << 16) | hactive);
1122 evo_data(push, ( vsynce << 16) | hsynce);
1123 evo_data(push, (vblanke << 16) | hblanke);
1124 evo_data(push, (vblanks << 16) | hblanks);
1125 evo_data(push, (vblan2e << 16) | vblan2s);
1126 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
1127 evo_data(push, 0x00000000);
1128 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1129 evo_data(push, 0x00000311);
1130 evo_data(push, 0x00000100);
1131 } else {
1132 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1133 evo_data(push, 0x00000000);
1134 evo_data(push, (vactive << 16) | hactive);
1135 evo_data(push, ( vsynce << 16) | hsynce);
1136 evo_data(push, (vblanke << 16) | hblanke);
1137 evo_data(push, (vblanks << 16) | hblanks);
1138 evo_data(push, (vblan2e << 16) | vblan2s);
1139 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1140 evo_data(push, 0x00000000); /* ??? */
1141 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1142 evo_data(push, mode->clock * 1000);
1143 evo_data(push, 0x00200000); /* ??? */
1144 evo_data(push, mode->clock * 1000);
1145 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1146 evo_data(push, 0x00000311);
1147 evo_data(push, 0x00000100);
1148 }
1149
1150 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001151 }
1152
1153 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001154 nv50_crtc_set_dither(nv_crtc, false);
1155 nv50_crtc_set_scale(nv_crtc, false);
1156 nv50_crtc_set_color_vibrance(nv_crtc, false);
Matt Roperf4510a22014-04-01 15:22:40 -07001157 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001158 return 0;
1159}
1160
1161static int
Ben Skeggse225f442012-11-21 14:40:21 +10001162nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001163 struct drm_framebuffer *old_fb)
1164{
Ben Skeggs77145f12012-07-31 16:16:21 +10001165 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001166 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1167 int ret;
1168
Matt Roperf4510a22014-04-01 15:22:40 -07001169 if (!crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001170 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001171 return 0;
1172 }
1173
Ben Skeggse225f442012-11-21 14:40:21 +10001174 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001175 if (ret)
1176 return ret;
1177
Ben Skeggse225f442012-11-21 14:40:21 +10001178 nv50_display_flip_stop(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07001179 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1180 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001181 return 0;
1182}
1183
1184static int
Ben Skeggse225f442012-11-21 14:40:21 +10001185nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001186 struct drm_framebuffer *fb, int x, int y,
1187 enum mode_set_atomic state)
1188{
1189 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001190 nv50_display_flip_stop(crtc);
1191 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001192 return 0;
1193}
1194
1195static void
Ben Skeggse225f442012-11-21 14:40:21 +10001196nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001197{
Ben Skeggse225f442012-11-21 14:40:21 +10001198 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001199 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1200 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1201 int i;
1202
1203 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001204 u16 r = nv_crtc->lut.r[i] >> 2;
1205 u16 g = nv_crtc->lut.g[i] >> 2;
1206 u16 b = nv_crtc->lut.b[i] >> 2;
1207
Ben Skeggs0ad72862014-08-10 04:10:22 +10001208 if (disp->disp->oclass < NVD0_DISP_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001209 writew(r + 0x0000, lut + (i * 0x08) + 0);
1210 writew(g + 0x0000, lut + (i * 0x08) + 2);
1211 writew(b + 0x0000, lut + (i * 0x08) + 4);
1212 } else {
1213 writew(r + 0x6000, lut + (i * 0x20) + 0);
1214 writew(g + 0x6000, lut + (i * 0x20) + 2);
1215 writew(b + 0x6000, lut + (i * 0x20) + 4);
1216 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001217 }
1218}
1219
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001220static void
1221nv50_crtc_disable(struct drm_crtc *crtc)
1222{
1223 struct nv50_head *head = nv50_head(crtc);
Ben Skeggsefa366f2014-06-05 12:56:35 +10001224 evo_sync(crtc->dev);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001225 if (head->image)
1226 nouveau_bo_unpin(head->image);
1227 nouveau_bo_ref(NULL, &head->image);
1228}
1229
Ben Skeggs438d99e2011-07-05 16:48:06 +10001230static int
Ben Skeggse225f442012-11-21 14:40:21 +10001231nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001232 uint32_t handle, uint32_t width, uint32_t height)
1233{
1234 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1235 struct drm_device *dev = crtc->dev;
1236 struct drm_gem_object *gem;
1237 struct nouveau_bo *nvbo;
1238 bool visible = (handle != 0);
1239 int i, ret = 0;
1240
1241 if (visible) {
1242 if (width != 64 || height != 64)
1243 return -EINVAL;
1244
1245 gem = drm_gem_object_lookup(dev, file_priv, handle);
1246 if (unlikely(!gem))
1247 return -ENOENT;
1248 nvbo = nouveau_gem_object(gem);
1249
1250 ret = nouveau_bo_map(nvbo);
1251 if (ret == 0) {
1252 for (i = 0; i < 64 * 64; i++) {
1253 u32 v = nouveau_bo_rd32(nvbo, i);
1254 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1255 }
1256 nouveau_bo_unmap(nvbo);
1257 }
1258
1259 drm_gem_object_unreference_unlocked(gem);
1260 }
1261
1262 if (visible != nv_crtc->cursor.visible) {
Ben Skeggse225f442012-11-21 14:40:21 +10001263 nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001264 nv_crtc->cursor.visible = visible;
1265 }
1266
1267 return ret;
1268}
1269
1270static int
Ben Skeggse225f442012-11-21 14:40:21 +10001271nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001272{
Ben Skeggse225f442012-11-21 14:40:21 +10001273 struct nv50_curs *curs = nv50_curs(crtc);
1274 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001275 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1276 nvif_wr32(&chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001277 return 0;
1278}
1279
1280static void
Ben Skeggse225f442012-11-21 14:40:21 +10001281nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001282 uint32_t start, uint32_t size)
1283{
1284 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Dan Carpenterbdefc8c2013-11-28 01:18:47 +03001285 u32 end = min_t(u32, start + size, 256);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001286 u32 i;
1287
1288 for (i = start; i < end; i++) {
1289 nv_crtc->lut.r[i] = r[i];
1290 nv_crtc->lut.g[i] = g[i];
1291 nv_crtc->lut.b[i] = b[i];
1292 }
1293
Ben Skeggse225f442012-11-21 14:40:21 +10001294 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001295}
1296
1297static void
Ben Skeggse225f442012-11-21 14:40:21 +10001298nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001299{
1300 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001301 struct nv50_disp *disp = nv50_disp(crtc->dev);
1302 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs0ad72862014-08-10 04:10:22 +10001303 struct nv50_fbdma *fbdma;
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001304
Ben Skeggs0ad72862014-08-10 04:10:22 +10001305 list_for_each_entry(fbdma, &disp->fbdma, head) {
1306 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1307 }
1308
1309 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1310 nv50_pioc_destroy(&head->oimm.base);
1311 nv50_dmac_destroy(&head->sync.base, disp->disp);
1312 nv50_pioc_destroy(&head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001313
1314 /*XXX: this shouldn't be necessary, but the core doesn't call
1315 * disconnect() during the cleanup paths
1316 */
1317 if (head->image)
1318 nouveau_bo_unpin(head->image);
1319 nouveau_bo_ref(NULL, &head->image);
1320
Ben Skeggs438d99e2011-07-05 16:48:06 +10001321 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001322 if (nv_crtc->cursor.nvbo)
1323 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001324 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001325
Ben Skeggs438d99e2011-07-05 16:48:06 +10001326 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001327 if (nv_crtc->lut.nvbo)
1328 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001329 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001330
Ben Skeggs438d99e2011-07-05 16:48:06 +10001331 drm_crtc_cleanup(crtc);
1332 kfree(crtc);
1333}
1334
Ben Skeggse225f442012-11-21 14:40:21 +10001335static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1336 .dpms = nv50_crtc_dpms,
1337 .prepare = nv50_crtc_prepare,
1338 .commit = nv50_crtc_commit,
1339 .mode_fixup = nv50_crtc_mode_fixup,
1340 .mode_set = nv50_crtc_mode_set,
1341 .mode_set_base = nv50_crtc_mode_set_base,
1342 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1343 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001344 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001345};
1346
Ben Skeggse225f442012-11-21 14:40:21 +10001347static const struct drm_crtc_funcs nv50_crtc_func = {
1348 .cursor_set = nv50_crtc_cursor_set,
1349 .cursor_move = nv50_crtc_cursor_move,
1350 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001351 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001352 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001353 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001354};
1355
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001356static void
Ben Skeggse225f442012-11-21 14:40:21 +10001357nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001358{
1359}
1360
1361static void
Ben Skeggse225f442012-11-21 14:40:21 +10001362nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001363{
1364}
1365
Ben Skeggs438d99e2011-07-05 16:48:06 +10001366static int
Ben Skeggs0ad72862014-08-10 04:10:22 +10001367nv50_crtc_create(struct drm_device *dev, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001368{
Ben Skeggse225f442012-11-21 14:40:21 +10001369 struct nv50_disp *disp = nv50_disp(dev);
1370 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001371 struct drm_crtc *crtc;
1372 int ret, i;
1373
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001374 head = kzalloc(sizeof(*head), GFP_KERNEL);
1375 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001376 return -ENOMEM;
1377
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001378 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001379 head->base.set_dither = nv50_crtc_set_dither;
1380 head->base.set_scale = nv50_crtc_set_scale;
1381 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001382 head->base.color_vibrance = 50;
1383 head->base.vibrant_hue = 0;
Ben Skeggse225f442012-11-21 14:40:21 +10001384 head->base.cursor.set_offset = nv50_cursor_set_offset;
1385 head->base.cursor.set_pos = nv50_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001386 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001387 head->base.lut.r[i] = i << 8;
1388 head->base.lut.g[i] = i << 8;
1389 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001390 }
1391
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001392 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001393 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1394 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001395 drm_mode_crtc_set_gamma_size(crtc, 256);
1396
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001397 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001398 0, 0x0000, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001399 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001400 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001401 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001402 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001403 if (ret)
1404 nouveau_bo_unpin(head->base.lut.nvbo);
1405 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001406 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001407 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001408 }
1409
1410 if (ret)
1411 goto out;
1412
Ben Skeggse225f442012-11-21 14:40:21 +10001413 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001414
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001415 /* allocate cursor resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001416 ret = nv50_curs_create(disp->disp, index, &head->curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001417 if (ret)
1418 goto out;
1419
1420 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
1421 0, 0x0000, NULL, &head->base.cursor.nvbo);
1422 if (!ret) {
1423 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001424 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001425 ret = nouveau_bo_map(head->base.cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001426 if (ret)
1427 nouveau_bo_unpin(head->base.lut.nvbo);
1428 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001429 if (ret)
1430 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
1431 }
1432
1433 if (ret)
1434 goto out;
1435
1436 /* allocate page flip / sync resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001437 ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
1438 &head->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001439 if (ret)
1440 goto out;
1441
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001442 head->sync.addr = EVO_FLIP_SEM0(index);
1443 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001444
1445 /* allocate overlay resources */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001446 ret = nv50_oimm_create(disp->disp, index, &head->oimm);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001447 if (ret)
1448 goto out;
1449
Ben Skeggs410f3ec2014-08-10 04:10:25 +10001450 ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
1451 &head->ovly);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001452 if (ret)
1453 goto out;
1454
Ben Skeggs438d99e2011-07-05 16:48:06 +10001455out:
1456 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001457 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001458 return ret;
1459}
1460
1461/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001462 * DAC
1463 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001464static void
Ben Skeggse225f442012-11-21 14:40:21 +10001465nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001466{
1467 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001468 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001469 struct {
1470 struct nv50_disp_mthd_v1 base;
1471 struct nv50_disp_dac_pwr_v0 pwr;
1472 } args = {
1473 .base.version = 1,
1474 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1475 .base.hasht = nv_encoder->dcb->hasht,
1476 .base.hashm = nv_encoder->dcb->hashm,
1477 .pwr.state = 1,
1478 .pwr.data = 1,
1479 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1480 mode != DRM_MODE_DPMS_OFF),
1481 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1482 mode != DRM_MODE_DPMS_OFF),
1483 };
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001484
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001485
Ben Skeggsbf0eb892014-08-10 04:10:26 +10001486 nvif_mthd(disp->disp, 0, &args, sizeof(args));
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001487}
1488
1489static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001490nv50_dac_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001491 const struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001492 struct drm_display_mode *adjusted_mode)
1493{
1494 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1495 struct nouveau_connector *nv_connector;
1496
1497 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1498 if (nv_connector && nv_connector->native_mode) {
1499 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1500 int id = adjusted_mode->base.id;
1501 *adjusted_mode = *nv_connector->native_mode;
1502 adjusted_mode->base.id = id;
1503 }
1504 }
1505
1506 return true;
1507}
1508
1509static void
Ben Skeggse225f442012-11-21 14:40:21 +10001510nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001511{
1512}
1513
1514static void
Ben Skeggse225f442012-11-21 14:40:21 +10001515nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001516 struct drm_display_mode *adjusted_mode)
1517{
Ben Skeggse225f442012-11-21 14:40:21 +10001518 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001519 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1520 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001521 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001522
Ben Skeggse225f442012-11-21 14:40:21 +10001523 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001524
Ben Skeggs97b19b52012-11-16 11:21:37 +10001525 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001526 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001527 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001528 u32 syncs = 0x00000000;
1529
1530 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1531 syncs |= 0x00000001;
1532 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1533 syncs |= 0x00000002;
1534
1535 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1536 evo_data(push, 1 << nv_crtc->index);
1537 evo_data(push, syncs);
1538 } else {
1539 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1540 u32 syncs = 0x00000001;
1541
1542 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1543 syncs |= 0x00000008;
1544 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1545 syncs |= 0x00000010;
1546
1547 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1548 magic |= 0x00000001;
1549
1550 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1551 evo_data(push, syncs);
1552 evo_data(push, magic);
1553 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1554 evo_data(push, 1 << nv_crtc->index);
1555 }
1556
1557 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001558 }
1559
1560 nv_encoder->crtc = encoder->crtc;
1561}
1562
1563static void
Ben Skeggse225f442012-11-21 14:40:21 +10001564nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001565{
1566 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001567 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001568 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001569 u32 *push;
1570
1571 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001572 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001573
Ben Skeggs97b19b52012-11-16 11:21:37 +10001574 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001575 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001576 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001577 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1578 evo_data(push, 0x00000000);
1579 } else {
1580 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1581 evo_data(push, 0x00000000);
1582 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001583 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001584 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001585 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001586
1587 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001588}
1589
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001590static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001591nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001592{
Ben Skeggse225f442012-11-21 14:40:21 +10001593 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs35b21d32012-11-08 12:08:55 +10001594 int ret, or = nouveau_encoder(encoder)->or;
Ben Skeggsd40ee482013-06-03 16:40:14 +10001595 u32 load = nouveau_drm(encoder->dev)->vbios.dactestval;
1596 if (load == 0)
1597 load = 340;
Ben Skeggsb6819932011-07-08 11:14:50 +10001598
Ben Skeggs0ad72862014-08-10 04:10:22 +10001599 ret = nvif_exec(disp->disp, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
Ben Skeggs4b31ebc2013-09-04 11:01:42 +10001600 if (ret || !load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001601 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001602
Ben Skeggs35b21d32012-11-08 12:08:55 +10001603 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001604}
1605
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001606static void
Ben Skeggse225f442012-11-21 14:40:21 +10001607nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001608{
1609 drm_encoder_cleanup(encoder);
1610 kfree(encoder);
1611}
1612
Ben Skeggse225f442012-11-21 14:40:21 +10001613static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1614 .dpms = nv50_dac_dpms,
1615 .mode_fixup = nv50_dac_mode_fixup,
1616 .prepare = nv50_dac_disconnect,
1617 .commit = nv50_dac_commit,
1618 .mode_set = nv50_dac_mode_set,
1619 .disable = nv50_dac_disconnect,
1620 .get_crtc = nv50_display_crtc_get,
1621 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001622};
1623
Ben Skeggse225f442012-11-21 14:40:21 +10001624static const struct drm_encoder_funcs nv50_dac_func = {
1625 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001626};
1627
1628static int
Ben Skeggse225f442012-11-21 14:40:21 +10001629nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001630{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001631 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001632 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001633 struct nouveau_encoder *nv_encoder;
1634 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001635 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001636
1637 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1638 if (!nv_encoder)
1639 return -ENOMEM;
1640 nv_encoder->dcb = dcbe;
1641 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001642 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001643
1644 encoder = to_drm_encoder(nv_encoder);
1645 encoder->possible_crtcs = dcbe->heads;
1646 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001647 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001648 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001649
1650 drm_mode_connector_attach_encoder(connector, encoder);
1651 return 0;
1652}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001653
1654/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001655 * Audio
1656 *****************************************************************************/
1657static void
Ben Skeggse225f442012-11-21 14:40:21 +10001658nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001659{
1660 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1661 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001662 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001663
1664 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1665 if (!drm_detect_monitor_audio(nv_connector->edid))
1666 return;
1667
Ben Skeggs78951d22011-11-11 18:13:13 +10001668 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs78951d22011-11-11 18:13:13 +10001669
Ben Skeggs0ad72862014-08-10 04:10:22 +10001670 nvif_exec(disp->disp, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or,
1671 nv_connector->base.eld,
1672 nv_connector->base.eld[2] * 4);
Ben Skeggs78951d22011-11-11 18:13:13 +10001673}
1674
1675static void
Ben Skeggse225f442012-11-21 14:40:21 +10001676nv50_audio_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001677{
1678 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001679 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001680
Ben Skeggs0ad72862014-08-10 04:10:22 +10001681 nvif_exec(disp->disp, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or, NULL, 0);
Ben Skeggs78951d22011-11-11 18:13:13 +10001682}
1683
1684/******************************************************************************
1685 * HDMI
1686 *****************************************************************************/
1687static void
Ben Skeggse225f442012-11-21 14:40:21 +10001688nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001689{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001690 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1691 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1692 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001693 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001694 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001695 u32 rekey = 56; /* binary driver, and tegra constant */
1696 u32 max_ac_packet;
Ben Skeggs0ad72862014-08-10 04:10:22 +10001697 u32 data;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001698
1699 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1700 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1701 return;
1702
1703 max_ac_packet = mode->htotal - mode->hdisplay;
1704 max_ac_packet -= rekey;
1705 max_ac_packet -= 18; /* constant from tegra */
1706 max_ac_packet /= 32;
1707
Ben Skeggs0ad72862014-08-10 04:10:22 +10001708 data = NV84_DISP_SOR_HDMI_PWR_STATE_ON | (max_ac_packet << 16) | rekey;
1709 nvif_exec(disp->disp, NV84_DISP_SOR_HDMI_PWR + moff, &data, sizeof(data));
Ben Skeggs091e40c2011-11-11 20:46:00 +10001710
Ben Skeggse225f442012-11-21 14:40:21 +10001711 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001712}
1713
1714static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001715nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +10001716{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001717 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001718 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001719 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
Ben Skeggs0ad72862014-08-10 04:10:22 +10001720 u32 data = 0;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001721
Ben Skeggse225f442012-11-21 14:40:21 +10001722 nv50_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001723
Ben Skeggs0ad72862014-08-10 04:10:22 +10001724 nvif_exec(disp->disp, NV84_DISP_SOR_HDMI_PWR + moff, &data, sizeof(data));
Ben Skeggs78951d22011-11-11 18:13:13 +10001725}
1726
1727/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001728 * SOR
1729 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001730static void
Ben Skeggse225f442012-11-21 14:40:21 +10001731nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001732{
1733 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1734 struct drm_device *dev = encoder->dev;
Ben Skeggse225f442012-11-21 14:40:21 +10001735 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001736 struct drm_encoder *partner;
Ben Skeggs0ad72862014-08-10 04:10:22 +10001737 u32 mthd, data;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001738
1739 nv_encoder->last_dpms = mode;
1740
1741 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1742 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1743
1744 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1745 continue;
1746
1747 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001748 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001749 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1750 return;
1751 break;
1752 }
1753 }
1754
Ben Skeggs276e5262014-06-30 11:10:02 +10001755 mthd = (ffs(nv_encoder->dcb->heads) - 1) << 3;
1756 mthd |= (ffs(nv_encoder->dcb->sorconf.link) - 1) << 2;
Ben Skeggs48743222014-05-31 01:48:06 +10001757 mthd |= nv_encoder->or;
1758
1759 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10001760 data = 1;
1761 nvif_exec(disp->disp, NV50_DISP_SOR_PWR | mthd, &data, sizeof(data));
Ben Skeggs48743222014-05-31 01:48:06 +10001762 mthd |= NV94_DISP_SOR_DP_PWR;
1763 } else {
1764 mthd |= NV50_DISP_SOR_PWR;
1765 }
1766
Ben Skeggs0ad72862014-08-10 04:10:22 +10001767 data = (mode == DRM_MODE_DPMS_ON);
1768 nvif_exec(disp->disp, mthd, &data, sizeof(data));
Ben Skeggs83fc0832011-07-05 13:08:40 +10001769}
1770
1771static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001772nv50_sor_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001773 const struct drm_display_mode *mode,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001774 struct drm_display_mode *adjusted_mode)
1775{
1776 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1777 struct nouveau_connector *nv_connector;
1778
1779 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1780 if (nv_connector && nv_connector->native_mode) {
1781 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1782 int id = adjusted_mode->base.id;
1783 *adjusted_mode = *nv_connector->native_mode;
1784 adjusted_mode->base.id = id;
1785 }
1786 }
1787
1788 return true;
1789}
1790
1791static void
Ben Skeggse84a35a2014-06-05 10:59:55 +10001792nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
1793{
1794 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
1795 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
1796 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
1797 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
1798 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
1799 evo_data(push, (nv_encoder->ctrl = temp));
1800 } else {
1801 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1802 evo_data(push, (nv_encoder->ctrl = temp));
1803 }
1804 evo_kick(push, mast);
1805 }
1806}
1807
1808static void
Ben Skeggse225f442012-11-21 14:40:21 +10001809nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001810{
1811 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001812 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001813
1814 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1815 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001816
1817 if (nv_crtc) {
1818 nv50_crtc_prepare(&nv_crtc->base);
1819 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
1820 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
1821 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001822}
1823
1824static void
Ben Skeggse225f442012-11-21 14:40:21 +10001825nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001826{
1827}
1828
1829static void
Ben Skeggse225f442012-11-21 14:40:21 +10001830nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001831 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001832{
Ben Skeggse225f442012-11-21 14:40:21 +10001833 struct nv50_disp *disp = nv50_disp(encoder->dev);
1834 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001835 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001836 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001837 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1838 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001839 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001840 struct nvbios *bios = &drm->vbios;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001841 u32 lvds = 0, mask, ctrl;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001842 u8 owner = 1 << nv_crtc->index;
1843 u8 proto = 0xf;
1844 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001845
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001846 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001847 nv_encoder->crtc = encoder->crtc;
1848
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001849 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001850 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001851 if (nv_encoder->dcb->sorconf.link & 1) {
1852 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001853 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001854 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001855 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001856 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001857 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001858 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001859
Ben Skeggse84a35a2014-06-05 10:59:55 +10001860 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001861 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001862 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001863 proto = 0x0;
1864
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001865 if (bios->fp_no_ddc) {
1866 if (bios->fp.dual_link)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001867 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001868 if (bios->fp.if_is_24bit)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001869 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001870 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001871 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001872 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001873 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001874 } else
1875 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001876 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001877 }
1878
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001879 if (lvds & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001880 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001881 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001882 } else {
1883 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001884 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001885 }
1886
1887 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001888 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001889 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001890
Ben Skeggs0ad72862014-08-10 04:10:22 +10001891 nvif_exec(disp->disp, NV50_DISP_SOR_LVDS_SCRIPT + nv_encoder->or, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001892 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001893 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001894 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001895 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001896 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001897 } else
1898 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001899 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001900 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001901 } else {
1902 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1903 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001904 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001905
1906 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001907 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001908 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001909 proto = 0x9;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001910 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001911 default:
1912 BUG_ON(1);
1913 break;
1914 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001915
Ben Skeggse84a35a2014-06-05 10:59:55 +10001916 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001917
Ben Skeggse84a35a2014-06-05 10:59:55 +10001918 if (nv50_vers(mast) >= NVD0_DISP_CLASS) {
1919 u32 *push = evo_wait(mast, 3);
1920 if (push) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001921 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1922 u32 syncs = 0x00000001;
1923
1924 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1925 syncs |= 0x00000008;
1926 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1927 syncs |= 0x00000010;
1928
1929 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1930 magic |= 0x00000001;
1931
1932 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1933 evo_data(push, syncs | (depth << 6));
1934 evo_data(push, magic);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001935 evo_kick(push, mast);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001936 }
1937
Ben Skeggse84a35a2014-06-05 10:59:55 +10001938 ctrl = proto << 8;
1939 mask = 0x00000f00;
1940 } else {
1941 ctrl = (depth << 16) | (proto << 8);
1942 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1943 ctrl |= 0x00001000;
1944 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1945 ctrl |= 0x00002000;
1946 mask = 0x000f3f00;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001947 }
1948
Ben Skeggse84a35a2014-06-05 10:59:55 +10001949 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001950}
1951
1952static void
Ben Skeggse225f442012-11-21 14:40:21 +10001953nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001954{
1955 drm_encoder_cleanup(encoder);
1956 kfree(encoder);
1957}
1958
Ben Skeggse225f442012-11-21 14:40:21 +10001959static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
1960 .dpms = nv50_sor_dpms,
1961 .mode_fixup = nv50_sor_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10001962 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10001963 .commit = nv50_sor_commit,
1964 .mode_set = nv50_sor_mode_set,
1965 .disable = nv50_sor_disconnect,
1966 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001967};
1968
Ben Skeggse225f442012-11-21 14:40:21 +10001969static const struct drm_encoder_funcs nv50_sor_func = {
1970 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001971};
1972
1973static int
Ben Skeggse225f442012-11-21 14:40:21 +10001974nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001975{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001976 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10001977 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001978 struct nouveau_encoder *nv_encoder;
1979 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001980 int type;
1981
1982 switch (dcbe->type) {
1983 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1984 case DCB_OUTPUT_TMDS:
1985 case DCB_OUTPUT_DP:
1986 default:
1987 type = DRM_MODE_ENCODER_TMDS;
1988 break;
1989 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001990
1991 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1992 if (!nv_encoder)
1993 return -ENOMEM;
1994 nv_encoder->dcb = dcbe;
1995 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001996 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001997 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1998
1999 encoder = to_drm_encoder(nv_encoder);
2000 encoder->possible_crtcs = dcbe->heads;
2001 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10002002 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10002003 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002004
2005 drm_mode_connector_attach_encoder(connector, encoder);
2006 return 0;
2007}
Ben Skeggs26f6d882011-07-04 16:25:18 +10002008
2009/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10002010 * PIOR
2011 *****************************************************************************/
2012
2013static void
2014nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2015{
2016 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2017 struct nv50_disp *disp = nv50_disp(encoder->dev);
2018 u32 mthd = (nv_encoder->dcb->type << 12) | nv_encoder->or;
2019 u32 ctrl = (mode == DRM_MODE_DPMS_ON);
Ben Skeggs0ad72862014-08-10 04:10:22 +10002020 nvif_exec(disp->disp, NV50_DISP_PIOR_PWR + mthd, &ctrl, sizeof(ctrl));
Ben Skeggseb6313a2013-02-11 09:52:58 +10002021}
2022
2023static bool
2024nv50_pior_mode_fixup(struct drm_encoder *encoder,
2025 const struct drm_display_mode *mode,
2026 struct drm_display_mode *adjusted_mode)
2027{
2028 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2029 struct nouveau_connector *nv_connector;
2030
2031 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2032 if (nv_connector && nv_connector->native_mode) {
2033 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
2034 int id = adjusted_mode->base.id;
2035 *adjusted_mode = *nv_connector->native_mode;
2036 adjusted_mode->base.id = id;
2037 }
2038 }
2039
2040 adjusted_mode->clock *= 2;
2041 return true;
2042}
2043
2044static void
2045nv50_pior_commit(struct drm_encoder *encoder)
2046{
2047}
2048
2049static void
2050nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2051 struct drm_display_mode *adjusted_mode)
2052{
2053 struct nv50_mast *mast = nv50_mast(encoder->dev);
2054 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2055 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2056 struct nouveau_connector *nv_connector;
2057 u8 owner = 1 << nv_crtc->index;
2058 u8 proto, depth;
2059 u32 *push;
2060
2061 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2062 switch (nv_connector->base.display_info.bpc) {
2063 case 10: depth = 0x6; break;
2064 case 8: depth = 0x5; break;
2065 case 6: depth = 0x2; break;
2066 default: depth = 0x0; break;
2067 }
2068
2069 switch (nv_encoder->dcb->type) {
2070 case DCB_OUTPUT_TMDS:
2071 case DCB_OUTPUT_DP:
2072 proto = 0x0;
2073 break;
2074 default:
2075 BUG_ON(1);
2076 break;
2077 }
2078
2079 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2080
2081 push = evo_wait(mast, 8);
2082 if (push) {
2083 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
2084 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2085 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2086 ctrl |= 0x00001000;
2087 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2088 ctrl |= 0x00002000;
2089 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2090 evo_data(push, ctrl);
2091 }
2092
2093 evo_kick(push, mast);
2094 }
2095
2096 nv_encoder->crtc = encoder->crtc;
2097}
2098
2099static void
2100nv50_pior_disconnect(struct drm_encoder *encoder)
2101{
2102 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2103 struct nv50_mast *mast = nv50_mast(encoder->dev);
2104 const int or = nv_encoder->or;
2105 u32 *push;
2106
2107 if (nv_encoder->crtc) {
2108 nv50_crtc_prepare(nv_encoder->crtc);
2109
2110 push = evo_wait(mast, 4);
2111 if (push) {
2112 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
2113 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2114 evo_data(push, 0x00000000);
2115 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002116 evo_kick(push, mast);
2117 }
2118 }
2119
2120 nv_encoder->crtc = NULL;
2121}
2122
2123static void
2124nv50_pior_destroy(struct drm_encoder *encoder)
2125{
2126 drm_encoder_cleanup(encoder);
2127 kfree(encoder);
2128}
2129
2130static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2131 .dpms = nv50_pior_dpms,
2132 .mode_fixup = nv50_pior_mode_fixup,
2133 .prepare = nv50_pior_disconnect,
2134 .commit = nv50_pior_commit,
2135 .mode_set = nv50_pior_mode_set,
2136 .disable = nv50_pior_disconnect,
2137 .get_crtc = nv50_display_crtc_get,
2138};
2139
2140static const struct drm_encoder_funcs nv50_pior_func = {
2141 .destroy = nv50_pior_destroy,
2142};
2143
2144static int
2145nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2146{
2147 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002148 struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
Ben Skeggseb6313a2013-02-11 09:52:58 +10002149 struct nouveau_i2c_port *ddc = NULL;
2150 struct nouveau_encoder *nv_encoder;
2151 struct drm_encoder *encoder;
2152 int type;
2153
2154 switch (dcbe->type) {
2155 case DCB_OUTPUT_TMDS:
2156 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2157 type = DRM_MODE_ENCODER_TMDS;
2158 break;
2159 case DCB_OUTPUT_DP:
2160 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2161 type = DRM_MODE_ENCODER_TMDS;
2162 break;
2163 default:
2164 return -ENODEV;
2165 }
2166
2167 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2168 if (!nv_encoder)
2169 return -ENOMEM;
2170 nv_encoder->dcb = dcbe;
2171 nv_encoder->or = ffs(dcbe->or) - 1;
2172 nv_encoder->i2c = ddc;
2173
2174 encoder = to_drm_encoder(nv_encoder);
2175 encoder->possible_crtcs = dcbe->heads;
2176 encoder->possible_clones = 0;
2177 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2178 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2179
2180 drm_mode_connector_attach_encoder(connector, encoder);
2181 return 0;
2182}
2183
2184/******************************************************************************
Ben Skeggsab0af552014-08-10 04:10:19 +10002185 * Framebuffer
2186 *****************************************************************************/
2187
Ben Skeggs8a423642014-08-10 04:10:19 +10002188static void
Ben Skeggs0ad72862014-08-10 04:10:22 +10002189nv50_fbdma_fini(struct nv50_fbdma *fbdma)
Ben Skeggs8a423642014-08-10 04:10:19 +10002190{
Ben Skeggs0ad72862014-08-10 04:10:22 +10002191 int i;
2192 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2193 nvif_object_fini(&fbdma->base[i]);
2194 nvif_object_fini(&fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002195 list_del(&fbdma->head);
2196 kfree(fbdma);
2197}
2198
2199static int
2200nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2201{
2202 struct nouveau_drm *drm = nouveau_drm(dev);
2203 struct nv50_disp *disp = nv50_disp(dev);
2204 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggs4acfd702014-08-10 04:10:24 +10002205 struct __attribute__ ((packed)) {
2206 struct nv_dma_v0 base;
2207 union {
2208 struct nv50_dma_v0 nv50;
2209 struct gf100_dma_v0 gf100;
2210 struct gf110_dma_v0 gf110;
2211 };
2212 } args = {};
Ben Skeggs8a423642014-08-10 04:10:19 +10002213 struct nv50_fbdma *fbdma;
2214 struct drm_crtc *crtc;
Ben Skeggs4acfd702014-08-10 04:10:24 +10002215 u32 size = sizeof(args.base);
Ben Skeggs8a423642014-08-10 04:10:19 +10002216 int ret;
2217
2218 list_for_each_entry(fbdma, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002219 if (fbdma->core.handle == name)
Ben Skeggs8a423642014-08-10 04:10:19 +10002220 return 0;
2221 }
2222
2223 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2224 if (!fbdma)
2225 return -ENOMEM;
2226 list_add(&fbdma->head, &disp->fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002227
Ben Skeggs4acfd702014-08-10 04:10:24 +10002228 args.base.target = NV_DMA_V0_TARGET_VRAM;
2229 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2230 args.base.start = offset;
2231 args.base.limit = offset + length - 1;
Ben Skeggs8a423642014-08-10 04:10:19 +10002232
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002233 if (drm->device.info.chipset < 0x80) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002234 args.nv50.part = NV50_DMA_V0_PART_256;
2235 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002236 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002237 if (drm->device.info.chipset < 0xc0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002238 args.nv50.part = NV50_DMA_V0_PART_256;
2239 args.nv50.kind = kind;
2240 size += sizeof(args.nv50);
Ben Skeggs8a423642014-08-10 04:10:19 +10002241 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002242 if (drm->device.info.chipset < 0xd0) {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002243 args.gf100.kind = kind;
2244 size += sizeof(args.gf100);
Ben Skeggs8a423642014-08-10 04:10:19 +10002245 } else {
Ben Skeggs4acfd702014-08-10 04:10:24 +10002246 args.gf110.page = GF110_DMA_V0_PAGE_LP;
2247 args.gf110.kind = kind;
2248 size += sizeof(args.gf110);
Ben Skeggs8a423642014-08-10 04:10:19 +10002249 }
2250
2251 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002252 struct nv50_head *head = nv50_head(crtc);
2253 int ret = nvif_object_init(&head->sync.base.base.user, NULL,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002254 name, NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002255 &fbdma->base[head->base.index]);
Ben Skeggs8a423642014-08-10 04:10:19 +10002256 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002257 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002258 return ret;
2259 }
2260 }
2261
Ben Skeggs0ad72862014-08-10 04:10:22 +10002262 ret = nvif_object_init(&mast->base.base.user, NULL, name,
Ben Skeggs4acfd702014-08-10 04:10:24 +10002263 NV_DMA_IN_MEMORY, &args, size,
Ben Skeggs0ad72862014-08-10 04:10:22 +10002264 &fbdma->core);
Ben Skeggs8a423642014-08-10 04:10:19 +10002265 if (ret) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002266 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002267 return ret;
2268 }
2269
2270 return 0;
2271}
2272
Ben Skeggsab0af552014-08-10 04:10:19 +10002273static void
2274nv50_fb_dtor(struct drm_framebuffer *fb)
2275{
2276}
2277
2278static int
2279nv50_fb_ctor(struct drm_framebuffer *fb)
2280{
2281 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2282 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2283 struct nouveau_bo *nvbo = nv_fb->nvbo;
Ben Skeggs8a423642014-08-10 04:10:19 +10002284 struct nv50_disp *disp = nv50_disp(fb->dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002285 struct nouveau_fb *pfb = nvkm_fb(&drm->device);
Ben Skeggs8a423642014-08-10 04:10:19 +10002286 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2287 u8 tile = nvbo->tile_mode;
Ben Skeggsab0af552014-08-10 04:10:19 +10002288
2289 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
2290 NV_ERROR(drm, "framebuffer requires contiguous bo\n");
2291 return -EINVAL;
2292 }
2293
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002294 if (drm->device.info.chipset >= 0xc0)
Ben Skeggs8a423642014-08-10 04:10:19 +10002295 tile >>= 4; /* yep.. */
2296
Ben Skeggsab0af552014-08-10 04:10:19 +10002297 switch (fb->depth) {
2298 case 8: nv_fb->r_format = 0x1e00; break;
2299 case 15: nv_fb->r_format = 0xe900; break;
2300 case 16: nv_fb->r_format = 0xe800; break;
2301 case 24:
2302 case 32: nv_fb->r_format = 0xcf00; break;
2303 case 30: nv_fb->r_format = 0xd100; break;
2304 default:
2305 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2306 return -EINVAL;
2307 }
2308
Ben Skeggs0ad72862014-08-10 04:10:22 +10002309 if (disp->disp->oclass < NV84_DISP_CLASS) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002310 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2311 (fb->pitches[0] | 0x00100000);
2312 nv_fb->r_format |= kind << 16;
2313 } else
Ben Skeggs0ad72862014-08-10 04:10:22 +10002314 if (disp->disp->oclass < NVD0_DISP_CLASS) {
Ben Skeggs8a423642014-08-10 04:10:19 +10002315 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2316 (fb->pitches[0] | 0x00100000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002317 } else {
Ben Skeggs8a423642014-08-10 04:10:19 +10002318 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2319 (fb->pitches[0] | 0x01000000);
Ben Skeggsab0af552014-08-10 04:10:19 +10002320 }
Ben Skeggs8a423642014-08-10 04:10:19 +10002321 nv_fb->r_handle = 0xffff0000 | kind;
Ben Skeggsab0af552014-08-10 04:10:19 +10002322
Ben Skeggs8a423642014-08-10 04:10:19 +10002323 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0, pfb->ram->size, kind);
Ben Skeggsab0af552014-08-10 04:10:19 +10002324}
2325
2326/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002327 * Init
2328 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002329
Ben Skeggs2a44e492011-11-09 11:36:33 +10002330void
Ben Skeggse225f442012-11-21 14:40:21 +10002331nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002332{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002333}
2334
2335int
Ben Skeggse225f442012-11-21 14:40:21 +10002336nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002337{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002338 struct nv50_disp *disp = nv50_disp(dev);
2339 struct drm_crtc *crtc;
2340 u32 *push;
2341
2342 push = evo_wait(nv50_mast(dev), 32);
2343 if (!push)
2344 return -EBUSY;
2345
2346 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2347 struct nv50_sync *sync = nv50_sync(crtc);
2348 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002349 }
2350
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002351 evo_mthd(push, 0x0088, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +10002352 evo_data(push, nv50_mast(dev)->base.sync.handle);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002353 evo_kick(push, nv50_mast(dev));
2354 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002355}
2356
2357void
Ben Skeggse225f442012-11-21 14:40:21 +10002358nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002359{
Ben Skeggse225f442012-11-21 14:40:21 +10002360 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs8a423642014-08-10 04:10:19 +10002361 struct nv50_fbdma *fbdma, *fbtmp;
2362
2363 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002364 nv50_fbdma_fini(fbdma);
Ben Skeggs8a423642014-08-10 04:10:19 +10002365 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002366
Ben Skeggs0ad72862014-08-10 04:10:22 +10002367 nv50_dmac_destroy(&disp->mast.base, disp->disp);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002368
Ben Skeggs816af2f2011-11-16 15:48:48 +10002369 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002370 if (disp->sync)
2371 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002372 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002373
Ben Skeggs77145f12012-07-31 16:16:21 +10002374 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002375 kfree(disp);
2376}
2377
2378int
Ben Skeggse225f442012-11-21 14:40:21 +10002379nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002380{
Ben Skeggs967e7bd2014-08-10 04:10:22 +10002381 struct nvif_device *device = &nouveau_drm(dev)->device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002382 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002383 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002384 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002385 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002386 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002387 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002388
2389 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2390 if (!disp)
2391 return -ENOMEM;
Ben Skeggs8a423642014-08-10 04:10:19 +10002392 INIT_LIST_HEAD(&disp->fbdma);
Ben Skeggs77145f12012-07-31 16:16:21 +10002393
2394 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002395 nouveau_display(dev)->dtor = nv50_display_destroy;
2396 nouveau_display(dev)->init = nv50_display_init;
2397 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggsab0af552014-08-10 04:10:19 +10002398 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2399 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002400 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002401
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002402 /* small shared memory area we use for notifiers and semaphores */
2403 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2404 0, 0x0000, NULL, &disp->sync);
2405 if (!ret) {
2406 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002407 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002408 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002409 if (ret)
2410 nouveau_bo_unpin(disp->sync);
2411 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002412 if (ret)
2413 nouveau_bo_ref(NULL, &disp->sync);
2414 }
2415
2416 if (ret)
2417 goto out;
2418
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002419 /* allocate master evo channel */
Ben Skeggs410f3ec2014-08-10 04:10:25 +10002420 ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
2421 &disp->mast);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002422 if (ret)
2423 goto out;
2424
Ben Skeggs438d99e2011-07-05 16:48:06 +10002425 /* create crtc objects to represent the hw heads */
Ben Skeggs0ad72862014-08-10 04:10:22 +10002426 if (disp->disp->oclass >= NVD0_DISP_CLASS)
Ben Skeggsdb2bec12014-08-10 04:10:22 +10002427 crtcs = nvif_rd32(device, 0x022448);
Ben Skeggs63718a02012-11-16 11:44:14 +10002428 else
2429 crtcs = 2;
2430
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002431 for (i = 0; i < crtcs; i++) {
Ben Skeggs0ad72862014-08-10 04:10:22 +10002432 ret = nv50_crtc_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002433 if (ret)
2434 goto out;
2435 }
2436
Ben Skeggs83fc0832011-07-05 13:08:40 +10002437 /* create encoder/connector objects based on VBIOS DCB table */
2438 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2439 connector = nouveau_connector_create(dev, dcbe->connector);
2440 if (IS_ERR(connector))
2441 continue;
2442
Ben Skeggseb6313a2013-02-11 09:52:58 +10002443 if (dcbe->location == DCB_LOC_ON_CHIP) {
2444 switch (dcbe->type) {
2445 case DCB_OUTPUT_TMDS:
2446 case DCB_OUTPUT_LVDS:
2447 case DCB_OUTPUT_DP:
2448 ret = nv50_sor_create(connector, dcbe);
2449 break;
2450 case DCB_OUTPUT_ANALOG:
2451 ret = nv50_dac_create(connector, dcbe);
2452 break;
2453 default:
2454 ret = -ENODEV;
2455 break;
2456 }
2457 } else {
2458 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002459 }
2460
Ben Skeggseb6313a2013-02-11 09:52:58 +10002461 if (ret) {
2462 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2463 dcbe->location, dcbe->type,
2464 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002465 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002466 }
2467 }
2468
2469 /* cull any connectors we created that don't have an encoder */
2470 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2471 if (connector->encoder_ids[0])
2472 continue;
2473
Ben Skeggs77145f12012-07-31 16:16:21 +10002474 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002475 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002476 connector->funcs->destroy(connector);
2477 }
2478
Ben Skeggs26f6d882011-07-04 16:25:18 +10002479out:
2480 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002481 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002482 return ret;
2483}