file.c 52 KB

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