Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / arm / smmu-common.c
blob0459850a93d92f7d5dabaa5fce148f5d9793a489
1 /*
2 * Copyright (C) 2014-2016 Broadcom Corporation
3 * Copyright (c) 2017 Red Hat, Inc.
4 * Written by Prem Mallappa, Eric Auger
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.
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.
15 * Author: Prem Mallappa <pmallapp@broadcom.com>
19 #include "qemu/osdep.h"
20 #include "trace.h"
21 #include "exec/target_page.h"
22 #include "hw/core/cpu.h"
23 #include "hw/qdev-properties.h"
24 #include "qapi/error.h"
25 #include "qemu/jhash.h"
26 #include "qemu/module.h"
28 #include "qemu/error-report.h"
29 #include "hw/arm/smmu-common.h"
30 #include "smmu-internal.h"
32 /* IOTLB Management */
34 static guint smmu_iotlb_key_hash(gconstpointer v)
36 SMMUIOTLBKey *key = (SMMUIOTLBKey *)v;
37 uint32_t a, b, c;
39 /* Jenkins hash */
40 a = b = c = JHASH_INITVAL + sizeof(*key);
41 a += key->asid + key->level + key->tg;
42 b += extract64(key->iova, 0, 32);
43 c += extract64(key->iova, 32, 32);
45 __jhash_mix(a, b, c);
46 __jhash_final(a, b, c);
48 return c;
51 static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)
53 SMMUIOTLBKey *k1 = (SMMUIOTLBKey *)v1, *k2 = (SMMUIOTLBKey *)v2;
55 return (k1->asid == k2->asid) && (k1->iova == k2->iova) &&
56 (k1->level == k2->level) && (k1->tg == k2->tg);
59 SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova,
60 uint8_t tg, uint8_t level)
62 SMMUIOTLBKey key = {.asid = asid, .iova = iova, .tg = tg, .level = level};
64 return key;
67 SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
68 SMMUTransTableInfo *tt, hwaddr iova)
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;
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;
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;
86 level++;
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));
102 return entry;
105 void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *new)
107 SMMUIOTLBKey *key = g_new0(SMMUIOTLBKey, 1);
108 uint8_t tg = (new->granule - 10) / 2;
110 if (g_hash_table_size(bs->iotlb) >= SMMU_IOTLB_MAX_SIZE) {
111 smmu_iotlb_inv_all(bs);
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);
116 g_hash_table_insert(bs->iotlb, key, new);
119 inline void smmu_iotlb_inv_all(SMMUState *s)
121 trace_smmu_iotlb_inv_all();
122 g_hash_table_remove_all(s->iotlb);
125 static gboolean smmu_hash_remove_by_asid(gpointer key, gpointer value,
126 gpointer user_data)
128 uint16_t asid = *(uint16_t *)user_data;
129 SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
131 return SMMU_IOTLB_ASID(*iotlb_key) == asid;
134 static gboolean smmu_hash_remove_by_asid_iova(gpointer key, gpointer value,
135 gpointer user_data)
137 SMMUTLBEntry *iter = (SMMUTLBEntry *)value;
138 IOMMUTLBEntry *entry = &iter->entry;
139 SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data;
140 SMMUIOTLBKey iotlb_key = *(SMMUIOTLBKey *)key;
142 if (info->asid >= 0 && info->asid != SMMU_IOTLB_ASID(iotlb_key)) {
143 return false;
145 return ((info->iova & ~entry->addr_mask) == entry->iova) ||
146 ((entry->iova & ~info->mask) == info->iova);
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)
153 /* if tg is not set we use 4KB range invalidation */
154 uint8_t granule = tg ? tg * 2 + 10 : 12;
156 if (ttl && (num_pages == 1) && (asid >= 0)) {
157 SMMUIOTLBKey key = smmu_get_iotlb_key(asid, iova, tg, ttl);
159 if (g_hash_table_remove(s->iotlb, &key)) {
160 return;
163 * if the entry is not found, let's see if it does not
164 * belong to a larger IOTLB entry
168 SMMUIOTLBPageInvInfo info = {
169 .asid = asid, .iova = iova,
170 .mask = (num_pages * 1 << granule) - 1};
172 g_hash_table_foreach_remove(s->iotlb,
173 smmu_hash_remove_by_asid_iova,
174 &info);
177 inline void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid)
179 trace_smmu_iotlb_inv_asid(asid);
180 g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_asid, &asid);
183 /* VMSAv8-64 Translation */
186 * get_pte - Get the content of a page table entry located at
187 * @base_addr[@index]
189 static int get_pte(dma_addr_t baseaddr, uint32_t index, uint64_t *pte,
190 SMMUPTWEventInfo *info)
192 int ret;
193 dma_addr_t addr = baseaddr + index * sizeof(*pte);
195 /* TODO: guarantee 64-bit single-copy atomicity */
196 ret = dma_memory_read(&address_space_memory, addr, pte, sizeof(*pte));
198 if (ret != MEMTX_OK) {
199 info->type = SMMU_PTW_ERR_WALK_EABT;
200 info->addr = addr;
201 return -EINVAL;
203 trace_smmu_get_pte(baseaddr, index, addr, *pte);
204 return 0;
207 /* VMSAv8-64 Translation Table Format Descriptor Decoding */
210 * get_page_pte_address - returns the L3 descriptor output address,
211 * ie. the page frame
212 * ARM ARM spec: Figure D4-17 VMSAv8-64 level 3 descriptor format
214 static inline hwaddr get_page_pte_address(uint64_t pte, int granule_sz)
216 return PTE_ADDRESS(pte, granule_sz);
220 * get_table_pte_address - return table descriptor output address,
221 * ie. address of next level table
222 * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
224 static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
226 return PTE_ADDRESS(pte, granule_sz);
230 * get_block_pte_address - return block descriptor output address and block size
231 * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
233 static inline hwaddr get_block_pte_address(uint64_t pte, int level,
234 int granule_sz, uint64_t *bsz)
236 int n = level_shift(level, granule_sz);
238 *bsz = 1ULL << n;
239 return PTE_ADDRESS(pte, n);
242 SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova)
244 bool tbi = extract64(iova, 55, 1) ? TBI1(cfg->tbi) : TBI0(cfg->tbi);
245 uint8_t tbi_byte = tbi * 8;
247 if (cfg->tt[0].tsz &&
248 !extract64(iova, 64 - cfg->tt[0].tsz, cfg->tt[0].tsz - tbi_byte)) {
249 /* there is a ttbr0 region and we are in it (high bits all zero) */
250 return &cfg->tt[0];
251 } else if (cfg->tt[1].tsz &&
252 !extract64(iova, 64 - cfg->tt[1].tsz, cfg->tt[1].tsz - tbi_byte)) {
253 /* there is a ttbr1 region and we are in it (high bits all one) */
254 return &cfg->tt[1];
255 } else if (!cfg->tt[0].tsz) {
256 /* ttbr0 region is "everything not in the ttbr1 region" */
257 return &cfg->tt[0];
258 } else if (!cfg->tt[1].tsz) {
259 /* ttbr1 region is "everything not in the ttbr0 region" */
260 return &cfg->tt[1];
262 /* in the gap between the two regions, this is a Translation fault */
263 return NULL;
267 * smmu_ptw_64 - VMSAv8-64 Walk of the page tables for a given IOVA
268 * @cfg: translation config
269 * @iova: iova to translate
270 * @perm: access type
271 * @tlbe: SMMUTLBEntry (out)
272 * @info: handle to an error info
274 * Return 0 on success, < 0 on error. In case of error, @info is filled
275 * and tlbe->perm is set to IOMMU_NONE.
276 * Upon success, @tlbe is filled with translated_addr and entry
277 * permission rights.
279 static int smmu_ptw_64(SMMUTransCfg *cfg,
280 dma_addr_t iova, IOMMUAccessFlags perm,
281 SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
283 dma_addr_t baseaddr, indexmask;
284 int stage = cfg->stage;
285 SMMUTransTableInfo *tt = select_tt(cfg, iova);
286 uint8_t level, granule_sz, inputsize, stride;
288 if (!tt || tt->disabled) {
289 info->type = SMMU_PTW_ERR_TRANSLATION;
290 goto error;
293 granule_sz = tt->granule_sz;
294 stride = granule_sz - 3;
295 inputsize = 64 - tt->tsz;
296 level = 4 - (inputsize - 4) / stride;
297 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
298 baseaddr = extract64(tt->ttb, 0, 48);
299 baseaddr &= ~indexmask;
301 while (level <= 3) {
302 uint64_t subpage_size = 1ULL << level_shift(level, granule_sz);
303 uint64_t mask = subpage_size - 1;
304 uint32_t offset = iova_level_offset(iova, inputsize, level, granule_sz);
305 uint64_t pte, gpa;
306 dma_addr_t pte_addr = baseaddr + offset * sizeof(pte);
307 uint8_t ap;
309 if (get_pte(baseaddr, offset, &pte, info)) {
310 goto error;
312 trace_smmu_ptw_level(level, iova, subpage_size,
313 baseaddr, offset, pte);
315 if (is_invalid_pte(pte) || is_reserved_pte(pte, level)) {
316 trace_smmu_ptw_invalid_pte(stage, level, baseaddr,
317 pte_addr, offset, pte);
318 break;
321 if (is_table_pte(pte, level)) {
322 ap = PTE_APTABLE(pte);
324 if (is_permission_fault(ap, perm) && !tt->had) {
325 info->type = SMMU_PTW_ERR_PERMISSION;
326 goto error;
328 baseaddr = get_table_pte_address(pte, granule_sz);
329 level++;
330 continue;
331 } else if (is_page_pte(pte, level)) {
332 gpa = get_page_pte_address(pte, granule_sz);
333 trace_smmu_ptw_page_pte(stage, level, iova,
334 baseaddr, pte_addr, pte, gpa);
335 } else {
336 uint64_t block_size;
338 gpa = get_block_pte_address(pte, level, granule_sz,
339 &block_size);
340 trace_smmu_ptw_block_pte(stage, level, baseaddr,
341 pte_addr, pte, iova, gpa,
342 block_size >> 20);
344 ap = PTE_AP(pte);
345 if (is_permission_fault(ap, perm)) {
346 info->type = SMMU_PTW_ERR_PERMISSION;
347 goto error;
350 tlbe->entry.translated_addr = gpa;
351 tlbe->entry.iova = iova & ~mask;
352 tlbe->entry.addr_mask = mask;
353 tlbe->entry.perm = PTE_AP_TO_PERM(ap);
354 tlbe->level = level;
355 tlbe->granule = granule_sz;
356 return 0;
358 info->type = SMMU_PTW_ERR_TRANSLATION;
360 error:
361 tlbe->entry.perm = IOMMU_NONE;
362 return -EINVAL;
366 * smmu_ptw - Walk the page tables for an IOVA, according to @cfg
368 * @cfg: translation configuration
369 * @iova: iova to translate
370 * @perm: tentative access type
371 * @tlbe: returned entry
372 * @info: ptw event handle
374 * return 0 on success
376 inline int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,
377 SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
379 if (!cfg->aa64) {
381 * This code path is not entered as we check this while decoding
382 * the configuration data in the derived SMMU model.
384 g_assert_not_reached();
387 return smmu_ptw_64(cfg, iova, perm, tlbe, info);
391 * The bus number is used for lookup when SID based invalidation occurs.
392 * In that case we lazily populate the SMMUPciBus array from the bus hash
393 * table. At the time the SMMUPciBus is created (smmu_find_add_as), the bus
394 * numbers may not be always initialized yet.
396 SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num)
398 SMMUPciBus *smmu_pci_bus = s->smmu_pcibus_by_bus_num[bus_num];
399 GHashTableIter iter;
401 if (smmu_pci_bus) {
402 return smmu_pci_bus;
405 g_hash_table_iter_init(&iter, s->smmu_pcibus_by_busptr);
406 while (g_hash_table_iter_next(&iter, NULL, (void **)&smmu_pci_bus)) {
407 if (pci_bus_num(smmu_pci_bus->bus) == bus_num) {
408 s->smmu_pcibus_by_bus_num[bus_num] = smmu_pci_bus;
409 return smmu_pci_bus;
413 return NULL;
416 static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn)
418 SMMUState *s = opaque;
419 SMMUPciBus *sbus = g_hash_table_lookup(s->smmu_pcibus_by_busptr, bus);
420 SMMUDevice *sdev;
421 static unsigned int index;
423 if (!sbus) {
424 sbus = g_malloc0(sizeof(SMMUPciBus) +
425 sizeof(SMMUDevice *) * SMMU_PCI_DEVFN_MAX);
426 sbus->bus = bus;
427 g_hash_table_insert(s->smmu_pcibus_by_busptr, bus, sbus);
430 sdev = sbus->pbdev[devfn];
431 if (!sdev) {
432 char *name = g_strdup_printf("%s-%d-%d", s->mrtypename, devfn, index++);
434 sdev = sbus->pbdev[devfn] = g_new0(SMMUDevice, 1);
436 sdev->smmu = s;
437 sdev->bus = bus;
438 sdev->devfn = devfn;
440 memory_region_init_iommu(&sdev->iommu, sizeof(sdev->iommu),
441 s->mrtypename,
442 OBJECT(s), name, 1ULL << SMMU_MAX_VA_BITS);
443 address_space_init(&sdev->as,
444 MEMORY_REGION(&sdev->iommu), name);
445 trace_smmu_add_mr(name);
446 g_free(name);
449 return &sdev->as;
452 IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid)
454 uint8_t bus_n, devfn;
455 SMMUPciBus *smmu_bus;
456 SMMUDevice *smmu;
458 bus_n = PCI_BUS_NUM(sid);
459 smmu_bus = smmu_find_smmu_pcibus(s, bus_n);
460 if (smmu_bus) {
461 devfn = SMMU_PCI_DEVFN(sid);
462 smmu = smmu_bus->pbdev[devfn];
463 if (smmu) {
464 return &smmu->iommu;
467 return NULL;
470 /* Unmap the whole notifier's range */
471 static void smmu_unmap_notifier_range(IOMMUNotifier *n)
473 IOMMUTLBEvent event;
475 event.type = IOMMU_NOTIFIER_UNMAP;
476 event.entry.target_as = &address_space_memory;
477 event.entry.iova = n->start;
478 event.entry.perm = IOMMU_NONE;
479 event.entry.addr_mask = n->end - n->start;
481 memory_region_notify_iommu_one(n, &event);
484 /* Unmap all notifiers attached to @mr */
485 inline void smmu_inv_notifiers_mr(IOMMUMemoryRegion *mr)
487 IOMMUNotifier *n;
489 trace_smmu_inv_notifiers_mr(mr->parent_obj.name);
490 IOMMU_NOTIFIER_FOREACH(n, mr) {
491 smmu_unmap_notifier_range(n);
495 /* Unmap all notifiers of all mr's */
496 void smmu_inv_notifiers_all(SMMUState *s)
498 SMMUDevice *sdev;
500 QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) {
501 smmu_inv_notifiers_mr(&sdev->iommu);
505 static void smmu_base_realize(DeviceState *dev, Error **errp)
507 SMMUState *s = ARM_SMMU(dev);
508 SMMUBaseClass *sbc = ARM_SMMU_GET_CLASS(dev);
509 Error *local_err = NULL;
511 sbc->parent_realize(dev, &local_err);
512 if (local_err) {
513 error_propagate(errp, local_err);
514 return;
516 s->configs = g_hash_table_new_full(NULL, NULL, NULL, g_free);
517 s->iotlb = g_hash_table_new_full(smmu_iotlb_key_hash, smmu_iotlb_key_equal,
518 g_free, g_free);
519 s->smmu_pcibus_by_busptr = g_hash_table_new(NULL, NULL);
521 if (s->primary_bus) {
522 pci_setup_iommu(s->primary_bus, smmu_find_add_as, s);
523 } else {
524 error_setg(errp, "SMMU is not attached to any PCI bus!");
528 static void smmu_base_reset(DeviceState *dev)
530 SMMUState *s = ARM_SMMU(dev);
532 g_hash_table_remove_all(s->configs);
533 g_hash_table_remove_all(s->iotlb);
536 static Property smmu_dev_properties[] = {
537 DEFINE_PROP_UINT8("bus_num", SMMUState, bus_num, 0),
538 DEFINE_PROP_LINK("primary-bus", SMMUState, primary_bus, "PCI", PCIBus *),
539 DEFINE_PROP_END_OF_LIST(),
542 static void smmu_base_class_init(ObjectClass *klass, void *data)
544 DeviceClass *dc = DEVICE_CLASS(klass);
545 SMMUBaseClass *sbc = ARM_SMMU_CLASS(klass);
547 device_class_set_props(dc, smmu_dev_properties);
548 device_class_set_parent_realize(dc, smmu_base_realize,
549 &sbc->parent_realize);
550 dc->reset = smmu_base_reset;
553 static const TypeInfo smmu_base_info = {
554 .name = TYPE_ARM_SMMU,
555 .parent = TYPE_SYS_BUS_DEVICE,
556 .instance_size = sizeof(SMMUState),
557 .class_data = NULL,
558 .class_size = sizeof(SMMUBaseClass),
559 .class_init = smmu_base_class_init,
560 .abstract = true,
563 static void smmu_base_register_types(void)
565 type_register_static(&smmu_base_info);
568 type_init(smmu_base_register_types)