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