file.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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 <linux/seq_file.h>
  30. #include <asm/io.h>
  31. #include <asm/semaphore.h>
  32. #include <asm/spu.h>
  33. #include <asm/spu_info.h>
  34. #include <asm/uaccess.h>
  35. #include "spufs.h"
  36. #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
  37. static int
  38. spufs_mem_open(struct inode *inode, struct file *file)
  39. {
  40. struct spufs_inode_info *i = SPUFS_I(inode);
  41. struct spu_context *ctx = i->i_ctx;
  42. mutex_lock(&ctx->mapping_lock);
  43. file->private_data = ctx;
  44. if (!i->i_openers++)
  45. ctx->local_store = inode->i_mapping;
  46. mutex_unlock(&ctx->mapping_lock);
  47. return 0;
  48. }
  49. static int
  50. spufs_mem_release(struct inode *inode, struct file *file)
  51. {
  52. struct spufs_inode_info *i = SPUFS_I(inode);
  53. struct spu_context *ctx = i->i_ctx;
  54. mutex_lock(&ctx->mapping_lock);
  55. if (!--i->i_openers)
  56. ctx->local_store = NULL;
  57. mutex_unlock(&ctx->mapping_lock);
  58. return 0;
  59. }
  60. static ssize_t
  61. __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
  62. size_t size, loff_t *pos)
  63. {
  64. char *local_store = ctx->ops->get_ls(ctx);
  65. return simple_read_from_buffer(buffer, size, pos, local_store,
  66. LS_SIZE);
  67. }
  68. static ssize_t
  69. spufs_mem_read(struct file *file, char __user *buffer,
  70. size_t size, loff_t *pos)
  71. {
  72. struct spu_context *ctx = file->private_data;
  73. ssize_t ret;
  74. spu_acquire(ctx);
  75. ret = __spufs_mem_read(ctx, buffer, size, pos);
  76. spu_release(ctx);
  77. return ret;
  78. }
  79. static ssize_t
  80. spufs_mem_write(struct file *file, const char __user *buffer,
  81. size_t size, loff_t *ppos)
  82. {
  83. struct spu_context *ctx = file->private_data;
  84. char *local_store;
  85. loff_t pos = *ppos;
  86. int ret;
  87. if (pos < 0)
  88. return -EINVAL;
  89. if (pos > LS_SIZE)
  90. return -EFBIG;
  91. if (size > LS_SIZE - pos)
  92. size = LS_SIZE - pos;
  93. spu_acquire(ctx);
  94. local_store = ctx->ops->get_ls(ctx);
  95. ret = copy_from_user(local_store + pos, buffer, size);
  96. spu_release(ctx);
  97. if (ret)
  98. return -EFAULT;
  99. *ppos = pos + size;
  100. return size;
  101. }
  102. static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
  103. unsigned long address)
  104. {
  105. struct spu_context *ctx = vma->vm_file->private_data;
  106. unsigned long pfn, offset, addr0 = address;
  107. #ifdef CONFIG_SPU_FS_64K_LS
  108. struct spu_state *csa = &ctx->csa;
  109. int psize;
  110. /* Check what page size we are using */
  111. psize = get_slice_psize(vma->vm_mm, address);
  112. /* Some sanity checking */
  113. BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
  114. /* Wow, 64K, cool, we need to align the address though */
  115. if (csa->use_big_pages) {
  116. BUG_ON(vma->vm_start & 0xffff);
  117. address &= ~0xfffful;
  118. }
  119. #endif /* CONFIG_SPU_FS_64K_LS */
  120. offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
  121. if (offset >= LS_SIZE)
  122. return NOPFN_SIGBUS;
  123. pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
  124. addr0, address, offset);
  125. spu_acquire(ctx);
  126. if (ctx->state == SPU_STATE_SAVED) {
  127. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  128. & ~_PAGE_NO_CACHE);
  129. pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
  130. } else {
  131. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  132. | _PAGE_NO_CACHE);
  133. pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
  134. }
  135. vm_insert_pfn(vma, address, pfn);
  136. spu_release(ctx);
  137. return NOPFN_REFAULT;
  138. }
  139. static struct vm_operations_struct spufs_mem_mmap_vmops = {
  140. .nopfn = spufs_mem_mmap_nopfn,
  141. };
  142. static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
  143. {
  144. #ifdef CONFIG_SPU_FS_64K_LS
  145. struct spu_context *ctx = file->private_data;
  146. struct spu_state *csa = &ctx->csa;
  147. /* Sanity check VMA alignment */
  148. if (csa->use_big_pages) {
  149. pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
  150. " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
  151. vma->vm_pgoff);
  152. if (vma->vm_start & 0xffff)
  153. return -EINVAL;
  154. if (vma->vm_pgoff & 0xf)
  155. return -EINVAL;
  156. }
  157. #endif /* CONFIG_SPU_FS_64K_LS */
  158. if (!(vma->vm_flags & VM_SHARED))
  159. return -EINVAL;
  160. vma->vm_flags |= VM_IO | VM_PFNMAP;
  161. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  162. | _PAGE_NO_CACHE);
  163. vma->vm_ops = &spufs_mem_mmap_vmops;
  164. return 0;
  165. }
  166. #ifdef CONFIG_SPU_FS_64K_LS
  167. unsigned long spufs_get_unmapped_area(struct file *file, unsigned long addr,
  168. unsigned long len, unsigned long pgoff,
  169. unsigned long flags)
  170. {
  171. struct spu_context *ctx = file->private_data;
  172. struct spu_state *csa = &ctx->csa;
  173. /* If not using big pages, fallback to normal MM g_u_a */
  174. if (!csa->use_big_pages)
  175. return current->mm->get_unmapped_area(file, addr, len,
  176. pgoff, flags);
  177. /* Else, try to obtain a 64K pages slice */
  178. return slice_get_unmapped_area(addr, len, flags,
  179. MMU_PAGE_64K, 1, 0);
  180. }
  181. #endif /* CONFIG_SPU_FS_64K_LS */
  182. static const struct file_operations spufs_mem_fops = {
  183. .open = spufs_mem_open,
  184. .release = spufs_mem_release,
  185. .read = spufs_mem_read,
  186. .write = spufs_mem_write,
  187. .llseek = generic_file_llseek,
  188. .mmap = spufs_mem_mmap,
  189. #ifdef CONFIG_SPU_FS_64K_LS
  190. .get_unmapped_area = spufs_get_unmapped_area,
  191. #endif
  192. };
  193. static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
  194. unsigned long address,
  195. unsigned long ps_offs,
  196. unsigned long ps_size)
  197. {
  198. struct spu_context *ctx = vma->vm_file->private_data;
  199. unsigned long area, offset = address - vma->vm_start;
  200. int ret;
  201. offset += vma->vm_pgoff << PAGE_SHIFT;
  202. if (offset >= ps_size)
  203. return NOPFN_SIGBUS;
  204. /* error here usually means a signal.. we might want to test
  205. * the error code more precisely though
  206. */
  207. ret = spu_acquire_runnable(ctx, 0);
  208. if (ret)
  209. return NOPFN_REFAULT;
  210. area = ctx->spu->problem_phys + ps_offs;
  211. vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
  212. spu_release(ctx);
  213. return NOPFN_REFAULT;
  214. }
  215. #if SPUFS_MMAP_4K
  216. static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
  217. unsigned long address)
  218. {
  219. return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
  220. }
  221. static struct vm_operations_struct spufs_cntl_mmap_vmops = {
  222. .nopfn = spufs_cntl_mmap_nopfn,
  223. };
  224. /*
  225. * mmap support for problem state control area [0x4000 - 0x4fff].
  226. */
  227. static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
  228. {
  229. if (!(vma->vm_flags & VM_SHARED))
  230. return -EINVAL;
  231. vma->vm_flags |= VM_IO | VM_PFNMAP;
  232. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  233. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  234. vma->vm_ops = &spufs_cntl_mmap_vmops;
  235. return 0;
  236. }
  237. #else /* SPUFS_MMAP_4K */
  238. #define spufs_cntl_mmap NULL
  239. #endif /* !SPUFS_MMAP_4K */
  240. static u64 spufs_cntl_get(void *data)
  241. {
  242. struct spu_context *ctx = data;
  243. u64 val;
  244. spu_acquire(ctx);
  245. val = ctx->ops->status_read(ctx);
  246. spu_release(ctx);
  247. return val;
  248. }
  249. static void spufs_cntl_set(void *data, u64 val)
  250. {
  251. struct spu_context *ctx = data;
  252. spu_acquire(ctx);
  253. ctx->ops->runcntl_write(ctx, val);
  254. spu_release(ctx);
  255. }
  256. static int spufs_cntl_open(struct inode *inode, struct file *file)
  257. {
  258. struct spufs_inode_info *i = SPUFS_I(inode);
  259. struct spu_context *ctx = i->i_ctx;
  260. mutex_lock(&ctx->mapping_lock);
  261. file->private_data = ctx;
  262. if (!i->i_openers++)
  263. ctx->cntl = inode->i_mapping;
  264. mutex_unlock(&ctx->mapping_lock);
  265. return simple_attr_open(inode, file, spufs_cntl_get,
  266. spufs_cntl_set, "0x%08lx");
  267. }
  268. static int
  269. spufs_cntl_release(struct inode *inode, struct file *file)
  270. {
  271. struct spufs_inode_info *i = SPUFS_I(inode);
  272. struct spu_context *ctx = i->i_ctx;
  273. simple_attr_close(inode, file);
  274. mutex_lock(&ctx->mapping_lock);
  275. if (!--i->i_openers)
  276. ctx->cntl = NULL;
  277. mutex_unlock(&ctx->mapping_lock);
  278. return 0;
  279. }
  280. static const struct file_operations spufs_cntl_fops = {
  281. .open = spufs_cntl_open,
  282. .release = spufs_cntl_release,
  283. .read = simple_attr_read,
  284. .write = simple_attr_write,
  285. .mmap = spufs_cntl_mmap,
  286. };
  287. static int
  288. spufs_regs_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 0;
  293. }
  294. static ssize_t
  295. __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
  296. size_t size, loff_t *pos)
  297. {
  298. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  299. return simple_read_from_buffer(buffer, size, pos,
  300. lscsa->gprs, sizeof lscsa->gprs);
  301. }
  302. static ssize_t
  303. spufs_regs_read(struct file *file, char __user *buffer,
  304. size_t size, loff_t *pos)
  305. {
  306. int ret;
  307. struct spu_context *ctx = file->private_data;
  308. spu_acquire_saved(ctx);
  309. ret = __spufs_regs_read(ctx, buffer, size, pos);
  310. spu_release(ctx);
  311. return ret;
  312. }
  313. static ssize_t
  314. spufs_regs_write(struct file *file, const char __user *buffer,
  315. size_t size, loff_t *pos)
  316. {
  317. struct spu_context *ctx = file->private_data;
  318. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  319. int ret;
  320. size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
  321. if (size <= 0)
  322. return -EFBIG;
  323. *pos += size;
  324. spu_acquire_saved(ctx);
  325. ret = copy_from_user(lscsa->gprs + *pos - size,
  326. buffer, size) ? -EFAULT : size;
  327. spu_release(ctx);
  328. return ret;
  329. }
  330. static const struct file_operations spufs_regs_fops = {
  331. .open = spufs_regs_open,
  332. .read = spufs_regs_read,
  333. .write = spufs_regs_write,
  334. .llseek = generic_file_llseek,
  335. };
  336. static ssize_t
  337. __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
  338. size_t size, loff_t * pos)
  339. {
  340. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  341. return simple_read_from_buffer(buffer, size, pos,
  342. &lscsa->fpcr, sizeof(lscsa->fpcr));
  343. }
  344. static ssize_t
  345. spufs_fpcr_read(struct file *file, char __user * buffer,
  346. size_t size, loff_t * pos)
  347. {
  348. int ret;
  349. struct spu_context *ctx = file->private_data;
  350. spu_acquire_saved(ctx);
  351. ret = __spufs_fpcr_read(ctx, buffer, size, pos);
  352. spu_release(ctx);
  353. return ret;
  354. }
  355. static ssize_t
  356. spufs_fpcr_write(struct file *file, const char __user * buffer,
  357. size_t size, loff_t * pos)
  358. {
  359. struct spu_context *ctx = file->private_data;
  360. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  361. int ret;
  362. size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
  363. if (size <= 0)
  364. return -EFBIG;
  365. *pos += size;
  366. spu_acquire_saved(ctx);
  367. ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
  368. buffer, size) ? -EFAULT : size;
  369. spu_release(ctx);
  370. return ret;
  371. }
  372. static const struct file_operations spufs_fpcr_fops = {
  373. .open = spufs_regs_open,
  374. .read = spufs_fpcr_read,
  375. .write = spufs_fpcr_write,
  376. .llseek = generic_file_llseek,
  377. };
  378. /* generic open function for all pipe-like files */
  379. static int spufs_pipe_open(struct inode *inode, struct file *file)
  380. {
  381. struct spufs_inode_info *i = SPUFS_I(inode);
  382. file->private_data = i->i_ctx;
  383. return nonseekable_open(inode, file);
  384. }
  385. /*
  386. * Read as many bytes from the mailbox as possible, until
  387. * one of the conditions becomes true:
  388. *
  389. * - no more data available in the mailbox
  390. * - end of the user provided buffer
  391. * - end of the mapped area
  392. */
  393. static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
  394. size_t len, loff_t *pos)
  395. {
  396. struct spu_context *ctx = file->private_data;
  397. u32 mbox_data, __user *udata;
  398. ssize_t count;
  399. if (len < 4)
  400. return -EINVAL;
  401. if (!access_ok(VERIFY_WRITE, buf, len))
  402. return -EFAULT;
  403. udata = (void __user *)buf;
  404. spu_acquire(ctx);
  405. for (count = 0; (count + 4) <= len; count += 4, udata++) {
  406. int ret;
  407. ret = ctx->ops->mbox_read(ctx, &mbox_data);
  408. if (ret == 0)
  409. break;
  410. /*
  411. * at the end of the mapped area, we can fault
  412. * but still need to return the data we have
  413. * read successfully so far.
  414. */
  415. ret = __put_user(mbox_data, udata);
  416. if (ret) {
  417. if (!count)
  418. count = -EFAULT;
  419. break;
  420. }
  421. }
  422. spu_release(ctx);
  423. if (!count)
  424. count = -EAGAIN;
  425. return count;
  426. }
  427. static const struct file_operations spufs_mbox_fops = {
  428. .open = spufs_pipe_open,
  429. .read = spufs_mbox_read,
  430. };
  431. static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
  432. size_t len, loff_t *pos)
  433. {
  434. struct spu_context *ctx = file->private_data;
  435. u32 mbox_stat;
  436. if (len < 4)
  437. return -EINVAL;
  438. spu_acquire(ctx);
  439. mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
  440. spu_release(ctx);
  441. if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
  442. return -EFAULT;
  443. return 4;
  444. }
  445. static const struct file_operations spufs_mbox_stat_fops = {
  446. .open = spufs_pipe_open,
  447. .read = spufs_mbox_stat_read,
  448. };
  449. /* low-level ibox access function */
  450. size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
  451. {
  452. return ctx->ops->ibox_read(ctx, data);
  453. }
  454. static int spufs_ibox_fasync(int fd, struct file *file, int on)
  455. {
  456. struct spu_context *ctx = file->private_data;
  457. return fasync_helper(fd, file, on, &ctx->ibox_fasync);
  458. }
  459. /* interrupt-level ibox callback function. */
  460. void spufs_ibox_callback(struct spu *spu)
  461. {
  462. struct spu_context *ctx = spu->ctx;
  463. wake_up_all(&ctx->ibox_wq);
  464. kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
  465. }
  466. /*
  467. * Read as many bytes from the interrupt mailbox as possible, until
  468. * one of the conditions becomes true:
  469. *
  470. * - no more data available in the mailbox
  471. * - end of the user provided buffer
  472. * - end of the mapped area
  473. *
  474. * If the file is opened without O_NONBLOCK, we wait here until
  475. * any data is available, but return when we have been able to
  476. * read something.
  477. */
  478. static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
  479. size_t len, loff_t *pos)
  480. {
  481. struct spu_context *ctx = file->private_data;
  482. u32 ibox_data, __user *udata;
  483. ssize_t count;
  484. if (len < 4)
  485. return -EINVAL;
  486. if (!access_ok(VERIFY_WRITE, buf, len))
  487. return -EFAULT;
  488. udata = (void __user *)buf;
  489. spu_acquire(ctx);
  490. /* wait only for the first element */
  491. count = 0;
  492. if (file->f_flags & O_NONBLOCK) {
  493. if (!spu_ibox_read(ctx, &ibox_data))
  494. count = -EAGAIN;
  495. } else {
  496. count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
  497. }
  498. if (count)
  499. goto out;
  500. /* if we can't write at all, return -EFAULT */
  501. count = __put_user(ibox_data, udata);
  502. if (count)
  503. goto out;
  504. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  505. int ret;
  506. ret = ctx->ops->ibox_read(ctx, &ibox_data);
  507. if (ret == 0)
  508. break;
  509. /*
  510. * at the end of the mapped area, we can fault
  511. * but still need to return the data we have
  512. * read successfully so far.
  513. */
  514. ret = __put_user(ibox_data, udata);
  515. if (ret)
  516. break;
  517. }
  518. out:
  519. spu_release(ctx);
  520. return count;
  521. }
  522. static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
  523. {
  524. struct spu_context *ctx = file->private_data;
  525. unsigned int mask;
  526. poll_wait(file, &ctx->ibox_wq, wait);
  527. spu_acquire(ctx);
  528. mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
  529. spu_release(ctx);
  530. return mask;
  531. }
  532. static const struct file_operations spufs_ibox_fops = {
  533. .open = spufs_pipe_open,
  534. .read = spufs_ibox_read,
  535. .poll = spufs_ibox_poll,
  536. .fasync = spufs_ibox_fasync,
  537. };
  538. static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
  539. size_t len, loff_t *pos)
  540. {
  541. struct spu_context *ctx = file->private_data;
  542. u32 ibox_stat;
  543. if (len < 4)
  544. return -EINVAL;
  545. spu_acquire(ctx);
  546. ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
  547. spu_release(ctx);
  548. if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
  549. return -EFAULT;
  550. return 4;
  551. }
  552. static const struct file_operations spufs_ibox_stat_fops = {
  553. .open = spufs_pipe_open,
  554. .read = spufs_ibox_stat_read,
  555. };
  556. /* low-level mailbox write */
  557. size_t spu_wbox_write(struct spu_context *ctx, u32 data)
  558. {
  559. return ctx->ops->wbox_write(ctx, data);
  560. }
  561. static int spufs_wbox_fasync(int fd, struct file *file, int on)
  562. {
  563. struct spu_context *ctx = file->private_data;
  564. int ret;
  565. ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
  566. return ret;
  567. }
  568. /* interrupt-level wbox callback function. */
  569. void spufs_wbox_callback(struct spu *spu)
  570. {
  571. struct spu_context *ctx = spu->ctx;
  572. wake_up_all(&ctx->wbox_wq);
  573. kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
  574. }
  575. /*
  576. * Write as many bytes to the interrupt mailbox as possible, until
  577. * one of the conditions becomes true:
  578. *
  579. * - the mailbox is full
  580. * - end of the user provided buffer
  581. * - end of the mapped area
  582. *
  583. * If the file is opened without O_NONBLOCK, we wait here until
  584. * space is availabyl, but return when we have been able to
  585. * write something.
  586. */
  587. static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
  588. size_t len, loff_t *pos)
  589. {
  590. struct spu_context *ctx = file->private_data;
  591. u32 wbox_data, __user *udata;
  592. ssize_t count;
  593. if (len < 4)
  594. return -EINVAL;
  595. udata = (void __user *)buf;
  596. if (!access_ok(VERIFY_READ, buf, len))
  597. return -EFAULT;
  598. if (__get_user(wbox_data, udata))
  599. return -EFAULT;
  600. spu_acquire(ctx);
  601. /*
  602. * make sure we can at least write one element, by waiting
  603. * in case of !O_NONBLOCK
  604. */
  605. count = 0;
  606. if (file->f_flags & O_NONBLOCK) {
  607. if (!spu_wbox_write(ctx, wbox_data))
  608. count = -EAGAIN;
  609. } else {
  610. count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
  611. }
  612. if (count)
  613. goto out;
  614. /* write aѕ much as possible */
  615. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  616. int ret;
  617. ret = __get_user(wbox_data, udata);
  618. if (ret)
  619. break;
  620. ret = spu_wbox_write(ctx, wbox_data);
  621. if (ret == 0)
  622. break;
  623. }
  624. out:
  625. spu_release(ctx);
  626. return count;
  627. }
  628. static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
  629. {
  630. struct spu_context *ctx = file->private_data;
  631. unsigned int mask;
  632. poll_wait(file, &ctx->wbox_wq, wait);
  633. spu_acquire(ctx);
  634. mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
  635. spu_release(ctx);
  636. return mask;
  637. }
  638. static const struct file_operations spufs_wbox_fops = {
  639. .open = spufs_pipe_open,
  640. .write = spufs_wbox_write,
  641. .poll = spufs_wbox_poll,
  642. .fasync = spufs_wbox_fasync,
  643. };
  644. static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
  645. size_t len, loff_t *pos)
  646. {
  647. struct spu_context *ctx = file->private_data;
  648. u32 wbox_stat;
  649. if (len < 4)
  650. return -EINVAL;
  651. spu_acquire(ctx);
  652. wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
  653. spu_release(ctx);
  654. if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
  655. return -EFAULT;
  656. return 4;
  657. }
  658. static const struct file_operations spufs_wbox_stat_fops = {
  659. .open = spufs_pipe_open,
  660. .read = spufs_wbox_stat_read,
  661. };
  662. static int spufs_signal1_open(struct inode *inode, struct file *file)
  663. {
  664. struct spufs_inode_info *i = SPUFS_I(inode);
  665. struct spu_context *ctx = i->i_ctx;
  666. mutex_lock(&ctx->mapping_lock);
  667. file->private_data = ctx;
  668. if (!i->i_openers++)
  669. ctx->signal1 = inode->i_mapping;
  670. mutex_unlock(&ctx->mapping_lock);
  671. return nonseekable_open(inode, file);
  672. }
  673. static int
  674. spufs_signal1_release(struct inode *inode, struct file *file)
  675. {
  676. struct spufs_inode_info *i = SPUFS_I(inode);
  677. struct spu_context *ctx = i->i_ctx;
  678. mutex_lock(&ctx->mapping_lock);
  679. if (!--i->i_openers)
  680. ctx->signal1 = NULL;
  681. mutex_unlock(&ctx->mapping_lock);
  682. return 0;
  683. }
  684. static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
  685. size_t len, loff_t *pos)
  686. {
  687. int ret = 0;
  688. u32 data;
  689. if (len < 4)
  690. return -EINVAL;
  691. if (ctx->csa.spu_chnlcnt_RW[3]) {
  692. data = ctx->csa.spu_chnldata_RW[3];
  693. ret = 4;
  694. }
  695. if (!ret)
  696. goto out;
  697. if (copy_to_user(buf, &data, 4))
  698. return -EFAULT;
  699. out:
  700. return ret;
  701. }
  702. static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
  703. size_t len, loff_t *pos)
  704. {
  705. int ret;
  706. struct spu_context *ctx = file->private_data;
  707. spu_acquire_saved(ctx);
  708. ret = __spufs_signal1_read(ctx, buf, len, pos);
  709. spu_release(ctx);
  710. return ret;
  711. }
  712. static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
  713. size_t len, loff_t *pos)
  714. {
  715. struct spu_context *ctx;
  716. u32 data;
  717. ctx = file->private_data;
  718. if (len < 4)
  719. return -EINVAL;
  720. if (copy_from_user(&data, buf, 4))
  721. return -EFAULT;
  722. spu_acquire(ctx);
  723. ctx->ops->signal1_write(ctx, data);
  724. spu_release(ctx);
  725. return 4;
  726. }
  727. static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
  728. unsigned long address)
  729. {
  730. #if PAGE_SIZE == 0x1000
  731. return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
  732. #elif PAGE_SIZE == 0x10000
  733. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  734. * signal 1 and 2 area
  735. */
  736. return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
  737. #else
  738. #error unsupported page size
  739. #endif
  740. }
  741. static struct vm_operations_struct spufs_signal1_mmap_vmops = {
  742. .nopfn = spufs_signal1_mmap_nopfn,
  743. };
  744. static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
  745. {
  746. if (!(vma->vm_flags & VM_SHARED))
  747. return -EINVAL;
  748. vma->vm_flags |= VM_IO | VM_PFNMAP;
  749. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  750. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  751. vma->vm_ops = &spufs_signal1_mmap_vmops;
  752. return 0;
  753. }
  754. static const struct file_operations spufs_signal1_fops = {
  755. .open = spufs_signal1_open,
  756. .release = spufs_signal1_release,
  757. .read = spufs_signal1_read,
  758. .write = spufs_signal1_write,
  759. .mmap = spufs_signal1_mmap,
  760. };
  761. static int spufs_signal2_open(struct inode *inode, struct file *file)
  762. {
  763. struct spufs_inode_info *i = SPUFS_I(inode);
  764. struct spu_context *ctx = i->i_ctx;
  765. mutex_lock(&ctx->mapping_lock);
  766. file->private_data = ctx;
  767. if (!i->i_openers++)
  768. ctx->signal2 = inode->i_mapping;
  769. mutex_unlock(&ctx->mapping_lock);
  770. return nonseekable_open(inode, file);
  771. }
  772. static int
  773. spufs_signal2_release(struct inode *inode, struct file *file)
  774. {
  775. struct spufs_inode_info *i = SPUFS_I(inode);
  776. struct spu_context *ctx = i->i_ctx;
  777. mutex_lock(&ctx->mapping_lock);
  778. if (!--i->i_openers)
  779. ctx->signal2 = NULL;
  780. mutex_unlock(&ctx->mapping_lock);
  781. return 0;
  782. }
  783. static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
  784. size_t len, loff_t *pos)
  785. {
  786. int ret = 0;
  787. u32 data;
  788. if (len < 4)
  789. return -EINVAL;
  790. if (ctx->csa.spu_chnlcnt_RW[4]) {
  791. data = ctx->csa.spu_chnldata_RW[4];
  792. ret = 4;
  793. }
  794. if (!ret)
  795. goto out;
  796. if (copy_to_user(buf, &data, 4))
  797. return -EFAULT;
  798. out:
  799. return ret;
  800. }
  801. static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
  802. size_t len, loff_t *pos)
  803. {
  804. struct spu_context *ctx = file->private_data;
  805. int ret;
  806. spu_acquire_saved(ctx);
  807. ret = __spufs_signal2_read(ctx, buf, len, pos);
  808. spu_release(ctx);
  809. return ret;
  810. }
  811. static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
  812. size_t len, loff_t *pos)
  813. {
  814. struct spu_context *ctx;
  815. u32 data;
  816. ctx = file->private_data;
  817. if (len < 4)
  818. return -EINVAL;
  819. if (copy_from_user(&data, buf, 4))
  820. return -EFAULT;
  821. spu_acquire(ctx);
  822. ctx->ops->signal2_write(ctx, data);
  823. spu_release(ctx);
  824. return 4;
  825. }
  826. #if SPUFS_MMAP_4K
  827. static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
  828. unsigned long address)
  829. {
  830. #if PAGE_SIZE == 0x1000
  831. return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
  832. #elif PAGE_SIZE == 0x10000
  833. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  834. * signal 1 and 2 area
  835. */
  836. return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
  837. #else
  838. #error unsupported page size
  839. #endif
  840. }
  841. static struct vm_operations_struct spufs_signal2_mmap_vmops = {
  842. .nopfn = spufs_signal2_mmap_nopfn,
  843. };
  844. static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
  845. {
  846. if (!(vma->vm_flags & VM_SHARED))
  847. return -EINVAL;
  848. vma->vm_flags |= VM_IO | VM_PFNMAP;
  849. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  850. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  851. vma->vm_ops = &spufs_signal2_mmap_vmops;
  852. return 0;
  853. }
  854. #else /* SPUFS_MMAP_4K */
  855. #define spufs_signal2_mmap NULL
  856. #endif /* !SPUFS_MMAP_4K */
  857. static const struct file_operations spufs_signal2_fops = {
  858. .open = spufs_signal2_open,
  859. .release = spufs_signal2_release,
  860. .read = spufs_signal2_read,
  861. .write = spufs_signal2_write,
  862. .mmap = spufs_signal2_mmap,
  863. };
  864. static void spufs_signal1_type_set(void *data, u64 val)
  865. {
  866. struct spu_context *ctx = data;
  867. spu_acquire(ctx);
  868. ctx->ops->signal1_type_set(ctx, val);
  869. spu_release(ctx);
  870. }
  871. static u64 __spufs_signal1_type_get(void *data)
  872. {
  873. struct spu_context *ctx = data;
  874. return ctx->ops->signal1_type_get(ctx);
  875. }
  876. static u64 spufs_signal1_type_get(void *data)
  877. {
  878. struct spu_context *ctx = data;
  879. u64 ret;
  880. spu_acquire(ctx);
  881. ret = __spufs_signal1_type_get(data);
  882. spu_release(ctx);
  883. return ret;
  884. }
  885. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  886. spufs_signal1_type_set, "%llu");
  887. static void spufs_signal2_type_set(void *data, u64 val)
  888. {
  889. struct spu_context *ctx = data;
  890. spu_acquire(ctx);
  891. ctx->ops->signal2_type_set(ctx, val);
  892. spu_release(ctx);
  893. }
  894. static u64 __spufs_signal2_type_get(void *data)
  895. {
  896. struct spu_context *ctx = data;
  897. return ctx->ops->signal2_type_get(ctx);
  898. }
  899. static u64 spufs_signal2_type_get(void *data)
  900. {
  901. struct spu_context *ctx = data;
  902. u64 ret;
  903. spu_acquire(ctx);
  904. ret = __spufs_signal2_type_get(data);
  905. spu_release(ctx);
  906. return ret;
  907. }
  908. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  909. spufs_signal2_type_set, "%llu");
  910. #if SPUFS_MMAP_4K
  911. static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
  912. unsigned long address)
  913. {
  914. return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
  915. }
  916. static struct vm_operations_struct spufs_mss_mmap_vmops = {
  917. .nopfn = spufs_mss_mmap_nopfn,
  918. };
  919. /*
  920. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  921. */
  922. static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
  923. {
  924. if (!(vma->vm_flags & VM_SHARED))
  925. return -EINVAL;
  926. vma->vm_flags |= VM_IO | VM_PFNMAP;
  927. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  928. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  929. vma->vm_ops = &spufs_mss_mmap_vmops;
  930. return 0;
  931. }
  932. #else /* SPUFS_MMAP_4K */
  933. #define spufs_mss_mmap NULL
  934. #endif /* !SPUFS_MMAP_4K */
  935. static int spufs_mss_open(struct inode *inode, struct file *file)
  936. {
  937. struct spufs_inode_info *i = SPUFS_I(inode);
  938. struct spu_context *ctx = i->i_ctx;
  939. file->private_data = i->i_ctx;
  940. mutex_lock(&ctx->mapping_lock);
  941. if (!i->i_openers++)
  942. ctx->mss = inode->i_mapping;
  943. mutex_unlock(&ctx->mapping_lock);
  944. return nonseekable_open(inode, file);
  945. }
  946. static int
  947. spufs_mss_release(struct inode *inode, struct file *file)
  948. {
  949. struct spufs_inode_info *i = SPUFS_I(inode);
  950. struct spu_context *ctx = i->i_ctx;
  951. mutex_lock(&ctx->mapping_lock);
  952. if (!--i->i_openers)
  953. ctx->mss = NULL;
  954. mutex_unlock(&ctx->mapping_lock);
  955. return 0;
  956. }
  957. static const struct file_operations spufs_mss_fops = {
  958. .open = spufs_mss_open,
  959. .release = spufs_mss_release,
  960. .mmap = spufs_mss_mmap,
  961. };
  962. static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
  963. unsigned long address)
  964. {
  965. return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
  966. }
  967. static struct vm_operations_struct spufs_psmap_mmap_vmops = {
  968. .nopfn = spufs_psmap_mmap_nopfn,
  969. };
  970. /*
  971. * mmap support for full problem state area [0x00000 - 0x1ffff].
  972. */
  973. static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
  974. {
  975. if (!(vma->vm_flags & VM_SHARED))
  976. return -EINVAL;
  977. vma->vm_flags |= VM_IO | VM_PFNMAP;
  978. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  979. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  980. vma->vm_ops = &spufs_psmap_mmap_vmops;
  981. return 0;
  982. }
  983. static int spufs_psmap_open(struct inode *inode, struct file *file)
  984. {
  985. struct spufs_inode_info *i = SPUFS_I(inode);
  986. struct spu_context *ctx = i->i_ctx;
  987. mutex_lock(&ctx->mapping_lock);
  988. file->private_data = i->i_ctx;
  989. if (!i->i_openers++)
  990. ctx->psmap = inode->i_mapping;
  991. mutex_unlock(&ctx->mapping_lock);
  992. return nonseekable_open(inode, file);
  993. }
  994. static int
  995. spufs_psmap_release(struct inode *inode, struct file *file)
  996. {
  997. struct spufs_inode_info *i = SPUFS_I(inode);
  998. struct spu_context *ctx = i->i_ctx;
  999. mutex_lock(&ctx->mapping_lock);
  1000. if (!--i->i_openers)
  1001. ctx->psmap = NULL;
  1002. mutex_unlock(&ctx->mapping_lock);
  1003. return 0;
  1004. }
  1005. static const struct file_operations spufs_psmap_fops = {
  1006. .open = spufs_psmap_open,
  1007. .release = spufs_psmap_release,
  1008. .mmap = spufs_psmap_mmap,
  1009. };
  1010. #if SPUFS_MMAP_4K
  1011. static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
  1012. unsigned long address)
  1013. {
  1014. return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
  1015. }
  1016. static struct vm_operations_struct spufs_mfc_mmap_vmops = {
  1017. .nopfn = spufs_mfc_mmap_nopfn,
  1018. };
  1019. /*
  1020. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  1021. */
  1022. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  1023. {
  1024. if (!(vma->vm_flags & VM_SHARED))
  1025. return -EINVAL;
  1026. vma->vm_flags |= VM_IO | VM_PFNMAP;
  1027. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  1028. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  1029. vma->vm_ops = &spufs_mfc_mmap_vmops;
  1030. return 0;
  1031. }
  1032. #else /* SPUFS_MMAP_4K */
  1033. #define spufs_mfc_mmap NULL
  1034. #endif /* !SPUFS_MMAP_4K */
  1035. static int spufs_mfc_open(struct inode *inode, struct file *file)
  1036. {
  1037. struct spufs_inode_info *i = SPUFS_I(inode);
  1038. struct spu_context *ctx = i->i_ctx;
  1039. /* we don't want to deal with DMA into other processes */
  1040. if (ctx->owner != current->mm)
  1041. return -EINVAL;
  1042. if (atomic_read(&inode->i_count) != 1)
  1043. return -EBUSY;
  1044. mutex_lock(&ctx->mapping_lock);
  1045. file->private_data = ctx;
  1046. if (!i->i_openers++)
  1047. ctx->mfc = inode->i_mapping;
  1048. mutex_unlock(&ctx->mapping_lock);
  1049. return nonseekable_open(inode, file);
  1050. }
  1051. static int
  1052. spufs_mfc_release(struct inode *inode, struct file *file)
  1053. {
  1054. struct spufs_inode_info *i = SPUFS_I(inode);
  1055. struct spu_context *ctx = i->i_ctx;
  1056. mutex_lock(&ctx->mapping_lock);
  1057. if (!--i->i_openers)
  1058. ctx->mfc = NULL;
  1059. mutex_unlock(&ctx->mapping_lock);
  1060. return 0;
  1061. }
  1062. /* interrupt-level mfc callback function. */
  1063. void spufs_mfc_callback(struct spu *spu)
  1064. {
  1065. struct spu_context *ctx = spu->ctx;
  1066. wake_up_all(&ctx->mfc_wq);
  1067. pr_debug("%s %s\n", __FUNCTION__, spu->name);
  1068. if (ctx->mfc_fasync) {
  1069. u32 free_elements, tagstatus;
  1070. unsigned int mask;
  1071. /* no need for spu_acquire in interrupt context */
  1072. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  1073. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  1074. mask = 0;
  1075. if (free_elements & 0xffff)
  1076. mask |= POLLOUT;
  1077. if (tagstatus & ctx->tagwait)
  1078. mask |= POLLIN;
  1079. kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
  1080. }
  1081. }
  1082. static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
  1083. {
  1084. /* See if there is one tag group is complete */
  1085. /* FIXME we need locking around tagwait */
  1086. *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
  1087. ctx->tagwait &= ~*status;
  1088. if (*status)
  1089. return 1;
  1090. /* enable interrupt waiting for any tag group,
  1091. may silently fail if interrupts are already enabled */
  1092. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  1093. return 0;
  1094. }
  1095. static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
  1096. size_t size, loff_t *pos)
  1097. {
  1098. struct spu_context *ctx = file->private_data;
  1099. int ret = -EINVAL;
  1100. u32 status;
  1101. if (size != 4)
  1102. goto out;
  1103. spu_acquire(ctx);
  1104. if (file->f_flags & O_NONBLOCK) {
  1105. status = ctx->ops->read_mfc_tagstatus(ctx);
  1106. if (!(status & ctx->tagwait))
  1107. ret = -EAGAIN;
  1108. else
  1109. ctx->tagwait &= ~status;
  1110. } else {
  1111. ret = spufs_wait(ctx->mfc_wq,
  1112. spufs_read_mfc_tagstatus(ctx, &status));
  1113. }
  1114. spu_release(ctx);
  1115. if (ret)
  1116. goto out;
  1117. ret = 4;
  1118. if (copy_to_user(buffer, &status, 4))
  1119. ret = -EFAULT;
  1120. out:
  1121. return ret;
  1122. }
  1123. static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
  1124. {
  1125. pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
  1126. cmd->ea, cmd->size, cmd->tag, cmd->cmd);
  1127. switch (cmd->cmd) {
  1128. case MFC_PUT_CMD:
  1129. case MFC_PUTF_CMD:
  1130. case MFC_PUTB_CMD:
  1131. case MFC_GET_CMD:
  1132. case MFC_GETF_CMD:
  1133. case MFC_GETB_CMD:
  1134. break;
  1135. default:
  1136. pr_debug("invalid DMA opcode %x\n", cmd->cmd);
  1137. return -EIO;
  1138. }
  1139. if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
  1140. pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
  1141. cmd->ea, cmd->lsa);
  1142. return -EIO;
  1143. }
  1144. switch (cmd->size & 0xf) {
  1145. case 1:
  1146. break;
  1147. case 2:
  1148. if (cmd->lsa & 1)
  1149. goto error;
  1150. break;
  1151. case 4:
  1152. if (cmd->lsa & 3)
  1153. goto error;
  1154. break;
  1155. case 8:
  1156. if (cmd->lsa & 7)
  1157. goto error;
  1158. break;
  1159. case 0:
  1160. if (cmd->lsa & 15)
  1161. goto error;
  1162. break;
  1163. error:
  1164. default:
  1165. pr_debug("invalid DMA alignment %x for size %x\n",
  1166. cmd->lsa & 0xf, cmd->size);
  1167. return -EIO;
  1168. }
  1169. if (cmd->size > 16 * 1024) {
  1170. pr_debug("invalid DMA size %x\n", cmd->size);
  1171. return -EIO;
  1172. }
  1173. if (cmd->tag & 0xfff0) {
  1174. /* we reserve the higher tag numbers for kernel use */
  1175. pr_debug("invalid DMA tag\n");
  1176. return -EIO;
  1177. }
  1178. if (cmd->class) {
  1179. /* not supported in this version */
  1180. pr_debug("invalid DMA class\n");
  1181. return -EIO;
  1182. }
  1183. return 0;
  1184. }
  1185. static int spu_send_mfc_command(struct spu_context *ctx,
  1186. struct mfc_dma_command cmd,
  1187. int *error)
  1188. {
  1189. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  1190. if (*error == -EAGAIN) {
  1191. /* wait for any tag group to complete
  1192. so we have space for the new command */
  1193. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  1194. /* try again, because the queue might be
  1195. empty again */
  1196. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  1197. if (*error == -EAGAIN)
  1198. return 0;
  1199. }
  1200. return 1;
  1201. }
  1202. static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
  1203. size_t size, loff_t *pos)
  1204. {
  1205. struct spu_context *ctx = file->private_data;
  1206. struct mfc_dma_command cmd;
  1207. int ret = -EINVAL;
  1208. if (size != sizeof cmd)
  1209. goto out;
  1210. ret = -EFAULT;
  1211. if (copy_from_user(&cmd, buffer, sizeof cmd))
  1212. goto out;
  1213. ret = spufs_check_valid_dma(&cmd);
  1214. if (ret)
  1215. goto out;
  1216. ret = spu_acquire_runnable(ctx, 0);
  1217. if (ret)
  1218. goto out;
  1219. if (file->f_flags & O_NONBLOCK) {
  1220. ret = ctx->ops->send_mfc_command(ctx, &cmd);
  1221. } else {
  1222. int status;
  1223. ret = spufs_wait(ctx->mfc_wq,
  1224. spu_send_mfc_command(ctx, cmd, &status));
  1225. if (status)
  1226. ret = status;
  1227. }
  1228. spu_release(ctx);
  1229. if (ret)
  1230. goto out;
  1231. ctx->tagwait |= 1 << cmd.tag;
  1232. ret = size;
  1233. out:
  1234. return ret;
  1235. }
  1236. static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
  1237. {
  1238. struct spu_context *ctx = file->private_data;
  1239. u32 free_elements, tagstatus;
  1240. unsigned int mask;
  1241. spu_acquire(ctx);
  1242. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
  1243. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  1244. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  1245. spu_release(ctx);
  1246. poll_wait(file, &ctx->mfc_wq, wait);
  1247. mask = 0;
  1248. if (free_elements & 0xffff)
  1249. mask |= POLLOUT | POLLWRNORM;
  1250. if (tagstatus & ctx->tagwait)
  1251. mask |= POLLIN | POLLRDNORM;
  1252. pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
  1253. free_elements, tagstatus, ctx->tagwait);
  1254. return mask;
  1255. }
  1256. static int spufs_mfc_flush(struct file *file, fl_owner_t id)
  1257. {
  1258. struct spu_context *ctx = file->private_data;
  1259. int ret;
  1260. spu_acquire(ctx);
  1261. #if 0
  1262. /* this currently hangs */
  1263. ret = spufs_wait(ctx->mfc_wq,
  1264. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
  1265. if (ret)
  1266. goto out;
  1267. ret = spufs_wait(ctx->mfc_wq,
  1268. ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
  1269. out:
  1270. #else
  1271. ret = 0;
  1272. #endif
  1273. spu_release(ctx);
  1274. return ret;
  1275. }
  1276. static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
  1277. int datasync)
  1278. {
  1279. return spufs_mfc_flush(file, NULL);
  1280. }
  1281. static int spufs_mfc_fasync(int fd, struct file *file, int on)
  1282. {
  1283. struct spu_context *ctx = file->private_data;
  1284. return fasync_helper(fd, file, on, &ctx->mfc_fasync);
  1285. }
  1286. static const struct file_operations spufs_mfc_fops = {
  1287. .open = spufs_mfc_open,
  1288. .release = spufs_mfc_release,
  1289. .read = spufs_mfc_read,
  1290. .write = spufs_mfc_write,
  1291. .poll = spufs_mfc_poll,
  1292. .flush = spufs_mfc_flush,
  1293. .fsync = spufs_mfc_fsync,
  1294. .fasync = spufs_mfc_fasync,
  1295. .mmap = spufs_mfc_mmap,
  1296. };
  1297. static void spufs_npc_set(void *data, u64 val)
  1298. {
  1299. struct spu_context *ctx = data;
  1300. spu_acquire(ctx);
  1301. ctx->ops->npc_write(ctx, val);
  1302. spu_release(ctx);
  1303. }
  1304. static u64 spufs_npc_get(void *data)
  1305. {
  1306. struct spu_context *ctx = data;
  1307. u64 ret;
  1308. spu_acquire(ctx);
  1309. ret = ctx->ops->npc_read(ctx);
  1310. spu_release(ctx);
  1311. return ret;
  1312. }
  1313. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
  1314. "0x%llx\n")
  1315. static void spufs_decr_set(void *data, u64 val)
  1316. {
  1317. struct spu_context *ctx = data;
  1318. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1319. spu_acquire_saved(ctx);
  1320. lscsa->decr.slot[0] = (u32) val;
  1321. spu_release(ctx);
  1322. }
  1323. static u64 __spufs_decr_get(void *data)
  1324. {
  1325. struct spu_context *ctx = data;
  1326. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1327. return lscsa->decr.slot[0];
  1328. }
  1329. static u64 spufs_decr_get(void *data)
  1330. {
  1331. struct spu_context *ctx = data;
  1332. u64 ret;
  1333. spu_acquire_saved(ctx);
  1334. ret = __spufs_decr_get(data);
  1335. spu_release(ctx);
  1336. return ret;
  1337. }
  1338. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  1339. "0x%llx\n")
  1340. static void spufs_decr_status_set(void *data, u64 val)
  1341. {
  1342. struct spu_context *ctx = data;
  1343. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1344. spu_acquire_saved(ctx);
  1345. lscsa->decr_status.slot[0] = (u32) val;
  1346. spu_release(ctx);
  1347. }
  1348. static u64 __spufs_decr_status_get(void *data)
  1349. {
  1350. struct spu_context *ctx = data;
  1351. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1352. return lscsa->decr_status.slot[0];
  1353. }
  1354. static u64 spufs_decr_status_get(void *data)
  1355. {
  1356. struct spu_context *ctx = data;
  1357. u64 ret;
  1358. spu_acquire_saved(ctx);
  1359. ret = __spufs_decr_status_get(data);
  1360. spu_release(ctx);
  1361. return ret;
  1362. }
  1363. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  1364. spufs_decr_status_set, "0x%llx\n")
  1365. static void spufs_event_mask_set(void *data, u64 val)
  1366. {
  1367. struct spu_context *ctx = data;
  1368. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1369. spu_acquire_saved(ctx);
  1370. lscsa->event_mask.slot[0] = (u32) val;
  1371. spu_release(ctx);
  1372. }
  1373. static u64 __spufs_event_mask_get(void *data)
  1374. {
  1375. struct spu_context *ctx = data;
  1376. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1377. return lscsa->event_mask.slot[0];
  1378. }
  1379. static u64 spufs_event_mask_get(void *data)
  1380. {
  1381. struct spu_context *ctx = data;
  1382. u64 ret;
  1383. spu_acquire_saved(ctx);
  1384. ret = __spufs_event_mask_get(data);
  1385. spu_release(ctx);
  1386. return ret;
  1387. }
  1388. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1389. spufs_event_mask_set, "0x%llx\n")
  1390. static u64 __spufs_event_status_get(void *data)
  1391. {
  1392. struct spu_context *ctx = data;
  1393. struct spu_state *state = &ctx->csa;
  1394. u64 stat;
  1395. stat = state->spu_chnlcnt_RW[0];
  1396. if (stat)
  1397. return state->spu_chnldata_RW[0];
  1398. return 0;
  1399. }
  1400. static u64 spufs_event_status_get(void *data)
  1401. {
  1402. struct spu_context *ctx = data;
  1403. u64 ret = 0;
  1404. spu_acquire_saved(ctx);
  1405. ret = __spufs_event_status_get(data);
  1406. spu_release(ctx);
  1407. return ret;
  1408. }
  1409. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
  1410. NULL, "0x%llx\n")
  1411. static void spufs_srr0_set(void *data, u64 val)
  1412. {
  1413. struct spu_context *ctx = data;
  1414. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1415. spu_acquire_saved(ctx);
  1416. lscsa->srr0.slot[0] = (u32) val;
  1417. spu_release(ctx);
  1418. }
  1419. static u64 spufs_srr0_get(void *data)
  1420. {
  1421. struct spu_context *ctx = data;
  1422. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1423. u64 ret;
  1424. spu_acquire_saved(ctx);
  1425. ret = lscsa->srr0.slot[0];
  1426. spu_release(ctx);
  1427. return ret;
  1428. }
  1429. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1430. "0x%llx\n")
  1431. static u64 spufs_id_get(void *data)
  1432. {
  1433. struct spu_context *ctx = data;
  1434. u64 num;
  1435. spu_acquire(ctx);
  1436. if (ctx->state == SPU_STATE_RUNNABLE)
  1437. num = ctx->spu->number;
  1438. else
  1439. num = (unsigned int)-1;
  1440. spu_release(ctx);
  1441. return num;
  1442. }
  1443. DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
  1444. static u64 __spufs_object_id_get(void *data)
  1445. {
  1446. struct spu_context *ctx = data;
  1447. return ctx->object_id;
  1448. }
  1449. static u64 spufs_object_id_get(void *data)
  1450. {
  1451. /* FIXME: Should there really be no locking here? */
  1452. return __spufs_object_id_get(data);
  1453. }
  1454. static void spufs_object_id_set(void *data, u64 id)
  1455. {
  1456. struct spu_context *ctx = data;
  1457. ctx->object_id = id;
  1458. }
  1459. DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
  1460. spufs_object_id_set, "0x%llx\n");
  1461. static u64 __spufs_lslr_get(void *data)
  1462. {
  1463. struct spu_context *ctx = data;
  1464. return ctx->csa.priv2.spu_lslr_RW;
  1465. }
  1466. static u64 spufs_lslr_get(void *data)
  1467. {
  1468. struct spu_context *ctx = data;
  1469. u64 ret;
  1470. spu_acquire_saved(ctx);
  1471. ret = __spufs_lslr_get(data);
  1472. spu_release(ctx);
  1473. return ret;
  1474. }
  1475. DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
  1476. static int spufs_info_open(struct inode *inode, struct file *file)
  1477. {
  1478. struct spufs_inode_info *i = SPUFS_I(inode);
  1479. struct spu_context *ctx = i->i_ctx;
  1480. file->private_data = ctx;
  1481. return 0;
  1482. }
  1483. static int spufs_caps_show(struct seq_file *s, void *private)
  1484. {
  1485. struct spu_context *ctx = s->private;
  1486. if (!(ctx->flags & SPU_CREATE_NOSCHED))
  1487. seq_puts(s, "sched\n");
  1488. if (!(ctx->flags & SPU_CREATE_ISOLATE))
  1489. seq_puts(s, "step\n");
  1490. return 0;
  1491. }
  1492. static int spufs_caps_open(struct inode *inode, struct file *file)
  1493. {
  1494. return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
  1495. }
  1496. static const struct file_operations spufs_caps_fops = {
  1497. .open = spufs_caps_open,
  1498. .read = seq_read,
  1499. .llseek = seq_lseek,
  1500. .release = single_release,
  1501. };
  1502. static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
  1503. char __user *buf, size_t len, loff_t *pos)
  1504. {
  1505. u32 mbox_stat;
  1506. u32 data;
  1507. mbox_stat = ctx->csa.prob.mb_stat_R;
  1508. if (mbox_stat & 0x0000ff) {
  1509. data = ctx->csa.prob.pu_mb_R;
  1510. }
  1511. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1512. }
  1513. static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
  1514. size_t len, loff_t *pos)
  1515. {
  1516. int ret;
  1517. struct spu_context *ctx = file->private_data;
  1518. if (!access_ok(VERIFY_WRITE, buf, len))
  1519. return -EFAULT;
  1520. spu_acquire_saved(ctx);
  1521. spin_lock(&ctx->csa.register_lock);
  1522. ret = __spufs_mbox_info_read(ctx, buf, len, pos);
  1523. spin_unlock(&ctx->csa.register_lock);
  1524. spu_release(ctx);
  1525. return ret;
  1526. }
  1527. static const struct file_operations spufs_mbox_info_fops = {
  1528. .open = spufs_info_open,
  1529. .read = spufs_mbox_info_read,
  1530. .llseek = generic_file_llseek,
  1531. };
  1532. static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
  1533. char __user *buf, size_t len, loff_t *pos)
  1534. {
  1535. u32 ibox_stat;
  1536. u32 data;
  1537. ibox_stat = ctx->csa.prob.mb_stat_R;
  1538. if (ibox_stat & 0xff0000) {
  1539. data = ctx->csa.priv2.puint_mb_R;
  1540. }
  1541. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1542. }
  1543. static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
  1544. size_t len, loff_t *pos)
  1545. {
  1546. struct spu_context *ctx = file->private_data;
  1547. int ret;
  1548. if (!access_ok(VERIFY_WRITE, buf, len))
  1549. return -EFAULT;
  1550. spu_acquire_saved(ctx);
  1551. spin_lock(&ctx->csa.register_lock);
  1552. ret = __spufs_ibox_info_read(ctx, buf, len, pos);
  1553. spin_unlock(&ctx->csa.register_lock);
  1554. spu_release(ctx);
  1555. return ret;
  1556. }
  1557. static const struct file_operations spufs_ibox_info_fops = {
  1558. .open = spufs_info_open,
  1559. .read = spufs_ibox_info_read,
  1560. .llseek = generic_file_llseek,
  1561. };
  1562. static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
  1563. char __user *buf, size_t len, loff_t *pos)
  1564. {
  1565. int i, cnt;
  1566. u32 data[4];
  1567. u32 wbox_stat;
  1568. wbox_stat = ctx->csa.prob.mb_stat_R;
  1569. cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
  1570. for (i = 0; i < cnt; i++) {
  1571. data[i] = ctx->csa.spu_mailbox_data[i];
  1572. }
  1573. return simple_read_from_buffer(buf, len, pos, &data,
  1574. cnt * sizeof(u32));
  1575. }
  1576. static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
  1577. size_t len, loff_t *pos)
  1578. {
  1579. struct spu_context *ctx = file->private_data;
  1580. int ret;
  1581. if (!access_ok(VERIFY_WRITE, buf, len))
  1582. return -EFAULT;
  1583. spu_acquire_saved(ctx);
  1584. spin_lock(&ctx->csa.register_lock);
  1585. ret = __spufs_wbox_info_read(ctx, buf, len, pos);
  1586. spin_unlock(&ctx->csa.register_lock);
  1587. spu_release(ctx);
  1588. return ret;
  1589. }
  1590. static const struct file_operations spufs_wbox_info_fops = {
  1591. .open = spufs_info_open,
  1592. .read = spufs_wbox_info_read,
  1593. .llseek = generic_file_llseek,
  1594. };
  1595. static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
  1596. char __user *buf, size_t len, loff_t *pos)
  1597. {
  1598. struct spu_dma_info info;
  1599. struct mfc_cq_sr *qp, *spuqp;
  1600. int i;
  1601. info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
  1602. info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
  1603. info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
  1604. info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
  1605. info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
  1606. for (i = 0; i < 16; i++) {
  1607. qp = &info.dma_info_command_data[i];
  1608. spuqp = &ctx->csa.priv2.spuq[i];
  1609. qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
  1610. qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
  1611. qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
  1612. qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
  1613. }
  1614. return simple_read_from_buffer(buf, len, pos, &info,
  1615. sizeof info);
  1616. }
  1617. static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
  1618. size_t len, loff_t *pos)
  1619. {
  1620. struct spu_context *ctx = file->private_data;
  1621. int ret;
  1622. if (!access_ok(VERIFY_WRITE, buf, len))
  1623. return -EFAULT;
  1624. spu_acquire_saved(ctx);
  1625. spin_lock(&ctx->csa.register_lock);
  1626. ret = __spufs_dma_info_read(ctx, buf, len, pos);
  1627. spin_unlock(&ctx->csa.register_lock);
  1628. spu_release(ctx);
  1629. return ret;
  1630. }
  1631. static const struct file_operations spufs_dma_info_fops = {
  1632. .open = spufs_info_open,
  1633. .read = spufs_dma_info_read,
  1634. };
  1635. static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
  1636. char __user *buf, size_t len, loff_t *pos)
  1637. {
  1638. struct spu_proxydma_info info;
  1639. struct mfc_cq_sr *qp, *puqp;
  1640. int ret = sizeof info;
  1641. int i;
  1642. if (len < ret)
  1643. return -EINVAL;
  1644. if (!access_ok(VERIFY_WRITE, buf, len))
  1645. return -EFAULT;
  1646. info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
  1647. info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
  1648. info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
  1649. for (i = 0; i < 8; i++) {
  1650. qp = &info.proxydma_info_command_data[i];
  1651. puqp = &ctx->csa.priv2.puq[i];
  1652. qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
  1653. qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
  1654. qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
  1655. qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
  1656. }
  1657. return simple_read_from_buffer(buf, len, pos, &info,
  1658. sizeof info);
  1659. }
  1660. static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
  1661. size_t len, loff_t *pos)
  1662. {
  1663. struct spu_context *ctx = file->private_data;
  1664. int ret;
  1665. spu_acquire_saved(ctx);
  1666. spin_lock(&ctx->csa.register_lock);
  1667. ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
  1668. spin_unlock(&ctx->csa.register_lock);
  1669. spu_release(ctx);
  1670. return ret;
  1671. }
  1672. static const struct file_operations spufs_proxydma_info_fops = {
  1673. .open = spufs_info_open,
  1674. .read = spufs_proxydma_info_read,
  1675. };
  1676. static int spufs_show_tid(struct seq_file *s, void *private)
  1677. {
  1678. struct spu_context *ctx = s->private;
  1679. seq_printf(s, "%d\n", ctx->tid);
  1680. return 0;
  1681. }
  1682. static int spufs_tid_open(struct inode *inode, struct file *file)
  1683. {
  1684. return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
  1685. }
  1686. static const struct file_operations spufs_tid_fops = {
  1687. .open = spufs_tid_open,
  1688. .read = seq_read,
  1689. .llseek = seq_lseek,
  1690. .release = single_release,
  1691. };
  1692. struct tree_descr spufs_dir_contents[] = {
  1693. { "capabilities", &spufs_caps_fops, 0444, },
  1694. { "mem", &spufs_mem_fops, 0666, },
  1695. { "regs", &spufs_regs_fops, 0666, },
  1696. { "mbox", &spufs_mbox_fops, 0444, },
  1697. { "ibox", &spufs_ibox_fops, 0444, },
  1698. { "wbox", &spufs_wbox_fops, 0222, },
  1699. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1700. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1701. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1702. { "signal1", &spufs_signal1_fops, 0666, },
  1703. { "signal2", &spufs_signal2_fops, 0666, },
  1704. { "signal1_type", &spufs_signal1_type, 0666, },
  1705. { "signal2_type", &spufs_signal2_type, 0666, },
  1706. { "cntl", &spufs_cntl_fops, 0666, },
  1707. { "fpcr", &spufs_fpcr_fops, 0666, },
  1708. { "lslr", &spufs_lslr_ops, 0444, },
  1709. { "mfc", &spufs_mfc_fops, 0666, },
  1710. { "mss", &spufs_mss_fops, 0666, },
  1711. { "npc", &spufs_npc_ops, 0666, },
  1712. { "srr0", &spufs_srr0_ops, 0666, },
  1713. { "decr", &spufs_decr_ops, 0666, },
  1714. { "decr_status", &spufs_decr_status_ops, 0666, },
  1715. { "event_mask", &spufs_event_mask_ops, 0666, },
  1716. { "event_status", &spufs_event_status_ops, 0444, },
  1717. { "psmap", &spufs_psmap_fops, 0666, },
  1718. { "phys-id", &spufs_id_ops, 0666, },
  1719. { "object-id", &spufs_object_id_ops, 0666, },
  1720. { "mbox_info", &spufs_mbox_info_fops, 0444, },
  1721. { "ibox_info", &spufs_ibox_info_fops, 0444, },
  1722. { "wbox_info", &spufs_wbox_info_fops, 0444, },
  1723. { "dma_info", &spufs_dma_info_fops, 0444, },
  1724. { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
  1725. { "tid", &spufs_tid_fops, 0444, },
  1726. {},
  1727. };
  1728. struct tree_descr spufs_dir_nosched_contents[] = {
  1729. { "capabilities", &spufs_caps_fops, 0444, },
  1730. { "mem", &spufs_mem_fops, 0666, },
  1731. { "mbox", &spufs_mbox_fops, 0444, },
  1732. { "ibox", &spufs_ibox_fops, 0444, },
  1733. { "wbox", &spufs_wbox_fops, 0222, },
  1734. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1735. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1736. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1737. { "signal1", &spufs_signal1_fops, 0666, },
  1738. { "signal2", &spufs_signal2_fops, 0666, },
  1739. { "signal1_type", &spufs_signal1_type, 0666, },
  1740. { "signal2_type", &spufs_signal2_type, 0666, },
  1741. { "mss", &spufs_mss_fops, 0666, },
  1742. { "mfc", &spufs_mfc_fops, 0666, },
  1743. { "cntl", &spufs_cntl_fops, 0666, },
  1744. { "npc", &spufs_npc_ops, 0666, },
  1745. { "psmap", &spufs_psmap_fops, 0666, },
  1746. { "phys-id", &spufs_id_ops, 0666, },
  1747. { "object-id", &spufs_object_id_ops, 0666, },
  1748. { "tid", &spufs_tid_fops, 0444, },
  1749. {},
  1750. };
  1751. struct spufs_coredump_reader spufs_coredump_read[] = {
  1752. { "regs", __spufs_regs_read, NULL, 128 * 16 },
  1753. { "fpcr", __spufs_fpcr_read, NULL, 16 },
  1754. { "lslr", NULL, __spufs_lslr_get, 11 },
  1755. { "decr", NULL, __spufs_decr_get, 11 },
  1756. { "decr_status", NULL, __spufs_decr_status_get, 11 },
  1757. { "mem", __spufs_mem_read, NULL, 256 * 1024, },
  1758. { "signal1", __spufs_signal1_read, NULL, 4 },
  1759. { "signal1_type", NULL, __spufs_signal1_type_get, 2 },
  1760. { "signal2", __spufs_signal2_read, NULL, 4 },
  1761. { "signal2_type", NULL, __spufs_signal2_type_get, 2 },
  1762. { "event_mask", NULL, __spufs_event_mask_get, 8 },
  1763. { "event_status", NULL, __spufs_event_status_get, 8 },
  1764. { "mbox_info", __spufs_mbox_info_read, NULL, 4 },
  1765. { "ibox_info", __spufs_ibox_info_read, NULL, 4 },
  1766. { "wbox_info", __spufs_wbox_info_read, NULL, 16 },
  1767. { "dma_info", __spufs_dma_info_read, NULL, 69 * 8 },
  1768. { "proxydma_info", __spufs_proxydma_info_read, NULL, 35 * 8 },
  1769. { "object-id", NULL, __spufs_object_id_get, 19 },
  1770. { },
  1771. };
  1772. int spufs_coredump_num_notes = ARRAY_SIZE(spufs_coredump_read) - 1;