file.c 57 KB

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