⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vermilion.c

📁 Linux环境下视频显示卡设备的驱动程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) Intel Corp. 2007. * All Rights Reserved. * * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to * develop this driver. * * This file is part of the Vermilion Range fb driver. * The Vermilion Range fb driver is free software; * you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * The Vermilion Range fb driver is distributed * in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this driver; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA * * Authors: *   Thomas Hellström <thomas-at-tungstengraphics-dot-com> *   Michel Dänzer <michel-at-tungstengraphics-dot-com> *   Alan Hourihane <alanh-at-tungstengraphics-dot-com> */#include <linux/module.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/string.h>#include <linux/delay.h>#include <linux/mm.h>#include <linux/fb.h>#include <linux/pci.h>#include <asm/cacheflush.h>#include <asm/tlbflush.h>#include <linux/mmzone.h>/* #define VERMILION_DEBUG */#include "vermilion.h"#define MODULE_NAME "vmlfb"#define VML_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)static struct mutex vml_mutex;static struct list_head global_no_mode;static struct list_head global_has_mode;static struct fb_ops vmlfb_ops;static struct vml_sys *subsys = NULL;static char *vml_default_mode = "1024x768@60";static struct fb_videomode defaultmode = {	NULL, 60, 1024, 768, 12896, 144, 24, 29, 3, 136, 6,	0, FB_VMODE_NONINTERLACED};static u32 vml_mem_requested = (10 * 1024 * 1024);static u32 vml_mem_contig = (4 * 1024 * 1024);static u32 vml_mem_min = (4 * 1024 * 1024);static u32 vml_clocks[] = {	6750,	13500,	27000,	29700,	37125,	54000,	59400,	74250,	120000,	148500};static u32 vml_num_clocks = ARRAY_SIZE(vml_clocks);/* * Allocate a contiguous vram area and make its linear kernel map * uncached. */static int vmlfb_alloc_vram_area(struct vram_area *va, unsigned max_order,				 unsigned min_order){	gfp_t flags;	unsigned long i;	max_order++;	do {		/*		 * Really try hard to get the needed memory.		 * We need memory below the first 32MB, so we		 * add the __GFP_DMA flag that guarantees that we are		 * below the first 16MB.		 */		flags = __GFP_DMA | __GFP_HIGH;		va->logical =			 __get_free_pages(flags, --max_order);	} while (va->logical == 0 && max_order > min_order);	if (!va->logical)		return -ENOMEM;	va->phys = virt_to_phys((void *)va->logical);	va->size = PAGE_SIZE << max_order;	va->order = max_order;	/*	 * It seems like __get_free_pages only ups the usage count	 * of the first page. This doesn't work with fault mapping, so	 * up the usage count once more (XXX: should use split_page or	 * compound page).	 */	memset((void *)va->logical, 0x00, va->size);	for (i = va->logical; i < va->logical + va->size; i += PAGE_SIZE) {		get_page(virt_to_page(i));	}	/*	 * Change caching policy of the linear kernel map to avoid	 * mapping type conflicts with user-space mappings.	 */	set_pages_uc(virt_to_page(va->logical), va->size >> PAGE_SHIFT);	printk(KERN_DEBUG MODULE_NAME	       ": Allocated %ld bytes vram area at 0x%08lx\n",	       va->size, va->phys);	return 0;}/* * Free a contiguous vram area and reset its linear kernel map * mapping type. */static void vmlfb_free_vram_area(struct vram_area *va){	unsigned long j;	if (va->logical) {		/*		 * Reset the linear kernel map caching policy.		 */		set_pages_wb(virt_to_page(va->logical),				 va->size >> PAGE_SHIFT);		/*		 * Decrease the usage count on the pages we've used		 * to compensate for upping when allocating.		 */		for (j = va->logical; j < va->logical + va->size;		     j += PAGE_SIZE) {			(void)put_page_testzero(virt_to_page(j));		}		printk(KERN_DEBUG MODULE_NAME		       ": Freeing %ld bytes vram area at 0x%08lx\n",		       va->size, va->phys);		free_pages(va->logical, va->order);		va->logical = 0;	}}/* * Free allocated vram. */static void vmlfb_free_vram(struct vml_info *vinfo){	int i;	for (i = 0; i < vinfo->num_areas; ++i) {		vmlfb_free_vram_area(&vinfo->vram[i]);	}	vinfo->num_areas = 0;}/* * Allocate vram. Currently we try to allocate contiguous areas from the * __GFP_DMA zone and puzzle them together. A better approach would be to * allocate one contiguous area for scanout and use one-page allocations for * offscreen areas. This requires user-space and GPU virtual mappings. */static int vmlfb_alloc_vram(struct vml_info *vinfo,			    size_t requested,			    size_t min_total, size_t min_contig){	int i, j;	int order;	int contiguous;	int err;	struct vram_area *va;	struct vram_area *va2;	vinfo->num_areas = 0;	for (i = 0; i < VML_VRAM_AREAS; ++i) {		va = &vinfo->vram[i];		order = 0;		while (requested > (PAGE_SIZE << order) && order < MAX_ORDER)			order++;		err = vmlfb_alloc_vram_area(va, order, 0);		if (err)			break;		if (i == 0) {			vinfo->vram_start = va->phys;			vinfo->vram_logical = (void __iomem *) va->logical;			vinfo->vram_contig_size = va->size;			vinfo->num_areas = 1;		} else {			contiguous = 0;			for (j = 0; j < i; ++j) {				va2 = &vinfo->vram[j];				if (va->phys + va->size == va2->phys ||				    va2->phys + va2->size == va->phys) {					contiguous = 1;					break;				}			}			if (contiguous) {				vinfo->num_areas++;				if (va->phys < vinfo->vram_start) {					vinfo->vram_start = va->phys;					vinfo->vram_logical =						(void __iomem *)va->logical;				}				vinfo->vram_contig_size += va->size;			} else {				vmlfb_free_vram_area(va);				break;			}		}		if (requested < va->size)			break;		else			requested -= va->size;	}	if (vinfo->vram_contig_size > min_total &&	    vinfo->vram_contig_size > min_contig) {		printk(KERN_DEBUG MODULE_NAME		       ": Contiguous vram: %ld bytes at physical 0x%08lx.\n",		       (unsigned long)vinfo->vram_contig_size,		       (unsigned long)vinfo->vram_start);		return 0;	}	printk(KERN_ERR MODULE_NAME	       ": Could not allocate requested minimal amount of vram.\n");	vmlfb_free_vram(vinfo);	return -ENOMEM;}/* * Find the GPU to use with our display controller. */static int vmlfb_get_gpu(struct vml_par *par){	mutex_lock(&vml_mutex);	par->gpu = pci_get_device(PCI_VENDOR_ID_INTEL, VML_DEVICE_GPU, NULL);	if (!par->gpu) {		mutex_unlock(&vml_mutex);		return -ENODEV;	}	mutex_unlock(&vml_mutex);	if (pci_enable_device(par->gpu) < 0)		return -ENODEV;	return 0;}/* * Find a contiguous vram area that contains a given offset from vram start. */static int vmlfb_vram_offset(struct vml_info *vinfo, unsigned long offset){	unsigned long aoffset;	unsigned i;	for (i = 0; i < vinfo->num_areas; ++i) {		aoffset = offset - (vinfo->vram[i].phys - vinfo->vram_start);		if (aoffset < vinfo->vram[i].size) {			return 0;		}	}	return -EINVAL;}/* * Remap the MMIO register spaces of the VDC and the GPU. */static int vmlfb_enable_mmio(struct vml_par *par){	int err;	par->vdc_mem_base = pci_resource_start(par->vdc, 0);	par->vdc_mem_size = pci_resource_len(par->vdc, 0);	if (!request_mem_region(par->vdc_mem_base, par->vdc_mem_size, "vmlfb")) {		printk(KERN_ERR MODULE_NAME		       ": Could not claim display controller MMIO.\n");		return -EBUSY;	}	par->vdc_mem = ioremap_nocache(par->vdc_mem_base, par->vdc_mem_size);	if (par->vdc_mem == NULL) {		printk(KERN_ERR MODULE_NAME		       ": Could not map display controller MMIO.\n");		err = -ENOMEM;		goto out_err_0;	}	par->gpu_mem_base = pci_resource_start(par->gpu, 0);	par->gpu_mem_size = pci_resource_len(par->gpu, 0);	if (!request_mem_region(par->gpu_mem_base, par->gpu_mem_size, "vmlfb")) {		printk(KERN_ERR MODULE_NAME ": Could not claim GPU MMIO.\n");		err = -EBUSY;		goto out_err_1;	}	par->gpu_mem = ioremap_nocache(par->gpu_mem_base, par->gpu_mem_size);	if (par->gpu_mem == NULL) {		printk(KERN_ERR MODULE_NAME ": Could not map GPU MMIO.\n");		err = -ENOMEM;		goto out_err_2;	}	return 0;out_err_2:	release_mem_region(par->gpu_mem_base, par->gpu_mem_size);out_err_1:	iounmap(par->vdc_mem);out_err_0:	release_mem_region(par->vdc_mem_base, par->vdc_mem_size);	return err;}/* * Unmap the VDC and GPU register spaces. */static void vmlfb_disable_mmio(struct vml_par *par){	iounmap(par->gpu_mem);	release_mem_region(par->gpu_mem_base, par->gpu_mem_size);	iounmap(par->vdc_mem);	release_mem_region(par->vdc_mem_base, par->vdc_mem_size);}/* * Release and uninit the VDC and GPU. */static void vmlfb_release_devices(struct vml_par *par){	if (atomic_dec_and_test(&par->refcount)) {		pci_set_drvdata(par->vdc, NULL);		pci_disable_device(par->gpu);		pci_disable_device(par->vdc);	}}/* * Free up allocated resources for a device. */static void __devexit vml_pci_remove(struct pci_dev *dev){	struct fb_info *info;	struct vml_info *vinfo;	struct vml_par *par;	info = pci_get_drvdata(dev);	if (info) {		vinfo = container_of(info, struct vml_info, info);		par = vinfo->par;		mutex_lock(&vml_mutex);		unregister_framebuffer(info);		fb_dealloc_cmap(&info->cmap);		vmlfb_free_vram(vinfo);		vmlfb_disable_mmio(par);		vmlfb_release_devices(par);		kfree(vinfo);		kfree(par);		mutex_unlock(&vml_mutex);	}}static void vmlfb_set_pref_pixel_format(struct fb_var_screeninfo *var){	switch (var->bits_per_pixel) {	case 16:		var->blue.offset = 0;		var->blue.length = 5;		var->green.offset = 5;		var->green.length = 5;		var->red.offset = 10;		var->red.length = 5;		var->transp.offset = 15;		var->transp.length = 1;		break;	case 32:		var->blue.offset = 0;		var->blue.length = 8;		var->green.offset = 8;		var->green.length = 8;		var->red.offset = 16;		var->red.length = 8;		var->transp.offset = 24;		var->transp.length = 0;		break;	default:		break;	}	var->blue.msb_right = var->green.msb_right =	    var->red.msb_right = var->transp.msb_right = 0;}/* * Device initialization. * We initialize one vml_par struct per device and one vml_info * struct per pipe. Currently we have only one pipe. */static int __devinit vml_pci_probe(struct pci_dev *dev,				   const struct pci_device_id *id){	struct vml_info *vinfo;	struct fb_info *info;	struct vml_par *par;	int err = 0;	par = kzalloc(sizeof(*par), GFP_KERNEL);	if (par == NULL)		return -ENOMEM;	vinfo = kzalloc(sizeof(*vinfo), GFP_KERNEL);	if (vinfo == NULL) {		err = -ENOMEM;		goto out_err_0;	}	vinfo->par = par;	par->vdc = dev;	atomic_set(&par->refcount, 1);	switch (id->device) {	case VML_DEVICE_VDC:		if ((err = vmlfb_get_gpu(par)))			goto out_err_1;		pci_set_drvdata(dev, &vinfo->info);		break;	default:		err = -ENODEV;		goto out_err_1;		break;	}	info = &vinfo->info;	info->flags = FBINFO_DEFAULT | FBINFO_PARTIAL_PAN_OK;	err = vmlfb_enable_mmio(par);	if (err)		goto out_err_2;	err = vmlfb_alloc_vram(vinfo, vml_mem_requested,			       vml_mem_contig, vml_mem_min);	if (err)		goto out_err_3;	strcpy(info->fix.id, "Vermilion Range");	info->fix.mmio_start = 0;	info->fix.mmio_len = 0;	info->fix.smem_start = vinfo->vram_start;	info->fix.smem_len = vinfo->vram_contig_size;	info->fix.type = FB_TYPE_PACKED_PIXELS;	info->fix.visual = FB_VISUAL_TRUECOLOR;	info->fix.ypanstep = 1;	info->fix.xpanstep = 1;	info->fix.ywrapstep = 0;	info->fix.accel = FB_ACCEL_NONE;	info->screen_base = vinfo->vram_logical;	info->pseudo_palette = vinfo->pseudo_palette;	info->par = par;	info->fbops = &vmlfb_ops;	info->device = &dev->dev;	INIT_LIST_HEAD(&vinfo->head);	vinfo->pipe_disabled = 1;	vinfo->cur_blank_mode = FB_BLANK_UNBLANK;	info->var.grayscale = 0;	info->var.bits_per_pixel = 16;	vmlfb_set_pref_pixel_format(&info->var);	if (!fb_find_mode	    (&info->var, info, vml_default_mode, NULL, 0, &defaultmode, 16)) {		printk(KERN_ERR MODULE_NAME ": Could not find initial mode\n");	}	if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {		err = -ENOMEM;		goto out_err_4;	}	err = register_framebuffer(info);	if (err) {		printk(KERN_ERR MODULE_NAME ": Register framebuffer error.\n");		goto out_err_5;	}	printk("Initialized vmlfb\n");	return 0;out_err_5:	fb_dealloc_cmap(&info->cmap);out_err_4:	vmlfb_free_vram(vinfo);out_err_3:	vmlfb_disable_mmio(par);out_err_2:	vmlfb_release_devices(par);out_err_1:	kfree(vinfo);out_err_0:	kfree(par);	return err;}static int vmlfb_open(struct fb_info *info, int user){	/*	 * Save registers here?	 */	return 0;}static int vmlfb_release(struct fb_info *info, int user){	/*	 * Restore registers here.	 */	return 0;}static int vml_nearest_clock(int clock){	int i;	int cur_index;	int cur_diff;	int diff;	cur_index = 0;	cur_diff = clock - vml_clocks[0];	cur_diff = (cur_diff < 0) ? -cur_diff : cur_diff;	for (i = 1; i < vml_num_clocks; ++i) {		diff = clock - vml_clocks[i];		diff = (diff < 0) ? -diff : diff;		if (diff < cur_diff) {			cur_index = i;			cur_diff = diff;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -