base.c 53 KB

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