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;
52 /* new device trees should provide the physical-id attribute */
53 prop = of_get_property(spe, "physical-id", &proplen);
57 /* celleb device tree provides the unit-id */
58 prop = of_get_property(spe, "unit-id", &proplen);
62 /* legacy device trees provide the id in the reg attribute */
63 prop = of_get_property(spe, "reg", &proplen);
70 static void spu_unmap(struct spu *spu)
72 if (!firmware_has_feature(FW_FEATURE_LPAR))
75 iounmap(spu->problem);
76 iounmap((__force u8 __iomem *)spu->local_store);
79 static int __init spu_map_interrupts_old(struct spu *spu,
80 struct device_node *np)
86 /* Get the interrupt source unit from the device-tree */
87 tmp = of_get_property(np, "isrc", NULL);
92 tmp = of_get_property(np->parent->parent, "node-id", NULL);
94 printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
99 /* Add the node number */
100 isrc |= nid << IIC_IRQ_NODE_SHIFT;
102 /* Now map interrupts of all 3 classes */
103 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
104 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
105 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
107 /* Right now, we only fail if class 2 failed */
108 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
111 static void __iomem * __init spu_map_prop_old(struct spu *spu,
112 struct device_node *n,
115 const struct address_prop {
116 unsigned long address;
118 } __attribute__((packed)) *prop;
121 prop = of_get_property(n, name, &proplen);
122 if (prop == NULL || proplen != sizeof (struct address_prop))
125 return ioremap(prop->address, prop->len);
128 static int __init spu_map_device_old(struct spu *spu)
130 struct device_node *node = spu->devnode;
135 spu->name = of_get_property(node, "name", NULL);
139 prop = of_get_property(node, "local-store", NULL);
142 spu->local_store_phys = *(unsigned long *)prop;
144 /* we use local store as ram, not io memory */
145 spu->local_store = (void __force *)
146 spu_map_prop_old(spu, node, "local-store");
147 if (!spu->local_store)
150 prop = of_get_property(node, "problem", NULL);
153 spu->problem_phys = *(unsigned long *)prop;
155 spu->problem = spu_map_prop_old(spu, node, "problem");
159 spu->priv2 = spu_map_prop_old(spu, node, "priv2");
163 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
164 spu->priv1 = spu_map_prop_old(spu, node, "priv1");
178 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
184 for (i=0; i < 3; i++) {
185 ret = of_irq_map_one(np, i, &oirq);
187 pr_debug("spu_new: failed to get irq %d\n", i);
191 pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
192 oirq.controller->full_name);
193 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
194 oirq.specifier, oirq.size);
195 if (spu->irqs[i] == NO_IRQ) {
196 pr_debug("spu_new: failed to map it !\n");
203 pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
205 for (; i >= 0; i--) {
206 if (spu->irqs[i] != NO_IRQ)
207 irq_dispose_mapping(spu->irqs[i]);
212 static int spu_map_resource(struct spu *spu, int nr,
213 void __iomem** virt, unsigned long *phys)
215 struct device_node *np = spu->devnode;
216 struct resource resource = { };
220 ret = of_address_to_resource(np, nr, &resource);
224 *phys = resource.start;
225 len = resource.end - resource.start + 1;
226 *virt = ioremap(resource.start, len);
232 static int __init spu_map_device(struct spu *spu)
234 struct device_node *np = spu->devnode;
237 spu->name = of_get_property(np, "name", NULL);
241 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
242 &spu->local_store_phys);
244 pr_debug("spu_new: failed to map %s resource 0\n",
248 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
251 pr_debug("spu_new: failed to map %s resource 1\n",
255 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
257 pr_debug("spu_new: failed to map %s resource 2\n",
261 if (!firmware_has_feature(FW_FEATURE_LPAR))
262 ret = spu_map_resource(spu, 3,
263 (void __iomem**)&spu->priv1, NULL);
265 pr_debug("spu_new: failed to map %s resource 3\n",
269 pr_debug("spu_new: %s maps:\n", np->full_name);
270 pr_debug(" local store : 0x%016lx -> 0x%p\n",
271 spu->local_store_phys, spu->local_store);
272 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
273 spu->problem_phys, spu->problem);
274 pr_debug(" priv2 : 0x%p\n", spu->priv2);
275 pr_debug(" priv1 : 0x%p\n", spu->priv1);
282 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
286 static int __init of_enumerate_spus(int (*fn)(void *data))
289 struct device_node *node;
293 for (node = of_find_node_by_type(NULL, "spe");
294 node; node = of_find_node_by_type(node, "spe")) {
297 printk(KERN_WARNING "%s: Error initializing %s\n",
298 __FUNCTION__, node->name);
303 return ret ? ret : n;
306 static int __init of_create_spu(struct spu *spu, void *data)
309 struct device_node *spe = (struct device_node *)data;
310 static int legacy_map = 0, legacy_irq = 0;
312 spu->devnode = of_node_get(spe);
313 spu->spe_id = find_spu_unit_number(spe);
315 spu->node = of_node_to_nid(spe);
316 if (spu->node >= MAX_NUMNODES) {
317 printk(KERN_WARNING "SPE %s on node %d ignored,"
318 " node number too big\n", spe->full_name, spu->node);
319 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
324 ret = spu_map_device(spu);
328 printk(KERN_WARNING "%s: Legacy device tree found, "
329 "trying to map old style\n", __FUNCTION__);
331 ret = spu_map_device_old(spu);
333 printk(KERN_ERR "Unable to map %s\n",
339 ret = spu_map_interrupts(spu, spe);
343 printk(KERN_WARNING "%s: Legacy device tree found, "
344 "trying old style irq\n", __FUNCTION__);
346 ret = spu_map_interrupts_old(spu, spe);
348 printk(KERN_ERR "%s: could not map interrupts",
354 pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
355 spu->local_store, spu->problem, spu->priv1,
356 spu->priv2, spu->number);
365 static int of_destroy_spu(struct spu *spu)
368 of_node_put(spu->devnode);
372 /* Hardcoded affinity idxs for qs20 */
373 #define QS20_SPES_PER_BE 8
374 static int qs20_reg_idxs[QS20_SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
375 static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
377 static struct spu *spu_lookup_reg(int node, u32 reg)
382 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
383 spu_reg = (u32*)of_get_property(spu_devnode(spu), "reg", NULL);
390 static void init_affinity_qs20_harcoded(void)
393 struct spu *last_spu, *spu;
396 for (node = 0; node < MAX_NUMNODES; node++) {
398 for (i = 0; i < QS20_SPES_PER_BE; i++) {
399 reg = qs20_reg_idxs[i];
400 spu = spu_lookup_reg(node, reg);
403 spu->has_mem_affinity = qs20_reg_memory[reg];
405 list_add_tail(&spu->aff_list,
406 &last_spu->aff_list);
412 static int of_has_vicinity(void)
416 spu = list_first_entry(&cbe_spu_info[0].spus, struct spu, cbe_list);
417 return of_find_property(spu_devnode(spu), "vicinity", NULL) != NULL;
420 static struct spu *devnode_spu(int cbe, struct device_node *dn)
424 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
425 if (spu_devnode(spu) == dn)
431 neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid)
434 struct device_node *spu_dn;
435 const phandle *vic_handles;
438 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
439 spu_dn = spu_devnode(spu);
442 vic_handles = of_get_property(spu_dn, "vicinity", &lenp);
443 for (i=0; i < (lenp / sizeof(phandle)); i++) {
444 if (vic_handles[i] == target->linux_phandle)
451 static void init_affinity_node(int cbe)
453 struct spu *spu, *last_spu;
454 struct device_node *vic_dn, *last_spu_dn;
456 const phandle *vic_handles;
460 last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu,
463 for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
464 last_spu_dn = spu_devnode(last_spu);
465 vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp);
468 * Walk through each phandle in vicinity property of the spu
469 * (tipically two vicinity phandles per spe node)
471 for (i = 0; i < (lenp / sizeof(phandle)); i++) {
472 if (vic_handles[i] == avoid_ph)
475 vic_dn = of_find_node_by_phandle(vic_handles[i]);
479 /* a neighbour might be spe, mic-tm, or bif0 */
480 name = of_get_property(vic_dn, "name", NULL);
484 if (strcmp(name, "spe") == 0) {
485 spu = devnode_spu(cbe, vic_dn);
486 avoid_ph = last_spu_dn->linux_phandle;
489 * "mic-tm" and "bif0" nodes do not have
490 * vicinity property. So we need to find the
491 * spe which has vic_dn as neighbour, but
492 * skipping the one we came from (last_spu_dn)
494 spu = neighbour_spu(cbe, vic_dn, last_spu_dn);
497 if (!strcmp(name, "mic-tm")) {
498 last_spu->has_mem_affinity = 1;
499 spu->has_mem_affinity = 1;
501 avoid_ph = vic_dn->linux_phandle;
504 list_add_tail(&spu->aff_list, &last_spu->aff_list);
511 static void init_affinity_fw(void)
515 for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
516 init_affinity_node(cbe);
519 static int __init init_affinity(void)
521 if (of_has_vicinity()) {
524 long root = of_get_flat_dt_root();
525 if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
526 init_affinity_qs20_harcoded();
528 printk("No affinity configuration found");
534 const struct spu_management_ops spu_management_of_ops = {
535 .enumerate_spus = of_enumerate_spus,
536 .create_spu = of_create_spu,
537 .destroy_spu = of_destroy_spu,
538 .init_affinity = init_affinity,