file.c 39 KB

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