Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Low-level SPU handling |
| 3 | * |
| 4 | * (C) Copyright IBM Deutschland Entwicklung GmbH 2005 |
| 5 | * |
| 6 | * Author: Arnd Bergmann <arndb@de.ibm.com> |
| 7 | * |
| 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) |
| 11 | * any later version. |
| 12 | * |
| 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. |
| 17 | * |
| 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. |
| 21 | */ |
| 22 | |
Arnd Bergmann | 3b3d22c | 2005-12-05 22:52:24 -0500 | [diff] [blame] | 23 | #undef DEBUG |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 24 | |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/module.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 28 | #include <linux/ptrace.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/wait.h> |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 31 | #include <linux/mm.h> |
| 32 | #include <linux/io.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Geert Uytterhoeven | bce9451 | 2007-07-17 04:05:52 -0700 | [diff] [blame] | 34 | #include <linux/linux_logo.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 35 | #include <asm/spu.h> |
Geoff Levand | 540270d | 2006-06-19 20:33:29 +0200 | [diff] [blame] | 36 | #include <asm/spu_priv1.h> |
Michael Ellerman | ff8a8f2 | 2006-10-24 18:31:27 +0200 | [diff] [blame] | 37 | #include <asm/xmon.h> |
Arnd Bergmann | 3ad216c | 2007-07-20 21:39:46 +0200 | [diff] [blame] | 38 | #include <asm/prom.h> |
| 39 | #include "spu_priv1_mmio.h" |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 40 | |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 41 | const struct spu_management_ops *spu_management_ops; |
Jeremy Kerr | ccf17e9 | 2007-04-23 21:08:29 +0200 | [diff] [blame] | 42 | EXPORT_SYMBOL_GPL(spu_management_ops); |
| 43 | |
Geoff Levand | 540270d | 2006-06-19 20:33:29 +0200 | [diff] [blame] | 44 | const struct spu_priv1_ops *spu_priv1_ops; |
| 45 | |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 46 | static LIST_HEAD(spu_full_list); |
| 47 | static DEFINE_MUTEX(spu_mutex); |
Thomas Gleixner | 057b184 | 2007-04-29 16:10:39 +0000 | [diff] [blame] | 48 | static DEFINE_SPINLOCK(spu_list_lock); |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 49 | |
Geoff Levand | 540270d | 2006-06-19 20:33:29 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL_GPL(spu_priv1_ops); |
| 51 | |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 52 | void spu_invalidate_slbs(struct spu *spu) |
| 53 | { |
| 54 | struct spu_priv2 __iomem *priv2 = spu->priv2; |
| 55 | |
| 56 | if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK) |
| 57 | out_be64(&priv2->slb_invalidate_all_W, 0UL); |
| 58 | } |
| 59 | EXPORT_SYMBOL_GPL(spu_invalidate_slbs); |
| 60 | |
| 61 | /* This is called by the MM core when a segment size is changed, to |
| 62 | * request a flush of all the SPEs using a given mm |
| 63 | */ |
| 64 | void spu_flush_all_slbs(struct mm_struct *mm) |
| 65 | { |
| 66 | struct spu *spu; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | spin_lock_irqsave(&spu_list_lock, flags); |
| 70 | list_for_each_entry(spu, &spu_full_list, full_list) { |
| 71 | if (spu->mm == mm) |
| 72 | spu_invalidate_slbs(spu); |
| 73 | } |
| 74 | spin_unlock_irqrestore(&spu_list_lock, flags); |
| 75 | } |
| 76 | |
| 77 | /* The hack below stinks... try to do something better one of |
| 78 | * these days... Does it even work properly with NR_CPUS == 1 ? |
| 79 | */ |
| 80 | static inline void mm_needs_global_tlbie(struct mm_struct *mm) |
| 81 | { |
| 82 | int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1; |
| 83 | |
| 84 | /* Global TLBIE broadcast required with SPEs. */ |
| 85 | __cpus_setall(&mm->cpu_vm_mask, nr); |
| 86 | } |
| 87 | |
| 88 | void spu_associate_mm(struct spu *spu, struct mm_struct *mm) |
| 89 | { |
| 90 | unsigned long flags; |
| 91 | |
| 92 | spin_lock_irqsave(&spu_list_lock, flags); |
| 93 | spu->mm = mm; |
| 94 | spin_unlock_irqrestore(&spu_list_lock, flags); |
| 95 | if (mm) |
| 96 | mm_needs_global_tlbie(mm); |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(spu_associate_mm); |
| 99 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 100 | static int __spu_trap_invalid_dma(struct spu *spu) |
| 101 | { |
| 102 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 103 | spu->dma_callback(spu, SPE_EVENT_INVALID_DMA); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int __spu_trap_dma_align(struct spu *spu) |
| 108 | { |
| 109 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 110 | spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int __spu_trap_error(struct spu *spu) |
| 115 | { |
| 116 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 117 | spu->dma_callback(spu, SPE_EVENT_SPE_ERROR); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void spu_restart_dma(struct spu *spu) |
| 122 | { |
| 123 | struct spu_priv2 __iomem *priv2 = spu->priv2; |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 124 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 125 | if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags)) |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 126 | out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static int __spu_trap_data_seg(struct spu *spu, unsigned long ea) |
| 130 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 131 | struct spu_priv2 __iomem *priv2 = spu->priv2; |
| 132 | struct mm_struct *mm = spu->mm; |
arnd@arndb.de | 724bd80 | 2006-06-19 20:33:23 +0200 | [diff] [blame] | 133 | u64 esid, vsid, llp; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 134 | int psize; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 135 | |
| 136 | pr_debug("%s\n", __FUNCTION__); |
| 137 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 138 | if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 139 | /* SLBs are pre-loaded for context switch, so |
| 140 | * we should never get here! |
| 141 | */ |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 142 | printk("%s: invalid access during switch!\n", __func__); |
| 143 | return 1; |
| 144 | } |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 145 | esid = (ea & ESID_MASK) | SLB_ESID_V; |
| 146 | |
| 147 | switch(REGION_ID(ea)) { |
| 148 | case USER_REGION_ID: |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 149 | #ifdef CONFIG_PPC_MM_SLICES |
| 150 | psize = get_slice_psize(mm, ea); |
| 151 | #else |
| 152 | psize = mm->context.user_psize; |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 153 | #endif |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 154 | vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) | |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 155 | SLB_VSID_USER; |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 156 | break; |
| 157 | case VMALLOC_REGION_ID: |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 158 | if (ea < VMALLOC_END) |
| 159 | psize = mmu_vmalloc_psize; |
| 160 | else |
| 161 | psize = mmu_io_psize; |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 162 | vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 163 | SLB_VSID_KERNEL; |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 164 | break; |
| 165 | case KERNEL_REGION_ID: |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 166 | psize = mmu_linear_psize; |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 167 | vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 168 | SLB_VSID_KERNEL; |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 169 | break; |
| 170 | default: |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 171 | /* Future: support kernel segments so that drivers |
| 172 | * can use SPUs. |
| 173 | */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 174 | pr_debug("invalid region access at %016lx\n", ea); |
| 175 | return 1; |
| 176 | } |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 177 | llp = mmu_psize_defs[psize].sllp; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 178 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 179 | out_be64(&priv2->slb_index_W, spu->slb_replace); |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 180 | out_be64(&priv2->slb_vsid_RW, vsid | llp); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 181 | out_be64(&priv2->slb_esid_RW, esid); |
| 182 | |
| 183 | spu->slb_replace++; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 184 | if (spu->slb_replace >= 8) |
| 185 | spu->slb_replace = 0; |
| 186 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 187 | spu_restart_dma(spu); |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 188 | spu->stats.slb_flt++; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 192 | extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 193 | static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 194 | { |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 195 | pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 196 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 197 | /* Handle kernel space hash faults immediately. |
| 198 | User hash faults need to be deferred to process context. */ |
| 199 | if ((dsisr & MFC_DSISR_PTE_NOT_FOUND) |
| 200 | && REGION_ID(ea) != USER_REGION_ID |
| 201 | && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) { |
| 202 | spu_restart_dma(spu); |
| 203 | return 0; |
| 204 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 205 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 206 | if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) { |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 207 | printk("%s: invalid access during switch!\n", __func__); |
| 208 | return 1; |
| 209 | } |
| 210 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 211 | spu->dar = ea; |
| 212 | spu->dsisr = dsisr; |
| 213 | mb(); |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 214 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static irqreturn_t |
Olaf Hering | f5a9245 | 2006-10-06 22:52:16 +0200 | [diff] [blame] | 219 | spu_irq_class_0(int irq, void *data) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 220 | { |
| 221 | struct spu *spu; |
| 222 | |
| 223 | spu = data; |
| 224 | spu->class_0_pending = 1; |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 225 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 226 | |
| 227 | return IRQ_HANDLED; |
| 228 | } |
| 229 | |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 230 | int |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 231 | spu_irq_class_0_bottom(struct spu *spu) |
| 232 | { |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 233 | unsigned long stat, mask; |
Ishizaki Kou | 3650cfe | 2007-01-12 09:52:41 +0900 | [diff] [blame] | 234 | unsigned long flags; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 235 | |
| 236 | spu->class_0_pending = 0; |
| 237 | |
Ishizaki Kou | 3650cfe | 2007-01-12 09:52:41 +0900 | [diff] [blame] | 238 | spin_lock_irqsave(&spu->register_lock, flags); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 239 | mask = spu_int_mask_get(spu, 0); |
| 240 | stat = spu_int_stat_get(spu, 0); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 241 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 242 | stat &= mask; |
| 243 | |
Arnd Bergmann | 2cd90bc | 2006-06-23 20:57:50 +0200 | [diff] [blame] | 244 | if (stat & 1) /* invalid DMA alignment */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 245 | __spu_trap_dma_align(spu); |
| 246 | |
Arnd Bergmann | 2cd90bc | 2006-06-23 20:57:50 +0200 | [diff] [blame] | 247 | if (stat & 2) /* invalid MFC DMA */ |
| 248 | __spu_trap_invalid_dma(spu); |
| 249 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 250 | if (stat & 4) /* error on SPU */ |
| 251 | __spu_trap_error(spu); |
| 252 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 253 | spu_int_stat_clear(spu, 0, stat); |
Ishizaki Kou | 3650cfe | 2007-01-12 09:52:41 +0900 | [diff] [blame] | 254 | spin_unlock_irqrestore(&spu->register_lock, flags); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 255 | |
| 256 | return (stat & 0x7) ? -EIO : 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 257 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 258 | EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 259 | |
| 260 | static irqreturn_t |
Olaf Hering | f5a9245 | 2006-10-06 22:52:16 +0200 | [diff] [blame] | 261 | spu_irq_class_1(int irq, void *data) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 262 | { |
| 263 | struct spu *spu; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 264 | unsigned long stat, mask, dar, dsisr; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 265 | |
| 266 | spu = data; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 267 | |
| 268 | /* atomically read & clear class1 status. */ |
| 269 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 270 | mask = spu_int_mask_get(spu, 1); |
| 271 | stat = spu_int_stat_get(spu, 1) & mask; |
| 272 | dar = spu_mfc_dar_get(spu); |
| 273 | dsisr = spu_mfc_dsisr_get(spu); |
Arnd Bergmann | 3830734 | 2005-12-09 19:04:18 +0100 | [diff] [blame] | 274 | if (stat & 2) /* mapping fault */ |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 275 | spu_mfc_dsisr_set(spu, 0ul); |
| 276 | spu_int_stat_clear(spu, 1, stat); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 277 | spin_unlock(&spu->register_lock); |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 278 | pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat, |
| 279 | dar, dsisr); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 280 | |
| 281 | if (stat & 1) /* segment fault */ |
| 282 | __spu_trap_data_seg(spu, dar); |
| 283 | |
| 284 | if (stat & 2) { /* mapping fault */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 285 | __spu_trap_data_map(spu, dar, dsisr); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (stat & 4) /* ls compare & suspend on get */ |
| 289 | ; |
| 290 | |
| 291 | if (stat & 8) /* ls compare & suspend on put */ |
| 292 | ; |
| 293 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 294 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 295 | } |
| 296 | |
| 297 | static irqreturn_t |
Olaf Hering | f5a9245 | 2006-10-06 22:52:16 +0200 | [diff] [blame] | 298 | spu_irq_class_2(int irq, void *data) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 299 | { |
| 300 | struct spu *spu; |
| 301 | unsigned long stat; |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 302 | unsigned long mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 303 | |
| 304 | spu = data; |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 305 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 306 | stat = spu_int_stat_get(spu, 2); |
| 307 | mask = spu_int_mask_get(spu, 2); |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 308 | /* ignore interrupts we're not waiting for */ |
| 309 | stat &= mask; |
| 310 | /* |
| 311 | * mailbox interrupts (0x1 and 0x10) are level triggered. |
| 312 | * mask them now before acknowledging. |
| 313 | */ |
| 314 | if (stat & 0x11) |
| 315 | spu_int_mask_and(spu, 2, ~(stat & 0x11)); |
| 316 | /* acknowledge all interrupts before the callbacks */ |
| 317 | spu_int_stat_clear(spu, 2, stat); |
| 318 | spin_unlock(&spu->register_lock); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 319 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 320 | pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 321 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 322 | if (stat & 1) /* PPC core mailbox */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 323 | spu->ibox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 324 | |
| 325 | if (stat & 2) /* SPU stop-and-signal */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 326 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 327 | |
| 328 | if (stat & 4) /* SPU halted */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 329 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 330 | |
| 331 | if (stat & 8) /* DMA tag group complete */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 332 | spu->mfc_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 333 | |
| 334 | if (stat & 0x10) /* SPU mailbox threshold */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 335 | spu->wbox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 336 | |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 337 | spu->stats.class2_intr++; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 338 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 339 | } |
| 340 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 341 | static int spu_request_irqs(struct spu *spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 342 | { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 343 | int ret = 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 344 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 345 | if (spu->irqs[0] != NO_IRQ) { |
| 346 | snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", |
| 347 | spu->number); |
| 348 | ret = request_irq(spu->irqs[0], spu_irq_class_0, |
| 349 | IRQF_DISABLED, |
| 350 | spu->irq_c0, spu); |
| 351 | if (ret) |
| 352 | goto bail0; |
| 353 | } |
| 354 | if (spu->irqs[1] != NO_IRQ) { |
| 355 | snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", |
| 356 | spu->number); |
| 357 | ret = request_irq(spu->irqs[1], spu_irq_class_1, |
| 358 | IRQF_DISABLED, |
| 359 | spu->irq_c1, spu); |
| 360 | if (ret) |
| 361 | goto bail1; |
| 362 | } |
| 363 | if (spu->irqs[2] != NO_IRQ) { |
| 364 | snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", |
| 365 | spu->number); |
| 366 | ret = request_irq(spu->irqs[2], spu_irq_class_2, |
| 367 | IRQF_DISABLED, |
| 368 | spu->irq_c2, spu); |
| 369 | if (ret) |
| 370 | goto bail2; |
| 371 | } |
| 372 | return 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 373 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 374 | bail2: |
| 375 | if (spu->irqs[1] != NO_IRQ) |
| 376 | free_irq(spu->irqs[1], spu); |
| 377 | bail1: |
| 378 | if (spu->irqs[0] != NO_IRQ) |
| 379 | free_irq(spu->irqs[0], spu); |
| 380 | bail0: |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 381 | return ret; |
| 382 | } |
| 383 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 384 | static void spu_free_irqs(struct spu *spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 385 | { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 386 | if (spu->irqs[0] != NO_IRQ) |
| 387 | free_irq(spu->irqs[0], spu); |
| 388 | if (spu->irqs[1] != NO_IRQ) |
| 389 | free_irq(spu->irqs[1], spu); |
| 390 | if (spu->irqs[2] != NO_IRQ) |
| 391 | free_irq(spu->irqs[2], spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 394 | static void spu_init_channels(struct spu *spu) |
| 395 | { |
| 396 | static const struct { |
| 397 | unsigned channel; |
| 398 | unsigned count; |
| 399 | } zero_list[] = { |
| 400 | { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, }, |
| 401 | { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, }, |
| 402 | }, count_list[] = { |
| 403 | { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, }, |
| 404 | { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, }, |
| 405 | { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, }, |
| 406 | }; |
Arnd Bergmann | 6ff730c | 2006-01-04 20:31:31 +0100 | [diff] [blame] | 407 | struct spu_priv2 __iomem *priv2; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 408 | int i; |
| 409 | |
| 410 | priv2 = spu->priv2; |
| 411 | |
| 412 | /* initialize all channel data to zero */ |
| 413 | for (i = 0; i < ARRAY_SIZE(zero_list); i++) { |
| 414 | int count; |
| 415 | |
| 416 | out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel); |
| 417 | for (count = 0; count < zero_list[i].count; count++) |
| 418 | out_be64(&priv2->spu_chnldata_RW, 0); |
| 419 | } |
| 420 | |
| 421 | /* initialize channel counts to meaningful values */ |
| 422 | for (i = 0; i < ARRAY_SIZE(count_list); i++) { |
| 423 | out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel); |
| 424 | out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count); |
| 425 | } |
| 426 | } |
| 427 | |
Arnd Bergmann | cbc23d3 | 2007-07-20 21:39:49 +0200 | [diff] [blame] | 428 | struct spu *spu_alloc_spu(struct spu *req_spu) |
| 429 | { |
| 430 | struct spu *spu, *ret = NULL; |
| 431 | |
| 432 | mutex_lock(&spu_mutex); |
| 433 | list_for_each_entry(spu, &cbe_spu_info[req_spu->node].free_spus, list) { |
| 434 | if (spu == req_spu) { |
| 435 | list_del_init(&spu->list); |
| 436 | pr_debug("Got SPU %d %d\n", spu->number, spu->node); |
| 437 | spu_init_channels(spu); |
| 438 | ret = spu; |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | mutex_unlock(&spu_mutex); |
| 443 | return ret; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(spu_alloc_spu); |
| 446 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 447 | struct spu *spu_alloc_node(int node) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 448 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 449 | struct spu *spu = NULL; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 450 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 451 | mutex_lock(&spu_mutex); |
Arnd Bergmann | aa6d5b2 | 2007-07-20 21:39:44 +0200 | [diff] [blame] | 452 | if (!list_empty(&cbe_spu_info[node].free_spus)) { |
| 453 | spu = list_entry(cbe_spu_info[node].free_spus.next, struct spu, |
| 454 | list); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 455 | list_del_init(&spu->list); |
Geoff Levand | cc21a66 | 2006-10-24 18:31:15 +0200 | [diff] [blame] | 456 | pr_debug("Got SPU %d %d\n", spu->number, spu->node); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 457 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 458 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 459 | |
Christoph Hellwig | 62c05d5 | 2007-04-23 21:08:14 +0200 | [diff] [blame] | 460 | if (spu) |
| 461 | spu_init_channels(spu); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 462 | return spu; |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(spu_alloc_node); |
| 465 | |
| 466 | struct spu *spu_alloc(void) |
| 467 | { |
| 468 | struct spu *spu = NULL; |
| 469 | int node; |
| 470 | |
| 471 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 472 | spu = spu_alloc_node(node); |
| 473 | if (spu) |
| 474 | break; |
| 475 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 476 | |
| 477 | return spu; |
| 478 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 479 | |
| 480 | void spu_free(struct spu *spu) |
| 481 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 482 | mutex_lock(&spu_mutex); |
Arnd Bergmann | aa6d5b2 | 2007-07-20 21:39:44 +0200 | [diff] [blame] | 483 | list_add_tail(&spu->list, &cbe_spu_info[spu->node].free_spus); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 484 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 485 | } |
Arnd Bergmann | 39c73c3 | 2005-12-05 22:52:21 -0500 | [diff] [blame] | 486 | EXPORT_SYMBOL_GPL(spu_free); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 487 | |
Geoff Levand | 6deac06 | 2007-06-16 07:17:32 +1000 | [diff] [blame] | 488 | static int spu_shutdown(struct sys_device *sysdev) |
| 489 | { |
| 490 | struct spu *spu = container_of(sysdev, struct spu, sysdev); |
| 491 | |
| 492 | spu_free_irqs(spu); |
| 493 | spu_destroy_spu(spu); |
| 494 | return 0; |
| 495 | } |
| 496 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 497 | struct sysdev_class spu_sysdev_class = { |
Geoff Levand | 6deac06 | 2007-06-16 07:17:32 +1000 | [diff] [blame] | 498 | set_kset_name("spu"), |
| 499 | .shutdown = spu_shutdown, |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 500 | }; |
| 501 | |
Christian Krafft | e570beb | 2006-10-24 18:31:23 +0200 | [diff] [blame] | 502 | int spu_add_sysdev_attr(struct sysdev_attribute *attr) |
| 503 | { |
| 504 | struct spu *spu; |
| 505 | mutex_lock(&spu_mutex); |
| 506 | |
| 507 | list_for_each_entry(spu, &spu_full_list, full_list) |
| 508 | sysdev_create_file(&spu->sysdev, attr); |
| 509 | |
| 510 | mutex_unlock(&spu_mutex); |
| 511 | return 0; |
| 512 | } |
| 513 | EXPORT_SYMBOL_GPL(spu_add_sysdev_attr); |
| 514 | |
| 515 | int spu_add_sysdev_attr_group(struct attribute_group *attrs) |
| 516 | { |
| 517 | struct spu *spu; |
| 518 | mutex_lock(&spu_mutex); |
| 519 | |
| 520 | list_for_each_entry(spu, &spu_full_list, full_list) |
| 521 | sysfs_create_group(&spu->sysdev.kobj, attrs); |
| 522 | |
| 523 | mutex_unlock(&spu_mutex); |
| 524 | return 0; |
| 525 | } |
| 526 | EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group); |
| 527 | |
| 528 | |
| 529 | void spu_remove_sysdev_attr(struct sysdev_attribute *attr) |
| 530 | { |
| 531 | struct spu *spu; |
| 532 | mutex_lock(&spu_mutex); |
| 533 | |
| 534 | list_for_each_entry(spu, &spu_full_list, full_list) |
| 535 | sysdev_remove_file(&spu->sysdev, attr); |
| 536 | |
| 537 | mutex_unlock(&spu_mutex); |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr); |
| 540 | |
| 541 | void spu_remove_sysdev_attr_group(struct attribute_group *attrs) |
| 542 | { |
| 543 | struct spu *spu; |
| 544 | mutex_lock(&spu_mutex); |
| 545 | |
| 546 | list_for_each_entry(spu, &spu_full_list, full_list) |
| 547 | sysfs_remove_group(&spu->sysdev.kobj, attrs); |
| 548 | |
| 549 | mutex_unlock(&spu_mutex); |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group); |
| 552 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 553 | static int spu_create_sysdev(struct spu *spu) |
| 554 | { |
| 555 | int ret; |
| 556 | |
| 557 | spu->sysdev.id = spu->number; |
| 558 | spu->sysdev.cls = &spu_sysdev_class; |
| 559 | ret = sysdev_register(&spu->sysdev); |
| 560 | if (ret) { |
| 561 | printk(KERN_ERR "Can't register SPU %d with sysfs\n", |
| 562 | spu->number); |
| 563 | return ret; |
| 564 | } |
| 565 | |
Geoff Levand | 0021550 | 2006-11-20 18:45:02 +0100 | [diff] [blame] | 566 | sysfs_add_device_to_node(&spu->sysdev, spu->node); |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 571 | static int __init create_spu(void *data) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 572 | { |
| 573 | struct spu *spu; |
| 574 | int ret; |
| 575 | static int number; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 576 | unsigned long flags; |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 577 | struct timespec ts; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 578 | |
| 579 | ret = -ENOMEM; |
Jeremy Kerr | ecec217 | 2006-06-19 20:33:26 +0200 | [diff] [blame] | 580 | spu = kzalloc(sizeof (*spu), GFP_KERNEL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 581 | if (!spu) |
| 582 | goto out; |
| 583 | |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 584 | spin_lock_init(&spu->register_lock); |
| 585 | mutex_lock(&spu_mutex); |
| 586 | spu->number = number++; |
| 587 | mutex_unlock(&spu_mutex); |
Benjamin Herrenschmidt | e5267b4 | 2006-10-10 15:14:12 +1000 | [diff] [blame] | 588 | |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 589 | ret = spu_create_spu(spu, data); |
| 590 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 591 | if (ret) |
| 592 | goto out_free; |
| 593 | |
Masato Noguchi | 24f43b3 | 2006-10-24 18:31:14 +0200 | [diff] [blame] | 594 | spu_mfc_sdr_setup(spu); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 595 | spu_mfc_sr1_set(spu, 0x33); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 596 | ret = spu_request_irqs(spu); |
| 597 | if (ret) |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 598 | goto out_destroy; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 599 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 600 | ret = spu_create_sysdev(spu); |
| 601 | if (ret) |
| 602 | goto out_free_irqs; |
| 603 | |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 604 | mutex_lock(&spu_mutex); |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 605 | spin_lock_irqsave(&spu_list_lock, flags); |
Arnd Bergmann | aa6d5b2 | 2007-07-20 21:39:44 +0200 | [diff] [blame] | 606 | list_add(&spu->list, &cbe_spu_info[spu->node].free_spus); |
| 607 | list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus); |
| 608 | cbe_spu_info[spu->node].n_spus++; |
Christian Krafft | e570beb | 2006-10-24 18:31:23 +0200 | [diff] [blame] | 609 | list_add(&spu->full_list, &spu_full_list); |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 610 | spin_unlock_irqrestore(&spu_list_lock, flags); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 611 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 612 | |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 613 | spu->stats.util_state = SPU_UTIL_IDLE_LOADED; |
| 614 | ktime_get_ts(&ts); |
| 615 | spu->stats.tstamp = timespec_to_ns(&ts); |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 616 | |
Arnd Bergmann | 9d92af6 | 2007-07-20 21:39:45 +0200 | [diff] [blame] | 617 | INIT_LIST_HEAD(&spu->aff_list); |
| 618 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 619 | goto out; |
| 620 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 621 | out_free_irqs: |
| 622 | spu_free_irqs(spu); |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 623 | out_destroy: |
| 624 | spu_destroy_spu(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 625 | out_free: |
| 626 | kfree(spu); |
| 627 | out: |
| 628 | return ret; |
| 629 | } |
| 630 | |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 631 | static const char *spu_state_names[] = { |
| 632 | "user", "system", "iowait", "idle" |
| 633 | }; |
| 634 | |
| 635 | static unsigned long long spu_acct_time(struct spu *spu, |
| 636 | enum spu_utilization_state state) |
| 637 | { |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 638 | struct timespec ts; |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 639 | unsigned long long time = spu->stats.times[state]; |
| 640 | |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 641 | /* |
| 642 | * If the spu is idle or the context is stopped, utilization |
| 643 | * statistics are not updated. Apply the time delta from the |
| 644 | * last recorded state of the spu. |
| 645 | */ |
| 646 | if (spu->stats.util_state == state) { |
| 647 | ktime_get_ts(&ts); |
| 648 | time += timespec_to_ns(&ts) - spu->stats.tstamp; |
| 649 | } |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 650 | |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 651 | return time / NSEC_PER_MSEC; |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf) |
| 656 | { |
| 657 | struct spu *spu = container_of(sysdev, struct spu, sysdev); |
| 658 | |
| 659 | return sprintf(buf, "%s %llu %llu %llu %llu " |
| 660 | "%llu %llu %llu %llu %llu %llu %llu %llu\n", |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 661 | spu_state_names[spu->stats.util_state], |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 662 | spu_acct_time(spu, SPU_UTIL_USER), |
| 663 | spu_acct_time(spu, SPU_UTIL_SYSTEM), |
| 664 | spu_acct_time(spu, SPU_UTIL_IOWAIT), |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 665 | spu_acct_time(spu, SPU_UTIL_IDLE_LOADED), |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 666 | spu->stats.vol_ctx_switch, |
| 667 | spu->stats.invol_ctx_switch, |
| 668 | spu->stats.slb_flt, |
| 669 | spu->stats.hash_flt, |
| 670 | spu->stats.min_flt, |
| 671 | spu->stats.maj_flt, |
| 672 | spu->stats.class2_intr, |
| 673 | spu->stats.libassist); |
| 674 | } |
| 675 | |
| 676 | static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL); |
| 677 | |
Arnd Bergmann | aa6d5b2 | 2007-07-20 21:39:44 +0200 | [diff] [blame] | 678 | struct cbe_spu_info cbe_spu_info[MAX_NUMNODES]; |
| 679 | EXPORT_SYMBOL_GPL(cbe_spu_info); |
| 680 | |
Arnd Bergmann | 3ad216c | 2007-07-20 21:39:46 +0200 | [diff] [blame] | 681 | /* Hardcoded affinity idxs for QS20 */ |
| 682 | #define SPES_PER_BE 8 |
| 683 | static int QS20_reg_idxs[SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 }; |
| 684 | static int QS20_reg_memory[SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 }; |
| 685 | |
| 686 | static struct spu *spu_lookup_reg(int node, u32 reg) |
| 687 | { |
| 688 | struct spu *spu; |
| 689 | |
| 690 | list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) { |
| 691 | if (*(u32 *)get_property(spu_devnode(spu), "reg", NULL) == reg) |
| 692 | return spu; |
| 693 | } |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | static void init_aff_QS20_harcoded(void) |
| 698 | { |
| 699 | int node, i; |
| 700 | struct spu *last_spu, *spu; |
| 701 | u32 reg; |
| 702 | |
| 703 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 704 | last_spu = NULL; |
| 705 | for (i = 0; i < SPES_PER_BE; i++) { |
| 706 | reg = QS20_reg_idxs[i]; |
| 707 | spu = spu_lookup_reg(node, reg); |
| 708 | if (!spu) |
| 709 | continue; |
| 710 | spu->has_mem_affinity = QS20_reg_memory[reg]; |
| 711 | if (last_spu) |
| 712 | list_add_tail(&spu->aff_list, |
| 713 | &last_spu->aff_list); |
| 714 | last_spu = spu; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | static int of_has_vicinity(void) |
| 720 | { |
| 721 | struct spu* spu; |
| 722 | |
| 723 | spu = list_entry(cbe_spu_info[0].spus.next, struct spu, cbe_list); |
| 724 | return of_find_property(spu_devnode(spu), "vicinity", NULL) != NULL; |
| 725 | } |
| 726 | |
Arnd Bergmann | 9e7cbcb | 2007-07-20 21:39:50 +0200 | [diff] [blame^] | 727 | static struct spu *aff_devnode_spu(int cbe, struct device_node *dn) |
| 728 | { |
| 729 | struct spu *spu; |
| 730 | |
| 731 | list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) |
| 732 | if (spu_devnode(spu) == dn) |
| 733 | return spu; |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | static struct spu * |
| 738 | aff_node_next_to(int cbe, struct device_node *target, struct device_node *avoid) |
| 739 | { |
| 740 | struct spu *spu; |
| 741 | const phandle *vic_handles; |
| 742 | int lenp, i; |
| 743 | |
| 744 | list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) { |
| 745 | if (spu_devnode(spu) == avoid) |
| 746 | continue; |
| 747 | vic_handles = get_property(spu_devnode(spu), "vicinity", &lenp); |
| 748 | for (i=0; i < (lenp / sizeof(phandle)); i++) { |
| 749 | if (vic_handles[i] == target->linux_phandle) |
| 750 | return spu; |
| 751 | } |
| 752 | } |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | static void init_aff_fw_vicinity_node(int cbe) |
| 757 | { |
| 758 | struct spu *spu, *last_spu; |
| 759 | struct device_node *vic_dn, *last_spu_dn; |
| 760 | phandle avoid_ph; |
| 761 | const phandle *vic_handles; |
| 762 | const char *name; |
| 763 | int lenp, i, added, mem_aff; |
| 764 | |
| 765 | last_spu = list_entry(cbe_spu_info[cbe].spus.next, struct spu, cbe_list); |
| 766 | avoid_ph = 0; |
| 767 | for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) { |
| 768 | last_spu_dn = spu_devnode(last_spu); |
| 769 | vic_handles = get_property(last_spu_dn, "vicinity", &lenp); |
| 770 | |
| 771 | for (i = 0; i < (lenp / sizeof(phandle)); i++) { |
| 772 | if (vic_handles[i] == avoid_ph) |
| 773 | continue; |
| 774 | |
| 775 | vic_dn = of_find_node_by_phandle(vic_handles[i]); |
| 776 | if (!vic_dn) |
| 777 | continue; |
| 778 | |
| 779 | name = get_property(vic_dn, "name", NULL); |
| 780 | if (strcmp(name, "spe") == 0) { |
| 781 | spu = aff_devnode_spu(cbe, vic_dn); |
| 782 | avoid_ph = last_spu_dn->linux_phandle; |
| 783 | } |
| 784 | else { |
| 785 | mem_aff = strcmp(name, "mic-tm") == 0; |
| 786 | spu = aff_node_next_to(cbe, vic_dn, last_spu_dn); |
| 787 | if (!spu) |
| 788 | continue; |
| 789 | if (mem_aff) { |
| 790 | last_spu->has_mem_affinity = 1; |
| 791 | spu->has_mem_affinity = 1; |
| 792 | } |
| 793 | avoid_ph = vic_dn->linux_phandle; |
| 794 | } |
| 795 | list_add_tail(&spu->aff_list, &last_spu->aff_list); |
| 796 | last_spu = spu; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static void init_aff_fw_vicinity(void) |
| 803 | { |
| 804 | int cbe; |
| 805 | |
| 806 | /* sets has_mem_affinity for each spu, as long as the |
| 807 | * spu->aff_list list, linking each spu to its neighbors |
| 808 | */ |
| 809 | for (cbe = 0; cbe < MAX_NUMNODES; cbe++) |
| 810 | init_aff_fw_vicinity_node(cbe); |
| 811 | } |
| 812 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 813 | static int __init init_spu_base(void) |
| 814 | { |
Christoph Hellwig | befdc74 | 2007-04-23 21:08:28 +0200 | [diff] [blame] | 815 | int i, ret = 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 816 | |
Arnd Bergmann | aa6d5b2 | 2007-07-20 21:39:44 +0200 | [diff] [blame] | 817 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 818 | INIT_LIST_HEAD(&cbe_spu_info[i].spus); |
| 819 | INIT_LIST_HEAD(&cbe_spu_info[i].free_spus); |
| 820 | } |
Jeremy Kerr | ccf17e9 | 2007-04-23 21:08:29 +0200 | [diff] [blame] | 821 | |
Stephen Rothwell | da06aa0 | 2006-11-27 19:18:54 +0100 | [diff] [blame] | 822 | if (!spu_management_ops) |
Christoph Hellwig | befdc74 | 2007-04-23 21:08:28 +0200 | [diff] [blame] | 823 | goto out; |
Stephen Rothwell | da06aa0 | 2006-11-27 19:18:54 +0100 | [diff] [blame] | 824 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 825 | /* create sysdev class for spus */ |
| 826 | ret = sysdev_class_register(&spu_sysdev_class); |
| 827 | if (ret) |
Christoph Hellwig | befdc74 | 2007-04-23 21:08:28 +0200 | [diff] [blame] | 828 | goto out; |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 829 | |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 830 | ret = spu_enumerate_spus(create_spu); |
| 831 | |
Geert Uytterhoeven | bce9451 | 2007-07-17 04:05:52 -0700 | [diff] [blame] | 832 | if (ret < 0) { |
Geoff Levand | e28b003 | 2006-11-23 00:46:49 +0100 | [diff] [blame] | 833 | printk(KERN_WARNING "%s: Error initializing spus\n", |
| 834 | __FUNCTION__); |
Christoph Hellwig | befdc74 | 2007-04-23 21:08:28 +0200 | [diff] [blame] | 835 | goto out_unregister_sysdev_class; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 836 | } |
Michael Ellerman | ff8a8f2 | 2006-10-24 18:31:27 +0200 | [diff] [blame] | 837 | |
Geert Uytterhoeven | bce9451 | 2007-07-17 04:05:52 -0700 | [diff] [blame] | 838 | if (ret > 0) { |
| 839 | /* |
| 840 | * We cannot put the forward declaration in |
| 841 | * <linux/linux_logo.h> because of conflicting session type |
| 842 | * conflicts for const and __initdata with different compiler |
| 843 | * versions |
| 844 | */ |
| 845 | extern const struct linux_logo logo_spe_clut224; |
| 846 | |
| 847 | fb_append_extra_logo(&logo_spe_clut224, ret); |
| 848 | } |
| 849 | |
Michael Ellerman | ff8a8f2 | 2006-10-24 18:31:27 +0200 | [diff] [blame] | 850 | xmon_register_spus(&spu_full_list); |
Andre Detsch | 8d2655e | 2007-07-20 21:39:27 +0200 | [diff] [blame] | 851 | crash_register_spus(&spu_full_list); |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 852 | spu_add_sysdev_attr(&attr_stat); |
| 853 | |
Arnd Bergmann | 9e7cbcb | 2007-07-20 21:39:50 +0200 | [diff] [blame^] | 854 | if (of_has_vicinity()) { |
| 855 | init_aff_fw_vicinity(); |
| 856 | } else { |
Arnd Bergmann | 3ad216c | 2007-07-20 21:39:46 +0200 | [diff] [blame] | 857 | long root = of_get_flat_dt_root(); |
| 858 | if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0")) |
| 859 | init_aff_QS20_harcoded(); |
| 860 | } |
| 861 | |
Christoph Hellwig | befdc74 | 2007-04-23 21:08:28 +0200 | [diff] [blame] | 862 | return 0; |
| 863 | |
| 864 | out_unregister_sysdev_class: |
| 865 | sysdev_class_unregister(&spu_sysdev_class); |
| 866 | out: |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 867 | return ret; |
| 868 | } |
| 869 | module_init(init_spu_base); |
| 870 | |
| 871 | MODULE_LICENSE("GPL"); |
| 872 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); |