blob: f8e66c08b11a292545a29e327966d3d4256e5f09 [file] [log] [blame]
Ben Skeggse225f442012-11-21 14:40:21 +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 Skeggs26f6d882011-07-04 16:25:18 +100029
Ben Skeggs77145f12012-07-31 16:16:21 +100030#include "nouveau_drm.h"
31#include "nouveau_dma.h"
32#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100033#include "nouveau_connector.h"
34#include "nouveau_encoder.h"
35#include "nouveau_crtc.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100036#include "nouveau_fence.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100037#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100038
Ben Skeggsb5a794b2012-10-16 14:18:32 +100039#include <core/client.h>
Ben Skeggs77145f12012-07-31 16:16:21 +100040#include <core/gpuobj.h>
Ben Skeggsb5a794b2012-10-16 14:18:32 +100041#include <core/class.h>
Ben Skeggs77145f12012-07-31 16:16:21 +100042
43#include <subdev/timer.h>
44#include <subdev/bar.h>
45#include <subdev/fb.h>
Ben Skeggs5ed50202013-02-11 20:15:03 +100046#include <subdev/i2c.h>
Ben Skeggs77145f12012-07-31 16:16:21 +100047
Ben Skeggs8a464382011-11-12 23:52:07 +100048#define EVO_DMA_NR 9
49
Ben Skeggsbdb8c212011-11-12 01:30:24 +100050#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100051#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100052#define EVO_OVLY(c) (0x05 + (c))
53#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100054#define EVO_CURS(c) (0x0d + (c))
55
Ben Skeggs816af2f2011-11-16 15:48:48 +100056/* offsets in shared sync bo of various structures */
57#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +100058#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
59#define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
60#define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
Ben Skeggs816af2f2011-11-16 15:48:48 +100061
Ben Skeggsb5a794b2012-10-16 14:18:32 +100062#define EVO_CORE_HANDLE (0xd1500000)
63#define EVO_CHAN_HANDLE(t,i) (0xd15c0000 | (((t) & 0x00ff) << 8) | (i))
64#define EVO_CHAN_OCLASS(t,c) ((nv_hclass(c) & 0xff00) | ((t) & 0x00ff))
65#define EVO_PUSH_HANDLE(t,i) (0xd15b0000 | (i) | \
66 (((NV50_DISP_##t##_CLASS) & 0x00ff) << 8))
67
68/******************************************************************************
69 * EVO channel
70 *****************************************************************************/
71
Ben Skeggse225f442012-11-21 14:40:21 +100072struct nv50_chan {
Ben Skeggsb5a794b2012-10-16 14:18:32 +100073 struct nouveau_object *user;
74 u32 handle;
75};
76
77static int
Ben Skeggse225f442012-11-21 14:40:21 +100078nv50_chan_create(struct nouveau_object *core, u32 bclass, u8 head,
79 void *data, u32 size, struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100080{
81 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
82 const u32 oclass = EVO_CHAN_OCLASS(bclass, core);
83 const u32 handle = EVO_CHAN_HANDLE(bclass, head);
84 int ret;
85
86 ret = nouveau_object_new(client, EVO_CORE_HANDLE, handle,
87 oclass, data, size, &chan->user);
88 if (ret)
89 return ret;
90
91 chan->handle = handle;
92 return 0;
93}
94
95static void
Ben Skeggse225f442012-11-21 14:40:21 +100096nv50_chan_destroy(struct nouveau_object *core, struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100097{
98 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
99 if (chan->handle)
100 nouveau_object_del(client, EVO_CORE_HANDLE, chan->handle);
101}
102
103/******************************************************************************
104 * PIO EVO channel
105 *****************************************************************************/
106
Ben Skeggse225f442012-11-21 14:40:21 +1000107struct nv50_pioc {
108 struct nv50_chan base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000109};
110
111static void
Ben Skeggse225f442012-11-21 14:40:21 +1000112nv50_pioc_destroy(struct nouveau_object *core, struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000113{
Ben Skeggse225f442012-11-21 14:40:21 +1000114 nv50_chan_destroy(core, &pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000115}
116
117static int
Ben Skeggse225f442012-11-21 14:40:21 +1000118nv50_pioc_create(struct nouveau_object *core, u32 bclass, u8 head,
119 void *data, u32 size, struct nv50_pioc *pioc)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000120{
Ben Skeggse225f442012-11-21 14:40:21 +1000121 return nv50_chan_create(core, bclass, head, data, size, &pioc->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000122}
123
124/******************************************************************************
125 * DMA EVO channel
126 *****************************************************************************/
127
Ben Skeggse225f442012-11-21 14:40:21 +1000128struct nv50_dmac {
129 struct nv50_chan base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000130 dma_addr_t handle;
131 u32 *ptr;
Daniel Vetter59ad1462012-12-02 14:49:44 +0100132
133 /* Protects against concurrent pushbuf access to this channel, lock is
134 * grabbed by evo_wait (if the pushbuf reservation is successful) and
135 * dropped again by evo_kick. */
136 struct mutex lock;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000137};
138
139static void
Ben Skeggse225f442012-11-21 14:40:21 +1000140nv50_dmac_destroy(struct nouveau_object *core, struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000141{
142 if (dmac->ptr) {
143 struct pci_dev *pdev = nv_device(core)->pdev;
144 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
145 }
146
Ben Skeggse225f442012-11-21 14:40:21 +1000147 nv50_chan_destroy(core, &dmac->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000148}
149
150static int
Ben Skeggs47057302012-11-16 13:58:48 +1000151nv50_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
152{
153 struct nouveau_fb *pfb = nouveau_fb(core);
154 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
155 struct nouveau_object *object;
156 int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
157 NV_DMA_IN_MEMORY_CLASS,
158 &(struct nv_dma_class) {
159 .flags = NV_DMA_TARGET_VRAM |
160 NV_DMA_ACCESS_RDWR,
161 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000162 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000163 .conf0 = NV50_DMA_CONF0_ENABLE |
164 NV50_DMA_CONF0_PART_256,
165 }, sizeof(struct nv_dma_class), &object);
166 if (ret)
167 return ret;
168
169 ret = nouveau_object_new(client, parent, NvEvoFB16,
170 NV_DMA_IN_MEMORY_CLASS,
171 &(struct nv_dma_class) {
172 .flags = NV_DMA_TARGET_VRAM |
173 NV_DMA_ACCESS_RDWR,
174 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000175 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000176 .conf0 = NV50_DMA_CONF0_ENABLE | 0x70 |
177 NV50_DMA_CONF0_PART_256,
178 }, sizeof(struct nv_dma_class), &object);
179 if (ret)
180 return ret;
181
182 ret = nouveau_object_new(client, parent, NvEvoFB32,
183 NV_DMA_IN_MEMORY_CLASS,
184 &(struct nv_dma_class) {
185 .flags = NV_DMA_TARGET_VRAM |
186 NV_DMA_ACCESS_RDWR,
187 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000188 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000189 .conf0 = NV50_DMA_CONF0_ENABLE | 0x7a |
190 NV50_DMA_CONF0_PART_256,
191 }, sizeof(struct nv_dma_class), &object);
192 return ret;
193}
194
195static int
196nvc0_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
197{
198 struct nouveau_fb *pfb = nouveau_fb(core);
199 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
200 struct nouveau_object *object;
201 int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
202 NV_DMA_IN_MEMORY_CLASS,
203 &(struct nv_dma_class) {
204 .flags = NV_DMA_TARGET_VRAM |
205 NV_DMA_ACCESS_RDWR,
206 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000207 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000208 .conf0 = NVC0_DMA_CONF0_ENABLE,
209 }, sizeof(struct nv_dma_class), &object);
210 if (ret)
211 return ret;
212
213 ret = nouveau_object_new(client, parent, NvEvoFB16,
214 NV_DMA_IN_MEMORY_CLASS,
215 &(struct nv_dma_class) {
216 .flags = NV_DMA_TARGET_VRAM |
217 NV_DMA_ACCESS_RDWR,
218 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000219 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000220 .conf0 = NVC0_DMA_CONF0_ENABLE | 0xfe,
221 }, sizeof(struct nv_dma_class), &object);
222 if (ret)
223 return ret;
224
225 ret = nouveau_object_new(client, parent, NvEvoFB32,
226 NV_DMA_IN_MEMORY_CLASS,
227 &(struct nv_dma_class) {
228 .flags = NV_DMA_TARGET_VRAM |
229 NV_DMA_ACCESS_RDWR,
230 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000231 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000232 .conf0 = NVC0_DMA_CONF0_ENABLE | 0xfe,
233 }, sizeof(struct nv_dma_class), &object);
234 return ret;
235}
236
237static int
238nvd0_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
239{
240 struct nouveau_fb *pfb = nouveau_fb(core);
241 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
242 struct nouveau_object *object;
243 int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
244 NV_DMA_IN_MEMORY_CLASS,
245 &(struct nv_dma_class) {
246 .flags = NV_DMA_TARGET_VRAM |
247 NV_DMA_ACCESS_RDWR,
248 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000249 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000250 .conf0 = NVD0_DMA_CONF0_ENABLE |
251 NVD0_DMA_CONF0_PAGE_LP,
252 }, sizeof(struct nv_dma_class), &object);
253 if (ret)
254 return ret;
255
256 ret = nouveau_object_new(client, parent, NvEvoFB32,
257 NV_DMA_IN_MEMORY_CLASS,
258 &(struct nv_dma_class) {
259 .flags = NV_DMA_TARGET_VRAM |
260 NV_DMA_ACCESS_RDWR,
261 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000262 .limit = pfb->ram->size - 1,
Ben Skeggs47057302012-11-16 13:58:48 +1000263 .conf0 = NVD0_DMA_CONF0_ENABLE | 0xfe |
264 NVD0_DMA_CONF0_PAGE_LP,
265 }, sizeof(struct nv_dma_class), &object);
266 return ret;
267}
268
269static int
Ben Skeggse225f442012-11-21 14:40:21 +1000270nv50_dmac_create(struct nouveau_object *core, u32 bclass, u8 head,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000271 void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000272 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000273{
274 struct nouveau_fb *pfb = nouveau_fb(core);
275 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
276 struct nouveau_object *object;
277 u32 pushbuf = *(u32 *)data;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000278 int ret;
279
Daniel Vetter59ad1462012-12-02 14:49:44 +0100280 mutex_init(&dmac->lock);
281
Ben Skeggs47057302012-11-16 13:58:48 +1000282 dmac->ptr = pci_alloc_consistent(nv_device(core)->pdev, PAGE_SIZE,
283 &dmac->handle);
284 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000285 return -ENOMEM;
286
287 ret = nouveau_object_new(client, NVDRM_DEVICE, pushbuf,
288 NV_DMA_FROM_MEMORY_CLASS,
289 &(struct nv_dma_class) {
290 .flags = NV_DMA_TARGET_PCI_US |
291 NV_DMA_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000292 .start = dmac->handle + 0x0000,
293 .limit = dmac->handle + 0x0fff,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000294 }, sizeof(struct nv_dma_class), &object);
295 if (ret)
296 return ret;
297
Ben Skeggse225f442012-11-21 14:40:21 +1000298 ret = nv50_chan_create(core, bclass, head, data, size, &dmac->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000299 if (ret)
300 return ret;
301
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000302 ret = nouveau_object_new(client, dmac->base.handle, NvEvoSync,
303 NV_DMA_IN_MEMORY_CLASS,
304 &(struct nv_dma_class) {
305 .flags = NV_DMA_TARGET_VRAM |
306 NV_DMA_ACCESS_RDWR,
307 .start = syncbuf + 0x0000,
308 .limit = syncbuf + 0x0fff,
309 }, sizeof(struct nv_dma_class), &object);
310 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000311 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000312
313 ret = nouveau_object_new(client, dmac->base.handle, NvEvoVRAM,
314 NV_DMA_IN_MEMORY_CLASS,
315 &(struct nv_dma_class) {
316 .flags = NV_DMA_TARGET_VRAM |
317 NV_DMA_ACCESS_RDWR,
318 .start = 0,
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000319 .limit = pfb->ram->size - 1,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000320 }, sizeof(struct nv_dma_class), &object);
321 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000322 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000323
Ben Skeggs47057302012-11-16 13:58:48 +1000324 if (nv_device(core)->card_type < NV_C0)
325 ret = nv50_dmac_create_fbdma(core, dmac->base.handle);
326 else
327 if (nv_device(core)->card_type < NV_D0)
328 ret = nvc0_dmac_create_fbdma(core, dmac->base.handle);
329 else
330 ret = nvd0_dmac_create_fbdma(core, dmac->base.handle);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000331 return ret;
332}
333
Ben Skeggse225f442012-11-21 14:40:21 +1000334struct nv50_mast {
335 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000336};
337
Ben Skeggse225f442012-11-21 14:40:21 +1000338struct nv50_curs {
339 struct nv50_pioc base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000340};
341
Ben Skeggse225f442012-11-21 14:40:21 +1000342struct nv50_sync {
343 struct nv50_dmac base;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000344 u32 addr;
345 u32 data;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000346};
347
Ben Skeggse225f442012-11-21 14:40:21 +1000348struct nv50_ovly {
349 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000350};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000351
Ben Skeggse225f442012-11-21 14:40:21 +1000352struct nv50_oimm {
353 struct nv50_pioc base;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000354};
355
Ben Skeggse225f442012-11-21 14:40:21 +1000356struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000357 struct nouveau_crtc base;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000358 struct nouveau_bo *image;
Ben Skeggse225f442012-11-21 14:40:21 +1000359 struct nv50_curs curs;
360 struct nv50_sync sync;
361 struct nv50_ovly ovly;
362 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000363};
364
Ben Skeggse225f442012-11-21 14:40:21 +1000365#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
366#define nv50_curs(c) (&nv50_head(c)->curs)
367#define nv50_sync(c) (&nv50_head(c)->sync)
368#define nv50_ovly(c) (&nv50_head(c)->ovly)
369#define nv50_oimm(c) (&nv50_head(c)->oimm)
370#define nv50_chan(c) (&(c)->base.base)
371#define nv50_vers(c) nv_mclass(nv50_chan(c)->user)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000372
Ben Skeggse225f442012-11-21 14:40:21 +1000373struct nv50_disp {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000374 struct nouveau_object *core;
Ben Skeggse225f442012-11-21 14:40:21 +1000375 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000376
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000377 u32 modeset;
378
379 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000380};
381
Ben Skeggse225f442012-11-21 14:40:21 +1000382static struct nv50_disp *
383nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000384{
Ben Skeggs77145f12012-07-31 16:16:21 +1000385 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000386}
387
Ben Skeggse225f442012-11-21 14:40:21 +1000388#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000389
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000390static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000391nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000392{
393 return nouveau_encoder(encoder)->crtc;
394}
395
396/******************************************************************************
397 * EVO channel helpers
398 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000399static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000400evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000401{
Ben Skeggse225f442012-11-21 14:40:21 +1000402 struct nv50_dmac *dmac = evoc;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000403 u32 put = nv_ro32(dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000404
Daniel Vetter59ad1462012-12-02 14:49:44 +0100405 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000406 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000407 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000408
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000409 nv_wo32(dmac->base.user, 0x0000, 0x00000000);
410 if (!nv_wait(dmac->base.user, 0x0004, ~0, 0x00000000)) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100411 mutex_unlock(&dmac->lock);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000412 NV_ERROR(dmac->base.user, "channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000413 return NULL;
414 }
415
416 put = 0;
417 }
418
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000419 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000420}
421
422static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000423evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000424{
Ben Skeggse225f442012-11-21 14:40:21 +1000425 struct nv50_dmac *dmac = evoc;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000426 nv_wo32(dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100427 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000428}
429
430#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
431#define evo_data(p,d) *((p)++) = (d)
432
Ben Skeggs3376ee32011-11-12 14:28:12 +1000433static bool
434evo_sync_wait(void *data)
435{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500436 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
437 return true;
438 usleep_range(1, 2);
439 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000440}
441
442static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000443evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000444{
Ben Skeggs77145f12012-07-31 16:16:21 +1000445 struct nouveau_device *device = nouveau_dev(dev);
Ben Skeggse225f442012-11-21 14:40:21 +1000446 struct nv50_disp *disp = nv50_disp(dev);
447 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000448 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000449 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000450 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000451 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000452 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000453 evo_mthd(push, 0x0080, 2);
454 evo_data(push, 0x00000000);
455 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000456 evo_kick(push, mast);
Ben Skeggs77145f12012-07-31 16:16:21 +1000457 if (nv_wait_cb(device, evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000458 return 0;
459 }
460
461 return -EBUSY;
462}
463
464/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000465 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000466 *****************************************************************************/
467struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000468nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000469{
Ben Skeggse225f442012-11-21 14:40:21 +1000470 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000471}
472
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000473struct nv50_display_flip {
474 struct nv50_disp *disp;
475 struct nv50_sync *chan;
476};
477
478static bool
479nv50_display_flip_wait(void *data)
480{
481 struct nv50_display_flip *flip = data;
482 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
Calvin Owensb1ea3e62013-04-07 21:01:19 -0500483 flip->chan->data)
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000484 return true;
485 usleep_range(1, 2);
486 return false;
487}
488
Ben Skeggs3376ee32011-11-12 14:28:12 +1000489void
Ben Skeggse225f442012-11-21 14:40:21 +1000490nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000491{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000492 struct nouveau_device *device = nouveau_dev(crtc->dev);
493 struct nv50_display_flip flip = {
494 .disp = nv50_disp(crtc->dev),
495 .chan = nv50_sync(crtc),
496 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000497 u32 *push;
498
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000499 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000500 if (push) {
501 evo_mthd(push, 0x0084, 1);
502 evo_data(push, 0x00000000);
503 evo_mthd(push, 0x0094, 1);
504 evo_data(push, 0x00000000);
505 evo_mthd(push, 0x00c0, 1);
506 evo_data(push, 0x00000000);
507 evo_mthd(push, 0x0080, 1);
508 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000509 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000510 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000511
512 nv_wait_cb(device, nv50_display_flip_wait, &flip);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000513}
514
515int
Ben Skeggse225f442012-11-21 14:40:21 +1000516nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000517 struct nouveau_channel *chan, u32 swap_interval)
518{
519 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000520 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000521 struct nv50_head *head = nv50_head(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000522 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000523 u32 *push;
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000524 int ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000525
526 swap_interval <<= 4;
527 if (swap_interval == 0)
528 swap_interval |= 0x100;
Ben Skeggsf60b6e72013-03-19 15:20:00 +1000529 if (chan == NULL)
530 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000531
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000532 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000533 if (unlikely(push == NULL))
534 return -EBUSY;
535
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000536 if (chan && nv_mclass(chan->object) < NV84_CHANNEL_IND_CLASS) {
537 ret = RING_SPACE(chan, 8);
538 if (ret)
539 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000540
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000541 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000542 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000543 OUT_RING (chan, sync->addr ^ 0x10);
544 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
545 OUT_RING (chan, sync->data + 1);
546 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
547 OUT_RING (chan, sync->addr);
548 OUT_RING (chan, sync->data);
549 } else
550 if (chan && nv_mclass(chan->object) < NVC0_CHANNEL_IND_CLASS) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000551 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000552 ret = RING_SPACE(chan, 12);
553 if (ret)
554 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000555
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000556 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
557 OUT_RING (chan, chan->vram);
558 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
559 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
560 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
561 OUT_RING (chan, sync->data + 1);
562 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
563 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
564 OUT_RING (chan, upper_32_bits(addr));
565 OUT_RING (chan, lower_32_bits(addr));
566 OUT_RING (chan, sync->data);
567 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
568 } else
569 if (chan) {
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000570 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000571 ret = RING_SPACE(chan, 10);
572 if (ret)
573 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000574
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000575 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
576 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
577 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
578 OUT_RING (chan, sync->data + 1);
579 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
580 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
581 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
582 OUT_RING (chan, upper_32_bits(addr));
583 OUT_RING (chan, lower_32_bits(addr));
584 OUT_RING (chan, sync->data);
585 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
586 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
587 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500588
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000589 if (chan) {
590 sync->addr ^= 0x10;
591 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000592 FIRE_RING (chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000593 }
594
595 /* queue the flip */
596 evo_mthd(push, 0x0100, 1);
597 evo_data(push, 0xfffe0000);
598 evo_mthd(push, 0x0084, 1);
599 evo_data(push, swap_interval);
600 if (!(swap_interval & 0x00000100)) {
601 evo_mthd(push, 0x00e0, 1);
602 evo_data(push, 0x40000000);
603 }
604 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000605 evo_data(push, sync->addr);
606 evo_data(push, sync->data++);
607 evo_data(push, sync->data);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000608 evo_data(push, NvEvoSync);
609 evo_mthd(push, 0x00a0, 2);
610 evo_data(push, 0x00000000);
611 evo_data(push, 0x00000000);
612 evo_mthd(push, 0x00c0, 1);
613 evo_data(push, nv_fb->r_dma);
614 evo_mthd(push, 0x0110, 2);
615 evo_data(push, 0x00000000);
616 evo_data(push, 0x00000000);
Ben Skeggse225f442012-11-21 14:40:21 +1000617 if (nv50_vers(sync) < NVD0_DISP_SYNC_CLASS) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000618 evo_mthd(push, 0x0800, 5);
619 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
620 evo_data(push, 0);
621 evo_data(push, (fb->height << 16) | fb->width);
622 evo_data(push, nv_fb->r_pitch);
623 evo_data(push, nv_fb->r_format);
624 } else {
625 evo_mthd(push, 0x0400, 5);
626 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
627 evo_data(push, 0);
628 evo_data(push, (fb->height << 16) | fb->width);
629 evo_data(push, nv_fb->r_pitch);
630 evo_data(push, nv_fb->r_format);
631 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000632 evo_mthd(push, 0x0080, 1);
633 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000634 evo_kick(push, sync);
Ben Skeggs8dda53f2013-07-09 12:35:55 +1000635
636 nouveau_bo_ref(nv_fb->nvbo, &head->image);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000637 return 0;
638}
639
Ben Skeggs26f6d882011-07-04 16:25:18 +1000640/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000641 * CRTC
642 *****************************************************************************/
643static int
Ben Skeggse225f442012-11-21 14:40:21 +1000644nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000645{
Ben Skeggse225f442012-11-21 14:40:21 +1000646 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000647 struct nouveau_connector *nv_connector;
648 struct drm_connector *connector;
649 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000650
Ben Skeggs488ff202011-10-17 10:38:10 +1000651 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000652 connector = &nv_connector->base;
653 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
654 if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
655 mode = DITHERING_MODE_DYNAMIC2X2;
656 } else {
657 mode = nv_connector->dithering_mode;
658 }
659
660 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
661 if (connector->display_info.bpc >= 8)
662 mode |= DITHERING_DEPTH_8BPC;
663 } else {
664 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000665 }
666
Ben Skeggsde8268c2012-11-16 10:24:31 +1000667 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000668 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000669 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000670 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
671 evo_data(push, mode);
672 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000673 if (nv50_vers(mast) < NVE0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000674 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
675 evo_data(push, mode);
676 } else {
677 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
678 evo_data(push, mode);
679 }
680
Ben Skeggs438d99e2011-07-05 16:48:06 +1000681 if (update) {
682 evo_mthd(push, 0x0080, 1);
683 evo_data(push, 0x00000000);
684 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000685 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000686 }
687
688 return 0;
689}
690
691static int
Ben Skeggse225f442012-11-21 14:40:21 +1000692nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000693{
Ben Skeggse225f442012-11-21 14:40:21 +1000694 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000695 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000696 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000697 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000698 int mode = DRM_MODE_SCALE_NONE;
699 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000700
Ben Skeggs92854622011-11-11 23:49:06 +1000701 /* start off at the resolution we programmed the crtc for, this
702 * effectively handles NONE/FULL scaling
703 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000704 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000705 if (nv_connector && nv_connector->native_mode)
706 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000707
Ben Skeggs92854622011-11-11 23:49:06 +1000708 if (mode != DRM_MODE_SCALE_NONE)
709 omode = nv_connector->native_mode;
710 else
711 omode = umode;
712
713 oX = omode->hdisplay;
714 oY = omode->vdisplay;
715 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
716 oY *= 2;
717
718 /* add overscan compensation if necessary, will keep the aspect
719 * ratio the same as the backend mode unless overridden by the
720 * user setting both hborder and vborder properties.
721 */
722 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
723 (nv_connector->underscan == UNDERSCAN_AUTO &&
724 nv_connector->edid &&
725 drm_detect_hdmi_monitor(nv_connector->edid)))) {
726 u32 bX = nv_connector->underscan_hborder;
727 u32 bY = nv_connector->underscan_vborder;
728 u32 aspect = (oY << 19) / oX;
729
730 if (bX) {
731 oX -= (bX * 2);
732 if (bY) oY -= (bY * 2);
733 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
734 } else {
735 oX -= (oX >> 4) + 32;
736 if (bY) oY -= (bY * 2);
737 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000738 }
739 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000740
Ben Skeggs92854622011-11-11 23:49:06 +1000741 /* handle CENTER/ASPECT scaling, taking into account the areas
742 * removed already for overscan compensation
743 */
744 switch (mode) {
745 case DRM_MODE_SCALE_CENTER:
746 oX = min((u32)umode->hdisplay, oX);
747 oY = min((u32)umode->vdisplay, oY);
748 /* fall-through */
749 case DRM_MODE_SCALE_ASPECT:
750 if (oY < oX) {
751 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
752 oX = ((oY * aspect) + (aspect / 2)) >> 19;
753 } else {
754 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
755 oY = ((oX * aspect) + (aspect / 2)) >> 19;
756 }
757 break;
758 default:
759 break;
760 }
761
Ben Skeggsde8268c2012-11-16 10:24:31 +1000762 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000763 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000764 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000765 /*XXX: SCALE_CTRL_ACTIVE??? */
766 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
767 evo_data(push, (oY << 16) | oX);
768 evo_data(push, (oY << 16) | oX);
769 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
770 evo_data(push, 0x00000000);
771 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
772 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
773 } else {
774 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
775 evo_data(push, (oY << 16) | oX);
776 evo_data(push, (oY << 16) | oX);
777 evo_data(push, (oY << 16) | oX);
778 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
779 evo_data(push, 0x00000000);
780 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
781 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
782 }
783
784 evo_kick(push, mast);
785
Ben Skeggs3376ee32011-11-12 14:28:12 +1000786 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000787 nv50_display_flip_stop(crtc);
788 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000789 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000790 }
791
792 return 0;
793}
794
795static int
Ben Skeggse225f442012-11-21 14:40:21 +1000796nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000797{
Ben Skeggse225f442012-11-21 14:40:21 +1000798 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000799 u32 *push, hue, vib;
800 int adj;
801
802 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
803 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
804 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
805
806 push = evo_wait(mast, 16);
807 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000808 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000809 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
810 evo_data(push, (hue << 20) | (vib << 8));
811 } else {
812 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
813 evo_data(push, (hue << 20) | (vib << 8));
814 }
815
816 if (update) {
817 evo_mthd(push, 0x0080, 1);
818 evo_data(push, 0x00000000);
819 }
820 evo_kick(push, mast);
821 }
822
823 return 0;
824}
825
826static int
Ben Skeggse225f442012-11-21 14:40:21 +1000827nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000828 int x, int y, bool update)
829{
830 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000831 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000832 u32 *push;
833
Ben Skeggsde8268c2012-11-16 10:24:31 +1000834 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000835 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000836 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000837 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
838 evo_data(push, nvfb->nvbo->bo.offset >> 8);
839 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
840 evo_data(push, (fb->height << 16) | fb->width);
841 evo_data(push, nvfb->r_pitch);
842 evo_data(push, nvfb->r_format);
843 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
844 evo_data(push, (y << 16) | x);
Ben Skeggse225f442012-11-21 14:40:21 +1000845 if (nv50_vers(mast) > NV50_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000846 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
847 evo_data(push, nvfb->r_dma);
848 }
849 } else {
850 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
851 evo_data(push, nvfb->nvbo->bo.offset >> 8);
852 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
853 evo_data(push, (fb->height << 16) | fb->width);
854 evo_data(push, nvfb->r_pitch);
855 evo_data(push, nvfb->r_format);
856 evo_data(push, nvfb->r_dma);
857 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
858 evo_data(push, (y << 16) | x);
859 }
860
Ben Skeggsa46232e2011-07-07 15:23:48 +1000861 if (update) {
862 evo_mthd(push, 0x0080, 1);
863 evo_data(push, 0x00000000);
864 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000865 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000866 }
867
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000868 nv_crtc->fb.tile_flags = nvfb->r_dma;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000869 return 0;
870}
871
872static void
Ben Skeggse225f442012-11-21 14:40:21 +1000873nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000874{
Ben Skeggse225f442012-11-21 14:40:21 +1000875 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000876 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000877 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000878 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000879 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
880 evo_data(push, 0x85000000);
881 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
882 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000883 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000884 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
885 evo_data(push, 0x85000000);
886 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
887 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
888 evo_data(push, NvEvoVRAM);
889 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000890 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
891 evo_data(push, 0x85000000);
892 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
893 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000894 evo_data(push, NvEvoVRAM);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000895 }
896 evo_kick(push, mast);
897 }
898}
899
900static void
Ben Skeggse225f442012-11-21 14:40:21 +1000901nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000902{
Ben Skeggse225f442012-11-21 14:40:21 +1000903 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000904 u32 *push = evo_wait(mast, 16);
905 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000906 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000907 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
908 evo_data(push, 0x05000000);
909 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000910 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000911 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
912 evo_data(push, 0x05000000);
913 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
914 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000915 } else {
916 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
917 evo_data(push, 0x05000000);
918 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
919 evo_data(push, 0x00000000);
920 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000921 evo_kick(push, mast);
922 }
923}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000924
Ben Skeggsde8268c2012-11-16 10:24:31 +1000925static void
Ben Skeggse225f442012-11-21 14:40:21 +1000926nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000927{
Ben Skeggse225f442012-11-21 14:40:21 +1000928 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000929
930 if (show)
Ben Skeggse225f442012-11-21 14:40:21 +1000931 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000932 else
Ben Skeggse225f442012-11-21 14:40:21 +1000933 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000934
935 if (update) {
936 u32 *push = evo_wait(mast, 2);
937 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000938 evo_mthd(push, 0x0080, 1);
939 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000940 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000941 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000942 }
943}
944
945static void
Ben Skeggse225f442012-11-21 14:40:21 +1000946nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000947{
948}
949
950static void
Ben Skeggse225f442012-11-21 14:40:21 +1000951nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000952{
953 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000954 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000955 u32 *push;
956
Ben Skeggse225f442012-11-21 14:40:21 +1000957 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000958
Ben Skeggsde8268c2012-11-16 10:24:31 +1000959 push = evo_wait(mast, 2);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000960 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000961 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000962 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
963 evo_data(push, 0x00000000);
964 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
965 evo_data(push, 0x40000000);
966 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000967 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000968 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
969 evo_data(push, 0x00000000);
970 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
971 evo_data(push, 0x40000000);
972 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
973 evo_data(push, 0x00000000);
974 } else {
975 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
976 evo_data(push, 0x00000000);
977 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
978 evo_data(push, 0x03000000);
979 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
980 evo_data(push, 0x00000000);
981 }
982
983 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000984 }
985
Ben Skeggse225f442012-11-21 14:40:21 +1000986 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000987}
988
989static void
Ben Skeggse225f442012-11-21 14:40:21 +1000990nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000991{
992 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000993 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000994 u32 *push;
995
Ben Skeggsde8268c2012-11-16 10:24:31 +1000996 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000997 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000998 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000999 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1000 evo_data(push, NvEvoVRAM_LP);
1001 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1002 evo_data(push, 0xc0000000);
1003 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1004 } else
Ben Skeggse225f442012-11-21 14:40:21 +10001005 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001006 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1007 evo_data(push, nv_crtc->fb.tile_flags);
1008 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1009 evo_data(push, 0xc0000000);
1010 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1011 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
1012 evo_data(push, NvEvoVRAM);
1013 } else {
1014 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
1015 evo_data(push, nv_crtc->fb.tile_flags);
1016 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1017 evo_data(push, 0x83000000);
1018 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1019 evo_data(push, 0x00000000);
1020 evo_data(push, 0x00000000);
1021 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
1022 evo_data(push, NvEvoVRAM);
1023 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1024 evo_data(push, 0xffffff00);
1025 }
1026
1027 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001028 }
1029
Ben Skeggse225f442012-11-21 14:40:21 +10001030 nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
1031 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001032}
1033
1034static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001035nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001036 struct drm_display_mode *adjusted_mode)
1037{
1038 return true;
1039}
1040
1041static int
Ben Skeggse225f442012-11-21 14:40:21 +10001042nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001043{
1044 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001045 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001046 int ret;
1047
1048 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001049 if (ret == 0) {
1050 if (head->image)
1051 nouveau_bo_unpin(head->image);
1052 nouveau_bo_ref(nvfb->nvbo, &head->image);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001053 }
1054
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001055 return ret;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001056}
1057
1058static int
Ben Skeggse225f442012-11-21 14:40:21 +10001059nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001060 struct drm_display_mode *mode, int x, int y,
1061 struct drm_framebuffer *old_fb)
1062{
Ben Skeggse225f442012-11-21 14:40:21 +10001063 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001064 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1065 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001066 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1067 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1068 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1069 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
1070 u32 vblan2e = 0, vblan2s = 1;
Ben Skeggs3488c572012-03-12 11:42:20 +10001071 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001072 int ret;
1073
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001074 hactive = mode->htotal;
1075 hsynce = mode->hsync_end - mode->hsync_start - 1;
1076 hbackp = mode->htotal - mode->hsync_end;
1077 hblanke = hsynce + hbackp;
1078 hfrontp = mode->hsync_start - mode->hdisplay;
1079 hblanks = mode->htotal - hfrontp - 1;
1080
1081 vactive = mode->vtotal * vscan / ilace;
1082 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1083 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1084 vblanke = vsynce + vbackp;
1085 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1086 vblanks = vactive - vfrontp - 1;
1087 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1088 vblan2e = vactive + vsynce + vbackp;
1089 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1090 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001091 }
1092
Ben Skeggse225f442012-11-21 14:40:21 +10001093 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001094 if (ret)
1095 return ret;
1096
Ben Skeggsde8268c2012-11-16 10:24:31 +10001097 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001098 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001099 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001100 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1101 evo_data(push, 0x00800000 | mode->clock);
1102 evo_data(push, (ilace == 2) ? 2 : 0);
1103 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
1104 evo_data(push, 0x00000000);
1105 evo_data(push, (vactive << 16) | hactive);
1106 evo_data(push, ( vsynce << 16) | hsynce);
1107 evo_data(push, (vblanke << 16) | hblanke);
1108 evo_data(push, (vblanks << 16) | hblanks);
1109 evo_data(push, (vblan2e << 16) | vblan2s);
1110 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
1111 evo_data(push, 0x00000000);
1112 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1113 evo_data(push, 0x00000311);
1114 evo_data(push, 0x00000100);
1115 } else {
1116 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1117 evo_data(push, 0x00000000);
1118 evo_data(push, (vactive << 16) | hactive);
1119 evo_data(push, ( vsynce << 16) | hsynce);
1120 evo_data(push, (vblanke << 16) | hblanke);
1121 evo_data(push, (vblanks << 16) | hblanks);
1122 evo_data(push, (vblan2e << 16) | vblan2s);
1123 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1124 evo_data(push, 0x00000000); /* ??? */
1125 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1126 evo_data(push, mode->clock * 1000);
1127 evo_data(push, 0x00200000); /* ??? */
1128 evo_data(push, mode->clock * 1000);
1129 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1130 evo_data(push, 0x00000311);
1131 evo_data(push, 0x00000100);
1132 }
1133
1134 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001135 }
1136
1137 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001138 nv50_crtc_set_dither(nv_crtc, false);
1139 nv50_crtc_set_scale(nv_crtc, false);
1140 nv50_crtc_set_color_vibrance(nv_crtc, false);
1141 nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001142 return 0;
1143}
1144
1145static int
Ben Skeggse225f442012-11-21 14:40:21 +10001146nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001147 struct drm_framebuffer *old_fb)
1148{
Ben Skeggs77145f12012-07-31 16:16:21 +10001149 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001150 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1151 int ret;
1152
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001153 if (!crtc->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001154 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001155 return 0;
1156 }
1157
Ben Skeggse225f442012-11-21 14:40:21 +10001158 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001159 if (ret)
1160 return ret;
1161
Ben Skeggse225f442012-11-21 14:40:21 +10001162 nv50_display_flip_stop(crtc);
1163 nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
1164 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001165 return 0;
1166}
1167
1168static int
Ben Skeggse225f442012-11-21 14:40:21 +10001169nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001170 struct drm_framebuffer *fb, int x, int y,
1171 enum mode_set_atomic state)
1172{
1173 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001174 nv50_display_flip_stop(crtc);
1175 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001176 return 0;
1177}
1178
1179static void
Ben Skeggse225f442012-11-21 14:40:21 +10001180nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001181{
Ben Skeggse225f442012-11-21 14:40:21 +10001182 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001183 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1184 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1185 int i;
1186
1187 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001188 u16 r = nv_crtc->lut.r[i] >> 2;
1189 u16 g = nv_crtc->lut.g[i] >> 2;
1190 u16 b = nv_crtc->lut.b[i] >> 2;
1191
1192 if (nv_mclass(disp->core) < NVD0_DISP_CLASS) {
1193 writew(r + 0x0000, lut + (i * 0x08) + 0);
1194 writew(g + 0x0000, lut + (i * 0x08) + 2);
1195 writew(b + 0x0000, lut + (i * 0x08) + 4);
1196 } else {
1197 writew(r + 0x6000, lut + (i * 0x20) + 0);
1198 writew(g + 0x6000, lut + (i * 0x20) + 2);
1199 writew(b + 0x6000, lut + (i * 0x20) + 4);
1200 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001201 }
1202}
1203
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001204static void
1205nv50_crtc_disable(struct drm_crtc *crtc)
1206{
1207 struct nv50_head *head = nv50_head(crtc);
1208 if (head->image)
1209 nouveau_bo_unpin(head->image);
1210 nouveau_bo_ref(NULL, &head->image);
1211}
1212
Ben Skeggs438d99e2011-07-05 16:48:06 +10001213static int
Ben Skeggse225f442012-11-21 14:40:21 +10001214nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001215 uint32_t handle, uint32_t width, uint32_t height)
1216{
1217 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1218 struct drm_device *dev = crtc->dev;
1219 struct drm_gem_object *gem;
1220 struct nouveau_bo *nvbo;
1221 bool visible = (handle != 0);
1222 int i, ret = 0;
1223
1224 if (visible) {
1225 if (width != 64 || height != 64)
1226 return -EINVAL;
1227
1228 gem = drm_gem_object_lookup(dev, file_priv, handle);
1229 if (unlikely(!gem))
1230 return -ENOENT;
1231 nvbo = nouveau_gem_object(gem);
1232
1233 ret = nouveau_bo_map(nvbo);
1234 if (ret == 0) {
1235 for (i = 0; i < 64 * 64; i++) {
1236 u32 v = nouveau_bo_rd32(nvbo, i);
1237 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1238 }
1239 nouveau_bo_unmap(nvbo);
1240 }
1241
1242 drm_gem_object_unreference_unlocked(gem);
1243 }
1244
1245 if (visible != nv_crtc->cursor.visible) {
Ben Skeggse225f442012-11-21 14:40:21 +10001246 nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001247 nv_crtc->cursor.visible = visible;
1248 }
1249
1250 return ret;
1251}
1252
1253static int
Ben Skeggse225f442012-11-21 14:40:21 +10001254nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001255{
Ben Skeggse225f442012-11-21 14:40:21 +10001256 struct nv50_curs *curs = nv50_curs(crtc);
1257 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001258 nv_wo32(chan->user, 0x0084, (y << 16) | (x & 0xffff));
1259 nv_wo32(chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001260 return 0;
1261}
1262
1263static void
Ben Skeggse225f442012-11-21 14:40:21 +10001264nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001265 uint32_t start, uint32_t size)
1266{
1267 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1268 u32 end = max(start + size, (u32)256);
1269 u32 i;
1270
1271 for (i = start; i < end; i++) {
1272 nv_crtc->lut.r[i] = r[i];
1273 nv_crtc->lut.g[i] = g[i];
1274 nv_crtc->lut.b[i] = b[i];
1275 }
1276
Ben Skeggse225f442012-11-21 14:40:21 +10001277 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001278}
1279
1280static void
Ben Skeggse225f442012-11-21 14:40:21 +10001281nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001282{
1283 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001284 struct nv50_disp *disp = nv50_disp(crtc->dev);
1285 struct nv50_head *head = nv50_head(crtc);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001286
Ben Skeggse225f442012-11-21 14:40:21 +10001287 nv50_dmac_destroy(disp->core, &head->ovly.base);
1288 nv50_pioc_destroy(disp->core, &head->oimm.base);
1289 nv50_dmac_destroy(disp->core, &head->sync.base);
1290 nv50_pioc_destroy(disp->core, &head->curs.base);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001291
1292 /*XXX: this shouldn't be necessary, but the core doesn't call
1293 * disconnect() during the cleanup paths
1294 */
1295 if (head->image)
1296 nouveau_bo_unpin(head->image);
1297 nouveau_bo_ref(NULL, &head->image);
1298
Ben Skeggs438d99e2011-07-05 16:48:06 +10001299 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001300 if (nv_crtc->cursor.nvbo)
1301 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001302 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001303
Ben Skeggs438d99e2011-07-05 16:48:06 +10001304 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001305 if (nv_crtc->lut.nvbo)
1306 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001307 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001308
Ben Skeggs438d99e2011-07-05 16:48:06 +10001309 drm_crtc_cleanup(crtc);
1310 kfree(crtc);
1311}
1312
Ben Skeggse225f442012-11-21 14:40:21 +10001313static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1314 .dpms = nv50_crtc_dpms,
1315 .prepare = nv50_crtc_prepare,
1316 .commit = nv50_crtc_commit,
1317 .mode_fixup = nv50_crtc_mode_fixup,
1318 .mode_set = nv50_crtc_mode_set,
1319 .mode_set_base = nv50_crtc_mode_set_base,
1320 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1321 .load_lut = nv50_crtc_lut_load,
Ben Skeggs8dda53f2013-07-09 12:35:55 +10001322 .disable = nv50_crtc_disable,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001323};
1324
Ben Skeggse225f442012-11-21 14:40:21 +10001325static const struct drm_crtc_funcs nv50_crtc_func = {
1326 .cursor_set = nv50_crtc_cursor_set,
1327 .cursor_move = nv50_crtc_cursor_move,
1328 .gamma_set = nv50_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001329 .set_config = nouveau_crtc_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001330 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001331 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001332};
1333
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001334static void
Ben Skeggse225f442012-11-21 14:40:21 +10001335nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001336{
1337}
1338
1339static void
Ben Skeggse225f442012-11-21 14:40:21 +10001340nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001341{
1342}
1343
Ben Skeggs438d99e2011-07-05 16:48:06 +10001344static int
Ben Skeggse225f442012-11-21 14:40:21 +10001345nv50_crtc_create(struct drm_device *dev, struct nouveau_object *core, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001346{
Ben Skeggse225f442012-11-21 14:40:21 +10001347 struct nv50_disp *disp = nv50_disp(dev);
1348 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001349 struct drm_crtc *crtc;
1350 int ret, i;
1351
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001352 head = kzalloc(sizeof(*head), GFP_KERNEL);
1353 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001354 return -ENOMEM;
1355
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001356 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001357 head->base.set_dither = nv50_crtc_set_dither;
1358 head->base.set_scale = nv50_crtc_set_scale;
1359 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001360 head->base.color_vibrance = 50;
1361 head->base.vibrant_hue = 0;
Ben Skeggse225f442012-11-21 14:40:21 +10001362 head->base.cursor.set_offset = nv50_cursor_set_offset;
1363 head->base.cursor.set_pos = nv50_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001364 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001365 head->base.lut.r[i] = i << 8;
1366 head->base.lut.g[i] = i << 8;
1367 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001368 }
1369
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001370 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001371 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1372 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001373 drm_mode_crtc_set_gamma_size(crtc, 256);
1374
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001375 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001376 0, 0x0000, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001377 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001378 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001379 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001380 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001381 if (ret)
1382 nouveau_bo_unpin(head->base.lut.nvbo);
1383 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001384 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001385 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001386 }
1387
1388 if (ret)
1389 goto out;
1390
Ben Skeggse225f442012-11-21 14:40:21 +10001391 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001392
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001393 /* allocate cursor resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001394 ret = nv50_pioc_create(disp->core, NV50_DISP_CURS_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001395 &(struct nv50_display_curs_class) {
1396 .head = index,
1397 }, sizeof(struct nv50_display_curs_class),
1398 &head->curs.base);
1399 if (ret)
1400 goto out;
1401
1402 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
1403 0, 0x0000, NULL, &head->base.cursor.nvbo);
1404 if (!ret) {
1405 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001406 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001407 ret = nouveau_bo_map(head->base.cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001408 if (ret)
1409 nouveau_bo_unpin(head->base.lut.nvbo);
1410 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001411 if (ret)
1412 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
1413 }
1414
1415 if (ret)
1416 goto out;
1417
1418 /* allocate page flip / sync resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001419 ret = nv50_dmac_create(disp->core, NV50_DISP_SYNC_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001420 &(struct nv50_display_sync_class) {
1421 .pushbuf = EVO_PUSH_HANDLE(SYNC, index),
1422 .head = index,
1423 }, sizeof(struct nv50_display_sync_class),
1424 disp->sync->bo.offset, &head->sync.base);
1425 if (ret)
1426 goto out;
1427
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001428 head->sync.addr = EVO_FLIP_SEM0(index);
1429 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001430
1431 /* allocate overlay resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001432 ret = nv50_pioc_create(disp->core, NV50_DISP_OIMM_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001433 &(struct nv50_display_oimm_class) {
1434 .head = index,
1435 }, sizeof(struct nv50_display_oimm_class),
1436 &head->oimm.base);
1437 if (ret)
1438 goto out;
1439
Ben Skeggse225f442012-11-21 14:40:21 +10001440 ret = nv50_dmac_create(disp->core, NV50_DISP_OVLY_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001441 &(struct nv50_display_ovly_class) {
1442 .pushbuf = EVO_PUSH_HANDLE(OVLY, index),
1443 .head = index,
1444 }, sizeof(struct nv50_display_ovly_class),
1445 disp->sync->bo.offset, &head->ovly.base);
1446 if (ret)
1447 goto out;
1448
Ben Skeggs438d99e2011-07-05 16:48:06 +10001449out:
1450 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001451 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001452 return ret;
1453}
1454
1455/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001456 * DAC
1457 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001458static void
Ben Skeggse225f442012-11-21 14:40:21 +10001459nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001460{
1461 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001462 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001463 int or = nv_encoder->or;
1464 u32 dpms_ctrl;
1465
Ben Skeggs35b21d32012-11-08 12:08:55 +10001466 dpms_ctrl = 0x00000000;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001467 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
1468 dpms_ctrl |= 0x00000001;
1469 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
1470 dpms_ctrl |= 0x00000004;
1471
Ben Skeggs35b21d32012-11-08 12:08:55 +10001472 nv_call(disp->core, NV50_DISP_DAC_PWR + or, dpms_ctrl);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001473}
1474
1475static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001476nv50_dac_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001477 const struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001478 struct drm_display_mode *adjusted_mode)
1479{
1480 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1481 struct nouveau_connector *nv_connector;
1482
1483 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1484 if (nv_connector && nv_connector->native_mode) {
1485 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1486 int id = adjusted_mode->base.id;
1487 *adjusted_mode = *nv_connector->native_mode;
1488 adjusted_mode->base.id = id;
1489 }
1490 }
1491
1492 return true;
1493}
1494
1495static void
Ben Skeggse225f442012-11-21 14:40:21 +10001496nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001497{
1498}
1499
1500static void
Ben Skeggse225f442012-11-21 14:40:21 +10001501nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001502 struct drm_display_mode *adjusted_mode)
1503{
Ben Skeggse225f442012-11-21 14:40:21 +10001504 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001505 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1506 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001507 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001508
Ben Skeggse225f442012-11-21 14:40:21 +10001509 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001510
Ben Skeggs97b19b52012-11-16 11:21:37 +10001511 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001512 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001513 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001514 u32 syncs = 0x00000000;
1515
1516 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1517 syncs |= 0x00000001;
1518 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1519 syncs |= 0x00000002;
1520
1521 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1522 evo_data(push, 1 << nv_crtc->index);
1523 evo_data(push, syncs);
1524 } else {
1525 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1526 u32 syncs = 0x00000001;
1527
1528 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1529 syncs |= 0x00000008;
1530 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1531 syncs |= 0x00000010;
1532
1533 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1534 magic |= 0x00000001;
1535
1536 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1537 evo_data(push, syncs);
1538 evo_data(push, magic);
1539 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1540 evo_data(push, 1 << nv_crtc->index);
1541 }
1542
1543 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001544 }
1545
1546 nv_encoder->crtc = encoder->crtc;
1547}
1548
1549static void
Ben Skeggse225f442012-11-21 14:40:21 +10001550nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001551{
1552 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001553 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001554 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001555 u32 *push;
1556
1557 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001558 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001559
Ben Skeggs97b19b52012-11-16 11:21:37 +10001560 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001561 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001562 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001563 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1564 evo_data(push, 0x00000000);
1565 } else {
1566 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1567 evo_data(push, 0x00000000);
1568 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001569 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001570 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001571 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001572
1573 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001574}
1575
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001576static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001577nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001578{
Ben Skeggse225f442012-11-21 14:40:21 +10001579 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs35b21d32012-11-08 12:08:55 +10001580 int ret, or = nouveau_encoder(encoder)->or;
Ben Skeggsd40ee482013-06-03 16:40:14 +10001581 u32 load = nouveau_drm(encoder->dev)->vbios.dactestval;
1582 if (load == 0)
1583 load = 340;
Ben Skeggsb6819932011-07-08 11:14:50 +10001584
Ben Skeggs35b21d32012-11-08 12:08:55 +10001585 ret = nv_exec(disp->core, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
Ben Skeggs4b31ebc2013-09-04 11:01:42 +10001586 if (ret || !load)
Ben Skeggs35b21d32012-11-08 12:08:55 +10001587 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001588
Ben Skeggs35b21d32012-11-08 12:08:55 +10001589 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001590}
1591
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001592static void
Ben Skeggse225f442012-11-21 14:40:21 +10001593nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001594{
1595 drm_encoder_cleanup(encoder);
1596 kfree(encoder);
1597}
1598
Ben Skeggse225f442012-11-21 14:40:21 +10001599static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1600 .dpms = nv50_dac_dpms,
1601 .mode_fixup = nv50_dac_mode_fixup,
1602 .prepare = nv50_dac_disconnect,
1603 .commit = nv50_dac_commit,
1604 .mode_set = nv50_dac_mode_set,
1605 .disable = nv50_dac_disconnect,
1606 .get_crtc = nv50_display_crtc_get,
1607 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001608};
1609
Ben Skeggse225f442012-11-21 14:40:21 +10001610static const struct drm_encoder_funcs nv50_dac_func = {
1611 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001612};
1613
1614static int
Ben Skeggse225f442012-11-21 14:40:21 +10001615nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001616{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001617 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1618 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001619 struct nouveau_encoder *nv_encoder;
1620 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001621 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001622
1623 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1624 if (!nv_encoder)
1625 return -ENOMEM;
1626 nv_encoder->dcb = dcbe;
1627 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001628 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001629
1630 encoder = to_drm_encoder(nv_encoder);
1631 encoder->possible_crtcs = dcbe->heads;
1632 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001633 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001634 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001635
1636 drm_mode_connector_attach_encoder(connector, encoder);
1637 return 0;
1638}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001639
1640/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001641 * Audio
1642 *****************************************************************************/
1643static void
Ben Skeggse225f442012-11-21 14:40:21 +10001644nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001645{
1646 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1647 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001648 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001649
1650 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1651 if (!drm_detect_monitor_audio(nv_connector->edid))
1652 return;
1653
Ben Skeggs78951d22011-11-11 18:13:13 +10001654 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs78951d22011-11-11 18:13:13 +10001655
Ben Skeggs0a9e2b952012-11-08 14:03:56 +10001656 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or,
1657 nv_connector->base.eld,
1658 nv_connector->base.eld[2] * 4);
Ben Skeggs78951d22011-11-11 18:13:13 +10001659}
1660
1661static void
Ben Skeggse225f442012-11-21 14:40:21 +10001662nv50_audio_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001663{
1664 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001665 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001666
Ben Skeggs0a9e2b952012-11-08 14:03:56 +10001667 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or, NULL, 0);
Ben Skeggs78951d22011-11-11 18:13:13 +10001668}
1669
1670/******************************************************************************
1671 * HDMI
1672 *****************************************************************************/
1673static void
Ben Skeggse225f442012-11-21 14:40:21 +10001674nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001675{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001676 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1677 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1678 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001679 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001680 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001681 u32 rekey = 56; /* binary driver, and tegra constant */
1682 u32 max_ac_packet;
1683
1684 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1685 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1686 return;
1687
1688 max_ac_packet = mode->htotal - mode->hdisplay;
1689 max_ac_packet -= rekey;
1690 max_ac_packet -= 18; /* constant from tegra */
1691 max_ac_packet /= 32;
1692
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001693 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff,
1694 NV84_DISP_SOR_HDMI_PWR_STATE_ON |
1695 (max_ac_packet << 16) | rekey);
Ben Skeggs091e40c2011-11-11 20:46:00 +10001696
Ben Skeggse225f442012-11-21 14:40:21 +10001697 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001698}
1699
1700static void
Ben Skeggse225f442012-11-21 14:40:21 +10001701nv50_hdmi_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001702{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001703 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1704 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001705 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001706 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001707
Ben Skeggse225f442012-11-21 14:40:21 +10001708 nv50_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001709
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001710 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff, 0x00000000);
Ben Skeggs78951d22011-11-11 18:13:13 +10001711}
1712
1713/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001714 * SOR
1715 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001716static void
Ben Skeggse225f442012-11-21 14:40:21 +10001717nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001718{
1719 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1720 struct drm_device *dev = encoder->dev;
Ben Skeggse225f442012-11-21 14:40:21 +10001721 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001722 struct drm_encoder *partner;
1723 int or = nv_encoder->or;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001724
1725 nv_encoder->last_dpms = mode;
1726
1727 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1728 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1729
1730 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1731 continue;
1732
1733 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001734 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001735 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1736 return;
1737 break;
1738 }
1739 }
1740
Ben Skeggs74b66852012-11-08 12:01:39 +10001741 nv_call(disp->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
Ben Skeggs83fc0832011-07-05 13:08:40 +10001742}
1743
1744static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001745nv50_sor_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001746 const struct drm_display_mode *mode,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001747 struct drm_display_mode *adjusted_mode)
1748{
1749 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1750 struct nouveau_connector *nv_connector;
1751
1752 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1753 if (nv_connector && nv_connector->native_mode) {
1754 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1755 int id = adjusted_mode->base.id;
1756 *adjusted_mode = *nv_connector->native_mode;
1757 adjusted_mode->base.id = id;
1758 }
1759 }
1760
1761 return true;
1762}
1763
1764static void
Ben Skeggse225f442012-11-21 14:40:21 +10001765nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001766{
1767 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001768 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001769 const int or = nv_encoder->or;
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001770 u32 *push;
1771
1772 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001773 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001774
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001775 push = evo_wait(mast, 4);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001776 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001777 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001778 evo_mthd(push, 0x0600 + (or * 0x40), 1);
1779 evo_data(push, 0x00000000);
1780 } else {
1781 evo_mthd(push, 0x0200 + (or * 0x20), 1);
1782 evo_data(push, 0x00000000);
1783 }
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001784 evo_kick(push, mast);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001785 }
1786
Ben Skeggse225f442012-11-21 14:40:21 +10001787 nv50_hdmi_disconnect(encoder);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001788 }
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001789
1790 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1791 nv_encoder->crtc = NULL;
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001792}
1793
1794static void
Ben Skeggse225f442012-11-21 14:40:21 +10001795nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001796{
1797}
1798
1799static void
Ben Skeggse225f442012-11-21 14:40:21 +10001800nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001801 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001802{
Ben Skeggse225f442012-11-21 14:40:21 +10001803 struct nv50_disp *disp = nv50_disp(encoder->dev);
1804 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001805 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001806 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001807 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1808 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001809 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001810 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001811 u32 *push, lvds = 0;
1812 u8 owner = 1 << nv_crtc->index;
1813 u8 proto = 0xf;
1814 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001815
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001816 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1817 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001818 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001819 if (nv_encoder->dcb->sorconf.link & 1) {
1820 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001821 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001822 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001823 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001824 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001825 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001826 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001827
Ben Skeggse225f442012-11-21 14:40:21 +10001828 nv50_hdmi_mode_set(encoder, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001829 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001830 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001831 proto = 0x0;
1832
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001833 if (bios->fp_no_ddc) {
1834 if (bios->fp.dual_link)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001835 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001836 if (bios->fp.if_is_24bit)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001837 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001838 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001839 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001840 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001841 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001842 } else
1843 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001844 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001845 }
1846
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001847 if (lvds & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001848 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001849 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001850 } else {
1851 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001852 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001853 }
1854
1855 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001856 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001857 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001858
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001859 nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + nv_encoder->or, lvds);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001860 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001861 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001862 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001863 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001864 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001865 } else
1866 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001867 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001868 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001869 } else {
1870 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1871 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001872 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001873
1874 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001875 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001876 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001877 proto = 0x9;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001878 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001879 default:
1880 BUG_ON(1);
1881 break;
1882 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001883
Ben Skeggse225f442012-11-21 14:40:21 +10001884 nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001885
Ben Skeggse225f442012-11-21 14:40:21 +10001886 push = evo_wait(nv50_mast(dev), 8);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001887 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001888 if (nv50_vers(mast) < NVD0_DISP_CLASS) {
Ben Skeggse2de1792013-02-11 13:56:31 +10001889 u32 ctrl = (depth << 16) | (proto << 8) | owner;
1890 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1891 ctrl |= 0x00001000;
1892 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1893 ctrl |= 0x00002000;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001894 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x040), 1);
Ben Skeggse2de1792013-02-11 13:56:31 +10001895 evo_data(push, ctrl);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001896 } else {
1897 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1898 u32 syncs = 0x00000001;
1899
1900 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1901 syncs |= 0x00000008;
1902 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1903 syncs |= 0x00000010;
1904
1905 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1906 magic |= 0x00000001;
1907
1908 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1909 evo_data(push, syncs | (depth << 6));
1910 evo_data(push, magic);
1911 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 1);
1912 evo_data(push, owner | (proto << 8));
1913 }
1914
1915 evo_kick(push, mast);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001916 }
1917
1918 nv_encoder->crtc = encoder->crtc;
1919}
1920
1921static void
Ben Skeggse225f442012-11-21 14:40:21 +10001922nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001923{
1924 drm_encoder_cleanup(encoder);
1925 kfree(encoder);
1926}
1927
Ben Skeggse225f442012-11-21 14:40:21 +10001928static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
1929 .dpms = nv50_sor_dpms,
1930 .mode_fixup = nv50_sor_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10001931 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10001932 .commit = nv50_sor_commit,
1933 .mode_set = nv50_sor_mode_set,
1934 .disable = nv50_sor_disconnect,
1935 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001936};
1937
Ben Skeggse225f442012-11-21 14:40:21 +10001938static const struct drm_encoder_funcs nv50_sor_func = {
1939 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001940};
1941
1942static int
Ben Skeggse225f442012-11-21 14:40:21 +10001943nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001944{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001945 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1946 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001947 struct nouveau_encoder *nv_encoder;
1948 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001949 int type;
1950
1951 switch (dcbe->type) {
1952 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1953 case DCB_OUTPUT_TMDS:
1954 case DCB_OUTPUT_DP:
1955 default:
1956 type = DRM_MODE_ENCODER_TMDS;
1957 break;
1958 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001959
1960 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1961 if (!nv_encoder)
1962 return -ENOMEM;
1963 nv_encoder->dcb = dcbe;
1964 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001965 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001966 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1967
1968 encoder = to_drm_encoder(nv_encoder);
1969 encoder->possible_crtcs = dcbe->heads;
1970 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001971 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001972 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001973
1974 drm_mode_connector_attach_encoder(connector, encoder);
1975 return 0;
1976}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001977
1978/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10001979 * PIOR
1980 *****************************************************************************/
1981
1982static void
1983nv50_pior_dpms(struct drm_encoder *encoder, int mode)
1984{
1985 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1986 struct nv50_disp *disp = nv50_disp(encoder->dev);
1987 u32 mthd = (nv_encoder->dcb->type << 12) | nv_encoder->or;
1988 u32 ctrl = (mode == DRM_MODE_DPMS_ON);
1989 nv_call(disp->core, NV50_DISP_PIOR_PWR + mthd, ctrl);
1990}
1991
1992static bool
1993nv50_pior_mode_fixup(struct drm_encoder *encoder,
1994 const struct drm_display_mode *mode,
1995 struct drm_display_mode *adjusted_mode)
1996{
1997 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1998 struct nouveau_connector *nv_connector;
1999
2000 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2001 if (nv_connector && nv_connector->native_mode) {
2002 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
2003 int id = adjusted_mode->base.id;
2004 *adjusted_mode = *nv_connector->native_mode;
2005 adjusted_mode->base.id = id;
2006 }
2007 }
2008
2009 adjusted_mode->clock *= 2;
2010 return true;
2011}
2012
2013static void
2014nv50_pior_commit(struct drm_encoder *encoder)
2015{
2016}
2017
2018static void
2019nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2020 struct drm_display_mode *adjusted_mode)
2021{
2022 struct nv50_mast *mast = nv50_mast(encoder->dev);
2023 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2024 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2025 struct nouveau_connector *nv_connector;
2026 u8 owner = 1 << nv_crtc->index;
2027 u8 proto, depth;
2028 u32 *push;
2029
2030 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2031 switch (nv_connector->base.display_info.bpc) {
2032 case 10: depth = 0x6; break;
2033 case 8: depth = 0x5; break;
2034 case 6: depth = 0x2; break;
2035 default: depth = 0x0; break;
2036 }
2037
2038 switch (nv_encoder->dcb->type) {
2039 case DCB_OUTPUT_TMDS:
2040 case DCB_OUTPUT_DP:
2041 proto = 0x0;
2042 break;
2043 default:
2044 BUG_ON(1);
2045 break;
2046 }
2047
2048 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2049
2050 push = evo_wait(mast, 8);
2051 if (push) {
2052 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
2053 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2054 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2055 ctrl |= 0x00001000;
2056 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2057 ctrl |= 0x00002000;
2058 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2059 evo_data(push, ctrl);
2060 }
2061
2062 evo_kick(push, mast);
2063 }
2064
2065 nv_encoder->crtc = encoder->crtc;
2066}
2067
2068static void
2069nv50_pior_disconnect(struct drm_encoder *encoder)
2070{
2071 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2072 struct nv50_mast *mast = nv50_mast(encoder->dev);
2073 const int or = nv_encoder->or;
2074 u32 *push;
2075
2076 if (nv_encoder->crtc) {
2077 nv50_crtc_prepare(nv_encoder->crtc);
2078
2079 push = evo_wait(mast, 4);
2080 if (push) {
2081 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
2082 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2083 evo_data(push, 0x00000000);
2084 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002085 evo_kick(push, mast);
2086 }
2087 }
2088
2089 nv_encoder->crtc = NULL;
2090}
2091
2092static void
2093nv50_pior_destroy(struct drm_encoder *encoder)
2094{
2095 drm_encoder_cleanup(encoder);
2096 kfree(encoder);
2097}
2098
2099static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2100 .dpms = nv50_pior_dpms,
2101 .mode_fixup = nv50_pior_mode_fixup,
2102 .prepare = nv50_pior_disconnect,
2103 .commit = nv50_pior_commit,
2104 .mode_set = nv50_pior_mode_set,
2105 .disable = nv50_pior_disconnect,
2106 .get_crtc = nv50_display_crtc_get,
2107};
2108
2109static const struct drm_encoder_funcs nv50_pior_func = {
2110 .destroy = nv50_pior_destroy,
2111};
2112
2113static int
2114nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2115{
2116 struct nouveau_drm *drm = nouveau_drm(connector->dev);
2117 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
2118 struct nouveau_i2c_port *ddc = NULL;
2119 struct nouveau_encoder *nv_encoder;
2120 struct drm_encoder *encoder;
2121 int type;
2122
2123 switch (dcbe->type) {
2124 case DCB_OUTPUT_TMDS:
2125 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2126 type = DRM_MODE_ENCODER_TMDS;
2127 break;
2128 case DCB_OUTPUT_DP:
2129 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2130 type = DRM_MODE_ENCODER_TMDS;
2131 break;
2132 default:
2133 return -ENODEV;
2134 }
2135
2136 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2137 if (!nv_encoder)
2138 return -ENOMEM;
2139 nv_encoder->dcb = dcbe;
2140 nv_encoder->or = ffs(dcbe->or) - 1;
2141 nv_encoder->i2c = ddc;
2142
2143 encoder = to_drm_encoder(nv_encoder);
2144 encoder->possible_crtcs = dcbe->heads;
2145 encoder->possible_clones = 0;
2146 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2147 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2148
2149 drm_mode_connector_attach_encoder(connector, encoder);
2150 return 0;
2151}
2152
2153/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002154 * Init
2155 *****************************************************************************/
Ben Skeggs2a44e492011-11-09 11:36:33 +10002156void
Ben Skeggse225f442012-11-21 14:40:21 +10002157nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002158{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002159}
2160
2161int
Ben Skeggse225f442012-11-21 14:40:21 +10002162nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002163{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002164 struct nv50_disp *disp = nv50_disp(dev);
2165 struct drm_crtc *crtc;
2166 u32 *push;
2167
2168 push = evo_wait(nv50_mast(dev), 32);
2169 if (!push)
2170 return -EBUSY;
2171
2172 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2173 struct nv50_sync *sync = nv50_sync(crtc);
2174 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002175 }
2176
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002177 evo_mthd(push, 0x0088, 1);
2178 evo_data(push, NvEvoSync);
2179 evo_kick(push, nv50_mast(dev));
2180 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002181}
2182
2183void
Ben Skeggse225f442012-11-21 14:40:21 +10002184nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002185{
Ben Skeggse225f442012-11-21 14:40:21 +10002186 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002187
Ben Skeggse225f442012-11-21 14:40:21 +10002188 nv50_dmac_destroy(disp->core, &disp->mast.base);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002189
Ben Skeggs816af2f2011-11-16 15:48:48 +10002190 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002191 if (disp->sync)
2192 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002193 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002194
Ben Skeggs77145f12012-07-31 16:16:21 +10002195 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002196 kfree(disp);
2197}
2198
2199int
Ben Skeggse225f442012-11-21 14:40:21 +10002200nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002201{
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002202 static const u16 oclass[] = {
Ben Skeggse5398b22013-03-30 22:31:25 +10002203 NVF0_DISP_CLASS,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002204 NVE0_DISP_CLASS,
2205 NVD0_DISP_CLASS,
Ben Skeggs63718a02012-11-16 11:44:14 +10002206 NVA3_DISP_CLASS,
2207 NV94_DISP_CLASS,
2208 NVA0_DISP_CLASS,
2209 NV84_DISP_CLASS,
2210 NV50_DISP_CLASS,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002211 };
Ben Skeggs77145f12012-07-31 16:16:21 +10002212 struct nouveau_device *device = nouveau_dev(dev);
2213 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002214 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002215 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002216 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002217 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002218 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002219
2220 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2221 if (!disp)
2222 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10002223
2224 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002225 nouveau_display(dev)->dtor = nv50_display_destroy;
2226 nouveau_display(dev)->init = nv50_display_init;
2227 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002228
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002229 /* small shared memory area we use for notifiers and semaphores */
2230 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2231 0, 0x0000, NULL, &disp->sync);
2232 if (!ret) {
2233 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002234 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002235 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002236 if (ret)
2237 nouveau_bo_unpin(disp->sync);
2238 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002239 if (ret)
2240 nouveau_bo_ref(NULL, &disp->sync);
2241 }
2242
2243 if (ret)
2244 goto out;
2245
2246 /* attempt to allocate a supported evo display class */
2247 ret = -ENODEV;
2248 for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
2249 ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
2250 0xd1500000, oclass[i], NULL, 0,
2251 &disp->core);
2252 }
2253
2254 if (ret)
2255 goto out;
2256
2257 /* allocate master evo channel */
Ben Skeggse225f442012-11-21 14:40:21 +10002258 ret = nv50_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002259 &(struct nv50_display_mast_class) {
2260 .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
2261 }, sizeof(struct nv50_display_mast_class),
2262 disp->sync->bo.offset, &disp->mast.base);
2263 if (ret)
2264 goto out;
2265
Ben Skeggs438d99e2011-07-05 16:48:06 +10002266 /* create crtc objects to represent the hw heads */
Ben Skeggs63718a02012-11-16 11:44:14 +10002267 if (nv_mclass(disp->core) >= NVD0_DISP_CLASS)
2268 crtcs = nv_rd32(device, 0x022448);
2269 else
2270 crtcs = 2;
2271
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002272 for (i = 0; i < crtcs; i++) {
Ben Skeggse225f442012-11-21 14:40:21 +10002273 ret = nv50_crtc_create(dev, disp->core, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002274 if (ret)
2275 goto out;
2276 }
2277
Ben Skeggs83fc0832011-07-05 13:08:40 +10002278 /* create encoder/connector objects based on VBIOS DCB table */
2279 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2280 connector = nouveau_connector_create(dev, dcbe->connector);
2281 if (IS_ERR(connector))
2282 continue;
2283
Ben Skeggseb6313a2013-02-11 09:52:58 +10002284 if (dcbe->location == DCB_LOC_ON_CHIP) {
2285 switch (dcbe->type) {
2286 case DCB_OUTPUT_TMDS:
2287 case DCB_OUTPUT_LVDS:
2288 case DCB_OUTPUT_DP:
2289 ret = nv50_sor_create(connector, dcbe);
2290 break;
2291 case DCB_OUTPUT_ANALOG:
2292 ret = nv50_dac_create(connector, dcbe);
2293 break;
2294 default:
2295 ret = -ENODEV;
2296 break;
2297 }
2298 } else {
2299 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002300 }
2301
Ben Skeggseb6313a2013-02-11 09:52:58 +10002302 if (ret) {
2303 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2304 dcbe->location, dcbe->type,
2305 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002306 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002307 }
2308 }
2309
2310 /* cull any connectors we created that don't have an encoder */
2311 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2312 if (connector->encoder_ids[0])
2313 continue;
2314
Ben Skeggs77145f12012-07-31 16:16:21 +10002315 NV_WARN(drm, "%s has no encoders, removing\n",
Ben Skeggs83fc0832011-07-05 13:08:40 +10002316 drm_get_connector_name(connector));
2317 connector->funcs->destroy(connector);
2318 }
2319
Ben Skeggs26f6d882011-07-04 16:25:18 +10002320out:
2321 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002322 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002323 return ret;
2324}