diff mbox

igt/gem_create_stolen: Verifying extended gem_create ioctl

Message ID 1430907712-5132-1-git-send-email-ankitprasad.r.sharma@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

ankitprasad.r.sharma@intel.com May 6, 2015, 10:21 a.m. UTC
From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>

This patch adds the testcases for verifying the new extended
gem_create ioctl. By means of this extended ioctl, memory
placement of the GEM object can be specified, i.e. either
shmem or stolen memory.
These testcases include functional tests and interface tests for
testing the gem_create ioctl call for stolen memory placement

v2: Testing pread/pwrite functionality for stolen backed objects,
added local struct for extended gem_create and gem_get_aperture,
until headers catch up (Chris)

v3: Removed get_aperture related functions, extended gem_pread
to compare speeds for user pages with and without page faults,
unexposed local_gem_create struct, changed gem_create_stolen
usage (Chris)

Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
---
 lib/ioctl_wrappers.c      |  60 +++++++
 lib/ioctl_wrappers.h      |  12 ++
 tests/Makefile.sources    |   1 +
 tests/gem_create_stolen.c | 387 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/gem_pread.c         | 100 ++++++++++++
 tests/gem_pwrite.c        |  45 ++++++
 6 files changed, 605 insertions(+)
 create mode 100644 tests/gem_create_stolen.c

Comments

Daniel Vetter May 7, 2015, 6:52 a.m. UTC | #1
On Wed, May 06, 2015 at 03:51:52PM +0530, ankitprasad.r.sharma@intel.com wrote:
> From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> 
> This patch adds the testcases for verifying the new extended
> gem_create ioctl. By means of this extended ioctl, memory
> placement of the GEM object can be specified, i.e. either
> shmem or stolen memory.
> These testcases include functional tests and interface tests for
> testing the gem_create ioctl call for stolen memory placement
> 
> v2: Testing pread/pwrite functionality for stolen backed objects,
> added local struct for extended gem_create and gem_get_aperture,
> until headers catch up (Chris)
> 
> v3: Removed get_aperture related functions, extended gem_pread
> to compare speeds for user pages with and without page faults,
> unexposed local_gem_create struct, changed gem_create_stolen
> usage (Chris)
> 
> Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>

An igt to check for invalid arguments of the gem create ioctl (especially
the newly added flags parameters) seems to be missing.
-Daniel

