Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <cpu.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <asm/cpu.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 12 | |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 15 | int cpu_x86_bind(struct udevice *dev) |
| 16 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 17 | struct cpu_plat *plat = dev_get_parent_plat(dev); |
Alexander Graf | 00b4fc2 | 2016-08-19 01:23:26 +0200 | [diff] [blame] | 18 | struct cpuid_result res; |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 19 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 20 | plat->cpu_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 21 | "intel,apic-id", -1); |
Alexander Graf | 00b4fc2 | 2016-08-19 01:23:26 +0200 | [diff] [blame] | 22 | plat->family = gd->arch.x86; |
| 23 | res = cpuid(1); |
| 24 | plat->id[0] = res.eax; |
| 25 | plat->id[1] = res.edx; |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 30 | int cpu_x86_get_vendor(const struct udevice *dev, char *buf, int size) |
Alexander Graf | 67f66bd | 2016-08-19 01:23:27 +0200 | [diff] [blame] | 31 | { |
| 32 | const char *vendor = cpu_vendor_name(gd->arch.x86_vendor); |
| 33 | |
| 34 | if (size < (strlen(vendor) + 1)) |
| 35 | return -ENOSPC; |
| 36 | |
| 37 | strcpy(buf, vendor); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 42 | int cpu_x86_get_desc(const struct udevice *dev, char *buf, int size) |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 43 | { |
Simon Glass | 0bd50cd | 2017-03-19 12:59:20 -0600 | [diff] [blame] | 44 | char *ptr; |
| 45 | |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 46 | if (size < CPU_MAX_NAME_LEN) |
| 47 | return -ENOSPC; |
| 48 | |
Simon Glass | 0bd50cd | 2017-03-19 12:59:20 -0600 | [diff] [blame] | 49 | ptr = cpu_get_name(buf); |
| 50 | if (ptr != buf) |
| 51 | strcpy(buf, ptr); |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 56 | int cpu_x86_get_count(const struct udevice *dev) |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 57 | { |
| 58 | int node, cpu; |
| 59 | int num = 0; |
| 60 | |
| 61 | node = fdt_path_offset(gd->fdt_blob, "/cpus"); |
| 62 | if (node < 0) |
| 63 | return -ENOENT; |
| 64 | |
| 65 | for (cpu = fdt_first_subnode(gd->fdt_blob, node); |
| 66 | cpu >= 0; |
| 67 | cpu = fdt_next_subnode(gd->fdt_blob, cpu)) { |
| 68 | const char *device_type; |
| 69 | |
| 70 | device_type = fdt_getprop(gd->fdt_blob, cpu, |
| 71 | "device_type", NULL); |
| 72 | if (!device_type) |
| 73 | continue; |
| 74 | if (strcmp(device_type, "cpu") == 0) |
| 75 | num++; |
| 76 | } |
| 77 | |
| 78 | return num; |
| 79 | } |
| 80 | |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 81 | static const struct cpu_ops cpu_x86_ops = { |
| 82 | .get_desc = cpu_x86_get_desc, |
Bin Meng | f967f9a | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 83 | .get_count = cpu_x86_get_count, |
Alexander Graf | 67f66bd | 2016-08-19 01:23:27 +0200 | [diff] [blame] | 84 | .get_vendor = cpu_x86_get_vendor, |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static const struct udevice_id cpu_x86_ids[] = { |
| 88 | { .compatible = "cpu-x86" }, |
| 89 | { } |
| 90 | }; |
| 91 | |
| 92 | U_BOOT_DRIVER(cpu_x86_drv) = { |
| 93 | .name = "cpu_x86", |
| 94 | .id = UCLASS_CPU, |
| 95 | .of_match = cpu_x86_ids, |
| 96 | .bind = cpu_x86_bind, |
| 97 | .ops = &cpu_x86_ops, |
Bin Meng | 09921ac | 2018-10-14 01:07:19 -0700 | [diff] [blame] | 98 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | 12d4105 | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 99 | }; |