file.c 64 KB

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