switch.c 62 KB

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