From patchwork Thu Feb 22 09:39:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 13566958 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 43665C48BF8 for ; Thu, 22 Feb 2024 09:42:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=wA7aAnFC9GW4ptxsWtsEHzVfE93ap9m0Tpd0RR1dUTU=; b=rMFqM7XwWNDInW oPLUU8wUEqXVdTZ9gWlJuOL9xVsIJlGX3xUuwh8SlAs1O+gjf4BbJ034MpGiD7hmBQIyEsTA5iL0o hAJrKLDj+WNrLMh8QL/x48ksk2GqOiPwl0GFTAZlW3OLvviEH4z552O9DwcVwFRG7cW9L0E7lXjwJ eAnv7litt0tureWAFgCRNOpVQuxGQLtLKZ+Z1w8YG5nA8XzBZHbqZS2M00BXVF/yzk4qBKJ0/hMVO pfuiY3WikJTVRFSH0YX0RqF1Ia52GE26fWk/FPb8dwy4oLw7CFhQr5zmYJeo2Vo5ksRHBRNVfNx6M zvJiFaZALErwxIO6uGcQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rd5az-00000004JFv-0VX9; Thu, 22 Feb 2024 09:42:25 +0000 Received: from mail-oi1-x22b.google.com ([2607:f8b0:4864:20::22b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rd5Zc-00000004I8C-1Qg1 for linux-arm-kernel@lists.infradead.org; Thu, 22 Feb 2024 09:41:03 +0000 Received: by mail-oi1-x22b.google.com with SMTP id 5614622812f47-3c1593b82acso2297744b6e.0 for ; Thu, 22 Feb 2024 01:40:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1708594858; x=1709199658; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JDM6EjBjzZEX6uCYOZJ5gyfkm0OuYc/6ObrJgVAlOzc=; b=nGtmVsWAaFJTEXq++0yrKgWSqDevODRDU8lj5d+G3Gni18Q5i9wOm1wXxWTahCmfCA rJiEllsdBdjYL7mzZM5vvBB0wnprf0HRT+JVB+4ZLuCfV0lZtNqfx9nYdvRiPcukP8vV 4ufZHJ8je4+X6LJ1gD8/hsf6A7f90fTD43TcEDpa9UNVDwfp1FhOiBNXZtsfd+OXJU9o 1q/Ius6hXPDpEStbQaZ3Qsi10MYO8oRCZkX3K8I7rrU6OXAI3g/uK1e3LYdc4+ZYoUMW uLYWS6A9a4H2dkF6fNZAYuU0BtKA7xhrQmNWLiw1k9UYLZXMB8wTzkvrfDtb5bNL4Irg gw7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708594858; x=1709199658; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JDM6EjBjzZEX6uCYOZJ5gyfkm0OuYc/6ObrJgVAlOzc=; b=oPBsXyy+8jLuUoxb9ZpaKIZfv2QJWmDQwlqPcYy6hpcjHBUHh5WlGNFCmyP9yhiV4Q 1eaZj61W/Ob9eAhQmWBzEpMmW4E+h8Mr6+/QlYYAQeYrZ2Gark+bAjOcLPHZxON0tJR8 sHCEtXUWuRLuP4Xp1tuw7fpnkmSqX3Dk2YYKF/7jwiUHLaBPtNhpWtua5pzZgh3wOB2y FLbyn0DL+1C/G7e6kl8Iz+tBkEiyNLQJ8/xa9nXYNDOMgxmtUoC/xZdy4We4k7oyDrOJ muZ6areWibR63eUVfckl0dW1KleWj7Z8o6cYuesx7wSrT5dSE4n3ATPH+s7/SiKNieov vjKA== X-Forwarded-Encrypted: i=1; AJvYcCXtkXeD1aru76T3fBbOyfL4suY6hPCbPzhqP6c9clskL5Bn9DOJClYeUw8Aodb71iwzMHjdtQUfBLktYkUDsmfNx0tnklXZ1NEVBC/hn8fqFliAuOU= X-Gm-Message-State: AOJu0Yw9lCVi7y3l4Gu9xgATM/P5qoRu5PsC0aSwE7HdbpzsAZMn8dQz LdcunCboP+e6ojxJXeHBATj+vLY21/iYrtkdokNbk+yY1hHGlZLIUNK1Eod0sBMWpEUHVCWHGRT XBk8= X-Google-Smtp-Source: AGHT+IGT29HtRoxTfhVvish182aY0aMpJ/2Uit/NlU+Lw0THPvmpBIMgrKBSZWm+oKAJD2qN/cmTKA== X-Received: by 2002:a05:6808:1291:b0:3c1:377a:4641 with SMTP id a17-20020a056808129100b003c1377a4641mr28333878oiw.24.1708594857974; Thu, 22 Feb 2024 01:40:57 -0800 (PST) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id n15-20020a05680803af00b003c17c2b8d09sm130699oie.31.2024.02.22.01.40.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 01:40:57 -0800 (PST) From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Thomas Gleixner , Rob Herring , Krzysztof Kozlowski , Frank Rowand , Conor Dooley Cc: Marc Zyngier , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Atish Patra , Andrew Jones , Sunil V L , Saravana Kannan , Anup Patel , linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Anup Patel Subject: [PATCH v14 05/18] irqchip/sifive-plic: Cleanup PLIC contexts upon irqdomain creation failure Date: Thu, 22 Feb 2024 15:09:53 +0530 Message-Id: <20240222094006.1030709-6-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222094006.1030709-1-apatel@ventanamicro.com> References: <20240222094006.1030709-1-apatel@ventanamicro.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240222_014100_663824_6A8230A7 X-CRM114-Status: GOOD ( 21.79 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The SiFive PLIC contexts should not be left dangling if irqdomain creation fails because plic_starting_cpu() can crash accessing unmapped registers. Signed-off-by: Anup Patel --- drivers/irqchip/irq-sifive-plic.c | 73 ++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/drivers/irqchip/irq-sifive-plic.c b/drivers/irqchip/irq-sifive-plic.c index 615498071b6e..f0df5d0cb76e 100644 --- a/drivers/irqchip/irq-sifive-plic.c +++ b/drivers/irqchip/irq-sifive-plic.c @@ -416,17 +416,45 @@ static const struct of_device_id plic_match[] = { {} }; +static int plic_parse_context_parent(struct platform_device *pdev, u32 context, + u32 *parent_hwirq, int *parent_cpu) +{ + struct device *dev = &pdev->dev; + struct of_phandle_args parent; + unsigned long hartid; + int rc; + + /* + * Currently, only OF fwnode is supported so extend this + * function for ACPI support. + */ + if (!is_of_node(dev->fwnode)) + return -EINVAL; + + rc = of_irq_parse_one(to_of_node(dev->fwnode), context, &parent); + if (rc) + return rc; + + rc = riscv_of_parent_hartid(parent.np, &hartid); + if (rc) + return rc; + + *parent_hwirq = parent.args[0]; + *parent_cpu = riscv_hartid_to_cpuid(hartid); + return 0; +} + static int plic_probe(struct platform_device *pdev) { - int error = 0, nr_contexts, nr_handlers = 0, i; + int error = 0, nr_contexts, nr_handlers = 0, cpu, i; struct device *dev = &pdev->dev; unsigned long plic_quirks = 0; struct plic_handler *handler; + u32 nr_irqs, parent_hwirq; struct irq_domain *domain; struct plic_priv *priv; + irq_hw_number_t hwirq; bool cpuhp_setup; - unsigned int cpu; - u32 nr_irqs; if (is_of_node(dev->fwnode)) { const struct of_device_id *id; @@ -462,13 +490,9 @@ static int plic_probe(struct platform_device *pdev) return -EINVAL; for (i = 0; i < nr_contexts; i++) { - struct of_phandle_args parent; - irq_hw_number_t hwirq; - int cpu; - unsigned long hartid; - - if (of_irq_parse_one(to_of_node(dev->fwnode), i, &parent)) { - dev_err(dev, "failed to parse parent for context %d.\n", i); + error = plic_parse_context_parent(pdev, i, &parent_hwirq, &cpu); + if (error) { + dev_warn(dev, "hwirq for context%d not found\n", i); continue; } @@ -476,7 +500,7 @@ static int plic_probe(struct platform_device *pdev) * Skip contexts other than external interrupts for our * privilege level. */ - if (parent.args[0] != RV_IRQ_EXT) { + if (parent_hwirq != RV_IRQ_EXT) { /* Disable S-mode enable bits if running in M-mode. */ if (IS_ENABLED(CONFIG_RISCV_M_MODE)) { void __iomem *enable_base = priv->regs + @@ -489,13 +513,6 @@ static int plic_probe(struct platform_device *pdev) continue; } - error = riscv_of_parent_hartid(parent.np, &hartid); - if (error < 0) { - dev_warn(dev, "failed to parse hart ID for context %d.\n", i); - continue; - } - - cpu = riscv_hartid_to_cpuid(hartid); if (cpu < 0) { dev_warn(dev, "Invalid cpuid for context %d\n", i); continue; @@ -534,7 +551,7 @@ static int plic_probe(struct platform_device *pdev) handler->enable_save = devm_kcalloc(dev, DIV_ROUND_UP(nr_irqs, 32), sizeof(*handler->enable_save), GFP_KERNEL); if (!handler->enable_save) - return -ENOMEM; + goto fail_cleanup_contexts; done: for (hwirq = 1; hwirq <= nr_irqs; hwirq++) { plic_toggle(handler, hwirq, 0); @@ -547,7 +564,7 @@ static int plic_probe(struct platform_device *pdev) priv->irqdomain = irq_domain_add_linear(to_of_node(dev->fwnode), nr_irqs + 1, &plic_irqdomain_ops, priv); if (WARN_ON(!priv->irqdomain)) - return -ENOMEM; + goto fail_cleanup_contexts; /* * We can have multiple PLIC instances so setup cpuhp state @@ -575,6 +592,22 @@ static int plic_probe(struct platform_device *pdev) dev_info(dev, "mapped %d interrupts with %d handlers for %d contexts.\n", nr_irqs, nr_handlers, nr_contexts); return 0; + +fail_cleanup_contexts: + for (i = 0; i < nr_contexts; i++) { + if (plic_parse_context_parent(pdev, i, &parent_hwirq, &cpu)) + continue; + if (parent_hwirq != RV_IRQ_EXT || cpu < 0) + continue; + + handler = per_cpu_ptr(&plic_handlers, cpu); + handler->present = false; + handler->hart_base = NULL; + handler->enable_base = NULL; + handler->enable_save = NULL; + handler->priv = NULL; + } + return -ENOMEM; } static struct platform_driver plic_driver = {