1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/types.h>
34#include <linux/kthread.h>
35
36#include "i915_drv.h"
37#include "intel_gvt.h"
38#include "gvt.h"
39#include <linux/vfio.h>
40#include <linux/mdev.h>
41
42struct intel_gvt_host intel_gvt_host;
43
44static const char * const supported_hypervisors[] = {
45 [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
46 [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
47};
48
49static const struct intel_gvt_ops intel_gvt_ops = {
50 .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
51 .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
52 .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
53 .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
54 .vgpu_create = intel_gvt_create_vgpu,
55 .vgpu_destroy = intel_gvt_destroy_vgpu,
56 .vgpu_release = intel_gvt_release_vgpu,
57 .vgpu_reset = intel_gvt_reset_vgpu,
58 .vgpu_activate = intel_gvt_activate_vgpu,
59 .vgpu_deactivate = intel_gvt_deactivate_vgpu,
60 .vgpu_query_plane = intel_vgpu_query_plane,
61 .vgpu_get_dmabuf = intel_vgpu_get_dmabuf,
62 .write_protect_handler = intel_vgpu_page_track_handler,
63 .emulate_hotplug = intel_vgpu_emulate_hotplug,
64};
65
66static void init_device_info(struct intel_gvt *gvt)
67{
68 struct intel_gvt_device_info *info = &gvt->device_info;
69 struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
70
71 info->max_support_vgpus = 8;
72 info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
73 info->mmio_size = 2 * 1024 * 1024;
74 info->mmio_bar = 0;
75 info->gtt_start_offset = 8 * 1024 * 1024;
76 info->gtt_entry_size = 8;
77 info->gtt_entry_size_shift = 3;
78 info->gmadr_bytes_in_cmd = 8;
79 info->max_surface_size = 36 * 1024 * 1024;
80 info->msi_cap_offset = pdev->msi_cap;
81}
82
83static void intel_gvt_test_and_emulate_vblank(struct intel_gvt *gvt)
84{
85 struct intel_vgpu *vgpu;
86 int id;
87
88 mutex_lock(&gvt->lock);
89 idr_for_each_entry((&(gvt)->vgpu_idr), (vgpu), (id)) {
90 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK + id,
91 (void *)&gvt->service_request)) {
92 if (vgpu->active)
93 intel_vgpu_emulate_vblank(vgpu);
94 }
95 }
96 mutex_unlock(&gvt->lock);
97}
98
99static int gvt_service_thread(void *data)
100{
101 struct intel_gvt *gvt = (struct intel_gvt *)data;
102 int ret;
103
104 gvt_dbg_core("service thread start\n");
105
106 while (!kthread_should_stop()) {
107 ret = wait_event_interruptible(gvt->service_thread_wq,
108 kthread_should_stop() || gvt->service_request);
109
110 if (kthread_should_stop())
111 break;
112
113 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
114 continue;
115
116 intel_gvt_test_and_emulate_vblank(gvt);
117
118 if (test_bit(INTEL_GVT_REQUEST_SCHED,
119 (void *)&gvt->service_request) ||
120 test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
121 (void *)&gvt->service_request)) {
122 intel_gvt_schedule(gvt);
123 }
124 }
125
126 return 0;
127}
128
129static void clean_service_thread(struct intel_gvt *gvt)
130{
131 kthread_stop(gvt->service_thread);
132}
133
134static int init_service_thread(struct intel_gvt *gvt)
135{
136 init_waitqueue_head(&gvt->service_thread_wq);
137
138 gvt->service_thread = kthread_run(gvt_service_thread,
139 gvt, "gvt_service_thread");
140 if (IS_ERR(gvt->service_thread)) {
141 gvt_err("fail to start service thread.\n");
142 return PTR_ERR(gvt->service_thread);
143 }
144 return 0;
145}
146
147
148
149
150
151
152
153
154
155void intel_gvt_clean_device(struct drm_i915_private *i915)
156{
157 struct intel_gvt *gvt = fetch_and_zero(&i915->gvt);
158
159 if (drm_WARN_ON(&i915->drm, !gvt))
160 return;
161
162 intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
163 intel_gvt_clean_vgpu_types(gvt);
164
165 intel_gvt_debugfs_clean(gvt);
166 clean_service_thread(gvt);
167 intel_gvt_clean_cmd_parser(gvt);
168 intel_gvt_clean_sched_policy(gvt);
169 intel_gvt_clean_workload_scheduler(gvt);
170 intel_gvt_clean_gtt(gvt);
171 intel_gvt_free_firmware(gvt);
172 intel_gvt_clean_mmio_info(gvt);
173 idr_destroy(&gvt->vgpu_idr);
174
175 kfree(i915->gvt);
176}
177
178
179
180
181
182
183
184
185
186
187
188
189int intel_gvt_init_device(struct drm_i915_private *i915)
190{
191 struct intel_gvt *gvt;
192 struct intel_vgpu *vgpu;
193 int ret;
194
195 if (drm_WARN_ON(&i915->drm, i915->gvt))
196 return -EEXIST;
197
198 gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
199 if (!gvt)
200 return -ENOMEM;
201
202 gvt_dbg_core("init gvt device\n");
203
204 idr_init_base(&gvt->vgpu_idr, 1);
205 spin_lock_init(&gvt->scheduler.mmio_context_lock);
206 mutex_init(&gvt->lock);
207 mutex_init(&gvt->sched_lock);
208 gvt->gt = &i915->gt;
209 i915->gvt = gvt;
210
211 init_device_info(gvt);
212
213 ret = intel_gvt_setup_mmio_info(gvt);
214 if (ret)
215 goto out_clean_idr;
216
217 intel_gvt_init_engine_mmio_context(gvt);
218
219 ret = intel_gvt_load_firmware(gvt);
220 if (ret)
221 goto out_clean_mmio_info;
222
223 ret = intel_gvt_init_irq(gvt);
224 if (ret)
225 goto out_free_firmware;
226
227 ret = intel_gvt_init_gtt(gvt);
228 if (ret)
229 goto out_free_firmware;
230
231 ret = intel_gvt_init_workload_scheduler(gvt);
232 if (ret)
233 goto out_clean_gtt;
234
235 ret = intel_gvt_init_sched_policy(gvt);
236 if (ret)
237 goto out_clean_workload_scheduler;
238
239 ret = intel_gvt_init_cmd_parser(gvt);
240 if (ret)
241 goto out_clean_sched_policy;
242
243 ret = init_service_thread(gvt);
244 if (ret)
245 goto out_clean_cmd_parser;
246
247 ret = intel_gvt_init_vgpu_types(gvt);
248 if (ret)
249 goto out_clean_thread;
250
251 vgpu = intel_gvt_create_idle_vgpu(gvt);
252 if (IS_ERR(vgpu)) {
253 ret = PTR_ERR(vgpu);
254 gvt_err("failed to create idle vgpu\n");
255 goto out_clean_types;
256 }
257 gvt->idle_vgpu = vgpu;
258
259 intel_gvt_debugfs_init(gvt);
260
261 gvt_dbg_core("gvt device initialization is done\n");
262 intel_gvt_host.dev = i915->drm.dev;
263 intel_gvt_host.initialized = true;
264 return 0;
265
266out_clean_types:
267 intel_gvt_clean_vgpu_types(gvt);
268out_clean_thread:
269 clean_service_thread(gvt);
270out_clean_cmd_parser:
271 intel_gvt_clean_cmd_parser(gvt);
272out_clean_sched_policy:
273 intel_gvt_clean_sched_policy(gvt);
274out_clean_workload_scheduler:
275 intel_gvt_clean_workload_scheduler(gvt);
276out_clean_gtt:
277 intel_gvt_clean_gtt(gvt);
278out_free_firmware:
279 intel_gvt_free_firmware(gvt);
280out_clean_mmio_info:
281 intel_gvt_clean_mmio_info(gvt);
282out_clean_idr:
283 idr_destroy(&gvt->vgpu_idr);
284 kfree(gvt);
285 i915->gvt = NULL;
286 return ret;
287}
288
289int
290intel_gvt_pm_resume(struct intel_gvt *gvt)
291{
292 intel_gvt_restore_fence(gvt);
293 intel_gvt_restore_mmio(gvt);
294 intel_gvt_restore_ggtt(gvt);
295 return 0;
296}
297
298int
299intel_gvt_register_hypervisor(const struct intel_gvt_mpt *m)
300{
301 int ret;
302 void *gvt;
303
304 if (!intel_gvt_host.initialized)
305 return -ENODEV;
306
307 if (m->type != INTEL_GVT_HYPERVISOR_KVM &&
308 m->type != INTEL_GVT_HYPERVISOR_XEN)
309 return -EINVAL;
310
311
312 if (!try_module_get(THIS_MODULE))
313 return -ENODEV;
314
315 intel_gvt_host.mpt = m;
316 intel_gvt_host.hypervisor_type = m->type;
317 gvt = (void *)kdev_to_i915(intel_gvt_host.dev)->gvt;
318
319 ret = intel_gvt_hypervisor_host_init(intel_gvt_host.dev, gvt,
320 &intel_gvt_ops);
321 if (ret < 0) {
322 gvt_err("Failed to init %s hypervisor module\n",
323 supported_hypervisors[intel_gvt_host.hypervisor_type]);
324 module_put(THIS_MODULE);
325 return -ENODEV;
326 }
327 gvt_dbg_core("Running with hypervisor %s in host mode\n",
328 supported_hypervisors[intel_gvt_host.hypervisor_type]);
329 return 0;
330}
331EXPORT_SYMBOL_GPL(intel_gvt_register_hypervisor);
332
333void
334intel_gvt_unregister_hypervisor(void)
335{
336 void *gvt = (void *)kdev_to_i915(intel_gvt_host.dev)->gvt;
337 intel_gvt_hypervisor_host_exit(intel_gvt_host.dev, gvt);
338 module_put(THIS_MODULE);
339}
340EXPORT_SYMBOL_GPL(intel_gvt_unregister_hypervisor);
341