base.c 66 KB

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