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> |
| 28 | #include <linux/poll.h> |
| 29 | #include <linux/ptrace.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/wait.h> |
| 32 | |
| 33 | #include <asm/io.h> |
| 34 | #include <asm/prom.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 35 | #include <linux/mutex.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 36 | #include <asm/spu.h> |
| 37 | #include <asm/mmu_context.h> |
| 38 | |
| 39 | #include "interrupt.h" |
| 40 | |
| 41 | static int __spu_trap_invalid_dma(struct spu *spu) |
| 42 | { |
| 43 | pr_debug("%s\n", __FUNCTION__); |
| 44 | force_sig(SIGBUS, /* info, */ current); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int __spu_trap_dma_align(struct spu *spu) |
| 49 | { |
| 50 | pr_debug("%s\n", __FUNCTION__); |
| 51 | force_sig(SIGBUS, /* info, */ current); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int __spu_trap_error(struct spu *spu) |
| 56 | { |
| 57 | pr_debug("%s\n", __FUNCTION__); |
| 58 | force_sig(SIGILL, /* info, */ current); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void spu_restart_dma(struct spu *spu) |
| 63 | { |
| 64 | struct spu_priv2 __iomem *priv2 = spu->priv2; |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 65 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 66 | if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags)) |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 67 | out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int __spu_trap_data_seg(struct spu *spu, unsigned long ea) |
| 71 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 72 | struct spu_priv2 __iomem *priv2 = spu->priv2; |
| 73 | struct mm_struct *mm = spu->mm; |
arnd@arndb.de | 724bd80 | 2006-06-19 20:33:23 +0200 | [diff] [blame] | 74 | u64 esid, vsid, llp; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 75 | |
| 76 | pr_debug("%s\n", __FUNCTION__); |
| 77 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 78 | if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 79 | /* SLBs are pre-loaded for context switch, so |
| 80 | * we should never get here! |
| 81 | */ |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 82 | printk("%s: invalid access during switch!\n", __func__); |
| 83 | return 1; |
| 84 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 85 | if (!mm || (REGION_ID(ea) != USER_REGION_ID)) { |
| 86 | /* Future: support kernel segments so that drivers |
| 87 | * can use SPUs. |
| 88 | */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 89 | pr_debug("invalid region access at %016lx\n", ea); |
| 90 | return 1; |
| 91 | } |
| 92 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 93 | esid = (ea & ESID_MASK) | SLB_ESID_V; |
arnd@arndb.de | 724bd80 | 2006-06-19 20:33:23 +0200 | [diff] [blame] | 94 | #ifdef CONFIG_HUGETLB_PAGE |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 95 | if (in_hugepage_area(mm->context, ea)) |
arnd@arndb.de | 724bd80 | 2006-06-19 20:33:23 +0200 | [diff] [blame] | 96 | llp = mmu_psize_defs[mmu_huge_psize].sllp; |
| 97 | else |
| 98 | #endif |
| 99 | llp = mmu_psize_defs[mmu_virtual_psize].sllp; |
| 100 | vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) | |
| 101 | SLB_VSID_USER | llp; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 102 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 103 | out_be64(&priv2->slb_index_W, spu->slb_replace); |
| 104 | out_be64(&priv2->slb_vsid_RW, vsid); |
| 105 | out_be64(&priv2->slb_esid_RW, esid); |
| 106 | |
| 107 | spu->slb_replace++; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 108 | if (spu->slb_replace >= 8) |
| 109 | spu->slb_replace = 0; |
| 110 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 111 | spu_restart_dma(spu); |
| 112 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 116 | 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] | 117 | 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] | 118 | { |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 119 | pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 120 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 121 | /* Handle kernel space hash faults immediately. |
| 122 | User hash faults need to be deferred to process context. */ |
| 123 | if ((dsisr & MFC_DSISR_PTE_NOT_FOUND) |
| 124 | && REGION_ID(ea) != USER_REGION_ID |
| 125 | && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) { |
| 126 | spu_restart_dma(spu); |
| 127 | return 0; |
| 128 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 129 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 130 | if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) { |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 131 | printk("%s: invalid access during switch!\n", __func__); |
| 132 | return 1; |
| 133 | } |
| 134 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 135 | spu->dar = ea; |
| 136 | spu->dsisr = dsisr; |
| 137 | mb(); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 138 | if (spu->stop_callback) |
| 139 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int __spu_trap_mailbox(struct spu *spu) |
| 144 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 145 | if (spu->ibox_callback) |
| 146 | spu->ibox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 147 | |
| 148 | /* atomically disable SPU mailbox interrupts */ |
| 149 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 150 | spu_int_mask_and(spu, 2, ~0x1); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 151 | spin_unlock(&spu->register_lock); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int __spu_trap_stop(struct spu *spu) |
| 156 | { |
| 157 | pr_debug("%s\n", __FUNCTION__); |
| 158 | spu->stop_code = in_be32(&spu->problem->spu_status_R); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 159 | if (spu->stop_callback) |
| 160 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int __spu_trap_halt(struct spu *spu) |
| 165 | { |
| 166 | pr_debug("%s\n", __FUNCTION__); |
| 167 | spu->stop_code = in_be32(&spu->problem->spu_status_R); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 168 | if (spu->stop_callback) |
| 169 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int __spu_trap_tag_group(struct spu *spu) |
| 174 | { |
| 175 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 176 | spu->mfc_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int __spu_trap_spubox(struct spu *spu) |
| 181 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 182 | if (spu->wbox_callback) |
| 183 | spu->wbox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 184 | |
| 185 | /* atomically disable SPU mailbox interrupts */ |
| 186 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 187 | spu_int_mask_and(spu, 2, ~0x10); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 188 | spin_unlock(&spu->register_lock); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static irqreturn_t |
| 193 | spu_irq_class_0(int irq, void *data, struct pt_regs *regs) |
| 194 | { |
| 195 | struct spu *spu; |
| 196 | |
| 197 | spu = data; |
| 198 | spu->class_0_pending = 1; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 199 | if (spu->stop_callback) |
| 200 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 201 | |
| 202 | return IRQ_HANDLED; |
| 203 | } |
| 204 | |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 205 | int |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 206 | spu_irq_class_0_bottom(struct spu *spu) |
| 207 | { |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 208 | unsigned long stat, mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 209 | |
| 210 | spu->class_0_pending = 0; |
| 211 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 212 | mask = spu_int_mask_get(spu, 0); |
| 213 | stat = spu_int_stat_get(spu, 0); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 214 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 215 | stat &= mask; |
| 216 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 217 | if (stat & 1) /* invalid MFC DMA */ |
| 218 | __spu_trap_invalid_dma(spu); |
| 219 | |
| 220 | if (stat & 2) /* invalid DMA alignment */ |
| 221 | __spu_trap_dma_align(spu); |
| 222 | |
| 223 | if (stat & 4) /* error on SPU */ |
| 224 | __spu_trap_error(spu); |
| 225 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 226 | spu_int_stat_clear(spu, 0, stat); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 227 | |
| 228 | return (stat & 0x7) ? -EIO : 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 229 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 231 | |
| 232 | static irqreturn_t |
| 233 | spu_irq_class_1(int irq, void *data, struct pt_regs *regs) |
| 234 | { |
| 235 | struct spu *spu; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 236 | unsigned long stat, mask, dar, dsisr; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 237 | |
| 238 | spu = data; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 239 | |
| 240 | /* atomically read & clear class1 status. */ |
| 241 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 242 | mask = spu_int_mask_get(spu, 1); |
| 243 | stat = spu_int_stat_get(spu, 1) & mask; |
| 244 | dar = spu_mfc_dar_get(spu); |
| 245 | dsisr = spu_mfc_dsisr_get(spu); |
Arnd Bergmann | 3830734 | 2005-12-09 19:04:18 +0100 | [diff] [blame] | 246 | if (stat & 2) /* mapping fault */ |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 247 | spu_mfc_dsisr_set(spu, 0ul); |
| 248 | spu_int_stat_clear(spu, 1, stat); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 249 | spin_unlock(&spu->register_lock); |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 250 | pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat, |
| 251 | dar, dsisr); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 252 | |
| 253 | if (stat & 1) /* segment fault */ |
| 254 | __spu_trap_data_seg(spu, dar); |
| 255 | |
| 256 | if (stat & 2) { /* mapping fault */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 257 | __spu_trap_data_map(spu, dar, dsisr); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (stat & 4) /* ls compare & suspend on get */ |
| 261 | ; |
| 262 | |
| 263 | if (stat & 8) /* ls compare & suspend on put */ |
| 264 | ; |
| 265 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 266 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 267 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 268 | EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 269 | |
| 270 | static irqreturn_t |
| 271 | spu_irq_class_2(int irq, void *data, struct pt_regs *regs) |
| 272 | { |
| 273 | struct spu *spu; |
| 274 | unsigned long stat; |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 275 | unsigned long mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 276 | |
| 277 | spu = data; |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 278 | stat = spu_int_stat_get(spu, 2); |
| 279 | mask = spu_int_mask_get(spu, 2); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 280 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 281 | pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 282 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 283 | stat &= mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 284 | |
| 285 | if (stat & 1) /* PPC core mailbox */ |
| 286 | __spu_trap_mailbox(spu); |
| 287 | |
| 288 | if (stat & 2) /* SPU stop-and-signal */ |
| 289 | __spu_trap_stop(spu); |
| 290 | |
| 291 | if (stat & 4) /* SPU halted */ |
| 292 | __spu_trap_halt(spu); |
| 293 | |
| 294 | if (stat & 8) /* DMA tag group complete */ |
| 295 | __spu_trap_tag_group(spu); |
| 296 | |
| 297 | if (stat & 0x10) /* SPU mailbox threshold */ |
| 298 | __spu_trap_spubox(spu); |
| 299 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 300 | spu_int_stat_clear(spu, 2, stat); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 301 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 302 | } |
| 303 | |
| 304 | static int |
| 305 | spu_request_irqs(struct spu *spu) |
| 306 | { |
| 307 | int ret; |
| 308 | int irq_base; |
| 309 | |
| 310 | irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET; |
| 311 | |
| 312 | snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number); |
| 313 | ret = request_irq(irq_base + spu->isrc, |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 314 | spu_irq_class_0, SA_INTERRUPT, spu->irq_c0, spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 315 | if (ret) |
| 316 | goto out; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 317 | |
| 318 | snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number); |
| 319 | ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 320 | spu_irq_class_1, SA_INTERRUPT, spu->irq_c1, spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 321 | if (ret) |
| 322 | goto out1; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 323 | |
| 324 | snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number); |
| 325 | ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 326 | spu_irq_class_2, SA_INTERRUPT, spu->irq_c2, spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 327 | if (ret) |
| 328 | goto out2; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 329 | goto out; |
| 330 | |
| 331 | out2: |
| 332 | free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu); |
| 333 | out1: |
| 334 | free_irq(irq_base + spu->isrc, spu); |
| 335 | out: |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | spu_free_irqs(struct spu *spu) |
| 341 | { |
| 342 | int irq_base; |
| 343 | |
| 344 | irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET; |
| 345 | |
| 346 | free_irq(irq_base + spu->isrc, spu); |
| 347 | free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu); |
| 348 | free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu); |
| 349 | } |
| 350 | |
| 351 | static LIST_HEAD(spu_list); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 352 | static DEFINE_MUTEX(spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 353 | |
| 354 | static void spu_init_channels(struct spu *spu) |
| 355 | { |
| 356 | static const struct { |
| 357 | unsigned channel; |
| 358 | unsigned count; |
| 359 | } zero_list[] = { |
| 360 | { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, }, |
| 361 | { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, }, |
| 362 | }, count_list[] = { |
| 363 | { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, }, |
| 364 | { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, }, |
| 365 | { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, }, |
| 366 | }; |
Arnd Bergmann | 6ff730c | 2006-01-04 20:31:31 +0100 | [diff] [blame] | 367 | struct spu_priv2 __iomem *priv2; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 368 | int i; |
| 369 | |
| 370 | priv2 = spu->priv2; |
| 371 | |
| 372 | /* initialize all channel data to zero */ |
| 373 | for (i = 0; i < ARRAY_SIZE(zero_list); i++) { |
| 374 | int count; |
| 375 | |
| 376 | out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel); |
| 377 | for (count = 0; count < zero_list[i].count; count++) |
| 378 | out_be64(&priv2->spu_chnldata_RW, 0); |
| 379 | } |
| 380 | |
| 381 | /* initialize channel counts to meaningful values */ |
| 382 | for (i = 0; i < ARRAY_SIZE(count_list); i++) { |
| 383 | out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel); |
| 384 | out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count); |
| 385 | } |
| 386 | } |
| 387 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 388 | struct spu *spu_alloc(void) |
| 389 | { |
| 390 | struct spu *spu; |
| 391 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 392 | mutex_lock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 393 | if (!list_empty(&spu_list)) { |
| 394 | spu = list_entry(spu_list.next, struct spu, list); |
| 395 | list_del_init(&spu->list); |
| 396 | pr_debug("Got SPU %x %d\n", spu->isrc, spu->number); |
| 397 | } else { |
| 398 | pr_debug("No SPU left\n"); |
| 399 | spu = NULL; |
| 400 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 401 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 402 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 403 | if (spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 404 | spu_init_channels(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 405 | |
| 406 | return spu; |
| 407 | } |
Arnd Bergmann | 39c73c3 | 2005-12-05 22:52:21 -0500 | [diff] [blame] | 408 | EXPORT_SYMBOL_GPL(spu_alloc); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 409 | |
| 410 | void spu_free(struct spu *spu) |
| 411 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 412 | mutex_lock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 413 | list_add_tail(&spu->list, &spu_list); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 414 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 415 | } |
Arnd Bergmann | 39c73c3 | 2005-12-05 22:52:21 -0500 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(spu_free); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 417 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 418 | static int spu_handle_mm_fault(struct spu *spu) |
| 419 | { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 420 | struct mm_struct *mm = spu->mm; |
| 421 | struct vm_area_struct *vma; |
| 422 | u64 ea, dsisr, is_write; |
| 423 | int ret; |
| 424 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 425 | ea = spu->dar; |
| 426 | dsisr = spu->dsisr; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 427 | #if 0 |
| 428 | if (!IS_VALID_EA(ea)) { |
| 429 | return -EFAULT; |
| 430 | } |
| 431 | #endif /* XXX */ |
| 432 | if (mm == NULL) { |
| 433 | return -EFAULT; |
| 434 | } |
| 435 | if (mm->pgd == NULL) { |
| 436 | return -EFAULT; |
| 437 | } |
| 438 | |
| 439 | down_read(&mm->mmap_sem); |
| 440 | vma = find_vma(mm, ea); |
| 441 | if (!vma) |
| 442 | goto bad_area; |
| 443 | if (vma->vm_start <= ea) |
| 444 | goto good_area; |
| 445 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 446 | goto bad_area; |
| 447 | #if 0 |
| 448 | if (expand_stack(vma, ea)) |
| 449 | goto bad_area; |
| 450 | #endif /* XXX */ |
| 451 | good_area: |
| 452 | is_write = dsisr & MFC_DSISR_ACCESS_PUT; |
| 453 | if (is_write) { |
| 454 | if (!(vma->vm_flags & VM_WRITE)) |
| 455 | goto bad_area; |
| 456 | } else { |
| 457 | if (dsisr & MFC_DSISR_ACCESS_DENIED) |
| 458 | goto bad_area; |
| 459 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
| 460 | goto bad_area; |
| 461 | } |
| 462 | ret = 0; |
| 463 | switch (handle_mm_fault(mm, vma, ea, is_write)) { |
| 464 | case VM_FAULT_MINOR: |
| 465 | current->min_flt++; |
| 466 | break; |
| 467 | case VM_FAULT_MAJOR: |
| 468 | current->maj_flt++; |
| 469 | break; |
| 470 | case VM_FAULT_SIGBUS: |
| 471 | ret = -EFAULT; |
| 472 | goto bad_area; |
| 473 | case VM_FAULT_OOM: |
| 474 | ret = -ENOMEM; |
| 475 | goto bad_area; |
| 476 | default: |
| 477 | BUG(); |
| 478 | } |
| 479 | up_read(&mm->mmap_sem); |
| 480 | return ret; |
| 481 | |
| 482 | bad_area: |
| 483 | up_read(&mm->mmap_sem); |
| 484 | return -EFAULT; |
| 485 | } |
| 486 | |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 487 | int spu_irq_class_1_bottom(struct spu *spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 488 | { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 489 | u64 ea, dsisr, access, error = 0UL; |
| 490 | int ret = 0; |
| 491 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 492 | ea = spu->dar; |
| 493 | dsisr = spu->dsisr; |
Arnd Bergmann | 79c227a | 2006-03-24 19:49:27 +0100 | [diff] [blame] | 494 | if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) { |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 495 | u64 flags; |
| 496 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 497 | access = (_PAGE_PRESENT | _PAGE_USER); |
| 498 | access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL; |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 499 | local_irq_save(flags); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 500 | if (hash_page(ea, access, 0x300) != 0) |
| 501 | error |= CLASS1_ENABLE_STORAGE_FAULT_INTR; |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 502 | local_irq_restore(flags); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 503 | } |
Arnd Bergmann | 79c227a | 2006-03-24 19:49:27 +0100 | [diff] [blame] | 504 | if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 505 | if ((ret = spu_handle_mm_fault(spu)) != 0) |
| 506 | error |= CLASS1_ENABLE_STORAGE_FAULT_INTR; |
| 507 | else |
| 508 | error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR; |
| 509 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 510 | spu->dar = 0UL; |
| 511 | spu->dsisr = 0UL; |
| 512 | if (!error) { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 513 | spu_restart_dma(spu); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 514 | } else { |
| 515 | __spu_trap_invalid_dma(spu); |
| 516 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 517 | return ret; |
| 518 | } |
| 519 | |
Arnd Bergmann | 2fb9d20 | 2006-01-05 14:05:29 +0000 | [diff] [blame] | 520 | void spu_irq_setaffinity(struct spu *spu, int cpu) |
| 521 | { |
| 522 | u64 target = iic_get_target_id(cpu); |
| 523 | u64 route = target << 48 | target << 32 | target << 16; |
| 524 | spu_int_route_set(spu, route); |
| 525 | } |
| 526 | EXPORT_SYMBOL_GPL(spu_irq_setaffinity); |
| 527 | |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 528 | static int __init find_spu_node_id(struct device_node *spe) |
| 529 | { |
| 530 | unsigned int *id; |
| 531 | struct device_node *cpu; |
| 532 | cpu = spe->parent->parent; |
| 533 | id = (unsigned int *)get_property(cpu, "node-id", NULL); |
| 534 | return id ? *id : 0; |
| 535 | } |
| 536 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 537 | static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe, |
| 538 | const char *prop) |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 539 | { |
| 540 | static DEFINE_MUTEX(add_spumem_mutex); |
| 541 | |
| 542 | struct address_prop { |
| 543 | unsigned long address; |
| 544 | unsigned int len; |
| 545 | } __attribute__((packed)) *p; |
| 546 | int proplen; |
| 547 | |
| 548 | unsigned long start_pfn, nr_pages; |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 549 | struct pglist_data *pgdata; |
| 550 | struct zone *zone; |
| 551 | int ret; |
| 552 | |
| 553 | p = (void*)get_property(spe, prop, &proplen); |
| 554 | WARN_ON(proplen != sizeof (*p)); |
| 555 | |
| 556 | start_pfn = p->address >> PAGE_SHIFT; |
| 557 | nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 558 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 559 | pgdata = NODE_DATA(spu->nid); |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 560 | zone = pgdata->node_zones; |
| 561 | |
| 562 | /* XXX rethink locking here */ |
| 563 | mutex_lock(&add_spumem_mutex); |
| 564 | ret = __add_pages(zone, start_pfn, nr_pages); |
| 565 | mutex_unlock(&add_spumem_mutex); |
| 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 570 | static void __iomem * __init map_spe_prop(struct spu *spu, |
| 571 | struct device_node *n, const char *name) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 572 | { |
| 573 | struct address_prop { |
| 574 | unsigned long address; |
| 575 | unsigned int len; |
| 576 | } __attribute__((packed)) *prop; |
| 577 | |
| 578 | void *p; |
| 579 | int proplen; |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 580 | void* ret = NULL; |
| 581 | int err = 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 582 | |
| 583 | p = get_property(n, name, &proplen); |
| 584 | if (proplen != sizeof (struct address_prop)) |
| 585 | return NULL; |
| 586 | |
| 587 | prop = p; |
| 588 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 589 | err = cell_spuprop_present(spu, n, name); |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 590 | if (err && (err != -EEXIST)) |
| 591 | goto out; |
| 592 | |
| 593 | ret = ioremap(prop->address, prop->len); |
| 594 | |
| 595 | out: |
| 596 | return ret; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static void spu_unmap(struct spu *spu) |
| 600 | { |
| 601 | iounmap(spu->priv2); |
| 602 | iounmap(spu->priv1); |
| 603 | iounmap(spu->problem); |
| 604 | iounmap((u8 __iomem *)spu->local_store); |
| 605 | } |
| 606 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 607 | static int __init spu_map_device(struct spu *spu, struct device_node *node) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 608 | { |
| 609 | char *prop; |
| 610 | int ret; |
| 611 | |
| 612 | ret = -ENODEV; |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 613 | prop = get_property(node, "isrc", NULL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 614 | if (!prop) |
| 615 | goto out; |
| 616 | spu->isrc = *(unsigned int *)prop; |
| 617 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 618 | spu->name = get_property(node, "name", NULL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 619 | if (!spu->name) |
| 620 | goto out; |
| 621 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 622 | prop = get_property(node, "local-store", NULL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 623 | if (!prop) |
| 624 | goto out; |
| 625 | spu->local_store_phys = *(unsigned long *)prop; |
| 626 | |
| 627 | /* we use local store as ram, not io memory */ |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 628 | spu->local_store = (void __force *) |
| 629 | map_spe_prop(spu, node, "local-store"); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 630 | if (!spu->local_store) |
| 631 | goto out; |
| 632 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 633 | prop = get_property(node, "problem", NULL); |
Mark Nutter | 6df10a8 | 2006-03-23 00:00:12 +0100 | [diff] [blame] | 634 | if (!prop) |
| 635 | goto out_unmap; |
| 636 | spu->problem_phys = *(unsigned long *)prop; |
| 637 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 638 | spu->problem= map_spe_prop(spu, node, "problem"); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 639 | if (!spu->problem) |
| 640 | goto out_unmap; |
| 641 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 642 | spu->priv1= map_spe_prop(spu, node, "priv1"); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 643 | /* priv1 is not available on a hypervisor */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 644 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 645 | spu->priv2= map_spe_prop(spu, node, "priv2"); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 646 | if (!spu->priv2) |
| 647 | goto out_unmap; |
| 648 | ret = 0; |
| 649 | goto out; |
| 650 | |
| 651 | out_unmap: |
| 652 | spu_unmap(spu); |
| 653 | out: |
| 654 | return ret; |
| 655 | } |
| 656 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 657 | struct sysdev_class spu_sysdev_class = { |
| 658 | set_kset_name("spu") |
| 659 | }; |
| 660 | |
| 661 | static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf) |
| 662 | { |
| 663 | struct spu *spu = container_of(sysdev, struct spu, sysdev); |
| 664 | return sprintf(buf, "%d\n", spu->isrc); |
| 665 | |
| 666 | } |
| 667 | static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL); |
| 668 | |
| 669 | extern int attach_sysdev_to_node(struct sys_device *dev, int nid); |
| 670 | |
| 671 | static int spu_create_sysdev(struct spu *spu) |
| 672 | { |
| 673 | int ret; |
| 674 | |
| 675 | spu->sysdev.id = spu->number; |
| 676 | spu->sysdev.cls = &spu_sysdev_class; |
| 677 | ret = sysdev_register(&spu->sysdev); |
| 678 | if (ret) { |
| 679 | printk(KERN_ERR "Can't register SPU %d with sysfs\n", |
| 680 | spu->number); |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | sysdev_create_file(&spu->sysdev, &attr_isrc); |
| 685 | sysfs_add_device_to_node(&spu->sysdev, spu->nid); |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static void spu_destroy_sysdev(struct spu *spu) |
| 691 | { |
| 692 | sysdev_remove_file(&spu->sysdev, &attr_isrc); |
| 693 | sysfs_remove_device_from_node(&spu->sysdev, spu->nid); |
| 694 | sysdev_unregister(&spu->sysdev); |
| 695 | } |
| 696 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 697 | static int __init create_spu(struct device_node *spe) |
| 698 | { |
| 699 | struct spu *spu; |
| 700 | int ret; |
| 701 | static int number; |
| 702 | |
| 703 | ret = -ENOMEM; |
Jeremy Kerr | ecec217 | 2006-06-19 20:33:26 +0200 | [diff] [blame] | 704 | spu = kzalloc(sizeof (*spu), GFP_KERNEL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 705 | if (!spu) |
| 706 | goto out; |
| 707 | |
| 708 | ret = spu_map_device(spu, spe); |
| 709 | if (ret) |
| 710 | goto out_free; |
| 711 | |
| 712 | spu->node = find_spu_node_id(spe); |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 713 | spu->nid = of_node_to_nid(spe); |
| 714 | if (spu->nid == -1) |
| 715 | spu->nid = 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 716 | spin_lock_init(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 717 | spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1)); |
| 718 | spu_mfc_sr1_set(spu, 0x33); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 719 | mutex_lock(&spu_mutex); |
Jeremy Kerr | ecec217 | 2006-06-19 20:33:26 +0200 | [diff] [blame] | 720 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 721 | spu->number = number++; |
| 722 | ret = spu_request_irqs(spu); |
| 723 | if (ret) |
| 724 | goto out_unmap; |
| 725 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 726 | ret = spu_create_sysdev(spu); |
| 727 | if (ret) |
| 728 | goto out_free_irqs; |
| 729 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 730 | list_add(&spu->list, &spu_list); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 731 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 732 | |
| 733 | pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n", |
| 734 | spu->name, spu->isrc, spu->local_store, |
| 735 | spu->problem, spu->priv1, spu->priv2, spu->number); |
| 736 | goto out; |
| 737 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 738 | out_free_irqs: |
| 739 | spu_free_irqs(spu); |
| 740 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 741 | out_unmap: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 742 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 743 | spu_unmap(spu); |
| 744 | out_free: |
| 745 | kfree(spu); |
| 746 | out: |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | static void destroy_spu(struct spu *spu) |
| 751 | { |
| 752 | list_del_init(&spu->list); |
| 753 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 754 | spu_destroy_sysdev(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 755 | spu_free_irqs(spu); |
| 756 | spu_unmap(spu); |
| 757 | kfree(spu); |
| 758 | } |
| 759 | |
| 760 | static void cleanup_spu_base(void) |
| 761 | { |
| 762 | struct spu *spu, *tmp; |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 763 | mutex_lock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 764 | list_for_each_entry_safe(spu, tmp, &spu_list, list) |
| 765 | destroy_spu(spu); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 766 | mutex_unlock(&spu_mutex); |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 767 | sysdev_class_unregister(&spu_sysdev_class); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 768 | } |
| 769 | module_exit(cleanup_spu_base); |
| 770 | |
| 771 | static int __init init_spu_base(void) |
| 772 | { |
| 773 | struct device_node *node; |
| 774 | int ret; |
| 775 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 776 | /* create sysdev class for spus */ |
| 777 | ret = sysdev_class_register(&spu_sysdev_class); |
| 778 | if (ret) |
| 779 | return ret; |
| 780 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 781 | ret = -ENODEV; |
| 782 | for (node = of_find_node_by_type(NULL, "spe"); |
| 783 | node; node = of_find_node_by_type(node, "spe")) { |
| 784 | ret = create_spu(node); |
| 785 | if (ret) { |
| 786 | printk(KERN_WARNING "%s: Error initializing %s\n", |
| 787 | __FUNCTION__, node->name); |
| 788 | cleanup_spu_base(); |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | /* in some old firmware versions, the spe is called 'spc', so we |
| 793 | look for that as well */ |
| 794 | for (node = of_find_node_by_type(NULL, "spc"); |
| 795 | node; node = of_find_node_by_type(node, "spc")) { |
| 796 | ret = create_spu(node); |
| 797 | if (ret) { |
| 798 | printk(KERN_WARNING "%s: Error initializing %s\n", |
| 799 | __FUNCTION__, node->name); |
| 800 | cleanup_spu_base(); |
| 801 | break; |
| 802 | } |
| 803 | } |
| 804 | return ret; |
| 805 | } |
| 806 | module_init(init_spu_base); |
| 807 | |
| 808 | MODULE_LICENSE("GPL"); |
| 809 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); |