Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Access to PCI I/O memory from user space programs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright IBM Corp. 2014
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author(s): Alexey Ishchuk <aishchuk@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/pci_io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/pci_debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static inline void zpci_err_mmio(u8 cc, u8 status, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		u8 cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	} data = {offset, cc, status};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	zpci_err_hex(&data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static inline int __pcistb_mio_inuser(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		void __iomem *ioaddr, const void __user *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		u64 len, u8 *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int cc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	asm volatile (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		"       sacf 256\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		"0:     .insn   rsy,0xeb00000000d4,%[len],%[ioaddr],%[src]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		"1:     ipm     %[cc]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		"       srl     %[cc],28\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		"2:     sacf 768\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		: [cc] "+d" (cc), [len] "+d" (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		: [ioaddr] "a" (ioaddr), [src] "Q" (*((u8 __force *)src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		: "cc", "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	*status = len >> 24 & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	return cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static inline int __pcistg_mio_inuser(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		void __iomem *ioaddr, const void __user *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		u64 ulen, u8 *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	register u64 addr asm("2") = (u64 __force) ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	register u64 len asm("3") = ulen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int cc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	u64 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	u64 cnt = ulen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u8 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 * copy 0 < @len <= 8 bytes from @src into the right most bytes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 * a register, then store it to PCI at @ioaddr while in secondary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * address space. pcistg then uses the user mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	asm volatile (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		"       sacf    256\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		"0:     llgc    %[tmp],0(%[src])\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		"       sllg    %[val],%[val],8\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		"       aghi    %[src],1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		"       ogr     %[val],%[tmp]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		"       brctg   %[cnt],0b\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		"1:     .insn   rre,0xb9d40000,%[val],%[ioaddr]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		"2:     ipm     %[cc]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		"       srl     %[cc],28\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		"3:     sacf    768\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		EX_TABLE(0b, 3b) EX_TABLE(1b, 3b) EX_TABLE(2b, 3b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		[src] "+a" (src), [cnt] "+d" (cnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		[val] "+d" (val), [tmp] "=d" (tmp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		[len] "+d" (len), [cc] "+d" (cc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		[ioaddr] "+a" (addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		:: "cc", "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	*status = len >> 24 & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* did we read everything from user memory? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!cc && cnt != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		cc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static inline int __memcpy_toio_inuser(void __iomem *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 				   const void __user *src, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int size, rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u8 status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	mm_segment_t old_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (!src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	old_fs = enable_sacf_uaccess();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	while (n > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		size = zpci_get_max_write_size((u64 __force) dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 					       (u64 __force) src, n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 					       ZPCI_MAX_WRITE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		if (size > 8) /* main path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			rc = __pcistb_mio_inuser(dst, src, size, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			rc = __pcistg_mio_inuser(dst, src, size, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		src += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		dst += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		n -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	disable_sacf_uaccess(old_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		zpci_err_mmio(rc, status, (__force u64) dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static long get_pfn(unsigned long user_addr, unsigned long access,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		    unsigned long *pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	mmap_read_lock(current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	vma = find_vma(current->mm, user_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (!vma || user_addr < vma->vm_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (!(vma->vm_flags & access))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	ret = follow_pfn(vma, user_addr, pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	mmap_read_unlock(current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) SYSCALL_DEFINE3(s390_pci_mmio_write, unsigned long, mmio_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		const void __user *, user_buffer, size_t, length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	u8 local_buf[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	void __iomem *io_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (!zpci_is_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (length <= 0 || PAGE_SIZE - (mmio_addr & ~PAGE_MASK) < length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * We only support write access to MIO capable devices if we are on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 * a MIO enabled system. Otherwise we would have to check for every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	 * address if it is a special ZPCI_ADDR and would have to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	 * a get_pfn() which we don't need for MIO capable devices.  Currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	 * ISM devices are the only devices without MIO support and there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	 * known need for accessing these from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (static_branch_likely(&have_mio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		ret = __memcpy_toio_inuser((void  __iomem *) mmio_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 					user_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 					length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (length > 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		buf = kmalloc(length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		buf = local_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	ret = get_pfn(mmio_addr, VM_WRITE, &pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	io_addr = (void __iomem *)((pfn << PAGE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			(mmio_addr & ~PAGE_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if ((unsigned long) io_addr < ZPCI_IOMAP_ADDR_BASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (copy_from_user(buf, user_buffer, length))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	ret = zpci_memcpy_toio(io_addr, buf, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (buf != local_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static inline int __pcilg_mio_inuser(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		void __user *dst, const void __iomem *ioaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		u64 ulen, u8 *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	register u64 addr asm("2") = (u64 __force) ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	register u64 len asm("3") = ulen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	u64 cnt = ulen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	int shift = ulen * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int cc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	u64 val, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * read 0 < @len <= 8 bytes from the PCI memory mapped at @ioaddr (in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 * user space) into a register using pcilg then store these bytes at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * user address @dst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	asm volatile (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		"       sacf    256\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		"0:     .insn   rre,0xb9d60000,%[val],%[ioaddr]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		"1:     ipm     %[cc]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		"       srl     %[cc],28\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		"       ltr     %[cc],%[cc]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		"       jne     4f\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		"2:     ahi     %[shift],-8\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		"       srlg    %[tmp],%[val],0(%[shift])\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		"3:     stc     %[tmp],0(%[dst])\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		"       aghi    %[dst],1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		"       brctg   %[cnt],2b\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		"4:     sacf    768\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		EX_TABLE(0b, 4b) EX_TABLE(1b, 4b) EX_TABLE(3b, 4b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		[cc] "+d" (cc), [val] "=d" (val), [len] "+d" (len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		[dst] "+a" (dst), [cnt] "+d" (cnt), [tmp] "=d" (tmp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		[shift] "+d" (shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		[ioaddr] "a" (addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		: "cc", "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	/* did we write everything to the user space buffer? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!cc && cnt != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		cc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	*status = len >> 24 & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static inline int __memcpy_fromio_inuser(void __user *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 				     const void __iomem *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				     unsigned long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	int size, rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	mm_segment_t old_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	old_fs = enable_sacf_uaccess();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	while (n > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		size = zpci_get_max_write_size((u64 __force) src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 					       (u64 __force) dst, n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 					       ZPCI_MAX_READ_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		rc = __pcilg_mio_inuser(dst, src, size, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		src += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		dst += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		n -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	disable_sacf_uaccess(old_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		zpci_err_mmio(rc, status, (__force u64) dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) SYSCALL_DEFINE3(s390_pci_mmio_read, unsigned long, mmio_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		void __user *, user_buffer, size_t, length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	u8 local_buf[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	void __iomem *io_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (!zpci_is_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (length <= 0 || PAGE_SIZE - (mmio_addr & ~PAGE_MASK) < length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * We only support read access to MIO capable devices if we are on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * a MIO enabled system. Otherwise we would have to check for every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * address if it is a special ZPCI_ADDR and would have to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * a get_pfn() which we don't need for MIO capable devices.  Currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * ISM devices are the only devices without MIO support and there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 * known need for accessing these from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (static_branch_likely(&have_mio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		ret = __memcpy_fromio_inuser(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				user_buffer, (const void __iomem *)mmio_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 				length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (length > 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		buf = kmalloc(length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		buf = local_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	ret = get_pfn(mmio_addr, VM_READ, &pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	io_addr = (void __iomem *)((pfn << PAGE_SHIFT) | (mmio_addr & ~PAGE_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if ((unsigned long) io_addr < ZPCI_IOMAP_ADDR_BASE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	ret = zpci_memcpy_fromio(buf, io_addr, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (copy_to_user(user_buffer, buf, length))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (buf != local_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }