blob: 1a9a98de5bdebc346c469f55818f08ed33b576e7 [file] [log] [blame]
Jan Glaubercd248342012-11-29 12:50:30 +01001#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 0x7fff
12#define ZPCI_IOMAP_ADDR_BASE 0x8000000000000000ULL
13#define ZPCI_IOMAP_ADDR_IDX_MASK 0x7fff000000000000ULL
14#define ZPCI_IOMAP_ADDR_OFF_MASK 0x0000ffffffffffffULL
15
16struct zpci_iomap_entry {
17 u32 fh;
18 u8 bar;
Michael S. Tsirkin8cfc99b2013-05-29 11:52:21 +093019 u16 count;
Jan Glaubercd248342012-11-29 12:50:30 +010020};
21
22extern struct zpci_iomap_entry *zpci_iomap_start;
23
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)
28
29#define ZPCI_CREATE_REQ(handle, space, len) \
30 ((u64) handle << 32 | space << 16 | len)
31
32#define zpci_read(LENGTH, RETTYPE) \
33static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr) \
34{ \
35 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
36 u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
37 u64 data; \
38 int rc; \
39 \
Martin Schwidefsky93893392013-06-25 14:52:23 +020040 rc = zpci_load(&data, req, ZPCI_OFFSET(addr)); \
Jan Glaubercd248342012-11-29 12:50:30 +010041 if (rc) \
42 data = -1ULL; \
43 return (RETTYPE) data; \
44}
45
46#define zpci_write(LENGTH, VALTYPE) \
47static inline void zpci_write_##VALTYPE(VALTYPE val, \
48 const volatile void __iomem *addr) \
49{ \
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; \
53 \
Martin Schwidefsky93893392013-06-25 14:52:23 +020054 zpci_store(data, req, ZPCI_OFFSET(addr)); \
Jan Glaubercd248342012-11-29 12:50:30 +010055}
56
57zpci_read(8, u64)
58zpci_read(4, u32)
59zpci_read(2, u16)
60zpci_read(1, u8)
61zpci_write(8, u64)
62zpci_write(4, u32)
63zpci_write(2, u16)
64zpci_write(1, u8)
65
66static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
67{
68 u64 val;
69
70 switch (len) {
71 case 1:
72 val = (u64) *((u8 *) data);
73 break;
74 case 2:
75 val = (u64) *((u16 *) data);
76 break;
77 case 4:
78 val = (u64) *((u32 *) data);
79 break;
80 case 8:
81 val = (u64) *((u64 *) data);
82 break;
83 default:
84 val = 0; /* let FW report error */
85 break;
86 }
Martin Schwidefsky93893392013-06-25 14:52:23 +020087 return zpci_store(val, req, offset);
Jan Glaubercd248342012-11-29 12:50:30 +010088}
89
90static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
91{
92 u64 data;
Sebastian Ottf0bacb72013-04-16 14:16:14 +020093 int cc;
Jan Glaubercd248342012-11-29 12:50:30 +010094
Martin Schwidefsky93893392013-06-25 14:52:23 +020095 cc = zpci_load(&data, req, offset);
Sebastian Ottb170bad2013-04-16 14:17:15 +020096 if (cc)
97 goto out;
98
Jan Glaubercd248342012-11-29 12:50:30 +010099 switch (len) {
100 case 1:
101 *((u8 *) dst) = (u8) data;
102 break;
103 case 2:
104 *((u16 *) dst) = (u16) data;
105 break;
106 case 4:
107 *((u32 *) dst) = (u32) data;
108 break;
109 case 8:
110 *((u64 *) dst) = (u64) data;
111 break;
112 }
Sebastian Ottb170bad2013-04-16 14:17:15 +0200113out:
Jan Glaubercd248342012-11-29 12:50:30 +0100114 return cc;
115}
116
117static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
118{
Martin Schwidefsky93893392013-06-25 14:52:23 +0200119 return zpci_store_block(data, req, offset);
Jan Glaubercd248342012-11-29 12:50:30 +0100120}
121
122static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
123{
124 int count = len > max ? max : len, size = 1;
125
126 while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
127 dst = dst >> 1;
128 src = src >> 1;
129 size = size << 1;
130 }
131 return size;
132}
133
134static inline int zpci_memcpy_fromio(void *dst,
135 const volatile void __iomem *src,
136 unsigned long n)
137{
138 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
139 u64 req, offset = ZPCI_OFFSET(src);
140 int size, rc = 0;
141
142 while (n > 0) {
Martin Schwidefsky5b9f2082014-10-30 10:30:45 +0100143 size = zpci_get_max_write_size((u64 __force) src,
144 (u64) dst, n, 8);
Jan Glaubercd248342012-11-29 12:50:30 +0100145 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
146 rc = zpci_read_single(req, dst, offset, size);
147 if (rc)
148 break;
149 offset += size;
150 dst += size;
151 n -= size;
152 }
153 return rc;
154}
155
156static inline int zpci_memcpy_toio(volatile void __iomem *dst,
157 const void *src, unsigned long n)
158{
159 struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
160 u64 req, offset = ZPCI_OFFSET(dst);
161 int size, rc = 0;
162
163 if (!src)
164 return -EINVAL;
165
166 while (n > 0) {
Martin Schwidefsky5b9f2082014-10-30 10:30:45 +0100167 size = zpci_get_max_write_size((u64 __force) dst,
168 (u64) src, n, 128);
Jan Glaubercd248342012-11-29 12:50:30 +0100169 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
170
171 if (size > 8) /* main path */
172 rc = zpci_write_block(req, src, offset);
173 else
174 rc = zpci_write_single(req, src, offset, size);
175 if (rc)
176 break;
177 offset += size;
178 src += size;
179 n -= size;
180 }
181 return rc;
182}
183
184static inline int zpci_memset_io(volatile void __iomem *dst,
185 unsigned char val, size_t count)
186{
187 u8 *src = kmalloc(count, GFP_KERNEL);
188 int rc;
189
190 if (src == NULL)
191 return -ENOMEM;
192 memset(src, val, count);
193
194 rc = zpci_memcpy_toio(dst, src, count);
195 kfree(src);
196 return rc;
197}
198
199#endif /* CONFIG_PCI */
200
201#endif /* _ASM_S390_PCI_IO_H */