Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 1 | /* |
| 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 Wang | 12d14cc | 2016-08-30 11:06:17 +0800 | [diff] [blame] | 22 | * |
| 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 Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include <linux/types.h> |
| 34 | #include <xen/xen.h> |
| 35 | |
| 36 | #include "i915_drv.h" |
| 37 | |
| 38 | struct intel_gvt_host intel_gvt_host; |
| 39 | |
| 40 | static const char * const supported_hypervisors[] = { |
| 41 | [INTEL_GVT_HYPERVISOR_XEN] = "XEN", |
| 42 | [INTEL_GVT_HYPERVISOR_KVM] = "KVM", |
| 43 | }; |
| 44 | |
Zhi Wang | 4d60c5fd | 2016-07-20 01:14:38 -0400 | [diff] [blame] | 45 | struct 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, |
Zhi Wang | e39c5ad | 2016-09-02 13:33:29 +0800 | [diff] [blame^] | 48 | .emulate_mmio_read = intel_vgpu_emulate_mmio_read, |
| 49 | .emulate_mmio_write = intel_vgpu_emulate_mmio_write, |
Zhi Wang | 4d60c5fd | 2016-07-20 01:14:38 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 52 | /** |
| 53 | * intel_gvt_init_host - Load MPT modules and detect if we're running in host |
| 54 | * @gvt: intel gvt device |
| 55 | * |
| 56 | * This function is called at the driver loading stage. If failed to find a |
| 57 | * loadable MPT module or detect currently we're running in a VM, then GVT-g |
| 58 | * will be disabled |
| 59 | * |
| 60 | * Returns: |
| 61 | * Zero on success, negative error code if failed. |
| 62 | * |
| 63 | */ |
| 64 | int intel_gvt_init_host(void) |
| 65 | { |
| 66 | if (intel_gvt_host.initialized) |
| 67 | return 0; |
| 68 | |
| 69 | /* Xen DOM U */ |
| 70 | if (xen_domain() && !xen_initial_domain()) |
| 71 | return -ENODEV; |
| 72 | |
| 73 | /* Try to load MPT modules for hypervisors */ |
| 74 | if (xen_initial_domain()) { |
| 75 | /* In Xen dom0 */ |
| 76 | intel_gvt_host.mpt = try_then_request_module( |
| 77 | symbol_get(xengt_mpt), "xengt"); |
| 78 | intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN; |
| 79 | } else { |
| 80 | /* not in Xen. Try KVMGT */ |
| 81 | intel_gvt_host.mpt = try_then_request_module( |
| 82 | symbol_get(kvmgt_mpt), "kvm"); |
| 83 | intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM; |
| 84 | } |
| 85 | |
| 86 | /* Fail to load MPT modules - bail out */ |
| 87 | if (!intel_gvt_host.mpt) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | /* Try to detect if we're running in host instead of VM. */ |
| 91 | if (!intel_gvt_hypervisor_detect_host()) |
| 92 | return -ENODEV; |
| 93 | |
| 94 | gvt_dbg_core("Running with hypervisor %s in host mode\n", |
| 95 | supported_hypervisors[intel_gvt_host.hypervisor_type]); |
| 96 | |
| 97 | intel_gvt_host.initialized = true; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void init_device_info(struct intel_gvt *gvt) |
| 102 | { |
Zhi Wang | 12d14cc | 2016-08-30 11:06:17 +0800 | [diff] [blame] | 103 | struct intel_gvt_device_info *info = &gvt->device_info; |
| 104 | |
| 105 | if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)) { |
| 106 | info->max_support_vgpus = 8; |
Zhi Wang | 579cea5 | 2016-06-30 12:45:34 -0400 | [diff] [blame] | 107 | info->cfg_space_size = 256; |
Zhi Wang | c8fe6a68 | 2015-09-17 09:22:08 +0800 | [diff] [blame] | 108 | info->mmio_size = 2 * 1024 * 1024; |
Zhi Wang | 579cea5 | 2016-06-30 12:45:34 -0400 | [diff] [blame] | 109 | info->mmio_bar = 0; |
Zhi Wang | c8fe6a68 | 2015-09-17 09:22:08 +0800 | [diff] [blame] | 110 | info->msi_cap_offset = IS_SKYLAKE(gvt->dev_priv) ? 0xac : 0x90; |
Zhi Wang | 2707e44 | 2016-03-28 23:23:16 +0800 | [diff] [blame] | 111 | info->gtt_start_offset = 8 * 1024 * 1024; |
| 112 | info->gtt_entry_size = 8; |
| 113 | info->gtt_entry_size_shift = 3; |
Zhi Wang | 12d14cc | 2016-08-30 11:06:17 +0800 | [diff] [blame] | 114 | } |
Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * intel_gvt_clean_device - clean a GVT device |
| 119 | * @gvt: intel gvt device |
| 120 | * |
| 121 | * This function is called at the driver unloading stage, to free the |
| 122 | * resources owned by a GVT device. |
| 123 | * |
| 124 | */ |
| 125 | void intel_gvt_clean_device(struct drm_i915_private *dev_priv) |
| 126 | { |
| 127 | struct intel_gvt *gvt = &dev_priv->gvt; |
| 128 | |
| 129 | if (WARN_ON(!gvt->initialized)) |
| 130 | return; |
| 131 | |
Zhi Wang | 4d60c5fd | 2016-07-20 01:14:38 -0400 | [diff] [blame] | 132 | intel_gvt_clean_opregion(gvt); |
Zhi Wang | 2707e44 | 2016-03-28 23:23:16 +0800 | [diff] [blame] | 133 | intel_gvt_clean_gtt(gvt); |
Zhi Wang | c8fe6a68 | 2015-09-17 09:22:08 +0800 | [diff] [blame] | 134 | intel_gvt_clean_irq(gvt); |
Zhi Wang | 12d14cc | 2016-08-30 11:06:17 +0800 | [diff] [blame] | 135 | intel_gvt_clean_mmio_info(gvt); |
Zhi Wang | 579cea5 | 2016-06-30 12:45:34 -0400 | [diff] [blame] | 136 | intel_gvt_free_firmware(gvt); |
Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 137 | |
| 138 | gvt->initialized = false; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * intel_gvt_init_device - initialize a GVT device |
| 143 | * @dev_priv: drm i915 private data |
| 144 | * |
| 145 | * This function is called at the initialization stage, to initialize |
| 146 | * necessary GVT components. |
| 147 | * |
| 148 | * Returns: |
| 149 | * Zero on success, negative error code if failed. |
| 150 | * |
| 151 | */ |
| 152 | int intel_gvt_init_device(struct drm_i915_private *dev_priv) |
| 153 | { |
| 154 | struct intel_gvt *gvt = &dev_priv->gvt; |
Zhi Wang | 12d14cc | 2016-08-30 11:06:17 +0800 | [diff] [blame] | 155 | int ret; |
| 156 | |
Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 157 | /* |
| 158 | * Cannot initialize GVT device without intel_gvt_host gets |
| 159 | * initialized first. |
| 160 | */ |
| 161 | if (WARN_ON(!intel_gvt_host.initialized)) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | if (WARN_ON(gvt->initialized)) |
| 165 | return -EEXIST; |
| 166 | |
| 167 | gvt_dbg_core("init gvt device\n"); |
| 168 | |
Zhi Wang | 28a60de | 2016-09-02 12:41:29 +0800 | [diff] [blame] | 169 | mutex_init(&gvt->lock); |
| 170 | gvt->dev_priv = dev_priv; |
| 171 | |
Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 172 | init_device_info(gvt); |
Zhi Wang | 12d14cc | 2016-08-30 11:06:17 +0800 | [diff] [blame] | 173 | |
| 174 | ret = intel_gvt_setup_mmio_info(gvt); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | |
Zhi Wang | 579cea5 | 2016-06-30 12:45:34 -0400 | [diff] [blame] | 178 | ret = intel_gvt_load_firmware(gvt); |
| 179 | if (ret) |
| 180 | goto out_clean_mmio_info; |
| 181 | |
Zhi Wang | c8fe6a68 | 2015-09-17 09:22:08 +0800 | [diff] [blame] | 182 | ret = intel_gvt_init_irq(gvt); |
| 183 | if (ret) |
| 184 | goto out_free_firmware; |
| 185 | |
Zhi Wang | 2707e44 | 2016-03-28 23:23:16 +0800 | [diff] [blame] | 186 | ret = intel_gvt_init_gtt(gvt); |
| 187 | if (ret) |
| 188 | goto out_clean_irq; |
| 189 | |
Zhi Wang | 4d60c5fd | 2016-07-20 01:14:38 -0400 | [diff] [blame] | 190 | ret = intel_gvt_init_opregion(gvt); |
| 191 | if (ret) |
| 192 | goto out_clean_gtt; |
| 193 | |
Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 194 | gvt_dbg_core("gvt device creation is done\n"); |
| 195 | gvt->initialized = true; |
| 196 | return 0; |
Zhi Wang | 579cea5 | 2016-06-30 12:45:34 -0400 | [diff] [blame] | 197 | |
Zhi Wang | 4d60c5fd | 2016-07-20 01:14:38 -0400 | [diff] [blame] | 198 | out_clean_gtt: |
| 199 | intel_gvt_clean_gtt(gvt); |
Zhi Wang | 2707e44 | 2016-03-28 23:23:16 +0800 | [diff] [blame] | 200 | out_clean_irq: |
| 201 | intel_gvt_clean_irq(gvt); |
Zhi Wang | c8fe6a68 | 2015-09-17 09:22:08 +0800 | [diff] [blame] | 202 | out_free_firmware: |
| 203 | intel_gvt_free_firmware(gvt); |
Zhi Wang | 579cea5 | 2016-06-30 12:45:34 -0400 | [diff] [blame] | 204 | out_clean_mmio_info: |
| 205 | intel_gvt_clean_mmio_info(gvt); |
| 206 | return ret; |
Zhi Wang | 0ad35fe | 2016-06-16 08:07:00 -0400 | [diff] [blame] | 207 | } |