file.c 57 KB

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