base.c 60 KB

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