xref: /freebsd/contrib/ofed/libmlx4/mmio.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 /* Licensed under the OpenIB.org BSD license (FreeBSD Variant) - See COPYING.md
2  */
3 #ifndef MMIO_H
4 #define MMIO_H
5 
6 #include <unistd.h>
7 #include <sys/syscall.h>
8 #ifdef __s390x__
9 
10 static inline long mmio_writeb(const unsigned long mmio_addr,
11 			       const uint8_t val)
12 {
13 	return syscall(__NR_s390_pci_mmio_write, mmio_addr, &val, sizeof(val));
14 }
15 
16 static inline long mmio_writew(const unsigned long mmio_addr,
17 			       const uint16_t val)
18 {
19 	return syscall(__NR_s390_pci_mmio_write, mmio_addr, &val, sizeof(val));
20 }
21 
22 static inline long mmio_writel(const unsigned long mmio_addr,
23 			       const uint32_t val)
24 {
25 	return syscall(__NR_s390_pci_mmio_write, mmio_addr, &val, sizeof(val));
26 }
27 
28 static inline long mmio_writeq(const unsigned long mmio_addr,
29 			       const uint64_t val)
30 {
31 	return syscall(__NR_s390_pci_mmio_write, mmio_addr, &val, sizeof(val));
32 }
33 
34 static inline long mmio_write(const unsigned long mmio_addr,
35 			      const void *val,
36 			      const size_t length)
37 {
38 	return syscall(__NR_s390_pci_mmio_write, mmio_addr, val, length);
39 }
40 
41 static inline long mmio_readb(const unsigned long mmio_addr, uint8_t *val)
42 {
43 	return syscall(__NR_s390_pci_mmio_read, mmio_addr, val, sizeof(*val));
44 }
45 
46 static inline long mmio_readw(const unsigned long mmio_addr, uint16_t *val)
47 {
48 	return syscall(__NR_s390_pci_mmio_read, mmio_addr, val, sizeof(*val));
49 }
50 
51 static inline long mmio_readl(const unsigned long mmio_addr, uint32_t *val)
52 {
53 	return syscall(__NR_s390_pci_mmio_read, mmio_addr, val, sizeof(*val));
54 }
55 
56 static inline long mmio_readq(const unsigned long mmio_addr, uint64_t *val)
57 {
58 	return syscall(__NR_s390_pci_mmio_read, mmio_addr, val, sizeof(*val));
59 }
60 
61 static inline long mmio_read(const unsigned long mmio_addr,
62 			     void *val,
63 			     const size_t length)
64 {
65 	return syscall(__NR_s390_pci_mmio_read, mmio_addr, val, length);
66 }
67 
68 static inline void mlx4_bf_copy(unsigned long *dst,
69 				unsigned long *src,
70 				unsigned bytecnt)
71 {
72 	mmio_write((unsigned long)dst, src, bytecnt);
73 }
74 
75 #else
76 
77 #define mmio_writeb(addr, value) \
78 	(*((volatile uint8_t *)addr) = value)
79 #define mmio_writew(addr, value) \
80 	(*((volatile uint16_t *)addr) = value)
81 #define mmio_writel(addr, value) \
82 	(*((volatile uint32_t *)addr) = value)
83 #define mmio_writeq(addr, value) \
84 	(*((volatile uint64_t *)addr) = value)
85 #define mmio_write(addr, value, length) \
86 	memcpy(addr, value, length)
87 
88 #define mmio_readb(addr, value) \
89 	(value = *((volatile uint8_t *)addr))
90 #define mmio_readw(addr, value) \
91 	(value = *((volatile uint16_t *)addr))
92 #define mmio_readl(addr, value) \
93 	(value = *((volatile uint32_t *)addr))
94 #define mmio_readq(addr, value) \
95 	(value = *((volatile uint64_t *)addr))
96 #define mmio_read(addr, value, length) \
97 	memcpy(value, addr, length)
98 
99 /*
100  * Avoid using memcpy() to copy to BlueFlame page, since memcpy()
101  * implementations may use move-string-buffer assembler instructions,
102  * which do not guarantee order of copying.
103  */
104 static inline void mlx4_bf_copy(unsigned long *dst,
105 				unsigned long *src,
106 				unsigned bytecnt)
107 {
108 	while (bytecnt > 0) {
109 		*dst++ = *src++;
110 		*dst++ = *src++;
111 		bytecnt -= 2 * sizeof(long);
112 	}
113 }
114 #endif
115 
116 #endif
117