root.c 9.6 KB

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