[POWERPC] spufs: further abstract priv1 register access
[safe/jmp/linux-2.6] / 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 void spu_irq_setaffinity(struct spu *spu, int cpu)
526 {
527         u64 target = iic_get_target_id(cpu);
528         u64 route = target << 48 | target << 32 | target << 16;
529         spu_int_route_set(spu, route);
530 }
531 EXPORT_SYMBOL_GPL(spu_irq_setaffinity);
532
533 static int __init find_spu_node_id(struct device_node *spe)
534 {
535         unsigned int *id;
536         struct device_node *cpu;
537         cpu = spe->parent->parent;
538         id = (unsigned int *)get_property(cpu, "node-id", NULL);
539         return id ? *id : 0;
540 }
541
542 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
543                 const char *prop)
544 {
545         static DEFINE_MUTEX(add_spumem_mutex);
546
547         struct address_prop {
548                 unsigned long address;
549                 unsigned int len;
550         } __attribute__((packed)) *p;
551         int proplen;
552
553         unsigned long start_pfn, nr_pages;
554         struct pglist_data *pgdata;
555         struct zone *zone;
556         int ret;
557
558         p = (void*)get_property(spe, prop, &proplen);
559         WARN_ON(proplen != sizeof (*p));
560
561         start_pfn = p->address >> PAGE_SHIFT;
562         nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
563
564         pgdata = NODE_DATA(spu->nid);
565         zone = pgdata->node_zones;
566
567         /* XXX rethink locking here */
568         mutex_lock(&add_spumem_mutex);
569         ret = __add_pages(zone, start_pfn, nr_pages);
570         mutex_unlock(&add_spumem_mutex);
571
572         return ret;
573 }
574
575 static void __iomem * __init map_spe_prop(struct spu *spu,
576                 struct device_node *n, const char *name)
577 {
578         struct address_prop {
579                 unsigned long address;
580                 unsigned int len;
581         } __attribute__((packed)) *prop;
582
583         void *p;
584         int proplen;
585         void* ret = NULL;
586         int err = 0;
587
588         p = get_property(n, name, &proplen);
589         if (proplen != sizeof (struct address_prop))
590                 return NULL;
591
592         prop = p;
593
594         err = cell_spuprop_present(spu, n, name);
595         if (err && (err != -EEXIST))
596                 goto out;
597
598         ret = ioremap(prop->address, prop->len);
599
600  out:
601         return ret;
602 }
603
604 static void spu_unmap(struct spu *spu)
605 {
606         iounmap(spu->priv2);
607         iounmap(spu->priv1);
608         iounmap(spu->problem);
609         iounmap((u8 __iomem *)spu->local_store);
610 }
611
612 static int __init spu_map_device(struct spu *spu, struct device_node *node)
613 {
614         char *prop;
615         int ret;
616
617         ret = -ENODEV;
618         prop = get_property(node, "isrc", NULL);
619         if (!prop)
620                 goto out;
621         spu->isrc = *(unsigned int *)prop;
622
623         spu->name = get_property(node, "name", NULL);
624         if (!spu->name)
625                 goto out;
626
627         prop = get_property(node, "local-store", NULL);
628         if (!prop)
629                 goto out;
630         spu->local_store_phys = *(unsigned long *)prop;
631
632         /* we use local store as ram, not io memory */
633         spu->local_store = (void __force *)
634                 map_spe_prop(spu, node, "local-store");
635         if (!spu->local_store)
636                 goto out;
637
638         prop = get_property(node, "problem", NULL);
639         if (!prop)
640                 goto out_unmap;
641         spu->problem_phys = *(unsigned long *)prop;
642
643         spu->problem= map_spe_prop(spu, node, "problem");
644         if (!spu->problem)
645                 goto out_unmap;
646
647         spu->priv1= map_spe_prop(spu, node, "priv1");
648         /* priv1 is not available on a hypervisor */
649
650         spu->priv2= map_spe_prop(spu, node, "priv2");
651         if (!spu->priv2)
652                 goto out_unmap;
653         ret = 0;
654         goto out;
655
656 out_unmap:
657         spu_unmap(spu);
658 out:
659         return ret;
660 }
661
662 struct sysdev_class spu_sysdev_class = {
663         set_kset_name("spu")
664 };
665
666 static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
667 {
668         struct spu *spu = container_of(sysdev, struct spu, sysdev);
669         return sprintf(buf, "%d\n", spu->isrc);
670
671 }
672 static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
673
674 extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
675
676 static int spu_create_sysdev(struct spu *spu)
677 {
678         int ret;
679
680         spu->sysdev.id = spu->number;
681         spu->sysdev.cls = &spu_sysdev_class;
682         ret = sysdev_register(&spu->sysdev);
683         if (ret) {
684                 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
685                                 spu->number);
686                 return ret;
687         }
688
689         sysdev_create_file(&spu->sysdev, &attr_isrc);
690         sysfs_add_device_to_node(&spu->sysdev, spu->nid);
691
692         return 0;
693 }
694
695 static void spu_destroy_sysdev(struct spu *spu)
696 {
697         sysdev_remove_file(&spu->sysdev, &attr_isrc);
698         sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
699         sysdev_unregister(&spu->sysdev);
700 }
701
702 static int __init create_spu(struct device_node *spe)
703 {
704         struct spu *spu;
705         int ret;
706         static int number;
707
708         ret = -ENOMEM;
709         spu = kzalloc(sizeof (*spu), GFP_KERNEL);
710         if (!spu)
711                 goto out;
712
713         ret = spu_map_device(spu, spe);
714         if (ret)
715                 goto out_free;
716
717         spu->node = find_spu_node_id(spe);
718         spu->nid = of_node_to_nid(spe);
719         if (spu->nid == -1)
720                 spu->nid = 0;
721         spin_lock_init(&spu->register_lock);
722         spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
723         spu_mfc_sr1_set(spu, 0x33);
724         mutex_lock(&spu_mutex);
725
726         spu->number = number++;
727         ret = spu_request_irqs(spu);
728         if (ret)
729                 goto out_unmap;
730
731         ret = spu_create_sysdev(spu);
732         if (ret)
733                 goto out_free_irqs;
734
735         list_add(&spu->list, &spu_list);
736         mutex_unlock(&spu_mutex);
737
738         pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
739                 spu->name, spu->isrc, spu->local_store,
740                 spu->problem, spu->priv1, spu->priv2, spu->number);
741         goto out;
742
743 out_free_irqs:
744         spu_free_irqs(spu);
745
746 out_unmap:
747         mutex_unlock(&spu_mutex);
748         spu_unmap(spu);
749 out_free:
750         kfree(spu);
751 out:
752         return ret;
753 }
754
755 static void destroy_spu(struct spu *spu)
756 {
757         list_del_init(&spu->list);
758
759         spu_destroy_sysdev(spu);
760         spu_free_irqs(spu);
761         spu_unmap(spu);
762         kfree(spu);
763 }
764
765 static void cleanup_spu_base(void)
766 {
767         struct spu *spu, *tmp;
768         mutex_lock(&spu_mutex);
769         list_for_each_entry_safe(spu, tmp, &spu_list, list)
770                 destroy_spu(spu);
771         mutex_unlock(&spu_mutex);
772         sysdev_class_unregister(&spu_sysdev_class);
773 }
774 module_exit(cleanup_spu_base);
775
776 static int __init init_spu_base(void)
777 {
778         struct device_node *node;
779         int ret;
780
781         /* create sysdev class for spus */
782         ret = sysdev_class_register(&spu_sysdev_class);
783         if (ret)
784                 return ret;
785
786         ret = -ENODEV;
787         for (node = of_find_node_by_type(NULL, "spe");
788                         node; node = of_find_node_by_type(node, "spe")) {
789                 ret = create_spu(node);
790                 if (ret) {
791                         printk(KERN_WARNING "%s: Error initializing %s\n",
792                                 __FUNCTION__, node->name);
793                         cleanup_spu_base();
794                         break;
795                 }
796         }
797         /* in some old firmware versions, the spe is called 'spc', so we
798            look for that as well */
799         for (node = of_find_node_by_type(NULL, "spc");
800                         node; node = of_find_node_by_type(node, "spc")) {
801                 ret = create_spu(node);
802                 if (ret) {
803                         printk(KERN_WARNING "%s: Error initializing %s\n",
804                                 __FUNCTION__, node->name);
805                         cleanup_spu_base();
806                         break;
807                 }
808         }
809         return ret;
810 }
811 module_init(init_spu_base);
812
813 MODULE_LICENSE("GPL");
814 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");