switch.c 61 KB

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