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 4327b1ea09SChristoph Hellwig enum { 4427b1ea09SChristoph Hellwig SPU_SCHED_WAS_ACTIVE, /* was active upon spu_acquire_saved() */ 4527b1ea09SChristoph Hellwig }; 4627b1ea09SChristoph 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. */ 5847d3a5faSChristoph Hellwig struct mutex 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; 63e45d48a3SChristoph Hellwig struct mutex run_mutex; 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 84476273adSChristoph Hellwig /* owner thread */ 85476273adSChristoph Hellwig pid_t tid; 86476273adSChristoph Hellwig 878389998aSChristoph Hellwig /* scheduler fields */ 88079cdb61SChristoph Hellwig struct list_head rq; 8937901802SChristoph Hellwig unsigned int time_slice; 9026bec673SChristoph Hellwig unsigned long sched_flags; 91ea1ae594SChristoph Hellwig cpumask_t cpus_allowed; 922eb1b120SChristoph Hellwig int policy; 938389998aSChristoph Hellwig int prio; 94e9f8a0b6SChristoph Hellwig 95e9f8a0b6SChristoph Hellwig /* statistics */ 96e9f8a0b6SChristoph Hellwig struct { 97e9f8a0b6SChristoph Hellwig /* updates protected by ctx->state_mutex */ 9827ec41d3SAndre Detsch enum spu_utilization_state util_state; 9927ec41d3SAndre Detsch unsigned long long tstamp; /* time of last state switch */ 10027ec41d3SAndre Detsch unsigned long long times[SPU_UTIL_MAX]; 101e9f8a0b6SChristoph Hellwig unsigned long long vol_ctx_switch; 102e9f8a0b6SChristoph Hellwig unsigned long long invol_ctx_switch; 103e9f8a0b6SChristoph Hellwig unsigned long long min_flt; 104e9f8a0b6SChristoph Hellwig unsigned long long maj_flt; 105e9f8a0b6SChristoph Hellwig unsigned long long hash_flt; 106e9f8a0b6SChristoph Hellwig unsigned long long slb_flt; 107e9f8a0b6SChristoph Hellwig unsigned long long slb_flt_base; /* # at last ctx switch */ 108e9f8a0b6SChristoph Hellwig unsigned long long class2_intr; 109e9f8a0b6SChristoph Hellwig unsigned long long class2_intr_base; /* # at last ctx switch */ 110e9f8a0b6SChristoph Hellwig unsigned long long libassist; 111e9f8a0b6SChristoph Hellwig } stats; 1126263203eSArnd Bergmann }; 1136263203eSArnd Bergmann 1146263203eSArnd Bergmann struct spu_gang { 1156263203eSArnd Bergmann struct list_head list; 1166263203eSArnd Bergmann struct mutex mutex; 1176263203eSArnd Bergmann struct kref kref; 1186263203eSArnd Bergmann int contexts; 11967207b96SArnd Bergmann }; 12067207b96SArnd Bergmann 121a33a7d73SArnd Bergmann struct mfc_dma_command { 122a33a7d73SArnd Bergmann int32_t pad; /* reserved */ 123a33a7d73SArnd Bergmann uint32_t lsa; /* local storage address */ 124a33a7d73SArnd Bergmann uint64_t ea; /* effective address */ 125a33a7d73SArnd Bergmann uint16_t size; /* transfer size */ 126a33a7d73SArnd Bergmann uint16_t tag; /* command tag */ 127a33a7d73SArnd Bergmann uint16_t class; /* class ID */ 128a33a7d73SArnd Bergmann uint16_t cmd; /* command opcode */ 129a33a7d73SArnd Bergmann }; 130a33a7d73SArnd Bergmann 131a33a7d73SArnd Bergmann 1328b3d6663SArnd Bergmann /* SPU context query/set operations. */ 1338b3d6663SArnd Bergmann struct spu_context_ops { 1348b3d6663SArnd Bergmann int (*mbox_read) (struct spu_context * ctx, u32 * data); 1358b3d6663SArnd Bergmann u32(*mbox_stat_read) (struct spu_context * ctx); 1363a843d7cSArnd Bergmann unsigned int (*mbox_stat_poll)(struct spu_context *ctx, 1373a843d7cSArnd Bergmann unsigned int events); 1388b3d6663SArnd Bergmann int (*ibox_read) (struct spu_context * ctx, u32 * data); 1398b3d6663SArnd Bergmann int (*wbox_write) (struct spu_context * ctx, u32 data); 1408b3d6663SArnd Bergmann u32(*signal1_read) (struct spu_context * ctx); 1418b3d6663SArnd Bergmann void (*signal1_write) (struct spu_context * ctx, u32 data); 1428b3d6663SArnd Bergmann u32(*signal2_read) (struct spu_context * ctx); 1438b3d6663SArnd Bergmann void (*signal2_write) (struct spu_context * ctx, u32 data); 1448b3d6663SArnd Bergmann void (*signal1_type_set) (struct spu_context * ctx, u64 val); 1458b3d6663SArnd Bergmann u64(*signal1_type_get) (struct spu_context * ctx); 1468b3d6663SArnd Bergmann void (*signal2_type_set) (struct spu_context * ctx, u64 val); 1478b3d6663SArnd Bergmann u64(*signal2_type_get) (struct spu_context * ctx); 1488b3d6663SArnd Bergmann u32(*npc_read) (struct spu_context * ctx); 1498b3d6663SArnd Bergmann void (*npc_write) (struct spu_context * ctx, u32 data); 1508b3d6663SArnd Bergmann u32(*status_read) (struct spu_context * ctx); 1518b3d6663SArnd Bergmann char*(*get_ls) (struct spu_context * ctx); 1523960c260SJeremy Kerr u32 (*runcntl_read) (struct spu_context * ctx); 1535110459fSArnd Bergmann void (*runcntl_write) (struct spu_context * ctx, u32 data); 154ee2d7340SArnd Bergmann void (*master_start) (struct spu_context * ctx); 155ee2d7340SArnd Bergmann void (*master_stop) (struct spu_context * ctx); 156a33a7d73SArnd Bergmann int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode); 157a33a7d73SArnd Bergmann u32 (*read_mfc_tagstatus)(struct spu_context * ctx); 158a33a7d73SArnd Bergmann u32 (*get_mfc_free_elements)(struct spu_context *ctx); 159a33a7d73SArnd Bergmann int (*send_mfc_command)(struct spu_context * ctx, 160a33a7d73SArnd Bergmann struct mfc_dma_command * cmd); 161b9e3bd77SDwayne Grant McConnell void (*dma_info_read) (struct spu_context * ctx, 162b9e3bd77SDwayne Grant McConnell struct spu_dma_info * info); 163b9e3bd77SDwayne Grant McConnell void (*proxydma_info_read) (struct spu_context * ctx, 164b9e3bd77SDwayne Grant McConnell struct spu_proxydma_info * info); 16557dace23SArnd Bergmann void (*restart_dma)(struct spu_context *ctx); 1668b3d6663SArnd Bergmann }; 1678b3d6663SArnd Bergmann 1688b3d6663SArnd Bergmann extern struct spu_context_ops spu_hw_ops; 1698b3d6663SArnd Bergmann extern struct spu_context_ops spu_backing_ops; 1708b3d6663SArnd Bergmann 17167207b96SArnd Bergmann struct spufs_inode_info { 17267207b96SArnd Bergmann struct spu_context *i_ctx; 1736263203eSArnd Bergmann struct spu_gang *i_gang; 17467207b96SArnd Bergmann struct inode vfs_inode; 17543c2bbd9SChristoph Hellwig int i_openers; 17667207b96SArnd Bergmann }; 17767207b96SArnd Bergmann #define SPUFS_I(inode) \ 17867207b96SArnd Bergmann container_of(inode, struct spufs_inode_info, vfs_inode) 17967207b96SArnd Bergmann 18067207b96SArnd Bergmann extern struct tree_descr spufs_dir_contents[]; 1815737edd1SMark Nutter extern struct tree_descr spufs_dir_nosched_contents[]; 18267207b96SArnd Bergmann 18367207b96SArnd Bergmann /* system call implementation */ 18450af32a9SJeremy Kerr long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status); 1856263203eSArnd Bergmann long spufs_create(struct nameidata *nd, 18667207b96SArnd Bergmann unsigned int flags, mode_t mode); 1879c2e08c5SArjan van de Ven extern const struct file_operations spufs_context_fops; 18867207b96SArnd Bergmann 1896263203eSArnd Bergmann /* gang management */ 1906263203eSArnd Bergmann struct spu_gang *alloc_spu_gang(void); 1916263203eSArnd Bergmann struct spu_gang *get_spu_gang(struct spu_gang *gang); 1926263203eSArnd Bergmann int put_spu_gang(struct spu_gang *gang); 1936263203eSArnd Bergmann void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx); 1946263203eSArnd Bergmann void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx); 1956263203eSArnd Bergmann 19657dace23SArnd Bergmann /* fault handling */ 19757dace23SArnd Bergmann int spufs_handle_class1(struct spu_context *ctx); 19857dace23SArnd Bergmann 19967207b96SArnd Bergmann /* context management */ 20065de66f0SChristoph Hellwig extern atomic_t nr_spu_contexts; 2016a0641e5SChristoph Hellwig static inline void spu_acquire(struct spu_context *ctx) 2026a0641e5SChristoph Hellwig { 2036a0641e5SChristoph Hellwig mutex_lock(&ctx->state_mutex); 2046a0641e5SChristoph Hellwig } 2056a0641e5SChristoph Hellwig 2066a0641e5SChristoph Hellwig static inline void spu_release(struct spu_context *ctx) 2076a0641e5SChristoph Hellwig { 2086a0641e5SChristoph Hellwig mutex_unlock(&ctx->state_mutex); 2096a0641e5SChristoph Hellwig } 2106a0641e5SChristoph Hellwig 2116263203eSArnd Bergmann struct spu_context * alloc_spu_context(struct spu_gang *gang); 21267207b96SArnd Bergmann void destroy_spu_context(struct kref *kref); 21367207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx); 21467207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx); 2155110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx); 21667207b96SArnd Bergmann 2178b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx); 21826bec673SChristoph Hellwig int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags); 21967207b96SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx); 22027b1ea09SChristoph Hellwig void spu_release_saved(struct spu_context *ctx); 22150b520d4SChristoph Hellwig 22226bec673SChristoph Hellwig int spu_activate(struct spu_context *ctx, unsigned long flags); 2238b3d6663SArnd Bergmann void spu_deactivate(struct spu_context *ctx); 2248b3d6663SArnd Bergmann void spu_yield(struct spu_context *ctx); 225fe443ef2SChristoph Hellwig void spu_set_timeslice(struct spu_context *ctx); 2262cf2b3b4SChristoph Hellwig void spu_update_sched_info(struct spu_context *ctx); 2272cf2b3b4SChristoph Hellwig void __spu_update_sched_info(struct spu_context *ctx); 2288b3d6663SArnd Bergmann int __init spu_sched_init(void); 229d1450317SSebastian Siewior void spu_sched_exit(void); 2308b3d6663SArnd Bergmann 231c6730ed4SJeremy Kerr extern char *isolated_loader; 232c6730ed4SJeremy Kerr 233ce8ab854SArnd Bergmann /* 234ce8ab854SArnd Bergmann * spufs_wait 235ce8ab854SArnd Bergmann * Same as wait_event_interruptible(), except that here 236ce8ab854SArnd Bergmann * we need to call spu_release(ctx) before sleeping, and 237ce8ab854SArnd Bergmann * then spu_acquire(ctx) when awoken. 238ce8ab854SArnd Bergmann */ 239ce8ab854SArnd Bergmann 240ce8ab854SArnd Bergmann #define spufs_wait(wq, condition) \ 241ce8ab854SArnd Bergmann ({ \ 242ce8ab854SArnd Bergmann int __ret = 0; \ 243ce8ab854SArnd Bergmann DEFINE_WAIT(__wait); \ 244ce8ab854SArnd Bergmann for (;;) { \ 245ce8ab854SArnd Bergmann prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \ 246ce8ab854SArnd Bergmann if (condition) \ 247ce8ab854SArnd Bergmann break; \ 248d3764397SJeremy Kerr if (signal_pending(current)) { \ 249d3764397SJeremy Kerr __ret = -ERESTARTSYS; \ 250d3764397SJeremy Kerr break; \ 251d3764397SJeremy Kerr } \ 252ce8ab854SArnd Bergmann spu_release(ctx); \ 253ce8ab854SArnd Bergmann schedule(); \ 254ce8ab854SArnd Bergmann spu_acquire(ctx); \ 255ce8ab854SArnd Bergmann } \ 256ce8ab854SArnd Bergmann finish_wait(&(wq), &__wait); \ 257ce8ab854SArnd Bergmann __ret; \ 258ce8ab854SArnd Bergmann }) 259ce8ab854SArnd Bergmann 2608b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data); 2618b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data); 2628b3d6663SArnd Bergmann 2638b3d6663SArnd Bergmann /* irq callback funcs. */ 2648b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu); 2658b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu); 2665110459fSArnd Bergmann void spufs_stop_callback(struct spu *spu); 267a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu); 2689add11daSArnd Bergmann void spufs_dma_callback(struct spu *spu, int type); 2698b3d6663SArnd Bergmann 270bf1ab978SDwayne Grant McConnell extern struct spu_coredump_calls spufs_coredump_calls; 271bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader { 272bf1ab978SDwayne Grant McConnell char *name; 273bf1ab978SDwayne Grant McConnell ssize_t (*read)(struct spu_context *ctx, 274bf1ab978SDwayne Grant McConnell char __user *buffer, size_t size, loff_t *pos); 275bf1ab978SDwayne Grant McConnell u64 (*get)(void *data); 276bf1ab978SDwayne Grant McConnell size_t size; 277bf1ab978SDwayne Grant McConnell }; 278bf1ab978SDwayne Grant McConnell extern struct spufs_coredump_reader spufs_coredump_read[]; 279bf1ab978SDwayne Grant McConnell extern int spufs_coredump_num_notes; 280bf1ab978SDwayne Grant McConnell 281e9f8a0b6SChristoph Hellwig /* 282e9f8a0b6SChristoph Hellwig * This function is a little bit too large for an inline, but 283e9f8a0b6SChristoph Hellwig * as fault.c is built into the kernel we can't move it out of 284e9f8a0b6SChristoph Hellwig * line. 285e9f8a0b6SChristoph Hellwig */ 286e9f8a0b6SChristoph Hellwig static inline void spuctx_switch_state(struct spu_context *ctx, 28727ec41d3SAndre Detsch enum spu_utilization_state new_state) 288e9f8a0b6SChristoph Hellwig { 28927ec41d3SAndre Detsch unsigned long long curtime; 29027ec41d3SAndre Detsch signed long long delta; 29127ec41d3SAndre Detsch struct timespec ts; 29227ec41d3SAndre Detsch struct spu *spu; 29327ec41d3SAndre Detsch enum spu_utilization_state old_state; 29427ec41d3SAndre Detsch 29527ec41d3SAndre Detsch ktime_get_ts(&ts); 29627ec41d3SAndre Detsch curtime = timespec_to_ns(&ts); 29727ec41d3SAndre Detsch delta = curtime - ctx->stats.tstamp; 29827ec41d3SAndre Detsch 299e9f8a0b6SChristoph Hellwig WARN_ON(!mutex_is_locked(&ctx->state_mutex)); 30027ec41d3SAndre Detsch WARN_ON(delta < 0); 301e9f8a0b6SChristoph Hellwig 30227ec41d3SAndre Detsch spu = ctx->spu; 30327ec41d3SAndre Detsch old_state = ctx->stats.util_state; 30427ec41d3SAndre Detsch ctx->stats.util_state = new_state; 305e9f8a0b6SChristoph Hellwig ctx->stats.tstamp = curtime; 306e9f8a0b6SChristoph Hellwig 30727ec41d3SAndre Detsch /* 30827ec41d3SAndre Detsch * Update the physical SPU utilization statistics. 30927ec41d3SAndre Detsch */ 31027ec41d3SAndre Detsch if (spu) { 31127ec41d3SAndre Detsch ctx->stats.times[old_state] += delta; 31227ec41d3SAndre Detsch spu->stats.times[old_state] += delta; 31327ec41d3SAndre Detsch spu->stats.util_state = new_state; 314fe2f896dSChristoph Hellwig spu->stats.tstamp = curtime; 315fe2f896dSChristoph Hellwig } 316fe2f896dSChristoph Hellwig } 317fe2f896dSChristoph Hellwig 31867207b96SArnd Bergmann #endif 319