2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
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:
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
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
24 #include <linux/acpi.h>
28 static int init_vgpu_opregion(struct intel_vgpu *vgpu, u32 gpa)
30 void __iomem *host_va = vgpu->gvt->opregion.opregion_va;
34 if (WARN((vgpu_opregion(vgpu)->va),
35 "vgpu%d: opregion has been initialized already.\n",
39 vgpu_opregion(vgpu)->va = (void *)__get_free_pages(GFP_ATOMIC |
40 GFP_DMA32 | __GFP_ZERO,
41 INTEL_GVT_OPREGION_PORDER);
43 if (!vgpu_opregion(vgpu)->va)
46 memcpy_fromio(vgpu_opregion(vgpu)->va, host_va,
47 INTEL_GVT_OPREGION_SIZE);
49 for (i = 0; i < INTEL_GVT_OPREGION_PAGES; i++)
50 vgpu_opregion(vgpu)->gfn[i] = (gpa >> PAGE_SHIFT) + i;
52 /* for unknown reason, the value in LID field is incorrect
53 * which block the windows guest, so workaround it by force
54 * setting it to "OPEN"
56 buf = (u8 *)vgpu_opregion(vgpu)->va;
57 buf[INTEL_GVT_OPREGION_CLID] = 0x3;
62 static int map_vgpu_opregion(struct intel_vgpu *vgpu, bool map)
67 for (i = 0; i < INTEL_GVT_OPREGION_PAGES; i++) {
68 mfn = intel_gvt_hypervisor_virt_to_mfn(vgpu_opregion(vgpu)
70 if (mfn == INTEL_GVT_INVALID_ADDR) {
71 gvt_err("fail to get MFN from VA\n");
74 ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu,
75 vgpu_opregion(vgpu)->gfn[i],
76 mfn, 1, map, GVT_MAP_OPREGION);
78 gvt_err("fail to map GFN to MFN, errno: %d\n", ret);
86 * intel_vgpu_clean_opregion - clean the stuff used to emulate opregion
90 void intel_vgpu_clean_opregion(struct intel_vgpu *vgpu)
94 gvt_dbg_core("vgpu%d: clean vgpu opregion\n", vgpu->id);
96 if (!vgpu_opregion(vgpu)->va)
99 if (intel_gvt_host.hypervisor_type == INTEL_GVT_HYPERVISOR_KVM) {
100 vunmap(vgpu_opregion(vgpu)->va);
101 for (i = 0; i < INTEL_GVT_OPREGION_PAGES; i++) {
102 if (vgpu_opregion(vgpu)->pages[i]) {
103 put_page(vgpu_opregion(vgpu)->pages[i]);
104 vgpu_opregion(vgpu)->pages[i] = NULL;
108 map_vgpu_opregion(vgpu, false);
109 free_pages((unsigned long)vgpu_opregion(vgpu)->va,
110 INTEL_GVT_OPREGION_PORDER);
113 vgpu_opregion(vgpu)->va = NULL;
117 * intel_vgpu_init_opregion - initialize the stuff used to emulate opregion
119 * @gpa: guest physical address of opregion
122 * Zero on success, negative error code if failed.
124 int intel_vgpu_init_opregion(struct intel_vgpu *vgpu, u32 gpa)
128 gvt_dbg_core("vgpu%d: init vgpu opregion\n", vgpu->id);
130 if (intel_gvt_host.hypervisor_type == INTEL_GVT_HYPERVISOR_XEN) {
131 gvt_dbg_core("emulate opregion from kernel\n");
133 ret = init_vgpu_opregion(vgpu, gpa);
137 ret = map_vgpu_opregion(vgpu, true);
141 gvt_dbg_core("emulate opregion from userspace\n");
144 * If opregion pages are not allocated from host kenrel,
145 * most of the params are meaningless
147 ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu,
160 * intel_gvt_clean_opregion - clean host opergion related stuffs
164 void intel_gvt_clean_opregion(struct intel_gvt *gvt)
166 iounmap(gvt->opregion.opregion_va);
167 gvt->opregion.opregion_va = NULL;
171 * intel_gvt_init_opregion - initialize host opergion related stuffs
175 * Zero on success, negative error code if failed.
177 int intel_gvt_init_opregion(struct intel_gvt *gvt)
179 gvt_dbg_core("init host opregion\n");
181 pci_read_config_dword(gvt->dev_priv->drm.pdev, INTEL_GVT_PCI_OPREGION,
182 &gvt->opregion.opregion_pa);
184 gvt->opregion.opregion_va = acpi_os_ioremap(gvt->opregion.opregion_pa,
185 INTEL_GVT_OPREGION_SIZE);
186 if (!gvt->opregion.opregion_va) {
187 gvt_err("fail to map host opregion\n");
193 #define GVT_OPREGION_FUNC(scic) \
196 __ret = (scic & OPREGION_SCIC_FUNC_MASK) >> \
197 OPREGION_SCIC_FUNC_SHIFT; \
201 #define GVT_OPREGION_SUBFUNC(scic) \
204 __ret = (scic & OPREGION_SCIC_SUBFUNC_MASK) >> \
205 OPREGION_SCIC_SUBFUNC_SHIFT; \
209 static const char *opregion_func_name(u32 func)
211 const char *name = NULL;
221 name = "Get BIOS Data";
225 name = "System BIOS Callbacks";
235 static const char *opregion_subfunc_name(u32 subfunc)
237 const char *name = NULL;
241 name = "Supported Calls";
245 name = "Requested Callbacks";
254 name = "Boot Display";
258 name = "TV-Standard/Video-Connector";
262 name = "Internal Graphics";
266 name = "Spread Spectrum Clocks";
280 static bool querying_capabilities(u32 scic)
284 func = GVT_OPREGION_FUNC(scic);
285 subfunc = GVT_OPREGION_SUBFUNC(scic);
287 if ((func == INTEL_GVT_OPREGION_SCIC_F_GETBIOSDATA &&
288 subfunc == INTEL_GVT_OPREGION_SCIC_SF_SUPPRTEDCALLS)
289 || (func == INTEL_GVT_OPREGION_SCIC_F_GETBIOSDATA &&
290 subfunc == INTEL_GVT_OPREGION_SCIC_SF_REQEUSTEDCALLBACKS)
291 || (func == INTEL_GVT_OPREGION_SCIC_F_GETBIOSCALLBACKS &&
292 subfunc == INTEL_GVT_OPREGION_SCIC_SF_SUPPRTEDCALLS)) {
299 * intel_vgpu_emulate_opregion_request - emulating OpRegion request
301 * @swsci: SWSCI request
304 * Zero on success, negative error code if failed
306 int intel_vgpu_emulate_opregion_request(struct intel_vgpu *vgpu, u32 swsci)
311 scic = vgpu_opregion(vgpu)->va + INTEL_GVT_OPREGION_SCIC;
312 parm = vgpu_opregion(vgpu)->va + INTEL_GVT_OPREGION_PARM;
314 if (!(swsci & SWSCI_SCI_SELECT)) {
315 gvt_err("vgpu%d: requesting SMI service\n", vgpu->id);
318 /* ignore non 0->1 trasitions */
319 if ((vgpu_cfg_space(vgpu)[INTEL_GVT_PCI_SWSCI]
320 & SWSCI_SCI_TRIGGER) ||
321 !(swsci & SWSCI_SCI_TRIGGER)) {
325 func = GVT_OPREGION_FUNC(*scic);
326 subfunc = GVT_OPREGION_SUBFUNC(*scic);
327 if (!querying_capabilities(*scic)) {
328 gvt_err("vgpu%d: requesting runtime service: func \"%s\","
331 opregion_func_name(func),
332 opregion_subfunc_name(subfunc));
334 * emulate exit status of function call, '0' means
335 * "failure, generic, unsupported or unknown cause"
337 *scic &= ~OPREGION_SCIC_EXIT_MASK;