blob: 34d5f9f8b4ae1fc97a6aef185a77198936366f8a [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
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>
Christoph Hellwig650f8b02007-02-13 21:36:50 +010026#include <linux/mutex.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050027#include <linux/spinlock.h>
28#include <linux/fs.h>
Christoph Hellwigea1ae592007-06-29 10:57:56 +100029#include <linux/cpumask.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050030
31#include <asm/spu.h>
Mark Nutter5473af02005-11-15 15:53:49 -050032#include <asm/spu_csa.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010033#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050034
35/* The magic number for our file system */
36enum {
37 SPUFS_MAGIC = 0x23c9b64e,
38};
39
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050040struct spu_context_ops;
Arnd Bergmann62632032006-10-04 17:26:15 +020041struct spu_gang;
42
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +100043/*
44 * This is the state for spu utilization reporting to userspace.
45 * Because this state is visible to userspace it must never change and needs
46 * to be kept strictly separate from any internal state kept by the kernel.
47 */
48enum spuctx_execution_state {
49 SPUCTX_UTIL_USER = 0,
50 SPUCTX_UTIL_SYSTEM,
51 SPUCTX_UTIL_IOWAIT,
52 SPUCTX_UTIL_LOADED,
53 SPUCTX_UTIL_MAX
54};
55
Arnd Bergmann67207b92005-11-15 15:53:48 -050056struct spu_context {
57 struct spu *spu; /* pointer to a physical SPU */
Mark Nutter5473af02005-11-15 15:53:49 -050058 struct spu_state csa; /* SPU context save area. */
Arnd Bergmann67207b92005-11-15 15:53:48 -050059 spinlock_t mmio_lock; /* protects mmio access */
Mark Nutter6df10a82006-03-23 00:00:12 +010060 struct address_space *local_store; /* local store mapping. */
61 struct address_space *mfc; /* 'mfc' area mappings. */
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +020062 struct address_space *cntl; /* 'control' area mappings. */
63 struct address_space *signal1; /* 'signal1' area mappings. */
64 struct address_space *signal2; /* 'signal2' area mappings. */
65 struct address_space *mss; /* 'mss' area mappings. */
66 struct address_space *psmap; /* 'psmap' area mappings. */
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +100067 struct mutex mapping_lock;
Arnd Bergmann86767272006-10-04 17:26:21 +020068 u64 object_id; /* user space pointer for oprofile */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050069
70 enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
Christoph Hellwig650f8b02007-02-13 21:36:50 +010071 struct mutex state_mutex;
Christoph Hellwige45d48a32007-04-23 21:08:17 +020072 struct mutex run_mutex;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050073
74 struct mm_struct *owner;
Arnd Bergmann67207b92005-11-15 15:53:48 -050075
76 struct kref kref;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050077 wait_queue_head_t ibox_wq;
78 wait_queue_head_t wbox_wq;
Arnd Bergmann51104592005-12-05 22:52:25 -050079 wait_queue_head_t stop_wq;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +010080 wait_queue_head_t mfc_wq;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050081 struct fasync_struct *ibox_fasync;
82 struct fasync_struct *wbox_fasync;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +010083 struct fasync_struct *mfc_fasync;
84 u32 tagwait;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050085 struct spu_context_ops *ops;
Arnd Bergmann2a911f02005-12-05 22:52:26 -050086 struct work_struct reap_work;
Arnd Bergmann9add11d2006-10-04 17:26:14 +020087 unsigned long flags;
88 unsigned long event_return;
Arnd Bergmann62632032006-10-04 17:26:15 +020089
90 struct list_head gang_list;
91 struct spu_gang *gang;
Christoph Hellwig83899982007-02-13 21:54:22 +010092
Christoph Hellwig476273a2007-06-29 10:58:01 +100093 /* owner thread */
94 pid_t tid;
95
Christoph Hellwig83899982007-02-13 21:54:22 +010096 /* scheduler fields */
Jeremy Kerr70225432007-06-29 10:58:00 +100097 struct list_head rq;
Christoph Hellwig37901802007-06-29 10:57:51 +100098 unsigned int time_slice;
Christoph Hellwig26bec672007-02-13 21:54:24 +010099 unsigned long sched_flags;
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000100 cpumask_t cpus_allowed;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100101 int policy;
Christoph Hellwig83899982007-02-13 21:54:22 +0100102 int prio;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000103
104 /* statistics */
105 struct {
106 /* updates protected by ctx->state_mutex */
107 enum spuctx_execution_state execution_state;
108 unsigned long tstamp; /* time of last ctx switch */
109 unsigned long times[SPUCTX_UTIL_MAX];
110 unsigned long long vol_ctx_switch;
111 unsigned long long invol_ctx_switch;
112 unsigned long long min_flt;
113 unsigned long long maj_flt;
114 unsigned long long hash_flt;
115 unsigned long long slb_flt;
116 unsigned long long slb_flt_base; /* # at last ctx switch */
117 unsigned long long class2_intr;
118 unsigned long long class2_intr_base; /* # at last ctx switch */
119 unsigned long long libassist;
120 } stats;
Arnd Bergmann62632032006-10-04 17:26:15 +0200121};
122
123struct spu_gang {
124 struct list_head list;
125 struct mutex mutex;
126 struct kref kref;
127 int contexts;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500128};
129
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100130struct mfc_dma_command {
131 int32_t pad; /* reserved */
132 uint32_t lsa; /* local storage address */
133 uint64_t ea; /* effective address */
134 uint16_t size; /* transfer size */
135 uint16_t tag; /* command tag */
136 uint16_t class; /* class ID */
137 uint16_t cmd; /* command opcode */
138};
139
140
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500141/* SPU context query/set operations. */
142struct spu_context_ops {
143 int (*mbox_read) (struct spu_context * ctx, u32 * data);
144 u32(*mbox_stat_read) (struct spu_context * ctx);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500145 unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
146 unsigned int events);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500147 int (*ibox_read) (struct spu_context * ctx, u32 * data);
148 int (*wbox_write) (struct spu_context * ctx, u32 data);
149 u32(*signal1_read) (struct spu_context * ctx);
150 void (*signal1_write) (struct spu_context * ctx, u32 data);
151 u32(*signal2_read) (struct spu_context * ctx);
152 void (*signal2_write) (struct spu_context * ctx, u32 data);
153 void (*signal1_type_set) (struct spu_context * ctx, u64 val);
154 u64(*signal1_type_get) (struct spu_context * ctx);
155 void (*signal2_type_set) (struct spu_context * ctx, u64 val);
156 u64(*signal2_type_get) (struct spu_context * ctx);
157 u32(*npc_read) (struct spu_context * ctx);
158 void (*npc_write) (struct spu_context * ctx, u32 data);
159 u32(*status_read) (struct spu_context * ctx);
160 char*(*get_ls) (struct spu_context * ctx);
Jeremy Kerr3960c262006-11-20 18:45:09 +0100161 u32 (*runcntl_read) (struct spu_context * ctx);
Arnd Bergmann51104592005-12-05 22:52:25 -0500162 void (*runcntl_write) (struct spu_context * ctx, u32 data);
Arnd Bergmannee2d7342006-11-20 18:45:08 +0100163 void (*master_start) (struct spu_context * ctx);
164 void (*master_stop) (struct spu_context * ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100165 int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
166 u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
167 u32 (*get_mfc_free_elements)(struct spu_context *ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +0100168 int (*send_mfc_command)(struct spu_context * ctx,
169 struct mfc_dma_command * cmd);
170 void (*dma_info_read) (struct spu_context * ctx,
171 struct spu_dma_info * info);
172 void (*proxydma_info_read) (struct spu_context * ctx,
173 struct spu_proxydma_info * info);
Arnd Bergmann57dace22007-04-23 21:08:15 +0200174 void (*restart_dma)(struct spu_context *ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500175};
176
177extern struct spu_context_ops spu_hw_ops;
178extern struct spu_context_ops spu_backing_ops;
179
Arnd Bergmann67207b92005-11-15 15:53:48 -0500180struct spufs_inode_info {
181 struct spu_context *i_ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200182 struct spu_gang *i_gang;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500183 struct inode vfs_inode;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200184 int i_openers;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500185};
186#define SPUFS_I(inode) \
187 container_of(inode, struct spufs_inode_info, vfs_inode)
188
189extern struct tree_descr spufs_dir_contents[];
Mark Nutter5737edd2006-10-24 18:31:16 +0200190extern struct tree_descr spufs_dir_nosched_contents[];
Arnd Bergmann67207b92005-11-15 15:53:48 -0500191
192/* system call implementation */
193long spufs_run_spu(struct file *file,
194 struct spu_context *ctx, u32 *npc, u32 *status);
Arnd Bergmann62632032006-10-04 17:26:15 +0200195long spufs_create(struct nameidata *nd,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196 unsigned int flags, mode_t mode);
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800197extern const struct file_operations spufs_context_fops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500198
Arnd Bergmann62632032006-10-04 17:26:15 +0200199/* gang management */
200struct spu_gang *alloc_spu_gang(void);
201struct spu_gang *get_spu_gang(struct spu_gang *gang);
202int put_spu_gang(struct spu_gang *gang);
203void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
204void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
205
Arnd Bergmann57dace22007-04-23 21:08:15 +0200206/* fault handling */
207int spufs_handle_class1(struct spu_context *ctx);
208
Arnd Bergmann67207b92005-11-15 15:53:48 -0500209/* context management */
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000210extern atomic_t nr_spu_contexts;
Christoph Hellwig6a0641e52007-02-13 21:54:21 +0100211static inline void spu_acquire(struct spu_context *ctx)
212{
213 mutex_lock(&ctx->state_mutex);
214}
215
216static inline void spu_release(struct spu_context *ctx)
217{
218 mutex_unlock(&ctx->state_mutex);
219}
220
Arnd Bergmann62632032006-10-04 17:26:15 +0200221struct spu_context * alloc_spu_context(struct spu_gang *gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500222void destroy_spu_context(struct kref *kref);
223struct spu_context * get_spu_context(struct spu_context *ctx);
224int put_spu_context(struct spu_context *ctx);
Arnd Bergmann51104592005-12-05 22:52:25 -0500225void spu_unmap_mappings(struct spu_context *ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500226
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500227void spu_forget(struct spu_context *ctx);
Christoph Hellwig26bec672007-02-13 21:54:24 +0100228int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500229void spu_acquire_saved(struct spu_context *ctx);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100230
Christoph Hellwig26bec672007-02-13 21:54:24 +0100231int spu_activate(struct spu_context *ctx, unsigned long flags);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500232void spu_deactivate(struct spu_context *ctx);
233void spu_yield(struct spu_context *ctx);
Christoph Hellwigfe443ef2007-06-29 10:57:52 +1000234void spu_set_timeslice(struct spu_context *ctx);
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000235void spu_update_sched_info(struct spu_context *ctx);
236void __spu_update_sched_info(struct spu_context *ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500237int __init spu_sched_init(void);
Sebastian Siewiord1450312007-07-20 21:39:29 +0200238void spu_sched_exit(void);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500239
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100240extern char *isolated_loader;
241
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100242/*
243 * spufs_wait
Jeremy Kerr70225432007-06-29 10:58:00 +1000244 * Same as wait_event_interruptible(), except that here
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100245 * we need to call spu_release(ctx) before sleeping, and
246 * then spu_acquire(ctx) when awoken.
247 */
248
249#define spufs_wait(wq, condition) \
250({ \
251 int __ret = 0; \
252 DEFINE_WAIT(__wait); \
253 for (;;) { \
254 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
255 if (condition) \
256 break; \
Jeremy Kerrd3764392007-04-23 21:08:24 +0200257 if (signal_pending(current)) { \
258 __ret = -ERESTARTSYS; \
259 break; \
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100260 } \
Jeremy Kerrd3764392007-04-23 21:08:24 +0200261 spu_release(ctx); \
262 schedule(); \
263 spu_acquire(ctx); \
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100264 } \
265 finish_wait(&(wq), &__wait); \
266 __ret; \
267})
268
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500269size_t spu_wbox_write(struct spu_context *ctx, u32 data);
270size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
271
272/* irq callback funcs. */
273void spufs_ibox_callback(struct spu *spu);
274void spufs_wbox_callback(struct spu *spu);
Arnd Bergmann51104592005-12-05 22:52:25 -0500275void spufs_stop_callback(struct spu *spu);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100276void spufs_mfc_callback(struct spu *spu);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200277void spufs_dma_callback(struct spu *spu, int type);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500278
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100279extern struct spu_coredump_calls spufs_coredump_calls;
280struct spufs_coredump_reader {
281 char *name;
282 ssize_t (*read)(struct spu_context *ctx,
283 char __user *buffer, size_t size, loff_t *pos);
284 u64 (*get)(void *data);
285 size_t size;
286};
287extern struct spufs_coredump_reader spufs_coredump_read[];
288extern int spufs_coredump_num_notes;
289
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000290/*
291 * This function is a little bit too large for an inline, but
292 * as fault.c is built into the kernel we can't move it out of
293 * line.
294 */
295static inline void spuctx_switch_state(struct spu_context *ctx,
296 enum spuctx_execution_state new_state)
297{
298 WARN_ON(!mutex_is_locked(&ctx->state_mutex));
299
300 if (ctx->stats.execution_state != new_state) {
301 unsigned long curtime = jiffies;
302
303 ctx->stats.times[ctx->stats.execution_state] +=
304 curtime - ctx->stats.tstamp;
305 ctx->stats.tstamp = curtime;
306 ctx->stats.execution_state = new_state;
307 }
308}
309
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000310static inline void spu_switch_state(struct spu *spu,
311 enum spuctx_execution_state new_state)
312{
313 if (spu->stats.utilization_state != new_state) {
314 unsigned long curtime = jiffies;
315
316 spu->stats.times[spu->stats.utilization_state] +=
317 curtime - spu->stats.tstamp;
318 spu->stats.tstamp = curtime;
319 spu->stats.utilization_state = new_state;
320 }
321}
322
Arnd Bergmann67207b92005-11-15 15:53:48 -0500323#endif