file.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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 + 4) <= 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 int spufs_recycle_open(struct inode *inode, struct file *file)
  1089. {
  1090. file->private_data = SPUFS_I(inode)->i_ctx;
  1091. return nonseekable_open(inode, file);
  1092. }
  1093. static ssize_t spufs_recycle_write(struct file *file,
  1094. const char __user *buffer, size_t size, loff_t *pos)
  1095. {
  1096. struct spu_context *ctx = file->private_data;
  1097. int ret;
  1098. if (!(ctx->flags & SPU_CREATE_ISOLATE))
  1099. return -EINVAL;
  1100. if (size < 1)
  1101. return -EINVAL;
  1102. ret = spu_recycle_isolated(ctx);
  1103. if (ret)
  1104. return ret;
  1105. return size;
  1106. }
  1107. static struct file_operations spufs_recycle_fops = {
  1108. .open = spufs_recycle_open,
  1109. .write = spufs_recycle_write,
  1110. };
  1111. static void spufs_npc_set(void *data, u64 val)
  1112. {
  1113. struct spu_context *ctx = data;
  1114. spu_acquire(ctx);
  1115. ctx->ops->npc_write(ctx, val);
  1116. spu_release(ctx);
  1117. }
  1118. static u64 spufs_npc_get(void *data)
  1119. {
  1120. struct spu_context *ctx = data;
  1121. u64 ret;
  1122. spu_acquire(ctx);
  1123. ret = ctx->ops->npc_read(ctx);
  1124. spu_release(ctx);
  1125. return ret;
  1126. }
  1127. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
  1128. "0x%llx\n")
  1129. static void spufs_decr_set(void *data, u64 val)
  1130. {
  1131. struct spu_context *ctx = data;
  1132. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1133. spu_acquire_saved(ctx);
  1134. lscsa->decr.slot[0] = (u32) val;
  1135. spu_release(ctx);
  1136. }
  1137. static u64 spufs_decr_get(void *data)
  1138. {
  1139. struct spu_context *ctx = data;
  1140. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1141. u64 ret;
  1142. spu_acquire_saved(ctx);
  1143. ret = lscsa->decr.slot[0];
  1144. spu_release(ctx);
  1145. return ret;
  1146. }
  1147. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  1148. "0x%llx\n")
  1149. static void spufs_decr_status_set(void *data, u64 val)
  1150. {
  1151. struct spu_context *ctx = data;
  1152. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1153. spu_acquire_saved(ctx);
  1154. lscsa->decr_status.slot[0] = (u32) val;
  1155. spu_release(ctx);
  1156. }
  1157. static u64 spufs_decr_status_get(void *data)
  1158. {
  1159. struct spu_context *ctx = data;
  1160. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1161. u64 ret;
  1162. spu_acquire_saved(ctx);
  1163. ret = lscsa->decr_status.slot[0];
  1164. spu_release(ctx);
  1165. return ret;
  1166. }
  1167. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  1168. spufs_decr_status_set, "0x%llx\n")
  1169. static void spufs_spu_tag_mask_set(void *data, u64 val)
  1170. {
  1171. struct spu_context *ctx = data;
  1172. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1173. spu_acquire_saved(ctx);
  1174. lscsa->tag_mask.slot[0] = (u32) val;
  1175. spu_release(ctx);
  1176. }
  1177. static u64 spufs_spu_tag_mask_get(void *data)
  1178. {
  1179. struct spu_context *ctx = data;
  1180. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1181. u64 ret;
  1182. spu_acquire_saved(ctx);
  1183. ret = lscsa->tag_mask.slot[0];
  1184. spu_release(ctx);
  1185. return ret;
  1186. }
  1187. DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
  1188. spufs_spu_tag_mask_set, "0x%llx\n")
  1189. static void spufs_event_mask_set(void *data, u64 val)
  1190. {
  1191. struct spu_context *ctx = data;
  1192. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1193. spu_acquire_saved(ctx);
  1194. lscsa->event_mask.slot[0] = (u32) val;
  1195. spu_release(ctx);
  1196. }
  1197. static u64 spufs_event_mask_get(void *data)
  1198. {
  1199. struct spu_context *ctx = data;
  1200. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1201. u64 ret;
  1202. spu_acquire_saved(ctx);
  1203. ret = lscsa->event_mask.slot[0];
  1204. spu_release(ctx);
  1205. return ret;
  1206. }
  1207. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1208. spufs_event_mask_set, "0x%llx\n")
  1209. static void spufs_srr0_set(void *data, u64 val)
  1210. {
  1211. struct spu_context *ctx = data;
  1212. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1213. spu_acquire_saved(ctx);
  1214. lscsa->srr0.slot[0] = (u32) val;
  1215. spu_release(ctx);
  1216. }
  1217. static u64 spufs_srr0_get(void *data)
  1218. {
  1219. struct spu_context *ctx = data;
  1220. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1221. u64 ret;
  1222. spu_acquire_saved(ctx);
  1223. ret = lscsa->srr0.slot[0];
  1224. spu_release(ctx);
  1225. return ret;
  1226. }
  1227. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1228. "0x%llx\n")
  1229. static u64 spufs_id_get(void *data)
  1230. {
  1231. struct spu_context *ctx = data;
  1232. u64 num;
  1233. spu_acquire(ctx);
  1234. if (ctx->state == SPU_STATE_RUNNABLE)
  1235. num = ctx->spu->number;
  1236. else
  1237. num = (unsigned int)-1;
  1238. spu_release(ctx);
  1239. return num;
  1240. }
  1241. DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
  1242. static u64 spufs_object_id_get(void *data)
  1243. {
  1244. struct spu_context *ctx = data;
  1245. return ctx->object_id;
  1246. }
  1247. static void spufs_object_id_set(void *data, u64 id)
  1248. {
  1249. struct spu_context *ctx = data;
  1250. ctx->object_id = id;
  1251. }
  1252. DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
  1253. spufs_object_id_set, "0x%llx\n");
  1254. struct tree_descr spufs_dir_contents[] = {
  1255. { "mem", &spufs_mem_fops, 0666, },
  1256. { "regs", &spufs_regs_fops, 0666, },
  1257. { "mbox", &spufs_mbox_fops, 0444, },
  1258. { "ibox", &spufs_ibox_fops, 0444, },
  1259. { "wbox", &spufs_wbox_fops, 0222, },
  1260. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1261. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1262. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1263. { "signal1", &spufs_signal1_fops, 0666, },
  1264. { "signal2", &spufs_signal2_fops, 0666, },
  1265. { "signal1_type", &spufs_signal1_type, 0666, },
  1266. { "signal2_type", &spufs_signal2_type, 0666, },
  1267. { "mss", &spufs_mss_fops, 0666, },
  1268. { "mfc", &spufs_mfc_fops, 0666, },
  1269. { "cntl", &spufs_cntl_fops, 0666, },
  1270. { "npc", &spufs_npc_ops, 0666, },
  1271. { "fpcr", &spufs_fpcr_fops, 0666, },
  1272. { "decr", &spufs_decr_ops, 0666, },
  1273. { "decr_status", &spufs_decr_status_ops, 0666, },
  1274. { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
  1275. { "event_mask", &spufs_event_mask_ops, 0666, },
  1276. { "srr0", &spufs_srr0_ops, 0666, },
  1277. { "psmap", &spufs_psmap_fops, 0666, },
  1278. { "phys-id", &spufs_id_ops, 0666, },
  1279. { "object-id", &spufs_object_id_ops, 0666, },
  1280. {},
  1281. };
  1282. struct tree_descr spufs_dir_nosched_contents[] = {
  1283. { "mem", &spufs_mem_fops, 0666, },
  1284. { "mbox", &spufs_mbox_fops, 0444, },
  1285. { "ibox", &spufs_ibox_fops, 0444, },
  1286. { "wbox", &spufs_wbox_fops, 0222, },
  1287. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1288. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1289. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1290. { "signal1", &spufs_signal1_fops, 0666, },
  1291. { "signal2", &spufs_signal2_fops, 0666, },
  1292. { "signal1_type", &spufs_signal1_type, 0666, },
  1293. { "signal2_type", &spufs_signal2_type, 0666, },
  1294. { "mss", &spufs_mss_fops, 0666, },
  1295. { "mfc", &spufs_mfc_fops, 0666, },
  1296. { "cntl", &spufs_cntl_fops, 0666, },
  1297. { "npc", &spufs_npc_ops, 0666, },
  1298. { "psmap", &spufs_psmap_fops, 0666, },
  1299. { "phys-id", &spufs_id_ops, 0666, },
  1300. { "object-id", &spufs_object_id_ops, 0666, },
  1301. { "recycle", &spufs_recycle_fops, 0222, },
  1302. {},
  1303. };