switch.c 63 KB

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