Add Bufmgr interfaces for create2 ioctl
cover Add Bufmgr interfaces for create2 ioctl
From: Sourab Gupta <[email protected]>
This patch series add the bufmgr interfaces for exposing the create2 ioctl.
By means of this ioctl, we can specify number of parameters during object creation time such as placement, cache domains, caching, madvise, tiling etc.
Sourab Gupta (2):
intel: Adding bufmgr interfaces for exposing create2 ioctl
intel: Add the cacheing logic for bo's created using create2 ioctl
include/drm/i915_drm.h | 107 ++++++++++++++++++++ intel/intel_bufmgr.c | 7 ++ intel/intel_bufmgr.h | 20 ++++ intel/intel_bufmgr_gem.c | 241 ++++++++++++++++++++++++++++++++++++++++++++++ intel/intel_bufmgr_priv.h | 16 +++ 5 files changed, 391 insertions(+)
Signature · 1 line
1.8.5.1
PATCH 1/2 intel: Adding bufmgr interfaces for exposing create2 ioctl
From: Sourab Gupta <[email protected]>
This patch series add the bufmgr interfaces for exposing the create2 ioctl.
By means of this ioctl, we can specify number of parameters during object creation time such as placement, cache domains, caching, madvise, tiling etc.
--- include/drm/i915_drm.h | 107 ++++++++++++++++++++++++++++++++++++ intel/intel_bufmgr.c | 7 +++ intel/intel_bufmgr.h | 20 +++++++ intel/intel_bufmgr_gem.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++ intel/intel_bufmgr_priv.h | 16 ++++++ 5 files changed, 286 insertions(+)
include/drm/i915_drm.h +107 -0
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {#define DRM_I915_GEM_GET_CACHING 0x30#define DRM_I915_REG_READ 0x31#define DRM_I915_GET_RESET_STATS 0x32 +#define DRM_I915_GEM_CREATE2 0x34#define DRM_IOCTL_I915_INIT DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)#define DRM_IOCTL_I915_FLUSH DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -253,6 +254,7 @@ typedef struct _drm_i915_sarea {#define DRM_IOCTL_I915_GEM_ENTERVT DRM_IO(DRM_COMMAND_BASE + DRM_I915_GEM_ENTERVT)#define DRM_IOCTL_I915_GEM_LEAVEVT DRM_IO(DRM_COMMAND_BASE + DRM_I915_GEM_LEAVEVT)#define DRM_IOCTL_I915_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct drm_i915_gem_create) +#define DRM_IOCTL_I915_GEM_CREATE2 DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE2, struct drm_i915_gem_create2)#define DRM_IOCTL_I915_GEM_PREAD DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_PREAD, struct drm_i915_gem_pread)#define DRM_IOCTL_I915_GEM_PWRITE DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_PWRITE, struct drm_i915_gem_pwrite)#define DRM_IOCTL_I915_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MMAP, struct drm_i915_gem_mmap)
@@ -434,6 +436,111 @@ struct drm_i915_gem_create {__u32 pad; }; +struct drm_i915_gem_create2 { +/** +* Requested size for the object. +* +* The (page-aligned) allocated size for the object will be returned. +*/ +__u64 size; + +/** +* Requested offset for the object. +* +* Can be used for "soft-pinning" the object into the per-process +* GTT of the target context upon creation. Only possible if using +* contexts and per-process GTTs. +* +* The address must be page-aligned, and have the valid bit set. +*/ +__u64 offset; +#define I915_CREATE_OFFSET_VALID (1<<0) + +/** +* Target context of the object. +* +* The context of the object can be used for setting the initial offset +* of the object in the per-process GTT. +*/ +__u32 context; + +/** +* Requested placement (which memory domain) +* +* You can request that the object be created from special memory +* rather than regular system pages. Such irregular objects may +* have certain restrictions (such as CPU access to a stolen +* object is verboten). +*/ +__u32 placement; +#define I915_CREATE_PLACEMENT_SYSTEM 0 +#define I915_CREATE_PLACEMENT_STOLEN 1/* Cannot use CPU mmaps or pread/pwrite */ +/** +* Requested domain (which cache domain) +* +* You can request that the object be created from memory in a +* certain cache domain (such as RENDER, CPU or GTT). In some cases, +* this then may allocate from a pool of such pages to avoid any +* migration overhead, but it is always equivalent to performing +* an explicit set-domain(read=DOMAIN, write=DOMAIN) on the +* constructed object. +* +* Set to 0, to leave the initial domain unspecified and defaulting +* to the domain set by the constructor. +* +* See DRM_IOCTL_I915_GEM_SET_DOMAIN +*/ +__u32 domain; + +/** +* Requested cache level. +* +* See DRM_IOCTL_I915_GEM_SET_CACHING +*/ +__u32 caching; + +/** +* Requested tiling mode. +* +* See DRM_IOCTL_I915_GEM_SET_TILING +*/ +__u32 tiling_mode; +/** +* Requested stride for tiling. +* +* See DRM_IOCTL_I915_GEM_SET_TILING +*/ +__u32 stride; + +/** +* Requested madvise priority. +* +* See DRM_IOCTL_I915_GEM_MADVISE +*/ +__u32 madvise; + +/** +* Additional miscellaneous flags +* +* Reserved for future use, must be zero. +*/ +__u32 flags; + +/** +* Padding for 64-bit struct alignment. +* +* Reserved for future use, must be zero. +*/ +__u32 pad; + +/** +* Returned handle for the object. +* +* Object handles are nonzero. +*/ +__u32 handle; +}; +struct drm_i915_gem_pread {/** Handle for the object being read. */ __u32 handle;
intel/intel_bufmgr.c +7 -0
@@ -69,6 +69,13 @@ drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, tiling_mode, pitch, flags); } +drm_intel_bo * +drm_intel_bo_alloc_from_params (drm_intel_bufmgr *bufmgr, +drm_intel_bo_alloc_params *params) +{ +return bufmgr->bo_alloc_from_params (bufmgr, params); +} +void drm_intel_bo_reference (drm_intel_bo *bo) { bo->bufmgr->bo_reference (bo);
intel/intel_bufmgr.h +20 -0
@@ -105,6 +105,22 @@ typedef struct _drm_intel_aub_annotation {uint32_t ending_offset; } drm_intel_aub_annotation; +typedef struct _drm_intel_bo_alloc_params { +uint32_t width; +uint32_t height; +uint32_t cpp; +uint32_t tiling_mode; +unsigned long stride; +unsigned long flags; +uint32_t offset; +uint32_t context; +uint32_t cache_domain; +uint32_t placement; +uint32_t caching; +uint32_t madvise; +char *name; +}drm_intel_bo_alloc_params ; +#define BO_ALLOC_FOR_RENDER (1<<0) drm_intel_bo *drm_intel_bo_alloc(drm_intel_bufmgr *bufmgr, const char *name,
@@ -119,6 +135,10 @@ drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,uint32_t *tiling_mode, unsigned long *pitch, unsigned long flags); +drm_intel_bo * +drm_intel_bo_alloc_from_params (drm_intel_bufmgr *bufmgr, +drm_intel_bo_alloc_params *params); +void drm_intel_bo_reference (drm_intel_bo *bo);void drm_intel_bo_unreference (drm_intel_bo *bo);int drm_intel_bo_map (drm_intel_bo *bo,int write_enable);
intel/intel_bufmgr_gem.c +136 -0
154 lines not on this page · raw message
intel/intel_bufmgr_priv.h +16 -0
@@ -83,6 +83,22 @@ struct _drm_intel_bufmgr {unsigned long *pitch, unsigned long flags); +/** +* Allocate a buffer object from params. +* Params include fields such as tiling mode, caching, domain, +* placement, madvise, gtt offset etc. +* The params are then used to populate the fields of create2 ioctl, +* which creates buffer obj, based on these parameters. +* +* Note the tiling format in params may be rejected; callers should +* check the 'tiling_mode' field on return, as well as the pitch +* value, which may have been rounded up to accommodate for tiling +* restrictions. +*/ + drm_intel_bo *(*bo_alloc_from_params) (drm_intel_bufmgr *bufmgr, + drm_intel_bo_alloc_params *params); + +/** Takes a reference on a buffer object */ void (*bo_reference) (drm_intel_bo *bo);
Signature · 1 line
1.8.5.1
PATCH 2/2 intel: Add the cacheing logic for bo's created using create2 ioctl
In reply to [email protected]From: Sourab Gupta <[email protected]>
This patch add the cacheing logic for the buffer objects created using create2 ioctl. This cacheing logic is in principle similar the cachecing employed in the earlier clear ioctl.
The cacheing is not employed if object is created in stolen region or if the offset is specified for the object.
--- intel/intel_bufmgr_gem.c | 201 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 153 insertions(+), 48 deletions(-)
intel/intel_bufmgr_gem.c +153 -48
@@ -781,62 +781,165 @@ drm_intel_gem_bo_alloc_create2(drm_intel_bufmgr *bufmgr, {drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;drm_intel_bo_gem *bo_gem; -struct drm_i915_gem_create2 create; -int ret; - - bo_gem =calloc (1 ,sizeof (*bo_gem)); -if (!bo_gem) -return NULL ; - - bo_gem->bo.size = size; +unsigned int page_size =getpagesize (); +struct drm_intel_gem_bo_bucket *bucket; +bool alloc_from_cache; +unsigned long bo_size; +bool for_render = false; +int ret, stride; -VG_CLEAR (create); - create.size = size; -if (params->offset &I915_CREATE_OFFSET_VALID ) { - create.offset = params->offset; - create.context = params->context; - } +if (params->flags &BO_ALLOC_FOR_RENDER ) + for_render = true;if (params->tiling_mode ==I915_TILING_NONE ) - create.stride =0 ; + stride =0 ;else - create.stride = params->stride; + stride = params->stride; - create.placement = params->placement; - create.domain = params->cache_domain; - create.caching = params->caching; - create.tiling_mode = params->tiling_mode; - create.madvise = params->madvise; +if (params->placement ==I915_CREATE_PLACEMENT_STOLEN || + params->offset &I915_CREATE_OFFSET_VALID || + params->madvise ==I915_MADV_DONTNEED ) { + bucket =NULL ; + bo_size = size; + }else { +/* Round the allocated size up to a power of two +* number of pages. +*/ + bucket =drm_intel_gem_bo_bucket_for_size (bufmgr_gem, size); +/* If we don't have caching at this size, don't actually +* round the allocation up. +*/ +if (bucket ==NULL ) { + bo_size = size; +if (bo_size < page_size) + bo_size = page_size; + }else { + bo_size = bucket->size; + } + } +pthread_mutex_lock (&bufmgr_gem->lock); +/* Get a buffer out of the cache if available */ +retry: + alloc_from_cache = false; +if (bucket !=NULL && !DRMLISTEMPTY (&bucket->head)) { +if (for_render) { +/* Allocate new render-target BOs from the tail (MRU) +* of the list, as it will likely be hot in the GPU +* cache and in the aperture for us. +*/ + bo_gem =DRMLISTENTRY (drm_intel_bo_gem, + bucket->head.prev, head); +DRMLISTDEL (&bo_gem->head); + alloc_from_cache = true; + }else { +/* For non-render-target BOs (where we're probably +* going to map it first thing in order to fill it +* with data), check if the last BO in the cache is +* unbusy, and only reuse in that case. Otherwise, +* allocating a new buffer is probably faster than +* waiting for the GPU to finish. +*/ + bo_gem =DRMLISTENTRY (drm_intel_bo_gem, + bucket->head.next, head); +if (!drm_intel_gem_bo_busy (&bo_gem->bo)) { + alloc_from_cache = true; +DRMLISTDEL (&bo_gem->head); + } + } +if (alloc_from_cache) { +struct drm_i915_gem_caching bo_cache; +struct drm_i915_gem_set_domain set_domain; +/* We need to call the functions for setcache, domain, +* tiling, madvise here, as these parameters may be +* different for the bo present in cache. +*/ +if (!drm_intel_gem_bo_madvise_internal + (bufmgr_gem, bo_gem, +I915_MADV_WILLNEED )) { +drm_intel_gem_bo_free (&bo_gem->bo); +drm_intel_gem_bo_cache_purge_bucket (bufmgr_gem, + bucket); + goto retry; + } - ret =drmIoctl (bufmgr_gem->fd, -DRM_IOCTL_I915_GEM_CREATE2 , - &create); - bo_gem->gem_handle = create.handle; - bo_gem->bo.handle = bo_gem->gem_handle; -if (ret !=0 ) { -free (bo_gem); -return NULL ; +if (drm_intel_gem_bo_set_tiling_internal (&bo_gem->bo, + params->tiling_mode, + stride)) { +drm_intel_gem_bo_free (&bo_gem->bo); + goto retry; + } +VG_CLEAR (bo_cache); + bo_cache.handle = bo_gem->gem_handle; + bo_cache.caching = params->caching; +if (drmIoctl (bufmgr_gem->fd, +DRM_IOCTL_I915_GEM_SET_CACHING , + &bo_cache)) { +drm_intel_gem_bo_free (&bo_gem->bo); + goto retry; + } +VG_CLEAR (set_domain); + set_domain.handle = bo_gem->gem_handle; + set_domain.read_domains = params->cache_domain; + set_domain.write_domain = params->cache_domain; +if (drmIoctl (bufmgr_gem->fd, +DRM_IOCTL_I915_GEM_SET_DOMAIN , + &set_domain)) { +drm_intel_gem_bo_free (&bo_gem->bo); + goto retry; + } + } } - bo_gem->bo.bufmgr = bufmgr; +pthread_mutex_unlock (&bufmgr_gem->lock); -/* We have to call the set_tiling ioctl, as create2 ioctl -* doesn't return the swizzle mode -* TODO: Is this required? Can the functionality be put in -* create2 ioctl? -*/ - bo_gem->tiling_mode = create.tiling_mode; - bo_gem->swizzle_mode =I915_BIT_6_SWIZZLE_NONE ; - bo_gem->stride = create.stride; +if (!alloc_from_cache) { +struct drm_i915_gem_create2 create; + bo_gem =calloc (1 ,sizeof (*bo_gem)); +if (!bo_gem) +return NULL ; + bo_gem->bo.size = bo_size; +VG_CLEAR (create); + create.size = bo_size; +if (params->offset &I915_CREATE_OFFSET_VALID ) { + create.offset = params->offset; + create.context = params->context; + } -if (drm_intel_gem_bo_set_tiling_internal (&bo_gem->bo, - create.tiling_mode, - create.stride)) { -drm_intel_gem_bo_free (&bo_gem->bo); -return NULL ; - } + create.stride = stride; + create.placement = params->placement; + create.domain = params->cache_domain; + create.caching = params->caching; + create.tiling_mode = params->tiling_mode; + create.madvise = params->madvise; -DRMINITLISTHEAD (&bo_gem->name_list); -DRMINITLISTHEAD (&bo_gem->vma_list); + ret =drmIoctl (bufmgr_gem->fd, +DRM_IOCTL_I915_GEM_CREATE2 , + &create); + bo_gem->gem_handle = create.handle; + bo_gem->bo.handle = bo_gem->gem_handle; +if (ret !=0 ) { +free (bo_gem); +return NULL ; + } + bo_gem->bo.bufmgr = bufmgr; + +/* We have to call the set_tiling ioctl, as create2 ioctl +* doesn't return the swizzle mode +* TODO: Can create2 achieve this functionality? +*/ + bo_gem->tiling_mode = create.tiling_mode; + bo_gem->swizzle_mode =I915_BIT_6_SWIZZLE_NONE ; + bo_gem->stride = stride; + +if (drm_intel_gem_bo_set_tiling_internal (&bo_gem->bo, + create.tiling_mode, + stride)) { +drm_intel_gem_bo_free (&bo_gem->bo); +return NULL ; + } + +DRMINITLISTHEAD (&bo_gem->name_list); +DRMINITLISTHEAD (&bo_gem->vma_list); + } bo_gem->name = params->name;atomic_set (&bo_gem->refcount,1 );
@@ -844,8 +947,10 @@ drm_intel_gem_bo_alloc_create2(drm_intel_bufmgr *bufmgr, bo_gem->reloc_tree_fences =0 ; bo_gem->used_as_reloc_target = false; bo_gem->has_error = false; -/* set reusable to false, as caching is not employed currently*/ - bo_gem->reusable = false; +if (params->placement ==I915_CREATE_PLACEMENT_STOLEN ) + bo_gem->reusable = false; +else + bo_gem->reusable = true; bo_gem->aub_annotations =NULL ; bo_gem->aub_annotation_count =0 ;
Signature · 1 line
1.8.5.1