base.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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/ctype.h>
  21. #include <linux/cpu.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/slab.h>
  26. #include <linux/proc_fs.h>
  27. #include "of_private.h"
  28. LIST_HEAD(aliases_lookup);
  29. struct device_node *of_allnodes;
  30. EXPORT_SYMBOL(of_allnodes);
  31. struct device_node *of_chosen;
  32. struct device_node *of_aliases;
  33. static struct device_node *of_stdout;
  34. DEFINE_MUTEX(of_aliases_mutex);
  35. /* use when traversing tree through the allnext, child, sibling,
  36. * or parent members of struct device_node.
  37. */
  38. DEFINE_RAW_SPINLOCK(devtree_lock);
  39. int of_n_addr_cells(struct device_node *np)
  40. {
  41. const __be32 *ip;
  42. do {
  43. if (np->parent)
  44. np = np->parent;
  45. ip = of_get_property(np, "#address-cells", NULL);
  46. if (ip)
  47. return be32_to_cpup(ip);
  48. } while (np->parent);
  49. /* No #address-cells property for the root node */
  50. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  51. }
  52. EXPORT_SYMBOL(of_n_addr_cells);
  53. int of_n_size_cells(struct device_node *np)
  54. {
  55. const __be32 *ip;
  56. do {
  57. if (np->parent)
  58. np = np->parent;
  59. ip = of_get_property(np, "#size-cells", NULL);
  60. if (ip)
  61. return be32_to_cpup(ip);
  62. } while (np->parent);
  63. /* No #size-cells property for the root node */
  64. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  65. }
  66. EXPORT_SYMBOL(of_n_size_cells);
  67. #ifdef CONFIG_NUMA
  68. int __weak of_node_to_nid(struct device_node *np)
  69. {
  70. return numa_node_id();
  71. }
  72. #endif
  73. #if defined(CONFIG_OF_DYNAMIC)
  74. /**
  75. * of_node_get - Increment refcount of a node
  76. * @node: Node to inc refcount, NULL is supported to
  77. * simplify writing of callers
  78. *
  79. * Returns node.
  80. */
  81. struct device_node *of_node_get(struct device_node *node)
  82. {
  83. if (node)
  84. kref_get(&node->kref);
  85. return node;
  86. }
  87. EXPORT_SYMBOL(of_node_get);
  88. static inline struct device_node *kref_to_device_node(struct kref *kref)
  89. {
  90. return container_of(kref, struct device_node, kref);
  91. }
  92. /**
  93. * of_node_release - release a dynamically allocated node
  94. * @kref: kref element of the node to be released
  95. *
  96. * In of_node_put() this function is passed to kref_put()
  97. * as the destructor.
  98. */
  99. static void of_node_release(struct kref *kref)
  100. {
  101. struct device_node *node = kref_to_device_node(kref);
  102. struct property *prop = node->properties;
  103. /* We should never be releasing nodes that haven't been detached. */
  104. if (!of_node_check_flag(node, OF_DETACHED)) {
  105. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  106. dump_stack();
  107. kref_init(&node->kref);
  108. return;
  109. }
  110. if (!of_node_check_flag(node, OF_DYNAMIC))
  111. return;
  112. while (prop) {
  113. struct property *next = prop->next;
  114. kfree(prop->name);
  115. kfree(prop->value);
  116. kfree(prop);
  117. prop = next;
  118. if (!prop) {
  119. prop = node->deadprops;
  120. node->deadprops = NULL;
  121. }
  122. }
  123. kfree(node->full_name);
  124. kfree(node->data);
  125. kfree(node);
  126. }
  127. /**
  128. * of_node_put - Decrement refcount of a node
  129. * @node: Node to dec refcount, NULL is supported to
  130. * simplify writing of callers
  131. *
  132. */
  133. void of_node_put(struct device_node *node)
  134. {
  135. if (node)
  136. kref_put(&node->kref, of_node_release);
  137. }
  138. EXPORT_SYMBOL(of_node_put);
  139. #endif /* CONFIG_OF_DYNAMIC */
  140. static struct property *__of_find_property(const struct device_node *np,
  141. const char *name, int *lenp)
  142. {
  143. struct property *pp;
  144. if (!np)
  145. return NULL;
  146. for (pp = np->properties; pp; pp = pp->next) {
  147. if (of_prop_cmp(pp->name, name) == 0) {
  148. if (lenp)
  149. *lenp = pp->length;
  150. break;
  151. }
  152. }
  153. return pp;
  154. }
  155. struct property *of_find_property(const struct device_node *np,
  156. const char *name,
  157. int *lenp)
  158. {
  159. struct property *pp;
  160. unsigned long flags;
  161. raw_spin_lock_irqsave(&devtree_lock, flags);
  162. pp = __of_find_property(np, name, lenp);
  163. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  164. return pp;
  165. }
  166. EXPORT_SYMBOL(of_find_property);
  167. /**
  168. * of_find_all_nodes - Get next node in global list
  169. * @prev: Previous node or NULL to start iteration
  170. * of_node_put() will be called on it
  171. *
  172. * Returns a node pointer with refcount incremented, use
  173. * of_node_put() on it when done.
  174. */
  175. struct device_node *of_find_all_nodes(struct device_node *prev)
  176. {
  177. struct device_node *np;
  178. unsigned long flags;
  179. raw_spin_lock_irqsave(&devtree_lock, flags);
  180. np = prev ? prev->allnext : of_allnodes;
  181. for (; np != NULL; np = np->allnext)
  182. if (of_node_get(np))
  183. break;
  184. of_node_put(prev);
  185. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  186. return np;
  187. }
  188. EXPORT_SYMBOL(of_find_all_nodes);
  189. /*
  190. * Find a property with a given name for a given node
  191. * and return the value.
  192. */
  193. static const void *__of_get_property(const struct device_node *np,
  194. const char *name, int *lenp)
  195. {
  196. struct property *pp = __of_find_property(np, name, lenp);
  197. return pp ? pp->value : NULL;
  198. }
  199. /*
  200. * Find a property with a given name for a given node
  201. * and return the value.
  202. */
  203. const void *of_get_property(const struct device_node *np, const char *name,
  204. int *lenp)
  205. {
  206. struct property *pp = of_find_property(np, name, lenp);
  207. return pp ? pp->value : NULL;
  208. }
  209. EXPORT_SYMBOL(of_get_property);
  210. /*
  211. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  212. *
  213. * @cpu: logical cpu index of a core/thread
  214. * @phys_id: physical identifier of a core/thread
  215. *
  216. * CPU logical to physical index mapping is architecture specific.
  217. * However this __weak function provides a default match of physical
  218. * id to logical cpu index. phys_id provided here is usually values read
  219. * from the device tree which must match the hardware internal registers.
  220. *
  221. * Returns true if the physical identifier and the logical cpu index
  222. * correspond to the same core/thread, false otherwise.
  223. */
  224. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  225. {
  226. return (u32)phys_id == cpu;
  227. }
  228. /**
  229. * Checks if the given "prop_name" property holds the physical id of the
  230. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  231. * NULL, local thread number within the core is returned in it.
  232. */
  233. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  234. const char *prop_name, int cpu, unsigned int *thread)
  235. {
  236. const __be32 *cell;
  237. int ac, prop_len, tid;
  238. u64 hwid;
  239. ac = of_n_addr_cells(cpun);
  240. cell = of_get_property(cpun, prop_name, &prop_len);
  241. if (!cell || !ac)
  242. return false;
  243. prop_len /= sizeof(*cell) * ac;
  244. for (tid = 0; tid < prop_len; tid++) {
  245. hwid = of_read_number(cell, ac);
  246. if (arch_match_cpu_phys_id(cpu, hwid)) {
  247. if (thread)
  248. *thread = tid;
  249. return true;
  250. }
  251. cell += ac;
  252. }
  253. return false;
  254. }
  255. /*
  256. * arch_find_n_match_cpu_physical_id - See if the given device node is
  257. * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
  258. * else false. If 'thread' is non-NULL, the local thread number within the
  259. * core is returned in it.
  260. */
  261. bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  262. int cpu, unsigned int *thread)
  263. {
  264. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  265. * for thread ids on PowerPC. If it doesn't exist fallback to
  266. * standard "reg" property.
  267. */
  268. if (IS_ENABLED(CONFIG_PPC) &&
  269. __of_find_n_match_cpu_property(cpun,
  270. "ibm,ppc-interrupt-server#s",
  271. cpu, thread))
  272. return true;
  273. if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
  274. return true;
  275. return false;
  276. }
  277. /**
  278. * of_get_cpu_node - Get device node associated with the given logical CPU
  279. *
  280. * @cpu: CPU number(logical index) for which device node is required
  281. * @thread: if not NULL, local thread number within the physical core is
  282. * returned
  283. *
  284. * The main purpose of this function is to retrieve the device node for the
  285. * given logical CPU index. It should be used to initialize the of_node in
  286. * cpu device. Once of_node in cpu device is populated, all the further
  287. * references can use that instead.
  288. *
  289. * CPU logical to physical index mapping is architecture specific and is built
  290. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  291. * which can be overridden by architecture specific implementation.
  292. *
  293. * Returns a node pointer for the logical cpu if found, else NULL.
  294. */
  295. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  296. {
  297. struct device_node *cpun;
  298. for_each_node_by_type(cpun, "cpu") {
  299. if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
  300. return cpun;
  301. }
  302. return NULL;
  303. }
  304. EXPORT_SYMBOL(of_get_cpu_node);
  305. /** Checks if the given "compat" string matches one of the strings in
  306. * the device's "compatible" property
  307. */
  308. static int __of_device_is_compatible(const struct device_node *device,
  309. const char *compat)
  310. {
  311. const char* cp;
  312. int cplen, l;
  313. cp = __of_get_property(device, "compatible", &cplen);
  314. if (cp == NULL)
  315. return 0;
  316. while (cplen > 0) {
  317. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  318. return 1;
  319. l = strlen(cp) + 1;
  320. cp += l;
  321. cplen -= l;
  322. }
  323. return 0;
  324. }
  325. /** Checks if the given "compat" string matches one of the strings in
  326. * the device's "compatible" property
  327. */
  328. int of_device_is_compatible(const struct device_node *device,
  329. const char *compat)
  330. {
  331. unsigned long flags;
  332. int res;
  333. raw_spin_lock_irqsave(&devtree_lock, flags);
  334. res = __of_device_is_compatible(device, compat);
  335. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  336. return res;
  337. }
  338. EXPORT_SYMBOL(of_device_is_compatible);
  339. /**
  340. * of_machine_is_compatible - Test root of device tree for a given compatible value
  341. * @compat: compatible string to look for in root node's compatible property.
  342. *
  343. * Returns true if the root node has the given value in its
  344. * compatible property.
  345. */
  346. int of_machine_is_compatible(const char *compat)
  347. {
  348. struct device_node *root;
  349. int rc = 0;
  350. root = of_find_node_by_path("/");
  351. if (root) {
  352. rc = of_device_is_compatible(root, compat);
  353. of_node_put(root);
  354. }
  355. return rc;
  356. }
  357. EXPORT_SYMBOL(of_machine_is_compatible);
  358. /**
  359. * __of_device_is_available - check if a device is available for use
  360. *
  361. * @device: Node to check for availability, with locks already held
  362. *
  363. * Returns 1 if the status property is absent or set to "okay" or "ok",
  364. * 0 otherwise
  365. */
  366. static int __of_device_is_available(const struct device_node *device)
  367. {
  368. const char *status;
  369. int statlen;
  370. if (!device)
  371. return 0;
  372. status = __of_get_property(device, "status", &statlen);
  373. if (status == NULL)
  374. return 1;
  375. if (statlen > 0) {
  376. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  377. return 1;
  378. }
  379. return 0;
  380. }
  381. /**
  382. * of_device_is_available - check if a device is available for use
  383. *
  384. * @device: Node to check for availability
  385. *
  386. * Returns 1 if the status property is absent or set to "okay" or "ok",
  387. * 0 otherwise
  388. */
  389. int of_device_is_available(const struct device_node *device)
  390. {
  391. unsigned long flags;
  392. int res;
  393. raw_spin_lock_irqsave(&devtree_lock, flags);
  394. res = __of_device_is_available(device);
  395. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  396. return res;
  397. }
  398. EXPORT_SYMBOL(of_device_is_available);
  399. /**
  400. * of_get_parent - Get a node's parent if any
  401. * @node: Node to get parent
  402. *
  403. * Returns a node pointer with refcount incremented, use
  404. * of_node_put() on it when done.
  405. */
  406. struct device_node *of_get_parent(const struct device_node *node)
  407. {
  408. struct device_node *np;
  409. unsigned long flags;
  410. if (!node)
  411. return NULL;
  412. raw_spin_lock_irqsave(&devtree_lock, flags);
  413. np = of_node_get(node->parent);
  414. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  415. return np;
  416. }
  417. EXPORT_SYMBOL(of_get_parent);
  418. /**
  419. * of_get_next_parent - Iterate to a node's parent
  420. * @node: Node to get parent of
  421. *
  422. * This is like of_get_parent() except that it drops the
  423. * refcount on the passed node, making it suitable for iterating
  424. * through a node's parents.
  425. *
  426. * Returns a node pointer with refcount incremented, use
  427. * of_node_put() on it when done.
  428. */
  429. struct device_node *of_get_next_parent(struct device_node *node)
  430. {
  431. struct device_node *parent;
  432. unsigned long flags;
  433. if (!node)
  434. return NULL;
  435. raw_spin_lock_irqsave(&devtree_lock, flags);
  436. parent = of_node_get(node->parent);
  437. of_node_put(node);
  438. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  439. return parent;
  440. }
  441. EXPORT_SYMBOL(of_get_next_parent);
  442. /**
  443. * of_get_next_child - Iterate a node childs
  444. * @node: parent node
  445. * @prev: previous child of the parent node, or NULL to get first
  446. *
  447. * Returns a node pointer with refcount incremented, use
  448. * of_node_put() on it when done.
  449. */
  450. struct device_node *of_get_next_child(const struct device_node *node,
  451. struct device_node *prev)
  452. {
  453. struct device_node *next;
  454. unsigned long flags;
  455. raw_spin_lock_irqsave(&devtree_lock, flags);
  456. next = prev ? prev->sibling : node->child;
  457. for (; next; next = next->sibling)
  458. if (of_node_get(next))
  459. break;
  460. of_node_put(prev);
  461. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  462. return next;
  463. }
  464. EXPORT_SYMBOL(of_get_next_child);
  465. /**
  466. * of_get_next_available_child - Find the next available child node
  467. * @node: parent node
  468. * @prev: previous child of the parent node, or NULL to get first
  469. *
  470. * This function is like of_get_next_child(), except that it
  471. * automatically skips any disabled nodes (i.e. status = "disabled").
  472. */
  473. struct device_node *of_get_next_available_child(const struct device_node *node,
  474. struct device_node *prev)
  475. {
  476. struct device_node *next;
  477. unsigned long flags;
  478. raw_spin_lock_irqsave(&devtree_lock, flags);
  479. next = prev ? prev->sibling : node->child;
  480. for (; next; next = next->sibling) {
  481. if (!__of_device_is_available(next))
  482. continue;
  483. if (of_node_get(next))
  484. break;
  485. }
  486. of_node_put(prev);
  487. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  488. return next;
  489. }
  490. EXPORT_SYMBOL(of_get_next_available_child);
  491. /**
  492. * of_get_child_by_name - Find the child node by name for a given parent
  493. * @node: parent node
  494. * @name: child name to look for.
  495. *
  496. * This function looks for child node for given matching name
  497. *
  498. * Returns a node pointer if found, with refcount incremented, use
  499. * of_node_put() on it when done.
  500. * Returns NULL if node is not found.
  501. */
  502. struct device_node *of_get_child_by_name(const struct device_node *node,
  503. const char *name)
  504. {
  505. struct device_node *child;
  506. for_each_child_of_node(node, child)
  507. if (child->name && (of_node_cmp(child->name, name) == 0))
  508. break;
  509. return child;
  510. }
  511. EXPORT_SYMBOL(of_get_child_by_name);
  512. /**
  513. * of_find_node_by_path - Find a node matching a full OF path
  514. * @path: The full path to match
  515. *
  516. * Returns a node pointer with refcount incremented, use
  517. * of_node_put() on it when done.
  518. */
  519. struct device_node *of_find_node_by_path(const char *path)
  520. {
  521. struct device_node *np = of_allnodes;
  522. unsigned long flags;
  523. raw_spin_lock_irqsave(&devtree_lock, flags);
  524. for (; np; np = np->allnext) {
  525. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  526. && of_node_get(np))
  527. break;
  528. }
  529. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  530. return np;
  531. }
  532. EXPORT_SYMBOL(of_find_node_by_path);
  533. /**
  534. * of_find_node_by_name - Find a node by its "name" property
  535. * @from: The node to start searching from or NULL, the node
  536. * you pass will not be searched, only the next one
  537. * will; typically, you pass what the previous call
  538. * returned. of_node_put() will be called on it
  539. * @name: The name string to match against
  540. *
  541. * Returns a node pointer with refcount incremented, use
  542. * of_node_put() on it when done.
  543. */
  544. struct device_node *of_find_node_by_name(struct device_node *from,
  545. const char *name)
  546. {
  547. struct device_node *np;
  548. unsigned long flags;
  549. raw_spin_lock_irqsave(&devtree_lock, flags);
  550. np = from ? from->allnext : of_allnodes;
  551. for (; np; np = np->allnext)
  552. if (np->name && (of_node_cmp(np->name, name) == 0)
  553. && of_node_get(np))
  554. break;
  555. of_node_put(from);
  556. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  557. return np;
  558. }
  559. EXPORT_SYMBOL(of_find_node_by_name);
  560. /**
  561. * of_find_node_by_type - Find a node by its "device_type" property
  562. * @from: The node to start searching from, or NULL to start searching
  563. * the entire device tree. The node you pass will not be
  564. * searched, only the next one will; typically, you pass
  565. * what the previous call returned. of_node_put() will be
  566. * called on from for you.
  567. * @type: The type string to match against
  568. *
  569. * Returns a node pointer with refcount incremented, use
  570. * of_node_put() on it when done.
  571. */
  572. struct device_node *of_find_node_by_type(struct device_node *from,
  573. const char *type)
  574. {
  575. struct device_node *np;
  576. unsigned long flags;
  577. raw_spin_lock_irqsave(&devtree_lock, flags);
  578. np = from ? from->allnext : of_allnodes;
  579. for (; np; np = np->allnext)
  580. if (np->type && (of_node_cmp(np->type, type) == 0)
  581. && of_node_get(np))
  582. break;
  583. of_node_put(from);
  584. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  585. return np;
  586. }
  587. EXPORT_SYMBOL(of_find_node_by_type);
  588. /**
  589. * of_find_compatible_node - Find a node based on type and one of the
  590. * tokens in its "compatible" property
  591. * @from: The node to start searching from or NULL, the node
  592. * you pass will not be searched, only the next one
  593. * will; typically, you pass what the previous call
  594. * returned. of_node_put() will be called on it
  595. * @type: The type string to match "device_type" or NULL to ignore
  596. * @compatible: The string to match to one of the tokens in the device
  597. * "compatible" list.
  598. *
  599. * Returns a node pointer with refcount incremented, use
  600. * of_node_put() on it when done.
  601. */
  602. struct device_node *of_find_compatible_node(struct device_node *from,
  603. const char *type, const char *compatible)
  604. {
  605. struct device_node *np;
  606. unsigned long flags;
  607. raw_spin_lock_irqsave(&devtree_lock, flags);
  608. np = from ? from->allnext : of_allnodes;
  609. for (; np; np = np->allnext) {
  610. if (type
  611. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  612. continue;
  613. if (__of_device_is_compatible(np, compatible) &&
  614. of_node_get(np))
  615. break;
  616. }
  617. of_node_put(from);
  618. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  619. return np;
  620. }
  621. EXPORT_SYMBOL(of_find_compatible_node);
  622. /**
  623. * of_find_node_with_property - Find a node which has a property with
  624. * the given name.
  625. * @from: The node to start searching from or NULL, the node
  626. * you pass will not be searched, only the next one
  627. * will; typically, you pass what the previous call
  628. * returned. of_node_put() will be called on it
  629. * @prop_name: The name of the property to look for.
  630. *
  631. * Returns a node pointer with refcount incremented, use
  632. * of_node_put() on it when done.
  633. */
  634. struct device_node *of_find_node_with_property(struct device_node *from,
  635. const char *prop_name)
  636. {
  637. struct device_node *np;
  638. struct property *pp;
  639. unsigned long flags;
  640. raw_spin_lock_irqsave(&devtree_lock, flags);
  641. np = from ? from->allnext : of_allnodes;
  642. for (; np; np = np->allnext) {
  643. for (pp = np->properties; pp; pp = pp->next) {
  644. if (of_prop_cmp(pp->name, prop_name) == 0) {
  645. of_node_get(np);
  646. goto out;
  647. }
  648. }
  649. }
  650. out:
  651. of_node_put(from);
  652. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  653. return np;
  654. }
  655. EXPORT_SYMBOL(of_find_node_with_property);
  656. static const struct of_device_id *
  657. of_match_compatible(const struct of_device_id *matches,
  658. const struct device_node *node)
  659. {
  660. const char *cp;
  661. int cplen, l;
  662. const struct of_device_id *m;
  663. cp = __of_get_property(node, "compatible", &cplen);
  664. while (cp && (cplen > 0)) {
  665. m = matches;
  666. while (m->name[0] || m->type[0] || m->compatible[0]) {
  667. /* Only match for the entries without type and name */
  668. if (m->name[0] || m->type[0] ||
  669. of_compat_cmp(m->compatible, cp,
  670. strlen(m->compatible)))
  671. m++;
  672. else
  673. return m;
  674. }
  675. /* Get node's next compatible string */
  676. l = strlen(cp) + 1;
  677. cp += l;
  678. cplen -= l;
  679. }
  680. return NULL;
  681. }
  682. static
  683. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  684. const struct device_node *node)
  685. {
  686. const struct of_device_id *m;
  687. if (!matches)
  688. return NULL;
  689. m = of_match_compatible(matches, node);
  690. if (m)
  691. return m;
  692. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  693. int match = 1;
  694. if (matches->name[0])
  695. match &= node->name
  696. && !strcmp(matches->name, node->name);
  697. if (matches->type[0])
  698. match &= node->type
  699. && !strcmp(matches->type, node->type);
  700. if (matches->compatible[0])
  701. match &= __of_device_is_compatible(node,
  702. matches->compatible);
  703. if (match)
  704. return matches;
  705. matches++;
  706. }
  707. return NULL;
  708. }
  709. /**
  710. * of_match_node - Tell if an device_node has a matching of_match structure
  711. * @matches: array of of device match structures to search in
  712. * @node: the of device structure to match against
  713. *
  714. * Low level utility function used by device matching. We have two ways
  715. * of matching:
  716. * - Try to find the best compatible match by comparing each compatible
  717. * string of device node with all the given matches respectively.
  718. * - If the above method failed, then try to match the compatible by using
  719. * __of_device_is_compatible() besides the match in type and name.
  720. */
  721. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  722. const struct device_node *node)
  723. {
  724. const struct of_device_id *match;
  725. unsigned long flags;
  726. raw_spin_lock_irqsave(&devtree_lock, flags);
  727. match = __of_match_node(matches, node);
  728. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  729. return match;
  730. }
  731. EXPORT_SYMBOL(of_match_node);
  732. /**
  733. * of_find_matching_node_and_match - Find a node based on an of_device_id
  734. * match table.
  735. * @from: The node to start searching from or NULL, the node
  736. * you pass will not be searched, only the next one
  737. * will; typically, you pass what the previous call
  738. * returned. of_node_put() will be called on it
  739. * @matches: array of of device match structures to search in
  740. * @match Updated to point at the matches entry which matched
  741. *
  742. * Returns a node pointer with refcount incremented, use
  743. * of_node_put() on it when done.
  744. */
  745. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  746. const struct of_device_id *matches,
  747. const struct of_device_id **match)
  748. {
  749. struct device_node *np;
  750. const struct of_device_id *m;
  751. unsigned long flags;
  752. if (match)
  753. *match = NULL;
  754. raw_spin_lock_irqsave(&devtree_lock, flags);
  755. np = from ? from->allnext : of_allnodes;
  756. for (; np; np = np->allnext) {
  757. m = __of_match_node(matches, np);
  758. if (m && of_node_get(np)) {
  759. if (match)
  760. *match = m;
  761. break;
  762. }
  763. }
  764. of_node_put(from);
  765. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  766. return np;
  767. }
  768. EXPORT_SYMBOL(of_find_matching_node_and_match);
  769. /**
  770. * of_modalias_node - Lookup appropriate modalias for a device node
  771. * @node: pointer to a device tree node
  772. * @modalias: Pointer to buffer that modalias value will be copied into
  773. * @len: Length of modalias value
  774. *
  775. * Based on the value of the compatible property, this routine will attempt
  776. * to choose an appropriate modalias value for a particular device tree node.
  777. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  778. * from the first entry in the compatible list property.
  779. *
  780. * This routine returns 0 on success, <0 on failure.
  781. */
  782. int of_modalias_node(struct device_node *node, char *modalias, int len)
  783. {
  784. const char *compatible, *p;
  785. int cplen;
  786. compatible = of_get_property(node, "compatible", &cplen);
  787. if (!compatible || strlen(compatible) > cplen)
  788. return -ENODEV;
  789. p = strchr(compatible, ',');
  790. strlcpy(modalias, p ? p + 1 : compatible, len);
  791. return 0;
  792. }
  793. EXPORT_SYMBOL_GPL(of_modalias_node);
  794. /**
  795. * of_find_node_by_phandle - Find a node given a phandle
  796. * @handle: phandle of the node to find
  797. *
  798. * Returns a node pointer with refcount incremented, use
  799. * of_node_put() on it when done.
  800. */
  801. struct device_node *of_find_node_by_phandle(phandle handle)
  802. {
  803. struct device_node *np;
  804. unsigned long flags;
  805. raw_spin_lock_irqsave(&devtree_lock, flags);
  806. for (np = of_allnodes; np; np = np->allnext)
  807. if (np->phandle == handle)
  808. break;
  809. of_node_get(np);
  810. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  811. return np;
  812. }
  813. EXPORT_SYMBOL(of_find_node_by_phandle);
  814. /**
  815. * of_find_property_value_of_size
  816. *
  817. * @np: device node from which the property value is to be read.
  818. * @propname: name of the property to be searched.
  819. * @len: requested length of property value
  820. *
  821. * Search for a property in a device node and valid the requested size.
  822. * Returns the property value on success, -EINVAL if the property does not
  823. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  824. * property data isn't large enough.
  825. *
  826. */
  827. static void *of_find_property_value_of_size(const struct device_node *np,
  828. const char *propname, u32 len)
  829. {
  830. struct property *prop = of_find_property(np, propname, NULL);
  831. if (!prop)
  832. return ERR_PTR(-EINVAL);
  833. if (!prop->value)
  834. return ERR_PTR(-ENODATA);
  835. if (len > prop->length)
  836. return ERR_PTR(-EOVERFLOW);
  837. return prop->value;
  838. }
  839. /**
  840. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  841. *
  842. * @np: device node from which the property value is to be read.
  843. * @propname: name of the property to be searched.
  844. * @index: index of the u32 in the list of values
  845. * @out_value: pointer to return value, modified only if no error.
  846. *
  847. * Search for a property in a device node and read nth 32-bit value from
  848. * it. Returns 0 on success, -EINVAL if the property does not exist,
  849. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  850. * property data isn't large enough.
  851. *
  852. * The out_value is modified only if a valid u32 value can be decoded.
  853. */
  854. int of_property_read_u32_index(const struct device_node *np,
  855. const char *propname,
  856. u32 index, u32 *out_value)
  857. {
  858. const u32 *val = of_find_property_value_of_size(np, propname,
  859. ((index + 1) * sizeof(*out_value)));
  860. if (IS_ERR(val))
  861. return PTR_ERR(val);
  862. *out_value = be32_to_cpup(((__be32 *)val) + index);
  863. return 0;
  864. }
  865. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  866. /**
  867. * of_property_read_u8_array - Find and read an array of u8 from a property.
  868. *
  869. * @np: device node from which the property value is to be read.
  870. * @propname: name of the property to be searched.
  871. * @out_values: pointer to return value, modified only if return value is 0.
  872. * @sz: number of array elements to read
  873. *
  874. * Search for a property in a device node and read 8-bit value(s) from
  875. * it. Returns 0 on success, -EINVAL if the property does not exist,
  876. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  877. * property data isn't large enough.
  878. *
  879. * dts entry of array should be like:
  880. * property = /bits/ 8 <0x50 0x60 0x70>;
  881. *
  882. * The out_values is modified only if a valid u8 value can be decoded.
  883. */
  884. int of_property_read_u8_array(const struct device_node *np,
  885. const char *propname, u8 *out_values, size_t sz)
  886. {
  887. const u8 *val = of_find_property_value_of_size(np, propname,
  888. (sz * sizeof(*out_values)));
  889. if (IS_ERR(val))
  890. return PTR_ERR(val);
  891. while (sz--)
  892. *out_values++ = *val++;
  893. return 0;
  894. }
  895. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  896. /**
  897. * of_property_read_u16_array - Find and read an array of u16 from a property.
  898. *
  899. * @np: device node from which the property value is to be read.
  900. * @propname: name of the property to be searched.
  901. * @out_values: pointer to return value, modified only if return value is 0.
  902. * @sz: number of array elements to read
  903. *
  904. * Search for a property in a device node and read 16-bit value(s) from
  905. * it. Returns 0 on success, -EINVAL if the property does not exist,
  906. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  907. * property data isn't large enough.
  908. *
  909. * dts entry of array should be like:
  910. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  911. *
  912. * The out_values is modified only if a valid u16 value can be decoded.
  913. */
  914. int of_property_read_u16_array(const struct device_node *np,
  915. const char *propname, u16 *out_values, size_t sz)
  916. {
  917. const __be16 *val = of_find_property_value_of_size(np, propname,
  918. (sz * sizeof(*out_values)));
  919. if (IS_ERR(val))
  920. return PTR_ERR(val);
  921. while (sz--)
  922. *out_values++ = be16_to_cpup(val++);
  923. return 0;
  924. }
  925. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  926. /**
  927. * of_property_read_u32_array - Find and read an array of 32 bit integers
  928. * from a property.
  929. *
  930. * @np: device node from which the property value is to be read.
  931. * @propname: name of the property to be searched.
  932. * @out_values: pointer to return value, modified only if return value is 0.
  933. * @sz: number of array elements to read
  934. *
  935. * Search for a property in a device node and read 32-bit value(s) from
  936. * it. Returns 0 on success, -EINVAL if the property does not exist,
  937. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  938. * property data isn't large enough.
  939. *
  940. * The out_values is modified only if a valid u32 value can be decoded.
  941. */
  942. int of_property_read_u32_array(const struct device_node *np,
  943. const char *propname, u32 *out_values,
  944. size_t sz)
  945. {
  946. const __be32 *val = of_find_property_value_of_size(np, propname,
  947. (sz * sizeof(*out_values)));
  948. if (IS_ERR(val))
  949. return PTR_ERR(val);
  950. while (sz--)
  951. *out_values++ = be32_to_cpup(val++);
  952. return 0;
  953. }
  954. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  955. /**
  956. * of_property_read_u64 - Find and read a 64 bit integer from a property
  957. * @np: device node from which the property value is to be read.
  958. * @propname: name of the property to be searched.
  959. * @out_value: pointer to return value, modified only if return value is 0.
  960. *
  961. * Search for a property in a device node and read a 64-bit value from
  962. * it. Returns 0 on success, -EINVAL if the property does not exist,
  963. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  964. * property data isn't large enough.
  965. *
  966. * The out_value is modified only if a valid u64 value can be decoded.
  967. */
  968. int of_property_read_u64(const struct device_node *np, const char *propname,
  969. u64 *out_value)
  970. {
  971. const __be32 *val = of_find_property_value_of_size(np, propname,
  972. sizeof(*out_value));
  973. if (IS_ERR(val))
  974. return PTR_ERR(val);
  975. *out_value = of_read_number(val, 2);
  976. return 0;
  977. }
  978. EXPORT_SYMBOL_GPL(of_property_read_u64);
  979. /**
  980. * of_property_read_string - Find and read a string from a property
  981. * @np: device node from which the property value is to be read.
  982. * @propname: name of the property to be searched.
  983. * @out_string: pointer to null terminated return string, modified only if
  984. * return value is 0.
  985. *
  986. * Search for a property in a device tree node and retrieve a null
  987. * terminated string value (pointer to data, not a copy). Returns 0 on
  988. * success, -EINVAL if the property does not exist, -ENODATA if property
  989. * does not have a value, and -EILSEQ if the string is not null-terminated
  990. * within the length of the property data.
  991. *
  992. * The out_string pointer is modified only if a valid string can be decoded.
  993. */
  994. int of_property_read_string(struct device_node *np, const char *propname,
  995. const char **out_string)
  996. {
  997. struct property *prop = of_find_property(np, propname, NULL);
  998. if (!prop)
  999. return -EINVAL;
  1000. if (!prop->value)
  1001. return -ENODATA;
  1002. if (strnlen(prop->value, prop->length) >= prop->length)
  1003. return -EILSEQ;
  1004. *out_string = prop->value;
  1005. return 0;
  1006. }
  1007. EXPORT_SYMBOL_GPL(of_property_read_string);
  1008. /**
  1009. * of_property_read_string_index - Find and read a string from a multiple
  1010. * strings property.
  1011. * @np: device node from which the property value is to be read.
  1012. * @propname: name of the property to be searched.
  1013. * @index: index of the string in the list of strings
  1014. * @out_string: pointer to null terminated return string, modified only if
  1015. * return value is 0.
  1016. *
  1017. * Search for a property in a device tree node and retrieve a null
  1018. * terminated string value (pointer to data, not a copy) in the list of strings
  1019. * contained in that property.
  1020. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  1021. * property does not have a value, and -EILSEQ if the string is not
  1022. * null-terminated within the length of the property data.
  1023. *
  1024. * The out_string pointer is modified only if a valid string can be decoded.
  1025. */
  1026. int of_property_read_string_index(struct device_node *np, const char *propname,
  1027. int index, const char **output)
  1028. {
  1029. struct property *prop = of_find_property(np, propname, NULL);
  1030. int i = 0;
  1031. size_t l = 0, total = 0;
  1032. const char *p;
  1033. if (!prop)
  1034. return -EINVAL;
  1035. if (!prop->value)
  1036. return -ENODATA;
  1037. if (strnlen(prop->value, prop->length) >= prop->length)
  1038. return -EILSEQ;
  1039. p = prop->value;
  1040. for (i = 0; total < prop->length; total += l, p += l) {
  1041. l = strlen(p) + 1;
  1042. if (i++ == index) {
  1043. *output = p;
  1044. return 0;
  1045. }
  1046. }
  1047. return -ENODATA;
  1048. }
  1049. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  1050. /**
  1051. * of_property_match_string() - Find string in a list and return index
  1052. * @np: pointer to node containing string list property
  1053. * @propname: string list property name
  1054. * @string: pointer to string to search for in string list
  1055. *
  1056. * This function searches a string list property and returns the index
  1057. * of a specific string value.
  1058. */
  1059. int of_property_match_string(struct device_node *np, const char *propname,
  1060. const char *string)
  1061. {
  1062. struct property *prop = of_find_property(np, propname, NULL);
  1063. size_t l;
  1064. int i;
  1065. const char *p, *end;
  1066. if (!prop)
  1067. return -EINVAL;
  1068. if (!prop->value)
  1069. return -ENODATA;
  1070. p = prop->value;
  1071. end = p + prop->length;
  1072. for (i = 0; p < end; i++, p += l) {
  1073. l = strlen(p) + 1;
  1074. if (p + l > end)
  1075. return -EILSEQ;
  1076. pr_debug("comparing %s with %s\n", string, p);
  1077. if (strcmp(string, p) == 0)
  1078. return i; /* Found it; return index */
  1079. }
  1080. return -ENODATA;
  1081. }
  1082. EXPORT_SYMBOL_GPL(of_property_match_string);
  1083. /**
  1084. * of_property_count_strings - Find and return the number of strings from a
  1085. * multiple strings property.
  1086. * @np: device node from which the property value is to be read.
  1087. * @propname: name of the property to be searched.
  1088. *
  1089. * Search for a property in a device tree node and retrieve the number of null
  1090. * terminated string contain in it. Returns the number of strings on
  1091. * success, -EINVAL if the property does not exist, -ENODATA if property
  1092. * does not have a value, and -EILSEQ if the string is not null-terminated
  1093. * within the length of the property data.
  1094. */
  1095. int of_property_count_strings(struct device_node *np, const char *propname)
  1096. {
  1097. struct property *prop = of_find_property(np, propname, NULL);
  1098. int i = 0;
  1099. size_t l = 0, total = 0;
  1100. const char *p;
  1101. if (!prop)
  1102. return -EINVAL;
  1103. if (!prop->value)
  1104. return -ENODATA;
  1105. if (strnlen(prop->value, prop->length) >= prop->length)
  1106. return -EILSEQ;
  1107. p = prop->value;
  1108. for (i = 0; total < prop->length; total += l, p += l, i++)
  1109. l = strlen(p) + 1;
  1110. return i;
  1111. }
  1112. EXPORT_SYMBOL_GPL(of_property_count_strings);
  1113. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1114. {
  1115. int i;
  1116. printk("%s %s", msg, of_node_full_name(args->np));
  1117. for (i = 0; i < args->args_count; i++)
  1118. printk(i ? ",%08x" : ":%08x", args->args[i]);
  1119. printk("\n");
  1120. }
  1121. static int __of_parse_phandle_with_args(const struct device_node *np,
  1122. const char *list_name,
  1123. const char *cells_name,
  1124. int cell_count, int index,
  1125. struct of_phandle_args *out_args)
  1126. {
  1127. const __be32 *list, *list_end;
  1128. int rc = 0, size, cur_index = 0;
  1129. uint32_t count = 0;
  1130. struct device_node *node = NULL;
  1131. phandle phandle;
  1132. /* Retrieve the phandle list property */
  1133. list = of_get_property(np, list_name, &size);
  1134. if (!list)
  1135. return -ENOENT;
  1136. list_end = list + size / sizeof(*list);
  1137. /* Loop over the phandles until all the requested entry is found */
  1138. while (list < list_end) {
  1139. rc = -EINVAL;
  1140. count = 0;
  1141. /*
  1142. * If phandle is 0, then it is an empty entry with no
  1143. * arguments. Skip forward to the next entry.
  1144. */
  1145. phandle = be32_to_cpup(list++);
  1146. if (phandle) {
  1147. /*
  1148. * Find the provider node and parse the #*-cells
  1149. * property to determine the argument length.
  1150. *
  1151. * This is not needed if the cell count is hard-coded
  1152. * (i.e. cells_name not set, but cell_count is set),
  1153. * except when we're going to return the found node
  1154. * below.
  1155. */
  1156. if (cells_name || cur_index == index) {
  1157. node = of_find_node_by_phandle(phandle);
  1158. if (!node) {
  1159. pr_err("%s: could not find phandle\n",
  1160. np->full_name);
  1161. goto err;
  1162. }
  1163. }
  1164. if (cells_name) {
  1165. if (of_property_read_u32(node, cells_name,
  1166. &count)) {
  1167. pr_err("%s: could not get %s for %s\n",
  1168. np->full_name, cells_name,
  1169. node->full_name);
  1170. goto err;
  1171. }
  1172. } else {
  1173. count = cell_count;
  1174. }
  1175. /*
  1176. * Make sure that the arguments actually fit in the
  1177. * remaining property data length
  1178. */
  1179. if (list + count > list_end) {
  1180. pr_err("%s: arguments longer than property\n",
  1181. np->full_name);
  1182. goto err;
  1183. }
  1184. }
  1185. /*
  1186. * All of the error cases above bail out of the loop, so at
  1187. * this point, the parsing is successful. If the requested
  1188. * index matches, then fill the out_args structure and return,
  1189. * or return -ENOENT for an empty entry.
  1190. */
  1191. rc = -ENOENT;
  1192. if (cur_index == index) {
  1193. if (!phandle)
  1194. goto err;
  1195. if (out_args) {
  1196. int i;
  1197. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1198. count = MAX_PHANDLE_ARGS;
  1199. out_args->np = node;
  1200. out_args->args_count = count;
  1201. for (i = 0; i < count; i++)
  1202. out_args->args[i] = be32_to_cpup(list++);
  1203. } else {
  1204. of_node_put(node);
  1205. }
  1206. /* Found it! return success */
  1207. return 0;
  1208. }
  1209. of_node_put(node);
  1210. node = NULL;
  1211. list += count;
  1212. cur_index++;
  1213. }
  1214. /*
  1215. * Unlock node before returning result; will be one of:
  1216. * -ENOENT : index is for empty phandle
  1217. * -EINVAL : parsing error on data
  1218. * [1..n] : Number of phandle (count mode; when index = -1)
  1219. */
  1220. rc = index < 0 ? cur_index : -ENOENT;
  1221. err:
  1222. if (node)
  1223. of_node_put(node);
  1224. return rc;
  1225. }
  1226. /**
  1227. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1228. * @np: Pointer to device node holding phandle property
  1229. * @phandle_name: Name of property holding a phandle value
  1230. * @index: For properties holding a table of phandles, this is the index into
  1231. * the table
  1232. *
  1233. * Returns the device_node pointer with refcount incremented. Use
  1234. * of_node_put() on it when done.
  1235. */
  1236. struct device_node *of_parse_phandle(const struct device_node *np,
  1237. const char *phandle_name, int index)
  1238. {
  1239. struct of_phandle_args args;
  1240. if (index < 0)
  1241. return NULL;
  1242. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1243. index, &args))
  1244. return NULL;
  1245. return args.np;
  1246. }
  1247. EXPORT_SYMBOL(of_parse_phandle);
  1248. /**
  1249. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1250. * @np: pointer to a device tree node containing a list
  1251. * @list_name: property name that contains a list
  1252. * @cells_name: property name that specifies phandles' arguments count
  1253. * @index: index of a phandle to parse out
  1254. * @out_args: optional pointer to output arguments structure (will be filled)
  1255. *
  1256. * This function is useful to parse lists of phandles and their arguments.
  1257. * Returns 0 on success and fills out_args, on error returns appropriate
  1258. * errno value.
  1259. *
  1260. * Caller is responsible to call of_node_put() on the returned out_args->node
  1261. * pointer.
  1262. *
  1263. * Example:
  1264. *
  1265. * phandle1: node1 {
  1266. * #list-cells = <2>;
  1267. * }
  1268. *
  1269. * phandle2: node2 {
  1270. * #list-cells = <1>;
  1271. * }
  1272. *
  1273. * node3 {
  1274. * list = <&phandle1 1 2 &phandle2 3>;
  1275. * }
  1276. *
  1277. * To get a device_node of the `node2' node you may call this:
  1278. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1279. */
  1280. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1281. const char *cells_name, int index,
  1282. struct of_phandle_args *out_args)
  1283. {
  1284. if (index < 0)
  1285. return -EINVAL;
  1286. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1287. index, out_args);
  1288. }
  1289. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1290. /**
  1291. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1292. * @np: pointer to a device tree node containing a list
  1293. * @list_name: property name that contains a list
  1294. * @cell_count: number of argument cells following the phandle
  1295. * @index: index of a phandle to parse out
  1296. * @out_args: optional pointer to output arguments structure (will be filled)
  1297. *
  1298. * This function is useful to parse lists of phandles and their arguments.
  1299. * Returns 0 on success and fills out_args, on error returns appropriate
  1300. * errno value.
  1301. *
  1302. * Caller is responsible to call of_node_put() on the returned out_args->node
  1303. * pointer.
  1304. *
  1305. * Example:
  1306. *
  1307. * phandle1: node1 {
  1308. * }
  1309. *
  1310. * phandle2: node2 {
  1311. * }
  1312. *
  1313. * node3 {
  1314. * list = <&phandle1 0 2 &phandle2 2 3>;
  1315. * }
  1316. *
  1317. * To get a device_node of the `node2' node you may call this:
  1318. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1319. */
  1320. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1321. const char *list_name, int cell_count,
  1322. int index, struct of_phandle_args *out_args)
  1323. {
  1324. if (index < 0)
  1325. return -EINVAL;
  1326. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1327. index, out_args);
  1328. }
  1329. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1330. /**
  1331. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1332. * @np: pointer to a device tree node containing a list
  1333. * @list_name: property name that contains a list
  1334. * @cells_name: property name that specifies phandles' arguments count
  1335. *
  1336. * Returns the number of phandle + argument tuples within a property. It
  1337. * is a typical pattern to encode a list of phandle and variable
  1338. * arguments into a single property. The number of arguments is encoded
  1339. * by a property in the phandle-target node. For example, a gpios
  1340. * property would contain a list of GPIO specifies consisting of a
  1341. * phandle and 1 or more arguments. The number of arguments are
  1342. * determined by the #gpio-cells property in the node pointed to by the
  1343. * phandle.
  1344. */
  1345. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1346. const char *cells_name)
  1347. {
  1348. return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
  1349. NULL);
  1350. }
  1351. EXPORT_SYMBOL(of_count_phandle_with_args);
  1352. #if defined(CONFIG_OF_DYNAMIC)
  1353. static int of_property_notify(int action, struct device_node *np,
  1354. struct property *prop)
  1355. {
  1356. struct of_prop_reconfig pr;
  1357. pr.dn = np;
  1358. pr.prop = prop;
  1359. return of_reconfig_notify(action, &pr);
  1360. }
  1361. #else
  1362. static int of_property_notify(int action, struct device_node *np,
  1363. struct property *prop)
  1364. {
  1365. return 0;
  1366. }
  1367. #endif
  1368. /**
  1369. * of_add_property - Add a property to a node
  1370. */
  1371. int of_add_property(struct device_node *np, struct property *prop)
  1372. {
  1373. struct property **next;
  1374. unsigned long flags;
  1375. int rc;
  1376. rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
  1377. if (rc)
  1378. return rc;
  1379. prop->next = NULL;
  1380. raw_spin_lock_irqsave(&devtree_lock, flags);
  1381. next = &np->properties;
  1382. while (*next) {
  1383. if (strcmp(prop->name, (*next)->name) == 0) {
  1384. /* duplicate ! don't insert it */
  1385. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1386. return -1;
  1387. }
  1388. next = &(*next)->next;
  1389. }
  1390. *next = prop;
  1391. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1392. #ifdef CONFIG_PROC_DEVICETREE
  1393. /* try to add to proc as well if it was initialized */
  1394. if (np->pde)
  1395. proc_device_tree_add_prop(np->pde, prop);
  1396. #endif /* CONFIG_PROC_DEVICETREE */
  1397. return 0;
  1398. }
  1399. /**
  1400. * of_remove_property - Remove a property from a node.
  1401. *
  1402. * Note that we don't actually remove it, since we have given out
  1403. * who-knows-how-many pointers to the data using get-property.
  1404. * Instead we just move the property to the "dead properties"
  1405. * list, so it won't be found any more.
  1406. */
  1407. int of_remove_property(struct device_node *np, struct property *prop)
  1408. {
  1409. struct property **next;
  1410. unsigned long flags;
  1411. int found = 0;
  1412. int rc;
  1413. rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1414. if (rc)
  1415. return rc;
  1416. raw_spin_lock_irqsave(&devtree_lock, flags);
  1417. next = &np->properties;
  1418. while (*next) {
  1419. if (*next == prop) {
  1420. /* found the node */
  1421. *next = prop->next;
  1422. prop->next = np->deadprops;
  1423. np->deadprops = prop;
  1424. found = 1;
  1425. break;
  1426. }
  1427. next = &(*next)->next;
  1428. }
  1429. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1430. if (!found)
  1431. return -ENODEV;
  1432. #ifdef CONFIG_PROC_DEVICETREE
  1433. /* try to remove the proc node as well */
  1434. if (np->pde)
  1435. proc_device_tree_remove_prop(np->pde, prop);
  1436. #endif /* CONFIG_PROC_DEVICETREE */
  1437. return 0;
  1438. }
  1439. /*
  1440. * of_update_property - Update a property in a node, if the property does
  1441. * not exist, add it.
  1442. *
  1443. * Note that we don't actually remove it, since we have given out
  1444. * who-knows-how-many pointers to the data using get-property.
  1445. * Instead we just move the property to the "dead properties" list,
  1446. * and add the new property to the property list
  1447. */
  1448. int of_update_property(struct device_node *np, struct property *newprop)
  1449. {
  1450. struct property **next, *oldprop;
  1451. unsigned long flags;
  1452. int rc, found = 0;
  1453. rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
  1454. if (rc)
  1455. return rc;
  1456. if (!newprop->name)
  1457. return -EINVAL;
  1458. oldprop = of_find_property(np, newprop->name, NULL);
  1459. if (!oldprop)
  1460. return of_add_property(np, newprop);
  1461. raw_spin_lock_irqsave(&devtree_lock, flags);
  1462. next = &np->properties;
  1463. while (*next) {
  1464. if (*next == oldprop) {
  1465. /* found the node */
  1466. newprop->next = oldprop->next;
  1467. *next = newprop;
  1468. oldprop->next = np->deadprops;
  1469. np->deadprops = oldprop;
  1470. found = 1;
  1471. break;
  1472. }
  1473. next = &(*next)->next;
  1474. }
  1475. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1476. if (!found)
  1477. return -ENODEV;
  1478. #ifdef CONFIG_PROC_DEVICETREE
  1479. /* try to add to proc as well if it was initialized */
  1480. if (np->pde)
  1481. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1482. #endif /* CONFIG_PROC_DEVICETREE */
  1483. return 0;
  1484. }
  1485. #if defined(CONFIG_OF_DYNAMIC)
  1486. /*
  1487. * Support for dynamic device trees.
  1488. *
  1489. * On some platforms, the device tree can be manipulated at runtime.
  1490. * The routines in this section support adding, removing and changing
  1491. * device tree nodes.
  1492. */
  1493. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  1494. int of_reconfig_notifier_register(struct notifier_block *nb)
  1495. {
  1496. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  1497. }
  1498. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  1499. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1500. {
  1501. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  1502. }
  1503. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  1504. int of_reconfig_notify(unsigned long action, void *p)
  1505. {
  1506. int rc;
  1507. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  1508. return notifier_to_errno(rc);
  1509. }
  1510. #ifdef CONFIG_PROC_DEVICETREE
  1511. static void of_add_proc_dt_entry(struct device_node *dn)
  1512. {
  1513. struct proc_dir_entry *ent;
  1514. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  1515. if (ent)
  1516. proc_device_tree_add_node(dn, ent);
  1517. }
  1518. #else
  1519. static void of_add_proc_dt_entry(struct device_node *dn)
  1520. {
  1521. return;
  1522. }
  1523. #endif
  1524. /**
  1525. * of_attach_node - Plug a device node into the tree and global list.
  1526. */
  1527. int of_attach_node(struct device_node *np)
  1528. {
  1529. unsigned long flags;
  1530. int rc;
  1531. rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  1532. if (rc)
  1533. return rc;
  1534. raw_spin_lock_irqsave(&devtree_lock, flags);
  1535. np->sibling = np->parent->child;
  1536. np->allnext = of_allnodes;
  1537. np->parent->child = np;
  1538. of_allnodes = np;
  1539. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1540. of_add_proc_dt_entry(np);
  1541. return 0;
  1542. }
  1543. #ifdef CONFIG_PROC_DEVICETREE
  1544. static void of_remove_proc_dt_entry(struct device_node *dn)
  1545. {
  1546. proc_remove(dn->pde);
  1547. }
  1548. #else
  1549. static void of_remove_proc_dt_entry(struct device_node *dn)
  1550. {
  1551. return;
  1552. }
  1553. #endif
  1554. /**
  1555. * of_detach_node - "Unplug" a node from the device tree.
  1556. *
  1557. * The caller must hold a reference to the node. The memory associated with
  1558. * the node is not freed until its refcount goes to zero.
  1559. */
  1560. int of_detach_node(struct device_node *np)
  1561. {
  1562. struct device_node *parent;
  1563. unsigned long flags;
  1564. int rc = 0;
  1565. rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  1566. if (rc)
  1567. return rc;
  1568. raw_spin_lock_irqsave(&devtree_lock, flags);
  1569. if (of_node_check_flag(np, OF_DETACHED)) {
  1570. /* someone already detached it */
  1571. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1572. return rc;
  1573. }
  1574. parent = np->parent;
  1575. if (!parent) {
  1576. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1577. return rc;
  1578. }
  1579. if (of_allnodes == np)
  1580. of_allnodes = np->allnext;
  1581. else {
  1582. struct device_node *prev;
  1583. for (prev = of_allnodes;
  1584. prev->allnext != np;
  1585. prev = prev->allnext)
  1586. ;
  1587. prev->allnext = np->allnext;
  1588. }
  1589. if (parent->child == np)
  1590. parent->child = np->sibling;
  1591. else {
  1592. struct device_node *prevsib;
  1593. for (prevsib = np->parent->child;
  1594. prevsib->sibling != np;
  1595. prevsib = prevsib->sibling)
  1596. ;
  1597. prevsib->sibling = np->sibling;
  1598. }
  1599. of_node_set_flag(np, OF_DETACHED);
  1600. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1601. of_remove_proc_dt_entry(np);
  1602. return rc;
  1603. }
  1604. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1605. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1606. int id, const char *stem, int stem_len)
  1607. {
  1608. ap->np = np;
  1609. ap->id = id;
  1610. strncpy(ap->stem, stem, stem_len);
  1611. ap->stem[stem_len] = 0;
  1612. list_add_tail(&ap->link, &aliases_lookup);
  1613. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1614. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1615. }
  1616. /**
  1617. * of_alias_scan - Scan all properties of 'aliases' node
  1618. *
  1619. * The function scans all the properties of 'aliases' node and populate
  1620. * the the global lookup table with the properties. It returns the
  1621. * number of alias_prop found, or error code in error case.
  1622. *
  1623. * @dt_alloc: An allocator that provides a virtual address to memory
  1624. * for the resulting tree
  1625. */
  1626. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1627. {
  1628. struct property *pp;
  1629. of_chosen = of_find_node_by_path("/chosen");
  1630. if (of_chosen == NULL)
  1631. of_chosen = of_find_node_by_path("/chosen@0");
  1632. if (of_chosen) {
  1633. const char *name;
  1634. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1635. if (name)
  1636. of_stdout = of_find_node_by_path(name);
  1637. }
  1638. of_aliases = of_find_node_by_path("/aliases");
  1639. if (!of_aliases)
  1640. return;
  1641. for_each_property_of_node(of_aliases, pp) {
  1642. const char *start = pp->name;
  1643. const char *end = start + strlen(start);
  1644. struct device_node *np;
  1645. struct alias_prop *ap;
  1646. int id, len;
  1647. /* Skip those we do not want to proceed */
  1648. if (!strcmp(pp->name, "name") ||
  1649. !strcmp(pp->name, "phandle") ||
  1650. !strcmp(pp->name, "linux,phandle"))
  1651. continue;
  1652. np = of_find_node_by_path(pp->value);
  1653. if (!np)
  1654. continue;
  1655. /* walk the alias backwards to extract the id and work out
  1656. * the 'stem' string */
  1657. while (isdigit(*(end-1)) && end > start)
  1658. end--;
  1659. len = end - start;
  1660. if (kstrtoint(end, 10, &id) < 0)
  1661. continue;
  1662. /* Allocate an alias_prop with enough space for the stem */
  1663. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1664. if (!ap)
  1665. continue;
  1666. memset(ap, 0, sizeof(*ap) + len + 1);
  1667. ap->alias = start;
  1668. of_alias_add(ap, np, id, start, len);
  1669. }
  1670. }
  1671. /**
  1672. * of_alias_get_id - Get alias id for the given device_node
  1673. * @np: Pointer to the given device_node
  1674. * @stem: Alias stem of the given device_node
  1675. *
  1676. * The function travels the lookup table to get alias id for the given
  1677. * device_node and alias stem. It returns the alias id if find it.
  1678. */
  1679. int of_alias_get_id(struct device_node *np, const char *stem)
  1680. {
  1681. struct alias_prop *app;
  1682. int id = -ENODEV;
  1683. mutex_lock(&of_aliases_mutex);
  1684. list_for_each_entry(app, &aliases_lookup, link) {
  1685. if (strcmp(app->stem, stem) != 0)
  1686. continue;
  1687. if (np == app->np) {
  1688. id = app->id;
  1689. break;
  1690. }
  1691. }
  1692. mutex_unlock(&of_aliases_mutex);
  1693. return id;
  1694. }
  1695. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1696. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1697. u32 *pu)
  1698. {
  1699. const void *curv = cur;
  1700. if (!prop)
  1701. return NULL;
  1702. if (!cur) {
  1703. curv = prop->value;
  1704. goto out_val;
  1705. }
  1706. curv += sizeof(*cur);
  1707. if (curv >= prop->value + prop->length)
  1708. return NULL;
  1709. out_val:
  1710. *pu = be32_to_cpup(curv);
  1711. return curv;
  1712. }
  1713. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1714. const char *of_prop_next_string(struct property *prop, const char *cur)
  1715. {
  1716. const void *curv = cur;
  1717. if (!prop)
  1718. return NULL;
  1719. if (!cur)
  1720. return prop->value;
  1721. curv += strlen(cur) + 1;
  1722. if (curv >= prop->value + prop->length)
  1723. return NULL;
  1724. return curv;
  1725. }
  1726. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1727. /**
  1728. * of_device_is_stdout_path - check if a device node matches the
  1729. * linux,stdout-path property
  1730. *
  1731. * Check if this device node matches the linux,stdout-path property
  1732. * in the chosen node. return true if yes, false otherwise.
  1733. */
  1734. int of_device_is_stdout_path(struct device_node *dn)
  1735. {
  1736. if (!of_stdout)
  1737. return false;
  1738. return of_stdout == dn;
  1739. }
  1740. EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
  1741. /**
  1742. * of_find_next_cache_node - Find a node's subsidiary cache
  1743. * @np: node of type "cpu" or "cache"
  1744. *
  1745. * Returns a node pointer with refcount incremented, use
  1746. * of_node_put() on it when done. Caller should hold a reference
  1747. * to np.
  1748. */
  1749. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1750. {
  1751. struct device_node *child;
  1752. const phandle *handle;
  1753. handle = of_get_property(np, "l2-cache", NULL);
  1754. if (!handle)
  1755. handle = of_get_property(np, "next-level-cache", NULL);
  1756. if (handle)
  1757. return of_find_node_by_phandle(be32_to_cpup(handle));
  1758. /* OF on pmac has nodes instead of properties named "l2-cache"
  1759. * beneath CPU nodes.
  1760. */
  1761. if (!strcmp(np->type, "cpu"))
  1762. for_each_child_of_node(np, child)
  1763. if (!strcmp(child->type, "cache"))
  1764. return child;
  1765. return NULL;
  1766. }