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 const struct spu_management_ops *spu_management_ops;
40 EXPORT_SYMBOL_GPL(spu_management_ops);
42 const struct spu_priv1_ops *spu_priv1_ops;
44 static struct list_head spu_list[MAX_NUMNODES];
45 static LIST_HEAD(spu_full_list);
46 static DEFINE_MUTEX(spu_mutex);
47 static DEFINE_SPINLOCK(spu_list_lock);
49 EXPORT_SYMBOL_GPL(spu_priv1_ops);
51 void spu_invalidate_slbs(struct spu *spu)
53 struct spu_priv2 __iomem *priv2 = spu->priv2;
55 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
56 out_be64(&priv2->slb_invalidate_all_W, 0UL);
58 EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
60 /* This is called by the MM core when a segment size is changed, to
61 * request a flush of all the SPEs using a given mm
63 void spu_flush_all_slbs(struct mm_struct *mm)
68 spin_lock_irqsave(&spu_list_lock, flags);
69 list_for_each_entry(spu, &spu_full_list, full_list) {
71 spu_invalidate_slbs(spu);
73 spin_unlock_irqrestore(&spu_list_lock, flags);
76 /* The hack below stinks... try to do something better one of
77 * these days... Does it even work properly with NR_CPUS == 1 ?
79 static inline void mm_needs_global_tlbie(struct mm_struct *mm)
81 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
83 /* Global TLBIE broadcast required with SPEs. */
84 __cpus_setall(&mm->cpu_vm_mask, nr);
87 void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
91 spin_lock_irqsave(&spu_list_lock, flags);
93 spin_unlock_irqrestore(&spu_list_lock, flags);
95 mm_needs_global_tlbie(mm);
97 EXPORT_SYMBOL_GPL(spu_associate_mm);
99 static int __spu_trap_invalid_dma(struct spu *spu)
101 pr_debug("%s\n", __FUNCTION__);
102 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
106 static int __spu_trap_dma_align(struct spu *spu)
108 pr_debug("%s\n", __FUNCTION__);
109 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
113 static int __spu_trap_error(struct spu *spu)
115 pr_debug("%s\n", __FUNCTION__);
116 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
120 static void spu_restart_dma(struct spu *spu)
122 struct spu_priv2 __iomem *priv2 = spu->priv2;
124 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
125 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
128 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
130 struct spu_priv2 __iomem *priv2 = spu->priv2;
131 struct mm_struct *mm = spu->mm;
135 pr_debug("%s\n", __FUNCTION__);
137 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
138 /* SLBs are pre-loaded for context switch, so
139 * we should never get here!
141 printk("%s: invalid access during switch!\n", __func__);
144 esid = (ea & ESID_MASK) | SLB_ESID_V;
146 switch(REGION_ID(ea)) {
148 #ifdef CONFIG_PPC_MM_SLICES
149 psize = get_slice_psize(mm, ea);
151 psize = mm->context.user_psize;
153 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
156 case VMALLOC_REGION_ID:
157 if (ea < VMALLOC_END)
158 psize = mmu_vmalloc_psize;
160 psize = mmu_io_psize;
161 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
164 case KERNEL_REGION_ID:
165 psize = mmu_linear_psize;
166 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
170 /* Future: support kernel segments so that drivers
173 pr_debug("invalid region access at %016lx\n", ea);
176 llp = mmu_psize_defs[psize].sllp;
178 out_be64(&priv2->slb_index_W, spu->slb_replace);
179 out_be64(&priv2->slb_vsid_RW, vsid | llp);
180 out_be64(&priv2->slb_esid_RW, esid);
183 if (spu->slb_replace >= 8)
184 spu->slb_replace = 0;
186 spu_restart_dma(spu);
187 spu->stats.slb_flt++;
191 extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
192 static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
194 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
196 /* Handle kernel space hash faults immediately.
197 User hash faults need to be deferred to process context. */
198 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
199 && REGION_ID(ea) != USER_REGION_ID
200 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
201 spu_restart_dma(spu);
205 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
206 printk("%s: invalid access during switch!\n", __func__);
213 spu->stop_callback(spu);
218 spu_irq_class_0(int irq, void *data)
223 spu->class_0_pending = 1;
224 spu->stop_callback(spu);
230 spu_irq_class_0_bottom(struct spu *spu)
232 unsigned long stat, mask;
235 spu->class_0_pending = 0;
237 spin_lock_irqsave(&spu->register_lock, flags);
238 mask = spu_int_mask_get(spu, 0);
239 stat = spu_int_stat_get(spu, 0);
243 if (stat & 1) /* invalid DMA alignment */
244 __spu_trap_dma_align(spu);
246 if (stat & 2) /* invalid MFC DMA */
247 __spu_trap_invalid_dma(spu);
249 if (stat & 4) /* error on SPU */
250 __spu_trap_error(spu);
252 spu_int_stat_clear(spu, 0, stat);
253 spin_unlock_irqrestore(&spu->register_lock, flags);
255 return (stat & 0x7) ? -EIO : 0;
257 EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
260 spu_irq_class_1(int irq, void *data)
263 unsigned long stat, mask, dar, dsisr;
267 /* atomically read & clear class1 status. */
268 spin_lock(&spu->register_lock);
269 mask = spu_int_mask_get(spu, 1);
270 stat = spu_int_stat_get(spu, 1) & mask;
271 dar = spu_mfc_dar_get(spu);
272 dsisr = spu_mfc_dsisr_get(spu);
273 if (stat & 2) /* mapping fault */
274 spu_mfc_dsisr_set(spu, 0ul);
275 spu_int_stat_clear(spu, 1, stat);
276 spin_unlock(&spu->register_lock);
277 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
280 if (stat & 1) /* segment fault */
281 __spu_trap_data_seg(spu, dar);
283 if (stat & 2) { /* mapping fault */
284 __spu_trap_data_map(spu, dar, dsisr);
287 if (stat & 4) /* ls compare & suspend on get */
290 if (stat & 8) /* ls compare & suspend on put */
293 return stat ? IRQ_HANDLED : IRQ_NONE;
297 spu_irq_class_2(int irq, void *data)
304 spin_lock(&spu->register_lock);
305 stat = spu_int_stat_get(spu, 2);
306 mask = spu_int_mask_get(spu, 2);
307 /* ignore interrupts we're not waiting for */
310 * mailbox interrupts (0x1 and 0x10) are level triggered.
311 * mask them now before acknowledging.
314 spu_int_mask_and(spu, 2, ~(stat & 0x11));
315 /* acknowledge all interrupts before the callbacks */
316 spu_int_stat_clear(spu, 2, stat);
317 spin_unlock(&spu->register_lock);
319 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
321 if (stat & 1) /* PPC core mailbox */
322 spu->ibox_callback(spu);
324 if (stat & 2) /* SPU stop-and-signal */
325 spu->stop_callback(spu);
327 if (stat & 4) /* SPU halted */
328 spu->stop_callback(spu);
330 if (stat & 8) /* DMA tag group complete */
331 spu->mfc_callback(spu);
333 if (stat & 0x10) /* SPU mailbox threshold */
334 spu->wbox_callback(spu);
336 spu->stats.class2_intr++;
337 return stat ? IRQ_HANDLED : IRQ_NONE;
340 static int spu_request_irqs(struct spu *spu)
344 if (spu->irqs[0] != NO_IRQ) {
345 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
347 ret = request_irq(spu->irqs[0], spu_irq_class_0,
353 if (spu->irqs[1] != NO_IRQ) {
354 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
356 ret = request_irq(spu->irqs[1], spu_irq_class_1,
362 if (spu->irqs[2] != NO_IRQ) {
363 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
365 ret = request_irq(spu->irqs[2], spu_irq_class_2,
374 if (spu->irqs[1] != NO_IRQ)
375 free_irq(spu->irqs[1], spu);
377 if (spu->irqs[0] != NO_IRQ)
378 free_irq(spu->irqs[0], spu);
383 static void spu_free_irqs(struct spu *spu)
385 if (spu->irqs[0] != NO_IRQ)
386 free_irq(spu->irqs[0], spu);
387 if (spu->irqs[1] != NO_IRQ)
388 free_irq(spu->irqs[1], spu);
389 if (spu->irqs[2] != NO_IRQ)
390 free_irq(spu->irqs[2], spu);
393 static void spu_init_channels(struct spu *spu)
395 static const struct {
399 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
400 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
402 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
403 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
404 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
406 struct spu_priv2 __iomem *priv2;
411 /* initialize all channel data to zero */
412 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
415 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
416 for (count = 0; count < zero_list[i].count; count++)
417 out_be64(&priv2->spu_chnldata_RW, 0);
420 /* initialize channel counts to meaningful values */
421 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
422 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
423 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
427 struct spu *spu_alloc_node(int node)
429 struct spu *spu = NULL;
431 mutex_lock(&spu_mutex);
432 if (!list_empty(&spu_list[node])) {
433 spu = list_entry(spu_list[node].next, struct spu, list);
434 list_del_init(&spu->list);
435 pr_debug("Got SPU %d %d\n", spu->number, spu->node);
437 mutex_unlock(&spu_mutex);
440 spu_init_channels(spu);
443 EXPORT_SYMBOL_GPL(spu_alloc_node);
445 struct spu *spu_alloc(void)
447 struct spu *spu = NULL;
450 for (node = 0; node < MAX_NUMNODES; node++) {
451 spu = spu_alloc_node(node);
459 void spu_free(struct spu *spu)
461 mutex_lock(&spu_mutex);
462 list_add_tail(&spu->list, &spu_list[spu->node]);
463 mutex_unlock(&spu_mutex);
465 EXPORT_SYMBOL_GPL(spu_free);
467 static int spu_shutdown(struct sys_device *sysdev)
469 struct spu *spu = container_of(sysdev, struct spu, sysdev);
472 spu_destroy_spu(spu);
476 struct sysdev_class spu_sysdev_class = {
477 set_kset_name("spu"),
478 .shutdown = spu_shutdown,
481 int spu_add_sysdev_attr(struct sysdev_attribute *attr)
484 mutex_lock(&spu_mutex);
486 list_for_each_entry(spu, &spu_full_list, full_list)
487 sysdev_create_file(&spu->sysdev, attr);
489 mutex_unlock(&spu_mutex);
492 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
494 int spu_add_sysdev_attr_group(struct attribute_group *attrs)
497 mutex_lock(&spu_mutex);
499 list_for_each_entry(spu, &spu_full_list, full_list)
500 sysfs_create_group(&spu->sysdev.kobj, attrs);
502 mutex_unlock(&spu_mutex);
505 EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
508 void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
511 mutex_lock(&spu_mutex);
513 list_for_each_entry(spu, &spu_full_list, full_list)
514 sysdev_remove_file(&spu->sysdev, attr);
516 mutex_unlock(&spu_mutex);
518 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
520 void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
523 mutex_lock(&spu_mutex);
525 list_for_each_entry(spu, &spu_full_list, full_list)
526 sysfs_remove_group(&spu->sysdev.kobj, attrs);
528 mutex_unlock(&spu_mutex);
530 EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
532 static int spu_create_sysdev(struct spu *spu)
536 spu->sysdev.id = spu->number;
537 spu->sysdev.cls = &spu_sysdev_class;
538 ret = sysdev_register(&spu->sysdev);
540 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
545 sysfs_add_device_to_node(&spu->sysdev, spu->node);
550 static int __init create_spu(void *data)
558 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
562 spin_lock_init(&spu->register_lock);
563 mutex_lock(&spu_mutex);
564 spu->number = number++;
565 mutex_unlock(&spu_mutex);
567 ret = spu_create_spu(spu, data);
572 spu_mfc_sdr_setup(spu);
573 spu_mfc_sr1_set(spu, 0x33);
574 ret = spu_request_irqs(spu);
578 ret = spu_create_sysdev(spu);
582 mutex_lock(&spu_mutex);
583 spin_lock_irqsave(&spu_list_lock, flags);
584 list_add(&spu->list, &spu_list[spu->node]);
585 list_add(&spu->full_list, &spu_full_list);
586 spin_unlock_irqrestore(&spu_list_lock, flags);
587 mutex_unlock(&spu_mutex);
589 spu->stats.utilization_state = SPU_UTIL_IDLE;
590 spu->stats.tstamp = jiffies;
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)
611 unsigned long long time = spu->stats.times[state];
613 if (spu->stats.utilization_state == state)
614 time += jiffies - spu->stats.tstamp;
616 return jiffies_to_msecs(time);
620 static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
622 struct spu *spu = container_of(sysdev, struct spu, sysdev);
624 return sprintf(buf, "%s %llu %llu %llu %llu "
625 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
626 spu_state_names[spu->stats.utilization_state],
627 spu_acct_time(spu, SPU_UTIL_USER),
628 spu_acct_time(spu, SPU_UTIL_SYSTEM),
629 spu_acct_time(spu, SPU_UTIL_IOWAIT),
630 spu_acct_time(spu, SPU_UTIL_IDLE),
631 spu->stats.vol_ctx_switch,
632 spu->stats.invol_ctx_switch,
637 spu->stats.class2_intr,
638 spu->stats.libassist);
641 static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
643 static int __init init_spu_base(void)
647 for (i = 0; i < MAX_NUMNODES; i++)
648 INIT_LIST_HEAD(&spu_list[i]);
650 if (!spu_management_ops)
653 /* create sysdev class for spus */
654 ret = sysdev_class_register(&spu_sysdev_class);
658 ret = spu_enumerate_spus(create_spu);
661 printk(KERN_WARNING "%s: Error initializing spus\n",
663 goto out_unregister_sysdev_class;
668 * We cannot put the forward declaration in
669 * <linux/linux_logo.h> because of conflicting session type
670 * conflicts for const and __initdata with different compiler
673 extern const struct linux_logo logo_spe_clut224;
675 fb_append_extra_logo(&logo_spe_clut224, ret);
678 xmon_register_spus(&spu_full_list);
680 spu_add_sysdev_attr(&attr_stat);
684 out_unregister_sysdev_class:
685 sysdev_class_unregister(&spu_sysdev_class);
690 module_init(init_spu_base);
692 MODULE_LICENSE("GPL");
693 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");