blob: ae6d80fff4bd0947a8b51e6c01f9dde5e0aa15bc [file] [log] [blame]
Ben Skeggs26f6d882011-07-04 16:25:18 +10001/*
2 * Copyright 2011 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
Ben Skeggs51beb422011-07-05 10:33:08 +100025#include <linux/dma-mapping.h>
Ben Skeggs83fc0832011-07-05 13:08:40 +100026
Ben Skeggs26f6d882011-07-04 16:25:18 +100027#include "drmP.h"
Ben Skeggs83fc0832011-07-05 13:08:40 +100028#include "drm_crtc_helper.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100029
30#include "nouveau_drv.h"
31#include "nouveau_connector.h"
32#include "nouveau_encoder.h"
33#include "nouveau_crtc.h"
Ben Skeggs37b034a2011-07-08 14:43:19 +100034#include "nouveau_dma.h"
Ben Skeggs438d99e2011-07-05 16:48:06 +100035#include "nouveau_fb.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100036#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100037
Ben Skeggs8a464382011-11-12 23:52:07 +100038#define EVO_DMA_NR 9
39
Ben Skeggsbdb8c212011-11-12 01:30:24 +100040#define EVO_MASTER (0x00)
Ben Skeggsa63a97e2011-11-16 15:22:34 +100041#define EVO_FLIP(c) (0x01 + (c))
Ben Skeggs8a464382011-11-12 23:52:07 +100042#define EVO_OVLY(c) (0x05 + (c))
43#define EVO_OIMM(c) (0x09 + (c))
Ben Skeggsbdb8c212011-11-12 01:30:24 +100044#define EVO_CURS(c) (0x0d + (c))
45
Ben Skeggs816af2f2011-11-16 15:48:48 +100046/* offsets in shared sync bo of various structures */
47#define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
48#define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
49#define EVO_FLIP_SEM0(c) EVO_SYNC((c), 0x00)
50#define EVO_FLIP_SEM1(c) EVO_SYNC((c), 0x10)
51
Ben Skeggs3376ee32011-11-12 14:28:12 +100052struct evo {
53 int idx;
54 dma_addr_t handle;
55 u32 *ptr;
56 struct {
Ben Skeggs3376ee32011-11-12 14:28:12 +100057 u32 offset;
58 u16 value;
59 } sem;
60};
61
Ben Skeggs26f6d882011-07-04 16:25:18 +100062struct nvd0_display {
63 struct nouveau_gpuobj *mem;
Ben Skeggs816af2f2011-11-16 15:48:48 +100064 struct nouveau_bo *sync;
Ben Skeggs8a464382011-11-12 23:52:07 +100065 struct evo evo[9];
Ben Skeggsf20ce962011-07-08 13:17:01 +100066
67 struct tasklet_struct tasklet;
Ben Skeggsee417792011-07-08 14:34:45 +100068 u32 modeset;
Ben Skeggs26f6d882011-07-04 16:25:18 +100069};
70
71static struct nvd0_display *
72nvd0_display(struct drm_device *dev)
73{
74 struct drm_nouveau_private *dev_priv = dev->dev_private;
75 return dev_priv->engine.display.priv;
76}
77
Ben Skeggsbdb8c212011-11-12 01:30:24 +100078static struct drm_crtc *
79nvd0_display_crtc_get(struct drm_encoder *encoder)
80{
81 return nouveau_encoder(encoder)->crtc;
82}
83
84/******************************************************************************
85 * EVO channel helpers
86 *****************************************************************************/
Ben Skeggs37b034a2011-07-08 14:43:19 +100087static inline int
Ben Skeggs51beb422011-07-05 10:33:08 +100088evo_icmd(struct drm_device *dev, int id, u32 mthd, u32 data)
89{
90 int ret = 0;
91 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000001);
92 nv_wr32(dev, 0x610704 + (id * 0x10), data);
93 nv_mask(dev, 0x610704 + (id * 0x10), 0x80000ffc, 0x80000000 | mthd);
94 if (!nv_wait(dev, 0x610704 + (id * 0x10), 0x80000000, 0x00000000))
95 ret = -EBUSY;
96 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000000);
97 return ret;
98}
99
100static u32 *
101evo_wait(struct drm_device *dev, int id, int nr)
102{
103 struct nvd0_display *disp = nvd0_display(dev);
104 u32 put = nv_rd32(dev, 0x640000 + (id * 0x1000)) / 4;
105
106 if (put + nr >= (PAGE_SIZE / 4)) {
107 disp->evo[id].ptr[put] = 0x20000000;
108
109 nv_wr32(dev, 0x640000 + (id * 0x1000), 0x00000000);
110 if (!nv_wait(dev, 0x640004 + (id * 0x1000), ~0, 0x00000000)) {
111 NV_ERROR(dev, "evo %d dma stalled\n", id);
112 return NULL;
113 }
114
115 put = 0;
116 }
117
Ben Skeggs27517dd2011-11-11 20:26:44 +1000118 if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
119 NV_INFO(dev, "Evo%d: %p START\n", id, disp->evo[id].ptr + put);
120
Ben Skeggs51beb422011-07-05 10:33:08 +1000121 return disp->evo[id].ptr + put;
122}
123
124static void
125evo_kick(u32 *push, struct drm_device *dev, int id)
126{
127 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs27517dd2011-11-11 20:26:44 +1000128
129 if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO) {
130 u32 curp = nv_rd32(dev, 0x640000 + (id * 0x1000)) >> 2;
131 u32 *cur = disp->evo[id].ptr + curp;
132
133 while (cur < push)
134 NV_INFO(dev, "Evo%d: 0x%08x\n", id, *cur++);
135 NV_INFO(dev, "Evo%d: %p KICK!\n", id, push);
136 }
137
Ben Skeggs51beb422011-07-05 10:33:08 +1000138 nv_wr32(dev, 0x640000 + (id * 0x1000), (push - disp->evo[id].ptr) << 2);
139}
140
141#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
142#define evo_data(p,d) *((p)++) = (d)
143
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000144static int
145evo_init_dma(struct drm_device *dev, int ch)
Ben Skeggs83fc0832011-07-05 13:08:40 +1000146{
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000147 struct nvd0_display *disp = nvd0_display(dev);
148 u32 flags;
149
150 flags = 0x00000000;
151 if (ch == EVO_MASTER)
152 flags |= 0x01000000;
153
154 nv_wr32(dev, 0x610494 + (ch * 0x0010), (disp->evo[ch].handle >> 8) | 3);
155 nv_wr32(dev, 0x610498 + (ch * 0x0010), 0x00010000);
156 nv_wr32(dev, 0x61049c + (ch * 0x0010), 0x00000001);
157 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000010);
158 nv_wr32(dev, 0x640000 + (ch * 0x1000), 0x00000000);
159 nv_wr32(dev, 0x610490 + (ch * 0x0010), 0x00000013 | flags);
160 if (!nv_wait(dev, 0x610490 + (ch * 0x0010), 0x80000000, 0x00000000)) {
161 NV_ERROR(dev, "PDISP: ch%d 0x%08x\n", ch,
162 nv_rd32(dev, 0x610490 + (ch * 0x0010)));
163 return -EBUSY;
164 }
165
166 nv_mask(dev, 0x610090, (1 << ch), (1 << ch));
167 nv_mask(dev, 0x6100a0, (1 << ch), (1 << ch));
168 return 0;
169}
170
171static void
172evo_fini_dma(struct drm_device *dev, int ch)
173{
174 if (!(nv_rd32(dev, 0x610490 + (ch * 0x0010)) & 0x00000010))
175 return;
176
177 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000000);
178 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000003, 0x00000000);
179 nv_wait(dev, 0x610490 + (ch * 0x0010), 0x80000000, 0x00000000);
180 nv_mask(dev, 0x610090, (1 << ch), 0x00000000);
181 nv_mask(dev, 0x6100a0, (1 << ch), 0x00000000);
182}
183
Ben Skeggs4acd4292011-11-12 12:57:54 +1000184static inline void
185evo_piow(struct drm_device *dev, int ch, u16 mthd, u32 data)
186{
187 nv_wr32(dev, 0x640000 + (ch * 0x1000) + mthd, data);
188}
189
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000190static int
191evo_init_pio(struct drm_device *dev, int ch)
192{
193 nv_wr32(dev, 0x610490 + (ch * 0x0010), 0x00000001);
194 if (!nv_wait(dev, 0x610490 + (ch * 0x0010), 0x00010000, 0x00010000)) {
195 NV_ERROR(dev, "PDISP: ch%d 0x%08x\n", ch,
196 nv_rd32(dev, 0x610490 + (ch * 0x0010)));
197 return -EBUSY;
198 }
199
200 nv_mask(dev, 0x610090, (1 << ch), (1 << ch));
201 nv_mask(dev, 0x6100a0, (1 << ch), (1 << ch));
202 return 0;
203}
204
205static void
206evo_fini_pio(struct drm_device *dev, int ch)
207{
208 if (!(nv_rd32(dev, 0x610490 + (ch * 0x0010)) & 0x00000001))
209 return;
210
211 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000010);
212 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000001, 0x00000000);
213 nv_wait(dev, 0x610490 + (ch * 0x0010), 0x00010000, 0x00000000);
214 nv_mask(dev, 0x610090, (1 << ch), 0x00000000);
215 nv_mask(dev, 0x6100a0, (1 << ch), 0x00000000);
Ben Skeggs83fc0832011-07-05 13:08:40 +1000216}
217
Ben Skeggs3376ee32011-11-12 14:28:12 +1000218static bool
219evo_sync_wait(void *data)
220{
Ben Skeggs816af2f2011-11-16 15:48:48 +1000221 return nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000222}
223
224static int
225evo_sync(struct drm_device *dev, int ch)
226{
227 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000228 u32 *push = evo_wait(dev, ch, 8);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000229 if (push) {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000230 nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000231 evo_mthd(push, 0x0084, 1);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000232 evo_data(push, 0x80000000 | EVO_MAST_NTFY);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000233 evo_mthd(push, 0x0080, 2);
234 evo_data(push, 0x00000000);
235 evo_data(push, 0x00000000);
236 evo_kick(push, dev, ch);
Ben Skeggs816af2f2011-11-16 15:48:48 +1000237 if (nv_wait_cb(dev, evo_sync_wait, disp->sync))
Ben Skeggs3376ee32011-11-12 14:28:12 +1000238 return 0;
239 }
240
241 return -EBUSY;
242}
243
244/******************************************************************************
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000245 * Page flipping channel
Ben Skeggs3376ee32011-11-12 14:28:12 +1000246 *****************************************************************************/
247struct nouveau_bo *
248nvd0_display_crtc_sema(struct drm_device *dev, int crtc)
249{
Ben Skeggs816af2f2011-11-16 15:48:48 +1000250 return nvd0_display(dev)->sync;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000251}
252
253void
254nvd0_display_flip_stop(struct drm_crtc *crtc)
255{
256 struct nvd0_display *disp = nvd0_display(crtc->dev);
257 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000258 struct evo *evo = &disp->evo[EVO_FLIP(nv_crtc->index)];
Ben Skeggs3376ee32011-11-12 14:28:12 +1000259 u32 *push;
260
261 push = evo_wait(crtc->dev, evo->idx, 8);
262 if (push) {
263 evo_mthd(push, 0x0084, 1);
264 evo_data(push, 0x00000000);
265 evo_mthd(push, 0x0094, 1);
266 evo_data(push, 0x00000000);
267 evo_mthd(push, 0x00c0, 1);
268 evo_data(push, 0x00000000);
269 evo_mthd(push, 0x0080, 1);
270 evo_data(push, 0x00000000);
271 evo_kick(push, crtc->dev, evo->idx);
272 }
273}
274
275int
276nvd0_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
277 struct nouveau_channel *chan, u32 swap_interval)
278{
279 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
280 struct nvd0_display *disp = nvd0_display(crtc->dev);
281 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggsa63a97e2011-11-16 15:22:34 +1000282 struct evo *evo = &disp->evo[EVO_FLIP(nv_crtc->index)];
Ben Skeggs3376ee32011-11-12 14:28:12 +1000283 u64 offset;
284 u32 *push;
285 int ret;
286
287 swap_interval <<= 4;
288 if (swap_interval == 0)
289 swap_interval |= 0x100;
290
291 push = evo_wait(crtc->dev, evo->idx, 128);
292 if (unlikely(push == NULL))
293 return -EBUSY;
294
295 /* synchronise with the rendering channel, if necessary */
296 if (likely(chan)) {
297 ret = RING_SPACE(chan, 10);
298 if (ret)
299 return ret;
300
301 offset = chan->dispc_vma[nv_crtc->index].offset;
302 offset += evo->sem.offset;
303
304 BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0010, 4);
305 OUT_RING (chan, upper_32_bits(offset));
306 OUT_RING (chan, lower_32_bits(offset));
307 OUT_RING (chan, 0xf00d0000 | evo->sem.value);
308 OUT_RING (chan, 0x1002);
309 BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0010, 4);
310 OUT_RING (chan, upper_32_bits(offset));
311 OUT_RING (chan, lower_32_bits(offset ^ 0x10));
312 OUT_RING (chan, 0x74b1e000);
313 OUT_RING (chan, 0x1001);
314 FIRE_RING (chan);
315 } else {
Ben Skeggs816af2f2011-11-16 15:48:48 +1000316 nouveau_bo_wr32(disp->sync, evo->sem.offset / 4,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000317 0xf00d0000 | evo->sem.value);
318 evo_sync(crtc->dev, EVO_MASTER);
319 }
320
321 /* queue the flip */
322 evo_mthd(push, 0x0100, 1);
323 evo_data(push, 0xfffe0000);
324 evo_mthd(push, 0x0084, 1);
325 evo_data(push, swap_interval);
326 if (!(swap_interval & 0x00000100)) {
327 evo_mthd(push, 0x00e0, 1);
328 evo_data(push, 0x40000000);
329 }
330 evo_mthd(push, 0x0088, 4);
331 evo_data(push, evo->sem.offset);
332 evo_data(push, 0xf00d0000 | evo->sem.value);
333 evo_data(push, 0x74b1e000);
334 evo_data(push, NvEvoSync);
335 evo_mthd(push, 0x00a0, 2);
336 evo_data(push, 0x00000000);
337 evo_data(push, 0x00000000);
338 evo_mthd(push, 0x00c0, 1);
339 evo_data(push, nv_fb->r_dma);
340 evo_mthd(push, 0x0110, 2);
341 evo_data(push, 0x00000000);
342 evo_data(push, 0x00000000);
343 evo_mthd(push, 0x0400, 5);
344 evo_data(push, nv_fb->nvbo->bo.offset >> 8);
345 evo_data(push, 0);
346 evo_data(push, (fb->height << 16) | fb->width);
347 evo_data(push, nv_fb->r_pitch);
348 evo_data(push, nv_fb->r_format);
349 evo_mthd(push, 0x0080, 1);
350 evo_data(push, 0x00000000);
351 evo_kick(push, crtc->dev, evo->idx);
352
353 evo->sem.offset ^= 0x10;
354 evo->sem.value++;
355 return 0;
356}
357
Ben Skeggs26f6d882011-07-04 16:25:18 +1000358/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000359 * CRTC
360 *****************************************************************************/
361static int
Ben Skeggs488ff202011-10-17 10:38:10 +1000362nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000363{
364 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggsde691852011-10-17 12:23:41 +1000365 struct nouveau_connector *nv_connector;
366 struct drm_connector *connector;
367 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000368
Ben Skeggs488ff202011-10-17 10:38:10 +1000369 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000370 connector = &nv_connector->base;
371 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
372 if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
373 mode = DITHERING_MODE_DYNAMIC2X2;
374 } else {
375 mode = nv_connector->dithering_mode;
376 }
377
378 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
379 if (connector->display_info.bpc >= 8)
380 mode |= DITHERING_DEPTH_8BPC;
381 } else {
382 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000383 }
384
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000385 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000386 if (push) {
387 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x300), 1);
388 evo_data(push, mode);
389 if (update) {
390 evo_mthd(push, 0x0080, 1);
391 evo_data(push, 0x00000000);
392 }
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000393 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000394 }
395
396 return 0;
397}
398
399static int
Ben Skeggs488ff202011-10-17 10:38:10 +1000400nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000401{
Ben Skeggs92854622011-11-11 23:49:06 +1000402 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000403 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggs3376ee32011-11-12 14:28:12 +1000404 struct drm_crtc *crtc = &nv_crtc->base;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000405 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000406 int mode = DRM_MODE_SCALE_NONE;
407 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000408
Ben Skeggs92854622011-11-11 23:49:06 +1000409 /* start off at the resolution we programmed the crtc for, this
410 * effectively handles NONE/FULL scaling
411 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000412 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000413 if (nv_connector && nv_connector->native_mode)
414 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000415
Ben Skeggs92854622011-11-11 23:49:06 +1000416 if (mode != DRM_MODE_SCALE_NONE)
417 omode = nv_connector->native_mode;
418 else
419 omode = umode;
420
421 oX = omode->hdisplay;
422 oY = omode->vdisplay;
423 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
424 oY *= 2;
425
426 /* add overscan compensation if necessary, will keep the aspect
427 * ratio the same as the backend mode unless overridden by the
428 * user setting both hborder and vborder properties.
429 */
430 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
431 (nv_connector->underscan == UNDERSCAN_AUTO &&
432 nv_connector->edid &&
433 drm_detect_hdmi_monitor(nv_connector->edid)))) {
434 u32 bX = nv_connector->underscan_hborder;
435 u32 bY = nv_connector->underscan_vborder;
436 u32 aspect = (oY << 19) / oX;
437
438 if (bX) {
439 oX -= (bX * 2);
440 if (bY) oY -= (bY * 2);
441 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
442 } else {
443 oX -= (oX >> 4) + 32;
444 if (bY) oY -= (bY * 2);
445 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000446 }
447 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000448
Ben Skeggs92854622011-11-11 23:49:06 +1000449 /* handle CENTER/ASPECT scaling, taking into account the areas
450 * removed already for overscan compensation
451 */
452 switch (mode) {
453 case DRM_MODE_SCALE_CENTER:
454 oX = min((u32)umode->hdisplay, oX);
455 oY = min((u32)umode->vdisplay, oY);
456 /* fall-through */
457 case DRM_MODE_SCALE_ASPECT:
458 if (oY < oX) {
459 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
460 oX = ((oY * aspect) + (aspect / 2)) >> 19;
461 } else {
462 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
463 oY = ((oX * aspect) + (aspect / 2)) >> 19;
464 }
465 break;
466 default:
467 break;
468 }
469
Ben Skeggs3376ee32011-11-12 14:28:12 +1000470 push = evo_wait(dev, EVO_MASTER, 8);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000471 if (push) {
472 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
Ben Skeggs92854622011-11-11 23:49:06 +1000473 evo_data(push, (oY << 16) | oX);
474 evo_data(push, (oY << 16) | oX);
475 evo_data(push, (oY << 16) | oX);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000476 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
477 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000478 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
Ben Skeggs92854622011-11-11 23:49:06 +1000479 evo_data(push, (umode->vdisplay << 16) | umode->hdisplay);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000480 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000481 if (update) {
482 nvd0_display_flip_stop(crtc);
483 nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
484 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000485 }
486
487 return 0;
488}
489
490static int
491nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
492 int x, int y, bool update)
493{
494 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
495 u32 *push;
496
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000497 push = evo_wait(fb->dev, EVO_MASTER, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000498 if (push) {
499 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
500 evo_data(push, nvfb->nvbo->bo.offset >> 8);
501 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
502 evo_data(push, (fb->height << 16) | fb->width);
503 evo_data(push, nvfb->r_pitch);
504 evo_data(push, nvfb->r_format);
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000505 evo_data(push, nvfb->r_dma);
Ben Skeggsc6f2f712011-07-08 12:11:58 +1000506 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
507 evo_data(push, (y << 16) | x);
Ben Skeggsa46232e2011-07-07 15:23:48 +1000508 if (update) {
509 evo_mthd(push, 0x0080, 1);
510 evo_data(push, 0x00000000);
511 }
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000512 evo_kick(push, fb->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000513 }
514
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000515 nv_crtc->fb.tile_flags = nvfb->r_dma;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000516 return 0;
517}
518
519static void
520nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc, bool show, bool update)
521{
522 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000523 u32 *push = evo_wait(dev, EVO_MASTER, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000524 if (push) {
525 if (show) {
526 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
527 evo_data(push, 0x85000000);
528 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
529 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000530 evo_data(push, NvEvoVRAM);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000531 } else {
532 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
533 evo_data(push, 0x05000000);
534 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
535 evo_data(push, 0x00000000);
536 }
537
538 if (update) {
539 evo_mthd(push, 0x0080, 1);
540 evo_data(push, 0x00000000);
541 }
542
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000543 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000544 }
545}
546
547static void
548nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
549{
550}
551
552static void
553nvd0_crtc_prepare(struct drm_crtc *crtc)
554{
555 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
556 u32 *push;
557
Ben Skeggs3376ee32011-11-12 14:28:12 +1000558 nvd0_display_flip_stop(crtc);
559
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000560 push = evo_wait(crtc->dev, EVO_MASTER, 2);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000561 if (push) {
562 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
563 evo_data(push, 0x00000000);
564 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
565 evo_data(push, 0x03000000);
566 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
567 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000568 evo_kick(push, crtc->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000569 }
570
571 nvd0_crtc_cursor_show(nv_crtc, false, false);
572}
573
574static void
575nvd0_crtc_commit(struct drm_crtc *crtc)
576{
577 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
578 u32 *push;
579
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000580 push = evo_wait(crtc->dev, EVO_MASTER, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000581 if (push) {
582 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
583 evo_data(push, nv_crtc->fb.tile_flags);
584 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
585 evo_data(push, 0x83000000);
586 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
587 evo_data(push, 0x00000000);
588 evo_data(push, 0x00000000);
589 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000590 evo_data(push, NvEvoVRAM);
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000591 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
592 evo_data(push, 0xffffff00);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000593 evo_kick(push, crtc->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000594 }
595
Ben Skeggs3376ee32011-11-12 14:28:12 +1000596 nvd0_crtc_cursor_show(nv_crtc, nv_crtc->cursor.visible, false);
597 nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000598}
599
600static bool
601nvd0_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
602 struct drm_display_mode *adjusted_mode)
603{
604 return true;
605}
606
607static int
608nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
609{
610 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
611 int ret;
612
613 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
614 if (ret)
615 return ret;
616
617 if (old_fb) {
618 nvfb = nouveau_framebuffer(old_fb);
619 nouveau_bo_unpin(nvfb->nvbo);
620 }
621
622 return 0;
623}
624
625static int
626nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
627 struct drm_display_mode *mode, int x, int y,
628 struct drm_framebuffer *old_fb)
629{
630 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
631 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000632 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
633 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
634 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
635 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
636 u32 vblan2e = 0, vblan2s = 1;
Ben Skeggs3488c572012-03-12 11:42:20 +1000637 u32 *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000638 int ret;
639
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000640 hactive = mode->htotal;
641 hsynce = mode->hsync_end - mode->hsync_start - 1;
642 hbackp = mode->htotal - mode->hsync_end;
643 hblanke = hsynce + hbackp;
644 hfrontp = mode->hsync_start - mode->hdisplay;
645 hblanks = mode->htotal - hfrontp - 1;
646
647 vactive = mode->vtotal * vscan / ilace;
648 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
649 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
650 vblanke = vsynce + vbackp;
651 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
652 vblanks = vactive - vfrontp - 1;
653 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
654 vblan2e = vactive + vsynce + vbackp;
655 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
656 vactive = (vactive * 2) + 1;
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000657 }
658
Ben Skeggs438d99e2011-07-05 16:48:06 +1000659 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
660 if (ret)
661 return ret;
662
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000663 push = evo_wait(crtc->dev, EVO_MASTER, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000664 if (push) {
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000665 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
Ben Skeggs629c1b92011-07-08 09:43:20 +1000666 evo_data(push, 0x00000000);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000667 evo_data(push, (vactive << 16) | hactive);
668 evo_data(push, ( vsynce << 16) | hsynce);
669 evo_data(push, (vblanke << 16) | hblanke);
670 evo_data(push, (vblanks << 16) | hblanks);
671 evo_data(push, (vblan2e << 16) | vblan2s);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000672 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
673 evo_data(push, 0x00000000); /* ??? */
674 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
675 evo_data(push, mode->clock * 1000);
676 evo_data(push, 0x00200000); /* ??? */
677 evo_data(push, mode->clock * 1000);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000678 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
679 evo_data(push, 0x00000311);
680 evo_data(push, 0x00000100);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000681 evo_kick(push, crtc->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000682 }
683
684 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs488ff202011-10-17 10:38:10 +1000685 nvd0_crtc_set_dither(nv_crtc, false);
686 nvd0_crtc_set_scale(nv_crtc, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000687 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
688 return 0;
689}
690
691static int
692nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
693 struct drm_framebuffer *old_fb)
694{
695 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
696 int ret;
697
Ben Skeggs84e2ad82011-08-26 09:40:39 +1000698 if (!crtc->fb) {
699 NV_DEBUG_KMS(crtc->dev, "No FB bound\n");
700 return 0;
701 }
702
Ben Skeggs438d99e2011-07-05 16:48:06 +1000703 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
704 if (ret)
705 return ret;
706
Ben Skeggs3376ee32011-11-12 14:28:12 +1000707 nvd0_display_flip_stop(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000708 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000709 nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000710 return 0;
711}
712
713static int
714nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
715 struct drm_framebuffer *fb, int x, int y,
716 enum mode_set_atomic state)
717{
718 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000719 nvd0_display_flip_stop(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000720 nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
721 return 0;
722}
723
724static void
725nvd0_crtc_lut_load(struct drm_crtc *crtc)
726{
727 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
728 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
729 int i;
730
731 for (i = 0; i < 256; i++) {
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000732 writew(0x6000 + (nv_crtc->lut.r[i] >> 2), lut + (i * 0x20) + 0);
733 writew(0x6000 + (nv_crtc->lut.g[i] >> 2), lut + (i * 0x20) + 2);
734 writew(0x6000 + (nv_crtc->lut.b[i] >> 2), lut + (i * 0x20) + 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000735 }
736}
737
738static int
739nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
740 uint32_t handle, uint32_t width, uint32_t height)
741{
742 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
743 struct drm_device *dev = crtc->dev;
744 struct drm_gem_object *gem;
745 struct nouveau_bo *nvbo;
746 bool visible = (handle != 0);
747 int i, ret = 0;
748
749 if (visible) {
750 if (width != 64 || height != 64)
751 return -EINVAL;
752
753 gem = drm_gem_object_lookup(dev, file_priv, handle);
754 if (unlikely(!gem))
755 return -ENOENT;
756 nvbo = nouveau_gem_object(gem);
757
758 ret = nouveau_bo_map(nvbo);
759 if (ret == 0) {
760 for (i = 0; i < 64 * 64; i++) {
761 u32 v = nouveau_bo_rd32(nvbo, i);
762 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
763 }
764 nouveau_bo_unmap(nvbo);
765 }
766
767 drm_gem_object_unreference_unlocked(gem);
768 }
769
770 if (visible != nv_crtc->cursor.visible) {
771 nvd0_crtc_cursor_show(nv_crtc, visible, true);
772 nv_crtc->cursor.visible = visible;
773 }
774
775 return ret;
776}
777
778static int
779nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
780{
781 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs4acd4292011-11-12 12:57:54 +1000782 int ch = EVO_CURS(nv_crtc->index);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000783
Ben Skeggs4acd4292011-11-12 12:57:54 +1000784 evo_piow(crtc->dev, ch, 0x0084, (y << 16) | x);
785 evo_piow(crtc->dev, ch, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000786 return 0;
787}
788
789static void
790nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
791 uint32_t start, uint32_t size)
792{
793 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
794 u32 end = max(start + size, (u32)256);
795 u32 i;
796
797 for (i = start; i < end; i++) {
798 nv_crtc->lut.r[i] = r[i];
799 nv_crtc->lut.g[i] = g[i];
800 nv_crtc->lut.b[i] = b[i];
801 }
802
803 nvd0_crtc_lut_load(crtc);
804}
805
806static void
807nvd0_crtc_destroy(struct drm_crtc *crtc)
808{
809 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
810 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
811 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
812 nouveau_bo_unmap(nv_crtc->lut.nvbo);
813 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
814 drm_crtc_cleanup(crtc);
815 kfree(crtc);
816}
817
818static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
819 .dpms = nvd0_crtc_dpms,
820 .prepare = nvd0_crtc_prepare,
821 .commit = nvd0_crtc_commit,
822 .mode_fixup = nvd0_crtc_mode_fixup,
823 .mode_set = nvd0_crtc_mode_set,
824 .mode_set_base = nvd0_crtc_mode_set_base,
825 .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
826 .load_lut = nvd0_crtc_lut_load,
827};
828
829static const struct drm_crtc_funcs nvd0_crtc_func = {
830 .cursor_set = nvd0_crtc_cursor_set,
831 .cursor_move = nvd0_crtc_cursor_move,
832 .gamma_set = nvd0_crtc_gamma_set,
833 .set_config = drm_crtc_helper_set_config,
834 .destroy = nvd0_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000835 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000836};
837
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000838static void
839nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
840{
841}
842
843static void
844nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
845{
846}
847
Ben Skeggs438d99e2011-07-05 16:48:06 +1000848static int
849nvd0_crtc_create(struct drm_device *dev, int index)
850{
851 struct nouveau_crtc *nv_crtc;
852 struct drm_crtc *crtc;
853 int ret, i;
854
855 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
856 if (!nv_crtc)
857 return -ENOMEM;
858
859 nv_crtc->index = index;
860 nv_crtc->set_dither = nvd0_crtc_set_dither;
861 nv_crtc->set_scale = nvd0_crtc_set_scale;
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000862 nv_crtc->cursor.set_offset = nvd0_cursor_set_offset;
863 nv_crtc->cursor.set_pos = nvd0_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000864 for (i = 0; i < 256; i++) {
865 nv_crtc->lut.r[i] = i << 8;
866 nv_crtc->lut.g[i] = i << 8;
867 nv_crtc->lut.b[i] = i << 8;
868 }
869
870 crtc = &nv_crtc->base;
871 drm_crtc_init(dev, crtc, &nvd0_crtc_func);
872 drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
873 drm_mode_crtc_set_gamma_size(crtc, 256);
874
875 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
876 0, 0x0000, &nv_crtc->cursor.nvbo);
877 if (!ret) {
878 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
879 if (!ret)
880 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
881 if (ret)
882 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
883 }
884
885 if (ret)
886 goto out;
887
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000888 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000889 0, 0x0000, &nv_crtc->lut.nvbo);
890 if (!ret) {
891 ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
892 if (!ret)
893 ret = nouveau_bo_map(nv_crtc->lut.nvbo);
894 if (ret)
895 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
896 }
897
898 if (ret)
899 goto out;
900
901 nvd0_crtc_lut_load(crtc);
902
903out:
904 if (ret)
905 nvd0_crtc_destroy(crtc);
906 return ret;
907}
908
909/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000910 * DAC
911 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000912static void
913nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
914{
915 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
916 struct drm_device *dev = encoder->dev;
917 int or = nv_encoder->or;
918 u32 dpms_ctrl;
919
920 dpms_ctrl = 0x80000000;
921 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
922 dpms_ctrl |= 0x00000001;
923 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
924 dpms_ctrl |= 0x00000004;
925
926 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
927 nv_mask(dev, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
928 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
929}
930
931static bool
932nvd0_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
933 struct drm_display_mode *adjusted_mode)
934{
935 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
936 struct nouveau_connector *nv_connector;
937
938 nv_connector = nouveau_encoder_connector_get(nv_encoder);
939 if (nv_connector && nv_connector->native_mode) {
940 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
941 int id = adjusted_mode->base.id;
942 *adjusted_mode = *nv_connector->native_mode;
943 adjusted_mode->base.id = id;
944 }
945 }
946
947 return true;
948}
949
950static void
951nvd0_dac_prepare(struct drm_encoder *encoder)
952{
953}
954
955static void
956nvd0_dac_commit(struct drm_encoder *encoder)
957{
958}
959
960static void
961nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
962 struct drm_display_mode *adjusted_mode)
963{
964 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
965 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3488c572012-03-12 11:42:20 +1000966 u32 syncs, magic, *push;
967
968 syncs = 0x00000001;
969 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
970 syncs |= 0x00000008;
971 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
972 syncs |= 0x00000010;
973
974 magic = 0x31ec6000 | (nv_crtc->index << 25);
975 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
976 magic |= 0x00000001;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000977
978 nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
979
Ben Skeggs3488c572012-03-12 11:42:20 +1000980 push = evo_wait(encoder->dev, EVO_MASTER, 8);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000981 if (push) {
Ben Skeggs3488c572012-03-12 11:42:20 +1000982 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
983 evo_data(push, syncs);
984 evo_data(push, magic);
985 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 2);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000986 evo_data(push, 1 << nv_crtc->index);
Ben Skeggsff8ff502011-07-08 11:53:37 +1000987 evo_data(push, 0x00ff);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000988 evo_kick(push, encoder->dev, EVO_MASTER);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000989 }
990
991 nv_encoder->crtc = encoder->crtc;
992}
993
994static void
995nvd0_dac_disconnect(struct drm_encoder *encoder)
996{
997 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
998 struct drm_device *dev = encoder->dev;
999 u32 *push;
1000
1001 if (nv_encoder->crtc) {
1002 nvd0_crtc_prepare(nv_encoder->crtc);
1003
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001004 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001005 if (push) {
1006 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
1007 evo_data(push, 0x00000000);
1008 evo_mthd(push, 0x0080, 1);
1009 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001010 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001011 }
1012
1013 nv_encoder->crtc = NULL;
1014 }
1015}
1016
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001017static enum drm_connector_status
1018nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
1019{
Ben Skeggsb6819932011-07-08 11:14:50 +10001020 enum drm_connector_status status = connector_status_disconnected;
1021 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1022 struct drm_device *dev = encoder->dev;
1023 int or = nv_encoder->or;
1024 u32 load;
1025
1026 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00100000);
1027 udelay(9500);
1028 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x80000000);
1029
1030 load = nv_rd32(dev, 0x61a00c + (or * 0x800));
1031 if ((load & 0x38000000) == 0x38000000)
1032 status = connector_status_connected;
1033
1034 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00000000);
1035 return status;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001036}
1037
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001038static void
1039nvd0_dac_destroy(struct drm_encoder *encoder)
1040{
1041 drm_encoder_cleanup(encoder);
1042 kfree(encoder);
1043}
1044
1045static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
1046 .dpms = nvd0_dac_dpms,
1047 .mode_fixup = nvd0_dac_mode_fixup,
1048 .prepare = nvd0_dac_prepare,
1049 .commit = nvd0_dac_commit,
1050 .mode_set = nvd0_dac_mode_set,
1051 .disable = nvd0_dac_disconnect,
1052 .get_crtc = nvd0_display_crtc_get,
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001053 .detect = nvd0_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001054};
1055
1056static const struct drm_encoder_funcs nvd0_dac_func = {
1057 .destroy = nvd0_dac_destroy,
1058};
1059
1060static int
1061nvd0_dac_create(struct drm_connector *connector, struct dcb_entry *dcbe)
1062{
1063 struct drm_device *dev = connector->dev;
1064 struct nouveau_encoder *nv_encoder;
1065 struct drm_encoder *encoder;
1066
1067 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1068 if (!nv_encoder)
1069 return -ENOMEM;
1070 nv_encoder->dcb = dcbe;
1071 nv_encoder->or = ffs(dcbe->or) - 1;
1072
1073 encoder = to_drm_encoder(nv_encoder);
1074 encoder->possible_crtcs = dcbe->heads;
1075 encoder->possible_clones = 0;
1076 drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
1077 drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
1078
1079 drm_mode_connector_attach_encoder(connector, encoder);
1080 return 0;
1081}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001082
1083/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001084 * Audio
1085 *****************************************************************************/
1086static void
1087nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1088{
1089 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1090 struct nouveau_connector *nv_connector;
1091 struct drm_device *dev = encoder->dev;
1092 int i, or = nv_encoder->or * 0x30;
1093
1094 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1095 if (!drm_detect_monitor_audio(nv_connector->edid))
1096 return;
1097
1098 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000001);
1099
1100 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
1101 if (nv_connector->base.eld[0]) {
1102 u8 *eld = nv_connector->base.eld;
1103
1104 for (i = 0; i < eld[2] * 4; i++)
1105 nv_wr32(dev, 0x10ec00 + or, (i << 8) | eld[i]);
1106 for (i = eld[2] * 4; i < 0x60; i++)
1107 nv_wr32(dev, 0x10ec00 + or, (i << 8) | 0x00);
1108
1109 nv_mask(dev, 0x10ec10 + or, 0x80000002, 0x80000002);
1110 }
1111}
1112
1113static void
1114nvd0_audio_disconnect(struct drm_encoder *encoder)
1115{
1116 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1117 struct drm_device *dev = encoder->dev;
1118 int or = nv_encoder->or * 0x30;
1119
1120 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000000);
1121}
1122
1123/******************************************************************************
1124 * HDMI
1125 *****************************************************************************/
1126static void
1127nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1128{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001129 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1130 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1131 struct nouveau_connector *nv_connector;
1132 struct drm_device *dev = encoder->dev;
1133 int head = nv_crtc->index * 0x800;
1134 u32 rekey = 56; /* binary driver, and tegra constant */
1135 u32 max_ac_packet;
1136
1137 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1138 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1139 return;
1140
1141 max_ac_packet = mode->htotal - mode->hdisplay;
1142 max_ac_packet -= rekey;
1143 max_ac_packet -= 18; /* constant from tegra */
1144 max_ac_packet /= 32;
1145
1146 /* AVI InfoFrame */
1147 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
1148 nv_wr32(dev, 0x61671c + head, 0x000d0282);
1149 nv_wr32(dev, 0x616720 + head, 0x0000006f);
1150 nv_wr32(dev, 0x616724 + head, 0x00000000);
1151 nv_wr32(dev, 0x616728 + head, 0x00000000);
1152 nv_wr32(dev, 0x61672c + head, 0x00000000);
1153 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000001);
1154
1155 /* ??? InfoFrame? */
1156 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
1157 nv_wr32(dev, 0x6167ac + head, 0x00000010);
1158 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000001);
1159
1160 /* HDMI_CTRL */
1161 nv_mask(dev, 0x616798 + head, 0x401f007f, 0x40000000 | rekey |
1162 max_ac_packet << 16);
1163
Ben Skeggs091e40c2011-11-11 20:46:00 +10001164 /* NFI, audio doesn't work without it though.. */
1165 nv_mask(dev, 0x616548 + head, 0x00000070, 0x00000000);
1166
Ben Skeggs78951d22011-11-11 18:13:13 +10001167 nvd0_audio_mode_set(encoder, mode);
1168}
1169
1170static void
1171nvd0_hdmi_disconnect(struct drm_encoder *encoder)
1172{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001173 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1174 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1175 struct drm_device *dev = encoder->dev;
1176 int head = nv_crtc->index * 0x800;
1177
Ben Skeggs78951d22011-11-11 18:13:13 +10001178 nvd0_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001179
1180 nv_mask(dev, 0x616798 + head, 0x40000000, 0x00000000);
1181 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
1182 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
Ben Skeggs78951d22011-11-11 18:13:13 +10001183}
1184
1185/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001186 * SOR
1187 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001188static inline u32
1189nvd0_sor_dp_lane_map(struct drm_device *dev, struct dcb_entry *dcb, u8 lane)
1190{
1191 static const u8 nvd0[] = { 16, 8, 0, 24 };
1192 return nvd0[lane];
1193}
1194
1195static void
1196nvd0_sor_dp_train_set(struct drm_device *dev, struct dcb_entry *dcb, u8 pattern)
1197{
1198 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1199 const u32 loff = (or * 0x800) + (link * 0x80);
1200 nv_mask(dev, 0x61c110 + loff, 0x0f0f0f0f, 0x01010101 * pattern);
1201}
1202
1203static void
1204nvd0_sor_dp_train_adj(struct drm_device *dev, struct dcb_entry *dcb,
1205 u8 lane, u8 swing, u8 preem)
1206{
1207 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1208 const u32 loff = (or * 0x800) + (link * 0x80);
1209 u32 shift = nvd0_sor_dp_lane_map(dev, dcb, lane);
1210 u32 mask = 0x000000ff << shift;
1211 u8 *table, *entry, *config = NULL;
1212
1213 switch (swing) {
1214 case 0: preem += 0; break;
1215 case 1: preem += 4; break;
1216 case 2: preem += 7; break;
1217 case 3: preem += 9; break;
1218 }
1219
1220 table = nouveau_dp_bios_data(dev, dcb, &entry);
1221 if (table) {
1222 if (table[0] == 0x30) {
1223 config = entry + table[4];
1224 config += table[5] * preem;
1225 }
1226 }
1227
1228 if (!config) {
1229 NV_ERROR(dev, "PDISP: unsupported DP table for chipset\n");
1230 return;
1231 }
1232
1233 nv_mask(dev, 0x61c118 + loff, mask, config[1] << shift);
1234 nv_mask(dev, 0x61c120 + loff, mask, config[2] << shift);
1235 nv_mask(dev, 0x61c130 + loff, 0x0000ff00, config[3] << 8);
1236 nv_mask(dev, 0x61c13c + loff, 0x00000000, 0x00000000);
1237}
1238
1239static void
1240nvd0_sor_dp_link_set(struct drm_device *dev, struct dcb_entry *dcb, int crtc,
1241 int link_nr, u32 link_bw, bool enhframe)
1242{
1243 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1244 const u32 loff = (or * 0x800) + (link * 0x80);
1245 const u32 soff = (or * 0x800);
1246 u32 dpctrl = nv_rd32(dev, 0x61c10c + loff) & ~0x001f4000;
1247 u32 clksor = nv_rd32(dev, 0x612300 + soff) & ~0x007c0000;
1248 u32 script = 0x0000, lane_mask = 0;
1249 u8 *table, *entry;
1250 int i;
1251
1252 link_bw /= 27000;
1253
1254 table = nouveau_dp_bios_data(dev, dcb, &entry);
1255 if (table) {
1256 if (table[0] == 0x30) entry = ROMPTR(dev, entry[10]);
1257 else entry = NULL;
1258
1259 while (entry) {
1260 if (entry[0] >= link_bw)
1261 break;
1262 entry += 3;
1263 }
1264
1265 nouveau_bios_run_init_table(dev, script, dcb, crtc);
1266 }
1267
1268 clksor |= link_bw << 18;
1269 dpctrl |= ((1 << link_nr) - 1) << 16;
1270 if (enhframe)
1271 dpctrl |= 0x00004000;
1272
1273 for (i = 0; i < link_nr; i++)
1274 lane_mask |= 1 << (nvd0_sor_dp_lane_map(dev, dcb, i) >> 3);
1275
1276 nv_wr32(dev, 0x612300 + soff, clksor);
1277 nv_wr32(dev, 0x61c10c + loff, dpctrl);
1278 nv_mask(dev, 0x61c130 + loff, 0x0000000f, lane_mask);
1279}
1280
1281static void
1282nvd0_sor_dp_link_get(struct drm_device *dev, struct dcb_entry *dcb,
1283 u32 *link_nr, u32 *link_bw)
1284{
1285 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1286 const u32 loff = (or * 0x800) + (link * 0x80);
1287 const u32 soff = (or * 0x800);
1288 u32 dpctrl = nv_rd32(dev, 0x61c10c + loff) & 0x000f0000;
1289 u32 clksor = nv_rd32(dev, 0x612300 + soff);
1290
1291 if (dpctrl > 0x00030000) *link_nr = 4;
1292 else if (dpctrl > 0x00010000) *link_nr = 2;
1293 else *link_nr = 1;
1294
1295 *link_bw = (clksor & 0x007c0000) >> 18;
1296 *link_bw *= 27000;
1297}
1298
1299static void
1300nvd0_sor_dp_calc_tu(struct drm_device *dev, struct dcb_entry *dcb,
1301 u32 crtc, u32 datarate)
1302{
1303 const u32 symbol = 100000;
1304 const u32 TU = 64;
1305 u32 link_nr, link_bw;
1306 u64 ratio, value;
1307
1308 nvd0_sor_dp_link_get(dev, dcb, &link_nr, &link_bw);
1309
1310 ratio = datarate;
1311 ratio *= symbol;
1312 do_div(ratio, link_nr * link_bw);
1313
1314 value = (symbol - ratio) * TU;
1315 value *= ratio;
1316 do_div(value, symbol);
1317 do_div(value, symbol);
1318
1319 value += 5;
1320 value |= 0x08000000;
1321
1322 nv_wr32(dev, 0x616610 + (crtc * 0x800), value);
1323}
1324
Ben Skeggs83fc0832011-07-05 13:08:40 +10001325static void
1326nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
1327{
1328 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1329 struct drm_device *dev = encoder->dev;
1330 struct drm_encoder *partner;
1331 int or = nv_encoder->or;
1332 u32 dpms_ctrl;
1333
1334 nv_encoder->last_dpms = mode;
1335
1336 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1337 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1338
1339 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1340 continue;
1341
1342 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001343 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001344 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1345 return;
1346 break;
1347 }
1348 }
1349
1350 dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
1351 dpms_ctrl |= 0x80000000;
1352
1353 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
1354 nv_mask(dev, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
1355 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
1356 nv_wait(dev, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001357
1358 if (nv_encoder->dcb->type == OUTPUT_DP) {
1359 struct dp_train_func func = {
1360 .link_set = nvd0_sor_dp_link_set,
1361 .train_set = nvd0_sor_dp_train_set,
1362 .train_adj = nvd0_sor_dp_train_adj
1363 };
1364
1365 nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
1366 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001367}
1368
1369static bool
1370nvd0_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
1371 struct drm_display_mode *adjusted_mode)
1372{
1373 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1374 struct nouveau_connector *nv_connector;
1375
1376 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1377 if (nv_connector && nv_connector->native_mode) {
1378 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1379 int id = adjusted_mode->base.id;
1380 *adjusted_mode = *nv_connector->native_mode;
1381 adjusted_mode->base.id = id;
1382 }
1383 }
1384
1385 return true;
1386}
1387
1388static void
1389nvd0_sor_prepare(struct drm_encoder *encoder)
1390{
1391}
1392
1393static void
1394nvd0_sor_commit(struct drm_encoder *encoder)
1395{
1396}
1397
1398static void
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001399nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
1400 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001401{
Ben Skeggs78951d22011-11-11 18:13:13 +10001402 struct drm_device *dev = encoder->dev;
1403 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001404 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1405 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001406 struct nouveau_connector *nv_connector;
1407 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001408 u32 mode_ctrl = (1 << nv_crtc->index);
Ben Skeggs3488c572012-03-12 11:42:20 +10001409 u32 syncs, magic, *push;
1410 u32 or_config;
1411
1412 syncs = 0x00000001;
1413 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1414 syncs |= 0x00000008;
1415 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1416 syncs |= 0x00000010;
1417
1418 magic = 0x31ec6000 | (nv_crtc->index << 25);
1419 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1420 magic |= 0x00000001;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001421
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001422 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1423 switch (nv_encoder->dcb->type) {
1424 case OUTPUT_TMDS:
1425 if (nv_encoder->dcb->sorconf.link & 1) {
1426 if (mode->clock < 165000)
1427 mode_ctrl |= 0x00000100;
1428 else
1429 mode_ctrl |= 0x00000500;
1430 } else {
1431 mode_ctrl |= 0x00000200;
1432 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001433
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001434 or_config = (mode_ctrl & 0x00000f00) >> 8;
1435 if (mode->clock >= 165000)
1436 or_config |= 0x0100;
Ben Skeggs78951d22011-11-11 18:13:13 +10001437
1438 nvd0_hdmi_mode_set(encoder, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001439 break;
1440 case OUTPUT_LVDS:
1441 or_config = (mode_ctrl & 0x00000f00) >> 8;
1442 if (bios->fp_no_ddc) {
1443 if (bios->fp.dual_link)
1444 or_config |= 0x0100;
1445 if (bios->fp.if_is_24bit)
1446 or_config |= 0x0200;
1447 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001448 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001449 if (((u8 *)nv_connector->edid)[121] == 2)
1450 or_config |= 0x0100;
1451 } else
1452 if (mode->clock >= bios->fp.duallink_transition_clk) {
1453 or_config |= 0x0100;
1454 }
1455
1456 if (or_config & 0x0100) {
1457 if (bios->fp.strapless_is_24bit & 2)
1458 or_config |= 0x0200;
1459 } else {
1460 if (bios->fp.strapless_is_24bit & 1)
1461 or_config |= 0x0200;
1462 }
1463
1464 if (nv_connector->base.display_info.bpc == 8)
1465 or_config |= 0x0200;
1466
1467 }
1468 break;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001469 case OUTPUT_DP:
Ben Skeggs3488c572012-03-12 11:42:20 +10001470 if (nv_connector->base.display_info.bpc == 6) {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001471 nv_encoder->dp.datarate = mode->clock * 18 / 8;
Ben Skeggs3488c572012-03-12 11:42:20 +10001472 syncs |= 0x00000140;
1473 } else {
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001474 nv_encoder->dp.datarate = mode->clock * 24 / 8;
Ben Skeggs3488c572012-03-12 11:42:20 +10001475 syncs |= 0x00000180;
1476 }
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001477
1478 if (nv_encoder->dcb->sorconf.link & 1)
1479 mode_ctrl |= 0x00000800;
1480 else
1481 mode_ctrl |= 0x00000900;
1482
1483 or_config = (mode_ctrl & 0x00000f00) >> 8;
1484 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001485 default:
1486 BUG_ON(1);
1487 break;
1488 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001489
Ben Skeggs83fc0832011-07-05 13:08:40 +10001490 nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
1491
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001492 if (nv_encoder->dcb->type == OUTPUT_DP) {
1493 nvd0_sor_dp_calc_tu(dev, nv_encoder->dcb, nv_crtc->index,
1494 nv_encoder->dp.datarate);
1495 }
1496
Ben Skeggs3488c572012-03-12 11:42:20 +10001497 push = evo_wait(dev, EVO_MASTER, 8);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001498 if (push) {
Ben Skeggs3488c572012-03-12 11:42:20 +10001499 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
1500 evo_data(push, syncs);
1501 evo_data(push, magic);
1502 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 2);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001503 evo_data(push, mode_ctrl);
Ben Skeggsff8ff502011-07-08 11:53:37 +10001504 evo_data(push, or_config);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001505 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001506 }
1507
1508 nv_encoder->crtc = encoder->crtc;
1509}
1510
1511static void
1512nvd0_sor_disconnect(struct drm_encoder *encoder)
1513{
1514 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1515 struct drm_device *dev = encoder->dev;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001516 u32 *push;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001517
1518 if (nv_encoder->crtc) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001519 nvd0_crtc_prepare(nv_encoder->crtc);
1520
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001521 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001522 if (push) {
1523 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1524 evo_data(push, 0x00000000);
1525 evo_mthd(push, 0x0080, 1);
1526 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001527 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001528 }
1529
Ben Skeggs78951d22011-11-11 18:13:13 +10001530 nvd0_hdmi_disconnect(encoder);
1531
Ben Skeggs83fc0832011-07-05 13:08:40 +10001532 nv_encoder->crtc = NULL;
1533 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1534 }
1535}
1536
1537static void
1538nvd0_sor_destroy(struct drm_encoder *encoder)
1539{
1540 drm_encoder_cleanup(encoder);
1541 kfree(encoder);
1542}
1543
1544static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
1545 .dpms = nvd0_sor_dpms,
1546 .mode_fixup = nvd0_sor_mode_fixup,
1547 .prepare = nvd0_sor_prepare,
1548 .commit = nvd0_sor_commit,
1549 .mode_set = nvd0_sor_mode_set,
1550 .disable = nvd0_sor_disconnect,
1551 .get_crtc = nvd0_display_crtc_get,
1552};
1553
1554static const struct drm_encoder_funcs nvd0_sor_func = {
1555 .destroy = nvd0_sor_destroy,
1556};
1557
1558static int
1559nvd0_sor_create(struct drm_connector *connector, struct dcb_entry *dcbe)
1560{
1561 struct drm_device *dev = connector->dev;
1562 struct nouveau_encoder *nv_encoder;
1563 struct drm_encoder *encoder;
1564
1565 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1566 if (!nv_encoder)
1567 return -ENOMEM;
1568 nv_encoder->dcb = dcbe;
1569 nv_encoder->or = ffs(dcbe->or) - 1;
1570 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1571
1572 encoder = to_drm_encoder(nv_encoder);
1573 encoder->possible_crtcs = dcbe->heads;
1574 encoder->possible_clones = 0;
1575 drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
1576 drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
1577
1578 drm_mode_connector_attach_encoder(connector, encoder);
1579 return 0;
1580}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001581
1582/******************************************************************************
1583 * IRQ
1584 *****************************************************************************/
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001585static struct dcb_entry *
1586lookup_dcb(struct drm_device *dev, int id, u32 mc)
1587{
1588 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsc6748442012-03-11 16:13:49 +10001589 int type, or, i, link = -1;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001590
1591 if (id < 4) {
1592 type = OUTPUT_ANALOG;
1593 or = id;
1594 } else {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001595 switch (mc & 0x00000f00) {
Ben Skeggsc6748442012-03-11 16:13:49 +10001596 case 0x00000000: link = 0; type = OUTPUT_LVDS; break;
1597 case 0x00000100: link = 0; type = OUTPUT_TMDS; break;
1598 case 0x00000200: link = 1; type = OUTPUT_TMDS; break;
1599 case 0x00000500: link = 0; type = OUTPUT_TMDS; break;
1600 case 0x00000800: link = 0; type = OUTPUT_DP; break;
1601 case 0x00000900: link = 1; type = OUTPUT_DP; break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001602 default:
Ben Skeggsee417792011-07-08 14:34:45 +10001603 NV_ERROR(dev, "PDISP: unknown SOR mc 0x%08x\n", mc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001604 return NULL;
1605 }
1606
1607 or = id - 4;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001608 }
1609
1610 for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
1611 struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
Ben Skeggsc6748442012-03-11 16:13:49 +10001612 if (dcb->type == type && (dcb->or & (1 << or)) &&
1613 (link < 0 || link == !(dcb->sorconf.link & 1)))
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001614 return dcb;
1615 }
1616
Ben Skeggsee417792011-07-08 14:34:45 +10001617 NV_ERROR(dev, "PDISP: DCB for %d/0x%08x not found\n", id, mc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001618 return NULL;
1619}
1620
Ben Skeggs46005222011-07-05 11:01:13 +10001621static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001622nvd0_display_unk1_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001623{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001624 struct dcb_entry *dcb;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001625 int i;
1626
Ben Skeggsee417792011-07-08 14:34:45 +10001627 for (i = 0; mask && i < 8; i++) {
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001628 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
Ben Skeggsee417792011-07-08 14:34:45 +10001629 if (!(mcc & (1 << crtc)))
1630 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001631
Ben Skeggsee417792011-07-08 14:34:45 +10001632 dcb = lookup_dcb(dev, i, mcc);
1633 if (!dcb)
1634 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001635
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001636 nouveau_bios_run_display_table(dev, 0x0000, -1, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001637 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001638
Ben Skeggs270a5742011-07-05 14:16:05 +10001639 nv_wr32(dev, 0x6101d4, 0x00000000);
1640 nv_wr32(dev, 0x6109d4, 0x00000000);
1641 nv_wr32(dev, 0x6101d0, 0x80000000);
1642}
1643
1644static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001645nvd0_display_unk2_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001646{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001647 struct dcb_entry *dcb;
Ben Skeggs37b034a2011-07-08 14:43:19 +10001648 u32 or, tmp, pclk;
Ben Skeggsee417792011-07-08 14:34:45 +10001649 int i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001650
Ben Skeggsee417792011-07-08 14:34:45 +10001651 for (i = 0; mask && i < 8; i++) {
1652 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
1653 if (!(mcc & (1 << crtc)))
1654 continue;
1655
1656 dcb = lookup_dcb(dev, i, mcc);
1657 if (!dcb)
1658 continue;
1659
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001660 nouveau_bios_run_display_table(dev, 0x0000, -2, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001661 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001662
Ben Skeggsee417792011-07-08 14:34:45 +10001663 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
1664 if (mask & 0x00010000) {
1665 nv50_crtc_set_clock(dev, crtc, pclk);
1666 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001667
Ben Skeggsee417792011-07-08 14:34:45 +10001668 for (i = 0; mask && i < 8; i++) {
1669 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1670 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1671 if (!(mcp & (1 << crtc)))
1672 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001673
Ben Skeggsee417792011-07-08 14:34:45 +10001674 dcb = lookup_dcb(dev, i, mcp);
1675 if (!dcb)
1676 continue;
1677 or = ffs(dcb->or) - 1;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001678
Ben Skeggsee417792011-07-08 14:34:45 +10001679 nouveau_bios_run_display_table(dev, cfg, pclk, dcb, crtc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001680
Ben Skeggsee417792011-07-08 14:34:45 +10001681 nv_wr32(dev, 0x612200 + (crtc * 0x800), 0x00000000);
1682 switch (dcb->type) {
1683 case OUTPUT_ANALOG:
1684 nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
1685 break;
1686 case OUTPUT_TMDS:
1687 case OUTPUT_LVDS:
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001688 case OUTPUT_DP:
Ben Skeggsee417792011-07-08 14:34:45 +10001689 if (cfg & 0x00000100)
1690 tmp = 0x00000101;
1691 else
1692 tmp = 0x00000000;
1693
1694 nv_mask(dev, 0x612300 + (or * 0x800), 0x00000707, tmp);
1695 break;
1696 default:
1697 break;
1698 }
1699
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001700 break;
1701 }
1702
Ben Skeggs270a5742011-07-05 14:16:05 +10001703 nv_wr32(dev, 0x6101d4, 0x00000000);
1704 nv_wr32(dev, 0x6109d4, 0x00000000);
1705 nv_wr32(dev, 0x6101d0, 0x80000000);
1706}
1707
1708static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001709nvd0_display_unk4_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001710{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001711 struct dcb_entry *dcb;
Ben Skeggsee417792011-07-08 14:34:45 +10001712 int pclk, i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001713
Ben Skeggsee417792011-07-08 14:34:45 +10001714 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001715
Ben Skeggsee417792011-07-08 14:34:45 +10001716 for (i = 0; mask && i < 8; i++) {
1717 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1718 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1719 if (!(mcp & (1 << crtc)))
1720 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001721
Ben Skeggsee417792011-07-08 14:34:45 +10001722 dcb = lookup_dcb(dev, i, mcp);
1723 if (!dcb)
1724 continue;
1725
1726 nouveau_bios_run_display_table(dev, cfg, -pclk, dcb, crtc);
1727 }
1728
Ben Skeggs270a5742011-07-05 14:16:05 +10001729 nv_wr32(dev, 0x6101d4, 0x00000000);
1730 nv_wr32(dev, 0x6109d4, 0x00000000);
1731 nv_wr32(dev, 0x6101d0, 0x80000000);
1732}
1733
1734static void
Ben Skeggsf20ce962011-07-08 13:17:01 +10001735nvd0_display_bh(unsigned long data)
1736{
1737 struct drm_device *dev = (struct drm_device *)data;
1738 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001739 u32 mask = 0, crtc = ~0;
Ben Skeggs37b034a2011-07-08 14:43:19 +10001740 int i;
1741
1742 if (drm_debug & (DRM_UT_DRIVER | DRM_UT_KMS)) {
1743 NV_INFO(dev, "PDISP: modeset req %d\n", disp->modeset);
1744 NV_INFO(dev, " STAT: 0x%08x 0x%08x 0x%08x\n",
1745 nv_rd32(dev, 0x6101d0),
1746 nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
1747 for (i = 0; i < 8; i++) {
1748 NV_INFO(dev, " %s%d: 0x%08x 0x%08x\n",
1749 i < 4 ? "DAC" : "SOR", i,
1750 nv_rd32(dev, 0x640180 + (i * 0x20)),
1751 nv_rd32(dev, 0x660180 + (i * 0x20)));
1752 }
1753 }
1754
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001755 while (!mask && ++crtc < dev->mode_config.num_crtc)
1756 mask = nv_rd32(dev, 0x6101d4 + (crtc * 0x800));
Ben Skeggsf20ce962011-07-08 13:17:01 +10001757
Ben Skeggsee417792011-07-08 14:34:45 +10001758 if (disp->modeset & 0x00000001)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001759 nvd0_display_unk1_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001760 if (disp->modeset & 0x00000002)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001761 nvd0_display_unk2_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001762 if (disp->modeset & 0x00000004)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001763 nvd0_display_unk4_handler(dev, crtc, mask);
Ben Skeggsf20ce962011-07-08 13:17:01 +10001764}
1765
1766static void
Ben Skeggs46005222011-07-05 11:01:13 +10001767nvd0_display_intr(struct drm_device *dev)
1768{
Ben Skeggsf20ce962011-07-08 13:17:01 +10001769 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs46005222011-07-05 11:01:13 +10001770 u32 intr = nv_rd32(dev, 0x610088);
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001771 int i;
Ben Skeggs46005222011-07-05 11:01:13 +10001772
Ben Skeggs84e052e2011-11-13 03:43:30 +10001773 if (intr & 0x00000001) {
1774 u32 stat = nv_rd32(dev, 0x61008c);
1775 nv_wr32(dev, 0x61008c, stat);
1776 intr &= ~0x00000001;
1777 }
1778
Ben Skeggs46005222011-07-05 11:01:13 +10001779 if (intr & 0x00000002) {
1780 u32 stat = nv_rd32(dev, 0x61009c);
1781 int chid = ffs(stat) - 1;
1782 if (chid >= 0) {
1783 u32 mthd = nv_rd32(dev, 0x6101f0 + (chid * 12));
1784 u32 data = nv_rd32(dev, 0x6101f4 + (chid * 12));
1785 u32 unkn = nv_rd32(dev, 0x6101f8 + (chid * 12));
1786
1787 NV_INFO(dev, "EvoCh: chid %d mthd 0x%04x data 0x%08x "
1788 "0x%08x 0x%08x\n",
1789 chid, (mthd & 0x0000ffc), data, mthd, unkn);
1790 nv_wr32(dev, 0x61009c, (1 << chid));
1791 nv_wr32(dev, 0x6101f0 + (chid * 12), 0x90000000);
1792 }
1793
1794 intr &= ~0x00000002;
1795 }
1796
Ben Skeggs270a5742011-07-05 14:16:05 +10001797 if (intr & 0x00100000) {
1798 u32 stat = nv_rd32(dev, 0x6100ac);
1799
1800 if (stat & 0x00000007) {
Ben Skeggsee417792011-07-08 14:34:45 +10001801 disp->modeset = stat;
Ben Skeggsf20ce962011-07-08 13:17:01 +10001802 tasklet_schedule(&disp->tasklet);
Ben Skeggs270a5742011-07-05 14:16:05 +10001803
Ben Skeggsf20ce962011-07-08 13:17:01 +10001804 nv_wr32(dev, 0x6100ac, (stat & 0x00000007));
Ben Skeggs270a5742011-07-05 14:16:05 +10001805 stat &= ~0x00000007;
1806 }
1807
1808 if (stat) {
1809 NV_INFO(dev, "PDISP: unknown intr24 0x%08x\n", stat);
1810 nv_wr32(dev, 0x6100ac, stat);
1811 }
1812
1813 intr &= ~0x00100000;
1814 }
1815
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001816 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1817 u32 mask = 0x01000000 << i;
1818 if (intr & mask) {
1819 u32 stat = nv_rd32(dev, 0x6100bc + (i * 0x800));
1820 nv_wr32(dev, 0x6100bc + (i * 0x800), stat);
1821 intr &= ~mask;
1822 }
Ben Skeggs46005222011-07-05 11:01:13 +10001823 }
1824
1825 if (intr)
1826 NV_INFO(dev, "PDISP: unknown intr 0x%08x\n", intr);
1827}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001828
1829/******************************************************************************
1830 * Init
1831 *****************************************************************************/
Ben Skeggs2a44e492011-11-09 11:36:33 +10001832void
Ben Skeggs26f6d882011-07-04 16:25:18 +10001833nvd0_display_fini(struct drm_device *dev)
1834{
1835 int i;
1836
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001837 /* fini cursors + overlays + flips */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001838 for (i = 1; i >= 0; i--) {
1839 evo_fini_pio(dev, EVO_CURS(i));
Ben Skeggs8a464382011-11-12 23:52:07 +10001840 evo_fini_pio(dev, EVO_OIMM(i));
1841 evo_fini_dma(dev, EVO_OVLY(i));
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001842 evo_fini_dma(dev, EVO_FLIP(i));
Ben Skeggs26f6d882011-07-04 16:25:18 +10001843 }
1844
1845 /* fini master */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001846 evo_fini_dma(dev, EVO_MASTER);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001847}
1848
1849int
1850nvd0_display_init(struct drm_device *dev)
1851{
1852 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001853 int ret, i;
Ben Skeggsefd272a2011-07-05 11:58:58 +10001854 u32 *push;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001855
1856 if (nv_rd32(dev, 0x6100ac) & 0x00000100) {
1857 nv_wr32(dev, 0x6100ac, 0x00000100);
1858 nv_mask(dev, 0x6194e8, 0x00000001, 0x00000000);
1859 if (!nv_wait(dev, 0x6194e8, 0x00000002, 0x00000000)) {
1860 NV_ERROR(dev, "PDISP: 0x6194e8 0x%08x\n",
1861 nv_rd32(dev, 0x6194e8));
1862 return -EBUSY;
1863 }
1864 }
1865
Ben Skeggsa36f04c2011-07-06 14:39:23 +10001866 /* nfi what these are exactly, i do know that SOR_MODE_CTRL won't
1867 * work at all unless you do the SOR part below.
1868 */
1869 for (i = 0; i < 3; i++) {
1870 u32 dac = nv_rd32(dev, 0x61a000 + (i * 0x800));
1871 nv_wr32(dev, 0x6101c0 + (i * 0x800), dac);
1872 }
1873
1874 for (i = 0; i < 4; i++) {
1875 u32 sor = nv_rd32(dev, 0x61c000 + (i * 0x800));
1876 nv_wr32(dev, 0x6301c4 + (i * 0x800), sor);
1877 }
1878
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001879 for (i = 0; i < dev->mode_config.num_crtc; i++) {
Ben Skeggsa36f04c2011-07-06 14:39:23 +10001880 u32 crtc0 = nv_rd32(dev, 0x616104 + (i * 0x800));
1881 u32 crtc1 = nv_rd32(dev, 0x616108 + (i * 0x800));
1882 u32 crtc2 = nv_rd32(dev, 0x61610c + (i * 0x800));
1883 nv_wr32(dev, 0x6101b4 + (i * 0x800), crtc0);
1884 nv_wr32(dev, 0x6101b8 + (i * 0x800), crtc1);
1885 nv_wr32(dev, 0x6101bc + (i * 0x800), crtc2);
1886 }
1887
1888 /* point at our hash table / objects, enable interrupts */
Ben Skeggs26f6d882011-07-04 16:25:18 +10001889 nv_wr32(dev, 0x610010, (disp->mem->vinst >> 8) | 9);
Ben Skeggs270a5742011-07-05 14:16:05 +10001890 nv_mask(dev, 0x6100b0, 0x00000307, 0x00000307);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001891
1892 /* init master */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001893 ret = evo_init_dma(dev, EVO_MASTER);
1894 if (ret)
1895 goto error;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001896
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001897 /* init flips + overlays + cursors */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001898 for (i = 0; i < dev->mode_config.num_crtc; i++) {
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001899 if ((ret = evo_init_dma(dev, EVO_FLIP(i))) ||
Ben Skeggs8a464382011-11-12 23:52:07 +10001900 (ret = evo_init_dma(dev, EVO_OVLY(i))) ||
1901 (ret = evo_init_pio(dev, EVO_OIMM(i))) ||
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001902 (ret = evo_init_pio(dev, EVO_CURS(i))))
1903 goto error;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001904 }
1905
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001906 push = evo_wait(dev, EVO_MASTER, 32);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001907 if (!push) {
1908 ret = -EBUSY;
1909 goto error;
1910 }
Ben Skeggsefd272a2011-07-05 11:58:58 +10001911 evo_mthd(push, 0x0088, 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001912 evo_data(push, NvEvoSync);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001913 evo_mthd(push, 0x0084, 1);
1914 evo_data(push, 0x00000000);
1915 evo_mthd(push, 0x0084, 1);
1916 evo_data(push, 0x80000000);
1917 evo_mthd(push, 0x008c, 1);
1918 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001919 evo_kick(push, dev, EVO_MASTER);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001920
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001921error:
1922 if (ret)
1923 nvd0_display_fini(dev);
1924 return ret;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001925}
1926
1927void
1928nvd0_display_destroy(struct drm_device *dev)
1929{
1930 struct drm_nouveau_private *dev_priv = dev->dev_private;
1931 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs51beb422011-07-05 10:33:08 +10001932 struct pci_dev *pdev = dev->pdev;
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001933 int i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001934
Ben Skeggs8a464382011-11-12 23:52:07 +10001935 for (i = 0; i < EVO_DMA_NR; i++) {
Ben Skeggs3376ee32011-11-12 14:28:12 +10001936 struct evo *evo = &disp->evo[i];
Ben Skeggs3376ee32011-11-12 14:28:12 +10001937 pci_free_consistent(pdev, PAGE_SIZE, evo->ptr, evo->handle);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001938 }
1939
Ben Skeggs26f6d882011-07-04 16:25:18 +10001940 nouveau_gpuobj_ref(NULL, &disp->mem);
Ben Skeggs816af2f2011-11-16 15:48:48 +10001941 nouveau_bo_unmap(disp->sync);
1942 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs46005222011-07-05 11:01:13 +10001943 nouveau_irq_unregister(dev, 26);
Ben Skeggs51beb422011-07-05 10:33:08 +10001944
1945 dev_priv->engine.display.priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001946 kfree(disp);
1947}
1948
1949int
1950nvd0_display_create(struct drm_device *dev)
1951{
1952 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsefd272a2011-07-05 11:58:58 +10001953 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001954 struct dcb_table *dcb = &dev_priv->vbios.dcb;
1955 struct drm_connector *connector, *tmp;
Ben Skeggs51beb422011-07-05 10:33:08 +10001956 struct pci_dev *pdev = dev->pdev;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001957 struct nvd0_display *disp;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001958 struct dcb_entry *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001959 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001960
1961 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
1962 if (!disp)
1963 return -ENOMEM;
1964 dev_priv->engine.display.priv = disp;
1965
Ben Skeggs438d99e2011-07-05 16:48:06 +10001966 /* create crtc objects to represent the hw heads */
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001967 crtcs = nv_rd32(dev, 0x022448);
1968 for (i = 0; i < crtcs; i++) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001969 ret = nvd0_crtc_create(dev, i);
1970 if (ret)
1971 goto out;
1972 }
1973
Ben Skeggs83fc0832011-07-05 13:08:40 +10001974 /* create encoder/connector objects based on VBIOS DCB table */
1975 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
1976 connector = nouveau_connector_create(dev, dcbe->connector);
1977 if (IS_ERR(connector))
1978 continue;
1979
1980 if (dcbe->location != DCB_LOC_ON_CHIP) {
1981 NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
1982 dcbe->type, ffs(dcbe->or) - 1);
1983 continue;
1984 }
1985
1986 switch (dcbe->type) {
1987 case OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001988 case OUTPUT_LVDS:
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001989 case OUTPUT_DP:
Ben Skeggs83fc0832011-07-05 13:08:40 +10001990 nvd0_sor_create(connector, dcbe);
1991 break;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001992 case OUTPUT_ANALOG:
1993 nvd0_dac_create(connector, dcbe);
1994 break;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001995 default:
1996 NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
1997 dcbe->type, ffs(dcbe->or) - 1);
1998 continue;
1999 }
2000 }
2001
2002 /* cull any connectors we created that don't have an encoder */
2003 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2004 if (connector->encoder_ids[0])
2005 continue;
2006
2007 NV_WARN(dev, "%s has no encoders, removing\n",
2008 drm_get_connector_name(connector));
2009 connector->funcs->destroy(connector);
2010 }
2011
Ben Skeggs46005222011-07-05 11:01:13 +10002012 /* setup interrupt handling */
Ben Skeggsf20ce962011-07-08 13:17:01 +10002013 tasklet_init(&disp->tasklet, nvd0_display_bh, (unsigned long)dev);
Ben Skeggs46005222011-07-05 11:01:13 +10002014 nouveau_irq_register(dev, 26, nvd0_display_intr);
2015
Ben Skeggs816af2f2011-11-16 15:48:48 +10002016 /* small shared memory area we use for notifiers and semaphores */
2017 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
2018 0, 0x0000, &disp->sync);
2019 if (!ret) {
2020 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
2021 if (!ret)
2022 ret = nouveau_bo_map(disp->sync);
2023 if (ret)
2024 nouveau_bo_ref(NULL, &disp->sync);
2025 }
2026
2027 if (ret)
2028 goto out;
2029
Ben Skeggs51beb422011-07-05 10:33:08 +10002030 /* hash table and dma objects for the memory areas we care about */
Ben Skeggsefd272a2011-07-05 11:58:58 +10002031 ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
2032 NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002033 if (ret)
2034 goto out;
2035
Ben Skeggs3376ee32011-11-12 14:28:12 +10002036 /* create evo dma channels */
Ben Skeggs8a464382011-11-12 23:52:07 +10002037 for (i = 0; i < EVO_DMA_NR; i++) {
Ben Skeggs3376ee32011-11-12 14:28:12 +10002038 struct evo *evo = &disp->evo[i];
Ben Skeggs816af2f2011-11-16 15:48:48 +10002039 u64 offset = disp->sync->bo.offset;
Ben Skeggs3376ee32011-11-12 14:28:12 +10002040 u32 dmao = 0x1000 + (i * 0x100);
2041 u32 hash = 0x0000 + (i * 0x040);
Ben Skeggs3376ee32011-11-12 14:28:12 +10002042
2043 evo->idx = i;
Ben Skeggs816af2f2011-11-16 15:48:48 +10002044 evo->sem.offset = EVO_SYNC(evo->idx, 0x00);
Ben Skeggs3376ee32011-11-12 14:28:12 +10002045 evo->ptr = pci_alloc_consistent(pdev, PAGE_SIZE, &evo->handle);
2046 if (!evo->ptr) {
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002047 ret = -ENOMEM;
2048 goto out;
2049 }
Ben Skeggs3376ee32011-11-12 14:28:12 +10002050
Ben Skeggs3376ee32011-11-12 14:28:12 +10002051 nv_wo32(disp->mem, dmao + 0x00, 0x00000049);
2052 nv_wo32(disp->mem, dmao + 0x04, (offset + 0x0000) >> 8);
2053 nv_wo32(disp->mem, dmao + 0x08, (offset + 0x0fff) >> 8);
2054 nv_wo32(disp->mem, dmao + 0x0c, 0x00000000);
2055 nv_wo32(disp->mem, dmao + 0x10, 0x00000000);
2056 nv_wo32(disp->mem, dmao + 0x14, 0x00000000);
2057 nv_wo32(disp->mem, hash + 0x00, NvEvoSync);
2058 nv_wo32(disp->mem, hash + 0x04, 0x00000001 | (i << 27) |
2059 ((dmao + 0x00) << 9));
2060
2061 nv_wo32(disp->mem, dmao + 0x20, 0x00000049);
2062 nv_wo32(disp->mem, dmao + 0x24, 0x00000000);
2063 nv_wo32(disp->mem, dmao + 0x28, (dev_priv->vram_size - 1) >> 8);
2064 nv_wo32(disp->mem, dmao + 0x2c, 0x00000000);
2065 nv_wo32(disp->mem, dmao + 0x30, 0x00000000);
2066 nv_wo32(disp->mem, dmao + 0x34, 0x00000000);
2067 nv_wo32(disp->mem, hash + 0x08, NvEvoVRAM);
2068 nv_wo32(disp->mem, hash + 0x0c, 0x00000001 | (i << 27) |
2069 ((dmao + 0x20) << 9));
2070
2071 nv_wo32(disp->mem, dmao + 0x40, 0x00000009);
2072 nv_wo32(disp->mem, dmao + 0x44, 0x00000000);
2073 nv_wo32(disp->mem, dmao + 0x48, (dev_priv->vram_size - 1) >> 8);
2074 nv_wo32(disp->mem, dmao + 0x4c, 0x00000000);
2075 nv_wo32(disp->mem, dmao + 0x50, 0x00000000);
2076 nv_wo32(disp->mem, dmao + 0x54, 0x00000000);
2077 nv_wo32(disp->mem, hash + 0x10, NvEvoVRAM_LP);
2078 nv_wo32(disp->mem, hash + 0x14, 0x00000001 | (i << 27) |
2079 ((dmao + 0x40) << 9));
2080
2081 nv_wo32(disp->mem, dmao + 0x60, 0x0fe00009);
2082 nv_wo32(disp->mem, dmao + 0x64, 0x00000000);
2083 nv_wo32(disp->mem, dmao + 0x68, (dev_priv->vram_size - 1) >> 8);
2084 nv_wo32(disp->mem, dmao + 0x6c, 0x00000000);
2085 nv_wo32(disp->mem, dmao + 0x70, 0x00000000);
2086 nv_wo32(disp->mem, dmao + 0x74, 0x00000000);
2087 nv_wo32(disp->mem, hash + 0x18, NvEvoFB32);
2088 nv_wo32(disp->mem, hash + 0x1c, 0x00000001 | (i << 27) |
2089 ((dmao + 0x60) << 9));
Ben Skeggs51beb422011-07-05 10:33:08 +10002090 }
2091
Ben Skeggs3376ee32011-11-12 14:28:12 +10002092 pinstmem->flush(dev);
2093
Ben Skeggs26f6d882011-07-04 16:25:18 +10002094out:
2095 if (ret)
2096 nvd0_display_destroy(dev);
2097 return ret;
2098}