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 "interrupt.h"
40 struct device_node *spu_devnode(struct spu *spu)
45 EXPORT_SYMBOL_GPL(spu_devnode);
47 static u64 __init find_spu_unit_number(struct device_node *spe)
49 const unsigned int *prop;
51 prop = get_property(spe, "unit-id", &proplen);
55 prop = get_property(spe, "reg", &proplen);
62 static void spu_unmap(struct spu *spu)
64 if (!firmware_has_feature(FW_FEATURE_LPAR))
67 iounmap(spu->problem);
68 iounmap((__force u8 __iomem *)spu->local_store);
71 static int __init spu_map_interrupts_old(struct spu *spu,
72 struct device_node *np)
78 /* Get the interrupt source unit from the device-tree */
79 tmp = get_property(np, "isrc", NULL);
84 tmp = get_property(np->parent->parent, "node-id", NULL);
86 printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
91 /* Add the node number */
92 isrc |= nid << IIC_IRQ_NODE_SHIFT;
94 /* Now map interrupts of all 3 classes */
95 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
96 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
97 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
99 /* Right now, we only fail if class 2 failed */
100 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
103 static void __iomem * __init spu_map_prop_old(struct spu *spu,
104 struct device_node *n,
107 const struct address_prop {
108 unsigned long address;
110 } __attribute__((packed)) *prop;
113 prop = get_property(n, name, &proplen);
114 if (prop == NULL || proplen != sizeof (struct address_prop))
117 return ioremap(prop->address, prop->len);
120 static int __init spu_map_device_old(struct spu *spu)
122 struct device_node *node = spu->devnode;
127 spu->name = get_property(node, "name", NULL);
131 prop = get_property(node, "local-store", NULL);
134 spu->local_store_phys = *(unsigned long *)prop;
136 /* we use local store as ram, not io memory */
137 spu->local_store = (void __force *)
138 spu_map_prop_old(spu, node, "local-store");
139 if (!spu->local_store)
142 prop = get_property(node, "problem", NULL);
145 spu->problem_phys = *(unsigned long *)prop;
147 spu->problem = spu_map_prop_old(spu, node, "problem");
151 spu->priv2 = spu_map_prop_old(spu, node, "priv2");
155 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
156 spu->priv1 = spu_map_prop_old(spu, node, "priv1");
170 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
176 for (i=0; i < 3; i++) {
177 ret = of_irq_map_one(np, i, &oirq);
179 pr_debug("spu_new: failed to get irq %d\n", i);
183 pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
184 oirq.controller->full_name);
185 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
186 oirq.specifier, oirq.size);
187 if (spu->irqs[i] == NO_IRQ) {
188 pr_debug("spu_new: failed to map it !\n");
195 pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
197 for (; i >= 0; i--) {
198 if (spu->irqs[i] != NO_IRQ)
199 irq_dispose_mapping(spu->irqs[i]);
204 static int spu_map_resource(struct spu *spu, int nr,
205 void __iomem** virt, unsigned long *phys)
207 struct device_node *np = spu->devnode;
208 struct resource resource = { };
212 ret = of_address_to_resource(np, nr, &resource);
216 *phys = resource.start;
217 len = resource.end - resource.start + 1;
218 *virt = ioremap(resource.start, len);
224 static int __init spu_map_device(struct spu *spu)
226 struct device_node *np = spu->devnode;
229 spu->name = get_property(np, "name", NULL);
233 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
234 &spu->local_store_phys);
236 pr_debug("spu_new: failed to map %s resource 0\n",
240 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
243 pr_debug("spu_new: failed to map %s resource 1\n",
247 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
249 pr_debug("spu_new: failed to map %s resource 2\n",
253 if (!firmware_has_feature(FW_FEATURE_LPAR))
254 ret = spu_map_resource(spu, 3,
255 (void __iomem**)&spu->priv1, NULL);
257 pr_debug("spu_new: failed to map %s resource 3\n",
261 pr_debug("spu_new: %s maps:\n", np->full_name);
262 pr_debug(" local store : 0x%016lx -> 0x%p\n",
263 spu->local_store_phys, spu->local_store);
264 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
265 spu->problem_phys, spu->problem);
266 pr_debug(" priv2 : 0x%p\n", spu->priv2);
267 pr_debug(" priv1 : 0x%p\n", spu->priv1);
274 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
278 static int __init of_enumerate_spus(int (*fn)(void *data))
281 struct device_node *node;
284 for (node = of_find_node_by_type(NULL, "spe");
285 node; node = of_find_node_by_type(node, "spe")) {
288 printk(KERN_WARNING "%s: Error initializing %s\n",
289 __FUNCTION__, node->name);
296 static int __init of_create_spu(struct spu *spu, void *data)
299 struct device_node *spe = (struct device_node *)data;
300 static int legacy_map = 0, legacy_irq = 0;
302 spu->devnode = of_node_get(spe);
303 spu->spe_id = find_spu_unit_number(spe);
305 spu->node = of_node_to_nid(spe);
306 if (spu->node >= MAX_NUMNODES) {
307 printk(KERN_WARNING "SPE %s on node %d ignored,"
308 " node number too big\n", spe->full_name, spu->node);
309 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
314 ret = spu_map_device(spu);
318 printk(KERN_WARNING "%s: Legacy device tree found, "
319 "trying to map old style\n", __FUNCTION__);
321 ret = spu_map_device_old(spu);
323 printk(KERN_ERR "Unable to map %s\n",
329 ret = spu_map_interrupts(spu, spe);
333 printk(KERN_WARNING "%s: Legacy device tree found, "
334 "trying old style irq\n", __FUNCTION__);
336 ret = spu_map_interrupts_old(spu, spe);
338 printk(KERN_ERR "%s: could not map interrupts",
344 pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
345 spu->local_store, spu->problem, spu->priv1,
346 spu->priv2, spu->number);
355 static int of_destroy_spu(struct spu *spu)
358 of_node_put(spu->devnode);
362 const struct spu_management_ops spu_management_of_ops = {
363 .enumerate_spus = of_enumerate_spus,
364 .create_spu = of_create_spu,
365 .destroy_spu = of_destroy_spu,