blob: dbc338f187a25a0288d6fda276ded42bc3f0705e [file] [log] [blame]
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001/*
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 Herrenschmidtacf7d762006-06-19 20:33:16 +02009#include <linux/percpu.h>
10#include <linux/types.h>
David Erb22b8c9f2006-10-24 18:31:20 +020011#include <linux/module.h>
Jon Loeligerd8caf742007-11-13 11:10:58 -060012#include <linux/of_device.h>
13#include <linux/of_platform.h>
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020014
15#include <asm/io.h>
16#include <asm/pgtable.h>
17#include <asm/prom.h>
18#include <asm/ptrace.h>
Benjamin Herrenschmidteef686a02007-10-04 15:40:42 +100019#include <asm/cell-regs.h>
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020020
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020021/*
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 */
28static struct cbe_regs_map
29{
30 struct device_node *cpu_node;
Christian Krafft9dd855a2007-04-23 21:35:46 +020031 struct device_node *be_node;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020032 struct cbe_pmd_regs __iomem *pmd_regs;
33 struct cbe_iic_regs __iomem *iic_regs;
David Erb22b8c9f2006-10-24 18:31:20 +020034 struct cbe_mic_tm_regs __iomem *mic_tm_regs;
Kevin Corrybffd4922006-10-24 18:31:21 +020035 struct cbe_pmd_shadow_regs pmd_shadow_regs;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020036} cbe_regs_maps[MAX_CBE];
37static int cbe_regs_map_count;
38
39static struct cbe_thread_map
40{
41 struct device_node *cpu_node;
Christian Krafft9dd855a2007-04-23 21:35:46 +020042 struct device_node *be_node;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020043 struct cbe_regs_map *regs;
Christian Krafft91a69c92007-04-23 21:35:39 +020044 unsigned int thread_id;
45 unsigned int cbe_id;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020046} cbe_thread_map[NR_CPUS];
47
Christian Krafft91a69c92007-04-23 21:35:39 +020048static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = CPU_MASK_NONE };
49static cpumask_t cbe_first_online_cpu = CPU_MASK_NONE;
50
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020051static struct cbe_regs_map *cbe_find_map(struct device_node *np)
52{
53 int i;
David Erb22b8c9f2006-10-24 18:31:20 +020054 struct device_node *tmp_np;
55
Christian Krafft9dd855a2007-04-23 21:35:46 +020056 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 Erb22b8c9f2006-10-24 18:31:20 +020062 }
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020063
Christian Krafft9dd855a2007-04-23 21:35:46 +020064 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 Herrenschmidtacf7d762006-06-19 20:33:16 +020079}
80
81struct 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 Erb22b8c9f2006-10-24 18:31:20 +020088EXPORT_SYMBOL_GPL(cbe_get_pmd_regs);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020089
90struct 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 Erb22b8c9f2006-10-24 18:31:20 +020097EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020098
Kevin Corrybffd4922006-10-24 18:31:21 +020099struct 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
107struct 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 Herrenschmidtacf7d762006-06-19 20:33:16 +0200115struct 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 Erb22b8c9f2006-10-24 18:31:20 +0200122
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200123struct 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 Erb22b8c9f2006-10-24 18:31:20 +0200131struct 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
139struct 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}
146EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs);
147
Maynard Johnson18f21902006-11-20 18:45:16 +0100148u32 cbe_get_hw_thread_id(int cpu)
149{
Christian Krafft91a69c92007-04-23 21:35:39 +0200150 return cbe_thread_map[cpu].thread_id;
Maynard Johnson18f21902006-11-20 18:45:16 +0100151}
152EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id);
David Erb22b8c9f2006-10-24 18:31:20 +0200153
Christian Krafft91a69c92007-04-23 21:35:39 +0200154u32 cbe_cpu_to_node(int cpu)
155{
156 return cbe_thread_map[cpu].cbe_id;
157}
158EXPORT_SYMBOL_GPL(cbe_cpu_to_node);
159
160u32 cbe_node_to_cpu(int node)
161{
162 return find_first_bit( (unsigned long *) &cbe_local_mask[node], sizeof(cpumask_t));
163}
164EXPORT_SYMBOL_GPL(cbe_node_to_cpu);
165
Christian Krafft9dd855a2007-04-23 21:35:46 +0200166static struct device_node *cbe_get_be_node(int cpu_id)
167{
168 struct device_node *np;
169
170 for_each_node_by_type (np, "be") {
171 int len,i;
172 const phandle *cpu_handle;
173
174 cpu_handle = of_get_property(np, "cpus", &len);
175
Jean-Christophe DUBOISb86ce012007-07-20 21:39:25 +0200176 /*
177 * the CAB SLOF tree is non compliant, so we just assume
178 * there is only one node
179 */
180 if (WARN_ON_ONCE(!cpu_handle))
181 return np;
182
Christian Krafft9dd855a2007-04-23 21:35:46 +0200183 for (i=0; i<len; i++)
184 if (of_find_node_by_phandle(cpu_handle[i]) == of_get_cpu_node(cpu_id, NULL))
185 return np;
186 }
187
188 return NULL;
189}
190
191void __init cbe_fill_regs_map(struct cbe_regs_map *map)
192{
193 if(map->be_node) {
194 struct device_node *be, *np;
195
196 be = map->be_node;
197
198 for_each_node_by_type(np, "pervasive")
199 if (of_get_parent(np) == be)
200 map->pmd_regs = of_iomap(np, 0);
201
202 for_each_node_by_type(np, "CBEA-Internal-Interrupt-Controller")
203 if (of_get_parent(np) == be)
204 map->iic_regs = of_iomap(np, 2);
205
206 for_each_node_by_type(np, "mic-tm")
207 if (of_get_parent(np) == be)
208 map->mic_tm_regs = of_iomap(np, 0);
209 } else {
210 struct device_node *cpu;
211 /* That hack must die die die ! */
212 const struct address_prop {
213 unsigned long address;
214 unsigned int len;
215 } __attribute__((packed)) *prop;
216
217 cpu = map->cpu_node;
218
219 prop = of_get_property(cpu, "pervasive", NULL);
220 if (prop != NULL)
221 map->pmd_regs = ioremap(prop->address, prop->len);
222
223 prop = of_get_property(cpu, "iic", NULL);
224 if (prop != NULL)
225 map->iic_regs = ioremap(prop->address, prop->len);
226
227 prop = of_get_property(cpu, "mic-tm", NULL);
228 if (prop != NULL)
229 map->mic_tm_regs = ioremap(prop->address, prop->len);
230 }
231}
232
233
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200234void __init cbe_regs_init(void)
235{
236 int i;
Christian Krafft91a69c92007-04-23 21:35:39 +0200237 unsigned int thread_id;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200238 struct device_node *cpu;
239
240 /* Build local fast map of CPUs */
Christian Krafft91a69c92007-04-23 21:35:39 +0200241 for_each_possible_cpu(i) {
242 cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id);
Christian Krafft9dd855a2007-04-23 21:35:46 +0200243 cbe_thread_map[i].be_node = cbe_get_be_node(i);
Christian Krafft91a69c92007-04-23 21:35:39 +0200244 cbe_thread_map[i].thread_id = thread_id;
245 }
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200246
247 /* Find maps for each device tree CPU */
248 for_each_node_by_type(cpu, "cpu") {
Christian Krafft91a69c92007-04-23 21:35:39 +0200249 struct cbe_regs_map *map;
250 unsigned int cbe_id;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200251
Christian Krafft91a69c92007-04-23 21:35:39 +0200252 cbe_id = cbe_regs_map_count++;
253 map = &cbe_regs_maps[cbe_id];
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200254
255 if (cbe_regs_map_count > MAX_CBE) {
256 printk(KERN_ERR "cbe_regs: More BE chips than supported"
257 "!\n");
258 cbe_regs_map_count--;
Julia Lawall1fe58a872007-12-13 15:56:16 -0800259 of_node_put(cpu);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200260 return;
261 }
262 map->cpu_node = cpu;
Christian Krafft91a69c92007-04-23 21:35:39 +0200263
264 for_each_possible_cpu(i) {
265 struct cbe_thread_map *thread = &cbe_thread_map[i];
266
267 if (thread->cpu_node == cpu) {
268 thread->regs = map;
269 thread->cbe_id = cbe_id;
Christian Krafft9dd855a2007-04-23 21:35:46 +0200270 map->be_node = thread->be_node;
Christian Krafft91a69c92007-04-23 21:35:39 +0200271 cpu_set(i, cbe_local_mask[cbe_id]);
272 if(thread->thread_id == 0)
273 cpu_set(i, cbe_first_online_cpu);
274 }
275 }
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200276
Christian Krafft9dd855a2007-04-23 21:35:46 +0200277 cbe_fill_regs_map(map);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200278 }
279}
280