file.c 41 KB

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