*/
#include <jailhouse/control.h>
+#include <jailhouse/mmio.h>
#include <jailhouse/pci.h>
#include <jailhouse/printk.h>
#include <jailhouse/string.h>
struct pci_ivshmem_endpoint {
u32 cspace[IVSHMEM_CFG_SIZE / sizeof(u32)];
u32 ivpos;
+ u64 bar0_address;
+ u64 bar4_address;
struct pci_device *device;
struct pci_ivshmem_endpoint *remote;
struct apic_irq_message irq_msg;
(PCI_CFG_BAR/8 + 2),
};
+static void ivshmem_write_doorbell(struct pci_ivshmem_endpoint *ive)
+{
+ struct pci_ivshmem_endpoint *remote = ive->remote;
+ struct apic_irq_message irq_msg;
+
+ if (!remote)
+ return;
+
+ /* get a copy of the struct before using it, the read barrier makes
+ * sure the copy is consistent */
+ irq_msg = remote->irq_msg;
+ memory_load_barrier();
+ if (irq_msg.valid)
+ apic_send_irq(irq_msg);
+}
+
+static enum mmio_result ivshmem_register_mmio(void *arg,
+ struct mmio_access *mmio)
+{
+ struct pci_ivshmem_endpoint *ive = arg;
+
+ /* read-only IVPosition */
+ if (mmio->address == IVSHMEM_REG_IVPOS && !mmio->is_write) {
+ mmio->value = ive->ivpos;
+ return MMIO_HANDLED;
+ }
+
+ if (mmio->address == IVSHMEM_REG_DBELL) {
+ if (mmio->is_write)
+ ivshmem_write_doorbell(ive);
+ else
+ mmio->value = 0;
+ return MMIO_HANDLED;
+ }
+ panic_printk("FATAL: Invalid ivshmem register %s, number %02x\n",
+ mmio->is_write ? "write" : "read", mmio->address);
+ return MMIO_ERROR;
+}
+
static bool ivshmem_is_msix_masked(struct pci_ivshmem_endpoint *ive)
{
union pci_msix_registers c;
return 0;
}
-/**
- * update the command register
- * note that we only accept writes to two flags
- */
-static int ivshmem_write_command(struct pci_ivshmem_endpoint *ive, u16 val)
-{
- u16 *cmd = (u16 *)&ive->cspace[PCI_CFG_COMMAND/4];
- int err;
-
- if ((val & PCI_CMD_MASTER) != (*cmd & PCI_CMD_MASTER)) {
- *cmd = (*cmd & ~PCI_CMD_MASTER) | (val & PCI_CMD_MASTER);
- err = ivshmem_update_msix(ive);
- if (err)
- return err;
- }
-
- *cmd = (*cmd & ~PCI_CMD_MEM) | (val & PCI_CMD_MEM);
- return 0;
-}
-
-static int ivshmem_msix_mmio(struct pci_ivshmem_endpoint *ive, bool is_write,
- u32 offset, u32 *value)
+static enum mmio_result ivshmem_msix_mmio(void *arg, struct mmio_access *mmio)
{
+ struct pci_ivshmem_endpoint *ive = arg;
u32 *msix_table = (u32 *)ive->device->msix_vectors;
- if (offset % 4)
+ if (mmio->address % 4)
goto fail;
/* MSI-X PBA */
- if (offset >= 0x10 * IVSHMEM_MSIX_VECTORS) {
- if (is_write) {
+ if (mmio->address >= 0x10 * IVSHMEM_MSIX_VECTORS) {
+ if (mmio->is_write) {
goto fail;
} else {
- *value = 0;
- return 1;
+ mmio->value = 0;
+ return MMIO_HANDLED;
}
/* MSI-X Table */
} else {
- if (is_write) {
- msix_table[offset/4] = *value;
+ if (mmio->is_write) {
+ msix_table[mmio->address / 4] = mmio->value;
if (ivshmem_update_msix(ive))
- return -1;
+ return MMIO_ERROR;
} else {
- *value = msix_table[offset/4];
+ mmio->value = msix_table[mmio->address / 4];
}
- return 1;
+ return MMIO_HANDLED;
}
fail:
panic_printk("FATAL: Invalid PCI MSI-X table/PBA access, device "
"%02x:%02x.%x\n", PCI_BDF_PARAMS(ive->device->info->bdf));
- return -1;
+ return MMIO_ERROR;
}
-static void ivshmem_write_doorbell(struct pci_ivshmem_endpoint *ive)
+/**
+ * update the command register
+ * note that we only accept writes to two flags
+ */
+static int ivshmem_write_command(struct pci_ivshmem_endpoint *ive, u16 val)
{
- struct pci_ivshmem_endpoint *remote = ive->remote;
- struct apic_irq_message irq_msg;
-
- if (!remote)
- return;
-
- /* get a copy of the struct before using it, the read barrier makes
- * sure the copy is consistent */
- irq_msg = remote->irq_msg;
- memory_load_barrier();
- if (irq_msg.valid)
- apic_send_irq(irq_msg);
-}
+ u16 *cmd = (u16 *)&ive->cspace[PCI_CFG_COMMAND/4];
+ struct pci_device *device = ive->device;
+ int err;
-static int ivshmem_register_mmio(struct pci_ivshmem_endpoint *ive,
- bool is_write, u32 offset, u32 *value)
-{
- /* read-only IVPosition */
- if (offset == IVSHMEM_REG_IVPOS && !is_write) {
- *value = ive->ivpos;
- return 1;
+ if ((val & PCI_CMD_MASTER) != (*cmd & PCI_CMD_MASTER)) {
+ *cmd = (*cmd & ~PCI_CMD_MASTER) | (val & PCI_CMD_MASTER);
+ err = ivshmem_update_msix(ive);
+ if (err)
+ return err;
}
- if (offset == IVSHMEM_REG_DBELL) {
- if (is_write) {
- ivshmem_write_doorbell(ive);
- } else {
- *value = 0;
+ if ((val & PCI_CMD_MEM) != (*cmd & PCI_CMD_MEM)) {
+ if (*cmd & PCI_CMD_MEM) {
+ mmio_region_unregister(device->cell, ive->bar0_address);
+ mmio_region_unregister(device->cell, ive->bar4_address);
}
- return 1;
+ if (val & PCI_CMD_MEM) {
+ ive->bar0_address = (*(u64 *)&device->bar[0]) & ~0xfL;
+ mmio_region_register(device->cell, ive->bar0_address,
+ IVSHMEM_BAR0_SIZE,
+ ivshmem_register_mmio, ive);
+
+ ive->bar4_address = (*(u64 *)&device->bar[4]) & ~0xfL;
+ mmio_region_register(device->cell, ive->bar4_address,
+ IVSHMEM_BAR4_SIZE,
+ ivshmem_msix_mmio, ive);
+ }
+ *cmd = (*cmd & ~PCI_CMD_MEM) | (val & PCI_CMD_MEM);
}
- panic_printk("FATAL: Invalid ivshmem register %s, number %02x\n",
- is_write ? "write" : "read", offset);
- return -1;
+
+ return 0;
}
static int ivshmem_write_msix_control(struct pci_ivshmem_endpoint *ive, u32 val)
{
struct pci_ivshmem_endpoint *remote = &iv->eps[(cellnum + 1) % 2];
struct pci_ivshmem_endpoint *ive = &iv->eps[cellnum];
+ u16 cmd = *(u16 *)&ive->cspace[PCI_CFG_COMMAND / 4];
+ if (cmd & PCI_CMD_MEM) {
+ mmio_region_unregister(this_cell(), ive->bar0_address);
+ mmio_region_unregister(this_cell(), ive->bar4_address);
+ }
ive->device->ivshmem_endpoint = NULL;
ive->device = NULL;
ive->remote = NULL;
remote->remote = NULL;
}
-/**
- * Handler for MMIO-accesses to this virtual PCI devices memory. Both for the
- * BAR containing the registers, and the MSI-X BAR.
- * @param cell The cell that issued the access.
- * @param is_write True if write access.
- * @param addr Address accessed.
- * @param value Pointer to value for reading/writing.
- *
- * @return 1 if handled successfully, 0 if unhandled, -1 on access error.
- *
- * @see pci_mmio_access_handler
- */
-int ivshmem_mmio_access_handler(const struct cell *cell, bool is_write,
- u64 addr, u32 *value)
-{
- struct pci_ivshmem_endpoint *ive;
- struct pci_device *device;
- u64 mem_start;
-
- for (device = cell->virtual_device_list; device;
- device = device->next_virtual_device) {
- ive = device->ivshmem_endpoint;
- if ((ive->cspace[PCI_CFG_COMMAND/4] & PCI_CMD_MEM) == 0)
- continue;
-
- /* BAR0: registers */
- mem_start = (*(u64 *)&device->bar[0]) & ~0xfL;
- if (addr >= mem_start &&
- addr <= (mem_start + IVSHMEM_BAR0_SIZE - 4))
- return ivshmem_register_mmio(ive, is_write,
- addr - mem_start,
- value);
-
- /* BAR4: MSI-X */
- mem_start = (*(u64 *)&device->bar[4]) & ~0xfL;
- if (addr >= mem_start &&
- addr <= (mem_start + IVSHMEM_BAR4_SIZE - 4))
- return ivshmem_msix_mmio(ive, is_write,
- addr - mem_start, value);
- }
-
- return 0;
-}
-
/**
* Handler for MMIO-write-accesses to PCI config space of this virtual device.
* @param device The device that access should be performed on.
*
* @see pci_cfg_write_moderate
*/
-enum pci_access pci_ivshmem_cfg_write(struct pci_device *dev, unsigned int row,
- u32 mask, u32 value)
+enum pci_access pci_ivshmem_cfg_write(struct pci_device *device,
+ unsigned int row, u32 mask, u32 value)
{
- struct pci_ivshmem_endpoint *ive = dev->ivshmem_endpoint;
+ struct pci_ivshmem_endpoint *ive = device->ivshmem_endpoint;
if (row >= ARRAY_SIZE(default_cspace))
return PCI_ACCESS_REJECT;
/**
* Handler for MMIO-read-accesses to PCI config space of this virtual device.
- * @param dev The device that access should be performed on.
+ * @param device The device that access should be performed on.
* @param address Config space address accessed.
* @param value Pointer to the return value.
*
*
* @see pci_cfg_read_moderate
*/
-enum pci_access pci_ivshmem_cfg_read(struct pci_device *dev, u16 address,
+enum pci_access pci_ivshmem_cfg_read(struct pci_device *device, u16 address,
u32 *value)
{
- struct pci_ivshmem_endpoint *ive = dev->ivshmem_endpoint;
+ struct pci_ivshmem_endpoint *ive = device->ivshmem_endpoint;
if (address < sizeof(default_cspace))
*value = ive->cspace[address / 4] >> ((address % 4) * 8);
/**
* Update cached MSI-X state of the given ivshmem device.
- * @param dev The device to be updated.
+ * @param device The device to be updated.
*
* @return 0 on success, negative error code otherwise.
*/
-int pci_ivshmem_update_msix(struct pci_device *dev)
+int pci_ivshmem_update_msix(struct pci_device *device)
{
- return ivshmem_update_msix(dev->ivshmem_endpoint);
+ return ivshmem_update_msix(device->ivshmem_endpoint);
}
/**
* Register a new ivshmem device.
* @param cell The cell the device should be attached to.
- * @param dev The device to be registered.
+ * @param device The device to be registered.
*
* @return 0 on success, negative error code otherwise.
*/
-int pci_ivshmem_init(struct cell *cell, struct pci_device *dev)
+int pci_ivshmem_init(struct cell *cell, struct pci_device *device)
{
const struct jailhouse_memory *mem, *mem0;
struct pci_ivshmem_data **ivp;
struct pci_device *dev0;
- if (dev->info->num_msix_vectors != 1)
+ if (device->info->num_msix_vectors != 1)
return trace_error(-EINVAL);
- if (dev->info->shmem_region >= cell->config->num_memory_regions)
+ if (device->info->shmem_region >= cell->config->num_memory_regions)
return trace_error(-EINVAL);
mem = jailhouse_cell_mem_regions(cell->config)
- + dev->info->shmem_region;
- ivp = ivshmem_find(dev, NULL);
+ + device->info->shmem_region;
+ ivp = ivshmem_find(device, NULL);
if (ivp) {
dev0 = (*ivp)->eps[0].device;
mem0 = jailhouse_cell_mem_regions(dev0->cell->config) +
(mem0->size == mem->size)) {
if ((*ivp)->eps[1].device)
return trace_error(-EBUSY);
- ivshmem_connect_cell(*ivp, dev, mem, 1);
+ ivshmem_connect_cell(*ivp, device, mem, 1);
printk("Virtual PCI connection established "
"\"%s\" <--> \"%s\"\n",
cell->config->name, dev0->cell->config->name);
*ivp = page_alloc(&mem_pool, 1);
if (!(*ivp))
return -ENOMEM;
- ivshmem_connect_cell(*ivp, dev, mem, 0);
+ ivshmem_connect_cell(*ivp, device, mem, 0);
connected:
- dev->cell = cell;
printk("Adding virtual PCI device %02x:%02x.%x to cell \"%s\"\n",
- PCI_BDF_PARAMS(dev->info->bdf), cell->config->name);
+ PCI_BDF_PARAMS(device->info->bdf), cell->config->name);
return 0;
}
/**
* Unregister a ivshmem device, typically when the corresponding cell exits.
- * @param dev The device to be stopped.
+ * @param device The device to be stopped.
*
*/
-void pci_ivshmem_exit(struct pci_device *dev)
+void pci_ivshmem_exit(struct pci_device *device)
{
struct pci_ivshmem_data **ivp, *iv;
int cellnum;
- ivp = ivshmem_find(dev, &cellnum);
+ ivp = ivshmem_find(device, &cellnum);
if (!ivp || !(*ivp))
return;