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 4736aaccc1SBob Nelson /* ctx->sched_flags */ 4836aaccc1SBob Nelson enum { 4936aaccc1SBob Nelson SPU_SCHED_NOTIFY_ACTIVE, 5036aaccc1SBob Nelson }; 5136aaccc1SBob Nelson 5267207b96SArnd Bergmann struct spu_context { 5367207b96SArnd Bergmann struct spu *spu; /* pointer to a physical SPU */ 545473af04SMark Nutter struct spu_state csa; /* SPU context save area. */ 5567207b96SArnd Bergmann spinlock_t mmio_lock; /* protects mmio access */ 566df10a82SMark Nutter struct address_space *local_store; /* local store mapping. */ 576df10a82SMark Nutter struct address_space *mfc; /* 'mfc' area mappings. */ 586df10a82SMark Nutter struct address_space *cntl; /* 'control' area mappings. */ 596df10a82SMark Nutter struct address_space *signal1; /* 'signal1' area mappings. */ 606df10a82SMark Nutter struct address_space *signal2; /* 'signal2' area mappings. */ 6117e0e270SBenjamin Herrenschmidt struct address_space *mss; /* 'mss' area mappings. */ 6217e0e270SBenjamin Herrenschmidt struct address_space *psmap; /* 'psmap' area mappings. */ 6347d3a5faSChristoph Hellwig struct mutex mapping_lock; 6486767277SArnd Bergmann u64 object_id; /* user space pointer for oprofile */ 658b3d6663SArnd Bergmann 668b3d6663SArnd Bergmann enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state; 67650f8b02SChristoph Hellwig struct mutex state_mutex; 68e45d48a3SChristoph Hellwig struct mutex run_mutex; 698b3d6663SArnd Bergmann 708b3d6663SArnd Bergmann struct mm_struct *owner; 7167207b96SArnd Bergmann 7267207b96SArnd Bergmann struct kref kref; 738b3d6663SArnd Bergmann wait_queue_head_t ibox_wq; 748b3d6663SArnd Bergmann wait_queue_head_t wbox_wq; 755110459fSArnd Bergmann wait_queue_head_t stop_wq; 76a33a7d73SArnd Bergmann wait_queue_head_t mfc_wq; 778b3d6663SArnd Bergmann struct fasync_struct *ibox_fasync; 788b3d6663SArnd Bergmann struct fasync_struct *wbox_fasync; 79a33a7d73SArnd Bergmann struct fasync_struct *mfc_fasync; 80a33a7d73SArnd Bergmann u32 tagwait; 818b3d6663SArnd Bergmann struct spu_context_ops *ops; 822a911f0bSArnd Bergmann struct work_struct reap_work; 839add11daSArnd Bergmann unsigned long flags; 849add11daSArnd Bergmann unsigned long event_return; 856263203eSArnd Bergmann 866263203eSArnd Bergmann struct list_head gang_list; 876263203eSArnd Bergmann struct spu_gang *gang; 881474855dSBob Nelson struct kref *prof_priv_kref; 891474855dSBob Nelson void ( * prof_priv_release) (struct kref *kref); 908389998aSChristoph Hellwig 91476273adSChristoph Hellwig /* owner thread */ 92476273adSChristoph Hellwig pid_t tid; 93476273adSChristoph Hellwig 948389998aSChristoph Hellwig /* scheduler fields */ 95079cdb61SChristoph Hellwig struct list_head rq; 9637901802SChristoph Hellwig unsigned int time_slice; 9726bec673SChristoph Hellwig unsigned long sched_flags; 98ea1ae594SChristoph Hellwig cpumask_t cpus_allowed; 992eb1b120SChristoph Hellwig int policy; 1008389998aSChristoph Hellwig int prio; 101e9f8a0b6SChristoph Hellwig 102e9f8a0b6SChristoph Hellwig /* statistics */ 103e9f8a0b6SChristoph Hellwig struct { 104e9f8a0b6SChristoph Hellwig /* updates protected by ctx->state_mutex */ 10527ec41d3SAndre Detsch enum spu_utilization_state util_state; 10627ec41d3SAndre Detsch unsigned long long tstamp; /* time of last state switch */ 10727ec41d3SAndre Detsch unsigned long long times[SPU_UTIL_MAX]; 108e9f8a0b6SChristoph Hellwig unsigned long long vol_ctx_switch; 109e9f8a0b6SChristoph Hellwig unsigned long long invol_ctx_switch; 110e9f8a0b6SChristoph Hellwig unsigned long long min_flt; 111e9f8a0b6SChristoph Hellwig unsigned long long maj_flt; 112e9f8a0b6SChristoph Hellwig unsigned long long hash_flt; 113e9f8a0b6SChristoph Hellwig unsigned long long slb_flt; 114e9f8a0b6SChristoph Hellwig unsigned long long slb_flt_base; /* # at last ctx switch */ 115e9f8a0b6SChristoph Hellwig unsigned long long class2_intr; 116e9f8a0b6SChristoph Hellwig unsigned long long class2_intr_base; /* # at last ctx switch */ 117e9f8a0b6SChristoph Hellwig unsigned long long libassist; 118e9f8a0b6SChristoph Hellwig } stats; 1198e68e2f2SArnd Bergmann 1208e68e2f2SArnd Bergmann struct list_head aff_list; 1218e68e2f2SArnd Bergmann int aff_head; 122c5fc8d2aSArnd Bergmann int aff_offset; 1236263203eSArnd Bergmann }; 1246263203eSArnd Bergmann 1256263203eSArnd Bergmann struct spu_gang { 1266263203eSArnd Bergmann struct list_head list; 1276263203eSArnd Bergmann struct mutex mutex; 1286263203eSArnd Bergmann struct kref kref; 1296263203eSArnd Bergmann int contexts; 1308e68e2f2SArnd Bergmann 1318e68e2f2SArnd Bergmann struct spu_context *aff_ref_ctx; 1328e68e2f2SArnd Bergmann struct list_head aff_list_head; 1338e68e2f2SArnd Bergmann struct mutex aff_mutex; 1348e68e2f2SArnd Bergmann int aff_flags; 135c5fc8d2aSArnd Bergmann struct spu *aff_ref_spu; 136c5fc8d2aSArnd Bergmann atomic_t aff_sched_count; 13767207b96SArnd Bergmann }; 13867207b96SArnd Bergmann 1398e68e2f2SArnd Bergmann /* Flag bits for spu_gang aff_flags */ 1408e68e2f2SArnd Bergmann #define AFF_OFFSETS_SET 1 1418e68e2f2SArnd Bergmann #define AFF_MERGED 2 1428e68e2f2SArnd Bergmann 143a33a7d73SArnd Bergmann struct mfc_dma_command { 144a33a7d73SArnd Bergmann int32_t pad; /* reserved */ 145a33a7d73SArnd Bergmann uint32_t lsa; /* local storage address */ 146a33a7d73SArnd Bergmann uint64_t ea; /* effective address */ 147a33a7d73SArnd Bergmann uint16_t size; /* transfer size */ 148a33a7d73SArnd Bergmann uint16_t tag; /* command tag */ 149a33a7d73SArnd Bergmann uint16_t class; /* class ID */ 150a33a7d73SArnd Bergmann uint16_t cmd; /* command opcode */ 151a33a7d73SArnd Bergmann }; 152a33a7d73SArnd Bergmann 153a33a7d73SArnd Bergmann 1548b3d6663SArnd Bergmann /* SPU context query/set operations. */ 1558b3d6663SArnd Bergmann struct spu_context_ops { 1568b3d6663SArnd Bergmann int (*mbox_read) (struct spu_context * ctx, u32 * data); 1578b3d6663SArnd Bergmann u32(*mbox_stat_read) (struct spu_context * ctx); 1583a843d7cSArnd Bergmann unsigned int (*mbox_stat_poll)(struct spu_context *ctx, 1593a843d7cSArnd Bergmann unsigned int events); 1608b3d6663SArnd Bergmann int (*ibox_read) (struct spu_context * ctx, u32 * data); 1618b3d6663SArnd Bergmann int (*wbox_write) (struct spu_context * ctx, u32 data); 1628b3d6663SArnd Bergmann u32(*signal1_read) (struct spu_context * ctx); 1638b3d6663SArnd Bergmann void (*signal1_write) (struct spu_context * ctx, u32 data); 1648b3d6663SArnd Bergmann u32(*signal2_read) (struct spu_context * ctx); 1658b3d6663SArnd Bergmann void (*signal2_write) (struct spu_context * ctx, u32 data); 1668b3d6663SArnd Bergmann void (*signal1_type_set) (struct spu_context * ctx, u64 val); 1678b3d6663SArnd Bergmann u64(*signal1_type_get) (struct spu_context * ctx); 1688b3d6663SArnd Bergmann void (*signal2_type_set) (struct spu_context * ctx, u64 val); 1698b3d6663SArnd Bergmann u64(*signal2_type_get) (struct spu_context * ctx); 1708b3d6663SArnd Bergmann u32(*npc_read) (struct spu_context * ctx); 1718b3d6663SArnd Bergmann void (*npc_write) (struct spu_context * ctx, u32 data); 1728b3d6663SArnd Bergmann u32(*status_read) (struct spu_context * ctx); 1738b3d6663SArnd Bergmann char*(*get_ls) (struct spu_context * ctx); 1743960c260SJeremy Kerr u32 (*runcntl_read) (struct spu_context * ctx); 1755110459fSArnd Bergmann void (*runcntl_write) (struct spu_context * ctx, u32 data); 176ee2d7340SArnd Bergmann void (*master_start) (struct spu_context * ctx); 177ee2d7340SArnd Bergmann void (*master_stop) (struct spu_context * ctx); 178a33a7d73SArnd Bergmann int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode); 179a33a7d73SArnd Bergmann u32 (*read_mfc_tagstatus)(struct spu_context * ctx); 180a33a7d73SArnd Bergmann u32 (*get_mfc_free_elements)(struct spu_context *ctx); 181a33a7d73SArnd Bergmann int (*send_mfc_command)(struct spu_context * ctx, 182a33a7d73SArnd Bergmann struct mfc_dma_command * cmd); 183b9e3bd77SDwayne Grant McConnell void (*dma_info_read) (struct spu_context * ctx, 184b9e3bd77SDwayne Grant McConnell struct spu_dma_info * info); 185b9e3bd77SDwayne Grant McConnell void (*proxydma_info_read) (struct spu_context * ctx, 186b9e3bd77SDwayne Grant McConnell struct spu_proxydma_info * info); 18757dace23SArnd Bergmann void (*restart_dma)(struct spu_context *ctx); 1888b3d6663SArnd Bergmann }; 1898b3d6663SArnd Bergmann 1908b3d6663SArnd Bergmann extern struct spu_context_ops spu_hw_ops; 1918b3d6663SArnd Bergmann extern struct spu_context_ops spu_backing_ops; 1928b3d6663SArnd Bergmann 19367207b96SArnd Bergmann struct spufs_inode_info { 19467207b96SArnd Bergmann struct spu_context *i_ctx; 1956263203eSArnd Bergmann struct spu_gang *i_gang; 19667207b96SArnd Bergmann struct inode vfs_inode; 19743c2bbd9SChristoph Hellwig int i_openers; 19867207b96SArnd Bergmann }; 19967207b96SArnd Bergmann #define SPUFS_I(inode) \ 20067207b96SArnd Bergmann container_of(inode, struct spufs_inode_info, vfs_inode) 20167207b96SArnd Bergmann 20267207b96SArnd Bergmann extern struct tree_descr spufs_dir_contents[]; 2035737edd1SMark Nutter extern struct tree_descr spufs_dir_nosched_contents[]; 20467207b96SArnd Bergmann 20567207b96SArnd Bergmann /* system call implementation */ 20650af32a9SJeremy Kerr long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status); 2078e68e2f2SArnd Bergmann long spufs_create(struct nameidata *nd, unsigned int flags, 2088e68e2f2SArnd Bergmann mode_t mode, struct file *filp); 2099c2e08c5SArjan van de Ven extern const struct file_operations spufs_context_fops; 21067207b96SArnd Bergmann 2116263203eSArnd Bergmann /* gang management */ 2126263203eSArnd Bergmann struct spu_gang *alloc_spu_gang(void); 2136263203eSArnd Bergmann struct spu_gang *get_spu_gang(struct spu_gang *gang); 2146263203eSArnd Bergmann int put_spu_gang(struct spu_gang *gang); 2156263203eSArnd Bergmann void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx); 2166263203eSArnd Bergmann void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx); 2176263203eSArnd Bergmann 21857dace23SArnd Bergmann /* fault handling */ 21957dace23SArnd Bergmann int spufs_handle_class1(struct spu_context *ctx); 22057dace23SArnd Bergmann 221c5fc8d2aSArnd Bergmann /* affinity */ 222c5fc8d2aSArnd Bergmann struct spu *affinity_check(struct spu_context *ctx); 223c5fc8d2aSArnd Bergmann 22467207b96SArnd Bergmann /* context management */ 22565de66f0SChristoph Hellwig extern atomic_t nr_spu_contexts; 2266a0641e5SChristoph Hellwig static inline void spu_acquire(struct spu_context *ctx) 2276a0641e5SChristoph Hellwig { 2286a0641e5SChristoph Hellwig mutex_lock(&ctx->state_mutex); 2296a0641e5SChristoph Hellwig } 2306a0641e5SChristoph Hellwig 2316a0641e5SChristoph Hellwig static inline void spu_release(struct spu_context *ctx) 2326a0641e5SChristoph Hellwig { 2336a0641e5SChristoph Hellwig mutex_unlock(&ctx->state_mutex); 2346a0641e5SChristoph Hellwig } 2356a0641e5SChristoph Hellwig 2366263203eSArnd Bergmann struct spu_context * alloc_spu_context(struct spu_gang *gang); 23767207b96SArnd Bergmann void destroy_spu_context(struct kref *kref); 23867207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx); 23967207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx); 2405110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx); 24167207b96SArnd Bergmann 2428b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx); 24326bec673SChristoph Hellwig int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags); 24467207b96SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx); 24527b1ea09SChristoph Hellwig void spu_release_saved(struct spu_context *ctx); 24650b520d4SChristoph Hellwig 24726bec673SChristoph Hellwig int spu_activate(struct spu_context *ctx, unsigned long flags); 2488b3d6663SArnd Bergmann void spu_deactivate(struct spu_context *ctx); 2498b3d6663SArnd Bergmann void spu_yield(struct spu_context *ctx); 25036aaccc1SBob Nelson void spu_switch_notify(struct spu *spu, struct spu_context *ctx); 251fe443ef2SChristoph Hellwig void spu_set_timeslice(struct spu_context *ctx); 2522cf2b3b4SChristoph Hellwig void spu_update_sched_info(struct spu_context *ctx); 2532cf2b3b4SChristoph Hellwig void __spu_update_sched_info(struct spu_context *ctx); 2548b3d6663SArnd Bergmann int __init spu_sched_init(void); 255d1450317SSebastian Siewior void spu_sched_exit(void); 2568b3d6663SArnd Bergmann 257c6730ed4SJeremy Kerr extern char *isolated_loader; 258c6730ed4SJeremy Kerr 259ce8ab854SArnd Bergmann /* 260ce8ab854SArnd Bergmann * spufs_wait 261ce8ab854SArnd Bergmann * Same as wait_event_interruptible(), except that here 262ce8ab854SArnd Bergmann * we need to call spu_release(ctx) before sleeping, and 263ce8ab854SArnd Bergmann * then spu_acquire(ctx) when awoken. 264ce8ab854SArnd Bergmann */ 265ce8ab854SArnd Bergmann 266ce8ab854SArnd Bergmann #define spufs_wait(wq, condition) \ 267ce8ab854SArnd Bergmann ({ \ 268ce8ab854SArnd Bergmann int __ret = 0; \ 269ce8ab854SArnd Bergmann DEFINE_WAIT(__wait); \ 270ce8ab854SArnd Bergmann for (;;) { \ 271ce8ab854SArnd Bergmann prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \ 272ce8ab854SArnd Bergmann if (condition) \ 273ce8ab854SArnd Bergmann break; \ 274d3764397SJeremy Kerr if (signal_pending(current)) { \ 275d3764397SJeremy Kerr __ret = -ERESTARTSYS; \ 276d3764397SJeremy Kerr break; \ 277d3764397SJeremy Kerr } \ 278ce8ab854SArnd Bergmann spu_release(ctx); \ 279ce8ab854SArnd Bergmann schedule(); \ 280ce8ab854SArnd Bergmann spu_acquire(ctx); \ 281ce8ab854SArnd Bergmann } \ 282ce8ab854SArnd Bergmann finish_wait(&(wq), &__wait); \ 283ce8ab854SArnd Bergmann __ret; \ 284ce8ab854SArnd Bergmann }) 285ce8ab854SArnd Bergmann 2868b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data); 2878b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data); 2888b3d6663SArnd Bergmann 2898b3d6663SArnd Bergmann /* irq callback funcs. */ 2908b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu); 2918b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu); 2925110459fSArnd Bergmann void spufs_stop_callback(struct spu *spu); 293a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu); 2949add11daSArnd Bergmann void spufs_dma_callback(struct spu *spu, int type); 2958b3d6663SArnd Bergmann 296bf1ab978SDwayne Grant McConnell extern struct spu_coredump_calls spufs_coredump_calls; 297bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader { 298bf1ab978SDwayne Grant McConnell char *name; 299bf1ab978SDwayne Grant McConnell ssize_t (*read)(struct spu_context *ctx, 300bf1ab978SDwayne Grant McConnell char __user *buffer, size_t size, loff_t *pos); 301bf1ab978SDwayne Grant McConnell u64 (*get)(void *data); 302bf1ab978SDwayne Grant McConnell size_t size; 303bf1ab978SDwayne Grant McConnell }; 304bf1ab978SDwayne Grant McConnell extern struct spufs_coredump_reader spufs_coredump_read[]; 305bf1ab978SDwayne Grant McConnell extern int spufs_coredump_num_notes; 306bf1ab978SDwayne Grant McConnell 307e9f8a0b6SChristoph Hellwig /* 308e9f8a0b6SChristoph Hellwig * This function is a little bit too large for an inline, but 309e9f8a0b6SChristoph Hellwig * as fault.c is built into the kernel we can't move it out of 310e9f8a0b6SChristoph Hellwig * line. 311e9f8a0b6SChristoph Hellwig */ 312e9f8a0b6SChristoph Hellwig static inline void spuctx_switch_state(struct spu_context *ctx, 31327ec41d3SAndre Detsch enum spu_utilization_state new_state) 314e9f8a0b6SChristoph Hellwig { 31527ec41d3SAndre Detsch unsigned long long curtime; 31627ec41d3SAndre Detsch signed long long delta; 31727ec41d3SAndre Detsch struct timespec ts; 31827ec41d3SAndre Detsch struct spu *spu; 31927ec41d3SAndre Detsch enum spu_utilization_state old_state; 32027ec41d3SAndre Detsch 32127ec41d3SAndre Detsch ktime_get_ts(&ts); 32227ec41d3SAndre Detsch curtime = timespec_to_ns(&ts); 32327ec41d3SAndre Detsch delta = curtime - ctx->stats.tstamp; 32427ec41d3SAndre Detsch 325e9f8a0b6SChristoph Hellwig WARN_ON(!mutex_is_locked(&ctx->state_mutex)); 32627ec41d3SAndre Detsch WARN_ON(delta < 0); 327e9f8a0b6SChristoph Hellwig 32827ec41d3SAndre Detsch spu = ctx->spu; 32927ec41d3SAndre Detsch old_state = ctx->stats.util_state; 33027ec41d3SAndre Detsch ctx->stats.util_state = new_state; 331e9f8a0b6SChristoph Hellwig ctx->stats.tstamp = curtime; 332e9f8a0b6SChristoph Hellwig 33327ec41d3SAndre Detsch /* 33427ec41d3SAndre Detsch * Update the physical SPU utilization statistics. 33527ec41d3SAndre Detsch */ 33627ec41d3SAndre Detsch if (spu) { 33727ec41d3SAndre Detsch ctx->stats.times[old_state] += delta; 33827ec41d3SAndre Detsch spu->stats.times[old_state] += delta; 33927ec41d3SAndre Detsch spu->stats.util_state = new_state; 340fe2f896dSChristoph Hellwig spu->stats.tstamp = curtime; 341fe2f896dSChristoph Hellwig } 342fe2f896dSChristoph Hellwig } 343fe2f896dSChristoph Hellwig 34467207b96SArnd Bergmann #endif 345