- patches.apparmor/remove_suid_new_case_in_2.6.22.diff: Merge fix.
[linux-flexiantxendom0-3.2.10.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/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/wait.h>
31 #include <linux/mm.h>
32 #include <linux/io.h>
33 #include <linux/mutex.h>
34 #include <asm/spu.h>
35 #include <asm/spu_priv1.h>
36 #include <asm/xmon.h>
37
38 const struct spu_management_ops *spu_management_ops;
39 EXPORT_SYMBOL_GPL(spu_management_ops);
40
41 const struct spu_priv1_ops *spu_priv1_ops;
42
43 static struct list_head spu_list[MAX_NUMNODES];
44 static LIST_HEAD(spu_full_list);
45 static DEFINE_MUTEX(spu_mutex);
46 static DEFINE_SPINLOCK(spu_list_lock);
47
48 EXPORT_SYMBOL_GPL(spu_priv1_ops);
49
50 void spu_invalidate_slbs(struct spu *spu)
51 {
52         struct spu_priv2 __iomem *priv2 = spu->priv2;
53
54         if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
55                 out_be64(&priv2->slb_invalidate_all_W, 0UL);
56 }
57 EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
58
59 /* This is called by the MM core when a segment size is changed, to
60  * request a flush of all the SPEs using a given mm
61  */
62 void spu_flush_all_slbs(struct mm_struct *mm)
63 {
64         struct spu *spu;
65         unsigned long flags;
66
67         spin_lock_irqsave(&spu_list_lock, flags);
68         list_for_each_entry(spu, &spu_full_list, full_list) {
69                 if (spu->mm == mm)
70                         spu_invalidate_slbs(spu);
71         }
72         spin_unlock_irqrestore(&spu_list_lock, flags);
73 }
74
75 /* The hack below stinks... try to do something better one of
76  * these days... Does it even work properly with NR_CPUS == 1 ?
77  */
78 static inline void mm_needs_global_tlbie(struct mm_struct *mm)
79 {
80         int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
81
82         /* Global TLBIE broadcast required with SPEs. */
83         __cpus_setall(&mm->cpu_vm_mask, nr);
84 }
85
86 void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
87 {
88         unsigned long flags;
89
90         spin_lock_irqsave(&spu_list_lock, flags);
91         spu->mm = mm;
92         spin_unlock_irqrestore(&spu_list_lock, flags);
93         if (mm)
94                 mm_needs_global_tlbie(mm);
95 }
96 EXPORT_SYMBOL_GPL(spu_associate_mm);
97
98 static int __spu_trap_invalid_dma(struct spu *spu)
99 {
100         pr_debug("%s\n", __FUNCTION__);
101         spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
102         return 0;
103 }
104
105 static int __spu_trap_dma_align(struct spu *spu)
106 {
107         pr_debug("%s\n", __FUNCTION__);
108         spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
109         return 0;
110 }
111
112 static int __spu_trap_error(struct spu *spu)
113 {
114         pr_debug("%s\n", __FUNCTION__);
115         spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
116         return 0;
117 }
118
119 static void spu_restart_dma(struct spu *spu)
120 {
121         struct spu_priv2 __iomem *priv2 = spu->priv2;
122
123         if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
124                 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
125 }
126
127 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
128 {
129         struct spu_priv2 __iomem *priv2 = spu->priv2;
130         struct mm_struct *mm = spu->mm;
131         u64 esid, vsid, llp;
132         int psize;
133
134         pr_debug("%s\n", __FUNCTION__);
135
136         if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
137                 /* SLBs are pre-loaded for context switch, so
138                  * we should never get here!
139                  */
140                 printk("%s: invalid access during switch!\n", __func__);
141                 return 1;
142         }
143         esid = (ea & ESID_MASK) | SLB_ESID_V;
144
145         switch(REGION_ID(ea)) {
146         case USER_REGION_ID:
147 #ifdef CONFIG_PPC_MM_SLICES
148                 psize = get_slice_psize(mm, ea);
149 #else
150                 psize = mm->context.user_psize;
151 #endif
152                 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
153                                 SLB_VSID_USER;
154                 break;
155         case VMALLOC_REGION_ID:
156                 if (ea < VMALLOC_END)
157                         psize = mmu_vmalloc_psize;
158                 else
159                         psize = mmu_io_psize;
160                 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
161                         SLB_VSID_KERNEL;
162                 break;
163         case KERNEL_REGION_ID:
164                 psize = mmu_linear_psize;
165                 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
166                         SLB_VSID_KERNEL;
167                 break;
168         default:
169                 /* Future: support kernel segments so that drivers
170                  * can use SPUs.
171                  */
172                 pr_debug("invalid region access at %016lx\n", ea);
173                 return 1;
174         }
175         llp = mmu_psize_defs[psize].sllp;
176
177         out_be64(&priv2->slb_index_W, spu->slb_replace);
178         out_be64(&priv2->slb_vsid_RW, vsid | llp);
179         out_be64(&priv2->slb_esid_RW, esid);
180
181         spu->slb_replace++;
182         if (spu->slb_replace >= 8)
183                 spu->slb_replace = 0;
184
185         spu_restart_dma(spu);
186
187         return 0;
188 }
189
190 extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
191 static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
192 {
193         pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
194
195         /* Handle kernel space hash faults immediately.
196            User hash faults need to be deferred to process context. */
197         if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
198             && REGION_ID(ea) != USER_REGION_ID
199             && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
200                 spu_restart_dma(spu);
201                 return 0;
202         }
203
204         if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
205                 printk("%s: invalid access during switch!\n", __func__);
206                 return 1;
207         }
208
209         spu->dar = ea;
210         spu->dsisr = dsisr;
211         mb();
212         spu->stop_callback(spu);
213         return 0;
214 }
215
216 static irqreturn_t
217 spu_irq_class_0(int irq, void *data)
218 {
219         struct spu *spu;
220         unsigned long stat, mask;
221
222         spu = data;
223
224         mask = spu_int_mask_get(spu, 0);
225         stat = spu_int_stat_get(spu, 0);
226         stat &= mask;
227
228         spin_lock(&spu->register_lock);
229         spu->class_0_pending |= stat;
230         spin_unlock(&spu->register_lock);
231
232         spu->stop_callback(spu);
233
234         spu_int_stat_clear(spu, 0, stat);
235
236         return IRQ_HANDLED;
237 }
238
239 int
240 spu_irq_class_0_bottom(struct spu *spu)
241 {
242         unsigned long flags;
243         unsigned long stat;
244
245         spin_lock_irqsave(&spu->register_lock, flags);
246         stat = spu->class_0_pending;
247         spu->class_0_pending = 0;
248
249         if (stat & 1) /* invalid DMA alignment */
250                 __spu_trap_dma_align(spu);
251
252         if (stat & 2) /* invalid MFC DMA */
253                 __spu_trap_invalid_dma(spu);
254
255         if (stat & 4) /* error on SPU */
256                 __spu_trap_error(spu);
257
258         spu_int_stat_clear(spu, 0, stat);
259         spin_unlock_irqrestore(&spu->register_lock, flags);
260
261         return (stat & 0x7) ? -EIO : 0;
262 }
263 EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
264
265 static irqreturn_t
266 spu_irq_class_1(int irq, void *data)
267 {
268         struct spu *spu;
269         unsigned long stat, mask, dar, dsisr;
270
271         spu = data;
272
273         /* atomically read & clear class1 status. */
274         spin_lock(&spu->register_lock);
275         mask  = spu_int_mask_get(spu, 1);
276         stat  = spu_int_stat_get(spu, 1) & mask;
277         dar   = spu_mfc_dar_get(spu);
278         dsisr = spu_mfc_dsisr_get(spu);
279         if (stat & 2) /* mapping fault */
280                 spu_mfc_dsisr_set(spu, 0ul);
281         spu_int_stat_clear(spu, 1, stat);
282         spin_unlock(&spu->register_lock);
283         pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
284                         dar, dsisr);
285
286         if (stat & 1) /* segment fault */
287                 __spu_trap_data_seg(spu, dar);
288
289         if (stat & 2) { /* mapping fault */
290                 __spu_trap_data_map(spu, dar, dsisr);
291         }
292
293         if (stat & 4) /* ls compare & suspend on get */
294                 ;
295
296         if (stat & 8) /* ls compare & suspend on put */
297                 ;
298
299         return stat ? IRQ_HANDLED : IRQ_NONE;
300 }
301
302 static irqreturn_t
303 spu_irq_class_2(int irq, void *data)
304 {
305         struct spu *spu;
306         unsigned long stat;
307         unsigned long mask;
308
309         spu = data;
310         spin_lock(&spu->register_lock);
311         stat = spu_int_stat_get(spu, 2);
312         mask = spu_int_mask_get(spu, 2);
313         /* ignore interrupts we're not waiting for */
314         stat &= mask;
315         /*
316          * mailbox interrupts (0x1 and 0x10) are level triggered.
317          * mask them now before acknowledging.
318          */
319         if (stat & 0x11)
320                 spu_int_mask_and(spu, 2, ~(stat & 0x11));
321         /* acknowledge all interrupts before the callbacks */
322         spu_int_stat_clear(spu, 2, stat);
323         spin_unlock(&spu->register_lock);
324
325         pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
326
327         if (stat & 1)  /* PPC core mailbox */
328                 spu->ibox_callback(spu);
329
330         if (stat & 2) /* SPU stop-and-signal */
331                 spu->stop_callback(spu);
332
333         if (stat & 4) /* SPU halted */
334                 spu->stop_callback(spu);
335
336         if (stat & 8) /* DMA tag group complete */
337                 spu->mfc_callback(spu);
338
339         if (stat & 0x10) /* SPU mailbox threshold */
340                 spu->wbox_callback(spu);
341
342         return stat ? IRQ_HANDLED : IRQ_NONE;
343 }
344
345 static int spu_request_irqs(struct spu *spu)
346 {
347         int ret = 0;
348
349         if (spu->irqs[0] != NO_IRQ) {
350                 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
351                          spu->number);
352                 ret = request_irq(spu->irqs[0], spu_irq_class_0,
353                                   IRQF_DISABLED,
354                                   spu->irq_c0, spu);
355                 if (ret)
356                         goto bail0;
357         }
358         if (spu->irqs[1] != NO_IRQ) {
359                 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
360                          spu->number);
361                 ret = request_irq(spu->irqs[1], spu_irq_class_1,
362                                   IRQF_DISABLED,
363                                   spu->irq_c1, spu);
364                 if (ret)
365                         goto bail1;
366         }
367         if (spu->irqs[2] != NO_IRQ) {
368                 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
369                          spu->number);
370                 ret = request_irq(spu->irqs[2], spu_irq_class_2,
371                                   IRQF_DISABLED,
372                                   spu->irq_c2, spu);
373                 if (ret)
374                         goto bail2;
375         }
376         return 0;
377
378 bail2:
379         if (spu->irqs[1] != NO_IRQ)
380                 free_irq(spu->irqs[1], spu);
381 bail1:
382         if (spu->irqs[0] != NO_IRQ)
383                 free_irq(spu->irqs[0], spu);
384 bail0:
385         return ret;
386 }
387
388 static void spu_free_irqs(struct spu *spu)
389 {
390         if (spu->irqs[0] != NO_IRQ)
391                 free_irq(spu->irqs[0], spu);
392         if (spu->irqs[1] != NO_IRQ)
393                 free_irq(spu->irqs[1], spu);
394         if (spu->irqs[2] != NO_IRQ)
395                 free_irq(spu->irqs[2], spu);
396 }
397
398 static void spu_init_channels(struct spu *spu)
399 {
400         static const struct {
401                  unsigned channel;
402                  unsigned count;
403         } zero_list[] = {
404                 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
405                 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
406         }, count_list[] = {
407                 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
408                 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
409                 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
410         };
411         struct spu_priv2 __iomem *priv2;
412         int i;
413
414         priv2 = spu->priv2;
415
416         /* initialize all channel data to zero */
417         for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
418                 int count;
419
420                 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
421                 for (count = 0; count < zero_list[i].count; count++)
422                         out_be64(&priv2->spu_chnldata_RW, 0);
423         }
424
425         /* initialize channel counts to meaningful values */
426         for (i = 0; i < ARRAY_SIZE(count_list); i++) {
427                 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
428                 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
429         }
430 }
431
432 struct spu *spu_alloc_node(int node)
433 {
434         struct spu *spu = NULL;
435
436         mutex_lock(&spu_mutex);
437         if (!list_empty(&spu_list[node])) {
438                 spu = list_entry(spu_list[node].next, struct spu, list);
439                 list_del_init(&spu->list);
440                 pr_debug("Got SPU %d %d\n", spu->number, spu->node);
441         }
442         mutex_unlock(&spu_mutex);
443
444         if (spu)
445                 spu_init_channels(spu);
446         return spu;
447 }
448 EXPORT_SYMBOL_GPL(spu_alloc_node);
449
450 struct spu *spu_alloc(void)
451 {
452         struct spu *spu = NULL;
453         int node;
454
455         for (node = 0; node < MAX_NUMNODES; node++) {
456                 spu = spu_alloc_node(node);
457                 if (spu)
458                         break;
459         }
460
461         return spu;
462 }
463
464 void spu_free(struct spu *spu)
465 {
466         mutex_lock(&spu_mutex);
467         list_add_tail(&spu->list, &spu_list[spu->node]);
468         mutex_unlock(&spu_mutex);
469 }
470 EXPORT_SYMBOL_GPL(spu_free);
471
472 struct sysdev_class spu_sysdev_class = {
473         set_kset_name("spu")
474 };
475
476 int spu_add_sysdev_attr(struct sysdev_attribute *attr)
477 {
478         struct spu *spu;
479         mutex_lock(&spu_mutex);
480
481         list_for_each_entry(spu, &spu_full_list, full_list)
482                 sysdev_create_file(&spu->sysdev, attr);
483
484         mutex_unlock(&spu_mutex);
485         return 0;
486 }
487 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
488
489 int spu_add_sysdev_attr_group(struct attribute_group *attrs)
490 {
491         struct spu *spu;
492         mutex_lock(&spu_mutex);
493
494         list_for_each_entry(spu, &spu_full_list, full_list)
495                 sysfs_create_group(&spu->sysdev.kobj, attrs);
496
497         mutex_unlock(&spu_mutex);
498         return 0;
499 }
500 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
501
502
503 void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
504 {
505         struct spu *spu;
506         mutex_lock(&spu_mutex);
507
508         list_for_each_entry(spu, &spu_full_list, full_list)
509                 sysdev_remove_file(&spu->sysdev, attr);
510
511         mutex_unlock(&spu_mutex);
512 }
513 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
514
515 void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
516 {
517         struct spu *spu;
518         mutex_lock(&spu_mutex);
519
520         list_for_each_entry(spu, &spu_full_list, full_list)
521                 sysfs_remove_group(&spu->sysdev.kobj, attrs);
522
523         mutex_unlock(&spu_mutex);
524 }
525 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
526
527 static int spu_create_sysdev(struct spu *spu)
528 {
529         int ret;
530
531         spu->sysdev.id = spu->number;
532         spu->sysdev.cls = &spu_sysdev_class;
533         ret = sysdev_register(&spu->sysdev);
534         if (ret) {
535                 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
536                                 spu->number);
537                 return ret;
538         }
539
540         sysfs_add_device_to_node(&spu->sysdev, spu->node);
541
542         return 0;
543 }
544
545 static int __init create_spu(void *data)
546 {
547         struct spu *spu;
548         int ret;
549         static int number;
550         unsigned long flags;
551
552         ret = -ENOMEM;
553         spu = kzalloc(sizeof (*spu), GFP_KERNEL);
554         if (!spu)
555                 goto out;
556
557         spin_lock_init(&spu->register_lock);
558         mutex_lock(&spu_mutex);
559         spu->number = number++;
560         mutex_unlock(&spu_mutex);
561
562         ret = spu_create_spu(spu, data);
563
564         if (ret)
565                 goto out_free;
566
567         spu_mfc_sdr_setup(spu);
568         spu_mfc_sr1_set(spu, 0x33);
569         ret = spu_request_irqs(spu);
570         if (ret)
571                 goto out_destroy;
572
573         ret = spu_create_sysdev(spu);
574         if (ret)
575                 goto out_free_irqs;
576
577         mutex_lock(&spu_mutex);
578         spin_lock_irqsave(&spu_list_lock, flags);
579         list_add(&spu->list, &spu_list[spu->node]);
580         list_add(&spu->full_list, &spu_full_list);
581         spin_unlock_irqrestore(&spu_list_lock, flags);
582         mutex_unlock(&spu_mutex);
583
584         goto out;
585
586 out_free_irqs:
587         spu_free_irqs(spu);
588 out_destroy:
589         spu_destroy_spu(spu);
590 out_free:
591         kfree(spu);
592 out:
593         return ret;
594 }
595
596 static int __init init_spu_base(void)
597 {
598         int i, ret = 0;
599
600         for (i = 0; i < MAX_NUMNODES; i++)
601                 INIT_LIST_HEAD(&spu_list[i]);
602
603         if (!spu_management_ops)
604                 goto out;
605
606         /* create sysdev class for spus */
607         ret = sysdev_class_register(&spu_sysdev_class);
608         if (ret)
609                 goto out;
610
611         ret = spu_enumerate_spus(create_spu);
612
613         if (ret) {
614                 printk(KERN_WARNING "%s: Error initializing spus\n",
615                         __FUNCTION__);
616                 goto out_unregister_sysdev_class;
617         }
618
619         xmon_register_spus(&spu_full_list);
620
621         return 0;
622
623  out_unregister_sysdev_class:
624         sysdev_class_unregister(&spu_sysdev_class);
625  out:
626
627         return ret;
628 }
629 module_init(init_spu_base);
630
631 MODULE_LICENSE("GPL");
632 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");