Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_CPU |
| 8 | |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <cpu.h> |
| 11 | #include <dm.h> |
Bin Meng | b891f78 | 2015-06-12 14:52:18 +0800 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 14 | #include <dm/lists.h> |
| 15 | #include <dm/root.h> |
Peng Fan | 221763a | 2020-05-03 21:58:47 +0800 | [diff] [blame] | 16 | #include <linux/err.h> |
Ovidiu Panait | fd8a016 | 2022-05-31 21:14:23 +0300 | [diff] [blame] | 17 | #include <relocate.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 20 | |
Mario Six | 0263b43 | 2018-08-06 10:23:43 +0200 | [diff] [blame] | 21 | int cpu_probe_all(void) |
| 22 | { |
| 23 | struct udevice *cpu; |
| 24 | int ret; |
| 25 | |
| 26 | ret = uclass_first_device(UCLASS_CPU, &cpu); |
| 27 | if (ret) { |
| 28 | debug("%s: No CPU found (err = %d)\n", __func__, ret); |
| 29 | return ret; |
| 30 | } |
| 31 | |
| 32 | while (cpu) { |
| 33 | ret = uclass_next_device(&cpu); |
| 34 | if (ret) { |
| 35 | debug("%s: Error while probing CPU (err = %d)\n", |
| 36 | __func__, ret); |
| 37 | return ret; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Peng Fan | 221763a | 2020-05-03 21:58:47 +0800 | [diff] [blame] | 44 | int cpu_is_current(struct udevice *cpu) |
| 45 | { |
| 46 | struct cpu_ops *ops = cpu_get_ops(cpu); |
| 47 | |
| 48 | if (ops->is_current) { |
| 49 | if (ops->is_current(cpu)) |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | return -ENOSYS; |
| 54 | } |
| 55 | |
| 56 | struct udevice *cpu_get_current_dev(void) |
| 57 | { |
| 58 | struct udevice *cpu; |
| 59 | int ret; |
| 60 | |
| 61 | uclass_foreach_dev_probe(UCLASS_CPU, cpu) { |
| 62 | if (cpu_is_current(cpu) > 0) |
| 63 | return cpu; |
| 64 | } |
| 65 | |
| 66 | /* If can't find current cpu device, use the first dev instead */ |
| 67 | ret = uclass_first_device_err(UCLASS_CPU, &cpu); |
| 68 | if (ret) { |
| 69 | debug("%s: Could not get CPU device (err = %d)\n", |
| 70 | __func__, ret); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | return cpu; |
| 75 | } |
| 76 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 77 | int cpu_get_desc(const struct udevice *dev, char *buf, int size) |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 78 | { |
| 79 | struct cpu_ops *ops = cpu_get_ops(dev); |
| 80 | |
| 81 | if (!ops->get_desc) |
| 82 | return -ENOSYS; |
| 83 | |
| 84 | return ops->get_desc(dev, buf, size); |
| 85 | } |
| 86 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 87 | int cpu_get_info(const struct udevice *dev, struct cpu_info *info) |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 88 | { |
| 89 | struct cpu_ops *ops = cpu_get_ops(dev); |
| 90 | |
Bin Meng | 6e02ec8 | 2015-06-12 14:52:19 +0800 | [diff] [blame] | 91 | if (!ops->get_info) |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 92 | return -ENOSYS; |
| 93 | |
Sagar Shrikant Kadam | ce0d69a | 2020-06-28 07:45:01 -0700 | [diff] [blame] | 94 | /* Init cpu_info to 0 */ |
| 95 | memset(info, 0, sizeof(struct cpu_info)); |
| 96 | |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 97 | return ops->get_info(dev, info); |
| 98 | } |
| 99 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 100 | int cpu_get_count(const struct udevice *dev) |
Bin Meng | d13b014 | 2015-06-17 11:15:34 +0800 | [diff] [blame] | 101 | { |
| 102 | struct cpu_ops *ops = cpu_get_ops(dev); |
| 103 | |
| 104 | if (!ops->get_count) |
| 105 | return -ENOSYS; |
| 106 | |
| 107 | return ops->get_count(dev); |
| 108 | } |
| 109 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 110 | int cpu_get_vendor(const struct udevice *dev, char *buf, int size) |
Alexander Graf | 67f66bd | 2016-08-19 01:23:27 +0200 | [diff] [blame] | 111 | { |
| 112 | struct cpu_ops *ops = cpu_get_ops(dev); |
| 113 | |
| 114 | if (!ops->get_vendor) |
| 115 | return -ENOSYS; |
| 116 | |
| 117 | return ops->get_vendor(dev, buf, size); |
| 118 | } |
| 119 | |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 120 | U_BOOT_DRIVER(cpu_bus) = { |
| 121 | .name = "cpu_bus", |
| 122 | .id = UCLASS_SIMPLE_BUS, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 123 | .per_child_plat_auto = sizeof(struct cpu_plat), |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static int uclass_cpu_init(struct uclass *uc) |
| 127 | { |
| 128 | struct udevice *dev; |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 129 | ofnode node; |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 130 | int ret; |
| 131 | |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 132 | node = ofnode_path("/cpus"); |
| 133 | if (!ofnode_valid(node)) |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 134 | return 0; |
| 135 | |
| 136 | ret = device_bind_driver_to_node(dm_root(), "cpu_bus", "cpus", node, |
| 137 | &dev); |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
Ovidiu Panait | fd8a016 | 2022-05-31 21:14:23 +0300 | [diff] [blame] | 142 | static int uclass_cpu_post_bind(struct udevice *dev) |
| 143 | { |
| 144 | if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC) && |
| 145 | (gd->flags & GD_FLG_RELOC)) { |
| 146 | struct cpu_ops *ops = cpu_get_ops(dev); |
| 147 | static int reloc_done; |
| 148 | |
| 149 | if (!reloc_done) { |
| 150 | if (ops->get_desc) |
| 151 | MANUAL_RELOC(ops->get_desc); |
| 152 | if (ops->get_info) |
| 153 | MANUAL_RELOC(ops->get_info); |
| 154 | if (ops->get_count) |
| 155 | MANUAL_RELOC(ops->get_count); |
| 156 | if (ops->get_vendor) |
| 157 | MANUAL_RELOC(ops->get_vendor); |
| 158 | if (ops->is_current) |
| 159 | MANUAL_RELOC(ops->is_current); |
| 160 | |
| 161 | reloc_done++; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 168 | UCLASS_DRIVER(cpu) = { |
| 169 | .id = UCLASS_CPU, |
| 170 | .name = "cpu", |
| 171 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 172 | .init = uclass_cpu_init, |
Ovidiu Panait | fd8a016 | 2022-05-31 21:14:23 +0300 | [diff] [blame] | 173 | .post_bind = uclass_cpu_post_bind, |
Simon Glass | 131a645 | 2015-04-28 20:25:09 -0600 | [diff] [blame] | 174 | }; |