2 * spu management operations for of based platforms
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 * Copyright 2006 Sony Corp.
6 * (C) Copyright 2007 TOSHIBA CORPORATION
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.
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.
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.
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>
30 #include <linux/mutex.h>
31 #include <linux/device.h>
34 #include <asm/spu_priv1.h>
35 #include <asm/firmware.h>
38 #include "spufs/spufs.h"
39 #include "interrupt.h"
41 struct device_node *spu_devnode(struct spu *spu)
46 EXPORT_SYMBOL_GPL(spu_devnode);
48 static u64 __init find_spu_unit_number(struct device_node *spe)
50 const unsigned int *prop;
52 prop = of_get_property(spe, "unit-id", &proplen);
56 prop = of_get_property(spe, "reg", &proplen);
63 static void spu_unmap(struct spu *spu)
65 if (!firmware_has_feature(FW_FEATURE_LPAR))
68 iounmap(spu->problem);
69 iounmap((__force u8 __iomem *)spu->local_store);
72 static int __init spu_map_interrupts_old(struct spu *spu,
73 struct device_node *np)
79 /* Get the interrupt source unit from the device-tree */
80 tmp = of_get_property(np, "isrc", NULL);
85 tmp = of_get_property(np->parent->parent, "node-id", NULL);
87 printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
92 /* Add the node number */
93 isrc |= nid << IIC_IRQ_NODE_SHIFT;
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);
100 /* Right now, we only fail if class 2 failed */
101 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
104 static void __iomem * __init spu_map_prop_old(struct spu *spu,
105 struct device_node *n,
108 const struct address_prop {
109 unsigned long address;
111 } __attribute__((packed)) *prop;
114 prop = of_get_property(n, name, &proplen);
115 if (prop == NULL || proplen != sizeof (struct address_prop))
118 return ioremap(prop->address, prop->len);
121 static int __init spu_map_device_old(struct spu *spu)
123 struct device_node *node = spu->devnode;
128 spu->name = of_get_property(node, "name", NULL);
132 prop = of_get_property(node, "local-store", NULL);
135 spu->local_store_phys = *(unsigned long *)prop;
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)
143 prop = of_get_property(node, "problem", NULL);
146 spu->problem_phys = *(unsigned long *)prop;
148 spu->problem = spu_map_prop_old(spu, node, "problem");
152 spu->priv2 = spu_map_prop_old(spu, node, "priv2");
156 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
157 spu->priv1 = spu_map_prop_old(spu, node, "priv1");
171 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
177 for (i=0; i < 3; i++) {
178 ret = of_irq_map_one(np, i, &oirq);
180 pr_debug("spu_new: failed to get irq %d\n", i);
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");
196 pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
198 for (; i >= 0; i--) {
199 if (spu->irqs[i] != NO_IRQ)
200 irq_dispose_mapping(spu->irqs[i]);
205 static int spu_map_resource(struct spu *spu, int nr,
206 void __iomem** virt, unsigned long *phys)
208 struct device_node *np = spu->devnode;
209 struct resource resource = { };
213 ret = of_address_to_resource(np, nr, &resource);
217 *phys = resource.start;
218 len = resource.end - resource.start + 1;
219 *virt = ioremap(resource.start, len);
225 static int __init spu_map_device(struct spu *spu)
227 struct device_node *np = spu->devnode;
230 spu->name = of_get_property(np, "name", NULL);
234 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
235 &spu->local_store_phys);
237 pr_debug("spu_new: failed to map %s resource 0\n",
241 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
244 pr_debug("spu_new: failed to map %s resource 1\n",
248 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
250 pr_debug("spu_new: failed to map %s resource 2\n",
254 if (!firmware_has_feature(FW_FEATURE_LPAR))
255 ret = spu_map_resource(spu, 3,
256 (void __iomem**)&spu->priv1, NULL);
258 pr_debug("spu_new: failed to map %s resource 3\n",
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);
275 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
279 static int __init of_enumerate_spus(int (*fn)(void *data))
282 struct device_node *node;
285 for (node = of_find_node_by_type(NULL, "spe");
286 node; node = of_find_node_by_type(node, "spe")) {
289 printk(KERN_WARNING "%s: Error initializing %s\n",
290 __FUNCTION__, node->name);
297 static int __init of_create_spu(struct spu *spu, void *data)
300 struct device_node *spe = (struct device_node *)data;
301 static int legacy_map = 0, legacy_irq = 0;
303 spu->devnode = of_node_get(spe);
304 spu->spe_id = find_spu_unit_number(spe);
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");
315 ret = spu_map_device(spu);
319 printk(KERN_WARNING "%s: Legacy device tree found, "
320 "trying to map old style\n", __FUNCTION__);
322 ret = spu_map_device_old(spu);
324 printk(KERN_ERR "Unable to map %s\n",
330 ret = spu_map_interrupts(spu, spe);
334 printk(KERN_WARNING "%s: Legacy device tree found, "
335 "trying old style irq\n", __FUNCTION__);
337 ret = spu_map_interrupts_old(spu, spe);
339 printk(KERN_ERR "%s: could not map interrupts",
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);
356 static int of_destroy_spu(struct spu *spu)
359 of_node_put(spu->devnode);
363 static int enable_spu_by_master_run(struct spu_context *ctx)
365 ctx->ops->master_start(ctx);
369 static int disable_spu_by_master_run(struct spu_context *ctx)
371 ctx->ops->master_stop(ctx);
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,