x86: --- drivers/pci/pci-sysfs.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++ include/asm-i386/pci.h | 1 include/asm-x86_64/pci.h | 1 include/linux/pci.h | 4 ++ 4 files changed, 69 insertions(+), 1 deletion(-) Index: linux/drivers/pci/pci-sysfs.c =================================================================== --- linux.orig/drivers/pci/pci-sysfs.c +++ linux/drivers/pci/pci-sysfs.c @@ -384,6 +384,49 @@ pci_mmap_resource(struct kobject *kobj, return pci_mmap_page_range(pdev, vma, mmap_type, 0); } +#ifdef HAVE_PCI_COHERENT_MMAP + +struct coh_mmap_data { + void *map; + struct device *dev; + dma_addr_t busadr; +}; + +void pci_coherent_mmap_close(struct vm_area_struct *vma) +{ + struct coh_mmap_data *cm = vma->vm_private_data; + dma_free_coherent(cm->dev, vma->vm_end - vma->vm_start, cm->map, + cm->busadr); +} + +static struct vm_operations_struct pci_coherent_vmops = { + .close = pci_coherent_mmap_close, +}; + +static int +pci_mmap_coherent_mem(struct kobject *kobj, struct bin_attribute *attr, + struct vm_area_struct *vma) +{ + struct device *dev = container_of(kobj, struct device, kobj); + struct pci_dev *pdev = to_pci_dev(dev); + struct coh_mmap_data *cm = kmalloc(sizeof(struct coh_mmap_data), + GFP_KERNEL); + if (!cm) + return -ENOMEM; + cm->map = dma_alloc_coherent(dev, vma->vm_end - vma->vm_start, + &cm->busadr, GFP_KERNEL); + cm->dev = dev; + if (!cm->map) { + kfree(cm->map); + return -ENOMEM; + } + vma->vm_private_data = cm; + vma->vm_pgoff = cm->busadr >> PAGE_SHIFT; + vma->vm_ops = &pci_coherent_vmops; + return pci_mmap_page_range(pdev, vma, pci_mmap_coherent, 0); +} +#endif + /** * pci_create_resource_files - create resource files in sysfs for @dev * @dev: dev in question @@ -419,6 +462,22 @@ pci_create_resource_files(struct pci_dev sysfs_create_bin_file(&pdev->dev.kobj, res_attr); } } +#ifdef HAVE_PCI_COHERENT_MMAP + { + struct bin_attribute *a = kzalloc(sizeof(struct bin_attribute), + GFP_KERNEL); + if (!a) + return; + pdev->coherent_attr = a; + a->attr.name = "coherent_mem"; + a->attr.mode = S_IRUSR | S_IWUSR; + a->attr.owner = THIS_MODULE; + a->size = *(pdev->dev.dma_mask); + a->mmap = pci_mmap_coherent_mem; + a->private = NULL; + sysfs_create_bin_file(&pdev->dev.kobj, a); + } +#endif } /** @@ -442,6 +501,11 @@ pci_remove_resource_files(struct pci_dev kfree(res_attr); } } + +#ifdef HAVE_PCI_COHERENT_MMAP + sysfs_remove_bin_file(&pdev->dev.kobj, pdev->coherent_attr); + kfree(pdev->coherent_attr); +#endif } #else /* !HAVE_PCI_MMAP */ static inline void pci_create_resource_files(struct pci_dev *dev) { return; } Index: linux/include/linux/pci.h =================================================================== --- linux.orig/include/linux/pci.h +++ linux/include/linux/pci.h @@ -55,7 +55,8 @@ /* File state for mmap()s on /proc/bus/pci/X/Y */ enum pci_mmap_state { pci_mmap_io, - pci_mmap_mem + pci_mmap_mem, + pci_mmap_coherent }; /* This defines the direction arg to the DMA mapping routines. */ @@ -172,6 +173,7 @@ struct pci_dev { struct bin_attribute *rom_attr; /* attribute descriptor for sysfs ROM entry */ int rom_attr_enabled; /* has display of the rom attribute been enabled? */ struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */ + struct bin_attribute *coherent_attr; }; #define pci_dev_g(n) list_entry(n, struct pci_dev, global_list) Index: linux/include/asm-i386/pci.h =================================================================== --- linux.orig/include/asm-i386/pci.h +++ linux/include/asm-i386/pci.h @@ -90,6 +90,7 @@ pci_dac_dma_sync_single_for_device(struc } #define HAVE_PCI_MMAP +#define HAVE_PCI_COHERENT_MMAP extern int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, enum pci_mmap_state mmap_state, int write_combine); Index: linux/include/asm-x86_64/pci.h =================================================================== --- linux.orig/include/asm-x86_64/pci.h +++ linux/include/asm-x86_64/pci.h @@ -132,6 +132,7 @@ static inline void pci_dma_burst_advice( #endif #define HAVE_PCI_MMAP +#define HAVE_PCI_COHERENT_MMAP extern int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, enum pci_mmap_state mmap_state, int write_combine);