xref: /linux/arch/powerpc/platforms/cell/spufs/fault.c (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
1*de6cc651SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
257dace23SArnd Bergmann /*
357dace23SArnd Bergmann  * Low-level SPU handling
457dace23SArnd Bergmann  *
557dace23SArnd Bergmann  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
657dace23SArnd Bergmann  *
757dace23SArnd Bergmann  * Author: Arnd Bergmann <arndb@de.ibm.com>
857dace23SArnd Bergmann  */
93f07c014SIngo Molnar #include <linux/sched/signal.h>
1057dace23SArnd Bergmann #include <linux/mm.h>
1157dace23SArnd Bergmann 
1257dace23SArnd Bergmann #include <asm/spu.h>
1357dace23SArnd Bergmann #include <asm/spu_csa.h>
1457dace23SArnd Bergmann 
1557dace23SArnd Bergmann #include "spufs.h"
1657dace23SArnd Bergmann 
17d6ad39bcSJeremy Kerr /**
18d6ad39bcSJeremy Kerr  * Handle an SPE event, depending on context SPU_CREATE_EVENTS_ENABLED flag.
19d6ad39bcSJeremy Kerr  *
20d6ad39bcSJeremy Kerr  * If the context was created with events, we just set the return event.
21d6ad39bcSJeremy Kerr  * Otherwise, send an appropriate signal to the process.
22d6ad39bcSJeremy Kerr  */
spufs_handle_event(struct spu_context * ctx,unsigned long ea,int type)23d6ad39bcSJeremy Kerr static void spufs_handle_event(struct spu_context *ctx,
24c8a1e939SJeremy Kerr 				unsigned long ea, int type)
2557dace23SArnd Bergmann {
2657dace23SArnd Bergmann 	if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
2757dace23SArnd Bergmann 		ctx->event_return |= type;
2857dace23SArnd Bergmann 		wake_up_all(&ctx->stop_wq);
29d6ad39bcSJeremy Kerr 		return;
30d6ad39bcSJeremy Kerr 	}
31d6ad39bcSJeremy Kerr 
3257dace23SArnd Bergmann 	switch (type) {
3357dace23SArnd Bergmann 	case SPE_EVENT_INVALID_DMA:
342e1661d2SEric W. Biederman 		force_sig_fault(SIGBUS, BUS_OBJERR, NULL);
35c8a1e939SJeremy Kerr 		break;
36c8a1e939SJeremy Kerr 	case SPE_EVENT_SPE_DATA_STORAGE:
3718789fb1SAndre Detsch 		ctx->ops->restart_dma(ctx);
382e1661d2SEric W. Biederman 		force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *)ea);
39c8a1e939SJeremy Kerr 		break;
40c8a1e939SJeremy Kerr 	case SPE_EVENT_DMA_ALIGNMENT:
41c8a1e939SJeremy Kerr 		/* DAR isn't set for an alignment fault :( */
422e1661d2SEric W. Biederman 		force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
4357dace23SArnd Bergmann 		break;
4457dace23SArnd Bergmann 	case SPE_EVENT_SPE_ERROR:
45f383d8b4SEric W. Biederman 		force_sig_fault(
46f383d8b4SEric W. Biederman 			SIGILL, ILL_ILLOPC,
47f383d8b4SEric W. Biederman 			(void __user *)(unsigned long)
482e1661d2SEric W. Biederman 			ctx->ops->npc_read(ctx) - 4);
4957dace23SArnd Bergmann 		break;
5057dace23SArnd Bergmann 	}
5157dace23SArnd Bergmann }
5257dace23SArnd Bergmann 
spufs_handle_class0(struct spu_context * ctx)53d6ad39bcSJeremy Kerr int spufs_handle_class0(struct spu_context *ctx)
5457dace23SArnd Bergmann {
55d6ad39bcSJeremy Kerr 	unsigned long stat = ctx->csa.class_0_pending & CLASS0_INTR_MASK;
56d6ad39bcSJeremy Kerr 
57d6ad39bcSJeremy Kerr 	if (likely(!stat))
58d6ad39bcSJeremy Kerr 		return 0;
59d6ad39bcSJeremy Kerr 
60d6ad39bcSJeremy Kerr 	if (stat & CLASS0_DMA_ALIGNMENT_INTR)
61f3d69e05SLuke Browning 		spufs_handle_event(ctx, ctx->csa.class_0_dar,
62f3d69e05SLuke Browning 			SPE_EVENT_DMA_ALIGNMENT);
63d6ad39bcSJeremy Kerr 
64d6ad39bcSJeremy Kerr 	if (stat & CLASS0_INVALID_DMA_COMMAND_INTR)
65f3d69e05SLuke Browning 		spufs_handle_event(ctx, ctx->csa.class_0_dar,
66f3d69e05SLuke Browning 			SPE_EVENT_INVALID_DMA);
67d6ad39bcSJeremy Kerr 
68d6ad39bcSJeremy Kerr 	if (stat & CLASS0_SPU_ERROR_INTR)
69f3d69e05SLuke Browning 		spufs_handle_event(ctx, ctx->csa.class_0_dar,
70f3d69e05SLuke Browning 			SPE_EVENT_SPE_ERROR);
71f3d69e05SLuke Browning 
72f3d69e05SLuke Browning 	ctx->csa.class_0_pending = 0;
73d6ad39bcSJeremy Kerr 
74d6ad39bcSJeremy Kerr 	return -EIO;
7557dace23SArnd Bergmann }
7657dace23SArnd Bergmann 
7757dace23SArnd Bergmann /*
7857dace23SArnd Bergmann  * bottom half handler for page faults, we can't do this from
7957dace23SArnd Bergmann  * interrupt context, since we might need to sleep.
8057dace23SArnd Bergmann  * we also need to give up the mutex so we can get scheduled
8157dace23SArnd Bergmann  * out while waiting for the backing store.
8257dace23SArnd Bergmann  *
8357dace23SArnd Bergmann  * TODO: try calling hash_page from the interrupt handler first
8457dace23SArnd Bergmann  *       in order to speed up the easy case.
8557dace23SArnd Bergmann  */
spufs_handle_class1(struct spu_context * ctx)8657dace23SArnd Bergmann int spufs_handle_class1(struct spu_context *ctx)
8757dace23SArnd Bergmann {
8857dace23SArnd Bergmann 	u64 ea, dsisr, access;
8957dace23SArnd Bergmann 	unsigned long flags;
9050a7ca3cSSouptick Joarder 	vm_fault_t flt = 0;
91eebead5bSChristoph Hellwig 	int ret;
9257dace23SArnd Bergmann 
9357dace23SArnd Bergmann 	/*
9457dace23SArnd Bergmann 	 * dar and dsisr get passed from the registers
9557dace23SArnd Bergmann 	 * to the spu_context, to this function, but not
9657dace23SArnd Bergmann 	 * back to the spu if it gets scheduled again.
9757dace23SArnd Bergmann 	 *
9857dace23SArnd Bergmann 	 * if we don't handle the fault for a saved context
9957dace23SArnd Bergmann 	 * in time, we can still expect to get the same fault
10057dace23SArnd Bergmann 	 * the immediately after the context restore.
10157dace23SArnd Bergmann 	 */
102f3d69e05SLuke Browning 	ea = ctx->csa.class_1_dar;
103f3d69e05SLuke Browning 	dsisr = ctx->csa.class_1_dsisr;
10457dace23SArnd Bergmann 
10557dace23SArnd Bergmann 	if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
10657dace23SArnd Bergmann 		return 0;
10757dace23SArnd Bergmann 
10827ec41d3SAndre Detsch 	spuctx_switch_state(ctx, SPU_UTIL_IOWAIT);
109e9f8a0b6SChristoph Hellwig 
1109477e455SStephen Rothwell 	pr_debug("ctx %p: ea %016llx, dsisr %016llx state %d\n", ctx, ea,
11157dace23SArnd Bergmann 		dsisr, ctx->state);
11257dace23SArnd Bergmann 
113e9f8a0b6SChristoph Hellwig 	ctx->stats.hash_flt++;
11427ec41d3SAndre Detsch 	if (ctx->state == SPU_STATE_RUNNABLE)
115fe2f896dSChristoph Hellwig 		ctx->spu->stats.hash_flt++;
116e9f8a0b6SChristoph Hellwig 
117e83d0169SIan Munsie 	/* we must not hold the lock when entering copro_handle_mm_fault */
11857dace23SArnd Bergmann 	spu_release(ctx);
11957dace23SArnd Bergmann 
120ac29c640SAneesh Kumar K.V 	access = (_PAGE_PRESENT | _PAGE_READ);
121c7d54842SAneesh Kumar K.V 	access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_WRITE : 0UL;
12257dace23SArnd Bergmann 	local_irq_save(flags);
123aefa5688SAneesh Kumar K.V 	ret = hash_page(ea, access, 0x300, dsisr);
12457dace23SArnd Bergmann 	local_irq_restore(flags);
12557dace23SArnd Bergmann 
12657dace23SArnd Bergmann 	/* hashing failed, so try the actual fault handler */
12757dace23SArnd Bergmann 	if (ret)
128e83d0169SIan Munsie 		ret = copro_handle_mm_fault(current->mm, ea, dsisr, &flt);
12957dace23SArnd Bergmann 
130c9101bdbSChristoph Hellwig 	/*
131eebead5bSChristoph Hellwig 	 * This is nasty: we need the state_mutex for all the bookkeeping even
132eebead5bSChristoph Hellwig 	 * if the syscall was interrupted by a signal. ewww.
133c9101bdbSChristoph Hellwig 	 */
134eebead5bSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
135d6ad39bcSJeremy Kerr 
136d6ad39bcSJeremy Kerr 	/*
137d6ad39bcSJeremy Kerr 	 * Clear dsisr under ctxt lock after handling the fault, so that
138d6ad39bcSJeremy Kerr 	 * time slicing will not preempt the context while the page fault
139d6ad39bcSJeremy Kerr 	 * handler is running. Context switch code removes mappings.
140d6ad39bcSJeremy Kerr 	 */
141f3d69e05SLuke Browning 	ctx->csa.class_1_dar = ctx->csa.class_1_dsisr = 0;
142d6ad39bcSJeremy Kerr 
14357dace23SArnd Bergmann 	/*
14457dace23SArnd Bergmann 	 * If we handled the fault successfully and are in runnable
14557dace23SArnd Bergmann 	 * state, restart the DMA.
14657dace23SArnd Bergmann 	 * In case of unhandled error report the problem to user space.
14757dace23SArnd Bergmann 	 */
14857dace23SArnd Bergmann 	if (!ret) {
14980422977SChristoph Hellwig 		if (flt & VM_FAULT_MAJOR)
150e9f8a0b6SChristoph Hellwig 			ctx->stats.maj_flt++;
151fe2f896dSChristoph Hellwig 		else
15280422977SChristoph Hellwig 			ctx->stats.min_flt++;
15380422977SChristoph Hellwig 		if (ctx->state == SPU_STATE_RUNNABLE) {
15480422977SChristoph Hellwig 			if (flt & VM_FAULT_MAJOR)
155fe2f896dSChristoph Hellwig 				ctx->spu->stats.maj_flt++;
15680422977SChristoph Hellwig 			else
15780422977SChristoph Hellwig 				ctx->spu->stats.min_flt++;
158fe2f896dSChristoph Hellwig 		}
159e9f8a0b6SChristoph Hellwig 
16057dace23SArnd Bergmann 		if (ctx->spu)
16157dace23SArnd Bergmann 			ctx->ops->restart_dma(ctx);
16257dace23SArnd Bergmann 	} else
163d6ad39bcSJeremy Kerr 		spufs_handle_event(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
16457dace23SArnd Bergmann 
16527ec41d3SAndre Detsch 	spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
16657dace23SArnd Bergmann 	return ret;
16757dace23SArnd Bergmann }
168