file.c 44 KB

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