switch.c 63 KB

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