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;
127 /* updates protected by ctx->state_mutex */
128 enum spu_utilization_state util_state;
129 unsigned long long tstamp; /* time of last state switch */
130 unsigned long long times[SPU_UTIL_MAX];
131 unsigned long long vol_ctx_switch;
132 unsigned long long invol_ctx_switch;
133 unsigned long long min_flt;
134 unsigned long long maj_flt;
135 unsigned long long hash_flt;
136 unsigned long long slb_flt;
137 unsigned long long slb_flt_base; /* # at last ctx switch */
138 unsigned long long class2_intr;
139 unsigned long long class2_intr_base; /* # at last ctx switch */
140 unsigned long long libassist;
143 /* context switch log */
144 struct switch_log *switch_log;
146 struct list_head aff_list;
152 struct list_head list;
157 struct spu_context *aff_ref_ctx;
158 struct list_head aff_list_head;
159 struct mutex aff_mutex;
161 struct spu *aff_ref_spu;
162 atomic_t aff_sched_count;
165 /* Flag bits for spu_gang aff_flags */
166 #define AFF_OFFSETS_SET 1
169 struct mfc_dma_command {
170 int32_t pad; /* reserved */
171 uint32_t lsa; /* local storage address */
172 uint64_t ea; /* effective address */
173 uint16_t size; /* transfer size */
174 uint16_t tag; /* command tag */
175 uint16_t class; /* class ID */
176 uint16_t cmd; /* command opcode */
180 /* SPU context query/set operations. */
181 struct spu_context_ops {
182 int (*mbox_read) (struct spu_context * ctx, u32 * data);
183 u32(*mbox_stat_read) (struct spu_context * ctx);
184 unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
185 unsigned int events);
186 int (*ibox_read) (struct spu_context * ctx, u32 * data);
187 int (*wbox_write) (struct spu_context * ctx, u32 data);
188 u32(*signal1_read) (struct spu_context * ctx);
189 void (*signal1_write) (struct spu_context * ctx, u32 data);
190 u32(*signal2_read) (struct spu_context * ctx);
191 void (*signal2_write) (struct spu_context * ctx, u32 data);
192 void (*signal1_type_set) (struct spu_context * ctx, u64 val);
193 u64(*signal1_type_get) (struct spu_context * ctx);
194 void (*signal2_type_set) (struct spu_context * ctx, u64 val);
195 u64(*signal2_type_get) (struct spu_context * ctx);
196 u32(*npc_read) (struct spu_context * ctx);
197 void (*npc_write) (struct spu_context * ctx, u32 data);
198 u32(*status_read) (struct spu_context * ctx);
199 char*(*get_ls) (struct spu_context * ctx);
200 void (*privcntl_write) (struct spu_context *ctx, u64 data);
201 u32 (*runcntl_read) (struct spu_context * ctx);
202 void (*runcntl_write) (struct spu_context * ctx, u32 data);
203 void (*runcntl_stop) (struct spu_context * ctx);
204 void (*master_start) (struct spu_context * ctx);
205 void (*master_stop) (struct spu_context * ctx);
206 int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
207 u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
208 u32 (*get_mfc_free_elements)(struct spu_context *ctx);
209 int (*send_mfc_command)(struct spu_context * ctx,
210 struct mfc_dma_command * cmd);
211 void (*dma_info_read) (struct spu_context * ctx,
212 struct spu_dma_info * info);
213 void (*proxydma_info_read) (struct spu_context * ctx,
214 struct spu_proxydma_info * info);
215 void (*restart_dma)(struct spu_context *ctx);
218 extern struct spu_context_ops spu_hw_ops;
219 extern struct spu_context_ops spu_backing_ops;
221 struct spufs_inode_info {
222 struct spu_context *i_ctx;
223 struct spu_gang *i_gang;
224 struct inode vfs_inode;
227 #define SPUFS_I(inode) \
228 container_of(inode, struct spufs_inode_info, vfs_inode)
230 extern struct tree_descr spufs_dir_contents[];
231 extern struct tree_descr spufs_dir_nosched_contents[];
233 /* system call implementation */
234 extern struct spufs_calls spufs_calls;
235 long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status);
236 long spufs_create(struct nameidata *nd, unsigned int flags,
237 mode_t mode, struct file *filp);
238 /* ELF coredump callbacks for writing SPU ELF notes */
239 extern int spufs_coredump_extra_notes_size(void);
240 extern int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset);
242 extern const struct file_operations spufs_context_fops;
244 /* gang management */
245 struct spu_gang *alloc_spu_gang(void);
246 struct spu_gang *get_spu_gang(struct spu_gang *gang);
247 int put_spu_gang(struct spu_gang *gang);
248 void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
249 void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
252 int spufs_handle_class1(struct spu_context *ctx);
253 int spufs_handle_class0(struct spu_context *ctx);
256 struct spu *affinity_check(struct spu_context *ctx);
258 /* context management */
259 extern atomic_t nr_spu_contexts;
260 static inline int __must_check spu_acquire(struct spu_context *ctx)
262 return mutex_lock_interruptible(&ctx->state_mutex);
265 static inline void spu_release(struct spu_context *ctx)
267 mutex_unlock(&ctx->state_mutex);
270 struct spu_context * alloc_spu_context(struct spu_gang *gang);
271 void destroy_spu_context(struct kref *kref);
272 struct spu_context * get_spu_context(struct spu_context *ctx);
273 int put_spu_context(struct spu_context *ctx);
274 void spu_unmap_mappings(struct spu_context *ctx);
276 void spu_forget(struct spu_context *ctx);
277 int __must_check spu_acquire_saved(struct spu_context *ctx);
278 void spu_release_saved(struct spu_context *ctx);
280 int spu_stopped(struct spu_context *ctx, u32 * stat);
281 void spu_del_from_rq(struct spu_context *ctx);
282 int spu_activate(struct spu_context *ctx, unsigned long flags);
283 void spu_deactivate(struct spu_context *ctx);
284 void spu_yield(struct spu_context *ctx);
285 void spu_switch_notify(struct spu *spu, struct spu_context *ctx);
286 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
288 void spu_set_timeslice(struct spu_context *ctx);
289 void spu_update_sched_info(struct spu_context *ctx);
290 void __spu_update_sched_info(struct spu_context *ctx);
291 int __init spu_sched_init(void);
292 void spu_sched_exit(void);
294 extern char *isolated_loader;
298 * Same as wait_event_interruptible(), except that here
299 * we need to call spu_release(ctx) before sleeping, and
300 * then spu_acquire(ctx) when awoken.
302 * Returns with state_mutex re-acquired when successfull or
303 * with -ERESTARTSYS and the state_mutex dropped when interrupted.
306 #define spufs_wait(wq, condition) \
309 DEFINE_WAIT(__wait); \
311 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
315 if (signal_pending(current)) { \
316 __ret = -ERESTARTSYS; \
320 __ret = spu_acquire(ctx); \
324 finish_wait(&(wq), &__wait); \
328 size_t spu_wbox_write(struct spu_context *ctx, u32 data);
329 size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
331 /* irq callback funcs. */
332 void spufs_ibox_callback(struct spu *spu);
333 void spufs_wbox_callback(struct spu *spu);
334 void spufs_stop_callback(struct spu *spu);
335 void spufs_mfc_callback(struct spu *spu);
336 void spufs_dma_callback(struct spu *spu, int type);
338 extern struct spu_coredump_calls spufs_coredump_calls;
339 struct spufs_coredump_reader {
341 ssize_t (*read)(struct spu_context *ctx,
342 char __user *buffer, size_t size, loff_t *pos);
343 u64 (*get)(struct spu_context *ctx);
346 extern struct spufs_coredump_reader spufs_coredump_read[];
347 extern int spufs_coredump_num_notes;
349 extern int spu_init_csa(struct spu_state *csa);
350 extern void spu_fini_csa(struct spu_state *csa);
351 extern int spu_save(struct spu_state *prev, struct spu *spu);
352 extern int spu_restore(struct spu_state *new, struct spu *spu);
353 extern int spu_switch(struct spu_state *prev, struct spu_state *new,
355 extern int spu_alloc_lscsa(struct spu_state *csa);
356 extern void spu_free_lscsa(struct spu_state *csa);
358 extern void spuctx_switch_state(struct spu_context *ctx,
359 enum spu_utilization_state new_state);
361 #define spu_context_trace(name, ctx, spu) \
362 trace_mark(name, "ctx %p spu %p", ctx, spu);
363 #define spu_context_nospu_trace(name, ctx) \
364 trace_mark(name, "ctx %p", ctx);