base.c 56 KB

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