base.c 52 KB

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