From patchwork Fri Mar 31 18:05:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 9657095 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9D60A60350 for ; Fri, 31 Mar 2017 18:05:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 906D0286B7 for ; Fri, 31 Mar 2017 18:05:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 84B9F286F2; Fri, 31 Mar 2017 18:05:59 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B4C79286B7 for ; Fri, 31 Mar 2017 18:05:58 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cu0tg-0005M7-4X; Fri, 31 Mar 2017 18:03:40 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cu0te-0005KF-SD for xen-devel@lists.xenproject.org; Fri, 31 Mar 2017 18:03:39 +0000 Received: from [85.158.137.68] by server-5.bemta-3.messagelabs.com id 87/15-19998-AF99ED85; Fri, 31 Mar 2017 18:03:38 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrGLMWRWlGSWpSXmKPExsVysyfVTffnzHs RBpueS1p83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBlNj/YxFyxOqFh28SNTA+Nsjy5GLg4hgU2M Eif2TGWCcJYzSkx4vJWxi5GTg01AV2LHzdfMILaIQKjE0wXfgWwODmaBSonuRfwgYWEBJ4mzH RvZQGwWAVWJzkN3wVp5BawlptzeCxaXEJCTaDh/H2wMp4CNxPxdP1hBbCGgmoZPd5knMHIvYG RYxahRnFpUllqka2Sml1SUmZ5RkpuYmaNraGCsl5taXJyYnpqTmFSsl5yfu4kR6N96BgbGHYw Ne/0OMUpyMCmJ8n4vvhchxJeUn1KZkVicEV9UmpNafIhRhoNDSYL3+gygnGBRanpqRVpmDjDQ YNISHDxKIrxSwGAT4i0uSMwtzkyHSJ1iVJQS5z0G0icAksgozYNrgwX3JUZZKWFeRgYGBiGeg tSi3MwSVPlXjOIcjErCvKog43ky80rgpr8CWswEtNji612QxSWJCCmpBkZGdnevey2Mpr6rl4 lFPZUu2v7VwvRsvK/On+oMS4tTfO9jW6POLpxdLfD4P9vB+VPfdu37e/rXn66tDTuOR7XL5my byfqA7V3Y84PJ928IM5Xs31hi/HBnRUk1y5KTvsFMOTkLbh/1Eaq48k80Mrj6ZBQHy5q32nZx kS/nb3wzV9r8U2T/9hVKLMUZiYZazEXFiQBq7+YBaQIAAA== X-Env-Sender: andre.przywara@arm.com X-Msg-Ref: server-11.tower-31.messagelabs.com!1490983416!62389951!1 X-Originating-IP: [217.140.101.70] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 7928 invoked from network); 31 Mar 2017 18:03:36 -0000 Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by server-11.tower-31.messagelabs.com with SMTP; 31 Mar 2017 18:03:36 -0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5608280D; Fri, 31 Mar 2017 11:03:36 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.207.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7125F3F59A; Fri, 31 Mar 2017 11:03:35 -0700 (PDT) From: Andre Przywara To: Julien Grall , Stefano Stabellini Date: Fri, 31 Mar 2017 19:05:05 +0100 Message-Id: <20170331180525.30038-7-andre.przywara@arm.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20170331180525.30038-1-andre.przywara@arm.com> References: <20170331180525.30038-1-andre.przywara@arm.com> Cc: xen-devel@lists.xenproject.org, Shanker Donthineni , Vijay Kilari Subject: [Xen-devel] [PATCH v3 06/26] ARM: GICv3 ITS: introduce device mapping X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host. For this we have to map each device that Dom0 may request to a host ITS device with the same identifier. Allocate the respective memory and enter each device into an rbtree to later be able to iterate over it or to easily teardown guests. Signed-off-by: Andre Przywara --- xen/arch/arm/gic-v3-its.c | 227 +++++++++++++++++++++++++++++++++++++++ xen/arch/arm/vgic-v3.c | 4 + xen/include/asm-arm/domain.h | 3 + xen/include/asm-arm/gic_v3_its.h | 23 ++++ 4 files changed, 257 insertions(+) diff --git a/xen/arch/arm/gic-v3-its.c b/xen/arch/arm/gic-v3-its.c index 1ac598f..295f7dc 100644 --- a/xen/arch/arm/gic-v3-its.c +++ b/xen/arch/arm/gic-v3-its.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include #include @@ -32,6 +34,18 @@ LIST_HEAD(host_its_list); +struct its_devices { + struct rb_node rbnode; + struct host_its *hw_its; + void *itt_addr; + paddr_t guest_doorbell; /* Identifies the virtual ITS */ + uint32_t host_devid; + uint32_t guest_devid; + uint32_t eventids; /* Number of event IDs (MSIs) */ + uint32_t *host_lpi_blocks; /* Which LPIs are used on the host */ + struct pending_irq *pend_irqs; /* One struct per event */ +}; + bool gicv3_its_host_has_its(void) { return !list_empty(&host_its_list); @@ -151,6 +165,26 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, return its_send_command(its, cmd); } +static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, + uint8_t size_bits, paddr_t itt_addr, bool valid) +{ + uint64_t cmd[4]; + + if ( valid ) + { + ASSERT(size_bits < 32); + ASSERT(!(itt_addr & ~GENMASK_ULL(51, 8))); + } + cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32); + cmd[1] = size_bits; + cmd[2] = itt_addr; + if ( valid ) + cmd[2] |= GITS_VALID_BIT; + cmd[3] = 0x00; + + return its_send_command(its, cmd); +} + /* Set up the (1:1) collection mapping for the given host CPU. */ int gicv3_its_setup_collection(unsigned int cpu) { @@ -376,6 +410,7 @@ static int gicv3_its_init_single_its(struct host_its *hw_its) hw_its->devid_bits = min(hw_its->devid_bits, max_its_device_bits); if ( reg & GITS_TYPER_PTA ) hw_its->flags |= HOST_ITS_USES_PTA; + hw_its->itte_size = GITS_TYPER_ITT_SIZE(reg); for ( i = 0; i < GITS_BASER_NR_REGS; i++ ) { @@ -432,6 +467,197 @@ int gicv3_its_init(void) return 0; } +static int remove_mapped_guest_device(struct its_devices *dev) +{ + int ret; + + if ( dev->hw_its ) + { + /* MAPD also discards all events with this device ID. */ + int ret = its_send_cmd_mapd(dev->hw_its, dev->host_devid, 0, 0, false); + if ( ret ) + return ret; + } + + ret = gicv3_its_wait_commands(dev->hw_its); + if ( ret ) + return ret; + + xfree(dev->itt_addr); + xfree(dev->pend_irqs); + xfree(dev); + + return 0; +} + +static struct host_its *gicv3_its_find_by_doorbell(paddr_t doorbell_address) +{ + struct host_its *hw_its; + + list_for_each_entry(hw_its, &host_its_list, entry) + { + if ( hw_its->addr + ITS_DOORBELL_OFFSET == doorbell_address ) + return hw_its; + } + + return NULL; +} + +static int compare_its_guest_devices(struct its_devices *dev, + paddr_t doorbell, uint32_t devid) +{ + if ( dev->guest_doorbell < doorbell ) + return -1; + + if ( dev->guest_doorbell > doorbell ) + return 1; + + if ( dev->guest_devid < devid ) + return -1; + + if ( dev->guest_devid > devid ) + return 1; + + return 0; +} + +/* + * Map a hardware device, identified by a certain host ITS and its device ID + * to domain d, a guest ITS (identified by its doorbell address) and device ID. + * Also provide the number of events (MSIs) needed for that device. + * This does not check if this particular hardware device is already mapped + * at another domain, it is expected that this would be done by the caller. + */ +int gicv3_its_map_guest_device(struct domain *d, + paddr_t host_doorbell, uint32_t host_devid, + paddr_t guest_doorbell, uint32_t guest_devid, + uint32_t nr_events, bool valid) +{ + void *itt_addr = NULL; + struct host_its *hw_its; + struct its_devices *dev = NULL; + struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL; + int ret = -ENOENT; + + hw_its = gicv3_its_find_by_doorbell(host_doorbell); + if ( !hw_its ) + return ret; + + /* check for already existing mappings */ + spin_lock(&d->arch.vgic.its_devices_lock); + while ( *new ) + { + struct its_devices *temp; + int cmp; + + temp = rb_entry(*new, struct its_devices, rbnode); + + parent = *new; + cmp = compare_its_guest_devices(temp, guest_doorbell, guest_devid); + if ( !cmp ) + { + if ( !valid ) + rb_erase(&temp->rbnode, &d->arch.vgic.its_devices); + + spin_unlock(&d->arch.vgic.its_devices_lock); + + if ( valid ) + return -EBUSY; + + return remove_mapped_guest_device(temp); + } + + if ( cmp > 0 ) + new = &((*new)->rb_left); + else + new = &((*new)->rb_right); + } + + if ( !valid ) + goto out_unlock; + + ret = -ENOMEM; + + /* An Interrupt Translation Table needs to be 256-byte aligned. */ + itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256); + if ( !itt_addr ) + goto out_unlock; + + dev = xzalloc(struct its_devices); + if ( !dev ) + goto out_unlock; + + /* + * Allocate the pending_irqs for each virtual LPI. They will be put + * into the domain's radix tree upon the guest's MAPTI command. + */ + dev->pend_irqs = xzalloc_array(struct pending_irq, nr_events); + if ( !dev->pend_irqs ) + goto out_unlock; + + ret = its_send_cmd_mapd(hw_its, host_devid, + fls(ROUNDUP(nr_events, LPI_BLOCK) - 1) - 1, + virt_to_maddr(itt_addr), true); + if ( ret ) + goto out_unlock; + + dev->itt_addr = itt_addr; + dev->hw_its = hw_its; + dev->guest_doorbell = guest_doorbell; + dev->guest_devid = guest_devid; + dev->host_devid = host_devid; + dev->eventids = nr_events; + + rb_link_node(&dev->rbnode, parent, new); + rb_insert_color(&dev->rbnode, &d->arch.vgic.its_devices); + + spin_unlock(&d->arch.vgic.its_devices_lock); + + return 0; + +out_unlock: + spin_unlock(&d->arch.vgic.its_devices_lock); + if ( dev ) + { + xfree(dev->pend_irqs); + xfree(dev->host_lpi_blocks); + } + xfree(itt_addr); + xfree(dev); + return ret; +} + +/* Removing any connections a domain had to any ITS in the system. */ +void gicv3_its_unmap_all_devices(struct domain *d) +{ + struct rb_node *victim; + struct its_devices *dev; + + /* + * This is an easily readable, but suboptimal implementation. + * It uses the provided iteration wrapper and erases each node, which + * possibly triggers rebalancing. + * This seems overkill since we are going to abolish the whole tree, but + * avoids an open-coded re-implementation of the traversal functions with + * some recursive function calls. + */ +restart: + spin_lock(&d->arch.vgic.its_devices_lock); + if ( (victim = rb_first(&d->arch.vgic.its_devices)) ) + { + dev = rb_entry(victim, struct its_devices, rbnode); + rb_erase(victim, &d->arch.vgic.its_devices); + + spin_unlock(&d->arch.vgic.its_devices_lock); + + remove_mapped_guest_device(dev); + + goto restart; + } + + spin_unlock(&d->arch.vgic.its_devices_lock); +} + /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */ void gicv3_its_dt_init(const struct dt_device_node *node) { @@ -459,6 +685,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node) its_data->addr = addr; its_data->size = size; its_data->dt_node = its; + spin_lock_init(&its_data->cmd_lock); printk("GICv3: Found ITS @0x%lx\n", addr); diff --git a/xen/arch/arm/vgic-v3.c b/xen/arch/arm/vgic-v3.c index d61479d..6242252 100644 --- a/xen/arch/arm/vgic-v3.c +++ b/xen/arch/arm/vgic-v3.c @@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d) d->arch.vgic.nr_regions = rdist_count; d->arch.vgic.rdist_regions = rdist_regions; + spin_lock_init(&d->arch.vgic.its_devices_lock); + d->arch.vgic.its_devices = RB_ROOT; + /* * Domain 0 gets the hardware address. * Guests get the virtual platform layout. @@ -1522,6 +1525,7 @@ static int vgic_v3_domain_init(struct domain *d) static void vgic_v3_domain_free(struct domain *d) { + gicv3_its_unmap_all_devices(d); xfree(d->arch.vgic.rdist_regions); } diff --git a/xen/include/asm-arm/domain.h b/xen/include/asm-arm/domain.h index 2d6fbb1..e559027 100644 --- a/xen/include/asm-arm/domain.h +++ b/xen/include/asm-arm/domain.h @@ -11,6 +11,7 @@ #include #include #include +#include struct hvm_domain { @@ -109,6 +110,8 @@ struct arch_domain } *rdist_regions; int nr_regions; /* Number of rdist regions */ uint32_t rdist_stride; /* Re-Distributor stride */ + struct rb_root its_devices; /* Devices mapped to an ITS */ + spinlock_t its_devices_lock; /* Protects the its_devices tree */ #endif } vgic; diff --git a/xen/include/asm-arm/gic_v3_its.h b/xen/include/asm-arm/gic_v3_its.h index 4c2ae1c..4ade5f6 100644 --- a/xen/include/asm-arm/gic_v3_its.h +++ b/xen/include/asm-arm/gic_v3_its.h @@ -48,6 +48,10 @@ #define GITS_TYPER_DEVICE_ID_BITS(r) (((r & GITS_TYPER_DEVIDS_MASK) >> \ GITS_TYPER_DEVIDS_SHIFT) + 1) #define GITS_TYPER_IDBITS_SHIFT 8 +#define GITS_TYPER_ITT_SIZE_SHIFT 4 +#define GITS_TYPER_ITT_SIZE_MASK (0xfUL << GITS_TYPER_ITT_SIZE_SHIFT) +#define GITS_TYPER_ITT_SIZE(r) ((((r) & GITS_TYPER_ITT_SIZE_MASK) >> \ + GITS_TYPER_ITT_SIZE_SHIFT) + 1) #define GITS_IIDR_VALUE 0x34c @@ -94,7 +98,10 @@ #define GITS_CMD_MOVALL 0x0e #define GITS_CMD_DISCARD 0x0f +#define ITS_DOORBELL_OFFSET 0x10040 + #include +#include #define HOST_ITS_FLUSH_CMD_QUEUE (1U << 0) #define HOST_ITS_USES_PTA (1U << 1) @@ -109,6 +116,7 @@ struct host_its { unsigned int devid_bits; spinlock_t cmd_lock; void *cmd_buf; + unsigned int itte_size; unsigned int flags; }; @@ -135,6 +143,17 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta); /* Map a collection for this host CPU to each host ITS. */ int gicv3_its_setup_collection(unsigned int cpu); +/* + * Map a device on the host by allocating an ITT on the host (ITS). + * "nr_event" specifies how many events (interrupts) this device will need. + * Setting "valid" to false deallocates the device. + */ +int gicv3_its_map_guest_device(struct domain *d, + paddr_t host_doorbell, uint32_t host_devid, + paddr_t guest_doorbell, uint32_t guest_devid, + uint32_t nr_events, bool valid); +void gicv3_its_unmap_all_devices(struct domain *d); + #else static LIST_HEAD(host_its_list); @@ -173,6 +192,10 @@ static inline int gicv3_its_setup_collection(unsigned int cpu) return 0; } +static inline void gicv3_its_unmap_all_devices(struct domain *d) +{ +} + #endif /* CONFIG_HAS_ITS */ #endif