Skip to content

Commit

Permalink
[PATCH] spufs: switchable spu contexts
Browse files Browse the repository at this point in the history
Add some infrastructure for saving and restoring the context of an
SPE. This patch creates a new structure that can hold the whole
state of a physical SPE in memory. It also contains code that
avoids races during the context switch and the binary code that
is loaded to the SPU in order to access its registers.

The actual PPE- and SPE-side context switch code are two separate
patches.

Signed-off-by: Arnd Bergmann <arndb@de.ibm.com>
Signed-off-by: Paul Mackerras <paulus@samba.org>
  • Loading branch information
Mark Nutter authored and Paul Mackerras committed Jan 9, 2006
1 parent 67207b9 commit 5473af0
Show file tree
Hide file tree
Showing 9 changed files with 975 additions and 4 deletions.
27 changes: 24 additions & 3 deletions arch/powerpc/platforms/cell/spu_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ static int __spu_trap_error(struct spu *spu)
static void spu_restart_dma(struct spu *spu)
{
struct spu_priv2 __iomem *priv2 = spu->priv2;
out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);

if (!test_bit(SPU_CONTEXT_SWITCH_PENDING_nr, &spu->flags))
out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
}

static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
Expand All @@ -72,6 +74,11 @@ static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)

pr_debug("%s\n", __FUNCTION__);

if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags)) {
printk("%s: invalid access during switch!\n", __func__);
return 1;
}

if (REGION_ID(ea) != USER_REGION_ID) {
pr_debug("invalid region access at %016lx\n", ea);
return 1;
Expand All @@ -98,6 +105,7 @@ static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
return 0;
}

extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
static int __spu_trap_data_map(struct spu *spu, unsigned long ea)
{
unsigned long dsisr;
Expand All @@ -107,8 +115,21 @@ static int __spu_trap_data_map(struct spu *spu, unsigned long ea)
priv1 = spu->priv1;
dsisr = in_be64(&priv1->mfc_dsisr_RW);

wake_up(&spu->stop_wq);
/* Handle kernel space hash faults immediately.
User hash faults need to be deferred to process context. */
if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
&& REGION_ID(ea) != USER_REGION_ID
&& hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
spu_restart_dma(spu);
return 0;
}

if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags)) {
printk("%s: invalid access during switch!\n", __func__);
return 1;
}

wake_up(&spu->stop_wq);
return 0;
}

Expand Down Expand Up @@ -382,7 +403,6 @@ void spu_free(struct spu *spu)
}
EXPORT_SYMBOL(spu_free);

extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
static int spu_handle_mm_fault(struct spu *spu)
{
struct spu_priv1 __iomem *priv1;
Expand Down Expand Up @@ -650,6 +670,7 @@ static int __init create_spu(struct device_node *spe)
spu->slb_replace = 0;
spu->mm = NULL;
spu->class_0_pending = 0;
spu->flags = 0UL;
spin_lock_init(&spu->register_lock);

out_be64(&spu->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
Expand Down
4 changes: 3 additions & 1 deletion arch/powerpc/platforms/cell/spufs/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
obj-$(CONFIG_SPU_FS) += spufs.o

spufs-y += inode.o file.o context.o syscalls.o
spufs-y += inode.o file.o context.o switch.o syscalls.o

$(obj)/switch.o: $(obj)/spu_save_dump.h $(obj)/spu_restore_dump.h
18 changes: 18 additions & 0 deletions arch/powerpc/platforms/cell/spufs/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <linux/slab.h>
#include <asm/spu.h>
#include <asm/spu_csa.h>
#include "spufs.h"

struct spu_context *alloc_spu_context(void)
Expand All @@ -30,9 +31,25 @@ struct spu_context *alloc_spu_context(void)
ctx = kmalloc(sizeof *ctx, GFP_KERNEL);
if (!ctx)
goto out;
/* Future enhancement: do not call spu_alloc()
* here. This step should be deferred until
* spu_run()!!
*
* More work needs to be done to read(),
* write(), mmap(), etc., so that operations
* are performed on CSA when the context is
* not currently being run. In this way we
* can support arbitrarily large number of
* entries in /spu, allow state queries, etc.
*/
ctx->spu = spu_alloc();
if (!ctx->spu)
goto out_free;
spu_init_csa(&ctx->csa);
if (!ctx->csa.lscsa) {
spu_free(ctx->spu);
goto out_free;
}
init_rwsem(&ctx->backing_sema);
spin_lock_init(&ctx->mmio_lock);
kref_init(&ctx->kref);
Expand All @@ -50,6 +67,7 @@ void destroy_spu_context(struct kref *kref)
ctx = container_of(kref, struct spu_context, kref);
if (ctx->spu)
spu_free(ctx->spu);
spu_fini_csa(&ctx->csa);
kfree(ctx);
}

Expand Down
231 changes: 231 additions & 0 deletions arch/powerpc/platforms/cell/spufs/spu_restore_dump.h_shipped
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/*
* spu_restore_dump.h: Copyright (C) 2005 IBM.
* Hex-dump auto generated from spu_restore.c.
* Do not edit!
*/
static unsigned int spu_restore_code[] __page_aligned = {
0x40800000, 0x409ff801, 0x24000080, 0x24fd8081,
0x1cd80081, 0x33001180, 0x42030003, 0x33800284,
0x1c010204, 0x40200000, 0x40200000, 0x40200000,
0x34000190, 0x34004191, 0x34008192, 0x3400c193,
0x141fc205, 0x23fffd84, 0x1c100183, 0x217ffa85,
0x3080a000, 0x3080a201, 0x3080a402, 0x3080a603,
0x3080a804, 0x3080aa05, 0x3080ac06, 0x3080ae07,
0x3080b008, 0x3080b209, 0x3080b40a, 0x3080b60b,
0x3080b80c, 0x3080ba0d, 0x3080bc0e, 0x3080be0f,
0x00003ffc, 0x00000000, 0x00000000, 0x00000000,
0x01a00182, 0x3ec00083, 0xb0a14103, 0x01a00204,
0x3ec10082, 0x4202800e, 0x04000703, 0xb0a14202,
0x21a00803, 0x3fbf028d, 0x3f20068d, 0x3fbe0682,
0x3fe30102, 0x21a00882, 0x3f82028f, 0x3fe3078f,
0x3fbf0784, 0x3f200204, 0x3fbe0204, 0x3fe30204,
0x04000203, 0x21a00903, 0x40848002, 0x21a00982,
0x40800003, 0x21a00a03, 0x40802002, 0x21a00a82,
0x21a00083, 0x40800082, 0x21a00b02, 0x10002818,
0x40a80002, 0x32800007, 0x4207000c, 0x18008208,
0x40a0000b, 0x4080020a, 0x40800709, 0x00200000,
0x42070002, 0x3ac30384, 0x1cffc489, 0x00200000,
0x18008383, 0x38830382, 0x4cffc486, 0x3ac28185,
0xb0408584, 0x28830382, 0x1c020387, 0x38828182,
0xb0408405, 0x1802c408, 0x28828182, 0x217ff886,
0x04000583, 0x21a00803, 0x3fbe0682, 0x3fe30102,
0x04000106, 0x21a00886, 0x04000603, 0x21a00903,
0x40803c02, 0x21a00982, 0x40800003, 0x04000184,
0x21a00a04, 0x40802202, 0x21a00a82, 0x42028005,
0x34208702, 0x21002282, 0x21a00804, 0x21a00886,
0x3fbf0782, 0x3f200102, 0x3fbe0102, 0x3fe30102,
0x21a00902, 0x40804003, 0x21a00983, 0x21a00a04,
0x40805a02, 0x21a00a82, 0x40800083, 0x21a00b83,
0x01a00c02, 0x01a00d83, 0x3420c282, 0x21a00e02,
0x34210283, 0x21a00f03, 0x34200284, 0x77400200,
0x3421c282, 0x21a00702, 0x34218283, 0x21a00083,
0x34214282, 0x21a00b02, 0x4200480c, 0x00200000,
0x1c010286, 0x34220284, 0x34220302, 0x0f608203,
0x5c024204, 0x3b81810b, 0x42013c02, 0x00200000,
0x18008185, 0x38808183, 0x3b814182, 0x21004e84,
0x4020007f, 0x35000100, 0x000004e0, 0x000002a0,
0x000002e8, 0x00000428, 0x00000360, 0x000002e8,
0x000004a0, 0x00000468, 0x000003c8, 0x00000360,
0x409ffe02, 0x30801203, 0x40800204, 0x3ec40085,
0x10009c09, 0x3ac10606, 0xb060c105, 0x4020007f,
0x4020007f, 0x20801203, 0x38810602, 0xb0408586,
0x28810602, 0x32004180, 0x34204702, 0x21a00382,
0x4020007f, 0x327fdc80, 0x409ffe02, 0x30801203,
0x40800204, 0x3ec40087, 0x40800405, 0x00200000,
0x40800606, 0x3ac10608, 0x3ac14609, 0x3ac1860a,
0xb060c107, 0x20801203, 0x41004003, 0x38810602,
0x4020007f, 0xb0408188, 0x4020007f, 0x28810602,
0x41201002, 0x38814603, 0x10009c09, 0xb060c109,
0x4020007f, 0x28814603, 0x41193f83, 0x38818602,
0x60ffc003, 0xb040818a, 0x28818602, 0x32003080,
0x409ffe02, 0x30801203, 0x40800204, 0x3ec40087,
0x41201008, 0x10009c14, 0x40800405, 0x3ac10609,
0x40800606, 0x3ac1460a, 0xb060c107, 0x3ac1860b,
0x20801203, 0x38810602, 0xb0408409, 0x28810602,
0x38814603, 0xb060c40a, 0x4020007f, 0x28814603,
0x41193f83, 0x38818602, 0x60ffc003, 0xb040818b,
0x28818602, 0x32002380, 0x409ffe02, 0x30801204,
0x40800205, 0x3ec40083, 0x40800406, 0x3ac14607,
0x3ac18608, 0xb0810103, 0x41004002, 0x20801204,
0x4020007f, 0x38814603, 0x10009c0b, 0xb060c107,
0x4020007f, 0x4020007f, 0x28814603, 0x38818602,
0x4020007f, 0x4020007f, 0xb0408588, 0x28818602,
0x4020007f, 0x32001780, 0x409ffe02, 0x1000640e,
0x40800204, 0x30801203, 0x40800405, 0x3ec40087,
0x40800606, 0x3ac10608, 0x3ac14609, 0x3ac1860a,
0xb060c107, 0x20801203, 0x413d8003, 0x38810602,
0x4020007f, 0x327fd780, 0x409ffe02, 0x10007f0c,
0x40800205, 0x30801204, 0x40800406, 0x3ec40083,
0x3ac14607, 0x3ac18608, 0xb0810103, 0x413d8002,
0x20801204, 0x38814603, 0x4020007f, 0x327feb80,
0x409ffe02, 0x30801203, 0x40800204, 0x3ec40087,
0x40800405, 0x1000650a, 0x40800606, 0x3ac10608,
0x3ac14609, 0x3ac1860a, 0xb060c107, 0x20801203,
0x38810602, 0xb0408588, 0x4020007f, 0x327fc980,
0x00400000, 0x40800003, 0x4020007f, 0x35000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
};
Loading

0 comments on commit 5473af0

Please sign in to comment.