// SPDX-License-Identifier: GPL-2.0 /* * PCI Backend - Functions for creating a virtual configuration space for * exported PCI Devices. * It's dangerous to allow PCI Driver Domains to change their * device's resources (memory, i/o ports, interrupts). We need to * restrict changes to certain PCI Configuration registers: * BARs, INTERRUPT_PIN, most registers in the header... * * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
*/
/* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
* xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */ #define DEFINE_PCI_CONFIG(op, size, type) \ int xen_pcibk_##op##_config_##size \
(struct pci_dev *dev, int offset, type value, void *data) \
{ \ return pci_##op##_config_##size(dev, offset, value); \
}
DEFINE_PCI_CONFIG(write, byte, u8)
DEFINE_PCI_CONFIG(write, word, u16)
DEFINE_PCI_CONFIG(write, dword, u32)
staticint conf_space_read(struct pci_dev *dev, conststruct config_field_entry *entry, int offset, u32 *value)
{ int ret = 0; conststruct config_field *field = entry->field;
*value = 0;
switch (field->size) { case 1: if (field->u.b.read)
ret = field->u.b.read(dev, offset, (u8 *) value,
entry->data); break; case 2: if (field->u.w.read)
ret = field->u.w.read(dev, offset, (u16 *) value,
entry->data); break; case 4: if (field->u.dw.read)
ret = field->u.dw.read(dev, offset, value, entry->data); break;
} return ret;
}
staticint conf_space_write(struct pci_dev *dev, conststruct config_field_entry *entry, int offset, u32 value)
{ int ret = 0; conststruct config_field *field = entry->field;
switch (field->size) { case 1: if (field->u.b.write)
ret = field->u.b.write(dev, offset, (u8) value,
entry->data); break; case 2: if (field->u.w.write)
ret = field->u.w.write(dev, offset, (u16) value,
entry->data); break; case 4: if (field->u.dw.write)
ret = field->u.dw.write(dev, offset, value,
entry->data); break;
} return ret;
}
staticint xen_pcibios_err_to_errno(int err)
{ switch (err) { case PCIBIOS_SUCCESSFUL: return XEN_PCI_ERR_success; case PCIBIOS_DEVICE_NOT_FOUND: return XEN_PCI_ERR_dev_not_found; case PCIBIOS_BAD_REGISTER_NUMBER: return XEN_PCI_ERR_invalid_offset; case PCIBIOS_FUNC_NOT_SUPPORTED: return XEN_PCI_ERR_not_implemented; case PCIBIOS_SET_FAILED: return XEN_PCI_ERR_access_denied;
} return err;
}
int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
u32 *ret_val)
{ int err = 0; struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev); conststruct config_field_entry *cfg_entry; conststruct config_field *field; int field_start, field_end; /* if read fails for any reason, return 0
* (as if device didn't respond) */
u32 value = 0, tmp_val;
dev_dbg(&dev->dev, "read %d bytes at 0x%x\n", size, offset);
if (!valid_request(offset, size)) {
err = XEN_PCI_ERR_invalid_offset; goto out;
}
/* Get the real value first, then modify as appropriate */ switch (size) { case 1:
err = pci_read_config_byte(dev, offset, (u8 *) &value); break; case 2:
err = pci_read_config_word(dev, offset, (u16 *) &value); break; case 4:
err = pci_read_config_dword(dev, offset, &value); break;
}
list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
field = cfg_entry->field;
/* handled is set true here, but not every byte * may have been written! Properly detecting if * every byte is handled is unnecessary as the * flag is used to detect devices that need * special helpers to work correctly.
*/
handled = 1;
}
}
if (!handled && !err) { /* By default, anything not specificially handled above is * read-only. The permissive flag changes this behavior so * that anything not specifically handled above is writable. * This means that some fields may still be read-only because * they have entries in the config_field list that intercept
* the write and do nothing. */ if (dev_data->permissive || xen_pcibk_permissive) { switch (size) { case 1:
err = pci_write_config_byte(dev, offset,
(u8) value); break; case 2:
err = pci_write_config_word(dev, offset,
(u16) value); break; case 4:
err = pci_write_config_dword(dev, offset,
(u32) value); break;
}
} elseif (!dev_data->warned_on_write) {
dev_data->warned_on_write = 1;
dev_warn(&dev->dev, "Driver tried to write to a " "read-only configuration space field at offset" " 0x%x, size %d. This may be harmless, but if " "you have problems with your device:\n" "1) see permissive attribute in sysfs\n" "2) report problems to the xen-devel " "mailing list along with details of your " "device obtained from lspci.\n", offset, size);
}
}
return xen_pcibios_err_to_errno(err);
}
int xen_pcibk_get_interrupt_type(struct pci_dev *dev)
{ int err;
u16 val; int ret = 0;
/* * Do not trust dev->msi(x)_enabled here, as enabling could be done * bypassing the pci_*msi* functions, by the qemu.
*/ if (dev->msi_cap) {
err = pci_read_config_word(dev,
dev->msi_cap + PCI_MSI_FLAGS,
&val); if (err) return err; if (val & PCI_MSI_FLAGS_ENABLE)
ret |= INTERRUPT_TYPE_MSI;
} if (dev->msix_cap) {
err = pci_read_config_word(dev,
dev->msix_cap + PCI_MSIX_FLAGS,
&val); if (err) return err; if (val & PCI_MSIX_FLAGS_ENABLE)
ret |= INTERRUPT_TYPE_MSIX;
}
/* * PCIe spec says device cannot use INTx if MSI/MSI-X is enabled, * so check for INTx only when both are disabled.
*/ if (!ret) {
err = pci_read_config_word(dev, PCI_COMMAND, &val); if (err) return err; if (!(val & PCI_COMMAND_INTX_DISABLE))
ret |= INTERRUPT_TYPE_INTX;
}
if (field->init) {
tmp = field->init(dev, OFFSET(cfg_entry));
if (IS_ERR(tmp)) {
err = PTR_ERR(tmp); goto out;
}
cfg_entry->data = tmp;
}
dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
OFFSET(cfg_entry));
list_add_tail(&cfg_entry->list, &dev_data->config_fields);
out: if (err)
kfree(cfg_entry);
return err;
}
/* This sets up the device's virtual configuration space to keep track of * certain registers (like the base address registers (BARs) so that we can * keep the client from manipulating them directly.
*/ int xen_pcibk_config_init_dev(struct pci_dev *dev)
{ int err = 0; struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.