> ---
>  lib/ioctl_wrappers.c      |  60 +++++++
>  lib/ioctl_wrappers.h      |  12 ++
>  tests/Makefile.sources    |   1 +
>  tests/gem_create_stolen.c | 387 ++++++++++++++++++++++++++++++++++++++++++++++
>  tests/gem_pread.c         | 100 ++++++++++++
>  tests/gem_pwrite.c        |  45 ++++++
>  6 files changed, 605 insertions(+)
>  create mode 100644 tests/gem_create_stolen.c
> 
> diff --git a/lib/ioctl_wrappers.c b/lib/ioctl_wrappers.c
> index ff78ef1..2bd341a 100644
> --- a/lib/ioctl_wrappers.c
> +++ b/lib/ioctl_wrappers.c
> @@ -379,6 +379,66 @@ void gem_sync(int fd, uint32_t handle)
>  	gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
>  }
>  
> +bool gem_create__has_stolen_support(int fd)
> +{
> +	static int has_stolen_support = -1;
> +	struct drm_i915_getparam gp;
> +	int val = -1;
> +
> +	return 1;
> +	if (has_stolen_support < 0) {
> +		memset(&gp, 0, sizeof(gp));
> +		gp.param = 35; /* CREATE_VERSION */
> +		gp.value = &val;
> +
> +		/* Do we have the extended gem_create_ioctl? */
> +		ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
> +		has_stolen_support = val >= 1;
> +	}
> +
> +	return has_stolen_support;
> +}
> +
> +struct local_i915_gem_create_v2 {
> +	uint64_t size;
> +	uint32_t handle;
> +	uint32_t pad;
> +#define I915_CREATE_PLACEMENT_STOLEN (1<<0)
> +	uint32_t flags;
> +};
> +
> +#define LOCAL_IOCTL_I915_GEM_CREATE       DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct local_i915_gem_create_v2)
> +/**
> + * gem_create_stolen:
> + * @fd: open i915 drm file descriptor
> + * @size: desired size of the buffer
> + * @flags: desired placement i.e. stolen or shmem
> + *
> + * This wraps the new GEM_CREATE ioctl, which allocates a
> + * new gem buffer object of @size and placement based on @flags.
> + *
> + * Returns: The file-private handle of the created buffer object
> + */
> +
> +uint32_t gem_create_stolen(int fd, int size)
> +{
> +	struct local_i915_gem_create_v2 create;
> +	int ret;
> +
> +	memset(&create, 0, sizeof(create));
> +	create.handle = 0;
> +	create.size = size;
> +	create.flags = I915_CREATE_PLACEMENT_STOLEN;
> +	ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
> +
> +	if (ret < 0)
> +		return 0;
> +
> +	errno = 0;
> +	return create.handle;
> +}
> +
> +
>  uint32_t __gem_create(int fd, int size)
>  {
>  	struct drm_i915_gem_create create;
> diff --git a/lib/ioctl_wrappers.h b/lib/ioctl_wrappers.h
> index ced7ef3..cac1c11 100644
> --- a/lib/ioctl_wrappers.h
> +++ b/lib/ioctl_wrappers.h
> @@ -56,6 +56,9 @@ void gem_read(int fd, uint32_t handle, uint32_t offset, void *buf, uint32_t leng
>  void gem_set_domain(int fd, uint32_t handle,
>  		    uint32_t read_domains, uint32_t write_domain);
>  void gem_sync(int fd, uint32_t handle);
> +
> +bool gem_create__has_stolen_support(int fd);
> +uint32_t gem_create_stolen(int fd, int size);
>  uint32_t __gem_create(int fd, int size);
>  uint32_t gem_create(int fd, int size);
>  void gem_execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
> @@ -67,6 +70,15 @@ bool gem_mmap__has_wc(int fd);
>  void *gem_mmap__wc(int fd, uint32_t handle, int offset, int size, int prot);
>  
>  /**
> + * gem_require_stolen_support:
> + * @fd: open i915 drm file descriptor
> + *
> + * Test macro to query whether support for allocating objects from stolen
> + * memory is available. Automatically skips through igt_require() if not.
> + */
> +#define gem_require_stolen_support(fd) \
> +			igt_require(gem_create__has_stolen_support(fd))
> +/**
>   * gem_require_mmap_wc:
>   * @fd: open i915 drm file descriptor
>   *
> diff --git a/tests/Makefile.sources b/tests/Makefile.sources
> index a165978..9b2ee90 100644
> --- a/tests/Makefile.sources
> +++ b/tests/Makefile.sources
> @@ -23,6 +23,7 @@ TESTS_progs_M = \
>  	gem_caching \
>  	gem_close_race \
>  	gem_concurrent_blit \
> +	gem_create_stolen \
>  	gem_cs_tlb \
>  	gem_ctx_param_basic \
>  	gem_ctx_bad_exec \
> diff --git a/tests/gem_create_stolen.c b/tests/gem_create_stolen.c
> new file mode 100644
> index 0000000..402d42b
> --- /dev/null
> +++ b/tests/gem_create_stolen.c
> @@ -0,0 +1,387 @@
> +/*
> + * Copyright © 2011 Intel Corporation
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + *
> + * Authors:
> + *    Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> + *
> + */
> +
> +/** @file gem_create_stolen.c
> + *
> + * This is a test for the extended gem_create ioctl, that includes allocation
> + * of object from stolen memory
> + *
> + * The goal is to simply ensure the basics work, and invalid input combinations
> + * are rejected.
> + */
> +
> +#include <stdlib.h>
> +#include <sys/ioctl.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <fcntl.h>
> +#include <inttypes.h>
> +#include <errno.h>
> +#include <sys/stat.h>
> +#include <sys/time.h>
> +#include <getopt.h>
> +
> +#include <drm.h>
> +
> +#include "ioctl_wrappers.h"
> +#include "intel_bufmgr.h"
> +#include "intel_batchbuffer.h"
> +#include "intel_io.h"
> +#include "intel_chipset.h"
> +#include "igt_aux.h"
> +#include "drmtest.h"
> +#include "drm.h"
> +#include "i915_drm.h"
> +
> +#define CLEAR(s) memset(&s, 0, sizeof(s))
> +#define SIZE 1024*1024
> +
> +static struct drm_intel_bufmgr *bufmgr;
> +static struct intel_batchbuffer *batch;
> +
> +static void verify_copy_op(drm_intel_bo *src, drm_intel_bo *dest)
> +{
> +	uint32_t *virt, i, ret;
> +	/* Fill the src BO with dwords */
> +	ret = drm_intel_gem_bo_map_gtt(src);
> +	igt_assert(!ret);
> +
> +	virt = src->virtual;
> +	for (i = 0; i < SIZE/4; i++)
> +		virt[i] = i;
> +
> +	intel_copy_bo(batch, dest, src, SIZE);
> +
> +	ret = drm_intel_gem_bo_map_gtt(dest);
> +	igt_assert(!ret);
> +
> +	virt = dest->virtual;
> +	/* verify */
> +	for (i = 0; i < SIZE/4; i++) {
> +		if (virt[i] != i) {
> +			fprintf(stderr, "Expected 0x%08x, found 0x%08x "
> +					"at offset 0x%08x\n",
> +					i, virt[i], i * 4);
> +			igt_assert(virt[i] == i);
> +		}
> +	}
> +
> +	drm_intel_bo_unmap(src);
> +	drm_intel_bo_unmap(dest);
> +}
> +
> +static void stolen_pwrite(int fd)
> +{
> +	drm_intel_bo *bo;
> +	uint32_t buf[SIZE/4];
> +	uint32_t handle = 0;
> +	uint32_t *virt;
> +	int i, ret = 0;
> +
> +	for (i = 0; i < SIZE/4; i ++)
> +		buf[i] = 0xdead;
> +
> +	gem_require_stolen_support(fd);
> +
> +	handle = gem_create_stolen(fd, SIZE);
> +	igt_assert(handle != 0);
> +
> +	gem_write(fd, handle, 0, buf, SIZE);
> +	bo = gem_handle_to_libdrm_bo(bufmgr, fd, "bo", handle);
> +
> +	ret = drm_intel_gem_bo_map_gtt(bo);
> +	igt_assert(!ret);
> +
> +	virt = bo->virtual;
> +
> +	for (i = 0; i < SIZE/4; i ++)
> +		igt_assert(virt[i] == 0xdead);
> +
> +	drm_intel_bo_unmap(bo);
> +	drm_intel_bo_unreference(bo);
> +	gem_close(fd, handle);
> +}
> +
> +static void stolen_pread(int fd)
> +{
> +	drm_intel_bo *bo;
> +	uint32_t buf[SIZE/4];
> +	uint32_t handle = 0;
> +	uint32_t *virt;
> +	int i, ret = 0;
> +
> +	CLEAR(buf);
> +
> +	gem_require_stolen_support(fd);
> +
> +	handle = gem_create_stolen(fd, SIZE);
> +	igt_assert(handle != 0);
> +
> +	bo = gem_handle_to_libdrm_bo(bufmgr, fd, "bo", handle);
> +
> +	ret = drm_intel_gem_bo_map_gtt(bo);
> +	igt_assert(!ret);
> +
> +	virt = bo->virtual;
> +
> +	for (i = 0; i < SIZE/4; i ++)
> +		virt[i] = 0xdead;
> +
> +	drm_intel_bo_unmap(bo);
> +	drm_intel_bo_unreference(bo);
> +
> +	gem_read(fd, handle, 0, buf, SIZE);
> +
> +	for (i = 0; i < SIZE/4; i ++)
> +		igt_assert(buf[i] == 0xdead);
> +
> +	gem_close(fd, handle);
> +}
> +
> +static void copy_test(int fd)
> +{
> +	drm_intel_bo *src, *dest;
> +	uint32_t src_handle = 0, dest_handle = 0;
> +	int ret = 0;
> +
> +	gem_require_stolen_support(fd);
> +
> +	src_handle = gem_create_stolen(fd, SIZE);
> +	igt_assert(src_handle != 0);
> +
> +	dest_handle = gem_create_stolen(fd, SIZE);
> +	igt_assert(dest_handle != 0);
> +
> +	src = gem_handle_to_libdrm_bo(bufmgr, fd, "src_bo", src_handle);
> +	dest = gem_handle_to_libdrm_bo(bufmgr, fd, "dst_bo", dest_handle);
> +
> +	igt_assert(src != NULL);
> +	igt_assert(dest != NULL);
> +
> +	verify_copy_op(src, dest);
> +
> +	drm_intel_bo_unreference(src);
> +	drm_intel_bo_unreference(dest);
> +	gem_close(fd, src_handle);
> +	gem_close(fd, dest_handle);
> +}
> +
> +static void verify_object_clear(int fd)
> +{
> +	drm_intel_bo *bo;
> +	uint32_t handle = 0;
> +	uint32_t *virt;
> +	int i, ret = 0;
> +
> +	gem_require_stolen_support(fd);
> +
> +	handle = gem_create_stolen(fd, SIZE);
> +	igt_assert(handle != 0);
> +
> +	bo = gem_handle_to_libdrm_bo(bufmgr, fd, "verify_bo", handle);
> +	igt_assert(bo != NULL);
> +
> +	ret = drm_intel_gem_bo_map_gtt(bo);
> +	igt_assert(ret == 0);
> +
> +	/* Verify if the BO is zeroed */
> +	virt = bo->virtual;
> +	for (i = 0; i < SIZE / 4; i++)
> +		igt_assert(virt[i] == 0);
> +
> +	drm_intel_bo_unmap(bo);
> +	drm_intel_bo_unreference(bo);
> +	gem_close(fd, handle);
> +}
> +
> +static void stolen_fill_purge_test(int fd)
> +{
> +	drm_intel_bo *bo;
> +	int mret = 0, ret = 0, obj_count = 0, i = 0;
> +	int _ret = 0, j = 0;
> +	uint32_t handle[100] = {0};
> +	uint32_t new_handle = 0;
> +	uint32_t *virt;
> +	int retained = 0;
> +
> +	gem_require_stolen_support(fd);
> +
> +	/* Exhaust Stolen space */
> +	while(ret == 0) {
> +		ret = -1;
> +		handle[i] = gem_create_stolen(fd, SIZE);
> +		if (handle[i] != 0) {
> +			ret = 0;
> +			bo = gem_handle_to_libdrm_bo(bufmgr, fd,
> +						     "verify_bo", handle[i]);
> +			igt_assert(bo != NULL);
> +
> +			_ret = drm_intel_gem_bo_map_gtt(bo);
> +			igt_assert(_ret == 0);
> +
> +			virt = bo->virtual;
> +			for (j = 0; j < SIZE / 4; j++)
> +				virt[j] = 0xab;
> +
> +			drm_intel_bo_unmap(bo);
> +			drm_intel_bo_unreference(bo);
> +
> +			obj_count++;
> +		}
> +
> +		i++;
> +	}
> +
> +	igt_assert(obj_count > 0);
> +
> +	/* Mark all stolen objects purgeable */
> +	for (i = 0; i < obj_count; i++)
> +		retained = gem_madvise(fd, handle[i], I915_MADV_DONTNEED);
> +
> +	/* Try to allocate one more object */
> +	new_handle = gem_create_stolen(fd, SIZE);
> +	igt_assert(new_handle != 0);
> +
> +	/* Check if the retained object's memory contents are intact */
> +	for (i = 0; i < obj_count; i++) {
> +		retained = gem_madvise(fd, handle[i], I915_MADV_WILLNEED);
> +		if (retained) {
> +			bo = gem_handle_to_libdrm_bo(bufmgr, fd,
> +						     "verify_bo", handle[i]);
> +			igt_assert(bo != NULL);
> +
> +			_ret = drm_intel_gem_bo_map_gtt(bo);
> +			igt_assert(_ret == 0);
> +
> +			virt = bo->virtual;
> +			for (j = 0; j < SIZE / 4; j++)
> +				igt_assert(virt[j] == 0xab);
> +
> +			drm_intel_bo_unmap(bo);
> +			drm_intel_bo_unreference(bo);
> +		}
> +	}
> +
> +	gem_close(fd, new_handle);
> +	for (i = 0; i < obj_count; i++)
> +		gem_close(fd, handle[i]);
> +}
> +
> +static void
> +stolen_no_mmap(int fd)
> +{
> +	void *addr;
> +	uint32_t handle = 0;
> +
> +	gem_require_stolen_support(fd);
> +
> +	handle = gem_create_stolen(fd, SIZE);
> +	igt_assert(handle != 0);
> +
> +	addr = gem_mmap__cpu(fd, handle, 0, SIZE, PROT_READ | PROT_WRITE);
> +	igt_assert(addr == NULL);
> +
> +	gem_close(fd, handle);
> +}
> +
> +static void invalid_flag_test(int fd)
> +{
> +	int handle = 0;
> +
> +struct local_i915_gem_create_v2 {
> +	uint64_t size;
> +	uint32_t handle;
> +	uint32_t pad;
> +#define I915_CREATE_PLACEMENT_STOLEN (1<<0)
> +	uint32_t flags;
> +} create;
> +
> +#define LOCAL_IOCTL_I915_GEM_CREATE       DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct local_i915_gem_create_v2)
> +	gem_require_stolen_support(fd);
> +
> +	create.handle = 0;
> +	create.size = SIZE;
> +	create.flags = ~I915_CREATE_PLACEMENT_STOLEN;
> +	handle = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
> +
> +	igt_assert(handle <= 0);
> +}
> +
> +igt_main
> +{
> +	int i, fd, gtt_size_total, gtt_size_mappable;
> +	uint32_t devid;
> +
> +	igt_skip_on_simulation();
> +
> +	igt_fixture {
> +		fd = drm_open_any();
> +		devid = intel_get_drm_devid(fd);
> +
> +		drm_intel_get_aperture_sizes(fd, (size_t*)&gtt_size_total,
> +				(size_t*)&gtt_size_mappable);
> +		bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
> +		batch = intel_batchbuffer_alloc(bufmgr, devid);
> +	}
> +
> +	igt_subtest("stolen-invalid-flag")
> +		invalid_flag_test(fd);
> +
> +	igt_subtest("stolen-clear")
> +		verify_object_clear(fd);
> +
> +	/*
> +	 * stolen mem special cases - checking for non cpu mappable
> +	 */
> +	igt_subtest("stolen-no-mmap")
> +		stolen_no_mmap(fd);
> +
> +	/* checking for pread/pwrite interfaces */
> +	igt_subtest("stolen-pwrite")
> +		stolen_pwrite(fd);
> +
> +	igt_subtest("stolen-pread")
> +		stolen_pread(fd);
> +
> +	/* Functional Test - blt copy */
> +	igt_subtest("stolen-copy")
> +		copy_test(fd);
> +
> +	/* Filling stolen completely and marking all the objects
> +	 * purgeable. Then trying to add one more object, to verify
> +	 * the purging logic.
> +	 * Again marking all objects WILLNEED and verifying the
> +	 * contents of the retained objects.
> +	 */
> +	igt_subtest("stolen-fill-purge")
> +		stolen_fill_purge_test(fd);
> +
> +	igt_fixture {
> +		intel_batchbuffer_free(batch);
> +		drm_intel_bufmgr_destroy(bufmgr);
> +	}
> +}
> diff --git a/tests/gem_pread.c b/tests/gem_pread.c
> index cc83948..95162d5 100644
> --- a/tests/gem_pread.c
> +++ b/tests/gem_pread.c
> @@ -41,6 +41,10 @@
>  #include "drmtest.h"
>  
>  #define OBJECT_SIZE 16384
> +#define LARGE_OBJECT_SIZE 1024 * 1024
> +#define KGRN "\x1B[32m"
> +#define KRED "\x1B[31m"
> +#define KNRM "\x1B[0m"
>  
>  static void do_gem_read(int fd, uint32_t handle, void *buf, int len, int loops)
>  {
> @@ -76,11 +80,14 @@ static const char *bytes_per_sec(char *buf, double v)
>  
>  
>  uint32_t *src, dst;
> +uint32_t *dst_user, src_stolen, large_stolen;
> +uint32_t *stolen_pf_user, *stolen_nopf_user;
>  int fd, count;
>  
>  int main(int argc, char **argv)
>  {
>  	int object_size = 0;
> +	int large_obj_size = LARGE_OBJECT_SIZE;
>  	uint32_t buf[20];
>  	const struct {
>  		int level;
> @@ -90,6 +97,9 @@ int main(int argc, char **argv)
>  		{ 1, "snoop" },
>  		{ 2, "display" },
>  		{ -1 },
> +		{ -1, "stolen-uncached"},
> +		{ -1, "stolen-snoop"},
> +		{ -1, "stolen-display"},
>  	}, *c;
>  
>  	igt_subtest_init(argc, argv);
> @@ -106,6 +116,8 @@ int main(int argc, char **argv)
>  
>  		dst = gem_create(fd, object_size);
>  		src = malloc(object_size);
> +		src_stolen = gem_create_stolen(fd, object_size);
> +		dst_user = malloc(object_size);
>  	}
>  
>  	igt_subtest("normal") {
> @@ -142,9 +154,97 @@ int main(int argc, char **argv)
>  		}
>  	}
>  
> +	igt_subtest("stolen-normal") {
> +		for (count = 1; count <= 1<<17; count <<= 1) {
> +			struct timeval start, end;
> +
> +			gettimeofday(&start, NULL);
> +			do_gem_read(fd, src_stolen, dst_user, object_size, count);
> +			gettimeofday(&end, NULL);
> +			igt_info("Time to pread %d bytes x %6d:	%7.3fµs, %s\n",
> +				 object_size, count,
> +				 elapsed(&start, &end, count),
> +				 bytes_per_sec((char *)buf,
> +				 object_size/elapsed(&start, &end, count)*1e6));
> +			fflush(stdout);
> +		}
> +	}
> +	for (c = cache; c->level != -1; c++) {
> +		igt_subtest((c + 4)->name) {
> +			gem_set_caching(fd, src_stolen, c->level);
> +
> +			for (count = 1; count <= 1<<17; count <<= 1) {
> +				struct timeval start, end;
> +
> +				gettimeofday(&start, NULL);
> +				do_gem_read(fd, src_stolen, dst_user,
> +					    object_size, count);
> +				gettimeofday(&end, NULL);
> +				igt_info("Time to %s pread %d bytes x %6d:      %7.3fµs, %s\n",
> +					 (c + 4)->name, object_size, count,
> +					 elapsed(&start, &end, count),
> +					 bytes_per_sec((char *)buf,
> +					 object_size/elapsed(&start, &end, count)*1e6));
> +				fflush(stdout);
> +			}
> +		}
> +	}
> +
> +	/* List pread times for stolen area with 1 page fault (page fault only
> +	 * first time) and multiple page faults (unmapping the pages at the
> +	 * end of each iteration)
> +	 */
> +	igt_subtest("pagefault-pread") {
> +		large_stolen = gem_create_stolen(fd, large_obj_size);
> +		stolen_nopf_user = (uint32_t *) mmap(NULL, large_obj_size,
> +						PROT_WRITE,
> +						MAP_ANONYMOUS|MAP_PRIVATE,
> +						-1, 0);
> +
> +		for (count = 1; count <= 10; count ++) {
> +			struct timeval start, end;
> +			uint32_t t_elapsed = 0;
> +
> +			gettimeofday(&start, NULL);
> +			do_gem_read(fd, large_stolen, stolen_nopf_user,
> +				    large_obj_size, 1);
> +			gettimeofday(&end, NULL);
> +			t_elapsed = elapsed(&start, &end, 1);
> +			igt_info("Pagefault-N - Time to pread %d bytes: %7.3fµs, %s\n",
> +				 large_obj_size,
> +				 elapsed(&start, &end, 1),
> +				 bytes_per_sec((char *)buf,
> +				 large_obj_size/elapsed(&start, &end, 1)*1e6));
> +
> +			stolen_pf_user = (uint32_t *) mmap(NULL, large_obj_size,
> +						      PROT_WRITE,
> +						      MAP_ANONYMOUS|MAP_PRIVATE,
> +						      -1, 0);
> +
> +			gettimeofday(&start, NULL);
> +			do_gem_read(fd, large_stolen, stolen_pf_user,
> +				    large_obj_size, 1);
> +			gettimeofday(&end, NULL);
> +			igt_info("Pagefault-Y - Time to pread %d bytes: %7.3fµs, %s%s%s\n",
> +				 large_obj_size,
> +				 elapsed(&start, &end, 1),
> +				 t_elapsed < elapsed(&start, &end, 1) ? KGRN : KRED,
> +				 bytes_per_sec((char *)buf,
> +				 large_obj_size/elapsed(&start, &end, 1)*1e6),
> +				 KNRM);
> +			fflush(stdout);
> +			munmap(stolen_pf_user, large_obj_size);
> +		}
> +			munmap(stolen_nopf_user, large_obj_size);
> +			gem_close(fd, large_stolen);
> +	}
> +
> +
>  	igt_fixture {
>  		free(src);
>  		gem_close(fd, dst);
> +		free(dst_user);
> +		gem_close(fd, src_stolen);
>  
>  		close(fd);
>  	}
> diff --git a/tests/gem_pwrite.c b/tests/gem_pwrite.c
> index 6378b0a..dd1ca95 100644
> --- a/tests/gem_pwrite.c
> +++ b/tests/gem_pwrite.c
> @@ -82,6 +82,7 @@ static const char *bytes_per_sec(char *buf, double v)
>  
>  
>  uint32_t *src, dst;
> +uint32_t *src_user, dst_stolen;
>  int fd;
>  
>  int main(int argc, char **argv)
> @@ -97,6 +98,9 @@ int main(int argc, char **argv)
>  		{ 1, "snoop" },
>  		{ 2, "display" },
>  		{ -1 },
> +		{ -1, "stolen-uncached"},
> +		{ -1, "stolen-snoop"},
> +		{ -1, "stolen-display"},
>  	}, *c;
>  
>  	igt_skip_on_simulation();
> @@ -114,6 +118,8 @@ int main(int argc, char **argv)
>  
>  		dst = gem_create(fd, object_size);
>  		src = malloc(object_size);
> +		dst_stolen = gem_create_stolen(fd, object_size);
> +		src_user = malloc(object_size);
>  	}
>  
>  	igt_subtest("normal") {
> @@ -150,9 +156,48 @@ int main(int argc, char **argv)
>  		}
>  	}
>  
> +	igt_subtest("stolen-normal") {
> +		for (count = 1; count <= 1<<17; count <<= 1) {
> +			struct timeval start, end;
> +
> +			gettimeofday(&start, NULL);
> +			do_gem_write(fd, dst_stolen, src_user,
> +				     object_size, count);
> +			gettimeofday(&end, NULL);
> +			igt_info("Time to pwrite %d bytes x %6d:        %7.3fµs, %s\n",
> +				 object_size, count,
> +				 elapsed(&start, &end, count),
> +				 bytes_per_sec((char *)buf,
> +				 object_size/elapsed(&start, &end, count)*1e6));
> +			fflush(stdout);
> +		}
> +	}
> +
> +	for (c = cache; c->level != -1; c++) {
> +		igt_subtest((c + 4)->name) {
> +			gem_set_caching(fd, dst, c->level);
> +			for (count = 1; count <= 1<<17; count <<= 1) {
> +				struct timeval start, end;
> +
> +				gettimeofday(&start, NULL);
> +				do_gem_write(fd, dst_stolen, src_user,
> +					     object_size, count);
> +				gettimeofday(&end, NULL);
> +				igt_info("Time to %s pwrite %d bytes x %6d:     %7.3fµs, %s\n",
> +					 (c + 4)->name, object_size, count,
> +					 elapsed(&start, &end, count),
> +					 bytes_per_sec((char *)buf,
> +					 object_size/elapsed(&start, &end, count)*1e6));
> +				fflush(stdout);
> +			}
> +		}
> +	}
> +
>  	igt_fixture {
>  		free(src);
>  		gem_close(fd, dst);
> +		free(src_user);
> +		gem_close(fd, dst_stolen);
>  
>  		close(fd);
>  	}
> -- 
> 1.9.1
> 
> _______________________________________________
> Intel-gfx mailing list
> Intel-gfx@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Chris Wilson May 7, 2015, 9:12 a.m. UTC | #2
On Thu, May 07, 2015 at 08:52:54AM +0200, Daniel Vetter wrote:
> On Wed, May 06, 2015 at 03:51:52PM +0530, ankitprasad.r.sharma@intel.com wrote:
> > From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > 
> > This patch adds the testcases for verifying the new extended
> > gem_create ioctl. By means of this extended ioctl, memory
> > placement of the GEM object can be specified, i.e. either
> > shmem or stolen memory.
> > These testcases include functional tests and interface tests for
> > testing the gem_create ioctl call for stolen memory placement
> > 
> > v2: Testing pread/pwrite functionality for stolen backed objects,
> > added local struct for extended gem_create and gem_get_aperture,
> > until headers catch up (Chris)
> > 
> > v3: Removed get_aperture related functions, extended gem_pread
> > to compare speeds for user pages with and without page faults,
> > unexposed local_gem_create struct, changed gem_create_stolen
> > usage (Chris)
> > 
> > Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> 
> An igt to check for invalid arguments of the gem create ioctl (especially
> the newly added flags parameters) seems to be missing.

