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 {
4408873095SChristoph Hellwig 	SPU_SCHED_EXITING = 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. */
5843c2bbd9SChristoph Hellwig 	spinlock_t mapping_lock;
5986767277SArnd Bergmann 	u64 object_id;		   /* user space pointer for oprofile */
608b3d6663SArnd Bergmann 
618b3d6663SArnd Bergmann 	enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
62650f8b02SChristoph Hellwig 	struct mutex state_mutex;
635ef8224aSArnd Bergmann 	struct semaphore run_sema;
648b3d6663SArnd Bergmann 
658b3d6663SArnd Bergmann 	struct mm_struct *owner;
6667207b96SArnd Bergmann 
6767207b96SArnd Bergmann 	struct kref kref;
688b3d6663SArnd Bergmann 	wait_queue_head_t ibox_wq;
698b3d6663SArnd Bergmann 	wait_queue_head_t wbox_wq;
705110459fSArnd Bergmann 	wait_queue_head_t stop_wq;
71a33a7d73SArnd Bergmann 	wait_queue_head_t mfc_wq;
728b3d6663SArnd Bergmann 	struct fasync_struct *ibox_fasync;
738b3d6663SArnd Bergmann 	struct fasync_struct *wbox_fasync;
74a33a7d73SArnd Bergmann 	struct fasync_struct *mfc_fasync;
75a33a7d73SArnd Bergmann 	u32 tagwait;
768b3d6663SArnd Bergmann 	struct spu_context_ops *ops;
772a911f0bSArnd Bergmann 	struct work_struct reap_work;
789add11daSArnd Bergmann 	unsigned long flags;
799add11daSArnd Bergmann 	unsigned long event_return;
806263203eSArnd Bergmann 
816263203eSArnd Bergmann 	struct list_head gang_list;
826263203eSArnd Bergmann 	struct spu_gang *gang;
838389998aSChristoph Hellwig 
848389998aSChristoph Hellwig 	/* scheduler fields */
85079cdb61SChristoph Hellwig  	struct list_head rq;
862eb1b120SChristoph Hellwig 	struct delayed_work sched_work;
8726bec673SChristoph Hellwig 	unsigned long sched_flags;
8852f04fcfSChristoph Hellwig 	unsigned long rt_priority;
892eb1b120SChristoph Hellwig 	int policy;
908389998aSChristoph Hellwig 	int prio;
916263203eSArnd Bergmann };
926263203eSArnd Bergmann 
936263203eSArnd Bergmann struct spu_gang {
946263203eSArnd Bergmann 	struct list_head list;
956263203eSArnd Bergmann 	struct mutex mutex;
966263203eSArnd Bergmann 	struct kref kref;
976263203eSArnd Bergmann 	int contexts;
9867207b96SArnd Bergmann };
9967207b96SArnd Bergmann 
100a33a7d73SArnd Bergmann struct mfc_dma_command {
101a33a7d73SArnd Bergmann 	int32_t pad;	/* reserved */
102a33a7d73SArnd Bergmann 	uint32_t lsa;	/* local storage address */
103a33a7d73SArnd Bergmann 	uint64_t ea;	/* effective address */
104a33a7d73SArnd Bergmann 	uint16_t size;	/* transfer size */
105a33a7d73SArnd Bergmann 	uint16_t tag;	/* command tag */
106a33a7d73SArnd Bergmann 	uint16_t class;	/* class ID */
107a33a7d73SArnd Bergmann 	uint16_t cmd;	/* command opcode */
108a33a7d73SArnd Bergmann };
109a33a7d73SArnd Bergmann 
110a33a7d73SArnd Bergmann 
1118b3d6663SArnd Bergmann /* SPU context query/set operations. */
1128b3d6663SArnd Bergmann struct spu_context_ops {
1138b3d6663SArnd Bergmann 	int (*mbox_read) (struct spu_context * ctx, u32 * data);
1148b3d6663SArnd Bergmann 	 u32(*mbox_stat_read) (struct spu_context * ctx);
1153a843d7cSArnd Bergmann 	unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
1163a843d7cSArnd Bergmann 					unsigned int events);
1178b3d6663SArnd Bergmann 	int (*ibox_read) (struct spu_context * ctx, u32 * data);
1188b3d6663SArnd Bergmann 	int (*wbox_write) (struct spu_context * ctx, u32 data);
1198b3d6663SArnd Bergmann 	 u32(*signal1_read) (struct spu_context * ctx);
1208b3d6663SArnd Bergmann 	void (*signal1_write) (struct spu_context * ctx, u32 data);
1218b3d6663SArnd Bergmann 	 u32(*signal2_read) (struct spu_context * ctx);
1228b3d6663SArnd Bergmann 	void (*signal2_write) (struct spu_context * ctx, u32 data);
1238b3d6663SArnd Bergmann 	void (*signal1_type_set) (struct spu_context * ctx, u64 val);
1248b3d6663SArnd Bergmann 	 u64(*signal1_type_get) (struct spu_context * ctx);
1258b3d6663SArnd Bergmann 	void (*signal2_type_set) (struct spu_context * ctx, u64 val);
1268b3d6663SArnd Bergmann 	 u64(*signal2_type_get) (struct spu_context * ctx);
1278b3d6663SArnd Bergmann 	 u32(*npc_read) (struct spu_context * ctx);
1288b3d6663SArnd Bergmann 	void (*npc_write) (struct spu_context * ctx, u32 data);
1298b3d6663SArnd Bergmann 	 u32(*status_read) (struct spu_context * ctx);
1308b3d6663SArnd Bergmann 	char*(*get_ls) (struct spu_context * ctx);
1313960c260SJeremy Kerr 	 u32 (*runcntl_read) (struct spu_context * ctx);
1325110459fSArnd Bergmann 	void (*runcntl_write) (struct spu_context * ctx, u32 data);
133ee2d7340SArnd Bergmann 	void (*master_start) (struct spu_context * ctx);
134ee2d7340SArnd Bergmann 	void (*master_stop) (struct spu_context * ctx);
135a33a7d73SArnd Bergmann 	int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
136a33a7d73SArnd Bergmann 	u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
137a33a7d73SArnd Bergmann 	u32 (*get_mfc_free_elements)(struct spu_context *ctx);
138a33a7d73SArnd Bergmann 	int (*send_mfc_command)(struct spu_context * ctx,
139a33a7d73SArnd Bergmann 				struct mfc_dma_command * cmd);
140b9e3bd77SDwayne Grant McConnell 	void (*dma_info_read) (struct spu_context * ctx,
141b9e3bd77SDwayne Grant McConnell 			       struct spu_dma_info * info);
142b9e3bd77SDwayne Grant McConnell 	void (*proxydma_info_read) (struct spu_context * ctx,
143b9e3bd77SDwayne Grant McConnell 				    struct spu_proxydma_info * info);
14457dace23SArnd Bergmann 	void (*restart_dma)(struct spu_context *ctx);
1458b3d6663SArnd Bergmann };
1468b3d6663SArnd Bergmann 
1478b3d6663SArnd Bergmann extern struct spu_context_ops spu_hw_ops;
1488b3d6663SArnd Bergmann extern struct spu_context_ops spu_backing_ops;
1498b3d6663SArnd Bergmann 
15067207b96SArnd Bergmann struct spufs_inode_info {
15167207b96SArnd Bergmann 	struct spu_context *i_ctx;
1526263203eSArnd Bergmann 	struct spu_gang *i_gang;
15367207b96SArnd Bergmann 	struct inode vfs_inode;
15443c2bbd9SChristoph Hellwig 	int i_openers;
15567207b96SArnd Bergmann };
15667207b96SArnd Bergmann #define SPUFS_I(inode) \
15767207b96SArnd Bergmann 	container_of(inode, struct spufs_inode_info, vfs_inode)
15867207b96SArnd Bergmann 
15967207b96SArnd Bergmann extern struct tree_descr spufs_dir_contents[];
1605737edd1SMark Nutter extern struct tree_descr spufs_dir_nosched_contents[];
16167207b96SArnd Bergmann 
16267207b96SArnd Bergmann /* system call implementation */
16367207b96SArnd Bergmann long spufs_run_spu(struct file *file,
16467207b96SArnd Bergmann 		   struct spu_context *ctx, u32 *npc, u32 *status);
1656263203eSArnd Bergmann long spufs_create(struct nameidata *nd,
16667207b96SArnd Bergmann 			 unsigned int flags, mode_t mode);
1679c2e08c5SArjan van de Ven extern const struct file_operations spufs_context_fops;
16867207b96SArnd Bergmann 
1696263203eSArnd Bergmann /* gang management */
1706263203eSArnd Bergmann struct spu_gang *alloc_spu_gang(void);
1716263203eSArnd Bergmann struct spu_gang *get_spu_gang(struct spu_gang *gang);
1726263203eSArnd Bergmann int put_spu_gang(struct spu_gang *gang);
1736263203eSArnd Bergmann void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
1746263203eSArnd Bergmann void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
1756263203eSArnd Bergmann 
17657dace23SArnd Bergmann /* fault handling */
17757dace23SArnd Bergmann int spufs_handle_class1(struct spu_context *ctx);
17857dace23SArnd Bergmann 
17967207b96SArnd Bergmann /* context management */
1806a0641e5SChristoph Hellwig static inline void spu_acquire(struct spu_context *ctx)
1816a0641e5SChristoph Hellwig {
1826a0641e5SChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
1836a0641e5SChristoph Hellwig }
1846a0641e5SChristoph Hellwig 
1856a0641e5SChristoph Hellwig static inline void spu_release(struct spu_context *ctx)
1866a0641e5SChristoph Hellwig {
1876a0641e5SChristoph Hellwig 	mutex_unlock(&ctx->state_mutex);
1886a0641e5SChristoph Hellwig }
1896a0641e5SChristoph Hellwig 
1906263203eSArnd Bergmann struct spu_context * alloc_spu_context(struct spu_gang *gang);
19167207b96SArnd Bergmann void destroy_spu_context(struct kref *kref);
19267207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx);
19367207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx);
1945110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx);
19567207b96SArnd Bergmann 
1968b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx);
19726bec673SChristoph Hellwig int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags);
19867207b96SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx);
19950b520d4SChristoph Hellwig 
20026bec673SChristoph Hellwig int spu_activate(struct spu_context *ctx, unsigned long flags);
2018b3d6663SArnd Bergmann void spu_deactivate(struct spu_context *ctx);
2028b3d6663SArnd Bergmann void spu_yield(struct spu_context *ctx);
2032eb1b120SChristoph Hellwig void spu_start_tick(struct spu_context *ctx);
2042eb1b120SChristoph Hellwig void spu_stop_tick(struct spu_context *ctx);
2052eb1b120SChristoph Hellwig void spu_sched_tick(struct work_struct *work);
2068b3d6663SArnd Bergmann int __init spu_sched_init(void);
2078b3d6663SArnd Bergmann void __exit spu_sched_exit(void);
2088b3d6663SArnd Bergmann 
209c6730ed4SJeremy Kerr extern char *isolated_loader;
210c6730ed4SJeremy Kerr 
211ce8ab854SArnd Bergmann /*
212ce8ab854SArnd Bergmann  * spufs_wait
213ce8ab854SArnd Bergmann  * 	Same as wait_event_interruptible(), except that here
214ce8ab854SArnd Bergmann  *	we need to call spu_release(ctx) before sleeping, and
215ce8ab854SArnd Bergmann  *	then spu_acquire(ctx) when awoken.
216ce8ab854SArnd Bergmann  */
217ce8ab854SArnd Bergmann 
218ce8ab854SArnd Bergmann #define spufs_wait(wq, condition)					\
219ce8ab854SArnd Bergmann ({									\
220ce8ab854SArnd Bergmann 	int __ret = 0;							\
221ce8ab854SArnd Bergmann 	DEFINE_WAIT(__wait);						\
222ce8ab854SArnd Bergmann 	for (;;) {							\
223ce8ab854SArnd Bergmann 		prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE);	\
224ce8ab854SArnd Bergmann 		if (condition)						\
225ce8ab854SArnd Bergmann 			break;						\
226ce8ab854SArnd Bergmann 		if (!signal_pending(current)) {				\
227ce8ab854SArnd Bergmann 			spu_release(ctx);				\
228ce8ab854SArnd Bergmann 			schedule();					\
229ce8ab854SArnd Bergmann 			spu_acquire(ctx);				\
230ce8ab854SArnd Bergmann 			continue;					\
231ce8ab854SArnd Bergmann 		}							\
232ce8ab854SArnd Bergmann 		__ret = -ERESTARTSYS;					\
233ce8ab854SArnd Bergmann 		break;							\
234ce8ab854SArnd Bergmann 	}								\
235ce8ab854SArnd Bergmann 	finish_wait(&(wq), &__wait);					\
236ce8ab854SArnd Bergmann 	__ret;								\
237ce8ab854SArnd Bergmann })
238ce8ab854SArnd Bergmann 
2398b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data);
2408b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
2418b3d6663SArnd Bergmann 
2428b3d6663SArnd Bergmann /* irq callback funcs. */
2438b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu);
2448b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu);
2455110459fSArnd Bergmann void spufs_stop_callback(struct spu *spu);
246a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu);
2479add11daSArnd Bergmann void spufs_dma_callback(struct spu *spu, int type);
2488b3d6663SArnd Bergmann 
249bf1ab978SDwayne Grant McConnell extern struct spu_coredump_calls spufs_coredump_calls;
250bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader {
251bf1ab978SDwayne Grant McConnell 	char *name;
252bf1ab978SDwayne Grant McConnell 	ssize_t (*read)(struct spu_context *ctx,
253bf1ab978SDwayne Grant McConnell 			char __user *buffer, size_t size, loff_t *pos);
254bf1ab978SDwayne Grant McConnell 	u64 (*get)(void *data);
255bf1ab978SDwayne Grant McConnell 	size_t size;
256bf1ab978SDwayne Grant McConnell };
257bf1ab978SDwayne Grant McConnell extern struct spufs_coredump_reader spufs_coredump_read[];
258bf1ab978SDwayne Grant McConnell extern int spufs_coredump_num_notes;
259bf1ab978SDwayne Grant McConnell 
26067207b96SArnd Bergmann #endif
261