Home | History | Annotate | Line # | Download | only in gvt
      1  1.1  riastrad /*	$NetBSD: opregion.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22  1.1  riastrad  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  1.1  riastrad  * SOFTWARE.
     24  1.1  riastrad  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: opregion.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/acpi.h>
     30  1.1  riastrad #include "i915_drv.h"
     31  1.1  riastrad #include "gvt.h"
     32  1.1  riastrad 
     33  1.1  riastrad /*
     34  1.1  riastrad  * Note: Only for GVT-g virtual VBT generation, other usage must
     35  1.1  riastrad  * not do like this.
     36  1.1  riastrad  */
     37  1.1  riastrad #define _INTEL_BIOS_PRIVATE
     38  1.1  riastrad #include "display/intel_vbt_defs.h"
     39  1.1  riastrad 
     40  1.1  riastrad #define OPREGION_SIGNATURE "IntelGraphicsMem"
     41  1.1  riastrad #define MBOX_VBT      (1<<3)
     42  1.1  riastrad 
     43  1.1  riastrad /* device handle */
     44  1.1  riastrad #define DEVICE_TYPE_CRT    0x01
     45  1.1  riastrad #define DEVICE_TYPE_EFP1   0x04
     46  1.1  riastrad #define DEVICE_TYPE_EFP2   0x40
     47  1.1  riastrad #define DEVICE_TYPE_EFP3   0x20
     48  1.1  riastrad #define DEVICE_TYPE_EFP4   0x10
     49  1.1  riastrad 
     50  1.1  riastrad struct opregion_header {
     51  1.1  riastrad 	u8 signature[16];
     52  1.1  riastrad 	u32 size;
     53  1.1  riastrad 	u32 opregion_ver;
     54  1.1  riastrad 	u8 bios_ver[32];
     55  1.1  riastrad 	u8 vbios_ver[16];
     56  1.1  riastrad 	u8 driver_ver[16];
     57  1.1  riastrad 	u32 mboxes;
     58  1.1  riastrad 	u32 driver_model;
     59  1.1  riastrad 	u32 pcon;
     60  1.1  riastrad 	u8 dver[32];
     61  1.1  riastrad 	u8 rsvd[124];
     62  1.1  riastrad } __packed;
     63  1.1  riastrad 
     64  1.1  riastrad struct bdb_data_header {
     65  1.1  riastrad 	u8 id;
     66  1.1  riastrad 	u16 size; /* data size */
     67  1.1  riastrad } __packed;
     68  1.1  riastrad 
     69  1.1  riastrad /* For supporting windows guest with opregion, here hardcode the emulated
     70  1.1  riastrad  * bdb header version as '186', and the corresponding child_device_config
     71  1.1  riastrad  * length should be '33' but not '38'.
     72  1.1  riastrad  */
     73  1.1  riastrad struct efp_child_device_config {
     74  1.1  riastrad 	u16 handle;
     75  1.1  riastrad 	u16 device_type;
     76  1.1  riastrad 	u16 device_class;
     77  1.1  riastrad 	u8 i2c_speed;
     78  1.1  riastrad 	u8 dp_onboard_redriver; /* 158 */
     79  1.1  riastrad 	u8 dp_ondock_redriver; /* 158 */
     80  1.1  riastrad 	u8 hdmi_level_shifter_value:4; /* 169 */
     81  1.1  riastrad 	u8 hdmi_max_data_rate:4; /* 204 */
     82  1.1  riastrad 	u16 dtd_buf_ptr; /* 161 */
     83  1.1  riastrad 	u8 edidless_efp:1; /* 161 */
     84  1.1  riastrad 	u8 compression_enable:1; /* 198 */
     85  1.1  riastrad 	u8 compression_method:1; /* 198 */
     86  1.1  riastrad 	u8 ganged_edp:1; /* 202 */
     87  1.1  riastrad 	u8 skip0:4;
     88  1.1  riastrad 	u8 compression_structure_index:4; /* 198 */
     89  1.1  riastrad 	u8 skip1:4;
     90  1.1  riastrad 	u8 slave_port; /*  202 */
     91  1.1  riastrad 	u8 skip2;
     92  1.1  riastrad 	u8 dvo_port;
     93  1.1  riastrad 	u8 i2c_pin; /* for add-in card */
     94  1.1  riastrad 	u8 slave_addr; /* for add-in card */
     95  1.1  riastrad 	u8 ddc_pin;
     96  1.1  riastrad 	u16 edid_ptr;
     97  1.1  riastrad 	u8 dvo_config;
     98  1.1  riastrad 	u8 efp_docked_port:1; /* 158 */
     99  1.1  riastrad 	u8 lane_reversal:1; /* 184 */
    100  1.1  riastrad 	u8 onboard_lspcon:1; /* 192 */
    101  1.1  riastrad 	u8 iboost_enable:1; /* 196 */
    102  1.1  riastrad 	u8 hpd_invert:1; /* BXT 196 */
    103  1.1  riastrad 	u8 slip3:3;
    104  1.1  riastrad 	u8 hdmi_compat:1;
    105  1.1  riastrad 	u8 dp_compat:1;
    106  1.1  riastrad 	u8 tmds_compat:1;
    107  1.1  riastrad 	u8 skip4:5;
    108  1.1  riastrad 	u8 aux_channel;
    109  1.1  riastrad 	u8 dongle_detect;
    110  1.1  riastrad 	u8 pipe_cap:2;
    111  1.1  riastrad 	u8 sdvo_stall:1; /* 158 */
    112  1.1  riastrad 	u8 hpd_status:2;
    113  1.1  riastrad 	u8 integrated_encoder:1;
    114  1.1  riastrad 	u8 skip5:2;
    115  1.1  riastrad 	u8 dvo_wiring;
    116  1.1  riastrad 	u8 mipi_bridge_type; /* 171 */
    117  1.1  riastrad 	u16 device_class_ext;
    118  1.1  riastrad 	u8 dvo_function;
    119  1.1  riastrad } __packed;
    120  1.1  riastrad 
    121  1.1  riastrad struct vbt {
    122  1.1  riastrad 	/* header->bdb_offset point to bdb_header offset */
    123  1.1  riastrad 	struct vbt_header header;
    124  1.1  riastrad 	struct bdb_header bdb_header;
    125  1.1  riastrad 
    126  1.1  riastrad 	struct bdb_data_header general_features_header;
    127  1.1  riastrad 	struct bdb_general_features general_features;
    128  1.1  riastrad 
    129  1.1  riastrad 	struct bdb_data_header general_definitions_header;
    130  1.1  riastrad 	struct bdb_general_definitions general_definitions;
    131  1.1  riastrad 
    132  1.1  riastrad 	struct efp_child_device_config child0;
    133  1.1  riastrad 	struct efp_child_device_config child1;
    134  1.1  riastrad 	struct efp_child_device_config child2;
    135  1.1  riastrad 	struct efp_child_device_config child3;
    136  1.1  riastrad 
    137  1.1  riastrad 	struct bdb_data_header driver_features_header;
    138  1.1  riastrad 	struct bdb_driver_features driver_features;
    139  1.1  riastrad };
    140  1.1  riastrad 
    141  1.1  riastrad static void virt_vbt_generation(struct vbt *v)
    142  1.1  riastrad {
    143  1.1  riastrad 	int num_child;
    144  1.1  riastrad 
    145  1.1  riastrad 	memset(v, 0, sizeof(struct vbt));
    146  1.1  riastrad 
    147  1.1  riastrad 	v->header.signature[0] = '$';
    148  1.1  riastrad 	v->header.signature[1] = 'V';
    149  1.1  riastrad 	v->header.signature[2] = 'B';
    150  1.1  riastrad 	v->header.signature[3] = 'T';
    151  1.1  riastrad 
    152  1.1  riastrad 	/* there's features depending on version! */
    153  1.1  riastrad 	v->header.version = 155;
    154  1.1  riastrad 	v->header.header_size = sizeof(v->header);
    155  1.1  riastrad 	v->header.vbt_size = sizeof(struct vbt) - sizeof(v->header);
    156  1.1  riastrad 	v->header.bdb_offset = offsetof(struct vbt, bdb_header);
    157  1.1  riastrad 
    158  1.1  riastrad 	strcpy(&v->bdb_header.signature[0], "BIOS_DATA_BLOCK");
    159  1.1  riastrad 	v->bdb_header.version = 186; /* child_dev_size = 33 */
    160  1.1  riastrad 	v->bdb_header.header_size = sizeof(v->bdb_header);
    161  1.1  riastrad 
    162  1.1  riastrad 	v->bdb_header.bdb_size = sizeof(struct vbt) - sizeof(struct vbt_header)
    163  1.1  riastrad 		- sizeof(struct bdb_header);
    164  1.1  riastrad 
    165  1.1  riastrad 	/* general features */
    166  1.1  riastrad 	v->general_features_header.id = BDB_GENERAL_FEATURES;
    167  1.1  riastrad 	v->general_features_header.size = sizeof(struct bdb_general_features);
    168  1.1  riastrad 	v->general_features.int_crt_support = 0;
    169  1.1  riastrad 	v->general_features.int_tv_support = 0;
    170  1.1  riastrad 
    171  1.1  riastrad 	/* child device */
    172  1.1  riastrad 	num_child = 4; /* each port has one child */
    173  1.1  riastrad 	v->general_definitions.child_dev_size =
    174  1.1  riastrad 		sizeof(struct efp_child_device_config);
    175  1.1  riastrad 	v->general_definitions_header.id = BDB_GENERAL_DEFINITIONS;
    176  1.1  riastrad 	/* size will include child devices */
    177  1.1  riastrad 	v->general_definitions_header.size =
    178  1.1  riastrad 		sizeof(struct bdb_general_definitions) +
    179  1.1  riastrad 			num_child * v->general_definitions.child_dev_size;
    180  1.1  riastrad 
    181  1.1  riastrad 	/* portA */
    182  1.1  riastrad 	v->child0.handle = DEVICE_TYPE_EFP1;
    183  1.1  riastrad 	v->child0.device_type = DEVICE_TYPE_DP;
    184  1.1  riastrad 	v->child0.dvo_port = DVO_PORT_DPA;
    185  1.1  riastrad 	v->child0.aux_channel = DP_AUX_A;
    186  1.1  riastrad 	v->child0.dp_compat = true;
    187  1.1  riastrad 	v->child0.integrated_encoder = true;
    188  1.1  riastrad 
    189  1.1  riastrad 	/* portB */
    190  1.1  riastrad 	v->child1.handle = DEVICE_TYPE_EFP2;
    191  1.1  riastrad 	v->child1.device_type = DEVICE_TYPE_DP;
    192  1.1  riastrad 	v->child1.dvo_port = DVO_PORT_DPB;
    193  1.1  riastrad 	v->child1.aux_channel = DP_AUX_B;
    194  1.1  riastrad 	v->child1.dp_compat = true;
    195  1.1  riastrad 	v->child1.integrated_encoder = true;
    196  1.1  riastrad 
    197  1.1  riastrad 	/* portC */
    198  1.1  riastrad 	v->child2.handle = DEVICE_TYPE_EFP3;
    199  1.1  riastrad 	v->child2.device_type = DEVICE_TYPE_DP;
    200  1.1  riastrad 	v->child2.dvo_port = DVO_PORT_DPC;
    201  1.1  riastrad 	v->child2.aux_channel = DP_AUX_C;
    202  1.1  riastrad 	v->child2.dp_compat = true;
    203  1.1  riastrad 	v->child2.integrated_encoder = true;
    204  1.1  riastrad 
    205  1.1  riastrad 	/* portD */
    206  1.1  riastrad 	v->child3.handle = DEVICE_TYPE_EFP4;
    207  1.1  riastrad 	v->child3.device_type = DEVICE_TYPE_DP;
    208  1.1  riastrad 	v->child3.dvo_port = DVO_PORT_DPD;
    209  1.1  riastrad 	v->child3.aux_channel = DP_AUX_D;
    210  1.1  riastrad 	v->child3.dp_compat = true;
    211  1.1  riastrad 	v->child3.integrated_encoder = true;
    212  1.1  riastrad 
    213  1.1  riastrad 	/* driver features */
    214  1.1  riastrad 	v->driver_features_header.id = BDB_DRIVER_FEATURES;
    215  1.1  riastrad 	v->driver_features_header.size = sizeof(struct bdb_driver_features);
    216  1.1  riastrad 	v->driver_features.lvds_config = BDB_DRIVER_FEATURE_NO_LVDS;
    217  1.1  riastrad }
    218  1.1  riastrad 
    219  1.1  riastrad /**
    220  1.1  riastrad  * intel_vgpu_init_opregion - initialize the stuff used to emulate opregion
    221  1.1  riastrad  * @vgpu: a vGPU
    222  1.1  riastrad  *
    223  1.1  riastrad  * Returns:
    224  1.1  riastrad  * Zero on success, negative error code if failed.
    225  1.1  riastrad  */
    226  1.1  riastrad int intel_vgpu_init_opregion(struct intel_vgpu *vgpu)
    227  1.1  riastrad {
    228  1.1  riastrad 	u8 *buf;
    229  1.1  riastrad 	struct opregion_header *header;
    230  1.1  riastrad 	struct vbt v;
    231  1.1  riastrad 	const char opregion_signature[16] = OPREGION_SIGNATURE;
    232  1.1  riastrad 
    233  1.1  riastrad 	gvt_dbg_core("init vgpu%d opregion\n", vgpu->id);
    234  1.1  riastrad 	vgpu_opregion(vgpu)->va = (void *)__get_free_pages(GFP_KERNEL |
    235  1.1  riastrad 			__GFP_ZERO,
    236  1.1  riastrad 			get_order(INTEL_GVT_OPREGION_SIZE));
    237  1.1  riastrad 	if (!vgpu_opregion(vgpu)->va) {
    238  1.1  riastrad 		gvt_err("fail to get memory for vgpu virt opregion\n");
    239  1.1  riastrad 		return -ENOMEM;
    240  1.1  riastrad 	}
    241  1.1  riastrad 
    242  1.1  riastrad 	/* emulated opregion with VBT mailbox only */
    243  1.1  riastrad 	buf = (u8 *)vgpu_opregion(vgpu)->va;
    244  1.1  riastrad 	header = (struct opregion_header *)buf;
    245  1.1  riastrad 	memcpy(header->signature, opregion_signature,
    246  1.1  riastrad 	       sizeof(opregion_signature));
    247  1.1  riastrad 	header->size = 0x8;
    248  1.1  riastrad 	header->opregion_ver = 0x02000000;
    249  1.1  riastrad 	header->mboxes = MBOX_VBT;
    250  1.1  riastrad 
    251  1.1  riastrad 	/* for unknown reason, the value in LID field is incorrect
    252  1.1  riastrad 	 * which block the windows guest, so workaround it by force
    253  1.1  riastrad 	 * setting it to "OPEN"
    254  1.1  riastrad 	 */
    255  1.1  riastrad 	buf[INTEL_GVT_OPREGION_CLID] = 0x3;
    256  1.1  riastrad 
    257  1.1  riastrad 	/* emulated vbt from virt vbt generation */
    258  1.1  riastrad 	virt_vbt_generation(&v);
    259  1.1  riastrad 	memcpy(buf + INTEL_GVT_OPREGION_VBT_OFFSET, &v, sizeof(struct vbt));
    260  1.1  riastrad 
    261  1.1  riastrad 	return 0;
    262  1.1  riastrad }
    263  1.1  riastrad 
    264  1.1  riastrad static int map_vgpu_opregion(struct intel_vgpu *vgpu, bool map)
    265  1.1  riastrad {
    266  1.1  riastrad 	u64 mfn;
    267  1.1  riastrad 	int i, ret;
    268  1.1  riastrad 
    269  1.1  riastrad 	for (i = 0; i < INTEL_GVT_OPREGION_PAGES; i++) {
    270  1.1  riastrad 		mfn = intel_gvt_hypervisor_virt_to_mfn(vgpu_opregion(vgpu)->va
    271  1.1  riastrad 			+ i * PAGE_SIZE);
    272  1.1  riastrad 		if (mfn == INTEL_GVT_INVALID_ADDR) {
    273  1.1  riastrad 			gvt_vgpu_err("fail to get MFN from VA\n");
    274  1.1  riastrad 			return -EINVAL;
    275  1.1  riastrad 		}
    276  1.1  riastrad 		ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu,
    277  1.1  riastrad 				vgpu_opregion(vgpu)->gfn[i],
    278  1.1  riastrad 				mfn, 1, map);
    279  1.1  riastrad 		if (ret) {
    280  1.1  riastrad 			gvt_vgpu_err("fail to map GFN to MFN, errno: %d\n",
    281  1.1  riastrad 				ret);
    282  1.1  riastrad 			return ret;
    283  1.1  riastrad 		}
    284  1.1  riastrad 	}
    285  1.1  riastrad 
    286  1.1  riastrad 	vgpu_opregion(vgpu)->mapped = map;
    287  1.1  riastrad 
    288  1.1  riastrad 	return 0;
    289  1.1  riastrad }
    290  1.1  riastrad 
    291  1.1  riastrad /**
    292  1.1  riastrad  * intel_vgpu_opregion_base_write_handler - Opregion base register write handler
    293  1.1  riastrad  *
    294  1.1  riastrad  * @vgpu: a vGPU
    295  1.1  riastrad  * @gpa: guest physical address of opregion
    296  1.1  riastrad  *
    297  1.1  riastrad  * Returns:
    298  1.1  riastrad  * Zero on success, negative error code if failed.
    299  1.1  riastrad  */
    300  1.1  riastrad int intel_vgpu_opregion_base_write_handler(struct intel_vgpu *vgpu, u32 gpa)
    301  1.1  riastrad {
    302  1.1  riastrad 
    303  1.1  riastrad 	int i, ret = 0;
    304  1.1  riastrad 
    305  1.1  riastrad 	gvt_dbg_core("emulate opregion from kernel\n");
    306  1.1  riastrad 
    307  1.1  riastrad 	switch (intel_gvt_host.hypervisor_type) {
    308  1.1  riastrad 	case INTEL_GVT_HYPERVISOR_KVM:
    309  1.1  riastrad 		for (i = 0; i < INTEL_GVT_OPREGION_PAGES; i++)
    310  1.1  riastrad 			vgpu_opregion(vgpu)->gfn[i] = (gpa >> PAGE_SHIFT) + i;
    311  1.1  riastrad 		break;
    312  1.1  riastrad 	case INTEL_GVT_HYPERVISOR_XEN:
    313  1.1  riastrad 		/**
    314  1.1  riastrad 		 * Wins guest on Xengt will write this register twice: xen
    315  1.1  riastrad 		 * hvmloader and windows graphic driver.
    316  1.1  riastrad 		 */
    317  1.1  riastrad 		if (vgpu_opregion(vgpu)->mapped)
    318  1.1  riastrad 			map_vgpu_opregion(vgpu, false);
    319  1.1  riastrad 
    320  1.1  riastrad 		for (i = 0; i < INTEL_GVT_OPREGION_PAGES; i++)
    321  1.1  riastrad 			vgpu_opregion(vgpu)->gfn[i] = (gpa >> PAGE_SHIFT) + i;
    322  1.1  riastrad 
    323  1.1  riastrad 		ret = map_vgpu_opregion(vgpu, true);
    324  1.1  riastrad 		break;
    325  1.1  riastrad 	default:
    326  1.1  riastrad 		ret = -EINVAL;
    327  1.1  riastrad 		gvt_vgpu_err("not supported hypervisor\n");
    328  1.1  riastrad 	}
    329  1.1  riastrad 
    330  1.1  riastrad 	return ret;
    331  1.1  riastrad }
    332  1.1  riastrad 
    333  1.1  riastrad /**
    334  1.1  riastrad  * intel_vgpu_clean_opregion - clean the stuff used to emulate opregion
    335  1.1  riastrad  * @vgpu: a vGPU
    336  1.1  riastrad  *
    337  1.1  riastrad  */
    338  1.1  riastrad void intel_vgpu_clean_opregion(struct intel_vgpu *vgpu)
    339  1.1  riastrad {
    340  1.1  riastrad 	gvt_dbg_core("vgpu%d: clean vgpu opregion\n", vgpu->id);
    341  1.1  riastrad 
    342  1.1  riastrad 	if (!vgpu_opregion(vgpu)->va)
    343  1.1  riastrad 		return;
    344  1.1  riastrad 
    345  1.1  riastrad 	if (intel_gvt_host.hypervisor_type == INTEL_GVT_HYPERVISOR_XEN) {
    346  1.1  riastrad 		if (vgpu_opregion(vgpu)->mapped)
    347  1.1  riastrad 			map_vgpu_opregion(vgpu, false);
    348  1.1  riastrad 	} else if (intel_gvt_host.hypervisor_type == INTEL_GVT_HYPERVISOR_KVM) {
    349  1.1  riastrad 		/* Guest opregion is released by VFIO */
    350  1.1  riastrad 	}
    351  1.1  riastrad 	free_pages((unsigned long)vgpu_opregion(vgpu)->va,
    352  1.1  riastrad 		   get_order(INTEL_GVT_OPREGION_SIZE));
    353  1.1  riastrad 
    354  1.1  riastrad 	vgpu_opregion(vgpu)->va = NULL;
    355  1.1  riastrad 
    356  1.1  riastrad }
    357  1.1  riastrad 
    358  1.1  riastrad 
    359  1.1  riastrad #define GVT_OPREGION_FUNC(scic)					\
    360  1.1  riastrad 	({							\
    361  1.1  riastrad 	 u32 __ret;						\
    362  1.1  riastrad 	 __ret = (scic & OPREGION_SCIC_FUNC_MASK) >>		\
    363  1.1  riastrad 	 OPREGION_SCIC_FUNC_SHIFT;				\
    364  1.1  riastrad 	 __ret;							\
    365  1.1  riastrad 	 })
    366  1.1  riastrad 
    367  1.1  riastrad #define GVT_OPREGION_SUBFUNC(scic)				\
    368  1.1  riastrad 	({							\
    369  1.1  riastrad 	 u32 __ret;						\
    370  1.1  riastrad 	 __ret = (scic & OPREGION_SCIC_SUBFUNC_MASK) >>		\
    371  1.1  riastrad 	 OPREGION_SCIC_SUBFUNC_SHIFT;				\
    372  1.1  riastrad 	 __ret;							\
    373  1.1  riastrad 	 })
    374  1.1  riastrad 
    375  1.1  riastrad static const char *opregion_func_name(u32 func)
    376  1.1  riastrad {
    377  1.1  riastrad 	const char *name = NULL;
    378  1.1  riastrad 
    379  1.1  riastrad 	switch (func) {
    380  1.1  riastrad 	case 0 ... 3:
    381  1.1  riastrad 	case 5:
    382  1.1  riastrad 	case 7 ... 15:
    383  1.1  riastrad 		name = "Reserved";
    384  1.1  riastrad 		break;
    385  1.1  riastrad 
    386  1.1  riastrad 	case 4:
    387  1.1  riastrad 		name = "Get BIOS Data";
    388  1.1  riastrad 		break;
    389  1.1  riastrad 
    390  1.1  riastrad 	case 6:
    391  1.1  riastrad 		name = "System BIOS Callbacks";
    392  1.1  riastrad 		break;
    393  1.1  riastrad 
    394  1.1  riastrad 	default:
    395  1.1  riastrad 		name = "Unknown";
    396  1.1  riastrad 		break;
    397  1.1  riastrad 	}
    398  1.1  riastrad 	return name;
    399  1.1  riastrad }
    400  1.1  riastrad 
    401  1.1  riastrad static const char *opregion_subfunc_name(u32 subfunc)
    402  1.1  riastrad {
    403  1.1  riastrad 	const char *name = NULL;
    404  1.1  riastrad 
    405  1.1  riastrad 	switch (subfunc) {
    406  1.1  riastrad 	case 0:
    407  1.1  riastrad 		name = "Supported Calls";
    408  1.1  riastrad 		break;
    409  1.1  riastrad 
    410  1.1  riastrad 	case 1:
    411  1.1  riastrad 		name = "Requested Callbacks";
    412  1.1  riastrad 		break;
    413  1.1  riastrad 
    414  1.1  riastrad 	case 2 ... 3:
    415  1.1  riastrad 	case 8 ... 9:
    416  1.1  riastrad 		name = "Reserved";
    417  1.1  riastrad 		break;
    418  1.1  riastrad 
    419  1.1  riastrad 	case 5:
    420  1.1  riastrad 		name = "Boot Display";
    421  1.1  riastrad 		break;
    422  1.1  riastrad 
    423  1.1  riastrad 	case 6:
    424  1.1  riastrad 		name = "TV-Standard/Video-Connector";
    425  1.1  riastrad 		break;
    426  1.1  riastrad 
    427  1.1  riastrad 	case 7:
    428  1.1  riastrad 		name = "Internal Graphics";
    429  1.1  riastrad 		break;
    430  1.1  riastrad 
    431  1.1  riastrad 	case 10:
    432  1.1  riastrad 		name = "Spread Spectrum Clocks";
    433  1.1  riastrad 		break;
    434  1.1  riastrad 
    435  1.1  riastrad 	case 11:
    436  1.1  riastrad 		name = "Get AKSV";
    437  1.1  riastrad 		break;
    438  1.1  riastrad 
    439  1.1  riastrad 	default:
    440  1.1  riastrad 		name = "Unknown";
    441  1.1  riastrad 		break;
    442  1.1  riastrad 	}
    443  1.1  riastrad 	return name;
    444  1.1  riastrad };
    445  1.1  riastrad 
    446  1.1  riastrad static bool querying_capabilities(u32 scic)
    447  1.1  riastrad {
    448  1.1  riastrad 	u32 func, subfunc;
    449  1.1  riastrad 
    450  1.1  riastrad 	func = GVT_OPREGION_FUNC(scic);
    451  1.1  riastrad 	subfunc = GVT_OPREGION_SUBFUNC(scic);
    452  1.1  riastrad 
    453  1.1  riastrad 	if ((func == INTEL_GVT_OPREGION_SCIC_F_GETBIOSDATA &&
    454  1.1  riastrad 		subfunc == INTEL_GVT_OPREGION_SCIC_SF_SUPPRTEDCALLS)
    455  1.1  riastrad 		|| (func == INTEL_GVT_OPREGION_SCIC_F_GETBIOSDATA &&
    456  1.1  riastrad 		 subfunc == INTEL_GVT_OPREGION_SCIC_SF_REQEUSTEDCALLBACKS)
    457  1.1  riastrad 		|| (func == INTEL_GVT_OPREGION_SCIC_F_GETBIOSCALLBACKS &&
    458  1.1  riastrad 		 subfunc == INTEL_GVT_OPREGION_SCIC_SF_SUPPRTEDCALLS)) {
    459  1.1  riastrad 		return true;
    460  1.1  riastrad 	}
    461  1.1  riastrad 	return false;
    462  1.1  riastrad }
    463  1.1  riastrad 
    464  1.1  riastrad /**
    465  1.1  riastrad  * intel_vgpu_emulate_opregion_request - emulating OpRegion request
    466  1.1  riastrad  * @vgpu: a vGPU
    467  1.1  riastrad  * @swsci: SWSCI request
    468  1.1  riastrad  *
    469  1.1  riastrad  * Returns:
    470  1.1  riastrad  * Zero on success, negative error code if failed
    471  1.1  riastrad  */
    472  1.1  riastrad int intel_vgpu_emulate_opregion_request(struct intel_vgpu *vgpu, u32 swsci)
    473  1.1  riastrad {
    474  1.1  riastrad 	u32 scic, parm;
    475  1.1  riastrad 	u32 func, subfunc;
    476  1.1  riastrad 	u64 scic_pa = 0, parm_pa = 0;
    477  1.1  riastrad 	int ret;
    478  1.1  riastrad 
    479  1.1  riastrad 	switch (intel_gvt_host.hypervisor_type) {
    480  1.1  riastrad 	case INTEL_GVT_HYPERVISOR_XEN:
    481  1.1  riastrad 		scic = *((u32 *)vgpu_opregion(vgpu)->va +
    482  1.1  riastrad 					INTEL_GVT_OPREGION_SCIC);
    483  1.1  riastrad 		parm = *((u32 *)vgpu_opregion(vgpu)->va +
    484  1.1  riastrad 					INTEL_GVT_OPREGION_PARM);
    485  1.1  riastrad 		break;
    486  1.1  riastrad 	case INTEL_GVT_HYPERVISOR_KVM:
    487  1.1  riastrad 		scic_pa = (vgpu_opregion(vgpu)->gfn[0] << PAGE_SHIFT) +
    488  1.1  riastrad 					INTEL_GVT_OPREGION_SCIC;
    489  1.1  riastrad 		parm_pa = (vgpu_opregion(vgpu)->gfn[0] << PAGE_SHIFT) +
    490  1.1  riastrad 					INTEL_GVT_OPREGION_PARM;
    491  1.1  riastrad 
    492  1.1  riastrad 		ret = intel_gvt_hypervisor_read_gpa(vgpu, scic_pa,
    493  1.1  riastrad 						    &scic, sizeof(scic));
    494  1.1  riastrad 		if (ret) {
    495  1.1  riastrad 			gvt_vgpu_err("guest opregion read error %d, gpa 0x%llx, len %lu\n",
    496  1.1  riastrad 				ret, scic_pa, sizeof(scic));
    497  1.1  riastrad 			return ret;
    498  1.1  riastrad 		}
    499  1.1  riastrad 
    500  1.1  riastrad 		ret = intel_gvt_hypervisor_read_gpa(vgpu, parm_pa,
    501  1.1  riastrad 						    &parm, sizeof(parm));
    502  1.1  riastrad 		if (ret) {
    503  1.1  riastrad 			gvt_vgpu_err("guest opregion read error %d, gpa 0x%llx, len %lu\n",
    504  1.1  riastrad 				ret, scic_pa, sizeof(scic));
    505  1.1  riastrad 			return ret;
    506  1.1  riastrad 		}
    507  1.1  riastrad 
    508  1.1  riastrad 		break;
    509  1.1  riastrad 	default:
    510  1.1  riastrad 		gvt_vgpu_err("not supported hypervisor\n");
    511  1.1  riastrad 		return -EINVAL;
    512  1.1  riastrad 	}
    513  1.1  riastrad 
    514  1.1  riastrad 	if (!(swsci & SWSCI_SCI_SELECT)) {
    515  1.1  riastrad 		gvt_vgpu_err("requesting SMI service\n");
    516  1.1  riastrad 		return 0;
    517  1.1  riastrad 	}
    518  1.1  riastrad 	/* ignore non 0->1 trasitions */
    519  1.1  riastrad 	if ((vgpu_cfg_space(vgpu)[INTEL_GVT_PCI_SWSCI]
    520  1.1  riastrad 				& SWSCI_SCI_TRIGGER) ||
    521  1.1  riastrad 			!(swsci & SWSCI_SCI_TRIGGER)) {
    522  1.1  riastrad 		return 0;
    523  1.1  riastrad 	}
    524  1.1  riastrad 
    525  1.1  riastrad 	func = GVT_OPREGION_FUNC(scic);
    526  1.1  riastrad 	subfunc = GVT_OPREGION_SUBFUNC(scic);
    527  1.1  riastrad 	if (!querying_capabilities(scic)) {
    528  1.1  riastrad 		gvt_vgpu_err("requesting runtime service: func \"%s\","
    529  1.1  riastrad 				" subfunc \"%s\"\n",
    530  1.1  riastrad 				opregion_func_name(func),
    531  1.1  riastrad 				opregion_subfunc_name(subfunc));
    532  1.1  riastrad 		/*
    533  1.1  riastrad 		 * emulate exit status of function call, '0' means
    534  1.1  riastrad 		 * "failure, generic, unsupported or unknown cause"
    535  1.1  riastrad 		 */
    536  1.1  riastrad 		scic &= ~OPREGION_SCIC_EXIT_MASK;
    537  1.1  riastrad 		goto out;
    538  1.1  riastrad 	}
    539  1.1  riastrad 
    540  1.1  riastrad 	scic = 0;
    541  1.1  riastrad 	parm = 0;
    542  1.1  riastrad 
    543  1.1  riastrad out:
    544  1.1  riastrad 	switch (intel_gvt_host.hypervisor_type) {
    545  1.1  riastrad 	case INTEL_GVT_HYPERVISOR_XEN:
    546  1.1  riastrad 		*((u32 *)vgpu_opregion(vgpu)->va +
    547  1.1  riastrad 					INTEL_GVT_OPREGION_SCIC) = scic;
    548  1.1  riastrad 		*((u32 *)vgpu_opregion(vgpu)->va +
    549  1.1  riastrad 					INTEL_GVT_OPREGION_PARM) = parm;
    550  1.1  riastrad 		break;
    551  1.1  riastrad 	case INTEL_GVT_HYPERVISOR_KVM:
    552  1.1  riastrad 		ret = intel_gvt_hypervisor_write_gpa(vgpu, scic_pa,
    553  1.1  riastrad 						    &scic, sizeof(scic));
    554  1.1  riastrad 		if (ret) {
    555  1.1  riastrad 			gvt_vgpu_err("guest opregion write error %d, gpa 0x%llx, len %lu\n",
    556  1.1  riastrad 				ret, scic_pa, sizeof(scic));
    557  1.1  riastrad 			return ret;
    558  1.1  riastrad 		}
    559  1.1  riastrad 
    560  1.1  riastrad 		ret = intel_gvt_hypervisor_write_gpa(vgpu, parm_pa,
    561  1.1  riastrad 						    &parm, sizeof(parm));
    562  1.1  riastrad 		if (ret) {
    563  1.1  riastrad 			gvt_vgpu_err("guest opregion write error %d, gpa 0x%llx, len %lu\n",
    564  1.1  riastrad 				ret, scic_pa, sizeof(scic));
    565  1.1  riastrad 			return ret;
    566  1.1  riastrad 		}
    567  1.1  riastrad 
    568  1.1  riastrad 		break;
    569  1.1  riastrad 	default:
    570  1.1  riastrad 		gvt_vgpu_err("not supported hypervisor\n");
    571  1.1  riastrad 		return -EINVAL;
    572  1.1  riastrad 	}
    573  1.1  riastrad 
    574  1.1  riastrad 	return 0;
    575  1.1  riastrad }
    576