base.c 62 KB

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