[POWERPC] Fix spu SLB invalidations
[powerpc.git] / arch / powerpc / platforms / cell / spu_base.c
index ccfd0c4..eba7a26 100644 (file)
 #include <linux/interrupt.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/pci.h>
-#include <linux/poll.h>
 #include <linux/ptrace.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
-
-#include <asm/firmware.h>
-#include <asm/io.h>
-#include <asm/prom.h>
+#include <linux/mm.h>
+#include <linux/io.h>
 #include <linux/mutex.h>
 #include <asm/spu.h>
 #include <asm/spu_priv1.h>
-#include <asm/mmu_context.h>
-
-#include "interrupt.h"
+#include <asm/xmon.h>
 
+const struct spu_management_ops *spu_management_ops;
 const struct spu_priv1_ops *spu_priv1_ops;
 
+static struct list_head spu_list[MAX_NUMNODES];
+static LIST_HEAD(spu_full_list);
+static DEFINE_MUTEX(spu_mutex);
+static spinlock_t spu_list_lock = SPIN_LOCK_UNLOCKED;
+
 EXPORT_SYMBOL_GPL(spu_priv1_ops);
 
+void spu_invalidate_slbs(struct spu *spu)
+{
+       struct spu_priv2 __iomem *priv2 = spu->priv2;
+
+       if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
+               out_be64(&priv2->slb_invalidate_all_W, 0UL);
+}
+EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
+
+/* This is called by the MM core when a segment size is changed, to
+ * request a flush of all the SPEs using a given mm
+ */
+void spu_flush_all_slbs(struct mm_struct *mm)
+{
+       struct spu *spu;
+       unsigned long flags;
+
+       spin_lock_irqsave(&spu_list_lock, flags);
+       list_for_each_entry(spu, &spu_full_list, full_list) {
+               if (spu->mm == mm)
+                       spu_invalidate_slbs(spu);
+       }
+       spin_unlock_irqrestore(&spu_list_lock, flags);
+}
+
+/* The hack below stinks... try to do something better one of
+ * these days... Does it even work properly with NR_CPUS == 1 ?
+ */
+static inline void mm_needs_global_tlbie(struct mm_struct *mm)
+{
+       int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
+
+       /* Global TLBIE broadcast required with SPEs. */
+       __cpus_setall(&mm->cpu_vm_mask, nr);
+}
+
+void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&spu_list_lock, flags);
+       spu->mm = mm;
+       spin_unlock_irqrestore(&spu_list_lock, flags);
+       if (mm)
+               mm_needs_global_tlbie(mm);
+}
+EXPORT_SYMBOL_GPL(spu_associate_mm);
+
 static int __spu_trap_invalid_dma(struct spu *spu)
 {
        pr_debug("%s\n", __FUNCTION__);
@@ -79,6 +127,7 @@ static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
        struct spu_priv2 __iomem *priv2 = spu->priv2;
        struct mm_struct *mm = spu->mm;
        u64 esid, vsid, llp;
+       int psize;
 
        pr_debug("%s\n", __FUNCTION__);
 
@@ -89,26 +138,43 @@ static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
                printk("%s: invalid access during switch!\n", __func__);
                return 1;
        }
