Initial commit
[wrt350n-kernel.git] / arch / powerpc / platforms / cell / spufs / fault.c
blobe46d300e21a5e18cf3b612b214a11af5f9562805
1 /*
2 * Low-level SPU handling
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
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.
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.
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.
22 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
26 #include <asm/spu.h>
27 #include <asm/spu_csa.h>
29 #include "spufs.h"
31 /**
32 * Handle an SPE event, depending on context SPU_CREATE_EVENTS_ENABLED flag.
34 * If the context was created with events, we just set the return event.
35 * Otherwise, send an appropriate signal to the process.
37 static void spufs_handle_event(struct spu_context *ctx,
38 unsigned long ea, int type)
40 siginfo_t info;
42 if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
43 ctx->event_return |= type;
44 wake_up_all(&ctx->stop_wq);
45 return;
48 memset(&info, 0, sizeof(info));
50 switch (type) {
51 case SPE_EVENT_INVALID_DMA:
52 info.si_signo = SIGBUS;
53 info.si_code = BUS_OBJERR;
54 break;
55 case SPE_EVENT_SPE_DATA_STORAGE:
56 info.si_signo = SIGSEGV;
57 info.si_addr = (void __user *)ea;
58 info.si_code = SEGV_ACCERR;
59 ctx->ops->restart_dma(ctx);
60 break;
61 case SPE_EVENT_DMA_ALIGNMENT:
62 info.si_signo = SIGBUS;
63 /* DAR isn't set for an alignment fault :( */
64 info.si_code = BUS_ADRALN;
65 break;
66 case SPE_EVENT_SPE_ERROR:
67 info.si_signo = SIGILL;
68 info.si_addr = (void __user *)(unsigned long)
69 ctx->ops->npc_read(ctx) - 4;
70 info.si_code = ILL_ILLOPC;
71 break;
74 if (info.si_signo)
75 force_sig_info(info.si_signo, &info, current);
78 int spufs_handle_class0(struct spu_context *ctx)
80 unsigned long stat = ctx->csa.class_0_pending & CLASS0_INTR_MASK;
82 if (likely(!stat))
83 return 0;
85 if (stat & CLASS0_DMA_ALIGNMENT_INTR)
86 spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_DMA_ALIGNMENT);
88 if (stat & CLASS0_INVALID_DMA_COMMAND_INTR)
89 spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_INVALID_DMA);
91 if (stat & CLASS0_SPU_ERROR_INTR)
92 spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_SPE_ERROR);
94 return -EIO;
98 * bottom half handler for page faults, we can't do this from
99 * interrupt context, since we might need to sleep.
100 * we also need to give up the mutex so we can get scheduled
101 * out while waiting for the backing store.
103 * TODO: try calling hash_page from the interrupt handler first
104 * in order to speed up the easy case.
106 int spufs_handle_class1(struct spu_context *ctx)
108 u64 ea, dsisr, access;
109 unsigned long flags;
110 unsigned flt = 0;
111 int ret;
114 * dar and dsisr get passed from the registers
115 * to the spu_context, to this function, but not
116 * back to the spu if it gets scheduled again.
118 * if we don't handle the fault for a saved context
119 * in time, we can still expect to get the same fault
120 * the immediately after the context restore.
122 ea = ctx->csa.dar;
123 dsisr = ctx->csa.dsisr;
125 if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
126 return 0;
128 spuctx_switch_state(ctx, SPU_UTIL_IOWAIT);
130 pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
131 dsisr, ctx->state);
133 ctx->stats.hash_flt++;
134 if (ctx->state == SPU_STATE_RUNNABLE)
135 ctx->spu->stats.hash_flt++;
137 /* we must not hold the lock when entering spu_handle_mm_fault */
138 spu_release(ctx);
140 access = (_PAGE_PRESENT | _PAGE_USER);
141 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
142 local_irq_save(flags);
143 ret = hash_page(ea, access, 0x300);
144 local_irq_restore(flags);
146 /* hashing failed, so try the actual fault handler */
147 if (ret)
148 ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
151 * This is nasty: we need the state_mutex for all the bookkeeping even
152 * if the syscall was interrupted by a signal. ewww.
154 mutex_lock(&ctx->state_mutex);
157 * Clear dsisr under ctxt lock after handling the fault, so that
158 * time slicing will not preempt the context while the page fault
159 * handler is running. Context switch code removes mappings.
161 ctx->csa.dar = ctx->csa.dsisr = 0;
164 * If we handled the fault successfully and are in runnable
165 * state, restart the DMA.
166 * In case of unhandled error report the problem to user space.
168 if (!ret) {
169 if (flt & VM_FAULT_MAJOR)
170 ctx->stats.maj_flt++;
171 else
172 ctx->stats.min_flt++;
173 if (ctx->state == SPU_STATE_RUNNABLE) {
174 if (flt & VM_FAULT_MAJOR)
175 ctx->spu->stats.maj_flt++;
176 else
177 ctx->spu->stats.min_flt++;
180 if (ctx->spu)
181 ctx->ops->restart_dma(ctx);
182 } else
183 spufs_handle_event(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
185 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
186 return ret;