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