ttm: add prime sharing support to TTM
Subsystems (6)
Dave Airlie
maintainerPATCH 1/2 ttm: add prime sharing support to TTM
From: Dave Airlie <[email protected]>
This adds the ability for ttm common code to take an SG table and use it as the backing for a slave TTM object.
The drivers can then populate their GTT tables using the SG object.
--- drivers/gpu/drm/nouveau/nouveau_bo.c | 2 +- drivers/gpu/drm/radeon/radeon_object.c | 2 +- drivers/gpu/drm/ttm/ttm_bo.c | 14 +++++++++++++- drivers/gpu/drm/vmwgfx/vmwgfx_resource.c | 2 +- include/drm/ttm/ttm_bo_api.h | 9 ++++++++- include/drm/ttm/ttm_bo_driver.h | 2 ++ 6 files changed, 26 insertions(+), 5 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +1 -1
@@ -121,7 +121,7 @@ nouveau_bo_new(struct drm_device *dev, int size, int align, ret =ttm_bo_init (&dev_priv->ttm.bdev, &nvbo->bo, size, ttm_bo_type_device, &nvbo->placement, - align >>PAGE_SHIFT ,0 , false,NULL , acc_size, + align >>PAGE_SHIFT ,0 , false,NULL , acc_size,NULL , nouveau_bo_del_ttm);if (ret) {/* ttm will call nouveau_bo_del_ttm if it fails.. */
drivers/gpu/drm/radeon/radeon_object.c +1 -1
@@ -155,7 +155,7 @@ retry:mutex_lock (&rdev->vram_mutex); r =ttm_bo_init (&rdev->mman.bdev, &bo->tbo, size, type, &bo->placement, page_align,0 , !kernel,NULL , - acc_size, &radeon_ttm_bo_destroy); + acc_size,NULL , &radeon_ttm_bo_destroy);mutex_unlock (&rdev->vram_mutex);if (unlikely (r !=0 )) {if (r != -ERESTARTSYS ) {
drivers/gpu/drm/ttm/ttm_bo.c +13 -1
@@ -343,6 +343,16 @@ static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)if (unlikely (bo->ttm ==NULL )) ret = -ENOMEM ;break ; +case ttm_bo_type_sg: + bo->ttm = bdev->driver->ttm_tt_create (bdev, bo->num_pages <<PAGE_SHIFT , + page_flags |TTM_PAGE_FLAG_SG , + glob->dummy_read_page); +if (unlikely (bo->ttm ==NULL )) { + ret = -ENOMEM ; +break ; + } + bo->ttm->sg = bo->sg; +break ;default :pr_err ("Illegal buffer object type\n" ); ret = -EINVAL ;
@@ -1169,6 +1179,7 @@ int ttm_bo_init(struct ttm_bo_device *bdev,bool interruptible, struct file *persistent_swap_storage, size_t acc_size, + struct sg_table *sg, void (*destroy) (struct ttm_buffer_object *)) { int ret = 0;
@@ -1223,6 +1234,7 @@ int ttm_bo_init(struct ttm_bo_device *bdev, bo->seq_valid = false; bo->persistent_swap_storage = persistent_swap_storage; bo->acc_size = acc_size; + bo->sg = sg;atomic_inc (&bo->glob->bo_count); ret =ttm_bo_check_placement (bo, placement);
@@ -1312,7 +1324,7 @@ int ttm_bo_create(struct ttm_bo_device *bdev, ret =ttm_bo_init (bdev, bo, size, type, placement, page_alignment, buffer_start, interruptible, - persistent_swap_storage, acc_size,NULL ); + persistent_swap_storage, acc_size,NULL ,NULL );if (likely (ret ==0 )) *p_bo = bo;
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +1 -1
@@ -1567,7 +1567,7 @@ int vmw_dmabuf_init(struct vmw_private *dev_priv, ret =ttm_bo_init (bdev, &vmw_bo->base, size, ttm_bo_type_device, placement,0 ,0 , interruptible, -NULL , acc_size, bo_free); +NULL , acc_size,NULL , bo_free);return ret; }
include/drm/ttm/ttm_bo_api.h +8 -1
@@ -124,11 +124,15 @@ struct ttm_mem_reg {* * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers, * but they cannot be accessed from user-space. For kernel-only use. +* +* @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another +* driver. */ enum ttm_bo_type { ttm_bo_type_device, - ttm_bo_type_kernel + ttm_bo_type_kernel, + ttm_bo_type_sg };struct ttm_tt ;
@@ -271,6 +275,8 @@ struct ttm_buffer_object {unsigned long offset;uint32_t cur_placement; + +struct sg_table *sg; };/**
@@ -503,6 +509,7 @@ extern int ttm_bo_init(struct ttm_bo_device *bdev,bool interrubtible, struct file *persistent_swap_storage, size_t acc_size, + struct sg_table *sg, void (*destroy) (struct ttm_buffer_object *));/**
include/drm/ttm/ttm_bo_driver.h +2 -0
@@ -81,6 +81,7 @@ struct ttm_backend_func {#define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)#define TTM_PAGE_FLAG_ZERO_ALLOC (1 << 6)#define TTM_PAGE_FLAG_DMA32 (1 << 7) +#define TTM_PAGE_FLAG_SG (1 << 8)enum ttm_caching_state { tt_uncached,
@@ -116,6 +117,7 @@ struct ttm_tt {struct page **pages;uint32_t page_flags;unsigned long num_pages; +struct sg_table *sg;/* for SG objects via dma-buf */ struct ttm_bo_global *glob;struct ttm_backend *be;struct file *swap_storage;
Signature · 1 line
1.7.6
Dave Airlie
maintainerPATCH 2/2 nouveau: add PRIME support
From: Dave Airlie <[email protected]>
This adds prime->fd and fd->prime support to nouveau, it passes the SG object to TTM, and then populates the GART entries using it.
--- drivers/gpu/drm/nouveau/Makefile | 2 +- drivers/gpu/drm/nouveau/nouveau_bo.c | 39 +++++++-- drivers/gpu/drm/nouveau/nouveau_channel.c | 2 +- drivers/gpu/drm/nouveau/nouveau_drv.c | 8 ++- drivers/gpu/drm/nouveau/nouveau_drv.h | 10 ++- drivers/gpu/drm/nouveau/nouveau_fence.c | 2 +- drivers/gpu/drm/nouveau/nouveau_gem.c | 6 +- drivers/gpu/drm/nouveau/nouveau_mem.c | 2 +- drivers/gpu/drm/nouveau/nouveau_prime.c | 132 +++++++++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_sgdma.c | 5 +- drivers/gpu/drm/nouveau/nouveau_vm.c | 57 ++++++++++++ drivers/gpu/drm/nouveau/nouveau_vm.h | 6 +- drivers/gpu/drm/nouveau/nv04_crtc.c | 2 +- drivers/gpu/drm/nouveau/nv50_crtc.c | 4 +- drivers/gpu/drm/nouveau/nv50_evo.c | 4 +- drivers/gpu/drm/nouveau/nvd0_display.c | 6 +- 16 files changed, 262 insertions(+), 25 deletions(-) create mode 100644 drivers/gpu/drm/nouveau/nouveau_prime.c
drivers/gpu/drm/nouveau/Makefile +1 -1
@@ -37,7 +37,7 @@ nouveau-y := nouveau_drv.o nouveau_state.o nouveau_channel.o nouveau_mem.o \ nv50_calc.o \ nv04_pm.o nv40_pm.o nv50_pm.o nva3_pm.o nvc0_pm.o \ nv50_vram.o nvc0_vram.o \ - nv50_vm.o nvc0_vm.o + nv50_vm.o nvc0_vm.o nouveau_prime.o nouveau-$(CONFIG_DRM_NOUVEAU_DEBUG) += nouveau_debugfs.o nouveau-$(CONFIG_COMPAT) += nouveau_ioc32.o
drivers/gpu/drm/nouveau/nouveau_bo.c +31 -8
@@ -89,12 +89,17 @@ nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,int nouveau_bo_new (struct drm_device *dev,int size,int align,uint32_t flags,uint32_t tile_mode,uint32_t tile_flags, +struct sg_table *sg,struct nouveau_bo **pnvbo) {struct drm_nouveau_private *dev_priv = dev->dev_private;struct nouveau_bo *nvbo;size_t acc_size;int ret; +int type = ttm_bo_type_device; + +if (sg) + type = ttm_bo_type_sg; nvbo =kzalloc (sizeof (struct nouveau_bo ),GFP_KERNEL );if (!nvbo)
@@ -120,8 +125,8 @@ nouveau_bo_new(struct drm_device *dev, int size, int align, sizeof(struct nouveau_bo)); ret =ttm_bo_init (&dev_priv->ttm.bdev, &nvbo->bo, size, - ttm_bo_type_device, &nvbo->placement, - align >>PAGE_SHIFT ,0 , false,NULL , acc_size,NULL , + type, &nvbo->placement, + align >>PAGE_SHIFT ,0 , false,NULL , acc_size, sg, nouveau_bo_del_ttm);if (ret) {/* ttm will call nouveau_bo_del_ttm if it fails.. */
@@ -817,9 +822,14 @@ nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem) }else if (new_mem && new_mem->mem_type ==TTM_PL_TT && nvbo->page_shift == vma->vm->spg_shift) { -nouveau_vm_map_sg (vma,0 , new_mem-> - num_pages <<PAGE_SHIFT , - new_mem->mm_node); +if (((struct nouveau_mem *)new_mem->mm_node)->sg) +nouveau_vm_map_sg_table (vma,0 , new_mem-> + num_pages <<PAGE_SHIFT , + new_mem->mm_node); +else +nouveau_vm_map_sg (vma,0 , new_mem-> + num_pages <<PAGE_SHIFT , + new_mem->mm_node); }else {nouveau_vm_unmap (vma); }
@@ -1058,10 +1068,16 @@ nouveau_ttm_tt_populate(struct ttm_tt *ttm)struct drm_device *dev;unsigned i;int r; +bool slave = !!(ttm->page_flags &TTM_PAGE_FLAG_SG );if (ttm->state != tt_unpopulated)return 0 ; +if (slave && ttm->sg) { + ttm->state = tt_unbound; +return 0 ; + } + dev_priv =nouveau_bdev (ttm->bdev); dev = dev_priv->dev;
@@ -1106,6 +1122,10 @@ nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)struct drm_nouveau_private *dev_priv;struct drm_device *dev;unsigned i; +bool slave = !!(ttm->page_flags &TTM_PAGE_FLAG_SG ); + +if (slave) +return ; dev_priv =nouveau_bdev (ttm->bdev); dev = dev_priv->dev;
@@ -1181,9 +1201,12 @@ nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,if (nvbo->bo.mem.mem_type ==TTM_PL_VRAM )nouveau_vm_map (vma, nvbo->bo.mem.mm_node); -else -if (nvbo->bo.mem.mem_type ==TTM_PL_TT ) -nouveau_vm_map_sg (vma,0 , size, node); +else if (nvbo->bo.mem.mem_type ==TTM_PL_TT ) { +if (node->sg) +nouveau_vm_map_sg_table (vma,0 , size, node); +else +nouveau_vm_map_sg (vma,0 , size, node); + }list_add_tail (&vma->head, &nvbo->vma_list); vma->refcount =1 ;
drivers/gpu/drm/nouveau/nouveau_channel.c +1 -1
@@ -38,7 +38,7 @@ nouveau_channel_pushbuf_init(struct nouveau_channel *chan)int ret;/* allocate buffer object */ - ret =nouveau_bo_new (dev,65536 ,0 , mem,0 ,0 , &chan->pushbuf_bo); + ret =nouveau_bo_new (dev,65536 ,0 , mem,0 ,0 ,NULL , &chan->pushbuf_bo);if (ret) goto out;
drivers/gpu/drm/nouveau/nouveau_drv.c +7 -1
@@ -408,7 +408,7 @@ static struct drm_driver driver = { .driver_features =DRIVER_USE_AGP |DRIVER_PCI_DMA |DRIVER_SG |DRIVER_HAVE_IRQ |DRIVER_IRQ_SHARED |DRIVER_GEM | - DRIVER_MODESET, + DRIVER_MODESET | DRIVER_PRIME, .load = nouveau_load, .firstopen = nouveau_firstopen, .lastclose = nouveau_lastclose,
@@ -430,6 +430,12 @@ static struct drm_driver driver = { .reclaim_buffers = drm_core_reclaim_buffers, .ioctls = nouveau_ioctls, .fops = &nouveau_driver_fops, + + .prime_handle_to_fd = drm_gem_prime_handle_to_fd, + .prime_fd_to_handle = drm_gem_prime_fd_to_handle, + .gem_prime_export = nouveau_gem_prime_export, + .gem_prime_import = nouveau_gem_prime_import, + .gem_init_object = nouveau_gem_object_new, .gem_free_object = nouveau_gem_object_del, .gem_open_object = nouveau_gem_object_open,
drivers/gpu/drm/nouveau/nouveau_drv.h +9 -1
@@ -86,6 +86,7 @@ struct nouveau_mem {u32 memtype;u64 offset;u64 size; +struct sg_table *sg; };struct nouveau_tile_reg {
@@ -1416,7 +1417,9 @@ extern int nv04_crtc_create(struct drm_device *, int index);extern struct ttm_bo_driver nouveau_bo_driver;extern int nouveau_bo_new (struct drm_device *,int size,int align,uint32_t flags,uint32_t tile_mode, -uint32_t tile_flags,struct nouveau_bo **); +uint32_t tile_flags, +struct sg_table *sg, +struct nouveau_bo **);extern int nouveau_bo_pin (struct nouveau_bo *,uint32_t flags);extern int nouveau_bo_unpin (struct nouveau_bo *);extern int nouveau_bo_map (struct nouveau_bo *);
@@ -1501,6 +1504,11 @@ extern int nouveau_gem_ioctl_cpu_fini(struct drm_device *, void *,extern int nouveau_gem_ioctl_info (struct drm_device *,void *,struct drm_file *); +extern struct dma_buf *nouveau_gem_prime_export (struct drm_device *dev, +struct drm_gem_object *obj,int flags); +extern struct drm_gem_object *nouveau_gem_prime_import (struct drm_device *dev, +struct dma_buf *dma_buf); +/* nouveau_display.c */ int nouveau_display_create (struct drm_device *dev);void nouveau_display_destroy (struct drm_device *dev);
drivers/gpu/drm/nouveau/nouveau_fence.c +1 -1
@@ -573,7 +573,7 @@ nouveau_fence_init(struct drm_device *dev)/* Create a shared VRAM heap for cross-channel sync. */ if (USE_SEMA (dev)) { ret =nouveau_bo_new (dev, size,0 ,TTM_PL_FLAG_VRAM , -0 ,0 , &dev_priv->fence.bo); +0 ,0 ,NULL , &dev_priv->fence.bo);if (ret)return ret;
drivers/gpu/drm/nouveau/nouveau_gem.c +5 -1
@@ -23,6 +23,7 @@* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ +#include <linux/dma-buf.h> #include "drmP.h" #include "drm.h"
@@ -53,6 +54,9 @@ nouveau_gem_object_del(struct drm_gem_object *gem)nouveau_bo_unpin (nvbo); } +if (gem->import_attach) +drm_prime_gem_destroy (gem, nvbo->bo.sg); +ttm_bo_unref (&bo);drm_gem_object_release (gem);
@@ -139,7 +143,7 @@ nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain, flags |=TTM_PL_FLAG_SYSTEM ; ret =nouveau_bo_new (dev, size, align, flags, tile_mode, - tile_flags, pnvbo); + tile_flags,NULL , pnvbo);if (ret)return ret; nvbo = *pnvbo;
drivers/gpu/drm/nouveau/nouveau_mem.c +1 -1
@@ -416,7 +416,7 @@ nouveau_mem_vram_init(struct drm_device *dev)if (dev_priv->card_type <NV_50 ) { ret =nouveau_bo_new (dev,256 *1024 ,0 ,TTM_PL_FLAG_VRAM , -0 ,0 , &dev_priv->vga_ram); +0 ,0 ,NULL , &dev_priv->vga_ram);if (ret ==0 ) ret =nouveau_bo_pin (dev_priv->vga_ram,TTM_PL_FLAG_VRAM );
drivers/gpu/drm/nouveau/nouveau_prime.c new file +132 -0
132 lines not on this page · raw message
drivers/gpu/drm/nouveau/nouveau_sgdma.c +4 -1
@@ -290,7 +290,10 @@ nv50_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)struct nouveau_mem *node = mem->mm_node;/* noop: bound in move_notify() */ - node->pages = nvbe->ttm.dma_address; +if (ttm->sg) { + node->sg = ttm->sg; + }else + node->pages = nvbe->ttm.dma_address;return 0 ; }
drivers/gpu/drm/nouveau/nouveau_vm.c +57 -0
63 lines not on this page · raw message
drivers/gpu/drm/nouveau/nouveau_vm.h +5 -1
@@ -72,6 +72,9 @@ struct nouveau_vm { u64 phys, u64 delta);void (*map_sg)(struct nouveau_vma *,struct nouveau_gpuobj *,struct nouveau_mem *,u32 pte,u32 cnt,dma_addr_t *); + +void (*map_sg_table)(struct nouveau_vma *,struct nouveau_gpuobj *, +struct nouveau_mem *,u32 pte,u32 cnt,dma_addr_t *);void (*unmap)(struct nouveau_gpuobj *pgt,u32 pte,u32 cnt);void (*flush)(struct nouveau_vm *); };
@@ -90,7 +93,8 @@ void nouveau_vm_unmap(struct nouveau_vma *);void nouveau_vm_unmap_at (struct nouveau_vma *,u64 offset,u64 length);void nouveau_vm_map_sg (struct nouveau_vma *,u64 offset,u64 length,struct nouveau_mem *); - +void nouveau_vm_map_sg_table (struct nouveau_vma *vma,u64 delta,u64 length, +struct nouveau_mem *mem);/* nv50_vm.c */ void nv50_vm_map_pgt (struct nouveau_gpuobj *pgd,u32 pde,struct nouveau_gpuobj *pgt[2 ]);
drivers/gpu/drm/nouveau/nv04_crtc.c +1 -1
@@ -1047,7 +1047,7 @@ nv04_crtc_create(struct drm_device *dev, int crtc_num)drm_mode_crtc_set_gamma_size (&nv_crtc->base,256 ); ret =nouveau_bo_new (dev,64 *64 *4 ,0x100 ,TTM_PL_FLAG_VRAM , -0 ,0x0000 , &nv_crtc->cursor.nvbo); +0 ,0x0000 ,NULL , &nv_crtc->cursor.nvbo);if (!ret) { ret =nouveau_bo_pin (nv_crtc->cursor.nvbo,TTM_PL_FLAG_VRAM );if (!ret)
drivers/gpu/drm/nouveau/nv50_crtc.c +2 -2
@@ -769,7 +769,7 @@ nv50_crtc_create(struct drm_device *dev, int index) nv_crtc->lut.depth =0 ; ret =nouveau_bo_new (dev,4096 ,0x100 ,TTM_PL_FLAG_VRAM , -0 ,0x0000 , &nv_crtc->lut.nvbo); +0 ,0x0000 ,NULL , &nv_crtc->lut.nvbo);if (!ret) { ret =nouveau_bo_pin (nv_crtc->lut.nvbo,TTM_PL_FLAG_VRAM );if (!ret)
@@ -795,7 +795,7 @@ nv50_crtc_create(struct drm_device *dev, int index)drm_mode_crtc_set_gamma_size (&nv_crtc->base,256 ); ret =nouveau_bo_new (dev,64 *64 *4 ,0x100 ,TTM_PL_FLAG_VRAM , -0 ,0x0000 , &nv_crtc->cursor.nvbo); +0 ,0x0000 ,NULL , &nv_crtc->cursor.nvbo);if (!ret) { ret =nouveau_bo_pin (nv_crtc->cursor.nvbo,TTM_PL_FLAG_VRAM );if (!ret)
drivers/gpu/drm/nouveau/nv50_evo.c +2 -2
@@ -117,7 +117,7 @@ nv50_evo_channel_new(struct drm_device *dev, int chid, evo->user_get =4 ; evo->user_put =0 ; - ret =nouveau_bo_new (dev,4096 ,0 ,TTM_PL_FLAG_VRAM ,0 ,0 , + ret =nouveau_bo_new (dev,4096 ,0 ,TTM_PL_FLAG_VRAM ,0 ,0 ,NULL , &evo->pushbuf_bo);if (ret ==0 ) ret =nouveau_bo_pin (evo->pushbuf_bo,TTM_PL_FLAG_VRAM );
@@ -333,7 +333,7 @@ nv50_evo_create(struct drm_device *dev) goto err; ret =nouveau_bo_new (dev,4096 ,0x1000 ,TTM_PL_FLAG_VRAM , -0 ,0x0000 , &dispc->sem.bo); +0 ,0x0000 ,NULL , &dispc->sem.bo);if (!ret) { ret =nouveau_bo_pin (dispc->sem.bo,TTM_PL_FLAG_VRAM );if (!ret)
drivers/gpu/drm/nouveau/nvd0_display.c +3 -3
@@ -882,7 +882,7 @@ nvd0_crtc_create(struct drm_device *dev, int index)drm_mode_crtc_set_gamma_size (crtc,256 ); ret =nouveau_bo_new (dev,64 *64 *4 ,0x100 ,TTM_PL_FLAG_VRAM , -0 ,0x0000 , &nv_crtc->cursor.nvbo); +0 ,0x0000 ,NULL , &nv_crtc->cursor.nvbo);if (!ret) { ret =nouveau_bo_pin (nv_crtc->cursor.nvbo,TTM_PL_FLAG_VRAM );if (!ret)
@@ -895,7 +895,7 @@ nvd0_crtc_create(struct drm_device *dev, int index) goto out; ret =nouveau_bo_new (dev,8192 ,0x100 ,TTM_PL_FLAG_VRAM , -0 ,0x0000 , &nv_crtc->lut.nvbo); +0 ,0x0000 ,NULL , &nv_crtc->lut.nvbo);if (!ret) { ret =nouveau_bo_pin (nv_crtc->lut.nvbo,TTM_PL_FLAG_VRAM );if (!ret)
@@ -2030,7 +2030,7 @@ nvd0_display_create(struct drm_device *dev)/* small shared memory area we use for notifiers and semaphores */ ret =nouveau_bo_new (dev,4096 ,0x1000 ,TTM_PL_FLAG_VRAM , -0 ,0x0000 , &disp->sync); +0 ,0x0000 ,NULL , &disp->sync);if (!ret) { ret =nouveau_bo_pin (disp->sync,TTM_PL_FLAG_VRAM );if (!ret)
Signature · 1 line
1.7.6
Quoted 4 lines · Dave Airlie
From: Dave Airlie <[email protected]>
This adds the ability for ttm common code to take an SG table and use it as the backing for a slave TTM object.
I took a look at both patches from the perspective of the TTM DMA pool code and it looks fine (thought I would prefer to test this first - but I don't have the hardware).
What I am curious is that you are allowing to map two or more of SG pages to different PCI devices. And for that you are using the nouveau_gem_map_dma_buf, which does this (roughly)
ttm->sg = sg_alloc_table().
for_each_sg()
sg->dma_address[i] = dma_map(..)
So I am seeing two potential issues:
1). ttm->sg = sg_alloc() is called on every new other device.
In other words, if you end up with three of these attachment PCI devices you are going to cause an memory leak of the sg_alloc() and over-write the ttm->sg every time. But I might be misreading how the import code works.
2). You are going to put in sg->dma_address() in of different PCI devices.
Meaning - the dma address might have a different value depending on whether there is an different IOMMU for each of the devices. On x86 that is usually not the case (the odd ball being the IBM high-end boxes that have an Calgary-X IOMMU for PCI buses).
What this means is that the DMA address you are programming in the sub-sequent PCI devices might the DMA address for a previous PCI device.
But both of these issues are only a problem if the slave cards have a different PCI device. If they are the same - then I think you are OK (except the multiple calls to import which would cause a memory leak).
Quoted 163 lines
The drivers can then populate their GTT tables using the SG object.
Signed-off-by Dave Airlie <[email protected]>--- drivers/gpu/drm/nouveau/nouveau_bo.c | 2 +- drivers/gpu/drm/radeon/radeon_object.c | 2 +- drivers/gpu/drm/ttm/ttm_bo.c | 14 +++++++++++++- drivers/gpu/drm/vmwgfx/vmwgfx_resource.c | 2 +- include/drm/ttm/ttm_bo_api.h | 9 ++++++++- include/drm/ttm/ttm_bo_driver.h | 2 ++ 6 files changed, 26 insertions(+), 5 deletions(-)drivers/gpu/drm/nouveau/nouveau_bo.c +1
-1@@ -121,7 +121,7 @@ nouveau_bo_new(struct drm_device *dev, int size, int align, ret =ttm_bo_init (&dev_priv->ttm.bdev, &nvbo->bo, size, ttm_bo_type_device, &nvbo->placement, - align >>PAGE_SHIFT ,0 , false,NULL , acc_size, + align >>PAGE_SHIFT ,0 , false,NULL , acc_size,NULL , nouveau_bo_del_ttm);if (ret) {/* ttm will call nouveau_bo_del_ttm if it fails.. */ drivers/gpu/drm/radeon/radeon_object.c +1
-1@@ -155,7 +155,7 @@ retry:mutex_lock (&rdev->vram_mutex); r =ttm_bo_init (&rdev->mman.bdev, &bo->tbo, size, type, &bo->placement, page_align,0 , !kernel,NULL , - acc_size, &radeon_ttm_bo_destroy); + acc_size,NULL , &radeon_ttm_bo_destroy);mutex_unlock (&rdev->vram_mutex);if (unlikely (r !=0 )) {if (r != -ERESTARTSYS ) {drivers/gpu/drm/ttm/ttm_bo.c +13
-1@@ -343,6 +343,16 @@ static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)if (unlikely (bo->ttm ==NULL )) ret = -ENOMEM ;break ; +case ttm_bo_type_sg: + bo->ttm = bdev->driver->ttm_tt_create (bdev, bo->num_pages <<PAGE_SHIFT , + page_flags |TTM_PAGE_FLAG_SG , + glob->dummy_read_page); +if (unlikely (bo->ttm ==NULL )) { + ret = -ENOMEM ; +break ; + } + bo->ttm->sg = bo->sg; +break ;default :pr_err ("Illegal buffer object type\n" ); ret = -EINVAL ;@@ -1169,6 +1179,7 @@ int ttm_bo_init(struct ttm_bo_device *bdev,bool interruptible, struct file *persistent_swap_storage, size_t acc_size, + struct sg_table *sg, void (*destroy) (struct ttm_buffer_object *)) { int ret = 0;@@ -1223,6 +1234,7 @@ int ttm_bo_init(struct ttm_bo_device *bdev, bo->seq_valid = false; bo->persistent_swap_storage = persistent_swap_storage; bo->acc_size = acc_size; + bo->sg = sg;atomic_inc (&bo->glob->bo_count); ret =ttm_bo_check_placement (bo, placement);@@ -1312,7 +1324,7 @@ int ttm_bo_create(struct ttm_bo_device *bdev, ret =ttm_bo_init (bdev, bo, size, type, placement, page_alignment, buffer_start, interruptible, - persistent_swap_storage, acc_size,NULL ); + persistent_swap_storage, acc_size,NULL ,NULL );if (likely (ret ==0 )) *p_bo = bo;drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +1
-1@@ -1567,7 +1567,7 @@ int vmw_dmabuf_init(struct vmw_private *dev_priv, ret =ttm_bo_init (bdev, &vmw_bo->base, size, ttm_bo_type_device, placement,0 ,0 , interruptible, -NULL , acc_size, bo_free); +NULL , acc_size,NULL , bo_free);return ret; }include/drm/ttm/ttm_bo_api.h +8
-1@@ -124,11 +124,15 @@ struct ttm_mem_reg {* * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers, * but they cannot be accessed from user-space. For kernel-only use. +* +* @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another +* driver. */ enum ttm_bo_type { ttm_bo_type_device, - ttm_bo_type_kernel + ttm_bo_type_kernel, + ttm_bo_type_sg };struct ttm_tt ;@@ -271,6 +275,8 @@ struct ttm_buffer_object {unsigned long offset;uint32_t cur_placement; + +struct sg_table *sg; };/** @@ -503,6 +509,7 @@ extern int ttm_bo_init(struct ttm_bo_device *bdev,bool interrubtible, struct file *persistent_swap_storage, size_t acc_size, + struct sg_table *sg, void (*destroy) (struct ttm_buffer_object *));/** include/drm/ttm/ttm_bo_driver.h +2
-0@@ -81,6 +81,7 @@ struct ttm_backend_func {#define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)#define TTM_PAGE_FLAG_ZERO_ALLOC (1 << 6)#define TTM_PAGE_FLAG_DMA32 (1 << 7) +#define TTM_PAGE_FLAG_SG (1 << 8)enum ttm_caching_state { tt_uncached,@@ -116,6 +117,7 @@ struct ttm_tt {struct page **pages;uint32_t page_flags;unsigned long num_pages; +struct sg_table *sg;/* for SG objects via dma-buf */ struct ttm_bo_global *glob;struct ttm_backend *be;struct file *swap_storage;Signature · 6 lines
1.7.6 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
Dave Airlie
maintainerQuoted 18 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Dave Airlie
From: Dave Airlie <[email protected]>
This adds the ability for ttm common code to take an SG table and use it as the backing for a slave TTM object.
I took a look at both patches from the perspective of the TTM DMA pool code and it looks fine (thought I would prefer to test this first - but I don't have the hardware).
What I am curious is that you are allowing to map two or more of SG pages to different PCI devices. And for that you are using the
nouveau_gem_map_dma_buf, which does this (roughly)ttm->sg =
sg_alloc_table().
for_each_sg()
sg->dma_address[i] =dma_map(..)
Quoted 6 lines
So I am seeing two potential issues:
1).ttm->sg=sg_alloc()is called on every new other device. In other words, if you end up with three of these attachment PCI devices you are going to cause an memory leak of thesg_alloc()and over-write thettm->sgevery time. But I might be misreading how the import code works.
I think you are mixing up the export and import sides.
The export side takes a TTM object, and create an sg table mapped into the importers address space.
The import side takes an exported sg_table and wraps an object around it.
So for multiple importers, there are multiple sg_tables. For one import there should only be one sg_table per object.
Quoted 9 lines
2). You are going to put in
sg->dma_address()in of different PCI devices. Meaning - the dma address might have a different value depending on whether there is an different IOMMU for each of the devices. On x86 that is usually not the case (the odd ball being the IBM high-end boxes that have an Calgary-X IOMMU for PCI buses).
What this means is that the DMA address you are programming in the sub-sequent PCI devices might the DMA address for a previous PCI device.
Quoted 3 lines
But both of these issues are only a problem if the slave cards have a different PCI device. If they are the same - then I think you are OK (except the multiple calls to import which would cause a memory leak).
Yeah we need to stop a slave calling map_dma_buf multiple times, but we block that by keeping a list of currently imported buffer handles.
Dave.