blob: 0635f292ae19fe2e0a90d3fea9dae33297aaa5cf [file] [log] [blame]
Arnd Bergmann57dace22007-04-23 21:08:15 +02001/*
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
Jeremy Kerrc8a1e932007-04-23 21:08:16 +020031static void spufs_handle_dma_error(struct spu_context *ctx,
32 unsigned long ea, int type)
Arnd Bergmann57dace22007-04-23 21:08:15 +020033{
34 if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
35 ctx->event_return |= type;
36 wake_up_all(&ctx->stop_wq);
37 } else {
Jeremy Kerrc8a1e932007-04-23 21:08:16 +020038 siginfo_t info;
39 memset(&info, 0, sizeof(info));
40
Arnd Bergmann57dace22007-04-23 21:08:15 +020041 switch (type) {
Arnd Bergmann57dace22007-04-23 21:08:15 +020042 case SPE_EVENT_INVALID_DMA:
Jeremy Kerrc8a1e932007-04-23 21:08:16 +020043 info.si_signo = SIGBUS;
44 info.si_code = BUS_OBJERR;
45 break;
46 case SPE_EVENT_SPE_DATA_STORAGE:
47 info.si_signo = SIGBUS;
48 info.si_addr = (void __user *)ea;
49 info.si_code = BUS_ADRERR;
50 break;
51 case SPE_EVENT_DMA_ALIGNMENT:
52 info.si_signo = SIGBUS;
53 /* DAR isn't set for an alignment fault :( */
54 info.si_code = BUS_ADRALN;
Arnd Bergmann57dace22007-04-23 21:08:15 +020055 break;
56 case SPE_EVENT_SPE_ERROR:
Jeremy Kerrc8a1e932007-04-23 21:08:16 +020057 info.si_signo = SIGILL;
58 info.si_addr = (void __user *)(unsigned long)
59 ctx->ops->npc_read(ctx) - 4;
60 info.si_code = ILL_ILLOPC;
Arnd Bergmann57dace22007-04-23 21:08:15 +020061 break;
62 }
Jeremy Kerrc8a1e932007-04-23 21:08:16 +020063 if (info.si_signo)
64 force_sig_info(info.si_signo, &info, current);
Arnd Bergmann57dace22007-04-23 21:08:15 +020065 }
66}
67
68void spufs_dma_callback(struct spu *spu, int type)
69{
Jeremy Kerrc8a1e932007-04-23 21:08:16 +020070 spufs_handle_dma_error(spu->ctx, spu->dar, type);
Arnd Bergmann57dace22007-04-23 21:08:15 +020071}
Arnd Bergmann57dace22007-04-23 21:08:15 +020072
73/*
74 * bottom half handler for page faults, we can't do this from
75 * interrupt context, since we might need to sleep.
76 * we also need to give up the mutex so we can get scheduled
77 * out while waiting for the backing store.
78 *
79 * TODO: try calling hash_page from the interrupt handler first
80 * in order to speed up the easy case.
81 */
82int spufs_handle_class1(struct spu_context *ctx)
83{
84 u64 ea, dsisr, access;
85 unsigned long flags;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +100086 unsigned flt = 0;
Arnd Bergmann57dace22007-04-23 21:08:15 +020087 int ret;
88
89 /*
90 * dar and dsisr get passed from the registers
91 * to the spu_context, to this function, but not
92 * back to the spu if it gets scheduled again.
93 *
94 * if we don't handle the fault for a saved context
95 * in time, we can still expect to get the same fault
96 * the immediately after the context restore.
97 */
98 if (ctx->state == SPU_STATE_RUNNABLE) {
99 ea = ctx->spu->dar;
100 dsisr = ctx->spu->dsisr;
101 ctx->spu->dar= ctx->spu->dsisr = 0;
102 } else {
103 ea = ctx->csa.priv1.mfc_dar_RW;
104 dsisr = ctx->csa.priv1.mfc_dsisr_RW;
105 ctx->csa.priv1.mfc_dar_RW = 0;
106 ctx->csa.priv1.mfc_dsisr_RW = 0;
107 }
108
109 if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
110 return 0;
111
Andre Detsch27ec41d2007-07-20 21:39:33 +0200112 spuctx_switch_state(ctx, SPU_UTIL_IOWAIT);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000113
Arnd Bergmann57dace22007-04-23 21:08:15 +0200114 pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
115 dsisr, ctx->state);
116
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000117 ctx->stats.hash_flt++;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200118 if (ctx->state == SPU_STATE_RUNNABLE)
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000119 ctx->spu->stats.hash_flt++;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000120
Arnd Bergmann57dace22007-04-23 21:08:15 +0200121 /* we must not hold the lock when entering spu_handle_mm_fault */
122 spu_release(ctx);
123
124 access = (_PAGE_PRESENT | _PAGE_USER);
125 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
126 local_irq_save(flags);
127 ret = hash_page(ea, access, 0x300);
128 local_irq_restore(flags);
129
130 /* hashing failed, so try the actual fault handler */
131 if (ret)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000132 ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
Arnd Bergmann57dace22007-04-23 21:08:15 +0200133
134 spu_acquire(ctx);
135 /*
136 * If we handled the fault successfully and are in runnable
137 * state, restart the DMA.
138 * In case of unhandled error report the problem to user space.
139 */
140 if (!ret) {
Christoph Hellwig80422972007-07-19 12:05:58 -0700141 if (flt & VM_FAULT_MAJOR)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000142 ctx->stats.maj_flt++;
Christoph Hellwig80422972007-07-19 12:05:58 -0700143 else
144 ctx->stats.min_flt++;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000145 if (ctx->state == SPU_STATE_RUNNABLE) {
Christoph Hellwig80422972007-07-19 12:05:58 -0700146 if (flt & VM_FAULT_MAJOR)
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000147 ctx->spu->stats.maj_flt++;
Christoph Hellwig80422972007-07-19 12:05:58 -0700148 else
149 ctx->spu->stats.min_flt++;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000150 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000151
Arnd Bergmann57dace22007-04-23 21:08:15 +0200152 if (ctx->spu)
153 ctx->ops->restart_dma(ctx);
154 } else
Jeremy Kerrc8a1e932007-04-23 21:08:16 +0200155 spufs_handle_dma_error(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
Arnd Bergmann57dace22007-04-23 21:08:15 +0200156
Andre Detsch27ec41d2007-07-20 21:39:33 +0200157 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
Arnd Bergmann57dace22007-04-23 21:08:15 +0200158 return ret;
159}