base.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
  13. * Grant Likely.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #define pr_fmt(fmt) "OF: " fmt
  21. #include <linux/console.h>
  22. #include <linux/ctype.h>
  23. #include <linux/cpu.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_graph.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/proc_fs.h>
  32. #include "of_private.h"
  33. LIST_HEAD(aliases_lookup);
  34. struct device_node *of_root;
  35. EXPORT_SYMBOL(of_root);
  36. struct device_node *of_chosen;
  37. struct device_node *of_aliases;
  38. struct device_node *of_stdout;
  39. static const char *of_stdout_options;
  40. struct kset *of_kset;
  41. /*
  42. * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
  43. * This mutex must be held whenever modifications are being made to the
  44. * device tree. The of_{attach,detach}_node() and
  45. * of_{add,remove,update}_property() helpers make sure this happens.
  46. */
  47. DEFINE_MUTEX(of_mutex);
  48. /* use when traversing tree through the child, sibling,
  49. * or parent members of struct device_node.
  50. */
  51. DEFINE_RAW_SPINLOCK(devtree_lock);
  52. int of_n_addr_cells(struct device_node *np)
  53. {
  54. const __be32 *ip;
  55. do {
  56. if (np->parent)
  57. np = np->parent;
  58. ip = of_get_property(np, "#address-cells", NULL);
  59. if (ip)
  60. return be32_to_cpup(ip);
  61. } while (np->parent);
  62. /* No #address-cells property for the root node */
  63. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  64. }
  65. EXPORT_SYMBOL(of_n_addr_cells);
  66. int of_n_size_cells(struct device_node *np)
  67. {
  68. const __be32 *ip;
  69. do {
  70. if (np->parent)
  71. np = np->parent;
  72. ip = of_get_property(np, "#size-cells", NULL);
  73. if (ip)
  74. return be32_to_cpup(ip);
  75. } while (np->parent);
  76. /* No #size-cells property for the root node */
  77. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  78. }
  79. EXPORT_SYMBOL(of_n_size_cells);
  80. #ifdef CONFIG_NUMA
  81. int __weak of_node_to_nid(struct device_node *np)
  82. {
  83. return NUMA_NO_NODE;
  84. }
  85. #endif
  86. #ifndef CONFIG_OF_DYNAMIC
  87. static void of_node_release(struct kobject *kobj)
  88. {
  89. /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
  90. }
  91. #endif /* CONFIG_OF_DYNAMIC */
  92. struct kobj_type of_node_ktype = {
  93. .release = of_node_release,
  94. };
  95. static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
  96. struct bin_attribute *bin_attr, char *buf,
  97. loff_t offset, size_t count)
  98. {
  99. struct property *pp = container_of(bin_attr, struct property, attr);
  100. return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
  101. }
  102. /* always return newly allocated name, caller must free after use */
  103. static const char *safe_name(struct kobject *kobj, const char *orig_name)
  104. {
  105. const char *name = orig_name;
  106. struct kernfs_node *kn;
  107. int i = 0;
  108. /* don't be a hero. After 16 tries give up */
  109. while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
  110. sysfs_put(kn);
  111. if (name != orig_name)
  112. kfree(name);
  113. name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
  114. }
  115. if (name == orig_name) {
  116. name = kstrdup(orig_name, GFP_KERNEL);
  117. } else {
  118. pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
  119. kobject_name(kobj), name);
  120. }
  121. return name;
  122. }
  123. int __of_add_property_sysfs(struct device_node *np, struct property *pp)
  124. {
  125. int rc;
  126. /* Important: Don't leak passwords */
  127. bool secure = strncmp(pp->name, "security-", 9) == 0;
  128. if (!IS_ENABLED(CONFIG_SYSFS))
  129. return 0;
  130. if (!of_kset || !of_node_is_attached(np))
  131. return 0;
  132. sysfs_bin_attr_init(&pp->attr);
  133. pp->attr.attr.name = safe_name(&np->kobj, pp->name);
  134. pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
  135. pp->attr.size = secure ? 0 : pp->length;
  136. pp->attr.read = of_node_property_read;
  137. rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
  138. WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
  139. return rc;
  140. }
  141. int __of_attach_node_sysfs(struct device_node *np)
  142. {
  143. const char *name;
  144. struct kobject *parent;
  145. struct property *pp;
  146. int rc;
  147. if (!IS_ENABLED(CONFIG_SYSFS))
  148. return 0;
  149. if (!of_kset)
  150. return 0;
  151. np->kobj.kset = of_kset;
  152. if (!np->parent) {
  153. /* Nodes without parents are new top level trees */
  154. name = safe_name(&of_kset->kobj, "base");
  155. parent = NULL;
  156. } else {
  157. name = safe_name(&np->parent->kobj, kbasename(np->full_name));
  158. parent = &np->parent->kobj;
  159. }
  160. if (!name)
  161. return -ENOMEM;
  162. rc = kobject_add(&np->kobj, parent, "%s", name);
  163. kfree(name);
  164. if (rc)
  165. return rc;
  166. for_each_property_of_node(np, pp)
  167. __of_add_property_sysfs(np, pp);
  168. return 0;
  169. }
  170. void __init of_core_init(void)
  171. {
  172. struct device_node *np;
  173. /* Create the kset, and register existing nodes */
  174. mutex_lock(&of_mutex);
  175. of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
  176. if (!of_kset) {
  177. mutex_unlock(&of_mutex);
  178. pr_err("failed to register existing nodes\n");
  179. return;
  180. }
  181. for_each_of_allnodes(np)
  182. __of_attach_node_sysfs(np);
  183. mutex_unlock(&of_mutex);
  184. /* Symlink in /proc as required by userspace ABI */
  185. if (of_root)
  186. proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
  187. }
  188. static struct property *__of_find_property(const struct device_node *np,
  189. const char *name, int *lenp)
  190. {
  191. struct property *pp;
  192. if (!np)
  193. return NULL;
  194. for (pp = np->properties; pp; pp = pp->next) {
  195. if (of_prop_cmp(pp->name, name) == 0) {
  196. if (lenp)
  197. *lenp = pp->length;
  198. break;
  199. }
  200. }
  201. return pp;
  202. }
  203. struct property *of_find_property(const struct device_node *np,
  204. const char *name,
  205. int *lenp)
  206. {
  207. struct property *pp;
  208. unsigned long flags;
  209. raw_spin_lock_irqsave(&devtree_lock, flags);
  210. pp = __of_find_property(np, name, lenp);
  211. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  212. return pp;
  213. }
  214. EXPORT_SYMBOL(of_find_property);
  215. struct device_node *__of_find_all_nodes(struct device_node *prev)
  216. {
  217. struct device_node *np;
  218. if (!prev) {
  219. np = of_root;
  220. } else if (prev->child) {
  221. np = prev->child;
  222. } else {
  223. /* Walk back up looking for a sibling, or the end of the structure */
  224. np = prev;
  225. while (np->parent && !np->sibling)
  226. np = np->parent;
  227. np = np->sibling; /* Might be null at the end of the tree */
  228. }
  229. return np;
  230. }
  231. /**
  232. * of_find_all_nodes - Get next node in global list
  233. * @prev: Previous node or NULL to start iteration
  234. * of_node_put() will be called on it
  235. *
  236. * Returns a node pointer with refcount incremented, use
  237. * of_node_put() on it when done.
  238. */
  239. struct device_node *of_find_all_nodes(struct device_node *prev)
  240. {
  241. struct device_node *np;
  242. unsigned long flags;
  243. raw_spin_lock_irqsave(&devtree_lock, flags);
  244. np = __of_find_all_nodes(prev);
  245. of_node_get(np);
  246. of_node_put(prev);
  247. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  248. return np;
  249. }
  250. EXPORT_SYMBOL(of_find_all_nodes);
  251. /*
  252. * Find a property with a given name for a given node
  253. * and return the value.
  254. */
  255. const void *__of_get_property(const struct device_node *np,
  256. const char *name, int *lenp)
  257. {
  258. struct property *pp = __of_find_property(np, name, lenp);
  259. return pp ? pp->value : NULL;
  260. }
  261. /*
  262. * Find a property with a given name for a given node
  263. * and return the value.
  264. */
  265. const void *of_get_property(const struct device_node *np, const char *name,
  266. int *lenp)
  267. {
  268. struct property *pp = of_find_property(np, name, lenp);
  269. return pp ? pp->value : NULL;
  270. }
  271. EXPORT_SYMBOL(of_get_property);
  272. /*
  273. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  274. *
  275. * @cpu: logical cpu index of a core/thread
  276. * @phys_id: physical identifier of a core/thread
  277. *
  278. * CPU logical to physical index mapping is architecture specific.
  279. * However this __weak function provides a default match of physical
  280. * id to logical cpu index. phys_id provided here is usually values read
  281. * from the device tree which must match the hardware internal registers.
  282. *
  283. * Returns true if the physical identifier and the logical cpu index
  284. * correspond to the same core/thread, false otherwise.
  285. */
  286. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  287. {
  288. return (u32)phys_id == cpu;
  289. }
  290. /**
  291. * Checks if the given "prop_name" property holds the physical id of the
  292. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  293. * NULL, local thread number within the core is returned in it.
  294. */
  295. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  296. const char *prop_name, int cpu, unsigned int *thread)
  297. {
  298. const __be32 *cell;
  299. int ac, prop_len, tid;
  300. u64 hwid;
  301. ac = of_n_addr_cells(cpun);
  302. cell = of_get_property(cpun, prop_name, &prop_len);
  303. if (!cell || !ac)
  304. return false;
  305. prop_len /= sizeof(*cell) * ac;
  306. for (tid = 0; tid < prop_len; tid++) {
  307. hwid = of_read_number(cell, ac);
  308. if (arch_match_cpu_phys_id(cpu, hwid)) {
  309. if (thread)
  310. *thread = tid;
  311. return true;
  312. }
  313. cell += ac;
  314. }
  315. return false;
  316. }
  317. /*
  318. * arch_find_n_match_cpu_physical_id - See if the given device node is
  319. * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
  320. * else false. If 'thread' is non-NULL, the local thread number within the
  321. * core is returned in it.
  322. */
  323. bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  324. int cpu, unsigned int *thread)
  325. {
  326. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  327. * for thread ids on PowerPC. If it doesn't exist fallback to
  328. * standard "reg" property.
  329. */
  330. if (IS_ENABLED(CONFIG_PPC) &&
  331. __of_find_n_match_cpu_property(cpun,
  332. "ibm,ppc-interrupt-server#s",
  333. cpu, thread))
  334. return true;
  335. return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
  336. }
  337. /**
  338. * of_get_cpu_node - Get device node associated with the given logical CPU
  339. *
  340. * @cpu: CPU number(logical index) for which device node is required
  341. * @thread: if not NULL, local thread number within the physical core is
  342. * returned
  343. *
  344. * The main purpose of this function is to retrieve the device node for the
  345. * given logical CPU index. It should be used to initialize the of_node in
  346. * cpu device. Once of_node in cpu device is populated, all the further
  347. * references can use that instead.
  348. *
  349. * CPU logical to physical index mapping is architecture specific and is built
  350. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  351. * which can be overridden by architecture specific implementation.
  352. *
  353. * Returns a node pointer for the logical cpu with refcount incremented, use
  354. * of_node_put() on it when done. Returns NULL if not found.
  355. */
  356. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  357. {
  358. struct device_node *cpun;
  359. for_each_node_by_type(cpun, "cpu") {
  360. if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
  361. return cpun;
  362. }
  363. return NULL;
  364. }
  365. EXPORT_SYMBOL(of_get_cpu_node);
  366. /**
  367. * __of_device_is_compatible() - Check if the node matches given constraints
  368. * @device: pointer to node
  369. * @compat: required compatible string, NULL or "" for any match
  370. * @type: required device_type value, NULL or "" for any match
  371. * @name: required node name, NULL or "" for any match
  372. *
  373. * Checks if the given @compat, @type and @name strings match the
  374. * properties of the given @device. A constraints can be skipped by
  375. * passing NULL or an empty string as the constraint.
  376. *
  377. * Returns 0 for no match, and a positive integer on match. The return
  378. * value is a relative score with larger values indicating better
  379. * matches. The score is weighted for the most specific compatible value
  380. * to get the highest score. Matching type is next, followed by matching
  381. * name. Practically speaking, this results in the following priority
  382. * order for matches:
  383. *
  384. * 1. specific compatible && type && name
  385. * 2. specific compatible && type
  386. * 3. specific compatible && name
  387. * 4. specific compatible
  388. * 5. general compatible && type && name
  389. * 6. general compatible && type
  390. * 7. general compatible && name
  391. * 8. general compatible
  392. * 9. type && name
  393. * 10. type
  394. * 11. name
  395. */
  396. static int __of_device_is_compatible(const struct device_node *device,
  397. const char *compat, const char *type, const char *name)
  398. {
  399. struct property *prop;
  400. const char *cp;
  401. int index = 0, score = 0;
  402. /* Compatible match has highest priority */
  403. if (compat && compat[0]) {
  404. prop = __of_find_property(device, "compatible", NULL);
  405. for (cp = of_prop_next_string(prop, NULL); cp;
  406. cp = of_prop_next_string(prop, cp), index++) {
  407. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  408. score = INT_MAX/2 - (index << 2);
  409. break;
  410. }
  411. }
  412. if (!score)
  413. return 0;
  414. }
  415. /* Matching type is better than matching name */
  416. if (type && type[0]) {
  417. if (!device->type || of_node_cmp(type, device->type))
  418. return 0;
  419. score += 2;
  420. }
  421. /* Matching name is a bit better than not */
  422. if (name && name[0]) {
  423. if (!device->name || of_node_cmp(name, device->name))
  424. return 0;
  425. score++;
  426. }
  427. return score;
  428. }
  429. /** Checks if the given "compat" string matches one of the strings in
  430. * the device's "compatible" property
  431. */
  432. int of_device_is_compatible(const struct device_node *device,
  433. const char *compat)
  434. {
  435. unsigned long flags;
  436. int res;
  437. raw_spin_lock_irqsave(&devtree_lock, flags);
  438. res = __of_device_is_compatible(device, compat, NULL, NULL);
  439. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  440. return res;
  441. }
  442. EXPORT_SYMBOL(of_device_is_compatible);
  443. /** Checks if the device is compatible with any of the entries in
  444. * a NULL terminated array of strings. Returns the best match
  445. * score or 0.
  446. */
  447. int of_device_compatible_match(struct device_node *device,
  448. const char *const *compat)
  449. {
  450. unsigned int tmp, score = 0;
  451. if (!compat)
  452. return 0;
  453. while (*compat) {
  454. tmp = of_device_is_compatible(device, *compat);
  455. if (tmp > score)
  456. score = tmp;
  457. compat++;
  458. }
  459. return score;
  460. }
  461. /**
  462. * of_machine_is_compatible - Test root of device tree for a given compatible value
  463. * @compat: compatible string to look for in root node's compatible property.
  464. *
  465. * Returns a positive integer if the root node has the given value in its
  466. * compatible property.
  467. */
  468. int of_machine_is_compatible(const char *compat)
  469. {
  470. struct device_node *root;
  471. int rc = 0;
  472. root = of_find_node_by_path("/");
  473. if (root) {
  474. rc = of_device_is_compatible(root, compat);
  475. of_node_put(root);
  476. }
  477. return rc;
  478. }
  479. EXPORT_SYMBOL(of_machine_is_compatible);
  480. /**
  481. * __of_device_is_available - check if a device is available for use
  482. *
  483. * @device: Node to check for availability, with locks already held
  484. *
  485. * Returns true if the status property is absent or set to "okay" or "ok",
  486. * false otherwise
  487. */
  488. static bool __of_device_is_available(const struct device_node *device)
  489. {
  490. const char *status;
  491. int statlen;
  492. if (!device)
  493. return false;
  494. status = __of_get_property(device, "status", &statlen);
  495. if (status == NULL)
  496. return true;
  497. if (statlen > 0) {
  498. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  499. return true;
  500. }
  501. return false;
  502. }
  503. /**
  504. * of_device_is_available - check if a device is available for use
  505. *
  506. * @device: Node to check for availability
  507. *
  508. * Returns true if the status property is absent or set to "okay" or "ok",
  509. * false otherwise
  510. */
  511. bool of_device_is_available(const struct device_node *device)
  512. {
  513. unsigned long flags;
  514. bool res;
  515. raw_spin_lock_irqsave(&devtree_lock, flags);
  516. res = __of_device_is_available(device);
  517. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  518. return res;
  519. }
  520. EXPORT_SYMBOL(of_device_is_available);
  521. /**
  522. * of_device_is_big_endian - check if a device has BE registers
  523. *
  524. * @device: Node to check for endianness
  525. *
  526. * Returns true if the device has a "big-endian" property, or if the kernel
  527. * was compiled for BE *and* the device has a "native-endian" property.
  528. * Returns false otherwise.
  529. *
  530. * Callers would nominally use ioread32be/iowrite32be if
  531. * of_device_is_big_endian() == true, or readl/writel otherwise.
  532. */
  533. bool of_device_is_big_endian(const struct device_node *device)
  534. {
  535. if (of_property_read_bool(device, "big-endian"))
  536. return true;
  537. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  538. of_property_read_bool(device, "native-endian"))
  539. return true;
  540. return false;
  541. }
  542. EXPORT_SYMBOL(of_device_is_big_endian);
  543. /**
  544. * of_get_parent - Get a node's parent if any
  545. * @node: Node to get parent
  546. *
  547. * Returns a node pointer with refcount incremented, use
  548. * of_node_put() on it when done.
  549. */
  550. struct device_node *of_get_parent(const struct device_node *node)
  551. {
  552. struct device_node *np;
  553. unsigned long flags;
  554. if (!node)
  555. return NULL;
  556. raw_spin_lock_irqsave(&devtree_lock, flags);
  557. np = of_node_get(node->parent);
  558. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  559. return np;
  560. }
  561. EXPORT_SYMBOL(of_get_parent);
  562. /**
  563. * of_get_next_parent - Iterate to a node's parent
  564. * @node: Node to get parent of
  565. *
  566. * This is like of_get_parent() except that it drops the
  567. * refcount on the passed node, making it suitable for iterating
  568. * through a node's parents.
  569. *
  570. * Returns a node pointer with refcount incremented, use
  571. * of_node_put() on it when done.
  572. */
  573. struct device_node *of_get_next_parent(struct device_node *node)
  574. {
  575. struct device_node *parent;
  576. unsigned long flags;
  577. if (!node)
  578. return NULL;
  579. raw_spin_lock_irqsave(&devtree_lock, flags);
  580. parent = of_node_get(node->parent);
  581. of_node_put(node);
  582. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  583. return parent;
  584. }
  585. EXPORT_SYMBOL(of_get_next_parent);
  586. static struct device_node *__of_get_next_child(const struct device_node *node,
  587. struct device_node *prev)
  588. {
  589. struct device_node *next;
  590. if (!node)
  591. return NULL;
  592. next = prev ? prev->sibling : node->child;
  593. for (; next; next = next->sibling)
  594. if (of_node_get(next))
  595. break;
  596. of_node_put(prev);
  597. return next;
  598. }
  599. #define __for_each_child_of_node(parent, child) \
  600. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  601. child = __of_get_next_child(parent, child))
  602. /**
  603. * of_get_next_child - Iterate a node childs
  604. * @node: parent node
  605. * @prev: previous child of the parent node, or NULL to get first
  606. *
  607. * Returns a node pointer with refcount incremented, use of_node_put() on
  608. * it when done. Returns NULL when prev is the last child. Decrements the
  609. * refcount of prev.
  610. */
  611. struct device_node *of_get_next_child(const struct device_node *node,
  612. struct device_node *prev)
  613. {
  614. struct device_node *next;
  615. unsigned long flags;
  616. raw_spin_lock_irqsave(&devtree_lock, flags);
  617. next = __of_get_next_child(node, prev);
  618. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  619. return next;
  620. }
  621. EXPORT_SYMBOL(of_get_next_child);
  622. /**
  623. * of_get_next_available_child - Find the next available child node
  624. * @node: parent node
  625. * @prev: previous child of the parent node, or NULL to get first
  626. *
  627. * This function is like of_get_next_child(), except that it
  628. * automatically skips any disabled nodes (i.e. status = "disabled").
  629. */
  630. struct device_node *of_get_next_available_child(const struct device_node *node,
  631. struct device_node *prev)
  632. {
  633. struct device_node *next;
  634. unsigned long flags;
  635. if (!node)
  636. return NULL;
  637. raw_spin_lock_irqsave(&devtree_lock, flags);
  638. next = prev ? prev->sibling : node->child;
  639. for (; next; next = next->sibling) {
  640. if (!__of_device_is_available(next))
  641. continue;
  642. if (of_node_get(next))
  643. break;
  644. }
  645. of_node_put(prev);
  646. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  647. return next;
  648. }
  649. EXPORT_SYMBOL(of_get_next_available_child);
  650. /**
  651. * of_get_child_by_name - Find the child node by name for a given parent
  652. * @node: parent node
  653. * @name: child name to look for.
  654. *
  655. * This function looks for child node for given matching name
  656. *
  657. * Returns a node pointer if found, with refcount incremented, use
  658. * of_node_put() on it when done.
  659. * Returns NULL if node is not found.
  660. */
  661. struct device_node *of_get_child_by_name(const struct device_node *node,
  662. const char *name)
  663. {
  664. struct device_node *child;
  665. for_each_child_of_node(node, child)
  666. if (child->name && (of_node_cmp(child->name, name) == 0))
  667. break;
  668. return child;
  669. }
  670. EXPORT_SYMBOL(of_get_child_by_name);
  671. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  672. const char *path)
  673. {
  674. struct device_node *child;
  675. int len;
  676. len = strcspn(path, "/:");
  677. if (!len)
  678. return NULL;
  679. __for_each_child_of_node(parent, child) {
  680. const char *name = strrchr(child->full_name, '/');
  681. if (WARN(!name, "malformed device_node %s\n", child->full_name))
  682. continue;
  683. name++;
  684. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  685. return child;
  686. }
  687. return NULL;
  688. }
  689. /**
  690. * of_find_node_opts_by_path - Find a node matching a full OF path
  691. * @path: Either the full path to match, or if the path does not
  692. * start with '/', the name of a property of the /aliases
  693. * node (an alias). In the case of an alias, the node
  694. * matching the alias' value will be returned.
  695. * @opts: Address of a pointer into which to store the start of
  696. * an options string appended to the end of the path with
  697. * a ':' separator.
  698. *
  699. * Valid paths:
  700. * /foo/bar Full path
  701. * foo Valid alias
  702. * foo/bar Valid alias + relative path
  703. *
  704. * Returns a node pointer with refcount incremented, use
  705. * of_node_put() on it when done.
  706. */
  707. struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
  708. {
  709. struct device_node *np = NULL;
  710. struct property *pp;
  711. unsigned long flags;
  712. const char *separator = strchr(path, ':');
  713. if (opts)
  714. *opts = separator ? separator + 1 : NULL;
  715. if (strcmp(path, "/") == 0)
  716. return of_node_get(of_root);
  717. /* The path could begin with an alias */
  718. if (*path != '/') {
  719. int len;
  720. const char *p = separator;
  721. if (!p)
  722. p = strchrnul(path, '/');
  723. len = p - path;
  724. /* of_aliases must not be NULL */
  725. if (!of_aliases)
  726. return NULL;
  727. for_each_property_of_node(of_aliases, pp) {
  728. if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
  729. np = of_find_node_by_path(pp->value);
  730. break;
  731. }
  732. }
  733. if (!np)
  734. return NULL;
  735. path = p;
  736. }
  737. /* Step down the tree matching path components */
  738. raw_spin_lock_irqsave(&devtree_lock, flags);
  739. if (!np)
  740. np = of_node_get(of_root);
  741. while (np && *path == '/') {
  742. struct device_node *tmp = np;
  743. path++; /* Increment past '/' delimiter */
  744. np = __of_find_node_by_path(np, path);
  745. of_node_put(tmp);
  746. path = strchrnul(path, '/');
  747. if (separator && separator < path)
  748. break;
  749. }
  750. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  751. return np;
  752. }
  753. EXPORT_SYMBOL(of_find_node_opts_by_path);
  754. /**
  755. * of_find_node_by_name - Find a node by its "name" property
  756. * @from: The node to start searching from or NULL, the node
  757. * you pass will not be searched, only the next one
  758. * will; typically, you pass what the previous call
  759. * returned. of_node_put() will be called on it
  760. * @name: The name string to match against
  761. *
  762. * Returns a node pointer with refcount incremented, use
  763. * of_node_put() on it when done.
  764. */
  765. struct device_node *of_find_node_by_name(struct device_node *from,
  766. const char *name)
  767. {
  768. struct device_node *np;
  769. unsigned long flags;
  770. raw_spin_lock_irqsave(&devtree_lock, flags);
  771. for_each_of_allnodes_from(from, np)
  772. if (np->name && (of_node_cmp(np->name, name) == 0)
  773. && of_node_get(np))
  774. break;
  775. of_node_put(from);
  776. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  777. return np;
  778. }
  779. EXPORT_SYMBOL(of_find_node_by_name);
  780. /**
  781. * of_find_node_by_type - Find a node by its "device_type" property
  782. * @from: The node to start searching from, or NULL to start searching
  783. * the entire device tree. The node you pass will not be
  784. * searched, only the next one will; typically, you pass
  785. * what the previous call returned. of_node_put() will be
  786. * called on from for you.
  787. * @type: The type string to match against
  788. *
  789. * Returns a node pointer with refcount incremented, use
  790. * of_node_put() on it when done.
  791. */
  792. struct device_node *of_find_node_by_type(struct device_node *from,
  793. const char *type)
  794. {
  795. struct device_node *np;
  796. unsigned long flags;
  797. raw_spin_lock_irqsave(&devtree_lock, flags);
  798. for_each_of_allnodes_from(from, np)
  799. if (np->type && (of_node_cmp(np->type, type) == 0)
  800. && of_node_get(np))
  801. break;
  802. of_node_put(from);
  803. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  804. return np;
  805. }
  806. EXPORT_SYMBOL(of_find_node_by_type);
  807. /**
  808. * of_find_compatible_node - Find a node based on type and one of the
  809. * tokens in its "compatible" property
  810. * @from: The node to start searching from or NULL, the node
  811. * you pass will not be searched, only the next one
  812. * will; typically, you pass what the previous call
  813. * returned. of_node_put() will be called on it
  814. * @type: The type string to match "device_type" or NULL to ignore
  815. * @compatible: The string to match to one of the tokens in the device
  816. * "compatible" list.
  817. *
  818. * Returns a node pointer with refcount incremented, use
  819. * of_node_put() on it when done.
  820. */
  821. struct device_node *of_find_compatible_node(struct device_node *from,
  822. const char *type, const char *compatible)
  823. {
  824. struct device_node *np;
  825. unsigned long flags;
  826. raw_spin_lock_irqsave(&devtree_lock, flags);
  827. for_each_of_allnodes_from(from, np)
  828. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  829. of_node_get(np))
  830. break;
  831. of_node_put(from);
  832. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  833. return np;
  834. }
  835. EXPORT_SYMBOL(of_find_compatible_node);
  836. /**
  837. * of_find_node_with_property - Find a node which has a property with
  838. * the given name.
  839. * @from: The node to start searching from or NULL, the node
  840. * you pass will not be searched, only the next one
  841. * will; typically, you pass what the previous call
  842. * returned. of_node_put() will be called on it
  843. * @prop_name: The name of the property to look for.
  844. *
  845. * Returns a node pointer with refcount incremented, use
  846. * of_node_put() on it when done.
  847. */
  848. struct device_node *of_find_node_with_property(struct device_node *from,
  849. const char *prop_name)
  850. {
  851. struct device_node *np;
  852. struct property *pp;
  853. unsigned long flags;
  854. raw_spin_lock_irqsave(&devtree_lock, flags);
  855. for_each_of_allnodes_from(from, np) {
  856. for (pp = np->properties; pp; pp = pp->next) {
  857. if (of_prop_cmp(pp->name, prop_name) == 0) {
  858. of_node_get(np);
  859. goto out;
  860. }
  861. }
  862. }
  863. out:
  864. of_node_put(from);
  865. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  866. return np;
  867. }
  868. EXPORT_SYMBOL(of_find_node_with_property);
  869. static
  870. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  871. const struct device_node *node)
  872. {
  873. const struct of_device_id *best_match = NULL;
  874. int score, best_score = 0;
  875. if (!matches)
  876. return NULL;
  877. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  878. score = __of_device_is_compatible(node, matches->compatible,
  879. matches->type, matches->name);
  880. if (score > best_score) {
  881. best_match = matches;
  882. best_score = score;
  883. }
  884. }
  885. return best_match;
  886. }
  887. /**
  888. * of_match_node - Tell if a device_node has a matching of_match structure
  889. * @matches: array of of device match structures to search in
  890. * @node: the of device structure to match against
  891. *
  892. * Low level utility function used by device matching.
  893. */
  894. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  895. const struct device_node *node)
  896. {
  897. const struct of_device_id *match;
  898. unsigned long flags;
  899. raw_spin_lock_irqsave(&devtree_lock, flags);
  900. match = __of_match_node(matches, node);
  901. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  902. return match;
  903. }
  904. EXPORT_SYMBOL(of_match_node);
  905. /**
  906. * of_find_matching_node_and_match - Find a node based on an of_device_id
  907. * match table.
  908. * @from: The node to start searching from or NULL, the node
  909. * you pass will not be searched, only the next one
  910. * will; typically, you pass what the previous call
  911. * returned. of_node_put() will be called on it
  912. * @matches: array of of device match structures to search in
  913. * @match Updated to point at the matches entry which matched
  914. *
  915. * Returns a node pointer with refcount incremented, use
  916. * of_node_put() on it when done.
  917. */
  918. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  919. const struct of_device_id *matches,
  920. const struct of_device_id **match)
  921. {
  922. struct device_node *np;
  923. const struct of_device_id *m;
  924. unsigned long flags;
  925. if (match)
  926. *match = NULL;
  927. raw_spin_lock_irqsave(&devtree_lock, flags);
  928. for_each_of_allnodes_from(from, np) {
  929. m = __of_match_node(matches, np);
  930. if (m && of_node_get(np)) {
  931. if (match)
  932. *match = m;
  933. break;
  934. }
  935. }
  936. of_node_put(from);
  937. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  938. return np;
  939. }
  940. EXPORT_SYMBOL(of_find_matching_node_and_match);
  941. /**
  942. * of_modalias_node - Lookup appropriate modalias for a device node
  943. * @node: pointer to a device tree node
  944. * @modalias: Pointer to buffer that modalias value will be copied into
  945. * @len: Length of modalias value
  946. *
  947. * Based on the value of the compatible property, this routine will attempt
  948. * to choose an appropriate modalias value for a particular device tree node.
  949. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  950. * from the first entry in the compatible list property.
  951. *
  952. * This routine returns 0 on success, <0 on failure.
  953. */
  954. int of_modalias_node(struct device_node *node, char *modalias, int len)
  955. {
  956. const char *compatible, *p;
  957. int cplen;
  958. compatible = of_get_property(node, "compatible", &cplen);
  959. if (!compatible || strlen(compatible) > cplen)
  960. return -ENODEV;
  961. p = strchr(compatible, ',');
  962. strlcpy(modalias, p ? p + 1 : compatible, len);
  963. return 0;
  964. }
  965. EXPORT_SYMBOL_GPL(of_modalias_node);
  966. /**
  967. * of_find_node_by_phandle - Find a node given a phandle
  968. * @handle: phandle of the node to find
  969. *
  970. * Returns a node pointer with refcount incremented, use
  971. * of_node_put() on it when done.
  972. */
  973. struct device_node *of_find_node_by_phandle(phandle handle)
  974. {
  975. struct device_node *np;
  976. unsigned long flags;
  977. if (!handle)
  978. return NULL;
  979. raw_spin_lock_irqsave(&devtree_lock, flags);
  980. for_each_of_allnodes(np)
  981. if (np->phandle == handle)
  982. break;
  983. of_node_get(np);
  984. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  985. return np;
  986. }
  987. EXPORT_SYMBOL(of_find_node_by_phandle);
  988. /**
  989. * of_property_count_elems_of_size - Count the number of elements in a property
  990. *
  991. * @np: device node from which the property value is to be read.
  992. * @propname: name of the property to be searched.
  993. * @elem_size: size of the individual element
  994. *
  995. * Search for a property in a device node and count the number of elements of
  996. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  997. * property does not exist or its length does not match a multiple of elem_size
  998. * and -ENODATA if the property does not have a value.
  999. */
  1000. int of_property_count_elems_of_size(const struct device_node *np,
  1001. const char *propname, int elem_size)
  1002. {
  1003. struct property *prop = of_find_property(np, propname, NULL);
  1004. if (!prop)
  1005. return -EINVAL;
  1006. if (!prop->value)
  1007. return -ENODATA;
  1008. if (prop->length % elem_size != 0) {
  1009. pr_err("size of %s in node %s is not a multiple of %d\n",
  1010. propname, np->full_name, elem_size);
  1011. return -EINVAL;
  1012. }
  1013. return prop->length / elem_size;
  1014. }
  1015. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  1016. /**
  1017. * of_find_property_value_of_size
  1018. *
  1019. * @np: device node from which the property value is to be read.
  1020. * @propname: name of the property to be searched.
  1021. * @min: minimum allowed length of property value
  1022. * @max: maximum allowed length of property value (0 means unlimited)
  1023. * @len: if !=NULL, actual length is written to here
  1024. *
  1025. * Search for a property in a device node and valid the requested size.
  1026. * Returns the property value on success, -EINVAL if the property does not
  1027. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  1028. * property data is too small or too large.
  1029. *
  1030. */
  1031. static void *of_find_property_value_of_size(const struct device_node *np,
  1032. const char *propname, u32 min, u32 max, size_t *len)
  1033. {
  1034. struct property *prop = of_find_property(np, propname, NULL);
  1035. if (!prop)
  1036. return ERR_PTR(-EINVAL);
  1037. if (!prop->value)
  1038. return ERR_PTR(-ENODATA);
  1039. if (prop->length < min)
  1040. return ERR_PTR(-EOVERFLOW);
  1041. if (max && prop->length > max)
  1042. return ERR_PTR(-EOVERFLOW);
  1043. if (len)
  1044. *len = prop->length;
  1045. return prop->value;
  1046. }
  1047. /**
  1048. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  1049. *
  1050. * @np: device node from which the property value is to be read.
  1051. * @propname: name of the property to be searched.
  1052. * @index: index of the u32 in the list of values
  1053. * @out_value: pointer to return value, modified only if no error.
  1054. *
  1055. * Search for a property in a device node and read nth 32-bit value from
  1056. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1057. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1058. * property data isn't large enough.
  1059. *
  1060. * The out_value is modified only if a valid u32 value can be decoded.
  1061. */
  1062. int of_property_read_u32_index(const struct device_node *np,
  1063. const char *propname,
  1064. u32 index, u32 *out_value)
  1065. {
  1066. const u32 *val = of_find_property_value_of_size(np, propname,
  1067. ((index + 1) * sizeof(*out_value)),
  1068. 0,
  1069. NULL);
  1070. if (IS_ERR(val))
  1071. return PTR_ERR(val);
  1072. *out_value = be32_to_cpup(((__be32 *)val) + index);
  1073. return 0;
  1074. }
  1075. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  1076. /**
  1077. * of_property_read_u64_index - Find and read a u64 from a multi-value property.
  1078. *
  1079. * @np: device node from which the property value is to be read.
  1080. * @propname: name of the property to be searched.
  1081. * @index: index of the u64 in the list of values
  1082. * @out_value: pointer to return value, modified only if no error.
  1083. *
  1084. * Search for a property in a device node and read nth 64-bit value from
  1085. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1086. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1087. * property data isn't large enough.
  1088. *
  1089. * The out_value is modified only if a valid u64 value can be decoded.
  1090. */
  1091. int of_property_read_u64_index(const struct device_node *np,
  1092. const char *propname,
  1093. u32 index, u64 *out_value)
  1094. {
  1095. const u64 *val = of_find_property_value_of_size(np, propname,
  1096. ((index + 1) * sizeof(*out_value)),
  1097. 0, NULL);
  1098. if (IS_ERR(val))
  1099. return PTR_ERR(val);
  1100. *out_value = be64_to_cpup(((__be64 *)val) + index);
  1101. return 0;
  1102. }
  1103. EXPORT_SYMBOL_GPL(of_property_read_u64_index);
  1104. /**
  1105. * of_property_read_variable_u8_array - Find and read an array of u8 from a
  1106. * property, with bounds on the minimum and maximum array size.
  1107. *
  1108. * @np: device node from which the property value is to be read.
  1109. * @propname: name of the property to be searched.
  1110. * @out_values: pointer to return value, modified only if return value is 0.
  1111. * @sz_min: minimum number of array elements to read
  1112. * @sz_max: maximum number of array elements to read, if zero there is no
  1113. * upper limit on the number of elements in the dts entry but only
  1114. * sz_min will be read.
  1115. *
  1116. * Search for a property in a device node and read 8-bit value(s) from
  1117. * it. Returns number of elements read on success, -EINVAL if the property
  1118. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1119. * if the property data is smaller than sz_min or longer than sz_max.
  1120. *
  1121. * dts entry of array should be like:
  1122. * property = /bits/ 8 <0x50 0x60 0x70>;
  1123. *
  1124. * The out_values is modified only if a valid u8 value can be decoded.
  1125. */
  1126. int of_property_read_variable_u8_array(const struct device_node *np,
  1127. const char *propname, u8 *out_values,
  1128. size_t sz_min, size_t sz_max)
  1129. {
  1130. size_t sz, count;
  1131. const u8 *val = of_find_property_value_of_size(np, propname,
  1132. (sz_min * sizeof(*out_values)),
  1133. (sz_max * sizeof(*out_values)),
  1134. &sz);
  1135. if (IS_ERR(val))
  1136. return PTR_ERR(val);
  1137. if (!sz_max)
  1138. sz = sz_min;
  1139. else
  1140. sz /= sizeof(*out_values);
  1141. count = sz;
  1142. while (count--)
  1143. *out_values++ = *val++;
  1144. return sz;
  1145. }
  1146. EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
  1147. /**
  1148. * of_property_read_variable_u16_array - Find and read an array of u16 from a
  1149. * property, with bounds on the minimum and maximum array size.
  1150. *
  1151. * @np: device node from which the property value is to be read.
  1152. * @propname: name of the property to be searched.
  1153. * @out_values: pointer to return value, modified only if return value is 0.
  1154. * @sz_min: minimum number of array elements to read
  1155. * @sz_max: maximum number of array elements to read, if zero there is no
  1156. * upper limit on the number of elements in the dts entry but only
  1157. * sz_min will be read.
  1158. *
  1159. * Search for a property in a device node and read 16-bit value(s) from
  1160. * it. Returns number of elements read on success, -EINVAL if the property
  1161. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1162. * if the property data is smaller than sz_min or longer than sz_max.
  1163. *
  1164. * dts entry of array should be like:
  1165. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  1166. *
  1167. * The out_values is modified only if a valid u16 value can be decoded.
  1168. */
  1169. int of_property_read_variable_u16_array(const struct device_node *np,
  1170. const char *propname, u16 *out_values,
  1171. size_t sz_min, size_t sz_max)
  1172. {
  1173. size_t sz, count;
  1174. const __be16 *val = of_find_property_value_of_size(np, propname,
  1175. (sz_min * sizeof(*out_values)),
  1176. (sz_max * sizeof(*out_values)),
  1177. &sz);
  1178. if (IS_ERR(val))
  1179. return PTR_ERR(val);
  1180. if (!sz_max)
  1181. sz = sz_min;
  1182. else
  1183. sz /= sizeof(*out_values);
  1184. count = sz;
  1185. while (count--)
  1186. *out_values++ = be16_to_cpup(val++);
  1187. return sz;
  1188. }
  1189. EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
  1190. /**
  1191. * of_property_read_variable_u32_array - Find and read an array of 32 bit
  1192. * integers from a property, with bounds on the minimum and maximum array size.
  1193. *
  1194. * @np: device node from which the property value is to be read.
  1195. * @propname: name of the property to be searched.
  1196. * @out_values: pointer to return value, modified only if return value is 0.
  1197. * @sz_min: minimum number of array elements to read
  1198. * @sz_max: maximum number of array elements to read, if zero there is no
  1199. * upper limit on the number of elements in the dts entry but only
  1200. * sz_min will be read.
  1201. *
  1202. * Search for a property in a device node and read 32-bit value(s) from
  1203. * it. Returns number of elements read on success, -EINVAL if the property
  1204. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1205. * if the property data is smaller than sz_min or longer than sz_max.
  1206. *
  1207. * The out_values is modified only if a valid u32 value can be decoded.
  1208. */
  1209. int of_property_read_variable_u32_array(const struct device_node *np,
  1210. const char *propname, u32 *out_values,
  1211. size_t sz_min, size_t sz_max)
  1212. {
  1213. size_t sz, count;
  1214. const __be32 *val = of_find_property_value_of_size(np, propname,
  1215. (sz_min * sizeof(*out_values)),
  1216. (sz_max * sizeof(*out_values)),
  1217. &sz);
  1218. if (IS_ERR(val))
  1219. return PTR_ERR(val);
  1220. if (!sz_max)
  1221. sz = sz_min;
  1222. else
  1223. sz /= sizeof(*out_values);
  1224. count = sz;
  1225. while (count--)
  1226. *out_values++ = be32_to_cpup(val++);
  1227. return sz;
  1228. }
  1229. EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
  1230. /**
  1231. * of_property_read_u64 - Find and read a 64 bit integer from a property
  1232. * @np: device node from which the property value is to be read.
  1233. * @propname: name of the property to be searched.
  1234. * @out_value: pointer to return value, modified only if return value is 0.
  1235. *
  1236. * Search for a property in a device node and read a 64-bit value from
  1237. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1238. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1239. * property data isn't large enough.
  1240. *
  1241. * The out_value is modified only if a valid u64 value can be decoded.
  1242. */
  1243. int of_property_read_u64(const struct device_node *np, const char *propname,
  1244. u64 *out_value)
  1245. {
  1246. const __be32 *val = of_find_property_value_of_size(np, propname,
  1247. sizeof(*out_value),
  1248. 0,
  1249. NULL);
  1250. if (IS_ERR(val))
  1251. return PTR_ERR(val);
  1252. *out_value = of_read_number(val, 2);
  1253. return 0;
  1254. }
  1255. EXPORT_SYMBOL_GPL(of_property_read_u64);
  1256. /**
  1257. * of_property_read_variable_u64_array - Find and read an array of 64 bit
  1258. * integers from a property, with bounds on the minimum and maximum array size.
  1259. *
  1260. * @np: device node from which the property value is to be read.
  1261. * @propname: name of the property to be searched.
  1262. * @out_values: pointer to return value, modified only if return value is 0.
  1263. * @sz_min: minimum number of array elements to read
  1264. * @sz_max: maximum number of array elements to read, if zero there is no
  1265. * upper limit on the number of elements in the dts entry but only
  1266. * sz_min will be read.
  1267. *
  1268. * Search for a property in a device node and read 64-bit value(s) from
  1269. * it. Returns number of elements read on success, -EINVAL if the property
  1270. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1271. * if the property data is smaller than sz_min or longer than sz_max.
  1272. *
  1273. * The out_values is modified only if a valid u64 value can be decoded.
  1274. */
  1275. int of_property_read_variable_u64_array(const struct device_node *np,
  1276. const char *propname, u64 *out_values,
  1277. size_t sz_min, size_t sz_max)
  1278. {
  1279. size_t sz, count;
  1280. const __be32 *val = of_find_property_value_of_size(np, propname,
  1281. (sz_min * sizeof(*out_values)),
  1282. (sz_max * sizeof(*out_values)),
  1283. &sz);
  1284. if (IS_ERR(val))
  1285. return PTR_ERR(val);
  1286. if (!sz_max)
  1287. sz = sz_min;
  1288. else
  1289. sz /= sizeof(*out_values);
  1290. count = sz;
  1291. while (count--) {
  1292. *out_values++ = of_read_number(val, 2);
  1293. val += 2;
  1294. }
  1295. return sz;
  1296. }
  1297. EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
  1298. /**
  1299. * of_property_read_string - Find and read a string from a property
  1300. * @np: device node from which the property value is to be read.
  1301. * @propname: name of the property to be searched.
  1302. * @out_string: pointer to null terminated return string, modified only if
  1303. * return value is 0.
  1304. *
  1305. * Search for a property in a device tree node and retrieve a null
  1306. * terminated string value (pointer to data, not a copy). Returns 0 on
  1307. * success, -EINVAL if the property does not exist, -ENODATA if property
  1308. * does not have a value, and -EILSEQ if the string is not null-terminated
  1309. * within the length of the property data.
  1310. *
  1311. * The out_string pointer is modified only if a valid string can be decoded.
  1312. */
  1313. int of_property_read_string(const struct device_node *np, const char *propname,
  1314. const char **out_string)
  1315. {
  1316. const struct property *prop = of_find_property(np, propname, NULL);
  1317. if (!prop)
  1318. return -EINVAL;
  1319. if (!prop->value)
  1320. return -ENODATA;
  1321. if (strnlen(prop->value, prop->length) >= prop->length)
  1322. return -EILSEQ;
  1323. *out_string = prop->value;
  1324. return 0;
  1325. }
  1326. EXPORT_SYMBOL_GPL(of_property_read_string);
  1327. /**
  1328. * of_property_match_string() - Find string in a list and return index
  1329. * @np: pointer to node containing string list property
  1330. * @propname: string list property name
  1331. * @string: pointer to string to search for in string list
  1332. *
  1333. * This function searches a string list property and returns the index
  1334. * of a specific string value.
  1335. */
  1336. int of_property_match_string(const struct device_node *np, const char *propname,
  1337. const char *string)
  1338. {
  1339. const struct property *prop = of_find_property(np, propname, NULL);
  1340. size_t l;
  1341. int i;
  1342. const char *p, *end;
  1343. if (!prop)
  1344. return -EINVAL;
  1345. if (!prop->value)
  1346. return -ENODATA;
  1347. p = prop->value;
  1348. end = p + prop->length;
  1349. for (i = 0; p < end; i++, p += l) {
  1350. l = strnlen(p, end - p) + 1;
  1351. if (p + l > end)
  1352. return -EILSEQ;
  1353. pr_debug("comparing %s with %s\n", string, p);
  1354. if (strcmp(string, p) == 0)
  1355. return i; /* Found it; return index */
  1356. }
  1357. return -ENODATA;
  1358. }
  1359. EXPORT_SYMBOL_GPL(of_property_match_string);
  1360. /**
  1361. * of_property_read_string_helper() - Utility helper for parsing string properties
  1362. * @np: device node from which the property value is to be read.
  1363. * @propname: name of the property to be searched.
  1364. * @out_strs: output array of string pointers.
  1365. * @sz: number of array elements to read.
  1366. * @skip: Number of strings to skip over at beginning of list.
  1367. *
  1368. * Don't call this function directly. It is a utility helper for the
  1369. * of_property_read_string*() family of functions.
  1370. */
  1371. int of_property_read_string_helper(const struct device_node *np,
  1372. const char *propname, const char **out_strs,
  1373. size_t sz, int skip)
  1374. {
  1375. const struct property *prop = of_find_property(np, propname, NULL);
  1376. int l = 0, i = 0;
  1377. const char *p, *end;
  1378. if (!prop)
  1379. return -EINVAL;
  1380. if (!prop->value)
  1381. return -ENODATA;
  1382. p = prop->value;
  1383. end = p + prop->length;
  1384. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  1385. l = strnlen(p, end - p) + 1;
  1386. if (p + l > end)
  1387. return -EILSEQ;
  1388. if (out_strs && i >= skip)
  1389. *out_strs++ = p;
  1390. }
  1391. i -= skip;
  1392. return i <= 0 ? -ENODATA : i;
  1393. }
  1394. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  1395. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1396. {
  1397. int i;
  1398. printk("%s %s", msg, of_node_full_name(args->np));
  1399. for (i = 0; i < args->args_count; i++) {
  1400. const char delim = i ? ',' : ':';
  1401. pr_cont("%c%08x", delim, args->args[i]);
  1402. }
  1403. pr_cont("\n");
  1404. }
  1405. int of_phandle_iterator_init(struct of_phandle_iterator *it,
  1406. const struct device_node *np,
  1407. const char *list_name,
  1408. const char *cells_name,
  1409. int cell_count)
  1410. {
  1411. const __be32 *list;
  1412. int size;
  1413. memset(it, 0, sizeof(*it));
  1414. list = of_get_property(np, list_name, &size);
  1415. if (!list)
  1416. return -ENOENT;
  1417. it->cells_name = cells_name;
  1418. it->cell_count = cell_count;
  1419. it->parent = np;
  1420. it->list_end = list + size / sizeof(*list);
  1421. it->phandle_end = list;
  1422. it->cur = list;
  1423. return 0;
  1424. }
  1425. int of_phandle_iterator_next(struct of_phandle_iterator *it)
  1426. {
  1427. uint32_t count = 0;
  1428. if (it->node) {
  1429. of_node_put(it->node);
  1430. it->node = NULL;
  1431. }
  1432. if (!it->cur || it->phandle_end >= it->list_end)
  1433. return -ENOENT;
  1434. it->cur = it->phandle_end;
  1435. /* If phandle is 0, then it is an empty entry with no arguments. */
  1436. it->phandle = be32_to_cpup(it->cur++);
  1437. if (it->phandle) {
  1438. /*
  1439. * Find the provider node and parse the #*-cells property to
  1440. * determine the argument length.
  1441. */
  1442. it->node = of_find_node_by_phandle(it->phandle);
  1443. if (it->cells_name) {
  1444. if (!it->node) {
  1445. pr_err("%s: could not find phandle\n",
  1446. it->parent->full_name);
  1447. goto err;
  1448. }
  1449. if (of_property_read_u32(it->node, it->cells_name,
  1450. &count)) {
  1451. pr_err("%s: could not get %s for %s\n",
  1452. it->parent->full_name,
  1453. it->cells_name,
  1454. it->node->full_name);
  1455. goto err;
  1456. }
  1457. } else {
  1458. count = it->cell_count;
  1459. }
  1460. /*
  1461. * Make sure that the arguments actually fit in the remaining
  1462. * property data length
  1463. */
  1464. if (it->cur + count > it->list_end) {
  1465. pr_err("%s: arguments longer than property\n",
  1466. it->parent->full_name);
  1467. goto err;
  1468. }
  1469. }
  1470. it->phandle_end = it->cur + count;
  1471. it->cur_count = count;
  1472. return 0;
  1473. err:
  1474. if (it->node) {
  1475. of_node_put(it->node);
  1476. it->node = NULL;
  1477. }
  1478. return -EINVAL;
  1479. }
  1480. int of_phandle_iterator_args(struct of_phandle_iterator *it,
  1481. uint32_t *args,
  1482. int size)
  1483. {
  1484. int i, count;
  1485. count = it->cur_count;
  1486. if (WARN_ON(size < count))
  1487. count = size;
  1488. for (i = 0; i < count; i++)
  1489. args[i] = be32_to_cpup(it->cur++);
  1490. return count;
  1491. }
  1492. static int __of_parse_phandle_with_args(const struct device_node *np,
  1493. const char *list_name,
  1494. const char *cells_name,
  1495. int cell_count, int index,
  1496. struct of_phandle_args *out_args)
  1497. {
  1498. struct of_phandle_iterator it;
  1499. int rc, cur_index = 0;
  1500. /* Loop over the phandles until all the requested entry is found */
  1501. of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
  1502. /*
  1503. * All of the error cases bail out of the loop, so at
  1504. * this point, the parsing is successful. If the requested
  1505. * index matches, then fill the out_args structure and return,
  1506. * or return -ENOENT for an empty entry.
  1507. */
  1508. rc = -ENOENT;
  1509. if (cur_index == index) {
  1510. if (!it.phandle)
  1511. goto err;
  1512. if (out_args) {
  1513. int c;
  1514. c = of_phandle_iterator_args(&it,
  1515. out_args->args,
  1516. MAX_PHANDLE_ARGS);
  1517. out_args->np = it.node;
  1518. out_args->args_count = c;
  1519. } else {
  1520. of_node_put(it.node);
  1521. }
  1522. /* Found it! return success */
  1523. return 0;
  1524. }
  1525. cur_index++;
  1526. }
  1527. /*
  1528. * Unlock node before returning result; will be one of:
  1529. * -ENOENT : index is for empty phandle
  1530. * -EINVAL : parsing error on data
  1531. */
  1532. err:
  1533. of_node_put(it.node);
  1534. return rc;
  1535. }
  1536. /**
  1537. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1538. * @np: Pointer to device node holding phandle property
  1539. * @phandle_name: Name of property holding a phandle value
  1540. * @index: For properties holding a table of phandles, this is the index into
  1541. * the table
  1542. *
  1543. * Returns the device_node pointer with refcount incremented. Use
  1544. * of_node_put() on it when done.
  1545. */
  1546. struct device_node *of_parse_phandle(const struct device_node *np,
  1547. const char *phandle_name, int index)
  1548. {
  1549. struct of_phandle_args args;
  1550. if (index < 0)
  1551. return NULL;
  1552. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1553. index, &args))
  1554. return NULL;
  1555. return args.np;
  1556. }
  1557. EXPORT_SYMBOL(of_parse_phandle);
  1558. /**
  1559. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1560. * @np: pointer to a device tree node containing a list
  1561. * @list_name: property name that contains a list
  1562. * @cells_name: property name that specifies phandles' arguments count
  1563. * @index: index of a phandle to parse out
  1564. * @out_args: optional pointer to output arguments structure (will be filled)
  1565. *
  1566. * This function is useful to parse lists of phandles and their arguments.
  1567. * Returns 0 on success and fills out_args, on error returns appropriate
  1568. * errno value.
  1569. *
  1570. * Caller is responsible to call of_node_put() on the returned out_args->np
  1571. * pointer.
  1572. *
  1573. * Example:
  1574. *
  1575. * phandle1: node1 {
  1576. * #list-cells = <2>;
  1577. * }
  1578. *
  1579. * phandle2: node2 {
  1580. * #list-cells = <1>;
  1581. * }
  1582. *
  1583. * node3 {
  1584. * list = <&phandle1 1 2 &phandle2 3>;
  1585. * }
  1586. *
  1587. * To get a device_node of the `node2' node you may call this:
  1588. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1589. */
  1590. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1591. const char *cells_name, int index,
  1592. struct of_phandle_args *out_args)
  1593. {
  1594. if (index < 0)
  1595. return -EINVAL;
  1596. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1597. index, out_args);
  1598. }
  1599. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1600. /**
  1601. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1602. * @np: pointer to a device tree node containing a list
  1603. * @list_name: property name that contains a list
  1604. * @cell_count: number of argument cells following the phandle
  1605. * @index: index of a phandle to parse out
  1606. * @out_args: optional pointer to output arguments structure (will be filled)
  1607. *
  1608. * This function is useful to parse lists of phandles and their arguments.
  1609. * Returns 0 on success and fills out_args, on error returns appropriate
  1610. * errno value.
  1611. *
  1612. * Caller is responsible to call of_node_put() on the returned out_args->np
  1613. * pointer.
  1614. *
  1615. * Example:
  1616. *
  1617. * phandle1: node1 {
  1618. * }
  1619. *
  1620. * phandle2: node2 {
  1621. * }
  1622. *
  1623. * node3 {
  1624. * list = <&phandle1 0 2 &phandle2 2 3>;
  1625. * }
  1626. *
  1627. * To get a device_node of the `node2' node you may call this:
  1628. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1629. */
  1630. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1631. const char *list_name, int cell_count,
  1632. int index, struct of_phandle_args *out_args)
  1633. {
  1634. if (index < 0)
  1635. return -EINVAL;
  1636. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1637. index, out_args);
  1638. }
  1639. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1640. /**
  1641. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1642. * @np: pointer to a device tree node containing a list
  1643. * @list_name: property name that contains a list
  1644. * @cells_name: property name that specifies phandles' arguments count
  1645. *
  1646. * Returns the number of phandle + argument tuples within a property. It
  1647. * is a typical pattern to encode a list of phandle and variable
  1648. * arguments into a single property. The number of arguments is encoded
  1649. * by a property in the phandle-target node. For example, a gpios
  1650. * property would contain a list of GPIO specifies consisting of a
  1651. * phandle and 1 or more arguments. The number of arguments are
  1652. * determined by the #gpio-cells property in the node pointed to by the
  1653. * phandle.
  1654. */
  1655. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1656. const char *cells_name)
  1657. {
  1658. struct of_phandle_iterator it;
  1659. int rc, cur_index = 0;
  1660. rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
  1661. if (rc)
  1662. return rc;
  1663. while ((rc = of_phandle_iterator_next(&it)) == 0)
  1664. cur_index += 1;
  1665. if (rc != -ENOENT)
  1666. return rc;
  1667. return cur_index;
  1668. }
  1669. EXPORT_SYMBOL(of_count_phandle_with_args);
  1670. /**
  1671. * __of_add_property - Add a property to a node without lock operations
  1672. */
  1673. int __of_add_property(struct device_node *np, struct property *prop)
  1674. {
  1675. struct property **next;
  1676. prop->next = NULL;
  1677. next = &np->properties;
  1678. while (*next) {
  1679. if (strcmp(prop->name, (*next)->name) == 0)
  1680. /* duplicate ! don't insert it */
  1681. return -EEXIST;
  1682. next = &(*next)->next;
  1683. }
  1684. *next = prop;
  1685. return 0;
  1686. }
  1687. /**
  1688. * of_add_property - Add a property to a node
  1689. */
  1690. int of_add_property(struct device_node *np, struct property *prop)
  1691. {
  1692. unsigned long flags;
  1693. int rc;
  1694. mutex_lock(&of_mutex);
  1695. raw_spin_lock_irqsave(&devtree_lock, flags);
  1696. rc = __of_add_property(np, prop);
  1697. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1698. if (!rc)
  1699. __of_add_property_sysfs(np, prop);
  1700. mutex_unlock(&of_mutex);
  1701. if (!rc)
  1702. of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
  1703. return rc;
  1704. }
  1705. int __of_remove_property(struct device_node *np, struct property *prop)
  1706. {
  1707. struct property **next;
  1708. for (next = &np->properties; *next; next = &(*next)->next) {
  1709. if (*next == prop)
  1710. break;
  1711. }
  1712. if (*next == NULL)
  1713. return -ENODEV;
  1714. /* found the node */
  1715. *next = prop->next;
  1716. prop->next = np->deadprops;
  1717. np->deadprops = prop;
  1718. return 0;
  1719. }
  1720. void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
  1721. {
  1722. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1723. kfree(prop->attr.attr.name);
  1724. }
  1725. void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
  1726. {
  1727. if (!IS_ENABLED(CONFIG_SYSFS))
  1728. return;
  1729. /* at early boot, bail here and defer setup to of_init() */
  1730. if (of_kset && of_node_is_attached(np))
  1731. __of_sysfs_remove_bin_file(np, prop);
  1732. }
  1733. /**
  1734. * of_remove_property - Remove a property from a node.
  1735. *
  1736. * Note that we don't actually remove it, since we have given out
  1737. * who-knows-how-many pointers to the data using get-property.
  1738. * Instead we just move the property to the "dead properties"
  1739. * list, so it won't be found any more.
  1740. */
  1741. int of_remove_property(struct device_node *np, struct property *prop)
  1742. {
  1743. unsigned long flags;
  1744. int rc;
  1745. if (!prop)
  1746. return -ENODEV;
  1747. mutex_lock(&of_mutex);
  1748. raw_spin_lock_irqsave(&devtree_lock, flags);
  1749. rc = __of_remove_property(np, prop);
  1750. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1751. if (!rc)
  1752. __of_remove_property_sysfs(np, prop);
  1753. mutex_unlock(&of_mutex);
  1754. if (!rc)
  1755. of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
  1756. return rc;
  1757. }
  1758. int __of_update_property(struct device_node *np, struct property *newprop,
  1759. struct property **oldpropp)
  1760. {
  1761. struct property **next, *oldprop;
  1762. for (next = &np->properties; *next; next = &(*next)->next) {
  1763. if (of_prop_cmp((*next)->name, newprop->name) == 0)
  1764. break;
  1765. }
  1766. *oldpropp = oldprop = *next;
  1767. if (oldprop) {
  1768. /* replace the node */
  1769. newprop->next = oldprop->next;
  1770. *next = newprop;
  1771. oldprop->next = np->deadprops;
  1772. np->deadprops = oldprop;
  1773. } else {
  1774. /* new node */
  1775. newprop->next = NULL;
  1776. *next = newprop;
  1777. }
  1778. return 0;
  1779. }
  1780. void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
  1781. struct property *oldprop)
  1782. {
  1783. if (!IS_ENABLED(CONFIG_SYSFS))
  1784. return;
  1785. /* At early boot, bail out and defer setup to of_init() */
  1786. if (!of_kset)
  1787. return;
  1788. if (oldprop)
  1789. __of_sysfs_remove_bin_file(np, oldprop);
  1790. __of_add_property_sysfs(np, newprop);
  1791. }
  1792. /*
  1793. * of_update_property - Update a property in a node, if the property does
  1794. * not exist, add it.
  1795. *
  1796. * Note that we don't actually remove it, since we have given out
  1797. * who-knows-how-many pointers to the data using get-property.
  1798. * Instead we just move the property to the "dead properties" list,
  1799. * and add the new property to the property list
  1800. */
  1801. int of_update_property(struct device_node *np, struct property *newprop)
  1802. {
  1803. struct property *oldprop;
  1804. unsigned long flags;
  1805. int rc;
  1806. if (!newprop->name)
  1807. return -EINVAL;
  1808. mutex_lock(&of_mutex);
  1809. raw_spin_lock_irqsave(&devtree_lock, flags);
  1810. rc = __of_update_property(np, newprop, &oldprop);
  1811. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1812. if (!rc)
  1813. __of_update_property_sysfs(np, newprop, oldprop);
  1814. mutex_unlock(&of_mutex);
  1815. if (!rc)
  1816. of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
  1817. return rc;
  1818. }
  1819. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1820. int id, const char *stem, int stem_len)
  1821. {
  1822. ap->np = np;
  1823. ap->id = id;
  1824. strncpy(ap->stem, stem, stem_len);
  1825. ap->stem[stem_len] = 0;
  1826. list_add_tail(&ap->link, &aliases_lookup);
  1827. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1828. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1829. }
  1830. /**
  1831. * of_alias_scan - Scan all properties of the 'aliases' node
  1832. *
  1833. * The function scans all the properties of the 'aliases' node and populates
  1834. * the global lookup table with the properties. It returns the
  1835. * number of alias properties found, or an error code in case of failure.
  1836. *
  1837. * @dt_alloc: An allocator that provides a virtual address to memory
  1838. * for storing the resulting tree
  1839. */
  1840. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1841. {
  1842. struct property *pp;
  1843. of_aliases = of_find_node_by_path("/aliases");
  1844. of_chosen = of_find_node_by_path("/chosen");
  1845. if (of_chosen == NULL)
  1846. of_chosen = of_find_node_by_path("/chosen@0");
  1847. if (of_chosen) {
  1848. /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
  1849. const char *name = of_get_property(of_chosen, "stdout-path", NULL);
  1850. if (!name)
  1851. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1852. if (IS_ENABLED(CONFIG_PPC) && !name)
  1853. name = of_get_property(of_aliases, "stdout", NULL);
  1854. if (name)
  1855. of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
  1856. }
  1857. if (!of_aliases)
  1858. return;
  1859. for_each_property_of_node(of_aliases, pp) {
  1860. const char *start = pp->name;
  1861. const char *end = start + strlen(start);
  1862. struct device_node *np;
  1863. struct alias_prop *ap;
  1864. int id, len;
  1865. /* Skip those we do not want to proceed */
  1866. if (!strcmp(pp->name, "name") ||
  1867. !strcmp(pp->name, "phandle") ||
  1868. !strcmp(pp->name, "linux,phandle"))
  1869. continue;
  1870. np = of_find_node_by_path(pp->value);
  1871. if (!np)
  1872. continue;
  1873. /* walk the alias backwards to extract the id and work out
  1874. * the 'stem' string */
  1875. while (isdigit(*(end-1)) && end > start)
  1876. end--;
  1877. len = end - start;
  1878. if (kstrtoint(end, 10, &id) < 0)
  1879. continue;
  1880. /* Allocate an alias_prop with enough space for the stem */
  1881. ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
  1882. if (!ap)
  1883. continue;
  1884. memset(ap, 0, sizeof(*ap) + len + 1);
  1885. ap->alias = start;
  1886. of_alias_add(ap, np, id, start, len);
  1887. }
  1888. }
  1889. /**
  1890. * of_alias_get_id - Get alias id for the given device_node
  1891. * @np: Pointer to the given device_node
  1892. * @stem: Alias stem of the given device_node
  1893. *
  1894. * The function travels the lookup table to get the alias id for the given
  1895. * device_node and alias stem. It returns the alias id if found.
  1896. */
  1897. int of_alias_get_id(struct device_node *np, const char *stem)
  1898. {
  1899. struct alias_prop *app;
  1900. int id = -ENODEV;
  1901. mutex_lock(&of_mutex);
  1902. list_for_each_entry(app, &aliases_lookup, link) {
  1903. if (strcmp(app->stem, stem) != 0)
  1904. continue;
  1905. if (np == app->np) {
  1906. id = app->id;
  1907. break;
  1908. }
  1909. }
  1910. mutex_unlock(&of_mutex);
  1911. return id;
  1912. }
  1913. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1914. /**
  1915. * of_alias_get_highest_id - Get highest alias id for the given stem
  1916. * @stem: Alias stem to be examined
  1917. *
  1918. * The function travels the lookup table to get the highest alias id for the
  1919. * given alias stem. It returns the alias id if found.
  1920. */
  1921. int of_alias_get_highest_id(const char *stem)
  1922. {
  1923. struct alias_prop *app;
  1924. int id = -ENODEV;
  1925. mutex_lock(&of_mutex);
  1926. list_for_each_entry(app, &aliases_lookup, link) {
  1927. if (strcmp(app->stem, stem) != 0)
  1928. continue;
  1929. if (app->id > id)
  1930. id = app->id;
  1931. }
  1932. mutex_unlock(&of_mutex);
  1933. return id;
  1934. }
  1935. EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
  1936. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1937. u32 *pu)
  1938. {
  1939. const void *curv = cur;
  1940. if (!prop)
  1941. return NULL;
  1942. if (!cur) {
  1943. curv = prop->value;
  1944. goto out_val;
  1945. }
  1946. curv += sizeof(*cur);
  1947. if (curv >= prop->value + prop->length)
  1948. return NULL;
  1949. out_val:
  1950. *pu = be32_to_cpup(curv);
  1951. return curv;
  1952. }
  1953. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1954. const char *of_prop_next_string(struct property *prop, const char *cur)
  1955. {
  1956. const void *curv = cur;
  1957. if (!prop)
  1958. return NULL;
  1959. if (!cur)
  1960. return prop->value;
  1961. curv += strlen(cur) + 1;
  1962. if (curv >= prop->value + prop->length)
  1963. return NULL;
  1964. return curv;
  1965. }
  1966. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1967. /**
  1968. * of_console_check() - Test and setup console for DT setup
  1969. * @dn - Pointer to device node
  1970. * @name - Name to use for preferred console without index. ex. "ttyS"
  1971. * @index - Index to use for preferred console.
  1972. *
  1973. * Check if the given device node matches the stdout-path property in the
  1974. * /chosen node. If it does then register it as the preferred console and return
  1975. * TRUE. Otherwise return FALSE.
  1976. */
  1977. bool of_console_check(struct device_node *dn, char *name, int index)
  1978. {
  1979. if (!dn || dn != of_stdout || console_set_on_cmdline)
  1980. return false;
  1981. return !add_preferred_console(name, index,
  1982. kstrdup(of_stdout_options, GFP_KERNEL));
  1983. }
  1984. EXPORT_SYMBOL_GPL(of_console_check);
  1985. /**
  1986. * of_find_next_cache_node - Find a node's subsidiary cache
  1987. * @np: node of type "cpu" or "cache"
  1988. *
  1989. * Returns a node pointer with refcount incremented, use
  1990. * of_node_put() on it when done. Caller should hold a reference
  1991. * to np.
  1992. */
  1993. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1994. {
  1995. struct device_node *child, *cache_node;
  1996. cache_node = of_parse_phandle(np, "l2-cache", 0);
  1997. if (!cache_node)
  1998. cache_node = of_parse_phandle(np, "next-level-cache", 0);
  1999. if (cache_node)
  2000. return cache_node;
  2001. /* OF on pmac has nodes instead of properties named "l2-cache"
  2002. * beneath CPU nodes.
  2003. */
  2004. if (!strcmp(np->type, "cpu"))
  2005. for_each_child_of_node(np, child)
  2006. if (!strcmp(child->type, "cache"))
  2007. return child;
  2008. return NULL;
  2009. }
  2010. /**
  2011. * of_find_last_cache_level - Find the level at which the last cache is
  2012. * present for the given logical cpu
  2013. *
  2014. * @cpu: cpu number(logical index) for which the last cache level is needed
  2015. *
  2016. * Returns the the level at which the last cache is present. It is exactly
  2017. * same as the total number of cache levels for the given logical cpu.
  2018. */
  2019. int of_find_last_cache_level(unsigned int cpu)
  2020. {
  2021. u32 cache_level = 0;
  2022. struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
  2023. while (np) {
  2024. prev = np;
  2025. of_node_put(np);
  2026. np = of_find_next_cache_node(np);
  2027. }
  2028. of_property_read_u32(prev, "cache-level", &cache_level);
  2029. return cache_level;
  2030. }
  2031. /**
  2032. * of_graph_parse_endpoint() - parse common endpoint node properties
  2033. * @node: pointer to endpoint device_node
  2034. * @endpoint: pointer to the OF endpoint data structure
  2035. *
  2036. * The caller should hold a reference to @node.
  2037. */
  2038. int of_graph_parse_endpoint(const struct device_node *node,
  2039. struct of_endpoint *endpoint)
  2040. {
  2041. struct device_node *port_node = of_get_parent(node);
  2042. WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
  2043. __func__, node->full_name);
  2044. memset(endpoint, 0, sizeof(*endpoint));
  2045. endpoint->local_node = node;
  2046. /*
  2047. * It doesn't matter whether the two calls below succeed.
  2048. * If they don't then the default value 0 is used.
  2049. */
  2050. of_property_read_u32(port_node, "reg", &endpoint->port);
  2051. of_property_read_u32(node, "reg", &endpoint->id);
  2052. of_node_put(port_node);
  2053. return 0;
  2054. }
  2055. EXPORT_SYMBOL(of_graph_parse_endpoint);
  2056. /**
  2057. * of_graph_get_port_by_id() - get the port matching a given id
  2058. * @parent: pointer to the parent device node
  2059. * @id: id of the port
  2060. *
  2061. * Return: A 'port' node pointer with refcount incremented. The caller
  2062. * has to use of_node_put() on it when done.
  2063. */
  2064. struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
  2065. {
  2066. struct device_node *node, *port;
  2067. node = of_get_child_by_name(parent, "ports");
  2068. if (node)
  2069. parent = node;
  2070. for_each_child_of_node(parent, port) {
  2071. u32 port_id = 0;
  2072. if (of_node_cmp(port->name, "port") != 0)
  2073. continue;
  2074. of_property_read_u32(port, "reg", &port_id);
  2075. if (id == port_id)
  2076. break;
  2077. }
  2078. of_node_put(node);
  2079. return port;
  2080. }
  2081. EXPORT_SYMBOL(of_graph_get_port_by_id);
  2082. /**
  2083. * of_graph_get_next_endpoint() - get next endpoint node
  2084. * @parent: pointer to the parent device node
  2085. * @prev: previous endpoint node, or NULL to get first
  2086. *
  2087. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  2088. * of the passed @prev node is decremented.
  2089. */
  2090. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  2091. struct device_node *prev)
  2092. {
  2093. struct device_node *endpoint;
  2094. struct device_node *port;
  2095. if (!parent)
  2096. return NULL;
  2097. /*
  2098. * Start by locating the port node. If no previous endpoint is specified
  2099. * search for the first port node, otherwise get the previous endpoint
  2100. * parent port node.
  2101. */
  2102. if (!prev) {
  2103. struct device_node *node;
  2104. node = of_get_child_by_name(parent, "ports");
  2105. if (node)
  2106. parent = node;
  2107. port = of_get_child_by_name(parent, "port");
  2108. of_node_put(node);
  2109. if (!port) {
  2110. pr_err("graph: no port node found in %s\n",
  2111. parent->full_name);
  2112. return NULL;
  2113. }
  2114. } else {
  2115. port = of_get_parent(prev);
  2116. if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
  2117. __func__, prev->full_name))
  2118. return NULL;
  2119. }
  2120. while (1) {
  2121. /*
  2122. * Now that we have a port node, get the next endpoint by
  2123. * getting the next child. If the previous endpoint is NULL this
  2124. * will return the first child.
  2125. */
  2126. endpoint = of_get_next_child(port, prev);
  2127. if (endpoint) {
  2128. of_node_put(port);
  2129. return endpoint;
  2130. }
  2131. /* No more endpoints under this port, try the next one. */
  2132. prev = NULL;
  2133. do {
  2134. port = of_get_next_child(parent, port);
  2135. if (!port)
  2136. return NULL;
  2137. } while (of_node_cmp(port->name, "port"));
  2138. }
  2139. }
  2140. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  2141. /**
  2142. * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
  2143. * @parent: pointer to the parent device node
  2144. * @port_reg: identifier (value of reg property) of the parent port node
  2145. * @reg: identifier (value of reg property) of the endpoint node
  2146. *
  2147. * Return: An 'endpoint' node pointer which is identified by reg and at the same
  2148. * is the child of a port node identified by port_reg. reg and port_reg are
  2149. * ignored when they are -1.
  2150. */
  2151. struct device_node *of_graph_get_endpoint_by_regs(
  2152. const struct device_node *parent, int port_reg, int reg)
  2153. {
  2154. struct of_endpoint endpoint;
  2155. struct device_node *node = NULL;
  2156. for_each_endpoint_of_node(parent, node) {
  2157. of_graph_parse_endpoint(node, &endpoint);
  2158. if (((port_reg == -1) || (endpoint.port == port_reg)) &&
  2159. ((reg == -1) || (endpoint.id == reg)))
  2160. return node;
  2161. }
  2162. return NULL;
  2163. }
  2164. EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
  2165. /**
  2166. * of_graph_get_remote_port_parent() - get remote port's parent node
  2167. * @node: pointer to a local endpoint device_node
  2168. *
  2169. * Return: Remote device node associated with remote endpoint node linked
  2170. * to @node. Use of_node_put() on it when done.
  2171. */
  2172. struct device_node *of_graph_get_remote_port_parent(
  2173. const struct device_node *node)
  2174. {
  2175. struct device_node *np;
  2176. unsigned int depth;
  2177. /* Get remote endpoint node. */
  2178. np = of_parse_phandle(node, "remote-endpoint", 0);
  2179. /* Walk 3 levels up only if there is 'ports' node. */
  2180. for (depth = 3; depth && np; depth--) {
  2181. np = of_get_next_parent(np);
  2182. if (depth == 2 && of_node_cmp(np->name, "ports"))
  2183. break;
  2184. }
  2185. return np;
  2186. }
  2187. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  2188. /**
  2189. * of_graph_get_remote_port() - get remote port node
  2190. * @node: pointer to a local endpoint device_node
  2191. *
  2192. * Return: Remote port node associated with remote endpoint node linked
  2193. * to @node. Use of_node_put() on it when done.
  2194. */
  2195. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  2196. {
  2197. struct device_node *np;
  2198. /* Get remote endpoint node. */
  2199. np = of_parse_phandle(node, "remote-endpoint", 0);
  2200. if (!np)
  2201. return NULL;
  2202. return of_get_next_parent(np);
  2203. }
  2204. EXPORT_SYMBOL(of_graph_get_remote_port);
  2205. /**
  2206. * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
  2207. * @node: pointer to parent device_node containing graph port/endpoint
  2208. * @port: identifier (value of reg property) of the parent port node
  2209. * @endpoint: identifier (value of reg property) of the endpoint node
  2210. *
  2211. * Return: Remote device node associated with remote endpoint node linked
  2212. * to @node. Use of_node_put() on it when done.
  2213. */
  2214. struct device_node *of_graph_get_remote_node(const struct device_node *node,
  2215. u32 port, u32 endpoint)
  2216. {
  2217. struct device_node *endpoint_node, *remote;
  2218. endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
  2219. if (!endpoint_node) {
  2220. pr_debug("no valid endpoint (%d, %d) for node %s\n",
  2221. port, endpoint, node->full_name);
  2222. return NULL;
  2223. }
  2224. remote = of_graph_get_remote_port_parent(endpoint_node);
  2225. of_node_put(endpoint_node);
  2226. if (!remote) {
  2227. pr_debug("no valid remote node\n");
  2228. return NULL;
  2229. }
  2230. if (!of_device_is_available(remote)) {
  2231. pr_debug("not available for remote node\n");
  2232. return NULL;
  2233. }
  2234. return remote;
  2235. }
  2236. EXPORT_SYMBOL(of_graph_get_remote_node);