If we do that,  I would actually create gem_create.c to do the parameter
testing of CREATE, and rename this to gem_stolen.c as this covers the
functional side of using stolen (i.e. not limited to testing the CREATE
API). And I want a pink pony.
-Chris
Daniel Vetter May 7, 2015, 1:16 p.m. UTC | #3
On Thu, May 07, 2015 at 10:12:08AM +0100, Chris Wilson wrote:
> On Thu, May 07, 2015 at 08:52:54AM +0200, Daniel Vetter wrote:
> > On Wed, May 06, 2015 at 03:51:52PM +0530, ankitprasad.r.sharma@intel.com wrote:
> > > From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > > 
> > > This patch adds the testcases for verifying the new extended
> > > gem_create ioctl. By means of this extended ioctl, memory
> > > placement of the GEM object can be specified, i.e. either
> > > shmem or stolen memory.
> > > These testcases include functional tests and interface tests for
> > > testing the gem_create ioctl call for stolen memory placement
> > > 
> > > v2: Testing pread/pwrite functionality for stolen backed objects,
> > > added local struct for extended gem_create and gem_get_aperture,
> > > until headers catch up (Chris)
> > > 
> > > v3: Removed get_aperture related functions, extended gem_pread
> > > to compare speeds for user pages with and without page faults,
> > > unexposed local_gem_create struct, changed gem_create_stolen
> > > usage (Chris)
> > > 
> > > Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > 
> > An igt to check for invalid arguments of the gem create ioctl (especially
> > the newly added flags parameters) seems to be missing.
> 
> If we do that,  I would actually create gem_create.c to do the parameter
> testing of CREATE, and rename this to gem_stolen.c as this covers the
> functional side of using stolen (i.e. not limited to testing the CREATE
> API). And I want a pink pony.

