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 <cpu.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <log.h>
13 #include <dm/lists.h>
14 #include <dm/root.h>
15 #include <linux/err.h>
16 #include <relocate.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
cpu_probe_all(void)20 int cpu_probe_all(void)
21 {
22 	int ret = uclass_probe_all(UCLASS_CPU);
23 
24 	if (ret) {
25 		debug("%s: Error while probing CPUs (err = %d %s)\n",
26 		      __func__, ret, errno_str(ret));
27 	}
28 	return ret;
29 }
30 
cpu_is_current(struct udevice * cpu)31 int 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 
cpu_get_current_dev(void)43 struct 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 
cpu_get_desc(const struct udevice * dev,char * buf,int size)64 int cpu_get_desc(const struct udevice *dev, char *buf, int size)
65 {
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 
cpu_get_info(const struct udevice * dev,struct cpu_info * info)74 int cpu_get_info(const struct udevice *dev, struct cpu_info *info)
75 {
76 	struct cpu_ops *ops = cpu_get_ops(dev);
77 
78 	if (!ops->get_info)
79 		return -ENOSYS;
80 
81 	/* Init cpu_info to 0 */
82 	memset(info, 0, sizeof(struct cpu_info));
83 
84 	return ops->get_info(dev, info);
85 }
86 
cpu_get_count(const struct udevice * dev)87 int cpu_get_count(const struct udevice *dev)
88 {
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 
cpu_get_vendor(const struct udevice * dev,char * buf,int size)97 int cpu_get_vendor(const struct udevice *dev, char *buf, int size)
98 {
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 
cpu_release_core(const struct udevice * dev,phys_addr_t addr)107 int cpu_release_core(const struct udevice *dev, phys_addr_t addr)
108 {
109 	struct cpu_ops *ops = cpu_get_ops(dev);
110 
111 	if (!ops->release_core)
112 		return -ENOSYS;
113 
114 	return ops->release_core(dev, addr);
115 }
116 
117 U_BOOT_DRIVER(cpu_bus) = {
118 	.name	= "cpu_bus",
119 	.id	= UCLASS_SIMPLE_BUS,
120 	.per_child_plat_auto	= sizeof(struct cpu_plat),
121 };
122 
uclass_cpu_init(struct uclass * uc)123 static int uclass_cpu_init(struct uclass *uc)
124 {
125 	struct udevice *dev;
126 	ofnode node;
127 	int ret;
128 
129 	node = ofnode_path("/cpus");
130 	if (!ofnode_valid(node))
131 		return 0;
132 
133 	ret = device_bind_driver_to_node(dm_root(), "cpu_bus", "cpus", node,
134 					 &dev);
135 
136 	return ret;
137 }
138 
139 UCLASS_DRIVER(cpu) = {
140 	.id		= UCLASS_CPU,
141 	.name		= "cpu",
142 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
143 	.init		= uclass_cpu_init,
144 };
145