file.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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/uaccess.h>
  33. #include "spufs.h"
  34. static int
  35. spufs_mem_open(struct inode *inode, struct file *file)
  36. {
  37. struct spufs_inode_info *i = SPUFS_I(inode);
  38. struct spu_context *ctx = i->i_ctx;
  39. file->private_data = ctx;
  40. file->f_mapping = inode->i_mapping;
  41. ctx->local_store = inode->i_mapping;
  42. return 0;
  43. }
  44. static ssize_t
  45. spufs_mem_read(struct file *file, char __user *buffer,
  46. size_t size, loff_t *pos)
  47. {
  48. struct spu_context *ctx = file->private_data;
  49. char *local_store;
  50. int ret;
  51. spu_acquire(ctx);
  52. local_store = ctx->ops->get_ls(ctx);
  53. ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
  54. spu_release(ctx);
  55. return ret;
  56. }
  57. static ssize_t
  58. spufs_mem_write(struct file *file, const char __user *buffer,
  59. size_t size, loff_t *pos)
  60. {
  61. struct spu_context *ctx = file->private_data;
  62. char *local_store;
  63. int ret;
  64. size = min_t(ssize_t, LS_SIZE - *pos, size);
  65. if (size <= 0)
  66. return -EFBIG;
  67. *pos += size;
  68. spu_acquire(ctx);
  69. local_store = ctx->ops->get_ls(ctx);
  70. ret = copy_from_user(local_store + *pos - size,
  71. buffer, size) ? -EFAULT : size;
  72. spu_release(ctx);
  73. return ret;
  74. }
  75. #ifdef CONFIG_SPUFS_MMAP
  76. static struct page *
  77. spufs_mem_mmap_nopage(struct vm_area_struct *vma,
  78. unsigned long address, int *type)
  79. {
  80. struct page *page = NOPAGE_SIGBUS;
  81. struct spu_context *ctx = vma->vm_file->private_data;
  82. unsigned long offset = address - vma->vm_start;
  83. offset += vma->vm_pgoff << PAGE_SHIFT;
  84. spu_acquire(ctx);
  85. if (ctx->state == SPU_STATE_SAVED)
  86. page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
  87. else
  88. page = pfn_to_page((ctx->spu->local_store_phys + offset)
  89. >> PAGE_SHIFT);
  90. spu_release(ctx);
  91. if (type)
  92. *type = VM_FAULT_MINOR;
  93. page_cache_get(page);
  94. return page;
  95. }
  96. static struct vm_operations_struct spufs_mem_mmap_vmops = {
  97. .nopage = spufs_mem_mmap_nopage,
  98. };
  99. static int
  100. spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
  101. {
  102. if (!(vma->vm_flags & VM_SHARED))
  103. return -EINVAL;
  104. /* FIXME: */
  105. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  106. | _PAGE_NO_CACHE);
  107. vma->vm_ops = &spufs_mem_mmap_vmops;
  108. return 0;
  109. }
  110. #endif
  111. static struct file_operations spufs_mem_fops = {
  112. .open = spufs_mem_open,
  113. .read = spufs_mem_read,
  114. .write = spufs_mem_write,
  115. .llseek = generic_file_llseek,
  116. #ifdef CONFIG_SPUFS_MMAP
  117. .mmap = spufs_mem_mmap,
  118. #endif
  119. };
  120. #ifdef CONFIG_SPUFS_MMAP
  121. static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
  122. unsigned long address,
  123. int *type, unsigned long ps_offs)
  124. {
  125. struct page *page = NOPAGE_SIGBUS;
  126. int fault_type = VM_FAULT_SIGBUS;
  127. struct spu_context *ctx = vma->vm_file->private_data;
  128. unsigned long offset = address - vma->vm_start;
  129. unsigned long area;
  130. int ret;
  131. offset += vma->vm_pgoff << PAGE_SHIFT;
  132. if (offset >= 0x4000)
  133. goto out;
  134. ret = spu_acquire_runnable(ctx);
  135. if (ret)
  136. goto out;
  137. area = ctx->spu->problem_phys + ps_offs;
  138. page = pfn_to_page((area + offset) >> PAGE_SHIFT);
  139. fault_type = VM_FAULT_MINOR;
  140. page_cache_get(page);
  141. spu_release(ctx);
  142. out:
  143. if (type)
  144. *type = fault_type;
  145. return page;
  146. }
  147. static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
  148. unsigned long address, int *type)
  149. {
  150. return spufs_ps_nopage(vma, address, type, 0x4000);
  151. }
  152. static struct vm_operations_struct spufs_cntl_mmap_vmops = {
  153. .nopage = spufs_cntl_mmap_nopage,
  154. };
  155. /*
  156. * mmap support for problem state control area [0x4000 - 0x4fff].
  157. * Mapping this area requires that the application have CAP_SYS_RAWIO,
  158. * as these registers require special care when read/writing.
  159. */
  160. static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
  161. {
  162. if (!(vma->vm_flags & VM_SHARED))
  163. return -EINVAL;
  164. if (!capable(CAP_SYS_RAWIO))
  165. return -EPERM;
  166. vma->vm_flags |= VM_RESERVED;
  167. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  168. | _PAGE_NO_CACHE);
  169. vma->vm_ops = &spufs_cntl_mmap_vmops;
  170. return 0;
  171. }
  172. #endif
  173. static int spufs_cntl_open(struct inode *inode, struct file *file)
  174. {
  175. struct spufs_inode_info *i = SPUFS_I(inode);
  176. struct spu_context *ctx = i->i_ctx;
  177. file->private_data = ctx;
  178. file->f_mapping = inode->i_mapping;
  179. ctx->cntl = inode->i_mapping;
  180. return 0;
  181. }
  182. static ssize_t
  183. spufs_cntl_read(struct file *file, char __user *buffer,
  184. size_t size, loff_t *pos)
  185. {
  186. /* FIXME: read from spu status */
  187. return -EINVAL;
  188. }
  189. static ssize_t
  190. spufs_cntl_write(struct file *file, const char __user *buffer,
  191. size_t size, loff_t *pos)
  192. {
  193. /* FIXME: write to runctl bit */
  194. return -EINVAL;
  195. }
  196. static struct file_operations spufs_cntl_fops = {
  197. .open = spufs_cntl_open,
  198. .read = spufs_cntl_read,
  199. .write = spufs_cntl_write,
  200. #ifdef CONFIG_SPUFS_MMAP
  201. .mmap = spufs_cntl_mmap,
  202. #endif
  203. };
  204. static int
  205. spufs_regs_open(struct inode *inode, struct file *file)
  206. {
  207. struct spufs_inode_info *i = SPUFS_I(inode);
  208. file->private_data = i->i_ctx;
  209. return 0;
  210. }
  211. static ssize_t
  212. spufs_regs_read(struct file *file, char __user *buffer,
  213. size_t size, loff_t *pos)
  214. {
  215. struct spu_context *ctx = file->private_data;
  216. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  217. int ret;
  218. spu_acquire_saved(ctx);
  219. ret = simple_read_from_buffer(buffer, size, pos,
  220. lscsa->gprs, sizeof lscsa->gprs);
  221. spu_release(ctx);
  222. return ret;
  223. }
  224. static ssize_t
  225. spufs_regs_write(struct file *file, const char __user *buffer,
  226. size_t size, loff_t *pos)
  227. {
  228. struct spu_context *ctx = file->private_data;
  229. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  230. int ret;
  231. size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
  232. if (size <= 0)
  233. return -EFBIG;
  234. *pos += size;
  235. spu_acquire_saved(ctx);
  236. ret = copy_from_user(lscsa->gprs + *pos - size,
  237. buffer, size) ? -EFAULT : size;
  238. spu_release(ctx);
  239. return ret;
  240. }
  241. static struct file_operations spufs_regs_fops = {
  242. .open = spufs_regs_open,
  243. .read = spufs_regs_read,
  244. .write = spufs_regs_write,
  245. .llseek = generic_file_llseek,
  246. };
  247. static ssize_t
  248. spufs_fpcr_read(struct file *file, char __user * buffer,
  249. size_t size, loff_t * pos)
  250. {
  251. struct spu_context *ctx = file->private_data;
  252. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  253. int ret;
  254. spu_acquire_saved(ctx);
  255. ret = simple_read_from_buffer(buffer, size, pos,
  256. &lscsa->fpcr, sizeof(lscsa->fpcr));
  257. spu_release(ctx);
  258. return ret;
  259. }
  260. static ssize_t
  261. spufs_fpcr_write(struct file *file, const char __user * buffer,
  262. size_t size, loff_t * pos)
  263. {
  264. struct spu_context *ctx = file->private_data;
  265. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  266. int ret;
  267. size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
  268. if (size <= 0)
  269. return -EFBIG;
  270. *pos += size;
  271. spu_acquire_saved(ctx);
  272. ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
  273. buffer, size) ? -EFAULT : size;
  274. spu_release(ctx);
  275. return ret;
  276. }
  277. static struct file_operations spufs_fpcr_fops = {
  278. .open = spufs_regs_open,
  279. .read = spufs_fpcr_read,
  280. .write = spufs_fpcr_write,
  281. .llseek = generic_file_llseek,
  282. };
  283. /* generic open function for all pipe-like files */
  284. static int spufs_pipe_open(struct inode *inode, struct file *file)
  285. {
  286. struct spufs_inode_info *i = SPUFS_I(inode);
  287. file->private_data = i->i_ctx;
  288. return nonseekable_open(inode, file);
  289. }
  290. static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
  291. size_t len, loff_t *pos)
  292. {
  293. struct spu_context *ctx = file->private_data;
  294. u32 mbox_data;
  295. int ret;
  296. if (len < 4)
  297. return -EINVAL;
  298. spu_acquire(ctx);
  299. ret = ctx->ops->mbox_read(ctx, &mbox_data);
  300. spu_release(ctx);
  301. if (!ret)
  302. return -EAGAIN;
  303. if (copy_to_user(buf, &mbox_data, sizeof mbox_data))
  304. return -EFAULT;
  305. return 4;
  306. }
  307. static struct file_operations spufs_mbox_fops = {
  308. .open = spufs_pipe_open,
  309. .read = spufs_mbox_read,
  310. };
  311. static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
  312. size_t len, loff_t *pos)
  313. {
  314. struct spu_context *ctx = file->private_data;
  315. u32 mbox_stat;
  316. if (len < 4)
  317. return -EINVAL;
  318. spu_acquire(ctx);
  319. mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
  320. spu_release(ctx);
  321. if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
  322. return -EFAULT;
  323. return 4;
  324. }
  325. static struct file_operations spufs_mbox_stat_fops = {
  326. .open = spufs_pipe_open,
  327. .read = spufs_mbox_stat_read,
  328. };
  329. /* low-level ibox access function */
  330. size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
  331. {
  332. return ctx->ops->ibox_read(ctx, data);
  333. }
  334. static int spufs_ibox_fasync(int fd, struct file *file, int on)
  335. {
  336. struct spu_context *ctx = file->private_data;
  337. return fasync_helper(fd, file, on, &ctx->ibox_fasync);
  338. }
  339. /* interrupt-level ibox callback function. */
  340. void spufs_ibox_callback(struct spu *spu)
  341. {
  342. struct spu_context *ctx = spu->ctx;
  343. wake_up_all(&ctx->ibox_wq);
  344. kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
  345. }
  346. static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
  347. size_t len, loff_t *pos)
  348. {
  349. struct spu_context *ctx = file->private_data;
  350. u32 ibox_data;
  351. ssize_t ret;
  352. if (len < 4)
  353. return -EINVAL;
  354. spu_acquire(ctx);
  355. ret = 0;
  356. if (file->f_flags & O_NONBLOCK) {
  357. if (!spu_ibox_read(ctx, &ibox_data))
  358. ret = -EAGAIN;
  359. } else {
  360. ret = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
  361. }
  362. spu_release(ctx);
  363. if (ret)
  364. return ret;
  365. ret = 4;
  366. if (copy_to_user(buf, &ibox_data, sizeof ibox_data))
  367. ret = -EFAULT;
  368. return ret;
  369. }
  370. static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
  371. {
  372. struct spu_context *ctx = file->private_data;
  373. unsigned int mask;
  374. poll_wait(file, &ctx->ibox_wq, wait);
  375. spu_acquire(ctx);
  376. mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
  377. spu_release(ctx);
  378. return mask;
  379. }
  380. static struct file_operations spufs_ibox_fops = {
  381. .open = spufs_pipe_open,
  382. .read = spufs_ibox_read,
  383. .poll = spufs_ibox_poll,
  384. .fasync = spufs_ibox_fasync,
  385. };
  386. static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
  387. size_t len, loff_t *pos)
  388. {
  389. struct spu_context *ctx = file->private_data;
  390. u32 ibox_stat;
  391. if (len < 4)
  392. return -EINVAL;
  393. spu_acquire(ctx);
  394. ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
  395. spu_release(ctx);
  396. if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
  397. return -EFAULT;
  398. return 4;
  399. }
  400. static struct file_operations spufs_ibox_stat_fops = {
  401. .open = spufs_pipe_open,
  402. .read = spufs_ibox_stat_read,
  403. };
  404. /* low-level mailbox write */
  405. size_t spu_wbox_write(struct spu_context *ctx, u32 data)
  406. {
  407. return ctx->ops->wbox_write(ctx, data);
  408. }
  409. static int spufs_wbox_fasync(int fd, struct file *file, int on)
  410. {
  411. struct spu_context *ctx = file->private_data;
  412. int ret;
  413. ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
  414. return ret;
  415. }
  416. /* interrupt-level wbox callback function. */
  417. void spufs_wbox_callback(struct spu *spu)
  418. {
  419. struct spu_context *ctx = spu->ctx;
  420. wake_up_all(&ctx->wbox_wq);
  421. kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
  422. }
  423. static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
  424. size_t len, loff_t *pos)
  425. {
  426. struct spu_context *ctx = file->private_data;
  427. u32 wbox_data;
  428. int ret;
  429. if (len < 4)
  430. return -EINVAL;
  431. if (copy_from_user(&wbox_data, buf, sizeof wbox_data))
  432. return -EFAULT;
  433. spu_acquire(ctx);
  434. ret = 0;
  435. if (file->f_flags & O_NONBLOCK) {
  436. if (!spu_wbox_write(ctx, wbox_data))
  437. ret = -EAGAIN;
  438. } else {
  439. ret = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
  440. }
  441. spu_release(ctx);
  442. return ret ? ret : sizeof wbox_data;
  443. }
  444. static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
  445. {
  446. struct spu_context *ctx = file->private_data;
  447. unsigned int mask;
  448. poll_wait(file, &ctx->wbox_wq, wait);
  449. spu_acquire(ctx);
  450. mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
  451. spu_release(ctx);
  452. return mask;
  453. }
  454. static struct file_operations spufs_wbox_fops = {
  455. .open = spufs_pipe_open,
  456. .write = spufs_wbox_write,
  457. .poll = spufs_wbox_poll,
  458. .fasync = spufs_wbox_fasync,
  459. };
  460. static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
  461. size_t len, loff_t *pos)
  462. {
  463. struct spu_context *ctx = file->private_data;
  464. u32 wbox_stat;
  465. if (len < 4)
  466. return -EINVAL;
  467. spu_acquire(ctx);
  468. wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
  469. spu_release(ctx);
  470. if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
  471. return -EFAULT;
  472. return 4;
  473. }
  474. static struct file_operations spufs_wbox_stat_fops = {
  475. .open = spufs_pipe_open,
  476. .read = spufs_wbox_stat_read,
  477. };
  478. static int spufs_signal1_open(struct inode *inode, struct file *file)
  479. {
  480. struct spufs_inode_info *i = SPUFS_I(inode);
  481. struct spu_context *ctx = i->i_ctx;
  482. file->private_data = ctx;
  483. file->f_mapping = inode->i_mapping;
  484. ctx->signal1 = inode->i_mapping;
  485. return nonseekable_open(inode, file);
  486. }
  487. static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
  488. size_t len, loff_t *pos)
  489. {
  490. struct spu_context *ctx = file->private_data;
  491. u32 data;
  492. if (len < 4)
  493. return -EINVAL;
  494. spu_acquire(ctx);
  495. data = ctx->ops->signal1_read(ctx);
  496. spu_release(ctx);
  497. if (copy_to_user(buf, &data, 4))
  498. return -EFAULT;
  499. return 4;
  500. }
  501. static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
  502. size_t len, loff_t *pos)
  503. {
  504. struct spu_context *ctx;
  505. u32 data;
  506. ctx = file->private_data;
  507. if (len < 4)
  508. return -EINVAL;
  509. if (copy_from_user(&data, buf, 4))
  510. return -EFAULT;
  511. spu_acquire(ctx);
  512. ctx->ops->signal1_write(ctx, data);
  513. spu_release(ctx);
  514. return 4;
  515. }
  516. #ifdef CONFIG_SPUFS_MMAP
  517. static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
  518. unsigned long address, int *type)
  519. {
  520. return spufs_ps_nopage(vma, address, type, 0x14000);
  521. }
  522. static struct vm_operations_struct spufs_signal1_mmap_vmops = {
  523. .nopage = spufs_signal1_mmap_nopage,
  524. };
  525. static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
  526. {
  527. if (!(vma->vm_flags & VM_SHARED))
  528. return -EINVAL;
  529. vma->vm_flags |= VM_RESERVED;
  530. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  531. | _PAGE_NO_CACHE);
  532. vma->vm_ops = &spufs_signal1_mmap_vmops;
  533. return 0;
  534. }
  535. #endif
  536. static struct file_operations spufs_signal1_fops = {
  537. .open = spufs_signal1_open,
  538. .read = spufs_signal1_read,
  539. .write = spufs_signal1_write,
  540. #ifdef CONFIG_SPUFS_MMAP
  541. .mmap = spufs_signal1_mmap,
  542. #endif
  543. };
  544. static int spufs_signal2_open(struct inode *inode, struct file *file)
  545. {
  546. struct spufs_inode_info *i = SPUFS_I(inode);
  547. struct spu_context *ctx = i->i_ctx;
  548. file->private_data = ctx;
  549. file->f_mapping = inode->i_mapping;
  550. ctx->signal2 = inode->i_mapping;
  551. return nonseekable_open(inode, file);
  552. }
  553. static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
  554. size_t len, loff_t *pos)
  555. {
  556. struct spu_context *ctx;
  557. u32 data;
  558. ctx = file->private_data;
  559. if (len < 4)
  560. return -EINVAL;
  561. spu_acquire(ctx);
  562. data = ctx->ops->signal2_read(ctx);
  563. spu_release(ctx);
  564. if (copy_to_user(buf, &data, 4))
  565. return -EFAULT;
  566. return 4;
  567. }
  568. static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
  569. size_t len, loff_t *pos)
  570. {
  571. struct spu_context *ctx;
  572. u32 data;
  573. ctx = file->private_data;
  574. if (len < 4)
  575. return -EINVAL;
  576. if (copy_from_user(&data, buf, 4))
  577. return -EFAULT;
  578. spu_acquire(ctx);
  579. ctx->ops->signal2_write(ctx, data);
  580. spu_release(ctx);
  581. return 4;
  582. }
  583. #ifdef CONFIG_SPUFS_MMAP
  584. static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
  585. unsigned long address, int *type)
  586. {
  587. return spufs_ps_nopage(vma, address, type, 0x1c000);
  588. }
  589. static struct vm_operations_struct spufs_signal2_mmap_vmops = {
  590. .nopage = spufs_signal2_mmap_nopage,
  591. };
  592. static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
  593. {
  594. if (!(vma->vm_flags & VM_SHARED))
  595. return -EINVAL;
  596. /* FIXME: */
  597. vma->vm_flags |= VM_RESERVED;
  598. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  599. | _PAGE_NO_CACHE);
  600. vma->vm_ops = &spufs_signal2_mmap_vmops;
  601. return 0;
  602. }
  603. #endif
  604. static struct file_operations spufs_signal2_fops = {
  605. .open = spufs_signal2_open,
  606. .read = spufs_signal2_read,
  607. .write = spufs_signal2_write,
  608. #ifdef CONFIG_SPUFS_MMAP
  609. .mmap = spufs_signal2_mmap,
  610. #endif
  611. };
  612. static void spufs_signal1_type_set(void *data, u64 val)
  613. {
  614. struct spu_context *ctx = data;
  615. spu_acquire(ctx);
  616. ctx->ops->signal1_type_set(ctx, val);
  617. spu_release(ctx);
  618. }
  619. static u64 spufs_signal1_type_get(void *data)
  620. {
  621. struct spu_context *ctx = data;
  622. u64 ret;
  623. spu_acquire(ctx);
  624. ret = ctx->ops->signal1_type_get(ctx);
  625. spu_release(ctx);
  626. return ret;
  627. }
  628. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  629. spufs_signal1_type_set, "%llu");
  630. static void spufs_signal2_type_set(void *data, u64 val)
  631. {
  632. struct spu_context *ctx = data;
  633. spu_acquire(ctx);
  634. ctx->ops->signal2_type_set(ctx, val);
  635. spu_release(ctx);
  636. }
  637. static u64 spufs_signal2_type_get(void *data)
  638. {
  639. struct spu_context *ctx = data;
  640. u64 ret;
  641. spu_acquire(ctx);
  642. ret = ctx->ops->signal2_type_get(ctx);
  643. spu_release(ctx);
  644. return ret;
  645. }
  646. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  647. spufs_signal2_type_set, "%llu");
  648. #ifdef CONFIG_SPUFS_MMAP
  649. static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
  650. unsigned long address, int *type)
  651. {
  652. return spufs_ps_nopage(vma, address, type, 0x3000);
  653. }
  654. static struct vm_operations_struct spufs_mfc_mmap_vmops = {
  655. .nopage = spufs_mfc_mmap_nopage,
  656. };
  657. /*
  658. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  659. * Mapping this area requires that the application have CAP_SYS_RAWIO,
  660. * as these registers require special care when read/writing.
  661. */
  662. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  663. {
  664. if (!(vma->vm_flags & VM_SHARED))
  665. return -EINVAL;
  666. if (!capable(CAP_SYS_RAWIO))
  667. return -EPERM;
  668. vma->vm_flags |= VM_RESERVED;
  669. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  670. | _PAGE_NO_CACHE);
  671. vma->vm_ops = &spufs_mfc_mmap_vmops;
  672. return 0;
  673. }
  674. #endif
  675. static int spufs_mfc_open(struct inode *inode, struct file *file)
  676. {
  677. struct spufs_inode_info *i = SPUFS_I(inode);
  678. struct spu_context *ctx = i->i_ctx;
  679. /* we don't want to deal with DMA into other processes */
  680. if (ctx->owner != current->mm)
  681. return -EINVAL;
  682. if (atomic_read(&inode->i_count) != 1)
  683. return -EBUSY;
  684. file->private_data = ctx;
  685. return nonseekable_open(inode, file);
  686. }
  687. /* interrupt-level mfc callback function. */
  688. void spufs_mfc_callback(struct spu *spu)
  689. {
  690. struct spu_context *ctx = spu->ctx;
  691. wake_up_all(&ctx->mfc_wq);
  692. pr_debug("%s %s\n", __FUNCTION__, spu->name);
  693. if (ctx->mfc_fasync) {
  694. u32 free_elements, tagstatus;
  695. unsigned int mask;
  696. /* no need for spu_acquire in interrupt context */
  697. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  698. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  699. mask = 0;
  700. if (free_elements & 0xffff)
  701. mask |= POLLOUT;
  702. if (tagstatus & ctx->tagwait)
  703. mask |= POLLIN;
  704. kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
  705. }
  706. }
  707. static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
  708. {
  709. /* See if there is one tag group is complete */
  710. /* FIXME we need locking around tagwait */
  711. *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
  712. ctx->tagwait &= ~*status;
  713. if (*status)
  714. return 1;
  715. /* enable interrupt waiting for any tag group,
  716. may silently fail if interrupts are already enabled */
  717. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  718. return 0;
  719. }
  720. static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
  721. size_t size, loff_t *pos)
  722. {
  723. struct spu_context *ctx = file->private_data;
  724. int ret = -EINVAL;
  725. u32 status;
  726. if (size != 4)
  727. goto out;
  728. spu_acquire(ctx);
  729. if (file->f_flags & O_NONBLOCK) {
  730. status = ctx->ops->read_mfc_tagstatus(ctx);
  731. if (!(status & ctx->tagwait))
  732. ret = -EAGAIN;
  733. else
  734. ctx->tagwait &= ~status;
  735. } else {
  736. ret = spufs_wait(ctx->mfc_wq,
  737. spufs_read_mfc_tagstatus(ctx, &status));
  738. }
  739. spu_release(ctx);
  740. if (ret)
  741. goto out;
  742. ret = 4;
  743. if (copy_to_user(buffer, &status, 4))
  744. ret = -EFAULT;
  745. out:
  746. return ret;
  747. }
  748. static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
  749. {
  750. pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
  751. cmd->ea, cmd->size, cmd->tag, cmd->cmd);
  752. switch (cmd->cmd) {
  753. case MFC_PUT_CMD:
  754. case MFC_PUTF_CMD:
  755. case MFC_PUTB_CMD:
  756. case MFC_GET_CMD:
  757. case MFC_GETF_CMD:
  758. case MFC_GETB_CMD:
  759. break;
  760. default:
  761. pr_debug("invalid DMA opcode %x\n", cmd->cmd);
  762. return -EIO;
  763. }
  764. if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
  765. pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
  766. cmd->ea, cmd->lsa);
  767. return -EIO;
  768. }
  769. switch (cmd->size & 0xf) {
  770. case 1:
  771. break;
  772. case 2:
  773. if (cmd->lsa & 1)
  774. goto error;
  775. break;
  776. case 4:
  777. if (cmd->lsa & 3)
  778. goto error;
  779. break;
  780. case 8:
  781. if (cmd->lsa & 7)
  782. goto error;
  783. break;
  784. case 0:
  785. if (cmd->lsa & 15)
  786. goto error;
  787. break;
  788. error:
  789. default:
  790. pr_debug("invalid DMA alignment %x for size %x\n",
  791. cmd->lsa & 0xf, cmd->size);
  792. return -EIO;
  793. }
  794. if (cmd->size > 16 * 1024) {
  795. pr_debug("invalid DMA size %x\n", cmd->size);
  796. return -EIO;
  797. }
  798. if (cmd->tag & 0xfff0) {
  799. /* we reserve the higher tag numbers for kernel use */
  800. pr_debug("invalid DMA tag\n");
  801. return -EIO;
  802. }
  803. if (cmd->class) {
  804. /* not supported in this version */
  805. pr_debug("invalid DMA class\n");
  806. return -EIO;
  807. }
  808. return 0;
  809. }
  810. static int spu_send_mfc_command(struct spu_context *ctx,
  811. struct mfc_dma_command cmd,
  812. int *error)
  813. {
  814. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  815. if (*error == -EAGAIN) {
  816. /* wait for any tag group to complete
  817. so we have space for the new command */
  818. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  819. /* try again, because the queue might be
  820. empty again */
  821. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  822. if (*error == -EAGAIN)
  823. return 0;
  824. }
  825. return 1;
  826. }
  827. static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
  828. size_t size, loff_t *pos)
  829. {
  830. struct spu_context *ctx = file->private_data;
  831. struct mfc_dma_command cmd;
  832. int ret = -EINVAL;
  833. if (size != sizeof cmd)
  834. goto out;
  835. ret = -EFAULT;
  836. if (copy_from_user(&cmd, buffer, sizeof cmd))
  837. goto out;
  838. ret = spufs_check_valid_dma(&cmd);
  839. if (ret)
  840. goto out;
  841. spu_acquire_runnable(ctx);
  842. if (file->f_flags & O_NONBLOCK) {
  843. ret = ctx->ops->send_mfc_command(ctx, &cmd);
  844. } else {
  845. int status;
  846. ret = spufs_wait(ctx->mfc_wq,
  847. spu_send_mfc_command(ctx, cmd, &status));
  848. if (status)
  849. ret = status;
  850. }
  851. spu_release(ctx);
  852. if (ret)
  853. goto out;
  854. ctx->tagwait |= 1 << cmd.tag;
  855. out:
  856. return ret;
  857. }
  858. static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
  859. {
  860. struct spu_context *ctx = file->private_data;
  861. u32 free_elements, tagstatus;
  862. unsigned int mask;
  863. spu_acquire(ctx);
  864. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
  865. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  866. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  867. spu_release(ctx);
  868. poll_wait(file, &ctx->mfc_wq, wait);
  869. mask = 0;
  870. if (free_elements & 0xffff)
  871. mask |= POLLOUT | POLLWRNORM;
  872. if (tagstatus & ctx->tagwait)
  873. mask |= POLLIN | POLLRDNORM;
  874. pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
  875. free_elements, tagstatus, ctx->tagwait);
  876. return mask;
  877. }
  878. static int spufs_mfc_flush(struct file *file)
  879. {
  880. struct spu_context *ctx = file->private_data;
  881. int ret;
  882. spu_acquire(ctx);
  883. #if 0
  884. /* this currently hangs */
  885. ret = spufs_wait(ctx->mfc_wq,
  886. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
  887. if (ret)
  888. goto out;
  889. ret = spufs_wait(ctx->mfc_wq,
  890. ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
  891. out:
  892. #else
  893. ret = 0;
  894. #endif
  895. spu_release(ctx);
  896. return ret;
  897. }
  898. static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
  899. int datasync)
  900. {
  901. return spufs_mfc_flush(file);
  902. }
  903. static int spufs_mfc_fasync(int fd, struct file *file, int on)
  904. {
  905. struct spu_context *ctx = file->private_data;
  906. return fasync_helper(fd, file, on, &ctx->mfc_fasync);
  907. }
  908. static struct file_operations spufs_mfc_fops = {
  909. .open = spufs_mfc_open,
  910. .read = spufs_mfc_read,
  911. .write = spufs_mfc_write,
  912. .poll = spufs_mfc_poll,
  913. .flush = spufs_mfc_flush,
  914. .fsync = spufs_mfc_fsync,
  915. .fasync = spufs_mfc_fasync,
  916. #ifdef CONFIG_SPUFS_MMAP
  917. .mmap = spufs_mfc_mmap,
  918. #endif
  919. };
  920. static void spufs_npc_set(void *data, u64 val)
  921. {
  922. struct spu_context *ctx = data;
  923. spu_acquire(ctx);
  924. ctx->ops->npc_write(ctx, val);
  925. spu_release(ctx);
  926. }
  927. static u64 spufs_npc_get(void *data)
  928. {
  929. struct spu_context *ctx = data;
  930. u64 ret;
  931. spu_acquire(ctx);
  932. ret = ctx->ops->npc_read(ctx);
  933. spu_release(ctx);
  934. return ret;
  935. }
  936. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
  937. static void spufs_decr_set(void *data, u64 val)
  938. {
  939. struct spu_context *ctx = data;
  940. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  941. spu_acquire_saved(ctx);
  942. lscsa->decr.slot[0] = (u32) val;
  943. spu_release(ctx);
  944. }
  945. static u64 spufs_decr_get(void *data)
  946. {
  947. struct spu_context *ctx = data;
  948. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  949. u64 ret;
  950. spu_acquire_saved(ctx);
  951. ret = lscsa->decr.slot[0];
  952. spu_release(ctx);
  953. return ret;
  954. }
  955. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  956. "%llx\n")
  957. static void spufs_decr_status_set(void *data, u64 val)
  958. {
  959. struct spu_context *ctx = data;
  960. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  961. spu_acquire_saved(ctx);
  962. lscsa->decr_status.slot[0] = (u32) val;
  963. spu_release(ctx);
  964. }
  965. static u64 spufs_decr_status_get(void *data)
  966. {
  967. struct spu_context *ctx = data;
  968. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  969. u64 ret;
  970. spu_acquire_saved(ctx);
  971. ret = lscsa->decr_status.slot[0];
  972. spu_release(ctx);
  973. return ret;
  974. }
  975. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  976. spufs_decr_status_set, "%llx\n")
  977. static void spufs_spu_tag_mask_set(void *data, u64 val)
  978. {
  979. struct spu_context *ctx = data;
  980. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  981. spu_acquire_saved(ctx);
  982. lscsa->tag_mask.slot[0] = (u32) val;
  983. spu_release(ctx);
  984. }
  985. static u64 spufs_spu_tag_mask_get(void *data)
  986. {
  987. struct spu_context *ctx = data;
  988. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  989. u64 ret;
  990. spu_acquire_saved(ctx);
  991. ret = lscsa->tag_mask.slot[0];
  992. spu_release(ctx);
  993. return ret;
  994. }
  995. DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
  996. spufs_spu_tag_mask_set, "%llx\n")
  997. static void spufs_event_mask_set(void *data, u64 val)
  998. {
  999. struct spu_context *ctx = data;
  1000. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1001. spu_acquire_saved(ctx);
  1002. lscsa->event_mask.slot[0] = (u32) val;
  1003. spu_release(ctx);
  1004. }
  1005. static u64 spufs_event_mask_get(void *data)
  1006. {
  1007. struct spu_context *ctx = data;
  1008. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1009. u64 ret;
  1010. spu_acquire_saved(ctx);
  1011. ret = lscsa->event_mask.slot[0];
  1012. spu_release(ctx);
  1013. return ret;
  1014. }
  1015. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1016. spufs_event_mask_set, "%llx\n")
  1017. static void spufs_srr0_set(void *data, u64 val)
  1018. {
  1019. struct spu_context *ctx = data;
  1020. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1021. spu_acquire_saved(ctx);
  1022. lscsa->srr0.slot[0] = (u32) val;
  1023. spu_release(ctx);
  1024. }
  1025. static u64 spufs_srr0_get(void *data)
  1026. {
  1027. struct spu_context *ctx = data;
  1028. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1029. u64 ret;
  1030. spu_acquire_saved(ctx);
  1031. ret = lscsa->srr0.slot[0];
  1032. spu_release(ctx);
  1033. return ret;
  1034. }
  1035. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1036. "%llx\n")
  1037. struct tree_descr spufs_dir_contents[] = {
  1038. { "mem", &spufs_mem_fops, 0666, },
  1039. { "regs", &spufs_regs_fops, 0666, },
  1040. { "mbox", &spufs_mbox_fops, 0444, },
  1041. { "ibox", &spufs_ibox_fops, 0444, },
  1042. { "wbox", &spufs_wbox_fops, 0222, },
  1043. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1044. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1045. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1046. { "signal1", &spufs_signal1_fops, 0666, },
  1047. { "signal2", &spufs_signal2_fops, 0666, },
  1048. { "signal1_type", &spufs_signal1_type, 0666, },
  1049. { "signal2_type", &spufs_signal2_type, 0666, },
  1050. { "mfc", &spufs_mfc_fops, 0666, },
  1051. { "cntl", &spufs_cntl_fops, 0666, },
  1052. { "npc", &spufs_npc_ops, 0666, },
  1053. { "fpcr", &spufs_fpcr_fops, 0666, },
  1054. { "decr", &spufs_decr_ops, 0666, },
  1055. { "decr_status", &spufs_decr_status_ops, 0666, },
  1056. { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
  1057. { "event_mask", &spufs_event_mask_ops, 0666, },
  1058. { "srr0", &spufs_srr0_ops, 0666, },
  1059. {},
  1060. };