ump.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. *
  3. * (C) COPYRIGHT ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. /**
  16. * @file
  17. *
  18. * This file contains the kernel space part of the UMP API.
  19. *
  20. */
  21. #ifndef _UMP_KERNEL_INTERFACE_H_
  22. #define _UMP_KERNEL_INTERFACE_H_
  23. /**
  24. * @addtogroup ump_api
  25. * @{
  26. */
  27. /** @defgroup ump_kernel_space_api UMP Kernel Space API
  28. * @{ */
  29. /**
  30. * External representation of a UMP handle in kernel space.
  31. */
  32. typedef void * ump_dd_handle;
  33. #ifdef CONFIG_KDS
  34. #include <linux/kds.h>
  35. #endif
  36. #include <linux/ump-common.h>
  37. #define UMP_KERNEL_API_EXPORT
  38. #if defined(__KERNEL__)
  39. #include <linux/ump-import.h>
  40. #else
  41. #include <ump/src/library/common/ump_user.h>
  42. #endif
  43. #ifdef __cplusplus
  44. extern "C"
  45. {
  46. #endif
  47. /**
  48. * Value to indicate an invalid UMP memory handle.
  49. */
  50. #define UMP_DD_INVALID_MEMORY_HANDLE ((ump_dd_handle)0)
  51. /**
  52. * Struct used to describe a physical block used by UMP memory
  53. */
  54. typedef struct ump_dd_physical_block_64
  55. {
  56. uint64_t addr; /**< The physical address of the block */
  57. uint64_t size; /**< The length of the block, in bytes, typically page aligned */
  58. } ump_dd_physical_block_64;
  59. /**
  60. * Security filter hook.
  61. *
  62. * Each allocation can have a security filter attached to it.@n
  63. * The hook receives
  64. * @li the secure ID
  65. * @li a handle to the allocation
  66. * @li the callback_data argument provided to @ref ump_dd_allocate_64 or @ref ump_dd_create_from_phys_blocks_64
  67. *
  68. * The hook must return @a MALI_TRUE to indicate that access to the handle is allowed or @n
  69. * @a MALI_FALSE to state that no access is permitted.@n
  70. * This hook is guaranteed to be called in the context of the requesting process/address space.
  71. *
  72. * The arguments provided to the hook are;
  73. * @li the secure ID
  74. * @li handle to the allocation
  75. * @li the callback_data set when registering the hook
  76. *
  77. * Return value;
  78. * @li @a TRUE to permit access
  79. * @li @a FALSE to deny access
  80. */
  81. typedef bool (*ump_dd_security_filter)(ump_secure_id, ump_dd_handle, void *);
  82. /**
  83. * Final release notify hook.
  84. *
  85. * Allocations can have a hook attached to them which is called when the last reference to the allocation is released.
  86. * No reference count manipulation is allowed on the provided handle, just property querying (ID get, size get, phys block get).
  87. * This is similar to finalizers in OO languages.
  88. *
  89. * The arguments provided to the hook are;
  90. * * handle to the allocation
  91. * * the callback_data set when registering the hook
  92. */
  93. typedef void (*ump_dd_final_release_callback)(const ump_dd_handle, void *);
  94. /**
  95. * Allocate a buffer.
  96. * The lifetime of the allocation is controlled by a reference count.
  97. * The reference count of the returned buffer is set to 1.
  98. * The memory will be freed once the reference count reaches 0.
  99. * Use @ref ump_dd_retain and @ref ump_dd_release to control the reference count.
  100. * @param size Number of bytes to allocate. Will be padded up to a multiple of the page size.
  101. * @param flags Bit-wise OR of zero or more of the allocation flag bits.
  102. * @param[in] filter_func Pointer to a function which will be called when an allocation is required from a
  103. * secure id before the allocation itself is returned to user-space.
  104. * NULL permitted if no need for a callback.
  105. * @param[in] final_release_func Pointer to a function which will be called when the last reference is removed,
  106. * just before the allocation is freed. NULL permitted if no need for a callback.
  107. * @param[in] callback_data An opaque pointer which will be provided to @a filter_func and @a final_release_func
  108. * @return Handle to the new allocation, or @a UMP_DD_INVALID_MEMORY_HANDLE on allocation failure.
  109. */
  110. UMP_KERNEL_API_EXPORT ump_dd_handle ump_dd_allocate_64(uint64_t size, ump_alloc_flags flags, ump_dd_security_filter filter_func, ump_dd_final_release_callback final_release_func, void* callback_data);
  111. /**
  112. * Allocation bits getter.
  113. * Retrieves the allocation flags used when instantiating the given handle.
  114. * Just a copy of the flag given to @ref ump_dd_allocate_64 and @ref ump_dd_create_from_phys_blocks_64
  115. * @param mem The handle retrieve the bits for
  116. * @return The allocation bits used to instantiate the allocation
  117. */
  118. UMP_KERNEL_API_EXPORT ump_alloc_flags ump_dd_allocation_flags_get(const ump_dd_handle mem);
  119. /**
  120. * Retrieves the secure ID for the specified UMP memory.
  121. *
  122. * This identifier is unique across the entire system, and uniquely identifies
  123. * the specified UMP memory allocation. This identifier can later be used through the
  124. * @ref ump_dd_from_secure_id or
  125. * @ref ump_from_secure_id
  126. * functions in order to access this UMP memory, for instance from another process (if shared of course).
  127. * Unless the allocation was marked as shared the returned ID will only be resolvable in the same process as did the allocation.
  128. *
  129. * Calling on an @a UMP_DD_INVALID_MEMORY_HANDLE will result in undefined behavior.
  130. * Debug builds will assert on this.
  131. *
  132. * @note There is a user space equivalent function called @ref ump_secure_id_get
  133. *
  134. * @see ump_dd_from_secure_id
  135. * @see ump_from_secure_id
  136. * @see ump_secure_id_get
  137. *
  138. * @param mem Handle to UMP memory.
  139. *
  140. * @return Returns the secure ID for the specified UMP memory.
  141. */
  142. UMP_KERNEL_API_EXPORT ump_secure_id ump_dd_secure_id_get(const ump_dd_handle mem);
  143. #ifdef CONFIG_KDS
  144. /**
  145. * Retrieve the KDS resource for the specified UMP memory.
  146. *
  147. * The KDS resource should be used to synchronize access to the UMP allocation.
  148. * See the KDS API for how to do that.
  149. *
  150. * @param mem Handle to the UMP memory to query.
  151. * @return Pointer to the KDS resource controlling access to the UMP memory.
  152. */
  153. UMP_KERNEL_API_EXPORT struct kds_resource * ump_dd_kds_resource_get(const ump_dd_handle mem);
  154. #endif
  155. /**
  156. * Retrieves a handle to allocated UMP memory.
  157. *
  158. * The usage of UMP memory is reference counted, so this will increment the reference
  159. * count by one for the specified UMP memory.
  160. * Use @ref ump_dd_release when there is no longer any
  161. * use for the retrieved handle.
  162. *
  163. * If called on an non-shared allocation and this is a different process @a UMP_DD_INVALID_MEMORY_HANDLE will be returned.
  164. *
  165. * Calling on an @a UMP_INVALID_SECURE_ID will return @a UMP_DD_INVALID_MEMORY_HANDLE
  166. *
  167. * @note There is a user space equivalent function called @ref ump_from_secure_id
  168. *
  169. * @see ump_dd_release
  170. * @see ump_from_secure_id
  171. *
  172. * @param secure_id The secure ID of the UMP memory to open, that can be retrieved using the @ref ump_secure_id_get function.
  173. *
  174. * @return @a UMP_DD_INVALID_MEMORY_HANDLE indicates failure, otherwise a valid handle is returned.
  175. */
  176. UMP_KERNEL_API_EXPORT ump_dd_handle ump_dd_from_secure_id(ump_secure_id secure_id);
  177. /**
  178. * Retrieves all physical memory block information for specified UMP memory.
  179. *
  180. * This function can be used by other device drivers in order to create MMU tables.
  181. * This function will return a pointer to an array of @ref ump_dd_physical_block_64 in @a pArray and the number of array elements in @a pCount
  182. *
  183. * Calling on an @a UMP_DD_INVALID_MEMORY_HANDLE results in undefined behavior.
  184. * Debug builds will assert on this.
  185. *
  186. * @param mem Handle to UMP memory.
  187. * @param[out] pCount Pointer to where to store the number of items in the returned array
  188. * @param[out] pArray Pointer to where to store a pointer to the physical blocks array
  189. */
  190. UMP_KERNEL_API_EXPORT void ump_dd_phys_blocks_get_64(const ump_dd_handle mem, uint64_t * const pCount, const ump_dd_physical_block_64 ** const pArray);
  191. /**
  192. * Retrieves the actual size of the specified UMP memory.
  193. *
  194. * The size is reported in bytes, and is typically page aligned.
  195. *
  196. * Calling on an @a UMP_DD_INVALID_MEMORY_HANDLE results in undefined behavior.
  197. * Debug builds will assert on this.
  198. *
  199. * @note There is a user space equivalent function called @ref ump_size_get
  200. *
  201. * @see ump_size_get
  202. *
  203. * @param mem Handle to UMP memory.
  204. *
  205. * @return Returns the allocated size of the specified UMP memory, in bytes.
  206. */
  207. UMP_KERNEL_API_EXPORT uint64_t ump_dd_size_get_64(const ump_dd_handle mem);
  208. /**
  209. * Adds an extra reference to the specified UMP memory allocation.
  210. *
  211. * The function @ref ump_dd_release must then be used
  212. * to release each copy of the UMP memory handle.
  213. *
  214. * Calling on an @a UMP_DD_INVALID_MEMORY_HANDLE results in undefined behavior.
  215. * Debug builds will assert on this.
  216. *
  217. * @note You are not required to call @ref ump_dd_retain
  218. * for UMP handles returned from
  219. * @ref ump_dd_from_secure_id,
  220. * because these handles are already reference counted by this function.
  221. *
  222. * @note There is a user space equivalent function called @ref ump_retain
  223. *
  224. * @see ump_retain
  225. *
  226. * @param mem Handle to UMP memory.
  227. * @return 0 indicates success, any other value indicates failure.
  228. */
  229. UMP_KERNEL_API_EXPORT int ump_dd_retain(ump_dd_handle mem);
  230. /**
  231. * Releases a reference from the specified UMP memory.
  232. *
  233. * This function must be called once for every reference to the UMP memory handle.
  234. * When the last reference is released, all resources associated with this UMP memory
  235. * handle are freed.
  236. *
  237. * One can only call ump_release when matched with a successful ump_dd_retain, ump_dd_allocate_64 or ump_dd_from_secure_id
  238. * If called on an @a UMP_DD_INVALID_MEMORY_HANDLE the function will early out.
  239. *
  240. * @note There is a user space equivalent function called @ref ump_release
  241. *
  242. * @see ump_release
  243. *
  244. * @param mem Handle to UMP memory.
  245. */
  246. UMP_KERNEL_API_EXPORT void ump_dd_release(ump_dd_handle mem);
  247. /**
  248. * Create an ump allocation handle based on externally managed memory.
  249. * Used to wrap an existing allocation as an UMP memory handle.
  250. * Once wrapped the memory acts just like a normal allocation coming from @ref ump_dd_allocate_64.
  251. * The only exception is that the freed physical memory is not put into the pool of free memory, but instead considered returned to the caller once @a final_release_func returns.
  252. * The blocks array will be copied, so no need to hold on to it after this function returns.
  253. * @param[in] blocks Array of @ref ump_dd_physical_block_64
  254. * @param num_blocks Number of elements in the array pointed to by @a blocks
  255. * @param flags Allocation flags to mark the handle with
  256. * @param[in] filter_func Pointer to a function which will be called when an allocation is required from a secure id before the allocation itself is returned to user-space.
  257. * NULL permitted if no need for a callback.
  258. * @param[in] final_release_func Pointer to a function which will be called when the last reference is removed, just before the allocation is freed. NULL permitted if no need for a callback.
  259. * @param[in] callback_data An opaque pointer which will be provided to @a filter_func and @a final_release_func
  260. * @return Handle to the UMP allocation handle created, or @a UMP_DD_INVALID_MEMORY_HANDLE if no such handle could be created.
  261. */
  262. UMP_KERNEL_API_EXPORT ump_dd_handle ump_dd_create_from_phys_blocks_64(const ump_dd_physical_block_64 * blocks, uint64_t num_blocks, ump_alloc_flags flags, ump_dd_security_filter filter_func, ump_dd_final_release_callback final_release_func, void* callback_data);
  263. /** @name UMP v1 API
  264. * Functions provided to support compatibility with UMP v1 API
  265. *
  266. *@{
  267. */
  268. /**
  269. * Value to indicate an invalid UMP memory handle.
  270. */
  271. #define UMP_DD_HANDLE_INVALID UMP_DD_INVALID_MEMORY_HANDLE
  272. /**
  273. * UMP error codes for kernel space.
  274. */
  275. typedef enum
  276. {
  277. UMP_DD_SUCCESS, /**< indicates success */
  278. UMP_DD_INVALID /**< indicates failure */
  279. } ump_dd_status_code;
  280. /**
  281. * Struct used to describe a physical block used by UMP memory
  282. */
  283. typedef struct ump_dd_physical_block
  284. {
  285. unsigned long addr; /**< The physical address of the block */
  286. unsigned long size; /**< The length of the block, typically page aligned */
  287. } ump_dd_physical_block;
  288. /**
  289. * Retrieves a handle to allocated UMP memory.
  290. *
  291. * The usage of UMP memory is reference counted, so this will increment the reference
  292. * count by one for the specified UMP memory.
  293. * Use @ref ump_dd_reference_release "ump_dd_reference_release" when there is no longer any
  294. * use for the retrieved handle.
  295. *
  296. * @note There is a user space equivalent function called @ref ump_handle_create_from_secure_id "ump_handle_create_from_secure_id"
  297. *
  298. * @see ump_dd_reference_release
  299. * @see ump_handle_create_from_secure_id
  300. *
  301. * @param secure_id The secure ID of the UMP memory to open, that can be retrieved using the @ref ump_secure_id_get "ump_secure_id_get " function.
  302. *
  303. * @return UMP_INVALID_MEMORY_HANDLE indicates failure, otherwise a valid handle is returned.
  304. */
  305. UMP_KERNEL_API_EXPORT ump_dd_handle ump_dd_handle_create_from_secure_id(ump_secure_id secure_id);
  306. /**
  307. * Create an ump allocation handle based on externally managed memory.
  308. * Used to wrap an existing allocation as an UMP memory handle.
  309. *
  310. * @param[in] blocks Array of @ref ump_dd_physical_block
  311. * @param num_blocks Number of elements in the array pointed to by @a blocks
  312. *
  313. * @return Handle to the UMP allocation handle created, or @a UMP_DD_INVALID_MEMORY_HANDLE if no such handle could be created.
  314. */
  315. UMP_KERNEL_API_EXPORT ump_dd_handle ump_dd_handle_create_from_phys_blocks(ump_dd_physical_block * blocks, unsigned long num_blocks);
  316. /**
  317. * Retrieves the number of physical blocks used by the specified UMP memory.
  318. *
  319. * This function retrieves the number of @ref ump_dd_physical_block "ump_dd_physical_block" structs needed
  320. * to describe the physical memory layout of the given UMP memory. This can later be used when calling
  321. * the functions @ref ump_dd_phys_blocks_get "ump_dd_phys_blocks_get" and
  322. * @ref ump_dd_phys_block_get "ump_dd_phys_block_get".
  323. *
  324. * @see ump_dd_phys_blocks_get
  325. * @see ump_dd_phys_block_get
  326. *
  327. * @param mem Handle to UMP memory.
  328. *
  329. * @return The number of ump_dd_physical_block structs required to describe the physical memory layout of the specified UMP memory.
  330. */
  331. UMP_KERNEL_API_EXPORT unsigned long ump_dd_phys_block_count_get(ump_dd_handle mem);
  332. /**
  333. * Retrieves all physical memory block information for specified UMP memory.
  334. *
  335. * This function can be used by other device drivers in order to create MMU tables.
  336. *
  337. * @note This function will fail if the num_blocks parameter is either to large or to small.
  338. *
  339. * @see ump_dd_phys_block_get
  340. *
  341. * @param mem Handle to UMP memory.
  342. * @param blocks An array of @ref ump_dd_physical_block "ump_dd_physical_block" structs that will receive the physical description.
  343. * @param num_blocks The number of blocks to return in the blocks array. Use the function
  344. * @ref ump_dd_phys_block_count_get "ump_dd_phys_block_count_get" first to determine the number of blocks required.
  345. *
  346. * @return UMP_DD_SUCCESS indicates success, UMP_DD_INVALID indicates failure.
  347. */
  348. UMP_KERNEL_API_EXPORT ump_dd_status_code ump_dd_phys_blocks_get(ump_dd_handle mem, ump_dd_physical_block * const blocks, unsigned long num_blocks);
  349. /**
  350. * Retrieves the physical memory block information for specified block for the specified UMP memory.
  351. *
  352. * This function can be used by other device drivers in order to create MMU tables.
  353. *
  354. * @note This function will return UMP_DD_INVALID if the specified index is out of range.
  355. *
  356. * @see ump_dd_phys_blocks_get
  357. *
  358. * @param mem Handle to UMP memory.
  359. * @param index Which physical info block to retrieve.
  360. * @param block Pointer to a @ref ump_dd_physical_block "ump_dd_physical_block" struct which will receive the requested information.
  361. *
  362. * @return UMP_DD_SUCCESS indicates success, UMP_DD_INVALID indicates failure.
  363. */
  364. UMP_KERNEL_API_EXPORT ump_dd_status_code ump_dd_phys_block_get(ump_dd_handle mem, unsigned long index, ump_dd_physical_block * const block);
  365. /**
  366. * Retrieves the actual size of the specified UMP memory.
  367. *
  368. * The size is reported in bytes, and is typically page aligned.
  369. *
  370. * @note There is a user space equivalent function called @ref ump_size_get "ump_size_get"
  371. *
  372. * @see ump_size_get
  373. *
  374. * @param mem Handle to UMP memory.
  375. *
  376. * @return Returns the allocated size of the specified UMP memory, in bytes.
  377. */
  378. UMP_KERNEL_API_EXPORT unsigned long ump_dd_size_get(ump_dd_handle mem);
  379. /**
  380. * Adds an extra reference to the specified UMP memory.
  381. *
  382. * This function adds an extra reference to the specified UMP memory. This function should
  383. * be used every time a UMP memory handle is duplicated, that is, assigned to another ump_dd_handle
  384. * variable. The function @ref ump_dd_reference_release "ump_dd_reference_release" must then be used
  385. * to release each copy of the UMP memory handle.
  386. *
  387. * @note You are not required to call @ref ump_dd_reference_add "ump_dd_reference_add"
  388. * for UMP handles returned from
  389. * @ref ump_dd_handle_create_from_secure_id "ump_dd_handle_create_from_secure_id",
  390. * because these handles are already reference counted by this function.
  391. *
  392. * @note There is a user space equivalent function called @ref ump_reference_add "ump_reference_add"
  393. *
  394. * @see ump_reference_add
  395. *
  396. * @param mem Handle to UMP memory.
  397. */
  398. UMP_KERNEL_API_EXPORT void ump_dd_reference_add(ump_dd_handle mem);
  399. /**
  400. * Releases a reference from the specified UMP memory.
  401. *
  402. * This function should be called once for every reference to the UMP memory handle.
  403. * When the last reference is released, all resources associated with this UMP memory
  404. * handle are freed.
  405. *
  406. * @note There is a user space equivalent function called @ref ump_reference_release "ump_reference_release"
  407. *
  408. * @see ump_reference_release
  409. *
  410. * @param mem Handle to UMP memory.
  411. */
  412. UMP_KERNEL_API_EXPORT void ump_dd_reference_release(ump_dd_handle mem);
  413. /* @} */
  414. #ifdef __cplusplus
  415. }
  416. #endif
  417. /** @} */ /* end group ump_kernel_space_api */
  418. /** @} */ /* end group ump_api */
  419. #endif /* _UMP_KERNEL_INTERFACE_H_ */