e064d0c0d80e6fac6240e86a4f94e0fd0785ca2b
[deliverable/linux.git] / arch / powerpc / platforms / cell / spufs / fault.c
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 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25
26 #include <asm/spu.h>
27 #include <asm/spu_csa.h>
28
29 #include "spufs.h"
30
31 /*
32 * This ought to be kept in sync with the powerpc specific do_page_fault
33 * function. Currently, there are a few corner cases that we haven't had
34 * to handle fortunately.
35 */
36 static int spu_handle_mm_fault(struct mm_struct *mm, unsigned long ea,
37 unsigned long dsisr, unsigned *flt)
38 {
39 struct vm_area_struct *vma;
40 unsigned long is_write;
41 int ret;
42
43 #if 0
44 if (!IS_VALID_EA(ea)) {
45 return -EFAULT;
46 }
47 #endif /* XXX */
48 if (mm == NULL) {
49 return -EFAULT;
50 }
51 if (mm->pgd == NULL) {
52 return -EFAULT;
53 }
54
55 down_read(&mm->mmap_sem);
56 vma = find_vma(mm, ea);
57 if (!vma)
58 goto bad_area;
59 if (vma->vm_start <= ea)
60 goto good_area;
61 if (!(vma->vm_flags & VM_GROWSDOWN))
62 goto bad_area;
63 if (expand_stack(vma, ea))
64 goto bad_area;
65 good_area:
66 is_write = dsisr & MFC_DSISR_ACCESS_PUT;
67 if (is_write) {
68 if (!(vma->vm_flags & VM_WRITE))
69 goto bad_area;
70 } else {
71 if (dsisr & MFC_DSISR_ACCESS_DENIED)
72 goto bad_area;
73 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
74 goto bad_area;
75 }
76 ret = 0;
77 *flt = handle_mm_fault(mm, vma, ea, is_write);
78 switch (*flt) {
79 case VM_FAULT_MINOR:
80 current->min_flt++;
81 break;
82 case VM_FAULT_MAJOR:
83 current->maj_flt++;
84 break;
85 case VM_FAULT_SIGBUS:
86 ret = -EFAULT;
87 goto bad_area;
88 case VM_FAULT_OOM:
89 ret = -ENOMEM;
90 goto bad_area;
91 default:
92 BUG();
93 }
94 up_read(&mm->mmap_sem);
95 return ret;
96
97 bad_area:
98 up_read(&mm->mmap_sem);
99 return -EFAULT;
100 }
101
102 static void spufs_handle_dma_error(struct spu_context *ctx,
103 unsigned long ea, int type)
104 {
105 if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
106 ctx->event_return |= type;
107 wake_up_all(&ctx->stop_wq);
108 } else {
109 siginfo_t info;
110 memset(&info, 0, sizeof(info));
111
112 switch (type) {
113 case SPE_EVENT_INVALID_DMA:
114 info.si_signo = SIGBUS;
115 info.si_code = BUS_OBJERR;
116 break;
117 case SPE_EVENT_SPE_DATA_STORAGE:
118 info.si_signo = SIGBUS;
119 info.si_addr = (void __user *)ea;
120 info.si_code = BUS_ADRERR;
121 break;
122 case SPE_EVENT_DMA_ALIGNMENT:
123 info.si_signo = SIGBUS;
124 /* DAR isn't set for an alignment fault :( */
125 info.si_code = BUS_ADRALN;
126 break;
127 case SPE_EVENT_SPE_ERROR:
128 info.si_signo = SIGILL;
129 info.si_addr = (void __user *)(unsigned long)
130 ctx->ops->npc_read(ctx) - 4;
131 info.si_code = ILL_ILLOPC;
132 break;
133 }
134 if (info.si_signo)
135 force_sig_info(info.si_signo, &info, current);
136 }
137 }
138
139 void spufs_dma_callback(struct spu *spu, int type)
140 {
141 spufs_handle_dma_error(spu->ctx, spu->dar, type);
142 }
143 EXPORT_SYMBOL_GPL(spufs_dma_callback);
144
145 /*
146 * bottom half handler for page faults, we can't do this from
147 * interrupt context, since we might need to sleep.
148 * we also need to give up the mutex so we can get scheduled
149 * out while waiting for the backing store.
150 *
151 * TODO: try calling hash_page from the interrupt handler first
152 * in order to speed up the easy case.
153 */
154 int spufs_handle_class1(struct spu_context *ctx)
155 {
156 u64 ea, dsisr, access;
157 unsigned long flags;
158 unsigned flt = 0;
159 int ret;
160
161 /*
162 * dar and dsisr get passed from the registers
163 * to the spu_context, to this function, but not
164 * back to the spu if it gets scheduled again.
165 *
166 * if we don't handle the fault for a saved context
167 * in time, we can still expect to get the same fault
168 * the immediately after the context restore.
169 */
170 if (ctx->state == SPU_STATE_RUNNABLE) {
171 ea = ctx->spu->dar;
172 dsisr = ctx->spu->dsisr;
173 ctx->spu->dar= ctx->spu->dsisr = 0;
174 } else {
175 ea = ctx->csa.priv1.mfc_dar_RW;
176 dsisr = ctx->csa.priv1.mfc_dsisr_RW;
177 ctx->csa.priv1.mfc_dar_RW = 0;
178 ctx->csa.priv1.mfc_dsisr_RW = 0;
179 }
180
181 if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
182 return 0;
183
184 spuctx_switch_state(ctx, SPUCTX_UTIL_IOWAIT);
185
186 pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
187 dsisr, ctx->state);
188
189 ctx->stats.hash_flt++;
190 if (ctx->state == SPU_STATE_RUNNABLE) {
191 ctx->spu->stats.hash_flt++;
192 spu_switch_state(ctx->spu, SPU_UTIL_IOWAIT);
193 }
194
195 /* we must not hold the lock when entering spu_handle_mm_fault */
196 spu_release(ctx);
197
198 access = (_PAGE_PRESENT | _PAGE_USER);
199 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
200 local_irq_save(flags);
201 ret = hash_page(ea, access, 0x300);
202 local_irq_restore(flags);
203
204 /* hashing failed, so try the actual fault handler */
205 if (ret)
206 ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
207
208 spu_acquire(ctx);
209 /*
210 * If we handled the fault successfully and are in runnable
211 * state, restart the DMA.
212 * In case of unhandled error report the problem to user space.
213 */
214 if (!ret) {
215 if (flt == VM_FAULT_MINOR)
216 ctx->stats.min_flt++;
217 else
218 ctx->stats.maj_flt++;
219 if (ctx->state == SPU_STATE_RUNNABLE) {
220 if (flt == VM_FAULT_MINOR)
221 ctx->spu->stats.min_flt++;
222 else
223 ctx->spu->stats.maj_flt++;
224 }
225
226 if (ctx->spu)
227 ctx->ops->restart_dma(ctx);
228 } else
229 spufs_handle_dma_error(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
230
231 spuctx_switch_state(ctx, SPUCTX_UTIL_SYSTEM);
232 return ret;
233 }
234 EXPORT_SYMBOL_GPL(spufs_handle_class1);
This page took 0.051259 seconds and 4 git commands to generate.