switch.c 64 KB

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