1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Intel Corp. 2007.
4  * All Rights Reserved.
5  *
6  * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
7  * develop this driver.
8  *
9  * This file is part of the Vermilion Range fb driver.
10  *
11  * Authors:
12  *   Thomas Hellström <thomas-at-tungstengraphics-dot-com>
13  *   Michel Dänzer <michel-at-tungstengraphics-dot-com>
14  *   Alan Hourihane <alanh-at-tungstengraphics-dot-com>
15  */
16 
17 #include <linux/aperture.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/string.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/fb.h>
26 #include <linux/pci.h>
27 #include <asm/set_memory.h>
28 #include <asm/tlbflush.h>
29 #include <linux/mmzone.h>
30 
31 /* #define VERMILION_DEBUG */
32 
33 #include "vermilion.h"
34 
35 #define MODULE_NAME "vmlfb"
36 
37 #define VML_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
38 
39 static struct mutex vml_mutex;
40 static struct list_head global_no_mode;
41 static struct list_head global_has_mode;
42 static struct fb_ops vmlfb_ops;
43 static struct vml_sys *subsys = NULL;
44 static char *vml_default_mode = "1024x768@60";
45 static const struct fb_videomode defaultmode = {
46 	NULL, 60, 1024, 768, 12896, 144, 24, 29, 3, 136, 6,
47 	0, FB_VMODE_NONINTERLACED
48 };
49 
50 static u32 vml_mem_requested = (10 * 1024 * 1024);
51 static u32 vml_mem_contig = (4 * 1024 * 1024);
52 static u32 vml_mem_min = (4 * 1024 * 1024);
53 
54 static u32 vml_clocks[] = {
55 	6750,
56 	13500,
57 	27000,
58 	29700,
59 	37125,
60 	54000,
61 	59400,
62 	74250,
63 	120000,
64 	148500
65 };
66 
67 static u32 vml_num_clocks = ARRAY_SIZE(vml_clocks);
68 
69 /*
70  * Allocate a contiguous vram area and make its linear kernel map
71  * uncached.
72  */
73 
74 static int vmlfb_alloc_vram_area(struct vram_area *va, unsigned max_order,
75 				 unsigned min_order)
76 {
77 	gfp_t flags;
78 	unsigned long i;
79 
80 	max_order++;
81 	do {
82 		/*
83 		 * Really try hard to get the needed memory.
84 		 * We need memory below the first 32MB, so we
85 		 * add the __GFP_DMA flag that guarantees that we are
86 		 * below the first 16MB.
87 		 */
88 
89 		flags = __GFP_DMA | __GFP_HIGH | __GFP_KSWAPD_RECLAIM;
90 		va->logical =
91 			 __get_free_pages(flags, --max_order);
92 	} while (va->logical == 0 && max_order > min_order);
93 
94 	if (!va->logical)
95 		return -ENOMEM;
96 
97 	va->phys = virt_to_phys((void *)va->logical);
98 	va->size = PAGE_SIZE << max_order;
99 	va->order = max_order;
100 
101 	/*
102 	 * It seems like __get_free_pages only ups the usage count
103 	 * of the first page. This doesn't work with fault mapping, so
104 	 * up the usage count once more (XXX: should use split_page or
105 	 * compound page).
106 	 */
107 
108 	memset((void *)va->logical, 0x00, va->size);
109 	for (i = va->logical; i < va->logical + va->size; i += PAGE_SIZE) {
110 		get_page(virt_to_page(i));
111 	}
112 
113 	/*
114 	 * Change caching policy of the linear kernel map to avoid
115 	 * mapping type conflicts with user-space mappings.
116 	 */
117 	set_pages_uc(virt_to_page(va->logical), va->size >> PAGE_SHIFT);
118 
119 	printk(KERN_DEBUG MODULE_NAME
120 	       ": Allocated %ld bytes vram area at 0x%08lx\n",
121 	       va->size, va->phys);
122 
123 	return 0;
124 }
125 
126 /*
127  * Free a contiguous vram area and reset its linear kernel map
128  * mapping type.
129  */
130 
131 static void vmlfb_free_vram_area(struct vram_area *va)
132 {
133 	unsigned long j;
134 
135 	if (va->logical) {
136 
137 		/*
138 		 * Reset the linear kernel map caching policy.
139 		 */
140 
141 		set_pages_wb(virt_to_page(va->logical),
142 				 va->size >> PAGE_SHIFT);
143 
144 		/*
145 		 * Decrease the usage count on the pages we've used
146 		 * to compensate for upping when allocating.
147 		 */
148 
149 		for (j = va->logical; j < va->logical + va->size;
150 		     j += PAGE_SIZE) {
151 			(void)put_page_testzero(virt_to_page(j));
152 		}
153 
154 		printk(KERN_DEBUG MODULE_NAME
155 		       ": Freeing %ld bytes vram area at 0x%08lx\n",
156 		       va->size, va->phys);
157 		free_pages(va->logical, va->order);
158 
159 		va->logical = 0;
160 	}
161 }
162 
163 /*
164  * Free allocated vram.
165  */
166 
167 static void vmlfb_free_vram(struct vml_info *vinfo)
168 {
169 	int i;
170 
171 	for (i = 0; i < vinfo->num_areas; ++i) {
172 		vmlfb_free_vram_area(&vinfo->vram[i]);
173 	}
174 	vinfo->num_areas = 0;
175 }
176 
177 /*
178  * Allocate vram. Currently we try to allocate contiguous areas from the
179  * __GFP_DMA zone and puzzle them together. A better approach would be to
180  * allocate one contiguous area for scanout and use one-page allocations for
181  * offscreen areas. This requires user-space and GPU virtual mappings.
182  */
183 
184 static int vmlfb_alloc_vram(struct vml_info *vinfo,
185 			    size_t requested,
186 			    size_t min_total, size_t min_contig)
187 {
188 	int i, j;
189 	int order;
190 	int contiguous;
191 	int err;
192 	struct vram_area *va;
193 	struct vram_area *va2;
194 
195 	vinfo->num_areas = 0;
196 	for (i = 0; i < VML_VRAM_AREAS; ++i) {
197 		va = &vinfo->vram[i];
198 		order = 0;
199 
200 		while (requested > (PAGE_SIZE << order) && order < MAX_ORDER)
201 			order++;
202 
203 		err = vmlfb_alloc_vram_area(va, order, 0);
204 
205 		if (err)
206 			break;
207 
208 		if (i == 0) {
209 			vinfo->vram_start = va->phys;
210 			vinfo->vram_logical = (void __iomem *) va->logical;
211 			vinfo->vram_contig_size = va->size;
212 			vinfo->num_areas = 1;
213 		} else {
214 			contiguous = 0;
215 
216 			for (j = 0; j < i; ++j) {
217 				va2 = &vinfo->vram[j];
218 				if (va->phys + va->size == va2->phys ||
219 				    va2->phys + va2->size == va->phys) {
220 					contiguous = 1;
221 					break;
222 				}
223 			}
224 
225 			if (contiguous) {
226 				vinfo->num_areas++;
227 				if (va->phys < vinfo->vram_start) {
228 					vinfo->vram_start = va->phys;
229 					vinfo->vram_logical =
230 						(void __iomem *)va->logical;
231 				}
232 				vinfo->vram_contig_size += va->size;
233 			} else {
234 				vmlfb_free_vram_area(va);
235 				break;
236 			}
237 		}
238 
239 		if (requested < va->size)
240 			break;
241 		else
242 			requested -= va->size;
243 	}
244 
245 	if (vinfo->vram_contig_size > min_total &&
246 	    vinfo->vram_contig_size > min_contig) {
247 
248 		printk(KERN_DEBUG MODULE_NAME
249 		       ": Contiguous vram: %ld bytes at physical 0x%08lx.\n",
250 		       (unsigned long)vinfo->vram_contig_size,
251 		       (unsigned long)vinfo->vram_start);
252 
253 		return 0;
254 	}
255 
256 	printk(KERN_ERR MODULE_NAME
257 	       ": Could not allocate requested minimal amount of vram.\n");
258 
259 	vmlfb_free_vram(vinfo);
260 
261 	return -ENOMEM;
262 }
263 
264 /*
265  * Find the GPU to use with our display controller.
266  */
267 
268 static int vmlfb_get_gpu(struct vml_par *par)
269 {
270 	mutex_lock(&vml_mutex);
271 
272 	par->gpu = pci_get_device(PCI_VENDOR_ID_INTEL, VML_DEVICE_GPU, NULL);
273 
274 	if (!par->gpu) {
275 		mutex_unlock(&vml_mutex);
276 		return -ENODEV;
277 	}
278 
279 	mutex_unlock(&vml_mutex);
280 
281 	if (pci_enable_device(par->gpu) < 0)
282 		return -ENODEV;
283 
284 	return 0;
285 }
286 
287 /*
288  * Find a contiguous vram area that contains a given offset from vram start.
289  */
290 static int vmlfb_vram_offset(struct vml_info *vinfo, unsigned long offset)
291 {
292 	unsigned long aoffset;
293 	unsigned i;
294 
295 	for (i = 0; i < vinfo->num_areas; ++i) {
296 		aoffset = offset - (vinfo->vram[i].phys - vinfo->vram_start);
297 
298 		if (aoffset < vinfo->vram[i].size) {
299 			return 0;
300 		}
301 	}
302 
303 	return -EINVAL;
304 }
305 
306 /*
307  * Remap the MMIO register spaces of the VDC and the GPU.
308  */
309 
310 static int vmlfb_enable_mmio(struct vml_par *par)
311 {
312 	int err;
313 
314 	par->vdc_mem_base = pci_resource_start(par->vdc, 0);
315 	par->vdc_mem_size = pci_resource_len(par->vdc, 0);
316 	if (!request_mem_region(par->vdc_mem_base, par->vdc_mem_size, "vmlfb")) {
317 		printk(KERN_ERR MODULE_NAME
318 		       ": Could not claim display controller MMIO.\n");
319 		return -EBUSY;
320 	}
321 	par->vdc_mem = ioremap(par->vdc_mem_base, par->vdc_mem_size);
322 	if (par->vdc_mem == NULL) {
323 		printk(KERN_ERR MODULE_NAME
324 		       ": Could not map display controller MMIO.\n");
325 		err = -ENOMEM;
326 		goto out_err_0;
327 	}
328 
329 	par->gpu_mem_base = pci_resource_start(par->gpu, 0);
330 	par->gpu_mem_size = pci_resource_len(par->gpu, 0);
331 	if (!request_mem_region(par->gpu_mem_base, par->gpu_mem_size, "vmlfb")) {
332 		printk(KERN_ERR MODULE_NAME ": Could not claim GPU MMIO.\n");
333 		err = -EBUSY;
334 		goto out_err_1;
335 	}
336 	par->gpu_mem = ioremap(par->gpu_mem_base, par->gpu_mem_size);
337 	if (par->gpu_mem == NULL) {
338 		printk(KERN_ERR MODULE_NAME ": Could not map GPU MMIO.\n");
339 		err = -ENOMEM;
340 		goto out_err_2;
341 	}
342 
343 	return 0;
344 
345 out_err_2:
346 	release_mem_region(par->gpu_mem_base, par->gpu_mem_size);
347 out_err_1:
348 	iounmap(par->vdc_mem);
349 out_err_0:
350 	release_mem_region(par->vdc_mem_base, par->vdc_mem_size);
351 	return err;
352 }
353 
354 /*
355  * Unmap the VDC and GPU register spaces.
356  */
357 
358 static void vmlfb_disable_mmio(struct vml_par *par)
359 {
360 	iounmap(par->gpu_mem);
361 	release_mem_region(par->gpu_mem_base, par->gpu_mem_size);
362 	iounmap(par->vdc_mem);
363 	release_mem_region(par->vdc_mem_base, par->vdc_mem_size);
364 }
365 
366 /*
367  * Release and uninit the VDC and GPU.
368  */
369 
370 static void vmlfb_release_devices(struct vml_par *par)
371 {
372 	if (atomic_dec_and_test(&par->refcount)) {
373 		pci_disable_device(par->gpu);
374 		pci_disable_device(par->vdc);
375 	}
376 }
377 
378 /*
379  * Free up allocated resources for a device.
380  */
381 
382 static void vml_pci_remove(struct pci_dev *dev)
383 {
384 	struct fb_info *info;
385 	struct vml_info *vinfo;
386 	struct vml_par *par;
387 
388 	info = pci_get_drvdata(dev);
389 	if (info) {
390 		vinfo = container_of(info, struct vml_info, info);
391 		par = vinfo->par;
392 		mutex_lock(&vml_mutex);
393 		unregister_framebuffer(info);
394 		fb_dealloc_cmap(&info->cmap);
395 		vmlfb_free_vram(vinfo);
396 		vmlfb_disable_mmio(par);
397 		vmlfb_release_devices(par);
398 		kfree(vinfo);
399 		kfree(par);
400 		mutex_unlock(&vml_mutex);
401 	}
402 }
403 
404 static void vmlfb_set_pref_pixel_format(struct fb_var_screeninfo *var)
405 {
406 	switch (var->bits_per_pixel) {
407 	case 16:
408 		var->blue.offset = 0;
409 		var->blue.length = 5;
410 		var->green.offset = 5;
411 		var->green.length = 5;
412 		var->red.offset = 10;
413 		var->red.length = 5;
414 		var->transp.offset = 15;
415 		var->transp.length = 1;
416 		break;
417 	case 32:
418 		var->blue.offset = 0;
419 		var->blue.length = 8;
420 		var->green.offset = 8;
421 		var->green.length = 8;
422 		var->red.offset = 16;
423 		var->red.length = 8;
424 		var->transp.offset = 24;
425 		var->transp.length = 0;
426 		break;
427 	default:
428 		break;
429 	}
430 
431 	var->blue.msb_right = var->green.msb_right =
432 	    var->red.msb_right = var->transp.msb_right = 0;
433 }
434 
435 /*
436  * Device initialization.
437  * We initialize one vml_par struct per device and one vml_info
438  * struct per pipe. Currently we have only one pipe.
439  */
440 
441 static int vml_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
442 {
443 	struct vml_info *vinfo;
444 	struct fb_info *info;
445 	struct vml_par *par;
446 	int err;
447 
448 	err = aperture_remove_conflicting_pci_devices(dev, "vmlfb");
449 	if (err)
450 		return err;
451 
452 	par = kzalloc(sizeof(*par), GFP_KERNEL);
453 	if (par == NULL)
454 		return -ENOMEM;
455 
456 	vinfo = kzalloc(sizeof(*vinfo), GFP_KERNEL);
457 	if (vinfo == NULL) {
458 		err = -ENOMEM;
459 		goto out_err_0;
460 	}
461 
462 	vinfo->par = par;
463 	par->vdc = dev;
464 	atomic_set(&par->refcount, 1);
465 
466 	switch (id->device) {
467 	case VML_DEVICE_VDC:
468 		if ((err = vmlfb_get_gpu(par)))
469 			goto out_err_1;
470 		pci_set_drvdata(dev, &vinfo->info);
471 		break;
472 	default:
473 		err = -ENODEV;
474 		goto out_err_1;
475 	}
476 
477 	info = &vinfo->info;
478 	info->flags = FBINFO_DEFAULT | FBINFO_PARTIAL_PAN_OK;
479 
480 	err = vmlfb_enable_mmio(par);
481 	if (err)
482 		goto out_err_2;
483 
484 	err = vmlfb_alloc_vram(vinfo, vml_mem_requested,
485 			       vml_mem_contig, vml_mem_min);
486 	if (err)
487 		goto out_err_3;
488 
489 	strcpy(info->fix.id, "Vermilion Range");
490 	info->fix.mmio_start = 0;
491 	info->fix.mmio_len = 0;
492 	info->fix.smem_start = vinfo->vram_start;
493 	info->fix.smem_len = vinfo->vram_contig_size;
494 	info->fix.type = FB_TYPE_PACKED_PIXELS;
495 	info->fix.visual = FB_VISUAL_TRUECOLOR;
496 	info->fix.ypanstep = 1;
497 	info->fix.xpanstep = 1;
498 	info->fix.ywrapstep = 0;
499 	info->fix.accel = FB_ACCEL_NONE;
500 	info->screen_base = vinfo->vram_logical;
501 	info->pseudo_palette = vinfo->pseudo_palette;
502 	info->par = par;
503 	info->fbops = &vmlfb_ops;
504 	info->device = &dev->dev;
505 
506 	INIT_LIST_HEAD(&vinfo->head);
507 	vinfo->pipe_disabled = 1;
508 	vinfo->cur_blank_mode = FB_BLANK_UNBLANK;
509 
510 	info->var.grayscale = 0;
511 	info->var.bits_per_pixel = 16;
512 	vmlfb_set_pref_pixel_format(&info->var);
513 
514 	if (!fb_find_mode
515 	    (&info->var, info, vml_default_mode, NULL, 0, &defaultmode, 16)) {
516 		printk(KERN_ERR MODULE_NAME ": Could not find initial mode\n");
517 	}
518 
519 	if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {
520 		err = -ENOMEM;
521 		goto out_err_4;
522 	}
523 
524 	err = register_framebuffer(info);
525 	if (err) {
526 		printk(KERN_ERR MODULE_NAME ": Register framebuffer error.\n");
527 		goto out_err_5;
528 	}
529 
530 	printk("Initialized vmlfb\n");
531 
532 	return 0;
533 
534 out_err_5:
535 	fb_dealloc_cmap(&info->cmap);
536 out_err_4:
537 	vmlfb_free_vram(vinfo);
538 out_err_3:
539 	vmlfb_disable_mmio(par);
540 out_err_2:
541 	vmlfb_release_devices(par);
542 out_err_1:
543 	kfree(vinfo);
544 out_err_0:
545 	kfree(par);
546 	return err;
547 }
548 
549 static int vmlfb_open(struct fb_info *info, int user)
550 {
551 	/*
552 	 * Save registers here?
553 	 */
554 	return 0;
555 }
556 
557 static int vmlfb_release(struct fb_info *info, int user)
558 {
559 	/*
560 	 * Restore registers here.
561 	 */
562 
563 	return 0;
564 }
565 
566 static int vml_nearest_clock(int clock)
567 {
568 
569 	int i;
570 	int cur_index;
571 	int cur_diff;
572 	int diff;
573 
574 	cur_index = 0;
575 	cur_diff = clock - vml_clocks[0];
576 	cur_diff = (cur_diff < 0) ? -cur_diff : cur_diff;
577 	for (i = 1; i < vml_num_clocks; ++i) {
578 		diff = clock - vml_clocks[i];
579 		diff = (diff < 0) ? -diff : diff;
580 		if (diff < cur_diff) {
581 			cur_index = i;
582 			cur_diff = diff;
583 		}
584 	}
585 	return vml_clocks[cur_index];
586 }
587 
588 static int vmlfb_check_var_locked(struct fb_var_screeninfo *var,
589 				  struct vml_info *vinfo)
590 {
591 	u32 pitch;
592 	u64 mem;
593 	int nearest_clock;
594 	int clock;
595 	int clock_diff;
596 	struct fb_var_screeninfo v;
597 
598 	v = *var;
599 	clock = PICOS2KHZ(var->pixclock);
600 
601 	if (subsys && subsys->nearest_clock) {
602 		nearest_clock = subsys->nearest_clock(subsys, clock);
603 	} else {
604 		nearest_clock = vml_nearest_clock(clock);
605 	}
606 
607 	/*
608 	 * Accept a 20% diff.
609 	 */
610 
611 	clock_diff = nearest_clock - clock;
612 	clock_diff = (clock_diff < 0) ? -clock_diff : clock_diff;
613 	if (clock_diff > clock / 5) {
614 #if 0
615 		printk(KERN_DEBUG MODULE_NAME ": Diff failure. %d %d\n",clock_diff,clock);
616 #endif
617 		return -EINVAL;
618 	}
619 
620 	v.pixclock = KHZ2PICOS(nearest_clock);
621 
622 	if (var->xres > VML_MAX_XRES || var->yres > VML_MAX_YRES) {
623 		printk(KERN_DEBUG MODULE_NAME ": Resolution failure.\n");
624 		return -EINVAL;
625 	}
626 	if (var->xres_virtual > VML_MAX_XRES_VIRTUAL) {
627 		printk(KERN_DEBUG MODULE_NAME
628 		       ": Virtual resolution failure.\n");
629 		return -EINVAL;
630 	}
631 	switch (v.bits_per_pixel) {
632 	case 0 ... 16:
633 		v.bits_per_pixel = 16;
634 		break;
635 	case 17 ... 32:
636 		v.bits_per_pixel = 32;
637 		break;
638 	default:
639 		printk(KERN_DEBUG MODULE_NAME ": Invalid bpp: %d.\n",
640 		       var->bits_per_pixel);
641 		return -EINVAL;
642 	}
643 
644 	pitch = ALIGN((var->xres * var->bits_per_pixel) >> 3, 0x40);
645 	mem = (u64)pitch * var->yres_virtual;
646 	if (mem > vinfo->vram_contig_size) {
647 		return -ENOMEM;
648 	}
649 
650 	switch (v.bits_per_pixel) {
651 	case 16:
652 		if (var->blue.offset != 0 ||
653 		    var->blue.length != 5 ||
654 		    var->green.offset != 5 ||
655 		    var->green.length != 5 ||
656 		    var->red.offset != 10 ||
657 		    var->red.length != 5 ||
658 		    var->transp.offset != 15 || var->transp.length != 1) {
659 			vmlfb_set_pref_pixel_format(&v);
660 		}
661 		break;
662 	case 32:
663 		if (var->blue.offset != 0 ||
664 		    var->blue.length != 8 ||
665 		    var->green.offset != 8 ||
666 		    var->green.length != 8 ||
667 		    var->red.offset != 16 ||
668 		    var->red.length != 8 ||
669 		    (var->transp.length != 0 && var->transp.length != 8) ||
670 		    (var->transp.length == 8 && var->transp.offset != 24)) {
671 			vmlfb_set_pref_pixel_format(&v);
672 		}
673 		break;
674 	default:
675 		return -EINVAL;
676 	}
677 
678 	*var = v;
679 
680 	return 0;
681 }
682 
683 static int vmlfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
684 {
685 	struct vml_info *vinfo = container_of(info, struct vml_info, info);
686 	int ret;
687 
688 	mutex_lock(&vml_mutex);
689 	ret = vmlfb_check_var_locked(var, vinfo);
690 	mutex_unlock(&vml_mutex);
691 
692 	return ret;
693 }
694 
695 static void vml_wait_vblank(struct vml_info *vinfo)
696 {
697 	/* Wait for vblank. For now, just wait for a 50Hz cycle (20ms)) */
698 	mdelay(20);
699 }
700 
701 static void vmlfb_disable_pipe(struct vml_info *vinfo)
702 {
703 	struct vml_par *par = vinfo->par;
704 
705 	/* Disable the MDVO pad */
706 	VML_WRITE32(par, VML_RCOMPSTAT, 0);
707 	while (!(VML_READ32(par, VML_RCOMPSTAT) & VML_MDVO_VDC_I_RCOMP)) ;
708 
709 	/* Disable display planes */
710 	VML_WRITE32(par, VML_DSPCCNTR,
711 		    VML_READ32(par, VML_DSPCCNTR) & ~VML_GFX_ENABLE);
712 	(void)VML_READ32(par, VML_DSPCCNTR);
713 	/* Wait for vblank for the disable to take effect */
714 	vml_wait_vblank(vinfo);
715 
716 	/* Next, disable display pipes */
717 	VML_WRITE32(par, VML_PIPEACONF, 0);
718 	(void)VML_READ32(par, VML_PIPEACONF);
719 
720 	vinfo->pipe_disabled = 1;
721 }
722 
723 #ifdef VERMILION_DEBUG
724 static void vml_dump_regs(struct vml_info *vinfo)
725 {
726 	struct vml_par *par = vinfo->par;
727 
728 	printk(KERN_DEBUG MODULE_NAME ": Modesetting register dump:\n");
729 	printk(KERN_DEBUG MODULE_NAME ": \tHTOTAL_A         : 0x%08x\n",
730 	       (unsigned)VML_READ32(par, VML_HTOTAL_A));
731 	printk(KERN_DEBUG MODULE_NAME ": \tHBLANK_A         : 0x%08x\n",
732 	       (unsigned)VML_READ32(par, VML_HBLANK_A));
733 	printk(KERN_DEBUG MODULE_NAME ": \tHSYNC_A          : 0x%08x\n",
734 	       (unsigned)VML_READ32(par, VML_HSYNC_A));
735 	printk(KERN_DEBUG MODULE_NAME ": \tVTOTAL_A         : 0x%08x\n",
736 	       (unsigned)VML_READ32(par, VML_VTOTAL_A));
737 	printk(KERN_DEBUG MODULE_NAME ": \tVBLANK_A         : 0x%08x\n",
738 	       (unsigned)VML_READ32(par, VML_VBLANK_A));
739 	printk(KERN_DEBUG MODULE_NAME ": \tVSYNC_A          : 0x%08x\n",
740 	       (unsigned)VML_READ32(par, VML_VSYNC_A));
741 	printk(KERN_DEBUG MODULE_NAME ": \tDSPCSTRIDE       : 0x%08x\n",
742 	       (unsigned)VML_READ32(par, VML_DSPCSTRIDE));
743 	printk(KERN_DEBUG MODULE_NAME ": \tDSPCSIZE         : 0x%08x\n",
744 	       (unsigned)VML_READ32(par, VML_DSPCSIZE));
745 	printk(KERN_DEBUG MODULE_NAME ": \tDSPCPOS          : 0x%08x\n",
746 	       (unsigned)VML_READ32(par, VML_DSPCPOS));
747 	printk(KERN_DEBUG MODULE_NAME ": \tDSPARB           : 0x%08x\n",
748 	       (unsigned)VML_READ32(par, VML_DSPARB));
749 	printk(KERN_DEBUG MODULE_NAME ": \tDSPCADDR         : 0x%08x\n",
750 	       (unsigned)VML_READ32(par, VML_DSPCADDR));
751 	printk(KERN_DEBUG MODULE_NAME ": \tBCLRPAT_A        : 0x%08x\n",
752 	       (unsigned)VML_READ32(par, VML_BCLRPAT_A));
753 	printk(KERN_DEBUG MODULE_NAME ": \tCANVSCLR_A       : 0x%08x\n",
754 	       (unsigned)VML_READ32(par, VML_CANVSCLR_A));
755 	printk(KERN_DEBUG MODULE_NAME ": \tPIPEASRC         : 0x%08x\n",
756 	       (unsigned)VML_READ32(par, VML_PIPEASRC));
757 	printk(KERN_DEBUG MODULE_NAME ": \tPIPEACONF        : 0x%08x\n",
758 	       (unsigned)VML_READ32(par, VML_PIPEACONF));
759 	printk(KERN_DEBUG MODULE_NAME ": \tDSPCCNTR         : 0x%08x\n",
760 	       (unsigned)VML_READ32(par, VML_DSPCCNTR));
761 	printk(KERN_DEBUG MODULE_NAME ": \tRCOMPSTAT        : 0x%08x\n",
762 	       (unsigned)VML_READ32(par, VML_RCOMPSTAT));
763 	printk(KERN_DEBUG MODULE_NAME ": End of modesetting register dump.\n");
764 }
765 #endif
766 
767 static int vmlfb_set_par_locked(struct vml_info *vinfo)
768 {
769 	struct vml_par *par = vinfo->par;
770 	struct fb_info *info = &vinfo->info;
771 	struct fb_var_screeninfo *var = &info->var;
772 	u32 htotal, hactive, hblank_start, hblank_end, hsync_start, hsync_end;
773 	u32 vtotal, vactive, vblank_start, vblank_end, vsync_start, vsync_end;
774 	u32 dspcntr;
775 	int clock;
776 
777 	vinfo->bytes_per_pixel = var->bits_per_pixel >> 3;
778 	vinfo->stride = ALIGN(var->xres_virtual * vinfo->bytes_per_pixel, 0x40);
779 	info->fix.line_length = vinfo->stride;
780 
781 	if (!subsys)
782 		return 0;
783 
784 	htotal =
785 	    var->xres + var->right_margin + var->hsync_len + var->left_margin;
786 	hactive = var->xres;
787 	hblank_start = var->xres;
788 	hblank_end = htotal;
789 	hsync_start = hactive + var->right_margin;
790 	hsync_end = hsync_start + var->hsync_len;
791 
792 	vtotal =
793 	    var->yres + var->lower_margin + var->vsync_len + var->upper_margin;
794 	vactive = var->yres;
795 	vblank_start = var->yres;
796 	vblank_end = vtotal;
797 	vsync_start = vactive + var->lower_margin;
798 	vsync_end = vsync_start + var->vsync_len;
799 
800 	dspcntr = VML_GFX_ENABLE | VML_GFX_GAMMABYPASS;
801 	clock = PICOS2KHZ(var->pixclock);
802 
803 	if (subsys->nearest_clock) {
804 		clock = subsys->nearest_clock(subsys, clock);
805 	} else {
806 		clock = vml_nearest_clock(clock);
807 	}
808 	printk(KERN_DEBUG MODULE_NAME
809 	       ": Set mode Hfreq : %d kHz, Vfreq : %d Hz.\n", clock / htotal,
810 	       ((clock / htotal) * 1000) / vtotal);
811 
812 	switch (var->bits_per_pixel) {
813 	case 16:
814 		dspcntr |= VML_GFX_ARGB1555;
815 		break;
816 	case 32:
817 		if (var->transp.length == 8)
818 			dspcntr |= VML_GFX_ARGB8888 | VML_GFX_ALPHAMULT;
819 		else
820 			dspcntr |= VML_GFX_RGB0888;
821 		break;
822 	default:
823 		return -EINVAL;
824 	}
825 
826 	vmlfb_disable_pipe(vinfo);
827 	mb();
828 
829 	if (subsys->set_clock)
830 		subsys->set_clock(subsys, clock);
831 	else
832 		return -EINVAL;
833 
834 	VML_WRITE32(par, VML_HTOTAL_A, ((htotal - 1) << 16) | (hactive - 1));
835 	VML_WRITE32(par, VML_HBLANK_A,
836 		    ((hblank_end - 1) << 16) | (hblank_start - 1));
837 	VML_WRITE32(par, VML_HSYNC_A,
838 		    ((hsync_end - 1) << 16) | (hsync_start - 1));
839 	VML_WRITE32(par, VML_VTOTAL_A, ((vtotal - 1) << 16) | (vactive - 1));
840 	VML_WRITE32(par, VML_VBLANK_A,
841 		    ((vblank_end - 1) << 16) | (vblank_start - 1));
842 	VML_WRITE32(par, VML_VSYNC_A,
843 		    ((vsync_end - 1) << 16) | (vsync_start - 1));
844 	VML_WRITE32(par, VML_DSPCSTRIDE, vinfo->stride);
845 	VML_WRITE32(par, VML_DSPCSIZE,
846 		    ((var->yres - 1) << 16) | (var->xres - 1));
847 	VML_WRITE32(par, VML_DSPCPOS, 0x00000000);
848 	VML_WRITE32(par, VML_DSPARB, VML_FIFO_DEFAULT);
849 	VML_WRITE32(par, VML_BCLRPAT_A, 0x00000000);
850 	VML_WRITE32(par, VML_CANVSCLR_A, 0x00000000);
851 	VML_WRITE32(par, VML_PIPEASRC,
852 		    ((var->xres - 1) << 16) | (var->yres - 1));
853 
854 	wmb();
855 	VML_WRITE32(par, VML_PIPEACONF, VML_PIPE_ENABLE);
856 	wmb();
857 	VML_WRITE32(par, VML_DSPCCNTR, dspcntr);
858 	wmb();
859 	VML_WRITE32(par, VML_DSPCADDR, (u32) vinfo->vram_start +
860 		    var->yoffset * vinfo->stride +
861 		    var->xoffset * vinfo->bytes_per_pixel);
862 
863 	VML_WRITE32(par, VML_RCOMPSTAT, VML_MDVO_PAD_ENABLE);
864 
865 	while (!(VML_READ32(par, VML_RCOMPSTAT) &
866 		 (VML_MDVO_VDC_I_RCOMP | VML_MDVO_PAD_ENABLE))) ;
867 
868 	vinfo->pipe_disabled = 0;
869 #ifdef VERMILION_DEBUG
870 	vml_dump_regs(vinfo);
871 #endif
872 
873 	return 0;
874 }
875 
876 static int vmlfb_set_par(struct fb_info *info)
877 {
878 	struct vml_info *vinfo = container_of(info, struct vml_info, info);
879 	int ret;
880 
881 	mutex_lock(&vml_mutex);
882 	list_move(&vinfo->head, (subsys) ? &global_has_mode : &global_no_mode);
883 	ret = vmlfb_set_par_locked(vinfo);
884 
885 	mutex_unlock(&vml_mutex);
886 	return ret;
887 }
888 
889 static int vmlfb_blank_locked(struct vml_info *vinfo)
890 {
891 	struct vml_par *par = vinfo->par;
892 	u32 cur = VML_READ32(par, VML_PIPEACONF);
893 
894 	switch (vinfo->cur_blank_mode) {
895 	case FB_BLANK_UNBLANK:
896 		if (vinfo->pipe_disabled) {
897 			vmlfb_set_par_locked(vinfo);
898 		}
899 		VML_WRITE32(par, VML_PIPEACONF, cur & ~VML_PIPE_FORCE_BORDER);
900 		(void)VML_READ32(par, VML_PIPEACONF);
901 		break;
902 	case FB_BLANK_NORMAL:
903 		if (vinfo->pipe_disabled) {
904 			vmlfb_set_par_locked(vinfo);
905 		}
906 		VML_WRITE32(par, VML_PIPEACONF, cur | VML_PIPE_FORCE_BORDER);
907 		(void)VML_READ32(par, VML_PIPEACONF);
908 		break;
909 	case FB_BLANK_VSYNC_SUSPEND:
910 	case FB_BLANK_HSYNC_SUSPEND:
911 		if (!vinfo->pipe_disabled) {
912 			vmlfb_disable_pipe(vinfo);
913 		}
914 		break;
915 	case FB_BLANK_POWERDOWN:
916 		if (!vinfo->pipe_disabled) {
917 			vmlfb_disable_pipe(vinfo);
918 		}
919 		break;
920 	default:
921 		return -EINVAL;
922 	}
923 
924 	return 0;
925 }
926 
927 static int vmlfb_blank(int blank_mode, struct fb_info *info)
928 {
929 	struct vml_info *vinfo = container_of(info, struct vml_info, info);
930 	int ret;
931 
932 	mutex_lock(&vml_mutex);
933 	vinfo->cur_blank_mode = blank_mode;
934 	ret = vmlfb_blank_locked(vinfo);
935 	mutex_unlock(&vml_mutex);
936 	return ret;
937 }
938 
939 static int vmlfb_pan_display(struct fb_var_screeninfo *var,
940 			     struct fb_info *info)
941 {
942 	struct vml_info *vinfo = container_of(info, struct vml_info, info);
943 	struct vml_par *par = vinfo->par;
944 
945 	mutex_lock(&vml_mutex);
946 	VML_WRITE32(par, VML_DSPCADDR, (u32) vinfo->vram_start +
947 		    var->yoffset * vinfo->stride +
948 		    var->xoffset * vinfo->bytes_per_pixel);
949 	(void)VML_READ32(par, VML_DSPCADDR);
950 	mutex_unlock(&vml_mutex);
951 
952 	return 0;
953 }
954 
955 static int vmlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
956 			   u_int transp, struct fb_info *info)
957 {
958 	u32 v;
959 
960 	if (regno >= 16)
961 		return -EINVAL;
962 
963 	if (info->var.grayscale) {
964 		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
965 	}
966 
967 	if (info->fix.visual != FB_VISUAL_TRUECOLOR)
968 		return -EINVAL;
969 
970 	red = VML_TOHW(red, info->var.red.length);
971 	blue = VML_TOHW(blue, info->var.blue.length);
972 	green = VML_TOHW(green, info->var.green.length);
973 	transp = VML_TOHW(transp, info->var.transp.length);
974 
975 	v = (red << info->var.red.offset) |
976 	    (green << info->var.green.offset) |
977 	    (blue << info->var.blue.offset) |
978 	    (transp << info->var.transp.offset);
979 
980 	switch (info->var.bits_per_pixel) {
981 	case 16:
982 		((u32 *) info->pseudo_palette)[regno] = v;
983 		break;
984 	case 24:
985 	case 32:
986 		((u32 *) info->pseudo_palette)[regno] = v;
987 		break;
988 	}
989 	return 0;
990 }
991 
992 static int vmlfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
993 {
994 	struct vml_info *vinfo = container_of(info, struct vml_info, info);
995 	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
996 	int ret;
997 	unsigned long prot;
998 
999 	ret = vmlfb_vram_offset(vinfo, offset);
1000 	if (ret)
1001 		return -EINVAL;
1002 
1003 	prot = pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK;
1004 	pgprot_val(vma->vm_page_prot) =
1005 		prot | cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS);
1006 
1007 	return vm_iomap_memory(vma, vinfo->vram_start,
1008 			vinfo->vram_contig_size);
1009 }
1010 
1011 static int vmlfb_sync(struct fb_info *info)
1012 {
1013 	return 0;
1014 }
1015 
1016 static int vmlfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1017 {
1018 	return -EINVAL;	/* just to force soft_cursor() call */
1019 }
1020 
1021 static struct fb_ops vmlfb_ops = {
1022 	.owner = THIS_MODULE,
1023 	.fb_open = vmlfb_open,
1024 	.fb_release = vmlfb_release,
1025 	.fb_check_var = vmlfb_check_var,
1026 	.fb_set_par = vmlfb_set_par,
1027 	.fb_blank = vmlfb_blank,
1028 	.fb_pan_display = vmlfb_pan_display,
1029 	.fb_fillrect = cfb_fillrect,
1030 	.fb_copyarea = cfb_copyarea,
1031 	.fb_imageblit = cfb_imageblit,
1032 	.fb_cursor = vmlfb_cursor,
1033 	.fb_sync = vmlfb_sync,
1034 	.fb_mmap = vmlfb_mmap,
1035 	.fb_setcolreg = vmlfb_setcolreg
1036 };
1037 
1038 static const struct pci_device_id vml_ids[] = {
1039 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, VML_DEVICE_VDC)},
1040 	{0}
1041 };
1042 
1043 static struct pci_driver vmlfb_pci_driver = {
1044 	.name = "vmlfb",
1045 	.id_table = vml_ids,
1046 	.probe = vml_pci_probe,
1047 	.remove = vml_pci_remove,
1048 };
1049 
1050 static void __exit vmlfb_cleanup(void)
1051 {
1052 	pci_unregister_driver(&vmlfb_pci_driver);
1053 }
1054 
1055 static int __init vmlfb_init(void)
1056 {
1057 
1058 #ifndef MODULE
1059 	char *option = NULL;
1060 
1061 	if (fb_get_options(MODULE_NAME, &option))
1062 		return -ENODEV;
1063 #endif
1064 
1065 	printk(KERN_DEBUG MODULE_NAME ": initializing\n");
1066 	mutex_init(&vml_mutex);
1067 	INIT_LIST_HEAD(&global_no_mode);
1068 	INIT_LIST_HEAD(&global_has_mode);
1069 
1070 	return pci_register_driver(&vmlfb_pci_driver);
1071 }
1072 
1073 int vmlfb_register_subsys(struct vml_sys *sys)
1074 {
1075 	struct vml_info *entry;
1076 	struct list_head *list;
1077 	u32 save_activate;
1078 
1079 	mutex_lock(&vml_mutex);
1080 	if (subsys != NULL) {
1081 		subsys->restore(subsys);
1082 	}
1083 	subsys = sys;
1084 	subsys->save(subsys);
1085 
1086 	/*
1087 	 * We need to restart list traversal for each item, since we
1088 	 * release the list mutex in the loop.
1089 	 */
1090 
1091 	list = global_no_mode.next;
1092 	while (list != &global_no_mode) {
1093 		list_del_init(list);
1094 		entry = list_entry(list, struct vml_info, head);
1095 
1096 		/*
1097 		 * First, try the current mode which might not be
1098 		 * completely validated with respect to the pixel clock.
1099 		 */
1100 
1101 		if (!vmlfb_check_var_locked(&entry->info.var, entry)) {
1102 			vmlfb_set_par_locked(entry);
1103 			list_add_tail(list, &global_has_mode);
1104 		} else {
1105 
1106 			/*
1107 			 * Didn't work. Try to find another mode,
1108 			 * that matches this subsys.
1109 			 */
1110 
1111 			mutex_unlock(&vml_mutex);
1112 			save_activate = entry->info.var.activate;
1113 			entry->info.var.bits_per_pixel = 16;
1114 			vmlfb_set_pref_pixel_format(&entry->info.var);
1115 			if (fb_find_mode(&entry->info.var,
1116 					 &entry->info,
1117 					 vml_default_mode, NULL, 0, NULL, 16)) {
1118 				entry->info.var.activate |=
1119 				    FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
1120 				fb_set_var(&entry->info, &entry->info.var);
1121 			} else {
1122 				printk(KERN_ERR MODULE_NAME
1123 				       ": Sorry. no mode found for this subsys.\n");
1124 			}
1125 			entry->info.var.activate = save_activate;
1126 			mutex_lock(&vml_mutex);
1127 		}
1128 		vmlfb_blank_locked(entry);
1129 		list = global_no_mode.next;
1130 	}
1131 	mutex_unlock(&vml_mutex);
1132 
1133 	printk(KERN_DEBUG MODULE_NAME ": Registered %s subsystem.\n",
1134 				subsys->name ? subsys->name : "unknown");
1135 	return 0;
1136 }
1137 
1138 EXPORT_SYMBOL_GPL(vmlfb_register_subsys);
1139 
1140 void vmlfb_unregister_subsys(struct vml_sys *sys)
1141 {
1142 	struct vml_info *entry, *next;
1143 
1144 	mutex_lock(&vml_mutex);
1145 	if (subsys != sys) {
1146 		mutex_unlock(&vml_mutex);
1147 		return;
1148 	}
1149 	subsys->restore(subsys);
1150 	subsys = NULL;
1151 	list_for_each_entry_safe(entry, next, &global_has_mode, head) {
1152 		printk(KERN_DEBUG MODULE_NAME ": subsys disable pipe\n");
1153 		vmlfb_disable_pipe(entry);
1154 		list_move_tail(&entry->head, &global_no_mode);
1155 	}
1156 	mutex_unlock(&vml_mutex);
1157 }
1158 
1159 EXPORT_SYMBOL_GPL(vmlfb_unregister_subsys);
1160 
1161 module_init(vmlfb_init);
1162 module_exit(vmlfb_cleanup);
1163 
1164 MODULE_AUTHOR("Tungsten Graphics");
1165 MODULE_DESCRIPTION("Initialization of the Vermilion display devices");
1166 MODULE_VERSION("1.0.0");
1167 MODULE_LICENSE("GPL");
1168