file.c 64 KB

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