switch.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. /*
  2. * spu_switch.c
  3. *
  4. * (C) Copyright IBM Corp. 2005
  5. *
  6. * Author: Mark Nutter <mnutter@us.ibm.com>
  7. *
  8. * Host-side part of SPU context switch sequence outlined in
  9. * Synergistic Processor Element, Book IV.
  10. *
  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.
  18. *
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2, or (at your option)
  23. * any later version.
  24. *
  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.
  29. *
  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.
  33. */
  34. #include <linux/config.h>
  35. #include <linux/module.h>
  36. #include <linux/errno.h>
  37. #include <linux/sched.h>
  38. #include <linux/kernel.h>
  39. #include <linux/mm.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/smp.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/stddef.h>
  44. #include <linux/unistd.h>
  45. #include <asm/io.h>
  46. #include <asm/spu.h>
  47. #include <asm/spu_csa.h>
  48. #include <asm/mmu_context.h>
  49. #include "spu_save_dump.h"
  50. #include "spu_restore_dump.h"
  51. #if 0
  52. #define POLL_WHILE_TRUE(_c) { \
  53. do { \
  54. } while (_c); \
  55. }
  56. #else
  57. #define RELAX_SPIN_COUNT 1000
  58. #define POLL_WHILE_TRUE(_c) { \
  59. do { \
  60. int _i; \
  61. for (_i=0; _i<RELAX_SPIN_COUNT && (_c); _i++) { \
  62. cpu_relax(); \
  63. } \
  64. if (unlikely(_c)) yield(); \
  65. else break; \
  66. } while (_c); \
  67. }
  68. #endif /* debug */
  69. #define POLL_WHILE_FALSE(_c) POLL_WHILE_TRUE(!(_c))
  70. static inline void acquire_spu_lock(struct spu *spu)
  71. {
  72. /* Save, Step 1:
  73. * Restore, Step 1:
  74. * Acquire SPU-specific mutual exclusion lock.
  75. * TBD.
  76. */
  77. }
  78. static inline void release_spu_lock(struct spu *spu)
  79. {
  80. /* Restore, Step 76:
  81. * Release SPU-specific mutual exclusion lock.
  82. * TBD.
  83. */
  84. }
  85. static inline int check_spu_isolate(struct spu_state *csa, struct spu *spu)
  86. {
  87. struct spu_problem __iomem *prob = spu->problem;
  88. u32 isolate_state;
  89. /* Save, Step 2:
  90. * Save, Step 6:
  91. * If SPU_Status[E,L,IS] any field is '1', this
  92. * SPU is in isolate state and cannot be context
  93. * saved at this time.
  94. */
  95. isolate_state = SPU_STATUS_ISOLATED_STATE |
  96. SPU_STATUS_ISOLATED_LOAD_STAUTUS | SPU_STATUS_ISOLATED_EXIT_STAUTUS;
  97. return (in_be32(&prob->spu_status_R) & isolate_state) ? 1 : 0;
  98. }
  99. static inline void disable_interrupts(struct spu_state *csa, struct spu *spu)
  100. {
  101. struct spu_priv1 __iomem *priv1 = spu->priv1;
  102. /* Save, Step 3:
  103. * Restore, Step 2:
  104. * Save INT_Mask_class0 in CSA.
  105. * Write INT_MASK_class0 with value of 0.
  106. * Save INT_Mask_class1 in CSA.
  107. * Write INT_MASK_class1 with value of 0.
  108. * Save INT_Mask_class2 in CSA.
  109. * Write INT_MASK_class2 with value of 0.
  110. */
  111. spin_lock_irq(&spu->register_lock);
  112. if (csa) {
  113. csa->priv1.int_mask_class0_RW =
  114. in_be64(&priv1->int_mask_class0_RW);
  115. csa->priv1.int_mask_class1_RW =
  116. in_be64(&priv1->int_mask_class1_RW);
  117. csa->priv1.int_mask_class2_RW =
  118. in_be64(&priv1->int_mask_class2_RW);
  119. }
  120. out_be64(&priv1->int_mask_class0_RW, 0UL);
  121. out_be64(&priv1->int_mask_class1_RW, 0UL);
  122. out_be64(&priv1->int_mask_class2_RW, 0UL);
  123. eieio();
  124. spin_unlock_irq(&spu->register_lock);
  125. }
  126. static inline void set_watchdog_timer(struct spu_state *csa, struct spu *spu)
  127. {
  128. /* Save, Step 4:
  129. * Restore, Step 25.
  130. * Set a software watchdog timer, which specifies the
  131. * maximum allowable time for a context save sequence.
  132. *
  133. * For present, this implementation will not set a global
  134. * watchdog timer, as virtualization & variable system load
  135. * may cause unpredictable execution times.
  136. */
  137. }
  138. static inline void inhibit_user_access(struct spu_state *csa, struct spu *spu)
  139. {
  140. /* Save, Step 5:
  141. * Restore, Step 3:
  142. * Inhibit user-space access (if provided) to this
  143. * SPU by unmapping the virtual pages assigned to
  144. * the SPU memory-mapped I/O (MMIO) for problem
  145. * state. TBD.
  146. */
  147. }
  148. static inline void set_switch_pending(struct spu_state *csa, struct spu *spu)
  149. {
  150. /* Save, Step 7:
  151. * Restore, Step 5:
  152. * Set a software context switch pending flag.
  153. */
  154. set_bit(SPU_CONTEXT_SWITCH_PENDING_nr, &spu->flags);
  155. mb();
  156. }
  157. static inline void save_mfc_cntl(struct spu_state *csa, struct spu *spu)
  158. {
  159. struct spu_priv2 __iomem *priv2 = spu->priv2;
  160. /* Save, Step 8:
  161. * Read and save MFC_CNTL[Ss].
  162. */
  163. if (csa) {
  164. csa->priv2.mfc_control_RW = in_be64(&priv2->mfc_control_RW) &
  165. MFC_CNTL_SUSPEND_DMA_STATUS_MASK;
  166. }
  167. }
  168. static inline void save_spu_runcntl(struct spu_state *csa, struct spu *spu)
  169. {
  170. struct spu_problem __iomem *prob = spu->problem;
  171. /* Save, Step 9:
  172. * Save SPU_Runcntl in the CSA. This value contains
  173. * the "Application Desired State".
  174. */
  175. csa->prob.spu_runcntl_RW = in_be32(&prob->spu_runcntl_RW);
  176. }
  177. static inline void save_mfc_sr1(struct spu_state *csa, struct spu *spu)
  178. {
  179. struct spu_priv1 __iomem *priv1 = spu->priv1;
  180. /* Save, Step 10:
  181. * Save MFC_SR1 in the CSA.
  182. */
  183. csa->priv1.mfc_sr1_RW = in_be64(&priv1->mfc_sr1_RW);
  184. }
  185. static inline void save_spu_status(struct spu_state *csa, struct spu *spu)
  186. {
  187. struct spu_problem __iomem *prob = spu->problem;
  188. /* Save, Step 11:
  189. * Read SPU_Status[R], and save to CSA.
  190. */
  191. if ((in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) == 0) {
  192. csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
  193. } else {
  194. u32 stopped;
  195. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
  196. eieio();
  197. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  198. SPU_STATUS_RUNNING);
  199. stopped =
  200. SPU_STATUS_INVALID_INSTR | SPU_STATUS_SINGLE_STEP |
  201. SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
  202. if ((in_be32(&prob->spu_status_R) & stopped) == 0)
  203. csa->prob.spu_status_R = SPU_STATUS_RUNNING;
  204. else
  205. csa->prob.spu_status_R = in_be32(&prob->spu_status_R);
  206. }
  207. }
  208. static inline void save_mfc_decr(struct spu_state *csa, struct spu *spu)
  209. {
  210. struct spu_priv2 __iomem *priv2 = spu->priv2;
  211. /* Save, Step 12:
  212. * Read MFC_CNTL[Ds]. Update saved copy of
  213. * CSA.MFC_CNTL[Ds].
  214. */
  215. if (in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DECREMENTER_RUNNING) {
  216. csa->priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
  217. csa->suspend_time = get_cycles();
  218. out_be64(&priv2->spu_chnlcntptr_RW, 7ULL);
  219. eieio();
  220. csa->spu_chnldata_RW[7] = in_be64(&priv2->spu_chnldata_RW);
  221. eieio();
  222. }
  223. }
  224. static inline void halt_mfc_decr(struct spu_state *csa, struct spu *spu)
  225. {
  226. struct spu_priv2 __iomem *priv2 = spu->priv2;
  227. /* Save, Step 13:
  228. * Write MFC_CNTL[Dh] set to a '1' to halt
  229. * the decrementer.
  230. */
  231. out_be64(&priv2->mfc_control_RW, MFC_CNTL_DECREMENTER_HALTED);
  232. eieio();
  233. }
  234. static inline void save_timebase(struct spu_state *csa, struct spu *spu)
  235. {
  236. /* Save, Step 14:
  237. * Read PPE Timebase High and Timebase low registers
  238. * and save in CSA. TBD.
  239. */
  240. csa->suspend_time = get_cycles();
  241. }
  242. static inline void remove_other_spu_access(struct spu_state *csa,
  243. struct spu *spu)
  244. {
  245. /* Save, Step 15:
  246. * Remove other SPU access to this SPU by unmapping
  247. * this SPU's pages from their address space. TBD.
  248. */
  249. }
  250. static inline void do_mfc_mssync(struct spu_state *csa, struct spu *spu)
  251. {
  252. struct spu_problem __iomem *prob = spu->problem;
  253. /* Save, Step 16:
  254. * Restore, Step 11.
  255. * Write SPU_MSSync register. Poll SPU_MSSync[P]
  256. * for a value of 0.
  257. */
  258. out_be64(&prob->spc_mssync_RW, 1UL);
  259. POLL_WHILE_TRUE(in_be64(&prob->spc_mssync_RW) & MS_SYNC_PENDING);
  260. }
  261. static inline void issue_mfc_tlbie(struct spu_state *csa, struct spu *spu)
  262. {
  263. struct spu_priv1 __iomem *priv1 = spu->priv1;
  264. /* Save, Step 17:
  265. * Restore, Step 12.
  266. * Restore, Step 48.
  267. * Write TLB_Invalidate_Entry[IS,VPN,L,Lp]=0 register.
  268. * Then issue a PPE sync instruction.
  269. */
  270. out_be64(&priv1->tlb_invalidate_entry_W, 0UL);
  271. mb();
  272. }
  273. static inline void handle_pending_interrupts(struct spu_state *csa,
  274. struct spu *spu)
  275. {
  276. /* Save, Step 18:
  277. * Handle any pending interrupts from this SPU
  278. * here. This is OS or hypervisor specific. One
  279. * option is to re-enable interrupts to handle any
  280. * pending interrupts, with the interrupt handlers
  281. * recognizing the software Context Switch Pending
  282. * flag, to ensure the SPU execution or MFC command
  283. * queue is not restarted. TBD.
  284. */
  285. }
  286. static inline void save_mfc_queues(struct spu_state *csa, struct spu *spu)
  287. {
  288. struct spu_priv2 __iomem *priv2 = spu->priv2;
  289. int i;
  290. /* Save, Step 19:
  291. * If MFC_Cntl[Se]=0 then save
  292. * MFC command queues.
  293. */
  294. if ((in_be64(&priv2->mfc_control_RW) & MFC_CNTL_DMA_QUEUES_EMPTY) == 0) {
  295. for (i = 0; i < 8; i++) {
  296. csa->priv2.puq[i].mfc_cq_data0_RW =
  297. in_be64(&priv2->puq[i].mfc_cq_data0_RW);
  298. csa->priv2.puq[i].mfc_cq_data1_RW =
  299. in_be64(&priv2->puq[i].mfc_cq_data1_RW);
  300. csa->priv2.puq[i].mfc_cq_data2_RW =
  301. in_be64(&priv2->puq[i].mfc_cq_data2_RW);
  302. csa->priv2.puq[i].mfc_cq_data3_RW =
  303. in_be64(&priv2->puq[i].mfc_cq_data3_RW);
  304. }
  305. for (i = 0; i < 16; i++) {
  306. csa->priv2.spuq[i].mfc_cq_data0_RW =
  307. in_be64(&priv2->spuq[i].mfc_cq_data0_RW);
  308. csa->priv2.spuq[i].mfc_cq_data1_RW =
  309. in_be64(&priv2->spuq[i].mfc_cq_data1_RW);
  310. csa->priv2.spuq[i].mfc_cq_data2_RW =
  311. in_be64(&priv2->spuq[i].mfc_cq_data2_RW);
  312. csa->priv2.spuq[i].mfc_cq_data3_RW =
  313. in_be64(&priv2->spuq[i].mfc_cq_data3_RW);
  314. }
  315. }
  316. }
  317. static inline void save_ppu_querymask(struct spu_state *csa, struct spu *spu)
  318. {
  319. struct spu_problem __iomem *prob = spu->problem;
  320. /* Save, Step 20:
  321. * Save the PPU_QueryMask register
  322. * in the CSA.
  323. */
  324. csa->prob.dma_querymask_RW = in_be32(&prob->dma_querymask_RW);
  325. }
  326. static inline void save_ppu_querytype(struct spu_state *csa, struct spu *spu)
  327. {
  328. struct spu_problem __iomem *prob = spu->problem;
  329. /* Save, Step 21:
  330. * Save the PPU_QueryType register
  331. * in the CSA.
  332. */
  333. csa->prob.dma_querytype_RW = in_be32(&prob->dma_querytype_RW);
  334. }
  335. static inline void save_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
  336. {
  337. struct spu_priv2 __iomem *priv2 = spu->priv2;
  338. /* Save, Step 22:
  339. * Save the MFC_CSR_TSQ register
  340. * in the LSCSA.
  341. */
  342. csa->priv2.spu_tag_status_query_RW =
  343. in_be64(&priv2->spu_tag_status_query_RW);
  344. }
  345. static inline void save_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
  346. {
  347. struct spu_priv2 __iomem *priv2 = spu->priv2;
  348. /* Save, Step 23:
  349. * Save the MFC_CSR_CMD1 and MFC_CSR_CMD2
  350. * registers in the CSA.
  351. */
  352. csa->priv2.spu_cmd_buf1_RW = in_be64(&priv2->spu_cmd_buf1_RW);
  353. csa->priv2.spu_cmd_buf2_RW = in_be64(&priv2->spu_cmd_buf2_RW);
  354. }
  355. static inline void save_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
  356. {
  357. struct spu_priv2 __iomem *priv2 = spu->priv2;
  358. /* Save, Step 24:
  359. * Save the MFC_CSR_ATO register in
  360. * the CSA.
  361. */
  362. csa->priv2.spu_atomic_status_RW = in_be64(&priv2->spu_atomic_status_RW);
  363. }
  364. static inline void save_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
  365. {
  366. struct spu_priv1 __iomem *priv1 = spu->priv1;
  367. /* Save, Step 25:
  368. * Save the MFC_TCLASS_ID register in
  369. * the CSA.
  370. */
  371. csa->priv1.mfc_tclass_id_RW = in_be64(&priv1->mfc_tclass_id_RW);
  372. }
  373. static inline void set_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
  374. {
  375. struct spu_priv1 __iomem *priv1 = spu->priv1;
  376. /* Save, Step 26:
  377. * Restore, Step 23.
  378. * Write the MFC_TCLASS_ID register with
  379. * the value 0x10000000.
  380. */
  381. out_be64(&priv1->mfc_tclass_id_RW, 0x10000000);
  382. eieio();
  383. }
  384. static inline void purge_mfc_queue(struct spu_state *csa, struct spu *spu)
  385. {
  386. struct spu_priv2 __iomem *priv2 = spu->priv2;
  387. /* Save, Step 27:
  388. * Restore, Step 14.
  389. * Write MFC_CNTL[Pc]=1 (purge queue).
  390. */
  391. out_be64(&priv2->mfc_control_RW, MFC_CNTL_PURGE_DMA_REQUEST);
  392. eieio();
  393. }
  394. static inline void wait_purge_complete(struct spu_state *csa, struct spu *spu)
  395. {
  396. struct spu_priv2 __iomem *priv2 = spu->priv2;
  397. /* Save, Step 28:
  398. * Poll MFC_CNTL[Ps] until value '11' is read
  399. * (purge complete).
  400. */
  401. POLL_WHILE_FALSE(in_be64(&priv2->mfc_control_RW) &
  402. MFC_CNTL_PURGE_DMA_COMPLETE);
  403. }
  404. static inline void save_mfc_slbs(struct spu_state *csa, struct spu *spu)
  405. {
  406. struct spu_priv1 __iomem *priv1 = spu->priv1;
  407. struct spu_priv2 __iomem *priv2 = spu->priv2;
  408. int i;
  409. /* Save, Step 29:
  410. * If MFC_SR1[R]='1', save SLBs in CSA.
  411. */
  412. if (in_be64(&priv1->mfc_sr1_RW) & MFC_STATE1_RELOCATE_MASK) {
  413. csa->priv2.slb_index_W = in_be64(&priv2->slb_index_W);
  414. for (i = 0; i < 8; i++) {
  415. out_be64(&priv2->slb_index_W, i);
  416. eieio();
  417. csa->slb_esid_RW[i] = in_be64(&priv2->slb_esid_RW);
  418. csa->slb_vsid_RW[i] = in_be64(&priv2->slb_vsid_RW);
  419. eieio();
  420. }
  421. }
  422. }
  423. static inline void setup_mfc_sr1(struct spu_state *csa, struct spu *spu)
  424. {
  425. struct spu_priv1 __iomem *priv1 = spu->priv1;
  426. /* Save, Step 30:
  427. * Restore, Step 18:
  428. * Write MFC_SR1 with MFC_SR1[D=0,S=1] and
  429. * MFC_SR1[TL,R,Pr,T] set correctly for the
  430. * OS specific environment.
  431. *
  432. * Implementation note: The SPU-side code
  433. * for save/restore is privileged, so the
  434. * MFC_SR1[Pr] bit is not set.
  435. *
  436. */
  437. out_be64(&priv1->mfc_sr1_RW, (MFC_STATE1_MASTER_RUN_CONTROL_MASK |
  438. MFC_STATE1_RELOCATE_MASK |
  439. MFC_STATE1_BUS_TLBIE_MASK));
  440. }
  441. static inline void save_spu_npc(struct spu_state *csa, struct spu *spu)
  442. {
  443. struct spu_problem __iomem *prob = spu->problem;
  444. /* Save, Step 31:
  445. * Save SPU_NPC in the CSA.
  446. */
  447. csa->prob.spu_npc_RW = in_be32(&prob->spu_npc_RW);
  448. }
  449. static inline void save_spu_privcntl(struct spu_state *csa, struct spu *spu)
  450. {
  451. struct spu_priv2 __iomem *priv2 = spu->priv2;
  452. /* Save, Step 32:
  453. * Save SPU_PrivCntl in the CSA.
  454. */
  455. csa->priv2.spu_privcntl_RW = in_be64(&priv2->spu_privcntl_RW);
  456. }
  457. static inline void reset_spu_privcntl(struct spu_state *csa, struct spu *spu)
  458. {
  459. struct spu_priv2 __iomem *priv2 = spu->priv2;
  460. /* Save, Step 33:
  461. * Restore, Step 16:
  462. * Write SPU_PrivCntl[S,Le,A] fields reset to 0.
  463. */
  464. out_be64(&priv2->spu_privcntl_RW, 0UL);
  465. eieio();
  466. }
  467. static inline void save_spu_lslr(struct spu_state *csa, struct spu *spu)
  468. {
  469. struct spu_priv2 __iomem *priv2 = spu->priv2;
  470. /* Save, Step 34:
  471. * Save SPU_LSLR in the CSA.
  472. */
  473. csa->priv2.spu_lslr_RW = in_be64(&priv2->spu_lslr_RW);
  474. }
  475. static inline void reset_spu_lslr(struct spu_state *csa, struct spu *spu)
  476. {
  477. struct spu_priv2 __iomem *priv2 = spu->priv2;
  478. /* Save, Step 35:
  479. * Restore, Step 17.
  480. * Reset SPU_LSLR.
  481. */
  482. out_be64(&priv2->spu_lslr_RW, LS_ADDR_MASK);
  483. eieio();
  484. }
  485. static inline void save_spu_cfg(struct spu_state *csa, struct spu *spu)
  486. {
  487. struct spu_priv2 __iomem *priv2 = spu->priv2;
  488. /* Save, Step 36:
  489. * Save SPU_Cfg in the CSA.
  490. */
  491. csa->priv2.spu_cfg_RW = in_be64(&priv2->spu_cfg_RW);
  492. }
  493. static inline void save_pm_trace(struct spu_state *csa, struct spu *spu)
  494. {
  495. /* Save, Step 37:
  496. * Save PM_Trace_Tag_Wait_Mask in the CSA.
  497. * Not performed by this implementation.
  498. */
  499. }
  500. static inline void save_mfc_rag(struct spu_state *csa, struct spu *spu)
  501. {
  502. struct spu_priv1 __iomem *priv1 = spu->priv1;
  503. /* Save, Step 38:
  504. * Save RA_GROUP_ID register and the
  505. * RA_ENABLE reigster in the CSA.
  506. */
  507. csa->priv1.resource_allocation_groupID_RW =
  508. in_be64(&priv1->resource_allocation_groupID_RW);
  509. csa->priv1.resource_allocation_enable_RW =
  510. in_be64(&priv1->resource_allocation_enable_RW);
  511. }
  512. static inline void save_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
  513. {
  514. struct spu_problem __iomem *prob = spu->problem;
  515. /* Save, Step 39:
  516. * Save MB_Stat register in the CSA.
  517. */
  518. csa->prob.mb_stat_R = in_be32(&prob->mb_stat_R);
  519. }
  520. static inline void save_ppu_mb(struct spu_state *csa, struct spu *spu)
  521. {
  522. struct spu_problem __iomem *prob = spu->problem;
  523. /* Save, Step 40:
  524. * Save the PPU_MB register in the CSA.
  525. */
  526. csa->prob.pu_mb_R = in_be32(&prob->pu_mb_R);
  527. }
  528. static inline void save_ppuint_mb(struct spu_state *csa, struct spu *spu)
  529. {
  530. struct spu_priv2 __iomem *priv2 = spu->priv2;
  531. /* Save, Step 41:
  532. * Save the PPUINT_MB register in the CSA.
  533. */
  534. csa->priv2.puint_mb_R = in_be64(&priv2->puint_mb_R);
  535. }
  536. static inline void save_ch_part1(struct spu_state *csa, struct spu *spu)
  537. {
  538. struct spu_priv2 __iomem *priv2 = spu->priv2;
  539. u64 idx, ch_indices[7] = { 0UL, 1UL, 3UL, 4UL, 24UL, 25UL, 27UL };
  540. int i;
  541. /* Save, Step 42:
  542. * Save the following CH: [0,1,3,4,24,25,27]
  543. */
  544. for (i = 0; i < 7; i++) {
  545. idx = ch_indices[i];
  546. out_be64(&priv2->spu_chnlcntptr_RW, idx);
  547. eieio();
  548. csa->spu_chnldata_RW[idx] = in_be64(&priv2->spu_chnldata_RW);
  549. csa->spu_chnlcnt_RW[idx] = in_be64(&priv2->spu_chnlcnt_RW);
  550. out_be64(&priv2->spu_chnldata_RW, 0UL);
  551. out_be64(&priv2->spu_chnlcnt_RW, 0UL);
  552. eieio();
  553. }
  554. }
  555. static inline void save_spu_mb(struct spu_state *csa, struct spu *spu)
  556. {
  557. struct spu_priv2 __iomem *priv2 = spu->priv2;
  558. int i;
  559. /* Save, Step 43:
  560. * Save SPU Read Mailbox Channel.
  561. */
  562. out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
  563. eieio();
  564. csa->spu_chnlcnt_RW[29] = in_be64(&priv2->spu_chnlcnt_RW);
  565. for (i = 0; i < 4; i++) {
  566. csa->spu_mailbox_data[i] = in_be64(&priv2->spu_chnldata_RW);
  567. }
  568. out_be64(&priv2->spu_chnlcnt_RW, 0UL);
  569. eieio();
  570. }
  571. static inline void save_mfc_cmd(struct spu_state *csa, struct spu *spu)
  572. {
  573. struct spu_priv2 __iomem *priv2 = spu->priv2;
  574. /* Save, Step 44:
  575. * Save MFC_CMD Channel.
  576. */
  577. out_be64(&priv2->spu_chnlcntptr_RW, 21UL);
  578. eieio();
  579. csa->spu_chnlcnt_RW[21] = in_be64(&priv2->spu_chnlcnt_RW);
  580. eieio();
  581. }
  582. static inline void reset_ch(struct spu_state *csa, struct spu *spu)
  583. {
  584. struct spu_priv2 __iomem *priv2 = spu->priv2;
  585. u64 ch_indices[4] = { 21UL, 23UL, 28UL, 30UL };
  586. u64 ch_counts[4] = { 16UL, 1UL, 1UL, 1UL };
  587. u64 idx;
  588. int i;
  589. /* Save, Step 45:
  590. * Reset the following CH: [21, 23, 28, 30]
  591. */
  592. for (i = 0; i < 4; i++) {
  593. idx = ch_indices[i];
  594. out_be64(&priv2->spu_chnlcntptr_RW, idx);
  595. eieio();
  596. out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
  597. eieio();
  598. }
  599. }
  600. static inline void resume_mfc_queue(struct spu_state *csa, struct spu *spu)
  601. {
  602. struct spu_priv2 __iomem *priv2 = spu->priv2;
  603. /* Save, Step 46:
  604. * Restore, Step 25.
  605. * Write MFC_CNTL[Sc]=0 (resume queue processing).
  606. */
  607. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESUME_DMA_QUEUE);
  608. }
  609. static inline void invalidate_slbs(struct spu_state *csa, struct spu *spu)
  610. {
  611. struct spu_priv1 __iomem *priv1 = spu->priv1;
  612. struct spu_priv2 __iomem *priv2 = spu->priv2;
  613. /* Save, Step 45:
  614. * Restore, Step 19:
  615. * If MFC_SR1[R]=1, write 0 to SLB_Invalidate_All.
  616. */
  617. if (in_be64(&priv1->mfc_sr1_RW) & MFC_STATE1_RELOCATE_MASK) {
  618. out_be64(&priv2->slb_invalidate_all_W, 0UL);
  619. eieio();
  620. }
  621. }
  622. static inline void get_kernel_slb(u64 ea, u64 slb[2])
  623. {
  624. slb[0] = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
  625. slb[1] = (ea & ESID_MASK) | SLB_ESID_V;
  626. /* Large pages are used for kernel text/data, but not vmalloc. */
  627. if (cpu_has_feature(CPU_FTR_16M_PAGE)
  628. && REGION_ID(ea) == KERNEL_REGION_ID)
  629. slb[0] |= SLB_VSID_L;
  630. }
  631. static inline void load_mfc_slb(struct spu *spu, u64 slb[2], int slbe)
  632. {
  633. struct spu_priv2 __iomem *priv2 = spu->priv2;
  634. out_be64(&priv2->slb_index_W, slbe);
  635. eieio();
  636. out_be64(&priv2->slb_vsid_RW, slb[0]);
  637. out_be64(&priv2->slb_esid_RW, slb[1]);
  638. eieio();
  639. }
  640. static inline void setup_mfc_slbs(struct spu_state *csa, struct spu *spu)
  641. {
  642. u64 code_slb[2];
  643. u64 lscsa_slb[2];
  644. /* Save, Step 47:
  645. * Restore, Step 30.
  646. * If MFC_SR1[R]=1, write 0 to SLB_Invalidate_All
  647. * register, then initialize SLB_VSID and SLB_ESID
  648. * to provide access to SPU context save code and
  649. * LSCSA.
  650. *
  651. * This implementation places both the context
  652. * switch code and LSCSA in kernel address space.
  653. *
  654. * Further this implementation assumes that the
  655. * MFC_SR1[R]=1 (in other words, assume that
  656. * translation is desired by OS environment).
  657. */
  658. invalidate_slbs(csa, spu);
  659. get_kernel_slb((unsigned long)&spu_save_code[0], code_slb);
  660. get_kernel_slb((unsigned long)csa->lscsa, lscsa_slb);
  661. load_mfc_slb(spu, code_slb, 0);
  662. if ((lscsa_slb[0] != code_slb[0]) || (lscsa_slb[1] != code_slb[1]))
  663. load_mfc_slb(spu, lscsa_slb, 1);
  664. }
  665. static inline void set_switch_active(struct spu_state *csa, struct spu *spu)
  666. {
  667. /* Save, Step 48:
  668. * Restore, Step 23.
  669. * Change the software context switch pending flag
  670. * to context switch active.
  671. */
  672. set_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags);
  673. clear_bit(SPU_CONTEXT_SWITCH_PENDING_nr, &spu->flags);
  674. mb();
  675. }
  676. static inline void enable_interrupts(struct spu_state *csa, struct spu *spu)
  677. {
  678. struct spu_priv1 __iomem *priv1 = spu->priv1;
  679. unsigned long class1_mask = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
  680. CLASS1_ENABLE_STORAGE_FAULT_INTR;
  681. /* Save, Step 49:
  682. * Restore, Step 22:
  683. * Reset and then enable interrupts, as
  684. * needed by OS.
  685. *
  686. * This implementation enables only class1
  687. * (translation) interrupts.
  688. */
  689. spin_lock_irq(&spu->register_lock);
  690. out_be64(&priv1->int_stat_class0_RW, ~(0UL));
  691. out_be64(&priv1->int_stat_class1_RW, ~(0UL));
  692. out_be64(&priv1->int_stat_class2_RW, ~(0UL));
  693. out_be64(&priv1->int_mask_class0_RW, 0UL);
  694. out_be64(&priv1->int_mask_class1_RW, class1_mask);
  695. out_be64(&priv1->int_mask_class2_RW, 0UL);
  696. spin_unlock_irq(&spu->register_lock);
  697. }
  698. static inline int send_mfc_dma(struct spu *spu, unsigned long ea,
  699. unsigned int ls_offset, unsigned int size,
  700. unsigned int tag, unsigned int rclass,
  701. unsigned int cmd)
  702. {
  703. struct spu_problem __iomem *prob = spu->problem;
  704. union mfc_tag_size_class_cmd command;
  705. unsigned int transfer_size;
  706. volatile unsigned int status = 0x0;
  707. while (size > 0) {
  708. transfer_size =
  709. (size > MFC_MAX_DMA_SIZE) ? MFC_MAX_DMA_SIZE : size;
  710. command.u.mfc_size = transfer_size;
  711. command.u.mfc_tag = tag;
  712. command.u.mfc_rclassid = rclass;
  713. command.u.mfc_cmd = cmd;
  714. do {
  715. out_be32(&prob->mfc_lsa_W, ls_offset);
  716. out_be64(&prob->mfc_ea_W, ea);
  717. out_be64(&prob->mfc_union_W.all64, command.all64);
  718. status =
  719. in_be32(&prob->mfc_union_W.by32.mfc_class_cmd32);
  720. if (unlikely(status & 0x2)) {
  721. cpu_relax();
  722. }
  723. } while (status & 0x3);
  724. size -= transfer_size;
  725. ea += transfer_size;
  726. ls_offset += transfer_size;
  727. }
  728. return 0;
  729. }
  730. static inline void save_ls_16kb(struct spu_state *csa, struct spu *spu)
  731. {
  732. unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
  733. unsigned int ls_offset = 0x0;
  734. unsigned int size = 16384;
  735. unsigned int tag = 0;
  736. unsigned int rclass = 0;
  737. unsigned int cmd = MFC_PUT_CMD;
  738. /* Save, Step 50:
  739. * Issue a DMA command to copy the first 16K bytes
  740. * of local storage to the CSA.
  741. */
  742. send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
  743. }
  744. static inline void set_spu_npc(struct spu_state *csa, struct spu *spu)
  745. {
  746. struct spu_problem __iomem *prob = spu->problem;
  747. /* Save, Step 51:
  748. * Restore, Step 31.
  749. * Write SPU_NPC[IE]=0 and SPU_NPC[LSA] to entry
  750. * point address of context save code in local
  751. * storage.
  752. *
  753. * This implementation uses SPU-side save/restore
  754. * programs with entry points at LSA of 0.
  755. */
  756. out_be32(&prob->spu_npc_RW, 0);
  757. eieio();
  758. }
  759. static inline void set_signot1(struct spu_state *csa, struct spu *spu)
  760. {
  761. struct spu_problem __iomem *prob = spu->problem;
  762. union {
  763. u64 ull;
  764. u32 ui[2];
  765. } addr64;
  766. /* Save, Step 52:
  767. * Restore, Step 32:
  768. * Write SPU_Sig_Notify_1 register with upper 32-bits
  769. * of the CSA.LSCSA effective address.
  770. */
  771. addr64.ull = (u64) csa->lscsa;
  772. out_be32(&prob->signal_notify1, addr64.ui[0]);
  773. eieio();
  774. }
  775. static inline void set_signot2(struct spu_state *csa, struct spu *spu)
  776. {
  777. struct spu_problem __iomem *prob = spu->problem;
  778. union {
  779. u64 ull;
  780. u32 ui[2];
  781. } addr64;
  782. /* Save, Step 53:
  783. * Restore, Step 33:
  784. * Write SPU_Sig_Notify_2 register with lower 32-bits
  785. * of the CSA.LSCSA effective address.
  786. */
  787. addr64.ull = (u64) csa->lscsa;
  788. out_be32(&prob->signal_notify2, addr64.ui[1]);
  789. eieio();
  790. }
  791. static inline void send_save_code(struct spu_state *csa, struct spu *spu)
  792. {
  793. unsigned long addr = (unsigned long)&spu_save_code[0];
  794. unsigned int ls_offset = 0x0;
  795. unsigned int size = sizeof(spu_save_code);
  796. unsigned int tag = 0;
  797. unsigned int rclass = 0;
  798. unsigned int cmd = MFC_GETFS_CMD;
  799. /* Save, Step 54:
  800. * Issue a DMA command to copy context save code
  801. * to local storage and start SPU.
  802. */
  803. send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
  804. }
  805. static inline void set_ppu_querymask(struct spu_state *csa, struct spu *spu)
  806. {
  807. struct spu_problem __iomem *prob = spu->problem;
  808. /* Save, Step 55:
  809. * Restore, Step 38.
  810. * Write PPU_QueryMask=1 (enable Tag Group 0)
  811. * and issue eieio instruction.
  812. */
  813. out_be32(&prob->dma_querymask_RW, MFC_TAGID_TO_TAGMASK(0));
  814. eieio();
  815. }
  816. static inline void wait_tag_complete(struct spu_state *csa, struct spu *spu)
  817. {
  818. struct spu_priv1 __iomem *priv1 = spu->priv1;
  819. struct spu_problem __iomem *prob = spu->problem;
  820. u32 mask = MFC_TAGID_TO_TAGMASK(0);
  821. unsigned long flags;
  822. /* Save, Step 56:
  823. * Restore, Step 39.
  824. * Restore, Step 39.
  825. * Restore, Step 46.
  826. * Poll PPU_TagStatus[gn] until 01 (Tag group 0 complete)
  827. * or write PPU_QueryType[TS]=01 and wait for Tag Group
  828. * Complete Interrupt. Write INT_Stat_Class0 or
  829. * INT_Stat_Class2 with value of 'handled'.
  830. */
  831. POLL_WHILE_FALSE(in_be32(&prob->dma_tagstatus_R) & mask);
  832. local_irq_save(flags);
  833. out_be64(&priv1->int_stat_class0_RW, ~(0UL));
  834. out_be64(&priv1->int_stat_class2_RW, ~(0UL));
  835. local_irq_restore(flags);
  836. }
  837. static inline void wait_spu_stopped(struct spu_state *csa, struct spu *spu)
  838. {
  839. struct spu_priv1 __iomem *priv1 = spu->priv1;
  840. struct spu_problem __iomem *prob = spu->problem;
  841. unsigned long flags;
  842. /* Save, Step 57:
  843. * Restore, Step 40.
  844. * Poll until SPU_Status[R]=0 or wait for SPU Class 0
  845. * or SPU Class 2 interrupt. Write INT_Stat_class0
  846. * or INT_Stat_class2 with value of handled.
  847. */
  848. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING);
  849. local_irq_save(flags);
  850. out_be64(&priv1->int_stat_class0_RW, ~(0UL));
  851. out_be64(&priv1->int_stat_class2_RW, ~(0UL));
  852. local_irq_restore(flags);
  853. }
  854. static inline int check_save_status(struct spu_state *csa, struct spu *spu)
  855. {
  856. struct spu_problem __iomem *prob = spu->problem;
  857. u32 complete;
  858. /* Save, Step 54:
  859. * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
  860. * context save succeeded, otherwise context save
  861. * failed.
  862. */
  863. complete = ((SPU_SAVE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
  864. SPU_STATUS_STOPPED_BY_STOP);
  865. return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
  866. }
  867. static inline void terminate_spu_app(struct spu_state *csa, struct spu *spu)
  868. {
  869. /* Restore, Step 4:
  870. * If required, notify the "using application" that
  871. * the SPU task has been terminated. TBD.
  872. */
  873. }
  874. static inline void suspend_mfc(struct spu_state *csa, struct spu *spu)
  875. {
  876. struct spu_priv2 __iomem *priv2 = spu->priv2;
  877. /* Restore, Step 7:
  878. * Restore, Step 47.
  879. * Write MFC_Cntl[Dh,Sc]='1','1' to suspend
  880. * the queue and halt the decrementer.
  881. */
  882. out_be64(&priv2->mfc_control_RW, MFC_CNTL_SUSPEND_DMA_QUEUE |
  883. MFC_CNTL_DECREMENTER_HALTED);
  884. eieio();
  885. }
  886. static inline void wait_suspend_mfc_complete(struct spu_state *csa,
  887. struct spu *spu)
  888. {
  889. struct spu_priv2 __iomem *priv2 = spu->priv2;
  890. /* Restore, Step 8:
  891. * Restore, Step 47.
  892. * Poll MFC_CNTL[Ss] until 11 is returned.
  893. */
  894. POLL_WHILE_FALSE(in_be64(&priv2->mfc_control_RW) &
  895. MFC_CNTL_SUSPEND_COMPLETE);
  896. }
  897. static inline int suspend_spe(struct spu_state *csa, struct spu *spu)
  898. {
  899. struct spu_problem __iomem *prob = spu->problem;
  900. /* Restore, Step 9:
  901. * If SPU_Status[R]=1, stop SPU execution
  902. * and wait for stop to complete.
  903. *
  904. * Returns 1 if SPU_Status[R]=1 on entry.
  905. * 0 otherwise
  906. */
  907. if (in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING) {
  908. if (in_be32(&prob->spu_status_R) &
  909. SPU_STATUS_ISOLATED_EXIT_STAUTUS) {
  910. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  911. SPU_STATUS_RUNNING);
  912. }
  913. if ((in_be32(&prob->spu_status_R) &
  914. SPU_STATUS_ISOLATED_LOAD_STAUTUS)
  915. || (in_be32(&prob->spu_status_R) &
  916. SPU_STATUS_ISOLATED_STATE)) {
  917. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
  918. eieio();
  919. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  920. SPU_STATUS_RUNNING);
  921. out_be32(&prob->spu_runcntl_RW, 0x2);
  922. eieio();
  923. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  924. SPU_STATUS_RUNNING);
  925. }
  926. if (in_be32(&prob->spu_status_R) &
  927. SPU_STATUS_WAITING_FOR_CHANNEL) {
  928. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
  929. eieio();
  930. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  931. SPU_STATUS_RUNNING);
  932. }
  933. return 1;
  934. }
  935. return 0;
  936. }
  937. static inline void clear_spu_status(struct spu_state *csa, struct spu *spu)
  938. {
  939. struct spu_problem __iomem *prob = spu->problem;
  940. struct spu_priv1 __iomem *priv1 = spu->priv1;
  941. /* Restore, Step 10:
  942. * If SPU_Status[R]=0 and SPU_Status[E,L,IS]=1,
  943. * release SPU from isolate state.
  944. */
  945. if (!(in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING)) {
  946. if (in_be32(&prob->spu_status_R) &
  947. SPU_STATUS_ISOLATED_EXIT_STAUTUS) {
  948. out_be64(&priv1->mfc_sr1_RW,
  949. MFC_STATE1_MASTER_RUN_CONTROL_MASK);
  950. eieio();
  951. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
  952. eieio();
  953. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  954. SPU_STATUS_RUNNING);
  955. }
  956. if ((in_be32(&prob->spu_status_R) &
  957. SPU_STATUS_ISOLATED_LOAD_STAUTUS)
  958. || (in_be32(&prob->spu_status_R) &
  959. SPU_STATUS_ISOLATED_STATE)) {
  960. out_be64(&priv1->mfc_sr1_RW,
  961. MFC_STATE1_MASTER_RUN_CONTROL_MASK);
  962. eieio();
  963. out_be32(&prob->spu_runcntl_RW, 0x2);
  964. eieio();
  965. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  966. SPU_STATUS_RUNNING);
  967. }
  968. }
  969. }
  970. static inline void reset_ch_part1(struct spu_state *csa, struct spu *spu)
  971. {
  972. struct spu_priv2 __iomem *priv2 = spu->priv2;
  973. u64 ch_indices[7] = { 0UL, 1UL, 3UL, 4UL, 24UL, 25UL, 27UL };
  974. u64 idx;
  975. int i;
  976. /* Restore, Step 20:
  977. * Reset the following CH: [0,1,3,4,24,25,27]
  978. */
  979. for (i = 0; i < 7; i++) {
  980. idx = ch_indices[i];
  981. out_be64(&priv2->spu_chnlcntptr_RW, idx);
  982. eieio();
  983. out_be64(&priv2->spu_chnldata_RW, 0UL);
  984. out_be64(&priv2->spu_chnlcnt_RW, 0UL);
  985. eieio();
  986. }
  987. }
  988. static inline void reset_ch_part2(struct spu_state *csa, struct spu *spu)
  989. {
  990. struct spu_priv2 __iomem *priv2 = spu->priv2;
  991. u64 ch_indices[5] = { 21UL, 23UL, 28UL, 29UL, 30UL };
  992. u64 ch_counts[5] = { 16UL, 1UL, 1UL, 0UL, 1UL };
  993. u64 idx;
  994. int i;
  995. /* Restore, Step 21:
  996. * Reset the following CH: [21, 23, 28, 29, 30]
  997. */
  998. for (i = 0; i < 5; i++) {
  999. idx = ch_indices[i];
  1000. out_be64(&priv2->spu_chnlcntptr_RW, idx);
  1001. eieio();
  1002. out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
  1003. eieio();
  1004. }
  1005. }
  1006. static inline void setup_spu_status_part1(struct spu_state *csa,
  1007. struct spu *spu)
  1008. {
  1009. u32 status_P = SPU_STATUS_STOPPED_BY_STOP;
  1010. u32 status_I = SPU_STATUS_INVALID_INSTR;
  1011. u32 status_H = SPU_STATUS_STOPPED_BY_HALT;
  1012. u32 status_S = SPU_STATUS_SINGLE_STEP;
  1013. u32 status_S_I = SPU_STATUS_SINGLE_STEP | SPU_STATUS_INVALID_INSTR;
  1014. u32 status_S_P = SPU_STATUS_SINGLE_STEP | SPU_STATUS_STOPPED_BY_STOP;
  1015. u32 status_P_H = SPU_STATUS_STOPPED_BY_HALT |SPU_STATUS_STOPPED_BY_STOP;
  1016. u32 status_P_I = SPU_STATUS_STOPPED_BY_STOP |SPU_STATUS_INVALID_INSTR;
  1017. u32 status_code;
  1018. /* Restore, Step 27:
  1019. * If the CSA.SPU_Status[I,S,H,P]=1 then add the correct
  1020. * instruction sequence to the end of the SPU based restore
  1021. * code (after the "context restored" stop and signal) to
  1022. * restore the correct SPU status.
  1023. *
  1024. * NOTE: Rather than modifying the SPU executable, we
  1025. * instead add a new 'stopped_status' field to the
  1026. * LSCSA. The SPU-side restore reads this field and
  1027. * takes the appropriate action when exiting.
  1028. */
  1029. status_code =
  1030. (csa->prob.spu_status_R >> SPU_STOP_STATUS_SHIFT) & 0xFFFF;
  1031. if ((csa->prob.spu_status_R & status_P_I) == status_P_I) {
  1032. /* SPU_Status[P,I]=1 - Illegal Instruction followed
  1033. * by Stop and Signal instruction, followed by 'br -4'.
  1034. *
  1035. */
  1036. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_I;
  1037. csa->lscsa->stopped_status.slot[1] = status_code;
  1038. } else if ((csa->prob.spu_status_R & status_P_H) == status_P_H) {
  1039. /* SPU_Status[P,H]=1 - Halt Conditional, followed
  1040. * by Stop and Signal instruction, followed by
  1041. * 'br -4'.
  1042. */
  1043. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P_H;
  1044. csa->lscsa->stopped_status.slot[1] = status_code;
  1045. } else if ((csa->prob.spu_status_R & status_S_P) == status_S_P) {
  1046. /* SPU_Status[S,P]=1 - Stop and Signal instruction
  1047. * followed by 'br -4'.
  1048. */
  1049. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_P;
  1050. csa->lscsa->stopped_status.slot[1] = status_code;
  1051. } else if ((csa->prob.spu_status_R & status_S_I) == status_S_I) {
  1052. /* SPU_Status[S,I]=1 - Illegal instruction followed
  1053. * by 'br -4'.
  1054. */
  1055. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S_I;
  1056. csa->lscsa->stopped_status.slot[1] = status_code;
  1057. } else if ((csa->prob.spu_status_R & status_P) == status_P) {
  1058. /* SPU_Status[P]=1 - Stop and Signal instruction
  1059. * followed by 'br -4'.
  1060. */
  1061. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_P;
  1062. csa->lscsa->stopped_status.slot[1] = status_code;
  1063. } else if ((csa->prob.spu_status_R & status_H) == status_H) {
  1064. /* SPU_Status[H]=1 - Halt Conditional, followed
  1065. * by 'br -4'.
  1066. */
  1067. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_H;
  1068. } else if ((csa->prob.spu_status_R & status_S) == status_S) {
  1069. /* SPU_Status[S]=1 - Two nop instructions.
  1070. */
  1071. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_S;
  1072. } else if ((csa->prob.spu_status_R & status_I) == status_I) {
  1073. /* SPU_Status[I]=1 - Illegal instruction followed
  1074. * by 'br -4'.
  1075. */
  1076. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_I;
  1077. }
  1078. }
  1079. static inline void setup_spu_status_part2(struct spu_state *csa,
  1080. struct spu *spu)
  1081. {
  1082. u32 mask;
  1083. /* Restore, Step 28:
  1084. * If the CSA.SPU_Status[I,S,H,P,R]=0 then
  1085. * add a 'br *' instruction to the end of
  1086. * the SPU based restore code.
  1087. *
  1088. * NOTE: Rather than modifying the SPU executable, we
  1089. * instead add a new 'stopped_status' field to the
  1090. * LSCSA. The SPU-side restore reads this field and
  1091. * takes the appropriate action when exiting.
  1092. */
  1093. mask = SPU_STATUS_INVALID_INSTR |
  1094. SPU_STATUS_SINGLE_STEP |
  1095. SPU_STATUS_STOPPED_BY_HALT |
  1096. SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
  1097. if (!(csa->prob.spu_status_R & mask)) {
  1098. csa->lscsa->stopped_status.slot[0] = SPU_STOPPED_STATUS_R;
  1099. }
  1100. }
  1101. static inline void restore_mfc_rag(struct spu_state *csa, struct spu *spu)
  1102. {
  1103. struct spu_priv1 __iomem *priv1 = spu->priv1;
  1104. /* Restore, Step 29:
  1105. * Restore RA_GROUP_ID register and the
  1106. * RA_ENABLE reigster from the CSA.
  1107. */
  1108. out_be64(&priv1->resource_allocation_groupID_RW,
  1109. csa->priv1.resource_allocation_groupID_RW);
  1110. out_be64(&priv1->resource_allocation_enable_RW,
  1111. csa->priv1.resource_allocation_enable_RW);
  1112. }
  1113. static inline void send_restore_code(struct spu_state *csa, struct spu *spu)
  1114. {
  1115. unsigned long addr = (unsigned long)&spu_restore_code[0];
  1116. unsigned int ls_offset = 0x0;
  1117. unsigned int size = sizeof(spu_restore_code);
  1118. unsigned int tag = 0;
  1119. unsigned int rclass = 0;
  1120. unsigned int cmd = MFC_GETFS_CMD;
  1121. /* Restore, Step 37:
  1122. * Issue MFC DMA command to copy context
  1123. * restore code to local storage.
  1124. */
  1125. send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
  1126. }
  1127. static inline void setup_decr(struct spu_state *csa, struct spu *spu)
  1128. {
  1129. /* Restore, Step 34:
  1130. * If CSA.MFC_CNTL[Ds]=1 (decrementer was
  1131. * running) then adjust decrementer, set
  1132. * decrementer running status in LSCSA,
  1133. * and set decrementer "wrapped" status
  1134. * in LSCSA.
  1135. */
  1136. if (csa->priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING) {
  1137. cycles_t resume_time = get_cycles();
  1138. cycles_t delta_time = resume_time - csa->suspend_time;
  1139. csa->lscsa->decr.slot[0] = delta_time;
  1140. }
  1141. }
  1142. static inline void setup_ppu_mb(struct spu_state *csa, struct spu *spu)
  1143. {
  1144. /* Restore, Step 35:
  1145. * Copy the CSA.PU_MB data into the LSCSA.
  1146. */
  1147. csa->lscsa->ppu_mb.slot[0] = csa->prob.pu_mb_R;
  1148. }
  1149. static inline void setup_ppuint_mb(struct spu_state *csa, struct spu *spu)
  1150. {
  1151. /* Restore, Step 36:
  1152. * Copy the CSA.PUINT_MB data into the LSCSA.
  1153. */
  1154. csa->lscsa->ppuint_mb.slot[0] = csa->priv2.puint_mb_R;
  1155. }
  1156. static inline int check_restore_status(struct spu_state *csa, struct spu *spu)
  1157. {
  1158. struct spu_problem __iomem *prob = spu->problem;
  1159. u32 complete;
  1160. /* Restore, Step 40:
  1161. * If SPU_Status[P]=1 and SPU_Status[SC] = "success",
  1162. * context restore succeeded, otherwise context restore
  1163. * failed.
  1164. */
  1165. complete = ((SPU_RESTORE_COMPLETE << SPU_STOP_STATUS_SHIFT) |
  1166. SPU_STATUS_STOPPED_BY_STOP);
  1167. return (in_be32(&prob->spu_status_R) != complete) ? 1 : 0;
  1168. }
  1169. static inline void restore_spu_privcntl(struct spu_state *csa, struct spu *spu)
  1170. {
  1171. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1172. /* Restore, Step 41:
  1173. * Restore SPU_PrivCntl from the CSA.
  1174. */
  1175. out_be64(&priv2->spu_privcntl_RW, csa->priv2.spu_privcntl_RW);
  1176. eieio();
  1177. }
  1178. static inline void restore_status_part1(struct spu_state *csa, struct spu *spu)
  1179. {
  1180. struct spu_problem __iomem *prob = spu->problem;
  1181. u32 mask;
  1182. /* Restore, Step 42:
  1183. * If any CSA.SPU_Status[I,S,H,P]=1, then
  1184. * restore the error or single step state.
  1185. */
  1186. mask = SPU_STATUS_INVALID_INSTR |
  1187. SPU_STATUS_SINGLE_STEP |
  1188. SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
  1189. if (csa->prob.spu_status_R & mask) {
  1190. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
  1191. eieio();
  1192. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  1193. SPU_STATUS_RUNNING);
  1194. }
  1195. }
  1196. static inline void restore_status_part2(struct spu_state *csa, struct spu *spu)
  1197. {
  1198. struct spu_problem __iomem *prob = spu->problem;
  1199. u32 mask;
  1200. /* Restore, Step 43:
  1201. * If all CSA.SPU_Status[I,S,H,P,R]=0 then write
  1202. * SPU_RunCntl[R0R1]='01', wait for SPU_Status[R]=1,
  1203. * then write '00' to SPU_RunCntl[R0R1] and wait
  1204. * for SPU_Status[R]=0.
  1205. */
  1206. mask = SPU_STATUS_INVALID_INSTR |
  1207. SPU_STATUS_SINGLE_STEP |
  1208. SPU_STATUS_STOPPED_BY_HALT |
  1209. SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_RUNNING;
  1210. if (!(csa->prob.spu_status_R & mask)) {
  1211. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
  1212. eieio();
  1213. POLL_WHILE_FALSE(in_be32(&prob->spu_status_R) &
  1214. SPU_STATUS_RUNNING);
  1215. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
  1216. eieio();
  1217. POLL_WHILE_TRUE(in_be32(&prob->spu_status_R) &
  1218. SPU_STATUS_RUNNING);
  1219. }
  1220. }
  1221. static inline void restore_ls_16kb(struct spu_state *csa, struct spu *spu)
  1222. {
  1223. unsigned long addr = (unsigned long)&csa->lscsa->ls[0];
  1224. unsigned int ls_offset = 0x0;
  1225. unsigned int size = 16384;
  1226. unsigned int tag = 0;
  1227. unsigned int rclass = 0;
  1228. unsigned int cmd = MFC_GET_CMD;
  1229. /* Restore, Step 44:
  1230. * Issue a DMA command to restore the first
  1231. * 16kb of local storage from CSA.
  1232. */
  1233. send_mfc_dma(spu, addr, ls_offset, size, tag, rclass, cmd);
  1234. }
  1235. static inline void clear_interrupts(struct spu_state *csa, struct spu *spu)
  1236. {
  1237. struct spu_priv1 __iomem *priv1 = spu->priv1;
  1238. /* Restore, Step 49:
  1239. * Write INT_MASK_class0 with value of 0.
  1240. * Write INT_MASK_class1 with value of 0.
  1241. * Write INT_MASK_class2 with value of 0.
  1242. * Write INT_STAT_class0 with value of -1.
  1243. * Write INT_STAT_class1 with value of -1.
  1244. * Write INT_STAT_class2 with value of -1.
  1245. */
  1246. spin_lock_irq(&spu->register_lock);
  1247. out_be64(&priv1->int_mask_class0_RW, 0UL);
  1248. out_be64(&priv1->int_mask_class1_RW, 0UL);
  1249. out_be64(&priv1->int_mask_class2_RW, 0UL);
  1250. out_be64(&priv1->int_stat_class0_RW, ~(0UL));
  1251. out_be64(&priv1->int_stat_class1_RW, ~(0UL));
  1252. out_be64(&priv1->int_stat_class2_RW, ~(0UL));
  1253. spin_unlock_irq(&spu->register_lock);
  1254. }
  1255. static inline void restore_mfc_queues(struct spu_state *csa, struct spu *spu)
  1256. {
  1257. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1258. int i;
  1259. /* Restore, Step 50:
  1260. * If MFC_Cntl[Se]!=0 then restore
  1261. * MFC command queues.
  1262. */
  1263. if ((csa->priv2.mfc_control_RW & MFC_CNTL_DMA_QUEUES_EMPTY_MASK) == 0) {
  1264. for (i = 0; i < 8; i++) {
  1265. out_be64(&priv2->puq[i].mfc_cq_data0_RW,
  1266. csa->priv2.puq[i].mfc_cq_data0_RW);
  1267. out_be64(&priv2->puq[i].mfc_cq_data1_RW,
  1268. csa->priv2.puq[i].mfc_cq_data1_RW);
  1269. out_be64(&priv2->puq[i].mfc_cq_data2_RW,
  1270. csa->priv2.puq[i].mfc_cq_data2_RW);
  1271. out_be64(&priv2->puq[i].mfc_cq_data3_RW,
  1272. csa->priv2.puq[i].mfc_cq_data3_RW);
  1273. }
  1274. for (i = 0; i < 16; i++) {
  1275. out_be64(&priv2->spuq[i].mfc_cq_data0_RW,
  1276. csa->priv2.spuq[i].mfc_cq_data0_RW);
  1277. out_be64(&priv2->spuq[i].mfc_cq_data1_RW,
  1278. csa->priv2.spuq[i].mfc_cq_data1_RW);
  1279. out_be64(&priv2->spuq[i].mfc_cq_data2_RW,
  1280. csa->priv2.spuq[i].mfc_cq_data2_RW);
  1281. out_be64(&priv2->spuq[i].mfc_cq_data3_RW,
  1282. csa->priv2.spuq[i].mfc_cq_data3_RW);
  1283. }
  1284. }
  1285. eieio();
  1286. }
  1287. static inline void restore_ppu_querymask(struct spu_state *csa, struct spu *spu)
  1288. {
  1289. struct spu_problem __iomem *prob = spu->problem;
  1290. /* Restore, Step 51:
  1291. * Restore the PPU_QueryMask register from CSA.
  1292. */
  1293. out_be32(&prob->dma_querymask_RW, csa->prob.dma_querymask_RW);
  1294. eieio();
  1295. }
  1296. static inline void restore_ppu_querytype(struct spu_state *csa, struct spu *spu)
  1297. {
  1298. struct spu_problem __iomem *prob = spu->problem;
  1299. /* Restore, Step 52:
  1300. * Restore the PPU_QueryType register from CSA.
  1301. */
  1302. out_be32(&prob->dma_querytype_RW, csa->prob.dma_querytype_RW);
  1303. eieio();
  1304. }
  1305. static inline void restore_mfc_csr_tsq(struct spu_state *csa, struct spu *spu)
  1306. {
  1307. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1308. /* Restore, Step 53:
  1309. * Restore the MFC_CSR_TSQ register from CSA.
  1310. */
  1311. out_be64(&priv2->spu_tag_status_query_RW,
  1312. csa->priv2.spu_tag_status_query_RW);
  1313. eieio();
  1314. }
  1315. static inline void restore_mfc_csr_cmd(struct spu_state *csa, struct spu *spu)
  1316. {
  1317. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1318. /* Restore, Step 54:
  1319. * Restore the MFC_CSR_CMD1 and MFC_CSR_CMD2
  1320. * registers from CSA.
  1321. */
  1322. out_be64(&priv2->spu_cmd_buf1_RW, csa->priv2.spu_cmd_buf1_RW);
  1323. out_be64(&priv2->spu_cmd_buf2_RW, csa->priv2.spu_cmd_buf2_RW);
  1324. eieio();
  1325. }
  1326. static inline void restore_mfc_csr_ato(struct spu_state *csa, struct spu *spu)
  1327. {
  1328. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1329. /* Restore, Step 55:
  1330. * Restore the MFC_CSR_ATO register from CSA.
  1331. */
  1332. out_be64(&priv2->spu_atomic_status_RW, csa->priv2.spu_atomic_status_RW);
  1333. }
  1334. static inline void restore_mfc_tclass_id(struct spu_state *csa, struct spu *spu)
  1335. {
  1336. struct spu_priv1 __iomem *priv1 = spu->priv1;
  1337. /* Restore, Step 56:
  1338. * Restore the MFC_TCLASS_ID register from CSA.
  1339. */
  1340. out_be64(&priv1->mfc_tclass_id_RW, csa->priv1.mfc_tclass_id_RW);
  1341. eieio();
  1342. }
  1343. static inline void set_llr_event(struct spu_state *csa, struct spu *spu)
  1344. {
  1345. u64 ch0_cnt, ch0_data;
  1346. u64 ch1_data;
  1347. /* Restore, Step 57:
  1348. * Set the Lock Line Reservation Lost Event by:
  1349. * 1. OR CSA.SPU_Event_Status with bit 21 (Lr) set to 1.
  1350. * 2. If CSA.SPU_Channel_0_Count=0 and
  1351. * CSA.SPU_Wr_Event_Mask[Lr]=1 and
  1352. * CSA.SPU_Event_Status[Lr]=0 then set
  1353. * CSA.SPU_Event_Status_Count=1.
  1354. */
  1355. ch0_cnt = csa->spu_chnlcnt_RW[0];
  1356. ch0_data = csa->spu_chnldata_RW[0];
  1357. ch1_data = csa->spu_chnldata_RW[1];
  1358. csa->spu_chnldata_RW[0] |= MFC_LLR_LOST_EVENT;
  1359. if ((ch0_cnt == 0) && !(ch0_data & MFC_LLR_LOST_EVENT) &&
  1360. (ch1_data & MFC_LLR_LOST_EVENT)) {
  1361. csa->spu_chnlcnt_RW[0] = 1;
  1362. }
  1363. }
  1364. static inline void restore_decr_wrapped(struct spu_state *csa, struct spu *spu)
  1365. {
  1366. /* Restore, Step 58:
  1367. * If the status of the CSA software decrementer
  1368. * "wrapped" flag is set, OR in a '1' to
  1369. * CSA.SPU_Event_Status[Tm].
  1370. */
  1371. if (csa->lscsa->decr_status.slot[0] == 1) {
  1372. csa->spu_chnldata_RW[0] |= 0x20;
  1373. }
  1374. if ((csa->lscsa->decr_status.slot[0] == 1) &&
  1375. (csa->spu_chnlcnt_RW[0] == 0 &&
  1376. ((csa->spu_chnldata_RW[2] & 0x20) == 0x0) &&
  1377. ((csa->spu_chnldata_RW[0] & 0x20) != 0x1))) {
  1378. csa->spu_chnlcnt_RW[0] = 1;
  1379. }
  1380. }
  1381. static inline void restore_ch_part1(struct spu_state *csa, struct spu *spu)
  1382. {
  1383. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1384. u64 idx, ch_indices[7] = { 0UL, 1UL, 3UL, 4UL, 24UL, 25UL, 27UL };
  1385. int i;
  1386. /* Restore, Step 59:
  1387. * Restore the following CH: [0,1,3,4,24,25,27]
  1388. */
  1389. for (i = 0; i < 7; i++) {
  1390. idx = ch_indices[i];
  1391. out_be64(&priv2->spu_chnlcntptr_RW, idx);
  1392. eieio();
  1393. out_be64(&priv2->spu_chnldata_RW, csa->spu_chnldata_RW[idx]);
  1394. out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[idx]);
  1395. eieio();
  1396. }
  1397. }
  1398. static inline void restore_ch_part2(struct spu_state *csa, struct spu *spu)
  1399. {
  1400. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1401. u64 ch_indices[3] = { 9UL, 21UL, 23UL };
  1402. u64 ch_counts[3] = { 1UL, 16UL, 1UL };
  1403. u64 idx;
  1404. int i;
  1405. /* Restore, Step 60:
  1406. * Restore the following CH: [9,21,23].
  1407. */
  1408. ch_counts[0] = 1UL;
  1409. ch_counts[1] = csa->spu_chnlcnt_RW[21];
  1410. ch_counts[2] = 1UL;
  1411. for (i = 0; i < 3; i++) {
  1412. idx = ch_indices[i];
  1413. out_be64(&priv2->spu_chnlcntptr_RW, idx);
  1414. eieio();
  1415. out_be64(&priv2->spu_chnlcnt_RW, ch_counts[i]);
  1416. eieio();
  1417. }
  1418. }
  1419. static inline void restore_spu_lslr(struct spu_state *csa, struct spu *spu)
  1420. {
  1421. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1422. /* Restore, Step 61:
  1423. * Restore the SPU_LSLR register from CSA.
  1424. */
  1425. out_be64(&priv2->spu_lslr_RW, csa->priv2.spu_lslr_RW);
  1426. eieio();
  1427. }
  1428. static inline void restore_spu_cfg(struct spu_state *csa, struct spu *spu)
  1429. {
  1430. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1431. /* Restore, Step 62:
  1432. * Restore the SPU_Cfg register from CSA.
  1433. */
  1434. out_be64(&priv2->spu_cfg_RW, csa->priv2.spu_cfg_RW);
  1435. eieio();
  1436. }
  1437. static inline void restore_pm_trace(struct spu_state *csa, struct spu *spu)
  1438. {
  1439. /* Restore, Step 63:
  1440. * Restore PM_Trace_Tag_Wait_Mask from CSA.
  1441. * Not performed by this implementation.
  1442. */
  1443. }
  1444. static inline void restore_spu_npc(struct spu_state *csa, struct spu *spu)
  1445. {
  1446. struct spu_problem __iomem *prob = spu->problem;
  1447. /* Restore, Step 64:
  1448. * Restore SPU_NPC from CSA.
  1449. */
  1450. out_be32(&prob->spu_npc_RW, csa->prob.spu_npc_RW);
  1451. eieio();
  1452. }
  1453. static inline void restore_spu_mb(struct spu_state *csa, struct spu *spu)
  1454. {
  1455. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1456. int i;
  1457. /* Restore, Step 65:
  1458. * Restore MFC_RdSPU_MB from CSA.
  1459. */
  1460. out_be64(&priv2->spu_chnlcntptr_RW, 29UL);
  1461. eieio();
  1462. out_be64(&priv2->spu_chnlcnt_RW, csa->spu_chnlcnt_RW[29]);
  1463. for (i = 0; i < 4; i++) {
  1464. out_be64(&priv2->spu_chnldata_RW, csa->spu_mailbox_data[i]);
  1465. }
  1466. eieio();
  1467. }
  1468. static inline void check_ppu_mb_stat(struct spu_state *csa, struct spu *spu)
  1469. {
  1470. struct spu_problem __iomem *prob = spu->problem;
  1471. u32 dummy = 0;
  1472. /* Restore, Step 66:
  1473. * If CSA.MB_Stat[P]=0 (mailbox empty) then
  1474. * read from the PPU_MB register.
  1475. */
  1476. if ((csa->prob.mb_stat_R & 0xFF) == 0) {
  1477. dummy = in_be32(&prob->pu_mb_R);
  1478. eieio();
  1479. }
  1480. }
  1481. static inline void check_ppuint_mb_stat(struct spu_state *csa, struct spu *spu)
  1482. {
  1483. struct spu_priv1 __iomem *priv1 = spu->priv1;
  1484. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1485. u64 dummy = 0UL;
  1486. /* Restore, Step 66:
  1487. * If CSA.MB_Stat[I]=0 (mailbox empty) then
  1488. * read from the PPUINT_MB register.
  1489. */
  1490. if ((csa->prob.mb_stat_R & 0xFF0000) == 0) {
  1491. dummy = in_be64(&priv2->puint_mb_R);
  1492. eieio();
  1493. out_be64(&priv1->int_stat_class2_RW,
  1494. CLASS2_ENABLE_MAILBOX_INTR);
  1495. eieio();
  1496. }
  1497. }
  1498. static inline void restore_mfc_slbs(struct spu_state *csa, struct spu *spu)
  1499. {
  1500. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1501. int i;
  1502. /* Restore, Step 68:
  1503. * If MFC_SR1[R]='1', restore SLBs from CSA.
  1504. */
  1505. if (csa->priv1.mfc_sr1_RW & MFC_STATE1_RELOCATE_MASK) {
  1506. for (i = 0; i < 8; i++) {
  1507. out_be64(&priv2->slb_index_W, i);
  1508. eieio();
  1509. out_be64(&priv2->slb_esid_RW, csa->slb_esid_RW[i]);
  1510. out_be64(&priv2->slb_vsid_RW, csa->slb_vsid_RW[i]);
  1511. eieio();
  1512. }
  1513. out_be64(&priv2->slb_index_W, csa->priv2.slb_index_W);
  1514. eieio();
  1515. }
  1516. }
  1517. static inline void restore_mfc_sr1(struct spu_state *csa, struct spu *spu)
  1518. {
  1519. struct spu_priv1 __iomem *priv1 = spu->priv1;
  1520. /* Restore, Step 69:
  1521. * Restore the MFC_SR1 register from CSA.
  1522. */
  1523. out_be64(&priv1->mfc_sr1_RW, csa->priv1.mfc_sr1_RW);
  1524. eieio();
  1525. }
  1526. static inline void restore_other_spu_access(struct spu_state *csa,
  1527. struct spu *spu)
  1528. {
  1529. /* Restore, Step 70:
  1530. * Restore other SPU mappings to this SPU. TBD.
  1531. */
  1532. }
  1533. static inline void restore_spu_runcntl(struct spu_state *csa, struct spu *spu)
  1534. {
  1535. struct spu_problem __iomem *prob = spu->problem;
  1536. /* Restore, Step 71:
  1537. * If CSA.SPU_Status[R]=1 then write
  1538. * SPU_RunCntl[R0R1]='01'.
  1539. */
  1540. if (csa->prob.spu_status_R & SPU_STATUS_RUNNING) {
  1541. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
  1542. eieio();
  1543. }
  1544. }
  1545. static inline void restore_mfc_cntl(struct spu_state *csa, struct spu *spu)
  1546. {
  1547. struct spu_priv2 __iomem *priv2 = spu->priv2;
  1548. /* Restore, Step 72:
  1549. * Restore the MFC_CNTL register for the CSA.
  1550. */
  1551. out_be64(&priv2->mfc_control_RW, csa->priv2.mfc_control_RW);
  1552. eieio();
  1553. }
  1554. static inline void enable_user_access(struct spu_state *csa, struct spu *spu)
  1555. {
  1556. /* Restore, Step 73:
  1557. * Enable user-space access (if provided) to this
  1558. * SPU by mapping the virtual pages assigned to
  1559. * the SPU memory-mapped I/O (MMIO) for problem
  1560. * state. TBD.
  1561. */
  1562. }
  1563. static inline void reset_switch_active(struct spu_state *csa, struct spu *spu)
  1564. {
  1565. /* Restore, Step 74:
  1566. * Reset the "context switch active" flag.
  1567. */
  1568. clear_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags);
  1569. mb();
  1570. }
  1571. static inline void reenable_interrupts(struct spu_state *csa, struct spu *spu)
  1572. {
  1573. struct spu_priv1 __iomem *priv1 = spu->priv1;
  1574. /* Restore, Step 75:
  1575. * Re-enable SPU interrupts.
  1576. */
  1577. spin_lock_irq(&spu->register_lock);
  1578. out_be64(&priv1->int_mask_class0_RW, csa->priv1.int_mask_class0_RW);
  1579. out_be64(&priv1->int_mask_class1_RW, csa->priv1.int_mask_class1_RW);
  1580. out_be64(&priv1->int_mask_class2_RW, csa->priv1.int_mask_class2_RW);
  1581. spin_unlock_irq(&spu->register_lock);
  1582. }
  1583. static int quiece_spu(struct spu_state *prev, struct spu *spu)
  1584. {
  1585. /*
  1586. * Combined steps 2-18 of SPU context save sequence, which
  1587. * quiesce the SPU state (disable SPU execution, MFC command
  1588. * queues, decrementer, SPU interrupts, etc.).
  1589. *
  1590. * Returns 0 on success.
  1591. * 2 if failed step 2.
  1592. * 6 if failed step 6.
  1593. */
  1594. if (check_spu_isolate(prev, spu)) { /* Step 2. */
  1595. return 2;
  1596. }
  1597. disable_interrupts(prev, spu); /* Step 3. */
  1598. set_watchdog_timer(prev, spu); /* Step 4. */
  1599. inhibit_user_access(prev, spu); /* Step 5. */
  1600. if (check_spu_isolate(prev, spu)) { /* Step 6. */
  1601. return 6;
  1602. }
  1603. set_switch_pending(prev, spu); /* Step 7. */
  1604. save_mfc_cntl(prev, spu); /* Step 8. */
  1605. save_spu_runcntl(prev, spu); /* Step 9. */
  1606. save_mfc_sr1(prev, spu); /* Step 10. */
  1607. save_spu_status(prev, spu); /* Step 11. */
  1608. save_mfc_decr(prev, spu); /* Step 12. */
  1609. halt_mfc_decr(prev, spu); /* Step 13. */
  1610. save_timebase(prev, spu); /* Step 14. */
  1611. remove_other_spu_access(prev, spu); /* Step 15. */
  1612. do_mfc_mssync(prev, spu); /* Step 16. */
  1613. issue_mfc_tlbie(prev, spu); /* Step 17. */
  1614. handle_pending_interrupts(prev, spu); /* Step 18. */
  1615. return 0;
  1616. }
  1617. static void save_csa(struct spu_state *prev, struct spu *spu)
  1618. {
  1619. /*
  1620. * Combine steps 19-44 of SPU context save sequence, which
  1621. * save regions of the privileged & problem state areas.
  1622. */
  1623. save_mfc_queues(prev, spu); /* Step 19. */
  1624. save_ppu_querymask(prev, spu); /* Step 20. */
  1625. save_ppu_querytype(prev, spu); /* Step 21. */
  1626. save_mfc_csr_tsq(prev, spu); /* Step 22. */
  1627. save_mfc_csr_cmd(prev, spu); /* Step 23. */
  1628. save_mfc_csr_ato(prev, spu); /* Step 24. */
  1629. save_mfc_tclass_id(prev, spu); /* Step 25. */
  1630. set_mfc_tclass_id(prev, spu); /* Step 26. */
  1631. purge_mfc_queue(prev, spu); /* Step 27. */
  1632. wait_purge_complete(prev, spu); /* Step 28. */
  1633. save_mfc_slbs(prev, spu); /* Step 29. */
  1634. setup_mfc_sr1(prev, spu); /* Step 30. */
  1635. save_spu_npc(prev, spu); /* Step 31. */
  1636. save_spu_privcntl(prev, spu); /* Step 32. */
  1637. reset_spu_privcntl(prev, spu); /* Step 33. */
  1638. save_spu_lslr(prev, spu); /* Step 34. */
  1639. reset_spu_lslr(prev, spu); /* Step 35. */
  1640. save_spu_cfg(prev, spu); /* Step 36. */
  1641. save_pm_trace(prev, spu); /* Step 37. */
  1642. save_mfc_rag(prev, spu); /* Step 38. */
  1643. save_ppu_mb_stat(prev, spu); /* Step 39. */
  1644. save_ppu_mb(prev, spu); /* Step 40. */
  1645. save_ppuint_mb(prev, spu); /* Step 41. */
  1646. save_ch_part1(prev, spu); /* Step 42. */
  1647. save_spu_mb(prev, spu); /* Step 43. */
  1648. save_mfc_cmd(prev, spu); /* Step 44. */
  1649. reset_ch(prev, spu); /* Step 45. */
  1650. }
  1651. static void save_lscsa(struct spu_state *prev, struct spu *spu)
  1652. {
  1653. /*
  1654. * Perform steps 46-57 of SPU context save sequence,
  1655. * which save regions of the local store and register
  1656. * file.
  1657. */
  1658. resume_mfc_queue(prev, spu); /* Step 46. */
  1659. setup_mfc_slbs(prev, spu); /* Step 47. */
  1660. set_switch_active(prev, spu); /* Step 48. */
  1661. enable_interrupts(prev, spu); /* Step 49. */
  1662. save_ls_16kb(prev, spu); /* Step 50. */
  1663. set_spu_npc(prev, spu); /* Step 51. */
  1664. set_signot1(prev, spu); /* Step 52. */
  1665. set_signot2(prev, spu); /* Step 53. */
  1666. send_save_code(prev, spu); /* Step 54. */
  1667. set_ppu_querymask(prev, spu); /* Step 55. */
  1668. wait_tag_complete(prev, spu); /* Step 56. */
  1669. wait_spu_stopped(prev, spu); /* Step 57. */
  1670. }
  1671. static void harvest(struct spu_state *prev, struct spu *spu)
  1672. {
  1673. /*
  1674. * Perform steps 2-25 of SPU context restore sequence,
  1675. * which resets an SPU either after a failed save, or
  1676. * when using SPU for first time.
  1677. */
  1678. disable_interrupts(prev, spu); /* Step 2. */
  1679. inhibit_user_access(prev, spu); /* Step 3. */
  1680. terminate_spu_app(prev, spu); /* Step 4. */
  1681. set_switch_pending(prev, spu); /* Step 5. */
  1682. remove_other_spu_access(prev, spu); /* Step 6. */
  1683. suspend_mfc(prev, spu); /* Step 7. */
  1684. wait_suspend_mfc_complete(prev, spu); /* Step 8. */
  1685. if (!suspend_spe(prev, spu)) /* Step 9. */
  1686. clear_spu_status(prev, spu); /* Step 10. */
  1687. do_mfc_mssync(prev, spu); /* Step 11. */
  1688. issue_mfc_tlbie(prev, spu); /* Step 12. */
  1689. handle_pending_interrupts(prev, spu); /* Step 13. */
  1690. purge_mfc_queue(prev, spu); /* Step 14. */
  1691. wait_purge_complete(prev, spu); /* Step 15. */
  1692. reset_spu_privcntl(prev, spu); /* Step 16. */
  1693. reset_spu_lslr(prev, spu); /* Step 17. */
  1694. setup_mfc_sr1(prev, spu); /* Step 18. */
  1695. invalidate_slbs(prev, spu); /* Step 19. */
  1696. reset_ch_part1(prev, spu); /* Step 20. */
  1697. reset_ch_part2(prev, spu); /* Step 21. */
  1698. enable_interrupts(prev, spu); /* Step 22. */
  1699. set_switch_active(prev, spu); /* Step 23. */
  1700. set_mfc_tclass_id(prev, spu); /* Step 24. */
  1701. resume_mfc_queue(prev, spu); /* Step 25. */
  1702. }
  1703. static void restore_lscsa(struct spu_state *next, struct spu *spu)
  1704. {
  1705. /*
  1706. * Perform steps 26-40 of SPU context restore sequence,
  1707. * which restores regions of the local store and register
  1708. * file.
  1709. */
  1710. set_watchdog_timer(next, spu); /* Step 26. */
  1711. setup_spu_status_part1(next, spu); /* Step 27. */
  1712. setup_spu_status_part2(next, spu); /* Step 28. */
  1713. restore_mfc_rag(next, spu); /* Step 29. */
  1714. setup_mfc_slbs(next, spu); /* Step 30. */
  1715. set_spu_npc(next, spu); /* Step 31. */
  1716. set_signot1(next, spu); /* Step 32. */
  1717. set_signot2(next, spu); /* Step 33. */
  1718. setup_decr(next, spu); /* Step 34. */
  1719. setup_ppu_mb(next, spu); /* Step 35. */
  1720. setup_ppuint_mb(next, spu); /* Step 36. */
  1721. send_restore_code(next, spu); /* Step 37. */
  1722. set_ppu_querymask(next, spu); /* Step 38. */
  1723. wait_tag_complete(next, spu); /* Step 39. */
  1724. wait_spu_stopped(next, spu); /* Step 40. */
  1725. }
  1726. static void restore_csa(struct spu_state *next, struct spu *spu)
  1727. {
  1728. /*
  1729. * Combine steps 41-76 of SPU context restore sequence, which
  1730. * restore regions of the privileged & problem state areas.
  1731. */
  1732. restore_spu_privcntl(next, spu); /* Step 41. */
  1733. restore_status_part1(next, spu); /* Step 42. */
  1734. restore_status_part2(next, spu); /* Step 43. */
  1735. restore_ls_16kb(next, spu); /* Step 44. */
  1736. wait_tag_complete(next, spu); /* Step 45. */
  1737. suspend_mfc(next, spu); /* Step 46. */
  1738. wait_suspend_mfc_complete(next, spu); /* Step 47. */
  1739. issue_mfc_tlbie(next, spu); /* Step 48. */
  1740. clear_interrupts(next, spu); /* Step 49. */
  1741. restore_mfc_queues(next, spu); /* Step 50. */
  1742. restore_ppu_querymask(next, spu); /* Step 51. */
  1743. restore_ppu_querytype(next, spu); /* Step 52. */
  1744. restore_mfc_csr_tsq(next, spu); /* Step 53. */
  1745. restore_mfc_csr_cmd(next, spu); /* Step 54. */
  1746. restore_mfc_csr_ato(next, spu); /* Step 55. */
  1747. restore_mfc_tclass_id(next, spu); /* Step 56. */
  1748. set_llr_event(next, spu); /* Step 57. */
  1749. restore_decr_wrapped(next, spu); /* Step 58. */
  1750. restore_ch_part1(next, spu); /* Step 59. */
  1751. restore_ch_part2(next, spu); /* Step 60. */
  1752. restore_spu_lslr(next, spu); /* Step 61. */
  1753. restore_spu_cfg(next, spu); /* Step 62. */
  1754. restore_pm_trace(next, spu); /* Step 63. */
  1755. restore_spu_npc(next, spu); /* Step 64. */
  1756. restore_spu_mb(next, spu); /* Step 65. */
  1757. check_ppu_mb_stat(next, spu); /* Step 66. */
  1758. check_ppuint_mb_stat(next, spu); /* Step 67. */
  1759. restore_mfc_slbs(next, spu); /* Step 68. */
  1760. restore_mfc_sr1(next, spu); /* Step 69. */
  1761. restore_other_spu_access(next, spu); /* Step 70. */
  1762. restore_spu_runcntl(next, spu); /* Step 71. */
  1763. restore_mfc_cntl(next, spu); /* Step 72. */
  1764. enable_user_access(next, spu); /* Step 73. */
  1765. reset_switch_active(next, spu); /* Step 74. */
  1766. reenable_interrupts(next, spu); /* Step 75. */
  1767. }
  1768. static int __do_spu_save(struct spu_state *prev, struct spu *spu)
  1769. {
  1770. int rc;
  1771. /*
  1772. * SPU context save can be broken into three phases:
  1773. *
  1774. * (a) quiesce [steps 2-16].
  1775. * (b) save of CSA, performed by PPE [steps 17-42]
  1776. * (c) save of LSCSA, mostly performed by SPU [steps 43-52].
  1777. *
  1778. * Returns 0 on success.
  1779. * 2,6 if failed to quiece SPU
  1780. * 53 if SPU-side of save failed.
  1781. */
  1782. rc = quiece_spu(prev, spu); /* Steps 2-16. */
  1783. switch (rc) {
  1784. default:
  1785. case 2:
  1786. case 6:
  1787. harvest(prev, spu);
  1788. return rc;
  1789. break;
  1790. case 0:
  1791. break;
  1792. }
  1793. save_csa(prev, spu); /* Steps 17-43. */
  1794. save_lscsa(prev, spu); /* Steps 44-53. */
  1795. return check_save_status(prev, spu); /* Step 54. */
  1796. }
  1797. static int __do_spu_restore(struct spu_state *next, struct spu *spu)
  1798. {
  1799. int rc;
  1800. /*
  1801. * SPU context restore can be broken into three phases:
  1802. *
  1803. * (a) harvest (or reset) SPU [steps 2-24].
  1804. * (b) restore LSCSA [steps 25-40], mostly performed by SPU.
  1805. * (c) restore CSA [steps 41-76], performed by PPE.
  1806. *
  1807. * The 'harvest' step is not performed here, but rather
  1808. * as needed below.
  1809. */
  1810. restore_lscsa(next, spu); /* Steps 24-39. */
  1811. rc = check_restore_status(next, spu); /* Step 40. */
  1812. switch (rc) {
  1813. default:
  1814. /* Failed. Return now. */
  1815. return rc;
  1816. break;
  1817. case 0:
  1818. /* Fall through to next step. */
  1819. break;
  1820. }
  1821. restore_csa(next, spu);
  1822. return 0;
  1823. }
  1824. /**
  1825. * spu_save - SPU context save, with locking.
  1826. * @prev: pointer to SPU context save area, to be saved.
  1827. * @spu: pointer to SPU iomem structure.
  1828. *
  1829. * Acquire locks, perform the save operation then return.
  1830. */
  1831. int spu_save(struct spu_state *prev, struct spu *spu)
  1832. {
  1833. int rc;
  1834. acquire_spu_lock(spu); /* Step 1. */
  1835. rc = __do_spu_save(prev, spu); /* Steps 2-53. */
  1836. release_spu_lock(spu);
  1837. if (rc) {
  1838. panic("%s failed on SPU[%d], rc=%d.\n",
  1839. __func__, spu->number, rc);
  1840. }
  1841. return rc;
  1842. }
  1843. /**
  1844. * spu_restore - SPU context restore, with harvest and locking.
  1845. * @new: pointer to SPU context save area, to be restored.
  1846. * @spu: pointer to SPU iomem structure.
  1847. *
  1848. * Perform harvest + restore, as we may not be coming
  1849. * from a previous succesful save operation, and the
  1850. * hardware state is unknown.
  1851. */
  1852. int spu_restore(struct spu_state *new, struct spu *spu)
  1853. {
  1854. int rc;
  1855. acquire_spu_lock(spu);
  1856. harvest(NULL, spu);
  1857. spu->stop_code = 0;
  1858. spu->dar = 0;
  1859. spu->dsisr = 0;
  1860. spu->slb_replace = 0;
  1861. spu->class_0_pending = 0;
  1862. rc = __do_spu_restore(new, spu);
  1863. release_spu_lock(spu);
  1864. if (rc) {
  1865. panic("%s failed on SPU[%d] rc=%d.\n",
  1866. __func__, spu->number, rc);
  1867. }
  1868. return rc;
  1869. }
  1870. /**
  1871. * spu_harvest - SPU harvest (reset) operation
  1872. * @spu: pointer to SPU iomem structure.
  1873. *
  1874. * Perform SPU harvest (reset) operation.
  1875. */
  1876. void spu_harvest(struct spu *spu)
  1877. {
  1878. acquire_spu_lock(spu);
  1879. harvest(NULL, spu);
  1880. release_spu_lock(spu);
  1881. }
  1882. static void init_prob(struct spu_state *csa)
  1883. {
  1884. csa->spu_chnlcnt_RW[9] = 1;
  1885. csa->spu_chnlcnt_RW[21] = 16;
  1886. csa->spu_chnlcnt_RW[23] = 1;
  1887. csa->spu_chnlcnt_RW[28] = 1;
  1888. csa->spu_chnlcnt_RW[30] = 1;
  1889. csa->prob.spu_runcntl_RW = SPU_RUNCNTL_STOP;
  1890. }
  1891. static void init_priv1(struct spu_state *csa)
  1892. {
  1893. /* Enable decode, relocate, tlbie response, master runcntl. */
  1894. csa->priv1.mfc_sr1_RW = MFC_STATE1_LOCAL_STORAGE_DECODE_MASK |
  1895. MFC_STATE1_MASTER_RUN_CONTROL_MASK |
  1896. MFC_STATE1_PROBLEM_STATE_MASK |
  1897. MFC_STATE1_RELOCATE_MASK | MFC_STATE1_BUS_TLBIE_MASK;
  1898. /* Set storage description. */
  1899. csa->priv1.mfc_sdr_RW = mfspr(SPRN_SDR1);
  1900. /* Enable OS-specific set of interrupts. */
  1901. csa->priv1.int_mask_class0_RW = CLASS0_ENABLE_DMA_ALIGNMENT_INTR |
  1902. CLASS0_ENABLE_INVALID_DMA_COMMAND_INTR |
  1903. CLASS0_ENABLE_SPU_ERROR_INTR;
  1904. csa->priv1.int_mask_class1_RW = CLASS1_ENABLE_SEGMENT_FAULT_INTR |
  1905. CLASS1_ENABLE_STORAGE_FAULT_INTR;
  1906. csa->priv1.int_mask_class2_RW = CLASS2_ENABLE_SPU_STOP_INTR |
  1907. CLASS2_ENABLE_SPU_HALT_INTR;
  1908. }
  1909. static void init_priv2(struct spu_state *csa)
  1910. {
  1911. csa->priv2.spu_lslr_RW = LS_ADDR_MASK;
  1912. csa->priv2.mfc_control_RW = MFC_CNTL_RESUME_DMA_QUEUE |
  1913. MFC_CNTL_NORMAL_DMA_QUEUE_OPERATION |
  1914. MFC_CNTL_DMA_QUEUES_EMPTY_MASK;
  1915. }
  1916. /**
  1917. * spu_alloc_csa - allocate and initialize an SPU context save area.
  1918. *
  1919. * Allocate and initialize the contents of an SPU context save area.
  1920. * This includes enabling address translation, interrupt masks, etc.,
  1921. * as appropriate for the given OS environment.
  1922. *
  1923. * Note that storage for the 'lscsa' is allocated separately,
  1924. * as it is by far the largest of the context save regions,
  1925. * and may need to be pinned or otherwise specially aligned.
  1926. */
  1927. void spu_init_csa(struct spu_state *csa)
  1928. {
  1929. struct spu_lscsa *lscsa;
  1930. unsigned char *p;
  1931. if (!csa)
  1932. return;
  1933. memset(csa, 0, sizeof(struct spu_state));
  1934. lscsa = vmalloc(sizeof(struct spu_lscsa));
  1935. if (!lscsa)
  1936. return;
  1937. memset(lscsa, 0, sizeof(struct spu_lscsa));
  1938. csa->lscsa = lscsa;
  1939. csa->register_lock = SPIN_LOCK_UNLOCKED;
  1940. /* Set LS pages reserved to allow for user-space mapping. */
  1941. for (p = lscsa->ls; p < lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  1942. SetPageReserved(vmalloc_to_page(p));
  1943. init_prob(csa);
  1944. init_priv1(csa);
  1945. init_priv2(csa);
  1946. }
  1947. void spu_fini_csa(struct spu_state *csa)
  1948. {
  1949. /* Clear reserved bit before vfree. */
  1950. unsigned char *p;
  1951. for (p = csa->lscsa->ls; p < csa->lscsa->ls + LS_SIZE; p += PAGE_SIZE)
  1952. ClearPageReserved(vmalloc_to_page(p));
  1953. vfree(csa->lscsa);
  1954. }