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/>.
29 /* #define DEBUG_TCE */
38 typedef struct sPAPRTCETable sPAPRTCETable
;
40 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 QLIST_FOREACH(tcet
, &spapr_tce_tables
, list
) {
57 if (tcet
->liobn
== liobn
) {
65 static int spapr_tce_translate(DMAContext
*dma
,
67 target_phys_addr_t
*paddr
,
68 target_phys_addr_t
*len
,
71 sPAPRTCETable
*tcet
= DO_UPCAST(sPAPRTCETable
, dma
, dma
);
72 enum sPAPRTCEAccess access
= (dir
== DMA_DIRECTION_FROM_DEVICE
)
73 ? SPAPR_TCE_WO
: SPAPR_TCE_RO
;
77 fprintf(stderr
, "spapr_tce_translate liobn=0x%" PRIx32
" addr=0x"
78 DMA_ADDR_FMT
"\n", tcet
->liobn
, addr
);
81 /* Check if we are in bound */
82 if (addr
>= tcet
->window_size
) {
84 fprintf(stderr
, "spapr_tce_translate out of bounds\n");
89 tce
= tcet
->table
[addr
>> SPAPR_TCE_PAGE_SHIFT
].tce
;
92 if (!(tce
& access
)) {
96 /* How much til end of page ? */
97 *len
= ((~addr
) & SPAPR_TCE_PAGE_MASK
) + 1;
100 *paddr
= (tce
& ~SPAPR_TCE_PAGE_MASK
) |
101 (addr
& SPAPR_TCE_PAGE_MASK
);
104 fprintf(stderr
, " -> *paddr=0x" TARGET_FMT_plx
", *len=0x"
105 TARGET_FMT_plx
"\n", *paddr
, *len
);
111 DMAContext
*spapr_tce_new_dma_context(uint32_t liobn
, size_t window_size
)
119 tcet
= g_malloc0(sizeof(*tcet
));
120 dma_context_init(&tcet
->dma
, spapr_tce_translate
, NULL
, NULL
);
123 tcet
->window_size
= window_size
;
126 tcet
->table
= kvmppc_create_spapr_tce(liobn
,
132 size_t table_size
= (window_size
>> SPAPR_TCE_PAGE_SHIFT
)
134 tcet
->table
= g_malloc0(table_size
);
138 fprintf(stderr
, "spapr_iommu: New TCE table, liobn=0x%x, context @ %p, "
139 "table @ %p, fd=%d\n", liobn
, &tcet
->dma
, tcet
->table
, tcet
->fd
);
142 QLIST_INSERT_HEAD(&spapr_tce_tables
, tcet
, list
);
147 void spapr_tce_free(DMAContext
*dma
)
151 sPAPRTCETable
*tcet
= DO_UPCAST(sPAPRTCETable
, dma
, dma
);
153 QLIST_REMOVE(tcet
, list
);
155 if (!kvm_enabled() ||
156 (kvmppc_remove_spapr_tce(tcet
->table
, tcet
->fd
,
157 tcet
->window_size
) != 0)) {
165 static target_ulong
put_tce_emu(sPAPRTCETable
*tcet
, target_ulong ioba
,
170 if (ioba
>= tcet
->window_size
) {
171 hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
172 TARGET_FMT_lx
"\n", ioba
);
176 tcep
= tcet
->table
+ (ioba
>> SPAPR_TCE_PAGE_SHIFT
);
182 static target_ulong
h_put_tce(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
183 target_ulong opcode
, target_ulong
*args
)
185 target_ulong liobn
= args
[0];
186 target_ulong ioba
= args
[1];
187 target_ulong tce
= args
[2];
188 sPAPRTCETable
*tcet
= spapr_tce_find_by_liobn(liobn
);
190 if (liobn
& 0xFFFFFFFF00000000ULL
) {
191 hcall_dprintf("spapr_vio_put_tce on out-of-boundsw LIOBN "
192 TARGET_FMT_lx
"\n", liobn
);
196 ioba
&= ~(SPAPR_TCE_PAGE_SIZE
- 1);
199 return put_tce_emu(tcet
, ioba
, tce
);
202 fprintf(stderr
, "%s on liobn=" TARGET_FMT_lx
/*%s*/
203 " ioba 0x" TARGET_FMT_lx
" TCE 0x" TARGET_FMT_lx
"\n",
204 __func__
, liobn
, /*dev->qdev.id, */ioba
, tce
);
210 void spapr_iommu_init(void)
212 QLIST_INIT(&spapr_tce_tables
);
215 spapr_register_hypercall(H_PUT_TCE
, h_put_tce
);
218 int spapr_dma_dt(void *fdt
, int node_off
, const char *propname
,
219 uint32_t liobn
, uint64_t window
, uint32_t size
)
221 uint32_t dma_prop
[5];
224 dma_prop
[0] = cpu_to_be32(liobn
);
225 dma_prop
[1] = cpu_to_be32(window
>> 32);
226 dma_prop
[2] = cpu_to_be32(window
& 0xFFFFFFFF);
227 dma_prop
[3] = 0; /* window size is 32 bits */
228 dma_prop
[4] = cpu_to_be32(size
);
230 ret
= fdt_setprop_cell(fdt
, node_off
, "ibm,#dma-address-cells", 2);
235 ret
= fdt_setprop_cell(fdt
, node_off
, "ibm,#dma-size-cells", 2);
240 ret
= fdt_setprop(fdt
, node_off
, propname
, dma_prop
, sizeof(dma_prop
));
248 int spapr_tcet_dma_dt(void *fdt
, int node_off
, const char *propname
,
255 if (iommu
->translate
== spapr_tce_translate
) {
256 sPAPRTCETable
*tcet
= DO_UPCAST(sPAPRTCETable
, dma
, iommu
);
257 return spapr_dma_dt(fdt
, node_off
, propname
,
258 tcet
->liobn
, 0, tcet
->window_size
);