switch.c 62 KB

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