base.c 52 KB

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