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>
2967207b96SArnd Bergmann 
3067207b96SArnd Bergmann #include <asm/spu.h>
315473af04SMark Nutter #include <asm/spu_csa.h>
32b9e3bd77SDwayne Grant McConnell #include <asm/spu_info.h>
3367207b96SArnd Bergmann 
3467207b96SArnd Bergmann /* The magic number for our file system */
3567207b96SArnd Bergmann enum {
3667207b96SArnd Bergmann 	SPUFS_MAGIC = 0x23c9b64e,
3767207b96SArnd Bergmann };
3867207b96SArnd Bergmann 
398b3d6663SArnd Bergmann struct spu_context_ops;
406263203eSArnd Bergmann struct spu_gang;
416263203eSArnd Bergmann 
4226bec673SChristoph Hellwig /* ctx->sched_flags */
4326bec673SChristoph Hellwig enum {
4426bec673SChristoph Hellwig 	SPU_SCHED_WAKE = 0,
4526bec673SChristoph Hellwig };
4626bec673SChristoph Hellwig 
4767207b96SArnd Bergmann struct spu_context {
4867207b96SArnd Bergmann 	struct spu *spu;		  /* pointer to a physical SPU */
495473af04SMark Nutter 	struct spu_state csa;		  /* SPU context save area. */
5067207b96SArnd Bergmann 	spinlock_t mmio_lock;		  /* protects mmio access */
516df10a82SMark Nutter 	struct address_space *local_store; /* local store mapping.  */
526df10a82SMark Nutter 	struct address_space *mfc;	   /* 'mfc' area mappings. */
536df10a82SMark Nutter 	struct address_space *cntl; 	   /* 'control' area mappings. */
546df10a82SMark Nutter 	struct address_space *signal1; 	   /* 'signal1' area mappings. */
556df10a82SMark Nutter 	struct address_space *signal2; 	   /* 'signal2' area mappings. */
5617e0e270SBenjamin Herrenschmidt 	struct address_space *mss; 	   /* 'mss' area mappings. */
5717e0e270SBenjamin Herrenschmidt 	struct address_space *psmap; 	   /* 'psmap' area mappings. */
5886767277SArnd Bergmann 	u64 object_id;		   /* user space pointer for oprofile */
598b3d6663SArnd Bergmann 
608b3d6663SArnd Bergmann 	enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
61650f8b02SChristoph Hellwig 	struct mutex state_mutex;
625ef8224aSArnd Bergmann 	struct semaphore run_sema;
638b3d6663SArnd Bergmann 
648b3d6663SArnd Bergmann 	struct mm_struct *owner;
6567207b96SArnd Bergmann 
6667207b96SArnd Bergmann 	struct kref kref;
678b3d6663SArnd Bergmann 	wait_queue_head_t ibox_wq;
688b3d6663SArnd Bergmann 	wait_queue_head_t wbox_wq;
695110459fSArnd Bergmann 	wait_queue_head_t stop_wq;
70a33a7d73SArnd Bergmann 	wait_queue_head_t mfc_wq;
718b3d6663SArnd Bergmann 	struct fasync_struct *ibox_fasync;
728b3d6663SArnd Bergmann 	struct fasync_struct *wbox_fasync;
73a33a7d73SArnd Bergmann 	struct fasync_struct *mfc_fasync;
74a33a7d73SArnd Bergmann 	u32 tagwait;
758b3d6663SArnd Bergmann 	struct spu_context_ops *ops;
762a911f0bSArnd Bergmann 	struct work_struct reap_work;
779add11daSArnd Bergmann 	unsigned long flags;
789add11daSArnd Bergmann 	unsigned long event_return;
796263203eSArnd Bergmann 
806263203eSArnd Bergmann 	struct list_head gang_list;
816263203eSArnd Bergmann 	struct spu_gang *gang;
828389998aSChristoph Hellwig 
838389998aSChristoph Hellwig 	/* scheduler fields */
84079cdb61SChristoph Hellwig  	struct list_head rq;
8526bec673SChristoph Hellwig 	unsigned long sched_flags;
868389998aSChristoph Hellwig 	int prio;
876263203eSArnd Bergmann };
886263203eSArnd Bergmann 
896263203eSArnd Bergmann struct spu_gang {
906263203eSArnd Bergmann 	struct list_head list;
916263203eSArnd Bergmann 	struct mutex mutex;
926263203eSArnd Bergmann 	struct kref kref;
936263203eSArnd Bergmann 	int contexts;
9467207b96SArnd Bergmann };
9567207b96SArnd Bergmann 
96a33a7d73SArnd Bergmann struct mfc_dma_command {
97a33a7d73SArnd Bergmann 	int32_t pad;	/* reserved */
98a33a7d73SArnd Bergmann 	uint32_t lsa;	/* local storage address */
99a33a7d73SArnd Bergmann 	uint64_t ea;	/* effective address */
100a33a7d73SArnd Bergmann 	uint16_t size;	/* transfer size */
101a33a7d73SArnd Bergmann 	uint16_t tag;	/* command tag */
102a33a7d73SArnd Bergmann 	uint16_t class;	/* class ID */
103a33a7d73SArnd Bergmann 	uint16_t cmd;	/* command opcode */
104a33a7d73SArnd Bergmann };
105a33a7d73SArnd Bergmann 
106a33a7d73SArnd Bergmann 
1078b3d6663SArnd Bergmann /* SPU context query/set operations. */
1088b3d6663SArnd Bergmann struct spu_context_ops {
1098b3d6663SArnd Bergmann 	int (*mbox_read) (struct spu_context * ctx, u32 * data);
1108b3d6663SArnd Bergmann 	 u32(*mbox_stat_read) (struct spu_context * ctx);
1113a843d7cSArnd Bergmann 	unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
1123a843d7cSArnd Bergmann 					unsigned int events);
1138b3d6663SArnd Bergmann 	int (*ibox_read) (struct spu_context * ctx, u32 * data);
1148b3d6663SArnd Bergmann 	int (*wbox_write) (struct spu_context * ctx, u32 data);
1158b3d6663SArnd Bergmann 	 u32(*signal1_read) (struct spu_context * ctx);
1168b3d6663SArnd Bergmann 	void (*signal1_write) (struct spu_context * ctx, u32 data);
1178b3d6663SArnd Bergmann 	 u32(*signal2_read) (struct spu_context * ctx);
1188b3d6663SArnd Bergmann 	void (*signal2_write) (struct spu_context * ctx, u32 data);
1198b3d6663SArnd Bergmann 	void (*signal1_type_set) (struct spu_context * ctx, u64 val);
1208b3d6663SArnd Bergmann 	 u64(*signal1_type_get) (struct spu_context * ctx);
1218b3d6663SArnd Bergmann 	void (*signal2_type_set) (struct spu_context * ctx, u64 val);
1228b3d6663SArnd Bergmann 	 u64(*signal2_type_get) (struct spu_context * ctx);
1238b3d6663SArnd Bergmann 	 u32(*npc_read) (struct spu_context * ctx);
1248b3d6663SArnd Bergmann 	void (*npc_write) (struct spu_context * ctx, u32 data);
1258b3d6663SArnd Bergmann 	 u32(*status_read) (struct spu_context * ctx);
1268b3d6663SArnd Bergmann 	char*(*get_ls) (struct spu_context * ctx);
1273960c260SJeremy Kerr 	 u32 (*runcntl_read) (struct spu_context * ctx);
1285110459fSArnd Bergmann 	void (*runcntl_write) (struct spu_context * ctx, u32 data);
129ee2d7340SArnd Bergmann 	void (*master_start) (struct spu_context * ctx);
130ee2d7340SArnd Bergmann 	void (*master_stop) (struct spu_context * ctx);
131a33a7d73SArnd Bergmann 	int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
132a33a7d73SArnd Bergmann 	u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
133a33a7d73SArnd Bergmann 	u32 (*get_mfc_free_elements)(struct spu_context *ctx);
134a33a7d73SArnd Bergmann 	int (*send_mfc_command)(struct spu_context * ctx,
135a33a7d73SArnd Bergmann 				struct mfc_dma_command * cmd);
136b9e3bd77SDwayne Grant McConnell 	void (*dma_info_read) (struct spu_context * ctx,
137b9e3bd77SDwayne Grant McConnell 			       struct spu_dma_info * info);
138b9e3bd77SDwayne Grant McConnell 	void (*proxydma_info_read) (struct spu_context * ctx,
139b9e3bd77SDwayne Grant McConnell 				    struct spu_proxydma_info * info);
1408b3d6663SArnd Bergmann };
1418b3d6663SArnd Bergmann 
1428b3d6663SArnd Bergmann extern struct spu_context_ops spu_hw_ops;
1438b3d6663SArnd Bergmann extern struct spu_context_ops spu_backing_ops;
1448b3d6663SArnd Bergmann 
14567207b96SArnd Bergmann struct spufs_inode_info {
14667207b96SArnd Bergmann 	struct spu_context *i_ctx;
1476263203eSArnd Bergmann 	struct spu_gang *i_gang;
14867207b96SArnd Bergmann 	struct inode vfs_inode;
14967207b96SArnd Bergmann };
15067207b96SArnd Bergmann #define SPUFS_I(inode) \
15167207b96SArnd Bergmann 	container_of(inode, struct spufs_inode_info, vfs_inode)
15267207b96SArnd Bergmann 
15367207b96SArnd Bergmann extern struct tree_descr spufs_dir_contents[];
1545737edd1SMark Nutter extern struct tree_descr spufs_dir_nosched_contents[];
15567207b96SArnd Bergmann 
15667207b96SArnd Bergmann /* system call implementation */
15767207b96SArnd Bergmann long spufs_run_spu(struct file *file,
15867207b96SArnd Bergmann 		   struct spu_context *ctx, u32 *npc, u32 *status);
1596263203eSArnd Bergmann long spufs_create(struct nameidata *nd,
16067207b96SArnd Bergmann 			 unsigned int flags, mode_t mode);
1619c2e08c5SArjan van de Ven extern const struct file_operations spufs_context_fops;
16267207b96SArnd Bergmann 
1636263203eSArnd Bergmann /* gang management */
1646263203eSArnd Bergmann struct spu_gang *alloc_spu_gang(void);
1656263203eSArnd Bergmann struct spu_gang *get_spu_gang(struct spu_gang *gang);
1666263203eSArnd Bergmann int put_spu_gang(struct spu_gang *gang);
1676263203eSArnd Bergmann void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
1686263203eSArnd Bergmann void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
1696263203eSArnd Bergmann 
17067207b96SArnd Bergmann /* context management */
1716a0641e5SChristoph Hellwig static inline void spu_acquire(struct spu_context *ctx)
1726a0641e5SChristoph Hellwig {
1736a0641e5SChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
1746a0641e5SChristoph Hellwig }
1756a0641e5SChristoph Hellwig 
1766a0641e5SChristoph Hellwig static inline void spu_release(struct spu_context *ctx)
1776a0641e5SChristoph Hellwig {
1786a0641e5SChristoph Hellwig 	mutex_unlock(&ctx->state_mutex);
1796a0641e5SChristoph Hellwig }
1806a0641e5SChristoph Hellwig 
1816263203eSArnd Bergmann struct spu_context * alloc_spu_context(struct spu_gang *gang);
18267207b96SArnd Bergmann void destroy_spu_context(struct kref *kref);
18367207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx);
18467207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx);
1855110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx);
18667207b96SArnd Bergmann 
1878b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx);
18826bec673SChristoph Hellwig int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags);
18967207b96SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx);
190099814bbSJeremy Kerr int spu_acquire_exclusive(struct spu_context *ctx);
19126bec673SChristoph Hellwig enum {
19226bec673SChristoph Hellwig 	SPU_ACTIVATE_NOWAKE = 1,
19326bec673SChristoph Hellwig };
19426bec673SChristoph Hellwig int spu_activate(struct spu_context *ctx, unsigned long flags);
1958b3d6663SArnd Bergmann void spu_deactivate(struct spu_context *ctx);
1968b3d6663SArnd Bergmann void spu_yield(struct spu_context *ctx);
1978b3d6663SArnd Bergmann int __init spu_sched_init(void);
1988b3d6663SArnd Bergmann void __exit spu_sched_exit(void);
1998b3d6663SArnd Bergmann 
200c6730ed4SJeremy Kerr extern char *isolated_loader;
201c6730ed4SJeremy Kerr 
202ce8ab854SArnd Bergmann /*
203ce8ab854SArnd Bergmann  * spufs_wait
204ce8ab854SArnd Bergmann  * 	Same as wait_event_interruptible(), except that here
205ce8ab854SArnd Bergmann  *	we need to call spu_release(ctx) before sleeping, and
206ce8ab854SArnd Bergmann  *	then spu_acquire(ctx) when awoken.
207ce8ab854SArnd Bergmann  */
208ce8ab854SArnd Bergmann 
209ce8ab854SArnd Bergmann #define spufs_wait(wq, condition)					\
210ce8ab854SArnd Bergmann ({									\
211ce8ab854SArnd Bergmann 	int __ret = 0;							\
212ce8ab854SArnd Bergmann 	DEFINE_WAIT(__wait);						\
213ce8ab854SArnd Bergmann 	for (;;) {							\
214ce8ab854SArnd Bergmann 		prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE);	\
215ce8ab854SArnd Bergmann 		if (condition)						\
216ce8ab854SArnd Bergmann 			break;						\
217ce8ab854SArnd Bergmann 		if (!signal_pending(current)) {				\
218ce8ab854SArnd Bergmann 			spu_release(ctx);				\
219ce8ab854SArnd Bergmann 			schedule();					\
220ce8ab854SArnd Bergmann 			spu_acquire(ctx);				\
221ce8ab854SArnd Bergmann 			continue;					\
222ce8ab854SArnd Bergmann 		}							\
223ce8ab854SArnd Bergmann 		__ret = -ERESTARTSYS;					\
224ce8ab854SArnd Bergmann 		break;							\
225ce8ab854SArnd Bergmann 	}								\
226ce8ab854SArnd Bergmann 	finish_wait(&(wq), &__wait);					\
227ce8ab854SArnd Bergmann 	__ret;								\
228ce8ab854SArnd Bergmann })
229ce8ab854SArnd Bergmann 
2308b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data);
2318b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
2328b3d6663SArnd Bergmann 
2338b3d6663SArnd Bergmann /* irq callback funcs. */
2348b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu);
2358b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu);
2365110459fSArnd Bergmann void spufs_stop_callback(struct spu *spu);
237a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu);
2389add11daSArnd Bergmann void spufs_dma_callback(struct spu *spu, int type);
2398b3d6663SArnd Bergmann 
240bf1ab978SDwayne Grant McConnell extern struct spu_coredump_calls spufs_coredump_calls;
241bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader {
242bf1ab978SDwayne Grant McConnell 	char *name;
243bf1ab978SDwayne Grant McConnell 	ssize_t (*read)(struct spu_context *ctx,
244bf1ab978SDwayne Grant McConnell 			char __user *buffer, size_t size, loff_t *pos);
245bf1ab978SDwayne Grant McConnell 	u64 (*get)(void *data);
246bf1ab978SDwayne Grant McConnell 	size_t size;
247bf1ab978SDwayne Grant McConnell };
248bf1ab978SDwayne Grant McConnell extern struct spufs_coredump_reader spufs_coredump_read[];
249bf1ab978SDwayne Grant McConnell extern int spufs_coredump_num_notes;
250bf1ab978SDwayne Grant McConnell 
25167207b96SArnd Bergmann #endif
252