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 | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 28 | #include <linux/pci.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 29 | #include <linux/poll.h> |
| 30 | #include <linux/ptrace.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/wait.h> |
| 33 | |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 34 | #include <asm/firmware.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 35 | #include <asm/io.h> |
| 36 | #include <asm/prom.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 38 | #include <asm/spu.h> |
Geoff Levand | 540270d | 2006-06-19 20:33:29 +0200 | [diff] [blame] | 39 | #include <asm/spu_priv1.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 40 | #include <asm/mmu_context.h> |
| 41 | |
| 42 | #include "interrupt.h" |
| 43 | |
Geoff Levand | 540270d | 2006-06-19 20:33:29 +0200 | [diff] [blame] | 44 | const struct spu_priv1_ops *spu_priv1_ops; |
| 45 | |
| 46 | EXPORT_SYMBOL_GPL(spu_priv1_ops); |
| 47 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 48 | static int __spu_trap_invalid_dma(struct spu *spu) |
| 49 | { |
| 50 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 51 | spu->dma_callback(spu, SPE_EVENT_INVALID_DMA); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int __spu_trap_dma_align(struct spu *spu) |
| 56 | { |
| 57 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 58 | spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int __spu_trap_error(struct spu *spu) |
| 63 | { |
| 64 | pr_debug("%s\n", __FUNCTION__); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 65 | spu->dma_callback(spu, SPE_EVENT_SPE_ERROR); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static void spu_restart_dma(struct spu *spu) |
| 70 | { |
| 71 | struct spu_priv2 __iomem *priv2 = spu->priv2; |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 72 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 73 | if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags)) |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 74 | out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int __spu_trap_data_seg(struct spu *spu, unsigned long ea) |
| 78 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 79 | struct spu_priv2 __iomem *priv2 = spu->priv2; |
| 80 | struct mm_struct *mm = spu->mm; |
arnd@arndb.de | 724bd80 | 2006-06-19 20:33:23 +0200 | [diff] [blame] | 81 | u64 esid, vsid, llp; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 82 | |
| 83 | pr_debug("%s\n", __FUNCTION__); |
| 84 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 85 | if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 86 | /* SLBs are pre-loaded for context switch, so |
| 87 | * we should never get here! |
| 88 | */ |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 89 | printk("%s: invalid access during switch!\n", __func__); |
| 90 | return 1; |
| 91 | } |
arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 92 | esid = (ea & ESID_MASK) | SLB_ESID_V; |
| 93 | |
| 94 | switch(REGION_ID(ea)) { |
| 95 | case USER_REGION_ID: |
| 96 | #ifdef CONFIG_HUGETLB_PAGE |
| 97 | if (in_hugepage_area(mm->context, ea)) |
| 98 | llp = mmu_psize_defs[mmu_huge_psize].sllp; |
| 99 | else |
| 100 | #endif |
| 101 | llp = mmu_psize_defs[mmu_virtual_psize].sllp; |
| 102 | vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) | |
| 103 | SLB_VSID_USER | llp; |
| 104 | break; |
| 105 | case VMALLOC_REGION_ID: |
| 106 | llp = mmu_psize_defs[mmu_virtual_psize].sllp; |
| 107 | vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | |
| 108 | SLB_VSID_KERNEL | llp; |
| 109 | break; |
| 110 | case KERNEL_REGION_ID: |
| 111 | llp = mmu_psize_defs[mmu_linear_psize].sllp; |
| 112 | vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | |
| 113 | SLB_VSID_KERNEL | llp; |
| 114 | break; |
| 115 | default: |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 116 | /* Future: support kernel segments so that drivers |
| 117 | * can use SPUs. |
| 118 | */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 119 | pr_debug("invalid region access at %016lx\n", ea); |
| 120 | return 1; |
| 121 | } |
| 122 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 123 | out_be64(&priv2->slb_index_W, spu->slb_replace); |
| 124 | out_be64(&priv2->slb_vsid_RW, vsid); |
| 125 | out_be64(&priv2->slb_esid_RW, esid); |
| 126 | |
| 127 | spu->slb_replace++; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 128 | if (spu->slb_replace >= 8) |
| 129 | spu->slb_replace = 0; |
| 130 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 131 | spu_restart_dma(spu); |
| 132 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 136 | 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] | 137 | 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] | 138 | { |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 139 | pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 140 | |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 141 | /* Handle kernel space hash faults immediately. |
| 142 | User hash faults need to be deferred to process context. */ |
| 143 | if ((dsisr & MFC_DSISR_PTE_NOT_FOUND) |
| 144 | && REGION_ID(ea) != USER_REGION_ID |
| 145 | && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) { |
| 146 | spu_restart_dma(spu); |
| 147 | return 0; |
| 148 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 149 | |
Arnd Bergmann | 8837d92 | 2006-01-04 20:31:28 +0100 | [diff] [blame] | 150 | if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) { |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 151 | printk("%s: invalid access during switch!\n", __func__); |
| 152 | return 1; |
| 153 | } |
| 154 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 155 | spu->dar = ea; |
| 156 | spu->dsisr = dsisr; |
| 157 | mb(); |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 158 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static irqreturn_t |
Olaf Hering | f5a9245 | 2006-10-06 22:52:16 +0200 | [diff] [blame] | 163 | spu_irq_class_0(int irq, void *data) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 164 | { |
| 165 | struct spu *spu; |
| 166 | |
| 167 | spu = data; |
| 168 | spu->class_0_pending = 1; |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 169 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 170 | |
| 171 | return IRQ_HANDLED; |
| 172 | } |
| 173 | |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 174 | int |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 175 | spu_irq_class_0_bottom(struct spu *spu) |
| 176 | { |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 177 | unsigned long stat, mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 178 | |
| 179 | spu->class_0_pending = 0; |
| 180 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 181 | mask = spu_int_mask_get(spu, 0); |
| 182 | stat = spu_int_stat_get(spu, 0); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 183 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 184 | stat &= mask; |
| 185 | |
Arnd Bergmann | 2cd90bc | 2006-06-23 20:57:50 +0200 | [diff] [blame] | 186 | if (stat & 1) /* invalid DMA alignment */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 187 | __spu_trap_dma_align(spu); |
| 188 | |
Arnd Bergmann | 2cd90bc | 2006-06-23 20:57:50 +0200 | [diff] [blame] | 189 | if (stat & 2) /* invalid MFC DMA */ |
| 190 | __spu_trap_invalid_dma(spu); |
| 191 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 192 | if (stat & 4) /* error on SPU */ |
| 193 | __spu_trap_error(spu); |
| 194 | |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 195 | spu_int_stat_clear(spu, 0, stat); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 196 | |
| 197 | return (stat & 0x7) ? -EIO : 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 198 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 199 | EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 200 | |
| 201 | static irqreturn_t |
Olaf Hering | f5a9245 | 2006-10-06 22:52:16 +0200 | [diff] [blame] | 202 | spu_irq_class_1(int irq, void *data) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 203 | { |
| 204 | struct spu *spu; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 205 | unsigned long stat, mask, dar, dsisr; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 206 | |
| 207 | spu = data; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 208 | |
| 209 | /* atomically read & clear class1 status. */ |
| 210 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 211 | mask = spu_int_mask_get(spu, 1); |
| 212 | stat = spu_int_stat_get(spu, 1) & mask; |
| 213 | dar = spu_mfc_dar_get(spu); |
| 214 | dsisr = spu_mfc_dsisr_get(spu); |
Arnd Bergmann | 3830734 | 2005-12-09 19:04:18 +0100 | [diff] [blame] | 215 | if (stat & 2) /* mapping fault */ |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 216 | spu_mfc_dsisr_set(spu, 0ul); |
| 217 | spu_int_stat_clear(spu, 1, stat); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 218 | spin_unlock(&spu->register_lock); |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 219 | pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat, |
| 220 | dar, dsisr); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 221 | |
| 222 | if (stat & 1) /* segment fault */ |
| 223 | __spu_trap_data_seg(spu, dar); |
| 224 | |
| 225 | if (stat & 2) { /* mapping fault */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 226 | __spu_trap_data_map(spu, dar, dsisr); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if (stat & 4) /* ls compare & suspend on get */ |
| 230 | ; |
| 231 | |
| 232 | if (stat & 8) /* ls compare & suspend on put */ |
| 233 | ; |
| 234 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 235 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 236 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 238 | |
| 239 | static irqreturn_t |
Olaf Hering | f5a9245 | 2006-10-06 22:52:16 +0200 | [diff] [blame] | 240 | spu_irq_class_2(int irq, void *data) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 241 | { |
| 242 | struct spu *spu; |
| 243 | unsigned long stat; |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 244 | unsigned long mask; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 245 | |
| 246 | spu = data; |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 247 | spin_lock(&spu->register_lock); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 248 | stat = spu_int_stat_get(spu, 2); |
| 249 | mask = spu_int_mask_get(spu, 2); |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 250 | /* ignore interrupts we're not waiting for */ |
| 251 | stat &= mask; |
| 252 | /* |
| 253 | * mailbox interrupts (0x1 and 0x10) are level triggered. |
| 254 | * mask them now before acknowledging. |
| 255 | */ |
| 256 | if (stat & 0x11) |
| 257 | spu_int_mask_and(spu, 2, ~(stat & 0x11)); |
| 258 | /* acknowledge all interrupts before the callbacks */ |
| 259 | spu_int_stat_clear(spu, 2, stat); |
| 260 | spin_unlock(&spu->register_lock); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 261 | |
Arnd Bergmann | 3a843d7 | 2005-12-05 22:52:27 -0500 | [diff] [blame] | 262 | pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 263 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 264 | if (stat & 1) /* PPC core mailbox */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 265 | spu->ibox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 266 | |
| 267 | if (stat & 2) /* SPU stop-and-signal */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 268 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 269 | |
| 270 | if (stat & 4) /* SPU halted */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 271 | spu->stop_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 272 | |
| 273 | if (stat & 8) /* DMA tag group complete */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 274 | spu->mfc_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 275 | |
| 276 | if (stat & 0x10) /* SPU mailbox threshold */ |
Masato Noguchi | ba723fe2 | 2006-06-19 20:33:33 +0200 | [diff] [blame] | 277 | spu->wbox_callback(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 278 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 279 | return stat ? IRQ_HANDLED : IRQ_NONE; |
| 280 | } |
| 281 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 282 | static int spu_request_irqs(struct spu *spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 283 | { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 284 | int ret = 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 285 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 286 | if (spu->irqs[0] != NO_IRQ) { |
| 287 | snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", |
| 288 | spu->number); |
| 289 | ret = request_irq(spu->irqs[0], spu_irq_class_0, |
| 290 | IRQF_DISABLED, |
| 291 | spu->irq_c0, spu); |
| 292 | if (ret) |
| 293 | goto bail0; |
| 294 | } |
| 295 | if (spu->irqs[1] != NO_IRQ) { |
| 296 | snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", |
| 297 | spu->number); |
| 298 | ret = request_irq(spu->irqs[1], spu_irq_class_1, |
| 299 | IRQF_DISABLED, |
| 300 | spu->irq_c1, spu); |
| 301 | if (ret) |
| 302 | goto bail1; |
| 303 | } |
| 304 | if (spu->irqs[2] != NO_IRQ) { |
| 305 | snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", |
| 306 | spu->number); |
| 307 | ret = request_irq(spu->irqs[2], spu_irq_class_2, |
| 308 | IRQF_DISABLED, |
| 309 | spu->irq_c2, spu); |
| 310 | if (ret) |
| 311 | goto bail2; |
| 312 | } |
| 313 | return 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 314 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 315 | bail2: |
| 316 | if (spu->irqs[1] != NO_IRQ) |
| 317 | free_irq(spu->irqs[1], spu); |
| 318 | bail1: |
| 319 | if (spu->irqs[0] != NO_IRQ) |
| 320 | free_irq(spu->irqs[0], spu); |
| 321 | bail0: |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 322 | return ret; |
| 323 | } |
| 324 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 325 | static void spu_free_irqs(struct spu *spu) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 326 | { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 327 | if (spu->irqs[0] != NO_IRQ) |
| 328 | free_irq(spu->irqs[0], spu); |
| 329 | if (spu->irqs[1] != NO_IRQ) |
| 330 | free_irq(spu->irqs[1], spu); |
| 331 | if (spu->irqs[2] != NO_IRQ) |
| 332 | free_irq(spu->irqs[2], spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 335 | static struct list_head spu_list[MAX_NUMNODES]; |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 336 | static DEFINE_MUTEX(spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 337 | |
| 338 | static void spu_init_channels(struct spu *spu) |
| 339 | { |
| 340 | static const struct { |
| 341 | unsigned channel; |
| 342 | unsigned count; |
| 343 | } zero_list[] = { |
| 344 | { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, }, |
| 345 | { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, }, |
| 346 | }, count_list[] = { |
| 347 | { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, }, |
| 348 | { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, }, |
| 349 | { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, }, |
| 350 | }; |
Arnd Bergmann | 6ff730c | 2006-01-04 20:31:31 +0100 | [diff] [blame] | 351 | struct spu_priv2 __iomem *priv2; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 352 | int i; |
| 353 | |
| 354 | priv2 = spu->priv2; |
| 355 | |
| 356 | /* initialize all channel data to zero */ |
| 357 | for (i = 0; i < ARRAY_SIZE(zero_list); i++) { |
| 358 | int count; |
| 359 | |
| 360 | out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel); |
| 361 | for (count = 0; count < zero_list[i].count; count++) |
| 362 | out_be64(&priv2->spu_chnldata_RW, 0); |
| 363 | } |
| 364 | |
| 365 | /* initialize channel counts to meaningful values */ |
| 366 | for (i = 0; i < ARRAY_SIZE(count_list); i++) { |
| 367 | out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel); |
| 368 | out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count); |
| 369 | } |
| 370 | } |
| 371 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 372 | struct spu *spu_alloc_node(int node) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 373 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 374 | struct spu *spu = NULL; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 375 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 376 | mutex_lock(&spu_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 377 | if (!list_empty(&spu_list[node])) { |
| 378 | spu = list_entry(spu_list[node].next, struct spu, list); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 379 | list_del_init(&spu->list); |
Geoff Levand | cc21a66 | 2006-10-24 18:31:15 +0200 | [diff] [blame] | 380 | pr_debug("Got SPU %d %d\n", spu->number, spu->node); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 381 | spu_init_channels(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 382 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 383 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 384 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 385 | return spu; |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(spu_alloc_node); |
| 388 | |
| 389 | struct spu *spu_alloc(void) |
| 390 | { |
| 391 | struct spu *spu = NULL; |
| 392 | int node; |
| 393 | |
| 394 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 395 | spu = spu_alloc_node(node); |
| 396 | if (spu) |
| 397 | break; |
| 398 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 399 | |
| 400 | return spu; |
| 401 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 402 | |
| 403 | void spu_free(struct spu *spu) |
| 404 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 405 | mutex_lock(&spu_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 406 | list_add_tail(&spu->list, &spu_list[spu->node]); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 407 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 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 | 79c227a | 2006-03-24 19:49:27 +0100 | [diff] [blame] | 487 | if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) { |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 488 | u64 flags; |
| 489 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 490 | access = (_PAGE_PRESENT | _PAGE_USER); |
| 491 | access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL; |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 492 | local_irq_save(flags); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 493 | if (hash_page(ea, access, 0x300) != 0) |
| 494 | error |= CLASS1_ENABLE_STORAGE_FAULT_INTR; |
Arnd Bergmann | f807221 | 2006-04-29 02:40:21 +0200 | [diff] [blame] | 495 | local_irq_restore(flags); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 496 | } |
Arnd Bergmann | 79c227a | 2006-03-24 19:49:27 +0100 | [diff] [blame] | 497 | if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 498 | if ((ret = spu_handle_mm_fault(spu)) != 0) |
| 499 | error |= CLASS1_ENABLE_STORAGE_FAULT_INTR; |
| 500 | else |
| 501 | error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR; |
| 502 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 503 | spu->dar = 0UL; |
| 504 | spu->dsisr = 0UL; |
| 505 | if (!error) { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 506 | spu_restart_dma(spu); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 507 | } else { |
| 508 | __spu_trap_invalid_dma(spu); |
| 509 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 510 | return ret; |
| 511 | } |
| 512 | |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 513 | static int __init find_spu_node_id(struct device_node *spe) |
| 514 | { |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 515 | const unsigned int *id; |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 516 | struct device_node *cpu; |
| 517 | cpu = spe->parent->parent; |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 518 | id = get_property(cpu, "node-id", NULL); |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 519 | return id ? *id : 0; |
| 520 | } |
| 521 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 522 | static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe, |
| 523 | const char *prop) |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 524 | { |
| 525 | static DEFINE_MUTEX(add_spumem_mutex); |
| 526 | |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 527 | const struct address_prop { |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 528 | unsigned long address; |
| 529 | unsigned int len; |
| 530 | } __attribute__((packed)) *p; |
| 531 | int proplen; |
| 532 | |
| 533 | unsigned long start_pfn, nr_pages; |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 534 | struct pglist_data *pgdata; |
| 535 | struct zone *zone; |
| 536 | int ret; |
| 537 | |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 538 | p = get_property(spe, prop, &proplen); |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 539 | WARN_ON(proplen != sizeof (*p)); |
| 540 | |
| 541 | start_pfn = p->address >> PAGE_SHIFT; |
| 542 | nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 543 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 544 | pgdata = NODE_DATA(spu->nid); |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 545 | zone = pgdata->node_zones; |
| 546 | |
| 547 | /* XXX rethink locking here */ |
| 548 | mutex_lock(&add_spumem_mutex); |
| 549 | ret = __add_pages(zone, start_pfn, nr_pages); |
| 550 | mutex_unlock(&add_spumem_mutex); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 555 | static void __iomem * __init map_spe_prop(struct spu *spu, |
| 556 | struct device_node *n, const char *name) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 557 | { |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 558 | const struct address_prop { |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 559 | unsigned long address; |
| 560 | unsigned int len; |
| 561 | } __attribute__((packed)) *prop; |
| 562 | |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 563 | const void *p; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 564 | int proplen; |
Al Viro | ed2bfcd | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 565 | void __iomem *ret = NULL; |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 566 | int err = 0; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 567 | |
| 568 | p = get_property(n, name, &proplen); |
| 569 | if (proplen != sizeof (struct address_prop)) |
| 570 | return NULL; |
| 571 | |
| 572 | prop = p; |
| 573 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 574 | err = cell_spuprop_present(spu, n, name); |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 575 | if (err && (err != -EEXIST)) |
| 576 | goto out; |
| 577 | |
| 578 | ret = ioremap(prop->address, prop->len); |
| 579 | |
| 580 | out: |
| 581 | return ret; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static void spu_unmap(struct spu *spu) |
| 585 | { |
| 586 | iounmap(spu->priv2); |
| 587 | iounmap(spu->priv1); |
| 588 | iounmap(spu->problem); |
Al Viro | ed2bfcd | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 589 | iounmap((__force u8 __iomem *)spu->local_store); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 590 | } |
| 591 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 592 | /* This function shall be abstracted for HV platforms */ |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 593 | static int __init spu_map_interrupts_old(struct spu *spu, struct device_node *np) |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 594 | { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 595 | unsigned int isrc; |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 596 | const u32 *tmp; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 597 | |
Benjamin Herrenschmidt | 2e19458 | 2006-09-29 15:00:29 +1000 | [diff] [blame] | 598 | /* Get the interrupt source unit from the device-tree */ |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 599 | tmp = get_property(np, "isrc", NULL); |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 600 | if (!tmp) |
| 601 | return -ENODEV; |
Benjamin Herrenschmidt | 2e19458 | 2006-09-29 15:00:29 +1000 | [diff] [blame] | 602 | isrc = tmp[0]; |
| 603 | |
| 604 | /* Add the node number */ |
| 605 | isrc |= spu->node << IIC_IRQ_NODE_SHIFT; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 606 | |
| 607 | /* Now map interrupts of all 3 classes */ |
Benjamin Herrenschmidt | 2e19458 | 2006-09-29 15:00:29 +1000 | [diff] [blame] | 608 | spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc); |
| 609 | spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc); |
| 610 | spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc); |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 611 | |
| 612 | /* Right now, we only fail if class 2 failed */ |
| 613 | return spu->irqs[2] == NO_IRQ ? -EINVAL : 0; |
| 614 | } |
| 615 | |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 616 | static int __init spu_map_device_old(struct spu *spu, struct device_node *node) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 617 | { |
Jeremy Kerr | c61c27d | 2006-07-12 15:39:54 +1000 | [diff] [blame] | 618 | const char *prop; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 619 | int ret; |
| 620 | |
| 621 | ret = -ENODEV; |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 622 | spu->name = get_property(node, "name", NULL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 623 | if (!spu->name) |
| 624 | goto out; |
| 625 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 626 | prop = get_property(node, "local-store", NULL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 627 | if (!prop) |
| 628 | goto out; |
| 629 | spu->local_store_phys = *(unsigned long *)prop; |
| 630 | |
| 631 | /* we use local store as ram, not io memory */ |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 632 | spu->local_store = (void __force *) |
| 633 | map_spe_prop(spu, node, "local-store"); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 634 | if (!spu->local_store) |
| 635 | goto out; |
| 636 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 637 | prop = get_property(node, "problem", NULL); |
Mark Nutter | 6df10a8 | 2006-03-23 00:00:12 +0100 | [diff] [blame] | 638 | if (!prop) |
| 639 | goto out_unmap; |
| 640 | spu->problem_phys = *(unsigned long *)prop; |
| 641 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 642 | spu->problem= map_spe_prop(spu, node, "problem"); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 643 | if (!spu->problem) |
| 644 | goto out_unmap; |
| 645 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 646 | spu->priv1= map_spe_prop(spu, node, "priv1"); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 647 | /* priv1 is not available on a hypervisor */ |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 648 | |
Jeremy Kerr | 8261aa6 | 2006-05-01 12:16:13 -0700 | [diff] [blame] | 649 | spu->priv2= map_spe_prop(spu, node, "priv2"); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 650 | if (!spu->priv2) |
| 651 | goto out_unmap; |
| 652 | ret = 0; |
| 653 | goto out; |
| 654 | |
| 655 | out_unmap: |
| 656 | spu_unmap(spu); |
| 657 | out: |
| 658 | return ret; |
| 659 | } |
| 660 | |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 661 | static int __init spu_map_interrupts(struct spu *spu, struct device_node *np) |
| 662 | { |
| 663 | struct of_irq oirq; |
| 664 | int ret; |
| 665 | int i; |
| 666 | |
| 667 | for (i=0; i < 3; i++) { |
| 668 | ret = of_irq_map_one(np, i, &oirq); |
| 669 | if (ret) |
| 670 | goto err; |
| 671 | |
| 672 | ret = -EINVAL; |
| 673 | spu->irqs[i] = irq_create_of_mapping(oirq.controller, |
| 674 | oirq.specifier, oirq.size); |
| 675 | if (spu->irqs[i] == NO_IRQ) |
| 676 | goto err; |
| 677 | } |
| 678 | return 0; |
| 679 | |
| 680 | err: |
| 681 | pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier, spu->name); |
| 682 | for (; i >= 0; i--) { |
| 683 | if (spu->irqs[i] != NO_IRQ) |
| 684 | irq_dispose_mapping(spu->irqs[i]); |
| 685 | } |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | static int spu_map_resource(struct device_node *node, int nr, |
| 690 | void __iomem** virt, unsigned long *phys) |
| 691 | { |
| 692 | struct resource resource = { }; |
| 693 | int ret; |
| 694 | |
| 695 | ret = of_address_to_resource(node, 0, &resource); |
| 696 | if (ret) |
| 697 | goto out; |
| 698 | |
| 699 | if (phys) |
| 700 | *phys = resource.start; |
| 701 | *virt = ioremap(resource.start, resource.end - resource.start); |
| 702 | if (!*virt) |
| 703 | ret = -EINVAL; |
| 704 | |
| 705 | out: |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | static int __init spu_map_device(struct spu *spu, struct device_node *node) |
| 710 | { |
| 711 | int ret = -ENODEV; |
| 712 | spu->name = get_property(node, "name", NULL); |
| 713 | if (!spu->name) |
| 714 | goto out; |
| 715 | |
| 716 | ret = spu_map_resource(node, 0, (void __iomem**)&spu->local_store, |
| 717 | &spu->local_store_phys); |
| 718 | if (ret) |
| 719 | goto out; |
| 720 | ret = spu_map_resource(node, 1, (void __iomem**)&spu->problem, |
| 721 | &spu->problem_phys); |
| 722 | if (ret) |
| 723 | goto out_unmap; |
| 724 | ret = spu_map_resource(node, 2, (void __iomem**)&spu->priv2, |
| 725 | NULL); |
| 726 | if (ret) |
| 727 | goto out_unmap; |
| 728 | |
| 729 | if (!firmware_has_feature(FW_FEATURE_LPAR)) |
| 730 | ret = spu_map_resource(node, 3, (void __iomem**)&spu->priv1, |
| 731 | NULL); |
| 732 | if (ret) |
| 733 | goto out_unmap; |
| 734 | return 0; |
| 735 | |
| 736 | out_unmap: |
| 737 | spu_unmap(spu); |
| 738 | out: |
| 739 | pr_debug("failed to map spe %s: %d\n", spu->name, ret); |
| 740 | return ret; |
| 741 | } |
| 742 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 743 | struct sysdev_class spu_sysdev_class = { |
| 744 | set_kset_name("spu") |
| 745 | }; |
| 746 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 747 | static int spu_create_sysdev(struct spu *spu) |
| 748 | { |
| 749 | int ret; |
| 750 | |
| 751 | spu->sysdev.id = spu->number; |
| 752 | spu->sysdev.cls = &spu_sysdev_class; |
| 753 | ret = sysdev_register(&spu->sysdev); |
| 754 | if (ret) { |
| 755 | printk(KERN_ERR "Can't register SPU %d with sysfs\n", |
| 756 | spu->number); |
| 757 | return ret; |
| 758 | } |
| 759 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 760 | sysfs_add_device_to_node(&spu->sysdev, spu->nid); |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static void spu_destroy_sysdev(struct spu *spu) |
| 766 | { |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 767 | sysfs_remove_device_from_node(&spu->sysdev, spu->nid); |
| 768 | sysdev_unregister(&spu->sysdev); |
| 769 | } |
| 770 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 771 | static int __init create_spu(struct device_node *spe) |
| 772 | { |
| 773 | struct spu *spu; |
| 774 | int ret; |
| 775 | static int number; |
| 776 | |
| 777 | ret = -ENOMEM; |
Jeremy Kerr | ecec217 | 2006-06-19 20:33:26 +0200 | [diff] [blame] | 778 | spu = kzalloc(sizeof (*spu), GFP_KERNEL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 779 | if (!spu) |
| 780 | goto out; |
| 781 | |
Benjamin Herrenschmidt | e5267b4 | 2006-10-10 15:14:12 +1000 | [diff] [blame] | 782 | spu->node = find_spu_node_id(spe); |
| 783 | if (spu->node >= MAX_NUMNODES) { |
| 784 | printk(KERN_WARNING "SPE %s on node %d ignored," |
| 785 | " node number too big\n", spe->full_name, spu->node); |
| 786 | printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n"); |
| 787 | return -ENODEV; |
| 788 | } |
| 789 | spu->nid = of_node_to_nid(spe); |
| 790 | if (spu->nid == -1) |
| 791 | spu->nid = 0; |
| 792 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 793 | ret = spu_map_device(spu, spe); |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 794 | /* try old method */ |
| 795 | if (ret) |
| 796 | ret = spu_map_device_old(spu, spe); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 797 | if (ret) |
| 798 | goto out_free; |
| 799 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 800 | ret = spu_map_interrupts(spu, spe); |
| 801 | if (ret) |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 802 | ret = spu_map_interrupts_old(spu, spe); |
| 803 | if (ret) |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 804 | goto out_unmap; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 805 | spin_lock_init(&spu->register_lock); |
Masato Noguchi | 24f43b3 | 2006-10-24 18:31:14 +0200 | [diff] [blame] | 806 | spu_mfc_sdr_setup(spu); |
Arnd Bergmann | f0831ac | 2006-01-04 20:31:30 +0100 | [diff] [blame] | 807 | spu_mfc_sr1_set(spu, 0x33); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 808 | mutex_lock(&spu_mutex); |
Jeremy Kerr | ecec217 | 2006-06-19 20:33:26 +0200 | [diff] [blame] | 809 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 810 | spu->number = number++; |
| 811 | ret = spu_request_irqs(spu); |
| 812 | if (ret) |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 813 | goto out_unlock; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 814 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 815 | ret = spu_create_sysdev(spu); |
| 816 | if (ret) |
| 817 | goto out_free_irqs; |
| 818 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 819 | list_add(&spu->list, &spu_list[spu->node]); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 820 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 821 | |
Geoff Levand | cc21a66 | 2006-10-24 18:31:15 +0200 | [diff] [blame] | 822 | pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n", |
| 823 | spu->name, spu->local_store, |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 824 | spu->problem, spu->priv1, spu->priv2, spu->number); |
| 825 | goto out; |
| 826 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 827 | out_free_irqs: |
| 828 | spu_free_irqs(spu); |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 829 | out_unlock: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 830 | mutex_unlock(&spu_mutex); |
Arnd Bergmann | 7650f2f | 2006-10-04 17:26:20 +0200 | [diff] [blame] | 831 | out_unmap: |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 832 | spu_unmap(spu); |
| 833 | out_free: |
| 834 | kfree(spu); |
| 835 | out: |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | static void destroy_spu(struct spu *spu) |
| 840 | { |
| 841 | list_del_init(&spu->list); |
| 842 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 843 | spu_destroy_sysdev(spu); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 844 | spu_free_irqs(spu); |
| 845 | spu_unmap(spu); |
| 846 | kfree(spu); |
| 847 | } |
| 848 | |
| 849 | static void cleanup_spu_base(void) |
| 850 | { |
| 851 | struct spu *spu, *tmp; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 852 | int node; |
| 853 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 854 | mutex_lock(&spu_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 855 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 856 | list_for_each_entry_safe(spu, tmp, &spu_list[node], list) |
| 857 | destroy_spu(spu); |
| 858 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 859 | mutex_unlock(&spu_mutex); |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 860 | sysdev_class_unregister(&spu_sysdev_class); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 861 | } |
| 862 | module_exit(cleanup_spu_base); |
| 863 | |
| 864 | static int __init init_spu_base(void) |
| 865 | { |
| 866 | struct device_node *node; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 867 | int i, ret; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 868 | |
Jeremy Kerr | 1d64093 | 2006-06-19 20:33:19 +0200 | [diff] [blame] | 869 | /* create sysdev class for spus */ |
| 870 | ret = sysdev_class_register(&spu_sysdev_class); |
| 871 | if (ret) |
| 872 | return ret; |
| 873 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 874 | for (i = 0; i < MAX_NUMNODES; i++) |
| 875 | INIT_LIST_HEAD(&spu_list[i]); |
| 876 | |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 877 | ret = -ENODEV; |
| 878 | for (node = of_find_node_by_type(NULL, "spe"); |
| 879 | node; node = of_find_node_by_type(node, "spe")) { |
| 880 | ret = create_spu(node); |
| 881 | if (ret) { |
| 882 | printk(KERN_WARNING "%s: Error initializing %s\n", |
| 883 | __FUNCTION__, node->name); |
| 884 | cleanup_spu_base(); |
| 885 | break; |
| 886 | } |
| 887 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 888 | return ret; |
| 889 | } |
| 890 | module_init(init_spu_base); |
| 891 | |
| 892 | MODULE_LICENSE("GPL"); |
| 893 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); |