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"
6315a1350SMichael S. Tsirkin
7315a1350SMichael S. Tsirkin #define SLOTID_CAP_LENGTH 4
8786a4ea8SStefan Hajnoczi #define SLOTID_NSLOTS_SHIFT ctz32(PCI_SID_ESR_NSLOTS)
9315a1350SMichael S. Tsirkin
slotid_cap_init(PCIDevice * d,int nslots,uint8_t chassis,unsigned offset,Error ** errp)10315a1350SMichael S. Tsirkin int slotid_cap_init(PCIDevice *d, int nslots,
11315a1350SMichael S. Tsirkin uint8_t chassis,
12344475e7SMao Zhongyi unsigned offset,
13344475e7SMao Zhongyi Error **errp)
14315a1350SMichael S. Tsirkin {
15315a1350SMichael S. Tsirkin int cap;
169a7c2a59SMao Zhongyi
17315a1350SMichael S. Tsirkin if (!chassis) {
18344475e7SMao Zhongyi error_setg(errp, "Bridge chassis not specified. Each bridge is required"
19315a1350SMichael S. Tsirkin " to be assigned a unique chassis id > 0.");
20315a1350SMichael S. Tsirkin return -EINVAL;
21315a1350SMichael S. Tsirkin }
22315a1350SMichael S. Tsirkin if (nslots < 0 || nslots > (PCI_SID_ESR_NSLOTS >> SLOTID_NSLOTS_SHIFT)) {
23315a1350SMichael S. Tsirkin /* TODO: error report? */
24315a1350SMichael S. Tsirkin return -EINVAL;
25315a1350SMichael S. Tsirkin }
26315a1350SMichael S. Tsirkin
279a7c2a59SMao Zhongyi cap = pci_add_capability(d, PCI_CAP_ID_SLOTID, offset,
28344475e7SMao Zhongyi SLOTID_CAP_LENGTH, errp);
29315a1350SMichael S. Tsirkin if (cap < 0) {
30315a1350SMichael S. Tsirkin return cap;
31315a1350SMichael S. Tsirkin }
32315a1350SMichael S. Tsirkin /* We make each chassis unique, this way each bridge is First in Chassis */
33315a1350SMichael S. Tsirkin d->config[cap + PCI_SID_ESR] = PCI_SID_ESR_FIC |
34315a1350SMichael S. Tsirkin (nslots << SLOTID_NSLOTS_SHIFT);
35315a1350SMichael S. Tsirkin d->cmask[cap + PCI_SID_ESR] = 0xff;
36315a1350SMichael S. Tsirkin d->config[cap + PCI_SID_CHASSIS_NR] = chassis;
37315a1350SMichael S. Tsirkin /* Note: Chassis number register is non-volatile,
38315a1350SMichael S. Tsirkin so we don't reset it. */
39315a1350SMichael S. Tsirkin /* TODO: store in eeprom? */
40315a1350SMichael S. Tsirkin d->wmask[cap + PCI_SID_CHASSIS_NR] = 0xff;
41315a1350SMichael S. Tsirkin
42315a1350SMichael S. Tsirkin d->cap_present |= QEMU_PCI_CAP_SLOTID;
43315a1350SMichael S. Tsirkin return 0;
44315a1350SMichael S. Tsirkin }
45315a1350SMichael S. Tsirkin
slotid_cap_cleanup(PCIDevice * d)46315a1350SMichael S. Tsirkin void slotid_cap_cleanup(PCIDevice *d)
47315a1350SMichael S. Tsirkin {
48315a1350SMichael S. Tsirkin /* TODO: cleanup config space? */
49315a1350SMichael S. Tsirkin d->cap_present &= ~QEMU_PCI_CAP_SLOTID;
50315a1350SMichael S. Tsirkin }
51