-       if (!mm || (REGION_ID(ea) != USER_REGION_ID)) {
+       esid = (ea & ESID_MASK) | SLB_ESID_V;
+
+       switch(REGION_ID(ea)) {
+       case USER_REGION_ID:
+#ifdef CONFIG_HUGETLB_PAGE
+               if (in_hugepage_area(mm->context, ea))
+                       psize = mmu_huge_psize;
+               else
+#endif
+                       psize = mm->context.user_psize;
+               vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
+                               SLB_VSID_USER;
+               break;
+       case VMALLOC_REGION_ID:
+               if (ea < VMALLOC_END)
+                       psize = mmu_vmalloc_psize;
+               else
+                       psize = mmu_io_psize;
+               vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
+                       SLB_VSID_KERNEL;
+               break;
+       case KERNEL_REGION_ID:
+               psize = mmu_linear_psize;
+               vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
+                       SLB_VSID_KERNEL;
+               break;
+       default:
                /* Future: support kernel segments so that drivers
                 * can use SPUs.
                 */
                pr_debug("invalid region access at %016lx\n", ea);
                return 1;
        }
-
-       esid = (ea & ESID_MASK) | SLB_ESID_V;
-#ifdef CONFIG_HUGETLB_PAGE
-       if (in_hugepage_area(mm->context, ea))
-               llp = mmu_psize_defs[mmu_huge_psize].sllp;
-       else
-#endif
-               llp = mmu_psize_defs[mmu_virtual_psize].sllp;
-       vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
-                       SLB_VSID_USER | llp;
+       llp = mmu_psize_defs[psize].sllp;
 
        out_be64(&priv2->slb_index_W, spu->slb_replace);
-       out_be64(&priv2->slb_vsid_RW, vsid);
+       out_be64(&priv2->slb_vsid_RW, vsid | llp);
        out_be64(&priv2->slb_esid_RW, esid);
 
        spu->slb_replace++;
@@ -162,9 +228,11 @@ int
 spu_irq_class_0_bottom(struct spu *spu)
 {
        unsigned long stat, mask;
+       unsigned long flags;
 
        spu->class_0_pending = 0;
 
+       spin_lock_irqsave(&spu->register_lock, flags);
        mask = spu_int_mask_get(spu, 0);
        stat = spu_int_stat_get(spu, 0);
 
@@ -180,6 +248,7 @@ spu_irq_class_0_bottom(struct spu *spu)
                __spu_trap_error(spu);
 
        spu_int_stat_clear(spu, 0, stat);
+       spin_unlock_irqrestore(&spu->register_lock, flags);
 
        return (stat & 0x7) ? -EIO : 0;
 }
@@ -319,9 +388,6 @@ static void spu_free_irqs(struct spu *spu)
                free_irq(spu->irqs[2], spu);
 }
 
