base.c 60 KB

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