Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * SPU file system -- SPU context management |
| 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 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/mm.h> |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 25 | #include <linux/module.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 26 | #include <linux/slab.h> |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 27 | #include <asm/atomic.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 28 | #include <asm/spu.h> |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 29 | #include <asm/spu_csa.h> |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 30 | #include "spufs.h" |
| 31 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 32 | |
| 33 | atomic_t nr_spu_contexts = ATOMIC_INIT(0); |
| 34 | |
Arnd Bergmann | 6263203 | 2006-10-04 17:26:15 +0200 | [diff] [blame] | 35 | struct spu_context *alloc_spu_context(struct spu_gang *gang) |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 36 | { |
| 37 | struct spu_context *ctx; |
Jeremy Kerr | c5c4591 | 2006-04-28 16:37:47 +0800 | [diff] [blame] | 38 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 39 | if (!ctx) |
| 40 | goto out; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 41 | /* Binding to physical processor deferred |
| 42 | * until spu_activate(). |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 43 | */ |
Benjamin Herrenschmidt | f1fa74f | 2007-05-08 16:27:29 +1000 | [diff] [blame] | 44 | if (spu_init_csa(&ctx->csa)) |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 45 | goto out_free; |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 46 | spin_lock_init(&ctx->mmio_lock); |
Christoph Hellwig | 47d3a5f | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 47 | mutex_init(&ctx->mapping_lock); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 48 | kref_init(&ctx->kref); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 49 | mutex_init(&ctx->state_mutex); |
Christoph Hellwig | e45d48a | 2007-04-23 21:08:17 +0200 | [diff] [blame] | 50 | mutex_init(&ctx->run_mutex); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 51 | init_waitqueue_head(&ctx->ibox_wq); |
| 52 | init_waitqueue_head(&ctx->wbox_wq); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 53 | init_waitqueue_head(&ctx->stop_wq); |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 54 | init_waitqueue_head(&ctx->mfc_wq); |
Arnd Bergmann | 33bfd7a | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 55 | init_waitqueue_head(&ctx->run_wq); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 56 | ctx->state = SPU_STATE_SAVED; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 57 | ctx->ops = &spu_backing_ops; |
| 58 | ctx->owner = get_task_mm(current); |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 59 | INIT_LIST_HEAD(&ctx->rq); |
Arnd Bergmann | 8e68e2f | 2007-07-20 21:39:47 +0200 | [diff] [blame] | 60 | INIT_LIST_HEAD(&ctx->aff_list); |
Arnd Bergmann | 6263203 | 2006-10-04 17:26:15 +0200 | [diff] [blame] | 61 | if (gang) |
| 62 | spu_gang_add_ctx(gang, ctx); |
Christoph Hellwig | 9d78592 | 2007-07-25 21:31:09 +1000 | [diff] [blame] | 63 | |
| 64 | __spu_update_sched_info(ctx); |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame] | 65 | spu_set_timeslice(ctx); |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 66 | ctx->stats.util_state = SPU_UTIL_IDLE_LOADED; |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 67 | |
| 68 | atomic_inc(&nr_spu_contexts); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 69 | goto out; |
| 70 | out_free: |
| 71 | kfree(ctx); |
| 72 | ctx = NULL; |
| 73 | out: |
| 74 | return ctx; |
| 75 | } |
| 76 | |
| 77 | void destroy_spu_context(struct kref *kref) |
| 78 | { |
| 79 | struct spu_context *ctx; |
| 80 | ctx = container_of(kref, struct spu_context, kref); |
Julio M. Merino Vidal | 5345788 | 2008-04-30 15:12:30 +1000 | [diff] [blame] | 81 | spu_context_nospu_trace(destroy_spu_context__enter, ctx); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 82 | mutex_lock(&ctx->state_mutex); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 83 | spu_deactivate(ctx); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 84 | mutex_unlock(&ctx->state_mutex); |
Mark Nutter | 5473af0 | 2005-11-15 15:53:49 -0500 | [diff] [blame] | 85 | spu_fini_csa(&ctx->csa); |
Arnd Bergmann | 6263203 | 2006-10-04 17:26:15 +0200 | [diff] [blame] | 86 | if (ctx->gang) |
| 87 | spu_gang_remove_ctx(ctx->gang, ctx); |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 88 | if (ctx->prof_priv_kref) |
| 89 | kref_put(ctx->prof_priv_kref, ctx->prof_priv_release); |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 90 | BUG_ON(!list_empty(&ctx->rq)); |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 91 | atomic_dec(&nr_spu_contexts); |
Christoph Hellwig | 5158e9b | 2008-04-29 17:08:38 +1000 | [diff] [blame] | 92 | kfree(ctx->switch_log); |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 93 | kfree(ctx); |
| 94 | } |
| 95 | |
| 96 | struct spu_context * get_spu_context(struct spu_context *ctx) |
| 97 | { |
| 98 | kref_get(&ctx->kref); |
| 99 | return ctx; |
| 100 | } |
| 101 | |
| 102 | int put_spu_context(struct spu_context *ctx) |
| 103 | { |
| 104 | return kref_put(&ctx->kref, &destroy_spu_context); |
| 105 | } |
| 106 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 107 | /* give up the mm reference when the context is about to be destroyed */ |
| 108 | void spu_forget(struct spu_context *ctx) |
| 109 | { |
| 110 | struct mm_struct *mm; |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * This is basically an open-coded spu_acquire_saved, except that |
Jeremy Kerr | 0111a70 | 2008-02-27 19:08:13 +1100 | [diff] [blame] | 114 | * we don't acquire the state mutex interruptible, and we don't |
| 115 | * want this context to be rescheduled on release. |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 116 | */ |
| 117 | mutex_lock(&ctx->state_mutex); |
Jeremy Kerr | 0111a70 | 2008-02-27 19:08:13 +1100 | [diff] [blame] | 118 | if (ctx->state != SPU_STATE_SAVED) |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 119 | spu_deactivate(ctx); |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 120 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 121 | mm = ctx->owner; |
| 122 | ctx->owner = NULL; |
| 123 | mmput(mm); |
| 124 | spu_release(ctx); |
| 125 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 126 | |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 127 | void spu_unmap_mappings(struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 128 | { |
Christoph Hellwig | 47d3a5f | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 129 | mutex_lock(&ctx->mapping_lock); |
Mark Nutter | 6df10a8 | 2006-03-23 00:00:12 +0100 | [diff] [blame] | 130 | if (ctx->local_store) |
| 131 | unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1); |
| 132 | if (ctx->mfc) |
Jeremy Kerr | 87ff609 | 2008-07-01 10:22:50 +1000 | [diff] [blame] | 133 | unmap_mapping_range(ctx->mfc, 0, SPUFS_MFC_MAP_SIZE, 1); |
Mark Nutter | 6df10a8 | 2006-03-23 00:00:12 +0100 | [diff] [blame] | 134 | if (ctx->cntl) |
Jeremy Kerr | 87ff609 | 2008-07-01 10:22:50 +1000 | [diff] [blame] | 135 | unmap_mapping_range(ctx->cntl, 0, SPUFS_CNTL_MAP_SIZE, 1); |
Mark Nutter | 6df10a8 | 2006-03-23 00:00:12 +0100 | [diff] [blame] | 136 | if (ctx->signal1) |
Jeremy Kerr | 87ff609 | 2008-07-01 10:22:50 +1000 | [diff] [blame] | 137 | unmap_mapping_range(ctx->signal1, 0, SPUFS_SIGNAL_MAP_SIZE, 1); |
Mark Nutter | 6df10a8 | 2006-03-23 00:00:12 +0100 | [diff] [blame] | 138 | if (ctx->signal2) |
Jeremy Kerr | 87ff609 | 2008-07-01 10:22:50 +1000 | [diff] [blame] | 139 | unmap_mapping_range(ctx->signal2, 0, SPUFS_SIGNAL_MAP_SIZE, 1); |
Benjamin Herrenschmidt | 17e0e27 | 2007-02-13 11:46:08 +1100 | [diff] [blame] | 140 | if (ctx->mss) |
Jeremy Kerr | 87ff609 | 2008-07-01 10:22:50 +1000 | [diff] [blame] | 141 | unmap_mapping_range(ctx->mss, 0, SPUFS_MSS_MAP_SIZE, 1); |
Benjamin Herrenschmidt | 17e0e27 | 2007-02-13 11:46:08 +1100 | [diff] [blame] | 142 | if (ctx->psmap) |
Jeremy Kerr | 87ff609 | 2008-07-01 10:22:50 +1000 | [diff] [blame] | 143 | unmap_mapping_range(ctx->psmap, 0, SPUFS_PS_MAP_SIZE, 1); |
Christoph Hellwig | 47d3a5f | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 144 | mutex_unlock(&ctx->mapping_lock); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Christoph Hellwig | 6a0641e5 | 2007-02-13 21:54:21 +0100 | [diff] [blame] | 147 | /** |
Christoph Hellwig | 6a0641e5 | 2007-02-13 21:54:21 +0100 | [diff] [blame] | 148 | * spu_acquire_saved - lock spu contex and make sure it is in saved state |
| 149 | * @ctx: spu contex to lock |
| 150 | */ |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 151 | int spu_acquire_saved(struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 152 | { |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 153 | int ret; |
| 154 | |
Julio M. Merino Vidal | 3734dfc | 2008-04-30 15:21:17 +1000 | [diff] [blame] | 155 | spu_context_nospu_trace(spu_acquire_saved__enter, ctx); |
| 156 | |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 157 | ret = spu_acquire(ctx); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | |
Christoph Hellwig | 27b1ea0 | 2007-07-20 21:39:34 +0200 | [diff] [blame] | 161 | if (ctx->state != SPU_STATE_SAVED) { |
| 162 | set_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 163 | spu_deactivate(ctx); |
Christoph Hellwig | 27b1ea0 | 2007-07-20 21:39:34 +0200 | [diff] [blame] | 164 | } |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 165 | |
| 166 | return 0; |
Christoph Hellwig | 27b1ea0 | 2007-07-20 21:39:34 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * spu_release_saved - unlock spu context and return it to the runqueue |
| 171 | * @ctx: context to unlock |
| 172 | */ |
| 173 | void spu_release_saved(struct spu_context *ctx) |
| 174 | { |
| 175 | BUG_ON(ctx->state != SPU_STATE_SAVED); |
| 176 | |
Jeremy Kerr | c368392 | 2008-03-11 12:46:18 +1100 | [diff] [blame] | 177 | if (test_and_clear_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags) && |
| 178 | test_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags)) |
Christoph Hellwig | 27b1ea0 | 2007-07-20 21:39:34 +0200 | [diff] [blame] | 179 | spu_activate(ctx, 0); |
| 180 | |
| 181 | spu_release(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 182 | } |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 183 | |