162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * VFIO PCI Intel Graphics support 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2016 Red Hat, Inc. All rights reserved. 662306a36Sopenharmony_ci * Author: Alex Williamson <alex.williamson@redhat.com> 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Register a device specific region through which to provide read-only 962306a36Sopenharmony_ci * access to the Intel IGD opregion. The register defining the opregion 1062306a36Sopenharmony_ci * address is also virtualized to prevent user modification. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#include <linux/io.h> 1462306a36Sopenharmony_ci#include <linux/pci.h> 1562306a36Sopenharmony_ci#include <linux/uaccess.h> 1662306a36Sopenharmony_ci#include <linux/vfio.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#include "vfio_pci_priv.h" 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#define OPREGION_SIGNATURE "IntelGraphicsMem" 2162306a36Sopenharmony_ci#define OPREGION_SIZE (8 * 1024) 2262306a36Sopenharmony_ci#define OPREGION_PCI_ADDR 0xfc 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#define OPREGION_RVDA 0x3ba 2562306a36Sopenharmony_ci#define OPREGION_RVDS 0x3c2 2662306a36Sopenharmony_ci#define OPREGION_VERSION 0x16 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_cistruct igd_opregion_vbt { 2962306a36Sopenharmony_ci void *opregion; 3062306a36Sopenharmony_ci void *vbt_ex; 3162306a36Sopenharmony_ci}; 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci/** 3462306a36Sopenharmony_ci * igd_opregion_shift_copy() - Copy OpRegion to user buffer and shift position. 3562306a36Sopenharmony_ci * @dst: User buffer ptr to copy to. 3662306a36Sopenharmony_ci * @off: Offset to user buffer ptr. Increased by bytes on return. 3762306a36Sopenharmony_ci * @src: Source buffer to copy from. 3862306a36Sopenharmony_ci * @pos: Increased by bytes on return. 3962306a36Sopenharmony_ci * @remaining: Decreased by bytes on return. 4062306a36Sopenharmony_ci * @bytes: Bytes to copy and adjust off, pos and remaining. 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * Copy OpRegion to offset from specific source ptr and shift the offset. 4362306a36Sopenharmony_ci * 4462306a36Sopenharmony_ci * Return: 0 on success, -EFAULT otherwise. 4562306a36Sopenharmony_ci * 4662306a36Sopenharmony_ci */ 4762306a36Sopenharmony_cistatic inline unsigned long igd_opregion_shift_copy(char __user *dst, 4862306a36Sopenharmony_ci loff_t *off, 4962306a36Sopenharmony_ci void *src, 5062306a36Sopenharmony_ci loff_t *pos, 5162306a36Sopenharmony_ci size_t *remaining, 5262306a36Sopenharmony_ci size_t bytes) 5362306a36Sopenharmony_ci{ 5462306a36Sopenharmony_ci if (copy_to_user(dst + (*off), src, bytes)) 5562306a36Sopenharmony_ci return -EFAULT; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci *off += bytes; 5862306a36Sopenharmony_ci *pos += bytes; 5962306a36Sopenharmony_ci *remaining -= bytes; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci return 0; 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_cistatic ssize_t vfio_pci_igd_rw(struct vfio_pci_core_device *vdev, 6562306a36Sopenharmony_ci char __user *buf, size_t count, loff_t *ppos, 6662306a36Sopenharmony_ci bool iswrite) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS; 6962306a36Sopenharmony_ci struct igd_opregion_vbt *opregionvbt = vdev->region[i].data; 7062306a36Sopenharmony_ci loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK, off = 0; 7162306a36Sopenharmony_ci size_t remaining; 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci if (pos >= vdev->region[i].size || iswrite) 7462306a36Sopenharmony_ci return -EINVAL; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci count = min_t(size_t, count, vdev->region[i].size - pos); 7762306a36Sopenharmony_ci remaining = count; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci /* Copy until OpRegion version */ 8062306a36Sopenharmony_ci if (remaining && pos < OPREGION_VERSION) { 8162306a36Sopenharmony_ci size_t bytes = min_t(size_t, remaining, OPREGION_VERSION - pos); 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci if (igd_opregion_shift_copy(buf, &off, 8462306a36Sopenharmony_ci opregionvbt->opregion + pos, &pos, 8562306a36Sopenharmony_ci &remaining, bytes)) 8662306a36Sopenharmony_ci return -EFAULT; 8762306a36Sopenharmony_ci } 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci /* Copy patched (if necessary) OpRegion version */ 9062306a36Sopenharmony_ci if (remaining && pos < OPREGION_VERSION + sizeof(__le16)) { 9162306a36Sopenharmony_ci size_t bytes = min_t(size_t, remaining, 9262306a36Sopenharmony_ci OPREGION_VERSION + sizeof(__le16) - pos); 9362306a36Sopenharmony_ci __le16 version = *(__le16 *)(opregionvbt->opregion + 9462306a36Sopenharmony_ci OPREGION_VERSION); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci /* Patch to 2.1 if OpRegion 2.0 has extended VBT */ 9762306a36Sopenharmony_ci if (le16_to_cpu(version) == 0x0200 && opregionvbt->vbt_ex) 9862306a36Sopenharmony_ci version = cpu_to_le16(0x0201); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci if (igd_opregion_shift_copy(buf, &off, 10162306a36Sopenharmony_ci (u8 *)&version + 10262306a36Sopenharmony_ci (pos - OPREGION_VERSION), 10362306a36Sopenharmony_ci &pos, &remaining, bytes)) 10462306a36Sopenharmony_ci return -EFAULT; 10562306a36Sopenharmony_ci } 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci /* Copy until RVDA */ 10862306a36Sopenharmony_ci if (remaining && pos < OPREGION_RVDA) { 10962306a36Sopenharmony_ci size_t bytes = min_t(size_t, remaining, OPREGION_RVDA - pos); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci if (igd_opregion_shift_copy(buf, &off, 11262306a36Sopenharmony_ci opregionvbt->opregion + pos, &pos, 11362306a36Sopenharmony_ci &remaining, bytes)) 11462306a36Sopenharmony_ci return -EFAULT; 11562306a36Sopenharmony_ci } 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci /* Copy modified (if necessary) RVDA */ 11862306a36Sopenharmony_ci if (remaining && pos < OPREGION_RVDA + sizeof(__le64)) { 11962306a36Sopenharmony_ci size_t bytes = min_t(size_t, remaining, 12062306a36Sopenharmony_ci OPREGION_RVDA + sizeof(__le64) - pos); 12162306a36Sopenharmony_ci __le64 rvda = cpu_to_le64(opregionvbt->vbt_ex ? 12262306a36Sopenharmony_ci OPREGION_SIZE : 0); 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci if (igd_opregion_shift_copy(buf, &off, 12562306a36Sopenharmony_ci (u8 *)&rvda + (pos - OPREGION_RVDA), 12662306a36Sopenharmony_ci &pos, &remaining, bytes)) 12762306a36Sopenharmony_ci return -EFAULT; 12862306a36Sopenharmony_ci } 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci /* Copy the rest of OpRegion */ 13162306a36Sopenharmony_ci if (remaining && pos < OPREGION_SIZE) { 13262306a36Sopenharmony_ci size_t bytes = min_t(size_t, remaining, OPREGION_SIZE - pos); 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci if (igd_opregion_shift_copy(buf, &off, 13562306a36Sopenharmony_ci opregionvbt->opregion + pos, &pos, 13662306a36Sopenharmony_ci &remaining, bytes)) 13762306a36Sopenharmony_ci return -EFAULT; 13862306a36Sopenharmony_ci } 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci /* Copy extended VBT if exists */ 14162306a36Sopenharmony_ci if (remaining && 14262306a36Sopenharmony_ci copy_to_user(buf + off, opregionvbt->vbt_ex + (pos - OPREGION_SIZE), 14362306a36Sopenharmony_ci remaining)) 14462306a36Sopenharmony_ci return -EFAULT; 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci *ppos += count; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci return count; 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_cistatic void vfio_pci_igd_release(struct vfio_pci_core_device *vdev, 15262306a36Sopenharmony_ci struct vfio_pci_region *region) 15362306a36Sopenharmony_ci{ 15462306a36Sopenharmony_ci struct igd_opregion_vbt *opregionvbt = region->data; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci if (opregionvbt->vbt_ex) 15762306a36Sopenharmony_ci memunmap(opregionvbt->vbt_ex); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci memunmap(opregionvbt->opregion); 16062306a36Sopenharmony_ci kfree(opregionvbt); 16162306a36Sopenharmony_ci} 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_cistatic const struct vfio_pci_regops vfio_pci_igd_regops = { 16462306a36Sopenharmony_ci .rw = vfio_pci_igd_rw, 16562306a36Sopenharmony_ci .release = vfio_pci_igd_release, 16662306a36Sopenharmony_ci}; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_cistatic int vfio_pci_igd_opregion_init(struct vfio_pci_core_device *vdev) 16962306a36Sopenharmony_ci{ 17062306a36Sopenharmony_ci __le32 *dwordp = (__le32 *)(vdev->vconfig + OPREGION_PCI_ADDR); 17162306a36Sopenharmony_ci u32 addr, size; 17262306a36Sopenharmony_ci struct igd_opregion_vbt *opregionvbt; 17362306a36Sopenharmony_ci int ret; 17462306a36Sopenharmony_ci u16 version; 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci ret = pci_read_config_dword(vdev->pdev, OPREGION_PCI_ADDR, &addr); 17762306a36Sopenharmony_ci if (ret) 17862306a36Sopenharmony_ci return ret; 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci if (!addr || !(~addr)) 18162306a36Sopenharmony_ci return -ENODEV; 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci opregionvbt = kzalloc(sizeof(*opregionvbt), GFP_KERNEL_ACCOUNT); 18462306a36Sopenharmony_ci if (!opregionvbt) 18562306a36Sopenharmony_ci return -ENOMEM; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci opregionvbt->opregion = memremap(addr, OPREGION_SIZE, MEMREMAP_WB); 18862306a36Sopenharmony_ci if (!opregionvbt->opregion) { 18962306a36Sopenharmony_ci kfree(opregionvbt); 19062306a36Sopenharmony_ci return -ENOMEM; 19162306a36Sopenharmony_ci } 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci if (memcmp(opregionvbt->opregion, OPREGION_SIGNATURE, 16)) { 19462306a36Sopenharmony_ci memunmap(opregionvbt->opregion); 19562306a36Sopenharmony_ci kfree(opregionvbt); 19662306a36Sopenharmony_ci return -EINVAL; 19762306a36Sopenharmony_ci } 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci size = le32_to_cpu(*(__le32 *)(opregionvbt->opregion + 16)); 20062306a36Sopenharmony_ci if (!size) { 20162306a36Sopenharmony_ci memunmap(opregionvbt->opregion); 20262306a36Sopenharmony_ci kfree(opregionvbt); 20362306a36Sopenharmony_ci return -EINVAL; 20462306a36Sopenharmony_ci } 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci size *= 1024; /* In KB */ 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci /* 20962306a36Sopenharmony_ci * OpRegion and VBT: 21062306a36Sopenharmony_ci * When VBT data doesn't exceed 6KB, it's stored in Mailbox #4. 21162306a36Sopenharmony_ci * When VBT data exceeds 6KB size, Mailbox #4 is no longer large enough 21262306a36Sopenharmony_ci * to hold the VBT data, the Extended VBT region is introduced since 21362306a36Sopenharmony_ci * OpRegion 2.0 to hold the VBT data. Since OpRegion 2.0, RVDA/RVDS are 21462306a36Sopenharmony_ci * introduced to define the extended VBT data location and size. 21562306a36Sopenharmony_ci * OpRegion 2.0: RVDA defines the absolute physical address of the 21662306a36Sopenharmony_ci * extended VBT data, RVDS defines the VBT data size. 21762306a36Sopenharmony_ci * OpRegion 2.1 and above: RVDA defines the relative address of the 21862306a36Sopenharmony_ci * extended VBT data to OpRegion base, RVDS defines the VBT data size. 21962306a36Sopenharmony_ci * 22062306a36Sopenharmony_ci * Due to the RVDA definition diff in OpRegion VBT (also the only diff 22162306a36Sopenharmony_ci * between 2.0 and 2.1), exposing OpRegion and VBT as a contiguous range 22262306a36Sopenharmony_ci * for OpRegion 2.0 and above makes it possible to support the 22362306a36Sopenharmony_ci * non-contiguous VBT through a single vfio region. From r/w ops view, 22462306a36Sopenharmony_ci * only contiguous VBT after OpRegion with version 2.1+ is exposed, 22562306a36Sopenharmony_ci * regardless the host OpRegion is 2.0 or non-contiguous 2.1+. The r/w 22662306a36Sopenharmony_ci * ops will on-the-fly shift the actural offset into VBT so that data at 22762306a36Sopenharmony_ci * correct position can be returned to the requester. 22862306a36Sopenharmony_ci */ 22962306a36Sopenharmony_ci version = le16_to_cpu(*(__le16 *)(opregionvbt->opregion + 23062306a36Sopenharmony_ci OPREGION_VERSION)); 23162306a36Sopenharmony_ci if (version >= 0x0200) { 23262306a36Sopenharmony_ci u64 rvda = le64_to_cpu(*(__le64 *)(opregionvbt->opregion + 23362306a36Sopenharmony_ci OPREGION_RVDA)); 23462306a36Sopenharmony_ci u32 rvds = le32_to_cpu(*(__le32 *)(opregionvbt->opregion + 23562306a36Sopenharmony_ci OPREGION_RVDS)); 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci /* The extended VBT is valid only when RVDA/RVDS are non-zero */ 23862306a36Sopenharmony_ci if (rvda && rvds) { 23962306a36Sopenharmony_ci size += rvds; 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci /* 24262306a36Sopenharmony_ci * Extended VBT location by RVDA: 24362306a36Sopenharmony_ci * Absolute physical addr for 2.0. 24462306a36Sopenharmony_ci * Relative addr to OpRegion header for 2.1+. 24562306a36Sopenharmony_ci */ 24662306a36Sopenharmony_ci if (version == 0x0200) 24762306a36Sopenharmony_ci addr = rvda; 24862306a36Sopenharmony_ci else 24962306a36Sopenharmony_ci addr += rvda; 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci opregionvbt->vbt_ex = memremap(addr, rvds, MEMREMAP_WB); 25262306a36Sopenharmony_ci if (!opregionvbt->vbt_ex) { 25362306a36Sopenharmony_ci memunmap(opregionvbt->opregion); 25462306a36Sopenharmony_ci kfree(opregionvbt); 25562306a36Sopenharmony_ci return -ENOMEM; 25662306a36Sopenharmony_ci } 25762306a36Sopenharmony_ci } 25862306a36Sopenharmony_ci } 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci ret = vfio_pci_core_register_dev_region(vdev, 26162306a36Sopenharmony_ci PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE, 26262306a36Sopenharmony_ci VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &vfio_pci_igd_regops, 26362306a36Sopenharmony_ci size, VFIO_REGION_INFO_FLAG_READ, opregionvbt); 26462306a36Sopenharmony_ci if (ret) { 26562306a36Sopenharmony_ci if (opregionvbt->vbt_ex) 26662306a36Sopenharmony_ci memunmap(opregionvbt->vbt_ex); 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci memunmap(opregionvbt->opregion); 26962306a36Sopenharmony_ci kfree(opregionvbt); 27062306a36Sopenharmony_ci return ret; 27162306a36Sopenharmony_ci } 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci /* Fill vconfig with the hw value and virtualize register */ 27462306a36Sopenharmony_ci *dwordp = cpu_to_le32(addr); 27562306a36Sopenharmony_ci memset(vdev->pci_config_map + OPREGION_PCI_ADDR, 27662306a36Sopenharmony_ci PCI_CAP_ID_INVALID_VIRT, 4); 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci return ret; 27962306a36Sopenharmony_ci} 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_cistatic ssize_t vfio_pci_igd_cfg_rw(struct vfio_pci_core_device *vdev, 28262306a36Sopenharmony_ci char __user *buf, size_t count, loff_t *ppos, 28362306a36Sopenharmony_ci bool iswrite) 28462306a36Sopenharmony_ci{ 28562306a36Sopenharmony_ci unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS; 28662306a36Sopenharmony_ci struct pci_dev *pdev = vdev->region[i].data; 28762306a36Sopenharmony_ci loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK; 28862306a36Sopenharmony_ci size_t size; 28962306a36Sopenharmony_ci int ret; 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci if (pos >= vdev->region[i].size || iswrite) 29262306a36Sopenharmony_ci return -EINVAL; 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci size = count = min(count, (size_t)(vdev->region[i].size - pos)); 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci if ((pos & 1) && size) { 29762306a36Sopenharmony_ci u8 val; 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_ci ret = pci_user_read_config_byte(pdev, pos, &val); 30062306a36Sopenharmony_ci if (ret) 30162306a36Sopenharmony_ci return ret; 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci if (copy_to_user(buf + count - size, &val, 1)) 30462306a36Sopenharmony_ci return -EFAULT; 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci pos++; 30762306a36Sopenharmony_ci size--; 30862306a36Sopenharmony_ci } 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci if ((pos & 3) && size > 2) { 31162306a36Sopenharmony_ci u16 val; 31262306a36Sopenharmony_ci __le16 lval; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci ret = pci_user_read_config_word(pdev, pos, &val); 31562306a36Sopenharmony_ci if (ret) 31662306a36Sopenharmony_ci return ret; 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci lval = cpu_to_le16(val); 31962306a36Sopenharmony_ci if (copy_to_user(buf + count - size, &lval, 2)) 32062306a36Sopenharmony_ci return -EFAULT; 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci pos += 2; 32362306a36Sopenharmony_ci size -= 2; 32462306a36Sopenharmony_ci } 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ci while (size > 3) { 32762306a36Sopenharmony_ci u32 val; 32862306a36Sopenharmony_ci __le32 lval; 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci ret = pci_user_read_config_dword(pdev, pos, &val); 33162306a36Sopenharmony_ci if (ret) 33262306a36Sopenharmony_ci return ret; 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_ci lval = cpu_to_le32(val); 33562306a36Sopenharmony_ci if (copy_to_user(buf + count - size, &lval, 4)) 33662306a36Sopenharmony_ci return -EFAULT; 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci pos += 4; 33962306a36Sopenharmony_ci size -= 4; 34062306a36Sopenharmony_ci } 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ci while (size >= 2) { 34362306a36Sopenharmony_ci u16 val; 34462306a36Sopenharmony_ci __le16 lval; 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_ci ret = pci_user_read_config_word(pdev, pos, &val); 34762306a36Sopenharmony_ci if (ret) 34862306a36Sopenharmony_ci return ret; 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ci lval = cpu_to_le16(val); 35162306a36Sopenharmony_ci if (copy_to_user(buf + count - size, &lval, 2)) 35262306a36Sopenharmony_ci return -EFAULT; 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci pos += 2; 35562306a36Sopenharmony_ci size -= 2; 35662306a36Sopenharmony_ci } 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci while (size) { 35962306a36Sopenharmony_ci u8 val; 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ci ret = pci_user_read_config_byte(pdev, pos, &val); 36262306a36Sopenharmony_ci if (ret) 36362306a36Sopenharmony_ci return ret; 36462306a36Sopenharmony_ci 36562306a36Sopenharmony_ci if (copy_to_user(buf + count - size, &val, 1)) 36662306a36Sopenharmony_ci return -EFAULT; 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci pos++; 36962306a36Sopenharmony_ci size--; 37062306a36Sopenharmony_ci } 37162306a36Sopenharmony_ci 37262306a36Sopenharmony_ci *ppos += count; 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_ci return count; 37562306a36Sopenharmony_ci} 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_cistatic void vfio_pci_igd_cfg_release(struct vfio_pci_core_device *vdev, 37862306a36Sopenharmony_ci struct vfio_pci_region *region) 37962306a36Sopenharmony_ci{ 38062306a36Sopenharmony_ci struct pci_dev *pdev = region->data; 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci pci_dev_put(pdev); 38362306a36Sopenharmony_ci} 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_cistatic const struct vfio_pci_regops vfio_pci_igd_cfg_regops = { 38662306a36Sopenharmony_ci .rw = vfio_pci_igd_cfg_rw, 38762306a36Sopenharmony_ci .release = vfio_pci_igd_cfg_release, 38862306a36Sopenharmony_ci}; 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_cistatic int vfio_pci_igd_cfg_init(struct vfio_pci_core_device *vdev) 39162306a36Sopenharmony_ci{ 39262306a36Sopenharmony_ci struct pci_dev *host_bridge, *lpc_bridge; 39362306a36Sopenharmony_ci int ret; 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_ci host_bridge = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0)); 39662306a36Sopenharmony_ci if (!host_bridge) 39762306a36Sopenharmony_ci return -ENODEV; 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci if (host_bridge->vendor != PCI_VENDOR_ID_INTEL || 40062306a36Sopenharmony_ci host_bridge->class != (PCI_CLASS_BRIDGE_HOST << 8)) { 40162306a36Sopenharmony_ci pci_dev_put(host_bridge); 40262306a36Sopenharmony_ci return -EINVAL; 40362306a36Sopenharmony_ci } 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ci ret = vfio_pci_core_register_dev_region(vdev, 40662306a36Sopenharmony_ci PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE, 40762306a36Sopenharmony_ci VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG, 40862306a36Sopenharmony_ci &vfio_pci_igd_cfg_regops, host_bridge->cfg_size, 40962306a36Sopenharmony_ci VFIO_REGION_INFO_FLAG_READ, host_bridge); 41062306a36Sopenharmony_ci if (ret) { 41162306a36Sopenharmony_ci pci_dev_put(host_bridge); 41262306a36Sopenharmony_ci return ret; 41362306a36Sopenharmony_ci } 41462306a36Sopenharmony_ci 41562306a36Sopenharmony_ci lpc_bridge = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0x1f, 0)); 41662306a36Sopenharmony_ci if (!lpc_bridge) 41762306a36Sopenharmony_ci return -ENODEV; 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci if (lpc_bridge->vendor != PCI_VENDOR_ID_INTEL || 42062306a36Sopenharmony_ci lpc_bridge->class != (PCI_CLASS_BRIDGE_ISA << 8)) { 42162306a36Sopenharmony_ci pci_dev_put(lpc_bridge); 42262306a36Sopenharmony_ci return -EINVAL; 42362306a36Sopenharmony_ci } 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ci ret = vfio_pci_core_register_dev_region(vdev, 42662306a36Sopenharmony_ci PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE, 42762306a36Sopenharmony_ci VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG, 42862306a36Sopenharmony_ci &vfio_pci_igd_cfg_regops, lpc_bridge->cfg_size, 42962306a36Sopenharmony_ci VFIO_REGION_INFO_FLAG_READ, lpc_bridge); 43062306a36Sopenharmony_ci if (ret) { 43162306a36Sopenharmony_ci pci_dev_put(lpc_bridge); 43262306a36Sopenharmony_ci return ret; 43362306a36Sopenharmony_ci } 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci return 0; 43662306a36Sopenharmony_ci} 43762306a36Sopenharmony_ci 43862306a36Sopenharmony_ciint vfio_pci_igd_init(struct vfio_pci_core_device *vdev) 43962306a36Sopenharmony_ci{ 44062306a36Sopenharmony_ci int ret; 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci ret = vfio_pci_igd_opregion_init(vdev); 44362306a36Sopenharmony_ci if (ret) 44462306a36Sopenharmony_ci return ret; 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_ci ret = vfio_pci_igd_cfg_init(vdev); 44762306a36Sopenharmony_ci if (ret) 44862306a36Sopenharmony_ci return ret; 44962306a36Sopenharmony_ci 45062306a36Sopenharmony_ci return 0; 45162306a36Sopenharmony_ci} 452