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 @@ -449,8 +449,78 @@ 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); + sysfs_remove_bin_file(&pdev->dev.kobj, pdev->coherent_wc_attr); + kfree(pdev->coherent_wc_attr); +#endif } +#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); + int wc = strstr(attr->attr.name, "_wc") != 0; + 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); + 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, wc); +} + +static struct bin_attribute *pci_init_coh_attr(struct pci_dev *pdev, char *name) +{ + struct bin_attribute *a = kzalloc(sizeof(struct bin_attribute), + GFP_KERNEL); + if (!a) + return NULL; + a->attr.name = name; + 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; + if (sysfs_create_bin_file(&pdev->dev.kobj, a) < 0) { + printk("PCI-SYSFS: cannot create %s\n", name); + kfree(a); + a = NULL; + } + return a; +} +#endif + /** * pci_create_resource_files - create resource files in sysfs for @dev * @dev: dev in question @@ -621,6 +691,11 @@ int __must_check pci_create_sysfs_dev_fi /* add platform-specific attributes */ pcibios_add_platform_entries(pdev); +#ifdef HAVE_PCI_COHERENT_MMAP + pdev->coherent_attr = pci_init_coh_attr(pdev, "coherent_mem"); + pdev->coherent_wc_attr = pci_init_coh_attr(pdev, "coherent_mem_wc"); +#endif + return 0; err_rom: Index: linux/include/linux/pci.h =================================================================== --- linux.orig/include/linux/pci.h +++ linux/include/linux/pci.h @@ -56,7 +56,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. */ @@ -173,6 +174,8 @@ 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; + struct bin_attribute *coherent_wc_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);