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