1 /*
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>
24 #include <linux/module.h>
25 #include <linux/ptrace.h>
26 #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 
37 #include "spufs/spufs.h"
38 #include "interrupt.h"
39 
40 struct device_node *spu_devnode(struct spu *spu)
41 {
42 	return spu->devnode;
43 }
44 
45 EXPORT_SYMBOL_GPL(spu_devnode);
46 
47 static u64 __init find_spu_unit_number(struct device_node *spe)
48 {
49 	const unsigned int *prop;
50 	int proplen;
51 
52 	/* new device trees should provide the physical-id attribute */
53 	prop = of_get_property(spe, "physical-id", &proplen);
54 	if (proplen == 4)
55 		return (u64)*prop;
56 
57 	/* 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 */
63 	prop = of_get_property(spe, "reg", &proplen);
64 	if (proplen == 4)
65 		return (u64)*prop;
66 
67 	return 0;
68 }
69 
70 static 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 
79 static 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 */
87 	tmp = of_get_property(np, "isrc", NULL);
88 	if (!tmp)
89 		return -ENODEV;
90 	isrc = tmp[0];
91 
92 	tmp = of_get_property(np->parent->parent, "node-id", NULL);
93 	if (!tmp) {
94 		printk(KERN_WARNING "%s: can't find node-id\n", __func__);
95 		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 
111 static 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 
121 	prop = of_get_property(n, name, &proplen);
122 	if (prop == NULL || proplen != sizeof (struct address_prop))
123 		return NULL;
124 
125 	return ioremap(prop->address, prop->len);
126 }
127 
128 static 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;
135 	spu->name = of_get_property(node, "name", NULL);
136 	if (!spu->name)
137 		goto out;
138 
139 	prop = of_get_property(node, "local-store", NULL);
140 	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 *)
146 		spu_map_prop_old(spu, node, "local-store");
147 	if (!spu->local_store)
148 		goto out;
149 
150 	prop = of_get_property(node, "problem", NULL);
151 	if (!prop)
152 		goto out_unmap;
153 	spu->problem_phys = *(unsigned long *)prop;
154 
155 	spu->problem = spu_map_prop_old(spu, node, "problem");
156 	if (!spu->problem)
157 		goto out_unmap;
158 
159 	spu->priv2 = spu_map_prop_old(spu, node, "priv2");
160 	if (!spu->priv2)
161 		goto out_unmap;
162 
163 	if (!firmware_has_feature(FW_FEATURE_LPAR)) {
164 		spu->priv1 = spu_map_prop_old(spu, node, "priv1");
165 		if (!spu->priv1)
166 			goto out_unmap;
167 	}
168 
169 	ret = 0;
170 	goto out;
171 
172 out_unmap:
173 	spu_unmap(spu);
174 out:
175 	return ret;
176 }
177 
178 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
179 {
180 	struct of_irq oirq;
181 	int ret;
182 	int i;
183 
184 	for (i=0; i < 3; i++) {
185 		ret = of_irq_map_one(np, i, &oirq);
186 		if (ret) {
187 			pr_debug("spu_new: failed to get irq %d\n", i);
188 			goto err;
189 		}
190 		ret = -EINVAL;
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");
197 			goto err;
198 		}
199 	}
200 	return 0;
201 
202 err:
203 	pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
204 		spu->name);
205 	for (; i >= 0; i--) {
206 		if (spu->irqs[i] != NO_IRQ)
207 			irq_dispose_mapping(spu->irqs[i]);
208 	}
209 	return ret;
210 }
211 
212 static int spu_map_resource(struct spu *spu, int nr,
213 			    void __iomem** virt, unsigned long *phys)
214 {
215 	struct device_node *np = spu->devnode;
216 	struct resource resource = { };
217 	unsigned long len;
218 	int ret;
219 
220 	ret = of_address_to_resource(np, nr, &resource);
221 	if (ret)
222 		return ret;
223 	if (phys)
224 		*phys = resource.start;
225 	len = resource.end - resource.start + 1;
226 	*virt = ioremap(resource.start, len);
227 	if (!*virt)
228 		return -EINVAL;
229 	return 0;
230 }
231 
232 static int __init spu_map_device(struct spu *spu)
233 {
234 	struct device_node *np = spu->devnode;
235 	int ret = -ENODEV;
236 
237 	spu->name = of_get_property(np, "name", NULL);
238 	if (!spu->name)
239 		goto out;
240 
241 	ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
242 			       &spu->local_store_phys);
243 	if (ret) {
244 		pr_debug("spu_new: failed to map %s resource 0\n",
245 			 np->full_name);
246 		goto out;
247 	}
248 	ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
249 			       &spu->problem_phys);
250 	if (ret) {
251 		pr_debug("spu_new: failed to map %s resource 1\n",
252 			 np->full_name);
253 		goto out_unmap;
254 	}
255 	ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
256 	if (ret) {
257 		pr_debug("spu_new: failed to map %s resource 2\n",
258 			 np->full_name);
259 		goto out_unmap;
260 	}
261 	if (!firmware_has_feature(FW_FEATURE_LPAR))
262 		ret = spu_map_resource(spu, 3,
263 			       (void __iomem**)&spu->priv1, NULL);
264 	if (ret) {
265 		pr_debug("spu_new: failed to map %s resource 3\n",
266 			 np->full_name);
267 		goto out_unmap;
268 	}
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);
276 
277 	return 0;
278 
279 out_unmap:
280 	spu_unmap(spu);
281 out:
282 	pr_debug("failed to map spe %s: %d\n", spu->name, ret);
283 	return ret;
284 }
285 
286 static int __init of_enumerate_spus(int (*fn)(void *data))
287 {
288 	int ret;
289 	struct device_node *node;
290 	unsigned int n = 0;
291 
292 	ret = -ENODEV;
293 	for (node = of_find_node_by_type(NULL, "spe");
294 			node; node = of_find_node_by_type(node, "spe")) {
295 		ret = fn(node);
296 		if (ret) {
297 			printk(KERN_WARNING "%s: Error initializing %s\n",
298 				__func__, node->name);
299 			break;
300 		}
301 		n++;
302 	}
303 	return ret ? ret : n;
304 }
305 
306 static int __init of_create_spu(struct spu *spu, void *data)
307 {
308 	int ret;
309 	struct device_node *spe = (struct device_node *)data;
310 	static int legacy_map = 0, legacy_irq = 0;
311 
312 	spu->devnode = of_node_get(spe);
313 	spu->spe_id = find_spu_unit_number(spe);
314 
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");
320 		ret = -ENODEV;
321 		goto out;
322 	}
323 
324 	ret = spu_map_device(spu);
325 	if (ret) {
326 		if (!legacy_map) {
327 			legacy_map = 1;
328 			printk(KERN_WARNING "%s: Legacy device tree found, "
329 				"trying to map old style\n", __func__);
330 		}
331 		ret = spu_map_device_old(spu);
332 		if (ret) {
333 			printk(KERN_ERR "Unable to map %s\n",
334 				spu->name);
335 			goto out;
336 		}
337 	}
338 
339 	ret = spu_map_interrupts(spu, spe);
340 	if (ret) {
341 		if (!legacy_irq) {
342 			legacy_irq = 1;
343 			printk(KERN_WARNING "%s: Legacy device tree found, "
344 				"trying old style irq\n", __func__);
345 		}
346 		ret = spu_map_interrupts_old(spu, spe);
347 		if (ret) {
348 			printk(KERN_ERR "%s: could not map interrupts\n",
349 				spu->name);
350 			goto out_unmap;
351 		}
352 	}
353 
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);
357 	goto out;
358 
359 out_unmap:
360 	spu_unmap(spu);
361 out:
362 	return ret;
363 }
364 
365 static int of_destroy_spu(struct spu *spu)
366 {
367 	spu_unmap(spu);
368 	of_node_put(spu->devnode);
369 	return 0;
370 }
371 
372 static void enable_spu_by_master_run(struct spu_context *ctx)
373 {
374 	ctx->ops->master_start(ctx);
375 }
376 
377 static void disable_spu_by_master_run(struct spu_context *ctx)
378 {
379 	ctx->ops->master_stop(ctx);
380 }
381 
382 /* Hardcoded affinity idxs for qs20 */
383 #define QS20_SPES_PER_BE 8
384 static int qs20_reg_idxs[QS20_SPES_PER_BE] =   { 0, 2, 4, 6, 7, 5, 3, 1 };
385 static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
386 
387 static struct spu *spu_lookup_reg(int node, u32 reg)
388 {
389 	struct spu *spu;
390 	const u32 *spu_reg;
391 
392 	list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
393 		spu_reg = of_get_property(spu_devnode(spu), "reg", NULL);
394 		if (*spu_reg == reg)
395 			return spu;
396 	}
397 	return NULL;
398 }
399 
400 static void init_affinity_qs20_harcoded(void)
401 {
402 	int node, i;
403 	struct spu *last_spu, *spu;
404 	u32 reg;
405 
406 	for (node = 0; node < MAX_NUMNODES; node++) {
407 		last_spu = NULL;
408 		for (i = 0; i < QS20_SPES_PER_BE; i++) {
409 			reg = qs20_reg_idxs[i];
410 			spu = spu_lookup_reg(node, reg);
411 			if (!spu)
412 				continue;
413 			spu->has_mem_affinity = qs20_reg_memory[reg];
414 			if (last_spu)
415 				list_add_tail(&spu->aff_list,
416 						&last_spu->aff_list);
417 			last_spu = spu;
418 		}
419 	}
420 }
421 
422 static int of_has_vicinity(void)
423 {
424 	struct device_node *dn;
425 
426 	for_each_node_by_type(dn, "spe") {
427 		if (of_find_property(dn, "vicinity", NULL))  {
428 			of_node_put(dn);
429 			return 1;
430 		}
431 	}
432 	return 0;
433 }
434 
435 static struct spu *devnode_spu(int cbe, struct device_node *dn)
436 {
437 	struct spu *spu;
438 
439 	list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
440 		if (spu_devnode(spu) == dn)
441 			return spu;
442 	return NULL;
443 }
444 
445 static struct spu *
446 neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid)
447 {
448 	struct spu *spu;
449 	struct device_node *spu_dn;
450 	const phandle *vic_handles;
451 	int lenp, i;
452 
453 	list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
454 		spu_dn = spu_devnode(spu);
455 		if (spu_dn == avoid)
456 			continue;
457 		vic_handles = of_get_property(spu_dn, "vicinity", &lenp);
458 		for (i=0; i < (lenp / sizeof(phandle)); i++) {
459 			if (vic_handles[i] == target->phandle)
460 				return spu;
461 		}
462 	}
463 	return NULL;
464 }
465 
466 static void init_affinity_node(int cbe)
467 {
468 	struct spu *spu, *last_spu;
469 	struct device_node *vic_dn, *last_spu_dn;
470 	phandle avoid_ph;
471 	const phandle *vic_handles;
472 	const char *name;
473 	int lenp, i, added;
474 
475 	last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu,
476 								cbe_list);
477 	avoid_ph = 0;
478 	for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
479 		last_spu_dn = spu_devnode(last_spu);
480 		vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp);
481 
482 		/*
483 		 * Walk through each phandle in vicinity property of the spu
484 		 * (tipically two vicinity phandles per spe node)
485 		 */
486 		for (i = 0; i < (lenp / sizeof(phandle)); i++) {
487 			if (vic_handles[i] == avoid_ph)
488 				continue;
489 
490 			vic_dn = of_find_node_by_phandle(vic_handles[i]);
491 			if (!vic_dn)
492 				continue;
493 
494 			/* a neighbour might be spe, mic-tm, or bif0 */
495 			name = of_get_property(vic_dn, "name", NULL);
496 			if (!name)
497 				continue;
498 
499 			if (strcmp(name, "spe") == 0) {
500 				spu = devnode_spu(cbe, vic_dn);
501 				avoid_ph = last_spu_dn->phandle;
502 			} else {
503 				/*
504 				 * "mic-tm" and "bif0" nodes do not have
505 				 * vicinity property. So we need to find the
506 				 * spe which has vic_dn as neighbour, but
507 				 * skipping the one we came from (last_spu_dn)
508 				 */
509 				spu = neighbour_spu(cbe, vic_dn, last_spu_dn);
510 				if (!spu)
511 					continue;
512 				if (!strcmp(name, "mic-tm")) {
513 					last_spu->has_mem_affinity = 1;
514 					spu->has_mem_affinity = 1;
515 				}
516 				avoid_ph = vic_dn->phandle;
517 			}
518 
519 			list_add_tail(&spu->aff_list, &last_spu->aff_list);
520 			last_spu = spu;
521 			break;
522 		}
523 	}
524 }
525 
526 static void init_affinity_fw(void)
527 {
528 	int cbe;
529 
530 	for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
531 		init_affinity_node(cbe);
532 }
533 
534 static int __init init_affinity(void)
535 {
536 	if (of_has_vicinity()) {
537 		init_affinity_fw();
538 	} else {
539 		long root = of_get_flat_dt_root();
540 		if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
541 			init_affinity_qs20_harcoded();
542 		else
543 			printk("No affinity configuration found\n");
544 	}
545 
546 	return 0;
547 }
548 
549 const struct spu_management_ops spu_management_of_ops = {
550 	.enumerate_spus = of_enumerate_spus,
551 	.create_spu = of_create_spu,
552 	.destroy_spu = of_destroy_spu,
553 	.enable_spu = enable_spu_by_master_run,
554 	.disable_spu = disable_spu_by_master_run,
555 	.init_affinity = init_affinity,
556 };
557