file.c 51 KB

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