spufs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * SPU file system
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #ifndef SPUFS_H
  23. #define SPUFS_H
  24. #include <linux/kref.h>
  25. #include <linux/mutex.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/fs.h>
  28. #include <linux/cpumask.h>
  29. #include <asm/spu.h>
  30. #include <asm/spu_csa.h>
  31. #include <asm/spu_info.h>
  32. /* The magic number for our file system */
  33. enum {
  34. SPUFS_MAGIC = 0x23c9b64e,
  35. };
  36. struct spu_context_ops;
  37. struct spu_gang;
  38. /* ctx->sched_flags */
  39. enum {
  40. SPU_SCHED_NOTIFY_ACTIVE,
  41. SPU_SCHED_WAS_ACTIVE, /* was active upon spu_acquire_saved() */
  42. };
  43. struct spu_context {
  44. struct spu *spu; /* pointer to a physical SPU */
  45. struct spu_state csa; /* SPU context save area. */
  46. spinlock_t mmio_lock; /* protects mmio access */
  47. struct address_space *local_store; /* local store mapping. */
  48. struct address_space *mfc; /* 'mfc' area mappings. */
  49. struct address_space *cntl; /* 'control' area mappings. */
  50. struct address_space *signal1; /* 'signal1' area mappings. */
  51. struct address_space *signal2; /* 'signal2' area mappings. */
  52. struct address_space *mss; /* 'mss' area mappings. */
  53. struct address_space *psmap; /* 'psmap' area mappings. */
  54. struct mutex mapping_lock;
  55. u64 object_id; /* user space pointer for oprofile */
  56. enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
  57. struct mutex state_mutex;
  58. struct mutex run_mutex;
  59. struct mm_struct *owner;
  60. struct kref kref;
  61. wait_queue_head_t ibox_wq;
  62. wait_queue_head_t wbox_wq;
  63. wait_queue_head_t stop_wq;
  64. wait_queue_head_t mfc_wq;
  65. wait_queue_head_t run_wq;
  66. struct fasync_struct *ibox_fasync;
  67. struct fasync_struct *wbox_fasync;
  68. struct fasync_struct *mfc_fasync;
  69. u32 tagwait;
  70. struct spu_context_ops *ops;
  71. struct work_struct reap_work;
  72. unsigned long flags;
  73. unsigned long event_return;
  74. struct list_head gang_list;
  75. struct spu_gang *gang;
  76. struct kref *prof_priv_kref;
  77. void ( * prof_priv_release) (struct kref *kref);
  78. /* owner thread */
  79. pid_t tid;
  80. /* scheduler fields */
  81. struct list_head rq;
  82. unsigned int time_slice;
  83. unsigned long sched_flags;
  84. cpumask_t cpus_allowed;
  85. int policy;
  86. int prio;
  87. /* statistics */
  88. struct {
  89. /* updates protected by ctx->state_mutex */
  90. enum spu_utilization_state util_state;
  91. unsigned long long tstamp; /* time of last state switch */
  92. unsigned long long times[SPU_UTIL_MAX];
  93. unsigned long long vol_ctx_switch;
  94. unsigned long long invol_ctx_switch;
  95. unsigned long long min_flt;
  96. unsigned long long maj_flt;
  97. unsigned long long hash_flt;
  98. unsigned long long slb_flt;
  99. unsigned long long slb_flt_base; /* # at last ctx switch */
  100. unsigned long long class2_intr;
  101. unsigned long long class2_intr_base; /* # at last ctx switch */
  102. unsigned long long libassist;
  103. } stats;
  104. struct list_head aff_list;
  105. int aff_head;
  106. int aff_offset;
  107. };
  108. struct spu_gang {
  109. struct list_head list;
  110. struct mutex mutex;
  111. struct kref kref;
  112. int contexts;
  113. struct spu_context *aff_ref_ctx;
  114. struct list_head aff_list_head;
  115. struct mutex aff_mutex;
  116. int aff_flags;
  117. struct spu *aff_ref_spu;
  118. atomic_t aff_sched_count;
  119. };
  120. /* Flag bits for spu_gang aff_flags */
  121. #define AFF_OFFSETS_SET 1
  122. #define AFF_MERGED 2
  123. struct mfc_dma_command {
  124. int32_t pad; /* reserved */
  125. uint32_t lsa; /* local storage address */
  126. uint64_t ea; /* effective address */
  127. uint16_t size; /* transfer size */
  128. uint16_t tag; /* command tag */
  129. uint16_t class; /* class ID */
  130. uint16_t cmd; /* command opcode */
  131. };
  132. /* SPU context query/set operations. */
  133. struct spu_context_ops {
  134. int (*mbox_read) (struct spu_context * ctx, u32 * data);
  135. u32(*mbox_stat_read) (struct spu_context * ctx);
  136. unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
  137. unsigned int events);
  138. int (*ibox_read) (struct spu_context * ctx, u32 * data);
  139. int (*wbox_write) (struct spu_context * ctx, u32 data);
  140. u32(*signal1_read) (struct spu_context * ctx);
  141. void (*signal1_write) (struct spu_context * ctx, u32 data);
  142. u32(*signal2_read) (struct spu_context * ctx);
  143. void (*signal2_write) (struct spu_context * ctx, u32 data);
  144. void (*signal1_type_set) (struct spu_context * ctx, u64 val);
  145. u64(*signal1_type_get) (struct spu_context * ctx);
  146. void (*signal2_type_set) (struct spu_context * ctx, u64 val);
  147. u64(*signal2_type_get) (struct spu_context * ctx);
  148. u32(*npc_read) (struct spu_context * ctx);
  149. void (*npc_write) (struct spu_context * ctx, u32 data);
  150. u32(*status_read) (struct spu_context * ctx);
  151. char*(*get_ls) (struct spu_context * ctx);
  152. void (*privcntl_write) (struct spu_context *ctx, u64 data);
  153. u32 (*runcntl_read) (struct spu_context * ctx);
  154. void (*runcntl_write) (struct spu_context * ctx, u32 data);
  155. void (*runcntl_stop) (struct spu_context * ctx);
  156. void (*master_start) (struct spu_context * ctx);
  157. void (*master_stop) (struct spu_context * ctx);
  158. int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
  159. u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
  160. u32 (*get_mfc_free_elements)(struct spu_context *ctx);
  161. int (*send_mfc_command)(struct spu_context * ctx,
  162. struct mfc_dma_command * cmd);
  163. void (*dma_info_read) (struct spu_context * ctx,
  164. struct spu_dma_info * info);
  165. void (*proxydma_info_read) (struct spu_context * ctx,
  166. struct spu_proxydma_info * info);
  167. void (*restart_dma)(struct spu_context *ctx);
  168. };
  169. extern struct spu_context_ops spu_hw_ops;
  170. extern struct spu_context_ops spu_backing_ops;
  171. struct spufs_inode_info {
  172. struct spu_context *i_ctx;
  173. struct spu_gang *i_gang;
  174. struct inode vfs_inode;
  175. int i_openers;
  176. };
  177. #define SPUFS_I(inode) \
  178. container_of(inode, struct spufs_inode_info, vfs_inode)
  179. extern struct tree_descr spufs_dir_contents[];
  180. extern struct tree_descr spufs_dir_nosched_contents[];
  181. /* system call implementation */
  182. extern struct spufs_calls spufs_calls;
  183. long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status);
  184. long spufs_create(struct nameidata *nd, unsigned int flags,
  185. mode_t mode, struct file *filp);
  186. /* ELF coredump callbacks for writing SPU ELF notes */
  187. extern int spufs_coredump_extra_notes_size(void);
  188. extern int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset);
  189. extern const struct file_operations spufs_context_fops;
  190. /* gang management */
  191. struct spu_gang *alloc_spu_gang(void);
  192. struct spu_gang *get_spu_gang(struct spu_gang *gang);
  193. int put_spu_gang(struct spu_gang *gang);
  194. void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
  195. void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
  196. /* fault handling */
  197. int spufs_handle_class1(struct spu_context *ctx);
  198. int spufs_handle_class0(struct spu_context *ctx);
  199. /* affinity */
  200. struct spu *affinity_check(struct spu_context *ctx);
  201. /* context management */
  202. extern atomic_t nr_spu_contexts;
  203. static inline int __must_check spu_acquire(struct spu_context *ctx)
  204. {
  205. return mutex_lock_interruptible(&ctx->state_mutex);
  206. }
  207. static inline void spu_release(struct spu_context *ctx)
  208. {
  209. mutex_unlock(&ctx->state_mutex);
  210. }
  211. struct spu_context * alloc_spu_context(struct spu_gang *gang);
  212. void destroy_spu_context(struct kref *kref);
  213. struct spu_context * get_spu_context(struct spu_context *ctx);
  214. int put_spu_context(struct spu_context *ctx);
  215. void spu_unmap_mappings(struct spu_context *ctx);
  216. void spu_forget(struct spu_context *ctx);
  217. int __must_check spu_acquire_saved(struct spu_context *ctx);
  218. void spu_release_saved(struct spu_context *ctx);
  219. int spu_stopped(struct spu_context *ctx, u32 * stat);
  220. void spu_del_from_rq(struct spu_context *ctx);
  221. int spu_activate(struct spu_context *ctx, unsigned long flags);
  222. void spu_deactivate(struct spu_context *ctx);
  223. void spu_yield(struct spu_context *ctx);
  224. void spu_switch_notify(struct spu *spu, struct spu_context *ctx);
  225. void spu_set_timeslice(struct spu_context *ctx);
  226. void spu_update_sched_info(struct spu_context *ctx);
  227. void __spu_update_sched_info(struct spu_context *ctx);
  228. int __init spu_sched_init(void);
  229. void spu_sched_exit(void);
  230. extern char *isolated_loader;
  231. /*
  232. * spufs_wait
  233. * Same as wait_event_interruptible(), except that here
  234. * we need to call spu_release(ctx) before sleeping, and
  235. * then spu_acquire(ctx) when awoken.
  236. *
  237. * Returns with state_mutex re-acquired when successfull or
  238. * with -ERESTARTSYS and the state_mutex dropped when interrupted.
  239. */
  240. #define spufs_wait(wq, condition) \
  241. ({ \
  242. int __ret = 0; \
  243. DEFINE_WAIT(__wait); \
  244. for (;;) { \
  245. prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
  246. if (condition) \
  247. break; \
  248. spu_release(ctx); \
  249. if (signal_pending(current)) { \
  250. __ret = -ERESTARTSYS; \
  251. break; \
  252. } \
  253. schedule(); \
  254. __ret = spu_acquire(ctx); \
  255. if (__ret) \
  256. break; \
  257. } \
  258. finish_wait(&(wq), &__wait); \
  259. __ret; \
  260. })
  261. size_t spu_wbox_write(struct spu_context *ctx, u32 data);
  262. size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
  263. /* irq callback funcs. */
  264. void spufs_ibox_callback(struct spu *spu);
  265. void spufs_wbox_callback(struct spu *spu);
  266. void spufs_stop_callback(struct spu *spu);
  267. void spufs_mfc_callback(struct spu *spu);
  268. void spufs_dma_callback(struct spu *spu, int type);
  269. extern struct spu_coredump_calls spufs_coredump_calls;
  270. struct spufs_coredump_reader {
  271. char *name;
  272. ssize_t (*read)(struct spu_context *ctx,
  273. char __user *buffer, size_t size, loff_t *pos);
  274. u64 (*get)(struct spu_context *ctx);
  275. size_t size;
  276. };
  277. extern struct spufs_coredump_reader spufs_coredump_read[];
  278. extern int spufs_coredump_num_notes;
  279. extern int spu_init_csa(struct spu_state *csa);
  280. extern void spu_fini_csa(struct spu_state *csa);
  281. extern int spu_save(struct spu_state *prev, struct spu *spu);
  282. extern int spu_restore(struct spu_state *new, struct spu *spu);
  283. extern int spu_switch(struct spu_state *prev, struct spu_state *new,
  284. struct spu *spu);
  285. extern int spu_alloc_lscsa(struct spu_state *csa);
  286. extern void spu_free_lscsa(struct spu_state *csa);
  287. extern void spuctx_switch_state(struct spu_context *ctx,
  288. enum spu_utilization_state new_state);
  289. #define spu_context_trace(name, ctx, spu) \
  290. trace_mark(name, "%p %p", ctx, spu);
  291. #define spu_context_nospu_trace(name, ctx) \
  292. trace_mark(name, "%p", ctx);
  293. #endif