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> |
| 35 | #include <asm/semaphore.h> |
| 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; |
| 74 | u64 esid, vsid; |
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; |
| 94 | vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) | SLB_VSID_USER; |
| 95 | if (in_hugepage_area(mm->context, ea)) |
| 96 | vsid |= SLB_VSID_L; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 97 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 98 | out_be64(&priv2->slb_index_W, spu->slb_replace); |
| 99 | out_be64(&priv2->slb_vsid_RW, vsid); |
| 100 | out_be64(&priv2->slb_esid_RW, esid); |
| 101 | |
| 102 | spu->slb_replace++; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 103 | if (spu->slb_replace >= 8) |
| 104 | spu->slb_replace = 0; |
| 105 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 106 | spu_restart_dma(spu); |
| 107 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 111 | 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] | 112 | 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] | 113 | { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 114 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 115 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 116 | /* Handle kernel space hash faults immediately. |
| 117 | User hash faults need to be deferred to process context. */ |
| 118 | if ((dsisr & MFC_DSISR_PTE_NOT_FOUND) |
| 119 | && REGION_ID(ea) != USER_REGION_ID |
| 120 | && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) { |
| 121 | spu_restart_dma(spu); |
| 122 | return 0; |
| 123 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 124 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 125 | if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) { |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 126 | printk("%s: invalid access during switch!\n", __func__); |
| 127 | return 1; |
| 128 | } |
| 129 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 130 | spu->dar = ea; |
| 131 | spu->dsisr = dsisr; |
| 132 | mb(); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 133 | if (spu->stop_callback) |
| 134 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int __spu_trap_mailbox(struct spu *spu) |
| 139 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 140 | if (spu->ibox_callback) |
| 141 | spu->ibox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 142 | |
| 143 | /* atomically disable SPU mailbox interrupts */ |
| 144 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 145 | spu_int_mask_and(spu, 2, ~0x1); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 146 | spin_unlock(&spu->register_lock); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int __spu_trap_stop(struct spu *spu) |
| 151 | { |
| 152 | pr_debug("%s\n", __FUNCTION__); |
| 153 | spu->stop_code = in_be32(&spu->problem->spu_status_R); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 154 | if (spu->stop_callback) |
| 155 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int __spu_trap_halt(struct spu *spu) |
| 160 | { |
| 161 | pr_debug("%s\n", __FUNCTION__); |
| 162 | spu->stop_code = in_be32(&spu->problem->spu_status_R); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 163 | if (spu->stop_callback) |
| 164 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int __spu_trap_tag_group(struct spu *spu) |
| 169 | { |
| 170 | pr_debug("%s\n", __FUNCTION__); |
| 171 | /* wake_up(&spu->dma_wq); */ |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int __spu_trap_spubox(struct spu *spu) |
| 176 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 177 | if (spu->wbox_callback) |
| 178 | spu->wbox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 179 | |
| 180 | /* atomically disable SPU mailbox interrupts */ |
| 181 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 182 | spu_int_mask_and(spu, 2, ~0x10); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 183 | spin_unlock(&spu->register_lock); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static irqreturn_t |
| 188 | spu_irq_class_0(int irq, void *data, struct pt_regs *regs) |
| 189 | { |
| 190 | struct spu *spu; |
| 191 | |
| 192 | spu = data; |
| 193 | spu->class_0_pending = 1; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 194 | if (spu->stop_callback) |
| 195 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 196 | |
| 197 | return IRQ_HANDLED; |
| 198 | } |
| 199 | |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 200 | int |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 201 | spu_irq_class_0_bottom(struct spu *spu) |
| 202 | { |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 203 | unsigned long stat, mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 204 | |
| 205 | spu->class_0_pending = 0; |
| 206 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 207 | mask = spu_int_mask_get(spu, 0); |
| 208 | stat = spu_int_stat_get(spu, 0); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 209 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 210 | stat &= mask; |
| 211 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 212 | if (stat & 1) /* invalid MFC DMA */ |
| 213 | __spu_trap_invalid_dma(spu); |
| 214 | |
| 215 | if (stat & 2) /* invalid DMA alignment */ |
| 216 | __spu_trap_dma_align(spu); |
| 217 | |
| 218 | if (stat & 4) /* error on SPU */ |
| 219 | __spu_trap_error(spu); |
| 220 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 221 | spu_int_stat_clear(spu, 0, stat); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 222 | |
| 223 | return (stat & 0x7) ? -EIO : 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 224 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 225 | EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 226 | |
| 227 | static irqreturn_t |
| 228 | spu_irq_class_1(int irq, void *data, struct pt_regs *regs) |
| 229 | { |
| 230 | struct spu *spu; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 231 | unsigned long stat, mask, dar, dsisr; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 232 | |
| 233 | spu = data; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 234 | |
| 235 | /* atomically read & clear class1 status. */ |
| 236 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 237 | mask = spu_int_mask_get(spu, 1); |
| 238 | stat = spu_int_stat_get(spu, 1) & mask; |
| 239 | dar = spu_mfc_dar_get(spu); |
| 240 | dsisr = spu_mfc_dsisr_get(spu); |
Arnd Bergmann | 3830734 | 2005-12-09 19:04:18 +0100 | [diff] [blame] | 241 | if (stat & 2) /* mapping fault */ |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 242 | spu_mfc_dsisr_set(spu, 0ul); |
| 243 | spu_int_stat_clear(spu, 1, stat); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 244 | spin_unlock(&spu->register_lock); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 245 | |
| 246 | if (stat & 1) /* segment fault */ |
| 247 | __spu_trap_data_seg(spu, dar); |
| 248 | |
| 249 | if (stat & 2) { /* mapping fault */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 250 | __spu_trap_data_map(spu, dar, dsisr); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (stat & 4) /* ls compare & suspend on get */ |
| 254 | ; |
| 255 | |
| 256 | if (stat & 8) /* ls compare & suspend on put */ |
| 257 | ; |
| 258 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 259 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 260 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 261 | EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 262 | |
| 263 | static irqreturn_t |
| 264 | spu_irq_class_2(int irq, void *data, struct pt_regs *regs) |
| 265 | { |
| 266 | struct spu *spu; |
| 267 | unsigned long stat; |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 268 | unsigned long mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 269 | |
| 270 | spu = data; |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 271 | stat = spu_int_stat_get(spu, 2); |
| 272 | mask = spu_int_mask_get(spu, 2); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 273 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 274 | pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 275 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 276 | stat &= mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 277 | |
| 278 | if (stat & 1) /* PPC core mailbox */ |
| 279 | __spu_trap_mailbox(spu); |
| 280 | |
| 281 | if (stat & 2) /* SPU stop-and-signal */ |
| 282 | __spu_trap_stop(spu); |
| 283 | |
| 284 | if (stat & 4) /* SPU halted */ |
| 285 | __spu_trap_halt(spu); |
| 286 | |
| 287 | if (stat & 8) /* DMA tag group complete */ |
| 288 | __spu_trap_tag_group(spu); |
| 289 | |
| 290 | if (stat & 0x10) /* SPU mailbox threshold */ |
| 291 | __spu_trap_spubox(spu); |
| 292 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 293 | spu_int_stat_clear(spu, 2, stat); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 294 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 295 | } |
| 296 | |
| 297 | static int |
| 298 | spu_request_irqs(struct spu *spu) |
| 299 | { |
| 300 | int ret; |
| 301 | int irq_base; |
| 302 | |
| 303 | irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET; |
| 304 | |
| 305 | snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number); |
| 306 | ret = request_irq(irq_base + spu->isrc, |
| 307 | spu_irq_class_0, 0, spu->irq_c0, spu); |
| 308 | if (ret) |
| 309 | goto out; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 310 | |
| 311 | snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number); |
| 312 | ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, |
| 313 | spu_irq_class_1, 0, spu->irq_c1, spu); |
| 314 | if (ret) |
| 315 | goto out1; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 316 | |
| 317 | snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number); |
| 318 | ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, |
| 319 | spu_irq_class_2, 0, spu->irq_c2, spu); |
| 320 | if (ret) |
| 321 | goto out2; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 322 | goto out; |
| 323 | |
| 324 | out2: |
| 325 | free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu); |
| 326 | out1: |
| 327 | free_irq(irq_base + spu->isrc, spu); |
| 328 | out: |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | static void |
| 333 | spu_free_irqs(struct spu *spu) |
| 334 | { |
| 335 | int irq_base; |
| 336 | |
| 337 | irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET; |
| 338 | |
| 339 | free_irq(irq_base + spu->isrc, spu); |
| 340 | free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu); |
| 341 | free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu); |
| 342 | } |
| 343 | |
| 344 | static LIST_HEAD(spu_list); |
| 345 | static DECLARE_MUTEX(spu_mutex); |
| 346 | |
| 347 | static void spu_init_channels(struct spu *spu) |
| 348 | { |
| 349 | static const struct { |
| 350 | unsigned channel; |
| 351 | unsigned count; |
| 352 | } zero_list[] = { |
| 353 | { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, }, |
| 354 | { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, }, |
| 355 | }, count_list[] = { |
| 356 | { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, }, |
| 357 | { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, }, |
| 358 | { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, }, |
| 359 | }; |
Arnd Bergmann | 6ff730c | 2006-01-04 20:31:31 +0100 | [diff] [blame^] | 360 | struct spu_priv2 __iomem *priv2; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 361 | int i; |
| 362 | |
| 363 | priv2 = spu->priv2; |
| 364 | |
| 365 | /* initialize all channel data to zero */ |
| 366 | for (i = 0; i < ARRAY_SIZE(zero_list); i++) { |
| 367 | int count; |
| 368 | |
| 369 | out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel); |
| 370 | for (count = 0; count < zero_list[i].count; count++) |
| 371 | out_be64(&priv2->spu_chnldata_RW, 0); |
| 372 | } |
| 373 | |
| 374 | /* initialize channel counts to meaningful values */ |
| 375 | for (i = 0; i < ARRAY_SIZE(count_list); i++) { |
| 376 | out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel); |
| 377 | out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count); |
| 378 | } |
| 379 | } |
| 380 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 381 | struct spu *spu_alloc(void) |
| 382 | { |
| 383 | struct spu *spu; |
| 384 | |
| 385 | down(&spu_mutex); |
| 386 | if (!list_empty(&spu_list)) { |
| 387 | spu = list_entry(spu_list.next, struct spu, list); |
| 388 | list_del_init(&spu->list); |
| 389 | pr_debug("Got SPU %x %d\n", spu->isrc, spu->number); |
| 390 | } else { |
| 391 | pr_debug("No SPU left\n"); |
| 392 | spu = NULL; |
| 393 | } |
| 394 | up(&spu_mutex); |
| 395 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 396 | if (spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 397 | spu_init_channels(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 398 | |
| 399 | return spu; |
| 400 | } |
Arnd Bergmann | 39c73c3 | 2005-12-05 22:52:21 -0500 | [diff] [blame] | 401 | EXPORT_SYMBOL_GPL(spu_alloc); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 402 | |
| 403 | void spu_free(struct spu *spu) |
| 404 | { |
| 405 | down(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 406 | list_add_tail(&spu->list, &spu_list); |
| 407 | up(&spu_mutex); |
| 408 | } |
Arnd Bergmann | 39c73c3 | 2005-12-05 22:52:21 -0500 | [diff] [blame] | 409 | EXPORT_SYMBOL_GPL(spu_free); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 410 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 411 | static int spu_handle_mm_fault(struct spu *spu) |
| 412 | { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 413 | struct mm_struct *mm = spu->mm; |
| 414 | struct vm_area_struct *vma; |
| 415 | u64 ea, dsisr, is_write; |
| 416 | int ret; |
| 417 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 418 | ea = spu->dar; |
| 419 | dsisr = spu->dsisr; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 420 | #if 0 |
| 421 | if (!IS_VALID_EA(ea)) { |
| 422 | return -EFAULT; |
| 423 | } |
| 424 | #endif /* XXX */ |
| 425 | if (mm == NULL) { |
| 426 | return -EFAULT; |
| 427 | } |
| 428 | if (mm->pgd == NULL) { |
| 429 | return -EFAULT; |
| 430 | } |
| 431 | |
| 432 | down_read(&mm->mmap_sem); |
| 433 | vma = find_vma(mm, ea); |
| 434 | if (!vma) |
| 435 | goto bad_area; |
| 436 | if (vma->vm_start <= ea) |
| 437 | goto good_area; |
| 438 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 439 | goto bad_area; |
| 440 | #if 0 |
| 441 | if (expand_stack(vma, ea)) |
| 442 | goto bad_area; |
| 443 | #endif /* XXX */ |
| 444 | good_area: |
| 445 | is_write = dsisr & MFC_DSISR_ACCESS_PUT; |
| 446 | if (is_write) { |
| 447 | if (!(vma->vm_flags & VM_WRITE)) |
| 448 | goto bad_area; |
| 449 | } else { |
| 450 | if (dsisr & MFC_DSISR_ACCESS_DENIED) |
| 451 | goto bad_area; |
| 452 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
| 453 | goto bad_area; |
| 454 | } |
| 455 | ret = 0; |
| 456 | switch (handle_mm_fault(mm, vma, ea, is_write)) { |
| 457 | case VM_FAULT_MINOR: |
| 458 | current->min_flt++; |
| 459 | break; |
| 460 | case VM_FAULT_MAJOR: |
| 461 | current->maj_flt++; |
| 462 | break; |
| 463 | case VM_FAULT_SIGBUS: |
| 464 | ret = -EFAULT; |
| 465 | goto bad_area; |
| 466 | case VM_FAULT_OOM: |
| 467 | ret = -ENOMEM; |
| 468 | goto bad_area; |
| 469 | default: |
| 470 | BUG(); |
| 471 | } |
| 472 | up_read(&mm->mmap_sem); |
| 473 | return ret; |
| 474 | |
| 475 | bad_area: |
| 476 | up_read(&mm->mmap_sem); |
| 477 | return -EFAULT; |
| 478 | } |
| 479 | |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 480 | int spu_irq_class_1_bottom(struct spu *spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 481 | { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 482 | u64 ea, dsisr, access, error = 0UL; |
| 483 | int ret = 0; |
| 484 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 485 | ea = spu->dar; |
| 486 | dsisr = spu->dsisr; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 487 | if (dsisr & MFC_DSISR_PTE_NOT_FOUND) { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 488 | access = (_PAGE_PRESENT | _PAGE_USER); |
| 489 | access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 490 | if (hash_page(ea, access, 0x300) != 0) |
| 491 | error |= CLASS1_ENABLE_STORAGE_FAULT_INTR; |
| 492 | } |
| 493 | if ((error & CLASS1_ENABLE_STORAGE_FAULT_INTR) || |
| 494 | (dsisr & MFC_DSISR_ACCESS_DENIED)) { |
| 495 | if ((ret = spu_handle_mm_fault(spu)) != 0) |
| 496 | error |= CLASS1_ENABLE_STORAGE_FAULT_INTR; |
| 497 | else |
| 498 | error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR; |
| 499 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 500 | spu->dar = 0UL; |
| 501 | spu->dsisr = 0UL; |
| 502 | if (!error) { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 503 | spu_restart_dma(spu); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 504 | } else { |
| 505 | __spu_trap_invalid_dma(spu); |
| 506 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 507 | return ret; |
| 508 | } |
| 509 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 510 | static void __iomem * __init map_spe_prop(struct device_node *n, |
| 511 | const char *name) |
| 512 | { |
| 513 | struct address_prop { |
| 514 | unsigned long address; |
| 515 | unsigned int len; |
| 516 | } __attribute__((packed)) *prop; |
| 517 | |
| 518 | void *p; |
| 519 | int proplen; |
| 520 | |
| 521 | p = get_property(n, name, &proplen); |
| 522 | if (proplen != sizeof (struct address_prop)) |
| 523 | return NULL; |
| 524 | |
| 525 | prop = p; |
| 526 | |
| 527 | return ioremap(prop->address, prop->len); |
| 528 | } |
| 529 | |
| 530 | static void spu_unmap(struct spu *spu) |
| 531 | { |
| 532 | iounmap(spu->priv2); |
| 533 | iounmap(spu->priv1); |
| 534 | iounmap(spu->problem); |
| 535 | iounmap((u8 __iomem *)spu->local_store); |
| 536 | } |
| 537 | |
| 538 | static int __init spu_map_device(struct spu *spu, struct device_node *spe) |
| 539 | { |
| 540 | char *prop; |
| 541 | int ret; |
| 542 | |
| 543 | ret = -ENODEV; |
| 544 | prop = get_property(spe, "isrc", NULL); |
| 545 | if (!prop) |
| 546 | goto out; |
| 547 | spu->isrc = *(unsigned int *)prop; |
| 548 | |
| 549 | spu->name = get_property(spe, "name", NULL); |
| 550 | if (!spu->name) |
| 551 | goto out; |
| 552 | |
| 553 | prop = get_property(spe, "local-store", NULL); |
| 554 | if (!prop) |
| 555 | goto out; |
| 556 | spu->local_store_phys = *(unsigned long *)prop; |
| 557 | |
| 558 | /* we use local store as ram, not io memory */ |
| 559 | spu->local_store = (void __force *)map_spe_prop(spe, "local-store"); |
| 560 | if (!spu->local_store) |
| 561 | goto out; |
| 562 | |
| 563 | spu->problem= map_spe_prop(spe, "problem"); |
| 564 | if (!spu->problem) |
| 565 | goto out_unmap; |
| 566 | |
| 567 | spu->priv1= map_spe_prop(spe, "priv1"); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 568 | /* priv1 is not available on a hypervisor */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 569 | |
| 570 | spu->priv2= map_spe_prop(spe, "priv2"); |
| 571 | if (!spu->priv2) |
| 572 | goto out_unmap; |
| 573 | ret = 0; |
| 574 | goto out; |
| 575 | |
| 576 | out_unmap: |
| 577 | spu_unmap(spu); |
| 578 | out: |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | static int __init find_spu_node_id(struct device_node *spe) |
| 583 | { |
| 584 | unsigned int *id; |
| 585 | struct device_node *cpu; |
| 586 | |
| 587 | cpu = spe->parent->parent; |
| 588 | id = (unsigned int *)get_property(cpu, "node-id", NULL); |
| 589 | |
| 590 | return id ? *id : 0; |
| 591 | } |
| 592 | |
| 593 | static int __init create_spu(struct device_node *spe) |
| 594 | { |
| 595 | struct spu *spu; |
| 596 | int ret; |
| 597 | static int number; |
| 598 | |
| 599 | ret = -ENOMEM; |
| 600 | spu = kmalloc(sizeof (*spu), GFP_KERNEL); |
| 601 | if (!spu) |
| 602 | goto out; |
| 603 | |
| 604 | ret = spu_map_device(spu, spe); |
| 605 | if (ret) |
| 606 | goto out_free; |
| 607 | |
| 608 | spu->node = find_spu_node_id(spe); |
| 609 | spu->stop_code = 0; |
| 610 | spu->slb_replace = 0; |
| 611 | spu->mm = NULL; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 612 | spu->ctx = NULL; |
| 613 | spu->rq = NULL; |
| 614 | spu->pid = 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 615 | spu->class_0_pending = 0; |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 616 | spu->flags = 0UL; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 617 | spu->dar = 0UL; |
| 618 | spu->dsisr = 0UL; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 619 | spin_lock_init(&spu->register_lock); |
| 620 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 621 | spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1)); |
| 622 | spu_mfc_sr1_set(spu, 0x33); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 623 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 624 | spu->ibox_callback = NULL; |
| 625 | spu->wbox_callback = NULL; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 626 | spu->stop_callback = NULL; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 627 | |
| 628 | down(&spu_mutex); |
| 629 | spu->number = number++; |
| 630 | ret = spu_request_irqs(spu); |
| 631 | if (ret) |
| 632 | goto out_unmap; |
| 633 | |
| 634 | list_add(&spu->list, &spu_list); |
| 635 | up(&spu_mutex); |
| 636 | |
| 637 | pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n", |
| 638 | spu->name, spu->isrc, spu->local_store, |
| 639 | spu->problem, spu->priv1, spu->priv2, spu->number); |
| 640 | goto out; |
| 641 | |
| 642 | out_unmap: |
| 643 | up(&spu_mutex); |
| 644 | spu_unmap(spu); |
| 645 | out_free: |
| 646 | kfree(spu); |
| 647 | out: |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | static void destroy_spu(struct spu *spu) |
| 652 | { |
| 653 | list_del_init(&spu->list); |
| 654 | |
| 655 | spu_free_irqs(spu); |
| 656 | spu_unmap(spu); |
| 657 | kfree(spu); |
| 658 | } |
| 659 | |
| 660 | static void cleanup_spu_base(void) |
| 661 | { |
| 662 | struct spu *spu, *tmp; |
| 663 | down(&spu_mutex); |
| 664 | list_for_each_entry_safe(spu, tmp, &spu_list, list) |
| 665 | destroy_spu(spu); |
| 666 | up(&spu_mutex); |
| 667 | } |
| 668 | module_exit(cleanup_spu_base); |
| 669 | |
| 670 | static int __init init_spu_base(void) |
| 671 | { |
| 672 | struct device_node *node; |
| 673 | int ret; |
| 674 | |
| 675 | ret = -ENODEV; |
| 676 | for (node = of_find_node_by_type(NULL, "spe"); |
| 677 | node; node = of_find_node_by_type(node, "spe")) { |
| 678 | ret = create_spu(node); |
| 679 | if (ret) { |
| 680 | printk(KERN_WARNING "%s: Error initializing %s\n", |
| 681 | __FUNCTION__, node->name); |
| 682 | cleanup_spu_base(); |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | /* in some old firmware versions, the spe is called 'spc', so we |
| 687 | look for that as well */ |
| 688 | for (node = of_find_node_by_type(NULL, "spc"); |
| 689 | node; node = of_find_node_by_type(node, "spc")) { |
| 690 | ret = create_spu(node); |
| 691 | if (ret) { |
| 692 | printk(KERN_WARNING "%s: Error initializing %s\n", |
| 693 | __FUNCTION__, node->name); |
| 694 | cleanup_spu_base(); |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | return ret; |
| 699 | } |
| 700 | module_init(init_spu_base); |
| 701 | |
| 702 | MODULE_LICENSE("GPL"); |
| 703 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); |