blob: 2db57990f65c3c7c6a3aca012b30384e36942876 [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,
162 .limit = pfb->ram.size - 1,
163 .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,
175 .limit = pfb->ram.size - 1,
176 .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,
188 .limit = pfb->ram.size - 1,
189 .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,
207 .limit = pfb->ram.size - 1,
208 .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,
219 .limit = pfb->ram.size - 1,
220 .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,
231 .limit = pfb->ram.size - 1,
232 .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,
249 .limit = pfb->ram.size - 1,
250 .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,
262 .limit = pfb->ram.size - 1,
263 .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,
319 .limit = pfb->ram.size - 1,
320 }, 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 Skeggse225f442012-11-21 14:40:21 +1000358 struct nv50_curs curs;
359 struct nv50_sync sync;
360 struct nv50_ovly ovly;
361 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000362};
363
Ben Skeggse225f442012-11-21 14:40:21 +1000364#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
365#define nv50_curs(c) (&nv50_head(c)->curs)
366#define nv50_sync(c) (&nv50_head(c)->sync)
367#define nv50_ovly(c) (&nv50_head(c)->ovly)
368#define nv50_oimm(c) (&nv50_head(c)->oimm)
369#define nv50_chan(c) (&(c)->base.base)
370#define nv50_vers(c) nv_mclass(nv50_chan(c)->user)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000371
Ben Skeggse225f442012-11-21 14:40:21 +1000372struct nv50_disp {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000373 struct nouveau_object *core;
Ben Skeggse225f442012-11-21 14:40:21 +1000374 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000375
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000376 u32 modeset;
377
378 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000379};
380
Ben Skeggse225f442012-11-21 14:40:21 +1000381static struct nv50_disp *
382nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000383{
Ben Skeggs77145f12012-07-31 16:16:21 +1000384 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000385}
386
Ben Skeggse225f442012-11-21 14:40:21 +1000387#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000388
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000389static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000390nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000391{
392 return nouveau_encoder(encoder)->crtc;
393}
394
395/******************************************************************************
396 * EVO channel helpers
397 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000398static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000399evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000400{
Ben Skeggse225f442012-11-21 14:40:21 +1000401 struct nv50_dmac *dmac = evoc;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000402 u32 put = nv_ro32(dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000403
Daniel Vetter59ad1462012-12-02 14:49:44 +0100404 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000405 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000406 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000407
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000408 nv_wo32(dmac->base.user, 0x0000, 0x00000000);
409 if (!nv_wait(dmac->base.user, 0x0004, ~0, 0x00000000)) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100410 mutex_unlock(&dmac->lock);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000411 NV_ERROR(dmac->base.user, "channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000412 return NULL;
413 }
414
415 put = 0;
416 }
417
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000418 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000419}
420
421static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000422evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000423{
Ben Skeggse225f442012-11-21 14:40:21 +1000424 struct nv50_dmac *dmac = evoc;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000425 nv_wo32(dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100426 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000427}
428
429#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
430#define evo_data(p,d) *((p)++) = (d)
431
Ben Skeggs3376ee32011-11-12 14:28:12 +1000432static bool
433evo_sync_wait(void *data)
434{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500435 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
436 return true;
437 usleep_range(1, 2);
438 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000439}
440
441static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000442evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000443{
Ben Skeggs77145f12012-07-31 16:16:21 +1000444 struct nouveau_device *device = nouveau_dev(dev);
Ben Skeggse225f442012-11-21 14:40:21 +1000445 struct nv50_disp *disp = nv50_disp(dev);
446 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000447 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000448 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000449 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000450 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000451 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000452 evo_mthd(push, 0x0080, 2);
453 evo_data(push, 0x00000000);
454 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000455 evo_kick(push, mast);
Ben Skeggs77145f12012-07-31 16:16:21 +1000456 if (nv_wait_cb(device, evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000457 return 0;
458 }
459
460 return -EBUSY;
461}
462
463/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000464 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000465 *****************************************************************************/
466struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000467nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000468{
Ben Skeggse225f442012-11-21 14:40:21 +1000469 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000470}
471
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000472struct nv50_display_flip {
473 struct nv50_disp *disp;
474 struct nv50_sync *chan;
475};
476
477static bool
478nv50_display_flip_wait(void *data)
479{
480 struct nv50_display_flip *flip = data;
481 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
482 flip->chan->data);
483 return true;
484 usleep_range(1, 2);
485 return false;
486}
487
Ben Skeggs3376ee32011-11-12 14:28:12 +1000488void
Ben Skeggse225f442012-11-21 14:40:21 +1000489nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000490{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000491 struct nouveau_device *device = nouveau_dev(crtc->dev);
492 struct nv50_display_flip flip = {
493 .disp = nv50_disp(crtc->dev),
494 .chan = nv50_sync(crtc),
495 };
Ben Skeggs3376ee32011-11-12 14:28:12 +1000496 u32 *push;
497
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000498 push = evo_wait(flip.chan, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000499 if (push) {
500 evo_mthd(push, 0x0084, 1);
501 evo_data(push, 0x00000000);
502 evo_mthd(push, 0x0094, 1);
503 evo_data(push, 0x00000000);
504 evo_mthd(push, 0x00c0, 1);
505 evo_data(push, 0x00000000);
506 evo_mthd(push, 0x0080, 1);
507 evo_data(push, 0x00000000);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000508 evo_kick(push, flip.chan);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000509 }
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000510
511 nv_wait_cb(device, nv50_display_flip_wait, &flip);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000512}
513
514int
Ben Skeggse225f442012-11-21 14:40:21 +1000515nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000516 struct nouveau_channel *chan, u32 swap_interval)
517{
518 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000519 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000520 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000521 int head = nv_crtc->index, ret;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000522 u32 *push;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000523
524 swap_interval <<= 4;
525 if (swap_interval == 0)
526 swap_interval |= 0x100;
527
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000528 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000529 if (unlikely(push == NULL))
530 return -EBUSY;
531
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000532 if (chan && nv_mclass(chan->object) < NV84_CHANNEL_IND_CLASS) {
533 ret = RING_SPACE(chan, 8);
534 if (ret)
535 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000536
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000537 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
538 OUT_RING (chan, NvEvoSema0 + head);
539 OUT_RING (chan, sync->addr ^ 0x10);
540 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
541 OUT_RING (chan, sync->data + 1);
542 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
543 OUT_RING (chan, sync->addr);
544 OUT_RING (chan, sync->data);
545 } else
546 if (chan && nv_mclass(chan->object) < NVC0_CHANNEL_IND_CLASS) {
547 u64 addr = nv84_fence_crtc(chan, head) + sync->addr;
548 ret = RING_SPACE(chan, 12);
549 if (ret)
550 return ret;
Ben Skeggsa34caf72013-02-14 09:28:37 +1000551
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000552 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
553 OUT_RING (chan, chan->vram);
554 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
555 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
556 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
557 OUT_RING (chan, sync->data + 1);
558 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
559 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
560 OUT_RING (chan, upper_32_bits(addr));
561 OUT_RING (chan, lower_32_bits(addr));
562 OUT_RING (chan, sync->data);
563 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
564 } else
565 if (chan) {
566 u64 addr = nv84_fence_crtc(chan, head) + sync->addr;
567 ret = RING_SPACE(chan, 10);
568 if (ret)
569 return ret;
Ben Skeggs67f97182013-02-26 12:02:54 +1000570
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000571 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
572 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
573 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
574 OUT_RING (chan, sync->data + 1);
575 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
576 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
577 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
578 OUT_RING (chan, upper_32_bits(addr));
579 OUT_RING (chan, lower_32_bits(addr));
580 OUT_RING (chan, sync->data);
581 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
582 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
583 }
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500584
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000585 if (chan) {
586 sync->addr ^= 0x10;
587 sync->data++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000588 FIRE_RING (chan);
589 } else {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000590 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000591 }
592
593 /* queue the flip */
594 evo_mthd(push, 0x0100, 1);
595 evo_data(push, 0xfffe0000);
596 evo_mthd(push, 0x0084, 1);
597 evo_data(push, swap_interval);
598 if (!(swap_interval & 0x00000100)) {
599 evo_mthd(push, 0x00e0, 1);
600 evo_data(push, 0x40000000);
601 }
602 evo_mthd(push, 0x0088, 4);
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +1000603 evo_data(push, sync->addr);
604 evo_data(push, sync->data++);
605 evo_data(push, sync->data);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000606 evo_data(push, NvEvoSync);
607 evo_mthd(push, 0x00a0, 2);
608 evo_data(push, 0x00000000);
609 evo_data(push, 0x00000000);
610 evo_mthd(push, 0x00c0, 1);
611 evo_data(push, nv_fb->r_dma);
612 evo_mthd(push, 0x0110, 2);
613 evo_data(push, 0x00000000);
614 evo_data(push, 0x00000000);
Ben Skeggse225f442012-11-21 14:40:21 +1000615 if (nv50_vers(sync) < NVD0_DISP_SYNC_CLASS) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000616 evo_mthd(push, 0x0800, 5);
617 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
618 evo_data(push, 0);
619 evo_data(push, (fb->height << 16) | fb->width);
620 evo_data(push, nv_fb->r_pitch);
621 evo_data(push, nv_fb->r_format);
622 } else {
623 evo_mthd(push, 0x0400, 5);
624 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
625 evo_data(push, 0);
626 evo_data(push, (fb->height << 16) | fb->width);
627 evo_data(push, nv_fb->r_pitch);
628 evo_data(push, nv_fb->r_format);
629 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000630 evo_mthd(push, 0x0080, 1);
631 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000632 evo_kick(push, sync);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000633 return 0;
634}
635
Ben Skeggs26f6d882011-07-04 16:25:18 +1000636/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000637 * CRTC
638 *****************************************************************************/
639static int
Ben Skeggse225f442012-11-21 14:40:21 +1000640nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000641{
Ben Skeggse225f442012-11-21 14:40:21 +1000642 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000643 struct nouveau_connector *nv_connector;
644 struct drm_connector *connector;
645 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000646
Ben Skeggs488ff202011-10-17 10:38:10 +1000647 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000648 connector = &nv_connector->base;
649 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
650 if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
651 mode = DITHERING_MODE_DYNAMIC2X2;
652 } else {
653 mode = nv_connector->dithering_mode;
654 }
655
656 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
657 if (connector->display_info.bpc >= 8)
658 mode |= DITHERING_DEPTH_8BPC;
659 } else {
660 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000661 }
662
Ben Skeggsde8268c2012-11-16 10:24:31 +1000663 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000664 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000665 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000666 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
667 evo_data(push, mode);
668 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000669 if (nv50_vers(mast) < NVE0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000670 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
671 evo_data(push, mode);
672 } else {
673 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
674 evo_data(push, mode);
675 }
676
Ben Skeggs438d99e2011-07-05 16:48:06 +1000677 if (update) {
678 evo_mthd(push, 0x0080, 1);
679 evo_data(push, 0x00000000);
680 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000681 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000682 }
683
684 return 0;
685}
686
687static int
Ben Skeggse225f442012-11-21 14:40:21 +1000688nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000689{
Ben Skeggse225f442012-11-21 14:40:21 +1000690 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000691 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000692 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000693 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000694 int mode = DRM_MODE_SCALE_NONE;
695 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000696
Ben Skeggs92854622011-11-11 23:49:06 +1000697 /* start off at the resolution we programmed the crtc for, this
698 * effectively handles NONE/FULL scaling
699 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000700 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000701 if (nv_connector && nv_connector->native_mode)
702 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000703
Ben Skeggs92854622011-11-11 23:49:06 +1000704 if (mode != DRM_MODE_SCALE_NONE)
705 omode = nv_connector->native_mode;
706 else
707 omode = umode;
708
709 oX = omode->hdisplay;
710 oY = omode->vdisplay;
711 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
712 oY *= 2;
713
714 /* add overscan compensation if necessary, will keep the aspect
715 * ratio the same as the backend mode unless overridden by the
716 * user setting both hborder and vborder properties.
717 */
718 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
719 (nv_connector->underscan == UNDERSCAN_AUTO &&
720 nv_connector->edid &&
721 drm_detect_hdmi_monitor(nv_connector->edid)))) {
722 u32 bX = nv_connector->underscan_hborder;
723 u32 bY = nv_connector->underscan_vborder;
724 u32 aspect = (oY << 19) / oX;
725
726 if (bX) {
727 oX -= (bX * 2);
728 if (bY) oY -= (bY * 2);
729 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
730 } else {
731 oX -= (oX >> 4) + 32;
732 if (bY) oY -= (bY * 2);
733 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000734 }
735 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000736
Ben Skeggs92854622011-11-11 23:49:06 +1000737 /* handle CENTER/ASPECT scaling, taking into account the areas
738 * removed already for overscan compensation
739 */
740 switch (mode) {
741 case DRM_MODE_SCALE_CENTER:
742 oX = min((u32)umode->hdisplay, oX);
743 oY = min((u32)umode->vdisplay, oY);
744 /* fall-through */
745 case DRM_MODE_SCALE_ASPECT:
746 if (oY < oX) {
747 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
748 oX = ((oY * aspect) + (aspect / 2)) >> 19;
749 } else {
750 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
751 oY = ((oX * aspect) + (aspect / 2)) >> 19;
752 }
753 break;
754 default:
755 break;
756 }
757
Ben Skeggsde8268c2012-11-16 10:24:31 +1000758 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000759 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000760 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000761 /*XXX: SCALE_CTRL_ACTIVE??? */
762 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
763 evo_data(push, (oY << 16) | oX);
764 evo_data(push, (oY << 16) | oX);
765 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
766 evo_data(push, 0x00000000);
767 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
768 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
769 } else {
770 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
771 evo_data(push, (oY << 16) | oX);
772 evo_data(push, (oY << 16) | oX);
773 evo_data(push, (oY << 16) | oX);
774 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
775 evo_data(push, 0x00000000);
776 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
777 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
778 }
779
780 evo_kick(push, mast);
781
Ben Skeggs3376ee32011-11-12 14:28:12 +1000782 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000783 nv50_display_flip_stop(crtc);
784 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000785 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000786 }
787
788 return 0;
789}
790
791static int
Ben Skeggse225f442012-11-21 14:40:21 +1000792nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000793{
Ben Skeggse225f442012-11-21 14:40:21 +1000794 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000795 u32 *push, hue, vib;
796 int adj;
797
798 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
799 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
800 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
801
802 push = evo_wait(mast, 16);
803 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000804 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000805 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
806 evo_data(push, (hue << 20) | (vib << 8));
807 } else {
808 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
809 evo_data(push, (hue << 20) | (vib << 8));
810 }
811
812 if (update) {
813 evo_mthd(push, 0x0080, 1);
814 evo_data(push, 0x00000000);
815 }
816 evo_kick(push, mast);
817 }
818
819 return 0;
820}
821
822static int
Ben Skeggse225f442012-11-21 14:40:21 +1000823nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000824 int x, int y, bool update)
825{
826 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000827 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000828 u32 *push;
829
Ben Skeggsde8268c2012-11-16 10:24:31 +1000830 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000831 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000832 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000833 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
834 evo_data(push, nvfb->nvbo->bo.offset >> 8);
835 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
836 evo_data(push, (fb->height << 16) | fb->width);
837 evo_data(push, nvfb->r_pitch);
838 evo_data(push, nvfb->r_format);
839 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
840 evo_data(push, (y << 16) | x);
Ben Skeggse225f442012-11-21 14:40:21 +1000841 if (nv50_vers(mast) > NV50_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000842 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
843 evo_data(push, nvfb->r_dma);
844 }
845 } else {
846 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
847 evo_data(push, nvfb->nvbo->bo.offset >> 8);
848 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
849 evo_data(push, (fb->height << 16) | fb->width);
850 evo_data(push, nvfb->r_pitch);
851 evo_data(push, nvfb->r_format);
852 evo_data(push, nvfb->r_dma);
853 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
854 evo_data(push, (y << 16) | x);
855 }
856
Ben Skeggsa46232e2011-07-07 15:23:48 +1000857 if (update) {
858 evo_mthd(push, 0x0080, 1);
859 evo_data(push, 0x00000000);
860 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000861 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000862 }
863
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000864 nv_crtc->fb.tile_flags = nvfb->r_dma;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000865 return 0;
866}
867
868static void
Ben Skeggse225f442012-11-21 14:40:21 +1000869nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000870{
Ben Skeggse225f442012-11-21 14:40:21 +1000871 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000872 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000873 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000874 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000875 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
876 evo_data(push, 0x85000000);
877 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
878 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000879 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000880 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
881 evo_data(push, 0x85000000);
882 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
883 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
884 evo_data(push, NvEvoVRAM);
885 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000886 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
887 evo_data(push, 0x85000000);
888 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
889 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000890 evo_data(push, NvEvoVRAM);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000891 }
892 evo_kick(push, mast);
893 }
894}
895
896static void
Ben Skeggse225f442012-11-21 14:40:21 +1000897nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000898{
Ben Skeggse225f442012-11-21 14:40:21 +1000899 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000900 u32 *push = evo_wait(mast, 16);
901 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000902 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000903 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
904 evo_data(push, 0x05000000);
905 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000906 if (nv50_vers(mast) < NVD0_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 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
910 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000911 } else {
912 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
913 evo_data(push, 0x05000000);
914 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
915 evo_data(push, 0x00000000);
916 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000917 evo_kick(push, mast);
918 }
919}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000920
Ben Skeggsde8268c2012-11-16 10:24:31 +1000921static void
Ben Skeggse225f442012-11-21 14:40:21 +1000922nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000923{
Ben Skeggse225f442012-11-21 14:40:21 +1000924 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000925
926 if (show)
Ben Skeggse225f442012-11-21 14:40:21 +1000927 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000928 else
Ben Skeggse225f442012-11-21 14:40:21 +1000929 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000930
931 if (update) {
932 u32 *push = evo_wait(mast, 2);
933 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000934 evo_mthd(push, 0x0080, 1);
935 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000936 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000937 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000938 }
939}
940
941static void
Ben Skeggse225f442012-11-21 14:40:21 +1000942nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000943{
944}
945
946static void
Ben Skeggse225f442012-11-21 14:40:21 +1000947nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000948{
949 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000950 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000951 u32 *push;
952
Ben Skeggse225f442012-11-21 14:40:21 +1000953 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000954
Ben Skeggsde8268c2012-11-16 10:24:31 +1000955 push = evo_wait(mast, 2);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000956 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000957 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000958 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
959 evo_data(push, 0x00000000);
960 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
961 evo_data(push, 0x40000000);
962 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000963 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000964 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
965 evo_data(push, 0x00000000);
966 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
967 evo_data(push, 0x40000000);
968 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
969 evo_data(push, 0x00000000);
970 } else {
971 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
972 evo_data(push, 0x00000000);
973 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
974 evo_data(push, 0x03000000);
975 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
976 evo_data(push, 0x00000000);
977 }
978
979 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000980 }
981
Ben Skeggse225f442012-11-21 14:40:21 +1000982 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000983}
984
985static void
Ben Skeggse225f442012-11-21 14:40:21 +1000986nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000987{
988 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000989 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000990 u32 *push;
991
Ben Skeggsde8268c2012-11-16 10:24:31 +1000992 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000993 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000994 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000995 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
996 evo_data(push, NvEvoVRAM_LP);
997 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
998 evo_data(push, 0xc0000000);
999 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1000 } else
Ben Skeggse225f442012-11-21 14:40:21 +10001001 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001002 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1003 evo_data(push, nv_crtc->fb.tile_flags);
1004 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1005 evo_data(push, 0xc0000000);
1006 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1007 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
1008 evo_data(push, NvEvoVRAM);
1009 } else {
1010 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
1011 evo_data(push, nv_crtc->fb.tile_flags);
1012 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1013 evo_data(push, 0x83000000);
1014 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1015 evo_data(push, 0x00000000);
1016 evo_data(push, 0x00000000);
1017 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
1018 evo_data(push, NvEvoVRAM);
1019 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1020 evo_data(push, 0xffffff00);
1021 }
1022
1023 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001024 }
1025
Ben Skeggse225f442012-11-21 14:40:21 +10001026 nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
1027 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001028}
1029
1030static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001031nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001032 struct drm_display_mode *adjusted_mode)
1033{
1034 return true;
1035}
1036
1037static int
Ben Skeggse225f442012-11-21 14:40:21 +10001038nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001039{
1040 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
1041 int ret;
1042
1043 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
1044 if (ret)
1045 return ret;
1046
1047 if (old_fb) {
1048 nvfb = nouveau_framebuffer(old_fb);
1049 nouveau_bo_unpin(nvfb->nvbo);
1050 }
1051
1052 return 0;
1053}
1054
1055static int
Ben Skeggse225f442012-11-21 14:40:21 +10001056nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001057 struct drm_display_mode *mode, int x, int y,
1058 struct drm_framebuffer *old_fb)
1059{
Ben Skeggse225f442012-11-21 14:40:21 +10001060 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001061 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1062 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001063 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1064 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1065 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1066 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
1067 u32 vblan2e = 0, vblan2s = 1;
Ben Skeggs3488c572012-03-12 11:42:20 +10001068 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001069 int ret;
1070
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001071 hactive = mode->htotal;
1072 hsynce = mode->hsync_end - mode->hsync_start - 1;
1073 hbackp = mode->htotal - mode->hsync_end;
1074 hblanke = hsynce + hbackp;
1075 hfrontp = mode->hsync_start - mode->hdisplay;
1076 hblanks = mode->htotal - hfrontp - 1;
1077
1078 vactive = mode->vtotal * vscan / ilace;
1079 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1080 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1081 vblanke = vsynce + vbackp;
1082 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1083 vblanks = vactive - vfrontp - 1;
1084 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1085 vblan2e = vactive + vsynce + vbackp;
1086 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1087 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001088 }
1089
Ben Skeggse225f442012-11-21 14:40:21 +10001090 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001091 if (ret)
1092 return ret;
1093
Ben Skeggsde8268c2012-11-16 10:24:31 +10001094 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001095 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001096 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001097 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1098 evo_data(push, 0x00800000 | mode->clock);
1099 evo_data(push, (ilace == 2) ? 2 : 0);
1100 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
1101 evo_data(push, 0x00000000);
1102 evo_data(push, (vactive << 16) | hactive);
1103 evo_data(push, ( vsynce << 16) | hsynce);
1104 evo_data(push, (vblanke << 16) | hblanke);
1105 evo_data(push, (vblanks << 16) | hblanks);
1106 evo_data(push, (vblan2e << 16) | vblan2s);
1107 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
1108 evo_data(push, 0x00000000);
1109 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1110 evo_data(push, 0x00000311);
1111 evo_data(push, 0x00000100);
1112 } else {
1113 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1114 evo_data(push, 0x00000000);
1115 evo_data(push, (vactive << 16) | hactive);
1116 evo_data(push, ( vsynce << 16) | hsynce);
1117 evo_data(push, (vblanke << 16) | hblanke);
1118 evo_data(push, (vblanks << 16) | hblanks);
1119 evo_data(push, (vblan2e << 16) | vblan2s);
1120 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1121 evo_data(push, 0x00000000); /* ??? */
1122 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1123 evo_data(push, mode->clock * 1000);
1124 evo_data(push, 0x00200000); /* ??? */
1125 evo_data(push, mode->clock * 1000);
1126 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1127 evo_data(push, 0x00000311);
1128 evo_data(push, 0x00000100);
1129 }
1130
1131 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001132 }
1133
1134 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001135 nv50_crtc_set_dither(nv_crtc, false);
1136 nv50_crtc_set_scale(nv_crtc, false);
1137 nv50_crtc_set_color_vibrance(nv_crtc, false);
1138 nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001139 return 0;
1140}
1141
1142static int
Ben Skeggse225f442012-11-21 14:40:21 +10001143nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001144 struct drm_framebuffer *old_fb)
1145{
Ben Skeggs77145f12012-07-31 16:16:21 +10001146 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001147 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1148 int ret;
1149
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001150 if (!crtc->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001151 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001152 return 0;
1153 }
1154
Ben Skeggse225f442012-11-21 14:40:21 +10001155 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001156 if (ret)
1157 return ret;
1158
Ben Skeggse225f442012-11-21 14:40:21 +10001159 nv50_display_flip_stop(crtc);
1160 nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
1161 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001162 return 0;
1163}
1164
1165static int
Ben Skeggse225f442012-11-21 14:40:21 +10001166nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001167 struct drm_framebuffer *fb, int x, int y,
1168 enum mode_set_atomic state)
1169{
1170 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001171 nv50_display_flip_stop(crtc);
1172 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001173 return 0;
1174}
1175
1176static void
Ben Skeggse225f442012-11-21 14:40:21 +10001177nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001178{
Ben Skeggse225f442012-11-21 14:40:21 +10001179 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001180 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1181 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1182 int i;
1183
1184 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001185 u16 r = nv_crtc->lut.r[i] >> 2;
1186 u16 g = nv_crtc->lut.g[i] >> 2;
1187 u16 b = nv_crtc->lut.b[i] >> 2;
1188
1189 if (nv_mclass(disp->core) < NVD0_DISP_CLASS) {
1190 writew(r + 0x0000, lut + (i * 0x08) + 0);
1191 writew(g + 0x0000, lut + (i * 0x08) + 2);
1192 writew(b + 0x0000, lut + (i * 0x08) + 4);
1193 } else {
1194 writew(r + 0x6000, lut + (i * 0x20) + 0);
1195 writew(g + 0x6000, lut + (i * 0x20) + 2);
1196 writew(b + 0x6000, lut + (i * 0x20) + 4);
1197 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001198 }
1199}
1200
1201static int
Ben Skeggse225f442012-11-21 14:40:21 +10001202nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001203 uint32_t handle, uint32_t width, uint32_t height)
1204{
1205 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1206 struct drm_device *dev = crtc->dev;
1207 struct drm_gem_object *gem;
1208 struct nouveau_bo *nvbo;
1209 bool visible = (handle != 0);
1210 int i, ret = 0;
1211
1212 if (visible) {
1213 if (width != 64 || height != 64)
1214 return -EINVAL;
1215
1216 gem = drm_gem_object_lookup(dev, file_priv, handle);
1217 if (unlikely(!gem))
1218 return -ENOENT;
1219 nvbo = nouveau_gem_object(gem);
1220
1221 ret = nouveau_bo_map(nvbo);
1222 if (ret == 0) {
1223 for (i = 0; i < 64 * 64; i++) {
1224 u32 v = nouveau_bo_rd32(nvbo, i);
1225 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1226 }
1227 nouveau_bo_unmap(nvbo);
1228 }
1229
1230 drm_gem_object_unreference_unlocked(gem);
1231 }
1232
1233 if (visible != nv_crtc->cursor.visible) {
Ben Skeggse225f442012-11-21 14:40:21 +10001234 nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001235 nv_crtc->cursor.visible = visible;
1236 }
1237
1238 return ret;
1239}
1240
1241static int
Ben Skeggse225f442012-11-21 14:40:21 +10001242nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001243{
Ben Skeggse225f442012-11-21 14:40:21 +10001244 struct nv50_curs *curs = nv50_curs(crtc);
1245 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001246 nv_wo32(chan->user, 0x0084, (y << 16) | (x & 0xffff));
1247 nv_wo32(chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001248 return 0;
1249}
1250
1251static void
Ben Skeggse225f442012-11-21 14:40:21 +10001252nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001253 uint32_t start, uint32_t size)
1254{
1255 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1256 u32 end = max(start + size, (u32)256);
1257 u32 i;
1258
1259 for (i = start; i < end; i++) {
1260 nv_crtc->lut.r[i] = r[i];
1261 nv_crtc->lut.g[i] = g[i];
1262 nv_crtc->lut.b[i] = b[i];
1263 }
1264
Ben Skeggse225f442012-11-21 14:40:21 +10001265 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001266}
1267
1268static void
Ben Skeggse225f442012-11-21 14:40:21 +10001269nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001270{
1271 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001272 struct nv50_disp *disp = nv50_disp(crtc->dev);
1273 struct nv50_head *head = nv50_head(crtc);
1274 nv50_dmac_destroy(disp->core, &head->ovly.base);
1275 nv50_pioc_destroy(disp->core, &head->oimm.base);
1276 nv50_dmac_destroy(disp->core, &head->sync.base);
1277 nv50_pioc_destroy(disp->core, &head->curs.base);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001278 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001279 if (nv_crtc->cursor.nvbo)
1280 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001281 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
1282 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001283 if (nv_crtc->lut.nvbo)
1284 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001285 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
1286 drm_crtc_cleanup(crtc);
1287 kfree(crtc);
1288}
1289
Ben Skeggse225f442012-11-21 14:40:21 +10001290static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1291 .dpms = nv50_crtc_dpms,
1292 .prepare = nv50_crtc_prepare,
1293 .commit = nv50_crtc_commit,
1294 .mode_fixup = nv50_crtc_mode_fixup,
1295 .mode_set = nv50_crtc_mode_set,
1296 .mode_set_base = nv50_crtc_mode_set_base,
1297 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1298 .load_lut = nv50_crtc_lut_load,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001299};
1300
Ben Skeggse225f442012-11-21 14:40:21 +10001301static const struct drm_crtc_funcs nv50_crtc_func = {
1302 .cursor_set = nv50_crtc_cursor_set,
1303 .cursor_move = nv50_crtc_cursor_move,
1304 .gamma_set = nv50_crtc_gamma_set,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001305 .set_config = drm_crtc_helper_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001306 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001307 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001308};
1309
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001310static void
Ben Skeggse225f442012-11-21 14:40:21 +10001311nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001312{
1313}
1314
1315static void
Ben Skeggse225f442012-11-21 14:40:21 +10001316nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001317{
1318}
1319
Ben Skeggs438d99e2011-07-05 16:48:06 +10001320static int
Ben Skeggse225f442012-11-21 14:40:21 +10001321nv50_crtc_create(struct drm_device *dev, struct nouveau_object *core, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001322{
Ben Skeggse225f442012-11-21 14:40:21 +10001323 struct nv50_disp *disp = nv50_disp(dev);
1324 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001325 struct drm_crtc *crtc;
1326 int ret, i;
1327
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001328 head = kzalloc(sizeof(*head), GFP_KERNEL);
1329 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001330 return -ENOMEM;
1331
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001332 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001333 head->base.set_dither = nv50_crtc_set_dither;
1334 head->base.set_scale = nv50_crtc_set_scale;
1335 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001336 head->base.color_vibrance = 50;
1337 head->base.vibrant_hue = 0;
Ben Skeggse225f442012-11-21 14:40:21 +10001338 head->base.cursor.set_offset = nv50_cursor_set_offset;
1339 head->base.cursor.set_pos = nv50_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001340 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001341 head->base.lut.r[i] = i << 8;
1342 head->base.lut.g[i] = i << 8;
1343 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001344 }
1345
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001346 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001347 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1348 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001349 drm_mode_crtc_set_gamma_size(crtc, 256);
1350
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001351 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001352 0, 0x0000, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001353 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001354 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001355 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001356 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001357 if (ret)
1358 nouveau_bo_unpin(head->base.lut.nvbo);
1359 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001360 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001361 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001362 }
1363
1364 if (ret)
1365 goto out;
1366
Ben Skeggse225f442012-11-21 14:40:21 +10001367 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001368
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001369 /* allocate cursor resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001370 ret = nv50_pioc_create(disp->core, NV50_DISP_CURS_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001371 &(struct nv50_display_curs_class) {
1372 .head = index,
1373 }, sizeof(struct nv50_display_curs_class),
1374 &head->curs.base);
1375 if (ret)
1376 goto out;
1377
1378 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
1379 0, 0x0000, NULL, &head->base.cursor.nvbo);
1380 if (!ret) {
1381 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001382 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001383 ret = nouveau_bo_map(head->base.cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001384 if (ret)
1385 nouveau_bo_unpin(head->base.lut.nvbo);
1386 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001387 if (ret)
1388 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
1389 }
1390
1391 if (ret)
1392 goto out;
1393
1394 /* allocate page flip / sync resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001395 ret = nv50_dmac_create(disp->core, NV50_DISP_SYNC_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001396 &(struct nv50_display_sync_class) {
1397 .pushbuf = EVO_PUSH_HANDLE(SYNC, index),
1398 .head = index,
1399 }, sizeof(struct nv50_display_sync_class),
1400 disp->sync->bo.offset, &head->sync.base);
1401 if (ret)
1402 goto out;
1403
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10001404 head->sync.addr = EVO_FLIP_SEM0(index);
1405 head->sync.data = 0x00000000;
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001406
1407 /* allocate overlay resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001408 ret = nv50_pioc_create(disp->core, NV50_DISP_OIMM_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001409 &(struct nv50_display_oimm_class) {
1410 .head = index,
1411 }, sizeof(struct nv50_display_oimm_class),
1412 &head->oimm.base);
1413 if (ret)
1414 goto out;
1415
Ben Skeggse225f442012-11-21 14:40:21 +10001416 ret = nv50_dmac_create(disp->core, NV50_DISP_OVLY_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001417 &(struct nv50_display_ovly_class) {
1418 .pushbuf = EVO_PUSH_HANDLE(OVLY, index),
1419 .head = index,
1420 }, sizeof(struct nv50_display_ovly_class),
1421 disp->sync->bo.offset, &head->ovly.base);
1422 if (ret)
1423 goto out;
1424
Ben Skeggs438d99e2011-07-05 16:48:06 +10001425out:
1426 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001427 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001428 return ret;
1429}
1430
1431/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001432 * DAC
1433 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001434static void
Ben Skeggse225f442012-11-21 14:40:21 +10001435nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001436{
1437 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001438 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001439 int or = nv_encoder->or;
1440 u32 dpms_ctrl;
1441
Ben Skeggs35b21d32012-11-08 12:08:55 +10001442 dpms_ctrl = 0x00000000;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001443 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
1444 dpms_ctrl |= 0x00000001;
1445 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
1446 dpms_ctrl |= 0x00000004;
1447
Ben Skeggs35b21d32012-11-08 12:08:55 +10001448 nv_call(disp->core, NV50_DISP_DAC_PWR + or, dpms_ctrl);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001449}
1450
1451static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001452nv50_dac_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001453 const struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001454 struct drm_display_mode *adjusted_mode)
1455{
1456 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1457 struct nouveau_connector *nv_connector;
1458
1459 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1460 if (nv_connector && nv_connector->native_mode) {
1461 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1462 int id = adjusted_mode->base.id;
1463 *adjusted_mode = *nv_connector->native_mode;
1464 adjusted_mode->base.id = id;
1465 }
1466 }
1467
1468 return true;
1469}
1470
1471static void
Ben Skeggse225f442012-11-21 14:40:21 +10001472nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001473{
1474}
1475
1476static void
Ben Skeggse225f442012-11-21 14:40:21 +10001477nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001478 struct drm_display_mode *adjusted_mode)
1479{
Ben Skeggse225f442012-11-21 14:40:21 +10001480 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001481 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1482 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001483 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001484
Ben Skeggse225f442012-11-21 14:40:21 +10001485 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001486
Ben Skeggs97b19b52012-11-16 11:21:37 +10001487 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001488 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001489 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001490 u32 syncs = 0x00000000;
1491
1492 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1493 syncs |= 0x00000001;
1494 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1495 syncs |= 0x00000002;
1496
1497 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1498 evo_data(push, 1 << nv_crtc->index);
1499 evo_data(push, syncs);
1500 } else {
1501 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1502 u32 syncs = 0x00000001;
1503
1504 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1505 syncs |= 0x00000008;
1506 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1507 syncs |= 0x00000010;
1508
1509 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1510 magic |= 0x00000001;
1511
1512 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1513 evo_data(push, syncs);
1514 evo_data(push, magic);
1515 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1516 evo_data(push, 1 << nv_crtc->index);
1517 }
1518
1519 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001520 }
1521
1522 nv_encoder->crtc = encoder->crtc;
1523}
1524
1525static void
Ben Skeggse225f442012-11-21 14:40:21 +10001526nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001527{
1528 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001529 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001530 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001531 u32 *push;
1532
1533 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001534 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001535
Ben Skeggs97b19b52012-11-16 11:21:37 +10001536 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001537 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001538 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001539 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1540 evo_data(push, 0x00000000);
1541 } else {
1542 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1543 evo_data(push, 0x00000000);
1544 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001545 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001546 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001547 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001548
1549 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001550}
1551
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001552static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001553nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001554{
Ben Skeggse225f442012-11-21 14:40:21 +10001555 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs35b21d32012-11-08 12:08:55 +10001556 int ret, or = nouveau_encoder(encoder)->or;
Ben Skeggs7ebb38b2012-11-09 09:38:06 +10001557 u32 load = 0;
Ben Skeggsb6819932011-07-08 11:14:50 +10001558
Ben Skeggs35b21d32012-11-08 12:08:55 +10001559 ret = nv_exec(disp->core, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
1560 if (ret || load != 7)
1561 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001562
Ben Skeggs35b21d32012-11-08 12:08:55 +10001563 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001564}
1565
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001566static void
Ben Skeggse225f442012-11-21 14:40:21 +10001567nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001568{
1569 drm_encoder_cleanup(encoder);
1570 kfree(encoder);
1571}
1572
Ben Skeggse225f442012-11-21 14:40:21 +10001573static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1574 .dpms = nv50_dac_dpms,
1575 .mode_fixup = nv50_dac_mode_fixup,
1576 .prepare = nv50_dac_disconnect,
1577 .commit = nv50_dac_commit,
1578 .mode_set = nv50_dac_mode_set,
1579 .disable = nv50_dac_disconnect,
1580 .get_crtc = nv50_display_crtc_get,
1581 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001582};
1583
Ben Skeggse225f442012-11-21 14:40:21 +10001584static const struct drm_encoder_funcs nv50_dac_func = {
1585 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001586};
1587
1588static int
Ben Skeggse225f442012-11-21 14:40:21 +10001589nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001590{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001591 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1592 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001593 struct nouveau_encoder *nv_encoder;
1594 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001595 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001596
1597 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1598 if (!nv_encoder)
1599 return -ENOMEM;
1600 nv_encoder->dcb = dcbe;
1601 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001602 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001603
1604 encoder = to_drm_encoder(nv_encoder);
1605 encoder->possible_crtcs = dcbe->heads;
1606 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001607 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001608 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001609
1610 drm_mode_connector_attach_encoder(connector, encoder);
1611 return 0;
1612}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001613
1614/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001615 * Audio
1616 *****************************************************************************/
1617static void
Ben Skeggse225f442012-11-21 14:40:21 +10001618nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001619{
1620 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1621 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001622 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001623
1624 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1625 if (!drm_detect_monitor_audio(nv_connector->edid))
1626 return;
1627
Ben Skeggs78951d22011-11-11 18:13:13 +10001628 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs78951d22011-11-11 18:13:13 +10001629
Ben Skeggs0a9e2b952012-11-08 14:03:56 +10001630 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or,
1631 nv_connector->base.eld,
1632 nv_connector->base.eld[2] * 4);
Ben Skeggs78951d22011-11-11 18:13:13 +10001633}
1634
1635static void
Ben Skeggse225f442012-11-21 14:40:21 +10001636nv50_audio_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001637{
1638 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001639 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001640
Ben Skeggs0a9e2b952012-11-08 14:03:56 +10001641 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or, NULL, 0);
Ben Skeggs78951d22011-11-11 18:13:13 +10001642}
1643
1644/******************************************************************************
1645 * HDMI
1646 *****************************************************************************/
1647static void
Ben Skeggse225f442012-11-21 14:40:21 +10001648nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001649{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001650 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1651 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1652 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001653 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001654 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001655 u32 rekey = 56; /* binary driver, and tegra constant */
1656 u32 max_ac_packet;
1657
1658 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1659 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1660 return;
1661
1662 max_ac_packet = mode->htotal - mode->hdisplay;
1663 max_ac_packet -= rekey;
1664 max_ac_packet -= 18; /* constant from tegra */
1665 max_ac_packet /= 32;
1666
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001667 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff,
1668 NV84_DISP_SOR_HDMI_PWR_STATE_ON |
1669 (max_ac_packet << 16) | rekey);
Ben Skeggs091e40c2011-11-11 20:46:00 +10001670
Ben Skeggse225f442012-11-21 14:40:21 +10001671 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001672}
1673
1674static void
Ben Skeggse225f442012-11-21 14:40:21 +10001675nv50_hdmi_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001676{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001677 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1678 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
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
Ben Skeggse225f442012-11-21 14:40:21 +10001682 nv50_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001683
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001684 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff, 0x00000000);
Ben Skeggs78951d22011-11-11 18:13:13 +10001685}
1686
1687/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001688 * SOR
1689 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001690static void
Ben Skeggse225f442012-11-21 14:40:21 +10001691nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001692{
1693 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1694 struct drm_device *dev = encoder->dev;
Ben Skeggse225f442012-11-21 14:40:21 +10001695 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001696 struct drm_encoder *partner;
1697 int or = nv_encoder->or;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001698
1699 nv_encoder->last_dpms = mode;
1700
1701 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1702 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1703
1704 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1705 continue;
1706
1707 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001708 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001709 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1710 return;
1711 break;
1712 }
1713 }
1714
Ben Skeggs74b66852012-11-08 12:01:39 +10001715 nv_call(disp->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
Ben Skeggs83fc0832011-07-05 13:08:40 +10001716}
1717
1718static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001719nv50_sor_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001720 const struct drm_display_mode *mode,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001721 struct drm_display_mode *adjusted_mode)
1722{
1723 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1724 struct nouveau_connector *nv_connector;
1725
1726 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1727 if (nv_connector && nv_connector->native_mode) {
1728 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1729 int id = adjusted_mode->base.id;
1730 *adjusted_mode = *nv_connector->native_mode;
1731 adjusted_mode->base.id = id;
1732 }
1733 }
1734
1735 return true;
1736}
1737
1738static void
Ben Skeggse225f442012-11-21 14:40:21 +10001739nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001740{
1741 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001742 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001743 const int or = nv_encoder->or;
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001744 u32 *push;
1745
1746 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001747 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001748
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001749 push = evo_wait(mast, 4);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001750 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001751 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001752 evo_mthd(push, 0x0600 + (or * 0x40), 1);
1753 evo_data(push, 0x00000000);
1754 } else {
1755 evo_mthd(push, 0x0200 + (or * 0x20), 1);
1756 evo_data(push, 0x00000000);
1757 }
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001758 evo_kick(push, mast);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001759 }
1760
Ben Skeggse225f442012-11-21 14:40:21 +10001761 nv50_hdmi_disconnect(encoder);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001762 }
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001763
1764 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1765 nv_encoder->crtc = NULL;
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001766}
1767
1768static void
Ben Skeggse225f442012-11-21 14:40:21 +10001769nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001770{
1771}
1772
1773static void
Ben Skeggse225f442012-11-21 14:40:21 +10001774nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001775 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001776{
Ben Skeggse225f442012-11-21 14:40:21 +10001777 struct nv50_disp *disp = nv50_disp(encoder->dev);
1778 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001779 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001780 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001781 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1782 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001783 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001784 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001785 u32 *push, lvds = 0;
1786 u8 owner = 1 << nv_crtc->index;
1787 u8 proto = 0xf;
1788 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001789
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001790 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1791 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001792 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001793 if (nv_encoder->dcb->sorconf.link & 1) {
1794 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001795 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001796 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001797 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001798 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001799 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001800 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001801
Ben Skeggse225f442012-11-21 14:40:21 +10001802 nv50_hdmi_mode_set(encoder, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001803 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001804 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001805 proto = 0x0;
1806
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001807 if (bios->fp_no_ddc) {
1808 if (bios->fp.dual_link)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001809 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001810 if (bios->fp.if_is_24bit)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001811 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001812 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001813 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001814 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001815 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001816 } else
1817 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001818 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001819 }
1820
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001821 if (lvds & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001822 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001823 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001824 } else {
1825 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001826 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001827 }
1828
1829 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001830 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001831 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001832
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001833 nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + nv_encoder->or, lvds);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001834 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001835 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001836 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001837 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001838 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001839 } else
1840 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001841 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001842 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001843 } else {
1844 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1845 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001846 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001847
1848 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001849 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001850 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001851 proto = 0x9;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001852 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001853 default:
1854 BUG_ON(1);
1855 break;
1856 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001857
Ben Skeggse225f442012-11-21 14:40:21 +10001858 nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001859
Ben Skeggse225f442012-11-21 14:40:21 +10001860 push = evo_wait(nv50_mast(dev), 8);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001861 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001862 if (nv50_vers(mast) < NVD0_DISP_CLASS) {
Ben Skeggse2de1792013-02-11 13:56:31 +10001863 u32 ctrl = (depth << 16) | (proto << 8) | owner;
1864 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1865 ctrl |= 0x00001000;
1866 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1867 ctrl |= 0x00002000;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001868 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x040), 1);
Ben Skeggse2de1792013-02-11 13:56:31 +10001869 evo_data(push, ctrl);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001870 } else {
1871 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1872 u32 syncs = 0x00000001;
1873
1874 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1875 syncs |= 0x00000008;
1876 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1877 syncs |= 0x00000010;
1878
1879 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1880 magic |= 0x00000001;
1881
1882 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1883 evo_data(push, syncs | (depth << 6));
1884 evo_data(push, magic);
1885 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 1);
1886 evo_data(push, owner | (proto << 8));
1887 }
1888
1889 evo_kick(push, mast);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001890 }
1891
1892 nv_encoder->crtc = encoder->crtc;
1893}
1894
1895static void
Ben Skeggse225f442012-11-21 14:40:21 +10001896nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001897{
1898 drm_encoder_cleanup(encoder);
1899 kfree(encoder);
1900}
1901
Ben Skeggse225f442012-11-21 14:40:21 +10001902static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
1903 .dpms = nv50_sor_dpms,
1904 .mode_fixup = nv50_sor_mode_fixup,
Ben Skeggs5a885f02013-02-20 14:34:18 +10001905 .prepare = nv50_sor_disconnect,
Ben Skeggse225f442012-11-21 14:40:21 +10001906 .commit = nv50_sor_commit,
1907 .mode_set = nv50_sor_mode_set,
1908 .disable = nv50_sor_disconnect,
1909 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001910};
1911
Ben Skeggse225f442012-11-21 14:40:21 +10001912static const struct drm_encoder_funcs nv50_sor_func = {
1913 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001914};
1915
1916static int
Ben Skeggse225f442012-11-21 14:40:21 +10001917nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001918{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001919 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1920 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001921 struct nouveau_encoder *nv_encoder;
1922 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001923 int type;
1924
1925 switch (dcbe->type) {
1926 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1927 case DCB_OUTPUT_TMDS:
1928 case DCB_OUTPUT_DP:
1929 default:
1930 type = DRM_MODE_ENCODER_TMDS;
1931 break;
1932 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001933
1934 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1935 if (!nv_encoder)
1936 return -ENOMEM;
1937 nv_encoder->dcb = dcbe;
1938 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001939 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001940 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1941
1942 encoder = to_drm_encoder(nv_encoder);
1943 encoder->possible_crtcs = dcbe->heads;
1944 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001945 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001946 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001947
1948 drm_mode_connector_attach_encoder(connector, encoder);
1949 return 0;
1950}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001951
1952/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10001953 * PIOR
1954 *****************************************************************************/
1955
1956static void
1957nv50_pior_dpms(struct drm_encoder *encoder, int mode)
1958{
1959 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1960 struct nv50_disp *disp = nv50_disp(encoder->dev);
1961 u32 mthd = (nv_encoder->dcb->type << 12) | nv_encoder->or;
1962 u32 ctrl = (mode == DRM_MODE_DPMS_ON);
1963 nv_call(disp->core, NV50_DISP_PIOR_PWR + mthd, ctrl);
1964}
1965
1966static bool
1967nv50_pior_mode_fixup(struct drm_encoder *encoder,
1968 const struct drm_display_mode *mode,
1969 struct drm_display_mode *adjusted_mode)
1970{
1971 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1972 struct nouveau_connector *nv_connector;
1973
1974 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1975 if (nv_connector && nv_connector->native_mode) {
1976 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1977 int id = adjusted_mode->base.id;
1978 *adjusted_mode = *nv_connector->native_mode;
1979 adjusted_mode->base.id = id;
1980 }
1981 }
1982
1983 adjusted_mode->clock *= 2;
1984 return true;
1985}
1986
1987static void
1988nv50_pior_commit(struct drm_encoder *encoder)
1989{
1990}
1991
1992static void
1993nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
1994 struct drm_display_mode *adjusted_mode)
1995{
1996 struct nv50_mast *mast = nv50_mast(encoder->dev);
1997 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1998 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1999 struct nouveau_connector *nv_connector;
2000 u8 owner = 1 << nv_crtc->index;
2001 u8 proto, depth;
2002 u32 *push;
2003
2004 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2005 switch (nv_connector->base.display_info.bpc) {
2006 case 10: depth = 0x6; break;
2007 case 8: depth = 0x5; break;
2008 case 6: depth = 0x2; break;
2009 default: depth = 0x0; break;
2010 }
2011
2012 switch (nv_encoder->dcb->type) {
2013 case DCB_OUTPUT_TMDS:
2014 case DCB_OUTPUT_DP:
2015 proto = 0x0;
2016 break;
2017 default:
2018 BUG_ON(1);
2019 break;
2020 }
2021
2022 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2023
2024 push = evo_wait(mast, 8);
2025 if (push) {
2026 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
2027 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2028 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2029 ctrl |= 0x00001000;
2030 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2031 ctrl |= 0x00002000;
2032 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2033 evo_data(push, ctrl);
2034 }
2035
2036 evo_kick(push, mast);
2037 }
2038
2039 nv_encoder->crtc = encoder->crtc;
2040}
2041
2042static void
2043nv50_pior_disconnect(struct drm_encoder *encoder)
2044{
2045 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2046 struct nv50_mast *mast = nv50_mast(encoder->dev);
2047 const int or = nv_encoder->or;
2048 u32 *push;
2049
2050 if (nv_encoder->crtc) {
2051 nv50_crtc_prepare(nv_encoder->crtc);
2052
2053 push = evo_wait(mast, 4);
2054 if (push) {
2055 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
2056 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2057 evo_data(push, 0x00000000);
2058 }
Ben Skeggseb6313a2013-02-11 09:52:58 +10002059 evo_kick(push, mast);
2060 }
2061 }
2062
2063 nv_encoder->crtc = NULL;
2064}
2065
2066static void
2067nv50_pior_destroy(struct drm_encoder *encoder)
2068{
2069 drm_encoder_cleanup(encoder);
2070 kfree(encoder);
2071}
2072
2073static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2074 .dpms = nv50_pior_dpms,
2075 .mode_fixup = nv50_pior_mode_fixup,
2076 .prepare = nv50_pior_disconnect,
2077 .commit = nv50_pior_commit,
2078 .mode_set = nv50_pior_mode_set,
2079 .disable = nv50_pior_disconnect,
2080 .get_crtc = nv50_display_crtc_get,
2081};
2082
2083static const struct drm_encoder_funcs nv50_pior_func = {
2084 .destroy = nv50_pior_destroy,
2085};
2086
2087static int
2088nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2089{
2090 struct nouveau_drm *drm = nouveau_drm(connector->dev);
2091 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
2092 struct nouveau_i2c_port *ddc = NULL;
2093 struct nouveau_encoder *nv_encoder;
2094 struct drm_encoder *encoder;
2095 int type;
2096
2097 switch (dcbe->type) {
2098 case DCB_OUTPUT_TMDS:
2099 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
2100 type = DRM_MODE_ENCODER_TMDS;
2101 break;
2102 case DCB_OUTPUT_DP:
2103 ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
2104 type = DRM_MODE_ENCODER_TMDS;
2105 break;
2106 default:
2107 return -ENODEV;
2108 }
2109
2110 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2111 if (!nv_encoder)
2112 return -ENOMEM;
2113 nv_encoder->dcb = dcbe;
2114 nv_encoder->or = ffs(dcbe->or) - 1;
2115 nv_encoder->i2c = ddc;
2116
2117 encoder = to_drm_encoder(nv_encoder);
2118 encoder->possible_crtcs = dcbe->heads;
2119 encoder->possible_clones = 0;
2120 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
2121 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2122
2123 drm_mode_connector_attach_encoder(connector, encoder);
2124 return 0;
2125}
2126
2127/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002128 * Init
2129 *****************************************************************************/
Ben Skeggs2a44e492011-11-09 11:36:33 +10002130void
Ben Skeggse225f442012-11-21 14:40:21 +10002131nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002132{
Ben Skeggs26f6d882011-07-04 16:25:18 +10002133}
2134
2135int
Ben Skeggse225f442012-11-21 14:40:21 +10002136nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002137{
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002138 struct nv50_disp *disp = nv50_disp(dev);
2139 struct drm_crtc *crtc;
2140 u32 *push;
2141
2142 push = evo_wait(nv50_mast(dev), 32);
2143 if (!push)
2144 return -EBUSY;
2145
2146 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2147 struct nv50_sync *sync = nv50_sync(crtc);
2148 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002149 }
2150
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002151 evo_mthd(push, 0x0088, 1);
2152 evo_data(push, NvEvoSync);
2153 evo_kick(push, nv50_mast(dev));
2154 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002155}
2156
2157void
Ben Skeggse225f442012-11-21 14:40:21 +10002158nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002159{
Ben Skeggse225f442012-11-21 14:40:21 +10002160 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002161
Ben Skeggse225f442012-11-21 14:40:21 +10002162 nv50_dmac_destroy(disp->core, &disp->mast.base);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002163
Ben Skeggs816af2f2011-11-16 15:48:48 +10002164 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002165 if (disp->sync)
2166 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002167 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002168
Ben Skeggs77145f12012-07-31 16:16:21 +10002169 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002170 kfree(disp);
2171}
2172
2173int
Ben Skeggse225f442012-11-21 14:40:21 +10002174nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002175{
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002176 static const u16 oclass[] = {
2177 NVE0_DISP_CLASS,
2178 NVD0_DISP_CLASS,
Ben Skeggs63718a02012-11-16 11:44:14 +10002179 NVA3_DISP_CLASS,
2180 NV94_DISP_CLASS,
2181 NVA0_DISP_CLASS,
2182 NV84_DISP_CLASS,
2183 NV50_DISP_CLASS,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002184 };
Ben Skeggs77145f12012-07-31 16:16:21 +10002185 struct nouveau_device *device = nouveau_dev(dev);
2186 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002187 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002188 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002189 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002190 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002191 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002192
2193 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2194 if (!disp)
2195 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10002196
2197 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002198 nouveau_display(dev)->dtor = nv50_display_destroy;
2199 nouveau_display(dev)->init = nv50_display_init;
2200 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002201
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002202 /* small shared memory area we use for notifiers and semaphores */
2203 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2204 0, 0x0000, NULL, &disp->sync);
2205 if (!ret) {
2206 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002207 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002208 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002209 if (ret)
2210 nouveau_bo_unpin(disp->sync);
2211 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002212 if (ret)
2213 nouveau_bo_ref(NULL, &disp->sync);
2214 }
2215
2216 if (ret)
2217 goto out;
2218
2219 /* attempt to allocate a supported evo display class */
2220 ret = -ENODEV;
2221 for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
2222 ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
2223 0xd1500000, oclass[i], NULL, 0,
2224 &disp->core);
2225 }
2226
2227 if (ret)
2228 goto out;
2229
2230 /* allocate master evo channel */
Ben Skeggse225f442012-11-21 14:40:21 +10002231 ret = nv50_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002232 &(struct nv50_display_mast_class) {
2233 .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
2234 }, sizeof(struct nv50_display_mast_class),
2235 disp->sync->bo.offset, &disp->mast.base);
2236 if (ret)
2237 goto out;
2238
Ben Skeggs438d99e2011-07-05 16:48:06 +10002239 /* create crtc objects to represent the hw heads */
Ben Skeggs63718a02012-11-16 11:44:14 +10002240 if (nv_mclass(disp->core) >= NVD0_DISP_CLASS)
2241 crtcs = nv_rd32(device, 0x022448);
2242 else
2243 crtcs = 2;
2244
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002245 for (i = 0; i < crtcs; i++) {
Ben Skeggse225f442012-11-21 14:40:21 +10002246 ret = nv50_crtc_create(dev, disp->core, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002247 if (ret)
2248 goto out;
2249 }
2250
Ben Skeggs83fc0832011-07-05 13:08:40 +10002251 /* create encoder/connector objects based on VBIOS DCB table */
2252 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2253 connector = nouveau_connector_create(dev, dcbe->connector);
2254 if (IS_ERR(connector))
2255 continue;
2256
Ben Skeggseb6313a2013-02-11 09:52:58 +10002257 if (dcbe->location == DCB_LOC_ON_CHIP) {
2258 switch (dcbe->type) {
2259 case DCB_OUTPUT_TMDS:
2260 case DCB_OUTPUT_LVDS:
2261 case DCB_OUTPUT_DP:
2262 ret = nv50_sor_create(connector, dcbe);
2263 break;
2264 case DCB_OUTPUT_ANALOG:
2265 ret = nv50_dac_create(connector, dcbe);
2266 break;
2267 default:
2268 ret = -ENODEV;
2269 break;
2270 }
2271 } else {
2272 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002273 }
2274
Ben Skeggseb6313a2013-02-11 09:52:58 +10002275 if (ret) {
2276 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2277 dcbe->location, dcbe->type,
2278 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002279 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002280 }
2281 }
2282
2283 /* cull any connectors we created that don't have an encoder */
2284 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2285 if (connector->encoder_ids[0])
2286 continue;
2287
Ben Skeggs77145f12012-07-31 16:16:21 +10002288 NV_WARN(drm, "%s has no encoders, removing\n",
Ben Skeggs83fc0832011-07-05 13:08:40 +10002289 drm_get_connector_name(connector));
2290 connector->funcs->destroy(connector);
2291 }
2292
Ben Skeggs26f6d882011-07-04 16:25:18 +10002293out:
2294 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002295 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002296 return ret;
2297}