blob: a7548325265db7736fe783168a873f81dff7604c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass11f4dc12015-04-28 20:25:09 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass11f4dc12015-04-28 20:25:09 -06005 */
6
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_CPU
8
Simon Glass11f4dc12015-04-28 20:25:09 -06009#include <common.h>
10#include <cpu.h>
11#include <dm.h>
Bin Meng166c3982015-06-12 14:52:18 +080012#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass11f4dc12015-04-28 20:25:09 -060014#include <dm/lists.h>
15#include <dm/root.h>
Peng Fan4c809ae2020-05-03 21:58:47 +080016#include <linux/err.h>
Ovidiu Panaitb391b912022-05-31 21:14:23 +030017#include <relocate.h>
18
19DECLARE_GLOBAL_DATA_PTR;
Simon Glass11f4dc12015-04-28 20:25:09 -060020
Mario Six57370de2018-08-06 10:23:43 +020021int cpu_probe_all(void)
22{
Michal Suchanekc0648b72022-10-12 21:57:51 +020023 int ret = uclass_probe_all(UCLASS_CPU);
Mario Six57370de2018-08-06 10:23:43 +020024
Mario Six57370de2018-08-06 10:23:43 +020025 if (ret) {
Michal Suchanekc0648b72022-10-12 21:57:51 +020026 debug("%s: Error while probing CPUs (err = %d %s)\n",
27 __func__, ret, errno_str(ret));
Mario Six57370de2018-08-06 10:23:43 +020028 }
Michal Suchanekc0648b72022-10-12 21:57:51 +020029 return ret;
Mario Six57370de2018-08-06 10:23:43 +020030}
31
Peng Fan4c809ae2020-05-03 21:58:47 +080032int 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
44struct 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 Glass961420f2020-01-26 22:06:27 -070065int cpu_get_desc(const struct udevice *dev, char *buf, int size)
Simon Glass11f4dc12015-04-28 20:25:09 -060066{
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 Glass961420f2020-01-26 22:06:27 -070075int cpu_get_info(const struct udevice *dev, struct cpu_info *info)
Simon Glass11f4dc12015-04-28 20:25:09 -060076{
77 struct cpu_ops *ops = cpu_get_ops(dev);
78
Bin Mengcb5cbfd2015-06-12 14:52:19 +080079 if (!ops->get_info)
Simon Glass11f4dc12015-04-28 20:25:09 -060080 return -ENOSYS;
81
Sagar Shrikant Kadam969251a2020-06-28 07:45:01 -070082 /* Init cpu_info to 0 */
83 memset(info, 0, sizeof(struct cpu_info));
84
Simon Glass11f4dc12015-04-28 20:25:09 -060085 return ops->get_info(dev, info);
86}
87
Simon Glass961420f2020-01-26 22:06:27 -070088int cpu_get_count(const struct udevice *dev)
Bin Meng780bfdd2015-06-17 11:15:34 +080089{
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 Glass961420f2020-01-26 22:06:27 -070098int cpu_get_vendor(const struct udevice *dev, char *buf, int size)
Alexander Graf94eaa792016-08-19 01:23:27 +020099{
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 Glass11f4dc12015-04-28 20:25:09 -0600108U_BOOT_DRIVER(cpu_bus) = {
109 .name = "cpu_bus",
110 .id = UCLASS_SIMPLE_BUS,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700111 .per_child_plat_auto = sizeof(struct cpu_plat),
Simon Glass11f4dc12015-04-28 20:25:09 -0600112};
113
114static int uclass_cpu_init(struct uclass *uc)
115{
116 struct udevice *dev;
Simon Glass45a26862017-05-18 20:09:07 -0600117 ofnode node;
Simon Glass11f4dc12015-04-28 20:25:09 -0600118 int ret;
119
Simon Glass45a26862017-05-18 20:09:07 -0600120 node = ofnode_path("/cpus");
121 if (!ofnode_valid(node))
Simon Glass11f4dc12015-04-28 20:25:09 -0600122 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 Panaitb391b912022-05-31 21:14:23 +0300130static 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 Glass11f4dc12015-04-28 20:25:09 -0600156UCLASS_DRIVER(cpu) = {
157 .id = UCLASS_CPU,
158 .name = "cpu",
159 .flags = DM_UC_FLAG_SEQ_ALIAS,
160 .init = uclass_cpu_init,
Ovidiu Panaitb391b912022-05-31 21:14:23 +0300161 .post_bind = uclass_cpu_post_bind,
Simon Glass11f4dc12015-04-28 20:25:09 -0600162};