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)
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>
24 #include <linux/module.h>
27 #include <asm/spu_csa.h>
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.
36 static int spu_handle_mm_fault(struct mm_struct
*mm
, unsigned long ea
, unsigned long dsisr
)
38 struct vm_area_struct
*vma
;
39 unsigned long is_write
;
43 if (!IS_VALID_EA(ea
)) {
50 if (mm
->pgd
== NULL
) {
54 down_read(&mm
->mmap_sem
);
55 vma
= find_vma(mm
, ea
);
58 if (vma
->vm_start
<= ea
)
60 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
62 if (expand_stack(vma
, ea
))
65 is_write
= dsisr
& MFC_DSISR_ACCESS_PUT
;
67 if (!(vma
->vm_flags
& VM_WRITE
))
70 if (dsisr
& MFC_DSISR_ACCESS_DENIED
)
72 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
76 switch (handle_mm_fault(mm
, vma
, ea
, is_write
)) {
92 up_read(&mm
->mmap_sem
);
96 up_read(&mm
->mmap_sem
);
100 static void spufs_handle_dma_error(struct spu_context
*ctx
,
101 unsigned long ea
, int type
)
103 if (ctx
->flags
& SPU_CREATE_EVENTS_ENABLED
) {
104 ctx
->event_return
|= type
;
105 wake_up_all(&ctx
->stop_wq
);
108 memset(&info
, 0, sizeof(info
));
111 case SPE_EVENT_INVALID_DMA
:
112 info
.si_signo
= SIGBUS
;
113 info
.si_code
= BUS_OBJERR
;
115 case SPE_EVENT_SPE_DATA_STORAGE
:
116 info
.si_signo
= SIGBUS
;
117 info
.si_addr
= (void __user
*)ea
;
118 info
.si_code
= BUS_ADRERR
;
120 case SPE_EVENT_DMA_ALIGNMENT
:
121 info
.si_signo
= SIGBUS
;
122 /* DAR isn't set for an alignment fault :( */
123 info
.si_code
= BUS_ADRALN
;
125 case SPE_EVENT_SPE_ERROR
:
126 info
.si_signo
= SIGILL
;
127 info
.si_addr
= (void __user
*)(unsigned long)
128 ctx
->ops
->npc_read(ctx
) - 4;
129 info
.si_code
= ILL_ILLOPC
;
133 force_sig_info(info
.si_signo
, &info
, current
);
137 void spufs_dma_callback(struct spu
*spu
, int type
)
139 spufs_handle_dma_error(spu
->ctx
, spu
->dar
, type
);
141 EXPORT_SYMBOL_GPL(spufs_dma_callback
);
144 * bottom half handler for page faults, we can't do this from
145 * interrupt context, since we might need to sleep.
146 * we also need to give up the mutex so we can get scheduled
147 * out while waiting for the backing store.
149 * TODO: try calling hash_page from the interrupt handler first
150 * in order to speed up the easy case.
152 int spufs_handle_class1(struct spu_context
*ctx
)
154 u64 ea
, dsisr
, access
;
159 * dar and dsisr get passed from the registers
160 * to the spu_context, to this function, but not
161 * back to the spu if it gets scheduled again.
163 * if we don't handle the fault for a saved context
164 * in time, we can still expect to get the same fault
165 * the immediately after the context restore.
167 if (ctx
->state
== SPU_STATE_RUNNABLE
) {
169 dsisr
= ctx
->spu
->dsisr
;
170 ctx
->spu
->dar
= ctx
->spu
->dsisr
= 0;
172 ea
= ctx
->csa
.priv1
.mfc_dar_RW
;
173 dsisr
= ctx
->csa
.priv1
.mfc_dsisr_RW
;
174 ctx
->csa
.priv1
.mfc_dar_RW
= 0;
175 ctx
->csa
.priv1
.mfc_dsisr_RW
= 0;
178 if (!(dsisr
& (MFC_DSISR_PTE_NOT_FOUND
| MFC_DSISR_ACCESS_DENIED
)))
181 pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx
, ea
,
184 /* we must not hold the lock when entering spu_handle_mm_fault */
187 access
= (_PAGE_PRESENT
| _PAGE_USER
);
188 access
|= (dsisr
& MFC_DSISR_ACCESS_PUT
) ? _PAGE_RW
: 0UL;
189 local_irq_save(flags
);
190 ret
= hash_page(ea
, access
, 0x300);
191 local_irq_restore(flags
);
193 /* hashing failed, so try the actual fault handler */
195 ret
= spu_handle_mm_fault(current
->mm
, ea
, dsisr
);
199 * If we handled the fault successfully and are in runnable
200 * state, restart the DMA.
201 * In case of unhandled error report the problem to user space.
205 ctx
->ops
->restart_dma(ctx
);
207 spufs_handle_dma_error(ctx
, ea
, SPE_EVENT_SPE_DATA_STORAGE
);
211 EXPORT_SYMBOL_GPL(spufs_handle_class1
);