spufs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. SPU_SCHED_SPU_RUN, /* context is within spu_run */
  43. };
  44. enum {
  45. SWITCH_LOG_BUFSIZE = 4096,
  46. };
  47. enum {
  48. SWITCH_LOG_START,
  49. SWITCH_LOG_STOP,
  50. SWITCH_LOG_EXIT,
  51. };
  52. struct switch_log {
  53. spinlock_t lock;
  54. wait_queue_head_t wait;
  55. unsigned long head;
  56. unsigned long tail;
  57. struct switch_log_entry {
  58. struct timespec tstamp;
  59. s32 spu_id;
  60. u32 type;
  61. u32 val;
  62. u64 timebase;
  63. } log[];
  64. };
  65. struct spu_context {
  66. struct spu *spu; /* pointer to a physical SPU */
  67. struct spu_state csa; /* SPU context save area. */
  68. spinlock_t mmio_lock; /* protects mmio access */
  69. struct address_space *local_store; /* local store mapping. */
  70. struct address_space *mfc; /* 'mfc' area mappings. */
  71. struct address_space *cntl; /* 'control' area mappings. */
  72. struct address_space *signal1; /* 'signal1' area mappings. */
  73. struct address_space *signal2; /* 'signal2' area mappings. */
  74. struct address_space *mss; /* 'mss' area mappings. */
  75. struct address_space *psmap; /* 'psmap' area mappings. */
  76. struct mutex mapping_lock;
  77. u64 object_id; /* user space pointer for oprofile */
  78. enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
  79. struct mutex state_mutex;
  80. struct mutex run_mutex;
  81. struct mm_struct *owner;
  82. struct kref kref;
  83. wait_queue_head_t ibox_wq;
  84. wait_queue_head_t wbox_wq;
  85. wait_queue_head_t stop_wq;
  86. wait_queue_head_t mfc_wq;
  87. wait_queue_head_t run_wq;
  88. struct fasync_struct *ibox_fasync;
  89. struct fasync_struct *wbox_fasync;
  90. struct fasync_struct *mfc_fasync;
  91. u32 tagwait;
  92. struct spu_context_ops *ops;
  93. struct work_struct reap_work;
  94. unsigned long flags;
  95. unsigned long event_return;
  96. struct list_head gang_list;
  97. struct spu_gang *gang;
  98. struct kref *prof_priv_kref;
  99. void ( * prof_priv_release) (struct kref *kref);
  100. /* owner thread */
  101. pid_t tid;
  102. /* scheduler fields */
  103. struct list_head rq;
  104. unsigned int time_slice;
  105. unsigned long sched_flags;
  106. cpumask_t cpus_allowed;
  107. int policy;
  108. int prio;
  109. /* statistics */
  110. struct {
  111. /* updates protected by ctx->state_mutex */
  112. enum spu_utilization_state util_state;
  113. unsigned long long tstamp; /* time of last state switch */
  114. unsigned long long times[SPU_UTIL_MAX];
  115. unsigned long long vol_ctx_switch;
  116. unsigned long long invol_ctx_switch;
  117. unsigned long long min_flt;
  118. unsigned long long maj_flt;
  119. unsigned long long hash_flt;
  120. unsigned long long slb_flt;
  121. unsigned long long slb_flt_base; /* # at last ctx switch */
  122. unsigned long long class2_intr;
  123. unsigned long long class2_intr_base; /* # at last ctx switch */
  124. unsigned long long libassist;
  125. } stats;
  126. /* context switch log */
  127. struct switch_log *switch_log;
  128. struct list_head aff_list;
  129. int aff_head;
  130. int aff_offset;
  131. };
  132. struct spu_gang {
  133. struct list_head list;
  134. struct mutex mutex;
  135. struct kref kref;
  136. int contexts;
  137. struct spu_context *aff_ref_ctx;
  138. struct list_head aff_list_head;
  139. struct mutex aff_mutex;
  140. int aff_flags;
  141. struct spu *aff_ref_spu;
  142. atomic_t aff_sched_count;
  143. };
  144. /* Flag bits for spu_gang aff_flags */
  145. #define AFF_OFFSETS_SET 1
  146. #define AFF_MERGED 2
  147. struct mfc_dma_command {
  148. int32_t pad; /* reserved */
  149. uint32_t lsa; /* local storage address */
  150. uint64_t ea; /* effective address */
  151. uint16_t size; /* transfer size */
  152. uint16_t tag; /* command tag */
  153. uint16_t class; /* class ID */
  154. uint16_t cmd; /* command opcode */
  155. };
  156. /* SPU context query/set operations. */
  157. struct spu_context_ops {
  158. int (*mbox_read) (struct spu_context * ctx, u32 * data);
  159. u32(*mbox_stat_read) (struct spu_context * ctx);
  160. unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
  161. unsigned int events);
  162. int (*ibox_read) (struct spu_context * ctx, u32 * data);
  163. int (*wbox_write) (struct spu_context * ctx, u32 data);
  164. u32(*signal1_read) (struct spu_context * ctx);
  165. void (*signal1_write) (struct spu_context * ctx, u32 data);
  166. u32(*signal2_read) (struct spu_context * ctx);
  167. void (*signal2_write) (struct spu_context * ctx, u32 data);
  168. void (*signal1_type_set) (struct spu_context * ctx, u64 val);
  169. u64(*signal1_type_get) (struct spu_context * ctx);
  170. void (*signal2_type_set) (struct spu_context * ctx, u64 val);
  171. u64(*signal2_type_get) (struct spu_context * ctx);
  172. u32(*npc_read) (struct spu_context * ctx);
  173. void (*npc_write) (struct spu_context * ctx, u32 data);
  174. u32(*status_read) (struct spu_context * ctx);
  175. char*(*get_ls) (struct spu_context * ctx);
  176. void (*privcntl_write) (struct spu_context *ctx, u64 data);
  177. u32 (*runcntl_read) (struct spu_context * ctx);
  178. void (*runcntl_write) (struct spu_context * ctx, u32 data);
  179. void (*runcntl_stop) (struct spu_context * ctx);
  180. void (*master_start) (struct spu_context * ctx);
  181. void (*master_stop) (struct spu_context * ctx);
  182. int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
  183. u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
  184. u32 (*get_mfc_free_elements)(struct spu_context *ctx);
  185. int (*send_mfc_command)(struct spu_context * ctx,
  186. struct mfc_dma_command * cmd);
  187. void (*dma_info_read) (struct spu_context * ctx,
  188. struct spu_dma_info * info);
  189. void (*proxydma_info_read) (struct spu_context * ctx,
  190. struct spu_proxydma_info * info);
  191. void (*restart_dma)(struct spu_context *ctx);
  192. };
  193. extern struct spu_context_ops spu_hw_ops;
  194. extern struct spu_context_ops spu_backing_ops;
  195. struct spufs_inode_info {
  196. struct spu_context *i_ctx;
  197. struct spu_gang *i_gang;
  198. struct inode vfs_inode;
  199. int i_openers;
  200. };
  201. #define SPUFS_I(inode) \
  202. container_of(inode, struct spufs_inode_info, vfs_inode)
  203. extern struct tree_descr spufs_dir_contents[];
  204. extern struct tree_descr spufs_dir_nosched_contents[];
  205. /* system call implementation */
  206. extern struct spufs_calls spufs_calls;
  207. long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status);
  208. long spufs_create(struct nameidata *nd, unsigned int flags,
  209. mode_t mode, struct file *filp);
  210. /* ELF coredump callbacks for writing SPU ELF notes */
  211. extern int spufs_coredump_extra_notes_size(void);
  212. extern int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset);
  213. extern const struct file_operations spufs_context_fops;
  214. /* gang management */
  215. struct spu_gang *alloc_spu_gang(void);
  216. struct spu_gang *get_spu_gang(struct spu_gang *gang);
  217. int put_spu_gang(struct spu_gang *gang);
  218. void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
  219. void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
  220. /* fault handling */
  221. int spufs_handle_class1(struct spu_context *ctx);
  222. int spufs_handle_class0(struct spu_context *ctx);
  223. /* affinity */
  224. struct spu *affinity_check(struct spu_context *ctx);
  225. /* context management */
  226. extern atomic_t nr_spu_contexts;
  227. static inline int __must_check spu_acquire(struct spu_context *ctx)
  228. {
  229. return mutex_lock_interruptible(&ctx->state_mutex);
  230. }
  231. static inline void spu_release(struct spu_context *ctx)
  232. {
  233. mutex_unlock(&ctx->state_mutex);
  234. }
  235. struct spu_context * alloc_spu_context(struct spu_gang *gang);
  236. void destroy_spu_context(struct kref *kref);
  237. struct spu_context * get_spu_context(struct spu_context *ctx);
  238. int put_spu_context(struct spu_context *ctx);
  239. void spu_unmap_mappings(struct spu_context *ctx);
  240. void spu_forget(struct spu_context *ctx);
  241. int __must_check spu_acquire_saved(struct spu_context *ctx);
  242. void spu_release_saved(struct spu_context *ctx);
  243. int spu_stopped(struct spu_context *ctx, u32 * stat);
  244. void spu_del_from_rq(struct spu_context *ctx);
  245. int spu_activate(struct spu_context *ctx, unsigned long flags);
  246. void spu_deactivate(struct spu_context *ctx);
  247. void spu_yield(struct spu_context *ctx);
  248. void spu_switch_notify(struct spu *spu, struct spu_context *ctx);
  249. void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
  250. u32 type, u32 val);
  251. void spu_set_timeslice(struct spu_context *ctx);
  252. void spu_update_sched_info(struct spu_context *ctx);
  253. void __spu_update_sched_info(struct spu_context *ctx);
  254. int __init spu_sched_init(void);
  255. void spu_sched_exit(void);
  256. extern char *isolated_loader;
  257. /*
  258. * spufs_wait
  259. * Same as wait_event_interruptible(), except that here
  260. * we need to call spu_release(ctx) before sleeping, and
  261. * then spu_acquire(ctx) when awoken.
  262. *
  263. * Returns with state_mutex re-acquired when successfull or
  264. * with -ERESTARTSYS and the state_mutex dropped when interrupted.
  265. */
  266. #define spufs_wait(wq, condition) \
  267. ({ \
  268. int __ret = 0; \
  269. DEFINE_WAIT(__wait); \
  270. for (;;) { \
  271. prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
  272. if (condition) \
  273. break; \
  274. spu_release(ctx); \
  275. if (signal_pending(current)) { \
  276. __ret = -ERESTARTSYS; \
  277. break; \
  278. } \
  279. schedule(); \
  280. __ret = spu_acquire(ctx); \
  281. if (__ret) \
  282. break; \
  283. } \
  284. finish_wait(&(wq), &__wait); \
  285. __ret; \
  286. })
  287. size_t spu_wbox_write(struct spu_context *ctx, u32 data);
  288. size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
  289. /* irq callback funcs. */
  290. void spufs_ibox_callback(struct spu *spu);
  291. void spufs_wbox_callback(struct spu *spu);
  292. void spufs_stop_callback(struct spu *spu);
  293. void spufs_mfc_callback(struct spu *spu);
  294. void spufs_dma_callback(struct spu *spu, int type);
  295. extern struct spu_coredump_calls spufs_coredump_calls;
  296. struct spufs_coredump_reader {
  297. char *name;
  298. ssize_t (*read)(struct spu_context *ctx,
  299. char __user *buffer, size_t size, loff_t *pos);
  300. u64 (*get)(struct spu_context *ctx);
  301. size_t size;
  302. };
  303. extern struct spufs_coredump_reader spufs_coredump_read[];
  304. extern int spufs_coredump_num_notes;
  305. extern int spu_init_csa(struct spu_state *csa);
  306. extern void spu_fini_csa(struct spu_state *csa);
  307. extern int spu_save(struct spu_state *prev, struct spu *spu);
  308. extern int spu_restore(struct spu_state *new, struct spu *spu);
  309. extern int spu_switch(struct spu_state *prev, struct spu_state *new,
  310. struct spu *spu);
  311. extern int spu_alloc_lscsa(struct spu_state *csa);
  312. extern void spu_free_lscsa(struct spu_state *csa);
  313. extern void spuctx_switch_state(struct spu_context *ctx,
  314. enum spu_utilization_state new_state);
  315. #define spu_context_trace(name, ctx, spu) \
  316. trace_mark(name, "ctx %p spu %p", ctx, spu);
  317. #define spu_context_nospu_trace(name, ctx) \
  318. trace_mark(name, "ctx %p", ctx);
  319. #endif