spufs.h 12 KB

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