1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #define LOG_CATEGORY UCLASS_CPU
8 
9 #include <common.h>
10 #include <cpu.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <log.h>
14 #include <dm/lists.h>
15 #include <dm/root.h>
16 #include <linux/err.h>
17 #include <relocate.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
cpu_probe_all(void)21 int cpu_probe_all(void)
22 {
23 	int ret = uclass_probe_all(UCLASS_CPU);
24 
25 	if (ret) {
26 		debug("%s: Error while probing CPUs (err = %d %s)\n",
27 		      __func__, ret, errno_str(ret));
28 	}
29 	return ret;
30 }
31 
cpu_is_current(struct udevice * cpu)32 int 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 
cpu_get_current_dev(void)44 struct 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 
cpu_get_desc(const struct udevice * dev,char * buf,int size)65 int cpu_get_desc(const struct udevice *dev, char *buf, int size)
66 {
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 
cpu_get_info(const struct udevice * dev,struct cpu_info * info)75 int cpu_get_info(const struct udevice *dev, struct cpu_info *info)
76 {
77 	struct cpu_ops *ops = cpu_get_ops(dev);
78 
79 	if (!ops->get_info)
80 		return -ENOSYS;
81 
82 	/* Init cpu_info to 0 */
83 	memset(info, 0, sizeof(struct cpu_info));
84 
85 	return ops->get_info(dev, info);
86 }
87 
cpu_get_count(const struct udevice * dev)88 int cpu_get_count(const struct udevice *dev)
89 {
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 
cpu_get_vendor(const struct udevice * dev,char * buf,int size)98 int cpu_get_vendor(const struct udevice *dev, char *buf, int size)
99 {
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 
108 U_BOOT_DRIVER(cpu_bus) = {
109 	.name	= "cpu_bus",
110 	.id	= UCLASS_SIMPLE_BUS,
111 	.per_child_plat_auto	= sizeof(struct cpu_plat),
112 };
113 
uclass_cpu_init(struct uclass * uc)114 static int uclass_cpu_init(struct uclass *uc)
115 {
116 	struct udevice *dev;
117 	ofnode node;
118 	int ret;
119 
120 	node = ofnode_path("/cpus");
121 	if (!ofnode_valid(node))
122 		return 0;
123 
124 	ret = device_bind_driver_to_node(dm_root(), "cpu_bus", "cpus", node,
125 					 &dev);
126 
127 	return ret;
128 }
129 
uclass_cpu_post_bind(struct udevice * dev)130 static 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 
156 UCLASS_DRIVER(cpu) = {
157 	.id		= UCLASS_CPU,
158 	.name		= "cpu",
159 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
160 	.init		= uclass_cpu_init,
161 	.post_bind	= uclass_cpu_post_bind,
162 };
163