file.c 57 KB

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