base.c 47 KB

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