blob: 16f8f2e52194a33869ad733a33cb0b9297f8d3ee [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 <cpu.h>
10#include <dm.h>
Bin Mengb891f782015-06-12 14:52:18 +080011#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass131a6452015-04-28 20:25:09 -060013#include <dm/lists.h>
14#include <dm/root.h>
Peng Fan221763a2020-05-03 21:58:47 +080015#include <linux/err.h>
Ovidiu Panaitfd8a0162022-05-31 21:14:23 +030016#include <relocate.h>
17
18DECLARE_GLOBAL_DATA_PTR;
Simon Glass131a6452015-04-28 20:25:09 -060019
Mario Six0263b432018-08-06 10:23:43 +020020int cpu_probe_all(void)
21{
Michal Suchanek44322b52022-10-12 21:57:51 +020022 int ret = uclass_probe_all(UCLASS_CPU);
Mario Six0263b432018-08-06 10:23:43 +020023
Mario Six0263b432018-08-06 10:23:43 +020024 if (ret) {
Michal Suchanek44322b52022-10-12 21:57:51 +020025 debug("%s: Error while probing CPUs (err = %d %s)\n",
26 __func__, ret, errno_str(ret));
Mario Six0263b432018-08-06 10:23:43 +020027 }
Michal Suchanek44322b52022-10-12 21:57:51 +020028 return ret;
Mario Six0263b432018-08-06 10:23:43 +020029}
30
Peng Fan221763a2020-05-03 21:58:47 +080031int cpu_is_current(struct udevice *cpu)
32{
33 struct cpu_ops *ops = cpu_get_ops(cpu);
34
35 if (ops->is_current) {
36 if (ops->is_current(cpu))
37 return 1;
38 }
39
40 return -ENOSYS;
41}
42
43struct udevice *cpu_get_current_dev(void)
44{
45 struct udevice *cpu;
46 int ret;
47
48 uclass_foreach_dev_probe(UCLASS_CPU, cpu) {
49 if (cpu_is_current(cpu) > 0)
50 return cpu;
51 }
52
53 /* If can't find current cpu device, use the first dev instead */
54 ret = uclass_first_device_err(UCLASS_CPU, &cpu);
55 if (ret) {
56 debug("%s: Could not get CPU device (err = %d)\n",
57 __func__, ret);
58 return NULL;
59 }
60
61 return cpu;
62}
63
Simon Glass791fa452020-01-26 22:06:27 -070064int cpu_get_desc(const struct udevice *dev, char *buf, int size)
Simon Glass131a6452015-04-28 20:25:09 -060065{
66 struct cpu_ops *ops = cpu_get_ops(dev);
67
68 if (!ops->get_desc)
69 return -ENOSYS;
70
71 return ops->get_desc(dev, buf, size);
72}
73
Simon Glass791fa452020-01-26 22:06:27 -070074int cpu_get_info(const struct udevice *dev, struct cpu_info *info)
Simon Glass131a6452015-04-28 20:25:09 -060075{
76 struct cpu_ops *ops = cpu_get_ops(dev);
77
Bin Meng6e02ec82015-06-12 14:52:19 +080078 if (!ops->get_info)
Simon Glass131a6452015-04-28 20:25:09 -060079 return -ENOSYS;
80
Sagar Shrikant Kadamce0d69a2020-06-28 07:45:01 -070081 /* Init cpu_info to 0 */
82 memset(info, 0, sizeof(struct cpu_info));
83
Simon Glass131a6452015-04-28 20:25:09 -060084 return ops->get_info(dev, info);
85}
86
Simon Glass791fa452020-01-26 22:06:27 -070087int cpu_get_count(const struct udevice *dev)
Bin Mengd13b0142015-06-17 11:15:34 +080088{
89 struct cpu_ops *ops = cpu_get_ops(dev);
90
91 if (!ops->get_count)
92 return -ENOSYS;
93
94 return ops->get_count(dev);
95}
96
Simon Glass791fa452020-01-26 22:06:27 -070097int cpu_get_vendor(const struct udevice *dev, char *buf, int size)
Alexander Graf67f66bd2016-08-19 01:23:27 +020098{
99 struct cpu_ops *ops = cpu_get_ops(dev);
100
101 if (!ops->get_vendor)
102 return -ENOSYS;
103
104 return ops->get_vendor(dev, buf, size);
105}
106
Simon Glass131a6452015-04-28 20:25:09 -0600107U_BOOT_DRIVER(cpu_bus) = {
108 .name = "cpu_bus",
109 .id = UCLASS_SIMPLE_BUS,
Simon Glassb75b15b2020-12-03 16:55:23 -0700110 .per_child_plat_auto = sizeof(struct cpu_plat),
Simon Glass131a6452015-04-28 20:25:09 -0600111};
112
113static int uclass_cpu_init(struct uclass *uc)
114{
115 struct udevice *dev;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600116 ofnode node;
Simon Glass131a6452015-04-28 20:25:09 -0600117 int ret;
118
Simon Glasse6dd8da2017-05-18 20:09:07 -0600119 node = ofnode_path("/cpus");
120 if (!ofnode_valid(node))
Simon Glass131a6452015-04-28 20:25:09 -0600121 return 0;
122
123 ret = device_bind_driver_to_node(dm_root(), "cpu_bus", "cpus", node,
124 &dev);
125
126 return ret;
127}
128
129UCLASS_DRIVER(cpu) = {
130 .id = UCLASS_CPU,
131 .name = "cpu",
132 .flags = DM_UC_FLAG_SEQ_ALIAS,
133 .init = uclass_cpu_init,
Simon Glass131a6452015-04-28 20:25:09 -0600134};