vpe.c 36 KB

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