base.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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/console.h>
  18. #include <linux/ctype.h>
  19. #include <linux/cpu.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_graph.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/proc_fs.h>
  28. #include "of_private.h"
  29. LIST_HEAD(aliases_lookup);
  30. struct device_node *of_root;
  31. EXPORT_SYMBOL(of_root);
  32. struct device_node *of_chosen;
  33. struct device_node *of_aliases;
  34. struct device_node *of_stdout;
  35. static const char *of_stdout_options;
  36. struct kset *of_kset;
  37. /*
  38. * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
  39. * This mutex must be held whenever modifications are being made to the
  40. * device tree. The of_{attach,detach}_node() and
  41. * of_{add,remove,update}_property() helpers make sure this happens.
  42. */
  43. DEFINE_MUTEX(of_mutex);
  44. /* use when traversing tree through the child, sibling,
  45. * or parent members of struct device_node.
  46. */
  47. DEFINE_RAW_SPINLOCK(devtree_lock);
  48. int of_n_addr_cells(struct device_node *np)
  49. {
  50. u32 cells;
  51. do {
  52. if (np->parent)
  53. np = np->parent;
  54. if (!of_property_read_u32(np, "#address-cells", &cells))
  55. return cells;
  56. } while (np->parent);
  57. /* No #address-cells property for the root node */
  58. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  59. }
  60. EXPORT_SYMBOL(of_n_addr_cells);
  61. int of_n_size_cells(struct device_node *np)
  62. {
  63. u32 cells;
  64. do {
  65. if (np->parent)
  66. np = np->parent;
  67. if (!of_property_read_u32(np, "#size-cells", &cells))
  68. return cells;
  69. } while (np->parent);
  70. /* No #size-cells property for the root node */
  71. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  72. }
  73. EXPORT_SYMBOL(of_n_size_cells);
  74. #ifdef CONFIG_NUMA
  75. int __weak of_node_to_nid(struct device_node *np)
  76. {
  77. return NUMA_NO_NODE;
  78. }
  79. #endif
  80. void __init of_core_init(void)
  81. {
  82. struct device_node *np;
  83. /* Create the kset, and register existing nodes */
  84. mutex_lock(&of_mutex);
  85. of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
  86. if (!of_kset) {
  87. mutex_unlock(&of_mutex);
  88. pr_err("failed to register existing nodes\n");
  89. return;
  90. }
  91. for_each_of_allnodes(np)
  92. __of_attach_node_sysfs(np);
  93. mutex_unlock(&of_mutex);
  94. /* Symlink in /proc as required by userspace ABI */
  95. if (of_root)
  96. proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
  97. }
  98. static struct property *__of_find_property(const struct device_node *np,
  99. const char *name, int *lenp)
  100. {
  101. struct property *pp;
  102. if (!np)
  103. return NULL;
  104. for (pp = np->properties; pp; pp = pp->next) {
  105. if (of_prop_cmp(pp->name, name) == 0) {
  106. if (lenp)
  107. *lenp = pp->length;
  108. break;
  109. }
  110. }
  111. return pp;
  112. }
  113. struct property *of_find_property(const struct device_node *np,
  114. const char *name,
  115. int *lenp)
  116. {
  117. struct property *pp;
  118. unsigned long flags;
  119. raw_spin_lock_irqsave(&devtree_lock, flags);
  120. pp = __of_find_property(np, name, lenp);
  121. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  122. return pp;
  123. }
  124. EXPORT_SYMBOL(of_find_property);
  125. struct device_node *__of_find_all_nodes(struct device_node *prev)
  126. {
  127. struct device_node *np;
  128. if (!prev) {
  129. np = of_root;
  130. } else if (prev->child) {
  131. np = prev->child;
  132. } else {
  133. /* Walk back up looking for a sibling, or the end of the structure */
  134. np = prev;
  135. while (np->parent && !np->sibling)
  136. np = np->parent;
  137. np = np->sibling; /* Might be null at the end of the tree */
  138. }
  139. return np;
  140. }
  141. /**
  142. * of_find_all_nodes - Get next node in global list
  143. * @prev: Previous node or NULL to start iteration
  144. * of_node_put() will be called on it
  145. *
  146. * Returns a node pointer with refcount incremented, use
  147. * of_node_put() on it when done.
  148. */
  149. struct device_node *of_find_all_nodes(struct device_node *prev)
  150. {
  151. struct device_node *np;
  152. unsigned long flags;
  153. raw_spin_lock_irqsave(&devtree_lock, flags);
  154. np = __of_find_all_nodes(prev);
  155. of_node_get(np);
  156. of_node_put(prev);
  157. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  158. return np;
  159. }
  160. EXPORT_SYMBOL(of_find_all_nodes);
  161. /*
  162. * Find a property with a given name for a given node
  163. * and return the value.
  164. */
  165. const void *__of_get_property(const struct device_node *np,
  166. const char *name, int *lenp)
  167. {
  168. struct property *pp = __of_find_property(np, name, lenp);
  169. return pp ? pp->value : NULL;
  170. }
  171. /*
  172. * Find a property with a given name for a given node
  173. * and return the value.
  174. */
  175. const void *of_get_property(const struct device_node *np, const char *name,
  176. int *lenp)
  177. {
  178. struct property *pp = of_find_property(np, name, lenp);
  179. return pp ? pp->value : NULL;
  180. }
  181. EXPORT_SYMBOL(of_get_property);
  182. /*
  183. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  184. *
  185. * @cpu: logical cpu index of a core/thread
  186. * @phys_id: physical identifier of a core/thread
  187. *
  188. * CPU logical to physical index mapping is architecture specific.
  189. * However this __weak function provides a default match of physical
  190. * id to logical cpu index. phys_id provided here is usually values read
  191. * from the device tree which must match the hardware internal registers.
  192. *
  193. * Returns true if the physical identifier and the logical cpu index
  194. * correspond to the same core/thread, false otherwise.
  195. */
  196. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  197. {
  198. return (u32)phys_id == cpu;
  199. }
  200. /**
  201. * Checks if the given "prop_name" property holds the physical id of the
  202. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  203. * NULL, local thread number within the core is returned in it.
  204. */
  205. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  206. const char *prop_name, int cpu, unsigned int *thread)
  207. {
  208. const __be32 *cell;
  209. int ac, prop_len, tid;
  210. u64 hwid;
  211. ac = of_n_addr_cells(cpun);
  212. cell = of_get_property(cpun, prop_name, &prop_len);
  213. if (!cell || !ac)
  214. return false;
  215. prop_len /= sizeof(*cell) * ac;
  216. for (tid = 0; tid < prop_len; tid++) {
  217. hwid = of_read_number(cell, ac);
  218. if (arch_match_cpu_phys_id(cpu, hwid)) {
  219. if (thread)
  220. *thread = tid;
  221. return true;
  222. }
  223. cell += ac;
  224. }
  225. return false;
  226. }
  227. /*
  228. * arch_find_n_match_cpu_physical_id - See if the given device node is
  229. * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
  230. * else false. If 'thread' is non-NULL, the local thread number within the
  231. * core is returned in it.
  232. */
  233. bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  234. int cpu, unsigned int *thread)
  235. {
  236. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  237. * for thread ids on PowerPC. If it doesn't exist fallback to
  238. * standard "reg" property.
  239. */
  240. if (IS_ENABLED(CONFIG_PPC) &&
  241. __of_find_n_match_cpu_property(cpun,
  242. "ibm,ppc-interrupt-server#s",
  243. cpu, thread))
  244. return true;
  245. return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
  246. }
  247. /**
  248. * of_get_cpu_node - Get device node associated with the given logical CPU
  249. *
  250. * @cpu: CPU number(logical index) for which device node is required
  251. * @thread: if not NULL, local thread number within the physical core is
  252. * returned
  253. *
  254. * The main purpose of this function is to retrieve the device node for the
  255. * given logical CPU index. It should be used to initialize the of_node in
  256. * cpu device. Once of_node in cpu device is populated, all the further
  257. * references can use that instead.
  258. *
  259. * CPU logical to physical index mapping is architecture specific and is built
  260. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  261. * which can be overridden by architecture specific implementation.
  262. *
  263. * Returns a node pointer for the logical cpu with refcount incremented, use
  264. * of_node_put() on it when done. Returns NULL if not found.
  265. */
  266. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  267. {
  268. struct device_node *cpun;
  269. for_each_node_by_type(cpun, "cpu") {
  270. if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
  271. return cpun;
  272. }
  273. return NULL;
  274. }
  275. EXPORT_SYMBOL(of_get_cpu_node);
  276. /**
  277. * of_cpu_node_to_id: Get the logical CPU number for a given device_node
  278. *
  279. * @cpu_node: Pointer to the device_node for CPU.
  280. *
  281. * Returns the logical CPU number of the given CPU device_node.
  282. * Returns -ENODEV if the CPU is not found.
  283. */
  284. int of_cpu_node_to_id(struct device_node *cpu_node)
  285. {
  286. int cpu;
  287. bool found = false;
  288. struct device_node *np;
  289. for_each_possible_cpu(cpu) {
  290. np = of_cpu_device_node_get(cpu);
  291. found = (cpu_node == np);
  292. of_node_put(np);
  293. if (found)
  294. return cpu;
  295. }
  296. return -ENODEV;
  297. }
  298. EXPORT_SYMBOL(of_cpu_node_to_id);
  299. /**
  300. * __of_device_is_compatible() - Check if the node matches given constraints
  301. * @device: pointer to node
  302. * @compat: required compatible string, NULL or "" for any match
  303. * @type: required device_type value, NULL or "" for any match
  304. * @name: required node name, NULL or "" for any match
  305. *
  306. * Checks if the given @compat, @type and @name strings match the
  307. * properties of the given @device. A constraints can be skipped by
  308. * passing NULL or an empty string as the constraint.
  309. *
  310. * Returns 0 for no match, and a positive integer on match. The return
  311. * value is a relative score with larger values indicating better
  312. * matches. The score is weighted for the most specific compatible value
  313. * to get the highest score. Matching type is next, followed by matching
  314. * name. Practically speaking, this results in the following priority
  315. * order for matches:
  316. *
  317. * 1. specific compatible && type && name
  318. * 2. specific compatible && type
  319. * 3. specific compatible && name
  320. * 4. specific compatible
  321. * 5. general compatible && type && name
  322. * 6. general compatible && type
  323. * 7. general compatible && name
  324. * 8. general compatible
  325. * 9. type && name
  326. * 10. type
  327. * 11. name
  328. */
  329. static int __of_device_is_compatible(const struct device_node *device,
  330. const char *compat, const char *type, const char *name)
  331. {
  332. struct property *prop;
  333. const char *cp;
  334. int index = 0, score = 0;
  335. /* Compatible match has highest priority */
  336. if (compat && compat[0]) {
  337. prop = __of_find_property(device, "compatible", NULL);
  338. for (cp = of_prop_next_string(prop, NULL); cp;
  339. cp = of_prop_next_string(prop, cp), index++) {
  340. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  341. score = INT_MAX/2 - (index << 2);
  342. break;
  343. }
  344. }
  345. if (!score)
  346. return 0;
  347. }
  348. /* Matching type is better than matching name */
  349. if (type && type[0]) {
  350. if (!device->type || of_node_cmp(type, device->type))
  351. return 0;
  352. score += 2;
  353. }
  354. /* Matching name is a bit better than not */
  355. if (name && name[0]) {
  356. if (!device->name || of_node_cmp(name, device->name))
  357. return 0;
  358. score++;
  359. }
  360. return score;
  361. }
  362. /** Checks if the given "compat" string matches one of the strings in
  363. * the device's "compatible" property
  364. */
  365. int of_device_is_compatible(const struct device_node *device,
  366. const char *compat)
  367. {
  368. unsigned long flags;
  369. int res;
  370. raw_spin_lock_irqsave(&devtree_lock, flags);
  371. res = __of_device_is_compatible(device, compat, NULL, NULL);
  372. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  373. return res;
  374. }
  375. EXPORT_SYMBOL(of_device_is_compatible);
  376. /** Checks if the device is compatible with any of the entries in
  377. * a NULL terminated array of strings. Returns the best match
  378. * score or 0.
  379. */
  380. int of_device_compatible_match(struct device_node *device,
  381. const char *const *compat)
  382. {
  383. unsigned int tmp, score = 0;
  384. if (!compat)
  385. return 0;
  386. while (*compat) {
  387. tmp = of_device_is_compatible(device, *compat);
  388. if (tmp > score)
  389. score = tmp;
  390. compat++;
  391. }
  392. return score;
  393. }
  394. /**
  395. * of_machine_is_compatible - Test root of device tree for a given compatible value
  396. * @compat: compatible string to look for in root node's compatible property.
  397. *
  398. * Returns a positive integer if the root node has the given value in its
  399. * compatible property.
  400. */
  401. int of_machine_is_compatible(const char *compat)
  402. {
  403. struct device_node *root;
  404. int rc = 0;
  405. root = of_find_node_by_path("/");
  406. if (root) {
  407. rc = of_device_is_compatible(root, compat);
  408. of_node_put(root);
  409. }
  410. return rc;
  411. }
  412. EXPORT_SYMBOL(of_machine_is_compatible);
  413. /**
  414. * __of_device_is_available - check if a device is available for use
  415. *
  416. * @device: Node to check for availability, with locks already held
  417. *
  418. * Returns true if the status property is absent or set to "okay" or "ok",
  419. * false otherwise
  420. */
  421. static bool __of_device_is_available(const struct device_node *device)
  422. {
  423. const char *status;
  424. int statlen;
  425. if (!device)
  426. return false;
  427. status = __of_get_property(device, "status", &statlen);
  428. if (status == NULL)
  429. return true;
  430. if (statlen > 0) {
  431. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  432. return true;
  433. }
  434. return false;
  435. }
  436. /**
  437. * of_device_is_available - check if a device is available for use
  438. *
  439. * @device: Node to check for availability
  440. *
  441. * Returns true if the status property is absent or set to "okay" or "ok",
  442. * false otherwise
  443. */
  444. bool of_device_is_available(const struct device_node *device)
  445. {
  446. unsigned long flags;
  447. bool res;
  448. raw_spin_lock_irqsave(&devtree_lock, flags);
  449. res = __of_device_is_available(device);
  450. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  451. return res;
  452. }
  453. EXPORT_SYMBOL(of_device_is_available);
  454. /**
  455. * of_device_is_big_endian - check if a device has BE registers
  456. *
  457. * @device: Node to check for endianness
  458. *
  459. * Returns true if the device has a "big-endian" property, or if the kernel
  460. * was compiled for BE *and* the device has a "native-endian" property.
  461. * Returns false otherwise.
  462. *
  463. * Callers would nominally use ioread32be/iowrite32be if
  464. * of_device_is_big_endian() == true, or readl/writel otherwise.
  465. */
  466. bool of_device_is_big_endian(const struct device_node *device)
  467. {
  468. if (of_property_read_bool(device, "big-endian"))
  469. return true;
  470. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  471. of_property_read_bool(device, "native-endian"))
  472. return true;
  473. return false;
  474. }
  475. EXPORT_SYMBOL(of_device_is_big_endian);
  476. /**
  477. * of_get_parent - Get a node's parent if any
  478. * @node: Node to get parent
  479. *
  480. * Returns a node pointer with refcount incremented, use
  481. * of_node_put() on it when done.
  482. */
  483. struct device_node *of_get_parent(const struct device_node *node)
  484. {
  485. struct device_node *np;
  486. unsigned long flags;
  487. if (!node)
  488. return NULL;
  489. raw_spin_lock_irqsave(&devtree_lock, flags);
  490. np = of_node_get(node->parent);
  491. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  492. return np;
  493. }
  494. EXPORT_SYMBOL(of_get_parent);
  495. /**
  496. * of_get_next_parent - Iterate to a node's parent
  497. * @node: Node to get parent of
  498. *
  499. * This is like of_get_parent() except that it drops the
  500. * refcount on the passed node, making it suitable for iterating
  501. * through a node's parents.
  502. *
  503. * Returns a node pointer with refcount incremented, use
  504. * of_node_put() on it when done.
  505. */
  506. struct device_node *of_get_next_parent(struct device_node *node)
  507. {
  508. struct device_node *parent;
  509. unsigned long flags;
  510. if (!node)
  511. return NULL;
  512. raw_spin_lock_irqsave(&devtree_lock, flags);
  513. parent = of_node_get(node->parent);
  514. of_node_put(node);
  515. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  516. return parent;
  517. }
  518. EXPORT_SYMBOL(of_get_next_parent);
  519. static struct device_node *__of_get_next_child(const struct device_node *node,
  520. struct device_node *prev)
  521. {
  522. struct device_node *next;
  523. if (!node)
  524. return NULL;
  525. next = prev ? prev->sibling : node->child;
  526. for (; next; next = next->sibling)
  527. if (of_node_get(next))
  528. break;
  529. of_node_put(prev);
  530. return next;
  531. }
  532. #define __for_each_child_of_node(parent, child) \
  533. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  534. child = __of_get_next_child(parent, child))
  535. /**
  536. * of_get_next_child - Iterate a node childs
  537. * @node: parent node
  538. * @prev: previous child of the parent node, or NULL to get first
  539. *
  540. * Returns a node pointer with refcount incremented, use of_node_put() on
  541. * it when done. Returns NULL when prev is the last child. Decrements the
  542. * refcount of prev.
  543. */
  544. struct device_node *of_get_next_child(const struct device_node *node,
  545. struct device_node *prev)
  546. {
  547. struct device_node *next;
  548. unsigned long flags;
  549. raw_spin_lock_irqsave(&devtree_lock, flags);
  550. next = __of_get_next_child(node, prev);
  551. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  552. return next;
  553. }
  554. EXPORT_SYMBOL(of_get_next_child);
  555. /**
  556. * of_get_next_available_child - Find the next available child node
  557. * @node: parent node
  558. * @prev: previous child of the parent node, or NULL to get first
  559. *
  560. * This function is like of_get_next_child(), except that it
  561. * automatically skips any disabled nodes (i.e. status = "disabled").
  562. */
  563. struct device_node *of_get_next_available_child(const struct device_node *node,
  564. struct device_node *prev)
  565. {
  566. struct device_node *next;
  567. unsigned long flags;
  568. if (!node)
  569. return NULL;
  570. raw_spin_lock_irqsave(&devtree_lock, flags);
  571. next = prev ? prev->sibling : node->child;
  572. for (; next; next = next->sibling) {
  573. if (!__of_device_is_available(next))
  574. continue;
  575. if (of_node_get(next))
  576. break;
  577. }
  578. of_node_put(prev);
  579. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  580. return next;
  581. }
  582. EXPORT_SYMBOL(of_get_next_available_child);
  583. /**
  584. * of_get_child_by_name - Find the child node by name for a given parent
  585. * @node: parent node
  586. * @name: child name to look for.
  587. *
  588. * This function looks for child node for given matching name
  589. *
  590. * Returns a node pointer if found, with refcount incremented, use
  591. * of_node_put() on it when done.
  592. * Returns NULL if node is not found.
  593. */
  594. struct device_node *of_get_child_by_name(const struct device_node *node,
  595. const char *name)
  596. {
  597. struct device_node *child;
  598. for_each_child_of_node(node, child)
  599. if (child->name && (of_node_cmp(child->name, name) == 0))
  600. break;
  601. return child;
  602. }
  603. EXPORT_SYMBOL(of_get_child_by_name);
  604. struct device_node *__of_find_node_by_path(struct device_node *parent,
  605. const char *path)
  606. {
  607. struct device_node *child;
  608. int len;
  609. len = strcspn(path, "/:");
  610. if (!len)
  611. return NULL;
  612. __for_each_child_of_node(parent, child) {
  613. const char *name = kbasename(child->full_name);
  614. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  615. return child;
  616. }
  617. return NULL;
  618. }
  619. struct device_node *__of_find_node_by_full_path(struct device_node *node,
  620. const char *path)
  621. {
  622. const char *separator = strchr(path, ':');
  623. while (node && *path == '/') {
  624. struct device_node *tmp = node;
  625. path++; /* Increment past '/' delimiter */
  626. node = __of_find_node_by_path(node, path);
  627. of_node_put(tmp);
  628. path = strchrnul(path, '/');
  629. if (separator && separator < path)
  630. break;
  631. }
  632. return node;
  633. }
  634. /**
  635. * of_find_node_opts_by_path - Find a node matching a full OF path
  636. * @path: Either the full path to match, or if the path does not
  637. * start with '/', the name of a property of the /aliases
  638. * node (an alias). In the case of an alias, the node
  639. * matching the alias' value will be returned.
  640. * @opts: Address of a pointer into which to store the start of
  641. * an options string appended to the end of the path with
  642. * a ':' separator.
  643. *
  644. * Valid paths:
  645. * /foo/bar Full path
  646. * foo Valid alias
  647. * foo/bar Valid alias + relative path
  648. *
  649. * Returns a node pointer with refcount incremented, use
  650. * of_node_put() on it when done.
  651. */
  652. struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
  653. {
  654. struct device_node *np = NULL;
  655. struct property *pp;
  656. unsigned long flags;
  657. const char *separator = strchr(path, ':');
  658. if (opts)
  659. *opts = separator ? separator + 1 : NULL;
  660. if (strcmp(path, "/") == 0)
  661. return of_node_get(of_root);
  662. /* The path could begin with an alias */
  663. if (*path != '/') {
  664. int len;
  665. const char *p = separator;
  666. if (!p)
  667. p = strchrnul(path, '/');
  668. len = p - path;
  669. /* of_aliases must not be NULL */
  670. if (!of_aliases)
  671. return NULL;
  672. for_each_property_of_node(of_aliases, pp) {
  673. if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
  674. np = of_find_node_by_path(pp->value);
  675. break;
  676. }
  677. }
  678. if (!np)
  679. return NULL;
  680. path = p;
  681. }
  682. /* Step down the tree matching path components */
  683. raw_spin_lock_irqsave(&devtree_lock, flags);
  684. if (!np)
  685. np = of_node_get(of_root);
  686. np = __of_find_node_by_full_path(np, path);
  687. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  688. return np;
  689. }
  690. EXPORT_SYMBOL(of_find_node_opts_by_path);
  691. /**
  692. * of_find_node_by_name - Find a node by its "name" property
  693. * @from: The node to start searching from or NULL; the node
  694. * you pass will not be searched, only the next one
  695. * will. Typically, you pass what the previous call
  696. * returned. of_node_put() will be called on @from.
  697. * @name: The name string to match against
  698. *
  699. * Returns a node pointer with refcount incremented, use
  700. * of_node_put() on it when done.
  701. */
  702. struct device_node *of_find_node_by_name(struct device_node *from,
  703. const char *name)
  704. {
  705. struct device_node *np;
  706. unsigned long flags;
  707. raw_spin_lock_irqsave(&devtree_lock, flags);
  708. for_each_of_allnodes_from(from, np)
  709. if (np->name && (of_node_cmp(np->name, name) == 0)
  710. && of_node_get(np))
  711. break;
  712. of_node_put(from);
  713. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  714. return np;
  715. }
  716. EXPORT_SYMBOL(of_find_node_by_name);
  717. /**
  718. * of_find_node_by_type - Find a node by its "device_type" property
  719. * @from: The node to start searching from, or NULL to start searching
  720. * the entire device tree. The node you pass will not be
  721. * searched, only the next one will; typically, you pass
  722. * what the previous call returned. of_node_put() will be
  723. * called on from for you.
  724. * @type: The type string to match against
  725. *
  726. * Returns a node pointer with refcount incremented, use
  727. * of_node_put() on it when done.
  728. */
  729. struct device_node *of_find_node_by_type(struct device_node *from,
  730. const char *type)
  731. {
  732. struct device_node *np;
  733. unsigned long flags;
  734. raw_spin_lock_irqsave(&devtree_lock, flags);
  735. for_each_of_allnodes_from(from, np)
  736. if (np->type && (of_node_cmp(np->type, type) == 0)
  737. && of_node_get(np))
  738. break;
  739. of_node_put(from);
  740. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  741. return np;
  742. }
  743. EXPORT_SYMBOL(of_find_node_by_type);
  744. /**
  745. * of_find_compatible_node - Find a node based on type and one of the
  746. * tokens in its "compatible" property
  747. * @from: The node to start searching from or NULL, the node
  748. * you pass will not be searched, only the next one
  749. * will; typically, you pass what the previous call
  750. * returned. of_node_put() will be called on it
  751. * @type: The type string to match "device_type" or NULL to ignore
  752. * @compatible: The string to match to one of the tokens in the device
  753. * "compatible" list.
  754. *
  755. * Returns a node pointer with refcount incremented, use
  756. * of_node_put() on it when done.
  757. */
  758. struct device_node *of_find_compatible_node(struct device_node *from,
  759. const char *type, const char *compatible)
  760. {
  761. struct device_node *np;
  762. unsigned long flags;
  763. raw_spin_lock_irqsave(&devtree_lock, flags);
  764. for_each_of_allnodes_from(from, np)
  765. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  766. of_node_get(np))
  767. break;
  768. of_node_put(from);
  769. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  770. return np;
  771. }
  772. EXPORT_SYMBOL(of_find_compatible_node);
  773. /**
  774. * of_find_node_with_property - Find a node which has a property with
  775. * the given name.
  776. * @from: The node to start searching from or NULL, the node
  777. * you pass will not be searched, only the next one
  778. * will; typically, you pass what the previous call
  779. * returned. of_node_put() will be called on it
  780. * @prop_name: The name of the property to look for.
  781. *
  782. * Returns a node pointer with refcount incremented, use
  783. * of_node_put() on it when done.
  784. */
  785. struct device_node *of_find_node_with_property(struct device_node *from,
  786. const char *prop_name)
  787. {
  788. struct device_node *np;
  789. struct property *pp;
  790. unsigned long flags;
  791. raw_spin_lock_irqsave(&devtree_lock, flags);
  792. for_each_of_allnodes_from(from, np) {
  793. for (pp = np->properties; pp; pp = pp->next) {
  794. if (of_prop_cmp(pp->name, prop_name) == 0) {
  795. of_node_get(np);
  796. goto out;
  797. }
  798. }
  799. }
  800. out:
  801. of_node_put(from);
  802. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  803. return np;
  804. }
  805. EXPORT_SYMBOL(of_find_node_with_property);
  806. static
  807. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  808. const struct device_node *node)
  809. {
  810. const struct of_device_id *best_match = NULL;
  811. int score, best_score = 0;
  812. if (!matches)
  813. return NULL;
  814. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  815. score = __of_device_is_compatible(node, matches->compatible,
  816. matches->type, matches->name);
  817. if (score > best_score) {
  818. best_match = matches;
  819. best_score = score;
  820. }
  821. }
  822. return best_match;
  823. }
  824. /**
  825. * of_match_node - Tell if a device_node has a matching of_match structure
  826. * @matches: array of of device match structures to search in
  827. * @node: the of device structure to match against
  828. *
  829. * Low level utility function used by device matching.
  830. */
  831. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  832. const struct device_node *node)
  833. {
  834. const struct of_device_id *match;
  835. unsigned long flags;
  836. raw_spin_lock_irqsave(&devtree_lock, flags);
  837. match = __of_match_node(matches, node);
  838. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  839. return match;
  840. }
  841. EXPORT_SYMBOL(of_match_node);
  842. /**
  843. * of_find_matching_node_and_match - Find a node based on an of_device_id
  844. * match table.
  845. * @from: The node to start searching from or NULL, the node
  846. * you pass will not be searched, only the next one
  847. * will; typically, you pass what the previous call
  848. * returned. of_node_put() will be called on it
  849. * @matches: array of of device match structures to search in
  850. * @match Updated to point at the matches entry which matched
  851. *
  852. * Returns a node pointer with refcount incremented, use
  853. * of_node_put() on it when done.
  854. */
  855. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  856. const struct of_device_id *matches,
  857. const struct of_device_id **match)
  858. {
  859. struct device_node *np;
  860. const struct of_device_id *m;
  861. unsigned long flags;
  862. if (match)
  863. *match = NULL;
  864. raw_spin_lock_irqsave(&devtree_lock, flags);
  865. for_each_of_allnodes_from(from, np) {
  866. m = __of_match_node(matches, np);
  867. if (m && of_node_get(np)) {
  868. if (match)
  869. *match = m;
  870. break;
  871. }
  872. }
  873. of_node_put(from);
  874. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  875. return np;
  876. }
  877. EXPORT_SYMBOL(of_find_matching_node_and_match);
  878. /**
  879. * of_modalias_node - Lookup appropriate modalias for a device node
  880. * @node: pointer to a device tree node
  881. * @modalias: Pointer to buffer that modalias value will be copied into
  882. * @len: Length of modalias value
  883. *
  884. * Based on the value of the compatible property, this routine will attempt
  885. * to choose an appropriate modalias value for a particular device tree node.
  886. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  887. * from the first entry in the compatible list property.
  888. *
  889. * This routine returns 0 on success, <0 on failure.
  890. */
  891. int of_modalias_node(struct device_node *node, char *modalias, int len)
  892. {
  893. const char *compatible, *p;
  894. int cplen;
  895. compatible = of_get_property(node, "compatible", &cplen);
  896. if (!compatible || strlen(compatible) > cplen)
  897. return -ENODEV;
  898. p = strchr(compatible, ',');
  899. strlcpy(modalias, p ? p + 1 : compatible, len);
  900. return 0;
  901. }
  902. EXPORT_SYMBOL_GPL(of_modalias_node);
  903. /**
  904. * of_find_node_by_phandle - Find a node given a phandle
  905. * @handle: phandle of the node to find
  906. *
  907. * Returns a node pointer with refcount incremented, use
  908. * of_node_put() on it when done.
  909. */
  910. struct device_node *of_find_node_by_phandle(phandle handle)
  911. {
  912. struct device_node *np;
  913. unsigned long flags;
  914. if (!handle)
  915. return NULL;
  916. raw_spin_lock_irqsave(&devtree_lock, flags);
  917. for_each_of_allnodes(np)
  918. if (np->phandle == handle)
  919. break;
  920. of_node_get(np);
  921. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  922. return np;
  923. }
  924. EXPORT_SYMBOL(of_find_node_by_phandle);
  925. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  926. {
  927. int i;
  928. printk("%s %pOF", msg, args->np);
  929. for (i = 0; i < args->args_count; i++) {
  930. const char delim = i ? ',' : ':';
  931. pr_cont("%c%08x", delim, args->args[i]);
  932. }
  933. pr_cont("\n");
  934. }
  935. int of_phandle_iterator_init(struct of_phandle_iterator *it,
  936. const struct device_node *np,
  937. const char *list_name,
  938. const char *cells_name,
  939. int cell_count)
  940. {
  941. const __be32 *list;
  942. int size;
  943. memset(it, 0, sizeof(*it));
  944. list = of_get_property(np, list_name, &size);
  945. if (!list)
  946. return -ENOENT;
  947. it->cells_name = cells_name;
  948. it->cell_count = cell_count;
  949. it->parent = np;
  950. it->list_end = list + size / sizeof(*list);
  951. it->phandle_end = list;
  952. it->cur = list;
  953. return 0;
  954. }
  955. EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
  956. int of_phandle_iterator_next(struct of_phandle_iterator *it)
  957. {
  958. uint32_t count = 0;
  959. if (it->node) {
  960. of_node_put(it->node);
  961. it->node = NULL;
  962. }
  963. if (!it->cur || it->phandle_end >= it->list_end)
  964. return -ENOENT;
  965. it->cur = it->phandle_end;
  966. /* If phandle is 0, then it is an empty entry with no arguments. */
  967. it->phandle = be32_to_cpup(it->cur++);
  968. if (it->phandle) {
  969. /*
  970. * Find the provider node and parse the #*-cells property to
  971. * determine the argument length.
  972. */
  973. it->node = of_find_node_by_phandle(it->phandle);
  974. if (it->cells_name) {
  975. if (!it->node) {
  976. pr_err("%pOF: could not find phandle\n",
  977. it->parent);
  978. goto err;
  979. }
  980. if (of_property_read_u32(it->node, it->cells_name,
  981. &count)) {
  982. pr_err("%pOF: could not get %s for %pOF\n",
  983. it->parent,
  984. it->cells_name,
  985. it->node);
  986. goto err;
  987. }
  988. } else {
  989. count = it->cell_count;
  990. }
  991. /*
  992. * Make sure that the arguments actually fit in the remaining
  993. * property data length
  994. */
  995. if (it->cur + count > it->list_end) {
  996. pr_err("%pOF: arguments longer than property\n",
  997. it->parent);
  998. goto err;
  999. }
  1000. }
  1001. it->phandle_end = it->cur + count;
  1002. it->cur_count = count;
  1003. return 0;
  1004. err:
  1005. if (it->node) {
  1006. of_node_put(it->node);
  1007. it->node = NULL;
  1008. }
  1009. return -EINVAL;
  1010. }
  1011. EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
  1012. int of_phandle_iterator_args(struct of_phandle_iterator *it,
  1013. uint32_t *args,
  1014. int size)
  1015. {
  1016. int i, count;
  1017. count = it->cur_count;
  1018. if (WARN_ON(size < count))
  1019. count = size;
  1020. for (i = 0; i < count; i++)
  1021. args[i] = be32_to_cpup(it->cur++);
  1022. return count;
  1023. }
  1024. static int __of_parse_phandle_with_args(const struct device_node *np,
  1025. const char *list_name,
  1026. const char *cells_name,
  1027. int cell_count, int index,
  1028. struct of_phandle_args *out_args)
  1029. {
  1030. struct of_phandle_iterator it;
  1031. int rc, cur_index = 0;
  1032. /* Loop over the phandles until all the requested entry is found */
  1033. of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
  1034. /*
  1035. * All of the error cases bail out of the loop, so at
  1036. * this point, the parsing is successful. If the requested
  1037. * index matches, then fill the out_args structure and return,
  1038. * or return -ENOENT for an empty entry.
  1039. */
  1040. rc = -ENOENT;
  1041. if (cur_index == index) {
  1042. if (!it.phandle)
  1043. goto err;
  1044. if (out_args) {
  1045. int c;
  1046. c = of_phandle_iterator_args(&it,
  1047. out_args->args,
  1048. MAX_PHANDLE_ARGS);
  1049. out_args->np = it.node;
  1050. out_args->args_count = c;
  1051. } else {
  1052. of_node_put(it.node);
  1053. }
  1054. /* Found it! return success */
  1055. return 0;
  1056. }
  1057. cur_index++;
  1058. }
  1059. /*
  1060. * Unlock node before returning result; will be one of:
  1061. * -ENOENT : index is for empty phandle
  1062. * -EINVAL : parsing error on data
  1063. */
  1064. err:
  1065. of_node_put(it.node);
  1066. return rc;
  1067. }
  1068. /**
  1069. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1070. * @np: Pointer to device node holding phandle property
  1071. * @phandle_name: Name of property holding a phandle value
  1072. * @index: For properties holding a table of phandles, this is the index into
  1073. * the table
  1074. *
  1075. * Returns the device_node pointer with refcount incremented. Use
  1076. * of_node_put() on it when done.
  1077. */
  1078. struct device_node *of_parse_phandle(const struct device_node *np,
  1079. const char *phandle_name, int index)
  1080. {
  1081. struct of_phandle_args args;
  1082. if (index < 0)
  1083. return NULL;
  1084. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1085. index, &args))
  1086. return NULL;
  1087. return args.np;
  1088. }
  1089. EXPORT_SYMBOL(of_parse_phandle);
  1090. /**
  1091. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1092. * @np: pointer to a device tree node containing a list
  1093. * @list_name: property name that contains a list
  1094. * @cells_name: property name that specifies phandles' arguments count
  1095. * @index: index of a phandle to parse out
  1096. * @out_args: optional pointer to output arguments structure (will be filled)
  1097. *
  1098. * This function is useful to parse lists of phandles and their arguments.
  1099. * Returns 0 on success and fills out_args, on error returns appropriate
  1100. * errno value.
  1101. *
  1102. * Caller is responsible to call of_node_put() on the returned out_args->np
  1103. * pointer.
  1104. *
  1105. * Example:
  1106. *
  1107. * phandle1: node1 {
  1108. * #list-cells = <2>;
  1109. * }
  1110. *
  1111. * phandle2: node2 {
  1112. * #list-cells = <1>;
  1113. * }
  1114. *
  1115. * node3 {
  1116. * list = <&phandle1 1 2 &phandle2 3>;
  1117. * }
  1118. *
  1119. * To get a device_node of the `node2' node you may call this:
  1120. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1121. */
  1122. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1123. const char *cells_name, int index,
  1124. struct of_phandle_args *out_args)
  1125. {
  1126. if (index < 0)
  1127. return -EINVAL;
  1128. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1129. index, out_args);
  1130. }
  1131. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1132. /**
  1133. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1134. * @np: pointer to a device tree node containing a list
  1135. * @list_name: property name that contains a list
  1136. * @cell_count: number of argument cells following the phandle
  1137. * @index: index of a phandle to parse out
  1138. * @out_args: optional pointer to output arguments structure (will be filled)
  1139. *
  1140. * This function is useful to parse lists of phandles and their arguments.
  1141. * Returns 0 on success and fills out_args, on error returns appropriate
  1142. * errno value.
  1143. *
  1144. * Caller is responsible to call of_node_put() on the returned out_args->np
  1145. * pointer.
  1146. *
  1147. * Example:
  1148. *
  1149. * phandle1: node1 {
  1150. * }
  1151. *
  1152. * phandle2: node2 {
  1153. * }
  1154. *
  1155. * node3 {
  1156. * list = <&phandle1 0 2 &phandle2 2 3>;
  1157. * }
  1158. *
  1159. * To get a device_node of the `node2' node you may call this:
  1160. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1161. */
  1162. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1163. const char *list_name, int cell_count,
  1164. int index, struct of_phandle_args *out_args)
  1165. {
  1166. if (index < 0)
  1167. return -EINVAL;
  1168. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1169. index, out_args);
  1170. }
  1171. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1172. /**
  1173. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1174. * @np: pointer to a device tree node containing a list
  1175. * @list_name: property name that contains a list
  1176. * @cells_name: property name that specifies phandles' arguments count
  1177. *
  1178. * Returns the number of phandle + argument tuples within a property. It
  1179. * is a typical pattern to encode a list of phandle and variable
  1180. * arguments into a single property. The number of arguments is encoded
  1181. * by a property in the phandle-target node. For example, a gpios
  1182. * property would contain a list of GPIO specifies consisting of a
  1183. * phandle and 1 or more arguments. The number of arguments are
  1184. * determined by the #gpio-cells property in the node pointed to by the
  1185. * phandle.
  1186. */
  1187. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1188. const char *cells_name)
  1189. {
  1190. struct of_phandle_iterator it;
  1191. int rc, cur_index = 0;
  1192. rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
  1193. if (rc)
  1194. return rc;
  1195. while ((rc = of_phandle_iterator_next(&it)) == 0)
  1196. cur_index += 1;
  1197. if (rc != -ENOENT)
  1198. return rc;
  1199. return cur_index;
  1200. }
  1201. EXPORT_SYMBOL(of_count_phandle_with_args);
  1202. /**
  1203. * __of_add_property - Add a property to a node without lock operations
  1204. */
  1205. int __of_add_property(struct device_node *np, struct property *prop)
  1206. {
  1207. struct property **next;
  1208. prop->next = NULL;
  1209. next = &np->properties;
  1210. while (*next) {
  1211. if (strcmp(prop->name, (*next)->name) == 0)
  1212. /* duplicate ! don't insert it */
  1213. return -EEXIST;
  1214. next = &(*next)->next;
  1215. }
  1216. *next = prop;
  1217. return 0;
  1218. }
  1219. /**
  1220. * of_add_property - Add a property to a node
  1221. */
  1222. int of_add_property(struct device_node *np, struct property *prop)
  1223. {
  1224. unsigned long flags;
  1225. int rc;
  1226. mutex_lock(&of_mutex);
  1227. raw_spin_lock_irqsave(&devtree_lock, flags);
  1228. rc = __of_add_property(np, prop);
  1229. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1230. if (!rc)
  1231. __of_add_property_sysfs(np, prop);
  1232. mutex_unlock(&of_mutex);
  1233. if (!rc)
  1234. of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
  1235. return rc;
  1236. }
  1237. int __of_remove_property(struct device_node *np, struct property *prop)
  1238. {
  1239. struct property **next;
  1240. for (next = &np->properties; *next; next = &(*next)->next) {
  1241. if (*next == prop)
  1242. break;
  1243. }
  1244. if (*next == NULL)
  1245. return -ENODEV;
  1246. /* found the node */
  1247. *next = prop->next;
  1248. prop->next = np->deadprops;
  1249. np->deadprops = prop;
  1250. return 0;
  1251. }
  1252. /**
  1253. * of_remove_property - Remove a property from a node.
  1254. *
  1255. * Note that we don't actually remove it, since we have given out
  1256. * who-knows-how-many pointers to the data using get-property.
  1257. * Instead we just move the property to the "dead properties"
  1258. * list, so it won't be found any more.
  1259. */
  1260. int of_remove_property(struct device_node *np, struct property *prop)
  1261. {
  1262. unsigned long flags;
  1263. int rc;
  1264. if (!prop)
  1265. return -ENODEV;
  1266. mutex_lock(&of_mutex);
  1267. raw_spin_lock_irqsave(&devtree_lock, flags);
  1268. rc = __of_remove_property(np, prop);
  1269. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1270. if (!rc)
  1271. __of_remove_property_sysfs(np, prop);
  1272. mutex_unlock(&of_mutex);
  1273. if (!rc)
  1274. of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
  1275. return rc;
  1276. }
  1277. int __of_update_property(struct device_node *np, struct property *newprop,
  1278. struct property **oldpropp)
  1279. {
  1280. struct property **next, *oldprop;
  1281. for (next = &np->properties; *next; next = &(*next)->next) {
  1282. if (of_prop_cmp((*next)->name, newprop->name) == 0)
  1283. break;
  1284. }
  1285. *oldpropp = oldprop = *next;
  1286. if (oldprop) {
  1287. /* replace the node */
  1288. newprop->next = oldprop->next;
  1289. *next = newprop;
  1290. oldprop->next = np->deadprops;
  1291. np->deadprops = oldprop;
  1292. } else {
  1293. /* new node */
  1294. newprop->next = NULL;
  1295. *next = newprop;
  1296. }
  1297. return 0;
  1298. }
  1299. /*
  1300. * of_update_property - Update a property in a node, if the property does
  1301. * not exist, add it.
  1302. *
  1303. * Note that we don't actually remove it, since we have given out
  1304. * who-knows-how-many pointers to the data using get-property.
  1305. * Instead we just move the property to the "dead properties" list,
  1306. * and add the new property to the property list
  1307. */
  1308. int of_update_property(struct device_node *np, struct property *newprop)
  1309. {
  1310. struct property *oldprop;
  1311. unsigned long flags;
  1312. int rc;
  1313. if (!newprop->name)
  1314. return -EINVAL;
  1315. mutex_lock(&of_mutex);
  1316. raw_spin_lock_irqsave(&devtree_lock, flags);
  1317. rc = __of_update_property(np, newprop, &oldprop);
  1318. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1319. if (!rc)
  1320. __of_update_property_sysfs(np, newprop, oldprop);
  1321. mutex_unlock(&of_mutex);
  1322. if (!rc)
  1323. of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
  1324. return rc;
  1325. }
  1326. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1327. int id, const char *stem, int stem_len)
  1328. {
  1329. ap->np = np;
  1330. ap->id = id;
  1331. strncpy(ap->stem, stem, stem_len);
  1332. ap->stem[stem_len] = 0;
  1333. list_add_tail(&ap->link, &aliases_lookup);
  1334. pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
  1335. ap->alias, ap->stem, ap->id, np);
  1336. }
  1337. /**
  1338. * of_alias_scan - Scan all properties of the 'aliases' node
  1339. *
  1340. * The function scans all the properties of the 'aliases' node and populates
  1341. * the global lookup table with the properties. It returns the
  1342. * number of alias properties found, or an error code in case of failure.
  1343. *
  1344. * @dt_alloc: An allocator that provides a virtual address to memory
  1345. * for storing the resulting tree
  1346. */
  1347. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1348. {
  1349. struct property *pp;
  1350. of_aliases = of_find_node_by_path("/aliases");
  1351. of_chosen = of_find_node_by_path("/chosen");
  1352. if (of_chosen == NULL)
  1353. of_chosen = of_find_node_by_path("/chosen@0");
  1354. if (of_chosen) {
  1355. /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
  1356. const char *name = NULL;
  1357. if (of_property_read_string(of_chosen, "stdout-path", &name))
  1358. of_property_read_string(of_chosen, "linux,stdout-path",
  1359. &name);
  1360. if (IS_ENABLED(CONFIG_PPC) && !name)
  1361. of_property_read_string(of_aliases, "stdout", &name);
  1362. if (name)
  1363. of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
  1364. }
  1365. if (!of_aliases)
  1366. return;
  1367. for_each_property_of_node(of_aliases, pp) {
  1368. const char *start = pp->name;
  1369. const char *end = start + strlen(start);
  1370. struct device_node *np;
  1371. struct alias_prop *ap;
  1372. int id, len;
  1373. /* Skip those we do not want to proceed */
  1374. if (!strcmp(pp->name, "name") ||
  1375. !strcmp(pp->name, "phandle") ||
  1376. !strcmp(pp->name, "linux,phandle"))
  1377. continue;
  1378. np = of_find_node_by_path(pp->value);
  1379. if (!np)
  1380. continue;
  1381. /* walk the alias backwards to extract the id and work out
  1382. * the 'stem' string */
  1383. while (isdigit(*(end-1)) && end > start)
  1384. end--;
  1385. len = end - start;
  1386. if (kstrtoint(end, 10, &id) < 0)
  1387. continue;
  1388. /* Allocate an alias_prop with enough space for the stem */
  1389. ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
  1390. if (!ap)
  1391. continue;
  1392. memset(ap, 0, sizeof(*ap) + len + 1);
  1393. ap->alias = start;
  1394. of_alias_add(ap, np, id, start, len);
  1395. }
  1396. }
  1397. /**
  1398. * of_alias_get_id - Get alias id for the given device_node
  1399. * @np: Pointer to the given device_node
  1400. * @stem: Alias stem of the given device_node
  1401. *
  1402. * The function travels the lookup table to get the alias id for the given
  1403. * device_node and alias stem. It returns the alias id if found.
  1404. */
  1405. int of_alias_get_id(struct device_node *np, const char *stem)
  1406. {
  1407. struct alias_prop *app;
  1408. int id = -ENODEV;
  1409. mutex_lock(&of_mutex);
  1410. list_for_each_entry(app, &aliases_lookup, link) {
  1411. if (strcmp(app->stem, stem) != 0)
  1412. continue;
  1413. if (np == app->np) {
  1414. id = app->id;
  1415. break;
  1416. }
  1417. }
  1418. mutex_unlock(&of_mutex);
  1419. return id;
  1420. }
  1421. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1422. /**
  1423. * of_alias_get_highest_id - Get highest alias id for the given stem
  1424. * @stem: Alias stem to be examined
  1425. *
  1426. * The function travels the lookup table to get the highest alias id for the
  1427. * given alias stem. It returns the alias id if found.
  1428. */
  1429. int of_alias_get_highest_id(const char *stem)
  1430. {
  1431. struct alias_prop *app;
  1432. int id = -ENODEV;
  1433. mutex_lock(&of_mutex);
  1434. list_for_each_entry(app, &aliases_lookup, link) {
  1435. if (strcmp(app->stem, stem) != 0)
  1436. continue;
  1437. if (app->id > id)
  1438. id = app->id;
  1439. }
  1440. mutex_unlock(&of_mutex);
  1441. return id;
  1442. }
  1443. EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
  1444. /**
  1445. * of_console_check() - Test and setup console for DT setup
  1446. * @dn - Pointer to device node
  1447. * @name - Name to use for preferred console without index. ex. "ttyS"
  1448. * @index - Index to use for preferred console.
  1449. *
  1450. * Check if the given device node matches the stdout-path property in the
  1451. * /chosen node. If it does then register it as the preferred console and return
  1452. * TRUE. Otherwise return FALSE.
  1453. */
  1454. bool of_console_check(struct device_node *dn, char *name, int index)
  1455. {
  1456. if (!dn || dn != of_stdout || console_set_on_cmdline)
  1457. return false;
  1458. /*
  1459. * XXX: cast `options' to char pointer to suppress complication
  1460. * warnings: printk, UART and console drivers expect char pointer.
  1461. */
  1462. return !add_preferred_console(name, index, (char *)of_stdout_options);
  1463. }
  1464. EXPORT_SYMBOL_GPL(of_console_check);
  1465. /**
  1466. * of_find_next_cache_node - Find a node's subsidiary cache
  1467. * @np: node of type "cpu" or "cache"
  1468. *
  1469. * Returns a node pointer with refcount incremented, use
  1470. * of_node_put() on it when done. Caller should hold a reference
  1471. * to np.
  1472. */
  1473. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1474. {
  1475. struct device_node *child, *cache_node;
  1476. cache_node = of_parse_phandle(np, "l2-cache", 0);
  1477. if (!cache_node)
  1478. cache_node = of_parse_phandle(np, "next-level-cache", 0);
  1479. if (cache_node)
  1480. return cache_node;
  1481. /* OF on pmac has nodes instead of properties named "l2-cache"
  1482. * beneath CPU nodes.
  1483. */
  1484. if (!strcmp(np->type, "cpu"))
  1485. for_each_child_of_node(np, child)
  1486. if (!strcmp(child->type, "cache"))
  1487. return child;
  1488. return NULL;
  1489. }
  1490. /**
  1491. * of_find_last_cache_level - Find the level at which the last cache is
  1492. * present for the given logical cpu
  1493. *
  1494. * @cpu: cpu number(logical index) for which the last cache level is needed
  1495. *
  1496. * Returns the the level at which the last cache is present. It is exactly
  1497. * same as the total number of cache levels for the given logical cpu.
  1498. */
  1499. int of_find_last_cache_level(unsigned int cpu)
  1500. {
  1501. u32 cache_level = 0;
  1502. struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
  1503. while (np) {
  1504. prev = np;
  1505. of_node_put(np);
  1506. np = of_find_next_cache_node(np);
  1507. }
  1508. of_property_read_u32(prev, "cache-level", &cache_level);
  1509. return cache_level;
  1510. }