blob: 9772578968bacc1f4f9d80974e29e09813298554 [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
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Simon Glass131a6452015-04-28 20:25:09 -060010#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{
Michal Suchanek44322b52022-10-12 21:57:51 +020023 int ret = uclass_probe_all(UCLASS_CPU);
Mario Six0263b432018-08-06 10:23:43 +020024
Mario Six0263b432018-08-06 10:23:43 +020025 if (ret) {
Michal Suchanek44322b52022-10-12 21:57:51 +020026 debug("%s: Error while probing CPUs (err = %d %s)\n",
27 __func__, ret, errno_str(ret));
Mario Six0263b432018-08-06 10:23:43 +020028 }
Michal Suchanek44322b52022-10-12 21:57:51 +020029 return ret;
Mario Six0263b432018-08-06 10:23:43 +020030}
31
Peng Fan221763a2020-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 Glass791fa452020-01-26 22:06:27 -070065int cpu_get_desc(const struct udevice *dev, char *buf, int size)
Simon Glass131a6452015-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 Glass791fa452020-01-26 22:06:27 -070075int cpu_get_info(const struct udevice *dev, struct cpu_info *info)
Simon Glass131a6452015-04-28 20:25:09 -060076{
77 struct cpu_ops *ops = cpu_get_ops(dev);
78
Bin Meng6e02ec82015-06-12 14:52:19 +080079 if (!ops->get_info)
Simon Glass131a6452015-04-28 20:25:09 -060080 return -ENOSYS;
81
Sagar Shrikant Kadamce0d69a2020-06-28 07:45:01 -070082 /* Init cpu_info to 0 */
83 memset(info, 0, sizeof(struct cpu_info));
84
Simon Glass131a6452015-04-28 20:25:09 -060085 return ops->get_info(dev, info);
86}
87
Simon Glass791fa452020-01-26 22:06:27 -070088int cpu_get_count(const struct udevice *dev)
Bin Mengd13b0142015-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 Glass791fa452020-01-26 22:06:27 -070098int cpu_get_vendor(const struct udevice *dev, char *buf, int size)
Alexander Graf67f66bd2016-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 Glass131a6452015-04-28 20:25:09 -0600108U_BOOT_DRIVER(cpu_bus) = {
109 .name = "cpu_bus",
110 .id = UCLASS_SIMPLE_BUS,
Simon Glassb75b15b2020-12-03 16:55:23 -0700111 .per_child_plat_auto = sizeof(struct cpu_plat),
Simon Glass131a6452015-04-28 20:25:09 -0600112};
113
114static int uclass_cpu_init(struct uclass *uc)
115{
116 struct udevice *dev;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600117 ofnode node;
Simon Glass131a6452015-04-28 20:25:09 -0600118 int ret;
119
Simon Glasse6dd8da2017-05-18 20:09:07 -0600120 node = ofnode_path("/cpus");
121 if (!ofnode_valid(node))
Simon Glass131a6452015-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
130UCLASS_DRIVER(cpu) = {
131 .id = UCLASS_CPU,
132 .name = "cpu",
133 .flags = DM_UC_FLAG_SEQ_ALIAS,
134 .init = uclass_cpu_init,
Simon Glass131a6452015-04-28 20:25:09 -0600135};