60e3db8f612b0047ba58aa0863b72f02f032b743
[lunaix-os.git] / lunaix-os / arch / i386 / hal / pci_hba.c
1 #include <sys/apic.h>
2 #include <sys/pci_hba.h>
3
4 void
5 pci_setup_msi(struct pci_device* device, int vector)
6 {
7     // Dest: APIC#0, Physical Destination, No redirection
8     u32_t msi_addr = (__APIC_BASE_PADDR);
9
10     // Edge trigger, Fixed delivery
11     u32_t msi_data = vector;
12
13     pci_write_cspace(
14       device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr);
15
16     pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
17     pci_reg_t msg_ctl = reg1 >> 16;
18
19     int offset = !!(msg_ctl & MSI_CAP_64BIT) * 4;
20     pci_write_cspace(device->cspace_base,
21                      PCI_MSI_DATA(device->msi_loc, offset),
22                      msi_data & 0xffff);
23
24     if ((msg_ctl & MSI_CAP_MASK)) {
25         pci_write_cspace(
26           device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset), 0);
27     }
28
29     // manipulate the MSI_CTRL to allow device using MSI to request service.
30     reg1 = (reg1 & 0xff8fffff) | 0x10000;
31     pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
32 }