-static struct list_head spu_list[MAX_NUMNODES];
-static DEFINE_MUTEX(spu_mutex);
-
 static void spu_init_channels(struct spu *spu)
 {
        static const struct {
@@ -364,8 +430,7 @@ struct spu *spu_alloc_node(int node)
        if (!list_empty(&spu_list[node])) {
                spu = list_entry(spu_list[node].next, struct spu, list);
                list_del_init(&spu->list);
-               pr_debug("Got SPU %x %d %d\n",
-                        spu->isrc, spu->number, spu->node);
+               pr_debug("Got SPU %d %d\n", spu->number, spu->node);
                spu_init_channels(spu);
        }
        mutex_unlock(&spu_mutex);
@@ -493,255 +558,65 @@ int spu_irq_class_1_bottom(struct spu *spu)
        if (!error) {
                spu_restart_dma(spu);
        } else {
-               __spu_trap_invalid_dma(spu);
+               spu->dma_callback(spu, SPE_EVENT_SPE_DATA_STORAGE);
        }
        return ret;
 }
 
-static int __init find_spu_node_id(struct device_node *spe)
-{
-       const unsigned int *id;
-       struct device_node *cpu;
-       cpu = spe->parent->parent;
-       id = get_property(cpu, "node-id", NULL);
-       return id ? *id : 0;
-}
-
-static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
-               const char *prop)
-{
-       static DEFINE_MUTEX(add_spumem_mutex);
-
-       const struct address_prop {
-               unsigned long address;
-               unsigned int len;
-       } __attribute__((packed)) *p;
-       int proplen;
-
-       unsigned long start_pfn, nr_pages;
-       struct pglist_data *pgdata;
-       struct zone *zone;
-       int ret;
-
-       p = get_property(spe, prop, &proplen);
-       WARN_ON(proplen != sizeof (*p));
-
-       start_pfn = p->address >> PAGE_SHIFT;
-       nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
-
-       pgdata = NODE_DATA(spu->nid);
-       zone = pgdata->node_zones;
-
-       /* XXX rethink locking here */
-       mutex_lock(&add_spumem_mutex);
-       ret = __add_pages(zone, start_pfn, nr_pages);
-       mutex_unlock(&add_spumem_mutex);
-
-       return ret;
-}
-
-static void __iomem * __init map_spe_prop(struct spu *spu,
-               struct device_node *n, const char *name)
-{
-       const struct address_prop {
-               unsigned long address;
-               unsigned int len;
-       } __attribute__((packed)) *prop;
-
-       const void *p;
-       int proplen;
-       void __iomem *ret = NULL;
-       int err = 0;
-
-       p = get_property(n, name, &proplen);
-       if (proplen != sizeof (struct address_prop))
-               return NULL;
-
-       prop = p;
-
-       err = cell_spuprop_present(spu, n, name);
-       if (err && (err != -EEXIST))
-               goto out;
-
-       ret = ioremap(prop->address, prop->len);
-
- out:
-       return ret;
-}
-
-static void spu_unmap(struct spu *spu)
-{
-       iounmap(spu->priv2);
-       iounmap(spu->priv1);
-       iounmap(spu->problem);
-       iounmap((__force u8 __iomem *)spu->local_store);
-}
-
-/* This function shall be abstracted for HV platforms */
-static int __init spu_map_interrupts_old(struct spu *spu, struct device_node *np)
-{
-       unsigned int isrc;
-       const u32 *tmp;
-
-       /* Get the interrupt source unit from the device-tree */
-       tmp = get_property(np, "isrc", NULL);
-       if (!tmp)
-               return -ENODEV;
-       isrc = tmp[0];
-
-       /* Add the node number */
-       isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
-       spu->isrc = isrc;
-
-       /* Now map interrupts of all 3 classes */
-       spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
-       spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
-       spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
-
-       /* Right now, we only fail if class 2 failed */
-       return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
-}
+struct sysdev_class spu_sysdev_class = {
+       set_kset_name("spu")
+};
 
-static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
+int spu_add_sysdev_attr(struct sysdev_attribute *attr)
 {
-       const char *prop;
-       int ret;
-
-       ret = -ENODEV;
-       spu->name = get_property(node, "name", NULL);
-       if (!spu->name)
-               goto out;
-
-       prop = get_property(node, "local-store", NULL);
-       if (!prop)
-               goto out;
-       spu->local_store_phys = *(unsigned long *)prop;
-
-       /* we use local store as ram, not io memory */
-       spu->local_store = (void __force *)
-               map_spe_prop(spu, node, "local-store");
-       if (!spu->local_store)
-               goto out;
-
-       prop = get_property(node, "problem", NULL);
-       if (!prop)
-               goto out_unmap;
-       spu->problem_phys = *(unsigned long *)prop;
-
-       spu->problem= map_spe_prop(spu, node, "problem");
-       if (!spu->problem)
-               goto out_unmap;
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-       spu->priv1= map_spe_prop(spu, node, "priv1");
-       /* priv1 is not available on a hypervisor */
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysdev_create_file(&spu->sysdev, attr);
 
-       spu->priv2= map_spe_prop(spu, node, "priv2");
-       if (!spu->priv2)
-               goto out_unmap;
-       ret = 0;
-       goto out;
-
-out_unmap:
-       spu_unmap(spu);
-out:
-       return ret;
+       mutex_unlock(&spu_mutex);
+       return 0;
 }
+EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
 
