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