From patchwork Wed May 4 15:18:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 67159 Delivered-To: patches@linaro.org Received: by 10.140.92.199 with SMTP id b65csp242050qge; Wed, 4 May 2016 08:18:43 -0700 (PDT) X-Received: by 10.28.18.10 with SMTP id 10mr33302528wms.0.1462375119676; Wed, 04 May 2016 08:18:39 -0700 (PDT) Return-Path: Received: from mail-wm0-x233.google.com (mail-wm0-x233.google.com. [2a00:1450:400c:c09::233]) by mx.google.com with ESMTPS id 128si5598772wmt.50.2016.05.04.08.18.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 May 2016 08:18:39 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::233 as permitted sender) client-ip=2a00:1450:400c:c09::233; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::233 as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-wm0-x233.google.com with SMTP id a17so100419101wme.0 for ; Wed, 04 May 2016 08:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=E3N2o151PrMYT1H0n6nwAA9pPdnYfRzif02BHkavJng=; b=kezhl2hDZhh58slotGd3lQTRY3wNdHsc0whDVvZXadeLv/te8sWATYgdhxrWDWN5IG zZ2WlxP+Iycojs1+u7jjBzuOubRpSuZXWFt//6jydBLFbixpqOibLnE3ihVPMwvhanGS QsUxdKztaixVJN32//sAXt7Zeb5+SXQMN9byc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=E3N2o151PrMYT1H0n6nwAA9pPdnYfRzif02BHkavJng=; b=InNqwPDzHtMTvT7NqCYbzalVu95hIYlyt56vRhU2ODlss3zDubSeR/VaqbNy46tTrr MEOcLz1SXVZmZVudrWgM4hmFPCw5dE9rG9mbYN5gjx/slTmQZeIQyf31YFw6b/f/tJMK Ncvp+acQIYb6lgm0PnDv/HuvkKwSJs662UaEG0xNGAExoE4s9D8SD/hKDK9yh28AfD2f XyACXsb0PyE2nVyVc8wMJIF6SP3vJCYpIYaInrOUysAUb905AYzKm7tVrLedcFSgdh3g D7PeyUYwOanYMRMN/rVwJzR0nm5p2zVxyPHEr8xzeow7MhNF+YUOa2Go2NwXu3AOFbFo OZbQ== X-Gm-Message-State: AOPr4FVhLsXf04tUPNAKfzKzeKK2xtJAls5HPmEkkQNmLDpmMXItL3ao46k2JtUM2BGeZu5GjrE= X-Received: by 10.28.126.145 with SMTP id z139mr30281486wmc.81.1462375119396; Wed, 04 May 2016 08:18:39 -0700 (PDT) Return-Path: Received: from new-host-57.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id c4sm4773821wjm.24.2016.05.04.08.18.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 04 May 2016 08:18:38 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, robin.murphy@arm.com, alex.williamson@redhat.com, will.deacon@arm.com, joro@8bytes.org, tglx@linutronix.de, jason@lakedaemon.net, marc.zyngier@arm.com, christoffer.dall@linaro.org, linux-arm-kernel@lists.infradead.org Cc: patches@linaro.org, linux-kernel@vger.kernel.org, Bharat.Bhushan@freescale.com, pranav.sawargaonkar@gmail.com, p.fedin@samsung.com, iommu@lists.linux-foundation.org, Jean-Philippe.Brucker@arm.com, julien.grall@arm.com, yehuday@marvell.com Subject: [PATCH v9 7/8] genirq/msi: map/unmap the MSI doorbells on msi_domain_alloc/free_irqs Date: Wed, 4 May 2016 15:18:13 +0000 Message-Id: <1462375094-2390-8-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1462375094-2390-1-git-send-email-eric.auger@linaro.org> References: <1462375094-2390-1-git-send-email-eric.auger@linaro.org> This patch handles the iommu mapping of MSI doorbells that require to be mapped in an iommu domain. This happens on msi_domain_alloc/free_irqs since this is called in code that can sleep (pci_enable/disable_msi): iommu_map/unmap is not stated as atomic. On msi_domain_(de)activate and msi_domain_set_affinity, which must be atomic, we just lookup for this pre-allocated/mapped IOVA. Signed-off-by: Eric Auger --- v8 -> v9: - decouple irq_data parsing from the actual mapping/unmapping v7 -> v8: - new percpu pointer type - exit from the irq domain hierarchy parsing on first map/unmap success - reset desc->irq to 0 on mapping failure v7: creation --- kernel/irq/msi.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 100 insertions(+), 8 deletions(-) -- 1.9.1 diff --git a/kernel/irq/msi.c b/kernel/irq/msi.c index 72bf4d6..d654524 100644 --- a/kernel/irq/msi.c +++ b/kernel/irq/msi.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include /* Temparory solution for building, will be removed later */ #include @@ -322,6 +324,74 @@ int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev, } /** + * msi_handle_doorbell_mappings: in case the irq data corresponds to an + * MSI that requires iommu mapping, traverse the irq domain hierarchy + * to retrieve the doorbells to handle and iommu_map/unmap them according + * to @map boolean. + * + * @data: irq data handle + * @map: mapping if true, unmapping if false + */ +static int msi_handle_doorbell_mappings(struct irq_data *data, bool map) +{ + const struct irq_chip_msi_doorbell_info *dbinfo; + struct iommu_domain *domain; + struct irq_chip *chip; + struct device *dev; + dma_addr_t iova; + int ret = 0, cpu; + + while (data) { + dev = msi_desc_to_dev(irq_data_get_msi_desc(data)); + domain = iommu_msi_domain(dev); + if (domain) { + chip = irq_data_get_irq_chip(data); + if (chip->msi_doorbell_info) + break; + } + data = data->parent_data; + } + + if (!data) + return 0; + + dbinfo = chip->msi_doorbell_info(data); + if (!dbinfo) + return -EINVAL; + + if (!dbinfo->doorbell_is_percpu) { + if (!map) { + iommu_msi_put_doorbell_iova(domain, + dbinfo->global_doorbell); + return 0; + } + return iommu_msi_get_doorbell_iova(domain, + dbinfo->global_doorbell, + dbinfo->size, dbinfo->prot, + &iova); + } + + /* percpu doorbells */ + for_each_possible_cpu(cpu) { + phys_addr_t __percpu *db_addr = + per_cpu_ptr(dbinfo->percpu_doorbells, cpu); + + if (!map) { + iommu_msi_put_doorbell_iova(domain, *db_addr); + } else { + + ret = iommu_msi_get_doorbell_iova(domain, *db_addr, + dbinfo->size, + dbinfo->prot, &iova); + if (ret) + return ret; + } + } + + return 0; +} + +/** * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain * @domain: The domain to allocate from * @dev: Pointer to device struct of the device for which the interrupts @@ -352,17 +422,29 @@ int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, virq = __irq_domain_alloc_irqs(domain, virq, desc->nvec_used, dev_to_node(dev), &arg, false); - if (virq < 0) { - ret = -ENOSPC; - if (ops->handle_error) - ret = ops->handle_error(domain, desc, ret); - if (ops->msi_finish) - ops->msi_finish(&arg, ret); - return ret; - } + if (virq < 0) + goto error; for (i = 0; i < desc->nvec_used; i++) irq_set_msi_desc_off(virq, i, desc); + + for (i = 0; i < desc->nvec_used; i++) { + struct irq_data *d = irq_get_irq_data(virq + i); + + ret = msi_handle_doorbell_mappings(d, true); + if (ret) + break; + } + if (ret) { + for (; i >= 0; i--) { + struct irq_data *d = irq_get_irq_data(virq + i); + + msi_handle_doorbell_mappings(d, false); + } + irq_domain_free_irqs(virq, desc->nvec_used); + desc->irq = 0; + goto error; + } } if (ops->msi_finish) @@ -377,6 +459,13 @@ int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, } return 0; +error: + ret = -ENOSPC; + if (ops->handle_error) + ret = ops->handle_error(domain, desc, ret); + if (ops->msi_finish) + ops->msi_finish(&arg, ret); + return ret; } /** @@ -396,6 +485,9 @@ void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) * entry. If that's the case, don't do anything. */ if (desc->irq) { + msi_handle_doorbell_mappings( + irq_get_irq_data(desc->irq), + false); irq_domain_free_irqs(desc->irq, desc->nvec_used); desc->irq = 0; }