2 * PCI Backend - Handles the virtual fields found on the capability lists
3 * in the configuration space.
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
8 #include <linux/kernel.h>
11 #include "conf_space.h"
13 static LIST_HEAD(capabilities);
14 struct xen_pcibk_config_capability {
15 struct list_head cap_list;
19 /* If the device has the capability found above, add these fields */
20 const struct config_field *fields;
23 static const struct config_field caplist_header[] = {
25 .offset = PCI_CAP_LIST_ID,
26 .size = 2, /* encompass PCI_CAP_LIST_ID & PCI_CAP_LIST_NEXT */
27 .u.w.read = xen_pcibk_read_config_word,
33 static inline void register_capability(struct xen_pcibk_config_capability *cap)
35 list_add_tail(&cap->cap_list, &capabilities);
38 int xen_pcibk_config_capability_add_fields(struct pci_dev *dev)
41 struct xen_pcibk_config_capability *cap;
44 list_for_each_entry(cap, &capabilities, cap_list) {
45 cap_offset = pci_find_capability(dev, cap->capability);
47 dev_dbg(&dev->dev, "Found capability 0x%x at 0x%x\n",
48 cap->capability, cap_offset);
50 err = xen_pcibk_config_add_fields_offset(dev,
55 err = xen_pcibk_config_add_fields_offset(dev,
67 static int vpd_address_write(struct pci_dev *dev, int offset, u16 value,
70 /* Disallow writes to the vital product data */
71 if (value & PCI_VPD_ADDR_F)
72 return PCIBIOS_SET_FAILED;
74 return pci_write_config_word(dev, offset, value);
77 static const struct config_field caplist_vpd[] = {
79 .offset = PCI_VPD_ADDR,
81 .u.w.read = xen_pcibk_read_config_word,
82 .u.w.write = vpd_address_write,
85 .offset = PCI_VPD_DATA,
87 .u.dw.read = xen_pcibk_read_config_dword,
93 static int pm_caps_read(struct pci_dev *dev, int offset, u16 *value,
99 err = pci_read_config_word(dev, offset, &real_value);
103 *value = real_value & ~PCI_PM_CAP_PME_MASK;
109 /* PM_OK_BITS specifies the bits that the driver domain is allowed to change.
110 * Can't allow driver domain to enable PMEs - they're shared */
111 #define PM_OK_BITS (PCI_PM_CTRL_PME_STATUS|PCI_PM_CTRL_DATA_SEL_MASK)
113 static int pm_ctrl_write(struct pci_dev *dev, int offset, u16 new_value,
118 pci_power_t new_state, old_state;
120 err = pci_read_config_word(dev, offset, &old_value);
124 old_state = (pci_power_t)(old_value & PCI_PM_CTRL_STATE_MASK);
125 new_state = (pci_power_t)(new_value & PCI_PM_CTRL_STATE_MASK);
127 new_value &= PM_OK_BITS;
128 if ((old_value & PM_OK_BITS) != new_value) {
129 new_value = (old_value & ~PM_OK_BITS) | new_value;
130 err = pci_write_config_word(dev, offset, new_value);
135 /* Let pci core handle the power management change */
136 dev_dbg(&dev->dev, "set power state to %x\n", new_state);
137 err = pci_set_power_state(dev, new_state);
139 err = PCIBIOS_SET_FAILED;
145 * Device may lose PCI config info on D3->D0 transition. This
146 * is a problem for some guests which will not reset BARs. Even
147 * those that have a go will be foiled by our BAR-write handler
148 * which will discard the write! Since Linux won't re-init
149 * the config space automatically in all cases, we do it here.
150 * Future: Should we re-initialise all first 64 bytes of config space?
152 if (new_state == PCI_D0 &&
153 (old_state == PCI_D3hot || old_state == PCI_D3cold) &&
154 !(old_value & PCI_PM_CTRL_NO_SOFT_RESET))
155 pci_restore_bars(dev);
162 /* Ensure PMEs are disabled */
163 static void *pm_ctrl_init(struct pci_dev *dev, int offset)
168 err = pci_read_config_word(dev, offset, &value);
172 if (value & PCI_PM_CTRL_PME_ENABLE) {
173 value &= ~PCI_PM_CTRL_PME_ENABLE;
174 err = pci_write_config_word(dev, offset, value);
181 static const struct config_field caplist_pm[] = {
183 .offset = PCI_PM_PMC,
185 .u.w.read = pm_caps_read,
188 .offset = PCI_PM_CTRL,
190 .init = pm_ctrl_init,
191 .u.w.read = xen_pcibk_read_config_word,
192 .u.w.write = pm_ctrl_write,
195 .offset = PCI_PM_PPB_EXTENSIONS,
197 .u.b.read = xen_pcibk_read_config_byte,
200 .offset = PCI_PM_DATA_REGISTER,
202 .u.b.read = xen_pcibk_read_config_byte,
207 static struct xen_pcibk_config_capability xen_pcibk_config_capability_pm = {
208 .capability = PCI_CAP_ID_PM,
209 .fields = caplist_pm,
211 static struct xen_pcibk_config_capability xen_pcibk_config_capability_vpd = {
212 .capability = PCI_CAP_ID_VPD,
213 .fields = caplist_vpd,
216 int xen_pcibk_config_capability_init(void)
218 register_capability(&xen_pcibk_config_capability_vpd);
219 register_capability(&xen_pcibk_config_capability_pm);