[POWERPC] cell: export force_sig_info()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / platforms / cell / spu_base.c
blob525712055bb2bd81aaaa9fcd3472d5c95c3cfe52
1 /*
2 * Low-level SPU handling
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
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.
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.
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.
23 #undef DEBUG
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 <linux/linux_logo.h>
35 #include <asm/spu.h>
36 #include <asm/spu_priv1.h>
37 #include <asm/xmon.h>
38 #include <asm/prom.h>
40 const struct spu_management_ops *spu_management_ops;
41 EXPORT_SYMBOL_GPL(spu_management_ops);
43 const struct spu_priv1_ops *spu_priv1_ops;
44 EXPORT_SYMBOL_GPL(spu_priv1_ops);
46 struct cbe_spu_info cbe_spu_info[MAX_NUMNODES];
47 EXPORT_SYMBOL_GPL(cbe_spu_info);
50 * The spufs fault-handling code needs to call force_sig_info to raise signals
51 * on DMA errors. Export it here to avoid general kernel-wide access to this
52 * function
54 EXPORT_SYMBOL_GPL(force_sig_info);
57 * Protects cbe_spu_info and spu->number.
59 static DEFINE_SPINLOCK(spu_lock);
62 * List of all spus in the system.
64 * This list is iterated by callers from irq context and callers that
65 * want to sleep. Thus modifications need to be done with both
66 * spu_full_list_lock and spu_full_list_mutex held, while iterating
67 * through it requires either of these locks.
69 * In addition spu_full_list_lock protects all assignmens to
70 * spu->mm.
72 static LIST_HEAD(spu_full_list);
73 static DEFINE_SPINLOCK(spu_full_list_lock);
74 static DEFINE_MUTEX(spu_full_list_mutex);
76 void spu_invalidate_slbs(struct spu *spu)
78 struct spu_priv2 __iomem *priv2 = spu->priv2;
80 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
81 out_be64(&priv2->slb_invalidate_all_W, 0UL);
83 EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
85 /* This is called by the MM core when a segment size is changed, to
86 * request a flush of all the SPEs using a given mm
88 void spu_flush_all_slbs(struct mm_struct *mm)
90 struct spu *spu;
91 unsigned long flags;
93 spin_lock_irqsave(&spu_full_list_lock, flags);
94 list_for_each_entry(spu, &spu_full_list, full_list) {
95 if (spu->mm == mm)
96 spu_invalidate_slbs(spu);
98 spin_unlock_irqrestore(&spu_full_list_lock, flags);
101 /* The hack below stinks... try to do something better one of
102 * these days... Does it even work properly with NR_CPUS == 1 ?
104 static inline void mm_needs_global_tlbie(struct mm_struct *mm)
106 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
108 /* Global TLBIE broadcast required with SPEs. */
109 __cpus_setall(&mm->cpu_vm_mask, nr);
112 void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
114 unsigned long flags;
116 spin_lock_irqsave(&spu_full_list_lock, flags);
117 spu->mm = mm;
118 spin_unlock_irqrestore(&spu_full_list_lock, flags);
119 if (mm)
120 mm_needs_global_tlbie(mm);
122 EXPORT_SYMBOL_GPL(spu_associate_mm);
124 static int __spu_trap_invalid_dma(struct spu *spu)
126 pr_debug("%s\n", __FUNCTION__);
127 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
128 return 0;
131 static int __spu_trap_dma_align(struct spu *spu)
133 pr_debug("%s\n", __FUNCTION__);
134 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
135 return 0;
138 static int __spu_trap_error(struct spu *spu)
140 pr_debug("%s\n", __FUNCTION__);
141 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
142 return 0;
145 static void spu_restart_dma(struct spu *spu)
147 struct spu_priv2 __iomem *priv2 = spu->priv2;
149 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
150 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
153 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
155 struct spu_priv2 __iomem *priv2 = spu->priv2;
156 struct mm_struct *mm = spu->mm;
157 u64 esid, vsid, llp;
158 int psize;
160 pr_debug("%s\n", __FUNCTION__);
162 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
163 /* SLBs are pre-loaded for context switch, so
164 * we should never get here!
166 printk("%s: invalid access during switch!\n", __func__);
167 return 1;
169 esid = (ea & ESID_MASK) | SLB_ESID_V;
171 switch(REGION_ID(ea)) {
172 case USER_REGION_ID:
173 #ifdef CONFIG_PPC_MM_SLICES
174 psize = get_slice_psize(mm, ea);
175 #else
176 psize = mm->context.user_psize;
177 #endif
178 vsid = (get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
179 SLB_VSID_USER;
180 break;
181 case VMALLOC_REGION_ID:
182 if (ea < VMALLOC_END)
183 psize = mmu_vmalloc_psize;
184 else
185 psize = mmu_io_psize;
186 vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
187 SLB_VSID_KERNEL;
188 break;
189 case KERNEL_REGION_ID:
190 psize = mmu_linear_psize;
191 vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
192 SLB_VSID_KERNEL;
193 break;
194 default:
195 /* Future: support kernel segments so that drivers
196 * can use SPUs.
198 pr_debug("invalid region access at %016lx\n", ea);
199 return 1;
201 llp = mmu_psize_defs[psize].sllp;
203 out_be64(&priv2->slb_index_W, spu->slb_replace);
204 out_be64(&priv2->slb_vsid_RW, vsid | llp);
205 out_be64(&priv2->slb_esid_RW, esid);
207 spu->slb_replace++;
208 if (spu->slb_replace >= 8)
209 spu->slb_replace = 0;
211 spu_restart_dma(spu);
212 spu->stats.slb_flt++;
213 return 0;
216 extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
217 static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
219 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
221 /* Handle kernel space hash faults immediately.
222 User hash faults need to be deferred to process context. */
223 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
224 && REGION_ID(ea) != USER_REGION_ID
225 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
226 spu_restart_dma(spu);
227 return 0;
230 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
231 printk("%s: invalid access during switch!\n", __func__);
232 return 1;
235 spu->dar = ea;
236 spu->dsisr = dsisr;
237 mb();
238 spu->stop_callback(spu);
239 return 0;
242 static irqreturn_t
243 spu_irq_class_0(int irq, void *data)
245 struct spu *spu;
246 unsigned long stat, mask;
248 spu = data;
250 mask = spu_int_mask_get(spu, 0);
251 stat = spu_int_stat_get(spu, 0);
252 stat &= mask;
254 spin_lock(&spu->register_lock);
255 spu->class_0_pending |= stat;
256 spin_unlock(&spu->register_lock);
258 spu->stop_callback(spu);
260 spu_int_stat_clear(spu, 0, stat);
262 return IRQ_HANDLED;
266 spu_irq_class_0_bottom(struct spu *spu)
268 unsigned long flags;
269 unsigned long stat;
271 spin_lock_irqsave(&spu->register_lock, flags);
272 stat = spu->class_0_pending;
273 spu->class_0_pending = 0;
275 if (stat & 1) /* invalid DMA alignment */
276 __spu_trap_dma_align(spu);
278 if (stat & 2) /* invalid MFC DMA */
279 __spu_trap_invalid_dma(spu);
281 if (stat & 4) /* error on SPU */
282 __spu_trap_error(spu);
284 spin_unlock_irqrestore(&spu->register_lock, flags);
286 return (stat & 0x7) ? -EIO : 0;
288 EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
290 static irqreturn_t
291 spu_irq_class_1(int irq, void *data)
293 struct spu *spu;
294 unsigned long stat, mask, dar, dsisr;
296 spu = data;
298 /* atomically read & clear class1 status. */
299 spin_lock(&spu->register_lock);
300 mask = spu_int_mask_get(spu, 1);
301 stat = spu_int_stat_get(spu, 1) & mask;
302 dar = spu_mfc_dar_get(spu);
303 dsisr = spu_mfc_dsisr_get(spu);
304 if (stat & 2) /* mapping fault */
305 spu_mfc_dsisr_set(spu, 0ul);
306 spu_int_stat_clear(spu, 1, stat);
307 spin_unlock(&spu->register_lock);
308 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
309 dar, dsisr);
311 if (stat & 1) /* segment fault */
312 __spu_trap_data_seg(spu, dar);
314 if (stat & 2) { /* mapping fault */
315 __spu_trap_data_map(spu, dar, dsisr);
318 if (stat & 4) /* ls compare & suspend on get */
321 if (stat & 8) /* ls compare & suspend on put */
324 return stat ? IRQ_HANDLED : IRQ_NONE;
327 static irqreturn_t
328 spu_irq_class_2(int irq, void *data)
330 struct spu *spu;
331 unsigned long stat;
332 unsigned long mask;
334 spu = data;
335 spin_lock(&spu->register_lock);
336 stat = spu_int_stat_get(spu, 2);
337 mask = spu_int_mask_get(spu, 2);
338 /* ignore interrupts we're not waiting for */
339 stat &= mask;
341 * mailbox interrupts (0x1 and 0x10) are level triggered.
342 * mask them now before acknowledging.
344 if (stat & 0x11)
345 spu_int_mask_and(spu, 2, ~(stat & 0x11));
346 /* acknowledge all interrupts before the callbacks */
347 spu_int_stat_clear(spu, 2, stat);
348 spin_unlock(&spu->register_lock);
350 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
352 if (stat & 1) /* PPC core mailbox */
353 spu->ibox_callback(spu);
355 if (stat & 2) /* SPU stop-and-signal */
356 spu->stop_callback(spu);
358 if (stat & 4) /* SPU halted */
359 spu->stop_callback(spu);
361 if (stat & 8) /* DMA tag group complete */
362 spu->mfc_callback(spu);
364 if (stat & 0x10) /* SPU mailbox threshold */
365 spu->wbox_callback(spu);
367 spu->stats.class2_intr++;
368 return stat ? IRQ_HANDLED : IRQ_NONE;
371 static int spu_request_irqs(struct spu *spu)
373 int ret = 0;
375 if (spu->irqs[0] != NO_IRQ) {
376 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
377 spu->number);
378 ret = request_irq(spu->irqs[0], spu_irq_class_0,
379 IRQF_DISABLED,
380 spu->irq_c0, spu);
381 if (ret)
382 goto bail0;
384 if (spu->irqs[1] != NO_IRQ) {
385 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
386 spu->number);
387 ret = request_irq(spu->irqs[1], spu_irq_class_1,
388 IRQF_DISABLED,
389 spu->irq_c1, spu);
390 if (ret)
391 goto bail1;
393 if (spu->irqs[2] != NO_IRQ) {
394 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
395 spu->number);
396 ret = request_irq(spu->irqs[2], spu_irq_class_2,
397 IRQF_DISABLED,
398 spu->irq_c2, spu);
399 if (ret)
400 goto bail2;
402 return 0;
404 bail2:
405 if (spu->irqs[1] != NO_IRQ)
406 free_irq(spu->irqs[1], spu);
407 bail1:
408 if (spu->irqs[0] != NO_IRQ)
409 free_irq(spu->irqs[0], spu);
410 bail0:
411 return ret;
414 static void spu_free_irqs(struct spu *spu)
416 if (spu->irqs[0] != NO_IRQ)
417 free_irq(spu->irqs[0], spu);
418 if (spu->irqs[1] != NO_IRQ)
419 free_irq(spu->irqs[1], spu);
420 if (spu->irqs[2] != NO_IRQ)
421 free_irq(spu->irqs[2], spu);
424 void spu_init_channels(struct spu *spu)
426 static const struct {
427 unsigned channel;
428 unsigned count;
429 } zero_list[] = {
430 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
431 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
432 }, count_list[] = {
433 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
434 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
435 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
437 struct spu_priv2 __iomem *priv2;
438 int i;
440 priv2 = spu->priv2;
442 /* initialize all channel data to zero */
443 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
444 int count;
446 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
447 for (count = 0; count < zero_list[i].count; count++)
448 out_be64(&priv2->spu_chnldata_RW, 0);
451 /* initialize channel counts to meaningful values */
452 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
453 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
454 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
457 EXPORT_SYMBOL_GPL(spu_init_channels);
459 static int spu_shutdown(struct sys_device *sysdev)
461 struct spu *spu = container_of(sysdev, struct spu, sysdev);
463 spu_free_irqs(spu);
464 spu_destroy_spu(spu);
465 return 0;
468 static struct sysdev_class spu_sysdev_class = {
469 set_kset_name("spu"),
470 .shutdown = spu_shutdown,
473 int spu_add_sysdev_attr(struct sysdev_attribute *attr)
475 struct spu *spu;
477 mutex_lock(&spu_full_list_mutex);
478 list_for_each_entry(spu, &spu_full_list, full_list)
479 sysdev_create_file(&spu->sysdev, attr);
480 mutex_unlock(&spu_full_list_mutex);
482 return 0;
484 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
486 int spu_add_sysdev_attr_group(struct attribute_group *attrs)
488 struct spu *spu;
490 mutex_lock(&spu_full_list_mutex);
491 list_for_each_entry(spu, &spu_full_list, full_list)
492 sysfs_create_group(&spu->sysdev.kobj, attrs);
493 mutex_unlock(&spu_full_list_mutex);
495 return 0;
497 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
500 void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
502 struct spu *spu;
504 mutex_lock(&spu_full_list_mutex);
505 list_for_each_entry(spu, &spu_full_list, full_list)
506 sysdev_remove_file(&spu->sysdev, attr);
507 mutex_unlock(&spu_full_list_mutex);
509 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
511 void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
513 struct spu *spu;
515 mutex_lock(&spu_full_list_mutex);
516 list_for_each_entry(spu, &spu_full_list, full_list)
517 sysfs_remove_group(&spu->sysdev.kobj, attrs);
518 mutex_unlock(&spu_full_list_mutex);
520 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
522 static int spu_create_sysdev(struct spu *spu)
524 int ret;
526 spu->sysdev.id = spu->number;
527 spu->sysdev.cls = &spu_sysdev_class;
528 ret = sysdev_register(&spu->sysdev);
529 if (ret) {
530 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
531 spu->number);
532 return ret;
535 sysfs_add_device_to_node(&spu->sysdev, spu->node);
537 return 0;
540 static int __init create_spu(void *data)
542 struct spu *spu;
543 int ret;
544 static int number;
545 unsigned long flags;
546 struct timespec ts;
548 ret = -ENOMEM;
549 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
550 if (!spu)
551 goto out;
553 spu->alloc_state = SPU_FREE;
555 spin_lock_init(&spu->register_lock);
556 spin_lock(&spu_lock);
557 spu->number = number++;
558 spin_unlock(&spu_lock);
560 ret = spu_create_spu(spu, data);
562 if (ret)
563 goto out_free;
565 spu_mfc_sdr_setup(spu);
566 spu_mfc_sr1_set(spu, 0x33);
567 ret = spu_request_irqs(spu);
568 if (ret)
569 goto out_destroy;
571 ret = spu_create_sysdev(spu);
572 if (ret)
573 goto out_free_irqs;
575 mutex_lock(&cbe_spu_info[spu->node].list_mutex);
576 list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
577 cbe_spu_info[spu->node].n_spus++;
578 mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
580 mutex_lock(&spu_full_list_mutex);
581 spin_lock_irqsave(&spu_full_list_lock, flags);
582 list_add(&spu->full_list, &spu_full_list);
583 spin_unlock_irqrestore(&spu_full_list_lock, flags);
584 mutex_unlock(&spu_full_list_mutex);
586 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
587 ktime_get_ts(&ts);
588 spu->stats.tstamp = timespec_to_ns(&ts);
590 INIT_LIST_HEAD(&spu->aff_list);
592 goto out;
594 out_free_irqs:
595 spu_free_irqs(spu);
596 out_destroy:
597 spu_destroy_spu(spu);
598 out_free:
599 kfree(spu);
600 out:
601 return ret;
604 static const char *spu_state_names[] = {
605 "user", "system", "iowait", "idle"
608 static unsigned long long spu_acct_time(struct spu *spu,
609 enum spu_utilization_state state)
611 struct timespec ts;
612 unsigned long long time = spu->stats.times[state];
615 * If the spu is idle or the context is stopped, utilization
616 * statistics are not updated. Apply the time delta from the
617 * last recorded state of the spu.
619 if (spu->stats.util_state == state) {
620 ktime_get_ts(&ts);
621 time += timespec_to_ns(&ts) - spu->stats.tstamp;
624 return time / NSEC_PER_MSEC;
628 static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
630 struct spu *spu = container_of(sysdev, struct spu, sysdev);
632 return sprintf(buf, "%s %llu %llu %llu %llu "
633 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
634 spu_state_names[spu->stats.util_state],
635 spu_acct_time(spu, SPU_UTIL_USER),
636 spu_acct_time(spu, SPU_UTIL_SYSTEM),
637 spu_acct_time(spu, SPU_UTIL_IOWAIT),
638 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
639 spu->stats.vol_ctx_switch,
640 spu->stats.invol_ctx_switch,
641 spu->stats.slb_flt,
642 spu->stats.hash_flt,
643 spu->stats.min_flt,
644 spu->stats.maj_flt,
645 spu->stats.class2_intr,
646 spu->stats.libassist);
649 static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
651 static int __init init_spu_base(void)
653 int i, ret = 0;
655 for (i = 0; i < MAX_NUMNODES; i++) {
656 mutex_init(&cbe_spu_info[i].list_mutex);
657 INIT_LIST_HEAD(&cbe_spu_info[i].spus);
660 if (!spu_management_ops)
661 goto out;
663 /* create sysdev class for spus */
664 ret = sysdev_class_register(&spu_sysdev_class);
665 if (ret)
666 goto out;
668 ret = spu_enumerate_spus(create_spu);
670 if (ret < 0) {
671 printk(KERN_WARNING "%s: Error initializing spus\n",
672 __FUNCTION__);
673 goto out_unregister_sysdev_class;
676 if (ret > 0) {
678 * We cannot put the forward declaration in
679 * <linux/linux_logo.h> because of conflicting session type
680 * conflicts for const and __initdata with different compiler
681 * versions
683 extern const struct linux_logo logo_spe_clut224;
685 fb_append_extra_logo(&logo_spe_clut224, ret);
688 mutex_lock(&spu_full_list_mutex);
689 xmon_register_spus(&spu_full_list);
690 crash_register_spus(&spu_full_list);
691 mutex_unlock(&spu_full_list_mutex);
692 spu_add_sysdev_attr(&attr_stat);
694 spu_init_affinity();
696 return 0;
698 out_unregister_sysdev_class:
699 sysdev_class_unregister(&spu_sysdev_class);
700 out:
701 return ret;
703 module_init(init_spu_base);
705 MODULE_LICENSE("GPL");
706 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");