1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Backend - Handles the virtual fields found on the capability lists
4 * in the configuration space.
6 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
9 #include <linux/kernel.h>
10 #include <linux/pci.h>
12 #include "conf_space.h"
14 static LIST_HEAD(capabilities
);
15 struct xen_pcibk_config_capability
{
16 struct list_head cap_list
;
20 /* If the device has the capability found above, add these fields */
21 const struct config_field
*fields
;
24 static const struct config_field caplist_header
[] = {
26 .offset
= PCI_CAP_LIST_ID
,
27 .size
= 2, /* encompass PCI_CAP_LIST_ID & PCI_CAP_LIST_NEXT */
28 .u
.w
.read
= xen_pcibk_read_config_word
,
34 static inline void register_capability(struct xen_pcibk_config_capability
*cap
)
36 list_add_tail(&cap
->cap_list
, &capabilities
);
39 int xen_pcibk_config_capability_add_fields(struct pci_dev
*dev
)
42 struct xen_pcibk_config_capability
*cap
;
45 list_for_each_entry(cap
, &capabilities
, cap_list
) {
46 cap_offset
= pci_find_capability(dev
, cap
->capability
);
48 dev_dbg(&dev
->dev
, "Found capability 0x%x at 0x%x\n",
49 cap
->capability
, cap_offset
);
51 err
= xen_pcibk_config_add_fields_offset(dev
,
56 err
= xen_pcibk_config_add_fields_offset(dev
,
68 static int vpd_address_write(struct pci_dev
*dev
, int offset
, u16 value
,
71 /* Disallow writes to the vital product data */
72 if (value
& PCI_VPD_ADDR_F
)
73 return PCIBIOS_SET_FAILED
;
75 return pci_write_config_word(dev
, offset
, value
);
78 static const struct config_field caplist_vpd
[] = {
80 .offset
= PCI_VPD_ADDR
,
82 .u
.w
.read
= xen_pcibk_read_config_word
,
83 .u
.w
.write
= vpd_address_write
,
86 .offset
= PCI_VPD_DATA
,
88 .u
.dw
.read
= xen_pcibk_read_config_dword
,
94 static int pm_caps_read(struct pci_dev
*dev
, int offset
, u16
*value
,
100 err
= pci_read_config_word(dev
, offset
, &real_value
);
104 *value
= real_value
& ~PCI_PM_CAP_PME_MASK
;
110 /* PM_OK_BITS specifies the bits that the driver domain is allowed to change.
111 * Can't allow driver domain to enable PMEs - they're shared */
112 #define PM_OK_BITS (PCI_PM_CTRL_PME_STATUS|PCI_PM_CTRL_DATA_SEL_MASK)
114 static int pm_ctrl_write(struct pci_dev
*dev
, int offset
, u16 new_value
,
119 pci_power_t new_state
;
121 err
= pci_read_config_word(dev
, offset
, &old_value
);
125 new_state
= (__force 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
;
147 /* Ensure PMEs are disabled */
148 static void *pm_ctrl_init(struct pci_dev
*dev
, int offset
)
153 err
= pci_read_config_word(dev
, offset
, &value
);
157 if (value
& PCI_PM_CTRL_PME_ENABLE
) {
158 value
&= ~PCI_PM_CTRL_PME_ENABLE
;
159 err
= pci_write_config_word(dev
, offset
, value
);
163 return err
? ERR_PTR(err
) : NULL
;
166 static const struct config_field caplist_pm
[] = {
168 .offset
= PCI_PM_PMC
,
170 .u
.w
.read
= pm_caps_read
,
173 .offset
= PCI_PM_CTRL
,
175 .init
= pm_ctrl_init
,
176 .u
.w
.read
= xen_pcibk_read_config_word
,
177 .u
.w
.write
= pm_ctrl_write
,
180 .offset
= PCI_PM_PPB_EXTENSIONS
,
182 .u
.b
.read
= xen_pcibk_read_config_byte
,
185 .offset
= PCI_PM_DATA_REGISTER
,
187 .u
.b
.read
= xen_pcibk_read_config_byte
,
192 static struct msi_msix_field_config
{
193 u16 enable_bit
; /* bit for enabling MSI/MSI-X */
194 u16 allowed_bits
; /* bits allowed to be changed */
195 unsigned int int_type
; /* interrupt type for exclusiveness check */
196 } msi_field_config
= {
197 .enable_bit
= PCI_MSI_FLAGS_ENABLE
,
198 .allowed_bits
= PCI_MSI_FLAGS_ENABLE
,
199 .int_type
= INTERRUPT_TYPE_MSI
,
200 }, msix_field_config
= {
201 .enable_bit
= PCI_MSIX_FLAGS_ENABLE
,
202 .allowed_bits
= PCI_MSIX_FLAGS_ENABLE
| PCI_MSIX_FLAGS_MASKALL
,
203 .int_type
= INTERRUPT_TYPE_MSIX
,
206 static void *msi_field_init(struct pci_dev
*dev
, int offset
)
208 return &msi_field_config
;
211 static void *msix_field_init(struct pci_dev
*dev
, int offset
)
213 return &msix_field_config
;
216 static int msi_msix_flags_write(struct pci_dev
*dev
, int offset
, u16 new_value
,
221 const struct msi_msix_field_config
*field_config
= data
;
222 const struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
224 if (xen_pcibk_permissive
|| dev_data
->permissive
)
227 err
= pci_read_config_word(dev
, offset
, &old_value
);
231 if (new_value
== old_value
)
234 if (!dev_data
->allow_interrupt_control
||
235 (new_value
^ old_value
) & ~field_config
->allowed_bits
)
236 return PCIBIOS_SET_FAILED
;
238 if (new_value
& field_config
->enable_bit
) {
240 * Don't allow enabling together with other interrupt type, but do
241 * allow enabling MSI(-X) while INTx is still active to please Linuxes
242 * MSI(-X) startup sequence. It is safe to do, as according to PCI
243 * spec, device with enabled MSI(-X) shouldn't use INTx.
245 int int_type
= xen_pcibk_get_interrupt_type(dev
);
247 if (int_type
== INTERRUPT_TYPE_NONE
||
248 int_type
== INTERRUPT_TYPE_INTX
||
249 int_type
== field_config
->int_type
)
251 return PCIBIOS_SET_FAILED
;
255 return pci_write_config_word(dev
, offset
, new_value
);
258 static const struct config_field caplist_msix
[] = {
260 .offset
= PCI_MSIX_FLAGS
,
262 .init
= msix_field_init
,
263 .u
.w
.read
= xen_pcibk_read_config_word
,
264 .u
.w
.write
= msi_msix_flags_write
,
269 static const struct config_field caplist_msi
[] = {
271 .offset
= PCI_MSI_FLAGS
,
273 .init
= msi_field_init
,
274 .u
.w
.read
= xen_pcibk_read_config_word
,
275 .u
.w
.write
= msi_msix_flags_write
,
280 static struct xen_pcibk_config_capability xen_pcibk_config_capability_pm
= {
281 .capability
= PCI_CAP_ID_PM
,
282 .fields
= caplist_pm
,
284 static struct xen_pcibk_config_capability xen_pcibk_config_capability_vpd
= {
285 .capability
= PCI_CAP_ID_VPD
,
286 .fields
= caplist_vpd
,
288 static struct xen_pcibk_config_capability xen_pcibk_config_capability_msi
= {
289 .capability
= PCI_CAP_ID_MSI
,
290 .fields
= caplist_msi
,
292 static struct xen_pcibk_config_capability xen_pcibk_config_capability_msix
= {
293 .capability
= PCI_CAP_ID_MSIX
,
294 .fields
= caplist_msix
,
297 int xen_pcibk_config_capability_init(void)
299 register_capability(&xen_pcibk_config_capability_vpd
);
300 register_capability(&xen_pcibk_config_capability_pm
);
301 register_capability(&xen_pcibk_config_capability_msi
);
302 register_capability(&xen_pcibk_config_capability_msix
);