blob: eb367dd89a7dbccd0f959a41c6f92af816d1f068 [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
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 Bergmann3b3d22c2005-12-05 22:52:24 -050023#undef DEBUG
Arnd Bergmann67207b92005-11-15 15:53:48 -050024
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 Molnar14cc3e22006-03-26 01:37:14 -080035#include <linux/mutex.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050036#include <asm/spu.h>
37#include <asm/mmu_context.h>
38
39#include "interrupt.h"
40
41static 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
48static 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
55static 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
62static void spu_restart_dma(struct spu *spu)
63{
64 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -050065
Arnd Bergmann8837d922006-01-04 20:31:28 +010066 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -050067 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -050068}
69
70static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
71{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050072 struct spu_priv2 __iomem *priv2 = spu->priv2;
73 struct mm_struct *mm = spu->mm;
arnd@arndb.de724bd802006-06-19 20:33:23 +020074 u64 esid, vsid, llp;
Arnd Bergmann67207b92005-11-15 15:53:48 -050075
76 pr_debug("%s\n", __FUNCTION__);
77
Arnd Bergmann8837d922006-01-04 20:31:28 +010078 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050079 /* SLBs are pre-loaded for context switch, so
80 * we should never get here!
81 */
Mark Nutter5473af02005-11-15 15:53:49 -050082 printk("%s: invalid access during switch!\n", __func__);
83 return 1;
84 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050085 if (!mm || (REGION_ID(ea) != USER_REGION_ID)) {
86 /* Future: support kernel segments so that drivers
87 * can use SPUs.
88 */
Arnd Bergmann67207b92005-11-15 15:53:48 -050089 pr_debug("invalid region access at %016lx\n", ea);
90 return 1;
91 }
92
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050093 esid = (ea & ESID_MASK) | SLB_ESID_V;
arnd@arndb.de724bd802006-06-19 20:33:23 +020094#ifdef CONFIG_HUGETLB_PAGE
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050095 if (in_hugepage_area(mm->context, ea))
arnd@arndb.de724bd802006-06-19 20:33:23 +020096 llp = mmu_psize_defs[mmu_huge_psize].sllp;
97 else
98#endif
99 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
100 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
101 SLB_VSID_USER | llp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500102
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500103 out_be64(&priv2->slb_index_W, spu->slb_replace);
104 out_be64(&priv2->slb_vsid_RW, vsid);
105 out_be64(&priv2->slb_esid_RW, esid);
106
107 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500108 if (spu->slb_replace >= 8)
109 spu->slb_replace = 0;
110
Arnd Bergmann67207b92005-11-15 15:53:48 -0500111 spu_restart_dma(spu);
112
Arnd Bergmann67207b92005-11-15 15:53:48 -0500113 return 0;
114}
115
Mark Nutter5473af02005-11-15 15:53:49 -0500116extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500117static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500118{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100119 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500120
Mark Nutter5473af02005-11-15 15:53:49 -0500121 /* Handle kernel space hash faults immediately.
122 User hash faults need to be deferred to process context. */
123 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
124 && REGION_ID(ea) != USER_REGION_ID
125 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
126 spu_restart_dma(spu);
127 return 0;
128 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500129
Arnd Bergmann8837d922006-01-04 20:31:28 +0100130 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Mark Nutter5473af02005-11-15 15:53:49 -0500131 printk("%s: invalid access during switch!\n", __func__);
132 return 1;
133 }
134
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500135 spu->dar = ea;
136 spu->dsisr = dsisr;
137 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500138 if (spu->stop_callback)
139 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500140 return 0;
141}
142
143static int __spu_trap_mailbox(struct spu *spu)
144{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500145 if (spu->ibox_callback)
146 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500147
148 /* atomically disable SPU mailbox interrupts */
149 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100150 spu_int_mask_and(spu, 2, ~0x1);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500151 spin_unlock(&spu->register_lock);
152 return 0;
153}
154
155static int __spu_trap_stop(struct spu *spu)
156{
157 pr_debug("%s\n", __FUNCTION__);
158 spu->stop_code = in_be32(&spu->problem->spu_status_R);
Arnd Bergmann51104592005-12-05 22:52:25 -0500159 if (spu->stop_callback)
160 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500161 return 0;
162}
163
164static int __spu_trap_halt(struct spu *spu)
165{
166 pr_debug("%s\n", __FUNCTION__);
167 spu->stop_code = in_be32(&spu->problem->spu_status_R);
Arnd Bergmann51104592005-12-05 22:52:25 -0500168 if (spu->stop_callback)
169 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500170 return 0;
171}
172
173static int __spu_trap_tag_group(struct spu *spu)
174{
175 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100176 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500177 return 0;
178}
179
180static int __spu_trap_spubox(struct spu *spu)
181{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500182 if (spu->wbox_callback)
183 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500184
185 /* atomically disable SPU mailbox interrupts */
186 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100187 spu_int_mask_and(spu, 2, ~0x10);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500188 spin_unlock(&spu->register_lock);
189 return 0;
190}
191
192static irqreturn_t
193spu_irq_class_0(int irq, void *data, struct pt_regs *regs)
194{
195 struct spu *spu;
196
197 spu = data;
198 spu->class_0_pending = 1;
Arnd Bergmann51104592005-12-05 22:52:25 -0500199 if (spu->stop_callback)
200 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500201
202 return IRQ_HANDLED;
203}
204
Arnd Bergmann51104592005-12-05 22:52:25 -0500205int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500206spu_irq_class_0_bottom(struct spu *spu)
207{
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500208 unsigned long stat, mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500209
210 spu->class_0_pending = 0;
211
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100212 mask = spu_int_mask_get(spu, 0);
213 stat = spu_int_stat_get(spu, 0);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500215 stat &= mask;
216
Arnd Bergmann67207b92005-11-15 15:53:48 -0500217 if (stat & 1) /* invalid MFC DMA */
218 __spu_trap_invalid_dma(spu);
219
220 if (stat & 2) /* invalid DMA alignment */
221 __spu_trap_dma_align(spu);
222
223 if (stat & 4) /* error on SPU */
224 __spu_trap_error(spu);
225
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100226 spu_int_stat_clear(spu, 0, stat);
Arnd Bergmann51104592005-12-05 22:52:25 -0500227
228 return (stat & 0x7) ? -EIO : 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500229}
Arnd Bergmann51104592005-12-05 22:52:25 -0500230EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500231
232static irqreturn_t
233spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
234{
235 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500236 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500237
238 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500239
240 /* atomically read & clear class1 status. */
241 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100242 mask = spu_int_mask_get(spu, 1);
243 stat = spu_int_stat_get(spu, 1) & mask;
244 dar = spu_mfc_dar_get(spu);
245 dsisr = spu_mfc_dsisr_get(spu);
Arnd Bergmann38307342005-12-09 19:04:18 +0100246 if (stat & 2) /* mapping fault */
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100247 spu_mfc_dsisr_set(spu, 0ul);
248 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500249 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100250 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
251 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500252
253 if (stat & 1) /* segment fault */
254 __spu_trap_data_seg(spu, dar);
255
256 if (stat & 2) { /* mapping fault */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500257 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500258 }
259
260 if (stat & 4) /* ls compare & suspend on get */
261 ;
262
263 if (stat & 8) /* ls compare & suspend on put */
264 ;
265
Arnd Bergmann67207b92005-11-15 15:53:48 -0500266 return stat ? IRQ_HANDLED : IRQ_NONE;
267}
Arnd Bergmann51104592005-12-05 22:52:25 -0500268EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500269
270static irqreturn_t
271spu_irq_class_2(int irq, void *data, struct pt_regs *regs)
272{
273 struct spu *spu;
274 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500275 unsigned long mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500276
277 spu = data;
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100278 stat = spu_int_stat_get(spu, 2);
279 mask = spu_int_mask_get(spu, 2);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500280
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500281 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500282
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500283 stat &= mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500284
285 if (stat & 1) /* PPC core mailbox */
286 __spu_trap_mailbox(spu);
287
288 if (stat & 2) /* SPU stop-and-signal */
289 __spu_trap_stop(spu);
290
291 if (stat & 4) /* SPU halted */
292 __spu_trap_halt(spu);
293
294 if (stat & 8) /* DMA tag group complete */
295 __spu_trap_tag_group(spu);
296
297 if (stat & 0x10) /* SPU mailbox threshold */
298 __spu_trap_spubox(spu);
299
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100300 spu_int_stat_clear(spu, 2, stat);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500301 return stat ? IRQ_HANDLED : IRQ_NONE;
302}
303
304static int
305spu_request_irqs(struct spu *spu)
306{
307 int ret;
308 int irq_base;
309
310 irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
311
312 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number);
313 ret = request_irq(irq_base + spu->isrc,
Arnd Bergmannf8072212006-04-29 02:40:21 +0200314 spu_irq_class_0, SA_INTERRUPT, spu->irq_c0, spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500315 if (ret)
316 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500317
318 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number);
319 ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc,
Arnd Bergmannf8072212006-04-29 02:40:21 +0200320 spu_irq_class_1, SA_INTERRUPT, spu->irq_c1, spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500321 if (ret)
322 goto out1;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500323
324 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number);
325 ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc,
Arnd Bergmannf8072212006-04-29 02:40:21 +0200326 spu_irq_class_2, SA_INTERRUPT, spu->irq_c2, spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500327 if (ret)
328 goto out2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500329 goto out;
330
331out2:
332 free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
333out1:
334 free_irq(irq_base + spu->isrc, spu);
335out:
336 return ret;
337}
338
339static void
340spu_free_irqs(struct spu *spu)
341{
342 int irq_base;
343
344 irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
345
346 free_irq(irq_base + spu->isrc, spu);
347 free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
348 free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu);
349}
350
351static LIST_HEAD(spu_list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800352static DEFINE_MUTEX(spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500353
354static void spu_init_channels(struct spu *spu)
355{
356 static const struct {
357 unsigned channel;
358 unsigned count;
359 } zero_list[] = {
360 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
361 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
362 }, count_list[] = {
363 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
364 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
365 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
366 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100367 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500368 int i;
369
370 priv2 = spu->priv2;
371
372 /* initialize all channel data to zero */
373 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
374 int count;
375
376 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
377 for (count = 0; count < zero_list[i].count; count++)
378 out_be64(&priv2->spu_chnldata_RW, 0);
379 }
380
381 /* initialize channel counts to meaningful values */
382 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
383 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
384 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
385 }
386}
387
Arnd Bergmann67207b92005-11-15 15:53:48 -0500388struct spu *spu_alloc(void)
389{
390 struct spu *spu;
391
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800392 mutex_lock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500393 if (!list_empty(&spu_list)) {
394 spu = list_entry(spu_list.next, struct spu, list);
395 list_del_init(&spu->list);
396 pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
397 } else {
398 pr_debug("No SPU left\n");
399 spu = NULL;
400 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800401 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500402
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100403 if (spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500404 spu_init_channels(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500405
406 return spu;
407}
Arnd Bergmann39c73c32005-12-05 22:52:21 -0500408EXPORT_SYMBOL_GPL(spu_alloc);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500409
410void spu_free(struct spu *spu)
411{
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800412 mutex_lock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500413 list_add_tail(&spu->list, &spu_list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800414 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500415}
Arnd Bergmann39c73c32005-12-05 22:52:21 -0500416EXPORT_SYMBOL_GPL(spu_free);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500417
Arnd Bergmann67207b92005-11-15 15:53:48 -0500418static int spu_handle_mm_fault(struct spu *spu)
419{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500420 struct mm_struct *mm = spu->mm;
421 struct vm_area_struct *vma;
422 u64 ea, dsisr, is_write;
423 int ret;
424
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500425 ea = spu->dar;
426 dsisr = spu->dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500427#if 0
428 if (!IS_VALID_EA(ea)) {
429 return -EFAULT;
430 }
431#endif /* XXX */
432 if (mm == NULL) {
433 return -EFAULT;
434 }
435 if (mm->pgd == NULL) {
436 return -EFAULT;
437 }
438
439 down_read(&mm->mmap_sem);
440 vma = find_vma(mm, ea);
441 if (!vma)
442 goto bad_area;
443 if (vma->vm_start <= ea)
444 goto good_area;
445 if (!(vma->vm_flags & VM_GROWSDOWN))
446 goto bad_area;
447#if 0
448 if (expand_stack(vma, ea))
449 goto bad_area;
450#endif /* XXX */
451good_area:
452 is_write = dsisr & MFC_DSISR_ACCESS_PUT;
453 if (is_write) {
454 if (!(vma->vm_flags & VM_WRITE))
455 goto bad_area;
456 } else {
457 if (dsisr & MFC_DSISR_ACCESS_DENIED)
458 goto bad_area;
459 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
460 goto bad_area;
461 }
462 ret = 0;
463 switch (handle_mm_fault(mm, vma, ea, is_write)) {
464 case VM_FAULT_MINOR:
465 current->min_flt++;
466 break;
467 case VM_FAULT_MAJOR:
468 current->maj_flt++;
469 break;
470 case VM_FAULT_SIGBUS:
471 ret = -EFAULT;
472 goto bad_area;
473 case VM_FAULT_OOM:
474 ret = -ENOMEM;
475 goto bad_area;
476 default:
477 BUG();
478 }
479 up_read(&mm->mmap_sem);
480 return ret;
481
482bad_area:
483 up_read(&mm->mmap_sem);
484 return -EFAULT;
485}
486
Arnd Bergmann51104592005-12-05 22:52:25 -0500487int spu_irq_class_1_bottom(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500488{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500489 u64 ea, dsisr, access, error = 0UL;
490 int ret = 0;
491
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500492 ea = spu->dar;
493 dsisr = spu->dsisr;
Arnd Bergmann79c227a2006-03-24 19:49:27 +0100494 if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
Arnd Bergmannf8072212006-04-29 02:40:21 +0200495 u64 flags;
496
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500497 access = (_PAGE_PRESENT | _PAGE_USER);
498 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
Arnd Bergmannf8072212006-04-29 02:40:21 +0200499 local_irq_save(flags);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500500 if (hash_page(ea, access, 0x300) != 0)
501 error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
Arnd Bergmannf8072212006-04-29 02:40:21 +0200502 local_irq_restore(flags);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500503 }
Arnd Bergmann79c227a2006-03-24 19:49:27 +0100504 if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500505 if ((ret = spu_handle_mm_fault(spu)) != 0)
506 error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
507 else
508 error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
509 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500510 spu->dar = 0UL;
511 spu->dsisr = 0UL;
512 if (!error) {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500513 spu_restart_dma(spu);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500514 } else {
515 __spu_trap_invalid_dma(spu);
516 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500517 return ret;
518}
519
Arnd Bergmann2fb9d202006-01-05 14:05:29 +0000520void spu_irq_setaffinity(struct spu *spu, int cpu)
521{
522 u64 target = iic_get_target_id(cpu);
523 u64 route = target << 48 | target << 32 | target << 16;
524 spu_int_route_set(spu, route);
525}
526EXPORT_SYMBOL_GPL(spu_irq_setaffinity);
527
Joel H Schoppbed120c2006-05-01 12:16:11 -0700528static int __init find_spu_node_id(struct device_node *spe)
529{
530 unsigned int *id;
531 struct device_node *cpu;
532 cpu = spe->parent->parent;
533 id = (unsigned int *)get_property(cpu, "node-id", NULL);
534 return id ? *id : 0;
535}
536
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700537static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
538 const char *prop)
Joel H Schoppbed120c2006-05-01 12:16:11 -0700539{
540 static DEFINE_MUTEX(add_spumem_mutex);
541
542 struct address_prop {
543 unsigned long address;
544 unsigned int len;
545 } __attribute__((packed)) *p;
546 int proplen;
547
548 unsigned long start_pfn, nr_pages;
Joel H Schoppbed120c2006-05-01 12:16:11 -0700549 struct pglist_data *pgdata;
550 struct zone *zone;
551 int ret;
552
553 p = (void*)get_property(spe, prop, &proplen);
554 WARN_ON(proplen != sizeof (*p));
555
556 start_pfn = p->address >> PAGE_SHIFT;
557 nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
558
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700559 pgdata = NODE_DATA(spu->nid);
Joel H Schoppbed120c2006-05-01 12:16:11 -0700560 zone = pgdata->node_zones;
561
562 /* XXX rethink locking here */
563 mutex_lock(&add_spumem_mutex);
564 ret = __add_pages(zone, start_pfn, nr_pages);
565 mutex_unlock(&add_spumem_mutex);
566
567 return ret;
568}
569
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700570static void __iomem * __init map_spe_prop(struct spu *spu,
571 struct device_node *n, const char *name)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500572{
573 struct address_prop {
574 unsigned long address;
575 unsigned int len;
576 } __attribute__((packed)) *prop;
577
578 void *p;
579 int proplen;
Joel H Schoppbed120c2006-05-01 12:16:11 -0700580 void* ret = NULL;
581 int err = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500582
583 p = get_property(n, name, &proplen);
584 if (proplen != sizeof (struct address_prop))
585 return NULL;
586
587 prop = p;
588
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700589 err = cell_spuprop_present(spu, n, name);
Joel H Schoppbed120c2006-05-01 12:16:11 -0700590 if (err && (err != -EEXIST))
591 goto out;
592
593 ret = ioremap(prop->address, prop->len);
594
595 out:
596 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500597}
598
599static void spu_unmap(struct spu *spu)
600{
601 iounmap(spu->priv2);
602 iounmap(spu->priv1);
603 iounmap(spu->problem);
604 iounmap((u8 __iomem *)spu->local_store);
605}
606
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700607static int __init spu_map_device(struct spu *spu, struct device_node *node)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500608{
609 char *prop;
610 int ret;
611
612 ret = -ENODEV;
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700613 prop = get_property(node, "isrc", NULL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500614 if (!prop)
615 goto out;
616 spu->isrc = *(unsigned int *)prop;
617
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700618 spu->name = get_property(node, "name", NULL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500619 if (!spu->name)
620 goto out;
621
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700622 prop = get_property(node, "local-store", NULL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500623 if (!prop)
624 goto out;
625 spu->local_store_phys = *(unsigned long *)prop;
626
627 /* we use local store as ram, not io memory */
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700628 spu->local_store = (void __force *)
629 map_spe_prop(spu, node, "local-store");
Arnd Bergmann67207b92005-11-15 15:53:48 -0500630 if (!spu->local_store)
631 goto out;
632
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700633 prop = get_property(node, "problem", NULL);
Mark Nutter6df10a82006-03-23 00:00:12 +0100634 if (!prop)
635 goto out_unmap;
636 spu->problem_phys = *(unsigned long *)prop;
637
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700638 spu->problem= map_spe_prop(spu, node, "problem");
Arnd Bergmann67207b92005-11-15 15:53:48 -0500639 if (!spu->problem)
640 goto out_unmap;
641
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700642 spu->priv1= map_spe_prop(spu, node, "priv1");
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100643 /* priv1 is not available on a hypervisor */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500644
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700645 spu->priv2= map_spe_prop(spu, node, "priv2");
Arnd Bergmann67207b92005-11-15 15:53:48 -0500646 if (!spu->priv2)
647 goto out_unmap;
648 ret = 0;
649 goto out;
650
651out_unmap:
652 spu_unmap(spu);
653out:
654 return ret;
655}
656
Jeremy Kerr1d640932006-06-19 20:33:19 +0200657struct sysdev_class spu_sysdev_class = {
658 set_kset_name("spu")
659};
660
661static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
662{
663 struct spu *spu = container_of(sysdev, struct spu, sysdev);
664 return sprintf(buf, "%d\n", spu->isrc);
665
666}
667static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
668
669extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
670
671static int spu_create_sysdev(struct spu *spu)
672{
673 int ret;
674
675 spu->sysdev.id = spu->number;
676 spu->sysdev.cls = &spu_sysdev_class;
677 ret = sysdev_register(&spu->sysdev);
678 if (ret) {
679 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
680 spu->number);
681 return ret;
682 }
683
684 sysdev_create_file(&spu->sysdev, &attr_isrc);
685 sysfs_add_device_to_node(&spu->sysdev, spu->nid);
686
687 return 0;
688}
689
690static void spu_destroy_sysdev(struct spu *spu)
691{
692 sysdev_remove_file(&spu->sysdev, &attr_isrc);
693 sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
694 sysdev_unregister(&spu->sysdev);
695}
696
Arnd Bergmann67207b92005-11-15 15:53:48 -0500697static int __init create_spu(struct device_node *spe)
698{
699 struct spu *spu;
700 int ret;
701 static int number;
702
703 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200704 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500705 if (!spu)
706 goto out;
707
708 ret = spu_map_device(spu, spe);
709 if (ret)
710 goto out_free;
711
712 spu->node = find_spu_node_id(spe);
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700713 spu->nid = of_node_to_nid(spe);
714 if (spu->nid == -1)
715 spu->nid = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500716 spin_lock_init(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100717 spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
718 spu_mfc_sr1_set(spu, 0x33);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800719 mutex_lock(&spu_mutex);
Jeremy Kerrecec2172006-06-19 20:33:26 +0200720
Arnd Bergmann67207b92005-11-15 15:53:48 -0500721 spu->number = number++;
722 ret = spu_request_irqs(spu);
723 if (ret)
724 goto out_unmap;
725
Jeremy Kerr1d640932006-06-19 20:33:19 +0200726 ret = spu_create_sysdev(spu);
727 if (ret)
728 goto out_free_irqs;
729
Arnd Bergmann67207b92005-11-15 15:53:48 -0500730 list_add(&spu->list, &spu_list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800731 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500732
733 pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
734 spu->name, spu->isrc, spu->local_store,
735 spu->problem, spu->priv1, spu->priv2, spu->number);
736 goto out;
737
Jeremy Kerr1d640932006-06-19 20:33:19 +0200738out_free_irqs:
739 spu_free_irqs(spu);
740
Arnd Bergmann67207b92005-11-15 15:53:48 -0500741out_unmap:
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800742 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500743 spu_unmap(spu);
744out_free:
745 kfree(spu);
746out:
747 return ret;
748}
749
750static void destroy_spu(struct spu *spu)
751{
752 list_del_init(&spu->list);
753
Jeremy Kerr1d640932006-06-19 20:33:19 +0200754 spu_destroy_sysdev(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500755 spu_free_irqs(spu);
756 spu_unmap(spu);
757 kfree(spu);
758}
759
760static void cleanup_spu_base(void)
761{
762 struct spu *spu, *tmp;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800763 mutex_lock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500764 list_for_each_entry_safe(spu, tmp, &spu_list, list)
765 destroy_spu(spu);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800766 mutex_unlock(&spu_mutex);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200767 sysdev_class_unregister(&spu_sysdev_class);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500768}
769module_exit(cleanup_spu_base);
770
771static int __init init_spu_base(void)
772{
773 struct device_node *node;
774 int ret;
775
Jeremy Kerr1d640932006-06-19 20:33:19 +0200776 /* create sysdev class for spus */
777 ret = sysdev_class_register(&spu_sysdev_class);
778 if (ret)
779 return ret;
780
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781 ret = -ENODEV;
782 for (node = of_find_node_by_type(NULL, "spe");
783 node; node = of_find_node_by_type(node, "spe")) {
784 ret = create_spu(node);
785 if (ret) {
786 printk(KERN_WARNING "%s: Error initializing %s\n",
787 __FUNCTION__, node->name);
788 cleanup_spu_base();
789 break;
790 }
791 }
792 /* in some old firmware versions, the spe is called 'spc', so we
793 look for that as well */
794 for (node = of_find_node_by_type(NULL, "spc");
795 node; node = of_find_node_by_type(node, "spc")) {
796 ret = create_spu(node);
797 if (ret) {
798 printk(KERN_WARNING "%s: Error initializing %s\n",
799 __FUNCTION__, node->name);
800 cleanup_spu_base();
801 break;
802 }
803 }
804 return ret;
805}
806module_init(init_spu_base);
807
808MODULE_LICENSE("GPL");
809MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");