Commit 97d55396 authored by Matthew Auld's avatar Matthew Auld Committed by Chris Wilson

drm/i915/region: convert object_create into object_init

Give more flexibility to the caller, if they already have an allocated
object, in case they wish to apply some transformation to the object
prior to handing it over to the region specific initialisation step,
like in gem_create_ext where we would like to first apply the extensions
to the object.
Signed-off-by: default avatarMatthew Auld <matthew.auld@intel.com>
Reviewed-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
Link: https://patchwork.freedesktop.org/patch/msgid/20210114182402.840247-3-matthew.auld@intel.com
parent 7d58fef2
...@@ -31,18 +31,13 @@ i915_gem_object_create_lmem(struct drm_i915_private *i915, ...@@ -31,18 +31,13 @@ i915_gem_object_create_lmem(struct drm_i915_private *i915,
size, flags); size, flags);
} }
struct drm_i915_gem_object * int __i915_gem_lmem_object_init(struct intel_memory_region *mem,
__i915_gem_lmem_object_create(struct intel_memory_region *mem, struct drm_i915_gem_object *obj,
resource_size_t size, resource_size_t size,
unsigned int flags) unsigned int flags)
{ {
static struct lock_class_key lock_class; static struct lock_class_key lock_class;
struct drm_i915_private *i915 = mem->i915; struct drm_i915_private *i915 = mem->i915;
struct drm_i915_gem_object *obj;
obj = i915_gem_object_alloc();
if (!obj)
return ERR_PTR(-ENOMEM);
drm_gem_private_object_init(&i915->drm, &obj->base, size); drm_gem_private_object_init(&i915->drm, &obj->base, size);
i915_gem_object_init(obj, &i915_gem_lmem_obj_ops, &lock_class); i915_gem_object_init(obj, &i915_gem_lmem_obj_ops, &lock_class);
...@@ -53,5 +48,5 @@ __i915_gem_lmem_object_create(struct intel_memory_region *mem, ...@@ -53,5 +48,5 @@ __i915_gem_lmem_object_create(struct intel_memory_region *mem,
i915_gem_object_init_memory_region(obj, mem, flags); i915_gem_object_init_memory_region(obj, mem, flags);
return obj; return 0;
} }
...@@ -21,9 +21,9 @@ i915_gem_object_create_lmem(struct drm_i915_private *i915, ...@@ -21,9 +21,9 @@ i915_gem_object_create_lmem(struct drm_i915_private *i915,
resource_size_t size, resource_size_t size,
unsigned int flags); unsigned int flags);
struct drm_i915_gem_object * int __i915_gem_lmem_object_init(struct intel_memory_region *mem,
__i915_gem_lmem_object_create(struct intel_memory_region *mem, struct drm_i915_gem_object *obj,
resource_size_t size, resource_size_t size,
unsigned int flags); unsigned int flags);
#endif /* !__I915_GEM_LMEM_H */ #endif /* !__I915_GEM_LMEM_H */
...@@ -143,6 +143,7 @@ i915_gem_object_create_region(struct intel_memory_region *mem, ...@@ -143,6 +143,7 @@ i915_gem_object_create_region(struct intel_memory_region *mem,
unsigned int flags) unsigned int flags)
{ {
struct drm_i915_gem_object *obj; struct drm_i915_gem_object *obj;
int err;
/* /*
* NB: Our use of resource_size_t for the size stems from using struct * NB: Our use of resource_size_t for the size stems from using struct
...@@ -160,6 +161,10 @@ i915_gem_object_create_region(struct intel_memory_region *mem, ...@@ -160,6 +161,10 @@ i915_gem_object_create_region(struct intel_memory_region *mem,
GEM_BUG_ON(!size); GEM_BUG_ON(!size);
GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_MIN_ALIGNMENT)); GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_MIN_ALIGNMENT));
obj = i915_gem_object_alloc();
if (!obj)
return ERR_PTR(-ENOMEM);
/* /*
* XXX: There is a prevalence of the assumption that we fit the * XXX: There is a prevalence of the assumption that we fit the
* object's page count inside a 32bit _signed_ variable. Let's document * object's page count inside a 32bit _signed_ variable. Let's document
...@@ -173,9 +178,14 @@ i915_gem_object_create_region(struct intel_memory_region *mem, ...@@ -173,9 +178,14 @@ i915_gem_object_create_region(struct intel_memory_region *mem,
if (overflows_type(size, obj->base.size)) if (overflows_type(size, obj->base.size))
return ERR_PTR(-E2BIG); return ERR_PTR(-E2BIG);
obj = mem->ops->create_object(mem, size, flags); err = mem->ops->init_object(mem, obj, size, flags);
if (!IS_ERR(obj)) if (err)
trace_i915_gem_object_create(obj); goto err_object_free;
trace_i915_gem_object_create(obj);
return obj; return obj;
err_object_free:
i915_gem_object_free(obj);
return ERR_PTR(err);
} }
...@@ -464,26 +464,21 @@ static int __create_shmem(struct drm_i915_private *i915, ...@@ -464,26 +464,21 @@ static int __create_shmem(struct drm_i915_private *i915,
return 0; return 0;
} }
static struct drm_i915_gem_object * static int shmem_object_init(struct intel_memory_region *mem,
create_shmem(struct intel_memory_region *mem, struct drm_i915_gem_object *obj,
resource_size_t size, resource_size_t size,
unsigned int flags) unsigned int flags)
{ {
static struct lock_class_key lock_class; static struct lock_class_key lock_class;
struct drm_i915_private *i915 = mem->i915; struct drm_i915_private *i915 = mem->i915;
struct drm_i915_gem_object *obj;
struct address_space *mapping; struct address_space *mapping;
unsigned int cache_level; unsigned int cache_level;
gfp_t mask; gfp_t mask;
int ret; int ret;
obj = i915_gem_object_alloc();
if (!obj)
return ERR_PTR(-ENOMEM);
ret = __create_shmem(i915, &obj->base, size); ret = __create_shmem(i915, &obj->base, size);
if (ret) if (ret)
goto fail; return ret;
mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
if (IS_I965GM(i915) || IS_I965G(i915)) { if (IS_I965GM(i915) || IS_I965G(i915)) {
...@@ -522,11 +517,7 @@ create_shmem(struct intel_memory_region *mem, ...@@ -522,11 +517,7 @@ create_shmem(struct intel_memory_region *mem,
i915_gem_object_init_memory_region(obj, mem, 0); i915_gem_object_init_memory_region(obj, mem, 0);
return obj; return 0;
fail:
i915_gem_object_free(obj);
return ERR_PTR(ret);
} }
struct drm_i915_gem_object * struct drm_i915_gem_object *
...@@ -611,7 +602,7 @@ static void release_shmem(struct intel_memory_region *mem) ...@@ -611,7 +602,7 @@ static void release_shmem(struct intel_memory_region *mem)
static const struct intel_memory_region_ops shmem_region_ops = { static const struct intel_memory_region_ops shmem_region_ops = {
.init = init_shmem, .init = init_shmem,
.release = release_shmem, .release = release_shmem,
.create_object = create_shmem, .init_object = shmem_object_init,
}; };
struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915) struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915)
......
...@@ -621,18 +621,13 @@ static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = { ...@@ -621,18 +621,13 @@ static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
.release = i915_gem_object_release_stolen, .release = i915_gem_object_release_stolen,
}; };
static struct drm_i915_gem_object * int __i915_gem_object_create_stolen(struct intel_memory_region *mem,
__i915_gem_object_create_stolen(struct intel_memory_region *mem, struct drm_i915_gem_object *obj,
struct drm_mm_node *stolen) struct drm_mm_node *stolen)
{ {
static struct lock_class_key lock_class; static struct lock_class_key lock_class;
struct drm_i915_gem_object *obj;
unsigned int cache_level; unsigned int cache_level;
int err = -ENOMEM; int err;
obj = i915_gem_object_alloc();
if (!obj)
goto err;
drm_gem_private_object_init(&mem->i915->drm, &obj->base, stolen->size); drm_gem_private_object_init(&mem->i915->drm, &obj->base, stolen->size);
i915_gem_object_init(obj, &i915_gem_object_stolen_ops, &lock_class); i915_gem_object_init(obj, &i915_gem_object_stolen_ops, &lock_class);
...@@ -644,55 +639,47 @@ __i915_gem_object_create_stolen(struct intel_memory_region *mem, ...@@ -644,55 +639,47 @@ __i915_gem_object_create_stolen(struct intel_memory_region *mem,
err = i915_gem_object_pin_pages(obj); err = i915_gem_object_pin_pages(obj);
if (err) if (err)
goto cleanup; return err;
i915_gem_object_init_memory_region(obj, mem, 0); i915_gem_object_init_memory_region(obj, mem, 0);
return obj; return 0;
cleanup:
i915_gem_object_free(obj);
err:
return ERR_PTR(err);
} }
static struct drm_i915_gem_object * int _i915_gem_object_stolen_init(struct intel_memory_region *mem,
_i915_gem_object_create_stolen(struct intel_memory_region *mem, struct drm_i915_gem_object *obj,
resource_size_t size, resource_size_t size,
unsigned int flags) unsigned int flags)
{ {
struct drm_i915_private *i915 = mem->i915; struct drm_i915_private *i915 = mem->i915;
struct drm_i915_gem_object *obj;
struct drm_mm_node *stolen; struct drm_mm_node *stolen;
int ret; int ret;
if (!drm_mm_initialized(&i915->mm.stolen)) if (!drm_mm_initialized(&i915->mm.stolen))
return ERR_PTR(-ENODEV); return -ENODEV;
if (size == 0) if (size == 0)
return ERR_PTR(-EINVAL); return -EINVAL;
stolen = kzalloc(sizeof(*stolen), GFP_KERNEL); stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
if (!stolen) if (!stolen)
return ERR_PTR(-ENOMEM); return -ENOMEM;
ret = i915_gem_stolen_insert_node(i915, stolen, size, 4096); ret = i915_gem_stolen_insert_node(i915, stolen, size, 4096);
if (ret) { if (ret)
obj = ERR_PTR(ret);
goto err_free; goto err_free;
}
obj = __i915_gem_object_create_stolen(mem, stolen); ret = __i915_gem_object_create_stolen(mem, obj, stolen);
if (IS_ERR(obj)) if (ret)
goto err_remove; goto err_remove;
return obj; return 0;
err_remove: err_remove:
i915_gem_stolen_remove_node(i915, stolen); i915_gem_stolen_remove_node(i915, stolen);
err_free: err_free:
kfree(stolen); kfree(stolen);
return obj; return ret;
} }
struct drm_i915_gem_object * struct drm_i915_gem_object *
...@@ -722,7 +709,7 @@ static void release_stolen(struct intel_memory_region *mem) ...@@ -722,7 +709,7 @@ static void release_stolen(struct intel_memory_region *mem)
static const struct intel_memory_region_ops i915_region_stolen_ops = { static const struct intel_memory_region_ops i915_region_stolen_ops = {
.init = init_stolen, .init = init_stolen,
.release = release_stolen, .release = release_stolen,
.create_object = _i915_gem_object_create_stolen, .init_object = _i915_gem_object_stolen_init,
}; };
struct intel_memory_region *i915_gem_stolen_setup(struct drm_i915_private *i915) struct intel_memory_region *i915_gem_stolen_setup(struct drm_i915_private *i915)
...@@ -771,13 +758,23 @@ i915_gem_object_create_stolen_for_preallocated(struct drm_i915_private *i915, ...@@ -771,13 +758,23 @@ i915_gem_object_create_stolen_for_preallocated(struct drm_i915_private *i915,
goto err_free; goto err_free;
} }
obj = __i915_gem_object_create_stolen(mem, stolen); obj = i915_gem_object_alloc();
if (IS_ERR(obj)) if (!obj) {
obj = ERR_PTR(-ENOMEM);
goto err_stolen; goto err_stolen;
}
ret = __i915_gem_object_create_stolen(mem, obj, stolen);
if (ret) {
obj = ERR_PTR(ret);
goto err_object_free;
}
i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE); i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
return obj; return obj;
err_object_free:
i915_gem_object_free(obj);
err_stolen: err_stolen:
i915_gem_stolen_remove_node(i915, stolen); i915_gem_stolen_remove_node(i915, stolen);
err_free: err_free:
......
...@@ -98,7 +98,7 @@ region_lmem_init(struct intel_memory_region *mem) ...@@ -98,7 +98,7 @@ region_lmem_init(struct intel_memory_region *mem)
static const struct intel_memory_region_ops intel_region_lmem_ops = { static const struct intel_memory_region_ops intel_region_lmem_ops = {
.init = region_lmem_init, .init = region_lmem_init,
.release = region_lmem_release, .release = region_lmem_release,
.create_object = __i915_gem_lmem_object_create, .init_object = __i915_gem_lmem_object_init,
}; };
struct intel_memory_region * struct intel_memory_region *
......
...@@ -57,10 +57,10 @@ struct intel_memory_region_ops { ...@@ -57,10 +57,10 @@ struct intel_memory_region_ops {
int (*init)(struct intel_memory_region *mem); int (*init)(struct intel_memory_region *mem);
void (*release)(struct intel_memory_region *mem); void (*release)(struct intel_memory_region *mem);
struct drm_i915_gem_object * int (*init_object)(struct intel_memory_region *mem,
(*create_object)(struct intel_memory_region *mem, struct drm_i915_gem_object *obj,
resource_size_t size, resource_size_t size,
unsigned int flags); unsigned int flags);
}; };
struct intel_memory_region { struct intel_memory_region {
......
...@@ -15,21 +15,16 @@ static const struct drm_i915_gem_object_ops mock_region_obj_ops = { ...@@ -15,21 +15,16 @@ static const struct drm_i915_gem_object_ops mock_region_obj_ops = {
.release = i915_gem_object_release_memory_region, .release = i915_gem_object_release_memory_region,
}; };
static struct drm_i915_gem_object * static int mock_object_init(struct intel_memory_region *mem,
mock_object_create(struct intel_memory_region *mem, struct drm_i915_gem_object *obj,
resource_size_t size, resource_size_t size,
unsigned int flags) unsigned int flags)
{ {
static struct lock_class_key lock_class; static struct lock_class_key lock_class;
struct drm_i915_private *i915 = mem->i915; struct drm_i915_private *i915 = mem->i915;
struct drm_i915_gem_object *obj;
if (size > mem->mm.size) if (size > mem->mm.size)
return ERR_PTR(-E2BIG); return -E2BIG;
obj = i915_gem_object_alloc();
if (!obj)
return ERR_PTR(-ENOMEM);
drm_gem_private_object_init(&i915->drm, &obj->base, size); drm_gem_private_object_init(&i915->drm, &obj->base, size);
i915_gem_object_init(obj, &mock_region_obj_ops, &lock_class); i915_gem_object_init(obj, &mock_region_obj_ops, &lock_class);
...@@ -40,13 +35,13 @@ mock_object_create(struct intel_memory_region *mem, ...@@ -40,13 +35,13 @@ mock_object_create(struct intel_memory_region *mem,
i915_gem_object_init_memory_region(obj, mem, flags); i915_gem_object_init_memory_region(obj, mem, flags);
return obj; return 0;
} }
static const struct intel_memory_region_ops mock_region_ops = { static const struct intel_memory_region_ops mock_region_ops = {
.init = intel_memory_region_init_buddy, .init = intel_memory_region_init_buddy,
.release = intel_memory_region_release_buddy, .release = intel_memory_region_release_buddy,
.create_object = mock_object_create, .init_object = mock_object_init,
}; };
struct intel_memory_region * struct intel_memory_region *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment