base.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <string.h>
  12. #include <drivers/ofw.h>
  13. #include <drivers/ofw_io.h>
  14. #include <drivers/ofw_fdt.h>
  15. #include <drivers/ofw_raw.h>
  16. #define DBG_TAG "rtdm.ofw"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. #include "ofw_internal.h"
  20. struct rt_ofw_node *ofw_node_root = RT_NULL;
  21. struct rt_ofw_node *ofw_node_cpus = RT_NULL;
  22. struct rt_ofw_node *ofw_node_chosen = RT_NULL;
  23. struct rt_ofw_node *ofw_node_aliases = RT_NULL;
  24. struct rt_ofw_node *ofw_node_reserved_memory = RT_NULL;
  25. static rt_phandle _phandle_range[2] = { 1, 1 };
  26. static struct rt_ofw_node **_phandle_hash = RT_NULL;
  27. static rt_list_t _aliases_nodes = RT_LIST_OBJECT_INIT(_aliases_nodes);
  28. rt_err_t ofw_phandle_hash_reset(rt_phandle min, rt_phandle max)
  29. {
  30. rt_err_t err = RT_EOK;
  31. struct rt_ofw_node **hash_ptr = RT_NULL;
  32. max = RT_ALIGN(max, OFW_NODE_MIN_HASH);
  33. if (max > _phandle_range[1])
  34. {
  35. rt_size_t size = sizeof(*_phandle_hash) * (max - min);
  36. if (!_phandle_hash)
  37. {
  38. hash_ptr = rt_calloc(1, size);
  39. }
  40. else
  41. {
  42. hash_ptr = rt_realloc(_phandle_hash, size);
  43. if (hash_ptr)
  44. {
  45. rt_size_t old_max = _phandle_range[1];
  46. rt_memset(&hash_ptr[old_max], 0, sizeof(_phandle_hash) * (max - old_max));
  47. }
  48. }
  49. }
  50. if (hash_ptr)
  51. {
  52. /* We always reset min value only once */
  53. if (min)
  54. {
  55. _phandle_range[0] = min;
  56. }
  57. _phandle_range[1] = max;
  58. _phandle_hash = hash_ptr;
  59. }
  60. else
  61. {
  62. err = -RT_ENOMEM;
  63. }
  64. return err;
  65. }
  66. static void ofw_prop_destroy(struct rt_ofw_prop *prop)
  67. {
  68. struct rt_ofw_prop *next;
  69. while (prop)
  70. {
  71. next = prop->next;
  72. rt_free(prop);
  73. prop = next;
  74. }
  75. }
  76. static struct rt_ofw_node *ofw_get_next_node(struct rt_ofw_node *prev)
  77. {
  78. struct rt_ofw_node *np;
  79. /*
  80. * Walk:
  81. *
  82. * / { ------------------------ [0] (START) has child, goto child.
  83. *
  84. * node0 { ---------------- [1] has child, goto child.
  85. *
  86. * node0_0 { ---------- [2] no child, has sibling, goto sibling.
  87. * };
  88. *
  89. * node0_1 { ---------- [3] no sibling now.
  90. * upward while the parent has sibling.
  91. * };
  92. * };
  93. *
  94. * node1 { ---------------- [4] come from node0 who find the sibling:
  95. * node1, node1 has child, goto child.
  96. *
  97. * node1_0 { ---------- [5] has child, goto child.
  98. *
  99. * node1_0_0 { ---- [6] no sibling now.
  100. * upward while the parent has sibling.
  101. * (END) in the root.
  102. * };
  103. * };
  104. * };
  105. * };
  106. */
  107. if (!prev)
  108. {
  109. np = ofw_node_root;
  110. }
  111. else if (prev->child)
  112. {
  113. np = prev->child;
  114. }
  115. else
  116. {
  117. np = prev;
  118. while (np->parent && !np->sibling)
  119. {
  120. np = np->parent;
  121. }
  122. np = np->sibling;
  123. }
  124. return np;
  125. }
  126. static void ofw_node_destroy(struct rt_ofw_node *np)
  127. {
  128. struct rt_ofw_node *prev;
  129. if (np->parent)
  130. {
  131. /* Ask parent and prev sibling we are destroy. */
  132. prev = np->parent->child;
  133. if (prev == np)
  134. {
  135. np->parent->child = RT_NULL;
  136. }
  137. else
  138. {
  139. while (prev->sibling != np)
  140. {
  141. prev = prev->sibling;
  142. }
  143. prev->sibling = np->sibling;
  144. }
  145. }
  146. while (np)
  147. {
  148. if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) == RT_FALSE)
  149. {
  150. LOG_E("%s is system node", np->full_name);
  151. RT_ASSERT(0);
  152. }
  153. prev = np;
  154. np = ofw_get_next_node(np);
  155. ofw_prop_destroy(prev->props);
  156. rt_free(prev);
  157. }
  158. }
  159. rt_err_t rt_ofw_node_destroy(struct rt_ofw_node *np)
  160. {
  161. rt_err_t err = RT_EOK;
  162. if (np)
  163. {
  164. if (rt_ref_read(&np->ref) <= 1)
  165. {
  166. ofw_node_destroy(np);
  167. }
  168. else
  169. {
  170. err = -RT_EBUSY;
  171. }
  172. }
  173. else
  174. {
  175. err = -RT_EINVAL;
  176. }
  177. return err;
  178. }
  179. struct rt_ofw_node *rt_ofw_node_get(struct rt_ofw_node *np)
  180. {
  181. if (np)
  182. {
  183. LOG_D("%s get ref = %d", np->full_name, rt_ref_read(&np->ref));
  184. rt_ref_get(&np->ref);
  185. }
  186. return np;
  187. }
  188. static void ofw_node_release(struct rt_ref *r)
  189. {
  190. struct rt_ofw_node *np = rt_container_of(r, struct rt_ofw_node, ref);
  191. LOG_E("%s is release", np->full_name);
  192. RT_ASSERT(0);
  193. }
  194. void rt_ofw_node_put(struct rt_ofw_node *np)
  195. {
  196. if (np)
  197. {
  198. LOG_D("%s put ref = %d", np->full_name, rt_ref_read(&np->ref));
  199. rt_ref_put(&np->ref, &ofw_node_release);
  200. }
  201. }
  202. rt_bool_t rt_ofw_node_tag_equ(const struct rt_ofw_node *np, const char *tag)
  203. {
  204. rt_bool_t ret = RT_FALSE;
  205. if (np && tag)
  206. {
  207. const char *node_name = rt_fdt_node_name(np->full_name);
  208. rt_size_t tag_len = strchrnul(node_name, '@') - node_name;
  209. ret = (rt_strlen(tag) == tag_len && !rt_strncmp(node_name, tag, tag_len));
  210. }
  211. return ret;
  212. }
  213. rt_bool_t rt_ofw_node_tag_prefix(const struct rt_ofw_node *np, const char *prefix)
  214. {
  215. rt_bool_t ret = RT_FALSE;
  216. if (np && prefix)
  217. {
  218. ret = !rt_strncmp(rt_fdt_node_name(np->full_name), prefix, rt_strlen(prefix));
  219. }
  220. return ret;
  221. }
  222. static int ofw_prop_index_of_string(struct rt_ofw_prop *prop, const char *string,
  223. rt_int32_t (*cmp)(const char *cs, const char *ct))
  224. {
  225. int index = -1;
  226. rt_size_t len = prop->length, slen = 0;
  227. const char *value = prop->value;
  228. for (int idx = 0; len > 0; ++idx)
  229. {
  230. /* Add '\0' */
  231. slen = rt_strlen(value) + 1;
  232. if (!cmp(value, string))
  233. {
  234. index = idx;
  235. break;
  236. }
  237. len -= slen;
  238. value += slen;
  239. }
  240. return index;
  241. }
  242. static rt_int32_t ofw_strcasecmp(const char *cs, const char *ct)
  243. {
  244. extern rt_int32_t strcasecmp(const char *cs, const char *ct);
  245. return rt_strcasecmp(cs, ct);
  246. }
  247. static int ofw_prop_index_of_compatible(struct rt_ofw_prop *prop, const char *compatible)
  248. {
  249. return ofw_prop_index_of_string(prop, compatible, ofw_strcasecmp);
  250. }
  251. static int ofw_node_index_of_compatible(const struct rt_ofw_node *np, const char *compatible)
  252. {
  253. int idx = -1;
  254. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  255. if (prop)
  256. {
  257. idx = ofw_prop_index_of_compatible(prop, compatible);
  258. }
  259. return idx;
  260. }
  261. rt_bool_t rt_ofw_machine_is_compatible(const char *compatible)
  262. {
  263. return ofw_node_index_of_compatible(ofw_node_root, compatible) >= 0;
  264. }
  265. /*
  266. * Property status:
  267. *
  268. * "okay" or "ok":
  269. * Indicates the device is operational.
  270. *
  271. * "disabled":
  272. * Indicates that the device is not presently operational, but it might
  273. * become operational in the future (for example, something is not
  274. * plugged in, or switched off).
  275. * Refer to the device binding for details on what disabled means for a
  276. * given device.
  277. *
  278. * "reserved":
  279. * Indicates that the device is operational, but should not be used.
  280. * Typically this is used for devices that are controlled by another
  281. * software component, such as platform firmware.
  282. *
  283. * "fail":
  284. * Indicates that the device is not operational. A serious error was
  285. * detected in the device, and it is unlikely to become operational
  286. * without repair.
  287. *
  288. * "fail-sss":
  289. * Indicates that the device is not operational. A serious error was
  290. * detected in the device and it is unlikely to become operational
  291. * without repair. The sss portion of the value is specific to the
  292. * device and indicates the error condition detected.
  293. */
  294. static rt_bool_t ofw_node_is_fail(const struct rt_ofw_node *np)
  295. {
  296. rt_bool_t res = RT_FALSE;
  297. const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
  298. if (status)
  299. {
  300. res = !rt_strcmp(status, "fail") || !rt_strncmp(status, "fail-", 5);
  301. }
  302. return res;
  303. }
  304. static rt_bool_t ofw_node_is_available(const struct rt_ofw_node *np)
  305. {
  306. rt_bool_t res = RT_TRUE;
  307. const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
  308. if (status)
  309. {
  310. res = !rt_strcmp(status, "okay") || !rt_strcmp(status, "ok");
  311. }
  312. return res;
  313. }
  314. rt_bool_t rt_ofw_node_is_available(const struct rt_ofw_node *np)
  315. {
  316. return np ? ofw_node_is_available(np) : RT_FALSE;
  317. }
  318. rt_bool_t rt_ofw_node_is_compatible(const struct rt_ofw_node *np, const char *compatible)
  319. {
  320. rt_bool_t res = RT_FALSE;
  321. if (np)
  322. {
  323. res = ofw_node_index_of_compatible(np, compatible) >= 0;
  324. }
  325. return res;
  326. }
  327. static struct rt_ofw_node_id *ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
  328. {
  329. int best_index = RT_UINT32_MAX >> 1, index;
  330. struct rt_ofw_node_id *found_id = RT_NULL, *id;
  331. for (id = (struct rt_ofw_node_id *)ids; id->compatible[0]; ++id)
  332. {
  333. index = ofw_prop_index_of_compatible(prop, id->compatible);
  334. if (index >= 0 && index < best_index)
  335. {
  336. found_id = id;
  337. best_index = index;
  338. }
  339. }
  340. return found_id;
  341. }
  342. struct rt_ofw_node_id *rt_ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
  343. {
  344. struct rt_ofw_node_id *id = RT_NULL;
  345. if (prop && ids && !rt_strcmp(prop->name, "compatible"))
  346. {
  347. id = ofw_prop_match(prop, ids);
  348. }
  349. return id;
  350. }
  351. struct rt_ofw_node_id *rt_ofw_node_match(struct rt_ofw_node *np, const struct rt_ofw_node_id *ids)
  352. {
  353. struct rt_ofw_prop *prop;
  354. struct rt_ofw_node_id *id = RT_NULL;
  355. if (np && ids && (prop = rt_ofw_get_prop(np, "compatible", RT_NULL)))
  356. {
  357. id = ofw_prop_match(prop, ids);
  358. }
  359. return id;
  360. }
  361. struct rt_ofw_node *rt_ofw_find_node_by_tag(struct rt_ofw_node *from, const char *tag)
  362. {
  363. struct rt_ofw_node *np = RT_NULL;
  364. if (tag)
  365. {
  366. rt_ofw_foreach_nodes(from, np)
  367. {
  368. if (rt_ofw_node_tag_equ(np, tag))
  369. {
  370. break;
  371. }
  372. }
  373. }
  374. return np;
  375. }
  376. struct rt_ofw_node *rt_ofw_find_node_by_prop_r(struct rt_ofw_node *from, const char *propname,
  377. const struct rt_ofw_prop **out_prop)
  378. {
  379. struct rt_ofw_node *np = RT_NULL;
  380. if (propname)
  381. {
  382. rt_ofw_foreach_nodes(from, np)
  383. {
  384. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
  385. if (prop)
  386. {
  387. if (out_prop)
  388. {
  389. *out_prop = prop;
  390. }
  391. break;
  392. }
  393. }
  394. }
  395. return np;
  396. }
  397. struct rt_ofw_node *rt_ofw_find_node_by_name(struct rt_ofw_node *from, const char *name)
  398. {
  399. struct rt_ofw_node *np = RT_NULL;
  400. if (name)
  401. {
  402. rt_ofw_foreach_nodes(from, np)
  403. {
  404. if (np->name && !rt_strcmp(np->name, name))
  405. {
  406. np = rt_ofw_node_get(np);
  407. break;
  408. }
  409. }
  410. }
  411. return np;
  412. }
  413. struct rt_ofw_node *rt_ofw_find_node_by_type(struct rt_ofw_node *from, const char *type)
  414. {
  415. struct rt_ofw_node *np = RT_NULL;
  416. if (type)
  417. {
  418. rt_ofw_foreach_nodes(from, np)
  419. {
  420. if (rt_ofw_node_is_type(np, type))
  421. {
  422. break;
  423. }
  424. }
  425. }
  426. return np;
  427. }
  428. struct rt_ofw_node *rt_ofw_find_node_by_compatible(struct rt_ofw_node *from, const char *compatible)
  429. {
  430. struct rt_ofw_node *np = RT_NULL;
  431. if (compatible)
  432. {
  433. rt_ofw_foreach_nodes(from, np)
  434. {
  435. if (ofw_node_index_of_compatible(np, compatible) >= 0)
  436. {
  437. break;
  438. }
  439. }
  440. }
  441. return np;
  442. }
  443. struct rt_ofw_node *rt_ofw_find_node_by_ids_r(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids,
  444. const struct rt_ofw_node_id **out_id)
  445. {
  446. struct rt_ofw_node *np = RT_NULL;
  447. if (ids)
  448. {
  449. rt_ofw_foreach_nodes(from, np)
  450. {
  451. struct rt_ofw_node_id *id = rt_ofw_node_match(np, ids);
  452. if (id)
  453. {
  454. if (out_id)
  455. {
  456. *out_id = id;
  457. }
  458. break;
  459. }
  460. }
  461. }
  462. return np;
  463. }
  464. struct rt_ofw_node *rt_ofw_find_node_by_path(const char *path)
  465. {
  466. struct rt_ofw_node *np = RT_NULL, *parent, *tmp = RT_NULL;
  467. if (path)
  468. {
  469. if (!rt_strcmp(path, "/"))
  470. {
  471. np = ofw_node_root;
  472. }
  473. else
  474. {
  475. ++path;
  476. parent = rt_ofw_node_get(ofw_node_root);
  477. while (*path)
  478. {
  479. const char *next = strchrnul(path, '/');
  480. rt_size_t len = next - path;
  481. tmp = RT_NULL;
  482. rt_ofw_foreach_child_node(parent, np)
  483. {
  484. if (!rt_strncmp(np->full_name, path, len))
  485. {
  486. rt_ofw_node_put(parent);
  487. parent = np;
  488. tmp = np;
  489. break;
  490. }
  491. }
  492. if (!tmp)
  493. {
  494. rt_ofw_node_put(parent);
  495. break;
  496. }
  497. path += len;
  498. }
  499. np = tmp;
  500. }
  501. rt_ofw_node_get(np);
  502. }
  503. return np;
  504. }
  505. struct rt_ofw_node *rt_ofw_find_node_by_phandle(rt_phandle phandle)
  506. {
  507. struct rt_ofw_node *np = RT_NULL;
  508. if (phandle >= OFW_PHANDLE_MIN && phandle <= OFW_PHANDLE_MAX)
  509. {
  510. /* rebase from zero */
  511. rt_phandle poff = phandle - _phandle_range[0];
  512. np = _phandle_hash[poff];
  513. if (!np)
  514. {
  515. rt_ofw_foreach_allnodes(np)
  516. {
  517. if (np->phandle == phandle)
  518. {
  519. _phandle_hash[poff] = np;
  520. break;
  521. }
  522. }
  523. }
  524. else
  525. {
  526. rt_ofw_node_get(np);
  527. }
  528. }
  529. return np;
  530. }
  531. struct rt_ofw_node *rt_ofw_get_parent(const struct rt_ofw_node *np)
  532. {
  533. if (np)
  534. {
  535. np = rt_ofw_node_get(np->parent);
  536. }
  537. return (struct rt_ofw_node *)np;
  538. }
  539. struct rt_ofw_node *rt_ofw_get_child_by_tag(const struct rt_ofw_node *parent, const char *tag)
  540. {
  541. struct rt_ofw_node *child = RT_NULL;
  542. if (parent && tag)
  543. {
  544. rt_ofw_foreach_child_node(parent, child)
  545. {
  546. if (rt_ofw_node_tag_equ(child, tag))
  547. {
  548. break;
  549. }
  550. }
  551. }
  552. return child;
  553. }
  554. struct rt_ofw_node *rt_ofw_get_child_by_compatible(const struct rt_ofw_node *parent, const char *compatible)
  555. {
  556. struct rt_ofw_node *child = RT_NULL;
  557. if (parent && compatible)
  558. {
  559. rt_ofw_foreach_child_node(parent, child)
  560. {
  561. if (ofw_node_index_of_compatible(child, compatible) >= 0)
  562. {
  563. break;
  564. }
  565. }
  566. }
  567. return child;
  568. }
  569. int rt_ofw_get_child_count(const struct rt_ofw_node *np)
  570. {
  571. int nr;
  572. if (np)
  573. {
  574. struct rt_ofw_node *child;
  575. nr = 0;
  576. rt_ofw_foreach_child_node(np, child)
  577. {
  578. ++nr;
  579. }
  580. }
  581. else
  582. {
  583. nr = -RT_EINVAL;
  584. }
  585. return nr;
  586. }
  587. int rt_ofw_get_available_child_count(const struct rt_ofw_node *np)
  588. {
  589. int nr;
  590. if (np)
  591. {
  592. struct rt_ofw_node *child;
  593. nr = 0;
  594. rt_ofw_foreach_available_child_node(np, child)
  595. {
  596. ++nr;
  597. }
  598. }
  599. else
  600. {
  601. nr = -RT_EINVAL;
  602. }
  603. return nr;
  604. }
  605. struct rt_ofw_node *rt_ofw_get_next_node(struct rt_ofw_node *prev)
  606. {
  607. struct rt_ofw_node *np;
  608. np = rt_ofw_node_get(ofw_get_next_node(prev));
  609. rt_ofw_node_put(prev);
  610. return np;
  611. }
  612. struct rt_ofw_node *rt_ofw_get_next_parent(struct rt_ofw_node *prev)
  613. {
  614. struct rt_ofw_node *next = RT_NULL;
  615. if (prev)
  616. {
  617. next = rt_ofw_node_get(prev->parent);
  618. rt_ofw_node_put(prev);
  619. }
  620. return next;
  621. }
  622. struct rt_ofw_node *rt_ofw_get_next_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
  623. {
  624. struct rt_ofw_node *next = RT_NULL;
  625. if (parent)
  626. {
  627. next = prev ? prev->sibling : parent->child;
  628. rt_ofw_node_put(prev);
  629. rt_ofw_node_get(next);
  630. }
  631. return next;
  632. }
  633. struct rt_ofw_node *rt_ofw_get_next_available_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
  634. {
  635. struct rt_ofw_node *next = RT_NULL;
  636. if (parent)
  637. {
  638. next = prev;
  639. do {
  640. next = rt_ofw_get_next_child(parent, next);
  641. } while (next && !ofw_node_is_available(next));
  642. }
  643. return next;
  644. }
  645. struct rt_ofw_node *rt_ofw_get_cpu_node(int cpu, int *thread, rt_bool_t (*match_cpu_hwid)(int cpu, rt_uint64_t hwid))
  646. {
  647. const char *propname = "reg";
  648. struct rt_ofw_node *cpu_np = RT_NULL;
  649. /*
  650. * "reg" (some of the obsolete arch may be other names):
  651. * The value of reg is a <prop-encoded-array> that defines a unique
  652. * CPU/thread id for the CPU/threads represented by the CPU node.
  653. *
  654. * If a CPU supports more than one thread (i.e. multiple streams of
  655. * execution) the reg property is an array with 1 element per thread. The
  656. * #address-cells on the /cpus node specifies how many cells each element
  657. * of the array takes. Software can determine the number of threads by
  658. * dividing the size of reg by the parent node’s #address-cells:
  659. *
  660. * thread-number = reg-cells / address-cells
  661. *
  662. * If a CPU/thread can be the target of an external interrupt the reg
  663. * property value must be a unique CPU/thread id that is addressable by the
  664. * interrupt controller.
  665. *
  666. * If a CPU/thread cannot be the target of an external interrupt, then reg
  667. * must be unique and out of bounds of the range addressed by the interrupt
  668. * controller
  669. *
  670. * If a CPU/thread’s PIR (pending interrupt register) is modifiable, a
  671. * client program should modify PIR to match the reg property value. If PIR
  672. * cannot be modified and the PIR value is distinct from the interrupt
  673. * controller number space, the CPUs binding may define a binding-specific
  674. * representation of PIR values if desired.
  675. */
  676. rt_ofw_foreach_cpu_node(cpu_np)
  677. {
  678. rt_ssize_t prop_len;
  679. rt_bool_t is_end = RT_FALSE;
  680. int tid, addr_cells = rt_ofw_io_addr_cells(cpu_np);
  681. const fdt32_t *cell = rt_ofw_prop_read_raw(cpu_np, propname, &prop_len);
  682. if (!cell && !addr_cells)
  683. {
  684. if (match_cpu_hwid && match_cpu_hwid(cpu, 0))
  685. {
  686. break;
  687. }
  688. continue;
  689. }
  690. if (!match_cpu_hwid)
  691. {
  692. continue;
  693. }
  694. prop_len /= sizeof(*cell) * addr_cells;
  695. for (tid = 0; tid < prop_len; ++tid)
  696. {
  697. rt_uint64_t hwid = rt_fdt_read_number(cell, addr_cells);
  698. if (match_cpu_hwid(cpu, hwid))
  699. {
  700. if (thread)
  701. {
  702. *thread = tid;
  703. }
  704. is_end = RT_TRUE;
  705. break;
  706. }
  707. cell += addr_cells;
  708. }
  709. if (is_end)
  710. {
  711. break;
  712. }
  713. }
  714. return cpu_np;
  715. }
  716. struct rt_ofw_node *rt_ofw_get_next_cpu_node(struct rt_ofw_node *prev)
  717. {
  718. struct rt_ofw_node *cpu_np;
  719. if (prev)
  720. {
  721. cpu_np = prev->sibling;
  722. rt_ofw_node_put(prev);
  723. }
  724. else
  725. {
  726. cpu_np = ofw_node_cpus->child;
  727. }
  728. for (; cpu_np; cpu_np = cpu_np->sibling)
  729. {
  730. if (ofw_node_is_fail(cpu_np))
  731. {
  732. continue;
  733. }
  734. if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
  735. {
  736. continue;
  737. }
  738. if (rt_ofw_node_get(cpu_np))
  739. {
  740. break;
  741. }
  742. }
  743. return cpu_np;
  744. }
  745. struct rt_ofw_node *rt_ofw_get_cpu_state_node(struct rt_ofw_node *cpu_np, int index)
  746. {
  747. struct rt_ofw_cell_args args;
  748. struct rt_ofw_node *np = RT_NULL, *state_np;
  749. rt_err_t err = rt_ofw_parse_phandle_cells(cpu_np, "power-domains", "#power-domain-cells", 0, &args);
  750. if (!err)
  751. {
  752. state_np = rt_ofw_parse_phandle(args.data, "domain-idle-states", index);
  753. rt_ofw_node_put(args.data);
  754. if (state_np)
  755. {
  756. np = state_np;
  757. }
  758. }
  759. if (!np)
  760. {
  761. int count = 0;
  762. rt_uint32_t phandle;
  763. const fdt32_t *cell;
  764. struct rt_ofw_prop *prop;
  765. rt_ofw_foreach_prop_u32(cpu_np, "cpu-idle-states", prop, cell, phandle)
  766. {
  767. if (count == index)
  768. {
  769. np = rt_ofw_find_node_by_phandle((rt_phandle)phandle);
  770. break;
  771. }
  772. ++count;
  773. }
  774. }
  775. return np;
  776. }
  777. rt_uint64_t rt_ofw_get_cpu_id(struct rt_ofw_node *cpu_np)
  778. {
  779. rt_uint64_t cpuid = ~0ULL;
  780. if (cpu_np)
  781. {
  782. rt_uint64_t idx = 0;
  783. struct rt_ofw_node *np = ofw_node_cpus->child;
  784. for (; np; np = np->sibling)
  785. {
  786. if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
  787. {
  788. continue;
  789. }
  790. if (cpu_np == np)
  791. {
  792. cpuid = idx;
  793. break;
  794. }
  795. ++idx;
  796. }
  797. if ((rt_int64_t)cpuid < 0 && !rt_ofw_prop_read_u64(cpu_np, "rt-thread,cpuid", &idx))
  798. {
  799. cpuid = idx;
  800. }
  801. }
  802. return cpuid;
  803. }
  804. rt_uint64_t rt_ofw_get_cpu_hwid(struct rt_ofw_node *cpu_np, unsigned int thread)
  805. {
  806. rt_uint64_t thread_id, hwid = ~0ULL;
  807. if (cpu_np && thread >= 0 && !rt_ofw_get_address(cpu_np, thread, &thread_id, RT_NULL))
  808. {
  809. hwid = thread_id;
  810. }
  811. return hwid;
  812. }
  813. rt_err_t ofw_alias_scan(void)
  814. {
  815. rt_err_t err = RT_EOK;
  816. struct rt_ofw_prop *prop;
  817. struct rt_ofw_node *np = ofw_node_aliases, *tmp;
  818. rt_ofw_foreach_prop(np, prop)
  819. {
  820. int id = 0, rate = 1;
  821. struct alias_info *info;
  822. const char *name = prop->name, *end;
  823. /* Maybe the bootloader will set the name, or other nodes reference the aliases */
  824. if (!rt_strcmp(name, "name") || !rt_strcmp(name, "phandle"))
  825. {
  826. continue;
  827. }
  828. if (!(tmp = rt_ofw_find_node_by_path(prop->value)))
  829. {
  830. continue;
  831. }
  832. end = name + rt_strlen(name);
  833. while (*end && !(*end >= '0' && *end <= '9'))
  834. {
  835. --end;
  836. }
  837. while (*end && (*end >= '0' && *end <= '9'))
  838. {
  839. id += (*end - '0') * rate;
  840. rate *= 10;
  841. --end;
  842. }
  843. info = rt_malloc(sizeof(*info));
  844. if (!info)
  845. {
  846. err = -RT_ENOMEM;
  847. break;
  848. }
  849. rt_list_init(&info->list);
  850. info->id = id;
  851. info->tag = name;
  852. info->tag_len = end - name;
  853. info->np = tmp;
  854. rt_list_insert_after(&_aliases_nodes, &info->list);
  855. }
  856. return err;
  857. }
  858. struct rt_ofw_node *rt_ofw_get_alias_node(const char *tag, int id)
  859. {
  860. struct alias_info *info;
  861. struct rt_ofw_node *np = RT_NULL;
  862. if (tag && id >= 0)
  863. {
  864. rt_list_for_each_entry(info, &_aliases_nodes, list)
  865. {
  866. if (rt_strncmp(info->tag, tag, info->tag_len))
  867. {
  868. continue;
  869. }
  870. if (info->id == id)
  871. {
  872. np = info->np;
  873. break;
  874. }
  875. }
  876. }
  877. return np;
  878. }
  879. int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag)
  880. {
  881. int id;
  882. struct alias_info *info;
  883. if (np && tag)
  884. {
  885. id = -1;
  886. rt_list_for_each_entry(info, &_aliases_nodes, list)
  887. {
  888. if (rt_strncmp(info->tag, tag, info->tag_len))
  889. {
  890. continue;
  891. }
  892. if (info->np == np)
  893. {
  894. id = info->id;
  895. break;
  896. }
  897. }
  898. }
  899. else
  900. {
  901. id = -RT_EINVAL;
  902. }
  903. return id;
  904. }
  905. int rt_ofw_get_alias_last_id(const char *tag)
  906. {
  907. int id;
  908. struct alias_info *info;
  909. if (tag)
  910. {
  911. id = -1;
  912. rt_list_for_each_entry(info, &_aliases_nodes, list)
  913. {
  914. if (rt_strncmp(info->tag, tag, info->tag_len))
  915. {
  916. continue;
  917. }
  918. if (info->id > id)
  919. {
  920. id = info->id;
  921. }
  922. }
  923. }
  924. else
  925. {
  926. id = -RT_EINVAL;
  927. }
  928. return id;
  929. }
  930. struct rt_ofw_node *rt_ofw_parse_phandle(const struct rt_ofw_node *np, const char *phandle_name, int index)
  931. {
  932. struct rt_ofw_cell_args args;
  933. struct rt_ofw_node *ref_np = RT_NULL;
  934. if (!rt_ofw_parse_phandle_cells(np, phandle_name, RT_NULL, index, &args))
  935. {
  936. ref_np = args.data;
  937. }
  938. return ref_np;
  939. }
  940. static rt_err_t ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  941. int index, struct rt_ofw_cell_args *out_args)
  942. {
  943. rt_err_t err = -RT_EEMPTY;
  944. rt_uint32_t value;
  945. rt_size_t count = 0;
  946. const fdt32_t *cell;
  947. struct rt_ofw_prop *prop;
  948. /*
  949. * List:
  950. *
  951. * phandle1: node1 {
  952. * #list-cells = <2>;
  953. * };
  954. *
  955. * phandle2: node2 {
  956. * #list-cells = <1>;
  957. * };
  958. *
  959. * node3 {
  960. * list = <&phandle1 0xaa 0xbb>, <&phandle2 0xcc>;
  961. * };
  962. *
  963. * if call:
  964. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 0, &args):
  965. *
  966. * args.data = node1;
  967. * args.args_count = 2;
  968. * args.args[0] = 0xaa;
  969. * args.args[1] = 0xbb;
  970. *
  971. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 1, &args):
  972. *
  973. * args.data = node2;
  974. * args.args_count = 1;
  975. * args.args[0] = 0xcc;
  976. */
  977. rt_ofw_foreach_prop_u32(np, list_name, prop, cell, value)
  978. {
  979. rt_uint32_t cells_count = 0;
  980. struct rt_ofw_node *phandle_np = rt_ofw_find_node_by_phandle((rt_phandle)value);
  981. /* if phandle node is undefined, we assume that the cels_count is 0 */
  982. if (cells_name && phandle_np)
  983. {
  984. rt_ofw_prop_read_u32(phandle_np, cells_name, &cells_count);
  985. }
  986. if (count++ == index)
  987. {
  988. for (int idx = 0; idx < cells_count; ++idx)
  989. {
  990. cell = rt_ofw_prop_next_u32(prop, cell, &value);
  991. out_args->args[idx] = value;
  992. }
  993. out_args->args_count = cells_count;
  994. out_args->data = phandle_np;
  995. if (out_args->data)
  996. {
  997. err = RT_EOK;
  998. }
  999. break;
  1000. }
  1001. cell += cells_count;
  1002. }
  1003. return err;
  1004. }
  1005. rt_err_t rt_ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  1006. int index, struct rt_ofw_cell_args *out_args)
  1007. {
  1008. rt_err_t err;
  1009. if (np && list_name && index >= 0 && out_args)
  1010. {
  1011. err = ofw_parse_phandle_cells(np, list_name, cells_name, index, out_args);
  1012. }
  1013. else
  1014. {
  1015. err = -RT_EINVAL;
  1016. }
  1017. return err;
  1018. }
  1019. int rt_ofw_count_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name)
  1020. {
  1021. int count;
  1022. if (np && list_name)
  1023. {
  1024. count = -1;
  1025. if (!cells_name)
  1026. {
  1027. rt_ssize_t length;
  1028. if (rt_ofw_get_prop(np, list_name, &length))
  1029. {
  1030. count = length / sizeof(fdt32_t);
  1031. }
  1032. }
  1033. else
  1034. {
  1035. int index = count = 0;
  1036. struct rt_ofw_cell_args args;
  1037. while (!ofw_parse_phandle_cells(np, list_name, cells_name, index, &args))
  1038. {
  1039. ++index;
  1040. ++count;
  1041. }
  1042. }
  1043. }
  1044. else
  1045. {
  1046. count = -RT_EINVAL;
  1047. }
  1048. return count;
  1049. }
  1050. struct rt_ofw_prop *rt_ofw_get_prop(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length)
  1051. {
  1052. struct rt_ofw_prop *prop = RT_NULL;
  1053. if (np && name)
  1054. {
  1055. rt_ofw_foreach_prop(np, prop)
  1056. {
  1057. if (!rt_strcmp(prop->name, name))
  1058. {
  1059. if (out_length)
  1060. {
  1061. *out_length = prop->length;
  1062. }
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. return prop;
  1068. }
  1069. #define OFW_PROP_READ_UXX_ARRAY_INDEX(bit) \
  1070. int rt_ofw_prop_read_u##bit##_array_index( \
  1071. const struct rt_ofw_node *np, const char *propname, \
  1072. int index, int nr, rt_uint##bit##_t *out_values) \
  1073. { \
  1074. int res, max_nr; \
  1075. if (np && propname && index >= 0 && nr >= 0 && out_values) \
  1076. { \
  1077. rt_ssize_t len; \
  1078. const fdt##bit##_t *elm; \
  1079. elm = rt_ofw_prop_read_raw(np, propname, &len); \
  1080. max_nr = len / sizeof(*elm); \
  1081. if (elm && index < max_nr) \
  1082. { \
  1083. elm += index; \
  1084. max_nr -= index; \
  1085. res = nr > max_nr ? max_nr : nr; \
  1086. for (nr = 0; nr < res; ++nr) \
  1087. { \
  1088. *out_values++ = fdt##bit##_to_cpu(*elm++); \
  1089. } \
  1090. } \
  1091. else \
  1092. { \
  1093. res = -RT_EEMPTY; \
  1094. } \
  1095. } \
  1096. else \
  1097. { \
  1098. res = -RT_EINVAL; \
  1099. } \
  1100. return res; \
  1101. }
  1102. OFW_PROP_READ_UXX_ARRAY_INDEX(8)
  1103. OFW_PROP_READ_UXX_ARRAY_INDEX(16)
  1104. OFW_PROP_READ_UXX_ARRAY_INDEX(32)
  1105. OFW_PROP_READ_UXX_ARRAY_INDEX(64)
  1106. #undef OFW_PROP_READ_UXX_ARRAY_INDEX
  1107. int rt_ofw_prop_read_string_array_index(const struct rt_ofw_node *np, const char *propname,
  1108. int index, int nr, const char **out_strings)
  1109. {
  1110. int res = 0;
  1111. if (np && propname && index >= 0 && nr >= 0 && out_strings)
  1112. {
  1113. rt_ssize_t len, slen = 0;
  1114. const char *value = rt_ofw_prop_read_raw(np, propname, &len);
  1115. if (value)
  1116. {
  1117. nr += index;
  1118. for (int idx = 0; idx < nr && len > 0; ++idx)
  1119. {
  1120. /* Add '\0' */
  1121. slen = rt_strlen(value) + 1;
  1122. if (idx >= index)
  1123. {
  1124. *out_strings++ = value;
  1125. ++res;
  1126. }
  1127. len -= slen;
  1128. value += slen;
  1129. }
  1130. }
  1131. else
  1132. {
  1133. res = -RT_EEMPTY;
  1134. }
  1135. }
  1136. else
  1137. {
  1138. res = -RT_EINVAL;
  1139. }
  1140. return res;
  1141. }
  1142. int rt_ofw_prop_count_of_size(const struct rt_ofw_node *np, const char *propname, int size)
  1143. {
  1144. int count;
  1145. if (np && propname && size > 0)
  1146. {
  1147. rt_ssize_t len;
  1148. count = -RT_EEMPTY;
  1149. if (rt_ofw_get_prop(np, propname, &len))
  1150. {
  1151. count = len / size;
  1152. }
  1153. }
  1154. else
  1155. {
  1156. count = -RT_EINVAL;
  1157. }
  1158. return count;
  1159. }
  1160. static rt_int32_t ofw_strcmp(const char *cs, const char *ct)
  1161. {
  1162. return rt_strcmp(cs, ct);
  1163. }
  1164. int rt_ofw_prop_index_of_string(const struct rt_ofw_node *np, const char *propname, const char *string)
  1165. {
  1166. int idx;
  1167. if (np && propname && string)
  1168. {
  1169. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
  1170. idx = -1;
  1171. if (prop)
  1172. {
  1173. idx = ofw_prop_index_of_string(prop, string, ofw_strcmp);
  1174. }
  1175. }
  1176. else
  1177. {
  1178. idx = -RT_EINVAL;
  1179. }
  1180. return idx;
  1181. }
  1182. const fdt32_t *rt_ofw_prop_next_u32(struct rt_ofw_prop *prop, const fdt32_t *cur, rt_uint32_t *out_value)
  1183. {
  1184. if (prop && out_value)
  1185. {
  1186. if (cur)
  1187. {
  1188. ++cur;
  1189. if ((void *)cur >= prop->value + prop->length)
  1190. {
  1191. cur = RT_NULL;
  1192. }
  1193. }
  1194. else
  1195. {
  1196. cur = prop->value;
  1197. }
  1198. if (cur)
  1199. {
  1200. *out_value = fdt32_to_cpu(*cur);
  1201. }
  1202. }
  1203. else
  1204. {
  1205. cur = RT_NULL;
  1206. }
  1207. return cur;
  1208. }
  1209. const char *rt_ofw_prop_next_string(struct rt_ofw_prop *prop, const char *cur)
  1210. {
  1211. if (prop)
  1212. {
  1213. if (cur)
  1214. {
  1215. cur += rt_strlen(cur) + 1;
  1216. if ((void *)cur >= prop->value + prop->length)
  1217. {
  1218. cur = RT_NULL;
  1219. }
  1220. }
  1221. else
  1222. {
  1223. cur = prop->value;
  1224. }
  1225. }
  1226. else
  1227. {
  1228. cur = RT_NULL;
  1229. }
  1230. return cur;
  1231. }