base.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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. status = __of_get_property(device, "status", &statlen);
  371. if (status == NULL)
  372. return 1;
  373. if (statlen > 0) {
  374. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  375. return 1;
  376. }
  377. return 0;
  378. }
  379. /**
  380. * of_device_is_available - check if a device is available for use
  381. *
  382. * @device: Node to check for availability
  383. *
  384. * Returns 1 if the status property is absent or set to "okay" or "ok",
  385. * 0 otherwise
  386. */
  387. int of_device_is_available(const struct device_node *device)
  388. {
  389. unsigned long flags;
  390. int res;
  391. raw_spin_lock_irqsave(&devtree_lock, flags);
  392. res = __of_device_is_available(device);
  393. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  394. return res;
  395. }
  396. EXPORT_SYMBOL(of_device_is_available);
  397. /**
  398. * of_get_parent - Get a node's parent if any
  399. * @node: Node to get parent
  400. *
  401. * Returns a node pointer with refcount incremented, use
  402. * of_node_put() on it when done.
  403. */
  404. struct device_node *of_get_parent(const struct device_node *node)
  405. {
  406. struct device_node *np;
  407. unsigned long flags;
  408. if (!node)
  409. return NULL;
  410. raw_spin_lock_irqsave(&devtree_lock, flags);
  411. np = of_node_get(node->parent);
  412. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  413. return np;
  414. }
  415. EXPORT_SYMBOL(of_get_parent);
  416. /**
  417. * of_get_next_parent - Iterate to a node's parent
  418. * @node: Node to get parent of
  419. *
  420. * This is like of_get_parent() except that it drops the
  421. * refcount on the passed node, making it suitable for iterating
  422. * through a node's parents.
  423. *
  424. * Returns a node pointer with refcount incremented, use
  425. * of_node_put() on it when done.
  426. */
  427. struct device_node *of_get_next_parent(struct device_node *node)
  428. {
  429. struct device_node *parent;
  430. unsigned long flags;
  431. if (!node)
  432. return NULL;
  433. raw_spin_lock_irqsave(&devtree_lock, flags);
  434. parent = of_node_get(node->parent);
  435. of_node_put(node);
  436. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  437. return parent;
  438. }
  439. EXPORT_SYMBOL(of_get_next_parent);
  440. /**
  441. * of_get_next_child - Iterate a node childs
  442. * @node: parent node
  443. * @prev: previous child of the parent node, or NULL to get first
  444. *
  445. * Returns a node pointer with refcount incremented, use
  446. * of_node_put() on it when done.
  447. */
  448. struct device_node *of_get_next_child(const struct device_node *node,
  449. struct device_node *prev)
  450. {
  451. struct device_node *next;
  452. unsigned long flags;
  453. raw_spin_lock_irqsave(&devtree_lock, flags);
  454. next = prev ? prev->sibling : node->child;
  455. for (; next; next = next->sibling)
  456. if (of_node_get(next))
  457. break;
  458. of_node_put(prev);
  459. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  460. return next;
  461. }
  462. EXPORT_SYMBOL(of_get_next_child);
  463. /**
  464. * of_get_next_available_child - Find the next available child node
  465. * @node: parent node
  466. * @prev: previous child of the parent node, or NULL to get first
  467. *
  468. * This function is like of_get_next_child(), except that it
  469. * automatically skips any disabled nodes (i.e. status = "disabled").
  470. */
  471. struct device_node *of_get_next_available_child(const struct device_node *node,
  472. struct device_node *prev)
  473. {
  474. struct device_node *next;
  475. unsigned long flags;
  476. raw_spin_lock_irqsave(&devtree_lock, flags);
  477. next = prev ? prev->sibling : node->child;
  478. for (; next; next = next->sibling) {
  479. if (!__of_device_is_available(next))
  480. continue;
  481. if (of_node_get(next))
  482. break;
  483. }
  484. of_node_put(prev);
  485. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  486. return next;
  487. }
  488. EXPORT_SYMBOL(of_get_next_available_child);
  489. /**
  490. * of_get_child_by_name - Find the child node by name for a given parent
  491. * @node: parent node
  492. * @name: child name to look for.
  493. *
  494. * This function looks for child node for given matching name
  495. *
  496. * Returns a node pointer if found, with refcount incremented, use
  497. * of_node_put() on it when done.
  498. * Returns NULL if node is not found.
  499. */
  500. struct device_node *of_get_child_by_name(const struct device_node *node,
  501. const char *name)
  502. {
  503. struct device_node *child;
  504. for_each_child_of_node(node, child)
  505. if (child->name && (of_node_cmp(child->name, name) == 0))
  506. break;
  507. return child;
  508. }
  509. EXPORT_SYMBOL(of_get_child_by_name);
  510. /**
  511. * of_find_node_by_path - Find a node matching a full OF path
  512. * @path: The full path to match
  513. *
  514. * Returns a node pointer with refcount incremented, use
  515. * of_node_put() on it when done.
  516. */
  517. struct device_node *of_find_node_by_path(const char *path)
  518. {
  519. struct device_node *np = of_allnodes;
  520. unsigned long flags;
  521. raw_spin_lock_irqsave(&devtree_lock, flags);
  522. for (; np; np = np->allnext) {
  523. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  524. && of_node_get(np))
  525. break;
  526. }
  527. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  528. return np;
  529. }
  530. EXPORT_SYMBOL(of_find_node_by_path);
  531. /**
  532. * of_find_node_by_name - Find a node by its "name" property
  533. * @from: The node to start searching from or NULL, the node
  534. * you pass will not be searched, only the next one
  535. * will; typically, you pass what the previous call
  536. * returned. of_node_put() will be called on it
  537. * @name: The name string to match against
  538. *
  539. * Returns a node pointer with refcount incremented, use
  540. * of_node_put() on it when done.
  541. */
  542. struct device_node *of_find_node_by_name(struct device_node *from,
  543. const char *name)
  544. {
  545. struct device_node *np;
  546. unsigned long flags;
  547. raw_spin_lock_irqsave(&devtree_lock, flags);
  548. np = from ? from->allnext : of_allnodes;
  549. for (; np; np = np->allnext)
  550. if (np->name && (of_node_cmp(np->name, name) == 0)
  551. && of_node_get(np))
  552. break;
  553. of_node_put(from);
  554. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  555. return np;
  556. }
  557. EXPORT_SYMBOL(of_find_node_by_name);
  558. /**
  559. * of_find_node_by_type - Find a node by its "device_type" property
  560. * @from: The node to start searching from, or NULL to start searching
  561. * the entire device tree. The node you pass will not be
  562. * searched, only the next one will; typically, you pass
  563. * what the previous call returned. of_node_put() will be
  564. * called on from for you.
  565. * @type: The type string to match against
  566. *
  567. * Returns a node pointer with refcount incremented, use
  568. * of_node_put() on it when done.
  569. */
  570. struct device_node *of_find_node_by_type(struct device_node *from,
  571. const char *type)
  572. {
  573. struct device_node *np;
  574. unsigned long flags;
  575. raw_spin_lock_irqsave(&devtree_lock, flags);
  576. np = from ? from->allnext : of_allnodes;
  577. for (; np; np = np->allnext)
  578. if (np->type && (of_node_cmp(np->type, type) == 0)
  579. && of_node_get(np))
  580. break;
  581. of_node_put(from);
  582. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  583. return np;
  584. }
  585. EXPORT_SYMBOL(of_find_node_by_type);
  586. /**
  587. * of_find_compatible_node - Find a node based on type and one of the
  588. * tokens in its "compatible" property
  589. * @from: The node to start searching from or NULL, the node
  590. * you pass will not be searched, only the next one
  591. * will; typically, you pass what the previous call
  592. * returned. of_node_put() will be called on it
  593. * @type: The type string to match "device_type" or NULL to ignore
  594. * @compatible: The string to match to one of the tokens in the device
  595. * "compatible" list.
  596. *
  597. * Returns a node pointer with refcount incremented, use
  598. * of_node_put() on it when done.
  599. */
  600. struct device_node *of_find_compatible_node(struct device_node *from,
  601. const char *type, const char *compatible)
  602. {
  603. struct device_node *np;
  604. unsigned long flags;
  605. raw_spin_lock_irqsave(&devtree_lock, flags);
  606. np = from ? from->allnext : of_allnodes;
  607. for (; np; np = np->allnext) {
  608. if (type
  609. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  610. continue;
  611. if (__of_device_is_compatible(np, compatible) &&
  612. of_node_get(np))
  613. break;
  614. }
  615. of_node_put(from);
  616. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  617. return np;
  618. }
  619. EXPORT_SYMBOL(of_find_compatible_node);
  620. /**
  621. * of_find_node_with_property - Find a node which has a property with
  622. * the given name.
  623. * @from: The node to start searching from or NULL, the node
  624. * you pass will not be searched, only the next one
  625. * will; typically, you pass what the previous call
  626. * returned. of_node_put() will be called on it
  627. * @prop_name: The name of the property to look for.
  628. *
  629. * Returns a node pointer with refcount incremented, use
  630. * of_node_put() on it when done.
  631. */
  632. struct device_node *of_find_node_with_property(struct device_node *from,
  633. const char *prop_name)
  634. {
  635. struct device_node *np;
  636. struct property *pp;
  637. unsigned long flags;
  638. raw_spin_lock_irqsave(&devtree_lock, flags);
  639. np = from ? from->allnext : of_allnodes;
  640. for (; np; np = np->allnext) {
  641. for (pp = np->properties; pp; pp = pp->next) {
  642. if (of_prop_cmp(pp->name, prop_name) == 0) {
  643. of_node_get(np);
  644. goto out;
  645. }
  646. }
  647. }
  648. out:
  649. of_node_put(from);
  650. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  651. return np;
  652. }
  653. EXPORT_SYMBOL(of_find_node_with_property);
  654. static
  655. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  656. const struct device_node *node)
  657. {
  658. if (!matches)
  659. return NULL;
  660. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  661. int match = 1;
  662. if (matches->name[0])
  663. match &= node->name
  664. && !strcmp(matches->name, node->name);
  665. if (matches->type[0])
  666. match &= node->type
  667. && !strcmp(matches->type, node->type);
  668. if (matches->compatible[0])
  669. match &= __of_device_is_compatible(node,
  670. matches->compatible);
  671. if (match)
  672. return matches;
  673. matches++;
  674. }
  675. return NULL;
  676. }
  677. /**
  678. * of_match_node - Tell if an device_node has a matching of_match structure
  679. * @matches: array of of device match structures to search in
  680. * @node: the of device structure to match against
  681. *
  682. * Low level utility function used by device matching.
  683. */
  684. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  685. const struct device_node *node)
  686. {
  687. const struct of_device_id *match;
  688. unsigned long flags;
  689. raw_spin_lock_irqsave(&devtree_lock, flags);
  690. match = __of_match_node(matches, node);
  691. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  692. return match;
  693. }
  694. EXPORT_SYMBOL(of_match_node);
  695. /**
  696. * of_find_matching_node_and_match - Find a node based on an of_device_id
  697. * match table.
  698. * @from: The node to start searching from or NULL, the node
  699. * you pass will not be searched, only the next one
  700. * will; typically, you pass what the previous call
  701. * returned. of_node_put() will be called on it
  702. * @matches: array of of device match structures to search in
  703. * @match Updated to point at the matches entry which matched
  704. *
  705. * Returns a node pointer with refcount incremented, use
  706. * of_node_put() on it when done.
  707. */
  708. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  709. const struct of_device_id *matches,
  710. const struct of_device_id **match)
  711. {
  712. struct device_node *np;
  713. const struct of_device_id *m;
  714. unsigned long flags;
  715. if (match)
  716. *match = NULL;
  717. raw_spin_lock_irqsave(&devtree_lock, flags);
  718. np = from ? from->allnext : of_allnodes;
  719. for (; np; np = np->allnext) {
  720. m = __of_match_node(matches, np);
  721. if (m && of_node_get(np)) {
  722. if (match)
  723. *match = m;
  724. break;
  725. }
  726. }
  727. of_node_put(from);
  728. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  729. return np;
  730. }
  731. EXPORT_SYMBOL(of_find_matching_node_and_match);
  732. /**
  733. * of_modalias_node - Lookup appropriate modalias for a device node
  734. * @node: pointer to a device tree node
  735. * @modalias: Pointer to buffer that modalias value will be copied into
  736. * @len: Length of modalias value
  737. *
  738. * Based on the value of the compatible property, this routine will attempt
  739. * to choose an appropriate modalias value for a particular device tree node.
  740. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  741. * from the first entry in the compatible list property.
  742. *
  743. * This routine returns 0 on success, <0 on failure.
  744. */
  745. int of_modalias_node(struct device_node *node, char *modalias, int len)
  746. {
  747. const char *compatible, *p;
  748. int cplen;
  749. compatible = of_get_property(node, "compatible", &cplen);
  750. if (!compatible || strlen(compatible) > cplen)
  751. return -ENODEV;
  752. p = strchr(compatible, ',');
  753. strlcpy(modalias, p ? p + 1 : compatible, len);
  754. return 0;
  755. }
  756. EXPORT_SYMBOL_GPL(of_modalias_node);
  757. /**
  758. * of_find_node_by_phandle - Find a node given a phandle
  759. * @handle: phandle of the node to find
  760. *
  761. * Returns a node pointer with refcount incremented, use
  762. * of_node_put() on it when done.
  763. */
  764. struct device_node *of_find_node_by_phandle(phandle handle)
  765. {
  766. struct device_node *np;
  767. unsigned long flags;
  768. raw_spin_lock_irqsave(&devtree_lock, flags);
  769. for (np = of_allnodes; np; np = np->allnext)
  770. if (np->phandle == handle)
  771. break;
  772. of_node_get(np);
  773. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  774. return np;
  775. }
  776. EXPORT_SYMBOL(of_find_node_by_phandle);
  777. /**
  778. * of_find_property_value_of_size
  779. *
  780. * @np: device node from which the property value is to be read.
  781. * @propname: name of the property to be searched.
  782. * @len: requested length of property value
  783. *
  784. * Search for a property in a device node and valid the requested size.
  785. * Returns the property value on success, -EINVAL if the property does not
  786. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  787. * property data isn't large enough.
  788. *
  789. */
  790. static void *of_find_property_value_of_size(const struct device_node *np,
  791. const char *propname, u32 len)
  792. {
  793. struct property *prop = of_find_property(np, propname, NULL);
  794. if (!prop)
  795. return ERR_PTR(-EINVAL);
  796. if (!prop->value)
  797. return ERR_PTR(-ENODATA);
  798. if (len > prop->length)
  799. return ERR_PTR(-EOVERFLOW);
  800. return prop->value;
  801. }
  802. /**
  803. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  804. *
  805. * @np: device node from which the property value is to be read.
  806. * @propname: name of the property to be searched.
  807. * @index: index of the u32 in the list of values
  808. * @out_value: pointer to return value, modified only if no error.
  809. *
  810. * Search for a property in a device node and read nth 32-bit value from
  811. * it. Returns 0 on success, -EINVAL if the property does not exist,
  812. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  813. * property data isn't large enough.
  814. *
  815. * The out_value is modified only if a valid u32 value can be decoded.
  816. */
  817. int of_property_read_u32_index(const struct device_node *np,
  818. const char *propname,
  819. u32 index, u32 *out_value)
  820. {
  821. const u32 *val = of_find_property_value_of_size(np, propname,
  822. ((index + 1) * sizeof(*out_value)));
  823. if (IS_ERR(val))
  824. return PTR_ERR(val);
  825. *out_value = be32_to_cpup(((__be32 *)val) + index);
  826. return 0;
  827. }
  828. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  829. /**
  830. * of_property_read_u8_array - Find and read an array of u8 from a property.
  831. *
  832. * @np: device node from which the property value is to be read.
  833. * @propname: name of the property to be searched.
  834. * @out_values: pointer to return value, modified only if return value is 0.
  835. * @sz: number of array elements to read
  836. *
  837. * Search for a property in a device node and read 8-bit value(s) from
  838. * it. Returns 0 on success, -EINVAL if the property does not exist,
  839. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  840. * property data isn't large enough.
  841. *
  842. * dts entry of array should be like:
  843. * property = /bits/ 8 <0x50 0x60 0x70>;
  844. *
  845. * The out_values is modified only if a valid u8 value can be decoded.
  846. */
  847. int of_property_read_u8_array(const struct device_node *np,
  848. const char *propname, u8 *out_values, size_t sz)
  849. {
  850. const u8 *val = of_find_property_value_of_size(np, propname,
  851. (sz * sizeof(*out_values)));
  852. if (IS_ERR(val))
  853. return PTR_ERR(val);
  854. while (sz--)
  855. *out_values++ = *val++;
  856. return 0;
  857. }
  858. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  859. /**
  860. * of_property_read_u16_array - Find and read an array of u16 from a property.
  861. *
  862. * @np: device node from which the property value is to be read.
  863. * @propname: name of the property to be searched.
  864. * @out_values: pointer to return value, modified only if return value is 0.
  865. * @sz: number of array elements to read
  866. *
  867. * Search for a property in a device node and read 16-bit value(s) from
  868. * it. Returns 0 on success, -EINVAL if the property does not exist,
  869. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  870. * property data isn't large enough.
  871. *
  872. * dts entry of array should be like:
  873. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  874. *
  875. * The out_values is modified only if a valid u16 value can be decoded.
  876. */
  877. int of_property_read_u16_array(const struct device_node *np,
  878. const char *propname, u16 *out_values, size_t sz)
  879. {
  880. const __be16 *val = of_find_property_value_of_size(np, propname,
  881. (sz * sizeof(*out_values)));
  882. if (IS_ERR(val))
  883. return PTR_ERR(val);
  884. while (sz--)
  885. *out_values++ = be16_to_cpup(val++);
  886. return 0;
  887. }
  888. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  889. /**
  890. * of_property_read_u32_array - Find and read an array of 32 bit integers
  891. * from a property.
  892. *
  893. * @np: device node from which the property value is to be read.
  894. * @propname: name of the property to be searched.
  895. * @out_values: pointer to return value, modified only if return value is 0.
  896. * @sz: number of array elements to read
  897. *
  898. * Search for a property in a device node and read 32-bit value(s) from
  899. * it. Returns 0 on success, -EINVAL if the property does not exist,
  900. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  901. * property data isn't large enough.
  902. *
  903. * The out_values is modified only if a valid u32 value can be decoded.
  904. */
  905. int of_property_read_u32_array(const struct device_node *np,
  906. const char *propname, u32 *out_values,
  907. size_t sz)
  908. {
  909. const __be32 *val = of_find_property_value_of_size(np, propname,
  910. (sz * sizeof(*out_values)));
  911. if (IS_ERR(val))
  912. return PTR_ERR(val);
  913. while (sz--)
  914. *out_values++ = be32_to_cpup(val++);
  915. return 0;
  916. }
  917. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  918. /**
  919. * of_property_read_u64 - Find and read a 64 bit integer from a property
  920. * @np: device node from which the property value is to be read.
  921. * @propname: name of the property to be searched.
  922. * @out_value: pointer to return value, modified only if return value is 0.
  923. *
  924. * Search for a property in a device node and read a 64-bit value from
  925. * it. Returns 0 on success, -EINVAL if the property does not exist,
  926. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  927. * property data isn't large enough.
  928. *
  929. * The out_value is modified only if a valid u64 value can be decoded.
  930. */
  931. int of_property_read_u64(const struct device_node *np, const char *propname,
  932. u64 *out_value)
  933. {
  934. const __be32 *val = of_find_property_value_of_size(np, propname,
  935. sizeof(*out_value));
  936. if (IS_ERR(val))
  937. return PTR_ERR(val);
  938. *out_value = of_read_number(val, 2);
  939. return 0;
  940. }
  941. EXPORT_SYMBOL_GPL(of_property_read_u64);
  942. /**
  943. * of_property_read_string - Find and read a string from a property
  944. * @np: device node from which the property value is to be read.
  945. * @propname: name of the property to be searched.
  946. * @out_string: pointer to null terminated return string, modified only if
  947. * return value is 0.
  948. *
  949. * Search for a property in a device tree node and retrieve a null
  950. * terminated string value (pointer to data, not a copy). Returns 0 on
  951. * success, -EINVAL if the property does not exist, -ENODATA if property
  952. * does not have a value, and -EILSEQ if the string is not null-terminated
  953. * within the length of the property data.
  954. *
  955. * The out_string pointer is modified only if a valid string can be decoded.
  956. */
  957. int of_property_read_string(struct device_node *np, const char *propname,
  958. const char **out_string)
  959. {
  960. struct property *prop = of_find_property(np, propname, NULL);
  961. if (!prop)
  962. return -EINVAL;
  963. if (!prop->value)
  964. return -ENODATA;
  965. if (strnlen(prop->value, prop->length) >= prop->length)
  966. return -EILSEQ;
  967. *out_string = prop->value;
  968. return 0;
  969. }
  970. EXPORT_SYMBOL_GPL(of_property_read_string);
  971. /**
  972. * of_property_read_string_index - Find and read a string from a multiple
  973. * strings property.
  974. * @np: device node from which the property value is to be read.
  975. * @propname: name of the property to be searched.
  976. * @index: index of the string in the list of strings
  977. * @out_string: pointer to null terminated return string, modified only if
  978. * return value is 0.
  979. *
  980. * Search for a property in a device tree node and retrieve a null
  981. * terminated string value (pointer to data, not a copy) in the list of strings
  982. * contained in that property.
  983. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  984. * property does not have a value, and -EILSEQ if the string is not
  985. * null-terminated within the length of the property data.
  986. *
  987. * The out_string pointer is modified only if a valid string can be decoded.
  988. */
  989. int of_property_read_string_index(struct device_node *np, const char *propname,
  990. int index, const char **output)
  991. {
  992. struct property *prop = of_find_property(np, propname, NULL);
  993. int i = 0;
  994. size_t l = 0, total = 0;
  995. const char *p;
  996. if (!prop)
  997. return -EINVAL;
  998. if (!prop->value)
  999. return -ENODATA;
  1000. if (strnlen(prop->value, prop->length) >= prop->length)
  1001. return -EILSEQ;
  1002. p = prop->value;
  1003. for (i = 0; total < prop->length; total += l, p += l) {
  1004. l = strlen(p) + 1;
  1005. if (i++ == index) {
  1006. *output = p;
  1007. return 0;
  1008. }
  1009. }
  1010. return -ENODATA;
  1011. }
  1012. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  1013. /**
  1014. * of_property_match_string() - Find string in a list and return index
  1015. * @np: pointer to node containing string list property
  1016. * @propname: string list property name
  1017. * @string: pointer to string to search for in string list
  1018. *
  1019. * This function searches a string list property and returns the index
  1020. * of a specific string value.
  1021. */
  1022. int of_property_match_string(struct device_node *np, const char *propname,
  1023. const char *string)
  1024. {
  1025. struct property *prop = of_find_property(np, propname, NULL);
  1026. size_t l;
  1027. int i;
  1028. const char *p, *end;
  1029. if (!prop)
  1030. return -EINVAL;
  1031. if (!prop->value)
  1032. return -ENODATA;
  1033. p = prop->value;
  1034. end = p + prop->length;
  1035. for (i = 0; p < end; i++, p += l) {
  1036. l = strlen(p) + 1;
  1037. if (p + l > end)
  1038. return -EILSEQ;
  1039. pr_debug("comparing %s with %s\n", string, p);
  1040. if (strcmp(string, p) == 0)
  1041. return i; /* Found it; return index */
  1042. }
  1043. return -ENODATA;
  1044. }
  1045. EXPORT_SYMBOL_GPL(of_property_match_string);
  1046. /**
  1047. * of_property_count_strings - Find and return the number of strings from a
  1048. * multiple strings property.
  1049. * @np: device node from which the property value is to be read.
  1050. * @propname: name of the property to be searched.
  1051. *
  1052. * Search for a property in a device tree node and retrieve the number of null
  1053. * terminated string contain in it. Returns the number of strings on
  1054. * success, -EINVAL if the property does not exist, -ENODATA if property
  1055. * does not have a value, and -EILSEQ if the string is not null-terminated
  1056. * within the length of the property data.
  1057. */
  1058. int of_property_count_strings(struct device_node *np, const char *propname)
  1059. {
  1060. struct property *prop = of_find_property(np, propname, NULL);
  1061. int i = 0;
  1062. size_t l = 0, total = 0;
  1063. const char *p;
  1064. if (!prop)
  1065. return -EINVAL;
  1066. if (!prop->value)
  1067. return -ENODATA;
  1068. if (strnlen(prop->value, prop->length) >= prop->length)
  1069. return -EILSEQ;
  1070. p = prop->value;
  1071. for (i = 0; total < prop->length; total += l, p += l, i++)
  1072. l = strlen(p) + 1;
  1073. return i;
  1074. }
  1075. EXPORT_SYMBOL_GPL(of_property_count_strings);
  1076. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1077. {
  1078. int i;
  1079. printk("%s %s", msg, of_node_full_name(args->np));
  1080. for (i = 0; i < args->args_count; i++)
  1081. printk(i ? ",%08x" : ":%08x", args->args[i]);
  1082. printk("\n");
  1083. }
  1084. static int __of_parse_phandle_with_args(const struct device_node *np,
  1085. const char *list_name,
  1086. const char *cells_name,
  1087. int cell_count, int index,
  1088. struct of_phandle_args *out_args)
  1089. {
  1090. const __be32 *list, *list_end;
  1091. int rc = 0, size, cur_index = 0;
  1092. uint32_t count = 0;
  1093. struct device_node *node = NULL;
  1094. phandle phandle;
  1095. /* Retrieve the phandle list property */
  1096. list = of_get_property(np, list_name, &size);
  1097. if (!list)
  1098. return -ENOENT;
  1099. list_end = list + size / sizeof(*list);
  1100. /* Loop over the phandles until all the requested entry is found */
  1101. while (list < list_end) {
  1102. rc = -EINVAL;
  1103. count = 0;
  1104. /*
  1105. * If phandle is 0, then it is an empty entry with no
  1106. * arguments. Skip forward to the next entry.
  1107. */
  1108. phandle = be32_to_cpup(list++);
  1109. if (phandle) {
  1110. /*
  1111. * Find the provider node and parse the #*-cells
  1112. * property to determine the argument length.
  1113. *
  1114. * This is not needed if the cell count is hard-coded
  1115. * (i.e. cells_name not set, but cell_count is set),
  1116. * except when we're going to return the found node
  1117. * below.
  1118. */
  1119. if (cells_name || cur_index == index) {
  1120. node = of_find_node_by_phandle(phandle);
  1121. if (!node) {
  1122. pr_err("%s: could not find phandle\n",
  1123. np->full_name);
  1124. goto err;
  1125. }
  1126. }
  1127. if (cells_name) {
  1128. if (of_property_read_u32(node, cells_name,
  1129. &count)) {
  1130. pr_err("%s: could not get %s for %s\n",
  1131. np->full_name, cells_name,
  1132. node->full_name);
  1133. goto err;
  1134. }
  1135. } else {
  1136. count = cell_count;
  1137. }
  1138. /*
  1139. * Make sure that the arguments actually fit in the
  1140. * remaining property data length
  1141. */
  1142. if (list + count > list_end) {
  1143. pr_err("%s: arguments longer than property\n",
  1144. np->full_name);
  1145. goto err;
  1146. }
  1147. }
  1148. /*
  1149. * All of the error cases above bail out of the loop, so at
  1150. * this point, the parsing is successful. If the requested
  1151. * index matches, then fill the out_args structure and return,
  1152. * or return -ENOENT for an empty entry.
  1153. */
  1154. rc = -ENOENT;
  1155. if (cur_index == index) {
  1156. if (!phandle)
  1157. goto err;
  1158. if (out_args) {
  1159. int i;
  1160. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1161. count = MAX_PHANDLE_ARGS;
  1162. out_args->np = node;
  1163. out_args->args_count = count;
  1164. for (i = 0; i < count; i++)
  1165. out_args->args[i] = be32_to_cpup(list++);
  1166. } else {
  1167. of_node_put(node);
  1168. }
  1169. /* Found it! return success */
  1170. return 0;
  1171. }
  1172. of_node_put(node);
  1173. node = NULL;
  1174. list += count;
  1175. cur_index++;
  1176. }
  1177. /*
  1178. * Unlock node before returning result; will be one of:
  1179. * -ENOENT : index is for empty phandle
  1180. * -EINVAL : parsing error on data
  1181. * [1..n] : Number of phandle (count mode; when index = -1)
  1182. */
  1183. rc = index < 0 ? cur_index : -ENOENT;
  1184. err:
  1185. if (node)
  1186. of_node_put(node);
  1187. return rc;
  1188. }
  1189. /**
  1190. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1191. * @np: Pointer to device node holding phandle property
  1192. * @phandle_name: Name of property holding a phandle value
  1193. * @index: For properties holding a table of phandles, this is the index into
  1194. * the table
  1195. *
  1196. * Returns the device_node pointer with refcount incremented. Use
  1197. * of_node_put() on it when done.
  1198. */
  1199. struct device_node *of_parse_phandle(const struct device_node *np,
  1200. const char *phandle_name, int index)
  1201. {
  1202. struct of_phandle_args args;
  1203. if (index < 0)
  1204. return NULL;
  1205. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1206. index, &args))
  1207. return NULL;
  1208. return args.np;
  1209. }
  1210. EXPORT_SYMBOL(of_parse_phandle);
  1211. /**
  1212. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1213. * @np: pointer to a device tree node containing a list
  1214. * @list_name: property name that contains a list
  1215. * @cells_name: property name that specifies phandles' arguments count
  1216. * @index: index of a phandle to parse out
  1217. * @out_args: optional pointer to output arguments structure (will be filled)
  1218. *
  1219. * This function is useful to parse lists of phandles and their arguments.
  1220. * Returns 0 on success and fills out_args, on error returns appropriate
  1221. * errno value.
  1222. *
  1223. * Caller is responsible to call of_node_put() on the returned out_args->node
  1224. * pointer.
  1225. *
  1226. * Example:
  1227. *
  1228. * phandle1: node1 {
  1229. * #list-cells = <2>;
  1230. * }
  1231. *
  1232. * phandle2: node2 {
  1233. * #list-cells = <1>;
  1234. * }
  1235. *
  1236. * node3 {
  1237. * list = <&phandle1 1 2 &phandle2 3>;
  1238. * }
  1239. *
  1240. * To get a device_node of the `node2' node you may call this:
  1241. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1242. */
  1243. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1244. const char *cells_name, int index,
  1245. struct of_phandle_args *out_args)
  1246. {
  1247. if (index < 0)
  1248. return -EINVAL;
  1249. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1250. index, out_args);
  1251. }
  1252. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1253. /**
  1254. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1255. * @np: pointer to a device tree node containing a list
  1256. * @list_name: property name that contains a list
  1257. * @cell_count: number of argument cells following the phandle
  1258. * @index: index of a phandle to parse out
  1259. * @out_args: optional pointer to output arguments structure (will be filled)
  1260. *
  1261. * This function is useful to parse lists of phandles and their arguments.
  1262. * Returns 0 on success and fills out_args, on error returns appropriate
  1263. * errno value.
  1264. *
  1265. * Caller is responsible to call of_node_put() on the returned out_args->node
  1266. * pointer.
  1267. *
  1268. * Example:
  1269. *
  1270. * phandle1: node1 {
  1271. * }
  1272. *
  1273. * phandle2: node2 {
  1274. * }
  1275. *
  1276. * node3 {
  1277. * list = <&phandle1 0 2 &phandle2 2 3>;
  1278. * }
  1279. *
  1280. * To get a device_node of the `node2' node you may call this:
  1281. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1282. */
  1283. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1284. const char *list_name, int cell_count,
  1285. int index, struct of_phandle_args *out_args)
  1286. {
  1287. if (index < 0)
  1288. return -EINVAL;
  1289. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1290. index, out_args);
  1291. }
  1292. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1293. /**
  1294. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1295. * @np: pointer to a device tree node containing a list
  1296. * @list_name: property name that contains a list
  1297. * @cells_name: property name that specifies phandles' arguments count
  1298. *
  1299. * Returns the number of phandle + argument tuples within a property. It
  1300. * is a typical pattern to encode a list of phandle and variable
  1301. * arguments into a single property. The number of arguments is encoded
  1302. * by a property in the phandle-target node. For example, a gpios
  1303. * property would contain a list of GPIO specifies consisting of a
  1304. * phandle and 1 or more arguments. The number of arguments are
  1305. * determined by the #gpio-cells property in the node pointed to by the
  1306. * phandle.
  1307. */
  1308. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1309. const char *cells_name)
  1310. {
  1311. return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
  1312. NULL);
  1313. }
  1314. EXPORT_SYMBOL(of_count_phandle_with_args);
  1315. #if defined(CONFIG_OF_DYNAMIC)
  1316. static int of_property_notify(int action, struct device_node *np,
  1317. struct property *prop)
  1318. {
  1319. struct of_prop_reconfig pr;
  1320. pr.dn = np;
  1321. pr.prop = prop;
  1322. return of_reconfig_notify(action, &pr);
  1323. }
  1324. #else
  1325. static int of_property_notify(int action, struct device_node *np,
  1326. struct property *prop)
  1327. {
  1328. return 0;
  1329. }
  1330. #endif
  1331. /**
  1332. * of_add_property - Add a property to a node
  1333. */
  1334. int of_add_property(struct device_node *np, struct property *prop)
  1335. {
  1336. struct property **next;
  1337. unsigned long flags;
  1338. int rc;
  1339. rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
  1340. if (rc)
  1341. return rc;
  1342. prop->next = NULL;
  1343. raw_spin_lock_irqsave(&devtree_lock, flags);
  1344. next = &np->properties;
  1345. while (*next) {
  1346. if (strcmp(prop->name, (*next)->name) == 0) {
  1347. /* duplicate ! don't insert it */
  1348. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1349. return -1;
  1350. }
  1351. next = &(*next)->next;
  1352. }
  1353. *next = prop;
  1354. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1355. #ifdef CONFIG_PROC_DEVICETREE
  1356. /* try to add to proc as well if it was initialized */
  1357. if (np->pde)
  1358. proc_device_tree_add_prop(np->pde, prop);
  1359. #endif /* CONFIG_PROC_DEVICETREE */
  1360. return 0;
  1361. }
  1362. /**
  1363. * of_remove_property - Remove a property from a node.
  1364. *
  1365. * Note that we don't actually remove it, since we have given out
  1366. * who-knows-how-many pointers to the data using get-property.
  1367. * Instead we just move the property to the "dead properties"
  1368. * list, so it won't be found any more.
  1369. */
  1370. int of_remove_property(struct device_node *np, struct property *prop)
  1371. {
  1372. struct property **next;
  1373. unsigned long flags;
  1374. int found = 0;
  1375. int rc;
  1376. rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1377. if (rc)
  1378. return rc;
  1379. raw_spin_lock_irqsave(&devtree_lock, flags);
  1380. next = &np->properties;
  1381. while (*next) {
  1382. if (*next == prop) {
  1383. /* found the node */
  1384. *next = prop->next;
  1385. prop->next = np->deadprops;
  1386. np->deadprops = prop;
  1387. found = 1;
  1388. break;
  1389. }
  1390. next = &(*next)->next;
  1391. }
  1392. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1393. if (!found)
  1394. return -ENODEV;
  1395. #ifdef CONFIG_PROC_DEVICETREE
  1396. /* try to remove the proc node as well */
  1397. if (np->pde)
  1398. proc_device_tree_remove_prop(np->pde, prop);
  1399. #endif /* CONFIG_PROC_DEVICETREE */
  1400. return 0;
  1401. }
  1402. /*
  1403. * of_update_property - Update a property in a node, if the property does
  1404. * not exist, add it.
  1405. *
  1406. * Note that we don't actually remove it, since we have given out
  1407. * who-knows-how-many pointers to the data using get-property.
  1408. * Instead we just move the property to the "dead properties" list,
  1409. * and add the new property to the property list
  1410. */
  1411. int of_update_property(struct device_node *np, struct property *newprop)
  1412. {
  1413. struct property **next, *oldprop;
  1414. unsigned long flags;
  1415. int rc, found = 0;
  1416. rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
  1417. if (rc)
  1418. return rc;
  1419. if (!newprop->name)
  1420. return -EINVAL;
  1421. oldprop = of_find_property(np, newprop->name, NULL);
  1422. if (!oldprop)
  1423. return of_add_property(np, newprop);
  1424. raw_spin_lock_irqsave(&devtree_lock, flags);
  1425. next = &np->properties;
  1426. while (*next) {
  1427. if (*next == oldprop) {
  1428. /* found the node */
  1429. newprop->next = oldprop->next;
  1430. *next = newprop;
  1431. oldprop->next = np->deadprops;
  1432. np->deadprops = oldprop;
  1433. found = 1;
  1434. break;
  1435. }
  1436. next = &(*next)->next;
  1437. }
  1438. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1439. if (!found)
  1440. return -ENODEV;
  1441. #ifdef CONFIG_PROC_DEVICETREE
  1442. /* try to add to proc as well if it was initialized */
  1443. if (np->pde)
  1444. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1445. #endif /* CONFIG_PROC_DEVICETREE */
  1446. return 0;
  1447. }
  1448. #if defined(CONFIG_OF_DYNAMIC)
  1449. /*
  1450. * Support for dynamic device trees.
  1451. *
  1452. * On some platforms, the device tree can be manipulated at runtime.
  1453. * The routines in this section support adding, removing and changing
  1454. * device tree nodes.
  1455. */
  1456. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  1457. int of_reconfig_notifier_register(struct notifier_block *nb)
  1458. {
  1459. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  1460. }
  1461. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  1462. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1463. {
  1464. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  1465. }
  1466. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  1467. int of_reconfig_notify(unsigned long action, void *p)
  1468. {
  1469. int rc;
  1470. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  1471. return notifier_to_errno(rc);
  1472. }
  1473. #ifdef CONFIG_PROC_DEVICETREE
  1474. static void of_add_proc_dt_entry(struct device_node *dn)
  1475. {
  1476. struct proc_dir_entry *ent;
  1477. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  1478. if (ent)
  1479. proc_device_tree_add_node(dn, ent);
  1480. }
  1481. #else
  1482. static void of_add_proc_dt_entry(struct device_node *dn)
  1483. {
  1484. return;
  1485. }
  1486. #endif
  1487. /**
  1488. * of_attach_node - Plug a device node into the tree and global list.
  1489. */
  1490. int of_attach_node(struct device_node *np)
  1491. {
  1492. unsigned long flags;
  1493. int rc;
  1494. rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  1495. if (rc)
  1496. return rc;
  1497. raw_spin_lock_irqsave(&devtree_lock, flags);
  1498. np->sibling = np->parent->child;
  1499. np->allnext = of_allnodes;
  1500. np->parent->child = np;
  1501. of_allnodes = np;
  1502. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1503. of_add_proc_dt_entry(np);
  1504. return 0;
  1505. }
  1506. #ifdef CONFIG_PROC_DEVICETREE
  1507. static void of_remove_proc_dt_entry(struct device_node *dn)
  1508. {
  1509. proc_remove(dn->pde);
  1510. }
  1511. #else
  1512. static void of_remove_proc_dt_entry(struct device_node *dn)
  1513. {
  1514. return;
  1515. }
  1516. #endif
  1517. /**
  1518. * of_detach_node - "Unplug" a node from the device tree.
  1519. *
  1520. * The caller must hold a reference to the node. The memory associated with
  1521. * the node is not freed until its refcount goes to zero.
  1522. */
  1523. int of_detach_node(struct device_node *np)
  1524. {
  1525. struct device_node *parent;
  1526. unsigned long flags;
  1527. int rc = 0;
  1528. rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  1529. if (rc)
  1530. return rc;
  1531. raw_spin_lock_irqsave(&devtree_lock, flags);
  1532. if (of_node_check_flag(np, OF_DETACHED)) {
  1533. /* someone already detached it */
  1534. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1535. return rc;
  1536. }
  1537. parent = np->parent;
  1538. if (!parent) {
  1539. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1540. return rc;
  1541. }
  1542. if (of_allnodes == np)
  1543. of_allnodes = np->allnext;
  1544. else {
  1545. struct device_node *prev;
  1546. for (prev = of_allnodes;
  1547. prev->allnext != np;
  1548. prev = prev->allnext)
  1549. ;
  1550. prev->allnext = np->allnext;
  1551. }
  1552. if (parent->child == np)
  1553. parent->child = np->sibling;
  1554. else {
  1555. struct device_node *prevsib;
  1556. for (prevsib = np->parent->child;
  1557. prevsib->sibling != np;
  1558. prevsib = prevsib->sibling)
  1559. ;
  1560. prevsib->sibling = np->sibling;
  1561. }
  1562. of_node_set_flag(np, OF_DETACHED);
  1563. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1564. of_remove_proc_dt_entry(np);
  1565. return rc;
  1566. }
  1567. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1568. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1569. int id, const char *stem, int stem_len)
  1570. {
  1571. ap->np = np;
  1572. ap->id = id;
  1573. strncpy(ap->stem, stem, stem_len);
  1574. ap->stem[stem_len] = 0;
  1575. list_add_tail(&ap->link, &aliases_lookup);
  1576. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1577. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1578. }
  1579. /**
  1580. * of_alias_scan - Scan all properties of 'aliases' node
  1581. *
  1582. * The function scans all the properties of 'aliases' node and populate
  1583. * the the global lookup table with the properties. It returns the
  1584. * number of alias_prop found, or error code in error case.
  1585. *
  1586. * @dt_alloc: An allocator that provides a virtual address to memory
  1587. * for the resulting tree
  1588. */
  1589. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1590. {
  1591. struct property *pp;
  1592. of_chosen = of_find_node_by_path("/chosen");
  1593. if (of_chosen == NULL)
  1594. of_chosen = of_find_node_by_path("/chosen@0");
  1595. if (of_chosen) {
  1596. const char *name;
  1597. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1598. if (name)
  1599. of_stdout = of_find_node_by_path(name);
  1600. }
  1601. of_aliases = of_find_node_by_path("/aliases");
  1602. if (!of_aliases)
  1603. return;
  1604. for_each_property_of_node(of_aliases, pp) {
  1605. const char *start = pp->name;
  1606. const char *end = start + strlen(start);
  1607. struct device_node *np;
  1608. struct alias_prop *ap;
  1609. int id, len;
  1610. /* Skip those we do not want to proceed */
  1611. if (!strcmp(pp->name, "name") ||
  1612. !strcmp(pp->name, "phandle") ||
  1613. !strcmp(pp->name, "linux,phandle"))
  1614. continue;
  1615. np = of_find_node_by_path(pp->value);
  1616. if (!np)
  1617. continue;
  1618. /* walk the alias backwards to extract the id and work out
  1619. * the 'stem' string */
  1620. while (isdigit(*(end-1)) && end > start)
  1621. end--;
  1622. len = end - start;
  1623. if (kstrtoint(end, 10, &id) < 0)
  1624. continue;
  1625. /* Allocate an alias_prop with enough space for the stem */
  1626. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1627. if (!ap)
  1628. continue;
  1629. memset(ap, 0, sizeof(*ap) + len + 1);
  1630. ap->alias = start;
  1631. of_alias_add(ap, np, id, start, len);
  1632. }
  1633. }
  1634. /**
  1635. * of_alias_get_id - Get alias id for the given device_node
  1636. * @np: Pointer to the given device_node
  1637. * @stem: Alias stem of the given device_node
  1638. *
  1639. * The function travels the lookup table to get alias id for the given
  1640. * device_node and alias stem. It returns the alias id if find it.
  1641. */
  1642. int of_alias_get_id(struct device_node *np, const char *stem)
  1643. {
  1644. struct alias_prop *app;
  1645. int id = -ENODEV;
  1646. mutex_lock(&of_aliases_mutex);
  1647. list_for_each_entry(app, &aliases_lookup, link) {
  1648. if (strcmp(app->stem, stem) != 0)
  1649. continue;
  1650. if (np == app->np) {
  1651. id = app->id;
  1652. break;
  1653. }
  1654. }
  1655. mutex_unlock(&of_aliases_mutex);
  1656. return id;
  1657. }
  1658. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1659. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1660. u32 *pu)
  1661. {
  1662. const void *curv = cur;
  1663. if (!prop)
  1664. return NULL;
  1665. if (!cur) {
  1666. curv = prop->value;
  1667. goto out_val;
  1668. }
  1669. curv += sizeof(*cur);
  1670. if (curv >= prop->value + prop->length)
  1671. return NULL;
  1672. out_val:
  1673. *pu = be32_to_cpup(curv);
  1674. return curv;
  1675. }
  1676. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1677. const char *of_prop_next_string(struct property *prop, const char *cur)
  1678. {
  1679. const void *curv = cur;
  1680. if (!prop)
  1681. return NULL;
  1682. if (!cur)
  1683. return prop->value;
  1684. curv += strlen(cur) + 1;
  1685. if (curv >= prop->value + prop->length)
  1686. return NULL;
  1687. return curv;
  1688. }
  1689. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1690. /**
  1691. * of_device_is_stdout_path - check if a device node matches the
  1692. * linux,stdout-path property
  1693. *
  1694. * Check if this device node matches the linux,stdout-path property
  1695. * in the chosen node. return true if yes, false otherwise.
  1696. */
  1697. int of_device_is_stdout_path(struct device_node *dn)
  1698. {
  1699. if (!of_stdout)
  1700. return false;
  1701. return of_stdout == dn;
  1702. }
  1703. EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
  1704. /**
  1705. * of_find_next_cache_node - Find a node's subsidiary cache
  1706. * @np: node of type "cpu" or "cache"
  1707. *
  1708. * Returns a node pointer with refcount incremented, use
  1709. * of_node_put() on it when done. Caller should hold a reference
  1710. * to np.
  1711. */
  1712. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1713. {
  1714. struct device_node *child;
  1715. const phandle *handle;
  1716. handle = of_get_property(np, "l2-cache", NULL);
  1717. if (!handle)
  1718. handle = of_get_property(np, "next-level-cache", NULL);
  1719. if (handle)
  1720. return of_find_node_by_phandle(be32_to_cpup(handle));
  1721. /* OF on pmac has nodes instead of properties named "l2-cache"
  1722. * beneath CPU nodes.
  1723. */
  1724. if (!strcmp(np->type, "cpu"))
  1725. for_each_child_of_node(np, child)
  1726. if (!strcmp(child->type, "cache"))
  1727. return child;
  1728. return NULL;
  1729. }