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 struct spu *spu; /* pointer to a physical SPU */
52 struct spu_state csa; /* SPU context save area. */
53 spinlock_t mmio_lock; /* protects mmio access */
54 struct address_space *local_store; /* local store mapping. */
55 struct address_space *mfc; /* 'mfc' area mappings. */
56 struct address_space *cntl; /* 'control' area mappings. */
57 struct address_space *signal1; /* 'signal1' area mappings. */
58 struct address_space *signal2; /* 'signal2' area mappings. */
59 struct address_space *mss; /* 'mss' area mappings. */
60 struct address_space *psmap; /* 'psmap' area mappings. */
61 struct mutex mapping_lock;
62 u64 object_id; /* user space pointer for oprofile */
64 enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
65 struct mutex state_mutex;
66 struct mutex run_mutex;
68 struct mm_struct *owner;
71 wait_queue_head_t ibox_wq;
72 wait_queue_head_t wbox_wq;
73 wait_queue_head_t stop_wq;
74 wait_queue_head_t mfc_wq;
75 wait_queue_head_t run_wq;
76 struct fasync_struct *ibox_fasync;
77 struct fasync_struct *wbox_fasync;
78 struct fasync_struct *mfc_fasync;
80 struct spu_context_ops *ops;
81 struct work_struct reap_work;
83 unsigned long event_return;
85 struct list_head gang_list;
86 struct spu_gang *gang;
87 struct kref *prof_priv_kref;
88 void ( * prof_priv_release) (struct kref *kref);
93 /* scheduler fields */
95 unsigned int time_slice;
96 unsigned long sched_flags;
97 cpumask_t cpus_allowed;
103 /* updates protected by ctx->state_mutex */
104 enum spu_utilization_state util_state;
105 unsigned long long tstamp; /* time of last state switch */
106 unsigned long long times[SPU_UTIL_MAX];
107 unsigned long long vol_ctx_switch;
108 unsigned long long invol_ctx_switch;
109 unsigned long long min_flt;
110 unsigned long long maj_flt;
111 unsigned long long hash_flt;
112 unsigned long long slb_flt;
113 unsigned long long slb_flt_base; /* # at last ctx switch */
114 unsigned long long class2_intr;
115 unsigned long long class2_intr_base; /* # at last ctx switch */
116 unsigned long long libassist;
119 struct list_head aff_list;
125 struct list_head list;
130 struct spu_context *aff_ref_ctx;
131 struct list_head aff_list_head;
132 struct mutex aff_mutex;
134 struct spu *aff_ref_spu;
135 atomic_t aff_sched_count;
138 /* Flag bits for spu_gang aff_flags */
139 #define AFF_OFFSETS_SET 1
142 struct mfc_dma_command {
143 int32_t pad; /* reserved */
144 uint32_t lsa; /* local storage address */
145 uint64_t ea; /* effective address */
146 uint16_t size; /* transfer size */
147 uint16_t tag; /* command tag */
148 uint16_t class; /* class ID */
149 uint16_t cmd; /* command opcode */
153 /* SPU context query/set operations. */
154 struct spu_context_ops {
155 int (*mbox_read) (struct spu_context * ctx, u32 * data);
156 u32(*mbox_stat_read) (struct spu_context * ctx);
157 unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
158 unsigned int events);
159 int (*ibox_read) (struct spu_context * ctx, u32 * data);
160 int (*wbox_write) (struct spu_context * ctx, u32 data);
161 u32(*signal1_read) (struct spu_context * ctx);
162 void (*signal1_write) (struct spu_context * ctx, u32 data);
163 u32(*signal2_read) (struct spu_context * ctx);
164 void (*signal2_write) (struct spu_context * ctx, u32 data);
165 void (*signal1_type_set) (struct spu_context * ctx, u64 val);
166 u64(*signal1_type_get) (struct spu_context * ctx);
167 void (*signal2_type_set) (struct spu_context * ctx, u64 val);
168 u64(*signal2_type_get) (struct spu_context * ctx);
169 u32(*npc_read) (struct spu_context * ctx);
170 void (*npc_write) (struct spu_context * ctx, u32 data);
171 u32(*status_read) (struct spu_context * ctx);
172 char*(*get_ls) (struct spu_context * ctx);
173 void (*privcntl_write) (struct spu_context *ctx, u64 data);
174 u32 (*runcntl_read) (struct spu_context * ctx);
175 void (*runcntl_write) (struct spu_context * ctx, u32 data);
176 void (*runcntl_stop) (struct spu_context * ctx);
177 void (*master_start) (struct spu_context * ctx);
178 void (*master_stop) (struct spu_context * ctx);
179 int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
180 u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
181 u32 (*get_mfc_free_elements)(struct spu_context *ctx);
182 int (*send_mfc_command)(struct spu_context * ctx,
183 struct mfc_dma_command * cmd);
184 void (*dma_info_read) (struct spu_context * ctx,
185 struct spu_dma_info * info);
186 void (*proxydma_info_read) (struct spu_context * ctx,
187 struct spu_proxydma_info * info);
188 void (*restart_dma)(struct spu_context *ctx);
191 extern struct spu_context_ops spu_hw_ops;
192 extern struct spu_context_ops spu_backing_ops;
194 struct spufs_inode_info {
195 struct spu_context *i_ctx;
196 struct spu_gang *i_gang;
197 struct inode vfs_inode;
200 #define SPUFS_I(inode) \
201 container_of(inode, struct spufs_inode_info, vfs_inode)
203 extern struct tree_descr spufs_dir_contents[];
204 extern struct tree_descr spufs_dir_nosched_contents[];
206 /* system call implementation */
207 extern struct spufs_calls spufs_calls;
208 long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status);
209 long spufs_create(struct nameidata *nd, unsigned int flags,
210 mode_t mode, struct file *filp);
211 /* ELF coredump callbacks for writing SPU ELF notes */
212 extern int spufs_coredump_extra_notes_size(void);
213 extern int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset);
215 extern const struct file_operations spufs_context_fops;
217 /* gang management */
218 struct spu_gang *alloc_spu_gang(void);
219 struct spu_gang *get_spu_gang(struct spu_gang *gang);
220 int put_spu_gang(struct spu_gang *gang);
221 void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
222 void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
225 int spufs_handle_class1(struct spu_context *ctx);
226 int spufs_handle_class0(struct spu_context *ctx);
229 struct spu *affinity_check(struct spu_context *ctx);
231 /* context management */
232 extern atomic_t nr_spu_contexts;
233 static inline int __must_check spu_acquire(struct spu_context *ctx)
235 return mutex_lock_interruptible(&ctx->state_mutex);
238 static inline void spu_release(struct spu_context *ctx)
240 mutex_unlock(&ctx->state_mutex);
243 struct spu_context * alloc_spu_context(struct spu_gang *gang);
244 void destroy_spu_context(struct kref *kref);
245 struct spu_context * get_spu_context(struct spu_context *ctx);
246 int put_spu_context(struct spu_context *ctx);
247 void spu_unmap_mappings(struct spu_context *ctx);
249 void spu_forget(struct spu_context *ctx);
250 int __must_check spu_acquire_saved(struct spu_context *ctx);
251 void spu_release_saved(struct spu_context *ctx);
253 int spu_stopped(struct spu_context *ctx, u32 * stat);
254 void spu_del_from_rq(struct spu_context *ctx);
255 int spu_activate(struct spu_context *ctx, unsigned long flags);
256 void spu_deactivate(struct spu_context *ctx);
257 void spu_yield(struct spu_context *ctx);
258 void spu_switch_notify(struct spu *spu, struct spu_context *ctx);
259 void spu_set_timeslice(struct spu_context *ctx);
260 void spu_update_sched_info(struct spu_context *ctx);
261 void __spu_update_sched_info(struct spu_context *ctx);
262 int __init spu_sched_init(void);
263 void spu_sched_exit(void);
265 extern char *isolated_loader;
269 * Same as wait_event_interruptible(), except that here
270 * we need to call spu_release(ctx) before sleeping, and
271 * then spu_acquire(ctx) when awoken.
273 * Returns with state_mutex re-acquired when successfull or
274 * with -ERESTARTSYS and the state_mutex dropped when interrupted.
277 #define spufs_wait(wq, condition) \
280 DEFINE_WAIT(__wait); \
282 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
286 if (signal_pending(current)) { \
287 __ret = -ERESTARTSYS; \
291 __ret = spu_acquire(ctx); \
295 finish_wait(&(wq), &__wait); \
299 size_t spu_wbox_write(struct spu_context *ctx, u32 data);
300 size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
302 /* irq callback funcs. */
303 void spufs_ibox_callback(struct spu *spu);
304 void spufs_wbox_callback(struct spu *spu);
305 void spufs_stop_callback(struct spu *spu);
306 void spufs_mfc_callback(struct spu *spu);
307 void spufs_dma_callback(struct spu *spu, int type);
309 extern struct spu_coredump_calls spufs_coredump_calls;
310 struct spufs_coredump_reader {
312 ssize_t (*read)(struct spu_context *ctx,
313 char __user *buffer, size_t size, loff_t *pos);
314 u64 (*get)(struct spu_context *ctx);
317 extern struct spufs_coredump_reader spufs_coredump_read[];
318 extern int spufs_coredump_num_notes;
320 extern int spu_init_csa(struct spu_state *csa);
321 extern void spu_fini_csa(struct spu_state *csa);
322 extern int spu_save(struct spu_state *prev, struct spu *spu);
323 extern int spu_restore(struct spu_state *new, struct spu *spu);
324 extern int spu_switch(struct spu_state *prev, struct spu_state *new,
326 extern int spu_alloc_lscsa(struct spu_state *csa);
327 extern void spu_free_lscsa(struct spu_state *csa);
329 extern void spuctx_switch_state(struct spu_context *ctx,
330 enum spu_utilization_state new_state);
332 #define spu_context_trace(name, ctx, spu) \
333 trace_mark(name, "%p %p", ctx, spu);
334 #define spu_context_nospu_trace(name, ctx) \
335 trace_mark(name, "%p", ctx);