]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/cell/spufs/spufs.h
[POWERPC] spufs: add context switch notification log
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / cell / spufs / spufs.h
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
125         /* statistics */
126         struct {
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;
141         } stats;
142
143         /* context switch log */
144         struct switch_log *switch_log;
145
146         struct list_head aff_list;
147         int aff_head;
148         int aff_offset;
149 };
150
151 struct spu_gang {
152         struct list_head list;
153         struct mutex mutex;
154         struct kref kref;
155         int contexts;
156
157         struct spu_context *aff_ref_ctx;
158         struct list_head aff_list_head;
159         struct mutex aff_mutex;
160         int aff_flags;
161         struct spu *aff_ref_spu;
162         atomic_t aff_sched_count;
163 };
164
165 /* Flag bits for spu_gang aff_flags */
166 #define AFF_OFFSETS_SET         1
167 #define AFF_MERGED              2
168
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 */
177 };
178
179
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);
216 };
217
218 extern struct spu_context_ops spu_hw_ops;
219 extern struct spu_context_ops spu_backing_ops;
220
221 struct spufs_inode_info {
222         struct spu_context *i_ctx;
223         struct spu_gang *i_gang;
224         struct inode vfs_inode;
225         int i_openers;
226 };
227 #define SPUFS_I(inode) \
228         container_of(inode, struct spufs_inode_info, vfs_inode)
229
230 extern struct tree_descr spufs_dir_contents[];
231 extern struct tree_descr spufs_dir_nosched_contents[];
232
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);
241
242 extern const struct file_operations spufs_context_fops;
243
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);
250
251 /* fault handling */
252 int spufs_handle_class1(struct spu_context *ctx);
253 int spufs_handle_class0(struct spu_context *ctx);
254
255 /* affinity */
256 struct spu *affinity_check(struct spu_context *ctx);
257
258 /* context management */
259 extern atomic_t nr_spu_contexts;
260 static inline int __must_check spu_acquire(struct spu_context *ctx)
261 {
262         return mutex_lock_interruptible(&ctx->state_mutex);
263 }
264
265 static inline void spu_release(struct spu_context *ctx)
266 {
267         mutex_unlock(&ctx->state_mutex);
268 }
269
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);
275
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);
279
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,
287                 u32 type, u32 val);
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);
293
294 extern char *isolated_loader;
295
296 /*
297  * spufs_wait
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.
301  *
302  *      Returns with state_mutex re-acquired when successfull or
303  *      with -ERESTARTSYS and the state_mutex dropped when interrupted.
304  */
305
306 #define spufs_wait(wq, condition)                                       \
307 ({                                                                      \
308         int __ret = 0;                                                  \
309         DEFINE_WAIT(__wait);                                            \
310         for (;;) {                                                      \
311                 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE);    \
312                 if (condition)                                          \
313                         break;                                          \
314                 spu_release(ctx);                                       \
315                 if (signal_pending(current)) {                          \
316                         __ret = -ERESTARTSYS;                           \
317                         break;                                          \
318                 }                                                       \
319                 schedule();                                             \
320                 __ret = spu_acquire(ctx);                               \
321                 if (__ret)                                              \
322                         break;                                          \
323         }                                                               \
324         finish_wait(&(wq), &__wait);                                    \
325         __ret;                                                          \
326 })
327
328 size_t spu_wbox_write(struct spu_context *ctx, u32 data);
329 size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
330
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);
337
338 extern struct spu_coredump_calls spufs_coredump_calls;
339 struct spufs_coredump_reader {
340         char *name;
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);
344         size_t size;
345 };
346 extern struct spufs_coredump_reader spufs_coredump_read[];
347 extern int spufs_coredump_num_notes;
348
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,
354                       struct spu *spu);
355 extern int spu_alloc_lscsa(struct spu_state *csa);
356 extern void spu_free_lscsa(struct spu_state *csa);
357
358 extern void spuctx_switch_state(struct spu_context *ctx,
359                 enum spu_utilization_state new_state);
360
361 #define spu_context_trace(name, ctx, spu) \
362         trace_mark(name, "%p %p", ctx, spu);
363 #define spu_context_nospu_trace(name, ctx) \
364         trace_mark(name, "%p", ctx);
365
366 #endif