Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[POWERPC] Refactor DCR code

Previously, DCR support was configured at compile time to either use
MMIO or native dcr instructions. Although this works for most
platforms, it fails on FPGA platforms:

1) Systems may include more than one DCR bus.
2) Systems may be native DCR capable and still use memory mapped DCR interface.

This patch provides runtime support based on the device trees for the
case where CONFIG_PPC_DCR_MMIO and CONFIG_PPC_DCR_NATIVE are both
selected. Previously, this was a poorly defined configuration, which
happened to provide NATIVE support. The runtime selection is made
based on the dcr-controller having a 'dcr-access-method' attribute
in the device tree. If only one of the above options is selected,
then the code uses #defines to select only the used code in order to
avoid introducing overhead in existing usage.

Signed-off-by: Stephen Neuendorffer <stephen.neuendorffer@xilinx.com>
Signed-off-by: Josh Boyer <jwboyer@linux.vnet.ibm.com>

authored by

Stephen Neuendorffer and committed by
Josh Boyer
b786af11 acf46481

+231 -47
+121 -33
arch/powerpc/sysdev/dcr.c
··· 23 23 #include <asm/prom.h> 24 24 #include <asm/dcr.h> 25 25 26 + static struct device_node *find_dcr_parent(struct device_node *node) 27 + { 28 + struct device_node *par, *tmp; 29 + const u32 *p; 30 + 31 + for (par = of_node_get(node); par;) { 32 + if (of_get_property(par, "dcr-controller", NULL)) 33 + break; 34 + p = of_get_property(par, "dcr-parent", NULL); 35 + tmp = par; 36 + if (p == NULL) 37 + par = of_get_parent(par); 38 + else 39 + par = of_find_node_by_phandle(*p); 40 + of_node_put(tmp); 41 + } 42 + return par; 43 + } 44 + 45 + #if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) 46 + 47 + bool dcr_map_ok_generic(dcr_host_t host) 48 + { 49 + if (host.type == DCR_HOST_NATIVE) 50 + return dcr_map_ok_native(host.host.native); 51 + else if (host.type == DCR_HOST_MMIO) 52 + return dcr_map_ok_mmio(host.host.mmio); 53 + else 54 + return 0; 55 + } 56 + EXPORT_SYMBOL_GPL(dcr_map_ok_generic); 57 + 58 + dcr_host_t dcr_map_generic(struct device_node *dev, 59 + unsigned int dcr_n, 60 + unsigned int dcr_c) 61 + { 62 + dcr_host_t host; 63 + struct device_node *dp; 64 + const char *prop; 65 + 66 + host.type = DCR_HOST_INVALID; 67 + 68 + dp = find_dcr_parent(dev); 69 + if (dp == NULL) 70 + return host; 71 + 72 + prop = of_get_property(dp, "dcr-access-method", NULL); 73 + 74 + pr_debug("dcr_map_generic(dcr-access-method = %s)\n", prop); 75 + 76 + if (!strcmp(prop, "native")) { 77 + host.type = DCR_HOST_NATIVE; 78 + host.host.native = dcr_map_native(dev, dcr_n, dcr_c); 79 + } else if (!strcmp(prop, "mmio")) { 80 + host.type = DCR_HOST_MMIO; 81 + host.host.mmio = dcr_map_mmio(dev, dcr_n, dcr_c); 82 + } 83 + 84 + of_node_put(dp); 85 + return host; 86 + } 87 + EXPORT_SYMBOL_GPL(dcr_map_generic); 88 + 89 + void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c) 90 + { 91 + if (host.type == DCR_HOST_NATIVE) 92 + dcr_unmap_native(host.host.native, dcr_c); 93 + else if (host.type == DCR_HOST_MMIO) 94 + dcr_unmap_mmio(host.host.mmio, dcr_c); 95 + else /* host.type == DCR_HOST_INVALID */ 96 + WARN_ON(true); 97 + } 98 + EXPORT_SYMBOL_GPL(dcr_unmap_generic); 99 + 100 + u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n) 101 + { 102 + if (host.type == DCR_HOST_NATIVE) 103 + return dcr_read_native(host.host.native, dcr_n); 104 + else if (host.type == DCR_HOST_MMIO) 105 + return dcr_read_mmio(host.host.mmio, dcr_n); 106 + else /* host.type == DCR_HOST_INVALID */ 107 + WARN_ON(true); 108 + return 0; 109 + } 110 + EXPORT_SYMBOL_GPL(dcr_read_generic); 111 + 112 + void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value) 113 + { 114 + if (host.type == DCR_HOST_NATIVE) 115 + dcr_write_native(host.host.native, dcr_n, value); 116 + else if (host.type == DCR_HOST_MMIO) 117 + dcr_write_mmio(host.host.mmio, dcr_n, value); 118 + else /* host.type == DCR_HOST_INVALID */ 119 + WARN_ON(true); 120 + } 121 + EXPORT_SYMBOL_GPL(dcr_write_generic); 122 + 123 + #endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */ 124 + 26 125 unsigned int dcr_resource_start(struct device_node *np, unsigned int index) 27 126 { 28 127 unsigned int ds; ··· 146 47 } 147 48 EXPORT_SYMBOL_GPL(dcr_resource_len); 148 49 149 - #ifndef CONFIG_PPC_DCR_NATIVE 150 - 151 - static struct device_node * find_dcr_parent(struct device_node * node) 152 - { 153 - struct device_node *par, *tmp; 154 - const u32 *p; 155 - 156 - for (par = of_node_get(node); par;) { 157 - if (of_get_property(par, "dcr-controller", NULL)) 158 - break; 159 - p = of_get_property(par, "dcr-parent", NULL); 160 - tmp = par; 161 - if (p == NULL) 162 - par = of_get_parent(par); 163 - else 164 - par = of_find_node_by_phandle(*p); 165 - of_node_put(tmp); 166 - } 167 - return par; 168 - } 50 + #ifdef CONFIG_PPC_DCR_MMIO 169 51 170 52 u64 of_translate_dcr_address(struct device_node *dev, 171 53 unsigned int dcr_n, ··· 155 75 struct device_node *dp; 156 76 const u32 *p; 157 77 unsigned int stride; 158 - u64 ret; 78 + u64 ret = OF_BAD_ADDR; 159 79 160 80 dp = find_dcr_parent(dev); 161 81 if (dp == NULL) ··· 170 90 if (p == NULL) 171 91 p = of_get_property(dp, "dcr-mmio-space", NULL); 172 92 if (p == NULL) 173 - return OF_BAD_ADDR; 93 + goto done; 174 94 175 95 /* Maybe could do some better range checking here */ 176 96 ret = of_translate_address(dp, p); ··· 178 98 ret += (u64)(stride) * (u64)dcr_n; 179 99 if (out_stride) 180 100 *out_stride = stride; 101 + 102 + done: 103 + of_node_put(dp); 181 104 return ret; 182 105 } 183 106 184 - dcr_host_t dcr_map(struct device_node *dev, unsigned int dcr_n, 185 - unsigned int dcr_c) 107 + dcr_host_mmio_t dcr_map_mmio(struct device_node *dev, 108 + unsigned int dcr_n, 109 + unsigned int dcr_c) 186 110 { 187 - dcr_host_t ret = { .token = NULL, .stride = 0, .base = dcr_n }; 111 + dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n }; 188 112 u64 addr; 189 113 190 114 pr_debug("dcr_map(%s, 0x%x, 0x%x)\n", 191 115 dev->full_name, dcr_n, dcr_c); 192 116 193 117 addr = of_translate_dcr_address(dev, dcr_n, &ret.stride); 194 - pr_debug("translates to addr: 0x%lx, stride: 0x%x\n", 195 - addr, ret.stride); 118 + pr_debug("translates to addr: 0x%llx, stride: 0x%x\n", 119 + (unsigned long long) addr, ret.stride); 196 120 if (addr == OF_BAD_ADDR) 197 121 return ret; 198 122 pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride); ··· 208 124 ret.token -= dcr_n * ret.stride; 209 125 return ret; 210 126 } 211 - EXPORT_SYMBOL_GPL(dcr_map); 127 + EXPORT_SYMBOL_GPL(dcr_map_mmio); 212 128 213 - void dcr_unmap(dcr_host_t host, unsigned int dcr_c) 129 + void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c) 214 130 { 215 - dcr_host_t h = host; 131 + dcr_host_mmio_t h = host; 216 132 217 133 if (h.token == NULL) 218 134 return; ··· 220 136 iounmap(h.token); 221 137 h.token = NULL; 222 138 } 223 - EXPORT_SYMBOL_GPL(dcr_unmap); 224 - #else /* defined(CONFIG_PPC_DCR_NATIVE) */ 139 + EXPORT_SYMBOL_GPL(dcr_unmap_mmio); 140 + 141 + #endif /* defined(CONFIG_PPC_DCR_MMIO) */ 142 + 143 + #ifdef CONFIG_PPC_DCR_NATIVE 225 144 DEFINE_SPINLOCK(dcr_ind_lock); 226 - #endif /* !defined(CONFIG_PPC_DCR_NATIVE) */ 145 + #endif /* defined(CONFIG_PPC_DCR_NATIVE) */ 146 +
+49
include/asm-powerpc/dcr-generic.h
··· 1 + /* 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_GENERIC_H 21 + #define _ASM_POWERPC_DCR_GENERIC_H 22 + #ifdef __KERNEL__ 23 + #ifndef __ASSEMBLY__ 24 + 25 + enum host_type_t {DCR_HOST_MMIO, DCR_HOST_NATIVE, DCR_HOST_INVALID}; 26 + 27 + typedef struct { 28 + enum host_type_t type; 29 + union { 30 + dcr_host_mmio_t mmio; 31 + dcr_host_native_t native; 32 + } host; 33 + } dcr_host_t; 34 + 35 + extern bool dcr_map_ok_generic(dcr_host_t host); 36 + 37 + extern dcr_host_t dcr_map_generic(struct device_node *dev, unsigned int dcr_n, 38 + unsigned int dcr_c); 39 + extern void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c); 40 + 41 + extern u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n); 42 + 43 + extern void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value); 44 + 45 + #endif /* __ASSEMBLY__ */ 46 + #endif /* __KERNEL__ */ 47 + #endif /* _ASM_POWERPC_DCR_GENERIC_H */ 48 + 49 +
+13 -7
include/asm-powerpc/dcr-mmio.h
··· 27 27 void __iomem *token; 28 28 unsigned int stride; 29 29 unsigned int base; 30 - } dcr_host_t; 30 + } dcr_host_mmio_t; 31 31 32 - #define DCR_MAP_OK(host) ((host).token != NULL) 32 + static inline bool dcr_map_ok_mmio(dcr_host_mmio_t host) 33 + { 34 + return host.token != NULL; 35 + } 33 36 34 - extern dcr_host_t dcr_map(struct device_node *dev, unsigned int dcr_n, 35 - unsigned int dcr_c); 36 - extern void dcr_unmap(dcr_host_t host, unsigned int dcr_c); 37 + extern dcr_host_mmio_t dcr_map_mmio(struct device_node *dev, 38 + unsigned int dcr_n, 39 + unsigned int dcr_c); 40 + extern void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c); 37 41 38 - static inline u32 dcr_read(dcr_host_t host, unsigned int dcr_n) 42 + static inline u32 dcr_read_mmio(dcr_host_mmio_t host, unsigned int dcr_n) 39 43 { 40 44 return in_be32(host.token + ((host.base + dcr_n) * host.stride)); 41 45 } 42 46 43 - static inline void dcr_write(dcr_host_t host, unsigned int dcr_n, u32 value) 47 + static inline void dcr_write_mmio(dcr_host_mmio_t host, 48 + unsigned int dcr_n, 49 + u32 value) 44 50 { 45 51 out_be32(host.token + ((host.base + dcr_n) * host.stride), value); 46 52 }
+10 -6
include/asm-powerpc/dcr-native.h
··· 26 26 27 27 typedef struct { 28 28 unsigned int base; 29 - } dcr_host_t; 29 + } dcr_host_native_t; 30 30 31 - #define DCR_MAP_OK(host) (1) 31 + static inline bool dcr_map_ok_native(dcr_host_native_t host) 32 + { 33 + return 1; 34 + } 32 35 33 - #define dcr_map(dev, dcr_n, dcr_c) ((dcr_host_t){ .base = (dcr_n) }) 34 - #define dcr_unmap(host, dcr_c) do {} while (0) 35 - #define dcr_read(host, dcr_n) mfdcr(dcr_n + host.base) 36 - #define dcr_write(host, dcr_n, value) mtdcr(dcr_n + host.base, value) 36 + #define dcr_map_native(dev, dcr_n, dcr_c) \ 37 + ((dcr_host_native_t){ .base = (dcr_n) }) 38 + #define dcr_unmap_native(host, dcr_c) do {} while (0) 39 + #define dcr_read_native(host, dcr_n) mfdcr(dcr_n + host.base) 40 + #define dcr_write_native(host, dcr_n, value) mtdcr(dcr_n + host.base, value) 37 41 38 42 /* Device Control Registers */ 39 43 void __mtdcr(int reg, unsigned int val);
+38 -1
include/asm-powerpc/dcr.h
··· 20 20 #ifndef _ASM_POWERPC_DCR_H 21 21 #define _ASM_POWERPC_DCR_H 22 22 #ifdef __KERNEL__ 23 + #ifndef __ASSEMBLY__ 23 24 #ifdef CONFIG_PPC_DCR 24 25 25 26 #ifdef CONFIG_PPC_DCR_NATIVE 26 27 #include <asm/dcr-native.h> 27 - #else 28 + #endif 29 + 30 + #ifdef CONFIG_PPC_DCR_MMIO 28 31 #include <asm/dcr-mmio.h> 29 32 #endif 33 + 34 + 35 + /* Indirection layer for providing both NATIVE and MMIO support. */ 36 + 37 + #if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) 38 + 39 + #include <asm/dcr-generic.h> 40 + 41 + #define DCR_MAP_OK(host) dcr_map_ok_generic(host) 42 + #define dcr_map(dev, dcr_n, dcr_c) dcr_map_generic(dev, dcr_n, dcr_c) 43 + #define dcr_unmap(host, dcr_c) dcr_unmap_generic(host, dcr_c) 44 + #define dcr_read(host, dcr_n) dcr_read_generic(host, dcr_n) 45 + #define dcr_write(host, dcr_n, value) dcr_write_generic(host, dcr_n, value) 46 + 47 + #else 48 + 49 + #ifdef CONFIG_PPC_DCR_NATIVE 50 + typedef dcr_host_native_t dcr_host_t; 51 + #define DCR_MAP_OK(host) dcr_map_ok_native(host) 52 + #define dcr_map(dev, dcr_n, dcr_c) dcr_map_native(dev, dcr_n, dcr_c) 53 + #define dcr_unmap(host, dcr_c) dcr_unmap_native(host, dcr_c) 54 + #define dcr_read(host, dcr_n) dcr_read_native(host, dcr_n) 55 + #define dcr_write(host, dcr_n, value) dcr_write_native(host, dcr_n, value) 56 + #else 57 + typedef dcr_host_mmio_t dcr_host_t; 58 + #define DCR_MAP_OK(host) dcr_map_ok_mmio(host) 59 + #define dcr_map(dev, dcr_n, dcr_c) dcr_map_mmio(dev, dcr_n, dcr_c) 60 + #define dcr_unmap(host, dcr_c) dcr_unmap_mmio(host, dcr_c) 61 + #define dcr_read(host, dcr_n) dcr_read_mmio(host, dcr_n) 62 + #define dcr_write(host, dcr_n, value) dcr_write_mmio(host, dcr_n, value) 63 + #endif 64 + 65 + #endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */ 30 66 31 67 /* 32 68 * On CONFIG_PPC_MERGE, we have additional helpers to read the DCR ··· 77 41 #endif /* CONFIG_PPC_MERGE */ 78 42 79 43 #endif /* CONFIG_PPC_DCR */ 44 + #endif /* __ASSEMBLY__ */ 80 45 #endif /* __KERNEL__ */ 81 46 #endif /* _ASM_POWERPC_DCR_H */