2 * QEMU sPAPR IOMMU (TCE) code
4 * Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "sysemu/kvm.h"
23 #include "sysemu/dma.h"
24 #include "exec/address-spaces.h"
26 #include "hw/ppc/spapr.h"
30 /* #define DEBUG_TCE */
39 struct sPAPRTCETable
{
46 QLIST_ENTRY(sPAPRTCETable
) list
;
50 QLIST_HEAD(spapr_tce_tables
, sPAPRTCETable
) spapr_tce_tables
;
52 static sPAPRTCETable
*spapr_tce_find_by_liobn(uint32_t liobn
)
56 if (liobn
& 0xFFFFFFFF00000000ULL
) {
57 hcall_dprintf("Request for out-of-bounds LIOBN 0x" TARGET_FMT_lx
"\n",
62 QLIST_FOREACH(tcet
, &spapr_tce_tables
, list
) {
63 if (tcet
->liobn
== liobn
) {
71 static IOMMUTLBEntry
spapr_tce_translate_iommu(MemoryRegion
*iommu
, hwaddr addr
)
73 sPAPRTCETable
*tcet
= container_of(iommu
, sPAPRTCETable
, iommu
);
77 fprintf(stderr
, "spapr_tce_translate liobn=0x%" PRIx32
" addr=0x"
78 DMA_ADDR_FMT
"\n", tcet
->liobn
, addr
);
82 return (IOMMUTLBEntry
) {
83 .target_as
= &address_space_memory
,
86 .addr_mask
= ~(hwaddr
)0,
91 /* Check if we are in bound */
92 if (addr
>= tcet
->window_size
) {
94 fprintf(stderr
, "spapr_tce_translate out of bounds\n");
96 return (IOMMUTLBEntry
) { .perm
= IOMMU_NONE
};
99 tce
= tcet
->table
[addr
>> SPAPR_TCE_PAGE_SHIFT
].tce
;
102 fprintf(stderr
, " -> *paddr=0x%llx, *len=0x%llx\n",
103 (tce
& ~SPAPR_TCE_PAGE_MASK
), SPAPR_TCE_PAGE_MASK
+ 1);
106 return (IOMMUTLBEntry
) {
107 .target_as
= &address_space_memory
,
108 .iova
= addr
& ~SPAPR_TCE_PAGE_MASK
,
109 .translated_addr
= tce
& ~SPAPR_TCE_PAGE_MASK
,
110 .addr_mask
= SPAPR_TCE_PAGE_MASK
,
115 static MemoryRegionIOMMUOps spapr_iommu_ops
= {
116 .translate
= spapr_tce_translate_iommu
,
119 sPAPRTCETable
*spapr_tce_new_table(DeviceState
*owner
, uint32_t liobn
, size_t window_size
)
123 if (spapr_tce_find_by_liobn(liobn
)) {
124 fprintf(stderr
, "Attempted to create TCE table with duplicate"
125 " LIOBN 0x%x\n", liobn
);
133 tcet
= g_malloc0(sizeof(*tcet
));
135 tcet
->window_size
= window_size
;
138 tcet
->table
= kvmppc_create_spapr_tce(liobn
,
144 size_t table_size
= (window_size
>> SPAPR_TCE_PAGE_SHIFT
)
146 tcet
->table
= g_malloc0(table_size
);
150 fprintf(stderr
, "spapr_iommu: New TCE table @ %p, liobn=0x%x, "
151 "table @ %p, fd=%d\n", tcet
, liobn
, tcet
->table
, tcet
->fd
);
154 memory_region_init_iommu(&tcet
->iommu
, OBJECT(owner
), &spapr_iommu_ops
,
155 "iommu-spapr", UINT64_MAX
);
157 QLIST_INSERT_HEAD(&spapr_tce_tables
, tcet
, list
);
162 void spapr_tce_free(sPAPRTCETable
*tcet
)
164 QLIST_REMOVE(tcet
, list
);
166 if (!kvm_enabled() ||
167 (kvmppc_remove_spapr_tce(tcet
->table
, tcet
->fd
,
168 tcet
->window_size
) != 0)) {
175 MemoryRegion
*spapr_tce_get_iommu(sPAPRTCETable
*tcet
)
180 void spapr_tce_set_bypass(sPAPRTCETable
*tcet
, bool bypass
)
182 tcet
->bypass
= bypass
;
185 void spapr_tce_reset(sPAPRTCETable
*tcet
)
187 size_t table_size
= (tcet
->window_size
>> SPAPR_TCE_PAGE_SHIFT
)
190 tcet
->bypass
= false;
191 memset(tcet
->table
, 0, table_size
);
194 static target_ulong
put_tce_emu(sPAPRTCETable
*tcet
, target_ulong ioba
,
200 if (ioba
>= tcet
->window_size
) {
201 hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
202 TARGET_FMT_lx
"\n", ioba
);
206 tcep
= tcet
->table
+ (ioba
>> SPAPR_TCE_PAGE_SHIFT
);
209 entry
.target_as
= &address_space_memory
,
210 entry
.iova
= ioba
& ~SPAPR_TCE_PAGE_MASK
;
211 entry
.translated_addr
= tce
& ~SPAPR_TCE_PAGE_MASK
;
212 entry
.addr_mask
= SPAPR_TCE_PAGE_MASK
;
214 memory_region_notify_iommu(&tcet
->iommu
, entry
);
219 static target_ulong
h_put_tce(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
220 target_ulong opcode
, target_ulong
*args
)
222 target_ulong liobn
= args
[0];
223 target_ulong ioba
= args
[1];
224 target_ulong tce
= args
[2];
225 sPAPRTCETable
*tcet
= spapr_tce_find_by_liobn(liobn
);
227 ioba
&= ~(SPAPR_TCE_PAGE_SIZE
- 1);
230 return put_tce_emu(tcet
, ioba
, tce
);
233 fprintf(stderr
, "%s on liobn=" TARGET_FMT_lx
/*%s*/
234 " ioba 0x" TARGET_FMT_lx
" TCE 0x" TARGET_FMT_lx
"\n",
235 __func__
, liobn
, /*dev->qdev.id, */ioba
, tce
);
241 void spapr_iommu_init(void)
243 QLIST_INIT(&spapr_tce_tables
);
246 spapr_register_hypercall(H_PUT_TCE
, h_put_tce
);
249 int spapr_dma_dt(void *fdt
, int node_off
, const char *propname
,
250 uint32_t liobn
, uint64_t window
, uint32_t size
)
252 uint32_t dma_prop
[5];
255 dma_prop
[0] = cpu_to_be32(liobn
);
256 dma_prop
[1] = cpu_to_be32(window
>> 32);
257 dma_prop
[2] = cpu_to_be32(window
& 0xFFFFFFFF);
258 dma_prop
[3] = 0; /* window size is 32 bits */
259 dma_prop
[4] = cpu_to_be32(size
);
261 ret
= fdt_setprop_cell(fdt
, node_off
, "ibm,#dma-address-cells", 2);
266 ret
= fdt_setprop_cell(fdt
, node_off
, "ibm,#dma-size-cells", 2);
271 ret
= fdt_setprop(fdt
, node_off
, propname
, dma_prop
, sizeof(dma_prop
));
279 int spapr_tcet_dma_dt(void *fdt
, int node_off
, const char *propname
,
286 return spapr_dma_dt(fdt
, node_off
, propname
,
287 tcet
->liobn
, 0, tcet
->window_size
);