base.c 69 KB

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