base.c 60 KB

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