Yes, that would be even nicer.
-Daniel
ankitprasad.r.sharma@intel.com May 8, 2015, 5:24 a.m. UTC | #4
On Thu, 2015-05-07 at 08:52 +0200, Daniel Vetter wrote:
> On Wed, May 06, 2015 at 03:51:52PM +0530, ankitprasad.r.sharma@intel.com wrote:
> > From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > 
> > This patch adds the testcases for verifying the new extended
> > gem_create ioctl. By means of this extended ioctl, memory
> > placement of the GEM object can be specified, i.e. either
> > shmem or stolen memory.
> > These testcases include functional tests and interface tests for
> > testing the gem_create ioctl call for stolen memory placement
> > 
> > v2: Testing pread/pwrite functionality for stolen backed objects,
> > added local struct for extended gem_create and gem_get_aperture,
> > until headers catch up (Chris)
> > 
> > v3: Removed get_aperture related functions, extended gem_pread
> > to compare speeds for user pages with and without page faults,
> > unexposed local_gem_create struct, changed gem_create_stolen
> > usage (Chris)
> > 
> > Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> 
> An igt to check for invalid arguments of the gem create ioctl (especially
> the newly added flags parameters) seems to be missing.
> -Daniel

\Wwe already have a test to check invalid arguments for the newly added
flags parameter in the current set of tests.

