blob: c3327f3d8cf75a8a7a6ea062efac0612b37f6595 [file] [log] [blame]
Ishizaki Kouc9868fe2007-02-02 16:45:33 +09001/*
2 * spu management operations for of based platforms
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 * Copyright 2006 Sony Corp.
6 * (C) Copyright 2007 TOSHIBA CORPORATION
7 *
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.
11 *
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.
16 *
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.
20 */
21
22#include <linux/interrupt.h>
23#include <linux/list.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040024#include <linux/export.h>
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090025#include <linux/ptrace.h>
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090026#include <linux/wait.h>
27#include <linux/mm.h>
28#include <linux/io.h>
29#include <linux/mutex.h>
30#include <linux/device.h>
31
32#include <asm/spu.h>
33#include <asm/spu_priv1.h>
34#include <asm/firmware.h>
35#include <asm/prom.h>
36
Masato Noguchic25620d2007-12-05 13:49:31 +110037#include "spufs/spufs.h"
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090038#include "interrupt.h"
39
40struct device_node *spu_devnode(struct spu *spu)
41{
42 return spu->devnode;
43}
44
45EXPORT_SYMBOL_GPL(spu_devnode);
46
47static u64 __init find_spu_unit_number(struct device_node *spe)
48{
49 const unsigned int *prop;
50 int proplen;
Christian Krafftaac2e682007-08-30 01:33:53 +020051
52 /* new device trees should provide the physical-id attribute */
Christian Krafftfa7f3742007-08-23 03:01:25 +100053 prop = of_get_property(spe, "physical-id", &proplen);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090054 if (proplen == 4)
55 return (u64)*prop;
56
Christian Krafftaac2e682007-08-30 01:33:53 +020057 /* celleb device tree provides the unit-id */
58 prop = of_get_property(spe, "unit-id", &proplen);
59 if (proplen == 4)
60 return (u64)*prop;
61
62 /* legacy device trees provide the id in the reg attribute */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100063 prop = of_get_property(spe, "reg", &proplen);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090064 if (proplen == 4)
65 return (u64)*prop;
66
67 return 0;
68}
69
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090070static void spu_unmap(struct spu *spu)
71{
72 if (!firmware_has_feature(FW_FEATURE_LPAR))
73 iounmap(spu->priv1);
74 iounmap(spu->priv2);
75 iounmap(spu->problem);
76 iounmap((__force u8 __iomem *)spu->local_store);
77}
78
79static int __init spu_map_interrupts_old(struct spu *spu,
80 struct device_node *np)
81{
82 unsigned int isrc;
83 const u32 *tmp;
84 int nid;
85
86 /* Get the interrupt source unit from the device-tree */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100087 tmp = of_get_property(np, "isrc", NULL);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090088 if (!tmp)
89 return -ENODEV;
90 isrc = tmp[0];
91
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100092 tmp = of_get_property(np->parent->parent, "node-id", NULL);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090093 if (!tmp) {
Harvey Harrisone48b1b42008-03-29 08:21:07 +110094 printk(KERN_WARNING "%s: can't find node-id\n", __func__);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +090095 nid = spu->node;
96 } else
97 nid = tmp[0];
98
99 /* Add the node number */
100 isrc |= nid << IIC_IRQ_NODE_SHIFT;
101
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);
106
107 /* Right now, we only fail if class 2 failed */
108 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
109}
110
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100111static void __iomem * __init spu_map_prop_old(struct spu *spu,
112 struct device_node *n,
113 const char *name)
114{
115 const struct address_prop {
116 unsigned long address;
117 unsigned int len;
118 } __attribute__((packed)) *prop;
119 int proplen;
120
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000121 prop = of_get_property(n, name, &proplen);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100122 if (prop == NULL || proplen != sizeof (struct address_prop))
123 return NULL;
124
125 return ioremap(prop->address, prop->len);
126}
127
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900128static int __init spu_map_device_old(struct spu *spu)
129{
130 struct device_node *node = spu->devnode;
131 const char *prop;
132 int ret;
133
134 ret = -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000135 spu->name = of_get_property(node, "name", NULL);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900136 if (!spu->name)
137 goto out;
138
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000139 prop = of_get_property(node, "local-store", NULL);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900140 if (!prop)
141 goto out;
142 spu->local_store_phys = *(unsigned long *)prop;
143
144 /* we use local store as ram, not io memory */
145 spu->local_store = (void __force *)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100146 spu_map_prop_old(spu, node, "local-store");
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900147 if (!spu->local_store)
148 goto out;
149
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000150 prop = of_get_property(node, "problem", NULL);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900151 if (!prop)
152 goto out_unmap;
153 spu->problem_phys = *(unsigned long *)prop;
154
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100155 spu->problem = spu_map_prop_old(spu, node, "problem");
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900156 if (!spu->problem)
157 goto out_unmap;
158
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100159 spu->priv2 = spu_map_prop_old(spu, node, "priv2");
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900160 if (!spu->priv2)
161 goto out_unmap;
162
163 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100164 spu->priv1 = spu_map_prop_old(spu, node, "priv1");
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900165 if (!spu->priv1)
166 goto out_unmap;
167 }
168
169 ret = 0;
170 goto out;
171
172out_unmap:
173 spu_unmap(spu);
174out:
175 return ret;
176}
177
178static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
179{
Grant Likely530210c2013-09-15 16:39:11 +0100180 struct of_phandle_args oirq;
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900181 int ret;
182 int i;
183
184 for (i=0; i < 3; i++) {
Grant Likely0c02c802013-09-19 11:22:36 -0500185 ret = of_irq_parse_one(np, i, &oirq);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900186 if (ret) {
187 pr_debug("spu_new: failed to get irq %d\n", i);
188 goto err;
189 }
190 ret = -EINVAL;
Grant Likely530210c2013-09-15 16:39:11 +0100191 pr_debug(" irq %d no 0x%x on %s\n", i, oirq.args[0],
192 oirq.np->full_name);
Grant Likelye6d30ab2013-09-15 16:55:53 +0100193 spu->irqs[i] = irq_create_of_mapping(&oirq);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900194 if (spu->irqs[i] == NO_IRQ) {
195 pr_debug("spu_new: failed to map it !\n");
196 goto err;
197 }
198 }
199 return 0;
200
201err:
Grant Likely530210c2013-09-15 16:39:11 +0100202 pr_debug("failed to map irq %x for spu %s\n", *oirq.args,
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900203 spu->name);
204 for (; i >= 0; i--) {
205 if (spu->irqs[i] != NO_IRQ)
206 irq_dispose_mapping(spu->irqs[i]);
207 }
208 return ret;
209}
210
211static int spu_map_resource(struct spu *spu, int nr,
212 void __iomem** virt, unsigned long *phys)
213{
214 struct device_node *np = spu->devnode;
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900215 struct resource resource = { };
216 unsigned long len;
217 int ret;
218
219 ret = of_address_to_resource(np, nr, &resource);
220 if (ret)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100221 return ret;
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900222 if (phys)
223 *phys = resource.start;
Joe Perches28f65c112011-06-09 09:13:32 -0700224 len = resource_size(&resource);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900225 *virt = ioremap(resource.start, len);
226 if (!*virt)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100227 return -EINVAL;
228 return 0;
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900229}
230
231static int __init spu_map_device(struct spu *spu)
232{
233 struct device_node *np = spu->devnode;
234 int ret = -ENODEV;
235
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000236 spu->name = of_get_property(np, "name", NULL);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900237 if (!spu->name)
238 goto out;
239
240 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
241 &spu->local_store_phys);
242 if (ret) {
243 pr_debug("spu_new: failed to map %s resource 0\n",
244 np->full_name);
245 goto out;
246 }
247 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
248 &spu->problem_phys);
249 if (ret) {
250 pr_debug("spu_new: failed to map %s resource 1\n",
251 np->full_name);
252 goto out_unmap;
253 }
254 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
255 if (ret) {
256 pr_debug("spu_new: failed to map %s resource 2\n",
257 np->full_name);
258 goto out_unmap;
259 }
260 if (!firmware_has_feature(FW_FEATURE_LPAR))
261 ret = spu_map_resource(spu, 3,
262 (void __iomem**)&spu->priv1, NULL);
263 if (ret) {
264 pr_debug("spu_new: failed to map %s resource 3\n",
265 np->full_name);
266 goto out_unmap;
267 }
268 pr_debug("spu_new: %s maps:\n", np->full_name);
269 pr_debug(" local store : 0x%016lx -> 0x%p\n",
270 spu->local_store_phys, spu->local_store);
271 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
272 spu->problem_phys, spu->problem);
273 pr_debug(" priv2 : 0x%p\n", spu->priv2);
274 pr_debug(" priv1 : 0x%p\n", spu->priv1);
275
276 return 0;
277
278out_unmap:
279 spu_unmap(spu);
280out:
281 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
282 return ret;
283}
284
285static int __init of_enumerate_spus(int (*fn)(void *data))
286{
287 int ret;
288 struct device_node *node;
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700289 unsigned int n = 0;
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900290
291 ret = -ENODEV;
292 for (node = of_find_node_by_type(NULL, "spe");
293 node; node = of_find_node_by_type(node, "spe")) {
294 ret = fn(node);
295 if (ret) {
296 printk(KERN_WARNING "%s: Error initializing %s\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100297 __func__, node->name);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900298 break;
299 }
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700300 n++;
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900301 }
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700302 return ret ? ret : n;
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900303}
304
305static int __init of_create_spu(struct spu *spu, void *data)
306{
307 int ret;
308 struct device_node *spe = (struct device_node *)data;
309 static int legacy_map = 0, legacy_irq = 0;
310
311 spu->devnode = of_node_get(spe);
312 spu->spe_id = find_spu_unit_number(spe);
313
314 spu->node = of_node_to_nid(spe);
315 if (spu->node >= MAX_NUMNODES) {
316 printk(KERN_WARNING "SPE %s on node %d ignored,"
317 " node number too big\n", spe->full_name, spu->node);
318 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
319 ret = -ENODEV;
320 goto out;
321 }
322
323 ret = spu_map_device(spu);
324 if (ret) {
325 if (!legacy_map) {
326 legacy_map = 1;
327 printk(KERN_WARNING "%s: Legacy device tree found, "
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100328 "trying to map old style\n", __func__);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900329 }
330 ret = spu_map_device_old(spu);
331 if (ret) {
332 printk(KERN_ERR "Unable to map %s\n",
333 spu->name);
334 goto out;
335 }
336 }
337
338 ret = spu_map_interrupts(spu, spe);
339 if (ret) {
340 if (!legacy_irq) {
341 legacy_irq = 1;
342 printk(KERN_WARNING "%s: Legacy device tree found, "
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100343 "trying old style irq\n", __func__);
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900344 }
345 ret = spu_map_interrupts_old(spu, spe);
346 if (ret) {
Ishizaki Kou23666eb2007-11-01 19:04:04 +0900347 printk(KERN_ERR "%s: could not map interrupts\n",
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900348 spu->name);
349 goto out_unmap;
350 }
351 }
352
353 pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
354 spu->local_store, spu->problem, spu->priv1,
355 spu->priv2, spu->number);
356 goto out;
357
358out_unmap:
359 spu_unmap(spu);
360out:
361 return ret;
362}
363
364static int of_destroy_spu(struct spu *spu)
365{
366 spu_unmap(spu);
367 of_node_put(spu->devnode);
368 return 0;
369}
370
Masato Noguchic25620d2007-12-05 13:49:31 +1100371static void enable_spu_by_master_run(struct spu_context *ctx)
372{
373 ctx->ops->master_start(ctx);
374}
375
376static void disable_spu_by_master_run(struct spu_context *ctx)
377{
378 ctx->ops->master_stop(ctx);
379}
380
Andre Detschf5996442007-08-03 18:53:46 -0700381/* Hardcoded affinity idxs for qs20 */
382#define QS20_SPES_PER_BE 8
383static int qs20_reg_idxs[QS20_SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
384static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
385
386static struct spu *spu_lookup_reg(int node, u32 reg)
387{
388 struct spu *spu;
Jeremy Kerrfb8299e2007-09-14 15:46:40 +1000389 const u32 *spu_reg;
Andre Detschf5996442007-08-03 18:53:46 -0700390
391 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
Jeremy Kerrfb8299e2007-09-14 15:46:40 +1000392 spu_reg = of_get_property(spu_devnode(spu), "reg", NULL);
Andre Detschf5996442007-08-03 18:53:46 -0700393 if (*spu_reg == reg)
394 return spu;
395 }
396 return NULL;
397}
398
399static void init_affinity_qs20_harcoded(void)
400{
401 int node, i;
402 struct spu *last_spu, *spu;
403 u32 reg;
404
405 for (node = 0; node < MAX_NUMNODES; node++) {
406 last_spu = NULL;
407 for (i = 0; i < QS20_SPES_PER_BE; i++) {
408 reg = qs20_reg_idxs[i];
409 spu = spu_lookup_reg(node, reg);
410 if (!spu)
411 continue;
412 spu->has_mem_affinity = qs20_reg_memory[reg];
413 if (last_spu)
414 list_add_tail(&spu->aff_list,
415 &last_spu->aff_list);
416 last_spu = spu;
417 }
418 }
419}
420
421static int of_has_vicinity(void)
422{
Andre Detscha0a7ae82007-12-05 13:49:31 +1100423 struct device_node *dn;
Andre Detschf5996442007-08-03 18:53:46 -0700424
Andre Detscha0a7ae82007-12-05 13:49:31 +1100425 for_each_node_by_type(dn, "spe") {
426 if (of_find_property(dn, "vicinity", NULL)) {
427 of_node_put(dn);
428 return 1;
429 }
430 }
431 return 0;
Andre Detschf5996442007-08-03 18:53:46 -0700432}
433
434static struct spu *devnode_spu(int cbe, struct device_node *dn)
435{
436 struct spu *spu;
437
438 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
439 if (spu_devnode(spu) == dn)
440 return spu;
441 return NULL;
442}
443
444static struct spu *
445neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid)
446{
447 struct spu *spu;
448 struct device_node *spu_dn;
449 const phandle *vic_handles;
450 int lenp, i;
451
452 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
453 spu_dn = spu_devnode(spu);
454 if (spu_dn == avoid)
455 continue;
456 vic_handles = of_get_property(spu_dn, "vicinity", &lenp);
457 for (i=0; i < (lenp / sizeof(phandle)); i++) {
Grant Likely6016a362010-01-28 14:06:53 -0700458 if (vic_handles[i] == target->phandle)
Andre Detschf5996442007-08-03 18:53:46 -0700459 return spu;
460 }
461 }
462 return NULL;
463}
464
465static void init_affinity_node(int cbe)
466{
467 struct spu *spu, *last_spu;
468 struct device_node *vic_dn, *last_spu_dn;
469 phandle avoid_ph;
470 const phandle *vic_handles;
471 const char *name;
472 int lenp, i, added;
473
474 last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu,
475 cbe_list);
476 avoid_ph = 0;
477 for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
478 last_spu_dn = spu_devnode(last_spu);
479 vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp);
480
481 /*
482 * Walk through each phandle in vicinity property of the spu
483 * (tipically two vicinity phandles per spe node)
484 */
485 for (i = 0; i < (lenp / sizeof(phandle)); i++) {
486 if (vic_handles[i] == avoid_ph)
487 continue;
488
489 vic_dn = of_find_node_by_phandle(vic_handles[i]);
490 if (!vic_dn)
491 continue;
492
493 /* a neighbour might be spe, mic-tm, or bif0 */
494 name = of_get_property(vic_dn, "name", NULL);
495 if (!name)
496 continue;
497
498 if (strcmp(name, "spe") == 0) {
499 spu = devnode_spu(cbe, vic_dn);
Grant Likely6016a362010-01-28 14:06:53 -0700500 avoid_ph = last_spu_dn->phandle;
Andre Detschf5996442007-08-03 18:53:46 -0700501 } else {
502 /*
503 * "mic-tm" and "bif0" nodes do not have
504 * vicinity property. So we need to find the
505 * spe which has vic_dn as neighbour, but
506 * skipping the one we came from (last_spu_dn)
507 */
508 spu = neighbour_spu(cbe, vic_dn, last_spu_dn);
509 if (!spu)
510 continue;
511 if (!strcmp(name, "mic-tm")) {
512 last_spu->has_mem_affinity = 1;
513 spu->has_mem_affinity = 1;
514 }
Grant Likely6016a362010-01-28 14:06:53 -0700515 avoid_ph = vic_dn->phandle;
Andre Detschf5996442007-08-03 18:53:46 -0700516 }
517
518 list_add_tail(&spu->aff_list, &last_spu->aff_list);
519 last_spu = spu;
520 break;
521 }
522 }
523}
524
525static void init_affinity_fw(void)
526{
527 int cbe;
528
529 for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
530 init_affinity_node(cbe);
531}
532
533static int __init init_affinity(void)
534{
535 if (of_has_vicinity()) {
536 init_affinity_fw();
537 } else {
538 long root = of_get_flat_dt_root();
539 if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
540 init_affinity_qs20_harcoded();
541 else
Ishizaki Kou23666eb2007-11-01 19:04:04 +0900542 printk("No affinity configuration found\n");
Andre Detschf5996442007-08-03 18:53:46 -0700543 }
544
545 return 0;
546}
547
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900548const struct spu_management_ops spu_management_of_ops = {
549 .enumerate_spus = of_enumerate_spus,
550 .create_spu = of_create_spu,
551 .destroy_spu = of_destroy_spu,
Masato Noguchic25620d2007-12-05 13:49:31 +1100552 .enable_spu = enable_spu_by_master_run,
553 .disable_spu = disable_spu_by_master_run,
Andre Detschf5996442007-08-03 18:53:46 -0700554 .init_affinity = init_affinity,
Ishizaki Kouc9868fe2007-02-02 16:45:33 +0900555};