[POWERPC] spufs: fix spu irq affinity setting
[powerpc.git] / arch / powerpc / platforms / cell / spu_base.c
1 /*
2  * Low-level SPU handling
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  *
6  * Author: Arnd Bergmann <arndb@de.ibm.com>
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; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #undef DEBUG
24
25 #include <linux/interrupt.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/poll.h>
29 #include <linux/ptrace.h>
30 #include <linux/slab.h>
31 #include <linux/wait.h>
32
33 #include <asm/io.h>
34 #include <asm/prom.h>
35 #include <linux/mutex.h>
36 #include <asm/spu.h>
37 #include <asm/spu_priv1.h>
38 #include <asm/mmu_context.h>
39
40 #include "interrupt.h"
41
42 const struct spu_priv1_ops *spu_priv1_ops;
43
44 EXPORT_SYMBOL_GPL(spu_priv1_ops);
45
46 static int __spu_trap_invalid_dma(struct spu *spu)
47 {
48         pr_debug("%s\n", __FUNCTION__);
49         force_sig(SIGBUS, /* info, */ current);
50         return 0;
51 }
52
53 static int __spu_trap_dma_align(struct spu *spu)
54 {
55         pr_debug("%s\n", __FUNCTION__);
56         force_sig(SIGBUS, /* info, */ current);
57         return 0;
58 }
59
60 static int __spu_trap_error(struct spu *spu)
61 {
62         pr_debug("%s\n", __FUNCTION__);
63         force_sig(SIGILL, /* info, */ current);
64         return 0;
65 }
66
67 static void spu_restart_dma(struct spu *spu)
68 {
69         struct spu_priv2 __iomem *priv2 = spu->priv2;
70
71         if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
72                 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
73 }
74
75 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
76 {
77         struct spu_priv2 __iomem *priv2 = spu->priv2;
78         struct mm_struct *mm = spu->mm;
79         u64 esid, vsid, llp;
80
81         pr_debug("%s\n", __FUNCTION__);
82
83         if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
84                 /* SLBs are pre-loaded for context switch, so
85                  * we should never get here!
86                  */
87                 printk("%s: invalid access during switch!\n", __func__);
88                 return 1;
89         }
90         if (!mm || (REGION_ID(ea) != USER_REGION_ID)) {
91                 /* Future: support kernel segments so that drivers
92                  * can use SPUs.
93                  */
94                 pr_debug("invalid region access at %016lx\n", ea);
95                 return 1;
96         }
97
98         esid = (ea & ESID_MASK) | SLB_ESID_V;
99 #ifdef CONFIG_HUGETLB_PAGE
100         if (in_hugepage_area(mm->context, ea))
101                 llp = mmu_psize_defs[mmu_huge_psize].sllp;
102         else
103 #endif
104                 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
105         vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
106                         SLB_VSID_USER | llp;
107
108         out_be64(&priv2->slb_index_W, spu->slb_replace);
109         out_be64(&priv2->slb_vsid_RW, vsid);
110         out_be64(&priv2->slb_esid_RW, esid);
111
112         spu->slb_replace++;
113         if (spu->slb_replace >= 8)
114                 spu->slb_replace = 0;
115
116         spu_restart_dma(spu);
117
118         return 0;
119 }
120
121 extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
122 static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
123 {
124         pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
125
126         /* Handle kernel space hash faults immediately.
127            User hash faults need to be deferred to process context. */
128         if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
129             && REGION_ID(ea) != USER_REGION_ID
130             && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
131                 spu_restart_dma(spu);
132                 return 0;
133         }
134
135         if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
136                 printk("%s: invalid access during switch!\n", __func__);
137                 return 1;
138         }
139
140         spu->dar = ea;
141         spu->dsisr = dsisr;
142         mb();
143         if (spu->stop_callback)
144                 spu->stop_callback(spu);
145         return 0;
146 }
147
148 static int __spu_trap_mailbox(struct spu *spu)
149 {
150         if (spu->ibox_callback)
151                 spu->ibox_callback(spu);
152
153         /* atomically disable SPU mailbox interrupts */
154         spin_lock(&spu->register_lock);
155         spu_int_mask_and(spu, 2, ~0x1);
156         spin_unlock(&spu->register_lock);
157         return 0;
158 }
159
160 static int __spu_trap_stop(struct spu *spu)
161 {
162         pr_debug("%s\n", __FUNCTION__);
163         spu->stop_code = in_be32(&spu->problem->spu_status_R);
164         if (spu->stop_callback)
165                 spu->stop_callback(spu);
166         return 0;
167 }
168
169 static int __spu_trap_halt(struct spu *spu)
170 {
171         pr_debug("%s\n", __FUNCTION__);
172         spu->stop_code = in_be32(&spu->problem->spu_status_R);
173         if (spu->stop_callback)
174                 spu->stop_callback(spu);
175         return 0;
176 }
177
178 static int __spu_trap_tag_group(struct spu *spu)
179 {
180         pr_debug("%s\n", __FUNCTION__);
181         spu->mfc_callback(spu);
182         return 0;
183 }
184
185 static int __spu_trap_spubox(struct spu *spu)
186 {
187         if (spu->wbox_callback)
188                 spu->wbox_callback(spu);
189
190         /* atomically disable SPU mailbox interrupts */
191         spin_lock(&spu->register_lock);
192         spu_int_mask_and(spu, 2, ~0x10);
193         spin_unlock(&spu->register_lock);
194         return 0;
195 }
196
197 static irqreturn_t
198 spu_irq_class_0(int irq, void *data, struct pt_regs *regs)
199 {
200         struct spu *spu;
201
202         spu = data;
203         spu->class_0_pending = 1;
204         if (spu->stop_callback)
205                 spu->stop_callback(spu);
206
207         return IRQ_HANDLED;
208 }
209
210 int
211 spu_irq_class_0_bottom(struct spu *spu)
212 {
213         unsigned long stat, mask;
214
215         spu->class_0_pending = 0;
216
217         mask = spu_int_mask_get(spu, 0);
218         stat = spu_int_stat_get(spu, 0);
219
220         stat &= mask;
221
222         if (stat & 1) /* invalid MFC DMA */
223                 __spu_trap_invalid_dma(spu);
224
225         if (stat & 2) /* invalid DMA alignment */
226                 __spu_trap_dma_align(spu);
227
228         if (stat & 4) /* error on SPU */
229                 __spu_trap_error(spu);
230
231         spu_int_stat_clear(spu, 0, stat);
232
233         return (stat & 0x7) ? -EIO : 0;
234 }
235 EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
236
237 static irqreturn_t
238 spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
239 {
240         struct spu *spu;
241         unsigned long stat, mask, dar, dsisr;
242
243         spu = data;
244
245         /* atomically read & clear class1 status. */
246         spin_lock(&spu->register_lock);
247         mask  = spu_int_mask_get(spu, 1);
248         stat  = spu_int_stat_get(spu, 1) & mask;
249         dar   = spu_mfc_dar_get(spu);
250         dsisr = spu_mfc_dsisr_get(spu);
251         if (stat & 2) /* mapping fault */
252                 spu_mfc_dsisr_set(spu, 0ul);
253         spu_int_stat_clear(spu, 1, stat);
254         spin_unlock(&spu->register_lock);
255         pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
256                         dar, dsisr);
257
258         if (stat & 1) /* segment fault */
259                 __spu_trap_data_seg(spu, dar);
260
261         if (stat & 2) { /* mapping fault */
262                 __spu_trap_data_map(spu, dar, dsisr);
263         }
264
265         if (stat & 4) /* ls compare & suspend on get */
266                 ;
267
268         if (stat & 8) /* ls compare & suspend on put */
269                 ;
270
271         return stat ? IRQ_HANDLED : IRQ_NONE;
272 }
273 EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
274
275 static irqreturn_t
276 spu_irq_class_2(int irq, void *data, struct pt_regs *regs)
277 {
278         struct spu *spu;
279         unsigned long stat;
280         unsigned long mask;
281
282         spu = data;
283         stat = spu_int_stat_get(spu, 2);
284         mask = spu_int_mask_get(spu, 2);
285
286         pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
287
288         stat &= mask;
289
290         if (stat & 1)  /* PPC core mailbox */
291                 __spu_trap_mailbox(spu);
292
293         if (stat & 2) /* SPU stop-and-signal */
294                 __spu_trap_stop(spu);
295
296         if (stat & 4) /* SPU halted */
297                 __spu_trap_halt(spu);
298
299         if (stat & 8) /* DMA tag group complete */
300                 __spu_trap_tag_group(spu);
301
302         if (stat & 0x10) /* SPU mailbox threshold */
303                 __spu_trap_spubox(spu);
304
305         spu_int_stat_clear(spu, 2, stat);
306         return stat ? IRQ_HANDLED : IRQ_NONE;
307 }
308
309 static int
310 spu_request_irqs(struct spu *spu)
311 {
312         int ret;
313         int irq_base;
314
315         irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
316
317         snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number);
318         ret = request_irq(irq_base + spu->isrc,
319                  spu_irq_class_0, SA_INTERRUPT, spu->irq_c0, spu);
320         if (ret)
321                 goto out;
322
323         snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number);
324         ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc,
325                  spu_irq_class_1, SA_INTERRUPT, spu->irq_c1, spu);
326         if (ret)
327                 goto out1;
328
329         snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number);
330         ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc,
331                  spu_irq_class_2, SA_INTERRUPT, spu->irq_c2, spu);
332         if (ret)
333                 goto out2;
334         goto out;
335
336 out2:
337         free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
338 out1:
339         free_irq(irq_base + spu->isrc, spu);
340 out:
341         return ret;
342 }
343
344 static void
345 spu_free_irqs(struct spu *spu)
346 {
347         int irq_base;
348
349         irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
350
351         free_irq(irq_base + spu->isrc, spu);
352         free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
353         free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu);
354 }
355
356 static LIST_HEAD(spu_list);
357 static DEFINE_MUTEX(spu_mutex);
358
359 static void spu_init_channels(struct spu *spu)
360 {
361         static const struct {
362                  unsigned channel;
363                  unsigned count;
364         } zero_list[] = {
365                 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
366                 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
367         }, count_list[] = {
368                 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
369                 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
370                 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
371         };
372         struct spu_priv2 __iomem *priv2;
373         int i;
374
375         priv2 = spu->priv2;
376
377         /* initialize all channel data to zero */
378         for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
379                 int count;
380
381                 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
382                 for (count = 0; count < zero_list[i].count; count++)
383                         out_be64(&priv2->spu_chnldata_RW, 0);
384         }
385
386         /* initialize channel counts to meaningful values */
387         for (i = 0; i < ARRAY_SIZE(count_list); i++) {
388                 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
389                 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
390         }
391 }
392
393 struct spu *spu_alloc(void)
394 {
395         struct spu *spu;
396
397         mutex_lock(&spu_mutex);
398         if (!list_empty(&spu_list)) {
399                 spu = list_entry(spu_list.next, struct spu, list);
400                 list_del_init(&spu->list);
401                 pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
402         } else {
403                 pr_debug("No SPU left\n");
404                 spu = NULL;
405         }
406         mutex_unlock(&spu_mutex);
407
408         if (spu)
409                 spu_init_channels(spu);
410
411         return spu;
412 }
413 EXPORT_SYMBOL_GPL(spu_alloc);
414
415 void spu_free(struct spu *spu)
416 {
417         mutex_lock(&spu_mutex);
418         list_add_tail(&spu->list, &spu_list);
419         mutex_unlock(&spu_mutex);
420 }
421 EXPORT_SYMBOL_GPL(spu_free);
422
423 static int spu_handle_mm_fault(struct spu *spu)
424 {
425         struct mm_struct *mm = spu->mm;
426         struct vm_area_struct *vma;
427         u64 ea, dsisr, is_write;
428         int ret;
429
430         ea = spu->dar;
431         dsisr = spu->dsisr;
432 #if 0
433         if (!IS_VALID_EA(ea)) {
434                 return -EFAULT;
435         }
436 #endif /* XXX */
437         if (mm == NULL) {
438                 return -EFAULT;
439         }
440         if (mm->pgd == NULL) {
441                 return -EFAULT;
442         }
443
444         down_read(&mm->mmap_sem);
445         vma = find_vma(mm, ea);
446         if (!vma)
447                 goto bad_area;
448         if (vma->vm_start <= ea)
449                 goto good_area;
450         if (!(vma->vm_flags & VM_GROWSDOWN))
451                 goto bad_area;
452 #if 0
453         if (expand_stack(vma, ea))
454                 goto bad_area;
455 #endif /* XXX */
456 good_area:
457         is_write = dsisr & MFC_DSISR_ACCESS_PUT;
458         if (is_write) {
459                 if (!(vma->vm_flags & VM_WRITE))
460                         goto bad_area;
461         } else {
462                 if (dsisr & MFC_DSISR_ACCESS_DENIED)
463                         goto bad_area;
464                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
465                         goto bad_area;
466         }
467         ret = 0;
468         switch (handle_mm_fault(mm, vma, ea, is_write)) {
469         case VM_FAULT_MINOR:
470                 current->min_flt++;
471                 break;
472         case VM_FAULT_MAJOR:
473                 current->maj_flt++;
474                 break;
475         case VM_FAULT_SIGBUS:
476                 ret = -EFAULT;
477                 goto bad_area;
478         case VM_FAULT_OOM:
479                 ret = -ENOMEM;
480                 goto bad_area;
481         default:
482                 BUG();
483         }
484         up_read(&mm->mmap_sem);
485         return ret;
486
487 bad_area:
488         up_read(&mm->mmap_sem);
489         return -EFAULT;
490 }
491
492 int spu_irq_class_1_bottom(struct spu *spu)
493 {
494         u64 ea, dsisr, access, error = 0UL;
495         int ret = 0;
496
497         ea = spu->dar;
498         dsisr = spu->dsisr;
499         if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
500                 u64 flags;
501
502                 access = (_PAGE_PRESENT | _PAGE_USER);
503                 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
504                 local_irq_save(flags);
505                 if (hash_page(ea, access, 0x300) != 0)
506                         error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
507                 local_irq_restore(flags);
508         }
509         if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
510                 if ((ret = spu_handle_mm_fault(spu)) != 0)
511                         error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
512                 else
513                         error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
514         }
515         spu->dar = 0UL;
516         spu->dsisr = 0UL;
517         if (!error) {
518                 spu_restart_dma(spu);
519         } else {
520                 __spu_trap_invalid_dma(spu);
521         }
522         return ret;
523 }
524
525 static int __init find_spu_node_id(struct device_node *spe)
526 {
527         unsigned int *id;
528         struct device_node *cpu;
529         cpu = spe->parent->parent;
530         id = (unsigned int *)get_property(cpu, "node-id", NULL);
531         return id ? *id : 0;
532 }
533
534 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
535                 const char *prop)
536 {
537         static DEFINE_MUTEX(add_spumem_mutex);
538
539         struct address_prop {
540                 unsigned long address;
541                 unsigned int len;
542         } __attribute__((packed)) *p;
543         int proplen;
544
545         unsigned long start_pfn, nr_pages;
546         struct pglist_data *pgdata;
547         struct zone *zone;
548         int ret;
549
550         p = (void*)get_property(spe, prop, &proplen);
551         WARN_ON(proplen != sizeof (*p));
552
553         start_pfn = p->address >> PAGE_SHIFT;
554         nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
555
556         pgdata = NODE_DATA(spu->nid);
557         zone = pgdata->node_zones;
558
559         /* XXX rethink locking here */
560         mutex_lock(&add_spumem_mutex);
561         ret = __add_pages(zone, start_pfn, nr_pages);
562         mutex_unlock(&add_spumem_mutex);
563
564         return ret;
565 }
566
567 static void __iomem * __init map_spe_prop(struct spu *spu,
568                 struct device_node *n, const char *name)
569 {
570         struct address_prop {
571                 unsigned long address;
572                 unsigned int len;
573         } __attribute__((packed)) *prop;
574
575         void *p;
576         int proplen;
577         void* ret = NULL;
578         int err = 0;
579
580         p = get_property(n, name, &proplen);
581         if (proplen != sizeof (struct address_prop))
582                 return NULL;
583
584         prop = p;
585
586         err = cell_spuprop_present(spu, n, name);
587         if (err && (err != -EEXIST))
588                 goto out;
589
590         ret = ioremap(prop->address, prop->len);
591
592  out:
593         return ret;
594 }
595
596 static void spu_unmap(struct spu *spu)
597 {
598         iounmap(spu->priv2);
599         iounmap(spu->priv1);
600         iounmap(spu->problem);
601         iounmap((u8 __iomem *)spu->local_store);
602 }
603
604 static int __init spu_map_device(struct spu *spu, struct device_node *node)
605 {
606         char *prop;
607         int ret;
608
609         ret = -ENODEV;
610         prop = get_property(node, "isrc", NULL);
611         if (!prop)
612                 goto out;
613         spu->isrc = *(unsigned int *)prop;
614
615         spu->name = get_property(node, "name", NULL);
616         if (!spu->name)
617                 goto out;
618
619         prop = get_property(node, "local-store", NULL);
620         if (!prop)
621                 goto out;
622         spu->local_store_phys = *(unsigned long *)prop;
623
624         /* we use local store as ram, not io memory */
625         spu->local_store = (void __force *)
626                 map_spe_prop(spu, node, "local-store");
627         if (!spu->local_store)
628                 goto out;
629
630         prop = get_property(node, "problem", NULL);
631         if (!prop)
632                 goto out_unmap;
633         spu->problem_phys = *(unsigned long *)prop;
634
635         spu->problem= map_spe_prop(spu, node, "problem");
636         if (!spu->problem)
637                 goto out_unmap;
638
639         spu->priv1= map_spe_prop(spu, node, "priv1");
640         /* priv1 is not available on a hypervisor */
641
642         spu->priv2= map_spe_prop(spu, node, "priv2");
643         if (!spu->priv2)
644                 goto out_unmap;
645         ret = 0;
646         goto out;
647
648 out_unmap:
649         spu_unmap(spu);
650 out:
651         return ret;
652 }
653
654 struct sysdev_class spu_sysdev_class = {
655         set_kset_name("spu")
656 };
657
658 static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
659 {
660         struct spu *spu = container_of(sysdev, struct spu, sysdev);
661         return sprintf(buf, "%d\n", spu->isrc);
662
663 }
664 static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
665
666 extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
667
668 static int spu_create_sysdev(struct spu *spu)
669 {
670         int ret;
671
672         spu->sysdev.id = spu->number;
673         spu->sysdev.cls = &spu_sysdev_class;
674         ret = sysdev_register(&spu->sysdev);
675         if (ret) {
676                 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
677                                 spu->number);
678                 return ret;
679         }
680
681         sysdev_create_file(&spu->sysdev, &attr_isrc);
682         sysfs_add_device_to_node(&spu->sysdev, spu->nid);
683
684         return 0;
685 }
686
687 static void spu_destroy_sysdev(struct spu *spu)
688 {
689         sysdev_remove_file(&spu->sysdev, &attr_isrc);
690         sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
691         sysdev_unregister(&spu->sysdev);
692 }
693
694 static int __init create_spu(struct device_node *spe)
695 {
696         struct spu *spu;
697         int ret;
698         static int number;
699
700         ret = -ENOMEM;
701         spu = kzalloc(sizeof (*spu), GFP_KERNEL);
702         if (!spu)
703                 goto out;
704
705         ret = spu_map_device(spu, spe);
706         if (ret)
707                 goto out_free;
708
709         spu->node = find_spu_node_id(spe);
710         spu->nid = of_node_to_nid(spe);
711         if (spu->nid == -1)
712                 spu->nid = 0;
713         spin_lock_init(&spu->register_lock);
714         spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
715         spu_mfc_sr1_set(spu, 0x33);
716         mutex_lock(&spu_mutex);
717
718         spu->number = number++;
719         ret = spu_request_irqs(spu);
720         if (ret)
721                 goto out_unmap;
722
723         ret = spu_create_sysdev(spu);
724         if (ret)
725                 goto out_free_irqs;
726
727         list_add(&spu->list, &spu_list);
728         mutex_unlock(&spu_mutex);
729
730         pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
731                 spu->name, spu->isrc, spu->local_store,
732                 spu->problem, spu->priv1, spu->priv2, spu->number);
733         goto out;
734
735 out_free_irqs:
736         spu_free_irqs(spu);
737
738 out_unmap:
739         mutex_unlock(&spu_mutex);
740         spu_unmap(spu);
741 out_free:
742         kfree(spu);
743 out:
744         return ret;
745 }
746
747 static void destroy_spu(struct spu *spu)
748 {
749         list_del_init(&spu->list);
750
751         spu_destroy_sysdev(spu);
752         spu_free_irqs(spu);
753         spu_unmap(spu);
754         kfree(spu);
755 }
756
757 static void cleanup_spu_base(void)
758 {
759         struct spu *spu, *tmp;
760         mutex_lock(&spu_mutex);
761         list_for_each_entry_safe(spu, tmp, &spu_list, list)
762                 destroy_spu(spu);
763         mutex_unlock(&spu_mutex);
764         sysdev_class_unregister(&spu_sysdev_class);
765 }
766 module_exit(cleanup_spu_base);
767
768 static int __init init_spu_base(void)
769 {
770         struct device_node *node;
771         int ret;
772
773         /* create sysdev class for spus */
774         ret = sysdev_class_register(&spu_sysdev_class);
775         if (ret)
776                 return ret;
777
778         ret = -ENODEV;
779         for (node = of_find_node_by_type(NULL, "spe");
780                         node; node = of_find_node_by_type(node, "spe")) {
781                 ret = create_spu(node);
782                 if (ret) {
783                         printk(KERN_WARNING "%s: Error initializing %s\n",
784                                 __FUNCTION__, node->name);
785                         cleanup_spu_base();
786                         break;
787                 }
788         }
789         /* in some old firmware versions, the spe is called 'spc', so we
790            look for that as well */
791         for (node = of_find_node_by_type(NULL, "spc");
792                         node; node = of_find_node_by_type(node, "spc")) {
793                 ret = create_spu(node);
794                 if (ret) {
795                         printk(KERN_WARNING "%s: Error initializing %s\n",
796                                 __FUNCTION__, node->name);
797                         cleanup_spu_base();
798                         break;
799                 }
800         }
801         return ret;
802 }
803 module_init(init_spu_base);
804
805 MODULE_LICENSE("GPL");
806 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");