file.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  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. 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_saved(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_saved(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_saved(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_saved(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_saved(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 const struct file_operations spufs_signal1_nosched_fops = {
  762. .open = spufs_signal1_open,
  763. .release = spufs_signal1_release,
  764. .write = spufs_signal1_write,
  765. .mmap = spufs_signal1_mmap,
  766. };
  767. static int spufs_signal2_open(struct inode *inode, struct file *file)
  768. {
  769. struct spufs_inode_info *i = SPUFS_I(inode);
  770. struct spu_context *ctx = i->i_ctx;
  771. mutex_lock(&ctx->mapping_lock);
  772. file->private_data = ctx;
  773. if (!i->i_openers++)
  774. ctx->signal2 = inode->i_mapping;
  775. mutex_unlock(&ctx->mapping_lock);
  776. return nonseekable_open(inode, file);
  777. }
  778. static int
  779. spufs_signal2_release(struct inode *inode, struct file *file)
  780. {
  781. struct spufs_inode_info *i = SPUFS_I(inode);
  782. struct spu_context *ctx = i->i_ctx;
  783. mutex_lock(&ctx->mapping_lock);
  784. if (!--i->i_openers)
  785. ctx->signal2 = NULL;
  786. mutex_unlock(&ctx->mapping_lock);
  787. return 0;
  788. }
  789. static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
  790. size_t len, loff_t *pos)
  791. {
  792. int ret = 0;
  793. u32 data;
  794. if (len < 4)
  795. return -EINVAL;
  796. if (ctx->csa.spu_chnlcnt_RW[4]) {
  797. data = ctx->csa.spu_chnldata_RW[4];
  798. ret = 4;
  799. }
  800. if (!ret)
  801. goto out;
  802. if (copy_to_user(buf, &data, 4))
  803. return -EFAULT;
  804. out:
  805. return ret;
  806. }
  807. static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
  808. size_t len, loff_t *pos)
  809. {
  810. struct spu_context *ctx = file->private_data;
  811. int ret;
  812. spu_acquire_saved(ctx);
  813. ret = __spufs_signal2_read(ctx, buf, len, pos);
  814. spu_release_saved(ctx);
  815. return ret;
  816. }
  817. static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
  818. size_t len, loff_t *pos)
  819. {
  820. struct spu_context *ctx;
  821. u32 data;
  822. ctx = file->private_data;
  823. if (len < 4)
  824. return -EINVAL;
  825. if (copy_from_user(&data, buf, 4))
  826. return -EFAULT;
  827. spu_acquire(ctx);
  828. ctx->ops->signal2_write(ctx, data);
  829. spu_release(ctx);
  830. return 4;
  831. }
  832. #if SPUFS_MMAP_4K
  833. static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
  834. unsigned long address)
  835. {
  836. #if PAGE_SIZE == 0x1000
  837. return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
  838. #elif PAGE_SIZE == 0x10000
  839. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  840. * signal 1 and 2 area
  841. */
  842. return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
  843. #else
  844. #error unsupported page size
  845. #endif
  846. }
  847. static struct vm_operations_struct spufs_signal2_mmap_vmops = {
  848. .nopfn = spufs_signal2_mmap_nopfn,
  849. };
  850. static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
  851. {
  852. if (!(vma->vm_flags & VM_SHARED))
  853. return -EINVAL;
  854. vma->vm_flags |= VM_IO | VM_PFNMAP;
  855. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  856. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  857. vma->vm_ops = &spufs_signal2_mmap_vmops;
  858. return 0;
  859. }
  860. #else /* SPUFS_MMAP_4K */
  861. #define spufs_signal2_mmap NULL
  862. #endif /* !SPUFS_MMAP_4K */
  863. static const struct file_operations spufs_signal2_fops = {
  864. .open = spufs_signal2_open,
  865. .release = spufs_signal2_release,
  866. .read = spufs_signal2_read,
  867. .write = spufs_signal2_write,
  868. .mmap = spufs_signal2_mmap,
  869. };
  870. static const struct file_operations spufs_signal2_nosched_fops = {
  871. .open = spufs_signal2_open,
  872. .release = spufs_signal2_release,
  873. .write = spufs_signal2_write,
  874. .mmap = spufs_signal2_mmap,
  875. };
  876. static void spufs_signal1_type_set(void *data, u64 val)
  877. {
  878. struct spu_context *ctx = data;
  879. spu_acquire(ctx);
  880. ctx->ops->signal1_type_set(ctx, val);
  881. spu_release(ctx);
  882. }
  883. static u64 __spufs_signal1_type_get(void *data)
  884. {
  885. struct spu_context *ctx = data;
  886. return ctx->ops->signal1_type_get(ctx);
  887. }
  888. static u64 spufs_signal1_type_get(void *data)
  889. {
  890. struct spu_context *ctx = data;
  891. u64 ret;
  892. spu_acquire(ctx);
  893. ret = __spufs_signal1_type_get(data);
  894. spu_release(ctx);
  895. return ret;
  896. }
  897. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  898. spufs_signal1_type_set, "%llu");
  899. static void spufs_signal2_type_set(void *data, u64 val)
  900. {
  901. struct spu_context *ctx = data;
  902. spu_acquire(ctx);
  903. ctx->ops->signal2_type_set(ctx, val);
  904. spu_release(ctx);
  905. }
  906. static u64 __spufs_signal2_type_get(void *data)
  907. {
  908. struct spu_context *ctx = data;
  909. return ctx->ops->signal2_type_get(ctx);
  910. }
  911. static u64 spufs_signal2_type_get(void *data)
  912. {
  913. struct spu_context *ctx = data;
  914. u64 ret;
  915. spu_acquire(ctx);
  916. ret = __spufs_signal2_type_get(data);
  917. spu_release(ctx);
  918. return ret;
  919. }
  920. DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  921. spufs_signal2_type_set, "%llu");
  922. #if SPUFS_MMAP_4K
  923. static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
  924. unsigned long address)
  925. {
  926. return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
  927. }
  928. static struct vm_operations_struct spufs_mss_mmap_vmops = {
  929. .nopfn = spufs_mss_mmap_nopfn,
  930. };
  931. /*
  932. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  933. */
  934. static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
  935. {
  936. if (!(vma->vm_flags & VM_SHARED))
  937. return -EINVAL;
  938. vma->vm_flags |= VM_IO | VM_PFNMAP;
  939. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  940. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  941. vma->vm_ops = &spufs_mss_mmap_vmops;
  942. return 0;
  943. }
  944. #else /* SPUFS_MMAP_4K */
  945. #define spufs_mss_mmap NULL
  946. #endif /* !SPUFS_MMAP_4K */
  947. static int spufs_mss_open(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. file->private_data = i->i_ctx;
  952. mutex_lock(&ctx->mapping_lock);
  953. if (!i->i_openers++)
  954. ctx->mss = inode->i_mapping;
  955. mutex_unlock(&ctx->mapping_lock);
  956. return nonseekable_open(inode, file);
  957. }
  958. static int
  959. spufs_mss_release(struct inode *inode, struct file *file)
  960. {
  961. struct spufs_inode_info *i = SPUFS_I(inode);
  962. struct spu_context *ctx = i->i_ctx;
  963. mutex_lock(&ctx->mapping_lock);
  964. if (!--i->i_openers)
  965. ctx->mss = NULL;
  966. mutex_unlock(&ctx->mapping_lock);
  967. return 0;
  968. }
  969. static const struct file_operations spufs_mss_fops = {
  970. .open = spufs_mss_open,
  971. .release = spufs_mss_release,
  972. .mmap = spufs_mss_mmap,
  973. };
  974. static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
  975. unsigned long address)
  976. {
  977. return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
  978. }
  979. static struct vm_operations_struct spufs_psmap_mmap_vmops = {
  980. .nopfn = spufs_psmap_mmap_nopfn,
  981. };
  982. /*
  983. * mmap support for full problem state area [0x00000 - 0x1ffff].
  984. */
  985. static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
  986. {
  987. if (!(vma->vm_flags & VM_SHARED))
  988. return -EINVAL;
  989. vma->vm_flags |= VM_IO | VM_PFNMAP;
  990. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  991. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  992. vma->vm_ops = &spufs_psmap_mmap_vmops;
  993. return 0;
  994. }
  995. static int spufs_psmap_open(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. file->private_data = i->i_ctx;
  1001. if (!i->i_openers++)
  1002. ctx->psmap = inode->i_mapping;
  1003. mutex_unlock(&ctx->mapping_lock);
  1004. return nonseekable_open(inode, file);
  1005. }
  1006. static int
  1007. spufs_psmap_release(struct inode *inode, struct file *file)
  1008. {
  1009. struct spufs_inode_info *i = SPUFS_I(inode);
  1010. struct spu_context *ctx = i->i_ctx;
  1011. mutex_lock(&ctx->mapping_lock);
  1012. if (!--i->i_openers)
  1013. ctx->psmap = NULL;
  1014. mutex_unlock(&ctx->mapping_lock);
  1015. return 0;
  1016. }
  1017. static const struct file_operations spufs_psmap_fops = {
  1018. .open = spufs_psmap_open,
  1019. .release = spufs_psmap_release,
  1020. .mmap = spufs_psmap_mmap,
  1021. };
  1022. #if SPUFS_MMAP_4K
  1023. static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
  1024. unsigned long address)
  1025. {
  1026. return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
  1027. }
  1028. static struct vm_operations_struct spufs_mfc_mmap_vmops = {
  1029. .nopfn = spufs_mfc_mmap_nopfn,
  1030. };
  1031. /*
  1032. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  1033. */
  1034. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  1035. {
  1036. if (!(vma->vm_flags & VM_SHARED))
  1037. return -EINVAL;
  1038. vma->vm_flags |= VM_IO | VM_PFNMAP;
  1039. vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
  1040. | _PAGE_NO_CACHE | _PAGE_GUARDED);
  1041. vma->vm_ops = &spufs_mfc_mmap_vmops;
  1042. return 0;
  1043. }
  1044. #else /* SPUFS_MMAP_4K */
  1045. #define spufs_mfc_mmap NULL
  1046. #endif /* !SPUFS_MMAP_4K */
  1047. static int spufs_mfc_open(struct inode *inode, struct file *file)
  1048. {
  1049. struct spufs_inode_info *i = SPUFS_I(inode);
  1050. struct spu_context *ctx = i->i_ctx;
  1051. /* we don't want to deal with DMA into other processes */
  1052. if (ctx->owner != current->mm)
  1053. return -EINVAL;
  1054. if (atomic_read(&inode->i_count) != 1)
  1055. return -EBUSY;
  1056. mutex_lock(&ctx->mapping_lock);
  1057. file->private_data = ctx;
  1058. if (!i->i_openers++)
  1059. ctx->mfc = inode->i_mapping;
  1060. mutex_unlock(&ctx->mapping_lock);
  1061. return nonseekable_open(inode, file);
  1062. }
  1063. static int
  1064. spufs_mfc_release(struct inode *inode, struct file *file)
  1065. {
  1066. struct spufs_inode_info *i = SPUFS_I(inode);
  1067. struct spu_context *ctx = i->i_ctx;
  1068. mutex_lock(&ctx->mapping_lock);
  1069. if (!--i->i_openers)
  1070. ctx->mfc = NULL;
  1071. mutex_unlock(&ctx->mapping_lock);
  1072. return 0;
  1073. }
  1074. /* interrupt-level mfc callback function. */
  1075. void spufs_mfc_callback(struct spu *spu)
  1076. {
  1077. struct spu_context *ctx = spu->ctx;
  1078. wake_up_all(&ctx->mfc_wq);
  1079. pr_debug("%s %s\n", __FUNCTION__, spu->name);
  1080. if (ctx->mfc_fasync) {
  1081. u32 free_elements, tagstatus;
  1082. unsigned int mask;
  1083. /* no need for spu_acquire in interrupt context */
  1084. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  1085. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  1086. mask = 0;
  1087. if (free_elements & 0xffff)
  1088. mask |= POLLOUT;
  1089. if (tagstatus & ctx->tagwait)
  1090. mask |= POLLIN;
  1091. kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
  1092. }
  1093. }
  1094. static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
  1095. {
  1096. /* See if there is one tag group is complete */
  1097. /* FIXME we need locking around tagwait */
  1098. *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
  1099. ctx->tagwait &= ~*status;
  1100. if (*status)
  1101. return 1;
  1102. /* enable interrupt waiting for any tag group,
  1103. may silently fail if interrupts are already enabled */
  1104. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  1105. return 0;
  1106. }
  1107. static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
  1108. size_t size, loff_t *pos)
  1109. {
  1110. struct spu_context *ctx = file->private_data;
  1111. int ret = -EINVAL;
  1112. u32 status;
  1113. if (size != 4)
  1114. goto out;
  1115. spu_acquire(ctx);
  1116. if (file->f_flags & O_NONBLOCK) {
  1117. status = ctx->ops->read_mfc_tagstatus(ctx);
  1118. if (!(status & ctx->tagwait))
  1119. ret = -EAGAIN;
  1120. else
  1121. ctx->tagwait &= ~status;
  1122. } else {
  1123. ret = spufs_wait(ctx->mfc_wq,
  1124. spufs_read_mfc_tagstatus(ctx, &status));
  1125. }
  1126. spu_release(ctx);
  1127. if (ret)
  1128. goto out;
  1129. ret = 4;
  1130. if (copy_to_user(buffer, &status, 4))
  1131. ret = -EFAULT;
  1132. out:
  1133. return ret;
  1134. }
  1135. static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
  1136. {
  1137. pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
  1138. cmd->ea, cmd->size, cmd->tag, cmd->cmd);
  1139. switch (cmd->cmd) {
  1140. case MFC_PUT_CMD:
  1141. case MFC_PUTF_CMD:
  1142. case MFC_PUTB_CMD:
  1143. case MFC_GET_CMD:
  1144. case MFC_GETF_CMD:
  1145. case MFC_GETB_CMD:
  1146. break;
  1147. default:
  1148. pr_debug("invalid DMA opcode %x\n", cmd->cmd);
  1149. return -EIO;
  1150. }
  1151. if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
  1152. pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
  1153. cmd->ea, cmd->lsa);
  1154. return -EIO;
  1155. }
  1156. switch (cmd->size & 0xf) {
  1157. case 1:
  1158. break;
  1159. case 2:
  1160. if (cmd->lsa & 1)
  1161. goto error;
  1162. break;
  1163. case 4:
  1164. if (cmd->lsa & 3)
  1165. goto error;
  1166. break;
  1167. case 8:
  1168. if (cmd->lsa & 7)
  1169. goto error;
  1170. break;
  1171. case 0:
  1172. if (cmd->lsa & 15)
  1173. goto error;
  1174. break;
  1175. error:
  1176. default:
  1177. pr_debug("invalid DMA alignment %x for size %x\n",
  1178. cmd->lsa & 0xf, cmd->size);
  1179. return -EIO;
  1180. }
  1181. if (cmd->size > 16 * 1024) {
  1182. pr_debug("invalid DMA size %x\n", cmd->size);
  1183. return -EIO;
  1184. }
  1185. if (cmd->tag & 0xfff0) {
  1186. /* we reserve the higher tag numbers for kernel use */
  1187. pr_debug("invalid DMA tag\n");
  1188. return -EIO;
  1189. }
  1190. if (cmd->class) {
  1191. /* not supported in this version */
  1192. pr_debug("invalid DMA class\n");
  1193. return -EIO;
  1194. }
  1195. return 0;
  1196. }
  1197. static int spu_send_mfc_command(struct spu_context *ctx,
  1198. struct mfc_dma_command cmd,
  1199. int *error)
  1200. {
  1201. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  1202. if (*error == -EAGAIN) {
  1203. /* wait for any tag group to complete
  1204. so we have space for the new command */
  1205. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
  1206. /* try again, because the queue might be
  1207. empty again */
  1208. *error = ctx->ops->send_mfc_command(ctx, &cmd);
  1209. if (*error == -EAGAIN)
  1210. return 0;
  1211. }
  1212. return 1;
  1213. }
  1214. static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
  1215. size_t size, loff_t *pos)
  1216. {
  1217. struct spu_context *ctx = file->private_data;
  1218. struct mfc_dma_command cmd;
  1219. int ret = -EINVAL;
  1220. if (size != sizeof cmd)
  1221. goto out;
  1222. ret = -EFAULT;
  1223. if (copy_from_user(&cmd, buffer, sizeof cmd))
  1224. goto out;
  1225. ret = spufs_check_valid_dma(&cmd);
  1226. if (ret)
  1227. goto out;
  1228. ret = spu_acquire_runnable(ctx, 0);
  1229. if (ret)
  1230. goto out;
  1231. if (file->f_flags & O_NONBLOCK) {
  1232. ret = ctx->ops->send_mfc_command(ctx, &cmd);
  1233. } else {
  1234. int status;
  1235. ret = spufs_wait(ctx->mfc_wq,
  1236. spu_send_mfc_command(ctx, cmd, &status));
  1237. if (status)
  1238. ret = status;
  1239. }
  1240. if (ret)
  1241. goto out_unlock;
  1242. ctx->tagwait |= 1 << cmd.tag;
  1243. ret = size;
  1244. out_unlock:
  1245. spu_release(ctx);
  1246. out:
  1247. return ret;
  1248. }
  1249. static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
  1250. {
  1251. struct spu_context *ctx = file->private_data;
  1252. u32 free_elements, tagstatus;
  1253. unsigned int mask;
  1254. poll_wait(file, &ctx->mfc_wq, wait);
  1255. spu_acquire(ctx);
  1256. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
  1257. free_elements = ctx->ops->get_mfc_free_elements(ctx);
  1258. tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
  1259. spu_release(ctx);
  1260. mask = 0;
  1261. if (free_elements & 0xffff)
  1262. mask |= POLLOUT | POLLWRNORM;
  1263. if (tagstatus & ctx->tagwait)
  1264. mask |= POLLIN | POLLRDNORM;
  1265. pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
  1266. free_elements, tagstatus, ctx->tagwait);
  1267. return mask;
  1268. }
  1269. static int spufs_mfc_flush(struct file *file, fl_owner_t id)
  1270. {
  1271. struct spu_context *ctx = file->private_data;
  1272. int ret;
  1273. spu_acquire(ctx);
  1274. #if 0
  1275. /* this currently hangs */
  1276. ret = spufs_wait(ctx->mfc_wq,
  1277. ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
  1278. if (ret)
  1279. goto out;
  1280. ret = spufs_wait(ctx->mfc_wq,
  1281. ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
  1282. out:
  1283. #else
  1284. ret = 0;
  1285. #endif
  1286. spu_release(ctx);
  1287. return ret;
  1288. }
  1289. static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
  1290. int datasync)
  1291. {
  1292. return spufs_mfc_flush(file, NULL);
  1293. }
  1294. static int spufs_mfc_fasync(int fd, struct file *file, int on)
  1295. {
  1296. struct spu_context *ctx = file->private_data;
  1297. return fasync_helper(fd, file, on, &ctx->mfc_fasync);
  1298. }
  1299. static const struct file_operations spufs_mfc_fops = {
  1300. .open = spufs_mfc_open,
  1301. .release = spufs_mfc_release,
  1302. .read = spufs_mfc_read,
  1303. .write = spufs_mfc_write,
  1304. .poll = spufs_mfc_poll,
  1305. .flush = spufs_mfc_flush,
  1306. .fsync = spufs_mfc_fsync,
  1307. .fasync = spufs_mfc_fasync,
  1308. .mmap = spufs_mfc_mmap,
  1309. };
  1310. static void spufs_npc_set(void *data, u64 val)
  1311. {
  1312. struct spu_context *ctx = data;
  1313. spu_acquire(ctx);
  1314. ctx->ops->npc_write(ctx, val);
  1315. spu_release(ctx);
  1316. }
  1317. static u64 spufs_npc_get(void *data)
  1318. {
  1319. struct spu_context *ctx = data;
  1320. u64 ret;
  1321. spu_acquire(ctx);
  1322. ret = ctx->ops->npc_read(ctx);
  1323. spu_release(ctx);
  1324. return ret;
  1325. }
  1326. DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
  1327. "0x%llx\n")
  1328. static void spufs_decr_set(void *data, u64 val)
  1329. {
  1330. struct spu_context *ctx = data;
  1331. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1332. spu_acquire_saved(ctx);
  1333. lscsa->decr.slot[0] = (u32) val;
  1334. spu_release_saved(ctx);
  1335. }
  1336. static u64 __spufs_decr_get(void *data)
  1337. {
  1338. struct spu_context *ctx = data;
  1339. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1340. return lscsa->decr.slot[0];
  1341. }
  1342. static u64 spufs_decr_get(void *data)
  1343. {
  1344. struct spu_context *ctx = data;
  1345. u64 ret;
  1346. spu_acquire_saved(ctx);
  1347. ret = __spufs_decr_get(data);
  1348. spu_release_saved(ctx);
  1349. return ret;
  1350. }
  1351. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  1352. "0x%llx\n")
  1353. static void spufs_decr_status_set(void *data, u64 val)
  1354. {
  1355. struct spu_context *ctx = data;
  1356. spu_acquire_saved(ctx);
  1357. if (val)
  1358. ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
  1359. else
  1360. ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
  1361. spu_release_saved(ctx);
  1362. }
  1363. static u64 __spufs_decr_status_get(void *data)
  1364. {
  1365. struct spu_context *ctx = data;
  1366. if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
  1367. return SPU_DECR_STATUS_RUNNING;
  1368. else
  1369. return 0;
  1370. }
  1371. static u64 spufs_decr_status_get(void *data)
  1372. {
  1373. struct spu_context *ctx = data;
  1374. u64 ret;
  1375. spu_acquire_saved(ctx);
  1376. ret = __spufs_decr_status_get(data);
  1377. spu_release_saved(ctx);
  1378. return ret;
  1379. }
  1380. DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  1381. spufs_decr_status_set, "0x%llx\n")
  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(void *data)
  1391. {
  1392. struct spu_context *ctx = data;
  1393. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1394. return lscsa->event_mask.slot[0];
  1395. }
  1396. static u64 spufs_event_mask_get(void *data)
  1397. {
  1398. struct spu_context *ctx = data;
  1399. u64 ret;
  1400. spu_acquire_saved(ctx);
  1401. ret = __spufs_event_mask_get(data);
  1402. spu_release_saved(ctx);
  1403. return ret;
  1404. }
  1405. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1406. spufs_event_mask_set, "0x%llx\n")
  1407. static u64 __spufs_event_status_get(void *data)
  1408. {
  1409. struct spu_context *ctx = data;
  1410. struct spu_state *state = &ctx->csa;
  1411. u64 stat;
  1412. stat = state->spu_chnlcnt_RW[0];
  1413. if (stat)
  1414. return state->spu_chnldata_RW[0];
  1415. return 0;
  1416. }
  1417. static u64 spufs_event_status_get(void *data)
  1418. {
  1419. struct spu_context *ctx = data;
  1420. u64 ret = 0;
  1421. spu_acquire_saved(ctx);
  1422. ret = __spufs_event_status_get(data);
  1423. spu_release_saved(ctx);
  1424. return ret;
  1425. }
  1426. DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
  1427. NULL, "0x%llx\n")
  1428. static void spufs_srr0_set(void *data, u64 val)
  1429. {
  1430. struct spu_context *ctx = data;
  1431. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1432. spu_acquire_saved(ctx);
  1433. lscsa->srr0.slot[0] = (u32) val;
  1434. spu_release_saved(ctx);
  1435. }
  1436. static u64 spufs_srr0_get(void *data)
  1437. {
  1438. struct spu_context *ctx = data;
  1439. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1440. u64 ret;
  1441. spu_acquire_saved(ctx);
  1442. ret = lscsa->srr0.slot[0];
  1443. spu_release_saved(ctx);
  1444. return ret;
  1445. }
  1446. DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1447. "0x%llx\n")
  1448. static u64 spufs_id_get(void *data)
  1449. {
  1450. struct spu_context *ctx = data;
  1451. u64 num;
  1452. spu_acquire(ctx);
  1453. if (ctx->state == SPU_STATE_RUNNABLE)
  1454. num = ctx->spu->number;
  1455. else
  1456. num = (unsigned int)-1;
  1457. spu_release(ctx);
  1458. return num;
  1459. }
  1460. DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
  1461. static u64 __spufs_object_id_get(void *data)
  1462. {
  1463. struct spu_context *ctx = data;
  1464. return ctx->object_id;
  1465. }
  1466. static u64 spufs_object_id_get(void *data)
  1467. {
  1468. /* FIXME: Should there really be no locking here? */
  1469. return __spufs_object_id_get(data);
  1470. }
  1471. static void spufs_object_id_set(void *data, u64 id)
  1472. {
  1473. struct spu_context *ctx = data;
  1474. ctx->object_id = id;
  1475. }
  1476. DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
  1477. spufs_object_id_set, "0x%llx\n");
  1478. static u64 __spufs_lslr_get(void *data)
  1479. {
  1480. struct spu_context *ctx = data;
  1481. return ctx->csa.priv2.spu_lslr_RW;
  1482. }
  1483. static u64 spufs_lslr_get(void *data)
  1484. {
  1485. struct spu_context *ctx = data;
  1486. u64 ret;
  1487. spu_acquire_saved(ctx);
  1488. ret = __spufs_lslr_get(data);
  1489. spu_release_saved(ctx);
  1490. return ret;
  1491. }
  1492. DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
  1493. static int spufs_info_open(struct inode *inode, struct file *file)
  1494. {
  1495. struct spufs_inode_info *i = SPUFS_I(inode);
  1496. struct spu_context *ctx = i->i_ctx;
  1497. file->private_data = ctx;
  1498. return 0;
  1499. }
  1500. static int spufs_caps_show(struct seq_file *s, void *private)
  1501. {
  1502. struct spu_context *ctx = s->private;
  1503. if (!(ctx->flags & SPU_CREATE_NOSCHED))
  1504. seq_puts(s, "sched\n");
  1505. if (!(ctx->flags & SPU_CREATE_ISOLATE))
  1506. seq_puts(s, "step\n");
  1507. return 0;
  1508. }
  1509. static int spufs_caps_open(struct inode *inode, struct file *file)
  1510. {
  1511. return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
  1512. }
  1513. static const struct file_operations spufs_caps_fops = {
  1514. .open = spufs_caps_open,
  1515. .read = seq_read,
  1516. .llseek = seq_lseek,
  1517. .release = single_release,
  1518. };
  1519. static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
  1520. char __user *buf, size_t len, loff_t *pos)
  1521. {
  1522. u32 mbox_stat;
  1523. u32 data;
  1524. mbox_stat = ctx->csa.prob.mb_stat_R;
  1525. if (mbox_stat & 0x0000ff) {
  1526. data = ctx->csa.prob.pu_mb_R;
  1527. }
  1528. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1529. }
  1530. static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
  1531. size_t len, loff_t *pos)
  1532. {
  1533. int ret;
  1534. struct spu_context *ctx = file->private_data;
  1535. if (!access_ok(VERIFY_WRITE, buf, len))
  1536. return -EFAULT;
  1537. spu_acquire_saved(ctx);
  1538. spin_lock(&ctx->csa.register_lock);
  1539. ret = __spufs_mbox_info_read(ctx, buf, len, pos);
  1540. spin_unlock(&ctx->csa.register_lock);
  1541. spu_release_saved(ctx);
  1542. return ret;
  1543. }
  1544. static const struct file_operations spufs_mbox_info_fops = {
  1545. .open = spufs_info_open,
  1546. .read = spufs_mbox_info_read,
  1547. .llseek = generic_file_llseek,
  1548. };
  1549. static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
  1550. char __user *buf, size_t len, loff_t *pos)
  1551. {
  1552. u32 ibox_stat;
  1553. u32 data;
  1554. ibox_stat = ctx->csa.prob.mb_stat_R;
  1555. if (ibox_stat & 0xff0000) {
  1556. data = ctx->csa.priv2.puint_mb_R;
  1557. }
  1558. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1559. }
  1560. static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
  1561. size_t len, loff_t *pos)
  1562. {
  1563. struct spu_context *ctx = file->private_data;
  1564. int ret;
  1565. if (!access_ok(VERIFY_WRITE, buf, len))
  1566. return -EFAULT;
  1567. spu_acquire_saved(ctx);
  1568. spin_lock(&ctx->csa.register_lock);
  1569. ret = __spufs_ibox_info_read(ctx, buf, len, pos);
  1570. spin_unlock(&ctx->csa.register_lock);
  1571. spu_release_saved(ctx);
  1572. return ret;
  1573. }
  1574. static const struct file_operations spufs_ibox_info_fops = {
  1575. .open = spufs_info_open,
  1576. .read = spufs_ibox_info_read,
  1577. .llseek = generic_file_llseek,
  1578. };
  1579. static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
  1580. char __user *buf, size_t len, loff_t *pos)
  1581. {
  1582. int i, cnt;
  1583. u32 data[4];
  1584. u32 wbox_stat;
  1585. wbox_stat = ctx->csa.prob.mb_stat_R;
  1586. cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
  1587. for (i = 0; i < cnt; i++) {
  1588. data[i] = ctx->csa.spu_mailbox_data[i];
  1589. }
  1590. return simple_read_from_buffer(buf, len, pos, &data,
  1591. cnt * sizeof(u32));
  1592. }
  1593. static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
  1594. size_t len, loff_t *pos)
  1595. {
  1596. struct spu_context *ctx = file->private_data;
  1597. int ret;
  1598. if (!access_ok(VERIFY_WRITE, buf, len))
  1599. return -EFAULT;
  1600. spu_acquire_saved(ctx);
  1601. spin_lock(&ctx->csa.register_lock);
  1602. ret = __spufs_wbox_info_read(ctx, buf, len, pos);
  1603. spin_unlock(&ctx->csa.register_lock);
  1604. spu_release_saved(ctx);
  1605. return ret;
  1606. }
  1607. static const struct file_operations spufs_wbox_info_fops = {
  1608. .open = spufs_info_open,
  1609. .read = spufs_wbox_info_read,
  1610. .llseek = generic_file_llseek,
  1611. };
  1612. static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
  1613. char __user *buf, size_t len, loff_t *pos)
  1614. {
  1615. struct spu_dma_info info;
  1616. struct mfc_cq_sr *qp, *spuqp;
  1617. int i;
  1618. info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
  1619. info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
  1620. info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
  1621. info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
  1622. info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
  1623. for (i = 0; i < 16; i++) {
  1624. qp = &info.dma_info_command_data[i];
  1625. spuqp = &ctx->csa.priv2.spuq[i];
  1626. qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
  1627. qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
  1628. qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
  1629. qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
  1630. }
  1631. return simple_read_from_buffer(buf, len, pos, &info,
  1632. sizeof info);
  1633. }
  1634. static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
  1635. size_t len, loff_t *pos)
  1636. {
  1637. struct spu_context *ctx = file->private_data;
  1638. int ret;
  1639. if (!access_ok(VERIFY_WRITE, buf, len))
  1640. return -EFAULT;
  1641. spu_acquire_saved(ctx);
  1642. spin_lock(&ctx->csa.register_lock);
  1643. ret = __spufs_dma_info_read(ctx, buf, len, pos);
  1644. spin_unlock(&ctx->csa.register_lock);
  1645. spu_release_saved(ctx);
  1646. return ret;
  1647. }
  1648. static const struct file_operations spufs_dma_info_fops = {
  1649. .open = spufs_info_open,
  1650. .read = spufs_dma_info_read,
  1651. };
  1652. static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
  1653. char __user *buf, size_t len, loff_t *pos)
  1654. {
  1655. struct spu_proxydma_info info;
  1656. struct mfc_cq_sr *qp, *puqp;
  1657. int ret = sizeof info;
  1658. int i;
  1659. if (len < ret)
  1660. return -EINVAL;
  1661. if (!access_ok(VERIFY_WRITE, buf, len))
  1662. return -EFAULT;
  1663. info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
  1664. info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
  1665. info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
  1666. for (i = 0; i < 8; i++) {
  1667. qp = &info.proxydma_info_command_data[i];
  1668. puqp = &ctx->csa.priv2.puq[i];
  1669. qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
  1670. qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
  1671. qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
  1672. qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
  1673. }
  1674. return simple_read_from_buffer(buf, len, pos, &info,
  1675. sizeof info);
  1676. }
  1677. static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
  1678. size_t len, loff_t *pos)
  1679. {
  1680. struct spu_context *ctx = file->private_data;
  1681. int ret;
  1682. spu_acquire_saved(ctx);
  1683. spin_lock(&ctx->csa.register_lock);
  1684. ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
  1685. spin_unlock(&ctx->csa.register_lock);
  1686. spu_release_saved(ctx);
  1687. return ret;
  1688. }
  1689. static const struct file_operations spufs_proxydma_info_fops = {
  1690. .open = spufs_info_open,
  1691. .read = spufs_proxydma_info_read,
  1692. };
  1693. static int spufs_show_tid(struct seq_file *s, void *private)
  1694. {
  1695. struct spu_context *ctx = s->private;
  1696. seq_printf(s, "%d\n", ctx->tid);
  1697. return 0;
  1698. }
  1699. static int spufs_tid_open(struct inode *inode, struct file *file)
  1700. {
  1701. return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
  1702. }
  1703. static const struct file_operations spufs_tid_fops = {
  1704. .open = spufs_tid_open,
  1705. .read = seq_read,
  1706. .llseek = seq_lseek,
  1707. .release = single_release,
  1708. };
  1709. static const char *ctx_state_names[] = {
  1710. "user", "system", "iowait", "loaded"
  1711. };
  1712. static unsigned long long spufs_acct_time(struct spu_context *ctx,
  1713. enum spu_utilization_state state)
  1714. {
  1715. struct timespec ts;
  1716. unsigned long long time = ctx->stats.times[state];
  1717. /*
  1718. * In general, utilization statistics are updated by the controlling
  1719. * thread as the spu context moves through various well defined
  1720. * state transitions, but if the context is lazily loaded its
  1721. * utilization statistics are not updated as the controlling thread
  1722. * is not tightly coupled with the execution of the spu context. We
  1723. * calculate and apply the time delta from the last recorded state
  1724. * of the spu context.
  1725. */
  1726. if (ctx->spu && ctx->stats.util_state == state) {
  1727. ktime_get_ts(&ts);
  1728. time += timespec_to_ns(&ts) - ctx->stats.tstamp;
  1729. }
  1730. return time / NSEC_PER_MSEC;
  1731. }
  1732. static unsigned long long spufs_slb_flts(struct spu_context *ctx)
  1733. {
  1734. unsigned long long slb_flts = ctx->stats.slb_flt;
  1735. if (ctx->state == SPU_STATE_RUNNABLE) {
  1736. slb_flts += (ctx->spu->stats.slb_flt -
  1737. ctx->stats.slb_flt_base);
  1738. }
  1739. return slb_flts;
  1740. }
  1741. static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
  1742. {
  1743. unsigned long long class2_intrs = ctx->stats.class2_intr;
  1744. if (ctx->state == SPU_STATE_RUNNABLE) {
  1745. class2_intrs += (ctx->spu->stats.class2_intr -
  1746. ctx->stats.class2_intr_base);
  1747. }
  1748. return class2_intrs;
  1749. }
  1750. static int spufs_show_stat(struct seq_file *s, void *private)
  1751. {
  1752. struct spu_context *ctx = s->private;
  1753. spu_acquire(ctx);
  1754. seq_printf(s, "%s %llu %llu %llu %llu "
  1755. "%llu %llu %llu %llu %llu %llu %llu %llu\n",
  1756. ctx_state_names[ctx->stats.util_state],
  1757. spufs_acct_time(ctx, SPU_UTIL_USER),
  1758. spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
  1759. spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
  1760. spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
  1761. ctx->stats.vol_ctx_switch,
  1762. ctx->stats.invol_ctx_switch,
  1763. spufs_slb_flts(ctx),
  1764. ctx->stats.hash_flt,
  1765. ctx->stats.min_flt,
  1766. ctx->stats.maj_flt,
  1767. spufs_class2_intrs(ctx),
  1768. ctx->stats.libassist);
  1769. spu_release(ctx);
  1770. return 0;
  1771. }
  1772. static int spufs_stat_open(struct inode *inode, struct file *file)
  1773. {
  1774. return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
  1775. }
  1776. static const struct file_operations spufs_stat_fops = {
  1777. .open = spufs_stat_open,
  1778. .read = seq_read,
  1779. .llseek = seq_lseek,
  1780. .release = single_release,
  1781. };
  1782. struct tree_descr spufs_dir_contents[] = {
  1783. { "capabilities", &spufs_caps_fops, 0444, },
  1784. { "mem", &spufs_mem_fops, 0666, },
  1785. { "regs", &spufs_regs_fops, 0666, },
  1786. { "mbox", &spufs_mbox_fops, 0444, },
  1787. { "ibox", &spufs_ibox_fops, 0444, },
  1788. { "wbox", &spufs_wbox_fops, 0222, },
  1789. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1790. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1791. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1792. { "signal1", &spufs_signal1_nosched_fops, 0222, },
  1793. { "signal2", &spufs_signal2_nosched_fops, 0222, },
  1794. { "signal1_type", &spufs_signal1_type, 0666, },
  1795. { "signal2_type", &spufs_signal2_type, 0666, },
  1796. { "cntl", &spufs_cntl_fops, 0666, },
  1797. { "fpcr", &spufs_fpcr_fops, 0666, },
  1798. { "lslr", &spufs_lslr_ops, 0444, },
  1799. { "mfc", &spufs_mfc_fops, 0666, },
  1800. { "mss", &spufs_mss_fops, 0666, },
  1801. { "npc", &spufs_npc_ops, 0666, },
  1802. { "srr0", &spufs_srr0_ops, 0666, },
  1803. { "decr", &spufs_decr_ops, 0666, },
  1804. { "decr_status", &spufs_decr_status_ops, 0666, },
  1805. { "event_mask", &spufs_event_mask_ops, 0666, },
  1806. { "event_status", &spufs_event_status_ops, 0444, },
  1807. { "psmap", &spufs_psmap_fops, 0666, },
  1808. { "phys-id", &spufs_id_ops, 0666, },
  1809. { "object-id", &spufs_object_id_ops, 0666, },
  1810. { "mbox_info", &spufs_mbox_info_fops, 0444, },
  1811. { "ibox_info", &spufs_ibox_info_fops, 0444, },
  1812. { "wbox_info", &spufs_wbox_info_fops, 0444, },
  1813. { "dma_info", &spufs_dma_info_fops, 0444, },
  1814. { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
  1815. { "tid", &spufs_tid_fops, 0444, },
  1816. { "stat", &spufs_stat_fops, 0444, },
  1817. {},
  1818. };
  1819. struct tree_descr spufs_dir_nosched_contents[] = {
  1820. { "capabilities", &spufs_caps_fops, 0444, },
  1821. { "mem", &spufs_mem_fops, 0666, },
  1822. { "mbox", &spufs_mbox_fops, 0444, },
  1823. { "ibox", &spufs_ibox_fops, 0444, },
  1824. { "wbox", &spufs_wbox_fops, 0222, },
  1825. { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
  1826. { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
  1827. { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
  1828. { "signal1", &spufs_signal1_nosched_fops, 0222, },
  1829. { "signal2", &spufs_signal2_nosched_fops, 0222, },
  1830. { "signal1_type", &spufs_signal1_type, 0666, },
  1831. { "signal2_type", &spufs_signal2_type, 0666, },
  1832. { "mss", &spufs_mss_fops, 0666, },
  1833. { "mfc", &spufs_mfc_fops, 0666, },
  1834. { "cntl", &spufs_cntl_fops, 0666, },
  1835. { "npc", &spufs_npc_ops, 0666, },
  1836. { "psmap", &spufs_psmap_fops, 0666, },
  1837. { "phys-id", &spufs_id_ops, 0666, },
  1838. { "object-id", &spufs_object_id_ops, 0666, },
  1839. { "tid", &spufs_tid_fops, 0444, },
  1840. { "stat", &spufs_stat_fops, 0444, },
  1841. {},
  1842. };
  1843. struct spufs_coredump_reader spufs_coredump_read[] = {
  1844. { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
  1845. { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
  1846. { "lslr", NULL, __spufs_lslr_get, 19 },
  1847. { "decr", NULL, __spufs_decr_get, 19 },
  1848. { "decr_status", NULL, __spufs_decr_status_get, 19 },
  1849. { "mem", __spufs_mem_read, NULL, LS_SIZE, },
  1850. { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
  1851. { "signal1_type", NULL, __spufs_signal1_type_get, 19 },
  1852. { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
  1853. { "signal2_type", NULL, __spufs_signal2_type_get, 19 },
  1854. { "event_mask", NULL, __spufs_event_mask_get, 19 },
  1855. { "event_status", NULL, __spufs_event_status_get, 19 },
  1856. { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
  1857. { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
  1858. { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
  1859. { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
  1860. { "proxydma_info", __spufs_proxydma_info_read,
  1861. NULL, sizeof(struct spu_proxydma_info)},
  1862. { "object-id", NULL, __spufs_object_id_get, 19 },
  1863. { NULL },
  1864. };