- patches.apparmor/remove_suid_new_case_in_2.6.22.diff: Merge fix.
[linux-flexiantxendom0-3.2.10.git] / arch / powerpc / platforms / cell / spu_manage.c
1 /*
2  * spu management operations for of based platforms
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  * Copyright 2006 Sony Corp.
6  * (C) Copyright 2007 TOSHIBA CORPORATION
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <linux/interrupt.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/wait.h>
28 #include <linux/mm.h>
29 #include <linux/io.h>
30 #include <linux/mutex.h>
31 #include <linux/device.h>
32
33 #include <asm/spu.h>
34 #include <asm/spu_priv1.h>
35 #include <asm/firmware.h>
36 #include <asm/prom.h>
37
38 #include "spufs/spufs.h"
39 #include "interrupt.h"
40
41 struct device_node *spu_devnode(struct spu *spu)
42 {
43         return spu->devnode;
44 }
45
46 EXPORT_SYMBOL_GPL(spu_devnode);
47
48 static u64 __init find_spu_unit_number(struct device_node *spe)
49 {
50         const unsigned int *prop;
51         int proplen;
52         prop = of_get_property(spe, "unit-id", &proplen);
53         if (proplen == 4)
54                 return (u64)*prop;
55
56         prop = of_get_property(spe, "reg", &proplen);
57         if (proplen == 4)
58                 return (u64)*prop;
59
60         return 0;
61 }
62
63 static void spu_unmap(struct spu *spu)
64 {
65         if (!firmware_has_feature(FW_FEATURE_LPAR))
66                 iounmap(spu->priv1);
67         iounmap(spu->priv2);
68         iounmap(spu->problem);
69         iounmap((__force u8 __iomem *)spu->local_store);
70 }
71
72 static int __init spu_map_interrupts_old(struct spu *spu,
73         struct device_node *np)
74 {
75         unsigned int isrc;
76         const u32 *tmp;
77         int nid;
78
79         /* Get the interrupt source unit from the device-tree */
80         tmp = of_get_property(np, "isrc", NULL);
81         if (!tmp)
82                 return -ENODEV;
83         isrc = tmp[0];
84
85         tmp = of_get_property(np->parent->parent, "node-id", NULL);
86         if (!tmp) {
87                 printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
88                 nid = spu->node;
89         } else
90                 nid = tmp[0];
91
92         /* Add the node number */
93         isrc |= nid << IIC_IRQ_NODE_SHIFT;
94
95         /* Now map interrupts of all 3 classes */
96         spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
97         spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
98         spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
99
100         /* Right now, we only fail if class 2 failed */
101         return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
102 }
103
104 static void __iomem * __init spu_map_prop_old(struct spu *spu,
105                                               struct device_node *n,
106                                               const char *name)
107 {
108         const struct address_prop {
109                 unsigned long address;
110                 unsigned int len;
111         } __attribute__((packed)) *prop;
112         int proplen;
113
114         prop = of_get_property(n, name, &proplen);
115         if (prop == NULL || proplen != sizeof (struct address_prop))
116                 return NULL;
117
118         return ioremap(prop->address, prop->len);
119 }
120
121 static int __init spu_map_device_old(struct spu *spu)
122 {
123         struct device_node *node = spu->devnode;
124         const char *prop;
125         int ret;
126
127         ret = -ENODEV;
128         spu->name = of_get_property(node, "name", NULL);
129         if (!spu->name)
130                 goto out;
131
132         prop = of_get_property(node, "local-store", NULL);
133         if (!prop)
134                 goto out;
135         spu->local_store_phys = *(unsigned long *)prop;
136
137         /* we use local store as ram, not io memory */
138         spu->local_store = (void __force *)
139                 spu_map_prop_old(spu, node, "local-store");
140         if (!spu->local_store)
141                 goto out;
142
143         prop = of_get_property(node, "problem", NULL);
144         if (!prop)
145                 goto out_unmap;
146         spu->problem_phys = *(unsigned long *)prop;
147
148         spu->problem = spu_map_prop_old(spu, node, "problem");
149         if (!spu->problem)
150                 goto out_unmap;
151
152         spu->priv2 = spu_map_prop_old(spu, node, "priv2");
153         if (!spu->priv2)
154                 goto out_unmap;
155
156         if (!firmware_has_feature(FW_FEATURE_LPAR)) {
157                 spu->priv1 = spu_map_prop_old(spu, node, "priv1");
158                 if (!spu->priv1)
159                         goto out_unmap;
160         }
161
162         ret = 0;
163         goto out;
164
165 out_unmap:
166         spu_unmap(spu);
167 out:
168         return ret;
169 }
170
171 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
172 {
173         struct of_irq oirq;
174         int ret;
175         int i;
176
177         for (i=0; i < 3; i++) {
178                 ret = of_irq_map_one(np, i, &oirq);
179                 if (ret) {
180                         pr_debug("spu_new: failed to get irq %d\n", i);
181                         goto err;
182                 }
183                 ret = -EINVAL;
184                 pr_debug("  irq %d no 0x%x on %s\n", i, oirq.specifier[0],
185                          oirq.controller->full_name);
186                 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
187                                         oirq.specifier, oirq.size);
188                 if (spu->irqs[i] == NO_IRQ) {
189                         pr_debug("spu_new: failed to map it !\n");
190                         goto err;
191                 }
192         }
193         return 0;
194
195 err:
196         pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
197                 spu->name);
198         for (; i >= 0; i--) {
199                 if (spu->irqs[i] != NO_IRQ)
200                         irq_dispose_mapping(spu->irqs[i]);
201         }
202         return ret;
203 }
204
205 static int spu_map_resource(struct spu *spu, int nr,
206                             void __iomem** virt, unsigned long *phys)
207 {
208         struct device_node *np = spu->devnode;
209         struct resource resource = { };
210         unsigned long len;
211         int ret;
212
213         ret = of_address_to_resource(np, nr, &resource);
214         if (ret)
215                 return ret;
216         if (phys)
217                 *phys = resource.start;
218         len = resource.end - resource.start + 1;
219         *virt = ioremap(resource.start, len);
220         if (!*virt)
221                 return -EINVAL;
222         return 0;
223 }
224
225 static int __init spu_map_device(struct spu *spu)
226 {
227         struct device_node *np = spu->devnode;
228         int ret = -ENODEV;
229
230         spu->name = of_get_property(np, "name", NULL);
231         if (!spu->name)
232                 goto out;
233
234         ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
235                                &spu->local_store_phys);
236         if (ret) {
237                 pr_debug("spu_new: failed to map %s resource 0\n",
238                          np->full_name);
239                 goto out;
240         }
241         ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
242                                &spu->problem_phys);
243         if (ret) {
244                 pr_debug("spu_new: failed to map %s resource 1\n",
245                          np->full_name);
246                 goto out_unmap;
247         }
248         ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
249         if (ret) {
250                 pr_debug("spu_new: failed to map %s resource 2\n",
251                          np->full_name);
252                 goto out_unmap;
253         }
254         if (!firmware_has_feature(FW_FEATURE_LPAR))
255                 ret = spu_map_resource(spu, 3,
256                                (void __iomem**)&spu->priv1, NULL);
257         if (ret) {
258                 pr_debug("spu_new: failed to map %s resource 3\n",
259                          np->full_name);
260                 goto out_unmap;
261         }
262         pr_debug("spu_new: %s maps:\n", np->full_name);
263         pr_debug("  local store   : 0x%016lx -> 0x%p\n",
264                  spu->local_store_phys, spu->local_store);
265         pr_debug("  problem state : 0x%016lx -> 0x%p\n",
266                  spu->problem_phys, spu->problem);
267         pr_debug("  priv2         :                       0x%p\n", spu->priv2);
268         pr_debug("  priv1         :                       0x%p\n", spu->priv1);
269
270         return 0;
271
272 out_unmap:
273         spu_unmap(spu);
274 out:
275         pr_debug("failed to map spe %s: %d\n", spu->name, ret);
276         return ret;
277 }
278
279 static int __init of_enumerate_spus(int (*fn)(void *data))
280 {
281         int ret;
282         struct device_node *node;
283
284         ret = -ENODEV;
285         for (node = of_find_node_by_type(NULL, "spe");
286                         node; node = of_find_node_by_type(node, "spe")) {
287                 ret = fn(node);
288                 if (ret) {
289                         printk(KERN_WARNING "%s: Error initializing %s\n",
290                                 __FUNCTION__, node->name);
291                         break;
292                 }
293         }
294         return ret;
295 }
296
297 static int __init of_create_spu(struct spu *spu, void *data)
298 {
299         int ret;
300         struct device_node *spe = (struct device_node *)data;
301         static int legacy_map = 0, legacy_irq = 0;
302
303         spu->devnode = of_node_get(spe);
304         spu->spe_id = find_spu_unit_number(spe);
305
306         spu->node = of_node_to_nid(spe);
307         if (spu->node >= MAX_NUMNODES) {
308                 printk(KERN_WARNING "SPE %s on node %d ignored,"
309                        " node number too big\n", spe->full_name, spu->node);
310                 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
311                 ret = -ENODEV;
312                 goto out;
313         }
314
315         ret = spu_map_device(spu);
316         if (ret) {
317                 if (!legacy_map) {
318                         legacy_map = 1;
319                         printk(KERN_WARNING "%s: Legacy device tree found, "
320                                 "trying to map old style\n", __FUNCTION__);
321                 }
322                 ret = spu_map_device_old(spu);
323                 if (ret) {
324                         printk(KERN_ERR "Unable to map %s\n",
325                                 spu->name);
326                         goto out;
327                 }
328         }
329
330         ret = spu_map_interrupts(spu, spe);
331         if (ret) {
332                 if (!legacy_irq) {
333                         legacy_irq = 1;
334                         printk(KERN_WARNING "%s: Legacy device tree found, "
335                                 "trying old style irq\n", __FUNCTION__);
336                 }
337                 ret = spu_map_interrupts_old(spu, spe);
338                 if (ret) {
339                         printk(KERN_ERR "%s: could not map interrupts",
340                                 spu->name);
341                         goto out_unmap;
342                 }
343         }
344
345         pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
346                 spu->local_store, spu->problem, spu->priv1,
347                 spu->priv2, spu->number);
348         goto out;
349
350 out_unmap:
351         spu_unmap(spu);
352 out:
353         return ret;
354 }
355
356 static int of_destroy_spu(struct spu *spu)
357 {
358         spu_unmap(spu);
359         of_node_put(spu->devnode);
360         return 0;
361 }
362
363 static int enable_spu_by_master_run(struct spu_context *ctx)
364 {
365         ctx->ops->master_start(ctx);
366         return 0;
367 }
368
369 static int disable_spu_by_master_run(struct spu_context *ctx)
370 {
371         ctx->ops->master_stop(ctx);
372         return 0;
373 }
374
375 const struct spu_management_ops spu_management_of_ops = {
376         .enumerate_spus = of_enumerate_spus,
377         .create_spu = of_create_spu,
378         .destroy_spu = of_destroy_spu,
379         .enable_spu = enable_spu_by_master_run,
380         .disable_spu = disable_spu_by_master_run,
381 };