file.c 57 KB

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