1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* hw_ops.c - query/set operations on active SPU context.
4 * Copyright (C) IBM 2005
5 * Author: Mark Nutter <mnutter@us.ibm.com>
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
12 #include <linux/poll.h>
13 #include <linux/smp.h>
14 #include <linux/stddef.h>
15 #include <linux/unistd.h>
19 #include <asm/spu_priv1.h>
20 #include <asm/spu_csa.h>
21 #include <asm/mmu_context.h>
24 static int spu_hw_mbox_read(struct spu_context
*ctx
, u32
* data
)
26 struct spu
*spu
= ctx
->spu
;
27 struct spu_problem __iomem
*prob
= spu
->problem
;
31 spin_lock_irq(&spu
->register_lock
);
32 mbox_stat
= in_be32(&prob
->mb_stat_R
);
33 if (mbox_stat
& 0x0000ff) {
34 *data
= in_be32(&prob
->pu_mb_R
);
37 spin_unlock_irq(&spu
->register_lock
);
41 static u32
spu_hw_mbox_stat_read(struct spu_context
*ctx
)
43 return in_be32(&ctx
->spu
->problem
->mb_stat_R
);
46 static __poll_t
spu_hw_mbox_stat_poll(struct spu_context
*ctx
, __poll_t events
)
48 struct spu
*spu
= ctx
->spu
;
52 spin_lock_irq(&spu
->register_lock
);
53 stat
= in_be32(&spu
->problem
->mb_stat_R
);
55 /* if the requested event is there, return the poll
56 mask, otherwise enable the interrupt to get notified,
57 but first mark any pending interrupts as done so
58 we don't get woken up unnecessarily */
60 if (events
& (EPOLLIN
| EPOLLRDNORM
)) {
62 ret
|= EPOLLIN
| EPOLLRDNORM
;
64 spu_int_stat_clear(spu
, 2, CLASS2_MAILBOX_INTR
);
65 spu_int_mask_or(spu
, 2, CLASS2_ENABLE_MAILBOX_INTR
);
68 if (events
& (EPOLLOUT
| EPOLLWRNORM
)) {
70 ret
= EPOLLOUT
| EPOLLWRNORM
;
72 spu_int_stat_clear(spu
, 2,
73 CLASS2_MAILBOX_THRESHOLD_INTR
);
74 spu_int_mask_or(spu
, 2,
75 CLASS2_ENABLE_MAILBOX_THRESHOLD_INTR
);
78 spin_unlock_irq(&spu
->register_lock
);
82 static int spu_hw_ibox_read(struct spu_context
*ctx
, u32
* data
)
84 struct spu
*spu
= ctx
->spu
;
85 struct spu_problem __iomem
*prob
= spu
->problem
;
86 struct spu_priv2 __iomem
*priv2
= spu
->priv2
;
89 spin_lock_irq(&spu
->register_lock
);
90 if (in_be32(&prob
->mb_stat_R
) & 0xff0000) {
91 /* read the first available word */
92 *data
= in_be64(&priv2
->puint_mb_R
);
95 /* make sure we get woken up by the interrupt */
96 spu_int_mask_or(spu
, 2, CLASS2_ENABLE_MAILBOX_INTR
);
99 spin_unlock_irq(&spu
->register_lock
);
103 static int spu_hw_wbox_write(struct spu_context
*ctx
, u32 data
)
105 struct spu
*spu
= ctx
->spu
;
106 struct spu_problem __iomem
*prob
= spu
->problem
;
109 spin_lock_irq(&spu
->register_lock
);
110 if (in_be32(&prob
->mb_stat_R
) & 0x00ff00) {
111 /* we have space to write wbox_data to */
112 out_be32(&prob
->spu_mb_W
, data
);
115 /* make sure we get woken up by the interrupt when space
117 spu_int_mask_or(spu
, 2, CLASS2_ENABLE_MAILBOX_THRESHOLD_INTR
);
120 spin_unlock_irq(&spu
->register_lock
);
124 static void spu_hw_signal1_write(struct spu_context
*ctx
, u32 data
)
126 out_be32(&ctx
->spu
->problem
->signal_notify1
, data
);
129 static void spu_hw_signal2_write(struct spu_context
*ctx
, u32 data
)
131 out_be32(&ctx
->spu
->problem
->signal_notify2
, data
);
134 static void spu_hw_signal1_type_set(struct spu_context
*ctx
, u64 val
)
136 struct spu
*spu
= ctx
->spu
;
137 struct spu_priv2 __iomem
*priv2
= spu
->priv2
;
140 spin_lock_irq(&spu
->register_lock
);
141 tmp
= in_be64(&priv2
->spu_cfg_RW
);
146 out_be64(&priv2
->spu_cfg_RW
, tmp
);
147 spin_unlock_irq(&spu
->register_lock
);
150 static u64
spu_hw_signal1_type_get(struct spu_context
*ctx
)
152 return ((in_be64(&ctx
->spu
->priv2
->spu_cfg_RW
) & 1) != 0);
155 static void spu_hw_signal2_type_set(struct spu_context
*ctx
, u64 val
)
157 struct spu
*spu
= ctx
->spu
;
158 struct spu_priv2 __iomem
*priv2
= spu
->priv2
;
161 spin_lock_irq(&spu
->register_lock
);
162 tmp
= in_be64(&priv2
->spu_cfg_RW
);
167 out_be64(&priv2
->spu_cfg_RW
, tmp
);
168 spin_unlock_irq(&spu
->register_lock
);
171 static u64
spu_hw_signal2_type_get(struct spu_context
*ctx
)
173 return ((in_be64(&ctx
->spu
->priv2
->spu_cfg_RW
) & 2) != 0);
176 static u32
spu_hw_npc_read(struct spu_context
*ctx
)
178 return in_be32(&ctx
->spu
->problem
->spu_npc_RW
);
181 static void spu_hw_npc_write(struct spu_context
*ctx
, u32 val
)
183 out_be32(&ctx
->spu
->problem
->spu_npc_RW
, val
);
186 static u32
spu_hw_status_read(struct spu_context
*ctx
)
188 return in_be32(&ctx
->spu
->problem
->spu_status_R
);
191 static char *spu_hw_get_ls(struct spu_context
*ctx
)
193 return ctx
->spu
->local_store
;
196 static void spu_hw_privcntl_write(struct spu_context
*ctx
, u64 val
)
198 out_be64(&ctx
->spu
->priv2
->spu_privcntl_RW
, val
);
201 static u32
spu_hw_runcntl_read(struct spu_context
*ctx
)
203 return in_be32(&ctx
->spu
->problem
->spu_runcntl_RW
);
206 static void spu_hw_runcntl_write(struct spu_context
*ctx
, u32 val
)
208 spin_lock_irq(&ctx
->spu
->register_lock
);
209 if (val
& SPU_RUNCNTL_ISOLATE
)
210 spu_hw_privcntl_write(ctx
,
211 SPU_PRIVCNT_LOAD_REQUEST_ENABLE_MASK
);
212 out_be32(&ctx
->spu
->problem
->spu_runcntl_RW
, val
);
213 spin_unlock_irq(&ctx
->spu
->register_lock
);
216 static void spu_hw_runcntl_stop(struct spu_context
*ctx
)
218 spin_lock_irq(&ctx
->spu
->register_lock
);
219 out_be32(&ctx
->spu
->problem
->spu_runcntl_RW
, SPU_RUNCNTL_STOP
);
220 while (in_be32(&ctx
->spu
->problem
->spu_status_R
) & SPU_STATUS_RUNNING
)
222 spin_unlock_irq(&ctx
->spu
->register_lock
);
225 static void spu_hw_master_start(struct spu_context
*ctx
)
227 struct spu
*spu
= ctx
->spu
;
230 spin_lock_irq(&spu
->register_lock
);
231 sr1
= spu_mfc_sr1_get(spu
) | MFC_STATE1_MASTER_RUN_CONTROL_MASK
;
232 spu_mfc_sr1_set(spu
, sr1
);
233 spin_unlock_irq(&spu
->register_lock
);
236 static void spu_hw_master_stop(struct spu_context
*ctx
)
238 struct spu
*spu
= ctx
->spu
;
241 spin_lock_irq(&spu
->register_lock
);
242 sr1
= spu_mfc_sr1_get(spu
) & ~MFC_STATE1_MASTER_RUN_CONTROL_MASK
;
243 spu_mfc_sr1_set(spu
, sr1
);
244 spin_unlock_irq(&spu
->register_lock
);
247 static int spu_hw_set_mfc_query(struct spu_context
* ctx
, u32 mask
, u32 mode
)
249 struct spu_problem __iomem
*prob
= ctx
->spu
->problem
;
252 spin_lock_irq(&ctx
->spu
->register_lock
);
254 if (in_be32(&prob
->dma_querytype_RW
))
257 out_be32(&prob
->dma_querymask_RW
, mask
);
258 out_be32(&prob
->dma_querytype_RW
, mode
);
260 spin_unlock_irq(&ctx
->spu
->register_lock
);
264 static u32
spu_hw_read_mfc_tagstatus(struct spu_context
* ctx
)
266 return in_be32(&ctx
->spu
->problem
->dma_tagstatus_R
);
269 static u32
spu_hw_get_mfc_free_elements(struct spu_context
*ctx
)
271 return in_be32(&ctx
->spu
->problem
->dma_qstatus_R
);
274 static int spu_hw_send_mfc_command(struct spu_context
*ctx
,
275 struct mfc_dma_command
*cmd
)
278 struct spu_problem __iomem
*prob
= ctx
->spu
->problem
;
280 spin_lock_irq(&ctx
->spu
->register_lock
);
281 out_be32(&prob
->mfc_lsa_W
, cmd
->lsa
);
282 out_be64(&prob
->mfc_ea_W
, cmd
->ea
);
283 out_be32(&prob
->mfc_union_W
.by32
.mfc_size_tag32
,
284 cmd
->size
<< 16 | cmd
->tag
);
285 out_be32(&prob
->mfc_union_W
.by32
.mfc_class_cmd32
,
286 cmd
->class << 16 | cmd
->cmd
);
287 status
= in_be32(&prob
->mfc_union_W
.by32
.mfc_class_cmd32
);
288 spin_unlock_irq(&ctx
->spu
->register_lock
);
290 switch (status
& 0xffff) {
300 static void spu_hw_restart_dma(struct spu_context
*ctx
)
302 struct spu_priv2 __iomem
*priv2
= ctx
->spu
->priv2
;
304 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING
, &ctx
->spu
->flags
))
305 out_be64(&priv2
->mfc_control_RW
, MFC_CNTL_RESTART_DMA_COMMAND
);
308 struct spu_context_ops spu_hw_ops
= {
309 .mbox_read
= spu_hw_mbox_read
,
310 .mbox_stat_read
= spu_hw_mbox_stat_read
,
311 .mbox_stat_poll
= spu_hw_mbox_stat_poll
,
312 .ibox_read
= spu_hw_ibox_read
,
313 .wbox_write
= spu_hw_wbox_write
,
314 .signal1_write
= spu_hw_signal1_write
,
315 .signal2_write
= spu_hw_signal2_write
,
316 .signal1_type_set
= spu_hw_signal1_type_set
,
317 .signal1_type_get
= spu_hw_signal1_type_get
,
318 .signal2_type_set
= spu_hw_signal2_type_set
,
319 .signal2_type_get
= spu_hw_signal2_type_get
,
320 .npc_read
= spu_hw_npc_read
,
321 .npc_write
= spu_hw_npc_write
,
322 .status_read
= spu_hw_status_read
,
323 .get_ls
= spu_hw_get_ls
,
324 .privcntl_write
= spu_hw_privcntl_write
,
325 .runcntl_read
= spu_hw_runcntl_read
,
326 .runcntl_write
= spu_hw_runcntl_write
,
327 .runcntl_stop
= spu_hw_runcntl_stop
,
328 .master_start
= spu_hw_master_start
,
329 .master_stop
= spu_hw_master_stop
,
330 .set_mfc_query
= spu_hw_set_mfc_query
,
331 .read_mfc_tagstatus
= spu_hw_read_mfc_tagstatus
,
332 .get_mfc_free_elements
= spu_hw_get_mfc_free_elements
,
333 .send_mfc_command
= spu_hw_send_mfc_command
,
334 .restart_dma
= spu_hw_restart_dma
,