file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 ((ret = spu_run_init(ctx, npc, status)) != 0)
  495. return ret;
  496. do {
  497. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, status));
  498. if (unlikely(ret))
  499. break;
  500. if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
  501. ret = spu_reacquire_runnable(ctx, npc, status);
  502. if (ret) {
  503. return ret;
  504. }
  505. continue;
  506. }
  507. ret = spu_process_events(ctx);
  508. } while (!ret && !(*status & (SPU_STATUS_STOPPED_BY_STOP |
  509. SPU_STATUS_STOPPED_BY_HALT)));
  510. ctx->ops->runcntl_stop(ctx);
  511. ret = spu_run_fini(ctx, npc, status);
  512. if (!ret)
  513. ret = *status;
  514. spu_yield(ctx);
  515. return ret;
  516. }
  517. static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
  518. size_t len, loff_t *pos)
  519. {
  520. struct spu_context *ctx = file->private_data;
  521. u32 data;
  522. if (len < 4)
  523. return -EINVAL;
  524. spu_acquire(ctx);
  525. data = ctx->ops->signal1_read(ctx);
  526. spu_release(ctx);
  527. if (copy_to_user(buf, &data, 4))
  528. return -EFAULT;
  529. return 4;
  530. }
  531. static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
  532. size_t len, loff_t *pos)
  533. {
  534. struct spu_context *ctx;
  535. u32 data;
  536. ctx = file->private_data;
  537. if (len < 4)
  538. return -EINVAL;
  539. if (copy_from_user(&data, buf, 4))
  540. return -EFAULT;
  541. spu_acquire(ctx);
  542. ctx->ops->signal1_write(ctx, data);
  543. spu_release(ctx);
  544. return 4;
  545. }
  546. static struct file_operations spufs_signal1_fops = {
  547. .open = spufs_pipe_open,
  548. .read = spufs_signal1_read,
  549. .write = spufs_signal1_write,
  550. };
  551. static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
  552. size_t len, loff_t *pos)
  553. {
  554. struct spu_context *ctx;
  555. u32 data;
  556. ctx = file->private_data;
  557. if (len < 4)
  558. return -EINVAL;
  559. spu_acquire(ctx);
  560. data = ctx->ops->signal2_read(ctx);
  561. spu_release(ctx);
  562. if (copy_to_user(buf, &data, 4))
  563. return -EFAULT;
  564. return 4;
  565. }
  566. static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
  567. size_t len, loff_t *pos)
  568. {
  569. struct spu_context *ctx;
  570. u32 data;
  571. ctx = file->private_data;
  572. if (len < 4)
  573. return -EINVAL;
  574. if (copy_from_user(&data, buf, 4))
  575. return -EFAULT;
  576. spu_acquire(ctx);
  577. ctx->ops->signal2_write(ctx, data);
  578. spu_release(ctx);
  579. return 4;
  580. }
  581. static struct file_operations spufs_signal2_fops = {
  582. .open = spufs_pipe_open,
  583. .read = spufs_signal2_read,
  584. .write = spufs_signal2_write,
  585. };
  586. static void spufs_signal1_type_set(void *data, u64 val)
  587. {
  588. struct spu_context *ctx = data;
  589. spu_acquire(ctx);
  590. ctx->ops->signal1_type_set(ctx, val);
  591. spu_release(ctx);
  592. }
  593. static u64 spufs_signal1_type_get(void *data)
  594. {
  595. struct spu_context *ctx = data;
  596. u64 ret;
  597. spu_acquire(ctx);
  598. ret = ctx->ops->signal1_type_get(ctx);
  599. spu_release(ctx);
  600. return ret;
  601. }
  602. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  603. spufs_signal1_type_set, "%llu");
  604. static void spufs_signal2_type_set(void *data, u64 val)
  605. {
  606. struct spu_context *ctx = data;
  607. spu_acquire(ctx);
  608. ctx->ops->signal2_type_set(ctx, val);
  609. spu_release(ctx);
  610. }
  611. static u64 spufs_signal2_type_get(void *data)
  612. {
  613. struct spu_context *ctx = data;
  614. u64 ret;
  615. spu_acquire(ctx);
  616. ret = ctx->ops->signal2_type_get(ctx);
  617. spu_release(ctx);
  618. return ret;
  619. }
  620. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  621. spufs_signal2_type_set, "%llu");
  622. static void spufs_npc_set(void *data, u64 val)
  623. {
  624. struct spu_context *ctx = data;
  625. spu_acquire(ctx);
  626. ctx->ops->npc_write(ctx, val);
  627. spu_release(ctx);
  628. }
  629. static u64 spufs_npc_get(void *data)
  630. {
  631. struct spu_context *ctx = data;
  632. u64 ret;
  633. spu_acquire(ctx);
  634. ret = ctx->ops->npc_read(ctx);
  635. spu_release(ctx);
  636. return ret;
  637. }
  638. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
  639. static void spufs_decr_set(void *data, u64 val)
  640. {
  641. struct spu_context *ctx = data;
  642. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  643. spu_acquire_saved(ctx);
  644. lscsa->decr.slot[0] = (u32) val;
  645. spu_release(ctx);
  646. }
  647. static u64 spufs_decr_get(void *data)
  648. {
  649. struct spu_context *ctx = data;
  650. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  651. u64 ret;
  652. spu_acquire_saved(ctx);
  653. ret = lscsa->decr.slot[0];
  654. spu_release(ctx);
  655. return ret;
  656. }
  657. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  658. "%llx\n")
  659. static void spufs_decr_status_set(void *data, u64 val)
  660. {
  661. struct spu_context *ctx = data;
  662. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  663. spu_acquire_saved(ctx);
  664. lscsa->decr_status.slot[0] = (u32) val;
  665. spu_release(ctx);
  666. }
  667. static u64 spufs_decr_status_get(void *data)
  668. {
  669. struct spu_context *ctx = data;
  670. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  671. u64 ret;
  672. spu_acquire_saved(ctx);
  673. ret = lscsa->decr_status.slot[0];
  674. spu_release(ctx);
  675. return ret;
  676. }
  677. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  678. spufs_decr_status_set, "%llx\n")
  679. static void spufs_spu_tag_mask_set(void *data, u64 val)
  680. {
  681. struct spu_context *ctx = data;
  682. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  683. spu_acquire_saved(ctx);
  684. lscsa->tag_mask.slot[0] = (u32) val;
  685. spu_release(ctx);
  686. }
  687. static u64 spufs_spu_tag_mask_get(void *data)
  688. {
  689. struct spu_context *ctx = data;
  690. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  691. u64 ret;
  692. spu_acquire_saved(ctx);
  693. ret = lscsa->tag_mask.slot[0];
  694. spu_release(ctx);
  695. return ret;
  696. }
  697. DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
  698. spufs_spu_tag_mask_set, "%llx\n")
  699. static void spufs_event_mask_set(void *data, u64 val)
  700. {
  701. struct spu_context *ctx = data;
  702. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  703. spu_acquire_saved(ctx);
  704. lscsa->event_mask.slot[0] = (u32) val;
  705. spu_release(ctx);
  706. }
  707. static u64 spufs_event_mask_get(void *data)
  708. {
  709. struct spu_context *ctx = data;
  710. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  711. u64 ret;
  712. spu_acquire_saved(ctx);
  713. ret = lscsa->event_mask.slot[0];
  714. spu_release(ctx);
  715. return ret;
  716. }
  717. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  718. spufs_event_mask_set, "%llx\n")
  719. static void spufs_srr0_set(void *data, u64 val)
  720. {
  721. struct spu_context *ctx = data;
  722. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  723. spu_acquire_saved(ctx);
  724. lscsa->srr0.slot[0] = (u32) val;
  725. spu_release(ctx);
  726. }
  727. static u64 spufs_srr0_get(void *data)
  728. {
  729. struct spu_context *ctx = data;
  730. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  731. u64 ret;
  732. spu_acquire_saved(ctx);
  733. ret = lscsa->srr0.slot[0];
  734. spu_release(ctx);
  735. return ret;
  736. }
  737. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  738. "%llx\n")
  739. struct tree_descr spufs_dir_contents[] = {
  740. { "mem", &spufs_mem_fops, 0666, },
  741. { "regs", &spufs_regs_fops, 0666, },
  742. { "mbox", &spufs_mbox_fops, 0444, },
  743. { "ibox", &spufs_ibox_fops, 0444, },
  744. { "wbox", &spufs_wbox_fops, 0222, },
  745. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  746. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  747. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  748. { "signal1", &spufs_signal1_fops, 0666, },
  749. { "signal2", &spufs_signal2_fops, 0666, },
  750. { "signal1_type", &spufs_signal1_type, 0666, },
  751. { "signal2_type", &spufs_signal2_type, 0666, },
  752. { "npc", &spufs_npc_ops, 0666, },
  753. { "fpcr", &spufs_fpcr_fops, 0666, },
  754. { "decr", &spufs_decr_ops, 0666, },
  755. { "decr_status", &spufs_decr_status_ops, 0666, },
  756. { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
  757. { "event_mask", &spufs_event_mask_ops, 0666, },
  758. { "srr0", &spufs_srr0_ops, 0666, },
  759. {},
  760. };