2323 *
2424 * Authors: Alex Deucher
2525 */
26+
27+ /**
28+ * DOC: PRIME Buffer Sharing
29+ *
30+ * The following callback implementations are used for :ref:`sharing GEM buffer
31+ * objects between different devices via PRIME <prime_buffer_sharing>`.
32+ */
33+
2634#include <drm/drmP.h>
2735
2836#include "amdgpu.h"
3240
3341static const struct dma_buf_ops amdgpu_dmabuf_ops ;
3442
43+ /**
44+ * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
45+ * implementation
46+ * @obj: GEM buffer object
47+ *
48+ * Returns:
49+ * A scatter/gather table for the pinned pages of the buffer object's memory.
50+ */
3551struct sg_table * amdgpu_gem_prime_get_sg_table (struct drm_gem_object * obj )
3652{
3753 struct amdgpu_bo * bo = gem_to_amdgpu_bo (obj );
@@ -40,6 +56,15 @@ struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
4056 return drm_prime_pages_to_sg (bo -> tbo .ttm -> pages , npages );
4157}
4258
59+ /**
60+ * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
61+ * @obj: GEM buffer object
62+ *
63+ * Sets up an in-kernel virtual mapping of the buffer object's memory.
64+ *
65+ * Returns:
66+ * The virtual address of the mapping or an error pointer.
67+ */
4368void * amdgpu_gem_prime_vmap (struct drm_gem_object * obj )
4469{
4570 struct amdgpu_bo * bo = gem_to_amdgpu_bo (obj );
@@ -53,13 +78,31 @@ void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
5378 return bo -> dma_buf_vmap .virtual ;
5479}
5580
81+ /**
82+ * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
83+ * @obj: GEM buffer object
84+ * @vaddr: virtual address (unused)
85+ *
86+ * Tears down the in-kernel virtual mapping of the buffer object's memory.
87+ */
5688void amdgpu_gem_prime_vunmap (struct drm_gem_object * obj , void * vaddr )
5789{
5890 struct amdgpu_bo * bo = gem_to_amdgpu_bo (obj );
5991
6092 ttm_bo_kunmap (& bo -> dma_buf_vmap );
6193}
6294
95+ /**
96+ * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
97+ * @obj: GEM buffer object
98+ * @vma: virtual memory area
99+ *
100+ * Sets up a userspace mapping of the buffer object's memory in the given
101+ * virtual memory area.
102+ *
103+ * Returns:
104+ * 0 on success or negative error code.
105+ */
63106int amdgpu_gem_prime_mmap (struct drm_gem_object * obj , struct vm_area_struct * vma )
64107{
65108 struct amdgpu_bo * bo = gem_to_amdgpu_bo (obj );
@@ -94,6 +137,19 @@ int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma
94137 return ret ;
95138}
96139
140+ /**
141+ * amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
142+ * implementation
143+ * @dev: DRM device
144+ * @attach: DMA-buf attachment
145+ * @sg: Scatter/gather table
146+ *
147+ * Import shared DMA buffer memory exported by another device.
148+ *
149+ * Returns:
150+ * A new GEM buffer object of the given DRM device, representing the memory
151+ * described by the given DMA-buf attachment and scatter/gather table.
152+ */
97153struct drm_gem_object *
98154amdgpu_gem_prime_import_sg_table (struct drm_device * dev ,
99155 struct dma_buf_attachment * attach ,
@@ -132,6 +188,19 @@ amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
132188 return ERR_PTR (ret );
133189}
134190
191+ /**
192+ * amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
193+ * @dma_buf: shared DMA buffer
194+ * @target_dev: target device
195+ * @attach: DMA-buf attachment
196+ *
197+ * Makes sure that the shared DMA buffer can be accessed by the target device.
198+ * For now, simply pins it to the GTT domain, where it should be accessible by
199+ * all DMA devices.
200+ *
201+ * Returns:
202+ * 0 on success or negative error code.
203+ */
135204static int amdgpu_gem_map_attach (struct dma_buf * dma_buf ,
136205 struct device * target_dev ,
137206 struct dma_buf_attachment * attach )
@@ -181,6 +250,14 @@ static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
181250 return r ;
182251}
183252
253+ /**
254+ * amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
255+ * @dma_buf: shared DMA buffer
256+ * @attach: DMA-buf attachment
257+ *
258+ * This is called when a shared DMA buffer no longer needs to be accessible by
259+ * the other device. For now, simply unpins the buffer from GTT.
260+ */
184261static void amdgpu_gem_map_detach (struct dma_buf * dma_buf ,
185262 struct dma_buf_attachment * attach )
186263{
@@ -202,13 +279,32 @@ static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
202279 drm_gem_map_detach (dma_buf , attach );
203280}
204281
282+ /**
283+ * amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
284+ * @obj: GEM buffer object
285+ *
286+ * Returns:
287+ * The buffer object's reservation object.
288+ */
205289struct reservation_object * amdgpu_gem_prime_res_obj (struct drm_gem_object * obj )
206290{
207291 struct amdgpu_bo * bo = gem_to_amdgpu_bo (obj );
208292
209293 return bo -> tbo .resv ;
210294}
211295
296+ /**
297+ * amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
298+ * @dma_buf: shared DMA buffer
299+ * @direction: direction of DMA transfer
300+ *
301+ * This is called before CPU access to the shared DMA buffer's memory. If it's
302+ * a read access, the buffer is moved to the GTT domain if possible, for optimal
303+ * CPU read performance.
304+ *
305+ * Returns:
306+ * 0 on success or negative error code.
307+ */
212308static int amdgpu_gem_begin_cpu_access (struct dma_buf * dma_buf ,
213309 enum dma_data_direction direction )
214310{
@@ -253,6 +349,18 @@ static const struct dma_buf_ops amdgpu_dmabuf_ops = {
253349 .vunmap = drm_gem_dmabuf_vunmap ,
254350};
255351
352+ /**
353+ * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
354+ * @dev: DRM device
355+ * @gobj: GEM buffer object
356+ * @flags: flags like DRM_CLOEXEC and DRM_RDWR
357+ *
358+ * The main work is done by the &drm_gem_prime_export helper, which in turn
359+ * uses &amdgpu_gem_prime_res_obj.
360+ *
361+ * Returns:
362+ * Shared DMA buffer representing the GEM buffer object from the given device.
363+ */
256364struct dma_buf * amdgpu_gem_prime_export (struct drm_device * dev ,
257365 struct drm_gem_object * gobj ,
258366 int flags )
@@ -273,6 +381,17 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
273381 return buf ;
274382}
275383
384+ /**
385+ * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
386+ * @dev: DRM device
387+ * @dma_buf: Shared DMA buffer
388+ *
389+ * The main work is done by the &drm_gem_prime_import helper, which in turn
390+ * uses &amdgpu_gem_prime_import_sg_table.
391+ *
392+ * Returns:
393+ * GEM buffer object representing the shared DMA buffer for the given device.
394+ */
276395struct drm_gem_object * amdgpu_gem_prime_import (struct drm_device * dev ,
277396 struct dma_buf * dma_buf )
278397{
0 commit comments