167207b96SArnd Bergmann /*
267207b96SArnd Bergmann  * SPU file system
367207b96SArnd Bergmann  *
467207b96SArnd Bergmann  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
567207b96SArnd Bergmann  *
667207b96SArnd Bergmann  * Author: Arnd Bergmann <arndb@de.ibm.com>
767207b96SArnd Bergmann  *
867207b96SArnd Bergmann  * This program is free software; you can redistribute it and/or modify
967207b96SArnd Bergmann  * it under the terms of the GNU General Public License as published by
1067207b96SArnd Bergmann  * the Free Software Foundation; either version 2, or (at your option)
1167207b96SArnd Bergmann  * any later version.
1267207b96SArnd Bergmann  *
1367207b96SArnd Bergmann  * This program is distributed in the hope that it will be useful,
1467207b96SArnd Bergmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1567207b96SArnd Bergmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1667207b96SArnd Bergmann  * GNU General Public License for more details.
1767207b96SArnd Bergmann  *
1867207b96SArnd Bergmann  * You should have received a copy of the GNU General Public License
1967207b96SArnd Bergmann  * along with this program; if not, write to the Free Software
2067207b96SArnd Bergmann  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2167207b96SArnd Bergmann  */
2267207b96SArnd Bergmann #ifndef SPUFS_H
2367207b96SArnd Bergmann #define SPUFS_H
2467207b96SArnd Bergmann 
2567207b96SArnd Bergmann #include <linux/kref.h>
26650f8b02SChristoph Hellwig #include <linux/mutex.h>
2767207b96SArnd Bergmann #include <linux/spinlock.h>
2867207b96SArnd Bergmann #include <linux/fs.h>
29ea1ae594SChristoph Hellwig #include <linux/cpumask.h>
3067207b96SArnd Bergmann 
3167207b96SArnd Bergmann #include <asm/spu.h>
325473af04SMark Nutter #include <asm/spu_csa.h>
33b9e3bd77SDwayne Grant McConnell #include <asm/spu_info.h>
3467207b96SArnd Bergmann 
3567207b96SArnd Bergmann /* The magic number for our file system */
3667207b96SArnd Bergmann enum {
3767207b96SArnd Bergmann 	SPUFS_MAGIC = 0x23c9b64e,
3867207b96SArnd Bergmann };
3967207b96SArnd Bergmann 
408b3d6663SArnd Bergmann struct spu_context_ops;
416263203eSArnd Bergmann struct spu_gang;
426263203eSArnd Bergmann 
43e9f8a0b6SChristoph Hellwig /*
44e9f8a0b6SChristoph Hellwig  * This is the state for spu utilization reporting to userspace.
45e9f8a0b6SChristoph Hellwig  * Because this state is visible to userspace it must never change and needs
46e9f8a0b6SChristoph Hellwig  * to be kept strictly separate from any internal state kept by the kernel.
47e9f8a0b6SChristoph Hellwig  */
48e9f8a0b6SChristoph Hellwig enum spuctx_execution_state {
49e9f8a0b6SChristoph Hellwig 	SPUCTX_UTIL_USER = 0,
50e9f8a0b6SChristoph Hellwig 	SPUCTX_UTIL_SYSTEM,
51e9f8a0b6SChristoph Hellwig 	SPUCTX_UTIL_IOWAIT,
52e9f8a0b6SChristoph Hellwig 	SPUCTX_UTIL_LOADED,
53e9f8a0b6SChristoph Hellwig 	SPUCTX_UTIL_MAX
54e9f8a0b6SChristoph Hellwig };
55e9f8a0b6SChristoph Hellwig 
5667207b96SArnd Bergmann struct spu_context {
5767207b96SArnd Bergmann 	struct spu *spu;		  /* pointer to a physical SPU */
585473af04SMark Nutter 	struct spu_state csa;		  /* SPU context save area. */
5967207b96SArnd Bergmann 	spinlock_t mmio_lock;		  /* protects mmio access */
606df10a82SMark Nutter 	struct address_space *local_store; /* local store mapping.  */
616df10a82SMark Nutter 	struct address_space *mfc;	   /* 'mfc' area mappings. */
626df10a82SMark Nutter 	struct address_space *cntl;	   /* 'control' area mappings. */
636df10a82SMark Nutter 	struct address_space *signal1;	   /* 'signal1' area mappings. */
646df10a82SMark Nutter 	struct address_space *signal2;	   /* 'signal2' area mappings. */
6517e0e270SBenjamin Herrenschmidt 	struct address_space *mss;	   /* 'mss' area mappings. */
6617e0e270SBenjamin Herrenschmidt 	struct address_space *psmap;	   /* 'psmap' area mappings. */
6747d3a5faSChristoph Hellwig 	struct mutex mapping_lock;
6886767277SArnd Bergmann 	u64 object_id;		   /* user space pointer for oprofile */
698b3d6663SArnd Bergmann 
708b3d6663SArnd Bergmann 	enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
71650f8b02SChristoph Hellwig 	struct mutex state_mutex;
72e45d48a3SChristoph Hellwig 	struct mutex run_mutex;
738b3d6663SArnd Bergmann 
748b3d6663SArnd Bergmann 	struct mm_struct *owner;
7567207b96SArnd Bergmann 
7667207b96SArnd Bergmann 	struct kref kref;
778b3d6663SArnd Bergmann 	wait_queue_head_t ibox_wq;
788b3d6663SArnd Bergmann 	wait_queue_head_t wbox_wq;
795110459fSArnd Bergmann 	wait_queue_head_t stop_wq;
80a33a7d73SArnd Bergmann 	wait_queue_head_t mfc_wq;
818b3d6663SArnd Bergmann 	struct fasync_struct *ibox_fasync;
828b3d6663SArnd Bergmann 	struct fasync_struct *wbox_fasync;
83a33a7d73SArnd Bergmann 	struct fasync_struct *mfc_fasync;
84a33a7d73SArnd Bergmann 	u32 tagwait;
858b3d6663SArnd Bergmann 	struct spu_context_ops *ops;
862a911f0bSArnd Bergmann 	struct work_struct reap_work;
879add11daSArnd Bergmann 	unsigned long flags;
889add11daSArnd Bergmann 	unsigned long event_return;
896263203eSArnd Bergmann 
906263203eSArnd Bergmann 	struct list_head gang_list;
916263203eSArnd Bergmann 	struct spu_gang *gang;
928389998aSChristoph Hellwig 
93476273adSChristoph Hellwig 	/* owner thread */
94476273adSChristoph Hellwig 	pid_t tid;
95476273adSChristoph Hellwig 
968389998aSChristoph Hellwig 	/* scheduler fields */
97079cdb61SChristoph Hellwig 	struct list_head rq;
9837901802SChristoph Hellwig 	unsigned int time_slice;
9926bec673SChristoph Hellwig 	unsigned long sched_flags;
100ea1ae594SChristoph Hellwig 	cpumask_t cpus_allowed;
1012eb1b120SChristoph Hellwig 	int policy;
1028389998aSChristoph Hellwig 	int prio;
103e9f8a0b6SChristoph Hellwig 
104e9f8a0b6SChristoph Hellwig 	/* statistics */
105e9f8a0b6SChristoph Hellwig 	struct {
106e9f8a0b6SChristoph Hellwig 		/* updates protected by ctx->state_mutex */
107e9f8a0b6SChristoph Hellwig 		enum spuctx_execution_state execution_state;
108e9f8a0b6SChristoph Hellwig 		unsigned long tstamp;		/* time of last ctx switch */
109e9f8a0b6SChristoph Hellwig 		unsigned long times[SPUCTX_UTIL_MAX];
110e9f8a0b6SChristoph Hellwig 		unsigned long long vol_ctx_switch;
111e9f8a0b6SChristoph Hellwig 		unsigned long long invol_ctx_switch;
112e9f8a0b6SChristoph Hellwig 		unsigned long long min_flt;
113e9f8a0b6SChristoph Hellwig 		unsigned long long maj_flt;
114e9f8a0b6SChristoph Hellwig 		unsigned long long hash_flt;
115e9f8a0b6SChristoph Hellwig 		unsigned long long slb_flt;
116e9f8a0b6SChristoph Hellwig 		unsigned long long slb_flt_base; /* # at last ctx switch */
117e9f8a0b6SChristoph Hellwig 		unsigned long long class2_intr;
118e9f8a0b6SChristoph Hellwig 		unsigned long long class2_intr_base; /* # at last ctx switch */
119e9f8a0b6SChristoph Hellwig 		unsigned long long libassist;
120e9f8a0b6SChristoph Hellwig 	} stats;
1216263203eSArnd Bergmann };
1226263203eSArnd Bergmann 
1236263203eSArnd Bergmann struct spu_gang {
1246263203eSArnd Bergmann 	struct list_head list;
1256263203eSArnd Bergmann 	struct mutex mutex;
1266263203eSArnd Bergmann 	struct kref kref;
1276263203eSArnd Bergmann 	int contexts;
12867207b96SArnd Bergmann };
12967207b96SArnd Bergmann 
130a33a7d73SArnd Bergmann struct mfc_dma_command {
131a33a7d73SArnd Bergmann 	int32_t pad;	/* reserved */
132a33a7d73SArnd Bergmann 	uint32_t lsa;	/* local storage address */
133a33a7d73SArnd Bergmann 	uint64_t ea;	/* effective address */
134a33a7d73SArnd Bergmann 	uint16_t size;	/* transfer size */
135a33a7d73SArnd Bergmann 	uint16_t tag;	/* command tag */
136a33a7d73SArnd Bergmann 	uint16_t class;	/* class ID */
137a33a7d73SArnd Bergmann 	uint16_t cmd;	/* command opcode */
138a33a7d73SArnd Bergmann };
139a33a7d73SArnd Bergmann 
140a33a7d73SArnd Bergmann 
1418b3d6663SArnd Bergmann /* SPU context query/set operations. */
1428b3d6663SArnd Bergmann struct spu_context_ops {
1438b3d6663SArnd Bergmann 	int (*mbox_read) (struct spu_context * ctx, u32 * data);
1448b3d6663SArnd Bergmann 	 u32(*mbox_stat_read) (struct spu_context * ctx);
1453a843d7cSArnd Bergmann 	unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
1463a843d7cSArnd Bergmann 					unsigned int events);
1478b3d6663SArnd Bergmann 	int (*ibox_read) (struct spu_context * ctx, u32 * data);
1488b3d6663SArnd Bergmann 	int (*wbox_write) (struct spu_context * ctx, u32 data);
1498b3d6663SArnd Bergmann 	 u32(*signal1_read) (struct spu_context * ctx);
1508b3d6663SArnd Bergmann 	void (*signal1_write) (struct spu_context * ctx, u32 data);
1518b3d6663SArnd Bergmann 	 u32(*signal2_read) (struct spu_context * ctx);
1528b3d6663SArnd Bergmann 	void (*signal2_write) (struct spu_context * ctx, u32 data);
1538b3d6663SArnd Bergmann 	void (*signal1_type_set) (struct spu_context * ctx, u64 val);
1548b3d6663SArnd Bergmann 	 u64(*signal1_type_get) (struct spu_context * ctx);
1558b3d6663SArnd Bergmann 	void (*signal2_type_set) (struct spu_context * ctx, u64 val);
1568b3d6663SArnd Bergmann 	 u64(*signal2_type_get) (struct spu_context * ctx);
1578b3d6663SArnd Bergmann 	 u32(*npc_read) (struct spu_context * ctx);
1588b3d6663SArnd Bergmann 	void (*npc_write) (struct spu_context * ctx, u32 data);
1598b3d6663SArnd Bergmann 	 u32(*status_read) (struct spu_context * ctx);
1608b3d6663SArnd Bergmann 	char*(*get_ls) (struct spu_context * ctx);
1613960c260SJeremy Kerr 	 u32 (*runcntl_read) (struct spu_context * ctx);
1625110459fSArnd Bergmann 	void (*runcntl_write) (struct spu_context * ctx, u32 data);
163ee2d7340SArnd Bergmann 	void (*master_start) (struct spu_context * ctx);
164ee2d7340SArnd Bergmann 	void (*master_stop) (struct spu_context * ctx);
165a33a7d73SArnd Bergmann 	int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
166a33a7d73SArnd Bergmann 	u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
167a33a7d73SArnd Bergmann 	u32 (*get_mfc_free_elements)(struct spu_context *ctx);
168a33a7d73SArnd Bergmann 	int (*send_mfc_command)(struct spu_context * ctx,
169a33a7d73SArnd Bergmann 				struct mfc_dma_command * cmd);
170b9e3bd77SDwayne Grant McConnell 	void (*dma_info_read) (struct spu_context * ctx,
171b9e3bd77SDwayne Grant McConnell 			       struct spu_dma_info * info);
172b9e3bd77SDwayne Grant McConnell 	void (*proxydma_info_read) (struct spu_context * ctx,
173b9e3bd77SDwayne Grant McConnell 				    struct spu_proxydma_info * info);
17457dace23SArnd Bergmann 	void (*restart_dma)(struct spu_context *ctx);
1758b3d6663SArnd Bergmann };
1768b3d6663SArnd Bergmann 
1778b3d6663SArnd Bergmann extern struct spu_context_ops spu_hw_ops;
1788b3d6663SArnd Bergmann extern struct spu_context_ops spu_backing_ops;
1798b3d6663SArnd Bergmann 
18067207b96SArnd Bergmann struct spufs_inode_info {
18167207b96SArnd Bergmann 	struct spu_context *i_ctx;
1826263203eSArnd Bergmann 	struct spu_gang *i_gang;
18367207b96SArnd Bergmann 	struct inode vfs_inode;
18443c2bbd9SChristoph Hellwig 	int i_openers;
18567207b96SArnd Bergmann };
18667207b96SArnd Bergmann #define SPUFS_I(inode) \
18767207b96SArnd Bergmann 	container_of(inode, struct spufs_inode_info, vfs_inode)
18867207b96SArnd Bergmann 
18967207b96SArnd Bergmann extern struct tree_descr spufs_dir_contents[];
1905737edd1SMark Nutter extern struct tree_descr spufs_dir_nosched_contents[];
19167207b96SArnd Bergmann 
19267207b96SArnd Bergmann /* system call implementation */
19367207b96SArnd Bergmann long spufs_run_spu(struct file *file,
19467207b96SArnd Bergmann 		   struct spu_context *ctx, u32 *npc, u32 *status);
1956263203eSArnd Bergmann long spufs_create(struct nameidata *nd,
19667207b96SArnd Bergmann 			 unsigned int flags, mode_t mode);
1979c2e08c5SArjan van de Ven extern const struct file_operations spufs_context_fops;
19867207b96SArnd Bergmann 
1996263203eSArnd Bergmann /* gang management */
2006263203eSArnd Bergmann struct spu_gang *alloc_spu_gang(void);
2016263203eSArnd Bergmann struct spu_gang *get_spu_gang(struct spu_gang *gang);
2026263203eSArnd Bergmann int put_spu_gang(struct spu_gang *gang);
2036263203eSArnd Bergmann void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
2046263203eSArnd Bergmann void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
2056263203eSArnd Bergmann 
20657dace23SArnd Bergmann /* fault handling */
20757dace23SArnd Bergmann int spufs_handle_class1(struct spu_context *ctx);
20857dace23SArnd Bergmann 
20967207b96SArnd Bergmann /* context management */
21065de66f0SChristoph Hellwig extern atomic_t nr_spu_contexts;
2116a0641e5SChristoph Hellwig static inline void spu_acquire(struct spu_context *ctx)
2126a0641e5SChristoph Hellwig {
2136a0641e5SChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
2146a0641e5SChristoph Hellwig }
2156a0641e5SChristoph Hellwig 
2166a0641e5SChristoph Hellwig static inline void spu_release(struct spu_context *ctx)
2176a0641e5SChristoph Hellwig {
2186a0641e5SChristoph Hellwig 	mutex_unlock(&ctx->state_mutex);
2196a0641e5SChristoph Hellwig }
2206a0641e5SChristoph Hellwig 
2216263203eSArnd Bergmann struct spu_context * alloc_spu_context(struct spu_gang *gang);
22267207b96SArnd Bergmann void destroy_spu_context(struct kref *kref);
22367207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx);
22467207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx);
2255110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx);
22667207b96SArnd Bergmann 
2278b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx);
22826bec673SChristoph Hellwig int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags);
22967207b96SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx);
23050b520d4SChristoph Hellwig 
23126bec673SChristoph Hellwig int spu_activate(struct spu_context *ctx, unsigned long flags);
2328b3d6663SArnd Bergmann void spu_deactivate(struct spu_context *ctx);
2338b3d6663SArnd Bergmann void spu_yield(struct spu_context *ctx);
234fe443ef2SChristoph Hellwig void spu_set_timeslice(struct spu_context *ctx);
2352cf2b3b4SChristoph Hellwig void spu_update_sched_info(struct spu_context *ctx);
2362cf2b3b4SChristoph Hellwig void __spu_update_sched_info(struct spu_context *ctx);
2378b3d6663SArnd Bergmann int __init spu_sched_init(void);
2388b3d6663SArnd Bergmann void __exit spu_sched_exit(void);
2398b3d6663SArnd Bergmann 
240c6730ed4SJeremy Kerr extern char *isolated_loader;
241c6730ed4SJeremy Kerr 
242ce8ab854SArnd Bergmann /*
243ce8ab854SArnd Bergmann  * spufs_wait
244ce8ab854SArnd Bergmann  *	Same as wait_event_interruptible(), except that here
245ce8ab854SArnd Bergmann  *	we need to call spu_release(ctx) before sleeping, and
246ce8ab854SArnd Bergmann  *	then spu_acquire(ctx) when awoken.
247ce8ab854SArnd Bergmann  */
248ce8ab854SArnd Bergmann 
249ce8ab854SArnd Bergmann #define spufs_wait(wq, condition)					\
250ce8ab854SArnd Bergmann ({									\
251ce8ab854SArnd Bergmann 	int __ret = 0;							\
252ce8ab854SArnd Bergmann 	DEFINE_WAIT(__wait);						\
253ce8ab854SArnd Bergmann 	for (;;) {							\
254ce8ab854SArnd Bergmann 		prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE);	\
255ce8ab854SArnd Bergmann 		if (condition)						\
256ce8ab854SArnd Bergmann 			break;						\
257d3764397SJeremy Kerr 		if (signal_pending(current)) {				\
258d3764397SJeremy Kerr 			__ret = -ERESTARTSYS;				\
259d3764397SJeremy Kerr 			break;						\
260d3764397SJeremy Kerr 		}							\
261ce8ab854SArnd Bergmann 		spu_release(ctx);					\
262ce8ab854SArnd Bergmann 		schedule();						\
263ce8ab854SArnd Bergmann 		spu_acquire(ctx);					\
264ce8ab854SArnd Bergmann 	}								\
265ce8ab854SArnd Bergmann 	finish_wait(&(wq), &__wait);					\
266ce8ab854SArnd Bergmann 	__ret;								\
267ce8ab854SArnd Bergmann })
268ce8ab854SArnd Bergmann 
2698b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data);
2708b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
2718b3d6663SArnd Bergmann 
2728b3d6663SArnd Bergmann /* irq callback funcs. */
2738b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu);
2748b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu);
2755110459fSArnd Bergmann void spufs_stop_callback(struct spu *spu);
276a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu);
2779add11daSArnd Bergmann void spufs_dma_callback(struct spu *spu, int type);
2788b3d6663SArnd Bergmann 
279bf1ab978SDwayne Grant McConnell extern struct spu_coredump_calls spufs_coredump_calls;
280bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader {
281bf1ab978SDwayne Grant McConnell 	char *name;
282bf1ab978SDwayne Grant McConnell 	ssize_t (*read)(struct spu_context *ctx,
283bf1ab978SDwayne Grant McConnell 			char __user *buffer, size_t size, loff_t *pos);
284bf1ab978SDwayne Grant McConnell 	u64 (*get)(void *data);
285bf1ab978SDwayne Grant McConnell 	size_t size;
286bf1ab978SDwayne Grant McConnell };
287bf1ab978SDwayne Grant McConnell extern struct spufs_coredump_reader spufs_coredump_read[];
288bf1ab978SDwayne Grant McConnell extern int spufs_coredump_num_notes;
289bf1ab978SDwayne Grant McConnell 
290e9f8a0b6SChristoph Hellwig /*
291e9f8a0b6SChristoph Hellwig  * This function is a little bit too large for an inline, but
292e9f8a0b6SChristoph Hellwig  * as fault.c is built into the kernel we can't move it out of
293e9f8a0b6SChristoph Hellwig  * line.
294e9f8a0b6SChristoph Hellwig  */
295e9f8a0b6SChristoph Hellwig static inline void spuctx_switch_state(struct spu_context *ctx,
296e9f8a0b6SChristoph Hellwig 		enum spuctx_execution_state new_state)
297e9f8a0b6SChristoph Hellwig {
298e9f8a0b6SChristoph Hellwig 	WARN_ON(!mutex_is_locked(&ctx->state_mutex));
299e9f8a0b6SChristoph Hellwig 
300e9f8a0b6SChristoph Hellwig 	if (ctx->stats.execution_state != new_state) {
301e9f8a0b6SChristoph Hellwig 		unsigned long curtime = jiffies;
302e9f8a0b6SChristoph Hellwig 
303e9f8a0b6SChristoph Hellwig 		ctx->stats.times[ctx->stats.execution_state] +=
304e9f8a0b6SChristoph Hellwig 				 curtime - ctx->stats.tstamp;
305e9f8a0b6SChristoph Hellwig 		ctx->stats.tstamp = curtime;
306e9f8a0b6SChristoph Hellwig 		ctx->stats.execution_state = new_state;
307e9f8a0b6SChristoph Hellwig 	}
308e9f8a0b6SChristoph Hellwig }
309e9f8a0b6SChristoph Hellwig 
31067207b96SArnd Bergmann #endif
311