/*
* 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 <linux/prime_numbers.h>
#include "gem/i915_gem_context.h"
#include "gem/selftests/mock_context.h"
#include "i915_scatterlist.h"
#include "i915_selftest.h"
#include "mock_gem_device.h"
#include "mock_gtt.h"
static bool assert_vma(struct i915_vma *vma,
struct drm_i915_gem_object *obj,
struct i915_gem_context *ctx)
{
bool ok = true;
if (vma->vm != rcu_access_pointer(ctx->vm)) {
pr_err("VMA created with wrong VM\n");
ok = false;
}
if (vma->size != obj->base.size) {
pr_err("VMA created with wrong size, found %llu, expected %zu\n",
vma->size, obj->base.size);
ok = false;
}
if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) {
pr_err("VMA created with wrong type [%d]\n",
vma->ggtt_view.type);
ok = false;
}
return ok;
}
static struct i915_vma *
checked_vma_instance(struct drm_i915_gem_object *obj,
struct i915_address_space *vm,
const struct i915_ggtt_view *view)
{
struct i915_vma *vma;
bool ok = true;
vma = i915_vma_instance(obj, vm, view);
if (IS_ERR(vma))
return vma;
/* Manual checks, will be reinforced by i915_vma_compare! */
if (vma->vm != vm) {
pr_err("VMA's vm [%p] does not match request [%p]\n",
vma->vm, vm);
ok = false;
}
if (i915_is_ggtt(vm) != i915_vma_is_ggtt(vma)) {
pr_err("VMA ggtt status [%d] does not match parent [%d]\n",
i915_vma_is_ggtt(vma), i915_is_ggtt(vm));
ok = false;
}
if (i915_vma_compare(vma, vm, view)) {
pr_err("i915_vma_compare failed with create parameters!\n");
return ERR_PTR(-EINVAL);
}
if (i915_vma_compare(vma, vma->vm,
i915_vma_is_ggtt(vma) ? &vma->ggtt_view : NULL)) {
pr_err("i915_vma_compare failed with itself\n");
return ERR_PTR(-EINVAL);
}
if (!ok) {
pr_err("i915_vma_compare failed to detect the difference!\n");
return ERR_PTR(-EINVAL);
}
return vma;
}
static int create_vmas(struct drm_i915_private *i915,
struct list_head *objects,
struct list_head *contexts)
{
struct drm_i915_gem_object *obj;
struct i915_gem_context *ctx;
int pinned;
list_for_each_entry(obj, objects, st_link) {
for (pinned = 0; pinned <= 1; pinned++) {
list_for_each_entry(ctx, contexts, link) {
struct i915_address_space *vm;
struct i915_vma *vma;
int err;
vm = i915_gem_context_get_vm_rcu(ctx);
vma = checked_vma_instance(obj, vm, NULL);
i915_vm_put(vm);
if (IS_ERR(vma))
return PTR_ERR(vma);
if (!assert_vma(vma, obj, ctx)) {
pr_err("VMA lookup/create failed\n");
return -EINVAL;
}
if (!pinned) {
err = i915_vma_pin(vma, 0, 0, PIN_USER);
if (err) {
pr_err("Failed to pin VMA\n");
return err;
}
} else {
i915_vma_unpin(vma);
}
}
}
}
return 0;
}
static int igt_vma_create(void *arg)
{
struct i915_ggtt *ggtt = arg;
struct drm_i915_private *i915 = ggtt->vm.i915;
struct drm_i915_gem_object *obj, *