1 /*
2  * SPU file system
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 #ifndef SPUFS_H
23 #define SPUFS_H
24 
25 #include <linux/kref.h>
26 #include <linux/mutex.h>
27 #include <linux/spinlock.h>
28 #include <linux/fs.h>
29 #include <linux/cpumask.h>
30 
31 #include <asm/spu.h>
32 #include <asm/spu_csa.h>
33 #include <asm/spu_info.h>
34 
35 /* The magic number for our file system */
36 enum {
37 	SPUFS_MAGIC = 0x23c9b64e,
38 };
39 
40 struct spu_context_ops;
41 struct spu_gang;
42 
43 /* ctx->sched_flags */
44 enum {
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 */
48 };
49 
50 enum {
51 	SWITCH_LOG_BUFSIZE = 4096,
52 };
53 
54 enum {
55 	SWITCH_LOG_START,
56 	SWITCH_LOG_STOP,
57 	SWITCH_LOG_EXIT,
58 };
59 
60 struct switch_log {
61 	spinlock_t		lock;
62 	wait_queue_head_t	wait;
63 	unsigned long		head;
64 	unsigned long		tail;
65 	struct switch_log_entry {
66 		struct timespec	tstamp;
67 		s32		spu_id;
68 		u32		type;
69 		u32		val;
70 		u64		timebase;
71 	} log[];
72 };
73 
74 struct spu_context {
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 */
87 
88 	enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
89 	struct mutex state_mutex;
90 	struct mutex run_mutex;
91 
92 	struct mm_struct *owner;
93 
94 	struct kref kref;
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;
103 	u32 tagwait;
104 	struct spu_context_ops *ops;
105 	struct work_struct reap_work;
106 	unsigned long flags;
107 	unsigned long event_return;
108 
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);
113 
114 	/* owner thread */
115 	pid_t tid;
116 
117 	/* scheduler fields */
118 	struct list_head rq;
119 	unsigned int time_slice;
120 	unsigned long sched_flags;
121 	cpumask_t cpus_allowed;
122 	int policy;
123 	int prio;
124 	int last_ran;
125 
126 	/* statistics */
127 	struct {
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;
142 	} stats;
143 
144 	/* context switch log */
145 	struct switch_log *switch_log;
146 
147 	struct list_head aff_list;
148 	int aff_head;
149 	int aff_offset;
150 };
151 
152 struct spu_gang {
153 	struct list_head list;
154 	struct mutex mutex;
155 	struct kref kref;
156 	int contexts;
157 
158 	struct spu_context *aff_ref_ctx;
159 	struct list_head aff_list_head;
160 	struct mutex aff_mutex;
161 	int aff_flags;
162 	struct spu *aff_ref_spu;
163 	atomic_t aff_sched_count;
164 };
165 
166 /* Flag bits for spu_gang aff_flags */
167 #define AFF_OFFSETS_SET		1
168 #define AFF_MERGED		2
169 
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 */
178 };
179 
180 
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);
217 };
218 
219 extern struct spu_context_ops spu_hw_ops;
220 extern struct spu_context_ops spu_backing_ops;
221 
222 struct spufs_inode_info {
223 	struct spu_context *i_ctx;
224 	struct spu_gang *i_gang;
225 	struct inode vfs_inode;
226 	int i_openers;
227 };
228 #define SPUFS_I(inode) \
229 	container_of(inode, struct spufs_inode_info, vfs_inode)
230 
231 extern struct tree_descr spufs_dir_contents[];
232 extern struct tree_descr spufs_dir_nosched_contents[];
233 
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);
242 
243 extern const struct file_operations spufs_context_fops;
244 
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);
251 
252 /* fault handling */
253 int spufs_handle_class1(struct spu_context *ctx);
254 int spufs_handle_class0(struct spu_context *ctx);
255 
256 /* affinity */
257 struct spu *affinity_check(struct spu_context *ctx);
258 
259 /* context management */
260 extern atomic_t nr_spu_contexts;
261 static inline int __must_check spu_acquire(struct spu_context *ctx)
262 {
263 	return mutex_lock_interruptible(&ctx->state_mutex);
264 }
265 
266 static inline void spu_release(struct spu_context *ctx)
267 {
268 	mutex_unlock(&ctx->state_mutex);
269 }
270 
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);
276 
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);
280 
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,
288 		u32 type, u32 val);
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);
294 
295 extern char *isolated_loader;
296 
297 /*
298  * spufs_wait
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.
302  *
303  * 	Returns with state_mutex re-acquired when successfull or
304  * 	with -ERESTARTSYS and the state_mutex dropped when interrupted.
305  */
306 
307 #define spufs_wait(wq, condition)					\
308 ({									\
309 	int __ret = 0;							\
310 	DEFINE_WAIT(__wait);						\
311 	for (;;) {							\
312 		prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE);	\
313 		if (condition)						\
314 			break;						\
315 		spu_release(ctx);					\
316 		if (signal_pending(current)) {				\
317 			__ret = -ERESTARTSYS;				\
318 			break;						\
319 		}							\
320 		schedule();						\
321 		__ret = spu_acquire(ctx);				\
322 		if (__ret)						\
323 			break;						\
324 	}								\
325 	finish_wait(&(wq), &__wait);					\
326 	__ret;								\
327 })
328 
329 size_t spu_wbox_write(struct spu_context *ctx, u32 data);
330 size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
331 
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);
338 
339 extern struct spu_coredump_calls spufs_coredump_calls;
340 struct spufs_coredump_reader {
341 	char *name;
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);
345 	size_t size;
346 };
347 extern struct spufs_coredump_reader spufs_coredump_read[];
348 extern int spufs_coredump_num_notes;
349 
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,
355 		      struct spu *spu);
356 extern int spu_alloc_lscsa(struct spu_state *csa);
357 extern void spu_free_lscsa(struct spu_state *csa);
358 
359 extern void spuctx_switch_state(struct spu_context *ctx,
360 		enum spu_utilization_state new_state);
361 
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);
366 
367 #endif
368