forked from Minki/linux
a679f58d05
When we return pages to the system, we ensure that they are marked as being in the CPU domain since any external access is uncontrolled and we must assume the worst. This means that we need to always flush the pages on acquisition if we need to use them on the GPU, and from the beginning have used set-domain. Set-domain is overkill for the purpose as it is a general synchronisation barrier, but our intent is to only flush the pages being swapped in. If we move that flush into the pages acquisition phase, we know then that when we have obj->mm.pages, they are coherent with the GPU and need only maintain that status without resorting to heavy handed use of set-domain. The principle knock-on effect for userspace is through mmap-gtt pagefaulting. Our uAPI has always implied that the GTT mmap was async (especially as when any pagefault occurs is unpredicatable to userspace) and so userspace had to apply explicit domain control itself (set-domain). However, swapping is transparent to the kernel, and so on first fault we need to acquire the pages and make them coherent for access through the GTT. Our use of set-domain here leaks into the uABI that the first pagefault was synchronous. This is unintentional and baring a few igt should be unoticed, nevertheless we bump the uABI version for mmap-gtt to reflect the change in behaviour. Another implication of the change is that gem_create() is presumed to create an object that is coherent with the CPU and is in the CPU write domain, so a set-domain(CPU) following a gem_create() would be a minor operation that merely checked whether we could allocate all pages for the object. On applying this change, a set-domain(CPU) causes a clflush as we acquire the pages. This will have a small impact on mesa as we move the clflush here on !llc from execbuf time to create, but that should have minimal performance impact as the same clflush exists but is now done early and because of the clflush issue, userspace recycles bo and so should resist allocating fresh objects. Internally, the presumption that objects are created in the CPU write-domain and remain so through writes to obj->mm.mapping is more prevalent than I expected; but easy enough to catch and apply a manual flush. For the future, we should push the page flush from the central set_pages() into the callers so that we can more finely control when it is applied, but for now doing it one location is easier to validate, at the cost of sometimes flushing when there is no need. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Matthew Auld <matthew.william.auld@gmail.com> Cc: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com> Cc: Antonio Argenziano <antonio.argenziano@intel.com> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com> Reviewed-by: Matthew Auld <matthew.william.auld@gmail.com> Link: https://patchwork.freedesktop.org/patch/msgid/20190321161908.8007-1-chris@chris-wilson.co.uk
405 lines
9.1 KiB
C
405 lines
9.1 KiB
C
/*
|
|
* Copyright © 2016 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.
|
|
*
|
|
*/
|
|
|
|
#include "../i915_selftest.h"
|
|
|
|
#include "mock_gem_device.h"
|
|
#include "mock_dmabuf.h"
|
|
|
|
static int igt_dmabuf_export(void *arg)
|
|
{
|
|
struct drm_i915_private *i915 = arg;
|
|
struct drm_i915_gem_object *obj;
|
|
struct dma_buf *dmabuf;
|
|
|
|
obj = i915_gem_object_create(i915, PAGE_SIZE);
|
|
if (IS_ERR(obj))
|
|
return PTR_ERR(obj);
|
|
|
|
dmabuf = i915_gem_prime_export(&i915->drm, &obj->base, 0);
|
|
i915_gem_object_put(obj);
|
|
if (IS_ERR(dmabuf)) {
|
|
pr_err("i915_gem_prime_export failed with err=%d\n",
|
|
(int)PTR_ERR(dmabuf));
|
|
return PTR_ERR(dmabuf);
|
|
}
|
|
|
|
dma_buf_put(dmabuf);
|
|
return 0;
|
|
}
|
|
|
|
static int igt_dmabuf_import_self(void *arg)
|
|
{
|
|
struct drm_i915_private *i915 = arg;
|
|
struct drm_i915_gem_object *obj;
|
|
struct drm_gem_object *import;
|
|
struct dma_buf *dmabuf;
|
|
int err;
|
|
|
|
obj = i915_gem_object_create(i915, PAGE_SIZE);
|
|
if (IS_ERR(obj))
|
|
return PTR_ERR(obj);
|
|
|
|
dmabuf = i915_gem_prime_export(&i915->drm, &obj->base, 0);
|
|
if (IS_ERR(dmabuf)) {
|
|
pr_err("i915_gem_prime_export failed with err=%d\n",
|
|
(int)PTR_ERR(dmabuf));
|
|
err = PTR_ERR(dmabuf);
|
|
goto out;
|
|
}
|
|
|
|
import = i915_gem_prime_import(&i915->drm, dmabuf);
|
|
if (IS_ERR(import)) {
|
|
pr_err("i915_gem_prime_import failed with err=%d\n",
|
|
(int)PTR_ERR(import));
|
|
err = PTR_ERR(import);
|
|
goto out_dmabuf;
|
|
}
|
|
|
|
if (import != &obj->base) {
|
|
pr_err("i915_gem_prime_import created a new object!\n");
|
|
err = -EINVAL;
|
|
goto out_import;
|
|
}
|
|
|
|
err = 0;
|
|
out_import:
|
|
i915_gem_object_put(to_intel_bo(import));
|
|
out_dmabuf:
|
|
dma_buf_put(dmabuf);
|
|
out:
|
|
i915_gem_object_put(obj);
|
|
return err;
|
|
}
|
|
|
|
static int igt_dmabuf_import(void *arg)
|
|
{
|
|
struct drm_i915_private *i915 = arg;
|
|
struct drm_i915_gem_object *obj;
|
|
struct dma_buf *dmabuf;
|
|
void *obj_map, *dma_map;
|
|
u32 pattern[] = { 0, 0xaa, 0xcc, 0x55, 0xff };
|
|
int err, i;
|
|
|
|
dmabuf = mock_dmabuf(1);
|
|
if (IS_ERR(dmabuf))
|
|
return PTR_ERR(dmabuf);
|
|
|
|
obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf));
|
|
if (IS_ERR(obj)) {
|
|
pr_err("i915_gem_prime_import failed with err=%d\n",
|
|
(int)PTR_ERR(obj));
|
|
err = PTR_ERR(obj);
|
|
goto out_dmabuf;
|
|
}
|
|
|
|
if (obj->base.dev != &i915->drm) {
|
|
pr_err("i915_gem_prime_import created a non-i915 object!\n");
|
|
err = -EINVAL;
|
|
goto out_obj;
|
|
}
|
|
|
|
if (obj->base.size != PAGE_SIZE) {
|
|
pr_err("i915_gem_prime_import is wrong size found %lld, expected %ld\n",
|
|
(long long)obj->base.size, PAGE_SIZE);
|
|
err = -EINVAL;
|
|
goto out_obj;
|
|
}
|
|
|
|
dma_map = dma_buf_vmap(dmabuf);
|
|
if (!dma_map) {
|
|
pr_err("dma_buf_vmap failed\n");
|
|
err = -ENOMEM;
|
|
goto out_obj;
|
|
}
|
|
|
|
if (0) { /* Can not yet map dmabuf */
|
|
obj_map = i915_gem_object_pin_map(obj, I915_MAP_WB);
|
|
if (IS_ERR(obj_map)) {
|
|
err = PTR_ERR(obj_map);
|
|
pr_err("i915_gem_object_pin_map failed with err=%d\n", err);
|
|
goto out_dma_map;
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pattern); i++) {
|
|
memset(dma_map, pattern[i], PAGE_SIZE);
|
|
if (memchr_inv(obj_map, pattern[i], PAGE_SIZE)) {
|
|
err = -EINVAL;
|
|
pr_err("imported vmap not all set to %x!\n", pattern[i]);
|
|
i915_gem_object_unpin_map(obj);
|
|
goto out_dma_map;
|
|
}
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pattern); i++) {
|
|
memset(obj_map, pattern[i], PAGE_SIZE);
|
|
if (memchr_inv(dma_map, pattern[i], PAGE_SIZE)) {
|
|
err = -EINVAL;
|
|
pr_err("exported vmap not all set to %x!\n", pattern[i]);
|
|
i915_gem_object_unpin_map(obj);
|
|
goto out_dma_map;
|
|
}
|
|
}
|
|
|
|
i915_gem_object_unpin_map(obj);
|
|
}
|
|
|
|
err = 0;
|
|
out_dma_map:
|
|
dma_buf_vunmap(dmabuf, dma_map);
|
|
out_obj:
|
|
i915_gem_object_put(obj);
|
|
out_dmabuf:
|
|
dma_buf_put(dmabuf);
|
|
return err;
|
|
}
|
|
|
|
static int igt_dmabuf_import_ownership(void *arg)
|
|
{
|
|
struct drm_i915_private *i915 = arg;
|
|
struct drm_i915_gem_object *obj;
|
|
struct dma_buf *dmabuf;
|
|
void *ptr;
|
|
int err;
|
|
|
|
dmabuf = mock_dmabuf(1);
|
|
if (IS_ERR(dmabuf))
|
|
return PTR_ERR(dmabuf);
|
|
|
|
ptr = dma_buf_vmap(dmabuf);
|
|
if (!ptr) {
|
|
pr_err("dma_buf_vmap failed\n");
|
|
err = -ENOMEM;
|
|
goto err_dmabuf;
|
|
}
|
|
|
|
memset(ptr, 0xc5, PAGE_SIZE);
|
|
dma_buf_vunmap(dmabuf, ptr);
|
|
|
|
obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf));
|
|
if (IS_ERR(obj)) {
|
|
pr_err("i915_gem_prime_import failed with err=%d\n",
|
|
(int)PTR_ERR(obj));
|
|
err = PTR_ERR(obj);
|
|
goto err_dmabuf;
|
|
}
|
|
|
|
dma_buf_put(dmabuf);
|
|
|
|
err = i915_gem_object_pin_pages(obj);
|
|
if (err) {
|
|
pr_err("i915_gem_object_pin_pages failed with err=%d\n", err);
|
|
goto out_obj;
|
|
}
|
|
|
|
err = 0;
|
|
i915_gem_object_unpin_pages(obj);
|
|
out_obj:
|
|
i915_gem_object_put(obj);
|
|
return err;
|
|
|
|
err_dmabuf:
|
|
dma_buf_put(dmabuf);
|
|
return err;
|
|
}
|
|
|
|
static int igt_dmabuf_export_vmap(void *arg)
|
|
{
|
|
struct drm_i915_private *i915 = arg;
|
|
struct drm_i915_gem_object *obj;
|
|
struct dma_buf *dmabuf;
|
|
void *ptr;
|
|
int err;
|
|
|
|
obj = i915_gem_object_create(i915, PAGE_SIZE);
|
|
if (IS_ERR(obj))
|
|
return PTR_ERR(obj);
|
|
|
|
dmabuf = i915_gem_prime_export(&i915->drm, &obj->base, 0);
|
|
if (IS_ERR(dmabuf)) {
|
|
pr_err("i915_gem_prime_export failed with err=%d\n",
|
|
(int)PTR_ERR(dmabuf));
|
|
err = PTR_ERR(dmabuf);
|
|
goto err_obj;
|
|
}
|
|
i915_gem_object_put(obj);
|
|
|
|
ptr = dma_buf_vmap(dmabuf);
|
|
if (!ptr) {
|
|
pr_err("dma_buf_vmap failed\n");
|
|
err = -ENOMEM;
|
|
goto out;
|
|
}
|
|
|
|
if (memchr_inv(ptr, 0, dmabuf->size)) {
|
|
pr_err("Exported object not initialiased to zero!\n");
|
|
err = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
memset(ptr, 0xc5, dmabuf->size);
|
|
|
|
err = 0;
|
|
dma_buf_vunmap(dmabuf, ptr);
|
|
out:
|
|
dma_buf_put(dmabuf);
|
|
return err;
|
|
|
|
err_obj:
|
|
i915_gem_object_put(obj);
|
|
return err;
|
|
}
|
|
|
|
static int igt_dmabuf_export_kmap(void *arg)
|
|
{
|
|
struct drm_i915_private *i915 = arg;
|
|
struct drm_i915_gem_object *obj;
|
|
struct dma_buf *dmabuf;
|
|
void *ptr;
|
|
int err;
|
|
|
|
obj = i915_gem_object_create(i915, 2*PAGE_SIZE);
|
|
if (IS_ERR(obj))
|
|
return PTR_ERR(obj);
|
|
|
|
dmabuf = i915_gem_prime_export(&i915->drm, &obj->base, 0);
|
|
i915_gem_object_put(obj);
|
|
if (IS_ERR(dmabuf)) {
|
|
err = PTR_ERR(dmabuf);
|
|
pr_err("i915_gem_prime_export failed with err=%d\n", err);
|
|
return err;
|
|
}
|
|
|
|
ptr = dma_buf_kmap(dmabuf, 0);
|
|
if (!ptr) {
|
|
pr_err("dma_buf_kmap failed\n");
|
|
err = -ENOMEM;
|
|
goto err;
|
|
}
|
|
|
|
if (memchr_inv(ptr, 0, PAGE_SIZE)) {
|
|
dma_buf_kunmap(dmabuf, 0, ptr);
|
|
pr_err("Exported page[0] not initialiased to zero!\n");
|
|
err = -EINVAL;
|
|
goto err;
|
|
}
|
|
|
|
memset(ptr, 0xc5, PAGE_SIZE);
|
|
dma_buf_kunmap(dmabuf, 0, ptr);
|
|
|
|
ptr = i915_gem_object_pin_map(obj, I915_MAP_WB);
|
|
if (IS_ERR(ptr)) {
|
|
err = PTR_ERR(ptr);
|
|
pr_err("i915_gem_object_pin_map failed with err=%d\n", err);
|
|
goto err;
|
|
}
|
|
memset(ptr + PAGE_SIZE, 0xaa, PAGE_SIZE);
|
|
i915_gem_object_flush_map(obj);
|
|
i915_gem_object_unpin_map(obj);
|
|
|
|
ptr = dma_buf_kmap(dmabuf, 1);
|
|
if (!ptr) {
|
|
pr_err("dma_buf_kmap failed\n");
|
|
err = -ENOMEM;
|
|
goto err;
|
|
}
|
|
|
|
if (memchr_inv(ptr, 0xaa, PAGE_SIZE)) {
|
|
dma_buf_kunmap(dmabuf, 1, ptr);
|
|
pr_err("Exported page[1] not set to 0xaa!\n");
|
|
err = -EINVAL;
|
|
goto err;
|
|
}
|
|
|
|
memset(ptr, 0xc5, PAGE_SIZE);
|
|
dma_buf_kunmap(dmabuf, 1, ptr);
|
|
|
|
ptr = dma_buf_kmap(dmabuf, 0);
|
|
if (!ptr) {
|
|
pr_err("dma_buf_kmap failed\n");
|
|
err = -ENOMEM;
|
|
goto err;
|
|
}
|
|
if (memchr_inv(ptr, 0xc5, PAGE_SIZE)) {
|
|
dma_buf_kunmap(dmabuf, 0, ptr);
|
|
pr_err("Exported page[0] did not retain 0xc5!\n");
|
|
err = -EINVAL;
|
|
goto err;
|
|
}
|
|
dma_buf_kunmap(dmabuf, 0, ptr);
|
|
|
|
ptr = dma_buf_kmap(dmabuf, 2);
|
|
if (ptr) {
|
|
pr_err("Erroneously kmapped beyond the end of the object!\n");
|
|
dma_buf_kunmap(dmabuf, 2, ptr);
|
|
err = -EINVAL;
|
|
goto err;
|
|
}
|
|
|
|
ptr = dma_buf_kmap(dmabuf, -1);
|
|
if (ptr) {
|
|
pr_err("Erroneously kmapped before the start of the object!\n");
|
|
dma_buf_kunmap(dmabuf, -1, ptr);
|
|
err = -EINVAL;
|
|
goto err;
|
|
}
|
|
|
|
err = 0;
|
|
err:
|
|
dma_buf_put(dmabuf);
|
|
return err;
|
|
}
|
|
|
|
int i915_gem_dmabuf_mock_selftests(void)
|
|
{
|
|
static const struct i915_subtest tests[] = {
|
|
SUBTEST(igt_dmabuf_export),
|
|
SUBTEST(igt_dmabuf_import_self),
|
|
SUBTEST(igt_dmabuf_import),
|
|
SUBTEST(igt_dmabuf_import_ownership),
|
|
SUBTEST(igt_dmabuf_export_vmap),
|
|
SUBTEST(igt_dmabuf_export_kmap),
|
|
};
|
|
struct drm_i915_private *i915;
|
|
int err;
|
|
|
|
i915 = mock_gem_device();
|
|
if (!i915)
|
|
return -ENOMEM;
|
|
|
|
err = i915_subtests(tests, i915);
|
|
|
|
drm_dev_put(&i915->drm);
|
|
return err;
|
|
}
|
|
|
|
int i915_gem_dmabuf_live_selftests(struct drm_i915_private *i915)
|
|
{
|
|
static const struct i915_subtest tests[] = {
|
|
SUBTEST(igt_dmabuf_export),
|
|
};
|
|
|
|
return i915_subtests(tests, i915);
|
|
}
|