base.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  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. #include <linux/console.h>
  21. #include <linux/ctype.h>
  22. #include <linux/cpu.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_graph.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/proc_fs.h>
  30. #include "of_private.h"
  31. LIST_HEAD(aliases_lookup);
  32. struct device_node *of_root;
  33. EXPORT_SYMBOL(of_root);
  34. struct device_node *of_chosen;
  35. struct device_node *of_aliases;
  36. struct device_node *of_stdout;
  37. static const char *of_stdout_options;
  38. struct kset *of_kset;
  39. /*
  40. * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
  41. * This mutex must be held whenever modifications are being made to the
  42. * device tree. The of_{attach,detach}_node() and
  43. * of_{add,remove,update}_property() helpers make sure this happens.
  44. */
  45. DEFINE_MUTEX(of_mutex);
  46. /* use when traversing tree through the child, sibling,
  47. * or parent members of struct device_node.
  48. */
  49. DEFINE_RAW_SPINLOCK(devtree_lock);
  50. int of_n_addr_cells(struct device_node *np)
  51. {
  52. const __be32 *ip;
  53. do {
  54. if (np->parent)
  55. np = np->parent;
  56. ip = of_get_property(np, "#address-cells", NULL);
  57. if (ip)
  58. return be32_to_cpup(ip);
  59. } while (np->parent);
  60. /* No #address-cells property for the root node */
  61. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  62. }
  63. EXPORT_SYMBOL(of_n_addr_cells);
  64. int of_n_size_cells(struct device_node *np)
  65. {
  66. const __be32 *ip;
  67. do {
  68. if (np->parent)
  69. np = np->parent;
  70. ip = of_get_property(np, "#size-cells", NULL);
  71. if (ip)
  72. return be32_to_cpup(ip);
  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. static const char *safe_name(struct kobject *kobj, const char *orig_name)
  101. {
  102. const char *name = orig_name;
  103. struct kernfs_node *kn;
  104. int i = 0;
  105. /* don't be a hero. After 16 tries give up */
  106. while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
  107. sysfs_put(kn);
  108. if (name != orig_name)
  109. kfree(name);
  110. name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
  111. }
  112. if (name != orig_name)
  113. pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
  114. kobject_name(kobj), name);
  115. return name;
  116. }
  117. int __of_add_property_sysfs(struct device_node *np, struct property *pp)
  118. {
  119. int rc;
  120. /* Important: Don't leak passwords */
  121. bool secure = strncmp(pp->name, "security-", 9) == 0;
  122. if (!IS_ENABLED(CONFIG_SYSFS))
  123. return 0;
  124. if (!of_kset || !of_node_is_attached(np))
  125. return 0;
  126. sysfs_bin_attr_init(&pp->attr);
  127. pp->attr.attr.name = safe_name(&np->kobj, pp->name);
  128. pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
  129. pp->attr.size = secure ? 0 : pp->length;
  130. pp->attr.read = of_node_property_read;
  131. rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
  132. WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
  133. return rc;
  134. }
  135. int __of_attach_node_sysfs(struct device_node *np)
  136. {
  137. const char *name;
  138. struct property *pp;
  139. int rc;
  140. if (!IS_ENABLED(CONFIG_SYSFS))
  141. return 0;
  142. if (!of_kset)
  143. return 0;
  144. np->kobj.kset = of_kset;
  145. if (!np->parent) {
  146. /* Nodes without parents are new top level trees */
  147. rc = kobject_add(&np->kobj, NULL, "%s",
  148. safe_name(&of_kset->kobj, "base"));
  149. } else {
  150. name = safe_name(&np->parent->kobj, kbasename(np->full_name));
  151. if (!name || !name[0])
  152. return -EINVAL;
  153. rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
  154. }
  155. if (rc)
  156. return rc;
  157. for_each_property_of_node(np, pp)
  158. __of_add_property_sysfs(np, pp);
  159. return 0;
  160. }
  161. void __init of_core_init(void)
  162. {
  163. struct device_node *np;
  164. /* Create the kset, and register existing nodes */
  165. mutex_lock(&of_mutex);
  166. of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
  167. if (!of_kset) {
  168. mutex_unlock(&of_mutex);
  169. pr_err("devicetree: failed to register existing nodes\n");
  170. return;
  171. }
  172. for_each_of_allnodes(np)
  173. __of_attach_node_sysfs(np);
  174. mutex_unlock(&of_mutex);
  175. /* Symlink in /proc as required by userspace ABI */
  176. if (of_root)
  177. proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
  178. }
  179. static struct property *__of_find_property(const struct device_node *np,
  180. const char *name, int *lenp)
  181. {
  182. struct property *pp;
  183. if (!np)
  184. return NULL;
  185. for (pp = np->properties; pp; pp = pp->next) {
  186. if (of_prop_cmp(pp->name, name) == 0) {
  187. if (lenp)
  188. *lenp = pp->length;
  189. break;
  190. }
  191. }
  192. return pp;
  193. }
  194. struct property *of_find_property(const struct device_node *np,
  195. const char *name,
  196. int *lenp)
  197. {
  198. struct property *pp;
  199. unsigned long flags;
  200. raw_spin_lock_irqsave(&devtree_lock, flags);
  201. pp = __of_find_property(np, name, lenp);
  202. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  203. return pp;
  204. }
  205. EXPORT_SYMBOL(of_find_property);
  206. struct device_node *__of_find_all_nodes(struct device_node *prev)
  207. {
  208. struct device_node *np;
  209. if (!prev) {
  210. np = of_root;
  211. } else if (prev->child) {
  212. np = prev->child;
  213. } else {
  214. /* Walk back up looking for a sibling, or the end of the structure */
  215. np = prev;
  216. while (np->parent && !np->sibling)
  217. np = np->parent;
  218. np = np->sibling; /* Might be null at the end of the tree */
  219. }
  220. return np;
  221. }
  222. /**
  223. * of_find_all_nodes - Get next node in global list
  224. * @prev: Previous node or NULL to start iteration
  225. * of_node_put() will be called on it
  226. *
  227. * Returns a node pointer with refcount incremented, use
  228. * of_node_put() on it when done.
  229. */
  230. struct device_node *of_find_all_nodes(struct device_node *prev)
  231. {
  232. struct device_node *np;
  233. unsigned long flags;
  234. raw_spin_lock_irqsave(&devtree_lock, flags);
  235. np = __of_find_all_nodes(prev);
  236. of_node_get(np);
  237. of_node_put(prev);
  238. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  239. return np;
  240. }
  241. EXPORT_SYMBOL(of_find_all_nodes);
  242. /*
  243. * Find a property with a given name for a given node
  244. * and return the value.
  245. */
  246. const void *__of_get_property(const struct device_node *np,
  247. const char *name, int *lenp)
  248. {
  249. struct property *pp = __of_find_property(np, name, lenp);
  250. return pp ? pp->value : NULL;
  251. }
  252. /*
  253. * Find a property with a given name for a given node
  254. * and return the value.
  255. */
  256. const void *of_get_property(const struct device_node *np, const char *name,
  257. int *lenp)
  258. {
  259. struct property *pp = of_find_property(np, name, lenp);
  260. return pp ? pp->value : NULL;
  261. }
  262. EXPORT_SYMBOL(of_get_property);
  263. /*
  264. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  265. *
  266. * @cpu: logical cpu index of a core/thread
  267. * @phys_id: physical identifier of a core/thread
  268. *
  269. * CPU logical to physical index mapping is architecture specific.
  270. * However this __weak function provides a default match of physical
  271. * id to logical cpu index. phys_id provided here is usually values read
  272. * from the device tree which must match the hardware internal registers.
  273. *
  274. * Returns true if the physical identifier and the logical cpu index
  275. * correspond to the same core/thread, false otherwise.
  276. */
  277. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  278. {
  279. return (u32)phys_id == cpu;
  280. }
  281. /**
  282. * Checks if the given "prop_name" property holds the physical id of the
  283. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  284. * NULL, local thread number within the core is returned in it.
  285. */
  286. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  287. const char *prop_name, int cpu, unsigned int *thread)
  288. {
  289. const __be32 *cell;
  290. int ac, prop_len, tid;
  291. u64 hwid;
  292. ac = of_n_addr_cells(cpun);
  293. cell = of_get_property(cpun, prop_name, &prop_len);
  294. if (!cell || !ac)
  295. return false;
  296. prop_len /= sizeof(*cell) * ac;
  297. for (tid = 0; tid < prop_len; tid++) {
  298. hwid = of_read_number(cell, ac);
  299. if (arch_match_cpu_phys_id(cpu, hwid)) {
  300. if (thread)
  301. *thread = tid;
  302. return true;
  303. }
  304. cell += ac;
  305. }
  306. return false;
  307. }
  308. /*
  309. * arch_find_n_match_cpu_physical_id - See if the given device node is
  310. * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
  311. * else false. If 'thread' is non-NULL, the local thread number within the
  312. * core is returned in it.
  313. */
  314. bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  315. int cpu, unsigned int *thread)
  316. {
  317. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  318. * for thread ids on PowerPC. If it doesn't exist fallback to
  319. * standard "reg" property.
  320. */
  321. if (IS_ENABLED(CONFIG_PPC) &&
  322. __of_find_n_match_cpu_property(cpun,
  323. "ibm,ppc-interrupt-server#s",
  324. cpu, thread))
  325. return true;
  326. return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
  327. }
  328. /**
  329. * of_get_cpu_node - Get device node associated with the given logical CPU
  330. *
  331. * @cpu: CPU number(logical index) for which device node is required
  332. * @thread: if not NULL, local thread number within the physical core is
  333. * returned
  334. *
  335. * The main purpose of this function is to retrieve the device node for the
  336. * given logical CPU index. It should be used to initialize the of_node in
  337. * cpu device. Once of_node in cpu device is populated, all the further
  338. * references can use that instead.
  339. *
  340. * CPU logical to physical index mapping is architecture specific and is built
  341. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  342. * which can be overridden by architecture specific implementation.
  343. *
  344. * Returns a node pointer for the logical cpu if found, else NULL.
  345. */
  346. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  347. {
  348. struct device_node *cpun;
  349. for_each_node_by_type(cpun, "cpu") {
  350. if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
  351. return cpun;
  352. }
  353. return NULL;
  354. }
  355. EXPORT_SYMBOL(of_get_cpu_node);
  356. /**
  357. * __of_device_is_compatible() - Check if the node matches given constraints
  358. * @device: pointer to node
  359. * @compat: required compatible string, NULL or "" for any match
  360. * @type: required device_type value, NULL or "" for any match
  361. * @name: required node name, NULL or "" for any match
  362. *
  363. * Checks if the given @compat, @type and @name strings match the
  364. * properties of the given @device. A constraints can be skipped by
  365. * passing NULL or an empty string as the constraint.
  366. *
  367. * Returns 0 for no match, and a positive integer on match. The return
  368. * value is a relative score with larger values indicating better
  369. * matches. The score is weighted for the most specific compatible value
  370. * to get the highest score. Matching type is next, followed by matching
  371. * name. Practically speaking, this results in the following priority
  372. * order for matches:
  373. *
  374. * 1. specific compatible && type && name
  375. * 2. specific compatible && type
  376. * 3. specific compatible && name
  377. * 4. specific compatible
  378. * 5. general compatible && type && name
  379. * 6. general compatible && type
  380. * 7. general compatible && name
  381. * 8. general compatible
  382. * 9. type && name
  383. * 10. type
  384. * 11. name
  385. */
  386. static int __of_device_is_compatible(const struct device_node *device,
  387. const char *compat, const char *type, const char *name)
  388. {
  389. struct property *prop;
  390. const char *cp;
  391. int index = 0, score = 0;
  392. /* Compatible match has highest priority */
  393. if (compat && compat[0]) {
  394. prop = __of_find_property(device, "compatible", NULL);
  395. for (cp = of_prop_next_string(prop, NULL); cp;
  396. cp = of_prop_next_string(prop, cp), index++) {
  397. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  398. score = INT_MAX/2 - (index << 2);
  399. break;
  400. }
  401. }
  402. if (!score)
  403. return 0;
  404. }
  405. /* Matching type is better than matching name */
  406. if (type && type[0]) {
  407. if (!device->type || of_node_cmp(type, device->type))
  408. return 0;
  409. score += 2;
  410. }
  411. /* Matching name is a bit better than not */
  412. if (name && name[0]) {
  413. if (!device->name || of_node_cmp(name, device->name))
  414. return 0;
  415. score++;
  416. }
  417. return score;
  418. }
  419. /** Checks if the given "compat" string matches one of the strings in
  420. * the device's "compatible" property
  421. */
  422. int of_device_is_compatible(const struct device_node *device,
  423. const char *compat)
  424. {
  425. unsigned long flags;
  426. int res;
  427. raw_spin_lock_irqsave(&devtree_lock, flags);
  428. res = __of_device_is_compatible(device, compat, NULL, NULL);
  429. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  430. return res;
  431. }
  432. EXPORT_SYMBOL(of_device_is_compatible);
  433. /**
  434. * of_machine_is_compatible - Test root of device tree for a given compatible value
  435. * @compat: compatible string to look for in root node's compatible property.
  436. *
  437. * Returns a positive integer if the root node has the given value in its
  438. * compatible property.
  439. */
  440. int of_machine_is_compatible(const char *compat)
  441. {
  442. struct device_node *root;
  443. int rc = 0;
  444. root = of_find_node_by_path("/");
  445. if (root) {
  446. rc = of_device_is_compatible(root, compat);
  447. of_node_put(root);
  448. }
  449. return rc;
  450. }
  451. EXPORT_SYMBOL(of_machine_is_compatible);
  452. /**
  453. * __of_device_is_available - check if a device is available for use
  454. *
  455. * @device: Node to check for availability, with locks already held
  456. *
  457. * Returns true if the status property is absent or set to "okay" or "ok",
  458. * false otherwise
  459. */
  460. static bool __of_device_is_available(const struct device_node *device)
  461. {
  462. const char *status;
  463. int statlen;
  464. if (!device)
  465. return false;
  466. status = __of_get_property(device, "status", &statlen);
  467. if (status == NULL)
  468. return true;
  469. if (statlen > 0) {
  470. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  471. return true;
  472. }
  473. return false;
  474. }
  475. /**
  476. * of_device_is_available - check if a device is available for use
  477. *
  478. * @device: Node to check for availability
  479. *
  480. * Returns true if the status property is absent or set to "okay" or "ok",
  481. * false otherwise
  482. */
  483. bool of_device_is_available(const struct device_node *device)
  484. {
  485. unsigned long flags;
  486. bool res;
  487. raw_spin_lock_irqsave(&devtree_lock, flags);
  488. res = __of_device_is_available(device);
  489. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  490. return res;
  491. }
  492. EXPORT_SYMBOL(of_device_is_available);
  493. /**
  494. * of_device_is_big_endian - check if a device has BE registers
  495. *
  496. * @device: Node to check for endianness
  497. *
  498. * Returns true if the device has a "big-endian" property, or if the kernel
  499. * was compiled for BE *and* the device has a "native-endian" property.
  500. * Returns false otherwise.
  501. *
  502. * Callers would nominally use ioread32be/iowrite32be if
  503. * of_device_is_big_endian() == true, or readl/writel otherwise.
  504. */
  505. bool of_device_is_big_endian(const struct device_node *device)
  506. {
  507. if (of_property_read_bool(device, "big-endian"))
  508. return true;
  509. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  510. of_property_read_bool(device, "native-endian"))
  511. return true;
  512. return false;
  513. }
  514. EXPORT_SYMBOL(of_device_is_big_endian);
  515. /**
  516. * of_get_parent - Get a node's parent if any
  517. * @node: Node to get parent
  518. *
  519. * Returns a node pointer with refcount incremented, use
  520. * of_node_put() on it when done.
  521. */
  522. struct device_node *of_get_parent(const struct device_node *node)
  523. {
  524. struct device_node *np;
  525. unsigned long flags;
  526. if (!node)
  527. return NULL;
  528. raw_spin_lock_irqsave(&devtree_lock, flags);
  529. np = of_node_get(node->parent);
  530. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  531. return np;
  532. }
  533. EXPORT_SYMBOL(of_get_parent);
  534. /**
  535. * of_get_next_parent - Iterate to a node's parent
  536. * @node: Node to get parent of
  537. *
  538. * This is like of_get_parent() except that it drops the
  539. * refcount on the passed node, making it suitable for iterating
  540. * through a node's parents.
  541. *
  542. * Returns a node pointer with refcount incremented, use
  543. * of_node_put() on it when done.
  544. */
  545. struct device_node *of_get_next_parent(struct device_node *node)
  546. {
  547. struct device_node *parent;
  548. unsigned long flags;
  549. if (!node)
  550. return NULL;
  551. raw_spin_lock_irqsave(&devtree_lock, flags);
  552. parent = of_node_get(node->parent);
  553. of_node_put(node);
  554. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  555. return parent;
  556. }
  557. EXPORT_SYMBOL(of_get_next_parent);
  558. static struct device_node *__of_get_next_child(const struct device_node *node,
  559. struct device_node *prev)
  560. {
  561. struct device_node *next;
  562. if (!node)
  563. return NULL;
  564. next = prev ? prev->sibling : node->child;
  565. for (; next; next = next->sibling)
  566. if (of_node_get(next))
  567. break;
  568. of_node_put(prev);
  569. return next;
  570. }
  571. #define __for_each_child_of_node(parent, child) \
  572. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  573. child = __of_get_next_child(parent, child))
  574. /**
  575. * of_get_next_child - Iterate a node childs
  576. * @node: parent node
  577. * @prev: previous child of the parent node, or NULL to get first
  578. *
  579. * Returns a node pointer with refcount incremented, use of_node_put() on
  580. * it when done. Returns NULL when prev is the last child. Decrements the
  581. * refcount of prev.
  582. */
  583. struct device_node *of_get_next_child(const struct device_node *node,
  584. struct device_node *prev)
  585. {
  586. struct device_node *next;
  587. unsigned long flags;
  588. raw_spin_lock_irqsave(&devtree_lock, flags);
  589. next = __of_get_next_child(node, prev);
  590. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  591. return next;
  592. }
  593. EXPORT_SYMBOL(of_get_next_child);
  594. /**
  595. * of_get_next_available_child - Find the next available child node
  596. * @node: parent node
  597. * @prev: previous child of the parent node, or NULL to get first
  598. *
  599. * This function is like of_get_next_child(), except that it
  600. * automatically skips any disabled nodes (i.e. status = "disabled").
  601. */
  602. struct device_node *of_get_next_available_child(const struct device_node *node,
  603. struct device_node *prev)
  604. {
  605. struct device_node *next;
  606. unsigned long flags;
  607. if (!node)
  608. return NULL;
  609. raw_spin_lock_irqsave(&devtree_lock, flags);
  610. next = prev ? prev->sibling : node->child;
  611. for (; next; next = next->sibling) {
  612. if (!__of_device_is_available(next))
  613. continue;
  614. if (of_node_get(next))
  615. break;
  616. }
  617. of_node_put(prev);
  618. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  619. return next;
  620. }
  621. EXPORT_SYMBOL(of_get_next_available_child);
  622. /**
  623. * of_get_child_by_name - Find the child node by name for a given parent
  624. * @node: parent node
  625. * @name: child name to look for.
  626. *
  627. * This function looks for child node for given matching name
  628. *
  629. * Returns a node pointer if found, with refcount incremented, use
  630. * of_node_put() on it when done.
  631. * Returns NULL if node is not found.
  632. */
  633. struct device_node *of_get_child_by_name(const struct device_node *node,
  634. const char *name)
  635. {
  636. struct device_node *child;
  637. for_each_child_of_node(node, child)
  638. if (child->name && (of_node_cmp(child->name, name) == 0))
  639. break;
  640. return child;
  641. }
  642. EXPORT_SYMBOL(of_get_child_by_name);
  643. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  644. const char *path)
  645. {
  646. struct device_node *child;
  647. int len;
  648. len = strcspn(path, "/:");
  649. if (!len)
  650. return NULL;
  651. __for_each_child_of_node(parent, child) {
  652. const char *name = strrchr(child->full_name, '/');
  653. if (WARN(!name, "malformed device_node %s\n", child->full_name))
  654. continue;
  655. name++;
  656. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  657. return child;
  658. }
  659. return NULL;
  660. }
  661. /**
  662. * of_find_node_opts_by_path - Find a node matching a full OF path
  663. * @path: Either the full path to match, or if the path does not
  664. * start with '/', the name of a property of the /aliases
  665. * node (an alias). In the case of an alias, the node
  666. * matching the alias' value will be returned.
  667. * @opts: Address of a pointer into which to store the start of
  668. * an options string appended to the end of the path with
  669. * a ':' separator.
  670. *
  671. * Valid paths:
  672. * /foo/bar Full path
  673. * foo Valid alias
  674. * foo/bar Valid alias + relative path
  675. *
  676. * Returns a node pointer with refcount incremented, use
  677. * of_node_put() on it when done.
  678. */
  679. struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
  680. {
  681. struct device_node *np = NULL;
  682. struct property *pp;
  683. unsigned long flags;
  684. const char *separator = strchr(path, ':');
  685. if (opts)
  686. *opts = separator ? separator + 1 : NULL;
  687. if (strcmp(path, "/") == 0)
  688. return of_node_get(of_root);
  689. /* The path could begin with an alias */
  690. if (*path != '/') {
  691. int len;
  692. const char *p = separator;
  693. if (!p)
  694. p = strchrnul(path, '/');
  695. len = p - path;
  696. /* of_aliases must not be NULL */
  697. if (!of_aliases)
  698. return NULL;
  699. for_each_property_of_node(of_aliases, pp) {
  700. if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
  701. np = of_find_node_by_path(pp->value);
  702. break;
  703. }
  704. }
  705. if (!np)
  706. return NULL;
  707. path = p;
  708. }
  709. /* Step down the tree matching path components */
  710. raw_spin_lock_irqsave(&devtree_lock, flags);
  711. if (!np)
  712. np = of_node_get(of_root);
  713. while (np && *path == '/') {
  714. path++; /* Increment past '/' delimiter */
  715. np = __of_find_node_by_path(np, path);
  716. path = strchrnul(path, '/');
  717. if (separator && separator < path)
  718. break;
  719. }
  720. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  721. return np;
  722. }
  723. EXPORT_SYMBOL(of_find_node_opts_by_path);
  724. /**
  725. * of_find_node_by_name - Find a node by its "name" property
  726. * @from: The node to start searching from or NULL, the node
  727. * you pass will not be searched, only the next one
  728. * will; typically, you pass what the previous call
  729. * returned. of_node_put() will be called on it
  730. * @name: The name string to match against
  731. *
  732. * Returns a node pointer with refcount incremented, use
  733. * of_node_put() on it when done.
  734. */
  735. struct device_node *of_find_node_by_name(struct device_node *from,
  736. const char *name)
  737. {
  738. struct device_node *np;
  739. unsigned long flags;
  740. raw_spin_lock_irqsave(&devtree_lock, flags);
  741. for_each_of_allnodes_from(from, np)
  742. if (np->name && (of_node_cmp(np->name, name) == 0)
  743. && of_node_get(np))
  744. break;
  745. of_node_put(from);
  746. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  747. return np;
  748. }
  749. EXPORT_SYMBOL(of_find_node_by_name);
  750. /**
  751. * of_find_node_by_type - Find a node by its "device_type" property
  752. * @from: The node to start searching from, or NULL to start searching
  753. * the entire device tree. The node you pass will not be
  754. * searched, only the next one will; typically, you pass
  755. * what the previous call returned. of_node_put() will be
  756. * called on from for you.
  757. * @type: The type string to match against
  758. *
  759. * Returns a node pointer with refcount incremented, use
  760. * of_node_put() on it when done.
  761. */
  762. struct device_node *of_find_node_by_type(struct device_node *from,
  763. const char *type)
  764. {
  765. struct device_node *np;
  766. unsigned long flags;
  767. raw_spin_lock_irqsave(&devtree_lock, flags);
  768. for_each_of_allnodes_from(from, np)
  769. if (np->type && (of_node_cmp(np->type, type) == 0)
  770. && of_node_get(np))
  771. break;
  772. of_node_put(from);
  773. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  774. return np;
  775. }
  776. EXPORT_SYMBOL(of_find_node_by_type);
  777. /**
  778. * of_find_compatible_node - Find a node based on type and one of the
  779. * tokens in its "compatible" property
  780. * @from: The node to start searching from or NULL, the node
  781. * you pass will not be searched, only the next one
  782. * will; typically, you pass what the previous call
  783. * returned. of_node_put() will be called on it
  784. * @type: The type string to match "device_type" or NULL to ignore
  785. * @compatible: The string to match to one of the tokens in the device
  786. * "compatible" list.
  787. *
  788. * Returns a node pointer with refcount incremented, use
  789. * of_node_put() on it when done.
  790. */
  791. struct device_node *of_find_compatible_node(struct device_node *from,
  792. const char *type, const char *compatible)
  793. {
  794. struct device_node *np;
  795. unsigned long flags;
  796. raw_spin_lock_irqsave(&devtree_lock, flags);
  797. for_each_of_allnodes_from(from, np)
  798. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  799. of_node_get(np))
  800. break;
  801. of_node_put(from);
  802. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  803. return np;
  804. }
  805. EXPORT_SYMBOL(of_find_compatible_node);
  806. /**
  807. * of_find_node_with_property - Find a node which has a property with
  808. * the given name.
  809. * @from: The node to start searching from or NULL, the node
  810. * you pass will not be searched, only the next one
  811. * will; typically, you pass what the previous call
  812. * returned. of_node_put() will be called on it
  813. * @prop_name: The name of the property to look for.
  814. *
  815. * Returns a node pointer with refcount incremented, use
  816. * of_node_put() on it when done.
  817. */
  818. struct device_node *of_find_node_with_property(struct device_node *from,
  819. const char *prop_name)
  820. {
  821. struct device_node *np;
  822. struct property *pp;
  823. unsigned long flags;
  824. raw_spin_lock_irqsave(&devtree_lock, flags);
  825. for_each_of_allnodes_from(from, np) {
  826. for (pp = np->properties; pp; pp = pp->next) {
  827. if (of_prop_cmp(pp->name, prop_name) == 0) {
  828. of_node_get(np);
  829. goto out;
  830. }
  831. }
  832. }
  833. out:
  834. of_node_put(from);
  835. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  836. return np;
  837. }
  838. EXPORT_SYMBOL(of_find_node_with_property);
  839. static
  840. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  841. const struct device_node *node)
  842. {
  843. const struct of_device_id *best_match = NULL;
  844. int score, best_score = 0;
  845. if (!matches)
  846. return NULL;
  847. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  848. score = __of_device_is_compatible(node, matches->compatible,
  849. matches->type, matches->name);
  850. if (score > best_score) {
  851. best_match = matches;
  852. best_score = score;
  853. }
  854. }
  855. return best_match;
  856. }
  857. /**
  858. * of_match_node - Tell if a device_node has a matching of_match structure
  859. * @matches: array of of device match structures to search in
  860. * @node: the of device structure to match against
  861. *
  862. * Low level utility function used by device matching.
  863. */
  864. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  865. const struct device_node *node)
  866. {
  867. const struct of_device_id *match;
  868. unsigned long flags;
  869. raw_spin_lock_irqsave(&devtree_lock, flags);
  870. match = __of_match_node(matches, node);
  871. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  872. return match;
  873. }
  874. EXPORT_SYMBOL(of_match_node);
  875. /**
  876. * of_find_matching_node_and_match - Find a node based on an of_device_id
  877. * match table.
  878. * @from: The node to start searching from or NULL, the node
  879. * you pass will not be searched, only the next one
  880. * will; typically, you pass what the previous call
  881. * returned. of_node_put() will be called on it
  882. * @matches: array of of device match structures to search in
  883. * @match Updated to point at the matches entry which matched
  884. *
  885. * Returns a node pointer with refcount incremented, use
  886. * of_node_put() on it when done.
  887. */
  888. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  889. const struct of_device_id *matches,
  890. const struct of_device_id **match)
  891. {
  892. struct device_node *np;
  893. const struct of_device_id *m;
  894. unsigned long flags;
  895. if (match)
  896. *match = NULL;
  897. raw_spin_lock_irqsave(&devtree_lock, flags);
  898. for_each_of_allnodes_from(from, np) {
  899. m = __of_match_node(matches, np);
  900. if (m && of_node_get(np)) {
  901. if (match)
  902. *match = m;
  903. break;
  904. }
  905. }
  906. of_node_put(from);
  907. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  908. return np;
  909. }
  910. EXPORT_SYMBOL(of_find_matching_node_and_match);
  911. /**
  912. * of_modalias_node - Lookup appropriate modalias for a device node
  913. * @node: pointer to a device tree node
  914. * @modalias: Pointer to buffer that modalias value will be copied into
  915. * @len: Length of modalias value
  916. *
  917. * Based on the value of the compatible property, this routine will attempt
  918. * to choose an appropriate modalias value for a particular device tree node.
  919. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  920. * from the first entry in the compatible list property.
  921. *
  922. * This routine returns 0 on success, <0 on failure.
  923. */
  924. int of_modalias_node(struct device_node *node, char *modalias, int len)
  925. {
  926. const char *compatible, *p;
  927. int cplen;
  928. compatible = of_get_property(node, "compatible", &cplen);
  929. if (!compatible || strlen(compatible) > cplen)
  930. return -ENODEV;
  931. p = strchr(compatible, ',');
  932. strlcpy(modalias, p ? p + 1 : compatible, len);
  933. return 0;
  934. }
  935. EXPORT_SYMBOL_GPL(of_modalias_node);
  936. /**
  937. * of_find_node_by_phandle - Find a node given a phandle
  938. * @handle: phandle of the node to find
  939. *
  940. * Returns a node pointer with refcount incremented, use
  941. * of_node_put() on it when done.
  942. */
  943. struct device_node *of_find_node_by_phandle(phandle handle)
  944. {
  945. struct device_node *np;
  946. unsigned long flags;
  947. if (!handle)
  948. return NULL;
  949. raw_spin_lock_irqsave(&devtree_lock, flags);
  950. for_each_of_allnodes(np)
  951. if (np->phandle == handle)
  952. break;
  953. of_node_get(np);
  954. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  955. return np;
  956. }
  957. EXPORT_SYMBOL(of_find_node_by_phandle);
  958. /**
  959. * of_property_count_elems_of_size - Count the number of elements in a property
  960. *
  961. * @np: device node from which the property value is to be read.
  962. * @propname: name of the property to be searched.
  963. * @elem_size: size of the individual element
  964. *
  965. * Search for a property in a device node and count the number of elements of
  966. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  967. * property does not exist or its length does not match a multiple of elem_size
  968. * and -ENODATA if the property does not have a value.
  969. */
  970. int of_property_count_elems_of_size(const struct device_node *np,
  971. const char *propname, int elem_size)
  972. {
  973. struct property *prop = of_find_property(np, propname, NULL);
  974. if (!prop)
  975. return -EINVAL;
  976. if (!prop->value)
  977. return -ENODATA;
  978. if (prop->length % elem_size != 0) {
  979. pr_err("size of %s in node %s is not a multiple of %d\n",
  980. propname, np->full_name, elem_size);
  981. return -EINVAL;
  982. }
  983. return prop->length / elem_size;
  984. }
  985. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  986. /**
  987. * of_find_property_value_of_size
  988. *
  989. * @np: device node from which the property value is to be read.
  990. * @propname: name of the property to be searched.
  991. * @len: requested length of property value
  992. *
  993. * Search for a property in a device node and valid the requested size.
  994. * Returns the property value on success, -EINVAL if the property does not
  995. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  996. * property data isn't large enough.
  997. *
  998. */
  999. static void *of_find_property_value_of_size(const struct device_node *np,
  1000. const char *propname, u32 len)
  1001. {
  1002. struct property *prop = of_find_property(np, propname, NULL);
  1003. if (!prop)
  1004. return ERR_PTR(-EINVAL);
  1005. if (!prop->value)
  1006. return ERR_PTR(-ENODATA);
  1007. if (len > prop->length)
  1008. return ERR_PTR(-EOVERFLOW);
  1009. return prop->value;
  1010. }
  1011. /**
  1012. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  1013. *
  1014. * @np: device node from which the property value is to be read.
  1015. * @propname: name of the property to be searched.
  1016. * @index: index of the u32 in the list of values
  1017. * @out_value: pointer to return value, modified only if no error.
  1018. *
  1019. * Search for a property in a device node and read nth 32-bit value from
  1020. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1021. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1022. * property data isn't large enough.
  1023. *
  1024. * The out_value is modified only if a valid u32 value can be decoded.
  1025. */
  1026. int of_property_read_u32_index(const struct device_node *np,
  1027. const char *propname,
  1028. u32 index, u32 *out_value)
  1029. {
  1030. const u32 *val = of_find_property_value_of_size(np, propname,
  1031. ((index + 1) * sizeof(*out_value)));
  1032. if (IS_ERR(val))
  1033. return PTR_ERR(val);
  1034. *out_value = be32_to_cpup(((__be32 *)val) + index);
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  1038. /**
  1039. * of_property_read_u8_array - Find and read an array of u8 from a property.
  1040. *
  1041. * @np: device node from which the property value is to be read.
  1042. * @propname: name of the property to be searched.
  1043. * @out_values: pointer to return value, modified only if return value is 0.
  1044. * @sz: number of array elements to read
  1045. *
  1046. * Search for a property in a device node and read 8-bit value(s) from
  1047. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1048. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1049. * property data isn't large enough.
  1050. *
  1051. * dts entry of array should be like:
  1052. * property = /bits/ 8 <0x50 0x60 0x70>;
  1053. *
  1054. * The out_values is modified only if a valid u8 value can be decoded.
  1055. */
  1056. int of_property_read_u8_array(const struct device_node *np,
  1057. const char *propname, u8 *out_values, size_t sz)
  1058. {
  1059. const u8 *val = of_find_property_value_of_size(np, propname,
  1060. (sz * sizeof(*out_values)));
  1061. if (IS_ERR(val))
  1062. return PTR_ERR(val);
  1063. while (sz--)
  1064. *out_values++ = *val++;
  1065. return 0;
  1066. }
  1067. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  1068. /**
  1069. * of_property_read_u16_array - Find and read an array of u16 from a property.
  1070. *
  1071. * @np: device node from which the property value is to be read.
  1072. * @propname: name of the property to be searched.
  1073. * @out_values: pointer to return value, modified only if return value is 0.
  1074. * @sz: number of array elements to read
  1075. *
  1076. * Search for a property in a device node and read 16-bit value(s) from
  1077. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1078. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1079. * property data isn't large enough.
  1080. *
  1081. * dts entry of array should be like:
  1082. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  1083. *
  1084. * The out_values is modified only if a valid u16 value can be decoded.
  1085. */
  1086. int of_property_read_u16_array(const struct device_node *np,
  1087. const char *propname, u16 *out_values, size_t sz)
  1088. {
  1089. const __be16 *val = of_find_property_value_of_size(np, propname,
  1090. (sz * sizeof(*out_values)));
  1091. if (IS_ERR(val))
  1092. return PTR_ERR(val);
  1093. while (sz--)
  1094. *out_values++ = be16_to_cpup(val++);
  1095. return 0;
  1096. }
  1097. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  1098. /**
  1099. * of_property_read_u32_array - Find and read an array of 32 bit integers
  1100. * from a property.
  1101. *
  1102. * @np: device node from which the property value is to be read.
  1103. * @propname: name of the property to be searched.
  1104. * @out_values: pointer to return value, modified only if return value is 0.
  1105. * @sz: number of array elements to read
  1106. *
  1107. * Search for a property in a device node and read 32-bit value(s) from
  1108. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1109. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1110. * property data isn't large enough.
  1111. *
  1112. * The out_values is modified only if a valid u32 value can be decoded.
  1113. */
  1114. int of_property_read_u32_array(const struct device_node *np,
  1115. const char *propname, u32 *out_values,
  1116. size_t sz)
  1117. {
  1118. const __be32 *val = of_find_property_value_of_size(np, propname,
  1119. (sz * sizeof(*out_values)));
  1120. if (IS_ERR(val))
  1121. return PTR_ERR(val);
  1122. while (sz--)
  1123. *out_values++ = be32_to_cpup(val++);
  1124. return 0;
  1125. }
  1126. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  1127. /**
  1128. * of_property_read_u64 - Find and read a 64 bit integer from a property
  1129. * @np: device node from which the property value is to be read.
  1130. * @propname: name of the property to be searched.
  1131. * @out_value: pointer to return value, modified only if return value is 0.
  1132. *
  1133. * Search for a property in a device node and read a 64-bit value from
  1134. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1135. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1136. * property data isn't large enough.
  1137. *
  1138. * The out_value is modified only if a valid u64 value can be decoded.
  1139. */
  1140. int of_property_read_u64(const struct device_node *np, const char *propname,
  1141. u64 *out_value)
  1142. {
  1143. const __be32 *val = of_find_property_value_of_size(np, propname,
  1144. sizeof(*out_value));
  1145. if (IS_ERR(val))
  1146. return PTR_ERR(val);
  1147. *out_value = of_read_number(val, 2);
  1148. return 0;
  1149. }
  1150. EXPORT_SYMBOL_GPL(of_property_read_u64);
  1151. /**
  1152. * of_property_read_u64_array - Find and read an array of 64 bit integers
  1153. * from a property.
  1154. *
  1155. * @np: device node from which the property value is to be read.
  1156. * @propname: name of the property to be searched.
  1157. * @out_values: pointer to return value, modified only if return value is 0.
  1158. * @sz: number of array elements to read
  1159. *
  1160. * Search for a property in a device node and read 64-bit value(s) from
  1161. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1162. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1163. * property data isn't large enough.
  1164. *
  1165. * The out_values is modified only if a valid u64 value can be decoded.
  1166. */
  1167. int of_property_read_u64_array(const struct device_node *np,
  1168. const char *propname, u64 *out_values,
  1169. size_t sz)
  1170. {
  1171. const __be32 *val = of_find_property_value_of_size(np, propname,
  1172. (sz * sizeof(*out_values)));
  1173. if (IS_ERR(val))
  1174. return PTR_ERR(val);
  1175. while (sz--) {
  1176. *out_values++ = of_read_number(val, 2);
  1177. val += 2;
  1178. }
  1179. return 0;
  1180. }
  1181. EXPORT_SYMBOL_GPL(of_property_read_u64_array);
  1182. /**
  1183. * of_property_read_string - Find and read a string from a property
  1184. * @np: device node from which the property value is to be read.
  1185. * @propname: name of the property to be searched.
  1186. * @out_string: pointer to null terminated return string, modified only if
  1187. * return value is 0.
  1188. *
  1189. * Search for a property in a device tree node and retrieve a null
  1190. * terminated string value (pointer to data, not a copy). Returns 0 on
  1191. * success, -EINVAL if the property does not exist, -ENODATA if property
  1192. * does not have a value, and -EILSEQ if the string is not null-terminated
  1193. * within the length of the property data.
  1194. *
  1195. * The out_string pointer is modified only if a valid string can be decoded.
  1196. */
  1197. int of_property_read_string(struct device_node *np, const char *propname,
  1198. const char **out_string)
  1199. {
  1200. struct property *prop = of_find_property(np, propname, NULL);
  1201. if (!prop)
  1202. return -EINVAL;
  1203. if (!prop->value)
  1204. return -ENODATA;
  1205. if (strnlen(prop->value, prop->length) >= prop->length)
  1206. return -EILSEQ;
  1207. *out_string = prop->value;
  1208. return 0;
  1209. }
  1210. EXPORT_SYMBOL_GPL(of_property_read_string);
  1211. /**
  1212. * of_property_match_string() - Find string in a list and return index
  1213. * @np: pointer to node containing string list property
  1214. * @propname: string list property name
  1215. * @string: pointer to string to search for in string list
  1216. *
  1217. * This function searches a string list property and returns the index
  1218. * of a specific string value.
  1219. */
  1220. int of_property_match_string(struct device_node *np, const char *propname,
  1221. const char *string)
  1222. {
  1223. struct property *prop = of_find_property(np, propname, NULL);
  1224. size_t l;
  1225. int i;
  1226. const char *p, *end;
  1227. if (!prop)
  1228. return -EINVAL;
  1229. if (!prop->value)
  1230. return -ENODATA;
  1231. p = prop->value;
  1232. end = p + prop->length;
  1233. for (i = 0; p < end; i++, p += l) {
  1234. l = strnlen(p, end - p) + 1;
  1235. if (p + l > end)
  1236. return -EILSEQ;
  1237. pr_debug("comparing %s with %s\n", string, p);
  1238. if (strcmp(string, p) == 0)
  1239. return i; /* Found it; return index */
  1240. }
  1241. return -ENODATA;
  1242. }
  1243. EXPORT_SYMBOL_GPL(of_property_match_string);
  1244. /**
  1245. * of_property_read_string_helper() - Utility helper for parsing string properties
  1246. * @np: device node from which the property value is to be read.
  1247. * @propname: name of the property to be searched.
  1248. * @out_strs: output array of string pointers.
  1249. * @sz: number of array elements to read.
  1250. * @skip: Number of strings to skip over at beginning of list.
  1251. *
  1252. * Don't call this function directly. It is a utility helper for the
  1253. * of_property_read_string*() family of functions.
  1254. */
  1255. int of_property_read_string_helper(struct device_node *np, const char *propname,
  1256. const char **out_strs, size_t sz, int skip)
  1257. {
  1258. struct property *prop = of_find_property(np, propname, NULL);
  1259. int l = 0, i = 0;
  1260. const char *p, *end;
  1261. if (!prop)
  1262. return -EINVAL;
  1263. if (!prop->value)
  1264. return -ENODATA;
  1265. p = prop->value;
  1266. end = p + prop->length;
  1267. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  1268. l = strnlen(p, end - p) + 1;
  1269. if (p + l > end)
  1270. return -EILSEQ;
  1271. if (out_strs && i >= skip)
  1272. *out_strs++ = p;
  1273. }
  1274. i -= skip;
  1275. return i <= 0 ? -ENODATA : i;
  1276. }
  1277. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  1278. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1279. {
  1280. int i;
  1281. printk("%s %s", msg, of_node_full_name(args->np));
  1282. for (i = 0; i < args->args_count; i++)
  1283. printk(i ? ",%08x" : ":%08x", args->args[i]);
  1284. printk("\n");
  1285. }
  1286. static int __of_parse_phandle_with_args(const struct device_node *np,
  1287. const char *list_name,
  1288. const char *cells_name,
  1289. int cell_count, int index,
  1290. struct of_phandle_args *out_args)
  1291. {
  1292. const __be32 *list, *list_end;
  1293. int rc = 0, size, cur_index = 0;
  1294. uint32_t count = 0;
  1295. struct device_node *node = NULL;
  1296. phandle phandle;
  1297. /* Retrieve the phandle list property */
  1298. list = of_get_property(np, list_name, &size);
  1299. if (!list)
  1300. return -ENOENT;
  1301. list_end = list + size / sizeof(*list);
  1302. /* Loop over the phandles until all the requested entry is found */
  1303. while (list < list_end) {
  1304. rc = -EINVAL;
  1305. count = 0;
  1306. /*
  1307. * If phandle is 0, then it is an empty entry with no
  1308. * arguments. Skip forward to the next entry.
  1309. */
  1310. phandle = be32_to_cpup(list++);
  1311. if (phandle) {
  1312. /*
  1313. * Find the provider node and parse the #*-cells
  1314. * property to determine the argument length.
  1315. *
  1316. * This is not needed if the cell count is hard-coded
  1317. * (i.e. cells_name not set, but cell_count is set),
  1318. * except when we're going to return the found node
  1319. * below.
  1320. */
  1321. if (cells_name || cur_index == index) {
  1322. node = of_find_node_by_phandle(phandle);
  1323. if (!node) {
  1324. pr_err("%s: could not find phandle\n",
  1325. np->full_name);
  1326. goto err;
  1327. }
  1328. }
  1329. if (cells_name) {
  1330. if (of_property_read_u32(node, cells_name,
  1331. &count)) {
  1332. pr_err("%s: could not get %s for %s\n",
  1333. np->full_name, cells_name,
  1334. node->full_name);
  1335. goto err;
  1336. }
  1337. } else {
  1338. count = cell_count;
  1339. }
  1340. /*
  1341. * Make sure that the arguments actually fit in the
  1342. * remaining property data length
  1343. */
  1344. if (list + count > list_end) {
  1345. pr_err("%s: arguments longer than property\n",
  1346. np->full_name);
  1347. goto err;
  1348. }
  1349. }
  1350. /*
  1351. * All of the error cases above bail out of the loop, so at
  1352. * this point, the parsing is successful. If the requested
  1353. * index matches, then fill the out_args structure and return,
  1354. * or return -ENOENT for an empty entry.
  1355. */
  1356. rc = -ENOENT;
  1357. if (cur_index == index) {
  1358. if (!phandle)
  1359. goto err;
  1360. if (out_args) {
  1361. int i;
  1362. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1363. count = MAX_PHANDLE_ARGS;
  1364. out_args->np = node;
  1365. out_args->args_count = count;
  1366. for (i = 0; i < count; i++)
  1367. out_args->args[i] = be32_to_cpup(list++);
  1368. } else {
  1369. of_node_put(node);
  1370. }
  1371. /* Found it! return success */
  1372. return 0;
  1373. }
  1374. of_node_put(node);
  1375. node = NULL;
  1376. list += count;
  1377. cur_index++;
  1378. }
  1379. /*
  1380. * Unlock node before returning result; will be one of:
  1381. * -ENOENT : index is for empty phandle
  1382. * -EINVAL : parsing error on data
  1383. * [1..n] : Number of phandle (count mode; when index = -1)
  1384. */
  1385. rc = index < 0 ? cur_index : -ENOENT;
  1386. err:
  1387. if (node)
  1388. of_node_put(node);
  1389. return rc;
  1390. }
  1391. /**
  1392. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1393. * @np: Pointer to device node holding phandle property
  1394. * @phandle_name: Name of property holding a phandle value
  1395. * @index: For properties holding a table of phandles, this is the index into
  1396. * the table
  1397. *
  1398. * Returns the device_node pointer with refcount incremented. Use
  1399. * of_node_put() on it when done.
  1400. */
  1401. struct device_node *of_parse_phandle(const struct device_node *np,
  1402. const char *phandle_name, int index)
  1403. {
  1404. struct of_phandle_args args;
  1405. if (index < 0)
  1406. return NULL;
  1407. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1408. index, &args))
  1409. return NULL;
  1410. return args.np;
  1411. }
  1412. EXPORT_SYMBOL(of_parse_phandle);
  1413. /**
  1414. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1415. * @np: pointer to a device tree node containing a list
  1416. * @list_name: property name that contains a list
  1417. * @cells_name: property name that specifies phandles' arguments count
  1418. * @index: index of a phandle to parse out
  1419. * @out_args: optional pointer to output arguments structure (will be filled)
  1420. *
  1421. * This function is useful to parse lists of phandles and their arguments.
  1422. * Returns 0 on success and fills out_args, on error returns appropriate
  1423. * errno value.
  1424. *
  1425. * Caller is responsible to call of_node_put() on the returned out_args->np
  1426. * pointer.
  1427. *
  1428. * Example:
  1429. *
  1430. * phandle1: node1 {
  1431. * #list-cells = <2>;
  1432. * }
  1433. *
  1434. * phandle2: node2 {
  1435. * #list-cells = <1>;
  1436. * }
  1437. *
  1438. * node3 {
  1439. * list = <&phandle1 1 2 &phandle2 3>;
  1440. * }
  1441. *
  1442. * To get a device_node of the `node2' node you may call this:
  1443. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1444. */
  1445. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1446. const char *cells_name, int index,
  1447. struct of_phandle_args *out_args)
  1448. {
  1449. if (index < 0)
  1450. return -EINVAL;
  1451. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1452. index, out_args);
  1453. }
  1454. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1455. /**
  1456. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1457. * @np: pointer to a device tree node containing a list
  1458. * @list_name: property name that contains a list
  1459. * @cell_count: number of argument cells following the phandle
  1460. * @index: index of a phandle to parse out
  1461. * @out_args: optional pointer to output arguments structure (will be filled)
  1462. *
  1463. * This function is useful to parse lists of phandles and their arguments.
  1464. * Returns 0 on success and fills out_args, on error returns appropriate
  1465. * errno value.
  1466. *
  1467. * Caller is responsible to call of_node_put() on the returned out_args->np
  1468. * pointer.
  1469. *
  1470. * Example:
  1471. *
  1472. * phandle1: node1 {
  1473. * }
  1474. *
  1475. * phandle2: node2 {
  1476. * }
  1477. *
  1478. * node3 {
  1479. * list = <&phandle1 0 2 &phandle2 2 3>;
  1480. * }
  1481. *
  1482. * To get a device_node of the `node2' node you may call this:
  1483. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1484. */
  1485. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1486. const char *list_name, int cell_count,
  1487. int index, struct of_phandle_args *out_args)
  1488. {
  1489. if (index < 0)
  1490. return -EINVAL;
  1491. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1492. index, out_args);
  1493. }
  1494. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1495. /**
  1496. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1497. * @np: pointer to a device tree node containing a list
  1498. * @list_name: property name that contains a list
  1499. * @cells_name: property name that specifies phandles' arguments count
  1500. *
  1501. * Returns the number of phandle + argument tuples within a property. It
  1502. * is a typical pattern to encode a list of phandle and variable
  1503. * arguments into a single property. The number of arguments is encoded
  1504. * by a property in the phandle-target node. For example, a gpios
  1505. * property would contain a list of GPIO specifies consisting of a
  1506. * phandle and 1 or more arguments. The number of arguments are
  1507. * determined by the #gpio-cells property in the node pointed to by the
  1508. * phandle.
  1509. */
  1510. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1511. const char *cells_name)
  1512. {
  1513. return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
  1514. NULL);
  1515. }
  1516. EXPORT_SYMBOL(of_count_phandle_with_args);
  1517. /**
  1518. * __of_add_property - Add a property to a node without lock operations
  1519. */
  1520. int __of_add_property(struct device_node *np, struct property *prop)
  1521. {
  1522. struct property **next;
  1523. prop->next = NULL;
  1524. next = &np->properties;
  1525. while (*next) {
  1526. if (strcmp(prop->name, (*next)->name) == 0)
  1527. /* duplicate ! don't insert it */
  1528. return -EEXIST;
  1529. next = &(*next)->next;
  1530. }
  1531. *next = prop;
  1532. return 0;
  1533. }
  1534. /**
  1535. * of_add_property - Add a property to a node
  1536. */
  1537. int of_add_property(struct device_node *np, struct property *prop)
  1538. {
  1539. unsigned long flags;
  1540. int rc;
  1541. mutex_lock(&of_mutex);
  1542. raw_spin_lock_irqsave(&devtree_lock, flags);
  1543. rc = __of_add_property(np, prop);
  1544. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1545. if (!rc)
  1546. __of_add_property_sysfs(np, prop);
  1547. mutex_unlock(&of_mutex);
  1548. if (!rc)
  1549. of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
  1550. return rc;
  1551. }
  1552. int __of_remove_property(struct device_node *np, struct property *prop)
  1553. {
  1554. struct property **next;
  1555. for (next = &np->properties; *next; next = &(*next)->next) {
  1556. if (*next == prop)
  1557. break;
  1558. }
  1559. if (*next == NULL)
  1560. return -ENODEV;
  1561. /* found the node */
  1562. *next = prop->next;
  1563. prop->next = np->deadprops;
  1564. np->deadprops = prop;
  1565. return 0;
  1566. }
  1567. void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
  1568. {
  1569. if (!IS_ENABLED(CONFIG_SYSFS))
  1570. return;
  1571. /* at early boot, bail here and defer setup to of_init() */
  1572. if (of_kset && of_node_is_attached(np))
  1573. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1574. }
  1575. /**
  1576. * of_remove_property - Remove a property from a node.
  1577. *
  1578. * Note that we don't actually remove it, since we have given out
  1579. * who-knows-how-many pointers to the data using get-property.
  1580. * Instead we just move the property to the "dead properties"
  1581. * list, so it won't be found any more.
  1582. */
  1583. int of_remove_property(struct device_node *np, struct property *prop)
  1584. {
  1585. unsigned long flags;
  1586. int rc;
  1587. mutex_lock(&of_mutex);
  1588. raw_spin_lock_irqsave(&devtree_lock, flags);
  1589. rc = __of_remove_property(np, prop);
  1590. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1591. if (!rc)
  1592. __of_remove_property_sysfs(np, prop);
  1593. mutex_unlock(&of_mutex);
  1594. if (!rc)
  1595. of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
  1596. return rc;
  1597. }
  1598. int __of_update_property(struct device_node *np, struct property *newprop,
  1599. struct property **oldpropp)
  1600. {
  1601. struct property **next, *oldprop;
  1602. for (next = &np->properties; *next; next = &(*next)->next) {
  1603. if (of_prop_cmp((*next)->name, newprop->name) == 0)
  1604. break;
  1605. }
  1606. *oldpropp = oldprop = *next;
  1607. if (oldprop) {
  1608. /* replace the node */
  1609. newprop->next = oldprop->next;
  1610. *next = newprop;
  1611. oldprop->next = np->deadprops;
  1612. np->deadprops = oldprop;
  1613. } else {
  1614. /* new node */
  1615. newprop->next = NULL;
  1616. *next = newprop;
  1617. }
  1618. return 0;
  1619. }
  1620. void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
  1621. struct property *oldprop)
  1622. {
  1623. if (!IS_ENABLED(CONFIG_SYSFS))
  1624. return;
  1625. /* At early boot, bail out and defer setup to of_init() */
  1626. if (!of_kset)
  1627. return;
  1628. if (oldprop)
  1629. sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
  1630. __of_add_property_sysfs(np, newprop);
  1631. }
  1632. /*
  1633. * of_update_property - Update a property in a node, if the property does
  1634. * not exist, add it.
  1635. *
  1636. * Note that we don't actually remove it, since we have given out
  1637. * who-knows-how-many pointers to the data using get-property.
  1638. * Instead we just move the property to the "dead properties" list,
  1639. * and add the new property to the property list
  1640. */
  1641. int of_update_property(struct device_node *np, struct property *newprop)
  1642. {
  1643. struct property *oldprop;
  1644. unsigned long flags;
  1645. int rc;
  1646. if (!newprop->name)
  1647. return -EINVAL;
  1648. mutex_lock(&of_mutex);
  1649. raw_spin_lock_irqsave(&devtree_lock, flags);
  1650. rc = __of_update_property(np, newprop, &oldprop);
  1651. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1652. if (!rc)
  1653. __of_update_property_sysfs(np, newprop, oldprop);
  1654. mutex_unlock(&of_mutex);
  1655. if (!rc)
  1656. of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
  1657. return rc;
  1658. }
  1659. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1660. int id, const char *stem, int stem_len)
  1661. {
  1662. ap->np = np;
  1663. ap->id = id;
  1664. strncpy(ap->stem, stem, stem_len);
  1665. ap->stem[stem_len] = 0;
  1666. list_add_tail(&ap->link, &aliases_lookup);
  1667. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1668. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1669. }
  1670. /**
  1671. * of_alias_scan - Scan all properties of the 'aliases' node
  1672. *
  1673. * The function scans all the properties of the 'aliases' node and populates
  1674. * the global lookup table with the properties. It returns the
  1675. * number of alias properties found, or an error code in case of failure.
  1676. *
  1677. * @dt_alloc: An allocator that provides a virtual address to memory
  1678. * for storing the resulting tree
  1679. */
  1680. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1681. {
  1682. struct property *pp;
  1683. of_aliases = of_find_node_by_path("/aliases");
  1684. of_chosen = of_find_node_by_path("/chosen");
  1685. if (of_chosen == NULL)
  1686. of_chosen = of_find_node_by_path("/chosen@0");
  1687. if (of_chosen) {
  1688. /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
  1689. const char *name = of_get_property(of_chosen, "stdout-path", NULL);
  1690. if (!name)
  1691. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1692. if (IS_ENABLED(CONFIG_PPC) && !name)
  1693. name = of_get_property(of_aliases, "stdout", NULL);
  1694. if (name)
  1695. of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
  1696. }
  1697. if (!of_aliases)
  1698. return;
  1699. for_each_property_of_node(of_aliases, pp) {
  1700. const char *start = pp->name;
  1701. const char *end = start + strlen(start);
  1702. struct device_node *np;
  1703. struct alias_prop *ap;
  1704. int id, len;
  1705. /* Skip those we do not want to proceed */
  1706. if (!strcmp(pp->name, "name") ||
  1707. !strcmp(pp->name, "phandle") ||
  1708. !strcmp(pp->name, "linux,phandle"))
  1709. continue;
  1710. np = of_find_node_by_path(pp->value);
  1711. if (!np)
  1712. continue;
  1713. /* walk the alias backwards to extract the id and work out
  1714. * the 'stem' string */
  1715. while (isdigit(*(end-1)) && end > start)
  1716. end--;
  1717. len = end - start;
  1718. if (kstrtoint(end, 10, &id) < 0)
  1719. continue;
  1720. /* Allocate an alias_prop with enough space for the stem */
  1721. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1722. if (!ap)
  1723. continue;
  1724. memset(ap, 0, sizeof(*ap) + len + 1);
  1725. ap->alias = start;
  1726. of_alias_add(ap, np, id, start, len);
  1727. }
  1728. }
  1729. /**
  1730. * of_alias_get_id - Get alias id for the given device_node
  1731. * @np: Pointer to the given device_node
  1732. * @stem: Alias stem of the given device_node
  1733. *
  1734. * The function travels the lookup table to get the alias id for the given
  1735. * device_node and alias stem. It returns the alias id if found.
  1736. */
  1737. int of_alias_get_id(struct device_node *np, const char *stem)
  1738. {
  1739. struct alias_prop *app;
  1740. int id = -ENODEV;
  1741. mutex_lock(&of_mutex);
  1742. list_for_each_entry(app, &aliases_lookup, link) {
  1743. if (strcmp(app->stem, stem) != 0)
  1744. continue;
  1745. if (np == app->np) {
  1746. id = app->id;
  1747. break;
  1748. }
  1749. }
  1750. mutex_unlock(&of_mutex);
  1751. return id;
  1752. }
  1753. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1754. /**
  1755. * of_alias_get_highest_id - Get highest alias id for the given stem
  1756. * @stem: Alias stem to be examined
  1757. *
  1758. * The function travels the lookup table to get the highest alias id for the
  1759. * given alias stem. It returns the alias id if found.
  1760. */
  1761. int of_alias_get_highest_id(const char *stem)
  1762. {
  1763. struct alias_prop *app;
  1764. int id = -ENODEV;
  1765. mutex_lock(&of_mutex);
  1766. list_for_each_entry(app, &aliases_lookup, link) {
  1767. if (strcmp(app->stem, stem) != 0)
  1768. continue;
  1769. if (app->id > id)
  1770. id = app->id;
  1771. }
  1772. mutex_unlock(&of_mutex);
  1773. return id;
  1774. }
  1775. EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
  1776. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1777. u32 *pu)
  1778. {
  1779. const void *curv = cur;
  1780. if (!prop)
  1781. return NULL;
  1782. if (!cur) {
  1783. curv = prop->value;
  1784. goto out_val;
  1785. }
  1786. curv += sizeof(*cur);
  1787. if (curv >= prop->value + prop->length)
  1788. return NULL;
  1789. out_val:
  1790. *pu = be32_to_cpup(curv);
  1791. return curv;
  1792. }
  1793. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1794. const char *of_prop_next_string(struct property *prop, const char *cur)
  1795. {
  1796. const void *curv = cur;
  1797. if (!prop)
  1798. return NULL;
  1799. if (!cur)
  1800. return prop->value;
  1801. curv += strlen(cur) + 1;
  1802. if (curv >= prop->value + prop->length)
  1803. return NULL;
  1804. return curv;
  1805. }
  1806. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1807. /**
  1808. * of_console_check() - Test and setup console for DT setup
  1809. * @dn - Pointer to device node
  1810. * @name - Name to use for preferred console without index. ex. "ttyS"
  1811. * @index - Index to use for preferred console.
  1812. *
  1813. * Check if the given device node matches the stdout-path property in the
  1814. * /chosen node. If it does then register it as the preferred console and return
  1815. * TRUE. Otherwise return FALSE.
  1816. */
  1817. bool of_console_check(struct device_node *dn, char *name, int index)
  1818. {
  1819. if (!dn || dn != of_stdout || console_set_on_cmdline)
  1820. return false;
  1821. return !add_preferred_console(name, index,
  1822. kstrdup(of_stdout_options, GFP_KERNEL));
  1823. }
  1824. EXPORT_SYMBOL_GPL(of_console_check);
  1825. /**
  1826. * of_find_next_cache_node - Find a node's subsidiary cache
  1827. * @np: node of type "cpu" or "cache"
  1828. *
  1829. * Returns a node pointer with refcount incremented, use
  1830. * of_node_put() on it when done. Caller should hold a reference
  1831. * to np.
  1832. */
  1833. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1834. {
  1835. struct device_node *child;
  1836. const phandle *handle;
  1837. handle = of_get_property(np, "l2-cache", NULL);
  1838. if (!handle)
  1839. handle = of_get_property(np, "next-level-cache", NULL);
  1840. if (handle)
  1841. return of_find_node_by_phandle(be32_to_cpup(handle));
  1842. /* OF on pmac has nodes instead of properties named "l2-cache"
  1843. * beneath CPU nodes.
  1844. */
  1845. if (!strcmp(np->type, "cpu"))
  1846. for_each_child_of_node(np, child)
  1847. if (!strcmp(child->type, "cache"))
  1848. return child;
  1849. return NULL;
  1850. }
  1851. /**
  1852. * of_graph_parse_endpoint() - parse common endpoint node properties
  1853. * @node: pointer to endpoint device_node
  1854. * @endpoint: pointer to the OF endpoint data structure
  1855. *
  1856. * The caller should hold a reference to @node.
  1857. */
  1858. int of_graph_parse_endpoint(const struct device_node *node,
  1859. struct of_endpoint *endpoint)
  1860. {
  1861. struct device_node *port_node = of_get_parent(node);
  1862. WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
  1863. __func__, node->full_name);
  1864. memset(endpoint, 0, sizeof(*endpoint));
  1865. endpoint->local_node = node;
  1866. /*
  1867. * It doesn't matter whether the two calls below succeed.
  1868. * If they don't then the default value 0 is used.
  1869. */
  1870. of_property_read_u32(port_node, "reg", &endpoint->port);
  1871. of_property_read_u32(node, "reg", &endpoint->id);
  1872. of_node_put(port_node);
  1873. return 0;
  1874. }
  1875. EXPORT_SYMBOL(of_graph_parse_endpoint);
  1876. /**
  1877. * of_graph_get_port_by_id() - get the port matching a given id
  1878. * @parent: pointer to the parent device node
  1879. * @id: id of the port
  1880. *
  1881. * Return: A 'port' node pointer with refcount incremented. The caller
  1882. * has to use of_node_put() on it when done.
  1883. */
  1884. struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
  1885. {
  1886. struct device_node *node, *port;
  1887. node = of_get_child_by_name(parent, "ports");
  1888. if (node)
  1889. parent = node;
  1890. for_each_child_of_node(parent, port) {
  1891. u32 port_id = 0;
  1892. if (of_node_cmp(port->name, "port") != 0)
  1893. continue;
  1894. of_property_read_u32(port, "reg", &port_id);
  1895. if (id == port_id)
  1896. break;
  1897. }
  1898. of_node_put(node);
  1899. return port;
  1900. }
  1901. EXPORT_SYMBOL(of_graph_get_port_by_id);
  1902. /**
  1903. * of_graph_get_next_endpoint() - get next endpoint node
  1904. * @parent: pointer to the parent device node
  1905. * @prev: previous endpoint node, or NULL to get first
  1906. *
  1907. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  1908. * of the passed @prev node is decremented.
  1909. */
  1910. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  1911. struct device_node *prev)
  1912. {
  1913. struct device_node *endpoint;
  1914. struct device_node *port;
  1915. if (!parent)
  1916. return NULL;
  1917. /*
  1918. * Start by locating the port node. If no previous endpoint is specified
  1919. * search for the first port node, otherwise get the previous endpoint
  1920. * parent port node.
  1921. */
  1922. if (!prev) {
  1923. struct device_node *node;
  1924. node = of_get_child_by_name(parent, "ports");
  1925. if (node)
  1926. parent = node;
  1927. port = of_get_child_by_name(parent, "port");
  1928. of_node_put(node);
  1929. if (!port) {
  1930. pr_err("%s(): no port node found in %s\n",
  1931. __func__, parent->full_name);
  1932. return NULL;
  1933. }
  1934. } else {
  1935. port = of_get_parent(prev);
  1936. if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
  1937. __func__, prev->full_name))
  1938. return NULL;
  1939. }
  1940. while (1) {
  1941. /*
  1942. * Now that we have a port node, get the next endpoint by
  1943. * getting the next child. If the previous endpoint is NULL this
  1944. * will return the first child.
  1945. */
  1946. endpoint = of_get_next_child(port, prev);
  1947. if (endpoint) {
  1948. of_node_put(port);
  1949. return endpoint;
  1950. }
  1951. /* No more endpoints under this port, try the next one. */
  1952. prev = NULL;
  1953. do {
  1954. port = of_get_next_child(parent, port);
  1955. if (!port)
  1956. return NULL;
  1957. } while (of_node_cmp(port->name, "port"));
  1958. }
  1959. }
  1960. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  1961. /**
  1962. * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
  1963. * @parent: pointer to the parent device node
  1964. * @port_reg: identifier (value of reg property) of the parent port node
  1965. * @reg: identifier (value of reg property) of the endpoint node
  1966. *
  1967. * Return: An 'endpoint' node pointer which is identified by reg and at the same
  1968. * is the child of a port node identified by port_reg. reg and port_reg are
  1969. * ignored when they are -1.
  1970. */
  1971. struct device_node *of_graph_get_endpoint_by_regs(
  1972. const struct device_node *parent, int port_reg, int reg)
  1973. {
  1974. struct of_endpoint endpoint;
  1975. struct device_node *node, *prev_node = NULL;
  1976. while (1) {
  1977. node = of_graph_get_next_endpoint(parent, prev_node);
  1978. of_node_put(prev_node);
  1979. if (!node)
  1980. break;
  1981. of_graph_parse_endpoint(node, &endpoint);
  1982. if (((port_reg == -1) || (endpoint.port == port_reg)) &&
  1983. ((reg == -1) || (endpoint.id == reg)))
  1984. return node;
  1985. prev_node = node;
  1986. }
  1987. return NULL;
  1988. }
  1989. EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
  1990. /**
  1991. * of_graph_get_remote_port_parent() - get remote port's parent node
  1992. * @node: pointer to a local endpoint device_node
  1993. *
  1994. * Return: Remote device node associated with remote endpoint node linked
  1995. * to @node. Use of_node_put() on it when done.
  1996. */
  1997. struct device_node *of_graph_get_remote_port_parent(
  1998. const struct device_node *node)
  1999. {
  2000. struct device_node *np;
  2001. unsigned int depth;
  2002. /* Get remote endpoint node. */
  2003. np = of_parse_phandle(node, "remote-endpoint", 0);
  2004. /* Walk 3 levels up only if there is 'ports' node. */
  2005. for (depth = 3; depth && np; depth--) {
  2006. np = of_get_next_parent(np);
  2007. if (depth == 2 && of_node_cmp(np->name, "ports"))
  2008. break;
  2009. }
  2010. return np;
  2011. }
  2012. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  2013. /**
  2014. * of_graph_get_remote_port() - get remote port node
  2015. * @node: pointer to a local endpoint device_node
  2016. *
  2017. * Return: Remote port node associated with remote endpoint node linked
  2018. * to @node. Use of_node_put() on it when done.
  2019. */
  2020. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  2021. {
  2022. struct device_node *np;
  2023. /* Get remote endpoint node. */
  2024. np = of_parse_phandle(node, "remote-endpoint", 0);
  2025. if (!np)
  2026. return NULL;
  2027. return of_get_next_parent(np);
  2028. }
  2029. EXPORT_SYMBOL(of_graph_get_remote_port);