blob: 12c9674b4b1f27d51f20f590613a7f1afb1273be [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>
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020012
13#include <asm/io.h>
14#include <asm/pgtable.h>
15#include <asm/prom.h>
16#include <asm/ptrace.h>
Christian Krafft9dd855a2007-04-23 21:35:46 +020017#include <asm/of_device.h>
18#include <asm/of_platform.h>
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020019
20#include "cbe_regs.h"
21
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020022/*
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
28 */
29static struct cbe_regs_map
30{
31 struct device_node *cpu_node;
Christian Krafft9dd855a2007-04-23 21:35:46 +020032 struct device_node *be_node;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020033 struct cbe_pmd_regs __iomem *pmd_regs;
34 struct cbe_iic_regs __iomem *iic_regs;
David Erb22b8c9f2006-10-24 18:31:20 +020035 struct cbe_mic_tm_regs __iomem *mic_tm_regs;
Kevin Corrybffd4922006-10-24 18:31:21 +020036 struct cbe_pmd_shadow_regs pmd_shadow_regs;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020037} cbe_regs_maps[MAX_CBE];
38static int cbe_regs_map_count;
39
40static struct cbe_thread_map
41{
42 struct device_node *cpu_node;
Christian Krafft9dd855a2007-04-23 21:35:46 +020043 struct device_node *be_node;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020044 struct cbe_regs_map *regs;
Christian Krafft91a69c92007-04-23 21:35:39 +020045 unsigned int thread_id;
46 unsigned int cbe_id;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020047} cbe_thread_map[NR_CPUS];
48
Christian Krafft91a69c92007-04-23 21:35:39 +020049static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = CPU_MASK_NONE };
50static cpumask_t cbe_first_online_cpu = CPU_MASK_NONE;
51
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020052static struct cbe_regs_map *cbe_find_map(struct device_node *np)
53{
54 int i;
David Erb22b8c9f2006-10-24 18:31:20 +020055 struct device_node *tmp_np;
56
Christian Krafft9dd855a2007-04-23 21:35:46 +020057 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];
62 return NULL;
David Erb22b8c9f2006-10-24 18:31:20 +020063 }
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020064
Christian Krafft9dd855a2007-04-23 21:35:46 +020065 if (np->data)
66 return np->data;
67
68 /* walk up path until cpu or be node was found */
69 tmp_np = np;
70 do {
71 tmp_np = tmp_np->parent;
72 /* on a correct devicetree we wont get up to root */
73 BUG_ON(!tmp_np);
74 } while (strcasecmp(tmp_np->type, "cpu") &&
75 strcasecmp(tmp_np->type, "be"));
76
77 np->data = cbe_find_map(tmp_np);
78
79 return np->data;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020080}
81
82struct cbe_pmd_regs __iomem *cbe_get_pmd_regs(struct device_node *np)
83{
84 struct cbe_regs_map *map = cbe_find_map(np);
85 if (map == NULL)
86 return NULL;
87 return map->pmd_regs;
88}
David Erb22b8c9f2006-10-24 18:31:20 +020089EXPORT_SYMBOL_GPL(cbe_get_pmd_regs);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020090
91struct cbe_pmd_regs __iomem *cbe_get_cpu_pmd_regs(int cpu)
92{
93 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
94 if (map == NULL)
95 return NULL;
96 return map->pmd_regs;
97}
David Erb22b8c9f2006-10-24 18:31:20 +020098EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020099
Kevin Corrybffd4922006-10-24 18:31:21 +0200100struct cbe_pmd_shadow_regs *cbe_get_pmd_shadow_regs(struct device_node *np)
101{
102 struct cbe_regs_map *map = cbe_find_map(np);
103 if (map == NULL)
104 return NULL;
105 return &map->pmd_shadow_regs;
106}
107
108struct cbe_pmd_shadow_regs *cbe_get_cpu_pmd_shadow_regs(int cpu)
109{
110 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
111 if (map == NULL)
112 return NULL;
113 return &map->pmd_shadow_regs;
114}
115
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200116struct cbe_iic_regs __iomem *cbe_get_iic_regs(struct device_node *np)
117{
118 struct cbe_regs_map *map = cbe_find_map(np);
119 if (map == NULL)
120 return NULL;
121 return map->iic_regs;
122}
David Erb22b8c9f2006-10-24 18:31:20 +0200123
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200124struct cbe_iic_regs __iomem *cbe_get_cpu_iic_regs(int cpu)
125{
126 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
127 if (map == NULL)
128 return NULL;
129 return map->iic_regs;
130}
131
David Erb22b8c9f2006-10-24 18:31:20 +0200132struct cbe_mic_tm_regs __iomem *cbe_get_mic_tm_regs(struct device_node *np)
133{
134 struct cbe_regs_map *map = cbe_find_map(np);
135 if (map == NULL)
136 return NULL;
137 return map->mic_tm_regs;
138}
139
140struct cbe_mic_tm_regs __iomem *cbe_get_cpu_mic_tm_regs(int cpu)
141{
142 struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
143 if (map == NULL)
144 return NULL;
145 return map->mic_tm_regs;
146}
147EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs);
148
Maynard Johnson18f21902006-11-20 18:45:16 +0100149u32 cbe_get_hw_thread_id(int cpu)
150{
Christian Krafft91a69c92007-04-23 21:35:39 +0200151 return cbe_thread_map[cpu].thread_id;
Maynard Johnson18f21902006-11-20 18:45:16 +0100152}
153EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id);
David Erb22b8c9f2006-10-24 18:31:20 +0200154
Christian Krafft91a69c92007-04-23 21:35:39 +0200155u32 cbe_cpu_to_node(int cpu)
156{
157 return cbe_thread_map[cpu].cbe_id;
158}
159EXPORT_SYMBOL_GPL(cbe_cpu_to_node);
160
161u32 cbe_node_to_cpu(int node)
162{
163 return find_first_bit( (unsigned long *) &cbe_local_mask[node], sizeof(cpumask_t));
164}
165EXPORT_SYMBOL_GPL(cbe_node_to_cpu);
166
Christian Krafft9dd855a2007-04-23 21:35:46 +0200167static 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
177 for (i=0; i<len; i++)
178 if (of_find_node_by_phandle(cpu_handle[i]) == of_get_cpu_node(cpu_id, NULL))
179 return np;
180 }
181
182 return NULL;
183}
184
185void __init cbe_fill_regs_map(struct cbe_regs_map *map)
186{
187 if(map->be_node) {
188 struct device_node *be, *np;
189
190 be = map->be_node;
191
192 for_each_node_by_type(np, "pervasive")
193 if (of_get_parent(np) == be)
194 map->pmd_regs = of_iomap(np, 0);
195
196 for_each_node_by_type(np, "CBEA-Internal-Interrupt-Controller")
197 if (of_get_parent(np) == be)
198 map->iic_regs = of_iomap(np, 2);
199
200 for_each_node_by_type(np, "mic-tm")
201 if (of_get_parent(np) == be)
202 map->mic_tm_regs = of_iomap(np, 0);
203 } else {
204 struct device_node *cpu;
205 /* That hack must die die die ! */
206 const struct address_prop {
207 unsigned long address;
208 unsigned int len;
209 } __attribute__((packed)) *prop;
210
211 cpu = map->cpu_node;
212
213 prop = of_get_property(cpu, "pervasive", NULL);
214 if (prop != NULL)
215 map->pmd_regs = ioremap(prop->address, prop->len);
216
217 prop = of_get_property(cpu, "iic", NULL);
218 if (prop != NULL)
219 map->iic_regs = ioremap(prop->address, prop->len);
220
221 prop = of_get_property(cpu, "mic-tm", NULL);
222 if (prop != NULL)
223 map->mic_tm_regs = ioremap(prop->address, prop->len);
224 }
225}
226
227
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200228void __init cbe_regs_init(void)
229{
230 int i;
Christian Krafft91a69c92007-04-23 21:35:39 +0200231 unsigned int thread_id;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200232 struct device_node *cpu;
233
234 /* Build local fast map of CPUs */
Christian Krafft91a69c92007-04-23 21:35:39 +0200235 for_each_possible_cpu(i) {
236 cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id);
Christian Krafft9dd855a2007-04-23 21:35:46 +0200237 cbe_thread_map[i].be_node = cbe_get_be_node(i);
Christian Krafft91a69c92007-04-23 21:35:39 +0200238 cbe_thread_map[i].thread_id = thread_id;
239 }
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200240
241 /* Find maps for each device tree CPU */
242 for_each_node_by_type(cpu, "cpu") {
Christian Krafft91a69c92007-04-23 21:35:39 +0200243 struct cbe_regs_map *map;
244 unsigned int cbe_id;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200245
Christian Krafft91a69c92007-04-23 21:35:39 +0200246 cbe_id = cbe_regs_map_count++;
247 map = &cbe_regs_maps[cbe_id];
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200248
249 if (cbe_regs_map_count > MAX_CBE) {
250 printk(KERN_ERR "cbe_regs: More BE chips than supported"
251 "!\n");
252 cbe_regs_map_count--;
253 return;
254 }
255 map->cpu_node = cpu;
Christian Krafft91a69c92007-04-23 21:35:39 +0200256
257 for_each_possible_cpu(i) {
258 struct cbe_thread_map *thread = &cbe_thread_map[i];
259
260 if (thread->cpu_node == cpu) {
261 thread->regs = map;
262 thread->cbe_id = cbe_id;
Christian Krafft9dd855a2007-04-23 21:35:46 +0200263 map->be_node = thread->be_node;
Christian Krafft91a69c92007-04-23 21:35:39 +0200264 cpu_set(i, cbe_local_mask[cbe_id]);
265 if(thread->thread_id == 0)
266 cpu_set(i, cbe_first_online_cpu);
267 }
268 }
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200269
Christian Krafft9dd855a2007-04-23 21:35:46 +0200270 cbe_fill_regs_map(map);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200271 }
272}
273