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. file->f_mapping = inode->i_mapping;
  43. ctx->local_store = inode->i_mapping;
  44. return 0;
  45. }
  46. static ssize_t
  47. __spufs_mem_read(struct 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 struct page *
  84. spufs_mem_mmap_nopage(struct vm_area_struct *vma,
  85. unsigned long address, int *type)
  86. {
  87. struct page *page = NOPAGE_SIGBUS;
  88. struct spu_context *ctx = vma->vm_file->private_data;
  89. unsigned long offset = address - vma->vm_start;
  90. offset += vma->vm_pgoff << PAGE_SHIFT;
  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. page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
  96. } else {
  97. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  98. | _PAGE_NO_CACHE);
  99. page = pfn_to_page((ctx->spu->local_store_phys + offset)
  100. >> PAGE_SHIFT);
  101. }
  102. spu_release(ctx);
  103. if (type)
  104. *type = VM_FAULT_MINOR;
  105. page_cache_get(page);
  106. return page;
  107. }
  108. static struct vm_operations_struct spufs_mem_mmap_vmops = {
  109. .nopage = spufs_mem_mmap_nopage,
  110. };
  111. static int
  112. spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
  113. {
  114. if (!(vma->vm_flags & VM_SHARED))
  115. return -EINVAL;
  116. vma->vm_flags |= VM_IO;
  117. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  118. | _PAGE_NO_CACHE);
  119. vma->vm_ops = &spufs_mem_mmap_vmops;
  120. return 0;
  121. }
  122. static struct file_operations spufs_mem_fops = {
  123. .open = spufs_mem_open,
  124. .read = spufs_mem_read,
  125. .write = spufs_mem_write,
  126. .llseek = generic_file_llseek,
  127. .mmap = spufs_mem_mmap,
  128. };
  129. static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
  130. unsigned long address,
  131. int *type, unsigned long ps_offs,
  132. unsigned long ps_size)
  133. {
  134. struct page *page = NOPAGE_SIGBUS;
  135. int fault_type = VM_FAULT_SIGBUS;
  136. struct spu_context *ctx = vma->vm_file->private_data;
  137. unsigned long offset = address - vma->vm_start;
  138. unsigned long area;
  139. int ret;
  140. offset += vma->vm_pgoff << PAGE_SHIFT;
  141. if (offset >= ps_size)
  142. goto out;
  143. ret = spu_acquire_runnable(ctx);
  144. if (ret)
  145. goto out;
  146. area = ctx->spu->problem_phys + ps_offs;
  147. page = pfn_to_page((area + offset) >> PAGE_SHIFT);
  148. fault_type = VM_FAULT_MINOR;
  149. page_cache_get(page);
  150. spu_release(ctx);
  151. out:
  152. if (type)
  153. *type = fault_type;
  154. return page;
  155. }
  156. #if SPUFS_MMAP_4K
  157. static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
  158. unsigned long address, int *type)
  159. {
  160. return spufs_ps_nopage(vma, address, type, 0x4000, 0x1000);
  161. }
  162. static struct vm_operations_struct spufs_cntl_mmap_vmops = {
  163. .nopage = spufs_cntl_mmap_nopage,
  164. };
  165. /*
  166. * mmap support for problem state control area [0x4000 - 0x4fff].
  167. */
  168. static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
  169. {
  170. if (!(vma->vm_flags & VM_SHARED))
  171. return -EINVAL;
  172. vma->vm_flags |= VM_IO;
  173. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  174. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  175. vma->vm_ops = &spufs_cntl_mmap_vmops;
  176. return 0;
  177. }
  178. #else /* SPUFS_MMAP_4K */
  179. #define spufs_cntl_mmap NULL
  180. #endif /* !SPUFS_MMAP_4K */
  181. static u64 spufs_cntl_get(void *data)
  182. {
  183. struct spu_context *ctx = data;
  184. u64 val;
  185. spu_acquire(ctx);
  186. val = ctx->ops->status_read(ctx);
  187. spu_release(ctx);
  188. return val;
  189. }
  190. static void spufs_cntl_set(void *data, u64 val)
  191. {
  192. struct spu_context *ctx = data;
  193. spu_acquire(ctx);
  194. ctx->ops->runcntl_write(ctx, val);
  195. spu_release(ctx);
  196. }
  197. static int spufs_cntl_open(struct inode *inode, struct file *file)
  198. {
  199. struct spufs_inode_info *i = SPUFS_I(inode);
  200. struct spu_context *ctx = i->i_ctx;
  201. file->private_data = ctx;
  202. file->f_mapping = inode->i_mapping;
  203. ctx->cntl = inode->i_mapping;
  204. return simple_attr_open(inode, file, spufs_cntl_get,
  205. spufs_cntl_set, "0x%08lx");
  206. }
  207. static struct file_operations spufs_cntl_fops = {
  208. .open = spufs_cntl_open,
  209. .release = simple_attr_close,
  210. .read = simple_attr_read,
  211. .write = simple_attr_write,
  212. .mmap = spufs_cntl_mmap,
  213. };
  214. static int
  215. spufs_regs_open(struct inode *inode, struct file *file)
  216. {
  217. struct spufs_inode_info *i = SPUFS_I(inode);
  218. file->private_data = i->i_ctx;
  219. return 0;
  220. }
  221. static ssize_t
  222. __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
  223. size_t size, loff_t *pos)
  224. {
  225. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  226. return simple_read_from_buffer(buffer, size, pos,
  227. lscsa->gprs, sizeof lscsa->gprs);
  228. }
  229. static ssize_t
  230. spufs_regs_read(struct file *file, char __user *buffer,
  231. size_t size, loff_t *pos)
  232. {
  233. int ret;
  234. struct spu_context *ctx = file->private_data;
  235. spu_acquire_saved(ctx);
  236. ret = __spufs_regs_read(ctx, buffer, size, pos);
  237. spu_release(ctx);
  238. return ret;
  239. }
  240. static ssize_t
  241. spufs_regs_write(struct file *file, const char __user *buffer,
  242. size_t size, loff_t *pos)
  243. {
  244. struct spu_context *ctx = file->private_data;
  245. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  246. int ret;
  247. size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
  248. if (size <= 0)
  249. return -EFBIG;
  250. *pos += size;
  251. spu_acquire_saved(ctx);
  252. ret = copy_from_user(lscsa->gprs + *pos - size,
  253. buffer, size) ? -EFAULT : size;
  254. spu_release(ctx);
  255. return ret;
  256. }
  257. static struct file_operations spufs_regs_fops = {
  258. .open = spufs_regs_open,
  259. .read = spufs_regs_read,
  260. .write = spufs_regs_write,
  261. .llseek = generic_file_llseek,
  262. };
  263. static ssize_t
  264. __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
  265. size_t size, loff_t * pos)
  266. {
  267. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  268. return simple_read_from_buffer(buffer, size, pos,
  269. &lscsa->fpcr, sizeof(lscsa->fpcr));
  270. }
  271. static ssize_t
  272. spufs_fpcr_read(struct file *file, char __user * buffer,
  273. size_t size, loff_t * pos)
  274. {
  275. int ret;
  276. struct spu_context *ctx = file->private_data;
  277. spu_acquire_saved(ctx);
  278. ret = __spufs_fpcr_read(ctx, buffer, size, pos);
  279. spu_release(ctx);
  280. return ret;
  281. }
  282. static ssize_t
  283. spufs_fpcr_write(struct file *file, const char __user * buffer,
  284. size_t size, loff_t * pos)
  285. {
  286. struct spu_context *ctx = file->private_data;
  287. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  288. int ret;
  289. size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
  290. if (size <= 0)
  291. return -EFBIG;
  292. *pos += size;
  293. spu_acquire_saved(ctx);
  294. ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
  295. buffer, size) ? -EFAULT : size;
  296. spu_release(ctx);
  297. return ret;
  298. }
  299. static struct file_operations spufs_fpcr_fops = {
  300. .open = spufs_regs_open,
  301. .read = spufs_fpcr_read,
  302. .write = spufs_fpcr_write,
  303. .llseek = generic_file_llseek,
  304. };
  305. /* generic open function for all pipe-like files */
  306. static int spufs_pipe_open(struct inode *inode, struct file *file)
  307. {
  308. struct spufs_inode_info *i = SPUFS_I(inode);
  309. file->private_data = i->i_ctx;
  310. return nonseekable_open(inode, file);
  311. }
  312. /*
  313. * Read as many bytes from the mailbox as possible, until
  314. * one of the conditions becomes true:
  315. *
  316. * - no more data available in the mailbox
  317. * - end of the user provided buffer
  318. * - end of the mapped area
  319. */
  320. static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
  321. size_t len, loff_t *pos)
  322. {
  323. struct spu_context *ctx = file->private_data;
  324. u32 mbox_data, __user *udata;
  325. ssize_t count;
  326. if (len < 4)
  327. return -EINVAL;
  328. if (!access_ok(VERIFY_WRITE, buf, len))
  329. return -EFAULT;
  330. udata = (void __user *)buf;
  331. spu_acquire(ctx);
  332. for (count = 0; (count + 4) <= len; count += 4, udata++) {
  333. int ret;
  334. ret = ctx->ops->mbox_read(ctx, &mbox_data);
  335. if (ret == 0)
  336. break;
  337. /*
  338. * at the end of the mapped area, we can fault
  339. * but still need to return the data we have
  340. * read successfully so far.
  341. */
  342. ret = __put_user(mbox_data, udata);
  343. if (ret) {
  344. if (!count)
  345. count = -EFAULT;
  346. break;
  347. }
  348. }
  349. spu_release(ctx);
  350. if (!count)
  351. count = -EAGAIN;
  352. return count;
  353. }
  354. static struct file_operations spufs_mbox_fops = {
  355. .open = spufs_pipe_open,
  356. .read = spufs_mbox_read,
  357. };
  358. static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
  359. size_t len, loff_t *pos)
  360. {
  361. struct spu_context *ctx = file->private_data;
  362. u32 mbox_stat;
  363. if (len < 4)
  364. return -EINVAL;
  365. spu_acquire(ctx);
  366. mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
  367. spu_release(ctx);
  368. if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
  369. return -EFAULT;
  370. return 4;
  371. }
  372. static struct file_operations spufs_mbox_stat_fops = {
  373. .open = spufs_pipe_open,
  374. .read = spufs_mbox_stat_read,
  375. };
  376. /* low-level ibox access function */
  377. size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
  378. {
  379. return ctx->ops->ibox_read(ctx, data);
  380. }
  381. static int spufs_ibox_fasync(int fd, struct file *file, int on)
  382. {
  383. struct spu_context *ctx = file->private_data;
  384. return fasync_helper(fd, file, on, &ctx->ibox_fasync);
  385. }
  386. /* interrupt-level ibox callback function. */
  387. void spufs_ibox_callback(struct spu *spu)
  388. {
  389. struct spu_context *ctx = spu->ctx;
  390. wake_up_all(&ctx->ibox_wq);
  391. kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
  392. }
  393. /*
  394. * Read as many bytes from the interrupt mailbox as possible, until
  395. * one of the conditions becomes true:
  396. *
  397. * - no more data available in the mailbox
  398. * - end of the user provided buffer
  399. * - end of the mapped area
  400. *
  401. * If the file is opened without O_NONBLOCK, we wait here until
  402. * any data is available, but return when we have been able to
  403. * read something.
  404. */
  405. static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
  406. size_t len, loff_t *pos)
  407. {
  408. struct spu_context *ctx = file->private_data;
  409. u32 ibox_data, __user *udata;
  410. ssize_t count;
  411. if (len < 4)
  412. return -EINVAL;
  413. if (!access_ok(VERIFY_WRITE, buf, len))
  414. return -EFAULT;
  415. udata = (void __user *)buf;
  416. spu_acquire(ctx);
  417. /* wait only for the first element */
  418. count = 0;
  419. if (file->f_flags & O_NONBLOCK) {
  420. if (!spu_ibox_read(ctx, &ibox_data))
  421. count = -EAGAIN;
  422. } else {
  423. count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
  424. }
  425. if (count)
  426. goto out;
  427. /* if we can't write at all, return -EFAULT */
  428. count = __put_user(ibox_data, udata);
  429. if (count)
  430. goto out;
  431. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  432. int ret;
  433. ret = ctx->ops->ibox_read(ctx, &ibox_data);
  434. if (ret == 0)
  435. break;
  436. /*
  437. * at the end of the mapped area, we can fault
  438. * but still need to return the data we have
  439. * read successfully so far.
  440. */
  441. ret = __put_user(ibox_data, udata);
  442. if (ret)
  443. break;
  444. }
  445. out:
  446. spu_release(ctx);
  447. return count;
  448. }
  449. static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
  450. {
  451. struct spu_context *ctx = file->private_data;
  452. unsigned int mask;
  453. poll_wait(file, &ctx->ibox_wq, wait);
  454. spu_acquire(ctx);
  455. mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
  456. spu_release(ctx);
  457. return mask;
  458. }
  459. static struct file_operations spufs_ibox_fops = {
  460. .open = spufs_pipe_open,
  461. .read = spufs_ibox_read,
  462. .poll = spufs_ibox_poll,
  463. .fasync = spufs_ibox_fasync,
  464. };
  465. static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
  466. size_t len, loff_t *pos)
  467. {
  468. struct spu_context *ctx = file->private_data;
  469. u32 ibox_stat;
  470. if (len < 4)
  471. return -EINVAL;
  472. spu_acquire(ctx);
  473. ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
  474. spu_release(ctx);
  475. if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
  476. return -EFAULT;
  477. return 4;
  478. }
  479. static struct file_operations spufs_ibox_stat_fops = {
  480. .open = spufs_pipe_open,
  481. .read = spufs_ibox_stat_read,
  482. };
  483. /* low-level mailbox write */
  484. size_t spu_wbox_write(struct spu_context *ctx, u32 data)
  485. {
  486. return ctx->ops->wbox_write(ctx, data);
  487. }
  488. static int spufs_wbox_fasync(int fd, struct file *file, int on)
  489. {
  490. struct spu_context *ctx = file->private_data;
  491. int ret;
  492. ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
  493. return ret;
  494. }
  495. /* interrupt-level wbox callback function. */
  496. void spufs_wbox_callback(struct spu *spu)
  497. {
  498. struct spu_context *ctx = spu->ctx;
  499. wake_up_all(&ctx->wbox_wq);
  500. kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
  501. }
  502. /*
  503. * Write as many bytes to the interrupt mailbox as possible, until
  504. * one of the conditions becomes true:
  505. *
  506. * - the mailbox is full
  507. * - end of the user provided buffer
  508. * - end of the mapped area
  509. *
  510. * If the file is opened without O_NONBLOCK, we wait here until
  511. * space is availabyl, but return when we have been able to
  512. * write something.
  513. */
  514. static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
  515. size_t len, loff_t *pos)
  516. {
  517. struct spu_context *ctx = file->private_data;
  518. u32 wbox_data, __user *udata;
  519. ssize_t count;
  520. if (len < 4)
  521. return -EINVAL;
  522. udata = (void __user *)buf;
  523. if (!access_ok(VERIFY_READ, buf, len))
  524. return -EFAULT;
  525. if (__get_user(wbox_data, udata))
  526. return -EFAULT;
  527. spu_acquire(ctx);
  528. /*
  529. * make sure we can at least write one element, by waiting
  530. * in case of !O_NONBLOCK
  531. */
  532. count = 0;
  533. if (file->f_flags & O_NONBLOCK) {
  534. if (!spu_wbox_write(ctx, wbox_data))
  535. count = -EAGAIN;
  536. } else {
  537. count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
  538. }
  539. if (count)
  540. goto out;
  541. /* write aѕ much as possible */
  542. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  543. int ret;
  544. ret = __get_user(wbox_data, udata);
  545. if (ret)
  546. break;
  547. ret = spu_wbox_write(ctx, wbox_data);
  548. if (ret == 0)
  549. break;
  550. }
  551. out:
  552. spu_release(ctx);
  553. return count;
  554. }
  555. static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
  556. {
  557. struct spu_context *ctx = file->private_data;
  558. unsigned int mask;
  559. poll_wait(file, &ctx->wbox_wq, wait);
  560. spu_acquire(ctx);
  561. mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
  562. spu_release(ctx);
  563. return mask;
  564. }
  565. static struct file_operations spufs_wbox_fops = {
  566. .open = spufs_pipe_open,
  567. .write = spufs_wbox_write,
  568. .poll = spufs_wbox_poll,
  569. .fasync = spufs_wbox_fasync,
  570. };
  571. static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
  572. size_t len, loff_t *pos)
  573. {
  574. struct spu_context *ctx = file->private_data;
  575. u32 wbox_stat;
  576. if (len < 4)
  577. return -EINVAL;
  578. spu_acquire(ctx);
  579. wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
  580. spu_release(ctx);
  581. if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
  582. return -EFAULT;
  583. return 4;
  584. }
  585. static struct file_operations spufs_wbox_stat_fops = {
  586. .open = spufs_pipe_open,
  587. .read = spufs_wbox_stat_read,
  588. };
  589. static int spufs_signal1_open(struct inode *inode, struct file *file)
  590. {
  591. struct spufs_inode_info *i = SPUFS_I(inode);
  592. struct spu_context *ctx = i->i_ctx;
  593. file->private_data = ctx;
  594. file->f_mapping = inode->i_mapping;
  595. ctx->signal1 = inode->i_mapping;
  596. return nonseekable_open(inode, file);
  597. }
  598. static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
  599. size_t len, loff_t *pos)
  600. {
  601. int ret = 0;
  602. u32 data;
  603. if (len < 4)
  604. return -EINVAL;
  605. if (ctx->csa.spu_chnlcnt_RW[3]) {
  606. data = ctx->csa.spu_chnldata_RW[3];
  607. ret = 4;
  608. }
  609. if (!ret)
  610. goto out;
  611. if (copy_to_user(buf, &data, 4))
  612. return -EFAULT;
  613. out:
  614. return ret;
  615. }
  616. static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
  617. size_t len, loff_t *pos)
  618. {
  619. int ret;
  620. struct spu_context *ctx = file->private_data;
  621. spu_acquire_saved(ctx);
  622. ret = __spufs_signal1_read(ctx, buf, len, pos);
  623. spu_release(ctx);
  624. return ret;
  625. }
  626. static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
  627. size_t len, loff_t *pos)
  628. {
  629. struct spu_context *ctx;
  630. u32 data;
  631. ctx = file->private_data;
  632. if (len < 4)
  633. return -EINVAL;
  634. if (copy_from_user(&data, buf, 4))
  635. return -EFAULT;
  636. spu_acquire(ctx);
  637. ctx->ops->signal1_write(ctx, data);
  638. spu_release(ctx);
  639. return 4;
  640. }
  641. static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
  642. unsigned long address, int *type)
  643. {
  644. #if PAGE_SIZE == 0x1000
  645. return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000);
  646. #elif PAGE_SIZE == 0x10000
  647. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  648. * signal 1 and 2 area
  649. */
  650. return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
  651. #else
  652. #error unsupported page size
  653. #endif
  654. }
  655. static struct vm_operations_struct spufs_signal1_mmap_vmops = {
  656. .nopage = spufs_signal1_mmap_nopage,
  657. };
  658. static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
  659. {
  660. if (!(vma->vm_flags & VM_SHARED))
  661. return -EINVAL;
  662. vma->vm_flags |= VM_IO;
  663. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  664. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  665. vma->vm_ops = &spufs_signal1_mmap_vmops;
  666. return 0;
  667. }
  668. static struct file_operations spufs_signal1_fops = {
  669. .open = spufs_signal1_open,
  670. .read = spufs_signal1_read,
  671. .write = spufs_signal1_write,
  672. .mmap = spufs_signal1_mmap,
  673. };
  674. static int spufs_signal2_open(struct inode *inode, struct file *file)
  675. {
  676. struct spufs_inode_info *i = SPUFS_I(inode);
  677. struct spu_context *ctx = i->i_ctx;
  678. file->private_data = ctx;
  679. file->f_mapping = inode->i_mapping;
  680. ctx->signal2 = inode->i_mapping;
  681. return nonseekable_open(inode, file);
  682. }
  683. static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
  684. size_t len, loff_t *pos)
  685. {
  686. int ret = 0;
  687. u32 data;
  688. if (len < 4)
  689. return -EINVAL;
  690. if (ctx->csa.spu_chnlcnt_RW[4]) {
  691. data = ctx->csa.spu_chnldata_RW[4];
  692. ret = 4;
  693. }
  694. if (!ret)
  695. goto out;
  696. if (copy_to_user(buf, &data, 4))
  697. return -EFAULT;
  698. out:
  699. return ret;
  700. }
  701. static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
  702. size_t len, loff_t *pos)
  703. {
  704. struct spu_context *ctx = file->private_data;
  705. int ret;
  706. spu_acquire_saved(ctx);
  707. ret = __spufs_signal2_read(ctx, buf, len, pos);
  708. spu_release(ctx);
  709. return ret;
  710. }
  711. static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
  712. size_t len, loff_t *pos)
  713. {
  714. struct spu_context *ctx;
  715. u32 data;
  716. ctx = file->private_data;
  717. if (len < 4)
  718. return -EINVAL;
  719. if (copy_from_user(&data, buf, 4))
  720. return -EFAULT;
  721. spu_acquire(ctx);
  722. ctx->ops->signal2_write(ctx, data);
  723. spu_release(ctx);
  724. return 4;
  725. }
  726. #if SPUFS_MMAP_4K
  727. static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
  728. unsigned long address, int *type)
  729. {
  730. #if PAGE_SIZE == 0x1000
  731. return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000);
  732. #elif PAGE_SIZE == 0x10000
  733. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  734. * signal 1 and 2 area
  735. */
  736. return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
  737. #else
  738. #error unsupported page size
  739. #endif
  740. }
  741. static struct vm_operations_struct spufs_signal2_mmap_vmops = {
  742. .nopage = spufs_signal2_mmap_nopage,
  743. };
  744. static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
  745. {
  746. if (!(vma->vm_flags & VM_SHARED))
  747. return -EINVAL;
  748. vma->vm_flags |= VM_IO;
  749. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  750. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  751. vma->vm_ops = &spufs_signal2_mmap_vmops;
  752. return 0;
  753. }
  754. #else /* SPUFS_MMAP_4K */
  755. #define spufs_signal2_mmap NULL
  756. #endif /* !SPUFS_MMAP_4K */
  757. static struct file_operations spufs_signal2_fops = {
  758. .open = spufs_signal2_open,
  759. .read = spufs_signal2_read,
  760. .write = spufs_signal2_write,
  761. .mmap = spufs_signal2_mmap,
  762. };
  763. static void spufs_signal1_type_set(void *data, u64 val)
  764. {
  765. struct spu_context *ctx = data;
  766. spu_acquire(ctx);
  767. ctx->ops->signal1_type_set(ctx, val);
  768. spu_release(ctx);
  769. }
  770. static u64 __spufs_signal1_type_get(void *data)
  771. {
  772. struct spu_context *ctx = data;
  773. return ctx->ops->signal1_type_get(ctx);
  774. }
  775. static u64 spufs_signal1_type_get(void *data)
  776. {
  777. struct spu_context *ctx = data;
  778. u64 ret;
  779. spu_acquire(ctx);
  780. ret = __spufs_signal1_type_get(data);
  781. spu_release(ctx);
  782. return ret;
  783. }
  784. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  785. spufs_signal1_type_set, "%llu");
  786. static void spufs_signal2_type_set(void *data, u64 val)
  787. {
  788. struct spu_context *ctx = data;
  789. spu_acquire(ctx);
  790. ctx->ops->signal2_type_set(ctx, val);
  791. spu_release(ctx);
  792. }
  793. static u64 __spufs_signal2_type_get(void *data)
  794. {
  795. struct spu_context *ctx = data;
  796. return ctx->ops->signal2_type_get(ctx);
  797. }
  798. static u64 spufs_signal2_type_get(void *data)
  799. {
  800. struct spu_context *ctx = data;
  801. u64 ret;
  802. spu_acquire(ctx);
  803. ret = __spufs_signal2_type_get(data);
  804. spu_release(ctx);
  805. return ret;
  806. }
  807. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  808. spufs_signal2_type_set, "%llu");
  809. #if SPUFS_MMAP_4K
  810. static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
  811. unsigned long address, int *type)
  812. {
  813. return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
  814. }
  815. static struct vm_operations_struct spufs_mss_mmap_vmops = {
  816. .nopage = spufs_mss_mmap_nopage,
  817. };
  818. /*
  819. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  820. */
  821. static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
  822. {
  823. if (!(vma->vm_flags & VM_SHARED))
  824. return -EINVAL;
  825. vma->vm_flags |= VM_IO;
  826. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  827. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  828. vma->vm_ops = &spufs_mss_mmap_vmops;
  829. return 0;
  830. }
  831. #else /* SPUFS_MMAP_4K */
  832. #define spufs_mss_mmap NULL
  833. #endif /* !SPUFS_MMAP_4K */
  834. static int spufs_mss_open(struct inode *inode, struct file *file)
  835. {
  836. struct spufs_inode_info *i = SPUFS_I(inode);
  837. file->private_data = i->i_ctx;
  838. return nonseekable_open(inode, file);
  839. }
  840. static struct file_operations spufs_mss_fops = {
  841. .open = spufs_mss_open,
  842. .mmap = spufs_mss_mmap,
  843. };
  844. static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
  845. unsigned long address, int *type)
  846. {
  847. return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
  848. }
  849. static struct vm_operations_struct spufs_psmap_mmap_vmops = {
  850. .nopage = spufs_psmap_mmap_nopage,
  851. };
  852. /*
  853. * mmap support for full problem state area [0x00000 - 0x1ffff].
  854. */
  855. static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
  856. {
  857. if (!(vma->vm_flags & VM_SHARED))
  858. return -EINVAL;
  859. vma->vm_flags |= VM_IO;
  860. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  861. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  862. vma->vm_ops = &spufs_psmap_mmap_vmops;
  863. return 0;
  864. }
  865. static int spufs_psmap_open(struct inode *inode, struct file *file)
  866. {
  867. struct spufs_inode_info *i = SPUFS_I(inode);
  868. file->private_data = i->i_ctx;
  869. return nonseekable_open(inode, file);
  870. }
  871. static struct file_operations spufs_psmap_fops = {
  872. .open = spufs_psmap_open,
  873. .mmap = spufs_psmap_mmap,
  874. };
  875. #if SPUFS_MMAP_4K
  876. static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
  877. unsigned long address, int *type)
  878. {
  879. return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
  880. }
  881. static struct vm_operations_struct spufs_mfc_mmap_vmops = {
  882. .nopage = spufs_mfc_mmap_nopage,
  883. };
  884. /*
  885. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  886. */
  887. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  888. {
  889. if (!(vma->vm_flags & VM_SHARED))
  890. return -EINVAL;
  891. vma->vm_flags |= VM_IO;
  892. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  893. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  894. vma->vm_ops = &spufs_mfc_mmap_vmops;
  895. return 0;
  896. }
  897. #else /* SPUFS_MMAP_4K */
  898. #define spufs_mfc_mmap NULL
  899. #endif /* !SPUFS_MMAP_4K */
  900. static int spufs_mfc_open(struct inode *inode, struct file *file)
  901. {
  902. struct spufs_inode_info *i = SPUFS_I(inode);
  903. struct spu_context *ctx = i->i_ctx;
  904. /* we don't want to deal with DMA into other processes */
  905. if (ctx->owner != current->mm)
  906. return -EINVAL;
  907. if (atomic_read(&inode->i_count) != 1)
  908. return -EBUSY;
  909. file->private_data = ctx;
  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);
  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 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 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 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 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 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 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;