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