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_crtc_helper.h"
30 #include "nouveau_drv.h"
31 #include "nouveau_connector.h"
32 #include "nouveau_encoder.h"
33 #include "nouveau_crtc.h"
34 #include "nouveau_fb.h"
35 #include "nv50_display.h"
37 #define MEM_SYNC 0xe0000001
38 #define MEM_VRAM 0xe0010000
39 #include "nouveau_dma.h"
42 struct nouveau_gpuobj *mem;
48 struct dcb_entry *dis;
49 struct dcb_entry *ena;
56 static struct nvd0_display *
57 nvd0_display(struct drm_device *dev)
59 struct drm_nouveau_private *dev_priv = dev->dev_private;
60 return dev_priv->engine.display.priv;
64 evo_icmd(struct drm_device *dev, int id, u32 mthd, u32 data)
67 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000001);
68 nv_wr32(dev, 0x610704 + (id * 0x10), data);
69 nv_mask(dev, 0x610704 + (id * 0x10), 0x80000ffc, 0x80000000 | mthd);
70 if (!nv_wait(dev, 0x610704 + (id * 0x10), 0x80000000, 0x00000000))
72 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000000);
77 evo_wait(struct drm_device *dev, int id, int nr)
79 struct nvd0_display *disp = nvd0_display(dev);
80 u32 put = nv_rd32(dev, 0x640000 + (id * 0x1000)) / 4;
82 if (put + nr >= (PAGE_SIZE / 4)) {
83 disp->evo[id].ptr[put] = 0x20000000;
85 nv_wr32(dev, 0x640000 + (id * 0x1000), 0x00000000);
86 if (!nv_wait(dev, 0x640004 + (id * 0x1000), ~0, 0x00000000)) {
87 NV_ERROR(dev, "evo %d dma stalled\n", id);
94 return disp->evo[id].ptr + put;
98 evo_kick(u32 *push, struct drm_device *dev, int id)
100 struct nvd0_display *disp = nvd0_display(dev);
101 nv_wr32(dev, 0x640000 + (id * 0x1000), (push - disp->evo[id].ptr) << 2);
104 #define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
105 #define evo_data(p,d) *((p)++) = (d)
107 static struct drm_crtc *
108 nvd0_display_crtc_get(struct drm_encoder *encoder)
110 return nouveau_encoder(encoder)->crtc;
113 /******************************************************************************
115 *****************************************************************************/
117 nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool on, bool update)
119 struct drm_device *dev = nv_crtc->base.dev;
124 /* 0x11: 6bpc dynamic 2x2
125 * 0x13: 8bpc dynamic 2x2
126 * 0x19: 6bpc static 2x2
127 * 0x1b: 8bpc static 2x2
128 * 0x21: 6bpc temporal
129 * 0x23: 8bpc temporal
134 push = evo_wait(dev, 0, 4);
136 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x300), 1);
137 evo_data(push, mode);
139 evo_mthd(push, 0x0080, 1);
140 evo_data(push, 0x00000000);
142 evo_kick(push, dev, 0);
149 nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, int type, bool update)
151 struct drm_display_mode *mode = &nv_crtc->base.mode;
152 struct drm_device *dev = nv_crtc->base.dev;
155 /*XXX: actually handle scaling */
157 push = evo_wait(dev, 0, 16);
159 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
160 evo_data(push, (mode->vdisplay << 16) | mode->hdisplay);
161 evo_data(push, (mode->vdisplay << 16) | mode->hdisplay);
162 evo_data(push, (mode->vdisplay << 16) | mode->hdisplay);
163 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
164 evo_data(push, 0x00000000);
165 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
166 evo_data(push, 0x00000000);
167 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
168 evo_data(push, (mode->vdisplay << 16) | mode->hdisplay);
170 evo_mthd(push, 0x0080, 1);
171 evo_data(push, 0x00000000);
173 evo_kick(push, dev, 0);
180 nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
181 int x, int y, bool update)
183 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
186 push = evo_wait(fb->dev, 0, 16);
188 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
189 evo_data(push, nvfb->nvbo->bo.offset >> 8);
190 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
191 evo_data(push, (fb->height << 16) | fb->width);
192 evo_data(push, nvfb->r_pitch);
193 evo_data(push, nvfb->r_format);
194 evo_data(push, nvfb->r_dma);
195 evo_kick(push, fb->dev, 0);
198 nv_crtc->fb.tile_flags = nvfb->r_dma;
203 nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc, bool show, bool update)
205 struct drm_device *dev = nv_crtc->base.dev;
206 u32 *push = evo_wait(dev, 0, 16);
209 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
210 evo_data(push, 0x85000000);
211 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
212 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
213 evo_data(push, MEM_VRAM);
215 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
216 evo_data(push, 0x05000000);
217 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
218 evo_data(push, 0x00000000);
222 evo_mthd(push, 0x0080, 1);
223 evo_data(push, 0x00000000);
226 evo_kick(push, dev, 0);
231 nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
236 nvd0_crtc_prepare(struct drm_crtc *crtc)
238 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
241 push = evo_wait(crtc->dev, 0, 2);
243 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
244 evo_data(push, 0x00000000);
245 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
246 evo_data(push, 0x03000000);
247 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
248 evo_data(push, 0x00000000);
249 evo_kick(push, crtc->dev, 0);
252 nvd0_crtc_cursor_show(nv_crtc, false, false);
256 nvd0_crtc_commit(struct drm_crtc *crtc)
258 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
261 push = evo_wait(crtc->dev, 0, 32);
263 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
264 evo_data(push, nv_crtc->fb.tile_flags);
265 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
266 evo_data(push, 0x83000000);
267 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
268 evo_data(push, 0x00000000);
269 evo_data(push, 0x00000000);
270 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
271 evo_data(push, MEM_VRAM);
272 evo_kick(push, crtc->dev, 0);
275 nvd0_crtc_cursor_show(nv_crtc, nv_crtc->cursor.visible, true);
279 nvd0_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
280 struct drm_display_mode *adjusted_mode)
286 nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
288 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
291 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
296 nvfb = nouveau_framebuffer(old_fb);
297 nouveau_bo_unpin(nvfb->nvbo);
304 nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
305 struct drm_display_mode *mode, int x, int y,
306 struct drm_framebuffer *old_fb)
308 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
309 struct nouveau_connector *nv_connector;
310 u32 htotal = mode->htotal;
311 u32 vtotal = mode->vtotal;
312 u32 hsyncw = mode->hsync_end - mode->hsync_start - 1;
313 u32 vsyncw = mode->vsync_end - mode->vsync_start - 1;
314 u32 hfrntp = mode->hsync_start - mode->hdisplay;
315 u32 vfrntp = mode->vsync_start - mode->vdisplay;
316 u32 hbackp = mode->htotal - mode->hsync_end;
317 u32 vbackp = mode->vtotal - mode->vsync_end;
318 u32 hss2be = hsyncw + hbackp;
319 u32 vss2be = vsyncw + vbackp;
320 u32 hss2de = htotal - hfrntp;
321 u32 vss2de = vtotal - vfrntp;
327 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
331 push = evo_wait(crtc->dev, 0, 64);
333 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 5);
334 evo_data(push, (vstart << 16) | hstart);
335 evo_data(push, (vtotal << 16) | htotal);
336 evo_data(push, (vsyncw << 16) | hsyncw);
337 evo_data(push, (vss2be << 16) | hss2be);
338 evo_data(push, (vss2de << 16) | hss2de);
339 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
340 evo_data(push, 0x00000000); /* ??? */
341 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
342 evo_data(push, mode->clock * 1000);
343 evo_data(push, 0x00200000); /* ??? */
344 evo_data(push, mode->clock * 1000);
345 evo_mthd(push, 0x0408 + (nv_crtc->index * 0x300), 1);
346 evo_data(push, 0x31ec6000); /* ??? */
347 evo_kick(push, crtc->dev, 0);
350 nv_connector = nouveau_crtc_connector_get(nv_crtc);
351 nvd0_crtc_set_dither(nv_crtc, nv_connector->use_dithering, false);
352 nvd0_crtc_set_scale(nv_crtc, nv_connector->scaling_mode, false);
353 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
358 nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
359 struct drm_framebuffer *old_fb)
361 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
364 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
368 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
373 nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
374 struct drm_framebuffer *fb, int x, int y,
375 enum mode_set_atomic state)
377 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
378 nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
383 nvd0_crtc_lut_load(struct drm_crtc *crtc)
385 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
386 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
389 for (i = 0; i < 256; i++) {
390 writew(nv_crtc->lut.r[i] >> 2, lut + 8*i + 0);
391 writew(nv_crtc->lut.g[i] >> 2, lut + 8*i + 2);
392 writew(nv_crtc->lut.b[i] >> 2, lut + 8*i + 4);
397 nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
398 uint32_t handle, uint32_t width, uint32_t height)
400 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
401 struct drm_device *dev = crtc->dev;
402 struct drm_gem_object *gem;
403 struct nouveau_bo *nvbo;
404 bool visible = (handle != 0);
408 if (width != 64 || height != 64)
411 gem = drm_gem_object_lookup(dev, file_priv, handle);
414 nvbo = nouveau_gem_object(gem);
416 ret = nouveau_bo_map(nvbo);
418 for (i = 0; i < 64 * 64; i++) {
419 u32 v = nouveau_bo_rd32(nvbo, i);
420 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
422 nouveau_bo_unmap(nvbo);
425 drm_gem_object_unreference_unlocked(gem);
428 if (visible != nv_crtc->cursor.visible) {
429 nvd0_crtc_cursor_show(nv_crtc, visible, true);
430 nv_crtc->cursor.visible = visible;
437 nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
439 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
440 const u32 data = (y << 16) | x;
442 nv_wr32(crtc->dev, 0x64d084 + (nv_crtc->index * 0x1000), data);
443 nv_wr32(crtc->dev, 0x64d080 + (nv_crtc->index * 0x1000), 0x00000000);
448 nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
449 uint32_t start, uint32_t size)
451 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
452 u32 end = max(start + size, (u32)256);
455 for (i = start; i < end; i++) {
456 nv_crtc->lut.r[i] = r[i];
457 nv_crtc->lut.g[i] = g[i];
458 nv_crtc->lut.b[i] = b[i];
461 nvd0_crtc_lut_load(crtc);
465 nvd0_crtc_destroy(struct drm_crtc *crtc)
467 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
468 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
469 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
470 nouveau_bo_unmap(nv_crtc->lut.nvbo);
471 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
472 drm_crtc_cleanup(crtc);
476 static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
477 .dpms = nvd0_crtc_dpms,
478 .prepare = nvd0_crtc_prepare,
479 .commit = nvd0_crtc_commit,
480 .mode_fixup = nvd0_crtc_mode_fixup,
481 .mode_set = nvd0_crtc_mode_set,
482 .mode_set_base = nvd0_crtc_mode_set_base,
483 .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
484 .load_lut = nvd0_crtc_lut_load,
487 static const struct drm_crtc_funcs nvd0_crtc_func = {
488 .cursor_set = nvd0_crtc_cursor_set,
489 .cursor_move = nvd0_crtc_cursor_move,
490 .gamma_set = nvd0_crtc_gamma_set,
491 .set_config = drm_crtc_helper_set_config,
492 .destroy = nvd0_crtc_destroy,
496 nvd0_crtc_create(struct drm_device *dev, int index)
498 struct nouveau_crtc *nv_crtc;
499 struct drm_crtc *crtc;
502 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
506 nv_crtc->index = index;
507 nv_crtc->set_dither = nvd0_crtc_set_dither;
508 nv_crtc->set_scale = nvd0_crtc_set_scale;
509 for (i = 0; i < 256; i++) {
510 nv_crtc->lut.r[i] = i << 8;
511 nv_crtc->lut.g[i] = i << 8;
512 nv_crtc->lut.b[i] = i << 8;
515 crtc = &nv_crtc->base;
516 drm_crtc_init(dev, crtc, &nvd0_crtc_func);
517 drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
518 drm_mode_crtc_set_gamma_size(crtc, 256);
520 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
521 0, 0x0000, &nv_crtc->cursor.nvbo);
523 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
525 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
527 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
533 ret = nouveau_bo_new(dev, 4096, 0x100, TTM_PL_FLAG_VRAM,
534 0, 0x0000, &nv_crtc->lut.nvbo);
536 ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
538 ret = nouveau_bo_map(nv_crtc->lut.nvbo);
540 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
546 nvd0_crtc_lut_load(crtc);
550 nvd0_crtc_destroy(crtc);
554 /******************************************************************************
556 *****************************************************************************/
558 nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
560 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
561 struct drm_device *dev = encoder->dev;
562 int or = nv_encoder->or;
565 dpms_ctrl = 0x80000000;
566 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
567 dpms_ctrl |= 0x00000001;
568 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
569 dpms_ctrl |= 0x00000004;
571 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
572 nv_mask(dev, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
573 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
577 nvd0_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
578 struct drm_display_mode *adjusted_mode)
580 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
581 struct nouveau_connector *nv_connector;
583 nv_connector = nouveau_encoder_connector_get(nv_encoder);
584 if (nv_connector && nv_connector->native_mode) {
585 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
586 int id = adjusted_mode->base.id;
587 *adjusted_mode = *nv_connector->native_mode;
588 adjusted_mode->base.id = id;
596 nvd0_dac_prepare(struct drm_encoder *encoder)
601 nvd0_dac_commit(struct drm_encoder *encoder)
606 nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
607 struct drm_display_mode *adjusted_mode)
609 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
610 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
613 nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
615 push = evo_wait(encoder->dev, 0, 2);
617 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
618 evo_data(push, 1 << nv_crtc->index);
619 evo_kick(push, encoder->dev, 0);
622 nv_encoder->crtc = encoder->crtc;
626 nvd0_dac_disconnect(struct drm_encoder *encoder)
628 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
629 struct drm_device *dev = encoder->dev;
632 if (nv_encoder->crtc) {
633 nvd0_crtc_prepare(nv_encoder->crtc);
635 push = evo_wait(dev, 0, 4);
637 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
638 evo_data(push, 0x00000000);
639 evo_mthd(push, 0x0080, 1);
640 evo_data(push, 0x00000000);
641 evo_kick(push, dev, 0);
644 nv_encoder->crtc = NULL;
648 static enum drm_connector_status
649 nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
651 return connector_status_disconnected;
655 nvd0_dac_destroy(struct drm_encoder *encoder)
657 drm_encoder_cleanup(encoder);
661 static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
662 .dpms = nvd0_dac_dpms,
663 .mode_fixup = nvd0_dac_mode_fixup,
664 .prepare = nvd0_dac_prepare,
665 .commit = nvd0_dac_commit,
666 .mode_set = nvd0_dac_mode_set,
667 .disable = nvd0_dac_disconnect,
668 .get_crtc = nvd0_display_crtc_get,
669 .detect = nvd0_dac_detect
672 static const struct drm_encoder_funcs nvd0_dac_func = {
673 .destroy = nvd0_dac_destroy,
677 nvd0_dac_create(struct drm_connector *connector, struct dcb_entry *dcbe)
679 struct drm_device *dev = connector->dev;
680 struct nouveau_encoder *nv_encoder;
681 struct drm_encoder *encoder;
683 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
686 nv_encoder->dcb = dcbe;
687 nv_encoder->or = ffs(dcbe->or) - 1;
689 encoder = to_drm_encoder(nv_encoder);
690 encoder->possible_crtcs = dcbe->heads;
691 encoder->possible_clones = 0;
692 drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
693 drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
695 drm_mode_connector_attach_encoder(connector, encoder);
699 /******************************************************************************
701 *****************************************************************************/
703 nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
705 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
706 struct drm_device *dev = encoder->dev;
707 struct drm_encoder *partner;
708 int or = nv_encoder->or;
711 nv_encoder->last_dpms = mode;
713 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
714 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
716 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
719 if (nv_partner != nv_encoder &&
720 nv_partner->dcb->or == nv_encoder->or) {
721 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
727 dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
728 dpms_ctrl |= 0x80000000;
730 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
731 nv_mask(dev, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
732 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
733 nv_wait(dev, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
737 nvd0_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
738 struct drm_display_mode *adjusted_mode)
740 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
741 struct nouveau_connector *nv_connector;
743 nv_connector = nouveau_encoder_connector_get(nv_encoder);
744 if (nv_connector && nv_connector->native_mode) {
745 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
746 int id = adjusted_mode->base.id;
747 *adjusted_mode = *nv_connector->native_mode;
748 adjusted_mode->base.id = id;
756 nvd0_sor_prepare(struct drm_encoder *encoder)
761 nvd0_sor_commit(struct drm_encoder *encoder)
766 nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
767 struct drm_display_mode *adjusted_mode)
769 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
770 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
771 u32 mode_ctrl = (1 << nv_crtc->index);
774 if (nv_encoder->dcb->sorconf.link & 1) {
775 if (adjusted_mode->clock < 165000)
776 mode_ctrl |= 0x00000100;
778 mode_ctrl |= 0x00000500;
780 mode_ctrl |= 0x00000200;
783 nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
785 push = evo_wait(encoder->dev, 0, 2);
787 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
788 evo_data(push, mode_ctrl);
789 evo_kick(push, encoder->dev, 0);
792 nv_encoder->crtc = encoder->crtc;
796 nvd0_sor_disconnect(struct drm_encoder *encoder)
798 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
799 struct drm_device *dev = encoder->dev;
802 if (nv_encoder->crtc) {
803 nvd0_crtc_prepare(nv_encoder->crtc);
805 push = evo_wait(dev, 0, 4);
807 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
808 evo_data(push, 0x00000000);
809 evo_mthd(push, 0x0080, 1);
810 evo_data(push, 0x00000000);
811 evo_kick(push, dev, 0);
814 nv_encoder->crtc = NULL;
815 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
820 nvd0_sor_destroy(struct drm_encoder *encoder)
822 drm_encoder_cleanup(encoder);
826 static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
827 .dpms = nvd0_sor_dpms,
828 .mode_fixup = nvd0_sor_mode_fixup,
829 .prepare = nvd0_sor_prepare,
830 .commit = nvd0_sor_commit,
831 .mode_set = nvd0_sor_mode_set,
832 .disable = nvd0_sor_disconnect,
833 .get_crtc = nvd0_display_crtc_get,
836 static const struct drm_encoder_funcs nvd0_sor_func = {
837 .destroy = nvd0_sor_destroy,
841 nvd0_sor_create(struct drm_connector *connector, struct dcb_entry *dcbe)
843 struct drm_device *dev = connector->dev;
844 struct nouveau_encoder *nv_encoder;
845 struct drm_encoder *encoder;
847 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
850 nv_encoder->dcb = dcbe;
851 nv_encoder->or = ffs(dcbe->or) - 1;
852 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
854 encoder = to_drm_encoder(nv_encoder);
855 encoder->possible_crtcs = dcbe->heads;
856 encoder->possible_clones = 0;
857 drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
858 drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
860 drm_mode_connector_attach_encoder(connector, encoder);
864 /******************************************************************************
866 *****************************************************************************/
867 static struct dcb_entry *
868 lookup_dcb(struct drm_device *dev, int id, u32 mc)
870 struct drm_nouveau_private *dev_priv = dev->dev_private;
874 type = OUTPUT_ANALOG;
881 for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
882 struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
883 if (dcb->type == type && (dcb->or & (1 << or)))
887 NV_INFO(dev, "PDISP: DCB for %d/0x%08x not found\n", id, mc);
892 nvd0_display_unk1_handler(struct drm_device *dev)
894 struct nvd0_display *disp = nvd0_display(dev);
895 struct dcb_entry *dcb;
899 NV_INFO(dev, "PDISP: 1 0x%08x 0x%08x 0x%08x\n", nv_rd32(dev, 0x6101d0),
900 nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
902 unkn = nv_rd32(dev, 0x6101d4);
904 unkn = nv_rd32(dev, 0x6109d4);
908 disp->irq.ena = NULL;
909 disp->irq.dis = NULL;
910 disp->irq.crtc = crtc;
911 disp->irq.pclk = nv_rd32(dev, 0x660450 + (disp->irq.crtc * 0x300));
912 disp->irq.pclk /= 1000;
914 for (i = 0; i < 8; i++) {
915 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
916 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
918 if (mcc & (1 << crtc))
919 disp->irq.dis = lookup_dcb(dev, i, mcc);
921 if (mcp & (1 << crtc)) {
922 disp->irq.ena = lookup_dcb(dev, i, mcp);
923 switch (disp->irq.ena->type) {
925 disp->irq.script = 0x00ff;
928 disp->irq.script = (mcp & 0x00000f00) >> 8;
929 if (disp->irq.pclk >= 165000)
930 disp->irq.script |= 0x0100;
933 disp->irq.script = 0xbeef;
941 nouveau_bios_run_display_table(dev, 0x0000, -1, dcb, crtc);
943 nv_wr32(dev, 0x6101d4, 0x00000000);
944 nv_wr32(dev, 0x6109d4, 0x00000000);
945 nv_wr32(dev, 0x6101d0, 0x80000000);
949 nvd0_display_unk2_handler(struct drm_device *dev)
951 struct nvd0_display *disp = nvd0_display(dev);
952 struct dcb_entry *dcb;
953 int crtc = disp->irq.crtc;
954 int pclk = disp->irq.pclk;
958 NV_INFO(dev, "PDISP: 2 0x%08x 0x%08x 0x%08x\n", nv_rd32(dev, 0x6101d0),
959 nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
962 disp->irq.dis = NULL;
964 nouveau_bios_run_display_table(dev, 0x0000, -2, dcb, crtc);
966 nv50_crtc_set_clock(dev, crtc, pclk);
971 or = ffs(dcb->or) - 1;
973 nouveau_bios_run_display_table(dev, disp->irq.script, pclk, dcb, crtc);
975 nv_wr32(dev, 0x612200 + (crtc * 0x800), 0x00000000);
978 nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
981 if (disp->irq.pclk >= 165000)
986 nv_mask(dev, 0x612300 + (or * 0x800), 0x00000707, tmp);
993 nv_wr32(dev, 0x6101d4, 0x00000000);
994 nv_wr32(dev, 0x6109d4, 0x00000000);
995 nv_wr32(dev, 0x6101d0, 0x80000000);
999 nvd0_display_unk4_handler(struct drm_device *dev)
1001 struct nvd0_display *disp = nvd0_display(dev);
1002 struct dcb_entry *dcb;
1003 int crtc = disp->irq.crtc;
1004 int pclk = disp->irq.pclk;
1006 NV_INFO(dev, "PDISP: 4 0x%08x 0x%08x 0x%08x\n", nv_rd32(dev, 0x6101d0),
1007 nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
1009 dcb = disp->irq.ena;
1010 disp->irq.ena = NULL;
1014 nouveau_bios_run_display_table(dev, disp->irq.script, pclk, dcb, crtc);
1017 nv_wr32(dev, 0x6101d4, 0x00000000);
1018 nv_wr32(dev, 0x6109d4, 0x00000000);
1019 nv_wr32(dev, 0x6101d0, 0x80000000);
1023 nvd0_display_intr(struct drm_device *dev)
1025 u32 intr = nv_rd32(dev, 0x610088);
1027 if (intr & 0x00000002) {
1028 u32 stat = nv_rd32(dev, 0x61009c);
1029 int chid = ffs(stat) - 1;
1031 u32 mthd = nv_rd32(dev, 0x6101f0 + (chid * 12));
1032 u32 data = nv_rd32(dev, 0x6101f4 + (chid * 12));
1033 u32 unkn = nv_rd32(dev, 0x6101f8 + (chid * 12));
1035 NV_INFO(dev, "EvoCh: chid %d mthd 0x%04x data 0x%08x "
1037 chid, (mthd & 0x0000ffc), data, mthd, unkn);
1038 nv_wr32(dev, 0x61009c, (1 << chid));
1039 nv_wr32(dev, 0x6101f0 + (chid * 12), 0x90000000);
1042 intr &= ~0x00000002;
1045 if (intr & 0x00100000) {
1046 u32 stat = nv_rd32(dev, 0x6100ac);
1048 if (stat & 0x00000007) {
1049 nv_wr32(dev, 0x6100ac, (stat & 0x00000007));
1051 if (stat & 0x00000001)
1052 nvd0_display_unk1_handler(dev);
1053 if (stat & 0x00000002)
1054 nvd0_display_unk2_handler(dev);
1055 if (stat & 0x00000004)
1056 nvd0_display_unk4_handler(dev);
1057 stat &= ~0x00000007;
1061 NV_INFO(dev, "PDISP: unknown intr24 0x%08x\n", stat);
1062 nv_wr32(dev, 0x6100ac, stat);
1065 intr &= ~0x00100000;
1068 if (intr & 0x01000000) {
1069 u32 stat = nv_rd32(dev, 0x6100bc);
1070 nv_wr32(dev, 0x6100bc, stat);
1071 intr &= ~0x01000000;
1074 if (intr & 0x02000000) {
1075 u32 stat = nv_rd32(dev, 0x6108bc);
1076 nv_wr32(dev, 0x6108bc, stat);
1077 intr &= ~0x02000000;
1081 NV_INFO(dev, "PDISP: unknown intr 0x%08x\n", intr);
1084 /******************************************************************************
1086 *****************************************************************************/
1088 nvd0_display_fini(struct drm_device *dev)
1093 for (i = 14; i >= 13; i--) {
1094 if (!(nv_rd32(dev, 0x610490 + (i * 0x10)) & 0x00000001))
1097 nv_mask(dev, 0x610490 + (i * 0x10), 0x00000001, 0x00000000);
1098 nv_wait(dev, 0x610490 + (i * 0x10), 0x00010000, 0x00000000);
1099 nv_mask(dev, 0x610090, 1 << i, 0x00000000);
1100 nv_mask(dev, 0x6100a0, 1 << i, 0x00000000);
1104 if (nv_rd32(dev, 0x610490) & 0x00000010) {
1105 nv_mask(dev, 0x610490, 0x00000010, 0x00000000);
1106 nv_mask(dev, 0x610490, 0x00000003, 0x00000000);
1107 nv_wait(dev, 0x610490, 0x80000000, 0x00000000);
1108 nv_mask(dev, 0x610090, 0x00000001, 0x00000000);
1109 nv_mask(dev, 0x6100a0, 0x00000001, 0x00000000);
1114 nvd0_display_init(struct drm_device *dev)
1116 struct nvd0_display *disp = nvd0_display(dev);
1120 if (nv_rd32(dev, 0x6100ac) & 0x00000100) {
1121 nv_wr32(dev, 0x6100ac, 0x00000100);
1122 nv_mask(dev, 0x6194e8, 0x00000001, 0x00000000);
1123 if (!nv_wait(dev, 0x6194e8, 0x00000002, 0x00000000)) {
1124 NV_ERROR(dev, "PDISP: 0x6194e8 0x%08x\n",
1125 nv_rd32(dev, 0x6194e8));
1130 /* nfi what these are exactly, i do know that SOR_MODE_CTRL won't
1131 * work at all unless you do the SOR part below.
1133 for (i = 0; i < 3; i++) {
1134 u32 dac = nv_rd32(dev, 0x61a000 + (i * 0x800));
1135 nv_wr32(dev, 0x6101c0 + (i * 0x800), dac);
1138 for (i = 0; i < 4; i++) {
1139 u32 sor = nv_rd32(dev, 0x61c000 + (i * 0x800));
1140 nv_wr32(dev, 0x6301c4 + (i * 0x800), sor);
1143 for (i = 0; i < 2; i++) {
1144 u32 crtc0 = nv_rd32(dev, 0x616104 + (i * 0x800));
1145 u32 crtc1 = nv_rd32(dev, 0x616108 + (i * 0x800));
1146 u32 crtc2 = nv_rd32(dev, 0x61610c + (i * 0x800));
1147 nv_wr32(dev, 0x6101b4 + (i * 0x800), crtc0);
1148 nv_wr32(dev, 0x6101b8 + (i * 0x800), crtc1);
1149 nv_wr32(dev, 0x6101bc + (i * 0x800), crtc2);
1152 /* point at our hash table / objects, enable interrupts */
1153 nv_wr32(dev, 0x610010, (disp->mem->vinst >> 8) | 9);
1154 nv_mask(dev, 0x6100b0, 0x00000307, 0x00000307);
1157 nv_wr32(dev, 0x610494, (disp->evo[0].handle >> 8) | 3);
1158 nv_wr32(dev, 0x610498, 0x00010000);
1159 nv_wr32(dev, 0x61049c, 0x00000001);
1160 nv_mask(dev, 0x610490, 0x00000010, 0x00000010);
1161 nv_wr32(dev, 0x640000, 0x00000000);
1162 nv_wr32(dev, 0x610490, 0x01000013);
1163 if (!nv_wait(dev, 0x610490, 0x80000000, 0x00000000)) {
1164 NV_ERROR(dev, "PDISP: master 0x%08x\n",
1165 nv_rd32(dev, 0x610490));
1168 nv_mask(dev, 0x610090, 0x00000001, 0x00000001);
1169 nv_mask(dev, 0x6100a0, 0x00000001, 0x00000001);
1172 for (i = 13; i <= 14; i++) {
1173 nv_wr32(dev, 0x610490 + (i * 0x10), 0x00000001);
1174 if (!nv_wait(dev, 0x610490 + (i * 0x10), 0x00010000, 0x00010000)) {
1175 NV_ERROR(dev, "PDISP: curs%d 0x%08x\n", i,
1176 nv_rd32(dev, 0x610490 + (i * 0x10)));
1180 nv_mask(dev, 0x610090, 1 << i, 1 << i);
1181 nv_mask(dev, 0x6100a0, 1 << i, 1 << i);
1184 push = evo_wait(dev, 0, 32);
1187 evo_mthd(push, 0x0088, 1);
1188 evo_data(push, MEM_SYNC);
1189 evo_mthd(push, 0x0084, 1);
1190 evo_data(push, 0x00000000);
1191 evo_mthd(push, 0x0084, 1);
1192 evo_data(push, 0x80000000);
1193 evo_mthd(push, 0x008c, 1);
1194 evo_data(push, 0x00000000);
1195 evo_kick(push, dev, 0);
1201 nvd0_display_destroy(struct drm_device *dev)
1203 struct drm_nouveau_private *dev_priv = dev->dev_private;
1204 struct nvd0_display *disp = nvd0_display(dev);
1205 struct pci_dev *pdev = dev->pdev;
1207 nvd0_display_fini(dev);
1209 pci_free_consistent(pdev, PAGE_SIZE, disp->evo[0].ptr, disp->evo[0].handle);
1210 nouveau_gpuobj_ref(NULL, &disp->mem);
1211 nouveau_irq_unregister(dev, 26);
1213 dev_priv->engine.display.priv = NULL;
1218 nvd0_display_create(struct drm_device *dev)
1220 struct drm_nouveau_private *dev_priv = dev->dev_private;
1221 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
1222 struct dcb_table *dcb = &dev_priv->vbios.dcb;
1223 struct drm_connector *connector, *tmp;
1224 struct pci_dev *pdev = dev->pdev;
1225 struct nvd0_display *disp;
1226 struct dcb_entry *dcbe;
1229 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
1232 dev_priv->engine.display.priv = disp;
1234 /* create crtc objects to represent the hw heads */
1235 for (i = 0; i < 2; i++) {
1236 ret = nvd0_crtc_create(dev, i);
1241 /* create encoder/connector objects based on VBIOS DCB table */
1242 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
1243 connector = nouveau_connector_create(dev, dcbe->connector);
1244 if (IS_ERR(connector))
1247 if (dcbe->location != DCB_LOC_ON_CHIP) {
1248 NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
1249 dcbe->type, ffs(dcbe->or) - 1);
1253 switch (dcbe->type) {
1255 nvd0_sor_create(connector, dcbe);
1258 nvd0_dac_create(connector, dcbe);
1261 NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
1262 dcbe->type, ffs(dcbe->or) - 1);
1267 /* cull any connectors we created that don't have an encoder */
1268 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
1269 if (connector->encoder_ids[0])
1272 NV_WARN(dev, "%s has no encoders, removing\n",
1273 drm_get_connector_name(connector));
1274 connector->funcs->destroy(connector);
1277 /* setup interrupt handling */
1278 nouveau_irq_register(dev, 26, nvd0_display_intr);
1280 /* hash table and dma objects for the memory areas we care about */
1281 ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
1282 NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
1286 nv_wo32(disp->mem, 0x1000, 0x00000049);
1287 nv_wo32(disp->mem, 0x1004, (disp->mem->vinst + 0x2000) >> 8);
1288 nv_wo32(disp->mem, 0x1008, (disp->mem->vinst + 0x2fff) >> 8);
1289 nv_wo32(disp->mem, 0x100c, 0x00000000);
1290 nv_wo32(disp->mem, 0x1010, 0x00000000);
1291 nv_wo32(disp->mem, 0x1014, 0x00000000);
1292 nv_wo32(disp->mem, 0x0000, MEM_SYNC);
1293 nv_wo32(disp->mem, 0x0004, (0x1000 << 9) | 0x00000001);
1295 nv_wo32(disp->mem, 0x1020, 0x00000049);
1296 nv_wo32(disp->mem, 0x1024, 0x00000000);
1297 nv_wo32(disp->mem, 0x1028, (dev_priv->vram_size - 1) >> 8);
1298 nv_wo32(disp->mem, 0x102c, 0x00000000);
1299 nv_wo32(disp->mem, 0x1030, 0x00000000);
1300 nv_wo32(disp->mem, 0x1034, 0x00000000);
1301 nv_wo32(disp->mem, 0x0008, MEM_VRAM);
1302 nv_wo32(disp->mem, 0x000c, (0x1020 << 9) | 0x00000001);
1304 nv_wo32(disp->mem, 0x1040, 0x00000009);
1305 nv_wo32(disp->mem, 0x1044, 0x00000000);
1306 nv_wo32(disp->mem, 0x1048, (dev_priv->vram_size - 1) >> 8);
1307 nv_wo32(disp->mem, 0x104c, 0x00000000);
1308 nv_wo32(disp->mem, 0x1050, 0x00000000);
1309 nv_wo32(disp->mem, 0x1054, 0x00000000);
1310 nv_wo32(disp->mem, 0x0010, NvEvoVRAM_LP);
1311 nv_wo32(disp->mem, 0x0014, (0x1040 << 9) | 0x00000001);
1313 nv_wo32(disp->mem, 0x1060, 0x0fe00009);
1314 nv_wo32(disp->mem, 0x1064, 0x00000000);
1315 nv_wo32(disp->mem, 0x1068, (dev_priv->vram_size - 1) >> 8);
1316 nv_wo32(disp->mem, 0x106c, 0x00000000);
1317 nv_wo32(disp->mem, 0x1070, 0x00000000);
1318 nv_wo32(disp->mem, 0x1074, 0x00000000);
1319 nv_wo32(disp->mem, 0x0018, NvEvoFB32);
1320 nv_wo32(disp->mem, 0x001c, (0x1060 << 9) | 0x00000001);
1322 pinstmem->flush(dev);
1324 /* push buffers for evo channels */
1326 pci_alloc_consistent(pdev, PAGE_SIZE, &disp->evo[0].handle);
1327 if (!disp->evo[0].ptr) {
1332 ret = nvd0_display_init(dev);
1338 nvd0_display_destroy(dev);