Merge remote-tracking branch 'remotes/stefanha/tags/tracing-pull-request' into staging
[qemu/ar7.git] / hw / ppc / spapr_rtas_ddw.c
blob3501b0581991bef1dc19cd6add0d4bd202213e16
1 /*
2 * QEMU sPAPR Dynamic DMA windows support
4 * Copyright (c) 2015 Alexey Kardashevskiy, IBM Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "qemu/error-report.h"
23 #include "qemu/module.h"
24 #include "hw/ppc/spapr.h"
25 #include "hw/pci-host/spapr.h"
26 #include "trace.h"
28 static int spapr_phb_get_active_win_num_cb(Object *child, void *opaque)
30 SpaprTceTable *tcet;
32 tcet = (SpaprTceTable *) object_dynamic_cast(child, TYPE_SPAPR_TCE_TABLE);
33 if (tcet && tcet->nb_table) {
34 ++*(unsigned *)opaque;
36 return 0;
39 static unsigned spapr_phb_get_active_win_num(SpaprPhbState *sphb)
41 unsigned ret = 0;
43 object_child_foreach(OBJECT(sphb), spapr_phb_get_active_win_num_cb, &ret);
45 return ret;
48 static int spapr_phb_get_free_liobn_cb(Object *child, void *opaque)
50 SpaprTceTable *tcet;
52 tcet = (SpaprTceTable *) object_dynamic_cast(child, TYPE_SPAPR_TCE_TABLE);
53 if (tcet && !tcet->nb_table) {
54 *(uint32_t *)opaque = tcet->liobn;
55 return 1;
57 return 0;
60 static unsigned spapr_phb_get_free_liobn(SpaprPhbState *sphb)
62 uint32_t liobn = 0;
64 object_child_foreach(OBJECT(sphb), spapr_phb_get_free_liobn_cb, &liobn);
66 return liobn;
69 static uint32_t spapr_page_mask_to_query_mask(uint64_t page_mask)
71 int i;
72 uint32_t mask = 0;
73 const struct { int shift; uint32_t mask; } masks[] = {
74 { 12, RTAS_DDW_PGSIZE_4K },
75 { 16, RTAS_DDW_PGSIZE_64K },
76 { 24, RTAS_DDW_PGSIZE_16M },
77 { 25, RTAS_DDW_PGSIZE_32M },
78 { 26, RTAS_DDW_PGSIZE_64M },
79 { 27, RTAS_DDW_PGSIZE_128M },
80 { 28, RTAS_DDW_PGSIZE_256M },
81 { 34, RTAS_DDW_PGSIZE_16G },
84 for (i = 0; i < ARRAY_SIZE(masks); ++i) {
85 if (page_mask & (1ULL << masks[i].shift)) {
86 mask |= masks[i].mask;
90 return mask;
93 static void rtas_ibm_query_pe_dma_window(PowerPCCPU *cpu,
94 SpaprMachineState *spapr,
95 uint32_t token, uint32_t nargs,
96 target_ulong args,
97 uint32_t nret, target_ulong rets)
99 SpaprPhbState *sphb;
100 uint64_t buid;
101 uint32_t avail, addr, pgmask = 0;
103 if ((nargs != 3) || (nret != 5)) {
104 goto param_error_exit;
107 buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
108 addr = rtas_ld(args, 0);
109 sphb = spapr_pci_find_phb(spapr, buid);
110 if (!sphb || !sphb->ddw_enabled) {
111 goto param_error_exit;
114 /* Translate page mask to LoPAPR format */
115 pgmask = spapr_page_mask_to_query_mask(sphb->page_size_mask);
117 avail = SPAPR_PCI_DMA_MAX_WINDOWS - spapr_phb_get_active_win_num(sphb);
119 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
120 rtas_st(rets, 1, avail);
121 rtas_st(rets, 2, 0x80000000); /* The largest window we can possibly have */
122 rtas_st(rets, 3, pgmask);
123 rtas_st(rets, 4, 0); /* DMA migration mask, not supported */
125 trace_spapr_iommu_ddw_query(buid, addr, avail, 0x80000000, pgmask);
126 return;
128 param_error_exit:
129 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
132 static void rtas_ibm_create_pe_dma_window(PowerPCCPU *cpu,
133 SpaprMachineState *spapr,
134 uint32_t token, uint32_t nargs,
135 target_ulong args,
136 uint32_t nret, target_ulong rets)
138 SpaprPhbState *sphb;
139 SpaprTceTable *tcet = NULL;
140 uint32_t addr, page_shift, window_shift, liobn;
141 uint64_t buid, win_addr;
142 int windows;
144 if ((nargs != 5) || (nret != 4)) {
145 goto param_error_exit;
148 buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
149 addr = rtas_ld(args, 0);
150 sphb = spapr_pci_find_phb(spapr, buid);
151 if (!sphb || !sphb->ddw_enabled) {
152 goto param_error_exit;
155 page_shift = rtas_ld(args, 3);
156 window_shift = rtas_ld(args, 4);
157 liobn = spapr_phb_get_free_liobn(sphb);
158 windows = spapr_phb_get_active_win_num(sphb);
160 if (!(sphb->page_size_mask & (1ULL << page_shift)) ||
161 (window_shift < page_shift)) {
162 goto param_error_exit;
165 if (!liobn || !sphb->ddw_enabled || windows == SPAPR_PCI_DMA_MAX_WINDOWS) {
166 goto hw_error_exit;
169 tcet = spapr_tce_find_by_liobn(liobn);
170 if (!tcet) {
171 goto hw_error_exit;
174 win_addr = (windows == 0) ? sphb->dma_win_addr : sphb->dma64_win_addr;
176 * We have just created a window, we know for the fact that it is empty,
177 * use a hack to avoid iterating over the table as it is quite possible
178 * to have billions of TCEs, all empty.
179 * Note that we cannot delay this to the first H_PUT_TCE as this hcall is
180 * mostly likely to be handled in KVM so QEMU just does not know if it
181 * happened.
183 tcet->skipping_replay = true;
184 spapr_tce_table_enable(tcet, page_shift, win_addr,
185 1ULL << (window_shift - page_shift));
186 tcet->skipping_replay = false;
187 if (!tcet->nb_table) {
188 goto hw_error_exit;
191 trace_spapr_iommu_ddw_create(buid, addr, 1ULL << page_shift,
192 1ULL << window_shift, tcet->bus_offset, liobn);
194 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
195 rtas_st(rets, 1, liobn);
196 rtas_st(rets, 2, tcet->bus_offset >> 32);
197 rtas_st(rets, 3, tcet->bus_offset & ((uint32_t) -1));
199 return;
201 hw_error_exit:
202 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
203 return;
205 param_error_exit:
206 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
209 static void rtas_ibm_remove_pe_dma_window(PowerPCCPU *cpu,
210 SpaprMachineState *spapr,
211 uint32_t token, uint32_t nargs,
212 target_ulong args,
213 uint32_t nret, target_ulong rets)
215 SpaprPhbState *sphb;
216 SpaprTceTable *tcet;
217 uint32_t liobn;
219 if ((nargs != 1) || (nret != 1)) {
220 goto param_error_exit;
223 liobn = rtas_ld(args, 0);
224 tcet = spapr_tce_find_by_liobn(liobn);
225 if (!tcet) {
226 goto param_error_exit;
229 sphb = SPAPR_PCI_HOST_BRIDGE(OBJECT(tcet)->parent);
230 if (!sphb || !sphb->ddw_enabled || !tcet->nb_table) {
231 goto param_error_exit;
234 spapr_tce_table_disable(tcet);
235 trace_spapr_iommu_ddw_remove(liobn);
237 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
238 return;
240 param_error_exit:
241 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
244 static void rtas_ibm_reset_pe_dma_window(PowerPCCPU *cpu,
245 SpaprMachineState *spapr,
246 uint32_t token, uint32_t nargs,
247 target_ulong args,
248 uint32_t nret, target_ulong rets)
250 SpaprPhbState *sphb;
251 uint64_t buid;
252 uint32_t addr;
254 if ((nargs != 3) || (nret != 1)) {
255 goto param_error_exit;
258 buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
259 addr = rtas_ld(args, 0);
260 sphb = spapr_pci_find_phb(spapr, buid);
261 if (!sphb || !sphb->ddw_enabled) {
262 goto param_error_exit;
265 spapr_phb_dma_reset(sphb);
266 trace_spapr_iommu_ddw_reset(buid, addr);
268 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
270 return;
272 param_error_exit:
273 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
276 static void spapr_rtas_ddw_init(void)
278 spapr_rtas_register(RTAS_IBM_QUERY_PE_DMA_WINDOW,
279 "ibm,query-pe-dma-window",
280 rtas_ibm_query_pe_dma_window);
281 spapr_rtas_register(RTAS_IBM_CREATE_PE_DMA_WINDOW,
282 "ibm,create-pe-dma-window",
283 rtas_ibm_create_pe_dma_window);
284 spapr_rtas_register(RTAS_IBM_REMOVE_PE_DMA_WINDOW,
285 "ibm,remove-pe-dma-window",
286 rtas_ibm_remove_pe_dma_window);
287 spapr_rtas_register(RTAS_IBM_RESET_PE_DMA_WINDOW,
288 "ibm,reset-pe-dma-window",
289 rtas_ibm_reset_pe_dma_window);
292 type_init(spapr_rtas_ddw_init)