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