static void invalid_flag_test(int fd)

-Ankit
Daniel Vetter May 8, 2015, 7:16 a.m. UTC | #5
On Fri, May 08, 2015 at 10:54:26AM +0530, Ankitprasad Sharma wrote:
> On Thu, 2015-05-07 at 08:52 +0200, Daniel Vetter wrote:
> > On Wed, May 06, 2015 at 03:51:52PM +0530, ankitprasad.r.sharma@intel.com wrote:
> > > From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > > 
> > > This patch adds the testcases for verifying the new extended
> > > gem_create ioctl. By means of this extended ioctl, memory
> > > placement of the GEM object can be specified, i.e. either
> > > shmem or stolen memory.
> > > These testcases include functional tests and interface tests for
> > > testing the gem_create ioctl call for stolen memory placement
> > > 
> > > v2: Testing pread/pwrite functionality for stolen backed objects,
> > > added local struct for extended gem_create and gem_get_aperture,
> > > until headers catch up (Chris)
> > > 
> > > v3: Removed get_aperture related functions, extended gem_pread
> > > to compare speeds for user pages with and without page faults,
> > > unexposed local_gem_create struct, changed gem_create_stolen
> > > usage (Chris)
> > > 
> > > Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > 
> > An igt to check for invalid arguments of the gem create ioctl (especially
> > the newly added flags parameters) seems to be missing.
> > -Daniel
> 
> \Wwe already have a test to check invalid arguments for the newly added
> flags parameter in the current set of tests.
> 
> static void invalid_flag_test(int fd)

Oh right I totally missed that. Especially for future extension I think
Chris' idea to split up tests sounds really good, i.e.

