Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[pandora-kernel.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 "interrupt.h"
39
40 struct device_node *spu_devnode(struct spu *spu)
41 {
42         return spu->devnode;
43 }
44
45 EXPORT_SYMBOL_GPL(spu_devnode);
46
47 static u64 __init find_spu_unit_number(struct device_node *spe)
48 {
49         const unsigned int *prop;
50         int proplen;
51         prop = get_property(spe, "unit-id", &proplen);
52         if (proplen == 4)
53                 return (u64)*prop;
54
55         prop = get_property(spe, "reg", &proplen);
56         if (proplen == 4)
57                 return (u64)*prop;
58
59         return 0;
60 }
61
62 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
63                 const char *prop)
64 {
65         const struct address_prop {
66                 unsigned long address;
67                 unsigned int len;
68         } __attribute__((packed)) *p;
69         int proplen;
70
71         unsigned long start_pfn, nr_pages;
72         struct pglist_data *pgdata;
73         struct zone *zone;
74         int ret;
75
76         p = get_property(spe, prop, &proplen);
77         WARN_ON(proplen != sizeof (*p));
78
79         start_pfn = p->address >> PAGE_SHIFT;
80         nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
81
82         pgdata = NODE_DATA(spu->node);
83         zone = pgdata->node_zones;
84
85         ret = __add_pages(zone, start_pfn, nr_pages);
86
87         return ret;
88 }
89
90 static void __iomem * __init map_spe_prop(struct spu *spu,
91                 struct device_node *n, const char *name)
92 {
93         const struct address_prop {
94                 unsigned long address;
95                 unsigned int len;
96         } __attribute__((packed)) *prop;
97
98         const void *p;
99         int proplen;
100         void __iomem *ret = NULL;
101         int err = 0;
102
103         p = get_property(n, name, &proplen);
104         if (proplen != sizeof (struct address_prop))
105                 return NULL;
106
107         prop = p;
108
109         err = cell_spuprop_present(spu, n, name);
110         if (err && (err != -EEXIST))
111                 goto out;
112
113         ret = ioremap(prop->address, prop->len);
114
115  out:
116         return ret;
117 }
118
119 static void spu_unmap(struct spu *spu)
120 {
121         if (!firmware_has_feature(FW_FEATURE_LPAR))
122                 iounmap(spu->priv1);
123         iounmap(spu->priv2);
124         iounmap(spu->problem);
125         iounmap((__force u8 __iomem *)spu->local_store);
126 }
127
128 static int __init spu_map_interrupts_old(struct spu *spu,
129         struct device_node *np)
130 {
131         unsigned int isrc;
132         const u32 *tmp;
133         int nid;
134
135         /* Get the interrupt source unit from the device-tree */
136         tmp = get_property(np, "isrc", NULL);
137         if (!tmp)
138                 return -ENODEV;
139         isrc = tmp[0];
140
141         tmp = get_property(np->parent->parent, "node-id", NULL);
142         if (!tmp) {
143                 printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
144                 nid = spu->node;
145         } else
146                 nid = tmp[0];
147
148         /* Add the node number */
149         isrc |= nid << IIC_IRQ_NODE_SHIFT;
150
151         /* Now map interrupts of all 3 classes */
152         spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
153         spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
154         spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
155
156         /* Right now, we only fail if class 2 failed */
157         return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
158 }
159
160 static int __init spu_map_device_old(struct spu *spu)
161 {
162         struct device_node *node = spu->devnode;
163         const char *prop;
164         int ret;
165
166         ret = -ENODEV;
167         spu->name = get_property(node, "name", NULL);
168         if (!spu->name)
169                 goto out;
170
171         prop = get_property(node, "local-store", NULL);
172         if (!prop)
173                 goto out;
174         spu->local_store_phys = *(unsigned long *)prop;
175
176         /* we use local store as ram, not io memory */
177         spu->local_store = (void __force *)
178                 map_spe_prop(spu, node, "local-store");
179         if (!spu->local_store)
180                 goto out;
181
182         prop = get_property(node, "problem", NULL);
183         if (!prop)
184                 goto out_unmap;
185         spu->problem_phys = *(unsigned long *)prop;
186
187         spu->problem = map_spe_prop(spu, node, "problem");
188         if (!spu->problem)
189                 goto out_unmap;
190
191         spu->priv2 = map_spe_prop(spu, node, "priv2");
192         if (!spu->priv2)
193                 goto out_unmap;
194
195         if (!firmware_has_feature(FW_FEATURE_LPAR)) {
196                 spu->priv1 = map_spe_prop(spu, node, "priv1");
197                 if (!spu->priv1)
198                         goto out_unmap;
199         }
200
201         ret = 0;
202         goto out;
203
204 out_unmap:
205         spu_unmap(spu);
206 out:
207         return ret;
208 }
209
210 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
211 {
212         struct of_irq oirq;
213         int ret;
214         int i;
215
216         for (i=0; i < 3; i++) {
217                 ret = of_irq_map_one(np, i, &oirq);
218                 if (ret) {
219                         pr_debug("spu_new: failed to get irq %d\n", i);
220                         goto err;
221                 }
222                 ret = -EINVAL;
223                 pr_debug("  irq %d no 0x%x on %s\n", i, oirq.specifier[0],
224                          oirq.controller->full_name);
225                 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
226                                         oirq.specifier, oirq.size);
227                 if (spu->irqs[i] == NO_IRQ) {
228                         pr_debug("spu_new: failed to map it !\n");
229                         goto err;
230                 }
231         }
232         return 0;
233
234 err:
235         pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
236                 spu->name);
237         for (; i >= 0; i--) {
238                 if (spu->irqs[i] != NO_IRQ)
239                         irq_dispose_mapping(spu->irqs[i]);
240         }
241         return ret;
242 }
243
244 static int spu_map_resource(struct spu *spu, int nr,
245                             void __iomem** virt, unsigned long *phys)
246 {
247         struct device_node *np = spu->devnode;
248         unsigned long start_pfn, nr_pages;
249         struct pglist_data *pgdata;
250         struct zone *zone;
251         struct resource resource = { };
252         unsigned long len;
253         int ret;
254
255         ret = of_address_to_resource(np, nr, &resource);
256         if (ret)
257                 goto out;
258
259         if (phys)
260                 *phys = resource.start;
261         len = resource.end - resource.start + 1;
262         *virt = ioremap(resource.start, len);
263         if (!*virt)
264                 ret = -EINVAL;
265
266         start_pfn = resource.start >> PAGE_SHIFT;
267         nr_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
268
269         pgdata = NODE_DATA(spu->node);
270         zone = pgdata->node_zones;
271
272         ret = __add_pages(zone, start_pfn, nr_pages);
273
274 out:
275         return ret;
276 }
277
278 static int __init spu_map_device(struct spu *spu)
279 {
280         struct device_node *np = spu->devnode;
281         int ret = -ENODEV;
282
283         spu->name = get_property(np, "name", NULL);
284         if (!spu->name)
285                 goto out;
286
287         ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
288                                &spu->local_store_phys);
289         if (ret) {
290                 pr_debug("spu_new: failed to map %s resource 0\n",
291                          np->full_name);
292                 goto out;
293         }
294         ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
295                                &spu->problem_phys);
296         if (ret) {
297                 pr_debug("spu_new: failed to map %s resource 1\n",
298                          np->full_name);
299                 goto out_unmap;
300         }
301         ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
302         if (ret) {
303                 pr_debug("spu_new: failed to map %s resource 2\n",
304                          np->full_name);
305                 goto out_unmap;
306         }
307         if (!firmware_has_feature(FW_FEATURE_LPAR))
308                 ret = spu_map_resource(spu, 3,
309                                (void __iomem**)&spu->priv1, NULL);
310         if (ret) {
311                 pr_debug("spu_new: failed to map %s resource 3\n",
312                          np->full_name);
313                 goto out_unmap;
314         }
315         pr_debug("spu_new: %s maps:\n", np->full_name);
316         pr_debug("  local store   : 0x%016lx -> 0x%p\n",
317                  spu->local_store_phys, spu->local_store);
318         pr_debug("  problem state : 0x%016lx -> 0x%p\n",
319                  spu->problem_phys, spu->problem);
320         pr_debug("  priv2         :                       0x%p\n", spu->priv2);
321         pr_debug("  priv1         :                       0x%p\n", spu->priv1);
322
323         return 0;
324
325 out_unmap:
326         spu_unmap(spu);
327 out:
328         pr_debug("failed to map spe %s: %d\n", spu->name, ret);
329         return ret;
330 }
331
332 static int __init of_enumerate_spus(int (*fn)(void *data))
333 {
334         int ret;
335         struct device_node *node;
336
337         ret = -ENODEV;
338         for (node = of_find_node_by_type(NULL, "spe");
339                         node; node = of_find_node_by_type(node, "spe")) {
340                 ret = fn(node);
341                 if (ret) {
342                         printk(KERN_WARNING "%s: Error initializing %s\n",
343                                 __FUNCTION__, node->name);
344                         break;
345                 }
346         }
347         return ret;
348 }
349
350 static int __init of_create_spu(struct spu *spu, void *data)
351 {
352         int ret;
353         struct device_node *spe = (struct device_node *)data;
354         static int legacy_map = 0, legacy_irq = 0;
355
356         spu->devnode = of_node_get(spe);
357         spu->spe_id = find_spu_unit_number(spe);
358
359         spu->node = of_node_to_nid(spe);
360         if (spu->node >= MAX_NUMNODES) {
361                 printk(KERN_WARNING "SPE %s on node %d ignored,"
362                        " node number too big\n", spe->full_name, spu->node);
363                 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
364                 ret = -ENODEV;
365                 goto out;
366         }
367
368         ret = spu_map_device(spu);
369         if (ret) {
370                 if (!legacy_map) {
371                         legacy_map = 1;
372                         printk(KERN_WARNING "%s: Legacy device tree found, "
373                                 "trying to map old style\n", __FUNCTION__);
374                 }
375                 ret = spu_map_device_old(spu);
376                 if (ret) {
377                         printk(KERN_ERR "Unable to map %s\n",
378                                 spu->name);
379                         goto out;
380                 }
381         }
382
383         ret = spu_map_interrupts(spu, spe);
384         if (ret) {
385                 if (!legacy_irq) {
386                         legacy_irq = 1;
387                         printk(KERN_WARNING "%s: Legacy device tree found, "
388                                 "trying old style irq\n", __FUNCTION__);
389                 }
390                 ret = spu_map_interrupts_old(spu, spe);
391                 if (ret) {
392                         printk(KERN_ERR "%s: could not map interrupts",
393                                 spu->name);
394                         goto out_unmap;
395                 }
396         }
397
398         pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
399                 spu->local_store, spu->problem, spu->priv1,
400                 spu->priv2, spu->number);
401         goto out;
402
403 out_unmap:
404         spu_unmap(spu);
405 out:
406         return ret;
407 }
408
409 static int of_destroy_spu(struct spu *spu)
410 {
411         spu_unmap(spu);
412         of_node_put(spu->devnode);
413         return 0;
414 }
415
416 const struct spu_management_ops spu_management_of_ops = {
417         .enumerate_spus = of_enumerate_spus,
418         .create_spu = of_create_spu,
419         .destroy_spu = of_destroy_spu,
420 };