From patchwork Thu Jul 25 13:30:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 11058823 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8AD8A6C5 for ; Thu, 25 Jul 2019 13:34:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7967228821 for ; Thu, 25 Jul 2019 13:34:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6D38A28857; Thu, 25 Jul 2019 13:34:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AF95D28866 for ; Thu, 25 Jul 2019 13:34:22 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hqdrK-0007Y4-Pn; Thu, 25 Jul 2019 13:32:38 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hqdrJ-0007Xt-3U for xen-devel@lists.xenproject.org; Thu, 25 Jul 2019 13:32:37 +0000 X-Inumbo-ID: a799dcc6-aee0-11e9-90aa-639d42ffd07b Received: from m9a0002g.houston.softwaregrp.com (unknown [15.124.64.67]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id a799dcc6-aee0-11e9-90aa-639d42ffd07b; Thu, 25 Jul 2019 13:32:32 +0000 (UTC) Received: FROM m9a0002g.houston.softwaregrp.com (15.121.0.190) BY m9a0002g.houston.softwaregrp.com WITH ESMTP; Thu, 25 Jul 2019 13:32:32 +0000 Received: from M9W0067.microfocus.com (2002:f79:be::f79:be) by M9W0067.microfocus.com (2002:f79:be::f79:be) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10; Thu, 25 Jul 2019 13:30:12 +0000 Received: from NAM05-BY2-obe.outbound.protection.outlook.com (15.124.72.13) by M9W0067.microfocus.com (15.121.0.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10 via Frontend Transport; Thu, 25 Jul 2019 13:30:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MLpZTKgbcqHRT52byFzdUGeVZNnZ1sOB+7GVAfGeRIS3DtHD3C/9E8deHWIzkSNSt2KkGsT1FwXmNvNYXbL1P+EH4UqwvxTdW7G1OyhxxAdJgq4ibA5QtnO7IWesdBCG6Giyj3LefXsSiNgbYeV2vydvRPfax5v9+E/ATBtFGrOhw4Lb/jT3gumif8h/KI8MBiI8syeMGyYw+1wpfzy0Gl1TURa3G69FTkQnDdlCz7OeV4RPBkNK2sjLC9CHrYSdsnDj2UnjnYnpx5p7gwAbLIMPbHJOPYaiurl7sVw0TLhCk9PSFwrVk3X2/SYNE0o0PYA4r/iqE0TYnmJH3LafCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0CADi5a8+LBlOADfhsq4q4HCc7ZVdPFX9aghCBAm2xc=; b=SsqxEWj7qqzYeYAcGt3isG9AGVluwEbsGivdt3+BKEvPVhmbaste0Di1o/2GjJ1zpWrQ26WKRiEJFCriNMQjGgye/S69fowW9LvY1hzBCrJY/458v0L5/YkTDKkAizIhsJqVgiGoEdamydc3mbFmtXKYy8efqLldxhw2hLIBZH16buZ0+JmWVLDwDYy+OQFwTYd+Y9m/qs0w9pP8qghktRDPo3rY7ATun/gM6eGAGKFW5kgSsgbG9nEsJKwqZwY0hVS2rLHW9hW+No/b5jZhJfjzkUNpaXM5mlYU67vn08ytmio9xstcSLcjszG9XOHfra9Mh64MtIzxHvha+6l2BQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1;spf=pass smtp.mailfrom=suse.com;dmarc=pass action=none header.from=suse.com;dkim=pass header.d=suse.com;arc=none Received: from BY5PR18MB3394.namprd18.prod.outlook.com (10.255.139.95) by BY5PR18MB3297.namprd18.prod.outlook.com (10.255.163.221) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2115.10; Thu, 25 Jul 2019 13:30:10 +0000 Received: from BY5PR18MB3394.namprd18.prod.outlook.com ([fe80::a4f0:4f46:c076:f74b]) by BY5PR18MB3394.namprd18.prod.outlook.com ([fe80::a4f0:4f46:c076:f74b%7]) with mapi id 15.20.2115.005; Thu, 25 Jul 2019 13:30:10 +0000 From: Jan Beulich To: "xen-devel@lists.xenproject.org" Thread-Topic: [PATCH v4 03/12] AMD/IOMMU: use bit field for IRTE Thread-Index: AQHVQu0VoTRlTMFGGECqlXoZBbx1cQ== Date: Thu, 25 Jul 2019 13:30:10 +0000 Message-ID: <12881c67-924c-6c38-9966-5de534a60001@suse.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: LO2P265CA0454.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:e::34) To BY5PR18MB3394.namprd18.prod.outlook.com (2603:10b6:a03:194::31) authentication-results: spf=none (sender IP is ) smtp.mailfrom=JBeulich@suse.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [87.234.252.170] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 77664d6e-342a-451b-a065-08d711043769 x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328)(7193020); SRVR:BY5PR18MB3297; x-ms-traffictypediagnostic: BY5PR18MB3297: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:5797; x-forefront-prvs: 0109D382B0 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(4636009)(39860400002)(366004)(136003)(396003)(376002)(346002)(189003)(199004)(6436002)(53936002)(2906002)(386003)(6506007)(2501003)(86362001)(478600001)(5660300002)(64756008)(4326008)(66556008)(305945005)(6486002)(31696002)(486006)(25786009)(66066001)(6512007)(102836004)(80792005)(66946007)(5640700003)(14454004)(26005)(66446008)(7736002)(14444005)(446003)(52116002)(99286004)(76176011)(54906003)(6116002)(3846002)(31686004)(316002)(81166006)(81156014)(6916009)(8676002)(8936002)(68736007)(2616005)(66476007)(11346002)(256004)(71190400001)(71200400001)(36756003)(476003)(2351001)(186003); DIR:OUT; SFP:1102; SCL:1; SRVR:BY5PR18MB3297; H:BY5PR18MB3394.namprd18.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: suse.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: xLUtyFAvV7e59L8/M3ck0y2XTexpFwp/ebUyH5ltgv0f2qUWRz0nIc7Ld2S4s0AHl9V9MBzpAWErOf42YUiBv22sJHnS7Q7JMqultG3U1hefWxe7WnOrvdblK08/1pDjamjWVNbMiBXdFsP59GnZUpXEiCvrcZVbpdzmjYldOPYPK5ftk4+gQEAyoe1UbSvN16hbv0E4J9iP7vbekTYLnlmLDlaMgY0K7NSGqAD/wbuq4Hi/UBYoG/0OHNrt6D3JgiWsP4JPxieAKQnAxEBrDog4O67JYkGVGoWutCxf5+vGiSB4/7vo3aRYrQKuoplywP9rBsRIHdR4ul8NjZP7Npi7rWCp/aL9gwXI7OXLyM/ojjLsDBpAmI083MwHdbOyZWhsnFBFPb21UYG+u1lhI9dlSwxr7oWm/JqDPFyLC8c= Content-ID: <6554CDC30DC6C644BC915A709FFDD372@namprd18.prod.outlook.com> MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 77664d6e-342a-451b-a065-08d711043769 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Jul 2019 13:30:10.5174 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 856b813c-16e5-49a5-85ec-6f081e13b527 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: JBeulich@suse.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR18MB3297 X-OriginatorOrg: suse.com Subject: [Xen-devel] [PATCH v4 03/12] AMD/IOMMU: use bit field for IRTE X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Brian Woods , Suravee Suthikulpanit Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP At the same time restrict its scope to just the single source file actually using it, and abstract accesses by introducing a union of pointers. (A union of the actual table entries is not used to make it impossible to [wrongly, once the 128-bit form gets added] perform pointer arithmetic / array accesses on derived types.) Also move away from updating the entries piecemeal: Construct a full new entry, and write it out. Signed-off-by: Jan Beulich Acked-by: Andrew Cooper Acked-by: Brian Woods --- v4: Re-base. Do away with standalone struct irte_basic. v3: Switch boolean bitfields to bool. v2: name {get,free}_intremap_entry()'s last parameter "index" instead of "offset". Introduce union irte32. --- a/xen/drivers/passthrough/amd/iommu_intr.c +++ b/xen/drivers/passthrough/amd/iommu_intr.c @@ -24,6 +24,26 @@ #include #include +union irte32 { + uint32_t raw; + struct { + bool remap_en:1; + bool sup_io_pf:1; + unsigned int int_type:3; + bool rq_eoi:1; + bool dm:1; + bool guest_mode:1; /* MBZ */ + unsigned int dest:8; + unsigned int vector:8; + unsigned int :8; + } flds; +}; + +union irte_ptr { + void *ptr; + union irte32 *ptr32; +}; + #define INTREMAP_TABLE_ORDER 1 #define INTREMAP_LENGTH 0xB #define INTREMAP_ENTRIES (1 << INTREMAP_LENGTH) @@ -102,47 +122,45 @@ static unsigned int alloc_intremap_entry return slot; } -static u32 *get_intremap_entry(int seg, int bdf, int offset) +static union irte_ptr get_intremap_entry(unsigned int seg, unsigned int bdf, + unsigned int index) { - u32 *table = get_ivrs_mappings(seg)[bdf].intremap_table; + union irte_ptr table = { + .ptr = get_ivrs_mappings(seg)[bdf].intremap_table + }; + + ASSERT(table.ptr && (index < INTREMAP_ENTRIES)); - ASSERT( (table != NULL) && (offset < INTREMAP_ENTRIES) ); + table.ptr32 += index; - return table + offset; + return table; } -static void free_intremap_entry(int seg, int bdf, int offset) -{ - u32 *entry = get_intremap_entry(seg, bdf, offset); - - memset(entry, 0, sizeof(u32)); - __clear_bit(offset, get_ivrs_mappings(seg)[bdf].intremap_inuse); -} - -static void update_intremap_entry(u32* entry, u8 vector, u8 int_type, - u8 dest_mode, u8 dest) -{ - set_field_in_reg_u32(IOMMU_CONTROL_ENABLED, 0, - INT_REMAP_ENTRY_REMAPEN_MASK, - INT_REMAP_ENTRY_REMAPEN_SHIFT, entry); - set_field_in_reg_u32(IOMMU_CONTROL_DISABLED, *entry, - INT_REMAP_ENTRY_SUPIOPF_MASK, - INT_REMAP_ENTRY_SUPIOPF_SHIFT, entry); - set_field_in_reg_u32(int_type, *entry, - INT_REMAP_ENTRY_INTTYPE_MASK, - INT_REMAP_ENTRY_INTTYPE_SHIFT, entry); - set_field_in_reg_u32(IOMMU_CONTROL_DISABLED, *entry, - INT_REMAP_ENTRY_REQEOI_MASK, - INT_REMAP_ENTRY_REQEOI_SHIFT, entry); - set_field_in_reg_u32((u32)dest_mode, *entry, - INT_REMAP_ENTRY_DM_MASK, - INT_REMAP_ENTRY_DM_SHIFT, entry); - set_field_in_reg_u32((u32)dest, *entry, - INT_REMAP_ENTRY_DEST_MAST, - INT_REMAP_ENTRY_DEST_SHIFT, entry); - set_field_in_reg_u32((u32)vector, *entry, - INT_REMAP_ENTRY_VECTOR_MASK, - INT_REMAP_ENTRY_VECTOR_SHIFT, entry); +static void free_intremap_entry(unsigned int seg, unsigned int bdf, + unsigned int index) +{ + union irte_ptr entry = get_intremap_entry(seg, bdf, index); + + ACCESS_ONCE(entry.ptr32->raw) = 0; + + __clear_bit(index, get_ivrs_mappings(seg)[bdf].intremap_inuse); +} + +static void update_intremap_entry(union irte_ptr entry, unsigned int vector, + unsigned int int_type, + unsigned int dest_mode, unsigned int dest) +{ + union irte32 irte = { + .flds = { + .remap_en = true, + .int_type = int_type, + .dm = dest_mode, + .dest = dest, + .vector = vector, + }, + }; + + ACCESS_ONCE(entry.ptr32->raw) = irte.raw; } static inline int get_rte_index(const struct IO_APIC_route_entry *rte) @@ -164,7 +182,7 @@ static int update_intremap_entry_from_io u16 *index) { unsigned long flags; - u32* entry; + union irte_ptr entry; u8 delivery_mode, dest, vector, dest_mode; int req_id; spinlock_t *lock; @@ -202,12 +220,8 @@ static int update_intremap_entry_from_io * so need to recover vector and delivery mode from IRTE. */ ASSERT(get_rte_index(rte) == offset); - vector = get_field_from_reg_u32(*entry, - INT_REMAP_ENTRY_VECTOR_MASK, - INT_REMAP_ENTRY_VECTOR_SHIFT); - delivery_mode = get_field_from_reg_u32(*entry, - INT_REMAP_ENTRY_INTTYPE_MASK, - INT_REMAP_ENTRY_INTTYPE_SHIFT); + vector = entry.ptr32->flds.vector; + delivery_mode = entry.ptr32->flds.int_type; } update_intremap_entry(entry, vector, delivery_mode, dest_mode, dest); @@ -229,7 +243,7 @@ int __init amd_iommu_setup_ioapic_remapp { struct IO_APIC_route_entry rte; unsigned long flags; - u32* entry; + union irte_ptr entry; int apic, pin; u8 delivery_mode, dest, vector, dest_mode; u16 seg, bdf, req_id; @@ -408,16 +422,14 @@ unsigned int amd_iommu_read_ioapic_from_ u16 bdf = ioapic_sbdf[idx].bdf; u16 seg = ioapic_sbdf[idx].seg; u16 req_id = get_intremap_requestor_id(seg, bdf); - const u32 *entry = get_intremap_entry(seg, req_id, offset); + union irte_ptr entry = get_intremap_entry(seg, req_id, offset); ASSERT(offset == (val & (INTREMAP_ENTRIES - 1))); val &= ~(INTREMAP_ENTRIES - 1); - val |= get_field_from_reg_u32(*entry, - INT_REMAP_ENTRY_INTTYPE_MASK, - INT_REMAP_ENTRY_INTTYPE_SHIFT) << 8; - val |= get_field_from_reg_u32(*entry, - INT_REMAP_ENTRY_VECTOR_MASK, - INT_REMAP_ENTRY_VECTOR_SHIFT); + val |= MASK_INSR(entry.ptr32->flds.int_type, + IO_APIC_REDIR_DELIV_MODE_MASK); + val |= MASK_INSR(entry.ptr32->flds.vector, + IO_APIC_REDIR_VECTOR_MASK); } return val; @@ -428,7 +440,7 @@ static int update_intremap_entry_from_ms int *remap_index, const struct msi_msg *msg, u32 *data) { unsigned long flags; - u32* entry; + union irte_ptr entry; u16 req_id, alias_id; u8 delivery_mode, dest, vector, dest_mode; spinlock_t *lock; @@ -582,7 +594,7 @@ void amd_iommu_read_msi_from_ire( const struct pci_dev *pdev = msi_desc->dev; u16 bdf = pdev ? PCI_BDF2(pdev->bus, pdev->devfn) : hpet_sbdf.bdf; u16 seg = pdev ? pdev->seg : hpet_sbdf.seg; - const u32 *entry; + union irte_ptr entry; if ( IS_ERR_OR_NULL(_find_iommu_for_device(seg, bdf)) ) return; @@ -598,12 +610,10 @@ void amd_iommu_read_msi_from_ire( } msg->data &= ~(INTREMAP_ENTRIES - 1); - msg->data |= get_field_from_reg_u32(*entry, - INT_REMAP_ENTRY_INTTYPE_MASK, - INT_REMAP_ENTRY_INTTYPE_SHIFT) << 8; - msg->data |= get_field_from_reg_u32(*entry, - INT_REMAP_ENTRY_VECTOR_MASK, - INT_REMAP_ENTRY_VECTOR_SHIFT); + msg->data |= MASK_INSR(entry.ptr32->flds.int_type, + MSI_DATA_DELIVERY_MODE_MASK); + msg->data |= MASK_INSR(entry.ptr32->flds.vector, + MSI_DATA_VECTOR_MASK); } int __init amd_iommu_free_intremap_table( --- a/xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +++ b/xen/include/asm-x86/hvm/svm/amd-iommu-defs.h @@ -469,22 +469,6 @@ struct amd_iommu_pte { #define IOMMU_CONTROL_DISABLED 0 #define IOMMU_CONTROL_ENABLED 1 -/* interrupt remapping table */ -#define INT_REMAP_ENTRY_REMAPEN_MASK 0x00000001 -#define INT_REMAP_ENTRY_REMAPEN_SHIFT 0 -#define INT_REMAP_ENTRY_SUPIOPF_MASK 0x00000002 -#define INT_REMAP_ENTRY_SUPIOPF_SHIFT 1 -#define INT_REMAP_ENTRY_INTTYPE_MASK 0x0000001C -#define INT_REMAP_ENTRY_INTTYPE_SHIFT 2 -#define INT_REMAP_ENTRY_REQEOI_MASK 0x00000020 -#define INT_REMAP_ENTRY_REQEOI_SHIFT 5 -#define INT_REMAP_ENTRY_DM_MASK 0x00000040 -#define INT_REMAP_ENTRY_DM_SHIFT 6 -#define INT_REMAP_ENTRY_DEST_MAST 0x0000FF00 -#define INT_REMAP_ENTRY_DEST_SHIFT 8 -#define INT_REMAP_ENTRY_VECTOR_MASK 0x00FF0000 -#define INT_REMAP_ENTRY_VECTOR_SHIFT 16 - #define INV_IOMMU_ALL_PAGES_ADDRESS ((1ULL << 63) - 1) #define IOMMU_RING_BUFFER_PTR_MASK 0x0007FFF0