1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7 * Author: Arnd Bergmann <arndb@de.ibm.com>
12 #include <linux/kref.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock.h>
16 #include <linux/cpumask.h>
17 #include <linux/sched/signal.h>
20 #include <asm/spu_csa.h>
21 #include <asm/spu_info.h>
23 #define SPUFS_PS_MAP_SIZE 0x20000
24 #define SPUFS_MFC_MAP_SIZE 0x1000
25 #define SPUFS_CNTL_MAP_SIZE 0x1000
26 #define SPUFS_SIGNAL_MAP_SIZE PAGE_SIZE
27 #define SPUFS_MSS_MAP_SIZE 0x1000
29 /* The magic number for our file system */
31 SPUFS_MAGIC
= 0x23c9b64e,
34 struct spu_context_ops
;
37 /* ctx->sched_flags */
39 SPU_SCHED_NOTIFY_ACTIVE
,
40 SPU_SCHED_WAS_ACTIVE
, /* was active upon spu_acquire_saved() */
41 SPU_SCHED_SPU_RUN
, /* context is within spu_run */
45 SWITCH_LOG_BUFSIZE
= 4096,
55 wait_queue_head_t wait
;
58 struct switch_log_entry
{
59 struct timespec64 tstamp
;
68 struct spu
*spu
; /* pointer to a physical SPU */
69 struct spu_state csa
; /* SPU context save area. */
70 spinlock_t mmio_lock
; /* protects mmio access */
71 struct address_space
*local_store
; /* local store mapping. */
72 struct address_space
*mfc
; /* 'mfc' area mappings. */
73 struct address_space
*cntl
; /* 'control' area mappings. */
74 struct address_space
*signal1
; /* 'signal1' area mappings. */
75 struct address_space
*signal2
; /* 'signal2' area mappings. */
76 struct address_space
*mss
; /* 'mss' area mappings. */
77 struct address_space
*psmap
; /* 'psmap' area mappings. */
78 struct mutex mapping_lock
;
79 u64 object_id
; /* user space pointer for oprofile */
81 enum { SPU_STATE_RUNNABLE
, SPU_STATE_SAVED
} state
;
82 struct mutex state_mutex
;
83 struct mutex run_mutex
;
85 struct mm_struct
*owner
;
88 wait_queue_head_t ibox_wq
;
89 wait_queue_head_t wbox_wq
;
90 wait_queue_head_t stop_wq
;
91 wait_queue_head_t mfc_wq
;
92 wait_queue_head_t run_wq
;
94 struct spu_context_ops
*ops
;
95 struct work_struct reap_work
;
97 unsigned long event_return
;
99 struct list_head gang_list
;
100 struct spu_gang
*gang
;
101 struct kref
*prof_priv_kref
;
102 void ( * prof_priv_release
) (struct kref
*kref
);
107 /* scheduler fields */
109 unsigned int time_slice
;
110 unsigned long sched_flags
;
111 cpumask_t cpus_allowed
;
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
;
134 /* context switch log */
135 struct switch_log
*switch_log
;
137 struct list_head aff_list
;
143 struct list_head list
;
148 struct spu_context
*aff_ref_ctx
;
149 struct list_head aff_list_head
;
150 struct mutex aff_mutex
;
152 struct spu
*aff_ref_spu
;
153 atomic_t aff_sched_count
;
156 /* Flag bits for spu_gang aff_flags */
157 #define AFF_OFFSETS_SET 1
160 struct mfc_dma_command
{
161 int32_t pad
; /* reserved */
162 uint32_t lsa
; /* local storage address */
163 uint64_t ea
; /* effective address */
164 uint16_t size
; /* transfer size */
165 uint16_t tag
; /* command tag */
166 uint16_t class; /* class ID */
167 uint16_t cmd
; /* command opcode */
171 /* SPU context query/set operations. */
172 struct spu_context_ops
{
173 int (*mbox_read
) (struct spu_context
* ctx
, u32
* data
);
174 u32(*mbox_stat_read
) (struct spu_context
* ctx
);
175 __poll_t (*mbox_stat_poll
)(struct spu_context
*ctx
, __poll_t events
);
176 int (*ibox_read
) (struct spu_context
* ctx
, u32
* data
);
177 int (*wbox_write
) (struct spu_context
* ctx
, u32 data
);
178 u32(*signal1_read
) (struct spu_context
* ctx
);
179 void (*signal1_write
) (struct spu_context
* ctx
, u32 data
);
180 u32(*signal2_read
) (struct spu_context
* ctx
);
181 void (*signal2_write
) (struct spu_context
* ctx
, u32 data
);
182 void (*signal1_type_set
) (struct spu_context
* ctx
, u64 val
);
183 u64(*signal1_type_get
) (struct spu_context
* ctx
);
184 void (*signal2_type_set
) (struct spu_context
* ctx
, u64 val
);
185 u64(*signal2_type_get
) (struct spu_context
* ctx
);
186 u32(*npc_read
) (struct spu_context
* ctx
);
187 void (*npc_write
) (struct spu_context
* ctx
, u32 data
);
188 u32(*status_read
) (struct spu_context
* ctx
);
189 char*(*get_ls
) (struct spu_context
* ctx
);
190 void (*privcntl_write
) (struct spu_context
*ctx
, u64 data
);
191 u32 (*runcntl_read
) (struct spu_context
* ctx
);
192 void (*runcntl_write
) (struct spu_context
* ctx
, u32 data
);
193 void (*runcntl_stop
) (struct spu_context
* ctx
);
194 void (*master_start
) (struct spu_context
* ctx
);
195 void (*master_stop
) (struct spu_context
* ctx
);
196 int (*set_mfc_query
)(struct spu_context
* ctx
, u32 mask
, u32 mode
);
197 u32 (*read_mfc_tagstatus
)(struct spu_context
* ctx
);
198 u32 (*get_mfc_free_elements
)(struct spu_context
*ctx
);
199 int (*send_mfc_command
)(struct spu_context
* ctx
,
200 struct mfc_dma_command
* cmd
);
201 void (*dma_info_read
) (struct spu_context
* ctx
,
202 struct spu_dma_info
* info
);
203 void (*proxydma_info_read
) (struct spu_context
* ctx
,
204 struct spu_proxydma_info
* info
);
205 void (*restart_dma
)(struct spu_context
*ctx
);
208 extern struct spu_context_ops spu_hw_ops
;
209 extern struct spu_context_ops spu_backing_ops
;
211 struct spufs_inode_info
{
212 struct spu_context
*i_ctx
;
213 struct spu_gang
*i_gang
;
214 struct inode vfs_inode
;
217 #define SPUFS_I(inode) \
218 container_of(inode, struct spufs_inode_info, vfs_inode)
220 struct spufs_tree_descr
{
222 const struct file_operations
*ops
;
227 extern const struct spufs_tree_descr spufs_dir_contents
[];
228 extern const struct spufs_tree_descr spufs_dir_nosched_contents
[];
229 extern const struct spufs_tree_descr spufs_dir_debug_contents
[];
231 /* system call implementation */
232 extern struct spufs_calls spufs_calls
;
233 struct coredump_params
;
234 long spufs_run_spu(struct spu_context
*ctx
, u32
*npc
, u32
*status
);
235 long spufs_create(struct path
*nd
, struct dentry
*dentry
, unsigned int flags
,
236 umode_t mode
, struct file
*filp
);
237 /* ELF coredump callbacks for writing SPU ELF notes */
238 extern int spufs_coredump_extra_notes_size(void);
239 extern int spufs_coredump_extra_notes_write(struct coredump_params
*cprm
);
241 extern const struct file_operations spufs_context_fops
;
243 /* gang management */
244 struct spu_gang
*alloc_spu_gang(void);
245 struct spu_gang
*get_spu_gang(struct spu_gang
*gang
);
246 int put_spu_gang(struct spu_gang
*gang
);
247 void spu_gang_remove_ctx(struct spu_gang
*gang
, struct spu_context
*ctx
);
248 void spu_gang_add_ctx(struct spu_gang
*gang
, struct spu_context
*ctx
);
251 int spufs_handle_class1(struct spu_context
*ctx
);
252 int spufs_handle_class0(struct spu_context
*ctx
);
255 struct spu
*affinity_check(struct spu_context
*ctx
);
257 /* context management */
258 extern atomic_t nr_spu_contexts
;
259 static inline int __must_check
spu_acquire(struct spu_context
*ctx
)
261 return mutex_lock_interruptible(&ctx
->state_mutex
);
264 static inline void spu_release(struct spu_context
*ctx
)
266 mutex_unlock(&ctx
->state_mutex
);
269 struct spu_context
* alloc_spu_context(struct spu_gang
*gang
);
270 void destroy_spu_context(struct kref
*kref
);
271 struct spu_context
* get_spu_context(struct spu_context
*ctx
);
272 int put_spu_context(struct spu_context
*ctx
);
273 void spu_unmap_mappings(struct spu_context
*ctx
);
275 void spu_forget(struct spu_context
*ctx
);
276 int __must_check
spu_acquire_saved(struct spu_context
*ctx
);
277 void spu_release_saved(struct spu_context
*ctx
);
279 int spu_stopped(struct spu_context
*ctx
, u32
* stat
);
280 void spu_del_from_rq(struct spu_context
*ctx
);
281 int spu_activate(struct spu_context
*ctx
, unsigned long flags
);
282 void spu_deactivate(struct spu_context
*ctx
);
283 void spu_yield(struct spu_context
*ctx
);
284 void spu_switch_notify(struct spu
*spu
, struct spu_context
*ctx
);
285 void spu_switch_log_notify(struct spu
*spu
, struct spu_context
*ctx
,
287 void spu_set_timeslice(struct spu_context
*ctx
);
288 void spu_update_sched_info(struct spu_context
*ctx
);
289 void __spu_update_sched_info(struct spu_context
*ctx
);
290 int __init
spu_sched_init(void);
291 void spu_sched_exit(void);
293 extern char *isolated_loader
;
297 * Same as wait_event_interruptible(), except that here
298 * we need to call spu_release(ctx) before sleeping, and
299 * then spu_acquire(ctx) when awoken.
301 * Returns with state_mutex re-acquired when successful or
302 * with -ERESTARTSYS and the state_mutex dropped when interrupted.
305 #define spufs_wait(wq, condition) \
308 DEFINE_WAIT(__wait); \
310 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
314 if (signal_pending(current)) { \
315 __ret = -ERESTARTSYS; \
319 __ret = spu_acquire(ctx); \
323 finish_wait(&(wq), &__wait); \
327 size_t spu_wbox_write(struct spu_context
*ctx
, u32 data
);
328 size_t spu_ibox_read(struct spu_context
*ctx
, u32
*data
);
330 /* irq callback funcs. */
331 void spufs_ibox_callback(struct spu
*spu
);
332 void spufs_wbox_callback(struct spu
*spu
);
333 void spufs_stop_callback(struct spu
*spu
, int irq
);
334 void spufs_mfc_callback(struct spu
*spu
);
335 void spufs_dma_callback(struct spu
*spu
, int type
);
337 extern struct spu_coredump_calls spufs_coredump_calls
;
338 struct spufs_coredump_reader
{
340 ssize_t (*read
)(struct spu_context
*ctx
,
341 char __user
*buffer
, size_t size
, loff_t
*pos
);
342 u64 (*get
)(struct spu_context
*ctx
);
345 extern const struct spufs_coredump_reader spufs_coredump_read
[];
346 extern int spufs_coredump_num_notes
;
348 extern int spu_init_csa(struct spu_state
*csa
);
349 extern void spu_fini_csa(struct spu_state
*csa
);
350 extern int spu_save(struct spu_state
*prev
, struct spu
*spu
);
351 extern int spu_restore(struct spu_state
*new, struct spu
*spu
);
352 extern int spu_switch(struct spu_state
*prev
, struct spu_state
*new,
354 extern int spu_alloc_lscsa(struct spu_state
*csa
);
355 extern void spu_free_lscsa(struct spu_state
*csa
);
357 extern void spuctx_switch_state(struct spu_context
*ctx
,
358 enum spu_utilization_state new_state
);