backing_ops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* backing_ops.c - query/set operations on saved SPU context.
  2. *
  3. * Copyright (C) IBM 2005
  4. * Author: Mark Nutter <mnutter@us.ibm.com>
  5. *
  6. * These register operations allow SPUFS to operate on saved
  7. * SPU contexts rather than hardware.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/sched.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/smp.h>
  29. #include <linux/stddef.h>
  30. #include <linux/unistd.h>
  31. #include <linux/poll.h>
  32. #include <asm/io.h>
  33. #include <asm/spu.h>
  34. #include <asm/spu_csa.h>
  35. #include <asm/spu_info.h>
  36. #include <asm/mmu_context.h>
  37. #include "spufs.h"
  38. /*
  39. * Reads/writes to various problem and priv2 registers require
  40. * state changes, i.e. generate SPU events, modify channel
  41. * counts, etc.
  42. */
  43. static void gen_spu_event(struct spu_context *ctx, u32 event)
  44. {
  45. u64 ch0_cnt;
  46. u64 ch0_data;
  47. u64 ch1_data;
  48. ch0_cnt = ctx->csa.spu_chnlcnt_RW[0];
  49. ch0_data = ctx->csa.spu_chnldata_RW[0];
  50. ch1_data = ctx->csa.spu_chnldata_RW[1];
  51. ctx->csa.spu_chnldata_RW[0] |= event;
  52. if ((ch0_cnt == 0) && !(ch0_data & event) && (ch1_data & event)) {
  53. ctx->csa.spu_chnlcnt_RW[0] = 1;
  54. }
  55. }
  56. static int spu_backing_mbox_read(struct spu_context *ctx, u32 * data)
  57. {
  58. u32 mbox_stat;
  59. int ret = 0;
  60. spin_lock(&ctx->csa.register_lock);
  61. mbox_stat = ctx->csa.prob.mb_stat_R;
  62. if (mbox_stat & 0x0000ff) {
  63. /* Read the first available word.
  64. * Implementation note: the depth
  65. * of pu_mb_R is currently 1.
  66. */
  67. *data = ctx->csa.prob.pu_mb_R;
  68. ctx->csa.prob.mb_stat_R &= ~(0x0000ff);
  69. ctx->csa.spu_chnlcnt_RW[28] = 1;
  70. gen_spu_event(ctx, MFC_PU_MAILBOX_AVAILABLE_EVENT);
  71. ret = 4;
  72. }
  73. spin_unlock(&ctx->csa.register_lock);
  74. return ret;
  75. }
  76. static u32 spu_backing_mbox_stat_read(struct spu_context *ctx)
  77. {
  78. return ctx->csa.prob.mb_stat_R;
  79. }
  80. static unsigned int spu_backing_mbox_stat_poll(struct spu_context *ctx,
  81. unsigned int events)
  82. {
  83. int ret;
  84. u32 stat;
  85. ret = 0;
  86. spin_lock_irq(&ctx->csa.register_lock);
  87. stat = ctx->csa.prob.mb_stat_R;
  88. /* if the requested event is there, return the poll
  89. mask, otherwise enable the interrupt to get notified,
  90. but first mark any pending interrupts as done so
  91. we don't get woken up unnecessarily */
  92. if (events & (POLLIN | POLLRDNORM)) {
  93. if (stat & 0xff0000)
  94. ret |= POLLIN | POLLRDNORM;
  95. else {
  96. ctx->csa.priv1.int_stat_class2_RW &=
  97. ~CLASS2_MAILBOX_INTR;
  98. ctx->csa.priv1.int_mask_class2_RW |=
  99. CLASS2_ENABLE_MAILBOX_INTR;
  100. }
  101. }
  102. if (events & (POLLOUT | POLLWRNORM)) {
  103. if (stat & 0x00ff00)
  104. ret = POLLOUT | POLLWRNORM;
  105. else {
  106. ctx->csa.priv1.int_stat_class2_RW &=
  107. ~CLASS2_MAILBOX_THRESHOLD_INTR;
  108. ctx->csa.priv1.int_mask_class2_RW |=
  109. CLASS2_ENABLE_MAILBOX_THRESHOLD_INTR;
  110. }
  111. }
  112. spin_unlock_irq(&ctx->csa.register_lock);
  113. return ret;
  114. }
  115. static int spu_backing_ibox_read(struct spu_context *ctx, u32 * data)
  116. {
  117. int ret;
  118. spin_lock(&ctx->csa.register_lock);
  119. if (ctx->csa.prob.mb_stat_R & 0xff0000) {
  120. /* Read the first available word.
  121. * Implementation note: the depth
  122. * of puint_mb_R is currently 1.
  123. */
  124. *data = ctx->csa.priv2.puint_mb_R;
  125. ctx->csa.prob.mb_stat_R &= ~(0xff0000);
  126. ctx->csa.spu_chnlcnt_RW[30] = 1;
  127. gen_spu_event(ctx, MFC_PU_INT_MAILBOX_AVAILABLE_EVENT);
  128. ret = 4;
  129. } else {
  130. /* make sure we get woken up by the interrupt */
  131. ctx->csa.priv1.int_mask_class2_RW |= CLASS2_ENABLE_MAILBOX_INTR;
  132. ret = 0;
  133. }
  134. spin_unlock(&ctx->csa.register_lock);
  135. return ret;
  136. }
  137. static int spu_backing_wbox_write(struct spu_context *ctx, u32 data)
  138. {
  139. int ret;
  140. spin_lock(&ctx->csa.register_lock);
  141. if ((ctx->csa.prob.mb_stat_R) & 0x00ff00) {
  142. int slot = ctx->csa.spu_chnlcnt_RW[29];
  143. int avail = (ctx->csa.prob.mb_stat_R & 0x00ff00) >> 8;
  144. /* We have space to write wbox_data.
  145. * Implementation note: the depth
  146. * of spu_mb_W is currently 4.
  147. */
  148. BUG_ON(avail != (4 - slot));
  149. ctx->csa.spu_mailbox_data[slot] = data;
  150. ctx->csa.spu_chnlcnt_RW[29] = ++slot;
  151. ctx->csa.prob.mb_stat_R &= ~(0x00ff00);
  152. ctx->csa.prob.mb_stat_R |= (((4 - slot) & 0xff) << 8);
  153. gen_spu_event(ctx, MFC_SPU_MAILBOX_WRITTEN_EVENT);
  154. ret = 4;
  155. } else {
  156. /* make sure we get woken up by the interrupt when space
  157. becomes available */
  158. ctx->csa.priv1.int_mask_class2_RW |=
  159. CLASS2_ENABLE_MAILBOX_THRESHOLD_INTR;
  160. ret = 0;
  161. }
  162. spin_unlock(&ctx->csa.register_lock);
  163. return ret;
  164. }
  165. static u32 spu_backing_signal1_read(struct spu_context *ctx)
  166. {
  167. return ctx->csa.spu_chnldata_RW[3];
  168. }
  169. static void spu_backing_signal1_write(struct spu_context *ctx, u32 data)
  170. {
  171. spin_lock(&ctx->csa.register_lock);
  172. if (ctx->csa.priv2.spu_cfg_RW & 0x1)
  173. ctx->csa.spu_chnldata_RW[3] |= data;
  174. else
  175. ctx->csa.spu_chnldata_RW[3] = data;
  176. ctx->csa.spu_chnlcnt_RW[3] = 1;
  177. gen_spu_event(ctx, MFC_SIGNAL_1_EVENT);
  178. spin_unlock(&ctx->csa.register_lock);
  179. }
  180. static u32 spu_backing_signal2_read(struct spu_context *ctx)
  181. {
  182. return ctx->csa.spu_chnldata_RW[4];
  183. }
  184. static void spu_backing_signal2_write(struct spu_context *ctx, u32 data)
  185. {
  186. spin_lock(&ctx->csa.register_lock);
  187. if (ctx->csa.priv2.spu_cfg_RW & 0x2)
  188. ctx->csa.spu_chnldata_RW[4] |= data;
  189. else
  190. ctx->csa.spu_chnldata_RW[4] = data;
  191. ctx->csa.spu_chnlcnt_RW[4] = 1;
  192. gen_spu_event(ctx, MFC_SIGNAL_2_EVENT);
  193. spin_unlock(&ctx->csa.register_lock);
  194. }
  195. static void spu_backing_signal1_type_set(struct spu_context *ctx, u64 val)
  196. {
  197. u64 tmp;
  198. spin_lock(&ctx->csa.register_lock);
  199. tmp = ctx->csa.priv2.spu_cfg_RW;
  200. if (val)
  201. tmp |= 1;
  202. else
  203. tmp &= ~1;
  204. ctx->csa.priv2.spu_cfg_RW = tmp;
  205. spin_unlock(&ctx->csa.register_lock);
  206. }
  207. static u64 spu_backing_signal1_type_get(struct spu_context *ctx)
  208. {
  209. return ((ctx->csa.priv2.spu_cfg_RW & 1) != 0);
  210. }
  211. static void spu_backing_signal2_type_set(struct spu_context *ctx, u64 val)
  212. {
  213. u64 tmp;
  214. spin_lock(&ctx->csa.register_lock);
  215. tmp = ctx->csa.priv2.spu_cfg_RW;
  216. if (val)
  217. tmp |= 2;
  218. else
  219. tmp &= ~2;
  220. ctx->csa.priv2.spu_cfg_RW = tmp;
  221. spin_unlock(&ctx->csa.register_lock);
  222. }
  223. static u64 spu_backing_signal2_type_get(struct spu_context *ctx)
  224. {
  225. return ((ctx->csa.priv2.spu_cfg_RW & 2) != 0);
  226. }
  227. static u32 spu_backing_npc_read(struct spu_context *ctx)
  228. {
  229. return ctx->csa.prob.spu_npc_RW;
  230. }
  231. static void spu_backing_npc_write(struct spu_context *ctx, u32 val)
  232. {
  233. ctx->csa.prob.spu_npc_RW = val;
  234. }
  235. static u32 spu_backing_status_read(struct spu_context *ctx)
  236. {
  237. return ctx->csa.prob.spu_status_R;
  238. }
  239. static char *spu_backing_get_ls(struct spu_context *ctx)
  240. {
  241. return ctx->csa.lscsa->ls;
  242. }
  243. static void spu_backing_privcntl_write(struct spu_context *ctx, u64 val)
  244. {
  245. ctx->csa.priv2.spu_privcntl_RW = val;
  246. }
  247. static u32 spu_backing_runcntl_read(struct spu_context *ctx)
  248. {
  249. return ctx->csa.prob.spu_runcntl_RW;
  250. }
  251. static void spu_backing_runcntl_write(struct spu_context *ctx, u32 val)
  252. {
  253. spin_lock(&ctx->csa.register_lock);
  254. ctx->csa.prob.spu_runcntl_RW = val;
  255. if (val & SPU_RUNCNTL_RUNNABLE) {
  256. ctx->csa.prob.spu_status_R &=
  257. ~SPU_STATUS_STOPPED_BY_STOP &
  258. ~SPU_STATUS_STOPPED_BY_HALT &
  259. ~SPU_STATUS_SINGLE_STEP &
  260. ~SPU_STATUS_INVALID_INSTR &
  261. ~SPU_STATUS_INVALID_CH;
  262. ctx->csa.prob.spu_status_R |= SPU_STATUS_RUNNING;
  263. } else {
  264. ctx->csa.prob.spu_status_R &= ~SPU_STATUS_RUNNING;
  265. }
  266. spin_unlock(&ctx->csa.register_lock);
  267. }
  268. static void spu_backing_runcntl_stop(struct spu_context *ctx)
  269. {
  270. spu_backing_runcntl_write(ctx, SPU_RUNCNTL_STOP);
  271. }
  272. static void spu_backing_master_start(struct spu_context *ctx)
  273. {
  274. struct spu_state *csa = &ctx->csa;
  275. u64 sr1;
  276. spin_lock(&csa->register_lock);
  277. sr1 = csa->priv1.mfc_sr1_RW | MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  278. csa->priv1.mfc_sr1_RW = sr1;
  279. spin_unlock(&csa->register_lock);
  280. }
  281. static void spu_backing_master_stop(struct spu_context *ctx)
  282. {
  283. struct spu_state *csa = &ctx->csa;
  284. u64 sr1;
  285. spin_lock(&csa->register_lock);
  286. sr1 = csa->priv1.mfc_sr1_RW & ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  287. csa->priv1.mfc_sr1_RW = sr1;
  288. spin_unlock(&csa->register_lock);
  289. }
  290. static int spu_backing_set_mfc_query(struct spu_context * ctx, u32 mask,
  291. u32 mode)
  292. {
  293. struct spu_problem_collapsed *prob = &ctx->csa.prob;
  294. int ret;
  295. spin_lock(&ctx->csa.register_lock);
  296. ret = -EAGAIN;
  297. if (prob->dma_querytype_RW)
  298. goto out;
  299. ret = 0;
  300. /* FIXME: what are the side-effects of this? */
  301. prob->dma_querymask_RW = mask;
  302. prob->dma_querytype_RW = mode;
  303. /* In the current implementation, the SPU context is always
  304. * acquired in runnable state when new bits are added to the
  305. * mask (tagwait), so it's sufficient just to mask
  306. * dma_tagstatus_R with the 'mask' parameter here.
  307. */
  308. ctx->csa.prob.dma_tagstatus_R &= mask;
  309. out:
  310. spin_unlock(&ctx->csa.register_lock);
  311. return ret;
  312. }
  313. static u32 spu_backing_read_mfc_tagstatus(struct spu_context * ctx)
  314. {
  315. return ctx->csa.prob.dma_tagstatus_R;
  316. }
  317. static u32 spu_backing_get_mfc_free_elements(struct spu_context *ctx)
  318. {
  319. return ctx->csa.prob.dma_qstatus_R;
  320. }
  321. static int spu_backing_send_mfc_command(struct spu_context *ctx,
  322. struct mfc_dma_command *cmd)
  323. {
  324. int ret;
  325. spin_lock(&ctx->csa.register_lock);
  326. ret = -EAGAIN;
  327. /* FIXME: set up priv2->puq */
  328. spin_unlock(&ctx->csa.register_lock);
  329. return ret;
  330. }
  331. static void spu_backing_restart_dma(struct spu_context *ctx)
  332. {
  333. ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_RESTART_DMA_COMMAND;
  334. }
  335. struct spu_context_ops spu_backing_ops = {
  336. .mbox_read = spu_backing_mbox_read,
  337. .mbox_stat_read = spu_backing_mbox_stat_read,
  338. .mbox_stat_poll = spu_backing_mbox_stat_poll,
  339. .ibox_read = spu_backing_ibox_read,
  340. .wbox_write = spu_backing_wbox_write,
  341. .signal1_read = spu_backing_signal1_read,
  342. .signal1_write = spu_backing_signal1_write,
  343. .signal2_read = spu_backing_signal2_read,
  344. .signal2_write = spu_backing_signal2_write,
  345. .signal1_type_set = spu_backing_signal1_type_set,
  346. .signal1_type_get = spu_backing_signal1_type_get,
  347. .signal2_type_set = spu_backing_signal2_type_set,
  348. .signal2_type_get = spu_backing_signal2_type_get,
  349. .npc_read = spu_backing_npc_read,
  350. .npc_write = spu_backing_npc_write,
  351. .status_read = spu_backing_status_read,
  352. .get_ls = spu_backing_get_ls,
  353. .privcntl_write = spu_backing_privcntl_write,
  354. .runcntl_read = spu_backing_runcntl_read,
  355. .runcntl_write = spu_backing_runcntl_write,
  356. .runcntl_stop = spu_backing_runcntl_stop,
  357. .master_start = spu_backing_master_start,
  358. .master_stop = spu_backing_master_stop,
  359. .set_mfc_query = spu_backing_set_mfc_query,
  360. .read_mfc_tagstatus = spu_backing_read_mfc_tagstatus,
  361. .get_mfc_free_elements = spu_backing_get_mfc_free_elements,
  362. .send_mfc_command = spu_backing_send_mfc_command,
  363. .restart_dma = spu_backing_restart_dma,
  364. };