-static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
+int spu_add_sysdev_attr_group(struct attribute_group *attrs)
 {
-       struct of_irq oirq;
-       int ret;
-       int i;
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-       for (i=0; i < 3; i++) {
-               ret = of_irq_map_one(np, i, &oirq);
-               if (ret)
-                       goto err;
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysfs_create_group(&spu->sysdev.kobj, attrs);
 
-               ret = -EINVAL;
-               spu->irqs[i] = irq_create_of_mapping(oirq.controller,
-                                       oirq.specifier, oirq.size);
-               if (spu->irqs[i] == NO_IRQ)
-                       goto err;
-       }
+       mutex_unlock(&spu_mutex);
        return 0;
-
-err:
-       pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier, spu->name);
-       for (; i >= 0; i--) {
-               if (spu->irqs[i] != NO_IRQ)
-                       irq_dispose_mapping(spu->irqs[i]);
-       }
-       return ret;
 }
+EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
 
-static int spu_map_resource(struct device_node *node, int nr,
-               void __iomem** virt, unsigned long *phys)
-{
-       struct resource resource = { };
-       int ret;
-
-       ret = of_address_to_resource(node, 0, &resource);
-       if (ret)
-               goto out;
-
-       if (phys)
-               *phys = resource.start;
-       *virt = ioremap(resource.start, resource.end - resource.start);
-       if (!*virt)
-               ret = -EINVAL;
-
-out:
-       return ret;
-}
 
-static int __init spu_map_device(struct spu *spu, struct device_node *node)
+void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
 {
-       int ret = -ENODEV;
-       spu->name = get_property(node, "name", NULL);
-       if (!spu->name)
-               goto out;
-
-       ret = spu_map_resource(node, 0, (void __iomem**)&spu->local_store,
-                                       &spu->local_store_phys);
-       if (ret)
-               goto out;
-       ret = spu_map_resource(node, 1, (void __iomem**)&spu->problem,
-                                       &spu->problem_phys);
-       if (ret)
-               goto out_unmap;
-       ret = spu_map_resource(node, 2, (void __iomem**)&spu->priv2,
-                                       NULL);
-       if (ret)
-               goto out_unmap;
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-       if (!firmware_has_feature(FW_FEATURE_LPAR))
-               ret = spu_map_resource(node, 3, (void __iomem**)&spu->priv1,
-                                       NULL);
-       if (ret)
-               goto out_unmap;
-       return 0;
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysdev_remove_file(&spu->sysdev, attr);
 
-out_unmap:
-       spu_unmap(spu);
-out:
-       pr_debug("failed to map spe %s: %d\n", spu->name, ret);
-       return ret;
+       mutex_unlock(&spu_mutex);
 }
+EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
 
-struct sysdev_class spu_sysdev_class = {
-       set_kset_name("spu")
-};
-
-static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
+void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
 {
-       struct spu *spu = container_of(sysdev, struct spu, sysdev);
-       return sprintf(buf, "%d\n", spu->isrc);
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-}
-static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysfs_remove_group(&spu->sysdev.kobj, attrs);
 
-extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
+       mutex_unlock(&spu_mutex);
+}
+EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
 
 static int spu_create_sysdev(struct spu *spu)
 {
@@ -756,75 +631,62 @@ static int spu_create_sysdev(struct spu *spu)
                return ret;
        }
 
-       if (spu->isrc != 0)
-               sysdev_create_file(&spu->sysdev, &attr_isrc);
-       sysfs_add_device_to_node(&spu->sysdev, spu->nid);
+       sysfs_add_device_to_node(&spu->sysdev, spu->node);
 
        return 0;
 }
 
 static void spu_destroy_sysdev(struct spu *spu)
 {
-       sysdev_remove_file(&spu->sysdev, &attr_isrc);
-       sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
+       sysfs_remove_device_from_node(&spu->sysdev, spu->node);
        sysdev_unregister(&spu->sysdev);
 }
 
