2012-06-27 08:50:44 +04:00
|
|
|
/*
|
|
|
|
* QEMU sPAPR IOMMU (TCE) code
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-01-26 21:16:58 +03:00
|
|
|
#include "qemu/osdep.h"
|
2016-06-01 11:57:33 +03:00
|
|
|
#include "qemu/error-report.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/hw.h"
|
2015-12-15 15:16:16 +03:00
|
|
|
#include "qemu/log.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/kvm.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/qdev.h"
|
2012-06-27 08:50:44 +04:00
|
|
|
#include "kvm_ppc.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/dma.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "exec/address-spaces.h"
|
2013-08-29 12:05:00 +04:00
|
|
|
#include "trace.h"
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/ppc/spapr.h"
|
2015-01-29 08:04:58 +03:00
|
|
|
#include "hw/ppc/spapr_vio.h"
|
2012-06-27 08:50:44 +04:00
|
|
|
|
|
|
|
#include <libfdt.h>
|
|
|
|
|
|
|
|
enum sPAPRTCEAccess {
|
|
|
|
SPAPR_TCE_FAULT = 0,
|
|
|
|
SPAPR_TCE_RO = 1,
|
|
|
|
SPAPR_TCE_WO = 2,
|
|
|
|
SPAPR_TCE_RW = 3,
|
|
|
|
};
|
|
|
|
|
2014-05-27 09:36:36 +04:00
|
|
|
#define IOMMU_PAGE_SIZE(shift) (1ULL << (shift))
|
|
|
|
#define IOMMU_PAGE_MASK(shift) (~(IOMMU_PAGE_SIZE(shift) - 1))
|
|
|
|
|
2014-05-03 00:34:40 +04:00
|
|
|
static QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2015-05-07 08:33:38 +03:00
|
|
|
sPAPRTCETable *spapr_tce_find_by_liobn(target_ulong liobn)
|
2012-06-27 08:50:44 +04:00
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet;
|
|
|
|
|
2013-04-29 22:33:51 +04:00
|
|
|
if (liobn & 0xFFFFFFFF00000000ULL) {
|
|
|
|
hcall_dprintf("Request for out-of-bounds LIOBN 0x" TARGET_FMT_lx "\n",
|
|
|
|
liobn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-27 08:50:44 +04:00
|
|
|
QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
|
2015-05-07 08:33:38 +03:00
|
|
|
if (tcet->liobn == (uint32_t)liobn) {
|
2012-06-27 08:50:44 +04:00
|
|
|
return tcet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-02 09:23:12 +03:00
|
|
|
static IOMMUAccessFlags spapr_tce_iommu_access_flags(uint64_t tce)
|
|
|
|
{
|
|
|
|
switch (tce & SPAPR_TCE_RW) {
|
|
|
|
case SPAPR_TCE_FAULT:
|
|
|
|
return IOMMU_NONE;
|
|
|
|
case SPAPR_TCE_RO:
|
|
|
|
return IOMMU_RO;
|
|
|
|
case SPAPR_TCE_WO:
|
|
|
|
return IOMMU_WO;
|
|
|
|
default: /* SPAPR_TCE_RW */
|
|
|
|
return IOMMU_RW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 09:52:19 +03:00
|
|
|
static uint64_t *spapr_tce_alloc_table(uint32_t liobn,
|
|
|
|
uint32_t page_shift,
|
2017-03-10 04:41:13 +03:00
|
|
|
uint64_t bus_offset,
|
2016-05-04 09:52:19 +03:00
|
|
|
uint32_t nb_table,
|
|
|
|
int *fd,
|
|
|
|
bool need_vfio)
|
|
|
|
{
|
|
|
|
uint64_t *table = NULL;
|
|
|
|
|
2017-03-10 04:41:13 +03:00
|
|
|
if (kvm_enabled()) {
|
|
|
|
table = kvmppc_create_spapr_tce(liobn, page_shift, bus_offset, nb_table,
|
|
|
|
fd, need_vfio);
|
2016-05-04 09:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!table) {
|
|
|
|
*fd = -1;
|
|
|
|
table = g_malloc0(nb_table * sizeof(uint64_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_spapr_iommu_new_table(liobn, table, *fd);
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void spapr_tce_free_table(uint64_t *table, int fd, uint32_t nb_table)
|
|
|
|
{
|
|
|
|
if (!kvm_enabled() ||
|
|
|
|
(kvmppc_remove_spapr_tce(table, fd, nb_table) != 0)) {
|
|
|
|
g_free(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 14:09:14 +03:00
|
|
|
/* Called from RCU critical section */
|
2017-07-11 06:56:19 +03:00
|
|
|
static IOMMUTLBEntry spapr_tce_translate_iommu(IOMMUMemoryRegion *iommu,
|
|
|
|
hwaddr addr,
|
2017-05-19 06:19:40 +03:00
|
|
|
IOMMUAccessFlags flag)
|
2012-06-27 08:50:44 +04:00
|
|
|
{
|
2013-04-11 14:35:33 +04:00
|
|
|
sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
|
2012-06-27 08:50:44 +04:00
|
|
|
uint64_t tce;
|
2013-08-29 12:05:00 +04:00
|
|
|
IOMMUTLBEntry ret = {
|
|
|
|
.target_as = &address_space_memory,
|
|
|
|
.iova = 0,
|
|
|
|
.translated_addr = 0,
|
|
|
|
.addr_mask = ~(hwaddr)0,
|
|
|
|
.perm = IOMMU_NONE,
|
|
|
|
};
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2015-01-29 08:04:58 +03:00
|
|
|
if ((addr >> tcet->page_shift) < tcet->nb_table) {
|
2013-08-29 12:05:00 +04:00
|
|
|
/* Check if we are in bound */
|
2014-05-27 09:36:36 +04:00
|
|
|
hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
|
|
|
|
|
|
|
|
tce = tcet->table[addr >> tcet->page_shift];
|
|
|
|
ret.iova = addr & page_mask;
|
|
|
|
ret.translated_addr = tce & page_mask;
|
|
|
|
ret.addr_mask = ~page_mask;
|
2015-07-02 09:23:12 +03:00
|
|
|
ret.perm = spapr_tce_iommu_access_flags(tce);
|
2012-06-27 08:50:44 +04:00
|
|
|
}
|
2013-08-29 12:05:00 +04:00
|
|
|
trace_spapr_iommu_xlate(tcet->liobn, addr, ret.iova, ret.perm,
|
|
|
|
ret.addr_mask);
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2013-08-29 12:05:00 +04:00
|
|
|
return ret;
|
2013-04-16 17:05:06 +04:00
|
|
|
}
|
|
|
|
|
2016-06-01 11:57:34 +03:00
|
|
|
static void spapr_tce_table_pre_save(void *opaque)
|
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
|
|
|
|
|
|
|
|
tcet->mig_table = tcet->table;
|
|
|
|
tcet->mig_nb_table = tcet->nb_table;
|
|
|
|
|
|
|
|
trace_spapr_iommu_pre_save(tcet->liobn, tcet->mig_nb_table,
|
|
|
|
tcet->bus_offset, tcet->page_shift);
|
|
|
|
}
|
|
|
|
|
2017-07-11 06:56:19 +03:00
|
|
|
static uint64_t spapr_tce_get_min_page_size(IOMMUMemoryRegion *iommu)
|
2016-06-21 04:14:01 +03:00
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
|
|
|
|
|
|
|
|
return 1ULL << tcet->page_shift;
|
|
|
|
}
|
|
|
|
|
2017-07-11 06:56:19 +03:00
|
|
|
static void spapr_tce_notify_flag_changed(IOMMUMemoryRegion *iommu,
|
2016-09-23 08:02:27 +03:00
|
|
|
IOMMUNotifierFlag old,
|
|
|
|
IOMMUNotifierFlag new)
|
2016-07-04 06:33:03 +03:00
|
|
|
{
|
2016-09-23 08:02:27 +03:00
|
|
|
struct sPAPRTCETable *tbl = container_of(iommu, sPAPRTCETable, iommu);
|
2016-07-04 06:33:03 +03:00
|
|
|
|
2016-09-23 08:02:27 +03:00
|
|
|
if (old == IOMMU_NOTIFIER_NONE && new != IOMMU_NOTIFIER_NONE) {
|
|
|
|
spapr_tce_set_need_vfio(tbl, true);
|
|
|
|
} else if (old != IOMMU_NOTIFIER_NONE && new == IOMMU_NOTIFIER_NONE) {
|
|
|
|
spapr_tce_set_need_vfio(tbl, false);
|
|
|
|
}
|
2016-07-04 06:33:03 +03:00
|
|
|
}
|
|
|
|
|
2015-01-29 08:04:58 +03:00
|
|
|
static int spapr_tce_table_post_load(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
|
2016-06-01 11:57:34 +03:00
|
|
|
uint32_t old_nb_table = tcet->nb_table;
|
|
|
|
uint64_t old_bus_offset = tcet->bus_offset;
|
|
|
|
uint32_t old_page_shift = tcet->page_shift;
|
2015-01-29 08:04:58 +03:00
|
|
|
|
|
|
|
if (tcet->vdev) {
|
|
|
|
spapr_vio_set_bypass(tcet->vdev, tcet->bypass);
|
|
|
|
}
|
|
|
|
|
2016-06-01 11:57:34 +03:00
|
|
|
if (tcet->mig_nb_table != tcet->nb_table) {
|
|
|
|
spapr_tce_table_disable(tcet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tcet->mig_nb_table) {
|
|
|
|
if (!tcet->nb_table) {
|
|
|
|
spapr_tce_table_enable(tcet, old_page_shift, old_bus_offset,
|
|
|
|
tcet->mig_nb_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tcet->table, tcet->mig_table,
|
|
|
|
tcet->nb_table * sizeof(tcet->table[0]));
|
|
|
|
|
|
|
|
free(tcet->mig_table);
|
|
|
|
tcet->mig_table = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_spapr_iommu_post_load(tcet->liobn, old_nb_table, tcet->nb_table,
|
|
|
|
tcet->bus_offset, tcet->page_shift);
|
|
|
|
|
2015-01-29 08:04:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-01 11:57:34 +03:00
|
|
|
static bool spapr_tce_table_ex_needed(void *opaque)
|
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet = opaque;
|
|
|
|
|
|
|
|
return tcet->bus_offset || tcet->page_shift != 0xC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_spapr_tce_table_ex = {
|
|
|
|
.name = "spapr_iommu_ex",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.needed = spapr_tce_table_ex_needed,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT64(bus_offset, sPAPRTCETable),
|
|
|
|
VMSTATE_UINT32(page_shift, sPAPRTCETable),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
static const VMStateDescription vmstate_spapr_tce_table = {
|
|
|
|
.name = "spapr_iommu",
|
2014-05-27 09:36:35 +04:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2016-06-01 11:57:34 +03:00
|
|
|
.pre_save = spapr_tce_table_pre_save,
|
2015-01-29 08:04:58 +03:00
|
|
|
.post_load = spapr_tce_table_post_load,
|
2014-05-27 09:36:35 +04:00
|
|
|
.fields = (VMStateField []) {
|
2013-07-18 23:32:58 +04:00
|
|
|
/* Sanity check */
|
2017-06-23 17:48:23 +03:00
|
|
|
VMSTATE_UINT32_EQUAL(liobn, sPAPRTCETable, NULL),
|
2013-07-18 23:32:58 +04:00
|
|
|
|
|
|
|
/* IOMMU state */
|
2016-06-01 11:57:34 +03:00
|
|
|
VMSTATE_UINT32(mig_nb_table, sPAPRTCETable),
|
2013-07-18 23:32:58 +04:00
|
|
|
VMSTATE_BOOL(bypass, sPAPRTCETable),
|
2016-06-01 11:57:34 +03:00
|
|
|
VMSTATE_VARRAY_UINT32_ALLOC(mig_table, sPAPRTCETable, mig_nb_table, 0,
|
|
|
|
vmstate_info_uint64, uint64_t),
|
2013-07-18 23:32:58 +04:00
|
|
|
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
2016-06-01 11:57:34 +03:00
|
|
|
.subsections = (const VMStateDescription*[]) {
|
|
|
|
&vmstate_spapr_tce_table_ex,
|
|
|
|
NULL
|
|
|
|
}
|
2013-07-18 23:32:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int spapr_tce_table_realize(DeviceState *dev)
|
2012-06-27 08:50:44 +04:00
|
|
|
{
|
2013-07-18 23:32:58 +04:00
|
|
|
sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
|
2016-06-01 11:57:35 +03:00
|
|
|
Object *tcetobj = OBJECT(tcet);
|
|
|
|
char tmp[32];
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2016-05-04 09:52:19 +03:00
|
|
|
tcet->fd = -1;
|
2016-06-01 11:57:33 +03:00
|
|
|
tcet->need_vfio = false;
|
2016-06-01 11:57:35 +03:00
|
|
|
snprintf(tmp, sizeof(tmp), "tce-root-%x", tcet->liobn);
|
|
|
|
memory_region_init(&tcet->root, tcetobj, tmp, UINT64_MAX);
|
|
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "tce-iommu-%x", tcet->liobn);
|
2017-07-11 06:56:20 +03:00
|
|
|
memory_region_init_iommu(&tcet->iommu, sizeof(tcet->iommu),
|
|
|
|
TYPE_SPAPR_IOMMU_MEMORY_REGION,
|
|
|
|
tcetobj, tmp, 0);
|
2013-04-11 14:35:33 +04:00
|
|
|
|
2012-06-27 08:50:44 +04:00
|
|
|
QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
|
|
|
|
|
2014-05-12 12:46:32 +04:00
|
|
|
vmstate_register(DEVICE(tcet), tcet->liobn, &vmstate_spapr_tce_table,
|
|
|
|
tcet);
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-01 03:46:10 +03:00
|
|
|
void spapr_tce_set_need_vfio(sPAPRTCETable *tcet, bool need_vfio)
|
|
|
|
{
|
|
|
|
size_t table_size = tcet->nb_table * sizeof(uint64_t);
|
|
|
|
void *newtable;
|
|
|
|
|
|
|
|
if (need_vfio == tcet->need_vfio) {
|
|
|
|
/* Nothing to do */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!need_vfio) {
|
|
|
|
/* FIXME: We don't support transition back to KVM accelerated
|
|
|
|
* TCEs yet */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcet->need_vfio = true;
|
|
|
|
|
|
|
|
if (tcet->fd < 0) {
|
|
|
|
/* Table is already in userspace, nothing to be do */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newtable = g_malloc(table_size);
|
|
|
|
memcpy(newtable, tcet->table, table_size);
|
|
|
|
|
|
|
|
kvmppc_remove_spapr_tce(tcet->table, tcet->fd, tcet->nb_table);
|
|
|
|
|
|
|
|
tcet->fd = -1;
|
|
|
|
tcet->table = newtable;
|
|
|
|
}
|
|
|
|
|
2016-06-01 11:57:33 +03:00
|
|
|
sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn)
|
2013-07-18 23:32:58 +04:00
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet;
|
2016-06-01 11:57:33 +03:00
|
|
|
char tmp[32];
|
2013-07-18 23:32:58 +04:00
|
|
|
|
|
|
|
if (spapr_tce_find_by_liobn(liobn)) {
|
2016-08-02 20:38:00 +03:00
|
|
|
error_report("Attempted to create TCE table with duplicate"
|
|
|
|
" LIOBN 0x%x", liobn);
|
2013-07-18 23:32:58 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcet = SPAPR_TCE_TABLE(object_new(TYPE_SPAPR_TCE_TABLE));
|
|
|
|
tcet->liobn = liobn;
|
|
|
|
|
2015-05-07 08:33:37 +03:00
|
|
|
snprintf(tmp, sizeof(tmp), "tce-table-%x", liobn);
|
|
|
|
object_property_add_child(OBJECT(owner), tmp, OBJECT(tcet), NULL);
|
2013-07-18 23:32:58 +04:00
|
|
|
|
2014-05-27 09:36:34 +04:00
|
|
|
object_property_set_bool(OBJECT(tcet), true, "realized", NULL);
|
2013-07-18 23:32:58 +04:00
|
|
|
|
2013-04-10 19:30:48 +04:00
|
|
|
return tcet;
|
2012-06-27 08:50:44 +04:00
|
|
|
}
|
|
|
|
|
2016-06-01 11:57:33 +03:00
|
|
|
void spapr_tce_table_enable(sPAPRTCETable *tcet,
|
|
|
|
uint32_t page_shift, uint64_t bus_offset,
|
|
|
|
uint32_t nb_table)
|
|
|
|
{
|
|
|
|
if (tcet->nb_table) {
|
2017-07-12 16:57:41 +03:00
|
|
|
warn_report("trying to enable already enabled TCE table");
|
2016-06-01 11:57:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcet->bus_offset = bus_offset;
|
|
|
|
tcet->page_shift = page_shift;
|
|
|
|
tcet->nb_table = nb_table;
|
|
|
|
tcet->table = spapr_tce_alloc_table(tcet->liobn,
|
|
|
|
tcet->page_shift,
|
2017-03-10 04:41:13 +03:00
|
|
|
tcet->bus_offset,
|
2016-06-01 11:57:33 +03:00
|
|
|
tcet->nb_table,
|
|
|
|
&tcet->fd,
|
|
|
|
tcet->need_vfio);
|
|
|
|
|
2017-07-11 06:56:19 +03:00
|
|
|
memory_region_set_size(MEMORY_REGION(&tcet->iommu),
|
2016-06-01 11:57:33 +03:00
|
|
|
(uint64_t)tcet->nb_table << tcet->page_shift);
|
2017-07-11 06:56:19 +03:00
|
|
|
memory_region_add_subregion(&tcet->root, tcet->bus_offset,
|
|
|
|
MEMORY_REGION(&tcet->iommu));
|
2016-06-01 11:57:33 +03:00
|
|
|
}
|
|
|
|
|
2016-06-01 11:57:34 +03:00
|
|
|
void spapr_tce_table_disable(sPAPRTCETable *tcet)
|
2016-06-01 11:57:33 +03:00
|
|
|
{
|
|
|
|
if (!tcet->nb_table) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 06:56:19 +03:00
|
|
|
memory_region_del_subregion(&tcet->root, MEMORY_REGION(&tcet->iommu));
|
|
|
|
memory_region_set_size(MEMORY_REGION(&tcet->iommu), 0);
|
2016-06-01 11:57:33 +03:00
|
|
|
|
|
|
|
spapr_tce_free_table(tcet->table, tcet->fd, tcet->nb_table);
|
|
|
|
tcet->fd = -1;
|
|
|
|
tcet->table = NULL;
|
|
|
|
tcet->bus_offset = 0;
|
|
|
|
tcet->page_shift = 0;
|
|
|
|
tcet->nb_table = 0;
|
|
|
|
}
|
|
|
|
|
2014-12-08 05:48:02 +03:00
|
|
|
static void spapr_tce_table_unrealize(DeviceState *dev, Error **errp)
|
2012-06-27 08:50:44 +04:00
|
|
|
{
|
2014-12-08 05:48:02 +03:00
|
|
|
sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
|
2013-07-18 23:32:58 +04:00
|
|
|
|
2013-04-10 19:30:48 +04:00
|
|
|
QLIST_REMOVE(tcet, list);
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2016-06-01 11:57:33 +03:00
|
|
|
spapr_tce_table_disable(tcet);
|
2012-06-27 08:50:44 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 14:35:33 +04:00
|
|
|
MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
|
|
|
|
{
|
2016-06-01 11:57:35 +03:00
|
|
|
return &tcet->root;
|
2013-04-11 14:35:33 +04:00
|
|
|
}
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
static void spapr_tce_reset(DeviceState *dev)
|
2012-09-12 20:57:14 +04:00
|
|
|
{
|
2013-07-18 23:32:58 +04:00
|
|
|
sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
|
2014-05-27 09:36:35 +04:00
|
|
|
size_t table_size = tcet->nb_table * sizeof(uint64_t);
|
2012-09-12 20:57:14 +04:00
|
|
|
|
2016-08-08 03:06:25 +03:00
|
|
|
if (tcet->nb_table) {
|
|
|
|
memset(tcet->table, 0, table_size);
|
|
|
|
}
|
2012-09-12 20:57:14 +04:00
|
|
|
}
|
|
|
|
|
2012-06-27 08:50:46 +04:00
|
|
|
static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
|
|
|
|
target_ulong tce)
|
|
|
|
{
|
2013-04-11 14:35:33 +04:00
|
|
|
IOMMUTLBEntry entry;
|
2014-05-27 09:36:36 +04:00
|
|
|
hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
|
2014-05-27 09:36:37 +04:00
|
|
|
unsigned long index = (ioba - tcet->bus_offset) >> tcet->page_shift;
|
2012-06-27 08:50:46 +04:00
|
|
|
|
2014-05-27 09:36:37 +04:00
|
|
|
if (index >= tcet->nb_table) {
|
2013-04-29 22:33:52 +04:00
|
|
|
hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
|
2012-06-27 08:50:46 +04:00
|
|
|
TARGET_FMT_lx "\n", ioba);
|
|
|
|
return H_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:36:37 +04:00
|
|
|
tcet->table[index] = tce;
|
2012-06-27 08:50:46 +04:00
|
|
|
|
2013-04-11 14:35:33 +04:00
|
|
|
entry.target_as = &address_space_memory,
|
2016-05-26 18:43:23 +03:00
|
|
|
entry.iova = (ioba - tcet->bus_offset) & page_mask;
|
2014-05-27 09:36:36 +04:00
|
|
|
entry.translated_addr = tce & page_mask;
|
|
|
|
entry.addr_mask = ~page_mask;
|
2015-07-02 09:23:12 +03:00
|
|
|
entry.perm = spapr_tce_iommu_access_flags(tce);
|
2013-04-11 14:35:33 +04:00
|
|
|
memory_region_notify_iommu(&tcet->iommu, entry);
|
|
|
|
|
2012-06-27 08:50:46 +04:00
|
|
|
return H_SUCCESS;
|
|
|
|
}
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2014-05-27 09:36:30 +04:00
|
|
|
static target_ulong h_put_tce_indirect(PowerPCCPU *cpu,
|
2015-07-02 09:23:04 +03:00
|
|
|
sPAPRMachineState *spapr,
|
2014-05-27 09:36:30 +04:00
|
|
|
target_ulong opcode, target_ulong *args)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
target_ulong liobn = args[0];
|
|
|
|
target_ulong ioba = args[1];
|
|
|
|
target_ulong ioba1 = ioba;
|
|
|
|
target_ulong tce_list = args[2];
|
|
|
|
target_ulong npages = args[3];
|
2015-05-07 08:33:29 +03:00
|
|
|
target_ulong ret = H_PARAMETER, tce = 0;
|
2014-05-27 09:36:30 +04:00
|
|
|
sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
|
|
|
|
CPUState *cs = CPU(cpu);
|
2014-05-27 09:36:36 +04:00
|
|
|
hwaddr page_mask, page_size;
|
2014-05-27 09:36:30 +04:00
|
|
|
|
|
|
|
if (!tcet) {
|
|
|
|
return H_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:36:36 +04:00
|
|
|
if ((npages > 512) || (tce_list & SPAPR_TCE_PAGE_MASK)) {
|
2014-05-27 09:36:30 +04:00
|
|
|
return H_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:36:36 +04:00
|
|
|
page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
|
|
|
|
page_size = IOMMU_PAGE_SIZE(tcet->page_shift);
|
|
|
|
ioba &= page_mask;
|
|
|
|
|
|
|
|
for (i = 0; i < npages; ++i, ioba += page_size) {
|
2015-07-02 09:23:11 +03:00
|
|
|
tce = ldq_be_phys(cs->as, tce_list + i * sizeof(target_ulong));
|
2014-05-27 09:36:30 +04:00
|
|
|
|
|
|
|
ret = put_tce_emu(tcet, ioba, tce);
|
|
|
|
if (ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trace last successful or the first problematic entry */
|
|
|
|
i = i ? (i - 1) : 0;
|
2015-05-07 08:33:33 +03:00
|
|
|
if (SPAPR_IS_PCI_LIOBN(liobn)) {
|
|
|
|
trace_spapr_iommu_pci_indirect(liobn, ioba1, tce_list, i, tce, ret);
|
|
|
|
} else {
|
|
|
|
trace_spapr_iommu_indirect(liobn, ioba1, tce_list, i, tce, ret);
|
|
|
|
}
|
2014-05-27 09:36:30 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-02 09:23:04 +03:00
|
|
|
static target_ulong h_stuff_tce(PowerPCCPU *cpu, sPAPRMachineState *spapr,
|
2014-05-27 09:36:30 +04:00
|
|
|
target_ulong opcode, target_ulong *args)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
target_ulong liobn = args[0];
|
|
|
|
target_ulong ioba = args[1];
|
|
|
|
target_ulong tce_value = args[2];
|
|
|
|
target_ulong npages = args[3];
|
|
|
|
target_ulong ret = H_PARAMETER;
|
|
|
|
sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
|
2014-05-27 09:36:36 +04:00
|
|
|
hwaddr page_mask, page_size;
|
2014-05-27 09:36:30 +04:00
|
|
|
|
|
|
|
if (!tcet) {
|
|
|
|
return H_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (npages > tcet->nb_table) {
|
|
|
|
return H_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:36:36 +04:00
|
|
|
page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
|
|
|
|
page_size = IOMMU_PAGE_SIZE(tcet->page_shift);
|
|
|
|
ioba &= page_mask;
|
2014-05-27 09:36:30 +04:00
|
|
|
|
2014-05-27 09:36:36 +04:00
|
|
|
for (i = 0; i < npages; ++i, ioba += page_size) {
|
2014-05-27 09:36:30 +04:00
|
|
|
ret = put_tce_emu(tcet, ioba, tce_value);
|
|
|
|
if (ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 08:33:33 +03:00
|
|
|
if (SPAPR_IS_PCI_LIOBN(liobn)) {
|
|
|
|
trace_spapr_iommu_pci_stuff(liobn, ioba, tce_value, npages, ret);
|
|
|
|
} else {
|
|
|
|
trace_spapr_iommu_stuff(liobn, ioba, tce_value, npages, ret);
|
|
|
|
}
|
2014-05-27 09:36:30 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-02 09:23:04 +03:00
|
|
|
static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPRMachineState *spapr,
|
2012-06-27 08:50:44 +04:00
|
|
|
target_ulong opcode, target_ulong *args)
|
|
|
|
{
|
|
|
|
target_ulong liobn = args[0];
|
|
|
|
target_ulong ioba = args[1];
|
|
|
|
target_ulong tce = args[2];
|
2013-08-29 12:05:00 +04:00
|
|
|
target_ulong ret = H_PARAMETER;
|
2012-06-27 08:50:44 +04:00
|
|
|
sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
|
|
|
|
|
2012-06-27 08:50:46 +04:00
|
|
|
if (tcet) {
|
2014-05-27 09:36:36 +04:00
|
|
|
hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
|
|
|
|
|
|
|
|
ioba &= page_mask;
|
|
|
|
|
2013-08-29 12:05:00 +04:00
|
|
|
ret = put_tce_emu(tcet, ioba, tce);
|
2012-06-27 08:50:46 +04:00
|
|
|
}
|
2015-05-07 08:33:33 +03:00
|
|
|
if (SPAPR_IS_PCI_LIOBN(liobn)) {
|
|
|
|
trace_spapr_iommu_pci_put(liobn, ioba, tce, ret);
|
|
|
|
} else {
|
|
|
|
trace_spapr_iommu_put(liobn, ioba, tce, ret);
|
|
|
|
}
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2013-08-29 12:05:00 +04:00
|
|
|
return ret;
|
2012-06-27 08:50:44 +04:00
|
|
|
}
|
|
|
|
|
2014-02-21 13:29:06 +04:00
|
|
|
static target_ulong get_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
|
|
|
|
target_ulong *tce)
|
|
|
|
{
|
2014-05-27 09:36:37 +04:00
|
|
|
unsigned long index = (ioba - tcet->bus_offset) >> tcet->page_shift;
|
|
|
|
|
|
|
|
if (index >= tcet->nb_table) {
|
2014-02-21 13:29:06 +04:00
|
|
|
hcall_dprintf("spapr_iommu_get_tce on out-of-bounds IOBA 0x"
|
|
|
|
TARGET_FMT_lx "\n", ioba);
|
|
|
|
return H_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:36:37 +04:00
|
|
|
*tce = tcet->table[index];
|
2014-02-21 13:29:06 +04:00
|
|
|
|
|
|
|
return H_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-07-02 09:23:04 +03:00
|
|
|
static target_ulong h_get_tce(PowerPCCPU *cpu, sPAPRMachineState *spapr,
|
2014-02-21 13:29:06 +04:00
|
|
|
target_ulong opcode, target_ulong *args)
|
|
|
|
{
|
|
|
|
target_ulong liobn = args[0];
|
|
|
|
target_ulong ioba = args[1];
|
|
|
|
target_ulong tce = 0;
|
|
|
|
target_ulong ret = H_PARAMETER;
|
|
|
|
sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
|
|
|
|
|
|
|
|
if (tcet) {
|
2014-05-27 09:36:36 +04:00
|
|
|
hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
|
|
|
|
|
|
|
|
ioba &= page_mask;
|
|
|
|
|
2014-02-21 13:29:06 +04:00
|
|
|
ret = get_tce_emu(tcet, ioba, &tce);
|
|
|
|
if (!ret) {
|
|
|
|
args[0] = tce;
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 08:33:33 +03:00
|
|
|
if (SPAPR_IS_PCI_LIOBN(liobn)) {
|
|
|
|
trace_spapr_iommu_pci_get(liobn, ioba, ret, tce);
|
|
|
|
} else {
|
|
|
|
trace_spapr_iommu_get(liobn, ioba, ret, tce);
|
|
|
|
}
|
2014-02-21 13:29:06 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-06-27 08:50:44 +04:00
|
|
|
int spapr_dma_dt(void *fdt, int node_off, const char *propname,
|
2012-08-07 20:10:38 +04:00
|
|
|
uint32_t liobn, uint64_t window, uint32_t size)
|
2012-06-27 08:50:44 +04:00
|
|
|
{
|
2012-08-07 20:10:38 +04:00
|
|
|
uint32_t dma_prop[5];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
dma_prop[0] = cpu_to_be32(liobn);
|
|
|
|
dma_prop[1] = cpu_to_be32(window >> 32);
|
|
|
|
dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
|
|
|
|
dma_prop[3] = 0; /* window size is 32 bits */
|
|
|
|
dma_prop[4] = cpu_to_be32(size);
|
|
|
|
|
|
|
|
ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2012-08-07 20:10:38 +04:00
|
|
|
ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2012-08-07 20:10:38 +04:00
|
|
|
ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2012-06-27 08:50:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-07 20:10:38 +04:00
|
|
|
|
|
|
|
int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
|
2013-04-10 19:30:48 +04:00
|
|
|
sPAPRTCETable *tcet)
|
2012-08-07 20:10:38 +04:00
|
|
|
{
|
2013-04-10 19:30:48 +04:00
|
|
|
if (!tcet) {
|
2012-08-07 20:10:38 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-10 19:30:48 +04:00
|
|
|
return spapr_dma_dt(fdt, node_off, propname,
|
2014-05-27 09:36:36 +04:00
|
|
|
tcet->liobn, 0, tcet->nb_table << tcet->page_shift);
|
2012-08-07 20:10:38 +04:00
|
|
|
}
|
2013-07-18 23:32:58 +04:00
|
|
|
|
|
|
|
static void spapr_tce_table_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
dc->init = spapr_tce_table_realize;
|
|
|
|
dc->reset = spapr_tce_reset;
|
2014-12-08 05:48:02 +03:00
|
|
|
dc->unrealize = spapr_tce_table_unrealize;
|
2013-07-18 23:32:58 +04:00
|
|
|
|
|
|
|
QLIST_INIT(&spapr_tce_tables);
|
|
|
|
|
|
|
|
/* hcall-tce */
|
|
|
|
spapr_register_hypercall(H_PUT_TCE, h_put_tce);
|
2014-02-21 13:29:06 +04:00
|
|
|
spapr_register_hypercall(H_GET_TCE, h_get_tce);
|
2014-05-27 09:36:30 +04:00
|
|
|
spapr_register_hypercall(H_PUT_TCE_INDIRECT, h_put_tce_indirect);
|
|
|
|
spapr_register_hypercall(H_STUFF_TCE, h_stuff_tce);
|
2013-07-18 23:32:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static TypeInfo spapr_tce_table_info = {
|
|
|
|
.name = TYPE_SPAPR_TCE_TABLE,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(sPAPRTCETable),
|
|
|
|
.class_init = spapr_tce_table_class_init,
|
|
|
|
};
|
|
|
|
|
2017-07-11 06:56:20 +03:00
|
|
|
static void spapr_iommu_memory_region_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
|
|
|
|
|
|
|
|
imrc->translate = spapr_tce_translate_iommu;
|
|
|
|
imrc->get_min_page_size = spapr_tce_get_min_page_size;
|
|
|
|
imrc->notify_flag_changed = spapr_tce_notify_flag_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo spapr_iommu_memory_region_info = {
|
|
|
|
.parent = TYPE_IOMMU_MEMORY_REGION,
|
|
|
|
.name = TYPE_SPAPR_IOMMU_MEMORY_REGION,
|
|
|
|
.class_init = spapr_iommu_memory_region_class_init,
|
|
|
|
};
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&spapr_tce_table_info);
|
2017-07-11 06:56:20 +03:00
|
|
|
type_register_static(&spapr_iommu_memory_region_info);
|
2013-07-18 23:32:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|