switch.c 62 KB

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