switch.c 63 KB

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