base.c 53 KB

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