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)
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.
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>
33 #include <linux/mutex.h>
34 #include <linux/linux_logo.h>
36 #include <asm/spu_priv1.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
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
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
)
93 spin_lock_irqsave(&spu_full_list_lock
, flags
);
94 list_for_each_entry(spu
, &spu_full_list
, full_list
) {
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
)
116 spin_lock_irqsave(&spu_full_list_lock
, flags
);
118 spin_unlock_irqrestore(&spu_full_list_lock
, flags
);
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
);
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
);
138 static int __spu_trap_error(struct spu
*spu
)
140 pr_debug("%s\n", __FUNCTION__
);
141 spu
->dma_callback(spu
, SPE_EVENT_SPE_ERROR
);
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
;
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__
);
169 esid
= (ea
& ESID_MASK
) | SLB_ESID_V
;
171 switch(REGION_ID(ea
)) {
173 #ifdef CONFIG_PPC_MM_SLICES
174 psize
= get_slice_psize(mm
, ea
);
176 psize
= mm
->context
.user_psize
;
178 vsid
= (get_vsid(mm
->context
.id
, ea
, MMU_SEGSIZE_256M
) << SLB_VSID_SHIFT
) |
181 case VMALLOC_REGION_ID
:
182 if (ea
< VMALLOC_END
)
183 psize
= mmu_vmalloc_psize
;
185 psize
= mmu_io_psize
;
186 vsid
= (get_kernel_vsid(ea
, MMU_SEGSIZE_256M
) << SLB_VSID_SHIFT
) |
189 case KERNEL_REGION_ID
:
190 psize
= mmu_linear_psize
;
191 vsid
= (get_kernel_vsid(ea
, MMU_SEGSIZE_256M
) << SLB_VSID_SHIFT
) |
195 /* Future: support kernel segments so that drivers
198 pr_debug("invalid region access at %016lx\n", ea
);
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
);
208 if (spu
->slb_replace
>= 8)
209 spu
->slb_replace
= 0;
211 spu_restart_dma(spu
);
212 spu
->stats
.slb_flt
++;
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
);
230 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE
, &spu
->flags
)) {
231 printk("%s: invalid access during switch!\n", __func__
);
238 spu
->stop_callback(spu
);
243 spu_irq_class_0(int irq
, void *data
)
246 unsigned long stat
, mask
;
250 mask
= spu_int_mask_get(spu
, 0);
251 stat
= spu_int_stat_get(spu
, 0);
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
);
266 spu_irq_class_0_bottom(struct spu
*spu
)
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
);
291 spu_irq_class_1(int irq
, void *data
)
294 unsigned long stat
, mask
, dar
, dsisr
;
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
,
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
;
328 spu_irq_class_2(int irq
, void *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 */
341 * mailbox interrupts (0x1 and 0x10) are level triggered.
342 * mask them now before acknowledging.
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
)
375 if (spu
->irqs
[0] != NO_IRQ
) {
376 snprintf(spu
->irq_c0
, sizeof (spu
->irq_c0
), "spe%02d.0",
378 ret
= request_irq(spu
->irqs
[0], spu_irq_class_0
,
384 if (spu
->irqs
[1] != NO_IRQ
) {
385 snprintf(spu
->irq_c1
, sizeof (spu
->irq_c1
), "spe%02d.1",
387 ret
= request_irq(spu
->irqs
[1], spu_irq_class_1
,
393 if (spu
->irqs
[2] != NO_IRQ
) {
394 snprintf(spu
->irq_c2
, sizeof (spu
->irq_c2
), "spe%02d.2",
396 ret
= request_irq(spu
->irqs
[2], spu_irq_class_2
,
405 if (spu
->irqs
[1] != NO_IRQ
)
406 free_irq(spu
->irqs
[1], spu
);
408 if (spu
->irqs
[0] != NO_IRQ
)
409 free_irq(spu
->irqs
[0], spu
);
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 {
430 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
431 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
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
;
442 /* initialize all channel data to zero */
443 for (i
= 0; i
< ARRAY_SIZE(zero_list
); i
++) {
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
);
464 spu_destroy_spu(spu
);
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
)
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
);
484 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr
);
486 int spu_add_sysdev_attr_group(struct attribute_group
*attrs
)
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
);
497 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group
);
500 void spu_remove_sysdev_attr(struct sysdev_attribute
*attr
)
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
)
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
)
526 spu
->sysdev
.id
= spu
->number
;
527 spu
->sysdev
.cls
= &spu_sysdev_class
;
528 ret
= sysdev_register(&spu
->sysdev
);
530 printk(KERN_ERR
"Can't register SPU %d with sysfs\n",
535 sysfs_add_device_to_node(&spu
->sysdev
, spu
->node
);
540 static int __init
create_spu(void *data
)
549 spu
= kzalloc(sizeof (*spu
), GFP_KERNEL
);
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
);
565 spu_mfc_sdr_setup(spu
);
566 spu_mfc_sr1_set(spu
, 0x33);
567 ret
= spu_request_irqs(spu
);
571 ret
= spu_create_sysdev(spu
);
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
;
588 spu
->stats
.tstamp
= timespec_to_ns(&ts
);
590 INIT_LIST_HEAD(&spu
->aff_list
);
597 spu_destroy_spu(spu
);
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
)
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
) {
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
,
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)
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
)
663 /* create sysdev class for spus */
664 ret
= sysdev_class_register(&spu_sysdev_class
);
668 ret
= spu_enumerate_spus(create_spu
);
671 printk(KERN_WARNING
"%s: Error initializing spus\n",
673 goto out_unregister_sysdev_class
;
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
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
);
698 out_unregister_sysdev_class
:
699 sysdev_class_unregister(&spu_sysdev_class
);
703 module_init(init_spu_base
);
705 MODULE_LICENSE("GPL");
706 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");