dynamic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. np->type = __of_get_property(np, "device_type", NULL);
  186. if (!np->name)
  187. np->name = "<NULL>";
  188. if (!np->type)
  189. np->type = "<NULL>";
  190. phandle = __of_get_property(np, "phandle", &sz);
  191. if (!phandle)
  192. phandle = __of_get_property(np, "linux,phandle", &sz);
  193. if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
  194. phandle = __of_get_property(np, "ibm,phandle", &sz);
  195. if (phandle && (sz >= 4))
  196. np->phandle = be32_to_cpup(phandle);
  197. else
  198. np->phandle = 0;
  199. }
  200. np->child = NULL;
  201. np->sibling = np->parent->child;
  202. np->parent->child = np;
  203. of_node_clear_flag(np, OF_DETACHED);
  204. }
  205. /**
  206. * of_attach_node() - Plug a device node into the tree and global list.
  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_free_phandle_cache_entry(np->phandle);
  244. }
  245. /**
  246. * of_detach_node() - "Unplug" a node from the device tree.
  247. */
  248. int of_detach_node(struct device_node *np)
  249. {
  250. struct of_reconfig_data rd;
  251. unsigned long flags;
  252. int rc = 0;
  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 rc;
  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. * @kref: kref element 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. kfree(node->full_name);
  311. kfree(node->data);
  312. kfree(node);
  313. }
  314. /**
  315. * __of_prop_dup - Copy a property dynamically.
  316. * @prop: Property to copy
  317. * @allocflags: Allocation flags (typically pass GFP_KERNEL)
  318. *
  319. * Copy a property by dynamically allocating the memory of both the
  320. * property structure and the property name & contents. The property's
  321. * flags have the OF_DYNAMIC bit set so that we can differentiate between
  322. * dynamically allocated properties and not.
  323. * Returns the newly allocated property or NULL on out of memory error.
  324. */
  325. struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
  326. {
  327. struct property *new;
  328. new = kzalloc(sizeof(*new), allocflags);
  329. if (!new)
  330. return NULL;
  331. /*
  332. * NOTE: There is no check for zero length value.
  333. * In case of a boolean property, this will allocate a value
  334. * of zero bytes. We do this to work around the use
  335. * of of_get_property() calls on boolean values.
  336. */
  337. new->name = kstrdup(prop->name, allocflags);
  338. new->value = kmemdup(prop->value, prop->length, allocflags);
  339. new->length = prop->length;
  340. if (!new->name || !new->value)
  341. goto err_free;
  342. /* mark the property as dynamic */
  343. of_property_set_flag(new, OF_DYNAMIC);
  344. return new;
  345. err_free:
  346. kfree(new->name);
  347. kfree(new->value);
  348. kfree(new);
  349. return NULL;
  350. }
  351. /**
  352. * __of_node_dup() - Duplicate or create an empty device node dynamically.
  353. * @np: if not NULL, contains properties to be duplicated in new node
  354. * @full_name: string value to be duplicated into new node's full_name field
  355. *
  356. * Create a device tree node, optionally duplicating the properties of
  357. * another node. The node data are dynamically allocated and all the node
  358. * flags have the OF_DYNAMIC & OF_DETACHED bits set.
  359. *
  360. * Returns the newly allocated node or NULL on out of memory error.
  361. */
  362. struct device_node *__of_node_dup(const struct device_node *np,
  363. const char *full_name)
  364. {
  365. struct device_node *node;
  366. node = kzalloc(sizeof(*node), GFP_KERNEL);
  367. if (!node)
  368. return NULL;
  369. node->full_name = kstrdup(full_name, GFP_KERNEL);
  370. if (!node->full_name) {
  371. kfree(node);
  372. return NULL;
  373. }
  374. of_node_set_flag(node, OF_DYNAMIC);
  375. of_node_set_flag(node, OF_DETACHED);
  376. of_node_init(node);
  377. /* Iterate over and duplicate all properties */
  378. if (np) {
  379. struct property *pp, *new_pp;
  380. for_each_property_of_node(np, pp) {
  381. new_pp = __of_prop_dup(pp, GFP_KERNEL);
  382. if (!new_pp)
  383. goto err_prop;
  384. if (__of_add_property(node, new_pp)) {
  385. kfree(new_pp->name);
  386. kfree(new_pp->value);
  387. kfree(new_pp);
  388. goto err_prop;
  389. }
  390. }
  391. }
  392. return node;
  393. err_prop:
  394. of_node_put(node); /* Frees the node and properties */
  395. return NULL;
  396. }
  397. static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
  398. {
  399. if (ce->action == OF_RECONFIG_ATTACH_NODE &&
  400. of_node_check_flag(ce->np, OF_OVERLAY)) {
  401. if (kref_read(&ce->np->kobj.kref) > 1) {
  402. 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",
  403. kref_read(&ce->np->kobj.kref), ce->np);
  404. } else {
  405. of_node_set_flag(ce->np, OF_OVERLAY_FREE_CSET);
  406. }
  407. }
  408. of_node_put(ce->np);
  409. list_del(&ce->node);
  410. kfree(ce);
  411. }
  412. #ifdef DEBUG
  413. static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  414. {
  415. switch (ce->action) {
  416. case OF_RECONFIG_ADD_PROPERTY:
  417. case OF_RECONFIG_REMOVE_PROPERTY:
  418. case OF_RECONFIG_UPDATE_PROPERTY:
  419. pr_debug("cset<%p> %-15s %pOF/%s\n", ce, action_names[ce->action],
  420. ce->np, ce->prop->name);
  421. break;
  422. case OF_RECONFIG_ATTACH_NODE:
  423. case OF_RECONFIG_DETACH_NODE:
  424. pr_debug("cset<%p> %-15s %pOF\n", ce, action_names[ce->action],
  425. ce->np);
  426. break;
  427. }
  428. }
  429. #else
  430. static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  431. {
  432. /* empty */
  433. }
  434. #endif
  435. static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
  436. struct of_changeset_entry *rce)
  437. {
  438. memcpy(rce, ce, sizeof(*rce));
  439. switch (ce->action) {
  440. case OF_RECONFIG_ATTACH_NODE:
  441. rce->action = OF_RECONFIG_DETACH_NODE;
  442. break;
  443. case OF_RECONFIG_DETACH_NODE:
  444. rce->action = OF_RECONFIG_ATTACH_NODE;
  445. break;
  446. case OF_RECONFIG_ADD_PROPERTY:
  447. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  448. break;
  449. case OF_RECONFIG_REMOVE_PROPERTY:
  450. rce->action = OF_RECONFIG_ADD_PROPERTY;
  451. break;
  452. case OF_RECONFIG_UPDATE_PROPERTY:
  453. rce->old_prop = ce->prop;
  454. rce->prop = ce->old_prop;
  455. /* update was used but original property did not exist */
  456. if (!rce->prop) {
  457. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  458. rce->prop = ce->prop;
  459. }
  460. break;
  461. }
  462. }
  463. static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
  464. bool revert)
  465. {
  466. struct of_reconfig_data rd;
  467. struct of_changeset_entry ce_inverted;
  468. int ret = 0;
  469. if (revert) {
  470. __of_changeset_entry_invert(ce, &ce_inverted);
  471. ce = &ce_inverted;
  472. }
  473. switch (ce->action) {
  474. case OF_RECONFIG_ATTACH_NODE:
  475. case OF_RECONFIG_DETACH_NODE:
  476. memset(&rd, 0, sizeof(rd));
  477. rd.dn = ce->np;
  478. ret = of_reconfig_notify(ce->action, &rd);
  479. break;
  480. case OF_RECONFIG_ADD_PROPERTY:
  481. case OF_RECONFIG_REMOVE_PROPERTY:
  482. case OF_RECONFIG_UPDATE_PROPERTY:
  483. ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
  484. break;
  485. default:
  486. pr_err("invalid devicetree changeset action: %i\n",
  487. (int)ce->action);
  488. ret = -EINVAL;
  489. }
  490. if (ret)
  491. pr_err("changeset notifier error @%pOF\n", ce->np);
  492. return ret;
  493. }
  494. static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
  495. {
  496. struct property *old_prop, **propp;
  497. unsigned long flags;
  498. int ret = 0;
  499. __of_changeset_entry_dump(ce);
  500. raw_spin_lock_irqsave(&devtree_lock, flags);
  501. switch (ce->action) {
  502. case OF_RECONFIG_ATTACH_NODE:
  503. __of_attach_node(ce->np);
  504. break;
  505. case OF_RECONFIG_DETACH_NODE:
  506. __of_detach_node(ce->np);
  507. break;
  508. case OF_RECONFIG_ADD_PROPERTY:
  509. /* If the property is in deadprops then it must be removed */
  510. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  511. if (*propp == ce->prop) {
  512. *propp = ce->prop->next;
  513. ce->prop->next = NULL;
  514. break;
  515. }
  516. }
  517. ret = __of_add_property(ce->np, ce->prop);
  518. if (ret) {
  519. pr_err("changeset: add_property failed @%pOF/%s\n",
  520. ce->np,
  521. ce->prop->name);
  522. break;
  523. }
  524. break;
  525. case OF_RECONFIG_REMOVE_PROPERTY:
  526. ret = __of_remove_property(ce->np, ce->prop);
  527. if (ret) {
  528. pr_err("changeset: remove_property failed @%pOF/%s\n",
  529. ce->np,
  530. ce->prop->name);
  531. break;
  532. }
  533. break;
  534. case OF_RECONFIG_UPDATE_PROPERTY:
  535. /* If the property is in deadprops then it must be removed */
  536. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  537. if (*propp == ce->prop) {
  538. *propp = ce->prop->next;
  539. ce->prop->next = NULL;
  540. break;
  541. }
  542. }
  543. ret = __of_update_property(ce->np, ce->prop, &old_prop);
  544. if (ret) {
  545. pr_err("changeset: update_property failed @%pOF/%s\n",
  546. ce->np,
  547. ce->prop->name);
  548. break;
  549. }
  550. break;
  551. default:
  552. ret = -EINVAL;
  553. }
  554. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  555. if (ret)
  556. return ret;
  557. switch (ce->action) {
  558. case OF_RECONFIG_ATTACH_NODE:
  559. __of_attach_node_sysfs(ce->np);
  560. break;
  561. case OF_RECONFIG_DETACH_NODE:
  562. __of_detach_node_sysfs(ce->np);
  563. break;
  564. case OF_RECONFIG_ADD_PROPERTY:
  565. /* ignore duplicate names */
  566. __of_add_property_sysfs(ce->np, ce->prop);
  567. break;
  568. case OF_RECONFIG_REMOVE_PROPERTY:
  569. __of_remove_property_sysfs(ce->np, ce->prop);
  570. break;
  571. case OF_RECONFIG_UPDATE_PROPERTY:
  572. __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
  573. break;
  574. }
  575. return 0;
  576. }
  577. static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
  578. {
  579. struct of_changeset_entry ce_inverted;
  580. __of_changeset_entry_invert(ce, &ce_inverted);
  581. return __of_changeset_entry_apply(&ce_inverted);
  582. }
  583. /**
  584. * of_changeset_init - Initialize a changeset for use
  585. *
  586. * @ocs: changeset pointer
  587. *
  588. * Initialize a changeset structure
  589. */
  590. void of_changeset_init(struct of_changeset *ocs)
  591. {
  592. memset(ocs, 0, sizeof(*ocs));
  593. INIT_LIST_HEAD(&ocs->entries);
  594. }
  595. EXPORT_SYMBOL_GPL(of_changeset_init);
  596. /**
  597. * of_changeset_destroy - Destroy a changeset
  598. *
  599. * @ocs: changeset pointer
  600. *
  601. * Destroys a changeset. Note that if a changeset is applied,
  602. * its changes to the tree cannot be reverted.
  603. */
  604. void of_changeset_destroy(struct of_changeset *ocs)
  605. {
  606. struct of_changeset_entry *ce, *cen;
  607. list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
  608. __of_changeset_entry_destroy(ce);
  609. }
  610. EXPORT_SYMBOL_GPL(of_changeset_destroy);
  611. /*
  612. * Apply the changeset entries in @ocs.
  613. * If apply fails, an attempt is made to revert the entries that were
  614. * successfully applied.
  615. *
  616. * If multiple revert errors occur then only the final revert error is reported.
  617. *
  618. * Returns 0 on success, a negative error value in case of an error.
  619. * If a revert error occurs, it is returned in *ret_revert.
  620. */
  621. int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
  622. {
  623. struct of_changeset_entry *ce;
  624. int ret, ret_tmp;
  625. pr_debug("changeset: applying...\n");
  626. list_for_each_entry(ce, &ocs->entries, node) {
  627. ret = __of_changeset_entry_apply(ce);
  628. if (ret) {
  629. pr_err("Error applying changeset (%d)\n", ret);
  630. list_for_each_entry_continue_reverse(ce, &ocs->entries,
  631. node) {
  632. ret_tmp = __of_changeset_entry_revert(ce);
  633. if (ret_tmp)
  634. *ret_revert = ret_tmp;
  635. }
  636. return ret;
  637. }
  638. }
  639. return 0;
  640. }
  641. /*
  642. * Returns 0 on success, a negative error value in case of an error.
  643. *
  644. * If multiple changeset entry notification errors occur then only the
  645. * final notification error is reported.
  646. */
  647. int __of_changeset_apply_notify(struct of_changeset *ocs)
  648. {
  649. struct of_changeset_entry *ce;
  650. int ret = 0, ret_tmp;
  651. pr_debug("changeset: emitting notifiers.\n");
  652. /* drop the global lock while emitting notifiers */
  653. mutex_unlock(&of_mutex);
  654. list_for_each_entry(ce, &ocs->entries, node) {
  655. ret_tmp = __of_changeset_entry_notify(ce, 0);
  656. if (ret_tmp)
  657. ret = ret_tmp;
  658. }
  659. mutex_lock(&of_mutex);
  660. pr_debug("changeset: notifiers sent.\n");
  661. return ret;
  662. }
  663. /*
  664. * Returns 0 on success, a negative error value in case of an error.
  665. *
  666. * If a changeset entry apply fails, an attempt is made to revert any
  667. * previous entries in the changeset. If any of the reverts fails,
  668. * that failure is not reported. Thus the state of the device tree
  669. * is unknown if an apply error occurs.
  670. */
  671. static int __of_changeset_apply(struct of_changeset *ocs)
  672. {
  673. int ret, ret_revert = 0;
  674. ret = __of_changeset_apply_entries(ocs, &ret_revert);
  675. if (!ret)
  676. ret = __of_changeset_apply_notify(ocs);
  677. return ret;
  678. }
  679. /**
  680. * of_changeset_apply - Applies a changeset
  681. *
  682. * @ocs: changeset pointer
  683. *
  684. * Applies a changeset to the live tree.
  685. * Any side-effects of live tree state changes are applied here on
  686. * success, like creation/destruction of devices and side-effects
  687. * like creation of sysfs properties and directories.
  688. * Returns 0 on success, a negative error value in case of an error.
  689. * On error the partially applied effects are reverted.
  690. */
  691. int of_changeset_apply(struct of_changeset *ocs)
  692. {
  693. int ret;
  694. mutex_lock(&of_mutex);
  695. ret = __of_changeset_apply(ocs);
  696. mutex_unlock(&of_mutex);
  697. return ret;
  698. }
  699. EXPORT_SYMBOL_GPL(of_changeset_apply);
  700. /*
  701. * Revert the changeset entries in @ocs.
  702. * If revert fails, an attempt is made to re-apply the entries that were
  703. * successfully removed.
  704. *
  705. * If multiple re-apply errors occur then only the final apply error is
  706. * reported.
  707. *
  708. * Returns 0 on success, a negative error value in case of an error.
  709. * If an apply error occurs, it is returned in *ret_apply.
  710. */
  711. int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
  712. {
  713. struct of_changeset_entry *ce;
  714. int ret, ret_tmp;
  715. pr_debug("changeset: reverting...\n");
  716. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  717. ret = __of_changeset_entry_revert(ce);
  718. if (ret) {
  719. pr_err("Error reverting changeset (%d)\n", ret);
  720. list_for_each_entry_continue(ce, &ocs->entries, node) {
  721. ret_tmp = __of_changeset_entry_apply(ce);
  722. if (ret_tmp)
  723. *ret_apply = ret_tmp;
  724. }
  725. return ret;
  726. }
  727. }
  728. return 0;
  729. }
  730. /*
  731. * If multiple changeset entry notification errors occur then only the
  732. * final notification error is reported.
  733. */
  734. int __of_changeset_revert_notify(struct of_changeset *ocs)
  735. {
  736. struct of_changeset_entry *ce;
  737. int ret = 0, ret_tmp;
  738. pr_debug("changeset: emitting notifiers.\n");
  739. /* drop the global lock while emitting notifiers */
  740. mutex_unlock(&of_mutex);
  741. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  742. ret_tmp = __of_changeset_entry_notify(ce, 1);
  743. if (ret_tmp)
  744. ret = ret_tmp;
  745. }
  746. mutex_lock(&of_mutex);
  747. pr_debug("changeset: notifiers sent.\n");
  748. return ret;
  749. }
  750. static int __of_changeset_revert(struct of_changeset *ocs)
  751. {
  752. int ret, ret_reply;
  753. ret_reply = 0;
  754. ret = __of_changeset_revert_entries(ocs, &ret_reply);
  755. if (!ret)
  756. ret = __of_changeset_revert_notify(ocs);
  757. return ret;
  758. }
  759. /**
  760. * of_changeset_revert - Reverts an applied changeset
  761. *
  762. * @ocs: changeset pointer
  763. *
  764. * Reverts a changeset returning the state of the tree to what it
  765. * was before the application.
  766. * Any side-effects like creation/destruction of devices and
  767. * removal of sysfs properties and directories are applied.
  768. * Returns 0 on success, a negative error value in case of an error.
  769. */
  770. int of_changeset_revert(struct of_changeset *ocs)
  771. {
  772. int ret;
  773. mutex_lock(&of_mutex);
  774. ret = __of_changeset_revert(ocs);
  775. mutex_unlock(&of_mutex);
  776. return ret;
  777. }
  778. EXPORT_SYMBOL_GPL(of_changeset_revert);
  779. /**
  780. * of_changeset_action - Add an action to the tail of the changeset list
  781. *
  782. * @ocs: changeset pointer
  783. * @action: action to perform
  784. * @np: Pointer to device node
  785. * @prop: Pointer to property
  786. *
  787. * On action being one of:
  788. * + OF_RECONFIG_ATTACH_NODE
  789. * + OF_RECONFIG_DETACH_NODE,
  790. * + OF_RECONFIG_ADD_PROPERTY
  791. * + OF_RECONFIG_REMOVE_PROPERTY,
  792. * + OF_RECONFIG_UPDATE_PROPERTY
  793. * Returns 0 on success, a negative error value in case of an error.
  794. */
  795. int of_changeset_action(struct of_changeset *ocs, unsigned long action,
  796. struct device_node *np, struct property *prop)
  797. {
  798. struct of_changeset_entry *ce;
  799. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  800. if (!ce)
  801. return -ENOMEM;
  802. /* get a reference to the node */
  803. ce->action = action;
  804. ce->np = of_node_get(np);
  805. ce->prop = prop;
  806. if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
  807. ce->old_prop = of_find_property(np, prop->name, NULL);
  808. /* add it to the list */
  809. list_add_tail(&ce->node, &ocs->entries);
  810. return 0;
  811. }
  812. EXPORT_SYMBOL_GPL(of_changeset_action);