blob: 71e5900d70e88ac83855445c7a28d3218b676258 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass131a6452015-04-28 20:25:09 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass131a6452015-04-28 20:25:09 -06005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_CPU
8
Simon Glass131a6452015-04-28 20:25:09 -06009#include <common.h>
10#include <cpu.h>
11#include <dm.h>
Bin Mengb891f782015-06-12 14:52:18 +080012#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass131a6452015-04-28 20:25:09 -060014#include <dm/lists.h>
15#include <dm/root.h>
Peng Fan221763a2020-05-03 21:58:47 +080016#include <linux/err.h>
Ovidiu Panaitfd8a0162022-05-31 21:14:23 +030017#include <relocate.h>
18
19DECLARE_GLOBAL_DATA_PTR;
Simon Glass131a6452015-04-28 20:25:09 -060020
Mario Six0263b432018-08-06 10:23:43 +020021int 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 Fan221763a2020-05-03 21:58:47 +080044int 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
56struct 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 Glass791fa452020-01-26 22:06:27 -070077int cpu_get_desc(const struct udevice *dev, char *buf, int size)
Simon Glass131a6452015-04-28 20:25:09 -060078{
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 Glass791fa452020-01-26 22:06:27 -070087int cpu_get_info(const struct udevice *dev, struct cpu_info *info)
Simon Glass131a6452015-04-28 20:25:09 -060088{
89 struct cpu_ops *ops = cpu_get_ops(dev);
90
Bin Meng6e02ec82015-06-12 14:52:19 +080091 if (!ops->get_info)
Simon Glass131a6452015-04-28 20:25:09 -060092 return -ENOSYS;
93
Sagar Shrikant Kadamce0d69a2020-06-28 07:45:01 -070094 /* Init cpu_info to 0 */
95 memset(info, 0, sizeof(struct cpu_info));
96
Simon Glass131a6452015-04-28 20:25:09 -060097 return ops->get_info(dev, info);
98}
99
Simon Glass791fa452020-01-26 22:06:27 -0700100int cpu_get_count(const struct udevice *dev)
Bin Mengd13b0142015-06-17 11:15:34 +0800101{
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 Glass791fa452020-01-26 22:06:27 -0700110int cpu_get_vendor(const struct udevice *dev, char *buf, int size)
Alexander Graf67f66bd2016-08-19 01:23:27 +0200111{
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 Glass131a6452015-04-28 20:25:09 -0600120U_BOOT_DRIVER(cpu_bus) = {
121 .name = "cpu_bus",
122 .id = UCLASS_SIMPLE_BUS,
Simon Glassb75b15b2020-12-03 16:55:23 -0700123 .per_child_plat_auto = sizeof(struct cpu_plat),
Simon Glass131a6452015-04-28 20:25:09 -0600124};
125
126static int uclass_cpu_init(struct uclass *uc)
127{
128 struct udevice *dev;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600129 ofnode node;
Simon Glass131a6452015-04-28 20:25:09 -0600130 int ret;
131
Simon Glasse6dd8da2017-05-18 20:09:07 -0600132 node = ofnode_path("/cpus");
133 if (!ofnode_valid(node))
Simon Glass131a6452015-04-28 20:25:09 -0600134 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 Panaitfd8a0162022-05-31 21:14:23 +0300142static 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 Glass131a6452015-04-28 20:25:09 -0600168UCLASS_DRIVER(cpu) = {
169 .id = UCLASS_CPU,
170 .name = "cpu",
171 .flags = DM_UC_FLAG_SEQ_ALIAS,
172 .init = uclass_cpu_init,
Ovidiu Panaitfd8a0162022-05-31 21:14:23 +0300173 .post_bind = uclass_cpu_post_bind,
Simon Glass131a6452015-04-28 20:25:09 -0600174};