file.c 44 KB

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