file.c 64 KB

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