drm/nvd0/disp: start implementing support for older display classes
[linux-block.git] / drivers / gpu / drm / nouveau / nvd0_display.c
CommitLineData
26f6d88b
BS
1/*
2 * 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
51beb428 25#include <linux/dma-mapping.h>
83fc083c 26
760285e7
DH
27#include <drm/drmP.h>
28#include <drm/drm_crtc_helper.h>
26f6d88b 29
77145f1c
BS
30#include "nouveau_drm.h"
31#include "nouveau_dma.h"
32#include "nouveau_gem.h"
26f6d88b
BS
33#include "nouveau_connector.h"
34#include "nouveau_encoder.h"
35#include "nouveau_crtc.h"
f589be88 36#include "nouveau_fence.h"
3a89cd02 37#include "nv50_display.h"
26f6d88b 38
b5a794b0 39#include <core/client.h>
77145f1c 40#include <core/gpuobj.h>
b5a794b0 41#include <core/class.h>
77145f1c
BS
42
43#include <subdev/timer.h>
44#include <subdev/bar.h>
45#include <subdev/fb.h>
46
8a46438a
BS
47#define EVO_DMA_NR 9
48
bdb8c212 49#define EVO_MASTER (0x00)
a63a97eb 50#define EVO_FLIP(c) (0x01 + (c))
8a46438a
BS
51#define EVO_OVLY(c) (0x05 + (c))
52#define EVO_OIMM(c) (0x09 + (c))
bdb8c212
BS
53#define EVO_CURS(c) (0x0d + (c))
54
816af2f2
BS
55/* offsets in shared sync bo of various structures */
56#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
57#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
58#define EVO_FLIP_SEM0(c) EVO_SYNC((c), 0x00)
59#define EVO_FLIP_SEM1(c) EVO_SYNC((c), 0x10)
60
b5a794b0
BS
61#define EVO_CORE_HANDLE (0xd1500000)
62#define EVO_CHAN_HANDLE(t,i) (0xd15c0000 | (((t) & 0x00ff) << 8) | (i))
63#define EVO_CHAN_OCLASS(t,c) ((nv_hclass(c) & 0xff00) | ((t) & 0x00ff))
64#define EVO_PUSH_HANDLE(t,i) (0xd15b0000 | (i) | \
65 (((NV50_DISP_##t##_CLASS) & 0x00ff) << 8))
66
67/******************************************************************************
68 * EVO channel
69 *****************************************************************************/
70
71struct nvd0_chan {
72 struct nouveau_object *user;
73 u32 handle;
74};
75
76static int
77nvd0_chan_create(struct nouveau_object *core, u32 bclass, u8 head,
78 void *data, u32 size, struct nvd0_chan *chan)
79{
80 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
81 const u32 oclass = EVO_CHAN_OCLASS(bclass, core);
82 const u32 handle = EVO_CHAN_HANDLE(bclass, head);
83 int ret;
84
85 ret = nouveau_object_new(client, EVO_CORE_HANDLE, handle,
86 oclass, data, size, &chan->user);
87 if (ret)
88 return ret;
89
90 chan->handle = handle;
91 return 0;
92}
93
94static void
95nvd0_chan_destroy(struct nouveau_object *core, struct nvd0_chan *chan)
96{
97 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
98 if (chan->handle)
99 nouveau_object_del(client, EVO_CORE_HANDLE, chan->handle);
100}
101
102/******************************************************************************
103 * PIO EVO channel
104 *****************************************************************************/
105
106struct nvd0_pioc {
107 struct nvd0_chan base;
108};
109
110static void
111nvd0_pioc_destroy(struct nouveau_object *core, struct nvd0_pioc *pioc)
112{
113 nvd0_chan_destroy(core, &pioc->base);
114}
115
116static int
117nvd0_pioc_create(struct nouveau_object *core, u32 bclass, u8 head,
118 void *data, u32 size, struct nvd0_pioc *pioc)
119{
120 return nvd0_chan_create(core, bclass, head, data, size, &pioc->base);
121}
122
123/******************************************************************************
124 * DMA EVO channel
125 *****************************************************************************/
126
127struct nvd0_dmac {
128 struct nvd0_chan base;
3376ee37
BS
129 dma_addr_t handle;
130 u32 *ptr;
b5a794b0
BS
131};
132
133static void
134nvd0_dmac_destroy(struct nouveau_object *core, struct nvd0_dmac *dmac)
135{
136 if (dmac->ptr) {
137 struct pci_dev *pdev = nv_device(core)->pdev;
138 pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
139 }
140
141 nvd0_chan_destroy(core, &dmac->base);
142}
143
144static int
145nvd0_dmac_create(struct nouveau_object *core, u32 bclass, u8 head,
146 void *data, u32 size, u64 syncbuf,
147 struct nvd0_dmac *dmac)
148{
149 struct nouveau_fb *pfb = nouveau_fb(core);
150 struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
151 struct nouveau_object *object;
152 u32 pushbuf = *(u32 *)data;
153 dma_addr_t handle;
154 void *ptr;
155 int ret;
156
157 ptr = pci_alloc_consistent(nv_device(core)->pdev, PAGE_SIZE, &handle);
158 if (!ptr)
159 return -ENOMEM;
160
161 ret = nouveau_object_new(client, NVDRM_DEVICE, pushbuf,
162 NV_DMA_FROM_MEMORY_CLASS,
163 &(struct nv_dma_class) {
164 .flags = NV_DMA_TARGET_PCI_US |
165 NV_DMA_ACCESS_RD,
166 .start = handle + 0x0000,
167 .limit = handle + 0x0fff,
168 }, sizeof(struct nv_dma_class), &object);
169 if (ret)
170 return ret;
171
172 ret = nvd0_chan_create(core, bclass, head, data, size, &dmac->base);
173 if (ret)
174 return ret;
175
176 dmac->handle = handle;
177 dmac->ptr = ptr;
178
179 ret = nouveau_object_new(client, dmac->base.handle, NvEvoSync,
180 NV_DMA_IN_MEMORY_CLASS,
181 &(struct nv_dma_class) {
182 .flags = NV_DMA_TARGET_VRAM |
183 NV_DMA_ACCESS_RDWR,
184 .start = syncbuf + 0x0000,
185 .limit = syncbuf + 0x0fff,
186 }, sizeof(struct nv_dma_class), &object);
187 if (ret)
188 goto out;
189
190 ret = nouveau_object_new(client, dmac->base.handle, NvEvoVRAM,
191 NV_DMA_IN_MEMORY_CLASS,
192 &(struct nv_dma_class) {
193 .flags = NV_DMA_TARGET_VRAM |
194 NV_DMA_ACCESS_RDWR,
195 .start = 0,
196 .limit = pfb->ram.size - 1,
197 }, sizeof(struct nv_dma_class), &object);
198 if (ret)
199 goto out;
200
201 ret = nouveau_object_new(client, dmac->base.handle, NvEvoVRAM_LP,
202 NV_DMA_IN_MEMORY_CLASS,
203 &(struct nv_dma_class) {
204 .flags = NV_DMA_TARGET_VRAM |
205 NV_DMA_ACCESS_RDWR,
206 .start = 0,
207 .limit = pfb->ram.size - 1,
208 .conf0 = NVD0_DMA_CONF0_ENABLE |
209 NVD0_DMA_CONF0_PAGE_LP,
210 }, sizeof(struct nv_dma_class), &object);
211 if (ret)
212 goto out;
213
214 ret = nouveau_object_new(client, dmac->base.handle, NvEvoFB32,
215 NV_DMA_IN_MEMORY_CLASS,
216 &(struct nv_dma_class) {
217 .flags = NV_DMA_TARGET_VRAM |
218 NV_DMA_ACCESS_RDWR,
219 .start = 0,
220 .limit = pfb->ram.size - 1,
221 .conf0 = 0x00fe |
222 NVD0_DMA_CONF0_ENABLE |
223 NVD0_DMA_CONF0_PAGE_LP,
224 }, sizeof(struct nv_dma_class), &object);
225out:
226 if (ret)
227 nvd0_dmac_destroy(core, dmac);
228 return ret;
229}
230
231struct nvd0_mast {
232 struct nvd0_dmac base;
233};
234
235struct nvd0_curs {
236 struct nvd0_pioc base;
237};
238
239struct nvd0_sync {
240 struct nvd0_dmac base;
3376ee37 241 struct {
3376ee37
BS
242 u32 offset;
243 u16 value;
244 } sem;
245};
246
b5a794b0
BS
247struct nvd0_ovly {
248 struct nvd0_dmac base;
249};
f20ce962 250
b5a794b0
BS
251struct nvd0_oimm {
252 struct nvd0_pioc base;
26f6d88b
BS
253};
254
dd0e3d53
BS
255struct nvd0_head {
256 struct nouveau_crtc base;
b5a794b0
BS
257 struct nvd0_curs curs;
258 struct nvd0_sync sync;
259 struct nvd0_ovly ovly;
260 struct nvd0_oimm oimm;
261};
262
263#define nvd0_head(c) ((struct nvd0_head *)nouveau_crtc(c))
264#define nvd0_curs(c) (&nvd0_head(c)->curs)
265#define nvd0_sync(c) (&nvd0_head(c)->sync)
266#define nvd0_ovly(c) (&nvd0_head(c)->ovly)
267#define nvd0_oimm(c) (&nvd0_head(c)->oimm)
268#define nvd0_chan(c) (&(c)->base.base)
de8268c5 269#define nvd0_vers(c) nv_mclass(nvd0_chan(c)->user)
b5a794b0
BS
270
271struct nvd0_disp {
272 struct nouveau_object *core;
273 struct nvd0_mast mast;
274
b5a794b0
BS
275 u32 modeset;
276
277 struct nouveau_bo *sync;
dd0e3d53
BS
278};
279
94e5c39b
BS
280static struct nvd0_disp *
281nvd0_disp(struct drm_device *dev)
26f6d88b 282{
77145f1c 283 return nouveau_display(dev)->priv;
26f6d88b
BS
284}
285
b5a794b0
BS
286#define nvd0_mast(d) (&nvd0_disp(d)->mast)
287
bdb8c212
BS
288static struct drm_crtc *
289nvd0_display_crtc_get(struct drm_encoder *encoder)
290{
291 return nouveau_encoder(encoder)->crtc;
292}
293
294/******************************************************************************
295 * EVO channel helpers
296 *****************************************************************************/
51beb428 297static u32 *
b5a794b0 298evo_wait(void *evoc, int nr)
51beb428 299{
b5a794b0
BS
300 struct nvd0_dmac *dmac = evoc;
301 u32 put = nv_ro32(dmac->base.user, 0x0000) / 4;
51beb428 302
de8268c5 303 if (put + nr >= (PAGE_SIZE / 4) - 8) {
b5a794b0 304 dmac->ptr[put] = 0x20000000;
51beb428 305
b5a794b0
BS
306 nv_wo32(dmac->base.user, 0x0000, 0x00000000);
307 if (!nv_wait(dmac->base.user, 0x0004, ~0, 0x00000000)) {
308 NV_ERROR(dmac->base.user, "channel stalled\n");
51beb428
BS
309 return NULL;
310 }
311
312 put = 0;
313 }
314
b5a794b0 315 return dmac->ptr + put;
51beb428
BS
316}
317
318static void
b5a794b0 319evo_kick(u32 *push, void *evoc)
51beb428 320{
b5a794b0
BS
321 struct nvd0_dmac *dmac = evoc;
322 nv_wo32(dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
51beb428
BS
323}
324
325#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
326#define evo_data(p,d) *((p)++) = (d)
327
3376ee37
BS
328static bool
329evo_sync_wait(void *data)
330{
816af2f2 331 return nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000;
3376ee37
BS
332}
333
334static int
b5a794b0 335evo_sync(struct drm_device *dev)
3376ee37 336{
77145f1c 337 struct nouveau_device *device = nouveau_dev(dev);
94e5c39b 338 struct nvd0_disp *disp = nvd0_disp(dev);
b5a794b0
BS
339 struct nvd0_mast *mast = nvd0_mast(dev);
340 u32 *push = evo_wait(mast, 8);
3376ee37 341 if (push) {
816af2f2 342 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
3376ee37 343 evo_mthd(push, 0x0084, 1);
816af2f2 344 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
3376ee37
BS
345 evo_mthd(push, 0x0080, 2);
346 evo_data(push, 0x00000000);
347 evo_data(push, 0x00000000);
b5a794b0 348 evo_kick(push, mast);
77145f1c 349 if (nv_wait_cb(device, evo_sync_wait, disp->sync))
3376ee37
BS
350 return 0;
351 }
352
353 return -EBUSY;
354}
355
356/******************************************************************************
a63a97eb 357 * Page flipping channel
3376ee37
BS
358 *****************************************************************************/
359struct nouveau_bo *
360nvd0_display_crtc_sema(struct drm_device *dev, int crtc)
361{
94e5c39b 362 return nvd0_disp(dev)->sync;
3376ee37
BS
363}
364
365void
366nvd0_display_flip_stop(struct drm_crtc *crtc)
367{
b5a794b0 368 struct nvd0_sync *sync = nvd0_sync(crtc);
3376ee37
BS
369 u32 *push;
370
b5a794b0 371 push = evo_wait(sync, 8);
3376ee37
BS
372 if (push) {
373 evo_mthd(push, 0x0084, 1);
374 evo_data(push, 0x00000000);
375 evo_mthd(push, 0x0094, 1);
376 evo_data(push, 0x00000000);
377 evo_mthd(push, 0x00c0, 1);
378 evo_data(push, 0x00000000);
379 evo_mthd(push, 0x0080, 1);
380 evo_data(push, 0x00000000);
b5a794b0 381 evo_kick(push, sync);
3376ee37
BS
382 }
383}
384
385int
386nvd0_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
387 struct nouveau_channel *chan, u32 swap_interval)
388{
389 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
94e5c39b 390 struct nvd0_disp *disp = nvd0_disp(crtc->dev);
3376ee37 391 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
b5a794b0 392 struct nvd0_sync *sync = nvd0_sync(crtc);
3376ee37
BS
393 u64 offset;
394 u32 *push;
395 int ret;
396
397 swap_interval <<= 4;
398 if (swap_interval == 0)
399 swap_interval |= 0x100;
400
b5a794b0 401 push = evo_wait(sync, 128);
3376ee37
BS
402 if (unlikely(push == NULL))
403 return -EBUSY;
404
405 /* synchronise with the rendering channel, if necessary */
406 if (likely(chan)) {
407 ret = RING_SPACE(chan, 10);
408 if (ret)
409 return ret;
410
35bcf5d5 411
f589be88 412 offset = nvc0_fence_crtc(chan, nv_crtc->index);
b5a794b0 413 offset += sync->sem.offset;
3376ee37 414
6d597027 415 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
3376ee37
BS
416 OUT_RING (chan, upper_32_bits(offset));
417 OUT_RING (chan, lower_32_bits(offset));
b5a794b0 418 OUT_RING (chan, 0xf00d0000 | sync->sem.value);
3376ee37 419 OUT_RING (chan, 0x1002);
6d597027 420 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
3376ee37
BS
421 OUT_RING (chan, upper_32_bits(offset));
422 OUT_RING (chan, lower_32_bits(offset ^ 0x10));
423 OUT_RING (chan, 0x74b1e000);
424 OUT_RING (chan, 0x1001);
425 FIRE_RING (chan);
426 } else {
b5a794b0
BS
427 nouveau_bo_wr32(disp->sync, sync->sem.offset / 4,
428 0xf00d0000 | sync->sem.value);
429 evo_sync(crtc->dev);
3376ee37
BS
430 }
431
432 /* queue the flip */
433 evo_mthd(push, 0x0100, 1);
434 evo_data(push, 0xfffe0000);
435 evo_mthd(push, 0x0084, 1);
436 evo_data(push, swap_interval);
437 if (!(swap_interval & 0x00000100)) {
438 evo_mthd(push, 0x00e0, 1);
439 evo_data(push, 0x40000000);
440 }
441 evo_mthd(push, 0x0088, 4);
b5a794b0
BS
442 evo_data(push, sync->sem.offset);
443 evo_data(push, 0xf00d0000 | sync->sem.value);
3376ee37
BS
444 evo_data(push, 0x74b1e000);
445 evo_data(push, NvEvoSync);
446 evo_mthd(push, 0x00a0, 2);
447 evo_data(push, 0x00000000);
448 evo_data(push, 0x00000000);
449 evo_mthd(push, 0x00c0, 1);
450 evo_data(push, nv_fb->r_dma);
451 evo_mthd(push, 0x0110, 2);
452 evo_data(push, 0x00000000);
453 evo_data(push, 0x00000000);
454 evo_mthd(push, 0x0400, 5);
455 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
456 evo_data(push, 0);
457 evo_data(push, (fb->height << 16) | fb->width);
458 evo_data(push, nv_fb->r_pitch);
459 evo_data(push, nv_fb->r_format);
460 evo_mthd(push, 0x0080, 1);
461 evo_data(push, 0x00000000);
b5a794b0 462 evo_kick(push, sync);
3376ee37 463
b5a794b0
BS
464 sync->sem.offset ^= 0x10;
465 sync->sem.value++;
3376ee37
BS
466 return 0;
467}
468
438d99e3
BS
469/******************************************************************************
470 * CRTC
471 *****************************************************************************/
472static int
488ff207 473nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
438d99e3 474{
de8268c5 475 struct nvd0_mast *mast = nvd0_mast(nv_crtc->base.dev);
de691855
BS
476 struct nouveau_connector *nv_connector;
477 struct drm_connector *connector;
478 u32 *push, mode = 0x00;
438d99e3 479
488ff207 480 nv_connector = nouveau_crtc_connector_get(nv_crtc);
de691855
BS
481 connector = &nv_connector->base;
482 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
483 if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
484 mode = DITHERING_MODE_DYNAMIC2X2;
485 } else {
486 mode = nv_connector->dithering_mode;
487 }
488
489 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
490 if (connector->display_info.bpc >= 8)
491 mode |= DITHERING_DEPTH_8BPC;
492 } else {
493 mode |= nv_connector->dithering_depth;
438d99e3
BS
494 }
495
de8268c5 496 push = evo_wait(mast, 4);
438d99e3 497 if (push) {
de8268c5
BS
498 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
499 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
500 evo_data(push, mode);
501 } else
502 if (nvd0_vers(mast) < NVE0_DISP_MAST_CLASS) {
503 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
504 evo_data(push, mode);
505 } else {
506 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
507 evo_data(push, mode);
508 }
509
438d99e3
BS
510 if (update) {
511 evo_mthd(push, 0x0080, 1);
512 evo_data(push, 0x00000000);
513 }
de8268c5 514 evo_kick(push, mast);
438d99e3
BS
515 }
516
517 return 0;
518}
519
520static int
488ff207 521nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
438d99e3 522{
de8268c5 523 struct nvd0_mast *mast = nvd0_mast(nv_crtc->base.dev);
92854622 524 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
3376ee37 525 struct drm_crtc *crtc = &nv_crtc->base;
f3fdc52d 526 struct nouveau_connector *nv_connector;
92854622
BS
527 int mode = DRM_MODE_SCALE_NONE;
528 u32 oX, oY, *push;
f3fdc52d 529
92854622
BS
530 /* start off at the resolution we programmed the crtc for, this
531 * effectively handles NONE/FULL scaling
532 */
f3fdc52d 533 nv_connector = nouveau_crtc_connector_get(nv_crtc);
92854622
BS
534 if (nv_connector && nv_connector->native_mode)
535 mode = nv_connector->scaling_mode;
536
537 if (mode != DRM_MODE_SCALE_NONE)
538 omode = nv_connector->native_mode;
539 else
540 omode = umode;
541
542 oX = omode->hdisplay;
543 oY = omode->vdisplay;
544 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
545 oY *= 2;
546
547 /* add overscan compensation if necessary, will keep the aspect
548 * ratio the same as the backend mode unless overridden by the
549 * user setting both hborder and vborder properties.
550 */
551 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
552 (nv_connector->underscan == UNDERSCAN_AUTO &&
553 nv_connector->edid &&
554 drm_detect_hdmi_monitor(nv_connector->edid)))) {
555 u32 bX = nv_connector->underscan_hborder;
556 u32 bY = nv_connector->underscan_vborder;
557 u32 aspect = (oY << 19) / oX;
558
559 if (bX) {
560 oX -= (bX * 2);
561 if (bY) oY -= (bY * 2);
562 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
563 } else {
564 oX -= (oX >> 4) + 32;
565 if (bY) oY -= (bY * 2);
566 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
567 }
568 }
569
570 /* handle CENTER/ASPECT scaling, taking into account the areas
571 * removed already for overscan compensation
572 */
573 switch (mode) {
574 case DRM_MODE_SCALE_CENTER:
575 oX = min((u32)umode->hdisplay, oX);
576 oY = min((u32)umode->vdisplay, oY);
577 /* fall-through */
578 case DRM_MODE_SCALE_ASPECT:
579 if (oY < oX) {
580 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
581 oX = ((oY * aspect) + (aspect / 2)) >> 19;
582 } else {
583 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
584 oY = ((oX * aspect) + (aspect / 2)) >> 19;
f3fdc52d 585 }
92854622
BS
586 break;
587 default:
588 break;
f3fdc52d 589 }
438d99e3 590
de8268c5 591 push = evo_wait(mast, 8);
438d99e3 592 if (push) {
de8268c5
BS
593 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
594 /*XXX: SCALE_CTRL_ACTIVE??? */
595 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
596 evo_data(push, (oY << 16) | oX);
597 evo_data(push, (oY << 16) | oX);
598 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
599 evo_data(push, 0x00000000);
600 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
601 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
602 } else {
603 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
604 evo_data(push, (oY << 16) | oX);
605 evo_data(push, (oY << 16) | oX);
606 evo_data(push, (oY << 16) | oX);
607 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
608 evo_data(push, 0x00000000);
609 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
610 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
611 }
612
613 evo_kick(push, mast);
614
438d99e3 615 if (update) {
3376ee37
BS
616 nvd0_display_flip_stop(crtc);
617 nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
438d99e3 618 }
438d99e3
BS
619 }
620
621 return 0;
622}
623
624static int
625nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
626 int x, int y, bool update)
627{
628 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
de8268c5 629 struct nvd0_mast *mast = nvd0_mast(nv_crtc->base.dev);
438d99e3
BS
630 u32 *push;
631
de8268c5 632 push = evo_wait(mast, 16);
438d99e3 633 if (push) {
de8268c5
BS
634 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
635 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
636 evo_data(push, nvfb->nvbo->bo.offset >> 8);
637 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
638 evo_data(push, (fb->height << 16) | fb->width);
639 evo_data(push, nvfb->r_pitch);
640 evo_data(push, nvfb->r_format);
641 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
642 evo_data(push, (y << 16) | x);
643 if (nvd0_vers(mast) > NV50_DISP_MAST_CLASS) {
644 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
645 evo_data(push, nvfb->r_dma);
646 }
647 } else {
648 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
649 evo_data(push, nvfb->nvbo->bo.offset >> 8);
650 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
651 evo_data(push, (fb->height << 16) | fb->width);
652 evo_data(push, nvfb->r_pitch);
653 evo_data(push, nvfb->r_format);
654 evo_data(push, nvfb->r_dma);
655 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
656 evo_data(push, (y << 16) | x);
657 }
658
a46232ee
BS
659 if (update) {
660 evo_mthd(push, 0x0080, 1);
661 evo_data(push, 0x00000000);
662 }
de8268c5 663 evo_kick(push, mast);
438d99e3
BS
664 }
665
c0cc92a1 666 nv_crtc->fb.tile_flags = nvfb->r_dma;
438d99e3
BS
667 return 0;
668}
669
670static void
de8268c5 671nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
438d99e3 672{
de8268c5
BS
673 struct nvd0_mast *mast = nvd0_mast(nv_crtc->base.dev);
674 u32 *push = evo_wait(mast, 16);
438d99e3 675 if (push) {
de8268c5
BS
676 if (nvd0_vers(mast) < NV84_DISP_MAST_CLASS) {
677 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
678 evo_data(push, 0x85000000);
679 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
680 } else
681 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
682 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
683 evo_data(push, 0x85000000);
684 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
685 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
686 evo_data(push, NvEvoVRAM);
687 } else {
438d99e3
BS
688 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
689 evo_data(push, 0x85000000);
690 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
691 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
37b034a6 692 evo_data(push, NvEvoVRAM);
de8268c5
BS
693 }
694 evo_kick(push, mast);
695 }
696}
697
698static void
699nvd0_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
700{
701 struct nvd0_mast *mast = nvd0_mast(nv_crtc->base.dev);
702 u32 *push = evo_wait(mast, 16);
703 if (push) {
704 if (nvd0_vers(mast) < NV84_DISP_MAST_CLASS) {
705 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
706 evo_data(push, 0x05000000);
707 } else
708 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
709 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
710 evo_data(push, 0x05000000);
711 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
712 evo_data(push, 0x00000000);
438d99e3
BS
713 } else {
714 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
715 evo_data(push, 0x05000000);
716 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
717 evo_data(push, 0x00000000);
718 }
de8268c5
BS
719 evo_kick(push, mast);
720 }
721}
438d99e3 722
de8268c5
BS
723static void
724nvd0_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
725{
726 struct nvd0_mast *mast = nvd0_mast(nv_crtc->base.dev);
727
728 if (show)
729 nvd0_crtc_cursor_show(nv_crtc);
730 else
731 nvd0_crtc_cursor_hide(nv_crtc);
732
733 if (update) {
734 u32 *push = evo_wait(mast, 2);
735 if (push) {
438d99e3
BS
736 evo_mthd(push, 0x0080, 1);
737 evo_data(push, 0x00000000);
de8268c5 738 evo_kick(push, mast);
438d99e3 739 }
438d99e3
BS
740 }
741}
742
743static void
744nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
745{
746}
747
748static void
749nvd0_crtc_prepare(struct drm_crtc *crtc)
750{
751 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
de8268c5 752 struct nvd0_mast *mast = nvd0_mast(crtc->dev);
438d99e3
BS
753 u32 *push;
754
3376ee37
BS
755 nvd0_display_flip_stop(crtc);
756
de8268c5 757 push = evo_wait(mast, 2);
438d99e3 758 if (push) {
de8268c5
BS
759 if (nvd0_vers(mast) < NV84_DISP_MAST_CLASS) {
760 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
761 evo_data(push, 0x00000000);
762 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
763 evo_data(push, 0x40000000);
764 } else
765 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
766 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
767 evo_data(push, 0x00000000);
768 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
769 evo_data(push, 0x40000000);
770 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
771 evo_data(push, 0x00000000);
772 } else {
773 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
774 evo_data(push, 0x00000000);
775 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
776 evo_data(push, 0x03000000);
777 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
778 evo_data(push, 0x00000000);
779 }
780
781 evo_kick(push, mast);
438d99e3
BS
782 }
783
de8268c5 784 nvd0_crtc_cursor_show_hide(nv_crtc, false, false);
438d99e3
BS
785}
786
787static void
788nvd0_crtc_commit(struct drm_crtc *crtc)
789{
790 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
de8268c5 791 struct nvd0_mast *mast = nvd0_mast(crtc->dev);
438d99e3
BS
792 u32 *push;
793
de8268c5 794 push = evo_wait(mast, 32);
438d99e3 795 if (push) {
de8268c5
BS
796 if (nvd0_vers(mast) < NV84_DISP_MAST_CLASS) {
797 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
798 evo_data(push, NvEvoVRAM_LP);
799 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
800 evo_data(push, 0xc0000000);
801 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
802 } else
803 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
804 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
805 evo_data(push, nv_crtc->fb.tile_flags);
806 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
807 evo_data(push, 0xc0000000);
808 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
809 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
810 evo_data(push, NvEvoVRAM);
811 } else {
812 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
813 evo_data(push, nv_crtc->fb.tile_flags);
814 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
815 evo_data(push, 0x83000000);
816 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
817 evo_data(push, 0x00000000);
818 evo_data(push, 0x00000000);
819 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
820 evo_data(push, NvEvoVRAM);
821 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
822 evo_data(push, 0xffffff00);
823 }
824
825 evo_kick(push, mast);
438d99e3
BS
826 }
827
de8268c5 828 nvd0_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
3376ee37 829 nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
438d99e3
BS
830}
831
832static bool
e811f5ae 833nvd0_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
438d99e3
BS
834 struct drm_display_mode *adjusted_mode)
835{
836 return true;
837}
838
839static int
840nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
841{
842 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
843 int ret;
844
845 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
846 if (ret)
847 return ret;
848
849 if (old_fb) {
850 nvfb = nouveau_framebuffer(old_fb);
851 nouveau_bo_unpin(nvfb->nvbo);
852 }
853
854 return 0;
855}
856
857static int
858nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
859 struct drm_display_mode *mode, int x, int y,
860 struct drm_framebuffer *old_fb)
861{
de8268c5 862 struct nvd0_mast *mast = nvd0_mast(crtc->dev);
438d99e3
BS
863 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
864 struct nouveau_connector *nv_connector;
2d1d898b
BS
865 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
866 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
867 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
868 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
869 u32 vblan2e = 0, vblan2s = 1;
3488c57b 870 u32 *push;
438d99e3
BS
871 int ret;
872
2d1d898b
BS
873 hactive = mode->htotal;
874 hsynce = mode->hsync_end - mode->hsync_start - 1;
875 hbackp = mode->htotal - mode->hsync_end;
876 hblanke = hsynce + hbackp;
877 hfrontp = mode->hsync_start - mode->hdisplay;
878 hblanks = mode->htotal - hfrontp - 1;
879
880 vactive = mode->vtotal * vscan / ilace;
881 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
882 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
883 vblanke = vsynce + vbackp;
884 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
885 vblanks = vactive - vfrontp - 1;
886 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
887 vblan2e = vactive + vsynce + vbackp;
888 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
889 vactive = (vactive * 2) + 1;
2d1d898b
BS
890 }
891
438d99e3
BS
892 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
893 if (ret)
894 return ret;
895
de8268c5 896 push = evo_wait(mast, 64);
438d99e3 897 if (push) {
de8268c5
BS
898 if (nvd0_vers(mast) < NVD0_DISP_MAST_CLASS) {
899 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
900 evo_data(push, 0x00800000 | mode->clock);
901 evo_data(push, (ilace == 2) ? 2 : 0);
902 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
903 evo_data(push, 0x00000000);
904 evo_data(push, (vactive << 16) | hactive);
905 evo_data(push, ( vsynce << 16) | hsynce);
906 evo_data(push, (vblanke << 16) | hblanke);
907 evo_data(push, (vblanks << 16) | hblanks);
908 evo_data(push, (vblan2e << 16) | vblan2s);
909 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
910 evo_data(push, 0x00000000);
911 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
912 evo_data(push, 0x00000311);
913 evo_data(push, 0x00000100);
914 } else {
915 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
916 evo_data(push, 0x00000000);
917 evo_data(push, (vactive << 16) | hactive);
918 evo_data(push, ( vsynce << 16) | hsynce);
919 evo_data(push, (vblanke << 16) | hblanke);
920 evo_data(push, (vblanks << 16) | hblanks);
921 evo_data(push, (vblan2e << 16) | vblan2s);
922 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
923 evo_data(push, 0x00000000); /* ??? */
924 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
925 evo_data(push, mode->clock * 1000);
926 evo_data(push, 0x00200000); /* ??? */
927 evo_data(push, mode->clock * 1000);
928 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
929 evo_data(push, 0x00000311);
930 evo_data(push, 0x00000100);
931 }
932
933 evo_kick(push, mast);
438d99e3
BS
934 }
935
936 nv_connector = nouveau_crtc_connector_get(nv_crtc);
488ff207
BS
937 nvd0_crtc_set_dither(nv_crtc, false);
938 nvd0_crtc_set_scale(nv_crtc, false);
438d99e3
BS
939 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
940 return 0;
941}
942
943static int
944nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
945 struct drm_framebuffer *old_fb)
946{
77145f1c 947 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
438d99e3
BS
948 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
949 int ret;
950
84e2ad8b 951 if (!crtc->fb) {
77145f1c 952 NV_DEBUG(drm, "No FB bound\n");
84e2ad8b
BS
953 return 0;
954 }
955
438d99e3
BS
956 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
957 if (ret)
958 return ret;
959
3376ee37 960 nvd0_display_flip_stop(crtc);
438d99e3 961 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
3376ee37 962 nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
438d99e3
BS
963 return 0;
964}
965
966static int
967nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
968 struct drm_framebuffer *fb, int x, int y,
969 enum mode_set_atomic state)
970{
971 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
3376ee37 972 nvd0_display_flip_stop(crtc);
438d99e3
BS
973 nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
974 return 0;
975}
976
977static void
978nvd0_crtc_lut_load(struct drm_crtc *crtc)
979{
de8268c5 980 struct nvd0_disp *disp = nvd0_disp(crtc->dev);
438d99e3
BS
981 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
982 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
983 int i;
984
985 for (i = 0; i < 256; i++) {
de8268c5
BS
986 u16 r = nv_crtc->lut.r[i] >> 2;
987 u16 g = nv_crtc->lut.g[i] >> 2;
988 u16 b = nv_crtc->lut.b[i] >> 2;
989
990 if (nv_mclass(disp->core) < NVD0_DISP_CLASS) {
991 writew(r + 0x0000, lut + (i * 0x08) + 0);
992 writew(g + 0x0000, lut + (i * 0x08) + 2);
993 writew(b + 0x0000, lut + (i * 0x08) + 4);
994 } else {
995 writew(r + 0x6000, lut + (i * 0x20) + 0);
996 writew(g + 0x6000, lut + (i * 0x20) + 2);
997 writew(b + 0x6000, lut + (i * 0x20) + 4);
998 }
438d99e3
BS
999 }
1000}
1001
1002static int
1003nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
1004 uint32_t handle, uint32_t width, uint32_t height)
1005{
1006 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1007 struct drm_device *dev = crtc->dev;
1008 struct drm_gem_object *gem;
1009 struct nouveau_bo *nvbo;
1010 bool visible = (handle != 0);
1011 int i, ret = 0;
1012
1013 if (visible) {
1014 if (width != 64 || height != 64)
1015 return -EINVAL;
1016
1017 gem = drm_gem_object_lookup(dev, file_priv, handle);
1018 if (unlikely(!gem))
1019 return -ENOENT;
1020 nvbo = nouveau_gem_object(gem);
1021
1022 ret = nouveau_bo_map(nvbo);
1023 if (ret == 0) {
1024 for (i = 0; i < 64 * 64; i++) {
1025 u32 v = nouveau_bo_rd32(nvbo, i);
1026 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
1027 }
1028 nouveau_bo_unmap(nvbo);
1029 }
1030
1031 drm_gem_object_unreference_unlocked(gem);
1032 }
1033
1034 if (visible != nv_crtc->cursor.visible) {
de8268c5 1035 nvd0_crtc_cursor_show_hide(nv_crtc, visible, true);
438d99e3
BS
1036 nv_crtc->cursor.visible = visible;
1037 }
1038
1039 return ret;
1040}
1041
1042static int
1043nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1044{
b5a794b0
BS
1045 struct nvd0_curs *curs = nvd0_curs(crtc);
1046 struct nvd0_chan *chan = nvd0_chan(curs);
1047 nv_wo32(chan->user, 0x0084, (y << 16) | (x & 0xffff));
1048 nv_wo32(chan->user, 0x0080, 0x00000000);
438d99e3
BS
1049 return 0;
1050}
1051
1052static void
1053nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
1054 uint32_t start, uint32_t size)
1055{
1056 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1057 u32 end = max(start + size, (u32)256);
1058 u32 i;
1059
1060 for (i = start; i < end; i++) {
1061 nv_crtc->lut.r[i] = r[i];
1062 nv_crtc->lut.g[i] = g[i];
1063 nv_crtc->lut.b[i] = b[i];
1064 }
1065
1066 nvd0_crtc_lut_load(crtc);
1067}
1068
1069static void
1070nvd0_crtc_destroy(struct drm_crtc *crtc)
1071{
1072 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
b5a794b0
BS
1073 struct nvd0_disp *disp = nvd0_disp(crtc->dev);
1074 struct nvd0_head *head = nvd0_head(crtc);
1075 nvd0_dmac_destroy(disp->core, &head->ovly.base);
1076 nvd0_pioc_destroy(disp->core, &head->oimm.base);
1077 nvd0_dmac_destroy(disp->core, &head->sync.base);
1078 nvd0_pioc_destroy(disp->core, &head->curs.base);
438d99e3
BS
1079 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
1080 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
1081 nouveau_bo_unmap(nv_crtc->lut.nvbo);
1082 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
1083 drm_crtc_cleanup(crtc);
1084 kfree(crtc);
1085}
1086
1087static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
1088 .dpms = nvd0_crtc_dpms,
1089 .prepare = nvd0_crtc_prepare,
1090 .commit = nvd0_crtc_commit,
1091 .mode_fixup = nvd0_crtc_mode_fixup,
1092 .mode_set = nvd0_crtc_mode_set,
1093 .mode_set_base = nvd0_crtc_mode_set_base,
1094 .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
1095 .load_lut = nvd0_crtc_lut_load,
1096};
1097
1098static const struct drm_crtc_funcs nvd0_crtc_func = {
1099 .cursor_set = nvd0_crtc_cursor_set,
1100 .cursor_move = nvd0_crtc_cursor_move,
1101 .gamma_set = nvd0_crtc_gamma_set,
1102 .set_config = drm_crtc_helper_set_config,
1103 .destroy = nvd0_crtc_destroy,
3376ee37 1104 .page_flip = nouveau_crtc_page_flip,
438d99e3
BS
1105};
1106
c20ab3e1
BS
1107static void
1108nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
1109{
1110}
1111
1112static void
1113nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
1114{
1115}
1116
438d99e3 1117static int
b5a794b0 1118nvd0_crtc_create(struct drm_device *dev, struct nouveau_object *core, int index)
438d99e3 1119{
b5a794b0 1120 struct nvd0_disp *disp = nvd0_disp(dev);
dd0e3d53 1121 struct nvd0_head *head;
438d99e3
BS
1122 struct drm_crtc *crtc;
1123 int ret, i;
1124
dd0e3d53
BS
1125 head = kzalloc(sizeof(*head), GFP_KERNEL);
1126 if (!head)
438d99e3
BS
1127 return -ENOMEM;
1128
dd0e3d53
BS
1129 head->base.index = index;
1130 head->base.set_dither = nvd0_crtc_set_dither;
1131 head->base.set_scale = nvd0_crtc_set_scale;
1132 head->base.cursor.set_offset = nvd0_cursor_set_offset;
1133 head->base.cursor.set_pos = nvd0_cursor_set_pos;
438d99e3 1134 for (i = 0; i < 256; i++) {
dd0e3d53
BS
1135 head->base.lut.r[i] = i << 8;
1136 head->base.lut.g[i] = i << 8;
1137 head->base.lut.b[i] = i << 8;
438d99e3
BS
1138 }
1139
dd0e3d53 1140 crtc = &head->base.base;
438d99e3
BS
1141 drm_crtc_init(dev, crtc, &nvd0_crtc_func);
1142 drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
1143 drm_mode_crtc_set_gamma_size(crtc, 256);
1144
b5a794b0
BS
1145 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
1146 0, 0x0000, NULL, &head->base.lut.nvbo);
1147 if (!ret) {
1148 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
1149 if (!ret)
1150 ret = nouveau_bo_map(head->base.lut.nvbo);
1151 if (ret)
1152 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
1153 }
1154
1155 if (ret)
1156 goto out;
1157
1158 nvd0_crtc_lut_load(crtc);
1159
1160 /* allocate cursor resources */
1161 ret = nvd0_pioc_create(disp->core, NV50_DISP_CURS_CLASS, index,
1162 &(struct nv50_display_curs_class) {
1163 .head = index,
1164 }, sizeof(struct nv50_display_curs_class),
1165 &head->curs.base);
1166 if (ret)
1167 goto out;
1168
438d99e3 1169 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
dd0e3d53 1170 0, 0x0000, NULL, &head->base.cursor.nvbo);
438d99e3 1171 if (!ret) {
dd0e3d53 1172 ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
438d99e3 1173 if (!ret)
dd0e3d53 1174 ret = nouveau_bo_map(head->base.cursor.nvbo);
438d99e3 1175 if (ret)
dd0e3d53 1176 nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
438d99e3
BS
1177 }
1178
1179 if (ret)
1180 goto out;
1181
b5a794b0
BS
1182 /* allocate page flip / sync resources */
1183 ret = nvd0_dmac_create(disp->core, NV50_DISP_SYNC_CLASS, index,
1184 &(struct nv50_display_sync_class) {
1185 .pushbuf = EVO_PUSH_HANDLE(SYNC, index),
1186 .head = index,
1187 }, sizeof(struct nv50_display_sync_class),
1188 disp->sync->bo.offset, &head->sync.base);
1189 if (ret)
1190 goto out;
1191
1192 head->sync.sem.offset = EVO_SYNC(1 + index, 0x00);
438d99e3 1193
b5a794b0
BS
1194 /* allocate overlay resources */
1195 ret = nvd0_pioc_create(disp->core, NV50_DISP_OIMM_CLASS, index,
1196 &(struct nv50_display_oimm_class) {
1197 .head = index,
1198 }, sizeof(struct nv50_display_oimm_class),
1199 &head->oimm.base);
438d99e3
BS
1200 if (ret)
1201 goto out;
1202
b5a794b0
BS
1203 ret = nvd0_dmac_create(disp->core, NV50_DISP_OVLY_CLASS, index,
1204 &(struct nv50_display_ovly_class) {
1205 .pushbuf = EVO_PUSH_HANDLE(OVLY, index),
1206 .head = index,
1207 }, sizeof(struct nv50_display_ovly_class),
1208 disp->sync->bo.offset, &head->ovly.base);
1209 if (ret)
1210 goto out;
438d99e3
BS
1211
1212out:
1213 if (ret)
1214 nvd0_crtc_destroy(crtc);
1215 return ret;
1216}
1217
26f6d88b
BS
1218/******************************************************************************
1219 * DAC
1220 *****************************************************************************/
8eaa9669
BS
1221static void
1222nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
1223{
1224 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
35b21d39 1225 struct nvd0_disp *disp = nvd0_disp(encoder->dev);
8eaa9669
BS
1226 int or = nv_encoder->or;
1227 u32 dpms_ctrl;
1228
35b21d39 1229 dpms_ctrl = 0x00000000;
8eaa9669
BS
1230 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
1231 dpms_ctrl |= 0x00000001;
1232 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
1233 dpms_ctrl |= 0x00000004;
1234
35b21d39 1235 nv_call(disp->core, NV50_DISP_DAC_PWR + or, dpms_ctrl);
8eaa9669
BS
1236}
1237
1238static bool
e811f5ae
LP
1239nvd0_dac_mode_fixup(struct drm_encoder *encoder,
1240 const struct drm_display_mode *mode,
8eaa9669
BS
1241 struct drm_display_mode *adjusted_mode)
1242{
1243 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1244 struct nouveau_connector *nv_connector;
1245
1246 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1247 if (nv_connector && nv_connector->native_mode) {
1248 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1249 int id = adjusted_mode->base.id;
1250 *adjusted_mode = *nv_connector->native_mode;
1251 adjusted_mode->base.id = id;
1252 }
1253 }
1254
1255 return true;
1256}
1257
8eaa9669
BS
1258static void
1259nvd0_dac_commit(struct drm_encoder *encoder)
1260{
1261}
1262
1263static void
1264nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
1265 struct drm_display_mode *adjusted_mode)
1266{
1267 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1268 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3488c57b
BS
1269 u32 syncs, magic, *push;
1270
1271 syncs = 0x00000001;
1272 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1273 syncs |= 0x00000008;
1274 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1275 syncs |= 0x00000010;
1276
1277 magic = 0x31ec6000 | (nv_crtc->index << 25);
1278 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1279 magic |= 0x00000001;
8eaa9669
BS
1280
1281 nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
1282
b5a794b0 1283 push = evo_wait(nvd0_mast(encoder->dev), 8);
8eaa9669 1284 if (push) {
3488c57b
BS
1285 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1286 evo_data(push, syncs);
1287 evo_data(push, magic);
4a230fa6 1288 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
8eaa9669 1289 evo_data(push, 1 << nv_crtc->index);
b5a794b0 1290 evo_kick(push, nvd0_mast(encoder->dev));
8eaa9669
BS
1291 }
1292
1293 nv_encoder->crtc = encoder->crtc;
1294}
1295
1296static void
1297nvd0_dac_disconnect(struct drm_encoder *encoder)
1298{
1299 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1300 struct drm_device *dev = encoder->dev;
1301 u32 *push;
1302
1303 if (nv_encoder->crtc) {
1304 nvd0_crtc_prepare(nv_encoder->crtc);
1305
b5a794b0 1306 push = evo_wait(nvd0_mast(dev), 4);
8eaa9669
BS
1307 if (push) {
1308 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
1309 evo_data(push, 0x00000000);
1310 evo_mthd(push, 0x0080, 1);
1311 evo_data(push, 0x00000000);
b5a794b0 1312 evo_kick(push, nvd0_mast(dev));
8eaa9669
BS
1313 }
1314
1315 nv_encoder->crtc = NULL;
1316 }
1317}
1318
b6d8e7ec
BS
1319static enum drm_connector_status
1320nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
1321{
35b21d39
BS
1322 struct nvd0_disp *disp = nvd0_disp(encoder->dev);
1323 int ret, or = nouveau_encoder(encoder)->or;
7ebb38b5 1324 u32 load = 0;
b681993f 1325
35b21d39
BS
1326 ret = nv_exec(disp->core, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
1327 if (ret || load != 7)
1328 return connector_status_disconnected;
b681993f 1329
35b21d39 1330 return connector_status_connected;
b6d8e7ec
BS
1331}
1332
8eaa9669
BS
1333static void
1334nvd0_dac_destroy(struct drm_encoder *encoder)
1335{
1336 drm_encoder_cleanup(encoder);
1337 kfree(encoder);
1338}
1339
1340static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
1341 .dpms = nvd0_dac_dpms,
1342 .mode_fixup = nvd0_dac_mode_fixup,
4cbb0f8d 1343 .prepare = nvd0_dac_disconnect,
8eaa9669
BS
1344 .commit = nvd0_dac_commit,
1345 .mode_set = nvd0_dac_mode_set,
1346 .disable = nvd0_dac_disconnect,
1347 .get_crtc = nvd0_display_crtc_get,
b6d8e7ec 1348 .detect = nvd0_dac_detect
8eaa9669
BS
1349};
1350
1351static const struct drm_encoder_funcs nvd0_dac_func = {
1352 .destroy = nvd0_dac_destroy,
1353};
1354
1355static int
cb75d97e 1356nvd0_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
8eaa9669
BS
1357{
1358 struct drm_device *dev = connector->dev;
1359 struct nouveau_encoder *nv_encoder;
1360 struct drm_encoder *encoder;
1361
1362 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1363 if (!nv_encoder)
1364 return -ENOMEM;
1365 nv_encoder->dcb = dcbe;
1366 nv_encoder->or = ffs(dcbe->or) - 1;
1367
1368 encoder = to_drm_encoder(nv_encoder);
1369 encoder->possible_crtcs = dcbe->heads;
1370 encoder->possible_clones = 0;
1371 drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
1372 drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
1373
1374 drm_mode_connector_attach_encoder(connector, encoder);
1375 return 0;
1376}
26f6d88b 1377
78951d22
BS
1378/******************************************************************************
1379 * Audio
1380 *****************************************************************************/
1381static void
1382nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1383{
1384 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1385 struct nouveau_connector *nv_connector;
0a9e2b95 1386 struct nvd0_disp *disp = nvd0_disp(encoder->dev);
78951d22
BS
1387
1388 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1389 if (!drm_detect_monitor_audio(nv_connector->edid))
1390 return;
1391
78951d22 1392 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
78951d22 1393
0a9e2b95
BS
1394 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or,
1395 nv_connector->base.eld,
1396 nv_connector->base.eld[2] * 4);
78951d22
BS
1397}
1398
1399static void
1400nvd0_audio_disconnect(struct drm_encoder *encoder)
1401{
1402 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
0a9e2b95 1403 struct nvd0_disp *disp = nvd0_disp(encoder->dev);
78951d22 1404
0a9e2b95 1405 nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or, NULL, 0);
78951d22
BS
1406}
1407
1408/******************************************************************************
1409 * HDMI
1410 *****************************************************************************/
1411static void
1412nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1413{
64d9cc04
BS
1414 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1415 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1416 struct nouveau_connector *nv_connector;
1c30cd09
BS
1417 struct nvd0_disp *disp = nvd0_disp(encoder->dev);
1418 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
64d9cc04
BS
1419 u32 rekey = 56; /* binary driver, and tegra constant */
1420 u32 max_ac_packet;
1421
1422 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1423 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1424 return;
1425
1426 max_ac_packet = mode->htotal - mode->hdisplay;
1427 max_ac_packet -= rekey;
1428 max_ac_packet -= 18; /* constant from tegra */
1429 max_ac_packet /= 32;
1430
1c30cd09
BS
1431 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff,
1432 NV84_DISP_SOR_HDMI_PWR_STATE_ON |
1433 (max_ac_packet << 16) | rekey);
091e40cd 1434
78951d22
BS
1435 nvd0_audio_mode_set(encoder, mode);
1436}
1437
1438static void
1439nvd0_hdmi_disconnect(struct drm_encoder *encoder)
1440{
64d9cc04
BS
1441 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1442 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1c30cd09
BS
1443 struct nvd0_disp *disp = nvd0_disp(encoder->dev);
1444 const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
64d9cc04 1445
78951d22 1446 nvd0_audio_disconnect(encoder);
64d9cc04 1447
1c30cd09 1448 nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff, 0x00000000);
78951d22
BS
1449}
1450
26f6d88b
BS
1451/******************************************************************************
1452 * SOR
1453 *****************************************************************************/
83fc083c
BS
1454static void
1455nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
1456{
1457 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1458 struct drm_device *dev = encoder->dev;
74b66850 1459 struct nvd0_disp *disp = nvd0_disp(dev);
83fc083c
BS
1460 struct drm_encoder *partner;
1461 int or = nv_encoder->or;
83fc083c
BS
1462
1463 nv_encoder->last_dpms = mode;
1464
1465 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1466 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1467
1468 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1469 continue;
1470
1471 if (nv_partner != nv_encoder &&
26cfa813 1472 nv_partner->dcb->or == nv_encoder->dcb->or) {
83fc083c
BS
1473 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1474 return;
1475 break;
1476 }
1477 }
1478
74b66850 1479 nv_call(disp->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
6e83fda2 1480
6c8e4633
BS
1481 if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1482 nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, disp->core);
83fc083c
BS
1483}
1484
1485static bool
e811f5ae
LP
1486nvd0_sor_mode_fixup(struct drm_encoder *encoder,
1487 const struct drm_display_mode *mode,
83fc083c
BS
1488 struct drm_display_mode *adjusted_mode)
1489{
1490 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1491 struct nouveau_connector *nv_connector;
1492
1493 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1494 if (nv_connector && nv_connector->native_mode) {
1495 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1496 int id = adjusted_mode->base.id;
1497 *adjusted_mode = *nv_connector->native_mode;
1498 adjusted_mode->base.id = id;
1499 }
1500 }
1501
1502 return true;
1503}
1504
4cbb0f8d
BS
1505static void
1506nvd0_sor_disconnect(struct drm_encoder *encoder)
1507{
1508 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1509 struct drm_device *dev = encoder->dev;
1510 u32 *push;
1511
1512 if (nv_encoder->crtc) {
1513 nvd0_crtc_prepare(nv_encoder->crtc);
1514
b5a794b0 1515 push = evo_wait(nvd0_mast(dev), 4);
4cbb0f8d
BS
1516 if (push) {
1517 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1518 evo_data(push, 0x00000000);
1519 evo_mthd(push, 0x0080, 1);
1520 evo_data(push, 0x00000000);
b5a794b0 1521 evo_kick(push, nvd0_mast(dev));
4cbb0f8d
BS
1522 }
1523
1524 nvd0_hdmi_disconnect(encoder);
1525
1526 nv_encoder->crtc = NULL;
1527 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1528 }
1529}
1530
83fc083c
BS
1531static void
1532nvd0_sor_prepare(struct drm_encoder *encoder)
1533{
4cbb0f8d 1534 nvd0_sor_disconnect(encoder);
cb75d97e 1535 if (nouveau_encoder(encoder)->dcb->type == DCB_OUTPUT_DP)
b5a794b0 1536 evo_sync(encoder->dev);
83fc083c
BS
1537}
1538
1539static void
1540nvd0_sor_commit(struct drm_encoder *encoder)
1541{
1542}
1543
1544static void
3b6d83d1
BS
1545nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
1546 struct drm_display_mode *mode)
83fc083c 1547{
4a230fa6 1548 struct nvd0_disp *disp = nvd0_disp(encoder->dev);
78951d22 1549 struct drm_device *dev = encoder->dev;
77145f1c 1550 struct nouveau_drm *drm = nouveau_drm(dev);
83fc083c
BS
1551 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1552 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
3b6d83d1 1553 struct nouveau_connector *nv_connector;
77145f1c 1554 struct nvbios *bios = &drm->vbios;
4a230fa6 1555 int or = nv_encoder->or;
83fc083c 1556 u32 mode_ctrl = (1 << nv_crtc->index);
3488c57b
BS
1557 u32 syncs, magic, *push;
1558 u32 or_config;
1559
1560 syncs = 0x00000001;
1561 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1562 syncs |= 0x00000008;
1563 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1564 syncs |= 0x00000010;
1565
1566 magic = 0x31ec6000 | (nv_crtc->index << 25);
1567 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1568 magic |= 0x00000001;
83fc083c 1569
3b6d83d1
BS
1570 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1571 switch (nv_encoder->dcb->type) {
cb75d97e 1572 case DCB_OUTPUT_TMDS:
3b6d83d1
BS
1573 if (nv_encoder->dcb->sorconf.link & 1) {
1574 if (mode->clock < 165000)
1575 mode_ctrl |= 0x00000100;
1576 else
1577 mode_ctrl |= 0x00000500;
1578 } else {
1579 mode_ctrl |= 0x00000200;
1580 }
1581
78951d22 1582 nvd0_hdmi_mode_set(encoder, mode);
3b6d83d1 1583 break;
cb75d97e 1584 case DCB_OUTPUT_LVDS:
3b6d83d1
BS
1585 or_config = (mode_ctrl & 0x00000f00) >> 8;
1586 if (bios->fp_no_ddc) {
1587 if (bios->fp.dual_link)
1588 or_config |= 0x0100;
1589 if (bios->fp.if_is_24bit)
1590 or_config |= 0x0200;
1591 } else {
befb51e9 1592 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
3b6d83d1
BS
1593 if (((u8 *)nv_connector->edid)[121] == 2)
1594 or_config |= 0x0100;
1595 } else
1596 if (mode->clock >= bios->fp.duallink_transition_clk) {
1597 or_config |= 0x0100;
1598 }
83fc083c 1599
3b6d83d1
BS
1600 if (or_config & 0x0100) {
1601 if (bios->fp.strapless_is_24bit & 2)
1602 or_config |= 0x0200;
1603 } else {
1604 if (bios->fp.strapless_is_24bit & 1)
1605 or_config |= 0x0200;
1606 }
1607
1608 if (nv_connector->base.display_info.bpc == 8)
1609 or_config |= 0x0200;
3b6d83d1 1610 }
4a230fa6
BS
1611
1612 nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + or, or_config);
3b6d83d1 1613 break;
cb75d97e 1614 case DCB_OUTPUT_DP:
3488c57b 1615 if (nv_connector->base.display_info.bpc == 6) {
6e83fda2 1616 nv_encoder->dp.datarate = mode->clock * 18 / 8;
a348cd5f 1617 syncs |= 0x00000002 << 6;
3488c57b 1618 } else {
6e83fda2 1619 nv_encoder->dp.datarate = mode->clock * 24 / 8;
a348cd5f 1620 syncs |= 0x00000005 << 6;
3488c57b 1621 }
6e83fda2
BS
1622
1623 if (nv_encoder->dcb->sorconf.link & 1)
1624 mode_ctrl |= 0x00000800;
1625 else
1626 mode_ctrl |= 0x00000900;
6e83fda2 1627 break;
3b6d83d1
BS
1628 default:
1629 BUG_ON(1);
1630 break;
1631 }
ff8ff503 1632
83fc083c
BS
1633 nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
1634
b5a794b0 1635 push = evo_wait(nvd0_mast(dev), 8);
83fc083c 1636 if (push) {
3488c57b
BS
1637 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1638 evo_data(push, syncs);
1639 evo_data(push, magic);
4a230fa6 1640 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 1);
83fc083c 1641 evo_data(push, mode_ctrl);
b5a794b0 1642 evo_kick(push, nvd0_mast(dev));
83fc083c
BS
1643 }
1644
1645 nv_encoder->crtc = encoder->crtc;
1646}
1647
83fc083c
BS
1648static void
1649nvd0_sor_destroy(struct drm_encoder *encoder)
1650{
1651 drm_encoder_cleanup(encoder);
1652 kfree(encoder);
1653}
1654
1655static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
1656 .dpms = nvd0_sor_dpms,
1657 .mode_fixup = nvd0_sor_mode_fixup,
1658 .prepare = nvd0_sor_prepare,
1659 .commit = nvd0_sor_commit,
1660 .mode_set = nvd0_sor_mode_set,
1661 .disable = nvd0_sor_disconnect,
1662 .get_crtc = nvd0_display_crtc_get,
1663};
1664
1665static const struct drm_encoder_funcs nvd0_sor_func = {
1666 .destroy = nvd0_sor_destroy,
1667};
1668
1669static int
cb75d97e 1670nvd0_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
83fc083c
BS
1671{
1672 struct drm_device *dev = connector->dev;
1673 struct nouveau_encoder *nv_encoder;
1674 struct drm_encoder *encoder;
1675
1676 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1677 if (!nv_encoder)
1678 return -ENOMEM;
1679 nv_encoder->dcb = dcbe;
1680 nv_encoder->or = ffs(dcbe->or) - 1;
1681 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1682
1683 encoder = to_drm_encoder(nv_encoder);
1684 encoder->possible_crtcs = dcbe->heads;
1685 encoder->possible_clones = 0;
1686 drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
1687 drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
1688
1689 drm_mode_connector_attach_encoder(connector, encoder);
1690 return 0;
1691}
26f6d88b 1692
26f6d88b
BS
1693/******************************************************************************
1694 * Init
1695 *****************************************************************************/
2a44e499 1696void
26f6d88b
BS
1697nvd0_display_fini(struct drm_device *dev)
1698{
26f6d88b
BS
1699}
1700
1701int
1702nvd0_display_init(struct drm_device *dev)
1703{
b5a794b0
BS
1704 u32 *push = evo_wait(nvd0_mast(dev), 32);
1705 if (push) {
1706 evo_mthd(push, 0x0088, 1);
1707 evo_data(push, NvEvoSync);
1708 evo_mthd(push, 0x0084, 1);
1709 evo_data(push, 0x00000000);
1710 evo_mthd(push, 0x0084, 1);
1711 evo_data(push, 0x80000000);
1712 evo_mthd(push, 0x008c, 1);
1713 evo_data(push, 0x00000000);
1714 evo_kick(push, nvd0_mast(dev));
1715 return 0;
bdb8c212 1716 }
efd272a7 1717
b5a794b0 1718 return -EBUSY;
26f6d88b
BS
1719}
1720
1721void
1722nvd0_display_destroy(struct drm_device *dev)
1723{
94e5c39b 1724 struct nvd0_disp *disp = nvd0_disp(dev);
bdb8c212 1725
b5a794b0 1726 nvd0_dmac_destroy(disp->core, &disp->mast.base);
26f6d88b 1727
816af2f2
BS
1728 nouveau_bo_unmap(disp->sync);
1729 nouveau_bo_ref(NULL, &disp->sync);
51beb428 1730
77145f1c 1731 nouveau_display(dev)->priv = NULL;
26f6d88b
BS
1732 kfree(disp);
1733}
1734
1735int
1736nvd0_display_create(struct drm_device *dev)
1737{
b5a794b0
BS
1738 static const u16 oclass[] = {
1739 NVE0_DISP_CLASS,
1740 NVD0_DISP_CLASS,
1741 };
77145f1c
BS
1742 struct nouveau_device *device = nouveau_dev(dev);
1743 struct nouveau_drm *drm = nouveau_drm(dev);
77145f1c 1744 struct dcb_table *dcb = &drm->vbios.dcb;
83fc083c 1745 struct drm_connector *connector, *tmp;
94e5c39b 1746 struct nvd0_disp *disp;
cb75d97e 1747 struct dcb_output *dcbe;
7c5f6a87 1748 int crtcs, ret, i;
26f6d88b
BS
1749
1750 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
1751 if (!disp)
1752 return -ENOMEM;
77145f1c
BS
1753
1754 nouveau_display(dev)->priv = disp;
1755 nouveau_display(dev)->dtor = nvd0_display_destroy;
1756 nouveau_display(dev)->init = nvd0_display_init;
1757 nouveau_display(dev)->fini = nvd0_display_fini;
26f6d88b 1758
b5a794b0
BS
1759 /* small shared memory area we use for notifiers and semaphores */
1760 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
1761 0, 0x0000, NULL, &disp->sync);
1762 if (!ret) {
1763 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
1764 if (!ret)
1765 ret = nouveau_bo_map(disp->sync);
1766 if (ret)
1767 nouveau_bo_ref(NULL, &disp->sync);
1768 }
1769
1770 if (ret)
1771 goto out;
1772
1773 /* attempt to allocate a supported evo display class */
1774 ret = -ENODEV;
1775 for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
1776 ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
1777 0xd1500000, oclass[i], NULL, 0,
1778 &disp->core);
1779 }
1780
1781 if (ret)
1782 goto out;
1783
1784 /* allocate master evo channel */
1785 ret = nvd0_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
1786 &(struct nv50_display_mast_class) {
1787 .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
1788 }, sizeof(struct nv50_display_mast_class),
1789 disp->sync->bo.offset, &disp->mast.base);
1790 if (ret)
1791 goto out;
1792
438d99e3 1793 /* create crtc objects to represent the hw heads */
77145f1c 1794 crtcs = nv_rd32(device, 0x022448);
7c5f6a87 1795 for (i = 0; i < crtcs; i++) {
b5a794b0 1796 ret = nvd0_crtc_create(dev, disp->core, i);
438d99e3
BS
1797 if (ret)
1798 goto out;
1799 }
1800
83fc083c
BS
1801 /* create encoder/connector objects based on VBIOS DCB table */
1802 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
1803 connector = nouveau_connector_create(dev, dcbe->connector);
1804 if (IS_ERR(connector))
1805 continue;
1806
1807 if (dcbe->location != DCB_LOC_ON_CHIP) {
77145f1c 1808 NV_WARN(drm, "skipping off-chip encoder %d/%d\n",
83fc083c
BS
1809 dcbe->type, ffs(dcbe->or) - 1);
1810 continue;
1811 }
1812
1813 switch (dcbe->type) {
cb75d97e
BS
1814 case DCB_OUTPUT_TMDS:
1815 case DCB_OUTPUT_LVDS:
1816 case DCB_OUTPUT_DP:
83fc083c
BS
1817 nvd0_sor_create(connector, dcbe);
1818 break;
cb75d97e 1819 case DCB_OUTPUT_ANALOG:
8eaa9669
BS
1820 nvd0_dac_create(connector, dcbe);
1821 break;
83fc083c 1822 default:
77145f1c 1823 NV_WARN(drm, "skipping unsupported encoder %d/%d\n",
83fc083c
BS
1824 dcbe->type, ffs(dcbe->or) - 1);
1825 continue;
1826 }
1827 }
1828
1829 /* cull any connectors we created that don't have an encoder */
1830 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
1831 if (connector->encoder_ids[0])
1832 continue;
1833
77145f1c 1834 NV_WARN(drm, "%s has no encoders, removing\n",
83fc083c
BS
1835 drm_get_connector_name(connector));
1836 connector->funcs->destroy(connector);
1837 }
1838
26f6d88b
BS
1839out:
1840 if (ret)
1841 nvd0_display_destroy(dev);
1842 return ret;
1843}