1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_PCI_IO_H
3 #define _ASM_S390_PCI_IO_H
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <asm/pci_insn.h>
11 /* I/O size constraints */
12 #define ZPCI_MAX_READ_SIZE 8
13 #define ZPCI_MAX_WRITE_SIZE 128
16 #define ZPCI_IOMAP_SHIFT 48
17 #define ZPCI_IOMAP_ADDR_BASE 0x8000000000000000UL
18 #define ZPCI_IOMAP_ADDR_OFF_MASK ((1UL << ZPCI_IOMAP_SHIFT) - 1)
19 #define ZPCI_IOMAP_MAX_ENTRIES \
20 ((ULONG_MAX - ZPCI_IOMAP_ADDR_BASE + 1) / (1UL << ZPCI_IOMAP_SHIFT))
21 #define ZPCI_IOMAP_ADDR_IDX_MASK \
22 (~ZPCI_IOMAP_ADDR_OFF_MASK - ZPCI_IOMAP_ADDR_BASE)
24 struct zpci_iomap_entry
{
30 extern struct zpci_iomap_entry
*zpci_iomap_start
;
32 #define ZPCI_ADDR(idx) (ZPCI_IOMAP_ADDR_BASE | ((u64) idx << ZPCI_IOMAP_SHIFT))
33 #define ZPCI_IDX(addr) \
34 (((__force u64) addr & ZPCI_IOMAP_ADDR_IDX_MASK) >> ZPCI_IOMAP_SHIFT)
35 #define ZPCI_OFFSET(addr) \
36 ((__force u64) addr & ZPCI_IOMAP_ADDR_OFF_MASK)
38 #define ZPCI_CREATE_REQ(handle, space, len) \
39 ((u64) handle << 32 | space << 16 | len)
41 #define zpci_read(LENGTH, RETTYPE) \
42 static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr) \
47 rc = zpci_load(&data, addr, LENGTH); \
50 return (RETTYPE) data; \
53 #define zpci_write(LENGTH, VALTYPE) \
54 static inline void zpci_write_##VALTYPE(VALTYPE val, \
55 const volatile void __iomem *addr) \
57 u64 data = (VALTYPE) val; \
59 zpci_store(addr, data, LENGTH); \
71 static inline int zpci_write_single(volatile void __iomem
*dst
, const void *src
,
78 val
= (u64
) *((u8
*) src
);
81 val
= (u64
) *((u16
*) src
);
84 val
= (u64
) *((u32
*) src
);
87 val
= (u64
) *((u64
*) src
);
90 val
= 0; /* let FW report error */
93 return zpci_store(dst
, val
, len
);
96 static inline int zpci_read_single(void *dst
, const volatile void __iomem
*src
,
102 cc
= zpci_load(&data
, src
, len
);
108 *((u8
*) dst
) = (u8
) data
;
111 *((u16
*) dst
) = (u16
) data
;
114 *((u32
*) dst
) = (u32
) data
;
117 *((u64
*) dst
) = (u64
) data
;
124 int zpci_write_block(volatile void __iomem
*dst
, const void *src
,
127 static inline u8
zpci_get_max_write_size(u64 src
, u64 dst
, int len
, int max
)
129 int count
= len
> max
? max
: len
, size
= 1;
131 while (!(src
& 0x1) && !(dst
& 0x1) && ((size
<< 1) <= count
)) {
139 static inline int zpci_memcpy_fromio(void *dst
,
140 const volatile void __iomem
*src
,
146 size
= zpci_get_max_write_size((u64 __force
) src
,
149 rc
= zpci_read_single(dst
, src
, size
);
159 static inline int zpci_memcpy_toio(volatile void __iomem
*dst
,
160 const void *src
, unsigned long n
)
168 size
= zpci_get_max_write_size((u64 __force
) dst
,
170 ZPCI_MAX_WRITE_SIZE
);
171 if (size
> 8) /* main path */
172 rc
= zpci_write_block(dst
, src
, size
);
174 rc
= zpci_write_single(dst
, src
, 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 */