Arm SMMUv3 uses a SEC_SID (StreamID Security state) to determine the
security state of the programming interface that controls a transaction.
The architecture explicitly states that the association between a device
and its SEC_SID is a system-defined property, not something derived from
the physical address space.

We need a way to represent this system-defined SEC_SID for PCI devices
if we want to implement SMMU's Secure state. So that SMMUv3 can select
the correct register bank and configuration when handling their streams.

This patch adds a new char *sec_sid field to PCIDevice, together with
a "sec-sid" QOM property. The property is intended to carry the
platform-defined SEC_SID for the device; for now only Non-secure and
Secure security states are supported.

Validate the property in the PCI setter, accepting only the
case-sensitive strings "non-secure" and "secure". Reject other values
with an error when the property is set, so invalid values are caught
in the PCI layer before reaching an SMMU.

Signed-off-by: Tao Tang <[email protected]>
Reviewed-by: Pierrick Bouvier <[email protected]>
Reviewed-by: Jim MacArthur <[email protected]>
---
 include/hw/pci/pci_device.h |  3 +++
 hw/pci/pci.c                | 48 +++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/include/hw/pci/pci_device.h b/include/hw/pci/pci_device.h
index 5cac6e16886..6e6c49fdd19 100644
--- a/include/hw/pci/pci_device.h
+++ b/include/hw/pci/pci_device.h
@@ -187,6 +187,9 @@ struct PCIDevice {
     uint32_t max_bounce_buffer_size;
 
     char *sriov_pf;
+
+    /* Arm SMMU SEC_SID */
+    char *sec_sid;
 };
 
 static inline int pci_intx(PCIDevice *pci_dev)
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index d8b117799f3..c0ca836fce9 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -51,6 +51,7 @@
 #include "hw/core/boards.h"
 #include "hw/nvram/fw_cfg.h"
 #include "qapi/error.h"
+#include "qapi/visitor.h"
 #include "qemu/cutils.h"
 #include "pci-internal.h"
 #include "qapi/qapi-builtin-type-infos.h"
@@ -86,6 +87,46 @@ static const PropertyInfo prop_pci_busnr = {
     .get = prop_pci_busnr_get,
 };
 
+static void prop_pci_sec_sid_get(Object *obj, Visitor *v, const char *name,
+                                void *opaque, Error **errp)
+{
+    qdev_prop_string.get(obj, v, name, opaque, errp);
+}
+
+static void prop_pci_sec_sid_set(Object *obj, Visitor *v, const char *name,
+                                void *opaque, Error **errp)
+{
+    PCIDevice *dev = PCI_DEVICE(obj);
+    g_autofree char *value = NULL;
+
+    if (!visit_type_str(v, name, &value, errp)) {
+        return;
+    }
+
+    if (strcmp(value, "non-secure") && strcmp(value, "secure")) {
+        error_setg(errp, "Invalid %s value '%s'; "
+                   "expected 'non-secure' or 'secure'", name, value);
+        return;
+    }
+
+    g_free(dev->sec_sid);
+    dev->sec_sid = g_steal_pointer(&value);
+}
+
+static void prop_pci_sec_sid_release(Object *obj, const char *name,
+                                    void *opaque)
+{
+    qdev_prop_string.release(obj, name, opaque);
+}
+
+static const PropertyInfo prop_pci_sec_sid = {
+    .type = "str",
+    .qapi_type = &str_type_info,
+    .get = prop_pci_sec_sid_get,
+    .set = prop_pci_sec_sid_set,
+    .release = prop_pci_sec_sid_release,
+};
+
 static const Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
     DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
@@ -107,6 +148,13 @@ static const Property pci_props[] = {
     DEFINE_PROP_STRING("sriov-pf", PCIDevice, sriov_pf),
     DEFINE_PROP_BIT("x-pcie-ext-tag", PCIDevice, cap_present,
                     QEMU_PCIE_EXT_TAG_BITNR, true),
+
+    /*
+     * System-defined, statically configured SEC_SID for this PCI device, used
+     * by Arm SMMU. Only support "non-secure" and "secure" security states.
+     */
+    DEFINE_PROP("sec-sid", PCIDevice, sec_sid, prop_pci_sec_sid, char *),
+
     { .name = "busnr", .info = &prop_pci_busnr },
 };
 
-- 
2.34.1


Reply via email to