| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Low-level SPU handling | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Author: Arnd Bergmann <arndb@de.ibm.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation; either version 2, or (at your option) | 
					
						
							|  |  |  |  * any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include <linux/sched.h>
 | 
					
						
							|  |  |  | #include <linux/mm.h>
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <asm/spu.h>
 | 
					
						
							|  |  |  | #include <asm/spu_csa.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "spufs.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Handle an SPE event, depending on context SPU_CREATE_EVENTS_ENABLED flag. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * If the context was created with events, we just set the return event. | 
					
						
							|  |  |  |  * Otherwise, send an appropriate signal to the process. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void spufs_handle_event(struct spu_context *ctx, | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:16 +02:00
										 |  |  | 				unsigned long ea, int type) | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 	siginfo_t info; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 	if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) { | 
					
						
							|  |  |  | 		ctx->event_return |= type; | 
					
						
							|  |  |  | 		wake_up_all(&ctx->stop_wq); | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	memset(&info, 0, sizeof(info)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (type) { | 
					
						
							|  |  |  | 	case SPE_EVENT_INVALID_DMA: | 
					
						
							|  |  |  | 		info.si_signo = SIGBUS; | 
					
						
							|  |  |  | 		info.si_code = BUS_OBJERR; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPE_EVENT_SPE_DATA_STORAGE: | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 		info.si_signo = SIGSEGV; | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 		info.si_addr = (void __user *)ea; | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 		info.si_code = SEGV_ACCERR; | 
					
						
							|  |  |  | 		ctx->ops->restart_dma(ctx); | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case SPE_EVENT_DMA_ALIGNMENT: | 
					
						
							|  |  |  | 		info.si_signo = SIGBUS; | 
					
						
							|  |  |  | 		/* DAR isn't set for an alignment fault :( */ | 
					
						
							|  |  |  | 		info.si_code = BUS_ADRALN; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPE_EVENT_SPE_ERROR: | 
					
						
							|  |  |  | 		info.si_signo = SIGILL; | 
					
						
							|  |  |  | 		info.si_addr = (void __user *)(unsigned long) | 
					
						
							|  |  |  | 			ctx->ops->npc_read(ctx) - 4; | 
					
						
							|  |  |  | 		info.si_code = ILL_ILLOPC; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (info.si_signo) | 
					
						
							|  |  |  | 		force_sig_info(info.si_signo, &info, current); | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | int spufs_handle_class0(struct spu_context *ctx) | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 	unsigned long stat = ctx->csa.class_0_pending & CLASS0_INTR_MASK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (likely(!stat)) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (stat & CLASS0_DMA_ALIGNMENT_INTR) | 
					
						
							| 
									
										
										
										
											2008-04-27 18:41:55 +00:00
										 |  |  | 		spufs_handle_event(ctx, ctx->csa.class_0_dar, | 
					
						
							|  |  |  | 			SPE_EVENT_DMA_ALIGNMENT); | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (stat & CLASS0_INVALID_DMA_COMMAND_INTR) | 
					
						
							| 
									
										
										
										
											2008-04-27 18:41:55 +00:00
										 |  |  | 		spufs_handle_event(ctx, ctx->csa.class_0_dar, | 
					
						
							|  |  |  | 			SPE_EVENT_INVALID_DMA); | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (stat & CLASS0_SPU_ERROR_INTR) | 
					
						
							| 
									
										
										
										
											2008-04-27 18:41:55 +00:00
										 |  |  | 		spufs_handle_event(ctx, ctx->csa.class_0_dar, | 
					
						
							|  |  |  | 			SPE_EVENT_SPE_ERROR); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx->csa.class_0_pending = 0; | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return -EIO; | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * bottom half handler for page faults, we can't do this from | 
					
						
							|  |  |  |  * interrupt context, since we might need to sleep. | 
					
						
							|  |  |  |  * we also need to give up the mutex so we can get scheduled | 
					
						
							|  |  |  |  * out while waiting for the backing store. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * TODO: try calling hash_page from the interrupt handler first | 
					
						
							|  |  |  |  *       in order to speed up the easy case. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int spufs_handle_class1(struct spu_context *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u64 ea, dsisr, access; | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:03 +10:00
										 |  |  | 	unsigned flt = 0; | 
					
						
							| 
									
										
										
										
											2008-02-08 15:50:41 +11:00
										 |  |  | 	int ret; | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * dar and dsisr get passed from the registers | 
					
						
							|  |  |  | 	 * to the spu_context, to this function, but not | 
					
						
							|  |  |  | 	 * back to the spu if it gets scheduled again. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * if we don't handle the fault for a saved context | 
					
						
							|  |  |  | 	 * in time, we can still expect to get the same fault | 
					
						
							|  |  |  | 	 * the immediately after the context restore. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2008-04-27 18:41:55 +00:00
										 |  |  | 	ea = ctx->csa.class_1_dar; | 
					
						
							|  |  |  | 	dsisr = ctx->csa.class_1_dsisr; | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED))) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-20 21:39:33 +02:00
										 |  |  | 	spuctx_switch_state(ctx, SPU_UTIL_IOWAIT); | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:03 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-06 14:27:38 +00:00
										 |  |  | 	pr_debug("ctx %p: ea %016llx, dsisr %016llx state %d\n", ctx, ea, | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 		dsisr, ctx->state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:03 +10:00
										 |  |  | 	ctx->stats.hash_flt++; | 
					
						
							| 
									
										
										
										
											2007-07-20 21:39:33 +02:00
										 |  |  | 	if (ctx->state == SPU_STATE_RUNNABLE) | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:07 +10:00
										 |  |  | 		ctx->spu->stats.hash_flt++; | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:03 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 	/* we must not hold the lock when entering spu_handle_mm_fault */ | 
					
						
							|  |  |  | 	spu_release(ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	access = (_PAGE_PRESENT | _PAGE_USER); | 
					
						
							|  |  |  | 	access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL; | 
					
						
							|  |  |  | 	local_irq_save(flags); | 
					
						
							|  |  |  | 	ret = hash_page(ea, access, 0x300); | 
					
						
							|  |  |  | 	local_irq_restore(flags); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* hashing failed, so try the actual fault handler */ | 
					
						
							|  |  |  | 	if (ret) | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:03 +10:00
										 |  |  | 		ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt); | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 	/*
 | 
					
						
							| 
									
										
										
										
											2008-02-08 15:50:41 +11:00
										 |  |  | 	 * This is nasty: we need the state_mutex for all the bookkeeping even | 
					
						
							|  |  |  | 	 * if the syscall was interrupted by a signal. ewww. | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2008-02-08 15:50:41 +11:00
										 |  |  | 	mutex_lock(&ctx->state_mutex); | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Clear dsisr under ctxt lock after handling the fault, so that | 
					
						
							|  |  |  | 	 * time slicing will not preempt the context while the page fault | 
					
						
							|  |  |  | 	 * handler is running. Context switch code removes mappings. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2008-04-27 18:41:55 +00:00
										 |  |  | 	ctx->csa.class_1_dar = ctx->csa.class_1_dsisr = 0; | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * If we handled the fault successfully and are in runnable | 
					
						
							|  |  |  | 	 * state, restart the DMA. | 
					
						
							|  |  |  | 	 * In case of unhandled error report the problem to user space. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	if (!ret) { | 
					
						
							| 
									
										
										
										
											2007-07-19 12:05:58 -07:00
										 |  |  | 		if (flt & VM_FAULT_MAJOR) | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:03 +10:00
										 |  |  | 			ctx->stats.maj_flt++; | 
					
						
							| 
									
										
										
										
											2007-07-19 12:05:58 -07:00
										 |  |  | 		else | 
					
						
							|  |  |  | 			ctx->stats.min_flt++; | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:07 +10:00
										 |  |  | 		if (ctx->state == SPU_STATE_RUNNABLE) { | 
					
						
							| 
									
										
										
										
											2007-07-19 12:05:58 -07:00
										 |  |  | 			if (flt & VM_FAULT_MAJOR) | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:07 +10:00
										 |  |  | 				ctx->spu->stats.maj_flt++; | 
					
						
							| 
									
										
										
										
											2007-07-19 12:05:58 -07:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				ctx->spu->stats.min_flt++; | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:07 +10:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2007-06-29 10:58:03 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 		if (ctx->spu) | 
					
						
							|  |  |  | 			ctx->ops->restart_dma(ctx); | 
					
						
							|  |  |  | 	} else | 
					
						
							| 
									
										
										
										
											2007-12-20 16:39:59 +09:00
										 |  |  | 		spufs_handle_event(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE); | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-20 21:39:33 +02:00
										 |  |  | 	spuctx_switch_state(ctx, SPU_UTIL_SYSTEM); | 
					
						
							| 
									
										
										
										
											2007-04-23 21:08:15 +02:00
										 |  |  | 	return ret; | 
					
						
							|  |  |  | } |