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