file.c 63 KB

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