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 
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 	if (!i915_gem_object_has_struct_page(obj)) {
29 		err = -EINVAL;
30 		pr_err("shmem has no struct page\n");
31 		goto out_obj;
32 	}
33 
34 	i915_gem_object_lock(obj, NULL);
35 	err = i915_gem_object_attach_phys(obj, PAGE_SIZE);
36 	i915_gem_object_unlock(obj);
37 	if (err) {
38 		pr_err("i915_gem_object_attach_phys failed, err=%d\n", err);
39 		goto out_obj;
40 	}
41 
42 	if (i915_gem_object_has_struct_page(obj)) {
43 		pr_err("i915_gem_object_attach_phys did not create a phys object\n");
44 		err = -EINVAL;
45 		goto out_obj;
46 	}
47 
48 	if (!atomic_read(&obj->mm.pages_pin_count)) {
49 		pr_err("i915_gem_object_attach_phys did not pin its phys pages\n");
50 		err = -EINVAL;
51 		goto out_obj;
52 	}
53 
54 	/* Make the object dirty so that put_pages must do copy back the data */
55 	i915_gem_object_lock(obj, NULL);
56 	err = i915_gem_object_set_to_gtt_domain(obj, true);
57 	i915_gem_object_unlock(obj);
58 	if (err) {
59 		pr_err("i915_gem_object_set_to_gtt_domain failed with err=%d\n",
60 		       err);
61 		goto out_obj;
62 	}
63 
64 out_obj:
65 	i915_gem_object_put(obj);
66 out:
67 	return err;
68 }
69 
70 int i915_gem_phys_mock_selftests(void)
71 {
72 	static const struct i915_subtest tests[] = {
73 		SUBTEST(mock_phys_object),
74 	};
75 	struct drm_i915_private *i915;
76 	int err;
77 
78 	i915 = mock_gem_device();
79 	if (!i915)
80 		return -ENOMEM;
81 
82 	err = i915_subtests(tests, i915);
83 
84 	mock_destroy_device(i915);
85 	return err;
86 }
87