]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - arch/blackfin/kernel/module.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[mv-sheeva.git] / arch / blackfin / kernel / module.c
index 50d7fbfe1ce62a7ddfcdbecfea8b9a52091520fe..67fc7a56c865211cef10d339f0dc4fecac696336 100644 (file)
@@ -37,6 +37,7 @@
 #include <linux/kernel.h>
 #include <asm/dma.h>
 #include <asm/cacheflush.h>
+#include <asm/uaccess.h>
 
 void *module_alloc(unsigned long size)
 {
@@ -53,7 +54,7 @@ void module_free(struct module *mod, void *module_region)
 
 /* Transfer the section to the L1 memory */
 int
-module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
+module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
                          char *secstrings, struct module *mod)
 {
        /*
@@ -64,12 +65,18 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
         * NOTE: this breaks the semantic of mod->arch structure.
         */
        Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
-       void *dest = NULL;
+       void *dest;
 
        for (s = sechdrs; s < sechdrs_end; ++s) {
-               if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) ||
-                   ((strcmp(".text", secstrings + s->sh_name) == 0) &&
-                    (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) {
+               const char *shname = secstrings + s->sh_name;
+
+               if (s->sh_size == 0)
+                       continue;
+
+               if (!strcmp(".l1.text", shname) ||
+                   (!strcmp(".text", shname) &&
+                    (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
+
                        dest = l1_inst_sram_alloc(s->sh_size);
                        mod->arch.text_l1 = dest;
                        if (dest == NULL) {
@@ -78,12 +85,11 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                return -1;
                        }
                        dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
-               if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
-                   ((strcmp(".data", secstrings + s->sh_name) == 0) &&
-                    (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
+
+               } else if (!strcmp(".l1.data", shname) ||
+                          (!strcmp(".data", shname) &&
+                           (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
+
                        dest = l1_data_sram_alloc(s->sh_size);
                        mod->arch.data_a_l1 = dest;
                        if (dest == NULL) {
@@ -92,12 +98,11 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                return -1;
                        }
                        memcpy(dest, (void *)s->sh_addr, s->sh_size);
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
-               if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 ||
-                   ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
-                    (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
+
+               } else if (!strcmp(".l1.bss", shname) ||
+                          (!strcmp(".bss", shname) &&
+                           (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
+
                        dest = l1_data_sram_zalloc(s->sh_size);
                        mod->arch.bss_a_l1 = dest;
                        if (dest == NULL) {
@@ -105,10 +110,9 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                        mod->name);
                                return -1;
                        }
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
-               if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
+
+               } else if (!strcmp(".l1.data.B", shname)) {
+
                        dest = l1_data_B_sram_alloc(s->sh_size);
                        mod->arch.data_b_l1 = dest;
                        if (dest == NULL) {
@@ -117,10 +121,9 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                return -1;
                        }
                        memcpy(dest, (void *)s->sh_addr, s->sh_size);
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
-               if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
+
+               } else if (!strcmp(".l1.bss.B", shname)) {
+
                        dest = l1_data_B_sram_alloc(s->sh_size);
                        mod->arch.bss_b_l1 = dest;
                        if (dest == NULL) {
@@ -129,12 +132,11 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                return -1;
                        }
                        memset(dest, 0, s->sh_size);
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
-               if ((strcmp(".l2.text", secstrings + s->sh_name) == 0) ||
-                   ((strcmp(".text", secstrings + s->sh_name) == 0) &&
-                    (hdr->e_flags & EF_BFIN_CODE_IN_L2) && (s->sh_size > 0))) {
+
+               } else if (!strcmp(".l2.text", shname) ||
+                          (!strcmp(".text", shname) &&
+                           (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
+
                        dest = l2_sram_alloc(s->sh_size);
                        mod->arch.text_l2 = dest;
                        if (dest == NULL) {
@@ -143,12 +145,11 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                return -1;
                        }
                        memcpy(dest, (void *)s->sh_addr, s->sh_size);
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
-               if ((strcmp(".l2.data", secstrings + s->sh_name) == 0) ||
-                   ((strcmp(".data", secstrings + s->sh_name) == 0) &&
-                    (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
+
+               } else if (!strcmp(".l2.data", shname) ||
+                          (!strcmp(".data", shname) &&
+                           (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
+
                        dest = l2_sram_alloc(s->sh_size);
                        mod->arch.data_l2 = dest;
                        if (dest == NULL) {
@@ -157,12 +158,11 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                return -1;
                        }
                        memcpy(dest, (void *)s->sh_addr, s->sh_size);
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
-               if (strcmp(".l2.bss", secstrings + s->sh_name) == 0 ||
-                   ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
-                    (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
+
+               } else if (!strcmp(".l2.bss", shname) ||
+                          (!strcmp(".bss", shname) &&
+                           (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
+
                        dest = l2_sram_zalloc(s->sh_size);
                        mod->arch.bss_l2 = dest;
                        if (dest == NULL) {
@@ -170,10 +170,14 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
                                        mod->name);
                                return -1;
                        }
-                       s->sh_flags &= ~SHF_ALLOC;
-                       s->sh_addr = (unsigned long)dest;
-               }
+
+               } else
+                       continue;
+
+               s->sh_flags &= ~SHF_ALLOC;
+               s->sh_addr = (unsigned long)dest;
        }
+
        return 0;
 }
 
@@ -196,26 +200,23 @@ apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
 /*            gas does not generate it.                                  */
 /*************************************************************************/
 int
-apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
+apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
                   unsigned int symindex, unsigned int relsec,
                   struct module *mod)
 {
        unsigned int i;
-       unsigned short tmp;
        Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
        Elf32_Sym *sym;
-       uint32_t *location32;
-       uint16_t *location16;
-       uint32_t value;
+       unsigned long location, value, size;
 
        pr_debug("applying relocate section %u to %u\n", mod->name,
                relsec, sechdrs[relsec].sh_info);
+
        for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
                /* This is where to make the change */
-               location16 =
-                   (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
-                                 rel[i].r_offset);
-               location32 = (uint32_t *) location16;
+               location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
+                          rel[i].r_offset;
+
                /* This is the symbol it is referring to. Note that all
                   undefined symbols have been resolved. */
                sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
@@ -224,39 +225,28 @@ apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
                value += rel[i].r_addend;
 
 #ifdef CONFIG_SMP
-               if ((unsigned long)location16 >= COREB_L1_DATA_A_START) {
+               if (location >= COREB_L1_DATA_A_START) {
                        pr_err("cannot relocate in L1: %u (SMP kernel)",
                                mod->name, ELF32_R_TYPE(rel[i].r_info));
                        return -ENOEXEC;
                }
 #endif
 
-               pr_debug("location is %lx, value is %x type is %d\n",
-                       mod->name, (unsigned long)location32, value,
-                       ELF32_R_TYPE(rel[i].r_info));
+               pr_debug("location is %lx, value is %lx type is %d\n",
+                       mod->name, location, value, ELF32_R_TYPE(rel[i].r_info));
 
                switch (ELF32_R_TYPE(rel[i].r_info)) {
 
-               case R_BFIN_LUIMM16:
-                       tmp = (value & 0xffff);
-                       if ((unsigned long)location16 >= L1_CODE_START) {
-                               dma_memcpy(location16, &tmp, 2);
-                       } else
-                               *location16 = tmp;
-                       break;
                case R_BFIN_HUIMM16:
-                       tmp = ((value >> 16) & 0xffff);
-                       if ((unsigned long)location16 >= L1_CODE_START) {
-                               dma_memcpy(location16, &tmp, 2);
-                       } else
-                               *location16 = tmp;
-                       break;
+                       value >>= 16;
+               case R_BFIN_LUIMM16:
                case R_BFIN_RIMM16:
-                       *location16 = (value & 0xffff);
+                       size = 2;
                        break;
                case R_BFIN_BYTE4_DATA:
-                       *location32 = value;
+                       size = 4;
                        break;
+
                case R_BFIN_PCREL24:
                case R_BFIN_PCREL24_JUMP_L:
                case R_BFIN_PCREL12_JUMP:
@@ -265,12 +255,31 @@ apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
                        pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
                                mod->name, ELF32_R_TYPE(rel[i].r_info));
                        return -ENOEXEC;
+
                default:
                        pr_err("unknown relocation: %u\n", mod->name,
                                ELF32_R_TYPE(rel[i].r_info));
                        return -ENOEXEC;
                }
+
+               switch (bfin_mem_access_type(location, size)) {
+               case BFIN_MEM_ACCESS_CORE:
+               case BFIN_MEM_ACCESS_CORE_ONLY:
+                       memcpy((void *)location, &value, size);
+                       break;
+               case BFIN_MEM_ACCESS_DMA:
+                       dma_memcpy((void *)location, &value, size);
+                       break;
+               case BFIN_MEM_ACCESS_ITEST:
+                       isram_memcpy((void *)location, &value, size);
+                       break;
+               default:
+                       pr_err("invalid relocation for %#lx\n",
+                               mod->name, location);
+                       return -ENOEXEC;
+               }
        }
+
        return 0;
 }
 
@@ -295,22 +304,28 @@ module_finalize(const Elf_Ehdr * hdr,
        for (i = 1; i < hdr->e_shnum; i++) {
                const char *strtab = (char *)sechdrs[strindex].sh_addr;
                unsigned int info = sechdrs[i].sh_info;
+               const char *shname = secstrings + sechdrs[i].sh_name;
 
                /* Not a valid relocation section? */
                if (info >= hdr->e_shnum)
                        continue;
 
-               if ((sechdrs[i].sh_type == SHT_RELA) &&
-                   ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) ||
-                   (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
-                   ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
-                       (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) {
+               /* Only support RELA relocation types */
+               if (sechdrs[i].sh_type != SHT_RELA)
+                       continue;
+
+               if (!strcmp(".rela.l2.text", shname) ||
+                   !strcmp(".rela.l1.text", shname) ||
+                   (!strcmp(".rela.text", shname) &&
+                        (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
+
                        err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
                                           symindex, i, mod);
                        if (err < 0)
                                return -ENOEXEC;
                }
        }
+
        return 0;
 }