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>
21 #include <asm/spu_priv1.h>
22 #include <asm/firmware.h>
25 #include "spufs/spufs.h"
26 #include "interrupt.h"
28 struct device_node *spu_devnode(struct spu *spu)
33 EXPORT_SYMBOL_GPL(spu_devnode);
35 static u64 __init find_spu_unit_number(struct device_node *spe)
37 const unsigned int *prop;
40 /* new device trees should provide the physical-id attribute */
41 prop = of_get_property(spe, "physical-id", &proplen);
45 /* celleb device tree provides the unit-id */
46 prop = of_get_property(spe, "unit-id", &proplen);
50 /* legacy device trees provide the id in the reg attribute */
51 prop = of_get_property(spe, "reg", &proplen);
58 static void spu_unmap(struct spu *spu)
60 if (!firmware_has_feature(FW_FEATURE_LPAR))
63 iounmap(spu->problem);
64 iounmap((__force u8 __iomem *)spu->local_store);
67 static int __init spu_map_interrupts_old(struct spu *spu,
68 struct device_node *np)
74 /* Get the interrupt source unit from the device-tree */
75 tmp = of_get_property(np, "isrc", NULL);
80 tmp = of_get_property(np->parent->parent, "node-id", NULL);
82 printk(KERN_WARNING "%s: can't find node-id\n", __func__);
87 /* Add the node number */
88 isrc |= nid << IIC_IRQ_NODE_SHIFT;
90 /* Now map interrupts of all 3 classes */
91 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
92 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
93 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
95 /* Right now, we only fail if class 2 failed */
102 static void __iomem * __init spu_map_prop_old(struct spu *spu,
103 struct device_node *n,
106 const struct address_prop {
107 unsigned long address;
109 } __attribute__((packed)) *prop;
112 prop = of_get_property(n, name, &proplen);
113 if (prop == NULL || proplen != sizeof (struct address_prop))
116 return ioremap(prop->address, prop->len);
119 static int __init spu_map_device_old(struct spu *spu)
121 struct device_node *node = spu->devnode;
126 spu->name = of_get_property(node, "name", NULL);
130 prop = of_get_property(node, "local-store", NULL);
133 spu->local_store_phys = *(unsigned long *)prop;
135 /* we use local store as ram, not io memory */
136 spu->local_store = (void __force *)
137 spu_map_prop_old(spu, node, "local-store");
138 if (!spu->local_store)
141 prop = of_get_property(node, "problem", NULL);
144 spu->problem_phys = *(unsigned long *)prop;
146 spu->problem = spu_map_prop_old(spu, node, "problem");
150 spu->priv2 = spu_map_prop_old(spu, node, "priv2");
154 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
155 spu->priv1 = spu_map_prop_old(spu, node, "priv1");
169 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
173 for (i=0; i < 3; i++) {
174 spu->irqs[i] = irq_of_parse_and_map(np, i);
181 pr_debug("failed to map irq %x for spu %s\n", i, spu->name);
182 for (; i >= 0; i--) {
184 irq_dispose_mapping(spu->irqs[i]);
189 static int spu_map_resource(struct spu *spu, int nr,
190 void __iomem** virt, unsigned long *phys)
192 struct device_node *np = spu->devnode;
193 struct resource resource = { };
197 ret = of_address_to_resource(np, nr, &resource);
201 *phys = resource.start;
202 len = resource_size(&resource);
203 *virt = ioremap(resource.start, len);
209 static int __init spu_map_device(struct spu *spu)
211 struct device_node *np = spu->devnode;
214 spu->name = of_get_property(np, "name", NULL);
218 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
219 &spu->local_store_phys);
221 pr_debug("spu_new: failed to map %pOF resource 0\n",
225 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
228 pr_debug("spu_new: failed to map %pOF resource 1\n",
232 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
234 pr_debug("spu_new: failed to map %pOF resource 2\n",
238 if (!firmware_has_feature(FW_FEATURE_LPAR))
239 ret = spu_map_resource(spu, 3,
240 (void __iomem**)&spu->priv1, NULL);
242 pr_debug("spu_new: failed to map %pOF resource 3\n",
246 pr_debug("spu_new: %pOF maps:\n", np);
247 pr_debug(" local store : 0x%016lx -> 0x%p\n",
248 spu->local_store_phys, spu->local_store);
249 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
250 spu->problem_phys, spu->problem);
251 pr_debug(" priv2 : 0x%p\n", spu->priv2);
252 pr_debug(" priv1 : 0x%p\n", spu->priv1);
259 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
263 static int __init of_enumerate_spus(int (*fn)(void *data))
266 struct device_node *node;
270 for_each_node_by_type(node, "spe") {
273 printk(KERN_WARNING "%s: Error initializing %pOFn\n",
280 return ret ? ret : n;
283 static int __init of_create_spu(struct spu *spu, void *data)
286 struct device_node *spe = (struct device_node *)data;
287 static int legacy_map = 0, legacy_irq = 0;
289 spu->devnode = of_node_get(spe);
290 spu->spe_id = find_spu_unit_number(spe);
292 spu->node = of_node_to_nid(spe);
293 if (spu->node >= MAX_NUMNODES) {
294 printk(KERN_WARNING "SPE %pOF on node %d ignored,"
295 " node number too big\n", spe, spu->node);
296 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
301 ret = spu_map_device(spu);
305 printk(KERN_WARNING "%s: Legacy device tree found, "
306 "trying to map old style\n", __func__);
308 ret = spu_map_device_old(spu);
310 printk(KERN_ERR "Unable to map %s\n",
316 ret = spu_map_interrupts(spu, spe);
320 printk(KERN_WARNING "%s: Legacy device tree found, "
321 "trying old style irq\n", __func__);
323 ret = spu_map_interrupts_old(spu, spe);
325 printk(KERN_ERR "%s: could not map interrupts\n",
331 pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
332 spu->local_store, spu->problem, spu->priv1,
333 spu->priv2, spu->number);
342 static int of_destroy_spu(struct spu *spu)
345 of_node_put(spu->devnode);
349 static void enable_spu_by_master_run(struct spu_context *ctx)
351 ctx->ops->master_start(ctx);
354 static void disable_spu_by_master_run(struct spu_context *ctx)
356 ctx->ops->master_stop(ctx);
359 /* Hardcoded affinity idxs for qs20 */
360 #define QS20_SPES_PER_BE 8
361 static int qs20_reg_idxs[QS20_SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
362 static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
364 static struct spu *spu_lookup_reg(int node, u32 reg)
369 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
370 spu_reg = of_get_property(spu_devnode(spu), "reg", NULL);
377 static void init_affinity_qs20_harcoded(void)
380 struct spu *last_spu, *spu;
383 for (node = 0; node < MAX_NUMNODES; node++) {
385 for (i = 0; i < QS20_SPES_PER_BE; i++) {
386 reg = qs20_reg_idxs[i];
387 spu = spu_lookup_reg(node, reg);
390 spu->has_mem_affinity = qs20_reg_memory[reg];
392 list_add_tail(&spu->aff_list,
393 &last_spu->aff_list);
399 static int of_has_vicinity(void)
401 struct device_node *dn;
403 for_each_node_by_type(dn, "spe") {
404 if (of_find_property(dn, "vicinity", NULL)) {
412 static struct spu *devnode_spu(int cbe, struct device_node *dn)
416 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
417 if (spu_devnode(spu) == dn)
423 neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid)
426 struct device_node *spu_dn;
427 const phandle *vic_handles;
430 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
431 spu_dn = spu_devnode(spu);
434 vic_handles = of_get_property(spu_dn, "vicinity", &lenp);
435 for (i=0; i < (lenp / sizeof(phandle)); i++) {
436 if (vic_handles[i] == target->phandle)
443 static void init_affinity_node(int cbe)
445 struct spu *spu, *last_spu;
446 struct device_node *vic_dn, *last_spu_dn;
448 const phandle *vic_handles;
451 last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu,
454 for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
455 last_spu_dn = spu_devnode(last_spu);
456 vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp);
459 * Walk through each phandle in vicinity property of the spu
460 * (tipically two vicinity phandles per spe node)
462 for (i = 0; i < (lenp / sizeof(phandle)); i++) {
463 if (vic_handles[i] == avoid_ph)
466 vic_dn = of_find_node_by_phandle(vic_handles[i]);
470 if (of_node_name_eq(vic_dn, "spe") ) {
471 spu = devnode_spu(cbe, vic_dn);
472 avoid_ph = last_spu_dn->phandle;
475 * "mic-tm" and "bif0" nodes do not have
476 * vicinity property. So we need to find the
477 * spe which has vic_dn as neighbour, but
478 * skipping the one we came from (last_spu_dn)
480 spu = neighbour_spu(cbe, vic_dn, last_spu_dn);
483 if (of_node_name_eq(vic_dn, "mic-tm")) {
484 last_spu->has_mem_affinity = 1;
485 spu->has_mem_affinity = 1;
487 avoid_ph = vic_dn->phandle;
490 list_add_tail(&spu->aff_list, &last_spu->aff_list);
497 static void init_affinity_fw(void)
501 for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
502 init_affinity_node(cbe);
505 static int __init init_affinity(void)
507 if (of_has_vicinity()) {
510 if (of_machine_is_compatible("IBM,CPBW-1.0"))
511 init_affinity_qs20_harcoded();
513 printk("No affinity configuration found\n");
519 const struct spu_management_ops spu_management_of_ops = {
520 .enumerate_spus = of_enumerate_spus,
521 .create_spu = of_create_spu,
522 .destroy_spu = of_destroy_spu,
523 .enable_spu = enable_spu_by_master_run,
524 .disable_spu = disable_spu_by_master_run,
525 .init_affinity = init_affinity,