base.c 64 KB

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