switch.c 63 KB

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