17fa005caSMax Gurtovoy /* SPDX-License-Identifier: GPL-2.0-only */ 27fa005caSMax Gurtovoy /* 37fa005caSMax Gurtovoy * Copyright (C) 2012 Red Hat, Inc. All rights reserved. 47fa005caSMax Gurtovoy * Author: Alex Williamson <alex.williamson@redhat.com> 57fa005caSMax Gurtovoy * 67fa005caSMax Gurtovoy * Derived from original vfio: 77fa005caSMax Gurtovoy * Copyright 2010 Cisco Systems, Inc. All rights reserved. 87fa005caSMax Gurtovoy * Author: Tom Lyon, pugs@cisco.com 97fa005caSMax Gurtovoy */ 107fa005caSMax Gurtovoy 117fa005caSMax Gurtovoy #include <linux/mutex.h> 127fa005caSMax Gurtovoy #include <linux/pci.h> 137fa005caSMax Gurtovoy #include <linux/vfio.h> 147fa005caSMax Gurtovoy #include <linux/irqbypass.h> 157fa005caSMax Gurtovoy #include <linux/types.h> 167fa005caSMax Gurtovoy #include <linux/uuid.h> 177fa005caSMax Gurtovoy #include <linux/notifier.h> 187fa005caSMax Gurtovoy 197fa005caSMax Gurtovoy #ifndef VFIO_PCI_CORE_H 207fa005caSMax Gurtovoy #define VFIO_PCI_CORE_H 217fa005caSMax Gurtovoy 227fa005caSMax Gurtovoy #define VFIO_PCI_OFFSET_SHIFT 40 237fa005caSMax Gurtovoy #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT) 247fa005caSMax Gurtovoy #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT) 257fa005caSMax Gurtovoy #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1) 267fa005caSMax Gurtovoy 277fa005caSMax Gurtovoy struct vfio_pci_core_device; 287fa005caSMax Gurtovoy struct vfio_pci_region; 297fa005caSMax Gurtovoy 307fa005caSMax Gurtovoy struct vfio_pci_regops { 317fa005caSMax Gurtovoy ssize_t (*rw)(struct vfio_pci_core_device *vdev, char __user *buf, 327fa005caSMax Gurtovoy size_t count, loff_t *ppos, bool iswrite); 337fa005caSMax Gurtovoy void (*release)(struct vfio_pci_core_device *vdev, 347fa005caSMax Gurtovoy struct vfio_pci_region *region); 357fa005caSMax Gurtovoy int (*mmap)(struct vfio_pci_core_device *vdev, 367fa005caSMax Gurtovoy struct vfio_pci_region *region, 377fa005caSMax Gurtovoy struct vm_area_struct *vma); 387fa005caSMax Gurtovoy int (*add_capability)(struct vfio_pci_core_device *vdev, 397fa005caSMax Gurtovoy struct vfio_pci_region *region, 407fa005caSMax Gurtovoy struct vfio_info_cap *caps); 417fa005caSMax Gurtovoy }; 427fa005caSMax Gurtovoy 437fa005caSMax Gurtovoy struct vfio_pci_region { 447fa005caSMax Gurtovoy u32 type; 457fa005caSMax Gurtovoy u32 subtype; 467fa005caSMax Gurtovoy const struct vfio_pci_regops *ops; 477fa005caSMax Gurtovoy void *data; 487fa005caSMax Gurtovoy size_t size; 497fa005caSMax Gurtovoy u32 flags; 507fa005caSMax Gurtovoy }; 517fa005caSMax Gurtovoy 527fa005caSMax Gurtovoy struct vfio_pci_core_device { 537fa005caSMax Gurtovoy struct vfio_device vdev; 547fa005caSMax Gurtovoy struct pci_dev *pdev; 557fa005caSMax Gurtovoy void __iomem *barmap[PCI_STD_NUM_BARS]; 567fa005caSMax Gurtovoy bool bar_mmap_supported[PCI_STD_NUM_BARS]; 577fa005caSMax Gurtovoy u8 *pci_config_map; 587fa005caSMax Gurtovoy u8 *vconfig; 597fa005caSMax Gurtovoy struct perm_bits *msi_perm; 607fa005caSMax Gurtovoy spinlock_t irqlock; 617fa005caSMax Gurtovoy struct mutex igate; 62b156e48fSReinette Chatre struct xarray ctx; 637fa005caSMax Gurtovoy int irq_type; 647fa005caSMax Gurtovoy int num_regions; 657fa005caSMax Gurtovoy struct vfio_pci_region *region; 667fa005caSMax Gurtovoy u8 msi_qmax; 677fa005caSMax Gurtovoy u8 msix_bar; 687fa005caSMax Gurtovoy u16 msix_size; 697fa005caSMax Gurtovoy u32 msix_offset; 707fa005caSMax Gurtovoy u32 rbar[7]; 71*dd27a707SReinette Chatre bool has_dyn_msix:1; 729cd0f6d5SReinette Chatre bool pci_2_3:1; 739cd0f6d5SReinette Chatre bool virq_disabled:1; 749cd0f6d5SReinette Chatre bool reset_works:1; 759cd0f6d5SReinette Chatre bool extended_caps:1; 769cd0f6d5SReinette Chatre bool bardirty:1; 779cd0f6d5SReinette Chatre bool has_vga:1; 789cd0f6d5SReinette Chatre bool needs_reset:1; 799cd0f6d5SReinette Chatre bool nointx:1; 809cd0f6d5SReinette Chatre bool needs_pm_restore:1; 819cd0f6d5SReinette Chatre bool pm_intx_masked:1; 829cd0f6d5SReinette Chatre bool pm_runtime_engaged:1; 837fa005caSMax Gurtovoy struct pci_saved_state *pci_saved_state; 847fa005caSMax Gurtovoy struct pci_saved_state *pm_save; 857fa005caSMax Gurtovoy int ioeventfds_nr; 867fa005caSMax Gurtovoy struct eventfd_ctx *err_trigger; 877fa005caSMax Gurtovoy struct eventfd_ctx *req_trigger; 88453e6c98SAbhishek Sahu struct eventfd_ctx *pm_wake_eventfd_ctx; 897fa005caSMax Gurtovoy struct list_head dummy_resources_list; 907fa005caSMax Gurtovoy struct mutex ioeventfds_lock; 917fa005caSMax Gurtovoy struct list_head ioeventfds_list; 927fa005caSMax Gurtovoy struct vfio_pci_vf_token *vf_token; 931ef3342aSJason Gunthorpe struct list_head sriov_pfs_item; 941ef3342aSJason Gunthorpe struct vfio_pci_core_device *sriov_pf_core_dev; 957fa005caSMax Gurtovoy struct notifier_block nb; 967fa005caSMax Gurtovoy struct mutex vma_lock; 977fa005caSMax Gurtovoy struct list_head vma_list; 987fa005caSMax Gurtovoy struct rw_semaphore memory_lock; 997fa005caSMax Gurtovoy }; 1007fa005caSMax Gurtovoy 101e34a0425SJason Gunthorpe /* Will be exported for vfio pci drivers usage */ 1021e979ef5SJason Gunthorpe int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, 1037fa005caSMax Gurtovoy unsigned int type, unsigned int subtype, 1047fa005caSMax Gurtovoy const struct vfio_pci_regops *ops, 1057fa005caSMax Gurtovoy size_t size, u32 flags, void *data); 1067fa005caSMax Gurtovoy void vfio_pci_core_set_params(bool nointxmask, bool is_disable_vga, 1077fa005caSMax Gurtovoy bool is_disable_idle_d3); 1087fa005caSMax Gurtovoy void vfio_pci_core_close_device(struct vfio_device *core_vdev); 10963d7c779SYi Liu int vfio_pci_core_init_dev(struct vfio_device *core_vdev); 11063d7c779SYi Liu void vfio_pci_core_release_dev(struct vfio_device *core_vdev); 1117fa005caSMax Gurtovoy int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev); 1127fa005caSMax Gurtovoy void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev); 1137fa005caSMax Gurtovoy extern const struct pci_error_handlers vfio_pci_core_err_handlers; 114ff806cbdSJason Gunthorpe int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, 115ff806cbdSJason Gunthorpe int nr_virtfn); 1167fa005caSMax Gurtovoy long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd, 1177fa005caSMax Gurtovoy unsigned long arg); 118445ad495SJason Gunthorpe int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, 119445ad495SJason Gunthorpe void __user *arg, size_t argsz); 1207fa005caSMax Gurtovoy ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf, 1217fa005caSMax Gurtovoy size_t count, loff_t *ppos); 1227fa005caSMax Gurtovoy ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf, 1237fa005caSMax Gurtovoy size_t count, loff_t *ppos); 1247fa005caSMax Gurtovoy int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma); 1257fa005caSMax Gurtovoy void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count); 1267fa005caSMax Gurtovoy int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf); 1277fa005caSMax Gurtovoy int vfio_pci_core_enable(struct vfio_pci_core_device *vdev); 1287fa005caSMax Gurtovoy void vfio_pci_core_disable(struct vfio_pci_core_device *vdev); 1297fa005caSMax Gurtovoy void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev); 130915076f7SYishai Hadas pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev, 131915076f7SYishai Hadas pci_channel_state_t state); 1327fa005caSMax Gurtovoy 1337fa005caSMax Gurtovoy #endif /* VFIO_PCI_CORE_H */ 134