xref: /linux/include/drm/drm_pagemap.h (revision f86ad0ed620cb3c91ec7d5468e93ac68d727539d)
1 /* SPDX-License-Identifier: MIT */
2 #ifndef _DRM_PAGEMAP_H_
3 #define _DRM_PAGEMAP_H_
4 
5 #include <linux/dma-direction.h>
6 #include <linux/hmm.h>
7 #include <linux/types.h>
8 
9 struct drm_pagemap;
10 struct drm_pagemap_zdd;
11 struct device;
12 
13 /**
14  * enum drm_interconnect_protocol - Used to identify an interconnect protocol.
15  *
16  * @DRM_INTERCONNECT_SYSTEM: DMA map is system pages
17  * @DRM_INTERCONNECT_DRIVER: DMA map is driver defined
18  */
19 enum drm_interconnect_protocol {
20 	DRM_INTERCONNECT_SYSTEM,
21 	DRM_INTERCONNECT_DRIVER,
22 	/* A driver can add private values beyond DRM_INTERCONNECT_DRIVER */
23 };
24 
25 /**
26  * struct drm_pagemap_device_addr - Device 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_device_addr {
36 	dma_addr_t addr;
37 	u64 proto : 54;
38 	u64 order : 8;
39 	u64 dir : 2;
40 };
41 
42 /**
43  * drm_pagemap_device_addr_encode() - Encode a dma address with metadata
44  * @addr: The dma address or driver-defined address for driver private interconnects.
45  * @proto: The interconnect protocol.
46  * @order: The page order of the dma mapping. (Size is PAGE_SIZE << order).
47  * @dir: The DMA direction.
48  *
49  * Return: A struct drm_pagemap_device_addr encoding the above information.
50  */
51 static inline struct drm_pagemap_device_addr
52 drm_pagemap_device_addr_encode(dma_addr_t addr,
53 			       enum drm_interconnect_protocol proto,
54 			       unsigned int order,
55 			       enum dma_data_direction dir)
56 {
57 	return (struct drm_pagemap_device_addr) {
58 		.addr = addr,
59 		.proto = proto,
60 		.order = order,
61 		.dir = dir,
62 	};
63 }
64 
65 /**
66  * struct drm_pagemap_ops: Ops for a drm-pagemap.
67  */
68 struct drm_pagemap_ops {
69 	/**
70 	 * @device_map: Map for device access or provide a virtual address suitable for
71 	 *
72 	 * @dpagemap: The struct drm_pagemap for the page.
73 	 * @dev: The device mapper.
74 	 * @page: The page to map.
75 	 * @order: The page order of the device mapping. (Size is PAGE_SIZE << order).
76 	 * @dir: The transfer direction.
77 	 */
78 	struct drm_pagemap_device_addr (*device_map)(struct drm_pagemap *dpagemap,
79 						     struct device *dev,
80 						     struct page *page,
81 						     unsigned int order,
82 						     enum dma_data_direction dir);
83 
84 	/**
85 	 * @device_unmap: Unmap a device address previously obtained using @device_map.
86 	 *
87 	 * @dpagemap: The struct drm_pagemap for the mapping.
88 	 * @dev: The device unmapper.
89 	 * @addr: The device address obtained when mapping.
90 	 */
91 	void (*device_unmap)(struct drm_pagemap *dpagemap,
92 			     struct device *dev,
93 			     struct drm_pagemap_device_addr addr);
94 
95 };
96 
97 /**
98  * struct drm_pagemap: Additional information for a struct dev_pagemap
99  * used for device p2p handshaking.
100  * @ops: The struct drm_pagemap_ops.
101  * @dev: The struct drevice owning the device-private memory.
102  */
103 struct drm_pagemap {
104 	const struct drm_pagemap_ops *ops;
105 	struct device *dev;
106 };
107 
108 struct drm_pagemap_devmem;
109 
110 /**
111  * struct drm_pagemap_devmem_ops - Operations structure for GPU SVM device memory
112  *
113  * This structure defines the operations for GPU Shared Virtual Memory (SVM)
114  * device memory. These operations are provided by the GPU driver to manage device memory
115  * allocations and perform operations such as migration between device memory and system
116  * RAM.
117  */
118 struct drm_pagemap_devmem_ops {
119 	/**
120 	 * @devmem_release: Release device memory allocation (optional)
121 	 * @devmem_allocation: device memory allocation
122 	 *
123 	 * Release device memory allocation and drop a reference to device
124 	 * memory allocation.
125 	 */
126 	void (*devmem_release)(struct drm_pagemap_devmem *devmem_allocation);
127 
128 	/**
129 	 * @populate_devmem_pfn: Populate device memory PFN (required for migration)
130 	 * @devmem_allocation: device memory allocation
131 	 * @npages: Number of pages to populate
132 	 * @pfn: Array of page frame numbers to populate
133 	 *
134 	 * Populate device memory page frame numbers (PFN).
135 	 *
136 	 * Return: 0 on success, a negative error code on failure.
137 	 */
138 	int (*populate_devmem_pfn)(struct drm_pagemap_devmem *devmem_allocation,
139 				   unsigned long npages, unsigned long *pfn);
140 
141 	/**
142 	 * @copy_to_devmem: Copy to device memory (required for migration)
143 	 * @pages: Pointer to array of device memory pages (destination)
144 	 * @dma_addr: Pointer to array of DMA addresses (source)
145 	 * @npages: Number of pages to copy
146 	 *
147 	 * Copy pages to device memory.
148 	 *
149 	 * Return: 0 on success, a negative error code on failure.
150 	 */
151 	int (*copy_to_devmem)(struct page **pages,
152 			      dma_addr_t *dma_addr,
153 			      unsigned long npages);
154 
155 	/**
156 	 * @copy_to_ram: Copy to system RAM (required for migration)
157 	 * @pages: Pointer to array of device memory pages (source)
158 	 * @dma_addr: Pointer to array of DMA addresses (destination)
159 	 * @npages: Number of pages to copy
160 	 *
161 	 * Copy pages to system RAM.
162 	 *
163 	 * Return: 0 on success, a negative error code on failure.
164 	 */
165 	int (*copy_to_ram)(struct page **pages,
166 			   dma_addr_t *dma_addr,
167 			   unsigned long npages);
168 };
169 
170 /**
171  * struct drm_pagemap_devmem - Structure representing a GPU SVM device memory allocation
172  *
173  * @dev: Pointer to the device structure which device memory allocation belongs to
174  * @mm: Pointer to the mm_struct for the address space
175  * @detached: device memory allocations is detached from device pages
176  * @ops: Pointer to the operations structure for GPU SVM device memory
177  * @dpagemap: The struct drm_pagemap of the pages this allocation belongs to.
178  * @size: Size of device memory allocation
179  * @timeslice_expiration: Timeslice expiration in jiffies
180  */
181 struct drm_pagemap_devmem {
182 	struct device *dev;
183 	struct mm_struct *mm;
184 	struct completion detached;
185 	const struct drm_pagemap_devmem_ops *ops;
186 	struct drm_pagemap *dpagemap;
187 	size_t size;
188 	u64 timeslice_expiration;
189 };
190 
191 int drm_pagemap_migrate_to_devmem(struct drm_pagemap_devmem *devmem_allocation,
192 				  struct mm_struct *mm,
193 				  unsigned long start, unsigned long end,
194 				  unsigned long timeslice_ms,
195 				  void *pgmap_owner);
196 
197 int drm_pagemap_evict_to_ram(struct drm_pagemap_devmem *devmem_allocation);
198 
199 const struct dev_pagemap_ops *drm_pagemap_pagemap_ops_get(void);
200 
201 struct drm_pagemap *drm_pagemap_page_to_dpagemap(struct page *page);
202 
203 void drm_pagemap_devmem_init(struct drm_pagemap_devmem *devmem_allocation,
204 			     struct device *dev, struct mm_struct *mm,
205 			     const struct drm_pagemap_devmem_ops *ops,
206 			     struct drm_pagemap *dpagemap, size_t size);
207 
208 #endif
209