]>
Commit | Line | Data |
---|---|---|
527773ee EA |
1 | /* |
2 | * Copyright (C) 2014-2016 Broadcom Corporation | |
3 | * Copyright (c) 2017 Red Hat, Inc. | |
4 | * Written by Prem Mallappa, Eric Auger | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * Author: Prem Mallappa <pmallapp@broadcom.com> | |
16 | * | |
17 | */ | |
18 | ||
19 | #include "qemu/osdep.h" | |
527773ee EA |
20 | #include "trace.h" |
21 | #include "exec/target_page.h" | |
2e5b09fd | 22 | #include "hw/core/cpu.h" |
527773ee EA |
23 | #include "hw/qdev-properties.h" |
24 | #include "qapi/error.h" | |
cc27ed81 | 25 | #include "qemu/jhash.h" |
0b8fa32f | 26 | #include "qemu/module.h" |
527773ee EA |
27 | |
28 | #include "qemu/error-report.h" | |
29 | #include "hw/arm/smmu-common.h" | |
93641948 EA |
30 | #include "smmu-internal.h" |
31 | ||
cc27ed81 EA |
32 | /* IOTLB Management */ |
33 | ||
60a61f1b EA |
34 | static guint smmu_iotlb_key_hash(gconstpointer v) |
35 | { | |
36 | SMMUIOTLBKey *key = (SMMUIOTLBKey *)v; | |
37 | uint32_t a, b, c; | |
38 | ||
39 | /* Jenkins hash */ | |
40 | a = b = c = JHASH_INITVAL + sizeof(*key); | |
9e54dee7 | 41 | a += key->asid + key->level + key->tg; |
60a61f1b EA |
42 | b += extract64(key->iova, 0, 32); |
43 | c += extract64(key->iova, 32, 32); | |
44 | ||
45 | __jhash_mix(a, b, c); | |
46 | __jhash_final(a, b, c); | |
47 | ||
48 | return c; | |
49 | } | |
50 | ||
51 | static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2) | |
52 | { | |
9e54dee7 | 53 | SMMUIOTLBKey *k1 = (SMMUIOTLBKey *)v1, *k2 = (SMMUIOTLBKey *)v2; |
60a61f1b | 54 | |
9e54dee7 EA |
55 | return (k1->asid == k2->asid) && (k1->iova == k2->iova) && |
56 | (k1->level == k2->level) && (k1->tg == k2->tg); | |
60a61f1b EA |
57 | } |
58 | ||
9e54dee7 EA |
59 | SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova, |
60 | uint8_t tg, uint8_t level) | |
60a61f1b | 61 | { |
9e54dee7 | 62 | SMMUIOTLBKey key = {.asid = asid, .iova = iova, .tg = tg, .level = level}; |
60a61f1b EA |
63 | |
64 | return key; | |
65 | } | |
66 | ||
a7550158 | 67 | SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, |
9e54dee7 | 68 | SMMUTransTableInfo *tt, hwaddr iova) |
6808bca9 | 69 | { |
9e54dee7 EA |
70 | uint8_t tg = (tt->granule_sz - 10) / 2; |
71 | uint8_t inputsize = 64 - tt->tsz; | |
72 | uint8_t stride = tt->granule_sz - 3; | |
73 | uint8_t level = 4 - (inputsize - 4) / stride; | |
74 | SMMUTLBEntry *entry = NULL; | |
75 | ||
76 | while (level <= 3) { | |
77 | uint64_t subpage_size = 1ULL << level_shift(level, tt->granule_sz); | |
78 | uint64_t mask = subpage_size - 1; | |
79 | SMMUIOTLBKey key; | |
80 | ||
81 | key = smmu_get_iotlb_key(cfg->asid, iova & ~mask, tg, level); | |
82 | entry = g_hash_table_lookup(bs->iotlb, &key); | |
83 | if (entry) { | |
84 | break; | |
85 | } | |
86 | level++; | |
87 | } | |
6808bca9 EA |
88 | |
89 | if (entry) { | |
90 | cfg->iotlb_hits++; | |
91 | trace_smmu_iotlb_lookup_hit(cfg->asid, iova, | |
92 | cfg->iotlb_hits, cfg->iotlb_misses, | |
93 | 100 * cfg->iotlb_hits / | |
94 | (cfg->iotlb_hits + cfg->iotlb_misses)); | |
95 | } else { | |
96 | cfg->iotlb_misses++; | |
97 | trace_smmu_iotlb_lookup_miss(cfg->asid, iova, | |
98 | cfg->iotlb_hits, cfg->iotlb_misses, | |
99 | 100 * cfg->iotlb_hits / | |
100 | (cfg->iotlb_hits + cfg->iotlb_misses)); | |
101 | } | |
102 | return entry; | |
103 | } | |
104 | ||
a7550158 | 105 | void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *new) |
6808bca9 EA |
106 | { |
107 | SMMUIOTLBKey *key = g_new0(SMMUIOTLBKey, 1); | |
9e54dee7 | 108 | uint8_t tg = (new->granule - 10) / 2; |
6808bca9 EA |
109 | |
110 | if (g_hash_table_size(bs->iotlb) >= SMMU_IOTLB_MAX_SIZE) { | |
111 | smmu_iotlb_inv_all(bs); | |
112 | } | |
113 | ||
9e54dee7 EA |
114 | *key = smmu_get_iotlb_key(cfg->asid, new->entry.iova, tg, new->level); |
115 | trace_smmu_iotlb_insert(cfg->asid, new->entry.iova, tg, new->level); | |
a7550158 | 116 | g_hash_table_insert(bs->iotlb, key, new); |
6808bca9 EA |
117 | } |
118 | ||
cc27ed81 EA |
119 | inline void smmu_iotlb_inv_all(SMMUState *s) |
120 | { | |
121 | trace_smmu_iotlb_inv_all(); | |
122 | g_hash_table_remove_all(s->iotlb); | |
123 | } | |
124 | ||
125 | static gboolean smmu_hash_remove_by_asid(gpointer key, gpointer value, | |
126 | gpointer user_data) | |
127 | { | |
128 | uint16_t asid = *(uint16_t *)user_data; | |
129 | SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key; | |
130 | ||
60a61f1b | 131 | return SMMU_IOTLB_ASID(*iotlb_key) == asid; |
cc27ed81 EA |
132 | } |
133 | ||
9e54dee7 EA |
134 | static gboolean smmu_hash_remove_by_asid_iova(gpointer key, gpointer value, |
135 | gpointer user_data) | |
cc27ed81 | 136 | { |
9e54dee7 EA |
137 | SMMUTLBEntry *iter = (SMMUTLBEntry *)value; |
138 | IOMMUTLBEntry *entry = &iter->entry; | |
139 | SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data; | |
140 | SMMUIOTLBKey iotlb_key = *(SMMUIOTLBKey *)key; | |
141 | ||
142 | if (info->asid >= 0 && info->asid != SMMU_IOTLB_ASID(iotlb_key)) { | |
143 | return false; | |
144 | } | |
d5291561 EA |
145 | return ((info->iova & ~entry->addr_mask) == entry->iova) || |
146 | ((entry->iova & ~info->mask) == info->iova); | |
9e54dee7 EA |
147 | } |
148 | ||
d5291561 EA |
149 | inline void |
150 | smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova, | |
151 | uint8_t tg, uint64_t num_pages, uint8_t ttl) | |
9e54dee7 | 152 | { |
6d9cd115 EA |
153 | /* if tg is not set we use 4KB range invalidation */ |
154 | uint8_t granule = tg ? tg * 2 + 10 : 12; | |
155 | ||
a4b6e1be | 156 | if (ttl && (num_pages == 1) && (asid >= 0)) { |
d5291561 | 157 | SMMUIOTLBKey key = smmu_get_iotlb_key(asid, iova, tg, ttl); |
cc27ed81 | 158 | |
6d9cd115 EA |
159 | if (g_hash_table_remove(s->iotlb, &key)) { |
160 | return; | |
161 | } | |
162 | /* | |
163 | * if the entry is not found, let's see if it does not | |
164 | * belong to a larger IOTLB entry | |
165 | */ | |
166 | } | |
d5291561 | 167 | |
6d9cd115 EA |
168 | SMMUIOTLBPageInvInfo info = { |
169 | .asid = asid, .iova = iova, | |
170 | .mask = (num_pages * 1 << granule) - 1}; | |
d5291561 | 171 | |
6d9cd115 EA |
172 | g_hash_table_foreach_remove(s->iotlb, |
173 | smmu_hash_remove_by_asid_iova, | |
174 | &info); | |
cc27ed81 EA |
175 | } |
176 | ||
177 | inline void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid) | |
178 | { | |
179 | trace_smmu_iotlb_inv_asid(asid); | |
180 | g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_asid, &asid); | |
181 | } | |
182 | ||
93641948 EA |
183 | /* VMSAv8-64 Translation */ |
184 | ||
185 | /** | |
186 | * get_pte - Get the content of a page table entry located at | |
187 | * @base_addr[@index] | |
188 | */ | |
189 | static int get_pte(dma_addr_t baseaddr, uint32_t index, uint64_t *pte, | |
190 | SMMUPTWEventInfo *info) | |
191 | { | |
192 | int ret; | |
193 | dma_addr_t addr = baseaddr + index * sizeof(*pte); | |
194 | ||
195 | /* TODO: guarantee 64-bit single-copy atomicity */ | |
ba06fe8a PMD |
196 | ret = dma_memory_read(&address_space_memory, addr, pte, sizeof(*pte), |
197 | MEMTXATTRS_UNSPECIFIED); | |
93641948 EA |
198 | |
199 | if (ret != MEMTX_OK) { | |
200 | info->type = SMMU_PTW_ERR_WALK_EABT; | |
201 | info->addr = addr; | |
202 | return -EINVAL; | |
203 | } | |
204 | trace_smmu_get_pte(baseaddr, index, addr, *pte); | |
205 | return 0; | |
206 | } | |
207 | ||
208 | /* VMSAv8-64 Translation Table Format Descriptor Decoding */ | |
209 | ||
210 | /** | |
211 | * get_page_pte_address - returns the L3 descriptor output address, | |
212 | * ie. the page frame | |
213 | * ARM ARM spec: Figure D4-17 VMSAv8-64 level 3 descriptor format | |
214 | */ | |
215 | static inline hwaddr get_page_pte_address(uint64_t pte, int granule_sz) | |
216 | { | |
217 | return PTE_ADDRESS(pte, granule_sz); | |
218 | } | |
219 | ||
220 | /** | |
221 | * get_table_pte_address - return table descriptor output address, | |
222 | * ie. address of next level table | |
223 | * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats | |
224 | */ | |
225 | static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz) | |
226 | { | |
227 | return PTE_ADDRESS(pte, granule_sz); | |
228 | } | |
229 | ||
230 | /** | |
231 | * get_block_pte_address - return block descriptor output address and block size | |
232 | * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats | |
233 | */ | |
234 | static inline hwaddr get_block_pte_address(uint64_t pte, int level, | |
235 | int granule_sz, uint64_t *bsz) | |
236 | { | |
118eee6c | 237 | int n = level_shift(level, granule_sz); |
93641948 | 238 | |
118eee6c | 239 | *bsz = 1ULL << n; |
93641948 EA |
240 | return PTE_ADDRESS(pte, n); |
241 | } | |
242 | ||
243 | SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova) | |
244 | { | |
245 | bool tbi = extract64(iova, 55, 1) ? TBI1(cfg->tbi) : TBI0(cfg->tbi); | |
246 | uint8_t tbi_byte = tbi * 8; | |
247 | ||
248 | if (cfg->tt[0].tsz && | |
249 | !extract64(iova, 64 - cfg->tt[0].tsz, cfg->tt[0].tsz - tbi_byte)) { | |
250 | /* there is a ttbr0 region and we are in it (high bits all zero) */ | |
251 | return &cfg->tt[0]; | |
252 | } else if (cfg->tt[1].tsz && | |
253 | !extract64(iova, 64 - cfg->tt[1].tsz, cfg->tt[1].tsz - tbi_byte)) { | |
254 | /* there is a ttbr1 region and we are in it (high bits all one) */ | |
255 | return &cfg->tt[1]; | |
256 | } else if (!cfg->tt[0].tsz) { | |
257 | /* ttbr0 region is "everything not in the ttbr1 region" */ | |
258 | return &cfg->tt[0]; | |
259 | } else if (!cfg->tt[1].tsz) { | |
260 | /* ttbr1 region is "everything not in the ttbr0 region" */ | |
261 | return &cfg->tt[1]; | |
262 | } | |
263 | /* in the gap between the two regions, this is a Translation fault */ | |
264 | return NULL; | |
265 | } | |
266 | ||
267 | /** | |
268 | * smmu_ptw_64 - VMSAv8-64 Walk of the page tables for a given IOVA | |
269 | * @cfg: translation config | |
270 | * @iova: iova to translate | |
271 | * @perm: access type | |
a7550158 | 272 | * @tlbe: SMMUTLBEntry (out) |
93641948 EA |
273 | * @info: handle to an error info |
274 | * | |
275 | * Return 0 on success, < 0 on error. In case of error, @info is filled | |
276 | * and tlbe->perm is set to IOMMU_NONE. | |
277 | * Upon success, @tlbe is filled with translated_addr and entry | |
278 | * permission rights. | |
279 | */ | |
280 | static int smmu_ptw_64(SMMUTransCfg *cfg, | |
281 | dma_addr_t iova, IOMMUAccessFlags perm, | |
a7550158 | 282 | SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info) |
93641948 EA |
283 | { |
284 | dma_addr_t baseaddr, indexmask; | |
285 | int stage = cfg->stage; | |
286 | SMMUTransTableInfo *tt = select_tt(cfg, iova); | |
287 | uint8_t level, granule_sz, inputsize, stride; | |
288 | ||
289 | if (!tt || tt->disabled) { | |
290 | info->type = SMMU_PTW_ERR_TRANSLATION; | |
291 | goto error; | |
292 | } | |
293 | ||
294 | granule_sz = tt->granule_sz; | |
295 | stride = granule_sz - 3; | |
296 | inputsize = 64 - tt->tsz; | |
297 | level = 4 - (inputsize - 4) / stride; | |
298 | indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1; | |
299 | baseaddr = extract64(tt->ttb, 0, 48); | |
300 | baseaddr &= ~indexmask; | |
301 | ||
93641948 EA |
302 | while (level <= 3) { |
303 | uint64_t subpage_size = 1ULL << level_shift(level, granule_sz); | |
304 | uint64_t mask = subpage_size - 1; | |
305 | uint32_t offset = iova_level_offset(iova, inputsize, level, granule_sz); | |
1733837d | 306 | uint64_t pte, gpa; |
93641948 EA |
307 | dma_addr_t pte_addr = baseaddr + offset * sizeof(pte); |
308 | uint8_t ap; | |
309 | ||
310 | if (get_pte(baseaddr, offset, &pte, info)) { | |
311 | goto error; | |
312 | } | |
313 | trace_smmu_ptw_level(level, iova, subpage_size, | |
314 | baseaddr, offset, pte); | |
315 | ||
316 | if (is_invalid_pte(pte) || is_reserved_pte(pte, level)) { | |
317 | trace_smmu_ptw_invalid_pte(stage, level, baseaddr, | |
318 | pte_addr, offset, pte); | |
1733837d | 319 | break; |
93641948 EA |
320 | } |
321 | ||
1733837d EA |
322 | if (is_table_pte(pte, level)) { |
323 | ap = PTE_APTABLE(pte); | |
93641948 | 324 | |
e7c3b9d9 | 325 | if (is_permission_fault(ap, perm) && !tt->had) { |
93641948 EA |
326 | info->type = SMMU_PTW_ERR_PERMISSION; |
327 | goto error; | |
328 | } | |
1733837d EA |
329 | baseaddr = get_table_pte_address(pte, granule_sz); |
330 | level++; | |
331 | continue; | |
332 | } else if (is_page_pte(pte, level)) { | |
333 | gpa = get_page_pte_address(pte, granule_sz); | |
93641948 EA |
334 | trace_smmu_ptw_page_pte(stage, level, iova, |
335 | baseaddr, pte_addr, pte, gpa); | |
1733837d | 336 | } else { |
93641948 | 337 | uint64_t block_size; |
93641948 | 338 | |
1733837d EA |
339 | gpa = get_block_pte_address(pte, level, granule_sz, |
340 | &block_size); | |
93641948 EA |
341 | trace_smmu_ptw_block_pte(stage, level, baseaddr, |
342 | pte_addr, pte, iova, gpa, | |
343 | block_size >> 20); | |
93641948 | 344 | } |
1733837d | 345 | ap = PTE_AP(pte); |
93641948 EA |
346 | if (is_permission_fault(ap, perm)) { |
347 | info->type = SMMU_PTW_ERR_PERMISSION; | |
348 | goto error; | |
349 | } | |
93641948 | 350 | |
9e54dee7 EA |
351 | tlbe->entry.translated_addr = gpa; |
352 | tlbe->entry.iova = iova & ~mask; | |
353 | tlbe->entry.addr_mask = mask; | |
a7550158 EA |
354 | tlbe->entry.perm = PTE_AP_TO_PERM(ap); |
355 | tlbe->level = level; | |
356 | tlbe->granule = granule_sz; | |
1733837d EA |
357 | return 0; |
358 | } | |
93641948 EA |
359 | info->type = SMMU_PTW_ERR_TRANSLATION; |
360 | ||
361 | error: | |
a7550158 | 362 | tlbe->entry.perm = IOMMU_NONE; |
93641948 EA |
363 | return -EINVAL; |
364 | } | |
365 | ||
366 | /** | |
367 | * smmu_ptw - Walk the page tables for an IOVA, according to @cfg | |
368 | * | |
369 | * @cfg: translation configuration | |
370 | * @iova: iova to translate | |
371 | * @perm: tentative access type | |
372 | * @tlbe: returned entry | |
373 | * @info: ptw event handle | |
374 | * | |
375 | * return 0 on success | |
376 | */ | |
377 | inline int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm, | |
a7550158 | 378 | SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info) |
93641948 EA |
379 | { |
380 | if (!cfg->aa64) { | |
381 | /* | |
382 | * This code path is not entered as we check this while decoding | |
383 | * the configuration data in the derived SMMU model. | |
384 | */ | |
385 | g_assert_not_reached(); | |
386 | } | |
387 | ||
388 | return smmu_ptw_64(cfg, iova, perm, tlbe, info); | |
389 | } | |
527773ee | 390 | |
cac994ef EA |
391 | /** |
392 | * The bus number is used for lookup when SID based invalidation occurs. | |
393 | * In that case we lazily populate the SMMUPciBus array from the bus hash | |
394 | * table. At the time the SMMUPciBus is created (smmu_find_add_as), the bus | |
395 | * numbers may not be always initialized yet. | |
396 | */ | |
397 | SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num) | |
398 | { | |
399 | SMMUPciBus *smmu_pci_bus = s->smmu_pcibus_by_bus_num[bus_num]; | |
5ca0e6fe | 400 | GHashTableIter iter; |
cac994ef | 401 | |
5ca0e6fe PMD |
402 | if (smmu_pci_bus) { |
403 | return smmu_pci_bus; | |
404 | } | |
cac994ef | 405 | |
5ca0e6fe PMD |
406 | g_hash_table_iter_init(&iter, s->smmu_pcibus_by_busptr); |
407 | while (g_hash_table_iter_next(&iter, NULL, (void **)&smmu_pci_bus)) { | |
408 | if (pci_bus_num(smmu_pci_bus->bus) == bus_num) { | |
409 | s->smmu_pcibus_by_bus_num[bus_num] = smmu_pci_bus; | |
410 | return smmu_pci_bus; | |
cac994ef EA |
411 | } |
412 | } | |
5ca0e6fe PMD |
413 | |
414 | return NULL; | |
cac994ef EA |
415 | } |
416 | ||
417 | static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn) | |
418 | { | |
419 | SMMUState *s = opaque; | |
420 | SMMUPciBus *sbus = g_hash_table_lookup(s->smmu_pcibus_by_busptr, bus); | |
421 | SMMUDevice *sdev; | |
6ce9297b | 422 | static unsigned int index; |
cac994ef EA |
423 | |
424 | if (!sbus) { | |
425 | sbus = g_malloc0(sizeof(SMMUPciBus) + | |
426 | sizeof(SMMUDevice *) * SMMU_PCI_DEVFN_MAX); | |
427 | sbus->bus = bus; | |
428 | g_hash_table_insert(s->smmu_pcibus_by_busptr, bus, sbus); | |
429 | } | |
430 | ||
431 | sdev = sbus->pbdev[devfn]; | |
432 | if (!sdev) { | |
6ce9297b EA |
433 | char *name = g_strdup_printf("%s-%d-%d", s->mrtypename, devfn, index++); |
434 | ||
cac994ef EA |
435 | sdev = sbus->pbdev[devfn] = g_new0(SMMUDevice, 1); |
436 | ||
437 | sdev->smmu = s; | |
438 | sdev->bus = bus; | |
439 | sdev->devfn = devfn; | |
440 | ||
441 | memory_region_init_iommu(&sdev->iommu, sizeof(sdev->iommu), | |
442 | s->mrtypename, | |
443 | OBJECT(s), name, 1ULL << SMMU_MAX_VA_BITS); | |
444 | address_space_init(&sdev->as, | |
445 | MEMORY_REGION(&sdev->iommu), name); | |
446 | trace_smmu_add_mr(name); | |
447 | g_free(name); | |
448 | } | |
449 | ||
450 | return &sdev->as; | |
451 | } | |
452 | ||
32cfd7f3 EA |
453 | IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid) |
454 | { | |
455 | uint8_t bus_n, devfn; | |
456 | SMMUPciBus *smmu_bus; | |
457 | SMMUDevice *smmu; | |
458 | ||
459 | bus_n = PCI_BUS_NUM(sid); | |
460 | smmu_bus = smmu_find_smmu_pcibus(s, bus_n); | |
461 | if (smmu_bus) { | |
b78aae9b | 462 | devfn = SMMU_PCI_DEVFN(sid); |
32cfd7f3 EA |
463 | smmu = smmu_bus->pbdev[devfn]; |
464 | if (smmu) { | |
465 | return &smmu->iommu; | |
466 | } | |
467 | } | |
468 | return NULL; | |
469 | } | |
470 | ||
832e4222 EA |
471 | /* Unmap the whole notifier's range */ |
472 | static void smmu_unmap_notifier_range(IOMMUNotifier *n) | |
473 | { | |
5039caf3 | 474 | IOMMUTLBEvent event; |
832e4222 | 475 | |
5039caf3 EP |
476 | event.type = IOMMU_NOTIFIER_UNMAP; |
477 | event.entry.target_as = &address_space_memory; | |
478 | event.entry.iova = n->start; | |
479 | event.entry.perm = IOMMU_NONE; | |
480 | event.entry.addr_mask = n->end - n->start; | |
832e4222 | 481 | |
5039caf3 | 482 | memory_region_notify_iommu_one(n, &event); |
832e4222 EA |
483 | } |
484 | ||
485 | /* Unmap all notifiers attached to @mr */ | |
486 | inline void smmu_inv_notifiers_mr(IOMMUMemoryRegion *mr) | |
487 | { | |
488 | IOMMUNotifier *n; | |
489 | ||
490 | trace_smmu_inv_notifiers_mr(mr->parent_obj.name); | |
491 | IOMMU_NOTIFIER_FOREACH(n, mr) { | |
492 | smmu_unmap_notifier_range(n); | |
493 | } | |
494 | } | |
495 | ||
496 | /* Unmap all notifiers of all mr's */ | |
497 | void smmu_inv_notifiers_all(SMMUState *s) | |
498 | { | |
c6370441 | 499 | SMMUDevice *sdev; |
832e4222 | 500 | |
c6370441 EA |
501 | QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) { |
502 | smmu_inv_notifiers_mr(&sdev->iommu); | |
832e4222 EA |
503 | } |
504 | } | |
505 | ||
527773ee EA |
506 | static void smmu_base_realize(DeviceState *dev, Error **errp) |
507 | { | |
cac994ef | 508 | SMMUState *s = ARM_SMMU(dev); |
527773ee EA |
509 | SMMUBaseClass *sbc = ARM_SMMU_GET_CLASS(dev); |
510 | Error *local_err = NULL; | |
511 | ||
512 | sbc->parent_realize(dev, &local_err); | |
513 | if (local_err) { | |
514 | error_propagate(errp, local_err); | |
515 | return; | |
516 | } | |
32cfd7f3 | 517 | s->configs = g_hash_table_new_full(NULL, NULL, NULL, g_free); |
cc27ed81 EA |
518 | s->iotlb = g_hash_table_new_full(smmu_iotlb_key_hash, smmu_iotlb_key_equal, |
519 | g_free, g_free); | |
cac994ef EA |
520 | s->smmu_pcibus_by_busptr = g_hash_table_new(NULL, NULL); |
521 | ||
522 | if (s->primary_bus) { | |
523 | pci_setup_iommu(s->primary_bus, smmu_find_add_as, s); | |
524 | } else { | |
525 | error_setg(errp, "SMMU is not attached to any PCI bus!"); | |
526 | } | |
527773ee EA |
527 | } |
528 | ||
529 | static void smmu_base_reset(DeviceState *dev) | |
530 | { | |
32cfd7f3 EA |
531 | SMMUState *s = ARM_SMMU(dev); |
532 | ||
533 | g_hash_table_remove_all(s->configs); | |
cc27ed81 | 534 | g_hash_table_remove_all(s->iotlb); |
527773ee EA |
535 | } |
536 | ||
537 | static Property smmu_dev_properties[] = { | |
538 | DEFINE_PROP_UINT8("bus_num", SMMUState, bus_num, 0), | |
539 | DEFINE_PROP_LINK("primary-bus", SMMUState, primary_bus, "PCI", PCIBus *), | |
540 | DEFINE_PROP_END_OF_LIST(), | |
541 | }; | |
542 | ||
543 | static void smmu_base_class_init(ObjectClass *klass, void *data) | |
544 | { | |
545 | DeviceClass *dc = DEVICE_CLASS(klass); | |
546 | SMMUBaseClass *sbc = ARM_SMMU_CLASS(klass); | |
547 | ||
4f67d30b | 548 | device_class_set_props(dc, smmu_dev_properties); |
527773ee EA |
549 | device_class_set_parent_realize(dc, smmu_base_realize, |
550 | &sbc->parent_realize); | |
551 | dc->reset = smmu_base_reset; | |
552 | } | |
553 | ||
554 | static const TypeInfo smmu_base_info = { | |
555 | .name = TYPE_ARM_SMMU, | |
556 | .parent = TYPE_SYS_BUS_DEVICE, | |
557 | .instance_size = sizeof(SMMUState), | |
558 | .class_data = NULL, | |
559 | .class_size = sizeof(SMMUBaseClass), | |
560 | .class_init = smmu_base_class_init, | |
561 | .abstract = true, | |
562 | }; | |
563 | ||
564 | static void smmu_base_register_types(void) | |
565 | { | |
566 | type_register_static(&smmu_base_info); | |
567 | } | |
568 | ||
569 | type_init(smmu_base_register_types) | |
570 |