switch.c 63 KB

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