Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / powerpc / platforms / cell / spufs / switch.c
blob99bcdf3041b8852ef16ed6cb66607371a39e1c1e
1 /*
2 * spu_switch.c
4 * (C) Copyright IBM Corp. 2005
6 * Author: Mark Nutter <mnutter@us.ibm.com>
8 * Host-side part of SPU context switch sequence outlined in
9 * Synergistic Processor Element, Book IV.
11 * A fully premptive switch of an SPE is very expensive in terms
12 * of time and system resources. SPE Book IV indicates that SPE
13 * allocation should follow a "serially reusable device" model,
14 * in which the SPE is assigned a task until it completes. When
15 * this is not possible, this sequence may be used to premptively
16 * save, and then later (optionally) restore the context of a
17 * program executing on an SPE.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2, or (at your option)
23 * any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 <<<<<<< HEAD:arch/powerpc/platforms/cell/spufs/switch.c
38 =======
39 #include <linux/hardirq.h>
40 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/powerpc/platforms/cell/spufs/switch.c
41 #include <linux/sched.h>
42 #include <linux/kernel.h>
43 #include <linux/mm.h>
44 #include <linux/vmalloc.h>
45 #include <linux/smp.h>
46 #include <linux/stddef.h>
47 #include <linux/unistd.h>
49 #include <asm/io.h>
50 #include <asm/spu.h>
51 #include <asm/spu_priv1.h>
52 #include <asm/spu_csa.h>
53 #include <asm/mmu_context.h>
55 #include "spufs.h"
57 #include "spu_save_dump.h"
58 #include "spu_restore_dump.h"
60 #if 0
61 #define POLL_WHILE_TRUE(_c) { \
62 do { \
63 } while (_c); \
65 #else
66 #define RELAX_SPIN_COUNT 1000
67 #define POLL_WHILE_TRUE(_c) { \
68 do { \
69 int _i; \
70 for (_i=0; _i<RELAX_SPIN_COUNT && (_c); _i++) { \
71 cpu_relax(); \
72 } \
73 if (unlikely(_c)) yield(); \
74 else break; \
75 } while (_c); \
77 #endif /* debug */
79 #define POLL_WHILE_FALSE(_c) POLL_WHILE_TRUE(!(_c))
81 static inline void acquire_spu_lock(struct spu *spu)
83 /* Save, Step 1:
84 * Restore, Step 1:
85 * Acquire SPU-specific mutual exclusion lock.
86 * TBD.
90 static inline void release_spu_lock(struct spu *spu)
92 /* Restore, Step 76:
93 * Release SPU-specific mutual exclusion lock.
94 * TBD.
98 static inline int check_spu_isolate(struct spu_state *csa, struct spu *spu)
100 struct spu_problem __iomem *prob = spu->problem;
101 u32 isolate_state;
103 /* Save, Step 2:
104 * Save, Step 6:
105 * If SPU_Status[E,L,IS] any field is '1', this
106 * SPU is in isolate state and cannot be context
107 * saved at this time.
109 isolate_state = SPU_STATUS_ISOLATED_STATE |
110 SPU_STATUS_ISOLATED_LOAD_STATUS | SPU_STATUS_ISOLATED_EXIT_STATUS;
111 return (in_be32(&prob->spu_status_R) & isolate_state) ? 1 : 0;
114 static inline void disable_interrupts(struct spu_state *csa, struct spu *spu)
116 /* Save, Step 3:
117 * Restore, Step 2:
118 * Save INT_Mask_class0 in CSA.
119 * Write INT_MASK_class0 with value of 0.
120 * Save INT_Mask_class1 in CSA.
121 * Write INT_MASK_class1 with value of 0.
122 * Save INT_Mask_class2 in CSA.
123 * Write INT_MASK_class2 with value of 0.
124 <<<<<<< HEAD:arch/powerpc/platforms/cell/spufs/switch.c
125 =======
126 * Synchronize all three interrupts to be sure
127 * we no longer execute a handler on another CPU.
128 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/powerpc/platforms/cell/spufs/switch.c
130 spin_lock_irq(&spu->register_lock);
131 if (csa) {
132 csa->priv1.int_mask_class0_RW = spu_int_mask_get(spu, 0);
133 csa->priv1.int_mask_class1_RW = spu_int_mask_get(spu, 1);
134 csa->priv1.int_mask_class2_RW = spu_int_mask_get(spu, 2);
136 spu_int_mask_set(spu, 0, 0ul);
137 spu_int_mask_set(spu, 1, 0ul);
138 spu_int_mask_set(spu, 2, 0ul);
139 eieio();
140 spin_unlock_irq(&spu->register_lock);
141 <<<<<<< HEAD:arch/powerpc/platforms/cell/spufs/switch.c
142 =======
143 synchronize_irq(spu->irqs[0]);
144 synchronize_irq(spu->irqs[1]);
145 synchronize_irq(spu->irqs[2]);
146 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/powerpc/platforms/cell/spufs/switch.c
149 static inline void set_watchdog_timer(struct spu_state *csa, struct spu *spu)
151 /* Save, Step 4:
152 * Restore, Step 25.
153 * Set a software watchdog timer, which specifies the
154 * maximum allowable time for a context save sequence.
156 * For present, this implementation will not set a global
157 * watchdog timer, as virtualization & variable system load
158 * may cause unpredictable execution times.
162 static inline void inhibit_user_access(struct spu_state *csa, struct spu *spu)
164 /* Save, Step 5:
165 * Restore, Step 3:
166 * Inhibit user-space access (if provided) to this
167 * SPU by unmapping the virtual pages assigned to
168 * the SPU memory-mapped I/O (MMIO) for problem
169 * state. TBD.
173 static inline void set_switch_pending(struct spu_state *csa, struct spu *spu)
175 /* Save, Step 7:
176 * Restore, Step 5:
177 * Set a software context switch pending flag.
179 set_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags);
180 mb();
183 static inline void save_mfc_cntl(struct spu_state *csa, struct spu *spu)
185 struct spu_priv2 __iomem *priv2 = spu->priv2;
187 /* Save, Step 8:
188 * Suspend DMA and save MFC_CNTL.
190 switch (in_be64(&priv2->mfc_control_RW) &
191 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) {
192 case MFC_CNTL_SUSPEND_IN_PROGRESS:
193 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
194 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) ==
195 MFC_CNTL_SUSPEND_COMPLETE);
196 /* fall through */
197 case MFC_CNTL_SUSPEND_COMPLETE:
198 if (csa) {
199 csa->priv2.mfc_control_RW =
200 MFC_CNTL_SUSPEND_MASK |
201 MFC_CNTL_SUSPEND_DMA_QUEUE;
203 break;
204 case MFC_CNTL_NORMAL_DMA_QUEUE_OPERATION:
205 out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE);
206 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
207 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) ==
208 MFC_CNTL_SUSPEND_COMPLETE);
209 if (csa) {
210 csa->priv2.mfc_control_RW = 0;
212 break;
216 static inline void save_spu_runcntl(struct spu_state *csa, struct spu *spu)
218 struct spu_problem __iomem *prob = spu->problem;
220 /* Save, Step 9:
221 * Save SPU_Runcntl in the CSA. This value contains
222 * the "Application Desired State".
224 csa->prob.spu_runcntl_RW = in_be32(&prob->spu_runcntl_RW);
227 static inline void save_mfc_sr1(struct spu_state *csa, struct spu *spu)
229 /* Save, Step 10:
230 * Save MFC_SR1 in the CSA.
232 csa->priv1.mfc_sr1_RW = spu_mfc_sr1_get(spu);
235 static inline void save_spu_status(struct spu_state *csa, struct spu *spu)
237 struct spu_problem __iomem *prob = spu->problem;
239 /* Save, Step 11:
240 * Read SPU_Status[R], and save to CSA.
242 if ((in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) == 0) {
243 csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
244 } else {
245 u32 stopped;
247 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
248 eieio();
249 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
250 SPU_STATUS_RUNNING);
251 stopped =
252 SPU_STATUS_INVALID_INSTR | SPU_STATUS_SINGLE_STEP |
253 SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
254 if ((in_be32(&prob->spu_status_R) & stopped) == 0)
255 csa->prob.spu_status_R = SPU_STATUS_RUNNING;
256 else
257 csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
261 static inline void save_mfc_decr(struct spu_state *csa, struct spu *spu)
263 struct spu_priv2 __iomem *priv2 = spu->priv2;
265 /* Save, Step 12:
266 * Read MFC_CNTL[Ds]. Update saved copy of
267 * CSA.MFC_CNTL[Ds].
269 csa->priv2.mfc_control_RW |=
270 in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DECREMENTER_RUNNING;
273 static inline void halt_mfc_decr(struct spu_state *csa, struct spu *spu)
275 struct spu_priv2 __iomem *priv2 = spu->priv2;
277 /* Save, Step 13:
278 * Write MFC_CNTL[Dh] set to a '1' to halt
279 * the decrementer.
281 out_be64(&priv2->mfc_control_RW,
282 MFC_CNTL_DECREMENTER_HALTED | MFC_CNTL_SUSPEND_MASK);
283 eieio();
286 static inline void save_timebase(struct spu_state *csa, struct spu *spu)
288 /* Save, Step 14:
289 * Read PPE Timebase High and Timebase low registers
290 * and save in CSA. TBD.
292 csa->suspend_time = get_cycles();
295 static inline void remove_other_spu_access(struct spu_state *csa,
296 struct spu *spu)
298 /* Save, Step 15:
299 * Remove other SPU access to this SPU by unmapping
300 * this SPU's pages from their address space. TBD.
304 static inline void do_mfc_mssync(struct spu_state *csa, struct spu *spu)
306 struct spu_problem __iomem *prob = spu->problem;
308 /* Save, Step 16:
309 * Restore, Step 11.
310 * Write SPU_MSSync register. Poll SPU_MSSync[P]
311 * for a value of 0.
313 out_be64(&prob->spc_mssync_RW, 1UL);
314 POLL_WHILE_TRUE(in_be64(&prob->spc_mssync_RW) & MS_SYNC_PENDING);
317 static inline void issue_mfc_tlbie(struct spu_state *csa, struct spu *spu)
319 /* Save, Step 17:
320 * Restore, Step 12.
321 * Restore, Step 48.
322 * Write TLB_Invalidate_Entry[IS,VPN,L,Lp]=0 register.
323 * Then issue a PPE sync instruction.
325 spu_tlb_invalidate(spu);
326 mb();
329 static inline void handle_pending_interrupts(struct spu_state *csa,
330 struct spu *spu)
332 /* Save, Step 18:
333 * Handle any pending interrupts from this SPU
334 * here. This is OS or hypervisor specific. One
335 * option is to re-enable interrupts to handle any
336 * pending interrupts, with the interrupt handlers
337 * recognizing the software Context Switch Pending
338 * flag, to ensure the SPU execution or MFC command
339 * queue is not restarted. TBD.
343 static inline void save_mfc_queues(struct spu_state *csa, struct spu *spu)
345 struct spu_priv2 __iomem *priv2 = spu->priv2;
346 int i;
348 /* Save, Step 19:
349 * If MFC_Cntl[Se]=0 then save
350 * MFC command queues.
352 if ((in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DMA_QUEUES_EMPTY) == 0) {
353 for (i = 0; i < 8; i++) {
354 csa->priv2.puq[i].mfc_cq_data0_RW =
355 in_be64(&priv2->puq[i].mfc_cq_data0_RW);
356 csa->priv2.puq[i].mfc_cq_data1_RW =
357 in_be64(&priv2->puq[i].mfc_cq_data1_RW);
358 csa->priv2.puq[i].mfc_cq_data2_RW =
359 in_be64(&priv2->puq[i].mfc_cq_data2_RW);
360 csa->priv2.puq[i].mfc_cq_data3_RW =
361 in_be64(&priv2->puq[i].mfc_cq_data3_RW);
363 for (i = 0; i < 16; i++) {
364 csa->priv2.spuq[i].mfc_cq_data0_RW =
365 in_be64(&priv2->spuq[i].mfc_cq_data0_RW);
366 csa->priv2.spuq[i].mfc_cq_data1_RW =
367 in_be64(&priv2->spuq[i].mfc_cq_data1_RW);
368 csa->priv2.spuq[i].mfc_cq_data2_RW =
369 in_be64(&priv2->spuq[i].mfc_cq_data2_RW);
370 csa->priv2.spuq[i].mfc_cq_data3_RW =
371 in_be64(&priv2->spuq[i].mfc_cq_data3_RW);
376 static inline void save_ppu_querymask(struct spu_state *csa, struct spu *spu)
378 struct spu_problem __iomem *prob = spu->problem;
380 /* Save, Step 20:
381 * Save the PPU_QueryMask register
382 * in the CSA.
384 csa->prob.dma_querymask_RW = in_be32(&prob->dma_querymask_RW);
387 static inline void save_ppu_querytype(struct spu_state *csa, struct spu *spu)
389 struct spu_problem __iomem *prob = spu->problem;
391 /* Save, Step 21:
392 * Save the PPU_QueryType register
393 * in the CSA.
395 csa->prob.dma_querytype_RW = in_be32(&prob->dma_querytype_RW);
398 static inline void save_ppu_tagstatus(struct spu_state *csa, struct spu *spu)
400 struct spu_problem __iomem *prob = spu->problem;
402 /* Save the Prxy_TagStatus register in the CSA.
404 * It is unnecessary to restore dma_tagstatus_R, however,
405 * dma_tagstatus_R in the CSA is accessed via backing_ops, so
406 * we must save it.
408 csa->prob.dma_tagstatus_R = in_be32(&prob->dma_tagstatus_R);
411 static inline void save_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
413 struct spu_priv2 __iomem *priv2 = spu->priv2;
415 /* Save, Step 22:
416 * Save the MFC_CSR_TSQ register
417 * in the LSCSA.
419 csa->priv2.spu_tag_status_query_RW =
420 in_be64(&priv2->spu_tag_status_query_RW);
423 static inline void save_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
425 struct spu_priv2 __iomem *priv2 = spu->priv2;
427 /* Save, Step 23:
428 * Save the MFC_CSR_CMD1 and MFC_CSR_CMD2
429 * registers in the CSA.
431 csa->priv2.spu_cmd_buf1_RW = in_be64(&priv2->spu_cmd_buf1_RW);
432 csa->priv2.spu_cmd_buf2_RW = in_be64(&priv2->spu_cmd_buf2_RW);
435 static inline void save_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
437 struct spu_priv2 __iomem *priv2 = spu->priv2;
439 /* Save, Step 24:
440 * Save the MFC_CSR_ATO register in
441 * the CSA.
443 csa->priv2.spu_atomic_status_RW = in_be64(&priv2->spu_atomic_status_RW);
446 static inline void save_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
448 /* Save, Step 25:
449 * Save the MFC_TCLASS_ID register in
450 * the CSA.
452 csa->priv1.mfc_tclass_id_RW = spu_mfc_tclass_id_get(spu);
455 static inline void set_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
457 /* Save, Step 26:
458 * Restore, Step 23.
459 * Write the MFC_TCLASS_ID register with
460 * the value 0x10000000.
462 spu_mfc_tclass_id_set(spu, 0x10000000);
463 eieio();
466 static inline void purge_mfc_queue(struct spu_state *csa, struct spu *spu)
468 struct spu_priv2 __iomem *priv2 = spu->priv2;
470 /* Save, Step 27:
471 * Restore, Step 14.
472 * Write MFC_CNTL[Pc]=1 (purge queue).
474 out_be64(&priv2->mfc_control_RW, MFC_CNTL_PURGE_DMA_REQUEST);
475 eieio();
478 static inline void wait_purge_complete(struct spu_state *csa, struct spu *spu)
480 struct spu_priv2 __iomem *priv2 = spu->priv2;
482 /* Save, Step 28:
483 * Poll MFC_CNTL[Ps] until value '11' is read
484 * (purge complete).
486 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
487 MFC_CNTL_PURGE_DMA_STATUS_MASK) ==
488 MFC_CNTL_PURGE_DMA_COMPLETE);
491 static inline void setup_mfc_sr1(struct spu_state *csa, struct spu *spu)
493 /* Save, Step 30:
494 * Restore, Step 18:
495 * Write MFC_SR1 with MFC_SR1[D=0,S=1] and
496 * MFC_SR1[TL,R,Pr,T] set correctly for the
497 * OS specific environment.
499 * Implementation note: The SPU-side code
500 * for save/restore is privileged, so the
501 * MFC_SR1[Pr] bit is not set.
504 spu_mfc_sr1_set(spu, (MFC_STATE1_MASTER_RUN_CONTROL_MASK |
505 MFC_STATE1_RELOCATE_MASK |
506 MFC_STATE1_BUS_TLBIE_MASK));
509 static inline void save_spu_npc(struct spu_state *csa, struct spu *spu)
511 struct spu_problem __iomem *prob = spu->problem;
513 /* Save, Step 31:
514 * Save SPU_NPC in the CSA.
516 csa->prob.spu_npc_RW = in_be32(&prob->spu_npc_RW);
519 static inline void save_spu_privcntl(struct spu_state *csa, struct spu *spu)
521 struct spu_priv2 __iomem *priv2 = spu->priv2;
523 /* Save, Step 32:
524 * Save SPU_PrivCntl in the CSA.
526 csa->priv2.spu_privcntl_RW = in_be64(&priv2->spu_privcntl_RW);
529 static inline void reset_spu_privcntl(struct spu_state *csa, struct spu *spu)
531 struct spu_priv2 __iomem *priv2 = spu->priv2;
533 /* Save, Step 33:
534 * Restore, Step 16:
535 * Write SPU_PrivCntl[S,Le,A] fields reset to 0.
537 out_be64(&priv2->spu_privcntl_RW, 0UL);
538 eieio();
541 static inline void save_spu_lslr(struct spu_state *csa, struct spu *spu)
543 struct spu_priv2 __iomem *priv2 = spu->priv2;
545 /* Save, Step 34:
546 * Save SPU_LSLR in the CSA.
548 csa->priv2.spu_lslr_RW = in_be64(&priv2->spu_lslr_RW);
551 static inline void reset_spu_lslr(struct spu_state *csa, struct spu *spu)
553 struct spu_priv2 __iomem *priv2 = spu->priv2;
555 /* Save, Step 35:
556 * Restore, Step 17.
557 * Reset SPU_LSLR.
559 out_be64(&priv2->spu_lslr_RW, LS_ADDR_MASK);
560 eieio();
563 static inline void save_spu_cfg(struct spu_state *csa, struct spu *spu)
565 struct spu_priv2 __iomem *priv2 = spu->priv2;
567 /* Save, Step 36:
568 * Save SPU_Cfg in the CSA.
570 csa->priv2.spu_cfg_RW = in_be64(&priv2->spu_cfg_RW);
573 static inline void save_pm_trace(struct spu_state *csa, struct spu *spu)
575 /* Save, Step 37:
576 * Save PM_Trace_Tag_Wait_Mask in the CSA.
577 * Not performed by this implementation.
581 static inline void save_mfc_rag(struct spu_state *csa, struct spu *spu)
583 /* Save, Step 38:
584 * Save RA_GROUP_ID register and the
585 * RA_ENABLE reigster in the CSA.
587 csa->priv1.resource_allocation_groupID_RW =
588 spu_resource_allocation_groupID_get(spu);
589 csa->priv1.resource_allocation_enable_RW =
590 spu_resource_allocation_enable_get(spu);
593 static inline void save_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
595 struct spu_problem __iomem *prob = spu->problem;
597 /* Save, Step 39:
598 * Save MB_Stat register in the CSA.
600 csa->prob.mb_stat_R = in_be32(&prob->mb_stat_R);
603 static inline void save_ppu_mb(struct spu_state *csa, struct spu *spu)
605 struct spu_problem __iomem *prob = spu->problem;
607 /* Save, Step 40:
608 * Save the PPU_MB register in the CSA.
610 csa->prob.pu_mb_R = in_be32(&prob->pu_mb_R);
613 static inline void save_ppuint_mb(struct spu_state *csa, struct spu *spu)
615 struct spu_priv2 __iomem *priv2 = spu->priv2;
617 /* Save, Step 41:
618 * Save the PPUINT_MB register in the CSA.
620 csa->priv2.puint_mb_R = in_be64(&priv2->puint_mb_R);
623 static inline void save_ch_part1(struct spu_state *csa, struct spu *spu)
625 struct spu_priv2 __iomem *priv2 = spu->priv2;
626 u64 idx, ch_indices[] = { 0UL, 3UL, 4UL, 24UL, 25UL, 27UL };
627 int i;
629 /* Save, Step 42:
632 /* Save CH 1, without channel count */
633 out_be64(&priv2->spu_chnlcntptr_RW, 1);
634 csa->spu_chnldata_RW[1] = in_be64(&priv2->spu_chnldata_RW);
636 /* Save the following CH: [0,3,4,24,25,27] */
637 for (i = 0; i < ARRAY_SIZE(ch_indices); i++) {
638 idx = ch_indices[i];
639 out_be64(&priv2->spu_chnlcntptr_RW, idx);
640 eieio();
641 csa->spu_chnldata_RW[idx] = in_be64(&priv2->spu_chnldata_RW);
642 csa->spu_chnlcnt_RW[idx] = in_be64(&priv2->spu_chnlcnt_RW);
643 out_be64(&priv2->spu_chnldata_RW, 0UL);
644 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
645 eieio();
649 static inline void save_spu_mb(struct spu_state *csa, struct spu *spu)
651 struct spu_priv2 __iomem *priv2 = spu->priv2;
652 int i;
654 /* Save, Step 43:
655 * Save SPU Read Mailbox Channel.
657 out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
658 eieio();
659 csa->spu_chnlcnt_RW[29] = in_be64(&priv2->spu_chnlcnt_RW);
660 for (i = 0; i < 4; i++) {
661 csa->spu_mailbox_data[i] = in_be64(&priv2->spu_chnldata_RW);
663 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
664 eieio();
667 static inline void save_mfc_cmd(struct spu_state *csa, struct spu *spu)
669 struct spu_priv2 __iomem *priv2 = spu->priv2;
671 /* Save, Step 44:
672 * Save MFC_CMD Channel.
674 out_be64(&priv2->spu_chnlcntptr_RW, 21UL);
675 eieio();
676 csa->spu_chnlcnt_RW[21] = in_be64(&priv2->spu_chnlcnt_RW);
677 eieio();
680 static inline void reset_ch(struct spu_state *csa, struct spu *spu)
682 struct spu_priv2 __iomem *priv2 = spu->priv2;
683 u64 ch_indices[4] = { 21UL, 23UL, 28UL, 30UL };
684 u64 ch_counts[4] = { 16UL, 1UL, 1UL, 1UL };
685 u64 idx;
686 int i;
688 /* Save, Step 45:
689 * Reset the following CH: [21, 23, 28, 30]
691 for (i = 0; i < 4; i++) {
692 idx = ch_indices[i];
693 out_be64(&priv2->spu_chnlcntptr_RW, idx);
694 eieio();
695 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
696 eieio();
700 static inline void resume_mfc_queue(struct spu_state *csa, struct spu *spu)
702 struct spu_priv2 __iomem *priv2 = spu->priv2;
704 /* Save, Step 46:
705 * Restore, Step 25.
706 * Write MFC_CNTL[Sc]=0 (resume queue processing).
708 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESUME_DMA_QUEUE);
711 static inline void setup_mfc_slbs(struct spu_state *csa, struct spu *spu,
712 unsigned int *code, int code_size)
714 /* Save, Step 47:
715 * Restore, Step 30.
716 * If MFC_SR1[R]=1, write 0 to SLB_Invalidate_All
717 * register, then initialize SLB_VSID and SLB_ESID
718 * to provide access to SPU context save code and
719 * LSCSA.
721 * This implementation places both the context
722 * switch code and LSCSA in kernel address space.
724 * Further this implementation assumes that the
725 * MFC_SR1[R]=1 (in other words, assume that
726 * translation is desired by OS environment).
728 spu_invalidate_slbs(spu);
729 spu_setup_kernel_slbs(spu, csa->lscsa, code, code_size);
732 static inline void set_switch_active(struct spu_state *csa, struct spu *spu)
734 /* Save, Step 48:
735 * Restore, Step 23.
736 * Change the software context switch pending flag
737 * to context switch active.
738 <<<<<<< HEAD:arch/powerpc/platforms/cell/spufs/switch.c
739 =======
741 * This implementation does not uses a switch active flag.
742 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/powerpc/platforms/cell/spufs/switch.c
744 <<<<<<< HEAD:arch/powerpc/platforms/cell/spufs/switch.c
745 set_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags);
746 =======
747 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/powerpc/platforms/cell/spufs/switch.c
748 clear_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags);
749 mb();
752 static inline void enable_interrupts(struct spu_state *csa, struct spu *spu)
754 unsigned long class1_mask = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
755 CLASS1_ENABLE_STORAGE_FAULT_INTR;
757 /* Save, Step 49:
758 * Restore, Step 22:
759 * Reset and then enable interrupts, as
760 * needed by OS.
762 * This implementation enables only class1
763 * (translation) interrupts.
765 spin_lock_irq(&spu->register_lock);
766 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
767 spu_int_stat_clear(spu, 1, CLASS1_INTR_MASK);
768 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
769 spu_int_mask_set(spu, 0, 0ul);
770 spu_int_mask_set(spu, 1, class1_mask);
771 spu_int_mask_set(spu, 2, 0ul);
772 spin_unlock_irq(&spu->register_lock);
775 static inline int send_mfc_dma(struct spu *spu, unsigned long ea,
776 unsigned int ls_offset, unsigned int size,
777 unsigned int tag, unsigned int rclass,
778 unsigned int cmd)
780 struct spu_problem __iomem *prob = spu->problem;
781 union mfc_tag_size_class_cmd command;
782 unsigned int transfer_size;
783 volatile unsigned int status = 0x0;
785 while (size > 0) {
786 transfer_size =
787 (size > MFC_MAX_DMA_SIZE) ? MFC_MAX_DMA_SIZE : size;
788 command.u.mfc_size = transfer_size;
789 command.u.mfc_tag = tag;
790 command.u.mfc_rclassid = rclass;
791 command.u.mfc_cmd = cmd;
792 do {
793 out_be32(&prob->mfc_lsa_W, ls_offset);
794 out_be64(&prob->mfc_ea_W, ea);
795 out_be64(&prob->mfc_union_W.all64, command.all64);
796 status =
797 in_be32(&prob->mfc_union_W.by32.mfc_class_cmd32);
798 if (unlikely(status & 0x2)) {
799 cpu_relax();
801 } while (status & 0x3);
802 size -= transfer_size;
803 ea += transfer_size;
804 ls_offset += transfer_size;
806 return 0;
809 static inline void save_ls_16kb(struct spu_state *csa, struct spu *spu)
811 unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
812 unsigned int ls_offset = 0x0;
813 unsigned int size = 16384;
814 unsigned int tag = 0;
815 unsigned int rclass = 0;
816 unsigned int cmd = MFC_PUT_CMD;
818 /* Save, Step 50:
819 * Issue a DMA command to copy the first 16K bytes
820 * of local storage to the CSA.
822 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
825 static inline void set_spu_npc(struct spu_state *csa, struct spu *spu)
827 struct spu_problem __iomem *prob = spu->problem;
829 /* Save, Step 51:
830 * Restore, Step 31.
831 * Write SPU_NPC[IE]=0 and SPU_NPC[LSA] to entry
832 * point address of context save code in local
833 * storage.
835 * This implementation uses SPU-side save/restore
836 * programs with entry points at LSA of 0.
838 out_be32(&prob->spu_npc_RW, 0);
839 eieio();
842 static inline void set_signot1(struct spu_state *csa, struct spu *spu)
844 struct spu_problem __iomem *prob = spu->problem;
845 union {
846 u64 ull;
847 u32 ui[2];
848 } addr64;
850 /* Save, Step 52:
851 * Restore, Step 32:
852 * Write SPU_Sig_Notify_1 register with upper 32-bits
853 * of the CSA.LSCSA effective address.
855 addr64.ull = (u64) csa->lscsa;
856 out_be32(&prob->signal_notify1, addr64.ui[0]);
857 eieio();
860 static inline void set_signot2(struct spu_state *csa, struct spu *spu)
862 struct spu_problem __iomem *prob = spu->problem;
863 union {
864 u64 ull;
865 u32 ui[2];
866 } addr64;
868 /* Save, Step 53:
869 * Restore, Step 33:
870 * Write SPU_Sig_Notify_2 register with lower 32-bits
871 * of the CSA.LSCSA effective address.
873 addr64.ull = (u64) csa->lscsa;
874 out_be32(&prob->signal_notify2, addr64.ui[1]);
875 eieio();
878 static inline void send_save_code(struct spu_state *csa, struct spu *spu)
880 unsigned long addr = (unsigned long)&spu_save_code[0];
881 unsigned int ls_offset = 0x0;
882 unsigned int size = sizeof(spu_save_code);
883 unsigned int tag = 0;
884 unsigned int rclass = 0;
885 unsigned int cmd = MFC_GETFS_CMD;
887 /* Save, Step 54:
888 * Issue a DMA command to copy context save code
889 * to local storage and start SPU.
891 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
894 static inline void set_ppu_querymask(struct spu_state *csa, struct spu *spu)
896 struct spu_problem __iomem *prob = spu->problem;
898 /* Save, Step 55:
899 * Restore, Step 38.
900 * Write PPU_QueryMask=1 (enable Tag Group 0)
901 * and issue eieio instruction.
903 out_be32(&prob->dma_querymask_RW, MFC_TAGID_TO_TAGMASK(0));
904 eieio();
907 static inline void wait_tag_complete(struct spu_state *csa, struct spu *spu)
909 struct spu_problem __iomem *prob = spu->problem;
910 u32 mask = MFC_TAGID_TO_TAGMASK(0);
911 unsigned long flags;
913 /* Save, Step 56:
914 * Restore, Step 39.
915 * Restore, Step 39.
916 * Restore, Step 46.
917 * Poll PPU_TagStatus[gn] until 01 (Tag group 0 complete)
918 * or write PPU_QueryType[TS]=01 and wait for Tag Group
919 * Complete Interrupt. Write INT_Stat_Class0 or
920 * INT_Stat_Class2 with value of 'handled'.
922 POLL_WHILE_FALSE(in_be32(&prob->dma_tagstatus_R) & mask);
924 local_irq_save(flags);
925 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
926 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
927 local_irq_restore(flags);
930 static inline void wait_spu_stopped(struct spu_state *csa, struct spu *spu)
932 struct spu_problem __iomem *prob = spu->problem;
933 unsigned long flags;
935 /* Save, Step 57:
936 * Restore, Step 40.
937 * Poll until SPU_Status[R]=0 or wait for SPU Class 0
938 * or SPU Class 2 interrupt. Write INT_Stat_class0
939 * or INT_Stat_class2 with value of handled.
941 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING);
943 local_irq_save(flags);
944 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
945 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
946 local_irq_restore(flags);
949 static inline int check_save_status(struct spu_state *csa, struct spu *spu)
951 struct spu_problem __iomem *prob = spu->problem;
952 u32 complete;
954 /* Save, Step 54:
955 * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
956 * context save succeeded, otherwise context save
957 * failed.
959 complete = ((SPU_SAVE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
960 SPU_STATUS_STOPPED_BY_STOP);
961 return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
964 static inline void terminate_spu_app(struct spu_state *csa, struct spu *spu)
966 /* Restore, Step 4:
967 * If required, notify the "using application" that
968 * the SPU task has been terminated. TBD.
972 static inline void suspend_mfc_and_halt_decr(struct spu_state *csa,
973 struct spu *spu)
975 struct spu_priv2 __iomem *priv2 = spu->priv2;
977 /* Restore, Step 7:
978 * Write MFC_Cntl[Dh,Sc,Sm]='1','1','0' to suspend
979 * the queue and halt the decrementer.
981 out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE |
982 MFC_CNTL_DECREMENTER_HALTED);
983 eieio();
986 static inline void wait_suspend_mfc_complete(struct spu_state *csa,
987 struct spu *spu)
989 struct spu_priv2 __iomem *priv2 = spu->priv2;
991 /* Restore, Step 8:
992 * Restore, Step 47.
993 * Poll MFC_CNTL[Ss] until 11 is returned.
995 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
996 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) ==
997 MFC_CNTL_SUSPEND_COMPLETE);
1000 static inline int suspend_spe(struct spu_state *csa, struct spu *spu)
1002 struct spu_problem __iomem *prob = spu->problem;
1004 /* Restore, Step 9:
1005 * If SPU_Status[R]=1, stop SPU execution
1006 * and wait for stop to complete.
1008 * Returns 1 if SPU_Status[R]=1 on entry.
1009 * 0 otherwise
1011 if (in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) {
1012 if (in_be32(&prob->spu_status_R) &
1013 SPU_STATUS_ISOLATED_EXIT_STATUS) {
1014 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1015 SPU_STATUS_RUNNING);
1017 if ((in_be32(&prob->spu_status_R) &
1018 SPU_STATUS_ISOLATED_LOAD_STATUS)
1019 || (in_be32(&prob->spu_status_R) &
1020 SPU_STATUS_ISOLATED_STATE)) {
1021 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1022 eieio();
1023 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1024 SPU_STATUS_RUNNING);
1025 out_be32(&prob->spu_runcntl_RW, 0x2);
1026 eieio();
1027 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1028 SPU_STATUS_RUNNING);
1030 if (in_be32(&prob->spu_status_R) &
1031 SPU_STATUS_WAITING_FOR_CHANNEL) {
1032 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1033 eieio();
1034 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1035 SPU_STATUS_RUNNING);
1037 return 1;
1039 return 0;
1042 static inline void clear_spu_status(struct spu_state *csa, struct spu *spu)
1044 struct spu_problem __iomem *prob = spu->problem;
1046 /* Restore, Step 10:
1047 * If SPU_Status[R]=0 and SPU_Status[E,L,IS]=1,
1048 * release SPU from isolate state.
1050 if (!(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING)) {
1051 if (in_be32(&prob->spu_status_R) &
1052 SPU_STATUS_ISOLATED_EXIT_STATUS) {
1053 spu_mfc_sr1_set(spu,
1054 MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1055 eieio();
1056 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1057 eieio();
1058 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1059 SPU_STATUS_RUNNING);
1061 if ((in_be32(&prob->spu_status_R) &
1062 SPU_STATUS_ISOLATED_LOAD_STATUS)
1063 || (in_be32(&prob->spu_status_R) &
1064 SPU_STATUS_ISOLATED_STATE)) {
1065 spu_mfc_sr1_set(spu,
1066 MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1067 eieio();
1068 out_be32(&prob->spu_runcntl_RW, 0x2);
1069 eieio();
1070 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1071 SPU_STATUS_RUNNING);
1076 static inline void reset_ch_part1(struct spu_state *csa, struct spu *spu)
1078 struct spu_priv2 __iomem *priv2 = spu->priv2;
1079 u64 ch_indices[] = { 0UL, 3UL, 4UL, 24UL, 25UL, 27UL };
1080 u64 idx;
1081 int i;
1083 /* Restore, Step 20:
1086 /* Reset CH 1 */
1087 out_be64(&priv2->spu_chnlcntptr_RW, 1);
1088 out_be64(&priv2->spu_chnldata_RW, 0UL);
1090 /* Reset the following CH: [0,3,4,24,25,27] */
1091 for (i = 0; i < ARRAY_SIZE(ch_indices); i++) {
1092 idx = ch_indices[i];
1093 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1094 eieio();
1095 out_be64(&priv2->spu_chnldata_RW, 0UL);
1096 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
1097 eieio();
1101 static inline void reset_ch_part2(struct spu_state *csa, struct spu *spu)
1103 struct spu_priv2 __iomem *priv2 = spu->priv2;
1104 u64 ch_indices[5] = { 21UL, 23UL, 28UL, 29UL, 30UL };
1105 u64 ch_counts[5] = { 16UL, 1UL, 1UL, 0UL, 1UL };
1106 u64 idx;
1107 int i;
1109 /* Restore, Step 21:
1110 * Reset the following CH: [21, 23, 28, 29, 30]
1112 for (i = 0; i < 5; i++) {
1113 idx = ch_indices[i];
1114 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1115 eieio();
1116 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
1117 eieio();
1121 static inline void setup_spu_status_part1(struct spu_state *csa,
1122 struct spu *spu)
1124 u32 status_P = SPU_STATUS_STOPPED_BY_STOP;
1125 u32 status_I = SPU_STATUS_INVALID_INSTR;
1126 u32 status_H = SPU_STATUS_STOPPED_BY_HALT;
1127 u32 status_S = SPU_STATUS_SINGLE_STEP;
1128 u32 status_S_I = SPU_STATUS_SINGLE_STEP | SPU_STATUS_INVALID_INSTR;
1129 u32 status_S_P = SPU_STATUS_SINGLE_STEP | SPU_STATUS_STOPPED_BY_STOP;
1130 u32 status_P_H = SPU_STATUS_STOPPED_BY_HALT |SPU_STATUS_STOPPED_BY_STOP;
1131 u32 status_P_I = SPU_STATUS_STOPPED_BY_STOP |SPU_STATUS_INVALID_INSTR;
1132 u32 status_code;
1134 /* Restore, Step 27:
1135 * If the CSA.SPU_Status[I,S,H,P]=1 then add the correct
1136 * instruction sequence to the end of the SPU based restore
1137 * code (after the "context restored" stop and signal) to
1138 * restore the correct SPU status.
1140 * NOTE: Rather than modifying the SPU executable, we
1141 * instead add a new 'stopped_status' field to the
1142 * LSCSA. The SPU-side restore reads this field and
1143 * takes the appropriate action when exiting.
1146 status_code =
1147 (csa->prob.spu_status_R >> SPU_STOP_STATUS_SHIFT) & 0xFFFF;
1148 if ((csa->prob.spu_status_R & status_P_I) == status_P_I) {
1150 /* SPU_Status[P,I]=1 - Illegal Instruction followed
1151 * by Stop and Signal instruction, followed by 'br -4'.
1154 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_I;
1155 csa->lscsa->stopped_status.slot[1] = status_code;
1157 } else if ((csa->prob.spu_status_R & status_P_H) == status_P_H) {
1159 /* SPU_Status[P,H]=1 - Halt Conditional, followed
1160 * by Stop and Signal instruction, followed by
1161 * 'br -4'.
1163 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_H;
1164 csa->lscsa->stopped_status.slot[1] = status_code;
1166 } else if ((csa->prob.spu_status_R & status_S_P) == status_S_P) {
1168 /* SPU_Status[S,P]=1 - Stop and Signal instruction
1169 * followed by 'br -4'.
1171 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_P;
1172 csa->lscsa->stopped_status.slot[1] = status_code;
1174 } else if ((csa->prob.spu_status_R & status_S_I) == status_S_I) {
1176 /* SPU_Status[S,I]=1 - Illegal instruction followed
1177 * by 'br -4'.
1179 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_I;
1180 csa->lscsa->stopped_status.slot[1] = status_code;
1182 } else if ((csa->prob.spu_status_R & status_P) == status_P) {
1184 /* SPU_Status[P]=1 - Stop and Signal instruction
1185 * followed by 'br -4'.
1187 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P;
1188 csa->lscsa->stopped_status.slot[1] = status_code;
1190 } else if ((csa->prob.spu_status_R & status_H) == status_H) {
1192 /* SPU_Status[H]=1 - Halt Conditional, followed
1193 * by 'br -4'.
1195 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_H;
1197 } else if ((csa->prob.spu_status_R & status_S) == status_S) {
1199 /* SPU_Status[S]=1 - Two nop instructions.
1201 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S;
1203 } else if ((csa->prob.spu_status_R & status_I) == status_I) {
1205 /* SPU_Status[I]=1 - Illegal instruction followed
1206 * by 'br -4'.
1208 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_I;
1213 static inline void setup_spu_status_part2(struct spu_state *csa,
1214 struct spu *spu)
1216 u32 mask;
1218 /* Restore, Step 28:
1219 * If the CSA.SPU_Status[I,S,H,P,R]=0 then
1220 * add a 'br *' instruction to the end of
1221 * the SPU based restore code.
1223 * NOTE: Rather than modifying the SPU executable, we
1224 * instead add a new 'stopped_status' field to the
1225 * LSCSA. The SPU-side restore reads this field and
1226 * takes the appropriate action when exiting.
1228 mask = SPU_STATUS_INVALID_INSTR |
1229 SPU_STATUS_SINGLE_STEP |
1230 SPU_STATUS_STOPPED_BY_HALT |
1231 SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
1232 if (!(csa->prob.spu_status_R & mask)) {
1233 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_R;
1237 static inline void restore_mfc_rag(struct spu_state *csa, struct spu *spu)
1239 /* Restore, Step 29:
1240 * Restore RA_GROUP_ID register and the
1241 * RA_ENABLE reigster from the CSA.
1243 spu_resource_allocation_groupID_set(spu,
1244 csa->priv1.resource_allocation_groupID_RW);
1245 spu_resource_allocation_enable_set(spu,
1246 csa->priv1.resource_allocation_enable_RW);
1249 static inline void send_restore_code(struct spu_state *csa, struct spu *spu)
1251 unsigned long addr = (unsigned long)&spu_restore_code[0];
1252 unsigned int ls_offset = 0x0;
1253 unsigned int size = sizeof(spu_restore_code);
1254 unsigned int tag = 0;
1255 unsigned int rclass = 0;
1256 unsigned int cmd = MFC_GETFS_CMD;
1258 /* Restore, Step 37:
1259 * Issue MFC DMA command to copy context
1260 * restore code to local storage.
1262 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
1265 static inline void setup_decr(struct spu_state *csa, struct spu *spu)
1267 /* Restore, Step 34:
1268 * If CSA.MFC_CNTL[Ds]=1 (decrementer was
1269 * running) then adjust decrementer, set
1270 * decrementer running status in LSCSA,
1271 * and set decrementer "wrapped" status
1272 * in LSCSA.
1274 if (csa->priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING) {
1275 cycles_t resume_time = get_cycles();
1276 cycles_t delta_time = resume_time - csa->suspend_time;
1278 csa->lscsa->decr_status.slot[0] = SPU_DECR_STATUS_RUNNING;
1279 if (csa->lscsa->decr.slot[0] < delta_time) {
1280 csa->lscsa->decr_status.slot[0] |=
1281 SPU_DECR_STATUS_WRAPPED;
1284 csa->lscsa->decr.slot[0] -= delta_time;
1285 } else {
1286 csa->lscsa->decr_status.slot[0] = 0;
1290 static inline void setup_ppu_mb(struct spu_state *csa, struct spu *spu)
1292 /* Restore, Step 35:
1293 * Copy the CSA.PU_MB data into the LSCSA.
1295 csa->lscsa->ppu_mb.slot[0] = csa->prob.pu_mb_R;
1298 static inline void setup_ppuint_mb(struct spu_state *csa, struct spu *spu)
1300 /* Restore, Step 36:
1301 * Copy the CSA.PUINT_MB data into the LSCSA.
1303 csa->lscsa->ppuint_mb.slot[0] = csa->priv2.puint_mb_R;
1306 static inline int check_restore_status(struct spu_state *csa, struct spu *spu)
1308 struct spu_problem __iomem *prob = spu->problem;
1309 u32 complete;
1311 /* Restore, Step 40:
1312 * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
1313 * context restore succeeded, otherwise context restore
1314 * failed.
1316 complete = ((SPU_RESTORE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
1317 SPU_STATUS_STOPPED_BY_STOP);
1318 return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
1321 static inline void restore_spu_privcntl(struct spu_state *csa, struct spu *spu)
1323 struct spu_priv2 __iomem *priv2 = spu->priv2;
1325 /* Restore, Step 41:
1326 * Restore SPU_PrivCntl from the CSA.
1328 out_be64(&priv2->spu_privcntl_RW, csa->priv2.spu_privcntl_RW);
1329 eieio();
1332 static inline void restore_status_part1(struct spu_state *csa, struct spu *spu)
1334 struct spu_problem __iomem *prob = spu->problem;
1335 u32 mask;
1337 /* Restore, Step 42:
1338 * If any CSA.SPU_Status[I,S,H,P]=1, then
1339 * restore the error or single step state.
1341 mask = SPU_STATUS_INVALID_INSTR |
1342 SPU_STATUS_SINGLE_STEP |
1343 SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
1344 if (csa->prob.spu_status_R & mask) {
1345 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1346 eieio();
1347 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1348 SPU_STATUS_RUNNING);
1352 static inline void restore_status_part2(struct spu_state *csa, struct spu *spu)
1354 struct spu_problem __iomem *prob = spu->problem;
1355 u32 mask;
1357 /* Restore, Step 43:
1358 * If all CSA.SPU_Status[I,S,H,P,R]=0 then write
1359 * SPU_RunCntl[R0R1]='01', wait for SPU_Status[R]=1,
1360 * then write '00' to SPU_RunCntl[R0R1] and wait
1361 * for SPU_Status[R]=0.
1363 mask = SPU_STATUS_INVALID_INSTR |
1364 SPU_STATUS_SINGLE_STEP |
1365 SPU_STATUS_STOPPED_BY_HALT |
1366 SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
1367 if (!(csa->prob.spu_status_R & mask)) {
1368 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1369 eieio();
1370 POLL_WHILE_FALSE(in_be32(&prob->spu_status_R) &
1371 SPU_STATUS_RUNNING);
1372 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1373 eieio();
1374 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1375 SPU_STATUS_RUNNING);
1379 static inline void restore_ls_16kb(struct spu_state *csa, struct spu *spu)
1381 unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
1382 unsigned int ls_offset = 0x0;
1383 unsigned int size = 16384;
1384 unsigned int tag = 0;
1385 unsigned int rclass = 0;
1386 unsigned int cmd = MFC_GET_CMD;
1388 /* Restore, Step 44:
1389 * Issue a DMA command to restore the first
1390 * 16kb of local storage from CSA.
1392 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
1395 static inline void suspend_mfc(struct spu_state *csa, struct spu *spu)
1397 struct spu_priv2 __iomem *priv2 = spu->priv2;
1399 /* Restore, Step 47.
1400 * Write MFC_Cntl[Sc,Sm]='1','0' to suspend
1401 * the queue.
1403 out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE);
1404 eieio();
1407 static inline void clear_interrupts(struct spu_state *csa, struct spu *spu)
1409 /* Restore, Step 49:
1410 * Write INT_MASK_class0 with value of 0.
1411 * Write INT_MASK_class1 with value of 0.
1412 * Write INT_MASK_class2 with value of 0.
1413 * Write INT_STAT_class0 with value of -1.
1414 * Write INT_STAT_class1 with value of -1.
1415 * Write INT_STAT_class2 with value of -1.
1417 spin_lock_irq(&spu->register_lock);
1418 spu_int_mask_set(spu, 0, 0ul);
1419 spu_int_mask_set(spu, 1, 0ul);
1420 spu_int_mask_set(spu, 2, 0ul);
1421 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
1422 spu_int_stat_clear(spu, 1, CLASS1_INTR_MASK);
1423 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
1424 spin_unlock_irq(&spu->register_lock);
1427 static inline void restore_mfc_queues(struct spu_state *csa, struct spu *spu)
1429 struct spu_priv2 __iomem *priv2 = spu->priv2;
1430 int i;
1432 /* Restore, Step 50:
1433 * If MFC_Cntl[Se]!=0 then restore
1434 * MFC command queues.
1436 if ((csa->priv2.mfc_control_RW & MFC_CNTL_DMA_QUEUES_EMPTY_MASK) == 0) {
1437 for (i = 0; i < 8; i++) {
1438 out_be64(&priv2->puq[i].mfc_cq_data0_RW,
1439 csa->priv2.puq[i].mfc_cq_data0_RW);
1440 out_be64(&priv2->puq[i].mfc_cq_data1_RW,
1441 csa->priv2.puq[i].mfc_cq_data1_RW);
1442 out_be64(&priv2->puq[i].mfc_cq_data2_RW,
1443 csa->priv2.puq[i].mfc_cq_data2_RW);
1444 out_be64(&priv2->puq[i].mfc_cq_data3_RW,
1445 csa->priv2.puq[i].mfc_cq_data3_RW);
1447 for (i = 0; i < 16; i++) {
1448 out_be64(&priv2->spuq[i].mfc_cq_data0_RW,
1449 csa->priv2.spuq[i].mfc_cq_data0_RW);
1450 out_be64(&priv2->spuq[i].mfc_cq_data1_RW,
1451 csa->priv2.spuq[i].mfc_cq_data1_RW);
1452 out_be64(&priv2->spuq[i].mfc_cq_data2_RW,
1453 csa->priv2.spuq[i].mfc_cq_data2_RW);
1454 out_be64(&priv2->spuq[i].mfc_cq_data3_RW,
1455 csa->priv2.spuq[i].mfc_cq_data3_RW);
1458 eieio();
1461 static inline void restore_ppu_querymask(struct spu_state *csa, struct spu *spu)
1463 struct spu_problem __iomem *prob = spu->problem;
1465 /* Restore, Step 51:
1466 * Restore the PPU_QueryMask register from CSA.
1468 out_be32(&prob->dma_querymask_RW, csa->prob.dma_querymask_RW);
1469 eieio();
1472 static inline void restore_ppu_querytype(struct spu_state *csa, struct spu *spu)
1474 struct spu_problem __iomem *prob = spu->problem;
1476 /* Restore, Step 52:
1477 * Restore the PPU_QueryType register from CSA.
1479 out_be32(&prob->dma_querytype_RW, csa->prob.dma_querytype_RW);
1480 eieio();
1483 static inline void restore_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
1485 struct spu_priv2 __iomem *priv2 = spu->priv2;
1487 /* Restore, Step 53:
1488 * Restore the MFC_CSR_TSQ register from CSA.
1490 out_be64(&priv2->spu_tag_status_query_RW,
1491 csa->priv2.spu_tag_status_query_RW);
1492 eieio();
1495 static inline void restore_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
1497 struct spu_priv2 __iomem *priv2 = spu->priv2;
1499 /* Restore, Step 54:
1500 * Restore the MFC_CSR_CMD1 and MFC_CSR_CMD2
1501 * registers from CSA.
1503 out_be64(&priv2->spu_cmd_buf1_RW, csa->priv2.spu_cmd_buf1_RW);
1504 out_be64(&priv2->spu_cmd_buf2_RW, csa->priv2.spu_cmd_buf2_RW);
1505 eieio();
1508 static inline void restore_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
1510 struct spu_priv2 __iomem *priv2 = spu->priv2;
1512 /* Restore, Step 55:
1513 * Restore the MFC_CSR_ATO register from CSA.
1515 out_be64(&priv2->spu_atomic_status_RW, csa->priv2.spu_atomic_status_RW);
1518 static inline void restore_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
1520 /* Restore, Step 56:
1521 * Restore the MFC_TCLASS_ID register from CSA.
1523 spu_mfc_tclass_id_set(spu, csa->priv1.mfc_tclass_id_RW);
1524 eieio();
1527 static inline void set_llr_event(struct spu_state *csa, struct spu *spu)
1529 u64 ch0_cnt, ch0_data;
1530 u64 ch1_data;
1532 /* Restore, Step 57:
1533 * Set the Lock Line Reservation Lost Event by:
1534 * 1. OR CSA.SPU_Event_Status with bit 21 (Lr) set to 1.
1535 * 2. If CSA.SPU_Channel_0_Count=0 and
1536 * CSA.SPU_Wr_Event_Mask[Lr]=1 and
1537 * CSA.SPU_Event_Status[Lr]=0 then set
1538 * CSA.SPU_Event_Status_Count=1.
1540 ch0_cnt = csa->spu_chnlcnt_RW[0];
1541 ch0_data = csa->spu_chnldata_RW[0];
1542 ch1_data = csa->spu_chnldata_RW[1];
1543 csa->spu_chnldata_RW[0] |= MFC_LLR_LOST_EVENT;
1544 if ((ch0_cnt == 0) && !(ch0_data & MFC_LLR_LOST_EVENT) &&
1545 (ch1_data & MFC_LLR_LOST_EVENT)) {
1546 csa->spu_chnlcnt_RW[0] = 1;
1550 static inline void restore_decr_wrapped(struct spu_state *csa, struct spu *spu)
1552 /* Restore, Step 58:
1553 * If the status of the CSA software decrementer
1554 * "wrapped" flag is set, OR in a '1' to
1555 * CSA.SPU_Event_Status[Tm].
1557 if (!(csa->lscsa->decr_status.slot[0] & SPU_DECR_STATUS_WRAPPED))
1558 return;
1560 if ((csa->spu_chnlcnt_RW[0] == 0) &&
1561 (csa->spu_chnldata_RW[1] & 0x20) &&
1562 !(csa->spu_chnldata_RW[0] & 0x20))
1563 csa->spu_chnlcnt_RW[0] = 1;
1565 csa->spu_chnldata_RW[0] |= 0x20;
1568 static inline void restore_ch_part1(struct spu_state *csa, struct spu *spu)
1570 struct spu_priv2 __iomem *priv2 = spu->priv2;
1571 u64 idx, ch_indices[] = { 0UL, 3UL, 4UL, 24UL, 25UL, 27UL };
1572 int i;
1574 /* Restore, Step 59:
1575 * Restore the following CH: [0,3,4,24,25,27]
1577 for (i = 0; i < ARRAY_SIZE(ch_indices); i++) {
1578 idx = ch_indices[i];
1579 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1580 eieio();
1581 out_be64(&priv2->spu_chnldata_RW, csa->spu_chnldata_RW[idx]);
1582 out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[idx]);
1583 eieio();
1587 static inline void restore_ch_part2(struct spu_state *csa, struct spu *spu)
1589 struct spu_priv2 __iomem *priv2 = spu->priv2;
1590 u64 ch_indices[3] = { 9UL, 21UL, 23UL };
1591 u64 ch_counts[3] = { 1UL, 16UL, 1UL };
1592 u64 idx;
1593 int i;
1595 /* Restore, Step 60:
1596 * Restore the following CH: [9,21,23].
1598 ch_counts[0] = 1UL;
1599 ch_counts[1] = csa->spu_chnlcnt_RW[21];
1600 ch_counts[2] = 1UL;
1601 for (i = 0; i < 3; i++) {
1602 idx = ch_indices[i];
1603 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1604 eieio();
1605 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
1606 eieio();
1610 static inline void restore_spu_lslr(struct spu_state *csa, struct spu *spu)
1612 struct spu_priv2 __iomem *priv2 = spu->priv2;
1614 /* Restore, Step 61:
1615 * Restore the SPU_LSLR register from CSA.
1617 out_be64(&priv2->spu_lslr_RW, csa->priv2.spu_lslr_RW);
1618 eieio();
1621 static inline void restore_spu_cfg(struct spu_state *csa, struct spu *spu)
1623 struct spu_priv2 __iomem *priv2 = spu->priv2;
1625 /* Restore, Step 62:
1626 * Restore the SPU_Cfg register from CSA.
1628 out_be64(&priv2->spu_cfg_RW, csa->priv2.spu_cfg_RW);
1629 eieio();
1632 static inline void restore_pm_trace(struct spu_state *csa, struct spu *spu)
1634 /* Restore, Step 63:
1635 * Restore PM_Trace_Tag_Wait_Mask from CSA.
1636 * Not performed by this implementation.
1640 static inline void restore_spu_npc(struct spu_state *csa, struct spu *spu)
1642 struct spu_problem __iomem *prob = spu->problem;
1644 /* Restore, Step 64:
1645 * Restore SPU_NPC from CSA.
1647 out_be32(&prob->spu_npc_RW, csa->prob.spu_npc_RW);
1648 eieio();
1651 static inline void restore_spu_mb(struct spu_state *csa, struct spu *spu)
1653 struct spu_priv2 __iomem *priv2 = spu->priv2;
1654 int i;
1656 /* Restore, Step 65:
1657 * Restore MFC_RdSPU_MB from CSA.
1659 out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
1660 eieio();
1661 out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[29]);
1662 for (i = 0; i < 4; i++) {
1663 out_be64(&priv2->spu_chnldata_RW, csa->spu_mailbox_data[i]);
1665 eieio();
1668 static inline void check_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
1670 struct spu_problem __iomem *prob = spu->problem;
1671 u32 dummy = 0;
1673 /* Restore, Step 66:
1674 * If CSA.MB_Stat[P]=0 (mailbox empty) then
1675 * read from the PPU_MB register.
1677 if ((csa->prob.mb_stat_R & 0xFF) == 0) {
1678 dummy = in_be32(&prob->pu_mb_R);
1679 eieio();
1683 static inline void check_ppuint_mb_stat(struct spu_state *csa, struct spu *spu)
1685 struct spu_priv2 __iomem *priv2 = spu->priv2;
1686 u64 dummy = 0UL;
1688 /* Restore, Step 66:
1689 * If CSA.MB_Stat[I]=0 (mailbox empty) then
1690 * read from the PPUINT_MB register.
1692 if ((csa->prob.mb_stat_R & 0xFF0000) == 0) {
1693 dummy = in_be64(&priv2->puint_mb_R);
1694 eieio();
1695 spu_int_stat_clear(spu, 2, CLASS2_ENABLE_MAILBOX_INTR);
1696 eieio();
1700 static inline void restore_mfc_sr1(struct spu_state *csa, struct spu *spu)
1702 /* Restore, Step 69:
1703 * Restore the MFC_SR1 register from CSA.
1705 spu_mfc_sr1_set(spu, csa->priv1.mfc_sr1_RW);
1706 eieio();
1709 static inline void restore_other_spu_access(struct spu_state *csa,
1710 struct spu *spu)
1712 /* Restore, Step 70:
1713 * Restore other SPU mappings to this SPU. TBD.
1717 static inline void restore_spu_runcntl(struct spu_state *csa, struct spu *spu)
1719 struct spu_problem __iomem *prob = spu->problem;
1721 /* Restore, Step 71:
1722 * If CSA.SPU_Status[R]=1 then write
1723 * SPU_RunCntl[R0R1]='01'.
1725 if (csa->prob.spu_status_R & SPU_STATUS_RUNNING) {
1726 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1727 eieio();
1731 static inline void restore_mfc_cntl(struct spu_state *csa, struct spu *spu)
1733 struct spu_priv2 __iomem *priv2 = spu->priv2;
1735 /* Restore, Step 72:
1736 * Restore the MFC_CNTL register for the CSA.
1738 out_be64(&priv2->mfc_control_RW, csa->priv2.mfc_control_RW);
1739 eieio();
1741 * FIXME: this is to restart a DMA that we were processing
1742 * before the save. better remember the fault information
1743 * in the csa instead.
1745 if ((csa->priv2.mfc_control_RW & MFC_CNTL_SUSPEND_DMA_QUEUE_MASK)) {
1746 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
1747 eieio();
1751 static inline void enable_user_access(struct spu_state *csa, struct spu *spu)
1753 /* Restore, Step 73:
1754 * Enable user-space access (if provided) to this
1755 * SPU by mapping the virtual pages assigned to
1756 * the SPU memory-mapped I/O (MMIO) for problem
1757 * state. TBD.
1761 static inline void reset_switch_active(struct spu_state *csa, struct spu *spu)
1763 /* Restore, Step 74:
1764 * Reset the "context switch active" flag.
1765 <<<<<<< HEAD:arch/powerpc/platforms/cell/spufs/switch.c
1766 =======
1767 * Not performed by this implementation.
1768 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/powerpc/platforms/cell/spufs/switch.c
1770 <<<<<<< HEAD:arch/powerpc/platforms/cell/spufs/switch.c
1771 clear_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags);
1772 mb();
1773 =======
1774 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/powerpc/platforms/cell/spufs/switch.c
1777 static inline void reenable_interrupts(struct spu_state *csa, struct spu *spu)
1779 /* Restore, Step 75:
1780 * Re-enable SPU interrupts.
1782 spin_lock_irq(&spu->register_lock);
1783 spu_int_mask_set(spu, 0, csa->priv1.int_mask_class0_RW);
1784 spu_int_mask_set(spu, 1, csa->priv1.int_mask_class1_RW);
1785 spu_int_mask_set(spu, 2, csa->priv1.int_mask_class2_RW);
1786 spin_unlock_irq(&spu->register_lock);
1789 static int quiece_spu(struct spu_state *prev, struct spu *spu)
1792 * Combined steps 2-18 of SPU context save sequence, which
1793 * quiesce the SPU state (disable SPU execution, MFC command
1794 * queues, decrementer, SPU interrupts, etc.).
1796 * Returns 0 on success.
1797 * 2 if failed step 2.
1798 * 6 if failed step 6.
1801 if (check_spu_isolate(prev, spu)) { /* Step 2. */
1802 return 2;
1804 disable_interrupts(prev, spu); /* Step 3. */
1805 set_watchdog_timer(prev, spu); /* Step 4. */
1806 inhibit_user_access(prev, spu); /* Step 5. */
1807 if (check_spu_isolate(prev, spu)) { /* Step 6. */
1808 return 6;
1810 set_switch_pending(prev, spu); /* Step 7. */
1811 save_mfc_cntl(prev, spu); /* Step 8. */
1812 save_spu_runcntl(prev, spu); /* Step 9. */
1813 save_mfc_sr1(prev, spu); /* Step 10. */
1814 save_spu_status(prev, spu); /* Step 11. */
1815 save_mfc_decr(prev, spu); /* Step 12. */
1816 halt_mfc_decr(prev, spu); /* Step 13. */
1817 save_timebase(prev, spu); /* Step 14. */
1818 remove_other_spu_access(prev, spu); /* Step 15. */
1819 do_mfc_mssync(prev, spu); /* Step 16. */
1820 issue_mfc_tlbie(prev, spu); /* Step 17. */
1821 handle_pending_interrupts(prev, spu); /* Step 18. */
1823 return 0;
1826 static void save_csa(struct spu_state *prev, struct spu *spu)
1829 * Combine steps 19-44 of SPU context save sequence, which
1830 * save regions of the privileged & problem state areas.
1833 save_mfc_queues(prev, spu); /* Step 19. */
1834 save_ppu_querymask(prev, spu); /* Step 20. */
1835 save_ppu_querytype(prev, spu); /* Step 21. */
1836 save_ppu_tagstatus(prev, spu); /* NEW. */
1837 save_mfc_csr_tsq(prev, spu); /* Step 22. */
1838 save_mfc_csr_cmd(prev, spu); /* Step 23. */
1839 save_mfc_csr_ato(prev, spu); /* Step 24. */
1840 save_mfc_tclass_id(prev, spu); /* Step 25. */
1841 set_mfc_tclass_id(prev, spu); /* Step 26. */
1842 purge_mfc_queue(prev, spu); /* Step 27. */
1843 wait_purge_complete(prev, spu); /* Step 28. */
1844 setup_mfc_sr1(prev, spu); /* Step 30. */
1845 save_spu_npc(prev, spu); /* Step 31. */
1846 save_spu_privcntl(prev, spu); /* Step 32. */
1847 reset_spu_privcntl(prev, spu); /* Step 33. */
1848 save_spu_lslr(prev, spu); /* Step 34. */
1849 reset_spu_lslr(prev, spu); /* Step 35. */
1850 save_spu_cfg(prev, spu); /* Step 36. */
1851 save_pm_trace(prev, spu); /* Step 37. */
1852 save_mfc_rag(prev, spu); /* Step 38. */
1853 save_ppu_mb_stat(prev, spu); /* Step 39. */
1854 save_ppu_mb(prev, spu); /* Step 40. */
1855 save_ppuint_mb(prev, spu); /* Step 41. */
1856 save_ch_part1(prev, spu); /* Step 42. */
1857 save_spu_mb(prev, spu); /* Step 43. */
1858 save_mfc_cmd(prev, spu); /* Step 44. */
1859 reset_ch(prev, spu); /* Step 45. */
1862 static void save_lscsa(struct spu_state *prev, struct spu *spu)
1865 * Perform steps 46-57 of SPU context save sequence,
1866 * which save regions of the local store and register
1867 * file.
1870 resume_mfc_queue(prev, spu); /* Step 46. */
1871 /* Step 47. */
1872 setup_mfc_slbs(prev, spu, spu_save_code, sizeof(spu_save_code));
1873 set_switch_active(prev, spu); /* Step 48. */
1874 enable_interrupts(prev, spu); /* Step 49. */
1875 save_ls_16kb(prev, spu); /* Step 50. */
1876 set_spu_npc(prev, spu); /* Step 51. */
1877 set_signot1(prev, spu); /* Step 52. */
1878 set_signot2(prev, spu); /* Step 53. */
1879 send_save_code(prev, spu); /* Step 54. */
1880 set_ppu_querymask(prev, spu); /* Step 55. */
1881 wait_tag_complete(prev, spu); /* Step 56. */
1882 wait_spu_stopped(prev, spu); /* Step 57. */
1885 static void force_spu_isolate_exit(struct spu *spu)
1887 struct spu_problem __iomem *prob = spu->problem;
1888 struct spu_priv2 __iomem *priv2 = spu->priv2;
1890 /* Stop SPE execution and wait for completion. */
1891 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1892 iobarrier_rw();
1893 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING);
1895 /* Restart SPE master runcntl. */
1896 spu_mfc_sr1_set(spu, MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1897 iobarrier_w();
1899 /* Initiate isolate exit request and wait for completion. */
1900 out_be64(&priv2->spu_privcntl_RW, 4LL);
1901 iobarrier_w();
1902 out_be32(&prob->spu_runcntl_RW, 2);
1903 iobarrier_rw();
1904 POLL_WHILE_FALSE((in_be32(&prob->spu_status_R)
1905 & SPU_STATUS_STOPPED_BY_STOP));
1907 /* Reset load request to normal. */
1908 out_be64(&priv2->spu_privcntl_RW, SPU_PRIVCNT_LOAD_REQUEST_NORMAL);
1909 iobarrier_w();
1913 * stop_spu_isolate
1914 * Check SPU run-control state and force isolated
1915 * exit function as necessary.
1917 static void stop_spu_isolate(struct spu *spu)
1919 struct spu_problem __iomem *prob = spu->problem;
1921 if (in_be32(&prob->spu_status_R) & SPU_STATUS_ISOLATED_STATE) {
1922 /* The SPU is in isolated state; the only way
1923 * to get it out is to perform an isolated
1924 * exit (clean) operation.
1926 force_spu_isolate_exit(spu);
1930 static void harvest(struct spu_state *prev, struct spu *spu)
1933 * Perform steps 2-25 of SPU context restore sequence,
1934 * which resets an SPU either after a failed save, or
1935 * when using SPU for first time.
1938 disable_interrupts(prev, spu); /* Step 2. */
1939 inhibit_user_access(prev, spu); /* Step 3. */
1940 terminate_spu_app(prev, spu); /* Step 4. */
1941 set_switch_pending(prev, spu); /* Step 5. */
1942 stop_spu_isolate(spu); /* NEW. */
1943 remove_other_spu_access(prev, spu); /* Step 6. */
1944 suspend_mfc_and_halt_decr(prev, spu); /* Step 7. */
1945 wait_suspend_mfc_complete(prev, spu); /* Step 8. */
1946 if (!suspend_spe(prev, spu)) /* Step 9. */
1947 clear_spu_status(prev, spu); /* Step 10. */
1948 do_mfc_mssync(prev, spu); /* Step 11. */
1949 issue_mfc_tlbie(prev, spu); /* Step 12. */
1950 handle_pending_interrupts(prev, spu); /* Step 13. */
1951 purge_mfc_queue(prev, spu); /* Step 14. */
1952 wait_purge_complete(prev, spu); /* Step 15. */
1953 reset_spu_privcntl(prev, spu); /* Step 16. */
1954 reset_spu_lslr(prev, spu); /* Step 17. */
1955 setup_mfc_sr1(prev, spu); /* Step 18. */
1956 spu_invalidate_slbs(spu); /* Step 19. */
1957 reset_ch_part1(prev, spu); /* Step 20. */
1958 reset_ch_part2(prev, spu); /* Step 21. */
1959 enable_interrupts(prev, spu); /* Step 22. */
1960 set_switch_active(prev, spu); /* Step 23. */
1961 set_mfc_tclass_id(prev, spu); /* Step 24. */
1962 resume_mfc_queue(prev, spu); /* Step 25. */
1965 static void restore_lscsa(struct spu_state *next, struct spu *spu)
1968 * Perform steps 26-40 of SPU context restore sequence,
1969 * which restores regions of the local store and register
1970 * file.
1973 set_watchdog_timer(next, spu); /* Step 26. */
1974 setup_spu_status_part1(next, spu); /* Step 27. */
1975 setup_spu_status_part2(next, spu); /* Step 28. */
1976 restore_mfc_rag(next, spu); /* Step 29. */
1977 /* Step 30. */
1978 setup_mfc_slbs(next, spu, spu_restore_code, sizeof(spu_restore_code));
1979 set_spu_npc(next, spu); /* Step 31. */
1980 set_signot1(next, spu); /* Step 32. */
1981 set_signot2(next, spu); /* Step 33. */
1982 setup_decr(next, spu); /* Step 34. */
1983 setup_ppu_mb(next, spu); /* Step 35. */
1984 setup_ppuint_mb(next, spu); /* Step 36. */
1985 send_restore_code(next, spu); /* Step 37. */
1986 set_ppu_querymask(next, spu); /* Step 38. */
1987 wait_tag_complete(next, spu); /* Step 39. */
1988 wait_spu_stopped(next, spu); /* Step 40. */
1991 static void restore_csa(struct spu_state *next, struct spu *spu)
1994 * Combine steps 41-76 of SPU context restore sequence, which
1995 * restore regions of the privileged & problem state areas.
1998 restore_spu_privcntl(next, spu); /* Step 41. */
1999 restore_status_part1(next, spu); /* Step 42. */
2000 restore_status_part2(next, spu); /* Step 43. */
2001 restore_ls_16kb(next, spu); /* Step 44. */
2002 wait_tag_complete(next, spu); /* Step 45. */
2003 suspend_mfc(next, spu); /* Step 46. */
2004 wait_suspend_mfc_complete(next, spu); /* Step 47. */
2005 issue_mfc_tlbie(next, spu); /* Step 48. */
2006 clear_interrupts(next, spu); /* Step 49. */
2007 restore_mfc_queues(next, spu); /* Step 50. */
2008 restore_ppu_querymask(next, spu); /* Step 51. */
2009 restore_ppu_querytype(next, spu); /* Step 52. */
2010 restore_mfc_csr_tsq(next, spu); /* Step 53. */
2011 restore_mfc_csr_cmd(next, spu); /* Step 54. */
2012 restore_mfc_csr_ato(next, spu); /* Step 55. */
2013 restore_mfc_tclass_id(next, spu); /* Step 56. */
2014 set_llr_event(next, spu); /* Step 57. */
2015 restore_decr_wrapped(next, spu); /* Step 58. */
2016 restore_ch_part1(next, spu); /* Step 59. */
2017 restore_ch_part2(next, spu); /* Step 60. */
2018 restore_spu_lslr(next, spu); /* Step 61. */
2019 restore_spu_cfg(next, spu); /* Step 62. */
2020 restore_pm_trace(next, spu); /* Step 63. */
2021 restore_spu_npc(next, spu); /* Step 64. */
2022 restore_spu_mb(next, spu); /* Step 65. */
2023 check_ppu_mb_stat(next, spu); /* Step 66. */
2024 check_ppuint_mb_stat(next, spu); /* Step 67. */
2025 spu_invalidate_slbs(spu); /* Modified Step 68. */
2026 restore_mfc_sr1(next, spu); /* Step 69. */
2027 restore_other_spu_access(next, spu); /* Step 70. */
2028 restore_spu_runcntl(next, spu); /* Step 71. */
2029 restore_mfc_cntl(next, spu); /* Step 72. */
2030 enable_user_access(next, spu); /* Step 73. */
2031 reset_switch_active(next, spu); /* Step 74. */
2032 reenable_interrupts(next, spu); /* Step 75. */
2035 static int __do_spu_save(struct spu_state *prev, struct spu *spu)
2037 int rc;
2040 * SPU context save can be broken into three phases:
2042 * (a) quiesce [steps 2-16].
2043 * (b) save of CSA, performed by PPE [steps 17-42]
2044 * (c) save of LSCSA, mostly performed by SPU [steps 43-52].
2046 * Returns 0 on success.
2047 * 2,6 if failed to quiece SPU
2048 * 53 if SPU-side of save failed.
2051 rc = quiece_spu(prev, spu); /* Steps 2-16. */
2052 switch (rc) {
2053 default:
2054 case 2:
2055 case 6:
2056 harvest(prev, spu);
2057 return rc;
2058 break;
2059 case 0:
2060 break;
2062 save_csa(prev, spu); /* Steps 17-43. */
2063 save_lscsa(prev, spu); /* Steps 44-53. */
2064 return check_save_status(prev, spu); /* Step 54. */
2067 static int __do_spu_restore(struct spu_state *next, struct spu *spu)
2069 int rc;
2072 * SPU context restore can be broken into three phases:
2074 * (a) harvest (or reset) SPU [steps 2-24].
2075 * (b) restore LSCSA [steps 25-40], mostly performed by SPU.
2076 * (c) restore CSA [steps 41-76], performed by PPE.
2078 * The 'harvest' step is not performed here, but rather
2079 * as needed below.
2082 restore_lscsa(next, spu); /* Steps 24-39. */
2083 rc = check_restore_status(next, spu); /* Step 40. */
2084 switch (rc) {
2085 default:
2086 /* Failed. Return now. */
2087 return rc;
2088 break;
2089 case 0:
2090 /* Fall through to next step. */
2091 break;
2093 restore_csa(next, spu);
2095 return 0;
2099 * spu_save - SPU context save, with locking.
2100 * @prev: pointer to SPU context save area, to be saved.
2101 * @spu: pointer to SPU iomem structure.
2103 * Acquire locks, perform the save operation then return.
2105 int spu_save(struct spu_state *prev, struct spu *spu)
2107 int rc;
2109 acquire_spu_lock(spu); /* Step 1. */
2110 rc = __do_spu_save(prev, spu); /* Steps 2-53. */
2111 release_spu_lock(spu);
2112 if (rc != 0 && rc != 2 && rc != 6) {
2113 panic("%s failed on SPU[%d], rc=%d.\n",
2114 __func__, spu->number, rc);
2116 return 0;
2118 EXPORT_SYMBOL_GPL(spu_save);
2121 * spu_restore - SPU context restore, with harvest and locking.
2122 * @new: pointer to SPU context save area, to be restored.
2123 * @spu: pointer to SPU iomem structure.
2125 * Perform harvest + restore, as we may not be coming
2126 * from a previous successful save operation, and the
2127 * hardware state is unknown.
2129 int spu_restore(struct spu_state *new, struct spu *spu)
2131 int rc;
2133 acquire_spu_lock(spu);
2134 harvest(NULL, spu);
2135 spu->slb_replace = 0;
2136 rc = __do_spu_restore(new, spu);
2137 release_spu_lock(spu);
2138 if (rc) {
2139 panic("%s failed on SPU[%d] rc=%d.\n",
2140 __func__, spu->number, rc);
2142 return rc;
2144 EXPORT_SYMBOL_GPL(spu_restore);
2146 static void init_prob(struct spu_state *csa)
2148 csa->spu_chnlcnt_RW[9] = 1;
2149 csa->spu_chnlcnt_RW[21] = 16;
2150 csa->spu_chnlcnt_RW[23] = 1;
2151 csa->spu_chnlcnt_RW[28] = 1;
2152 csa->spu_chnlcnt_RW[30] = 1;
2153 csa->prob.spu_runcntl_RW = SPU_RUNCNTL_STOP;
2154 csa->prob.mb_stat_R = 0x000400;
2157 static void init_priv1(struct spu_state *csa)
2159 /* Enable decode, relocate, tlbie response, master runcntl. */
2160 csa->priv1.mfc_sr1_RW = MFC_STATE1_LOCAL_STORAGE_DECODE_MASK |
2161 MFC_STATE1_MASTER_RUN_CONTROL_MASK |
2162 MFC_STATE1_PROBLEM_STATE_MASK |
2163 MFC_STATE1_RELOCATE_MASK | MFC_STATE1_BUS_TLBIE_MASK;
2165 /* Enable OS-specific set of interrupts. */
2166 csa->priv1.int_mask_class0_RW = CLASS0_ENABLE_DMA_ALIGNMENT_INTR |
2167 CLASS0_ENABLE_INVALID_DMA_COMMAND_INTR |
2168 CLASS0_ENABLE_SPU_ERROR_INTR;
2169 csa->priv1.int_mask_class1_RW = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
2170 CLASS1_ENABLE_STORAGE_FAULT_INTR;
2171 csa->priv1.int_mask_class2_RW = CLASS2_ENABLE_SPU_STOP_INTR |
2172 CLASS2_ENABLE_SPU_HALT_INTR |
2173 CLASS2_ENABLE_SPU_DMA_TAG_GROUP_COMPLETE_INTR;
2176 static void init_priv2(struct spu_state *csa)
2178 csa->priv2.spu_lslr_RW = LS_ADDR_MASK;
2179 csa->priv2.mfc_control_RW = MFC_CNTL_RESUME_DMA_QUEUE |
2180 MFC_CNTL_NORMAL_DMA_QUEUE_OPERATION |
2181 MFC_CNTL_DMA_QUEUES_EMPTY_MASK;
2185 * spu_alloc_csa - allocate and initialize an SPU context save area.
2187 * Allocate and initialize the contents of an SPU context save area.
2188 * This includes enabling address translation, interrupt masks, etc.,
2189 * as appropriate for the given OS environment.
2191 * Note that storage for the 'lscsa' is allocated separately,
2192 * as it is by far the largest of the context save regions,
2193 * and may need to be pinned or otherwise specially aligned.
2195 int spu_init_csa(struct spu_state *csa)
2197 int rc;
2199 if (!csa)
2200 return -EINVAL;
2201 memset(csa, 0, sizeof(struct spu_state));
2203 rc = spu_alloc_lscsa(csa);
2204 if (rc)
2205 return rc;
2207 spin_lock_init(&csa->register_lock);
2209 init_prob(csa);
2210 init_priv1(csa);
2211 init_priv2(csa);
2213 return 0;
2216 void spu_fini_csa(struct spu_state *csa)
2218 spu_free_lscsa(csa);