1 // SPDX-License-Identifier: GPL-2.0
3 * Access to PCI I/O memory from user space programs.
5 * Copyright IBM Corp. 2014
8 #include <linux/kernel.h>
9 #include <linux/syscalls.h>
10 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/pci.h>
15 static long get_pfn(unsigned long user_addr, unsigned long access,
18 struct vm_area_struct *vma;
21 down_read(¤t->mm->mmap_sem);
23 vma = find_vma(current->mm, user_addr);
27 if (!(vma->vm_flags & access))
29 ret = follow_pfn(vma, user_addr, pfn);
31 up_read(¤t->mm->mmap_sem);
35 SYSCALL_DEFINE3(s390_pci_mmio_write, unsigned long, mmio_addr,
36 const void __user *, user_buffer, size_t, length)
39 void __iomem *io_addr;
44 if (!zpci_is_enabled())
47 if (length <= 0 || PAGE_SIZE - (mmio_addr & ~PAGE_MASK) < length)
50 buf = kmalloc(length, GFP_KERNEL);
56 ret = get_pfn(mmio_addr, VM_WRITE, &pfn);
59 io_addr = (void __iomem *)((pfn << PAGE_SHIFT) | (mmio_addr & ~PAGE_MASK));
62 if ((unsigned long) io_addr < ZPCI_IOMAP_ADDR_BASE)
65 if (copy_from_user(buf, user_buffer, length))
68 ret = zpci_memcpy_toio(io_addr, buf, length);
75 SYSCALL_DEFINE3(s390_pci_mmio_read, unsigned long, mmio_addr,
76 void __user *, user_buffer, size_t, length)
79 void __iomem *io_addr;
84 if (!zpci_is_enabled())
87 if (length <= 0 || PAGE_SIZE - (mmio_addr & ~PAGE_MASK) < length)
90 buf = kmalloc(length, GFP_KERNEL);
96 ret = get_pfn(mmio_addr, VM_READ, &pfn);
99 io_addr = (void __iomem *)((pfn << PAGE_SHIFT) | (mmio_addr & ~PAGE_MASK));
101 if ((unsigned long) io_addr < ZPCI_IOMAP_ADDR_BASE) {
105 ret = zpci_memcpy_fromio(buf, io_addr, length);
108 if (copy_to_user(user_buffer, buf, length))
112 if (buf != local_buf)