file.c 64 KB

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