1/*	$NetBSD: mock_gtt.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $	*/
2
3/*
4 * Copyright �� 2016 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: mock_gtt.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $");
29
30#include "mock_gtt.h"
31
32static void mock_insert_page(struct i915_address_space *vm,
33			     dma_addr_t addr,
34			     u64 offset,
35			     enum i915_cache_level level,
36			     u32 flags)
37{
38}
39
40static void mock_insert_entries(struct i915_address_space *vm,
41				struct i915_vma *vma,
42				enum i915_cache_level level, u32 flags)
43{
44}
45
46static int mock_bind_ppgtt(struct i915_vma *vma,
47			   enum i915_cache_level cache_level,
48			   u32 flags)
49{
50	GEM_BUG_ON(flags & I915_VMA_GLOBAL_BIND);
51	set_bit(I915_VMA_LOCAL_BIND_BIT, __i915_vma_flags(vma));
52	return 0;
53}
54
55static void mock_unbind_ppgtt(struct i915_vma *vma)
56{
57}
58
59static void mock_cleanup(struct i915_address_space *vm)
60{
61}
62
63static void mock_clear_range(struct i915_address_space *vm,
64			     u64 start, u64 length)
65{
66}
67
68struct i915_ppgtt *mock_ppgtt(struct drm_i915_private *i915, const char *name)
69{
70	struct i915_ppgtt *ppgtt;
71
72	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
73	if (!ppgtt)
74		return NULL;
75
76	ppgtt->vm.gt = &i915->gt;
77	ppgtt->vm.i915 = i915;
78	ppgtt->vm.total = round_down(U64_MAX, PAGE_SIZE);
79	ppgtt->vm.file = ERR_PTR(-ENODEV);
80
81	i915_address_space_init(&ppgtt->vm, VM_CLASS_PPGTT);
82
83	ppgtt->vm.clear_range = mock_clear_range;
84	ppgtt->vm.insert_page = mock_insert_page;
85	ppgtt->vm.insert_entries = mock_insert_entries;
86	ppgtt->vm.cleanup = mock_cleanup;
87
88	ppgtt->vm.vma_ops.bind_vma    = mock_bind_ppgtt;
89	ppgtt->vm.vma_ops.unbind_vma  = mock_unbind_ppgtt;
90	ppgtt->vm.vma_ops.set_pages   = ppgtt_set_pages;
91	ppgtt->vm.vma_ops.clear_pages = clear_pages;
92
93	return ppgtt;
94}
95
96static int mock_bind_ggtt(struct i915_vma *vma,
97			  enum i915_cache_level cache_level,
98			  u32 flags)
99{
100	atomic_or(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND, &vma->flags);
101	return 0;
102}
103
104static void mock_unbind_ggtt(struct i915_vma *vma)
105{
106}
107
108void mock_init_ggtt(struct drm_i915_private *i915, struct i915_ggtt *ggtt)
109{
110	memset(ggtt, 0, sizeof(*ggtt));
111
112	ggtt->vm.gt = &i915->gt;
113	ggtt->vm.i915 = i915;
114	ggtt->vm.is_ggtt = true;
115
116	ggtt->gmadr = (struct resource) DEFINE_RES_MEM(0, 2048 * PAGE_SIZE);
117	ggtt->mappable_end = resource_size(&ggtt->gmadr);
118	ggtt->vm.total = 4096 * PAGE_SIZE;
119
120	ggtt->vm.clear_range = mock_clear_range;
121	ggtt->vm.insert_page = mock_insert_page;
122	ggtt->vm.insert_entries = mock_insert_entries;
123	ggtt->vm.cleanup = mock_cleanup;
124
125	ggtt->vm.vma_ops.bind_vma    = mock_bind_ggtt;
126	ggtt->vm.vma_ops.unbind_vma  = mock_unbind_ggtt;
127	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
128	ggtt->vm.vma_ops.clear_pages = clear_pages;
129
130	i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
131	i915->gt.ggtt = ggtt;
132}
133
134void mock_fini_ggtt(struct i915_ggtt *ggtt)
135{
136	i915_address_space_fini(&ggtt->vm);
137}
138