switch.c 63 KB

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