file.c 57 KB

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