Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 2 | /* |
| 3 | * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp. |
| 4 | * <benh@kernel.crashing.org> |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef _ASM_POWERPC_DCR_NATIVE_H |
| 8 | #define _ASM_POWERPC_DCR_NATIVE_H |
| 9 | #ifdef __KERNEL__ |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 10 | #ifndef __ASSEMBLY__ |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 11 | |
Benjamin Herrenschmidt | 0e6140a | 2007-12-21 15:39:22 +1100 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 13 | #include <asm/cputable.h> |
Kevin Hao | b92a226 | 2016-07-23 14:42:40 +0530 | [diff] [blame] | 14 | #include <asm/cpu_has_feature.h> |
Christophe Leroy | 5c35a02 | 2018-07-05 16:24:59 +0000 | [diff] [blame] | 15 | #include <linux/stringify.h> |
Benjamin Herrenschmidt | 0e6140a | 2007-12-21 15:39:22 +1100 | [diff] [blame] | 16 | |
Michael Ellerman | 0b94a1e | 2007-09-17 16:05:00 +1000 | [diff] [blame] | 17 | typedef struct { |
| 18 | unsigned int base; |
Stephen Neuendorffer | b786af11 | 2008-05-07 04:29:17 +1000 | [diff] [blame] | 19 | } dcr_host_native_t; |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 20 | |
Stephen Neuendorffer | b786af11 | 2008-05-07 04:29:17 +1000 | [diff] [blame] | 21 | static inline bool dcr_map_ok_native(dcr_host_native_t host) |
| 22 | { |
Joe Perches | acdb668 | 2015-03-30 16:46:04 -0700 | [diff] [blame] | 23 | return true; |
Stephen Neuendorffer | b786af11 | 2008-05-07 04:29:17 +1000 | [diff] [blame] | 24 | } |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 25 | |
Stephen Neuendorffer | b786af11 | 2008-05-07 04:29:17 +1000 | [diff] [blame] | 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) |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 31 | |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 32 | /* Table based DCR accessors */ |
| 33 | extern void __mtdcr(unsigned int reg, unsigned int val); |
| 34 | extern unsigned int __mfdcr(unsigned int reg); |
| 35 | |
| 36 | /* mfdcrx/mtdcrx instruction based accessors. We hand code |
| 37 | * the opcodes in order not to depend on newer binutils |
| 38 | */ |
| 39 | static inline unsigned int mfdcrx(unsigned int reg) |
| 40 | { |
| 41 | unsigned int ret; |
| 42 | asm volatile(".long 0x7c000206 | (%0 << 21) | (%1 << 16)" |
| 43 | : "=r" (ret) : "r" (reg)); |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | static inline void mtdcrx(unsigned int reg, unsigned int val) |
| 48 | { |
| 49 | asm volatile(".long 0x7c000306 | (%0 << 21) | (%1 << 16)" |
| 50 | : : "r" (val), "r" (reg)); |
| 51 | } |
| 52 | |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 53 | #define mfdcr(rn) \ |
| 54 | ({unsigned int rval; \ |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 55 | if (__builtin_constant_p(rn) && rn < 1024) \ |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 56 | asm volatile("mfdcr %0," __stringify(rn) \ |
| 57 | : "=r" (rval)); \ |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 58 | else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \ |
| 59 | rval = mfdcrx(rn); \ |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 60 | else \ |
| 61 | rval = __mfdcr(rn); \ |
| 62 | rval;}) |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 63 | |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 64 | #define mtdcr(rn, v) \ |
| 65 | do { \ |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 66 | if (__builtin_constant_p(rn) && rn < 1024) \ |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 67 | asm volatile("mtdcr " __stringify(rn) ",%0" \ |
| 68 | : : "r" (v)); \ |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 69 | else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \ |
| 70 | mtdcrx(rn, v); \ |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 71 | else \ |
| 72 | __mtdcr(rn, v); \ |
| 73 | } while (0) |
| 74 | |
| 75 | /* R/W of indirect DCRs make use of standard naming conventions for DCRs */ |
Benjamin Herrenschmidt | 0e6140a | 2007-12-21 15:39:22 +1100 | [diff] [blame] | 76 | extern spinlock_t dcr_ind_lock; |
| 77 | |
Valentine Barshak | e8318d9 | 2008-02-06 05:36:49 +1100 | [diff] [blame] | 78 | static inline unsigned __mfdcri(int base_addr, int base_data, int reg) |
| 79 | { |
| 80 | unsigned long flags; |
| 81 | unsigned int val; |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 82 | |
Valentine Barshak | e8318d9 | 2008-02-06 05:36:49 +1100 | [diff] [blame] | 83 | spin_lock_irqsave(&dcr_ind_lock, flags); |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 84 | if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) { |
| 85 | mtdcrx(base_addr, reg); |
| 86 | val = mfdcrx(base_data); |
| 87 | } else { |
| 88 | __mtdcr(base_addr, reg); |
| 89 | val = __mfdcr(base_data); |
| 90 | } |
Valentine Barshak | e8318d9 | 2008-02-06 05:36:49 +1100 | [diff] [blame] | 91 | spin_unlock_irqrestore(&dcr_ind_lock, flags); |
| 92 | return val; |
| 93 | } |
| 94 | |
| 95 | static inline void __mtdcri(int base_addr, int base_data, int reg, |
| 96 | unsigned val) |
| 97 | { |
| 98 | unsigned long flags; |
| 99 | |
| 100 | spin_lock_irqsave(&dcr_ind_lock, flags); |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 101 | if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) { |
| 102 | mtdcrx(base_addr, reg); |
| 103 | mtdcrx(base_data, val); |
| 104 | } else { |
| 105 | __mtdcr(base_addr, reg); |
| 106 | __mtdcr(base_data, val); |
| 107 | } |
Valentine Barshak | e8318d9 | 2008-02-06 05:36:49 +1100 | [diff] [blame] | 108 | spin_unlock_irqrestore(&dcr_ind_lock, flags); |
| 109 | } |
| 110 | |
Valentine Barshak | 266d028 | 2008-03-06 05:38:04 +1100 | [diff] [blame] | 111 | static inline void __dcri_clrset(int base_addr, int base_data, int reg, |
| 112 | unsigned clr, unsigned set) |
| 113 | { |
| 114 | unsigned long flags; |
| 115 | unsigned int val; |
| 116 | |
| 117 | spin_lock_irqsave(&dcr_ind_lock, flags); |
Benjamin Herrenschmidt | 6d2170b | 2008-12-18 19:13:22 +0000 | [diff] [blame] | 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); |
| 122 | } else { |
| 123 | __mtdcr(base_addr, reg); |
| 124 | val = (__mfdcr(base_data) & ~clr) | set; |
| 125 | __mtdcr(base_data, val); |
| 126 | } |
Valentine Barshak | 266d028 | 2008-03-06 05:38:04 +1100 | [diff] [blame] | 127 | spin_unlock_irqrestore(&dcr_ind_lock, flags); |
| 128 | } |
| 129 | |
Valentine Barshak | e8318d9 | 2008-02-06 05:36:49 +1100 | [diff] [blame] | 130 | #define mfdcri(base, reg) __mfdcri(DCRN_ ## base ## _CONFIG_ADDR, \ |
| 131 | DCRN_ ## base ## _CONFIG_DATA, \ |
| 132 | reg) |
| 133 | |
| 134 | #define mtdcri(base, reg, data) __mtdcri(DCRN_ ## base ## _CONFIG_ADDR, \ |
| 135 | DCRN_ ## base ## _CONFIG_DATA, \ |
| 136 | reg, data) |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 137 | |
Valentine Barshak | 266d028 | 2008-03-06 05:38:04 +1100 | [diff] [blame] | 138 | #define dcri_clrset(base, reg, clr, set) __dcri_clrset(DCRN_ ## base ## _CONFIG_ADDR, \ |
| 139 | DCRN_ ## base ## _CONFIG_DATA, \ |
| 140 | reg, clr, set) |
| 141 | |
Kumar Gala | 45d8e7a | 2006-12-10 23:15:47 -0600 | [diff] [blame] | 142 | #endif /* __ASSEMBLY__ */ |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 143 | #endif /* __KERNEL__ */ |
| 144 | #endif /* _ASM_POWERPC_DCR_NATIVE_H */ |