spufs.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. struct spu_context {
  39. struct spu *spu; /* pointer to a physical SPU */
  40. struct spu_state csa; /* SPU context save area. */
  41. spinlock_t mmio_lock; /* protects mmio access */
  42. struct address_space *local_store; /* local store mapping. */
  43. struct address_space *mfc; /* 'mfc' area mappings. */
  44. struct address_space *cntl; /* 'control' area mappings. */
  45. struct address_space *signal1; /* 'signal1' area mappings. */
  46. struct address_space *signal2; /* 'signal2' area mappings. */
  47. struct address_space *mss; /* 'mss' area mappings. */
  48. struct address_space *psmap; /* 'psmap' area mappings. */
  49. struct mutex mapping_lock;
  50. u64 object_id; /* user space pointer for oprofile */
  51. enum { SPU_STATE_RUNNABLE, SPU_STATE_SAVED } state;
  52. struct mutex state_mutex;
  53. struct mutex run_mutex;
  54. struct mm_struct *owner;
  55. struct kref kref;
  56. wait_queue_head_t ibox_wq;
  57. wait_queue_head_t wbox_wq;
  58. wait_queue_head_t stop_wq;
  59. wait_queue_head_t mfc_wq;
  60. struct fasync_struct *ibox_fasync;
  61. struct fasync_struct *wbox_fasync;
  62. struct fasync_struct *mfc_fasync;
  63. u32 tagwait;
  64. struct spu_context_ops *ops;
  65. struct work_struct reap_work;
  66. unsigned long flags;
  67. unsigned long event_return;
  68. struct list_head gang_list;
  69. struct spu_gang *gang;
  70. /* owner thread */
  71. pid_t tid;
  72. /* scheduler fields */
  73. struct list_head rq;
  74. unsigned int time_slice;
  75. unsigned long sched_flags;
  76. cpumask_t cpus_allowed;
  77. int policy;
  78. int prio;
  79. /* statistics */
  80. struct {
  81. /* updates protected by ctx->state_mutex */
  82. enum spu_utilization_state util_state;
  83. unsigned long long tstamp; /* time of last state switch */
  84. unsigned long long times[SPU_UTIL_MAX];
  85. unsigned long long vol_ctx_switch;
  86. unsigned long long invol_ctx_switch;
  87. unsigned long long min_flt;
  88. unsigned long long maj_flt;
  89. unsigned long long hash_flt;
  90. unsigned long long slb_flt;
  91. unsigned long long slb_flt_base; /* # at last ctx switch */
  92. unsigned long long class2_intr;
  93. unsigned long long class2_intr_base; /* # at last ctx switch */
  94. unsigned long long libassist;
  95. } stats;
  96. };
  97. struct spu_gang {
  98. struct list_head list;
  99. struct mutex mutex;
  100. struct kref kref;
  101. int contexts;
  102. };
  103. struct mfc_dma_command {
  104. int32_t pad; /* reserved */
  105. uint32_t lsa; /* local storage address */
  106. uint64_t ea; /* effective address */
  107. uint16_t size; /* transfer size */
  108. uint16_t tag; /* command tag */
  109. uint16_t class; /* class ID */
  110. uint16_t cmd; /* command opcode */
  111. };
  112. /* SPU context query/set operations. */
  113. struct spu_context_ops {
  114. int (*mbox_read) (struct spu_context * ctx, u32 * data);
  115. u32(*mbox_stat_read) (struct spu_context * ctx);
  116. unsigned int (*mbox_stat_poll)(struct spu_context *ctx,
  117. unsigned int events);
  118. int (*ibox_read) (struct spu_context * ctx, u32 * data);
  119. int (*wbox_write) (struct spu_context * ctx, u32 data);
  120. u32(*signal1_read) (struct spu_context * ctx);
  121. void (*signal1_write) (struct spu_context * ctx, u32 data);
  122. u32(*signal2_read) (struct spu_context * ctx);
  123. void (*signal2_write) (struct spu_context * ctx, u32 data);
  124. void (*signal1_type_set) (struct spu_context * ctx, u64 val);
  125. u64(*signal1_type_get) (struct spu_context * ctx);
  126. void (*signal2_type_set) (struct spu_context * ctx, u64 val);
  127. u64(*signal2_type_get) (struct spu_context * ctx);
  128. u32(*npc_read) (struct spu_context * ctx);
  129. void (*npc_write) (struct spu_context * ctx, u32 data);
  130. u32(*status_read) (struct spu_context * ctx);
  131. char*(*get_ls) (struct spu_context * ctx);
  132. u32 (*runcntl_read) (struct spu_context * ctx);
  133. void (*runcntl_write) (struct spu_context * ctx, u32 data);
  134. void (*master_start) (struct spu_context * ctx);
  135. void (*master_stop) (struct spu_context * ctx);
  136. int (*set_mfc_query)(struct spu_context * ctx, u32 mask, u32 mode);
  137. u32 (*read_mfc_tagstatus)(struct spu_context * ctx);
  138. u32 (*get_mfc_free_elements)(struct spu_context *ctx);
  139. int (*send_mfc_command)(struct spu_context * ctx,
  140. struct mfc_dma_command * cmd);
  141. void (*dma_info_read) (struct spu_context * ctx,
  142. struct spu_dma_info * info);
  143. void (*proxydma_info_read) (struct spu_context * ctx,
  144. struct spu_proxydma_info * info);
  145. void (*restart_dma)(struct spu_context *ctx);
  146. };
  147. extern struct spu_context_ops spu_hw_ops;
  148. extern struct spu_context_ops spu_backing_ops;
  149. struct spufs_inode_info {
  150. struct spu_context *i_ctx;
  151. struct spu_gang *i_gang;
  152. struct inode vfs_inode;
  153. int i_openers;
  154. };
  155. #define SPUFS_I(inode) \
  156. container_of(inode, struct spufs_inode_info, vfs_inode)
  157. extern struct tree_descr spufs_dir_contents[];
  158. extern struct tree_descr spufs_dir_nosched_contents[];
  159. /* system call implementation */
  160. long spufs_run_spu(struct file *file,
  161. struct spu_context *ctx, u32 *npc, u32 *status);
  162. long spufs_create(struct nameidata *nd,
  163. unsigned int flags, mode_t mode);
  164. extern const struct file_operations spufs_context_fops;
  165. /* gang management */
  166. struct spu_gang *alloc_spu_gang(void);
  167. struct spu_gang *get_spu_gang(struct spu_gang *gang);
  168. int put_spu_gang(struct spu_gang *gang);
  169. void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx);
  170. void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx);
  171. /* fault handling */
  172. int spufs_handle_class1(struct spu_context *ctx);
  173. /* context management */
  174. extern atomic_t nr_spu_contexts;
  175. static inline void spu_acquire(struct spu_context *ctx)
  176. {
  177. mutex_lock(&ctx->state_mutex);
  178. }
  179. static inline void spu_release(struct spu_context *ctx)
  180. {
  181. mutex_unlock(&ctx->state_mutex);
  182. }
  183. struct spu_context * alloc_spu_context(struct spu_gang *gang);
  184. void destroy_spu_context(struct kref *kref);
  185. struct spu_context * get_spu_context(struct spu_context *ctx);
  186. int put_spu_context(struct spu_context *ctx);
  187. void spu_unmap_mappings(struct spu_context *ctx);
  188. void spu_forget(struct spu_context *ctx);
  189. int spu_acquire_runnable(struct spu_context *ctx, unsigned long flags);
  190. void spu_acquire_saved(struct spu_context *ctx);
  191. int spu_activate(struct spu_context *ctx, unsigned long flags);
  192. void spu_deactivate(struct spu_context *ctx);
  193. void spu_yield(struct spu_context *ctx);
  194. void spu_set_timeslice(struct spu_context *ctx);
  195. void spu_update_sched_info(struct spu_context *ctx);
  196. void __spu_update_sched_info(struct spu_context *ctx);
  197. int __init spu_sched_init(void);
  198. void spu_sched_exit(void);
  199. extern char *isolated_loader;
  200. /*
  201. * spufs_wait
  202. * Same as wait_event_interruptible(), except that here
  203. * we need to call spu_release(ctx) before sleeping, and
  204. * then spu_acquire(ctx) when awoken.
  205. */
  206. #define spufs_wait(wq, condition) \
  207. ({ \
  208. int __ret = 0; \
  209. DEFINE_WAIT(__wait); \
  210. for (;;) { \
  211. prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
  212. if (condition) \
  213. break; \
  214. if (signal_pending(current)) { \
  215. __ret = -ERESTARTSYS; \
  216. break; \
  217. } \
  218. spu_release(ctx); \
  219. schedule(); \
  220. spu_acquire(ctx); \
  221. } \
  222. finish_wait(&(wq), &__wait); \
  223. __ret; \
  224. })
  225. size_t spu_wbox_write(struct spu_context *ctx, u32 data);
  226. size_t spu_ibox_read(struct spu_context *ctx, u32 *data);
  227. /* irq callback funcs. */
  228. void spufs_ibox_callback(struct spu *spu);
  229. void spufs_wbox_callback(struct spu *spu);
  230. void spufs_stop_callback(struct spu *spu);
  231. void spufs_mfc_callback(struct spu *spu);
  232. void spufs_dma_callback(struct spu *spu, int type);
  233. extern struct spu_coredump_calls spufs_coredump_calls;
  234. struct spufs_coredump_reader {
  235. char *name;
  236. ssize_t (*read)(struct spu_context *ctx,
  237. char __user *buffer, size_t size, loff_t *pos);
  238. u64 (*get)(void *data);
  239. size_t size;
  240. };
  241. extern struct spufs_coredump_reader spufs_coredump_read[];
  242. extern int spufs_coredump_num_notes;
  243. /*
  244. * This function is a little bit too large for an inline, but
  245. * as fault.c is built into the kernel we can't move it out of
  246. * line.
  247. */
  248. static inline void spuctx_switch_state(struct spu_context *ctx,
  249. enum spu_utilization_state new_state)
  250. {
  251. unsigned long long curtime;
  252. signed long long delta;
  253. struct timespec ts;
  254. struct spu *spu;
  255. enum spu_utilization_state old_state;
  256. ktime_get_ts(&ts);
  257. curtime = timespec_to_ns(&ts);
  258. delta = curtime - ctx->stats.tstamp;
  259. WARN_ON(!mutex_is_locked(&ctx->state_mutex));
  260. WARN_ON(delta < 0);
  261. spu = ctx->spu;
  262. old_state = ctx->stats.util_state;
  263. ctx->stats.util_state = new_state;
  264. ctx->stats.tstamp = curtime;
  265. /*
  266. * Update the physical SPU utilization statistics.
  267. */
  268. if (spu) {
  269. ctx->stats.times[old_state] += delta;
  270. spu->stats.times[old_state] += delta;
  271. spu->stats.util_state = new_state;
  272. spu->stats.tstamp = curtime;
  273. }
  274. }
  275. #endif