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>
27 #include <asm/kexec.h>
29 const struct spu_management_ops *spu_management_ops;
30 EXPORT_SYMBOL_GPL(spu_management_ops);
32 const struct spu_priv1_ops *spu_priv1_ops;
33 EXPORT_SYMBOL_GPL(spu_priv1_ops);
35 struct cbe_spu_info cbe_spu_info[MAX_NUMNODES];
36 EXPORT_SYMBOL_GPL(cbe_spu_info);
39 * The spufs fault-handling code needs to call force_sig_fault to raise signals
40 * on DMA errors. Export it here to avoid general kernel-wide access to this
43 EXPORT_SYMBOL_GPL(force_sig_fault);
46 * Protects cbe_spu_info and spu->number.
48 static DEFINE_SPINLOCK(spu_lock);
51 * List of all spus in the system.
53 * This list is iterated by callers from irq context and callers that
54 * want to sleep. Thus modifications need to be done with both
55 * spu_full_list_lock and spu_full_list_mutex held, while iterating
56 * through it requires either of these locks.
58 * In addition spu_full_list_lock protects all assignments to
61 static LIST_HEAD(spu_full_list);
62 static DEFINE_SPINLOCK(spu_full_list_lock);
63 static DEFINE_MUTEX(spu_full_list_mutex);
65 void spu_invalidate_slbs(struct spu *spu)
67 struct spu_priv2 __iomem *priv2 = spu->priv2;
70 spin_lock_irqsave(&spu->register_lock, flags);
71 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
72 out_be64(&priv2->slb_invalidate_all_W, 0UL);
73 spin_unlock_irqrestore(&spu->register_lock, flags);
75 EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
77 /* This is called by the MM core when a segment size is changed, to
78 * request a flush of all the SPEs using a given mm
80 void spu_flush_all_slbs(struct mm_struct *mm)
85 spin_lock_irqsave(&spu_full_list_lock, flags);
86 list_for_each_entry(spu, &spu_full_list, full_list) {
88 spu_invalidate_slbs(spu);
90 spin_unlock_irqrestore(&spu_full_list_lock, flags);
93 /* The hack below stinks... try to do something better one of
94 * these days... Does it even work properly with NR_CPUS == 1 ?
96 static inline void mm_needs_global_tlbie(struct mm_struct *mm)
98 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
100 /* Global TLBIE broadcast required with SPEs. */
101 bitmap_fill(cpumask_bits(mm_cpumask(mm)), nr);
104 void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
108 spin_lock_irqsave(&spu_full_list_lock, flags);
110 spin_unlock_irqrestore(&spu_full_list_lock, flags);
112 mm_needs_global_tlbie(mm);
114 EXPORT_SYMBOL_GPL(spu_associate_mm);
116 int spu_64k_pages_available(void)
118 return mmu_psize_defs[MMU_PAGE_64K].shift != 0;
120 EXPORT_SYMBOL_GPL(spu_64k_pages_available);
122 static void spu_restart_dma(struct spu *spu)
124 struct spu_priv2 __iomem *priv2 = spu->priv2;
126 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
127 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
129 set_bit(SPU_CONTEXT_FAULT_PENDING, &spu->flags);
134 static inline void spu_load_slb(struct spu *spu, int slbe, struct copro_slb *slb)
136 struct spu_priv2 __iomem *priv2 = spu->priv2;
138 pr_debug("%s: adding SLB[%d] 0x%016llx 0x%016llx\n",
139 __func__, slbe, slb->vsid, slb->esid);
141 out_be64(&priv2->slb_index_W, slbe);
142 /* set invalid before writing vsid */
143 out_be64(&priv2->slb_esid_RW, 0);
144 /* now it's safe to write the vsid */
145 out_be64(&priv2->slb_vsid_RW, slb->vsid);
146 /* setting the new esid makes the entry valid again */
147 out_be64(&priv2->slb_esid_RW, slb->esid);
150 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
152 struct copro_slb slb;
155 ret = copro_calculate_slb(spu->mm, ea, &slb);
159 spu_load_slb(spu, spu->slb_replace, &slb);
162 if (spu->slb_replace >= 8)
163 spu->slb_replace = 0;
165 spu_restart_dma(spu);
166 spu->stats.slb_flt++;
170 extern int hash_page(unsigned long ea, unsigned long access,
171 unsigned long trap, unsigned long dsisr); //XXX
172 static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
176 pr_debug("%s, %llx, %lx\n", __func__, dsisr, ea);
179 * Handle kernel space hash faults immediately. User hash
180 * faults need to be deferred to process context.
182 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND) &&
183 (get_region_id(ea) != USER_REGION_ID)) {
185 spin_unlock(&spu->register_lock);
187 _PAGE_PRESENT | _PAGE_READ | _PAGE_PRIVILEGED,
189 spin_lock(&spu->register_lock);
192 spu_restart_dma(spu);
197 spu->class_1_dar = ea;
198 spu->class_1_dsisr = dsisr;
200 spu->stop_callback(spu, 1);
202 spu->class_1_dar = 0;
203 spu->class_1_dsisr = 0;
208 static void __spu_kernel_slb(void *addr, struct copro_slb *slb)
210 unsigned long ea = (unsigned long)addr;
213 if (get_region_id(ea) == LINEAR_MAP_REGION_ID)
214 llp = mmu_psize_defs[mmu_linear_psize].sllp;
216 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
218 slb->vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
219 SLB_VSID_KERNEL | llp;
220 slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
224 * Given an array of @nr_slbs SLB entries, @slbs, return non-zero if the
225 * address @new_addr is present.
227 static inline int __slb_present(struct copro_slb *slbs, int nr_slbs,
230 unsigned long ea = (unsigned long)new_addr;
233 for (i = 0; i < nr_slbs; i++)
234 if (!((slbs[i].esid ^ ea) & ESID_MASK))
241 * Setup the SPU kernel SLBs, in preparation for a context save/restore. We
242 * need to map both the context save area, and the save/restore code.
244 * Because the lscsa and code may cross segment boundaries, we check to see
245 * if mappings are required for the start and end of each range. We currently
246 * assume that the mappings are smaller that one segment - if not, something
247 * is seriously wrong.
249 void spu_setup_kernel_slbs(struct spu *spu, struct spu_lscsa *lscsa,
250 void *code, int code_size)
252 struct copro_slb slbs[4];
254 /* start and end addresses of both mappings */
256 lscsa, (void *)lscsa + sizeof(*lscsa) - 1,
257 code, code + code_size - 1
260 /* check the set of addresses, and create a new entry in the slbs array
261 * if there isn't already a SLB for that address */
262 for (i = 0; i < ARRAY_SIZE(addrs); i++) {
263 if (__slb_present(slbs, nr_slbs, addrs[i]))
266 __spu_kernel_slb(addrs[i], &slbs[nr_slbs]);
270 spin_lock_irq(&spu->register_lock);
271 /* Add the set of SLBs */
272 for (i = 0; i < nr_slbs; i++)
273 spu_load_slb(spu, i, &slbs[i]);
274 spin_unlock_irq(&spu->register_lock);
276 EXPORT_SYMBOL_GPL(spu_setup_kernel_slbs);
279 spu_irq_class_0(int irq, void *data)
282 unsigned long stat, mask;
286 spin_lock(&spu->register_lock);
287 mask = spu_int_mask_get(spu, 0);
288 stat = spu_int_stat_get(spu, 0) & mask;
290 spu->class_0_pending |= stat;
291 spu->class_0_dar = spu_mfc_dar_get(spu);
292 spu->stop_callback(spu, 0);
293 spu->class_0_pending = 0;
294 spu->class_0_dar = 0;
296 spu_int_stat_clear(spu, 0, stat);
297 spin_unlock(&spu->register_lock);
303 spu_irq_class_1(int irq, void *data)
306 unsigned long stat, mask, dar, dsisr;
310 /* atomically read & clear class1 status. */
311 spin_lock(&spu->register_lock);
312 mask = spu_int_mask_get(spu, 1);
313 stat = spu_int_stat_get(spu, 1) & mask;
314 dar = spu_mfc_dar_get(spu);
315 dsisr = spu_mfc_dsisr_get(spu);
316 if (stat & CLASS1_STORAGE_FAULT_INTR)
317 spu_mfc_dsisr_set(spu, 0ul);
318 spu_int_stat_clear(spu, 1, stat);
320 pr_debug("%s: %lx %lx %lx %lx\n", __func__, mask, stat,
323 if (stat & CLASS1_SEGMENT_FAULT_INTR)
324 __spu_trap_data_seg(spu, dar);
326 if (stat & CLASS1_STORAGE_FAULT_INTR)
327 __spu_trap_data_map(spu, dar, dsisr);
329 if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_GET_INTR)
332 if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_PUT_INTR)
335 spu->class_1_dsisr = 0;
336 spu->class_1_dar = 0;
338 spin_unlock(&spu->register_lock);
340 return stat ? IRQ_HANDLED : IRQ_NONE;
344 spu_irq_class_2(int irq, void *data)
349 const int mailbox_intrs =
350 CLASS2_MAILBOX_THRESHOLD_INTR | CLASS2_MAILBOX_INTR;
353 spin_lock(&spu->register_lock);
354 stat = spu_int_stat_get(spu, 2);
355 mask = spu_int_mask_get(spu, 2);
356 /* ignore interrupts we're not waiting for */
358 /* mailbox interrupts are level triggered. mask them now before
360 if (stat & mailbox_intrs)
361 spu_int_mask_and(spu, 2, ~(stat & mailbox_intrs));
362 /* acknowledge all interrupts before the callbacks */
363 spu_int_stat_clear(spu, 2, stat);
365 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
367 if (stat & CLASS2_MAILBOX_INTR)
368 spu->ibox_callback(spu);
370 if (stat & CLASS2_SPU_STOP_INTR)
371 spu->stop_callback(spu, 2);
373 if (stat & CLASS2_SPU_HALT_INTR)
374 spu->stop_callback(spu, 2);
376 if (stat & CLASS2_SPU_DMA_TAG_GROUP_COMPLETE_INTR)
377 spu->mfc_callback(spu);
379 if (stat & CLASS2_MAILBOX_THRESHOLD_INTR)
380 spu->wbox_callback(spu);
382 spu->stats.class2_intr++;
384 spin_unlock(&spu->register_lock);
386 return stat ? IRQ_HANDLED : IRQ_NONE;
389 static int __init spu_request_irqs(struct spu *spu)
394 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
396 ret = request_irq(spu->irqs[0], spu_irq_class_0,
397 0, spu->irq_c0, spu);
402 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
404 ret = request_irq(spu->irqs[1], spu_irq_class_1,
405 0, spu->irq_c1, spu);
410 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
412 ret = request_irq(spu->irqs[2], spu_irq_class_2,
413 0, spu->irq_c2, spu);
421 free_irq(spu->irqs[1], spu);
424 free_irq(spu->irqs[0], spu);
429 static void spu_free_irqs(struct spu *spu)
432 free_irq(spu->irqs[0], spu);
434 free_irq(spu->irqs[1], spu);
436 free_irq(spu->irqs[2], spu);
439 void spu_init_channels(struct spu *spu)
441 static const struct {
445 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
446 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
448 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
449 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
450 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
452 struct spu_priv2 __iomem *priv2;
457 /* initialize all channel data to zero */
458 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
461 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
462 for (count = 0; count < zero_list[i].count; count++)
463 out_be64(&priv2->spu_chnldata_RW, 0);
466 /* initialize channel counts to meaningful values */
467 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
468 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
469 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
472 EXPORT_SYMBOL_GPL(spu_init_channels);
474 static struct bus_type spu_subsys = {
479 int spu_add_dev_attr(struct device_attribute *attr)
483 mutex_lock(&spu_full_list_mutex);
484 list_for_each_entry(spu, &spu_full_list, full_list)
485 device_create_file(&spu->dev, attr);
486 mutex_unlock(&spu_full_list_mutex);
490 EXPORT_SYMBOL_GPL(spu_add_dev_attr);
492 int spu_add_dev_attr_group(const struct attribute_group *attrs)
497 mutex_lock(&spu_full_list_mutex);
498 list_for_each_entry(spu, &spu_full_list, full_list) {
499 rc = sysfs_create_group(&spu->dev.kobj, attrs);
501 /* we're in trouble here, but try unwinding anyway */
503 printk(KERN_ERR "%s: can't create sysfs group '%s'\n",
504 __func__, attrs->name);
506 list_for_each_entry_continue_reverse(spu,
507 &spu_full_list, full_list)
508 sysfs_remove_group(&spu->dev.kobj, attrs);
513 mutex_unlock(&spu_full_list_mutex);
517 EXPORT_SYMBOL_GPL(spu_add_dev_attr_group);
520 void spu_remove_dev_attr(struct device_attribute *attr)
524 mutex_lock(&spu_full_list_mutex);
525 list_for_each_entry(spu, &spu_full_list, full_list)
526 device_remove_file(&spu->dev, attr);
527 mutex_unlock(&spu_full_list_mutex);
529 EXPORT_SYMBOL_GPL(spu_remove_dev_attr);
531 void spu_remove_dev_attr_group(const struct attribute_group *attrs)
535 mutex_lock(&spu_full_list_mutex);
536 list_for_each_entry(spu, &spu_full_list, full_list)
537 sysfs_remove_group(&spu->dev.kobj, attrs);
538 mutex_unlock(&spu_full_list_mutex);
540 EXPORT_SYMBOL_GPL(spu_remove_dev_attr_group);
542 static int __init spu_create_dev(struct spu *spu)
546 spu->dev.id = spu->number;
547 spu->dev.bus = &spu_subsys;
548 ret = device_register(&spu->dev);
550 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
555 sysfs_add_device_to_node(&spu->dev, spu->node);
560 static int __init create_spu(void *data)
568 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
572 spu->alloc_state = SPU_FREE;
574 spin_lock_init(&spu->register_lock);
575 spin_lock(&spu_lock);
576 spu->number = number++;
577 spin_unlock(&spu_lock);
579 ret = spu_create_spu(spu, data);
584 spu_mfc_sdr_setup(spu);
585 spu_mfc_sr1_set(spu, 0x33);
586 ret = spu_request_irqs(spu);
590 ret = spu_create_dev(spu);
594 mutex_lock(&cbe_spu_info[spu->node].list_mutex);
595 list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
596 cbe_spu_info[spu->node].n_spus++;
597 mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
599 mutex_lock(&spu_full_list_mutex);
600 spin_lock_irqsave(&spu_full_list_lock, flags);
601 list_add(&spu->full_list, &spu_full_list);
602 spin_unlock_irqrestore(&spu_full_list_lock, flags);
603 mutex_unlock(&spu_full_list_mutex);
605 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
606 spu->stats.tstamp = ktime_get_ns();
608 INIT_LIST_HEAD(&spu->aff_list);
615 spu_destroy_spu(spu);
622 static const char *spu_state_names[] = {
623 "user", "system", "iowait", "idle"
626 static unsigned long long spu_acct_time(struct spu *spu,
627 enum spu_utilization_state state)
629 unsigned long long time = spu->stats.times[state];
632 * If the spu is idle or the context is stopped, utilization
633 * statistics are not updated. Apply the time delta from the
634 * last recorded state of the spu.
636 if (spu->stats.util_state == state)
637 time += ktime_get_ns() - spu->stats.tstamp;
639 return time / NSEC_PER_MSEC;
643 static ssize_t spu_stat_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
646 struct spu *spu = container_of(dev, struct spu, dev);
648 return sprintf(buf, "%s %llu %llu %llu %llu "
649 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
650 spu_state_names[spu->stats.util_state],
651 spu_acct_time(spu, SPU_UTIL_USER),
652 spu_acct_time(spu, SPU_UTIL_SYSTEM),
653 spu_acct_time(spu, SPU_UTIL_IOWAIT),
654 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
655 spu->stats.vol_ctx_switch,
656 spu->stats.invol_ctx_switch,
661 spu->stats.class2_intr,
662 spu->stats.libassist);
665 static DEVICE_ATTR(stat, 0444, spu_stat_show, NULL);
667 #ifdef CONFIG_KEXEC_CORE
669 struct crash_spu_info {
671 u32 saved_spu_runcntl_RW;
672 u32 saved_spu_status_R;
673 u32 saved_spu_npc_RW;
674 u64 saved_mfc_sr1_RW;
679 #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
680 static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
682 static void crash_kexec_stop_spus(void)
688 for (i = 0; i < CRASH_NUM_SPUS; i++) {
689 if (!crash_spu_info[i].spu)
692 spu = crash_spu_info[i].spu;
694 crash_spu_info[i].saved_spu_runcntl_RW =
695 in_be32(&spu->problem->spu_runcntl_RW);
696 crash_spu_info[i].saved_spu_status_R =
697 in_be32(&spu->problem->spu_status_R);
698 crash_spu_info[i].saved_spu_npc_RW =
699 in_be32(&spu->problem->spu_npc_RW);
701 crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
702 crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
703 tmp = spu_mfc_sr1_get(spu);
704 crash_spu_info[i].saved_mfc_sr1_RW = tmp;
706 tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
707 spu_mfc_sr1_set(spu, tmp);
713 static void __init crash_register_spus(struct list_head *list)
718 list_for_each_entry(spu, list, full_list) {
719 if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
722 crash_spu_info[spu->number].spu = spu;
725 ret = crash_shutdown_register(&crash_kexec_stop_spus);
727 printk(KERN_ERR "Could not register SPU crash handler");
731 static inline void crash_register_spus(struct list_head *list)
736 static void spu_shutdown(void)
740 mutex_lock(&spu_full_list_mutex);
741 list_for_each_entry(spu, &spu_full_list, full_list) {
743 spu_destroy_spu(spu);
745 mutex_unlock(&spu_full_list_mutex);
748 static struct syscore_ops spu_syscore_ops = {
749 .shutdown = spu_shutdown,
752 static int __init init_spu_base(void)
756 for (i = 0; i < MAX_NUMNODES; i++) {
757 mutex_init(&cbe_spu_info[i].list_mutex);
758 INIT_LIST_HEAD(&cbe_spu_info[i].spus);
761 if (!spu_management_ops)
764 /* create system subsystem for spus */
765 ret = subsys_system_register(&spu_subsys, NULL);
769 ret = spu_enumerate_spus(create_spu);
772 printk(KERN_WARNING "%s: Error initializing spus\n",
774 goto out_unregister_subsys;
778 fb_append_extra_logo(&logo_spe_clut224, ret);
780 mutex_lock(&spu_full_list_mutex);
781 xmon_register_spus(&spu_full_list);
782 crash_register_spus(&spu_full_list);
783 mutex_unlock(&spu_full_list_mutex);
784 spu_add_dev_attr(&dev_attr_stat);
785 register_syscore_ops(&spu_syscore_ops);
791 out_unregister_subsys:
792 bus_unregister(&spu_subsys);
796 device_initcall(init_spu_base);