base.c 53 KB

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