1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_PCI_IO_H
3 #define _ASM_S390_PCI_IO_H
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <asm/pci_insn.h>
12 #define ZPCI_IOMAP_SHIFT 48
13 #define ZPCI_IOMAP_ADDR_BASE 0x8000000000000000UL
14 #define ZPCI_IOMAP_ADDR_OFF_MASK ((1UL << ZPCI_IOMAP_SHIFT) - 1)
15 #define ZPCI_IOMAP_MAX_ENTRIES \
16 ((ULONG_MAX - ZPCI_IOMAP_ADDR_BASE + 1) / (1UL << ZPCI_IOMAP_SHIFT))
17 #define ZPCI_IOMAP_ADDR_IDX_MASK \
18 (~ZPCI_IOMAP_ADDR_OFF_MASK - ZPCI_IOMAP_ADDR_BASE)
20 struct zpci_iomap_entry {
26 extern struct zpci_iomap_entry *zpci_iomap_start;
28 #define ZPCI_ADDR(idx) (ZPCI_IOMAP_ADDR_BASE | ((u64) idx << ZPCI_IOMAP_SHIFT))
29 #define ZPCI_IDX(addr) \
30 (((__force u64) addr & ZPCI_IOMAP_ADDR_IDX_MASK) >> ZPCI_IOMAP_SHIFT)
31 #define ZPCI_OFFSET(addr) \
32 ((__force u64) addr & ZPCI_IOMAP_ADDR_OFF_MASK)
34 #define ZPCI_CREATE_REQ(handle, space, len) \
35 ((u64) handle << 32 | space << 16 | len)
37 #define zpci_read(LENGTH, RETTYPE) \
38 static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr) \
40 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
41 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
45 rc = zpci_load(&data, req, ZPCI_OFFSET(addr)); \
48 return (RETTYPE) data; \
51 #define zpci_write(LENGTH, VALTYPE) \
52 static inline void zpci_write_##VALTYPE(VALTYPE val, \
53 const volatile void __iomem *addr) \
55 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
56 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
57 u64 data = (VALTYPE) val; \
59 zpci_store(data, req, ZPCI_OFFSET(addr)); \
71 static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
77 val = (u64) *((u8 *) data);
80 val = (u64) *((u16 *) data);
83 val = (u64) *((u32 *) data);
86 val = (u64) *((u64 *) data);
89 val = 0; /* let FW report error */
92 return zpci_store(val, req, offset);
95 static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
100 cc = zpci_load(&data, req, offset);
106 *((u8 *) dst) = (u8) data;
109 *((u16 *) dst) = (u16) data;
112 *((u32 *) dst) = (u32) data;
115 *((u64 *) dst) = (u64) data;
122 static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
124 return zpci_store_block(data, req, offset);
127 static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
129 int count = len > max ? max : len, size = 1;
131 while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
139 static inline int zpci_memcpy_fromio(void *dst,
140 const volatile void __iomem *src,
143 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
144 u64 req, offset = ZPCI_OFFSET(src);
148 size = zpci_get_max_write_size((u64 __force) src,
150 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
151 rc = zpci_read_single(req, dst, offset, size);
161 static inline int zpci_memcpy_toio(volatile void __iomem *dst,
162 const void *src, unsigned long n)
164 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
165 u64 req, offset = ZPCI_OFFSET(dst);
172 size = zpci_get_max_write_size((u64 __force) dst,
174 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
176 if (size > 8) /* main path */
177 rc = zpci_write_block(req, src, offset);
179 rc = zpci_write_single(req, src, offset, size);
189 static inline int zpci_memset_io(volatile void __iomem *dst,
190 unsigned char val, size_t count)
192 u8 *src = kmalloc(count, GFP_KERNEL);
197 memset(src, val, count);
199 rc = zpci_memcpy_toio(dst, src, count);
204 #endif /* CONFIG_PCI */
206 #endif /* _ASM_S390_PCI_IO_H */