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