2 * Access to PCI I/O memory from user space programs.
4 * Copyright IBM Corp. 2014
5 * Author(s): Alexey Ishchuk <aishchuk@linux.vnet.ibm.com>
7 #include <linux/kernel.h>
8 #include <linux/syscalls.h>
9 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/pci.h>
14 static long get_pfn(unsigned long user_addr, unsigned long access,
17 struct vm_area_struct *vma;
20 down_read(¤t->mm->mmap_sem);
22 vma = find_vma(current->mm, user_addr);
26 if (!(vma->vm_flags & access))
28 ret = follow_pfn(vma, user_addr, pfn);
30 up_read(¤t->mm->mmap_sem);
34 SYSCALL_DEFINE3(s390_pci_mmio_write, unsigned long, mmio_addr,
35 const void __user *, user_buffer, size_t, length)
38 void __iomem *io_addr;
43 if (!zpci_is_enabled())
46 if (length <= 0 || PAGE_SIZE - (mmio_addr & ~PAGE_MASK) < length)
49 buf = kmalloc(length, GFP_KERNEL);
55 ret = get_pfn(mmio_addr, VM_WRITE, &pfn);
58 io_addr = (void __iomem *)((pfn << PAGE_SHIFT) | (mmio_addr & ~PAGE_MASK));
61 if ((unsigned long) io_addr < ZPCI_IOMAP_ADDR_BASE)
64 if (copy_from_user(buf, user_buffer, length))
67 ret = zpci_memcpy_toio(io_addr, buf, length);
74 SYSCALL_DEFINE3(s390_pci_mmio_read, unsigned long, mmio_addr,
75 void __user *, user_buffer, size_t, length)
78 void __iomem *io_addr;
83 if (!zpci_is_enabled())
86 if (length <= 0 || PAGE_SIZE - (mmio_addr & ~PAGE_MASK) < length)
89 buf = kmalloc(length, GFP_KERNEL);
95 ret = get_pfn(mmio_addr, VM_READ, &pfn);
98 io_addr = (void __iomem *)((pfn << PAGE_SHIFT) | (mmio_addr & ~PAGE_MASK));
100 if ((unsigned long) io_addr < ZPCI_IOMAP_ADDR_BASE) {
104 ret = zpci_memcpy_fromio(buf, io_addr, length);
107 if (copy_to_user(user_buffer, buf, length))
111 if (buf != local_buf)