file.c 52 KB

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