Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cbe_regs.c |
| 3 | * |
| 4 | * Accessor routines for the various MMIO register blocks of the CBE |
| 5 | * |
| 6 | * (c) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp. |
| 7 | */ |
| 8 | |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 9 | #include <linux/percpu.h> |
| 10 | #include <linux/types.h> |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Jon Loeliger | d8caf74 | 2007-11-13 11:10:58 -0600 | [diff] [blame] | 12 | #include <linux/of_device.h> |
| 13 | #include <linux/of_platform.h> |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 14 | |
| 15 | #include <asm/io.h> |
| 16 | #include <asm/pgtable.h> |
| 17 | #include <asm/prom.h> |
| 18 | #include <asm/ptrace.h> |
Benjamin Herrenschmidt | eef686a0 | 2007-10-04 15:40:42 +1000 | [diff] [blame] | 19 | #include <asm/cell-regs.h> |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 20 | |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 21 | /* |
| 22 | * Current implementation uses "cpu" nodes. We build our own mapping |
| 23 | * array of cpu numbers to cpu nodes locally for now to allow interrupt |
| 24 | * time code to have a fast path rather than call of_get_cpu_node(). If |
| 25 | * we implement cpu hotplug, we'll have to install an appropriate norifier |
| 26 | * in order to release references to the cpu going away |
| 27 | */ |
| 28 | static struct cbe_regs_map |
| 29 | { |
| 30 | struct device_node *cpu_node; |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 31 | struct device_node *be_node; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 32 | struct cbe_pmd_regs __iomem *pmd_regs; |
| 33 | struct cbe_iic_regs __iomem *iic_regs; |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 34 | struct cbe_mic_tm_regs __iomem *mic_tm_regs; |
Kevin Corry | bffd492 | 2006-10-24 18:31:21 +0200 | [diff] [blame] | 35 | struct cbe_pmd_shadow_regs pmd_shadow_regs; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 36 | } cbe_regs_maps[MAX_CBE]; |
| 37 | static int cbe_regs_map_count; |
| 38 | |
| 39 | static struct cbe_thread_map |
| 40 | { |
| 41 | struct device_node *cpu_node; |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 42 | struct device_node *be_node; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 43 | struct cbe_regs_map *regs; |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 44 | unsigned int thread_id; |
| 45 | unsigned int cbe_id; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 46 | } cbe_thread_map[NR_CPUS]; |
| 47 | |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 48 | static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = {CPU_BITS_NONE} }; |
| 49 | static cpumask_t cbe_first_online_cpu = { CPU_BITS_NONE }; |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 50 | |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 51 | static struct cbe_regs_map *cbe_find_map(struct device_node *np) |
| 52 | { |
| 53 | int i; |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 54 | struct device_node *tmp_np; |
| 55 | |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 56 | if (strcasecmp(np->type, "spe")) { |
| 57 | for (i = 0; i < cbe_regs_map_count; i++) |
| 58 | if (cbe_regs_maps[i].cpu_node == np || |
| 59 | cbe_regs_maps[i].be_node == np) |
| 60 | return &cbe_regs_maps[i]; |
| 61 | return NULL; |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 62 | } |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 63 | |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 64 | if (np->data) |
| 65 | return np->data; |
| 66 | |
| 67 | /* walk up path until cpu or be node was found */ |
| 68 | tmp_np = np; |
| 69 | do { |
| 70 | tmp_np = tmp_np->parent; |
| 71 | /* on a correct devicetree we wont get up to root */ |
| 72 | BUG_ON(!tmp_np); |
| 73 | } while (strcasecmp(tmp_np->type, "cpu") && |
| 74 | strcasecmp(tmp_np->type, "be")); |
| 75 | |
| 76 | np->data = cbe_find_map(tmp_np); |
| 77 | |
| 78 | return np->data; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | struct cbe_pmd_regs __iomem *cbe_get_pmd_regs(struct device_node *np) |
| 82 | { |
| 83 | struct cbe_regs_map *map = cbe_find_map(np); |
| 84 | if (map == NULL) |
| 85 | return NULL; |
| 86 | return map->pmd_regs; |
| 87 | } |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(cbe_get_pmd_regs); |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 89 | |
| 90 | struct cbe_pmd_regs __iomem *cbe_get_cpu_pmd_regs(int cpu) |
| 91 | { |
| 92 | struct cbe_regs_map *map = cbe_thread_map[cpu].regs; |
| 93 | if (map == NULL) |
| 94 | return NULL; |
| 95 | return map->pmd_regs; |
| 96 | } |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 97 | EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs); |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 98 | |
Kevin Corry | bffd492 | 2006-10-24 18:31:21 +0200 | [diff] [blame] | 99 | struct cbe_pmd_shadow_regs *cbe_get_pmd_shadow_regs(struct device_node *np) |
| 100 | { |
| 101 | struct cbe_regs_map *map = cbe_find_map(np); |
| 102 | if (map == NULL) |
| 103 | return NULL; |
| 104 | return &map->pmd_shadow_regs; |
| 105 | } |
| 106 | |
| 107 | struct cbe_pmd_shadow_regs *cbe_get_cpu_pmd_shadow_regs(int cpu) |
| 108 | { |
| 109 | struct cbe_regs_map *map = cbe_thread_map[cpu].regs; |
| 110 | if (map == NULL) |
| 111 | return NULL; |
| 112 | return &map->pmd_shadow_regs; |
| 113 | } |
| 114 | |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 115 | struct cbe_iic_regs __iomem *cbe_get_iic_regs(struct device_node *np) |
| 116 | { |
| 117 | struct cbe_regs_map *map = cbe_find_map(np); |
| 118 | if (map == NULL) |
| 119 | return NULL; |
| 120 | return map->iic_regs; |
| 121 | } |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 122 | |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 123 | struct cbe_iic_regs __iomem *cbe_get_cpu_iic_regs(int cpu) |
| 124 | { |
| 125 | struct cbe_regs_map *map = cbe_thread_map[cpu].regs; |
| 126 | if (map == NULL) |
| 127 | return NULL; |
| 128 | return map->iic_regs; |
| 129 | } |
| 130 | |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 131 | struct cbe_mic_tm_regs __iomem *cbe_get_mic_tm_regs(struct device_node *np) |
| 132 | { |
| 133 | struct cbe_regs_map *map = cbe_find_map(np); |
| 134 | if (map == NULL) |
| 135 | return NULL; |
| 136 | return map->mic_tm_regs; |
| 137 | } |
| 138 | |
| 139 | struct cbe_mic_tm_regs __iomem *cbe_get_cpu_mic_tm_regs(int cpu) |
| 140 | { |
| 141 | struct cbe_regs_map *map = cbe_thread_map[cpu].regs; |
| 142 | if (map == NULL) |
| 143 | return NULL; |
| 144 | return map->mic_tm_regs; |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs); |
| 147 | |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 148 | u32 cbe_get_hw_thread_id(int cpu) |
| 149 | { |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 150 | return cbe_thread_map[cpu].thread_id; |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 151 | } |
| 152 | EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id); |
David Erb | 22b8c9f | 2006-10-24 18:31:20 +0200 | [diff] [blame] | 153 | |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 154 | u32 cbe_cpu_to_node(int cpu) |
| 155 | { |
| 156 | return cbe_thread_map[cpu].cbe_id; |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(cbe_cpu_to_node); |
| 159 | |
| 160 | u32 cbe_node_to_cpu(int node) |
| 161 | { |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 162 | return cpumask_first(&cbe_local_mask[node]); |
| 163 | |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 164 | } |
| 165 | EXPORT_SYMBOL_GPL(cbe_node_to_cpu); |
| 166 | |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 167 | static struct device_node *cbe_get_be_node(int cpu_id) |
| 168 | { |
| 169 | struct device_node *np; |
| 170 | |
| 171 | for_each_node_by_type (np, "be") { |
| 172 | int len,i; |
| 173 | const phandle *cpu_handle; |
| 174 | |
| 175 | cpu_handle = of_get_property(np, "cpus", &len); |
| 176 | |
Jean-Christophe DUBOIS | b86ce01 | 2007-07-20 21:39:25 +0200 | [diff] [blame] | 177 | /* |
| 178 | * the CAB SLOF tree is non compliant, so we just assume |
| 179 | * there is only one node |
| 180 | */ |
| 181 | if (WARN_ON_ONCE(!cpu_handle)) |
| 182 | return np; |
| 183 | |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 184 | for (i=0; i<len; i++) |
| 185 | if (of_find_node_by_phandle(cpu_handle[i]) == of_get_cpu_node(cpu_id, NULL)) |
| 186 | return np; |
| 187 | } |
| 188 | |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | void __init cbe_fill_regs_map(struct cbe_regs_map *map) |
| 193 | { |
| 194 | if(map->be_node) { |
| 195 | struct device_node *be, *np; |
| 196 | |
| 197 | be = map->be_node; |
| 198 | |
| 199 | for_each_node_by_type(np, "pervasive") |
| 200 | if (of_get_parent(np) == be) |
| 201 | map->pmd_regs = of_iomap(np, 0); |
| 202 | |
| 203 | for_each_node_by_type(np, "CBEA-Internal-Interrupt-Controller") |
| 204 | if (of_get_parent(np) == be) |
| 205 | map->iic_regs = of_iomap(np, 2); |
| 206 | |
| 207 | for_each_node_by_type(np, "mic-tm") |
| 208 | if (of_get_parent(np) == be) |
| 209 | map->mic_tm_regs = of_iomap(np, 0); |
| 210 | } else { |
| 211 | struct device_node *cpu; |
| 212 | /* That hack must die die die ! */ |
| 213 | const struct address_prop { |
| 214 | unsigned long address; |
| 215 | unsigned int len; |
| 216 | } __attribute__((packed)) *prop; |
| 217 | |
| 218 | cpu = map->cpu_node; |
| 219 | |
| 220 | prop = of_get_property(cpu, "pervasive", NULL); |
| 221 | if (prop != NULL) |
| 222 | map->pmd_regs = ioremap(prop->address, prop->len); |
| 223 | |
| 224 | prop = of_get_property(cpu, "iic", NULL); |
| 225 | if (prop != NULL) |
| 226 | map->iic_regs = ioremap(prop->address, prop->len); |
| 227 | |
| 228 | prop = of_get_property(cpu, "mic-tm", NULL); |
| 229 | if (prop != NULL) |
| 230 | map->mic_tm_regs = ioremap(prop->address, prop->len); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 235 | void __init cbe_regs_init(void) |
| 236 | { |
| 237 | int i; |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 238 | unsigned int thread_id; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 239 | struct device_node *cpu; |
| 240 | |
| 241 | /* Build local fast map of CPUs */ |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 242 | for_each_possible_cpu(i) { |
| 243 | cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id); |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 244 | cbe_thread_map[i].be_node = cbe_get_be_node(i); |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 245 | cbe_thread_map[i].thread_id = thread_id; |
| 246 | } |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 247 | |
| 248 | /* Find maps for each device tree CPU */ |
| 249 | for_each_node_by_type(cpu, "cpu") { |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 250 | struct cbe_regs_map *map; |
| 251 | unsigned int cbe_id; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 252 | |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 253 | cbe_id = cbe_regs_map_count++; |
| 254 | map = &cbe_regs_maps[cbe_id]; |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 255 | |
| 256 | if (cbe_regs_map_count > MAX_CBE) { |
| 257 | printk(KERN_ERR "cbe_regs: More BE chips than supported" |
| 258 | "!\n"); |
| 259 | cbe_regs_map_count--; |
Julia Lawall | 1fe58a8 | 2007-12-13 15:56:16 -0800 | [diff] [blame] | 260 | of_node_put(cpu); |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | map->cpu_node = cpu; |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 264 | |
| 265 | for_each_possible_cpu(i) { |
| 266 | struct cbe_thread_map *thread = &cbe_thread_map[i]; |
| 267 | |
| 268 | if (thread->cpu_node == cpu) { |
| 269 | thread->regs = map; |
| 270 | thread->cbe_id = cbe_id; |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 271 | map->be_node = thread->be_node; |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 272 | cpumask_set_cpu(i, &cbe_local_mask[cbe_id]); |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 273 | if(thread->thread_id == 0) |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 274 | cpumask_set_cpu(i, &cbe_first_online_cpu); |
Christian Krafft | 91a69c9 | 2007-04-23 21:35:39 +0200 | [diff] [blame] | 275 | } |
| 276 | } |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 277 | |
Christian Krafft | 9dd855a | 2007-04-23 21:35:46 +0200 | [diff] [blame] | 278 | cbe_fill_regs_map(map); |
Benjamin Herrenschmidt | acf7d76 | 2006-06-19 20:33:16 +0200 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |