file.c 64 KB

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