graph-lock: TSA annotations for lock/unlock functions
[qemu.git] / hw / arm / smmu-common.c
blobe09b9c13b7458c2007c74853e7d9f7af50ba77cb
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),
197 MEMTXATTRS_UNSPECIFIED);
199 if (ret != MEMTX_OK) {
200 info->type = SMMU_PTW_ERR_WALK_EABT;
201 info->addr = addr;
202 return -EINVAL;
204 trace_smmu_get_pte(baseaddr, index, addr, *pte);
205 return 0;
208 /* VMSAv8-64 Translation Table Format Descriptor Decoding */
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
215 static inline hwaddr get_page_pte_address(uint64_t pte, int granule_sz)
217 return PTE_ADDRESS(pte, granule_sz);
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
225 static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
227 return PTE_ADDRESS(pte, granule_sz);
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
234 static inline hwaddr get_block_pte_address(uint64_t pte, int level,
235 int granule_sz, uint64_t *bsz)
237 int n = level_shift(level, granule_sz);
239 *bsz = 1ULL << n;
240 return PTE_ADDRESS(pte, n);
243 SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova)
245 bool tbi = extract64(iova, 55, 1) ? TBI1(cfg->tbi) : TBI0(cfg->tbi);
246 uint8_t tbi_byte = tbi * 8;
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];
263 /* in the gap between the two regions, this is a Translation fault */
264 return NULL;
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
272 * @tlbe: SMMUTLBEntry (out)
273 * @info: handle to an error info
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.
280 static int smmu_ptw_64(SMMUTransCfg *cfg,
281 dma_addr_t iova, IOMMUAccessFlags perm,
282 SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
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;
289 if (!tt || tt->disabled) {
290 info->type = SMMU_PTW_ERR_TRANSLATION;
291 goto error;
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;
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);
306 uint64_t pte, gpa;
307 dma_addr_t pte_addr = baseaddr + offset * sizeof(pte);
308 uint8_t ap;
310 if (get_pte(baseaddr, offset, &pte, info)) {
311 goto error;
313 trace_smmu_ptw_level(level, iova, subpage_size,
314 baseaddr, offset, pte);
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);
319 break;
322 if (is_table_pte(pte, level)) {
323 ap = PTE_APTABLE(pte);
325 if (is_permission_fault(ap, perm) && !tt->had) {
326 info->type = SMMU_PTW_ERR_PERMISSION;
327 goto error;
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);
334 trace_smmu_ptw_page_pte(stage, level, iova,
335 baseaddr, pte_addr, pte, gpa);
336 } else {
337 uint64_t block_size;
339 gpa = get_block_pte_address(pte, level, granule_sz,
340 &block_size);
341 trace_smmu_ptw_block_pte(stage, level, baseaddr,
342 pte_addr, pte, iova, gpa,
343 block_size >> 20);
345 ap = PTE_AP(pte);
346 if (is_permission_fault(ap, perm)) {
347 info->type = SMMU_PTW_ERR_PERMISSION;
348 goto error;
351 tlbe->entry.translated_addr = gpa;
352 tlbe->entry.iova = iova & ~mask;
353 tlbe->entry.addr_mask = mask;
354 tlbe->entry.perm = PTE_AP_TO_PERM(ap);
355 tlbe->level = level;
356 tlbe->granule = granule_sz;
357 return 0;
359 info->type = SMMU_PTW_ERR_TRANSLATION;
361 error:
362 tlbe->entry.perm = IOMMU_NONE;
363 return -EINVAL;
367 * smmu_ptw - Walk the page tables for an IOVA, according to @cfg
369 * @cfg: translation configuration
370 * @iova: iova to translate
371 * @perm: tentative access type
372 * @tlbe: returned entry
373 * @info: ptw event handle
375 * return 0 on success
377 inline int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,
378 SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
380 if (!cfg->aa64) {
382 * This code path is not entered as we check this while decoding
383 * the configuration data in the derived SMMU model.
385 g_assert_not_reached();
388 return smmu_ptw_64(cfg, iova, perm, tlbe, info);
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.
397 SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num)
399 SMMUPciBus *smmu_pci_bus = s->smmu_pcibus_by_bus_num[bus_num];
400 GHashTableIter iter;
402 if (smmu_pci_bus) {
403 return smmu_pci_bus;
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;
414 return NULL;
417 static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn)
419 SMMUState *s = opaque;
420 SMMUPciBus *sbus = g_hash_table_lookup(s->smmu_pcibus_by_busptr, bus);
421 SMMUDevice *sdev;
422 static unsigned int index;
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);
431 sdev = sbus->pbdev[devfn];
432 if (!sdev) {
433 char *name = g_strdup_printf("%s-%d-%d", s->mrtypename, devfn, index++);
435 sdev = sbus->pbdev[devfn] = g_new0(SMMUDevice, 1);
437 sdev->smmu = s;
438 sdev->bus = bus;
439 sdev->devfn = devfn;
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);
450 return &sdev->as;
453 IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid)
455 uint8_t bus_n, devfn;
456 SMMUPciBus *smmu_bus;
457 SMMUDevice *smmu;
459 bus_n = PCI_BUS_NUM(sid);
460 smmu_bus = smmu_find_smmu_pcibus(s, bus_n);
461 if (smmu_bus) {
462 devfn = SMMU_PCI_DEVFN(sid);
463 smmu = smmu_bus->pbdev[devfn];
464 if (smmu) {
465 return &smmu->iommu;
468 return NULL;
471 /* Unmap the whole notifier's range */
472 static void smmu_unmap_notifier_range(IOMMUNotifier *n)
474 IOMMUTLBEvent event;
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;
482 memory_region_notify_iommu_one(n, &event);
485 /* Unmap all notifiers attached to @mr */
486 inline void smmu_inv_notifiers_mr(IOMMUMemoryRegion *mr)
488 IOMMUNotifier *n;
490 trace_smmu_inv_notifiers_mr(mr->parent_obj.name);
491 IOMMU_NOTIFIER_FOREACH(n, mr) {
492 smmu_unmap_notifier_range(n);
496 /* Unmap all notifiers of all mr's */
497 void smmu_inv_notifiers_all(SMMUState *s)
499 SMMUDevice *sdev;
501 QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) {
502 smmu_inv_notifiers_mr(&sdev->iommu);
506 static void smmu_base_realize(DeviceState *dev, Error **errp)
508 SMMUState *s = ARM_SMMU(dev);
509 SMMUBaseClass *sbc = ARM_SMMU_GET_CLASS(dev);
510 Error *local_err = NULL;
512 sbc->parent_realize(dev, &local_err);
513 if (local_err) {
514 error_propagate(errp, local_err);
515 return;
517 s->configs = g_hash_table_new_full(NULL, NULL, NULL, g_free);
518 s->iotlb = g_hash_table_new_full(smmu_iotlb_key_hash, smmu_iotlb_key_equal,
519 g_free, g_free);
520 s->smmu_pcibus_by_busptr = g_hash_table_new(NULL, NULL);
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!");
529 static void smmu_base_reset(DeviceState *dev)
531 SMMUState *s = ARM_SMMU(dev);
533 g_hash_table_remove_all(s->configs);
534 g_hash_table_remove_all(s->iotlb);
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(),
543 static void smmu_base_class_init(ObjectClass *klass, void *data)
545 DeviceClass *dc = DEVICE_CLASS(klass);
546 SMMUBaseClass *sbc = ARM_SMMU_CLASS(klass);
548 device_class_set_props(dc, smmu_dev_properties);
549 device_class_set_parent_realize(dc, smmu_base_realize,
550 &sbc->parent_realize);
551 dc->reset = smmu_base_reset;
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,
564 static void smmu_base_register_types(void)
566 type_register_static(&smmu_base_info);
569 type_init(smmu_base_register_types)