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 "exec/address-spaces.h"
22 #include "exec/target_page.h"
23 #include "hw/core/cpu.h"
24 #include "hw/qdev-properties.h"
25 #include "qapi/error.h"
26 #include "qemu/jhash.h"
27 #include "qemu/module.h"
29 #include "qemu/error-report.h"
30 #include "hw/arm/smmu-common.h"
31 #include "smmu-internal.h"
33 /* IOTLB Management */
35 static guint
smmu_iotlb_key_hash(gconstpointer v
)
37 SMMUIOTLBKey
*key
= (SMMUIOTLBKey
*)v
;
41 a
= b
= c
= JHASH_INITVAL
+ sizeof(*key
);
42 a
+= key
->asid
+ key
->level
+ key
->tg
;
43 b
+= extract64(key
->iova
, 0, 32);
44 c
+= extract64(key
->iova
, 32, 32);
47 __jhash_final(a
, b
, c
);
52 static gboolean
smmu_iotlb_key_equal(gconstpointer v1
, gconstpointer v2
)
54 SMMUIOTLBKey
*k1
= (SMMUIOTLBKey
*)v1
, *k2
= (SMMUIOTLBKey
*)v2
;
56 return (k1
->asid
== k2
->asid
) && (k1
->iova
== k2
->iova
) &&
57 (k1
->level
== k2
->level
) && (k1
->tg
== k2
->tg
);
60 SMMUIOTLBKey
smmu_get_iotlb_key(uint16_t asid
, uint64_t iova
,
61 uint8_t tg
, uint8_t level
)
63 SMMUIOTLBKey key
= {.asid
= asid
, .iova
= iova
, .tg
= tg
, .level
= level
};
68 SMMUTLBEntry
*smmu_iotlb_lookup(SMMUState
*bs
, SMMUTransCfg
*cfg
,
69 SMMUTransTableInfo
*tt
, hwaddr iova
)
71 uint8_t tg
= (tt
->granule_sz
- 10) / 2;
72 uint8_t inputsize
= 64 - tt
->tsz
;
73 uint8_t stride
= tt
->granule_sz
- 3;
74 uint8_t level
= 4 - (inputsize
- 4) / stride
;
75 SMMUTLBEntry
*entry
= NULL
;
78 uint64_t subpage_size
= 1ULL << level_shift(level
, tt
->granule_sz
);
79 uint64_t mask
= subpage_size
- 1;
82 key
= smmu_get_iotlb_key(cfg
->asid
, iova
& ~mask
, tg
, level
);
83 entry
= g_hash_table_lookup(bs
->iotlb
, &key
);
92 trace_smmu_iotlb_lookup_hit(cfg
->asid
, iova
,
93 cfg
->iotlb_hits
, cfg
->iotlb_misses
,
94 100 * cfg
->iotlb_hits
/
95 (cfg
->iotlb_hits
+ cfg
->iotlb_misses
));
98 trace_smmu_iotlb_lookup_miss(cfg
->asid
, iova
,
99 cfg
->iotlb_hits
, cfg
->iotlb_misses
,
100 100 * cfg
->iotlb_hits
/
101 (cfg
->iotlb_hits
+ cfg
->iotlb_misses
));
106 void smmu_iotlb_insert(SMMUState
*bs
, SMMUTransCfg
*cfg
, SMMUTLBEntry
*new)
108 SMMUIOTLBKey
*key
= g_new0(SMMUIOTLBKey
, 1);
109 uint8_t tg
= (new->granule
- 10) / 2;
111 if (g_hash_table_size(bs
->iotlb
) >= SMMU_IOTLB_MAX_SIZE
) {
112 smmu_iotlb_inv_all(bs
);
115 *key
= smmu_get_iotlb_key(cfg
->asid
, new->entry
.iova
, tg
, new->level
);
116 trace_smmu_iotlb_insert(cfg
->asid
, new->entry
.iova
, tg
, new->level
);
117 g_hash_table_insert(bs
->iotlb
, key
, new);
120 inline void smmu_iotlb_inv_all(SMMUState
*s
)
122 trace_smmu_iotlb_inv_all();
123 g_hash_table_remove_all(s
->iotlb
);
126 static gboolean
smmu_hash_remove_by_asid(gpointer key
, gpointer value
,
129 uint16_t asid
= *(uint16_t *)user_data
;
130 SMMUIOTLBKey
*iotlb_key
= (SMMUIOTLBKey
*)key
;
132 return SMMU_IOTLB_ASID(*iotlb_key
) == asid
;
135 static gboolean
smmu_hash_remove_by_asid_iova(gpointer key
, gpointer value
,
138 SMMUTLBEntry
*iter
= (SMMUTLBEntry
*)value
;
139 IOMMUTLBEntry
*entry
= &iter
->entry
;
140 SMMUIOTLBPageInvInfo
*info
= (SMMUIOTLBPageInvInfo
*)user_data
;
141 SMMUIOTLBKey iotlb_key
= *(SMMUIOTLBKey
*)key
;
143 if (info
->asid
>= 0 && info
->asid
!= SMMU_IOTLB_ASID(iotlb_key
)) {
146 return ((info
->iova
& ~entry
->addr_mask
) == entry
->iova
) ||
147 ((entry
->iova
& ~info
->mask
) == info
->iova
);
151 smmu_iotlb_inv_iova(SMMUState
*s
, int asid
, dma_addr_t iova
,
152 uint8_t tg
, uint64_t num_pages
, uint8_t ttl
)
154 if (ttl
&& (num_pages
== 1)) {
155 SMMUIOTLBKey key
= smmu_get_iotlb_key(asid
, iova
, tg
, ttl
);
157 g_hash_table_remove(s
->iotlb
, &key
);
159 /* if tg is not set we use 4KB range invalidation */
160 uint8_t granule
= tg
? tg
* 2 + 10 : 12;
162 SMMUIOTLBPageInvInfo info
= {
163 .asid
= asid
, .iova
= iova
,
164 .mask
= (num_pages
* 1 << granule
) - 1};
166 g_hash_table_foreach_remove(s
->iotlb
,
167 smmu_hash_remove_by_asid_iova
,
172 inline void smmu_iotlb_inv_asid(SMMUState
*s
, uint16_t asid
)
174 trace_smmu_iotlb_inv_asid(asid
);
175 g_hash_table_foreach_remove(s
->iotlb
, smmu_hash_remove_by_asid
, &asid
);
178 /* VMSAv8-64 Translation */
181 * get_pte - Get the content of a page table entry located at
184 static int get_pte(dma_addr_t baseaddr
, uint32_t index
, uint64_t *pte
,
185 SMMUPTWEventInfo
*info
)
188 dma_addr_t addr
= baseaddr
+ index
* sizeof(*pte
);
190 /* TODO: guarantee 64-bit single-copy atomicity */
191 ret
= dma_memory_read(&address_space_memory
, addr
, pte
, sizeof(*pte
));
193 if (ret
!= MEMTX_OK
) {
194 info
->type
= SMMU_PTW_ERR_WALK_EABT
;
198 trace_smmu_get_pte(baseaddr
, index
, addr
, *pte
);
202 /* VMSAv8-64 Translation Table Format Descriptor Decoding */
205 * get_page_pte_address - returns the L3 descriptor output address,
207 * ARM ARM spec: Figure D4-17 VMSAv8-64 level 3 descriptor format
209 static inline hwaddr
get_page_pte_address(uint64_t pte
, int granule_sz
)
211 return PTE_ADDRESS(pte
, granule_sz
);
215 * get_table_pte_address - return table descriptor output address,
216 * ie. address of next level table
217 * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
219 static inline hwaddr
get_table_pte_address(uint64_t pte
, int granule_sz
)
221 return PTE_ADDRESS(pte
, granule_sz
);
225 * get_block_pte_address - return block descriptor output address and block size
226 * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
228 static inline hwaddr
get_block_pte_address(uint64_t pte
, int level
,
229 int granule_sz
, uint64_t *bsz
)
231 int n
= level_shift(level
, granule_sz
);
234 return PTE_ADDRESS(pte
, n
);
237 SMMUTransTableInfo
*select_tt(SMMUTransCfg
*cfg
, dma_addr_t iova
)
239 bool tbi
= extract64(iova
, 55, 1) ? TBI1(cfg
->tbi
) : TBI0(cfg
->tbi
);
240 uint8_t tbi_byte
= tbi
* 8;
242 if (cfg
->tt
[0].tsz
&&
243 !extract64(iova
, 64 - cfg
->tt
[0].tsz
, cfg
->tt
[0].tsz
- tbi_byte
)) {
244 /* there is a ttbr0 region and we are in it (high bits all zero) */
246 } else if (cfg
->tt
[1].tsz
&&
247 !extract64(iova
, 64 - cfg
->tt
[1].tsz
, cfg
->tt
[1].tsz
- tbi_byte
)) {
248 /* there is a ttbr1 region and we are in it (high bits all one) */
250 } else if (!cfg
->tt
[0].tsz
) {
251 /* ttbr0 region is "everything not in the ttbr1 region" */
253 } else if (!cfg
->tt
[1].tsz
) {
254 /* ttbr1 region is "everything not in the ttbr0 region" */
257 /* in the gap between the two regions, this is a Translation fault */
262 * smmu_ptw_64 - VMSAv8-64 Walk of the page tables for a given IOVA
263 * @cfg: translation config
264 * @iova: iova to translate
266 * @tlbe: SMMUTLBEntry (out)
267 * @info: handle to an error info
269 * Return 0 on success, < 0 on error. In case of error, @info is filled
270 * and tlbe->perm is set to IOMMU_NONE.
271 * Upon success, @tlbe is filled with translated_addr and entry
274 static int smmu_ptw_64(SMMUTransCfg
*cfg
,
275 dma_addr_t iova
, IOMMUAccessFlags perm
,
276 SMMUTLBEntry
*tlbe
, SMMUPTWEventInfo
*info
)
278 dma_addr_t baseaddr
, indexmask
;
279 int stage
= cfg
->stage
;
280 SMMUTransTableInfo
*tt
= select_tt(cfg
, iova
);
281 uint8_t level
, granule_sz
, inputsize
, stride
;
283 if (!tt
|| tt
->disabled
) {
284 info
->type
= SMMU_PTW_ERR_TRANSLATION
;
288 granule_sz
= tt
->granule_sz
;
289 stride
= granule_sz
- 3;
290 inputsize
= 64 - tt
->tsz
;
291 level
= 4 - (inputsize
- 4) / stride
;
292 indexmask
= (1ULL << (inputsize
- (stride
* (4 - level
)))) - 1;
293 baseaddr
= extract64(tt
->ttb
, 0, 48);
294 baseaddr
&= ~indexmask
;
297 uint64_t subpage_size
= 1ULL << level_shift(level
, granule_sz
);
298 uint64_t mask
= subpage_size
- 1;
299 uint32_t offset
= iova_level_offset(iova
, inputsize
, level
, granule_sz
);
301 dma_addr_t pte_addr
= baseaddr
+ offset
* sizeof(pte
);
304 if (get_pte(baseaddr
, offset
, &pte
, info
)) {
307 trace_smmu_ptw_level(level
, iova
, subpage_size
,
308 baseaddr
, offset
, pte
);
310 if (is_invalid_pte(pte
) || is_reserved_pte(pte
, level
)) {
311 trace_smmu_ptw_invalid_pte(stage
, level
, baseaddr
,
312 pte_addr
, offset
, pte
);
316 if (is_table_pte(pte
, level
)) {
317 ap
= PTE_APTABLE(pte
);
319 if (is_permission_fault(ap
, perm
) && !tt
->had
) {
320 info
->type
= SMMU_PTW_ERR_PERMISSION
;
323 baseaddr
= get_table_pte_address(pte
, granule_sz
);
326 } else if (is_page_pte(pte
, level
)) {
327 gpa
= get_page_pte_address(pte
, granule_sz
);
328 trace_smmu_ptw_page_pte(stage
, level
, iova
,
329 baseaddr
, pte_addr
, pte
, gpa
);
333 gpa
= get_block_pte_address(pte
, level
, granule_sz
,
335 trace_smmu_ptw_block_pte(stage
, level
, baseaddr
,
336 pte_addr
, pte
, iova
, gpa
,
340 if (is_permission_fault(ap
, perm
)) {
341 info
->type
= SMMU_PTW_ERR_PERMISSION
;
345 tlbe
->entry
.translated_addr
= gpa
;
346 tlbe
->entry
.iova
= iova
& ~mask
;
347 tlbe
->entry
.addr_mask
= mask
;
348 tlbe
->entry
.perm
= PTE_AP_TO_PERM(ap
);
350 tlbe
->granule
= granule_sz
;
353 info
->type
= SMMU_PTW_ERR_TRANSLATION
;
356 tlbe
->entry
.perm
= IOMMU_NONE
;
361 * smmu_ptw - Walk the page tables for an IOVA, according to @cfg
363 * @cfg: translation configuration
364 * @iova: iova to translate
365 * @perm: tentative access type
366 * @tlbe: returned entry
367 * @info: ptw event handle
369 * return 0 on success
371 inline int smmu_ptw(SMMUTransCfg
*cfg
, dma_addr_t iova
, IOMMUAccessFlags perm
,
372 SMMUTLBEntry
*tlbe
, SMMUPTWEventInfo
*info
)
376 * This code path is not entered as we check this while decoding
377 * the configuration data in the derived SMMU model.
379 g_assert_not_reached();
382 return smmu_ptw_64(cfg
, iova
, perm
, tlbe
, info
);
386 * The bus number is used for lookup when SID based invalidation occurs.
387 * In that case we lazily populate the SMMUPciBus array from the bus hash
388 * table. At the time the SMMUPciBus is created (smmu_find_add_as), the bus
389 * numbers may not be always initialized yet.
391 SMMUPciBus
*smmu_find_smmu_pcibus(SMMUState
*s
, uint8_t bus_num
)
393 SMMUPciBus
*smmu_pci_bus
= s
->smmu_pcibus_by_bus_num
[bus_num
];
400 g_hash_table_iter_init(&iter
, s
->smmu_pcibus_by_busptr
);
401 while (g_hash_table_iter_next(&iter
, NULL
, (void **)&smmu_pci_bus
)) {
402 if (pci_bus_num(smmu_pci_bus
->bus
) == bus_num
) {
403 s
->smmu_pcibus_by_bus_num
[bus_num
] = smmu_pci_bus
;
411 static AddressSpace
*smmu_find_add_as(PCIBus
*bus
, void *opaque
, int devfn
)
413 SMMUState
*s
= opaque
;
414 SMMUPciBus
*sbus
= g_hash_table_lookup(s
->smmu_pcibus_by_busptr
, bus
);
416 static unsigned int index
;
419 sbus
= g_malloc0(sizeof(SMMUPciBus
) +
420 sizeof(SMMUDevice
*) * SMMU_PCI_DEVFN_MAX
);
422 g_hash_table_insert(s
->smmu_pcibus_by_busptr
, bus
, sbus
);
425 sdev
= sbus
->pbdev
[devfn
];
427 char *name
= g_strdup_printf("%s-%d-%d", s
->mrtypename
, devfn
, index
++);
429 sdev
= sbus
->pbdev
[devfn
] = g_new0(SMMUDevice
, 1);
435 memory_region_init_iommu(&sdev
->iommu
, sizeof(sdev
->iommu
),
437 OBJECT(s
), name
, 1ULL << SMMU_MAX_VA_BITS
);
438 address_space_init(&sdev
->as
,
439 MEMORY_REGION(&sdev
->iommu
), name
);
440 trace_smmu_add_mr(name
);
447 IOMMUMemoryRegion
*smmu_iommu_mr(SMMUState
*s
, uint32_t sid
)
449 uint8_t bus_n
, devfn
;
450 SMMUPciBus
*smmu_bus
;
453 bus_n
= PCI_BUS_NUM(sid
);
454 smmu_bus
= smmu_find_smmu_pcibus(s
, bus_n
);
456 devfn
= SMMU_PCI_DEVFN(sid
);
457 smmu
= smmu_bus
->pbdev
[devfn
];
465 /* Unmap the whole notifier's range */
466 static void smmu_unmap_notifier_range(IOMMUNotifier
*n
)
470 event
.type
= IOMMU_NOTIFIER_UNMAP
;
471 event
.entry
.target_as
= &address_space_memory
;
472 event
.entry
.iova
= n
->start
;
473 event
.entry
.perm
= IOMMU_NONE
;
474 event
.entry
.addr_mask
= n
->end
- n
->start
;
476 memory_region_notify_iommu_one(n
, &event
);
479 /* Unmap all notifiers attached to @mr */
480 inline void smmu_inv_notifiers_mr(IOMMUMemoryRegion
*mr
)
484 trace_smmu_inv_notifiers_mr(mr
->parent_obj
.name
);
485 IOMMU_NOTIFIER_FOREACH(n
, mr
) {
486 smmu_unmap_notifier_range(n
);
490 /* Unmap all notifiers of all mr's */
491 void smmu_inv_notifiers_all(SMMUState
*s
)
495 QLIST_FOREACH(sdev
, &s
->devices_with_notifiers
, next
) {
496 smmu_inv_notifiers_mr(&sdev
->iommu
);
500 static void smmu_base_realize(DeviceState
*dev
, Error
**errp
)
502 SMMUState
*s
= ARM_SMMU(dev
);
503 SMMUBaseClass
*sbc
= ARM_SMMU_GET_CLASS(dev
);
504 Error
*local_err
= NULL
;
506 sbc
->parent_realize(dev
, &local_err
);
508 error_propagate(errp
, local_err
);
511 s
->configs
= g_hash_table_new_full(NULL
, NULL
, NULL
, g_free
);
512 s
->iotlb
= g_hash_table_new_full(smmu_iotlb_key_hash
, smmu_iotlb_key_equal
,
514 s
->smmu_pcibus_by_busptr
= g_hash_table_new(NULL
, NULL
);
516 if (s
->primary_bus
) {
517 pci_setup_iommu(s
->primary_bus
, smmu_find_add_as
, s
);
519 error_setg(errp
, "SMMU is not attached to any PCI bus!");
523 static void smmu_base_reset(DeviceState
*dev
)
525 SMMUState
*s
= ARM_SMMU(dev
);
527 g_hash_table_remove_all(s
->configs
);
528 g_hash_table_remove_all(s
->iotlb
);
531 static Property smmu_dev_properties
[] = {
532 DEFINE_PROP_UINT8("bus_num", SMMUState
, bus_num
, 0),
533 DEFINE_PROP_LINK("primary-bus", SMMUState
, primary_bus
, "PCI", PCIBus
*),
534 DEFINE_PROP_END_OF_LIST(),
537 static void smmu_base_class_init(ObjectClass
*klass
, void *data
)
539 DeviceClass
*dc
= DEVICE_CLASS(klass
);
540 SMMUBaseClass
*sbc
= ARM_SMMU_CLASS(klass
);
542 device_class_set_props(dc
, smmu_dev_properties
);
543 device_class_set_parent_realize(dc
, smmu_base_realize
,
544 &sbc
->parent_realize
);
545 dc
->reset
= smmu_base_reset
;
548 static const TypeInfo smmu_base_info
= {
549 .name
= TYPE_ARM_SMMU
,
550 .parent
= TYPE_SYS_BUS_DEVICE
,
551 .instance_size
= sizeof(SMMUState
),
553 .class_size
= sizeof(SMMUBaseClass
),
554 .class_init
= smmu_base_class_init
,
558 static void smmu_base_register_types(void)
560 type_register_static(&smmu_base_info
);
563 type_init(smmu_base_register_types
)