file.c 51 KB

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