file.c 64 KB

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