vpe.c 36 KB

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