base.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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. #define _GNU_SOURCE
  11. #include <rtthread.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. }