file.c 52 KB

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