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/>.
|
|
|
|
*/
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/hw.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"
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
|
|
|
|
|
|
|
|
static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
if (tcet->liobn == liobn) {
|
|
|
|
return tcet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-11 14:35:33 +04:00
|
|
|
static IOMMUTLBEntry spapr_tce_translate_iommu(MemoryRegion *iommu, hwaddr addr)
|
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
|
|
|
|
2012-09-12 20:57:20 +04:00
|
|
|
if (tcet->bypass) {
|
2013-08-29 12:05:00 +04:00
|
|
|
ret.perm = IOMMU_RW;
|
|
|
|
} else if (addr < tcet->window_size) {
|
|
|
|
/* Check if we are in bound */
|
|
|
|
tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT];
|
|
|
|
ret.iova = addr & ~SPAPR_TCE_PAGE_MASK;
|
|
|
|
ret.translated_addr = tce & ~SPAPR_TCE_PAGE_MASK;
|
|
|
|
ret.addr_mask = SPAPR_TCE_PAGE_MASK;
|
|
|
|
ret.perm = 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
|
|
|
}
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
static int spapr_tce_table_pre_load(void *opaque)
|
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
|
|
|
|
|
|
|
|
tcet->nb_table = tcet->window_size >> SPAPR_TCE_PAGE_SHIFT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_spapr_tce_table = {
|
|
|
|
.name = "spapr_iommu",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.pre_load = spapr_tce_table_pre_load,
|
|
|
|
.fields = (VMStateField []) {
|
|
|
|
/* Sanity check */
|
|
|
|
VMSTATE_UINT32_EQUAL(liobn, sPAPRTCETable),
|
|
|
|
VMSTATE_UINT32_EQUAL(window_size, sPAPRTCETable),
|
|
|
|
|
|
|
|
/* IOMMU state */
|
|
|
|
VMSTATE_BOOL(bypass, sPAPRTCETable),
|
|
|
|
VMSTATE_VARRAY_UINT32(table, sPAPRTCETable, nb_table, 0, vmstate_info_uint64, uint64_t),
|
|
|
|
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-04-11 14:35:33 +04:00
|
|
|
static MemoryRegionIOMMUOps spapr_iommu_ops = {
|
|
|
|
.translate = spapr_tce_translate_iommu,
|
|
|
|
};
|
2012-06-27 08:50:44 +04:00
|
|
|
|
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);
|
2012-06-27 08:50:44 +04:00
|
|
|
|
|
|
|
if (kvm_enabled()) {
|
2013-07-18 23:32:58 +04:00
|
|
|
tcet->table = kvmppc_create_spapr_tce(tcet->liobn,
|
|
|
|
tcet->window_size,
|
2012-06-27 08:50:44 +04:00
|
|
|
&tcet->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tcet->table) {
|
2013-07-18 23:32:58 +04:00
|
|
|
size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
|
|
|
|
* sizeof(uint64_t);
|
2012-06-27 08:50:44 +04:00
|
|
|
tcet->table = g_malloc0(table_size);
|
|
|
|
}
|
2013-07-18 23:32:58 +04:00
|
|
|
tcet->nb_table = tcet->window_size >> SPAPR_TCE_PAGE_SHIFT;
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2013-08-29 12:05:00 +04:00
|
|
|
trace_spapr_iommu_new_table(tcet->liobn, tcet, tcet->table, tcet->fd);
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
memory_region_init_iommu(&tcet->iommu, OBJECT(dev), &spapr_iommu_ops,
|
2013-04-11 14:35:33 +04:00
|
|
|
"iommu-spapr", UINT64_MAX);
|
|
|
|
|
2012-06-27 08:50:44 +04:00
|
|
|
QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn, size_t window_size)
|
|
|
|
{
|
|
|
|
sPAPRTCETable *tcet;
|
|
|
|
|
|
|
|
if (spapr_tce_find_by_liobn(liobn)) {
|
|
|
|
fprintf(stderr, "Attempted to create TCE table with duplicate"
|
|
|
|
" LIOBN 0x%x\n", liobn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!window_size) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcet = SPAPR_TCE_TABLE(object_new(TYPE_SPAPR_TCE_TABLE));
|
|
|
|
tcet->liobn = liobn;
|
|
|
|
tcet->window_size = window_size;
|
|
|
|
|
|
|
|
object_property_add_child(OBJECT(owner), "tce-table", OBJECT(tcet), NULL);
|
|
|
|
|
|
|
|
qdev_init_nofail(DEVICE(tcet));
|
|
|
|
|
2013-04-10 19:30:48 +04:00
|
|
|
return tcet;
|
2012-06-27 08:50:44 +04:00
|
|
|
}
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
static void spapr_tce_table_finalize(Object *obj)
|
2012-06-27 08:50:44 +04:00
|
|
|
{
|
2013-07-18 23:32:58 +04:00
|
|
|
sPAPRTCETable *tcet = SPAPR_TCE_TABLE(obj);
|
|
|
|
|
2013-04-10 19:30:48 +04:00
|
|
|
QLIST_REMOVE(tcet, list);
|
2012-06-27 08:50:44 +04:00
|
|
|
|
2013-04-10 19:30:48 +04:00
|
|
|
if (!kvm_enabled() ||
|
|
|
|
(kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
|
|
|
|
tcet->window_size) != 0)) {
|
|
|
|
g_free(tcet->table);
|
2012-06-27 08:50:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 14:35:33 +04:00
|
|
|
MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
|
|
|
|
{
|
|
|
|
return &tcet->iommu;
|
|
|
|
}
|
|
|
|
|
2013-04-10 19:30:48 +04:00
|
|
|
void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
|
|
|
|
{
|
2012-09-12 20:57:20 +04:00
|
|
|
tcet->bypass = bypass;
|
|
|
|
}
|
|
|
|
|
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);
|
2012-09-12 20:57:20 +04:00
|
|
|
size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
|
2013-07-18 23:32:58 +04:00
|
|
|
* sizeof(uint64_t);
|
2012-09-12 20:57:14 +04:00
|
|
|
|
2012-09-12 20:57:20 +04:00
|
|
|
tcet->bypass = false;
|
|
|
|
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;
|
2012-06-27 08:50:46 +04:00
|
|
|
|
|
|
|
if (ioba >= tcet->window_size) {
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-18 23:32:58 +04:00
|
|
|
tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT] = tce;
|
2012-06-27 08:50:46 +04:00
|
|
|
|
2013-04-11 14:35:33 +04:00
|
|
|
entry.target_as = &address_space_memory,
|
|
|
|
entry.iova = ioba & ~SPAPR_TCE_PAGE_MASK;
|
|
|
|
entry.translated_addr = tce & ~SPAPR_TCE_PAGE_MASK;
|
|
|
|
entry.addr_mask = SPAPR_TCE_PAGE_MASK;
|
|
|
|
entry.perm = tce;
|
|
|
|
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
|
|
|
|
2012-05-03 08:23:01 +04:00
|
|
|
static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *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);
|
|
|
|
|
|
|
|
ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
|
|
|
|
|
2012-06-27 08:50:46 +04:00
|
|
|
if (tcet) {
|
2013-08-29 12:05:00 +04:00
|
|
|
ret = put_tce_emu(tcet, ioba, tce);
|
2012-06-27 08:50:46 +04:00
|
|
|
}
|
2013-08-29 12:05:00 +04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (ioba >= tcet->window_size) {
|
|
|
|
hcall_dprintf("spapr_iommu_get_tce on out-of-bounds IOBA 0x"
|
|
|
|
TARGET_FMT_lx "\n", ioba);
|
|
|
|
return H_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
*tce = tcet->table[ioba >> SPAPR_TCE_PAGE_SHIFT];
|
|
|
|
|
|
|
|
return H_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static target_ulong h_get_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
|
|
|
|
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);
|
|
|
|
|
|
|
|
ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
|
|
|
|
|
|
|
|
if (tcet) {
|
|
|
|
ret = get_tce_emu(tcet, ioba, &tce);
|
|
|
|
if (!ret) {
|
|
|
|
args[0] = tce;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trace_spapr_iommu_get(liobn, ioba, ret, tce);
|
|
|
|
|
|
|
|
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,
|
|
|
|
tcet->liobn, 0, tcet->window_size);
|
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->vmsd = &vmstate_spapr_tce_table;
|
|
|
|
dc->init = spapr_tce_table_realize;
|
|
|
|
dc->reset = spapr_tce_reset;
|
|
|
|
|
|
|
|
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);
|
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,
|
|
|
|
.instance_finalize = spapr_tce_table_finalize,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&spapr_tce_table_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|