2 * spu hypervisor abstraction for direct hardware access.
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 * Copyright 2006 Sony Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/interrupt.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/ptrace.h>
25 #include <linux/slab.h>
26 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/device.h>
33 #include <asm/spu_priv1.h>
34 #include <asm/firmware.h>
37 #include "interrupt.h"
38 #include "spu_priv1_mmio.h"
40 static DEFINE_MUTEX(add_spumem_mutex);
44 struct device_node *devnode;
45 struct spu_priv1 __iomem *priv1;
48 static struct spu_pdata *spu_get_pdata(struct spu *spu)
54 struct device_node *spu_devnode(struct spu *spu)
56 return spu_get_pdata(spu)->devnode;
59 EXPORT_SYMBOL_GPL(spu_devnode);
61 static int __init find_spu_node_id(struct device_node *spe)
63 const unsigned int *id;
64 struct device_node *cpu;
65 cpu = spe->parent->parent;
66 id = get_property(cpu, "node-id", NULL);
70 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
73 const struct address_prop {
74 unsigned long address;
76 } __attribute__((packed)) *p;
79 unsigned long start_pfn, nr_pages;
80 struct pglist_data *pgdata;
84 p = get_property(spe, prop, &proplen);
85 WARN_ON(proplen != sizeof (*p));
87 start_pfn = p->address >> PAGE_SHIFT;
88 nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
90 pgdata = NODE_DATA(spu_get_pdata(spu)->nid);
91 zone = pgdata->node_zones;
93 /* XXX rethink locking here */
94 mutex_lock(&add_spumem_mutex);
95 ret = __add_pages(zone, start_pfn, nr_pages);
96 mutex_unlock(&add_spumem_mutex);
101 static void __iomem * __init map_spe_prop(struct spu *spu,
102 struct device_node *n, const char *name)
104 const struct address_prop {
105 unsigned long address;
107 } __attribute__((packed)) *prop;
111 void __iomem *ret = NULL;
114 p = get_property(n, name, &proplen);
115 if (proplen != sizeof (struct address_prop))
120 err = cell_spuprop_present(spu, n, name);
121 if (err && (err != -EEXIST))
124 ret = ioremap(prop->address, prop->len);
130 static void spu_unmap(struct spu *spu)
133 iounmap(spu_get_pdata(spu)->priv1);
134 iounmap(spu->problem);
135 iounmap((__force u8 __iomem *)spu->local_store);
138 static int __init spu_map_interrupts_old(struct spu *spu,
139 struct device_node *np)
144 /* Get the interrupt source unit from the device-tree */
145 tmp = get_property(np, "isrc", NULL);
150 /* Add the node number */
151 isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
153 /* Now map interrupts of all 3 classes */
154 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
155 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
156 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
158 /* Right now, we only fail if class 2 failed */
159 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
162 static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
168 spu->name = get_property(node, "name", NULL);
172 prop = get_property(node, "local-store", NULL);
175 spu->local_store_phys = *(unsigned long *)prop;
177 /* we use local store as ram, not io memory */
178 spu->local_store = (void __force *)
179 map_spe_prop(spu, node, "local-store");
180 if (!spu->local_store)
183 prop = get_property(node, "problem", NULL);
186 spu->problem_phys = *(unsigned long *)prop;
188 spu->problem= map_spe_prop(spu, node, "problem");
192 spu_get_pdata(spu)->priv1= map_spe_prop(spu, node, "priv1");
194 spu->priv2= map_spe_prop(spu, node, "priv2");
206 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
212 for (i=0; i < 3; i++) {
213 ret = of_irq_map_one(np, i, &oirq);
215 pr_debug("spu_new: failed to get irq %d\n", i);
219 pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
220 oirq.controller->full_name);
221 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
222 oirq.specifier, oirq.size);
223 if (spu->irqs[i] == NO_IRQ) {
224 pr_debug("spu_new: failed to map it !\n");
231 pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
233 for (; i >= 0; i--) {
234 if (spu->irqs[i] != NO_IRQ)
235 irq_dispose_mapping(spu->irqs[i]);
240 static int spu_map_resource(struct spu *spu, int nr,
241 void __iomem** virt, unsigned long *phys)
243 struct device_node *np = spu_get_pdata(spu)->devnode;
244 unsigned long start_pfn, nr_pages;
245 struct pglist_data *pgdata;
247 struct resource resource = { };
251 ret = of_address_to_resource(np, nr, &resource);
256 *phys = resource.start;
257 len = resource.end - resource.start + 1;
258 *virt = ioremap(resource.start, len);
262 start_pfn = resource.start >> PAGE_SHIFT;
263 nr_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
265 pgdata = NODE_DATA(spu_get_pdata(spu)->nid);
266 zone = pgdata->node_zones;
268 /* XXX rethink locking here */
269 mutex_lock(&add_spumem_mutex);
270 ret = __add_pages(zone, start_pfn, nr_pages);
271 mutex_unlock(&add_spumem_mutex);
277 static int __init spu_map_device(struct spu *spu)
279 struct device_node *np = spu_get_pdata(spu)->devnode;
282 spu->name = get_property(np, "name", NULL);
286 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
287 &spu->local_store_phys);
289 pr_debug("spu_new: failed to map %s resource 0\n",
293 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
296 pr_debug("spu_new: failed to map %s resource 1\n",
300 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
302 pr_debug("spu_new: failed to map %s resource 2\n",
306 if (!firmware_has_feature(FW_FEATURE_LPAR))
307 ret = spu_map_resource(spu, 3,
308 (void __iomem**)&spu_get_pdata(spu)->priv1, NULL);
310 pr_debug("spu_new: failed to map %s resource 3\n",
314 pr_debug("spu_new: %s maps:\n", np->full_name);
315 pr_debug(" local store : 0x%016lx -> 0x%p\n",
316 spu->local_store_phys, spu->local_store);
317 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
318 spu->problem_phys, spu->problem);
319 pr_debug(" priv2 : 0x%p\n", spu->priv2);
320 pr_debug(" priv1 : 0x%p\n",
321 spu_get_pdata(spu)->priv1);
328 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
332 static int __init of_enumerate_spus(int (*fn)(void *data))
335 struct device_node *node;
338 for (node = of_find_node_by_type(NULL, "spe");
339 node; node = of_find_node_by_type(node, "spe")) {
342 printk(KERN_WARNING "%s: Error initializing %s\n",
343 __FUNCTION__, node->name);
350 static int __init of_create_spu(struct spu *spu, void *data)
353 struct device_node *spe = (struct device_node *)data;
355 spu->pdata = kzalloc(sizeof(struct spu_pdata),
361 spu_get_pdata(spu)->devnode = of_node_get(spe);
363 spu->node = find_spu_node_id(spe);
364 if (spu->node >= MAX_NUMNODES) {
365 printk(KERN_WARNING "SPE %s on node %d ignored,"
366 " node number too big\n", spe->full_name, spu->node);
367 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
372 spu_get_pdata(spu)->nid = of_node_to_nid(spe);
373 if (spu_get_pdata(spu)->nid == -1)
374 spu_get_pdata(spu)->nid = 0;
376 ret = spu_map_device(spu);
379 ret = spu_map_device_old(spu, spe);
383 ret = spu_map_interrupts(spu, spe);
385 ret = spu_map_interrupts_old(spu, spe);
389 pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n", spu->name,
390 spu->local_store, spu->problem, spu_get_pdata(spu)->priv1,
391 spu->priv2, spu->number);
403 static int of_destroy_spu(struct spu *spu)
406 of_node_put(spu_get_pdata(spu)->devnode);
412 const struct spu_management_ops spu_management_of_ops = {
413 .enumerate_spus = of_enumerate_spus,
414 .create_spu = of_create_spu,
415 .destroy_spu = of_destroy_spu,
418 static void int_mask_and(struct spu *spu, int class, u64 mask)
422 old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
423 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
427 static void int_mask_or(struct spu *spu, int class, u64 mask)
431 old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
432 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
436 static void int_mask_set(struct spu *spu, int class, u64 mask)
438 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class], mask);
441 static u64 int_mask_get(struct spu *spu, int class)
443 return in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
446 static void int_stat_clear(struct spu *spu, int class, u64 stat)
448 out_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class], stat);
451 static u64 int_stat_get(struct spu *spu, int class)
453 return in_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class]);
456 static void cpu_affinity_set(struct spu *spu, int cpu)
458 u64 target = iic_get_target_id(cpu);
459 u64 route = target << 48 | target << 32 | target << 16;
460 out_be64(&spu_get_pdata(spu)->priv1->int_route_RW, route);
463 static u64 mfc_dar_get(struct spu *spu)
465 return in_be64(&spu_get_pdata(spu)->priv1->mfc_dar_RW);
468 static u64 mfc_dsisr_get(struct spu *spu)
470 return in_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW);
473 static void mfc_dsisr_set(struct spu *spu, u64 dsisr)
475 out_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW, dsisr);
478 static void mfc_sdr_setup(struct spu *spu)
480 out_be64(&spu_get_pdata(spu)->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
483 static void mfc_sr1_set(struct spu *spu, u64 sr1)
485 out_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW, sr1);
488 static u64 mfc_sr1_get(struct spu *spu)
490 return in_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW);
493 static void mfc_tclass_id_set(struct spu *spu, u64 tclass_id)
495 out_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW, tclass_id);
498 static u64 mfc_tclass_id_get(struct spu *spu)
500 return in_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW);
503 static void tlb_invalidate(struct spu *spu)
505 out_be64(&spu_get_pdata(spu)->priv1->tlb_invalidate_entry_W, 0ul);
508 static void resource_allocation_groupID_set(struct spu *spu, u64 id)
510 out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW,
514 static u64 resource_allocation_groupID_get(struct spu *spu)
517 &spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW);
520 static void resource_allocation_enable_set(struct spu *spu, u64 enable)
522 out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_enable_RW,
526 static u64 resource_allocation_enable_get(struct spu *spu)
529 &spu_get_pdata(spu)->priv1->resource_allocation_enable_RW);
532 const struct spu_priv1_ops spu_priv1_mmio_ops =
534 .int_mask_and = int_mask_and,
535 .int_mask_or = int_mask_or,
536 .int_mask_set = int_mask_set,
537 .int_mask_get = int_mask_get,
538 .int_stat_clear = int_stat_clear,
539 .int_stat_get = int_stat_get,
540 .cpu_affinity_set = cpu_affinity_set,
541 .mfc_dar_get = mfc_dar_get,
542 .mfc_dsisr_get = mfc_dsisr_get,
543 .mfc_dsisr_set = mfc_dsisr_set,
544 .mfc_sdr_setup = mfc_sdr_setup,
545 .mfc_sr1_set = mfc_sr1_set,
546 .mfc_sr1_get = mfc_sr1_get,
547 .mfc_tclass_id_set = mfc_tclass_id_set,
548 .mfc_tclass_id_get = mfc_tclass_id_get,
549 .tlb_invalidate = tlb_invalidate,
550 .resource_allocation_groupID_set = resource_allocation_groupID_set,
551 .resource_allocation_groupID_get = resource_allocation_groupID_get,
552 .resource_allocation_enable_set = resource_allocation_enable_set,
553 .resource_allocation_enable_get = resource_allocation_enable_get,