public inbox for intel-gfx@lists.freedesktop.org
 help / color / mirror / Atom feed
From: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
To: Chris Wilson <chris@chris-wilson.co.uk>, intel-gfx@lists.freedesktop.org
Subject: Re: [PATCH 13/16] drm/i915: Add selftests for i915_gem_request
Date: Fri, 9 Dec 2016 15:51:12 +0000	[thread overview]
Message-ID: <0e080907-6caa-af59-efaf-e2f19f910318@linux.intel.com> (raw)
In-Reply-To: <20161207135833.32740-14-chris@chris-wilson.co.uk>


On 07/12/2016 13:58, Chris Wilson wrote:
> Simple starting point for adding seltests for i915_gem_request, first
> mock a device (with engines and contexts) that allows us to construct
> and execute a request, along with waiting for the request to complete.
>
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> ---
>  drivers/gpu/drm/i915/i915_gem_request.c    | 402 +++++++++++++++++++++++++++++
>  drivers/gpu/drm/i915/i915_mock_selftests.h |   1 +
>  2 files changed, 403 insertions(+)
>
> diff --git a/drivers/gpu/drm/i915/i915_gem_request.c b/drivers/gpu/drm/i915/i915_gem_request.c
> index 881bed5347fb..6553457adc77 100644
> --- a/drivers/gpu/drm/i915/i915_gem_request.c
> +++ b/drivers/gpu/drm/i915/i915_gem_request.c
> @@ -1208,3 +1208,405 @@ void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
>  	for_each_engine(engine, dev_priv, id)
>  		engine_retire_requests(engine);
>  }
> +
> +#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
> +#include "i915_selftest.h"
> +
> +struct mock_engine {
> +	struct intel_engine_cs base;
> +
> +	spinlock_t hw_lock;
> +	struct list_head hw_queue;
> +	struct timer_list hw_delay;
> +};
> +
> +struct mock_request {
> +	struct drm_i915_gem_request base;
> +
> +	struct list_head link;
> +	unsigned long delay;
> +};
> +
> +static void mock_seqno_advance(struct intel_engine_cs *engine, u32 seqno)
> +{
> +	intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
> +	intel_engine_wakeup(engine);
> +}
> +
> +static void hw_delay_complete(unsigned long data)
> +{
> +	struct mock_engine *engine = (typeof(engine))data;
> +	struct mock_request *request;
> +
> +	spin_lock(&engine->hw_lock);
> +	request = list_first_entry_or_null(&engine->hw_queue,
> +					   typeof(*request),
> +					   link);
> +	if (request) {
> +		list_del(&request->link);
> +		mock_seqno_advance(&engine->base, request->base.global_seqno);
> +	}
> +
> +	request = list_first_entry_or_null(&engine->hw_queue,
> +					   typeof(*request),
> +					   link);
> +	if (request)
> +		mod_timer(&engine->hw_delay, jiffies + request->delay);
> +	spin_unlock(&engine->hw_lock);
> +}
> +
> +static void mock_engine_flush(struct intel_engine_cs *engine)
> +{
> +	struct mock_engine *mock =
> +		container_of(engine, typeof(*mock), base);
> +	struct mock_request *request, *rn;
> +
> +	del_timer_sync(&mock->hw_delay);
> +
> +	spin_lock_irq(&mock->hw_lock);
> +	list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
> +		list_del_init(&request->link);
> +		mock_seqno_advance(&mock->base, request->base.global_seqno);
> +	}
> +	spin_unlock_irq(&mock->hw_lock);
> +}
> +
> +static int mock_context_pin(struct intel_engine_cs *engine,
> +			    struct i915_gem_context *ctx)
> +{
> +	i915_gem_context_get(ctx);
> +	return 0;
> +}
> +
> +static void mock_context_unpin(struct intel_engine_cs *engine,
> +			       struct i915_gem_context *ctx)
> +{
> +	i915_gem_context_put(ctx);
> +}
> +
> +static int mock_request_alloc(struct drm_i915_gem_request *request)
> +{
> +	request->ring = request->engine->buffer;
> +	return 0;
> +}
> +
> +static int mock_emit_flush(struct drm_i915_gem_request *request,
> +			   unsigned int flags)
> +{
> +	return 0;
> +}
> +
> +static void mock_emit_breadcrumb(struct drm_i915_gem_request *request,
> +				 u32 *flags)
> +{
> +}
> +
> +static void mock_submit_request(struct drm_i915_gem_request *request)
> +{
> +	struct mock_request *mock = container_of(request, typeof(*mock), base);
> +	struct mock_engine *engine =
> +		container_of(request->engine, typeof(*engine), base);
> +
> +	i915_gem_request_submit(request);
> +
> +	spin_lock_irq(&engine->hw_lock);
> +	list_add_tail(&mock->link, &engine->hw_queue);
> +	if (list_first_entry(&engine->hw_queue, typeof(*mock), link) == mock)
> +		mod_timer(&engine->hw_delay, jiffies + mock->delay);
> +	spin_unlock_irq(&engine->hw_lock);
> +}
> +
> +static struct drm_i915_gem_request *
> +mock_request(struct intel_engine_cs *engine,
> +	     struct i915_gem_context *context,
> +	     unsigned long delay)
> +{
> +	struct drm_i915_gem_request *request;
> +	struct mock_request *mock;
> +
> +	request = i915_gem_request_alloc(engine, context);
> +	if (!request)
> +		return NULL;
> +
> +	mock = container_of(request, typeof(*mock), base);
> +	INIT_LIST_HEAD(&mock->link);
> +	mock->delay = delay;

How about just:

mock = (struct mock_request *)i915_gem_request_alloc(...)

?

Both versions are equally confusing until the reader figures out the 
kmem cache size is correct. Doesn't matter really, just thinking out 
loud. :)

