blob: c6e063ec6c5d288b097ae0b755277cca1eecf253 [file] [log] [blame]
Zhi Wang0ad35fe2016-06-16 08:07:00 -04001/*
2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
Zhi Wang12d14cc2016-08-30 11:06:17 +080022 *
23 * Authors:
24 * Kevin Tian <kevin.tian@intel.com>
25 * Eddie Dong <eddie.dong@intel.com>
26 *
27 * Contributors:
28 * Niu Bing <bing.niu@intel.com>
29 * Zhi Wang <zhi.a.wang@intel.com>
30 *
Zhi Wang0ad35fe2016-06-16 08:07:00 -040031 */
32
33#include <linux/types.h>
34#include <xen/xen.h>
35
36#include "i915_drv.h"
37
38struct intel_gvt_host intel_gvt_host;
39
40static const char * const supported_hypervisors[] = {
41 [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
42 [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
43};
44
Zhi Wang4d60c5fd2016-07-20 01:14:38 -040045struct intel_gvt_io_emulation_ops intel_gvt_io_emulation_ops = {
46 .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
47 .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
48};
49
Zhi Wang0ad35fe2016-06-16 08:07:00 -040050/**
51 * intel_gvt_init_host - Load MPT modules and detect if we're running in host
52 * @gvt: intel gvt device
53 *
54 * This function is called at the driver loading stage. If failed to find a
55 * loadable MPT module or detect currently we're running in a VM, then GVT-g
56 * will be disabled
57 *
58 * Returns:
59 * Zero on success, negative error code if failed.
60 *
61 */
62int intel_gvt_init_host(void)
63{
64 if (intel_gvt_host.initialized)
65 return 0;
66
67 /* Xen DOM U */
68 if (xen_domain() && !xen_initial_domain())
69 return -ENODEV;
70
71 /* Try to load MPT modules for hypervisors */
72 if (xen_initial_domain()) {
73 /* In Xen dom0 */
74 intel_gvt_host.mpt = try_then_request_module(
75 symbol_get(xengt_mpt), "xengt");
76 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
77 } else {
78 /* not in Xen. Try KVMGT */
79 intel_gvt_host.mpt = try_then_request_module(
80 symbol_get(kvmgt_mpt), "kvm");
81 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
82 }
83
84 /* Fail to load MPT modules - bail out */
85 if (!intel_gvt_host.mpt)
86 return -EINVAL;
87
88 /* Try to detect if we're running in host instead of VM. */
89 if (!intel_gvt_hypervisor_detect_host())
90 return -ENODEV;
91
92 gvt_dbg_core("Running with hypervisor %s in host mode\n",
93 supported_hypervisors[intel_gvt_host.hypervisor_type]);
94
95 intel_gvt_host.initialized = true;
96 return 0;
97}
98
99static void init_device_info(struct intel_gvt *gvt)
100{
Zhi Wang12d14cc2016-08-30 11:06:17 +0800101 struct intel_gvt_device_info *info = &gvt->device_info;
102
103 if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)) {
104 info->max_support_vgpus = 8;
Zhi Wang579cea52016-06-30 12:45:34 -0400105 info->cfg_space_size = 256;
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800106 info->mmio_size = 2 * 1024 * 1024;
Zhi Wang579cea52016-06-30 12:45:34 -0400107 info->mmio_bar = 0;
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800108 info->msi_cap_offset = IS_SKYLAKE(gvt->dev_priv) ? 0xac : 0x90;
Zhi Wang2707e442016-03-28 23:23:16 +0800109 info->gtt_start_offset = 8 * 1024 * 1024;
110 info->gtt_entry_size = 8;
111 info->gtt_entry_size_shift = 3;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800112 }
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400113}
114
115/**
116 * intel_gvt_clean_device - clean a GVT device
117 * @gvt: intel gvt device
118 *
119 * This function is called at the driver unloading stage, to free the
120 * resources owned by a GVT device.
121 *
122 */
123void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
124{
125 struct intel_gvt *gvt = &dev_priv->gvt;
126
127 if (WARN_ON(!gvt->initialized))
128 return;
129
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400130 intel_gvt_clean_opregion(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800131 intel_gvt_clean_gtt(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800132 intel_gvt_clean_irq(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800133 intel_gvt_clean_mmio_info(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400134 intel_gvt_free_firmware(gvt);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400135
136 gvt->initialized = false;
137}
138
139/**
140 * intel_gvt_init_device - initialize a GVT device
141 * @dev_priv: drm i915 private data
142 *
143 * This function is called at the initialization stage, to initialize
144 * necessary GVT components.
145 *
146 * Returns:
147 * Zero on success, negative error code if failed.
148 *
149 */
150int intel_gvt_init_device(struct drm_i915_private *dev_priv)
151{
152 struct intel_gvt *gvt = &dev_priv->gvt;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800153 int ret;
154
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400155 /*
156 * Cannot initialize GVT device without intel_gvt_host gets
157 * initialized first.
158 */
159 if (WARN_ON(!intel_gvt_host.initialized))
160 return -EINVAL;
161
162 if (WARN_ON(gvt->initialized))
163 return -EEXIST;
164
165 gvt_dbg_core("init gvt device\n");
166
Zhi Wang28a60de2016-09-02 12:41:29 +0800167 mutex_init(&gvt->lock);
168 gvt->dev_priv = dev_priv;
169
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400170 init_device_info(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800171
172 ret = intel_gvt_setup_mmio_info(gvt);
173 if (ret)
174 return ret;
175
Zhi Wang579cea52016-06-30 12:45:34 -0400176 ret = intel_gvt_load_firmware(gvt);
177 if (ret)
178 goto out_clean_mmio_info;
179
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800180 ret = intel_gvt_init_irq(gvt);
181 if (ret)
182 goto out_free_firmware;
183
Zhi Wang2707e442016-03-28 23:23:16 +0800184 ret = intel_gvt_init_gtt(gvt);
185 if (ret)
186 goto out_clean_irq;
187
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400188 ret = intel_gvt_init_opregion(gvt);
189 if (ret)
190 goto out_clean_gtt;
191
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400192 gvt_dbg_core("gvt device creation is done\n");
193 gvt->initialized = true;
194 return 0;
Zhi Wang579cea52016-06-30 12:45:34 -0400195
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400196out_clean_gtt:
197 intel_gvt_clean_gtt(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800198out_clean_irq:
199 intel_gvt_clean_irq(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800200out_free_firmware:
201 intel_gvt_free_firmware(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400202out_clean_mmio_info:
203 intel_gvt_clean_mmio_info(gvt);
204 return ret;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400205}