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