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