From patchwork Wed May 25 18:36:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Matthew Auld X-Patchwork-Id: 12861509 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 15CA7C433EF for ; Wed, 25 May 2022 18:38:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F2A2510ECA7; Wed, 25 May 2022 18:38:11 +0000 (UTC) Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9CD6B10EC60; Wed, 25 May 2022 18:38:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653503886; x=1685039886; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8pN6+CukTItDpR/QzRY/B16VmTn4iiK2Kgi5CzcYWUQ=; b=VnUrmYBtjvHOfKC5DWhbGnVyq4n4qZkBfuDnQ7bqWc216SR8xMh/FOHa HQww0WoTk7bVMFfZTnQF1n6HahAWhig1py7SgO2lInsmNYF8gkPj1bvBA UcYUFgvO3TOsrpRiuyP6Ab3d6pJqKiTG7h2YTekcEgOyQxhojeTC2IpN4 3Fy0HfPjs9K3TCwwEhqH5CNcb53BjCjQ8PZcd/pcAQBBlL/mBPPd87uvv sWO1NlYi2pQhjQPZg+R+pT1Dj0kGpoMbI2blktC/zMbp8NWyHhf4X2F/v OgxcpW7i+ffWWADTn7HsBpUxoQG9EtmajjdHby9+meKPrTx7rae52Rwzl g==; X-IronPort-AV: E=McAfee;i="6400,9594,10358"; a="336954769" X-IronPort-AV: E=Sophos;i="5.91,250,1647327600"; d="scan'208";a="336954769" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2022 11:37:51 -0700 X-IronPort-AV: E=Sophos;i="5.91,250,1647327600"; d="scan'208";a="526989516" Received: from daithiby-mobl.ger.corp.intel.com (HELO mwauld-desk1.intel.com) ([10.252.5.16]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2022 11:37:50 -0700 From: Matthew Auld To: igt-dev@lists.freedesktop.org Date: Wed, 25 May 2022 19:36:57 +0100 Message-Id: <20220525183702.490989-5-matthew.auld@intel.com> X-Mailer: git-send-email 2.34.3 In-Reply-To: <20220525183702.490989-1-matthew.auld@intel.com> References: <20220525183702.490989-1-matthew.auld@intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 4/9] lib/i915: add gem_create_with_cpu_access_in_memory_regions X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Thomas_Hellstr=C3=B6m?= , intel-gfx@lists.freedesktop.org Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Most users shouldn't care about such an interface, but where required, this should be useful to aid in setting NEEDS_CPU_ACCESS for a given BO. Underneath we try to smooth over needing to provide an explicit SMEM region, or if this is SMEM-only, we don't want the kernel to throw an error. Signed-off-by: Matthew Auld Cc: Thomas Hellström --- lib/i915/intel_memory_region.c | 10 +++++--- lib/i915/intel_memory_region.h | 46 +++++++++++++++++++++++++++++++--- tests/i915/gem_eio.c | 1 + tests/i915/gem_lmem_swapping.c | 2 +- tests/i915/i915_pm_rpm.c | 6 ++--- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/lib/i915/intel_memory_region.c b/lib/i915/intel_memory_region.c index f0589e98..da81650d 100644 --- a/lib/i915/intel_memory_region.c +++ b/lib/i915/intel_memory_region.c @@ -197,7 +197,7 @@ bool gem_has_lmem(int fd) /* A version of gem_create_in_memory_region_list which can be allowed to fail so that the object creation can be retried */ -int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t *size, +int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t *size, uint32_t flags, struct drm_i915_gem_memory_class_instance *mem_regions, int num_regions) { @@ -208,7 +208,9 @@ int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t *size, }; int ret; - ret = __gem_create_ext(fd, size, 0, handle, &ext_regions.base); + ret = __gem_create_ext(fd, size, flags, handle, &ext_regions.base); + if (flags && ret == -EINVAL) + ret = __gem_create_ext(fd, size, 0, handle, &ext_regions.base); /* * Provide fallback for stable kernels if region passed in the array @@ -231,12 +233,12 @@ int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t *size, * @mem_regions: memory regions array (priority list) * @num_regions: @mem_regions length */ -uint32_t gem_create_in_memory_region_list(int fd, uint64_t size, +uint32_t gem_create_in_memory_region_list(int fd, uint64_t size, uint32_t flags, struct drm_i915_gem_memory_class_instance *mem_regions, int num_regions) { uint32_t handle; - int ret = __gem_create_in_memory_region_list(fd, &handle, &size, + int ret = __gem_create_in_memory_region_list(fd, &handle, &size, flags, mem_regions, num_regions); igt_assert_eq(ret, 0); return handle; diff --git a/lib/i915/intel_memory_region.h b/lib/i915/intel_memory_region.h index f9af9401..5aa163dd 100644 --- a/lib/i915/intel_memory_region.h +++ b/lib/i915/intel_memory_region.h @@ -21,6 +21,7 @@ * IN THE SOFTWARE. */ #include "igt_collection.h" +#include "i915_drm_local.h" #ifndef INTEL_MEMORY_REGION_H #define INTEL_MEMORY_REGION_H @@ -64,11 +65,11 @@ bool gem_has_lmem(int fd); struct drm_i915_gem_memory_class_instance; -int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t *size, +int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t *size, uint32_t flags, struct drm_i915_gem_memory_class_instance *mem_regions, int num_regions); -uint32_t gem_create_in_memory_region_list(int fd, uint64_t size, +uint32_t gem_create_in_memory_region_list(int fd, uint64_t size, uint32_t flags, struct drm_i915_gem_memory_class_instance *mem_regions, int num_regions); @@ -84,7 +85,7 @@ uint32_t gem_create_in_memory_region_list(int fd, uint64_t size, arr_query__[i__].memory_class = MEMORY_TYPE_FROM_REGION(arr__[i__]); \ arr_query__[i__].memory_instance = MEMORY_INSTANCE_FROM_REGION(arr__[i__]); \ } \ - __gem_create_in_memory_region_list(fd, handle, size, arr_query__, ARRAY_SIZE(arr_query__)); \ + __gem_create_in_memory_region_list(fd, handle, size, 0, arr_query__, ARRAY_SIZE(arr_query__)); \ }) #define gem_create_in_memory_regions(fd, size, regions...) ({ \ unsigned int arr__[] = { regions }; \ @@ -93,7 +94,44 @@ uint32_t gem_create_in_memory_region_list(int fd, uint64_t size, arr_query__[i__].memory_class = MEMORY_TYPE_FROM_REGION(arr__[i__]); \ arr_query__[i__].memory_instance = MEMORY_INSTANCE_FROM_REGION(arr__[i__]); \ } \ - gem_create_in_memory_region_list(fd, size, arr_query__, ARRAY_SIZE(arr_query__)); \ + gem_create_in_memory_region_list(fd, size, 0, arr_query__, ARRAY_SIZE(arr_query__)); \ +}) + +/* + * Create an object that requires CPU access. This only becomes interesting on + * platforms that have a small BAR for LMEM CPU access. Without this the object + * might need to be migrated when CPU faulting the object, or if that is not + * possible we hit SIGBUS. Most users should be fine with this. If enabled the + * kernel will never allocate this object in the non-CPU visible portion of + * LMEM. + * + * Underneath this just enables the I915_GEM_CREATE_EXT_FLAG_NEEDS_CPU_ACCESS + * flag, if we also have an LMEM placement. Also since the kernel requires SMEM + * as a potential placement, we automatically attach that as a possible + * placement, if not already provided. If this happens to be an SMEM-only + * placement then we don't supply the flag, and instead just treat as normal + * allocation. + */ +#define gem_create_with_cpu_access_in_memory_regions(fd, size, regions...) ({ \ + unsigned int arr__[] = { regions }; \ + struct drm_i915_gem_memory_class_instance arr_query__[ARRAY_SIZE(arr__) + 1]; \ + int i__, arr_query_size__ = ARRAY_SIZE(arr__); \ + uint32_t ext_flags__ = 0; \ + bool ext_found_smem__ = false; \ + for (i__ = 0; i__ < arr_query_size__; ++i__) { \ + arr_query__[i__].memory_class = MEMORY_TYPE_FROM_REGION(arr__[i__]); \ + if (arr_query__[i__].memory_class == I915_MEMORY_CLASS_DEVICE) \ + ext_flags__ = I915_GEM_CREATE_EXT_FLAG_NEEDS_CPU_ACCESS; \ + else \ + ext_found_smem__ = true; \ + arr_query__[i__].memory_instance = MEMORY_INSTANCE_FROM_REGION(arr__[i__]); \ + } \ + if (ext_flags__ && !ext_found_smem__) { \ + arr_query__[i__].memory_class = I915_MEMORY_CLASS_SYSTEM; \ + arr_query__[i__].memory_instance = 0; \ + arr_query_size__++; \ + } \ + gem_create_in_memory_region_list(fd, size, ext_flags__, arr_query__, arr_query_size__); \ }) struct igt_collection * diff --git a/tests/i915/gem_eio.c b/tests/i915/gem_eio.c index 913a21f9..a9641cca 100644 --- a/tests/i915/gem_eio.c +++ b/tests/i915/gem_eio.c @@ -146,6 +146,7 @@ static void test_create_ext(int fd) igt_assert_eq(__gem_create_in_memory_region_list(fd, &handle, &size, + 0, &r->ci, 1), 0); diff --git a/tests/i915/gem_lmem_swapping.c b/tests/i915/gem_lmem_swapping.c index 5d93e9da..bb9e69db 100644 --- a/tests/i915/gem_lmem_swapping.c +++ b/tests/i915/gem_lmem_swapping.c @@ -131,7 +131,7 @@ static uint32_t create_bo(int i915, int ret; retry: - ret = __gem_create_in_memory_region_list(i915, &handle, size, region, 1); + ret = __gem_create_in_memory_region_list(i915, &handle, size, 0, region, 1); if (do_oom_test && ret == -ENOMEM) goto retry; igt_assert_eq(ret, 0); diff --git a/tests/i915/i915_pm_rpm.c b/tests/i915/i915_pm_rpm.c index bf145b6c..e95875dc 100644 --- a/tests/i915/i915_pm_rpm.c +++ b/tests/i915/i915_pm_rpm.c @@ -1117,7 +1117,7 @@ static void gem_mmap_args(const struct mmap_offset *t, /* Create, map and set data while the device is active. */ enable_one_screen_or_forcewake_get_and_wait(&ms_data); - handle = gem_create_in_memory_region_list(drm_fd, buf_size, mem_regions, 1); + handle = gem_create_in_memory_region_list(drm_fd, buf_size, 0, mem_regions, 1); gem_buf = __gem_mmap_offset(drm_fd, handle, 0, buf_size, PROT_READ | PROT_WRITE, t->type); @@ -1348,7 +1348,7 @@ static void gem_execbuf_subtest(struct drm_i915_gem_memory_class_instance *mem_r /* Create and set data while the device is active. */ enable_one_screen_or_forcewake_get_and_wait(&ms_data); - handle = gem_create_in_memory_region_list(drm_fd, dst_size, mem_regions, 1); + handle = gem_create_in_memory_region_list(drm_fd, dst_size, 0, mem_regions, 1); cpu_buf = malloc(dst_size); igt_assert(cpu_buf); @@ -1453,7 +1453,7 @@ gem_execbuf_stress_subtest(int rounds, int wait_flags, if (wait_flags & WAIT_PC8_RES) handle = gem_create(drm_fd, batch_size); else - handle = gem_create_in_memory_region_list(drm_fd, batch_size, mem_regions, 1); + handle = gem_create_in_memory_region_list(drm_fd, batch_size, 0, mem_regions, 1); gem_write(drm_fd, handle, 0, batch_buf, batch_size);