4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
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)
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.
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.
25 #include <linux/kref.h>
26 #include <linux/mutex.h>
27 #include <linux/spinlock.h>
29 #include <linux/cpumask.h>
32 #include <asm/spu_csa.h>
33 #include <asm/spu_info.h>
35 /* The magic number for our file system */
37 SPUFS_MAGIC = 0x23c9b64e,
40 struct spu_context_ops;
44 * This is the state for spu utilization reporting to userspace.
45 * Because this state is visible to userspace it must never change and needs
46 * to be kept strictly separate from any internal state kept by the kernel.
48 enum spuctx_execution_state {
57 struct spu *spu; /* pointer to a physical SPU */
58 struct spu_state csa; /* SPU context save area. */
59 spinlock_t mmio_lock; /* protects mmio access */
60 struct address_space *local_store; /* local store mapping. */
61 struct address_space *mfc; /* 'mfc' area mappings. */
62 struct address_space *cntl; /* 'control' area mappings. */
63 struct address_space *signal1; /* 'signal1' area mappings. */
64 struct address_space *signal2; /* 'signal2' area mappings. */
65 struct address_space *mss; /* 'mss' area mappings. */
66 struct address_space *psmap; /* 'psmap' area mappings. */
67 struct mutex mapping_lock;
68 u64 object_id; /* user space pointer for oprofile */
70 enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
71 struct mutex state_mutex;
72 struct mutex run_mutex;
74 struct mm_struct *owner;
77 wait_queue_head_t ibox_wq;
78 wait_queue_head_t wbox_wq;
79 wait_queue_head_t stop_wq;
80 wait_queue_head_t mfc_wq;
81 struct fasync_struct *ibox_fasync;
82 struct fasync_struct *wbox_fasync;
83 struct fasync_struct *mfc_fasync;
85 struct spu_context_ops *ops;
86 struct work_struct reap_work;
88 unsigned long event_return;
90 struct list_head gang_list;
91 struct spu_gang *gang;
96 /* scheduler fields */
98 unsigned int time_slice;
99 unsigned long sched_flags;
100 cpumask_t cpus_allowed;
106 /* updates protected by ctx->state_mutex */
107 enum spuctx_execution_state execution_state;
108 unsigned long tstamp; /* time of last ctx switch */
109 unsigned long times[SPUCTX_UTIL_MAX];
110 unsigned long long vol_ctx_switch;
111 unsigned long long invol_ctx_switch;
112 unsigned long long min_flt;
113 unsigned long long maj_flt;
114 unsigned long long hash_flt;
115 unsigned long long slb_flt;
116 unsigned long long slb_flt_base; /* # at last ctx switch */
117 unsigned long long class2_intr;
118 unsigned long long class2_intr_base; /* # at last ctx switch */
119 unsigned long long libassist;
124 struct list_head list;
130 struct mfc_dma_command {
131 int32_t pad; /* reserved */
132 uint32_t lsa; /* local storage address */
133 uint64_t ea; /* effective address */
134 uint16_t size; /* transfer size */
135 uint16_t tag; /* command tag */
136 uint16_t class; /* class ID */
137 uint16_t cmd; /* command opcode */
141 /* SPU context query/set operations. */
142 struct spu_context_ops {
143 int (*mbox_read) (struct spu_context * ctx, u32 * data);
144 u32(*mbox_stat_read) (struct spu_context * ctx);
145 unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
146 unsigned int events);
147 int (*ibox_read) (struct spu_context * ctx, u32 * data);
148 int (*wbox_write) (struct spu_context * ctx, u32 data);
149 u32(*signal1_read) (struct spu_context * ctx);
150 void (*signal1_write) (struct spu_context * ctx, u32 data);
151 u32(*signal2_read) (struct spu_context * ctx);
152 void (*signal2_write) (struct spu_context * ctx, u32 data);
153 void (*signal1_type_set) (struct spu_context * ctx, u64 val);
154 u64(*signal1_type_get) (struct spu_context * ctx);
155 void (*signal2_type_set) (struct spu_context * ctx, u64 val);
156 u64(*signal2_type_get) (struct spu_context * ctx);
157 u32(*npc_read) (struct spu_context * ctx);
158 void (*npc_write) (struct spu_context * ctx, u32 data);
159 u32(*status_read) (struct spu_context * ctx);
160 char*(*get_ls) (struct spu_context * ctx);
161 u32 (*runcntl_read) (struct spu_context * ctx);
162 void (*runcntl_write) (struct spu_context * ctx, u32 data);
163 void (*master_start) (struct spu_context * ctx);
164 void (*master_stop) (struct spu_context * ctx);
165 int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
166 u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
167 u32 (*get_mfc_free_elements)(struct spu_context *ctx);
168 int (*send_mfc_command)(struct spu_context * ctx,
169 struct mfc_dma_command * cmd);
170 void (*dma_info_read) (struct spu_context * ctx,
171 struct spu_dma_info * info);
172 void (*proxydma_info_read) (struct spu_context * ctx,
173 struct spu_proxydma_info * info);
174 void (*restart_dma)(struct spu_context *ctx);
177 extern struct spu_context_ops spu_hw_ops;
178 extern struct spu_context_ops spu_backing_ops;
180 struct spufs_inode_info {
181 struct spu_context *i_ctx;
182 struct spu_gang *i_gang;
183 struct inode vfs_inode;
186 #define SPUFS_I(inode) \
187 container_of(inode, struct spufs_inode_info, vfs_inode)
189 extern struct tree_descr spufs_dir_contents[];
190 extern struct tree_descr spufs_dir_nosched_contents[];
192 /* system call implementation */
193 long spufs_run_spu(struct file *file,
194 struct spu_context *ctx, u32 *npc, u32 *status);
195 long spufs_create(struct nameidata *nd,
196 unsigned int flags, mode_t mode);
197 extern const struct file_operations spufs_context_fops;
199 /* gang management */
200 struct spu_gang *alloc_spu_gang(void);
201 struct spu_gang *get_spu_gang(struct spu_gang *gang);
202 int put_spu_gang(struct spu_gang *gang);
203 void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
204 void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
207 int spufs_handle_class1(struct spu_context *ctx);
209 /* context management */
210 extern atomic_t nr_spu_contexts;
211 static inline void spu_acquire(struct spu_context *ctx)
213 mutex_lock(&ctx->state_mutex);
216 static inline void spu_release(struct spu_context *ctx)
218 mutex_unlock(&ctx->state_mutex);
221 struct spu_context * alloc_spu_context(struct spu_gang *gang);
222 void destroy_spu_context(struct kref *kref);
223 struct spu_context * get_spu_context(struct spu_context *ctx);
224 int put_spu_context(struct spu_context *ctx);
225 void spu_unmap_mappings(struct spu_context *ctx);
227 void spu_forget(struct spu_context *ctx);
228 int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags);
229 void spu_acquire_saved(struct spu_context *ctx);
231 int spu_activate(struct spu_context *ctx, unsigned long flags);
232 void spu_deactivate(struct spu_context *ctx);
233 void spu_yield(struct spu_context *ctx);
234 void spu_set_timeslice(struct spu_context *ctx);
235 void spu_update_sched_info(struct spu_context *ctx);
236 void __spu_update_sched_info(struct spu_context *ctx);
237 int __init spu_sched_init(void);
238 void __exit spu_sched_exit(void);
240 extern char *isolated_loader;
244 * Same as wait_event_interruptible(), except that here
245 * we need to call spu_release(ctx) before sleeping, and
246 * then spu_acquire(ctx) when awoken.
249 #define spufs_wait(wq, condition) \
252 DEFINE_WAIT(__wait); \
254 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
257 if (signal_pending(current)) { \
258 __ret = -ERESTARTSYS; \
265 finish_wait(&(wq), &__wait); \
269 size_t spu_wbox_write(struct spu_context *ctx, u32 data);
270 size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
272 /* irq callback funcs. */
273 void spufs_ibox_callback(struct spu *spu);
274 void spufs_wbox_callback(struct spu *spu);
275 void spufs_stop_callback(struct spu *spu);
276 void spufs_mfc_callback(struct spu *spu);
277 void spufs_dma_callback(struct spu *spu, int type);
279 extern struct spu_coredump_calls spufs_coredump_calls;
280 struct spufs_coredump_reader {
282 ssize_t (*read)(struct spu_context *ctx,
283 char __user *buffer, size_t size, loff_t *pos);
284 u64 (*get)(void *data);
287 extern struct spufs_coredump_reader spufs_coredump_read[];
288 extern int spufs_coredump_num_notes;
291 * This function is a little bit too large for an inline, but
292 * as fault.c is built into the kernel we can't move it out of
295 static inline void spuctx_switch_state(struct spu_context *ctx,
296 enum spuctx_execution_state new_state)
298 WARN_ON(!mutex_is_locked(&ctx->state_mutex));
300 if (ctx->stats.execution_state != new_state) {
301 unsigned long curtime = jiffies;
303 ctx->stats.times[ctx->stats.execution_state] +=
304 curtime - ctx->stats.tstamp;
305 ctx->stats.tstamp = curtime;
306 ctx->stats.execution_state = new_state;