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)
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 #include <linux/hardirq.h>
38 #include <linux/sched.h>
39 #include <linux/kernel.h>
41 #include <linux/vmalloc.h>
42 #include <linux/smp.h>
43 #include <linux/stddef.h>
44 #include <linux/unistd.h>
48 #include <asm/spu_priv1.h>
49 #include <asm/spu_csa.h>
50 #include <asm/mmu_context.h>
54 #include "spu_save_dump.h"
55 #include "spu_restore_dump.h"
58 #define POLL_WHILE_TRUE(_c) { \
63 #define RELAX_SPIN_COUNT 1000
64 #define POLL_WHILE_TRUE(_c) { \
67 for (_i=0; _i<RELAX_SPIN_COUNT && (_c); _i++) { \
70 if (unlikely(_c)) yield(); \
76 #define POLL_WHILE_FALSE(_c) POLL_WHILE_TRUE(!(_c))
78 static inline void acquire_spu_lock(struct spu *spu)
82 * Acquire SPU-specific mutual exclusion lock.
87 static inline void release_spu_lock(struct spu *spu)
90 * Release SPU-specific mutual exclusion lock.
95 static inline int check_spu_isolate(struct spu_state *csa, struct spu *spu)
97 struct spu_problem __iomem *prob = spu->problem;
102 * If SPU_Status[E,L,IS] any field is '1', this
103 * SPU is in isolate state and cannot be context
104 * saved at this time.
106 isolate_state = SPU_STATUS_ISOLATED_STATE |
107 SPU_STATUS_ISOLATED_LOAD_STATUS | SPU_STATUS_ISOLATED_EXIT_STATUS;
108 return (in_be32(&prob->spu_status_R) & isolate_state) ? 1 : 0;
111 static inline void disable_interrupts(struct spu_state *csa, struct spu *spu)
115 * Save INT_Mask_class0 in CSA.
116 * Write INT_MASK_class0 with value of 0.
117 * Save INT_Mask_class1 in CSA.
118 * Write INT_MASK_class1 with value of 0.
119 * Save INT_Mask_class2 in CSA.
120 * Write INT_MASK_class2 with value of 0.
121 * Synchronize all three interrupts to be sure
122 * we no longer execute a handler on another CPU.
124 spin_lock_irq(&spu->register_lock);
126 csa->priv1.int_mask_class0_RW = spu_int_mask_get(spu, 0);
127 csa->priv1.int_mask_class1_RW = spu_int_mask_get(spu, 1);
128 csa->priv1.int_mask_class2_RW = spu_int_mask_get(spu, 2);
130 spu_int_mask_set(spu, 0, 0ul);
131 spu_int_mask_set(spu, 1, 0ul);
132 spu_int_mask_set(spu, 2, 0ul);
134 spin_unlock_irq(&spu->register_lock);
135 synchronize_irq(spu->irqs[0]);
136 synchronize_irq(spu->irqs[1]);
137 synchronize_irq(spu->irqs[2]);
140 static inline void set_watchdog_timer(struct spu_state *csa, struct spu *spu)
144 * Set a software watchdog timer, which specifies the
145 * maximum allowable time for a context save sequence.
147 * For present, this implementation will not set a global
148 * watchdog timer, as virtualization & variable system load
149 * may cause unpredictable execution times.
153 static inline void inhibit_user_access(struct spu_state *csa, struct spu *spu)
157 * Inhibit user-space access (if provided) to this
158 * SPU by unmapping the virtual pages assigned to
159 * the SPU memory-mapped I/O (MMIO) for problem
164 static inline void set_switch_pending(struct spu_state *csa, struct spu *spu)
168 * Set a software context switch pending flag.
170 set_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags);
174 static inline void save_mfc_cntl(struct spu_state *csa, struct spu *spu)
176 struct spu_priv2 __iomem *priv2 = spu->priv2;
179 * Suspend DMA and save MFC_CNTL.
181 switch (in_be64(&priv2->mfc_control_RW) &
182 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) {
183 case MFC_CNTL_SUSPEND_IN_PROGRESS:
184 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
185 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) ==
186 MFC_CNTL_SUSPEND_COMPLETE);
188 case MFC_CNTL_SUSPEND_COMPLETE:
190 csa->priv2.mfc_control_RW =
191 in_be64(&priv2->mfc_control_RW) |
192 MFC_CNTL_SUSPEND_DMA_QUEUE;
194 case MFC_CNTL_NORMAL_DMA_QUEUE_OPERATION:
195 out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE);
196 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
197 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) ==
198 MFC_CNTL_SUSPEND_COMPLETE);
200 csa->priv2.mfc_control_RW =
201 in_be64(&priv2->mfc_control_RW) &
202 ~MFC_CNTL_SUSPEND_DMA_QUEUE &
203 ~MFC_CNTL_SUSPEND_MASK;
208 static inline void save_spu_runcntl(struct spu_state *csa, struct spu *spu)
210 struct spu_problem __iomem *prob = spu->problem;
213 * Save SPU_Runcntl in the CSA. This value contains
214 * the "Application Desired State".
216 csa->prob.spu_runcntl_RW = in_be32(&prob->spu_runcntl_RW);
219 static inline void save_mfc_sr1(struct spu_state *csa, struct spu *spu)
222 * Save MFC_SR1 in the CSA.
224 csa->priv1.mfc_sr1_RW = spu_mfc_sr1_get(spu);
227 static inline void save_spu_status(struct spu_state *csa, struct spu *spu)
229 struct spu_problem __iomem *prob = spu->problem;
232 * Read SPU_Status[R], and save to CSA.
234 if ((in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) == 0) {
235 csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
239 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
241 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
244 SPU_STATUS_INVALID_INSTR | SPU_STATUS_SINGLE_STEP |
245 SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
246 if ((in_be32(&prob->spu_status_R) & stopped) == 0)
247 csa->prob.spu_status_R = SPU_STATUS_RUNNING;
249 csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
253 static inline void save_mfc_decr(struct spu_state *csa, struct spu *spu)
255 struct spu_priv2 __iomem *priv2 = spu->priv2;
258 * Read MFC_CNTL[Ds]. Update saved copy of
261 csa->priv2.mfc_control_RW |=
262 in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DECREMENTER_RUNNING;
265 static inline void halt_mfc_decr(struct spu_state *csa, struct spu *spu)
267 struct spu_priv2 __iomem *priv2 = spu->priv2;
270 * Write MFC_CNTL[Dh] set to a '1' to halt
273 out_be64(&priv2->mfc_control_RW,
274 MFC_CNTL_DECREMENTER_HALTED | MFC_CNTL_SUSPEND_MASK);
278 static inline void save_timebase(struct spu_state *csa, struct spu *spu)
281 * Read PPE Timebase High and Timebase low registers
282 * and save in CSA. TBD.
284 csa->suspend_time = get_cycles();
287 static inline void remove_other_spu_access(struct spu_state *csa,
291 * Remove other SPU access to this SPU by unmapping
292 * this SPU's pages from their address space. TBD.
296 static inline void do_mfc_mssync(struct spu_state *csa, struct spu *spu)
298 struct spu_problem __iomem *prob = spu->problem;
302 * Write SPU_MSSync register. Poll SPU_MSSync[P]
305 out_be64(&prob->spc_mssync_RW, 1UL);
306 POLL_WHILE_TRUE(in_be64(&prob->spc_mssync_RW) & MS_SYNC_PENDING);
309 static inline void issue_mfc_tlbie(struct spu_state *csa, struct spu *spu)
314 * Write TLB_Invalidate_Entry[IS,VPN,L,Lp]=0 register.
315 * Then issue a PPE sync instruction.
317 spu_tlb_invalidate(spu);
321 static inline void handle_pending_interrupts(struct spu_state *csa,
325 * Handle any pending interrupts from this SPU
326 * here. This is OS or hypervisor specific. One
327 * option is to re-enable interrupts to handle any
328 * pending interrupts, with the interrupt handlers
329 * recognizing the software Context Switch Pending
330 * flag, to ensure the SPU execution or MFC command
331 * queue is not restarted. TBD.
335 static inline void save_mfc_queues(struct spu_state *csa, struct spu *spu)
337 struct spu_priv2 __iomem *priv2 = spu->priv2;
341 * If MFC_Cntl[Se]=0 then save
342 * MFC command queues.
344 if ((in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DMA_QUEUES_EMPTY) == 0) {
345 for (i = 0; i < 8; i++) {
346 csa->priv2.puq[i].mfc_cq_data0_RW =
347 in_be64(&priv2->puq[i].mfc_cq_data0_RW);
348 csa->priv2.puq[i].mfc_cq_data1_RW =
349 in_be64(&priv2->puq[i].mfc_cq_data1_RW);
350 csa->priv2.puq[i].mfc_cq_data2_RW =
351 in_be64(&priv2->puq[i].mfc_cq_data2_RW);
352 csa->priv2.puq[i].mfc_cq_data3_RW =
353 in_be64(&priv2->puq[i].mfc_cq_data3_RW);
355 for (i = 0; i < 16; i++) {
356 csa->priv2.spuq[i].mfc_cq_data0_RW =
357 in_be64(&priv2->spuq[i].mfc_cq_data0_RW);
358 csa->priv2.spuq[i].mfc_cq_data1_RW =
359 in_be64(&priv2->spuq[i].mfc_cq_data1_RW);
360 csa->priv2.spuq[i].mfc_cq_data2_RW =
361 in_be64(&priv2->spuq[i].mfc_cq_data2_RW);
362 csa->priv2.spuq[i].mfc_cq_data3_RW =
363 in_be64(&priv2->spuq[i].mfc_cq_data3_RW);
368 static inline void save_ppu_querymask(struct spu_state *csa, struct spu *spu)
370 struct spu_problem __iomem *prob = spu->problem;
373 * Save the PPU_QueryMask register
376 csa->prob.dma_querymask_RW = in_be32(&prob->dma_querymask_RW);
379 static inline void save_ppu_querytype(struct spu_state *csa, struct spu *spu)
381 struct spu_problem __iomem *prob = spu->problem;
384 * Save the PPU_QueryType register
387 csa->prob.dma_querytype_RW = in_be32(&prob->dma_querytype_RW);
390 static inline void save_ppu_tagstatus(struct spu_state *csa, struct spu *spu)
392 struct spu_problem __iomem *prob = spu->problem;
394 /* Save the Prxy_TagStatus register in the CSA.
396 * It is unnecessary to restore dma_tagstatus_R, however,
397 * dma_tagstatus_R in the CSA is accessed via backing_ops, so
400 csa->prob.dma_tagstatus_R = in_be32(&prob->dma_tagstatus_R);
403 static inline void save_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
405 struct spu_priv2 __iomem *priv2 = spu->priv2;
408 * Save the MFC_CSR_TSQ register
411 csa->priv2.spu_tag_status_query_RW =
412 in_be64(&priv2->spu_tag_status_query_RW);
415 static inline void save_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
417 struct spu_priv2 __iomem *priv2 = spu->priv2;
420 * Save the MFC_CSR_CMD1 and MFC_CSR_CMD2
421 * registers in the CSA.
423 csa->priv2.spu_cmd_buf1_RW = in_be64(&priv2->spu_cmd_buf1_RW);
424 csa->priv2.spu_cmd_buf2_RW = in_be64(&priv2->spu_cmd_buf2_RW);
427 static inline void save_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
429 struct spu_priv2 __iomem *priv2 = spu->priv2;
432 * Save the MFC_CSR_ATO register in
435 csa->priv2.spu_atomic_status_RW = in_be64(&priv2->spu_atomic_status_RW);
438 static inline void save_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
441 * Save the MFC_TCLASS_ID register in
444 csa->priv1.mfc_tclass_id_RW = spu_mfc_tclass_id_get(spu);
447 static inline void set_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
451 * Write the MFC_TCLASS_ID register with
452 * the value 0x10000000.
454 spu_mfc_tclass_id_set(spu, 0x10000000);
458 static inline void purge_mfc_queue(struct spu_state *csa, struct spu *spu)
460 struct spu_priv2 __iomem *priv2 = spu->priv2;
464 * Write MFC_CNTL[Pc]=1 (purge queue).
466 out_be64(&priv2->mfc_control_RW,
467 MFC_CNTL_PURGE_DMA_REQUEST |
468 MFC_CNTL_SUSPEND_MASK);
472 static inline void wait_purge_complete(struct spu_state *csa, struct spu *spu)
474 struct spu_priv2 __iomem *priv2 = spu->priv2;
477 * Poll MFC_CNTL[Ps] until value '11' is read
480 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
481 MFC_CNTL_PURGE_DMA_STATUS_MASK) ==
482 MFC_CNTL_PURGE_DMA_COMPLETE);
485 static inline void setup_mfc_sr1(struct spu_state *csa, struct spu *spu)
489 * Write MFC_SR1 with MFC_SR1[D=0,S=1] and
490 * MFC_SR1[TL,R,Pr,T] set correctly for the
491 * OS specific environment.
493 * Implementation note: The SPU-side code
494 * for save/restore is privileged, so the
495 * MFC_SR1[Pr] bit is not set.
498 spu_mfc_sr1_set(spu, (MFC_STATE1_MASTER_RUN_CONTROL_MASK |
499 MFC_STATE1_RELOCATE_MASK |
500 MFC_STATE1_BUS_TLBIE_MASK));
503 static inline void save_spu_npc(struct spu_state *csa, struct spu *spu)
505 struct spu_problem __iomem *prob = spu->problem;
508 * Save SPU_NPC in the CSA.
510 csa->prob.spu_npc_RW = in_be32(&prob->spu_npc_RW);
513 static inline void save_spu_privcntl(struct spu_state *csa, struct spu *spu)
515 struct spu_priv2 __iomem *priv2 = spu->priv2;
518 * Save SPU_PrivCntl in the CSA.
520 csa->priv2.spu_privcntl_RW = in_be64(&priv2->spu_privcntl_RW);
523 static inline void reset_spu_privcntl(struct spu_state *csa, struct spu *spu)
525 struct spu_priv2 __iomem *priv2 = spu->priv2;
529 * Write SPU_PrivCntl[S,Le,A] fields reset to 0.
531 out_be64(&priv2->spu_privcntl_RW, 0UL);
535 static inline void save_spu_lslr(struct spu_state *csa, struct spu *spu)
537 struct spu_priv2 __iomem *priv2 = spu->priv2;
540 * Save SPU_LSLR in the CSA.
542 csa->priv2.spu_lslr_RW = in_be64(&priv2->spu_lslr_RW);
545 static inline void reset_spu_lslr(struct spu_state *csa, struct spu *spu)
547 struct spu_priv2 __iomem *priv2 = spu->priv2;
553 out_be64(&priv2->spu_lslr_RW, LS_ADDR_MASK);
557 static inline void save_spu_cfg(struct spu_state *csa, struct spu *spu)
559 struct spu_priv2 __iomem *priv2 = spu->priv2;
562 * Save SPU_Cfg in the CSA.
564 csa->priv2.spu_cfg_RW = in_be64(&priv2->spu_cfg_RW);
567 static inline void save_pm_trace(struct spu_state *csa, struct spu *spu)
570 * Save PM_Trace_Tag_Wait_Mask in the CSA.
571 * Not performed by this implementation.
575 static inline void save_mfc_rag(struct spu_state *csa, struct spu *spu)
578 * Save RA_GROUP_ID register and the
579 * RA_ENABLE reigster in the CSA.
581 csa->priv1.resource_allocation_groupID_RW =
582 spu_resource_allocation_groupID_get(spu);
583 csa->priv1.resource_allocation_enable_RW =
584 spu_resource_allocation_enable_get(spu);
587 static inline void save_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
589 struct spu_problem __iomem *prob = spu->problem;
592 * Save MB_Stat register in the CSA.
594 csa->prob.mb_stat_R = in_be32(&prob->mb_stat_R);
597 static inline void save_ppu_mb(struct spu_state *csa, struct spu *spu)
599 struct spu_problem __iomem *prob = spu->problem;
602 * Save the PPU_MB register in the CSA.
604 csa->prob.pu_mb_R = in_be32(&prob->pu_mb_R);
607 static inline void save_ppuint_mb(struct spu_state *csa, struct spu *spu)
609 struct spu_priv2 __iomem *priv2 = spu->priv2;
612 * Save the PPUINT_MB register in the CSA.
614 csa->priv2.puint_mb_R = in_be64(&priv2->puint_mb_R);
617 static inline void save_ch_part1(struct spu_state *csa, struct spu *spu)
619 struct spu_priv2 __iomem *priv2 = spu->priv2;
620 u64 idx, ch_indices[] = { 0UL, 3UL, 4UL, 24UL, 25UL, 27UL };
626 /* Save CH 1, without channel count */
627 out_be64(&priv2->spu_chnlcntptr_RW, 1);
628 csa->spu_chnldata_RW[1] = in_be64(&priv2->spu_chnldata_RW);
630 /* Save the following CH: [0,3,4,24,25,27] */
631 for (i = 0; i < ARRAY_SIZE(ch_indices); i++) {
633 out_be64(&priv2->spu_chnlcntptr_RW, idx);
635 csa->spu_chnldata_RW[idx] = in_be64(&priv2->spu_chnldata_RW);
636 csa->spu_chnlcnt_RW[idx] = in_be64(&priv2->spu_chnlcnt_RW);
637 out_be64(&priv2->spu_chnldata_RW, 0UL);
638 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
643 static inline void save_spu_mb(struct spu_state *csa, struct spu *spu)
645 struct spu_priv2 __iomem *priv2 = spu->priv2;
649 * Save SPU Read Mailbox Channel.
651 out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
653 csa->spu_chnlcnt_RW[29] = in_be64(&priv2->spu_chnlcnt_RW);
654 for (i = 0; i < 4; i++) {
655 csa->spu_mailbox_data[i] = in_be64(&priv2->spu_chnldata_RW);
657 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
661 static inline void save_mfc_cmd(struct spu_state *csa, struct spu *spu)
663 struct spu_priv2 __iomem *priv2 = spu->priv2;
666 * Save MFC_CMD Channel.
668 out_be64(&priv2->spu_chnlcntptr_RW, 21UL);
670 csa->spu_chnlcnt_RW[21] = in_be64(&priv2->spu_chnlcnt_RW);
674 static inline void reset_ch(struct spu_state *csa, struct spu *spu)
676 struct spu_priv2 __iomem *priv2 = spu->priv2;
677 u64 ch_indices[4] = { 21UL, 23UL, 28UL, 30UL };
678 u64 ch_counts[4] = { 16UL, 1UL, 1UL, 1UL };
683 * Reset the following CH: [21, 23, 28, 30]
685 for (i = 0; i < 4; i++) {
687 out_be64(&priv2->spu_chnlcntptr_RW, idx);
689 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
694 static inline void resume_mfc_queue(struct spu_state *csa, struct spu *spu)
696 struct spu_priv2 __iomem *priv2 = spu->priv2;
700 * Write MFC_CNTL[Sc]=0 (resume queue processing).
702 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESUME_DMA_QUEUE);
705 static inline void setup_mfc_slbs(struct spu_state *csa, struct spu *spu,
706 unsigned int *code, int code_size)
710 * If MFC_SR1[R]=1, write 0 to SLB_Invalidate_All
711 * register, then initialize SLB_VSID and SLB_ESID
712 * to provide access to SPU context save code and
715 * This implementation places both the context
716 * switch code and LSCSA in kernel address space.
718 * Further this implementation assumes that the
719 * MFC_SR1[R]=1 (in other words, assume that
720 * translation is desired by OS environment).
722 spu_invalidate_slbs(spu);
723 spu_setup_kernel_slbs(spu, csa->lscsa, code, code_size);
726 static inline void set_switch_active(struct spu_state *csa, struct spu *spu)
730 * Change the software context switch pending flag
731 * to context switch active.
733 * This implementation does not uses a switch active flag.
735 clear_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags);
739 static inline void enable_interrupts(struct spu_state *csa, struct spu *spu)
741 unsigned long class1_mask = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
742 CLASS1_ENABLE_STORAGE_FAULT_INTR;
746 * Reset and then enable interrupts, as
749 * This implementation enables only class1
750 * (translation) interrupts.
752 spin_lock_irq(&spu->register_lock);
753 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
754 spu_int_stat_clear(spu, 1, CLASS1_INTR_MASK);
755 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
756 spu_int_mask_set(spu, 0, 0ul);
757 spu_int_mask_set(spu, 1, class1_mask);
758 spu_int_mask_set(spu, 2, 0ul);
759 spin_unlock_irq(&spu->register_lock);
762 static inline int send_mfc_dma(struct spu *spu, unsigned long ea,
763 unsigned int ls_offset, unsigned int size,
764 unsigned int tag, unsigned int rclass,
767 struct spu_problem __iomem *prob = spu->problem;
768 union mfc_tag_size_class_cmd command;
769 unsigned int transfer_size;
770 volatile unsigned int status = 0x0;
774 (size > MFC_MAX_DMA_SIZE) ? MFC_MAX_DMA_SIZE : size;
775 command.u.mfc_size = transfer_size;
776 command.u.mfc_tag = tag;
777 command.u.mfc_rclassid = rclass;
778 command.u.mfc_cmd = cmd;
780 out_be32(&prob->mfc_lsa_W, ls_offset);
781 out_be64(&prob->mfc_ea_W, ea);
782 out_be64(&prob->mfc_union_W.all64, command.all64);
784 in_be32(&prob->mfc_union_W.by32.mfc_class_cmd32);
785 if (unlikely(status & 0x2)) {
788 } while (status & 0x3);
789 size -= transfer_size;
791 ls_offset += transfer_size;
796 static inline void save_ls_16kb(struct spu_state *csa, struct spu *spu)
798 unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
799 unsigned int ls_offset = 0x0;
800 unsigned int size = 16384;
801 unsigned int tag = 0;
802 unsigned int rclass = 0;
803 unsigned int cmd = MFC_PUT_CMD;
806 * Issue a DMA command to copy the first 16K bytes
807 * of local storage to the CSA.
809 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
812 static inline void set_spu_npc(struct spu_state *csa, struct spu *spu)
814 struct spu_problem __iomem *prob = spu->problem;
818 * Write SPU_NPC[IE]=0 and SPU_NPC[LSA] to entry
819 * point address of context save code in local
822 * This implementation uses SPU-side save/restore
823 * programs with entry points at LSA of 0.
825 out_be32(&prob->spu_npc_RW, 0);
829 static inline void set_signot1(struct spu_state *csa, struct spu *spu)
831 struct spu_problem __iomem *prob = spu->problem;
839 * Write SPU_Sig_Notify_1 register with upper 32-bits
840 * of the CSA.LSCSA effective address.
842 addr64.ull = (u64) csa->lscsa;
843 out_be32(&prob->signal_notify1, addr64.ui[0]);
847 static inline void set_signot2(struct spu_state *csa, struct spu *spu)
849 struct spu_problem __iomem *prob = spu->problem;
857 * Write SPU_Sig_Notify_2 register with lower 32-bits
858 * of the CSA.LSCSA effective address.
860 addr64.ull = (u64) csa->lscsa;
861 out_be32(&prob->signal_notify2, addr64.ui[1]);
865 static inline void send_save_code(struct spu_state *csa, struct spu *spu)
867 unsigned long addr = (unsigned long)&spu_save_code[0];
868 unsigned int ls_offset = 0x0;
869 unsigned int size = sizeof(spu_save_code);
870 unsigned int tag = 0;
871 unsigned int rclass = 0;
872 unsigned int cmd = MFC_GETFS_CMD;
875 * Issue a DMA command to copy context save code
876 * to local storage and start SPU.
878 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
881 static inline void set_ppu_querymask(struct spu_state *csa, struct spu *spu)
883 struct spu_problem __iomem *prob = spu->problem;
887 * Write PPU_QueryMask=1 (enable Tag Group 0)
888 * and issue eieio instruction.
890 out_be32(&prob->dma_querymask_RW, MFC_TAGID_TO_TAGMASK(0));
894 static inline void wait_tag_complete(struct spu_state *csa, struct spu *spu)
896 struct spu_problem __iomem *prob = spu->problem;
897 u32 mask = MFC_TAGID_TO_TAGMASK(0);
904 * Poll PPU_TagStatus[gn] until 01 (Tag group 0 complete)
905 * or write PPU_QueryType[TS]=01 and wait for Tag Group
906 * Complete Interrupt. Write INT_Stat_Class0 or
907 * INT_Stat_Class2 with value of 'handled'.
909 POLL_WHILE_FALSE(in_be32(&prob->dma_tagstatus_R) & mask);
911 local_irq_save(flags);
912 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
913 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
914 local_irq_restore(flags);
917 static inline void wait_spu_stopped(struct spu_state *csa, struct spu *spu)
919 struct spu_problem __iomem *prob = spu->problem;
924 * Poll until SPU_Status[R]=0 or wait for SPU Class 0
925 * or SPU Class 2 interrupt. Write INT_Stat_class0
926 * or INT_Stat_class2 with value of handled.
928 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING);
930 local_irq_save(flags);
931 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
932 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
933 local_irq_restore(flags);
936 static inline int check_save_status(struct spu_state *csa, struct spu *spu)
938 struct spu_problem __iomem *prob = spu->problem;
942 * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
943 * context save succeeded, otherwise context save
946 complete = ((SPU_SAVE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
947 SPU_STATUS_STOPPED_BY_STOP);
948 return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
951 static inline void terminate_spu_app(struct spu_state *csa, struct spu *spu)
954 * If required, notify the "using application" that
955 * the SPU task has been terminated. TBD.
959 static inline void suspend_mfc_and_halt_decr(struct spu_state *csa,
962 struct spu_priv2 __iomem *priv2 = spu->priv2;
965 * Write MFC_Cntl[Dh,Sc,Sm]='1','1','0' to suspend
966 * the queue and halt the decrementer.
968 out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE |
969 MFC_CNTL_DECREMENTER_HALTED);
973 static inline void wait_suspend_mfc_complete(struct spu_state *csa,
976 struct spu_priv2 __iomem *priv2 = spu->priv2;
980 * Poll MFC_CNTL[Ss] until 11 is returned.
982 POLL_WHILE_FALSE((in_be64(&priv2->mfc_control_RW) &
983 MFC_CNTL_SUSPEND_DMA_STATUS_MASK) ==
984 MFC_CNTL_SUSPEND_COMPLETE);
987 static inline int suspend_spe(struct spu_state *csa, struct spu *spu)
989 struct spu_problem __iomem *prob = spu->problem;
992 * If SPU_Status[R]=1, stop SPU execution
993 * and wait for stop to complete.
995 * Returns 1 if SPU_Status[R]=1 on entry.
998 if (in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) {
999 if (in_be32(&prob->spu_status_R) &
1000 SPU_STATUS_ISOLATED_EXIT_STATUS) {
1001 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1002 SPU_STATUS_RUNNING);
1004 if ((in_be32(&prob->spu_status_R) &
1005 SPU_STATUS_ISOLATED_LOAD_STATUS)
1006 || (in_be32(&prob->spu_status_R) &
1007 SPU_STATUS_ISOLATED_STATE)) {
1008 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1010 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1011 SPU_STATUS_RUNNING);
1012 out_be32(&prob->spu_runcntl_RW, 0x2);
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_WAITING_FOR_CHANNEL) {
1019 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1021 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1022 SPU_STATUS_RUNNING);
1029 static inline void clear_spu_status(struct spu_state *csa, struct spu *spu)
1031 struct spu_problem __iomem *prob = spu->problem;
1033 /* Restore, Step 10:
1034 * If SPU_Status[R]=0 and SPU_Status[E,L,IS]=1,
1035 * release SPU from isolate state.
1037 if (!(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING)) {
1038 if (in_be32(&prob->spu_status_R) &
1039 SPU_STATUS_ISOLATED_EXIT_STATUS) {
1040 spu_mfc_sr1_set(spu,
1041 MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1043 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1045 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1046 SPU_STATUS_RUNNING);
1048 if ((in_be32(&prob->spu_status_R) &
1049 SPU_STATUS_ISOLATED_LOAD_STATUS)
1050 || (in_be32(&prob->spu_status_R) &
1051 SPU_STATUS_ISOLATED_STATE)) {
1052 spu_mfc_sr1_set(spu,
1053 MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1055 out_be32(&prob->spu_runcntl_RW, 0x2);
1057 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1058 SPU_STATUS_RUNNING);
1063 static inline void reset_ch_part1(struct spu_state *csa, struct spu *spu)
1065 struct spu_priv2 __iomem *priv2 = spu->priv2;
1066 u64 ch_indices[] = { 0UL, 3UL, 4UL, 24UL, 25UL, 27UL };
1070 /* Restore, Step 20:
1074 out_be64(&priv2->spu_chnlcntptr_RW, 1);
1075 out_be64(&priv2->spu_chnldata_RW, 0UL);
1077 /* Reset the following CH: [0,3,4,24,25,27] */
1078 for (i = 0; i < ARRAY_SIZE(ch_indices); i++) {
1079 idx = ch_indices[i];
1080 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1082 out_be64(&priv2->spu_chnldata_RW, 0UL);
1083 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
1088 static inline void reset_ch_part2(struct spu_state *csa, struct spu *spu)
1090 struct spu_priv2 __iomem *priv2 = spu->priv2;
1091 u64 ch_indices[5] = { 21UL, 23UL, 28UL, 29UL, 30UL };
1092 u64 ch_counts[5] = { 16UL, 1UL, 1UL, 0UL, 1UL };
1096 /* Restore, Step 21:
1097 * Reset the following CH: [21, 23, 28, 29, 30]
1099 for (i = 0; i < 5; i++) {
1100 idx = ch_indices[i];
1101 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1103 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
1108 static inline void setup_spu_status_part1(struct spu_state *csa,
1111 u32 status_P = SPU_STATUS_STOPPED_BY_STOP;
1112 u32 status_I = SPU_STATUS_INVALID_INSTR;
1113 u32 status_H = SPU_STATUS_STOPPED_BY_HALT;
1114 u32 status_S = SPU_STATUS_SINGLE_STEP;
1115 u32 status_S_I = SPU_STATUS_SINGLE_STEP | SPU_STATUS_INVALID_INSTR;
1116 u32 status_S_P = SPU_STATUS_SINGLE_STEP | SPU_STATUS_STOPPED_BY_STOP;
1117 u32 status_P_H = SPU_STATUS_STOPPED_BY_HALT |SPU_STATUS_STOPPED_BY_STOP;
1118 u32 status_P_I = SPU_STATUS_STOPPED_BY_STOP |SPU_STATUS_INVALID_INSTR;
1121 /* Restore, Step 27:
1122 * If the CSA.SPU_Status[I,S,H,P]=1 then add the correct
1123 * instruction sequence to the end of the SPU based restore
1124 * code (after the "context restored" stop and signal) to
1125 * restore the correct SPU status.
1127 * NOTE: Rather than modifying the SPU executable, we
1128 * instead add a new 'stopped_status' field to the
1129 * LSCSA. The SPU-side restore reads this field and
1130 * takes the appropriate action when exiting.
1134 (csa->prob.spu_status_R >> SPU_STOP_STATUS_SHIFT) & 0xFFFF;
1135 if ((csa->prob.spu_status_R & status_P_I) == status_P_I) {
1137 /* SPU_Status[P,I]=1 - Illegal Instruction followed
1138 * by Stop and Signal instruction, followed by 'br -4'.
1141 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_I;
1142 csa->lscsa->stopped_status.slot[1] = status_code;
1144 } else if ((csa->prob.spu_status_R & status_P_H) == status_P_H) {
1146 /* SPU_Status[P,H]=1 - Halt Conditional, followed
1147 * by Stop and Signal instruction, followed by
1150 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_H;
1151 csa->lscsa->stopped_status.slot[1] = status_code;
1153 } else if ((csa->prob.spu_status_R & status_S_P) == status_S_P) {
1155 /* SPU_Status[S,P]=1 - Stop and Signal instruction
1156 * followed by 'br -4'.
1158 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_P;
1159 csa->lscsa->stopped_status.slot[1] = status_code;
1161 } else if ((csa->prob.spu_status_R & status_S_I) == status_S_I) {
1163 /* SPU_Status[S,I]=1 - Illegal instruction followed
1166 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_I;
1167 csa->lscsa->stopped_status.slot[1] = status_code;
1169 } else if ((csa->prob.spu_status_R & status_P) == status_P) {
1171 /* SPU_Status[P]=1 - Stop and Signal instruction
1172 * followed by 'br -4'.
1174 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P;
1175 csa->lscsa->stopped_status.slot[1] = status_code;
1177 } else if ((csa->prob.spu_status_R & status_H) == status_H) {
1179 /* SPU_Status[H]=1 - Halt Conditional, followed
1182 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_H;
1184 } else if ((csa->prob.spu_status_R & status_S) == status_S) {
1186 /* SPU_Status[S]=1 - Two nop instructions.
1188 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S;
1190 } else if ((csa->prob.spu_status_R & status_I) == status_I) {
1192 /* SPU_Status[I]=1 - Illegal instruction followed
1195 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_I;
1200 static inline void setup_spu_status_part2(struct spu_state *csa,
1205 /* Restore, Step 28:
1206 * If the CSA.SPU_Status[I,S,H,P,R]=0 then
1207 * add a 'br *' instruction to the end of
1208 * the SPU based restore code.
1210 * NOTE: Rather than modifying the SPU executable, we
1211 * instead add a new 'stopped_status' field to the
1212 * LSCSA. The SPU-side restore reads this field and
1213 * takes the appropriate action when exiting.
1215 mask = SPU_STATUS_INVALID_INSTR |
1216 SPU_STATUS_SINGLE_STEP |
1217 SPU_STATUS_STOPPED_BY_HALT |
1218 SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
1219 if (!(csa->prob.spu_status_R & mask)) {
1220 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_R;
1224 static inline void restore_mfc_rag(struct spu_state *csa, struct spu *spu)
1226 /* Restore, Step 29:
1227 * Restore RA_GROUP_ID register and the
1228 * RA_ENABLE reigster from the CSA.
1230 spu_resource_allocation_groupID_set(spu,
1231 csa->priv1.resource_allocation_groupID_RW);
1232 spu_resource_allocation_enable_set(spu,
1233 csa->priv1.resource_allocation_enable_RW);
1236 static inline void send_restore_code(struct spu_state *csa, struct spu *spu)
1238 unsigned long addr = (unsigned long)&spu_restore_code[0];
1239 unsigned int ls_offset = 0x0;
1240 unsigned int size = sizeof(spu_restore_code);
1241 unsigned int tag = 0;
1242 unsigned int rclass = 0;
1243 unsigned int cmd = MFC_GETFS_CMD;
1245 /* Restore, Step 37:
1246 * Issue MFC DMA command to copy context
1247 * restore code to local storage.
1249 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
1252 static inline void setup_decr(struct spu_state *csa, struct spu *spu)
1254 /* Restore, Step 34:
1255 * If CSA.MFC_CNTL[Ds]=1 (decrementer was
1256 * running) then adjust decrementer, set
1257 * decrementer running status in LSCSA,
1258 * and set decrementer "wrapped" status
1261 if (csa->priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING) {
1262 cycles_t resume_time = get_cycles();
1263 cycles_t delta_time = resume_time - csa->suspend_time;
1265 csa->lscsa->decr_status.slot[0] = SPU_DECR_STATUS_RUNNING;
1266 if (csa->lscsa->decr.slot[0] < delta_time) {
1267 csa->lscsa->decr_status.slot[0] |=
1268 SPU_DECR_STATUS_WRAPPED;
1271 csa->lscsa->decr.slot[0] -= delta_time;
1273 csa->lscsa->decr_status.slot[0] = 0;
1277 static inline void setup_ppu_mb(struct spu_state *csa, struct spu *spu)
1279 /* Restore, Step 35:
1280 * Copy the CSA.PU_MB data into the LSCSA.
1282 csa->lscsa->ppu_mb.slot[0] = csa->prob.pu_mb_R;
1285 static inline void setup_ppuint_mb(struct spu_state *csa, struct spu *spu)
1287 /* Restore, Step 36:
1288 * Copy the CSA.PUINT_MB data into the LSCSA.
1290 csa->lscsa->ppuint_mb.slot[0] = csa->priv2.puint_mb_R;
1293 static inline int check_restore_status(struct spu_state *csa, struct spu *spu)
1295 struct spu_problem __iomem *prob = spu->problem;
1298 /* Restore, Step 40:
1299 * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
1300 * context restore succeeded, otherwise context restore
1303 complete = ((SPU_RESTORE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
1304 SPU_STATUS_STOPPED_BY_STOP);
1305 return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
1308 static inline void restore_spu_privcntl(struct spu_state *csa, struct spu *spu)
1310 struct spu_priv2 __iomem *priv2 = spu->priv2;
1312 /* Restore, Step 41:
1313 * Restore SPU_PrivCntl from the CSA.
1315 out_be64(&priv2->spu_privcntl_RW, csa->priv2.spu_privcntl_RW);
1319 static inline void restore_status_part1(struct spu_state *csa, struct spu *spu)
1321 struct spu_problem __iomem *prob = spu->problem;
1324 /* Restore, Step 42:
1325 * If any CSA.SPU_Status[I,S,H,P]=1, then
1326 * restore the error or single step state.
1328 mask = SPU_STATUS_INVALID_INSTR |
1329 SPU_STATUS_SINGLE_STEP |
1330 SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
1331 if (csa->prob.spu_status_R & mask) {
1332 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1334 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1335 SPU_STATUS_RUNNING);
1339 static inline void restore_status_part2(struct spu_state *csa, struct spu *spu)
1341 struct spu_problem __iomem *prob = spu->problem;
1344 /* Restore, Step 43:
1345 * If all CSA.SPU_Status[I,S,H,P,R]=0 then write
1346 * SPU_RunCntl[R0R1]='01', wait for SPU_Status[R]=1,
1347 * then write '00' to SPU_RunCntl[R0R1] and wait
1348 * for SPU_Status[R]=0.
1350 mask = SPU_STATUS_INVALID_INSTR |
1351 SPU_STATUS_SINGLE_STEP |
1352 SPU_STATUS_STOPPED_BY_HALT |
1353 SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
1354 if (!(csa->prob.spu_status_R & mask)) {
1355 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1357 POLL_WHILE_FALSE(in_be32(&prob->spu_status_R) &
1358 SPU_STATUS_RUNNING);
1359 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1361 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1362 SPU_STATUS_RUNNING);
1366 static inline void restore_ls_16kb(struct spu_state *csa, struct spu *spu)
1368 unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
1369 unsigned int ls_offset = 0x0;
1370 unsigned int size = 16384;
1371 unsigned int tag = 0;
1372 unsigned int rclass = 0;
1373 unsigned int cmd = MFC_GET_CMD;
1375 /* Restore, Step 44:
1376 * Issue a DMA command to restore the first
1377 * 16kb of local storage from CSA.
1379 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
1382 static inline void suspend_mfc(struct spu_state *csa, struct spu *spu)
1384 struct spu_priv2 __iomem *priv2 = spu->priv2;
1386 /* Restore, Step 47.
1387 * Write MFC_Cntl[Sc,Sm]='1','0' to suspend
1390 out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE);
1394 static inline void clear_interrupts(struct spu_state *csa, struct spu *spu)
1396 /* Restore, Step 49:
1397 * Write INT_MASK_class0 with value of 0.
1398 * Write INT_MASK_class1 with value of 0.
1399 * Write INT_MASK_class2 with value of 0.
1400 * Write INT_STAT_class0 with value of -1.
1401 * Write INT_STAT_class1 with value of -1.
1402 * Write INT_STAT_class2 with value of -1.
1404 spin_lock_irq(&spu->register_lock);
1405 spu_int_mask_set(spu, 0, 0ul);
1406 spu_int_mask_set(spu, 1, 0ul);
1407 spu_int_mask_set(spu, 2, 0ul);
1408 spu_int_stat_clear(spu, 0, CLASS0_INTR_MASK);
1409 spu_int_stat_clear(spu, 1, CLASS1_INTR_MASK);
1410 spu_int_stat_clear(spu, 2, CLASS2_INTR_MASK);
1411 spin_unlock_irq(&spu->register_lock);
1414 static inline void restore_mfc_queues(struct spu_state *csa, struct spu *spu)
1416 struct spu_priv2 __iomem *priv2 = spu->priv2;
1419 /* Restore, Step 50:
1420 * If MFC_Cntl[Se]!=0 then restore
1421 * MFC command queues.
1423 if ((csa->priv2.mfc_control_RW & MFC_CNTL_DMA_QUEUES_EMPTY_MASK) == 0) {
1424 for (i = 0; i < 8; i++) {
1425 out_be64(&priv2->puq[i].mfc_cq_data0_RW,
1426 csa->priv2.puq[i].mfc_cq_data0_RW);
1427 out_be64(&priv2->puq[i].mfc_cq_data1_RW,
1428 csa->priv2.puq[i].mfc_cq_data1_RW);
1429 out_be64(&priv2->puq[i].mfc_cq_data2_RW,
1430 csa->priv2.puq[i].mfc_cq_data2_RW);
1431 out_be64(&priv2->puq[i].mfc_cq_data3_RW,
1432 csa->priv2.puq[i].mfc_cq_data3_RW);
1434 for (i = 0; i < 16; i++) {
1435 out_be64(&priv2->spuq[i].mfc_cq_data0_RW,
1436 csa->priv2.spuq[i].mfc_cq_data0_RW);
1437 out_be64(&priv2->spuq[i].mfc_cq_data1_RW,
1438 csa->priv2.spuq[i].mfc_cq_data1_RW);
1439 out_be64(&priv2->spuq[i].mfc_cq_data2_RW,
1440 csa->priv2.spuq[i].mfc_cq_data2_RW);
1441 out_be64(&priv2->spuq[i].mfc_cq_data3_RW,
1442 csa->priv2.spuq[i].mfc_cq_data3_RW);
1448 static inline void restore_ppu_querymask(struct spu_state *csa, struct spu *spu)
1450 struct spu_problem __iomem *prob = spu->problem;
1452 /* Restore, Step 51:
1453 * Restore the PPU_QueryMask register from CSA.
1455 out_be32(&prob->dma_querymask_RW, csa->prob.dma_querymask_RW);
1459 static inline void restore_ppu_querytype(struct spu_state *csa, struct spu *spu)
1461 struct spu_problem __iomem *prob = spu->problem;
1463 /* Restore, Step 52:
1464 * Restore the PPU_QueryType register from CSA.
1466 out_be32(&prob->dma_querytype_RW, csa->prob.dma_querytype_RW);
1470 static inline void restore_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
1472 struct spu_priv2 __iomem *priv2 = spu->priv2;
1474 /* Restore, Step 53:
1475 * Restore the MFC_CSR_TSQ register from CSA.
1477 out_be64(&priv2->spu_tag_status_query_RW,
1478 csa->priv2.spu_tag_status_query_RW);
1482 static inline void restore_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
1484 struct spu_priv2 __iomem *priv2 = spu->priv2;
1486 /* Restore, Step 54:
1487 * Restore the MFC_CSR_CMD1 and MFC_CSR_CMD2
1488 * registers from CSA.
1490 out_be64(&priv2->spu_cmd_buf1_RW, csa->priv2.spu_cmd_buf1_RW);
1491 out_be64(&priv2->spu_cmd_buf2_RW, csa->priv2.spu_cmd_buf2_RW);
1495 static inline void restore_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
1497 struct spu_priv2 __iomem *priv2 = spu->priv2;
1499 /* Restore, Step 55:
1500 * Restore the MFC_CSR_ATO register from CSA.
1502 out_be64(&priv2->spu_atomic_status_RW, csa->priv2.spu_atomic_status_RW);
1505 static inline void restore_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
1507 /* Restore, Step 56:
1508 * Restore the MFC_TCLASS_ID register from CSA.
1510 spu_mfc_tclass_id_set(spu, csa->priv1.mfc_tclass_id_RW);
1514 static inline void set_llr_event(struct spu_state *csa, struct spu *spu)
1516 u64 ch0_cnt, ch0_data;
1519 /* Restore, Step 57:
1520 * Set the Lock Line Reservation Lost Event by:
1521 * 1. OR CSA.SPU_Event_Status with bit 21 (Lr) set to 1.
1522 * 2. If CSA.SPU_Channel_0_Count=0 and
1523 * CSA.SPU_Wr_Event_Mask[Lr]=1 and
1524 * CSA.SPU_Event_Status[Lr]=0 then set
1525 * CSA.SPU_Event_Status_Count=1.
1527 ch0_cnt = csa->spu_chnlcnt_RW[0];
1528 ch0_data = csa->spu_chnldata_RW[0];
1529 ch1_data = csa->spu_chnldata_RW[1];
1530 csa->spu_chnldata_RW[0] |= MFC_LLR_LOST_EVENT;
1531 if ((ch0_cnt == 0) && !(ch0_data & MFC_LLR_LOST_EVENT) &&
1532 (ch1_data & MFC_LLR_LOST_EVENT)) {
1533 csa->spu_chnlcnt_RW[0] = 1;
1537 static inline void restore_decr_wrapped(struct spu_state *csa, struct spu *spu)
1539 /* Restore, Step 58:
1540 * If the status of the CSA software decrementer
1541 * "wrapped" flag is set, OR in a '1' to
1542 * CSA.SPU_Event_Status[Tm].
1544 if (!(csa->lscsa->decr_status.slot[0] & SPU_DECR_STATUS_WRAPPED))
1547 if ((csa->spu_chnlcnt_RW[0] == 0) &&
1548 (csa->spu_chnldata_RW[1] & 0x20) &&
1549 !(csa->spu_chnldata_RW[0] & 0x20))
1550 csa->spu_chnlcnt_RW[0] = 1;
1552 csa->spu_chnldata_RW[0] |= 0x20;
1555 static inline void restore_ch_part1(struct spu_state *csa, struct spu *spu)
1557 struct spu_priv2 __iomem *priv2 = spu->priv2;
1558 u64 idx, ch_indices[] = { 0UL, 3UL, 4UL, 24UL, 25UL, 27UL };
1561 /* Restore, Step 59:
1562 * Restore the following CH: [0,3,4,24,25,27]
1564 for (i = 0; i < ARRAY_SIZE(ch_indices); i++) {
1565 idx = ch_indices[i];
1566 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1568 out_be64(&priv2->spu_chnldata_RW, csa->spu_chnldata_RW[idx]);
1569 out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[idx]);
1574 static inline void restore_ch_part2(struct spu_state *csa, struct spu *spu)
1576 struct spu_priv2 __iomem *priv2 = spu->priv2;
1577 u64 ch_indices[3] = { 9UL, 21UL, 23UL };
1578 u64 ch_counts[3] = { 1UL, 16UL, 1UL };
1582 /* Restore, Step 60:
1583 * Restore the following CH: [9,21,23].
1586 ch_counts[1] = csa->spu_chnlcnt_RW[21];
1588 for (i = 0; i < 3; i++) {
1589 idx = ch_indices[i];
1590 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1592 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
1597 static inline void restore_spu_lslr(struct spu_state *csa, struct spu *spu)
1599 struct spu_priv2 __iomem *priv2 = spu->priv2;
1601 /* Restore, Step 61:
1602 * Restore the SPU_LSLR register from CSA.
1604 out_be64(&priv2->spu_lslr_RW, csa->priv2.spu_lslr_RW);
1608 static inline void restore_spu_cfg(struct spu_state *csa, struct spu *spu)
1610 struct spu_priv2 __iomem *priv2 = spu->priv2;
1612 /* Restore, Step 62:
1613 * Restore the SPU_Cfg register from CSA.
1615 out_be64(&priv2->spu_cfg_RW, csa->priv2.spu_cfg_RW);
1619 static inline void restore_pm_trace(struct spu_state *csa, struct spu *spu)
1621 /* Restore, Step 63:
1622 * Restore PM_Trace_Tag_Wait_Mask from CSA.
1623 * Not performed by this implementation.
1627 static inline void restore_spu_npc(struct spu_state *csa, struct spu *spu)
1629 struct spu_problem __iomem *prob = spu->problem;
1631 /* Restore, Step 64:
1632 * Restore SPU_NPC from CSA.
1634 out_be32(&prob->spu_npc_RW, csa->prob.spu_npc_RW);
1638 static inline void restore_spu_mb(struct spu_state *csa, struct spu *spu)
1640 struct spu_priv2 __iomem *priv2 = spu->priv2;
1643 /* Restore, Step 65:
1644 * Restore MFC_RdSPU_MB from CSA.
1646 out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
1648 out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[29]);
1649 for (i = 0; i < 4; i++) {
1650 out_be64(&priv2->spu_chnldata_RW, csa->spu_mailbox_data[i]);
1655 static inline void check_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
1657 struct spu_problem __iomem *prob = spu->problem;
1660 /* Restore, Step 66:
1661 * If CSA.MB_Stat[P]=0 (mailbox empty) then
1662 * read from the PPU_MB register.
1664 if ((csa->prob.mb_stat_R & 0xFF) == 0) {
1665 dummy = in_be32(&prob->pu_mb_R);
1670 static inline void check_ppuint_mb_stat(struct spu_state *csa, struct spu *spu)
1672 struct spu_priv2 __iomem *priv2 = spu->priv2;
1675 /* Restore, Step 66:
1676 * If CSA.MB_Stat[I]=0 (mailbox empty) then
1677 * read from the PPUINT_MB register.
1679 if ((csa->prob.mb_stat_R & 0xFF0000) == 0) {
1680 dummy = in_be64(&priv2->puint_mb_R);
1682 spu_int_stat_clear(spu, 2, CLASS2_ENABLE_MAILBOX_INTR);
1687 static inline void restore_mfc_sr1(struct spu_state *csa, struct spu *spu)
1689 /* Restore, Step 69:
1690 * Restore the MFC_SR1 register from CSA.
1692 spu_mfc_sr1_set(spu, csa->priv1.mfc_sr1_RW);
1696 static inline void restore_other_spu_access(struct spu_state *csa,
1699 /* Restore, Step 70:
1700 * Restore other SPU mappings to this SPU. TBD.
1704 static inline void restore_spu_runcntl(struct spu_state *csa, struct spu *spu)
1706 struct spu_problem __iomem *prob = spu->problem;
1708 /* Restore, Step 71:
1709 * If CSA.SPU_Status[R]=1 then write
1710 * SPU_RunCntl[R0R1]='01'.
1712 if (csa->prob.spu_status_R & SPU_STATUS_RUNNING) {
1713 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1718 static inline void restore_mfc_cntl(struct spu_state *csa, struct spu *spu)
1720 struct spu_priv2 __iomem *priv2 = spu->priv2;
1722 /* Restore, Step 72:
1723 * Restore the MFC_CNTL register for the CSA.
1725 out_be64(&priv2->mfc_control_RW, csa->priv2.mfc_control_RW);
1728 * FIXME: this is to restart a DMA that we were processing
1729 * before the save. better remember the fault information
1730 * in the csa instead.
1732 if ((csa->priv2.mfc_control_RW & MFC_CNTL_SUSPEND_DMA_QUEUE_MASK)) {
1733 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
1738 static inline void enable_user_access(struct spu_state *csa, struct spu *spu)
1740 /* Restore, Step 73:
1741 * Enable user-space access (if provided) to this
1742 * SPU by mapping the virtual pages assigned to
1743 * the SPU memory-mapped I/O (MMIO) for problem
1748 static inline void reset_switch_active(struct spu_state *csa, struct spu *spu)
1750 /* Restore, Step 74:
1751 * Reset the "context switch active" flag.
1752 * Not performed by this implementation.
1756 static inline void reenable_interrupts(struct spu_state *csa, struct spu *spu)
1758 /* Restore, Step 75:
1759 * Re-enable SPU interrupts.
1761 spin_lock_irq(&spu->register_lock);
1762 spu_int_mask_set(spu, 0, csa->priv1.int_mask_class0_RW);
1763 spu_int_mask_set(spu, 1, csa->priv1.int_mask_class1_RW);
1764 spu_int_mask_set(spu, 2, csa->priv1.int_mask_class2_RW);
1765 spin_unlock_irq(&spu->register_lock);
1768 static int quiece_spu(struct spu_state *prev, struct spu *spu)
1771 * Combined steps 2-18 of SPU context save sequence, which
1772 * quiesce the SPU state (disable SPU execution, MFC command
1773 * queues, decrementer, SPU interrupts, etc.).
1775 * Returns 0 on success.
1776 * 2 if failed step 2.
1777 * 6 if failed step 6.
1780 if (check_spu_isolate(prev, spu)) { /* Step 2. */
1783 disable_interrupts(prev, spu); /* Step 3. */
1784 set_watchdog_timer(prev, spu); /* Step 4. */
1785 inhibit_user_access(prev, spu); /* Step 5. */
1786 if (check_spu_isolate(prev, spu)) { /* Step 6. */
1789 set_switch_pending(prev, spu); /* Step 7. */
1790 save_mfc_cntl(prev, spu); /* Step 8. */
1791 save_spu_runcntl(prev, spu); /* Step 9. */
1792 save_mfc_sr1(prev, spu); /* Step 10. */
1793 save_spu_status(prev, spu); /* Step 11. */
1794 save_mfc_decr(prev, spu); /* Step 12. */
1795 halt_mfc_decr(prev, spu); /* Step 13. */
1796 save_timebase(prev, spu); /* Step 14. */
1797 remove_other_spu_access(prev, spu); /* Step 15. */
1798 do_mfc_mssync(prev, spu); /* Step 16. */
1799 issue_mfc_tlbie(prev, spu); /* Step 17. */
1800 handle_pending_interrupts(prev, spu); /* Step 18. */
1805 static void save_csa(struct spu_state *prev, struct spu *spu)
1808 * Combine steps 19-44 of SPU context save sequence, which
1809 * save regions of the privileged & problem state areas.
1812 save_mfc_queues(prev, spu); /* Step 19. */
1813 save_ppu_querymask(prev, spu); /* Step 20. */
1814 save_ppu_querytype(prev, spu); /* Step 21. */
1815 save_ppu_tagstatus(prev, spu); /* NEW. */
1816 save_mfc_csr_tsq(prev, spu); /* Step 22. */
1817 save_mfc_csr_cmd(prev, spu); /* Step 23. */
1818 save_mfc_csr_ato(prev, spu); /* Step 24. */
1819 save_mfc_tclass_id(prev, spu); /* Step 25. */
1820 set_mfc_tclass_id(prev, spu); /* Step 26. */
1821 save_mfc_cmd(prev, spu); /* Step 26a - moved from 44. */
1822 purge_mfc_queue(prev, spu); /* Step 27. */
1823 wait_purge_complete(prev, spu); /* Step 28. */
1824 setup_mfc_sr1(prev, spu); /* Step 30. */
1825 save_spu_npc(prev, spu); /* Step 31. */
1826 save_spu_privcntl(prev, spu); /* Step 32. */
1827 reset_spu_privcntl(prev, spu); /* Step 33. */
1828 save_spu_lslr(prev, spu); /* Step 34. */
1829 reset_spu_lslr(prev, spu); /* Step 35. */
1830 save_spu_cfg(prev, spu); /* Step 36. */
1831 save_pm_trace(prev, spu); /* Step 37. */
1832 save_mfc_rag(prev, spu); /* Step 38. */
1833 save_ppu_mb_stat(prev, spu); /* Step 39. */
1834 save_ppu_mb(prev, spu); /* Step 40. */
1835 save_ppuint_mb(prev, spu); /* Step 41. */
1836 save_ch_part1(prev, spu); /* Step 42. */
1837 save_spu_mb(prev, spu); /* Step 43. */
1838 reset_ch(prev, spu); /* Step 45. */
1841 static void save_lscsa(struct spu_state *prev, struct spu *spu)
1844 * Perform steps 46-57 of SPU context save sequence,
1845 * which save regions of the local store and register
1849 resume_mfc_queue(prev, spu); /* Step 46. */
1851 setup_mfc_slbs(prev, spu, spu_save_code, sizeof(spu_save_code));
1852 set_switch_active(prev, spu); /* Step 48. */
1853 enable_interrupts(prev, spu); /* Step 49. */
1854 save_ls_16kb(prev, spu); /* Step 50. */
1855 set_spu_npc(prev, spu); /* Step 51. */
1856 set_signot1(prev, spu); /* Step 52. */
1857 set_signot2(prev, spu); /* Step 53. */
1858 send_save_code(prev, spu); /* Step 54. */
1859 set_ppu_querymask(prev, spu); /* Step 55. */
1860 wait_tag_complete(prev, spu); /* Step 56. */
1861 wait_spu_stopped(prev, spu); /* Step 57. */
1864 static void force_spu_isolate_exit(struct spu *spu)
1866 struct spu_problem __iomem *prob = spu->problem;
1867 struct spu_priv2 __iomem *priv2 = spu->priv2;
1869 /* Stop SPE execution and wait for completion. */
1870 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1872 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING);
1874 /* Restart SPE master runcntl. */
1875 spu_mfc_sr1_set(spu, MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1878 /* Initiate isolate exit request and wait for completion. */
1879 out_be64(&priv2->spu_privcntl_RW, 4LL);
1881 out_be32(&prob->spu_runcntl_RW, 2);
1883 POLL_WHILE_FALSE((in_be32(&prob->spu_status_R)
1884 & SPU_STATUS_STOPPED_BY_STOP));
1886 /* Reset load request to normal. */
1887 out_be64(&priv2->spu_privcntl_RW, SPU_PRIVCNT_LOAD_REQUEST_NORMAL);
1893 * Check SPU run-control state and force isolated
1894 * exit function as necessary.
1896 static void stop_spu_isolate(struct spu *spu)
1898 struct spu_problem __iomem *prob = spu->problem;
1900 if (in_be32(&prob->spu_status_R) & SPU_STATUS_ISOLATED_STATE) {
1901 /* The SPU is in isolated state; the only way
1902 * to get it out is to perform an isolated
1903 * exit (clean) operation.
1905 force_spu_isolate_exit(spu);
1909 static void harvest(struct spu_state *prev, struct spu *spu)
1912 * Perform steps 2-25 of SPU context restore sequence,
1913 * which resets an SPU either after a failed save, or
1914 * when using SPU for first time.
1917 disable_interrupts(prev, spu); /* Step 2. */
1918 inhibit_user_access(prev, spu); /* Step 3. */
1919 terminate_spu_app(prev, spu); /* Step 4. */
1920 set_switch_pending(prev, spu); /* Step 5. */
1921 stop_spu_isolate(spu); /* NEW. */
1922 remove_other_spu_access(prev, spu); /* Step 6. */
1923 suspend_mfc_and_halt_decr(prev, spu); /* Step 7. */
1924 wait_suspend_mfc_complete(prev, spu); /* Step 8. */
1925 if (!suspend_spe(prev, spu)) /* Step 9. */
1926 clear_spu_status(prev, spu); /* Step 10. */
1927 do_mfc_mssync(prev, spu); /* Step 11. */
1928 issue_mfc_tlbie(prev, spu); /* Step 12. */
1929 handle_pending_interrupts(prev, spu); /* Step 13. */
1930 purge_mfc_queue(prev, spu); /* Step 14. */
1931 wait_purge_complete(prev, spu); /* Step 15. */
1932 reset_spu_privcntl(prev, spu); /* Step 16. */
1933 reset_spu_lslr(prev, spu); /* Step 17. */
1934 setup_mfc_sr1(prev, spu); /* Step 18. */
1935 spu_invalidate_slbs(spu); /* Step 19. */
1936 reset_ch_part1(prev, spu); /* Step 20. */
1937 reset_ch_part2(prev, spu); /* Step 21. */
1938 enable_interrupts(prev, spu); /* Step 22. */
1939 set_switch_active(prev, spu); /* Step 23. */
1940 set_mfc_tclass_id(prev, spu); /* Step 24. */
1941 resume_mfc_queue(prev, spu); /* Step 25. */
1944 static void restore_lscsa(struct spu_state *next, struct spu *spu)
1947 * Perform steps 26-40 of SPU context restore sequence,
1948 * which restores regions of the local store and register
1952 set_watchdog_timer(next, spu); /* Step 26. */
1953 setup_spu_status_part1(next, spu); /* Step 27. */
1954 setup_spu_status_part2(next, spu); /* Step 28. */
1955 restore_mfc_rag(next, spu); /* Step 29. */
1957 setup_mfc_slbs(next, spu, spu_restore_code, sizeof(spu_restore_code));
1958 set_spu_npc(next, spu); /* Step 31. */
1959 set_signot1(next, spu); /* Step 32. */
1960 set_signot2(next, spu); /* Step 33. */
1961 setup_decr(next, spu); /* Step 34. */
1962 setup_ppu_mb(next, spu); /* Step 35. */
1963 setup_ppuint_mb(next, spu); /* Step 36. */
1964 send_restore_code(next, spu); /* Step 37. */
1965 set_ppu_querymask(next, spu); /* Step 38. */
1966 wait_tag_complete(next, spu); /* Step 39. */
1967 wait_spu_stopped(next, spu); /* Step 40. */
1970 static void restore_csa(struct spu_state *next, struct spu *spu)
1973 * Combine steps 41-76 of SPU context restore sequence, which
1974 * restore regions of the privileged & problem state areas.
1977 restore_spu_privcntl(next, spu); /* Step 41. */
1978 restore_status_part1(next, spu); /* Step 42. */
1979 restore_status_part2(next, spu); /* Step 43. */
1980 restore_ls_16kb(next, spu); /* Step 44. */
1981 wait_tag_complete(next, spu); /* Step 45. */
1982 suspend_mfc(next, spu); /* Step 46. */
1983 wait_suspend_mfc_complete(next, spu); /* Step 47. */
1984 issue_mfc_tlbie(next, spu); /* Step 48. */
1985 clear_interrupts(next, spu); /* Step 49. */
1986 restore_mfc_queues(next, spu); /* Step 50. */
1987 restore_ppu_querymask(next, spu); /* Step 51. */
1988 restore_ppu_querytype(next, spu); /* Step 52. */
1989 restore_mfc_csr_tsq(next, spu); /* Step 53. */
1990 restore_mfc_csr_cmd(next, spu); /* Step 54. */
1991 restore_mfc_csr_ato(next, spu); /* Step 55. */
1992 restore_mfc_tclass_id(next, spu); /* Step 56. */
1993 set_llr_event(next, spu); /* Step 57. */
1994 restore_decr_wrapped(next, spu); /* Step 58. */
1995 restore_ch_part1(next, spu); /* Step 59. */
1996 restore_ch_part2(next, spu); /* Step 60. */
1997 restore_spu_lslr(next, spu); /* Step 61. */
1998 restore_spu_cfg(next, spu); /* Step 62. */
1999 restore_pm_trace(next, spu); /* Step 63. */
2000 restore_spu_npc(next, spu); /* Step 64. */
2001 restore_spu_mb(next, spu); /* Step 65. */
2002 check_ppu_mb_stat(next, spu); /* Step 66. */
2003 check_ppuint_mb_stat(next, spu); /* Step 67. */
2004 spu_invalidate_slbs(spu); /* Modified Step 68. */
2005 restore_mfc_sr1(next, spu); /* Step 69. */
2006 restore_other_spu_access(next, spu); /* Step 70. */
2007 restore_spu_runcntl(next, spu); /* Step 71. */
2008 restore_mfc_cntl(next, spu); /* Step 72. */
2009 enable_user_access(next, spu); /* Step 73. */
2010 reset_switch_active(next, spu); /* Step 74. */
2011 reenable_interrupts(next, spu); /* Step 75. */
2014 static int __do_spu_save(struct spu_state *prev, struct spu *spu)
2019 * SPU context save can be broken into three phases:
2021 * (a) quiesce [steps 2-16].
2022 * (b) save of CSA, performed by PPE [steps 17-42]
2023 * (c) save of LSCSA, mostly performed by SPU [steps 43-52].
2025 * Returns 0 on success.
2026 * 2,6 if failed to quiece SPU
2027 * 53 if SPU-side of save failed.
2030 rc = quiece_spu(prev, spu); /* Steps 2-16. */
2041 save_csa(prev, spu); /* Steps 17-43. */
2042 save_lscsa(prev, spu); /* Steps 44-53. */
2043 return check_save_status(prev, spu); /* Step 54. */
2046 static int __do_spu_restore(struct spu_state *next, struct spu *spu)
2051 * SPU context restore can be broken into three phases:
2053 * (a) harvest (or reset) SPU [steps 2-24].
2054 * (b) restore LSCSA [steps 25-40], mostly performed by SPU.
2055 * (c) restore CSA [steps 41-76], performed by PPE.
2057 * The 'harvest' step is not performed here, but rather
2061 restore_lscsa(next, spu); /* Steps 24-39. */
2062 rc = check_restore_status(next, spu); /* Step 40. */
2065 /* Failed. Return now. */
2069 /* Fall through to next step. */
2072 restore_csa(next, spu);
2078 * spu_save - SPU context save, with locking.
2079 * @prev: pointer to SPU context save area, to be saved.
2080 * @spu: pointer to SPU iomem structure.
2082 * Acquire locks, perform the save operation then return.
2084 int spu_save(struct spu_state *prev, struct spu *spu)
2088 acquire_spu_lock(spu); /* Step 1. */
2089 rc = __do_spu_save(prev, spu); /* Steps 2-53. */
2090 release_spu_lock(spu);
2091 if (rc != 0 && rc != 2 && rc != 6) {
2092 panic("%s failed on SPU[%d], rc=%d.\n",
2093 __func__, spu->number, rc);
2097 EXPORT_SYMBOL_GPL(spu_save);
2100 * spu_restore - SPU context restore, with harvest and locking.
2101 * @new: pointer to SPU context save area, to be restored.
2102 * @spu: pointer to SPU iomem structure.
2104 * Perform harvest + restore, as we may not be coming
2105 * from a previous successful save operation, and the
2106 * hardware state is unknown.
2108 int spu_restore(struct spu_state *new, struct spu *spu)
2112 acquire_spu_lock(spu);
2114 spu->slb_replace = 0;
2115 rc = __do_spu_restore(new, spu);
2116 release_spu_lock(spu);
2118 panic("%s failed on SPU[%d] rc=%d.\n",
2119 __func__, spu->number, rc);
2123 EXPORT_SYMBOL_GPL(spu_restore);
2125 static void init_prob(struct spu_state *csa)
2127 csa->spu_chnlcnt_RW[9] = 1;
2128 csa->spu_chnlcnt_RW[21] = 16;
2129 csa->spu_chnlcnt_RW[23] = 1;
2130 csa->spu_chnlcnt_RW[28] = 1;
2131 csa->spu_chnlcnt_RW[30] = 1;
2132 csa->prob.spu_runcntl_RW = SPU_RUNCNTL_STOP;
2133 csa->prob.mb_stat_R = 0x000400;
2136 static void init_priv1(struct spu_state *csa)
2138 /* Enable decode, relocate, tlbie response, master runcntl. */
2139 csa->priv1.mfc_sr1_RW = MFC_STATE1_LOCAL_STORAGE_DECODE_MASK |
2140 MFC_STATE1_MASTER_RUN_CONTROL_MASK |
2141 MFC_STATE1_PROBLEM_STATE_MASK |
2142 MFC_STATE1_RELOCATE_MASK | MFC_STATE1_BUS_TLBIE_MASK;
2144 /* Enable OS-specific set of interrupts. */
2145 csa->priv1.int_mask_class0_RW = CLASS0_ENABLE_DMA_ALIGNMENT_INTR |
2146 CLASS0_ENABLE_INVALID_DMA_COMMAND_INTR |
2147 CLASS0_ENABLE_SPU_ERROR_INTR;
2148 csa->priv1.int_mask_class1_RW = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
2149 CLASS1_ENABLE_STORAGE_FAULT_INTR;
2150 csa->priv1.int_mask_class2_RW = CLASS2_ENABLE_SPU_STOP_INTR |
2151 CLASS2_ENABLE_SPU_HALT_INTR |
2152 CLASS2_ENABLE_SPU_DMA_TAG_GROUP_COMPLETE_INTR;
2155 static void init_priv2(struct spu_state *csa)
2157 csa->priv2.spu_lslr_RW = LS_ADDR_MASK;
2158 csa->priv2.mfc_control_RW = MFC_CNTL_RESUME_DMA_QUEUE |
2159 MFC_CNTL_NORMAL_DMA_QUEUE_OPERATION |
2160 MFC_CNTL_DMA_QUEUES_EMPTY_MASK;
2164 * spu_alloc_csa - allocate and initialize an SPU context save area.
2166 * Allocate and initialize the contents of an SPU context save area.
2167 * This includes enabling address translation, interrupt masks, etc.,
2168 * as appropriate for the given OS environment.
2170 * Note that storage for the 'lscsa' is allocated separately,
2171 * as it is by far the largest of the context save regions,
2172 * and may need to be pinned or otherwise specially aligned.
2174 int spu_init_csa(struct spu_state *csa)
2180 memset(csa, 0, sizeof(struct spu_state));
2182 rc = spu_alloc_lscsa(csa);
2186 spin_lock_init(&csa->register_lock);
2195 void spu_fini_csa(struct spu_state *csa)
2197 spu_free_lscsa(csa);