switch.c 62 KB

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