drm_pagemap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* SPDX-License-Identifier: MIT */
  2. #ifndef _DRM_PAGEMAP_H_
  3. #define _DRM_PAGEMAP_H_
  4. #include <linux/dma-direction.h>
  5. #include <linux/hmm.h>
  6. #include <linux/types.h>
  7. #define NR_PAGES(order) (1U << (order))
  8. struct dma_fence;
  9. struct drm_pagemap;
  10. struct drm_pagemap_cache;
  11. struct drm_pagemap_dev_hold;
  12. struct drm_pagemap_zdd;
  13. struct device;
  14. /**
  15. * enum drm_interconnect_protocol - Used to identify an interconnect protocol.
  16. *
  17. * @DRM_INTERCONNECT_SYSTEM: DMA map is system pages
  18. * @DRM_INTERCONNECT_DRIVER: DMA map is driver defined
  19. */
  20. enum drm_interconnect_protocol {
  21. DRM_INTERCONNECT_SYSTEM,
  22. DRM_INTERCONNECT_DRIVER,
  23. /* A driver can add private values beyond DRM_INTERCONNECT_DRIVER */
  24. };
  25. /**
  26. * struct drm_pagemap_addr - Address representation.
  27. * @addr: The dma address or driver-defined address for driver private interconnects.
  28. * @proto: The interconnect protocol.
  29. * @order: The page order of the device mapping. (Size is PAGE_SIZE << order).
  30. * @dir: The DMA direction.
  31. *
  32. * Note: There is room for improvement here. We should be able to pack into
  33. * 64 bits.
  34. */
  35. struct drm_pagemap_addr {
  36. dma_addr_t addr;
  37. u64 proto : 54;
  38. u64 order : 8;
  39. u64 dir : 2;
  40. };
  41. /**
  42. * drm_pagemap_addr_encode() - Encode a dma address with metadata
  43. * @addr: The dma address or driver-defined address for driver private interconnects.
  44. * @proto: The interconnect protocol.
  45. * @order: The page order of the dma mapping. (Size is PAGE_SIZE << order).
  46. * @dir: The DMA direction.
  47. *
  48. * Return: A struct drm_pagemap_addr encoding the above information.
  49. */
  50. static inline struct drm_pagemap_addr
  51. drm_pagemap_addr_encode(dma_addr_t addr,
  52. enum drm_interconnect_protocol proto,
  53. unsigned int order,
  54. enum dma_data_direction dir)
  55. {
  56. return (struct drm_pagemap_addr) {
  57. .addr = addr,
  58. .proto = proto,
  59. .order = order,
  60. .dir = dir,
  61. };
  62. }
  63. /**
  64. * struct drm_pagemap_ops: Ops for a drm-pagemap.
  65. */
  66. struct drm_pagemap_ops {
  67. /**
  68. * @device_map: Map for device access or provide a virtual address suitable for
  69. *
  70. * @dpagemap: The struct drm_pagemap for the page.
  71. * @dev: The device mapper.
  72. * @page: The page to map.
  73. * @order: The page order of the device mapping. (Size is PAGE_SIZE << order).
  74. * @dir: The transfer direction.
  75. */
  76. struct drm_pagemap_addr (*device_map)(struct drm_pagemap *dpagemap,
  77. struct device *dev,
  78. struct page *page,
  79. unsigned int order,
  80. enum dma_data_direction dir);
  81. /**
  82. * @device_unmap: Unmap a device address previously obtained using @device_map.
  83. *
  84. * @dpagemap: The struct drm_pagemap for the mapping.
  85. * @dev: The device unmapper.
  86. * @addr: The device address obtained when mapping.
  87. */
  88. void (*device_unmap)(struct drm_pagemap *dpagemap,
  89. struct device *dev,
  90. const struct drm_pagemap_addr *addr);
  91. /**
  92. * @populate_mm: Populate part of the mm with @dpagemap memory,
  93. * migrating existing data.
  94. * @dpagemap: The struct drm_pagemap managing the memory.
  95. * @start: The virtual start address in @mm
  96. * @end: The virtual end address in @mm
  97. * @mm: Pointer to a live mm. The caller must have an mmget()
  98. * reference.
  99. *
  100. * The caller will have the mm lock at least in read mode.
  101. * Note that there is no guarantee that the memory is resident
  102. * after the function returns, it's best effort only.
  103. * When the mm is not using the memory anymore,
  104. * it will be released. The struct drm_pagemap might have a
  105. * mechanism in place to reclaim the memory and the data will
  106. * then be migrated. Typically to system memory.
  107. * The implementation should hold sufficient runtime power-
  108. * references while pages are used in an address space and
  109. * should ideally guard against hardware device unbind in
  110. * a way such that device pages are migrated back to system
  111. * followed by device page removal. The implementation should
  112. * return -ENODEV after device removal.
  113. *
  114. * Return: 0 if successful. Negative error code on error.
  115. */
  116. int (*populate_mm)(struct drm_pagemap *dpagemap,
  117. unsigned long start, unsigned long end,
  118. struct mm_struct *mm,
  119. unsigned long timeslice_ms);
  120. /**
  121. * @destroy: Destroy the drm_pagemap and associated resources.
  122. * @dpagemap: The drm_pagemap to destroy.
  123. * @is_atomic_or_reclaim: The function may be called from
  124. * atomic- or reclaim context.
  125. *
  126. * The implementation should take care not to attempt to
  127. * destroy resources that may already have been destroyed
  128. * using devm_ callbacks, since this function may be called
  129. * after the underlying struct device has been unbound.
  130. * If the implementation defers the execution to a work item
  131. * to avoid locking issues, then it must make sure the work
  132. * items are flushed before module exit. If the destroy call
  133. * happens after the provider's pci_remove() callback has
  134. * been executed, a module reference and drm device reference is
  135. * held across the destroy callback.
  136. */
  137. void (*destroy)(struct drm_pagemap *dpagemap,
  138. bool is_atomic_or_reclaim);
  139. };
  140. /**
  141. * struct drm_pagemap: Additional information for a struct dev_pagemap
  142. * used for device p2p handshaking.
  143. * @ops: The struct drm_pagemap_ops.
  144. * @ref: Reference count.
  145. * @drm: The struct drm device owning the device-private memory.
  146. * @pagemap: Pointer to the underlying dev_pagemap.
  147. * @dev_hold: Pointer to a struct drm_pagemap_dev_hold for
  148. * device referencing.
  149. * @cache: Back-pointer to the &struct drm_pagemap_cache used for this
  150. * &struct drm_pagemap. May be NULL if no cache is used.
  151. * @shrink_link: Link into the shrinker's list of drm_pagemaps. Only
  152. * used if also using a pagemap cache.
  153. */
  154. struct drm_pagemap {
  155. const struct drm_pagemap_ops *ops;
  156. struct kref ref;
  157. struct drm_device *drm;
  158. struct dev_pagemap *pagemap;
  159. struct drm_pagemap_dev_hold *dev_hold;
  160. struct drm_pagemap_cache *cache;
  161. struct list_head shrink_link;
  162. };
  163. struct drm_pagemap_devmem;
  164. /**
  165. * struct drm_pagemap_devmem_ops - Operations structure for GPU SVM device memory
  166. *
  167. * This structure defines the operations for GPU Shared Virtual Memory (SVM)
  168. * device memory. These operations are provided by the GPU driver to manage device memory
  169. * allocations and perform operations such as migration between device memory and system
  170. * RAM.
  171. */
  172. struct drm_pagemap_devmem_ops {
  173. /**
  174. * @devmem_release: Release device memory allocation (optional)
  175. * @devmem_allocation: device memory allocation
  176. *
  177. * Release device memory allocation and drop a reference to device
  178. * memory allocation.
  179. */
  180. void (*devmem_release)(struct drm_pagemap_devmem *devmem_allocation);
  181. /**
  182. * @populate_devmem_pfn: Populate device memory PFN (required for migration)
  183. * @devmem_allocation: device memory allocation
  184. * @npages: Number of pages to populate
  185. * @pfn: Array of page frame numbers to populate
  186. *
  187. * Populate device memory page frame numbers (PFN).
  188. *
  189. * Return: 0 on success, a negative error code on failure.
  190. */
  191. int (*populate_devmem_pfn)(struct drm_pagemap_devmem *devmem_allocation,
  192. unsigned long npages, unsigned long *pfn);
  193. /**
  194. * @copy_to_devmem: Copy to device memory (required for migration)
  195. * @pages: Pointer to array of device memory pages (destination)
  196. * @pagemap_addr: Pointer to array of DMA information (source)
  197. * @npages: Number of pages to copy
  198. * @pre_migrate_fence: dma-fence to wait for before migration start.
  199. * May be NULL.
  200. *
  201. * Copy pages to device memory. If the order of a @pagemap_addr entry
  202. * is greater than 0, the entry is populated but subsequent entries
  203. * within the range of that order are not populated.
  204. *
  205. * Return: 0 on success, a negative error code on failure.
  206. */
  207. int (*copy_to_devmem)(struct page **pages,
  208. struct drm_pagemap_addr *pagemap_addr,
  209. unsigned long npages,
  210. struct dma_fence *pre_migrate_fence);
  211. /**
  212. * @copy_to_ram: Copy to system RAM (required for migration)
  213. * @pages: Pointer to array of device memory pages (source)
  214. * @pagemap_addr: Pointer to array of DMA information (destination)
  215. * @npages: Number of pages to copy
  216. * @pre_migrate_fence: dma-fence to wait for before migration start.
  217. * May be NULL.
  218. *
  219. * Copy pages to system RAM. If the order of a @pagemap_addr entry
  220. * is greater than 0, the entry is populated but subsequent entries
  221. * within the range of that order are not populated.
  222. *
  223. * Return: 0 on success, a negative error code on failure.
  224. */
  225. int (*copy_to_ram)(struct page **pages,
  226. struct drm_pagemap_addr *pagemap_addr,
  227. unsigned long npages,
  228. struct dma_fence *pre_migrate_fence);
  229. };
  230. #if IS_ENABLED(CONFIG_ZONE_DEVICE)
  231. int drm_pagemap_init(struct drm_pagemap *dpagemap,
  232. struct dev_pagemap *pagemap,
  233. struct drm_device *drm,
  234. const struct drm_pagemap_ops *ops);
  235. struct drm_pagemap *drm_pagemap_create(struct drm_device *drm,
  236. struct dev_pagemap *pagemap,
  237. const struct drm_pagemap_ops *ops);
  238. struct drm_pagemap *drm_pagemap_page_to_dpagemap(struct page *page);
  239. void drm_pagemap_put(struct drm_pagemap *dpagemap);
  240. #else
  241. static inline struct drm_pagemap *drm_pagemap_page_to_dpagemap(struct page *page)
  242. {
  243. return NULL;
  244. }
  245. static inline void drm_pagemap_put(struct drm_pagemap *dpagemap)
  246. {
  247. }
  248. #endif /* IS_ENABLED(CONFIG_ZONE_DEVICE) */
  249. /**
  250. * drm_pagemap_get() - Obtain a reference on a struct drm_pagemap
  251. * @dpagemap: Pointer to the struct drm_pagemap, or NULL.
  252. *
  253. * Return: Pointer to the struct drm_pagemap, or NULL.
  254. */
  255. static inline struct drm_pagemap *
  256. drm_pagemap_get(struct drm_pagemap *dpagemap)
  257. {
  258. if (likely(dpagemap))
  259. kref_get(&dpagemap->ref);
  260. return dpagemap;
  261. }
  262. /**
  263. * drm_pagemap_get_unless_zero() - Obtain a reference on a struct drm_pagemap
  264. * unless the current reference count is zero.
  265. * @dpagemap: Pointer to the drm_pagemap or NULL.
  266. *
  267. * Return: A pointer to @dpagemap if the reference count was successfully
  268. * incremented. NULL if @dpagemap was NULL, or its refcount was 0.
  269. */
  270. static inline struct drm_pagemap * __must_check
  271. drm_pagemap_get_unless_zero(struct drm_pagemap *dpagemap)
  272. {
  273. return (dpagemap && kref_get_unless_zero(&dpagemap->ref)) ? dpagemap : NULL;
  274. }
  275. /**
  276. * struct drm_pagemap_devmem - Structure representing a GPU SVM device memory allocation
  277. *
  278. * @dev: Pointer to the device structure which device memory allocation belongs to
  279. * @mm: Pointer to the mm_struct for the address space
  280. * @detached: device memory allocations is detached from device pages
  281. * @ops: Pointer to the operations structure for GPU SVM device memory
  282. * @dpagemap: The struct drm_pagemap of the pages this allocation belongs to.
  283. * @size: Size of device memory allocation
  284. * @timeslice_expiration: Timeslice expiration in jiffies
  285. * @pre_migrate_fence: Fence to wait for or pipeline behind before migration starts.
  286. * (May be NULL).
  287. */
  288. struct drm_pagemap_devmem {
  289. struct device *dev;
  290. struct mm_struct *mm;
  291. struct completion detached;
  292. const struct drm_pagemap_devmem_ops *ops;
  293. struct drm_pagemap *dpagemap;
  294. size_t size;
  295. u64 timeslice_expiration;
  296. struct dma_fence *pre_migrate_fence;
  297. };
  298. /**
  299. * struct drm_pagemap_migrate_details - Details to govern migration.
  300. * @timeslice_ms: The time requested for the migrated pagemap pages to
  301. * be present in @mm before being allowed to be migrated back.
  302. * @can_migrate_same_pagemap: Whether the copy function as indicated by
  303. * the @source_peer_migrates flag, can migrate device pages within a
  304. * single drm_pagemap.
  305. * @source_peer_migrates: Whether on p2p migration, The source drm_pagemap
  306. * should use the copy_to_ram() callback rather than the destination
  307. * drm_pagemap should use the copy_to_devmem() callback.
  308. */
  309. struct drm_pagemap_migrate_details {
  310. unsigned long timeslice_ms;
  311. u32 can_migrate_same_pagemap : 1;
  312. u32 source_peer_migrates : 1;
  313. };
  314. #if IS_ENABLED(CONFIG_ZONE_DEVICE)
  315. int drm_pagemap_migrate_to_devmem(struct drm_pagemap_devmem *devmem_allocation,
  316. struct mm_struct *mm,
  317. unsigned long start, unsigned long end,
  318. const struct drm_pagemap_migrate_details *mdetails);
  319. int drm_pagemap_evict_to_ram(struct drm_pagemap_devmem *devmem_allocation);
  320. const struct dev_pagemap_ops *drm_pagemap_pagemap_ops_get(void);
  321. void drm_pagemap_devmem_init(struct drm_pagemap_devmem *devmem_allocation,
  322. struct device *dev, struct mm_struct *mm,
  323. const struct drm_pagemap_devmem_ops *ops,
  324. struct drm_pagemap *dpagemap, size_t size,
  325. struct dma_fence *pre_migrate_fence);
  326. int drm_pagemap_populate_mm(struct drm_pagemap *dpagemap,
  327. unsigned long start, unsigned long end,
  328. struct mm_struct *mm,
  329. unsigned long timeslice_ms);
  330. void drm_pagemap_destroy(struct drm_pagemap *dpagemap, bool is_atomic_or_reclaim);
  331. int drm_pagemap_reinit(struct drm_pagemap *dpagemap);
  332. #endif /* IS_ENABLED(CONFIG_ZONE_DEVICE) */
  333. #endif