dynamic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Support for dynamic device trees.
  3. *
  4. * On some platforms, the device tree can be manipulated at runtime.
  5. * The routines in this section support adding, removing and changing
  6. * device tree nodes.
  7. */
  8. #include <linux/of.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. #include <linux/proc_fs.h>
  13. #include "of_private.h"
  14. /**
  15. * of_node_get() - Increment refcount of a node
  16. * @node: Node to inc refcount, NULL is supported to simplify writing of
  17. * callers
  18. *
  19. * Returns node.
  20. */
  21. struct device_node *of_node_get(struct device_node *node)
  22. {
  23. if (node)
  24. kobject_get(&node->kobj);
  25. return node;
  26. }
  27. EXPORT_SYMBOL(of_node_get);
  28. /**
  29. * of_node_put() - Decrement refcount of a node
  30. * @node: Node to dec refcount, NULL is supported to simplify writing of
  31. * callers
  32. */
  33. void of_node_put(struct device_node *node)
  34. {
  35. if (node)
  36. kobject_put(&node->kobj);
  37. }
  38. EXPORT_SYMBOL(of_node_put);
  39. void __of_detach_node_sysfs(struct device_node *np)
  40. {
  41. struct property *pp;
  42. if (!IS_ENABLED(CONFIG_SYSFS))
  43. return;
  44. BUG_ON(!of_node_is_initialized(np));
  45. if (!of_kset)
  46. return;
  47. /* only remove properties if on sysfs */
  48. if (of_node_is_attached(np)) {
  49. for_each_property_of_node(np, pp)
  50. sysfs_remove_bin_file(&np->kobj, &pp->attr);
  51. kobject_del(&np->kobj);
  52. }
  53. /* finally remove the kobj_init ref */
  54. of_node_put(np);
  55. }
  56. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  57. int of_reconfig_notifier_register(struct notifier_block *nb)
  58. {
  59. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  60. }
  61. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  62. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  63. {
  64. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  65. }
  66. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  67. int of_reconfig_notify(unsigned long action, void *p)
  68. {
  69. int rc;
  70. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  71. return notifier_to_errno(rc);
  72. }
  73. int of_property_notify(int action, struct device_node *np,
  74. struct property *prop, struct property *oldprop)
  75. {
  76. struct of_prop_reconfig pr;
  77. /* only call notifiers if the node is attached */
  78. if (!of_node_is_attached(np))
  79. return 0;
  80. pr.dn = np;
  81. pr.prop = prop;
  82. pr.old_prop = oldprop;
  83. return of_reconfig_notify(action, &pr);
  84. }
  85. void __of_attach_node(struct device_node *np)
  86. {
  87. const __be32 *phandle;
  88. int sz;
  89. np->name = __of_get_property(np, "name", NULL) ? : "<NULL>";
  90. np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>";
  91. phandle = __of_get_property(np, "phandle", &sz);
  92. if (!phandle)
  93. phandle = __of_get_property(np, "linux,phandle", &sz);
  94. if (IS_ENABLED(PPC_PSERIES) && !phandle)
  95. phandle = __of_get_property(np, "ibm,phandle", &sz);
  96. np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
  97. np->child = NULL;
  98. np->sibling = np->parent->child;
  99. np->allnext = np->parent->allnext;
  100. np->parent->allnext = np;
  101. np->parent->child = np;
  102. of_node_clear_flag(np, OF_DETACHED);
  103. }
  104. /**
  105. * of_attach_node() - Plug a device node into the tree and global list.
  106. */
  107. int of_attach_node(struct device_node *np)
  108. {
  109. unsigned long flags;
  110. mutex_lock(&of_mutex);
  111. raw_spin_lock_irqsave(&devtree_lock, flags);
  112. __of_attach_node(np);
  113. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  114. __of_attach_node_sysfs(np);
  115. mutex_unlock(&of_mutex);
  116. of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  117. return 0;
  118. }
  119. void __of_detach_node(struct device_node *np)
  120. {
  121. struct device_node *parent;
  122. if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
  123. return;
  124. parent = np->parent;
  125. if (WARN_ON(!parent))
  126. return;
  127. if (of_allnodes == np)
  128. of_allnodes = np->allnext;
  129. else {
  130. struct device_node *prev;
  131. for (prev = of_allnodes;
  132. prev->allnext != np;
  133. prev = prev->allnext)
  134. ;
  135. prev->allnext = np->allnext;
  136. }
  137. if (parent->child == np)
  138. parent->child = np->sibling;
  139. else {
  140. struct device_node *prevsib;
  141. for (prevsib = np->parent->child;
  142. prevsib->sibling != np;
  143. prevsib = prevsib->sibling)
  144. ;
  145. prevsib->sibling = np->sibling;
  146. }
  147. of_node_set_flag(np, OF_DETACHED);
  148. }
  149. /**
  150. * of_detach_node() - "Unplug" a node from the device tree.
  151. *
  152. * The caller must hold a reference to the node. The memory associated with
  153. * the node is not freed until its refcount goes to zero.
  154. */
  155. int of_detach_node(struct device_node *np)
  156. {
  157. unsigned long flags;
  158. int rc = 0;
  159. mutex_lock(&of_mutex);
  160. raw_spin_lock_irqsave(&devtree_lock, flags);
  161. __of_detach_node(np);
  162. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  163. __of_detach_node_sysfs(np);
  164. mutex_unlock(&of_mutex);
  165. of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  166. return rc;
  167. }
  168. /**
  169. * of_node_release() - release a dynamically allocated node
  170. * @kref: kref element of the node to be released
  171. *
  172. * In of_node_put() this function is passed to kref_put() as the destructor.
  173. */
  174. void of_node_release(struct kobject *kobj)
  175. {
  176. struct device_node *node = kobj_to_device_node(kobj);
  177. struct property *prop = node->properties;
  178. /* We should never be releasing nodes that haven't been detached. */
  179. if (!of_node_check_flag(node, OF_DETACHED)) {
  180. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  181. dump_stack();
  182. return;
  183. }
  184. if (!of_node_check_flag(node, OF_DYNAMIC))
  185. return;
  186. while (prop) {
  187. struct property *next = prop->next;
  188. kfree(prop->name);
  189. kfree(prop->value);
  190. kfree(prop);
  191. prop = next;
  192. if (!prop) {
  193. prop = node->deadprops;
  194. node->deadprops = NULL;
  195. }
  196. }
  197. kfree(node->full_name);
  198. kfree(node->data);
  199. kfree(node);
  200. }
  201. /**
  202. * __of_prop_dup - Copy a property dynamically.
  203. * @prop: Property to copy
  204. * @allocflags: Allocation flags (typically pass GFP_KERNEL)
  205. *
  206. * Copy a property by dynamically allocating the memory of both the
  207. * property stucture and the property name & contents. The property's
  208. * flags have the OF_DYNAMIC bit set so that we can differentiate between
  209. * dynamically allocated properties and not.
  210. * Returns the newly allocated property or NULL on out of memory error.
  211. */
  212. struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
  213. {
  214. struct property *new;
  215. new = kzalloc(sizeof(*new), allocflags);
  216. if (!new)
  217. return NULL;
  218. /*
  219. * NOTE: There is no check for zero length value.
  220. * In case of a boolean property, this will allocate a value
  221. * of zero bytes. We do this to work around the use
  222. * of of_get_property() calls on boolean values.
  223. */
  224. new->name = kstrdup(prop->name, allocflags);
  225. new->value = kmemdup(prop->value, prop->length, allocflags);
  226. new->length = prop->length;
  227. if (!new->name || !new->value)
  228. goto err_free;
  229. /* mark the property as dynamic */
  230. of_property_set_flag(new, OF_DYNAMIC);
  231. return new;
  232. err_free:
  233. kfree(new->name);
  234. kfree(new->value);
  235. kfree(new);
  236. return NULL;
  237. }
  238. /**
  239. * __of_node_alloc() - Create an empty device node dynamically.
  240. * @full_name: Full name of the new device node
  241. * @allocflags: Allocation flags (typically pass GFP_KERNEL)
  242. *
  243. * Create an empty device tree node, suitable for further modification.
  244. * The node data are dynamically allocated and all the node flags
  245. * have the OF_DYNAMIC & OF_DETACHED bits set.
  246. * Returns the newly allocated node or NULL on out of memory error.
  247. */
  248. struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
  249. {
  250. struct device_node *node;
  251. node = kzalloc(sizeof(*node), allocflags);
  252. if (!node)
  253. return NULL;
  254. node->full_name = kstrdup(full_name, allocflags);
  255. of_node_set_flag(node, OF_DYNAMIC);
  256. of_node_set_flag(node, OF_DETACHED);
  257. if (!node->full_name)
  258. goto err_free;
  259. of_node_init(node);
  260. return node;
  261. err_free:
  262. kfree(node->full_name);
  263. kfree(node);
  264. return NULL;
  265. }
  266. static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
  267. {
  268. of_node_put(ce->np);
  269. list_del(&ce->node);
  270. kfree(ce);
  271. }
  272. #ifdef DEBUG
  273. static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  274. {
  275. switch (ce->action) {
  276. case OF_RECONFIG_ADD_PROPERTY:
  277. pr_debug("%p: %s %s/%s\n",
  278. ce, "ADD_PROPERTY ", ce->np->full_name,
  279. ce->prop->name);
  280. break;
  281. case OF_RECONFIG_REMOVE_PROPERTY:
  282. pr_debug("%p: %s %s/%s\n",
  283. ce, "REMOVE_PROPERTY", ce->np->full_name,
  284. ce->prop->name);
  285. break;
  286. case OF_RECONFIG_UPDATE_PROPERTY:
  287. pr_debug("%p: %s %s/%s\n",
  288. ce, "UPDATE_PROPERTY", ce->np->full_name,
  289. ce->prop->name);
  290. break;
  291. case OF_RECONFIG_ATTACH_NODE:
  292. pr_debug("%p: %s %s\n",
  293. ce, "ATTACH_NODE ", ce->np->full_name);
  294. break;
  295. case OF_RECONFIG_DETACH_NODE:
  296. pr_debug("%p: %s %s\n",
  297. ce, "DETACH_NODE ", ce->np->full_name);
  298. break;
  299. }
  300. }
  301. #else
  302. static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  303. {
  304. /* empty */
  305. }
  306. #endif
  307. static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
  308. struct of_changeset_entry *rce)
  309. {
  310. memcpy(rce, ce, sizeof(*rce));
  311. switch (ce->action) {
  312. case OF_RECONFIG_ATTACH_NODE:
  313. rce->action = OF_RECONFIG_DETACH_NODE;
  314. break;
  315. case OF_RECONFIG_DETACH_NODE:
  316. rce->action = OF_RECONFIG_ATTACH_NODE;
  317. break;
  318. case OF_RECONFIG_ADD_PROPERTY:
  319. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  320. break;
  321. case OF_RECONFIG_REMOVE_PROPERTY:
  322. rce->action = OF_RECONFIG_ADD_PROPERTY;
  323. break;
  324. case OF_RECONFIG_UPDATE_PROPERTY:
  325. rce->old_prop = ce->prop;
  326. rce->prop = ce->old_prop;
  327. break;
  328. }
  329. }
  330. static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
  331. {
  332. struct of_changeset_entry ce_inverted;
  333. int ret;
  334. if (revert) {
  335. __of_changeset_entry_invert(ce, &ce_inverted);
  336. ce = &ce_inverted;
  337. }
  338. switch (ce->action) {
  339. case OF_RECONFIG_ATTACH_NODE:
  340. case OF_RECONFIG_DETACH_NODE:
  341. ret = of_reconfig_notify(ce->action, ce->np);
  342. break;
  343. case OF_RECONFIG_ADD_PROPERTY:
  344. case OF_RECONFIG_REMOVE_PROPERTY:
  345. case OF_RECONFIG_UPDATE_PROPERTY:
  346. ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
  347. break;
  348. default:
  349. pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
  350. (int)ce->action);
  351. return;
  352. }
  353. if (ret)
  354. pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
  355. }
  356. static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
  357. {
  358. struct property *old_prop, **propp;
  359. unsigned long flags;
  360. int ret = 0;
  361. __of_changeset_entry_dump(ce);
  362. raw_spin_lock_irqsave(&devtree_lock, flags);
  363. switch (ce->action) {
  364. case OF_RECONFIG_ATTACH_NODE:
  365. __of_attach_node(ce->np);
  366. break;
  367. case OF_RECONFIG_DETACH_NODE:
  368. __of_detach_node(ce->np);
  369. break;
  370. case OF_RECONFIG_ADD_PROPERTY:
  371. /* If the property is in deadprops then it must be removed */
  372. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  373. if (*propp == ce->prop) {
  374. *propp = ce->prop->next;
  375. ce->prop->next = NULL;
  376. break;
  377. }
  378. }
  379. ret = __of_add_property(ce->np, ce->prop);
  380. if (ret) {
  381. pr_err("%s: add_property failed @%s/%s\n",
  382. __func__, ce->np->full_name,
  383. ce->prop->name);
  384. break;
  385. }
  386. break;
  387. case OF_RECONFIG_REMOVE_PROPERTY:
  388. ret = __of_remove_property(ce->np, ce->prop);
  389. if (ret) {
  390. pr_err("%s: remove_property failed @%s/%s\n",
  391. __func__, ce->np->full_name,
  392. ce->prop->name);
  393. break;
  394. }
  395. break;
  396. case OF_RECONFIG_UPDATE_PROPERTY:
  397. /* If the property is in deadprops then it must be removed */
  398. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  399. if (*propp == ce->prop) {
  400. *propp = ce->prop->next;
  401. ce->prop->next = NULL;
  402. break;
  403. }
  404. }
  405. ret = __of_update_property(ce->np, ce->prop, &old_prop);
  406. if (ret) {
  407. pr_err("%s: update_property failed @%s/%s\n",
  408. __func__, ce->np->full_name,
  409. ce->prop->name);
  410. break;
  411. }
  412. break;
  413. default:
  414. ret = -EINVAL;
  415. }
  416. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  417. if (ret)
  418. return ret;
  419. switch (ce->action) {
  420. case OF_RECONFIG_ATTACH_NODE:
  421. __of_attach_node_sysfs(ce->np);
  422. break;
  423. case OF_RECONFIG_DETACH_NODE:
  424. __of_detach_node_sysfs(ce->np);
  425. break;
  426. case OF_RECONFIG_ADD_PROPERTY:
  427. /* ignore duplicate names */
  428. __of_add_property_sysfs(ce->np, ce->prop);
  429. break;
  430. case OF_RECONFIG_REMOVE_PROPERTY:
  431. __of_remove_property_sysfs(ce->np, ce->prop);
  432. break;
  433. case OF_RECONFIG_UPDATE_PROPERTY:
  434. __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
  435. break;
  436. }
  437. return 0;
  438. }
  439. static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
  440. {
  441. struct of_changeset_entry ce_inverted;
  442. __of_changeset_entry_invert(ce, &ce_inverted);
  443. return __of_changeset_entry_apply(&ce_inverted);
  444. }
  445. /**
  446. * of_changeset_init - Initialize a changeset for use
  447. *
  448. * @ocs: changeset pointer
  449. *
  450. * Initialize a changeset structure
  451. */
  452. void of_changeset_init(struct of_changeset *ocs)
  453. {
  454. memset(ocs, 0, sizeof(*ocs));
  455. INIT_LIST_HEAD(&ocs->entries);
  456. }
  457. /**
  458. * of_changeset_destroy - Destroy a changeset
  459. *
  460. * @ocs: changeset pointer
  461. *
  462. * Destroys a changeset. Note that if a changeset is applied,
  463. * its changes to the tree cannot be reverted.
  464. */
  465. void of_changeset_destroy(struct of_changeset *ocs)
  466. {
  467. struct of_changeset_entry *ce, *cen;
  468. list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
  469. __of_changeset_entry_destroy(ce);
  470. }
  471. /**
  472. * of_changeset_apply - Applies a changeset
  473. *
  474. * @ocs: changeset pointer
  475. *
  476. * Applies a changeset to the live tree.
  477. * Any side-effects of live tree state changes are applied here on
  478. * sucess, like creation/destruction of devices and side-effects
  479. * like creation of sysfs properties and directories.
  480. * Returns 0 on success, a negative error value in case of an error.
  481. * On error the partially applied effects are reverted.
  482. */
  483. int of_changeset_apply(struct of_changeset *ocs)
  484. {
  485. struct of_changeset_entry *ce;
  486. int ret;
  487. /* perform the rest of the work */
  488. pr_debug("of_changeset: applying...\n");
  489. list_for_each_entry(ce, &ocs->entries, node) {
  490. ret = __of_changeset_entry_apply(ce);
  491. if (ret) {
  492. pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
  493. list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
  494. __of_changeset_entry_revert(ce);
  495. return ret;
  496. }
  497. }
  498. pr_debug("of_changeset: applied, emitting notifiers.\n");
  499. /* drop the global lock while emitting notifiers */
  500. mutex_unlock(&of_mutex);
  501. list_for_each_entry(ce, &ocs->entries, node)
  502. __of_changeset_entry_notify(ce, 0);
  503. mutex_lock(&of_mutex);
  504. pr_debug("of_changeset: notifiers sent.\n");
  505. return 0;
  506. }
  507. /**
  508. * of_changeset_revert - Reverts an applied changeset
  509. *
  510. * @ocs: changeset pointer
  511. *
  512. * Reverts a changeset returning the state of the tree to what it
  513. * was before the application.
  514. * Any side-effects like creation/destruction of devices and
  515. * removal of sysfs properties and directories are applied.
  516. * Returns 0 on success, a negative error value in case of an error.
  517. */
  518. int of_changeset_revert(struct of_changeset *ocs)
  519. {
  520. struct of_changeset_entry *ce;
  521. int ret;
  522. pr_debug("of_changeset: reverting...\n");
  523. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  524. ret = __of_changeset_entry_revert(ce);
  525. if (ret) {
  526. pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
  527. list_for_each_entry_continue(ce, &ocs->entries, node)
  528. __of_changeset_entry_apply(ce);
  529. return ret;
  530. }
  531. }
  532. pr_debug("of_changeset: reverted, emitting notifiers.\n");
  533. /* drop the global lock while emitting notifiers */
  534. mutex_unlock(&of_mutex);
  535. list_for_each_entry_reverse(ce, &ocs->entries, node)
  536. __of_changeset_entry_notify(ce, 1);
  537. mutex_lock(&of_mutex);
  538. pr_debug("of_changeset: notifiers sent.\n");
  539. return 0;
  540. }
  541. /**
  542. * of_changeset_action - Perform a changeset action
  543. *
  544. * @ocs: changeset pointer
  545. * @action: action to perform
  546. * @np: Pointer to device node
  547. * @prop: Pointer to property
  548. *
  549. * On action being one of:
  550. * + OF_RECONFIG_ATTACH_NODE
  551. * + OF_RECONFIG_DETACH_NODE,
  552. * + OF_RECONFIG_ADD_PROPERTY
  553. * + OF_RECONFIG_REMOVE_PROPERTY,
  554. * + OF_RECONFIG_UPDATE_PROPERTY
  555. * Returns 0 on success, a negative error value in case of an error.
  556. */
  557. int of_changeset_action(struct of_changeset *ocs, unsigned long action,
  558. struct device_node *np, struct property *prop)
  559. {
  560. struct of_changeset_entry *ce;
  561. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  562. if (!ce) {
  563. pr_err("%s: Failed to allocate\n", __func__);
  564. return -ENOMEM;
  565. }
  566. /* get a reference to the node */
  567. ce->action = action;
  568. ce->np = of_node_get(np);
  569. ce->prop = prop;
  570. if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
  571. ce->old_prop = of_find_property(np, prop->name, NULL);
  572. /* add it to the list */
  573. list_add_tail(&ce->node, &ocs->entries);
  574. return 0;
  575. }