file.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  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/export.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. .open = __fops ## _open, \
  130. .release = spufs_attr_release, \
  131. .read = spufs_attr_read, \
  132. .write = spufs_attr_write, \
  133. .llseek = generic_file_llseek, \
  134. };
  135. static int
  136. spufs_mem_open(struct inode *inode, struct file *file)
  137. {
  138. struct spufs_inode_info *i = SPUFS_I(inode);
  139. struct spu_context *ctx = i->i_ctx;
  140. mutex_lock(&ctx->mapping_lock);
  141. file->private_data = ctx;
  142. if (!i->i_openers++)
  143. ctx->local_store = inode->i_mapping;
  144. mutex_unlock(&ctx->mapping_lock);
  145. return 0;
  146. }
  147. static int
  148. spufs_mem_release(struct inode *inode, struct file *file)
  149. {
  150. struct spufs_inode_info *i = SPUFS_I(inode);
  151. struct spu_context *ctx = i->i_ctx;
  152. mutex_lock(&ctx->mapping_lock);
  153. if (!--i->i_openers)
  154. ctx->local_store = NULL;
  155. mutex_unlock(&ctx->mapping_lock);
  156. return 0;
  157. }
  158. static ssize_t
  159. __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
  160. size_t size, loff_t *pos)
  161. {
  162. char *local_store = ctx->ops->get_ls(ctx);
  163. return simple_read_from_buffer(buffer, size, pos, local_store,
  164. LS_SIZE);
  165. }
  166. static ssize_t
  167. spufs_mem_read(struct file *file, char __user *buffer,
  168. size_t size, loff_t *pos)
  169. {
  170. struct spu_context *ctx = file->private_data;
  171. ssize_t ret;
  172. ret = spu_acquire(ctx);
  173. if (ret)
  174. return ret;
  175. ret = __spufs_mem_read(ctx, buffer, size, pos);
  176. spu_release(ctx);
  177. return ret;
  178. }
  179. static ssize_t
  180. spufs_mem_write(struct file *file, const char __user *buffer,
  181. size_t size, loff_t *ppos)
  182. {
  183. struct spu_context *ctx = file->private_data;
  184. char *local_store;
  185. loff_t pos = *ppos;
  186. int ret;
  187. if (pos > LS_SIZE)
  188. return -EFBIG;
  189. ret = spu_acquire(ctx);
  190. if (ret)
  191. return ret;
  192. local_store = ctx->ops->get_ls(ctx);
  193. size = simple_write_to_buffer(local_store, LS_SIZE, ppos, buffer, size);
  194. spu_release(ctx);
  195. return size;
  196. }
  197. static int
  198. spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  199. {
  200. struct spu_context *ctx = vma->vm_file->private_data;
  201. unsigned long address = (unsigned long)vmf->virtual_address;
  202. unsigned long pfn, offset;
  203. #ifdef CONFIG_SPU_FS_64K_LS
  204. struct spu_state *csa = &ctx->csa;
  205. int psize;
  206. /* Check what page size we are using */
  207. psize = get_slice_psize(vma->vm_mm, address);
  208. /* Some sanity checking */
  209. BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
  210. /* Wow, 64K, cool, we need to align the address though */
  211. if (csa->use_big_pages) {
  212. BUG_ON(vma->vm_start & 0xffff);
  213. address &= ~0xfffful;
  214. }
  215. #endif /* CONFIG_SPU_FS_64K_LS */
  216. offset = vmf->pgoff << PAGE_SHIFT;
  217. if (offset >= LS_SIZE)
  218. return VM_FAULT_SIGBUS;
  219. pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
  220. address, offset);
  221. if (spu_acquire(ctx))
  222. return VM_FAULT_NOPAGE;
  223. if (ctx->state == SPU_STATE_SAVED) {
  224. vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
  225. pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
  226. } else {
  227. vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
  228. pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
  229. }
  230. vm_insert_pfn(vma, address, pfn);
  231. spu_release(ctx);
  232. return VM_FAULT_NOPAGE;
  233. }
  234. static int spufs_mem_mmap_access(struct vm_area_struct *vma,
  235. unsigned long address,
  236. void *buf, int len, int write)
  237. {
  238. struct spu_context *ctx = vma->vm_file->private_data;
  239. unsigned long offset = address - vma->vm_start;
  240. char *local_store;
  241. if (write && !(vma->vm_flags & VM_WRITE))
  242. return -EACCES;
  243. if (spu_acquire(ctx))
  244. return -EINTR;
  245. if ((offset + len) > vma->vm_end)
  246. len = vma->vm_end - offset;
  247. local_store = ctx->ops->get_ls(ctx);
  248. if (write)
  249. memcpy_toio(local_store + offset, buf, len);
  250. else
  251. memcpy_fromio(buf, local_store + offset, len);
  252. spu_release(ctx);
  253. return len;
  254. }
  255. static const struct vm_operations_struct spufs_mem_mmap_vmops = {
  256. .fault = spufs_mem_mmap_fault,
  257. .access = spufs_mem_mmap_access,
  258. };
  259. static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
  260. {
  261. #ifdef CONFIG_SPU_FS_64K_LS
  262. struct spu_context *ctx = file->private_data;
  263. struct spu_state *csa = &ctx->csa;
  264. /* Sanity check VMA alignment */
  265. if (csa->use_big_pages) {
  266. pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
  267. " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
  268. vma->vm_pgoff);
  269. if (vma->vm_start & 0xffff)
  270. return -EINVAL;
  271. if (vma->vm_pgoff & 0xf)
  272. return -EINVAL;
  273. }
  274. #endif /* CONFIG_SPU_FS_64K_LS */
  275. if (!(vma->vm_flags & VM_SHARED))
  276. return -EINVAL;
  277. vma->vm_flags |= VM_IO | VM_PFNMAP;
  278. vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
  279. vma->vm_ops = &spufs_mem_mmap_vmops;
  280. return 0;
  281. }
  282. #ifdef CONFIG_SPU_FS_64K_LS
  283. static unsigned long spufs_get_unmapped_area(struct file *file,
  284. unsigned long addr, unsigned long len, unsigned long pgoff,
  285. unsigned long flags)
  286. {
  287. struct spu_context *ctx = file->private_data;
  288. struct spu_state *csa = &ctx->csa;
  289. /* If not using big pages, fallback to normal MM g_u_a */
  290. if (!csa->use_big_pages)
  291. return current->mm->get_unmapped_area(file, addr, len,
  292. pgoff, flags);
  293. /* Else, try to obtain a 64K pages slice */
  294. return slice_get_unmapped_area(addr, len, flags,
  295. MMU_PAGE_64K, 1);
  296. }
  297. #endif /* CONFIG_SPU_FS_64K_LS */
  298. static const struct file_operations spufs_mem_fops = {
  299. .open = spufs_mem_open,
  300. .release = spufs_mem_release,
  301. .read = spufs_mem_read,
  302. .write = spufs_mem_write,
  303. .llseek = generic_file_llseek,
  304. .mmap = spufs_mem_mmap,
  305. #ifdef CONFIG_SPU_FS_64K_LS
  306. .get_unmapped_area = spufs_get_unmapped_area,
  307. #endif
  308. };
  309. static int spufs_ps_fault(struct vm_area_struct *vma,
  310. struct vm_fault *vmf,
  311. unsigned long ps_offs,
  312. unsigned long ps_size)
  313. {
  314. struct spu_context *ctx = vma->vm_file->private_data;
  315. unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
  316. int ret = 0;
  317. spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
  318. if (offset >= ps_size)
  319. return VM_FAULT_SIGBUS;
  320. if (fatal_signal_pending(current))
  321. return VM_FAULT_SIGBUS;
  322. /*
  323. * Because we release the mmap_sem, the context may be destroyed while
  324. * we're in spu_wait. Grab an extra reference so it isn't destroyed
  325. * in the meantime.
  326. */
  327. get_spu_context(ctx);
  328. /*
  329. * We have to wait for context to be loaded before we have
  330. * pages to hand out to the user, but we don't want to wait
  331. * with the mmap_sem held.
  332. * It is possible to drop the mmap_sem here, but then we need
  333. * to return VM_FAULT_NOPAGE because the mappings may have
  334. * hanged.
  335. */
  336. if (spu_acquire(ctx))
  337. goto refault;
  338. if (ctx->state == SPU_STATE_SAVED) {
  339. up_read(&current->mm->mmap_sem);
  340. spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
  341. ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
  342. spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
  343. down_read(&current->mm->mmap_sem);
  344. } else {
  345. area = ctx->spu->problem_phys + ps_offs;
  346. vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
  347. (area + offset) >> PAGE_SHIFT);
  348. spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
  349. }
  350. if (!ret)
  351. spu_release(ctx);
  352. refault:
  353. put_spu_context(ctx);
  354. return VM_FAULT_NOPAGE;
  355. }
  356. #if SPUFS_MMAP_4K
  357. static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
  358. struct vm_fault *vmf)
  359. {
  360. return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
  361. }
  362. static const struct vm_operations_struct spufs_cntl_mmap_vmops = {
  363. .fault = spufs_cntl_mmap_fault,
  364. };
  365. /*
  366. * mmap support for problem state control area [0x4000 - 0x4fff].
  367. */
  368. static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
  369. {
  370. if (!(vma->vm_flags & VM_SHARED))
  371. return -EINVAL;
  372. vma->vm_flags |= VM_IO | VM_PFNMAP;
  373. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  374. vma->vm_ops = &spufs_cntl_mmap_vmops;
  375. return 0;
  376. }
  377. #else /* SPUFS_MMAP_4K */
  378. #define spufs_cntl_mmap NULL
  379. #endif /* !SPUFS_MMAP_4K */
  380. static int spufs_cntl_get(void *data, u64 *val)
  381. {
  382. struct spu_context *ctx = data;
  383. int ret;
  384. ret = spu_acquire(ctx);
  385. if (ret)
  386. return ret;
  387. *val = ctx->ops->status_read(ctx);
  388. spu_release(ctx);
  389. return 0;
  390. }
  391. static int spufs_cntl_set(void *data, u64 val)
  392. {
  393. struct spu_context *ctx = data;
  394. int ret;
  395. ret = spu_acquire(ctx);
  396. if (ret)
  397. return ret;
  398. ctx->ops->runcntl_write(ctx, val);
  399. spu_release(ctx);
  400. return 0;
  401. }
  402. static int spufs_cntl_open(struct inode *inode, struct file *file)
  403. {
  404. struct spufs_inode_info *i = SPUFS_I(inode);
  405. struct spu_context *ctx = i->i_ctx;
  406. mutex_lock(&ctx->mapping_lock);
  407. file->private_data = ctx;
  408. if (!i->i_openers++)
  409. ctx->cntl = inode->i_mapping;
  410. mutex_unlock(&ctx->mapping_lock);
  411. return simple_attr_open(inode, file, spufs_cntl_get,
  412. spufs_cntl_set, "0x%08lx");
  413. }
  414. static int
  415. spufs_cntl_release(struct inode *inode, struct file *file)
  416. {
  417. struct spufs_inode_info *i = SPUFS_I(inode);
  418. struct spu_context *ctx = i->i_ctx;
  419. simple_attr_release(inode, file);
  420. mutex_lock(&ctx->mapping_lock);
  421. if (!--i->i_openers)
  422. ctx->cntl = NULL;
  423. mutex_unlock(&ctx->mapping_lock);
  424. return 0;
  425. }
  426. static const struct file_operations spufs_cntl_fops = {
  427. .open = spufs_cntl_open,
  428. .release = spufs_cntl_release,
  429. .read = simple_attr_read,
  430. .write = simple_attr_write,
  431. .llseek = generic_file_llseek,
  432. .mmap = spufs_cntl_mmap,
  433. };
  434. static int
  435. spufs_regs_open(struct inode *inode, struct file *file)
  436. {
  437. struct spufs_inode_info *i = SPUFS_I(inode);
  438. file->private_data = i->i_ctx;
  439. return 0;
  440. }
  441. static ssize_t
  442. __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
  443. size_t size, loff_t *pos)
  444. {
  445. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  446. return simple_read_from_buffer(buffer, size, pos,
  447. lscsa->gprs, sizeof lscsa->gprs);
  448. }
  449. static ssize_t
  450. spufs_regs_read(struct file *file, char __user *buffer,
  451. size_t size, loff_t *pos)
  452. {
  453. int ret;
  454. struct spu_context *ctx = file->private_data;
  455. /* pre-check for file position: if we'd return EOF, there's no point
  456. * causing a deschedule */
  457. if (*pos >= sizeof(ctx->csa.lscsa->gprs))
  458. return 0;
  459. ret = spu_acquire_saved(ctx);
  460. if (ret)
  461. return ret;
  462. ret = __spufs_regs_read(ctx, buffer, size, pos);
  463. spu_release_saved(ctx);
  464. return ret;
  465. }
  466. static ssize_t
  467. spufs_regs_write(struct file *file, const char __user *buffer,
  468. size_t size, loff_t *pos)
  469. {
  470. struct spu_context *ctx = file->private_data;
  471. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  472. int ret;
  473. if (*pos >= sizeof(lscsa->gprs))
  474. return -EFBIG;
  475. ret = spu_acquire_saved(ctx);
  476. if (ret)
  477. return ret;
  478. size = simple_write_to_buffer(lscsa->gprs, sizeof(lscsa->gprs), pos,
  479. buffer, size);
  480. spu_release_saved(ctx);
  481. return size;
  482. }
  483. static const struct file_operations spufs_regs_fops = {
  484. .open = spufs_regs_open,
  485. .read = spufs_regs_read,
  486. .write = spufs_regs_write,
  487. .llseek = generic_file_llseek,
  488. };
  489. static ssize_t
  490. __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
  491. size_t size, loff_t * pos)
  492. {
  493. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  494. return simple_read_from_buffer(buffer, size, pos,
  495. &lscsa->fpcr, sizeof(lscsa->fpcr));
  496. }
  497. static ssize_t
  498. spufs_fpcr_read(struct file *file, char __user * buffer,
  499. size_t size, loff_t * pos)
  500. {
  501. int ret;
  502. struct spu_context *ctx = file->private_data;
  503. ret = spu_acquire_saved(ctx);
  504. if (ret)
  505. return ret;
  506. ret = __spufs_fpcr_read(ctx, buffer, size, pos);
  507. spu_release_saved(ctx);
  508. return ret;
  509. }
  510. static ssize_t
  511. spufs_fpcr_write(struct file *file, const char __user * buffer,
  512. size_t size, loff_t * pos)
  513. {
  514. struct spu_context *ctx = file->private_data;
  515. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  516. int ret;
  517. if (*pos >= sizeof(lscsa->fpcr))
  518. return -EFBIG;
  519. ret = spu_acquire_saved(ctx);
  520. if (ret)
  521. return ret;
  522. size = simple_write_to_buffer(&lscsa->fpcr, sizeof(lscsa->fpcr), pos,
  523. buffer, size);
  524. spu_release_saved(ctx);
  525. return size;
  526. }
  527. static const struct file_operations spufs_fpcr_fops = {
  528. .open = spufs_regs_open,
  529. .read = spufs_fpcr_read,
  530. .write = spufs_fpcr_write,
  531. .llseek = generic_file_llseek,
  532. };
  533. /* generic open function for all pipe-like files */
  534. static int spufs_pipe_open(struct inode *inode, struct file *file)
  535. {
  536. struct spufs_inode_info *i = SPUFS_I(inode);
  537. file->private_data = i->i_ctx;
  538. return nonseekable_open(inode, file);
  539. }
  540. /*
  541. * Read as many bytes from the mailbox as possible, until
  542. * one of the conditions becomes true:
  543. *
  544. * - no more data available in the mailbox
  545. * - end of the user provided buffer
  546. * - end of the mapped area
  547. */
  548. static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
  549. size_t len, loff_t *pos)
  550. {
  551. struct spu_context *ctx = file->private_data;
  552. u32 mbox_data, __user *udata;
  553. ssize_t count;
  554. if (len < 4)
  555. return -EINVAL;
  556. if (!access_ok(VERIFY_WRITE, buf, len))
  557. return -EFAULT;
  558. udata = (void __user *)buf;
  559. count = spu_acquire(ctx);
  560. if (count)
  561. return count;
  562. for (count = 0; (count + 4) <= len; count += 4, udata++) {
  563. int ret;
  564. ret = ctx->ops->mbox_read(ctx, &mbox_data);
  565. if (ret == 0)
  566. break;
  567. /*
  568. * at the end of the mapped area, we can fault
  569. * but still need to return the data we have
  570. * read successfully so far.
  571. */
  572. ret = __put_user(mbox_data, udata);
  573. if (ret) {
  574. if (!count)
  575. count = -EFAULT;
  576. break;
  577. }
  578. }
  579. spu_release(ctx);
  580. if (!count)
  581. count = -EAGAIN;
  582. return count;
  583. }
  584. static const struct file_operations spufs_mbox_fops = {
  585. .open = spufs_pipe_open,
  586. .read = spufs_mbox_read,
  587. .llseek = no_llseek,
  588. };
  589. static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
  590. size_t len, loff_t *pos)
  591. {
  592. struct spu_context *ctx = file->private_data;
  593. ssize_t ret;
  594. u32 mbox_stat;
  595. if (len < 4)
  596. return -EINVAL;
  597. ret = spu_acquire(ctx);
  598. if (ret)
  599. return ret;
  600. mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
  601. spu_release(ctx);
  602. if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
  603. return -EFAULT;
  604. return 4;
  605. }
  606. static const struct file_operations spufs_mbox_stat_fops = {
  607. .open = spufs_pipe_open,
  608. .read = spufs_mbox_stat_read,
  609. .llseek = no_llseek,
  610. };
  611. /* low-level ibox access function */
  612. size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
  613. {
  614. return ctx->ops->ibox_read(ctx, data);
  615. }
  616. static int spufs_ibox_fasync(int fd, struct file *file, int on)
  617. {
  618. struct spu_context *ctx = file->private_data;
  619. return fasync_helper(fd, file, on, &ctx->ibox_fasync);
  620. }
  621. /* interrupt-level ibox callback function. */
  622. void spufs_ibox_callback(struct spu *spu)
  623. {
  624. struct spu_context *ctx = spu->ctx;
  625. if (!ctx)
  626. return;
  627. wake_up_all(&ctx->ibox_wq);
  628. kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
  629. }
  630. /*
  631. * Read as many bytes from the interrupt mailbox as possible, until
  632. * one of the conditions becomes true:
  633. *
  634. * - no more data available in the mailbox
  635. * - end of the user provided buffer
  636. * - end of the mapped area
  637. *
  638. * If the file is opened without O_NONBLOCK, we wait here until
  639. * any data is available, but return when we have been able to
  640. * read something.
  641. */
  642. static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
  643. size_t len, loff_t *pos)
  644. {
  645. struct spu_context *ctx = file->private_data;
  646. u32 ibox_data, __user *udata;
  647. ssize_t count;
  648. if (len < 4)
  649. return -EINVAL;
  650. if (!access_ok(VERIFY_WRITE, buf, len))
  651. return -EFAULT;
  652. udata = (void __user *)buf;
  653. count = spu_acquire(ctx);
  654. if (count)
  655. goto out;
  656. /* wait only for the first element */
  657. count = 0;
  658. if (file->f_flags & O_NONBLOCK) {
  659. if (!spu_ibox_read(ctx, &ibox_data)) {
  660. count = -EAGAIN;
  661. goto out_unlock;
  662. }
  663. } else {
  664. count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
  665. if (count)
  666. goto out;
  667. }
  668. /* if we can't write at all, return -EFAULT */
  669. count = __put_user(ibox_data, udata);
  670. if (count)
  671. goto out_unlock;
  672. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  673. int ret;
  674. ret = ctx->ops->ibox_read(ctx, &ibox_data);
  675. if (ret == 0)
  676. break;
  677. /*
  678. * at the end of the mapped area, we can fault
  679. * but still need to return the data we have
  680. * read successfully so far.
  681. */
  682. ret = __put_user(ibox_data, udata);
  683. if (ret)
  684. break;
  685. }
  686. out_unlock:
  687. spu_release(ctx);
  688. out:
  689. return count;
  690. }
  691. static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
  692. {
  693. struct spu_context *ctx = file->private_data;
  694. unsigned int mask;
  695. poll_wait(file, &ctx->ibox_wq, wait);
  696. /*
  697. * For now keep this uninterruptible and also ignore the rule
  698. * that poll should not sleep. Will be fixed later.
  699. */
  700. mutex_lock(&ctx->state_mutex);
  701. mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
  702. spu_release(ctx);
  703. return mask;
  704. }
  705. static const struct file_operations spufs_ibox_fops = {
  706. .open = spufs_pipe_open,
  707. .read = spufs_ibox_read,
  708. .poll = spufs_ibox_poll,
  709. .fasync = spufs_ibox_fasync,
  710. .llseek = no_llseek,
  711. };
  712. static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
  713. size_t len, loff_t *pos)
  714. {
  715. struct spu_context *ctx = file->private_data;
  716. ssize_t ret;
  717. u32 ibox_stat;
  718. if (len < 4)
  719. return -EINVAL;
  720. ret = spu_acquire(ctx);
  721. if (ret)
  722. return ret;
  723. ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
  724. spu_release(ctx);
  725. if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
  726. return -EFAULT;
  727. return 4;
  728. }
  729. static const struct file_operations spufs_ibox_stat_fops = {
  730. .open = spufs_pipe_open,
  731. .read = spufs_ibox_stat_read,
  732. .llseek = no_llseek,
  733. };
  734. /* low-level mailbox write */
  735. size_t spu_wbox_write(struct spu_context *ctx, u32 data)
  736. {
  737. return ctx->ops->wbox_write(ctx, data);
  738. }
  739. static int spufs_wbox_fasync(int fd, struct file *file, int on)
  740. {
  741. struct spu_context *ctx = file->private_data;
  742. int ret;
  743. ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
  744. return ret;
  745. }
  746. /* interrupt-level wbox callback function. */
  747. void spufs_wbox_callback(struct spu *spu)
  748. {
  749. struct spu_context *ctx = spu->ctx;
  750. if (!ctx)
  751. return;
  752. wake_up_all(&ctx->wbox_wq);
  753. kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
  754. }
  755. /*
  756. * Write as many bytes to the interrupt mailbox as possible, until
  757. * one of the conditions becomes true:
  758. *
  759. * - the mailbox is full
  760. * - end of the user provided buffer
  761. * - end of the mapped area
  762. *
  763. * If the file is opened without O_NONBLOCK, we wait here until
  764. * space is availabyl, but return when we have been able to
  765. * write something.
  766. */
  767. static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
  768. size_t len, loff_t *pos)
  769. {
  770. struct spu_context *ctx = file->private_data;
  771. u32 wbox_data, __user *udata;
  772. ssize_t count;
  773. if (len < 4)
  774. return -EINVAL;
  775. udata = (void __user *)buf;
  776. if (!access_ok(VERIFY_READ, buf, len))
  777. return -EFAULT;
  778. if (__get_user(wbox_data, udata))
  779. return -EFAULT;
  780. count = spu_acquire(ctx);
  781. if (count)
  782. goto out;
  783. /*
  784. * make sure we can at least write one element, by waiting
  785. * in case of !O_NONBLOCK
  786. */
  787. count = 0;
  788. if (file->f_flags & O_NONBLOCK) {
  789. if (!spu_wbox_write(ctx, wbox_data)) {
  790. count = -EAGAIN;
  791. goto out_unlock;
  792. }
  793. } else {
  794. count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
  795. if (count)
  796. goto out;
  797. }
  798. /* write as much as possible */
  799. for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
  800. int ret;
  801. ret = __get_user(wbox_data, udata);
  802. if (ret)
  803. break;
  804. ret = spu_wbox_write(ctx, wbox_data);
  805. if (ret == 0)
  806. break;
  807. }
  808. out_unlock:
  809. spu_release(ctx);
  810. out:
  811. return count;
  812. }
  813. static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
  814. {
  815. struct spu_context *ctx = file->private_data;
  816. unsigned int mask;
  817. poll_wait(file, &ctx->wbox_wq, wait);
  818. /*
  819. * For now keep this uninterruptible and also ignore the rule
  820. * that poll should not sleep. Will be fixed later.
  821. */
  822. mutex_lock(&ctx->state_mutex);
  823. mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
  824. spu_release(ctx);
  825. return mask;
  826. }
  827. static const struct file_operations spufs_wbox_fops = {
  828. .open = spufs_pipe_open,
  829. .write = spufs_wbox_write,
  830. .poll = spufs_wbox_poll,
  831. .fasync = spufs_wbox_fasync,
  832. .llseek = no_llseek,
  833. };
  834. static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
  835. size_t len, loff_t *pos)
  836. {
  837. struct spu_context *ctx = file->private_data;
  838. ssize_t ret;
  839. u32 wbox_stat;
  840. if (len < 4)
  841. return -EINVAL;
  842. ret = spu_acquire(ctx);
  843. if (ret)
  844. return ret;
  845. wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
  846. spu_release(ctx);
  847. if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
  848. return -EFAULT;
  849. return 4;
  850. }
  851. static const struct file_operations spufs_wbox_stat_fops = {
  852. .open = spufs_pipe_open,
  853. .read = spufs_wbox_stat_read,
  854. .llseek = no_llseek,
  855. };
  856. static int spufs_signal1_open(struct inode *inode, struct file *file)
  857. {
  858. struct spufs_inode_info *i = SPUFS_I(inode);
  859. struct spu_context *ctx = i->i_ctx;
  860. mutex_lock(&ctx->mapping_lock);
  861. file->private_data = ctx;
  862. if (!i->i_openers++)
  863. ctx->signal1 = inode->i_mapping;
  864. mutex_unlock(&ctx->mapping_lock);
  865. return nonseekable_open(inode, file);
  866. }
  867. static int
  868. spufs_signal1_release(struct inode *inode, struct file *file)
  869. {
  870. struct spufs_inode_info *i = SPUFS_I(inode);
  871. struct spu_context *ctx = i->i_ctx;
  872. mutex_lock(&ctx->mapping_lock);
  873. if (!--i->i_openers)
  874. ctx->signal1 = NULL;
  875. mutex_unlock(&ctx->mapping_lock);
  876. return 0;
  877. }
  878. static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
  879. size_t len, loff_t *pos)
  880. {
  881. int ret = 0;
  882. u32 data;
  883. if (len < 4)
  884. return -EINVAL;
  885. if (ctx->csa.spu_chnlcnt_RW[3]) {
  886. data = ctx->csa.spu_chnldata_RW[3];
  887. ret = 4;
  888. }
  889. if (!ret)
  890. goto out;
  891. if (copy_to_user(buf, &data, 4))
  892. return -EFAULT;
  893. out:
  894. return ret;
  895. }
  896. static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
  897. size_t len, loff_t *pos)
  898. {
  899. int ret;
  900. struct spu_context *ctx = file->private_data;
  901. ret = spu_acquire_saved(ctx);
  902. if (ret)
  903. return ret;
  904. ret = __spufs_signal1_read(ctx, buf, len, pos);
  905. spu_release_saved(ctx);
  906. return ret;
  907. }
  908. static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
  909. size_t len, loff_t *pos)
  910. {
  911. struct spu_context *ctx;
  912. ssize_t ret;
  913. u32 data;
  914. ctx = file->private_data;
  915. if (len < 4)
  916. return -EINVAL;
  917. if (copy_from_user(&data, buf, 4))
  918. return -EFAULT;
  919. ret = spu_acquire(ctx);
  920. if (ret)
  921. return ret;
  922. ctx->ops->signal1_write(ctx, data);
  923. spu_release(ctx);
  924. return 4;
  925. }
  926. static int
  927. spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  928. {
  929. #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
  930. return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
  931. #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
  932. /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
  933. * signal 1 and 2 area
  934. */
  935. return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
  936. #else
  937. #error unsupported page size
  938. #endif
  939. }
  940. static const struct vm_operations_struct spufs_signal1_mmap_vmops = {
  941. .fault = spufs_signal1_mmap_fault,
  942. };
  943. static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
  944. {
  945. if (!(vma->vm_flags & VM_SHARED))
  946. return -EINVAL;
  947. vma->vm_flags |= VM_IO | VM_PFNMAP;
  948. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  949. vma->vm_ops = &spufs_signal1_mmap_vmops;
  950. return 0;
  951. }
  952. static const struct file_operations spufs_signal1_fops = {
  953. .open = spufs_signal1_open,
  954. .release = spufs_signal1_release,
  955. .read = spufs_signal1_read,
  956. .write = spufs_signal1_write,
  957. .mmap = spufs_signal1_mmap,
  958. .llseek = no_llseek,
  959. };
  960. static const struct file_operations spufs_signal1_nosched_fops = {
  961. .open = spufs_signal1_open,
  962. .release = spufs_signal1_release,
  963. .write = spufs_signal1_write,
  964. .mmap = spufs_signal1_mmap,
  965. .llseek = no_llseek,
  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 const 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_noncached(vma->vm_page_prot);
  1061. vma->vm_ops = &spufs_signal2_mmap_vmops;
  1062. return 0;
  1063. }
  1064. #else /* SPUFS_MMAP_4K */
  1065. #define spufs_signal2_mmap NULL
  1066. #endif /* !SPUFS_MMAP_4K */
  1067. static const struct file_operations spufs_signal2_fops = {
  1068. .open = spufs_signal2_open,
  1069. .release = spufs_signal2_release,
  1070. .read = spufs_signal2_read,
  1071. .write = spufs_signal2_write,
  1072. .mmap = spufs_signal2_mmap,
  1073. .llseek = no_llseek,
  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. .llseek = no_llseek,
  1081. };
  1082. /*
  1083. * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
  1084. * work of acquiring (or not) the SPU context before calling through
  1085. * to the actual get routine. The set routine is called directly.
  1086. */
  1087. #define SPU_ATTR_NOACQUIRE 0
  1088. #define SPU_ATTR_ACQUIRE 1
  1089. #define SPU_ATTR_ACQUIRE_SAVED 2
  1090. #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
  1091. static int __##__get(void *data, u64 *val) \
  1092. { \
  1093. struct spu_context *ctx = data; \
  1094. int ret = 0; \
  1095. \
  1096. if (__acquire == SPU_ATTR_ACQUIRE) { \
  1097. ret = spu_acquire(ctx); \
  1098. if (ret) \
  1099. return ret; \
  1100. *val = __get(ctx); \
  1101. spu_release(ctx); \
  1102. } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
  1103. ret = spu_acquire_saved(ctx); \
  1104. if (ret) \
  1105. return ret; \
  1106. *val = __get(ctx); \
  1107. spu_release_saved(ctx); \
  1108. } else \
  1109. *val = __get(ctx); \
  1110. \
  1111. return 0; \
  1112. } \
  1113. DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
  1114. static int spufs_signal1_type_set(void *data, u64 val)
  1115. {
  1116. struct spu_context *ctx = data;
  1117. int ret;
  1118. ret = spu_acquire(ctx);
  1119. if (ret)
  1120. return ret;
  1121. ctx->ops->signal1_type_set(ctx, val);
  1122. spu_release(ctx);
  1123. return 0;
  1124. }
  1125. static u64 spufs_signal1_type_get(struct spu_context *ctx)
  1126. {
  1127. return ctx->ops->signal1_type_get(ctx);
  1128. }
  1129. DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
  1130. spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
  1131. static int spufs_signal2_type_set(void *data, u64 val)
  1132. {
  1133. struct spu_context *ctx = data;
  1134. int ret;
  1135. ret = spu_acquire(ctx);
  1136. if (ret)
  1137. return ret;
  1138. ctx->ops->signal2_type_set(ctx, val);
  1139. spu_release(ctx);
  1140. return 0;
  1141. }
  1142. static u64 spufs_signal2_type_get(struct spu_context *ctx)
  1143. {
  1144. return ctx->ops->signal2_type_get(ctx);
  1145. }
  1146. DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
  1147. spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
  1148. #if SPUFS_MMAP_4K
  1149. static int
  1150. spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1151. {
  1152. return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
  1153. }
  1154. static const struct vm_operations_struct spufs_mss_mmap_vmops = {
  1155. .fault = spufs_mss_mmap_fault,
  1156. };
  1157. /*
  1158. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  1159. */
  1160. static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
  1161. {
  1162. if (!(vma->vm_flags & VM_SHARED))
  1163. return -EINVAL;
  1164. vma->vm_flags |= VM_IO | VM_PFNMAP;
  1165. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  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. .llseek = no_llseek,
  1199. };
  1200. static int
  1201. spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1202. {
  1203. return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
  1204. }
  1205. static const struct vm_operations_struct spufs_psmap_mmap_vmops = {
  1206. .fault = spufs_psmap_mmap_fault,
  1207. };
  1208. /*
  1209. * mmap support for full problem state area [0x00000 - 0x1ffff].
  1210. */
  1211. static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
  1212. {
  1213. if (!(vma->vm_flags & VM_SHARED))
  1214. return -EINVAL;
  1215. vma->vm_flags |= VM_IO | VM_PFNMAP;
  1216. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  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. .llseek = no_llseek,
  1247. };
  1248. #if SPUFS_MMAP_4K
  1249. static int
  1250. spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1251. {
  1252. return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
  1253. }
  1254. static const struct vm_operations_struct spufs_mfc_mmap_vmops = {
  1255. .fault = spufs_mfc_mmap_fault,
  1256. };
  1257. /*
  1258. * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
  1259. */
  1260. static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  1261. {
  1262. if (!(vma->vm_flags & VM_SHARED))
  1263. return -EINVAL;
  1264. vma->vm_flags |= VM_IO | VM_PFNMAP;
  1265. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  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 %llx %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 %llx 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, loff_t start, loff_t end, int datasync)
  1534. {
  1535. struct inode *inode = file_inode(file);
  1536. int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1537. if (!err) {
  1538. mutex_lock(&inode->i_mutex);
  1539. err = spufs_mfc_flush(file, NULL);
  1540. mutex_unlock(&inode->i_mutex);
  1541. }
  1542. return err;
  1543. }
  1544. static int spufs_mfc_fasync(int fd, struct file *file, int on)
  1545. {
  1546. struct spu_context *ctx = file->private_data;
  1547. return fasync_helper(fd, file, on, &ctx->mfc_fasync);
  1548. }
  1549. static const struct file_operations spufs_mfc_fops = {
  1550. .open = spufs_mfc_open,
  1551. .release = spufs_mfc_release,
  1552. .read = spufs_mfc_read,
  1553. .write = spufs_mfc_write,
  1554. .poll = spufs_mfc_poll,
  1555. .flush = spufs_mfc_flush,
  1556. .fsync = spufs_mfc_fsync,
  1557. .fasync = spufs_mfc_fasync,
  1558. .mmap = spufs_mfc_mmap,
  1559. .llseek = no_llseek,
  1560. };
  1561. static int spufs_npc_set(void *data, u64 val)
  1562. {
  1563. struct spu_context *ctx = data;
  1564. int ret;
  1565. ret = spu_acquire(ctx);
  1566. if (ret)
  1567. return ret;
  1568. ctx->ops->npc_write(ctx, val);
  1569. spu_release(ctx);
  1570. return 0;
  1571. }
  1572. static u64 spufs_npc_get(struct spu_context *ctx)
  1573. {
  1574. return ctx->ops->npc_read(ctx);
  1575. }
  1576. DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
  1577. "0x%llx\n", SPU_ATTR_ACQUIRE);
  1578. static int spufs_decr_set(void *data, u64 val)
  1579. {
  1580. struct spu_context *ctx = data;
  1581. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1582. int ret;
  1583. ret = spu_acquire_saved(ctx);
  1584. if (ret)
  1585. return ret;
  1586. lscsa->decr.slot[0] = (u32) val;
  1587. spu_release_saved(ctx);
  1588. return 0;
  1589. }
  1590. static u64 spufs_decr_get(struct spu_context *ctx)
  1591. {
  1592. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1593. return lscsa->decr.slot[0];
  1594. }
  1595. DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
  1596. "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
  1597. static int spufs_decr_status_set(void *data, u64 val)
  1598. {
  1599. struct spu_context *ctx = data;
  1600. int ret;
  1601. ret = spu_acquire_saved(ctx);
  1602. if (ret)
  1603. return ret;
  1604. if (val)
  1605. ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
  1606. else
  1607. ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
  1608. spu_release_saved(ctx);
  1609. return 0;
  1610. }
  1611. static u64 spufs_decr_status_get(struct spu_context *ctx)
  1612. {
  1613. if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
  1614. return SPU_DECR_STATUS_RUNNING;
  1615. else
  1616. return 0;
  1617. }
  1618. DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
  1619. spufs_decr_status_set, "0x%llx\n",
  1620. SPU_ATTR_ACQUIRE_SAVED);
  1621. static int spufs_event_mask_set(void *data, u64 val)
  1622. {
  1623. struct spu_context *ctx = data;
  1624. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1625. int ret;
  1626. ret = spu_acquire_saved(ctx);
  1627. if (ret)
  1628. return ret;
  1629. lscsa->event_mask.slot[0] = (u32) val;
  1630. spu_release_saved(ctx);
  1631. return 0;
  1632. }
  1633. static u64 spufs_event_mask_get(struct spu_context *ctx)
  1634. {
  1635. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1636. return lscsa->event_mask.slot[0];
  1637. }
  1638. DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
  1639. spufs_event_mask_set, "0x%llx\n",
  1640. SPU_ATTR_ACQUIRE_SAVED);
  1641. static u64 spufs_event_status_get(struct spu_context *ctx)
  1642. {
  1643. struct spu_state *state = &ctx->csa;
  1644. u64 stat;
  1645. stat = state->spu_chnlcnt_RW[0];
  1646. if (stat)
  1647. return state->spu_chnldata_RW[0];
  1648. return 0;
  1649. }
  1650. DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
  1651. NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
  1652. static int spufs_srr0_set(void *data, u64 val)
  1653. {
  1654. struct spu_context *ctx = data;
  1655. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1656. int ret;
  1657. ret = spu_acquire_saved(ctx);
  1658. if (ret)
  1659. return ret;
  1660. lscsa->srr0.slot[0] = (u32) val;
  1661. spu_release_saved(ctx);
  1662. return 0;
  1663. }
  1664. static u64 spufs_srr0_get(struct spu_context *ctx)
  1665. {
  1666. struct spu_lscsa *lscsa = ctx->csa.lscsa;
  1667. return lscsa->srr0.slot[0];
  1668. }
  1669. DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
  1670. "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
  1671. static u64 spufs_id_get(struct spu_context *ctx)
  1672. {
  1673. u64 num;
  1674. if (ctx->state == SPU_STATE_RUNNABLE)
  1675. num = ctx->spu->number;
  1676. else
  1677. num = (unsigned int)-1;
  1678. return num;
  1679. }
  1680. DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
  1681. SPU_ATTR_ACQUIRE)
  1682. static u64 spufs_object_id_get(struct spu_context *ctx)
  1683. {
  1684. /* FIXME: Should there really be no locking here? */
  1685. return ctx->object_id;
  1686. }
  1687. static int spufs_object_id_set(void *data, u64 id)
  1688. {
  1689. struct spu_context *ctx = data;
  1690. ctx->object_id = id;
  1691. return 0;
  1692. }
  1693. DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
  1694. spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
  1695. static u64 spufs_lslr_get(struct spu_context *ctx)
  1696. {
  1697. return ctx->csa.priv2.spu_lslr_RW;
  1698. }
  1699. DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
  1700. SPU_ATTR_ACQUIRE_SAVED);
  1701. static int spufs_info_open(struct inode *inode, struct file *file)
  1702. {
  1703. struct spufs_inode_info *i = SPUFS_I(inode);
  1704. struct spu_context *ctx = i->i_ctx;
  1705. file->private_data = ctx;
  1706. return 0;
  1707. }
  1708. static int spufs_caps_show(struct seq_file *s, void *private)
  1709. {
  1710. struct spu_context *ctx = s->private;
  1711. if (!(ctx->flags & SPU_CREATE_NOSCHED))
  1712. seq_puts(s, "sched\n");
  1713. if (!(ctx->flags & SPU_CREATE_ISOLATE))
  1714. seq_puts(s, "step\n");
  1715. return 0;
  1716. }
  1717. static int spufs_caps_open(struct inode *inode, struct file *file)
  1718. {
  1719. return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
  1720. }
  1721. static const struct file_operations spufs_caps_fops = {
  1722. .open = spufs_caps_open,
  1723. .read = seq_read,
  1724. .llseek = seq_lseek,
  1725. .release = single_release,
  1726. };
  1727. static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
  1728. char __user *buf, size_t len, loff_t *pos)
  1729. {
  1730. u32 data;
  1731. /* EOF if there's no entry in the mbox */
  1732. if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
  1733. return 0;
  1734. data = ctx->csa.prob.pu_mb_R;
  1735. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1736. }
  1737. static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
  1738. size_t len, loff_t *pos)
  1739. {
  1740. int ret;
  1741. struct spu_context *ctx = file->private_data;
  1742. if (!access_ok(VERIFY_WRITE, buf, len))
  1743. return -EFAULT;
  1744. ret = spu_acquire_saved(ctx);
  1745. if (ret)
  1746. return ret;
  1747. spin_lock(&ctx->csa.register_lock);
  1748. ret = __spufs_mbox_info_read(ctx, buf, len, pos);
  1749. spin_unlock(&ctx->csa.register_lock);
  1750. spu_release_saved(ctx);
  1751. return ret;
  1752. }
  1753. static const struct file_operations spufs_mbox_info_fops = {
  1754. .open = spufs_info_open,
  1755. .read = spufs_mbox_info_read,
  1756. .llseek = generic_file_llseek,
  1757. };
  1758. static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
  1759. char __user *buf, size_t len, loff_t *pos)
  1760. {
  1761. u32 data;
  1762. /* EOF if there's no entry in the ibox */
  1763. if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
  1764. return 0;
  1765. data = ctx->csa.priv2.puint_mb_R;
  1766. return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
  1767. }
  1768. static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
  1769. size_t len, loff_t *pos)
  1770. {
  1771. struct spu_context *ctx = file->private_data;
  1772. int ret;
  1773. if (!access_ok(VERIFY_WRITE, buf, len))
  1774. return -EFAULT;
  1775. ret = spu_acquire_saved(ctx);
  1776. if (ret)
  1777. return ret;
  1778. spin_lock(&ctx->csa.register_lock);
  1779. ret = __spufs_ibox_info_read(ctx, buf, len, pos);
  1780. spin_unlock(&ctx->csa.register_lock);
  1781. spu_release_saved(ctx);
  1782. return ret;
  1783. }
  1784. static const struct file_operations spufs_ibox_info_fops = {
  1785. .open = spufs_info_open,
  1786. .read = spufs_ibox_info_read,
  1787. .llseek = generic_file_llseek,
  1788. };
  1789. static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
  1790. char __user *buf, size_t len, loff_t *pos)
  1791. {
  1792. int i, cnt;
  1793. u32 data[4];
  1794. u32 wbox_stat;
  1795. wbox_stat = ctx->csa.prob.mb_stat_R;
  1796. cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
  1797. for (i = 0; i < cnt; i++) {
  1798. data[i] = ctx->csa.spu_mailbox_data[i];
  1799. }
  1800. return simple_read_from_buffer(buf, len, pos, &data,
  1801. cnt * sizeof(u32));
  1802. }
  1803. static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
  1804. size_t len, loff_t *pos)
  1805. {
  1806. struct spu_context *ctx = file->private_data;
  1807. int ret;
  1808. if (!access_ok(VERIFY_WRITE, buf, len))
  1809. return -EFAULT;
  1810. ret = spu_acquire_saved(ctx);
  1811. if (ret)
  1812. return ret;
  1813. spin_lock(&ctx->csa.register_lock);
  1814. ret = __spufs_wbox_info_read(ctx, buf, len, pos);
  1815. spin_unlock(&ctx->csa.register_lock);
  1816. spu_release_saved(ctx);
  1817. return ret;
  1818. }
  1819. static const struct file_operations spufs_wbox_info_fops = {
  1820. .open = spufs_info_open,
  1821. .read = spufs_wbox_info_read,
  1822. .llseek = generic_file_llseek,
  1823. };
  1824. static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
  1825. char __user *buf, size_t len, loff_t *pos)
  1826. {
  1827. struct spu_dma_info info;
  1828. struct mfc_cq_sr *qp, *spuqp;
  1829. int i;
  1830. info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
  1831. info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
  1832. info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
  1833. info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
  1834. info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
  1835. for (i = 0; i < 16; i++) {
  1836. qp = &info.dma_info_command_data[i];
  1837. spuqp = &ctx->csa.priv2.spuq[i];
  1838. qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
  1839. qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
  1840. qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
  1841. qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
  1842. }
  1843. return simple_read_from_buffer(buf, len, pos, &info,
  1844. sizeof info);
  1845. }
  1846. static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
  1847. size_t len, loff_t *pos)
  1848. {
  1849. struct spu_context *ctx = file->private_data;
  1850. int ret;
  1851. if (!access_ok(VERIFY_WRITE, buf, len))
  1852. return -EFAULT;
  1853. ret = spu_acquire_saved(ctx);
  1854. if (ret)
  1855. return ret;
  1856. spin_lock(&ctx->csa.register_lock);
  1857. ret = __spufs_dma_info_read(ctx, buf, len, pos);
  1858. spin_unlock(&ctx->csa.register_lock);
  1859. spu_release_saved(ctx);
  1860. return ret;
  1861. }
  1862. static const struct file_operations spufs_dma_info_fops = {
  1863. .open = spufs_info_open,
  1864. .read = spufs_dma_info_read,
  1865. .llseek = no_llseek,
  1866. };
  1867. static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
  1868. char __user *buf, size_t len, loff_t *pos)
  1869. {
  1870. struct spu_proxydma_info info;
  1871. struct mfc_cq_sr *qp, *puqp;
  1872. int ret = sizeof info;
  1873. int i;
  1874. if (len < ret)
  1875. return -EINVAL;
  1876. if (!access_ok(VERIFY_WRITE, buf, len))
  1877. return -EFAULT;
  1878. info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
  1879. info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
  1880. info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
  1881. for (i = 0; i < 8; i++) {
  1882. qp = &info.proxydma_info_command_data[i];
  1883. puqp = &ctx->csa.priv2.puq[i];
  1884. qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
  1885. qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
  1886. qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
  1887. qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
  1888. }
  1889. return simple_read_from_buffer(buf, len, pos, &info,
  1890. sizeof info);
  1891. }
  1892. static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
  1893. size_t len, loff_t *pos)
  1894. {
  1895. struct spu_context *ctx = file->private_data;
  1896. int ret;
  1897. ret = spu_acquire_saved(ctx);
  1898. if (ret)
  1899. return ret;
  1900. spin_lock(&ctx->csa.register_lock);
  1901. ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
  1902. spin_unlock(&ctx->csa.register_lock);
  1903. spu_release_saved(ctx);
  1904. return ret;
  1905. }
  1906. static const struct file_operations spufs_proxydma_info_fops = {
  1907. .open = spufs_info_open,
  1908. .read = spufs_proxydma_info_read,
  1909. .llseek = no_llseek,
  1910. };
  1911. static int spufs_show_tid(struct seq_file *s, void *private)
  1912. {
  1913. struct spu_context *ctx = s->private;
  1914. seq_printf(s, "%d\n", ctx->tid);
  1915. return 0;
  1916. }
  1917. static int spufs_tid_open(struct inode *inode, struct file *file)
  1918. {
  1919. return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
  1920. }
  1921. static const struct file_operations spufs_tid_fops = {
  1922. .open = spufs_tid_open,
  1923. .read = seq_read,
  1924. .llseek = seq_lseek,
  1925. .release = single_release,
  1926. };
  1927. static const char *ctx_state_names[] = {
  1928. "user", "system", "iowait", "loaded"
  1929. };
  1930. static unsigned long long spufs_acct_time(struct spu_context *ctx,
  1931. enum spu_utilization_state state)
  1932. {
  1933. unsigned long long time = ctx->stats.times[state];
  1934. /*
  1935. * In general, utilization statistics are updated by the controlling
  1936. * thread as the spu context moves through various well defined
  1937. * state transitions, but if the context is lazily loaded its
  1938. * utilization statistics are not updated as the controlling thread
  1939. * is not tightly coupled with the execution of the spu context. We
  1940. * calculate and apply the time delta from the last recorded state
  1941. * of the spu context.
  1942. */
  1943. if (ctx->spu && ctx->stats.util_state == state) {
  1944. time += ktime_get_ns() - ctx->stats.tstamp;
  1945. }
  1946. return time / NSEC_PER_MSEC;
  1947. }
  1948. static unsigned long long spufs_slb_flts(struct spu_context *ctx)
  1949. {
  1950. unsigned long long slb_flts = ctx->stats.slb_flt;
  1951. if (ctx->state == SPU_STATE_RUNNABLE) {
  1952. slb_flts += (ctx->spu->stats.slb_flt -
  1953. ctx->stats.slb_flt_base);
  1954. }
  1955. return slb_flts;
  1956. }
  1957. static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
  1958. {
  1959. unsigned long long class2_intrs = ctx->stats.class2_intr;
  1960. if (ctx->state == SPU_STATE_RUNNABLE) {
  1961. class2_intrs += (ctx->spu->stats.class2_intr -
  1962. ctx->stats.class2_intr_base);
  1963. }
  1964. return class2_intrs;
  1965. }
  1966. static int spufs_show_stat(struct seq_file *s, void *private)
  1967. {
  1968. struct spu_context *ctx = s->private;
  1969. int ret;
  1970. ret = spu_acquire(ctx);
  1971. if (ret)
  1972. return ret;
  1973. seq_printf(s, "%s %llu %llu %llu %llu "
  1974. "%llu %llu %llu %llu %llu %llu %llu %llu\n",
  1975. ctx_state_names[ctx->stats.util_state],
  1976. spufs_acct_time(ctx, SPU_UTIL_USER),
  1977. spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
  1978. spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
  1979. spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
  1980. ctx->stats.vol_ctx_switch,
  1981. ctx->stats.invol_ctx_switch,
  1982. spufs_slb_flts(ctx),
  1983. ctx->stats.hash_flt,
  1984. ctx->stats.min_flt,
  1985. ctx->stats.maj_flt,
  1986. spufs_class2_intrs(ctx),
  1987. ctx->stats.libassist);
  1988. spu_release(ctx);
  1989. return 0;
  1990. }
  1991. static int spufs_stat_open(struct inode *inode, struct file *file)
  1992. {
  1993. return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
  1994. }
  1995. static const struct file_operations spufs_stat_fops = {
  1996. .open = spufs_stat_open,
  1997. .read = seq_read,
  1998. .llseek = seq_lseek,
  1999. .release = single_release,
  2000. };
  2001. static inline int spufs_switch_log_used(struct spu_context *ctx)
  2002. {
  2003. return (ctx->switch_log->head - ctx->switch_log->tail) %
  2004. SWITCH_LOG_BUFSIZE;
  2005. }
  2006. static inline int spufs_switch_log_avail(struct spu_context *ctx)
  2007. {
  2008. return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
  2009. }
  2010. static int spufs_switch_log_open(struct inode *inode, struct file *file)
  2011. {
  2012. struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
  2013. int rc;
  2014. rc = spu_acquire(ctx);
  2015. if (rc)
  2016. return rc;
  2017. if (ctx->switch_log) {
  2018. rc = -EBUSY;
  2019. goto out;
  2020. }
  2021. ctx->switch_log = kmalloc(sizeof(struct switch_log) +
  2022. SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
  2023. GFP_KERNEL);
  2024. if (!ctx->switch_log) {
  2025. rc = -ENOMEM;
  2026. goto out;
  2027. }
  2028. ctx->switch_log->head = ctx->switch_log->tail = 0;
  2029. init_waitqueue_head(&ctx->switch_log->wait);
  2030. rc = 0;
  2031. out:
  2032. spu_release(ctx);
  2033. return rc;
  2034. }
  2035. static int spufs_switch_log_release(struct inode *inode, struct file *file)
  2036. {
  2037. struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
  2038. int rc;
  2039. rc = spu_acquire(ctx);
  2040. if (rc)
  2041. return rc;
  2042. kfree(ctx->switch_log);
  2043. ctx->switch_log = NULL;
  2044. spu_release(ctx);
  2045. return 0;
  2046. }
  2047. static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
  2048. {
  2049. struct switch_log_entry *p;
  2050. p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
  2051. return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
  2052. (unsigned int) p->tstamp.tv_sec,
  2053. (unsigned int) p->tstamp.tv_nsec,
  2054. p->spu_id,
  2055. (unsigned int) p->type,
  2056. (unsigned int) p->val,
  2057. (unsigned long long) p->timebase);
  2058. }
  2059. static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
  2060. size_t len, loff_t *ppos)
  2061. {
  2062. struct inode *inode = file_inode(file);
  2063. struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
  2064. int error = 0, cnt = 0;
  2065. if (!buf)
  2066. return -EINVAL;
  2067. error = spu_acquire(ctx);
  2068. if (error)
  2069. return error;
  2070. while (cnt < len) {
  2071. char tbuf[128];
  2072. int width;
  2073. if (spufs_switch_log_used(ctx) == 0) {
  2074. if (cnt > 0) {
  2075. /* If there's data ready to go, we can
  2076. * just return straight away */
  2077. break;
  2078. } else if (file->f_flags & O_NONBLOCK) {
  2079. error = -EAGAIN;
  2080. break;
  2081. } else {
  2082. /* spufs_wait will drop the mutex and
  2083. * re-acquire, but since we're in read(), the
  2084. * file cannot be _released (and so
  2085. * ctx->switch_log is stable).
  2086. */
  2087. error = spufs_wait(ctx->switch_log->wait,
  2088. spufs_switch_log_used(ctx) > 0);
  2089. /* On error, spufs_wait returns without the
  2090. * state mutex held */
  2091. if (error)
  2092. return error;
  2093. /* We may have had entries read from underneath
  2094. * us while we dropped the mutex in spufs_wait,
  2095. * so re-check */
  2096. if (spufs_switch_log_used(ctx) == 0)
  2097. continue;
  2098. }
  2099. }
  2100. width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
  2101. if (width < len)
  2102. ctx->switch_log->tail =
  2103. (ctx->switch_log->tail + 1) %
  2104. SWITCH_LOG_BUFSIZE;
  2105. else
  2106. /* If the record is greater than space available return
  2107. * partial buffer (so far) */
  2108. break;
  2109. error = copy_to_user(buf + cnt, tbuf, width);
  2110. if (error)
  2111. break;
  2112. cnt += width;
  2113. }
  2114. spu_release(ctx);
  2115. return cnt == 0 ? error : cnt;
  2116. }
  2117. static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
  2118. {
  2119. struct inode *inode = file_inode(file);
  2120. struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
  2121. unsigned int mask = 0;
  2122. int rc;
  2123. poll_wait(file, &ctx->switch_log->wait, wait);
  2124. rc = spu_acquire(ctx);
  2125. if (rc)
  2126. return rc;
  2127. if (spufs_switch_log_used(ctx) > 0)
  2128. mask |= POLLIN;
  2129. spu_release(ctx);
  2130. return mask;
  2131. }
  2132. static const struct file_operations spufs_switch_log_fops = {
  2133. .open = spufs_switch_log_open,
  2134. .read = spufs_switch_log_read,
  2135. .poll = spufs_switch_log_poll,
  2136. .release = spufs_switch_log_release,
  2137. .llseek = no_llseek,
  2138. };
  2139. /**
  2140. * Log a context switch event to a switch log reader.
  2141. *
  2142. * Must be called with ctx->state_mutex held.
  2143. */
  2144. void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
  2145. u32 type, u32 val)
  2146. {
  2147. if (!ctx->switch_log)
  2148. return;
  2149. if (spufs_switch_log_avail(ctx) > 1) {
  2150. struct switch_log_entry *p;
  2151. p = ctx->switch_log->log + ctx->switch_log->head;
  2152. ktime_get_ts(&p->tstamp);
  2153. p->timebase = get_tb();
  2154. p->spu_id = spu ? spu->number : -1;
  2155. p->type = type;
  2156. p->val = val;
  2157. ctx->switch_log->head =
  2158. (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
  2159. }
  2160. wake_up(&ctx->switch_log->wait);
  2161. }
  2162. static int spufs_show_ctx(struct seq_file *s, void *private)
  2163. {
  2164. struct spu_context *ctx = s->private;
  2165. u64 mfc_control_RW;
  2166. mutex_lock(&ctx->state_mutex);
  2167. if (ctx->spu) {
  2168. struct spu *spu = ctx->spu;
  2169. struct spu_priv2 __iomem *priv2 = spu->priv2;
  2170. spin_lock_irq(&spu->register_lock);
  2171. mfc_control_RW = in_be64(&priv2->mfc_control_RW);
  2172. spin_unlock_irq(&spu->register_lock);
  2173. } else {
  2174. struct spu_state *csa = &ctx->csa;
  2175. mfc_control_RW = csa->priv2.mfc_control_RW;
  2176. }
  2177. seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
  2178. " %c %llx %llx %llx %llx %x %x\n",
  2179. ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
  2180. ctx->flags,
  2181. ctx->sched_flags,
  2182. ctx->prio,
  2183. ctx->time_slice,
  2184. ctx->spu ? ctx->spu->number : -1,
  2185. !list_empty(&ctx->rq) ? 'q' : ' ',
  2186. ctx->csa.class_0_pending,
  2187. ctx->csa.class_0_dar,
  2188. ctx->csa.class_1_dsisr,
  2189. mfc_control_RW,
  2190. ctx->ops->runcntl_read(ctx),
  2191. ctx->ops->status_read(ctx));
  2192. mutex_unlock(&ctx->state_mutex);
  2193. return 0;
  2194. }
  2195. static int spufs_ctx_open(struct inode *inode, struct file *file)
  2196. {
  2197. return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
  2198. }
  2199. static const struct file_operations spufs_ctx_fops = {
  2200. .open = spufs_ctx_open,
  2201. .read = seq_read,
  2202. .llseek = seq_lseek,
  2203. .release = single_release,
  2204. };
  2205. const struct spufs_tree_descr spufs_dir_contents[] = {
  2206. { "capabilities", &spufs_caps_fops, 0444, },
  2207. { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
  2208. { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
  2209. { "mbox", &spufs_mbox_fops, 0444, },
  2210. { "ibox", &spufs_ibox_fops, 0444, },
  2211. { "wbox", &spufs_wbox_fops, 0222, },
  2212. { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
  2213. { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
  2214. { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
  2215. { "signal1", &spufs_signal1_fops, 0666, },
  2216. { "signal2", &spufs_signal2_fops, 0666, },
  2217. { "signal1_type", &spufs_signal1_type, 0666, },
  2218. { "signal2_type", &spufs_signal2_type, 0666, },
  2219. { "cntl", &spufs_cntl_fops, 0666, },
  2220. { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
  2221. { "lslr", &spufs_lslr_ops, 0444, },
  2222. { "mfc", &spufs_mfc_fops, 0666, },
  2223. { "mss", &spufs_mss_fops, 0666, },
  2224. { "npc", &spufs_npc_ops, 0666, },
  2225. { "srr0", &spufs_srr0_ops, 0666, },
  2226. { "decr", &spufs_decr_ops, 0666, },
  2227. { "decr_status", &spufs_decr_status_ops, 0666, },
  2228. { "event_mask", &spufs_event_mask_ops, 0666, },
  2229. { "event_status", &spufs_event_status_ops, 0444, },
  2230. { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
  2231. { "phys-id", &spufs_id_ops, 0666, },
  2232. { "object-id", &spufs_object_id_ops, 0666, },
  2233. { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
  2234. { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
  2235. { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
  2236. { "dma_info", &spufs_dma_info_fops, 0444,
  2237. sizeof(struct spu_dma_info), },
  2238. { "proxydma_info", &spufs_proxydma_info_fops, 0444,
  2239. sizeof(struct spu_proxydma_info)},
  2240. { "tid", &spufs_tid_fops, 0444, },
  2241. { "stat", &spufs_stat_fops, 0444, },
  2242. { "switch_log", &spufs_switch_log_fops, 0444 },
  2243. {},
  2244. };
  2245. const struct spufs_tree_descr spufs_dir_nosched_contents[] = {
  2246. { "capabilities", &spufs_caps_fops, 0444, },
  2247. { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
  2248. { "mbox", &spufs_mbox_fops, 0444, },
  2249. { "ibox", &spufs_ibox_fops, 0444, },
  2250. { "wbox", &spufs_wbox_fops, 0222, },
  2251. { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
  2252. { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
  2253. { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
  2254. { "signal1", &spufs_signal1_nosched_fops, 0222, },
  2255. { "signal2", &spufs_signal2_nosched_fops, 0222, },
  2256. { "signal1_type", &spufs_signal1_type, 0666, },
  2257. { "signal2_type", &spufs_signal2_type, 0666, },
  2258. { "mss", &spufs_mss_fops, 0666, },
  2259. { "mfc", &spufs_mfc_fops, 0666, },
  2260. { "cntl", &spufs_cntl_fops, 0666, },
  2261. { "npc", &spufs_npc_ops, 0666, },
  2262. { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
  2263. { "phys-id", &spufs_id_ops, 0666, },
  2264. { "object-id", &spufs_object_id_ops, 0666, },
  2265. { "tid", &spufs_tid_fops, 0444, },
  2266. { "stat", &spufs_stat_fops, 0444, },
  2267. {},
  2268. };
  2269. const struct spufs_tree_descr spufs_dir_debug_contents[] = {
  2270. { ".ctx", &spufs_ctx_fops, 0444, },
  2271. {},
  2272. };
  2273. const struct spufs_coredump_reader spufs_coredump_read[] = {
  2274. { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
  2275. { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
  2276. { "lslr", NULL, spufs_lslr_get, 19 },
  2277. { "decr", NULL, spufs_decr_get, 19 },
  2278. { "decr_status", NULL, spufs_decr_status_get, 19 },
  2279. { "mem", __spufs_mem_read, NULL, LS_SIZE, },
  2280. { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
  2281. { "signal1_type", NULL, spufs_signal1_type_get, 19 },
  2282. { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
  2283. { "signal2_type", NULL, spufs_signal2_type_get, 19 },
  2284. { "event_mask", NULL, spufs_event_mask_get, 19 },
  2285. { "event_status", NULL, spufs_event_status_get, 19 },
  2286. { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
  2287. { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
  2288. { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
  2289. { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
  2290. { "proxydma_info", __spufs_proxydma_info_read,
  2291. NULL, sizeof(struct spu_proxydma_info)},
  2292. { "object-id", NULL, spufs_object_id_get, 19 },
  2293. { "npc", NULL, spufs_npc_get, 19 },
  2294. { NULL },
  2295. };