OSDN Git Service

x86, microcode, AMD: Simplify get_next_ucode
[android-x86/kernel.git] / arch / x86 / kernel / microcode_amd.c
index 0fe6d1a..8b58fc1 100644 (file)
@@ -88,9 +88,9 @@ static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
        return 0;
 }
 
-static int get_matching_microcode(int cpu, void *mc, int rev)
+static int get_matching_microcode(int cpu, struct microcode_header_amd *mc_hdr,
+                                 int rev)
 {
-       struct microcode_header_amd *mc_header = mc;
        unsigned int current_cpu_id;
        u16 equiv_cpu_id = 0;
        unsigned int i = 0;
@@ -109,17 +109,17 @@ static int get_matching_microcode(int cpu, void *mc, int rev)
        if (!equiv_cpu_id)
                return 0;
 
-       if (mc_header->processor_rev_id != equiv_cpu_id)
+       if (mc_hdr->processor_rev_id != equiv_cpu_id)
                return 0;
 
        /* ucode might be chipset specific -- currently we don't support this */
-       if (mc_header->nb_dev_id || mc_header->sb_dev_id) {
+       if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
                pr_err("CPU%d: loading of chipset specific code not yet supported\n",
                       cpu);
                return 0;
        }
 
-       if (mc_header->patch_id <= rev)
+       if (mc_hdr->patch_id <= rev)
                return 0;
 
        return 1;
@@ -155,21 +155,18 @@ static int apply_microcode_amd(int cpu)
        return 0;
 }
 
-static void *
+static struct microcode_header_amd *
 get_next_ucode(const u8 *buf, unsigned int size, unsigned int *mc_size)
 {
+       struct microcode_header_amd *mc;
        unsigned int total_size;
-       u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
-       void *mc;
 
-       get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR);
-
-       if (section_hdr[0] != UCODE_UCODE_TYPE) {
+       if (buf[0] != UCODE_UCODE_TYPE) {
                pr_err("error: invalid type field in container file section header\n");
                return NULL;
        }
 
-       total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8));
+       total_size = buf[4] + (buf[5] << 8);
 
        if (total_size > size || total_size > UCODE_MAX_SIZE) {
                pr_err("error: size mismatch\n");
@@ -188,27 +185,22 @@ get_next_ucode(const u8 *buf, unsigned int size, unsigned int *mc_size)
 
 static int install_equiv_cpu_table(const u8 *buf)
 {
-       u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
-       unsigned int *buf_pos = (unsigned int *)container_hdr;
-       unsigned long size;
-
-       get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE);
+       unsigned int *ibuf = (unsigned int *)buf;
+       unsigned int type = ibuf[1];
+       unsigned int size = ibuf[2];
 
-       size = buf_pos[2];
-
-       if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
+       if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
                pr_err("error: invalid type field in container file section header\n");
-               return 0;
+               return -EINVAL;
        }
 
        equiv_cpu_table = vmalloc(size);
        if (!equiv_cpu_table) {
                pr_err("failed to allocate equivalent CPU table\n");
-               return 0;
+               return -ENOMEM;
        }
 
-       buf += UCODE_CONTAINER_HEADER_SIZE;
-       get_ucode_data(equiv_cpu_table, buf, size);
+       get_ucode_data(equiv_cpu_table, buf + UCODE_CONTAINER_HEADER_SIZE, size);
 
        return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
 }
