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);
43 struct device_node *devnode;
44 struct spu_priv1 __iomem *priv1;
47 static struct spu_pdata *spu_get_pdata(struct spu *spu)
53 struct device_node *spu_devnode(struct spu *spu)
55 return spu_get_pdata(spu)->devnode;
58 EXPORT_SYMBOL_GPL(spu_devnode);
60 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
63 const struct address_prop {
64 unsigned long address;
66 } __attribute__((packed)) *p;
69 unsigned long start_pfn, nr_pages;
70 struct pglist_data *pgdata;
74 p = get_property(spe, prop, &proplen);
75 WARN_ON(proplen != sizeof (*p));
77 start_pfn = p->address >> PAGE_SHIFT;
78 nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
80 pgdata = NODE_DATA(spu->node);
81 zone = pgdata->node_zones;
83 /* XXX rethink locking here */
84 mutex_lock(&add_spumem_mutex);
85 ret = __add_pages(zone, start_pfn, nr_pages);
86 mutex_unlock(&add_spumem_mutex);
91 static void __iomem * __init map_spe_prop(struct spu *spu,
92 struct device_node *n, const char *name)
94 const struct address_prop {
95 unsigned long address;
97 } __attribute__((packed)) *prop;
101 void __iomem *ret = NULL;
104 p = get_property(n, name, &proplen);
105 if (proplen != sizeof (struct address_prop))
110 err = cell_spuprop_present(spu, n, name);
111 if (err && (err != -EEXIST))
114 ret = ioremap(prop->address, prop->len);
120 static void spu_unmap(struct spu *spu)
123 iounmap(spu_get_pdata(spu)->priv1);
124 iounmap(spu->problem);
125 iounmap((__force u8 __iomem *)spu->local_store);
128 static int __init spu_map_interrupts_old(struct spu *spu,
129 struct device_node *np)
135 /* Get the interrupt source unit from the device-tree */
136 tmp = get_property(np, "isrc", NULL);
141 tmp = get_property(np->parent->parent, "node-id", NULL);
143 printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
148 /* Add the node number */
149 isrc |= nid << IIC_IRQ_NODE_SHIFT;
151 /* Now map interrupts of all 3 classes */
152 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
153 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
154 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
156 /* Right now, we only fail if class 2 failed */
157 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
160 static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
166 spu->name = get_property(node, "name", NULL);
170 prop = get_property(node, "local-store", NULL);
173 spu->local_store_phys = *(unsigned long *)prop;
175 /* we use local store as ram, not io memory */
176 spu->local_store = (void __force *)
177 map_spe_prop(spu, node, "local-store");
178 if (!spu->local_store)
181 prop = get_property(node, "problem", NULL);
184 spu->problem_phys = *(unsigned long *)prop;
186 spu->problem= map_spe_prop(spu, node, "problem");
190 spu_get_pdata(spu)->priv1= map_spe_prop(spu, node, "priv1");
192 spu->priv2= map_spe_prop(spu, node, "priv2");
204 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
210 for (i=0; i < 3; i++) {
211 ret = of_irq_map_one(np, i, &oirq);
213 pr_debug("spu_new: failed to get irq %d\n", i);
217 pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
218 oirq.controller->full_name);
219 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
220 oirq.specifier, oirq.size);
221 if (spu->irqs[i] == NO_IRQ) {
222 pr_debug("spu_new: failed to map it !\n");
229 pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
231 for (; i >= 0; i--) {
232 if (spu->irqs[i] != NO_IRQ)
233 irq_dispose_mapping(spu->irqs[i]);
238 static int spu_map_resource(struct spu *spu, int nr,
239 void __iomem** virt, unsigned long *phys)
241 struct device_node *np = spu_get_pdata(spu)->devnode;
242 unsigned long start_pfn, nr_pages;
243 struct pglist_data *pgdata;
245 struct resource resource = { };
249 ret = of_address_to_resource(np, nr, &resource);
254 *phys = resource.start;
255 len = resource.end - resource.start + 1;
256 *virt = ioremap(resource.start, len);
260 start_pfn = resource.start >> PAGE_SHIFT;
261 nr_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
263 pgdata = NODE_DATA(spu->node);
264 zone = pgdata->node_zones;
266 /* XXX rethink locking here */
267 mutex_lock(&add_spumem_mutex);
268 ret = __add_pages(zone, start_pfn, nr_pages);
269 mutex_unlock(&add_spumem_mutex);
275 static int __init spu_map_device(struct spu *spu)
277 struct device_node *np = spu_get_pdata(spu)->devnode;
280 spu->name = get_property(np, "name", NULL);
284 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
285 &spu->local_store_phys);
287 pr_debug("spu_new: failed to map %s resource 0\n",
291 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
294 pr_debug("spu_new: failed to map %s resource 1\n",
298 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
300 pr_debug("spu_new: failed to map %s resource 2\n",
304 if (!firmware_has_feature(FW_FEATURE_LPAR))
305 ret = spu_map_resource(spu, 3,
306 (void __iomem**)&spu_get_pdata(spu)->priv1, NULL);
308 pr_debug("spu_new: failed to map %s resource 3\n",
312 pr_debug("spu_new: %s maps:\n", np->full_name);
313 pr_debug(" local store : 0x%016lx -> 0x%p\n",
314 spu->local_store_phys, spu->local_store);
315 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
316 spu->problem_phys, spu->problem);
317 pr_debug(" priv2 : 0x%p\n", spu->priv2);
318 pr_debug(" priv1 : 0x%p\n",
319 spu_get_pdata(spu)->priv1);
326 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
330 static int __init of_enumerate_spus(int (*fn)(void *data))
333 struct device_node *node;
336 for (node = of_find_node_by_type(NULL, "spe");
337 node; node = of_find_node_by_type(node, "spe")) {
340 printk(KERN_WARNING "%s: Error initializing %s\n",
341 __FUNCTION__, node->name);
348 static int __init of_create_spu(struct spu *spu, void *data)
351 struct device_node *spe = (struct device_node *)data;
353 spu->pdata = kzalloc(sizeof(struct spu_pdata),
359 spu_get_pdata(spu)->devnode = of_node_get(spe);
361 spu->node = of_node_to_nid(spe);
362 if (spu->node >= MAX_NUMNODES) {
363 printk(KERN_WARNING "SPE %s on node %d ignored,"
364 " node number too big\n", spe->full_name, spu->node);
365 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
370 ret = spu_map_device(spu);
373 ret = spu_map_device_old(spu, spe);
377 ret = spu_map_interrupts(spu, spe);
379 ret = spu_map_interrupts_old(spu, spe);
383 pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n", spu->name,
384 spu->local_store, spu->problem, spu_get_pdata(spu)->priv1,
385 spu->priv2, spu->number);
397 static int of_destroy_spu(struct spu *spu)
400 of_node_put(spu_get_pdata(spu)->devnode);
406 const struct spu_management_ops spu_management_of_ops = {
407 .enumerate_spus = of_enumerate_spus,
408 .create_spu = of_create_spu,
409 .destroy_spu = of_destroy_spu,
412 static void int_mask_and(struct spu *spu, int class, u64 mask)
416 old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
417 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
421 static void int_mask_or(struct spu *spu, int class, u64 mask)
425 old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
426 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
430 static void int_mask_set(struct spu *spu, int class, u64 mask)
432 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class], mask);
435 static u64 int_mask_get(struct spu *spu, int class)
437 return in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
440 static void int_stat_clear(struct spu *spu, int class, u64 stat)
442 out_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class], stat);
445 static u64 int_stat_get(struct spu *spu, int class)
447 return in_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class]);
450 static void cpu_affinity_set(struct spu *spu, int cpu)
452 u64 target = iic_get_target_id(cpu);
453 u64 route = target << 48 | target << 32 | target << 16;
454 out_be64(&spu_get_pdata(spu)->priv1->int_route_RW, route);
457 static u64 mfc_dar_get(struct spu *spu)
459 return in_be64(&spu_get_pdata(spu)->priv1->mfc_dar_RW);
462 static u64 mfc_dsisr_get(struct spu *spu)
464 return in_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW);
467 static void mfc_dsisr_set(struct spu *spu, u64 dsisr)
469 out_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW, dsisr);
472 static void mfc_sdr_setup(struct spu *spu)
474 out_be64(&spu_get_pdata(spu)->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
477 static void mfc_sr1_set(struct spu *spu, u64 sr1)
479 out_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW, sr1);
482 static u64 mfc_sr1_get(struct spu *spu)
484 return in_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW);
487 static void mfc_tclass_id_set(struct spu *spu, u64 tclass_id)
489 out_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW, tclass_id);
492 static u64 mfc_tclass_id_get(struct spu *spu)
494 return in_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW);
497 static void tlb_invalidate(struct spu *spu)
499 out_be64(&spu_get_pdata(spu)->priv1->tlb_invalidate_entry_W, 0ul);
502 static void resource_allocation_groupID_set(struct spu *spu, u64 id)
504 out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW,
508 static u64 resource_allocation_groupID_get(struct spu *spu)
511 &spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW);
514 static void resource_allocation_enable_set(struct spu *spu, u64 enable)
516 out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_enable_RW,
520 static u64 resource_allocation_enable_get(struct spu *spu)
523 &spu_get_pdata(spu)->priv1->resource_allocation_enable_RW);
526 const struct spu_priv1_ops spu_priv1_mmio_ops =
528 .int_mask_and = int_mask_and,
529 .int_mask_or = int_mask_or,
530 .int_mask_set = int_mask_set,
531 .int_mask_get = int_mask_get,
532 .int_stat_clear = int_stat_clear,
533 .int_stat_get = int_stat_get,
534 .cpu_affinity_set = cpu_affinity_set,
535 .mfc_dar_get = mfc_dar_get,
536 .mfc_dsisr_get = mfc_dsisr_get,
537 .mfc_dsisr_set = mfc_dsisr_set,
538 .mfc_sdr_setup = mfc_sdr_setup,
539 .mfc_sr1_set = mfc_sr1_set,
540 .mfc_sr1_get = mfc_sr1_get,
541 .mfc_tclass_id_set = mfc_tclass_id_set,
542 .mfc_tclass_id_get = mfc_tclass_id_get,
543 .tlb_invalidate = tlb_invalidate,
544 .resource_allocation_groupID_set = resource_allocation_groupID_set,
545 .resource_allocation_groupID_get = resource_allocation_groupID_get,
546 .resource_allocation_enable_set = resource_allocation_enable_set,
547 .resource_allocation_enable_get = resource_allocation_enable_get,