2 * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
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.
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
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.
22 * Provides support for loading a MIPS SP program on VPE1.
23 * The SP enviroment is rather simple, no tlb's. It needs to be relocatable
24 * (or partially linked). You should initialise your stack in the startup
25 * code. This loader looks for the symbol __start and sets up
26 * execution to resume from there. The MIPS SDE kit contains suitable examples.
28 * To load and run, simply cat a SP 'program file' to /dev/vpe1.
29 * i.e cat spapp >/dev/vpe1.
31 * You'll need to have the following device files.
32 * mknod /dev/vpe0 c 63 0
33 * mknod /dev/vpe1 c 63 1
36 #include <linux/kernel.h>
37 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <asm/uaccess.h>
41 #include <linux/slab.h>
42 #include <linux/list.h>
43 #include <linux/vmalloc.h>
44 #include <linux/elf.h>
45 #include <linux/seq_file.h>
46 #include <linux/syscalls.h>
47 #include <linux/moduleloader.h>
48 #include <linux/interrupt.h>
49 #include <linux/poll.h>
50 #include <linux/bootmem.h>
51 #include <asm/mipsregs.h>
52 #include <asm/cacheflush.h>
53 #include <asm/atomic.h>
55 #include <asm/processor.h>
56 #include <asm/system.h>
58 typedef void *vpe_handle;
60 // defined here because the kernel module loader doesn't have
61 // anything to do with it.
62 #define SHN_MIPS_SCOMMON 0xff03
64 #ifndef ARCH_SHF_SMALL
65 #define ARCH_SHF_SMALL 0
68 /* If this is set, the section belongs in the init part of the module */
69 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74 static char module_name[] = "vpe";
77 /* grab the likely amount of memory we will need. */
78 #ifdef CONFIG_MIPS_VPE_LOADER_TOM
79 #define P_SIZE (2 * 1024 * 1024)
81 /* add an overhead to the max kmalloc size for non-striped symbols/etc */
82 #define P_SIZE (256 * 1024)
108 /* The list of TC's with this VPE */
111 /* The global list of tc's */
112 struct list_head list;
116 enum vpe_state state;
118 /* (device) minor associated with this vpe */
121 /* elfloader stuff */
127 unsigned long __start;
129 /* tc's associated with this vpe */
132 /* The list of vpe's */
133 struct list_head list;
135 /* shared symbol address */
140 /* Virtual processing elements */
141 struct list_head vpe_list;
143 /* Thread contexts */
144 struct list_head tc_list;
147 static void release_progmem(void *ptr);
148 static void dump_vpe(vpe_t * v);
149 extern void save_gp_address(unsigned int secbase, unsigned int rel);
151 /* get the vpe associated with this minor */
152 struct vpe *get_vpe(int minor)
156 list_for_each_entry(v, &vpecontrol.vpe_list, list) {
157 if (v->minor == minor)
161 printk(KERN_DEBUG "VPE: get_vpe minor %d not found\n", minor);
165 /* get the vpe associated with this minor */
166 struct tc *get_tc(int index)
170 list_for_each_entry(t, &vpecontrol.tc_list, list) {
171 if (t->index == index)
175 printk(KERN_DEBUG "VPE: get_tc index %d not found\n", index);
180 struct tc *get_tc_unused(void)
184 list_for_each_entry(t, &vpecontrol.tc_list, list) {
185 if (t->state == TC_STATE_UNUSED)
189 printk(KERN_DEBUG "VPE: All TC's are in use\n");
194 /* allocate a vpe and associate it with this minor (or index) */
195 struct vpe *alloc_vpe(int minor)
199 if ((v = kmalloc(sizeof(struct vpe), GFP_KERNEL)) == NULL) {
200 printk(KERN_WARNING "VPE: alloc_vpe no mem\n");
204 memset(v, 0, sizeof(struct vpe));
206 INIT_LIST_HEAD(&v->tc);
207 list_add_tail(&v->list, &vpecontrol.vpe_list);
213 /* allocate a tc. At startup only tc0 is running, all other can be halted. */
214 struct tc *alloc_tc(int index)
218 if ((t = kmalloc(sizeof(struct tc), GFP_KERNEL)) == NULL) {
219 printk(KERN_WARNING "VPE: alloc_tc no mem\n");
223 memset(t, 0, sizeof(struct tc));
225 INIT_LIST_HEAD(&t->tc);
226 list_add_tail(&t->list, &vpecontrol.tc_list);
233 /* clean up and free everything */
234 void release_vpe(struct vpe *v)
242 void dump_mtregs(void)
246 val = read_c0_config3();
247 printk("config3 0x%lx MT %ld\n", val,
248 (val & CONFIG3_MT) >> CONFIG3_MT_SHIFT);
250 val = read_c0_mvpconf0();
251 printk("mvpconf0 0x%lx, PVPE %ld PTC %ld M %ld\n", val,
252 (val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT,
253 val & MVPCONF0_PTC, (val & MVPCONF0_M) >> MVPCONF0_M_SHIFT);
255 val = read_c0_mvpcontrol();
256 printk("MVPControl 0x%lx, STLB %ld VPC %ld EVP %ld\n", val,
257 (val & MVPCONTROL_STLB) >> MVPCONTROL_STLB_SHIFT,
258 (val & MVPCONTROL_VPC) >> MVPCONTROL_VPC_SHIFT,
259 (val & MVPCONTROL_EVP));
261 val = read_c0_vpeconf0();
262 printk("VPEConf0 0x%lx MVP %ld\n", val,
263 (val & VPECONF0_MVP) >> VPECONF0_MVP_SHIFT);
266 /* Find some VPE program space */
267 static void *alloc_progmem(u32 len)
269 #ifdef CONFIG_MIPS_VPE_LOADER_TOM
270 /* this means you must tell linux to use less memory than you physically have */
271 return (void *)((max_pfn * PAGE_SIZE) + KSEG0);
273 // simple grab some mem for now
274 return kmalloc(len, GFP_KERNEL);
278 static void release_progmem(void *ptr)
280 #ifndef CONFIG_MIPS_VPE_LOADER_TOM
285 /* Update size with this section: return offset. */
286 static long get_offset(unsigned long *size, Elf_Shdr * sechdr)
290 ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
291 *size = ret + sechdr->sh_size;
295 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
296 might -- code, read-only data, read-write data, small data. Tally
297 sizes, and place the offsets into sh_entsize fields: high bit means it
299 static void layout_sections(struct module *mod, const Elf_Ehdr * hdr,
300 Elf_Shdr * sechdrs, const char *secstrings)
302 static unsigned long const masks[][2] = {
303 /* NOTE: all executable code must be the first section
304 * in this array; otherwise modify the text_size
305 * finder in the two loops below */
306 {SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
307 {SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
308 {SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
309 {ARCH_SHF_SMALL | SHF_ALLOC, 0}
313 for (i = 0; i < hdr->e_shnum; i++)
314 sechdrs[i].sh_entsize = ~0UL;
316 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
317 for (i = 0; i < hdr->e_shnum; ++i) {
318 Elf_Shdr *s = &sechdrs[i];
320 // || strncmp(secstrings + s->sh_name, ".init", 5) == 0)
321 if ((s->sh_flags & masks[m][0]) != masks[m][0]
322 || (s->sh_flags & masks[m][1])
323 || s->sh_entsize != ~0UL)
325 s->sh_entsize = get_offset(&mod->core_size, s);
329 mod->core_text_size = mod->core_size;
335 /* from module-elf32.c, but subverted a little */
338 struct mips_hi16 *next;
343 static struct mips_hi16 *mips_hi16_list;
344 static unsigned int gp_offs, gp_addr;
346 static int apply_r_mips_none(struct module *me, uint32_t *location,
352 static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
357 if( !(*location & 0xffff) ) {
358 rel = (int)v - gp_addr;
361 /* .sbss + gp(relative) + offset */
363 rel = (int)(short)((int)v + gp_offs +
364 (int)(short)(*location & 0xffff) - gp_addr);
367 if( (rel > 32768) || (rel < -32768) ) {
369 "apply_r_mips_gprel16: relative address out of range 0x%x %d\n",
374 *location = (*location & 0xffff0000) | (rel & 0xffff);
379 static int apply_r_mips_pc16(struct module *me, uint32_t *location,
383 rel = (((unsigned int)v - (unsigned int)location));
384 rel >>= 2; // because the offset is in _instructions_ not bytes.
385 rel -= 1; // and one instruction less due to the branch delay slot.
387 if( (rel > 32768) || (rel < -32768) ) {
389 "apply_r_mips_pc16: relative address out of range 0x%x\n", rel);
393 *location = (*location & 0xffff0000) | (rel & 0xffff);
398 static int apply_r_mips_32(struct module *me, uint32_t *location,
406 static int apply_r_mips_26(struct module *me, uint32_t *location,
410 printk(KERN_ERR "module %s: dangerous relocation mod4\n", me->name);
414 /* Not desperately convinced this is a good check of an overflow condition
415 anyway. But it gets in the way of handling undefined weak symbols which
416 we want to set to zero.
417 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
419 "module %s: relocation overflow\n",
425 *location = (*location & ~0x03ffffff) |
426 ((*location + (v >> 2)) & 0x03ffffff);
430 static int apply_r_mips_hi16(struct module *me, uint32_t *location,
436 * We cannot relocate this one now because we don't know the value of
437 * the carry we need to add. Save the information, and let LO16 do the
440 n = kmalloc(sizeof *n, GFP_KERNEL);
446 n->next = mips_hi16_list;
452 static int apply_r_mips_lo16(struct module *me, uint32_t *location,
455 unsigned long insnlo = *location;
456 Elf32_Addr val, vallo;
458 /* Sign extend the addend we extract from the lo insn. */
459 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
461 if (mips_hi16_list != NULL) {
466 struct mips_hi16 *next;
470 * The value for the HI16 had best be the same.
473 printk("%d != %d\n", v, l->value);
479 * Do the HI16 relocation. Note that we actually don't
480 * need to know anything about the LO16 itself, except
481 * where to find the low 16 bits of the addend needed
485 val = ((insn & 0xffff) << 16) + vallo;
489 * Account for the sign extension that will happen in
492 val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
494 insn = (insn & ~0xffff) | val;
502 mips_hi16_list = NULL;
506 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
509 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
515 printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
520 static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
522 [R_MIPS_NONE] = apply_r_mips_none,
523 [R_MIPS_32] = apply_r_mips_32,
524 [R_MIPS_26] = apply_r_mips_26,
525 [R_MIPS_HI16] = apply_r_mips_hi16,
526 [R_MIPS_LO16] = apply_r_mips_lo16,
527 [R_MIPS_GPREL16] = apply_r_mips_gprel16,
528 [R_MIPS_PC16] = apply_r_mips_pc16
532 int apply_relocations(Elf32_Shdr *sechdrs,
534 unsigned int symindex,
538 Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
545 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
546 Elf32_Word r_info = rel[i].r_info;
548 /* This is where to make the change */
549 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
551 /* This is the symbol it is referring to */
552 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
553 + ELF32_R_SYM(r_info);
555 if (!sym->st_value) {
556 printk(KERN_DEBUG "%s: undefined weak symbol %s\n",
557 me->name, strtab + sym->st_name);
558 /* just print the warning, dont barf */
563 res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
566 "relocation error 0x%x sym refer <%s> value 0x%x "
567 "type 0x%x r_info 0x%x\n",
568 (unsigned int)location, strtab + sym->st_name, v,
569 r_info, ELF32_R_TYPE(r_info));
579 void save_gp_address(unsigned int secbase, unsigned int rel)
581 gp_addr = secbase + rel;
582 gp_offs = gp_addr - (secbase & 0xffff0000);
584 /* end module-elf32.c */
588 /* Change all symbols so that sh_value encodes the pointer directly. */
589 static int simplify_symbols(Elf_Shdr * sechdrs,
590 unsigned int symindex,
592 const char *secstrings,
593 unsigned int nsecs, struct module *mod)
595 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
596 unsigned long secbase, bssbase = 0;
597 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
600 /* find the .bss section for COMMON symbols */
601 for (i = 0; i < nsecs; i++) {
602 if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0)
603 bssbase = sechdrs[i].sh_addr;
606 for (i = 1; i < n; i++) {
607 switch (sym[i].st_shndx) {
609 /* Allocate space for the symbol in the .bss section. st_value is currently size.
610 We want it to have the address of the symbol. */
612 size = sym[i].st_value;
613 sym[i].st_value = bssbase;
619 /* Don't need to do anything */
626 case SHN_MIPS_SCOMMON:
629 "simplify_symbols: ignoring SHN_MIPS_SCOMMON symbol <%s> st_shndx %d\n",
630 strtab + sym[i].st_name, sym[i].st_shndx);
636 secbase = sechdrs[sym[i].st_shndx].sh_addr;
638 if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0) {
639 save_gp_address(secbase, sym[i].st_value);
642 sym[i].st_value += secbase;
651 #ifdef DEBUG_ELFLOADER
652 static void dump_elfsymbols(Elf_Shdr * sechdrs, unsigned int symindex,
653 const char *strtab, struct module *mod)
655 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
656 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
658 printk(KERN_DEBUG "dump_elfsymbols: n %d\n", n);
659 for (i = 1; i < n; i++) {
660 printk(KERN_DEBUG " i %d name <%s> 0x%x\n", i,
661 strtab + sym[i].st_name, sym[i].st_value);
666 static void dump_tc(struct tc *t)
668 printk(KERN_WARNING "VPE: TC index %d TCStatus 0x%lx halt 0x%lx\n",
669 t->index, read_tc_c0_tcstatus(), read_tc_c0_tchalt());
670 printk(KERN_WARNING "VPE: tcrestart 0x%lx\n", read_tc_c0_tcrestart());
673 static void dump_tclist(void)
677 list_for_each_entry(t, &vpecontrol.tc_list, list) {
682 /* We are prepared so configure and start the VPE... */
683 int vpe_run(vpe_t * v)
688 /* check we are the Master VPE */
689 val = read_c0_vpeconf0();
690 if (!(val & VPECONF0_MVP)) {
692 "VPE: only Master VPE's are allowed to configure MT\n");
696 /* disable MT (using dvpe) */
699 /* Put MVPE's into 'configuration state' */
700 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_VPC);
702 if (!list_empty(&v->tc)) {
703 if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
704 printk(KERN_WARNING "VPE: TC %d is already in use.\n",
709 printk(KERN_WARNING "VPE: No TC's associated with VPE %d\n",
716 val = read_vpe_c0_vpeconf0();
718 /* should check it is halted, and not activated */
719 if ((read_tc_c0_tcstatus() & TCSTATUS_A) || !(read_tc_c0_tchalt() & TCHALT_H)) {
720 printk(KERN_WARNING "VPE: TC %d is already doing something!\n",
727 /* Write the address we want it to start running from in the TCPC register. */
728 write_tc_c0_tcrestart((unsigned long)v->__start);
730 /* write the sivc_info address to tccontext */
731 write_tc_c0_tccontext((unsigned long)0);
733 /* Set up the XTC bit in vpeconf0 to point at our tc */
734 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | (t->index << VPECONF0_XTC_SHIFT));
736 /* mark the TC as activated, not interrupt exempt and not dynamically allocatable */
737 val = read_tc_c0_tcstatus();
738 val = (val & ~(TCSTATUS_DA | TCSTATUS_IXMT)) | TCSTATUS_A;
739 write_tc_c0_tcstatus(val);
741 write_tc_c0_tchalt(read_tc_c0_tchalt() & ~TCHALT_H);
744 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE); // no multiple TC's
745 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA); // enable this VPE
748 * The sde-kit passes 'memsize' to __start in $a3, so set something
750 * Or set $a3 (register 7) to zero and define DFLT_STACK_SIZE and
751 * DFLT_HEAP_SIZE when you compile your program
756 /* set config to be the same as vpe0, particularly kseg0 coherency alg */
757 write_vpe_c0_config(read_c0_config());
759 /* clear out any left overs from a previous program */
760 write_vpe_c0_cause(0);
762 /* take system out of configuration state */
763 write_c0_mvpcontrol(read_c0_mvpcontrol() & ~MVPCONTROL_VPC);
765 /* clear interrupts enabled IE, ERL, EXL, and KSU from c0 status */
766 write_vpe_c0_status(read_vpe_c0_status() & ~(ST0_ERL | ST0_KSU | ST0_IE | ST0_EXL));
774 static unsigned long find_vpe_symbols(vpe_t * v, Elf_Shdr * sechdrs,
775 unsigned int symindex, const char *strtab,
778 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
779 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
781 for (i = 1; i < n; i++) {
782 if (strcmp(strtab + sym[i].st_name, "__start") == 0) {
783 v->__start = sym[i].st_value;
786 if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0) {
787 v->shared_ptr = (void *)sym[i].st_value;
794 /* Allocates a VPE with some program code space(the load address), copies the contents
795 of the program (p)buffer performing relocatations/etc, free's it when finished.
797 int vpe_elfload(vpe_t * v)
802 char *secstrings, *strtab = NULL;
803 unsigned int len, i, symindex = 0, strindex = 0;
805 struct module mod; // so we can re-use the relocations code
807 memset(&mod, 0, sizeof(struct module));
808 strcpy(mod.name, "VPE dummy prog module");
810 hdr = (Elf_Ehdr *) v->pbuffer;
813 /* Sanity checks against insmoding binaries or wrong arch,
815 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
816 || hdr->e_type != ET_REL || !elf_check_arch(hdr)
817 || hdr->e_shentsize != sizeof(*sechdrs)) {
819 "VPE program, wrong arch or weird elf version\n");
824 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
825 printk(KERN_ERR "VPE program length %u truncated\n", len);
829 /* Convenience variables */
830 sechdrs = (void *)hdr + hdr->e_shoff;
831 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
832 sechdrs[0].sh_addr = 0;
834 /* And these should exist, but gcc whinges if we don't init them */
835 symindex = strindex = 0;
837 for (i = 1; i < hdr->e_shnum; i++) {
839 if (sechdrs[i].sh_type != SHT_NOBITS
840 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) {
841 printk(KERN_ERR "VPE program length %u truncated\n",
846 /* Mark all sections sh_addr with their address in the
848 sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
850 /* Internal symbols and strings. */
851 if (sechdrs[i].sh_type == SHT_SYMTAB) {
853 strindex = sechdrs[i].sh_link;
854 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
858 layout_sections(&mod, hdr, sechdrs, secstrings);
860 v->load_addr = alloc_progmem(mod.core_size);
861 memset(v->load_addr, 0, mod.core_size);
863 printk("VPE elf_loader: loading to %p\n", v->load_addr);
865 for (i = 0; i < hdr->e_shnum; i++) {
868 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
871 dest = v->load_addr + sechdrs[i].sh_entsize;
873 if (sechdrs[i].sh_type != SHT_NOBITS)
874 memcpy(dest, (void *)sechdrs[i].sh_addr,
876 /* Update sh_addr to point to copy in image. */
877 sechdrs[i].sh_addr = (unsigned long)dest;
880 /* Fix up syms, so that st_value is a pointer to location. */
882 simplify_symbols(sechdrs, symindex, strtab, secstrings,
885 printk(KERN_WARNING "VPE: unable to simplify symbols\n");
889 /* Now do relocations. */
890 for (i = 1; i < hdr->e_shnum; i++) {
891 const char *strtab = (char *)sechdrs[strindex].sh_addr;
892 unsigned int info = sechdrs[i].sh_info;
894 /* Not a valid relocation section? */
895 if (info >= hdr->e_shnum)
898 /* Don't bother with non-allocated sections */
899 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
902 if (sechdrs[i].sh_type == SHT_REL)
904 apply_relocations(sechdrs, strtab, symindex, i, &mod);
905 else if (sechdrs[i].sh_type == SHT_RELA)
906 err = apply_relocate_add(sechdrs, strtab, symindex, i,
910 "vpe_elfload: error in relocations err %ld\n",
916 /* make sure it's physically written out */
917 flush_icache_range((unsigned long)v->load_addr,
918 (unsigned long)v->load_addr + v->len);
920 if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
923 "VPE: program doesn't contain __start or vpe_shared symbols\n");
927 printk(" elf loaded\n");
933 static void dump_vpe(vpe_t * v)
937 printk(KERN_DEBUG "VPEControl 0x%lx\n", read_vpe_c0_vpecontrol());
938 printk(KERN_DEBUG "VPEConf0 0x%lx\n", read_vpe_c0_vpeconf0());
940 list_for_each_entry(t, &vpecontrol.tc_list, list) {
945 /* checks for VPE is unused and gets ready to load program */
946 static int vpe_open(struct inode *inode, struct file *filp)
951 /* assume only 1 device at the mo. */
952 if ((minor = MINOR(inode->i_rdev)) != 1) {
953 printk(KERN_WARNING "VPE: only vpe1 is supported\n");
957 if ((v = get_vpe(minor)) == NULL) {
958 printk(KERN_WARNING "VPE: unable to get vpe\n");
962 if (v->state != VPE_STATE_UNUSED) {
966 printk(KERN_WARNING "VPE: device %d already in use\n", minor);
971 printk(KERN_WARNING "VPE: re-initialising %d\n", minor);
973 release_progmem(v->load_addr);
977 tmp = read_tc_c0_tcstatus();
979 /* mark not allocated and not dynamically allocatable */
980 tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
981 tmp |= TCSTATUS_IXMT; /* interrupt exempt */
982 write_tc_c0_tcstatus(tmp);
984 write_tc_c0_tchalt(TCHALT_H);
988 // allocate it so when we get write ops we know it's expected.
989 v->state = VPE_STATE_INUSE;
991 /* this of-course trashes what was there before... */
992 v->pbuffer = vmalloc(P_SIZE);
1000 static int vpe_release(struct inode *inode, struct file *filp)
1006 minor = MINOR(inode->i_rdev);
1007 if ((v = get_vpe(minor)) == NULL)
1010 // simple case of fire and forget, so tell the VPE to run...
1012 hdr = (Elf_Ehdr *) v->pbuffer;
1013 if (memcmp(hdr->e_ident, ELFMAG, 4) == 0) {
1014 if (vpe_elfload(v) >= 0)
1017 printk(KERN_WARNING "VPE: ELF load failed.\n");
1021 printk(KERN_WARNING "VPE: only elf files are supported\n");
1025 // cleanup any temp buffers
1032 static ssize_t vpe_write(struct file *file, const char __user * buffer,
1033 size_t count, loff_t * ppos)
1039 minor = MINOR(file->f_dentry->d_inode->i_rdev);
1040 if ((v = get_vpe(minor)) == NULL)
1043 if (v->pbuffer == NULL) {
1044 printk(KERN_ERR "vpe_write: no pbuffer\n");
1048 if ((count + v->len) > v->plen) {
1050 "VPE Loader: elf size too big. Perhaps strip uneeded symbols\n");
1054 count -= copy_from_user(v->pbuffer + v->len, buffer, count);
1056 printk("vpe_write: copy_to_user failed\n");
1064 static struct file_operations vpe_fops = {
1065 .owner = THIS_MODULE,
1067 .release = vpe_release,
1071 /* module wrapper entry points */
1073 vpe_handle vpe_alloc(void)
1079 for (i = 1; i < MAX_VPES; i++) {
1080 if ((v = get_vpe(i)) != NULL) {
1081 v->state = VPE_STATE_INUSE;
1088 EXPORT_SYMBOL(vpe_alloc);
1090 /* start running from here */
1091 int vpe_start(vpe_handle vpe, unsigned long start)
1093 struct vpe *v = vpe;
1099 EXPORT_SYMBOL(vpe_start);
1101 /* halt it for now */
1102 int vpe_stop(vpe_handle vpe)
1104 struct vpe *v = vpe;
1106 unsigned int evpe_flags;
1108 evpe_flags = dvpe();
1110 if ((t = list_entry(v->tc.next, struct tc, tc)) != NULL) {
1113 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
1121 EXPORT_SYMBOL(vpe_stop);
1123 /* I've done with it thank you */
1124 int vpe_free(vpe_handle vpe)
1126 struct vpe *v = vpe;
1128 unsigned int evpe_flags;
1130 if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
1134 evpe_flags = dvpe();
1136 /* Put MVPE's into 'configuration state' */
1137 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_VPC);
1140 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
1142 /* mark the TC unallocated and halt'ed */
1143 write_tc_c0_tcstatus(read_tc_c0_tcstatus() & ~TCSTATUS_A);
1144 write_tc_c0_tchalt(TCHALT_H);
1146 v->state = VPE_STATE_UNUSED;
1148 write_c0_mvpcontrol(read_c0_mvpcontrol() & ~MVPCONTROL_VPC);
1154 EXPORT_SYMBOL(vpe_free);
1156 void *vpe_get_shared(int index)
1160 if ((v = get_vpe(index)) == NULL) {
1161 printk(KERN_WARNING "vpe: invalid vpe index %d\n", index);
1165 return v->shared_ptr;
1168 EXPORT_SYMBOL(vpe_get_shared);
1170 static int __init vpe_module_init(void)
1172 struct vpe *v = NULL;
1177 if (!cpu_has_mipsmt) {
1178 printk("VPE loader: not a MIPS MT capable processor\n");
1182 if ((major = register_chrdev(VPE_MAJOR, module_name, &vpe_fops) < 0)) {
1183 printk("VPE loader: unable to register character device\n");
1193 /* Put MVPE's into 'configuration state' */
1194 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_VPC);
1196 /* dump_mtregs(); */
1198 INIT_LIST_HEAD(&vpecontrol.vpe_list);
1199 INIT_LIST_HEAD(&vpecontrol.tc_list);
1201 val = read_c0_mvpconf0();
1202 for (i = 0; i < ((val & MVPCONF0_PTC) + 1); i++) {
1206 if (i < ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1) {
1209 if ((v = alloc_vpe(i)) == NULL) {
1210 printk(KERN_WARNING "VPE: unable to allocate VPE\n");
1214 list_add(&t->tc, &v->tc); /* add the tc to the list of this vpe's tc's. */
1216 /* deactivate all but vpe0 */
1218 unsigned long tmp = read_vpe_c0_vpeconf0();
1220 tmp &= ~VPECONF0_VPA;
1223 tmp |= VPECONF0_MVP;
1224 write_vpe_c0_vpeconf0(tmp);
1227 /* disable multi-threading with TC's */
1228 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
1231 write_vpe_c0_status((read_c0_status() &
1232 ~(ST0_IM | ST0_IE | ST0_KSU))
1235 /* set config to be the same as vpe0, particularly kseg0 coherency alg */
1236 write_vpe_c0_config(read_c0_config());
1242 t->pvpe = v; /* set the parent vpe */
1247 /* tc 0 will of course be running.... */
1249 t->state = TC_STATE_RUNNING;
1253 /* bind a TC to each VPE, May as well put all excess TC's
1255 if (i >= (((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1))
1256 write_tc_c0_tcbind(read_tc_c0_tcbind() |
1257 ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT));
1259 write_tc_c0_tcbind(read_tc_c0_tcbind() | i);
1261 tmp = read_tc_c0_tcstatus();
1263 /* mark not allocated and not dynamically allocatable */
1264 tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
1265 tmp |= TCSTATUS_IXMT; /* interrupt exempt */
1266 write_tc_c0_tcstatus(tmp);
1268 write_tc_c0_tchalt(TCHALT_H);
1272 /* release config state */
1273 write_c0_mvpcontrol(read_c0_mvpcontrol() & ~MVPCONTROL_VPC);
1278 static void __exit vpe_module_exit(void)
1282 list_for_each_entry_safe(v, n, &vpecontrol.vpe_list, list) {
1283 if (v->state != VPE_STATE_UNUSED) {
1288 unregister_chrdev(major, module_name);
1291 module_init(vpe_module_init);
1292 module_exit(vpe_module_exit);
1293 MODULE_DESCRIPTION("MIPS VPE Loader");
1294 MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc");
1295 MODULE_LICENSE("GPL");