blob: 720e111f1f6a05c975e57fdea4cd7de433b47b93 [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 Kerrd6ad39b2007-12-20 16:39:59 +090031/**
32 * Handle an SPE event, depending on context SPU_CREATE_EVENTS_ENABLED flag.
33 *
34 * If the context was created with events, we just set the return event.
35 * Otherwise, send an appropriate signal to the process.
36 */
37static void spufs_handle_event(struct spu_context *ctx,
Jeremy Kerrc8a1e932007-04-23 21:08:16 +020038 unsigned long ea, int type)
Arnd Bergmann57dace22007-04-23 21:08:15 +020039{
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +090040 siginfo_t info;
41
Arnd Bergmann57dace22007-04-23 21:08:15 +020042 if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
43 ctx->event_return |= type;
44 wake_up_all(&ctx->stop_wq);
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +090045 return;
Arnd Bergmann57dace22007-04-23 21:08:15 +020046 }
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +090047
48 memset(&info, 0, sizeof(info));
49
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 = SIGBUS;
57 info.si_addr = (void __user *)ea;
58 info.si_code = BUS_ADRERR;
59 break;
60 case SPE_EVENT_DMA_ALIGNMENT:
61 info.si_signo = SIGBUS;
62 /* DAR isn't set for an alignment fault :( */
63 info.si_code = BUS_ADRALN;
64 break;
65 case SPE_EVENT_SPE_ERROR:
66 info.si_signo = SIGILL;
67 info.si_addr = (void __user *)(unsigned long)
68 ctx->ops->npc_read(ctx) - 4;
69 info.si_code = ILL_ILLOPC;
70 break;
71 }
72
73 if (info.si_signo)
74 force_sig_info(info.si_signo, &info, current);
Arnd Bergmann57dace22007-04-23 21:08:15 +020075}
76
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +090077int spufs_handle_class0(struct spu_context *ctx)
Arnd Bergmann57dace22007-04-23 21:08:15 +020078{
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +090079 unsigned long stat = ctx->csa.class_0_pending & CLASS0_INTR_MASK;
80
81 if (likely(!stat))
82 return 0;
83
84 if (stat & CLASS0_DMA_ALIGNMENT_INTR)
85 spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_DMA_ALIGNMENT);
86
87 if (stat & CLASS0_INVALID_DMA_COMMAND_INTR)
88 spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_INVALID_DMA);
89
90 if (stat & CLASS0_SPU_ERROR_INTR)
91 spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_SPE_ERROR);
92
93 return -EIO;
Arnd Bergmann57dace22007-04-23 21:08:15 +020094}
Arnd Bergmann57dace22007-04-23 21:08:15 +020095
96/*
97 * bottom half handler for page faults, we can't do this from
98 * interrupt context, since we might need to sleep.
99 * we also need to give up the mutex so we can get scheduled
100 * out while waiting for the backing store.
101 *
102 * TODO: try calling hash_page from the interrupt handler first
103 * in order to speed up the easy case.
104 */
105int spufs_handle_class1(struct spu_context *ctx)
106{
107 u64 ea, dsisr, access;
108 unsigned long flags;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000109 unsigned flt = 0;
Arnd Bergmann57dace22007-04-23 21:08:15 +0200110 int ret;
111
112 /*
113 * dar and dsisr get passed from the registers
114 * to the spu_context, to this function, but not
115 * back to the spu if it gets scheduled again.
116 *
117 * if we don't handle the fault for a saved context
118 * in time, we can still expect to get the same fault
119 * the immediately after the context restore.
120 */
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900121 ea = ctx->csa.dar;
122 dsisr = ctx->csa.dsisr;
Arnd Bergmann57dace22007-04-23 21:08:15 +0200123
124 if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
125 return 0;
126
Andre Detsch27ec41d2007-07-20 21:39:33 +0200127 spuctx_switch_state(ctx, SPU_UTIL_IOWAIT);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000128
Arnd Bergmann57dace22007-04-23 21:08:15 +0200129 pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
130 dsisr, ctx->state);
131
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000132 ctx->stats.hash_flt++;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200133 if (ctx->state == SPU_STATE_RUNNABLE)
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000134 ctx->spu->stats.hash_flt++;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000135
Arnd Bergmann57dace22007-04-23 21:08:15 +0200136 /* we must not hold the lock when entering spu_handle_mm_fault */
137 spu_release(ctx);
138
139 access = (_PAGE_PRESENT | _PAGE_USER);
140 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
141 local_irq_save(flags);
142 ret = hash_page(ea, access, 0x300);
143 local_irq_restore(flags);
144
145 /* hashing failed, so try the actual fault handler */
146 if (ret)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000147 ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
Arnd Bergmann57dace22007-04-23 21:08:15 +0200148
149 spu_acquire(ctx);
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900150
151 /*
152 * Clear dsisr under ctxt lock after handling the fault, so that
153 * time slicing will not preempt the context while the page fault
154 * handler is running. Context switch code removes mappings.
155 */
156 ctx->csa.dar = ctx->csa.dsisr = 0;
157
Arnd Bergmann57dace22007-04-23 21:08:15 +0200158 /*
159 * If we handled the fault successfully and are in runnable
160 * state, restart the DMA.
161 * In case of unhandled error report the problem to user space.
162 */
163 if (!ret) {
Christoph Hellwig80422972007-07-19 12:05:58 -0700164 if (flt & VM_FAULT_MAJOR)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000165 ctx->stats.maj_flt++;
Christoph Hellwig80422972007-07-19 12:05:58 -0700166 else
167 ctx->stats.min_flt++;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000168 if (ctx->state == SPU_STATE_RUNNABLE) {
Christoph Hellwig80422972007-07-19 12:05:58 -0700169 if (flt & VM_FAULT_MAJOR)
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000170 ctx->spu->stats.maj_flt++;
Christoph Hellwig80422972007-07-19 12:05:58 -0700171 else
172 ctx->spu->stats.min_flt++;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000173 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000174
Arnd Bergmann57dace22007-04-23 21:08:15 +0200175 if (ctx->spu)
176 ctx->ops->restart_dma(ctx);
177 } else
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900178 spufs_handle_event(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
Arnd Bergmann57dace22007-04-23 21:08:15 +0200179
Andre Detsch27ec41d2007-07-20 21:39:33 +0200180 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
Arnd Bergmann57dace22007-04-23 21:08:15 +0200181 return ret;
182}