file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * SPU file system -- file contents
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/ioctl.h>
  24. #include <linux/module.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/poll.h>
  27. #include <linux/ptrace.h>
  28. #include <asm/io.h>
  29. #include <asm/semaphore.h>
  30. #include <asm/spu.h>
  31. #include <asm/uaccess.h>
  32. #include "spufs.h"
  33. static int
  34. spufs_mem_open(struct inode *inode, struct file *file)
  35. {
  36. struct spufs_inode_info *i = SPUFS_I(inode);
  37. file->private_data = i->i_ctx;
  38. file->f_mapping = i->i_ctx->local_store;
  39. return 0;
  40. }
  41. static ssize_t
  42. spufs_mem_read(struct file *file, char __user *buffer,
  43. size_t size, loff_t *pos)
  44. {
  45. struct spu_context *ctx = file->private_data;
  46. char *local_store;
  47. int ret;
  48. spu_acquire(ctx);
  49. local_store = ctx->ops->get_ls(ctx);
  50. ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
  51. spu_release(ctx);
  52. return ret;
  53. }
  54. static ssize_t
  55. spufs_mem_write(struct file *file, const char __user *buffer,
  56. size_t size, loff_t *pos)
  57. {
  58. struct spu_context *ctx = file->private_data;
  59. char *local_store;
  60. int ret;
  61. size = min_t(ssize_t, LS_SIZE - *pos, size);
  62. if (size <= 0)
  63. return -EFBIG;
  64. *pos += size;
  65. spu_acquire(ctx);
  66. local_store = ctx->ops->get_ls(ctx);
  67. ret = copy_from_user(local_store + *pos - size,
  68. buffer, size) ? -EFAULT : size;
  69. spu_release(ctx);
  70. return ret;
  71. }
  72. #ifdef CONFIG_SPARSEMEM
  73. static struct page *
  74. spufs_mem_mmap_nopage(struct vm_area_struct *vma,
  75. unsigned long address, int *type)
  76. {
  77. struct page *page = NOPAGE_SIGBUS;
  78. struct spu_context *ctx = vma->vm_file->private_data;
  79. unsigned long offset = address - vma->vm_start;
  80. offset += vma->vm_pgoff << PAGE_SHIFT;
  81. spu_acquire(ctx);
  82. if (ctx->state == SPU_STATE_SAVED)
  83. page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
  84. else
  85. page = pfn_to_page((ctx->spu->local_store_phys + offset)
  86. >> PAGE_SHIFT);
  87. spu_release(ctx);
  88. if (type)
  89. *type = VM_FAULT_MINOR;
  90. page_cache_get(page);
  91. return page;
  92. }
  93. static struct vm_operations_struct spufs_mem_mmap_vmops = {
  94. .nopage = spufs_mem_mmap_nopage,
  95. };
  96. static int
  97. spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
  98. {
  99. if (!(vma->vm_flags & VM_SHARED))
  100. return -EINVAL;
  101. /* FIXME: */
  102. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  103. | _PAGE_NO_CACHE);
  104. vma->vm_ops = &spufs_mem_mmap_vmops;
  105. return 0;
  106. }
  107. #endif
  108. static struct file_operations spufs_mem_fops = {
  109. .open = spufs_mem_open,
  110. .read = spufs_mem_read,
  111. .write = spufs_mem_write,
  112. .llseek = generic_file_llseek,
  113. #ifdef CONFIG_SPARSEMEM
  114. .mmap = spufs_mem_mmap,
  115. #endif
  116. };
  117. static int
  118. spufs_regs_open(struct inode *inode, struct file *file)
  119. {
  120. struct spufs_inode_info *i = SPUFS_I(inode);
  121. file->private_data = i->i_ctx;
  122. return 0;
  123. }
  124. static ssize_t
  125. spufs_regs_read(struct file *file, char __user *buffer,
  126. size_t size, loff_t *pos)
  127. {
  128. struct spu_context *ctx = file->private_data;
  129. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  130. int ret;
  131. spu_acquire_saved(ctx);
  132. ret = simple_read_from_buffer(buffer, size, pos,
  133. lscsa->gprs, sizeof lscsa->gprs);
  134. spu_release(ctx);
  135. return ret;
  136. }
  137. static ssize_t
  138. spufs_regs_write(struct file *file, const char __user *buffer,
  139. size_t size, loff_t *pos)
  140. {
  141. struct spu_context *ctx = file->private_data;
  142. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  143. int ret;
  144. size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
  145. if (size <= 0)
  146. return -EFBIG;
  147. *pos += size;
  148. spu_acquire_saved(ctx);
  149. ret = copy_from_user(lscsa->gprs + *pos - size,
  150. buffer, size) ? -EFAULT : size;
  151. spu_release(ctx);
  152. return ret;
  153. }
  154. static struct file_operations spufs_regs_fops = {
  155. .open = spufs_regs_open,
  156. .read = spufs_regs_read,
  157. .write = spufs_regs_write,
  158. .llseek = generic_file_llseek,
  159. };
  160. static ssize_t
  161. spufs_fpcr_read(struct file *file, char __user * buffer,
  162. size_t size, loff_t * pos)
  163. {
  164. struct spu_context *ctx = file->private_data;
  165. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  166. int ret;
  167. spu_acquire_saved(ctx);
  168. ret = simple_read_from_buffer(buffer, size, pos,
  169. &lscsa->fpcr, sizeof(lscsa->fpcr));
  170. spu_release(ctx);
  171. return ret;
  172. }
  173. static ssize_t
  174. spufs_fpcr_write(struct file *file, const char __user * buffer,
  175. size_t size, loff_t * pos)
  176. {
  177. struct spu_context *ctx = file->private_data;
  178. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  179. int ret;
  180. size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
  181. if (size <= 0)
  182. return -EFBIG;
  183. *pos += size;
  184. spu_acquire_saved(ctx);
  185. ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
  186. buffer, size) ? -EFAULT : size;
  187. spu_release(ctx);
  188. return ret;
  189. }
  190. static struct file_operations spufs_fpcr_fops = {
  191. .open = spufs_regs_open,
  192. .read = spufs_fpcr_read,
  193. .write = spufs_fpcr_write,
  194. .llseek = generic_file_llseek,
  195. };
  196. /* generic open function for all pipe-like files */
  197. static int spufs_pipe_open(struct inode *inode, struct file *file)
  198. {
  199. struct spufs_inode_info *i = SPUFS_I(inode);
  200. file->private_data = i->i_ctx;
  201. return nonseekable_open(inode, file);
  202. }
  203. static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
  204. size_t len, loff_t *pos)
  205. {
  206. struct spu_context *ctx = file->private_data;
  207. u32 mbox_data;
  208. int ret;
  209. if (len < 4)
  210. return -EINVAL;
  211. spu_acquire(ctx);
  212. ret = ctx->ops->mbox_read(ctx, &mbox_data);
  213. spu_release(ctx);
  214. if (!ret)
  215. return -EAGAIN;
  216. if (copy_to_user(buf, &mbox_data, sizeof mbox_data))
  217. return -EFAULT;
  218. return 4;
  219. }
  220. static struct file_operations spufs_mbox_fops = {
  221. .open = spufs_pipe_open,
  222. .read = spufs_mbox_read,
  223. };
  224. static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
  225. size_t len, loff_t *pos)
  226. {
  227. struct spu_context *ctx = file->private_data;
  228. u32 mbox_stat;
  229. if (len < 4)
  230. return -EINVAL;
  231. spu_acquire(ctx);
  232. mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
  233. spu_release(ctx);
  234. if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
  235. return -EFAULT;
  236. return 4;
  237. }
  238. static struct file_operations spufs_mbox_stat_fops = {
  239. .open = spufs_pipe_open,
  240. .read = spufs_mbox_stat_read,
  241. };
  242. /*
  243. * spufs_wait
  244. * Same as wait_event_interruptible(), except that here
  245. * we need to call spu_release(ctx) before sleeping, and
  246. * then spu_acquire(ctx) when awoken.
  247. */
  248. #define spufs_wait(wq, condition) \
  249. ({ \
  250. int __ret = 0; \
  251. DEFINE_WAIT(__wait); \
  252. for (;;) { \
  253. prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
  254. if (condition) \
  255. break; \
  256. if (!signal_pending(current)) { \
  257. spu_release(ctx); \
  258. schedule(); \
  259. spu_acquire(ctx); \
  260. continue; \
  261. } \
  262. __ret = -ERESTARTSYS; \
  263. break; \
  264. } \
  265. finish_wait(&(wq), &__wait); \
  266. __ret; \
  267. })
  268. /* low-level ibox access function */
  269. size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
  270. {
  271. return ctx->ops->ibox_read(ctx, data);
  272. }
  273. static int spufs_ibox_fasync(int fd, struct file *file, int on)
  274. {
  275. struct spu_context *ctx = file->private_data;
  276. return fasync_helper(fd, file, on, &ctx->ibox_fasync);
  277. }
  278. /* interrupt-level ibox callback function. */
  279. void spufs_ibox_callback(struct spu *spu)
  280. {
  281. struct spu_context *ctx = spu->ctx;
  282. wake_up_all(&ctx->ibox_wq);
  283. kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
  284. }
  285. static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
  286. size_t len, loff_t *pos)
  287. {
  288. struct spu_context *ctx = file->private_data;
  289. u32 ibox_data;
  290. ssize_t ret;
  291. if (len < 4)
  292. return -EINVAL;
  293. spu_acquire(ctx);
  294. ret = 0;
  295. if (file->f_flags & O_NONBLOCK) {
  296. if (!spu_ibox_read(ctx, &ibox_data))
  297. ret = -EAGAIN;
  298. } else {
  299. ret = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
  300. }
  301. spu_release(ctx);
  302. if (ret)
  303. return ret;
  304. ret = 4;
  305. if (copy_to_user(buf, &ibox_data, sizeof ibox_data))
  306. ret = -EFAULT;
  307. return ret;
  308. }
  309. static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
  310. {
  311. struct spu_context *ctx = file->private_data;
  312. unsigned int mask;
  313. poll_wait(file, &ctx->ibox_wq, wait);
  314. spu_acquire(ctx);
  315. mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
  316. spu_release(ctx);
  317. return mask;
  318. }
  319. static struct file_operations spufs_ibox_fops = {
  320. .open = spufs_pipe_open,
  321. .read = spufs_ibox_read,
  322. .poll = spufs_ibox_poll,
  323. .fasync = spufs_ibox_fasync,
  324. };
  325. static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
  326. size_t len, loff_t *pos)
  327. {
  328. struct spu_context *ctx = file->private_data;
  329. u32 ibox_stat;
  330. if (len < 4)
  331. return -EINVAL;
  332. spu_acquire(ctx);
  333. ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
  334. spu_release(ctx);
  335. if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
  336. return -EFAULT;
  337. return 4;
  338. }
  339. static struct file_operations spufs_ibox_stat_fops = {
  340. .open = spufs_pipe_open,
  341. .read = spufs_ibox_stat_read,
  342. };
  343. /* low-level mailbox write */
  344. size_t spu_wbox_write(struct spu_context *ctx, u32 data)
  345. {
  346. return ctx->ops->wbox_write(ctx, data);
  347. }
  348. static int spufs_wbox_fasync(int fd, struct file *file, int on)
  349. {
  350. struct spu_context *ctx = file->private_data;
  351. int ret;
  352. ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
  353. return ret;
  354. }
  355. /* interrupt-level wbox callback function. */
  356. void spufs_wbox_callback(struct spu *spu)
  357. {
  358. struct spu_context *ctx = spu->ctx;
  359. wake_up_all(&ctx->wbox_wq);
  360. kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
  361. }
  362. static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
  363. size_t len, loff_t *pos)
  364. {
  365. struct spu_context *ctx = file->private_data;
  366. u32 wbox_data;
  367. int ret;
  368. if (len < 4)
  369. return -EINVAL;
  370. if (copy_from_user(&wbox_data, buf, sizeof wbox_data))
  371. return -EFAULT;
  372. spu_acquire(ctx);
  373. ret = 0;
  374. if (file->f_flags & O_NONBLOCK) {
  375. if (!spu_wbox_write(ctx, wbox_data))
  376. ret = -EAGAIN;
  377. } else {
  378. ret = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
  379. }
  380. spu_release(ctx);
  381. return ret ? ret : sizeof wbox_data;
  382. }
  383. static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
  384. {
  385. struct spu_context *ctx = file->private_data;
  386. unsigned int mask;
  387. poll_wait(file, &ctx->wbox_wq, wait);
  388. spu_acquire(ctx);
  389. mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
  390. spu_release(ctx);
  391. return mask;
  392. }
  393. static struct file_operations spufs_wbox_fops = {
  394. .open = spufs_pipe_open,
  395. .write = spufs_wbox_write,
  396. .poll = spufs_wbox_poll,
  397. .fasync = spufs_wbox_fasync,
  398. };
  399. static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
  400. size_t len, loff_t *pos)
  401. {
  402. struct spu_context *ctx = file->private_data;
  403. u32 wbox_stat;
  404. if (len < 4)
  405. return -EINVAL;
  406. spu_acquire(ctx);
  407. wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
  408. spu_release(ctx);
  409. if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
  410. return -EFAULT;
  411. return 4;
  412. }
  413. static struct file_operations spufs_wbox_stat_fops = {
  414. .open = spufs_pipe_open,
  415. .read = spufs_wbox_stat_read,
  416. };
  417. /* interrupt-level stop callback function. */
  418. void spufs_stop_callback(struct spu *spu)
  419. {
  420. struct spu_context *ctx = spu->ctx;
  421. wake_up_all(&ctx->stop_wq);
  422. }
  423. static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
  424. {
  425. struct spu *spu;
  426. u64 pte_fault;
  427. *stat = ctx->ops->status_read(ctx);
  428. if (ctx->state != SPU_STATE_RUNNABLE)
  429. return 1;
  430. spu = ctx->spu;
  431. pte_fault = spu->dsisr &
  432. (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
  433. return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
  434. }
  435. static inline int spu_run_init(struct spu_context *ctx, u32 * npc,
  436. u32 * status)
  437. {
  438. int ret;
  439. if ((ret = spu_acquire_runnable(ctx)) != 0)
  440. return ret;
  441. ctx->ops->npc_write(ctx, *npc);
  442. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  443. return 0;
  444. }
  445. static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
  446. u32 * status)
  447. {
  448. int ret = 0;
  449. *status = ctx->ops->status_read(ctx);
  450. *npc = ctx->ops->npc_read(ctx);
  451. spu_release(ctx);
  452. if (signal_pending(current))
  453. ret = -ERESTARTSYS;
  454. if (unlikely(current->ptrace & PT_PTRACED)) {
  455. if ((*status & SPU_STATUS_STOPPED_BY_STOP)
  456. && (*status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
  457. force_sig(SIGTRAP, current);
  458. ret = -ERESTARTSYS;
  459. }
  460. }
  461. return ret;
  462. }
  463. static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
  464. u32 *status)
  465. {
  466. int ret;
  467. if ((ret = spu_run_fini(ctx, npc, status)) != 0)
  468. return ret;
  469. if (*status & (SPU_STATUS_STOPPED_BY_STOP |
  470. SPU_STATUS_STOPPED_BY_HALT)) {
  471. return *status;
  472. }
  473. if ((ret = spu_run_init(ctx, npc, status)) != 0)
  474. return ret;
  475. return 0;
  476. }
  477. static inline int spu_process_events(struct spu_context *ctx)
  478. {
  479. struct spu *spu = ctx->spu;
  480. u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
  481. int ret = 0;
  482. if (spu->dsisr & pte_fault)
  483. ret = spu_irq_class_1_bottom(spu);
  484. if (spu->class_0_pending)
  485. ret = spu_irq_class_0_bottom(spu);
  486. if (!ret && signal_pending(current))
  487. ret = -ERESTARTSYS;
  488. return ret;
  489. }
  490. long spufs_run_spu(struct file *file, struct spu_context *ctx,
  491. u32 * npc, u32 * status)
  492. {
  493. int ret;
  494. if (down_interruptible(&ctx->run_sema))
  495. return -ERESTARTSYS;
  496. ret = spu_run_init(ctx, npc, status);
  497. if (ret)
  498. goto out;
  499. do {
  500. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, status));
  501. if (unlikely(ret))
  502. break;
  503. if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
  504. ret = spu_reacquire_runnable(ctx, npc, status);
  505. if (ret)
  506. goto out;
  507. continue;
  508. }
  509. ret = spu_process_events(ctx);
  510. } while (!ret && !(*status & (SPU_STATUS_STOPPED_BY_STOP |
  511. SPU_STATUS_STOPPED_BY_HALT)));
  512. ctx->ops->runcntl_stop(ctx);
  513. ret = spu_run_fini(ctx, npc, status);
  514. if (!ret)
  515. ret = *status;
  516. spu_yield(ctx);
  517. out:
  518. up(&ctx->run_sema);
  519. return ret;
  520. }
  521. static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
  522. size_t len, loff_t *pos)
  523. {
  524. struct spu_context *ctx = file->private_data;
  525. u32 data;
  526. if (len < 4)
  527. return -EINVAL;
  528. spu_acquire(ctx);
  529. data = ctx->ops->signal1_read(ctx);
  530. spu_release(ctx);
  531. if (copy_to_user(buf, &data, 4))
  532. return -EFAULT;
  533. return 4;
  534. }
  535. static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
  536. size_t len, loff_t *pos)
  537. {
  538. struct spu_context *ctx;
  539. u32 data;
  540. ctx = file->private_data;
  541. if (len < 4)
  542. return -EINVAL;
  543. if (copy_from_user(&data, buf, 4))
  544. return -EFAULT;
  545. spu_acquire(ctx);
  546. ctx->ops->signal1_write(ctx, data);
  547. spu_release(ctx);
  548. return 4;
  549. }
  550. static struct file_operations spufs_signal1_fops = {
  551. .open = spufs_pipe_open,
  552. .read = spufs_signal1_read,
  553. .write = spufs_signal1_write,
  554. };
  555. static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
  556. size_t len, loff_t *pos)
  557. {
  558. struct spu_context *ctx;
  559. u32 data;
  560. ctx = file->private_data;
  561. if (len < 4)
  562. return -EINVAL;
  563. spu_acquire(ctx);
  564. data = ctx->ops->signal2_read(ctx);
  565. spu_release(ctx);
  566. if (copy_to_user(buf, &data, 4))
  567. return -EFAULT;
  568. return 4;
  569. }
  570. static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
  571. size_t len, loff_t *pos)
  572. {
  573. struct spu_context *ctx;
  574. u32 data;
  575. ctx = file->private_data;
  576. if (len < 4)
  577. return -EINVAL;
  578. if (copy_from_user(&data, buf, 4))
  579. return -EFAULT;
  580. spu_acquire(ctx);
  581. ctx->ops->signal2_write(ctx, data);
  582. spu_release(ctx);
  583. return 4;
  584. }
  585. static struct file_operations spufs_signal2_fops = {
  586. .open = spufs_pipe_open,
  587. .read = spufs_signal2_read,
  588. .write = spufs_signal2_write,
  589. };
  590. static void spufs_signal1_type_set(void *data, u64 val)
  591. {
  592. struct spu_context *ctx = data;
  593. spu_acquire(ctx);
  594. ctx->ops->signal1_type_set(ctx, val);
  595. spu_release(ctx);
  596. }
  597. static u64 spufs_signal1_type_get(void *data)
  598. {
  599. struct spu_context *ctx = data;
  600. u64 ret;
  601. spu_acquire(ctx);
  602. ret = ctx->ops->signal1_type_get(ctx);
  603. spu_release(ctx);
  604. return ret;
  605. }
  606. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  607. spufs_signal1_type_set, "%llu");
  608. static void spufs_signal2_type_set(void *data, u64 val)
  609. {
  610. struct spu_context *ctx = data;
  611. spu_acquire(ctx);
  612. ctx->ops->signal2_type_set(ctx, val);
  613. spu_release(ctx);
  614. }
  615. static u64 spufs_signal2_type_get(void *data)
  616. {
  617. struct spu_context *ctx = data;
  618. u64 ret;
  619. spu_acquire(ctx);
  620. ret = ctx->ops->signal2_type_get(ctx);
  621. spu_release(ctx);
  622. return ret;
  623. }
  624. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  625. spufs_signal2_type_set, "%llu");
  626. static void spufs_npc_set(void *data, u64 val)
  627. {
  628. struct spu_context *ctx = data;
  629. spu_acquire(ctx);
  630. ctx->ops->npc_write(ctx, val);
  631. spu_release(ctx);
  632. }
  633. static u64 spufs_npc_get(void *data)
  634. {
  635. struct spu_context *ctx = data;
  636. u64 ret;
  637. spu_acquire(ctx);
  638. ret = ctx->ops->npc_read(ctx);
  639. spu_release(ctx);
  640. return ret;
  641. }
  642. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
  643. static void spufs_decr_set(void *data, u64 val)
  644. {
  645. struct spu_context *ctx = data;
  646. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  647. spu_acquire_saved(ctx);
  648. lscsa->decr.slot[0] = (u32) val;
  649. spu_release(ctx);
  650. }
  651. static u64 spufs_decr_get(void *data)
  652. {
  653. struct spu_context *ctx = data;
  654. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  655. u64 ret;
  656. spu_acquire_saved(ctx);
  657. ret = lscsa->decr.slot[0];
  658. spu_release(ctx);
  659. return ret;
  660. }
  661. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  662. "%llx\n")
  663. static void spufs_decr_status_set(void *data, u64 val)
  664. {
  665. struct spu_context *ctx = data;
  666. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  667. spu_acquire_saved(ctx);
  668. lscsa->decr_status.slot[0] = (u32) val;
  669. spu_release(ctx);
  670. }
  671. static u64 spufs_decr_status_get(void *data)
  672. {
  673. struct spu_context *ctx = data;
  674. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  675. u64 ret;
  676. spu_acquire_saved(ctx);
  677. ret = lscsa->decr_status.slot[0];
  678. spu_release(ctx);
  679. return ret;
  680. }
  681. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  682. spufs_decr_status_set, "%llx\n")
  683. static void spufs_spu_tag_mask_set(void *data, u64 val)
  684. {
  685. struct spu_context *ctx = data;
  686. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  687. spu_acquire_saved(ctx);
  688. lscsa->tag_mask.slot[0] = (u32) val;
  689. spu_release(ctx);
  690. }
  691. static u64 spufs_spu_tag_mask_get(void *data)
  692. {
  693. struct spu_context *ctx = data;
  694. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  695. u64 ret;
  696. spu_acquire_saved(ctx);
  697. ret = lscsa->tag_mask.slot[0];
  698. spu_release(ctx);
  699. return ret;
  700. }
  701. DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
  702. spufs_spu_tag_mask_set, "%llx\n")
  703. static void spufs_event_mask_set(void *data, u64 val)
  704. {
  705. struct spu_context *ctx = data;
  706. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  707. spu_acquire_saved(ctx);
  708. lscsa->event_mask.slot[0] = (u32) val;
  709. spu_release(ctx);
  710. }
  711. static u64 spufs_event_mask_get(void *data)
  712. {
  713. struct spu_context *ctx = data;
  714. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  715. u64 ret;
  716. spu_acquire_saved(ctx);
  717. ret = lscsa->event_mask.slot[0];
  718. spu_release(ctx);
  719. return ret;
  720. }
  721. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  722. spufs_event_mask_set, "%llx\n")
  723. static void spufs_srr0_set(void *data, u64 val)
  724. {
  725. struct spu_context *ctx = data;
  726. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  727. spu_acquire_saved(ctx);
  728. lscsa->srr0.slot[0] = (u32) val;
  729. spu_release(ctx);
  730. }
  731. static u64 spufs_srr0_get(void *data)
  732. {
  733. struct spu_context *ctx = data;
  734. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  735. u64 ret;
  736. spu_acquire_saved(ctx);
  737. ret = lscsa->srr0.slot[0];
  738. spu_release(ctx);
  739. return ret;
  740. }
  741. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  742. "%llx\n")
  743. struct tree_descr spufs_dir_contents[] = {
  744. { "mem", &spufs_mem_fops, 0666, },
  745. { "regs", &spufs_regs_fops, 0666, },
  746. { "mbox", &spufs_mbox_fops, 0444, },
  747. { "ibox", &spufs_ibox_fops, 0444, },
  748. { "wbox", &spufs_wbox_fops, 0222, },
  749. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  750. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  751. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  752. { "signal1", &spufs_signal1_fops, 0666, },
  753. { "signal2", &spufs_signal2_fops, 0666, },
  754. { "signal1_type", &spufs_signal1_type, 0666, },
  755. { "signal2_type", &spufs_signal2_type, 0666, },
  756. { "npc", &spufs_npc_ops, 0666, },
  757. { "fpcr", &spufs_fpcr_fops, 0666, },
  758. { "decr", &spufs_decr_ops, 0666, },
  759. { "decr_status", &spufs_decr_status_ops, 0666, },
  760. { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
  761. { "event_mask", &spufs_event_mask_ops, 0666, },
  762. { "srr0", &spufs_srr0_ops, 0666, },
  763. {},
  764. };