197d5408fSPeter Maydell #include "qemu/osdep.h" 2c759b24fSMichael S. Tsirkin #include "hw/pci/slotid_cap.h" 3c759b24fSMichael S. Tsirkin #include "hw/pci/pci.h" 4b4a42f81SPaolo Bonzini #include "qemu/error-report.h" 5*9a7c2a59SMao 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, 12762833b3SMichael S. Tsirkin unsigned offset) 13762833b3SMichael S. Tsirkin { 14762833b3SMichael S. Tsirkin int cap; 15*9a7c2a59SMao Zhongyi Error *local_err = NULL; 16*9a7c2a59SMao Zhongyi 17762833b3SMichael S. Tsirkin if (!chassis) { 18762833b3SMichael S. Tsirkin error_report("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 27*9a7c2a59SMao Zhongyi cap = pci_add_capability(d, PCI_CAP_ID_SLOTID, offset, 28*9a7c2a59SMao Zhongyi SLOTID_CAP_LENGTH, &local_err); 29762833b3SMichael S. Tsirkin if (cap < 0) { 30*9a7c2a59SMao Zhongyi error_report_err(local_err); 31762833b3SMichael S. Tsirkin return cap; 32762833b3SMichael S. Tsirkin } 33762833b3SMichael S. Tsirkin /* We make each chassis unique, this way each bridge is First in Chassis */ 34762833b3SMichael S. Tsirkin d->config[cap + PCI_SID_ESR] = PCI_SID_ESR_FIC | 35762833b3SMichael S. Tsirkin (nslots << SLOTID_NSLOTS_SHIFT); 36762833b3SMichael S. Tsirkin d->cmask[cap + PCI_SID_ESR] = 0xff; 37762833b3SMichael S. Tsirkin d->config[cap + PCI_SID_CHASSIS_NR] = chassis; 38762833b3SMichael S. Tsirkin /* Note: Chassis number register is non-volatile, 39762833b3SMichael S. Tsirkin so we don't reset it. */ 40762833b3SMichael S. Tsirkin /* TODO: store in eeprom? */ 41762833b3SMichael S. Tsirkin d->wmask[cap + PCI_SID_CHASSIS_NR] = 0xff; 42762833b3SMichael S. Tsirkin 43762833b3SMichael S. Tsirkin d->cap_present |= QEMU_PCI_CAP_SLOTID; 44762833b3SMichael S. Tsirkin return 0; 45762833b3SMichael S. Tsirkin } 46762833b3SMichael S. Tsirkin 47762833b3SMichael S. Tsirkin void slotid_cap_cleanup(PCIDevice *d) 48762833b3SMichael S. Tsirkin { 49762833b3SMichael S. Tsirkin /* TODO: cleanup config space? */ 50762833b3SMichael S. Tsirkin d->cap_present &= ~QEMU_PCI_CAP_SLOTID; 51762833b3SMichael S. Tsirkin } 52