Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * SPU file system
0004  *
0005  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
0006  *
0007  * Author: Arnd Bergmann <arndb@de.ibm.com>
0008  */
0009 #ifndef SPUFS_H
0010 #define SPUFS_H
0011 
0012 #include <linux/kref.h>
0013 #include <linux/mutex.h>
0014 #include <linux/spinlock.h>
0015 #include <linux/fs.h>
0016 #include <linux/cpumask.h>
0017 #include <linux/sched/signal.h>
0018 
0019 #include <asm/spu.h>
0020 #include <asm/spu_csa.h>
0021 #include <asm/spu_info.h>
0022 
0023 #define SPUFS_PS_MAP_SIZE   0x20000
0024 #define SPUFS_MFC_MAP_SIZE  0x1000
0025 #define SPUFS_CNTL_MAP_SIZE 0x1000
0026 #define SPUFS_SIGNAL_MAP_SIZE   PAGE_SIZE
0027 #define SPUFS_MSS_MAP_SIZE  0x1000
0028 
0029 /* The magic number for our file system */
0030 enum {
0031     SPUFS_MAGIC = 0x23c9b64e,
0032 };
0033 
0034 struct spu_context_ops;
0035 struct spu_gang;
0036 
0037 /* ctx->sched_flags */
0038 enum {
0039     SPU_SCHED_NOTIFY_ACTIVE,
0040     SPU_SCHED_WAS_ACTIVE,   /* was active upon spu_acquire_saved()  */
0041     SPU_SCHED_SPU_RUN,  /* context is within spu_run */
0042 };
0043 
0044 enum {
0045     SWITCH_LOG_BUFSIZE = 4096,
0046 };
0047 
0048 enum {
0049     SWITCH_LOG_START,
0050     SWITCH_LOG_STOP,
0051     SWITCH_LOG_EXIT,
0052 };
0053 
0054 struct switch_log {
0055     wait_queue_head_t   wait;
0056     unsigned long       head;
0057     unsigned long       tail;
0058     struct switch_log_entry {
0059         struct timespec64 tstamp;
0060         s32     spu_id;
0061         u32     type;
0062         u32     val;
0063         u64     timebase;
0064     } log[];
0065 };
0066 
0067 struct spu_context {
0068     struct spu *spu;          /* pointer to a physical SPU */
0069     struct spu_state csa;         /* SPU context save area. */
0070     spinlock_t mmio_lock;         /* protects mmio access */
0071     struct address_space *local_store; /* local store mapping.  */
0072     struct address_space *mfc;     /* 'mfc' area mappings. */
0073     struct address_space *cntl;    /* 'control' area mappings. */
0074     struct address_space *signal1;     /* 'signal1' area mappings. */
0075     struct address_space *signal2;     /* 'signal2' area mappings. */
0076     struct address_space *mss;     /* 'mss' area mappings. */
0077     struct address_space *psmap;       /* 'psmap' area mappings. */
0078     struct mutex mapping_lock;
0079     u64 object_id;         /* user space pointer for GNU Debugger */
0080 
0081     enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
0082     struct mutex state_mutex;
0083     struct mutex run_mutex;
0084 
0085     struct mm_struct *owner;
0086 
0087     struct kref kref;
0088     wait_queue_head_t ibox_wq;
0089     wait_queue_head_t wbox_wq;
0090     wait_queue_head_t stop_wq;
0091     wait_queue_head_t mfc_wq;
0092     wait_queue_head_t run_wq;
0093     u32 tagwait;
0094     struct spu_context_ops *ops;
0095     struct work_struct reap_work;
0096     unsigned long flags;
0097     unsigned long event_return;
0098 
0099     struct list_head gang_list;
0100     struct spu_gang *gang;
0101     struct kref *prof_priv_kref;
0102     void ( * prof_priv_release) (struct kref *kref);
0103 
0104     /* owner thread */
0105     pid_t tid;
0106 
0107     /* scheduler fields */
0108     struct list_head rq;
0109     unsigned int time_slice;
0110     unsigned long sched_flags;
0111     cpumask_t cpus_allowed;
0112     int policy;
0113     int prio;
0114     int last_ran;
0115 
0116     /* statistics */
0117     struct {
0118         /* updates protected by ctx->state_mutex */
0119         enum spu_utilization_state util_state;
0120         unsigned long long tstamp;  /* time of last state switch */
0121         unsigned long long times[SPU_UTIL_MAX];
0122         unsigned long long vol_ctx_switch;
0123         unsigned long long invol_ctx_switch;
0124         unsigned long long min_flt;
0125         unsigned long long maj_flt;
0126         unsigned long long hash_flt;
0127         unsigned long long slb_flt;
0128         unsigned long long slb_flt_base; /* # at last ctx switch */
0129         unsigned long long class2_intr;
0130         unsigned long long class2_intr_base; /* # at last ctx switch */
0131         unsigned long long libassist;
0132     } stats;
0133 
0134     /* context switch log */
0135     struct switch_log *switch_log;
0136 
0137     struct list_head aff_list;
0138     int aff_head;
0139     int aff_offset;
0140 };
0141 
0142 struct spu_gang {
0143     struct list_head list;
0144     struct mutex mutex;
0145     struct kref kref;
0146     int contexts;
0147 
0148     struct spu_context *aff_ref_ctx;
0149     struct list_head aff_list_head;
0150     struct mutex aff_mutex;
0151     int aff_flags;
0152     struct spu *aff_ref_spu;
0153     atomic_t aff_sched_count;
0154 };
0155 
0156 /* Flag bits for spu_gang aff_flags */
0157 #define AFF_OFFSETS_SET     1
0158 #define AFF_MERGED      2
0159 
0160 struct mfc_dma_command {
0161     int32_t pad;    /* reserved */
0162     uint32_t lsa;   /* local storage address */
0163     uint64_t ea;    /* effective address */
0164     uint16_t size;  /* transfer size */
0165     uint16_t tag;   /* command tag */
0166     uint16_t class; /* class ID */
0167     uint16_t cmd;   /* command opcode */
0168 };
0169 
0170 
0171 /* SPU context query/set operations. */
0172 struct spu_context_ops {
0173     int (*mbox_read) (struct spu_context * ctx, u32 * data);
0174      u32(*mbox_stat_read) (struct spu_context * ctx);
0175     __poll_t (*mbox_stat_poll)(struct spu_context *ctx, __poll_t events);
0176     int (*ibox_read) (struct spu_context * ctx, u32 * data);
0177     int (*wbox_write) (struct spu_context * ctx, u32 data);
0178      u32(*signal1_read) (struct spu_context * ctx);
0179     void (*signal1_write) (struct spu_context * ctx, u32 data);
0180      u32(*signal2_read) (struct spu_context * ctx);
0181     void (*signal2_write) (struct spu_context * ctx, u32 data);
0182     void (*signal1_type_set) (struct spu_context * ctx, u64 val);
0183      u64(*signal1_type_get) (struct spu_context * ctx);
0184     void (*signal2_type_set) (struct spu_context * ctx, u64 val);
0185      u64(*signal2_type_get) (struct spu_context * ctx);
0186      u32(*npc_read) (struct spu_context * ctx);
0187     void (*npc_write) (struct spu_context * ctx, u32 data);
0188      u32(*status_read) (struct spu_context * ctx);
0189     char*(*get_ls) (struct spu_context * ctx);
0190     void (*privcntl_write) (struct spu_context *ctx, u64 data);
0191      u32 (*runcntl_read) (struct spu_context * ctx);
0192     void (*runcntl_write) (struct spu_context * ctx, u32 data);
0193     void (*runcntl_stop) (struct spu_context * ctx);
0194     void (*master_start) (struct spu_context * ctx);
0195     void (*master_stop) (struct spu_context * ctx);
0196     int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
0197     u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
0198     u32 (*get_mfc_free_elements)(struct spu_context *ctx);
0199     int (*send_mfc_command)(struct spu_context * ctx,
0200                 struct mfc_dma_command * cmd);
0201     void (*dma_info_read) (struct spu_context * ctx,
0202                    struct spu_dma_info * info);
0203     void (*proxydma_info_read) (struct spu_context * ctx,
0204                     struct spu_proxydma_info * info);
0205     void (*restart_dma)(struct spu_context *ctx);
0206 };
0207 
0208 extern struct spu_context_ops spu_hw_ops;
0209 extern struct spu_context_ops spu_backing_ops;
0210 
0211 struct spufs_inode_info {
0212     struct spu_context *i_ctx;
0213     struct spu_gang *i_gang;
0214     struct inode vfs_inode;
0215     int i_openers;
0216 };
0217 #define SPUFS_I(inode) \
0218     container_of(inode, struct spufs_inode_info, vfs_inode)
0219 
0220 struct spufs_tree_descr {
0221     const char *name;
0222     const struct file_operations *ops;
0223     umode_t mode;
0224     size_t size;
0225 };
0226 
0227 extern const struct spufs_tree_descr spufs_dir_contents[];
0228 extern const struct spufs_tree_descr spufs_dir_nosched_contents[];
0229 extern const struct spufs_tree_descr spufs_dir_debug_contents[];
0230 
0231 /* system call implementation */
0232 extern struct spufs_calls spufs_calls;
0233 struct coredump_params;
0234 long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status);
0235 long spufs_create(struct path *nd, struct dentry *dentry, unsigned int flags,
0236             umode_t mode, struct file *filp);
0237 /* ELF coredump callbacks for writing SPU ELF notes */
0238 extern int spufs_coredump_extra_notes_size(void);
0239 extern int spufs_coredump_extra_notes_write(struct coredump_params *cprm);
0240 
0241 extern const struct file_operations spufs_context_fops;
0242 
0243 /* gang management */
0244 struct spu_gang *alloc_spu_gang(void);
0245 struct spu_gang *get_spu_gang(struct spu_gang *gang);
0246 int put_spu_gang(struct spu_gang *gang);
0247 void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
0248 void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
0249 
0250 /* fault handling */
0251 int spufs_handle_class1(struct spu_context *ctx);
0252 int spufs_handle_class0(struct spu_context *ctx);
0253 
0254 /* affinity */
0255 struct spu *affinity_check(struct spu_context *ctx);
0256 
0257 /* context management */
0258 extern atomic_t nr_spu_contexts;
0259 static inline int __must_check spu_acquire(struct spu_context *ctx)
0260 {
0261     return mutex_lock_interruptible(&ctx->state_mutex);
0262 }
0263 
0264 static inline void spu_release(struct spu_context *ctx)
0265 {
0266     mutex_unlock(&ctx->state_mutex);
0267 }
0268 
0269 struct spu_context * alloc_spu_context(struct spu_gang *gang);
0270 void destroy_spu_context(struct kref *kref);
0271 struct spu_context * get_spu_context(struct spu_context *ctx);
0272 int put_spu_context(struct spu_context *ctx);
0273 void spu_unmap_mappings(struct spu_context *ctx);
0274 
0275 void spu_forget(struct spu_context *ctx);
0276 int __must_check spu_acquire_saved(struct spu_context *ctx);
0277 void spu_release_saved(struct spu_context *ctx);
0278 
0279 int spu_stopped(struct spu_context *ctx, u32 * stat);
0280 void spu_del_from_rq(struct spu_context *ctx);
0281 int spu_activate(struct spu_context *ctx, unsigned long flags);
0282 void spu_deactivate(struct spu_context *ctx);
0283 void spu_yield(struct spu_context *ctx);
0284 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
0285         u32 type, u32 val);
0286 void spu_set_timeslice(struct spu_context *ctx);
0287 void spu_update_sched_info(struct spu_context *ctx);
0288 void __spu_update_sched_info(struct spu_context *ctx);
0289 int __init spu_sched_init(void);
0290 void spu_sched_exit(void);
0291 
0292 extern char *isolated_loader;
0293 
0294 /*
0295  * spufs_wait
0296  *  Same as wait_event_interruptible(), except that here
0297  *  we need to call spu_release(ctx) before sleeping, and
0298  *  then spu_acquire(ctx) when awoken.
0299  *
0300  *  Returns with state_mutex re-acquired when successful or
0301  *  with -ERESTARTSYS and the state_mutex dropped when interrupted.
0302  */
0303 
0304 #define spufs_wait(wq, condition)                   \
0305 ({                                  \
0306     int __ret = 0;                          \
0307     DEFINE_WAIT(__wait);                        \
0308     for (;;) {                          \
0309         prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE);    \
0310         if (condition)                      \
0311             break;                      \
0312         spu_release(ctx);                   \
0313         if (signal_pending(current)) {              \
0314             __ret = -ERESTARTSYS;               \
0315             break;                      \
0316         }                           \
0317         schedule();                     \
0318         __ret = spu_acquire(ctx);               \
0319         if (__ret)                      \
0320             break;                      \
0321     }                               \
0322     finish_wait(&(wq), &__wait);                    \
0323     __ret;                              \
0324 })
0325 
0326 size_t spu_wbox_write(struct spu_context *ctx, u32 data);
0327 size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
0328 
0329 /* irq callback funcs. */
0330 void spufs_ibox_callback(struct spu *spu);
0331 void spufs_wbox_callback(struct spu *spu);
0332 void spufs_stop_callback(struct spu *spu, int irq);
0333 void spufs_mfc_callback(struct spu *spu);
0334 void spufs_dma_callback(struct spu *spu, int type);
0335 
0336 extern struct spu_coredump_calls spufs_coredump_calls;
0337 struct spufs_coredump_reader {
0338     char *name;
0339     ssize_t (*dump)(struct spu_context *ctx, struct coredump_params *cprm);
0340     u64 (*get)(struct spu_context *ctx);
0341     size_t size;
0342 };
0343 extern const struct spufs_coredump_reader spufs_coredump_read[];
0344 extern int spufs_coredump_num_notes;
0345 
0346 extern int spu_init_csa(struct spu_state *csa);
0347 extern void spu_fini_csa(struct spu_state *csa);
0348 extern int spu_save(struct spu_state *prev, struct spu *spu);
0349 extern int spu_restore(struct spu_state *new, struct spu *spu);
0350 extern int spu_switch(struct spu_state *prev, struct spu_state *new,
0351               struct spu *spu);
0352 extern int spu_alloc_lscsa(struct spu_state *csa);
0353 extern void spu_free_lscsa(struct spu_state *csa);
0354 
0355 extern void spuctx_switch_state(struct spu_context *ctx,
0356         enum spu_utilization_state new_state);
0357 
0358 #endif