base.c 62 KB

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