file.c 64 KB

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