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;
43 /* ctx->sched_flags */
45 SPU_SCHED_NOTIFY_ACTIVE,
46 SPU_SCHED_WAS_ACTIVE, /* was active upon spu_acquire_saved() */
47 SPU_SCHED_SPU_RUN, /* context is within spu_run */
51 SWITCH_LOG_BUFSIZE = 4096,
62 wait_queue_head_t wait;
65 struct switch_log_entry {
66 struct timespec tstamp;
75 struct spu *spu; /* pointer to a physical SPU */
76 struct spu_state csa; /* SPU context save area. */
77 spinlock_t mmio_lock; /* protects mmio access */
78 struct address_space *local_store; /* local store mapping. */
79 struct address_space *mfc; /* 'mfc' area mappings. */
80 struct address_space *cntl; /* 'control' area mappings. */
81 struct address_space *signal1; /* 'signal1' area mappings. */
82 struct address_space *signal2; /* 'signal2' area mappings. */
83 struct address_space *mss; /* 'mss' area mappings. */
84 struct address_space *psmap; /* 'psmap' area mappings. */
85 struct mutex mapping_lock;
86 u64 object_id; /* user space pointer for oprofile */
88 enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
89 struct mutex state_mutex;
90 struct mutex run_mutex;
92 struct mm_struct *owner;
95 wait_queue_head_t ibox_wq;
96 wait_queue_head_t wbox_wq;
97 wait_queue_head_t stop_wq;
98 wait_queue_head_t mfc_wq;
99 wait_queue_head_t run_wq;
100 struct fasync_struct *ibox_fasync;
101 struct fasync_struct *wbox_fasync;
102 struct fasync_struct *mfc_fasync;
104 struct spu_context_ops *ops;
105 struct work_struct reap_work;
107 unsigned long event_return;
109 struct list_head gang_list;
110 struct spu_gang *gang;
111 struct kref *prof_priv_kref;
112 void ( * prof_priv_release) (struct kref *kref);
117 /* scheduler fields */
119 unsigned int time_slice;
120 unsigned long sched_flags;
121 cpumask_t cpus_allowed;
128 /* updates protected by ctx->state_mutex */
129 enum spu_utilization_state util_state;
130 unsigned long long tstamp; /* time of last state switch */
131 unsigned long long times[SPU_UTIL_MAX];
132 unsigned long long vol_ctx_switch;
133 unsigned long long invol_ctx_switch;
134 unsigned long long min_flt;
135 unsigned long long maj_flt;
136 unsigned long long hash_flt;
137 unsigned long long slb_flt;
138 unsigned long long slb_flt_base; /* # at last ctx switch */
139 unsigned long long class2_intr;
140 unsigned long long class2_intr_base; /* # at last ctx switch */
141 unsigned long long libassist;
144 /* context switch log */
145 struct switch_log *switch_log;
147 struct list_head aff_list;
153 struct list_head list;
158 struct spu_context *aff_ref_ctx;
159 struct list_head aff_list_head;
160 struct mutex aff_mutex;
162 struct spu *aff_ref_spu;
163 atomic_t aff_sched_count;
166 /* Flag bits for spu_gang aff_flags */
167 #define AFF_OFFSETS_SET 1
170 struct mfc_dma_command {
171 int32_t pad; /* reserved */
172 uint32_t lsa; /* local storage address */
173 uint64_t ea; /* effective address */
174 uint16_t size; /* transfer size */
175 uint16_t tag; /* command tag */
176 uint16_t class; /* class ID */
177 uint16_t cmd; /* command opcode */
181 /* SPU context query/set operations. */
182 struct spu_context_ops {
183 int (*mbox_read) (struct spu_context * ctx, u32 * data);
184 u32(*mbox_stat_read) (struct spu_context * ctx);
185 unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
186 unsigned int events);
187 int (*ibox_read) (struct spu_context * ctx, u32 * data);
188 int (*wbox_write) (struct spu_context * ctx, u32 data);
189 u32(*signal1_read) (struct spu_context * ctx);
190 void (*signal1_write) (struct spu_context * ctx, u32 data);
191 u32(*signal2_read) (struct spu_context * ctx);
192 void (*signal2_write) (struct spu_context * ctx, u32 data);
193 void (*signal1_type_set) (struct spu_context * ctx, u64 val);
194 u64(*signal1_type_get) (struct spu_context * ctx);
195 void (*signal2_type_set) (struct spu_context * ctx, u64 val);
196 u64(*signal2_type_get) (struct spu_context * ctx);
197 u32(*npc_read) (struct spu_context * ctx);
198 void (*npc_write) (struct spu_context * ctx, u32 data);
199 u32(*status_read) (struct spu_context * ctx);
200 char*(*get_ls) (struct spu_context * ctx);
201 void (*privcntl_write) (struct spu_context *ctx, u64 data);
202 u32 (*runcntl_read) (struct spu_context * ctx);
203 void (*runcntl_write) (struct spu_context * ctx, u32 data);
204 void (*runcntl_stop) (struct spu_context * ctx);
205 void (*master_start) (struct spu_context * ctx);
206 void (*master_stop) (struct spu_context * ctx);
207 int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
208 u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
209 u32 (*get_mfc_free_elements)(struct spu_context *ctx);
210 int (*send_mfc_command)(struct spu_context * ctx,
211 struct mfc_dma_command * cmd);
212 void (*dma_info_read) (struct spu_context * ctx,
213 struct spu_dma_info * info);
214 void (*proxydma_info_read) (struct spu_context * ctx,
215 struct spu_proxydma_info * info);
216 void (*restart_dma)(struct spu_context *ctx);
219 extern struct spu_context_ops spu_hw_ops;
220 extern struct spu_context_ops spu_backing_ops;
222 struct spufs_inode_info {
223 struct spu_context *i_ctx;
224 struct spu_gang *i_gang;
225 struct inode vfs_inode;
228 #define SPUFS_I(inode) \
229 container_of(inode, struct spufs_inode_info, vfs_inode)
231 extern struct tree_descr spufs_dir_contents[];
232 extern struct tree_descr spufs_dir_nosched_contents[];
234 /* system call implementation */
235 extern struct spufs_calls spufs_calls;
236 long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status);
237 long spufs_create(struct nameidata *nd, unsigned int flags,
238 mode_t mode, struct file *filp);
239 /* ELF coredump callbacks for writing SPU ELF notes */
240 extern int spufs_coredump_extra_notes_size(void);
241 extern int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset);
243 extern const struct file_operations spufs_context_fops;
245 /* gang management */
246 struct spu_gang *alloc_spu_gang(void);
247 struct spu_gang *get_spu_gang(struct spu_gang *gang);
248 int put_spu_gang(struct spu_gang *gang);
249 void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
250 void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
253 int spufs_handle_class1(struct spu_context *ctx);
254 int spufs_handle_class0(struct spu_context *ctx);
257 struct spu *affinity_check(struct spu_context *ctx);
259 /* context management */
260 extern atomic_t nr_spu_contexts;
261 static inline int __must_check spu_acquire(struct spu_context *ctx)
263 return mutex_lock_interruptible(&ctx->state_mutex);
266 static inline void spu_release(struct spu_context *ctx)
268 mutex_unlock(&ctx->state_mutex);
271 struct spu_context * alloc_spu_context(struct spu_gang *gang);
272 void destroy_spu_context(struct kref *kref);
273 struct spu_context * get_spu_context(struct spu_context *ctx);
274 int put_spu_context(struct spu_context *ctx);
275 void spu_unmap_mappings(struct spu_context *ctx);
277 void spu_forget(struct spu_context *ctx);
278 int __must_check spu_acquire_saved(struct spu_context *ctx);
279 void spu_release_saved(struct spu_context *ctx);
281 int spu_stopped(struct spu_context *ctx, u32 * stat);
282 void spu_del_from_rq(struct spu_context *ctx);
283 int spu_activate(struct spu_context *ctx, unsigned long flags);
284 void spu_deactivate(struct spu_context *ctx);
285 void spu_yield(struct spu_context *ctx);
286 void spu_switch_notify(struct spu *spu, struct spu_context *ctx);
287 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
289 void spu_set_timeslice(struct spu_context *ctx);
290 void spu_update_sched_info(struct spu_context *ctx);
291 void __spu_update_sched_info(struct spu_context *ctx);
292 int __init spu_sched_init(void);
293 void spu_sched_exit(void);
295 extern char *isolated_loader;
299 * Same as wait_event_interruptible(), except that here
300 * we need to call spu_release(ctx) before sleeping, and
301 * then spu_acquire(ctx) when awoken.
303 * Returns with state_mutex re-acquired when successfull or
304 * with -ERESTARTSYS and the state_mutex dropped when interrupted.
307 #define spufs_wait(wq, condition) \
310 DEFINE_WAIT(__wait); \
312 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
316 if (signal_pending(current)) { \
317 __ret = -ERESTARTSYS; \
321 __ret = spu_acquire(ctx); \
325 finish_wait(&(wq), &__wait); \
329 size_t spu_wbox_write(struct spu_context *ctx, u32 data);
330 size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
332 /* irq callback funcs. */
333 void spufs_ibox_callback(struct spu *spu);
334 void spufs_wbox_callback(struct spu *spu);
335 void spufs_stop_callback(struct spu *spu, int irq);
336 void spufs_mfc_callback(struct spu *spu);
337 void spufs_dma_callback(struct spu *spu, int type);
339 extern struct spu_coredump_calls spufs_coredump_calls;
340 struct spufs_coredump_reader {
342 ssize_t (*read)(struct spu_context *ctx,
343 char __user *buffer, size_t size, loff_t *pos);
344 u64 (*get)(struct spu_context *ctx);
347 extern struct spufs_coredump_reader spufs_coredump_read[];
348 extern int spufs_coredump_num_notes;
350 extern int spu_init_csa(struct spu_state *csa);
351 extern void spu_fini_csa(struct spu_state *csa);
352 extern int spu_save(struct spu_state *prev, struct spu *spu);
353 extern int spu_restore(struct spu_state *new, struct spu *spu);
354 extern int spu_switch(struct spu_state *prev, struct spu_state *new,
356 extern int spu_alloc_lscsa(struct spu_state *csa);
357 extern void spu_free_lscsa(struct spu_state *csa);
359 extern void spuctx_switch_state(struct spu_context *ctx,
360 enum spu_utilization_state new_state);
362 #define spu_context_trace(name, ctx, spu) \
363 trace_mark(name, "ctx %p spu %p", ctx, spu);
364 #define spu_context_nospu_trace(name, ctx) \
365 trace_mark(name, "ctx %p", ctx);