1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  *
5  * (C) Copyright 2012
6  * Pavel Herrmann <morpheus.ibis@gmail.com>
7  */
8 
9 #include <common.h>
10 #include <errno.h>
11 #include <fdtdec.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <asm-generic/sections.h>
15 #include <asm/global_data.h>
16 #include <linux/libfdt.h>
17 #include <dm/acpi.h>
18 #include <dm/device.h>
19 #include <dm/device-internal.h>
20 #include <dm/lists.h>
21 #include <dm/of.h>
22 #include <dm/of_access.h>
23 #include <dm/platdata.h>
24 #include <dm/read.h>
25 #include <dm/root.h>
26 #include <dm/uclass.h>
27 #include <dm/util.h>
28 #include <linux/list.h>
29 
30 DECLARE_GLOBAL_DATA_PTR;
31 
32 static struct driver_info root_info = {
33 	.name		= "root_driver",
34 };
35 
dm_root(void)36 struct udevice *dm_root(void)
37 {
38 	if (!gd->dm_root) {
39 		dm_warn("Virtual root driver does not exist!\n");
40 		return NULL;
41 	}
42 
43 	return gd->dm_root;
44 }
45 
dm_fixup_for_gd_move(struct global_data * new_gd)46 void dm_fixup_for_gd_move(struct global_data *new_gd)
47 {
48 	/* The sentinel node has moved, so update things that point to it */
49 	if (gd->dm_root) {
50 		new_gd->uclass_root->next->prev = new_gd->uclass_root;
51 		new_gd->uclass_root->prev->next = new_gd->uclass_root;
52 	}
53 }
54 
fix_drivers(void)55 void fix_drivers(void)
56 {
57 	struct driver *drv =
58 		ll_entry_start(struct driver, driver);
59 	const int n_ents = ll_entry_count(struct driver, driver);
60 	struct driver *entry;
61 
62 	for (entry = drv; entry != drv + n_ents; entry++) {
63 		if (entry->of_match)
64 			entry->of_match = (const struct udevice_id *)
65 				((ulong)entry->of_match + gd->reloc_off);
66 		if (entry->bind)
67 			entry->bind += gd->reloc_off;
68 		if (entry->probe)
69 			entry->probe += gd->reloc_off;
70 		if (entry->remove)
71 			entry->remove += gd->reloc_off;
72 		if (entry->unbind)
73 			entry->unbind += gd->reloc_off;
74 		if (entry->of_to_plat)
75 			entry->of_to_plat += gd->reloc_off;
76 		if (entry->child_post_bind)
77 			entry->child_post_bind += gd->reloc_off;
78 		if (entry->child_pre_probe)
79 			entry->child_pre_probe += gd->reloc_off;
80 		if (entry->child_post_remove)
81 			entry->child_post_remove += gd->reloc_off;
82 		/* OPS are fixed in every uclass post_probe function */
83 		if (entry->ops)
84 			entry->ops += gd->reloc_off;
85 	}
86 }
87 
fix_uclass(void)88 void fix_uclass(void)
89 {
90 	struct uclass_driver *uclass =
91 		ll_entry_start(struct uclass_driver, uclass_driver);
92 	const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
93 	struct uclass_driver *entry;
94 
95 	for (entry = uclass; entry != uclass + n_ents; entry++) {
96 		if (entry->post_bind)
97 			entry->post_bind += gd->reloc_off;
98 		if (entry->pre_unbind)
99 			entry->pre_unbind += gd->reloc_off;
100 		if (entry->pre_probe)
101 			entry->pre_probe += gd->reloc_off;
102 		if (entry->post_probe)
103 			entry->post_probe += gd->reloc_off;
104 		if (entry->pre_remove)
105 			entry->pre_remove += gd->reloc_off;
106 		if (entry->child_post_bind)
107 			entry->child_post_bind += gd->reloc_off;
108 		if (entry->child_pre_probe)
109 			entry->child_pre_probe += gd->reloc_off;
110 		if (entry->init)
111 			entry->init += gd->reloc_off;
112 		if (entry->destroy)
113 			entry->destroy += gd->reloc_off;
114 		/* FIXME maybe also need to fix these ops */
115 		if (entry->ops)
116 			entry->ops += gd->reloc_off;
117 	}
118 }
119 
fix_devices(void)120 void fix_devices(void)
121 {
122 	struct driver_info *dev =
123 		ll_entry_start(struct driver_info, driver_info);
124 	const int n_ents = ll_entry_count(struct driver_info, driver_info);
125 	struct driver_info *entry;
126 
127 	for (entry = dev; entry != dev + n_ents; entry++) {
128 		if (entry->plat)
129 			entry->plat += gd->reloc_off;
130 	}
131 }
132 
dm_setup_inst(void)133 static int dm_setup_inst(void)
134 {
135 	DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
136 
137 	if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
138 		struct udevice_rt *urt;
139 		void *base;
140 		int n_ents;
141 		uint size;
142 
143 		/* Allocate the udevice_rt table */
144 		n_ents = ll_entry_count(struct udevice, udevice);
145 		urt = calloc(n_ents, sizeof(struct udevice_rt));
146 		if (!urt)
147 			return log_msg_ret("urt", -ENOMEM);
148 		gd_set_dm_udevice_rt(urt);
149 
150 		/* Now allocate space for the priv/plat data, and copy it in */
151 		size = __priv_data_end - __priv_data_start;
152 
153 		base = calloc(1, size);
154 		if (!base)
155 			return log_msg_ret("priv", -ENOMEM);
156 		memcpy(base, __priv_data_start, size);
157 		gd_set_dm_priv_base(base);
158 	}
159 
160 	return 0;
161 }
162 
dm_init(bool of_live)163 int dm_init(bool of_live)
164 {
165 	int ret;
166 
167 	if (gd->dm_root) {
168 		dm_warn("Virtual root driver already exists!\n");
169 		return -EINVAL;
170 	}
171 	if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
172 		gd->uclass_root = &uclass_head;
173 	} else {
174 		gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
175 		INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
176 	}
177 
178 	if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
179 		fix_drivers();
180 		fix_uclass();
181 		fix_devices();
182 	}
183 
184 	if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
185 		ret = dm_setup_inst();
186 		if (ret) {
187 			log_debug("dm_setup_inst() failed: %d\n", ret);
188 			return ret;
189 		}
190 	} else {
191 		ret = device_bind_by_name(NULL, false, &root_info,
192 					  &DM_ROOT_NON_CONST);
193 		if (ret)
194 			return ret;
195 		if (CONFIG_IS_ENABLED(OF_CONTROL))
196 			dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
197 		ret = device_probe(DM_ROOT_NON_CONST);
198 		if (ret)
199 			return ret;
200 	}
201 
202 	return 0;
203 }
204 
dm_uninit(void)205 int dm_uninit(void)
206 {
207 	/* Remove non-vital devices first */
208 	device_remove(dm_root(), DM_REMOVE_NON_VITAL);
209 	device_remove(dm_root(), DM_REMOVE_NORMAL);
210 	device_unbind(dm_root());
211 	gd->dm_root = NULL;
212 
213 	return 0;
214 }
215 
216 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
dm_remove_devices_flags(uint flags)217 int dm_remove_devices_flags(uint flags)
218 {
219 	device_remove(dm_root(), flags);
220 
221 	return 0;
222 }
223 #endif
224 
dm_scan_plat(bool pre_reloc_only)225 int dm_scan_plat(bool pre_reloc_only)
226 {
227 	int ret;
228 
229 	if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
230 		struct driver_rt *dyn;
231 		int n_ents;
232 
233 		n_ents = ll_entry_count(struct driver_info, driver_info);
234 		dyn = calloc(n_ents, sizeof(struct driver_rt));
235 		if (!dyn)
236 			return -ENOMEM;
237 		gd_set_dm_driver_rt(dyn);
238 	}
239 
240 	ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
241 	if (ret == -ENOENT) {
242 		dm_warn("Some drivers were not found\n");
243 		ret = 0;
244 	}
245 
246 	return ret;
247 }
248 
249 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
250 /**
251  * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
252  *
253  * This scans the subnodes of a device tree node and and creates a driver
254  * for each one.
255  *
256  * @parent: Parent device for the devices that will be created
257  * @node: Node to scan
258  * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
259  * flag. If false bind all drivers.
260  * @return 0 if OK, -ve on error
261  */
dm_scan_fdt_node(struct udevice * parent,ofnode parent_node,bool pre_reloc_only)262 static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
263 			    bool pre_reloc_only)
264 {
265 	int ret = 0, err = 0;
266 	ofnode node;
267 
268 	if (!ofnode_valid(parent_node))
269 		return 0;
270 
271 	for (node = ofnode_first_subnode(parent_node);
272 	     ofnode_valid(node);
273 	     node = ofnode_next_subnode(node)) {
274 		const char *node_name = ofnode_get_name(node);
275 
276 		if (!ofnode_is_enabled(node)) {
277 			pr_debug("   - ignoring disabled device\n");
278 			continue;
279 		}
280 		err = lists_bind_fdt(parent, node, NULL, pre_reloc_only);
281 		if (err && !ret) {
282 			ret = err;
283 			debug("%s: ret=%d\n", node_name, ret);
284 		}
285 	}
286 
287 	if (ret)
288 		dm_warn("Some drivers failed to bind\n");
289 
290 	return ret;
291 }
292 
dm_scan_fdt_dev(struct udevice * dev)293 int dm_scan_fdt_dev(struct udevice *dev)
294 {
295 	return dm_scan_fdt_node(dev, dev_ofnode(dev),
296 				gd->flags & GD_FLG_RELOC ? false : true);
297 }
298 
dm_scan_fdt(bool pre_reloc_only)299 int dm_scan_fdt(bool pre_reloc_only)
300 {
301 	return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
302 }
303 
dm_scan_fdt_ofnode_path(const char * path,bool pre_reloc_only)304 static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
305 {
306 	ofnode node;
307 
308 	node = ofnode_path(path);
309 
310 	return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
311 }
312 
dm_extended_scan(bool pre_reloc_only)313 int dm_extended_scan(bool pre_reloc_only)
314 {
315 	int ret, i;
316 	const char * const nodes[] = {
317 		"/chosen",
318 		"/clocks",
319 		"/firmware"
320 	};
321 
322 	ret = dm_scan_fdt(pre_reloc_only);
323 	if (ret) {
324 		debug("dm_scan_fdt() failed: %d\n", ret);
325 		return ret;
326 	}
327 
328 	/* Some nodes aren't devices themselves but may contain some */
329 	for (i = 0; i < ARRAY_SIZE(nodes); i++) {
330 		ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
331 		if (ret) {
332 			debug("dm_scan_fdt() scan for %s failed: %d\n",
333 			      nodes[i], ret);
334 			return ret;
335 		}
336 	}
337 
338 	return ret;
339 }
340 #endif
341 
dm_scan_other(bool pre_reloc_only)342 __weak int dm_scan_other(bool pre_reloc_only)
343 {
344 	return 0;
345 }
346 
347 #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
dm_priv_to_rw(void * priv)348 void *dm_priv_to_rw(void *priv)
349 {
350 	long offset = priv - (void *)__priv_data_start;
351 
352 	return gd_dm_priv_base() + offset;
353 }
354 #endif
355 
356 /**
357  * dm_scan() - Scan tables to bind devices
358  *
359  * Runs through the driver_info tables and binds the devices it finds. Then runs
360  * through the devicetree nodes. Finally calls dm_scan_other() to add any
361  * special devices
362  *
363  * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
364  * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
365  */
dm_scan(bool pre_reloc_only)366 static int dm_scan(bool pre_reloc_only)
367 {
368 	int ret;
369 
370 	ret = dm_scan_plat(pre_reloc_only);
371 	if (ret) {
372 		debug("dm_scan_plat() failed: %d\n", ret);
373 		return ret;
374 	}
375 
376 	if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
377 		ret = dm_extended_scan(pre_reloc_only);
378 		if (ret) {
379 			debug("dm_extended_scan() failed: %d\n", ret);
380 			return ret;
381 		}
382 	}
383 
384 	ret = dm_scan_other(pre_reloc_only);
385 	if (ret)
386 		return ret;
387 
388 	return 0;
389 }
390 
dm_init_and_scan(bool pre_reloc_only)391 int dm_init_and_scan(bool pre_reloc_only)
392 {
393 	int ret;
394 
395 	ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
396 	if (ret) {
397 		debug("dm_init() failed: %d\n", ret);
398 		return ret;
399 	}
400 	if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
401 		ret = dm_scan(pre_reloc_only);
402 		if (ret) {
403 			log_debug("dm_scan() failed: %d\n", ret);
404 			return ret;
405 		}
406 	}
407 
408 	return 0;
409 }
410 
411 #ifdef CONFIG_ACPIGEN
root_acpi_get_name(const struct udevice * dev,char * out_name)412 static int root_acpi_get_name(const struct udevice *dev, char *out_name)
413 {
414 	return acpi_copy_name(out_name, "\\_SB");
415 }
416 
417 struct acpi_ops root_acpi_ops = {
418 	.get_name	= root_acpi_get_name,
419 };
420 #endif
421 
422 /* This is the root driver - all drivers are children of this */
423 U_BOOT_DRIVER(root_driver) = {
424 	.name	= "root_driver",
425 	.id	= UCLASS_ROOT,
426 	ACPI_OPS_PTR(&root_acpi_ops)
427 };
428 
429 /* This is the root uclass */
430 UCLASS_DRIVER(root) = {
431 	.name	= "root",
432 	.id	= UCLASS_ROOT,
433 };
434