0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #include <linux/kernel.h>
0041 #include <linux/module.h>
0042 #include <linux/pci.h>
0043 #include <linux/pci-ecam.h>
0044 #include <linux/delay.h>
0045 #include <linux/semaphore.h>
0046 #include <linux/irq.h>
0047 #include <linux/msi.h>
0048 #include <linux/hyperv.h>
0049 #include <linux/refcount.h>
0050 #include <linux/irqdomain.h>
0051 #include <linux/acpi.h>
0052 #include <asm/mshyperv.h>
0053
0054
0055
0056
0057
0058
0059 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
0060 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
0061 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
0062
0063 enum pci_protocol_version_t {
0064 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),
0065 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2),
0066 PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3),
0067 PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4),
0068 };
0069
0070 #define CPU_AFFINITY_ALL -1ULL
0071
0072
0073
0074
0075
0076 static enum pci_protocol_version_t pci_protocol_versions[] = {
0077 PCI_PROTOCOL_VERSION_1_4,
0078 PCI_PROTOCOL_VERSION_1_3,
0079 PCI_PROTOCOL_VERSION_1_2,
0080 PCI_PROTOCOL_VERSION_1_1,
0081 };
0082
0083 #define PCI_CONFIG_MMIO_LENGTH 0x2000
0084 #define CFG_PAGE_OFFSET 0x1000
0085 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
0086
0087 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
0088
0089 #define STATUS_REVISION_MISMATCH 0xC0000059
0090
0091
0092 #define SLOT_NAME_SIZE 11
0093
0094
0095
0096
0097
0098
0099 #define HV_PCI_RQSTOR_SIZE 64
0100
0101
0102
0103
0104
0105 enum pci_message_type {
0106
0107
0108
0109 PCI_MESSAGE_BASE = 0x42490000,
0110 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
0111 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
0112 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
0113 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
0114 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
0115 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
0116 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
0117 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
0118 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
0119 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
0120 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
0121 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
0122 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
0123 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
0124 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
0125 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
0126 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
0127 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
0128 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
0129 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
0130 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
0131 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
0132 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18,
0133 PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
0134 PCI_RESOURCES_ASSIGNED3 = PCI_MESSAGE_BASE + 0x1A,
0135 PCI_CREATE_INTERRUPT_MESSAGE3 = PCI_MESSAGE_BASE + 0x1B,
0136 PCI_MESSAGE_MAXIMUM
0137 };
0138
0139
0140
0141
0142
0143 union pci_version {
0144 struct {
0145 u16 minor_version;
0146 u16 major_version;
0147 } parts;
0148 u32 version;
0149 } __packed;
0150
0151
0152
0153
0154
0155
0156
0157 union win_slot_encoding {
0158 struct {
0159 u32 dev:5;
0160 u32 func:3;
0161 u32 reserved:24;
0162 } bits;
0163 u32 slot;
0164 } __packed;
0165
0166
0167
0168
0169 struct pci_function_description {
0170 u16 v_id;
0171 u16 d_id;
0172 u8 rev;
0173 u8 prog_intf;
0174 u8 subclass;
0175 u8 base_class;
0176 u32 subsystem_id;
0177 union win_slot_encoding win_slot;
0178 u32 ser;
0179 } __packed;
0180
0181 enum pci_device_description_flags {
0182 HV_PCI_DEVICE_FLAG_NONE = 0x0,
0183 HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
0184 };
0185
0186 struct pci_function_description2 {
0187 u16 v_id;
0188 u16 d_id;
0189 u8 rev;
0190 u8 prog_intf;
0191 u8 subclass;
0192 u8 base_class;
0193 u32 subsystem_id;
0194 union win_slot_encoding win_slot;
0195 u32 ser;
0196 u32 flags;
0197 u16 virtual_numa_node;
0198 u16 reserved;
0199 } __packed;
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217 struct hv_msi_desc {
0218 u8 vector;
0219 u8 delivery_mode;
0220 u16 vector_count;
0221 u32 reserved;
0222 u64 cpu_mask;
0223 } __packed;
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241 struct hv_msi_desc2 {
0242 u8 vector;
0243 u8 delivery_mode;
0244 u16 vector_count;
0245 u16 processor_count;
0246 u16 processor_array[32];
0247 } __packed;
0248
0249
0250
0251
0252
0253
0254
0255 struct hv_msi_desc3 {
0256 u32 vector;
0257 u8 delivery_mode;
0258 u8 reserved;
0259 u16 vector_count;
0260 u16 processor_count;
0261 u16 processor_array[32];
0262 } __packed;
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 struct tran_int_desc {
0277 u16 reserved;
0278 u16 vector_count;
0279 u32 data;
0280 u64 address;
0281 } __packed;
0282
0283
0284
0285
0286
0287
0288 struct pci_message {
0289 u32 type;
0290 } __packed;
0291
0292 struct pci_child_message {
0293 struct pci_message message_type;
0294 union win_slot_encoding wslot;
0295 } __packed;
0296
0297 struct pci_incoming_message {
0298 struct vmpacket_descriptor hdr;
0299 struct pci_message message_type;
0300 } __packed;
0301
0302 struct pci_response {
0303 struct vmpacket_descriptor hdr;
0304 s32 status;
0305 } __packed;
0306
0307 struct pci_packet {
0308 void (*completion_func)(void *context, struct pci_response *resp,
0309 int resp_packet_size);
0310 void *compl_ctxt;
0311
0312 struct pci_message message[];
0313 };
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329 struct pci_version_request {
0330 struct pci_message message_type;
0331 u32 protocol_version;
0332 } __packed;
0333
0334
0335
0336
0337
0338
0339 struct pci_bus_d0_entry {
0340 struct pci_message message_type;
0341 u32 reserved;
0342 u64 mmio_base;
0343 } __packed;
0344
0345 struct pci_bus_relations {
0346 struct pci_incoming_message incoming;
0347 u32 device_count;
0348 struct pci_function_description func[];
0349 } __packed;
0350
0351 struct pci_bus_relations2 {
0352 struct pci_incoming_message incoming;
0353 u32 device_count;
0354 struct pci_function_description2 func[];
0355 } __packed;
0356
0357 struct pci_q_res_req_response {
0358 struct vmpacket_descriptor hdr;
0359 s32 status;
0360 u32 probed_bar[PCI_STD_NUM_BARS];
0361 } __packed;
0362
0363 struct pci_set_power {
0364 struct pci_message message_type;
0365 union win_slot_encoding wslot;
0366 u32 power_state;
0367 u32 reserved;
0368 } __packed;
0369
0370 struct pci_set_power_response {
0371 struct vmpacket_descriptor hdr;
0372 s32 status;
0373 union win_slot_encoding wslot;
0374 u32 resultant_state;
0375 u32 reserved;
0376 } __packed;
0377
0378 struct pci_resources_assigned {
0379 struct pci_message message_type;
0380 union win_slot_encoding wslot;
0381 u8 memory_range[0x14][6];
0382 u32 msi_descriptors;
0383 u32 reserved[4];
0384 } __packed;
0385
0386 struct pci_resources_assigned2 {
0387 struct pci_message message_type;
0388 union win_slot_encoding wslot;
0389 u8 memory_range[0x14][6];
0390 u32 msi_descriptor_count;
0391 u8 reserved[70];
0392 } __packed;
0393
0394 struct pci_create_interrupt {
0395 struct pci_message message_type;
0396 union win_slot_encoding wslot;
0397 struct hv_msi_desc int_desc;
0398 } __packed;
0399
0400 struct pci_create_int_response {
0401 struct pci_response response;
0402 u32 reserved;
0403 struct tran_int_desc int_desc;
0404 } __packed;
0405
0406 struct pci_create_interrupt2 {
0407 struct pci_message message_type;
0408 union win_slot_encoding wslot;
0409 struct hv_msi_desc2 int_desc;
0410 } __packed;
0411
0412 struct pci_create_interrupt3 {
0413 struct pci_message message_type;
0414 union win_slot_encoding wslot;
0415 struct hv_msi_desc3 int_desc;
0416 } __packed;
0417
0418 struct pci_delete_interrupt {
0419 struct pci_message message_type;
0420 union win_slot_encoding wslot;
0421 struct tran_int_desc int_desc;
0422 } __packed;
0423
0424
0425
0426
0427 struct pci_read_block {
0428 struct pci_message message_type;
0429 u32 block_id;
0430 union win_slot_encoding wslot;
0431 u32 bytes_requested;
0432 } __packed;
0433
0434 struct pci_read_block_response {
0435 struct vmpacket_descriptor hdr;
0436 u32 status;
0437 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
0438 } __packed;
0439
0440
0441
0442
0443 struct pci_write_block {
0444 struct pci_message message_type;
0445 u32 block_id;
0446 union win_slot_encoding wslot;
0447 u32 byte_count;
0448 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
0449 } __packed;
0450
0451 struct pci_dev_inval_block {
0452 struct pci_incoming_message incoming;
0453 union win_slot_encoding wslot;
0454 u64 block_mask;
0455 } __packed;
0456
0457 struct pci_dev_incoming {
0458 struct pci_incoming_message incoming;
0459 union win_slot_encoding wslot;
0460 } __packed;
0461
0462 struct pci_eject_response {
0463 struct pci_message message_type;
0464 union win_slot_encoding wslot;
0465 u32 status;
0466 } __packed;
0467
0468 static int pci_ring_size = (4 * PAGE_SIZE);
0469
0470
0471
0472
0473
0474 enum hv_pcibus_state {
0475 hv_pcibus_init = 0,
0476 hv_pcibus_probed,
0477 hv_pcibus_installed,
0478 hv_pcibus_removing,
0479 hv_pcibus_maximum
0480 };
0481
0482 struct hv_pcibus_device {
0483 #ifdef CONFIG_X86
0484 struct pci_sysdata sysdata;
0485 #elif defined(CONFIG_ARM64)
0486 struct pci_config_window sysdata;
0487 #endif
0488 struct pci_host_bridge *bridge;
0489 struct fwnode_handle *fwnode;
0490
0491 enum pci_protocol_version_t protocol_version;
0492 enum hv_pcibus_state state;
0493 struct hv_device *hdev;
0494 resource_size_t low_mmio_space;
0495 resource_size_t high_mmio_space;
0496 struct resource *mem_config;
0497 struct resource *low_mmio_res;
0498 struct resource *high_mmio_res;
0499 struct completion *survey_event;
0500 struct pci_bus *pci_bus;
0501 spinlock_t config_lock;
0502 spinlock_t device_list_lock;
0503 void __iomem *cfg_addr;
0504
0505 struct list_head children;
0506 struct list_head dr_list;
0507
0508 struct msi_domain_info msi_info;
0509 struct irq_domain *irq_domain;
0510
0511 spinlock_t retarget_msi_interrupt_lock;
0512
0513 struct workqueue_struct *wq;
0514
0515
0516 int wslot_res_allocated;
0517
0518
0519 struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
0520
0521
0522
0523
0524 };
0525
0526
0527
0528
0529
0530
0531 struct hv_dr_work {
0532 struct work_struct wrk;
0533 struct hv_pcibus_device *bus;
0534 };
0535
0536 struct hv_pcidev_description {
0537 u16 v_id;
0538 u16 d_id;
0539 u8 rev;
0540 u8 prog_intf;
0541 u8 subclass;
0542 u8 base_class;
0543 u32 subsystem_id;
0544 union win_slot_encoding win_slot;
0545 u32 ser;
0546 u32 flags;
0547 u16 virtual_numa_node;
0548 };
0549
0550 struct hv_dr_state {
0551 struct list_head list_entry;
0552 u32 device_count;
0553 struct hv_pcidev_description func[];
0554 };
0555
0556 enum hv_pcichild_state {
0557 hv_pcichild_init = 0,
0558 hv_pcichild_requirements,
0559 hv_pcichild_resourced,
0560 hv_pcichild_ejecting,
0561 hv_pcichild_maximum
0562 };
0563
0564 struct hv_pci_dev {
0565
0566 struct list_head list_entry;
0567 refcount_t refs;
0568 enum hv_pcichild_state state;
0569 struct pci_slot *pci_slot;
0570 struct hv_pcidev_description desc;
0571 bool reported_missing;
0572 struct hv_pcibus_device *hbus;
0573 struct work_struct wrk;
0574
0575 void (*block_invalidate)(void *context, u64 block_mask);
0576 void *invalidate_context;
0577
0578
0579
0580
0581
0582 u32 probed_bar[PCI_STD_NUM_BARS];
0583 };
0584
0585 struct hv_pci_compl {
0586 struct completion host_event;
0587 s32 completion_status;
0588 };
0589
0590 static void hv_pci_onchannelcallback(void *context);
0591
0592 #ifdef CONFIG_X86
0593 #define DELIVERY_MODE APIC_DELIVERY_MODE_FIXED
0594 #define FLOW_HANDLER handle_edge_irq
0595 #define FLOW_NAME "edge"
0596
0597 static int hv_pci_irqchip_init(void)
0598 {
0599 return 0;
0600 }
0601
0602 static struct irq_domain *hv_pci_get_root_domain(void)
0603 {
0604 return x86_vector_domain;
0605 }
0606
0607 static unsigned int hv_msi_get_int_vector(struct irq_data *data)
0608 {
0609 struct irq_cfg *cfg = irqd_cfg(data);
0610
0611 return cfg->vector;
0612 }
0613
0614 static int hv_msi_prepare(struct irq_domain *domain, struct device *dev,
0615 int nvec, msi_alloc_info_t *info)
0616 {
0617 int ret = pci_msi_prepare(domain, dev, nvec, info);
0618
0619
0620
0621
0622
0623 if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
0624 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
0625
0626 return ret;
0627 }
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639 static void hv_arch_irq_unmask(struct irq_data *data)
0640 {
0641 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
0642 struct hv_retarget_device_interrupt *params;
0643 struct tran_int_desc *int_desc;
0644 struct hv_pcibus_device *hbus;
0645 const struct cpumask *dest;
0646 cpumask_var_t tmp;
0647 struct pci_bus *pbus;
0648 struct pci_dev *pdev;
0649 unsigned long flags;
0650 u32 var_size = 0;
0651 int cpu, nr_bank;
0652 u64 res;
0653
0654 dest = irq_data_get_effective_affinity_mask(data);
0655 pdev = msi_desc_to_pci_dev(msi_desc);
0656 pbus = pdev->bus;
0657 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
0658 int_desc = data->chip_data;
0659
0660 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
0661
0662 params = &hbus->retarget_msi_interrupt_params;
0663 memset(params, 0, sizeof(*params));
0664 params->partition_id = HV_PARTITION_ID_SELF;
0665 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
0666 params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff;
0667 params->int_entry.msi_entry.data.as_uint32 = int_desc->data;
0668 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
0669 (hbus->hdev->dev_instance.b[4] << 16) |
0670 (hbus->hdev->dev_instance.b[7] << 8) |
0671 (hbus->hdev->dev_instance.b[6] & 0xf8) |
0672 PCI_FUNC(pdev->devfn);
0673 params->int_target.vector = hv_msi_get_int_vector(data);
0674
0675
0676
0677
0678
0679
0680
0681
0682 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
0683
0684
0685
0686
0687
0688
0689
0690 params->int_target.flags |=
0691 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
0692
0693 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
0694 res = 1;
0695 goto exit_unlock;
0696 }
0697
0698 cpumask_and(tmp, dest, cpu_online_mask);
0699 nr_bank = cpumask_to_vpset(¶ms->int_target.vp_set, tmp);
0700 free_cpumask_var(tmp);
0701
0702 if (nr_bank <= 0) {
0703 res = 1;
0704 goto exit_unlock;
0705 }
0706
0707
0708
0709
0710
0711
0712 var_size = 1 + nr_bank;
0713 } else {
0714 for_each_cpu_and(cpu, dest, cpu_online_mask) {
0715 params->int_target.vp_mask |=
0716 (1ULL << hv_cpu_number_to_vp_number(cpu));
0717 }
0718 }
0719
0720 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
0721 params, NULL);
0722
0723 exit_unlock:
0724 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
0743 dev_err(&hbus->hdev->device,
0744 "%s() failed: %#llx", __func__, res);
0745 }
0746 #elif defined(CONFIG_ARM64)
0747
0748
0749
0750
0751
0752 #define HV_PCI_MSI_SPI_START 64
0753 #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
0754 #define DELIVERY_MODE 0
0755 #define FLOW_HANDLER NULL
0756 #define FLOW_NAME NULL
0757 #define hv_msi_prepare NULL
0758
0759 struct hv_pci_chip_data {
0760 DECLARE_BITMAP(spi_map, HV_PCI_MSI_SPI_NR);
0761 struct mutex map_lock;
0762 };
0763
0764
0765 static struct irq_domain *hv_msi_gic_irq_domain;
0766
0767
0768 static struct irq_chip hv_arm64_msi_irq_chip = {
0769 .name = "MSI",
0770 .irq_set_affinity = irq_chip_set_affinity_parent,
0771 .irq_eoi = irq_chip_eoi_parent,
0772 .irq_mask = irq_chip_mask_parent,
0773 .irq_unmask = irq_chip_unmask_parent
0774 };
0775
0776 static unsigned int hv_msi_get_int_vector(struct irq_data *irqd)
0777 {
0778 return irqd->parent_data->hwirq;
0779 }
0780
0781
0782
0783
0784
0785
0786
0787 static void hv_pci_vec_irq_free(struct irq_domain *domain,
0788 unsigned int virq,
0789 unsigned int nr_bm_irqs,
0790 unsigned int nr_dom_irqs)
0791 {
0792 struct hv_pci_chip_data *chip_data = domain->host_data;
0793 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
0794 int first = d->hwirq - HV_PCI_MSI_SPI_START;
0795 int i;
0796
0797 mutex_lock(&chip_data->map_lock);
0798 bitmap_release_region(chip_data->spi_map,
0799 first,
0800 get_count_order(nr_bm_irqs));
0801 mutex_unlock(&chip_data->map_lock);
0802 for (i = 0; i < nr_dom_irqs; i++) {
0803 if (i)
0804 d = irq_domain_get_irq_data(domain, virq + i);
0805 irq_domain_reset_irq_data(d);
0806 }
0807
0808 irq_domain_free_irqs_parent(domain, virq, nr_dom_irqs);
0809 }
0810
0811 static void hv_pci_vec_irq_domain_free(struct irq_domain *domain,
0812 unsigned int virq,
0813 unsigned int nr_irqs)
0814 {
0815 hv_pci_vec_irq_free(domain, virq, nr_irqs, nr_irqs);
0816 }
0817
0818 static int hv_pci_vec_alloc_device_irq(struct irq_domain *domain,
0819 unsigned int nr_irqs,
0820 irq_hw_number_t *hwirq)
0821 {
0822 struct hv_pci_chip_data *chip_data = domain->host_data;
0823 int index;
0824
0825
0826 mutex_lock(&chip_data->map_lock);
0827 index = bitmap_find_free_region(chip_data->spi_map,
0828 HV_PCI_MSI_SPI_NR,
0829 get_count_order(nr_irqs));
0830 mutex_unlock(&chip_data->map_lock);
0831 if (index < 0)
0832 return -ENOSPC;
0833
0834 *hwirq = index + HV_PCI_MSI_SPI_START;
0835
0836 return 0;
0837 }
0838
0839 static int hv_pci_vec_irq_gic_domain_alloc(struct irq_domain *domain,
0840 unsigned int virq,
0841 irq_hw_number_t hwirq)
0842 {
0843 struct irq_fwspec fwspec;
0844 struct irq_data *d;
0845 int ret;
0846
0847 fwspec.fwnode = domain->parent->fwnode;
0848 fwspec.param_count = 2;
0849 fwspec.param[0] = hwirq;
0850 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
0851
0852 ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
0853 if (ret)
0854 return ret;
0855
0856
0857
0858
0859
0860
0861 d = irq_domain_get_irq_data(domain->parent, virq);
0862
0863 return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
0864 }
0865
0866 static int hv_pci_vec_irq_domain_alloc(struct irq_domain *domain,
0867 unsigned int virq, unsigned int nr_irqs,
0868 void *args)
0869 {
0870 irq_hw_number_t hwirq;
0871 unsigned int i;
0872 int ret;
0873
0874 ret = hv_pci_vec_alloc_device_irq(domain, nr_irqs, &hwirq);
0875 if (ret)
0876 return ret;
0877
0878 for (i = 0; i < nr_irqs; i++) {
0879 ret = hv_pci_vec_irq_gic_domain_alloc(domain, virq + i,
0880 hwirq + i);
0881 if (ret) {
0882 hv_pci_vec_irq_free(domain, virq, nr_irqs, i);
0883 return ret;
0884 }
0885
0886 irq_domain_set_hwirq_and_chip(domain, virq + i,
0887 hwirq + i,
0888 &hv_arm64_msi_irq_chip,
0889 domain->host_data);
0890 pr_debug("pID:%d vID:%u\n", (int)(hwirq + i), virq + i);
0891 }
0892
0893 return 0;
0894 }
0895
0896
0897
0898
0899
0900
0901
0902 static int hv_pci_vec_irq_domain_activate(struct irq_domain *domain,
0903 struct irq_data *irqd, bool reserve)
0904 {
0905 int cpu = cpumask_first(cpu_present_mask);
0906
0907 irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
0908
0909 return 0;
0910 }
0911
0912 static const struct irq_domain_ops hv_pci_domain_ops = {
0913 .alloc = hv_pci_vec_irq_domain_alloc,
0914 .free = hv_pci_vec_irq_domain_free,
0915 .activate = hv_pci_vec_irq_domain_activate,
0916 };
0917
0918 static int hv_pci_irqchip_init(void)
0919 {
0920 static struct hv_pci_chip_data *chip_data;
0921 struct fwnode_handle *fn = NULL;
0922 int ret = -ENOMEM;
0923
0924 chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
0925 if (!chip_data)
0926 return ret;
0927
0928 mutex_init(&chip_data->map_lock);
0929 fn = irq_domain_alloc_named_fwnode("hv_vpci_arm64");
0930 if (!fn)
0931 goto free_chip;
0932
0933
0934
0935
0936
0937
0938 hv_msi_gic_irq_domain = acpi_irq_create_hierarchy(0, HV_PCI_MSI_SPI_NR,
0939 fn, &hv_pci_domain_ops,
0940 chip_data);
0941
0942 if (!hv_msi_gic_irq_domain) {
0943 pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n");
0944 goto free_chip;
0945 }
0946
0947 return 0;
0948
0949 free_chip:
0950 kfree(chip_data);
0951 if (fn)
0952 irq_domain_free_fwnode(fn);
0953
0954 return ret;
0955 }
0956
0957 static struct irq_domain *hv_pci_get_root_domain(void)
0958 {
0959 return hv_msi_gic_irq_domain;
0960 }
0961
0962
0963
0964
0965
0966 static void hv_arch_irq_unmask(struct irq_data *data) { }
0967 #endif
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979 static void hv_pci_generic_compl(void *context, struct pci_response *resp,
0980 int resp_packet_size)
0981 {
0982 struct hv_pci_compl *comp_pkt = context;
0983
0984 comp_pkt->completion_status = resp->status;
0985 complete(&comp_pkt->host_event);
0986 }
0987
0988 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
0989 u32 wslot);
0990
0991 static void get_pcichild(struct hv_pci_dev *hpdev)
0992 {
0993 refcount_inc(&hpdev->refs);
0994 }
0995
0996 static void put_pcichild(struct hv_pci_dev *hpdev)
0997 {
0998 if (refcount_dec_and_test(&hpdev->refs))
0999 kfree(hpdev);
1000 }
1001
1002
1003
1004
1005
1006 static int wait_for_response(struct hv_device *hdev,
1007 struct completion *comp)
1008 {
1009 while (true) {
1010 if (hdev->channel->rescind) {
1011 dev_warn_once(&hdev->device, "The device is gone.\n");
1012 return -ENODEV;
1013 }
1014
1015 if (wait_for_completion_timeout(comp, HZ / 10))
1016 break;
1017 }
1018
1019 return 0;
1020 }
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030 static u32 devfn_to_wslot(int devfn)
1031 {
1032 union win_slot_encoding wslot;
1033
1034 wslot.slot = 0;
1035 wslot.bits.dev = PCI_SLOT(devfn);
1036 wslot.bits.func = PCI_FUNC(devfn);
1037
1038 return wslot.slot;
1039 }
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049 static int wslot_to_devfn(u32 wslot)
1050 {
1051 union win_slot_encoding slot_no;
1052
1053 slot_no.slot = wslot;
1054 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072 static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
1073 int size, u32 *val)
1074 {
1075 unsigned long flags;
1076 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
1077
1078
1079
1080
1081 if (where + size <= PCI_COMMAND) {
1082 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
1083 } else if (where >= PCI_CLASS_REVISION && where + size <=
1084 PCI_CACHE_LINE_SIZE) {
1085 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
1086 PCI_CLASS_REVISION, size);
1087 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
1088 PCI_ROM_ADDRESS) {
1089 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
1090 PCI_SUBSYSTEM_VENDOR_ID, size);
1091 } else if (where >= PCI_ROM_ADDRESS && where + size <=
1092 PCI_CAPABILITY_LIST) {
1093
1094 *val = 0;
1095 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
1096 PCI_INTERRUPT_PIN) {
1097
1098
1099
1100
1101
1102 *val = 0;
1103 } else if (where + size <= CFG_PAGE_SIZE) {
1104 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
1105
1106 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
1107
1108 mb();
1109
1110 switch (size) {
1111 case 1:
1112 *val = readb(addr);
1113 break;
1114 case 2:
1115 *val = readw(addr);
1116 break;
1117 default:
1118 *val = readl(addr);
1119 break;
1120 }
1121
1122
1123
1124
1125 mb();
1126 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1127 } else {
1128 dev_err(&hpdev->hbus->hdev->device,
1129 "Attempt to read beyond a function's config space.\n");
1130 }
1131 }
1132
1133 static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
1134 {
1135 u16 ret;
1136 unsigned long flags;
1137 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
1138 PCI_VENDOR_ID;
1139
1140 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
1141
1142
1143 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
1144
1145 mb();
1146
1147 ret = readw(addr);
1148
1149
1150
1151
1152
1153 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1154
1155 return ret;
1156 }
1157
1158
1159
1160
1161
1162
1163
1164
1165 static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
1166 int size, u32 val)
1167 {
1168 unsigned long flags;
1169 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
1170
1171 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
1172 where + size <= PCI_CAPABILITY_LIST) {
1173
1174 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
1175 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
1176
1177 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
1178
1179 wmb();
1180
1181 switch (size) {
1182 case 1:
1183 writeb(val, addr);
1184 break;
1185 case 2:
1186 writew(val, addr);
1187 break;
1188 default:
1189 writel(val, addr);
1190 break;
1191 }
1192
1193
1194
1195
1196 mb();
1197 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1198 } else {
1199 dev_err(&hpdev->hbus->hdev->device,
1200 "Attempt to write beyond a function's config space.\n");
1201 }
1202 }
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215 static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
1216 int where, int size, u32 *val)
1217 {
1218 struct hv_pcibus_device *hbus =
1219 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
1220 struct hv_pci_dev *hpdev;
1221
1222 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
1223 if (!hpdev)
1224 return PCIBIOS_DEVICE_NOT_FOUND;
1225
1226 _hv_pcifront_read_config(hpdev, where, size, val);
1227
1228 put_pcichild(hpdev);
1229 return PCIBIOS_SUCCESSFUL;
1230 }
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243 static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
1244 int where, int size, u32 val)
1245 {
1246 struct hv_pcibus_device *hbus =
1247 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
1248 struct hv_pci_dev *hpdev;
1249
1250 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
1251 if (!hpdev)
1252 return PCIBIOS_DEVICE_NOT_FOUND;
1253
1254 _hv_pcifront_write_config(hpdev, where, size, val);
1255
1256 put_pcichild(hpdev);
1257 return PCIBIOS_SUCCESSFUL;
1258 }
1259
1260
1261 static struct pci_ops hv_pcifront_ops = {
1262 .read = hv_pcifront_read_config,
1263 .write = hv_pcifront_write_config,
1264 };
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 struct hv_read_config_compl {
1292 struct hv_pci_compl comp_pkt;
1293 void *buf;
1294 unsigned int len;
1295 unsigned int bytes_returned;
1296 };
1297
1298
1299
1300
1301
1302
1303
1304
1305 static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
1306 int resp_packet_size)
1307 {
1308 struct hv_read_config_compl *comp = context;
1309 struct pci_read_block_response *read_resp =
1310 (struct pci_read_block_response *)resp;
1311 unsigned int data_len, hdr_len;
1312
1313 hdr_len = offsetof(struct pci_read_block_response, bytes);
1314 if (resp_packet_size < hdr_len) {
1315 comp->comp_pkt.completion_status = -1;
1316 goto out;
1317 }
1318
1319 data_len = resp_packet_size - hdr_len;
1320 if (data_len > 0 && read_resp->status == 0) {
1321 comp->bytes_returned = min(comp->len, data_len);
1322 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
1323 } else {
1324 comp->bytes_returned = 0;
1325 }
1326
1327 comp->comp_pkt.completion_status = read_resp->status;
1328 out:
1329 complete(&comp->comp_pkt.host_event);
1330 }
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343 static int hv_read_config_block(struct pci_dev *pdev, void *buf,
1344 unsigned int len, unsigned int block_id,
1345 unsigned int *bytes_returned)
1346 {
1347 struct hv_pcibus_device *hbus =
1348 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1349 sysdata);
1350 struct {
1351 struct pci_packet pkt;
1352 char buf[sizeof(struct pci_read_block)];
1353 } pkt;
1354 struct hv_read_config_compl comp_pkt;
1355 struct pci_read_block *read_blk;
1356 int ret;
1357
1358 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1359 return -EINVAL;
1360
1361 init_completion(&comp_pkt.comp_pkt.host_event);
1362 comp_pkt.buf = buf;
1363 comp_pkt.len = len;
1364
1365 memset(&pkt, 0, sizeof(pkt));
1366 pkt.pkt.completion_func = hv_pci_read_config_compl;
1367 pkt.pkt.compl_ctxt = &comp_pkt;
1368 read_blk = (struct pci_read_block *)&pkt.pkt.message;
1369 read_blk->message_type.type = PCI_READ_BLOCK;
1370 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1371 read_blk->block_id = block_id;
1372 read_blk->bytes_requested = len;
1373
1374 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
1375 sizeof(*read_blk), (unsigned long)&pkt.pkt,
1376 VM_PKT_DATA_INBAND,
1377 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1378 if (ret)
1379 return ret;
1380
1381 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
1382 if (ret)
1383 return ret;
1384
1385 if (comp_pkt.comp_pkt.completion_status != 0 ||
1386 comp_pkt.bytes_returned == 0) {
1387 dev_err(&hbus->hdev->device,
1388 "Read Config Block failed: 0x%x, bytes_returned=%d\n",
1389 comp_pkt.comp_pkt.completion_status,
1390 comp_pkt.bytes_returned);
1391 return -EIO;
1392 }
1393
1394 *bytes_returned = comp_pkt.bytes_returned;
1395 return 0;
1396 }
1397
1398
1399
1400
1401
1402
1403
1404
1405 static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
1406 int resp_packet_size)
1407 {
1408 struct hv_pci_compl *comp_pkt = context;
1409
1410 comp_pkt->completion_status = resp->status;
1411 complete(&comp_pkt->host_event);
1412 }
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424 static int hv_write_config_block(struct pci_dev *pdev, void *buf,
1425 unsigned int len, unsigned int block_id)
1426 {
1427 struct hv_pcibus_device *hbus =
1428 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1429 sysdata);
1430 struct {
1431 struct pci_packet pkt;
1432 char buf[sizeof(struct pci_write_block)];
1433 u32 reserved;
1434 } pkt;
1435 struct hv_pci_compl comp_pkt;
1436 struct pci_write_block *write_blk;
1437 u32 pkt_size;
1438 int ret;
1439
1440 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1441 return -EINVAL;
1442
1443 init_completion(&comp_pkt.host_event);
1444
1445 memset(&pkt, 0, sizeof(pkt));
1446 pkt.pkt.completion_func = hv_pci_write_config_compl;
1447 pkt.pkt.compl_ctxt = &comp_pkt;
1448 write_blk = (struct pci_write_block *)&pkt.pkt.message;
1449 write_blk->message_type.type = PCI_WRITE_BLOCK;
1450 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1451 write_blk->block_id = block_id;
1452 write_blk->byte_count = len;
1453 memcpy(write_blk->bytes, buf, len);
1454 pkt_size = offsetof(struct pci_write_block, bytes) + len;
1455
1456
1457
1458
1459
1460
1461
1462 pkt_size += sizeof(pkt.reserved);
1463
1464 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
1465 (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
1466 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1467 if (ret)
1468 return ret;
1469
1470 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
1471 if (ret)
1472 return ret;
1473
1474 if (comp_pkt.completion_status != 0) {
1475 dev_err(&hbus->hdev->device,
1476 "Write Config Block failed: 0x%x\n",
1477 comp_pkt.completion_status);
1478 return -EIO;
1479 }
1480
1481 return 0;
1482 }
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493 static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
1494 void (*block_invalidate)(void *context,
1495 u64 block_mask))
1496 {
1497 struct hv_pcibus_device *hbus =
1498 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1499 sysdata);
1500 struct hv_pci_dev *hpdev;
1501
1502 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1503 if (!hpdev)
1504 return -ENODEV;
1505
1506 hpdev->block_invalidate = block_invalidate;
1507 hpdev->invalidate_context = context;
1508
1509 put_pcichild(hpdev);
1510 return 0;
1511
1512 }
1513
1514
1515 static void hv_int_desc_free(struct hv_pci_dev *hpdev,
1516 struct tran_int_desc *int_desc)
1517 {
1518 struct pci_delete_interrupt *int_pkt;
1519 struct {
1520 struct pci_packet pkt;
1521 u8 buffer[sizeof(struct pci_delete_interrupt)];
1522 } ctxt;
1523
1524 if (!int_desc->vector_count) {
1525 kfree(int_desc);
1526 return;
1527 }
1528 memset(&ctxt, 0, sizeof(ctxt));
1529 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
1530 int_pkt->message_type.type =
1531 PCI_DELETE_INTERRUPT_MESSAGE;
1532 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1533 int_pkt->int_desc = *int_desc;
1534 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
1535 0, VM_PKT_DATA_INBAND, 0);
1536 kfree(int_desc);
1537 }
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550 static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
1551 unsigned int irq)
1552 {
1553 struct hv_pcibus_device *hbus;
1554 struct hv_pci_dev *hpdev;
1555 struct pci_dev *pdev;
1556 struct tran_int_desc *int_desc;
1557 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
1558 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
1559
1560 pdev = msi_desc_to_pci_dev(msi);
1561 hbus = info->data;
1562 int_desc = irq_data_get_irq_chip_data(irq_data);
1563 if (!int_desc)
1564 return;
1565
1566 irq_data->chip_data = NULL;
1567 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1568 if (!hpdev) {
1569 kfree(int_desc);
1570 return;
1571 }
1572
1573 hv_int_desc_free(hpdev, int_desc);
1574 put_pcichild(hpdev);
1575 }
1576
1577 static void hv_irq_mask(struct irq_data *data)
1578 {
1579 pci_msi_mask_irq(data);
1580 if (data->parent_data->chip->irq_mask)
1581 irq_chip_mask_parent(data);
1582 }
1583
1584 static void hv_irq_unmask(struct irq_data *data)
1585 {
1586 hv_arch_irq_unmask(data);
1587
1588 if (data->parent_data->chip->irq_unmask)
1589 irq_chip_unmask_parent(data);
1590 pci_msi_unmask_irq(data);
1591 }
1592
1593 struct compose_comp_ctxt {
1594 struct hv_pci_compl comp_pkt;
1595 struct tran_int_desc int_desc;
1596 };
1597
1598 static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1599 int resp_packet_size)
1600 {
1601 struct compose_comp_ctxt *comp_pkt = context;
1602 struct pci_create_int_response *int_resp =
1603 (struct pci_create_int_response *)resp;
1604
1605 if (resp_packet_size < sizeof(*int_resp)) {
1606 comp_pkt->comp_pkt.completion_status = -1;
1607 goto out;
1608 }
1609 comp_pkt->comp_pkt.completion_status = resp->status;
1610 comp_pkt->int_desc = int_resp->int_desc;
1611 out:
1612 complete(&comp_pkt->comp_pkt.host_event);
1613 }
1614
1615 static u32 hv_compose_msi_req_v1(
1616 struct pci_create_interrupt *int_pkt, const struct cpumask *affinity,
1617 u32 slot, u8 vector, u8 vector_count)
1618 {
1619 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1620 int_pkt->wslot.slot = slot;
1621 int_pkt->int_desc.vector = vector;
1622 int_pkt->int_desc.vector_count = vector_count;
1623 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
1624
1625
1626
1627
1628
1629 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1630
1631 return sizeof(*int_pkt);
1632 }
1633
1634
1635
1636
1637
1638 static int hv_compose_msi_req_get_cpu(const struct cpumask *affinity)
1639 {
1640 return cpumask_first_and(affinity, cpu_online_mask);
1641 }
1642
1643 static u32 hv_compose_msi_req_v2(
1644 struct pci_create_interrupt2 *int_pkt, const struct cpumask *affinity,
1645 u32 slot, u8 vector, u8 vector_count)
1646 {
1647 int cpu;
1648
1649 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1650 int_pkt->wslot.slot = slot;
1651 int_pkt->int_desc.vector = vector;
1652 int_pkt->int_desc.vector_count = vector_count;
1653 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
1654 cpu = hv_compose_msi_req_get_cpu(affinity);
1655 int_pkt->int_desc.processor_array[0] =
1656 hv_cpu_number_to_vp_number(cpu);
1657 int_pkt->int_desc.processor_count = 1;
1658
1659 return sizeof(*int_pkt);
1660 }
1661
1662 static u32 hv_compose_msi_req_v3(
1663 struct pci_create_interrupt3 *int_pkt, const struct cpumask *affinity,
1664 u32 slot, u32 vector, u8 vector_count)
1665 {
1666 int cpu;
1667
1668 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3;
1669 int_pkt->wslot.slot = slot;
1670 int_pkt->int_desc.vector = vector;
1671 int_pkt->int_desc.reserved = 0;
1672 int_pkt->int_desc.vector_count = vector_count;
1673 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
1674 cpu = hv_compose_msi_req_get_cpu(affinity);
1675 int_pkt->int_desc.processor_array[0] =
1676 hv_cpu_number_to_vp_number(cpu);
1677 int_pkt->int_desc.processor_count = 1;
1678
1679 return sizeof(*int_pkt);
1680 }
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693 static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1694 {
1695 struct hv_pcibus_device *hbus;
1696 struct vmbus_channel *channel;
1697 struct hv_pci_dev *hpdev;
1698 struct pci_bus *pbus;
1699 struct pci_dev *pdev;
1700 const struct cpumask *dest;
1701 struct compose_comp_ctxt comp;
1702 struct tran_int_desc *int_desc;
1703 struct msi_desc *msi_desc;
1704 u8 vector, vector_count;
1705 struct {
1706 struct pci_packet pci_pkt;
1707 union {
1708 struct pci_create_interrupt v1;
1709 struct pci_create_interrupt2 v2;
1710 struct pci_create_interrupt3 v3;
1711 } int_pkts;
1712 } __packed ctxt;
1713 u64 trans_id;
1714 u32 size;
1715 int ret;
1716
1717
1718 if (data->chip_data) {
1719 int_desc = data->chip_data;
1720 msg->address_hi = int_desc->address >> 32;
1721 msg->address_lo = int_desc->address & 0xffffffff;
1722 msg->data = int_desc->data;
1723 return;
1724 }
1725
1726 msi_desc = irq_data_get_msi_desc(data);
1727 pdev = msi_desc_to_pci_dev(msi_desc);
1728 dest = irq_data_get_effective_affinity_mask(data);
1729 pbus = pdev->bus;
1730 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1731 channel = hbus->hdev->channel;
1732 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1733 if (!hpdev)
1734 goto return_null_message;
1735
1736 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
1737 if (!int_desc)
1738 goto drop_reference;
1739
1740 if (!msi_desc->pci.msi_attrib.is_msix && msi_desc->nvec_used > 1) {
1741
1742
1743
1744
1745 if (msi_desc->irq != data->irq) {
1746 data->chip_data = int_desc;
1747 int_desc->address = msi_desc->msg.address_lo |
1748 (u64)msi_desc->msg.address_hi << 32;
1749 int_desc->data = msi_desc->msg.data +
1750 (data->irq - msi_desc->irq);
1751 msg->address_hi = msi_desc->msg.address_hi;
1752 msg->address_lo = msi_desc->msg.address_lo;
1753 msg->data = int_desc->data;
1754 put_pcichild(hpdev);
1755 return;
1756 }
1757
1758
1759
1760
1761
1762
1763 vector = 32;
1764 vector_count = msi_desc->nvec_used;
1765 } else {
1766 vector = hv_msi_get_int_vector(data);
1767 vector_count = 1;
1768 }
1769
1770 memset(&ctxt, 0, sizeof(ctxt));
1771 init_completion(&comp.comp_pkt.host_event);
1772 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1773 ctxt.pci_pkt.compl_ctxt = ∁
1774
1775 switch (hbus->protocol_version) {
1776 case PCI_PROTOCOL_VERSION_1_1:
1777 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
1778 dest,
1779 hpdev->desc.win_slot.slot,
1780 vector,
1781 vector_count);
1782 break;
1783
1784 case PCI_PROTOCOL_VERSION_1_2:
1785 case PCI_PROTOCOL_VERSION_1_3:
1786 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
1787 dest,
1788 hpdev->desc.win_slot.slot,
1789 vector,
1790 vector_count);
1791 break;
1792
1793 case PCI_PROTOCOL_VERSION_1_4:
1794 size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3,
1795 dest,
1796 hpdev->desc.win_slot.slot,
1797 vector,
1798 vector_count);
1799 break;
1800
1801 default:
1802
1803
1804
1805
1806 dev_err(&hbus->hdev->device,
1807 "Unexpected vPCI protocol, update driver.");
1808 goto free_int_desc;
1809 }
1810
1811 ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1812 size, (unsigned long)&ctxt.pci_pkt,
1813 &trans_id, VM_PKT_DATA_INBAND,
1814 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1815 if (ret) {
1816 dev_err(&hbus->hdev->device,
1817 "Sending request for interrupt failed: 0x%x",
1818 comp.comp_pkt.completion_status);
1819 goto free_int_desc;
1820 }
1821
1822
1823
1824
1825
1826 tasklet_disable_in_atomic(&channel->callback_event);
1827
1828
1829
1830
1831
1832 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
1833 unsigned long flags;
1834
1835
1836 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
1837 dev_err_once(&hbus->hdev->device,
1838 "the device has gone\n");
1839 goto enable_tasklet;
1840 }
1841
1842
1843
1844
1845
1846
1847
1848
1849 spin_lock_irqsave(&channel->sched_lock, flags);
1850 if (unlikely(channel->onchannel_callback == NULL)) {
1851 spin_unlock_irqrestore(&channel->sched_lock, flags);
1852 goto enable_tasklet;
1853 }
1854 hv_pci_onchannelcallback(hbus);
1855 spin_unlock_irqrestore(&channel->sched_lock, flags);
1856
1857 if (hpdev->state == hv_pcichild_ejecting) {
1858 dev_err_once(&hbus->hdev->device,
1859 "the device is being ejected\n");
1860 goto enable_tasklet;
1861 }
1862
1863 udelay(100);
1864 }
1865
1866 tasklet_enable(&channel->callback_event);
1867
1868 if (comp.comp_pkt.completion_status < 0) {
1869 dev_err(&hbus->hdev->device,
1870 "Request for interrupt failed: 0x%x",
1871 comp.comp_pkt.completion_status);
1872 goto free_int_desc;
1873 }
1874
1875
1876
1877
1878
1879
1880 *int_desc = comp.int_desc;
1881 data->chip_data = int_desc;
1882
1883
1884 msg->address_hi = comp.int_desc.address >> 32;
1885 msg->address_lo = comp.int_desc.address & 0xffffffff;
1886 msg->data = comp.int_desc.data;
1887
1888 put_pcichild(hpdev);
1889 return;
1890
1891 enable_tasklet:
1892 tasklet_enable(&channel->callback_event);
1893
1894
1895
1896
1897
1898
1899
1900
1901 vmbus_request_addr_match(channel, trans_id, (unsigned long)&ctxt.pci_pkt);
1902 free_int_desc:
1903 kfree(int_desc);
1904 drop_reference:
1905 put_pcichild(hpdev);
1906 return_null_message:
1907 msg->address_hi = 0;
1908 msg->address_lo = 0;
1909 msg->data = 0;
1910 }
1911
1912
1913 static struct irq_chip hv_msi_irq_chip = {
1914 .name = "Hyper-V PCIe MSI",
1915 .irq_compose_msi_msg = hv_compose_msi_msg,
1916 .irq_set_affinity = irq_chip_set_affinity_parent,
1917 #ifdef CONFIG_X86
1918 .irq_ack = irq_chip_ack_parent,
1919 #elif defined(CONFIG_ARM64)
1920 .irq_eoi = irq_chip_eoi_parent,
1921 #endif
1922 .irq_mask = hv_irq_mask,
1923 .irq_unmask = hv_irq_unmask,
1924 };
1925
1926 static struct msi_domain_ops hv_msi_ops = {
1927 .msi_prepare = hv_msi_prepare,
1928 .msi_free = hv_msi_free,
1929 };
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944 static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1945 {
1946 hbus->msi_info.chip = &hv_msi_irq_chip;
1947 hbus->msi_info.ops = &hv_msi_ops;
1948 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1949 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1950 MSI_FLAG_PCI_MSIX);
1951 hbus->msi_info.handler = FLOW_HANDLER;
1952 hbus->msi_info.handler_name = FLOW_NAME;
1953 hbus->msi_info.data = hbus;
1954 hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode,
1955 &hbus->msi_info,
1956 hv_pci_get_root_domain());
1957 if (!hbus->irq_domain) {
1958 dev_err(&hbus->hdev->device,
1959 "Failed to build an MSI IRQ domain\n");
1960 return -ENODEV;
1961 }
1962
1963 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain);
1964
1965 return 0;
1966 }
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982 static u64 get_bar_size(u64 bar_val)
1983 {
1984 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1985 PAGE_SIZE);
1986 }
1987
1988
1989
1990
1991
1992 static void survey_child_resources(struct hv_pcibus_device *hbus)
1993 {
1994 struct hv_pci_dev *hpdev;
1995 resource_size_t bar_size = 0;
1996 unsigned long flags;
1997 struct completion *event;
1998 u64 bar_val;
1999 int i;
2000
2001
2002 event = xchg(&hbus->survey_event, NULL);
2003 if (!event)
2004 return;
2005
2006
2007 if (hbus->low_mmio_space || hbus->high_mmio_space) {
2008 complete(event);
2009 return;
2010 }
2011
2012 spin_lock_irqsave(&hbus->device_list_lock, flags);
2013
2014
2015
2016
2017
2018
2019 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2020 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
2021 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
2022 dev_err(&hbus->hdev->device,
2023 "There's an I/O BAR in this list!\n");
2024
2025 if (hpdev->probed_bar[i] != 0) {
2026
2027
2028
2029
2030
2031 bar_val = hpdev->probed_bar[i];
2032 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
2033 bar_val |=
2034 ((u64)hpdev->probed_bar[++i] << 32);
2035 else
2036 bar_val |= 0xffffffff00000000ULL;
2037
2038 bar_size = get_bar_size(bar_val);
2039
2040 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
2041 hbus->high_mmio_space += bar_size;
2042 else
2043 hbus->low_mmio_space += bar_size;
2044 }
2045 }
2046 }
2047
2048 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2049 complete(event);
2050 }
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064 static void prepopulate_bars(struct hv_pcibus_device *hbus)
2065 {
2066 resource_size_t high_size = 0;
2067 resource_size_t low_size = 0;
2068 resource_size_t high_base = 0;
2069 resource_size_t low_base = 0;
2070 resource_size_t bar_size;
2071 struct hv_pci_dev *hpdev;
2072 unsigned long flags;
2073 u64 bar_val;
2074 u32 command;
2075 bool high;
2076 int i;
2077
2078 if (hbus->low_mmio_space) {
2079 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2080 low_base = hbus->low_mmio_res->start;
2081 }
2082
2083 if (hbus->high_mmio_space) {
2084 high_size = 1ULL <<
2085 (63 - __builtin_clzll(hbus->high_mmio_space));
2086 high_base = hbus->high_mmio_res->start;
2087 }
2088
2089 spin_lock_irqsave(&hbus->device_list_lock, flags);
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2103 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
2104 command &= ~PCI_COMMAND_MEMORY;
2105 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
2106 }
2107
2108
2109 do {
2110 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2111 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
2112 bar_val = hpdev->probed_bar[i];
2113 if (bar_val == 0)
2114 continue;
2115 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
2116 if (high) {
2117 bar_val |=
2118 ((u64)hpdev->probed_bar[i + 1]
2119 << 32);
2120 } else {
2121 bar_val |= 0xffffffffULL << 32;
2122 }
2123 bar_size = get_bar_size(bar_val);
2124 if (high) {
2125 if (high_size != bar_size) {
2126 i++;
2127 continue;
2128 }
2129 _hv_pcifront_write_config(hpdev,
2130 PCI_BASE_ADDRESS_0 + (4 * i),
2131 4,
2132 (u32)(high_base & 0xffffff00));
2133 i++;
2134 _hv_pcifront_write_config(hpdev,
2135 PCI_BASE_ADDRESS_0 + (4 * i),
2136 4, (u32)(high_base >> 32));
2137 high_base += bar_size;
2138 } else {
2139 if (low_size != bar_size)
2140 continue;
2141 _hv_pcifront_write_config(hpdev,
2142 PCI_BASE_ADDRESS_0 + (4 * i),
2143 4,
2144 (u32)(low_base & 0xffffff00));
2145 low_base += bar_size;
2146 }
2147 }
2148 if (high_size <= 1 && low_size <= 1) {
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160 break;
2161 }
2162 }
2163
2164 high_size >>= 1;
2165 low_size >>= 1;
2166 } while (high_size || low_size);
2167
2168 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2169 }
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180 static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
2181 {
2182 struct hv_pci_dev *hpdev;
2183 char name[SLOT_NAME_SIZE];
2184 int slot_nr;
2185
2186 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2187 if (hpdev->pci_slot)
2188 continue;
2189
2190 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
2191 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
2192 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr,
2193 name, NULL);
2194 if (IS_ERR(hpdev->pci_slot)) {
2195 pr_warn("pci_create slot %s failed\n", name);
2196 hpdev->pci_slot = NULL;
2197 }
2198 }
2199 }
2200
2201
2202
2203
2204 static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
2205 {
2206 struct hv_pci_dev *hpdev;
2207
2208 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2209 if (!hpdev->pci_slot)
2210 continue;
2211 pci_destroy_slot(hpdev->pci_slot);
2212 hpdev->pci_slot = NULL;
2213 }
2214 }
2215
2216
2217
2218
2219 static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
2220 {
2221 struct pci_dev *dev;
2222 struct pci_bus *bus = hbus->bridge->bus;
2223 struct hv_pci_dev *hv_dev;
2224
2225 list_for_each_entry(dev, &bus->devices, bus_list) {
2226 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
2227 if (!hv_dev)
2228 continue;
2229
2230 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY &&
2231 hv_dev->desc.virtual_numa_node < num_possible_nodes())
2232
2233
2234
2235
2236
2237
2238 set_dev_node(&dev->dev,
2239 numa_map_to_online_node(
2240 hv_dev->desc.virtual_numa_node));
2241
2242 put_pcichild(hv_dev);
2243 }
2244 }
2245
2246
2247
2248
2249
2250
2251
2252 static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
2253 {
2254 int error;
2255 struct pci_host_bridge *bridge = hbus->bridge;
2256
2257 bridge->dev.parent = &hbus->hdev->device;
2258 bridge->sysdata = &hbus->sysdata;
2259 bridge->ops = &hv_pcifront_ops;
2260
2261 error = pci_scan_root_bus_bridge(bridge);
2262 if (error)
2263 return error;
2264
2265 pci_lock_rescan_remove();
2266 hv_pci_assign_numa_node(hbus);
2267 pci_bus_assign_resources(bridge->bus);
2268 hv_pci_assign_slots(hbus);
2269 pci_bus_add_devices(bridge->bus);
2270 pci_unlock_rescan_remove();
2271 hbus->state = hv_pcibus_installed;
2272 return 0;
2273 }
2274
2275 struct q_res_req_compl {
2276 struct completion host_event;
2277 struct hv_pci_dev *hpdev;
2278 };
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289 static void q_resource_requirements(void *context, struct pci_response *resp,
2290 int resp_packet_size)
2291 {
2292 struct q_res_req_compl *completion = context;
2293 struct pci_q_res_req_response *q_res_req =
2294 (struct pci_q_res_req_response *)resp;
2295 s32 status;
2296 int i;
2297
2298 status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
2299 if (status < 0) {
2300 dev_err(&completion->hpdev->hbus->hdev->device,
2301 "query resource requirements failed: %x\n",
2302 status);
2303 } else {
2304 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
2305 completion->hpdev->probed_bar[i] =
2306 q_res_req->probed_bar[i];
2307 }
2308 }
2309
2310 complete(&completion->host_event);
2311 }
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324 static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
2325 struct hv_pcidev_description *desc)
2326 {
2327 struct hv_pci_dev *hpdev;
2328 struct pci_child_message *res_req;
2329 struct q_res_req_compl comp_pkt;
2330 struct {
2331 struct pci_packet init_packet;
2332 u8 buffer[sizeof(struct pci_child_message)];
2333 } pkt;
2334 unsigned long flags;
2335 int ret;
2336
2337 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
2338 if (!hpdev)
2339 return NULL;
2340
2341 hpdev->hbus = hbus;
2342
2343 memset(&pkt, 0, sizeof(pkt));
2344 init_completion(&comp_pkt.host_event);
2345 comp_pkt.hpdev = hpdev;
2346 pkt.init_packet.compl_ctxt = &comp_pkt;
2347 pkt.init_packet.completion_func = q_resource_requirements;
2348 res_req = (struct pci_child_message *)&pkt.init_packet.message;
2349 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
2350 res_req->wslot.slot = desc->win_slot.slot;
2351
2352 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
2353 sizeof(struct pci_child_message),
2354 (unsigned long)&pkt.init_packet,
2355 VM_PKT_DATA_INBAND,
2356 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2357 if (ret)
2358 goto error;
2359
2360 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
2361 goto error;
2362
2363 hpdev->desc = *desc;
2364 refcount_set(&hpdev->refs, 1);
2365 get_pcichild(hpdev);
2366 spin_lock_irqsave(&hbus->device_list_lock, flags);
2367
2368 list_add_tail(&hpdev->list_entry, &hbus->children);
2369 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2370 return hpdev;
2371
2372 error:
2373 kfree(hpdev);
2374 return NULL;
2375 }
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
2391 u32 wslot)
2392 {
2393 unsigned long flags;
2394 struct hv_pci_dev *iter, *hpdev = NULL;
2395
2396 spin_lock_irqsave(&hbus->device_list_lock, flags);
2397 list_for_each_entry(iter, &hbus->children, list_entry) {
2398 if (iter->desc.win_slot.slot == wslot) {
2399 hpdev = iter;
2400 get_pcichild(hpdev);
2401 break;
2402 }
2403 }
2404 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2405
2406 return hpdev;
2407 }
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432 static void pci_devices_present_work(struct work_struct *work)
2433 {
2434 u32 child_no;
2435 bool found;
2436 struct hv_pcidev_description *new_desc;
2437 struct hv_pci_dev *hpdev;
2438 struct hv_pcibus_device *hbus;
2439 struct list_head removed;
2440 struct hv_dr_work *dr_wrk;
2441 struct hv_dr_state *dr = NULL;
2442 unsigned long flags;
2443
2444 dr_wrk = container_of(work, struct hv_dr_work, wrk);
2445 hbus = dr_wrk->bus;
2446 kfree(dr_wrk);
2447
2448 INIT_LIST_HEAD(&removed);
2449
2450
2451 spin_lock_irqsave(&hbus->device_list_lock, flags);
2452 while (!list_empty(&hbus->dr_list)) {
2453 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
2454 list_entry);
2455 list_del(&dr->list_entry);
2456
2457
2458 if (!list_empty(&hbus->dr_list)) {
2459 kfree(dr);
2460 continue;
2461 }
2462 }
2463 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2464
2465 if (!dr)
2466 return;
2467
2468
2469 spin_lock_irqsave(&hbus->device_list_lock, flags);
2470 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2471 hpdev->reported_missing = true;
2472 }
2473 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2474
2475
2476 for (child_no = 0; child_no < dr->device_count; child_no++) {
2477 found = false;
2478 new_desc = &dr->func[child_no];
2479
2480 spin_lock_irqsave(&hbus->device_list_lock, flags);
2481 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2482 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
2483 (hpdev->desc.v_id == new_desc->v_id) &&
2484 (hpdev->desc.d_id == new_desc->d_id) &&
2485 (hpdev->desc.ser == new_desc->ser)) {
2486 hpdev->reported_missing = false;
2487 found = true;
2488 }
2489 }
2490 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2491
2492 if (!found) {
2493 hpdev = new_pcichild_device(hbus, new_desc);
2494 if (!hpdev)
2495 dev_err(&hbus->hdev->device,
2496 "couldn't record a child device.\n");
2497 }
2498 }
2499
2500
2501 spin_lock_irqsave(&hbus->device_list_lock, flags);
2502 do {
2503 found = false;
2504 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2505 if (hpdev->reported_missing) {
2506 found = true;
2507 put_pcichild(hpdev);
2508 list_move_tail(&hpdev->list_entry, &removed);
2509 break;
2510 }
2511 }
2512 } while (found);
2513 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2514
2515
2516 while (!list_empty(&removed)) {
2517 hpdev = list_first_entry(&removed, struct hv_pci_dev,
2518 list_entry);
2519 list_del(&hpdev->list_entry);
2520
2521 if (hpdev->pci_slot)
2522 pci_destroy_slot(hpdev->pci_slot);
2523
2524 put_pcichild(hpdev);
2525 }
2526
2527 switch (hbus->state) {
2528 case hv_pcibus_installed:
2529
2530
2531
2532
2533 pci_lock_rescan_remove();
2534 pci_scan_child_bus(hbus->bridge->bus);
2535 hv_pci_assign_numa_node(hbus);
2536 hv_pci_assign_slots(hbus);
2537 pci_unlock_rescan_remove();
2538 break;
2539
2540 case hv_pcibus_init:
2541 case hv_pcibus_probed:
2542 survey_child_resources(hbus);
2543 break;
2544
2545 default:
2546 break;
2547 }
2548
2549 kfree(dr);
2550 }
2551
2552
2553
2554
2555
2556
2557
2558
2559 static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
2560 struct hv_dr_state *dr)
2561 {
2562 struct hv_dr_work *dr_wrk;
2563 unsigned long flags;
2564 bool pending_dr;
2565
2566 if (hbus->state == hv_pcibus_removing) {
2567 dev_info(&hbus->hdev->device,
2568 "PCI VMBus BUS_RELATIONS: ignored\n");
2569 return -ENOENT;
2570 }
2571
2572 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
2573 if (!dr_wrk)
2574 return -ENOMEM;
2575
2576 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
2577 dr_wrk->bus = hbus;
2578
2579 spin_lock_irqsave(&hbus->device_list_lock, flags);
2580
2581
2582
2583
2584
2585 pending_dr = !list_empty(&hbus->dr_list);
2586 list_add_tail(&dr->list_entry, &hbus->dr_list);
2587 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2588
2589 if (pending_dr)
2590 kfree(dr_wrk);
2591 else
2592 queue_work(hbus->wq, &dr_wrk->wrk);
2593
2594 return 0;
2595 }
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606 static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
2607 struct pci_bus_relations *relations)
2608 {
2609 struct hv_dr_state *dr;
2610 int i;
2611
2612 dr = kzalloc(struct_size(dr, func, relations->device_count),
2613 GFP_NOWAIT);
2614 if (!dr)
2615 return;
2616
2617 dr->device_count = relations->device_count;
2618 for (i = 0; i < dr->device_count; i++) {
2619 dr->func[i].v_id = relations->func[i].v_id;
2620 dr->func[i].d_id = relations->func[i].d_id;
2621 dr->func[i].rev = relations->func[i].rev;
2622 dr->func[i].prog_intf = relations->func[i].prog_intf;
2623 dr->func[i].subclass = relations->func[i].subclass;
2624 dr->func[i].base_class = relations->func[i].base_class;
2625 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2626 dr->func[i].win_slot = relations->func[i].win_slot;
2627 dr->func[i].ser = relations->func[i].ser;
2628 }
2629
2630 if (hv_pci_start_relations_work(hbus, dr))
2631 kfree(dr);
2632 }
2633
2634
2635
2636
2637
2638
2639
2640
2641 static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
2642 struct pci_bus_relations2 *relations)
2643 {
2644 struct hv_dr_state *dr;
2645 int i;
2646
2647 dr = kzalloc(struct_size(dr, func, relations->device_count),
2648 GFP_NOWAIT);
2649 if (!dr)
2650 return;
2651
2652 dr->device_count = relations->device_count;
2653 for (i = 0; i < dr->device_count; i++) {
2654 dr->func[i].v_id = relations->func[i].v_id;
2655 dr->func[i].d_id = relations->func[i].d_id;
2656 dr->func[i].rev = relations->func[i].rev;
2657 dr->func[i].prog_intf = relations->func[i].prog_intf;
2658 dr->func[i].subclass = relations->func[i].subclass;
2659 dr->func[i].base_class = relations->func[i].base_class;
2660 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2661 dr->func[i].win_slot = relations->func[i].win_slot;
2662 dr->func[i].ser = relations->func[i].ser;
2663 dr->func[i].flags = relations->func[i].flags;
2664 dr->func[i].virtual_numa_node =
2665 relations->func[i].virtual_numa_node;
2666 }
2667
2668 if (hv_pci_start_relations_work(hbus, dr))
2669 kfree(dr);
2670 }
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681 static void hv_eject_device_work(struct work_struct *work)
2682 {
2683 struct pci_eject_response *ejct_pkt;
2684 struct hv_pcibus_device *hbus;
2685 struct hv_pci_dev *hpdev;
2686 struct pci_dev *pdev;
2687 unsigned long flags;
2688 int wslot;
2689 struct {
2690 struct pci_packet pkt;
2691 u8 buffer[sizeof(struct pci_eject_response)];
2692 } ctxt;
2693
2694 hpdev = container_of(work, struct hv_pci_dev, wrk);
2695 hbus = hpdev->hbus;
2696
2697 WARN_ON(hpdev->state != hv_pcichild_ejecting);
2698
2699
2700
2701
2702
2703
2704
2705 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
2706 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot);
2707 if (pdev) {
2708 pci_lock_rescan_remove();
2709 pci_stop_and_remove_bus_device(pdev);
2710 pci_dev_put(pdev);
2711 pci_unlock_rescan_remove();
2712 }
2713
2714 spin_lock_irqsave(&hbus->device_list_lock, flags);
2715 list_del(&hpdev->list_entry);
2716 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2717
2718 if (hpdev->pci_slot)
2719 pci_destroy_slot(hpdev->pci_slot);
2720
2721 memset(&ctxt, 0, sizeof(ctxt));
2722 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
2723 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
2724 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
2725 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
2726 sizeof(*ejct_pkt), 0,
2727 VM_PKT_DATA_INBAND, 0);
2728
2729
2730 put_pcichild(hpdev);
2731
2732 put_pcichild(hpdev);
2733 put_pcichild(hpdev);
2734
2735 }
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745 static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
2746 {
2747 struct hv_pcibus_device *hbus = hpdev->hbus;
2748 struct hv_device *hdev = hbus->hdev;
2749
2750 if (hbus->state == hv_pcibus_removing) {
2751 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
2752 return;
2753 }
2754
2755 hpdev->state = hv_pcichild_ejecting;
2756 get_pcichild(hpdev);
2757 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
2758 queue_work(hbus->wq, &hpdev->wrk);
2759 }
2760
2761
2762
2763
2764
2765
2766
2767
2768 static void hv_pci_onchannelcallback(void *context)
2769 {
2770 const int packet_size = 0x100;
2771 int ret;
2772 struct hv_pcibus_device *hbus = context;
2773 struct vmbus_channel *chan = hbus->hdev->channel;
2774 u32 bytes_recvd;
2775 u64 req_id, req_addr;
2776 struct vmpacket_descriptor *desc;
2777 unsigned char *buffer;
2778 int bufferlen = packet_size;
2779 struct pci_packet *comp_packet;
2780 struct pci_response *response;
2781 struct pci_incoming_message *new_message;
2782 struct pci_bus_relations *bus_rel;
2783 struct pci_bus_relations2 *bus_rel2;
2784 struct pci_dev_inval_block *inval;
2785 struct pci_dev_incoming *dev_message;
2786 struct hv_pci_dev *hpdev;
2787 unsigned long flags;
2788
2789 buffer = kmalloc(bufferlen, GFP_ATOMIC);
2790 if (!buffer)
2791 return;
2792
2793 while (1) {
2794 ret = vmbus_recvpacket_raw(chan, buffer, bufferlen,
2795 &bytes_recvd, &req_id);
2796
2797 if (ret == -ENOBUFS) {
2798 kfree(buffer);
2799
2800 bufferlen = bytes_recvd;
2801 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
2802 if (!buffer)
2803 return;
2804 continue;
2805 }
2806
2807
2808 if (ret || !bytes_recvd)
2809 break;
2810
2811
2812
2813
2814
2815 if (bytes_recvd <= sizeof(struct pci_response))
2816 continue;
2817 desc = (struct vmpacket_descriptor *)buffer;
2818
2819 switch (desc->type) {
2820 case VM_PKT_COMP:
2821
2822 lock_requestor(chan, flags);
2823 req_addr = __vmbus_request_addr_match(chan, req_id,
2824 VMBUS_RQST_ADDR_ANY);
2825 if (req_addr == VMBUS_RQST_ERROR) {
2826 unlock_requestor(chan, flags);
2827 dev_err(&hbus->hdev->device,
2828 "Invalid transaction ID %llx\n",
2829 req_id);
2830 break;
2831 }
2832 comp_packet = (struct pci_packet *)req_addr;
2833 response = (struct pci_response *)buffer;
2834
2835
2836
2837
2838
2839
2840
2841 comp_packet->completion_func(comp_packet->compl_ctxt,
2842 response,
2843 bytes_recvd);
2844 unlock_requestor(chan, flags);
2845 break;
2846
2847 case VM_PKT_DATA_INBAND:
2848
2849 new_message = (struct pci_incoming_message *)buffer;
2850 switch (new_message->message_type.type) {
2851 case PCI_BUS_RELATIONS:
2852
2853 bus_rel = (struct pci_bus_relations *)buffer;
2854 if (bytes_recvd < sizeof(*bus_rel) ||
2855 bytes_recvd <
2856 struct_size(bus_rel, func,
2857 bus_rel->device_count)) {
2858 dev_err(&hbus->hdev->device,
2859 "bus relations too small\n");
2860 break;
2861 }
2862
2863 hv_pci_devices_present(hbus, bus_rel);
2864 break;
2865
2866 case PCI_BUS_RELATIONS2:
2867
2868 bus_rel2 = (struct pci_bus_relations2 *)buffer;
2869 if (bytes_recvd < sizeof(*bus_rel2) ||
2870 bytes_recvd <
2871 struct_size(bus_rel2, func,
2872 bus_rel2->device_count)) {
2873 dev_err(&hbus->hdev->device,
2874 "bus relations v2 too small\n");
2875 break;
2876 }
2877
2878 hv_pci_devices_present2(hbus, bus_rel2);
2879 break;
2880
2881 case PCI_EJECT:
2882
2883 dev_message = (struct pci_dev_incoming *)buffer;
2884 if (bytes_recvd < sizeof(*dev_message)) {
2885 dev_err(&hbus->hdev->device,
2886 "eject message too small\n");
2887 break;
2888 }
2889 hpdev = get_pcichild_wslot(hbus,
2890 dev_message->wslot.slot);
2891 if (hpdev) {
2892 hv_pci_eject_device(hpdev);
2893 put_pcichild(hpdev);
2894 }
2895 break;
2896
2897 case PCI_INVALIDATE_BLOCK:
2898
2899 inval = (struct pci_dev_inval_block *)buffer;
2900 if (bytes_recvd < sizeof(*inval)) {
2901 dev_err(&hbus->hdev->device,
2902 "invalidate message too small\n");
2903 break;
2904 }
2905 hpdev = get_pcichild_wslot(hbus,
2906 inval->wslot.slot);
2907 if (hpdev) {
2908 if (hpdev->block_invalidate) {
2909 hpdev->block_invalidate(
2910 hpdev->invalidate_context,
2911 inval->block_mask);
2912 }
2913 put_pcichild(hpdev);
2914 }
2915 break;
2916
2917 default:
2918 dev_warn(&hbus->hdev->device,
2919 "Unimplemented protocol message %x\n",
2920 new_message->message_type.type);
2921 break;
2922 }
2923 break;
2924
2925 default:
2926 dev_err(&hbus->hdev->device,
2927 "unhandled packet type %d, tid %llx len %d\n",
2928 desc->type, req_id, bytes_recvd);
2929 break;
2930 }
2931 }
2932
2933 kfree(buffer);
2934 }
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955 static int hv_pci_protocol_negotiation(struct hv_device *hdev,
2956 enum pci_protocol_version_t version[],
2957 int num_version)
2958 {
2959 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2960 struct pci_version_request *version_req;
2961 struct hv_pci_compl comp_pkt;
2962 struct pci_packet *pkt;
2963 int ret;
2964 int i;
2965
2966
2967
2968
2969
2970
2971
2972 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2973 if (!pkt)
2974 return -ENOMEM;
2975
2976 init_completion(&comp_pkt.host_event);
2977 pkt->completion_func = hv_pci_generic_compl;
2978 pkt->compl_ctxt = &comp_pkt;
2979 version_req = (struct pci_version_request *)&pkt->message;
2980 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
2981
2982 for (i = 0; i < num_version; i++) {
2983 version_req->protocol_version = version[i];
2984 ret = vmbus_sendpacket(hdev->channel, version_req,
2985 sizeof(struct pci_version_request),
2986 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2987 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2988 if (!ret)
2989 ret = wait_for_response(hdev, &comp_pkt.host_event);
2990
2991 if (ret) {
2992 dev_err(&hdev->device,
2993 "PCI Pass-through VSP failed to request version: %d",
2994 ret);
2995 goto exit;
2996 }
2997
2998 if (comp_pkt.completion_status >= 0) {
2999 hbus->protocol_version = version[i];
3000 dev_info(&hdev->device,
3001 "PCI VMBus probing: Using version %#x\n",
3002 hbus->protocol_version);
3003 goto exit;
3004 }
3005
3006 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
3007 dev_err(&hdev->device,
3008 "PCI Pass-through VSP failed version request: %#x",
3009 comp_pkt.completion_status);
3010 ret = -EPROTO;
3011 goto exit;
3012 }
3013
3014 reinit_completion(&comp_pkt.host_event);
3015 }
3016
3017 dev_err(&hdev->device,
3018 "PCI pass-through VSP failed to find supported version");
3019 ret = -EPROTO;
3020
3021 exit:
3022 kfree(pkt);
3023 return ret;
3024 }
3025
3026
3027
3028
3029
3030
3031 static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
3032 {
3033
3034
3035
3036
3037
3038 if (hbus->low_mmio_space && hbus->low_mmio_res) {
3039 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
3040 vmbus_free_mmio(hbus->low_mmio_res->start,
3041 resource_size(hbus->low_mmio_res));
3042 }
3043
3044 if (hbus->high_mmio_space && hbus->high_mmio_res) {
3045 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
3046 vmbus_free_mmio(hbus->high_mmio_res->start,
3047 resource_size(hbus->high_mmio_res));
3048 }
3049 }
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
3077 {
3078 resource_size_t align;
3079 int ret;
3080
3081 if (hbus->low_mmio_space) {
3082 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
3083 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
3084 (u64)(u32)0xffffffff,
3085 hbus->low_mmio_space,
3086 align, false);
3087 if (ret) {
3088 dev_err(&hbus->hdev->device,
3089 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
3090 hbus->low_mmio_space);
3091 return ret;
3092 }
3093
3094
3095 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
3096 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
3097 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res);
3098 }
3099
3100 if (hbus->high_mmio_space) {
3101 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
3102 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
3103 0x100000000, -1,
3104 hbus->high_mmio_space, align,
3105 false);
3106 if (ret) {
3107 dev_err(&hbus->hdev->device,
3108 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
3109 hbus->high_mmio_space);
3110 goto release_low_mmio;
3111 }
3112
3113
3114 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
3115 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
3116 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res);
3117 }
3118
3119 return 0;
3120
3121 release_low_mmio:
3122 if (hbus->low_mmio_res) {
3123 vmbus_free_mmio(hbus->low_mmio_res->start,
3124 resource_size(hbus->low_mmio_res));
3125 }
3126
3127 return ret;
3128 }
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139 static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
3140 {
3141 int ret;
3142
3143
3144
3145
3146
3147 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
3148 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
3149 if (ret)
3150 return ret;
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160 hbus->mem_config->flags |= IORESOURCE_BUSY;
3161
3162 return 0;
3163 }
3164
3165 static void hv_free_config_window(struct hv_pcibus_device *hbus)
3166 {
3167 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
3168 }
3169
3170 static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
3171
3172
3173
3174
3175
3176
3177
3178 static int hv_pci_enter_d0(struct hv_device *hdev)
3179 {
3180 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3181 struct pci_bus_d0_entry *d0_entry;
3182 struct hv_pci_compl comp_pkt;
3183 struct pci_packet *pkt;
3184 int ret;
3185
3186
3187
3188
3189
3190
3191
3192 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
3193 if (!pkt)
3194 return -ENOMEM;
3195
3196 init_completion(&comp_pkt.host_event);
3197 pkt->completion_func = hv_pci_generic_compl;
3198 pkt->compl_ctxt = &comp_pkt;
3199 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
3200 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
3201 d0_entry->mmio_base = hbus->mem_config->start;
3202
3203 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
3204 (unsigned long)pkt, VM_PKT_DATA_INBAND,
3205 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3206 if (!ret)
3207 ret = wait_for_response(hdev, &comp_pkt.host_event);
3208
3209 if (ret)
3210 goto exit;
3211
3212 if (comp_pkt.completion_status < 0) {
3213 dev_err(&hdev->device,
3214 "PCI Pass-through VSP failed D0 Entry with status %x\n",
3215 comp_pkt.completion_status);
3216 ret = -EPROTO;
3217 goto exit;
3218 }
3219
3220 ret = 0;
3221
3222 exit:
3223 kfree(pkt);
3224 return ret;
3225 }
3226
3227
3228
3229
3230
3231
3232
3233
3234 static int hv_pci_query_relations(struct hv_device *hdev)
3235 {
3236 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3237 struct pci_message message;
3238 struct completion comp;
3239 int ret;
3240
3241
3242 init_completion(&comp);
3243 if (cmpxchg(&hbus->survey_event, NULL, &comp))
3244 return -ENOTEMPTY;
3245
3246 memset(&message, 0, sizeof(message));
3247 message.type = PCI_QUERY_BUS_RELATIONS;
3248
3249 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
3250 0, VM_PKT_DATA_INBAND, 0);
3251 if (!ret)
3252 ret = wait_for_response(hdev, &comp);
3253
3254 return ret;
3255 }
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274 static int hv_send_resources_allocated(struct hv_device *hdev)
3275 {
3276 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3277 struct pci_resources_assigned *res_assigned;
3278 struct pci_resources_assigned2 *res_assigned2;
3279 struct hv_pci_compl comp_pkt;
3280 struct hv_pci_dev *hpdev;
3281 struct pci_packet *pkt;
3282 size_t size_res;
3283 int wslot;
3284 int ret;
3285
3286 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
3287 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
3288
3289 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
3290 if (!pkt)
3291 return -ENOMEM;
3292
3293 ret = 0;
3294
3295 for (wslot = 0; wslot < 256; wslot++) {
3296 hpdev = get_pcichild_wslot(hbus, wslot);
3297 if (!hpdev)
3298 continue;
3299
3300 memset(pkt, 0, sizeof(*pkt) + size_res);
3301 init_completion(&comp_pkt.host_event);
3302 pkt->completion_func = hv_pci_generic_compl;
3303 pkt->compl_ctxt = &comp_pkt;
3304
3305 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
3306 res_assigned =
3307 (struct pci_resources_assigned *)&pkt->message;
3308 res_assigned->message_type.type =
3309 PCI_RESOURCES_ASSIGNED;
3310 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
3311 } else {
3312 res_assigned2 =
3313 (struct pci_resources_assigned2 *)&pkt->message;
3314 res_assigned2->message_type.type =
3315 PCI_RESOURCES_ASSIGNED2;
3316 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
3317 }
3318 put_pcichild(hpdev);
3319
3320 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
3321 size_res, (unsigned long)pkt,
3322 VM_PKT_DATA_INBAND,
3323 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3324 if (!ret)
3325 ret = wait_for_response(hdev, &comp_pkt.host_event);
3326 if (ret)
3327 break;
3328
3329 if (comp_pkt.completion_status < 0) {
3330 ret = -EPROTO;
3331 dev_err(&hdev->device,
3332 "resource allocated returned 0x%x",
3333 comp_pkt.completion_status);
3334 break;
3335 }
3336
3337 hbus->wslot_res_allocated = wslot;
3338 }
3339
3340 kfree(pkt);
3341 return ret;
3342 }
3343
3344
3345
3346
3347
3348
3349
3350
3351 static int hv_send_resources_released(struct hv_device *hdev)
3352 {
3353 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3354 struct pci_child_message pkt;
3355 struct hv_pci_dev *hpdev;
3356 int wslot;
3357 int ret;
3358
3359 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
3360 hpdev = get_pcichild_wslot(hbus, wslot);
3361 if (!hpdev)
3362 continue;
3363
3364 memset(&pkt, 0, sizeof(pkt));
3365 pkt.message_type.type = PCI_RESOURCES_RELEASED;
3366 pkt.wslot.slot = hpdev->desc.win_slot.slot;
3367
3368 put_pcichild(hpdev);
3369
3370 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
3371 VM_PKT_DATA_INBAND, 0);
3372 if (ret)
3373 return ret;
3374
3375 hbus->wslot_res_allocated = wslot - 1;
3376 }
3377
3378 hbus->wslot_res_allocated = -1;
3379
3380 return 0;
3381 }
3382
3383 #define HVPCI_DOM_MAP_SIZE (64 * 1024)
3384 static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
3385
3386
3387
3388
3389
3390 #define HVPCI_DOM_INVALID 0
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401 static u16 hv_get_dom_num(u16 dom)
3402 {
3403 unsigned int i;
3404
3405 if (test_and_set_bit(dom, hvpci_dom_map) == 0)
3406 return dom;
3407
3408 for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
3409 if (test_and_set_bit(i, hvpci_dom_map) == 0)
3410 return i;
3411 }
3412
3413 return HVPCI_DOM_INVALID;
3414 }
3415
3416
3417
3418
3419
3420 static void hv_put_dom_num(u16 dom)
3421 {
3422 clear_bit(dom, hvpci_dom_map);
3423 }
3424
3425
3426
3427
3428
3429
3430
3431
3432 static int hv_pci_probe(struct hv_device *hdev,
3433 const struct hv_vmbus_device_id *dev_id)
3434 {
3435 struct pci_host_bridge *bridge;
3436 struct hv_pcibus_device *hbus;
3437 u16 dom_req, dom;
3438 char *name;
3439 bool enter_d0_retry = true;
3440 int ret;
3441
3442
3443
3444
3445
3446 BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
3447
3448 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0);
3449 if (!bridge)
3450 return -ENOMEM;
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470 hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
3471 if (!hbus)
3472 return -ENOMEM;
3473
3474 hbus->bridge = bridge;
3475 hbus->state = hv_pcibus_init;
3476 hbus->wslot_res_allocated = -1;
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
3492 dom = hv_get_dom_num(dom_req);
3493
3494 if (dom == HVPCI_DOM_INVALID) {
3495 dev_err(&hdev->device,
3496 "Unable to use dom# 0x%x or other numbers", dom_req);
3497 ret = -EINVAL;
3498 goto free_bus;
3499 }
3500
3501 if (dom != dom_req)
3502 dev_info(&hdev->device,
3503 "PCI dom# 0x%x has collision, using 0x%x",
3504 dom_req, dom);
3505
3506 hbus->bridge->domain_nr = dom;
3507 #ifdef CONFIG_X86
3508 hbus->sysdata.domain = dom;
3509 #elif defined(CONFIG_ARM64)
3510
3511
3512
3513
3514
3515
3516
3517 hbus->sysdata.parent = hdev->device.parent;
3518 #endif
3519
3520 hbus->hdev = hdev;
3521 INIT_LIST_HEAD(&hbus->children);
3522 INIT_LIST_HEAD(&hbus->dr_list);
3523 spin_lock_init(&hbus->config_lock);
3524 spin_lock_init(&hbus->device_list_lock);
3525 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
3526 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
3527 hbus->bridge->domain_nr);
3528 if (!hbus->wq) {
3529 ret = -ENOMEM;
3530 goto free_dom;
3531 }
3532
3533 hdev->channel->next_request_id_callback = vmbus_next_request_id;
3534 hdev->channel->request_addr_callback = vmbus_request_addr;
3535 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
3536
3537 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3538 hv_pci_onchannelcallback, hbus);
3539 if (ret)
3540 goto destroy_wq;
3541
3542 hv_set_drvdata(hdev, hbus);
3543
3544 ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
3545 ARRAY_SIZE(pci_protocol_versions));
3546 if (ret)
3547 goto close;
3548
3549 ret = hv_allocate_config_window(hbus);
3550 if (ret)
3551 goto close;
3552
3553 hbus->cfg_addr = ioremap(hbus->mem_config->start,
3554 PCI_CONFIG_MMIO_LENGTH);
3555 if (!hbus->cfg_addr) {
3556 dev_err(&hdev->device,
3557 "Unable to map a virtual address for config space\n");
3558 ret = -ENOMEM;
3559 goto free_config;
3560 }
3561
3562 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
3563 if (!name) {
3564 ret = -ENOMEM;
3565 goto unmap;
3566 }
3567
3568 hbus->fwnode = irq_domain_alloc_named_fwnode(name);
3569 kfree(name);
3570 if (!hbus->fwnode) {
3571 ret = -ENOMEM;
3572 goto unmap;
3573 }
3574
3575 ret = hv_pcie_init_irq_domain(hbus);
3576 if (ret)
3577 goto free_fwnode;
3578
3579 retry:
3580 ret = hv_pci_query_relations(hdev);
3581 if (ret)
3582 goto free_irq_domain;
3583
3584 ret = hv_pci_enter_d0(hdev);
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598 if (ret == -EPROTO && enter_d0_retry) {
3599 enter_d0_retry = false;
3600
3601 dev_err(&hdev->device, "Retrying D0 Entry\n");
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611 hbus->wslot_res_allocated = 255;
3612 ret = hv_pci_bus_exit(hdev, true);
3613
3614 if (ret == 0)
3615 goto retry;
3616
3617 dev_err(&hdev->device,
3618 "Retrying D0 failed with ret %d\n", ret);
3619 }
3620 if (ret)
3621 goto free_irq_domain;
3622
3623 ret = hv_pci_allocate_bridge_windows(hbus);
3624 if (ret)
3625 goto exit_d0;
3626
3627 ret = hv_send_resources_allocated(hdev);
3628 if (ret)
3629 goto free_windows;
3630
3631 prepopulate_bars(hbus);
3632
3633 hbus->state = hv_pcibus_probed;
3634
3635 ret = create_root_hv_pci_bus(hbus);
3636 if (ret)
3637 goto free_windows;
3638
3639 return 0;
3640
3641 free_windows:
3642 hv_pci_free_bridge_windows(hbus);
3643 exit_d0:
3644 (void) hv_pci_bus_exit(hdev, true);
3645 free_irq_domain:
3646 irq_domain_remove(hbus->irq_domain);
3647 free_fwnode:
3648 irq_domain_free_fwnode(hbus->fwnode);
3649 unmap:
3650 iounmap(hbus->cfg_addr);
3651 free_config:
3652 hv_free_config_window(hbus);
3653 close:
3654 vmbus_close(hdev->channel);
3655 destroy_wq:
3656 destroy_workqueue(hbus->wq);
3657 free_dom:
3658 hv_put_dom_num(hbus->bridge->domain_nr);
3659 free_bus:
3660 kfree(hbus);
3661 return ret;
3662 }
3663
3664 static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
3665 {
3666 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3667 struct vmbus_channel *chan = hdev->channel;
3668 struct {
3669 struct pci_packet teardown_packet;
3670 u8 buffer[sizeof(struct pci_message)];
3671 } pkt;
3672 struct hv_pci_compl comp_pkt;
3673 struct hv_pci_dev *hpdev, *tmp;
3674 unsigned long flags;
3675 u64 trans_id;
3676 int ret;
3677
3678
3679
3680
3681
3682 if (chan->rescind)
3683 return 0;
3684
3685 if (!keep_devs) {
3686 struct list_head removed;
3687
3688
3689 INIT_LIST_HEAD(&removed);
3690 spin_lock_irqsave(&hbus->device_list_lock, flags);
3691 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry)
3692 list_move_tail(&hpdev->list_entry, &removed);
3693 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
3694
3695
3696 list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) {
3697 list_del(&hpdev->list_entry);
3698 if (hpdev->pci_slot)
3699 pci_destroy_slot(hpdev->pci_slot);
3700
3701 put_pcichild(hpdev);
3702 put_pcichild(hpdev);
3703 }
3704 }
3705
3706 ret = hv_send_resources_released(hdev);
3707 if (ret) {
3708 dev_err(&hdev->device,
3709 "Couldn't send resources released packet(s)\n");
3710 return ret;
3711 }
3712
3713 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
3714 init_completion(&comp_pkt.host_event);
3715 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
3716 pkt.teardown_packet.compl_ctxt = &comp_pkt;
3717 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
3718
3719 ret = vmbus_sendpacket_getid(chan, &pkt.teardown_packet.message,
3720 sizeof(struct pci_message),
3721 (unsigned long)&pkt.teardown_packet,
3722 &trans_id, VM_PKT_DATA_INBAND,
3723 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3724 if (ret)
3725 return ret;
3726
3727 if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0) {
3728
3729
3730
3731
3732
3733
3734
3735 vmbus_request_addr_match(chan, trans_id,
3736 (unsigned long)&pkt.teardown_packet);
3737 return -ETIMEDOUT;
3738 }
3739
3740 return 0;
3741 }
3742
3743
3744
3745
3746
3747
3748
3749 static int hv_pci_remove(struct hv_device *hdev)
3750 {
3751 struct hv_pcibus_device *hbus;
3752 int ret;
3753
3754 hbus = hv_get_drvdata(hdev);
3755 if (hbus->state == hv_pcibus_installed) {
3756 tasklet_disable(&hdev->channel->callback_event);
3757 hbus->state = hv_pcibus_removing;
3758 tasklet_enable(&hdev->channel->callback_event);
3759 destroy_workqueue(hbus->wq);
3760 hbus->wq = NULL;
3761
3762
3763
3764
3765
3766
3767
3768 pci_lock_rescan_remove();
3769 pci_stop_root_bus(hbus->bridge->bus);
3770 hv_pci_remove_slots(hbus);
3771 pci_remove_root_bus(hbus->bridge->bus);
3772 pci_unlock_rescan_remove();
3773 }
3774
3775 ret = hv_pci_bus_exit(hdev, false);
3776
3777 vmbus_close(hdev->channel);
3778
3779 iounmap(hbus->cfg_addr);
3780 hv_free_config_window(hbus);
3781 hv_pci_free_bridge_windows(hbus);
3782 irq_domain_remove(hbus->irq_domain);
3783 irq_domain_free_fwnode(hbus->fwnode);
3784
3785 hv_put_dom_num(hbus->bridge->domain_nr);
3786
3787 kfree(hbus);
3788 return ret;
3789 }
3790
3791 static int hv_pci_suspend(struct hv_device *hdev)
3792 {
3793 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3794 enum hv_pcibus_state old_state;
3795 int ret;
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815 tasklet_disable(&hdev->channel->callback_event);
3816
3817
3818 old_state = hbus->state;
3819 if (hbus->state == hv_pcibus_installed)
3820 hbus->state = hv_pcibus_removing;
3821
3822 tasklet_enable(&hdev->channel->callback_event);
3823
3824 if (old_state != hv_pcibus_installed)
3825 return -EINVAL;
3826
3827 flush_workqueue(hbus->wq);
3828
3829 ret = hv_pci_bus_exit(hdev, true);
3830 if (ret)
3831 return ret;
3832
3833 vmbus_close(hdev->channel);
3834
3835 return 0;
3836 }
3837
3838 static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
3839 {
3840 struct irq_data *irq_data;
3841 struct msi_desc *entry;
3842 int ret = 0;
3843
3844 msi_lock_descs(&pdev->dev);
3845 msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
3846 irq_data = irq_get_irq_data(entry->irq);
3847 if (WARN_ON_ONCE(!irq_data)) {
3848 ret = -EINVAL;
3849 break;
3850 }
3851
3852 hv_compose_msi_msg(irq_data, &entry->msg);
3853 }
3854 msi_unlock_descs(&pdev->dev);
3855
3856 return ret;
3857 }
3858
3859
3860
3861
3862
3863
3864
3865
3866 static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
3867 {
3868 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL);
3869 }
3870
3871 static int hv_pci_resume(struct hv_device *hdev)
3872 {
3873 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3874 enum pci_protocol_version_t version[1];
3875 int ret;
3876
3877 hbus->state = hv_pcibus_init;
3878
3879 hdev->channel->next_request_id_callback = vmbus_next_request_id;
3880 hdev->channel->request_addr_callback = vmbus_request_addr;
3881 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
3882
3883 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3884 hv_pci_onchannelcallback, hbus);
3885 if (ret)
3886 return ret;
3887
3888
3889 version[0] = hbus->protocol_version;
3890 ret = hv_pci_protocol_negotiation(hdev, version, 1);
3891 if (ret)
3892 goto out;
3893
3894 ret = hv_pci_query_relations(hdev);
3895 if (ret)
3896 goto out;
3897
3898 ret = hv_pci_enter_d0(hdev);
3899 if (ret)
3900 goto out;
3901
3902 ret = hv_send_resources_allocated(hdev);
3903 if (ret)
3904 goto out;
3905
3906 prepopulate_bars(hbus);
3907
3908 hv_pci_restore_msi_state(hbus);
3909
3910 hbus->state = hv_pcibus_installed;
3911 return 0;
3912 out:
3913 vmbus_close(hdev->channel);
3914 return ret;
3915 }
3916
3917 static const struct hv_vmbus_device_id hv_pci_id_table[] = {
3918
3919
3920 { HV_PCIE_GUID, },
3921 { },
3922 };
3923
3924 MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
3925
3926 static struct hv_driver hv_pci_drv = {
3927 .name = "hv_pci",
3928 .id_table = hv_pci_id_table,
3929 .probe = hv_pci_probe,
3930 .remove = hv_pci_remove,
3931 .suspend = hv_pci_suspend,
3932 .resume = hv_pci_resume,
3933 };
3934
3935 static void __exit exit_hv_pci_drv(void)
3936 {
3937 vmbus_driver_unregister(&hv_pci_drv);
3938
3939 hvpci_block_ops.read_block = NULL;
3940 hvpci_block_ops.write_block = NULL;
3941 hvpci_block_ops.reg_blk_invalidate = NULL;
3942 }
3943
3944 static int __init init_hv_pci_drv(void)
3945 {
3946 int ret;
3947
3948 if (!hv_is_hyperv_initialized())
3949 return -ENODEV;
3950
3951 ret = hv_pci_irqchip_init();
3952 if (ret)
3953 return ret;
3954
3955
3956 set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
3957
3958
3959 hvpci_block_ops.read_block = hv_read_config_block;
3960 hvpci_block_ops.write_block = hv_write_config_block;
3961 hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
3962
3963 return vmbus_driver_register(&hv_pci_drv);
3964 }
3965
3966 module_init(init_hv_pci_drv);
3967 module_exit(exit_hv_pci_drv);
3968
3969 MODULE_DESCRIPTION("Hyper-V PCI");
3970 MODULE_LICENSE("GPL v2");