xref: /qemu/hw/pci/slotid_cap.c (revision b4a42f81383d60900aae09513f42eb857a5a7c7c)
1c759b24fSMichael S. Tsirkin #include "hw/pci/slotid_cap.h"
2c759b24fSMichael S. Tsirkin #include "hw/pci/pci.h"
3*b4a42f81SPaolo Bonzini #include "qemu/error-report.h"
4762833b3SMichael S. Tsirkin 
5762833b3SMichael S. Tsirkin #define SLOTID_CAP_LENGTH 4
6762833b3SMichael S. Tsirkin #define SLOTID_NSLOTS_SHIFT (ffs(PCI_SID_ESR_NSLOTS) - 1)
7762833b3SMichael S. Tsirkin 
8762833b3SMichael S. Tsirkin int slotid_cap_init(PCIDevice *d, int nslots,
9762833b3SMichael S. Tsirkin                     uint8_t chassis,
10762833b3SMichael S. Tsirkin                     unsigned offset)
11762833b3SMichael S. Tsirkin {
12762833b3SMichael S. Tsirkin     int cap;
13762833b3SMichael S. Tsirkin     if (!chassis) {
14762833b3SMichael S. Tsirkin         error_report("Bridge chassis not specified. Each bridge is required "
15762833b3SMichael S. Tsirkin                      "to be assigned a unique chassis id > 0.");
16762833b3SMichael S. Tsirkin         return -EINVAL;
17762833b3SMichael S. Tsirkin     }
18762833b3SMichael S. Tsirkin     if (nslots < 0 || nslots > (PCI_SID_ESR_NSLOTS >> SLOTID_NSLOTS_SHIFT)) {
19762833b3SMichael S. Tsirkin         /* TODO: error report? */
20762833b3SMichael S. Tsirkin         return -EINVAL;
21762833b3SMichael S. Tsirkin     }
22762833b3SMichael S. Tsirkin 
23762833b3SMichael S. Tsirkin     cap = pci_add_capability(d, PCI_CAP_ID_SLOTID, offset, SLOTID_CAP_LENGTH);
24762833b3SMichael S. Tsirkin     if (cap < 0) {
25762833b3SMichael S. Tsirkin         return cap;
26762833b3SMichael S. Tsirkin     }
27762833b3SMichael S. Tsirkin     /* We make each chassis unique, this way each bridge is First in Chassis */
28762833b3SMichael S. Tsirkin     d->config[cap + PCI_SID_ESR] = PCI_SID_ESR_FIC |
29762833b3SMichael S. Tsirkin         (nslots << SLOTID_NSLOTS_SHIFT);
30762833b3SMichael S. Tsirkin     d->cmask[cap + PCI_SID_ESR] = 0xff;
31762833b3SMichael S. Tsirkin     d->config[cap + PCI_SID_CHASSIS_NR] = chassis;
32762833b3SMichael S. Tsirkin     /* Note: Chassis number register is non-volatile,
33762833b3SMichael S. Tsirkin        so we don't reset it. */
34762833b3SMichael S. Tsirkin     /* TODO: store in eeprom? */
35762833b3SMichael S. Tsirkin     d->wmask[cap + PCI_SID_CHASSIS_NR] = 0xff;
36762833b3SMichael S. Tsirkin 
37762833b3SMichael S. Tsirkin     d->cap_present |= QEMU_PCI_CAP_SLOTID;
38762833b3SMichael S. Tsirkin     return 0;
39762833b3SMichael S. Tsirkin }
40762833b3SMichael S. Tsirkin 
41762833b3SMichael S. Tsirkin void slotid_cap_cleanup(PCIDevice *d)
42762833b3SMichael S. Tsirkin {
43762833b3SMichael S. Tsirkin     /* TODO: cleanup config space? */
44762833b3SMichael S. Tsirkin     d->cap_present &= ~QEMU_PCI_CAP_SLOTID;
45762833b3SMichael S. Tsirkin }
46