-static int __init create_spu(struct device_node *spe)
+static int __init create_spu(void *data)
 {
        struct spu *spu;
        int ret;
        static int number;
+       unsigned long flags;
 
        ret = -ENOMEM;
        spu = kzalloc(sizeof (*spu), GFP_KERNEL);
        if (!spu)
                goto out;
 
-       ret = spu_map_device(spu, spe);
-       /* try old method */
-       if (ret)
-               ret = spu_map_device_old(spu, spe);
+       spin_lock_init(&spu->register_lock);
+       mutex_lock(&spu_mutex);
+       spu->number = number++;
+       mutex_unlock(&spu_mutex);
+
+       ret = spu_create_spu(spu, data);
+
        if (ret)
                goto out_free;
 
-       spu->node = find_spu_node_id(spe);
-       spu->nid = of_node_to_nid(spe);
-       if (spu->nid == -1)
-               spu->nid = 0;
-       ret = spu_map_interrupts(spu, spe);
-       if (ret)
-               ret = spu_map_interrupts_old(spu, spe);
-       if (ret)
-               goto out_unmap;
-       spin_lock_init(&spu->register_lock);
-       spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
+       spu_mfc_sdr_setup(spu);
        spu_mfc_sr1_set(spu, 0x33);
-       mutex_lock(&spu_mutex);
-
-       spu->number = number++;
        ret = spu_request_irqs(spu);
        if (ret)
-               goto out_unlock;
+               goto out_destroy;
 
        ret = spu_create_sysdev(spu);
        if (ret)
                goto out_free_irqs;
 
+       mutex_lock(&spu_mutex);
+       spin_lock_irqsave(&spu_list_lock, flags);
        list_add(&spu->list, &spu_list[spu->node]);
+       list_add(&spu->full_list, &spu_full_list);
+       spin_unlock_irqrestore(&spu_list_lock, flags);
        mutex_unlock(&spu_mutex);
 
-       pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
-               spu->name, spu->isrc, spu->local_store,
-               spu->problem, spu->priv1, spu->priv2, spu->number);
        goto out;
 
 out_free_irqs:
        spu_free_irqs(spu);
-out_unlock:
-       mutex_unlock(&spu_mutex);
-out_unmap:
-       spu_unmap(spu);
+out_destroy:
+       spu_destroy_spu(spu);
 out_free:
        kfree(spu);
 out:
@@ -834,10 +696,11 @@ out:
 static void destroy_spu(struct spu *spu)
 {
        list_del_init(&spu->list);
+       list_del_init(&spu->full_list);
 
        spu_destroy_sysdev(spu);
        spu_free_irqs(spu);
-       spu_unmap(spu);
+       spu_destroy_spu(spu);
        kfree(spu);
 }
 
@@ -858,9 +721,11 @@ module_exit(cleanup_spu_base);
 
 static int __init init_spu_base(void)
 {
-       struct device_node *node;
        int i, ret;
 
+       if (!spu_management_ops)
+               return 0;
+
        /* create sysdev class for spus */
        ret = sysdev_class_register(&spu_sysdev_class);
        if (ret)
@@ -869,17 +734,17 @@ static int __init init_spu_base(void)
        for (i = 0; i < MAX_NUMNODES; i++)
                INIT_LIST_HEAD(&spu_list[i]);
 
-       ret = -ENODEV;
-       for (node = of_find_node_by_type(NULL, "spe");
-                       node; node = of_find_node_by_type(node, "spe")) {
-               ret = create_spu(node);
-               if (ret) {
-                       printk(KERN_WARNING "%s: Error initializing %s\n",
-                               __FUNCTION__, node->name);
-                       cleanup_spu_base();
-                       break;
-               }
+       ret = spu_enumerate_spus(create_spu);
+
+       if (ret) {
+               printk(KERN_WARNING "%s: Error initializing spus\n",
+                       __FUNCTION__);
+               cleanup_spu_base();
+               return ret;
        }
+
+       xmon_register_spus(&spu_full_list);
+
        return ret;
 }
 module_init(init_spu_base);