1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016 Intel Corporation
5  */
6 
7 #include "i915_selftest.h"
8 
9 #include "selftests/mock_gem_device.h"
10 
mock_phys_object(void * arg)11 static int mock_phys_object(void *arg)
12 {
13 	struct drm_i915_private *i915 = arg;
14 	struct drm_i915_gem_object *obj;
15 	int err;
16 
17 	/* Create an object and bind it to a contiguous set of physical pages,
18 	 * i.e. exercise the i915_gem_object_phys API.
19 	 */
20 
21 	obj = i915_gem_object_create_shmem(i915, PAGE_SIZE);
22 	if (IS_ERR(obj)) {
23 		err = PTR_ERR(obj);
24 		pr_err("i915_gem_object_create failed, err=%d\n", err);
25 		goto out;
26 	}
27 
28 	i915_gem_object_lock(obj, NULL);
29 	if (!i915_gem_object_has_struct_page(obj)) {
30 		i915_gem_object_unlock(obj);
31 		err = -EINVAL;
32 		pr_err("shmem has no struct page\n");
33 		goto out_obj;
34 	}
35 
36 	err = i915_gem_object_attach_phys(obj, PAGE_SIZE);
37 	i915_gem_object_unlock(obj);
38 	if (err) {
39 		pr_err("i915_gem_object_attach_phys failed, err=%d\n", err);
40 		goto out_obj;
41 	}
42 
43 	if (i915_gem_object_has_struct_page(obj)) {
44 		pr_err("i915_gem_object_attach_phys did not create a phys object\n");
45 		err = -EINVAL;
46 		goto out_obj;
47 	}
48 
49 	if (!atomic_read(&obj->mm.pages_pin_count)) {
50 		pr_err("i915_gem_object_attach_phys did not pin its phys pages\n");
51 		err = -EINVAL;
52 		goto out_obj;
53 	}
54 
55 	/* Make the object dirty so that put_pages must do copy back the data */
56 	i915_gem_object_lock(obj, NULL);
57 	err = i915_gem_object_set_to_gtt_domain(obj, true);
58 	i915_gem_object_unlock(obj);
59 	if (err) {
60 		pr_err("i915_gem_object_set_to_gtt_domain failed with err=%d\n",
61 		       err);
62 		goto out_obj;
63 	}
64 
65 out_obj:
66 	i915_gem_object_put(obj);
67 out:
68 	return err;
69 }
70 
i915_gem_phys_mock_selftests(void)71 int i915_gem_phys_mock_selftests(void)
72 {
73 	static const struct i915_subtest tests[] = {
74 		SUBTEST(mock_phys_object),
75 	};
76 	struct drm_i915_private *i915;
77 	int err;
78 
79 	i915 = mock_gem_device();
80 	if (!i915)
81 		return -ENOMEM;
82 
83 	err = i915_subtests(tests, i915);
84 
85 	mock_destroy_device(i915);
86 	return err;
87 }
88