device.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * (C) Copyright 2012
  5. * Pavel Herrmann <morpheus.ibis@gmail.com>
  6. * Marek Vasut <marex@denx.de>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #ifndef _DM_DEVICE_H
  11. #define _DM_DEVICE_H
  12. #include <dm/uclass-id.h>
  13. #include <fdtdec.h>
  14. #include <linker_lists.h>
  15. #include <linux/compat.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. struct driver_info;
  19. /* Driver is active (probed). Cleared when it is removed */
  20. #define DM_FLAG_ACTIVATED (1 << 0)
  21. /* DM is responsible for allocating and freeing platdata */
  22. #define DM_FLAG_ALLOC_PDATA (1 << 1)
  23. /* DM should init this device prior to relocation */
  24. #define DM_FLAG_PRE_RELOC (1 << 2)
  25. /* DM is responsible for allocating and freeing parent_platdata */
  26. #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
  27. /* DM is responsible for allocating and freeing uclass_platdata */
  28. #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
  29. /* Allocate driver private data on a DMA boundary */
  30. #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
  31. /* Device is bound */
  32. #define DM_FLAG_BOUND (1 << 6)
  33. /**
  34. * struct udevice - An instance of a driver
  35. *
  36. * This holds information about a device, which is a driver bound to a
  37. * particular port or peripheral (essentially a driver instance).
  38. *
  39. * A device will come into existence through a 'bind' call, either due to
  40. * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
  41. * in the device tree (in which case of_offset is >= 0). In the latter case
  42. * we translate the device tree information into platdata in a function
  43. * implemented by the driver ofdata_to_platdata method (called just before the
  44. * probe method if the device has a device tree node.
  45. *
  46. * All three of platdata, priv and uclass_priv can be allocated by the
  47. * driver, or you can use the auto_alloc_size members of struct driver and
  48. * struct uclass_driver to have driver model do this automatically.
  49. *
  50. * @driver: The driver used by this device
  51. * @name: Name of device, typically the FDT node name
  52. * @platdata: Configuration data for this device
  53. * @parent_platdata: The parent bus's configuration data for this device
  54. * @uclass_platdata: The uclass's configuration data for this device
  55. * @of_offset: Device tree node offset for this device (- for none)
  56. * @driver_data: Driver data word for the entry that matched this device with
  57. * its driver
  58. * @parent: Parent of this device, or NULL for the top level device
  59. * @priv: Private data for this device
  60. * @uclass: Pointer to uclass for this device
  61. * @uclass_priv: The uclass's private data for this device
  62. * @parent_priv: The parent's private data for this device
  63. * @uclass_node: Used by uclass to link its devices
  64. * @child_head: List of children of this device
  65. * @sibling_node: Next device in list of all devices
  66. * @flags: Flags for this device DM_FLAG_...
  67. * @req_seq: Requested sequence number for this device (-1 = any)
  68. * @seq: Allocated sequence number for this device (-1 = none). This is set up
  69. * when the device is probed and will be unique within the device's uclass.
  70. */
  71. struct udevice {
  72. const struct driver *driver;
  73. const char *name;
  74. void *platdata;
  75. void *parent_platdata;
  76. void *uclass_platdata;
  77. int of_offset;
  78. ulong driver_data;
  79. struct udevice *parent;
  80. void *priv;
  81. struct uclass *uclass;
  82. void *uclass_priv;
  83. void *parent_priv;
  84. struct list_head uclass_node;
  85. struct list_head child_head;
  86. struct list_head sibling_node;
  87. uint32_t flags;
  88. int req_seq;
  89. int seq;
  90. #ifdef CONFIG_DEVRES
  91. struct list_head devres_head;
  92. #endif
  93. };
  94. /* Maximum sequence number supported */
  95. #define DM_MAX_SEQ 999
  96. /* Returns the operations for a device */
  97. #define device_get_ops(dev) (dev->driver->ops)
  98. /* Returns non-zero if the device is active (probed and not removed) */
  99. #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
  100. /**
  101. * struct udevice_id - Lists the compatible strings supported by a driver
  102. * @compatible: Compatible string
  103. * @data: Data for this compatible string
  104. */
  105. struct udevice_id {
  106. const char *compatible;
  107. ulong data;
  108. };
  109. #if CONFIG_IS_ENABLED(OF_CONTROL)
  110. #define of_match_ptr(_ptr) (_ptr)
  111. #else
  112. #define of_match_ptr(_ptr) NULL
  113. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  114. /**
  115. * struct driver - A driver for a feature or peripheral
  116. *
  117. * This holds methods for setting up a new device, and also removing it.
  118. * The device needs information to set itself up - this is provided either
  119. * by platdata or a device tree node (which we find by looking up
  120. * matching compatible strings with of_match).
  121. *
  122. * Drivers all belong to a uclass, representing a class of devices of the
  123. * same type. Common elements of the drivers can be implemented in the uclass,
  124. * or the uclass can provide a consistent interface to the drivers within
  125. * it.
  126. *
  127. * @name: Device name
  128. * @id: Identiies the uclass we belong to
  129. * @of_match: List of compatible strings to match, and any identifying data
  130. * for each.
  131. * @bind: Called to bind a device to its driver
  132. * @probe: Called to probe a device, i.e. activate it
  133. * @remove: Called to remove a device, i.e. de-activate it
  134. * @unbind: Called to unbind a device from its driver
  135. * @ofdata_to_platdata: Called before probe to decode device tree data
  136. * @child_post_bind: Called after a new child has been bound
  137. * @child_pre_probe: Called before a child device is probed. The device has
  138. * memory allocated but it has not yet been probed.
  139. * @child_post_remove: Called after a child device is removed. The device
  140. * has memory allocated but its device_remove() method has been called.
  141. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  142. * to be allocated in the device's ->priv pointer. If zero, then the driver
  143. * is responsible for allocating any data required.
  144. * @platdata_auto_alloc_size: If non-zero this is the size of the
  145. * platform data to be allocated in the device's ->platdata pointer.
  146. * This is typically only useful for device-tree-aware drivers (those with
  147. * an of_match), since drivers which use platdata will have the data
  148. * provided in the U_BOOT_DEVICE() instantiation.
  149. * @per_child_auto_alloc_size: Each device can hold private data owned by
  150. * its parent. If required this will be automatically allocated if this
  151. * value is non-zero.
  152. * TODO(sjg@chromium.org): I'm considering dropping this, and just having
  153. * device_probe_child() pass it in. So far the use case for allocating it
  154. * is SPI, but I found that unsatisfactory. Since it is here I will leave it
  155. * until things are clearer.
  156. * @per_child_platdata_auto_alloc_size: A bus likes to store information about
  157. * its children. If non-zero this is the size of this data, to be allocated
  158. * in the child's parent_platdata pointer.
  159. * @ops: Driver-specific operations. This is typically a list of function
  160. * pointers defined by the driver, to implement driver functions required by
  161. * the uclass.
  162. * @flags: driver flags - see DM_FLAGS_...
  163. */
  164. struct driver {
  165. char *name;
  166. enum uclass_id id;
  167. const struct udevice_id *of_match;
  168. int (*bind)(struct udevice *dev);
  169. int (*probe)(struct udevice *dev);
  170. int (*remove)(struct udevice *dev);
  171. int (*unbind)(struct udevice *dev);
  172. int (*ofdata_to_platdata)(struct udevice *dev);
  173. int (*child_post_bind)(struct udevice *dev);
  174. int (*child_pre_probe)(struct udevice *dev);
  175. int (*child_post_remove)(struct udevice *dev);
  176. int priv_auto_alloc_size;
  177. int platdata_auto_alloc_size;
  178. int per_child_auto_alloc_size;
  179. int per_child_platdata_auto_alloc_size;
  180. const void *ops; /* driver-specific operations */
  181. uint32_t flags;
  182. };
  183. /* Declare a new U-Boot driver */
  184. #define U_BOOT_DRIVER(__name) \
  185. ll_entry_declare(struct driver, __name, driver)
  186. /**
  187. * dev_get_platdata() - Get the platform data for a device
  188. *
  189. * This checks that dev is not NULL, but no other checks for now
  190. *
  191. * @dev Device to check
  192. * @return platform data, or NULL if none
  193. */
  194. void *dev_get_platdata(struct udevice *dev);
  195. /**
  196. * dev_get_parent_platdata() - Get the parent platform data for a device
  197. *
  198. * This checks that dev is not NULL, but no other checks for now
  199. *
  200. * @dev Device to check
  201. * @return parent's platform data, or NULL if none
  202. */
  203. void *dev_get_parent_platdata(struct udevice *dev);
  204. /**
  205. * dev_get_uclass_platdata() - Get the uclass platform data for a device
  206. *
  207. * This checks that dev is not NULL, but no other checks for now
  208. *
  209. * @dev Device to check
  210. * @return uclass's platform data, or NULL if none
  211. */
  212. void *dev_get_uclass_platdata(struct udevice *dev);
  213. /**
  214. * dev_get_parent_priv() - Get the parent private data for a device
  215. *
  216. * The parent private data is data stored in the device but owned by the
  217. * parent. For example, a USB device may have parent data which contains
  218. * information about how to talk to the device over USB.
  219. *
  220. * This checks that dev is not NULL, but no other checks for now
  221. *
  222. * @dev Device to check
  223. * @return parent data, or NULL if none
  224. */
  225. void *dev_get_parent_priv(struct udevice *dev);
  226. /**
  227. * dev_get_priv() - Get the private data for a device
  228. *
  229. * This checks that dev is not NULL, but no other checks for now
  230. *
  231. * @dev Device to check
  232. * @return private data, or NULL if none
  233. */
  234. void *dev_get_priv(struct udevice *dev);
  235. /**
  236. * struct dev_get_parent() - Get the parent of a device
  237. *
  238. * @child: Child to check
  239. * @return parent of child, or NULL if this is the root device
  240. */
  241. struct udevice *dev_get_parent(struct udevice *child);
  242. /**
  243. * dev_get_uclass_priv() - Get the private uclass data for a device
  244. *
  245. * This checks that dev is not NULL, but no other checks for now
  246. *
  247. * @dev Device to check
  248. * @return private uclass data for this device, or NULL if none
  249. */
  250. void *dev_get_uclass_priv(struct udevice *dev);
  251. /**
  252. * dev_get_driver_data() - get the driver data used to bind a device
  253. *
  254. * When a device is bound using a device tree node, it matches a
  255. * particular compatible string as in struct udevice_id. This function
  256. * returns the associated data value for that compatible string. This is
  257. * the 'data' field in struct udevice_id.
  258. *
  259. * For USB devices, this is the driver_info field in struct usb_device_id.
  260. *
  261. * @dev: Device to check
  262. */
  263. ulong dev_get_driver_data(struct udevice *dev);
  264. /**
  265. * dev_get_driver_ops() - get the device's driver's operations
  266. *
  267. * This checks that dev is not NULL, and returns the pointer to device's
  268. * driver's operations.
  269. *
  270. * @dev: Device to check
  271. * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
  272. */
  273. const void *dev_get_driver_ops(struct udevice *dev);
  274. /*
  275. * device_get_uclass_id() - return the uclass ID of a device
  276. *
  277. * @dev: Device to check
  278. * @return uclass ID for the device
  279. */
  280. enum uclass_id device_get_uclass_id(struct udevice *dev);
  281. /*
  282. * dev_get_uclass_name() - return the uclass name of a device
  283. *
  284. * This checks that dev is not NULL.
  285. *
  286. * @dev: Device to check
  287. * @return pointer to the uclass name for the device
  288. */
  289. const char *dev_get_uclass_name(struct udevice *dev);
  290. /**
  291. * device_get_child() - Get the child of a device by index
  292. *
  293. * Returns the numbered child, 0 being the first. This does not use
  294. * sequence numbers, only the natural order.
  295. *
  296. * @dev: Parent device to check
  297. * @index: Child index
  298. * @devp: Returns pointer to device
  299. * @return 0 if OK, -ENODEV if no such device, other error if the device fails
  300. * to probe
  301. */
  302. int device_get_child(struct udevice *parent, int index, struct udevice **devp);
  303. /**
  304. * device_find_child_by_seq() - Find a child device based on a sequence
  305. *
  306. * This searches for a device with the given seq or req_seq.
  307. *
  308. * For seq, if an active device has this sequence it will be returned.
  309. * If there is no such device then this will return -ENODEV.
  310. *
  311. * For req_seq, if a device (whether activated or not) has this req_seq
  312. * value, that device will be returned. This is a strong indication that
  313. * the device will receive that sequence when activated.
  314. *
  315. * @parent: Parent device
  316. * @seq_or_req_seq: Sequence number to find (0=first)
  317. * @find_req_seq: true to find req_seq, false to find seq
  318. * @devp: Returns pointer to device (there is only one per for each seq).
  319. * Set to NULL if none is found
  320. * @return 0 if OK, -ve on error
  321. */
  322. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  323. bool find_req_seq, struct udevice **devp);
  324. /**
  325. * device_get_child_by_seq() - Get a child device based on a sequence
  326. *
  327. * If an active device has this sequence it will be returned. If there is no
  328. * such device then this will check for a device that is requesting this
  329. * sequence.
  330. *
  331. * The device is probed to activate it ready for use.
  332. *
  333. * @parent: Parent device
  334. * @seq: Sequence number to find (0=first)
  335. * @devp: Returns pointer to device (there is only one per for each seq)
  336. * Set to NULL if none is found
  337. * @return 0 if OK, -ve on error
  338. */
  339. int device_get_child_by_seq(struct udevice *parent, int seq,
  340. struct udevice **devp);
  341. /**
  342. * device_find_child_by_of_offset() - Find a child device based on FDT offset
  343. *
  344. * Locates a child device by its device tree offset.
  345. *
  346. * @parent: Parent device
  347. * @of_offset: Device tree offset to find
  348. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  349. * @return 0 if OK, -ve on error
  350. */
  351. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  352. struct udevice **devp);
  353. /**
  354. * device_get_child_by_of_offset() - Get a child device based on FDT offset
  355. *
  356. * Locates a child device by its device tree offset.
  357. *
  358. * The device is probed to activate it ready for use.
  359. *
  360. * @parent: Parent device
  361. * @of_offset: Device tree offset to find
  362. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  363. * @return 0 if OK, -ve on error
  364. */
  365. int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
  366. struct udevice **devp);
  367. /**
  368. * device_get_global_by_of_offset() - Get a device based on FDT offset
  369. *
  370. * Locates a device by its device tree offset, searching globally throughout
  371. * the all driver model devices.
  372. *
  373. * The device is probed to activate it ready for use.
  374. *
  375. * @of_offset: Device tree offset to find
  376. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  377. * @return 0 if OK, -ve on error
  378. */
  379. int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
  380. /**
  381. * device_find_first_child() - Find the first child of a device
  382. *
  383. * @parent: Parent device to search
  384. * @devp: Returns first child device, or NULL if none
  385. * @return 0
  386. */
  387. int device_find_first_child(struct udevice *parent, struct udevice **devp);
  388. /**
  389. * device_find_next_child() - Find the next child of a device
  390. *
  391. * @devp: Pointer to previous child device on entry. Returns pointer to next
  392. * child device, or NULL if none
  393. * @return 0
  394. */
  395. int device_find_next_child(struct udevice **devp);
  396. /**
  397. * dev_get_addr() - Get the reg property of a device
  398. *
  399. * @dev: Pointer to a device
  400. *
  401. * @return addr
  402. */
  403. fdt_addr_t dev_get_addr(struct udevice *dev);
  404. /**
  405. * device_has_children() - check if a device has any children
  406. *
  407. * @dev: Device to check
  408. * @return true if the device has one or more children
  409. */
  410. bool device_has_children(struct udevice *dev);
  411. /**
  412. * device_has_active_children() - check if a device has any active children
  413. *
  414. * @dev: Device to check
  415. * @return true if the device has one or more children and at least one of
  416. * them is active (probed).
  417. */
  418. bool device_has_active_children(struct udevice *dev);
  419. /**
  420. * device_is_last_sibling() - check if a device is the last sibling
  421. *
  422. * This function can be useful for display purposes, when special action needs
  423. * to be taken when displaying the last sibling. This can happen when a tree
  424. * view of devices is being displayed.
  425. *
  426. * @dev: Device to check
  427. * @return true if there are no more siblings after this one - i.e. is it
  428. * last in the list.
  429. */
  430. bool device_is_last_sibling(struct udevice *dev);
  431. /**
  432. * device_set_name() - set the name of a device
  433. *
  434. * This must be called in the device's bind() method and no later. Normally
  435. * this is unnecessary but for probed devices which don't get a useful name
  436. * this function can be helpful.
  437. *
  438. * @dev: Device to update
  439. * @name: New name (this string is allocated new memory and attached to
  440. * the device)
  441. * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
  442. * string
  443. */
  444. int device_set_name(struct udevice *dev, const char *name);
  445. /**
  446. * device_is_on_pci_bus - Test if a device is on a PCI bus
  447. *
  448. * @dev: device to test
  449. * @return: true if it is on a PCI bus, false otherwise
  450. */
  451. static inline bool device_is_on_pci_bus(struct udevice *dev)
  452. {
  453. return device_get_uclass_id(dev->parent) == UCLASS_PCI;
  454. }
  455. /* device resource management */
  456. typedef void (*dr_release_t)(struct udevice *dev, void *res);
  457. typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
  458. #ifdef CONFIG_DEVRES
  459. #ifdef CONFIG_DEBUG_DEVRES
  460. void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  461. const char *name);
  462. #define _devres_alloc(release, size, gfp) \
  463. __devres_alloc(release, size, gfp, #release)
  464. #else
  465. void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  466. #endif
  467. /**
  468. * devres_alloc - Allocate device resource data
  469. * @release: Release function devres will be associated with
  470. * @size: Allocation size
  471. * @gfp: Allocation flags
  472. *
  473. * Allocate devres of @size bytes. The allocated area is associated
  474. * with @release. The returned pointer can be passed to
  475. * other devres_*() functions.
  476. *
  477. * RETURNS:
  478. * Pointer to allocated devres on success, NULL on failure.
  479. */
  480. #define devres_alloc(release, size, gfp) \
  481. _devres_alloc(release, size, gfp | __GFP_ZERO)
  482. /**
  483. * devres_free - Free device resource data
  484. * @res: Pointer to devres data to free
  485. *
  486. * Free devres created with devres_alloc().
  487. */
  488. void devres_free(void *res);
  489. /**
  490. * devres_add - Register device resource
  491. * @dev: Device to add resource to
  492. * @res: Resource to register
  493. *
  494. * Register devres @res to @dev. @res should have been allocated
  495. * using devres_alloc(). On driver detach, the associated release
  496. * function will be invoked and devres will be freed automatically.
  497. */
  498. void devres_add(struct udevice *dev, void *res);
  499. /**
  500. * devres_find - Find device resource
  501. * @dev: Device to lookup resource from
  502. * @release: Look for resources associated with this release function
  503. * @match: Match function (optional)
  504. * @match_data: Data for the match function
  505. *
  506. * Find the latest devres of @dev which is associated with @release
  507. * and for which @match returns 1. If @match is NULL, it's considered
  508. * to match all.
  509. *
  510. * RETURNS:
  511. * Pointer to found devres, NULL if not found.
  512. */
  513. void *devres_find(struct udevice *dev, dr_release_t release,
  514. dr_match_t match, void *match_data);
  515. /**
  516. * devres_get - Find devres, if non-existent, add one atomically
  517. * @dev: Device to lookup or add devres for
  518. * @new_res: Pointer to new initialized devres to add if not found
  519. * @match: Match function (optional)
  520. * @match_data: Data for the match function
  521. *
  522. * Find the latest devres of @dev which has the same release function
  523. * as @new_res and for which @match return 1. If found, @new_res is
  524. * freed; otherwise, @new_res is added atomically.
  525. *
  526. * RETURNS:
  527. * Pointer to found or added devres.
  528. */
  529. void *devres_get(struct udevice *dev, void *new_res,
  530. dr_match_t match, void *match_data);
  531. /**
  532. * devres_remove - Find a device resource and remove it
  533. * @dev: Device to find resource from
  534. * @release: Look for resources associated with this release function
  535. * @match: Match function (optional)
  536. * @match_data: Data for the match function
  537. *
  538. * Find the latest devres of @dev associated with @release and for
  539. * which @match returns 1. If @match is NULL, it's considered to
  540. * match all. If found, the resource is removed atomically and
  541. * returned.
  542. *
  543. * RETURNS:
  544. * Pointer to removed devres on success, NULL if not found.
  545. */
  546. void *devres_remove(struct udevice *dev, dr_release_t release,
  547. dr_match_t match, void *match_data);
  548. /**
  549. * devres_destroy - Find a device resource and destroy it
  550. * @dev: Device to find resource from
  551. * @release: Look for resources associated with this release function
  552. * @match: Match function (optional)
  553. * @match_data: Data for the match function
  554. *
  555. * Find the latest devres of @dev associated with @release and for
  556. * which @match returns 1. If @match is NULL, it's considered to
  557. * match all. If found, the resource is removed atomically and freed.
  558. *
  559. * Note that the release function for the resource will not be called,
  560. * only the devres-allocated data will be freed. The caller becomes
  561. * responsible for freeing any other data.
  562. *
  563. * RETURNS:
  564. * 0 if devres is found and freed, -ENOENT if not found.
  565. */
  566. int devres_destroy(struct udevice *dev, dr_release_t release,
  567. dr_match_t match, void *match_data);
  568. /**
  569. * devres_release - Find a device resource and destroy it, calling release
  570. * @dev: Device to find resource from
  571. * @release: Look for resources associated with this release function
  572. * @match: Match function (optional)
  573. * @match_data: Data for the match function
  574. *
  575. * Find the latest devres of @dev associated with @release and for
  576. * which @match returns 1. If @match is NULL, it's considered to
  577. * match all. If found, the resource is removed atomically, the
  578. * release function called and the resource freed.
  579. *
  580. * RETURNS:
  581. * 0 if devres is found and freed, -ENOENT if not found.
  582. */
  583. int devres_release(struct udevice *dev, dr_release_t release,
  584. dr_match_t match, void *match_data);
  585. /* managed devm_k.alloc/kfree for device drivers */
  586. /**
  587. * devm_kmalloc - Resource-managed kmalloc
  588. * @dev: Device to allocate memory for
  589. * @size: Allocation size
  590. * @gfp: Allocation gfp flags
  591. *
  592. * Managed kmalloc. Memory allocated with this function is
  593. * automatically freed on driver detach. Like all other devres
  594. * resources, guaranteed alignment is unsigned long long.
  595. *
  596. * RETURNS:
  597. * Pointer to allocated memory on success, NULL on failure.
  598. */
  599. void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
  600. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  601. {
  602. return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
  603. }
  604. static inline void *devm_kmalloc_array(struct udevice *dev,
  605. size_t n, size_t size, gfp_t flags)
  606. {
  607. if (size != 0 && n > SIZE_MAX / size)
  608. return NULL;
  609. return devm_kmalloc(dev, n * size, flags);
  610. }
  611. static inline void *devm_kcalloc(struct udevice *dev,
  612. size_t n, size_t size, gfp_t flags)
  613. {
  614. return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
  615. }
  616. /**
  617. * devm_kfree - Resource-managed kfree
  618. * @dev: Device this memory belongs to
  619. * @p: Memory to free
  620. *
  621. * Free memory allocated with devm_kmalloc().
  622. */
  623. void devm_kfree(struct udevice *dev, void *p);
  624. #else /* ! CONFIG_DEVRES */
  625. static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
  626. {
  627. return kzalloc(size, gfp);
  628. }
  629. static inline void devres_free(void *res)
  630. {
  631. kfree(res);
  632. }
  633. static inline void devres_add(struct udevice *dev, void *res)
  634. {
  635. }
  636. static inline void *devres_find(struct udevice *dev, dr_release_t release,
  637. dr_match_t match, void *match_data)
  638. {
  639. return NULL;
  640. }
  641. static inline void *devres_get(struct udevice *dev, void *new_res,
  642. dr_match_t match, void *match_data)
  643. {
  644. return NULL;
  645. }
  646. static inline void *devres_remove(struct udevice *dev, dr_release_t release,
  647. dr_match_t match, void *match_data)
  648. {
  649. return NULL;
  650. }
  651. static inline int devres_destroy(struct udevice *dev, dr_release_t release,
  652. dr_match_t match, void *match_data)
  653. {
  654. return 0;
  655. }
  656. static inline int devres_release(struct udevice *dev, dr_release_t release,
  657. dr_match_t match, void *match_data)
  658. {
  659. return 0;
  660. }
  661. static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
  662. {
  663. return kmalloc(size, gfp);
  664. }
  665. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  666. {
  667. return kzalloc(size, gfp);
  668. }
  669. static inline void *devm_kmaloc_array(struct udevice *dev,
  670. size_t n, size_t size, gfp_t flags)
  671. {
  672. /* TODO: add kmalloc_array() to linux/compat.h */
  673. if (size != 0 && n > SIZE_MAX / size)
  674. return NULL;
  675. return kmalloc(n * size, flags);
  676. }
  677. static inline void *devm_kcalloc(struct udevice *dev,
  678. size_t n, size_t size, gfp_t flags)
  679. {
  680. /* TODO: add kcalloc() to linux/compat.h */
  681. return kmalloc(n * size, flags | __GFP_ZERO);
  682. }
  683. static inline void devm_kfree(struct udevice *dev, void *p)
  684. {
  685. kfree(p);
  686. }
  687. #endif /* ! CONFIG_DEVRES */
  688. #endif