spufs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. int last_ran;
  110. /* statistics */
  111. struct {
  112. /* updates protected by ctx->state_mutex */
  113. enum spu_utilization_state util_state;
  114. unsigned long long tstamp; /* time of last state switch */
  115. unsigned long long times[SPU_UTIL_MAX];
  116. unsigned long long vol_ctx_switch;
  117. unsigned long long invol_ctx_switch;
  118. unsigned long long min_flt;
  119. unsigned long long maj_flt;
  120. unsigned long long hash_flt;
  121. unsigned long long slb_flt;
  122. unsigned long long slb_flt_base; /* # at last ctx switch */
  123. unsigned long long class2_intr;
  124. unsigned long long class2_intr_base; /* # at last ctx switch */
  125. unsigned long long libassist;
  126. } stats;
  127. /* context switch log */
  128. struct switch_log *switch_log;
  129. struct list_head aff_list;
  130. int aff_head;
  131. int aff_offset;
  132. };
  133. struct spu_gang {
  134. struct list_head list;
  135. struct mutex mutex;
  136. struct kref kref;
  137. int contexts;
  138. struct spu_context *aff_ref_ctx;
  139. struct list_head aff_list_head;
  140. struct mutex aff_mutex;
  141. int aff_flags;
  142. struct spu *aff_ref_spu;
  143. atomic_t aff_sched_count;
  144. };
  145. /* Flag bits for spu_gang aff_flags */
  146. #define AFF_OFFSETS_SET 1
  147. #define AFF_MERGED 2
  148. struct mfc_dma_command {
  149. int32_t pad; /* reserved */
  150. uint32_t lsa; /* local storage address */
  151. uint64_t ea; /* effective address */
  152. uint16_t size; /* transfer size */
  153. uint16_t tag; /* command tag */
  154. uint16_t class; /* class ID */
  155. uint16_t cmd; /* command opcode */
  156. };
  157. /* SPU context query/set operations. */
  158. struct spu_context_ops {
  159. int (*mbox_read) (struct spu_context * ctx, u32 * data);
  160. u32(*mbox_stat_read) (struct spu_context * ctx);
  161. unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
  162. unsigned int events);
  163. int (*ibox_read) (struct spu_context * ctx, u32 * data);
  164. int (*wbox_write) (struct spu_context * ctx, u32 data);
  165. u32(*signal1_read) (struct spu_context * ctx);
  166. void (*signal1_write) (struct spu_context * ctx, u32 data);
  167. u32(*signal2_read) (struct spu_context * ctx);
  168. void (*signal2_write) (struct spu_context * ctx, u32 data);
  169. void (*signal1_type_set) (struct spu_context * ctx, u64 val);
  170. u64(*signal1_type_get) (struct spu_context * ctx);
  171. void (*signal2_type_set) (struct spu_context * ctx, u64 val);
  172. u64(*signal2_type_get) (struct spu_context * ctx);
  173. u32(*npc_read) (struct spu_context * ctx);
  174. void (*npc_write) (struct spu_context * ctx, u32 data);
  175. u32(*status_read) (struct spu_context * ctx);
  176. char*(*get_ls) (struct spu_context * ctx);
  177. void (*privcntl_write) (struct spu_context *ctx, u64 data);
  178. u32 (*runcntl_read) (struct spu_context * ctx);
  179. void (*runcntl_write) (struct spu_context * ctx, u32 data);
  180. void (*runcntl_stop) (struct spu_context * ctx);
  181. void (*master_start) (struct spu_context * ctx);
  182. void (*master_stop) (struct spu_context * ctx);
  183. int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
  184. u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
  185. u32 (*get_mfc_free_elements)(struct spu_context *ctx);
  186. int (*send_mfc_command)(struct spu_context * ctx,
  187. struct mfc_dma_command * cmd);
  188. void (*dma_info_read) (struct spu_context * ctx,
  189. struct spu_dma_info * info);
  190. void (*proxydma_info_read) (struct spu_context * ctx,
  191. struct spu_proxydma_info * info);
  192. void (*restart_dma)(struct spu_context *ctx);
  193. };
  194. extern struct spu_context_ops spu_hw_ops;
  195. extern struct spu_context_ops spu_backing_ops;
  196. struct spufs_inode_info {
  197. struct spu_context *i_ctx;
  198. struct spu_gang *i_gang;
  199. struct inode vfs_inode;
  200. int i_openers;
  201. };
  202. #define SPUFS_I(inode) \
  203. container_of(inode, struct spufs_inode_info, vfs_inode)
  204. extern struct tree_descr spufs_dir_contents[];
  205. extern struct tree_descr spufs_dir_nosched_contents[];
  206. /* system call implementation */
  207. extern struct spufs_calls spufs_calls;
  208. long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *status);
  209. long spufs_create(struct nameidata *nd, unsigned int flags,
  210. mode_t mode, struct file *filp);
  211. /* ELF coredump callbacks for writing SPU ELF notes */
  212. extern int spufs_coredump_extra_notes_size(void);
  213. extern int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset);
  214. extern const struct file_operations spufs_context_fops;
  215. /* gang management */
  216. struct spu_gang *alloc_spu_gang(void);
  217. struct spu_gang *get_spu_gang(struct spu_gang *gang);
  218. int put_spu_gang(struct spu_gang *gang);
  219. void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
  220. void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
  221. /* fault handling */
  222. int spufs_handle_class1(struct spu_context *ctx);
  223. int spufs_handle_class0(struct spu_context *ctx);
  224. /* affinity */
  225. struct spu *affinity_check(struct spu_context *ctx);
  226. /* context management */
  227. extern atomic_t nr_spu_contexts;
  228. static inline int __must_check spu_acquire(struct spu_context *ctx)
  229. {
  230. return mutex_lock_interruptible(&ctx->state_mutex);
  231. }
  232. static inline void spu_release(struct spu_context *ctx)
  233. {
  234. mutex_unlock(&ctx->state_mutex);
  235. }
  236. struct spu_context * alloc_spu_context(struct spu_gang *gang);
  237. void destroy_spu_context(struct kref *kref);
  238. struct spu_context * get_spu_context(struct spu_context *ctx);
  239. int put_spu_context(struct spu_context *ctx);
  240. void spu_unmap_mappings(struct spu_context *ctx);
  241. void spu_forget(struct spu_context *ctx);
  242. int __must_check spu_acquire_saved(struct spu_context *ctx);
  243. void spu_release_saved(struct spu_context *ctx);
  244. int spu_stopped(struct spu_context *ctx, u32 * stat);
  245. void spu_del_from_rq(struct spu_context *ctx);
  246. int spu_activate(struct spu_context *ctx, unsigned long flags);
  247. void spu_deactivate(struct spu_context *ctx);
  248. void spu_yield(struct spu_context *ctx);
  249. void spu_switch_notify(struct spu *spu, struct spu_context *ctx);
  250. void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
  251. u32 type, u32 val);
  252. void spu_set_timeslice(struct spu_context *ctx);
  253. void spu_update_sched_info(struct spu_context *ctx);
  254. void __spu_update_sched_info(struct spu_context *ctx);
  255. int __init spu_sched_init(void);
  256. void spu_sched_exit(void);
  257. extern char *isolated_loader;
  258. /*
  259. * spufs_wait
  260. * Same as wait_event_interruptible(), except that here
  261. * we need to call spu_release(ctx) before sleeping, and
  262. * then spu_acquire(ctx) when awoken.
  263. *
  264. * Returns with state_mutex re-acquired when successfull or
  265. * with -ERESTARTSYS and the state_mutex dropped when interrupted.
  266. */
  267. #define spufs_wait(wq, condition) \
  268. ({ \
  269. int __ret = 0; \
  270. DEFINE_WAIT(__wait); \
  271. for (;;) { \
  272. prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
  273. if (condition) \
  274. break; \
  275. spu_release(ctx); \
  276. if (signal_pending(current)) { \
  277. __ret = -ERESTARTSYS; \
  278. break; \
  279. } \
  280. schedule(); \
  281. __ret = spu_acquire(ctx); \
  282. if (__ret) \
  283. break; \
  284. } \
  285. finish_wait(&(wq), &__wait); \
  286. __ret; \
  287. })
  288. size_t spu_wbox_write(struct spu_context *ctx, u32 data);
  289. size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
  290. /* irq callback funcs. */
  291. void spufs_ibox_callback(struct spu *spu);
  292. void spufs_wbox_callback(struct spu *spu);
  293. void spufs_stop_callback(struct spu *spu, int irq);
  294. void spufs_mfc_callback(struct spu *spu);
  295. void spufs_dma_callback(struct spu *spu, int type);
  296. extern struct spu_coredump_calls spufs_coredump_calls;
  297. struct spufs_coredump_reader {
  298. char *name;
  299. ssize_t (*read)(struct spu_context *ctx,
  300. char __user *buffer, size_t size, loff_t *pos);
  301. u64 (*get)(struct spu_context *ctx);
  302. size_t size;
  303. };
  304. extern struct spufs_coredump_reader spufs_coredump_read[];
  305. extern int spufs_coredump_num_notes;
  306. extern int spu_init_csa(struct spu_state *csa);
  307. extern void spu_fini_csa(struct spu_state *csa);
  308. extern int spu_save(struct spu_state *prev, struct spu *spu);
  309. extern int spu_restore(struct spu_state *new, struct spu *spu);
  310. extern int spu_switch(struct spu_state *prev, struct spu_state *new,
  311. struct spu *spu);
  312. extern int spu_alloc_lscsa(struct spu_state *csa);
  313. extern void spu_free_lscsa(struct spu_state *csa);
  314. extern void spuctx_switch_state(struct spu_context *ctx,
  315. enum spu_utilization_state new_state);
  316. #define spu_context_trace(name, ctx, spu) \
  317. trace_mark(name, "ctx %p spu %p", ctx, spu);
  318. #define spu_context_nospu_trace(name, ctx) \
  319. trace_mark(name, "ctx %p", ctx);
  320. #endif