base.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  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_variable_u8_array - Find and read an array of u8 from a
  1078. * property, with bounds on the minimum and maximum array size.
  1079. *
  1080. * @np: device node from which the property value is to be read.
  1081. * @propname: name of the property to be searched.
  1082. * @out_values: pointer to return value, modified only if return value is 0.
  1083. * @sz_min: minimum number of array elements to read
  1084. * @sz_max: maximum number of array elements to read, if zero there is no
  1085. * upper limit on the number of elements in the dts entry but only
  1086. * sz_min will be read.
  1087. *
  1088. * Search for a property in a device node and read 8-bit value(s) from
  1089. * it. Returns number of elements read on success, -EINVAL if the property
  1090. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1091. * if the property data is smaller than sz_min or longer than sz_max.
  1092. *
  1093. * dts entry of array should be like:
  1094. * property = /bits/ 8 <0x50 0x60 0x70>;
  1095. *
  1096. * The out_values is modified only if a valid u8 value can be decoded.
  1097. */
  1098. int of_property_read_variable_u8_array(const struct device_node *np,
  1099. const char *propname, u8 *out_values,
  1100. size_t sz_min, size_t sz_max)
  1101. {
  1102. size_t sz, count;
  1103. const u8 *val = of_find_property_value_of_size(np, propname,
  1104. (sz_min * sizeof(*out_values)),
  1105. (sz_max * sizeof(*out_values)),
  1106. &sz);
  1107. if (IS_ERR(val))
  1108. return PTR_ERR(val);
  1109. if (!sz_max)
  1110. sz = sz_min;
  1111. else
  1112. sz /= sizeof(*out_values);
  1113. count = sz;
  1114. while (count--)
  1115. *out_values++ = *val++;
  1116. return sz;
  1117. }
  1118. EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
  1119. /**
  1120. * of_property_read_variable_u16_array - Find and read an array of u16 from a
  1121. * property, with bounds on the minimum and maximum array size.
  1122. *
  1123. * @np: device node from which the property value is to be read.
  1124. * @propname: name of the property to be searched.
  1125. * @out_values: pointer to return value, modified only if return value is 0.
  1126. * @sz_min: minimum number of array elements to read
  1127. * @sz_max: maximum number of array elements to read, if zero there is no
  1128. * upper limit on the number of elements in the dts entry but only
  1129. * sz_min will be read.
  1130. *
  1131. * Search for a property in a device node and read 16-bit value(s) from
  1132. * it. Returns number of elements read on success, -EINVAL if the property
  1133. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1134. * if the property data is smaller than sz_min or longer than sz_max.
  1135. *
  1136. * dts entry of array should be like:
  1137. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  1138. *
  1139. * The out_values is modified only if a valid u16 value can be decoded.
  1140. */
  1141. int of_property_read_variable_u16_array(const struct device_node *np,
  1142. const char *propname, u16 *out_values,
  1143. size_t sz_min, size_t sz_max)
  1144. {
  1145. size_t sz, count;
  1146. const __be16 *val = of_find_property_value_of_size(np, propname,
  1147. (sz_min * sizeof(*out_values)),
  1148. (sz_max * sizeof(*out_values)),
  1149. &sz);
  1150. if (IS_ERR(val))
  1151. return PTR_ERR(val);
  1152. if (!sz_max)
  1153. sz = sz_min;
  1154. else
  1155. sz /= sizeof(*out_values);
  1156. count = sz;
  1157. while (count--)
  1158. *out_values++ = be16_to_cpup(val++);
  1159. return sz;
  1160. }
  1161. EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
  1162. /**
  1163. * of_property_read_variable_u32_array - Find and read an array of 32 bit
  1164. * integers from a property, with bounds on the minimum and maximum array size.
  1165. *
  1166. * @np: device node from which the property value is to be read.
  1167. * @propname: name of the property to be searched.
  1168. * @out_values: pointer to return value, modified only if return value is 0.
  1169. * @sz_min: minimum number of array elements to read
  1170. * @sz_max: maximum number of array elements to read, if zero there is no
  1171. * upper limit on the number of elements in the dts entry but only
  1172. * sz_min will be read.
  1173. *
  1174. * Search for a property in a device node and read 32-bit value(s) from
  1175. * it. Returns number of elements read on success, -EINVAL if the property
  1176. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1177. * if the property data is smaller than sz_min or longer than sz_max.
  1178. *
  1179. * The out_values is modified only if a valid u32 value can be decoded.
  1180. */
  1181. int of_property_read_variable_u32_array(const struct device_node *np,
  1182. const char *propname, u32 *out_values,
  1183. size_t sz_min, size_t sz_max)
  1184. {
  1185. size_t sz, count;
  1186. const __be32 *val = of_find_property_value_of_size(np, propname,
  1187. (sz_min * sizeof(*out_values)),
  1188. (sz_max * sizeof(*out_values)),
  1189. &sz);
  1190. if (IS_ERR(val))
  1191. return PTR_ERR(val);
  1192. if (!sz_max)
  1193. sz = sz_min;
  1194. else
  1195. sz /= sizeof(*out_values);
  1196. count = sz;
  1197. while (count--)
  1198. *out_values++ = be32_to_cpup(val++);
  1199. return sz;
  1200. }
  1201. EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
  1202. /**
  1203. * of_property_read_u64 - Find and read a 64 bit integer from a property
  1204. * @np: device node from which the property value is to be read.
  1205. * @propname: name of the property to be searched.
  1206. * @out_value: pointer to return value, modified only if return value is 0.
  1207. *
  1208. * Search for a property in a device node and read a 64-bit value from
  1209. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1210. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1211. * property data isn't large enough.
  1212. *
  1213. * The out_value is modified only if a valid u64 value can be decoded.
  1214. */
  1215. int of_property_read_u64(const struct device_node *np, const char *propname,
  1216. u64 *out_value)
  1217. {
  1218. const __be32 *val = of_find_property_value_of_size(np, propname,
  1219. sizeof(*out_value),
  1220. 0,
  1221. NULL);
  1222. if (IS_ERR(val))
  1223. return PTR_ERR(val);
  1224. *out_value = of_read_number(val, 2);
  1225. return 0;
  1226. }
  1227. EXPORT_SYMBOL_GPL(of_property_read_u64);
  1228. /**
  1229. * of_property_read_variable_u64_array - Find and read an array of 64 bit
  1230. * integers from a property, with bounds on the minimum and maximum array size.
  1231. *
  1232. * @np: device node from which the property value is to be read.
  1233. * @propname: name of the property to be searched.
  1234. * @out_values: pointer to return value, modified only if return value is 0.
  1235. * @sz_min: minimum number of array elements to read
  1236. * @sz_max: maximum number of array elements to read, if zero there is no
  1237. * upper limit on the number of elements in the dts entry but only
  1238. * sz_min will be read.
  1239. *
  1240. * Search for a property in a device node and read 64-bit value(s) from
  1241. * it. Returns number of elements read on success, -EINVAL if the property
  1242. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  1243. * if the property data is smaller than sz_min or longer than sz_max.
  1244. *
  1245. * The out_values is modified only if a valid u64 value can be decoded.
  1246. */
  1247. int of_property_read_variable_u64_array(const struct device_node *np,
  1248. const char *propname, u64 *out_values,
  1249. size_t sz_min, size_t sz_max)
  1250. {
  1251. size_t sz, count;
  1252. const __be32 *val = of_find_property_value_of_size(np, propname,
  1253. (sz_min * sizeof(*out_values)),
  1254. (sz_max * sizeof(*out_values)),
  1255. &sz);
  1256. if (IS_ERR(val))
  1257. return PTR_ERR(val);
  1258. if (!sz_max)
  1259. sz = sz_min;
  1260. else
  1261. sz /= sizeof(*out_values);
  1262. count = sz;
  1263. while (count--) {
  1264. *out_values++ = of_read_number(val, 2);
  1265. val += 2;
  1266. }
  1267. return sz;
  1268. }
  1269. EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
  1270. /**
  1271. * of_property_read_string - Find and read a string from a property
  1272. * @np: device node from which the property value is to be read.
  1273. * @propname: name of the property to be searched.
  1274. * @out_string: pointer to null terminated return string, modified only if
  1275. * return value is 0.
  1276. *
  1277. * Search for a property in a device tree node and retrieve a null
  1278. * terminated string value (pointer to data, not a copy). Returns 0 on
  1279. * success, -EINVAL if the property does not exist, -ENODATA if property
  1280. * does not have a value, and -EILSEQ if the string is not null-terminated
  1281. * within the length of the property data.
  1282. *
  1283. * The out_string pointer is modified only if a valid string can be decoded.
  1284. */
  1285. int of_property_read_string(const struct device_node *np, const char *propname,
  1286. const char **out_string)
  1287. {
  1288. const struct property *prop = of_find_property(np, propname, NULL);
  1289. if (!prop)
  1290. return -EINVAL;
  1291. if (!prop->value)
  1292. return -ENODATA;
  1293. if (strnlen(prop->value, prop->length) >= prop->length)
  1294. return -EILSEQ;
  1295. *out_string = prop->value;
  1296. return 0;
  1297. }
  1298. EXPORT_SYMBOL_GPL(of_property_read_string);
  1299. /**
  1300. * of_property_match_string() - Find string in a list and return index
  1301. * @np: pointer to node containing string list property
  1302. * @propname: string list property name
  1303. * @string: pointer to string to search for in string list
  1304. *
  1305. * This function searches a string list property and returns the index
  1306. * of a specific string value.
  1307. */
  1308. int of_property_match_string(const struct device_node *np, const char *propname,
  1309. const char *string)
  1310. {
  1311. const struct property *prop = of_find_property(np, propname, NULL);
  1312. size_t l;
  1313. int i;
  1314. const char *p, *end;
  1315. if (!prop)
  1316. return -EINVAL;
  1317. if (!prop->value)
  1318. return -ENODATA;
  1319. p = prop->value;
  1320. end = p + prop->length;
  1321. for (i = 0; p < end; i++, p += l) {
  1322. l = strnlen(p, end - p) + 1;
  1323. if (p + l > end)
  1324. return -EILSEQ;
  1325. pr_debug("comparing %s with %s\n", string, p);
  1326. if (strcmp(string, p) == 0)
  1327. return i; /* Found it; return index */
  1328. }
  1329. return -ENODATA;
  1330. }
  1331. EXPORT_SYMBOL_GPL(of_property_match_string);
  1332. /**
  1333. * of_property_read_string_helper() - Utility helper for parsing string properties
  1334. * @np: device node from which the property value is to be read.
  1335. * @propname: name of the property to be searched.
  1336. * @out_strs: output array of string pointers.
  1337. * @sz: number of array elements to read.
  1338. * @skip: Number of strings to skip over at beginning of list.
  1339. *
  1340. * Don't call this function directly. It is a utility helper for the
  1341. * of_property_read_string*() family of functions.
  1342. */
  1343. int of_property_read_string_helper(const struct device_node *np,
  1344. const char *propname, const char **out_strs,
  1345. size_t sz, int skip)
  1346. {
  1347. const struct property *prop = of_find_property(np, propname, NULL);
  1348. int l = 0, i = 0;
  1349. const char *p, *end;
  1350. if (!prop)
  1351. return -EINVAL;
  1352. if (!prop->value)
  1353. return -ENODATA;
  1354. p = prop->value;
  1355. end = p + prop->length;
  1356. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  1357. l = strnlen(p, end - p) + 1;
  1358. if (p + l > end)
  1359. return -EILSEQ;
  1360. if (out_strs && i >= skip)
  1361. *out_strs++ = p;
  1362. }
  1363. i -= skip;
  1364. return i <= 0 ? -ENODATA : i;
  1365. }
  1366. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  1367. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1368. {
  1369. int i;
  1370. printk("%s %s", msg, of_node_full_name(args->np));
  1371. for (i = 0; i < args->args_count; i++) {
  1372. const char delim = i ? ',' : ':';
  1373. pr_cont("%c%08x", delim, args->args[i]);
  1374. }
  1375. pr_cont("\n");
  1376. }
  1377. int of_phandle_iterator_init(struct of_phandle_iterator *it,
  1378. const struct device_node *np,
  1379. const char *list_name,
  1380. const char *cells_name,
  1381. int cell_count)
  1382. {
  1383. const __be32 *list;
  1384. int size;
  1385. memset(it, 0, sizeof(*it));
  1386. list = of_get_property(np, list_name, &size);
  1387. if (!list)
  1388. return -ENOENT;
  1389. it->cells_name = cells_name;
  1390. it->cell_count = cell_count;
  1391. it->parent = np;
  1392. it->list_end = list + size / sizeof(*list);
  1393. it->phandle_end = list;
  1394. it->cur = list;
  1395. return 0;
  1396. }
  1397. int of_phandle_iterator_next(struct of_phandle_iterator *it)
  1398. {
  1399. uint32_t count = 0;
  1400. if (it->node) {
  1401. of_node_put(it->node);
  1402. it->node = NULL;
  1403. }
  1404. if (!it->cur || it->phandle_end >= it->list_end)
  1405. return -ENOENT;
  1406. it->cur = it->phandle_end;
  1407. /* If phandle is 0, then it is an empty entry with no arguments. */
  1408. it->phandle = be32_to_cpup(it->cur++);
  1409. if (it->phandle) {
  1410. /*
  1411. * Find the provider node and parse the #*-cells property to
  1412. * determine the argument length.
  1413. */
  1414. it->node = of_find_node_by_phandle(it->phandle);
  1415. if (it->cells_name) {
  1416. if (!it->node) {
  1417. pr_err("%s: could not find phandle\n",
  1418. it->parent->full_name);
  1419. goto err;
  1420. }
  1421. if (of_property_read_u32(it->node, it->cells_name,
  1422. &count)) {
  1423. pr_err("%s: could not get %s for %s\n",
  1424. it->parent->full_name,
  1425. it->cells_name,
  1426. it->node->full_name);
  1427. goto err;
  1428. }
  1429. } else {
  1430. count = it->cell_count;
  1431. }
  1432. /*
  1433. * Make sure that the arguments actually fit in the remaining
  1434. * property data length
  1435. */
  1436. if (it->cur + count > it->list_end) {
  1437. pr_err("%s: arguments longer than property\n",
  1438. it->parent->full_name);
  1439. goto err;
  1440. }
  1441. }
  1442. it->phandle_end = it->cur + count;
  1443. it->cur_count = count;
  1444. return 0;
  1445. err:
  1446. if (it->node) {
  1447. of_node_put(it->node);
  1448. it->node = NULL;
  1449. }
  1450. return -EINVAL;
  1451. }
  1452. int of_phandle_iterator_args(struct of_phandle_iterator *it,
  1453. uint32_t *args,
  1454. int size)
  1455. {
  1456. int i, count;
  1457. count = it->cur_count;
  1458. if (WARN_ON(size < count))
  1459. count = size;
  1460. for (i = 0; i < count; i++)
  1461. args[i] = be32_to_cpup(it->cur++);
  1462. return count;
  1463. }
  1464. static int __of_parse_phandle_with_args(const struct device_node *np,
  1465. const char *list_name,
  1466. const char *cells_name,
  1467. int cell_count, int index,
  1468. struct of_phandle_args *out_args)
  1469. {
  1470. struct of_phandle_iterator it;
  1471. int rc, cur_index = 0;
  1472. /* Loop over the phandles until all the requested entry is found */
  1473. of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
  1474. /*
  1475. * All of the error cases bail out of the loop, so at
  1476. * this point, the parsing is successful. If the requested
  1477. * index matches, then fill the out_args structure and return,
  1478. * or return -ENOENT for an empty entry.
  1479. */
  1480. rc = -ENOENT;
  1481. if (cur_index == index) {
  1482. if (!it.phandle)
  1483. goto err;
  1484. if (out_args) {
  1485. int c;
  1486. c = of_phandle_iterator_args(&it,
  1487. out_args->args,
  1488. MAX_PHANDLE_ARGS);
  1489. out_args->np = it.node;
  1490. out_args->args_count = c;
  1491. } else {
  1492. of_node_put(it.node);
  1493. }
  1494. /* Found it! return success */
  1495. return 0;
  1496. }
  1497. cur_index++;
  1498. }
  1499. /*
  1500. * Unlock node before returning result; will be one of:
  1501. * -ENOENT : index is for empty phandle
  1502. * -EINVAL : parsing error on data
  1503. */
  1504. err:
  1505. of_node_put(it.node);
  1506. return rc;
  1507. }
  1508. /**
  1509. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1510. * @np: Pointer to device node holding phandle property
  1511. * @phandle_name: Name of property holding a phandle value
  1512. * @index: For properties holding a table of phandles, this is the index into
  1513. * the table
  1514. *
  1515. * Returns the device_node pointer with refcount incremented. Use
  1516. * of_node_put() on it when done.
  1517. */
  1518. struct device_node *of_parse_phandle(const struct device_node *np,
  1519. const char *phandle_name, int index)
  1520. {
  1521. struct of_phandle_args args;
  1522. if (index < 0)
  1523. return NULL;
  1524. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1525. index, &args))
  1526. return NULL;
  1527. return args.np;
  1528. }
  1529. EXPORT_SYMBOL(of_parse_phandle);
  1530. /**
  1531. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1532. * @np: pointer to a device tree node containing a list
  1533. * @list_name: property name that contains a list
  1534. * @cells_name: property name that specifies phandles' arguments count
  1535. * @index: index of a phandle to parse out
  1536. * @out_args: optional pointer to output arguments structure (will be filled)
  1537. *
  1538. * This function is useful to parse lists of phandles and their arguments.
  1539. * Returns 0 on success and fills out_args, on error returns appropriate
  1540. * errno value.
  1541. *
  1542. * Caller is responsible to call of_node_put() on the returned out_args->np
  1543. * pointer.
  1544. *
  1545. * Example:
  1546. *
  1547. * phandle1: node1 {
  1548. * #list-cells = <2>;
  1549. * }
  1550. *
  1551. * phandle2: node2 {
  1552. * #list-cells = <1>;
  1553. * }
  1554. *
  1555. * node3 {
  1556. * list = <&phandle1 1 2 &phandle2 3>;
  1557. * }
  1558. *
  1559. * To get a device_node of the `node2' node you may call this:
  1560. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1561. */
  1562. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1563. const char *cells_name, int index,
  1564. struct of_phandle_args *out_args)
  1565. {
  1566. if (index < 0)
  1567. return -EINVAL;
  1568. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1569. index, out_args);
  1570. }
  1571. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1572. /**
  1573. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1574. * @np: pointer to a device tree node containing a list
  1575. * @list_name: property name that contains a list
  1576. * @cell_count: number of argument cells following the phandle
  1577. * @index: index of a phandle to parse out
  1578. * @out_args: optional pointer to output arguments structure (will be filled)
  1579. *
  1580. * This function is useful to parse lists of phandles and their arguments.
  1581. * Returns 0 on success and fills out_args, on error returns appropriate
  1582. * errno value.
  1583. *
  1584. * Caller is responsible to call of_node_put() on the returned out_args->np
  1585. * pointer.
  1586. *
  1587. * Example:
  1588. *
  1589. * phandle1: node1 {
  1590. * }
  1591. *
  1592. * phandle2: node2 {
  1593. * }
  1594. *
  1595. * node3 {
  1596. * list = <&phandle1 0 2 &phandle2 2 3>;
  1597. * }
  1598. *
  1599. * To get a device_node of the `node2' node you may call this:
  1600. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1601. */
  1602. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1603. const char *list_name, int cell_count,
  1604. int index, struct of_phandle_args *out_args)
  1605. {
  1606. if (index < 0)
  1607. return -EINVAL;
  1608. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1609. index, out_args);
  1610. }
  1611. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1612. /**
  1613. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1614. * @np: pointer to a device tree node containing a list
  1615. * @list_name: property name that contains a list
  1616. * @cells_name: property name that specifies phandles' arguments count
  1617. *
  1618. * Returns the number of phandle + argument tuples within a property. It
  1619. * is a typical pattern to encode a list of phandle and variable
  1620. * arguments into a single property. The number of arguments is encoded
  1621. * by a property in the phandle-target node. For example, a gpios
  1622. * property would contain a list of GPIO specifies consisting of a
  1623. * phandle and 1 or more arguments. The number of arguments are
  1624. * determined by the #gpio-cells property in the node pointed to by the
  1625. * phandle.
  1626. */
  1627. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1628. const char *cells_name)
  1629. {
  1630. struct of_phandle_iterator it;
  1631. int rc, cur_index = 0;
  1632. rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
  1633. if (rc)
  1634. return rc;
  1635. while ((rc = of_phandle_iterator_next(&it)) == 0)
  1636. cur_index += 1;
  1637. if (rc != -ENOENT)
  1638. return rc;
  1639. return cur_index;
  1640. }
  1641. EXPORT_SYMBOL(of_count_phandle_with_args);
  1642. /**
  1643. * __of_add_property - Add a property to a node without lock operations
  1644. */
  1645. int __of_add_property(struct device_node *np, struct property *prop)
  1646. {
  1647. struct property **next;
  1648. prop->next = NULL;
  1649. next = &np->properties;
  1650. while (*next) {
  1651. if (strcmp(prop->name, (*next)->name) == 0)
  1652. /* duplicate ! don't insert it */
  1653. return -EEXIST;
  1654. next = &(*next)->next;
  1655. }
  1656. *next = prop;
  1657. return 0;
  1658. }
  1659. /**
  1660. * of_add_property - Add a property to a node
  1661. */
  1662. int of_add_property(struct device_node *np, struct property *prop)
  1663. {
  1664. unsigned long flags;
  1665. int rc;
  1666. mutex_lock(&of_mutex);
  1667. raw_spin_lock_irqsave(&devtree_lock, flags);
  1668. rc = __of_add_property(np, prop);
  1669. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1670. if (!rc)
  1671. __of_add_property_sysfs(np, prop);
  1672. mutex_unlock(&of_mutex);
  1673. if (!rc)
  1674. of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
  1675. return rc;
  1676. }
  1677. int __of_remove_property(struct device_node *np, struct property *prop)
  1678. {
  1679. struct property **next;
  1680. for (next = &np->properties; *next; next = &(*next)->next) {
  1681. if (*next == prop)
  1682. break;
  1683. }
  1684. if (*next == NULL)
  1685. return -ENODEV;
  1686. /* found the node */
  1687. *next = prop->next;
  1688. prop->next = np->deadprops;
  1689. np->deadprops = prop;
  1690. return 0;
  1691. }
  1692. void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
  1693. {
  1694. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1695. kfree(prop->attr.attr.name);
  1696. }
  1697. void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
  1698. {
  1699. if (!IS_ENABLED(CONFIG_SYSFS))
  1700. return;
  1701. /* at early boot, bail here and defer setup to of_init() */
  1702. if (of_kset && of_node_is_attached(np))
  1703. __of_sysfs_remove_bin_file(np, prop);
  1704. }
  1705. /**
  1706. * of_remove_property - Remove a property from a node.
  1707. *
  1708. * Note that we don't actually remove it, since we have given out
  1709. * who-knows-how-many pointers to the data using get-property.
  1710. * Instead we just move the property to the "dead properties"
  1711. * list, so it won't be found any more.
  1712. */
  1713. int of_remove_property(struct device_node *np, struct property *prop)
  1714. {
  1715. unsigned long flags;
  1716. int rc;
  1717. if (!prop)
  1718. return -ENODEV;
  1719. mutex_lock(&of_mutex);
  1720. raw_spin_lock_irqsave(&devtree_lock, flags);
  1721. rc = __of_remove_property(np, prop);
  1722. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1723. if (!rc)
  1724. __of_remove_property_sysfs(np, prop);
  1725. mutex_unlock(&of_mutex);
  1726. if (!rc)
  1727. of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
  1728. return rc;
  1729. }
  1730. int __of_update_property(struct device_node *np, struct property *newprop,
  1731. struct property **oldpropp)
  1732. {
  1733. struct property **next, *oldprop;
  1734. for (next = &np->properties; *next; next = &(*next)->next) {
  1735. if (of_prop_cmp((*next)->name, newprop->name) == 0)
  1736. break;
  1737. }
  1738. *oldpropp = oldprop = *next;
  1739. if (oldprop) {
  1740. /* replace the node */
  1741. newprop->next = oldprop->next;
  1742. *next = newprop;
  1743. oldprop->next = np->deadprops;
  1744. np->deadprops = oldprop;
  1745. } else {
  1746. /* new node */
  1747. newprop->next = NULL;
  1748. *next = newprop;
  1749. }
  1750. return 0;
  1751. }
  1752. void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
  1753. struct property *oldprop)
  1754. {
  1755. if (!IS_ENABLED(CONFIG_SYSFS))
  1756. return;
  1757. /* At early boot, bail out and defer setup to of_init() */
  1758. if (!of_kset)
  1759. return;
  1760. if (oldprop)
  1761. __of_sysfs_remove_bin_file(np, oldprop);
  1762. __of_add_property_sysfs(np, newprop);
  1763. }
  1764. /*
  1765. * of_update_property - Update a property in a node, if the property does
  1766. * not exist, add it.
  1767. *
  1768. * Note that we don't actually remove it, since we have given out
  1769. * who-knows-how-many pointers to the data using get-property.
  1770. * Instead we just move the property to the "dead properties" list,
  1771. * and add the new property to the property list
  1772. */
  1773. int of_update_property(struct device_node *np, struct property *newprop)
  1774. {
  1775. struct property *oldprop;
  1776. unsigned long flags;
  1777. int rc;
  1778. if (!newprop->name)
  1779. return -EINVAL;
  1780. mutex_lock(&of_mutex);
  1781. raw_spin_lock_irqsave(&devtree_lock, flags);
  1782. rc = __of_update_property(np, newprop, &oldprop);
  1783. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1784. if (!rc)
  1785. __of_update_property_sysfs(np, newprop, oldprop);
  1786. mutex_unlock(&of_mutex);
  1787. if (!rc)
  1788. of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
  1789. return rc;
  1790. }
  1791. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1792. int id, const char *stem, int stem_len)
  1793. {
  1794. ap->np = np;
  1795. ap->id = id;
  1796. strncpy(ap->stem, stem, stem_len);
  1797. ap->stem[stem_len] = 0;
  1798. list_add_tail(&ap->link, &aliases_lookup);
  1799. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1800. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1801. }
  1802. /**
  1803. * of_alias_scan - Scan all properties of the 'aliases' node
  1804. *
  1805. * The function scans all the properties of the 'aliases' node and populates
  1806. * the global lookup table with the properties. It returns the
  1807. * number of alias properties found, or an error code in case of failure.
  1808. *
  1809. * @dt_alloc: An allocator that provides a virtual address to memory
  1810. * for storing the resulting tree
  1811. */
  1812. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1813. {
  1814. struct property *pp;
  1815. of_aliases = of_find_node_by_path("/aliases");
  1816. of_chosen = of_find_node_by_path("/chosen");
  1817. if (of_chosen == NULL)
  1818. of_chosen = of_find_node_by_path("/chosen@0");
  1819. if (of_chosen) {
  1820. /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
  1821. const char *name = of_get_property(of_chosen, "stdout-path", NULL);
  1822. if (!name)
  1823. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1824. if (IS_ENABLED(CONFIG_PPC) && !name)
  1825. name = of_get_property(of_aliases, "stdout", NULL);
  1826. if (name)
  1827. of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
  1828. }
  1829. if (!of_aliases)
  1830. return;
  1831. for_each_property_of_node(of_aliases, pp) {
  1832. const char *start = pp->name;
  1833. const char *end = start + strlen(start);
  1834. struct device_node *np;
  1835. struct alias_prop *ap;
  1836. int id, len;
  1837. /* Skip those we do not want to proceed */
  1838. if (!strcmp(pp->name, "name") ||
  1839. !strcmp(pp->name, "phandle") ||
  1840. !strcmp(pp->name, "linux,phandle"))
  1841. continue;
  1842. np = of_find_node_by_path(pp->value);
  1843. if (!np)
  1844. continue;
  1845. /* walk the alias backwards to extract the id and work out
  1846. * the 'stem' string */
  1847. while (isdigit(*(end-1)) && end > start)
  1848. end--;
  1849. len = end - start;
  1850. if (kstrtoint(end, 10, &id) < 0)
  1851. continue;
  1852. /* Allocate an alias_prop with enough space for the stem */
  1853. ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
  1854. if (!ap)
  1855. continue;
  1856. memset(ap, 0, sizeof(*ap) + len + 1);
  1857. ap->alias = start;
  1858. of_alias_add(ap, np, id, start, len);
  1859. }
  1860. }
  1861. /**
  1862. * of_alias_get_id - Get alias id for the given device_node
  1863. * @np: Pointer to the given device_node
  1864. * @stem: Alias stem of the given device_node
  1865. *
  1866. * The function travels the lookup table to get the alias id for the given
  1867. * device_node and alias stem. It returns the alias id if found.
  1868. */
  1869. int of_alias_get_id(struct device_node *np, const char *stem)
  1870. {
  1871. struct alias_prop *app;
  1872. int id = -ENODEV;
  1873. mutex_lock(&of_mutex);
  1874. list_for_each_entry(app, &aliases_lookup, link) {
  1875. if (strcmp(app->stem, stem) != 0)
  1876. continue;
  1877. if (np == app->np) {
  1878. id = app->id;
  1879. break;
  1880. }
  1881. }
  1882. mutex_unlock(&of_mutex);
  1883. return id;
  1884. }
  1885. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1886. /**
  1887. * of_alias_get_highest_id - Get highest alias id for the given stem
  1888. * @stem: Alias stem to be examined
  1889. *
  1890. * The function travels the lookup table to get the highest alias id for the
  1891. * given alias stem. It returns the alias id if found.
  1892. */
  1893. int of_alias_get_highest_id(const char *stem)
  1894. {
  1895. struct alias_prop *app;
  1896. int id = -ENODEV;
  1897. mutex_lock(&of_mutex);
  1898. list_for_each_entry(app, &aliases_lookup, link) {
  1899. if (strcmp(app->stem, stem) != 0)
  1900. continue;
  1901. if (app->id > id)
  1902. id = app->id;
  1903. }
  1904. mutex_unlock(&of_mutex);
  1905. return id;
  1906. }
  1907. EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
  1908. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1909. u32 *pu)
  1910. {
  1911. const void *curv = cur;
  1912. if (!prop)
  1913. return NULL;
  1914. if (!cur) {
  1915. curv = prop->value;
  1916. goto out_val;
  1917. }
  1918. curv += sizeof(*cur);
  1919. if (curv >= prop->value + prop->length)
  1920. return NULL;
  1921. out_val:
  1922. *pu = be32_to_cpup(curv);
  1923. return curv;
  1924. }
  1925. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1926. const char *of_prop_next_string(struct property *prop, const char *cur)
  1927. {
  1928. const void *curv = cur;
  1929. if (!prop)
  1930. return NULL;
  1931. if (!cur)
  1932. return prop->value;
  1933. curv += strlen(cur) + 1;
  1934. if (curv >= prop->value + prop->length)
  1935. return NULL;
  1936. return curv;
  1937. }
  1938. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1939. /**
  1940. * of_console_check() - Test and setup console for DT setup
  1941. * @dn - Pointer to device node
  1942. * @name - Name to use for preferred console without index. ex. "ttyS"
  1943. * @index - Index to use for preferred console.
  1944. *
  1945. * Check if the given device node matches the stdout-path property in the
  1946. * /chosen node. If it does then register it as the preferred console and return
  1947. * TRUE. Otherwise return FALSE.
  1948. */
  1949. bool of_console_check(struct device_node *dn, char *name, int index)
  1950. {
  1951. if (!dn || dn != of_stdout || console_set_on_cmdline)
  1952. return false;
  1953. return !add_preferred_console(name, index,
  1954. kstrdup(of_stdout_options, GFP_KERNEL));
  1955. }
  1956. EXPORT_SYMBOL_GPL(of_console_check);
  1957. /**
  1958. * of_find_next_cache_node - Find a node's subsidiary cache
  1959. * @np: node of type "cpu" or "cache"
  1960. *
  1961. * Returns a node pointer with refcount incremented, use
  1962. * of_node_put() on it when done. Caller should hold a reference
  1963. * to np.
  1964. */
  1965. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1966. {
  1967. struct device_node *child;
  1968. const phandle *handle;
  1969. handle = of_get_property(np, "l2-cache", NULL);
  1970. if (!handle)
  1971. handle = of_get_property(np, "next-level-cache", NULL);
  1972. if (handle)
  1973. return of_find_node_by_phandle(be32_to_cpup(handle));
  1974. /* OF on pmac has nodes instead of properties named "l2-cache"
  1975. * beneath CPU nodes.
  1976. */
  1977. if (!strcmp(np->type, "cpu"))
  1978. for_each_child_of_node(np, child)
  1979. if (!strcmp(child->type, "cache"))
  1980. return child;
  1981. return NULL;
  1982. }
  1983. /**
  1984. * of_find_last_cache_level - Find the level at which the last cache is
  1985. * present for the given logical cpu
  1986. *
  1987. * @cpu: cpu number(logical index) for which the last cache level is needed
  1988. *
  1989. * Returns the the level at which the last cache is present. It is exactly
  1990. * same as the total number of cache levels for the given logical cpu.
  1991. */
  1992. int of_find_last_cache_level(unsigned int cpu)
  1993. {
  1994. u32 cache_level = 0;
  1995. struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
  1996. while (np) {
  1997. prev = np;
  1998. of_node_put(np);
  1999. np = of_find_next_cache_node(np);
  2000. }
  2001. of_property_read_u32(prev, "cache-level", &cache_level);
  2002. return cache_level;
  2003. }
  2004. /**
  2005. * of_graph_parse_endpoint() - parse common endpoint node properties
  2006. * @node: pointer to endpoint device_node
  2007. * @endpoint: pointer to the OF endpoint data structure
  2008. *
  2009. * The caller should hold a reference to @node.
  2010. */
  2011. int of_graph_parse_endpoint(const struct device_node *node,
  2012. struct of_endpoint *endpoint)
  2013. {
  2014. struct device_node *port_node = of_get_parent(node);
  2015. WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
  2016. __func__, node->full_name);
  2017. memset(endpoint, 0, sizeof(*endpoint));
  2018. endpoint->local_node = node;
  2019. /*
  2020. * It doesn't matter whether the two calls below succeed.
  2021. * If they don't then the default value 0 is used.
  2022. */
  2023. of_property_read_u32(port_node, "reg", &endpoint->port);
  2024. of_property_read_u32(node, "reg", &endpoint->id);
  2025. of_node_put(port_node);
  2026. return 0;
  2027. }
  2028. EXPORT_SYMBOL(of_graph_parse_endpoint);
  2029. /**
  2030. * of_graph_get_port_by_id() - get the port matching a given id
  2031. * @parent: pointer to the parent device node
  2032. * @id: id of the port
  2033. *
  2034. * Return: A 'port' node pointer with refcount incremented. The caller
  2035. * has to use of_node_put() on it when done.
  2036. */
  2037. struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
  2038. {
  2039. struct device_node *node, *port;
  2040. node = of_get_child_by_name(parent, "ports");
  2041. if (node)
  2042. parent = node;
  2043. for_each_child_of_node(parent, port) {
  2044. u32 port_id = 0;
  2045. if (of_node_cmp(port->name, "port") != 0)
  2046. continue;
  2047. of_property_read_u32(port, "reg", &port_id);
  2048. if (id == port_id)
  2049. break;
  2050. }
  2051. of_node_put(node);
  2052. return port;
  2053. }
  2054. EXPORT_SYMBOL(of_graph_get_port_by_id);
  2055. /**
  2056. * of_graph_get_next_endpoint() - get next endpoint node
  2057. * @parent: pointer to the parent device node
  2058. * @prev: previous endpoint node, or NULL to get first
  2059. *
  2060. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  2061. * of the passed @prev node is decremented.
  2062. */
  2063. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  2064. struct device_node *prev)
  2065. {
  2066. struct device_node *endpoint;
  2067. struct device_node *port;
  2068. if (!parent)
  2069. return NULL;
  2070. /*
  2071. * Start by locating the port node. If no previous endpoint is specified
  2072. * search for the first port node, otherwise get the previous endpoint
  2073. * parent port node.
  2074. */
  2075. if (!prev) {
  2076. struct device_node *node;
  2077. node = of_get_child_by_name(parent, "ports");
  2078. if (node)
  2079. parent = node;
  2080. port = of_get_child_by_name(parent, "port");
  2081. of_node_put(node);
  2082. if (!port) {
  2083. pr_err("graph: no port node found in %s\n",
  2084. parent->full_name);
  2085. return NULL;
  2086. }
  2087. } else {
  2088. port = of_get_parent(prev);
  2089. if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
  2090. __func__, prev->full_name))
  2091. return NULL;
  2092. }
  2093. while (1) {
  2094. /*
  2095. * Now that we have a port node, get the next endpoint by
  2096. * getting the next child. If the previous endpoint is NULL this
  2097. * will return the first child.
  2098. */
  2099. endpoint = of_get_next_child(port, prev);
  2100. if (endpoint) {
  2101. of_node_put(port);
  2102. return endpoint;
  2103. }
  2104. /* No more endpoints under this port, try the next one. */
  2105. prev = NULL;
  2106. do {
  2107. port = of_get_next_child(parent, port);
  2108. if (!port)
  2109. return NULL;
  2110. } while (of_node_cmp(port->name, "port"));
  2111. }
  2112. }
  2113. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  2114. /**
  2115. * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
  2116. * @parent: pointer to the parent device node
  2117. * @port_reg: identifier (value of reg property) of the parent port node
  2118. * @reg: identifier (value of reg property) of the endpoint node
  2119. *
  2120. * Return: An 'endpoint' node pointer which is identified by reg and at the same
  2121. * is the child of a port node identified by port_reg. reg and port_reg are
  2122. * ignored when they are -1.
  2123. */
  2124. struct device_node *of_graph_get_endpoint_by_regs(
  2125. const struct device_node *parent, int port_reg, int reg)
  2126. {
  2127. struct of_endpoint endpoint;
  2128. struct device_node *node = NULL;
  2129. for_each_endpoint_of_node(parent, node) {
  2130. of_graph_parse_endpoint(node, &endpoint);
  2131. if (((port_reg == -1) || (endpoint.port == port_reg)) &&
  2132. ((reg == -1) || (endpoint.id == reg)))
  2133. return node;
  2134. }
  2135. return NULL;
  2136. }
  2137. EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
  2138. /**
  2139. * of_graph_get_remote_port_parent() - get remote port's parent node
  2140. * @node: pointer to a local endpoint device_node
  2141. *
  2142. * Return: Remote device node associated with remote endpoint node linked
  2143. * to @node. Use of_node_put() on it when done.
  2144. */
  2145. struct device_node *of_graph_get_remote_port_parent(
  2146. const struct device_node *node)
  2147. {
  2148. struct device_node *np;
  2149. unsigned int depth;
  2150. /* Get remote endpoint node. */
  2151. np = of_parse_phandle(node, "remote-endpoint", 0);
  2152. /* Walk 3 levels up only if there is 'ports' node. */
  2153. for (depth = 3; depth && np; depth--) {
  2154. np = of_get_next_parent(np);
  2155. if (depth == 2 && of_node_cmp(np->name, "ports"))
  2156. break;
  2157. }
  2158. return np;
  2159. }
  2160. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  2161. /**
  2162. * of_graph_get_remote_port() - get remote port node
  2163. * @node: pointer to a local endpoint device_node
  2164. *
  2165. * Return: Remote port node associated with remote endpoint node linked
  2166. * to @node. Use of_node_put() on it when done.
  2167. */
  2168. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  2169. {
  2170. struct device_node *np;
  2171. /* Get remote endpoint node. */
  2172. np = of_parse_phandle(node, "remote-endpoint", 0);
  2173. if (!np)
  2174. return NULL;
  2175. return of_get_next_parent(np);
  2176. }
  2177. EXPORT_SYMBOL(of_graph_get_remote_port);
  2178. /**
  2179. * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
  2180. * @node: pointer to parent device_node containing graph port/endpoint
  2181. * @port: identifier (value of reg property) of the parent port node
  2182. * @endpoint: identifier (value of reg property) of the endpoint node
  2183. *
  2184. * Return: Remote device node associated with remote endpoint node linked
  2185. * to @node. Use of_node_put() on it when done.
  2186. */
  2187. struct device_node *of_graph_get_remote_node(const struct device_node *node,
  2188. u32 port, u32 endpoint)
  2189. {
  2190. struct device_node *endpoint_node, *remote;
  2191. endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
  2192. if (!endpoint_node) {
  2193. pr_debug("no valid endpoint (%d, %d) for node %s\n",
  2194. port, endpoint, node->full_name);
  2195. return NULL;
  2196. }
  2197. remote = of_graph_get_remote_port_parent(endpoint_node);
  2198. of_node_put(endpoint_node);
  2199. if (!remote) {
  2200. pr_debug("no valid remote node\n");
  2201. return NULL;
  2202. }
  2203. if (!of_device_is_available(remote)) {
  2204. pr_debug("not available for remote node\n");
  2205. return NULL;
  2206. }
  2207. return remote;
  2208. }
  2209. EXPORT_SYMBOL(of_graph_get_remote_node);