]> err.no Git - linux-2.6/blob - arch/powerpc/platforms/cell/cbe_regs.c
[POWERPC] cell: add cbe_node_to_cpu function
[linux-2.6] / arch / powerpc / platforms / cell / cbe_regs.c
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
9 #include <linux/percpu.h>
10 #include <linux/types.h>
11 #include <linux/module.h>
12
13 #include <asm/io.h>
14 #include <asm/pgtable.h>
15 #include <asm/prom.h>
16 #include <asm/ptrace.h>
17
18 #include "cbe_regs.h"
19
20 /*
21  * Current implementation uses "cpu" nodes. We build our own mapping
22  * array of cpu numbers to cpu nodes locally for now to allow interrupt
23  * time code to have a fast path rather than call of_get_cpu_node(). If
24  * we implement cpu hotplug, we'll have to install an appropriate norifier
25  * in order to release references to the cpu going away
26  */
27 static struct cbe_regs_map
28 {
29         struct device_node *cpu_node;
30         struct cbe_pmd_regs __iomem *pmd_regs;
31         struct cbe_iic_regs __iomem *iic_regs;
32         struct cbe_mic_tm_regs __iomem *mic_tm_regs;
33         struct cbe_pmd_shadow_regs pmd_shadow_regs;
34 } cbe_regs_maps[MAX_CBE];
35 static int cbe_regs_map_count;
36
37 static struct cbe_thread_map
38 {
39         struct device_node *cpu_node;
40         struct cbe_regs_map *regs;
41         unsigned int thread_id;
42         unsigned int cbe_id;
43 } cbe_thread_map[NR_CPUS];
44
45 static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = CPU_MASK_NONE };
46 static cpumask_t cbe_first_online_cpu = CPU_MASK_NONE;
47
48 static struct cbe_regs_map *cbe_find_map(struct device_node *np)
49 {
50         int i;
51         struct device_node *tmp_np;
52
53         if (strcasecmp(np->type, "spe") == 0) {
54                 if (np->data == NULL) {
55                         /* walk up path until cpu node was found */
56                         tmp_np = np->parent;
57                         while (tmp_np != NULL && strcasecmp(tmp_np->type, "cpu") != 0)
58                                 tmp_np = tmp_np->parent;
59
60                         np->data = cbe_find_map(tmp_np);
61                 }
62                 return np->data;
63         }
64
65         for (i = 0; i < cbe_regs_map_count; i++)
66                 if (cbe_regs_maps[i].cpu_node == np)
67                         return &cbe_regs_maps[i];
68         return NULL;
69 }
70
71 struct cbe_pmd_regs __iomem *cbe_get_pmd_regs(struct device_node *np)
72 {
73         struct cbe_regs_map *map = cbe_find_map(np);
74         if (map == NULL)
75                 return NULL;
76         return map->pmd_regs;
77 }
78 EXPORT_SYMBOL_GPL(cbe_get_pmd_regs);
79
80 struct cbe_pmd_regs __iomem *cbe_get_cpu_pmd_regs(int cpu)
81 {
82         struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
83         if (map == NULL)
84                 return NULL;
85         return map->pmd_regs;
86 }
87 EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs);
88
89 struct cbe_pmd_shadow_regs *cbe_get_pmd_shadow_regs(struct device_node *np)
90 {
91         struct cbe_regs_map *map = cbe_find_map(np);
92         if (map == NULL)
93                 return NULL;
94         return &map->pmd_shadow_regs;
95 }
96
97 struct cbe_pmd_shadow_regs *cbe_get_cpu_pmd_shadow_regs(int cpu)
98 {
99         struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
100         if (map == NULL)
101                 return NULL;
102         return &map->pmd_shadow_regs;
103 }
104
105 struct cbe_iic_regs __iomem *cbe_get_iic_regs(struct device_node *np)
106 {
107         struct cbe_regs_map *map = cbe_find_map(np);
108         if (map == NULL)
109                 return NULL;
110         return map->iic_regs;
111 }
112
113 struct cbe_iic_regs __iomem *cbe_get_cpu_iic_regs(int cpu)
114 {
115         struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
116         if (map == NULL)
117                 return NULL;
118         return map->iic_regs;
119 }
120
121 struct cbe_mic_tm_regs __iomem *cbe_get_mic_tm_regs(struct device_node *np)
122 {
123         struct cbe_regs_map *map = cbe_find_map(np);
124         if (map == NULL)
125                 return NULL;
126         return map->mic_tm_regs;
127 }
128
129 struct cbe_mic_tm_regs __iomem *cbe_get_cpu_mic_tm_regs(int cpu)
130 {
131         struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
132         if (map == NULL)
133                 return NULL;
134         return map->mic_tm_regs;
135 }
136 EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs);
137
138 u32 cbe_get_hw_thread_id(int cpu)
139 {
140         return cbe_thread_map[cpu].thread_id;
141 }
142 EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id);
143
144 u32 cbe_cpu_to_node(int cpu)
145 {
146         return cbe_thread_map[cpu].cbe_id;
147 }
148 EXPORT_SYMBOL_GPL(cbe_cpu_to_node);
149
150 u32 cbe_node_to_cpu(int node)
151 {
152         return find_first_bit( (unsigned long *) &cbe_local_mask[node], sizeof(cpumask_t));
153 }
154 EXPORT_SYMBOL_GPL(cbe_node_to_cpu);
155
156 void __init cbe_regs_init(void)
157 {
158         int i;
159         unsigned int thread_id;
160         struct device_node *cpu;
161
162         /* Build local fast map of CPUs */
163         for_each_possible_cpu(i) {
164                 cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id);
165                 cbe_thread_map[i].thread_id = thread_id;
166         }
167
168         /* Find maps for each device tree CPU */
169         for_each_node_by_type(cpu, "cpu") {
170                 struct cbe_regs_map *map;
171                 unsigned int cbe_id;
172
173                 /* That hack must die die die ! */
174                 const struct address_prop {
175                         unsigned long address;
176                         unsigned int len;
177                 } __attribute__((packed)) *prop;
178
179                 cbe_id = cbe_regs_map_count++;
180                 map = &cbe_regs_maps[cbe_id];
181
182                 if (cbe_regs_map_count > MAX_CBE) {
183                         printk(KERN_ERR "cbe_regs: More BE chips than supported"
184                                "!\n");
185                         cbe_regs_map_count--;
186                         return;
187                 }
188                 map->cpu_node = cpu;
189
190                 for_each_possible_cpu(i) {
191                         struct cbe_thread_map *thread = &cbe_thread_map[i];
192
193                         if (thread->cpu_node == cpu) {
194                                 thread->regs = map;
195                                 thread->cbe_id = cbe_id;
196                                 cpu_set(i, cbe_local_mask[cbe_id]);
197                                 if(thread->thread_id == 0)
198                                         cpu_set(i, cbe_first_online_cpu);
199                         }
200                 }
201
202                 prop = of_get_property(cpu, "pervasive", NULL);
203                 if (prop != NULL)
204                         map->pmd_regs = ioremap(prop->address, prop->len);
205
206                 prop = of_get_property(cpu, "iic", NULL);
207                 if (prop != NULL)
208                         map->iic_regs = ioremap(prop->address, prop->len);
209
210                 prop = of_get_property(cpu, "mic-tm", NULL);
211                 if (prop != NULL)
212                         map->mic_tm_regs = ioremap(prop->address, prop->len);
213         }
214 }
215