1 // SPDX-License-Identifier: GPL-2.0-only
3 * spu management operations for of based platforms
5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Copyright 2006 Sony Corp.
7 * (C) Copyright 2007 TOSHIBA CORPORATION
10 #include <linux/interrupt.h>
11 #include <linux/list.h>
12 #include <linux/export.h>
13 #include <linux/ptrace.h>
14 #include <linux/wait.h>
17 #include <linux/mutex.h>
18 #include <linux/device.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
23 #include <asm/spu_priv1.h>
24 #include <asm/firmware.h>
26 #include "spufs/spufs.h"
27 #include "interrupt.h"
28 #include "spu_priv1_mmio.h"
30 struct device_node *spu_devnode(struct spu *spu)
35 EXPORT_SYMBOL_GPL(spu_devnode);
37 static u64 __init find_spu_unit_number(struct device_node *spe)
39 const unsigned int *prop;
42 /* new device trees should provide the physical-id attribute */
43 prop = of_get_property(spe, "physical-id", &proplen);
47 /* celleb device tree provides the unit-id */
48 prop = of_get_property(spe, "unit-id", &proplen);
52 /* legacy device trees provide the id in the reg attribute */
53 prop = of_get_property(spe, "reg", &proplen);
60 static void spu_unmap(struct spu *spu)
62 if (!firmware_has_feature(FW_FEATURE_LPAR))
65 iounmap(spu->problem);
66 iounmap((__force u8 __iomem *)spu->local_store);
69 static int __init spu_map_interrupts_old(struct spu *spu,
70 struct device_node *np)
76 /* Get the interrupt source unit from the device-tree */
77 tmp = of_get_property(np, "isrc", NULL);
82 tmp = of_get_property(np->parent->parent, "node-id", NULL);
84 printk(KERN_WARNING "%s: can't find node-id\n", __func__);
89 /* Add the node number */
90 isrc |= nid << IIC_IRQ_NODE_SHIFT;
92 /* Now map interrupts of all 3 classes */
93 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
94 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
95 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
97 /* Right now, we only fail if class 2 failed */
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)
175 for (i=0; i < 3; i++) {
176 spu->irqs[i] = irq_of_parse_and_map(np, i);
183 pr_debug("failed to map irq %x for spu %s\n", i, spu->name);
184 for (; i >= 0; i--) {
186 irq_dispose_mapping(spu->irqs[i]);
191 static int __init spu_map_resource(struct spu *spu, int nr,
192 void __iomem** virt, unsigned long *phys)
194 struct device_node *np = spu->devnode;
195 struct resource resource = { };
199 ret = of_address_to_resource(np, nr, &resource);
203 *phys = resource.start;
204 len = resource_size(&resource);
205 *virt = ioremap(resource.start, len);
211 static int __init spu_map_device(struct spu *spu)
213 struct device_node *np = spu->devnode;
216 spu->name = of_get_property(np, "name", NULL);
220 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
221 &spu->local_store_phys);
223 pr_debug("spu_new: failed to map %pOF resource 0\n",
227 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
230 pr_debug("spu_new: failed to map %pOF resource 1\n",
234 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
236 pr_debug("spu_new: failed to map %pOF resource 2\n",
240 if (!firmware_has_feature(FW_FEATURE_LPAR))
241 ret = spu_map_resource(spu, 3,
242 (void __iomem**)&spu->priv1, NULL);
244 pr_debug("spu_new: failed to map %pOF resource 3\n",
248 pr_debug("spu_new: %pOF maps:\n", np);
249 pr_debug(" local store : 0x%016lx -> 0x%p\n",
250 spu->local_store_phys, spu->local_store);
251 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
252 spu->problem_phys, spu->problem);
253 pr_debug(" priv2 : 0x%p\n", spu->priv2);
254 pr_debug(" priv1 : 0x%p\n", spu->priv1);
261 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
265 static int __init of_enumerate_spus(int (*fn)(void *data))
268 struct device_node *node;
272 for_each_node_by_type(node, "spe") {
275 printk(KERN_WARNING "%s: Error initializing %pOFn\n",
282 return ret ? ret : n;
285 static int __init of_create_spu(struct spu *spu, void *data)
288 struct device_node *spe = (struct device_node *)data;
289 static int legacy_map = 0, legacy_irq = 0;
291 spu->devnode = of_node_get(spe);
292 spu->spe_id = find_spu_unit_number(spe);
294 spu->node = of_node_to_nid(spe);
295 if (spu->node >= MAX_NUMNODES) {
296 printk(KERN_WARNING "SPE %pOF on node %d ignored,"
297 " node number too big\n", spe, spu->node);
298 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
303 ret = spu_map_device(spu);
307 printk(KERN_WARNING "%s: Legacy device tree found, "
308 "trying to map old style\n", __func__);
310 ret = spu_map_device_old(spu);
312 printk(KERN_ERR "Unable to map %s\n",
318 ret = spu_map_interrupts(spu, spe);
322 printk(KERN_WARNING "%s: Legacy device tree found, "
323 "trying old style irq\n", __func__);
325 ret = spu_map_interrupts_old(spu, spe);
327 printk(KERN_ERR "%s: could not map interrupts\n",
333 pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
334 spu->local_store, spu->problem, spu->priv1,
335 spu->priv2, spu->number);
344 static int of_destroy_spu(struct spu *spu)
347 of_node_put(spu->devnode);
351 static void enable_spu_by_master_run(struct spu_context *ctx)
353 ctx->ops->master_start(ctx);
356 static void disable_spu_by_master_run(struct spu_context *ctx)
358 ctx->ops->master_stop(ctx);
361 /* Hardcoded affinity idxs for qs20 */
362 #define QS20_SPES_PER_BE 8
363 static int qs20_reg_idxs[QS20_SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
364 static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
366 static struct spu *__init spu_lookup_reg(int node, u32 reg)
371 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
372 spu_reg = of_get_property(spu_devnode(spu), "reg", NULL);
379 static void __init init_affinity_qs20_harcoded(void)
382 struct spu *last_spu, *spu;
385 for (node = 0; node < MAX_NUMNODES; node++) {
387 for (i = 0; i < QS20_SPES_PER_BE; i++) {
388 reg = qs20_reg_idxs[i];
389 spu = spu_lookup_reg(node, reg);
392 spu->has_mem_affinity = qs20_reg_memory[reg];
394 list_add_tail(&spu->aff_list,
395 &last_spu->aff_list);
401 static int __init of_has_vicinity(void)
403 struct device_node *dn;
405 for_each_node_by_type(dn, "spe") {
406 if (of_property_present(dn, "vicinity")) {
414 static struct spu *__init devnode_spu(int cbe, struct device_node *dn)
418 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
419 if (spu_devnode(spu) == dn)
424 static struct spu * __init
425 neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid)
428 struct device_node *spu_dn;
429 const phandle *vic_handles;
432 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
433 spu_dn = spu_devnode(spu);
436 vic_handles = of_get_property(spu_dn, "vicinity", &lenp);
437 for (i=0; i < (lenp / sizeof(phandle)); i++) {
438 if (vic_handles[i] == target->phandle)
445 static void __init init_affinity_node(int cbe)
447 struct spu *spu, *last_spu;
448 struct device_node *vic_dn, *last_spu_dn;
450 const phandle *vic_handles;
453 last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu,
456 for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
457 last_spu_dn = spu_devnode(last_spu);
458 vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp);
461 * Walk through each phandle in vicinity property of the spu
462 * (typically two vicinity phandles per spe node)
464 for (i = 0; i < (lenp / sizeof(phandle)); i++) {
465 if (vic_handles[i] == avoid_ph)
468 vic_dn = of_find_node_by_phandle(vic_handles[i]);
472 if (of_node_name_eq(vic_dn, "spe") ) {
473 spu = devnode_spu(cbe, vic_dn);
474 avoid_ph = last_spu_dn->phandle;
477 * "mic-tm" and "bif0" nodes do not have
478 * vicinity property. So we need to find the
479 * spe which has vic_dn as neighbour, but
480 * skipping the one we came from (last_spu_dn)
482 spu = neighbour_spu(cbe, vic_dn, last_spu_dn);
485 if (of_node_name_eq(vic_dn, "mic-tm")) {
486 last_spu->has_mem_affinity = 1;
487 spu->has_mem_affinity = 1;
489 avoid_ph = vic_dn->phandle;
494 list_add_tail(&spu->aff_list, &last_spu->aff_list);
501 static void __init init_affinity_fw(void)
505 for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
506 init_affinity_node(cbe);
509 static int __init init_affinity(void)
511 if (of_has_vicinity()) {
514 if (of_machine_is_compatible("IBM,CPBW-1.0"))
515 init_affinity_qs20_harcoded();
517 printk("No affinity configuration found\n");
523 const struct spu_management_ops spu_management_of_ops = {
524 .enumerate_spus = of_enumerate_spus,
525 .create_spu = of_create_spu,
526 .destroy_spu = of_destroy_spu,
527 .enable_spu = enable_spu_by_master_run,
528 .disable_spu = disable_spu_by_master_run,
529 .init_affinity = init_affinity,