2 * Copyright 2011 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #include <linux/dma-mapping.h>
28 #include <drm/drm_crtc_helper.h>
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_dp_helper.h>
31 #include <drm/drm_fb_helper.h>
33 #include <nvif/class.h>
34 #include <nvif/cl0002.h>
35 #include <nvif/cl5070.h>
36 #include <nvif/cl507a.h>
37 #include <nvif/cl507b.h>
38 #include <nvif/cl507c.h>
39 #include <nvif/cl507d.h>
40 #include <nvif/cl507e.h>
42 #include "nouveau_drv.h"
43 #include "nouveau_dma.h"
44 #include "nouveau_gem.h"
45 #include "nouveau_connector.h"
46 #include "nouveau_encoder.h"
47 #include "nouveau_crtc.h"
48 #include "nouveau_fence.h"
49 #include "nv50_display.h"
53 #define EVO_MASTER (0x00)
54 #define EVO_FLIP(c) (0x01 + (c))
55 #define EVO_OVLY(c) (0x05 + (c))
56 #define EVO_OIMM(c) (0x09 + (c))
57 #define EVO_CURS(c) (0x0d + (c))
59 /* offsets in shared sync bo of various structures */
60 #define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
61 #define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
62 #define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
63 #define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
65 /******************************************************************************
67 *****************************************************************************/
70 struct nvif_object user;
71 struct nvif_device *device;
75 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
76 const s32 *oclass, u8 head, void *data, u32 size,
77 struct nv50_chan *chan)
79 struct nvif_sclass *sclass;
82 chan->device = device;
84 ret = n = nvif_object_sclass_get(disp, &sclass);
89 for (i = 0; i < n; i++) {
90 if (sclass[i].oclass == oclass[0]) {
91 ret = nvif_object_init(disp, 0, oclass[0],
92 data, size, &chan->user);
94 nvif_object_map(&chan->user);
95 nvif_object_sclass_put(&sclass);
102 nvif_object_sclass_put(&sclass);
107 nv50_chan_destroy(struct nv50_chan *chan)
109 nvif_object_fini(&chan->user);
112 /******************************************************************************
114 *****************************************************************************/
117 struct nv50_chan base;
121 nv50_pioc_destroy(struct nv50_pioc *pioc)
123 nv50_chan_destroy(&pioc->base);
127 nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
128 const s32 *oclass, u8 head, void *data, u32 size,
129 struct nv50_pioc *pioc)
131 return nv50_chan_create(device, disp, oclass, head, data, size,
135 /******************************************************************************
137 *****************************************************************************/
140 struct nv50_pioc base;
144 nv50_curs_create(struct nvif_device *device, struct nvif_object *disp,
145 int head, struct nv50_curs *curs)
147 struct nv50_disp_cursor_v0 args = {
150 static const s32 oclass[] = {
159 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
163 /******************************************************************************
165 *****************************************************************************/
168 struct nv50_pioc base;
172 nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
173 int head, struct nv50_oimm *oimm)
175 struct nv50_disp_cursor_v0 args = {
178 static const s32 oclass[] = {
187 return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
191 /******************************************************************************
193 *****************************************************************************/
196 struct nv50_chan base;
200 struct nvif_object sync;
201 struct nvif_object vram;
203 /* Protects against concurrent pushbuf access to this channel, lock is
204 * grabbed by evo_wait (if the pushbuf reservation is successful) and
205 * dropped again by evo_kick. */
210 nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
212 struct nvif_device *device = dmac->base.device;
214 nvif_object_fini(&dmac->vram);
215 nvif_object_fini(&dmac->sync);
217 nv50_chan_destroy(&dmac->base);
220 struct device *dev = nvxx_device(device)->dev;
221 dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
226 nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
227 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
228 struct nv50_dmac *dmac)
230 struct nv50_disp_core_channel_dma_v0 *args = data;
231 struct nvif_object pushbuf;
234 mutex_init(&dmac->lock);
236 dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
237 &dmac->handle, GFP_KERNEL);
241 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
242 &(struct nv_dma_v0) {
243 .target = NV_DMA_V0_TARGET_PCI_US,
244 .access = NV_DMA_V0_ACCESS_RD,
245 .start = dmac->handle + 0x0000,
246 .limit = dmac->handle + 0x0fff,
247 }, sizeof(struct nv_dma_v0), &pushbuf);
251 args->pushbuf = nvif_handle(&pushbuf);
253 ret = nv50_chan_create(device, disp, oclass, head, data, size,
255 nvif_object_fini(&pushbuf);
259 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
260 &(struct nv_dma_v0) {
261 .target = NV_DMA_V0_TARGET_VRAM,
262 .access = NV_DMA_V0_ACCESS_RDWR,
263 .start = syncbuf + 0x0000,
264 .limit = syncbuf + 0x0fff,
265 }, sizeof(struct nv_dma_v0),
270 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
271 &(struct nv_dma_v0) {
272 .target = NV_DMA_V0_TARGET_VRAM,
273 .access = NV_DMA_V0_ACCESS_RDWR,
275 .limit = device->info.ram_user - 1,
276 }, sizeof(struct nv_dma_v0),
284 /******************************************************************************
286 *****************************************************************************/
289 struct nv50_dmac base;
293 nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
294 u64 syncbuf, struct nv50_mast *core)
296 struct nv50_disp_core_channel_dma_v0 args = {
297 .pushbuf = 0xb0007d00,
299 static const s32 oclass[] = {
300 GP100_DISP_CORE_CHANNEL_DMA,
301 GM200_DISP_CORE_CHANNEL_DMA,
302 GM107_DISP_CORE_CHANNEL_DMA,
303 GK110_DISP_CORE_CHANNEL_DMA,
304 GK104_DISP_CORE_CHANNEL_DMA,
305 GF110_DISP_CORE_CHANNEL_DMA,
306 GT214_DISP_CORE_CHANNEL_DMA,
307 GT206_DISP_CORE_CHANNEL_DMA,
308 GT200_DISP_CORE_CHANNEL_DMA,
309 G82_DISP_CORE_CHANNEL_DMA,
310 NV50_DISP_CORE_CHANNEL_DMA,
314 return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
315 syncbuf, &core->base);
318 /******************************************************************************
320 *****************************************************************************/
323 struct nv50_dmac base;
329 nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
330 int head, u64 syncbuf, struct nv50_sync *base)
332 struct nv50_disp_base_channel_dma_v0 args = {
333 .pushbuf = 0xb0007c00 | head,
336 static const s32 oclass[] = {
337 GK110_DISP_BASE_CHANNEL_DMA,
338 GK104_DISP_BASE_CHANNEL_DMA,
339 GF110_DISP_BASE_CHANNEL_DMA,
340 GT214_DISP_BASE_CHANNEL_DMA,
341 GT200_DISP_BASE_CHANNEL_DMA,
342 G82_DISP_BASE_CHANNEL_DMA,
343 NV50_DISP_BASE_CHANNEL_DMA,
347 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
348 syncbuf, &base->base);
351 /******************************************************************************
353 *****************************************************************************/
356 struct nv50_dmac base;
360 nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
361 int head, u64 syncbuf, struct nv50_ovly *ovly)
363 struct nv50_disp_overlay_channel_dma_v0 args = {
364 .pushbuf = 0xb0007e00 | head,
367 static const s32 oclass[] = {
368 GK104_DISP_OVERLAY_CONTROL_DMA,
369 GF110_DISP_OVERLAY_CONTROL_DMA,
370 GT214_DISP_OVERLAY_CHANNEL_DMA,
371 GT200_DISP_OVERLAY_CHANNEL_DMA,
372 G82_DISP_OVERLAY_CHANNEL_DMA,
373 NV50_DISP_OVERLAY_CHANNEL_DMA,
377 return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
378 syncbuf, &ovly->base);
382 struct nouveau_crtc base;
383 struct nouveau_bo *image;
384 struct nv50_curs curs;
385 struct nv50_sync sync;
386 struct nv50_ovly ovly;
387 struct nv50_oimm oimm;
390 #define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
391 #define nv50_curs(c) (&nv50_head(c)->curs)
392 #define nv50_sync(c) (&nv50_head(c)->sync)
393 #define nv50_ovly(c) (&nv50_head(c)->ovly)
394 #define nv50_oimm(c) (&nv50_head(c)->oimm)
395 #define nv50_chan(c) (&(c)->base.base)
396 #define nv50_vers(c) nv50_chan(c)->user.oclass
399 struct list_head head;
400 struct nvif_object core;
401 struct nvif_object base[4];
405 struct nvif_object *disp;
406 struct nv50_mast mast;
408 struct list_head fbdma;
410 struct nouveau_bo *sync;
413 static struct nv50_disp *
414 nv50_disp(struct drm_device *dev)
416 return nouveau_display(dev)->priv;
419 #define nv50_mast(d) (&nv50_disp(d)->mast)
421 static struct drm_crtc *
422 nv50_display_crtc_get(struct drm_encoder *encoder)
424 return nouveau_encoder(encoder)->crtc;
427 /******************************************************************************
428 * EVO channel helpers
429 *****************************************************************************/
431 evo_wait(void *evoc, int nr)
433 struct nv50_dmac *dmac = evoc;
434 struct nvif_device *device = dmac->base.device;
435 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
437 mutex_lock(&dmac->lock);
438 if (put + nr >= (PAGE_SIZE / 4) - 8) {
439 dmac->ptr[put] = 0x20000000;
441 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
442 if (nvif_msec(device, 2000,
443 if (!nvif_rd32(&dmac->base.user, 0x0004))
446 mutex_unlock(&dmac->lock);
447 printk(KERN_ERR "nouveau: evo channel stalled\n");
454 return dmac->ptr + put;
458 evo_kick(u32 *push, void *evoc)
460 struct nv50_dmac *dmac = evoc;
461 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
462 mutex_unlock(&dmac->lock);
466 #define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
467 #define evo_data(p,d) *((p)++) = (d)
469 #define evo_mthd(p,m,s) do { \
470 const u32 _m = (m), _s = (s); \
471 printk(KERN_ERR "%04x %d %s\n", _m, _s, __func__); \
472 *((p)++) = ((_s << 18) | _m); \
474 #define evo_data(p,d) do { \
475 const u32 _d = (d); \
476 printk(KERN_ERR "\t%08x\n", _d); \
482 evo_sync_wait(void *data)
484 if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
491 evo_sync(struct drm_device *dev)
493 struct nvif_device *device = &nouveau_drm(dev)->device;
494 struct nv50_disp *disp = nv50_disp(dev);
495 struct nv50_mast *mast = nv50_mast(dev);
496 u32 *push = evo_wait(mast, 8);
498 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
499 evo_mthd(push, 0x0084, 1);
500 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
501 evo_mthd(push, 0x0080, 2);
502 evo_data(push, 0x00000000);
503 evo_data(push, 0x00000000);
504 evo_kick(push, mast);
505 if (nvif_msec(device, 2000,
506 if (evo_sync_wait(disp->sync))
515 /******************************************************************************
516 * Page flipping channel
517 *****************************************************************************/
519 nv50_display_crtc_sema(struct drm_device *dev, int crtc)
521 return nv50_disp(dev)->sync;
524 struct nv50_display_flip {
525 struct nv50_disp *disp;
526 struct nv50_sync *chan;
530 nv50_display_flip_wait(void *data)
532 struct nv50_display_flip *flip = data;
533 if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
541 nv50_display_flip_stop(struct drm_crtc *crtc)
543 struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
544 struct nv50_display_flip flip = {
545 .disp = nv50_disp(crtc->dev),
546 .chan = nv50_sync(crtc),
550 push = evo_wait(flip.chan, 8);
552 evo_mthd(push, 0x0084, 1);
553 evo_data(push, 0x00000000);
554 evo_mthd(push, 0x0094, 1);
555 evo_data(push, 0x00000000);
556 evo_mthd(push, 0x00c0, 1);
557 evo_data(push, 0x00000000);
558 evo_mthd(push, 0x0080, 1);
559 evo_data(push, 0x00000000);
560 evo_kick(push, flip.chan);
563 nvif_msec(device, 2000,
564 if (nv50_display_flip_wait(&flip))
570 nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
571 struct nouveau_channel *chan, u32 swap_interval)
573 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
574 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
575 struct nv50_head *head = nv50_head(crtc);
576 struct nv50_sync *sync = nv50_sync(crtc);
580 if (crtc->primary->fb->width != fb->width ||
581 crtc->primary->fb->height != fb->height)
585 if (swap_interval == 0)
586 swap_interval |= 0x100;
590 push = evo_wait(sync, 128);
591 if (unlikely(push == NULL))
594 if (chan && chan->user.oclass < G82_CHANNEL_GPFIFO) {
595 ret = RING_SPACE(chan, 8);
599 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
600 OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
601 OUT_RING (chan, sync->addr ^ 0x10);
602 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
603 OUT_RING (chan, sync->data + 1);
604 BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
605 OUT_RING (chan, sync->addr);
606 OUT_RING (chan, sync->data);
608 if (chan && chan->user.oclass < FERMI_CHANNEL_GPFIFO) {
609 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
610 ret = RING_SPACE(chan, 12);
614 BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
615 OUT_RING (chan, chan->vram.handle);
616 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
617 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
618 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
619 OUT_RING (chan, sync->data + 1);
620 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
621 BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
622 OUT_RING (chan, upper_32_bits(addr));
623 OUT_RING (chan, lower_32_bits(addr));
624 OUT_RING (chan, sync->data);
625 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
628 u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
629 ret = RING_SPACE(chan, 10);
633 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
634 OUT_RING (chan, upper_32_bits(addr ^ 0x10));
635 OUT_RING (chan, lower_32_bits(addr ^ 0x10));
636 OUT_RING (chan, sync->data + 1);
637 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
638 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
639 BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
640 OUT_RING (chan, upper_32_bits(addr));
641 OUT_RING (chan, lower_32_bits(addr));
642 OUT_RING (chan, sync->data);
643 OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
644 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
654 evo_mthd(push, 0x0100, 1);
655 evo_data(push, 0xfffe0000);
656 evo_mthd(push, 0x0084, 1);
657 evo_data(push, swap_interval);
658 if (!(swap_interval & 0x00000100)) {
659 evo_mthd(push, 0x00e0, 1);
660 evo_data(push, 0x40000000);
662 evo_mthd(push, 0x0088, 4);
663 evo_data(push, sync->addr);
664 evo_data(push, sync->data++);
665 evo_data(push, sync->data);
666 evo_data(push, sync->base.sync.handle);
667 evo_mthd(push, 0x00a0, 2);
668 evo_data(push, 0x00000000);
669 evo_data(push, 0x00000000);
670 evo_mthd(push, 0x00c0, 1);
671 evo_data(push, nv_fb->r_handle);
672 evo_mthd(push, 0x0110, 2);
673 evo_data(push, 0x00000000);
674 evo_data(push, 0x00000000);
675 if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
676 evo_mthd(push, 0x0800, 5);
677 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
679 evo_data(push, (fb->height << 16) | fb->width);
680 evo_data(push, nv_fb->r_pitch);
681 evo_data(push, nv_fb->r_format);
683 evo_mthd(push, 0x0400, 5);
684 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
686 evo_data(push, (fb->height << 16) | fb->width);
687 evo_data(push, nv_fb->r_pitch);
688 evo_data(push, nv_fb->r_format);
690 evo_mthd(push, 0x0080, 1);
691 evo_data(push, 0x00000000);
692 evo_kick(push, sync);
694 nouveau_bo_ref(nv_fb->nvbo, &head->image);
698 /******************************************************************************
700 *****************************************************************************/
702 nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
704 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
705 struct nouveau_connector *nv_connector;
706 struct drm_connector *connector;
707 u32 *push, mode = 0x00;
709 nv_connector = nouveau_crtc_connector_get(nv_crtc);
710 connector = &nv_connector->base;
711 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
712 if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
713 mode = DITHERING_MODE_DYNAMIC2X2;
715 mode = nv_connector->dithering_mode;
718 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
719 if (connector->display_info.bpc >= 8)
720 mode |= DITHERING_DEPTH_8BPC;
722 mode |= nv_connector->dithering_depth;
725 push = evo_wait(mast, 4);
727 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
728 evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
729 evo_data(push, mode);
731 if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
732 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
733 evo_data(push, mode);
735 evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
736 evo_data(push, mode);
740 evo_mthd(push, 0x0080, 1);
741 evo_data(push, 0x00000000);
743 evo_kick(push, mast);
750 nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
752 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
753 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
754 struct drm_crtc *crtc = &nv_crtc->base;
755 struct nouveau_connector *nv_connector;
756 int mode = DRM_MODE_SCALE_NONE;
759 /* start off at the resolution we programmed the crtc for, this
760 * effectively handles NONE/FULL scaling
762 nv_connector = nouveau_crtc_connector_get(nv_crtc);
763 if (nv_connector && nv_connector->native_mode) {
764 mode = nv_connector->scaling_mode;
765 if (nv_connector->scaling_full) /* non-EDID LVDS/eDP mode */
766 mode = DRM_MODE_SCALE_FULLSCREEN;
769 if (mode != DRM_MODE_SCALE_NONE)
770 omode = nv_connector->native_mode;
774 oX = omode->hdisplay;
775 oY = omode->vdisplay;
776 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
779 /* add overscan compensation if necessary, will keep the aspect
780 * ratio the same as the backend mode unless overridden by the
781 * user setting both hborder and vborder properties.
783 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
784 (nv_connector->underscan == UNDERSCAN_AUTO &&
785 drm_detect_hdmi_monitor(nv_connector->edid)))) {
786 u32 bX = nv_connector->underscan_hborder;
787 u32 bY = nv_connector->underscan_vborder;
788 u32 aspect = (oY << 19) / oX;
792 if (bY) oY -= (bY * 2);
793 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
795 oX -= (oX >> 4) + 32;
796 if (bY) oY -= (bY * 2);
797 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
801 /* handle CENTER/ASPECT scaling, taking into account the areas
802 * removed already for overscan compensation
805 case DRM_MODE_SCALE_CENTER:
806 oX = min((u32)umode->hdisplay, oX);
807 oY = min((u32)umode->vdisplay, oY);
809 case DRM_MODE_SCALE_ASPECT:
811 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
812 oX = ((oY * aspect) + (aspect / 2)) >> 19;
814 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
815 oY = ((oX * aspect) + (aspect / 2)) >> 19;
822 push = evo_wait(mast, 8);
824 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
825 /*XXX: SCALE_CTRL_ACTIVE??? */
826 evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
827 evo_data(push, (oY << 16) | oX);
828 evo_data(push, (oY << 16) | oX);
829 evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
830 evo_data(push, 0x00000000);
831 evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
832 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
834 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
835 evo_data(push, (oY << 16) | oX);
836 evo_data(push, (oY << 16) | oX);
837 evo_data(push, (oY << 16) | oX);
838 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
839 evo_data(push, 0x00000000);
840 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
841 evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
844 evo_kick(push, mast);
847 nv50_display_flip_stop(crtc);
848 nv50_display_flip_next(crtc, crtc->primary->fb,
857 nv50_crtc_set_raster_vblank_dmi(struct nouveau_crtc *nv_crtc, u32 usec)
859 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
862 push = evo_wait(mast, 8);
866 evo_mthd(push, 0x0828 + (nv_crtc->index * 0x400), 1);
867 evo_data(push, usec);
868 evo_kick(push, mast);
873 nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
875 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
879 adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
880 vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
881 hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
883 push = evo_wait(mast, 16);
885 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
886 evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
887 evo_data(push, (hue << 20) | (vib << 8));
889 evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
890 evo_data(push, (hue << 20) | (vib << 8));
894 evo_mthd(push, 0x0080, 1);
895 evo_data(push, 0x00000000);
897 evo_kick(push, mast);
904 nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
905 int x, int y, bool update)
907 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
908 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
911 push = evo_wait(mast, 16);
913 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
914 evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
915 evo_data(push, nvfb->nvbo->bo.offset >> 8);
916 evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
917 evo_data(push, (fb->height << 16) | fb->width);
918 evo_data(push, nvfb->r_pitch);
919 evo_data(push, nvfb->r_format);
920 evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
921 evo_data(push, (y << 16) | x);
922 if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
923 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
924 evo_data(push, nvfb->r_handle);
927 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
928 evo_data(push, nvfb->nvbo->bo.offset >> 8);
929 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
930 evo_data(push, (fb->height << 16) | fb->width);
931 evo_data(push, nvfb->r_pitch);
932 evo_data(push, nvfb->r_format);
933 evo_data(push, nvfb->r_handle);
934 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
935 evo_data(push, (y << 16) | x);
939 evo_mthd(push, 0x0080, 1);
940 evo_data(push, 0x00000000);
942 evo_kick(push, mast);
945 nv_crtc->fb.handle = nvfb->r_handle;
950 nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
952 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
953 u32 *push = evo_wait(mast, 16);
955 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
956 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
957 evo_data(push, 0x85000000);
958 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
960 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
961 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
962 evo_data(push, 0x85000000);
963 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
964 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
965 evo_data(push, mast->base.vram.handle);
967 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
968 evo_data(push, 0x85000000);
969 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
970 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
971 evo_data(push, mast->base.vram.handle);
973 evo_kick(push, mast);
975 nv_crtc->cursor.visible = true;
979 nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
981 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
982 u32 *push = evo_wait(mast, 16);
984 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
985 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
986 evo_data(push, 0x05000000);
988 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
989 evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
990 evo_data(push, 0x05000000);
991 evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
992 evo_data(push, 0x00000000);
994 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
995 evo_data(push, 0x05000000);
996 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
997 evo_data(push, 0x00000000);
999 evo_kick(push, mast);
1001 nv_crtc->cursor.visible = false;
1005 nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
1007 struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
1009 if (show && nv_crtc->cursor.nvbo && nv_crtc->base.enabled)
1010 nv50_crtc_cursor_show(nv_crtc);
1012 nv50_crtc_cursor_hide(nv_crtc);
1015 u32 *push = evo_wait(mast, 2);
1017 evo_mthd(push, 0x0080, 1);
1018 evo_data(push, 0x00000000);
1019 evo_kick(push, mast);
1025 nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
1030 nv50_crtc_prepare(struct drm_crtc *crtc)
1032 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1033 struct nv50_mast *mast = nv50_mast(crtc->dev);
1036 nv50_display_flip_stop(crtc);
1038 push = evo_wait(mast, 6);
1040 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
1041 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1042 evo_data(push, 0x00000000);
1043 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
1044 evo_data(push, 0x40000000);
1046 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
1047 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1048 evo_data(push, 0x00000000);
1049 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
1050 evo_data(push, 0x40000000);
1051 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
1052 evo_data(push, 0x00000000);
1054 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
1055 evo_data(push, 0x00000000);
1056 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
1057 evo_data(push, 0x03000000);
1058 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
1059 evo_data(push, 0x00000000);
1062 evo_kick(push, mast);
1065 nv50_crtc_cursor_show_hide(nv_crtc, false, false);
1069 nv50_crtc_commit(struct drm_crtc *crtc)
1071 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1072 struct nv50_mast *mast = nv50_mast(crtc->dev);
1075 push = evo_wait(mast, 32);
1077 if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
1078 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1079 evo_data(push, nv_crtc->fb.handle);
1080 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1081 evo_data(push, 0xc0000000);
1082 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1084 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
1085 evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
1086 evo_data(push, nv_crtc->fb.handle);
1087 evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
1088 evo_data(push, 0xc0000000);
1089 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1090 evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
1091 evo_data(push, mast->base.vram.handle);
1093 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
1094 evo_data(push, nv_crtc->fb.handle);
1095 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
1096 evo_data(push, 0x83000000);
1097 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
1098 evo_data(push, 0x00000000);
1099 evo_data(push, 0x00000000);
1100 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
1101 evo_data(push, mast->base.vram.handle);
1102 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
1103 evo_data(push, 0xffffff00);
1106 evo_kick(push, mast);
1109 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
1110 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
1114 nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
1115 struct drm_display_mode *adjusted_mode)
1117 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
1122 nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
1124 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
1125 struct nv50_head *head = nv50_head(crtc);
1128 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM, true);
1131 nouveau_bo_unpin(head->image);
1132 nouveau_bo_ref(nvfb->nvbo, &head->image);
1139 nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
1140 struct drm_display_mode *mode, int x, int y,
1141 struct drm_framebuffer *old_fb)
1143 struct nv50_mast *mast = nv50_mast(crtc->dev);
1144 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1145 struct nouveau_connector *nv_connector;
1146 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
1147 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
1148 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
1149 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
1150 u32 vblan2e = 0, vblan2s = 1, vblankus = 0;
1154 hactive = mode->htotal;
1155 hsynce = mode->hsync_end - mode->hsync_start - 1;
1156 hbackp = mode->htotal - mode->hsync_end;
1157 hblanke = hsynce + hbackp;
1158 hfrontp = mode->hsync_start - mode->hdisplay;
1159 hblanks = mode->htotal - hfrontp - 1;
1161 vactive = mode->vtotal * vscan / ilace;
1162 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
1163 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
1164 vblanke = vsynce + vbackp;
1165 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
1166 vblanks = vactive - vfrontp - 1;
1167 /* XXX: Safe underestimate, even "0" works */
1168 vblankus = (vactive - mode->vdisplay - 2) * hactive;
1170 vblankus /= mode->clock;
1172 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1173 vblan2e = vactive + vsynce + vbackp;
1174 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
1175 vactive = (vactive * 2) + 1;
1178 ret = nv50_crtc_swap_fbs(crtc, old_fb);
1182 push = evo_wait(mast, 64);
1184 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
1185 evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
1186 evo_data(push, 0x00800000 | mode->clock);
1187 evo_data(push, (ilace == 2) ? 2 : 0);
1188 evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
1189 evo_data(push, 0x00000000);
1190 evo_data(push, (vactive << 16) | hactive);
1191 evo_data(push, ( vsynce << 16) | hsynce);
1192 evo_data(push, (vblanke << 16) | hblanke);
1193 evo_data(push, (vblanks << 16) | hblanks);
1194 evo_data(push, (vblan2e << 16) | vblan2s);
1195 evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
1196 evo_data(push, 0x00000000);
1197 evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
1198 evo_data(push, 0x00000311);
1199 evo_data(push, 0x00000100);
1201 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
1202 evo_data(push, 0x00000000);
1203 evo_data(push, (vactive << 16) | hactive);
1204 evo_data(push, ( vsynce << 16) | hsynce);
1205 evo_data(push, (vblanke << 16) | hblanke);
1206 evo_data(push, (vblanks << 16) | hblanks);
1207 evo_data(push, (vblan2e << 16) | vblan2s);
1208 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
1209 evo_data(push, 0x00000000); /* ??? */
1210 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
1211 evo_data(push, mode->clock * 1000);
1212 evo_data(push, 0x00200000); /* ??? */
1213 evo_data(push, mode->clock * 1000);
1214 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
1215 evo_data(push, 0x00000311);
1216 evo_data(push, 0x00000100);
1219 evo_kick(push, mast);
1222 nv_connector = nouveau_crtc_connector_get(nv_crtc);
1223 nv50_crtc_set_dither(nv_crtc, false);
1224 nv50_crtc_set_scale(nv_crtc, false);
1226 /* G94 only accepts this after setting scale */
1227 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA)
1228 nv50_crtc_set_raster_vblank_dmi(nv_crtc, vblankus);
1230 nv50_crtc_set_color_vibrance(nv_crtc, false);
1231 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
1236 nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
1237 struct drm_framebuffer *old_fb)
1239 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
1240 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1243 if (!crtc->primary->fb) {
1244 NV_DEBUG(drm, "No FB bound\n");
1248 ret = nv50_crtc_swap_fbs(crtc, old_fb);
1252 nv50_display_flip_stop(crtc);
1253 nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
1254 nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
1259 nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
1260 struct drm_framebuffer *fb, int x, int y,
1261 enum mode_set_atomic state)
1263 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1264 nv50_display_flip_stop(crtc);
1265 nv50_crtc_set_image(nv_crtc, fb, x, y, true);
1270 nv50_crtc_lut_load(struct drm_crtc *crtc)
1272 struct nv50_disp *disp = nv50_disp(crtc->dev);
1273 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1274 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
1277 for (i = 0; i < 256; i++) {
1278 u16 r = nv_crtc->lut.r[i] >> 2;
1279 u16 g = nv_crtc->lut.g[i] >> 2;
1280 u16 b = nv_crtc->lut.b[i] >> 2;
1282 if (disp->disp->oclass < GF110_DISP) {
1283 writew(r + 0x0000, lut + (i * 0x08) + 0);
1284 writew(g + 0x0000, lut + (i * 0x08) + 2);
1285 writew(b + 0x0000, lut + (i * 0x08) + 4);
1287 writew(r + 0x6000, lut + (i * 0x20) + 0);
1288 writew(g + 0x6000, lut + (i * 0x20) + 2);
1289 writew(b + 0x6000, lut + (i * 0x20) + 4);
1295 nv50_crtc_disable(struct drm_crtc *crtc)
1297 struct nv50_head *head = nv50_head(crtc);
1298 evo_sync(crtc->dev);
1300 nouveau_bo_unpin(head->image);
1301 nouveau_bo_ref(NULL, &head->image);
1305 nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
1306 uint32_t handle, uint32_t width, uint32_t height)
1308 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1309 struct drm_gem_object *gem = NULL;
1310 struct nouveau_bo *nvbo = NULL;
1314 if (width != 64 || height != 64)
1317 gem = drm_gem_object_lookup(file_priv, handle);
1320 nvbo = nouveau_gem_object(gem);
1322 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, true);
1326 if (nv_crtc->cursor.nvbo)
1327 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1328 nouveau_bo_ref(nvbo, &nv_crtc->cursor.nvbo);
1330 drm_gem_object_unreference_unlocked(gem);
1332 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
1337 nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1339 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1340 struct nv50_curs *curs = nv50_curs(crtc);
1341 struct nv50_chan *chan = nv50_chan(curs);
1342 nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
1343 nvif_wr32(&chan->user, 0x0080, 0x00000000);
1345 nv_crtc->cursor_saved_x = x;
1346 nv_crtc->cursor_saved_y = y;
1351 nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
1354 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1357 for (i = 0; i < size; i++) {
1358 nv_crtc->lut.r[i] = r[i];
1359 nv_crtc->lut.g[i] = g[i];
1360 nv_crtc->lut.b[i] = b[i];
1363 nv50_crtc_lut_load(crtc);
1369 nv50_crtc_cursor_restore(struct nouveau_crtc *nv_crtc, int x, int y)
1371 nv50_crtc_cursor_move(&nv_crtc->base, x, y);
1373 nv50_crtc_cursor_show_hide(nv_crtc, true, true);
1377 nv50_crtc_destroy(struct drm_crtc *crtc)
1379 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1380 struct nv50_disp *disp = nv50_disp(crtc->dev);
1381 struct nv50_head *head = nv50_head(crtc);
1382 struct nv50_fbdma *fbdma;
1384 list_for_each_entry(fbdma, &disp->fbdma, head) {
1385 nvif_object_fini(&fbdma->base[nv_crtc->index]);
1388 nv50_dmac_destroy(&head->ovly.base, disp->disp);
1389 nv50_pioc_destroy(&head->oimm.base);
1390 nv50_dmac_destroy(&head->sync.base, disp->disp);
1391 nv50_pioc_destroy(&head->curs.base);
1393 /*XXX: this shouldn't be necessary, but the core doesn't call
1394 * disconnect() during the cleanup paths
1397 nouveau_bo_unpin(head->image);
1398 nouveau_bo_ref(NULL, &head->image);
1401 if (nv_crtc->cursor.nvbo)
1402 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1403 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
1405 nouveau_bo_unmap(nv_crtc->lut.nvbo);
1406 if (nv_crtc->lut.nvbo)
1407 nouveau_bo_unpin(nv_crtc->lut.nvbo);
1408 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
1410 drm_crtc_cleanup(crtc);
1414 static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
1415 .dpms = nv50_crtc_dpms,
1416 .prepare = nv50_crtc_prepare,
1417 .commit = nv50_crtc_commit,
1418 .mode_fixup = nv50_crtc_mode_fixup,
1419 .mode_set = nv50_crtc_mode_set,
1420 .mode_set_base = nv50_crtc_mode_set_base,
1421 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
1422 .load_lut = nv50_crtc_lut_load,
1423 .disable = nv50_crtc_disable,
1426 static const struct drm_crtc_funcs nv50_crtc_func = {
1427 .cursor_set = nv50_crtc_cursor_set,
1428 .cursor_move = nv50_crtc_cursor_move,
1429 .gamma_set = nv50_crtc_gamma_set,
1430 .set_config = nouveau_crtc_set_config,
1431 .destroy = nv50_crtc_destroy,
1432 .page_flip = nouveau_crtc_page_flip,
1436 nv50_crtc_create(struct drm_device *dev, int index)
1438 struct nouveau_drm *drm = nouveau_drm(dev);
1439 struct nvif_device *device = &drm->device;
1440 struct nv50_disp *disp = nv50_disp(dev);
1441 struct nv50_head *head;
1442 struct drm_crtc *crtc;
1445 head = kzalloc(sizeof(*head), GFP_KERNEL);
1449 head->base.index = index;
1450 head->base.set_dither = nv50_crtc_set_dither;
1451 head->base.set_scale = nv50_crtc_set_scale;
1452 head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
1453 head->base.color_vibrance = 50;
1454 head->base.vibrant_hue = 0;
1455 head->base.cursor.set_pos = nv50_crtc_cursor_restore;
1456 for (i = 0; i < 256; i++) {
1457 head->base.lut.r[i] = i << 8;
1458 head->base.lut.g[i] = i << 8;
1459 head->base.lut.b[i] = i << 8;
1462 crtc = &head->base.base;
1463 drm_crtc_init(dev, crtc, &nv50_crtc_func);
1464 drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
1465 drm_mode_crtc_set_gamma_size(crtc, 256);
1467 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
1468 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
1470 ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
1472 ret = nouveau_bo_map(head->base.lut.nvbo);
1474 nouveau_bo_unpin(head->base.lut.nvbo);
1477 nouveau_bo_ref(NULL, &head->base.lut.nvbo);
1483 /* allocate cursor resources */
1484 ret = nv50_curs_create(device, disp->disp, index, &head->curs);
1488 /* allocate page flip / sync resources */
1489 ret = nv50_base_create(device, disp->disp, index, disp->sync->bo.offset,
1494 head->sync.addr = EVO_FLIP_SEM0(index);
1495 head->sync.data = 0x00000000;
1497 /* allocate overlay resources */
1498 ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
1502 ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
1509 nv50_crtc_destroy(crtc);
1513 /******************************************************************************
1515 *****************************************************************************/
1517 nv50_encoder_mode_fixup(struct drm_encoder *encoder,
1518 const struct drm_display_mode *mode,
1519 struct drm_display_mode *adjusted_mode)
1521 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1522 struct nouveau_connector *nv_connector;
1524 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1525 if (nv_connector && nv_connector->native_mode) {
1526 nv_connector->scaling_full = false;
1527 if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) {
1528 switch (nv_connector->type) {
1529 case DCB_CONNECTOR_LVDS:
1530 case DCB_CONNECTOR_LVDS_SPWG:
1531 case DCB_CONNECTOR_eDP:
1532 /* force use of scaler for non-edid modes */
1533 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
1535 nv_connector->scaling_full = true;
1542 drm_mode_copy(adjusted_mode, nv_connector->native_mode);
1548 /******************************************************************************
1550 *****************************************************************************/
1552 nv50_dac_dpms(struct drm_encoder *encoder, int mode)
1554 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1555 struct nv50_disp *disp = nv50_disp(encoder->dev);
1557 struct nv50_disp_mthd_v1 base;
1558 struct nv50_disp_dac_pwr_v0 pwr;
1561 .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
1562 .base.hasht = nv_encoder->dcb->hasht,
1563 .base.hashm = nv_encoder->dcb->hashm,
1566 .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
1567 mode != DRM_MODE_DPMS_OFF),
1568 .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
1569 mode != DRM_MODE_DPMS_OFF),
1572 nvif_mthd(disp->disp, 0, &args, sizeof(args));
1576 nv50_dac_commit(struct drm_encoder *encoder)
1581 nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
1582 struct drm_display_mode *adjusted_mode)
1584 struct nv50_mast *mast = nv50_mast(encoder->dev);
1585 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1586 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1589 nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
1591 push = evo_wait(mast, 8);
1593 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
1594 u32 syncs = 0x00000000;
1596 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1597 syncs |= 0x00000001;
1598 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1599 syncs |= 0x00000002;
1601 evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
1602 evo_data(push, 1 << nv_crtc->index);
1603 evo_data(push, syncs);
1605 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
1606 u32 syncs = 0x00000001;
1608 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1609 syncs |= 0x00000008;
1610 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1611 syncs |= 0x00000010;
1613 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1614 magic |= 0x00000001;
1616 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1617 evo_data(push, syncs);
1618 evo_data(push, magic);
1619 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
1620 evo_data(push, 1 << nv_crtc->index);
1623 evo_kick(push, mast);
1626 nv_encoder->crtc = encoder->crtc;
1630 nv50_dac_disconnect(struct drm_encoder *encoder)
1632 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1633 struct nv50_mast *mast = nv50_mast(encoder->dev);
1634 const int or = nv_encoder->or;
1637 if (nv_encoder->crtc) {
1638 nv50_crtc_prepare(nv_encoder->crtc);
1640 push = evo_wait(mast, 4);
1642 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
1643 evo_mthd(push, 0x0400 + (or * 0x080), 1);
1644 evo_data(push, 0x00000000);
1646 evo_mthd(push, 0x0180 + (or * 0x020), 1);
1647 evo_data(push, 0x00000000);
1649 evo_kick(push, mast);
1653 nv_encoder->crtc = NULL;
1656 static enum drm_connector_status
1657 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
1659 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1660 struct nv50_disp *disp = nv50_disp(encoder->dev);
1662 struct nv50_disp_mthd_v1 base;
1663 struct nv50_disp_dac_load_v0 load;
1666 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
1667 .base.hasht = nv_encoder->dcb->hasht,
1668 .base.hashm = nv_encoder->dcb->hashm,
1672 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
1673 if (args.load.data == 0)
1674 args.load.data = 340;
1676 ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
1677 if (ret || !args.load.load)
1678 return connector_status_disconnected;
1680 return connector_status_connected;
1684 nv50_dac_destroy(struct drm_encoder *encoder)
1686 drm_encoder_cleanup(encoder);
1690 static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
1691 .dpms = nv50_dac_dpms,
1692 .mode_fixup = nv50_encoder_mode_fixup,
1693 .prepare = nv50_dac_disconnect,
1694 .commit = nv50_dac_commit,
1695 .mode_set = nv50_dac_mode_set,
1696 .disable = nv50_dac_disconnect,
1697 .get_crtc = nv50_display_crtc_get,
1698 .detect = nv50_dac_detect
1701 static const struct drm_encoder_funcs nv50_dac_func = {
1702 .destroy = nv50_dac_destroy,
1706 nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
1708 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1709 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
1710 struct nvkm_i2c_bus *bus;
1711 struct nouveau_encoder *nv_encoder;
1712 struct drm_encoder *encoder;
1713 int type = DRM_MODE_ENCODER_DAC;
1715 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1718 nv_encoder->dcb = dcbe;
1719 nv_encoder->or = ffs(dcbe->or) - 1;
1721 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1723 nv_encoder->i2c = &bus->i2c;
1725 encoder = to_drm_encoder(nv_encoder);
1726 encoder->possible_crtcs = dcbe->heads;
1727 encoder->possible_clones = 0;
1728 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type, NULL);
1729 drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
1731 drm_mode_connector_attach_encoder(connector, encoder);
1735 /******************************************************************************
1737 *****************************************************************************/
1739 nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1741 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1742 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1743 struct nouveau_connector *nv_connector;
1744 struct nv50_disp *disp = nv50_disp(encoder->dev);
1747 struct nv50_disp_mthd_v1 mthd;
1748 struct nv50_disp_sor_hda_eld_v0 eld;
1750 u8 data[sizeof(nv_connector->base.eld)];
1752 .base.mthd.version = 1,
1753 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1754 .base.mthd.hasht = nv_encoder->dcb->hasht,
1755 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1756 (0x0100 << nv_crtc->index),
1759 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1760 if (!drm_detect_monitor_audio(nv_connector->edid))
1763 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
1764 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
1766 nvif_mthd(disp->disp, 0, &args,
1767 sizeof(args.base) + drm_eld_size(args.data));
1771 nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
1773 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1774 struct nv50_disp *disp = nv50_disp(encoder->dev);
1776 struct nv50_disp_mthd_v1 base;
1777 struct nv50_disp_sor_hda_eld_v0 eld;
1780 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
1781 .base.hasht = nv_encoder->dcb->hasht,
1782 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1783 (0x0100 << nv_crtc->index),
1786 nvif_mthd(disp->disp, 0, &args, sizeof(args));
1789 /******************************************************************************
1791 *****************************************************************************/
1793 nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1795 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1796 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1797 struct nv50_disp *disp = nv50_disp(encoder->dev);
1799 struct nv50_disp_mthd_v1 base;
1800 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1803 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1804 .base.hasht = nv_encoder->dcb->hasht,
1805 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1806 (0x0100 << nv_crtc->index),
1808 .pwr.rekey = 56, /* binary driver, and tegra, constant */
1810 struct nouveau_connector *nv_connector;
1813 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1814 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1817 max_ac_packet = mode->htotal - mode->hdisplay;
1818 max_ac_packet -= args.pwr.rekey;
1819 max_ac_packet -= 18; /* constant from tegra */
1820 args.pwr.max_ac_packet = max_ac_packet / 32;
1822 nvif_mthd(disp->disp, 0, &args, sizeof(args));
1823 nv50_audio_mode_set(encoder, mode);
1827 nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
1829 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1830 struct nv50_disp *disp = nv50_disp(encoder->dev);
1832 struct nv50_disp_mthd_v1 base;
1833 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
1836 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
1837 .base.hasht = nv_encoder->dcb->hasht,
1838 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
1839 (0x0100 << nv_crtc->index),
1842 nvif_mthd(disp->disp, 0, &args, sizeof(args));
1845 /******************************************************************************
1847 *****************************************************************************/
1849 nv50_sor_dpms(struct drm_encoder *encoder, int mode)
1851 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1852 struct nv50_disp *disp = nv50_disp(encoder->dev);
1854 struct nv50_disp_mthd_v1 base;
1855 struct nv50_disp_sor_pwr_v0 pwr;
1858 .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
1859 .base.hasht = nv_encoder->dcb->hasht,
1860 .base.hashm = nv_encoder->dcb->hashm,
1861 .pwr.state = mode == DRM_MODE_DPMS_ON,
1864 struct nv50_disp_mthd_v1 base;
1865 struct nv50_disp_sor_dp_pwr_v0 pwr;
1868 .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
1869 .base.hasht = nv_encoder->dcb->hasht,
1870 .base.hashm = nv_encoder->dcb->hashm,
1871 .pwr.state = mode == DRM_MODE_DPMS_ON,
1873 struct drm_device *dev = encoder->dev;
1874 struct drm_encoder *partner;
1876 nv_encoder->last_dpms = mode;
1878 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1879 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1881 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1884 if (nv_partner != nv_encoder &&
1885 nv_partner->dcb->or == nv_encoder->dcb->or) {
1886 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1892 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
1894 nvif_mthd(disp->disp, 0, &args, sizeof(args));
1895 nvif_mthd(disp->disp, 0, &link, sizeof(link));
1897 nvif_mthd(disp->disp, 0, &args, sizeof(args));
1902 nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
1904 struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
1905 u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
1906 if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
1907 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
1908 evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
1909 evo_data(push, (nv_encoder->ctrl = temp));
1911 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1912 evo_data(push, (nv_encoder->ctrl = temp));
1914 evo_kick(push, mast);
1919 nv50_sor_disconnect(struct drm_encoder *encoder)
1921 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1922 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1924 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1925 nv_encoder->crtc = NULL;
1928 nv50_crtc_prepare(&nv_crtc->base);
1929 nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
1930 nv50_audio_disconnect(encoder, nv_crtc);
1931 nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
1936 nv50_sor_commit(struct drm_encoder *encoder)
1941 nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
1942 struct drm_display_mode *mode)
1944 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1945 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1947 struct nv50_disp_mthd_v1 base;
1948 struct nv50_disp_sor_lvds_script_v0 lvds;
1951 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1952 .base.hasht = nv_encoder->dcb->hasht,
1953 .base.hashm = nv_encoder->dcb->hashm,
1955 struct nv50_disp *disp = nv50_disp(encoder->dev);
1956 struct nv50_mast *mast = nv50_mast(encoder->dev);
1957 struct drm_device *dev = encoder->dev;
1958 struct nouveau_drm *drm = nouveau_drm(dev);
1959 struct nouveau_connector *nv_connector;
1960 struct nvbios *bios = &drm->vbios;
1962 u8 owner = 1 << nv_crtc->index;
1966 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1967 nv_encoder->crtc = encoder->crtc;
1969 switch (nv_encoder->dcb->type) {
1970 case DCB_OUTPUT_TMDS:
1971 if (nv_encoder->dcb->sorconf.link & 1) {
1973 /* Only enable dual-link if:
1974 * - Need to (i.e. rate > 165MHz)
1976 * - Not an HDMI monitor, since there's no dual-link
1979 if (mode->clock >= 165000 &&
1980 nv_encoder->dcb->duallink_possible &&
1981 !drm_detect_hdmi_monitor(nv_connector->edid))
1987 nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
1989 case DCB_OUTPUT_LVDS:
1992 if (bios->fp_no_ddc) {
1993 if (bios->fp.dual_link)
1994 lvds.lvds.script |= 0x0100;
1995 if (bios->fp.if_is_24bit)
1996 lvds.lvds.script |= 0x0200;
1998 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1999 if (((u8 *)nv_connector->edid)[121] == 2)
2000 lvds.lvds.script |= 0x0100;
2002 if (mode->clock >= bios->fp.duallink_transition_clk) {
2003 lvds.lvds.script |= 0x0100;
2006 if (lvds.lvds.script & 0x0100) {
2007 if (bios->fp.strapless_is_24bit & 2)
2008 lvds.lvds.script |= 0x0200;
2010 if (bios->fp.strapless_is_24bit & 1)
2011 lvds.lvds.script |= 0x0200;
2014 if (nv_connector->base.display_info.bpc == 8)
2015 lvds.lvds.script |= 0x0200;
2018 nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
2021 if (nv_connector->base.display_info.bpc == 6) {
2022 nv_encoder->dp.datarate = mode->clock * 18 / 8;
2025 if (nv_connector->base.display_info.bpc == 8) {
2026 nv_encoder->dp.datarate = mode->clock * 24 / 8;
2029 nv_encoder->dp.datarate = mode->clock * 30 / 8;
2033 if (nv_encoder->dcb->sorconf.link & 1)
2037 nv50_audio_mode_set(encoder, mode);
2044 nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
2046 if (nv50_vers(mast) >= GF110_DISP) {
2047 u32 *push = evo_wait(mast, 3);
2049 u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
2050 u32 syncs = 0x00000001;
2052 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2053 syncs |= 0x00000008;
2054 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2055 syncs |= 0x00000010;
2057 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2058 magic |= 0x00000001;
2060 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
2061 evo_data(push, syncs | (depth << 6));
2062 evo_data(push, magic);
2063 evo_kick(push, mast);
2069 ctrl = (depth << 16) | (proto << 8);
2070 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2072 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2077 nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
2081 nv50_sor_destroy(struct drm_encoder *encoder)
2083 drm_encoder_cleanup(encoder);
2087 static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
2088 .dpms = nv50_sor_dpms,
2089 .mode_fixup = nv50_encoder_mode_fixup,
2090 .prepare = nv50_sor_disconnect,
2091 .commit = nv50_sor_commit,
2092 .mode_set = nv50_sor_mode_set,
2093 .disable = nv50_sor_disconnect,
2094 .get_crtc = nv50_display_crtc_get,
2097 static const struct drm_encoder_funcs nv50_sor_func = {
2098 .destroy = nv50_sor_destroy,
2102 nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
2104 struct nouveau_drm *drm = nouveau_drm(connector->dev);
2105 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
2106 struct nouveau_encoder *nv_encoder;
2107 struct drm_encoder *encoder;
2110 switch (dcbe->type) {
2111 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
2112 case DCB_OUTPUT_TMDS:
2115 type = DRM_MODE_ENCODER_TMDS;
2119 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2122 nv_encoder->dcb = dcbe;
2123 nv_encoder->or = ffs(dcbe->or) - 1;
2124 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
2126 if (dcbe->type == DCB_OUTPUT_DP) {
2127 struct nvkm_i2c_aux *aux =
2128 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
2130 nv_encoder->i2c = &aux->i2c;
2131 nv_encoder->aux = aux;
2134 struct nvkm_i2c_bus *bus =
2135 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
2137 nv_encoder->i2c = &bus->i2c;
2140 encoder = to_drm_encoder(nv_encoder);
2141 encoder->possible_crtcs = dcbe->heads;
2142 encoder->possible_clones = 0;
2143 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type, NULL);
2144 drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
2146 drm_mode_connector_attach_encoder(connector, encoder);
2150 /******************************************************************************
2152 *****************************************************************************/
2155 nv50_pior_dpms(struct drm_encoder *encoder, int mode)
2157 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2158 struct nv50_disp *disp = nv50_disp(encoder->dev);
2160 struct nv50_disp_mthd_v1 base;
2161 struct nv50_disp_pior_pwr_v0 pwr;
2164 .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
2165 .base.hasht = nv_encoder->dcb->hasht,
2166 .base.hashm = nv_encoder->dcb->hashm,
2167 .pwr.state = mode == DRM_MODE_DPMS_ON,
2168 .pwr.type = nv_encoder->dcb->type,
2171 nvif_mthd(disp->disp, 0, &args, sizeof(args));
2175 nv50_pior_mode_fixup(struct drm_encoder *encoder,
2176 const struct drm_display_mode *mode,
2177 struct drm_display_mode *adjusted_mode)
2179 if (!nv50_encoder_mode_fixup(encoder, mode, adjusted_mode))
2181 adjusted_mode->clock *= 2;
2186 nv50_pior_commit(struct drm_encoder *encoder)
2191 nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
2192 struct drm_display_mode *adjusted_mode)
2194 struct nv50_mast *mast = nv50_mast(encoder->dev);
2195 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2196 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
2197 struct nouveau_connector *nv_connector;
2198 u8 owner = 1 << nv_crtc->index;
2202 nv_connector = nouveau_encoder_connector_get(nv_encoder);
2203 switch (nv_connector->base.display_info.bpc) {
2204 case 10: depth = 0x6; break;
2205 case 8: depth = 0x5; break;
2206 case 6: depth = 0x2; break;
2207 default: depth = 0x0; break;
2210 switch (nv_encoder->dcb->type) {
2211 case DCB_OUTPUT_TMDS:
2220 nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
2222 push = evo_wait(mast, 8);
2224 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2225 u32 ctrl = (depth << 16) | (proto << 8) | owner;
2226 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2228 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2230 evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
2231 evo_data(push, ctrl);
2234 evo_kick(push, mast);
2237 nv_encoder->crtc = encoder->crtc;
2241 nv50_pior_disconnect(struct drm_encoder *encoder)
2243 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2244 struct nv50_mast *mast = nv50_mast(encoder->dev);
2245 const int or = nv_encoder->or;
2248 if (nv_encoder->crtc) {
2249 nv50_crtc_prepare(nv_encoder->crtc);
2251 push = evo_wait(mast, 4);
2253 if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
2254 evo_mthd(push, 0x0700 + (or * 0x040), 1);
2255 evo_data(push, 0x00000000);
2257 evo_kick(push, mast);
2261 nv_encoder->crtc = NULL;
2265 nv50_pior_destroy(struct drm_encoder *encoder)
2267 drm_encoder_cleanup(encoder);
2271 static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
2272 .dpms = nv50_pior_dpms,
2273 .mode_fixup = nv50_pior_mode_fixup,
2274 .prepare = nv50_pior_disconnect,
2275 .commit = nv50_pior_commit,
2276 .mode_set = nv50_pior_mode_set,
2277 .disable = nv50_pior_disconnect,
2278 .get_crtc = nv50_display_crtc_get,
2281 static const struct drm_encoder_funcs nv50_pior_func = {
2282 .destroy = nv50_pior_destroy,
2286 nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
2288 struct nouveau_drm *drm = nouveau_drm(connector->dev);
2289 struct nvkm_i2c *i2c = nvxx_i2c(&drm->device);
2290 struct nvkm_i2c_bus *bus = NULL;
2291 struct nvkm_i2c_aux *aux = NULL;
2292 struct i2c_adapter *ddc;
2293 struct nouveau_encoder *nv_encoder;
2294 struct drm_encoder *encoder;
2297 switch (dcbe->type) {
2298 case DCB_OUTPUT_TMDS:
2299 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
2300 ddc = bus ? &bus->i2c : NULL;
2301 type = DRM_MODE_ENCODER_TMDS;
2304 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
2305 ddc = aux ? &aux->i2c : NULL;
2306 type = DRM_MODE_ENCODER_TMDS;
2312 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
2315 nv_encoder->dcb = dcbe;
2316 nv_encoder->or = ffs(dcbe->or) - 1;
2317 nv_encoder->i2c = ddc;
2318 nv_encoder->aux = aux;
2320 encoder = to_drm_encoder(nv_encoder);
2321 encoder->possible_crtcs = dcbe->heads;
2322 encoder->possible_clones = 0;
2323 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type, NULL);
2324 drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
2326 drm_mode_connector_attach_encoder(connector, encoder);
2330 /******************************************************************************
2332 *****************************************************************************/
2335 nv50_fbdma_fini(struct nv50_fbdma *fbdma)
2338 for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
2339 nvif_object_fini(&fbdma->base[i]);
2340 nvif_object_fini(&fbdma->core);
2341 list_del(&fbdma->head);
2346 nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
2348 struct nouveau_drm *drm = nouveau_drm(dev);
2349 struct nv50_disp *disp = nv50_disp(dev);
2350 struct nv50_mast *mast = nv50_mast(dev);
2351 struct __attribute__ ((packed)) {
2352 struct nv_dma_v0 base;
2354 struct nv50_dma_v0 nv50;
2355 struct gf100_dma_v0 gf100;
2356 struct gf119_dma_v0 gf119;
2359 struct nv50_fbdma *fbdma;
2360 struct drm_crtc *crtc;
2361 u32 size = sizeof(args.base);
2364 list_for_each_entry(fbdma, &disp->fbdma, head) {
2365 if (fbdma->core.handle == name)
2369 fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
2372 list_add(&fbdma->head, &disp->fbdma);
2374 args.base.target = NV_DMA_V0_TARGET_VRAM;
2375 args.base.access = NV_DMA_V0_ACCESS_RDWR;
2376 args.base.start = offset;
2377 args.base.limit = offset + length - 1;
2379 if (drm->device.info.chipset < 0x80) {
2380 args.nv50.part = NV50_DMA_V0_PART_256;
2381 size += sizeof(args.nv50);
2383 if (drm->device.info.chipset < 0xc0) {
2384 args.nv50.part = NV50_DMA_V0_PART_256;
2385 args.nv50.kind = kind;
2386 size += sizeof(args.nv50);
2388 if (drm->device.info.chipset < 0xd0) {
2389 args.gf100.kind = kind;
2390 size += sizeof(args.gf100);
2392 args.gf119.page = GF119_DMA_V0_PAGE_LP;
2393 args.gf119.kind = kind;
2394 size += sizeof(args.gf119);
2397 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2398 struct nv50_head *head = nv50_head(crtc);
2399 int ret = nvif_object_init(&head->sync.base.base.user, name,
2400 NV_DMA_IN_MEMORY, &args, size,
2401 &fbdma->base[head->base.index]);
2403 nv50_fbdma_fini(fbdma);
2408 ret = nvif_object_init(&mast->base.base.user, name, NV_DMA_IN_MEMORY,
2409 &args, size, &fbdma->core);
2411 nv50_fbdma_fini(fbdma);
2419 nv50_fb_dtor(struct drm_framebuffer *fb)
2424 nv50_fb_ctor(struct drm_framebuffer *fb)
2426 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
2427 struct nouveau_drm *drm = nouveau_drm(fb->dev);
2428 struct nouveau_bo *nvbo = nv_fb->nvbo;
2429 struct nv50_disp *disp = nv50_disp(fb->dev);
2430 u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
2431 u8 tile = nvbo->tile_mode;
2433 if (drm->device.info.chipset >= 0xc0)
2434 tile >>= 4; /* yep.. */
2436 switch (fb->depth) {
2437 case 8: nv_fb->r_format = 0x1e00; break;
2438 case 15: nv_fb->r_format = 0xe900; break;
2439 case 16: nv_fb->r_format = 0xe800; break;
2441 case 32: nv_fb->r_format = 0xcf00; break;
2442 case 30: nv_fb->r_format = 0xd100; break;
2444 NV_ERROR(drm, "unknown depth %d\n", fb->depth);
2448 if (disp->disp->oclass < G82_DISP) {
2449 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2450 (fb->pitches[0] | 0x00100000);
2451 nv_fb->r_format |= kind << 16;
2453 if (disp->disp->oclass < GF110_DISP) {
2454 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2455 (fb->pitches[0] | 0x00100000);
2457 nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
2458 (fb->pitches[0] | 0x01000000);
2460 nv_fb->r_handle = 0xffff0000 | kind;
2462 return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
2463 drm->device.info.ram_user, kind);
2466 /******************************************************************************
2468 *****************************************************************************/
2471 nv50_display_fini(struct drm_device *dev)
2476 nv50_display_init(struct drm_device *dev)
2478 struct nv50_disp *disp = nv50_disp(dev);
2479 struct drm_crtc *crtc;
2482 push = evo_wait(nv50_mast(dev), 32);
2486 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2487 struct nv50_sync *sync = nv50_sync(crtc);
2489 nv50_crtc_lut_load(crtc);
2490 nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
2493 evo_mthd(push, 0x0088, 1);
2494 evo_data(push, nv50_mast(dev)->base.sync.handle);
2495 evo_kick(push, nv50_mast(dev));
2500 nv50_display_destroy(struct drm_device *dev)
2502 struct nv50_disp *disp = nv50_disp(dev);
2503 struct nv50_fbdma *fbdma, *fbtmp;
2505 list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
2506 nv50_fbdma_fini(fbdma);
2509 nv50_dmac_destroy(&disp->mast.base, disp->disp);
2511 nouveau_bo_unmap(disp->sync);
2513 nouveau_bo_unpin(disp->sync);
2514 nouveau_bo_ref(NULL, &disp->sync);
2516 nouveau_display(dev)->priv = NULL;
2521 nv50_display_create(struct drm_device *dev)
2523 struct nvif_device *device = &nouveau_drm(dev)->device;
2524 struct nouveau_drm *drm = nouveau_drm(dev);
2525 struct dcb_table *dcb = &drm->vbios.dcb;
2526 struct drm_connector *connector, *tmp;
2527 struct nv50_disp *disp;
2528 struct dcb_output *dcbe;
2531 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2534 INIT_LIST_HEAD(&disp->fbdma);
2536 nouveau_display(dev)->priv = disp;
2537 nouveau_display(dev)->dtor = nv50_display_destroy;
2538 nouveau_display(dev)->init = nv50_display_init;
2539 nouveau_display(dev)->fini = nv50_display_fini;
2540 nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
2541 nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
2542 disp->disp = &nouveau_display(dev)->disp;
2544 /* small shared memory area we use for notifiers and semaphores */
2545 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2546 0, 0x0000, NULL, NULL, &disp->sync);
2548 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
2550 ret = nouveau_bo_map(disp->sync);
2552 nouveau_bo_unpin(disp->sync);
2555 nouveau_bo_ref(NULL, &disp->sync);
2561 /* allocate master evo channel */
2562 ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
2567 /* create crtc objects to represent the hw heads */
2568 if (disp->disp->oclass >= GF110_DISP)
2569 crtcs = nvif_rd32(&device->object, 0x022448);
2573 for (i = 0; i < crtcs; i++) {
2574 ret = nv50_crtc_create(dev, i);
2579 /* create encoder/connector objects based on VBIOS DCB table */
2580 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2581 connector = nouveau_connector_create(dev, dcbe->connector);
2582 if (IS_ERR(connector))
2585 if (dcbe->location == DCB_LOC_ON_CHIP) {
2586 switch (dcbe->type) {
2587 case DCB_OUTPUT_TMDS:
2588 case DCB_OUTPUT_LVDS:
2590 ret = nv50_sor_create(connector, dcbe);
2592 case DCB_OUTPUT_ANALOG:
2593 ret = nv50_dac_create(connector, dcbe);
2600 ret = nv50_pior_create(connector, dcbe);
2604 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2605 dcbe->location, dcbe->type,
2606 ffs(dcbe->or) - 1, ret);
2611 /* cull any connectors we created that don't have an encoder */
2612 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2613 if (connector->encoder_ids[0])
2616 NV_WARN(drm, "%s has no encoders, removing\n",
2618 connector->funcs->destroy(connector);
2623 nv50_display_destroy(dev);