vpe.c 36 KB

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