dynamic.c 23 KB

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