18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright(c) 2011-2016 Intel Corporation. All rights reserved. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next 128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 138c2ecf20Sopenharmony_ci * Software. 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 208c2ecf20Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 218c2ecf20Sopenharmony_ci * SOFTWARE. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * Authors: 248c2ecf20Sopenharmony_ci * Eddie Dong <eddie.dong@intel.com> 258c2ecf20Sopenharmony_ci * Jike Song <jike.song@intel.com> 268c2ecf20Sopenharmony_ci * 278c2ecf20Sopenharmony_ci * Contributors: 288c2ecf20Sopenharmony_ci * Zhi Wang <zhi.a.wang@intel.com> 298c2ecf20Sopenharmony_ci * Min He <min.he@intel.com> 308c2ecf20Sopenharmony_ci * Bing Niu <bing.niu@intel.com> 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#include "i915_drv.h" 358c2ecf20Sopenharmony_ci#include "gvt.h" 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cienum { 388c2ecf20Sopenharmony_ci INTEL_GVT_PCI_BAR_GTTMMIO = 0, 398c2ecf20Sopenharmony_ci INTEL_GVT_PCI_BAR_APERTURE, 408c2ecf20Sopenharmony_ci INTEL_GVT_PCI_BAR_PIO, 418c2ecf20Sopenharmony_ci INTEL_GVT_PCI_BAR_MAX, 428c2ecf20Sopenharmony_ci}; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* bitmap for writable bits (RW or RW1C bits, but cannot co-exist in one 458c2ecf20Sopenharmony_ci * byte) byte by byte in standard pci configuration space. (not the full 468c2ecf20Sopenharmony_ci * 256 bytes.) 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_cistatic const u8 pci_cfg_space_rw_bmp[PCI_INTERRUPT_LINE + 4] = { 498c2ecf20Sopenharmony_ci [PCI_COMMAND] = 0xff, 0x07, 508c2ecf20Sopenharmony_ci [PCI_STATUS] = 0x00, 0xf9, /* the only one RW1C byte */ 518c2ecf20Sopenharmony_ci [PCI_CACHE_LINE_SIZE] = 0xff, 528c2ecf20Sopenharmony_ci [PCI_BASE_ADDRESS_0 ... PCI_CARDBUS_CIS - 1] = 0xff, 538c2ecf20Sopenharmony_ci [PCI_ROM_ADDRESS] = 0x01, 0xf8, 0xff, 0xff, 548c2ecf20Sopenharmony_ci [PCI_INTERRUPT_LINE] = 0xff, 558c2ecf20Sopenharmony_ci}; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/** 588c2ecf20Sopenharmony_ci * vgpu_pci_cfg_mem_write - write virtual cfg space memory 598c2ecf20Sopenharmony_ci * @vgpu: target vgpu 608c2ecf20Sopenharmony_ci * @off: offset 618c2ecf20Sopenharmony_ci * @src: src ptr to write 628c2ecf20Sopenharmony_ci * @bytes: number of bytes 638c2ecf20Sopenharmony_ci * 648c2ecf20Sopenharmony_ci * Use this function to write virtual cfg space memory. 658c2ecf20Sopenharmony_ci * For standard cfg space, only RW bits can be changed, 668c2ecf20Sopenharmony_ci * and we emulates the RW1C behavior of PCI_STATUS register. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_cistatic void vgpu_pci_cfg_mem_write(struct intel_vgpu *vgpu, unsigned int off, 698c2ecf20Sopenharmony_ci u8 *src, unsigned int bytes) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci u8 *cfg_base = vgpu_cfg_space(vgpu); 728c2ecf20Sopenharmony_ci u8 mask, new, old; 738c2ecf20Sopenharmony_ci pci_power_t pwr; 748c2ecf20Sopenharmony_ci int i = 0; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci for (; i < bytes && (off + i < sizeof(pci_cfg_space_rw_bmp)); i++) { 778c2ecf20Sopenharmony_ci mask = pci_cfg_space_rw_bmp[off + i]; 788c2ecf20Sopenharmony_ci old = cfg_base[off + i]; 798c2ecf20Sopenharmony_ci new = src[i] & mask; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /** 828c2ecf20Sopenharmony_ci * The PCI_STATUS high byte has RW1C bits, here 838c2ecf20Sopenharmony_ci * emulates clear by writing 1 for these bits. 848c2ecf20Sopenharmony_ci * Writing a 0b to RW1C bits has no effect. 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_ci if (off + i == PCI_STATUS + 1) 878c2ecf20Sopenharmony_ci new = (~new & old) & mask; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci cfg_base[off + i] = (old & ~mask) | new; 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* For other configuration space directly copy as it is. */ 938c2ecf20Sopenharmony_ci if (i < bytes) 948c2ecf20Sopenharmony_ci memcpy(cfg_base + off + i, src + i, bytes - i); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci if (off == vgpu->cfg_space.pmcsr_off && vgpu->cfg_space.pmcsr_off) { 978c2ecf20Sopenharmony_ci pwr = (pci_power_t __force)(*(u16*)(&vgpu_cfg_space(vgpu)[off]) 988c2ecf20Sopenharmony_ci & PCI_PM_CTRL_STATE_MASK); 998c2ecf20Sopenharmony_ci if (pwr == PCI_D3hot) 1008c2ecf20Sopenharmony_ci vgpu->d3_entered = true; 1018c2ecf20Sopenharmony_ci gvt_dbg_core("vgpu-%d power status changed to %d\n", 1028c2ecf20Sopenharmony_ci vgpu->id, pwr); 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/** 1078c2ecf20Sopenharmony_ci * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space read 1088c2ecf20Sopenharmony_ci * @vgpu: target vgpu 1098c2ecf20Sopenharmony_ci * @offset: offset 1108c2ecf20Sopenharmony_ci * @p_data: return data ptr 1118c2ecf20Sopenharmony_ci * @bytes: number of bytes to read 1128c2ecf20Sopenharmony_ci * 1138c2ecf20Sopenharmony_ci * Returns: 1148c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed. 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_ciint intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset, 1178c2ecf20Sopenharmony_ci void *p_data, unsigned int bytes) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct drm_i915_private *i915 = vgpu->gvt->gt->i915; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, bytes > 4)) 1228c2ecf20Sopenharmony_ci return -EINVAL; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, 1258c2ecf20Sopenharmony_ci offset + bytes > vgpu->gvt->device_info.cfg_space_size)) 1268c2ecf20Sopenharmony_ci return -EINVAL; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci memcpy(p_data, vgpu_cfg_space(vgpu) + offset, bytes); 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic int map_aperture(struct intel_vgpu *vgpu, bool map) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci phys_addr_t aperture_pa = vgpu_aperture_pa_base(vgpu); 1358c2ecf20Sopenharmony_ci unsigned long aperture_sz = vgpu_aperture_sz(vgpu); 1368c2ecf20Sopenharmony_ci u64 first_gfn; 1378c2ecf20Sopenharmony_ci u64 val; 1388c2ecf20Sopenharmony_ci int ret; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if (map == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked) 1418c2ecf20Sopenharmony_ci return 0; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_2]; 1448c2ecf20Sopenharmony_ci if (val & PCI_BASE_ADDRESS_MEM_TYPE_64) 1458c2ecf20Sopenharmony_ci val = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2); 1468c2ecf20Sopenharmony_ci else 1478c2ecf20Sopenharmony_ci val = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci first_gfn = (val + vgpu_aperture_offset(vgpu)) >> PAGE_SHIFT; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu, first_gfn, 1528c2ecf20Sopenharmony_ci aperture_pa >> PAGE_SHIFT, 1538c2ecf20Sopenharmony_ci aperture_sz >> PAGE_SHIFT, 1548c2ecf20Sopenharmony_ci map); 1558c2ecf20Sopenharmony_ci if (ret) 1568c2ecf20Sopenharmony_ci return ret; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked = map; 1598c2ecf20Sopenharmony_ci return 0; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic int trap_gttmmio(struct intel_vgpu *vgpu, bool trap) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci u64 start, end; 1658c2ecf20Sopenharmony_ci u64 val; 1668c2ecf20Sopenharmony_ci int ret; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (trap == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked) 1698c2ecf20Sopenharmony_ci return 0; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_0]; 1728c2ecf20Sopenharmony_ci if (val & PCI_BASE_ADDRESS_MEM_TYPE_64) 1738c2ecf20Sopenharmony_ci start = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0); 1748c2ecf20Sopenharmony_ci else 1758c2ecf20Sopenharmony_ci start = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci start &= ~GENMASK(3, 0); 1788c2ecf20Sopenharmony_ci end = start + vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size - 1; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci ret = intel_gvt_hypervisor_set_trap_area(vgpu, start, end, trap); 1818c2ecf20Sopenharmony_ci if (ret) 1828c2ecf20Sopenharmony_ci return ret; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked = trap; 1858c2ecf20Sopenharmony_ci return 0; 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cistatic int emulate_pci_command_write(struct intel_vgpu *vgpu, 1898c2ecf20Sopenharmony_ci unsigned int offset, void *p_data, unsigned int bytes) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci u8 old = vgpu_cfg_space(vgpu)[offset]; 1928c2ecf20Sopenharmony_ci u8 new = *(u8 *)p_data; 1938c2ecf20Sopenharmony_ci u8 changed = old ^ new; 1948c2ecf20Sopenharmony_ci int ret; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes); 1978c2ecf20Sopenharmony_ci if (!(changed & PCI_COMMAND_MEMORY)) 1988c2ecf20Sopenharmony_ci return 0; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci if (old & PCI_COMMAND_MEMORY) { 2018c2ecf20Sopenharmony_ci ret = trap_gttmmio(vgpu, false); 2028c2ecf20Sopenharmony_ci if (ret) 2038c2ecf20Sopenharmony_ci return ret; 2048c2ecf20Sopenharmony_ci ret = map_aperture(vgpu, false); 2058c2ecf20Sopenharmony_ci if (ret) 2068c2ecf20Sopenharmony_ci return ret; 2078c2ecf20Sopenharmony_ci } else { 2088c2ecf20Sopenharmony_ci ret = trap_gttmmio(vgpu, true); 2098c2ecf20Sopenharmony_ci if (ret) 2108c2ecf20Sopenharmony_ci return ret; 2118c2ecf20Sopenharmony_ci ret = map_aperture(vgpu, true); 2128c2ecf20Sopenharmony_ci if (ret) 2138c2ecf20Sopenharmony_ci return ret; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci return 0; 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_cistatic int emulate_pci_rom_bar_write(struct intel_vgpu *vgpu, 2208c2ecf20Sopenharmony_ci unsigned int offset, void *p_data, unsigned int bytes) 2218c2ecf20Sopenharmony_ci{ 2228c2ecf20Sopenharmony_ci u32 *pval = (u32 *)(vgpu_cfg_space(vgpu) + offset); 2238c2ecf20Sopenharmony_ci u32 new = *(u32 *)(p_data); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if ((new & PCI_ROM_ADDRESS_MASK) == PCI_ROM_ADDRESS_MASK) 2268c2ecf20Sopenharmony_ci /* We don't have rom, return size of 0. */ 2278c2ecf20Sopenharmony_ci *pval = 0; 2288c2ecf20Sopenharmony_ci else 2298c2ecf20Sopenharmony_ci vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes); 2308c2ecf20Sopenharmony_ci return 0; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistatic int emulate_pci_bar_write(struct intel_vgpu *vgpu, unsigned int offset, 2348c2ecf20Sopenharmony_ci void *p_data, unsigned int bytes) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci u32 new = *(u32 *)(p_data); 2378c2ecf20Sopenharmony_ci bool lo = IS_ALIGNED(offset, 8); 2388c2ecf20Sopenharmony_ci u64 size; 2398c2ecf20Sopenharmony_ci int ret = 0; 2408c2ecf20Sopenharmony_ci bool mmio_enabled = 2418c2ecf20Sopenharmony_ci vgpu_cfg_space(vgpu)[PCI_COMMAND] & PCI_COMMAND_MEMORY; 2428c2ecf20Sopenharmony_ci struct intel_vgpu_pci_bar *bars = vgpu->cfg_space.bar; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* 2458c2ecf20Sopenharmony_ci * Power-up software can determine how much address 2468c2ecf20Sopenharmony_ci * space the device requires by writing a value of 2478c2ecf20Sopenharmony_ci * all 1's to the register and then reading the value 2488c2ecf20Sopenharmony_ci * back. The device will return 0's in all don't-care 2498c2ecf20Sopenharmony_ci * address bits. 2508c2ecf20Sopenharmony_ci */ 2518c2ecf20Sopenharmony_ci if (new == 0xffffffff) { 2528c2ecf20Sopenharmony_ci switch (offset) { 2538c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_0: 2548c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_1: 2558c2ecf20Sopenharmony_ci size = ~(bars[INTEL_GVT_PCI_BAR_GTTMMIO].size -1); 2568c2ecf20Sopenharmony_ci intel_vgpu_write_pci_bar(vgpu, offset, 2578c2ecf20Sopenharmony_ci size >> (lo ? 0 : 32), lo); 2588c2ecf20Sopenharmony_ci /* 2598c2ecf20Sopenharmony_ci * Untrap the BAR, since guest hasn't configured a 2608c2ecf20Sopenharmony_ci * valid GPA 2618c2ecf20Sopenharmony_ci */ 2628c2ecf20Sopenharmony_ci ret = trap_gttmmio(vgpu, false); 2638c2ecf20Sopenharmony_ci break; 2648c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_2: 2658c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_3: 2668c2ecf20Sopenharmony_ci size = ~(bars[INTEL_GVT_PCI_BAR_APERTURE].size -1); 2678c2ecf20Sopenharmony_ci intel_vgpu_write_pci_bar(vgpu, offset, 2688c2ecf20Sopenharmony_ci size >> (lo ? 0 : 32), lo); 2698c2ecf20Sopenharmony_ci ret = map_aperture(vgpu, false); 2708c2ecf20Sopenharmony_ci break; 2718c2ecf20Sopenharmony_ci default: 2728c2ecf20Sopenharmony_ci /* Unimplemented BARs */ 2738c2ecf20Sopenharmony_ci intel_vgpu_write_pci_bar(vgpu, offset, 0x0, false); 2748c2ecf20Sopenharmony_ci } 2758c2ecf20Sopenharmony_ci } else { 2768c2ecf20Sopenharmony_ci switch (offset) { 2778c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_0: 2788c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_1: 2798c2ecf20Sopenharmony_ci /* 2808c2ecf20Sopenharmony_ci * Untrap the old BAR first, since guest has 2818c2ecf20Sopenharmony_ci * re-configured the BAR 2828c2ecf20Sopenharmony_ci */ 2838c2ecf20Sopenharmony_ci trap_gttmmio(vgpu, false); 2848c2ecf20Sopenharmony_ci intel_vgpu_write_pci_bar(vgpu, offset, new, lo); 2858c2ecf20Sopenharmony_ci ret = trap_gttmmio(vgpu, mmio_enabled); 2868c2ecf20Sopenharmony_ci break; 2878c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_2: 2888c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_3: 2898c2ecf20Sopenharmony_ci map_aperture(vgpu, false); 2908c2ecf20Sopenharmony_ci intel_vgpu_write_pci_bar(vgpu, offset, new, lo); 2918c2ecf20Sopenharmony_ci ret = map_aperture(vgpu, mmio_enabled); 2928c2ecf20Sopenharmony_ci break; 2938c2ecf20Sopenharmony_ci default: 2948c2ecf20Sopenharmony_ci intel_vgpu_write_pci_bar(vgpu, offset, new, lo); 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci } 2978c2ecf20Sopenharmony_ci return ret; 2988c2ecf20Sopenharmony_ci} 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci/** 3018c2ecf20Sopenharmony_ci * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space write 3028c2ecf20Sopenharmony_ci * @vgpu: target vgpu 3038c2ecf20Sopenharmony_ci * @offset: offset 3048c2ecf20Sopenharmony_ci * @p_data: write data ptr 3058c2ecf20Sopenharmony_ci * @bytes: number of bytes to write 3068c2ecf20Sopenharmony_ci * 3078c2ecf20Sopenharmony_ci * Returns: 3088c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed. 3098c2ecf20Sopenharmony_ci */ 3108c2ecf20Sopenharmony_ciint intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset, 3118c2ecf20Sopenharmony_ci void *p_data, unsigned int bytes) 3128c2ecf20Sopenharmony_ci{ 3138c2ecf20Sopenharmony_ci struct drm_i915_private *i915 = vgpu->gvt->gt->i915; 3148c2ecf20Sopenharmony_ci int ret; 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, bytes > 4)) 3178c2ecf20Sopenharmony_ci return -EINVAL; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, 3208c2ecf20Sopenharmony_ci offset + bytes > vgpu->gvt->device_info.cfg_space_size)) 3218c2ecf20Sopenharmony_ci return -EINVAL; 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci /* First check if it's PCI_COMMAND */ 3248c2ecf20Sopenharmony_ci if (IS_ALIGNED(offset, 2) && offset == PCI_COMMAND) { 3258c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, bytes > 2)) 3268c2ecf20Sopenharmony_ci return -EINVAL; 3278c2ecf20Sopenharmony_ci return emulate_pci_command_write(vgpu, offset, p_data, bytes); 3288c2ecf20Sopenharmony_ci } 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci switch (rounddown(offset, 4)) { 3318c2ecf20Sopenharmony_ci case PCI_ROM_ADDRESS: 3328c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, !IS_ALIGNED(offset, 4))) 3338c2ecf20Sopenharmony_ci return -EINVAL; 3348c2ecf20Sopenharmony_ci return emulate_pci_rom_bar_write(vgpu, offset, p_data, bytes); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_5: 3378c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, !IS_ALIGNED(offset, 4))) 3388c2ecf20Sopenharmony_ci return -EINVAL; 3398c2ecf20Sopenharmony_ci return emulate_pci_bar_write(vgpu, offset, p_data, bytes); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci case INTEL_GVT_PCI_SWSCI: 3428c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, !IS_ALIGNED(offset, 4))) 3438c2ecf20Sopenharmony_ci return -EINVAL; 3448c2ecf20Sopenharmony_ci ret = intel_vgpu_emulate_opregion_request(vgpu, *(u32 *)p_data); 3458c2ecf20Sopenharmony_ci if (ret) 3468c2ecf20Sopenharmony_ci return ret; 3478c2ecf20Sopenharmony_ci break; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci case INTEL_GVT_PCI_OPREGION: 3508c2ecf20Sopenharmony_ci if (drm_WARN_ON(&i915->drm, !IS_ALIGNED(offset, 4))) 3518c2ecf20Sopenharmony_ci return -EINVAL; 3528c2ecf20Sopenharmony_ci ret = intel_vgpu_opregion_base_write_handler(vgpu, 3538c2ecf20Sopenharmony_ci *(u32 *)p_data); 3548c2ecf20Sopenharmony_ci if (ret) 3558c2ecf20Sopenharmony_ci return ret; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes); 3588c2ecf20Sopenharmony_ci break; 3598c2ecf20Sopenharmony_ci default: 3608c2ecf20Sopenharmony_ci vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes); 3618c2ecf20Sopenharmony_ci break; 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci return 0; 3648c2ecf20Sopenharmony_ci} 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci/** 3678c2ecf20Sopenharmony_ci * intel_vgpu_init_cfg_space - init vGPU configuration space when create vGPU 3688c2ecf20Sopenharmony_ci * 3698c2ecf20Sopenharmony_ci * @vgpu: a vGPU 3708c2ecf20Sopenharmony_ci * @primary: is the vGPU presented as primary 3718c2ecf20Sopenharmony_ci * 3728c2ecf20Sopenharmony_ci */ 3738c2ecf20Sopenharmony_civoid intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu, 3748c2ecf20Sopenharmony_ci bool primary) 3758c2ecf20Sopenharmony_ci{ 3768c2ecf20Sopenharmony_ci struct intel_gvt *gvt = vgpu->gvt; 3778c2ecf20Sopenharmony_ci const struct intel_gvt_device_info *info = &gvt->device_info; 3788c2ecf20Sopenharmony_ci u16 *gmch_ctl; 3798c2ecf20Sopenharmony_ci u8 next; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci memcpy(vgpu_cfg_space(vgpu), gvt->firmware.cfg_space, 3828c2ecf20Sopenharmony_ci info->cfg_space_size); 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci if (!primary) { 3858c2ecf20Sopenharmony_ci vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] = 3868c2ecf20Sopenharmony_ci INTEL_GVT_PCI_CLASS_VGA_OTHER; 3878c2ecf20Sopenharmony_ci vgpu_cfg_space(vgpu)[PCI_CLASS_PROG] = 3888c2ecf20Sopenharmony_ci INTEL_GVT_PCI_CLASS_VGA_OTHER; 3898c2ecf20Sopenharmony_ci } 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci /* Show guest that there isn't any stolen memory.*/ 3928c2ecf20Sopenharmony_ci gmch_ctl = (u16 *)(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_GMCH_CONTROL); 3938c2ecf20Sopenharmony_ci *gmch_ctl &= ~(BDW_GMCH_GMS_MASK << BDW_GMCH_GMS_SHIFT); 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci intel_vgpu_write_pci_bar(vgpu, PCI_BASE_ADDRESS_2, 3968c2ecf20Sopenharmony_ci gvt_aperture_pa_base(gvt), true); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci vgpu_cfg_space(vgpu)[PCI_COMMAND] &= ~(PCI_COMMAND_IO 3998c2ecf20Sopenharmony_ci | PCI_COMMAND_MEMORY 4008c2ecf20Sopenharmony_ci | PCI_COMMAND_MASTER); 4018c2ecf20Sopenharmony_ci /* 4028c2ecf20Sopenharmony_ci * Clear the bar upper 32bit and let guest to assign the new value 4038c2ecf20Sopenharmony_ci */ 4048c2ecf20Sopenharmony_ci memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_1, 0, 4); 4058c2ecf20Sopenharmony_ci memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_3, 0, 4); 4068c2ecf20Sopenharmony_ci memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_4, 0, 8); 4078c2ecf20Sopenharmony_ci memset(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_OPREGION, 0, 4); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size = 4108c2ecf20Sopenharmony_ci pci_resource_len(gvt->gt->i915->drm.pdev, 0); 4118c2ecf20Sopenharmony_ci vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].size = 4128c2ecf20Sopenharmony_ci pci_resource_len(gvt->gt->i915->drm.pdev, 2); 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci memset(vgpu_cfg_space(vgpu) + PCI_ROM_ADDRESS, 0, 4); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci /* PM Support */ 4178c2ecf20Sopenharmony_ci vgpu->cfg_space.pmcsr_off = 0; 4188c2ecf20Sopenharmony_ci if (vgpu_cfg_space(vgpu)[PCI_STATUS] & PCI_STATUS_CAP_LIST) { 4198c2ecf20Sopenharmony_ci next = vgpu_cfg_space(vgpu)[PCI_CAPABILITY_LIST]; 4208c2ecf20Sopenharmony_ci do { 4218c2ecf20Sopenharmony_ci if (vgpu_cfg_space(vgpu)[next + PCI_CAP_LIST_ID] == PCI_CAP_ID_PM) { 4228c2ecf20Sopenharmony_ci vgpu->cfg_space.pmcsr_off = next + PCI_PM_CTRL; 4238c2ecf20Sopenharmony_ci break; 4248c2ecf20Sopenharmony_ci } 4258c2ecf20Sopenharmony_ci next = vgpu_cfg_space(vgpu)[next + PCI_CAP_LIST_NEXT]; 4268c2ecf20Sopenharmony_ci } while (next); 4278c2ecf20Sopenharmony_ci } 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci/** 4318c2ecf20Sopenharmony_ci * intel_vgpu_reset_cfg_space - reset vGPU configuration space 4328c2ecf20Sopenharmony_ci * 4338c2ecf20Sopenharmony_ci * @vgpu: a vGPU 4348c2ecf20Sopenharmony_ci * 4358c2ecf20Sopenharmony_ci */ 4368c2ecf20Sopenharmony_civoid intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu) 4378c2ecf20Sopenharmony_ci{ 4388c2ecf20Sopenharmony_ci u8 cmd = vgpu_cfg_space(vgpu)[PCI_COMMAND]; 4398c2ecf20Sopenharmony_ci bool primary = vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] != 4408c2ecf20Sopenharmony_ci INTEL_GVT_PCI_CLASS_VGA_OTHER; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci if (cmd & PCI_COMMAND_MEMORY) { 4438c2ecf20Sopenharmony_ci trap_gttmmio(vgpu, false); 4448c2ecf20Sopenharmony_ci map_aperture(vgpu, false); 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci /** 4488c2ecf20Sopenharmony_ci * Currently we only do such reset when vGPU is not 4498c2ecf20Sopenharmony_ci * owned by any VM, so we simply restore entire cfg 4508c2ecf20Sopenharmony_ci * space to default value. 4518c2ecf20Sopenharmony_ci */ 4528c2ecf20Sopenharmony_ci intel_vgpu_init_cfg_space(vgpu, primary); 4538c2ecf20Sopenharmony_ci} 454