1
#include <linux/string.h>
2
#include <linux/slab.h>
3
#include <linux/types.h>
4
#include <linux/init.h>
5
#include <linux/module.h>
7
#include <linux/kernel.h>
8
#include <linux/errno.h>
10
#include <linux/cdev.h>
11
#include <linux/sysfs.h>
12
#include <asm/atomic.h>
13
#include <linux/pagemap.h>
14
#include <linux/spinlock.h>
15
#include <linux/list.h>
16
#include <asm/scatterlist.h>
17
#include <linux/vmalloc.h>
18
#include <linux/stat.h>
19
#include <linux/interrupt.h>
20
#include <linux/wait.h>
27
* Called when an application open()s a /dev/fpga*, attaches the private data
28
* with the file pointer.
31
static int pcidriver_open(struct inode *inode, struct file *filp)
33
pcidriver_privdata_t *privdata;
35
/* Set the private data area for the file */
36
privdata = container_of( inode->i_cdev, pcidriver_privdata_t, cdev);
37
filp->private_data = privdata;
39
pcidriver_module_get(privdata);
46
* Called when the application close()s the file descriptor. Does nothing at
50
static int pcidriver_release(struct inode *inode, struct file *filp)
52
pcidriver_privdata_t *privdata;
54
/* Get the private data area */
55
privdata = filp->private_data;
57
pcidriver_module_put(privdata);
63
/*************************************************************************/
64
/* Internal driver functions */
65
static int pcidriver_mmap_bar(pcidriver_privdata_t *privdata, struct vm_area_struct *vmap, int bar)
67
#ifdef PCIDRIVER_DUMMY_DEVICE
69
#else /* PCIDRIVER_DUMMY_DEVICE */
71
unsigned long bar_addr;
72
unsigned long bar_length, vma_size;
73
unsigned long bar_flags;
75
mod_info_dbg("Entering mmap_pci\n");
78
/* Get info of the BAR to be mapped */
79
bar_addr = pci_resource_start(privdata->pdev, bar);
80
bar_length = pci_resource_len(privdata->pdev, bar);
81
bar_flags = pci_resource_flags(privdata->pdev, bar);
84
vma_size = (vmap->vm_end - vmap->vm_start);
86
if ((vma_size != bar_length) &&
87
((bar_length < PAGE_SIZE) && (vma_size != PAGE_SIZE))) {
88
mod_info( "mmap size is not correct! bar: %lu - vma: %lu\n", bar_length, vma_size );
92
if (bar_flags & IORESOURCE_IO) {
93
/* Unlikely case, we will mmap a IO region */
95
/* IO regions are never cacheable */
96
vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
99
ret = io_remap_pfn_range(vmap, vmap->vm_start, (bar_addr >> PAGE_SHIFT), bar_length, vmap->vm_page_prot);
101
/* Normal case, mmap a memory region */
103
/* Ensure this VMA is non-cached, if it is not flaged as prefetchable.
104
* If it is prefetchable, caching is allowed and will give better performance.
105
* This should be set properly by the BIOS, but we want to be sure. */
106
/* adapted from drivers/char/mem.c, mmap function. */
108
/* Setting noncached disables MTRR registers, and we want to use them.
109
* So we take this code out. This can lead to caching problems if and only if
110
* the System BIOS set something wrong. Check LDDv3, page 425.
113
// if (!(bar_flags & IORESOURCE_PREFETCH))
114
// vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
118
ret = remap_pfn_range(vmap, vmap->vm_start, (bar_addr >> PAGE_SHIFT), bar_length, vmap->vm_page_prot);
122
mod_info("remap_pfn_range failed\n");
126
return 0; /* success */
127
#endif /* PCIDRIVER_DUMMY_DEVICE */
132
* This function is the entry point for mmap() and calls either pcidriver_mmap_bar
133
* or pcidriver_mmap_kmem
135
* @see pcidriver_mmap_bar
136
* @see pcidriver_mmap_kmem
139
static int pcidriver_mmap(struct file *filp, struct vm_area_struct *vma)
141
pcidriver_privdata_t *privdata;
144
mod_info_dbg("Entering mmap\n");
146
/* Get the private data area */
147
privdata = filp->private_data;
149
/* Check the current mmap mode */
150
switch (privdata->mmap_mode) {
151
case PCIDRIVER_MMAP_PCI:
152
bar = privdata->mmap_area;
153
if ((bar < 0)||(bar > 5)) {
154
mod_info("Attempted to mmap a PCI area with the wrong mmap_area value: %d\n",privdata->mmap_area);
157
ret = pcidriver_mmap_bar(privdata, vma, bar);
159
case PCIDRIVER_MMAP_KMEM:
160
ret = pcidriver_mmap_kmem(privdata, vma);
163
mod_info( "Invalid mmap_mode value (%d)\n",privdata->mmap_mode );
164
return -EINVAL; /* Invalid parameter (mode) */
170
static struct file_operations pcidriver_fops = {
171
.owner = THIS_MODULE,
172
.unlocked_ioctl = pcidriver_ioctl,
173
.mmap = pcidriver_mmap,
174
.open = pcidriver_open,
175
.release = pcidriver_release,
178
const struct file_operations *pcidriver_get_fops(void)
180
return &pcidriver_fops;
184
void pcidriver_module_get(pcidriver_privdata_t *privdata) {
185
atomic_inc(&(privdata->refs));
186
// mod_info("Ref: %i\n", atomic_read(&(privdata->refs)));
189
void pcidriver_module_put(pcidriver_privdata_t *privdata) {
190
if (atomic_add_negative(-1, &(privdata->refs))) {
191
atomic_inc(&(privdata->refs));
192
mod_info("Reference counting error...");
194
// mod_info("Unref: %i\n", atomic_read(&(privdata->refs)));