base.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
  13. * Grant Likely.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #define pr_fmt(fmt) "OF: " fmt
  21. #include <linux/console.h>
  22. #include <linux/ctype.h>
  23. #include <linux/cpu.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_graph.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/proc_fs.h>
  32. #include "of_private.h"
  33. LIST_HEAD(aliases_lookup);
  34. struct device_node *of_root;
  35. EXPORT_SYMBOL(of_root);
  36. struct device_node *of_chosen;
  37. struct device_node *of_aliases;
  38. struct device_node *of_stdout;
  39. static const char *of_stdout_options;
  40. struct kset *of_kset;
  41. /*
  42. * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
  43. * This mutex must be held whenever modifications are being made to the
  44. * device tree. The of_{attach,detach}_node() and
  45. * of_{add,remove,update}_property() helpers make sure this happens.
  46. */
  47. DEFINE_MUTEX(of_mutex);
  48. /* use when traversing tree through the child, sibling,
  49. * or parent members of struct device_node.
  50. */
  51. DEFINE_RAW_SPINLOCK(devtree_lock);
  52. int of_n_addr_cells(struct device_node *np)
  53. {
  54. u32 cells;
  55. do {
  56. if (np->parent)
  57. np = np->parent;
  58. if (!of_property_read_u32(np, "#address-cells", &cells))
  59. return cells;
  60. } while (np->parent);
  61. /* No #address-cells property for the root node */
  62. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  63. }
  64. EXPORT_SYMBOL(of_n_addr_cells);
  65. int of_n_size_cells(struct device_node *np)
  66. {
  67. u32 cells;
  68. do {
  69. if (np->parent)
  70. np = np->parent;
  71. if (!of_property_read_u32(np, "#size-cells", &cells))
  72. return cells;
  73. } while (np->parent);
  74. /* No #size-cells property for the root node */
  75. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  76. }
  77. EXPORT_SYMBOL(of_n_size_cells);
  78. #ifdef CONFIG_NUMA
  79. int __weak of_node_to_nid(struct device_node *np)
  80. {
  81. return NUMA_NO_NODE;
  82. }
  83. #endif
  84. #ifndef CONFIG_OF_DYNAMIC
  85. static void of_node_release(struct kobject *kobj)
  86. {
  87. /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
  88. }
  89. #endif /* CONFIG_OF_DYNAMIC */
  90. struct kobj_type of_node_ktype = {
  91. .release = of_node_release,
  92. };
  93. static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
  94. struct bin_attribute *bin_attr, char *buf,
  95. loff_t offset, size_t count)
  96. {
  97. struct property *pp = container_of(bin_attr, struct property, attr);
  98. return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
  99. }
  100. /* always return newly allocated name, caller must free after use */
  101. static const char *safe_name(struct kobject *kobj, const char *orig_name)
  102. {
  103. const char *name = orig_name;
  104. struct kernfs_node *kn;
  105. int i = 0;
  106. /* don't be a hero. After 16 tries give up */
  107. while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
  108. sysfs_put(kn);
  109. if (name != orig_name)
  110. kfree(name);
  111. name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
  112. }
  113. if (name == orig_name) {
  114. name = kstrdup(orig_name, GFP_KERNEL);
  115. } else {
  116. pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
  117. kobject_name(kobj), name);
  118. }
  119. return name;
  120. }
  121. int __of_add_property_sysfs(struct device_node *np, struct property *pp)
  122. {
  123. int rc;
  124. /* Important: Don't leak passwords */
  125. bool secure = strncmp(pp->name, "security-", 9) == 0;
  126. if (!IS_ENABLED(CONFIG_SYSFS))
  127. return 0;
  128. if (!of_kset || !of_node_is_attached(np))
  129. return 0;
  130. sysfs_bin_attr_init(&pp->attr);
  131. pp->attr.attr.name = safe_name(&np->kobj, pp->name);
  132. pp->attr.attr.mode = secure ? 0400 : 0444;
  133. pp->attr.size = secure ? 0 : pp->length;
  134. pp->attr.read = of_node_property_read;
  135. rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
  136. WARN(rc, "error adding attribute %s to node %pOF\n", pp->name, np);
  137. return rc;
  138. }
  139. int __of_attach_node_sysfs(struct device_node *np)
  140. {
  141. const char *name;
  142. struct kobject *parent;
  143. struct property *pp;
  144. int rc;
  145. if (!of_kset)
  146. return 0;
  147. np->kobj.kset = of_kset;
  148. if (!np->parent) {
  149. /* Nodes without parents are new top level trees */
  150. name = safe_name(&of_kset->kobj, "base");
  151. parent = NULL;
  152. } else {
  153. name = safe_name(&np->parent->kobj, kbasename(np->full_name));
  154. parent = &np->parent->kobj;
  155. }
  156. if (!name)
  157. return -ENOMEM;
  158. rc = kobject_add(&np->kobj, parent, "%s", name);
  159. kfree(name);
  160. if (rc)
  161. return rc;
  162. for_each_property_of_node(np, pp)
  163. __of_add_property_sysfs(np, pp);
  164. return 0;
  165. }
  166. void __init of_core_init(void)
  167. {
  168. struct device_node *np;
  169. /* Create the kset, and register existing nodes */
  170. mutex_lock(&of_mutex);
  171. of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
  172. if (!of_kset) {
  173. mutex_unlock(&of_mutex);
  174. pr_err("failed to register existing nodes\n");
  175. return;
  176. }
  177. for_each_of_allnodes(np)
  178. __of_attach_node_sysfs(np);
  179. mutex_unlock(&of_mutex);
  180. /* Symlink in /proc as required by userspace ABI */
  181. if (of_root)
  182. proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
  183. }
  184. static struct property *__of_find_property(const struct device_node *np,
  185. const char *name, int *lenp)
  186. {
  187. struct property *pp;
  188. if (!np)
  189. return NULL;
  190. for (pp = np->properties; pp; pp = pp->next) {
  191. if (of_prop_cmp(pp->name, name) == 0) {
  192. if (lenp)
  193. *lenp = pp->length;
  194. break;
  195. }
  196. }
  197. return pp;
  198. }
  199. struct property *of_find_property(const struct device_node *np,
  200. const char *name,
  201. int *lenp)
  202. {
  203. struct property *pp;
  204. unsigned long flags;
  205. raw_spin_lock_irqsave(&devtree_lock, flags);
  206. pp = __of_find_property(np, name, lenp);
  207. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  208. return pp;
  209. }
  210. EXPORT_SYMBOL(of_find_property);
  211. struct device_node *__of_find_all_nodes(struct device_node *prev)
  212. {
  213. struct device_node *np;
  214. if (!prev) {
  215. np = of_root;
  216. } else if (prev->child) {
  217. np = prev->child;
  218. } else {
  219. /* Walk back up looking for a sibling, or the end of the structure */
  220. np = prev;
  221. while (np->parent && !np->sibling)
  222. np = np->parent;
  223. np = np->sibling; /* Might be null at the end of the tree */
  224. }
  225. return np;
  226. }
  227. /**
  228. * of_find_all_nodes - Get next node in global list
  229. * @prev: Previous node or NULL to start iteration
  230. * of_node_put() will be called on it
  231. *
  232. * Returns a node pointer with refcount incremented, use
  233. * of_node_put() on it when done.
  234. */
  235. struct device_node *of_find_all_nodes(struct device_node *prev)
  236. {
  237. struct device_node *np;
  238. unsigned long flags;
  239. raw_spin_lock_irqsave(&devtree_lock, flags);
  240. np = __of_find_all_nodes(prev);
  241. of_node_get(np);
  242. of_node_put(prev);
  243. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  244. return np;
  245. }
  246. EXPORT_SYMBOL(of_find_all_nodes);
  247. /*
  248. * Find a property with a given name for a given node
  249. * and return the value.
  250. */
  251. const void *__of_get_property(const struct device_node *np,
  252. const char *name, int *lenp)
  253. {
  254. struct property *pp = __of_find_property(np, name, lenp);
  255. return pp ? pp->value : NULL;
  256. }
  257. /*
  258. * Find a property with a given name for a given node
  259. * and return the value.
  260. */
  261. const void *of_get_property(const struct device_node *np, const char *name,
  262. int *lenp)
  263. {
  264. struct property *pp = of_find_property(np, name, lenp);
  265. return pp ? pp->value : NULL;
  266. }
  267. EXPORT_SYMBOL(of_get_property);
  268. /*
  269. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  270. *
  271. * @cpu: logical cpu index of a core/thread
  272. * @phys_id: physical identifier of a core/thread
  273. *
  274. * CPU logical to physical index mapping is architecture specific.
  275. * However this __weak function provides a default match of physical
  276. * id to logical cpu index. phys_id provided here is usually values read
  277. * from the device tree which must match the hardware internal registers.
  278. *
  279. * Returns true if the physical identifier and the logical cpu index
  280. * correspond to the same core/thread, false otherwise.
  281. */
  282. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  283. {
  284. return (u32)phys_id == cpu;
  285. }
  286. /**
  287. * Checks if the given "prop_name" property holds the physical id of the
  288. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  289. * NULL, local thread number within the core is returned in it.
  290. */
  291. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  292. const char *prop_name, int cpu, unsigned int *thread)
  293. {
  294. const __be32 *cell;
  295. int ac, prop_len, tid;
  296. u64 hwid;
  297. ac = of_n_addr_cells(cpun);
  298. cell = of_get_property(cpun, prop_name, &prop_len);
  299. if (!cell || !ac)
  300. return false;
  301. prop_len /= sizeof(*cell) * ac;
  302. for (tid = 0; tid < prop_len; tid++) {
  303. hwid = of_read_number(cell, ac);
  304. if (arch_match_cpu_phys_id(cpu, hwid)) {
  305. if (thread)
  306. *thread = tid;
  307. return true;
  308. }
  309. cell += ac;
  310. }
  311. return false;
  312. }
  313. /*
  314. * arch_find_n_match_cpu_physical_id - See if the given device node is
  315. * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
  316. * else false. If 'thread' is non-NULL, the local thread number within the
  317. * core is returned in it.
  318. */
  319. bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  320. int cpu, unsigned int *thread)
  321. {
  322. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  323. * for thread ids on PowerPC. If it doesn't exist fallback to
  324. * standard "reg" property.
  325. */
  326. if (IS_ENABLED(CONFIG_PPC) &&
  327. __of_find_n_match_cpu_property(cpun,
  328. "ibm,ppc-interrupt-server#s",
  329. cpu, thread))
  330. return true;
  331. return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
  332. }
  333. /**
  334. * of_get_cpu_node - Get device node associated with the given logical CPU
  335. *
  336. * @cpu: CPU number(logical index) for which device node is required
  337. * @thread: if not NULL, local thread number within the physical core is
  338. * returned
  339. *
  340. * The main purpose of this function is to retrieve the device node for the
  341. * given logical CPU index. It should be used to initialize the of_node in
  342. * cpu device. Once of_node in cpu device is populated, all the further
  343. * references can use that instead.
  344. *
  345. * CPU logical to physical index mapping is architecture specific and is built
  346. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  347. * which can be overridden by architecture specific implementation.
  348. *
  349. * Returns a node pointer for the logical cpu with refcount incremented, use
  350. * of_node_put() on it when done. Returns NULL if not found.
  351. */
  352. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  353. {
  354. struct device_node *cpun;
  355. for_each_node_by_type(cpun, "cpu") {
  356. if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
  357. return cpun;
  358. }
  359. return NULL;
  360. }
  361. EXPORT_SYMBOL(of_get_cpu_node);
  362. /**
  363. * __of_device_is_compatible() - Check if the node matches given constraints
  364. * @device: pointer to node
  365. * @compat: required compatible string, NULL or "" for any match
  366. * @type: required device_type value, NULL or "" for any match
  367. * @name: required node name, NULL or "" for any match
  368. *
  369. * Checks if the given @compat, @type and @name strings match the
  370. * properties of the given @device. A constraints can be skipped by
  371. * passing NULL or an empty string as the constraint.
  372. *
  373. * Returns 0 for no match, and a positive integer on match. The return
  374. * value is a relative score with larger values indicating better
  375. * matches. The score is weighted for the most specific compatible value
  376. * to get the highest score. Matching type is next, followed by matching
  377. * name. Practically speaking, this results in the following priority
  378. * order for matches:
  379. *
  380. * 1. specific compatible && type && name
  381. * 2. specific compatible && type
  382. * 3. specific compatible && name
  383. * 4. specific compatible
  384. * 5. general compatible && type && name
  385. * 6. general compatible && type
  386. * 7. general compatible && name
  387. * 8. general compatible
  388. * 9. type && name
  389. * 10. type
  390. * 11. name
  391. */
  392. static int __of_device_is_compatible(const struct device_node *device,
  393. const char *compat, const char *type, const char *name)
  394. {
  395. struct property *prop;
  396. const char *cp;
  397. int index = 0, score = 0;
  398. /* Compatible match has highest priority */
  399. if (compat && compat[0]) {
  400. prop = __of_find_property(device, "compatible", NULL);
  401. for (cp = of_prop_next_string(prop, NULL); cp;
  402. cp = of_prop_next_string(prop, cp), index++) {
  403. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  404. score = INT_MAX/2 - (index << 2);
  405. break;
  406. }
  407. }
  408. if (!score)
  409. return 0;
  410. }
  411. /* Matching type is better than matching name */
  412. if (type && type[0]) {
  413. if (!device->type || of_node_cmp(type, device->type))
  414. return 0;
  415. score += 2;
  416. }
  417. /* Matching name is a bit better than not */
  418. if (name && name[0]) {
  419. if (!device->name || of_node_cmp(name, device->name))
  420. return 0;
  421. score++;
  422. }
  423. return score;
  424. }
  425. /** Checks if the given "compat" string matches one of the strings in
  426. * the device's "compatible" property
  427. */
  428. int of_device_is_compatible(const struct device_node *device,
  429. const char *compat)
  430. {
  431. unsigned long flags;
  432. int res;
  433. raw_spin_lock_irqsave(&devtree_lock, flags);
  434. res = __of_device_is_compatible(device, compat, NULL, NULL);
  435. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  436. return res;
  437. }
  438. EXPORT_SYMBOL(of_device_is_compatible);
  439. /** Checks if the device is compatible with any of the entries in
  440. * a NULL terminated array of strings. Returns the best match
  441. * score or 0.
  442. */
  443. int of_device_compatible_match(struct device_node *device,
  444. const char *const *compat)
  445. {
  446. unsigned int tmp, score = 0;
  447. if (!compat)
  448. return 0;
  449. while (*compat) {
  450. tmp = of_device_is_compatible(device, *compat);
  451. if (tmp > score)
  452. score = tmp;
  453. compat++;
  454. }
  455. return score;
  456. }
  457. /**
  458. * of_machine_is_compatible - Test root of device tree for a given compatible value
  459. * @compat: compatible string to look for in root node's compatible property.
  460. *
  461. * Returns a positive integer if the root node has the given value in its
  462. * compatible property.
  463. */
  464. int of_machine_is_compatible(const char *compat)
  465. {
  466. struct device_node *root;
  467. int rc = 0;
  468. root = of_find_node_by_path("/");
  469. if (root) {
  470. rc = of_device_is_compatible(root, compat);
  471. of_node_put(root);
  472. }
  473. return rc;
  474. }
  475. EXPORT_SYMBOL(of_machine_is_compatible);
  476. /**
  477. * __of_device_is_available - check if a device is available for use
  478. *
  479. * @device: Node to check for availability, with locks already held
  480. *
  481. * Returns true if the status property is absent or set to "okay" or "ok",
  482. * false otherwise
  483. */
  484. static bool __of_device_is_available(const struct device_node *device)
  485. {
  486. const char *status;
  487. int statlen;
  488. if (!device)
  489. return false;
  490. status = __of_get_property(device, "status", &statlen);
  491. if (status == NULL)
  492. return true;
  493. if (statlen > 0) {
  494. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  495. return true;
  496. }
  497. return false;
  498. }
  499. /**
  500. * of_device_is_available - check if a device is available for use
  501. *
  502. * @device: Node to check for availability
  503. *
  504. * Returns true if the status property is absent or set to "okay" or "ok",
  505. * false otherwise
  506. */
  507. bool of_device_is_available(const struct device_node *device)
  508. {
  509. unsigned long flags;
  510. bool res;
  511. raw_spin_lock_irqsave(&devtree_lock, flags);
  512. res = __of_device_is_available(device);
  513. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  514. return res;
  515. }
  516. EXPORT_SYMBOL(of_device_is_available);
  517. /**
  518. * of_device_is_big_endian - check if a device has BE registers
  519. *
  520. * @device: Node to check for endianness
  521. *
  522. * Returns true if the device has a "big-endian" property, or if the kernel
  523. * was compiled for BE *and* the device has a "native-endian" property.
  524. * Returns false otherwise.
  525. *
  526. * Callers would nominally use ioread32be/iowrite32be if
  527. * of_device_is_big_endian() == true, or readl/writel otherwise.
  528. */
  529. bool of_device_is_big_endian(const struct device_node *device)
  530. {
  531. if (of_property_read_bool(device, "big-endian"))
  532. return true;
  533. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  534. of_property_read_bool(device, "native-endian"))
  535. return true;
  536. return false;
  537. }
  538. EXPORT_SYMBOL(of_device_is_big_endian);
  539. /**
  540. * of_get_parent - Get a node's parent if any
  541. * @node: Node to get parent
  542. *
  543. * Returns a node pointer with refcount incremented, use
  544. * of_node_put() on it when done.
  545. */
  546. struct device_node *of_get_parent(const struct device_node *node)
  547. {
  548. struct device_node *np;
  549. unsigned long flags;
  550. if (!node)
  551. return NULL;
  552. raw_spin_lock_irqsave(&devtree_lock, flags);
  553. np = of_node_get(node->parent);
  554. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  555. return np;
  556. }
  557. EXPORT_SYMBOL(of_get_parent);
  558. /**
  559. * of_get_next_parent - Iterate to a node's parent
  560. * @node: Node to get parent of
  561. *
  562. * This is like of_get_parent() except that it drops the
  563. * refcount on the passed node, making it suitable for iterating
  564. * through a node's parents.
  565. *
  566. * Returns a node pointer with refcount incremented, use
  567. * of_node_put() on it when done.
  568. */
  569. struct device_node *of_get_next_parent(struct device_node *node)
  570. {
  571. struct device_node *parent;
  572. unsigned long flags;
  573. if (!node)
  574. return NULL;
  575. raw_spin_lock_irqsave(&devtree_lock, flags);
  576. parent = of_node_get(node->parent);
  577. of_node_put(node);
  578. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  579. return parent;
  580. }
  581. EXPORT_SYMBOL(of_get_next_parent);
  582. static struct device_node *__of_get_next_child(const struct device_node *node,
  583. struct device_node *prev)
  584. {
  585. struct device_node *next;
  586. if (!node)
  587. return NULL;
  588. next = prev ? prev->sibling : node->child;
  589. for (; next; next = next->sibling)
  590. if (of_node_get(next))
  591. break;
  592. of_node_put(prev);
  593. return next;
  594. }
  595. #define __for_each_child_of_node(parent, child) \
  596. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  597. child = __of_get_next_child(parent, child))
  598. /**
  599. * of_get_next_child - Iterate a node childs
  600. * @node: parent node
  601. * @prev: previous child of the parent node, or NULL to get first
  602. *
  603. * Returns a node pointer with refcount incremented, use of_node_put() on
  604. * it when done. Returns NULL when prev is the last child. Decrements the
  605. * refcount of prev.
  606. */
  607. struct device_node *of_get_next_child(const struct device_node *node,
  608. struct device_node *prev)
  609. {
  610. struct device_node *next;
  611. unsigned long flags;
  612. raw_spin_lock_irqsave(&devtree_lock, flags);
  613. next = __of_get_next_child(node, prev);
  614. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  615. return next;
  616. }
  617. EXPORT_SYMBOL(of_get_next_child);
  618. /**
  619. * of_get_next_available_child - Find the next available child node
  620. * @node: parent node
  621. * @prev: previous child of the parent node, or NULL to get first
  622. *
  623. * This function is like of_get_next_child(), except that it
  624. * automatically skips any disabled nodes (i.e. status = "disabled").
  625. */
  626. struct device_node *of_get_next_available_child(const struct device_node *node,
  627. struct device_node *prev)
  628. {
  629. struct device_node *next;
  630. unsigned long flags;
  631. if (!node)
  632. return NULL;
  633. raw_spin_lock_irqsave(&devtree_lock, flags);
  634. next = prev ? prev->sibling : node->child;
  635. for (; next; next = next->sibling) {
  636. if (!__of_device_is_available(next))
  637. continue;
  638. if (of_node_get(next))
  639. break;
  640. }
  641. of_node_put(prev);
  642. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  643. return next;
  644. }
  645. EXPORT_SYMBOL(of_get_next_available_child);
  646. /**
  647. * of_get_compatible_child - Find compatible child node
  648. * @parent: parent node
  649. * @compatible: compatible string
  650. *
  651. * Lookup child node whose compatible property contains the given compatible
  652. * string.
  653. *
  654. * Returns a node pointer with refcount incremented, use of_node_put() on it
  655. * when done; or NULL if not found.
  656. */
  657. struct device_node *of_get_compatible_child(const struct device_node *parent,
  658. const char *compatible)
  659. {
  660. struct device_node *child;
  661. for_each_child_of_node(parent, child) {
  662. if (of_device_is_compatible(child, compatible))
  663. break;
  664. }
  665. return child;
  666. }
  667. EXPORT_SYMBOL(of_get_compatible_child);
  668. /**
  669. * of_get_child_by_name - Find the child node by name for a given parent
  670. * @node: parent node
  671. * @name: child name to look for.
  672. *
  673. * This function looks for child node for given matching name
  674. *
  675. * Returns a node pointer if found, with refcount incremented, use
  676. * of_node_put() on it when done.
  677. * Returns NULL if node is not found.
  678. */
  679. struct device_node *of_get_child_by_name(const struct device_node *node,
  680. const char *name)
  681. {
  682. struct device_node *child;
  683. for_each_child_of_node(node, child)
  684. if (child->name && (of_node_cmp(child->name, name) == 0))
  685. break;
  686. return child;
  687. }
  688. EXPORT_SYMBOL(of_get_child_by_name);
  689. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  690. const char *path)
  691. {
  692. struct device_node *child;
  693. int len;
  694. len = strcspn(path, "/:");
  695. if (!len)
  696. return NULL;
  697. __for_each_child_of_node(parent, child) {
  698. const char *name = kbasename(child->full_name);
  699. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  700. return child;
  701. }
  702. return NULL;
  703. }
  704. struct device_node *__of_find_node_by_full_path(struct device_node *node,
  705. const char *path)
  706. {
  707. const char *separator = strchr(path, ':');
  708. while (node && *path == '/') {
  709. struct device_node *tmp = node;
  710. path++; /* Increment past '/' delimiter */
  711. node = __of_find_node_by_path(node, path);
  712. of_node_put(tmp);
  713. path = strchrnul(path, '/');
  714. if (separator && separator < path)
  715. break;
  716. }
  717. return node;
  718. }
  719. /**
  720. * of_find_node_opts_by_path - Find a node matching a full OF path
  721. * @path: Either the full path to match, or if the path does not
  722. * start with '/', the name of a property of the /aliases
  723. * node (an alias). In the case of an alias, the node
  724. * matching the alias' value will be returned.
  725. * @opts: Address of a pointer into which to store the start of
  726. * an options string appended to the end of the path with
  727. * a ':' separator.
  728. *
  729. * Valid paths:
  730. * /foo/bar Full path
  731. * foo Valid alias
  732. * foo/bar Valid alias + relative path
  733. *
  734. * Returns a node pointer with refcount incremented, use
  735. * of_node_put() on it when done.
  736. */
  737. struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
  738. {
  739. struct device_node *np = NULL;
  740. struct property *pp;
  741. unsigned long flags;
  742. const char *separator = strchr(path, ':');
  743. if (opts)
  744. *opts = separator ? separator + 1 : NULL;
  745. if (strcmp(path, "/") == 0)
  746. return of_node_get(of_root);
  747. /* The path could begin with an alias */
  748. if (*path != '/') {
  749. int len;
  750. const char *p = separator;
  751. if (!p)
  752. p = strchrnul(path, '/');
  753. len = p - path;
  754. /* of_aliases must not be NULL */
  755. if (!of_aliases)
  756. return NULL;
  757. for_each_property_of_node(of_aliases, pp) {
  758. if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
  759. np = of_find_node_by_path(pp->value);
  760. break;
  761. }
  762. }
  763. if (!np)
  764. return NULL;
  765. path = p;
  766. }
  767. /* Step down the tree matching path components */
  768. raw_spin_lock_irqsave(&devtree_lock, flags);
  769. if (!np)
  770. np = of_node_get(of_root);
  771. np = __of_find_node_by_full_path(np, path);
  772. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  773. return np;
  774. }
  775. EXPORT_SYMBOL(of_find_node_opts_by_path);
  776. /**
  777. * of_find_node_by_name - Find a node by its "name" property
  778. * @from: The node to start searching from or NULL, the node
  779. * you pass will not be searched, only the next one
  780. * will; typically, you pass what the previous call
  781. * returned. of_node_put() will be called on it
  782. * @name: The name string to match against
  783. *
  784. * Returns a node pointer with refcount incremented, use
  785. * of_node_put() on it when done.
  786. */
  787. struct device_node *of_find_node_by_name(struct device_node *from,
  788. const char *name)
  789. {
  790. struct device_node *np;
  791. unsigned long flags;
  792. raw_spin_lock_irqsave(&devtree_lock, flags);
  793. for_each_of_allnodes_from(from, np)
  794. if (np->name && (of_node_cmp(np->name, name) == 0)
  795. && of_node_get(np))
  796. break;
  797. of_node_put(from);
  798. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  799. return np;
  800. }
  801. EXPORT_SYMBOL(of_find_node_by_name);
  802. /**
  803. * of_find_node_by_type - Find a node by its "device_type" property
  804. * @from: The node to start searching from, or NULL to start searching
  805. * the entire device tree. The node you pass will not be
  806. * searched, only the next one will; typically, you pass
  807. * what the previous call returned. of_node_put() will be
  808. * called on from for you.
  809. * @type: The type string to match against
  810. *
  811. * Returns a node pointer with refcount incremented, use
  812. * of_node_put() on it when done.
  813. */
  814. struct device_node *of_find_node_by_type(struct device_node *from,
  815. const char *type)
  816. {
  817. struct device_node *np;
  818. unsigned long flags;
  819. raw_spin_lock_irqsave(&devtree_lock, flags);
  820. for_each_of_allnodes_from(from, np)
  821. if (np->type && (of_node_cmp(np->type, type) == 0)
  822. && of_node_get(np))
  823. break;
  824. of_node_put(from);
  825. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  826. return np;
  827. }
  828. EXPORT_SYMBOL(of_find_node_by_type);
  829. /**
  830. * of_find_compatible_node - Find a node based on type and one of the
  831. * tokens in its "compatible" property
  832. * @from: The node to start searching from or NULL, the node
  833. * you pass will not be searched, only the next one
  834. * will; typically, you pass what the previous call
  835. * returned. of_node_put() will be called on it
  836. * @type: The type string to match "device_type" or NULL to ignore
  837. * @compatible: The string to match to one of the tokens in the device
  838. * "compatible" list.
  839. *
  840. * Returns a node pointer with refcount incremented, use
  841. * of_node_put() on it when done.
  842. */
  843. struct device_node *of_find_compatible_node(struct device_node *from,
  844. const char *type, const char *compatible)
  845. {
  846. struct device_node *np;
  847. unsigned long flags;
  848. raw_spin_lock_irqsave(&devtree_lock, flags);
  849. for_each_of_allnodes_from(from, np)
  850. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  851. of_node_get(np))
  852. break;
  853. of_node_put(from);
  854. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  855. return np;
  856. }
  857. EXPORT_SYMBOL(of_find_compatible_node);
  858. /**
  859. * of_find_node_with_property - Find a node which has a property with
  860. * the given name.
  861. * @from: The node to start searching from or NULL, the node
  862. * you pass will not be searched, only the next one
  863. * will; typically, you pass what the previous call
  864. * returned. of_node_put() will be called on it
  865. * @prop_name: The name of the property to look for.
  866. *
  867. * Returns a node pointer with refcount incremented, use
  868. * of_node_put() on it when done.
  869. */
  870. struct device_node *of_find_node_with_property(struct device_node *from,
  871. const char *prop_name)
  872. {
  873. struct device_node *np;
  874. struct property *pp;
  875. unsigned long flags;
  876. raw_spin_lock_irqsave(&devtree_lock, flags);
  877. for_each_of_allnodes_from(from, np) {
  878. for (pp = np->properties; pp; pp = pp->next) {
  879. if (of_prop_cmp(pp->name, prop_name) == 0) {
  880. of_node_get(np);
  881. goto out;
  882. }
  883. }
  884. }
  885. out:
  886. of_node_put(from);
  887. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  888. return np;
  889. }
  890. EXPORT_SYMBOL(of_find_node_with_property);
  891. static
  892. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  893. const struct device_node *node)
  894. {
  895. const struct of_device_id *best_match = NULL;
  896. int score, best_score = 0;
  897. if (!matches)
  898. return NULL;
  899. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  900. score = __of_device_is_compatible(node, matches->compatible,
  901. matches->type, matches->name);
  902. if (score > best_score) {
  903. best_match = matches;
  904. best_score = score;
  905. }
  906. }
  907. return best_match;
  908. }
  909. /**
  910. * of_match_node - Tell if a device_node has a matching of_match structure
  911. * @matches: array of of device match structures to search in
  912. * @node: the of device structure to match against
  913. *
  914. * Low level utility function used by device matching.
  915. */
  916. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  917. const struct device_node *node)
  918. {
  919. const struct of_device_id *match;
  920. unsigned long flags;
  921. raw_spin_lock_irqsave(&devtree_lock, flags);
  922. match = __of_match_node(matches, node);
  923. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  924. return match;
  925. }
  926. EXPORT_SYMBOL(of_match_node);
  927. /**
  928. * of_find_matching_node_and_match - Find a node based on an of_device_id
  929. * match table.
  930. * @from: The node to start searching from or NULL, the node
  931. * you pass will not be searched, only the next one
  932. * will; typically, you pass what the previous call
  933. * returned. of_node_put() will be called on it
  934. * @matches: array of of device match structures to search in
  935. * @match Updated to point at the matches entry which matched
  936. *
  937. * Returns a node pointer with refcount incremented, use
  938. * of_node_put() on it when done.
  939. */
  940. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  941. const struct of_device_id *matches,
  942. const struct of_device_id **match)
  943. {
  944. struct device_node *np;
  945. const struct of_device_id *m;
  946. unsigned long flags;
  947. if (match)
  948. *match = NULL;
  949. raw_spin_lock_irqsave(&devtree_lock, flags);
  950. for_each_of_allnodes_from(from, np) {
  951. m = __of_match_node(matches, np);
  952. if (m && of_node_get(np)) {
  953. if (match)
  954. *match = m;
  955. break;
  956. }
  957. }
  958. of_node_put(from);
  959. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  960. return np;
  961. }
  962. EXPORT_SYMBOL(of_find_matching_node_and_match);
  963. /**
  964. * of_modalias_node - Lookup appropriate modalias for a device node
  965. * @node: pointer to a device tree node
  966. * @modalias: Pointer to buffer that modalias value will be copied into
  967. * @len: Length of modalias value
  968. *
  969. * Based on the value of the compatible property, this routine will attempt
  970. * to choose an appropriate modalias value for a particular device tree node.
  971. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  972. * from the first entry in the compatible list property.
  973. *
  974. * This routine returns 0 on success, <0 on failure.
  975. */
  976. int of_modalias_node(struct device_node *node, char *modalias, int len)
  977. {
  978. const char *compatible, *p;
  979. int cplen;
  980. compatible = of_get_property(node, "compatible", &cplen);
  981. if (!compatible || strlen(compatible) > cplen)
  982. return -ENODEV;
  983. p = strchr(compatible, ',');
  984. strlcpy(modalias, p ? p + 1 : compatible, len);
  985. return 0;
  986. }
  987. EXPORT_SYMBOL_GPL(of_modalias_node);
  988. /**
  989. * of_find_node_by_phandle - Find a node given a phandle
  990. * @handle: phandle of the node to find
  991. *
  992. * Returns a node pointer with refcount incremented, use
  993. * of_node_put() on it when done.
  994. */
  995. struct device_node *of_find_node_by_phandle(phandle handle)
  996. {
  997. struct device_node *np;
  998. unsigned long flags;
  999. if (!handle)
  1000. return NULL;
  1001. raw_spin_lock_irqsave(&devtree_lock, flags);
  1002. for_each_of_allnodes(np)
  1003. if (np->phandle == handle)
  1004. break;
  1005. of_node_get(np);
  1006. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1007. return np;
  1008. }
  1009. EXPORT_SYMBOL(of_find_node_by_phandle);
  1010. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1011. {
  1012. int i;
  1013. printk("%s %pOF", msg, args->np);
  1014. for (i = 0; i < args->args_count; i++) {
  1015. const char delim = i ? ',' : ':';
  1016. pr_cont("%c%08x", delim, args->args[i]);
  1017. }
  1018. pr_cont("\n");
  1019. }
  1020. int of_phandle_iterator_init(struct of_phandle_iterator *it,
  1021. const struct device_node *np,
  1022. const char *list_name,
  1023. const char *cells_name,
  1024. int cell_count)
  1025. {
  1026. const __be32 *list;
  1027. int size;
  1028. memset(it, 0, sizeof(*it));
  1029. list = of_get_property(np, list_name, &size);
  1030. if (!list)
  1031. return -ENOENT;
  1032. it->cells_name = cells_name;
  1033. it->cell_count = cell_count;
  1034. it->parent = np;
  1035. it->list_end = list + size / sizeof(*list);
  1036. it->phandle_end = list;
  1037. it->cur = list;
  1038. return 0;
  1039. }
  1040. EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
  1041. int of_phandle_iterator_next(struct of_phandle_iterator *it)
  1042. {
  1043. uint32_t count = 0;
  1044. if (it->node) {
  1045. of_node_put(it->node);
  1046. it->node = NULL;
  1047. }
  1048. if (!it->cur || it->phandle_end >= it->list_end)
  1049. return -ENOENT;
  1050. it->cur = it->phandle_end;
  1051. /* If phandle is 0, then it is an empty entry with no arguments. */
  1052. it->phandle = be32_to_cpup(it->cur++);
  1053. if (it->phandle) {
  1054. /*
  1055. * Find the provider node and parse the #*-cells property to
  1056. * determine the argument length.
  1057. */
  1058. it->node = of_find_node_by_phandle(it->phandle);
  1059. if (it->cells_name) {
  1060. if (!it->node) {
  1061. pr_err("%pOF: could not find phandle\n",
  1062. it->parent);
  1063. goto err;
  1064. }
  1065. if (of_property_read_u32(it->node, it->cells_name,
  1066. &count)) {
  1067. pr_err("%pOF: could not get %s for %pOF\n",
  1068. it->parent,
  1069. it->cells_name,
  1070. it->node);
  1071. goto err;
  1072. }
  1073. } else {
  1074. count = it->cell_count;
  1075. }
  1076. /*
  1077. * Make sure that the arguments actually fit in the remaining
  1078. * property data length
  1079. */
  1080. if (it->cur + count > it->list_end) {
  1081. pr_err("%pOF: arguments longer than property\n",
  1082. it->parent);
  1083. goto err;
  1084. }
  1085. }
  1086. it->phandle_end = it->cur + count;
  1087. it->cur_count = count;
  1088. return 0;
  1089. err:
  1090. if (it->node) {
  1091. of_node_put(it->node);
  1092. it->node = NULL;
  1093. }
  1094. return -EINVAL;
  1095. }
  1096. EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
  1097. int of_phandle_iterator_args(struct of_phandle_iterator *it,
  1098. uint32_t *args,
  1099. int size)
  1100. {
  1101. int i, count;
  1102. count = it->cur_count;
  1103. if (WARN_ON(size < count))
  1104. count = size;
  1105. for (i = 0; i < count; i++)
  1106. args[i] = be32_to_cpup(it->cur++);
  1107. return count;
  1108. }
  1109. static int __of_parse_phandle_with_args(const struct device_node *np,
  1110. const char *list_name,
  1111. const char *cells_name,
  1112. int cell_count, int index,
  1113. struct of_phandle_args *out_args)
  1114. {
  1115. struct of_phandle_iterator it;
  1116. int rc, cur_index = 0;
  1117. /* Loop over the phandles until all the requested entry is found */
  1118. of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
  1119. /*
  1120. * All of the error cases bail out of the loop, so at
  1121. * this point, the parsing is successful. If the requested
  1122. * index matches, then fill the out_args structure and return,
  1123. * or return -ENOENT for an empty entry.
  1124. */
  1125. rc = -ENOENT;
  1126. if (cur_index == index) {
  1127. if (!it.phandle)
  1128. goto err;
  1129. if (out_args) {
  1130. int c;
  1131. c = of_phandle_iterator_args(&it,
  1132. out_args->args,
  1133. MAX_PHANDLE_ARGS);
  1134. out_args->np = it.node;
  1135. out_args->args_count = c;
  1136. } else {
  1137. of_node_put(it.node);
  1138. }
  1139. /* Found it! return success */
  1140. return 0;
  1141. }
  1142. cur_index++;
  1143. }
  1144. /*
  1145. * Unlock node before returning result; will be one of:
  1146. * -ENOENT : index is for empty phandle
  1147. * -EINVAL : parsing error on data
  1148. */
  1149. err:
  1150. of_node_put(it.node);
  1151. return rc;
  1152. }
  1153. /**
  1154. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1155. * @np: Pointer to device node holding phandle property
  1156. * @phandle_name: Name of property holding a phandle value
  1157. * @index: For properties holding a table of phandles, this is the index into
  1158. * the table
  1159. *
  1160. * Returns the device_node pointer with refcount incremented. Use
  1161. * of_node_put() on it when done.
  1162. */
  1163. struct device_node *of_parse_phandle(const struct device_node *np,
  1164. const char *phandle_name, int index)
  1165. {
  1166. struct of_phandle_args args;
  1167. if (index < 0)
  1168. return NULL;
  1169. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1170. index, &args))
  1171. return NULL;
  1172. return args.np;
  1173. }
  1174. EXPORT_SYMBOL(of_parse_phandle);
  1175. /**
  1176. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1177. * @np: pointer to a device tree node containing a list
  1178. * @list_name: property name that contains a list
  1179. * @cells_name: property name that specifies phandles' arguments count
  1180. * @index: index of a phandle to parse out
  1181. * @out_args: optional pointer to output arguments structure (will be filled)
  1182. *
  1183. * This function is useful to parse lists of phandles and their arguments.
  1184. * Returns 0 on success and fills out_args, on error returns appropriate
  1185. * errno value.
  1186. *
  1187. * Caller is responsible to call of_node_put() on the returned out_args->np
  1188. * pointer.
  1189. *
  1190. * Example:
  1191. *
  1192. * phandle1: node1 {
  1193. * #list-cells = <2>;
  1194. * }
  1195. *
  1196. * phandle2: node2 {
  1197. * #list-cells = <1>;
  1198. * }
  1199. *
  1200. * node3 {
  1201. * list = <&phandle1 1 2 &phandle2 3>;
  1202. * }
  1203. *
  1204. * To get a device_node of the `node2' node you may call this:
  1205. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1206. */
  1207. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1208. const char *cells_name, int index,
  1209. struct of_phandle_args *out_args)
  1210. {
  1211. if (index < 0)
  1212. return -EINVAL;
  1213. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1214. index, out_args);
  1215. }
  1216. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1217. /**
  1218. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1219. * @np: pointer to a device tree node containing a list
  1220. * @list_name: property name that contains a list
  1221. * @cell_count: number of argument cells following the phandle
  1222. * @index: index of a phandle to parse out
  1223. * @out_args: optional pointer to output arguments structure (will be filled)
  1224. *
  1225. * This function is useful to parse lists of phandles and their arguments.
  1226. * Returns 0 on success and fills out_args, on error returns appropriate
  1227. * errno value.
  1228. *
  1229. * Caller is responsible to call of_node_put() on the returned out_args->np
  1230. * pointer.
  1231. *
  1232. * Example:
  1233. *
  1234. * phandle1: node1 {
  1235. * }
  1236. *
  1237. * phandle2: node2 {
  1238. * }
  1239. *
  1240. * node3 {
  1241. * list = <&phandle1 0 2 &phandle2 2 3>;
  1242. * }
  1243. *
  1244. * To get a device_node of the `node2' node you may call this:
  1245. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1246. */
  1247. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1248. const char *list_name, int cell_count,
  1249. int index, struct of_phandle_args *out_args)
  1250. {
  1251. if (index < 0)
  1252. return -EINVAL;
  1253. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1254. index, out_args);
  1255. }
  1256. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1257. /**
  1258. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1259. * @np: pointer to a device tree node containing a list
  1260. * @list_name: property name that contains a list
  1261. * @cells_name: property name that specifies phandles' arguments count
  1262. *
  1263. * Returns the number of phandle + argument tuples within a property. It
  1264. * is a typical pattern to encode a list of phandle and variable
  1265. * arguments into a single property. The number of arguments is encoded
  1266. * by a property in the phandle-target node. For example, a gpios
  1267. * property would contain a list of GPIO specifies consisting of a
  1268. * phandle and 1 or more arguments. The number of arguments are
  1269. * determined by the #gpio-cells property in the node pointed to by the
  1270. * phandle.
  1271. */
  1272. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1273. const char *cells_name)
  1274. {
  1275. struct of_phandle_iterator it;
  1276. int rc, cur_index = 0;
  1277. rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
  1278. if (rc)
  1279. return rc;
  1280. while ((rc = of_phandle_iterator_next(&it)) == 0)
  1281. cur_index += 1;
  1282. if (rc != -ENOENT)
  1283. return rc;
  1284. return cur_index;
  1285. }
  1286. EXPORT_SYMBOL(of_count_phandle_with_args);
  1287. /**
  1288. * __of_add_property - Add a property to a node without lock operations
  1289. */
  1290. int __of_add_property(struct device_node *np, struct property *prop)
  1291. {
  1292. struct property **next;
  1293. prop->next = NULL;
  1294. next = &np->properties;
  1295. while (*next) {
  1296. if (strcmp(prop->name, (*next)->name) == 0)
  1297. /* duplicate ! don't insert it */
  1298. return -EEXIST;
  1299. next = &(*next)->next;
  1300. }
  1301. *next = prop;
  1302. return 0;
  1303. }
  1304. /**
  1305. * of_add_property - Add a property to a node
  1306. */
  1307. int of_add_property(struct device_node *np, struct property *prop)
  1308. {
  1309. unsigned long flags;
  1310. int rc;
  1311. mutex_lock(&of_mutex);
  1312. raw_spin_lock_irqsave(&devtree_lock, flags);
  1313. rc = __of_add_property(np, prop);
  1314. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1315. if (!rc)
  1316. __of_add_property_sysfs(np, prop);
  1317. mutex_unlock(&of_mutex);
  1318. if (!rc)
  1319. of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
  1320. return rc;
  1321. }
  1322. int __of_remove_property(struct device_node *np, struct property *prop)
  1323. {
  1324. struct property **next;
  1325. for (next = &np->properties; *next; next = &(*next)->next) {
  1326. if (*next == prop)
  1327. break;
  1328. }
  1329. if (*next == NULL)
  1330. return -ENODEV;
  1331. /* found the node */
  1332. *next = prop->next;
  1333. prop->next = np->deadprops;
  1334. np->deadprops = prop;
  1335. return 0;
  1336. }
  1337. void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
  1338. {
  1339. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1340. kfree(prop->attr.attr.name);
  1341. }
  1342. void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
  1343. {
  1344. if (!IS_ENABLED(CONFIG_SYSFS))
  1345. return;
  1346. /* at early boot, bail here and defer setup to of_init() */
  1347. if (of_kset && of_node_is_attached(np))
  1348. __of_sysfs_remove_bin_file(np, prop);
  1349. }
  1350. /**
  1351. * of_remove_property - Remove a property from a node.
  1352. *
  1353. * Note that we don't actually remove it, since we have given out
  1354. * who-knows-how-many pointers to the data using get-property.
  1355. * Instead we just move the property to the "dead properties"
  1356. * list, so it won't be found any more.
  1357. */
  1358. int of_remove_property(struct device_node *np, struct property *prop)
  1359. {
  1360. unsigned long flags;
  1361. int rc;
  1362. if (!prop)
  1363. return -ENODEV;
  1364. mutex_lock(&of_mutex);
  1365. raw_spin_lock_irqsave(&devtree_lock, flags);
  1366. rc = __of_remove_property(np, prop);
  1367. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1368. if (!rc)
  1369. __of_remove_property_sysfs(np, prop);
  1370. mutex_unlock(&of_mutex);
  1371. if (!rc)
  1372. of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
  1373. return rc;
  1374. }
  1375. int __of_update_property(struct device_node *np, struct property *newprop,
  1376. struct property **oldpropp)
  1377. {
  1378. struct property **next, *oldprop;
  1379. for (next = &np->properties; *next; next = &(*next)->next) {
  1380. if (of_prop_cmp((*next)->name, newprop->name) == 0)
  1381. break;
  1382. }
  1383. *oldpropp = oldprop = *next;
  1384. if (oldprop) {
  1385. /* replace the node */
  1386. newprop->next = oldprop->next;
  1387. *next = newprop;
  1388. oldprop->next = np->deadprops;
  1389. np->deadprops = oldprop;
  1390. } else {
  1391. /* new node */
  1392. newprop->next = NULL;
  1393. *next = newprop;
  1394. }
  1395. return 0;
  1396. }
  1397. void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
  1398. struct property *oldprop)
  1399. {
  1400. if (!IS_ENABLED(CONFIG_SYSFS))
  1401. return;
  1402. /* At early boot, bail out and defer setup to of_init() */
  1403. if (!of_kset)
  1404. return;
  1405. if (oldprop)
  1406. __of_sysfs_remove_bin_file(np, oldprop);
  1407. __of_add_property_sysfs(np, newprop);
  1408. }
  1409. /*
  1410. * of_update_property - Update a property in a node, if the property does
  1411. * not exist, add it.
  1412. *
  1413. * Note that we don't actually remove it, since we have given out
  1414. * who-knows-how-many pointers to the data using get-property.
  1415. * Instead we just move the property to the "dead properties" list,
  1416. * and add the new property to the property list
  1417. */
  1418. int of_update_property(struct device_node *np, struct property *newprop)
  1419. {
  1420. struct property *oldprop;
  1421. unsigned long flags;
  1422. int rc;
  1423. if (!newprop->name)
  1424. return -EINVAL;
  1425. mutex_lock(&of_mutex);
  1426. raw_spin_lock_irqsave(&devtree_lock, flags);
  1427. rc = __of_update_property(np, newprop, &oldprop);
  1428. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1429. if (!rc)
  1430. __of_update_property_sysfs(np, newprop, oldprop);
  1431. mutex_unlock(&of_mutex);
  1432. if (!rc)
  1433. of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
  1434. return rc;
  1435. }
  1436. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1437. int id, const char *stem, int stem_len)
  1438. {
  1439. ap->np = np;
  1440. ap->id = id;
  1441. strncpy(ap->stem, stem, stem_len);
  1442. ap->stem[stem_len] = 0;
  1443. list_add_tail(&ap->link, &aliases_lookup);
  1444. pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
  1445. ap->alias, ap->stem, ap->id, np);
  1446. }
  1447. /**
  1448. * of_alias_scan - Scan all properties of the 'aliases' node
  1449. *
  1450. * The function scans all the properties of the 'aliases' node and populates
  1451. * the global lookup table with the properties. It returns the
  1452. * number of alias properties found, or an error code in case of failure.
  1453. *
  1454. * @dt_alloc: An allocator that provides a virtual address to memory
  1455. * for storing the resulting tree
  1456. */
  1457. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1458. {
  1459. struct property *pp;
  1460. of_aliases = of_find_node_by_path("/aliases");
  1461. of_chosen = of_find_node_by_path("/chosen");
  1462. if (of_chosen == NULL)
  1463. of_chosen = of_find_node_by_path("/chosen@0");
  1464. if (of_chosen) {
  1465. /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
  1466. const char *name = NULL;
  1467. if (of_property_read_string(of_chosen, "stdout-path", &name))
  1468. of_property_read_string(of_chosen, "linux,stdout-path",
  1469. &name);
  1470. if (IS_ENABLED(CONFIG_PPC) && !name)
  1471. of_property_read_string(of_aliases, "stdout", &name);
  1472. if (name)
  1473. of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
  1474. }
  1475. if (!of_aliases)
  1476. return;
  1477. for_each_property_of_node(of_aliases, pp) {
  1478. const char *start = pp->name;
  1479. const char *end = start + strlen(start);
  1480. struct device_node *np;
  1481. struct alias_prop *ap;
  1482. int id, len;
  1483. /* Skip those we do not want to proceed */
  1484. if (!strcmp(pp->name, "name") ||
  1485. !strcmp(pp->name, "phandle") ||
  1486. !strcmp(pp->name, "linux,phandle"))
  1487. continue;
  1488. np = of_find_node_by_path(pp->value);
  1489. if (!np)
  1490. continue;
  1491. /* walk the alias backwards to extract the id and work out
  1492. * the 'stem' string */
  1493. while (isdigit(*(end-1)) && end > start)
  1494. end--;
  1495. len = end - start;
  1496. if (kstrtoint(end, 10, &id) < 0)
  1497. continue;
  1498. /* Allocate an alias_prop with enough space for the stem */
  1499. ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
  1500. if (!ap)
  1501. continue;
  1502. memset(ap, 0, sizeof(*ap) + len + 1);
  1503. ap->alias = start;
  1504. of_alias_add(ap, np, id, start, len);
  1505. }
  1506. }
  1507. /**
  1508. * of_alias_get_id - Get alias id for the given device_node
  1509. * @np: Pointer to the given device_node
  1510. * @stem: Alias stem of the given device_node
  1511. *
  1512. * The function travels the lookup table to get the alias id for the given
  1513. * device_node and alias stem. It returns the alias id if found.
  1514. */
  1515. int of_alias_get_id(struct device_node *np, const char *stem)
  1516. {
  1517. struct alias_prop *app;
  1518. int id = -ENODEV;
  1519. mutex_lock(&of_mutex);
  1520. list_for_each_entry(app, &aliases_lookup, link) {
  1521. if (strcmp(app->stem, stem) != 0)
  1522. continue;
  1523. if (np == app->np) {
  1524. id = app->id;
  1525. break;
  1526. }
  1527. }
  1528. mutex_unlock(&of_mutex);
  1529. return id;
  1530. }
  1531. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1532. /**
  1533. * of_alias_get_highest_id - Get highest alias id for the given stem
  1534. * @stem: Alias stem to be examined
  1535. *
  1536. * The function travels the lookup table to get the highest alias id for the
  1537. * given alias stem. It returns the alias id if found.
  1538. */
  1539. int of_alias_get_highest_id(const char *stem)
  1540. {
  1541. struct alias_prop *app;
  1542. int id = -ENODEV;
  1543. mutex_lock(&of_mutex);
  1544. list_for_each_entry(app, &aliases_lookup, link) {
  1545. if (strcmp(app->stem, stem) != 0)
  1546. continue;
  1547. if (app->id > id)
  1548. id = app->id;
  1549. }
  1550. mutex_unlock(&of_mutex);
  1551. return id;
  1552. }
  1553. EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
  1554. /**
  1555. * of_console_check() - Test and setup console for DT setup
  1556. * @dn - Pointer to device node
  1557. * @name - Name to use for preferred console without index. ex. "ttyS"
  1558. * @index - Index to use for preferred console.
  1559. *
  1560. * Check if the given device node matches the stdout-path property in the
  1561. * /chosen node. If it does then register it as the preferred console and return
  1562. * TRUE. Otherwise return FALSE.
  1563. */
  1564. bool of_console_check(struct device_node *dn, char *name, int index)
  1565. {
  1566. if (!dn || dn != of_stdout || console_set_on_cmdline)
  1567. return false;
  1568. /*
  1569. * XXX: cast `options' to char pointer to suppress complication
  1570. * warnings: printk, UART and console drivers expect char pointer.
  1571. */
  1572. return !add_preferred_console(name, index, (char *)of_stdout_options);
  1573. }
  1574. EXPORT_SYMBOL_GPL(of_console_check);
  1575. /**
  1576. * of_find_next_cache_node - Find a node's subsidiary cache
  1577. * @np: node of type "cpu" or "cache"
  1578. *
  1579. * Returns a node pointer with refcount incremented, use
  1580. * of_node_put() on it when done. Caller should hold a reference
  1581. * to np.
  1582. */
  1583. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1584. {
  1585. struct device_node *child, *cache_node;
  1586. cache_node = of_parse_phandle(np, "l2-cache", 0);
  1587. if (!cache_node)
  1588. cache_node = of_parse_phandle(np, "next-level-cache", 0);
  1589. if (cache_node)
  1590. return cache_node;
  1591. /* OF on pmac has nodes instead of properties named "l2-cache"
  1592. * beneath CPU nodes.
  1593. */
  1594. if (IS_ENABLED(CONFIG_PPC_PMAC) && !strcmp(np->type, "cpu"))
  1595. for_each_child_of_node(np, child)
  1596. if (!strcmp(child->type, "cache"))
  1597. return child;
  1598. return NULL;
  1599. }
  1600. /**
  1601. * of_find_last_cache_level - Find the level at which the last cache is
  1602. * present for the given logical cpu
  1603. *
  1604. * @cpu: cpu number(logical index) for which the last cache level is needed
  1605. *
  1606. * Returns the the level at which the last cache is present. It is exactly
  1607. * same as the total number of cache levels for the given logical cpu.
  1608. */
  1609. int of_find_last_cache_level(unsigned int cpu)
  1610. {
  1611. u32 cache_level = 0;
  1612. struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
  1613. while (np) {
  1614. prev = np;
  1615. of_node_put(np);
  1616. np = of_find_next_cache_node(np);
  1617. }
  1618. of_property_read_u32(prev, "cache-level", &cache_level);
  1619. return cache_level;
  1620. }