file.c 64 KB

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