1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
7 #ifndef _ASM_POWERPC_DCR_NATIVE_H
8 #define _ASM_POWERPC_DCR_NATIVE_H
12 #include <linux/spinlock.h>
13 #include <asm/cputable.h>
14 #include <asm/cpu_has_feature.h>
15 #include <linux/stringify.h>
21 static inline bool dcr_map_ok_native(dcr_host_native_t host
)
26 #define dcr_map_native(dev, dcr_n, dcr_c) \
27 ((dcr_host_native_t){ .base = (dcr_n) })
28 #define dcr_unmap_native(host, dcr_c) do {} while (0)
29 #define dcr_read_native(host, dcr_n) mfdcr(dcr_n + host.base)
30 #define dcr_write_native(host, dcr_n, value) mtdcr(dcr_n + host.base, value)
32 /* Table based DCR accessors */
33 extern void __mtdcr(unsigned int reg
, unsigned int val
);
34 extern unsigned int __mfdcr(unsigned int reg
);
36 /* mfdcrx/mtdcrx instruction based accessors. We hand code
37 * the opcodes in order not to depend on newer binutils
39 static inline unsigned int mfdcrx(unsigned int reg
)
42 asm volatile(".long 0x7c000206 | (%0 << 21) | (%1 << 16)"
43 : "=r" (ret
) : "r" (reg
));
47 static inline void mtdcrx(unsigned int reg
, unsigned int val
)
49 asm volatile(".long 0x7c000306 | (%0 << 21) | (%1 << 16)"
50 : : "r" (val
), "r" (reg
));
54 ({unsigned int rval; \
55 if (__builtin_constant_p(rn) && rn < 1024) \
56 asm volatile("mfdcr %0," __stringify(rn) \
58 else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
64 #define mtdcr(rn, v) \
66 if (__builtin_constant_p(rn) && rn < 1024) \
67 asm volatile("mtdcr " __stringify(rn) ",%0" \
69 else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
75 /* R/W of indirect DCRs make use of standard naming conventions for DCRs */
76 extern spinlock_t dcr_ind_lock
;
78 static inline unsigned __mfdcri(int base_addr
, int base_data
, int reg
)
83 spin_lock_irqsave(&dcr_ind_lock
, flags
);
84 if (cpu_has_feature(CPU_FTR_INDEXED_DCR
)) {
85 mtdcrx(base_addr
, reg
);
86 val
= mfdcrx(base_data
);
88 __mtdcr(base_addr
, reg
);
89 val
= __mfdcr(base_data
);
91 spin_unlock_irqrestore(&dcr_ind_lock
, flags
);
95 static inline void __mtdcri(int base_addr
, int base_data
, int reg
,
100 spin_lock_irqsave(&dcr_ind_lock
, flags
);
101 if (cpu_has_feature(CPU_FTR_INDEXED_DCR
)) {
102 mtdcrx(base_addr
, reg
);
103 mtdcrx(base_data
, val
);
105 __mtdcr(base_addr
, reg
);
106 __mtdcr(base_data
, val
);
108 spin_unlock_irqrestore(&dcr_ind_lock
, flags
);
111 static inline void __dcri_clrset(int base_addr
, int base_data
, int reg
,
112 unsigned clr
, unsigned set
)
117 spin_lock_irqsave(&dcr_ind_lock
, flags
);
118 if (cpu_has_feature(CPU_FTR_INDEXED_DCR
)) {
119 mtdcrx(base_addr
, reg
);
120 val
= (mfdcrx(base_data
) & ~clr
) | set
;
121 mtdcrx(base_data
, val
);
123 __mtdcr(base_addr
, reg
);
124 val
= (__mfdcr(base_data
) & ~clr
) | set
;
125 __mtdcr(base_data
, val
);
127 spin_unlock_irqrestore(&dcr_ind_lock
, flags
);
130 #define mfdcri(base, reg) __mfdcri(DCRN_ ## base ## _CONFIG_ADDR, \
131 DCRN_ ## base ## _CONFIG_DATA, \
134 #define mtdcri(base, reg, data) __mtdcri(DCRN_ ## base ## _CONFIG_ADDR, \
135 DCRN_ ## base ## _CONFIG_DATA, \
138 #define dcri_clrset(base, reg, clr, set) __dcri_clrset(DCRN_ ## base ## _CONFIG_ADDR, \
139 DCRN_ ## base ## _CONFIG_DATA, \
142 #endif /* __ASSEMBLY__ */
143 #endif /* __KERNEL__ */
144 #endif /* _ASM_POWERPC_DCR_NATIVE_H */