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/config.h>
36 #include <linux/module.h>
37 #include <linux/errno.h>
38 #include <linux/sched.h>
39 #include <linux/kernel.h>
41 #include <linux/vmalloc.h>
42 #include <linux/smp.h>
43 #include <linux/smp_lock.h>
44 #include <linux/stddef.h>
45 #include <linux/unistd.h>
49 #include <asm/spu_csa.h>
50 #include <asm/mmu_context.h>
52 #include "spu_save_dump.h"
53 #include "spu_restore_dump.h"
56 #define POLL_WHILE_TRUE(_c) { \
61 #define RELAX_SPIN_COUNT 1000
62 #define POLL_WHILE_TRUE(_c) { \
65 for (_i=0; _i<RELAX_SPIN_COUNT && (_c); _i++) { \
68 if (unlikely(_c)) yield(); \
74 #define POLL_WHILE_FALSE(_c) POLL_WHILE_TRUE(!(_c))
76 static inline void acquire_spu_lock(struct spu *spu)
80 * Acquire SPU-specific mutual exclusion lock.
85 static inline void release_spu_lock(struct spu *spu)
88 * Release SPU-specific mutual exclusion lock.
93 static inline int check_spu_isolate(struct spu_state *csa, struct spu *spu)
95 struct spu_problem __iomem *prob = spu->problem;
100 * If SPU_Status[E,L,IS] any field is '1', this
101 * SPU is in isolate state and cannot be context
102 * saved at this time.
104 isolate_state = SPU_STATUS_ISOLATED_STATE |
105 SPU_STATUS_ISOLATED_LOAD_STAUTUS | SPU_STATUS_ISOLATED_EXIT_STAUTUS;
106 return (in_be32(&prob->spu_status_R) & isolate_state) ? 1 : 0;
109 static inline void disable_interrupts(struct spu_state *csa, struct spu *spu)
111 struct spu_priv1 __iomem *priv1 = spu->priv1;
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.
122 spin_lock_irq(&spu->register_lock);
124 csa->priv1.int_mask_class0_RW =
125 in_be64(&priv1->int_mask_class0_RW);
126 csa->priv1.int_mask_class1_RW =
127 in_be64(&priv1->int_mask_class1_RW);
128 csa->priv1.int_mask_class2_RW =
129 in_be64(&priv1->int_mask_class2_RW);
131 out_be64(&priv1->int_mask_class0_RW, 0UL);
132 out_be64(&priv1->int_mask_class1_RW, 0UL);
133 out_be64(&priv1->int_mask_class2_RW, 0UL);
135 spin_unlock_irq(&spu->register_lock);
138 static inline void set_watchdog_timer(struct spu_state *csa, struct spu *spu)
142 * Set a software watchdog timer, which specifies the
143 * maximum allowable time for a context save sequence.
145 * For present, this implementation will not set a global
146 * watchdog timer, as virtualization & variable system load
147 * may cause unpredictable execution times.
151 static inline void inhibit_user_access(struct spu_state *csa, struct spu *spu)
155 * Inhibit user-space access (if provided) to this
156 * SPU by unmapping the virtual pages assigned to
157 * the SPU memory-mapped I/O (MMIO) for problem
162 static inline void set_switch_pending(struct spu_state *csa, struct spu *spu)
166 * Set a software context switch pending flag.
168 set_bit(SPU_CONTEXT_SWITCH_PENDING_nr, &spu->flags);
172 static inline void save_mfc_cntl(struct spu_state *csa, struct spu *spu)
174 struct spu_priv2 __iomem *priv2 = spu->priv2;
177 * Read and save MFC_CNTL[Ss].
180 csa->priv2.mfc_control_RW = in_be64(&priv2->mfc_control_RW) &
181 MFC_CNTL_SUSPEND_DMA_STATUS_MASK;
185 static inline void save_spu_runcntl(struct spu_state *csa, struct spu *spu)
187 struct spu_problem __iomem *prob = spu->problem;
190 * Save SPU_Runcntl in the CSA. This value contains
191 * the "Application Desired State".
193 csa->prob.spu_runcntl_RW = in_be32(&prob->spu_runcntl_RW);
196 static inline void save_mfc_sr1(struct spu_state *csa, struct spu *spu)
198 struct spu_priv1 __iomem *priv1 = spu->priv1;
201 * Save MFC_SR1 in the CSA.
203 csa->priv1.mfc_sr1_RW = in_be64(&priv1->mfc_sr1_RW);
206 static inline void save_spu_status(struct spu_state *csa, struct spu *spu)
208 struct spu_problem __iomem *prob = spu->problem;
211 * Read SPU_Status[R], and save to CSA.
213 if ((in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) == 0) {
214 csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
218 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
220 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
223 SPU_STATUS_INVALID_INSTR | SPU_STATUS_SINGLE_STEP |
224 SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
225 if ((in_be32(&prob->spu_status_R) & stopped) == 0)
226 csa->prob.spu_status_R = SPU_STATUS_RUNNING;
228 csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
232 static inline void save_mfc_decr(struct spu_state *csa, struct spu *spu)
234 struct spu_priv2 __iomem *priv2 = spu->priv2;
237 * Read MFC_CNTL[Ds]. Update saved copy of
240 if (in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DECREMENTER_RUNNING) {
241 csa->priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
242 csa->suspend_time = get_cycles();
243 out_be64(&priv2->spu_chnlcntptr_RW, 7ULL);
245 csa->spu_chnldata_RW[7] = in_be64(&priv2->spu_chnldata_RW);
250 static inline void halt_mfc_decr(struct spu_state *csa, struct spu *spu)
252 struct spu_priv2 __iomem *priv2 = spu->priv2;
255 * Write MFC_CNTL[Dh] set to a '1' to halt
258 out_be64(&priv2->mfc_control_RW, MFC_CNTL_DECREMENTER_HALTED);
262 static inline void save_timebase(struct spu_state *csa, struct spu *spu)
265 * Read PPE Timebase High and Timebase low registers
266 * and save in CSA. TBD.
268 csa->suspend_time = get_cycles();
271 static inline void remove_other_spu_access(struct spu_state *csa,
275 * Remove other SPU access to this SPU by unmapping
276 * this SPU's pages from their address space. TBD.
280 static inline void do_mfc_mssync(struct spu_state *csa, struct spu *spu)
282 struct spu_problem __iomem *prob = spu->problem;
286 * Write SPU_MSSync register. Poll SPU_MSSync[P]
289 out_be64(&prob->spc_mssync_RW, 1UL);
290 POLL_WHILE_TRUE(in_be64(&prob->spc_mssync_RW) & MS_SYNC_PENDING);
293 static inline void issue_mfc_tlbie(struct spu_state *csa, struct spu *spu)
295 struct spu_priv1 __iomem *priv1 = spu->priv1;
300 * Write TLB_Invalidate_Entry[IS,VPN,L,Lp]=0 register.
301 * Then issue a PPE sync instruction.
303 out_be64(&priv1->tlb_invalidate_entry_W, 0UL);
307 static inline void handle_pending_interrupts(struct spu_state *csa,
311 * Handle any pending interrupts from this SPU
312 * here. This is OS or hypervisor specific. One
313 * option is to re-enable interrupts to handle any
314 * pending interrupts, with the interrupt handlers
315 * recognizing the software Context Switch Pending
316 * flag, to ensure the SPU execution or MFC command
317 * queue is not restarted. TBD.
321 static inline void save_mfc_queues(struct spu_state *csa, struct spu *spu)
323 struct spu_priv2 __iomem *priv2 = spu->priv2;
327 * If MFC_Cntl[Se]=0 then save
328 * MFC command queues.
330 if ((in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DMA_QUEUES_EMPTY) == 0) {
331 for (i = 0; i < 8; i++) {
332 csa->priv2.puq[i].mfc_cq_data0_RW =
333 in_be64(&priv2->puq[i].mfc_cq_data0_RW);
334 csa->priv2.puq[i].mfc_cq_data1_RW =
335 in_be64(&priv2->puq[i].mfc_cq_data1_RW);
336 csa->priv2.puq[i].mfc_cq_data2_RW =
337 in_be64(&priv2->puq[i].mfc_cq_data2_RW);
338 csa->priv2.puq[i].mfc_cq_data3_RW =
339 in_be64(&priv2->puq[i].mfc_cq_data3_RW);
341 for (i = 0; i < 16; i++) {
342 csa->priv2.spuq[i].mfc_cq_data0_RW =
343 in_be64(&priv2->spuq[i].mfc_cq_data0_RW);
344 csa->priv2.spuq[i].mfc_cq_data1_RW =
345 in_be64(&priv2->spuq[i].mfc_cq_data1_RW);
346 csa->priv2.spuq[i].mfc_cq_data2_RW =
347 in_be64(&priv2->spuq[i].mfc_cq_data2_RW);
348 csa->priv2.spuq[i].mfc_cq_data3_RW =
349 in_be64(&priv2->spuq[i].mfc_cq_data3_RW);
354 static inline void save_ppu_querymask(struct spu_state *csa, struct spu *spu)
356 struct spu_problem __iomem *prob = spu->problem;
359 * Save the PPU_QueryMask register
362 csa->prob.dma_querymask_RW = in_be32(&prob->dma_querymask_RW);
365 static inline void save_ppu_querytype(struct spu_state *csa, struct spu *spu)
367 struct spu_problem __iomem *prob = spu->problem;
370 * Save the PPU_QueryType register
373 csa->prob.dma_querytype_RW = in_be32(&prob->dma_querytype_RW);
376 static inline void save_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
378 struct spu_priv2 __iomem *priv2 = spu->priv2;
381 * Save the MFC_CSR_TSQ register
384 csa->priv2.spu_tag_status_query_RW =
385 in_be64(&priv2->spu_tag_status_query_RW);
388 static inline void save_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
390 struct spu_priv2 __iomem *priv2 = spu->priv2;
393 * Save the MFC_CSR_CMD1 and MFC_CSR_CMD2
394 * registers in the CSA.
396 csa->priv2.spu_cmd_buf1_RW = in_be64(&priv2->spu_cmd_buf1_RW);
397 csa->priv2.spu_cmd_buf2_RW = in_be64(&priv2->spu_cmd_buf2_RW);
400 static inline void save_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
402 struct spu_priv2 __iomem *priv2 = spu->priv2;
405 * Save the MFC_CSR_ATO register in
408 csa->priv2.spu_atomic_status_RW = in_be64(&priv2->spu_atomic_status_RW);
411 static inline void save_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
413 struct spu_priv1 __iomem *priv1 = spu->priv1;
416 * Save the MFC_TCLASS_ID register in
419 csa->priv1.mfc_tclass_id_RW = in_be64(&priv1->mfc_tclass_id_RW);
422 static inline void set_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
424 struct spu_priv1 __iomem *priv1 = spu->priv1;
428 * Write the MFC_TCLASS_ID register with
429 * the value 0x10000000.
431 out_be64(&priv1->mfc_tclass_id_RW, 0x10000000);
435 static inline void purge_mfc_queue(struct spu_state *csa, struct spu *spu)
437 struct spu_priv2 __iomem *priv2 = spu->priv2;
441 * Write MFC_CNTL[Pc]=1 (purge queue).
443 out_be64(&priv2->mfc_control_RW, MFC_CNTL_PURGE_DMA_REQUEST);
447 static inline void wait_purge_complete(struct spu_state *csa, struct spu *spu)
449 struct spu_priv2 __iomem *priv2 = spu->priv2;
452 * Poll MFC_CNTL[Ps] until value '11' is read
455 POLL_WHILE_FALSE(in_be64(&priv2->mfc_control_RW) &
456 MFC_CNTL_PURGE_DMA_COMPLETE);
459 static inline void save_mfc_slbs(struct spu_state *csa, struct spu *spu)
461 struct spu_priv1 __iomem *priv1 = spu->priv1;
462 struct spu_priv2 __iomem *priv2 = spu->priv2;
466 * If MFC_SR1[R]='1', save SLBs in CSA.
468 if (in_be64(&priv1->mfc_sr1_RW) & MFC_STATE1_RELOCATE_MASK) {
469 csa->priv2.slb_index_W = in_be64(&priv2->slb_index_W);
470 for (i = 0; i < 8; i++) {
471 out_be64(&priv2->slb_index_W, i);
473 csa->slb_esid_RW[i] = in_be64(&priv2->slb_esid_RW);
474 csa->slb_vsid_RW[i] = in_be64(&priv2->slb_vsid_RW);
480 static inline void setup_mfc_sr1(struct spu_state *csa, struct spu *spu)
482 struct spu_priv1 __iomem *priv1 = spu->priv1;
486 * Write MFC_SR1 with MFC_SR1[D=0,S=1] and
487 * MFC_SR1[TL,R,Pr,T] set correctly for the
488 * OS specific environment.
490 * Implementation note: The SPU-side code
491 * for save/restore is privileged, so the
492 * MFC_SR1[Pr] bit is not set.
495 out_be64(&priv1->mfc_sr1_RW, (MFC_STATE1_MASTER_RUN_CONTROL_MASK |
496 MFC_STATE1_RELOCATE_MASK |
497 MFC_STATE1_BUS_TLBIE_MASK));
500 static inline void save_spu_npc(struct spu_state *csa, struct spu *spu)
502 struct spu_problem __iomem *prob = spu->problem;
505 * Save SPU_NPC in the CSA.
507 csa->prob.spu_npc_RW = in_be32(&prob->spu_npc_RW);
510 static inline void save_spu_privcntl(struct spu_state *csa, struct spu *spu)
512 struct spu_priv2 __iomem *priv2 = spu->priv2;
515 * Save SPU_PrivCntl in the CSA.
517 csa->priv2.spu_privcntl_RW = in_be64(&priv2->spu_privcntl_RW);
520 static inline void reset_spu_privcntl(struct spu_state *csa, struct spu *spu)
522 struct spu_priv2 __iomem *priv2 = spu->priv2;
526 * Write SPU_PrivCntl[S,Le,A] fields reset to 0.
528 out_be64(&priv2->spu_privcntl_RW, 0UL);
532 static inline void save_spu_lslr(struct spu_state *csa, struct spu *spu)
534 struct spu_priv2 __iomem *priv2 = spu->priv2;
537 * Save SPU_LSLR in the CSA.
539 csa->priv2.spu_lslr_RW = in_be64(&priv2->spu_lslr_RW);
542 static inline void reset_spu_lslr(struct spu_state *csa, struct spu *spu)
544 struct spu_priv2 __iomem *priv2 = spu->priv2;
550 out_be64(&priv2->spu_lslr_RW, LS_ADDR_MASK);
554 static inline void save_spu_cfg(struct spu_state *csa, struct spu *spu)
556 struct spu_priv2 __iomem *priv2 = spu->priv2;
559 * Save SPU_Cfg in the CSA.
561 csa->priv2.spu_cfg_RW = in_be64(&priv2->spu_cfg_RW);
564 static inline void save_pm_trace(struct spu_state *csa, struct spu *spu)
567 * Save PM_Trace_Tag_Wait_Mask in the CSA.
568 * Not performed by this implementation.
572 static inline void save_mfc_rag(struct spu_state *csa, struct spu *spu)
574 struct spu_priv1 __iomem *priv1 = spu->priv1;
577 * Save RA_GROUP_ID register and the
578 * RA_ENABLE reigster in the CSA.
580 csa->priv1.resource_allocation_groupID_RW =
581 in_be64(&priv1->resource_allocation_groupID_RW);
582 csa->priv1.resource_allocation_enable_RW =
583 in_be64(&priv1->resource_allocation_enable_RW);
586 static inline void save_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
588 struct spu_problem __iomem *prob = spu->problem;
591 * Save MB_Stat register in the CSA.
593 csa->prob.mb_stat_R = in_be32(&prob->mb_stat_R);
596 static inline void save_ppu_mb(struct spu_state *csa, struct spu *spu)
598 struct spu_problem __iomem *prob = spu->problem;
601 * Save the PPU_MB register in the CSA.
603 csa->prob.pu_mb_R = in_be32(&prob->pu_mb_R);
606 static inline void save_ppuint_mb(struct spu_state *csa, struct spu *spu)
608 struct spu_priv2 __iomem *priv2 = spu->priv2;
611 * Save the PPUINT_MB register in the CSA.
613 csa->priv2.puint_mb_R = in_be64(&priv2->puint_mb_R);
616 static inline void save_ch_part1(struct spu_state *csa, struct spu *spu)
618 struct spu_priv2 __iomem *priv2 = spu->priv2;
619 u64 idx, ch_indices[7] = { 0UL, 1UL, 3UL, 4UL, 24UL, 25UL, 27UL };
623 * Save the following CH: [0,1,3,4,24,25,27]
625 for (i = 0; i < 7; i++) {
627 out_be64(&priv2->spu_chnlcntptr_RW, idx);
629 csa->spu_chnldata_RW[idx] = in_be64(&priv2->spu_chnldata_RW);
630 csa->spu_chnlcnt_RW[idx] = in_be64(&priv2->spu_chnlcnt_RW);
631 out_be64(&priv2->spu_chnldata_RW, 0UL);
632 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
637 static inline void save_spu_mb(struct spu_state *csa, struct spu *spu)
639 struct spu_priv2 __iomem *priv2 = spu->priv2;
643 * Save SPU Read Mailbox Channel.
645 out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
647 csa->spu_chnlcnt_RW[29] = in_be64(&priv2->spu_chnlcnt_RW);
648 for (i = 0; i < 4; i++) {
649 csa->spu_mailbox_data[i] = in_be64(&priv2->spu_chnldata_RW);
651 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
655 static inline void save_mfc_cmd(struct spu_state *csa, struct spu *spu)
657 struct spu_priv2 __iomem *priv2 = spu->priv2;
660 * Save MFC_CMD Channel.
662 out_be64(&priv2->spu_chnlcntptr_RW, 21UL);
664 csa->spu_chnlcnt_RW[21] = in_be64(&priv2->spu_chnlcnt_RW);
668 static inline void reset_ch(struct spu_state *csa, struct spu *spu)
670 struct spu_priv2 __iomem *priv2 = spu->priv2;
671 u64 ch_indices[4] = { 21UL, 23UL, 28UL, 30UL };
672 u64 ch_counts[4] = { 16UL, 1UL, 1UL, 1UL };
677 * Reset the following CH: [21, 23, 28, 30]
679 for (i = 0; i < 4; i++) {
681 out_be64(&priv2->spu_chnlcntptr_RW, idx);
683 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
688 static inline void resume_mfc_queue(struct spu_state *csa, struct spu *spu)
690 struct spu_priv2 __iomem *priv2 = spu->priv2;
694 * Write MFC_CNTL[Sc]=0 (resume queue processing).
696 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESUME_DMA_QUEUE);
699 static inline void invalidate_slbs(struct spu_state *csa, struct spu *spu)
701 struct spu_priv1 __iomem *priv1 = spu->priv1;
702 struct spu_priv2 __iomem *priv2 = spu->priv2;
706 * If MFC_SR1[R]=1, write 0 to SLB_Invalidate_All.
708 if (in_be64(&priv1->mfc_sr1_RW) & MFC_STATE1_RELOCATE_MASK) {
709 out_be64(&priv2->slb_invalidate_all_W, 0UL);
714 static inline void get_kernel_slb(u64 ea, u64 slb[2])
716 slb[0] = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
717 slb[1] = (ea & ESID_MASK) | SLB_ESID_V;
719 /* Large pages are used for kernel text/data, but not vmalloc. */
720 if (cpu_has_feature(CPU_FTR_16M_PAGE)
721 && REGION_ID(ea) == KERNEL_REGION_ID)
722 slb[0] |= SLB_VSID_L;
725 static inline void load_mfc_slb(struct spu *spu, u64 slb[2], int slbe)
727 struct spu_priv2 __iomem *priv2 = spu->priv2;
729 out_be64(&priv2->slb_index_W, slbe);
731 out_be64(&priv2->slb_vsid_RW, slb[0]);
732 out_be64(&priv2->slb_esid_RW, slb[1]);
736 static inline void setup_mfc_slbs(struct spu_state *csa, struct spu *spu)
743 * If MFC_SR1[R]=1, write 0 to SLB_Invalidate_All
744 * register, then initialize SLB_VSID and SLB_ESID
745 * to provide access to SPU context save code and
748 * This implementation places both the context
749 * switch code and LSCSA in kernel address space.
751 * Further this implementation assumes that the
752 * MFC_SR1[R]=1 (in other words, assume that
753 * translation is desired by OS environment).
755 invalidate_slbs(csa, spu);
756 get_kernel_slb((unsigned long)&spu_save_code[0], code_slb);
757 get_kernel_slb((unsigned long)csa->lscsa, lscsa_slb);
758 load_mfc_slb(spu, code_slb, 0);
759 if ((lscsa_slb[0] != code_slb[0]) || (lscsa_slb[1] != code_slb[1]))
760 load_mfc_slb(spu, lscsa_slb, 1);
763 static inline void set_switch_active(struct spu_state *csa, struct spu *spu)
767 * Change the software context switch pending flag
768 * to context switch active.
770 set_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags);
771 clear_bit(SPU_CONTEXT_SWITCH_PENDING_nr, &spu->flags);
775 static inline void enable_interrupts(struct spu_state *csa, struct spu *spu)
777 struct spu_priv1 __iomem *priv1 = spu->priv1;
778 unsigned long class1_mask = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
779 CLASS1_ENABLE_STORAGE_FAULT_INTR;
783 * Reset and then enable interrupts, as
786 * This implementation enables only class1
787 * (translation) interrupts.
789 spin_lock_irq(&spu->register_lock);
790 out_be64(&priv1->int_stat_class0_RW, ~(0UL));
791 out_be64(&priv1->int_stat_class1_RW, ~(0UL));
792 out_be64(&priv1->int_stat_class2_RW, ~(0UL));
793 out_be64(&priv1->int_mask_class0_RW, 0UL);
794 out_be64(&priv1->int_mask_class1_RW, class1_mask);
795 out_be64(&priv1->int_mask_class2_RW, 0UL);
796 spin_unlock_irq(&spu->register_lock);
799 static inline int send_mfc_dma(struct spu *spu, unsigned long ea,
800 unsigned int ls_offset, unsigned int size,
801 unsigned int tag, unsigned int rclass,
804 struct spu_problem __iomem *prob = spu->problem;
805 union mfc_tag_size_class_cmd command;
806 unsigned int transfer_size;
807 volatile unsigned int status = 0x0;
811 (size > MFC_MAX_DMA_SIZE) ? MFC_MAX_DMA_SIZE : size;
812 command.u.mfc_size = transfer_size;
813 command.u.mfc_tag = tag;
814 command.u.mfc_rclassid = rclass;
815 command.u.mfc_cmd = cmd;
817 out_be32(&prob->mfc_lsa_W, ls_offset);
818 out_be64(&prob->mfc_ea_W, ea);
819 out_be64(&prob->mfc_union_W.all64, command.all64);
821 in_be32(&prob->mfc_union_W.by32.mfc_class_cmd32);
822 if (unlikely(status & 0x2)) {
825 } while (status & 0x3);
826 size -= transfer_size;
828 ls_offset += transfer_size;
833 static inline void save_ls_16kb(struct spu_state *csa, struct spu *spu)
835 unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
836 unsigned int ls_offset = 0x0;
837 unsigned int size = 16384;
838 unsigned int tag = 0;
839 unsigned int rclass = 0;
840 unsigned int cmd = MFC_PUT_CMD;
843 * Issue a DMA command to copy the first 16K bytes
844 * of local storage to the CSA.
846 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
849 static inline void set_spu_npc(struct spu_state *csa, struct spu *spu)
851 struct spu_problem __iomem *prob = spu->problem;
855 * Write SPU_NPC[IE]=0 and SPU_NPC[LSA] to entry
856 * point address of context save code in local
859 * This implementation uses SPU-side save/restore
860 * programs with entry points at LSA of 0.
862 out_be32(&prob->spu_npc_RW, 0);
866 static inline void set_signot1(struct spu_state *csa, struct spu *spu)
868 struct spu_problem __iomem *prob = spu->problem;
876 * Write SPU_Sig_Notify_1 register with upper 32-bits
877 * of the CSA.LSCSA effective address.
879 addr64.ull = (u64) csa->lscsa;
880 out_be32(&prob->signal_notify1, addr64.ui[0]);
884 static inline void set_signot2(struct spu_state *csa, struct spu *spu)
886 struct spu_problem __iomem *prob = spu->problem;
894 * Write SPU_Sig_Notify_2 register with lower 32-bits
895 * of the CSA.LSCSA effective address.
897 addr64.ull = (u64) csa->lscsa;
898 out_be32(&prob->signal_notify2, addr64.ui[1]);
902 static inline void send_save_code(struct spu_state *csa, struct spu *spu)
904 unsigned long addr = (unsigned long)&spu_save_code[0];
905 unsigned int ls_offset = 0x0;
906 unsigned int size = sizeof(spu_save_code);
907 unsigned int tag = 0;
908 unsigned int rclass = 0;
909 unsigned int cmd = MFC_GETFS_CMD;
912 * Issue a DMA command to copy context save code
913 * to local storage and start SPU.
915 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
918 static inline void set_ppu_querymask(struct spu_state *csa, struct spu *spu)
920 struct spu_problem __iomem *prob = spu->problem;
924 * Write PPU_QueryMask=1 (enable Tag Group 0)
925 * and issue eieio instruction.
927 out_be32(&prob->dma_querymask_RW, MFC_TAGID_TO_TAGMASK(0));
931 static inline void wait_tag_complete(struct spu_state *csa, struct spu *spu)
933 struct spu_priv1 __iomem *priv1 = spu->priv1;
934 struct spu_problem __iomem *prob = spu->problem;
935 u32 mask = MFC_TAGID_TO_TAGMASK(0);
942 * Poll PPU_TagStatus[gn] until 01 (Tag group 0 complete)
943 * or write PPU_QueryType[TS]=01 and wait for Tag Group
944 * Complete Interrupt. Write INT_Stat_Class0 or
945 * INT_Stat_Class2 with value of 'handled'.
947 POLL_WHILE_FALSE(in_be32(&prob->dma_tagstatus_R) & mask);
949 local_irq_save(flags);
950 out_be64(&priv1->int_stat_class0_RW, ~(0UL));
951 out_be64(&priv1->int_stat_class2_RW, ~(0UL));
952 local_irq_restore(flags);
955 static inline void wait_spu_stopped(struct spu_state *csa, struct spu *spu)
957 struct spu_priv1 __iomem *priv1 = spu->priv1;
958 struct spu_problem __iomem *prob = spu->problem;
963 * Poll until SPU_Status[R]=0 or wait for SPU Class 0
964 * or SPU Class 2 interrupt. Write INT_Stat_class0
965 * or INT_Stat_class2 with value of handled.
967 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING);
969 local_irq_save(flags);
970 out_be64(&priv1->int_stat_class0_RW, ~(0UL));
971 out_be64(&priv1->int_stat_class2_RW, ~(0UL));
972 local_irq_restore(flags);
975 static inline int check_save_status(struct spu_state *csa, struct spu *spu)
977 struct spu_problem __iomem *prob = spu->problem;
981 * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
982 * context save succeeded, otherwise context save
985 complete = ((SPU_SAVE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
986 SPU_STATUS_STOPPED_BY_STOP);
987 return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
990 static inline void terminate_spu_app(struct spu_state *csa, struct spu *spu)
993 * If required, notify the "using application" that
994 * the SPU task has been terminated. TBD.
998 static inline void suspend_mfc(struct spu_state *csa, struct spu *spu)
1000 struct spu_priv2 __iomem *priv2 = spu->priv2;
1004 * Write MFC_Cntl[Dh,Sc]='1','1' to suspend
1005 * the queue and halt the decrementer.
1007 out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE |
1008 MFC_CNTL_DECREMENTER_HALTED);
1012 static inline void wait_suspend_mfc_complete(struct spu_state *csa,
1015 struct spu_priv2 __iomem *priv2 = spu->priv2;
1019 * Poll MFC_CNTL[Ss] until 11 is returned.
1021 POLL_WHILE_FALSE(in_be64(&priv2->mfc_control_RW) &
1022 MFC_CNTL_SUSPEND_COMPLETE);
1025 static inline int suspend_spe(struct spu_state *csa, struct spu *spu)
1027 struct spu_problem __iomem *prob = spu->problem;
1030 * If SPU_Status[R]=1, stop SPU execution
1031 * and wait for stop to complete.
1033 * Returns 1 if SPU_Status[R]=1 on entry.
1036 if (in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) {
1037 if (in_be32(&prob->spu_status_R) &
1038 SPU_STATUS_ISOLATED_EXIT_STAUTUS) {
1039 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1040 SPU_STATUS_RUNNING);
1042 if ((in_be32(&prob->spu_status_R) &
1043 SPU_STATUS_ISOLATED_LOAD_STAUTUS)
1044 || (in_be32(&prob->spu_status_R) &
1045 SPU_STATUS_ISOLATED_STATE)) {
1046 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1048 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1049 SPU_STATUS_RUNNING);
1050 out_be32(&prob->spu_runcntl_RW, 0x2);
1052 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1053 SPU_STATUS_RUNNING);
1055 if (in_be32(&prob->spu_status_R) &
1056 SPU_STATUS_WAITING_FOR_CHANNEL) {
1057 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1059 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1060 SPU_STATUS_RUNNING);
1067 static inline void clear_spu_status(struct spu_state *csa, struct spu *spu)
1069 struct spu_problem __iomem *prob = spu->problem;
1070 struct spu_priv1 __iomem *priv1 = spu->priv1;
1072 /* Restore, Step 10:
1073 * If SPU_Status[R]=0 and SPU_Status[E,L,IS]=1,
1074 * release SPU from isolate state.
1076 if (!(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING)) {
1077 if (in_be32(&prob->spu_status_R) &
1078 SPU_STATUS_ISOLATED_EXIT_STAUTUS) {
1079 out_be64(&priv1->mfc_sr1_RW,
1080 MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1082 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1084 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1085 SPU_STATUS_RUNNING);
1087 if ((in_be32(&prob->spu_status_R) &
1088 SPU_STATUS_ISOLATED_LOAD_STAUTUS)
1089 || (in_be32(&prob->spu_status_R) &
1090 SPU_STATUS_ISOLATED_STATE)) {
1091 out_be64(&priv1->mfc_sr1_RW,
1092 MFC_STATE1_MASTER_RUN_CONTROL_MASK);
1094 out_be32(&prob->spu_runcntl_RW, 0x2);
1096 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1097 SPU_STATUS_RUNNING);
1102 static inline void reset_ch_part1(struct spu_state *csa, struct spu *spu)
1104 struct spu_priv2 __iomem *priv2 = spu->priv2;
1105 u64 ch_indices[7] = { 0UL, 1UL, 3UL, 4UL, 24UL, 25UL, 27UL };
1109 /* Restore, Step 20:
1110 * Reset the following CH: [0,1,3,4,24,25,27]
1112 for (i = 0; i < 7; i++) {
1113 idx = ch_indices[i];
1114 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1116 out_be64(&priv2->spu_chnldata_RW, 0UL);
1117 out_be64(&priv2->spu_chnlcnt_RW, 0UL);
1122 static inline void reset_ch_part2(struct spu_state *csa, struct spu *spu)
1124 struct spu_priv2 __iomem *priv2 = spu->priv2;
1125 u64 ch_indices[5] = { 21UL, 23UL, 28UL, 29UL, 30UL };
1126 u64 ch_counts[5] = { 16UL, 1UL, 1UL, 0UL, 1UL };
1130 /* Restore, Step 21:
1131 * Reset the following CH: [21, 23, 28, 29, 30]
1133 for (i = 0; i < 5; i++) {
1134 idx = ch_indices[i];
1135 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1137 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
1142 static inline void setup_spu_status_part1(struct spu_state *csa,
1145 u32 status_P = SPU_STATUS_STOPPED_BY_STOP;
1146 u32 status_I = SPU_STATUS_INVALID_INSTR;
1147 u32 status_H = SPU_STATUS_STOPPED_BY_HALT;
1148 u32 status_S = SPU_STATUS_SINGLE_STEP;
1149 u32 status_S_I = SPU_STATUS_SINGLE_STEP | SPU_STATUS_INVALID_INSTR;
1150 u32 status_S_P = SPU_STATUS_SINGLE_STEP | SPU_STATUS_STOPPED_BY_STOP;
1151 u32 status_P_H = SPU_STATUS_STOPPED_BY_HALT |SPU_STATUS_STOPPED_BY_STOP;
1152 u32 status_P_I = SPU_STATUS_STOPPED_BY_STOP |SPU_STATUS_INVALID_INSTR;
1155 /* Restore, Step 27:
1156 * If the CSA.SPU_Status[I,S,H,P]=1 then add the correct
1157 * instruction sequence to the end of the SPU based restore
1158 * code (after the "context restored" stop and signal) to
1159 * restore the correct SPU status.
1161 * NOTE: Rather than modifying the SPU executable, we
1162 * instead add a new 'stopped_status' field to the
1163 * LSCSA. The SPU-side restore reads this field and
1164 * takes the appropriate action when exiting.
1168 (csa->prob.spu_status_R >> SPU_STOP_STATUS_SHIFT) & 0xFFFF;
1169 if ((csa->prob.spu_status_R & status_P_I) == status_P_I) {
1171 /* SPU_Status[P,I]=1 - Illegal Instruction followed
1172 * by Stop and Signal instruction, followed by 'br -4'.
1175 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_I;
1176 csa->lscsa->stopped_status.slot[1] = status_code;
1178 } else if ((csa->prob.spu_status_R & status_P_H) == status_P_H) {
1180 /* SPU_Status[P,H]=1 - Halt Conditional, followed
1181 * by Stop and Signal instruction, followed by
1184 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_H;
1185 csa->lscsa->stopped_status.slot[1] = status_code;
1187 } else if ((csa->prob.spu_status_R & status_S_P) == status_S_P) {
1189 /* SPU_Status[S,P]=1 - Stop and Signal instruction
1190 * followed by 'br -4'.
1192 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_P;
1193 csa->lscsa->stopped_status.slot[1] = status_code;
1195 } else if ((csa->prob.spu_status_R & status_S_I) == status_S_I) {
1197 /* SPU_Status[S,I]=1 - Illegal instruction followed
1200 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_I;
1201 csa->lscsa->stopped_status.slot[1] = status_code;
1203 } else if ((csa->prob.spu_status_R & status_P) == status_P) {
1205 /* SPU_Status[P]=1 - Stop and Signal instruction
1206 * followed by 'br -4'.
1208 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P;
1209 csa->lscsa->stopped_status.slot[1] = status_code;
1211 } else if ((csa->prob.spu_status_R & status_H) == status_H) {
1213 /* SPU_Status[H]=1 - Halt Conditional, followed
1216 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_H;
1218 } else if ((csa->prob.spu_status_R & status_S) == status_S) {
1220 /* SPU_Status[S]=1 - Two nop instructions.
1222 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S;
1224 } else if ((csa->prob.spu_status_R & status_I) == status_I) {
1226 /* SPU_Status[I]=1 - Illegal instruction followed
1229 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_I;
1234 static inline void setup_spu_status_part2(struct spu_state *csa,
1239 /* Restore, Step 28:
1240 * If the CSA.SPU_Status[I,S,H,P,R]=0 then
1241 * add a 'br *' instruction to the end of
1242 * the SPU based restore code.
1244 * NOTE: Rather than modifying the SPU executable, we
1245 * instead add a new 'stopped_status' field to the
1246 * LSCSA. The SPU-side restore reads this field and
1247 * takes the appropriate action when exiting.
1249 mask = SPU_STATUS_INVALID_INSTR |
1250 SPU_STATUS_SINGLE_STEP |
1251 SPU_STATUS_STOPPED_BY_HALT |
1252 SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
1253 if (!(csa->prob.spu_status_R & mask)) {
1254 csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_R;
1258 static inline void restore_mfc_rag(struct spu_state *csa, struct spu *spu)
1260 struct spu_priv1 __iomem *priv1 = spu->priv1;
1262 /* Restore, Step 29:
1263 * Restore RA_GROUP_ID register and the
1264 * RA_ENABLE reigster from the CSA.
1266 out_be64(&priv1->resource_allocation_groupID_RW,
1267 csa->priv1.resource_allocation_groupID_RW);
1268 out_be64(&priv1->resource_allocation_enable_RW,
1269 csa->priv1.resource_allocation_enable_RW);
1272 static inline void send_restore_code(struct spu_state *csa, struct spu *spu)
1274 unsigned long addr = (unsigned long)&spu_restore_code[0];
1275 unsigned int ls_offset = 0x0;
1276 unsigned int size = sizeof(spu_restore_code);
1277 unsigned int tag = 0;
1278 unsigned int rclass = 0;
1279 unsigned int cmd = MFC_GETFS_CMD;
1281 /* Restore, Step 37:
1282 * Issue MFC DMA command to copy context
1283 * restore code to local storage.
1285 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
1288 static inline void setup_decr(struct spu_state *csa, struct spu *spu)
1290 /* Restore, Step 34:
1291 * If CSA.MFC_CNTL[Ds]=1 (decrementer was
1292 * running) then adjust decrementer, set
1293 * decrementer running status in LSCSA,
1294 * and set decrementer "wrapped" status
1297 if (csa->priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING) {
1298 cycles_t resume_time = get_cycles();
1299 cycles_t delta_time = resume_time - csa->suspend_time;
1301 csa->lscsa->decr.slot[0] = delta_time;
1305 static inline void setup_ppu_mb(struct spu_state *csa, struct spu *spu)
1307 /* Restore, Step 35:
1308 * Copy the CSA.PU_MB data into the LSCSA.
1310 csa->lscsa->ppu_mb.slot[0] = csa->prob.pu_mb_R;
1313 static inline void setup_ppuint_mb(struct spu_state *csa, struct spu *spu)
1315 /* Restore, Step 36:
1316 * Copy the CSA.PUINT_MB data into the LSCSA.
1318 csa->lscsa->ppuint_mb.slot[0] = csa->priv2.puint_mb_R;
1321 static inline int check_restore_status(struct spu_state *csa, struct spu *spu)
1323 struct spu_problem __iomem *prob = spu->problem;
1326 /* Restore, Step 40:
1327 * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
1328 * context restore succeeded, otherwise context restore
1331 complete = ((SPU_RESTORE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
1332 SPU_STATUS_STOPPED_BY_STOP);
1333 return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
1336 static inline void restore_spu_privcntl(struct spu_state *csa, struct spu *spu)
1338 struct spu_priv2 __iomem *priv2 = spu->priv2;
1340 /* Restore, Step 41:
1341 * Restore SPU_PrivCntl from the CSA.
1343 out_be64(&priv2->spu_privcntl_RW, csa->priv2.spu_privcntl_RW);
1347 static inline void restore_status_part1(struct spu_state *csa, struct spu *spu)
1349 struct spu_problem __iomem *prob = spu->problem;
1352 /* Restore, Step 42:
1353 * If any CSA.SPU_Status[I,S,H,P]=1, then
1354 * restore the error or single step state.
1356 mask = SPU_STATUS_INVALID_INSTR |
1357 SPU_STATUS_SINGLE_STEP |
1358 SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
1359 if (csa->prob.spu_status_R & mask) {
1360 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1362 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1363 SPU_STATUS_RUNNING);
1367 static inline void restore_status_part2(struct spu_state *csa, struct spu *spu)
1369 struct spu_problem __iomem *prob = spu->problem;
1372 /* Restore, Step 43:
1373 * If all CSA.SPU_Status[I,S,H,P,R]=0 then write
1374 * SPU_RunCntl[R0R1]='01', wait for SPU_Status[R]=1,
1375 * then write '00' to SPU_RunCntl[R0R1] and wait
1376 * for SPU_Status[R]=0.
1378 mask = SPU_STATUS_INVALID_INSTR |
1379 SPU_STATUS_SINGLE_STEP |
1380 SPU_STATUS_STOPPED_BY_HALT |
1381 SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
1382 if (!(csa->prob.spu_status_R & mask)) {
1383 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1385 POLL_WHILE_FALSE(in_be32(&prob->spu_status_R) &
1386 SPU_STATUS_RUNNING);
1387 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
1389 POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
1390 SPU_STATUS_RUNNING);
1394 static inline void restore_ls_16kb(struct spu_state *csa, struct spu *spu)
1396 unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
1397 unsigned int ls_offset = 0x0;
1398 unsigned int size = 16384;
1399 unsigned int tag = 0;
1400 unsigned int rclass = 0;
1401 unsigned int cmd = MFC_GET_CMD;
1403 /* Restore, Step 44:
1404 * Issue a DMA command to restore the first
1405 * 16kb of local storage from CSA.
1407 send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
1410 static inline void clear_interrupts(struct spu_state *csa, struct spu *spu)
1412 struct spu_priv1 __iomem *priv1 = spu->priv1;
1414 /* Restore, Step 49:
1415 * Write INT_MASK_class0 with value of 0.
1416 * Write INT_MASK_class1 with value of 0.
1417 * Write INT_MASK_class2 with value of 0.
1418 * Write INT_STAT_class0 with value of -1.
1419 * Write INT_STAT_class1 with value of -1.
1420 * Write INT_STAT_class2 with value of -1.
1422 spin_lock_irq(&spu->register_lock);
1423 out_be64(&priv1->int_mask_class0_RW, 0UL);
1424 out_be64(&priv1->int_mask_class1_RW, 0UL);
1425 out_be64(&priv1->int_mask_class2_RW, 0UL);
1426 out_be64(&priv1->int_stat_class0_RW, ~(0UL));
1427 out_be64(&priv1->int_stat_class1_RW, ~(0UL));
1428 out_be64(&priv1->int_stat_class2_RW, ~(0UL));
1429 spin_unlock_irq(&spu->register_lock);
1432 static inline void restore_mfc_queues(struct spu_state *csa, struct spu *spu)
1434 struct spu_priv2 __iomem *priv2 = spu->priv2;
1437 /* Restore, Step 50:
1438 * If MFC_Cntl[Se]!=0 then restore
1439 * MFC command queues.
1441 if ((csa->priv2.mfc_control_RW & MFC_CNTL_DMA_QUEUES_EMPTY_MASK) == 0) {
1442 for (i = 0; i < 8; i++) {
1443 out_be64(&priv2->puq[i].mfc_cq_data0_RW,
1444 csa->priv2.puq[i].mfc_cq_data0_RW);
1445 out_be64(&priv2->puq[i].mfc_cq_data1_RW,
1446 csa->priv2.puq[i].mfc_cq_data1_RW);
1447 out_be64(&priv2->puq[i].mfc_cq_data2_RW,
1448 csa->priv2.puq[i].mfc_cq_data2_RW);
1449 out_be64(&priv2->puq[i].mfc_cq_data3_RW,
1450 csa->priv2.puq[i].mfc_cq_data3_RW);
1452 for (i = 0; i < 16; i++) {
1453 out_be64(&priv2->spuq[i].mfc_cq_data0_RW,
1454 csa->priv2.spuq[i].mfc_cq_data0_RW);
1455 out_be64(&priv2->spuq[i].mfc_cq_data1_RW,
1456 csa->priv2.spuq[i].mfc_cq_data1_RW);
1457 out_be64(&priv2->spuq[i].mfc_cq_data2_RW,
1458 csa->priv2.spuq[i].mfc_cq_data2_RW);
1459 out_be64(&priv2->spuq[i].mfc_cq_data3_RW,
1460 csa->priv2.spuq[i].mfc_cq_data3_RW);
1466 static inline void restore_ppu_querymask(struct spu_state *csa, struct spu *spu)
1468 struct spu_problem __iomem *prob = spu->problem;
1470 /* Restore, Step 51:
1471 * Restore the PPU_QueryMask register from CSA.
1473 out_be32(&prob->dma_querymask_RW, csa->prob.dma_querymask_RW);
1477 static inline void restore_ppu_querytype(struct spu_state *csa, struct spu *spu)
1479 struct spu_problem __iomem *prob = spu->problem;
1481 /* Restore, Step 52:
1482 * Restore the PPU_QueryType register from CSA.
1484 out_be32(&prob->dma_querytype_RW, csa->prob.dma_querytype_RW);
1488 static inline void restore_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
1490 struct spu_priv2 __iomem *priv2 = spu->priv2;
1492 /* Restore, Step 53:
1493 * Restore the MFC_CSR_TSQ register from CSA.
1495 out_be64(&priv2->spu_tag_status_query_RW,
1496 csa->priv2.spu_tag_status_query_RW);
1500 static inline void restore_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
1502 struct spu_priv2 __iomem *priv2 = spu->priv2;
1504 /* Restore, Step 54:
1505 * Restore the MFC_CSR_CMD1 and MFC_CSR_CMD2
1506 * registers from CSA.
1508 out_be64(&priv2->spu_cmd_buf1_RW, csa->priv2.spu_cmd_buf1_RW);
1509 out_be64(&priv2->spu_cmd_buf2_RW, csa->priv2.spu_cmd_buf2_RW);
1513 static inline void restore_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
1515 struct spu_priv2 __iomem *priv2 = spu->priv2;
1517 /* Restore, Step 55:
1518 * Restore the MFC_CSR_ATO register from CSA.
1520 out_be64(&priv2->spu_atomic_status_RW, csa->priv2.spu_atomic_status_RW);
1523 static inline void restore_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
1525 struct spu_priv1 __iomem *priv1 = spu->priv1;
1527 /* Restore, Step 56:
1528 * Restore the MFC_TCLASS_ID register from CSA.
1530 out_be64(&priv1->mfc_tclass_id_RW, csa->priv1.mfc_tclass_id_RW);
1534 static inline void set_llr_event(struct spu_state *csa, struct spu *spu)
1536 u64 ch0_cnt, ch0_data;
1539 /* Restore, Step 57:
1540 * Set the Lock Line Reservation Lost Event by:
1541 * 1. OR CSA.SPU_Event_Status with bit 21 (Lr) set to 1.
1542 * 2. If CSA.SPU_Channel_0_Count=0 and
1543 * CSA.SPU_Wr_Event_Mask[Lr]=1 and
1544 * CSA.SPU_Event_Status[Lr]=0 then set
1545 * CSA.SPU_Event_Status_Count=1.
1547 ch0_cnt = csa->spu_chnlcnt_RW[0];
1548 ch0_data = csa->spu_chnldata_RW[0];
1549 ch1_data = csa->spu_chnldata_RW[1];
1550 csa->spu_chnldata_RW[0] |= MFC_LLR_LOST_EVENT;
1551 if ((ch0_cnt == 0) && !(ch0_data & MFC_LLR_LOST_EVENT) &&
1552 (ch1_data & MFC_LLR_LOST_EVENT)) {
1553 csa->spu_chnlcnt_RW[0] = 1;
1557 static inline void restore_decr_wrapped(struct spu_state *csa, struct spu *spu)
1559 /* Restore, Step 58:
1560 * If the status of the CSA software decrementer
1561 * "wrapped" flag is set, OR in a '1' to
1562 * CSA.SPU_Event_Status[Tm].
1564 if (csa->lscsa->decr_status.slot[0] == 1) {
1565 csa->spu_chnldata_RW[0] |= 0x20;
1567 if ((csa->lscsa->decr_status.slot[0] == 1) &&
1568 (csa->spu_chnlcnt_RW[0] == 0 &&
1569 ((csa->spu_chnldata_RW[2] & 0x20) == 0x0) &&
1570 ((csa->spu_chnldata_RW[0] & 0x20) != 0x1))) {
1571 csa->spu_chnlcnt_RW[0] = 1;
1575 static inline void restore_ch_part1(struct spu_state *csa, struct spu *spu)
1577 struct spu_priv2 __iomem *priv2 = spu->priv2;
1578 u64 idx, ch_indices[7] = { 0UL, 1UL, 3UL, 4UL, 24UL, 25UL, 27UL };
1581 /* Restore, Step 59:
1582 * Restore the following CH: [0,1,3,4,24,25,27]
1584 for (i = 0; i < 7; i++) {
1585 idx = ch_indices[i];
1586 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1588 out_be64(&priv2->spu_chnldata_RW, csa->spu_chnldata_RW[idx]);
1589 out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[idx]);
1594 static inline void restore_ch_part2(struct spu_state *csa, struct spu *spu)
1596 struct spu_priv2 __iomem *priv2 = spu->priv2;
1597 u64 ch_indices[3] = { 9UL, 21UL, 23UL };
1598 u64 ch_counts[3] = { 1UL, 16UL, 1UL };
1602 /* Restore, Step 60:
1603 * Restore the following CH: [9,21,23].
1606 ch_counts[1] = csa->spu_chnlcnt_RW[21];
1608 for (i = 0; i < 3; i++) {
1609 idx = ch_indices[i];
1610 out_be64(&priv2->spu_chnlcntptr_RW, idx);
1612 out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
1617 static inline void restore_spu_lslr(struct spu_state *csa, struct spu *spu)
1619 struct spu_priv2 __iomem *priv2 = spu->priv2;
1621 /* Restore, Step 61:
1622 * Restore the SPU_LSLR register from CSA.
1624 out_be64(&priv2->spu_lslr_RW, csa->priv2.spu_lslr_RW);
1628 static inline void restore_spu_cfg(struct spu_state *csa, struct spu *spu)
1630 struct spu_priv2 __iomem *priv2 = spu->priv2;
1632 /* Restore, Step 62:
1633 * Restore the SPU_Cfg register from CSA.
1635 out_be64(&priv2->spu_cfg_RW, csa->priv2.spu_cfg_RW);
1639 static inline void restore_pm_trace(struct spu_state *csa, struct spu *spu)
1641 /* Restore, Step 63:
1642 * Restore PM_Trace_Tag_Wait_Mask from CSA.
1643 * Not performed by this implementation.
1647 static inline void restore_spu_npc(struct spu_state *csa, struct spu *spu)
1649 struct spu_problem __iomem *prob = spu->problem;
1651 /* Restore, Step 64:
1652 * Restore SPU_NPC from CSA.
1654 out_be32(&prob->spu_npc_RW, csa->prob.spu_npc_RW);
1658 static inline void restore_spu_mb(struct spu_state *csa, struct spu *spu)
1660 struct spu_priv2 __iomem *priv2 = spu->priv2;
1663 /* Restore, Step 65:
1664 * Restore MFC_RdSPU_MB from CSA.
1666 out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
1668 out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[29]);
1669 for (i = 0; i < 4; i++) {
1670 out_be64(&priv2->spu_chnldata_RW, csa->spu_mailbox_data[i]);
1675 static inline void check_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
1677 struct spu_problem __iomem *prob = spu->problem;
1680 /* Restore, Step 66:
1681 * If CSA.MB_Stat[P]=0 (mailbox empty) then
1682 * read from the PPU_MB register.
1684 if ((csa->prob.mb_stat_R & 0xFF) == 0) {
1685 dummy = in_be32(&prob->pu_mb_R);
1690 static inline void check_ppuint_mb_stat(struct spu_state *csa, struct spu *spu)
1692 struct spu_priv1 __iomem *priv1 = spu->priv1;
1693 struct spu_priv2 __iomem *priv2 = spu->priv2;
1696 /* Restore, Step 66:
1697 * If CSA.MB_Stat[I]=0 (mailbox empty) then
1698 * read from the PPUINT_MB register.
1700 if ((csa->prob.mb_stat_R & 0xFF0000) == 0) {
1701 dummy = in_be64(&priv2->puint_mb_R);
1703 out_be64(&priv1->int_stat_class2_RW,
1704 CLASS2_ENABLE_MAILBOX_INTR);
1709 static inline void restore_mfc_slbs(struct spu_state *csa, struct spu *spu)
1711 struct spu_priv2 __iomem *priv2 = spu->priv2;
1714 /* Restore, Step 68:
1715 * If MFC_SR1[R]='1', restore SLBs from CSA.
1717 if (csa->priv1.mfc_sr1_RW & MFC_STATE1_RELOCATE_MASK) {
1718 for (i = 0; i < 8; i++) {
1719 out_be64(&priv2->slb_index_W, i);
1721 out_be64(&priv2->slb_esid_RW, csa->slb_esid_RW[i]);
1722 out_be64(&priv2->slb_vsid_RW, csa->slb_vsid_RW[i]);
1725 out_be64(&priv2->slb_index_W, csa->priv2.slb_index_W);
1730 static inline void restore_mfc_sr1(struct spu_state *csa, struct spu *spu)
1732 struct spu_priv1 __iomem *priv1 = spu->priv1;
1734 /* Restore, Step 69:
1735 * Restore the MFC_SR1 register from CSA.
1737 out_be64(&priv1->mfc_sr1_RW, csa->priv1.mfc_sr1_RW);
1741 static inline void restore_other_spu_access(struct spu_state *csa,
1744 /* Restore, Step 70:
1745 * Restore other SPU mappings to this SPU. TBD.
1749 static inline void restore_spu_runcntl(struct spu_state *csa, struct spu *spu)
1751 struct spu_problem __iomem *prob = spu->problem;
1753 /* Restore, Step 71:
1754 * If CSA.SPU_Status[R]=1 then write
1755 * SPU_RunCntl[R0R1]='01'.
1757 if (csa->prob.spu_status_R & SPU_STATUS_RUNNING) {
1758 out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
1763 static inline void restore_mfc_cntl(struct spu_state *csa, struct spu *spu)
1765 struct spu_priv2 __iomem *priv2 = spu->priv2;
1767 /* Restore, Step 72:
1768 * Restore the MFC_CNTL register for the CSA.
1770 out_be64(&priv2->mfc_control_RW, csa->priv2.mfc_control_RW);
1774 static inline void enable_user_access(struct spu_state *csa, struct spu *spu)
1776 /* Restore, Step 73:
1777 * Enable user-space access (if provided) to this
1778 * SPU by mapping the virtual pages assigned to
1779 * the SPU memory-mapped I/O (MMIO) for problem
1784 static inline void reset_switch_active(struct spu_state *csa, struct spu *spu)
1786 /* Restore, Step 74:
1787 * Reset the "context switch active" flag.
1789 clear_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags);
1793 static inline void reenable_interrupts(struct spu_state *csa, struct spu *spu)
1795 struct spu_priv1 __iomem *priv1 = spu->priv1;
1797 /* Restore, Step 75:
1798 * Re-enable SPU interrupts.
1800 spin_lock_irq(&spu->register_lock);
1801 out_be64(&priv1->int_mask_class0_RW, csa->priv1.int_mask_class0_RW);
1802 out_be64(&priv1->int_mask_class1_RW, csa->priv1.int_mask_class1_RW);
1803 out_be64(&priv1->int_mask_class2_RW, csa->priv1.int_mask_class2_RW);
1804 spin_unlock_irq(&spu->register_lock);
1807 static int quiece_spu(struct spu_state *prev, struct spu *spu)
1810 * Combined steps 2-18 of SPU context save sequence, which
1811 * quiesce the SPU state (disable SPU execution, MFC command
1812 * queues, decrementer, SPU interrupts, etc.).
1814 * Returns 0 on success.
1815 * 2 if failed step 2.
1816 * 6 if failed step 6.
1819 if (check_spu_isolate(prev, spu)) { /* Step 2. */
1822 disable_interrupts(prev, spu); /* Step 3. */
1823 set_watchdog_timer(prev, spu); /* Step 4. */
1824 inhibit_user_access(prev, spu); /* Step 5. */
1825 if (check_spu_isolate(prev, spu)) { /* Step 6. */
1828 set_switch_pending(prev, spu); /* Step 7. */
1829 save_mfc_cntl(prev, spu); /* Step 8. */
1830 save_spu_runcntl(prev, spu); /* Step 9. */
1831 save_mfc_sr1(prev, spu); /* Step 10. */
1832 save_spu_status(prev, spu); /* Step 11. */
1833 save_mfc_decr(prev, spu); /* Step 12. */
1834 halt_mfc_decr(prev, spu); /* Step 13. */
1835 save_timebase(prev, spu); /* Step 14. */
1836 remove_other_spu_access(prev, spu); /* Step 15. */
1837 do_mfc_mssync(prev, spu); /* Step 16. */
1838 issue_mfc_tlbie(prev, spu); /* Step 17. */
1839 handle_pending_interrupts(prev, spu); /* Step 18. */
1844 static void save_csa(struct spu_state *prev, struct spu *spu)
1847 * Combine steps 19-44 of SPU context save sequence, which
1848 * save regions of the privileged & problem state areas.
1851 save_mfc_queues(prev, spu); /* Step 19. */
1852 save_ppu_querymask(prev, spu); /* Step 20. */
1853 save_ppu_querytype(prev, spu); /* Step 21. */
1854 save_mfc_csr_tsq(prev, spu); /* Step 22. */
1855 save_mfc_csr_cmd(prev, spu); /* Step 23. */
1856 save_mfc_csr_ato(prev, spu); /* Step 24. */
1857 save_mfc_tclass_id(prev, spu); /* Step 25. */
1858 set_mfc_tclass_id(prev, spu); /* Step 26. */
1859 purge_mfc_queue(prev, spu); /* Step 27. */
1860 wait_purge_complete(prev, spu); /* Step 28. */
1861 save_mfc_slbs(prev, spu); /* Step 29. */
1862 setup_mfc_sr1(prev, spu); /* Step 30. */
1863 save_spu_npc(prev, spu); /* Step 31. */
1864 save_spu_privcntl(prev, spu); /* Step 32. */
1865 reset_spu_privcntl(prev, spu); /* Step 33. */
1866 save_spu_lslr(prev, spu); /* Step 34. */
1867 reset_spu_lslr(prev, spu); /* Step 35. */
1868 save_spu_cfg(prev, spu); /* Step 36. */
1869 save_pm_trace(prev, spu); /* Step 37. */
1870 save_mfc_rag(prev, spu); /* Step 38. */
1871 save_ppu_mb_stat(prev, spu); /* Step 39. */
1872 save_ppu_mb(prev, spu); /* Step 40. */
1873 save_ppuint_mb(prev, spu); /* Step 41. */
1874 save_ch_part1(prev, spu); /* Step 42. */
1875 save_spu_mb(prev, spu); /* Step 43. */
1876 save_mfc_cmd(prev, spu); /* Step 44. */
1877 reset_ch(prev, spu); /* Step 45. */
1880 static void save_lscsa(struct spu_state *prev, struct spu *spu)
1883 * Perform steps 46-57 of SPU context save sequence,
1884 * which save regions of the local store and register
1888 resume_mfc_queue(prev, spu); /* Step 46. */
1889 setup_mfc_slbs(prev, spu); /* Step 47. */
1890 set_switch_active(prev, spu); /* Step 48. */
1891 enable_interrupts(prev, spu); /* Step 49. */
1892 save_ls_16kb(prev, spu); /* Step 50. */
1893 set_spu_npc(prev, spu); /* Step 51. */
1894 set_signot1(prev, spu); /* Step 52. */
1895 set_signot2(prev, spu); /* Step 53. */
1896 send_save_code(prev, spu); /* Step 54. */
1897 set_ppu_querymask(prev, spu); /* Step 55. */
1898 wait_tag_complete(prev, spu); /* Step 56. */
1899 wait_spu_stopped(prev, spu); /* Step 57. */
1902 static void harvest(struct spu_state *prev, struct spu *spu)
1905 * Perform steps 2-25 of SPU context restore sequence,
1906 * which resets an SPU either after a failed save, or
1907 * when using SPU for first time.
1910 disable_interrupts(prev, spu); /* Step 2. */
1911 inhibit_user_access(prev, spu); /* Step 3. */
1912 terminate_spu_app(prev, spu); /* Step 4. */
1913 set_switch_pending(prev, spu); /* Step 5. */
1914 remove_other_spu_access(prev, spu); /* Step 6. */
1915 suspend_mfc(prev, spu); /* Step 7. */
1916 wait_suspend_mfc_complete(prev, spu); /* Step 8. */
1917 if (!suspend_spe(prev, spu)) /* Step 9. */
1918 clear_spu_status(prev, spu); /* Step 10. */
1919 do_mfc_mssync(prev, spu); /* Step 11. */
1920 issue_mfc_tlbie(prev, spu); /* Step 12. */
1921 handle_pending_interrupts(prev, spu); /* Step 13. */
1922 purge_mfc_queue(prev, spu); /* Step 14. */
1923 wait_purge_complete(prev, spu); /* Step 15. */
1924 reset_spu_privcntl(prev, spu); /* Step 16. */
1925 reset_spu_lslr(prev, spu); /* Step 17. */
1926 setup_mfc_sr1(prev, spu); /* Step 18. */
1927 invalidate_slbs(prev, spu); /* Step 19. */
1928 reset_ch_part1(prev, spu); /* Step 20. */
1929 reset_ch_part2(prev, spu); /* Step 21. */
1930 enable_interrupts(prev, spu); /* Step 22. */
1931 set_switch_active(prev, spu); /* Step 23. */
1932 set_mfc_tclass_id(prev, spu); /* Step 24. */
1933 resume_mfc_queue(prev, spu); /* Step 25. */
1936 static void restore_lscsa(struct spu_state *next, struct spu *spu)
1939 * Perform steps 26-40 of SPU context restore sequence,
1940 * which restores regions of the local store and register
1944 set_watchdog_timer(next, spu); /* Step 26. */
1945 setup_spu_status_part1(next, spu); /* Step 27. */
1946 setup_spu_status_part2(next, spu); /* Step 28. */
1947 restore_mfc_rag(next, spu); /* Step 29. */
1948 setup_mfc_slbs(next, spu); /* Step 30. */
1949 set_spu_npc(next, spu); /* Step 31. */
1950 set_signot1(next, spu); /* Step 32. */
1951 set_signot2(next, spu); /* Step 33. */
1952 setup_decr(next, spu); /* Step 34. */
1953 setup_ppu_mb(next, spu); /* Step 35. */
1954 setup_ppuint_mb(next, spu); /* Step 36. */
1955 send_restore_code(next, spu); /* Step 37. */
1956 set_ppu_querymask(next, spu); /* Step 38. */
1957 wait_tag_complete(next, spu); /* Step 39. */
1958 wait_spu_stopped(next, spu); /* Step 40. */
1961 static void restore_csa(struct spu_state *next, struct spu *spu)
1964 * Combine steps 41-76 of SPU context restore sequence, which
1965 * restore regions of the privileged & problem state areas.
1968 restore_spu_privcntl(next, spu); /* Step 41. */
1969 restore_status_part1(next, spu); /* Step 42. */
1970 restore_status_part2(next, spu); /* Step 43. */
1971 restore_ls_16kb(next, spu); /* Step 44. */
1972 wait_tag_complete(next, spu); /* Step 45. */
1973 suspend_mfc(next, spu); /* Step 46. */
1974 wait_suspend_mfc_complete(next, spu); /* Step 47. */
1975 issue_mfc_tlbie(next, spu); /* Step 48. */
1976 clear_interrupts(next, spu); /* Step 49. */
1977 restore_mfc_queues(next, spu); /* Step 50. */
1978 restore_ppu_querymask(next, spu); /* Step 51. */
1979 restore_ppu_querytype(next, spu); /* Step 52. */
1980 restore_mfc_csr_tsq(next, spu); /* Step 53. */
1981 restore_mfc_csr_cmd(next, spu); /* Step 54. */
1982 restore_mfc_csr_ato(next, spu); /* Step 55. */
1983 restore_mfc_tclass_id(next, spu); /* Step 56. */
1984 set_llr_event(next, spu); /* Step 57. */
1985 restore_decr_wrapped(next, spu); /* Step 58. */
1986 restore_ch_part1(next, spu); /* Step 59. */
1987 restore_ch_part2(next, spu); /* Step 60. */
1988 restore_spu_lslr(next, spu); /* Step 61. */
1989 restore_spu_cfg(next, spu); /* Step 62. */
1990 restore_pm_trace(next, spu); /* Step 63. */
1991 restore_spu_npc(next, spu); /* Step 64. */
1992 restore_spu_mb(next, spu); /* Step 65. */
1993 check_ppu_mb_stat(next, spu); /* Step 66. */
1994 check_ppuint_mb_stat(next, spu); /* Step 67. */
1995 restore_mfc_slbs(next, spu); /* Step 68. */
1996 restore_mfc_sr1(next, spu); /* Step 69. */
1997 restore_other_spu_access(next, spu); /* Step 70. */
1998 restore_spu_runcntl(next, spu); /* Step 71. */
1999 restore_mfc_cntl(next, spu); /* Step 72. */
2000 enable_user_access(next, spu); /* Step 73. */
2001 reset_switch_active(next, spu); /* Step 74. */
2002 reenable_interrupts(next, spu); /* Step 75. */
2005 static int __do_spu_save(struct spu_state *prev, struct spu *spu)
2010 * SPU context save can be broken into three phases:
2012 * (a) quiesce [steps 2-16].
2013 * (b) save of CSA, performed by PPE [steps 17-42]
2014 * (c) save of LSCSA, mostly performed by SPU [steps 43-52].
2016 * Returns 0 on success.
2017 * 2,6 if failed to quiece SPU
2018 * 53 if SPU-side of save failed.
2021 rc = quiece_spu(prev, spu); /* Steps 2-16. */
2032 save_csa(prev, spu); /* Steps 17-43. */
2033 save_lscsa(prev, spu); /* Steps 44-53. */
2034 return check_save_status(prev, spu); /* Step 54. */
2037 static int __do_spu_restore(struct spu_state *next, struct spu *spu)
2042 * SPU context restore can be broken into three phases:
2044 * (a) harvest (or reset) SPU [steps 2-24].
2045 * (b) restore LSCSA [steps 25-40], mostly performed by SPU.
2046 * (c) restore CSA [steps 41-76], performed by PPE.
2048 * The 'harvest' step is not performed here, but rather
2052 restore_lscsa(next, spu); /* Steps 24-39. */
2053 rc = check_restore_status(next, spu); /* Step 40. */
2056 /* Failed. Return now. */
2060 /* Fall through to next step. */
2063 restore_csa(next, spu);
2069 * spu_save - SPU context save, with locking.
2070 * @prev: pointer to SPU context save area, to be saved.
2071 * @spu: pointer to SPU iomem structure.
2073 * Acquire locks, perform the save operation then return.
2075 int spu_save(struct spu_state *prev, struct spu *spu)
2079 acquire_spu_lock(spu); /* Step 1. */
2080 rc = __do_spu_save(prev, spu); /* Steps 2-53. */
2081 release_spu_lock(spu);
2083 panic("%s failed on SPU[%d], rc=%d.\n",
2084 __func__, spu->number, rc);
2090 * spu_restore - SPU context restore, with harvest and locking.
2091 * @new: pointer to SPU context save area, to be restored.
2092 * @spu: pointer to SPU iomem structure.
2094 * Perform harvest + restore, as we may not be coming
2095 * from a previous succesful save operation, and the
2096 * hardware state is unknown.
2098 int spu_restore(struct spu_state *new, struct spu *spu)
2102 acquire_spu_lock(spu);
2107 spu->slb_replace = 0;
2108 spu->class_0_pending = 0;
2109 rc = __do_spu_restore(new, spu);
2110 release_spu_lock(spu);
2112 panic("%s failed on SPU[%d] rc=%d.\n",
2113 __func__, spu->number, rc);
2119 * spu_harvest - SPU harvest (reset) operation
2120 * @spu: pointer to SPU iomem structure.
2122 * Perform SPU harvest (reset) operation.
2124 void spu_harvest(struct spu *spu)
2126 acquire_spu_lock(spu);
2128 release_spu_lock(spu);
2131 static void init_prob(struct spu_state *csa)
2133 csa->spu_chnlcnt_RW[9] = 1;
2134 csa->spu_chnlcnt_RW[21] = 16;
2135 csa->spu_chnlcnt_RW[23] = 1;
2136 csa->spu_chnlcnt_RW[28] = 1;
2137 csa->spu_chnlcnt_RW[30] = 1;
2138 csa->prob.spu_runcntl_RW = SPU_RUNCNTL_STOP;
2141 static void init_priv1(struct spu_state *csa)
2143 /* Enable decode, relocate, tlbie response, master runcntl. */
2144 csa->priv1.mfc_sr1_RW = MFC_STATE1_LOCAL_STORAGE_DECODE_MASK |
2145 MFC_STATE1_MASTER_RUN_CONTROL_MASK |
2146 MFC_STATE1_PROBLEM_STATE_MASK |
2147 MFC_STATE1_RELOCATE_MASK | MFC_STATE1_BUS_TLBIE_MASK;
2149 /* Set storage description. */
2150 csa->priv1.mfc_sdr_RW = mfspr(SPRN_SDR1);
2152 /* Enable OS-specific set of interrupts. */
2153 csa->priv1.int_mask_class0_RW = CLASS0_ENABLE_DMA_ALIGNMENT_INTR |
2154 CLASS0_ENABLE_INVALID_DMA_COMMAND_INTR |
2155 CLASS0_ENABLE_SPU_ERROR_INTR;
2156 csa->priv1.int_mask_class1_RW = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
2157 CLASS1_ENABLE_STORAGE_FAULT_INTR;
2158 csa->priv1.int_mask_class2_RW = CLASS2_ENABLE_SPU_STOP_INTR |
2159 CLASS2_ENABLE_SPU_HALT_INTR;
2162 static void init_priv2(struct spu_state *csa)
2164 csa->priv2.spu_lslr_RW = LS_ADDR_MASK;
2165 csa->priv2.mfc_control_RW = MFC_CNTL_RESUME_DMA_QUEUE |
2166 MFC_CNTL_NORMAL_DMA_QUEUE_OPERATION |
2167 MFC_CNTL_DMA_QUEUES_EMPTY_MASK;
2171 * spu_alloc_csa - allocate and initialize an SPU context save area.
2173 * Allocate and initialize the contents of an SPU context save area.
2174 * This includes enabling address translation, interrupt masks, etc.,
2175 * as appropriate for the given OS environment.
2177 * Note that storage for the 'lscsa' is allocated separately,
2178 * as it is by far the largest of the context save regions,
2179 * and may need to be pinned or otherwise specially aligned.
2181 void spu_init_csa(struct spu_state *csa)
2183 struct spu_lscsa *lscsa;
2188 memset(csa, 0, sizeof(struct spu_state));
2190 lscsa = vmalloc(sizeof(struct spu_lscsa));
2194 memset(lscsa, 0, sizeof(struct spu_lscsa));
2196 csa->register_lock = SPIN_LOCK_UNLOCKED;
2198 /* Set LS pages reserved to allow for user-space mapping. */
2199 for (p = lscsa->ls; p < lscsa->ls + LS_SIZE; p += PAGE_SIZE)
2200 SetPageReserved(vmalloc_to_page(p));
2207 void spu_fini_csa(struct spu_state *csa)
2209 /* Clear reserved bit before vfree. */
2211 for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
2212 ClearPageReserved(vmalloc_to_page(p));