dynamic.c 23 KB

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