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>
39 #include "spu_priv1_mmio.h"
41 const struct spu_management_ops *spu_management_ops;
42 EXPORT_SYMBOL_GPL(spu_management_ops);
44 const struct spu_priv1_ops *spu_priv1_ops;
45 EXPORT_SYMBOL_GPL(spu_priv1_ops);
47 struct cbe_spu_info cbe_spu_info[MAX_NUMNODES];
48 EXPORT_SYMBOL_GPL(cbe_spu_info);
51 * Protects cbe_spu_info and spu->number.
53 static DEFINE_SPINLOCK(spu_lock);
56 * List of all spus in the system.
58 * This list is iterated by callers from irq context and callers that
59 * want to sleep. Thus modifications need to be done with both
60 * spu_full_list_lock and spu_full_list_mutex held, while iterating
61 * through it requires either of these locks.
63 * In addition spu_full_list_lock protects all assignmens to
66 static LIST_HEAD(spu_full_list);
67 static DEFINE_SPINLOCK(spu_full_list_lock);
68 static DEFINE_MUTEX(spu_full_list_mutex);
70 void spu_invalidate_slbs(struct spu *spu)
72 struct spu_priv2 __iomem *priv2 = spu->priv2;
74 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
75 out_be64(&priv2->slb_invalidate_all_W, 0UL);
77 EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
79 /* This is called by the MM core when a segment size is changed, to
80 * request a flush of all the SPEs using a given mm
82 void spu_flush_all_slbs(struct mm_struct *mm)
87 spin_lock_irqsave(&spu_full_list_lock, flags);
88 list_for_each_entry(spu, &spu_full_list, full_list) {
90 spu_invalidate_slbs(spu);
92 spin_unlock_irqrestore(&spu_full_list_lock, flags);
95 /* The hack below stinks... try to do something better one of
96 * these days... Does it even work properly with NR_CPUS == 1 ?
98 static inline void mm_needs_global_tlbie(struct mm_struct *mm)
100 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
102 /* Global TLBIE broadcast required with SPEs. */
103 __cpus_setall(&mm->cpu_vm_mask, nr);
106 void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
110 spin_lock_irqsave(&spu_full_list_lock, flags);
112 spin_unlock_irqrestore(&spu_full_list_lock, flags);
114 mm_needs_global_tlbie(mm);
116 EXPORT_SYMBOL_GPL(spu_associate_mm);
118 static int __spu_trap_invalid_dma(struct spu *spu)
120 pr_debug("%s\n", __FUNCTION__);
121 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
125 static int __spu_trap_dma_align(struct spu *spu)
127 pr_debug("%s\n", __FUNCTION__);
128 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
132 static int __spu_trap_error(struct spu *spu)
134 pr_debug("%s\n", __FUNCTION__);
135 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
139 static void spu_restart_dma(struct spu *spu)
141 struct spu_priv2 __iomem *priv2 = spu->priv2;
143 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
144 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
147 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
149 struct spu_priv2 __iomem *priv2 = spu->priv2;
150 struct mm_struct *mm = spu->mm;
154 pr_debug("%s\n", __FUNCTION__);
156 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
157 /* SLBs are pre-loaded for context switch, so
158 * we should never get here!
160 printk("%s: invalid access during switch!\n", __func__);
163 esid = (ea & ESID_MASK) | SLB_ESID_V;
165 switch(REGION_ID(ea)) {
167 #ifdef CONFIG_PPC_MM_SLICES
168 psize = get_slice_psize(mm, ea);
170 psize = mm->context.user_psize;
172 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
175 case VMALLOC_REGION_ID:
176 if (ea < VMALLOC_END)
177 psize = mmu_vmalloc_psize;
179 psize = mmu_io_psize;
180 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
183 case KERNEL_REGION_ID:
184 psize = mmu_linear_psize;
185 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
189 /* Future: support kernel segments so that drivers
192 pr_debug("invalid region access at %016lx\n", ea);
195 llp = mmu_psize_defs[psize].sllp;
197 out_be64(&priv2->slb_index_W, spu->slb_replace);
198 out_be64(&priv2->slb_vsid_RW, vsid | llp);
199 out_be64(&priv2->slb_esid_RW, esid);
202 if (spu->slb_replace >= 8)
203 spu->slb_replace = 0;
205 spu_restart_dma(spu);
206 spu->stats.slb_flt++;
210 extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
211 static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
213 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
215 /* Handle kernel space hash faults immediately.
216 User hash faults need to be deferred to process context. */
217 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
218 && REGION_ID(ea) != USER_REGION_ID
219 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
220 spu_restart_dma(spu);
224 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
225 printk("%s: invalid access during switch!\n", __func__);
232 spu->stop_callback(spu);
237 spu_irq_class_0(int irq, void *data)
242 spu->class_0_pending = 1;
243 spu->stop_callback(spu);
249 spu_irq_class_0_bottom(struct spu *spu)
251 unsigned long stat, mask;
254 spu->class_0_pending = 0;
256 spin_lock_irqsave(&spu->register_lock, flags);
257 mask = spu_int_mask_get(spu, 0);
258 stat = spu_int_stat_get(spu, 0);
262 if (stat & 1) /* invalid DMA alignment */
263 __spu_trap_dma_align(spu);
265 if (stat & 2) /* invalid MFC DMA */
266 __spu_trap_invalid_dma(spu);
268 if (stat & 4) /* error on SPU */
269 __spu_trap_error(spu);
271 spu_int_stat_clear(spu, 0, stat);
272 spin_unlock_irqrestore(&spu->register_lock, flags);
274 return (stat & 0x7) ? -EIO : 0;
276 EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
279 spu_irq_class_1(int irq, void *data)
282 unsigned long stat, mask, dar, dsisr;
286 /* atomically read & clear class1 status. */
287 spin_lock(&spu->register_lock);
288 mask = spu_int_mask_get(spu, 1);
289 stat = spu_int_stat_get(spu, 1) & mask;
290 dar = spu_mfc_dar_get(spu);
291 dsisr = spu_mfc_dsisr_get(spu);
292 if (stat & 2) /* mapping fault */
293 spu_mfc_dsisr_set(spu, 0ul);
294 spu_int_stat_clear(spu, 1, stat);
295 spin_unlock(&spu->register_lock);
296 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
299 if (stat & 1) /* segment fault */
300 __spu_trap_data_seg(spu, dar);
302 if (stat & 2) { /* mapping fault */
303 __spu_trap_data_map(spu, dar, dsisr);
306 if (stat & 4) /* ls compare & suspend on get */
309 if (stat & 8) /* ls compare & suspend on put */
312 return stat ? IRQ_HANDLED : IRQ_NONE;
316 spu_irq_class_2(int irq, void *data)
323 spin_lock(&spu->register_lock);
324 stat = spu_int_stat_get(spu, 2);
325 mask = spu_int_mask_get(spu, 2);
326 /* ignore interrupts we're not waiting for */
329 * mailbox interrupts (0x1 and 0x10) are level triggered.
330 * mask them now before acknowledging.
333 spu_int_mask_and(spu, 2, ~(stat & 0x11));
334 /* acknowledge all interrupts before the callbacks */
335 spu_int_stat_clear(spu, 2, stat);
336 spin_unlock(&spu->register_lock);
338 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
340 if (stat & 1) /* PPC core mailbox */
341 spu->ibox_callback(spu);
343 if (stat & 2) /* SPU stop-and-signal */
344 spu->stop_callback(spu);
346 if (stat & 4) /* SPU halted */
347 spu->stop_callback(spu);
349 if (stat & 8) /* DMA tag group complete */
350 spu->mfc_callback(spu);
352 if (stat & 0x10) /* SPU mailbox threshold */
353 spu->wbox_callback(spu);
355 spu->stats.class2_intr++;
356 return stat ? IRQ_HANDLED : IRQ_NONE;
359 static int spu_request_irqs(struct spu *spu)
363 if (spu->irqs[0] != NO_IRQ) {
364 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
366 ret = request_irq(spu->irqs[0], spu_irq_class_0,
372 if (spu->irqs[1] != NO_IRQ) {
373 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
375 ret = request_irq(spu->irqs[1], spu_irq_class_1,
381 if (spu->irqs[2] != NO_IRQ) {
382 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
384 ret = request_irq(spu->irqs[2], spu_irq_class_2,
393 if (spu->irqs[1] != NO_IRQ)
394 free_irq(spu->irqs[1], spu);
396 if (spu->irqs[0] != NO_IRQ)
397 free_irq(spu->irqs[0], spu);
402 static void spu_free_irqs(struct spu *spu)
404 if (spu->irqs[0] != NO_IRQ)
405 free_irq(spu->irqs[0], spu);
406 if (spu->irqs[1] != NO_IRQ)
407 free_irq(spu->irqs[1], spu);
408 if (spu->irqs[2] != NO_IRQ)
409 free_irq(spu->irqs[2], spu);
412 void spu_init_channels(struct spu *spu)
414 static const struct {
418 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
419 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
421 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
422 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
423 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
425 struct spu_priv2 __iomem *priv2;
430 /* initialize all channel data to zero */
431 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
434 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
435 for (count = 0; count < zero_list[i].count; count++)
436 out_be64(&priv2->spu_chnldata_RW, 0);
439 /* initialize channel counts to meaningful values */
440 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
441 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
442 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
445 EXPORT_SYMBOL_GPL(spu_init_channels);
447 static int spu_shutdown(struct sys_device *sysdev)
449 struct spu *spu = container_of(sysdev, struct spu, sysdev);
452 spu_destroy_spu(spu);
456 struct sysdev_class spu_sysdev_class = {
457 set_kset_name("spu"),
458 .shutdown = spu_shutdown,
461 int spu_add_sysdev_attr(struct sysdev_attribute *attr)
465 mutex_lock(&spu_full_list_mutex);
466 list_for_each_entry(spu, &spu_full_list, full_list)
467 sysdev_create_file(&spu->sysdev, attr);
468 mutex_unlock(&spu_full_list_mutex);
472 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
474 int spu_add_sysdev_attr_group(struct attribute_group *attrs)
478 mutex_lock(&spu_full_list_mutex);
479 list_for_each_entry(spu, &spu_full_list, full_list)
480 sysfs_create_group(&spu->sysdev.kobj, attrs);
481 mutex_unlock(&spu_full_list_mutex);
485 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
488 void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
492 mutex_lock(&spu_full_list_mutex);
493 list_for_each_entry(spu, &spu_full_list, full_list)
494 sysdev_remove_file(&spu->sysdev, attr);
495 mutex_unlock(&spu_full_list_mutex);
497 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
499 void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
503 mutex_lock(&spu_full_list_mutex);
504 list_for_each_entry(spu, &spu_full_list, full_list)
505 sysfs_remove_group(&spu->sysdev.kobj, attrs);
506 mutex_unlock(&spu_full_list_mutex);
508 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
510 static int spu_create_sysdev(struct spu *spu)
514 spu->sysdev.id = spu->number;
515 spu->sysdev.cls = &spu_sysdev_class;
516 ret = sysdev_register(&spu->sysdev);
518 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
523 sysfs_add_device_to_node(&spu->sysdev, spu->node);
528 static int __init create_spu(void *data)
537 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
541 spu->alloc_state = SPU_FREE;
543 spin_lock_init(&spu->register_lock);
544 spin_lock(&spu_lock);
545 spu->number = number++;
546 spin_unlock(&spu_lock);
548 ret = spu_create_spu(spu, data);
553 spu_mfc_sdr_setup(spu);
554 spu_mfc_sr1_set(spu, 0x33);
555 ret = spu_request_irqs(spu);
559 ret = spu_create_sysdev(spu);
563 mutex_lock(&cbe_spu_info[spu->node].list_mutex);
564 list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
565 cbe_spu_info[spu->node].n_spus++;
566 mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
568 mutex_lock(&spu_full_list_mutex);
569 spin_lock_irqsave(&spu_full_list_lock, flags);
570 list_add(&spu->full_list, &spu_full_list);
571 spin_unlock_irqrestore(&spu_full_list_lock, flags);
572 mutex_unlock(&spu_full_list_mutex);
574 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
576 spu->stats.tstamp = timespec_to_ns(&ts);
578 INIT_LIST_HEAD(&spu->aff_list);
585 spu_destroy_spu(spu);
592 static const char *spu_state_names[] = {
593 "user", "system", "iowait", "idle"
596 static unsigned long long spu_acct_time(struct spu *spu,
597 enum spu_utilization_state state)
600 unsigned long long time = spu->stats.times[state];
603 * If the spu is idle or the context is stopped, utilization
604 * statistics are not updated. Apply the time delta from the
605 * last recorded state of the spu.
607 if (spu->stats.util_state == state) {
609 time += timespec_to_ns(&ts) - spu->stats.tstamp;
612 return time / NSEC_PER_MSEC;
616 static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
618 struct spu *spu = container_of(sysdev, struct spu, sysdev);
620 return sprintf(buf, "%s %llu %llu %llu %llu "
621 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
622 spu_state_names[spu->stats.util_state],
623 spu_acct_time(spu, SPU_UTIL_USER),
624 spu_acct_time(spu, SPU_UTIL_SYSTEM),
625 spu_acct_time(spu, SPU_UTIL_IOWAIT),
626 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
627 spu->stats.vol_ctx_switch,
628 spu->stats.invol_ctx_switch,
633 spu->stats.class2_intr,
634 spu->stats.libassist);
637 static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
639 /* Hardcoded affinity idxs for QS20 */
640 #define SPES_PER_BE 8
641 static int QS20_reg_idxs[SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
642 static int QS20_reg_memory[SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
644 static struct spu *spu_lookup_reg(int node, u32 reg)
648 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
649 if (*(u32 *)get_property(spu_devnode(spu), "reg", NULL) == reg)
655 static void init_aff_QS20_harcoded(void)
658 struct spu *last_spu, *spu;
661 for (node = 0; node < MAX_NUMNODES; node++) {
663 for (i = 0; i < SPES_PER_BE; i++) {
664 reg = QS20_reg_idxs[i];
665 spu = spu_lookup_reg(node, reg);
668 spu->has_mem_affinity = QS20_reg_memory[reg];
670 list_add_tail(&spu->aff_list,
671 &last_spu->aff_list);
677 static int of_has_vicinity(void)
681 spu = list_entry(cbe_spu_info[0].spus.next, struct spu, cbe_list);
682 return of_find_property(spu_devnode(spu), "vicinity", NULL) != NULL;
685 static struct spu *aff_devnode_spu(int cbe, struct device_node *dn)
689 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
690 if (spu_devnode(spu) == dn)
696 aff_node_next_to(int cbe, struct device_node *target, struct device_node *avoid)
699 const phandle *vic_handles;
702 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
703 if (spu_devnode(spu) == avoid)
705 vic_handles = get_property(spu_devnode(spu), "vicinity", &lenp);
706 for (i=0; i < (lenp / sizeof(phandle)); i++) {
707 if (vic_handles[i] == target->linux_phandle)
714 static void init_aff_fw_vicinity_node(int cbe)
716 struct spu *spu, *last_spu;
717 struct device_node *vic_dn, *last_spu_dn;
719 const phandle *vic_handles;
721 int lenp, i, added, mem_aff;
723 last_spu = list_entry(cbe_spu_info[cbe].spus.next, struct spu, cbe_list);
725 for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
726 last_spu_dn = spu_devnode(last_spu);
727 vic_handles = get_property(last_spu_dn, "vicinity", &lenp);
729 for (i = 0; i < (lenp / sizeof(phandle)); i++) {
730 if (vic_handles[i] == avoid_ph)
733 vic_dn = of_find_node_by_phandle(vic_handles[i]);
737 name = get_property(vic_dn, "name", NULL);
738 if (strcmp(name, "spe") == 0) {
739 spu = aff_devnode_spu(cbe, vic_dn);
740 avoid_ph = last_spu_dn->linux_phandle;
743 mem_aff = strcmp(name, "mic-tm") == 0;
744 spu = aff_node_next_to(cbe, vic_dn, last_spu_dn);
748 last_spu->has_mem_affinity = 1;
749 spu->has_mem_affinity = 1;
751 avoid_ph = vic_dn->linux_phandle;
753 list_add_tail(&spu->aff_list, &last_spu->aff_list);
760 static void init_aff_fw_vicinity(void)
764 /* sets has_mem_affinity for each spu, as long as the
765 * spu->aff_list list, linking each spu to its neighbors
767 for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
768 init_aff_fw_vicinity_node(cbe);
771 static int __init init_spu_base(void)
775 for (i = 0; i < MAX_NUMNODES; i++) {
776 mutex_init(&cbe_spu_info[i].list_mutex);
777 INIT_LIST_HEAD(&cbe_spu_info[i].spus);
780 if (!spu_management_ops)
783 /* create sysdev class for spus */
784 ret = sysdev_class_register(&spu_sysdev_class);
788 ret = spu_enumerate_spus(create_spu);
791 printk(KERN_WARNING "%s: Error initializing spus\n",
793 goto out_unregister_sysdev_class;
798 * We cannot put the forward declaration in
799 * <linux/linux_logo.h> because of conflicting session type
800 * conflicts for const and __initdata with different compiler
803 extern const struct linux_logo logo_spe_clut224;
805 fb_append_extra_logo(&logo_spe_clut224, ret);
808 mutex_lock(&spu_full_list_mutex);
809 xmon_register_spus(&spu_full_list);
810 crash_register_spus(&spu_full_list);
811 mutex_unlock(&spu_full_list_mutex);
812 spu_add_sysdev_attr(&attr_stat);
814 if (of_has_vicinity()) {
815 init_aff_fw_vicinity();
817 long root = of_get_flat_dt_root();
818 if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
819 init_aff_QS20_harcoded();
824 out_unregister_sysdev_class:
825 sysdev_class_unregister(&spu_sysdev_class);
829 module_init(init_spu_base);
831 MODULE_LICENSE("GPL");
832 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");