dynamic.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for dynamic device trees.
  4. *
  5. * On some platforms, the device tree can be manipulated at runtime.
  6. * The routines in this section support adding, removing and changing
  7. * device tree nodes.
  8. */
  9. #define pr_fmt(fmt) "OF: " fmt
  10. #include <linux/cleanup.h>
  11. #include <linux/device.h>
  12. #include <linux/of.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/proc_fs.h>
  17. #include "of_private.h"
  18. static struct device_node *kobj_to_device_node(struct kobject *kobj)
  19. {
  20. return container_of(kobj, struct device_node, kobj);
  21. }
  22. /**
  23. * of_node_get() - Increment refcount of a node
  24. * @node: Node to inc refcount, NULL is supported to simplify writing of
  25. * callers
  26. *
  27. * Return: The node with refcount incremented.
  28. */
  29. struct device_node *of_node_get(struct device_node *node)
  30. {
  31. if (node)
  32. kobject_get(&node->kobj);
  33. return node;
  34. }
  35. EXPORT_SYMBOL(of_node_get);
  36. /**
  37. * of_node_put() - Decrement refcount of a node
  38. * @node: Node to dec refcount, NULL is supported to simplify writing of
  39. * callers
  40. */
  41. void of_node_put(struct device_node *node)
  42. {
  43. if (node)
  44. kobject_put(&node->kobj);
  45. }
  46. EXPORT_SYMBOL(of_node_put);
  47. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  48. int of_reconfig_notifier_register(struct notifier_block *nb)
  49. {
  50. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  51. }
  52. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  53. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  54. {
  55. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  56. }
  57. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  58. static const char *action_names[] = {
  59. [0] = "INVALID",
  60. [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
  61. [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
  62. [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
  63. [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
  64. [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
  65. };
  66. #define _do_print(func, prefix, action, node, prop, ...) ({ \
  67. func("changeset: " prefix "%-15s %pOF%s%s\n", \
  68. ##__VA_ARGS__, action_names[action], node, \
  69. prop ? ":" : "", prop ? prop->name : ""); \
  70. })
  71. #define of_changeset_action_err(...) _do_print(pr_err, __VA_ARGS__)
  72. #define of_changeset_action_debug(...) _do_print(pr_debug, __VA_ARGS__)
  73. int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
  74. {
  75. int rc;
  76. struct of_reconfig_data *pr = p;
  77. of_changeset_action_debug("notify: ", action, pr->dn, pr->prop);
  78. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  79. return notifier_to_errno(rc);
  80. }
  81. /*
  82. * of_reconfig_get_state_change() - Returns new state of device
  83. * @action - action of the of notifier
  84. * @arg - argument of the of notifier
  85. *
  86. * Returns the new state of a device based on the notifier used.
  87. *
  88. * Return: OF_RECONFIG_CHANGE_REMOVE on device going from enabled to
  89. * disabled, OF_RECONFIG_CHANGE_ADD on device going from disabled to
  90. * enabled and OF_RECONFIG_NO_CHANGE on no change.
  91. */
  92. int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
  93. {
  94. struct property *prop, *old_prop = NULL;
  95. int is_status, status_state, old_status_state, prev_state, new_state;
  96. /* figure out if a device should be created or destroyed */
  97. switch (action) {
  98. case OF_RECONFIG_ATTACH_NODE:
  99. case OF_RECONFIG_DETACH_NODE:
  100. prop = of_find_property(pr->dn, "status", NULL);
  101. break;
  102. case OF_RECONFIG_ADD_PROPERTY:
  103. case OF_RECONFIG_REMOVE_PROPERTY:
  104. prop = pr->prop;
  105. break;
  106. case OF_RECONFIG_UPDATE_PROPERTY:
  107. prop = pr->prop;
  108. old_prop = pr->old_prop;
  109. break;
  110. default:
  111. return OF_RECONFIG_NO_CHANGE;
  112. }
  113. is_status = 0;
  114. status_state = -1;
  115. old_status_state = -1;
  116. prev_state = -1;
  117. new_state = -1;
  118. if (prop && !strcmp(prop->name, "status")) {
  119. is_status = 1;
  120. status_state = !strcmp(prop->value, "okay") ||
  121. !strcmp(prop->value, "ok");
  122. if (old_prop)
  123. old_status_state = !strcmp(old_prop->value, "okay") ||
  124. !strcmp(old_prop->value, "ok");
  125. }
  126. switch (action) {
  127. case OF_RECONFIG_ATTACH_NODE:
  128. prev_state = 0;
  129. /* -1 & 0 status either missing or okay */
  130. new_state = status_state != 0;
  131. break;
  132. case OF_RECONFIG_DETACH_NODE:
  133. /* -1 & 0 status either missing or okay */
  134. prev_state = status_state != 0;
  135. new_state = 0;
  136. break;
  137. case OF_RECONFIG_ADD_PROPERTY:
  138. if (is_status) {
  139. /* no status property -> enabled (legacy) */
  140. prev_state = 1;
  141. new_state = status_state;
  142. }
  143. break;
  144. case OF_RECONFIG_REMOVE_PROPERTY:
  145. if (is_status) {
  146. prev_state = status_state;
  147. /* no status property -> enabled (legacy) */
  148. new_state = 1;
  149. }
  150. break;
  151. case OF_RECONFIG_UPDATE_PROPERTY:
  152. if (is_status) {
  153. prev_state = old_status_state != 0;
  154. new_state = status_state != 0;
  155. }
  156. break;
  157. }
  158. if (prev_state == new_state)
  159. return OF_RECONFIG_NO_CHANGE;
  160. return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
  161. }
  162. EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
  163. int of_property_notify(int action, struct device_node *np,
  164. struct property *prop, struct property *oldprop)
  165. {
  166. struct of_reconfig_data pr;
  167. /* only call notifiers if the node is attached */
  168. if (!of_node_is_attached(np))
  169. return 0;
  170. pr.dn = np;
  171. pr.prop = prop;
  172. pr.old_prop = oldprop;
  173. return of_reconfig_notify(action, &pr);
  174. }
  175. static void __of_attach_node(struct device_node *np)
  176. {
  177. const __be32 *phandle;
  178. int sz;
  179. unsigned long flags;
  180. raw_spin_lock_irqsave(&devtree_lock, flags);
  181. if (!of_node_check_flag(np, OF_OVERLAY)) {
  182. np->name = __of_get_property(np, "name", NULL);
  183. if (!np->name)
  184. np->name = "<NULL>";
  185. phandle = __of_get_property(np, "phandle", &sz);
  186. if (!phandle)
  187. phandle = __of_get_property(np, "linux,phandle", &sz);
  188. if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
  189. phandle = __of_get_property(np, "ibm,phandle", &sz);
  190. if (phandle && (sz >= 4))
  191. np->phandle = be32_to_cpup(phandle);
  192. else
  193. np->phandle = 0;
  194. }
  195. np->child = NULL;
  196. np->sibling = np->parent->child;
  197. np->parent->child = np;
  198. of_node_clear_flag(np, OF_DETACHED);
  199. np->fwnode.flags |= FWNODE_FLAG_NOT_DEVICE;
  200. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  201. __of_attach_node_sysfs(np);
  202. }
  203. /**
  204. * of_attach_node() - Plug a device node into the tree and global list.
  205. * @np: Pointer to the caller's Device Node
  206. */
  207. int of_attach_node(struct device_node *np)
  208. {
  209. struct of_reconfig_data rd;
  210. memset(&rd, 0, sizeof(rd));
  211. rd.dn = np;
  212. mutex_lock(&of_mutex);
  213. __of_attach_node(np);
  214. mutex_unlock(&of_mutex);
  215. of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
  216. return 0;
  217. }
  218. void __of_detach_node(struct device_node *np)
  219. {
  220. struct device_node *parent;
  221. unsigned long flags;
  222. raw_spin_lock_irqsave(&devtree_lock, flags);
  223. parent = np->parent;
  224. if (WARN_ON(of_node_check_flag(np, OF_DETACHED) || !parent)) {
  225. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  226. return;
  227. }
  228. if (parent->child == np)
  229. parent->child = np->sibling;
  230. else {
  231. struct device_node *prevsib;
  232. for (prevsib = np->parent->child;
  233. prevsib->sibling != np;
  234. prevsib = prevsib->sibling)
  235. ;
  236. prevsib->sibling = np->sibling;
  237. }
  238. of_node_set_flag(np, OF_DETACHED);
  239. /* race with of_find_node_by_phandle() prevented by devtree_lock */
  240. __of_phandle_cache_inv_entry(np->phandle);
  241. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  242. __of_detach_node_sysfs(np);
  243. }
  244. /**
  245. * of_detach_node() - "Unplug" a node from the device tree.
  246. * @np: Pointer to the caller's Device Node
  247. */
  248. int of_detach_node(struct device_node *np)
  249. {
  250. struct of_reconfig_data rd;
  251. memset(&rd, 0, sizeof(rd));
  252. rd.dn = np;
  253. mutex_lock(&of_mutex);
  254. __of_detach_node(np);
  255. mutex_unlock(&of_mutex);
  256. of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(of_detach_node);
  260. void __of_prop_free(struct property *prop)
  261. {
  262. kfree(prop->name);
  263. kfree(prop->value);
  264. kfree(prop);
  265. }
  266. static void property_list_free(struct property *prop_list)
  267. {
  268. struct property *prop, *next;
  269. for (prop = prop_list; prop != NULL; prop = next) {
  270. next = prop->next;
  271. __of_prop_free(prop);
  272. }
  273. }
  274. /**
  275. * of_node_release() - release a dynamically allocated node
  276. * @kobj: kernel object of the node to be released
  277. *
  278. * In of_node_put() this function is passed to kref_put() as the destructor.
  279. */
  280. void of_node_release(struct kobject *kobj)
  281. {
  282. struct device_node *node = kobj_to_device_node(kobj);
  283. /*
  284. * can not use '"%pOF", node' in pr_err() calls from this function
  285. * because an of_node_get(node) when refcount is already zero
  286. * will result in an error and a stack dump
  287. */
  288. /* We should never be releasing nodes that haven't been detached. */
  289. if (!of_node_check_flag(node, OF_DETACHED)) {
  290. pr_err("ERROR: %s() detected bad of_node_put() on %pOF/%s\n",
  291. __func__, node->parent, node->full_name);
  292. /*
  293. * of unittests will test this path. Do not print the stack
  294. * trace when the error is caused by unittest so that we do
  295. * not display what a normal developer might reasonably
  296. * consider a real bug.
  297. */
  298. if (!IS_ENABLED(CONFIG_OF_UNITTEST) ||
  299. strcmp(node->parent->full_name, "testcase-data")) {
  300. dump_stack();
  301. pr_err("ERROR: next of_node_put() on this node will result in a kobject warning 'refcount_t: underflow; use-after-free.'\n");
  302. }
  303. return;
  304. }
  305. if (!of_node_check_flag(node, OF_DYNAMIC))
  306. return;
  307. if (of_node_check_flag(node, OF_OVERLAY)) {
  308. if (!of_node_check_flag(node, OF_OVERLAY_FREE_CSET)) {
  309. /* premature refcount of zero, do not free memory */
  310. pr_err("ERROR: memory leak before free overlay changeset, %pOF\n",
  311. node);
  312. return;
  313. }
  314. /*
  315. * If node->properties non-empty then properties were added
  316. * to this node either by different overlay that has not
  317. * yet been removed, or by a non-overlay mechanism.
  318. */
  319. if (node->properties)
  320. pr_err("ERROR: %s(), unexpected properties in %pOF\n",
  321. __func__, node);
  322. }
  323. if (node->child)
  324. pr_err("ERROR: %s() unexpected children for %pOF/%s\n",
  325. __func__, node->parent, node->full_name);
  326. property_list_free(node->properties);
  327. property_list_free(node->deadprops);
  328. fwnode_links_purge(of_fwnode_handle(node));
  329. kfree(node->full_name);
  330. kfree(node->data);
  331. kfree(node);
  332. }
  333. /**
  334. * __of_prop_dup - Copy a property dynamically.
  335. * @prop: Property to copy
  336. * @allocflags: Allocation flags (typically pass GFP_KERNEL)
  337. *
  338. * Copy a property by dynamically allocating the memory of both the
  339. * property structure and the property name & contents. The property's
  340. * flags have the OF_DYNAMIC bit set so that we can differentiate between
  341. * dynamically allocated properties and not.
  342. *
  343. * Return: The newly allocated property or NULL on out of memory error.
  344. */
  345. struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
  346. {
  347. struct property *new;
  348. new = kzalloc_obj(*new, allocflags);
  349. if (!new)
  350. return NULL;
  351. /*
  352. * NOTE: There is no check for zero length value.
  353. * In case of a boolean property, this will allocate a value
  354. * of zero bytes. We do this to work around the use
  355. * of of_get_property() calls on boolean values.
  356. */
  357. new->name = kstrdup(prop->name, allocflags);
  358. new->value = kmemdup(prop->value, prop->length, allocflags);
  359. new->length = prop->length;
  360. if (!new->name || !new->value)
  361. goto err_free;
  362. /* mark the property as dynamic */
  363. of_property_set_flag(new, OF_DYNAMIC);
  364. return new;
  365. err_free:
  366. __of_prop_free(new);
  367. return NULL;
  368. }
  369. /**
  370. * __of_node_dup() - Duplicate or create an empty device node dynamically.
  371. * @np: if not NULL, contains properties to be duplicated in new node
  372. * @full_name: string value to be duplicated into new node's full_name field
  373. *
  374. * Create a device tree node, optionally duplicating the properties of
  375. * another node. The node data are dynamically allocated and all the node
  376. * flags have the OF_DYNAMIC & OF_DETACHED bits set.
  377. *
  378. * Return: The newly allocated node or NULL on out of memory error. Use
  379. * of_node_put() on it when done to free the memory allocated for it.
  380. */
  381. struct device_node *__of_node_dup(const struct device_node *np,
  382. const char *full_name)
  383. {
  384. struct device_node *node;
  385. node = kzalloc_obj(*node);
  386. if (!node)
  387. return NULL;
  388. node->full_name = kstrdup(full_name, GFP_KERNEL);
  389. if (!node->full_name) {
  390. kfree(node);
  391. return NULL;
  392. }
  393. of_node_set_flag(node, OF_DYNAMIC);
  394. of_node_set_flag(node, OF_DETACHED);
  395. of_node_init(node);
  396. /* Iterate over and duplicate all properties */
  397. if (np) {
  398. struct property *pp, *new_pp;
  399. for_each_property_of_node(np, pp) {
  400. new_pp = __of_prop_dup(pp, GFP_KERNEL);
  401. if (!new_pp)
  402. goto err_prop;
  403. if (__of_add_property(node, new_pp)) {
  404. __of_prop_free(new_pp);
  405. goto err_prop;
  406. }
  407. }
  408. }
  409. return node;
  410. err_prop:
  411. of_node_put(node); /* Frees the node and properties */
  412. return NULL;
  413. }
  414. /**
  415. * of_changeset_create_node - Dynamically create a device node and attach to
  416. * a given changeset.
  417. *
  418. * @ocs: Pointer to changeset
  419. * @parent: Pointer to parent device node
  420. * @full_name: Node full name
  421. *
  422. * Return: Pointer to the created device node or NULL in case of an error.
  423. */
  424. struct device_node *of_changeset_create_node(struct of_changeset *ocs,
  425. struct device_node *parent,
  426. const char *full_name)
  427. {
  428. struct device_node *np;
  429. int ret;
  430. np = __of_node_dup(NULL, full_name);
  431. if (!np)
  432. return NULL;
  433. np->parent = parent;
  434. ret = of_changeset_attach_node(ocs, np);
  435. if (ret) {
  436. of_node_put(np);
  437. return NULL;
  438. }
  439. return np;
  440. }
  441. EXPORT_SYMBOL(of_changeset_create_node);
  442. static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
  443. {
  444. if (ce->action == OF_RECONFIG_ATTACH_NODE &&
  445. of_node_check_flag(ce->np, OF_OVERLAY)) {
  446. if (kref_read(&ce->np->kobj.kref) > 1) {
  447. pr_err("ERROR: memory leak, expected refcount 1 instead of %d, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node %pOF\n",
  448. kref_read(&ce->np->kobj.kref), ce->np);
  449. } else {
  450. of_node_set_flag(ce->np, OF_OVERLAY_FREE_CSET);
  451. }
  452. }
  453. of_node_put(ce->np);
  454. list_del(&ce->node);
  455. kfree(ce);
  456. }
  457. static void __of_changeset_entry_invert(const struct of_changeset_entry *ce,
  458. struct of_changeset_entry *rce)
  459. {
  460. memcpy(rce, ce, sizeof(*rce));
  461. switch (ce->action) {
  462. case OF_RECONFIG_ATTACH_NODE:
  463. rce->action = OF_RECONFIG_DETACH_NODE;
  464. break;
  465. case OF_RECONFIG_DETACH_NODE:
  466. rce->action = OF_RECONFIG_ATTACH_NODE;
  467. break;
  468. case OF_RECONFIG_ADD_PROPERTY:
  469. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  470. break;
  471. case OF_RECONFIG_REMOVE_PROPERTY:
  472. rce->action = OF_RECONFIG_ADD_PROPERTY;
  473. break;
  474. case OF_RECONFIG_UPDATE_PROPERTY:
  475. rce->old_prop = ce->prop;
  476. rce->prop = ce->old_prop;
  477. /* update was used but original property did not exist */
  478. if (!rce->prop) {
  479. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  480. rce->prop = ce->prop;
  481. }
  482. break;
  483. }
  484. }
  485. static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
  486. bool revert)
  487. {
  488. struct of_reconfig_data rd;
  489. struct of_changeset_entry ce_inverted;
  490. int ret = 0;
  491. if (revert) {
  492. __of_changeset_entry_invert(ce, &ce_inverted);
  493. ce = &ce_inverted;
  494. }
  495. switch (ce->action) {
  496. case OF_RECONFIG_ATTACH_NODE:
  497. case OF_RECONFIG_DETACH_NODE:
  498. memset(&rd, 0, sizeof(rd));
  499. rd.dn = ce->np;
  500. ret = of_reconfig_notify(ce->action, &rd);
  501. break;
  502. case OF_RECONFIG_ADD_PROPERTY:
  503. case OF_RECONFIG_REMOVE_PROPERTY:
  504. case OF_RECONFIG_UPDATE_PROPERTY:
  505. ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
  506. break;
  507. default:
  508. pr_err("invalid devicetree changeset action: %i\n",
  509. (int)ce->action);
  510. ret = -EINVAL;
  511. }
  512. if (ret)
  513. pr_err("changeset notifier error @%pOF\n", ce->np);
  514. return ret;
  515. }
  516. static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
  517. {
  518. int ret = 0;
  519. of_changeset_action_debug("apply: ", ce->action, ce->np, ce->prop);
  520. switch (ce->action) {
  521. case OF_RECONFIG_ATTACH_NODE:
  522. __of_attach_node(ce->np);
  523. break;
  524. case OF_RECONFIG_DETACH_NODE:
  525. __of_detach_node(ce->np);
  526. break;
  527. case OF_RECONFIG_ADD_PROPERTY:
  528. ret = __of_add_property(ce->np, ce->prop);
  529. break;
  530. case OF_RECONFIG_REMOVE_PROPERTY:
  531. ret = __of_remove_property(ce->np, ce->prop);
  532. break;
  533. case OF_RECONFIG_UPDATE_PROPERTY:
  534. ret = __of_update_property(ce->np, ce->prop, &ce->old_prop);
  535. break;
  536. default:
  537. ret = -EINVAL;
  538. }
  539. if (ret) {
  540. of_changeset_action_err("apply failed: ", ce->action, ce->np, ce->prop);
  541. return ret;
  542. }
  543. return 0;
  544. }
  545. static inline int __of_changeset_entry_revert(const struct of_changeset_entry *ce)
  546. {
  547. struct of_changeset_entry ce_inverted;
  548. __of_changeset_entry_invert(ce, &ce_inverted);
  549. return __of_changeset_entry_apply(&ce_inverted);
  550. }
  551. /**
  552. * of_changeset_init - Initialize a changeset for use
  553. *
  554. * @ocs: changeset pointer
  555. *
  556. * Initialize a changeset structure
  557. */
  558. void of_changeset_init(struct of_changeset *ocs)
  559. {
  560. memset(ocs, 0, sizeof(*ocs));
  561. INIT_LIST_HEAD(&ocs->entries);
  562. }
  563. EXPORT_SYMBOL_GPL(of_changeset_init);
  564. /**
  565. * of_changeset_destroy - Destroy a changeset
  566. *
  567. * @ocs: changeset pointer
  568. *
  569. * Destroys a changeset. Note that if a changeset is applied,
  570. * its changes to the tree cannot be reverted.
  571. */
  572. void of_changeset_destroy(struct of_changeset *ocs)
  573. {
  574. struct of_changeset_entry *ce, *cen;
  575. /*
  576. * When a device is deleted, the device links to/from it are also queued
  577. * for deletion. Until these device links are freed, the devices
  578. * themselves aren't freed. If the device being deleted is due to an
  579. * overlay change, this device might be holding a reference to a device
  580. * node that will be freed. So, wait until all already pending device
  581. * links are deleted before freeing a device node. This ensures we don't
  582. * free any device node that has a non-zero reference count.
  583. */
  584. device_link_wait_removal();
  585. list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
  586. __of_changeset_entry_destroy(ce);
  587. }
  588. EXPORT_SYMBOL_GPL(of_changeset_destroy);
  589. /*
  590. * Apply the changeset entries in @ocs.
  591. * If apply fails, an attempt is made to revert the entries that were
  592. * successfully applied.
  593. *
  594. * If multiple revert errors occur then only the final revert error is reported.
  595. *
  596. * Returns 0 on success, a negative error value in case of an error.
  597. * If a revert error occurs, it is returned in *ret_revert.
  598. */
  599. int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
  600. {
  601. struct of_changeset_entry *ce;
  602. int ret, ret_tmp;
  603. pr_debug("changeset: applying...\n");
  604. list_for_each_entry(ce, &ocs->entries, node) {
  605. ret = __of_changeset_entry_apply(ce);
  606. if (ret) {
  607. pr_err("Error applying changeset (%d)\n", ret);
  608. list_for_each_entry_continue_reverse(ce, &ocs->entries,
  609. node) {
  610. ret_tmp = __of_changeset_entry_revert(ce);
  611. if (ret_tmp)
  612. *ret_revert = ret_tmp;
  613. }
  614. return ret;
  615. }
  616. }
  617. return 0;
  618. }
  619. /*
  620. * Returns 0 on success, a negative error value in case of an error.
  621. *
  622. * If multiple changeset entry notification errors occur then only the
  623. * final notification error is reported.
  624. */
  625. int __of_changeset_apply_notify(struct of_changeset *ocs)
  626. {
  627. struct of_changeset_entry *ce;
  628. int ret = 0, ret_tmp;
  629. pr_debug("changeset: emitting notifiers.\n");
  630. /* drop the global lock while emitting notifiers */
  631. mutex_unlock(&of_mutex);
  632. list_for_each_entry(ce, &ocs->entries, node) {
  633. ret_tmp = __of_changeset_entry_notify(ce, 0);
  634. if (ret_tmp)
  635. ret = ret_tmp;
  636. }
  637. mutex_lock(&of_mutex);
  638. pr_debug("changeset: notifiers sent.\n");
  639. return ret;
  640. }
  641. /*
  642. * Returns 0 on success, a negative error value in case of an error.
  643. *
  644. * If a changeset entry apply fails, an attempt is made to revert any
  645. * previous entries in the changeset. If any of the reverts fails,
  646. * that failure is not reported. Thus the state of the device tree
  647. * is unknown if an apply error occurs.
  648. */
  649. static int __of_changeset_apply(struct of_changeset *ocs)
  650. {
  651. int ret, ret_revert = 0;
  652. ret = __of_changeset_apply_entries(ocs, &ret_revert);
  653. if (!ret)
  654. ret = __of_changeset_apply_notify(ocs);
  655. return ret;
  656. }
  657. /**
  658. * of_changeset_apply - Applies a changeset
  659. *
  660. * @ocs: changeset pointer
  661. *
  662. * Applies a changeset to the live tree.
  663. * Any side-effects of live tree state changes are applied here on
  664. * success, like creation/destruction of devices and side-effects
  665. * like creation of sysfs properties and directories.
  666. *
  667. * Return: 0 on success, a negative error value in case of an error.
  668. * On error the partially applied effects are reverted.
  669. */
  670. int of_changeset_apply(struct of_changeset *ocs)
  671. {
  672. int ret;
  673. mutex_lock(&of_mutex);
  674. ret = __of_changeset_apply(ocs);
  675. mutex_unlock(&of_mutex);
  676. return ret;
  677. }
  678. EXPORT_SYMBOL_GPL(of_changeset_apply);
  679. /*
  680. * Revert the changeset entries in @ocs.
  681. * If revert fails, an attempt is made to re-apply the entries that were
  682. * successfully removed.
  683. *
  684. * If multiple re-apply errors occur then only the final apply error is
  685. * reported.
  686. *
  687. * Returns 0 on success, a negative error value in case of an error.
  688. * If an apply error occurs, it is returned in *ret_apply.
  689. */
  690. int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
  691. {
  692. struct of_changeset_entry *ce;
  693. int ret, ret_tmp;
  694. pr_debug("changeset: reverting...\n");
  695. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  696. ret = __of_changeset_entry_revert(ce);
  697. if (ret) {
  698. pr_err("Error reverting changeset (%d)\n", ret);
  699. list_for_each_entry_continue(ce, &ocs->entries, node) {
  700. ret_tmp = __of_changeset_entry_apply(ce);
  701. if (ret_tmp)
  702. *ret_apply = ret_tmp;
  703. }
  704. return ret;
  705. }
  706. }
  707. return 0;
  708. }
  709. /*
  710. * If multiple changeset entry notification errors occur then only the
  711. * final notification error is reported.
  712. */
  713. int __of_changeset_revert_notify(struct of_changeset *ocs)
  714. {
  715. struct of_changeset_entry *ce;
  716. int ret = 0, ret_tmp;
  717. pr_debug("changeset: emitting notifiers.\n");
  718. /* drop the global lock while emitting notifiers */
  719. mutex_unlock(&of_mutex);
  720. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  721. ret_tmp = __of_changeset_entry_notify(ce, 1);
  722. if (ret_tmp)
  723. ret = ret_tmp;
  724. }
  725. mutex_lock(&of_mutex);
  726. pr_debug("changeset: notifiers sent.\n");
  727. return ret;
  728. }
  729. static int __of_changeset_revert(struct of_changeset *ocs)
  730. {
  731. int ret, ret_reply;
  732. ret_reply = 0;
  733. ret = __of_changeset_revert_entries(ocs, &ret_reply);
  734. if (!ret)
  735. ret = __of_changeset_revert_notify(ocs);
  736. return ret;
  737. }
  738. /**
  739. * of_changeset_revert - Reverts an applied changeset
  740. *
  741. * @ocs: changeset pointer
  742. *
  743. * Reverts a changeset returning the state of the tree to what it
  744. * was before the application.
  745. * Any side-effects like creation/destruction of devices and
  746. * removal of sysfs properties and directories are applied.
  747. *
  748. * Return: 0 on success, a negative error value in case of an error.
  749. */
  750. int of_changeset_revert(struct of_changeset *ocs)
  751. {
  752. int ret;
  753. mutex_lock(&of_mutex);
  754. ret = __of_changeset_revert(ocs);
  755. mutex_unlock(&of_mutex);
  756. return ret;
  757. }
  758. EXPORT_SYMBOL_GPL(of_changeset_revert);
  759. /**
  760. * of_changeset_action - Add an action to the tail of the changeset list
  761. *
  762. * @ocs: changeset pointer
  763. * @action: action to perform
  764. * @np: Pointer to device node
  765. * @prop: Pointer to property
  766. *
  767. * On action being one of:
  768. * + OF_RECONFIG_ATTACH_NODE
  769. * + OF_RECONFIG_DETACH_NODE,
  770. * + OF_RECONFIG_ADD_PROPERTY
  771. * + OF_RECONFIG_REMOVE_PROPERTY,
  772. * + OF_RECONFIG_UPDATE_PROPERTY
  773. *
  774. * Return: 0 on success, a negative error value in case of an error.
  775. */
  776. int of_changeset_action(struct of_changeset *ocs, unsigned long action,
  777. struct device_node *np, struct property *prop)
  778. {
  779. struct of_changeset_entry *ce;
  780. if (WARN_ON(action >= ARRAY_SIZE(action_names)))
  781. return -EINVAL;
  782. ce = kzalloc_obj(*ce);
  783. if (!ce)
  784. return -ENOMEM;
  785. /* get a reference to the node */
  786. ce->action = action;
  787. ce->np = of_node_get(np);
  788. ce->prop = prop;
  789. /* add it to the list */
  790. list_add_tail(&ce->node, &ocs->entries);
  791. return 0;
  792. }
  793. EXPORT_SYMBOL_GPL(of_changeset_action);
  794. static int of_changeset_add_prop_helper(struct of_changeset *ocs,
  795. struct device_node *np,
  796. const struct property *pp)
  797. {
  798. struct property *new_pp;
  799. int ret;
  800. new_pp = __of_prop_dup(pp, GFP_KERNEL);
  801. if (!new_pp)
  802. return -ENOMEM;
  803. ret = of_changeset_add_property(ocs, np, new_pp);
  804. if (ret) {
  805. __of_prop_free(new_pp);
  806. return ret;
  807. }
  808. new_pp->next = np->deadprops;
  809. np->deadprops = new_pp;
  810. return 0;
  811. }
  812. /**
  813. * of_changeset_add_prop_string - Add a string property to a changeset
  814. *
  815. * @ocs: changeset pointer
  816. * @np: device node pointer
  817. * @prop_name: name of the property to be added
  818. * @str: pointer to null terminated string
  819. *
  820. * Create a string property and add it to a changeset.
  821. *
  822. * Return: 0 on success, a negative error value in case of an error.
  823. */
  824. int of_changeset_add_prop_string(struct of_changeset *ocs,
  825. struct device_node *np,
  826. const char *prop_name, const char *str)
  827. {
  828. struct property prop;
  829. prop.name = (char *)prop_name;
  830. prop.length = strlen(str) + 1;
  831. prop.value = (void *)str;
  832. return of_changeset_add_prop_helper(ocs, np, &prop);
  833. }
  834. EXPORT_SYMBOL_GPL(of_changeset_add_prop_string);
  835. /**
  836. * of_changeset_add_prop_string_array - Add a string list property to
  837. * a changeset
  838. *
  839. * @ocs: changeset pointer
  840. * @np: device node pointer
  841. * @prop_name: name of the property to be added
  842. * @str_array: pointer to an array of null terminated strings
  843. * @sz: number of string array elements
  844. *
  845. * Create a string list property and add it to a changeset.
  846. *
  847. * Return: 0 on success, a negative error value in case of an error.
  848. */
  849. int of_changeset_add_prop_string_array(struct of_changeset *ocs,
  850. struct device_node *np,
  851. const char *prop_name,
  852. const char * const *str_array, size_t sz)
  853. {
  854. struct property prop;
  855. int i, ret;
  856. char *vp;
  857. prop.name = (char *)prop_name;
  858. prop.length = 0;
  859. for (i = 0; i < sz; i++)
  860. prop.length += strlen(str_array[i]) + 1;
  861. prop.value = kmalloc(prop.length, GFP_KERNEL);
  862. if (!prop.value)
  863. return -ENOMEM;
  864. vp = prop.value;
  865. for (i = 0; i < sz; i++) {
  866. vp += snprintf(vp, (char *)prop.value + prop.length - vp, "%s",
  867. str_array[i]) + 1;
  868. }
  869. ret = of_changeset_add_prop_helper(ocs, np, &prop);
  870. kfree(prop.value);
  871. return ret;
  872. }
  873. EXPORT_SYMBOL_GPL(of_changeset_add_prop_string_array);
  874. /**
  875. * of_changeset_add_prop_u32_array - Add a property of 32 bit integers
  876. * property to a changeset
  877. *
  878. * @ocs: changeset pointer
  879. * @np: device node pointer
  880. * @prop_name: name of the property to be added
  881. * @array: pointer to an array of 32 bit integers
  882. * @sz: number of array elements
  883. *
  884. * Create a property of 32 bit integers and add it to a changeset.
  885. *
  886. * Return: 0 on success, a negative error value in case of an error.
  887. */
  888. int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
  889. struct device_node *np,
  890. const char *prop_name,
  891. const u32 *array, size_t sz)
  892. {
  893. struct property prop;
  894. __be32 *val __free(kfree) = kcalloc(sz, sizeof(__be32), GFP_KERNEL);
  895. int i;
  896. if (!val)
  897. return -ENOMEM;
  898. for (i = 0; i < sz; i++)
  899. val[i] = cpu_to_be32(array[i]);
  900. prop.name = (char *)prop_name;
  901. prop.length = sizeof(u32) * sz;
  902. prop.value = (void *)val;
  903. return of_changeset_add_prop_helper(ocs, np, &prop);
  904. }
  905. EXPORT_SYMBOL_GPL(of_changeset_add_prop_u32_array);
  906. /**
  907. * of_changeset_add_prop_bool - Add a boolean property (i.e. a property without
  908. * any values) to a changeset.
  909. *
  910. * @ocs: changeset pointer
  911. * @np: device node pointer
  912. * @prop_name: name of the property to be added
  913. *
  914. * Create a boolean property and add it to a changeset.
  915. *
  916. * Return: 0 on success, a negative error value in case of an error.
  917. */
  918. int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np,
  919. const char *prop_name)
  920. {
  921. struct property prop;
  922. prop.name = (char *)prop_name;
  923. prop.length = 0;
  924. prop.value = NULL;
  925. return of_changeset_add_prop_helper(ocs, np, &prop);
  926. }
  927. EXPORT_SYMBOL_GPL(of_changeset_add_prop_bool);
  928. static int of_changeset_update_prop_helper(struct of_changeset *ocs,
  929. struct device_node *np,
  930. const struct property *pp)
  931. {
  932. struct property *new_pp;
  933. int ret;
  934. new_pp = __of_prop_dup(pp, GFP_KERNEL);
  935. if (!new_pp)
  936. return -ENOMEM;
  937. ret = of_changeset_update_property(ocs, np, new_pp);
  938. if (ret)
  939. __of_prop_free(new_pp);
  940. return ret;
  941. }
  942. /**
  943. * of_changeset_update_prop_string - Add a string property update to a changeset
  944. *
  945. * @ocs: changeset pointer
  946. * @np: device node pointer
  947. * @prop_name: name of the property to be updated
  948. * @str: pointer to null terminated string
  949. *
  950. * Create a string property to be updated and add it to a changeset.
  951. *
  952. * Return: 0 on success, a negative error value in case of an error.
  953. */
  954. int of_changeset_update_prop_string(struct of_changeset *ocs,
  955. struct device_node *np,
  956. const char *prop_name, const char *str)
  957. {
  958. struct property prop = {
  959. .name = (char *)prop_name,
  960. .length = strlen(str) + 1,
  961. .value = (void *)str,
  962. };
  963. return of_changeset_update_prop_helper(ocs, np, &prop);
  964. }
  965. EXPORT_SYMBOL_GPL(of_changeset_update_prop_string);