base.c 58 KB

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