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 inline void smmu_iotlb_inv_all(SMMUState
*s
)
37 trace_smmu_iotlb_inv_all();
38 g_hash_table_remove_all(s
->iotlb
);
41 static gboolean
smmu_hash_remove_by_asid(gpointer key
, gpointer value
,
44 uint16_t asid
= *(uint16_t *)user_data
;
45 SMMUIOTLBKey
*iotlb_key
= (SMMUIOTLBKey
*)key
;
47 return iotlb_key
->asid
== asid
;
50 inline void smmu_iotlb_inv_iova(SMMUState
*s
, uint16_t asid
, dma_addr_t iova
)
52 SMMUIOTLBKey key
= {.asid
= asid
, .iova
= iova
};
54 trace_smmu_iotlb_inv_iova(asid
, iova
);
55 g_hash_table_remove(s
->iotlb
, &key
);
58 inline void smmu_iotlb_inv_asid(SMMUState
*s
, uint16_t asid
)
60 trace_smmu_iotlb_inv_asid(asid
);
61 g_hash_table_foreach_remove(s
->iotlb
, smmu_hash_remove_by_asid
, &asid
);
64 /* VMSAv8-64 Translation */
67 * get_pte - Get the content of a page table entry located at
70 static int get_pte(dma_addr_t baseaddr
, uint32_t index
, uint64_t *pte
,
71 SMMUPTWEventInfo
*info
)
74 dma_addr_t addr
= baseaddr
+ index
* sizeof(*pte
);
76 /* TODO: guarantee 64-bit single-copy atomicity */
77 ret
= dma_memory_read(&address_space_memory
, addr
, pte
, sizeof(*pte
));
79 if (ret
!= MEMTX_OK
) {
80 info
->type
= SMMU_PTW_ERR_WALK_EABT
;
84 trace_smmu_get_pte(baseaddr
, index
, addr
, *pte
);
88 /* VMSAv8-64 Translation Table Format Descriptor Decoding */
91 * get_page_pte_address - returns the L3 descriptor output address,
93 * ARM ARM spec: Figure D4-17 VMSAv8-64 level 3 descriptor format
95 static inline hwaddr
get_page_pte_address(uint64_t pte
, int granule_sz
)
97 return PTE_ADDRESS(pte
, granule_sz
);
101 * get_table_pte_address - return table descriptor output address,
102 * ie. address of next level table
103 * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
105 static inline hwaddr
get_table_pte_address(uint64_t pte
, int granule_sz
)
107 return PTE_ADDRESS(pte
, granule_sz
);
111 * get_block_pte_address - return block descriptor output address and block size
112 * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
114 static inline hwaddr
get_block_pte_address(uint64_t pte
, int level
,
115 int granule_sz
, uint64_t *bsz
)
117 int n
= level_shift(level
, granule_sz
);
120 return PTE_ADDRESS(pte
, n
);
123 SMMUTransTableInfo
*select_tt(SMMUTransCfg
*cfg
, dma_addr_t iova
)
125 bool tbi
= extract64(iova
, 55, 1) ? TBI1(cfg
->tbi
) : TBI0(cfg
->tbi
);
126 uint8_t tbi_byte
= tbi
* 8;
128 if (cfg
->tt
[0].tsz
&&
129 !extract64(iova
, 64 - cfg
->tt
[0].tsz
, cfg
->tt
[0].tsz
- tbi_byte
)) {
130 /* there is a ttbr0 region and we are in it (high bits all zero) */
132 } else if (cfg
->tt
[1].tsz
&&
133 !extract64(iova
, 64 - cfg
->tt
[1].tsz
, cfg
->tt
[1].tsz
- tbi_byte
)) {
134 /* there is a ttbr1 region and we are in it (high bits all one) */
136 } else if (!cfg
->tt
[0].tsz
) {
137 /* ttbr0 region is "everything not in the ttbr1 region" */
139 } else if (!cfg
->tt
[1].tsz
) {
140 /* ttbr1 region is "everything not in the ttbr0 region" */
143 /* in the gap between the two regions, this is a Translation fault */
148 * smmu_ptw_64 - VMSAv8-64 Walk of the page tables for a given IOVA
149 * @cfg: translation config
150 * @iova: iova to translate
152 * @tlbe: IOMMUTLBEntry (out)
153 * @info: handle to an error info
155 * Return 0 on success, < 0 on error. In case of error, @info is filled
156 * and tlbe->perm is set to IOMMU_NONE.
157 * Upon success, @tlbe is filled with translated_addr and entry
160 static int smmu_ptw_64(SMMUTransCfg
*cfg
,
161 dma_addr_t iova
, IOMMUAccessFlags perm
,
162 IOMMUTLBEntry
*tlbe
, SMMUPTWEventInfo
*info
)
164 dma_addr_t baseaddr
, indexmask
;
165 int stage
= cfg
->stage
;
166 SMMUTransTableInfo
*tt
= select_tt(cfg
, iova
);
167 uint8_t level
, granule_sz
, inputsize
, stride
;
169 if (!tt
|| tt
->disabled
) {
170 info
->type
= SMMU_PTW_ERR_TRANSLATION
;
174 granule_sz
= tt
->granule_sz
;
175 stride
= granule_sz
- 3;
176 inputsize
= 64 - tt
->tsz
;
177 level
= 4 - (inputsize
- 4) / stride
;
178 indexmask
= (1ULL << (inputsize
- (stride
* (4 - level
)))) - 1;
179 baseaddr
= extract64(tt
->ttb
, 0, 48);
180 baseaddr
&= ~indexmask
;
183 tlbe
->addr_mask
= (1 << granule_sz
) - 1;
186 uint64_t subpage_size
= 1ULL << level_shift(level
, granule_sz
);
187 uint64_t mask
= subpage_size
- 1;
188 uint32_t offset
= iova_level_offset(iova
, inputsize
, level
, granule_sz
);
190 dma_addr_t pte_addr
= baseaddr
+ offset
* sizeof(pte
);
193 if (get_pte(baseaddr
, offset
, &pte
, info
)) {
196 trace_smmu_ptw_level(level
, iova
, subpage_size
,
197 baseaddr
, offset
, pte
);
199 if (is_invalid_pte(pte
) || is_reserved_pte(pte
, level
)) {
200 trace_smmu_ptw_invalid_pte(stage
, level
, baseaddr
,
201 pte_addr
, offset
, pte
);
202 info
->type
= SMMU_PTW_ERR_TRANSLATION
;
206 if (is_page_pte(pte
, level
)) {
207 uint64_t gpa
= get_page_pte_address(pte
, granule_sz
);
210 if (is_permission_fault(ap
, perm
)) {
211 info
->type
= SMMU_PTW_ERR_PERMISSION
;
215 tlbe
->translated_addr
= gpa
+ (iova
& mask
);
216 tlbe
->perm
= PTE_AP_TO_PERM(ap
);
217 trace_smmu_ptw_page_pte(stage
, level
, iova
,
218 baseaddr
, pte_addr
, pte
, gpa
);
221 if (is_block_pte(pte
, level
)) {
223 hwaddr gpa
= get_block_pte_address(pte
, level
, granule_sz
,
227 if (is_permission_fault(ap
, perm
)) {
228 info
->type
= SMMU_PTW_ERR_PERMISSION
;
232 trace_smmu_ptw_block_pte(stage
, level
, baseaddr
,
233 pte_addr
, pte
, iova
, gpa
,
236 tlbe
->translated_addr
= gpa
+ (iova
& mask
);
237 tlbe
->perm
= PTE_AP_TO_PERM(ap
);
242 ap
= PTE_APTABLE(pte
);
244 if (is_permission_fault(ap
, perm
)) {
245 info
->type
= SMMU_PTW_ERR_PERMISSION
;
248 baseaddr
= get_table_pte_address(pte
, granule_sz
);
252 info
->type
= SMMU_PTW_ERR_TRANSLATION
;
255 tlbe
->perm
= IOMMU_NONE
;
260 * smmu_ptw - Walk the page tables for an IOVA, according to @cfg
262 * @cfg: translation configuration
263 * @iova: iova to translate
264 * @perm: tentative access type
265 * @tlbe: returned entry
266 * @info: ptw event handle
268 * return 0 on success
270 inline int smmu_ptw(SMMUTransCfg
*cfg
, dma_addr_t iova
, IOMMUAccessFlags perm
,
271 IOMMUTLBEntry
*tlbe
, SMMUPTWEventInfo
*info
)
275 * This code path is not entered as we check this while decoding
276 * the configuration data in the derived SMMU model.
278 g_assert_not_reached();
281 return smmu_ptw_64(cfg
, iova
, perm
, tlbe
, info
);
285 * The bus number is used for lookup when SID based invalidation occurs.
286 * In that case we lazily populate the SMMUPciBus array from the bus hash
287 * table. At the time the SMMUPciBus is created (smmu_find_add_as), the bus
288 * numbers may not be always initialized yet.
290 SMMUPciBus
*smmu_find_smmu_pcibus(SMMUState
*s
, uint8_t bus_num
)
292 SMMUPciBus
*smmu_pci_bus
= s
->smmu_pcibus_by_bus_num
[bus_num
];
299 g_hash_table_iter_init(&iter
, s
->smmu_pcibus_by_busptr
);
300 while (g_hash_table_iter_next(&iter
, NULL
, (void **)&smmu_pci_bus
)) {
301 if (pci_bus_num(smmu_pci_bus
->bus
) == bus_num
) {
302 s
->smmu_pcibus_by_bus_num
[bus_num
] = smmu_pci_bus
;
310 static AddressSpace
*smmu_find_add_as(PCIBus
*bus
, void *opaque
, int devfn
)
312 SMMUState
*s
= opaque
;
313 SMMUPciBus
*sbus
= g_hash_table_lookup(s
->smmu_pcibus_by_busptr
, bus
);
315 static unsigned int index
;
318 sbus
= g_malloc0(sizeof(SMMUPciBus
) +
319 sizeof(SMMUDevice
*) * SMMU_PCI_DEVFN_MAX
);
321 g_hash_table_insert(s
->smmu_pcibus_by_busptr
, bus
, sbus
);
324 sdev
= sbus
->pbdev
[devfn
];
326 char *name
= g_strdup_printf("%s-%d-%d", s
->mrtypename
, devfn
, index
++);
328 sdev
= sbus
->pbdev
[devfn
] = g_new0(SMMUDevice
, 1);
334 memory_region_init_iommu(&sdev
->iommu
, sizeof(sdev
->iommu
),
336 OBJECT(s
), name
, 1ULL << SMMU_MAX_VA_BITS
);
337 address_space_init(&sdev
->as
,
338 MEMORY_REGION(&sdev
->iommu
), name
);
339 trace_smmu_add_mr(name
);
346 IOMMUMemoryRegion
*smmu_iommu_mr(SMMUState
*s
, uint32_t sid
)
348 uint8_t bus_n
, devfn
;
349 SMMUPciBus
*smmu_bus
;
352 bus_n
= PCI_BUS_NUM(sid
);
353 smmu_bus
= smmu_find_smmu_pcibus(s
, bus_n
);
355 devfn
= SMMU_PCI_DEVFN(sid
);
356 smmu
= smmu_bus
->pbdev
[devfn
];
364 static guint
smmu_iotlb_key_hash(gconstpointer v
)
366 SMMUIOTLBKey
*key
= (SMMUIOTLBKey
*)v
;
370 a
= b
= c
= JHASH_INITVAL
+ sizeof(*key
);
372 b
+= extract64(key
->iova
, 0, 32);
373 c
+= extract64(key
->iova
, 32, 32);
375 __jhash_mix(a
, b
, c
);
376 __jhash_final(a
, b
, c
);
381 static gboolean
smmu_iotlb_key_equal(gconstpointer v1
, gconstpointer v2
)
383 const SMMUIOTLBKey
*k1
= v1
;
384 const SMMUIOTLBKey
*k2
= v2
;
386 return (k1
->asid
== k2
->asid
) && (k1
->iova
== k2
->iova
);
389 /* Unmap the whole notifier's range */
390 static void smmu_unmap_notifier_range(IOMMUNotifier
*n
)
394 entry
.target_as
= &address_space_memory
;
395 entry
.iova
= n
->start
;
396 entry
.perm
= IOMMU_NONE
;
397 entry
.addr_mask
= n
->end
- n
->start
;
399 memory_region_notify_one(n
, &entry
);
402 /* Unmap all notifiers attached to @mr */
403 inline void smmu_inv_notifiers_mr(IOMMUMemoryRegion
*mr
)
407 trace_smmu_inv_notifiers_mr(mr
->parent_obj
.name
);
408 IOMMU_NOTIFIER_FOREACH(n
, mr
) {
409 smmu_unmap_notifier_range(n
);
413 /* Unmap all notifiers of all mr's */
414 void smmu_inv_notifiers_all(SMMUState
*s
)
418 QLIST_FOREACH(sdev
, &s
->devices_with_notifiers
, next
) {
419 smmu_inv_notifiers_mr(&sdev
->iommu
);
423 static void smmu_base_realize(DeviceState
*dev
, Error
**errp
)
425 SMMUState
*s
= ARM_SMMU(dev
);
426 SMMUBaseClass
*sbc
= ARM_SMMU_GET_CLASS(dev
);
427 Error
*local_err
= NULL
;
429 sbc
->parent_realize(dev
, &local_err
);
431 error_propagate(errp
, local_err
);
434 s
->configs
= g_hash_table_new_full(NULL
, NULL
, NULL
, g_free
);
435 s
->iotlb
= g_hash_table_new_full(smmu_iotlb_key_hash
, smmu_iotlb_key_equal
,
437 s
->smmu_pcibus_by_busptr
= g_hash_table_new(NULL
, NULL
);
439 if (s
->primary_bus
) {
440 pci_setup_iommu(s
->primary_bus
, smmu_find_add_as
, s
);
442 error_setg(errp
, "SMMU is not attached to any PCI bus!");
446 static void smmu_base_reset(DeviceState
*dev
)
448 SMMUState
*s
= ARM_SMMU(dev
);
450 g_hash_table_remove_all(s
->configs
);
451 g_hash_table_remove_all(s
->iotlb
);
454 static Property smmu_dev_properties
[] = {
455 DEFINE_PROP_UINT8("bus_num", SMMUState
, bus_num
, 0),
456 DEFINE_PROP_LINK("primary-bus", SMMUState
, primary_bus
, "PCI", PCIBus
*),
457 DEFINE_PROP_END_OF_LIST(),
460 static void smmu_base_class_init(ObjectClass
*klass
, void *data
)
462 DeviceClass
*dc
= DEVICE_CLASS(klass
);
463 SMMUBaseClass
*sbc
= ARM_SMMU_CLASS(klass
);
465 device_class_set_props(dc
, smmu_dev_properties
);
466 device_class_set_parent_realize(dc
, smmu_base_realize
,
467 &sbc
->parent_realize
);
468 dc
->reset
= smmu_base_reset
;
471 static const TypeInfo smmu_base_info
= {
472 .name
= TYPE_ARM_SMMU
,
473 .parent
= TYPE_SYS_BUS_DEVICE
,
474 .instance_size
= sizeof(SMMUState
),
476 .class_size
= sizeof(SMMUBaseClass
),
477 .class_init
= smmu_base_class_init
,
481 static void smmu_base_register_types(void)
483 type_register_static(&smmu_base_info
);
486 type_init(smmu_base_register_types
)