xref: /qemu/hw/pci/slotid_cap.c (revision edf5ca5dbe8031e7814ea34eb109b8f7d4024ae5)
197d5408fSPeter Maydell #include "qemu/osdep.h"
2c759b24fSMichael S. Tsirkin #include "hw/pci/slotid_cap.h"
3*edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
4b4a42f81SPaolo Bonzini #include "qemu/error-report.h"
59a7c2a59SMao Zhongyi #include "qapi/error.h"
6762833b3SMichael S. Tsirkin 
7762833b3SMichael S. Tsirkin #define SLOTID_CAP_LENGTH 4
8786a4ea8SStefan Hajnoczi #define SLOTID_NSLOTS_SHIFT ctz32(PCI_SID_ESR_NSLOTS)
9762833b3SMichael S. Tsirkin 
10762833b3SMichael S. Tsirkin int slotid_cap_init(PCIDevice *d, int nslots,
11762833b3SMichael S. Tsirkin                     uint8_t chassis,
12344475e7SMao Zhongyi                     unsigned offset,
13344475e7SMao Zhongyi                     Error **errp)
14762833b3SMichael S. Tsirkin {
15762833b3SMichael S. Tsirkin     int cap;
169a7c2a59SMao Zhongyi 
17762833b3SMichael S. Tsirkin     if (!chassis) {
18344475e7SMao Zhongyi         error_setg(errp, "Bridge chassis not specified. Each bridge is required"
19762833b3SMichael S. Tsirkin                    " to be assigned a unique chassis id > 0.");
20762833b3SMichael S. Tsirkin         return -EINVAL;
21762833b3SMichael S. Tsirkin     }
22762833b3SMichael S. Tsirkin     if (nslots < 0 || nslots > (PCI_SID_ESR_NSLOTS >> SLOTID_NSLOTS_SHIFT)) {
23762833b3SMichael S. Tsirkin         /* TODO: error report? */
24762833b3SMichael S. Tsirkin         return -EINVAL;
25762833b3SMichael S. Tsirkin     }
26762833b3SMichael S. Tsirkin 
279a7c2a59SMao Zhongyi     cap = pci_add_capability(d, PCI_CAP_ID_SLOTID, offset,
28344475e7SMao Zhongyi                              SLOTID_CAP_LENGTH, errp);
29762833b3SMichael S. Tsirkin     if (cap < 0) {
30762833b3SMichael S. Tsirkin         return cap;
31762833b3SMichael S. Tsirkin     }
32762833b3SMichael S. Tsirkin     /* We make each chassis unique, this way each bridge is First in Chassis */
33762833b3SMichael S. Tsirkin     d->config[cap + PCI_SID_ESR] = PCI_SID_ESR_FIC |
34762833b3SMichael S. Tsirkin         (nslots << SLOTID_NSLOTS_SHIFT);
35762833b3SMichael S. Tsirkin     d->cmask[cap + PCI_SID_ESR] = 0xff;
36762833b3SMichael S. Tsirkin     d->config[cap + PCI_SID_CHASSIS_NR] = chassis;
37762833b3SMichael S. Tsirkin     /* Note: Chassis number register is non-volatile,
38762833b3SMichael S. Tsirkin        so we don't reset it. */
39762833b3SMichael S. Tsirkin     /* TODO: store in eeprom? */
40762833b3SMichael S. Tsirkin     d->wmask[cap + PCI_SID_CHASSIS_NR] = 0xff;
41762833b3SMichael S. Tsirkin 
42762833b3SMichael S. Tsirkin     d->cap_present |= QEMU_PCI_CAP_SLOTID;
43762833b3SMichael S. Tsirkin     return 0;
44762833b3SMichael S. Tsirkin }
45762833b3SMichael S. Tsirkin 
46762833b3SMichael S. Tsirkin void slotid_cap_cleanup(PCIDevice *d)
47762833b3SMichael S. Tsirkin {
48762833b3SMichael S. Tsirkin     /* TODO: cleanup config space? */
49762833b3SMichael S. Tsirkin     d->cap_present &= ~QEMU_PCI_CAP_SLOTID;
50762833b3SMichael S. Tsirkin }
51