file.c 55 KB

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