file.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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. vma->vm_flags |= VM_IO;
  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_IO;
  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_IO;
  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. vma->vm_flags |= VM_IO;
  717. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  718. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  719. vma->vm_ops = &spufs_signal2_mmap_vmops;
  720. return 0;
  721. }
  722. #else /* SPUFS_MMAP_4K */
  723. #define spufs_signal2_mmap NULL
  724. #endif /* !SPUFS_MMAP_4K */
  725. static struct file_operations spufs_signal2_fops = {
  726. .open = spufs_signal2_open,
  727. .read = spufs_signal2_read,
  728. .write = spufs_signal2_write,
  729. .mmap = spufs_signal2_mmap,
  730. };
  731. static void spufs_signal1_type_set(void *data, u64 val)
  732. {
  733. struct spu_context *ctx = data;
  734. spu_acquire(ctx);
  735. ctx->ops->signal1_type_set(ctx, val);
  736. spu_release(ctx);
  737. }
  738. static u64 spufs_signal1_type_get(void *data)
  739. {
  740. struct spu_context *ctx = data;
  741. u64 ret;
  742. spu_acquire(ctx);
  743. ret = ctx->ops->signal1_type_get(ctx);
  744. spu_release(ctx);
  745. return ret;
  746. }
  747. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  748. spufs_signal1_type_set, "%llu");
  749. static void spufs_signal2_type_set(void *data, u64 val)
  750. {
  751. struct spu_context *ctx = data;
  752. spu_acquire(ctx);
  753. ctx->ops->signal2_type_set(ctx, val);
  754. spu_release(ctx);
  755. }
  756. static u64 spufs_signal2_type_get(void *data)
  757. {
  758. struct spu_context *ctx = data;
  759. u64 ret;
  760. spu_acquire(ctx);
  761. ret = ctx->ops->signal2_type_get(ctx);
  762. spu_release(ctx);
  763. return ret;
  764. }
  765. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  766. spufs_signal2_type_set, "%llu");
  767. #if SPUFS_MMAP_4K
  768. static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
  769. unsigned long address, int *type)
  770. {
  771. return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
  772. }
  773. static struct vm_operations_struct spufs_mss_mmap_vmops = {
  774. .nopage = spufs_mss_mmap_nopage,
  775. };
  776. /*
  777. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  778. */
  779. static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
  780. {
  781. if (!(vma->vm_flags & VM_SHARED))
  782. return -EINVAL;
  783. vma->vm_flags |= VM_IO;
  784. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  785. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  786. vma->vm_ops = &spufs_mss_mmap_vmops;
  787. return 0;
  788. }
  789. #else /* SPUFS_MMAP_4K */
  790. #define spufs_mss_mmap NULL
  791. #endif /* !SPUFS_MMAP_4K */
  792. static int spufs_mss_open(struct inode *inode, struct file *file)
  793. {
  794. struct spufs_inode_info *i = SPUFS_I(inode);
  795. file->private_data = i->i_ctx;
  796. return nonseekable_open(inode, file);
  797. }
  798. static struct file_operations spufs_mss_fops = {
  799. .open = spufs_mss_open,
  800. .mmap = spufs_mss_mmap,
  801. };
  802. static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
  803. unsigned long address, int *type)
  804. {
  805. return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
  806. }
  807. static struct vm_operations_struct spufs_psmap_mmap_vmops = {
  808. .nopage = spufs_psmap_mmap_nopage,
  809. };
  810. /*
  811. * mmap support for full problem state area [0x00000 - 0x1ffff].
  812. */
  813. static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
  814. {
  815. if (!(vma->vm_flags & VM_SHARED))
  816. return -EINVAL;
  817. vma->vm_flags |= VM_IO;
  818. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  819. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  820. vma->vm_ops = &spufs_psmap_mmap_vmops;
  821. return 0;
  822. }
  823. static int spufs_psmap_open(struct inode *inode, struct file *file)
  824. {
  825. struct spufs_inode_info *i = SPUFS_I(inode);
  826. file->private_data = i->i_ctx;
  827. return nonseekable_open(inode, file);
  828. }
  829. static struct file_operations spufs_psmap_fops = {
  830. .open = spufs_psmap_open,
  831. .mmap = spufs_psmap_mmap,
  832. };
  833. #if SPUFS_MMAP_4K
  834. static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
  835. unsigned long address, int *type)
  836. {
  837. return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
  838. }
  839. static struct vm_operations_struct spufs_mfc_mmap_vmops = {
  840. .nopage = spufs_mfc_mmap_nopage,
  841. };
  842. /*
  843. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  844. */
  845. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  846. {
  847. if (!(vma->vm_flags & VM_SHARED))
  848. return -EINVAL;
  849. vma->vm_flags |= VM_IO;
  850. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  851. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  852. vma->vm_ops = &spufs_mfc_mmap_vmops;
  853. return 0;
  854. }
  855. #else /* SPUFS_MMAP_4K */
  856. #define spufs_mfc_mmap NULL
  857. #endif /* !SPUFS_MMAP_4K */
  858. static int spufs_mfc_open(struct inode *inode, struct file *file)
  859. {
  860. struct spufs_inode_info *i = SPUFS_I(inode);
  861. struct spu_context *ctx = i->i_ctx;
  862. /* we don't want to deal with DMA into other processes */
  863. if (ctx->owner != current->mm)
  864. return -EINVAL;
  865. if (atomic_read(&inode->i_count) != 1)
  866. return -EBUSY;
  867. file->private_data = ctx;
  868. return nonseekable_open(inode, file);
  869. }
  870. /* interrupt-level mfc callback function. */
  871. void spufs_mfc_callback(struct spu *spu)
  872. {
  873. struct spu_context *ctx = spu->ctx;
  874. wake_up_all(&ctx->mfc_wq);
  875. pr_debug("%s %s\n", __FUNCTION__, spu->name);
  876. if (ctx->mfc_fasync) {
  877. u32 free_elements, tagstatus;
  878. unsigned int mask;
  879. /* no need for spu_acquire in interrupt context */
  880. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  881. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  882. mask = 0;
  883. if (free_elements & 0xffff)
  884. mask |= POLLOUT;
  885. if (tagstatus & ctx->tagwait)
  886. mask |= POLLIN;
  887. kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
  888. }
  889. }
  890. static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
  891. {
  892. /* See if there is one tag group is complete */
  893. /* FIXME we need locking around tagwait */
  894. *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
  895. ctx->tagwait &= ~*status;
  896. if (*status)
  897. return 1;
  898. /* enable interrupt waiting for any tag group,
  899. may silently fail if interrupts are already enabled */
  900. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  901. return 0;
  902. }
  903. static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
  904. size_t size, loff_t *pos)
  905. {
  906. struct spu_context *ctx = file->private_data;
  907. int ret = -EINVAL;
  908. u32 status;
  909. if (size != 4)
  910. goto out;
  911. spu_acquire(ctx);
  912. if (file->f_flags & O_NONBLOCK) {
  913. status = ctx->ops->read_mfc_tagstatus(ctx);
  914. if (!(status & ctx->tagwait))
  915. ret = -EAGAIN;
  916. else
  917. ctx->tagwait &= ~status;
  918. } else {
  919. ret = spufs_wait(ctx->mfc_wq,
  920. spufs_read_mfc_tagstatus(ctx, &status));
  921. }
  922. spu_release(ctx);
  923. if (ret)
  924. goto out;
  925. ret = 4;
  926. if (copy_to_user(buffer, &status, 4))
  927. ret = -EFAULT;
  928. out:
  929. return ret;
  930. }
  931. static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
  932. {
  933. pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
  934. cmd->ea, cmd->size, cmd->tag, cmd->cmd);
  935. switch (cmd->cmd) {
  936. case MFC_PUT_CMD:
  937. case MFC_PUTF_CMD:
  938. case MFC_PUTB_CMD:
  939. case MFC_GET_CMD:
  940. case MFC_GETF_CMD:
  941. case MFC_GETB_CMD:
  942. break;
  943. default:
  944. pr_debug("invalid DMA opcode %x\n", cmd->cmd);
  945. return -EIO;
  946. }
  947. if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
  948. pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
  949. cmd->ea, cmd->lsa);
  950. return -EIO;
  951. }
  952. switch (cmd->size & 0xf) {
  953. case 1:
  954. break;
  955. case 2:
  956. if (cmd->lsa & 1)
  957. goto error;
  958. break;
  959. case 4:
  960. if (cmd->lsa & 3)
  961. goto error;
  962. break;
  963. case 8:
  964. if (cmd->lsa & 7)
  965. goto error;
  966. break;
  967. case 0:
  968. if (cmd->lsa & 15)
  969. goto error;
  970. break;
  971. error:
  972. default:
  973. pr_debug("invalid DMA alignment %x for size %x\n",
  974. cmd->lsa & 0xf, cmd->size);
  975. return -EIO;
  976. }
  977. if (cmd->size > 16 * 1024) {
  978. pr_debug("invalid DMA size %x\n", cmd->size);
  979. return -EIO;
  980. }
  981. if (cmd->tag & 0xfff0) {
  982. /* we reserve the higher tag numbers for kernel use */
  983. pr_debug("invalid DMA tag\n");
  984. return -EIO;
  985. }
  986. if (cmd->class) {
  987. /* not supported in this version */
  988. pr_debug("invalid DMA class\n");
  989. return -EIO;
  990. }
  991. return 0;
  992. }
  993. static int spu_send_mfc_command(struct spu_context *ctx,
  994. struct mfc_dma_command cmd,
  995. int *error)
  996. {
  997. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  998. if (*error == -EAGAIN) {
  999. /* wait for any tag group to complete
  1000. so we have space for the new command */
  1001. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  1002. /* try again, because the queue might be
  1003. empty again */
  1004. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  1005. if (*error == -EAGAIN)
  1006. return 0;
  1007. }
  1008. return 1;
  1009. }
  1010. static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
  1011. size_t size, loff_t *pos)
  1012. {
  1013. struct spu_context *ctx = file->private_data;
  1014. struct mfc_dma_command cmd;
  1015. int ret = -EINVAL;
  1016. if (size != sizeof cmd)
  1017. goto out;
  1018. ret = -EFAULT;
  1019. if (copy_from_user(&cmd, buffer, sizeof cmd))
  1020. goto out;
  1021. ret = spufs_check_valid_dma(&cmd);
  1022. if (ret)
  1023. goto out;
  1024. spu_acquire_runnable(ctx);
  1025. if (file->f_flags & O_NONBLOCK) {
  1026. ret = ctx->ops->send_mfc_command(ctx, &cmd);
  1027. } else {
  1028. int status;
  1029. ret = spufs_wait(ctx->mfc_wq,
  1030. spu_send_mfc_command(ctx, cmd, &status));
  1031. if (status)
  1032. ret = status;
  1033. }
  1034. spu_release(ctx);
  1035. if (ret)
  1036. goto out;
  1037. ctx->tagwait |= 1 << cmd.tag;
  1038. out:
  1039. return ret;
  1040. }
  1041. static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
  1042. {
  1043. struct spu_context *ctx = file->private_data;
  1044. u32 free_elements, tagstatus;
  1045. unsigned int mask;
  1046. spu_acquire(ctx);
  1047. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
  1048. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  1049. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  1050. spu_release(ctx);
  1051. poll_wait(file, &ctx->mfc_wq, wait);
  1052. mask = 0;
  1053. if (free_elements & 0xffff)
  1054. mask |= POLLOUT | POLLWRNORM;
  1055. if (tagstatus & ctx->tagwait)
  1056. mask |= POLLIN | POLLRDNORM;
  1057. pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
  1058. free_elements, tagstatus, ctx->tagwait);
  1059. return mask;
  1060. }
  1061. static int spufs_mfc_flush(struct file *file, fl_owner_t id)
  1062. {
  1063. struct spu_context *ctx = file->private_data;
  1064. int ret;
  1065. spu_acquire(ctx);
  1066. #if 0
  1067. /* this currently hangs */
  1068. ret = spufs_wait(ctx->mfc_wq,
  1069. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
  1070. if (ret)
  1071. goto out;
  1072. ret = spufs_wait(ctx->mfc_wq,
  1073. ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
  1074. out:
  1075. #else
  1076. ret = 0;
  1077. #endif
  1078. spu_release(ctx);
  1079. return ret;
  1080. }
  1081. static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
  1082. int datasync)
  1083. {
  1084. return spufs_mfc_flush(file, NULL);
  1085. }
  1086. static int spufs_mfc_fasync(int fd, struct file *file, int on)
  1087. {
  1088. struct spu_context *ctx = file->private_data;
  1089. return fasync_helper(fd, file, on, &ctx->mfc_fasync);
  1090. }
  1091. static struct file_operations spufs_mfc_fops = {
  1092. .open = spufs_mfc_open,
  1093. .read = spufs_mfc_read,
  1094. .write = spufs_mfc_write,
  1095. .poll = spufs_mfc_poll,
  1096. .flush = spufs_mfc_flush,
  1097. .fsync = spufs_mfc_fsync,
  1098. .fasync = spufs_mfc_fasync,
  1099. .mmap = spufs_mfc_mmap,
  1100. };
  1101. static int spufs_recycle_open(struct inode *inode, struct file *file)
  1102. {
  1103. file->private_data = SPUFS_I(inode)->i_ctx;
  1104. return nonseekable_open(inode, file);
  1105. }
  1106. static ssize_t spufs_recycle_write(struct file *file,
  1107. const char __user *buffer, size_t size, loff_t *pos)
  1108. {
  1109. struct spu_context *ctx = file->private_data;
  1110. int ret;
  1111. if (!(ctx->flags & SPU_CREATE_ISOLATE))
  1112. return -EINVAL;
  1113. if (size < 1)
  1114. return -EINVAL;
  1115. ret = spu_recycle_isolated(ctx);
  1116. if (ret)
  1117. return ret;
  1118. return size;
  1119. }
  1120. static struct file_operations spufs_recycle_fops = {
  1121. .open = spufs_recycle_open,
  1122. .write = spufs_recycle_write,
  1123. };
  1124. static void spufs_npc_set(void *data, u64 val)
  1125. {
  1126. struct spu_context *ctx = data;
  1127. spu_acquire(ctx);
  1128. ctx->ops->npc_write(ctx, val);
  1129. spu_release(ctx);
  1130. }
  1131. static u64 spufs_npc_get(void *data)
  1132. {
  1133. struct spu_context *ctx = data;
  1134. u64 ret;
  1135. spu_acquire(ctx);
  1136. ret = ctx->ops->npc_read(ctx);
  1137. spu_release(ctx);
  1138. return ret;
  1139. }
  1140. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
  1141. "0x%llx\n")
  1142. static void spufs_decr_set(void *data, u64 val)
  1143. {
  1144. struct spu_context *ctx = data;
  1145. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1146. spu_acquire_saved(ctx);
  1147. lscsa->decr.slot[0] = (u32) val;
  1148. spu_release(ctx);
  1149. }
  1150. static u64 spufs_decr_get(void *data)
  1151. {
  1152. struct spu_context *ctx = data;
  1153. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1154. u64 ret;
  1155. spu_acquire_saved(ctx);
  1156. ret = lscsa->decr.slot[0];
  1157. spu_release(ctx);
  1158. return ret;
  1159. }
  1160. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  1161. "0x%llx\n")
  1162. static void spufs_decr_status_set(void *data, u64 val)
  1163. {
  1164. struct spu_context *ctx = data;
  1165. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1166. spu_acquire_saved(ctx);
  1167. lscsa->decr_status.slot[0] = (u32) val;
  1168. spu_release(ctx);
  1169. }
  1170. static u64 spufs_decr_status_get(void *data)
  1171. {
  1172. struct spu_context *ctx = data;
  1173. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1174. u64 ret;
  1175. spu_acquire_saved(ctx);
  1176. ret = lscsa->decr_status.slot[0];
  1177. spu_release(ctx);
  1178. return ret;
  1179. }
  1180. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  1181. spufs_decr_status_set, "0x%llx\n")
  1182. static void spufs_event_mask_set(void *data, u64 val)
  1183. {
  1184. struct spu_context *ctx = data;
  1185. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1186. spu_acquire_saved(ctx);
  1187. lscsa->event_mask.slot[0] = (u32) val;
  1188. spu_release(ctx);
  1189. }
  1190. static u64 spufs_event_mask_get(void *data)
  1191. {
  1192. struct spu_context *ctx = data;
  1193. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1194. u64 ret;
  1195. spu_acquire_saved(ctx);
  1196. ret = lscsa->event_mask.slot[0];
  1197. spu_release(ctx);
  1198. return ret;
  1199. }
  1200. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1201. spufs_event_mask_set, "0x%llx\n")
  1202. static u64 spufs_event_status_get(void *data)
  1203. {
  1204. struct spu_context *ctx = data;
  1205. struct spu_state *state = &ctx->csa;
  1206. u64 ret = 0;
  1207. u64 stat;
  1208. spu_acquire_saved(ctx);
  1209. stat = state->spu_chnlcnt_RW[0];
  1210. if (stat)
  1211. ret = state->spu_chnldata_RW[0];
  1212. spu_release(ctx);
  1213. return ret;
  1214. }
  1215. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
  1216. NULL, "0x%llx\n")
  1217. static void spufs_srr0_set(void *data, u64 val)
  1218. {
  1219. struct spu_context *ctx = data;
  1220. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1221. spu_acquire_saved(ctx);
  1222. lscsa->srr0.slot[0] = (u32) val;
  1223. spu_release(ctx);
  1224. }
  1225. static u64 spufs_srr0_get(void *data)
  1226. {
  1227. struct spu_context *ctx = data;
  1228. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1229. u64 ret;
  1230. spu_acquire_saved(ctx);
  1231. ret = lscsa->srr0.slot[0];
  1232. spu_release(ctx);
  1233. return ret;
  1234. }
  1235. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1236. "0x%llx\n")
  1237. static u64 spufs_id_get(void *data)
  1238. {
  1239. struct spu_context *ctx = data;
  1240. u64 num;
  1241. spu_acquire(ctx);
  1242. if (ctx->state == SPU_STATE_RUNNABLE)
  1243. num = ctx->spu->number;
  1244. else
  1245. num = (unsigned int)-1;
  1246. spu_release(ctx);
  1247. return num;
  1248. }
  1249. DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
  1250. static u64 spufs_object_id_get(void *data)
  1251. {
  1252. struct spu_context *ctx = data;
  1253. return ctx->object_id;
  1254. }
  1255. static void spufs_object_id_set(void *data, u64 id)
  1256. {
  1257. struct spu_context *ctx = data;
  1258. ctx->object_id = id;
  1259. }
  1260. DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
  1261. spufs_object_id_set, "0x%llx\n");
  1262. static u64 spufs_lslr_get(void *data)
  1263. {
  1264. struct spu_context *ctx = data;
  1265. u64 ret;
  1266. spu_acquire_saved(ctx);
  1267. ret = ctx->csa.priv2.spu_lslr_RW;
  1268. spu_release(ctx);
  1269. return ret;
  1270. }
  1271. DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
  1272. static int spufs_info_open(struct inode *inode, struct file *file)
  1273. {
  1274. struct spufs_inode_info *i = SPUFS_I(inode);
  1275. struct spu_context *ctx = i->i_ctx;
  1276. file->private_data = ctx;
  1277. return 0;
  1278. }
  1279. static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
  1280. size_t len, loff_t *pos)
  1281. {
  1282. struct spu_context *ctx = file->private_data;
  1283. u32 mbox_stat;
  1284. u32 data;
  1285. if (!access_ok(VERIFY_WRITE, buf, len))
  1286. return -EFAULT;
  1287. spu_acquire_saved(ctx);
  1288. spin_lock(&ctx->csa.register_lock);
  1289. mbox_stat = ctx->csa.prob.mb_stat_R;
  1290. if (mbox_stat & 0x0000ff) {
  1291. data = ctx->csa.prob.pu_mb_R;
  1292. }
  1293. spin_unlock(&ctx->csa.register_lock);
  1294. spu_release(ctx);
  1295. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1296. }
  1297. static struct file_operations spufs_mbox_info_fops = {
  1298. .open = spufs_info_open,
  1299. .read = spufs_mbox_info_read,
  1300. .llseek = generic_file_llseek,
  1301. };
  1302. static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
  1303. size_t len, loff_t *pos)
  1304. {
  1305. struct spu_context *ctx = file->private_data;
  1306. u32 ibox_stat;
  1307. u32 data;
  1308. if (!access_ok(VERIFY_WRITE, buf, len))
  1309. return -EFAULT;
  1310. spu_acquire_saved(ctx);
  1311. spin_lock(&ctx->csa.register_lock);
  1312. ibox_stat = ctx->csa.prob.mb_stat_R;
  1313. if (ibox_stat & 0xff0000) {
  1314. data = ctx->csa.priv2.puint_mb_R;
  1315. }
  1316. spin_unlock(&ctx->csa.register_lock);
  1317. spu_release(ctx);
  1318. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1319. }
  1320. static struct file_operations spufs_ibox_info_fops = {
  1321. .open = spufs_info_open,
  1322. .read = spufs_ibox_info_read,
  1323. .llseek = generic_file_llseek,
  1324. };
  1325. static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
  1326. size_t len, loff_t *pos)
  1327. {
  1328. struct spu_context *ctx = file->private_data;
  1329. int i, cnt;
  1330. u32 data[4];
  1331. u32 wbox_stat;
  1332. if (!access_ok(VERIFY_WRITE, buf, len))
  1333. return -EFAULT;
  1334. spu_acquire_saved(ctx);
  1335. spin_lock(&ctx->csa.register_lock);
  1336. wbox_stat = ctx->csa.prob.mb_stat_R;
  1337. cnt = (wbox_stat & 0x00ff00) >> 8;
  1338. for (i = 0; i < cnt; i++) {
  1339. data[i] = ctx->csa.spu_mailbox_data[i];
  1340. }
  1341. spin_unlock(&ctx->csa.register_lock);
  1342. spu_release(ctx);
  1343. return simple_read_from_buffer(buf, len, pos, &data,
  1344. cnt * sizeof(u32));
  1345. }
  1346. static struct file_operations spufs_wbox_info_fops = {
  1347. .open = spufs_info_open,
  1348. .read = spufs_wbox_info_read,
  1349. .llseek = generic_file_llseek,
  1350. };
  1351. static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
  1352. size_t len, loff_t *pos)
  1353. {
  1354. struct spu_context *ctx = file->private_data;
  1355. struct spu_dma_info info;
  1356. struct mfc_cq_sr *qp, *spuqp;
  1357. int i;
  1358. if (!access_ok(VERIFY_WRITE, buf, len))
  1359. return -EFAULT;
  1360. spu_acquire_saved(ctx);
  1361. spin_lock(&ctx->csa.register_lock);
  1362. info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
  1363. info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
  1364. info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
  1365. info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
  1366. info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
  1367. for (i = 0; i < 16; i++) {
  1368. qp = &info.dma_info_command_data[i];
  1369. spuqp = &ctx->csa.priv2.spuq[i];
  1370. qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
  1371. qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
  1372. qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
  1373. qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
  1374. }
  1375. spin_unlock(&ctx->csa.register_lock);
  1376. spu_release(ctx);
  1377. return simple_read_from_buffer(buf, len, pos, &info,
  1378. sizeof info);
  1379. }
  1380. static struct file_operations spufs_dma_info_fops = {
  1381. .open = spufs_info_open,
  1382. .read = spufs_dma_info_read,
  1383. };
  1384. static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
  1385. size_t len, loff_t *pos)
  1386. {
  1387. struct spu_context *ctx = file->private_data;
  1388. struct spu_proxydma_info info;
  1389. int ret = sizeof info;
  1390. struct mfc_cq_sr *qp, *puqp;
  1391. int i;
  1392. if (len < ret)
  1393. return -EINVAL;
  1394. if (!access_ok(VERIFY_WRITE, buf, len))
  1395. return -EFAULT;
  1396. spu_acquire_saved(ctx);
  1397. spin_lock(&ctx->csa.register_lock);
  1398. info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
  1399. info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
  1400. info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
  1401. for (i = 0; i < 8; i++) {
  1402. qp = &info.proxydma_info_command_data[i];
  1403. puqp = &ctx->csa.priv2.puq[i];
  1404. qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
  1405. qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
  1406. qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
  1407. qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
  1408. }
  1409. spin_unlock(&ctx->csa.register_lock);
  1410. spu_release(ctx);
  1411. if (copy_to_user(buf, &info, sizeof info))
  1412. ret = -EFAULT;
  1413. return ret;
  1414. }
  1415. static struct file_operations spufs_proxydma_info_fops = {
  1416. .open = spufs_info_open,
  1417. .read = spufs_proxydma_info_read,
  1418. };
  1419. struct tree_descr spufs_dir_contents[] = {
  1420. { "mem", &spufs_mem_fops, 0666, },
  1421. { "regs", &spufs_regs_fops, 0666, },
  1422. { "mbox", &spufs_mbox_fops, 0444, },
  1423. { "ibox", &spufs_ibox_fops, 0444, },
  1424. { "wbox", &spufs_wbox_fops, 0222, },
  1425. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1426. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1427. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1428. { "signal1", &spufs_signal1_fops, 0666, },
  1429. { "signal2", &spufs_signal2_fops, 0666, },
  1430. { "signal1_type", &spufs_signal1_type, 0666, },
  1431. { "signal2_type", &spufs_signal2_type, 0666, },
  1432. { "cntl", &spufs_cntl_fops, 0666, },
  1433. { "fpcr", &spufs_fpcr_fops, 0666, },
  1434. { "lslr", &spufs_lslr_ops, 0444, },
  1435. { "mfc", &spufs_mfc_fops, 0666, },
  1436. { "mss", &spufs_mss_fops, 0666, },
  1437. { "npc", &spufs_npc_ops, 0666, },
  1438. { "srr0", &spufs_srr0_ops, 0666, },
  1439. { "decr", &spufs_decr_ops, 0666, },
  1440. { "decr_status", &spufs_decr_status_ops, 0666, },
  1441. { "event_mask", &spufs_event_mask_ops, 0666, },
  1442. { "event_status", &spufs_event_status_ops, 0444, },
  1443. { "psmap", &spufs_psmap_fops, 0666, },
  1444. { "phys-id", &spufs_id_ops, 0666, },
  1445. { "object-id", &spufs_object_id_ops, 0666, },
  1446. { "mbox_info", &spufs_mbox_info_fops, 0444, },
  1447. { "ibox_info", &spufs_ibox_info_fops, 0444, },
  1448. { "wbox_info", &spufs_wbox_info_fops, 0444, },
  1449. { "dma_info", &spufs_dma_info_fops, 0444, },
  1450. { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
  1451. {},
  1452. };
  1453. struct tree_descr spufs_dir_nosched_contents[] = {
  1454. { "mem", &spufs_mem_fops, 0666, },
  1455. { "mbox", &spufs_mbox_fops, 0444, },
  1456. { "ibox", &spufs_ibox_fops, 0444, },
  1457. { "wbox", &spufs_wbox_fops, 0222, },
  1458. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1459. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1460. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1461. { "signal1", &spufs_signal1_fops, 0666, },
  1462. { "signal2", &spufs_signal2_fops, 0666, },
  1463. { "signal1_type", &spufs_signal1_type, 0666, },
  1464. { "signal2_type", &spufs_signal2_type, 0666, },
  1465. { "mss", &spufs_mss_fops, 0666, },
  1466. { "mfc", &spufs_mfc_fops, 0666, },
  1467. { "cntl", &spufs_cntl_fops, 0666, },
  1468. { "npc", &spufs_npc_ops, 0666, },
  1469. { "psmap", &spufs_psmap_fops, 0666, },
  1470. { "phys-id", &spufs_id_ops, 0666, },
  1471. { "object-id", &spufs_object_id_ops, 0666, },
  1472. { "recycle", &spufs_recycle_fops, 0222, },
  1473. {},
  1474. };