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 0x7fff
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_IDX(addr) \
25 (((__force u64) addr & ZPCI_IOMAP_ADDR_IDX_MASK) >> 48)
26 #define ZPCI_OFFSET(addr) \
27 ((__force u64) addr & ZPCI_IOMAP_ADDR_OFF_MASK)
29 #define ZPCI_CREATE_REQ(handle, space, len) \
30 ((u64) handle << 32 | space << 16 | len)
32 #define zpci_read(LENGTH, RETTYPE) \
33 static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr) \
35 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
36 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
40 rc = zpci_load(&data, req, ZPCI_OFFSET(addr)); \
43 return (RETTYPE) data; \
46 #define zpci_write(LENGTH, VALTYPE) \
47 static inline void zpci_write_##VALTYPE(VALTYPE val, \
48 const volatile void __iomem *addr) \
50 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
51 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
52 u64 data = (VALTYPE) val; \
54 zpci_store(data, req, ZPCI_OFFSET(addr)); \
66 static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
72 val = (u64) *((u8 *) data);
75 val = (u64) *((u16 *) data);
78 val = (u64) *((u32 *) data);
81 val = (u64) *((u64 *) data);
84 val = 0; /* let FW report error */
87 return zpci_store(val, req, offset);
90 static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
95 cc = zpci_load(&data, req, offset);
101 *((u8 *) dst) = (u8) data;
104 *((u16 *) dst) = (u16) data;
107 *((u32 *) dst) = (u32) data;
110 *((u64 *) dst) = (u64) data;
117 static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
119 return zpci_store_block(data, req, offset);
122 static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
124 int count = len > max ? max : len, size = 1;
126 while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
134 static inline int zpci_memcpy_fromio(void *dst,
135 const volatile void __iomem *src,
138 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
139 u64 req, offset = ZPCI_OFFSET(src);
143 size = zpci_get_max_write_size((u64 __force) src,
145 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
146 rc = zpci_read_single(req, dst, offset, size);
156 static inline int zpci_memcpy_toio(volatile void __iomem *dst,
157 const void *src, unsigned long n)
159 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
160 u64 req, offset = ZPCI_OFFSET(dst);
167 size = zpci_get_max_write_size((u64 __force) dst,
169 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
171 if (size > 8) /* main path */
172 rc = zpci_write_block(req, src, offset);
174 rc = zpci_write_single(req, src, offset, size);
184 static inline int zpci_memset_io(volatile void __iomem *dst,
185 unsigned char val, size_t count)
187 u8 *src = kmalloc(count, GFP_KERNEL);
192 memset(src, val, count);
194 rc = zpci_memcpy_toio(dst, src, count);
199 #endif /* CONFIG_PCI */
201 #endif /* _ASM_S390_PCI_IO_H */