1 #ifndef _ASM_S390_PCI_IO_H
2 #define _ASM_S390_PCI_IO_H
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <asm/pci_insn.h>
11 #define ZPCI_IOMAP_MAX_ENTRIES 0x8000
12 #define ZPCI_IOMAP_ADDR_BASE 0x8000000000000000ULL
13 #define ZPCI_IOMAP_ADDR_IDX_MASK 0x7fff000000000000ULL
14 #define ZPCI_IOMAP_ADDR_OFF_MASK 0x0000ffffffffffffULL
16 struct zpci_iomap_entry {
22 extern struct zpci_iomap_entry *zpci_iomap_start;
24 #define ZPCI_ADDR(idx) (ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48))
25 #define ZPCI_IDX(addr) \
26 (((__force u64) addr & ZPCI_IOMAP_ADDR_IDX_MASK) >> 48)
27 #define ZPCI_OFFSET(addr) \
28 ((__force u64) addr & ZPCI_IOMAP_ADDR_OFF_MASK)
30 #define ZPCI_CREATE_REQ(handle, space, len) \
31 ((u64) handle << 32 | space << 16 | len)
33 #define zpci_read(LENGTH, RETTYPE) \
34 static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr) \
36 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
37 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
41 rc = zpci_load(&data, req, ZPCI_OFFSET(addr)); \
44 return (RETTYPE) data; \
47 #define zpci_write(LENGTH, VALTYPE) \
48 static inline void zpci_write_##VALTYPE(VALTYPE val, \
49 const volatile void __iomem *addr) \
51 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
52 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
53 u64 data = (VALTYPE) val; \
55 zpci_store(data, req, ZPCI_OFFSET(addr)); \
67 static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
73 val = (u64) *((u8 *) data);
76 val = (u64) *((u16 *) data);
79 val = (u64) *((u32 *) data);
82 val = (u64) *((u64 *) data);
85 val = 0; /* let FW report error */
88 return zpci_store(val, req, offset);
91 static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
96 cc = zpci_load(&data, req, offset);
102 *((u8 *) dst) = (u8) data;
105 *((u16 *) dst) = (u16) data;
108 *((u32 *) dst) = (u32) data;
111 *((u64 *) dst) = (u64) data;
118 static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
120 return zpci_store_block(data, req, offset);
123 static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
125 int count = len > max ? max : len, size = 1;
127 while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
135 static inline int zpci_memcpy_fromio(void *dst,
136 const volatile void __iomem *src,
139 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
140 u64 req, offset = ZPCI_OFFSET(src);
144 size = zpci_get_max_write_size((u64 __force) src,
146 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
147 rc = zpci_read_single(req, dst, offset, size);
157 static inline int zpci_memcpy_toio(volatile void __iomem *dst,
158 const void *src, unsigned long n)
160 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
161 u64 req, offset = ZPCI_OFFSET(dst);
168 size = zpci_get_max_write_size((u64 __force) dst,
170 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
172 if (size > 8) /* main path */
173 rc = zpci_write_block(req, src, offset);
175 rc = zpci_write_single(req, src, offset, size);
185 static inline int zpci_memset_io(volatile void __iomem *dst,
186 unsigned char val, size_t count)
188 u8 *src = kmalloc(count, GFP_KERNEL);
193 memset(src, val, count);
195 rc = zpci_memcpy_toio(dst, src, count);
200 #endif /* CONFIG_PCI */
202 #endif /* _ASM_S390_PCI_IO_H */