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