blob: 3c2543190bb8cf2194b7e4dda56450ec0602a191 [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;
637 u32 magic = 0x31ec6000;
Ben Skeggs629c1b92011-07-08 09:43:20 +1000638 u32 syncs, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000639 int ret;
640
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000641 hactive = mode->htotal;
642 hsynce = mode->hsync_end - mode->hsync_start - 1;
643 hbackp = mode->htotal - mode->hsync_end;
644 hblanke = hsynce + hbackp;
645 hfrontp = mode->hsync_start - mode->hdisplay;
646 hblanks = mode->htotal - hfrontp - 1;
647
648 vactive = mode->vtotal * vscan / ilace;
649 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
650 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
651 vblanke = vsynce + vbackp;
652 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
653 vblanks = vactive - vfrontp - 1;
654 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
655 vblan2e = vactive + vsynce + vbackp;
656 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
657 vactive = (vactive * 2) + 1;
658 magic |= 0x00000001;
659 }
660
Ben Skeggs629c1b92011-07-08 09:43:20 +1000661 syncs = 0x00000001;
662 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
663 syncs |= 0x00000008;
664 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
665 syncs |= 0x00000010;
666
Ben Skeggs438d99e2011-07-05 16:48:06 +1000667 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
668 if (ret)
669 return ret;
670
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000671 push = evo_wait(crtc->dev, EVO_MASTER, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000672 if (push) {
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000673 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
Ben Skeggs629c1b92011-07-08 09:43:20 +1000674 evo_data(push, 0x00000000);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000675 evo_data(push, (vactive << 16) | hactive);
676 evo_data(push, ( vsynce << 16) | hsynce);
677 evo_data(push, (vblanke << 16) | hblanke);
678 evo_data(push, (vblanks << 16) | hblanks);
679 evo_data(push, (vblan2e << 16) | vblan2s);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000680 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
681 evo_data(push, 0x00000000); /* ??? */
682 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
683 evo_data(push, mode->clock * 1000);
684 evo_data(push, 0x00200000); /* ??? */
685 evo_data(push, mode->clock * 1000);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000686 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
Ben Skeggs629c1b92011-07-08 09:43:20 +1000687 evo_data(push, syncs);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000688 evo_data(push, magic);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000689 evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
690 evo_data(push, 0x00000311);
691 evo_data(push, 0x00000100);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000692 evo_kick(push, crtc->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000693 }
694
695 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs488ff202011-10-17 10:38:10 +1000696 nvd0_crtc_set_dither(nv_crtc, false);
697 nvd0_crtc_set_scale(nv_crtc, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000698 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
699 return 0;
700}
701
702static int
703nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
704 struct drm_framebuffer *old_fb)
705{
706 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
707 int ret;
708
Ben Skeggs84e2ad82011-08-26 09:40:39 +1000709 if (!crtc->fb) {
710 NV_DEBUG_KMS(crtc->dev, "No FB bound\n");
711 return 0;
712 }
713
Ben Skeggs438d99e2011-07-05 16:48:06 +1000714 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
715 if (ret)
716 return ret;
717
Ben Skeggs3376ee32011-11-12 14:28:12 +1000718 nvd0_display_flip_stop(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000719 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000720 nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000721 return 0;
722}
723
724static int
725nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
726 struct drm_framebuffer *fb, int x, int y,
727 enum mode_set_atomic state)
728{
729 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs3376ee32011-11-12 14:28:12 +1000730 nvd0_display_flip_stop(crtc);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000731 nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
732 return 0;
733}
734
735static void
736nvd0_crtc_lut_load(struct drm_crtc *crtc)
737{
738 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
739 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
740 int i;
741
742 for (i = 0; i < 256; i++) {
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000743 writew(0x6000 + (nv_crtc->lut.r[i] >> 2), lut + (i * 0x20) + 0);
744 writew(0x6000 + (nv_crtc->lut.g[i] >> 2), lut + (i * 0x20) + 2);
745 writew(0x6000 + (nv_crtc->lut.b[i] >> 2), lut + (i * 0x20) + 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000746 }
747}
748
749static int
750nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
751 uint32_t handle, uint32_t width, uint32_t height)
752{
753 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
754 struct drm_device *dev = crtc->dev;
755 struct drm_gem_object *gem;
756 struct nouveau_bo *nvbo;
757 bool visible = (handle != 0);
758 int i, ret = 0;
759
760 if (visible) {
761 if (width != 64 || height != 64)
762 return -EINVAL;
763
764 gem = drm_gem_object_lookup(dev, file_priv, handle);
765 if (unlikely(!gem))
766 return -ENOENT;
767 nvbo = nouveau_gem_object(gem);
768
769 ret = nouveau_bo_map(nvbo);
770 if (ret == 0) {
771 for (i = 0; i < 64 * 64; i++) {
772 u32 v = nouveau_bo_rd32(nvbo, i);
773 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
774 }
775 nouveau_bo_unmap(nvbo);
776 }
777
778 drm_gem_object_unreference_unlocked(gem);
779 }
780
781 if (visible != nv_crtc->cursor.visible) {
782 nvd0_crtc_cursor_show(nv_crtc, visible, true);
783 nv_crtc->cursor.visible = visible;
784 }
785
786 return ret;
787}
788
789static int
790nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
791{
792 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs4acd4292011-11-12 12:57:54 +1000793 int ch = EVO_CURS(nv_crtc->index);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000794
Ben Skeggs4acd4292011-11-12 12:57:54 +1000795 evo_piow(crtc->dev, ch, 0x0084, (y << 16) | x);
796 evo_piow(crtc->dev, ch, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000797 return 0;
798}
799
800static void
801nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
802 uint32_t start, uint32_t size)
803{
804 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
805 u32 end = max(start + size, (u32)256);
806 u32 i;
807
808 for (i = start; i < end; i++) {
809 nv_crtc->lut.r[i] = r[i];
810 nv_crtc->lut.g[i] = g[i];
811 nv_crtc->lut.b[i] = b[i];
812 }
813
814 nvd0_crtc_lut_load(crtc);
815}
816
817static void
818nvd0_crtc_destroy(struct drm_crtc *crtc)
819{
820 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
821 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
822 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
823 nouveau_bo_unmap(nv_crtc->lut.nvbo);
824 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
825 drm_crtc_cleanup(crtc);
826 kfree(crtc);
827}
828
829static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
830 .dpms = nvd0_crtc_dpms,
831 .prepare = nvd0_crtc_prepare,
832 .commit = nvd0_crtc_commit,
833 .mode_fixup = nvd0_crtc_mode_fixup,
834 .mode_set = nvd0_crtc_mode_set,
835 .mode_set_base = nvd0_crtc_mode_set_base,
836 .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
837 .load_lut = nvd0_crtc_lut_load,
838};
839
840static const struct drm_crtc_funcs nvd0_crtc_func = {
841 .cursor_set = nvd0_crtc_cursor_set,
842 .cursor_move = nvd0_crtc_cursor_move,
843 .gamma_set = nvd0_crtc_gamma_set,
844 .set_config = drm_crtc_helper_set_config,
845 .destroy = nvd0_crtc_destroy,
Ben Skeggs3376ee32011-11-12 14:28:12 +1000846 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000847};
848
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000849static void
850nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
851{
852}
853
854static void
855nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
856{
857}
858
Ben Skeggs438d99e2011-07-05 16:48:06 +1000859static int
860nvd0_crtc_create(struct drm_device *dev, int index)
861{
862 struct nouveau_crtc *nv_crtc;
863 struct drm_crtc *crtc;
864 int ret, i;
865
866 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
867 if (!nv_crtc)
868 return -ENOMEM;
869
870 nv_crtc->index = index;
871 nv_crtc->set_dither = nvd0_crtc_set_dither;
872 nv_crtc->set_scale = nvd0_crtc_set_scale;
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000873 nv_crtc->cursor.set_offset = nvd0_cursor_set_offset;
874 nv_crtc->cursor.set_pos = nvd0_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000875 for (i = 0; i < 256; i++) {
876 nv_crtc->lut.r[i] = i << 8;
877 nv_crtc->lut.g[i] = i << 8;
878 nv_crtc->lut.b[i] = i << 8;
879 }
880
881 crtc = &nv_crtc->base;
882 drm_crtc_init(dev, crtc, &nvd0_crtc_func);
883 drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
884 drm_mode_crtc_set_gamma_size(crtc, 256);
885
886 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
887 0, 0x0000, &nv_crtc->cursor.nvbo);
888 if (!ret) {
889 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
890 if (!ret)
891 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
892 if (ret)
893 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
894 }
895
896 if (ret)
897 goto out;
898
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000899 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000900 0, 0x0000, &nv_crtc->lut.nvbo);
901 if (!ret) {
902 ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
903 if (!ret)
904 ret = nouveau_bo_map(nv_crtc->lut.nvbo);
905 if (ret)
906 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
907 }
908
909 if (ret)
910 goto out;
911
912 nvd0_crtc_lut_load(crtc);
913
914out:
915 if (ret)
916 nvd0_crtc_destroy(crtc);
917 return ret;
918}
919
920/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000921 * DAC
922 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000923static void
924nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
925{
926 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
927 struct drm_device *dev = encoder->dev;
928 int or = nv_encoder->or;
929 u32 dpms_ctrl;
930
931 dpms_ctrl = 0x80000000;
932 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
933 dpms_ctrl |= 0x00000001;
934 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
935 dpms_ctrl |= 0x00000004;
936
937 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
938 nv_mask(dev, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
939 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
940}
941
942static bool
943nvd0_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
944 struct drm_display_mode *adjusted_mode)
945{
946 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
947 struct nouveau_connector *nv_connector;
948
949 nv_connector = nouveau_encoder_connector_get(nv_encoder);
950 if (nv_connector && nv_connector->native_mode) {
951 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
952 int id = adjusted_mode->base.id;
953 *adjusted_mode = *nv_connector->native_mode;
954 adjusted_mode->base.id = id;
955 }
956 }
957
958 return true;
959}
960
961static void
962nvd0_dac_prepare(struct drm_encoder *encoder)
963{
964}
965
966static void
967nvd0_dac_commit(struct drm_encoder *encoder)
968{
969}
970
971static void
972nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
973 struct drm_display_mode *adjusted_mode)
974{
975 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
976 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
977 u32 *push;
978
979 nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
980
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000981 push = evo_wait(encoder->dev, EVO_MASTER, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000982 if (push) {
Ben Skeggsff8ff502011-07-08 11:53:37 +1000983 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 2);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000984 evo_data(push, 1 << nv_crtc->index);
Ben Skeggsff8ff502011-07-08 11:53:37 +1000985 evo_data(push, 0x00ff);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000986 evo_kick(push, encoder->dev, EVO_MASTER);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000987 }
988
989 nv_encoder->crtc = encoder->crtc;
990}
991
992static void
993nvd0_dac_disconnect(struct drm_encoder *encoder)
994{
995 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
996 struct drm_device *dev = encoder->dev;
997 u32 *push;
998
999 if (nv_encoder->crtc) {
1000 nvd0_crtc_prepare(nv_encoder->crtc);
1001
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001002 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001003 if (push) {
1004 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
1005 evo_data(push, 0x00000000);
1006 evo_mthd(push, 0x0080, 1);
1007 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001008 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001009 }
1010
1011 nv_encoder->crtc = NULL;
1012 }
1013}
1014
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001015static enum drm_connector_status
1016nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
1017{
Ben Skeggsb6819932011-07-08 11:14:50 +10001018 enum drm_connector_status status = connector_status_disconnected;
1019 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1020 struct drm_device *dev = encoder->dev;
1021 int or = nv_encoder->or;
1022 u32 load;
1023
1024 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00100000);
1025 udelay(9500);
1026 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x80000000);
1027
1028 load = nv_rd32(dev, 0x61a00c + (or * 0x800));
1029 if ((load & 0x38000000) == 0x38000000)
1030 status = connector_status_connected;
1031
1032 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00000000);
1033 return status;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001034}
1035
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001036static void
1037nvd0_dac_destroy(struct drm_encoder *encoder)
1038{
1039 drm_encoder_cleanup(encoder);
1040 kfree(encoder);
1041}
1042
1043static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
1044 .dpms = nvd0_dac_dpms,
1045 .mode_fixup = nvd0_dac_mode_fixup,
1046 .prepare = nvd0_dac_prepare,
1047 .commit = nvd0_dac_commit,
1048 .mode_set = nvd0_dac_mode_set,
1049 .disable = nvd0_dac_disconnect,
1050 .get_crtc = nvd0_display_crtc_get,
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +10001051 .detect = nvd0_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001052};
1053
1054static const struct drm_encoder_funcs nvd0_dac_func = {
1055 .destroy = nvd0_dac_destroy,
1056};
1057
1058static int
1059nvd0_dac_create(struct drm_connector *connector, struct dcb_entry *dcbe)
1060{
1061 struct drm_device *dev = connector->dev;
1062 struct nouveau_encoder *nv_encoder;
1063 struct drm_encoder *encoder;
1064
1065 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1066 if (!nv_encoder)
1067 return -ENOMEM;
1068 nv_encoder->dcb = dcbe;
1069 nv_encoder->or = ffs(dcbe->or) - 1;
1070
1071 encoder = to_drm_encoder(nv_encoder);
1072 encoder->possible_crtcs = dcbe->heads;
1073 encoder->possible_clones = 0;
1074 drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
1075 drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
1076
1077 drm_mode_connector_attach_encoder(connector, encoder);
1078 return 0;
1079}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001080
1081/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +10001082 * Audio
1083 *****************************************************************************/
1084static void
1085nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1086{
1087 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1088 struct nouveau_connector *nv_connector;
1089 struct drm_device *dev = encoder->dev;
1090 int i, or = nv_encoder->or * 0x30;
1091
1092 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1093 if (!drm_detect_monitor_audio(nv_connector->edid))
1094 return;
1095
1096 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000001);
1097
1098 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
1099 if (nv_connector->base.eld[0]) {
1100 u8 *eld = nv_connector->base.eld;
1101
1102 for (i = 0; i < eld[2] * 4; i++)
1103 nv_wr32(dev, 0x10ec00 + or, (i << 8) | eld[i]);
1104 for (i = eld[2] * 4; i < 0x60; i++)
1105 nv_wr32(dev, 0x10ec00 + or, (i << 8) | 0x00);
1106
1107 nv_mask(dev, 0x10ec10 + or, 0x80000002, 0x80000002);
1108 }
1109}
1110
1111static void
1112nvd0_audio_disconnect(struct drm_encoder *encoder)
1113{
1114 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1115 struct drm_device *dev = encoder->dev;
1116 int or = nv_encoder->or * 0x30;
1117
1118 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000000);
1119}
1120
1121/******************************************************************************
1122 * HDMI
1123 *****************************************************************************/
1124static void
1125nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
1126{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001127 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1128 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1129 struct nouveau_connector *nv_connector;
1130 struct drm_device *dev = encoder->dev;
1131 int head = nv_crtc->index * 0x800;
1132 u32 rekey = 56; /* binary driver, and tegra constant */
1133 u32 max_ac_packet;
1134
1135 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1136 if (!drm_detect_hdmi_monitor(nv_connector->edid))
1137 return;
1138
1139 max_ac_packet = mode->htotal - mode->hdisplay;
1140 max_ac_packet -= rekey;
1141 max_ac_packet -= 18; /* constant from tegra */
1142 max_ac_packet /= 32;
1143
1144 /* AVI InfoFrame */
1145 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
1146 nv_wr32(dev, 0x61671c + head, 0x000d0282);
1147 nv_wr32(dev, 0x616720 + head, 0x0000006f);
1148 nv_wr32(dev, 0x616724 + head, 0x00000000);
1149 nv_wr32(dev, 0x616728 + head, 0x00000000);
1150 nv_wr32(dev, 0x61672c + head, 0x00000000);
1151 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000001);
1152
1153 /* ??? InfoFrame? */
1154 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
1155 nv_wr32(dev, 0x6167ac + head, 0x00000010);
1156 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000001);
1157
1158 /* HDMI_CTRL */
1159 nv_mask(dev, 0x616798 + head, 0x401f007f, 0x40000000 | rekey |
1160 max_ac_packet << 16);
1161
Ben Skeggs091e40c2011-11-11 20:46:00 +10001162 /* NFI, audio doesn't work without it though.. */
1163 nv_mask(dev, 0x616548 + head, 0x00000070, 0x00000000);
1164
Ben Skeggs78951d22011-11-11 18:13:13 +10001165 nvd0_audio_mode_set(encoder, mode);
1166}
1167
1168static void
1169nvd0_hdmi_disconnect(struct drm_encoder *encoder)
1170{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001171 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1172 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1173 struct drm_device *dev = encoder->dev;
1174 int head = nv_crtc->index * 0x800;
1175
Ben Skeggs78951d22011-11-11 18:13:13 +10001176 nvd0_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001177
1178 nv_mask(dev, 0x616798 + head, 0x40000000, 0x00000000);
1179 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
1180 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
Ben Skeggs78951d22011-11-11 18:13:13 +10001181}
1182
1183/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001184 * SOR
1185 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001186static inline u32
1187nvd0_sor_dp_lane_map(struct drm_device *dev, struct dcb_entry *dcb, u8 lane)
1188{
1189 static const u8 nvd0[] = { 16, 8, 0, 24 };
1190 return nvd0[lane];
1191}
1192
1193static void
1194nvd0_sor_dp_train_set(struct drm_device *dev, struct dcb_entry *dcb, u8 pattern)
1195{
1196 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1197 const u32 loff = (or * 0x800) + (link * 0x80);
1198 nv_mask(dev, 0x61c110 + loff, 0x0f0f0f0f, 0x01010101 * pattern);
1199}
1200
1201static void
1202nvd0_sor_dp_train_adj(struct drm_device *dev, struct dcb_entry *dcb,
1203 u8 lane, u8 swing, u8 preem)
1204{
1205 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1206 const u32 loff = (or * 0x800) + (link * 0x80);
1207 u32 shift = nvd0_sor_dp_lane_map(dev, dcb, lane);
1208 u32 mask = 0x000000ff << shift;
1209 u8 *table, *entry, *config = NULL;
1210
1211 switch (swing) {
1212 case 0: preem += 0; break;
1213 case 1: preem += 4; break;
1214 case 2: preem += 7; break;
1215 case 3: preem += 9; break;
1216 }
1217
1218 table = nouveau_dp_bios_data(dev, dcb, &entry);
1219 if (table) {
1220 if (table[0] == 0x30) {
1221 config = entry + table[4];
1222 config += table[5] * preem;
1223 }
1224 }
1225
1226 if (!config) {
1227 NV_ERROR(dev, "PDISP: unsupported DP table for chipset\n");
1228 return;
1229 }
1230
1231 nv_mask(dev, 0x61c118 + loff, mask, config[1] << shift);
1232 nv_mask(dev, 0x61c120 + loff, mask, config[2] << shift);
1233 nv_mask(dev, 0x61c130 + loff, 0x0000ff00, config[3] << 8);
1234 nv_mask(dev, 0x61c13c + loff, 0x00000000, 0x00000000);
1235}
1236
1237static void
1238nvd0_sor_dp_link_set(struct drm_device *dev, struct dcb_entry *dcb, int crtc,
1239 int link_nr, u32 link_bw, bool enhframe)
1240{
1241 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1242 const u32 loff = (or * 0x800) + (link * 0x80);
1243 const u32 soff = (or * 0x800);
1244 u32 dpctrl = nv_rd32(dev, 0x61c10c + loff) & ~0x001f4000;
1245 u32 clksor = nv_rd32(dev, 0x612300 + soff) & ~0x007c0000;
1246 u32 script = 0x0000, lane_mask = 0;
1247 u8 *table, *entry;
1248 int i;
1249
1250 link_bw /= 27000;
1251
1252 table = nouveau_dp_bios_data(dev, dcb, &entry);
1253 if (table) {
1254 if (table[0] == 0x30) entry = ROMPTR(dev, entry[10]);
1255 else entry = NULL;
1256
1257 while (entry) {
1258 if (entry[0] >= link_bw)
1259 break;
1260 entry += 3;
1261 }
1262
1263 nouveau_bios_run_init_table(dev, script, dcb, crtc);
1264 }
1265
1266 clksor |= link_bw << 18;
1267 dpctrl |= ((1 << link_nr) - 1) << 16;
1268 if (enhframe)
1269 dpctrl |= 0x00004000;
1270
1271 for (i = 0; i < link_nr; i++)
1272 lane_mask |= 1 << (nvd0_sor_dp_lane_map(dev, dcb, i) >> 3);
1273
1274 nv_wr32(dev, 0x612300 + soff, clksor);
1275 nv_wr32(dev, 0x61c10c + loff, dpctrl);
1276 nv_mask(dev, 0x61c130 + loff, 0x0000000f, lane_mask);
1277}
1278
1279static void
1280nvd0_sor_dp_link_get(struct drm_device *dev, struct dcb_entry *dcb,
1281 u32 *link_nr, u32 *link_bw)
1282{
1283 const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
1284 const u32 loff = (or * 0x800) + (link * 0x80);
1285 const u32 soff = (or * 0x800);
1286 u32 dpctrl = nv_rd32(dev, 0x61c10c + loff) & 0x000f0000;
1287 u32 clksor = nv_rd32(dev, 0x612300 + soff);
1288
1289 if (dpctrl > 0x00030000) *link_nr = 4;
1290 else if (dpctrl > 0x00010000) *link_nr = 2;
1291 else *link_nr = 1;
1292
1293 *link_bw = (clksor & 0x007c0000) >> 18;
1294 *link_bw *= 27000;
1295}
1296
1297static void
1298nvd0_sor_dp_calc_tu(struct drm_device *dev, struct dcb_entry *dcb,
1299 u32 crtc, u32 datarate)
1300{
1301 const u32 symbol = 100000;
1302 const u32 TU = 64;
1303 u32 link_nr, link_bw;
1304 u64 ratio, value;
1305
1306 nvd0_sor_dp_link_get(dev, dcb, &link_nr, &link_bw);
1307
1308 ratio = datarate;
1309 ratio *= symbol;
1310 do_div(ratio, link_nr * link_bw);
1311
1312 value = (symbol - ratio) * TU;
1313 value *= ratio;
1314 do_div(value, symbol);
1315 do_div(value, symbol);
1316
1317 value += 5;
1318 value |= 0x08000000;
1319
1320 nv_wr32(dev, 0x616610 + (crtc * 0x800), value);
1321}
1322
Ben Skeggs83fc0832011-07-05 13:08:40 +10001323static void
1324nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
1325{
1326 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1327 struct drm_device *dev = encoder->dev;
1328 struct drm_encoder *partner;
1329 int or = nv_encoder->or;
1330 u32 dpms_ctrl;
1331
1332 nv_encoder->last_dpms = mode;
1333
1334 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1335 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1336
1337 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1338 continue;
1339
1340 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001341 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001342 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1343 return;
1344 break;
1345 }
1346 }
1347
1348 dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
1349 dpms_ctrl |= 0x80000000;
1350
1351 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
1352 nv_mask(dev, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
1353 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
1354 nv_wait(dev, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001355
1356 if (nv_encoder->dcb->type == OUTPUT_DP) {
1357 struct dp_train_func func = {
1358 .link_set = nvd0_sor_dp_link_set,
1359 .train_set = nvd0_sor_dp_train_set,
1360 .train_adj = nvd0_sor_dp_train_adj
1361 };
1362
1363 nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
1364 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001365}
1366
1367static bool
1368nvd0_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
1369 struct drm_display_mode *adjusted_mode)
1370{
1371 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1372 struct nouveau_connector *nv_connector;
1373
1374 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1375 if (nv_connector && nv_connector->native_mode) {
1376 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1377 int id = adjusted_mode->base.id;
1378 *adjusted_mode = *nv_connector->native_mode;
1379 adjusted_mode->base.id = id;
1380 }
1381 }
1382
1383 return true;
1384}
1385
1386static void
1387nvd0_sor_prepare(struct drm_encoder *encoder)
1388{
1389}
1390
1391static void
1392nvd0_sor_commit(struct drm_encoder *encoder)
1393{
1394}
1395
1396static void
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001397nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
1398 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001399{
Ben Skeggs78951d22011-11-11 18:13:13 +10001400 struct drm_device *dev = encoder->dev;
1401 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001402 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1403 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001404 struct nouveau_connector *nv_connector;
1405 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001406 u32 mode_ctrl = (1 << nv_crtc->index);
Ben Skeggsff8ff502011-07-08 11:53:37 +10001407 u32 *push, or_config;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001408
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001409 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1410 switch (nv_encoder->dcb->type) {
1411 case OUTPUT_TMDS:
1412 if (nv_encoder->dcb->sorconf.link & 1) {
1413 if (mode->clock < 165000)
1414 mode_ctrl |= 0x00000100;
1415 else
1416 mode_ctrl |= 0x00000500;
1417 } else {
1418 mode_ctrl |= 0x00000200;
1419 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001420
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001421 or_config = (mode_ctrl & 0x00000f00) >> 8;
1422 if (mode->clock >= 165000)
1423 or_config |= 0x0100;
Ben Skeggs78951d22011-11-11 18:13:13 +10001424
1425 nvd0_hdmi_mode_set(encoder, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001426 break;
1427 case OUTPUT_LVDS:
1428 or_config = (mode_ctrl & 0x00000f00) >> 8;
1429 if (bios->fp_no_ddc) {
1430 if (bios->fp.dual_link)
1431 or_config |= 0x0100;
1432 if (bios->fp.if_is_24bit)
1433 or_config |= 0x0200;
1434 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001435 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001436 if (((u8 *)nv_connector->edid)[121] == 2)
1437 or_config |= 0x0100;
1438 } else
1439 if (mode->clock >= bios->fp.duallink_transition_clk) {
1440 or_config |= 0x0100;
1441 }
1442
1443 if (or_config & 0x0100) {
1444 if (bios->fp.strapless_is_24bit & 2)
1445 or_config |= 0x0200;
1446 } else {
1447 if (bios->fp.strapless_is_24bit & 1)
1448 or_config |= 0x0200;
1449 }
1450
1451 if (nv_connector->base.display_info.bpc == 8)
1452 or_config |= 0x0200;
1453
1454 }
1455 break;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001456 case OUTPUT_DP:
1457 if (nv_connector->base.display_info.bpc == 6)
1458 nv_encoder->dp.datarate = mode->clock * 18 / 8;
1459 else
1460 nv_encoder->dp.datarate = mode->clock * 24 / 8;
1461
1462 if (nv_encoder->dcb->sorconf.link & 1)
1463 mode_ctrl |= 0x00000800;
1464 else
1465 mode_ctrl |= 0x00000900;
1466
1467 or_config = (mode_ctrl & 0x00000f00) >> 8;
1468 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001469 default:
1470 BUG_ON(1);
1471 break;
1472 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001473
Ben Skeggs83fc0832011-07-05 13:08:40 +10001474 nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
1475
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001476 if (nv_encoder->dcb->type == OUTPUT_DP) {
1477 nvd0_sor_dp_calc_tu(dev, nv_encoder->dcb, nv_crtc->index,
1478 nv_encoder->dp.datarate);
1479 }
1480
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001481 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001482 if (push) {
Ben Skeggsff8ff502011-07-08 11:53:37 +10001483 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 2);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001484 evo_data(push, mode_ctrl);
Ben Skeggsff8ff502011-07-08 11:53:37 +10001485 evo_data(push, or_config);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001486 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001487 }
1488
1489 nv_encoder->crtc = encoder->crtc;
1490}
1491
1492static void
1493nvd0_sor_disconnect(struct drm_encoder *encoder)
1494{
1495 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1496 struct drm_device *dev = encoder->dev;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001497 u32 *push;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001498
1499 if (nv_encoder->crtc) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001500 nvd0_crtc_prepare(nv_encoder->crtc);
1501
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001502 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001503 if (push) {
1504 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1505 evo_data(push, 0x00000000);
1506 evo_mthd(push, 0x0080, 1);
1507 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001508 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001509 }
1510
Ben Skeggs78951d22011-11-11 18:13:13 +10001511 nvd0_hdmi_disconnect(encoder);
1512
Ben Skeggs83fc0832011-07-05 13:08:40 +10001513 nv_encoder->crtc = NULL;
1514 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1515 }
1516}
1517
1518static void
1519nvd0_sor_destroy(struct drm_encoder *encoder)
1520{
1521 drm_encoder_cleanup(encoder);
1522 kfree(encoder);
1523}
1524
1525static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
1526 .dpms = nvd0_sor_dpms,
1527 .mode_fixup = nvd0_sor_mode_fixup,
1528 .prepare = nvd0_sor_prepare,
1529 .commit = nvd0_sor_commit,
1530 .mode_set = nvd0_sor_mode_set,
1531 .disable = nvd0_sor_disconnect,
1532 .get_crtc = nvd0_display_crtc_get,
1533};
1534
1535static const struct drm_encoder_funcs nvd0_sor_func = {
1536 .destroy = nvd0_sor_destroy,
1537};
1538
1539static int
1540nvd0_sor_create(struct drm_connector *connector, struct dcb_entry *dcbe)
1541{
1542 struct drm_device *dev = connector->dev;
1543 struct nouveau_encoder *nv_encoder;
1544 struct drm_encoder *encoder;
1545
1546 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1547 if (!nv_encoder)
1548 return -ENOMEM;
1549 nv_encoder->dcb = dcbe;
1550 nv_encoder->or = ffs(dcbe->or) - 1;
1551 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1552
1553 encoder = to_drm_encoder(nv_encoder);
1554 encoder->possible_crtcs = dcbe->heads;
1555 encoder->possible_clones = 0;
1556 drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
1557 drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
1558
1559 drm_mode_connector_attach_encoder(connector, encoder);
1560 return 0;
1561}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001562
1563/******************************************************************************
1564 * IRQ
1565 *****************************************************************************/
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001566static struct dcb_entry *
1567lookup_dcb(struct drm_device *dev, int id, u32 mc)
1568{
1569 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsc6748442012-03-11 16:13:49 +10001570 int type, or, i, link = -1;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001571
1572 if (id < 4) {
1573 type = OUTPUT_ANALOG;
1574 or = id;
1575 } else {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001576 switch (mc & 0x00000f00) {
Ben Skeggsc6748442012-03-11 16:13:49 +10001577 case 0x00000000: link = 0; type = OUTPUT_LVDS; break;
1578 case 0x00000100: link = 0; type = OUTPUT_TMDS; break;
1579 case 0x00000200: link = 1; type = OUTPUT_TMDS; break;
1580 case 0x00000500: link = 0; type = OUTPUT_TMDS; break;
1581 case 0x00000800: link = 0; type = OUTPUT_DP; break;
1582 case 0x00000900: link = 1; type = OUTPUT_DP; break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001583 default:
Ben Skeggsee417792011-07-08 14:34:45 +10001584 NV_ERROR(dev, "PDISP: unknown SOR mc 0x%08x\n", mc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001585 return NULL;
1586 }
1587
1588 or = id - 4;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001589 }
1590
1591 for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
1592 struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
Ben Skeggsc6748442012-03-11 16:13:49 +10001593 if (dcb->type == type && (dcb->or & (1 << or)) &&
1594 (link < 0 || link == !(dcb->sorconf.link & 1)))
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001595 return dcb;
1596 }
1597
Ben Skeggsee417792011-07-08 14:34:45 +10001598 NV_ERROR(dev, "PDISP: DCB for %d/0x%08x not found\n", id, mc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001599 return NULL;
1600}
1601
Ben Skeggs46005222011-07-05 11:01:13 +10001602static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001603nvd0_display_unk1_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001604{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001605 struct dcb_entry *dcb;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001606 int i;
1607
Ben Skeggsee417792011-07-08 14:34:45 +10001608 for (i = 0; mask && i < 8; i++) {
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001609 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
Ben Skeggsee417792011-07-08 14:34:45 +10001610 if (!(mcc & (1 << crtc)))
1611 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001612
Ben Skeggsee417792011-07-08 14:34:45 +10001613 dcb = lookup_dcb(dev, i, mcc);
1614 if (!dcb)
1615 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001616
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001617 nouveau_bios_run_display_table(dev, 0x0000, -1, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001618 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001619
Ben Skeggs270a5742011-07-05 14:16:05 +10001620 nv_wr32(dev, 0x6101d4, 0x00000000);
1621 nv_wr32(dev, 0x6109d4, 0x00000000);
1622 nv_wr32(dev, 0x6101d0, 0x80000000);
1623}
1624
1625static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001626nvd0_display_unk2_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001627{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001628 struct dcb_entry *dcb;
Ben Skeggs37b034a2011-07-08 14:43:19 +10001629 u32 or, tmp, pclk;
Ben Skeggsee417792011-07-08 14:34:45 +10001630 int i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001631
Ben Skeggsee417792011-07-08 14:34:45 +10001632 for (i = 0; mask && i < 8; i++) {
1633 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
1634 if (!(mcc & (1 << crtc)))
1635 continue;
1636
1637 dcb = lookup_dcb(dev, i, mcc);
1638 if (!dcb)
1639 continue;
1640
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001641 nouveau_bios_run_display_table(dev, 0x0000, -2, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001642 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001643
Ben Skeggsee417792011-07-08 14:34:45 +10001644 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
1645 if (mask & 0x00010000) {
1646 nv50_crtc_set_clock(dev, crtc, pclk);
1647 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001648
Ben Skeggsee417792011-07-08 14:34:45 +10001649 for (i = 0; mask && i < 8; i++) {
1650 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1651 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1652 if (!(mcp & (1 << crtc)))
1653 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001654
Ben Skeggsee417792011-07-08 14:34:45 +10001655 dcb = lookup_dcb(dev, i, mcp);
1656 if (!dcb)
1657 continue;
1658 or = ffs(dcb->or) - 1;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001659
Ben Skeggsee417792011-07-08 14:34:45 +10001660 nouveau_bios_run_display_table(dev, cfg, pclk, dcb, crtc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001661
Ben Skeggsee417792011-07-08 14:34:45 +10001662 nv_wr32(dev, 0x612200 + (crtc * 0x800), 0x00000000);
1663 switch (dcb->type) {
1664 case OUTPUT_ANALOG:
1665 nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
1666 break;
1667 case OUTPUT_TMDS:
1668 case OUTPUT_LVDS:
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001669 case OUTPUT_DP:
Ben Skeggsee417792011-07-08 14:34:45 +10001670 if (cfg & 0x00000100)
1671 tmp = 0x00000101;
1672 else
1673 tmp = 0x00000000;
1674
1675 nv_mask(dev, 0x612300 + (or * 0x800), 0x00000707, tmp);
1676 break;
1677 default:
1678 break;
1679 }
1680
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001681 break;
1682 }
1683
Ben Skeggs270a5742011-07-05 14:16:05 +10001684 nv_wr32(dev, 0x6101d4, 0x00000000);
1685 nv_wr32(dev, 0x6109d4, 0x00000000);
1686 nv_wr32(dev, 0x6101d0, 0x80000000);
1687}
1688
1689static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001690nvd0_display_unk4_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001691{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001692 struct dcb_entry *dcb;
Ben Skeggsee417792011-07-08 14:34:45 +10001693 int pclk, i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001694
Ben Skeggsee417792011-07-08 14:34:45 +10001695 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001696
Ben Skeggsee417792011-07-08 14:34:45 +10001697 for (i = 0; mask && i < 8; i++) {
1698 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1699 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1700 if (!(mcp & (1 << crtc)))
1701 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001702
Ben Skeggsee417792011-07-08 14:34:45 +10001703 dcb = lookup_dcb(dev, i, mcp);
1704 if (!dcb)
1705 continue;
1706
1707 nouveau_bios_run_display_table(dev, cfg, -pclk, dcb, crtc);
1708 }
1709
Ben Skeggs270a5742011-07-05 14:16:05 +10001710 nv_wr32(dev, 0x6101d4, 0x00000000);
1711 nv_wr32(dev, 0x6109d4, 0x00000000);
1712 nv_wr32(dev, 0x6101d0, 0x80000000);
1713}
1714
1715static void
Ben Skeggsf20ce962011-07-08 13:17:01 +10001716nvd0_display_bh(unsigned long data)
1717{
1718 struct drm_device *dev = (struct drm_device *)data;
1719 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001720 u32 mask = 0, crtc = ~0;
Ben Skeggs37b034a2011-07-08 14:43:19 +10001721 int i;
1722
1723 if (drm_debug & (DRM_UT_DRIVER | DRM_UT_KMS)) {
1724 NV_INFO(dev, "PDISP: modeset req %d\n", disp->modeset);
1725 NV_INFO(dev, " STAT: 0x%08x 0x%08x 0x%08x\n",
1726 nv_rd32(dev, 0x6101d0),
1727 nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
1728 for (i = 0; i < 8; i++) {
1729 NV_INFO(dev, " %s%d: 0x%08x 0x%08x\n",
1730 i < 4 ? "DAC" : "SOR", i,
1731 nv_rd32(dev, 0x640180 + (i * 0x20)),
1732 nv_rd32(dev, 0x660180 + (i * 0x20)));
1733 }
1734 }
1735
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001736 while (!mask && ++crtc < dev->mode_config.num_crtc)
1737 mask = nv_rd32(dev, 0x6101d4 + (crtc * 0x800));
Ben Skeggsf20ce962011-07-08 13:17:01 +10001738
Ben Skeggsee417792011-07-08 14:34:45 +10001739 if (disp->modeset & 0x00000001)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001740 nvd0_display_unk1_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001741 if (disp->modeset & 0x00000002)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001742 nvd0_display_unk2_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001743 if (disp->modeset & 0x00000004)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001744 nvd0_display_unk4_handler(dev, crtc, mask);
Ben Skeggsf20ce962011-07-08 13:17:01 +10001745}
1746
1747static void
Ben Skeggs46005222011-07-05 11:01:13 +10001748nvd0_display_intr(struct drm_device *dev)
1749{
Ben Skeggsf20ce962011-07-08 13:17:01 +10001750 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs46005222011-07-05 11:01:13 +10001751 u32 intr = nv_rd32(dev, 0x610088);
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001752 int i;
Ben Skeggs46005222011-07-05 11:01:13 +10001753
Ben Skeggs84e052e2011-11-13 03:43:30 +10001754 if (intr & 0x00000001) {
1755 u32 stat = nv_rd32(dev, 0x61008c);
1756 nv_wr32(dev, 0x61008c, stat);
1757 intr &= ~0x00000001;
1758 }
1759
Ben Skeggs46005222011-07-05 11:01:13 +10001760 if (intr & 0x00000002) {
1761 u32 stat = nv_rd32(dev, 0x61009c);
1762 int chid = ffs(stat) - 1;
1763 if (chid >= 0) {
1764 u32 mthd = nv_rd32(dev, 0x6101f0 + (chid * 12));
1765 u32 data = nv_rd32(dev, 0x6101f4 + (chid * 12));
1766 u32 unkn = nv_rd32(dev, 0x6101f8 + (chid * 12));
1767
1768 NV_INFO(dev, "EvoCh: chid %d mthd 0x%04x data 0x%08x "
1769 "0x%08x 0x%08x\n",
1770 chid, (mthd & 0x0000ffc), data, mthd, unkn);
1771 nv_wr32(dev, 0x61009c, (1 << chid));
1772 nv_wr32(dev, 0x6101f0 + (chid * 12), 0x90000000);
1773 }
1774
1775 intr &= ~0x00000002;
1776 }
1777
Ben Skeggs270a5742011-07-05 14:16:05 +10001778 if (intr & 0x00100000) {
1779 u32 stat = nv_rd32(dev, 0x6100ac);
1780
1781 if (stat & 0x00000007) {
Ben Skeggsee417792011-07-08 14:34:45 +10001782 disp->modeset = stat;
Ben Skeggsf20ce962011-07-08 13:17:01 +10001783 tasklet_schedule(&disp->tasklet);
Ben Skeggs270a5742011-07-05 14:16:05 +10001784
Ben Skeggsf20ce962011-07-08 13:17:01 +10001785 nv_wr32(dev, 0x6100ac, (stat & 0x00000007));
Ben Skeggs270a5742011-07-05 14:16:05 +10001786 stat &= ~0x00000007;
1787 }
1788
1789 if (stat) {
1790 NV_INFO(dev, "PDISP: unknown intr24 0x%08x\n", stat);
1791 nv_wr32(dev, 0x6100ac, stat);
1792 }
1793
1794 intr &= ~0x00100000;
1795 }
1796
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001797 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1798 u32 mask = 0x01000000 << i;
1799 if (intr & mask) {
1800 u32 stat = nv_rd32(dev, 0x6100bc + (i * 0x800));
1801 nv_wr32(dev, 0x6100bc + (i * 0x800), stat);
1802 intr &= ~mask;
1803 }
Ben Skeggs46005222011-07-05 11:01:13 +10001804 }
1805
1806 if (intr)
1807 NV_INFO(dev, "PDISP: unknown intr 0x%08x\n", intr);
1808}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001809
1810/******************************************************************************
1811 * Init
1812 *****************************************************************************/
Ben Skeggs2a44e492011-11-09 11:36:33 +10001813void
Ben Skeggs26f6d882011-07-04 16:25:18 +10001814nvd0_display_fini(struct drm_device *dev)
1815{
1816 int i;
1817
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001818 /* fini cursors + overlays + flips */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001819 for (i = 1; i >= 0; i--) {
1820 evo_fini_pio(dev, EVO_CURS(i));
Ben Skeggs8a464382011-11-12 23:52:07 +10001821 evo_fini_pio(dev, EVO_OIMM(i));
1822 evo_fini_dma(dev, EVO_OVLY(i));
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001823 evo_fini_dma(dev, EVO_FLIP(i));
Ben Skeggs26f6d882011-07-04 16:25:18 +10001824 }
1825
1826 /* fini master */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001827 evo_fini_dma(dev, EVO_MASTER);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001828}
1829
1830int
1831nvd0_display_init(struct drm_device *dev)
1832{
1833 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001834 int ret, i;
Ben Skeggsefd272a2011-07-05 11:58:58 +10001835 u32 *push;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001836
1837 if (nv_rd32(dev, 0x6100ac) & 0x00000100) {
1838 nv_wr32(dev, 0x6100ac, 0x00000100);
1839 nv_mask(dev, 0x6194e8, 0x00000001, 0x00000000);
1840 if (!nv_wait(dev, 0x6194e8, 0x00000002, 0x00000000)) {
1841 NV_ERROR(dev, "PDISP: 0x6194e8 0x%08x\n",
1842 nv_rd32(dev, 0x6194e8));
1843 return -EBUSY;
1844 }
1845 }
1846
Ben Skeggsa36f04c2011-07-06 14:39:23 +10001847 /* nfi what these are exactly, i do know that SOR_MODE_CTRL won't
1848 * work at all unless you do the SOR part below.
1849 */
1850 for (i = 0; i < 3; i++) {
1851 u32 dac = nv_rd32(dev, 0x61a000 + (i * 0x800));
1852 nv_wr32(dev, 0x6101c0 + (i * 0x800), dac);
1853 }
1854
1855 for (i = 0; i < 4; i++) {
1856 u32 sor = nv_rd32(dev, 0x61c000 + (i * 0x800));
1857 nv_wr32(dev, 0x6301c4 + (i * 0x800), sor);
1858 }
1859
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001860 for (i = 0; i < dev->mode_config.num_crtc; i++) {
Ben Skeggsa36f04c2011-07-06 14:39:23 +10001861 u32 crtc0 = nv_rd32(dev, 0x616104 + (i * 0x800));
1862 u32 crtc1 = nv_rd32(dev, 0x616108 + (i * 0x800));
1863 u32 crtc2 = nv_rd32(dev, 0x61610c + (i * 0x800));
1864 nv_wr32(dev, 0x6101b4 + (i * 0x800), crtc0);
1865 nv_wr32(dev, 0x6101b8 + (i * 0x800), crtc1);
1866 nv_wr32(dev, 0x6101bc + (i * 0x800), crtc2);
1867 }
1868
1869 /* point at our hash table / objects, enable interrupts */
Ben Skeggs26f6d882011-07-04 16:25:18 +10001870 nv_wr32(dev, 0x610010, (disp->mem->vinst >> 8) | 9);
Ben Skeggs270a5742011-07-05 14:16:05 +10001871 nv_mask(dev, 0x6100b0, 0x00000307, 0x00000307);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001872
1873 /* init master */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001874 ret = evo_init_dma(dev, EVO_MASTER);
1875 if (ret)
1876 goto error;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001877
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001878 /* init flips + overlays + cursors */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001879 for (i = 0; i < dev->mode_config.num_crtc; i++) {
Ben Skeggsa63a97e2011-11-16 15:22:34 +10001880 if ((ret = evo_init_dma(dev, EVO_FLIP(i))) ||
Ben Skeggs8a464382011-11-12 23:52:07 +10001881 (ret = evo_init_dma(dev, EVO_OVLY(i))) ||
1882 (ret = evo_init_pio(dev, EVO_OIMM(i))) ||
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001883 (ret = evo_init_pio(dev, EVO_CURS(i))))
1884 goto error;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001885 }
1886
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001887 push = evo_wait(dev, EVO_MASTER, 32);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001888 if (!push) {
1889 ret = -EBUSY;
1890 goto error;
1891 }
Ben Skeggsefd272a2011-07-05 11:58:58 +10001892 evo_mthd(push, 0x0088, 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001893 evo_data(push, NvEvoSync);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001894 evo_mthd(push, 0x0084, 1);
1895 evo_data(push, 0x00000000);
1896 evo_mthd(push, 0x0084, 1);
1897 evo_data(push, 0x80000000);
1898 evo_mthd(push, 0x008c, 1);
1899 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001900 evo_kick(push, dev, EVO_MASTER);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001901
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001902error:
1903 if (ret)
1904 nvd0_display_fini(dev);
1905 return ret;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001906}
1907
1908void
1909nvd0_display_destroy(struct drm_device *dev)
1910{
1911 struct drm_nouveau_private *dev_priv = dev->dev_private;
1912 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs51beb422011-07-05 10:33:08 +10001913 struct pci_dev *pdev = dev->pdev;
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001914 int i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001915
Ben Skeggs8a464382011-11-12 23:52:07 +10001916 for (i = 0; i < EVO_DMA_NR; i++) {
Ben Skeggs3376ee32011-11-12 14:28:12 +10001917 struct evo *evo = &disp->evo[i];
Ben Skeggs3376ee32011-11-12 14:28:12 +10001918 pci_free_consistent(pdev, PAGE_SIZE, evo->ptr, evo->handle);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001919 }
1920
Ben Skeggs26f6d882011-07-04 16:25:18 +10001921 nouveau_gpuobj_ref(NULL, &disp->mem);
Ben Skeggs816af2f2011-11-16 15:48:48 +10001922 nouveau_bo_unmap(disp->sync);
1923 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs46005222011-07-05 11:01:13 +10001924 nouveau_irq_unregister(dev, 26);
Ben Skeggs51beb422011-07-05 10:33:08 +10001925
1926 dev_priv->engine.display.priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001927 kfree(disp);
1928}
1929
1930int
1931nvd0_display_create(struct drm_device *dev)
1932{
1933 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsefd272a2011-07-05 11:58:58 +10001934 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001935 struct dcb_table *dcb = &dev_priv->vbios.dcb;
1936 struct drm_connector *connector, *tmp;
Ben Skeggs51beb422011-07-05 10:33:08 +10001937 struct pci_dev *pdev = dev->pdev;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001938 struct nvd0_display *disp;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001939 struct dcb_entry *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001940 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001941
1942 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
1943 if (!disp)
1944 return -ENOMEM;
1945 dev_priv->engine.display.priv = disp;
1946
Ben Skeggs438d99e2011-07-05 16:48:06 +10001947 /* create crtc objects to represent the hw heads */
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10001948 crtcs = nv_rd32(dev, 0x022448);
1949 for (i = 0; i < crtcs; i++) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001950 ret = nvd0_crtc_create(dev, i);
1951 if (ret)
1952 goto out;
1953 }
1954
Ben Skeggs83fc0832011-07-05 13:08:40 +10001955 /* create encoder/connector objects based on VBIOS DCB table */
1956 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
1957 connector = nouveau_connector_create(dev, dcbe->connector);
1958 if (IS_ERR(connector))
1959 continue;
1960
1961 if (dcbe->location != DCB_LOC_ON_CHIP) {
1962 NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
1963 dcbe->type, ffs(dcbe->or) - 1);
1964 continue;
1965 }
1966
1967 switch (dcbe->type) {
1968 case OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001969 case OUTPUT_LVDS:
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001970 case OUTPUT_DP:
Ben Skeggs83fc0832011-07-05 13:08:40 +10001971 nvd0_sor_create(connector, dcbe);
1972 break;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001973 case OUTPUT_ANALOG:
1974 nvd0_dac_create(connector, dcbe);
1975 break;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001976 default:
1977 NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
1978 dcbe->type, ffs(dcbe->or) - 1);
1979 continue;
1980 }
1981 }
1982
1983 /* cull any connectors we created that don't have an encoder */
1984 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
1985 if (connector->encoder_ids[0])
1986 continue;
1987
1988 NV_WARN(dev, "%s has no encoders, removing\n",
1989 drm_get_connector_name(connector));
1990 connector->funcs->destroy(connector);
1991 }
1992
Ben Skeggs46005222011-07-05 11:01:13 +10001993 /* setup interrupt handling */
Ben Skeggsf20ce962011-07-08 13:17:01 +10001994 tasklet_init(&disp->tasklet, nvd0_display_bh, (unsigned long)dev);
Ben Skeggs46005222011-07-05 11:01:13 +10001995 nouveau_irq_register(dev, 26, nvd0_display_intr);
1996
Ben Skeggs816af2f2011-11-16 15:48:48 +10001997 /* small shared memory area we use for notifiers and semaphores */
1998 ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
1999 0, 0x0000, &disp->sync);
2000 if (!ret) {
2001 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
2002 if (!ret)
2003 ret = nouveau_bo_map(disp->sync);
2004 if (ret)
2005 nouveau_bo_ref(NULL, &disp->sync);
2006 }
2007
2008 if (ret)
2009 goto out;
2010
Ben Skeggs51beb422011-07-05 10:33:08 +10002011 /* hash table and dma objects for the memory areas we care about */
Ben Skeggsefd272a2011-07-05 11:58:58 +10002012 ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
2013 NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002014 if (ret)
2015 goto out;
2016
Ben Skeggs3376ee32011-11-12 14:28:12 +10002017 /* create evo dma channels */
Ben Skeggs8a464382011-11-12 23:52:07 +10002018 for (i = 0; i < EVO_DMA_NR; i++) {
Ben Skeggs3376ee32011-11-12 14:28:12 +10002019 struct evo *evo = &disp->evo[i];
Ben Skeggs816af2f2011-11-16 15:48:48 +10002020 u64 offset = disp->sync->bo.offset;
Ben Skeggs3376ee32011-11-12 14:28:12 +10002021 u32 dmao = 0x1000 + (i * 0x100);
2022 u32 hash = 0x0000 + (i * 0x040);
Ben Skeggs3376ee32011-11-12 14:28:12 +10002023
2024 evo->idx = i;
Ben Skeggs816af2f2011-11-16 15:48:48 +10002025 evo->sem.offset = EVO_SYNC(evo->idx, 0x00);
Ben Skeggs3376ee32011-11-12 14:28:12 +10002026 evo->ptr = pci_alloc_consistent(pdev, PAGE_SIZE, &evo->handle);
2027 if (!evo->ptr) {
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002028 ret = -ENOMEM;
2029 goto out;
2030 }
Ben Skeggs3376ee32011-11-12 14:28:12 +10002031
Ben Skeggs3376ee32011-11-12 14:28:12 +10002032 nv_wo32(disp->mem, dmao + 0x00, 0x00000049);
2033 nv_wo32(disp->mem, dmao + 0x04, (offset + 0x0000) >> 8);
2034 nv_wo32(disp->mem, dmao + 0x08, (offset + 0x0fff) >> 8);
2035 nv_wo32(disp->mem, dmao + 0x0c, 0x00000000);
2036 nv_wo32(disp->mem, dmao + 0x10, 0x00000000);
2037 nv_wo32(disp->mem, dmao + 0x14, 0x00000000);
2038 nv_wo32(disp->mem, hash + 0x00, NvEvoSync);
2039 nv_wo32(disp->mem, hash + 0x04, 0x00000001 | (i << 27) |
2040 ((dmao + 0x00) << 9));
2041
2042 nv_wo32(disp->mem, dmao + 0x20, 0x00000049);
2043 nv_wo32(disp->mem, dmao + 0x24, 0x00000000);
2044 nv_wo32(disp->mem, dmao + 0x28, (dev_priv->vram_size - 1) >> 8);
2045 nv_wo32(disp->mem, dmao + 0x2c, 0x00000000);
2046 nv_wo32(disp->mem, dmao + 0x30, 0x00000000);
2047 nv_wo32(disp->mem, dmao + 0x34, 0x00000000);
2048 nv_wo32(disp->mem, hash + 0x08, NvEvoVRAM);
2049 nv_wo32(disp->mem, hash + 0x0c, 0x00000001 | (i << 27) |
2050 ((dmao + 0x20) << 9));
2051
2052 nv_wo32(disp->mem, dmao + 0x40, 0x00000009);
2053 nv_wo32(disp->mem, dmao + 0x44, 0x00000000);
2054 nv_wo32(disp->mem, dmao + 0x48, (dev_priv->vram_size - 1) >> 8);
2055 nv_wo32(disp->mem, dmao + 0x4c, 0x00000000);
2056 nv_wo32(disp->mem, dmao + 0x50, 0x00000000);
2057 nv_wo32(disp->mem, dmao + 0x54, 0x00000000);
2058 nv_wo32(disp->mem, hash + 0x10, NvEvoVRAM_LP);
2059 nv_wo32(disp->mem, hash + 0x14, 0x00000001 | (i << 27) |
2060 ((dmao + 0x40) << 9));
2061
2062 nv_wo32(disp->mem, dmao + 0x60, 0x0fe00009);
2063 nv_wo32(disp->mem, dmao + 0x64, 0x00000000);
2064 nv_wo32(disp->mem, dmao + 0x68, (dev_priv->vram_size - 1) >> 8);
2065 nv_wo32(disp->mem, dmao + 0x6c, 0x00000000);
2066 nv_wo32(disp->mem, dmao + 0x70, 0x00000000);
2067 nv_wo32(disp->mem, dmao + 0x74, 0x00000000);
2068 nv_wo32(disp->mem, hash + 0x18, NvEvoFB32);
2069 nv_wo32(disp->mem, hash + 0x1c, 0x00000001 | (i << 27) |
2070 ((dmao + 0x60) << 9));
Ben Skeggs51beb422011-07-05 10:33:08 +10002071 }
2072
Ben Skeggs3376ee32011-11-12 14:28:12 +10002073 pinstmem->flush(dev);
2074
Ben Skeggs26f6d882011-07-04 16:25:18 +10002075out:
2076 if (ret)
2077 nvd0_display_destroy(dev);
2078 return ret;
2079}