Merge tag 'pm-extra-4.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / i915 / gvt / gvt.c
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.
22  */
23
24 #include <linux/types.h>
25 #include <xen/xen.h>
26
27 #include "i915_drv.h"
28
29 struct intel_gvt_host intel_gvt_host;
30
31 static const char * const supported_hypervisors[] = {
32         [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
33         [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
34 };
35
36 /**
37  * intel_gvt_init_host - Load MPT modules and detect if we're running in host
38  * @gvt: intel gvt device
39  *
40  * This function is called at the driver loading stage. If failed to find a
41  * loadable MPT module or detect currently we're running in a VM, then GVT-g
42  * will be disabled
43  *
44  * Returns:
45  * Zero on success, negative error code if failed.
46  *
47  */
48 int intel_gvt_init_host(void)
49 {
50         if (intel_gvt_host.initialized)
51                 return 0;
52
53         /* Xen DOM U */
54         if (xen_domain() && !xen_initial_domain())
55                 return -ENODEV;
56
57         /* Try to load MPT modules for hypervisors */
58         if (xen_initial_domain()) {
59                 /* In Xen dom0 */
60                 intel_gvt_host.mpt = try_then_request_module(
61                                 symbol_get(xengt_mpt), "xengt");
62                 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
63         } else {
64                 /* not in Xen. Try KVMGT */
65                 intel_gvt_host.mpt = try_then_request_module(
66                                 symbol_get(kvmgt_mpt), "kvm");
67                 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
68         }
69
70         /* Fail to load MPT modules - bail out */
71         if (!intel_gvt_host.mpt)
72                 return -EINVAL;
73
74         /* Try to detect if we're running in host instead of VM. */
75         if (!intel_gvt_hypervisor_detect_host())
76                 return -ENODEV;
77
78         gvt_dbg_core("Running with hypervisor %s in host mode\n",
79                         supported_hypervisors[intel_gvt_host.hypervisor_type]);
80
81         intel_gvt_host.initialized = true;
82         return 0;
83 }
84
85 static void init_device_info(struct intel_gvt *gvt)
86 {
87         if (IS_BROADWELL(gvt->dev_priv))
88                 gvt->device_info.max_support_vgpus = 8;
89         /* This function will grow large in GVT device model patches. */
90 }
91
92 /**
93  * intel_gvt_clean_device - clean a GVT device
94  * @gvt: intel gvt device
95  *
96  * This function is called at the driver unloading stage, to free the
97  * resources owned by a GVT device.
98  *
99  */
100 void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
101 {
102         struct intel_gvt *gvt = &dev_priv->gvt;
103
104         if (WARN_ON(!gvt->initialized))
105                 return;
106
107         /* Other de-initialization of GVT components will be introduced. */
108
109         gvt->initialized = false;
110 }
111
112 /**
113  * intel_gvt_init_device - initialize a GVT device
114  * @dev_priv: drm i915 private data
115  *
116  * This function is called at the initialization stage, to initialize
117  * necessary GVT components.
118  *
119  * Returns:
120  * Zero on success, negative error code if failed.
121  *
122  */
123 int intel_gvt_init_device(struct drm_i915_private *dev_priv)
124 {
125         struct intel_gvt *gvt = &dev_priv->gvt;
126         /*
127          * Cannot initialize GVT device without intel_gvt_host gets
128          * initialized first.
129          */
130         if (WARN_ON(!intel_gvt_host.initialized))
131                 return -EINVAL;
132
133         if (WARN_ON(gvt->initialized))
134                 return -EEXIST;
135
136         gvt_dbg_core("init gvt device\n");
137
138         init_device_info(gvt);
139         /*
140          * Other initialization of GVT components will be introduce here.
141          */
142         gvt_dbg_core("gvt device creation is done\n");
143         gvt->initialized = true;
144         return 0;
145 }