@@ -223,16 +215,16 @@ static enum ucode_state
 generic_load_microcode(int cpu, const u8 *data, size_t size)
 {
        struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+       struct microcode_header_amd *mc_hdr = NULL;
+       unsigned int mc_size, leftover;
+       unsigned long offset;
        const u8 *ucode_ptr = data;
        void *new_mc = NULL;
-       void *mc;
        int new_rev = uci->cpu_sig.rev;
-       unsigned int leftover;
-       unsigned long offset;
        enum ucode_state state = UCODE_OK;
 
        offset = install_equiv_cpu_table(ucode_ptr);
-       if (!offset) {
+       if (offset < 0) {
                pr_err("failed to create equivalent cpu table\n");
                return UCODE_ERROR;
        }
@@ -241,64 +233,65 @@ generic_load_microcode(int cpu, const u8 *data, size_t size)
        leftover = size - offset;
 
        while (leftover) {
-               unsigned int uninitialized_var(mc_size);
-               struct microcode_header_amd *mc_header;
-
-               mc = get_next_ucode(ucode_ptr, leftover, &mc_size);
-               if (!mc)
+               mc_hdr = get_next_ucode(ucode_ptr, leftover, &mc_size);
+               if (!mc_hdr)
                        break;
 
-               mc_header = (struct microcode_header_amd *)mc;
-               if (get_matching_microcode(cpu, mc, new_rev)) {
+               if (get_matching_microcode(cpu, mc_hdr, new_rev)) {
                        vfree(new_mc);
-                       new_rev = mc_header->patch_id;
-                       new_mc  = mc;
+                       new_rev = mc_hdr->patch_id;
+                       new_mc  = mc_hdr;
                } else
-                       vfree(mc);
+                       vfree(mc_hdr);
 
                ucode_ptr += mc_size;
                leftover  -= mc_size;
        }
 
-       if (new_mc) {
-               if (!leftover) {
-                       vfree(uci->mc);
-                       uci->mc = new_mc;
-                       pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
-                                cpu, new_rev, uci->cpu_sig.rev);
-               } else {
-                       vfree(new_mc);
-                       state = UCODE_ERROR;
-               }
-       } else
+       if (!new_mc) {
                state = UCODE_NFOUND;
+               goto free_table;
+       }
 
+       if (!leftover) {
+               vfree(uci->mc);
+               uci->mc = new_mc;
+               pr_debug("CPU%d update ucode to version 0x%x (from 0x%x)\n",
+                        cpu, new_rev, uci->cpu_sig.rev);
+       } else {
+               vfree(new_mc);
+               state = UCODE_ERROR;
+       }
+
+free_table:
        free_equiv_cpu_table();
 
        return state;
 }
 
-static enum ucode_state request_microcode_fw(int cpu, struct device *device)
+static enum ucode_state request_microcode_amd(int cpu, struct device *device)
 {
        const char *fw_name = "amd-ucode/microcode_amd.bin";
-       const struct firmware *firmware;
-       enum ucode_state ret;
+       const struct firmware *fw;
+       enum ucode_state ret = UCODE_NFOUND;
 
-       if (request_firmware(&firmware, fw_name, device)) {
+       if (request_firmware(&fw, fw_name, device)) {
                printk(KERN_ERR "microcode: failed to load file %s\n", fw_name);
-               return UCODE_NFOUND;
+               goto out;
        }
 
-       if (*(u32 *)firmware->data != UCODE_MAGIC) {
-               pr_err("invalid UCODE_MAGIC (0x%08x)\n",
-                      *(u32 *)firmware->data);
-               return UCODE_ERROR;
+       ret = UCODE_ERROR;
+       if (*(u32 *)fw->data != UCODE_MAGIC) {
+               pr_err("Invalid UCODE_MAGIC (0x%08x)\n", *(u32 *)fw->data);
+               goto fw_release;
        }
 
-       ret = generic_load_microcode(cpu, firmware->data, firmware->size);
+       ret = generic_load_microcode(cpu, fw->data, fw->size);
 
-       release_firmware(firmware);
+fw_release:
+       release_firmware(fw);
 
+out:
        return ret;
 }
 
@@ -319,7 +312,7 @@ static void microcode_fini_cpu_amd(int cpu)
 
 static struct microcode_ops microcode_amd_ops = {
        .request_microcode_user           = request_microcode_user,
-       .request_microcode_fw             = request_microcode_fw,
+       .request_microcode_fw             = request_microcode_amd,
        .collect_cpu_info                 = collect_cpu_info_amd,
        .apply_microcode                  = apply_microcode_amd,
        .microcode_fini_cpu               = microcode_fini_cpu_amd,