vpe.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can distribute it and/or modify it
  5. * under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. * for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  16. */
  17. /*
  18. * VPE support module
  19. *
  20. * Provides support for loading a MIPS SP program on VPE1.
  21. * The SP enviroment is rather simple, no tlb's. It needs to be relocatable
  22. * (or partially linked). You should initialise your stack in the startup
  23. * code. This loader looks for the symbol __start and sets up
  24. * execution to resume from there. The MIPS SDE kit contains suitable examples.
  25. *
  26. * To load and run, simply cat a SP 'program file' to /dev/vpe1.
  27. * i.e cat spapp >/dev/vpe1.
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/device.h>
  31. #include <linux/module.h>
  32. #include <linux/fs.h>
  33. #include <linux/init.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/slab.h>
  36. #include <linux/list.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/elf.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/syscalls.h>
  41. #include <linux/moduleloader.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/poll.h>
  44. #include <linux/bootmem.h>
  45. #include <asm/mipsregs.h>
  46. #include <asm/mipsmtregs.h>
  47. #include <asm/cacheflush.h>
  48. #include <asm/atomic.h>
  49. #include <asm/cpu.h>
  50. #include <asm/mips_mt.h>
  51. #include <asm/processor.h>
  52. #include <asm/system.h>
  53. #include <asm/vpe.h>
  54. #include <asm/kspd.h>
  55. #include <asm/mips_mt.h>
  56. typedef void *vpe_handle;
  57. #ifndef ARCH_SHF_SMALL
  58. #define ARCH_SHF_SMALL 0
  59. #endif
  60. /* If this is set, the section belongs in the init part of the module */
  61. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  62. /*
  63. * The number of TCs and VPEs physically available on the core
  64. */
  65. static int hw_tcs, hw_vpes;
  66. static char module_name[] = "vpe";
  67. static int major;
  68. static const int minor = 1; /* fixed for now */
  69. #ifdef CONFIG_MIPS_APSP_KSPD
  70. static struct kspd_notifications kspd_events;
  71. static int kspd_events_reqd = 0;
  72. #endif
  73. /* grab the likely amount of memory we will need. */
  74. #ifdef CONFIG_MIPS_VPE_LOADER_TOM
  75. #define P_SIZE (2 * 1024 * 1024)
  76. #else
  77. /* add an overhead to the max kmalloc size for non-striped symbols/etc */
  78. #define P_SIZE (256 * 1024)
  79. #endif
  80. extern unsigned long physical_memsize;
  81. #define MAX_VPES 16
  82. #define VPE_PATH_MAX 256
  83. enum vpe_state {
  84. VPE_STATE_UNUSED = 0,
  85. VPE_STATE_INUSE,
  86. VPE_STATE_RUNNING
  87. };
  88. enum tc_state {
  89. TC_STATE_UNUSED = 0,
  90. TC_STATE_INUSE,
  91. TC_STATE_RUNNING,
  92. TC_STATE_DYNAMIC
  93. };
  94. struct vpe {
  95. enum vpe_state state;
  96. /* (device) minor associated with this vpe */
  97. int minor;
  98. /* elfloader stuff */
  99. void *load_addr;
  100. unsigned long len;
  101. char *pbuffer;
  102. unsigned long plen;
  103. unsigned int uid, gid;
  104. char cwd[VPE_PATH_MAX];
  105. unsigned long __start;
  106. /* tc's associated with this vpe */
  107. struct list_head tc;
  108. /* The list of vpe's */
  109. struct list_head list;
  110. /* shared symbol address */
  111. void *shared_ptr;
  112. /* the list of who wants to know when something major happens */
  113. struct list_head notify;
  114. unsigned int ntcs;
  115. };
  116. struct tc {
  117. enum tc_state state;
  118. int index;
  119. struct vpe *pvpe; /* parent VPE */
  120. struct list_head tc; /* The list of TC's with this VPE */
  121. struct list_head list; /* The global list of tc's */
  122. };
  123. struct {
  124. /* Virtual processing elements */
  125. struct list_head vpe_list;
  126. /* Thread contexts */
  127. struct list_head tc_list;
  128. } vpecontrol = {
  129. .vpe_list = LIST_HEAD_INIT(vpecontrol.vpe_list),
  130. .tc_list = LIST_HEAD_INIT(vpecontrol.tc_list)
  131. };
  132. static void release_progmem(void *ptr);
  133. extern void save_gp_address(unsigned int secbase, unsigned int rel);
  134. /* get the vpe associated with this minor */
  135. struct vpe *get_vpe(int minor)
  136. {
  137. struct vpe *v;
  138. if (!cpu_has_mipsmt)
  139. return NULL;
  140. list_for_each_entry(v, &vpecontrol.vpe_list, list) {
  141. if (v->minor == minor)
  142. return v;
  143. }
  144. return NULL;
  145. }
  146. /* get the vpe associated with this minor */
  147. struct tc *get_tc(int index)
  148. {
  149. struct tc *t;
  150. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  151. if (t->index == index)
  152. return t;
  153. }
  154. return NULL;
  155. }
  156. struct tc *get_tc_unused(void)
  157. {
  158. struct tc *t;
  159. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  160. if (t->state == TC_STATE_UNUSED)
  161. return t;
  162. }
  163. return NULL;
  164. }
  165. /* allocate a vpe and associate it with this minor (or index) */
  166. struct vpe *alloc_vpe(int minor)
  167. {
  168. struct vpe *v;
  169. if ((v = kzalloc(sizeof(struct vpe), GFP_KERNEL)) == NULL) {
  170. return NULL;
  171. }
  172. INIT_LIST_HEAD(&v->tc);
  173. list_add_tail(&v->list, &vpecontrol.vpe_list);
  174. INIT_LIST_HEAD(&v->notify);
  175. v->minor = minor;
  176. return v;
  177. }
  178. /* allocate a tc. At startup only tc0 is running, all other can be halted. */
  179. struct tc *alloc_tc(int index)
  180. {
  181. struct tc *tc;
  182. if ((tc = kzalloc(sizeof(struct tc), GFP_KERNEL)) == NULL)
  183. goto out;
  184. INIT_LIST_HEAD(&tc->tc);
  185. tc->index = index;
  186. list_add_tail(&tc->list, &vpecontrol.tc_list);
  187. out:
  188. return tc;
  189. }
  190. /* clean up and free everything */
  191. void release_vpe(struct vpe *v)
  192. {
  193. list_del(&v->list);
  194. if (v->load_addr)
  195. release_progmem(v);
  196. kfree(v);
  197. }
  198. void dump_mtregs(void)
  199. {
  200. unsigned long val;
  201. val = read_c0_config3();
  202. printk("config3 0x%lx MT %ld\n", val,
  203. (val & CONFIG3_MT) >> CONFIG3_MT_SHIFT);
  204. val = read_c0_mvpcontrol();
  205. printk("MVPControl 0x%lx, STLB %ld VPC %ld EVP %ld\n", val,
  206. (val & MVPCONTROL_STLB) >> MVPCONTROL_STLB_SHIFT,
  207. (val & MVPCONTROL_VPC) >> MVPCONTROL_VPC_SHIFT,
  208. (val & MVPCONTROL_EVP));
  209. val = read_c0_mvpconf0();
  210. printk("mvpconf0 0x%lx, PVPE %ld PTC %ld M %ld\n", val,
  211. (val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT,
  212. val & MVPCONF0_PTC, (val & MVPCONF0_M) >> MVPCONF0_M_SHIFT);
  213. }
  214. /* Find some VPE program space */
  215. static void *alloc_progmem(unsigned long len)
  216. {
  217. #ifdef CONFIG_MIPS_VPE_LOADER_TOM
  218. /* this means you must tell linux to use less memory than you physically have */
  219. return pfn_to_kaddr(max_pfn);
  220. #else
  221. // simple grab some mem for now
  222. return kmalloc(len, GFP_KERNEL);
  223. #endif
  224. }
  225. static void release_progmem(void *ptr)
  226. {
  227. #ifndef CONFIG_MIPS_VPE_LOADER_TOM
  228. kfree(ptr);
  229. #endif
  230. }
  231. /* Update size with this section: return offset. */
  232. static long get_offset(unsigned long *size, Elf_Shdr * sechdr)
  233. {
  234. long ret;
  235. ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
  236. *size = ret + sechdr->sh_size;
  237. return ret;
  238. }
  239. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  240. might -- code, read-only data, read-write data, small data. Tally
  241. sizes, and place the offsets into sh_entsize fields: high bit means it
  242. belongs in init. */
  243. static void layout_sections(struct module *mod, const Elf_Ehdr * hdr,
  244. Elf_Shdr * sechdrs, const char *secstrings)
  245. {
  246. static unsigned long const masks[][2] = {
  247. /* NOTE: all executable code must be the first section
  248. * in this array; otherwise modify the text_size
  249. * finder in the two loops below */
  250. {SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
  251. {SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
  252. {SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
  253. {ARCH_SHF_SMALL | SHF_ALLOC, 0}
  254. };
  255. unsigned int m, i;
  256. for (i = 0; i < hdr->e_shnum; i++)
  257. sechdrs[i].sh_entsize = ~0UL;
  258. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  259. for (i = 0; i < hdr->e_shnum; ++i) {
  260. Elf_Shdr *s = &sechdrs[i];
  261. // || strncmp(secstrings + s->sh_name, ".init", 5) == 0)
  262. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  263. || (s->sh_flags & masks[m][1])
  264. || s->sh_entsize != ~0UL)
  265. continue;
  266. s->sh_entsize = get_offset(&mod->core_size, s);
  267. }
  268. if (m == 0)
  269. mod->core_text_size = mod->core_size;
  270. }
  271. }
  272. /* from module-elf32.c, but subverted a little */
  273. struct mips_hi16 {
  274. struct mips_hi16 *next;
  275. Elf32_Addr *addr;
  276. Elf32_Addr value;
  277. };
  278. static struct mips_hi16 *mips_hi16_list;
  279. static unsigned int gp_offs, gp_addr;
  280. static int apply_r_mips_none(struct module *me, uint32_t *location,
  281. Elf32_Addr v)
  282. {
  283. return 0;
  284. }
  285. static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
  286. Elf32_Addr v)
  287. {
  288. int rel;
  289. if( !(*location & 0xffff) ) {
  290. rel = (int)v - gp_addr;
  291. }
  292. else {
  293. /* .sbss + gp(relative) + offset */
  294. /* kludge! */
  295. rel = (int)(short)((int)v + gp_offs +
  296. (int)(short)(*location & 0xffff) - gp_addr);
  297. }
  298. if( (rel > 32768) || (rel < -32768) ) {
  299. printk(KERN_DEBUG "VPE loader: apply_r_mips_gprel16: "
  300. "relative address 0x%x out of range of gp register\n",
  301. rel);
  302. return -ENOEXEC;
  303. }
  304. *location = (*location & 0xffff0000) | (rel & 0xffff);
  305. return 0;
  306. }
  307. static int apply_r_mips_pc16(struct module *me, uint32_t *location,
  308. Elf32_Addr v)
  309. {
  310. int rel;
  311. rel = (((unsigned int)v - (unsigned int)location));
  312. rel >>= 2; // because the offset is in _instructions_ not bytes.
  313. rel -= 1; // and one instruction less due to the branch delay slot.
  314. if( (rel > 32768) || (rel < -32768) ) {
  315. printk(KERN_DEBUG "VPE loader: "
  316. "apply_r_mips_pc16: relative address out of range 0x%x\n", rel);
  317. return -ENOEXEC;
  318. }
  319. *location = (*location & 0xffff0000) | (rel & 0xffff);
  320. return 0;
  321. }
  322. static int apply_r_mips_32(struct module *me, uint32_t *location,
  323. Elf32_Addr v)
  324. {
  325. *location += v;
  326. return 0;
  327. }
  328. static int apply_r_mips_26(struct module *me, uint32_t *location,
  329. Elf32_Addr v)
  330. {
  331. if (v % 4) {
  332. printk(KERN_DEBUG "VPE loader: apply_r_mips_26 "
  333. " unaligned relocation\n");
  334. return -ENOEXEC;
  335. }
  336. /*
  337. * Not desperately convinced this is a good check of an overflow condition
  338. * anyway. But it gets in the way of handling undefined weak symbols which
  339. * we want to set to zero.
  340. * if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  341. * printk(KERN_ERR
  342. * "module %s: relocation overflow\n",
  343. * me->name);
  344. * return -ENOEXEC;
  345. * }
  346. */
  347. *location = (*location & ~0x03ffffff) |
  348. ((*location + (v >> 2)) & 0x03ffffff);
  349. return 0;
  350. }
  351. static int apply_r_mips_hi16(struct module *me, uint32_t *location,
  352. Elf32_Addr v)
  353. {
  354. struct mips_hi16 *n;
  355. /*
  356. * We cannot relocate this one now because we don't know the value of
  357. * the carry we need to add. Save the information, and let LO16 do the
  358. * actual relocation.
  359. */
  360. n = kmalloc(sizeof *n, GFP_KERNEL);
  361. if (!n)
  362. return -ENOMEM;
  363. n->addr = location;
  364. n->value = v;
  365. n->next = mips_hi16_list;
  366. mips_hi16_list = n;
  367. return 0;
  368. }
  369. static int apply_r_mips_lo16(struct module *me, uint32_t *location,
  370. Elf32_Addr v)
  371. {
  372. unsigned long insnlo = *location;
  373. Elf32_Addr val, vallo;
  374. /* Sign extend the addend we extract from the lo insn. */
  375. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  376. if (mips_hi16_list != NULL) {
  377. struct mips_hi16 *l;
  378. l = mips_hi16_list;
  379. while (l != NULL) {
  380. struct mips_hi16 *next;
  381. unsigned long insn;
  382. /*
  383. * The value for the HI16 had best be the same.
  384. */
  385. if (v != l->value) {
  386. printk(KERN_DEBUG "VPE loader: "
  387. "apply_r_mips_lo16/hi16: "
  388. "inconsistent value information\n");
  389. return -ENOEXEC;
  390. }
  391. /*
  392. * Do the HI16 relocation. Note that we actually don't
  393. * need to know anything about the LO16 itself, except
  394. * where to find the low 16 bits of the addend needed
  395. * by the LO16.
  396. */
  397. insn = *l->addr;
  398. val = ((insn & 0xffff) << 16) + vallo;
  399. val += v;
  400. /*
  401. * Account for the sign extension that will happen in
  402. * the low bits.
  403. */
  404. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  405. insn = (insn & ~0xffff) | val;
  406. *l->addr = insn;
  407. next = l->next;
  408. kfree(l);
  409. l = next;
  410. }
  411. mips_hi16_list = NULL;
  412. }
  413. /*
  414. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  415. */
  416. val = v + vallo;
  417. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  418. *location = insnlo;
  419. return 0;
  420. }
  421. static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
  422. Elf32_Addr v) = {
  423. [R_MIPS_NONE] = apply_r_mips_none,
  424. [R_MIPS_32] = apply_r_mips_32,
  425. [R_MIPS_26] = apply_r_mips_26,
  426. [R_MIPS_HI16] = apply_r_mips_hi16,
  427. [R_MIPS_LO16] = apply_r_mips_lo16,
  428. [R_MIPS_GPREL16] = apply_r_mips_gprel16,
  429. [R_MIPS_PC16] = apply_r_mips_pc16
  430. };
  431. static char *rstrs[] = {
  432. [R_MIPS_NONE] = "MIPS_NONE",
  433. [R_MIPS_32] = "MIPS_32",
  434. [R_MIPS_26] = "MIPS_26",
  435. [R_MIPS_HI16] = "MIPS_HI16",
  436. [R_MIPS_LO16] = "MIPS_LO16",
  437. [R_MIPS_GPREL16] = "MIPS_GPREL16",
  438. [R_MIPS_PC16] = "MIPS_PC16"
  439. };
  440. int apply_relocations(Elf32_Shdr *sechdrs,
  441. const char *strtab,
  442. unsigned int symindex,
  443. unsigned int relsec,
  444. struct module *me)
  445. {
  446. Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  447. Elf32_Sym *sym;
  448. uint32_t *location;
  449. unsigned int i;
  450. Elf32_Addr v;
  451. int res;
  452. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  453. Elf32_Word r_info = rel[i].r_info;
  454. /* This is where to make the change */
  455. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  456. + rel[i].r_offset;
  457. /* This is the symbol it is referring to */
  458. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  459. + ELF32_R_SYM(r_info);
  460. if (!sym->st_value) {
  461. printk(KERN_DEBUG "%s: undefined weak symbol %s\n",
  462. me->name, strtab + sym->st_name);
  463. /* just print the warning, dont barf */
  464. }
  465. v = sym->st_value;
  466. res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
  467. if( res ) {
  468. char *r = rstrs[ELF32_R_TYPE(r_info)];
  469. printk(KERN_WARNING "VPE loader: .text+0x%x "
  470. "relocation type %s for symbol \"%s\" failed\n",
  471. rel[i].r_offset, r ? r : "UNKNOWN",
  472. strtab + sym->st_name);
  473. return res;
  474. }
  475. }
  476. return 0;
  477. }
  478. void save_gp_address(unsigned int secbase, unsigned int rel)
  479. {
  480. gp_addr = secbase + rel;
  481. gp_offs = gp_addr - (secbase & 0xffff0000);
  482. }
  483. /* end module-elf32.c */
  484. /* Change all symbols so that sh_value encodes the pointer directly. */
  485. static void simplify_symbols(Elf_Shdr * sechdrs,
  486. unsigned int symindex,
  487. const char *strtab,
  488. const char *secstrings,
  489. unsigned int nsecs, struct module *mod)
  490. {
  491. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  492. unsigned long secbase, bssbase = 0;
  493. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  494. int size;
  495. /* find the .bss section for COMMON symbols */
  496. for (i = 0; i < nsecs; i++) {
  497. if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0) {
  498. bssbase = sechdrs[i].sh_addr;
  499. break;
  500. }
  501. }
  502. for (i = 1; i < n; i++) {
  503. switch (sym[i].st_shndx) {
  504. case SHN_COMMON:
  505. /* Allocate space for the symbol in the .bss section.
  506. st_value is currently size.
  507. We want it to have the address of the symbol. */
  508. size = sym[i].st_value;
  509. sym[i].st_value = bssbase;
  510. bssbase += size;
  511. break;
  512. case SHN_ABS:
  513. /* Don't need to do anything */
  514. break;
  515. case SHN_UNDEF:
  516. /* ret = -ENOENT; */
  517. break;
  518. case SHN_MIPS_SCOMMON:
  519. printk(KERN_DEBUG "simplify_symbols: ignoring SHN_MIPS_SCOMMON"
  520. "symbol <%s> st_shndx %d\n", strtab + sym[i].st_name,
  521. sym[i].st_shndx);
  522. // .sbss section
  523. break;
  524. default:
  525. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  526. if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0) {
  527. save_gp_address(secbase, sym[i].st_value);
  528. }
  529. sym[i].st_value += secbase;
  530. break;
  531. }
  532. }
  533. }
  534. #ifdef DEBUG_ELFLOADER
  535. static void dump_elfsymbols(Elf_Shdr * sechdrs, unsigned int symindex,
  536. const char *strtab, struct module *mod)
  537. {
  538. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  539. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  540. printk(KERN_DEBUG "dump_elfsymbols: n %d\n", n);
  541. for (i = 1; i < n; i++) {
  542. printk(KERN_DEBUG " i %d name <%s> 0x%x\n", i,
  543. strtab + sym[i].st_name, sym[i].st_value);
  544. }
  545. }
  546. #endif
  547. /* We are prepared so configure and start the VPE... */
  548. static int vpe_run(struct vpe * v)
  549. {
  550. unsigned long flags, val, dmt_flag;
  551. struct vpe_notifications *n;
  552. unsigned int vpeflags;
  553. struct tc *t;
  554. /* check we are the Master VPE */
  555. local_irq_save(flags);
  556. val = read_c0_vpeconf0();
  557. if (!(val & VPECONF0_MVP)) {
  558. printk(KERN_WARNING
  559. "VPE loader: only Master VPE's are allowed to configure MT\n");
  560. local_irq_restore(flags);
  561. return -1;
  562. }
  563. dmt_flag = dmt();
  564. vpeflags = dvpe();
  565. if (!list_empty(&v->tc)) {
  566. if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
  567. evpe(vpeflags);
  568. emt(dmt_flag);
  569. local_irq_restore(flags);
  570. printk(KERN_WARNING
  571. "VPE loader: TC %d is already in use.\n",
  572. t->index);
  573. return -ENOEXEC;
  574. }
  575. } else {
  576. evpe(vpeflags);
  577. emt(dmt_flag);
  578. local_irq_restore(flags);
  579. printk(KERN_WARNING
  580. "VPE loader: No TC's associated with VPE %d\n",
  581. v->minor);
  582. return -ENOEXEC;
  583. }
  584. /* Put MVPE's into 'configuration state' */
  585. set_c0_mvpcontrol(MVPCONTROL_VPC);
  586. settc(t->index);
  587. /* should check it is halted, and not activated */
  588. if ((read_tc_c0_tcstatus() & TCSTATUS_A) || !(read_tc_c0_tchalt() & TCHALT_H)) {
  589. evpe(vpeflags);
  590. emt(dmt_flag);
  591. local_irq_restore(flags);
  592. printk(KERN_WARNING "VPE loader: TC %d is already active!\n",
  593. t->index);
  594. return -ENOEXEC;
  595. }
  596. /* Write the address we want it to start running from in the TCPC register. */
  597. write_tc_c0_tcrestart((unsigned long)v->__start);
  598. write_tc_c0_tccontext((unsigned long)0);
  599. /*
  600. * Mark the TC as activated, not interrupt exempt and not dynamically
  601. * allocatable
  602. */
  603. val = read_tc_c0_tcstatus();
  604. val = (val & ~(TCSTATUS_DA | TCSTATUS_IXMT)) | TCSTATUS_A;
  605. write_tc_c0_tcstatus(val);
  606. write_tc_c0_tchalt(read_tc_c0_tchalt() & ~TCHALT_H);
  607. /*
  608. * The sde-kit passes 'memsize' to __start in $a3, so set something
  609. * here... Or set $a3 to zero and define DFLT_STACK_SIZE and
  610. * DFLT_HEAP_SIZE when you compile your program
  611. */
  612. mttgpr(6, v->ntcs);
  613. mttgpr(7, physical_memsize);
  614. /* set up VPE1 */
  615. /*
  616. * bind the TC to VPE 1 as late as possible so we only have the final
  617. * VPE registers to set up, and so an EJTAG probe can trigger on it
  618. */
  619. write_tc_c0_tcbind((read_tc_c0_tcbind() & ~TCBIND_CURVPE) | 1);
  620. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~(VPECONF0_VPA));
  621. back_to_back_c0_hazard();
  622. /* Set up the XTC bit in vpeconf0 to point at our tc */
  623. write_vpe_c0_vpeconf0( (read_vpe_c0_vpeconf0() & ~(VPECONF0_XTC))
  624. | (t->index << VPECONF0_XTC_SHIFT));
  625. back_to_back_c0_hazard();
  626. /* enable this VPE */
  627. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  628. /* clear out any left overs from a previous program */
  629. write_vpe_c0_status(0);
  630. write_vpe_c0_cause(0);
  631. /* take system out of configuration state */
  632. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  633. #ifdef CONFIG_SMP
  634. evpe(EVPE_ENABLE);
  635. #else
  636. evpe(vpeflags);
  637. #endif
  638. emt(dmt_flag);
  639. local_irq_restore(flags);
  640. list_for_each_entry(n, &v->notify, list)
  641. n->start(minor);
  642. return 0;
  643. }
  644. static int find_vpe_symbols(struct vpe * v, Elf_Shdr * sechdrs,
  645. unsigned int symindex, const char *strtab,
  646. struct module *mod)
  647. {
  648. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  649. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  650. for (i = 1; i < n; i++) {
  651. if (strcmp(strtab + sym[i].st_name, "__start") == 0) {
  652. v->__start = sym[i].st_value;
  653. }
  654. if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0) {
  655. v->shared_ptr = (void *)sym[i].st_value;
  656. }
  657. }
  658. if ( (v->__start == 0) || (v->shared_ptr == NULL))
  659. return -1;
  660. return 0;
  661. }
  662. /*
  663. * Allocates a VPE with some program code space(the load address), copies the
  664. * contents of the program (p)buffer performing relocatations/etc, free's it
  665. * when finished.
  666. */
  667. static int vpe_elfload(struct vpe * v)
  668. {
  669. Elf_Ehdr *hdr;
  670. Elf_Shdr *sechdrs;
  671. long err = 0;
  672. char *secstrings, *strtab = NULL;
  673. unsigned int len, i, symindex = 0, strindex = 0, relocate = 0;
  674. struct module mod; // so we can re-use the relocations code
  675. memset(&mod, 0, sizeof(struct module));
  676. strcpy(mod.name, "VPE loader");
  677. hdr = (Elf_Ehdr *) v->pbuffer;
  678. len = v->plen;
  679. /* Sanity checks against insmoding binaries or wrong arch,
  680. weird elf version */
  681. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  682. || (hdr->e_type != ET_REL && hdr->e_type != ET_EXEC)
  683. || !elf_check_arch(hdr)
  684. || hdr->e_shentsize != sizeof(*sechdrs)) {
  685. printk(KERN_WARNING
  686. "VPE loader: program wrong arch or weird elf version\n");
  687. return -ENOEXEC;
  688. }
  689. if (hdr->e_type == ET_REL)
  690. relocate = 1;
  691. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
  692. printk(KERN_ERR "VPE loader: program length %u truncated\n",
  693. len);
  694. return -ENOEXEC;
  695. }
  696. /* Convenience variables */
  697. sechdrs = (void *)hdr + hdr->e_shoff;
  698. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  699. sechdrs[0].sh_addr = 0;
  700. /* And these should exist, but gcc whinges if we don't init them */
  701. symindex = strindex = 0;
  702. if (relocate) {
  703. for (i = 1; i < hdr->e_shnum; i++) {
  704. if (sechdrs[i].sh_type != SHT_NOBITS
  705. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) {
  706. printk(KERN_ERR "VPE program length %u truncated\n",
  707. len);
  708. return -ENOEXEC;
  709. }
  710. /* Mark all sections sh_addr with their address in the
  711. temporary image. */
  712. sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
  713. /* Internal symbols and strings. */
  714. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  715. symindex = i;
  716. strindex = sechdrs[i].sh_link;
  717. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  718. }
  719. }
  720. layout_sections(&mod, hdr, sechdrs, secstrings);
  721. }
  722. v->load_addr = alloc_progmem(mod.core_size);
  723. memset(v->load_addr, 0, mod.core_size);
  724. printk("VPE loader: loading to %p\n", v->load_addr);
  725. if (relocate) {
  726. for (i = 0; i < hdr->e_shnum; i++) {
  727. void *dest;
  728. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  729. continue;
  730. dest = v->load_addr + sechdrs[i].sh_entsize;
  731. if (sechdrs[i].sh_type != SHT_NOBITS)
  732. memcpy(dest, (void *)sechdrs[i].sh_addr,
  733. sechdrs[i].sh_size);
  734. /* Update sh_addr to point to copy in image. */
  735. sechdrs[i].sh_addr = (unsigned long)dest;
  736. printk(KERN_DEBUG " section sh_name %s sh_addr 0x%x\n",
  737. secstrings + sechdrs[i].sh_name, sechdrs[i].sh_addr);
  738. }
  739. /* Fix up syms, so that st_value is a pointer to location. */
  740. simplify_symbols(sechdrs, symindex, strtab, secstrings,
  741. hdr->e_shnum, &mod);
  742. /* Now do relocations. */
  743. for (i = 1; i < hdr->e_shnum; i++) {
  744. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  745. unsigned int info = sechdrs[i].sh_info;
  746. /* Not a valid relocation section? */
  747. if (info >= hdr->e_shnum)
  748. continue;
  749. /* Don't bother with non-allocated sections */
  750. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  751. continue;
  752. if (sechdrs[i].sh_type == SHT_REL)
  753. err = apply_relocations(sechdrs, strtab, symindex, i,
  754. &mod);
  755. else if (sechdrs[i].sh_type == SHT_RELA)
  756. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  757. &mod);
  758. if (err < 0)
  759. return err;
  760. }
  761. } else {
  762. for (i = 0; i < hdr->e_shnum; i++) {
  763. /* Internal symbols and strings. */
  764. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  765. symindex = i;
  766. strindex = sechdrs[i].sh_link;
  767. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  768. /* mark the symtab's address for when we try to find the
  769. magic symbols */
  770. sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
  771. }
  772. /* filter sections we dont want in the final image */
  773. if (!(sechdrs[i].sh_flags & SHF_ALLOC) ||
  774. (sechdrs[i].sh_type == SHT_MIPS_REGINFO)) {
  775. printk( KERN_DEBUG " ignoring section, "
  776. "name %s type %x address 0x%x \n",
  777. secstrings + sechdrs[i].sh_name,
  778. sechdrs[i].sh_type, sechdrs[i].sh_addr);
  779. continue;
  780. }
  781. if (sechdrs[i].sh_addr < (unsigned int)v->load_addr) {
  782. printk( KERN_WARNING "VPE loader: "
  783. "fully linked image has invalid section, "
  784. "name %s type %x address 0x%x, before load "
  785. "address of 0x%x\n",
  786. secstrings + sechdrs[i].sh_name,
  787. sechdrs[i].sh_type, sechdrs[i].sh_addr,
  788. (unsigned int)v->load_addr);
  789. return -ENOEXEC;
  790. }
  791. printk(KERN_DEBUG " copying section sh_name %s, sh_addr 0x%x "
  792. "size 0x%x0 from x%p\n",
  793. secstrings + sechdrs[i].sh_name, sechdrs[i].sh_addr,
  794. sechdrs[i].sh_size, hdr + sechdrs[i].sh_offset);
  795. if (sechdrs[i].sh_type != SHT_NOBITS)
  796. memcpy((void *)sechdrs[i].sh_addr,
  797. (char *)hdr + sechdrs[i].sh_offset,
  798. sechdrs[i].sh_size);
  799. else
  800. memset((void *)sechdrs[i].sh_addr, 0, sechdrs[i].sh_size);
  801. }
  802. }
  803. /* make sure it's physically written out */
  804. flush_icache_range((unsigned long)v->load_addr,
  805. (unsigned long)v->load_addr + v->len);
  806. if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
  807. if (v->__start == 0) {
  808. printk(KERN_WARNING "VPE loader: program does not contain "
  809. "a __start symbol\n");
  810. return -ENOEXEC;
  811. }
  812. if (v->shared_ptr == NULL)
  813. printk(KERN_WARNING "VPE loader: "
  814. "program does not contain vpe_shared symbol.\n"
  815. " Unable to use AMVP (AP/SP) facilities.\n");
  816. }
  817. printk(" elf loaded\n");
  818. return 0;
  819. }
  820. static void cleanup_tc(struct tc *tc)
  821. {
  822. unsigned long flags;
  823. unsigned int mtflags, vpflags;
  824. int tmp;
  825. local_irq_save(flags);
  826. mtflags = dmt();
  827. vpflags = dvpe();
  828. /* Put MVPE's into 'configuration state' */
  829. set_c0_mvpcontrol(MVPCONTROL_VPC);
  830. settc(tc->index);
  831. tmp = read_tc_c0_tcstatus();
  832. /* mark not allocated and not dynamically allocatable */
  833. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  834. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  835. write_tc_c0_tcstatus(tmp);
  836. write_tc_c0_tchalt(TCHALT_H);
  837. /* bind it to anything other than VPE1 */
  838. // write_tc_c0_tcbind(read_tc_c0_tcbind() & ~TCBIND_CURVPE); // | TCBIND_CURVPE
  839. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  840. evpe(vpflags);
  841. emt(mtflags);
  842. local_irq_restore(flags);
  843. }
  844. static int getcwd(char *buff, int size)
  845. {
  846. mm_segment_t old_fs;
  847. int ret;
  848. old_fs = get_fs();
  849. set_fs(KERNEL_DS);
  850. ret = sys_getcwd(buff,size);
  851. set_fs(old_fs);
  852. return ret;
  853. }
  854. /* checks VPE is unused and gets ready to load program */
  855. static int vpe_open(struct inode *inode, struct file *filp)
  856. {
  857. enum vpe_state state;
  858. struct vpe_notifications *not;
  859. struct vpe *v;
  860. int ret;
  861. if (minor != iminor(inode)) {
  862. /* assume only 1 device at the moment. */
  863. printk(KERN_WARNING "VPE loader: only vpe1 is supported\n");
  864. return -ENODEV;
  865. }
  866. if ((v = get_vpe(tclimit)) == NULL) {
  867. printk(KERN_WARNING "VPE loader: unable to get vpe\n");
  868. return -ENODEV;
  869. }
  870. state = xchg(&v->state, VPE_STATE_INUSE);
  871. if (state != VPE_STATE_UNUSED) {
  872. printk(KERN_DEBUG "VPE loader: tc in use dumping regs\n");
  873. list_for_each_entry(not, &v->notify, list) {
  874. not->stop(tclimit);
  875. }
  876. release_progmem(v->load_addr);
  877. cleanup_tc(get_tc(tclimit));
  878. }
  879. /* this of-course trashes what was there before... */
  880. v->pbuffer = vmalloc(P_SIZE);
  881. v->plen = P_SIZE;
  882. v->load_addr = NULL;
  883. v->len = 0;
  884. v->uid = filp->f_uid;
  885. v->gid = filp->f_gid;
  886. #ifdef CONFIG_MIPS_APSP_KSPD
  887. /* get kspd to tell us when a syscall_exit happens */
  888. if (!kspd_events_reqd) {
  889. kspd_notify(&kspd_events);
  890. kspd_events_reqd++;
  891. }
  892. #endif
  893. v->cwd[0] = 0;
  894. ret = getcwd(v->cwd, VPE_PATH_MAX);
  895. if (ret < 0)
  896. printk(KERN_WARNING "VPE loader: open, getcwd returned %d\n", ret);
  897. v->shared_ptr = NULL;
  898. v->__start = 0;
  899. return 0;
  900. }
  901. static int vpe_release(struct inode *inode, struct file *filp)
  902. {
  903. struct vpe *v;
  904. Elf_Ehdr *hdr;
  905. int ret = 0;
  906. v = get_vpe(tclimit);
  907. if (v == NULL)
  908. return -ENODEV;
  909. hdr = (Elf_Ehdr *) v->pbuffer;
  910. if (memcmp(hdr->e_ident, ELFMAG, 4) == 0) {
  911. if (vpe_elfload(v) >= 0) {
  912. vpe_run(v);
  913. } else {
  914. printk(KERN_WARNING "VPE loader: ELF load failed.\n");
  915. ret = -ENOEXEC;
  916. }
  917. } else {
  918. printk(KERN_WARNING "VPE loader: only elf files are supported\n");
  919. ret = -ENOEXEC;
  920. }
  921. /* It's good to be able to run the SP and if it chokes have a look at
  922. the /dev/rt?. But if we reset the pointer to the shared struct we
  923. loose what has happened. So perhaps if garbage is sent to the vpe
  924. device, use it as a trigger for the reset. Hopefully a nice
  925. executable will be along shortly. */
  926. if (ret < 0)
  927. v->shared_ptr = NULL;
  928. // cleanup any temp buffers
  929. if (v->pbuffer)
  930. vfree(v->pbuffer);
  931. v->plen = 0;
  932. return ret;
  933. }
  934. static ssize_t vpe_write(struct file *file, const char __user * buffer,
  935. size_t count, loff_t * ppos)
  936. {
  937. size_t ret = count;
  938. struct vpe *v;
  939. if (iminor(file->f_path.dentry->d_inode) != minor)
  940. return -ENODEV;
  941. v = get_vpe(tclimit);
  942. if (v == NULL)
  943. return -ENODEV;
  944. if (v->pbuffer == NULL) {
  945. printk(KERN_ERR "VPE loader: no buffer for program\n");
  946. return -ENOMEM;
  947. }
  948. if ((count + v->len) > v->plen) {
  949. printk(KERN_WARNING
  950. "VPE loader: elf size too big. Perhaps strip uneeded symbols\n");
  951. return -ENOMEM;
  952. }
  953. count -= copy_from_user(v->pbuffer + v->len, buffer, count);
  954. if (!count)
  955. return -EFAULT;
  956. v->len += count;
  957. return ret;
  958. }
  959. static const struct file_operations vpe_fops = {
  960. .owner = THIS_MODULE,
  961. .open = vpe_open,
  962. .release = vpe_release,
  963. .write = vpe_write
  964. };
  965. /* module wrapper entry points */
  966. /* give me a vpe */
  967. vpe_handle vpe_alloc(void)
  968. {
  969. int i;
  970. struct vpe *v;
  971. /* find a vpe */
  972. for (i = 1; i < MAX_VPES; i++) {
  973. if ((v = get_vpe(i)) != NULL) {
  974. v->state = VPE_STATE_INUSE;
  975. return v;
  976. }
  977. }
  978. return NULL;
  979. }
  980. EXPORT_SYMBOL(vpe_alloc);
  981. /* start running from here */
  982. int vpe_start(vpe_handle vpe, unsigned long start)
  983. {
  984. struct vpe *v = vpe;
  985. v->__start = start;
  986. return vpe_run(v);
  987. }
  988. EXPORT_SYMBOL(vpe_start);
  989. /* halt it for now */
  990. int vpe_stop(vpe_handle vpe)
  991. {
  992. struct vpe *v = vpe;
  993. struct tc *t;
  994. unsigned int evpe_flags;
  995. evpe_flags = dvpe();
  996. if ((t = list_entry(v->tc.next, struct tc, tc)) != NULL) {
  997. settc(t->index);
  998. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  999. }
  1000. evpe(evpe_flags);
  1001. return 0;
  1002. }
  1003. EXPORT_SYMBOL(vpe_stop);
  1004. /* I've done with it thank you */
  1005. int vpe_free(vpe_handle vpe)
  1006. {
  1007. struct vpe *v = vpe;
  1008. struct tc *t;
  1009. unsigned int evpe_flags;
  1010. if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
  1011. return -ENOEXEC;
  1012. }
  1013. evpe_flags = dvpe();
  1014. /* Put MVPE's into 'configuration state' */
  1015. set_c0_mvpcontrol(MVPCONTROL_VPC);
  1016. settc(t->index);
  1017. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  1018. /* mark the TC unallocated and halt'ed */
  1019. write_tc_c0_tcstatus(read_tc_c0_tcstatus() & ~TCSTATUS_A);
  1020. write_tc_c0_tchalt(TCHALT_H);
  1021. v->state = VPE_STATE_UNUSED;
  1022. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  1023. evpe(evpe_flags);
  1024. return 0;
  1025. }
  1026. EXPORT_SYMBOL(vpe_free);
  1027. void *vpe_get_shared(int index)
  1028. {
  1029. struct vpe *v;
  1030. if ((v = get_vpe(index)) == NULL)
  1031. return NULL;
  1032. return v->shared_ptr;
  1033. }
  1034. EXPORT_SYMBOL(vpe_get_shared);
  1035. int vpe_getuid(int index)
  1036. {
  1037. struct vpe *v;
  1038. if ((v = get_vpe(index)) == NULL)
  1039. return -1;
  1040. return v->uid;
  1041. }
  1042. EXPORT_SYMBOL(vpe_getuid);
  1043. int vpe_getgid(int index)
  1044. {
  1045. struct vpe *v;
  1046. if ((v = get_vpe(index)) == NULL)
  1047. return -1;
  1048. return v->gid;
  1049. }
  1050. EXPORT_SYMBOL(vpe_getgid);
  1051. int vpe_notify(int index, struct vpe_notifications *notify)
  1052. {
  1053. struct vpe *v;
  1054. if ((v = get_vpe(index)) == NULL)
  1055. return -1;
  1056. list_add(&notify->list, &v->notify);
  1057. return 0;
  1058. }
  1059. EXPORT_SYMBOL(vpe_notify);
  1060. char *vpe_getcwd(int index)
  1061. {
  1062. struct vpe *v;
  1063. if ((v = get_vpe(index)) == NULL)
  1064. return NULL;
  1065. return v->cwd;
  1066. }
  1067. EXPORT_SYMBOL(vpe_getcwd);
  1068. #ifdef CONFIG_MIPS_APSP_KSPD
  1069. static void kspd_sp_exit( int sp_id)
  1070. {
  1071. cleanup_tc(get_tc(sp_id));
  1072. }
  1073. #endif
  1074. static ssize_t store_kill(struct class_device *dev, const char *buf, size_t len)
  1075. {
  1076. struct vpe *vpe = get_vpe(tclimit);
  1077. struct vpe_notifications *not;
  1078. list_for_each_entry(not, &vpe->notify, list) {
  1079. not->stop(tclimit);
  1080. }
  1081. release_progmem(vpe->load_addr);
  1082. cleanup_tc(get_tc(tclimit));
  1083. vpe_stop(vpe);
  1084. vpe_free(vpe);
  1085. return len;
  1086. }
  1087. static ssize_t show_ntcs(struct class_device *cd, char *buf)
  1088. {
  1089. struct vpe *vpe = get_vpe(tclimit);
  1090. return sprintf(buf, "%d\n", vpe->ntcs);
  1091. }
  1092. static ssize_t store_ntcs(struct class_device *dev, const char *buf, size_t len)
  1093. {
  1094. struct vpe *vpe = get_vpe(tclimit);
  1095. unsigned long new;
  1096. char *endp;
  1097. new = simple_strtoul(buf, &endp, 0);
  1098. if (endp == buf)
  1099. goto out_einval;
  1100. if (new == 0 || new > (hw_tcs - tclimit))
  1101. goto out_einval;
  1102. vpe->ntcs = new;
  1103. return len;
  1104. out_einval:
  1105. return -EINVAL;;
  1106. }
  1107. static struct class_device_attribute vpe_class_attributes[] = {
  1108. __ATTR(kill, S_IWUSR, NULL, store_kill),
  1109. __ATTR(ntcs, S_IRUGO | S_IWUSR, show_ntcs, store_ntcs),
  1110. {}
  1111. };
  1112. static void vpe_class_device_release(struct class_device *cd)
  1113. {
  1114. kfree(cd);
  1115. }
  1116. struct class vpe_class = {
  1117. .name = "vpe",
  1118. .owner = THIS_MODULE,
  1119. .release = vpe_class_device_release,
  1120. .class_dev_attrs = vpe_class_attributes,
  1121. };
  1122. struct class_device vpe_device;
  1123. static int __init vpe_module_init(void)
  1124. {
  1125. unsigned int mtflags, vpflags;
  1126. unsigned long flags, val;
  1127. struct vpe *v = NULL;
  1128. struct tc *t;
  1129. int tc, err;
  1130. if (!cpu_has_mipsmt) {
  1131. printk("VPE loader: not a MIPS MT capable processor\n");
  1132. return -ENODEV;
  1133. }
  1134. if (vpelimit == 0) {
  1135. printk(KERN_WARNING "No VPEs reserved for AP/SP, not "
  1136. "initializing VPE loader.\nPass maxvpes=<n> argument as "
  1137. "kernel argument\n");
  1138. return -ENODEV;
  1139. }
  1140. if (tclimit == 0) {
  1141. printk(KERN_WARNING "No TCs reserved for AP/SP, not "
  1142. "initializing VPE loader.\nPass maxtcs=<n> argument as "
  1143. "kernel argument\n");
  1144. return -ENODEV;
  1145. }
  1146. major = register_chrdev(0, module_name, &vpe_fops);
  1147. if (major < 0) {
  1148. printk("VPE loader: unable to register character device\n");
  1149. return major;
  1150. }
  1151. err = class_register(&vpe_class);
  1152. if (err) {
  1153. printk(KERN_ERR "vpe_class registration failed\n");
  1154. goto out_chrdev;
  1155. }
  1156. class_device_initialize(&vpe_device);
  1157. vpe_device.class = &vpe_class,
  1158. vpe_device.parent = NULL,
  1159. strlcpy(vpe_device.class_id, "vpe1", BUS_ID_SIZE);
  1160. vpe_device.devt = MKDEV(major, minor);
  1161. err = class_device_add(&vpe_device);
  1162. if (err) {
  1163. printk(KERN_ERR "Adding vpe_device failed\n");
  1164. goto out_class;
  1165. }
  1166. local_irq_save(flags);
  1167. mtflags = dmt();
  1168. vpflags = dvpe();
  1169. /* Put MVPE's into 'configuration state' */
  1170. set_c0_mvpcontrol(MVPCONTROL_VPC);
  1171. /* dump_mtregs(); */
  1172. val = read_c0_mvpconf0();
  1173. hw_tcs = (val & MVPCONF0_PTC) + 1;
  1174. hw_vpes = ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
  1175. for (tc = tclimit; tc < hw_tcs; tc++) {
  1176. /*
  1177. * Must re-enable multithreading temporarily or in case we
  1178. * reschedule send IPIs or similar we might hang.
  1179. */
  1180. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  1181. evpe(vpflags);
  1182. emt(mtflags);
  1183. local_irq_restore(flags);
  1184. t = alloc_tc(tc);
  1185. if (!t) {
  1186. err = -ENOMEM;
  1187. goto out;
  1188. }
  1189. local_irq_save(flags);
  1190. mtflags = dmt();
  1191. vpflags = dvpe();
  1192. set_c0_mvpcontrol(MVPCONTROL_VPC);
  1193. /* VPE's */
  1194. if (tc < hw_tcs) {
  1195. settc(tc);
  1196. if ((v = alloc_vpe(tc)) == NULL) {
  1197. printk(KERN_WARNING "VPE: unable to allocate VPE\n");
  1198. goto out_reenable;
  1199. }
  1200. v->ntcs = hw_tcs - tclimit;
  1201. /* add the tc to the list of this vpe's tc's. */
  1202. list_add(&t->tc, &v->tc);
  1203. /* deactivate all but vpe0 */
  1204. if (tc >= tclimit) {
  1205. unsigned long tmp = read_vpe_c0_vpeconf0();
  1206. tmp &= ~VPECONF0_VPA;
  1207. /* master VPE */
  1208. tmp |= VPECONF0_MVP;
  1209. write_vpe_c0_vpeconf0(tmp);
  1210. }
  1211. /* disable multi-threading with TC's */
  1212. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
  1213. if (tc >= vpelimit) {
  1214. /*
  1215. * Set config to be the same as vpe0,
  1216. * particularly kseg0 coherency alg
  1217. */
  1218. write_vpe_c0_config(read_c0_config());
  1219. }
  1220. }
  1221. /* TC's */
  1222. t->pvpe = v; /* set the parent vpe */
  1223. if (tc >= tclimit) {
  1224. unsigned long tmp;
  1225. settc(tc);
  1226. /* Any TC that is bound to VPE0 gets left as is - in case
  1227. we are running SMTC on VPE0. A TC that is bound to any
  1228. other VPE gets bound to VPE0, ideally I'd like to make
  1229. it homeless but it doesn't appear to let me bind a TC
  1230. to a non-existent VPE. Which is perfectly reasonable.
  1231. The (un)bound state is visible to an EJTAG probe so may
  1232. notify GDB...
  1233. */
  1234. if (((tmp = read_tc_c0_tcbind()) & TCBIND_CURVPE)) {
  1235. /* tc is bound >vpe0 */
  1236. write_tc_c0_tcbind(tmp & ~TCBIND_CURVPE);
  1237. t->pvpe = get_vpe(0); /* set the parent vpe */
  1238. }
  1239. tmp = read_tc_c0_tcstatus();
  1240. /* mark not activated and not dynamically allocatable */
  1241. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  1242. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  1243. write_tc_c0_tcstatus(tmp);
  1244. write_tc_c0_tchalt(TCHALT_H);
  1245. }
  1246. }
  1247. out_reenable:
  1248. /* release config state */
  1249. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  1250. evpe(vpflags);
  1251. emt(mtflags);
  1252. local_irq_restore(flags);
  1253. #ifdef CONFIG_MIPS_APSP_KSPD
  1254. kspd_events.kspd_sp_exit = kspd_sp_exit;
  1255. #endif
  1256. return 0;
  1257. out_class:
  1258. class_unregister(&vpe_class);
  1259. out_chrdev:
  1260. unregister_chrdev(major, module_name);
  1261. out:
  1262. return err;
  1263. }
  1264. static void __exit vpe_module_exit(void)
  1265. {
  1266. struct vpe *v, *n;
  1267. list_for_each_entry_safe(v, n, &vpecontrol.vpe_list, list) {
  1268. if (v->state != VPE_STATE_UNUSED) {
  1269. release_vpe(v);
  1270. }
  1271. }
  1272. class_device_del(&vpe_device);
  1273. unregister_chrdev(major, module_name);
  1274. }
  1275. module_init(vpe_module_init);
  1276. module_exit(vpe_module_exit);
  1277. MODULE_DESCRIPTION("MIPS VPE Loader");
  1278. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  1279. MODULE_LICENSE("GPL");