27 #include <linux/pci.h>
29 #include <linux/vfio.h>
33 #define PCI_CFG_SPACE_SIZE 256
36 #define PCI_CAP_ID_BASIC 0
37 #define PCI_CAP_ID_INVALID 0xFF
39 #define is_bar(offset) \
40 ((offset >= PCI_BASE_ADDRESS_0 && offset < PCI_BASE_ADDRESS_5 + 4) || \
41 (offset >= PCI_ROM_ADDRESS && offset < PCI_ROM_ADDRESS + 4))
48 static u8 pci_cap_length[] = {
76 static u16 pci_ext_cap_length[] = {
123 #define ALL_VIRT 0xFFFFFFFFU
125 #define ALL_WRITE 0xFFFFFFFFU
127 static int vfio_user_config_read(
struct pci_dev *pdev,
int offset,
137 ret = pci_user_read_config_byte(pdev, offset, &tmp);
144 ret = pci_user_read_config_word(pdev, offset, &tmp);
149 ret = pci_user_read_config_dword(pdev, offset, &tmp_val);
155 return pcibios_err_to_errno(ret);
158 static int vfio_user_config_write(
struct pci_dev *pdev,
int offset,
166 ret = pci_user_write_config_byte(pdev, offset, tmp_val);
169 ret = pci_user_write_config_word(pdev, offset, tmp_val);
172 ret = pci_user_write_config_dword(pdev, offset, tmp_val);
176 return pcibios_err_to_errno(ret);
195 ret = vfio_user_config_read(pdev, pos, &phys_val, count);
199 *val = (phys_val & ~virt) | (*val & virt);
205 static int vfio_default_config_write(
struct vfio_pci_device *vdev,
int pos,
224 virt_val &= ~(
write & virt);
225 virt_val |= (val & (
write & virt));
236 ret = vfio_user_config_read(pdev, pos, &phys_val, count);
240 phys_val &= ~(
write & ~virt);
241 phys_val |= (val & (
write & ~virt));
243 ret = vfio_user_config_write(pdev, pos, phys_val, count);
252 static int vfio_direct_config_read(
struct vfio_pci_device *vdev,
int pos,
258 ret = vfio_user_config_read(vdev->
pdev, pos, val, count);
260 return pcibios_err_to_errno(ret);
276 static int vfio_direct_config_write(
struct vfio_pci_device *vdev,
int pos,
282 ret = vfio_user_config_write(vdev->
pdev, pos, val, count);
297 static void free_perm_bits(
struct perm_bits *perm)
324 free_perm_bits(perm);
328 perm->
readfn = vfio_default_config_read;
329 perm->
writefn = vfio_default_config_write;
351 static inline void p_setd(
struct perm_bits *p,
int off,
u32 virt,
u32 write)
370 pr_info(
"%s: %s reset recovery - restoring bars\n",
371 __func__, dev_name(&pdev->
dev));
374 pci_user_write_config_dword(pdev, i, *rbar);
379 static __le32 vfio_generate_bar_flags(
struct pci_dev *pdev,
int bar)
420 *bar |= vfio_generate_bar_flags(pdev, i);
446 static int vfio_basic_config_read(
struct vfio_pci_device *vdev,
int pos,
451 vfio_bar_fixup(vdev);
453 count = vfio_default_config_read(vdev, pos, count, perm, offset, val);
467 static int vfio_basic_config_write(
struct vfio_pci_device *vdev,
int pos,
479 bool phys_mem, virt_mem, new_mem, phys_io, virt_io, new_io;
482 ret = pci_user_read_config_word(pdev,
PCI_COMMAND, &phys_cmd);
504 if ((new_mem && virt_mem && !phys_mem) ||
505 (new_io && virt_io && !phys_io))
506 vfio_bar_restore(vdev);
509 count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
526 bool virt_intx_disable;
552 perm->
readfn = vfio_basic_config_read;
553 perm->
writefn = vfio_basic_config_write;
728 free_perm_bits(&cap_perms[PCI_CAP_ID_PM]);
731 free_perm_bits(&cap_perms[PCI_CAP_ID_AF]);
734 free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
745 ret |= init_pci_cap_pm_perm(&cap_perms[PCI_CAP_ID_PM]);
750 ret |= init_pci_cap_af_perm(&cap_perms[PCI_CAP_ID_AF]);
754 ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
792 start = vfio_find_cap_start(vdev, pos);
800 return vfio_default_config_read(vdev, pos, count, perm, offset, val);
803 static int vfio_msi_config_write(
struct vfio_pci_device *vdev,
int pos,
807 count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
817 start = vfio_find_cap_start(vdev, pos);
829 flags &= ~PCI_MSI_FLAGS_QSIZE;
835 ret = pci_user_write_config_word(vdev->
pdev,
839 return pcibios_err_to_errno(ret);
849 static int init_pci_cap_msi_perm(
struct perm_bits *perm,
int len,
u16 flags)
851 if (alloc_perm_bits(perm, len))
854 perm->
readfn = vfio_msi_config_read;
855 perm->
writefn = vfio_msi_config_write;
889 ret = pci_read_config_word(pdev, pos +
PCI_MSI_FLAGS, &flags);
891 return pcibios_err_to_errno(ret);
894 if (flags & PCI_MSI_FLAGS_64BIT)
906 ret = init_pci_cap_msi_perm(vdev->
msi_perm, len, flags);
923 return pcibios_err_to_errno(ret);
928 return pcibios_err_to_errno(ret);
964 return vfio_msi_cap_len(vdev, pos);
966 ret = pci_read_config_word(pdev, pos +
PCI_X_CMD, &word);
968 return pcibios_err_to_errno(ret);
977 ret = pci_read_config_byte(pdev, pos +
PCI_CAP_FLAGS, &byte);
979 return pcibios_err_to_errno(ret);
984 ret = pci_read_config_word(pdev, pos +
PCI_EXP_FLAGS, &word);
986 return pcibios_err_to_errno(ret);
995 ret = pci_read_config_byte(pdev, pos + 3, &byte);
997 return pcibios_err_to_errno(ret);
1002 ret = pci_read_config_byte(pdev, pos +
PCI_SATA_REGS, &byte);
1004 return pcibios_err_to_errno(ret);
1012 pr_warn(
"%s: %s unknown length for pci cap 0x%x@0x%x\n",
1013 dev_name(&pdev->
dev), __func__, cap, pos);
1028 ret = pci_read_config_dword(pdev, epos +
PCI_VSEC_HDR, &dword);
1030 return pcibios_err_to_errno(ret);
1036 return vfio_vc_cap_len(vdev, epos);
1038 ret = pci_read_config_byte(pdev, epos +
PCI_ACS_CAP, &byte);
1040 return pcibios_err_to_errno(ret);
1045 ret = pci_read_config_byte(pdev,
1049 return pcibios_err_to_errno(ret);
1051 bits = byte ?
round_up(byte, 32) : 256;
1052 return 8 + (bits / 8);
1059 return pcibios_err_to_errno(ret);
1064 return 4 + (byte * 8);
1066 ret = pci_read_config_byte(pdev, epos +
PCI_DPA_CAP, &byte);
1068 return pcibios_err_to_errno(ret);
1074 ret = pci_read_config_dword(pdev, epos +
PCI_TPH_CAP, &dword);
1076 return pcibios_err_to_errno(ret);
1087 pr_warn(
"%s: %s unknown length for pci ecap 0x%x@0x%x\n",
1088 dev_name(&pdev->
dev), __func__, ecap, epos);
1095 int offset,
int size)
1108 if (size >= 4 && !(offset % 4)) {
1112 ret = pci_read_config_dword(pdev, offset, &dword);
1117 }
else if (size >= 2 && !(offset % 2)) {
1121 ret = pci_read_config_word(pdev, offset, &word);
1128 ret = pci_read_config_byte(pdev, offset, byte);
1150 ret = pci_read_config_word(pdev,
PCI_STATUS, &status);
1166 while (pos && loops--) {
1170 ret = pci_read_config_byte(pdev, pos, &cap);
1174 ret = pci_read_config_byte(pdev,
1180 len = pci_cap_length[
cap];
1182 len = vfio_cap_len(vdev, cap, pos);
1189 pr_info(
"%s: %s hiding cap 0x%x\n",
1190 __func__, dev_name(&pdev->
dev), cap);
1197 for (i = 0; i < len; i += 4) {
1201 pr_warn(
"%s: %s pci config conflict @0x%x, was cap 0x%x now cap 0x%x\n",
1202 __func__, dev_name(&pdev->
dev),
1203 pos + i, map[pos + i], cap);
1206 memset(map + (pos / 4), cap, len / 4);
1207 ret = vfio_fill_vconfig_bytes(vdev, pos, len);
1231 int loops,
ret, ecaps = 0;
1244 bool hidden =
false;
1246 ret = pci_read_config_dword(pdev, epos, &header);
1253 len = pci_ext_cap_length[ecap];
1255 len = vfio_ext_cap_len(vdev, ecap, epos);
1262 pr_info(
"%s: %s hiding ecap 0x%x@0x%x\n",
1263 __func__, dev_name(&pdev->
dev), ecap, epos);
1281 for (i = 0; i < len; i += 4) {
1285 pr_warn(
"%s: %s pci config conflict @0x%x, was ecap 0x%x now ecap 0x%x\n",
1286 __func__, dev_name(&pdev->
dev),
1287 epos + i, map[epos + i], ecap);
1297 memset(map + (epos / 4), ecap, len / 4);
1298 ret = vfio_fill_vconfig_bytes(vdev, epos, len);
1389 ret = vfio_cap_init(vdev);
1393 ret = vfio_ecap_init(vdev);
1404 return pcibios_err_to_errno(ret);
1418 size_t count, loff_t *ppos,
bool iswrite)
1423 int cap_start = 0,
offset;
1427 if (*ppos < 0 || *ppos + count > pdev->
cfg_size)
1434 if (count >
sizeof(val))
1461 perm = &ecap_perms[
cap_id];
1462 cap_start = vfio_find_cap_start(vdev, *ppos);
1467 perm = &cap_perms[
cap_id];
1473 cap_start = vfio_find_cap_start(vdev, *ppos);
1479 offset = *ppos - cap_start;
1488 ret = perm->
writefn(vdev, *ppos, count, perm, offset, val);
1491 ret = perm->
readfn(vdev, *ppos, count,
1492 perm, offset, &val);
1505 char __user *buf,
size_t count,
1506 loff_t *ppos,
bool iswrite)
1521 if (count >= 4 && !(pos % 4))
1522 ret = vfio_config_do_rw(vdev, buf, 4, &pos, iswrite);
1523 else if (count >= 2 && !(pos % 2))
1524 ret = vfio_config_do_rw(vdev, buf, 2, &pos, iswrite);
1526 ret = vfio_config_do_rw(vdev, buf, 1, &pos, iswrite);