vpe.c 37 KB

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