Merge branch 'linux-2.6' into for-linus
[sfrench/cifs-2.6.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/pci.h>
29 #include <linux/poll.h>
30 #include <linux/ptrace.h>
31 #include <linux/slab.h>
32 #include <linux/wait.h>
33
34 #include <asm/firmware.h>
35 #include <asm/io.h>
36 #include <asm/prom.h>
37 #include <linux/mutex.h>
38 #include <asm/spu.h>
39 #include <asm/spu_priv1.h>
40 #include <asm/mmu_context.h>
41 #include <asm/xmon.h>
42
43 #include "interrupt.h"
44
45 const struct spu_priv1_ops *spu_priv1_ops;
46
47 EXPORT_SYMBOL_GPL(spu_priv1_ops);
48
49 static int __spu_trap_invalid_dma(struct spu *spu)
50 {
51         pr_debug("%s\n", __FUNCTION__);
52         spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
53         return 0;
54 }
55
56 static int __spu_trap_dma_align(struct spu *spu)
57 {
58         pr_debug("%s\n", __FUNCTION__);
59         spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
60         return 0;
61 }
62
63 static int __spu_trap_error(struct spu *spu)
64 {
65         pr_debug("%s\n", __FUNCTION__);
66         spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
67         return 0;
68 }
69
70 static void spu_restart_dma(struct spu *spu)
71 {
72         struct spu_priv2 __iomem *priv2 = spu->priv2;
73
74         if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
75                 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
76 }
77
78 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
79 {
80         struct spu_priv2 __iomem *priv2 = spu->priv2;
81         struct mm_struct *mm = spu->mm;
82         u64 esid, vsid, llp;
83
84         pr_debug("%s\n", __FUNCTION__);
85
86         if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
87                 /* SLBs are pre-loaded for context switch, so
88                  * we should never get here!
89                  */
90                 printk("%s: invalid access during switch!\n", __func__);
91                 return 1;
92         }
93         esid = (ea & ESID_MASK) | SLB_ESID_V;
94
95         switch(REGION_ID(ea)) {
96         case USER_REGION_ID:
97 #ifdef CONFIG_HUGETLB_PAGE
98                 if (in_hugepage_area(mm->context, ea))
99                         llp = mmu_psize_defs[mmu_huge_psize].sllp;
100                 else
101 #endif
102                         llp = mmu_psize_defs[mmu_virtual_psize].sllp;
103                 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
104                                 SLB_VSID_USER | llp;
105                 break;
106         case VMALLOC_REGION_ID:
107                 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
108                 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
109                         SLB_VSID_KERNEL | llp;
110                 break;
111         case KERNEL_REGION_ID:
112                 llp = mmu_psize_defs[mmu_linear_psize].sllp;
113                 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
114                         SLB_VSID_KERNEL | llp;
115                 break;
116         default:
117                 /* Future: support kernel segments so that drivers
118                  * can use SPUs.
119                  */
120                 pr_debug("invalid region access at %016lx\n", ea);
121                 return 1;
122         }
123
124         out_be64(&priv2->slb_index_W, spu->slb_replace);
125         out_be64(&priv2->slb_vsid_RW, vsid);
126         out_be64(&priv2->slb_esid_RW, esid);
127
128         spu->slb_replace++;
129         if (spu->slb_replace >= 8)
130                 spu->slb_replace = 0;
131
132         spu_restart_dma(spu);
133
134         return 0;
135 }
136
137 extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
138 static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
139 {
140         pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
141
142         /* Handle kernel space hash faults immediately.
143            User hash faults need to be deferred to process context. */
144         if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
145             && REGION_ID(ea) != USER_REGION_ID
146             && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
147                 spu_restart_dma(spu);
148                 return 0;
149         }
150
151         if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
152                 printk("%s: invalid access during switch!\n", __func__);
153                 return 1;
154         }
155
156         spu->dar = ea;
157         spu->dsisr = dsisr;
158         mb();
159         spu->stop_callback(spu);
160         return 0;
161 }
162
163 static irqreturn_t
164 spu_irq_class_0(int irq, void *data)
165 {
166         struct spu *spu;
167
168         spu = data;
169         spu->class_0_pending = 1;
170         spu->stop_callback(spu);
171
172         return IRQ_HANDLED;
173 }
174
175 int
176 spu_irq_class_0_bottom(struct spu *spu)
177 {
178         unsigned long stat, mask;
179
180         spu->class_0_pending = 0;
181
182         mask = spu_int_mask_get(spu, 0);
183         stat = spu_int_stat_get(spu, 0);
184
185         stat &= mask;
186
187         if (stat & 1) /* invalid DMA alignment */
188                 __spu_trap_dma_align(spu);
189
190         if (stat & 2) /* invalid MFC DMA */
191                 __spu_trap_invalid_dma(spu);
192
193         if (stat & 4) /* error on SPU */
194                 __spu_trap_error(spu);
195
196         spu_int_stat_clear(spu, 0, stat);
197
198         return (stat & 0x7) ? -EIO : 0;
199 }
200 EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
201
202 static irqreturn_t
203 spu_irq_class_1(int irq, void *data)
204 {
205         struct spu *spu;
206         unsigned long stat, mask, dar, dsisr;
207
208         spu = data;
209
210         /* atomically read & clear class1 status. */
211         spin_lock(&spu->register_lock);
212         mask  = spu_int_mask_get(spu, 1);
213         stat  = spu_int_stat_get(spu, 1) & mask;
214         dar   = spu_mfc_dar_get(spu);
215         dsisr = spu_mfc_dsisr_get(spu);
216         if (stat & 2) /* mapping fault */
217                 spu_mfc_dsisr_set(spu, 0ul);
218         spu_int_stat_clear(spu, 1, stat);
219         spin_unlock(&spu->register_lock);
220         pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
221                         dar, dsisr);
222
223         if (stat & 1) /* segment fault */
224                 __spu_trap_data_seg(spu, dar);
225
226         if (stat & 2) { /* mapping fault */
227                 __spu_trap_data_map(spu, dar, dsisr);
228         }
229
230         if (stat & 4) /* ls compare & suspend on get */
231                 ;
232
233         if (stat & 8) /* ls compare & suspend on put */
234                 ;
235
236         return stat ? IRQ_HANDLED : IRQ_NONE;
237 }
238 EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
239
240 static irqreturn_t
241 spu_irq_class_2(int irq, void *data)
242 {
243         struct spu *spu;
244         unsigned long stat;
245         unsigned long mask;
246
247         spu = data;
248         spin_lock(&spu->register_lock);
249         stat = spu_int_stat_get(spu, 2);
250         mask = spu_int_mask_get(spu, 2);
251         /* ignore interrupts we're not waiting for */
252         stat &= mask;
253         /*
254          * mailbox interrupts (0x1 and 0x10) are level triggered.
255          * mask them now before acknowledging.
256          */
257         if (stat & 0x11)
258                 spu_int_mask_and(spu, 2, ~(stat & 0x11));
259         /* acknowledge all interrupts before the callbacks */
260         spu_int_stat_clear(spu, 2, stat);
261         spin_unlock(&spu->register_lock);
262
263         pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
264
265         if (stat & 1)  /* PPC core mailbox */
266                 spu->ibox_callback(spu);
267
268         if (stat & 2) /* SPU stop-and-signal */
269                 spu->stop_callback(spu);
270
271         if (stat & 4) /* SPU halted */
272                 spu->stop_callback(spu);
273
274         if (stat & 8) /* DMA tag group complete */
275                 spu->mfc_callback(spu);
276
277         if (stat & 0x10) /* SPU mailbox threshold */
278                 spu->wbox_callback(spu);
279
280         return stat ? IRQ_HANDLED : IRQ_NONE;
281 }
282
283 static int spu_request_irqs(struct spu *spu)
284 {
285         int ret = 0;
286
287         if (spu->irqs[0] != NO_IRQ) {
288                 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
289                          spu->number);
290                 ret = request_irq(spu->irqs[0], spu_irq_class_0,
291                                   IRQF_DISABLED,
292                                   spu->irq_c0, spu);
293                 if (ret)
294                         goto bail0;
295         }
296         if (spu->irqs[1] != NO_IRQ) {
297                 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
298                          spu->number);
299                 ret = request_irq(spu->irqs[1], spu_irq_class_1,
300                                   IRQF_DISABLED,
301                                   spu->irq_c1, spu);
302                 if (ret)
303                         goto bail1;
304         }
305         if (spu->irqs[2] != NO_IRQ) {
306                 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
307                          spu->number);
308                 ret = request_irq(spu->irqs[2], spu_irq_class_2,
309                                   IRQF_DISABLED,
310                                   spu->irq_c2, spu);
311                 if (ret)
312                         goto bail2;
313         }
314         return 0;
315
316 bail2:
317         if (spu->irqs[1] != NO_IRQ)
318                 free_irq(spu->irqs[1], spu);
319 bail1:
320         if (spu->irqs[0] != NO_IRQ)
321                 free_irq(spu->irqs[0], spu);
322 bail0:
323         return ret;
324 }
325
326 static void spu_free_irqs(struct spu *spu)
327 {
328         if (spu->irqs[0] != NO_IRQ)
329                 free_irq(spu->irqs[0], spu);
330         if (spu->irqs[1] != NO_IRQ)
331                 free_irq(spu->irqs[1], spu);
332         if (spu->irqs[2] != NO_IRQ)
333                 free_irq(spu->irqs[2], spu);
334 }
335
336 static struct list_head spu_list[MAX_NUMNODES];
337 static LIST_HEAD(spu_full_list);
338 static DEFINE_MUTEX(spu_mutex);
339
340 static void spu_init_channels(struct spu *spu)
341 {
342         static const struct {
343                  unsigned channel;
344                  unsigned count;
345         } zero_list[] = {
346                 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
347                 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
348         }, count_list[] = {
349                 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
350                 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
351                 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
352         };
353         struct spu_priv2 __iomem *priv2;
354         int i;
355
356         priv2 = spu->priv2;
357
358         /* initialize all channel data to zero */
359         for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
360                 int count;
361
362                 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
363                 for (count = 0; count < zero_list[i].count; count++)
364                         out_be64(&priv2->spu_chnldata_RW, 0);
365         }
366
367         /* initialize channel counts to meaningful values */
368         for (i = 0; i < ARRAY_SIZE(count_list); i++) {
369                 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
370                 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
371         }
372 }
373
374 struct spu *spu_alloc_node(int node)
375 {
376         struct spu *spu = NULL;
377
378         mutex_lock(&spu_mutex);
379         if (!list_empty(&spu_list[node])) {
380                 spu = list_entry(spu_list[node].next, struct spu, list);
381                 list_del_init(&spu->list);
382                 pr_debug("Got SPU %d %d\n", spu->number, spu->node);
383                 spu_init_channels(spu);
384         }
385         mutex_unlock(&spu_mutex);
386
387         return spu;
388 }
389 EXPORT_SYMBOL_GPL(spu_alloc_node);
390
391 struct spu *spu_alloc(void)
392 {
393         struct spu *spu = NULL;
394         int node;
395
396         for (node = 0; node < MAX_NUMNODES; node++) {
397                 spu = spu_alloc_node(node);
398                 if (spu)
399                         break;
400         }
401
402         return spu;
403 }
404
405 void spu_free(struct spu *spu)
406 {
407         mutex_lock(&spu_mutex);
408         list_add_tail(&spu->list, &spu_list[spu->node]);
409         mutex_unlock(&spu_mutex);
410 }
411 EXPORT_SYMBOL_GPL(spu_free);
412
413 static int spu_handle_mm_fault(struct spu *spu)
414 {
415         struct mm_struct *mm = spu->mm;
416         struct vm_area_struct *vma;
417         u64 ea, dsisr, is_write;
418         int ret;
419
420         ea = spu->dar;
421         dsisr = spu->dsisr;
422 #if 0
423         if (!IS_VALID_EA(ea)) {
424                 return -EFAULT;
425         }
426 #endif /* XXX */
427         if (mm == NULL) {
428                 return -EFAULT;
429         }
430         if (mm->pgd == NULL) {
431                 return -EFAULT;
432         }
433
434         down_read(&mm->mmap_sem);
435         vma = find_vma(mm, ea);
436         if (!vma)
437                 goto bad_area;
438         if (vma->vm_start <= ea)
439                 goto good_area;
440         if (!(vma->vm_flags & VM_GROWSDOWN))
441                 goto bad_area;
442 #if 0
443         if (expand_stack(vma, ea))
444                 goto bad_area;
445 #endif /* XXX */
446 good_area:
447         is_write = dsisr & MFC_DSISR_ACCESS_PUT;
448         if (is_write) {
449                 if (!(vma->vm_flags & VM_WRITE))
450                         goto bad_area;
451         } else {
452                 if (dsisr & MFC_DSISR_ACCESS_DENIED)
453                         goto bad_area;
454                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
455                         goto bad_area;
456         }
457         ret = 0;
458         switch (handle_mm_fault(mm, vma, ea, is_write)) {
459         case VM_FAULT_MINOR:
460                 current->min_flt++;
461                 break;
462         case VM_FAULT_MAJOR:
463                 current->maj_flt++;
464                 break;
465         case VM_FAULT_SIGBUS:
466                 ret = -EFAULT;
467                 goto bad_area;
468         case VM_FAULT_OOM:
469                 ret = -ENOMEM;
470                 goto bad_area;
471         default:
472                 BUG();
473         }
474         up_read(&mm->mmap_sem);
475         return ret;
476
477 bad_area:
478         up_read(&mm->mmap_sem);
479         return -EFAULT;
480 }
481
482 int spu_irq_class_1_bottom(struct spu *spu)
483 {
484         u64 ea, dsisr, access, error = 0UL;
485         int ret = 0;
486
487         ea = spu->dar;
488         dsisr = spu->dsisr;
489         if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
490                 u64 flags;
491
492                 access = (_PAGE_PRESENT | _PAGE_USER);
493                 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
494                 local_irq_save(flags);
495                 if (hash_page(ea, access, 0x300) != 0)
496                         error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
497                 local_irq_restore(flags);
498         }
499         if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
500                 if ((ret = spu_handle_mm_fault(spu)) != 0)
501                         error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
502                 else
503                         error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
504         }
505         spu->dar = 0UL;
506         spu->dsisr = 0UL;
507         if (!error) {
508                 spu_restart_dma(spu);
509         } else {
510                 __spu_trap_invalid_dma(spu);
511         }
512         return ret;
513 }
514
515 static int __init find_spu_node_id(struct device_node *spe)
516 {
517         const unsigned int *id;
518         struct device_node *cpu;
519         cpu = spe->parent->parent;
520         id = get_property(cpu, "node-id", NULL);
521         return id ? *id : 0;
522 }
523
524 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
525                 const char *prop)
526 {
527         static DEFINE_MUTEX(add_spumem_mutex);
528
529         const struct address_prop {
530                 unsigned long address;
531                 unsigned int len;
532         } __attribute__((packed)) *p;
533         int proplen;
534
535         unsigned long start_pfn, nr_pages;
536         struct pglist_data *pgdata;
537         struct zone *zone;
538         int ret;
539
540         p = get_property(spe, prop, &proplen);
541         WARN_ON(proplen != sizeof (*p));
542
543         start_pfn = p->address >> PAGE_SHIFT;
544         nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
545
546         pgdata = NODE_DATA(spu->nid);
547         zone = pgdata->node_zones;
548
549         /* XXX rethink locking here */
550         mutex_lock(&add_spumem_mutex);
551         ret = __add_pages(zone, start_pfn, nr_pages);
552         mutex_unlock(&add_spumem_mutex);
553
554         return ret;
555 }
556
557 static void __iomem * __init map_spe_prop(struct spu *spu,
558                 struct device_node *n, const char *name)
559 {
560         const struct address_prop {
561                 unsigned long address;
562                 unsigned int len;
563         } __attribute__((packed)) *prop;
564
565         const void *p;
566         int proplen;
567         void __iomem *ret = NULL;
568         int err = 0;
569
570         p = get_property(n, name, &proplen);
571         if (proplen != sizeof (struct address_prop))
572                 return NULL;
573
574         prop = p;
575
576         err = cell_spuprop_present(spu, n, name);
577         if (err && (err != -EEXIST))
578                 goto out;
579
580         ret = ioremap(prop->address, prop->len);
581
582  out:
583         return ret;
584 }
585
586 static void spu_unmap(struct spu *spu)
587 {
588         iounmap(spu->priv2);
589         iounmap(spu->priv1);
590         iounmap(spu->problem);
591         iounmap((__force u8 __iomem *)spu->local_store);
592 }
593
594 /* This function shall be abstracted for HV platforms */
595 static int __init spu_map_interrupts_old(struct spu *spu, struct device_node *np)
596 {
597         unsigned int isrc;
598         const u32 *tmp;
599
600         /* Get the interrupt source unit from the device-tree */
601         tmp = get_property(np, "isrc", NULL);
602         if (!tmp)
603                 return -ENODEV;
604         isrc = tmp[0];
605
606         /* Add the node number */
607         isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
608
609         /* Now map interrupts of all 3 classes */
610         spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
611         spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
612         spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
613
614         /* Right now, we only fail if class 2 failed */
615         return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
616 }
617
618 static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
619 {
620         const char *prop;
621         int ret;
622
623         ret = -ENODEV;
624         spu->name = get_property(node, "name", NULL);
625         if (!spu->name)
626                 goto out;
627
628         prop = get_property(node, "local-store", NULL);
629         if (!prop)
630                 goto out;
631         spu->local_store_phys = *(unsigned long *)prop;
632
633         /* we use local store as ram, not io memory */
634         spu->local_store = (void __force *)
635                 map_spe_prop(spu, node, "local-store");
636         if (!spu->local_store)
637                 goto out;
638
639         prop = get_property(node, "problem", NULL);
640         if (!prop)
641                 goto out_unmap;
642         spu->problem_phys = *(unsigned long *)prop;
643
644         spu->problem= map_spe_prop(spu, node, "problem");
645         if (!spu->problem)
646                 goto out_unmap;
647
648         spu->priv1= map_spe_prop(spu, node, "priv1");
649         /* priv1 is not available on a hypervisor */
650
651         spu->priv2= map_spe_prop(spu, node, "priv2");
652         if (!spu->priv2)
653                 goto out_unmap;
654         ret = 0;
655         goto out;
656
657 out_unmap:
658         spu_unmap(spu);
659 out:
660         return ret;
661 }
662
663 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
664 {
665         struct of_irq oirq;
666         int ret;
667         int i;
668
669         for (i=0; i < 3; i++) {
670                 ret = of_irq_map_one(np, i, &oirq);
671                 if (ret) {
672                         pr_debug("spu_new: failed to get irq %d\n", i);
673                         goto err;
674                 }
675                 ret = -EINVAL;
676                 pr_debug("  irq %d no 0x%x on %s\n", i, oirq.specifier[0],
677                          oirq.controller->full_name);
678                 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
679                                         oirq.specifier, oirq.size);
680                 if (spu->irqs[i] == NO_IRQ) {
681                         pr_debug("spu_new: failed to map it !\n");
682                         goto err;
683                 }
684         }
685         return 0;
686
687 err:
688         pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier, spu->name);
689         for (; i >= 0; i--) {
690                 if (spu->irqs[i] != NO_IRQ)
691                         irq_dispose_mapping(spu->irqs[i]);
692         }
693         return ret;
694 }
695
696 static int spu_map_resource(struct device_node *node, int nr,
697                 void __iomem** virt, unsigned long *phys)
698 {
699         struct resource resource = { };
700         int ret;
701
702         ret = of_address_to_resource(node, nr, &resource);
703         if (ret)
704                 goto out;
705
706         if (phys)
707                 *phys = resource.start;
708         *virt = ioremap(resource.start, resource.end - resource.start);
709         if (!*virt)
710                 ret = -EINVAL;
711
712 out:
713         return ret;
714 }
715
716 static int __init spu_map_device(struct spu *spu, struct device_node *node)
717 {
718         int ret = -ENODEV;
719         spu->name = get_property(node, "name", NULL);
720         if (!spu->name)
721                 goto out;
722
723         ret = spu_map_resource(node, 0, (void __iomem**)&spu->local_store,
724                                         &spu->local_store_phys);
725         if (ret) {
726                 pr_debug("spu_new: failed to map %s resource 0\n",
727                          node->full_name);
728                 goto out;
729         }
730         ret = spu_map_resource(node, 1, (void __iomem**)&spu->problem,
731                                         &spu->problem_phys);
732         if (ret) {
733                 pr_debug("spu_new: failed to map %s resource 1\n",
734                          node->full_name);
735                 goto out_unmap;
736         }
737         ret = spu_map_resource(node, 2, (void __iomem**)&spu->priv2,
738                                         NULL);
739         if (ret) {
740                 pr_debug("spu_new: failed to map %s resource 2\n",
741                          node->full_name);
742                 goto out_unmap;
743         }
744
745         if (!firmware_has_feature(FW_FEATURE_LPAR))
746                 ret = spu_map_resource(node, 3, (void __iomem**)&spu->priv1,
747                                         NULL);
748         if (ret) {
749                 pr_debug("spu_new: failed to map %s resource 3\n",
750                          node->full_name);
751                 goto out_unmap;
752         }
753         pr_debug("spu_new: %s maps:\n", node->full_name);
754         pr_debug("  local store   : 0x%016lx -> 0x%p\n",
755                  spu->local_store_phys, spu->local_store);
756         pr_debug("  problem state : 0x%016lx -> 0x%p\n",
757                  spu->problem_phys, spu->problem);
758         pr_debug("  priv2         :                       0x%p\n", spu->priv2);
759         pr_debug("  priv1         :                       0x%p\n", spu->priv1);
760
761         return 0;
762
763 out_unmap:
764         spu_unmap(spu);
765 out:
766         pr_debug("failed to map spe %s: %d\n", spu->name, ret);
767         return ret;
768 }
769
770 struct sysdev_class spu_sysdev_class = {
771         set_kset_name("spu")
772 };
773
774 int spu_add_sysdev_attr(struct sysdev_attribute *attr)
775 {
776         struct spu *spu;
777         mutex_lock(&spu_mutex);
778
779         list_for_each_entry(spu, &spu_full_list, full_list)
780                 sysdev_create_file(&spu->sysdev, attr);
781
782         mutex_unlock(&spu_mutex);
783         return 0;
784 }
785 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
786
787 int spu_add_sysdev_attr_group(struct attribute_group *attrs)
788 {
789         struct spu *spu;
790         mutex_lock(&spu_mutex);
791
792         list_for_each_entry(spu, &spu_full_list, full_list)
793                 sysfs_create_group(&spu->sysdev.kobj, attrs);
794
795         mutex_unlock(&spu_mutex);
796         return 0;
797 }
798 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
799
800
801 void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
802 {
803         struct spu *spu;
804         mutex_lock(&spu_mutex);
805
806         list_for_each_entry(spu, &spu_full_list, full_list)
807                 sysdev_remove_file(&spu->sysdev, attr);
808
809         mutex_unlock(&spu_mutex);
810 }
811 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
812
813 void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
814 {
815         struct spu *spu;
816         mutex_lock(&spu_mutex);
817
818         list_for_each_entry(spu, &spu_full_list, full_list)
819                 sysfs_remove_group(&spu->sysdev.kobj, attrs);
820
821         mutex_unlock(&spu_mutex);
822 }
823 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
824
825 static int spu_create_sysdev(struct spu *spu)
826 {
827         int ret;
828
829         spu->sysdev.id = spu->number;
830         spu->sysdev.cls = &spu_sysdev_class;
831         ret = sysdev_register(&spu->sysdev);
832         if (ret) {
833                 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
834                                 spu->number);
835                 return ret;
836         }
837
838         sysfs_add_device_to_node(&spu->sysdev, spu->nid);
839
840         return 0;
841 }
842
843 static void spu_destroy_sysdev(struct spu *spu)
844 {
845         sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
846         sysdev_unregister(&spu->sysdev);
847 }
848
849 static int __init create_spu(struct device_node *spe)
850 {
851         struct spu *spu;
852         int ret;
853         static int number;
854
855         ret = -ENOMEM;
856         spu = kzalloc(sizeof (*spu), GFP_KERNEL);
857         if (!spu)
858                 goto out;
859
860         spu->node = find_spu_node_id(spe);
861         if (spu->node >= MAX_NUMNODES) {
862                 printk(KERN_WARNING "SPE %s on node %d ignored,"
863                        " node number too big\n", spe->full_name, spu->node);
864                 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
865                 return -ENODEV;
866         }
867         spu->nid = of_node_to_nid(spe);
868         if (spu->nid == -1)
869                 spu->nid = 0;
870
871         ret = spu_map_device(spu, spe);
872         /* try old method */
873         if (ret)
874                 ret = spu_map_device_old(spu, spe);
875         if (ret)
876                 goto out_free;
877
878         ret = spu_map_interrupts(spu, spe);
879         if (ret)
880                 ret = spu_map_interrupts_old(spu, spe);
881         if (ret)
882                 goto out_unmap;
883         spin_lock_init(&spu->register_lock);
884         spu_mfc_sdr_setup(spu);
885         spu_mfc_sr1_set(spu, 0x33);
886         mutex_lock(&spu_mutex);
887
888         spu->number = number++;
889         ret = spu_request_irqs(spu);
890         if (ret)
891                 goto out_unlock;
892
893         ret = spu_create_sysdev(spu);
894         if (ret)
895                 goto out_free_irqs;
896
897         list_add(&spu->list, &spu_list[spu->node]);
898         list_add(&spu->full_list, &spu_full_list);
899         spu->devnode = of_node_get(spe);
900
901         mutex_unlock(&spu_mutex);
902
903         pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n",
904                 spu->name, spu->local_store,
905                 spu->problem, spu->priv1, spu->priv2, spu->number);
906         goto out;
907
908 out_free_irqs:
909         spu_free_irqs(spu);
910 out_unlock:
911         mutex_unlock(&spu_mutex);
912 out_unmap:
913         spu_unmap(spu);
914 out_free:
915         kfree(spu);
916 out:
917         return ret;
918 }
919
920 static void destroy_spu(struct spu *spu)
921 {
922         list_del_init(&spu->list);
923         list_del_init(&spu->full_list);
924
925         of_node_put(spu->devnode);
926
927         spu_destroy_sysdev(spu);
928         spu_free_irqs(spu);
929         spu_unmap(spu);
930         kfree(spu);
931 }
932
933 static void cleanup_spu_base(void)
934 {
935         struct spu *spu, *tmp;
936         int node;
937
938         mutex_lock(&spu_mutex);
939         for (node = 0; node < MAX_NUMNODES; node++) {
940                 list_for_each_entry_safe(spu, tmp, &spu_list[node], list)
941                         destroy_spu(spu);
942         }
943         mutex_unlock(&spu_mutex);
944         sysdev_class_unregister(&spu_sysdev_class);
945 }
946 module_exit(cleanup_spu_base);
947
948 static int __init init_spu_base(void)
949 {
950         struct device_node *node;
951         int i, ret;
952
953         /* create sysdev class for spus */
954         ret = sysdev_class_register(&spu_sysdev_class);
955         if (ret)
956                 return ret;
957
958         for (i = 0; i < MAX_NUMNODES; i++)
959                 INIT_LIST_HEAD(&spu_list[i]);
960
961         ret = -ENODEV;
962         for (node = of_find_node_by_type(NULL, "spe");
963                         node; node = of_find_node_by_type(node, "spe")) {
964                 ret = create_spu(node);
965                 if (ret) {
966                         printk(KERN_WARNING "%s: Error initializing %s\n",
967                                 __FUNCTION__, node->name);
968                         cleanup_spu_base();
969                         break;
970                 }
971         }
972
973         xmon_register_spus(&spu_full_list);
974
975         return ret;
976 }
977 module_init(init_spu_base);
978
979 MODULE_LICENSE("GPL");
980 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");