blob: c168ae3eaa97cab0a18e6a10b9ee811e815a5a66 [file] [log] [blame]
Ben Skeggsebb945a2012-07-20 08:17:34 +10001/*
2 * Copyright 2012 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 Skeggs370c00f2012-08-14 14:11:49 +100025#include <core/object.h>
26#include <core/parent.h>
27#include <core/handle.h>
28#include <core/class.h>
29
Ben Skeggsebb945a2012-07-20 08:17:34 +100030#include <engine/disp.h>
31
Ben Skeggs186ecad2012-11-09 12:09:48 +100032#include <subdev/bios.h>
33#include <subdev/bios/dcb.h>
34#include <subdev/bios/disp.h>
35#include <subdev/bios/init.h>
36#include <subdev/bios/pll.h>
Ben Skeggs88524bc2013-03-05 10:53:54 +100037#include <subdev/devinit.h>
Ben Skeggs446b05a2012-08-14 12:50:14 +100038#include <subdev/timer.h>
Ben Skeggs370c00f2012-08-14 14:11:49 +100039#include <subdev/fb.h>
Ben Skeggs446b05a2012-08-14 12:50:14 +100040
Ben Skeggs70cabe42012-08-14 10:04:04 +100041#include "nv50.h"
42
43/*******************************************************************************
Ben Skeggs370c00f2012-08-14 14:11:49 +100044 * EVO channel base class
Ben Skeggs70cabe42012-08-14 10:04:04 +100045 ******************************************************************************/
46
Ben Skeggs370c00f2012-08-14 14:11:49 +100047int
48nv50_disp_chan_create_(struct nouveau_object *parent,
49 struct nouveau_object *engine,
50 struct nouveau_oclass *oclass, int chid,
51 int length, void **pobject)
52{
53 struct nv50_disp_base *base = (void *)parent;
54 struct nv50_disp_chan *chan;
55 int ret;
56
57 if (base->chan & (1 << chid))
58 return -EBUSY;
59 base->chan |= (1 << chid);
60
61 ret = nouveau_namedb_create_(parent, engine, oclass, 0, NULL,
62 (1ULL << NVDEV_ENGINE_DMAOBJ),
63 length, pobject);
64 chan = *pobject;
65 if (ret)
66 return ret;
67
68 chan->chid = chid;
69 return 0;
70}
71
72void
73nv50_disp_chan_destroy(struct nv50_disp_chan *chan)
74{
75 struct nv50_disp_base *base = (void *)nv_object(chan)->parent;
76 base->chan &= ~(1 << chan->chid);
77 nouveau_namedb_destroy(&chan->base);
78}
79
80u32
Ben Skeggs70cabe42012-08-14 10:04:04 +100081nv50_disp_chan_rd32(struct nouveau_object *object, u64 addr)
82{
Ben Skeggs370c00f2012-08-14 14:11:49 +100083 struct nv50_disp_priv *priv = (void *)object->engine;
84 struct nv50_disp_chan *chan = (void *)object;
85 return nv_rd32(priv, 0x640000 + (chan->chid * 0x1000) + addr);
86}
87
88void
89nv50_disp_chan_wr32(struct nouveau_object *object, u64 addr, u32 data)
90{
91 struct nv50_disp_priv *priv = (void *)object->engine;
92 struct nv50_disp_chan *chan = (void *)object;
93 nv_wr32(priv, 0x640000 + (chan->chid * 0x1000) + addr, data);
94}
95
96/*******************************************************************************
97 * EVO DMA channel base class
98 ******************************************************************************/
99
100static int
101nv50_disp_dmac_object_attach(struct nouveau_object *parent,
102 struct nouveau_object *object, u32 name)
103{
104 struct nv50_disp_base *base = (void *)parent->parent;
105 struct nv50_disp_chan *chan = (void *)parent;
106 u32 addr = nv_gpuobj(object)->node->offset;
107 u32 chid = chan->chid;
108 u32 data = (chid << 28) | (addr << 10) | chid;
109 return nouveau_ramht_insert(base->ramht, chid, name, data);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000110}
111
112static void
Ben Skeggs370c00f2012-08-14 14:11:49 +1000113nv50_disp_dmac_object_detach(struct nouveau_object *parent, int cookie)
Ben Skeggs70cabe42012-08-14 10:04:04 +1000114{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000115 struct nv50_disp_base *base = (void *)parent->parent;
116 nouveau_ramht_remove(base->ramht, cookie);
117}
118
119int
120nv50_disp_dmac_create_(struct nouveau_object *parent,
121 struct nouveau_object *engine,
122 struct nouveau_oclass *oclass, u32 pushbuf, int chid,
123 int length, void **pobject)
124{
125 struct nv50_disp_dmac *dmac;
126 int ret;
127
128 ret = nv50_disp_chan_create_(parent, engine, oclass, chid,
129 length, pobject);
130 dmac = *pobject;
131 if (ret)
132 return ret;
133
134 dmac->pushdma = (void *)nouveau_handle_ref(parent, pushbuf);
135 if (!dmac->pushdma)
136 return -ENOENT;
137
138 switch (nv_mclass(dmac->pushdma)) {
139 case 0x0002:
140 case 0x003d:
141 if (dmac->pushdma->limit - dmac->pushdma->start != 0xfff)
142 return -EINVAL;
143
144 switch (dmac->pushdma->target) {
145 case NV_MEM_TARGET_VRAM:
146 dmac->push = 0x00000000 | dmac->pushdma->start >> 8;
147 break;
Ben Skeggs944234d2012-10-30 10:03:38 +1000148 case NV_MEM_TARGET_PCI_NOSNOOP:
149 dmac->push = 0x00000003 | dmac->pushdma->start >> 8;
150 break;
Ben Skeggs370c00f2012-08-14 14:11:49 +1000151 default:
152 return -EINVAL;
153 }
154 break;
155 default:
156 return -EINVAL;
157 }
158
159 return 0;
160}
161
162void
163nv50_disp_dmac_dtor(struct nouveau_object *object)
164{
165 struct nv50_disp_dmac *dmac = (void *)object;
166 nouveau_object_ref(NULL, (struct nouveau_object **)&dmac->pushdma);
167 nv50_disp_chan_destroy(&dmac->base);
168}
169
170static int
171nv50_disp_dmac_init(struct nouveau_object *object)
172{
173 struct nv50_disp_priv *priv = (void *)object->engine;
174 struct nv50_disp_dmac *dmac = (void *)object;
175 int chid = dmac->base.chid;
176 int ret;
177
178 ret = nv50_disp_chan_init(&dmac->base);
179 if (ret)
180 return ret;
181
182 /* enable error reporting */
183 nv_mask(priv, 0x610028, 0x00010001 << chid, 0x00010001 << chid);
184
185 /* initialise channel for dma command submission */
186 nv_wr32(priv, 0x610204 + (chid * 0x0010), dmac->push);
187 nv_wr32(priv, 0x610208 + (chid * 0x0010), 0x00010000);
188 nv_wr32(priv, 0x61020c + (chid * 0x0010), chid);
189 nv_mask(priv, 0x610200 + (chid * 0x0010), 0x00000010, 0x00000010);
190 nv_wr32(priv, 0x640000 + (chid * 0x1000), 0x00000000);
191 nv_wr32(priv, 0x610200 + (chid * 0x0010), 0x00000013);
192
193 /* wait for it to go inactive */
194 if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x80000000, 0x00000000)) {
195 nv_error(dmac, "init timeout, 0x%08x\n",
196 nv_rd32(priv, 0x610200 + (chid * 0x10)));
197 return -EBUSY;
198 }
199
200 return 0;
201}
202
203static int
204nv50_disp_dmac_fini(struct nouveau_object *object, bool suspend)
205{
206 struct nv50_disp_priv *priv = (void *)object->engine;
207 struct nv50_disp_dmac *dmac = (void *)object;
208 int chid = dmac->base.chid;
209
210 /* deactivate channel */
211 nv_mask(priv, 0x610200 + (chid * 0x0010), 0x00001010, 0x00001000);
212 nv_mask(priv, 0x610200 + (chid * 0x0010), 0x00000003, 0x00000000);
213 if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x001e0000, 0x00000000)) {
214 nv_error(dmac, "fini timeout, 0x%08x\n",
215 nv_rd32(priv, 0x610200 + (chid * 0x10)));
216 if (suspend)
217 return -EBUSY;
218 }
219
220 /* disable error reporting */
221 nv_mask(priv, 0x610028, 0x00010001 << chid, 0x00000000 << chid);
222
223 return nv50_disp_chan_fini(&dmac->base, suspend);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000224}
225
226/*******************************************************************************
227 * EVO master channel object
228 ******************************************************************************/
229
230static int
231nv50_disp_mast_ctor(struct nouveau_object *parent,
232 struct nouveau_object *engine,
233 struct nouveau_oclass *oclass, void *data, u32 size,
234 struct nouveau_object **pobject)
235{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000236 struct nv50_display_mast_class *args = data;
237 struct nv50_disp_dmac *mast;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000238 int ret;
239
Ben Skeggs370c00f2012-08-14 14:11:49 +1000240 if (size < sizeof(*args))
241 return -EINVAL;
242
243 ret = nv50_disp_dmac_create_(parent, engine, oclass, args->pushbuf,
244 0, sizeof(*mast), (void **)&mast);
245 *pobject = nv_object(mast);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000246 if (ret)
247 return ret;
248
Ben Skeggs370c00f2012-08-14 14:11:49 +1000249 nv_parent(mast)->object_attach = nv50_disp_dmac_object_attach;
250 nv_parent(mast)->object_detach = nv50_disp_dmac_object_detach;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000251 return 0;
252}
253
Ben Skeggs70cabe42012-08-14 10:04:04 +1000254static int
255nv50_disp_mast_init(struct nouveau_object *object)
256{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000257 struct nv50_disp_priv *priv = (void *)object->engine;
258 struct nv50_disp_dmac *mast = (void *)object;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000259 int ret;
260
Ben Skeggs370c00f2012-08-14 14:11:49 +1000261 ret = nv50_disp_chan_init(&mast->base);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000262 if (ret)
263 return ret;
264
Ben Skeggs370c00f2012-08-14 14:11:49 +1000265 /* enable error reporting */
266 nv_mask(priv, 0x610028, 0x00010001, 0x00010001);
267
268 /* attempt to unstick channel from some unknown state */
269 if ((nv_rd32(priv, 0x610200) & 0x009f0000) == 0x00020000)
270 nv_mask(priv, 0x610200, 0x00800000, 0x00800000);
271 if ((nv_rd32(priv, 0x610200) & 0x003f0000) == 0x00030000)
272 nv_mask(priv, 0x610200, 0x00600000, 0x00600000);
273
274 /* initialise channel for dma command submission */
275 nv_wr32(priv, 0x610204, mast->push);
276 nv_wr32(priv, 0x610208, 0x00010000);
277 nv_wr32(priv, 0x61020c, 0x00000000);
278 nv_mask(priv, 0x610200, 0x00000010, 0x00000010);
279 nv_wr32(priv, 0x640000, 0x00000000);
280 nv_wr32(priv, 0x610200, 0x01000013);
281
282 /* wait for it to go inactive */
283 if (!nv_wait(priv, 0x610200, 0x80000000, 0x00000000)) {
284 nv_error(mast, "init: 0x%08x\n", nv_rd32(priv, 0x610200));
285 return -EBUSY;
286 }
287
Ben Skeggs70cabe42012-08-14 10:04:04 +1000288 return 0;
289}
290
291static int
292nv50_disp_mast_fini(struct nouveau_object *object, bool suspend)
293{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000294 struct nv50_disp_priv *priv = (void *)object->engine;
295 struct nv50_disp_dmac *mast = (void *)object;
296
297 /* deactivate channel */
298 nv_mask(priv, 0x610200, 0x00000010, 0x00000000);
299 nv_mask(priv, 0x610200, 0x00000003, 0x00000000);
300 if (!nv_wait(priv, 0x610200, 0x001e0000, 0x00000000)) {
301 nv_error(mast, "fini: 0x%08x\n", nv_rd32(priv, 0x610200));
302 if (suspend)
303 return -EBUSY;
304 }
305
306 /* disable error reporting */
307 nv_mask(priv, 0x610028, 0x00010001, 0x00000000);
308
309 return nv50_disp_chan_fini(&mast->base, suspend);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000310}
311
312struct nouveau_ofuncs
313nv50_disp_mast_ofuncs = {
314 .ctor = nv50_disp_mast_ctor,
Ben Skeggs370c00f2012-08-14 14:11:49 +1000315 .dtor = nv50_disp_dmac_dtor,
Ben Skeggs70cabe42012-08-14 10:04:04 +1000316 .init = nv50_disp_mast_init,
317 .fini = nv50_disp_mast_fini,
318 .rd32 = nv50_disp_chan_rd32,
319 .wr32 = nv50_disp_chan_wr32,
320};
321
322/*******************************************************************************
Ben Skeggs370c00f2012-08-14 14:11:49 +1000323 * EVO sync channel objects
Ben Skeggs70cabe42012-08-14 10:04:04 +1000324 ******************************************************************************/
325
326static int
Ben Skeggs370c00f2012-08-14 14:11:49 +1000327nv50_disp_sync_ctor(struct nouveau_object *parent,
Ben Skeggs70cabe42012-08-14 10:04:04 +1000328 struct nouveau_object *engine,
329 struct nouveau_oclass *oclass, void *data, u32 size,
330 struct nouveau_object **pobject)
331{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000332 struct nv50_display_sync_class *args = data;
333 struct nv50_disp_dmac *dmac;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000334 int ret;
335
Dan Carpenteraf1ac182013-01-23 11:27:56 +0300336 if (size < sizeof(*args) || args->head > 1)
Ben Skeggs370c00f2012-08-14 14:11:49 +1000337 return -EINVAL;
338
339 ret = nv50_disp_dmac_create_(parent, engine, oclass, args->pushbuf,
340 1 + args->head, sizeof(*dmac),
341 (void **)&dmac);
342 *pobject = nv_object(dmac);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000343 if (ret)
344 return ret;
345
Ben Skeggs370c00f2012-08-14 14:11:49 +1000346 nv_parent(dmac)->object_attach = nv50_disp_dmac_object_attach;
347 nv_parent(dmac)->object_detach = nv50_disp_dmac_object_detach;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000348 return 0;
349}
350
Ben Skeggs70cabe42012-08-14 10:04:04 +1000351struct nouveau_ofuncs
Ben Skeggs370c00f2012-08-14 14:11:49 +1000352nv50_disp_sync_ofuncs = {
353 .ctor = nv50_disp_sync_ctor,
Ben Skeggs70cabe42012-08-14 10:04:04 +1000354 .dtor = nv50_disp_dmac_dtor,
355 .init = nv50_disp_dmac_init,
356 .fini = nv50_disp_dmac_fini,
357 .rd32 = nv50_disp_chan_rd32,
358 .wr32 = nv50_disp_chan_wr32,
359};
360
361/*******************************************************************************
Ben Skeggs370c00f2012-08-14 14:11:49 +1000362 * EVO overlay channel objects
Ben Skeggs70cabe42012-08-14 10:04:04 +1000363 ******************************************************************************/
364
365static int
Ben Skeggs370c00f2012-08-14 14:11:49 +1000366nv50_disp_ovly_ctor(struct nouveau_object *parent,
Ben Skeggs70cabe42012-08-14 10:04:04 +1000367 struct nouveau_object *engine,
368 struct nouveau_oclass *oclass, void *data, u32 size,
369 struct nouveau_object **pobject)
370{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000371 struct nv50_display_ovly_class *args = data;
372 struct nv50_disp_dmac *dmac;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000373 int ret;
374
Dan Carpenteraf1ac182013-01-23 11:27:56 +0300375 if (size < sizeof(*args) || args->head > 1)
Ben Skeggs370c00f2012-08-14 14:11:49 +1000376 return -EINVAL;
377
378 ret = nv50_disp_dmac_create_(parent, engine, oclass, args->pushbuf,
379 3 + args->head, sizeof(*dmac),
380 (void **)&dmac);
381 *pobject = nv_object(dmac);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000382 if (ret)
383 return ret;
384
Ben Skeggs370c00f2012-08-14 14:11:49 +1000385 nv_parent(dmac)->object_attach = nv50_disp_dmac_object_attach;
386 nv_parent(dmac)->object_detach = nv50_disp_dmac_object_detach;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000387 return 0;
388}
389
Ben Skeggs370c00f2012-08-14 14:11:49 +1000390struct nouveau_ofuncs
391nv50_disp_ovly_ofuncs = {
392 .ctor = nv50_disp_ovly_ctor,
393 .dtor = nv50_disp_dmac_dtor,
394 .init = nv50_disp_dmac_init,
395 .fini = nv50_disp_dmac_fini,
396 .rd32 = nv50_disp_chan_rd32,
397 .wr32 = nv50_disp_chan_wr32,
398};
399
400/*******************************************************************************
401 * EVO PIO channel base class
402 ******************************************************************************/
403
404static int
405nv50_disp_pioc_create_(struct nouveau_object *parent,
406 struct nouveau_object *engine,
407 struct nouveau_oclass *oclass, int chid,
408 int length, void **pobject)
409{
410 return nv50_disp_chan_create_(parent, engine, oclass, chid,
411 length, pobject);
412}
413
Ben Skeggs70cabe42012-08-14 10:04:04 +1000414static void
415nv50_disp_pioc_dtor(struct nouveau_object *object)
416{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000417 struct nv50_disp_pioc *pioc = (void *)object;
418 nv50_disp_chan_destroy(&pioc->base);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000419}
420
421static int
422nv50_disp_pioc_init(struct nouveau_object *object)
423{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000424 struct nv50_disp_priv *priv = (void *)object->engine;
425 struct nv50_disp_pioc *pioc = (void *)object;
426 int chid = pioc->base.chid;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000427 int ret;
428
Ben Skeggs370c00f2012-08-14 14:11:49 +1000429 ret = nv50_disp_chan_init(&pioc->base);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000430 if (ret)
431 return ret;
432
Ben Skeggs370c00f2012-08-14 14:11:49 +1000433 nv_wr32(priv, 0x610200 + (chid * 0x10), 0x00002000);
434 if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x00000000, 0x00000000)) {
435 nv_error(pioc, "timeout0: 0x%08x\n",
436 nv_rd32(priv, 0x610200 + (chid * 0x10)));
437 return -EBUSY;
438 }
439
440 nv_wr32(priv, 0x610200 + (chid * 0x10), 0x00000001);
441 if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x00030000, 0x00010000)) {
442 nv_error(pioc, "timeout1: 0x%08x\n",
443 nv_rd32(priv, 0x610200 + (chid * 0x10)));
444 return -EBUSY;
445 }
446
Ben Skeggs70cabe42012-08-14 10:04:04 +1000447 return 0;
448}
449
450static int
451nv50_disp_pioc_fini(struct nouveau_object *object, bool suspend)
452{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000453 struct nv50_disp_priv *priv = (void *)object->engine;
454 struct nv50_disp_pioc *pioc = (void *)object;
455 int chid = pioc->base.chid;
456
457 nv_mask(priv, 0x610200 + (chid * 0x10), 0x00000001, 0x00000000);
458 if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x00030000, 0x00000000)) {
459 nv_error(pioc, "timeout: 0x%08x\n",
460 nv_rd32(priv, 0x610200 + (chid * 0x10)));
461 if (suspend)
462 return -EBUSY;
463 }
464
465 return nv50_disp_chan_fini(&pioc->base, suspend);
466}
467
468/*******************************************************************************
469 * EVO immediate overlay channel objects
470 ******************************************************************************/
471
472static int
473nv50_disp_oimm_ctor(struct nouveau_object *parent,
474 struct nouveau_object *engine,
475 struct nouveau_oclass *oclass, void *data, u32 size,
476 struct nouveau_object **pobject)
477{
478 struct nv50_display_oimm_class *args = data;
479 struct nv50_disp_pioc *pioc;
480 int ret;
481
482 if (size < sizeof(*args) || args->head > 1)
483 return -EINVAL;
484
485 ret = nv50_disp_pioc_create_(parent, engine, oclass, 5 + args->head,
486 sizeof(*pioc), (void **)&pioc);
487 *pobject = nv_object(pioc);
488 if (ret)
489 return ret;
490
491 return 0;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000492}
493
494struct nouveau_ofuncs
Ben Skeggs370c00f2012-08-14 14:11:49 +1000495nv50_disp_oimm_ofuncs = {
496 .ctor = nv50_disp_oimm_ctor,
497 .dtor = nv50_disp_pioc_dtor,
498 .init = nv50_disp_pioc_init,
499 .fini = nv50_disp_pioc_fini,
500 .rd32 = nv50_disp_chan_rd32,
501 .wr32 = nv50_disp_chan_wr32,
502};
503
504/*******************************************************************************
505 * EVO cursor channel objects
506 ******************************************************************************/
507
508static int
509nv50_disp_curs_ctor(struct nouveau_object *parent,
510 struct nouveau_object *engine,
511 struct nouveau_oclass *oclass, void *data, u32 size,
512 struct nouveau_object **pobject)
513{
514 struct nv50_display_curs_class *args = data;
515 struct nv50_disp_pioc *pioc;
516 int ret;
517
518 if (size < sizeof(*args) || args->head > 1)
519 return -EINVAL;
520
521 ret = nv50_disp_pioc_create_(parent, engine, oclass, 7 + args->head,
522 sizeof(*pioc), (void **)&pioc);
523 *pobject = nv_object(pioc);
524 if (ret)
525 return ret;
526
527 return 0;
528}
529
530struct nouveau_ofuncs
531nv50_disp_curs_ofuncs = {
532 .ctor = nv50_disp_curs_ctor,
Ben Skeggs70cabe42012-08-14 10:04:04 +1000533 .dtor = nv50_disp_pioc_dtor,
534 .init = nv50_disp_pioc_init,
535 .fini = nv50_disp_pioc_fini,
536 .rd32 = nv50_disp_chan_rd32,
537 .wr32 = nv50_disp_chan_wr32,
538};
539
540/*******************************************************************************
541 * Base display object
542 ******************************************************************************/
543
Ben Skeggs1d7c71a2013-01-31 09:23:34 +1000544static void
545nv50_disp_base_vblank_enable(struct nouveau_event *event, int head)
546{
Maarten Lankhorstc8f28f82013-03-05 14:59:26 +0100547 nv_mask(event->priv, 0x61002c, (4 << head), (4 << head));
Ben Skeggs1d7c71a2013-01-31 09:23:34 +1000548}
549
550static void
551nv50_disp_base_vblank_disable(struct nouveau_event *event, int head)
552{
Maarten Lankhorstc8f28f82013-03-05 14:59:26 +0100553 nv_mask(event->priv, 0x61002c, (4 << head), 0);
Ben Skeggs1d7c71a2013-01-31 09:23:34 +1000554}
555
Ben Skeggs70cabe42012-08-14 10:04:04 +1000556static int
557nv50_disp_base_ctor(struct nouveau_object *parent,
558 struct nouveau_object *engine,
559 struct nouveau_oclass *oclass, void *data, u32 size,
560 struct nouveau_object **pobject)
561{
562 struct nv50_disp_priv *priv = (void *)engine;
563 struct nv50_disp_base *base;
564 int ret;
565
566 ret = nouveau_parent_create(parent, engine, oclass, 0,
567 priv->sclass, 0, &base);
568 *pobject = nv_object(base);
569 if (ret)
570 return ret;
571
Ben Skeggs1d7c71a2013-01-31 09:23:34 +1000572 priv->base.vblank->priv = priv;
573 priv->base.vblank->enable = nv50_disp_base_vblank_enable;
574 priv->base.vblank->disable = nv50_disp_base_vblank_disable;
Ben Skeggs2ecda482013-04-24 18:04:22 +1000575 return nouveau_ramht_new(nv_object(base), nv_object(base), 0x1000, 0,
576 &base->ramht);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000577}
578
579static void
580nv50_disp_base_dtor(struct nouveau_object *object)
581{
582 struct nv50_disp_base *base = (void *)object;
Ben Skeggs370c00f2012-08-14 14:11:49 +1000583 nouveau_ramht_ref(NULL, &base->ramht);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000584 nouveau_parent_destroy(&base->base);
585}
586
587static int
588nv50_disp_base_init(struct nouveau_object *object)
589{
Ben Skeggsab772142012-08-14 11:29:57 +1000590 struct nv50_disp_priv *priv = (void *)object->engine;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000591 struct nv50_disp_base *base = (void *)object;
Ben Skeggsab772142012-08-14 11:29:57 +1000592 int ret, i;
593 u32 tmp;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000594
595 ret = nouveau_parent_init(&base->base);
596 if (ret)
597 return ret;
598
Ben Skeggsab772142012-08-14 11:29:57 +1000599 /* The below segments of code copying values from one register to
600 * another appear to inform EVO of the display capabilities or
601 * something similar. NFI what the 0x614004 caps are for..
602 */
603 tmp = nv_rd32(priv, 0x614004);
604 nv_wr32(priv, 0x610184, tmp);
605
606 /* ... CRTC caps */
607 for (i = 0; i < priv->head.nr; i++) {
608 tmp = nv_rd32(priv, 0x616100 + (i * 0x800));
609 nv_wr32(priv, 0x610190 + (i * 0x10), tmp);
610 tmp = nv_rd32(priv, 0x616104 + (i * 0x800));
611 nv_wr32(priv, 0x610194 + (i * 0x10), tmp);
612 tmp = nv_rd32(priv, 0x616108 + (i * 0x800));
613 nv_wr32(priv, 0x610198 + (i * 0x10), tmp);
614 tmp = nv_rd32(priv, 0x61610c + (i * 0x800));
615 nv_wr32(priv, 0x61019c + (i * 0x10), tmp);
616 }
617
618 /* ... DAC caps */
619 for (i = 0; i < priv->dac.nr; i++) {
620 tmp = nv_rd32(priv, 0x61a000 + (i * 0x800));
621 nv_wr32(priv, 0x6101d0 + (i * 0x04), tmp);
622 }
623
624 /* ... SOR caps */
625 for (i = 0; i < priv->sor.nr; i++) {
626 tmp = nv_rd32(priv, 0x61c000 + (i * 0x800));
627 nv_wr32(priv, 0x6101e0 + (i * 0x04), tmp);
628 }
629
Ben Skeggs476e84e2013-02-11 09:24:23 +1000630 /* ... PIOR caps */
Emil Velikovb969fa52013-07-30 01:01:10 +0100631 for (i = 0; i < priv->pior.nr; i++) {
Ben Skeggsab772142012-08-14 11:29:57 +1000632 tmp = nv_rd32(priv, 0x61e000 + (i * 0x800));
633 nv_wr32(priv, 0x6101f0 + (i * 0x04), tmp);
634 }
635
Ben Skeggs446b05a2012-08-14 12:50:14 +1000636 /* steal display away from vbios, or something like that */
637 if (nv_rd32(priv, 0x610024) & 0x00000100) {
638 nv_wr32(priv, 0x610024, 0x00000100);
639 nv_mask(priv, 0x6194e8, 0x00000001, 0x00000000);
640 if (!nv_wait(priv, 0x6194e8, 0x00000002, 0x00000000)) {
641 nv_error(priv, "timeout acquiring display\n");
642 return -EBUSY;
643 }
644 }
645
646 /* point at display engine memory area (hash table, objects) */
Ben Skeggs370c00f2012-08-14 14:11:49 +1000647 nv_wr32(priv, 0x610010, (nv_gpuobj(base->ramht)->addr >> 8) | 9);
Ben Skeggs446b05a2012-08-14 12:50:14 +1000648
649 /* enable supervisor interrupts, disable everything else */
Ben Skeggs370c00f2012-08-14 14:11:49 +1000650 nv_wr32(priv, 0x61002c, 0x00000370);
651 nv_wr32(priv, 0x610028, 0x00000000);
Ben Skeggs70cabe42012-08-14 10:04:04 +1000652 return 0;
653}
654
655static int
656nv50_disp_base_fini(struct nouveau_object *object, bool suspend)
657{
Ben Skeggs446b05a2012-08-14 12:50:14 +1000658 struct nv50_disp_priv *priv = (void *)object->engine;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000659 struct nv50_disp_base *base = (void *)object;
Ben Skeggs446b05a2012-08-14 12:50:14 +1000660
661 /* disable all interrupts */
662 nv_wr32(priv, 0x610024, 0x00000000);
663 nv_wr32(priv, 0x610020, 0x00000000);
664
Ben Skeggs70cabe42012-08-14 10:04:04 +1000665 return nouveau_parent_fini(&base->base, suspend);
666}
667
668struct nouveau_ofuncs
669nv50_disp_base_ofuncs = {
670 .ctor = nv50_disp_base_ctor,
671 .dtor = nv50_disp_base_dtor,
672 .init = nv50_disp_base_init,
673 .fini = nv50_disp_base_fini,
674};
675
Ben Skeggsef22c8b2012-11-09 09:32:56 +1000676static struct nouveau_omthds
677nv50_disp_base_omthds[] = {
678 { SOR_MTHD(NV50_DISP_SOR_PWR) , nv50_sor_mthd },
Ben Skeggs4a230fa2012-11-09 11:25:37 +1000679 { SOR_MTHD(NV50_DISP_SOR_LVDS_SCRIPT) , nv50_sor_mthd },
Ben Skeggsef22c8b2012-11-09 09:32:56 +1000680 { DAC_MTHD(NV50_DISP_DAC_PWR) , nv50_dac_mthd },
681 { DAC_MTHD(NV50_DISP_DAC_LOAD) , nv50_dac_mthd },
Ben Skeggsa2bc2832013-02-11 09:11:08 +1000682 { PIOR_MTHD(NV50_DISP_PIOR_PWR) , nv50_pior_mthd },
683 { PIOR_MTHD(NV50_DISP_PIOR_TMDS_PWR) , nv50_pior_mthd },
684 { PIOR_MTHD(NV50_DISP_PIOR_DP_PWR) , nv50_pior_mthd },
Ben Skeggsef22c8b2012-11-09 09:32:56 +1000685 {},
686};
687
Ben Skeggs70cabe42012-08-14 10:04:04 +1000688static struct nouveau_oclass
689nv50_disp_base_oclass[] = {
Ben Skeggsef22c8b2012-11-09 09:32:56 +1000690 { NV50_DISP_CLASS, &nv50_disp_base_ofuncs, nv50_disp_base_omthds },
Ben Skeggs370c00f2012-08-14 14:11:49 +1000691 {}
Ben Skeggsebb945a2012-07-20 08:17:34 +1000692};
693
694static struct nouveau_oclass
695nv50_disp_sclass[] = {
Ben Skeggs370c00f2012-08-14 14:11:49 +1000696 { NV50_DISP_MAST_CLASS, &nv50_disp_mast_ofuncs },
697 { NV50_DISP_SYNC_CLASS, &nv50_disp_sync_ofuncs },
698 { NV50_DISP_OVLY_CLASS, &nv50_disp_ovly_ofuncs },
699 { NV50_DISP_OIMM_CLASS, &nv50_disp_oimm_ofuncs },
700 { NV50_DISP_CURS_CLASS, &nv50_disp_curs_ofuncs },
Ben Skeggs70cabe42012-08-14 10:04:04 +1000701 {}
Ben Skeggsebb945a2012-07-20 08:17:34 +1000702};
703
Ben Skeggs70cabe42012-08-14 10:04:04 +1000704/*******************************************************************************
705 * Display context, tracks instmem allocation and prevents more than one
706 * client using the display hardware at any time.
707 ******************************************************************************/
708
709static int
710nv50_disp_data_ctor(struct nouveau_object *parent,
711 struct nouveau_object *engine,
712 struct nouveau_oclass *oclass, void *data, u32 size,
713 struct nouveau_object **pobject)
714{
Ben Skeggs370c00f2012-08-14 14:11:49 +1000715 struct nv50_disp_priv *priv = (void *)engine;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000716 struct nouveau_engctx *ectx;
Ben Skeggs370c00f2012-08-14 14:11:49 +1000717 int ret = -EBUSY;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000718
Ben Skeggs370c00f2012-08-14 14:11:49 +1000719 /* no context needed for channel objects... */
720 if (nv_mclass(parent) != NV_DEVICE_CLASS) {
721 atomic_inc(&parent->refcount);
722 *pobject = parent;
Ben Skeggs43e6e512013-04-26 00:12:59 +1000723 return 1;
Ben Skeggs370c00f2012-08-14 14:11:49 +1000724 }
Ben Skeggs70cabe42012-08-14 10:04:04 +1000725
Ben Skeggs370c00f2012-08-14 14:11:49 +1000726 /* allocate display hardware to client */
727 mutex_lock(&nv_subdev(priv)->mutex);
728 if (list_empty(&nv_engine(priv)->contexts)) {
729 ret = nouveau_engctx_create(parent, engine, oclass, NULL,
730 0x10000, 0x10000,
731 NVOBJ_FLAG_HEAP, &ectx);
732 *pobject = nv_object(ectx);
733 }
734 mutex_unlock(&nv_subdev(priv)->mutex);
735 return ret;
Ben Skeggs70cabe42012-08-14 10:04:04 +1000736}
737
738struct nouveau_oclass
739nv50_disp_cclass = {
740 .handle = NV_ENGCTX(DISP, 0x50),
741 .ofuncs = &(struct nouveau_ofuncs) {
742 .ctor = nv50_disp_data_ctor,
743 .dtor = _nouveau_engctx_dtor,
744 .init = _nouveau_engctx_init,
745 .fini = _nouveau_engctx_fini,
746 .rd32 = _nouveau_engctx_rd32,
747 .wr32 = _nouveau_engctx_wr32,
748 },
749};
750
751/*******************************************************************************
752 * Display engine implementation
753 ******************************************************************************/
754
Ben Skeggsebb945a2012-07-20 08:17:34 +1000755static void
Ben Skeggs186ecad2012-11-09 12:09:48 +1000756nv50_disp_intr_error(struct nv50_disp_priv *priv)
757{
758 u32 channels = (nv_rd32(priv, 0x610020) & 0x001f0000) >> 16;
759 u32 addr, data;
760 int chid;
761
762 for (chid = 0; chid < 5; chid++) {
763 if (!(channels & (1 << chid)))
764 continue;
765
766 nv_wr32(priv, 0x610020, 0x00010000 << chid);
767 addr = nv_rd32(priv, 0x610080 + (chid * 0x08));
768 data = nv_rd32(priv, 0x610084 + (chid * 0x08));
769 nv_wr32(priv, 0x610080 + (chid * 0x08), 0x90000000);
770
771 nv_error(priv, "chid %d mthd 0x%04x data 0x%08x 0x%08x\n",
772 chid, addr & 0xffc, data, addr);
773 }
774}
775
Ben Skeggs186ecad2012-11-09 12:09:48 +1000776static u16
777exec_lookup(struct nv50_disp_priv *priv, int head, int outp, u32 ctrl,
778 struct dcb_output *dcb, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
779 struct nvbios_outp *info)
780{
781 struct nouveau_bios *bios = nouveau_bios(priv);
782 u16 mask, type, data;
783
784 if (outp < 4) {
785 type = DCB_OUTPUT_ANALOG;
786 mask = 0;
Ben Skeggs476e84e2013-02-11 09:24:23 +1000787 } else
788 if (outp < 8) {
Ben Skeggs186ecad2012-11-09 12:09:48 +1000789 switch (ctrl & 0x00000f00) {
790 case 0x00000000: type = DCB_OUTPUT_LVDS; mask = 1; break;
791 case 0x00000100: type = DCB_OUTPUT_TMDS; mask = 1; break;
792 case 0x00000200: type = DCB_OUTPUT_TMDS; mask = 2; break;
793 case 0x00000500: type = DCB_OUTPUT_TMDS; mask = 3; break;
794 case 0x00000800: type = DCB_OUTPUT_DP; mask = 1; break;
795 case 0x00000900: type = DCB_OUTPUT_DP; mask = 2; break;
796 default:
797 nv_error(priv, "unknown SOR mc 0x%08x\n", ctrl);
798 return 0x0000;
799 }
Ben Skeggs476e84e2013-02-11 09:24:23 +1000800 outp -= 4;
801 } else {
802 outp = outp - 8;
803 type = 0x0010;
804 mask = 0;
805 switch (ctrl & 0x00000f00) {
806 case 0x00000000: type |= priv->pior.type[outp]; break;
807 default:
808 nv_error(priv, "unknown PIOR mc 0x%08x\n", ctrl);
809 return 0x0000;
810 }
Ben Skeggs186ecad2012-11-09 12:09:48 +1000811 }
812
813 mask = 0x00c0 & (mask << 6);
814 mask |= 0x0001 << outp;
815 mask |= 0x0100 << head;
816
817 data = dcb_outp_match(bios, type, mask, ver, hdr, dcb);
818 if (!data)
819 return 0x0000;
820
Ben Skeggs476e84e2013-02-11 09:24:23 +1000821 /* off-chip encoders require matching the exact encoder type */
822 if (dcb->location != 0)
823 type |= dcb->extdev << 8;
824
Ben Skeggs186ecad2012-11-09 12:09:48 +1000825 return nvbios_outp_match(bios, type, mask, ver, hdr, cnt, len, info);
826}
827
828static bool
829exec_script(struct nv50_disp_priv *priv, int head, int id)
830{
831 struct nouveau_bios *bios = nouveau_bios(priv);
832 struct nvbios_outp info;
833 struct dcb_output dcb;
834 u8 ver, hdr, cnt, len;
835 u16 data;
836 u32 ctrl = 0x00000000;
Emil Velikovb969fa52013-07-30 01:01:10 +0100837 u32 reg;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000838 int i;
839
Ben Skeggs476e84e2013-02-11 09:24:23 +1000840 /* DAC */
Emil Velikovb969fa52013-07-30 01:01:10 +0100841 for (i = 0; !(ctrl & (1 << head)) && i < priv->dac.nr; i++)
Ben Skeggs186ecad2012-11-09 12:09:48 +1000842 ctrl = nv_rd32(priv, 0x610b5c + (i * 8));
843
Ben Skeggs476e84e2013-02-11 09:24:23 +1000844 /* SOR */
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100845 if (!(ctrl & (1 << head))) {
846 if (nv_device(priv)->chipset < 0x90 ||
847 nv_device(priv)->chipset == 0x92 ||
848 nv_device(priv)->chipset == 0xa0) {
Emil Velikovb969fa52013-07-30 01:01:10 +0100849 reg = 0x610b74;
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100850 } else {
Emil Velikovb969fa52013-07-30 01:01:10 +0100851 reg = 0x610798;
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100852 }
Emil Velikovb969fa52013-07-30 01:01:10 +0100853 for (i = 0; !(ctrl & (1 << head)) && i < priv->sor.nr; i++)
854 ctrl = nv_rd32(priv, reg + (i * 8));
855 i += 4;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000856 }
857
Ben Skeggs476e84e2013-02-11 09:24:23 +1000858 /* PIOR */
859 if (!(ctrl & (1 << head))) {
Emil Velikovb969fa52013-07-30 01:01:10 +0100860 for (i = 0; !(ctrl & (1 << head)) && i < priv->pior.nr; i++)
Ben Skeggs476e84e2013-02-11 09:24:23 +1000861 ctrl = nv_rd32(priv, 0x610b84 + (i * 8));
862 i += 8;
863 }
864
Ben Skeggs186ecad2012-11-09 12:09:48 +1000865 if (!(ctrl & (1 << head)))
866 return false;
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100867 i--;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000868
869 data = exec_lookup(priv, head, i, ctrl, &dcb, &ver, &hdr, &cnt, &len, &info);
870 if (data) {
871 struct nvbios_init init = {
872 .subdev = nv_subdev(priv),
873 .bios = bios,
874 .offset = info.script[id],
875 .outp = &dcb,
876 .crtc = head,
877 .execute = 1,
878 };
879
880 return nvbios_exec(&init) == 0;
881 }
882
883 return false;
884}
885
886static u32
887exec_clkcmp(struct nv50_disp_priv *priv, int head, int id, u32 pclk,
888 struct dcb_output *outp)
889{
890 struct nouveau_bios *bios = nouveau_bios(priv);
891 struct nvbios_outp info1;
892 struct nvbios_ocfg info2;
893 u8 ver, hdr, cnt, len;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000894 u32 ctrl = 0x00000000;
Ben Skeggs46c13c12013-02-16 13:49:21 +1000895 u32 data, conf = ~0;
Emil Velikovb969fa52013-07-30 01:01:10 +0100896 u32 reg;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000897 int i;
898
Ben Skeggs476e84e2013-02-11 09:24:23 +1000899 /* DAC */
Emil Velikovb969fa52013-07-30 01:01:10 +0100900 for (i = 0; !(ctrl & (1 << head)) && i < priv->dac.nr; i++)
Ben Skeggs186ecad2012-11-09 12:09:48 +1000901 ctrl = nv_rd32(priv, 0x610b58 + (i * 8));
902
Ben Skeggs476e84e2013-02-11 09:24:23 +1000903 /* SOR */
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100904 if (!(ctrl & (1 << head))) {
905 if (nv_device(priv)->chipset < 0x90 ||
906 nv_device(priv)->chipset == 0x92 ||
907 nv_device(priv)->chipset == 0xa0) {
Emil Velikovb969fa52013-07-30 01:01:10 +0100908 reg = 0x610b70;
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100909 } else {
Emil Velikovb969fa52013-07-30 01:01:10 +0100910 reg = 0x610794;
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100911 }
Emil Velikovb969fa52013-07-30 01:01:10 +0100912 for (i = 0; !(ctrl & (1 << head)) && i < priv->sor.nr; i++)
913 ctrl = nv_rd32(priv, reg + (i * 8));
914 i += 4;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000915 }
916
Ben Skeggs476e84e2013-02-11 09:24:23 +1000917 /* PIOR */
918 if (!(ctrl & (1 << head))) {
Emil Velikovb969fa52013-07-30 01:01:10 +0100919 for (i = 0; !(ctrl & (1 << head)) && i < priv->pior.nr; i++)
Ben Skeggs476e84e2013-02-11 09:24:23 +1000920 ctrl = nv_rd32(priv, 0x610b80 + (i * 8));
921 i += 8;
922 }
923
Ben Skeggs186ecad2012-11-09 12:09:48 +1000924 if (!(ctrl & (1 << head)))
Ben Skeggs46c13c12013-02-16 13:49:21 +1000925 return conf;
Marcin Slusarzc684cef2013-01-03 19:38:45 +0100926 i--;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000927
928 data = exec_lookup(priv, head, i, ctrl, outp, &ver, &hdr, &cnt, &len, &info1);
929 if (!data)
Ben Skeggs46c13c12013-02-16 13:49:21 +1000930 return conf;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000931
Ben Skeggs476e84e2013-02-11 09:24:23 +1000932 if (outp->location == 0) {
933 switch (outp->type) {
934 case DCB_OUTPUT_TMDS:
935 conf = (ctrl & 0x00000f00) >> 8;
936 if (pclk >= 165000)
937 conf |= 0x0100;
938 break;
939 case DCB_OUTPUT_LVDS:
940 conf = priv->sor.lvdsconf;
941 break;
942 case DCB_OUTPUT_DP:
943 conf = (ctrl & 0x00000f00) >> 8;
944 break;
945 case DCB_OUTPUT_ANALOG:
946 default:
947 conf = 0x00ff;
948 break;
949 }
950 } else {
Ben Skeggs186ecad2012-11-09 12:09:48 +1000951 conf = (ctrl & 0x00000f00) >> 8;
Ben Skeggs476e84e2013-02-11 09:24:23 +1000952 pclk = pclk / 2;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000953 }
954
955 data = nvbios_ocfg_match(bios, data, conf, &ver, &hdr, &cnt, &len, &info2);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500956 if (data && id < 0xff) {
Ben Skeggs186ecad2012-11-09 12:09:48 +1000957 data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
958 if (data) {
959 struct nvbios_init init = {
960 .subdev = nv_subdev(priv),
961 .bios = bios,
962 .offset = data,
963 .outp = outp,
964 .crtc = head,
965 .execute = 1,
966 };
967
Ben Skeggs46c13c12013-02-16 13:49:21 +1000968 nvbios_exec(&init);
Ben Skeggs186ecad2012-11-09 12:09:48 +1000969 }
970 }
971
Ben Skeggs46c13c12013-02-16 13:49:21 +1000972 return conf;
Ben Skeggs186ecad2012-11-09 12:09:48 +1000973}
974
975static void
Ben Skeggs16d4c032013-02-20 18:56:33 +1000976nv50_disp_intr_unk10_0(struct nv50_disp_priv *priv, int head)
Ben Skeggs186ecad2012-11-09 12:09:48 +1000977{
Ben Skeggs16d4c032013-02-20 18:56:33 +1000978 exec_script(priv, head, 1);
Ben Skeggs186ecad2012-11-09 12:09:48 +1000979}
980
981static void
Ben Skeggs16d4c032013-02-20 18:56:33 +1000982nv50_disp_intr_unk20_0(struct nv50_disp_priv *priv, int head)
983{
984 exec_script(priv, head, 2);
985}
986
987static void
988nv50_disp_intr_unk20_1(struct nv50_disp_priv *priv, int head)
989{
Ben Skeggs88524bc2013-03-05 10:53:54 +1000990 struct nouveau_devinit *devinit = nouveau_devinit(priv);
Ben Skeggs16d4c032013-02-20 18:56:33 +1000991 u32 pclk = nv_rd32(priv, 0x610ad0 + (head * 0x540)) & 0x3fffff;
992 if (pclk)
Ben Skeggs88524bc2013-03-05 10:53:54 +1000993 devinit->pll_set(devinit, PLL_VPLL0 + head, pclk);
Ben Skeggs16d4c032013-02-20 18:56:33 +1000994}
995
996static void
997nv50_disp_intr_unk20_2_dp(struct nv50_disp_priv *priv,
998 struct dcb_output *outp, u32 pclk)
Ben Skeggs186ecad2012-11-09 12:09:48 +1000999{
1000 const int link = !(outp->sorconf.link & 1);
1001 const int or = ffs(outp->or) - 1;
1002 const u32 soff = ( or * 0x800);
1003 const u32 loff = (link * 0x080) + soff;
1004 const u32 ctrl = nv_rd32(priv, 0x610794 + (or * 8));
Ben Skeggs186ecad2012-11-09 12:09:48 +10001005 const u32 symbol = 100000;
1006 u32 dpctrl = nv_rd32(priv, 0x61c10c + loff) & 0x0000f0000;
1007 u32 clksor = nv_rd32(priv, 0x614300 + soff);
1008 int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
1009 int TU, VTUi, VTUf, VTUa;
1010 u64 link_data_rate, link_ratio, unk;
1011 u32 best_diff = 64 * symbol;
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001012 u32 link_nr, link_bw, bits, r;
Ben Skeggs186ecad2012-11-09 12:09:48 +10001013
1014 /* calculate packed data rate for each lane */
1015 if (dpctrl > 0x00030000) link_nr = 4;
1016 else if (dpctrl > 0x00010000) link_nr = 2;
1017 else link_nr = 1;
1018
1019 if (clksor & 0x000c0000)
1020 link_bw = 270000;
1021 else
1022 link_bw = 162000;
1023
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001024 if ((ctrl & 0xf0000) == 0x60000) bits = 30;
1025 else if ((ctrl & 0xf0000) == 0x50000) bits = 24;
1026 else bits = 18;
1027
Ben Skeggs186ecad2012-11-09 12:09:48 +10001028 link_data_rate = (pclk * bits / 8) / link_nr;
1029
1030 /* calculate ratio of packed data rate to link symbol rate */
1031 link_ratio = link_data_rate * symbol;
1032 r = do_div(link_ratio, link_bw);
1033
1034 for (TU = 64; TU >= 32; TU--) {
1035 /* calculate average number of valid symbols in each TU */
1036 u32 tu_valid = link_ratio * TU;
1037 u32 calc, diff;
1038
1039 /* find a hw representation for the fraction.. */
1040 VTUi = tu_valid / symbol;
1041 calc = VTUi * symbol;
1042 diff = tu_valid - calc;
1043 if (diff) {
1044 if (diff >= (symbol / 2)) {
1045 VTUf = symbol / (symbol - diff);
1046 if (symbol - (VTUf * diff))
1047 VTUf++;
1048
1049 if (VTUf <= 15) {
1050 VTUa = 1;
1051 calc += symbol - (symbol / VTUf);
1052 } else {
1053 VTUa = 0;
1054 VTUf = 1;
1055 calc += symbol;
1056 }
1057 } else {
1058 VTUa = 0;
1059 VTUf = min((int)(symbol / diff), 15);
1060 calc += symbol / VTUf;
1061 }
1062
1063 diff = calc - tu_valid;
1064 } else {
1065 /* no remainder, but the hw doesn't like the fractional
1066 * part to be zero. decrement the integer part and
1067 * have the fraction add a whole symbol back
1068 */
1069 VTUa = 0;
1070 VTUf = 1;
1071 VTUi--;
1072 }
1073
1074 if (diff < best_diff) {
1075 best_diff = diff;
1076 bestTU = TU;
1077 bestVTUa = VTUa;
1078 bestVTUf = VTUf;
1079 bestVTUi = VTUi;
1080 if (diff == 0)
1081 break;
1082 }
1083 }
1084
1085 if (!bestTU) {
1086 nv_error(priv, "unable to find suitable dp config\n");
1087 return;
1088 }
1089
1090 /* XXX close to vbios numbers, but not right */
1091 unk = (symbol - link_ratio) * bestTU;
1092 unk *= link_ratio;
1093 r = do_div(unk, symbol);
1094 r = do_div(unk, symbol);
1095 unk += 6;
1096
1097 nv_mask(priv, 0x61c10c + loff, 0x000001fc, bestTU << 2);
1098 nv_mask(priv, 0x61c128 + loff, 0x010f7f3f, bestVTUa << 24 |
1099 bestVTUf << 16 |
1100 bestVTUi << 8 | unk);
1101}
1102
1103static void
Ben Skeggs16d4c032013-02-20 18:56:33 +10001104nv50_disp_intr_unk20_2(struct nv50_disp_priv *priv, int head)
Ben Skeggs186ecad2012-11-09 12:09:48 +10001105{
1106 struct dcb_output outp;
Ben Skeggs16d4c032013-02-20 18:56:33 +10001107 u32 pclk = nv_rd32(priv, 0x610ad0 + (head * 0x540)) & 0x3fffff;
1108 u32 hval, hreg = 0x614200 + (head * 0x800);
1109 u32 oval, oreg;
Emil Velikov378f2bc2013-07-02 14:44:12 +01001110 u32 mask;
Ben Skeggs16d4c032013-02-20 18:56:33 +10001111 u32 conf = exec_clkcmp(priv, head, 0xff, pclk, &outp);
1112 if (conf != ~0) {
1113 if (outp.location == 0 && outp.type == DCB_OUTPUT_DP) {
1114 u32 soff = (ffs(outp.or) - 1) * 0x08;
1115 u32 ctrl = nv_rd32(priv, 0x610798 + soff);
1116 u32 datarate;
Ben Skeggs186ecad2012-11-09 12:09:48 +10001117
Ben Skeggs16d4c032013-02-20 18:56:33 +10001118 switch ((ctrl & 0x000f0000) >> 16) {
1119 case 6: datarate = pclk * 30 / 8; break;
1120 case 5: datarate = pclk * 24 / 8; break;
1121 case 2:
1122 default:
1123 datarate = pclk * 18 / 8;
1124 break;
Ben Skeggs0a0afd22013-02-18 23:17:53 -05001125 }
1126
Ben Skeggs16d4c032013-02-20 18:56:33 +10001127 nouveau_dp_train(&priv->base, priv->sor.dp,
1128 &outp, head, datarate);
Ben Skeggs186ecad2012-11-09 12:09:48 +10001129 }
Ben Skeggs186ecad2012-11-09 12:09:48 +10001130
Ben Skeggs16d4c032013-02-20 18:56:33 +10001131 exec_clkcmp(priv, head, 0, pclk, &outp);
1132
1133 if (!outp.location && outp.type == DCB_OUTPUT_ANALOG) {
1134 oreg = 0x614280 + (ffs(outp.or) - 1) * 0x800;
1135 oval = 0x00000000;
1136 hval = 0x00000000;
Emil Velikov378f2bc2013-07-02 14:44:12 +01001137 mask = 0xffffffff;
Ben Skeggs16d4c032013-02-20 18:56:33 +10001138 } else
1139 if (!outp.location) {
1140 if (outp.type == DCB_OUTPUT_DP)
1141 nv50_disp_intr_unk20_2_dp(priv, &outp, pclk);
1142 oreg = 0x614300 + (ffs(outp.or) - 1) * 0x800;
1143 oval = (conf & 0x0100) ? 0x00000101 : 0x00000000;
1144 hval = 0x00000000;
Emil Velikov378f2bc2013-07-02 14:44:12 +01001145 mask = 0x00000707;
Ben Skeggs16d4c032013-02-20 18:56:33 +10001146 } else {
1147 oreg = 0x614380 + (ffs(outp.or) - 1) * 0x800;
1148 oval = 0x00000001;
1149 hval = 0x00000001;
Emil Velikov378f2bc2013-07-02 14:44:12 +01001150 mask = 0x00000707;
Ben Skeggs16d4c032013-02-20 18:56:33 +10001151 }
1152
1153 nv_mask(priv, hreg, 0x0000000f, hval);
Emil Velikov378f2bc2013-07-02 14:44:12 +01001154 nv_mask(priv, oreg, mask, oval);
Ben Skeggs16d4c032013-02-20 18:56:33 +10001155 }
Ben Skeggs186ecad2012-11-09 12:09:48 +10001156}
1157
1158/* If programming a TMDS output on a SOR that can also be configured for
1159 * DisplayPort, make sure NV50_SOR_DP_CTRL_ENABLE is forced off.
1160 *
1161 * It looks like the VBIOS TMDS scripts make an attempt at this, however,
1162 * the VBIOS scripts on at least one board I have only switch it off on
1163 * link 0, causing a blank display if the output has previously been
1164 * programmed for DisplayPort.
1165 */
1166static void
Ben Skeggs16d4c032013-02-20 18:56:33 +10001167nv50_disp_intr_unk40_0_tmds(struct nv50_disp_priv *priv, struct dcb_output *outp)
Ben Skeggs186ecad2012-11-09 12:09:48 +10001168{
1169 struct nouveau_bios *bios = nouveau_bios(priv);
1170 const int link = !(outp->sorconf.link & 1);
1171 const int or = ffs(outp->or) - 1;
1172 const u32 loff = (or * 0x800) + (link * 0x80);
1173 const u16 mask = (outp->sorconf.link << 6) | outp->or;
1174 u8 ver, hdr;
1175
1176 if (dcb_outp_match(bios, DCB_OUTPUT_DP, mask, &ver, &hdr, outp))
1177 nv_mask(priv, 0x61c10c + loff, 0x00000001, 0x00000000);
1178}
1179
1180static void
Ben Skeggs16d4c032013-02-20 18:56:33 +10001181nv50_disp_intr_unk40_0(struct nv50_disp_priv *priv, int head)
Ben Skeggs186ecad2012-11-09 12:09:48 +10001182{
Ben Skeggs16d4c032013-02-20 18:56:33 +10001183 struct dcb_output outp;
1184 u32 pclk = nv_rd32(priv, 0x610ad0 + (head * 0x540)) & 0x3fffff;
1185 if (exec_clkcmp(priv, head, 1, pclk, &outp) != ~0) {
1186 if (outp.location == 0 && outp.type == DCB_OUTPUT_TMDS)
1187 nv50_disp_intr_unk40_0_tmds(priv, &outp);
1188 else
1189 if (outp.location == 1 && outp.type == DCB_OUTPUT_DP) {
1190 u32 soff = (ffs(outp.or) - 1) * 0x08;
1191 u32 ctrl = nv_rd32(priv, 0x610b84 + soff);
1192 u32 datarate;
Ben Skeggs476e84e2013-02-11 09:24:23 +10001193
Ben Skeggs16d4c032013-02-20 18:56:33 +10001194 switch ((ctrl & 0x000f0000) >> 16) {
1195 case 6: datarate = pclk * 30 / 8; break;
1196 case 5: datarate = pclk * 24 / 8; break;
1197 case 2:
1198 default:
1199 datarate = pclk * 18 / 8;
1200 break;
Ben Skeggs476e84e2013-02-11 09:24:23 +10001201 }
Ben Skeggs16d4c032013-02-20 18:56:33 +10001202
1203 nouveau_dp_train(&priv->base, priv->pior.dp,
1204 &outp, head, datarate);
Ben Skeggs476e84e2013-02-11 09:24:23 +10001205 }
Ben Skeggs186ecad2012-11-09 12:09:48 +10001206 }
Ben Skeggs186ecad2012-11-09 12:09:48 +10001207}
1208
Ben Skeggs5cc027f2013-02-18 17:50:51 -05001209void
1210nv50_disp_intr_supervisor(struct work_struct *work)
Ben Skeggs186ecad2012-11-09 12:09:48 +10001211{
Ben Skeggs5cc027f2013-02-18 17:50:51 -05001212 struct nv50_disp_priv *priv =
1213 container_of(work, struct nv50_disp_priv, supervisor);
Ben Skeggs186ecad2012-11-09 12:09:48 +10001214 u32 super = nv_rd32(priv, 0x610030);
Ben Skeggs16d4c032013-02-20 18:56:33 +10001215 int head;
Ben Skeggs186ecad2012-11-09 12:09:48 +10001216
Ben Skeggs5cc027f2013-02-18 17:50:51 -05001217 nv_debug(priv, "supervisor 0x%08x 0x%08x\n", priv->super, super);
Ben Skeggs186ecad2012-11-09 12:09:48 +10001218
Ben Skeggs16d4c032013-02-20 18:56:33 +10001219 if (priv->super & 0x00000010) {
1220 for (head = 0; head < priv->head.nr; head++) {
1221 if (!(super & (0x00000020 << head)))
1222 continue;
1223 if (!(super & (0x00000080 << head)))
1224 continue;
1225 nv50_disp_intr_unk10_0(priv, head);
1226 }
1227 } else
1228 if (priv->super & 0x00000020) {
1229 for (head = 0; head < priv->head.nr; head++) {
1230 if (!(super & (0x00000080 << head)))
1231 continue;
1232 nv50_disp_intr_unk20_0(priv, head);
1233 }
1234 for (head = 0; head < priv->head.nr; head++) {
1235 if (!(super & (0x00000200 << head)))
1236 continue;
1237 nv50_disp_intr_unk20_1(priv, head);
1238 }
1239 for (head = 0; head < priv->head.nr; head++) {
1240 if (!(super & (0x00000080 << head)))
1241 continue;
1242 nv50_disp_intr_unk20_2(priv, head);
1243 }
1244 } else
1245 if (priv->super & 0x00000040) {
1246 for (head = 0; head < priv->head.nr; head++) {
1247 if (!(super & (0x00000080 << head)))
1248 continue;
1249 nv50_disp_intr_unk40_0(priv, head);
1250 }
1251 }
1252
1253 nv_wr32(priv, 0x610030, 0x80000000);
Ben Skeggs186ecad2012-11-09 12:09:48 +10001254}
1255
Ben Skeggs70cabe42012-08-14 10:04:04 +10001256void
Ben Skeggsebb945a2012-07-20 08:17:34 +10001257nv50_disp_intr(struct nouveau_subdev *subdev)
1258{
1259 struct nv50_disp_priv *priv = (void *)subdev;
Ben Skeggs186ecad2012-11-09 12:09:48 +10001260 u32 intr0 = nv_rd32(priv, 0x610020);
1261 u32 intr1 = nv_rd32(priv, 0x610024);
Ben Skeggsebb945a2012-07-20 08:17:34 +10001262
Ben Skeggs186ecad2012-11-09 12:09:48 +10001263 if (intr0 & 0x001f0000) {
1264 nv50_disp_intr_error(priv);
1265 intr0 &= ~0x001f0000;
1266 }
1267
1268 if (intr1 & 0x00000004) {
Ben Skeggs1d7c71a2013-01-31 09:23:34 +10001269 nouveau_event_trigger(priv->base.vblank, 0);
Ben Skeggsebb945a2012-07-20 08:17:34 +10001270 nv_wr32(priv, 0x610024, 0x00000004);
Ben Skeggs186ecad2012-11-09 12:09:48 +10001271 intr1 &= ~0x00000004;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001272 }
1273
Ben Skeggs186ecad2012-11-09 12:09:48 +10001274 if (intr1 & 0x00000008) {
Ben Skeggs1d7c71a2013-01-31 09:23:34 +10001275 nouveau_event_trigger(priv->base.vblank, 1);
Ben Skeggsebb945a2012-07-20 08:17:34 +10001276 nv_wr32(priv, 0x610024, 0x00000008);
Ben Skeggs186ecad2012-11-09 12:09:48 +10001277 intr1 &= ~0x00000008;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001278 }
1279
Ben Skeggs186ecad2012-11-09 12:09:48 +10001280 if (intr1 & 0x00000070) {
Ben Skeggs5cc027f2013-02-18 17:50:51 -05001281 priv->super = (intr1 & 0x00000070);
1282 schedule_work(&priv->supervisor);
1283 nv_wr32(priv, 0x610024, priv->super);
Ben Skeggs186ecad2012-11-09 12:09:48 +10001284 intr1 &= ~0x00000070;
1285 }
Ben Skeggsebb945a2012-07-20 08:17:34 +10001286}
1287
1288static int
1289nv50_disp_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
Ben Skeggs370c00f2012-08-14 14:11:49 +10001290 struct nouveau_oclass *oclass, void *data, u32 size,
1291 struct nouveau_object **pobject)
Ben Skeggsebb945a2012-07-20 08:17:34 +10001292{
1293 struct nv50_disp_priv *priv;
1294 int ret;
1295
Ben Skeggs1d7c71a2013-01-31 09:23:34 +10001296 ret = nouveau_disp_create(parent, engine, oclass, 2, "PDISP",
Ben Skeggsebb945a2012-07-20 08:17:34 +10001297 "display", &priv);
1298 *pobject = nv_object(priv);
1299 if (ret)
1300 return ret;
1301
Ben Skeggs70cabe42012-08-14 10:04:04 +10001302 nv_engine(priv)->sclass = nv50_disp_base_oclass;
1303 nv_engine(priv)->cclass = &nv50_disp_cclass;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001304 nv_subdev(priv)->intr = nv50_disp_intr;
Ben Skeggs5cc027f2013-02-18 17:50:51 -05001305 INIT_WORK(&priv->supervisor, nv50_disp_intr_supervisor);
Ben Skeggs70cabe42012-08-14 10:04:04 +10001306 priv->sclass = nv50_disp_sclass;
1307 priv->head.nr = 2;
1308 priv->dac.nr = 3;
1309 priv->sor.nr = 2;
Ben Skeggsa2bc2832013-02-11 09:11:08 +10001310 priv->pior.nr = 3;
Ben Skeggsef22c8b2012-11-09 09:32:56 +10001311 priv->dac.power = nv50_dac_power;
Ben Skeggs7ebb38b2012-11-09 09:38:06 +10001312 priv->dac.sense = nv50_dac_sense;
Ben Skeggsef22c8b2012-11-09 09:32:56 +10001313 priv->sor.power = nv50_sor_power;
Ben Skeggsa2bc2832013-02-11 09:11:08 +10001314 priv->pior.power = nv50_pior_power;
1315 priv->pior.dp = &nv50_pior_dp_func;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001316 return 0;
1317}
1318
1319struct nouveau_oclass
1320nv50_disp_oclass = {
1321 .handle = NV_ENGINE(DISP, 0x50),
1322 .ofuncs = &(struct nouveau_ofuncs) {
1323 .ctor = nv50_disp_ctor,
1324 .dtor = _nouveau_disp_dtor,
1325 .init = _nouveau_disp_init,
1326 .fini = _nouveau_disp_fini,
1327 },
1328};