file.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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. #undef DEBUG
  23. #include <linux/fs.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/module.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/poll.h>
  28. #include <linux/ptrace.h>
  29. #include <asm/io.h>
  30. #include <asm/semaphore.h>
  31. #include <asm/spu.h>
  32. #include <asm/uaccess.h>
  33. #include "spufs.h"
  34. #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
  35. static int
  36. spufs_mem_open(struct inode *inode, struct file *file)
  37. {
  38. struct spufs_inode_info *i = SPUFS_I(inode);
  39. struct spu_context *ctx = i->i_ctx;
  40. file->private_data = ctx;
  41. file->f_mapping = inode->i_mapping;
  42. ctx->local_store = inode->i_mapping;
  43. return 0;
  44. }
  45. static ssize_t
  46. spufs_mem_read(struct file *file, char __user *buffer,
  47. size_t size, loff_t *pos)
  48. {
  49. struct spu_context *ctx = file->private_data;
  50. char *local_store;
  51. int ret;
  52. spu_acquire(ctx);
  53. local_store = ctx->ops->get_ls(ctx);
  54. ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
  55. spu_release(ctx);
  56. return ret;
  57. }
  58. static ssize_t
  59. spufs_mem_write(struct file *file, const char __user *buffer,
  60. size_t size, loff_t *pos)
  61. {
  62. struct spu_context *ctx = file->private_data;
  63. char *local_store;
  64. int ret;
  65. size = min_t(ssize_t, LS_SIZE - *pos, size);
  66. if (size <= 0)
  67. return -EFBIG;
  68. *pos += size;
  69. spu_acquire(ctx);
  70. local_store = ctx->ops->get_ls(ctx);
  71. ret = copy_from_user(local_store + *pos - size,
  72. buffer, size) ? -EFAULT : size;
  73. spu_release(ctx);
  74. return ret;
  75. }
  76. static struct page *
  77. spufs_mem_mmap_nopage(struct vm_area_struct *vma,
  78. unsigned long address, int *type)
  79. {
  80. struct page *page = NOPAGE_SIGBUS;
  81. struct spu_context *ctx = vma->vm_file->private_data;
  82. unsigned long offset = address - vma->vm_start;
  83. offset += vma->vm_pgoff << PAGE_SHIFT;
  84. spu_acquire(ctx);
  85. if (ctx->state == SPU_STATE_SAVED) {
  86. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  87. & ~(_PAGE_NO_CACHE | _PAGE_GUARDED));
  88. page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
  89. } else {
  90. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  91. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  92. page = pfn_to_page((ctx->spu->local_store_phys + offset)
  93. >> PAGE_SHIFT);
  94. }
  95. spu_release(ctx);
  96. if (type)
  97. *type = VM_FAULT_MINOR;
  98. page_cache_get(page);
  99. return page;
  100. }
  101. static struct vm_operations_struct spufs_mem_mmap_vmops = {
  102. .nopage = spufs_mem_mmap_nopage,
  103. };
  104. static int
  105. spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
  106. {
  107. if (!(vma->vm_flags & VM_SHARED))
  108. return -EINVAL;
  109. /* FIXME: */
  110. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  111. | _PAGE_NO_CACHE);
  112. vma->vm_ops = &spufs_mem_mmap_vmops;
  113. return 0;
  114. }
  115. static struct file_operations spufs_mem_fops = {
  116. .open = spufs_mem_open,
  117. .read = spufs_mem_read,
  118. .write = spufs_mem_write,
  119. .llseek = generic_file_llseek,
  120. .mmap = spufs_mem_mmap,
  121. };
  122. static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
  123. unsigned long address,
  124. int *type, unsigned long ps_offs,
  125. unsigned long ps_size)
  126. {
  127. struct page *page = NOPAGE_SIGBUS;
  128. int fault_type = VM_FAULT_SIGBUS;
  129. struct spu_context *ctx = vma->vm_file->private_data;
  130. unsigned long offset = address - vma->vm_start;
  131. unsigned long area;
  132. int ret;
  133. offset += vma->vm_pgoff << PAGE_SHIFT;
  134. if (offset >= ps_size)
  135. goto out;
  136. ret = spu_acquire_runnable(ctx);
  137. if (ret)
  138. goto out;
  139. area = ctx->spu->problem_phys + ps_offs;
  140. page = pfn_to_page((area + offset) >> PAGE_SHIFT);
  141. fault_type = VM_FAULT_MINOR;
  142. page_cache_get(page);
  143. spu_release(ctx);
  144. out:
  145. if (type)
  146. *type = fault_type;
  147. return page;
  148. }
  149. #if SPUFS_MMAP_4K
  150. static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
  151. unsigned long address, int *type)
  152. {
  153. return spufs_ps_nopage(vma, address, type, 0x4000, 0x1000);
  154. }
  155. static struct vm_operations_struct spufs_cntl_mmap_vmops = {
  156. .nopage = spufs_cntl_mmap_nopage,
  157. };
  158. /*
  159. * mmap support for problem state control area [0x4000 - 0x4fff].
  160. */
  161. static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
  162. {
  163. if (!(vma->vm_flags & VM_SHARED))
  164. return -EINVAL;
  165. vma->vm_flags |= VM_RESERVED;
  166. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  167. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  168. vma->vm_ops = &spufs_cntl_mmap_vmops;
  169. return 0;
  170. }
  171. #else /* SPUFS_MMAP_4K */
  172. #define spufs_cntl_mmap NULL
  173. #endif /* !SPUFS_MMAP_4K */
  174. static u64 spufs_cntl_get(void *data)
  175. {
  176. struct spu_context *ctx = data;
  177. u64 val;
  178. spu_acquire(ctx);
  179. val = ctx->ops->status_read(ctx);
  180. spu_release(ctx);
  181. return val;
  182. }
  183. static void spufs_cntl_set(void *data, u64 val)
  184. {
  185. struct spu_context *ctx = data;
  186. spu_acquire(ctx);
  187. ctx->ops->runcntl_write(ctx, val);
  188. spu_release(ctx);
  189. }
  190. static int spufs_cntl_open(struct inode *inode, struct file *file)
  191. {
  192. struct spufs_inode_info *i = SPUFS_I(inode);
  193. struct spu_context *ctx = i->i_ctx;
  194. file->private_data = ctx;
  195. file->f_mapping = inode->i_mapping;
  196. ctx->cntl = inode->i_mapping;
  197. return simple_attr_open(inode, file, spufs_cntl_get,
  198. spufs_cntl_set, "0x%08lx");
  199. }
  200. static struct file_operations spufs_cntl_fops = {
  201. .open = spufs_cntl_open,
  202. .release = simple_attr_close,
  203. .read = simple_attr_read,
  204. .write = simple_attr_write,
  205. .mmap = spufs_cntl_mmap,
  206. };
  207. static int
  208. spufs_regs_open(struct inode *inode, struct file *file)
  209. {
  210. struct spufs_inode_info *i = SPUFS_I(inode);
  211. file->private_data = i->i_ctx;
  212. return 0;
  213. }
  214. static ssize_t
  215. spufs_regs_read(struct file *file, char __user *buffer,
  216. size_t size, loff_t *pos)
  217. {
  218. struct spu_context *ctx = file->private_data;
  219. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  220. int ret;
  221. spu_acquire_saved(ctx);
  222. ret = simple_read_from_buffer(buffer, size, pos,
  223. lscsa->gprs, sizeof lscsa->gprs);
  224. spu_release(ctx);
  225. return ret;
  226. }
  227. static ssize_t
  228. spufs_regs_write(struct file *file, const char __user *buffer,
  229. size_t size, loff_t *pos)
  230. {
  231. struct spu_context *ctx = file->private_data;
  232. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  233. int ret;
  234. size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
  235. if (size <= 0)
  236. return -EFBIG;
  237. *pos += size;
  238. spu_acquire_saved(ctx);
  239. ret = copy_from_user(lscsa->gprs + *pos - size,
  240. buffer, size) ? -EFAULT : size;
  241. spu_release(ctx);
  242. return ret;
  243. }
  244. static struct file_operations spufs_regs_fops = {
  245. .open = spufs_regs_open,
  246. .read = spufs_regs_read,
  247. .write = spufs_regs_write,
  248. .llseek = generic_file_llseek,
  249. };
  250. static ssize_t
  251. spufs_fpcr_read(struct file *file, char __user * buffer,
  252. size_t size, loff_t * pos)
  253. {
  254. struct spu_context *ctx = file->private_data;
  255. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  256. int ret;
  257. spu_acquire_saved(ctx);
  258. ret = simple_read_from_buffer(buffer, size, pos,
  259. &lscsa->fpcr, sizeof(lscsa->fpcr));
  260. spu_release(ctx);
  261. return ret;
  262. }
  263. static ssize_t
  264. spufs_fpcr_write(struct file *file, const char __user * buffer,
  265. size_t size, loff_t * pos)
  266. {
  267. struct spu_context *ctx = file->private_data;
  268. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  269. int ret;
  270. size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
  271. if (size <= 0)
  272. return -EFBIG;
  273. *pos += size;
  274. spu_acquire_saved(ctx);
  275. ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
  276. buffer, size) ? -EFAULT : size;
  277. spu_release(ctx);
  278. return ret;
  279. }
  280. static struct file_operations spufs_fpcr_fops = {
  281. .open = spufs_regs_open,
  282. .read = spufs_fpcr_read,
  283. .write = spufs_fpcr_write,
  284. .llseek = generic_file_llseek,
  285. };
  286. /* generic open function for all pipe-like files */
  287. static int spufs_pipe_open(struct inode *inode, struct file *file)
  288. {
  289. struct spufs_inode_info *i = SPUFS_I(inode);
  290. file->private_data = i->i_ctx;
  291. return nonseekable_open(inode, file);
  292. }
  293. /*
  294. * Read as many bytes from the mailbox as possible, until
  295. * one of the conditions becomes true:
  296. *
  297. * - no more data available in the mailbox
  298. * - end of the user provided buffer
  299. * - end of the mapped area
  300. */
  301. static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
  302. size_t len, loff_t *pos)
  303. {
  304. struct spu_context *ctx = file->private_data;
  305. u32 mbox_data, __user *udata;
  306. ssize_t count;
  307. if (len < 4)
  308. return -EINVAL;
  309. if (!access_ok(VERIFY_WRITE, buf, len))
  310. return -EFAULT;
  311. udata = (void __user *)buf;
  312. spu_acquire(ctx);
  313. for (count = 0; count <= len; count += 4, udata++) {
  314. int ret;
  315. ret = ctx->ops->mbox_read(ctx, &mbox_data);
  316. if (ret == 0)
  317. break;
  318. /*
  319. * at the end of the mapped area, we can fault
  320. * but still need to return the data we have
  321. * read successfully so far.
  322. */
  323. ret = __put_user(mbox_data, udata);
  324. if (ret) {
  325. if (!count)
  326. count = -EFAULT;
  327. break;
  328. }
  329. }
  330. spu_release(ctx);
  331. if (!count)
  332. count = -EAGAIN;
  333. return count;
  334. }
  335. static struct file_operations spufs_mbox_fops = {
  336. .open = spufs_pipe_open,
  337. .read = spufs_mbox_read,
  338. };
  339. static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
  340. size_t len, loff_t *pos)
  341. {
  342. struct spu_context *ctx = file->private_data;
  343. u32 mbox_stat;
  344. if (len < 4)
  345. return -EINVAL;
  346. spu_acquire(ctx);
  347. mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
  348. spu_release(ctx);
  349. if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
  350. return -EFAULT;
  351. return 4;
  352. }
  353. static struct file_operations spufs_mbox_stat_fops = {
  354. .open = spufs_pipe_open,
  355. .read = spufs_mbox_stat_read,
  356. };
  357. /* low-level ibox access function */
  358. size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
  359. {
  360. return ctx->ops->ibox_read(ctx, data);
  361. }
  362. static int spufs_ibox_fasync(int fd, struct file *file, int on)
  363. {
  364. struct spu_context *ctx = file->private_data;
  365. return fasync_helper(fd, file, on, &ctx->ibox_fasync);
  366. }
  367. /* interrupt-level ibox callback function. */
  368. void spufs_ibox_callback(struct spu *spu)
  369. {
  370. struct spu_context *ctx = spu->ctx;
  371. wake_up_all(&ctx->ibox_wq);
  372. kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
  373. }
  374. /*
  375. * Read as many bytes from the interrupt mailbox as possible, until
  376. * one of the conditions becomes true:
  377. *
  378. * - no more data available in the mailbox
  379. * - end of the user provided buffer
  380. * - end of the mapped area
  381. *
  382. * If the file is opened without O_NONBLOCK, we wait here until
  383. * any data is available, but return when we have been able to
  384. * read something.
  385. */
  386. static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
  387. size_t len, loff_t *pos)
  388. {
  389. struct spu_context *ctx = file->private_data;
  390. u32 ibox_data, __user *udata;
  391. ssize_t count;
  392. if (len < 4)
  393. return -EINVAL;
  394. if (!access_ok(VERIFY_WRITE, buf, len))
  395. return -EFAULT;
  396. udata = (void __user *)buf;
  397. spu_acquire(ctx);
  398. /* wait only for the first element */
  399. count = 0;
  400. if (file->f_flags & O_NONBLOCK) {
  401. if (!spu_ibox_read(ctx, &ibox_data))
  402. count = -EAGAIN;
  403. } else {
  404. count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
  405. }
  406. if (count)
  407. goto out;
  408. /* if we can't write at all, return -EFAULT */
  409. count = __put_user(ibox_data, udata);
  410. if (count)
  411. goto out;
  412. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  413. int ret;
  414. ret = ctx->ops->ibox_read(ctx, &ibox_data);
  415. if (ret == 0)
  416. break;
  417. /*
  418. * at the end of the mapped area, we can fault
  419. * but still need to return the data we have
  420. * read successfully so far.
  421. */
  422. ret = __put_user(ibox_data, udata);
  423. if (ret)
  424. break;
  425. }
  426. out:
  427. spu_release(ctx);
  428. return count;
  429. }
  430. static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
  431. {
  432. struct spu_context *ctx = file->private_data;
  433. unsigned int mask;
  434. poll_wait(file, &ctx->ibox_wq, wait);
  435. spu_acquire(ctx);
  436. mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
  437. spu_release(ctx);
  438. return mask;
  439. }
  440. static struct file_operations spufs_ibox_fops = {
  441. .open = spufs_pipe_open,
  442. .read = spufs_ibox_read,
  443. .poll = spufs_ibox_poll,
  444. .fasync = spufs_ibox_fasync,
  445. };
  446. static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
  447. size_t len, loff_t *pos)
  448. {
  449. struct spu_context *ctx = file->private_data;
  450. u32 ibox_stat;
  451. if (len < 4)
  452. return -EINVAL;
  453. spu_acquire(ctx);
  454. ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
  455. spu_release(ctx);
  456. if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
  457. return -EFAULT;
  458. return 4;
  459. }
  460. static struct file_operations spufs_ibox_stat_fops = {
  461. .open = spufs_pipe_open,
  462. .read = spufs_ibox_stat_read,
  463. };
  464. /* low-level mailbox write */
  465. size_t spu_wbox_write(struct spu_context *ctx, u32 data)
  466. {
  467. return ctx->ops->wbox_write(ctx, data);
  468. }
  469. static int spufs_wbox_fasync(int fd, struct file *file, int on)
  470. {
  471. struct spu_context *ctx = file->private_data;
  472. int ret;
  473. ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
  474. return ret;
  475. }
  476. /* interrupt-level wbox callback function. */
  477. void spufs_wbox_callback(struct spu *spu)
  478. {
  479. struct spu_context *ctx = spu->ctx;
  480. wake_up_all(&ctx->wbox_wq);
  481. kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
  482. }
  483. /*
  484. * Write as many bytes to the interrupt mailbox as possible, until
  485. * one of the conditions becomes true:
  486. *
  487. * - the mailbox is full
  488. * - end of the user provided buffer
  489. * - end of the mapped area
  490. *
  491. * If the file is opened without O_NONBLOCK, we wait here until
  492. * space is availabyl, but return when we have been able to
  493. * write something.
  494. */
  495. static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
  496. size_t len, loff_t *pos)
  497. {
  498. struct spu_context *ctx = file->private_data;
  499. u32 wbox_data, __user *udata;
  500. ssize_t count;
  501. if (len < 4)
  502. return -EINVAL;
  503. udata = (void __user *)buf;
  504. if (!access_ok(VERIFY_READ, buf, len))
  505. return -EFAULT;
  506. if (__get_user(wbox_data, udata))
  507. return -EFAULT;
  508. spu_acquire(ctx);
  509. /*
  510. * make sure we can at least write one element, by waiting
  511. * in case of !O_NONBLOCK
  512. */
  513. count = 0;
  514. if (file->f_flags & O_NONBLOCK) {
  515. if (!spu_wbox_write(ctx, wbox_data))
  516. count = -EAGAIN;
  517. } else {
  518. count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
  519. }
  520. if (count)
  521. goto out;
  522. /* write aѕ much as possible */
  523. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  524. int ret;
  525. ret = __get_user(wbox_data, udata);
  526. if (ret)
  527. break;
  528. ret = spu_wbox_write(ctx, wbox_data);
  529. if (ret == 0)
  530. break;
  531. }
  532. out:
  533. spu_release(ctx);
  534. return count;
  535. }
  536. static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
  537. {
  538. struct spu_context *ctx = file->private_data;
  539. unsigned int mask;
  540. poll_wait(file, &ctx->wbox_wq, wait);
  541. spu_acquire(ctx);
  542. mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
  543. spu_release(ctx);
  544. return mask;
  545. }
  546. static struct file_operations spufs_wbox_fops = {
  547. .open = spufs_pipe_open,
  548. .write = spufs_wbox_write,
  549. .poll = spufs_wbox_poll,
  550. .fasync = spufs_wbox_fasync,
  551. };
  552. static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
  553. size_t len, loff_t *pos)
  554. {
  555. struct spu_context *ctx = file->private_data;
  556. u32 wbox_stat;
  557. if (len < 4)
  558. return -EINVAL;
  559. spu_acquire(ctx);
  560. wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
  561. spu_release(ctx);
  562. if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
  563. return -EFAULT;
  564. return 4;
  565. }
  566. static struct file_operations spufs_wbox_stat_fops = {
  567. .open = spufs_pipe_open,
  568. .read = spufs_wbox_stat_read,
  569. };
  570. static int spufs_signal1_open(struct inode *inode, struct file *file)
  571. {
  572. struct spufs_inode_info *i = SPUFS_I(inode);
  573. struct spu_context *ctx = i->i_ctx;
  574. file->private_data = ctx;
  575. file->f_mapping = inode->i_mapping;
  576. ctx->signal1 = inode->i_mapping;
  577. return nonseekable_open(inode, file);
  578. }
  579. static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
  580. size_t len, loff_t *pos)
  581. {
  582. struct spu_context *ctx = file->private_data;
  583. u32 data;
  584. if (len < 4)
  585. return -EINVAL;
  586. spu_acquire(ctx);
  587. data = ctx->ops->signal1_read(ctx);
  588. spu_release(ctx);
  589. if (copy_to_user(buf, &data, 4))
  590. return -EFAULT;
  591. return 4;
  592. }
  593. static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
  594. size_t len, loff_t *pos)
  595. {
  596. struct spu_context *ctx;
  597. u32 data;
  598. ctx = file->private_data;
  599. if (len < 4)
  600. return -EINVAL;
  601. if (copy_from_user(&data, buf, 4))
  602. return -EFAULT;
  603. spu_acquire(ctx);
  604. ctx->ops->signal1_write(ctx, data);
  605. spu_release(ctx);
  606. return 4;
  607. }
  608. static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
  609. unsigned long address, int *type)
  610. {
  611. #if PAGE_SIZE == 0x1000
  612. return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000);
  613. #elif PAGE_SIZE == 0x10000
  614. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  615. * signal 1 and 2 area
  616. */
  617. return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
  618. #else
  619. #error unsupported page size
  620. #endif
  621. }
  622. static struct vm_operations_struct spufs_signal1_mmap_vmops = {
  623. .nopage = spufs_signal1_mmap_nopage,
  624. };
  625. static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
  626. {
  627. if (!(vma->vm_flags & VM_SHARED))
  628. return -EINVAL;
  629. vma->vm_flags |= VM_RESERVED;
  630. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  631. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  632. vma->vm_ops = &spufs_signal1_mmap_vmops;
  633. return 0;
  634. }
  635. static struct file_operations spufs_signal1_fops = {
  636. .open = spufs_signal1_open,
  637. .read = spufs_signal1_read,
  638. .write = spufs_signal1_write,
  639. .mmap = spufs_signal1_mmap,
  640. };
  641. static int spufs_signal2_open(struct inode *inode, struct file *file)
  642. {
  643. struct spufs_inode_info *i = SPUFS_I(inode);
  644. struct spu_context *ctx = i->i_ctx;
  645. file->private_data = ctx;
  646. file->f_mapping = inode->i_mapping;
  647. ctx->signal2 = inode->i_mapping;
  648. return nonseekable_open(inode, file);
  649. }
  650. static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
  651. size_t len, loff_t *pos)
  652. {
  653. struct spu_context *ctx;
  654. u32 data;
  655. ctx = file->private_data;
  656. if (len < 4)
  657. return -EINVAL;
  658. spu_acquire(ctx);
  659. data = ctx->ops->signal2_read(ctx);
  660. spu_release(ctx);
  661. if (copy_to_user(buf, &data, 4))
  662. return -EFAULT;
  663. return 4;
  664. }
  665. static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
  666. size_t len, loff_t *pos)
  667. {
  668. struct spu_context *ctx;
  669. u32 data;
  670. ctx = file->private_data;
  671. if (len < 4)
  672. return -EINVAL;
  673. if (copy_from_user(&data, buf, 4))
  674. return -EFAULT;
  675. spu_acquire(ctx);
  676. ctx->ops->signal2_write(ctx, data);
  677. spu_release(ctx);
  678. return 4;
  679. }
  680. #if SPUFS_MMAP_4K
  681. static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
  682. unsigned long address, int *type)
  683. {
  684. #if PAGE_SIZE == 0x1000
  685. return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000);
  686. #elif PAGE_SIZE == 0x10000
  687. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  688. * signal 1 and 2 area
  689. */
  690. return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
  691. #else
  692. #error unsupported page size
  693. #endif
  694. }
  695. static struct vm_operations_struct spufs_signal2_mmap_vmops = {
  696. .nopage = spufs_signal2_mmap_nopage,
  697. };
  698. static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
  699. {
  700. if (!(vma->vm_flags & VM_SHARED))
  701. return -EINVAL;
  702. /* FIXME: */
  703. vma->vm_flags |= VM_RESERVED;
  704. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  705. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  706. vma->vm_ops = &spufs_signal2_mmap_vmops;
  707. return 0;
  708. }
  709. #else /* SPUFS_MMAP_4K */
  710. #define spufs_signal2_mmap NULL
  711. #endif /* !SPUFS_MMAP_4K */
  712. static struct file_operations spufs_signal2_fops = {
  713. .open = spufs_signal2_open,
  714. .read = spufs_signal2_read,
  715. .write = spufs_signal2_write,
  716. .mmap = spufs_signal2_mmap,
  717. };
  718. static void spufs_signal1_type_set(void *data, u64 val)
  719. {
  720. struct spu_context *ctx = data;
  721. spu_acquire(ctx);
  722. ctx->ops->signal1_type_set(ctx, val);
  723. spu_release(ctx);
  724. }
  725. static u64 spufs_signal1_type_get(void *data)
  726. {
  727. struct spu_context *ctx = data;
  728. u64 ret;
  729. spu_acquire(ctx);
  730. ret = ctx->ops->signal1_type_get(ctx);
  731. spu_release(ctx);
  732. return ret;
  733. }
  734. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  735. spufs_signal1_type_set, "%llu");
  736. static void spufs_signal2_type_set(void *data, u64 val)
  737. {
  738. struct spu_context *ctx = data;
  739. spu_acquire(ctx);
  740. ctx->ops->signal2_type_set(ctx, val);
  741. spu_release(ctx);
  742. }
  743. static u64 spufs_signal2_type_get(void *data)
  744. {
  745. struct spu_context *ctx = data;
  746. u64 ret;
  747. spu_acquire(ctx);
  748. ret = ctx->ops->signal2_type_get(ctx);
  749. spu_release(ctx);
  750. return ret;
  751. }
  752. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  753. spufs_signal2_type_set, "%llu");
  754. #if SPUFS_MMAP_4K
  755. static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
  756. unsigned long address, int *type)
  757. {
  758. return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
  759. }
  760. static struct vm_operations_struct spufs_mss_mmap_vmops = {
  761. .nopage = spufs_mss_mmap_nopage,
  762. };
  763. /*
  764. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  765. */
  766. static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
  767. {
  768. if (!(vma->vm_flags & VM_SHARED))
  769. return -EINVAL;
  770. vma->vm_flags |= VM_RESERVED;
  771. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  772. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  773. vma->vm_ops = &spufs_mss_mmap_vmops;
  774. return 0;
  775. }
  776. #else /* SPUFS_MMAP_4K */
  777. #define spufs_mss_mmap NULL
  778. #endif /* !SPUFS_MMAP_4K */
  779. static int spufs_mss_open(struct inode *inode, struct file *file)
  780. {
  781. struct spufs_inode_info *i = SPUFS_I(inode);
  782. file->private_data = i->i_ctx;
  783. return nonseekable_open(inode, file);
  784. }
  785. static struct file_operations spufs_mss_fops = {
  786. .open = spufs_mss_open,
  787. .mmap = spufs_mss_mmap,
  788. };
  789. static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
  790. unsigned long address, int *type)
  791. {
  792. return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
  793. }
  794. static struct vm_operations_struct spufs_psmap_mmap_vmops = {
  795. .nopage = spufs_psmap_mmap_nopage,
  796. };
  797. /*
  798. * mmap support for full problem state area [0x00000 - 0x1ffff].
  799. */
  800. static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
  801. {
  802. if (!(vma->vm_flags & VM_SHARED))
  803. return -EINVAL;
  804. vma->vm_flags |= VM_RESERVED;
  805. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  806. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  807. vma->vm_ops = &spufs_psmap_mmap_vmops;
  808. return 0;
  809. }
  810. static int spufs_psmap_open(struct inode *inode, struct file *file)
  811. {
  812. struct spufs_inode_info *i = SPUFS_I(inode);
  813. file->private_data = i->i_ctx;
  814. return nonseekable_open(inode, file);
  815. }
  816. static struct file_operations spufs_psmap_fops = {
  817. .open = spufs_psmap_open,
  818. .mmap = spufs_psmap_mmap,
  819. };
  820. #if SPUFS_MMAP_4K
  821. static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
  822. unsigned long address, int *type)
  823. {
  824. return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
  825. }
  826. static struct vm_operations_struct spufs_mfc_mmap_vmops = {
  827. .nopage = spufs_mfc_mmap_nopage,
  828. };
  829. /*
  830. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  831. */
  832. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  833. {
  834. if (!(vma->vm_flags & VM_SHARED))
  835. return -EINVAL;
  836. vma->vm_flags |= VM_RESERVED;
  837. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  838. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  839. vma->vm_ops = &spufs_mfc_mmap_vmops;
  840. return 0;
  841. }
  842. #else /* SPUFS_MMAP_4K */
  843. #define spufs_mfc_mmap NULL
  844. #endif /* !SPUFS_MMAP_4K */
  845. static int spufs_mfc_open(struct inode *inode, struct file *file)
  846. {
  847. struct spufs_inode_info *i = SPUFS_I(inode);
  848. struct spu_context *ctx = i->i_ctx;
  849. /* we don't want to deal with DMA into other processes */
  850. if (ctx->owner != current->mm)
  851. return -EINVAL;
  852. if (atomic_read(&inode->i_count) != 1)
  853. return -EBUSY;
  854. file->private_data = ctx;
  855. return nonseekable_open(inode, file);
  856. }
  857. /* interrupt-level mfc callback function. */
  858. void spufs_mfc_callback(struct spu *spu)
  859. {
  860. struct spu_context *ctx = spu->ctx;
  861. wake_up_all(&ctx->mfc_wq);
  862. pr_debug("%s %s\n", __FUNCTION__, spu->name);
  863. if (ctx->mfc_fasync) {
  864. u32 free_elements, tagstatus;
  865. unsigned int mask;
  866. /* no need for spu_acquire in interrupt context */
  867. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  868. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  869. mask = 0;
  870. if (free_elements & 0xffff)
  871. mask |= POLLOUT;
  872. if (tagstatus & ctx->tagwait)
  873. mask |= POLLIN;
  874. kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
  875. }
  876. }
  877. static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
  878. {
  879. /* See if there is one tag group is complete */
  880. /* FIXME we need locking around tagwait */
  881. *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
  882. ctx->tagwait &= ~*status;
  883. if (*status)
  884. return 1;
  885. /* enable interrupt waiting for any tag group,
  886. may silently fail if interrupts are already enabled */
  887. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  888. return 0;
  889. }
  890. static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
  891. size_t size, loff_t *pos)
  892. {
  893. struct spu_context *ctx = file->private_data;
  894. int ret = -EINVAL;
  895. u32 status;
  896. if (size != 4)
  897. goto out;
  898. spu_acquire(ctx);
  899. if (file->f_flags & O_NONBLOCK) {
  900. status = ctx->ops->read_mfc_tagstatus(ctx);
  901. if (!(status & ctx->tagwait))
  902. ret = -EAGAIN;
  903. else
  904. ctx->tagwait &= ~status;
  905. } else {
  906. ret = spufs_wait(ctx->mfc_wq,
  907. spufs_read_mfc_tagstatus(ctx, &status));
  908. }
  909. spu_release(ctx);
  910. if (ret)
  911. goto out;
  912. ret = 4;
  913. if (copy_to_user(buffer, &status, 4))
  914. ret = -EFAULT;
  915. out:
  916. return ret;
  917. }
  918. static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
  919. {
  920. pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
  921. cmd->ea, cmd->size, cmd->tag, cmd->cmd);
  922. switch (cmd->cmd) {
  923. case MFC_PUT_CMD:
  924. case MFC_PUTF_CMD:
  925. case MFC_PUTB_CMD:
  926. case MFC_GET_CMD:
  927. case MFC_GETF_CMD:
  928. case MFC_GETB_CMD:
  929. break;
  930. default:
  931. pr_debug("invalid DMA opcode %x\n", cmd->cmd);
  932. return -EIO;
  933. }
  934. if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
  935. pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
  936. cmd->ea, cmd->lsa);
  937. return -EIO;
  938. }
  939. switch (cmd->size & 0xf) {
  940. case 1:
  941. break;
  942. case 2:
  943. if (cmd->lsa & 1)
  944. goto error;
  945. break;
  946. case 4:
  947. if (cmd->lsa & 3)
  948. goto error;
  949. break;
  950. case 8:
  951. if (cmd->lsa & 7)
  952. goto error;
  953. break;
  954. case 0:
  955. if (cmd->lsa & 15)
  956. goto error;
  957. break;
  958. error:
  959. default:
  960. pr_debug("invalid DMA alignment %x for size %x\n",
  961. cmd->lsa & 0xf, cmd->size);
  962. return -EIO;
  963. }
  964. if (cmd->size > 16 * 1024) {
  965. pr_debug("invalid DMA size %x\n", cmd->size);
  966. return -EIO;
  967. }
  968. if (cmd->tag & 0xfff0) {
  969. /* we reserve the higher tag numbers for kernel use */
  970. pr_debug("invalid DMA tag\n");
  971. return -EIO;
  972. }
  973. if (cmd->class) {
  974. /* not supported in this version */
  975. pr_debug("invalid DMA class\n");
  976. return -EIO;
  977. }
  978. return 0;
  979. }
  980. static int spu_send_mfc_command(struct spu_context *ctx,
  981. struct mfc_dma_command cmd,
  982. int *error)
  983. {
  984. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  985. if (*error == -EAGAIN) {
  986. /* wait for any tag group to complete
  987. so we have space for the new command */
  988. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  989. /* try again, because the queue might be
  990. empty again */
  991. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  992. if (*error == -EAGAIN)
  993. return 0;
  994. }
  995. return 1;
  996. }
  997. static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
  998. size_t size, loff_t *pos)
  999. {
  1000. struct spu_context *ctx = file->private_data;
  1001. struct mfc_dma_command cmd;
  1002. int ret = -EINVAL;
  1003. if (size != sizeof cmd)
  1004. goto out;
  1005. ret = -EFAULT;
  1006. if (copy_from_user(&cmd, buffer, sizeof cmd))
  1007. goto out;
  1008. ret = spufs_check_valid_dma(&cmd);
  1009. if (ret)
  1010. goto out;
  1011. spu_acquire_runnable(ctx);
  1012. if (file->f_flags & O_NONBLOCK) {
  1013. ret = ctx->ops->send_mfc_command(ctx, &cmd);
  1014. } else {
  1015. int status;
  1016. ret = spufs_wait(ctx->mfc_wq,
  1017. spu_send_mfc_command(ctx, cmd, &status));
  1018. if (status)
  1019. ret = status;
  1020. }
  1021. spu_release(ctx);
  1022. if (ret)
  1023. goto out;
  1024. ctx->tagwait |= 1 << cmd.tag;
  1025. out:
  1026. return ret;
  1027. }
  1028. static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
  1029. {
  1030. struct spu_context *ctx = file->private_data;
  1031. u32 free_elements, tagstatus;
  1032. unsigned int mask;
  1033. spu_acquire(ctx);
  1034. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
  1035. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  1036. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  1037. spu_release(ctx);
  1038. poll_wait(file, &ctx->mfc_wq, wait);
  1039. mask = 0;
  1040. if (free_elements & 0xffff)
  1041. mask |= POLLOUT | POLLWRNORM;
  1042. if (tagstatus & ctx->tagwait)
  1043. mask |= POLLIN | POLLRDNORM;
  1044. pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
  1045. free_elements, tagstatus, ctx->tagwait);
  1046. return mask;
  1047. }
  1048. static int spufs_mfc_flush(struct file *file, fl_owner_t id)
  1049. {
  1050. struct spu_context *ctx = file->private_data;
  1051. int ret;
  1052. spu_acquire(ctx);
  1053. #if 0
  1054. /* this currently hangs */
  1055. ret = spufs_wait(ctx->mfc_wq,
  1056. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
  1057. if (ret)
  1058. goto out;
  1059. ret = spufs_wait(ctx->mfc_wq,
  1060. ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
  1061. out:
  1062. #else
  1063. ret = 0;
  1064. #endif
  1065. spu_release(ctx);
  1066. return ret;
  1067. }
  1068. static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
  1069. int datasync)
  1070. {
  1071. return spufs_mfc_flush(file, NULL);
  1072. }
  1073. static int spufs_mfc_fasync(int fd, struct file *file, int on)
  1074. {
  1075. struct spu_context *ctx = file->private_data;
  1076. return fasync_helper(fd, file, on, &ctx->mfc_fasync);
  1077. }
  1078. static struct file_operations spufs_mfc_fops = {
  1079. .open = spufs_mfc_open,
  1080. .read = spufs_mfc_read,
  1081. .write = spufs_mfc_write,
  1082. .poll = spufs_mfc_poll,
  1083. .flush = spufs_mfc_flush,
  1084. .fsync = spufs_mfc_fsync,
  1085. .fasync = spufs_mfc_fasync,
  1086. .mmap = spufs_mfc_mmap,
  1087. };
  1088. static void spufs_npc_set(void *data, u64 val)
  1089. {
  1090. struct spu_context *ctx = data;
  1091. spu_acquire(ctx);
  1092. ctx->ops->npc_write(ctx, val);
  1093. spu_release(ctx);
  1094. }
  1095. static u64 spufs_npc_get(void *data)
  1096. {
  1097. struct spu_context *ctx = data;
  1098. u64 ret;
  1099. spu_acquire(ctx);
  1100. ret = ctx->ops->npc_read(ctx);
  1101. spu_release(ctx);
  1102. return ret;
  1103. }
  1104. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
  1105. static void spufs_decr_set(void *data, u64 val)
  1106. {
  1107. struct spu_context *ctx = data;
  1108. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1109. spu_acquire_saved(ctx);
  1110. lscsa->decr.slot[0] = (u32) val;
  1111. spu_release(ctx);
  1112. }
  1113. static u64 spufs_decr_get(void *data)
  1114. {
  1115. struct spu_context *ctx = data;
  1116. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1117. u64 ret;
  1118. spu_acquire_saved(ctx);
  1119. ret = lscsa->decr.slot[0];
  1120. spu_release(ctx);
  1121. return ret;
  1122. }
  1123. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  1124. "%llx\n")
  1125. static void spufs_decr_status_set(void *data, u64 val)
  1126. {
  1127. struct spu_context *ctx = data;
  1128. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1129. spu_acquire_saved(ctx);
  1130. lscsa->decr_status.slot[0] = (u32) val;
  1131. spu_release(ctx);
  1132. }
  1133. static u64 spufs_decr_status_get(void *data)
  1134. {
  1135. struct spu_context *ctx = data;
  1136. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1137. u64 ret;
  1138. spu_acquire_saved(ctx);
  1139. ret = lscsa->decr_status.slot[0];
  1140. spu_release(ctx);
  1141. return ret;
  1142. }
  1143. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  1144. spufs_decr_status_set, "%llx\n")
  1145. static void spufs_spu_tag_mask_set(void *data, u64 val)
  1146. {
  1147. struct spu_context *ctx = data;
  1148. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1149. spu_acquire_saved(ctx);
  1150. lscsa->tag_mask.slot[0] = (u32) val;
  1151. spu_release(ctx);
  1152. }
  1153. static u64 spufs_spu_tag_mask_get(void *data)
  1154. {
  1155. struct spu_context *ctx = data;
  1156. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1157. u64 ret;
  1158. spu_acquire_saved(ctx);
  1159. ret = lscsa->tag_mask.slot[0];
  1160. spu_release(ctx);
  1161. return ret;
  1162. }
  1163. DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
  1164. spufs_spu_tag_mask_set, "%llx\n")
  1165. static void spufs_event_mask_set(void *data, u64 val)
  1166. {
  1167. struct spu_context *ctx = data;
  1168. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1169. spu_acquire_saved(ctx);
  1170. lscsa->event_mask.slot[0] = (u32) val;
  1171. spu_release(ctx);
  1172. }
  1173. static u64 spufs_event_mask_get(void *data)
  1174. {
  1175. struct spu_context *ctx = data;
  1176. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1177. u64 ret;
  1178. spu_acquire_saved(ctx);
  1179. ret = lscsa->event_mask.slot[0];
  1180. spu_release(ctx);
  1181. return ret;
  1182. }
  1183. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1184. spufs_event_mask_set, "%llx\n")
  1185. static void spufs_srr0_set(void *data, u64 val)
  1186. {
  1187. struct spu_context *ctx = data;
  1188. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1189. spu_acquire_saved(ctx);
  1190. lscsa->srr0.slot[0] = (u32) val;
  1191. spu_release(ctx);
  1192. }
  1193. static u64 spufs_srr0_get(void *data)
  1194. {
  1195. struct spu_context *ctx = data;
  1196. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1197. u64 ret;
  1198. spu_acquire_saved(ctx);
  1199. ret = lscsa->srr0.slot[0];
  1200. spu_release(ctx);
  1201. return ret;
  1202. }
  1203. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1204. "%llx\n")
  1205. static u64 spufs_id_get(void *data)
  1206. {
  1207. struct spu_context *ctx = data;
  1208. u64 num;
  1209. spu_acquire(ctx);
  1210. if (ctx->state == SPU_STATE_RUNNABLE)
  1211. num = ctx->spu->number;
  1212. else
  1213. num = (unsigned int)-1;
  1214. spu_release(ctx);
  1215. return num;
  1216. }
  1217. DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
  1218. static u64 spufs_object_id_get(void *data)
  1219. {
  1220. struct spu_context *ctx = data;
  1221. return ctx->object_id;
  1222. }
  1223. static void spufs_object_id_set(void *data, u64 id)
  1224. {
  1225. struct spu_context *ctx = data;
  1226. ctx->object_id = id;
  1227. }
  1228. DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
  1229. spufs_object_id_set, "0x%llx\n");
  1230. struct tree_descr spufs_dir_contents[] = {
  1231. { "mem", &spufs_mem_fops, 0666, },
  1232. { "regs", &spufs_regs_fops, 0666, },
  1233. { "mbox", &spufs_mbox_fops, 0444, },
  1234. { "ibox", &spufs_ibox_fops, 0444, },
  1235. { "wbox", &spufs_wbox_fops, 0222, },
  1236. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1237. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1238. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1239. { "signal1", &spufs_signal1_fops, 0666, },
  1240. { "signal2", &spufs_signal2_fops, 0666, },
  1241. { "signal1_type", &spufs_signal1_type, 0666, },
  1242. { "signal2_type", &spufs_signal2_type, 0666, },
  1243. { "mss", &spufs_mss_fops, 0666, },
  1244. { "mfc", &spufs_mfc_fops, 0666, },
  1245. { "cntl", &spufs_cntl_fops, 0666, },
  1246. { "npc", &spufs_npc_ops, 0666, },
  1247. { "fpcr", &spufs_fpcr_fops, 0666, },
  1248. { "decr", &spufs_decr_ops, 0666, },
  1249. { "decr_status", &spufs_decr_status_ops, 0666, },
  1250. { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
  1251. { "event_mask", &spufs_event_mask_ops, 0666, },
  1252. { "srr0", &spufs_srr0_ops, 0666, },
  1253. { "psmap", &spufs_psmap_fops, 0666, },
  1254. { "phys-id", &spufs_id_ops, 0666, },
  1255. { "object-id", &spufs_object_id_ops, 0666, },
  1256. {},
  1257. };