1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Low-level SPU handling
5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7 * Author: Arnd Bergmann <arndb@de.ibm.com>
12 #include <linux/interrupt.h>
13 #include <linux/list.h>
14 #include <linux/init.h>
15 #include <linux/ptrace.h>
16 #include <linux/slab.h>
17 #include <linux/wait.h>
20 #include <linux/mutex.h>
21 #include <linux/linux_logo.h>
22 #include <linux/syscore_ops.h>
24 #include <asm/spu_priv1.h>
25 #include <asm/spu_csa.h>
28 #include <asm/kexec.h>
30 const struct spu_management_ops
*spu_management_ops
;
31 EXPORT_SYMBOL_GPL(spu_management_ops
);
33 const struct spu_priv1_ops
*spu_priv1_ops
;
34 EXPORT_SYMBOL_GPL(spu_priv1_ops
);
36 struct cbe_spu_info cbe_spu_info
[MAX_NUMNODES
];
37 EXPORT_SYMBOL_GPL(cbe_spu_info
);
40 * The spufs fault-handling code needs to call force_sig_fault to raise signals
41 * on DMA errors. Export it here to avoid general kernel-wide access to this
44 EXPORT_SYMBOL_GPL(force_sig_fault
);
47 * Protects cbe_spu_info and spu->number.
49 static DEFINE_SPINLOCK(spu_lock
);
52 * List of all spus in the system.
54 * This list is iterated by callers from irq context and callers that
55 * want to sleep. Thus modifications need to be done with both
56 * spu_full_list_lock and spu_full_list_mutex held, while iterating
57 * through it requires either of these locks.
59 * In addition spu_full_list_lock protects all assignments to
62 static LIST_HEAD(spu_full_list
);
63 static DEFINE_SPINLOCK(spu_full_list_lock
);
64 static DEFINE_MUTEX(spu_full_list_mutex
);
66 void spu_invalidate_slbs(struct spu
*spu
)
68 struct spu_priv2 __iomem
*priv2
= spu
->priv2
;
71 spin_lock_irqsave(&spu
->register_lock
, flags
);
72 if (spu_mfc_sr1_get(spu
) & MFC_STATE1_RELOCATE_MASK
)
73 out_be64(&priv2
->slb_invalidate_all_W
, 0UL);
74 spin_unlock_irqrestore(&spu
->register_lock
, flags
);
76 EXPORT_SYMBOL_GPL(spu_invalidate_slbs
);
78 /* This is called by the MM core when a segment size is changed, to
79 * request a flush of all the SPEs using a given mm
81 void spu_flush_all_slbs(struct mm_struct
*mm
)
86 spin_lock_irqsave(&spu_full_list_lock
, flags
);
87 list_for_each_entry(spu
, &spu_full_list
, full_list
) {
89 spu_invalidate_slbs(spu
);
91 spin_unlock_irqrestore(&spu_full_list_lock
, flags
);
94 /* The hack below stinks... try to do something better one of
95 * these days... Does it even work properly with NR_CPUS == 1 ?
97 static inline void mm_needs_global_tlbie(struct mm_struct
*mm
)
99 int nr
= (NR_CPUS
> 1) ? NR_CPUS
: NR_CPUS
+ 1;
101 /* Global TLBIE broadcast required with SPEs. */
102 bitmap_fill(cpumask_bits(mm_cpumask(mm
)), nr
);
105 void spu_associate_mm(struct spu
*spu
, struct mm_struct
*mm
)
109 spin_lock_irqsave(&spu_full_list_lock
, flags
);
111 spin_unlock_irqrestore(&spu_full_list_lock
, flags
);
113 mm_needs_global_tlbie(mm
);
115 EXPORT_SYMBOL_GPL(spu_associate_mm
);
117 int spu_64k_pages_available(void)
119 return mmu_psize_defs
[MMU_PAGE_64K
].shift
!= 0;
121 EXPORT_SYMBOL_GPL(spu_64k_pages_available
);
123 static void spu_restart_dma(struct spu
*spu
)
125 struct spu_priv2 __iomem
*priv2
= spu
->priv2
;
127 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING
, &spu
->flags
))
128 out_be64(&priv2
->mfc_control_RW
, MFC_CNTL_RESTART_DMA_COMMAND
);
130 set_bit(SPU_CONTEXT_FAULT_PENDING
, &spu
->flags
);
135 static inline void spu_load_slb(struct spu
*spu
, int slbe
, struct copro_slb
*slb
)
137 struct spu_priv2 __iomem
*priv2
= spu
->priv2
;
139 pr_debug("%s: adding SLB[%d] 0x%016llx 0x%016llx\n",
140 __func__
, slbe
, slb
->vsid
, slb
->esid
);
142 out_be64(&priv2
->slb_index_W
, slbe
);
143 /* set invalid before writing vsid */
144 out_be64(&priv2
->slb_esid_RW
, 0);
145 /* now it's safe to write the vsid */
146 out_be64(&priv2
->slb_vsid_RW
, slb
->vsid
);
147 /* setting the new esid makes the entry valid again */
148 out_be64(&priv2
->slb_esid_RW
, slb
->esid
);
151 static int __spu_trap_data_seg(struct spu
*spu
, unsigned long ea
)
153 struct copro_slb slb
;
156 ret
= copro_calculate_slb(spu
->mm
, ea
, &slb
);
160 spu_load_slb(spu
, spu
->slb_replace
, &slb
);
163 if (spu
->slb_replace
>= 8)
164 spu
->slb_replace
= 0;
166 spu_restart_dma(spu
);
167 spu
->stats
.slb_flt
++;
171 extern int hash_page(unsigned long ea
, unsigned long access
,
172 unsigned long trap
, unsigned long dsisr
); //XXX
173 static int __spu_trap_data_map(struct spu
*spu
, unsigned long ea
, u64 dsisr
)
177 pr_debug("%s, %llx, %lx\n", __func__
, dsisr
, ea
);
180 * Handle kernel space hash faults immediately. User hash
181 * faults need to be deferred to process context.
183 if ((dsisr
& MFC_DSISR_PTE_NOT_FOUND
) &&
184 (get_region_id(ea
) != USER_REGION_ID
)) {
186 spin_unlock(&spu
->register_lock
);
188 _PAGE_PRESENT
| _PAGE_READ
| _PAGE_PRIVILEGED
,
190 spin_lock(&spu
->register_lock
);
193 spu_restart_dma(spu
);
198 spu
->class_1_dar
= ea
;
199 spu
->class_1_dsisr
= dsisr
;
201 spu
->stop_callback(spu
, 1);
203 spu
->class_1_dar
= 0;
204 spu
->class_1_dsisr
= 0;
209 static void __spu_kernel_slb(void *addr
, struct copro_slb
*slb
)
211 unsigned long ea
= (unsigned long)addr
;
214 if (get_region_id(ea
) == LINEAR_MAP_REGION_ID
)
215 llp
= mmu_psize_defs
[mmu_linear_psize
].sllp
;
217 llp
= mmu_psize_defs
[mmu_virtual_psize
].sllp
;
219 slb
->vsid
= (get_kernel_vsid(ea
, MMU_SEGSIZE_256M
) << SLB_VSID_SHIFT
) |
220 SLB_VSID_KERNEL
| llp
;
221 slb
->esid
= (ea
& ESID_MASK
) | SLB_ESID_V
;
225 * Given an array of @nr_slbs SLB entries, @slbs, return non-zero if the
226 * address @new_addr is present.
228 static inline int __slb_present(struct copro_slb
*slbs
, int nr_slbs
,
231 unsigned long ea
= (unsigned long)new_addr
;
234 for (i
= 0; i
< nr_slbs
; i
++)
235 if (!((slbs
[i
].esid
^ ea
) & ESID_MASK
))
242 * Setup the SPU kernel SLBs, in preparation for a context save/restore. We
243 * need to map both the context save area, and the save/restore code.
245 * Because the lscsa and code may cross segment boundaries, we check to see
246 * if mappings are required for the start and end of each range. We currently
247 * assume that the mappings are smaller that one segment - if not, something
248 * is seriously wrong.
250 void spu_setup_kernel_slbs(struct spu
*spu
, struct spu_lscsa
*lscsa
,
251 void *code
, int code_size
)
253 struct copro_slb slbs
[4];
255 /* start and end addresses of both mappings */
257 lscsa
, (void *)lscsa
+ sizeof(*lscsa
) - 1,
258 code
, code
+ code_size
- 1
261 /* check the set of addresses, and create a new entry in the slbs array
262 * if there isn't already a SLB for that address */
263 for (i
= 0; i
< ARRAY_SIZE(addrs
); i
++) {
264 if (__slb_present(slbs
, nr_slbs
, addrs
[i
]))
267 __spu_kernel_slb(addrs
[i
], &slbs
[nr_slbs
]);
271 spin_lock_irq(&spu
->register_lock
);
272 /* Add the set of SLBs */
273 for (i
= 0; i
< nr_slbs
; i
++)
274 spu_load_slb(spu
, i
, &slbs
[i
]);
275 spin_unlock_irq(&spu
->register_lock
);
277 EXPORT_SYMBOL_GPL(spu_setup_kernel_slbs
);
280 spu_irq_class_0(int irq
, void *data
)
283 unsigned long stat
, mask
;
287 spin_lock(&spu
->register_lock
);
288 mask
= spu_int_mask_get(spu
, 0);
289 stat
= spu_int_stat_get(spu
, 0) & mask
;
291 spu
->class_0_pending
|= stat
;
292 spu
->class_0_dar
= spu_mfc_dar_get(spu
);
293 spu
->stop_callback(spu
, 0);
294 spu
->class_0_pending
= 0;
295 spu
->class_0_dar
= 0;
297 spu_int_stat_clear(spu
, 0, stat
);
298 spin_unlock(&spu
->register_lock
);
304 spu_irq_class_1(int irq
, void *data
)
307 unsigned long stat
, mask
, dar
, dsisr
;
311 /* atomically read & clear class1 status. */
312 spin_lock(&spu
->register_lock
);
313 mask
= spu_int_mask_get(spu
, 1);
314 stat
= spu_int_stat_get(spu
, 1) & mask
;
315 dar
= spu_mfc_dar_get(spu
);
316 dsisr
= spu_mfc_dsisr_get(spu
);
317 if (stat
& CLASS1_STORAGE_FAULT_INTR
)
318 spu_mfc_dsisr_set(spu
, 0ul);
319 spu_int_stat_clear(spu
, 1, stat
);
321 pr_debug("%s: %lx %lx %lx %lx\n", __func__
, mask
, stat
,
324 if (stat
& CLASS1_SEGMENT_FAULT_INTR
)
325 __spu_trap_data_seg(spu
, dar
);
327 if (stat
& CLASS1_STORAGE_FAULT_INTR
)
328 __spu_trap_data_map(spu
, dar
, dsisr
);
330 if (stat
& CLASS1_LS_COMPARE_SUSPEND_ON_GET_INTR
)
333 if (stat
& CLASS1_LS_COMPARE_SUSPEND_ON_PUT_INTR
)
336 spu
->class_1_dsisr
= 0;
337 spu
->class_1_dar
= 0;
339 spin_unlock(&spu
->register_lock
);
341 return stat
? IRQ_HANDLED
: IRQ_NONE
;
345 spu_irq_class_2(int irq
, void *data
)
350 const int mailbox_intrs
=
351 CLASS2_MAILBOX_THRESHOLD_INTR
| CLASS2_MAILBOX_INTR
;
354 spin_lock(&spu
->register_lock
);
355 stat
= spu_int_stat_get(spu
, 2);
356 mask
= spu_int_mask_get(spu
, 2);
357 /* ignore interrupts we're not waiting for */
359 /* mailbox interrupts are level triggered. mask them now before
361 if (stat
& mailbox_intrs
)
362 spu_int_mask_and(spu
, 2, ~(stat
& mailbox_intrs
));
363 /* acknowledge all interrupts before the callbacks */
364 spu_int_stat_clear(spu
, 2, stat
);
366 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq
, stat
, mask
);
368 if (stat
& CLASS2_MAILBOX_INTR
)
369 spu
->ibox_callback(spu
);
371 if (stat
& CLASS2_SPU_STOP_INTR
)
372 spu
->stop_callback(spu
, 2);
374 if (stat
& CLASS2_SPU_HALT_INTR
)
375 spu
->stop_callback(spu
, 2);
377 if (stat
& CLASS2_SPU_DMA_TAG_GROUP_COMPLETE_INTR
)
378 spu
->mfc_callback(spu
);
380 if (stat
& CLASS2_MAILBOX_THRESHOLD_INTR
)
381 spu
->wbox_callback(spu
);
383 spu
->stats
.class2_intr
++;
385 spin_unlock(&spu
->register_lock
);
387 return stat
? IRQ_HANDLED
: IRQ_NONE
;
390 static int spu_request_irqs(struct spu
*spu
)
395 snprintf(spu
->irq_c0
, sizeof (spu
->irq_c0
), "spe%02d.0",
397 ret
= request_irq(spu
->irqs
[0], spu_irq_class_0
,
398 0, spu
->irq_c0
, spu
);
403 snprintf(spu
->irq_c1
, sizeof (spu
->irq_c1
), "spe%02d.1",
405 ret
= request_irq(spu
->irqs
[1], spu_irq_class_1
,
406 0, spu
->irq_c1
, spu
);
411 snprintf(spu
->irq_c2
, sizeof (spu
->irq_c2
), "spe%02d.2",
413 ret
= request_irq(spu
->irqs
[2], spu_irq_class_2
,
414 0, spu
->irq_c2
, spu
);
422 free_irq(spu
->irqs
[1], spu
);
425 free_irq(spu
->irqs
[0], spu
);
430 static void spu_free_irqs(struct spu
*spu
)
433 free_irq(spu
->irqs
[0], spu
);
435 free_irq(spu
->irqs
[1], spu
);
437 free_irq(spu
->irqs
[2], spu
);
440 void spu_init_channels(struct spu
*spu
)
442 static const struct {
446 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
447 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
449 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
450 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
451 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
453 struct spu_priv2 __iomem
*priv2
;
458 /* initialize all channel data to zero */
459 for (i
= 0; i
< ARRAY_SIZE(zero_list
); i
++) {
462 out_be64(&priv2
->spu_chnlcntptr_RW
, zero_list
[i
].channel
);
463 for (count
= 0; count
< zero_list
[i
].count
; count
++)
464 out_be64(&priv2
->spu_chnldata_RW
, 0);
467 /* initialize channel counts to meaningful values */
468 for (i
= 0; i
< ARRAY_SIZE(count_list
); i
++) {
469 out_be64(&priv2
->spu_chnlcntptr_RW
, count_list
[i
].channel
);
470 out_be64(&priv2
->spu_chnlcnt_RW
, count_list
[i
].count
);
473 EXPORT_SYMBOL_GPL(spu_init_channels
);
475 static struct bus_type spu_subsys
= {
480 int spu_add_dev_attr(struct device_attribute
*attr
)
484 mutex_lock(&spu_full_list_mutex
);
485 list_for_each_entry(spu
, &spu_full_list
, full_list
)
486 device_create_file(&spu
->dev
, attr
);
487 mutex_unlock(&spu_full_list_mutex
);
491 EXPORT_SYMBOL_GPL(spu_add_dev_attr
);
493 int spu_add_dev_attr_group(struct attribute_group
*attrs
)
498 mutex_lock(&spu_full_list_mutex
);
499 list_for_each_entry(spu
, &spu_full_list
, full_list
) {
500 rc
= sysfs_create_group(&spu
->dev
.kobj
, attrs
);
502 /* we're in trouble here, but try unwinding anyway */
504 printk(KERN_ERR
"%s: can't create sysfs group '%s'\n",
505 __func__
, attrs
->name
);
507 list_for_each_entry_continue_reverse(spu
,
508 &spu_full_list
, full_list
)
509 sysfs_remove_group(&spu
->dev
.kobj
, attrs
);
514 mutex_unlock(&spu_full_list_mutex
);
518 EXPORT_SYMBOL_GPL(spu_add_dev_attr_group
);
521 void spu_remove_dev_attr(struct device_attribute
*attr
)
525 mutex_lock(&spu_full_list_mutex
);
526 list_for_each_entry(spu
, &spu_full_list
, full_list
)
527 device_remove_file(&spu
->dev
, attr
);
528 mutex_unlock(&spu_full_list_mutex
);
530 EXPORT_SYMBOL_GPL(spu_remove_dev_attr
);
532 void spu_remove_dev_attr_group(struct attribute_group
*attrs
)
536 mutex_lock(&spu_full_list_mutex
);
537 list_for_each_entry(spu
, &spu_full_list
, full_list
)
538 sysfs_remove_group(&spu
->dev
.kobj
, attrs
);
539 mutex_unlock(&spu_full_list_mutex
);
541 EXPORT_SYMBOL_GPL(spu_remove_dev_attr_group
);
543 static int spu_create_dev(struct spu
*spu
)
547 spu
->dev
.id
= spu
->number
;
548 spu
->dev
.bus
= &spu_subsys
;
549 ret
= device_register(&spu
->dev
);
551 printk(KERN_ERR
"Can't register SPU %d with sysfs\n",
556 sysfs_add_device_to_node(&spu
->dev
, spu
->node
);
561 static int __init
create_spu(void *data
)
569 spu
= kzalloc(sizeof (*spu
), GFP_KERNEL
);
573 spu
->alloc_state
= SPU_FREE
;
575 spin_lock_init(&spu
->register_lock
);
576 spin_lock(&spu_lock
);
577 spu
->number
= number
++;
578 spin_unlock(&spu_lock
);
580 ret
= spu_create_spu(spu
, data
);
585 spu_mfc_sdr_setup(spu
);
586 spu_mfc_sr1_set(spu
, 0x33);
587 ret
= spu_request_irqs(spu
);
591 ret
= spu_create_dev(spu
);
595 mutex_lock(&cbe_spu_info
[spu
->node
].list_mutex
);
596 list_add(&spu
->cbe_list
, &cbe_spu_info
[spu
->node
].spus
);
597 cbe_spu_info
[spu
->node
].n_spus
++;
598 mutex_unlock(&cbe_spu_info
[spu
->node
].list_mutex
);
600 mutex_lock(&spu_full_list_mutex
);
601 spin_lock_irqsave(&spu_full_list_lock
, flags
);
602 list_add(&spu
->full_list
, &spu_full_list
);
603 spin_unlock_irqrestore(&spu_full_list_lock
, flags
);
604 mutex_unlock(&spu_full_list_mutex
);
606 spu
->stats
.util_state
= SPU_UTIL_IDLE_LOADED
;
607 spu
->stats
.tstamp
= ktime_get_ns();
609 INIT_LIST_HEAD(&spu
->aff_list
);
616 spu_destroy_spu(spu
);
623 static const char *spu_state_names
[] = {
624 "user", "system", "iowait", "idle"
627 static unsigned long long spu_acct_time(struct spu
*spu
,
628 enum spu_utilization_state state
)
630 unsigned long long time
= spu
->stats
.times
[state
];
633 * If the spu is idle or the context is stopped, utilization
634 * statistics are not updated. Apply the time delta from the
635 * last recorded state of the spu.
637 if (spu
->stats
.util_state
== state
)
638 time
+= ktime_get_ns() - spu
->stats
.tstamp
;
640 return time
/ NSEC_PER_MSEC
;
644 static ssize_t
spu_stat_show(struct device
*dev
,
645 struct device_attribute
*attr
, char *buf
)
647 struct spu
*spu
= container_of(dev
, struct spu
, dev
);
649 return sprintf(buf
, "%s %llu %llu %llu %llu "
650 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
651 spu_state_names
[spu
->stats
.util_state
],
652 spu_acct_time(spu
, SPU_UTIL_USER
),
653 spu_acct_time(spu
, SPU_UTIL_SYSTEM
),
654 spu_acct_time(spu
, SPU_UTIL_IOWAIT
),
655 spu_acct_time(spu
, SPU_UTIL_IDLE_LOADED
),
656 spu
->stats
.vol_ctx_switch
,
657 spu
->stats
.invol_ctx_switch
,
662 spu
->stats
.class2_intr
,
663 spu
->stats
.libassist
);
666 static DEVICE_ATTR(stat
, 0444, spu_stat_show
, NULL
);
668 #ifdef CONFIG_KEXEC_CORE
670 struct crash_spu_info
{
672 u32 saved_spu_runcntl_RW
;
673 u32 saved_spu_status_R
;
674 u32 saved_spu_npc_RW
;
675 u64 saved_mfc_sr1_RW
;
680 #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
681 static struct crash_spu_info crash_spu_info
[CRASH_NUM_SPUS
];
683 static void crash_kexec_stop_spus(void)
689 for (i
= 0; i
< CRASH_NUM_SPUS
; i
++) {
690 if (!crash_spu_info
[i
].spu
)
693 spu
= crash_spu_info
[i
].spu
;
695 crash_spu_info
[i
].saved_spu_runcntl_RW
=
696 in_be32(&spu
->problem
->spu_runcntl_RW
);
697 crash_spu_info
[i
].saved_spu_status_R
=
698 in_be32(&spu
->problem
->spu_status_R
);
699 crash_spu_info
[i
].saved_spu_npc_RW
=
700 in_be32(&spu
->problem
->spu_npc_RW
);
702 crash_spu_info
[i
].saved_mfc_dar
= spu_mfc_dar_get(spu
);
703 crash_spu_info
[i
].saved_mfc_dsisr
= spu_mfc_dsisr_get(spu
);
704 tmp
= spu_mfc_sr1_get(spu
);
705 crash_spu_info
[i
].saved_mfc_sr1_RW
= tmp
;
707 tmp
&= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK
;
708 spu_mfc_sr1_set(spu
, tmp
);
714 static void crash_register_spus(struct list_head
*list
)
719 list_for_each_entry(spu
, list
, full_list
) {
720 if (WARN_ON(spu
->number
>= CRASH_NUM_SPUS
))
723 crash_spu_info
[spu
->number
].spu
= spu
;
726 ret
= crash_shutdown_register(&crash_kexec_stop_spus
);
728 printk(KERN_ERR
"Could not register SPU crash handler");
732 static inline void crash_register_spus(struct list_head
*list
)
737 static void spu_shutdown(void)
741 mutex_lock(&spu_full_list_mutex
);
742 list_for_each_entry(spu
, &spu_full_list
, full_list
) {
744 spu_destroy_spu(spu
);
746 mutex_unlock(&spu_full_list_mutex
);
749 static struct syscore_ops spu_syscore_ops
= {
750 .shutdown
= spu_shutdown
,
753 static int __init
init_spu_base(void)
757 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
758 mutex_init(&cbe_spu_info
[i
].list_mutex
);
759 INIT_LIST_HEAD(&cbe_spu_info
[i
].spus
);
762 if (!spu_management_ops
)
765 /* create system subsystem for spus */
766 ret
= subsys_system_register(&spu_subsys
, NULL
);
770 ret
= spu_enumerate_spus(create_spu
);
773 printk(KERN_WARNING
"%s: Error initializing spus\n",
775 goto out_unregister_subsys
;
779 fb_append_extra_logo(&logo_spe_clut224
, ret
);
781 mutex_lock(&spu_full_list_mutex
);
782 xmon_register_spus(&spu_full_list
);
783 crash_register_spus(&spu_full_list
);
784 mutex_unlock(&spu_full_list_mutex
);
785 spu_add_dev_attr(&dev_attr_stat
);
786 register_syscore_ops(&spu_syscore_ops
);
792 out_unregister_subsys
:
793 bus_unregister(&spu_subsys
);
797 device_initcall(init_spu_base
);