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 {
21 extern struct zpci_iomap_entry *zpci_iomap_start;
23 #define ZPCI_IDX(addr) \
24 (((__force u64) addr & ZPCI_IOMAP_ADDR_IDX_MASK) >> 48)
25 #define ZPCI_OFFSET(addr) \
26 ((__force u64) addr & ZPCI_IOMAP_ADDR_OFF_MASK)
28 #define ZPCI_CREATE_REQ(handle, space, len) \
29 ((u64) handle << 32 | space << 16 | len)
31 #define zpci_read(LENGTH, RETTYPE) \
32 static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr) \
34 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
35 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
39 rc = s390pci_load(&data, req, ZPCI_OFFSET(addr)); \
42 return (RETTYPE) data; \
45 #define zpci_write(LENGTH, VALTYPE) \
46 static inline void zpci_write_##VALTYPE(VALTYPE val, \
47 const volatile void __iomem *addr) \
49 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
50 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
51 u64 data = (VALTYPE) val; \
53 s390pci_store(data, req, ZPCI_OFFSET(addr)); \
65 static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
71 val = (u64) *((u8 *) data);
74 val = (u64) *((u16 *) data);
77 val = (u64) *((u32 *) data);
80 val = (u64) *((u64 *) data);
83 val = 0; /* let FW report error */
86 return s390pci_store(val, req, offset);
89 static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
94 cc = s390pci_load(&data, req, offset);
100 *((u8 *) dst) = (u8) data;
103 *((u16 *) dst) = (u16) data;
106 *((u32 *) dst) = (u32) data;
109 *((u64 *) dst) = (u64) data;
116 static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
118 return s390pci_store_block(data, req, offset);
121 static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
123 int count = len > max ? max : len, size = 1;
125 while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
133 static inline int zpci_memcpy_fromio(void *dst,
134 const volatile void __iomem *src,
137 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
138 u64 req, offset = ZPCI_OFFSET(src);
142 size = zpci_get_max_write_size((u64) src, (u64) dst, n, 8);
143 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
144 rc = zpci_read_single(req, dst, offset, size);
154 static inline int zpci_memcpy_toio(volatile void __iomem *dst,
155 const void *src, unsigned long n)
157 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
158 u64 req, offset = ZPCI_OFFSET(dst);
165 size = zpci_get_max_write_size((u64) dst, (u64) src, n, 128);
166 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
168 if (size > 8) /* main path */
169 rc = zpci_write_block(req, src, offset);
171 rc = zpci_write_single(req, src, offset, size);
181 static inline int zpci_memset_io(volatile void __iomem *dst,
182 unsigned char val, size_t count)
184 u8 *src = kmalloc(count, GFP_KERNEL);
189 memset(src, val, count);
191 rc = zpci_memcpy_toio(dst, src, count);
196 #endif /* CONFIG_PCI */
198 #endif /* _ASM_S390_PCI_IO_H */