gem_create/invalid-flags (the one testcase I didn't spot)
gem_stolen/<all the other tests>

Otherwise the next person extending gem_create will miss your
invalid-flags test for it.
Thanks, Daniel
ankitprasad.r.sharma@intel.com May 10, 2015, 6:47 p.m. UTC | #6
On Fri, 2015-05-08 at 09:16 +0200, Daniel Vetter wrote:
> On Fri, May 08, 2015 at 10:54:26AM +0530, Ankitprasad Sharma wrote:
> > On Thu, 2015-05-07 at 08:52 +0200, Daniel Vetter wrote:
> > > On Wed, May 06, 2015 at 03:51:52PM +0530, ankitprasad.r.sharma@intel.com wrote:
> > > > From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > > > 
> > > > This patch adds the testcases for verifying the new extended
> > > > gem_create ioctl. By means of this extended ioctl, memory
> > > > placement of the GEM object can be specified, i.e. either
> > > > shmem or stolen memory.
> > > > These testcases include functional tests and interface tests for
> > > > testing the gem_create ioctl call for stolen memory placement
> > > > 
> > > > v2: Testing pread/pwrite functionality for stolen backed objects,
> > > > added local struct for extended gem_create and gem_get_aperture,
> > > > until headers catch up (Chris)
> > > > 
> > > > v3: Removed get_aperture related functions, extended gem_pread
> > > > to compare speeds for user pages with and without page faults,
> > > > unexposed local_gem_create struct, changed gem_create_stolen
> > > > usage (Chris)
> > > > 
> > > > Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > > 
> > > An igt to check for invalid arguments of the gem create ioctl (especially
> > > the newly added flags parameters) seems to be missing.
> > > -Daniel
> > 
> > \Wwe already have a test to check invalid arguments for the newly added
> > flags parameter in the current set of tests.
> > 
> > static void invalid_flag_test(int fd)
> 
> Oh right I totally missed that. Especially for future extension I think
> Chris' idea to split up tests sounds really good, i.e.
> 
> gem_create/invalid-flags (the one testcase I didn't spot)
> gem_stolen/<all the other tests>
> 
> Otherwise the next person extending gem_create will miss your
> invalid-flags test for it.
> Thanks, Daniel

As I see, to validate the parameters for gem_create ioctl there needs to
be verification for size too, other than the newly added parameter
flags. 
I will create new igt for validating the parameters of gem_create ioctl.
(i.e. invalid size and flags)
Can you please suggest any other tests that can be added to the
gem_create igt?

Thanks,
Ankit
Daniel Vetter May 11, 2015, 9:49 a.m. UTC | #7
On Mon, May 11, 2015 at 12:17:09AM +0530, Ankitprasad Sharma wrote:
> On Fri, 2015-05-08 at 09:16 +0200, Daniel Vetter wrote:
> > On Fri, May 08, 2015 at 10:54:26AM +0530, Ankitprasad Sharma wrote:
> > > On Thu, 2015-05-07 at 08:52 +0200, Daniel Vetter wrote:
> > > > On Wed, May 06, 2015 at 03:51:52PM +0530, ankitprasad.r.sharma@intel.com wrote:
> > > > > From: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > > > > 
> > > > > This patch adds the testcases for verifying the new extended
> > > > > gem_create ioctl. By means of this extended ioctl, memory
> > > > > placement of the GEM object can be specified, i.e. either
> > > > > shmem or stolen memory.
> > > > > These testcases include functional tests and interface tests for
> > > > > testing the gem_create ioctl call for stolen memory placement
> > > > > 
> > > > > v2: Testing pread/pwrite functionality for stolen backed objects,
> > > > > added local struct for extended gem_create and gem_get_aperture,
> > > > > until headers catch up (Chris)
> > > > > 
> > > > > v3: Removed get_aperture related functions, extended gem_pread
> > > > > to compare speeds for user pages with and without page faults,
> > > > > unexposed local_gem_create struct, changed gem_create_stolen
> > > > > usage (Chris)
> > > > > 
> > > > > Signed-off-by: Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
> > > > 
> > > > An igt to check for invalid arguments of the gem create ioctl (especially
> > > > the newly added flags parameters) seems to be missing.
> > > > -Daniel
> > > 
> > > \Wwe already have a test to check invalid arguments for the newly added
> > > flags parameter in the current set of tests.
> > > 
> > > static void invalid_flag_test(int fd)
> > 
> > Oh right I totally missed that. Especially for future extension I think
> > Chris' idea to split up tests sounds really good, i.e.
> > 
> > gem_create/invalid-flags (the one testcase I didn't spot)
> > gem_stolen/<all the other tests>
> > 
> > Otherwise the next person extending gem_create will miss your
> > invalid-flags test for it.
> > Thanks, Daniel
> 
> As I see, to validate the parameters for gem_create ioctl there needs to
> be verification for size too, other than the newly added parameter
> flags. 
> I will create new igt for validating the parameters of gem_create ioctl.
> (i.e. invalid size and flags)
> Can you please suggest any other tests that can be added to the
> gem_create igt?

Besides testing a few invalid sizes (0, not page-aligned) and ensuring
invalid flags get rejected I don't think we need more gem_create ioctl
input validation tests.
-Daniel
diff mbox

Patch

diff --git a/lib/ioctl_wrappers.c b/lib/ioctl_wrappers.c
index ff78ef1..2bd341a 100644
--- a/lib/ioctl_wrappers.c
+++ b/lib/ioctl_wrappers.c
@@ -379,6 +379,66 @@  void gem_sync(int fd, uint32_t handle)
 	gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
 }
 
+bool gem_create__has_stolen_support(int fd)
+{
+	static int has_stolen_support = -1;
+	struct drm_i915_getparam gp;
+	int val = -1;
+
+	return 1;
+	if (has_stolen_support < 0) {
+		memset(&gp, 0, sizeof(gp));
+		gp.param = 35; /* CREATE_VERSION */
+		gp.value = &val;
+
+		/* Do we have the extended gem_create_ioctl? */
+		ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
+		has_stolen_support = val >= 1;
+	}
+
+	return has_stolen_support;
+}
+
+struct local_i915_gem_create_v2 {
+	uint64_t size;
+	uint32_t handle;
+	uint32_t pad;
+#define I915_CREATE_PLACEMENT_STOLEN (1<<0)
+	uint32_t flags;
+};
+
+#define LOCAL_IOCTL_I915_GEM_CREATE       DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct local_i915_gem_create_v2)
+/**
+ * gem_create_stolen:
+ * @fd: open i915 drm file descriptor
+ * @size: desired size of the buffer
+ * @flags: desired placement i.e. stolen or shmem
+ *
+ * This wraps the new GEM_CREATE ioctl, which allocates a
+ * new gem buffer object of @size and placement based on @flags.
+ *
+ * Returns: The file-private handle of the created buffer object
+ */
+
+uint32_t gem_create_stolen(int fd, int size)
+{
+	struct local_i915_gem_create_v2 create;
+	int ret;
+
+	memset(&create, 0, sizeof(create));
+	create.handle = 0;
+	create.size = size;
+	create.flags = I915_CREATE_PLACEMENT_STOLEN;
+	ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
+
+	if (ret < 0)
+		return 0;
+
+	errno = 0;
+	return create.handle;
+}
+
+
 uint32_t __gem_create(int fd, int size)
 {
 	struct drm_i915_gem_create create;
diff --git a/lib/ioctl_wrappers.h b/lib/ioctl_wrappers.h
index ced7ef3..cac1c11 100644
--- a/lib/ioctl_wrappers.h
+++ b/lib/ioctl_wrappers.h
@@ -56,6 +56,9 @@  void gem_read(int fd, uint32_t handle, uint32_t offset, void *buf, uint32_t leng
 void gem_set_domain(int fd, uint32_t handle,
 		    uint32_t read_domains, uint32_t write_domain);
 void gem_sync(int fd, uint32_t handle);
+
+bool gem_create__has_stolen_support(int fd);
+uint32_t gem_create_stolen(int fd, int size);
 uint32_t __gem_create(int fd, int size);
 uint32_t gem_create(int fd, int size);
 void gem_execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
@@ -67,6 +70,15 @@  bool gem_mmap__has_wc(int fd);
 void *gem_mmap__wc(int fd, uint32_t handle, int offset, int size, int prot);
 
 /**
+ * gem_require_stolen_support:
+ * @fd: open i915 drm file descriptor
+ *
+ * Test macro to query whether support for allocating objects from stolen
+ * memory is available. Automatically skips through igt_require() if not.
+ */
+#define gem_require_stolen_support(fd) \
+			igt_require(gem_create__has_stolen_support(fd))
+/**
  * gem_require_mmap_wc:
  * @fd: open i915 drm file descriptor
  *
diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index a165978..9b2ee90 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -23,6 +23,7 @@  TESTS_progs_M = \
 	gem_caching \
 	gem_close_race \
 	gem_concurrent_blit \
+	gem_create_stolen \
 	gem_cs_tlb \
 	gem_ctx_param_basic \
 	gem_ctx_bad_exec \
diff --git a/tests/gem_create_stolen.c b/tests/gem_create_stolen.c
new file mode 100644
index 0000000..402d42b
--- /dev/null
+++ b/tests/gem_create_stolen.c
@@ -0,0 +1,387 @@ 
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Ankitprasad Sharma <ankitprasad.r.sharma@intel.com>
+ *
+ */
+
+/** @file gem_create_stolen.c
+ *
+ * This is a test for the extended gem_create ioctl, that includes allocation
+ * of object from stolen memory
+ *
+ * The goal is to simply ensure the basics work, and invalid input combinations
+ * are rejected.
+ */
+
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <getopt.h>
+
+#include <drm.h>
+
+#include "ioctl_wrappers.h"
+#include "intel_bufmgr.h"
+#include "intel_batchbuffer.h"
+#include "intel_io.h"
+#include "intel_chipset.h"
+#include "igt_aux.h"
+#include "drmtest.h"
+#include "drm.h"
+#include "i915_drm.h"
+
+#define CLEAR(s) memset(&s, 0, sizeof(s))
+#define SIZE 1024*1024
+
+static struct drm_intel_bufmgr *bufmgr;
+static struct intel_batchbuffer *batch;
+
+static void verify_copy_op(drm_intel_bo *src, drm_intel_bo *dest)
+{
+	uint32_t *virt, i, ret;
+	/* Fill the src BO with dwords */
+	ret = drm_intel_gem_bo_map_gtt(src);
+	igt_assert(!ret);
+
+	virt = src->virtual;
+	for (i = 0; i < SIZE/4; i++)
+		virt[i] = i;
+
+	intel_copy_bo(batch, dest, src, SIZE);
+
+	ret = drm_intel_gem_bo_map_gtt(dest);
+	igt_assert(!ret);
+
+	virt = dest->virtual;
+	/* verify */
+	for (i = 0; i < SIZE/4; i++) {
+		if (virt[i] != i) {
+			fprintf(stderr, "Expected 0x%08x, found 0x%08x "
+					"at offset 0x%08x\n",
+					i, virt[i], i * 4);
+			igt_assert(virt[i] == i);
+		}
+	}
+
+	drm_intel_bo_unmap(src);
+	drm_intel_bo_unmap(dest);
+}
+
+static void stolen_pwrite(int fd)
+{
+	drm_intel_bo *bo;
+	uint32_t buf[SIZE/4];
+	uint32_t handle = 0;
+	uint32_t *virt;
+	int i, ret = 0;
+
+	for (i = 0; i < SIZE/4; i ++)
+		buf[i] = 0xdead;
+
+	gem_require_stolen_support(fd);
+
+	handle = gem_create_stolen(fd, SIZE);
+	igt_assert(handle != 0);
+
+	gem_write(fd, handle, 0, buf, SIZE);
+	bo = gem_handle_to_libdrm_bo(bufmgr, fd, "bo", handle);
+
+	ret = drm_intel_gem_bo_map_gtt(bo);
+	igt_assert(!ret);
+
+	virt = bo->virtual;
+
+	for (i = 0; i < SIZE/4; i ++)
+		igt_assert(virt[i] == 0xdead);
+
+	drm_intel_bo_unmap(bo);
+	drm_intel_bo_unreference(bo);
+	gem_close(fd, handle);
+}
+
+static void stolen_pread(int fd)
+{
+	drm_intel_bo *bo;
+	uint32_t buf[SIZE/4];
+	uint32_t handle = 0;
+	uint32_t *virt;
+	int i, ret = 0;
+
+	CLEAR(buf);
+
+	gem_require_stolen_support(fd);
+
+	handle = gem_create_stolen(fd, SIZE);
+	igt_assert(handle != 0);
+
+	bo = gem_handle_to_libdrm_bo(bufmgr, fd, "bo", handle);
+
+	ret = drm_intel_gem_bo_map_gtt(bo);
+	igt_assert(!ret);
+
+	virt = bo->virtual;
+
+	for (i = 0; i < SIZE/4; i ++)
+		virt[i] = 0xdead;
+
+	drm_intel_bo_unmap(bo);
+	drm_intel_bo_unreference(bo);
+
+	gem_read(fd, handle, 0, buf, SIZE);
+
+	for (i = 0; i < SIZE/4; i ++)
+		igt_assert(buf[i] == 0xdead);
+
+	gem_close(fd, handle);
+}
+
+static void copy_test(int fd)
+{
+	drm_intel_bo *src, *dest;
+	uint32_t src_handle = 0, dest_handle = 0;
+	int ret = 0;
+
+	gem_require_stolen_support(fd);
+
+	src_handle = gem_create_stolen(fd, SIZE);
+	igt_assert(src_handle != 0);
+
+	dest_handle = gem_create_stolen(fd, SIZE);
+	igt_assert(dest_handle != 0);
+
+	src = gem_handle_to_libdrm_bo(bufmgr, fd, "src_bo", src_handle);
+	dest = gem_handle_to_libdrm_bo(bufmgr, fd, "dst_bo", dest_handle);
+
+	igt_assert(src != NULL);
+	igt_assert(dest != NULL);
+
+	verify_copy_op(src, dest);
+
+	drm_intel_bo_unreference(src);
+	drm_intel_bo_unreference(dest);
+	gem_close(fd, src_handle);
+	gem_close(fd, dest_handle);
+}
+
+static void verify_object_clear(int fd)
+{
+	drm_intel_bo *bo;
+	uint32_t handle = 0;
+	uint32_t *virt;
+	int i, ret = 0;
+
+	gem_require_stolen_support(fd);
+
+	handle = gem_create_stolen(fd, SIZE);
+	igt_assert(handle != 0);
+
+	bo = gem_handle_to_libdrm_bo(bufmgr, fd, "verify_bo", handle);
+	igt_assert(bo != NULL);
+
+	ret = drm_intel_gem_bo_map_gtt(bo);
+	igt_assert(ret == 0);
+
+	/* Verify if the BO is zeroed */
+	virt = bo->virtual;
+	for (i = 0; i < SIZE / 4; i++)
+		igt_assert(virt[i] == 0);
+
+	drm_intel_bo_unmap(bo);
+	drm_intel_bo_unreference(bo);
+	gem_close(fd, handle);
+}
+
+static void stolen_fill_purge_test(int fd)
+{
+	drm_intel_bo *bo;
+	int mret = 0, ret = 0, obj_count = 0, i = 0;
+	int _ret = 0, j = 0;
+	uint32_t handle[100] = {0};
+	uint32_t new_handle = 0;
+	uint32_t *virt;
+	int retained = 0;
+
+	gem_require_stolen_support(fd);
+
+	/* Exhaust Stolen space */
+	while(ret == 0) {
+		ret = -1;
+		handle[i] = gem_create_stolen(fd, SIZE);
+		if (handle[i] != 0) {
+			ret = 0;
+			bo = gem_handle_to_libdrm_bo(bufmgr, fd,
+						     "verify_bo", handle[i]);
+			igt_assert(bo != NULL);
+
+			_ret = drm_intel_gem_bo_map_gtt(bo);
+			igt_assert(_ret == 0);
+
+			virt = bo->virtual;
+			for (j = 0; j < SIZE / 4; j++)
+				virt[j] = 0xab;
+
+			drm_intel_bo_unmap(bo);
+			drm_intel_bo_unreference(bo);
+
+			obj_count++;
+		}
+
+		i++;
+	}
+
+	igt_assert(obj_count > 0);
+
+	/* Mark all stolen objects purgeable */
+	for (i = 0; i < obj_count; i++)
+		retained = gem_madvise(fd, handle[i], I915_MADV_DONTNEED);
+
+	/* Try to allocate one more object */
+	new_handle = gem_create_stolen(fd, SIZE);
+	igt_assert(new_handle != 0);
+
+	/* Check if the retained object's memory contents are intact */
+	for (i = 0; i < obj_count; i++) {
+		retained = gem_madvise(fd, handle[i], I915_MADV_WILLNEED);
+		if (retained) {
+			bo = gem_handle_to_libdrm_bo(bufmgr, fd,
+						     "verify_bo", handle[i]);
+			igt_assert(bo != NULL);
+
+			_ret = drm_intel_gem_bo_map_gtt(bo);
+			igt_assert(_ret == 0);
+
+			virt = bo->virtual;
+			for (j = 0; j < SIZE / 4; j++)
+				igt_assert(virt[j] == 0xab);
+
+			drm_intel_bo_unmap(bo);
+			drm_intel_bo_unreference(bo);
+		}
+	}
+
+	gem_close(fd, new_handle);
+	for (i = 0; i < obj_count; i++)
+		gem_close(fd, handle[i]);
+}
+
+static void
+stolen_no_mmap(int fd)
+{
+	void *addr;
+	uint32_t handle = 0;
+
+	gem_require_stolen_support(fd);
+
+	handle = gem_create_stolen(fd, SIZE);
+	igt_assert(handle != 0);
+
+	addr = gem_mmap__cpu(fd, handle, 0, SIZE, PROT_READ | PROT_WRITE);
+	igt_assert(addr == NULL);
+
+	gem_close(fd, handle);
+}
+
+static void invalid_flag_test(int fd)
+{
+	int handle = 0;
+
+struct local_i915_gem_create_v2 {
+	uint64_t size;
+	uint32_t handle;
+	uint32_t pad;
+#define I915_CREATE_PLACEMENT_STOLEN (1<<0)
+	uint32_t flags;
+} create;
+
+#define LOCAL_IOCTL_I915_GEM_CREATE       DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct local_i915_gem_create_v2)
+	gem_require_stolen_support(fd);
+
+	create.handle = 0;
+	create.size = SIZE;
+	create.flags = ~I915_CREATE_PLACEMENT_STOLEN;
+	handle = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
+
+	igt_assert(handle <= 0);
+}
+
+igt_main
+{
+	int i, fd, gtt_size_total, gtt_size_mappable;
+	uint32_t devid;
+
+	igt_skip_on_simulation();
+
+	igt_fixture {
+		fd = drm_open_any();
+		devid = intel_get_drm_devid(fd);
+
+		drm_intel_get_aperture_sizes(fd, (size_t*)&gtt_size_total,
+				(size_t*)&gtt_size_mappable);
+		bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
+		batch = intel_batchbuffer_alloc(bufmgr, devid);
+	}
+
+	igt_subtest("stolen-invalid-flag")
+		invalid_flag_test(fd);
+
+	igt_subtest("stolen-clear")
+		verify_object_clear(fd);
+
+	/*
+	 * stolen mem special cases - checking for non cpu mappable
+	 */
+	igt_subtest("stolen-no-mmap")
+		stolen_no_mmap(fd);
+
+	/* checking for pread/pwrite interfaces */
+	igt_subtest("stolen-pwrite")
+		stolen_pwrite(fd);
+
+	igt_subtest("stolen-pread")
+		stolen_pread(fd);
+
+	/* Functional Test - blt copy */
+	igt_subtest("stolen-copy")
+		copy_test(fd);
+
+	/* Filling stolen completely and marking all the objects
+	 * purgeable. Then trying to add one more object, to verify
+	 * the purging logic.
+	 * Again marking all objects WILLNEED and verifying the
+	 * contents of the retained objects.
+	 */
+	igt_subtest("stolen-fill-purge")
+		stolen_fill_purge_test(fd);
+
+	igt_fixture {
+		intel_batchbuffer_free(batch);
+		drm_intel_bufmgr_destroy(bufmgr);
+	}
+}
diff --git a/tests/gem_pread.c b/tests/gem_pread.c
index cc83948..95162d5 100644
--- a/tests/gem_pread.c
+++ b/tests/gem_pread.c
@@ -41,6 +41,10 @@ 
 #include "drmtest.h"
 
 #define OBJECT_SIZE 16384
+#define LARGE_OBJECT_SIZE 1024 * 1024
+#define KGRN "\x1B[32m"
+#define KRED "\x1B[31m"
+#define KNRM "\x1B[0m"
 
 static void do_gem_read(int fd, uint32_t handle, void *buf, int len, int loops)
 {
@@ -76,11 +80,14 @@  static const char *bytes_per_sec(char *buf, double v)
 
 
 uint32_t *src, dst;
+uint32_t *dst_user, src_stolen, large_stolen;
+uint32_t *stolen_pf_user, *stolen_nopf_user;
 int fd, count;
 
 int main(int argc, char **argv)
 {
 	int object_size = 0;
+	int large_obj_size = LARGE_OBJECT_SIZE;
 	uint32_t buf[20];
 	const struct {
 		int level;
@@ -90,6 +97,9 @@  int main(int argc, char **argv)
 		{ 1, "snoop" },
 		{ 2, "display" },
 		{ -1 },
+		{ -1, "stolen-uncached"},
+		{ -1, "stolen-snoop"},
+		{ -1, "stolen-display"},
 	}, *c;
 
 	igt_subtest_init(argc, argv);
@@ -106,6 +116,8 @@  int main(int argc, char **argv)
 
 		dst = gem_create(fd, object_size);
 		src = malloc(object_size);
+		src_stolen = gem_create_stolen(fd, object_size);
+		dst_user = malloc(object_size);
 	}
 
 	igt_subtest("normal") {
@@ -142,9 +154,97 @@  int main(int argc, char **argv)
 		}
 	}
 
+	igt_subtest("stolen-normal") {
+		for (count = 1; count <= 1<<17; count <<= 1) {
+			struct timeval start, end;
+
+			gettimeofday(&start, NULL);
+			do_gem_read(fd, src_stolen, dst_user, object_size, count);
+			gettimeofday(&end, NULL);
+			igt_info("Time to pread %d bytes x %6d:	%7.3fµs, %s\n",
+				 object_size, count,
+				 elapsed(&start, &end, count),
+				 bytes_per_sec((char *)buf,
+				 object_size/elapsed(&start, &end, count)*1e6));
+			fflush(stdout);
+		}
+	}
+	for (c = cache; c->level != -1; c++) {
+		igt_subtest((c + 4)->name) {
+			gem_set_caching(fd, src_stolen, c->level);
+
+			for (count = 1; count <= 1<<17; count <<= 1) {
+				struct timeval start, end;
+
+				gettimeofday(&start, NULL);
+				do_gem_read(fd, src_stolen, dst_user,
+					    object_size, count);
+				gettimeofday(&end, NULL);
+				igt_info("Time to %s pread %d bytes x %6d:      %7.3fµs, %s\n",
+					 (c + 4)->name, object_size, count,
+					 elapsed(&start, &end, count),
+					 bytes_per_sec((char *)buf,
+					 object_size/elapsed(&start, &end, count)*1e6));
+				fflush(stdout);
+			}
+		}
+	}
+
+	/* List pread times for stolen area with 1 page fault (page fault only
+	 * first time) and multiple page faults (unmapping the pages at the
+	 * end of each iteration)
+	 */
+	igt_subtest("pagefault-pread") {
+		large_stolen = gem_create_stolen(fd, large_obj_size);
+		stolen_nopf_user = (uint32_t *) mmap(NULL, large_obj_size,
+						PROT_WRITE,
+						MAP_ANONYMOUS|MAP_PRIVATE,
+						-1, 0);
+
+		for (count = 1; count <= 10; count ++) {
+			struct timeval start, end;
+			uint32_t t_elapsed = 0;
+
+			gettimeofday(&start, NULL);
+			do_gem_read(fd, large_stolen, stolen_nopf_user,
+				    large_obj_size, 1);
+			gettimeofday(&end, NULL);
+			t_elapsed = elapsed(&start, &end, 1);
+			igt_info("Pagefault-N - Time to pread %d bytes: %7.3fµs, %s\n",
+				 large_obj_size,
+				 elapsed(&start, &end, 1),
+				 bytes_per_sec((char *)buf,
+				 large_obj_size/elapsed(&start, &end, 1)*1e6));
+
+			stolen_pf_user = (uint32_t *) mmap(NULL, large_obj_size,
+						      PROT_WRITE,
+						      MAP_ANONYMOUS|MAP_PRIVATE,
+						      -1, 0);
+
+			gettimeofday(&start, NULL);
+			do_gem_read(fd, large_stolen, stolen_pf_user,
+				    large_obj_size, 1);
+			gettimeofday(&end, NULL);
+			igt_info("Pagefault-Y - Time to pread %d bytes: %7.3fµs, %s%s%s\n",
+				 large_obj_size,
+				 elapsed(&start, &end, 1),
+				 t_elapsed < elapsed(&start, &end, 1) ? KGRN : KRED,
+				 bytes_per_sec((char *)buf,
+				 large_obj_size/elapsed(&start, &end, 1)*1e6),
+				 KNRM);
+			fflush(stdout);
+			munmap(stolen_pf_user, large_obj_size);
+		}
+			munmap(stolen_nopf_user, large_obj_size);
+			gem_close(fd, large_stolen);
+	}
+
+
 	igt_fixture {
 		free(src);
 		gem_close(fd, dst);
+		free(dst_user);
+		gem_close(fd, src_stolen);
 
 		close(fd);
 	}
diff --git a/tests/gem_pwrite.c b/tests/gem_pwrite.c
index 6378b0a..dd1ca95 100644
--- a/tests/gem_pwrite.c
+++ b/tests/gem_pwrite.c
@@ -82,6 +82,7 @@  static const char *bytes_per_sec(char *buf, double v)
 
 
 uint32_t *src, dst;
+uint32_t *src_user, dst_stolen;
 int fd;
 
 int main(int argc, char **argv)
@@ -97,6 +98,9 @@  int main(int argc, char **argv)
 		{ 1, "snoop" },
 		{ 2, "display" },
 		{ -1 },
+		{ -1, "stolen-uncached"},
+		{ -1, "stolen-snoop"},
+		{ -1, "stolen-display"},
 	}, *c;
 
 	igt_skip_on_simulation();
@@ -114,6 +118,8 @@  int main(int argc, char **argv)
 
 		dst = gem_create(fd, object_size);
 		src = malloc(object_size);
+		dst_stolen = gem_create_stolen(fd, object_size);
+		src_user = malloc(object_size);
 	}
 
 	igt_subtest("normal") {
@@ -150,9 +156,48 @@  int main(int argc, char **argv)
 		}
 	}
 
+	igt_subtest("stolen-normal") {
+		for (count = 1; count <= 1<<17; count <<= 1) {
+			struct timeval start, end;
+
+			gettimeofday(&start, NULL);
+			do_gem_write(fd, dst_stolen, src_user,
+				     object_size, count);
+			gettimeofday(&end, NULL);
+			igt_info("Time to pwrite %d bytes x %6d:        %7.3fµs, %s\n",
+				 object_size, count,
+				 elapsed(&start, &end, count),
+				 bytes_per_sec((char *)buf,
+				 object_size/elapsed(&start, &end, count)*1e6));
+			fflush(stdout);
+		}
+	}
+
+	for (c = cache; c->level != -1; c++) {
+		igt_subtest((c + 4)->name) {
+			gem_set_caching(fd, dst, c->level);
+			for (count = 1; count <= 1<<17; count <<= 1) {
+				struct timeval start, end;
+
+				gettimeofday(&start, NULL);
+				do_gem_write(fd, dst_stolen, src_user,
+					     object_size, count);
+				gettimeofday(&end, NULL);
+				igt_info("Time to %s pwrite %d bytes x %6d:     %7.3fµs, %s\n",
+					 (c + 4)->name, object_size, count,
+					 elapsed(&start, &end, count),
+					 bytes_per_sec((char *)buf,
+					 object_size/elapsed(&start, &end, count)*1e6));
+				fflush(stdout);
+			}
+		}
+	}
+
 	igt_fixture {
 		free(src);
 		gem_close(fd, dst);
+		free(src_user);
+		gem_close(fd, dst_stolen);
 
 		close(fd);
 	}