4 * Accessor routines for the various MMIO register blocks of the CBE
6 * (c) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
9 #include <linux/percpu.h>
10 #include <linux/types.h>
11 #include <linux/module.h>
14 #include <asm/pgtable.h>
16 #include <asm/ptrace.h>
17 #include <asm/of_device.h>
18 #include <asm/of_platform.h>
23 * Current implementation uses "cpu" nodes. We build our own mapping
24 * array of cpu numbers to cpu nodes locally for now to allow interrupt
25 * time code to have a fast path rather than call of_get_cpu_node(). If
26 * we implement cpu hotplug, we'll have to install an appropriate norifier
27 * in order to release references to the cpu going away
29 static struct cbe_regs_map
31 struct device_node *cpu_node;
32 struct device_node *be_node;
33 struct cbe_pmd_regs __iomem *pmd_regs;
34 struct cbe_iic_regs __iomem *iic_regs;
35 struct cbe_mic_tm_regs __iomem *mic_tm_regs;
36 struct cbe_pmd_shadow_regs pmd_shadow_regs;
37 } cbe_regs_maps[MAX_CBE];
38 static int cbe_regs_map_count;
40 static struct cbe_thread_map
42 struct device_node *cpu_node;
43 struct device_node *be_node;
44 struct cbe_regs_map *regs;
45 unsigned int thread_id;
47 } cbe_thread_map[NR_CPUS];
49 static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = CPU_MASK_NONE };
50 static cpumask_t cbe_first_online_cpu = CPU_MASK_NONE;
52 static struct cbe_regs_map *cbe_find_map(struct device_node *np)
55 struct device_node *tmp_np;
57 if (strcasecmp(np->type, "spe")) {
58 for (i = 0; i < cbe_regs_map_count; i++)
59 if (cbe_regs_maps[i].cpu_node == np ||
60 cbe_regs_maps[i].be_node == np)
61 return &cbe_regs_maps[i];
68 /* walk up path until cpu or be node was found */
71 tmp_np = tmp_np->parent;
72 /* on a correct devicetree we wont get up to root */
74 } while (strcasecmp(tmp_np->type, "cpu") &&
75 strcasecmp(tmp_np->type, "be"));
77 np->data = cbe_find_map(tmp_np);
82 struct cbe_pmd_regs __iomem *cbe_get_pmd_regs(struct device_node *np)
84 struct cbe_regs_map *map = cbe_find_map(np);
89 EXPORT_SYMBOL_GPL(cbe_get_pmd_regs);
91 struct cbe_pmd_regs __iomem *cbe_get_cpu_pmd_regs(int cpu)
93 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
98 EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs);
100 struct cbe_pmd_shadow_regs *cbe_get_pmd_shadow_regs(struct device_node *np)
102 struct cbe_regs_map *map = cbe_find_map(np);
105 return &map->pmd_shadow_regs;
108 struct cbe_pmd_shadow_regs *cbe_get_cpu_pmd_shadow_regs(int cpu)
110 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
113 return &map->pmd_shadow_regs;
116 struct cbe_iic_regs __iomem *cbe_get_iic_regs(struct device_node *np)
118 struct cbe_regs_map *map = cbe_find_map(np);
121 return map->iic_regs;
124 struct cbe_iic_regs __iomem *cbe_get_cpu_iic_regs(int cpu)
126 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
129 return map->iic_regs;
132 struct cbe_mic_tm_regs __iomem *cbe_get_mic_tm_regs(struct device_node *np)
134 struct cbe_regs_map *map = cbe_find_map(np);
137 return map->mic_tm_regs;
140 struct cbe_mic_tm_regs __iomem *cbe_get_cpu_mic_tm_regs(int cpu)
142 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
145 return map->mic_tm_regs;
147 EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs);
149 u32 cbe_get_hw_thread_id(int cpu)
151 return cbe_thread_map[cpu].thread_id;
153 EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id);
155 u32 cbe_cpu_to_node(int cpu)
157 return cbe_thread_map[cpu].cbe_id;
159 EXPORT_SYMBOL_GPL(cbe_cpu_to_node);
161 u32 cbe_node_to_cpu(int node)
163 return find_first_bit( (unsigned long *) &cbe_local_mask[node], sizeof(cpumask_t));
165 EXPORT_SYMBOL_GPL(cbe_node_to_cpu);
167 static struct device_node *cbe_get_be_node(int cpu_id)
169 struct device_node *np;
171 for_each_node_by_type (np, "be") {
173 const phandle *cpu_handle;
175 cpu_handle = of_get_property(np, "cpus", &len);
178 * the CAB SLOF tree is non compliant, so we just assume
179 * there is only one node
181 if (WARN_ON_ONCE(!cpu_handle))
184 for (i=0; i<len; i++)
185 if (of_find_node_by_phandle(cpu_handle[i]) == of_get_cpu_node(cpu_id, NULL))
192 void __init cbe_fill_regs_map(struct cbe_regs_map *map)
195 struct device_node *be, *np;
199 for_each_node_by_type(np, "pervasive")
200 if (of_get_parent(np) == be)
201 map->pmd_regs = of_iomap(np, 0);
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);
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);
211 struct device_node *cpu;
212 /* That hack must die die die ! */
213 const struct address_prop {
214 unsigned long address;
216 } __attribute__((packed)) *prop;
220 prop = of_get_property(cpu, "pervasive", NULL);
222 map->pmd_regs = ioremap(prop->address, prop->len);
224 prop = of_get_property(cpu, "iic", NULL);
226 map->iic_regs = ioremap(prop->address, prop->len);
228 prop = of_get_property(cpu, "mic-tm", NULL);
230 map->mic_tm_regs = ioremap(prop->address, prop->len);
235 void __init cbe_regs_init(void)
238 unsigned int thread_id;
239 struct device_node *cpu;
241 /* Build local fast map of CPUs */
242 for_each_possible_cpu(i) {
243 cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id);
244 cbe_thread_map[i].be_node = cbe_get_be_node(i);
245 cbe_thread_map[i].thread_id = thread_id;
248 /* Find maps for each device tree CPU */
249 for_each_node_by_type(cpu, "cpu") {
250 struct cbe_regs_map *map;
253 cbe_id = cbe_regs_map_count++;
254 map = &cbe_regs_maps[cbe_id];
256 if (cbe_regs_map_count > MAX_CBE) {
257 printk(KERN_ERR "cbe_regs: More BE chips than supported"
259 cbe_regs_map_count--;
264 for_each_possible_cpu(i) {
265 struct cbe_thread_map *thread = &cbe_thread_map[i];
267 if (thread->cpu_node == cpu) {
269 thread->cbe_id = cbe_id;
270 map->be_node = thread->be_node;
271 cpu_set(i, cbe_local_mask[cbe_id]);
272 if(thread->thread_id == 0)
273 cpu_set(i, cbe_first_online_cpu);
277 cbe_fill_regs_map(map);