27dbc1129dfc7eb9c974ed5f3e391dfe7e7e535b
[cascardo/linux.git] / arch / s390 / include / asm / pci_io.h
1 #ifndef _ASM_S390_PCI_IO_H
2 #define _ASM_S390_PCI_IO_H
3
4 #ifdef CONFIG_PCI
5
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <asm/pci_insn.h>
9
10 /* I/O Map */
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
15
16 struct zpci_iomap_entry {
17         u32 fh;
18         u8 bar;
19         u16 count;
20 };
21
22 extern struct zpci_iomap_entry *zpci_iomap_start;
23
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)
29
30 #define ZPCI_CREATE_REQ(handle, space, len)                                     \
31         ((u64) handle << 32 | space << 16 | len)
32
33 #define zpci_read(LENGTH, RETTYPE)                                              \
34 static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr)    \
35 {                                                                               \
36         struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)];     \
37         u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH);               \
38         u64 data;                                                               \
39         int rc;                                                                 \
40                                                                                 \
41         rc = zpci_load(&data, req, ZPCI_OFFSET(addr));                          \
42         if (rc)                                                                 \
43                 data = -1ULL;                                                   \
44         return (RETTYPE) data;                                                  \
45 }
46
47 #define zpci_write(LENGTH, VALTYPE)                                             \
48 static inline void zpci_write_##VALTYPE(VALTYPE val,                            \
49                                         const volatile void __iomem *addr)      \
50 {                                                                               \
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;                                               \
54                                                                                 \
55         zpci_store(data, req, ZPCI_OFFSET(addr));                               \
56 }
57
58 zpci_read(8, u64)
59 zpci_read(4, u32)
60 zpci_read(2, u16)
61 zpci_read(1, u8)
62 zpci_write(8, u64)
63 zpci_write(4, u32)
64 zpci_write(2, u16)
65 zpci_write(1, u8)
66
67 static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
68 {
69         u64 val;
70
71         switch (len) {
72         case 1:
73                 val = (u64) *((u8 *) data);
74                 break;
75         case 2:
76                 val = (u64) *((u16 *) data);
77                 break;
78         case 4:
79                 val = (u64) *((u32 *) data);
80                 break;
81         case 8:
82                 val = (u64) *((u64 *) data);
83                 break;
84         default:
85                 val = 0;                /* let FW report error */
86                 break;
87         }
88         return zpci_store(val, req, offset);
89 }
90
91 static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
92 {
93         u64 data;
94         int cc;
95
96         cc = zpci_load(&data, req, offset);
97         if (cc)
98                 goto out;
99
100         switch (len) {
101         case 1:
102                 *((u8 *) dst) = (u8) data;
103                 break;
104         case 2:
105                 *((u16 *) dst) = (u16) data;
106                 break;
107         case 4:
108                 *((u32 *) dst) = (u32) data;
109                 break;
110         case 8:
111                 *((u64 *) dst) = (u64) data;
112                 break;
113         }
114 out:
115         return cc;
116 }
117
118 static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
119 {
120         return zpci_store_block(data, req, offset);
121 }
122
123 static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
124 {
125         int count = len > max ? max : len, size = 1;
126
127         while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
128                 dst = dst >> 1;
129                 src = src >> 1;
130                 size = size << 1;
131         }
132         return size;
133 }
134
135 static inline int zpci_memcpy_fromio(void *dst,
136                                      const volatile void __iomem *src,
137                                      unsigned long n)
138 {
139         struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
140         u64 req, offset = ZPCI_OFFSET(src);
141         int size, rc = 0;
142
143         while (n > 0) {
144                 size = zpci_get_max_write_size((u64 __force) src,
145                                                (u64) dst, n, 8);
146                 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
147                 rc = zpci_read_single(req, dst, offset, size);
148                 if (rc)
149                         break;
150                 offset += size;
151                 dst += size;
152                 n -= size;
153         }
154         return rc;
155 }
156
157 static inline int zpci_memcpy_toio(volatile void __iomem *dst,
158                                    const void *src, unsigned long n)
159 {
160         struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
161         u64 req, offset = ZPCI_OFFSET(dst);
162         int size, rc = 0;
163
164         if (!src)
165                 return -EINVAL;
166
167         while (n > 0) {
168                 size = zpci_get_max_write_size((u64 __force) dst,
169                                                (u64) src, n, 128);
170                 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
171
172                 if (size > 8) /* main path */
173                         rc = zpci_write_block(req, src, offset);
174                 else
175                         rc = zpci_write_single(req, src, offset, size);
176                 if (rc)
177                         break;
178                 offset += size;
179                 src += size;
180                 n -= size;
181         }
182         return rc;
183 }
184
185 static inline int zpci_memset_io(volatile void __iomem *dst,
186                                  unsigned char val, size_t count)
187 {
188         u8 *src = kmalloc(count, GFP_KERNEL);
189         int rc;
190
191         if (src == NULL)
192                 return -ENOMEM;
193         memset(src, val, count);
194
195         rc = zpci_memcpy_toio(dst, src, count);
196         kfree(src);
197         return rc;
198 }
199
200 #endif /* CONFIG_PCI */
201
202 #endif /* _ASM_S390_PCI_IO_H */