blob: 2cd37979dcd3f67c76a7b1b120e4ba33ef20c79a [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))
58#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
59#define EVO_FLIP_SEM0(c) EVO_SYNC((c), 0x00)
60#define EVO_FLIP_SEM1(c) EVO_SYNC((c), 0x10)
61
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;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000132};
133
134static void
Ben Skeggse225f442012-11-21 14:40:21 +1000135nv50_dmac_destroy(struct nouveau_object *core, struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000136{
137 if (dmac->ptr) {
138 struct pci_dev *pdev = nv_device(core)->pdev;
139 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
140 }
141
Ben Skeggse225f442012-11-21 14:40:21 +1000142 nv50_chan_destroy(core, &dmac->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000143}
144
145static int
Ben Skeggs47057302012-11-16 13:58:48 +1000146nv50_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
147{
148 struct nouveau_fb *pfb = nouveau_fb(core);
149 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
150 struct nouveau_object *object;
151 int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
152 NV_DMA_IN_MEMORY_CLASS,
153 &(struct nv_dma_class) {
154 .flags = NV_DMA_TARGET_VRAM |
155 NV_DMA_ACCESS_RDWR,
156 .start = 0,
157 .limit = pfb->ram.size - 1,
158 .conf0 = NV50_DMA_CONF0_ENABLE |
159 NV50_DMA_CONF0_PART_256,
160 }, sizeof(struct nv_dma_class), &object);
161 if (ret)
162 return ret;
163
164 ret = nouveau_object_new(client, parent, NvEvoFB16,
165 NV_DMA_IN_MEMORY_CLASS,
166 &(struct nv_dma_class) {
167 .flags = NV_DMA_TARGET_VRAM |
168 NV_DMA_ACCESS_RDWR,
169 .start = 0,
170 .limit = pfb->ram.size - 1,
171 .conf0 = NV50_DMA_CONF0_ENABLE | 0x70 |
172 NV50_DMA_CONF0_PART_256,
173 }, sizeof(struct nv_dma_class), &object);
174 if (ret)
175 return ret;
176
177 ret = nouveau_object_new(client, parent, NvEvoFB32,
178 NV_DMA_IN_MEMORY_CLASS,
179 &(struct nv_dma_class) {
180 .flags = NV_DMA_TARGET_VRAM |
181 NV_DMA_ACCESS_RDWR,
182 .start = 0,
183 .limit = pfb->ram.size - 1,
184 .conf0 = NV50_DMA_CONF0_ENABLE | 0x7a |
185 NV50_DMA_CONF0_PART_256,
186 }, sizeof(struct nv_dma_class), &object);
187 return ret;
188}
189
190static int
191nvc0_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
192{
193 struct nouveau_fb *pfb = nouveau_fb(core);
194 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
195 struct nouveau_object *object;
196 int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
197 NV_DMA_IN_MEMORY_CLASS,
198 &(struct nv_dma_class) {
199 .flags = NV_DMA_TARGET_VRAM |
200 NV_DMA_ACCESS_RDWR,
201 .start = 0,
202 .limit = pfb->ram.size - 1,
203 .conf0 = NVC0_DMA_CONF0_ENABLE,
204 }, sizeof(struct nv_dma_class), &object);
205 if (ret)
206 return ret;
207
208 ret = nouveau_object_new(client, parent, NvEvoFB16,
209 NV_DMA_IN_MEMORY_CLASS,
210 &(struct nv_dma_class) {
211 .flags = NV_DMA_TARGET_VRAM |
212 NV_DMA_ACCESS_RDWR,
213 .start = 0,
214 .limit = pfb->ram.size - 1,
215 .conf0 = NVC0_DMA_CONF0_ENABLE | 0xfe,
216 }, sizeof(struct nv_dma_class), &object);
217 if (ret)
218 return ret;
219
220 ret = nouveau_object_new(client, parent, NvEvoFB32,
221 NV_DMA_IN_MEMORY_CLASS,
222 &(struct nv_dma_class) {
223 .flags = NV_DMA_TARGET_VRAM |
224 NV_DMA_ACCESS_RDWR,
225 .start = 0,
226 .limit = pfb->ram.size - 1,
227 .conf0 = NVC0_DMA_CONF0_ENABLE | 0xfe,
228 }, sizeof(struct nv_dma_class), &object);
229 return ret;
230}
231
232static int
233nvd0_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
234{
235 struct nouveau_fb *pfb = nouveau_fb(core);
236 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
237 struct nouveau_object *object;
238 int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
239 NV_DMA_IN_MEMORY_CLASS,
240 &(struct nv_dma_class) {
241 .flags = NV_DMA_TARGET_VRAM |
242 NV_DMA_ACCESS_RDWR,
243 .start = 0,
244 .limit = pfb->ram.size - 1,
245 .conf0 = NVD0_DMA_CONF0_ENABLE |
246 NVD0_DMA_CONF0_PAGE_LP,
247 }, sizeof(struct nv_dma_class), &object);
248 if (ret)
249 return ret;
250
251 ret = nouveau_object_new(client, parent, NvEvoFB32,
252 NV_DMA_IN_MEMORY_CLASS,
253 &(struct nv_dma_class) {
254 .flags = NV_DMA_TARGET_VRAM |
255 NV_DMA_ACCESS_RDWR,
256 .start = 0,
257 .limit = pfb->ram.size - 1,
258 .conf0 = NVD0_DMA_CONF0_ENABLE | 0xfe |
259 NVD0_DMA_CONF0_PAGE_LP,
260 }, sizeof(struct nv_dma_class), &object);
261 return ret;
262}
263
264static int
Ben Skeggse225f442012-11-21 14:40:21 +1000265nv50_dmac_create(struct nouveau_object *core, u32 bclass, u8 head,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000266 void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000267 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000268{
269 struct nouveau_fb *pfb = nouveau_fb(core);
270 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
271 struct nouveau_object *object;
272 u32 pushbuf = *(u32 *)data;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000273 int ret;
274
Ben Skeggs47057302012-11-16 13:58:48 +1000275 dmac->ptr = pci_alloc_consistent(nv_device(core)->pdev, PAGE_SIZE,
276 &dmac->handle);
277 if (!dmac->ptr)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000278 return -ENOMEM;
279
280 ret = nouveau_object_new(client, NVDRM_DEVICE, pushbuf,
281 NV_DMA_FROM_MEMORY_CLASS,
282 &(struct nv_dma_class) {
283 .flags = NV_DMA_TARGET_PCI_US |
284 NV_DMA_ACCESS_RD,
Ben Skeggs47057302012-11-16 13:58:48 +1000285 .start = dmac->handle + 0x0000,
286 .limit = dmac->handle + 0x0fff,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000287 }, sizeof(struct nv_dma_class), &object);
288 if (ret)
289 return ret;
290
Ben Skeggse225f442012-11-21 14:40:21 +1000291 ret = nv50_chan_create(core, bclass, head, data, size, &dmac->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000292 if (ret)
293 return ret;
294
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000295 ret = nouveau_object_new(client, dmac->base.handle, NvEvoSync,
296 NV_DMA_IN_MEMORY_CLASS,
297 &(struct nv_dma_class) {
298 .flags = NV_DMA_TARGET_VRAM |
299 NV_DMA_ACCESS_RDWR,
300 .start = syncbuf + 0x0000,
301 .limit = syncbuf + 0x0fff,
302 }, sizeof(struct nv_dma_class), &object);
303 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000304 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000305
306 ret = nouveau_object_new(client, dmac->base.handle, NvEvoVRAM,
307 NV_DMA_IN_MEMORY_CLASS,
308 &(struct nv_dma_class) {
309 .flags = NV_DMA_TARGET_VRAM |
310 NV_DMA_ACCESS_RDWR,
311 .start = 0,
312 .limit = pfb->ram.size - 1,
313 }, sizeof(struct nv_dma_class), &object);
314 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000315 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000316
Ben Skeggs47057302012-11-16 13:58:48 +1000317 if (nv_device(core)->card_type < NV_C0)
318 ret = nv50_dmac_create_fbdma(core, dmac->base.handle);
319 else
320 if (nv_device(core)->card_type < NV_D0)
321 ret = nvc0_dmac_create_fbdma(core, dmac->base.handle);
322 else
323 ret = nvd0_dmac_create_fbdma(core, dmac->base.handle);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000324 return ret;
325}
326
Ben Skeggse225f442012-11-21 14:40:21 +1000327struct nv50_mast {
328 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000329};
330
Ben Skeggse225f442012-11-21 14:40:21 +1000331struct nv50_curs {
332 struct nv50_pioc base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000333};
334
Ben Skeggse225f442012-11-21 14:40:21 +1000335struct nv50_sync {
336 struct nv50_dmac base;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000337 struct {
Ben Skeggs3376ee32011-11-12 14:28:12 +1000338 u32 offset;
339 u16 value;
340 } sem;
341};
342
Ben Skeggse225f442012-11-21 14:40:21 +1000343struct nv50_ovly {
344 struct nv50_dmac base;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000345};
Ben Skeggsf20ce962011-07-08 13:17:01 +1000346
Ben Skeggse225f442012-11-21 14:40:21 +1000347struct nv50_oimm {
348 struct nv50_pioc base;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000349};
350
Ben Skeggse225f442012-11-21 14:40:21 +1000351struct nv50_head {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000352 struct nouveau_crtc base;
Ben Skeggse225f442012-11-21 14:40:21 +1000353 struct nv50_curs curs;
354 struct nv50_sync sync;
355 struct nv50_ovly ovly;
356 struct nv50_oimm oimm;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000357};
358
Ben Skeggse225f442012-11-21 14:40:21 +1000359#define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
360#define nv50_curs(c) (&nv50_head(c)->curs)
361#define nv50_sync(c) (&nv50_head(c)->sync)
362#define nv50_ovly(c) (&nv50_head(c)->ovly)
363#define nv50_oimm(c) (&nv50_head(c)->oimm)
364#define nv50_chan(c) (&(c)->base.base)
365#define nv50_vers(c) nv_mclass(nv50_chan(c)->user)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000366
Ben Skeggse225f442012-11-21 14:40:21 +1000367struct nv50_disp {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000368 struct nouveau_object *core;
Ben Skeggse225f442012-11-21 14:40:21 +1000369 struct nv50_mast mast;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000370
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000371 u32 modeset;
372
373 struct nouveau_bo *sync;
Ben Skeggsdd0e3d52012-10-16 14:00:31 +1000374};
375
Ben Skeggse225f442012-11-21 14:40:21 +1000376static struct nv50_disp *
377nv50_disp(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +1000378{
Ben Skeggs77145f12012-07-31 16:16:21 +1000379 return nouveau_display(dev)->priv;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000380}
381
Ben Skeggse225f442012-11-21 14:40:21 +1000382#define nv50_mast(d) (&nv50_disp(d)->mast)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000383
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000384static struct drm_crtc *
Ben Skeggse225f442012-11-21 14:40:21 +1000385nv50_display_crtc_get(struct drm_encoder *encoder)
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000386{
387 return nouveau_encoder(encoder)->crtc;
388}
389
390/******************************************************************************
391 * EVO channel helpers
392 *****************************************************************************/
Ben Skeggs51beb422011-07-05 10:33:08 +1000393static u32 *
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000394evo_wait(void *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000395{
Ben Skeggse225f442012-11-21 14:40:21 +1000396 struct nv50_dmac *dmac = evoc;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000397 u32 put = nv_ro32(dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000398
Ben Skeggsde8268c2012-11-16 10:24:31 +1000399 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000400 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000401
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000402 nv_wo32(dmac->base.user, 0x0000, 0x00000000);
403 if (!nv_wait(dmac->base.user, 0x0004, ~0, 0x00000000)) {
404 NV_ERROR(dmac->base.user, "channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000405 return NULL;
406 }
407
408 put = 0;
409 }
410
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000411 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000412}
413
414static void
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000415evo_kick(u32 *push, void *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000416{
Ben Skeggse225f442012-11-21 14:40:21 +1000417 struct nv50_dmac *dmac = evoc;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000418 nv_wo32(dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Ben Skeggs51beb422011-07-05 10:33:08 +1000419}
420
421#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
422#define evo_data(p,d) *((p)++) = (d)
423
Ben Skeggs3376ee32011-11-12 14:28:12 +1000424static bool
425evo_sync_wait(void *data)
426{
Ben Skeggs5cc027f2013-02-18 17:50:51 -0500427 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
428 return true;
429 usleep_range(1, 2);
430 return false;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000431}
432
433static int
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000434evo_sync(struct drm_device *dev)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000435{
Ben Skeggs77145f12012-07-31 16:16:21 +1000436 struct nouveau_device *device = nouveau_dev(dev);
Ben Skeggse225f442012-11-21 14:40:21 +1000437 struct nv50_disp *disp = nv50_disp(dev);
438 struct nv50_mast *mast = nv50_mast(dev);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000439 u32 *push = evo_wait(mast, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000440 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000441 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000442 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000443 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000444 evo_mthd(push, 0x0080, 2);
445 evo_data(push, 0x00000000);
446 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000447 evo_kick(push, mast);
Ben Skeggs77145f12012-07-31 16:16:21 +1000448 if (nv_wait_cb(device, evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000449 return 0;
450 }
451
452 return -EBUSY;
453}
454
455/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000456 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000457 *****************************************************************************/
458struct nouveau_bo *
Ben Skeggse225f442012-11-21 14:40:21 +1000459nv50_display_crtc_sema(struct drm_device *dev, int crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000460{
Ben Skeggse225f442012-11-21 14:40:21 +1000461 return nv50_disp(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000462}
463
464void
Ben Skeggse225f442012-11-21 14:40:21 +1000465nv50_display_flip_stop(struct drm_crtc *crtc)
Ben Skeggs3376ee32011-11-12 14:28:12 +1000466{
Ben Skeggse225f442012-11-21 14:40:21 +1000467 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000468 u32 *push;
469
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000470 push = evo_wait(sync, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000471 if (push) {
472 evo_mthd(push, 0x0084, 1);
473 evo_data(push, 0x00000000);
474 evo_mthd(push, 0x0094, 1);
475 evo_data(push, 0x00000000);
476 evo_mthd(push, 0x00c0, 1);
477 evo_data(push, 0x00000000);
478 evo_mthd(push, 0x0080, 1);
479 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000480 evo_kick(push, sync);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000481 }
482}
483
484int
Ben Skeggse225f442012-11-21 14:40:21 +1000485nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000486 struct nouveau_channel *chan, u32 swap_interval)
487{
488 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000489 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000490 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000491 struct nv50_sync *sync = nv50_sync(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000492 u32 *push;
493 int ret;
494
495 swap_interval <<= 4;
496 if (swap_interval == 0)
497 swap_interval |= 0x100;
498
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000499 push = evo_wait(sync, 128);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000500 if (unlikely(push == NULL))
501 return -EBUSY;
502
503 /* synchronise with the rendering channel, if necessary */
504 if (likely(chan)) {
505 ret = RING_SPACE(chan, 10);
506 if (ret)
507 return ret;
508
Ben Skeggsa34caf72013-02-14 09:28:37 +1000509 if (nv_mclass(chan->object) < NV84_CHANNEL_IND_CLASS) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000510 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
511 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
512 OUT_RING (chan, sync->sem.offset);
513 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
514 OUT_RING (chan, 0xf00d0000 | sync->sem.value);
515 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
516 OUT_RING (chan, sync->sem.offset ^ 0x10);
517 OUT_RING (chan, 0x74b1e000);
518 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
Ben Skeggsa34caf72013-02-14 09:28:37 +1000519 OUT_RING (chan, NvSema);
520 } else
521 if (nv_mclass(chan->object) < NVC0_CHANNEL_IND_CLASS) {
522 u64 offset = nv84_fence_crtc(chan, nv_crtc->index);
523 offset += sync->sem.offset;
524
525 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
526 OUT_RING (chan, upper_32_bits(offset));
527 OUT_RING (chan, lower_32_bits(offset));
528 OUT_RING (chan, 0xf00d0000 | sync->sem.value);
529 OUT_RING (chan, 0x00000002);
530 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
531 OUT_RING (chan, upper_32_bits(offset));
532 OUT_RING (chan, lower_32_bits(offset ^ 0x10));
533 OUT_RING (chan, 0x74b1e000);
534 OUT_RING (chan, 0x00000001);
Ben Skeggsed5085a52012-11-16 13:16:51 +1000535 } else {
Ben Skeggsa34caf72013-02-14 09:28:37 +1000536 u64 offset = nv84_fence_crtc(chan, nv_crtc->index);
Ben Skeggsed5085a52012-11-16 13:16:51 +1000537 offset += sync->sem.offset;
Ben Skeggs35bcf5d2012-04-30 11:34:10 -0500538
Ben Skeggsed5085a52012-11-16 13:16:51 +1000539 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
540 OUT_RING (chan, upper_32_bits(offset));
541 OUT_RING (chan, lower_32_bits(offset));
542 OUT_RING (chan, 0xf00d0000 | sync->sem.value);
Ben Skeggsa34caf72013-02-14 09:28:37 +1000543 OUT_RING (chan, 0x00001002);
Ben Skeggsed5085a52012-11-16 13:16:51 +1000544 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
545 OUT_RING (chan, upper_32_bits(offset));
546 OUT_RING (chan, lower_32_bits(offset ^ 0x10));
547 OUT_RING (chan, 0x74b1e000);
Ben Skeggsa34caf72013-02-14 09:28:37 +1000548 OUT_RING (chan, 0x00001001);
Ben Skeggsed5085a52012-11-16 13:16:51 +1000549 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000550
Ben Skeggs3376ee32011-11-12 14:28:12 +1000551 FIRE_RING (chan);
552 } else {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000553 nouveau_bo_wr32(disp->sync, sync->sem.offset / 4,
554 0xf00d0000 | sync->sem.value);
555 evo_sync(crtc->dev);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000556 }
557
558 /* queue the flip */
559 evo_mthd(push, 0x0100, 1);
560 evo_data(push, 0xfffe0000);
561 evo_mthd(push, 0x0084, 1);
562 evo_data(push, swap_interval);
563 if (!(swap_interval & 0x00000100)) {
564 evo_mthd(push, 0x00e0, 1);
565 evo_data(push, 0x40000000);
566 }
567 evo_mthd(push, 0x0088, 4);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000568 evo_data(push, sync->sem.offset);
569 evo_data(push, 0xf00d0000 | sync->sem.value);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000570 evo_data(push, 0x74b1e000);
571 evo_data(push, NvEvoSync);
572 evo_mthd(push, 0x00a0, 2);
573 evo_data(push, 0x00000000);
574 evo_data(push, 0x00000000);
575 evo_mthd(push, 0x00c0, 1);
576 evo_data(push, nv_fb->r_dma);
577 evo_mthd(push, 0x0110, 2);
578 evo_data(push, 0x00000000);
579 evo_data(push, 0x00000000);
Ben Skeggse225f442012-11-21 14:40:21 +1000580 if (nv50_vers(sync) < NVD0_DISP_SYNC_CLASS) {
Ben Skeggsed5085a52012-11-16 13:16:51 +1000581 evo_mthd(push, 0x0800, 5);
582 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
583 evo_data(push, 0);
584 evo_data(push, (fb->height << 16) | fb->width);
585 evo_data(push, nv_fb->r_pitch);
586 evo_data(push, nv_fb->r_format);
587 } else {
588 evo_mthd(push, 0x0400, 5);
589 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
590 evo_data(push, 0);
591 evo_data(push, (fb->height << 16) | fb->width);
592 evo_data(push, nv_fb->r_pitch);
593 evo_data(push, nv_fb->r_format);
594 }
Ben Skeggs3376ee32011-11-12 14:28:12 +1000595 evo_mthd(push, 0x0080, 1);
596 evo_data(push, 0x00000000);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000597 evo_kick(push, sync);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000598
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000599 sync->sem.offset ^= 0x10;
600 sync->sem.value++;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000601 return 0;
602}
603
Ben Skeggs26f6d882011-07-04 16:25:18 +1000604/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000605 * CRTC
606 *****************************************************************************/
607static int
Ben Skeggse225f442012-11-21 14:40:21 +1000608nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000609{
Ben Skeggse225f442012-11-21 14:40:21 +1000610 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde691852011-10-17 12:23:41 +1000611 struct nouveau_connector *nv_connector;
612 struct drm_connector *connector;
613 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000614
Ben Skeggs488ff202011-10-17 10:38:10 +1000615 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000616 connector = &nv_connector->base;
617 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
618 if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
619 mode = DITHERING_MODE_DYNAMIC2X2;
620 } else {
621 mode = nv_connector->dithering_mode;
622 }
623
624 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
625 if (connector->display_info.bpc >= 8)
626 mode |= DITHERING_DEPTH_8BPC;
627 } else {
628 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000629 }
630
Ben Skeggsde8268c2012-11-16 10:24:31 +1000631 push = evo_wait(mast, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000632 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000633 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000634 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
635 evo_data(push, mode);
636 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000637 if (nv50_vers(mast) < NVE0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000638 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
639 evo_data(push, mode);
640 } else {
641 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
642 evo_data(push, mode);
643 }
644
Ben Skeggs438d99e2011-07-05 16:48:06 +1000645 if (update) {
646 evo_mthd(push, 0x0080, 1);
647 evo_data(push, 0x00000000);
648 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000649 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000650 }
651
652 return 0;
653}
654
655static int
Ben Skeggse225f442012-11-21 14:40:21 +1000656nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000657{
Ben Skeggse225f442012-11-21 14:40:21 +1000658 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs92854622011-11-11 23:49:06 +1000659 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000660 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000661 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000662 int mode = DRM_MODE_SCALE_NONE;
663 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000664
Ben Skeggs92854622011-11-11 23:49:06 +1000665 /* start off at the resolution we programmed the crtc for, this
666 * effectively handles NONE/FULL scaling
667 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000668 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000669 if (nv_connector && nv_connector->native_mode)
670 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000671
Ben Skeggs92854622011-11-11 23:49:06 +1000672 if (mode != DRM_MODE_SCALE_NONE)
673 omode = nv_connector->native_mode;
674 else
675 omode = umode;
676
677 oX = omode->hdisplay;
678 oY = omode->vdisplay;
679 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
680 oY *= 2;
681
682 /* add overscan compensation if necessary, will keep the aspect
683 * ratio the same as the backend mode unless overridden by the
684 * user setting both hborder and vborder properties.
685 */
686 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
687 (nv_connector->underscan == UNDERSCAN_AUTO &&
688 nv_connector->edid &&
689 drm_detect_hdmi_monitor(nv_connector->edid)))) {
690 u32 bX = nv_connector->underscan_hborder;
691 u32 bY = nv_connector->underscan_vborder;
692 u32 aspect = (oY << 19) / oX;
693
694 if (bX) {
695 oX -= (bX * 2);
696 if (bY) oY -= (bY * 2);
697 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
698 } else {
699 oX -= (oX >> 4) + 32;
700 if (bY) oY -= (bY * 2);
701 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000702 }
703 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000704
Ben Skeggs92854622011-11-11 23:49:06 +1000705 /* handle CENTER/ASPECT scaling, taking into account the areas
706 * removed already for overscan compensation
707 */
708 switch (mode) {
709 case DRM_MODE_SCALE_CENTER:
710 oX = min((u32)umode->hdisplay, oX);
711 oY = min((u32)umode->vdisplay, oY);
712 /* fall-through */
713 case DRM_MODE_SCALE_ASPECT:
714 if (oY < oX) {
715 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
716 oX = ((oY * aspect) + (aspect / 2)) >> 19;
717 } else {
718 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
719 oY = ((oX * aspect) + (aspect / 2)) >> 19;
720 }
721 break;
722 default:
723 break;
724 }
725
Ben Skeggsde8268c2012-11-16 10:24:31 +1000726 push = evo_wait(mast, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000727 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000728 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000729 /*XXX: SCALE_CTRL_ACTIVE??? */
730 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
731 evo_data(push, (oY << 16) | oX);
732 evo_data(push, (oY << 16) | oX);
733 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
734 evo_data(push, 0x00000000);
735 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
736 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
737 } else {
738 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
739 evo_data(push, (oY << 16) | oX);
740 evo_data(push, (oY << 16) | oX);
741 evo_data(push, (oY << 16) | oX);
742 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
743 evo_data(push, 0x00000000);
744 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
745 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
746 }
747
748 evo_kick(push, mast);
749
Ben Skeggs3376ee32011-11-12 14:28:12 +1000750 if (update) {
Ben Skeggse225f442012-11-21 14:40:21 +1000751 nv50_display_flip_stop(crtc);
752 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000753 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000754 }
755
756 return 0;
757}
758
759static int
Ben Skeggse225f442012-11-21 14:40:21 +1000760nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggsf9887d02012-11-21 13:03:42 +1000761{
Ben Skeggse225f442012-11-21 14:40:21 +1000762 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsf9887d02012-11-21 13:03:42 +1000763 u32 *push, hue, vib;
764 int adj;
765
766 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
767 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
768 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
769
770 push = evo_wait(mast, 16);
771 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000772 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsf9887d02012-11-21 13:03:42 +1000773 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
774 evo_data(push, (hue << 20) | (vib << 8));
775 } else {
776 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
777 evo_data(push, (hue << 20) | (vib << 8));
778 }
779
780 if (update) {
781 evo_mthd(push, 0x0080, 1);
782 evo_data(push, 0x00000000);
783 }
784 evo_kick(push, mast);
785 }
786
787 return 0;
788}
789
790static int
Ben Skeggse225f442012-11-21 14:40:21 +1000791nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000792 int x, int y, bool update)
793{
794 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
Ben Skeggse225f442012-11-21 14:40:21 +1000795 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000796 u32 *push;
797
Ben Skeggsde8268c2012-11-16 10:24:31 +1000798 push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000799 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000800 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000801 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
802 evo_data(push, nvfb->nvbo->bo.offset >> 8);
803 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
804 evo_data(push, (fb->height << 16) | fb->width);
805 evo_data(push, nvfb->r_pitch);
806 evo_data(push, nvfb->r_format);
807 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
808 evo_data(push, (y << 16) | x);
Ben Skeggse225f442012-11-21 14:40:21 +1000809 if (nv50_vers(mast) > NV50_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000810 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
811 evo_data(push, nvfb->r_dma);
812 }
813 } else {
814 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
815 evo_data(push, nvfb->nvbo->bo.offset >> 8);
816 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
817 evo_data(push, (fb->height << 16) | fb->width);
818 evo_data(push, nvfb->r_pitch);
819 evo_data(push, nvfb->r_format);
820 evo_data(push, nvfb->r_dma);
821 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
822 evo_data(push, (y << 16) | x);
823 }
824
Ben Skeggsa46232e2011-07-07 15:23:48 +1000825 if (update) {
826 evo_mthd(push, 0x0080, 1);
827 evo_data(push, 0x00000000);
828 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000829 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000830 }
831
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000832 nv_crtc->fb.tile_flags = nvfb->r_dma;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000833 return 0;
834}
835
836static void
Ben Skeggse225f442012-11-21 14:40:21 +1000837nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000838{
Ben Skeggse225f442012-11-21 14:40:21 +1000839 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000840 u32 *push = evo_wait(mast, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000841 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000842 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000843 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
844 evo_data(push, 0x85000000);
845 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
846 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000847 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000848 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
849 evo_data(push, 0x85000000);
850 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
851 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
852 evo_data(push, NvEvoVRAM);
853 } else {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000854 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
855 evo_data(push, 0x85000000);
856 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
857 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000858 evo_data(push, NvEvoVRAM);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000859 }
860 evo_kick(push, mast);
861 }
862}
863
864static void
Ben Skeggse225f442012-11-21 14:40:21 +1000865nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000866{
Ben Skeggse225f442012-11-21 14:40:21 +1000867 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000868 u32 *push = evo_wait(mast, 16);
869 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000870 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000871 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
872 evo_data(push, 0x05000000);
873 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000874 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000875 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
876 evo_data(push, 0x05000000);
877 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
878 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000879 } else {
880 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
881 evo_data(push, 0x05000000);
882 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
883 evo_data(push, 0x00000000);
884 }
Ben Skeggsde8268c2012-11-16 10:24:31 +1000885 evo_kick(push, mast);
886 }
887}
Ben Skeggs438d99e2011-07-05 16:48:06 +1000888
Ben Skeggsde8268c2012-11-16 10:24:31 +1000889static void
Ben Skeggse225f442012-11-21 14:40:21 +1000890nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
Ben Skeggsde8268c2012-11-16 10:24:31 +1000891{
Ben Skeggse225f442012-11-21 14:40:21 +1000892 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000893
894 if (show)
Ben Skeggse225f442012-11-21 14:40:21 +1000895 nv50_crtc_cursor_show(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000896 else
Ben Skeggse225f442012-11-21 14:40:21 +1000897 nv50_crtc_cursor_hide(nv_crtc);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000898
899 if (update) {
900 u32 *push = evo_wait(mast, 2);
901 if (push) {
Ben Skeggs438d99e2011-07-05 16:48:06 +1000902 evo_mthd(push, 0x0080, 1);
903 evo_data(push, 0x00000000);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000904 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000905 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000906 }
907}
908
909static void
Ben Skeggse225f442012-11-21 14:40:21 +1000910nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000911{
912}
913
914static void
Ben Skeggse225f442012-11-21 14:40:21 +1000915nv50_crtc_prepare(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000916{
917 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000918 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000919 u32 *push;
920
Ben Skeggse225f442012-11-21 14:40:21 +1000921 nv50_display_flip_stop(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000922
Ben Skeggsde8268c2012-11-16 10:24:31 +1000923 push = evo_wait(mast, 2);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000924 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000925 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000926 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
927 evo_data(push, 0x00000000);
928 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
929 evo_data(push, 0x40000000);
930 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000931 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000932 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
933 evo_data(push, 0x00000000);
934 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
935 evo_data(push, 0x40000000);
936 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
937 evo_data(push, 0x00000000);
938 } else {
939 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
940 evo_data(push, 0x00000000);
941 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
942 evo_data(push, 0x03000000);
943 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
944 evo_data(push, 0x00000000);
945 }
946
947 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000948 }
949
Ben Skeggse225f442012-11-21 14:40:21 +1000950 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000951}
952
953static void
Ben Skeggse225f442012-11-21 14:40:21 +1000954nv50_crtc_commit(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000955{
956 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000957 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000958 u32 *push;
959
Ben Skeggsde8268c2012-11-16 10:24:31 +1000960 push = evo_wait(mast, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000961 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +1000962 if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000963 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
964 evo_data(push, NvEvoVRAM_LP);
965 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
966 evo_data(push, 0xc0000000);
967 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
968 } else
Ben Skeggse225f442012-11-21 14:40:21 +1000969 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +1000970 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
971 evo_data(push, nv_crtc->fb.tile_flags);
972 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
973 evo_data(push, 0xc0000000);
974 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
975 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
976 evo_data(push, NvEvoVRAM);
977 } else {
978 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
979 evo_data(push, nv_crtc->fb.tile_flags);
980 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
981 evo_data(push, 0x83000000);
982 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
983 evo_data(push, 0x00000000);
984 evo_data(push, 0x00000000);
985 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
986 evo_data(push, NvEvoVRAM);
987 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
988 evo_data(push, 0xffffff00);
989 }
990
991 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000992 }
993
Ben Skeggse225f442012-11-21 14:40:21 +1000994 nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
995 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000996}
997
998static bool
Ben Skeggse225f442012-11-21 14:40:21 +1000999nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001000 struct drm_display_mode *adjusted_mode)
1001{
1002 return true;
1003}
1004
1005static int
Ben Skeggse225f442012-11-21 14:40:21 +10001006nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001007{
1008 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
1009 int ret;
1010
1011 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
1012 if (ret)
1013 return ret;
1014
1015 if (old_fb) {
1016 nvfb = nouveau_framebuffer(old_fb);
1017 nouveau_bo_unpin(nvfb->nvbo);
1018 }
1019
1020 return 0;
1021}
1022
1023static int
Ben Skeggse225f442012-11-21 14:40:21 +10001024nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001025 struct drm_display_mode *mode, int x, int y,
1026 struct drm_framebuffer *old_fb)
1027{
Ben Skeggse225f442012-11-21 14:40:21 +10001028 struct nv50_mast *mast = nv50_mast(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001029 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1030 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001031 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1032 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1033 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1034 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
1035 u32 vblan2e = 0, vblan2s = 1;
Ben Skeggs3488c572012-03-12 11:42:20 +10001036 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001037 int ret;
1038
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001039 hactive = mode->htotal;
1040 hsynce = mode->hsync_end - mode->hsync_start - 1;
1041 hbackp = mode->htotal - mode->hsync_end;
1042 hblanke = hsynce + hbackp;
1043 hfrontp = mode->hsync_start - mode->hdisplay;
1044 hblanks = mode->htotal - hfrontp - 1;
1045
1046 vactive = mode->vtotal * vscan / ilace;
1047 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1048 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1049 vblanke = vsynce + vbackp;
1050 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1051 vblanks = vactive - vfrontp - 1;
1052 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1053 vblan2e = vactive + vsynce + vbackp;
1054 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1055 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +10001056 }
1057
Ben Skeggse225f442012-11-21 14:40:21 +10001058 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001059 if (ret)
1060 return ret;
1061
Ben Skeggsde8268c2012-11-16 10:24:31 +10001062 push = evo_wait(mast, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001063 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001064 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001065 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1066 evo_data(push, 0x00800000 | mode->clock);
1067 evo_data(push, (ilace == 2) ? 2 : 0);
1068 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
1069 evo_data(push, 0x00000000);
1070 evo_data(push, (vactive << 16) | hactive);
1071 evo_data(push, ( vsynce << 16) | hsynce);
1072 evo_data(push, (vblanke << 16) | hblanke);
1073 evo_data(push, (vblanks << 16) | hblanks);
1074 evo_data(push, (vblan2e << 16) | vblan2s);
1075 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
1076 evo_data(push, 0x00000000);
1077 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1078 evo_data(push, 0x00000311);
1079 evo_data(push, 0x00000100);
1080 } else {
1081 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1082 evo_data(push, 0x00000000);
1083 evo_data(push, (vactive << 16) | hactive);
1084 evo_data(push, ( vsynce << 16) | hsynce);
1085 evo_data(push, (vblanke << 16) | hblanke);
1086 evo_data(push, (vblanks << 16) | hblanks);
1087 evo_data(push, (vblan2e << 16) | vblan2s);
1088 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1089 evo_data(push, 0x00000000); /* ??? */
1090 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1091 evo_data(push, mode->clock * 1000);
1092 evo_data(push, 0x00200000); /* ??? */
1093 evo_data(push, mode->clock * 1000);
1094 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1095 evo_data(push, 0x00000311);
1096 evo_data(push, 0x00000100);
1097 }
1098
1099 evo_kick(push, mast);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001100 }
1101
1102 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001103 nv50_crtc_set_dither(nv_crtc, false);
1104 nv50_crtc_set_scale(nv_crtc, false);
1105 nv50_crtc_set_color_vibrance(nv_crtc, false);
1106 nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001107 return 0;
1108}
1109
1110static int
Ben Skeggse225f442012-11-21 14:40:21 +10001111nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001112 struct drm_framebuffer *old_fb)
1113{
Ben Skeggs77145f12012-07-31 16:16:21 +10001114 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001115 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1116 int ret;
1117
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001118 if (!crtc->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001119 NV_DEBUG(drm, "No FB bound\n");
Ben Skeggs84e2ad82011-08-26 09:40:39 +10001120 return 0;
1121 }
1122
Ben Skeggse225f442012-11-21 14:40:21 +10001123 ret = nv50_crtc_swap_fbs(crtc, old_fb);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001124 if (ret)
1125 return ret;
1126
Ben Skeggse225f442012-11-21 14:40:21 +10001127 nv50_display_flip_stop(crtc);
1128 nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
1129 nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001130 return 0;
1131}
1132
1133static int
Ben Skeggse225f442012-11-21 14:40:21 +10001134nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001135 struct drm_framebuffer *fb, int x, int y,
1136 enum mode_set_atomic state)
1137{
1138 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001139 nv50_display_flip_stop(crtc);
1140 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001141 return 0;
1142}
1143
1144static void
Ben Skeggse225f442012-11-21 14:40:21 +10001145nv50_crtc_lut_load(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001146{
Ben Skeggse225f442012-11-21 14:40:21 +10001147 struct nv50_disp *disp = nv50_disp(crtc->dev);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001148 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1149 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1150 int i;
1151
1152 for (i = 0; i < 256; i++) {
Ben Skeggsde8268c2012-11-16 10:24:31 +10001153 u16 r = nv_crtc->lut.r[i] >> 2;
1154 u16 g = nv_crtc->lut.g[i] >> 2;
1155 u16 b = nv_crtc->lut.b[i] >> 2;
1156
1157 if (nv_mclass(disp->core) < NVD0_DISP_CLASS) {
1158 writew(r + 0x0000, lut + (i * 0x08) + 0);
1159 writew(g + 0x0000, lut + (i * 0x08) + 2);
1160 writew(b + 0x0000, lut + (i * 0x08) + 4);
1161 } else {
1162 writew(r + 0x6000, lut + (i * 0x20) + 0);
1163 writew(g + 0x6000, lut + (i * 0x20) + 2);
1164 writew(b + 0x6000, lut + (i * 0x20) + 4);
1165 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001166 }
1167}
1168
1169static int
Ben Skeggse225f442012-11-21 14:40:21 +10001170nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001171 uint32_t handle, uint32_t width, uint32_t height)
1172{
1173 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1174 struct drm_device *dev = crtc->dev;
1175 struct drm_gem_object *gem;
1176 struct nouveau_bo *nvbo;
1177 bool visible = (handle != 0);
1178 int i, ret = 0;
1179
1180 if (visible) {
1181 if (width != 64 || height != 64)
1182 return -EINVAL;
1183
1184 gem = drm_gem_object_lookup(dev, file_priv, handle);
1185 if (unlikely(!gem))
1186 return -ENOENT;
1187 nvbo = nouveau_gem_object(gem);
1188
1189 ret = nouveau_bo_map(nvbo);
1190 if (ret == 0) {
1191 for (i = 0; i < 64 * 64; i++) {
1192 u32 v = nouveau_bo_rd32(nvbo, i);
1193 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1194 }
1195 nouveau_bo_unmap(nvbo);
1196 }
1197
1198 drm_gem_object_unreference_unlocked(gem);
1199 }
1200
1201 if (visible != nv_crtc->cursor.visible) {
Ben Skeggse225f442012-11-21 14:40:21 +10001202 nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001203 nv_crtc->cursor.visible = visible;
1204 }
1205
1206 return ret;
1207}
1208
1209static int
Ben Skeggse225f442012-11-21 14:40:21 +10001210nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001211{
Ben Skeggse225f442012-11-21 14:40:21 +10001212 struct nv50_curs *curs = nv50_curs(crtc);
1213 struct nv50_chan *chan = nv50_chan(curs);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001214 nv_wo32(chan->user, 0x0084, (y << 16) | (x & 0xffff));
1215 nv_wo32(chan->user, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001216 return 0;
1217}
1218
1219static void
Ben Skeggse225f442012-11-21 14:40:21 +10001220nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001221 uint32_t start, uint32_t size)
1222{
1223 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1224 u32 end = max(start + size, (u32)256);
1225 u32 i;
1226
1227 for (i = start; i < end; i++) {
1228 nv_crtc->lut.r[i] = r[i];
1229 nv_crtc->lut.g[i] = g[i];
1230 nv_crtc->lut.b[i] = b[i];
1231 }
1232
Ben Skeggse225f442012-11-21 14:40:21 +10001233 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001234}
1235
1236static void
Ben Skeggse225f442012-11-21 14:40:21 +10001237nv50_crtc_destroy(struct drm_crtc *crtc)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001238{
1239 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001240 struct nv50_disp *disp = nv50_disp(crtc->dev);
1241 struct nv50_head *head = nv50_head(crtc);
1242 nv50_dmac_destroy(disp->core, &head->ovly.base);
1243 nv50_pioc_destroy(disp->core, &head->oimm.base);
1244 nv50_dmac_destroy(disp->core, &head->sync.base);
1245 nv50_pioc_destroy(disp->core, &head->curs.base);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001246 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001247 if (nv_crtc->cursor.nvbo)
1248 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001249 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
1250 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001251 if (nv_crtc->lut.nvbo)
1252 nouveau_bo_unpin(nv_crtc->lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001253 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
1254 drm_crtc_cleanup(crtc);
1255 kfree(crtc);
1256}
1257
Ben Skeggse225f442012-11-21 14:40:21 +10001258static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1259 .dpms = nv50_crtc_dpms,
1260 .prepare = nv50_crtc_prepare,
1261 .commit = nv50_crtc_commit,
1262 .mode_fixup = nv50_crtc_mode_fixup,
1263 .mode_set = nv50_crtc_mode_set,
1264 .mode_set_base = nv50_crtc_mode_set_base,
1265 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1266 .load_lut = nv50_crtc_lut_load,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001267};
1268
Ben Skeggse225f442012-11-21 14:40:21 +10001269static const struct drm_crtc_funcs nv50_crtc_func = {
1270 .cursor_set = nv50_crtc_cursor_set,
1271 .cursor_move = nv50_crtc_cursor_move,
1272 .gamma_set = nv50_crtc_gamma_set,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001273 .set_config = drm_crtc_helper_set_config,
Ben Skeggse225f442012-11-21 14:40:21 +10001274 .destroy = nv50_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +10001275 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +10001276};
1277
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001278static void
Ben Skeggse225f442012-11-21 14:40:21 +10001279nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001280{
1281}
1282
1283static void
Ben Skeggse225f442012-11-21 14:40:21 +10001284nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
Ben Skeggsc20ab3e2011-08-25 14:09:43 +10001285{
1286}
1287
Ben Skeggs438d99e2011-07-05 16:48:06 +10001288static int
Ben Skeggse225f442012-11-21 14:40:21 +10001289nv50_crtc_create(struct drm_device *dev, struct nouveau_object *core, int index)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001290{
Ben Skeggse225f442012-11-21 14:40:21 +10001291 struct nv50_disp *disp = nv50_disp(dev);
1292 struct nv50_head *head;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001293 struct drm_crtc *crtc;
1294 int ret, i;
1295
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001296 head = kzalloc(sizeof(*head), GFP_KERNEL);
1297 if (!head)
Ben Skeggs438d99e2011-07-05 16:48:06 +10001298 return -ENOMEM;
1299
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001300 head->base.index = index;
Ben Skeggse225f442012-11-21 14:40:21 +10001301 head->base.set_dither = nv50_crtc_set_dither;
1302 head->base.set_scale = nv50_crtc_set_scale;
1303 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
Ben Skeggsf9887d02012-11-21 13:03:42 +10001304 head->base.color_vibrance = 50;
1305 head->base.vibrant_hue = 0;
Ben Skeggse225f442012-11-21 14:40:21 +10001306 head->base.cursor.set_offset = nv50_cursor_set_offset;
1307 head->base.cursor.set_pos = nv50_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001308 for (i = 0; i < 256; i++) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001309 head->base.lut.r[i] = i << 8;
1310 head->base.lut.g[i] = i << 8;
1311 head->base.lut.b[i] = i << 8;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001312 }
1313
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001314 crtc = &head->base.base;
Ben Skeggse225f442012-11-21 14:40:21 +10001315 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1316 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001317 drm_mode_crtc_set_gamma_size(crtc, 256);
1318
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +10001319 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001320 0, 0x0000, NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001321 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001322 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001323 if (!ret) {
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001324 ret = nouveau_bo_map(head->base.lut.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001325 if (ret)
1326 nouveau_bo_unpin(head->base.lut.nvbo);
1327 }
Ben Skeggs438d99e2011-07-05 16:48:06 +10001328 if (ret)
Ben Skeggsdd0e3d52012-10-16 14:00:31 +10001329 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001330 }
1331
1332 if (ret)
1333 goto out;
1334
Ben Skeggse225f442012-11-21 14:40:21 +10001335 nv50_crtc_lut_load(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001336
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001337 /* allocate cursor resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001338 ret = nv50_pioc_create(disp->core, NV50_DISP_CURS_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001339 &(struct nv50_display_curs_class) {
1340 .head = index,
1341 }, sizeof(struct nv50_display_curs_class),
1342 &head->curs.base);
1343 if (ret)
1344 goto out;
1345
1346 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
1347 0, 0x0000, NULL, &head->base.cursor.nvbo);
1348 if (!ret) {
1349 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001350 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001351 ret = nouveau_bo_map(head->base.cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001352 if (ret)
1353 nouveau_bo_unpin(head->base.lut.nvbo);
1354 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001355 if (ret)
1356 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
1357 }
1358
1359 if (ret)
1360 goto out;
1361
1362 /* allocate page flip / sync resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001363 ret = nv50_dmac_create(disp->core, NV50_DISP_SYNC_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001364 &(struct nv50_display_sync_class) {
1365 .pushbuf = EVO_PUSH_HANDLE(SYNC, index),
1366 .head = index,
1367 }, sizeof(struct nv50_display_sync_class),
1368 disp->sync->bo.offset, &head->sync.base);
1369 if (ret)
1370 goto out;
1371
1372 head->sync.sem.offset = EVO_SYNC(1 + index, 0x00);
1373
1374 /* allocate overlay resources */
Ben Skeggse225f442012-11-21 14:40:21 +10001375 ret = nv50_pioc_create(disp->core, NV50_DISP_OIMM_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001376 &(struct nv50_display_oimm_class) {
1377 .head = index,
1378 }, sizeof(struct nv50_display_oimm_class),
1379 &head->oimm.base);
1380 if (ret)
1381 goto out;
1382
Ben Skeggse225f442012-11-21 14:40:21 +10001383 ret = nv50_dmac_create(disp->core, NV50_DISP_OVLY_CLASS, index,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001384 &(struct nv50_display_ovly_class) {
1385 .pushbuf = EVO_PUSH_HANDLE(OVLY, index),
1386 .head = index,
1387 }, sizeof(struct nv50_display_ovly_class),
1388 disp->sync->bo.offset, &head->ovly.base);
1389 if (ret)
1390 goto out;
1391
Ben Skeggs438d99e2011-07-05 16:48:06 +10001392out:
1393 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10001394 nv50_crtc_destroy(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +10001395 return ret;
1396}
1397
1398/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001399 * DAC
1400 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001401static void
Ben Skeggse225f442012-11-21 14:40:21 +10001402nv50_dac_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001403{
1404 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001405 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001406 int or = nv_encoder->or;
1407 u32 dpms_ctrl;
1408
Ben Skeggs35b21d32012-11-08 12:08:55 +10001409 dpms_ctrl = 0x00000000;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001410 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
1411 dpms_ctrl |= 0x00000001;
1412 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
1413 dpms_ctrl |= 0x00000004;
1414
Ben Skeggs35b21d32012-11-08 12:08:55 +10001415 nv_call(disp->core, NV50_DISP_DAC_PWR + or, dpms_ctrl);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001416}
1417
1418static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001419nv50_dac_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001420 const struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001421 struct drm_display_mode *adjusted_mode)
1422{
1423 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1424 struct nouveau_connector *nv_connector;
1425
1426 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1427 if (nv_connector && nv_connector->native_mode) {
1428 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1429 int id = adjusted_mode->base.id;
1430 *adjusted_mode = *nv_connector->native_mode;
1431 adjusted_mode->base.id = id;
1432 }
1433 }
1434
1435 return true;
1436}
1437
1438static void
Ben Skeggse225f442012-11-21 14:40:21 +10001439nv50_dac_commit(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001440{
1441}
1442
1443static void
Ben Skeggse225f442012-11-21 14:40:21 +10001444nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001445 struct drm_display_mode *adjusted_mode)
1446{
Ben Skeggse225f442012-11-21 14:40:21 +10001447 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001448 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1449 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001450 u32 *push;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001451
Ben Skeggse225f442012-11-21 14:40:21 +10001452 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001453
Ben Skeggs97b19b52012-11-16 11:21:37 +10001454 push = evo_wait(mast, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001455 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001456 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001457 u32 syncs = 0x00000000;
1458
1459 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1460 syncs |= 0x00000001;
1461 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1462 syncs |= 0x00000002;
1463
1464 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1465 evo_data(push, 1 << nv_crtc->index);
1466 evo_data(push, syncs);
1467 } else {
1468 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1469 u32 syncs = 0x00000001;
1470
1471 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1472 syncs |= 0x00000008;
1473 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1474 syncs |= 0x00000010;
1475
1476 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1477 magic |= 0x00000001;
1478
1479 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1480 evo_data(push, syncs);
1481 evo_data(push, magic);
1482 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1483 evo_data(push, 1 << nv_crtc->index);
1484 }
1485
1486 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001487 }
1488
1489 nv_encoder->crtc = encoder->crtc;
1490}
1491
1492static void
Ben Skeggse225f442012-11-21 14:40:21 +10001493nv50_dac_disconnect(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001494{
1495 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001496 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001497 const int or = nv_encoder->or;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001498 u32 *push;
1499
1500 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001501 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001502
Ben Skeggs97b19b52012-11-16 11:21:37 +10001503 push = evo_wait(mast, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001504 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001505 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs97b19b52012-11-16 11:21:37 +10001506 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1507 evo_data(push, 0x00000000);
1508 } else {
1509 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1510 evo_data(push, 0x00000000);
1511 }
1512
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001513 evo_mthd(push, 0x0080, 1);
1514 evo_data(push, 0x00000000);
Ben Skeggs97b19b52012-11-16 11:21:37 +10001515 evo_kick(push, mast);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001516 }
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001517 }
Ben Skeggs97b19b52012-11-16 11:21:37 +10001518
1519 nv_encoder->crtc = NULL;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001520}
1521
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001522static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +10001523nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001524{
Ben Skeggse225f442012-11-21 14:40:21 +10001525 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs35b21d32012-11-08 12:08:55 +10001526 int ret, or = nouveau_encoder(encoder)->or;
Ben Skeggs7ebb38b2012-11-09 09:38:06 +10001527 u32 load = 0;
Ben Skeggsb6819932011-07-08 11:14:50 +10001528
Ben Skeggs35b21d32012-11-08 12:08:55 +10001529 ret = nv_exec(disp->core, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
1530 if (ret || load != 7)
1531 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +10001532
Ben Skeggs35b21d32012-11-08 12:08:55 +10001533 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001534}
1535
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001536static void
Ben Skeggse225f442012-11-21 14:40:21 +10001537nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001538{
1539 drm_encoder_cleanup(encoder);
1540 kfree(encoder);
1541}
1542
Ben Skeggse225f442012-11-21 14:40:21 +10001543static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1544 .dpms = nv50_dac_dpms,
1545 .mode_fixup = nv50_dac_mode_fixup,
1546 .prepare = nv50_dac_disconnect,
1547 .commit = nv50_dac_commit,
1548 .mode_set = nv50_dac_mode_set,
1549 .disable = nv50_dac_disconnect,
1550 .get_crtc = nv50_display_crtc_get,
1551 .detect = nv50_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001552};
1553
Ben Skeggse225f442012-11-21 14:40:21 +10001554static const struct drm_encoder_funcs nv50_dac_func = {
1555 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001556};
1557
1558static int
Ben Skeggse225f442012-11-21 14:40:21 +10001559nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001560{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001561 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1562 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001563 struct nouveau_encoder *nv_encoder;
1564 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001565 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001566
1567 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1568 if (!nv_encoder)
1569 return -ENOMEM;
1570 nv_encoder->dcb = dcbe;
1571 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001572 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001573
1574 encoder = to_drm_encoder(nv_encoder);
1575 encoder->possible_crtcs = dcbe->heads;
1576 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001577 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001578 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001579
1580 drm_mode_connector_attach_encoder(connector, encoder);
1581 return 0;
1582}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001583
1584/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001585 * Audio
1586 *****************************************************************************/
1587static void
Ben Skeggse225f442012-11-21 14:40:21 +10001588nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001589{
1590 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1591 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001592 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001593
1594 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1595 if (!drm_detect_monitor_audio(nv_connector->edid))
1596 return;
1597
Ben Skeggs78951d22011-11-11 18:13:13 +10001598 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
Ben Skeggs78951d22011-11-11 18:13:13 +10001599
Ben Skeggs0a9e2b952012-11-08 14:03:56 +10001600 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or,
1601 nv_connector->base.eld,
1602 nv_connector->base.eld[2] * 4);
Ben Skeggs78951d22011-11-11 18:13:13 +10001603}
1604
1605static void
Ben Skeggse225f442012-11-21 14:40:21 +10001606nv50_audio_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001607{
1608 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001609 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001610
Ben Skeggs0a9e2b952012-11-08 14:03:56 +10001611 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or, NULL, 0);
Ben Skeggs78951d22011-11-11 18:13:13 +10001612}
1613
1614/******************************************************************************
1615 * HDMI
1616 *****************************************************************************/
1617static void
Ben Skeggse225f442012-11-21 14:40:21 +10001618nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +10001619{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001620 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1621 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1622 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +10001623 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001624 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001625 u32 rekey = 56; /* binary driver, and tegra constant */
1626 u32 max_ac_packet;
1627
1628 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1629 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1630 return;
1631
1632 max_ac_packet = mode->htotal - mode->hdisplay;
1633 max_ac_packet -= rekey;
1634 max_ac_packet -= 18; /* constant from tegra */
1635 max_ac_packet /= 32;
1636
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001637 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff,
1638 NV84_DISP_SOR_HDMI_PWR_STATE_ON |
1639 (max_ac_packet << 16) | rekey);
Ben Skeggs091e40c2011-11-11 20:46:00 +10001640
Ben Skeggse225f442012-11-21 14:40:21 +10001641 nv50_audio_mode_set(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +10001642}
1643
1644static void
Ben Skeggse225f442012-11-21 14:40:21 +10001645nv50_hdmi_disconnect(struct drm_encoder *encoder)
Ben Skeggs78951d22011-11-11 18:13:13 +10001646{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001647 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1648 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +10001649 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001650 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001651
Ben Skeggse225f442012-11-21 14:40:21 +10001652 nv50_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001653
Ben Skeggs1c30cd02012-11-08 14:22:28 +10001654 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff, 0x00000000);
Ben Skeggs78951d22011-11-11 18:13:13 +10001655}
1656
1657/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001658 * SOR
1659 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001660static void
Ben Skeggse225f442012-11-21 14:40:21 +10001661nv50_sor_dpms(struct drm_encoder *encoder, int mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001662{
1663 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1664 struct drm_device *dev = encoder->dev;
Ben Skeggse225f442012-11-21 14:40:21 +10001665 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001666 struct drm_encoder *partner;
1667 int or = nv_encoder->or;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001668
1669 nv_encoder->last_dpms = mode;
1670
1671 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1672 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1673
1674 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1675 continue;
1676
1677 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001678 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001679 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1680 return;
1681 break;
1682 }
1683 }
1684
Ben Skeggs74b66852012-11-08 12:01:39 +10001685 nv_call(disp->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001686
Ben Skeggs6c8e4632012-11-15 18:56:02 +10001687 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1688 nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, disp->core);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001689}
1690
1691static bool
Ben Skeggse225f442012-11-21 14:40:21 +10001692nv50_sor_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001693 const struct drm_display_mode *mode,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001694 struct drm_display_mode *adjusted_mode)
1695{
1696 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1697 struct nouveau_connector *nv_connector;
1698
1699 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1700 if (nv_connector && nv_connector->native_mode) {
1701 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1702 int id = adjusted_mode->base.id;
1703 *adjusted_mode = *nv_connector->native_mode;
1704 adjusted_mode->base.id = id;
1705 }
1706 }
1707
1708 return true;
1709}
1710
1711static void
Ben Skeggse225f442012-11-21 14:40:21 +10001712nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001713{
1714 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +10001715 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001716 const int or = nv_encoder->or;
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001717 u32 *push;
1718
1719 if (nv_encoder->crtc) {
Ben Skeggse225f442012-11-21 14:40:21 +10001720 nv50_crtc_prepare(nv_encoder->crtc);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001721
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001722 push = evo_wait(mast, 4);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001723 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001724 if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001725 evo_mthd(push, 0x0600 + (or * 0x40), 1);
1726 evo_data(push, 0x00000000);
1727 } else {
1728 evo_mthd(push, 0x0200 + (or * 0x20), 1);
1729 evo_data(push, 0x00000000);
1730 }
1731
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001732 evo_mthd(push, 0x0080, 1);
1733 evo_data(push, 0x00000000);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001734 evo_kick(push, mast);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001735 }
1736
Ben Skeggse225f442012-11-21 14:40:21 +10001737 nv50_hdmi_disconnect(encoder);
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001738 }
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001739
1740 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1741 nv_encoder->crtc = NULL;
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001742}
1743
1744static void
Ben Skeggse225f442012-11-21 14:40:21 +10001745nv50_sor_prepare(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001746{
Ben Skeggse225f442012-11-21 14:40:21 +10001747 nv50_sor_disconnect(encoder);
Ben Skeggscb75d972012-07-11 10:44:20 +10001748 if (nouveau_encoder(encoder)->dcb->type == DCB_OUTPUT_DP)
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001749 evo_sync(encoder->dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001750}
1751
1752static void
Ben Skeggse225f442012-11-21 14:40:21 +10001753nv50_sor_commit(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001754{
1755}
1756
1757static void
Ben Skeggse225f442012-11-21 14:40:21 +10001758nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001759 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001760{
Ben Skeggse225f442012-11-21 14:40:21 +10001761 struct nv50_disp *disp = nv50_disp(encoder->dev);
1762 struct nv50_mast *mast = nv50_mast(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001763 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001764 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001765 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1766 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001767 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001768 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001769 u32 *push, lvds = 0;
1770 u8 owner = 1 << nv_crtc->index;
1771 u8 proto = 0xf;
1772 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001773
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001774 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1775 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001776 case DCB_OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001777 if (nv_encoder->dcb->sorconf.link & 1) {
1778 if (mode->clock < 165000)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001779 proto = 0x1;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001780 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001781 proto = 0x5;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001782 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001783 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001784 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001785
Ben Skeggse225f442012-11-21 14:40:21 +10001786 nv50_hdmi_mode_set(encoder, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001787 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001788 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001789 proto = 0x0;
1790
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001791 if (bios->fp_no_ddc) {
1792 if (bios->fp.dual_link)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001793 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001794 if (bios->fp.if_is_24bit)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001795 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001796 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001797 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001798 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001799 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001800 } else
1801 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001802 lvds |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001803 }
1804
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001805 if (lvds & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001806 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001807 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001808 } else {
1809 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001810 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001811 }
1812
1813 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001814 lvds |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001815 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001816
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001817 nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + nv_encoder->or, lvds);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001818 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001819 case DCB_OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001820 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001821 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001822 depth = 0x2;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001823 } else
1824 if (nv_connector->base.display_info.bpc == 8) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001825 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001826 depth = 0x5;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001827 } else {
1828 nv_encoder->dp.datarate = mode->clock * 30 / 8;
1829 depth = 0x6;
Ben Skeggs3488c572012-03-12 11:42:20 +10001830 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001831
1832 if (nv_encoder->dcb->sorconf.link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001833 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001834 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001835 proto = 0x9;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001836 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001837 default:
1838 BUG_ON(1);
1839 break;
1840 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001841
Ben Skeggse225f442012-11-21 14:40:21 +10001842 nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001843
Ben Skeggse225f442012-11-21 14:40:21 +10001844 push = evo_wait(nv50_mast(dev), 8);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001845 if (push) {
Ben Skeggse225f442012-11-21 14:40:21 +10001846 if (nv50_vers(mast) < NVD0_DISP_CLASS) {
Ben Skeggse2de1792013-02-11 13:56:31 +10001847 u32 ctrl = (depth << 16) | (proto << 8) | owner;
1848 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1849 ctrl |= 0x00001000;
1850 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1851 ctrl |= 0x00002000;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001852 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x040), 1);
Ben Skeggse2de1792013-02-11 13:56:31 +10001853 evo_data(push, ctrl);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001854 } else {
1855 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1856 u32 syncs = 0x00000001;
1857
1858 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1859 syncs |= 0x00000008;
1860 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1861 syncs |= 0x00000010;
1862
1863 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1864 magic |= 0x00000001;
1865
1866 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1867 evo_data(push, syncs | (depth << 6));
1868 evo_data(push, magic);
1869 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 1);
1870 evo_data(push, owner | (proto << 8));
1871 }
1872
1873 evo_kick(push, mast);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001874 }
1875
1876 nv_encoder->crtc = encoder->crtc;
1877}
1878
1879static void
Ben Skeggse225f442012-11-21 14:40:21 +10001880nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001881{
1882 drm_encoder_cleanup(encoder);
1883 kfree(encoder);
1884}
1885
Ben Skeggse225f442012-11-21 14:40:21 +10001886static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
1887 .dpms = nv50_sor_dpms,
1888 .mode_fixup = nv50_sor_mode_fixup,
1889 .prepare = nv50_sor_prepare,
1890 .commit = nv50_sor_commit,
1891 .mode_set = nv50_sor_mode_set,
1892 .disable = nv50_sor_disconnect,
1893 .get_crtc = nv50_display_crtc_get,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001894};
1895
Ben Skeggse225f442012-11-21 14:40:21 +10001896static const struct drm_encoder_funcs nv50_sor_func = {
1897 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001898};
1899
1900static int
Ben Skeggse225f442012-11-21 14:40:21 +10001901nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001902{
Ben Skeggs5ed50202013-02-11 20:15:03 +10001903 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1904 struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001905 struct nouveau_encoder *nv_encoder;
1906 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001907 int type;
1908
1909 switch (dcbe->type) {
1910 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1911 case DCB_OUTPUT_TMDS:
1912 case DCB_OUTPUT_DP:
1913 default:
1914 type = DRM_MODE_ENCODER_TMDS;
1915 break;
1916 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001917
1918 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1919 if (!nv_encoder)
1920 return -ENOMEM;
1921 nv_encoder->dcb = dcbe;
1922 nv_encoder->or = ffs(dcbe->or) - 1;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001923 nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001924 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1925
1926 encoder = to_drm_encoder(nv_encoder);
1927 encoder->possible_crtcs = dcbe->heads;
1928 encoder->possible_clones = 0;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001929 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
Ben Skeggse225f442012-11-21 14:40:21 +10001930 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001931
1932 drm_mode_connector_attach_encoder(connector, encoder);
1933 return 0;
1934}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001935
1936/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001937 * Init
1938 *****************************************************************************/
Ben Skeggs2a44e492011-11-09 11:36:33 +10001939void
Ben Skeggse225f442012-11-21 14:40:21 +10001940nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10001941{
Ben Skeggs26f6d882011-07-04 16:25:18 +10001942}
1943
1944int
Ben Skeggse225f442012-11-21 14:40:21 +10001945nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10001946{
Ben Skeggse225f442012-11-21 14:40:21 +10001947 u32 *push = evo_wait(nv50_mast(dev), 32);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001948 if (push) {
1949 evo_mthd(push, 0x0088, 1);
1950 evo_data(push, NvEvoSync);
Ben Skeggse225f442012-11-21 14:40:21 +10001951 evo_kick(push, nv50_mast(dev));
Ben Skeggs647bf612012-11-19 10:18:25 +10001952 return evo_sync(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001953 }
1954
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001955 return -EBUSY;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001956}
1957
1958void
Ben Skeggse225f442012-11-21 14:40:21 +10001959nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10001960{
Ben Skeggse225f442012-11-21 14:40:21 +10001961 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001962
Ben Skeggse225f442012-11-21 14:40:21 +10001963 nv50_dmac_destroy(disp->core, &disp->mast.base);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001964
Ben Skeggs816af2f2011-11-16 15:48:48 +10001965 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001966 if (disp->sync)
1967 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10001968 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10001969
Ben Skeggs77145f12012-07-31 16:16:21 +10001970 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001971 kfree(disp);
1972}
1973
1974int
Ben Skeggse225f442012-11-21 14:40:21 +10001975nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10001976{
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001977 static const u16 oclass[] = {
1978 NVE0_DISP_CLASS,
1979 NVD0_DISP_CLASS,
Ben Skeggs63718a02012-11-16 11:44:14 +10001980 NVA3_DISP_CLASS,
1981 NV94_DISP_CLASS,
1982 NVA0_DISP_CLASS,
1983 NV84_DISP_CLASS,
1984 NV50_DISP_CLASS,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10001985 };
Ben Skeggs77145f12012-07-31 16:16:21 +10001986 struct nouveau_device *device = nouveau_dev(dev);
1987 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10001988 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001989 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10001990 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10001991 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001992 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001993
1994 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
1995 if (!disp)
1996 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10001997
1998 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10001999 nouveau_display(dev)->dtor = nv50_display_destroy;
2000 nouveau_display(dev)->init = nv50_display_init;
2001 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002002
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002003 /* small shared memory area we use for notifiers and semaphores */
2004 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2005 0, 0x0000, NULL, &disp->sync);
2006 if (!ret) {
2007 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002008 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002009 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002010 if (ret)
2011 nouveau_bo_unpin(disp->sync);
2012 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002013 if (ret)
2014 nouveau_bo_ref(NULL, &disp->sync);
2015 }
2016
2017 if (ret)
2018 goto out;
2019
2020 /* attempt to allocate a supported evo display class */
2021 ret = -ENODEV;
2022 for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
2023 ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
2024 0xd1500000, oclass[i], NULL, 0,
2025 &disp->core);
2026 }
2027
2028 if (ret)
2029 goto out;
2030
2031 /* allocate master evo channel */
Ben Skeggse225f442012-11-21 14:40:21 +10002032 ret = nv50_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002033 &(struct nv50_display_mast_class) {
2034 .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
2035 }, sizeof(struct nv50_display_mast_class),
2036 disp->sync->bo.offset, &disp->mast.base);
2037 if (ret)
2038 goto out;
2039
Ben Skeggs438d99e2011-07-05 16:48:06 +10002040 /* create crtc objects to represent the hw heads */
Ben Skeggs63718a02012-11-16 11:44:14 +10002041 if (nv_mclass(disp->core) >= NVD0_DISP_CLASS)
2042 crtcs = nv_rd32(device, 0x022448);
2043 else
2044 crtcs = 2;
2045
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002046 for (i = 0; i < crtcs; i++) {
Ben Skeggse225f442012-11-21 14:40:21 +10002047 ret = nv50_crtc_create(dev, disp->core, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002048 if (ret)
2049 goto out;
2050 }
2051
Ben Skeggs83fc0832011-07-05 13:08:40 +10002052 /* create encoder/connector objects based on VBIOS DCB table */
2053 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2054 connector = nouveau_connector_create(dev, dcbe->connector);
2055 if (IS_ERR(connector))
2056 continue;
2057
2058 if (dcbe->location != DCB_LOC_ON_CHIP) {
Ben Skeggs77145f12012-07-31 16:16:21 +10002059 NV_WARN(drm, "skipping off-chip encoder %d/%d\n",
Ben Skeggs83fc0832011-07-05 13:08:40 +10002060 dcbe->type, ffs(dcbe->or) - 1);
2061 continue;
2062 }
2063
2064 switch (dcbe->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10002065 case DCB_OUTPUT_TMDS:
2066 case DCB_OUTPUT_LVDS:
2067 case DCB_OUTPUT_DP:
Ben Skeggse225f442012-11-21 14:40:21 +10002068 nv50_sor_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002069 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10002070 case DCB_OUTPUT_ANALOG:
Ben Skeggse225f442012-11-21 14:40:21 +10002071 nv50_dac_create(connector, dcbe);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10002072 break;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002073 default:
Ben Skeggs77145f12012-07-31 16:16:21 +10002074 NV_WARN(drm, "skipping unsupported encoder %d/%d\n",
Ben Skeggs83fc0832011-07-05 13:08:40 +10002075 dcbe->type, ffs(dcbe->or) - 1);
2076 continue;
2077 }
2078 }
2079
2080 /* cull any connectors we created that don't have an encoder */
2081 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2082 if (connector->encoder_ids[0])
2083 continue;
2084
Ben Skeggs77145f12012-07-31 16:16:21 +10002085 NV_WARN(drm, "%s has no encoders, removing\n",
Ben Skeggs83fc0832011-07-05 13:08:40 +10002086 drm_get_connector_name(connector));
2087 connector->funcs->destroy(connector);
2088 }
2089
Ben Skeggs26f6d882011-07-04 16:25:18 +10002090out:
2091 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002092 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002093 return ret;
2094}