blob: 151dff555f5039e1fed593ae4432e38ffe48205a [file] [log] [blame]
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +11001/*
2 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef _ASM_POWERPC_DCR_NATIVE_H
21#define _ASM_POWERPC_DCR_NATIVE_H
22#ifdef __KERNEL__
Kumar Gala45d8e7a2006-12-10 23:15:47 -060023#ifndef __ASSEMBLY__
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +110024
Benjamin Herrenschmidt0e6140a2007-12-21 15:39:22 +110025#include <linux/spinlock.h>
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +000026#include <asm/cputable.h>
Kevin Haob92a2262016-07-23 14:42:40 +053027#include <asm/cpu_has_feature.h>
Christophe Leroy5c35a022018-07-05 16:24:59 +000028#include <linux/stringify.h>
Benjamin Herrenschmidt0e6140a2007-12-21 15:39:22 +110029
Michael Ellerman0b94a1e2007-09-17 16:05:00 +100030typedef struct {
31 unsigned int base;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100032} dcr_host_native_t;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +110033
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100034static inline bool dcr_map_ok_native(dcr_host_native_t host)
35{
Joe Perchesacdb6682015-03-30 16:46:04 -070036 return true;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100037}
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +110038
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100039#define dcr_map_native(dev, dcr_n, dcr_c) \
40 ((dcr_host_native_t){ .base = (dcr_n) })
41#define dcr_unmap_native(host, dcr_c) do {} while (0)
42#define dcr_read_native(host, dcr_n) mfdcr(dcr_n + host.base)
43#define dcr_write_native(host, dcr_n, value) mtdcr(dcr_n + host.base, value)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +110044
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +000045/* Table based DCR accessors */
46extern void __mtdcr(unsigned int reg, unsigned int val);
47extern unsigned int __mfdcr(unsigned int reg);
48
49/* mfdcrx/mtdcrx instruction based accessors. We hand code
50 * the opcodes in order not to depend on newer binutils
51 */
52static inline unsigned int mfdcrx(unsigned int reg)
53{
54 unsigned int ret;
55 asm volatile(".long 0x7c000206 | (%0 << 21) | (%1 << 16)"
56 : "=r" (ret) : "r" (reg));
57 return ret;
58}
59
60static inline void mtdcrx(unsigned int reg, unsigned int val)
61{
62 asm volatile(".long 0x7c000306 | (%0 << 21) | (%1 << 16)"
63 : : "r" (val), "r" (reg));
64}
65
Kumar Gala45d8e7a2006-12-10 23:15:47 -060066#define mfdcr(rn) \
67 ({unsigned int rval; \
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +000068 if (__builtin_constant_p(rn) && rn < 1024) \
Kumar Gala45d8e7a2006-12-10 23:15:47 -060069 asm volatile("mfdcr %0," __stringify(rn) \
70 : "=r" (rval)); \
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +000071 else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
72 rval = mfdcrx(rn); \
Kumar Gala45d8e7a2006-12-10 23:15:47 -060073 else \
74 rval = __mfdcr(rn); \
75 rval;})
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +110076
Kumar Gala45d8e7a2006-12-10 23:15:47 -060077#define mtdcr(rn, v) \
78do { \
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +000079 if (__builtin_constant_p(rn) && rn < 1024) \
Kumar Gala45d8e7a2006-12-10 23:15:47 -060080 asm volatile("mtdcr " __stringify(rn) ",%0" \
81 : : "r" (v)); \
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +000082 else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
83 mtdcrx(rn, v); \
Kumar Gala45d8e7a2006-12-10 23:15:47 -060084 else \
85 __mtdcr(rn, v); \
86} while (0)
87
88/* R/W of indirect DCRs make use of standard naming conventions for DCRs */
Benjamin Herrenschmidt0e6140a2007-12-21 15:39:22 +110089extern spinlock_t dcr_ind_lock;
90
Valentine Barshake8318d92008-02-06 05:36:49 +110091static inline unsigned __mfdcri(int base_addr, int base_data, int reg)
92{
93 unsigned long flags;
94 unsigned int val;
Kumar Gala45d8e7a2006-12-10 23:15:47 -060095
Valentine Barshake8318d92008-02-06 05:36:49 +110096 spin_lock_irqsave(&dcr_ind_lock, flags);
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +000097 if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
98 mtdcrx(base_addr, reg);
99 val = mfdcrx(base_data);
100 } else {
101 __mtdcr(base_addr, reg);
102 val = __mfdcr(base_data);
103 }
Valentine Barshake8318d92008-02-06 05:36:49 +1100104 spin_unlock_irqrestore(&dcr_ind_lock, flags);
105 return val;
106}
107
108static inline void __mtdcri(int base_addr, int base_data, int reg,
109 unsigned val)
110{
111 unsigned long flags;
112
113 spin_lock_irqsave(&dcr_ind_lock, flags);
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +0000114 if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
115 mtdcrx(base_addr, reg);
116 mtdcrx(base_data, val);
117 } else {
118 __mtdcr(base_addr, reg);
119 __mtdcr(base_data, val);
120 }
Valentine Barshake8318d92008-02-06 05:36:49 +1100121 spin_unlock_irqrestore(&dcr_ind_lock, flags);
122}
123
Valentine Barshak266d0282008-03-06 05:38:04 +1100124static inline void __dcri_clrset(int base_addr, int base_data, int reg,
125 unsigned clr, unsigned set)
126{
127 unsigned long flags;
128 unsigned int val;
129
130 spin_lock_irqsave(&dcr_ind_lock, flags);
Benjamin Herrenschmidt6d2170b2008-12-18 19:13:22 +0000131 if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
132 mtdcrx(base_addr, reg);
133 val = (mfdcrx(base_data) & ~clr) | set;
134 mtdcrx(base_data, val);
135 } else {
136 __mtdcr(base_addr, reg);
137 val = (__mfdcr(base_data) & ~clr) | set;
138 __mtdcr(base_data, val);
139 }
Valentine Barshak266d0282008-03-06 05:38:04 +1100140 spin_unlock_irqrestore(&dcr_ind_lock, flags);
141}
142
Valentine Barshake8318d92008-02-06 05:36:49 +1100143#define mfdcri(base, reg) __mfdcri(DCRN_ ## base ## _CONFIG_ADDR, \
144 DCRN_ ## base ## _CONFIG_DATA, \
145 reg)
146
147#define mtdcri(base, reg, data) __mtdcri(DCRN_ ## base ## _CONFIG_ADDR, \
148 DCRN_ ## base ## _CONFIG_DATA, \
149 reg, data)
Kumar Gala45d8e7a2006-12-10 23:15:47 -0600150
Valentine Barshak266d0282008-03-06 05:38:04 +1100151#define dcri_clrset(base, reg, clr, set) __dcri_clrset(DCRN_ ## base ## _CONFIG_ADDR, \
152 DCRN_ ## base ## _CONFIG_DATA, \
153 reg, clr, set)
154
Kumar Gala45d8e7a2006-12-10 23:15:47 -0600155#endif /* __ASSEMBLY__ */
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100156#endif /* __KERNEL__ */
157#endif /* _ASM_POWERPC_DCR_NATIVE_H */