spufs.h 11 KB

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