2018-08-08 09:36:43 +08:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011 NetApp, Inc.
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Virtual PCI device related operations (read/write, etc) */
|
|
|
|
|
2019-02-25 16:44:07 +08:00
|
|
|
#include <vm.h>
|
|
|
|
#include <logmsg.h>
|
2018-08-08 09:36:43 +08:00
|
|
|
#include "pci_priv.h"
|
|
|
|
|
2019-03-07 06:21:36 +08:00
|
|
|
static inline bool is_hostbridge(const struct pci_vdev *vdev)
|
|
|
|
{
|
|
|
|
return (vdev->vbdf.value == 0U);
|
|
|
|
}
|
2019-02-26 05:34:02 +08:00
|
|
|
|
2019-01-15 15:28:40 +08:00
|
|
|
static inline bool is_valid_bar_type(const struct pci_bar *bar)
|
|
|
|
{
|
|
|
|
return (bar->type == PCIBAR_MEM32) || (bar->type == PCIBAR_MEM64);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool is_valid_bar_size(const struct pci_bar *bar)
|
|
|
|
{
|
|
|
|
return (bar->size > 0UL) && (bar->size <= 0xffffffffU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only MMIO is supported and bar size cannot be greater than 4GB */
|
|
|
|
static inline bool is_valid_bar(const struct pci_bar *bar)
|
|
|
|
{
|
|
|
|
return (is_valid_bar_type(bar) && is_valid_bar_size(bar));
|
|
|
|
}
|
|
|
|
|
2019-02-16 03:53:14 +08:00
|
|
|
static void partition_mode_pdev_init(struct pci_vdev *vdev, union pci_bdf pbdf)
|
2019-01-15 15:28:40 +08:00
|
|
|
{
|
2019-03-07 06:21:36 +08:00
|
|
|
struct pci_pdev *pdev;
|
2019-01-15 15:28:40 +08:00
|
|
|
uint32_t idx;
|
|
|
|
struct pci_bar *pbar, *vbar;
|
|
|
|
|
2019-03-07 06:21:36 +08:00
|
|
|
pdev = find_pci_pdev(pbdf);
|
|
|
|
if (pdev != NULL) {
|
|
|
|
vdev->pdev = pdev;
|
2019-01-15 15:28:40 +08:00
|
|
|
|
|
|
|
/* Sanity checking for vbar */
|
|
|
|
for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) {
|
2019-01-25 10:52:34 +08:00
|
|
|
pbar = &vdev->pdev->bar[idx];
|
2019-01-15 15:28:40 +08:00
|
|
|
vbar = &vdev->bar[idx];
|
|
|
|
|
|
|
|
if (is_valid_bar(pbar)) {
|
|
|
|
vbar->size = (pbar->size < 0x1000U) ? 0x1000U : pbar->size;
|
|
|
|
vbar->type = PCIBAR_MEM32;
|
|
|
|
} else {
|
|
|
|
/* Mark this vbar as invalid */
|
|
|
|
vbar->size = 0UL;
|
|
|
|
vbar->type = PCIBAR_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:34:02 +08:00
|
|
|
/**
|
|
|
|
* @pre vm != NULL
|
|
|
|
* @pre vm->vpci->pci_vdev_cnt <= CONFIG_MAX_PCI_DEV_NUM
|
|
|
|
* @pre vm_config != NULL
|
|
|
|
* @pre vdev != NULL
|
|
|
|
* @pre ptdev_config != NULL
|
|
|
|
*/
|
2019-01-11 01:15:02 +08:00
|
|
|
static int32_t partition_mode_vpci_init(const struct acrn_vm *vm)
|
2018-08-08 09:36:43 +08:00
|
|
|
{
|
2019-02-26 05:34:02 +08:00
|
|
|
struct acrn_vpci *vpci = (struct acrn_vpci *)&(vm->vpci);
|
2018-08-08 09:36:43 +08:00
|
|
|
struct pci_vdev *vdev;
|
2019-01-21 02:18:05 +08:00
|
|
|
struct acrn_vm_config *vm_config = get_vm_config(vm->vm_id);
|
2019-02-02 16:52:05 +08:00
|
|
|
struct acrn_vm_pci_ptdev_config *ptdev_config;
|
2019-02-26 05:34:02 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
vpci->pci_vdev_cnt = vm_config->pci_ptdev_num;
|
2018-08-08 09:36:43 +08:00
|
|
|
|
2019-02-26 05:34:02 +08:00
|
|
|
for (i = 0U; i < vpci->pci_vdev_cnt; i++) {
|
|
|
|
vdev = &vpci->pci_vdevs[i];
|
2018-10-09 08:08:04 +08:00
|
|
|
vdev->vpci = vpci;
|
2019-02-26 05:34:02 +08:00
|
|
|
ptdev_config = &vm_config->pci_ptdevs[i];
|
2019-02-02 16:52:05 +08:00
|
|
|
vdev->vbdf.value = ptdev_config->vbdf.value;
|
2018-08-08 09:36:43 +08:00
|
|
|
|
2019-03-07 06:21:36 +08:00
|
|
|
if (is_hostbridge(vdev)) {
|
|
|
|
vdev->ops = &pci_ops_vdev_hostbridge;
|
|
|
|
} else {
|
2019-02-16 03:53:14 +08:00
|
|
|
partition_mode_pdev_init(vdev, ptdev_config->pbdf);
|
2019-01-18 01:34:26 +08:00
|
|
|
vdev->ops = &pci_ops_vdev_pt;
|
2019-01-15 15:28:40 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 01:34:26 +08:00
|
|
|
if (vdev->ops->init != NULL) {
|
2018-11-07 14:09:15 +08:00
|
|
|
if (vdev->ops->init(vdev) != 0) {
|
2019-01-25 10:52:34 +08:00
|
|
|
pr_err("%s() failed at PCI device (vbdf %x)!", __func__,
|
2018-10-09 08:08:04 +08:00
|
|
|
vdev->vbdf);
|
|
|
|
}
|
2018-08-08 09:36:43 +08:00
|
|
|
}
|
2018-10-09 08:08:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:34:02 +08:00
|
|
|
/**
|
|
|
|
* @pre vdev != NULL
|
|
|
|
*/
|
2019-01-11 01:15:02 +08:00
|
|
|
static void partition_mode_vpci_deinit(const struct acrn_vm *vm)
|
2018-10-09 08:08:04 +08:00
|
|
|
{
|
|
|
|
struct pci_vdev *vdev;
|
2019-02-26 05:34:02 +08:00
|
|
|
uint32_t i;
|
2018-10-09 08:08:04 +08:00
|
|
|
|
2019-02-26 05:34:02 +08:00
|
|
|
for (i = 0U; i < vm->vpci.pci_vdev_cnt; i++) {
|
|
|
|
vdev = (struct pci_vdev *) &(vm->vpci.pci_vdevs[i]);
|
2018-10-09 08:08:04 +08:00
|
|
|
if ((vdev->ops != NULL) && (vdev->ops->deinit != NULL)) {
|
2018-11-07 14:09:15 +08:00
|
|
|
if (vdev->ops->deinit(vdev) != 0) {
|
2018-10-09 08:08:04 +08:00
|
|
|
pr_err("vdev->ops->deinit failed!");
|
|
|
|
}
|
2018-08-08 09:36:43 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-09 08:08:04 +08:00
|
|
|
}
|
2018-08-08 09:36:43 +08:00
|
|
|
|
2018-12-14 14:50:56 +08:00
|
|
|
static void partition_mode_cfgread(struct acrn_vpci *vpci, union pci_bdf vbdf,
|
2018-10-09 08:08:04 +08:00
|
|
|
uint32_t offset, uint32_t bytes, uint32_t *val)
|
|
|
|
{
|
2019-02-16 04:58:26 +08:00
|
|
|
struct pci_vdev *vdev = pci_find_vdev_by_vbdf(vpci, vbdf);
|
2019-01-15 15:28:40 +08:00
|
|
|
|
2018-10-09 08:08:04 +08:00
|
|
|
if ((vdev != NULL) && (vdev->ops != NULL)
|
|
|
|
&& (vdev->ops->cfgread != NULL)) {
|
|
|
|
(void)vdev->ops->cfgread(vdev, offset, bytes, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:50:56 +08:00
|
|
|
static void partition_mode_cfgwrite(struct acrn_vpci *vpci, union pci_bdf vbdf,
|
2018-10-09 08:08:04 +08:00
|
|
|
uint32_t offset, uint32_t bytes, uint32_t val)
|
|
|
|
{
|
2019-02-16 04:58:26 +08:00
|
|
|
struct pci_vdev *vdev = pci_find_vdev_by_vbdf(vpci, vbdf);
|
2019-01-15 15:28:40 +08:00
|
|
|
|
2018-10-09 08:08:04 +08:00
|
|
|
if ((vdev != NULL) && (vdev->ops != NULL)
|
|
|
|
&& (vdev->ops->cfgwrite != NULL)) {
|
|
|
|
(void)vdev->ops->cfgwrite(vdev, offset, bytes, val);
|
2018-08-08 09:36:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:45:31 +08:00
|
|
|
const struct vpci_ops partition_mode_vpci_ops = {
|
2018-10-09 08:08:04 +08:00
|
|
|
.init = partition_mode_vpci_init,
|
|
|
|
.deinit = partition_mode_vpci_deinit,
|
|
|
|
.cfgread = partition_mode_cfgread,
|
|
|
|
.cfgwrite = partition_mode_cfgwrite,
|
|
|
|
};
|