vpe.c 36 KB

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