file.c 64 KB

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