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