> +
> +	return &mock->base;
> +}
> +
> +static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
> +{
> +	struct intel_ring *ring;
> +
> +	ring = kzalloc(sizeof(*ring) + 4096, GFP_KERNEL);
> +	if (!ring)
> +		return NULL;
> +
> +	ring->engine = engine;
> +	ring->size = 4096;
> +	ring->effective_size = ring->size;
> +	ring->vaddr = (void *)(ring + 1);
> +
> +	INIT_LIST_HEAD(&ring->request_list);
> +	ring->last_retired_head = -1;
> +	intel_ring_update_space(ring);
> +
> +	return ring;
> +}
> +
> +static struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
> +					   const char *name)
> +{
> +	struct mock_engine *engine;
> +	static int id;
> +
> +	engine = kzalloc(sizeof(*engine) + 4096, GFP_KERNEL);
> +	if (!engine)
> +		return NULL;
> +
> +	engine->base.buffer = mock_ring(&engine->base);
> +	if (!engine->base.buffer) {
> +		kfree(engine);
> +		return NULL;
> +	}
> +
> +	/* minimal engine setup for requests */
> +	engine->base.i915 = i915;
> +	engine->base.name = name;
> +	engine->base.id = id++;
> +	engine->base.status_page.page_addr = (void *)(engine + 1);
> +
> +	engine->base.context_pin = mock_context_pin;
> +	engine->base.context_unpin = mock_context_unpin;
> +	engine->base.request_alloc = mock_request_alloc;
> +	engine->base.emit_flush = mock_emit_flush;
> +	engine->base.emit_breadcrumb = mock_emit_breadcrumb;
> +	engine->base.submit_request = mock_submit_request;
> +
> +	engine->base.timeline =
> +		&i915->gt.global_timeline.engine[engine->base.id];
> +
> +	/* minimal breadcrumbs init */
> +	spin_lock_init(&engine->base.breadcrumbs.lock);
> +	engine->base.breadcrumbs.mock = true;
> +
> +	/* fake hw queue */
> +	spin_lock_init(&engine->hw_lock);
> +	setup_timer(&engine->hw_delay,
> +		    hw_delay_complete,
> +		    (unsigned long)engine);
> +	INIT_LIST_HEAD(&engine->hw_queue);
> +
> +	return &engine->base;
> +}
> +
> +static void mock_engine_free(struct intel_engine_cs *engine)
> +{
> +	if (engine->last_context)
> +		engine->context_unpin(engine, engine->last_context);
> +
> +	kfree(engine->buffer);
> +	kfree(engine);
> +}
> +
> +static void mock_ppgtt_cleanup(struct i915_address_space *vm)
> +{
> +}
> +
> +static struct i915_hw_ppgtt *
> +mock_ppgtt(struct drm_i915_private *i915,
> +	   const char *name)
> +{
> +	struct i915_hw_ppgtt *ppgtt;
> +
> +	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
> +	if (!ppgtt)
> +		return NULL;
> +
> +	kref_init(&ppgtt->ref);
> +
> +	INIT_LIST_HEAD(&ppgtt->base.active_list);
> +	INIT_LIST_HEAD(&ppgtt->base.inactive_list);
> +	INIT_LIST_HEAD(&ppgtt->base.unbound_list);
> +
> +	INIT_LIST_HEAD(&ppgtt->base.global_link);
> +	drm_mm_init(&ppgtt->base.mm, 0, ~0);
> +	i915_gem_timeline_init(i915, &ppgtt->base.timeline, name);
> +
> +	ppgtt->base.cleanup = mock_ppgtt_cleanup;
> +
> +	return ppgtt;
> +}
> +
> +static struct i915_gem_context *
> +mock_context(struct drm_i915_private *i915,
> +	     const char *name)
> +{
> +	struct i915_gem_context *ctx;
> +	int ret;
> +
> +	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
> +	if (!ctx)
> +		return NULL;
> +
> +	kref_init(&ctx->ref);
> +	INIT_LIST_HEAD(&ctx->link);
> +	ctx->name = name ? kstrdup(name, GFP_KERNEL) : NULL;
> +	ctx->i915 = i915;
> +
> +	ret = ida_simple_get(&i915->context_hw_ida,
> +			     0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
> +	if (ret < 0) {
> +		kfree(ctx);
> +		return NULL;
> +	}
> +	ctx->hw_id = ret;
> +
> +	if (name) {
> +		ctx->ppgtt = mock_ppgtt(i915, name);
> +		if (!ctx->ppgtt) {
> +			kfree(ctx);
> +			return NULL;
> +		}
> +	}
> +
> +	return ctx;
> +}
> +
> +static void mock_retire_work_handler(struct work_struct *work)
> +{
> +}
> +
> +static void mock_idle_work_handler(struct work_struct *work)
> +{
> +}
> +
> +static struct drm_i915_private *mock_device(void)
> +{
> +	struct drm_i915_private *i915;
> +
> +	i915 = kzalloc(sizeof(*i915), GFP_KERNEL);
> +	if (!i915)
> +		return NULL;
> +
> +	mutex_init(&i915->drm.struct_mutex);
> +
> +	init_waitqueue_head(&i915->gpu_error.wait_queue);
> +	init_waitqueue_head(&i915->gpu_error.reset_queue);
> +
> +	ida_init(&i915->context_hw_ida);
> +
> +	INIT_DELAYED_WORK(&i915->gt.retire_work, mock_retire_work_handler);
> +	INIT_DELAYED_WORK(&i915->gt.idle_work, mock_idle_work_handler);
> +
> +	INIT_LIST_HEAD(&i915->gt.timelines);
> +	i915->gt.awake = true;
> +
> +	mutex_lock(&i915->drm.struct_mutex);
> +	i915_gem_timeline_init__global(i915);
> +	i915_gem_timeline_init(i915, &i915->ggtt.base.timeline, "mock");
> +	mutex_unlock(&i915->drm.struct_mutex);
> +
> +	i915->requests = KMEM_CACHE(mock_request,
> +				    SLAB_HWCACHE_ALIGN |
> +				    SLAB_RECLAIM_ACCOUNT |
> +				    SLAB_DESTROY_BY_RCU);
> +	if (!i915->requests)
> +		goto err_device;
> +
> +	i915->dependencies = KMEM_CACHE(i915_dependency,
> +					SLAB_HWCACHE_ALIGN |
> +					SLAB_RECLAIM_ACCOUNT);
> +	if (!i915->dependencies)
> +		goto err_requests;
> +
> +	mkwrite_device_info(i915)->ring_mask = BIT(0);
> +	i915->engine[RCS] = mock_engine(i915, "mock");
> +	if (!i915->engine[RCS])
> +		goto err_dependencies;
> +
> +	i915->kernel_context = mock_context(i915, NULL);
> +	if (!i915->kernel_context)
> +		goto err_engine;
> +
> +	return i915;
> +
> +err_engine:
> +	mock_engine_free(i915->engine[RCS]);
> +err_dependencies:
> +	kmem_cache_destroy(i915->dependencies);
> +err_requests:
> +	kmem_cache_destroy(i915->requests);
> +err_device:
> +	kfree(i915);
> +	return NULL;
> +}
> +
> +static void mock_device_free(struct drm_i915_private *i915)
> +{
> +	struct intel_engine_cs *engine;
> +	enum intel_engine_id id;
> +
> +	for_each_engine(engine, i915, id)
> +		mock_engine_flush(engine);
> +
> +	mutex_lock(&i915->drm.struct_mutex);
> +	i915_gem_retire_requests(i915);
> +	mutex_unlock(&i915->drm.struct_mutex);
> +
> +	cancel_delayed_work_sync(&i915->gt.retire_work);
> +	cancel_delayed_work_sync(&i915->gt.idle_work);
> +
> +	mutex_lock(&i915->drm.struct_mutex);
> +	for_each_engine(engine, i915, id)
> +		mock_engine_free(engine);
> +
> +	i915_gem_context_fini(i915);
> +
> +	i915_gem_timeline_fini(&i915->ggtt.base.timeline);
> +	i915_gem_timeline_fini(&i915->gt.global_timeline);
> +	mutex_unlock(&i915->drm.struct_mutex);
> +
> +	kmem_cache_destroy(i915->dependencies);
> +	kmem_cache_destroy(i915->requests);
> +
> +	kfree(i915);
> +}
> +
> +static int igt_add_request(void *ignore)
> +{
> +	struct drm_i915_private *i915;
> +	struct drm_i915_gem_request *request;
> +	int err = -ENOMEM;
> +
> +	i915 = mock_device();
> +	if (!i915)
> +		goto out;
> +
> +	mutex_lock(&i915->drm.struct_mutex);
> +	request = mock_request(i915->engine[RCS],
> +			       i915->kernel_context,
> +			       HZ / 10);
> +	if (!request)
> +		goto out_unlock;
> +
> +	i915_add_request(request);
> +
> +	err = 0;
> +out_unlock:
> +	mutex_unlock(&i915->drm.struct_mutex);
> +	mock_device_free(i915);
> +out:
> +	return err;
> +}
> +
> +int i915_gem_request_selftest(void)
> +{
> +	static const struct i915_subtest tests[] = {
> +		SUBTEST(igt_add_request),
> +	};
> +
> +	return i915_subtests(tests, NULL);
> +}
> +#endif
> diff --git a/drivers/gpu/drm/i915/i915_mock_selftests.h b/drivers/gpu/drm/i915/i915_mock_selftests.h
> index 1603fd35d190..9ff379b18c20 100644
> --- a/drivers/gpu/drm/i915/i915_mock_selftests.h
> +++ b/drivers/gpu/drm/i915/i915_mock_selftests.h
> @@ -8,5 +8,6 @@
>   *
>   * Tests are executed in reverse order by igt/drv_selftest
>   */
> +selftest(requests, i915_gem_request_selftest)
>  selftest(breadcrumbs, intel_breadcrumbs_selftest)
>  selftest(sanitycheck, i915_mock_sanitycheck) /* keep last */
>

It looks tidy enough. I trust you'll run it through the full memory 
debugging arsenal just to double-check it doesn't leak anything. But it 
looks OK to me.

Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Regards,

Tvrtko
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

  reply	other threads:[~2016-12-09 15:51 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-07 13:58 [RFC] Smattering of selftests Chris Wilson
2016-12-07 13:58 ` [PATCH 01/16] drm/i915: Provide a hook for selftests Chris Wilson
2016-12-08 10:47   ` Tvrtko Ursulin
2016-12-08 11:15     ` Chris Wilson
2016-12-08 12:30       ` Tvrtko Ursulin
2016-12-08 12:40         ` Chris Wilson
2016-12-07 13:58 ` [PATCH 02/16] kselftests: Exercise hw-independent mock tests for i915.ko Chris Wilson
2016-12-07 14:09   ` Chris Wilson
2016-12-08 15:50     ` Shuah Khan
2016-12-08 16:01       ` Chris Wilson
2016-12-08 16:44         ` Shuah Khan
2016-12-07 13:58 ` [PATCH 03/16] drm/i915: Add unit tests for the breadcrumb rbtree, insert/remove Chris Wilson
2016-12-08 11:00   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 04/16] drm/i915: Add unit tests for the breadcrumb rbtree, completion Chris Wilson
2016-12-08 11:47   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 05/16] drm/i915: Add unit tests for the breadcrumb rbtree, wakeups Chris Wilson
2016-12-08 17:38   ` Tvrtko Ursulin
2016-12-08 21:04     ` Chris Wilson
2016-12-09  9:33       ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 06/16] drm/i915: Add a reminder that i915_vma_move_to_active() requires struct_mutex Chris Wilson
2016-12-08 17:40   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 07/16] drm/i915: Move intel_lrc_context_pin() to avoid the forward declaration Chris Wilson
2016-12-08 17:45   ` Tvrtko Ursulin
2016-12-08 20:55     ` Chris Wilson
2016-12-07 13:58 ` [PATCH 08/16] drm/i915: Unify active context tracking between legacy/execlists/guc Chris Wilson
2016-12-09 11:48   ` Tvrtko Ursulin
2016-12-09 12:17     ` Chris Wilson
2016-12-07 13:58 ` [PATCH 09/16] drm/i915: Simplify releasing context reference Chris Wilson
2016-12-09 15:03   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 10/16] drm/i915: Mark the shadow gvt context as closed Chris Wilson
2016-12-09 15:07   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 11/16] drm/i915/execlists: Request the kernel context be pinned high Chris Wilson
2016-12-09 15:08   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 12/16] drm/i915: Swap if(enable_execlists) in i915_gem_request_alloc for a vfunc Chris Wilson
2016-12-09 15:16   ` Tvrtko Ursulin
2016-12-09 15:25     ` Chris Wilson
2016-12-09 15:53       ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 13/16] drm/i915: Add selftests for i915_gem_request Chris Wilson
2016-12-09 15:51   ` Tvrtko Ursulin [this message]
2016-12-07 13:58 ` [PATCH 14/16] drm/i915: Add a simple request selftest for waiting Chris Wilson
2016-12-09 15:59   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 15/16] drm/i915: Add a simple fence selftest to i915_gem_request Chris Wilson
2016-12-09 16:02   ` Tvrtko Ursulin
2016-12-07 13:58 ` [PATCH 16/16] drm/i915: Add selftests for object allocation, phys Chris Wilson
2016-12-13 17:10   ` Tvrtko Ursulin
2016-12-17 13:55     ` Chris Wilson
2016-12-07 14:04 ` [RFC] Smattering of selftests Chris Wilson
2016-12-07 15:45 ` ✓ Fi.CI.BAT: success for series starting with [01/16] drm/i915: Provide a hook for selftests Patchwork
2016-12-07 18:52 ` [PATCH 1/2] drm/i915: Move uncore selfchecks to late selftest infrastructure Chris Wilson
2016-12-07 18:52   ` [PATCH 2/2] drm/i915: Test all fw tables during mock selftests Chris Wilson
2016-12-08 12:14     ` Tvrtko Ursulin
2016-12-08 12:28       ` Chris Wilson
2016-12-08 13:11     ` Joonas Lahtinen
2016-12-08 16:52     ` Tvrtko Ursulin
2016-12-08 22:29       ` Chris Wilson
2016-12-09  9:41         ` Tvrtko Ursulin
2016-12-09 10:18           ` Chris Wilson
2016-12-09 10:35             ` Tvrtko Ursulin
2016-12-09 10:51               ` Chris Wilson
2016-12-08 11:58   ` [PATCH 1/2] drm/i915: Move uncore selfchecks to late selftest infrastructure Tvrtko Ursulin
2016-12-07 19:56 ` [RFC] Smattering of selftests Paulo Zanoni
2016-12-07 20:52 ` ✓ Fi.CI.BAT: success for series starting with [01/16] drm/i915: Provide a hook for selftests (rev2) Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0e080907-6caa-af59-efaf-e2f19f910318@linux.intel.com \
    --to=tvrtko.ursulin@linux.intel.com \
    --cc=chris@chris-wilson.co.uk \
    --cc=intel-gfx@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox