vpe.c 37 KB

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