switch.c 62 KB

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