1 /*
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 #include "nv50.h"
25 #include "rootnv50.h"
26 
27 #include <subdev/bios.h>
28 #include <subdev/bios/disp.h>
29 #include <subdev/bios/init.h>
30 #include <subdev/bios/pll.h>
31 #include <subdev/devinit.h>
32 
33 void
34 gf119_disp_vblank_init(struct nv50_disp *disp, int head)
35 {
36 	struct nvkm_device *device = disp->base.engine.subdev.device;
37 	nvkm_mask(device, 0x6100c0 + (head * 0x800), 0x00000001, 0x00000001);
38 }
39 
40 void
41 gf119_disp_vblank_fini(struct nv50_disp *disp, int head)
42 {
43 	struct nvkm_device *device = disp->base.engine.subdev.device;
44 	nvkm_mask(device, 0x6100c0 + (head * 0x800), 0x00000001, 0x00000000);
45 }
46 
47 static struct nvkm_output *
48 exec_lookup(struct nv50_disp *disp, int head, int or, u32 ctrl,
49 	    u32 *data, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
50 	    struct nvbios_outp *info)
51 {
52 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
53 	struct nvkm_bios *bios = subdev->device->bios;
54 	struct nvkm_output *outp;
55 	u16 mask, type;
56 
57 	if (or < 4) {
58 		type = DCB_OUTPUT_ANALOG;
59 		mask = 0;
60 	} else {
61 		or -= 4;
62 		switch (ctrl & 0x00000f00) {
63 		case 0x00000000: type = DCB_OUTPUT_LVDS; mask = 1; break;
64 		case 0x00000100: type = DCB_OUTPUT_TMDS; mask = 1; break;
65 		case 0x00000200: type = DCB_OUTPUT_TMDS; mask = 2; break;
66 		case 0x00000500: type = DCB_OUTPUT_TMDS; mask = 3; break;
67 		case 0x00000800: type = DCB_OUTPUT_DP; mask = 1; break;
68 		case 0x00000900: type = DCB_OUTPUT_DP; mask = 2; break;
69 		default:
70 			nvkm_error(subdev, "unknown SOR mc %08x\n", ctrl);
71 			return NULL;
72 		}
73 	}
74 
75 	mask  = 0x00c0 & (mask << 6);
76 	mask |= 0x0001 << or;
77 	mask |= 0x0100 << head;
78 
79 	list_for_each_entry(outp, &disp->base.outp, head) {
80 		if ((outp->info.hasht & 0xff) == type &&
81 		    (outp->info.hashm & mask) == mask) {
82 			*data = nvbios_outp_match(bios, outp->info.hasht, mask,
83 						  ver, hdr, cnt, len, info);
84 			if (!*data)
85 				return NULL;
86 			return outp;
87 		}
88 	}
89 
90 	return NULL;
91 }
92 
93 static struct nvkm_output *
94 exec_script(struct nv50_disp *disp, int head, int id)
95 {
96 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
97 	struct nvkm_device *device = subdev->device;
98 	struct nvkm_bios *bios = device->bios;
99 	struct nvkm_output *outp;
100 	struct nvbios_outp info;
101 	u8  ver, hdr, cnt, len;
102 	u32 data, ctrl = 0;
103 	int or;
104 
105 	for (or = 0; !(ctrl & (1 << head)) && or < 8; or++) {
106 		ctrl = nvkm_rd32(device, 0x640180 + (or * 0x20));
107 		if (ctrl & (1 << head))
108 			break;
109 	}
110 
111 	if (or == 8)
112 		return NULL;
113 
114 	outp = exec_lookup(disp, head, or, ctrl, &data, &ver, &hdr, &cnt, &len, &info);
115 	if (outp) {
116 		struct nvbios_init init = {
117 			.subdev = subdev,
118 			.bios = bios,
119 			.offset = info.script[id],
120 			.outp = &outp->info,
121 			.crtc = head,
122 			.execute = 1,
123 		};
124 
125 		nvbios_exec(&init);
126 	}
127 
128 	return outp;
129 }
130 
131 static struct nvkm_output *
132 exec_clkcmp(struct nv50_disp *disp, int head, int id, u32 pclk, u32 *conf)
133 {
134 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
135 	struct nvkm_device *device = subdev->device;
136 	struct nvkm_bios *bios = device->bios;
137 	struct nvkm_output *outp;
138 	struct nvbios_outp info1;
139 	struct nvbios_ocfg info2;
140 	u8  ver, hdr, cnt, len;
141 	u32 data, ctrl = 0;
142 	int or;
143 
144 	for (or = 0; !(ctrl & (1 << head)) && or < 8; or++) {
145 		ctrl = nvkm_rd32(device, 0x660180 + (or * 0x20));
146 		if (ctrl & (1 << head))
147 			break;
148 	}
149 
150 	if (or == 8)
151 		return NULL;
152 
153 	outp = exec_lookup(disp, head, or, ctrl, &data, &ver, &hdr, &cnt, &len, &info1);
154 	if (!outp)
155 		return NULL;
156 
157 	*conf = (ctrl & 0x00000f00) >> 8;
158 	switch (outp->info.type) {
159 	case DCB_OUTPUT_TMDS:
160 		if (*conf == 5)
161 			*conf |= 0x0100;
162 		break;
163 	case DCB_OUTPUT_LVDS:
164 		*conf |= disp->sor.lvdsconf;
165 		break;
166 	default:
167 		break;
168 	}
169 
170 	data = nvbios_ocfg_match(bios, data, *conf & 0xff, *conf >> 8,
171 				 &ver, &hdr, &cnt, &len, &info2);
172 	if (data && id < 0xff) {
173 		data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
174 		if (data) {
175 			struct nvbios_init init = {
176 				.subdev = subdev,
177 				.bios = bios,
178 				.offset = data,
179 				.outp = &outp->info,
180 				.crtc = head,
181 				.execute = 1,
182 			};
183 
184 			nvbios_exec(&init);
185 		}
186 	}
187 
188 	return outp;
189 }
190 
191 static void
192 gf119_disp_intr_unk1_0(struct nv50_disp *disp, int head)
193 {
194 	exec_script(disp, head, 1);
195 }
196 
197 static void
198 gf119_disp_intr_unk2_0(struct nv50_disp *disp, int head)
199 {
200 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
201 	struct nvkm_output *outp = exec_script(disp, head, 2);
202 
203 	/* see note in nv50_disp_intr_unk20_0() */
204 	if (outp && outp->info.type == DCB_OUTPUT_DP) {
205 		struct nvkm_output_dp *outpdp = nvkm_output_dp(outp);
206 		if (!outpdp->lt.mst) {
207 			struct nvbios_init init = {
208 				.subdev = subdev,
209 				.bios = subdev->device->bios,
210 				.outp = &outp->info,
211 				.crtc = head,
212 				.offset = outpdp->info.script[4],
213 				.execute = 1,
214 			};
215 
216 			nvkm_notify_put(&outpdp->irq);
217 			nvbios_exec(&init);
218 			atomic_set(&outpdp->lt.done, 0);
219 		}
220 	}
221 }
222 
223 static void
224 gf119_disp_intr_unk2_1(struct nv50_disp *disp, int head)
225 {
226 	struct nvkm_device *device = disp->base.engine.subdev.device;
227 	struct nvkm_devinit *devinit = device->devinit;
228 	u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
229 	if (pclk)
230 		nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head, pclk);
231 	nvkm_wr32(device, 0x612200 + (head * 0x800), 0x00000000);
232 }
233 
234 static void
235 gf119_disp_intr_unk2_2_tu(struct nv50_disp *disp, int head,
236 			  struct dcb_output *outp)
237 {
238 	struct nvkm_device *device = disp->base.engine.subdev.device;
239 	const int or = ffs(outp->or) - 1;
240 	const u32 ctrl = nvkm_rd32(device, 0x660200 + (or   * 0x020));
241 	const u32 conf = nvkm_rd32(device, 0x660404 + (head * 0x300));
242 	const s32 vactive = nvkm_rd32(device, 0x660414 + (head * 0x300)) & 0xffff;
243 	const s32 vblanke = nvkm_rd32(device, 0x66041c + (head * 0x300)) & 0xffff;
244 	const s32 vblanks = nvkm_rd32(device, 0x660420 + (head * 0x300)) & 0xffff;
245 	const u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
246 	const u32 link = ((ctrl & 0xf00) == 0x800) ? 0 : 1;
247 	const u32 hoff = (head * 0x800);
248 	const u32 soff = (  or * 0x800);
249 	const u32 loff = (link * 0x080) + soff;
250 	const u32 symbol = 100000;
251 	const u32 TU = 64;
252 	u32 dpctrl = nvkm_rd32(device, 0x61c10c + loff);
253 	u32 clksor = nvkm_rd32(device, 0x612300 + soff);
254 	u32 datarate, link_nr, link_bw, bits;
255 	u64 ratio, value;
256 
257 	link_nr  = hweight32(dpctrl & 0x000f0000);
258 	link_bw  = (clksor & 0x007c0000) >> 18;
259 	link_bw *= 27000;
260 
261 	/* symbols/hblank - algorithm taken from comments in tegra driver */
262 	value = vblanke + vactive - vblanks - 7;
263 	value = value * link_bw;
264 	do_div(value, pclk);
265 	value = value - (3 * !!(dpctrl & 0x00004000)) - (12 / link_nr);
266 	nvkm_mask(device, 0x616620 + hoff, 0x0000ffff, value);
267 
268 	/* symbols/vblank - algorithm taken from comments in tegra driver */
269 	value = vblanks - vblanke - 25;
270 	value = value * link_bw;
271 	do_div(value, pclk);
272 	value = value - ((36 / link_nr) + 3) - 1;
273 	nvkm_mask(device, 0x616624 + hoff, 0x00ffffff, value);
274 
275 	/* watermark */
276 	if      ((conf & 0x3c0) == 0x180) bits = 30;
277 	else if ((conf & 0x3c0) == 0x140) bits = 24;
278 	else                              bits = 18;
279 	datarate = (pclk * bits) / 8;
280 
281 	ratio  = datarate;
282 	ratio *= symbol;
283 	do_div(ratio, link_nr * link_bw);
284 
285 	value  = (symbol - ratio) * TU;
286 	value *= ratio;
287 	do_div(value, symbol);
288 	do_div(value, symbol);
289 
290 	value += 5;
291 	value |= 0x08000000;
292 
293 	nvkm_wr32(device, 0x616610 + hoff, value);
294 }
295 
296 static void
297 gf119_disp_intr_unk2_2(struct nv50_disp *disp, int head)
298 {
299 	struct nvkm_device *device = disp->base.engine.subdev.device;
300 	struct nvkm_output *outp;
301 	u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
302 	u32 conf, addr, data;
303 
304 	outp = exec_clkcmp(disp, head, 0xff, pclk, &conf);
305 	if (!outp)
306 		return;
307 
308 	/* see note in nv50_disp_intr_unk20_2() */
309 	if (outp->info.type == DCB_OUTPUT_DP) {
310 		u32 sync = nvkm_rd32(device, 0x660404 + (head * 0x300));
311 		switch ((sync & 0x000003c0) >> 6) {
312 		case 6: pclk = pclk * 30; break;
313 		case 5: pclk = pclk * 24; break;
314 		case 2:
315 		default:
316 			pclk = pclk * 18;
317 			break;
318 		}
319 
320 		if (nvkm_output_dp_train(outp, pclk))
321 			OUTP_ERR(outp, "link not trained before attach");
322 	} else {
323 		if (disp->func->sor.magic)
324 			disp->func->sor.magic(outp);
325 	}
326 
327 	exec_clkcmp(disp, head, 0, pclk, &conf);
328 
329 	if (outp->info.type == DCB_OUTPUT_ANALOG) {
330 		addr = 0x612280 + (ffs(outp->info.or) - 1) * 0x800;
331 		data = 0x00000000;
332 	} else {
333 		addr = 0x612300 + (ffs(outp->info.or) - 1) * 0x800;
334 		data = (conf & 0x0100) ? 0x00000101 : 0x00000000;
335 		switch (outp->info.type) {
336 		case DCB_OUTPUT_TMDS:
337 			nvkm_mask(device, addr, 0x007c0000, 0x00280000);
338 			break;
339 		case DCB_OUTPUT_DP:
340 			gf119_disp_intr_unk2_2_tu(disp, head, &outp->info);
341 			break;
342 		default:
343 			break;
344 		}
345 	}
346 
347 	nvkm_mask(device, addr, 0x00000707, data);
348 }
349 
350 static void
351 gf119_disp_intr_unk4_0(struct nv50_disp *disp, int head)
352 {
353 	struct nvkm_device *device = disp->base.engine.subdev.device;
354 	u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
355 	u32 conf;
356 
357 	exec_clkcmp(disp, head, 1, pclk, &conf);
358 }
359 
360 void
361 gf119_disp_intr_supervisor(struct work_struct *work)
362 {
363 	struct nv50_disp *disp =
364 		container_of(work, struct nv50_disp, supervisor);
365 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
366 	struct nvkm_device *device = subdev->device;
367 	u32 mask[4];
368 	int head;
369 
370 	nvkm_debug(subdev, "supervisor %d\n", ffs(disp->super));
371 	for (head = 0; head < disp->base.head.nr; head++) {
372 		mask[head] = nvkm_rd32(device, 0x6101d4 + (head * 0x800));
373 		nvkm_debug(subdev, "head %d: %08x\n", head, mask[head]);
374 	}
375 
376 	if (disp->super & 0x00000001) {
377 		nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
378 		for (head = 0; head < disp->base.head.nr; head++) {
379 			if (!(mask[head] & 0x00001000))
380 				continue;
381 			nvkm_debug(subdev, "supervisor 1.0 - head %d\n", head);
382 			gf119_disp_intr_unk1_0(disp, head);
383 		}
384 	} else
385 	if (disp->super & 0x00000002) {
386 		for (head = 0; head < disp->base.head.nr; head++) {
387 			if (!(mask[head] & 0x00001000))
388 				continue;
389 			nvkm_debug(subdev, "supervisor 2.0 - head %d\n", head);
390 			gf119_disp_intr_unk2_0(disp, head);
391 		}
392 		for (head = 0; head < disp->base.head.nr; head++) {
393 			if (!(mask[head] & 0x00010000))
394 				continue;
395 			nvkm_debug(subdev, "supervisor 2.1 - head %d\n", head);
396 			gf119_disp_intr_unk2_1(disp, head);
397 		}
398 		for (head = 0; head < disp->base.head.nr; head++) {
399 			if (!(mask[head] & 0x00001000))
400 				continue;
401 			nvkm_debug(subdev, "supervisor 2.2 - head %d\n", head);
402 			gf119_disp_intr_unk2_2(disp, head);
403 		}
404 	} else
405 	if (disp->super & 0x00000004) {
406 		for (head = 0; head < disp->base.head.nr; head++) {
407 			if (!(mask[head] & 0x00001000))
408 				continue;
409 			nvkm_debug(subdev, "supervisor 3.0 - head %d\n", head);
410 			gf119_disp_intr_unk4_0(disp, head);
411 		}
412 	}
413 
414 	for (head = 0; head < disp->base.head.nr; head++)
415 		nvkm_wr32(device, 0x6101d4 + (head * 0x800), 0x00000000);
416 	nvkm_wr32(device, 0x6101d0, 0x80000000);
417 }
418 
419 void
420 gf119_disp_intr_error(struct nv50_disp *disp, int chid)
421 {
422 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
423 	struct nvkm_device *device = subdev->device;
424 	u32 mthd = nvkm_rd32(device, 0x6101f0 + (chid * 12));
425 	u32 data = nvkm_rd32(device, 0x6101f4 + (chid * 12));
426 	u32 unkn = nvkm_rd32(device, 0x6101f8 + (chid * 12));
427 
428 	nvkm_error(subdev, "chid %d mthd %04x data %08x %08x %08x\n",
429 		   chid, (mthd & 0x0000ffc), data, mthd, unkn);
430 
431 	if (chid < ARRAY_SIZE(disp->chan)) {
432 		switch (mthd & 0xffc) {
433 		case 0x0080:
434 			nv50_disp_chan_mthd(disp->chan[chid], NV_DBG_ERROR);
435 			break;
436 		default:
437 			break;
438 		}
439 	}
440 
441 	nvkm_wr32(device, 0x61009c, (1 << chid));
442 	nvkm_wr32(device, 0x6101f0 + (chid * 12), 0x90000000);
443 }
444 
445 void
446 gf119_disp_intr(struct nv50_disp *disp)
447 {
448 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
449 	struct nvkm_device *device = subdev->device;
450 	u32 intr = nvkm_rd32(device, 0x610088);
451 	int i;
452 
453 	if (intr & 0x00000001) {
454 		u32 stat = nvkm_rd32(device, 0x61008c);
455 		while (stat) {
456 			int chid = __ffs(stat); stat &= ~(1 << chid);
457 			nv50_disp_chan_uevent_send(disp, chid);
458 			nvkm_wr32(device, 0x61008c, 1 << chid);
459 		}
460 		intr &= ~0x00000001;
461 	}
462 
463 	if (intr & 0x00000002) {
464 		u32 stat = nvkm_rd32(device, 0x61009c);
465 		int chid = ffs(stat) - 1;
466 		if (chid >= 0)
467 			disp->func->intr_error(disp, chid);
468 		intr &= ~0x00000002;
469 	}
470 
471 	if (intr & 0x00100000) {
472 		u32 stat = nvkm_rd32(device, 0x6100ac);
473 		if (stat & 0x00000007) {
474 			disp->super = (stat & 0x00000007);
475 			schedule_work(&disp->supervisor);
476 			nvkm_wr32(device, 0x6100ac, disp->super);
477 			stat &= ~0x00000007;
478 		}
479 
480 		if (stat) {
481 			nvkm_warn(subdev, "intr24 %08x\n", stat);
482 			nvkm_wr32(device, 0x6100ac, stat);
483 		}
484 
485 		intr &= ~0x00100000;
486 	}
487 
488 	for (i = 0; i < disp->base.head.nr; i++) {
489 		u32 mask = 0x01000000 << i;
490 		if (mask & intr) {
491 			u32 stat = nvkm_rd32(device, 0x6100bc + (i * 0x800));
492 			if (stat & 0x00000001)
493 				nvkm_disp_vblank(&disp->base, i);
494 			nvkm_mask(device, 0x6100bc + (i * 0x800), 0, 0);
495 			nvkm_rd32(device, 0x6100c0 + (i * 0x800));
496 		}
497 	}
498 }
499 
500 int
501 gf119_disp_new_(const struct nv50_disp_func *func, struct nvkm_device *device,
502 		int index, struct nvkm_disp **pdisp)
503 {
504 	u32 heads = nvkm_rd32(device, 0x022448);
505 	return nv50_disp_new_(func, device, index, heads, pdisp);
506 }
507 
508 static const struct nv50_disp_func
509 gf119_disp = {
510 	.intr = gf119_disp_intr,
511 	.intr_error = gf119_disp_intr_error,
512 	.uevent = &gf119_disp_chan_uevent,
513 	.super = gf119_disp_intr_supervisor,
514 	.root = &gf119_disp_root_oclass,
515 	.head.vblank_init = gf119_disp_vblank_init,
516 	.head.vblank_fini = gf119_disp_vblank_fini,
517 	.head.scanoutpos = gf119_disp_root_scanoutpos,
518 	.outp.internal.crt = nv50_dac_output_new,
519 	.outp.internal.tmds = nv50_sor_output_new,
520 	.outp.internal.lvds = nv50_sor_output_new,
521 	.outp.internal.dp = gf119_sor_dp_new,
522 	.dac.nr = 3,
523 	.dac.power = nv50_dac_power,
524 	.dac.sense = nv50_dac_sense,
525 	.sor.nr = 4,
526 	.sor.power = nv50_sor_power,
527 	.sor.hda_eld = gf119_hda_eld,
528 	.sor.hdmi = gf119_hdmi_ctrl,
529 };
530 
531 int
532 gf119_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
533 {
534 	return gf119_disp_new_(&gf119_disp, device, index, pdisp);
535 }
536