root.c 12 KB

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