file.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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_mss_mmap_nopage(struct vm_area_struct *vma,
  650. unsigned long address, int *type)
  651. {
  652. return spufs_ps_nopage(vma, address, type, 0x0000);
  653. }
  654. static struct vm_operations_struct spufs_mss_mmap_vmops = {
  655. .nopage = spufs_mss_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_mss_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_mss_mmap_vmops;
  672. return 0;
  673. }
  674. #endif
  675. static int spufs_mss_open(struct inode *inode, struct file *file)
  676. {
  677. struct spufs_inode_info *i = SPUFS_I(inode);
  678. file->private_data = i->i_ctx;
  679. return nonseekable_open(inode, file);
  680. }
  681. static struct file_operations spufs_mss_fops = {
  682. .open = spufs_mss_open,
  683. #ifdef CONFIG_SPUFS_MMAP
  684. .mmap = spufs_mss_mmap,
  685. #endif
  686. };
  687. #ifdef CONFIG_SPUFS_MMAP
  688. static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
  689. unsigned long address, int *type)
  690. {
  691. return spufs_ps_nopage(vma, address, type, 0x3000);
  692. }
  693. static struct vm_operations_struct spufs_mfc_mmap_vmops = {
  694. .nopage = spufs_mfc_mmap_nopage,
  695. };
  696. /*
  697. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  698. * Mapping this area requires that the application have CAP_SYS_RAWIO,
  699. * as these registers require special care when read/writing.
  700. */
  701. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  702. {
  703. if (!(vma->vm_flags & VM_SHARED))
  704. return -EINVAL;
  705. if (!capable(CAP_SYS_RAWIO))
  706. return -EPERM;
  707. vma->vm_flags |= VM_RESERVED;
  708. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  709. | _PAGE_NO_CACHE);
  710. vma->vm_ops = &spufs_mfc_mmap_vmops;
  711. return 0;
  712. }
  713. #endif
  714. static int spufs_mfc_open(struct inode *inode, struct file *file)
  715. {
  716. struct spufs_inode_info *i = SPUFS_I(inode);
  717. struct spu_context *ctx = i->i_ctx;
  718. /* we don't want to deal with DMA into other processes */
  719. if (ctx->owner != current->mm)
  720. return -EINVAL;
  721. if (atomic_read(&inode->i_count) != 1)
  722. return -EBUSY;
  723. file->private_data = ctx;
  724. return nonseekable_open(inode, file);
  725. }
  726. /* interrupt-level mfc callback function. */
  727. void spufs_mfc_callback(struct spu *spu)
  728. {
  729. struct spu_context *ctx = spu->ctx;
  730. wake_up_all(&ctx->mfc_wq);
  731. pr_debug("%s %s\n", __FUNCTION__, spu->name);
  732. if (ctx->mfc_fasync) {
  733. u32 free_elements, tagstatus;
  734. unsigned int mask;
  735. /* no need for spu_acquire in interrupt context */
  736. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  737. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  738. mask = 0;
  739. if (free_elements & 0xffff)
  740. mask |= POLLOUT;
  741. if (tagstatus & ctx->tagwait)
  742. mask |= POLLIN;
  743. kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
  744. }
  745. }
  746. static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
  747. {
  748. /* See if there is one tag group is complete */
  749. /* FIXME we need locking around tagwait */
  750. *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
  751. ctx->tagwait &= ~*status;
  752. if (*status)
  753. return 1;
  754. /* enable interrupt waiting for any tag group,
  755. may silently fail if interrupts are already enabled */
  756. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  757. return 0;
  758. }
  759. static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
  760. size_t size, loff_t *pos)
  761. {
  762. struct spu_context *ctx = file->private_data;
  763. int ret = -EINVAL;
  764. u32 status;
  765. if (size != 4)
  766. goto out;
  767. spu_acquire(ctx);
  768. if (file->f_flags & O_NONBLOCK) {
  769. status = ctx->ops->read_mfc_tagstatus(ctx);
  770. if (!(status & ctx->tagwait))
  771. ret = -EAGAIN;
  772. else
  773. ctx->tagwait &= ~status;
  774. } else {
  775. ret = spufs_wait(ctx->mfc_wq,
  776. spufs_read_mfc_tagstatus(ctx, &status));
  777. }
  778. spu_release(ctx);
  779. if (ret)
  780. goto out;
  781. ret = 4;
  782. if (copy_to_user(buffer, &status, 4))
  783. ret = -EFAULT;
  784. out:
  785. return ret;
  786. }
  787. static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
  788. {
  789. pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
  790. cmd->ea, cmd->size, cmd->tag, cmd->cmd);
  791. switch (cmd->cmd) {
  792. case MFC_PUT_CMD:
  793. case MFC_PUTF_CMD:
  794. case MFC_PUTB_CMD:
  795. case MFC_GET_CMD:
  796. case MFC_GETF_CMD:
  797. case MFC_GETB_CMD:
  798. break;
  799. default:
  800. pr_debug("invalid DMA opcode %x\n", cmd->cmd);
  801. return -EIO;
  802. }
  803. if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
  804. pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
  805. cmd->ea, cmd->lsa);
  806. return -EIO;
  807. }
  808. switch (cmd->size & 0xf) {
  809. case 1:
  810. break;
  811. case 2:
  812. if (cmd->lsa & 1)
  813. goto error;
  814. break;
  815. case 4:
  816. if (cmd->lsa & 3)
  817. goto error;
  818. break;
  819. case 8:
  820. if (cmd->lsa & 7)
  821. goto error;
  822. break;
  823. case 0:
  824. if (cmd->lsa & 15)
  825. goto error;
  826. break;
  827. error:
  828. default:
  829. pr_debug("invalid DMA alignment %x for size %x\n",
  830. cmd->lsa & 0xf, cmd->size);
  831. return -EIO;
  832. }
  833. if (cmd->size > 16 * 1024) {
  834. pr_debug("invalid DMA size %x\n", cmd->size);
  835. return -EIO;
  836. }
  837. if (cmd->tag & 0xfff0) {
  838. /* we reserve the higher tag numbers for kernel use */
  839. pr_debug("invalid DMA tag\n");
  840. return -EIO;
  841. }
  842. if (cmd->class) {
  843. /* not supported in this version */
  844. pr_debug("invalid DMA class\n");
  845. return -EIO;
  846. }
  847. return 0;
  848. }
  849. static int spu_send_mfc_command(struct spu_context *ctx,
  850. struct mfc_dma_command cmd,
  851. int *error)
  852. {
  853. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  854. if (*error == -EAGAIN) {
  855. /* wait for any tag group to complete
  856. so we have space for the new command */
  857. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  858. /* try again, because the queue might be
  859. empty again */
  860. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  861. if (*error == -EAGAIN)
  862. return 0;
  863. }
  864. return 1;
  865. }
  866. static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
  867. size_t size, loff_t *pos)
  868. {
  869. struct spu_context *ctx = file->private_data;
  870. struct mfc_dma_command cmd;
  871. int ret = -EINVAL;
  872. if (size != sizeof cmd)
  873. goto out;
  874. ret = -EFAULT;
  875. if (copy_from_user(&cmd, buffer, sizeof cmd))
  876. goto out;
  877. ret = spufs_check_valid_dma(&cmd);
  878. if (ret)
  879. goto out;
  880. spu_acquire_runnable(ctx);
  881. if (file->f_flags & O_NONBLOCK) {
  882. ret = ctx->ops->send_mfc_command(ctx, &cmd);
  883. } else {
  884. int status;
  885. ret = spufs_wait(ctx->mfc_wq,
  886. spu_send_mfc_command(ctx, cmd, &status));
  887. if (status)
  888. ret = status;
  889. }
  890. spu_release(ctx);
  891. if (ret)
  892. goto out;
  893. ctx->tagwait |= 1 << cmd.tag;
  894. out:
  895. return ret;
  896. }
  897. static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
  898. {
  899. struct spu_context *ctx = file->private_data;
  900. u32 free_elements, tagstatus;
  901. unsigned int mask;
  902. spu_acquire(ctx);
  903. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
  904. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  905. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  906. spu_release(ctx);
  907. poll_wait(file, &ctx->mfc_wq, wait);
  908. mask = 0;
  909. if (free_elements & 0xffff)
  910. mask |= POLLOUT | POLLWRNORM;
  911. if (tagstatus & ctx->tagwait)
  912. mask |= POLLIN | POLLRDNORM;
  913. pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
  914. free_elements, tagstatus, ctx->tagwait);
  915. return mask;
  916. }
  917. static int spufs_mfc_flush(struct file *file)
  918. {
  919. struct spu_context *ctx = file->private_data;
  920. int ret;
  921. spu_acquire(ctx);
  922. #if 0
  923. /* this currently hangs */
  924. ret = spufs_wait(ctx->mfc_wq,
  925. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
  926. if (ret)
  927. goto out;
  928. ret = spufs_wait(ctx->mfc_wq,
  929. ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
  930. out:
  931. #else
  932. ret = 0;
  933. #endif
  934. spu_release(ctx);
  935. return ret;
  936. }
  937. static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
  938. int datasync)
  939. {
  940. return spufs_mfc_flush(file);
  941. }
  942. static int spufs_mfc_fasync(int fd, struct file *file, int on)
  943. {
  944. struct spu_context *ctx = file->private_data;
  945. return fasync_helper(fd, file, on, &ctx->mfc_fasync);
  946. }
  947. static struct file_operations spufs_mfc_fops = {
  948. .open = spufs_mfc_open,
  949. .read = spufs_mfc_read,
  950. .write = spufs_mfc_write,
  951. .poll = spufs_mfc_poll,
  952. .flush = spufs_mfc_flush,
  953. .fsync = spufs_mfc_fsync,
  954. .fasync = spufs_mfc_fasync,
  955. #ifdef CONFIG_SPUFS_MMAP
  956. .mmap = spufs_mfc_mmap,
  957. #endif
  958. };
  959. static void spufs_npc_set(void *data, u64 val)
  960. {
  961. struct spu_context *ctx = data;
  962. spu_acquire(ctx);
  963. ctx->ops->npc_write(ctx, val);
  964. spu_release(ctx);
  965. }
  966. static u64 spufs_npc_get(void *data)
  967. {
  968. struct spu_context *ctx = data;
  969. u64 ret;
  970. spu_acquire(ctx);
  971. ret = ctx->ops->npc_read(ctx);
  972. spu_release(ctx);
  973. return ret;
  974. }
  975. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
  976. static void spufs_decr_set(void *data, u64 val)
  977. {
  978. struct spu_context *ctx = data;
  979. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  980. spu_acquire_saved(ctx);
  981. lscsa->decr.slot[0] = (u32) val;
  982. spu_release(ctx);
  983. }
  984. static u64 spufs_decr_get(void *data)
  985. {
  986. struct spu_context *ctx = data;
  987. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  988. u64 ret;
  989. spu_acquire_saved(ctx);
  990. ret = lscsa->decr.slot[0];
  991. spu_release(ctx);
  992. return ret;
  993. }
  994. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  995. "%llx\n")
  996. static void spufs_decr_status_set(void *data, u64 val)
  997. {
  998. struct spu_context *ctx = data;
  999. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1000. spu_acquire_saved(ctx);
  1001. lscsa->decr_status.slot[0] = (u32) val;
  1002. spu_release(ctx);
  1003. }
  1004. static u64 spufs_decr_status_get(void *data)
  1005. {
  1006. struct spu_context *ctx = data;
  1007. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1008. u64 ret;
  1009. spu_acquire_saved(ctx);
  1010. ret = lscsa->decr_status.slot[0];
  1011. spu_release(ctx);
  1012. return ret;
  1013. }
  1014. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  1015. spufs_decr_status_set, "%llx\n")
  1016. static void spufs_spu_tag_mask_set(void *data, u64 val)
  1017. {
  1018. struct spu_context *ctx = data;
  1019. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1020. spu_acquire_saved(ctx);
  1021. lscsa->tag_mask.slot[0] = (u32) val;
  1022. spu_release(ctx);
  1023. }
  1024. static u64 spufs_spu_tag_mask_get(void *data)
  1025. {
  1026. struct spu_context *ctx = data;
  1027. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1028. u64 ret;
  1029. spu_acquire_saved(ctx);
  1030. ret = lscsa->tag_mask.slot[0];
  1031. spu_release(ctx);
  1032. return ret;
  1033. }
  1034. DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
  1035. spufs_spu_tag_mask_set, "%llx\n")
  1036. static void spufs_event_mask_set(void *data, u64 val)
  1037. {
  1038. struct spu_context *ctx = data;
  1039. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1040. spu_acquire_saved(ctx);
  1041. lscsa->event_mask.slot[0] = (u32) val;
  1042. spu_release(ctx);
  1043. }
  1044. static u64 spufs_event_mask_get(void *data)
  1045. {
  1046. struct spu_context *ctx = data;
  1047. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1048. u64 ret;
  1049. spu_acquire_saved(ctx);
  1050. ret = lscsa->event_mask.slot[0];
  1051. spu_release(ctx);
  1052. return ret;
  1053. }
  1054. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1055. spufs_event_mask_set, "%llx\n")
  1056. static void spufs_srr0_set(void *data, u64 val)
  1057. {
  1058. struct spu_context *ctx = data;
  1059. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1060. spu_acquire_saved(ctx);
  1061. lscsa->srr0.slot[0] = (u32) val;
  1062. spu_release(ctx);
  1063. }
  1064. static u64 spufs_srr0_get(void *data)
  1065. {
  1066. struct spu_context *ctx = data;
  1067. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1068. u64 ret;
  1069. spu_acquire_saved(ctx);
  1070. ret = lscsa->srr0.slot[0];
  1071. spu_release(ctx);
  1072. return ret;
  1073. }
  1074. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1075. "%llx\n")
  1076. struct tree_descr spufs_dir_contents[] = {
  1077. { "mem", &spufs_mem_fops, 0666, },
  1078. { "regs", &spufs_regs_fops, 0666, },
  1079. { "mbox", &spufs_mbox_fops, 0444, },
  1080. { "ibox", &spufs_ibox_fops, 0444, },
  1081. { "wbox", &spufs_wbox_fops, 0222, },
  1082. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1083. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1084. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1085. { "signal1", &spufs_signal1_fops, 0666, },
  1086. { "signal2", &spufs_signal2_fops, 0666, },
  1087. { "signal1_type", &spufs_signal1_type, 0666, },
  1088. { "signal2_type", &spufs_signal2_type, 0666, },
  1089. { "mss", &spufs_mss_fops, 0666, },
  1090. { "mfc", &spufs_mfc_fops, 0666, },
  1091. { "cntl", &spufs_cntl_fops, 0666, },
  1092. { "npc", &spufs_npc_ops, 0666, },
  1093. { "fpcr", &spufs_fpcr_fops, 0666, },
  1094. { "decr", &spufs_decr_ops, 0666, },
  1095. { "decr_status", &spufs_decr_status_ops, 0666, },
  1096. { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
  1097. { "event_mask", &spufs_event_mask_ops, 0666, },
  1098. { "srr0", &spufs_srr0_ops, 0666, },
  1099. {},
  1100. };