From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAA9928EA5E for ; Tue, 22 Apr 2025 14:54:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745333679; cv=none; b=BaTf3r8rEPhmR+JljR72Z8808UzxUbhtdot7T3YRQaxovOOOu+50844tyT9lN2FqFrtv9z+khH5hr8yTcyyH/deyQfti60GiynjT6hMFNsn83sSUoW43iHADbPk1vi5ji6/ea5d8N+aT+VSzIBhKtAF028046pBF1y4RmYK0GWk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745333679; c=relaxed/simple; bh=OeAjeCW91zOMarXM0kb3jzoSPAPF26xRkDPsEfYcHsk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=h0xj6nhRFGChJM/0SsG5WuaWPjJaWKB2kOrw2x0DSP4Re7kfZozhLwo9o7CEOFnjIE7yilZ8eHwm4HJoi5hwBkdEvbTpANd1sVGGXunoH5+VwV5lnodvh0YpxnuICU5c6+SQlkZ40+kCCi0681eyHEgNmI2Uqc0Q5zax8cfKbXY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=j6rVgTQF; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="j6rVgTQF" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-3913d129c1aso3869832f8f.0 for ; Tue, 22 Apr 2025 07:54:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1745333675; x=1745938475; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ExdkWFS3PjXe/Dbc/bXhKvoVp3dzgWOeR9qxYAnKhmY=; b=j6rVgTQFEHpRTLzqLaT6J21V8it8xQopPNt2QWegb3CXYAMK5v4PzQiycu47w4QIYN RkFP7j3ZSqDGqXZv2UQtd6iPQz8oOZNnvNh8F7wjvtlv/Mlj0admHMxygQvrfC8mEqla alNo51cDUkrddUcrWPaiUjahrG880BDf/1NuwvS6UONq6NVlzSj5ko3vc7Hz/RBbx2kx CEq8Ur/uttnvs7Mp4OseNGmma5+hpg0WorY8tkj4VdX4E4fuw0pe1ZtSw3F5tblRAYN8 bsK15VcslPM6wxyvJw+YlKst956mAMOs0jQs9/6TvPRgWyxoykRvrpPhWSonQ1NWc7Qr 9Ebw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745333675; x=1745938475; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ExdkWFS3PjXe/Dbc/bXhKvoVp3dzgWOeR9qxYAnKhmY=; b=g158ZzYwZzypDXuWO+I69mPScYz+KH2QS2O1yl4ziM+i50nxErvaQuaNY17yz2i3bb bCDXIQPRhFJ0SVQqaovWFA6oOomO/LahmERRnyUX72zXO65kQKSVJIAWeQR//AwEliy7 /SYQ0VXsSbi9lT7zPfHf/E+9CB1ZJeEKBa5mqeg3iH+iEWp/pJbJyXPHb+yfg/bZJa76 SBwS1XRjeX9Uxh8v9GaaEDDar2IriLSODG010E/Qqs8XB0xPq0BJxfREIt0RTSkGEPhG 9wMlk3WuhZAqzqtVtSAaLnjh4sgjKvxGAUzyjkkYFwT7aRyOHgJnpIK6dBoJpbNyMIR7 z8/Q== X-Forwarded-Encrypted: i=1; AJvYcCXYkibr44eY9rfwoLGeFLmRg8TCiKQfhejBgBbHtYe58EP8FeTmxQ01sgyfHQRtUzfr7DaFYqn+jt54aaQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyzhOZhPQs3BqorIM2fDgS1RbJahFPZO50oyAzTJCQalMmGpFBZ 0CsjzeNBlvaG2nHGYQV2+ZpeLJHiMv/sWMsBGok/D8VmYZkRvUcHNbwUx4CKVa0= X-Gm-Gg: ASbGncsgz61E2vyUKKukgR0b+0tYxdV+K4J9ItTUh5XaZh7lRtLM/z4OxHy6D65rxk/ Ydii/IHYe1a9px+ktX07cTLbOG+Q2EPEF18p59rl8KpUedQ/Cu/YHaI/DVIKSYAdvxP/ume8F5t ntRevAYaOW+ZVMpOjaIRLUy5Nn9g5THSuidWGYIYsjPPDdNicFfwk/fytFflut9+fQghSUjGvQg ilftV4VlDpJUo6MSxklN+ynrFyNrGoWgN14k9dhYgZ3I/4d+EDvZs8aRuPJECCY4T7C5juimrkX uMchA00pn1wCKMOoRKDG1PBdb71ivGIkltV7nTg2r/+L/FTtVun1l3Y= X-Google-Smtp-Source: AGHT+IEETbq5XFAfg5FeeK1CWsmUukRX4l/5LgF2s+uEV4e4L2MV/SS+HmQwhOulznZpXEph1AgW5g== X-Received: by 2002:a05:6000:2510:b0:39c:12ce:1112 with SMTP id ffacd0b85a97d-39ef90916b5mr14296744f8f.21.1745333675135; Tue, 22 Apr 2025 07:54:35 -0700 (PDT) Received: from toaster.baylibre.com ([2a01:e0a:3c5:5fb1:5f77:7d42:6228:3358]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-4406d5a9de9sm177304635e9.6.2025.04.22.07.54.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Apr 2025 07:54:34 -0700 (PDT) From: Jerome Brunet Date: Tue, 22 Apr 2025 16:54:19 +0200 Subject: [PATCH v4 1/2] PCI: endpoint: improve fixed_size bar handling when allocating space Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250422-pci-ep-size-alignment-v4-1-6bd58443fee9@baylibre.com> References: <20250422-pci-ep-size-alignment-v4-0-6bd58443fee9@baylibre.com> In-Reply-To: <20250422-pci-ep-size-alignment-v4-0-6bd58443fee9@baylibre.com> To: Manivannan Sadhasivam , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Kishon Vijay Abraham I , Bjorn Helgaas , Lorenzo Pieralisi , Jon Mason , Dave Jiang , Allen Hubbe , Christoph Hellwig , Sagi Grimberg , Chaitanya Kulkarni Cc: Marek Vasut , Yoshihiro Shimoda , Yuya Hamamachi , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, ntb@lists.linux.dev, linux-nvme@lists.infradead.org, Jerome Brunet X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4249; i=jbrunet@baylibre.com; h=from:subject:message-id; bh=OeAjeCW91zOMarXM0kb3jzoSPAPF26xRkDPsEfYcHsk=; b=owEBbQKS/ZANAwAKAeb8Dxw38tqFAcsmYgBoB62neNdYS0rNPcjwIqteNfi5mysq9qc+xVwMk VScu+42jpqJAjMEAAEKAB0WIQT04VmuGPP1bV8btxvm/A8cN/LahQUCaAetpwAKCRDm/A8cN/La hQMuD/94sBI1ts5f3oxCtZsY9RoXDtXYwn3cTiRTEDYF7fL8Op98EiGWcTYhB4dutn7hTR2d40g cXo+mc7XIljWXLXpETSXbyLwK78PqdA868JyvhPAiQOvjjlncWuU6UAhgNM9eJDjVDdagpC9Dqa 6evRYZS2SBMqfrEXmUDJx4ElUwI8Em2XEAJWCMiSEjzIqqvTtizlKQGRcTGi4AAOdIUcNa9jsr5 GCR7RXVOkTlo5FEUsh1zmKQcAd0RGnOxL5csgDfmCCRixRzuR+jXWYingmMIscd2d83l5Yk95jt WIpZ4Nl6yfQ45BG080rQMK+NwmPedlOByTsUceehldOGPaRn1rYYgZALkrTogPDpZCxzwTa5iLf 0x9aSfAH9Fcr2sH9YtZcUtnGGQ6+dR6Ta6tczKD8z9NdvUBlqfgM/v2XWi+U4ETMw48xWW8Q4P5 2d5l1eDB33gO059069B27rorvqYrc2ch6VA6cwIuY1/uJo2AjqLYRaxU7y5gflP/aOGEWZ4a317 hHX9bRQwouG/qElI3eGPtxQwYeouwFFklJ2n6vdeQmcEvlElMSooKPE3aHO/HKZIvOymUHHw/3J xBxL0tAgXRKzZAB5xC1EtcZe53WHlIPCvK9bvr2Pfvcc0AOGfMFJIkD0A+romszh+CWo2Jk/Jwb CFFGTHItAq5LAJA== X-Developer-Key: i=jbrunet@baylibre.com; a=openpgp; fpr=F29F26CF27BAE1A9719AE6BDC3C92AAF3E60AED9 When trying to allocate space for an endpoint function on a BAR with a fixed size, the size saved in the 'struct pci_epf_bar' should be the fixed size. This is expected by pci_epc_set_bar(). However, if the fixed_size is smaller that the alignment, the size saved in the 'struct pci_epf_bar' matches the alignment and it is a problem for pci_epc_set_bar(). To solve this, continue to allocate space that match the iATU alignment requirement, saving it as .aligned_size, then save the size that matches what is present in the BAR. Fixes: 2a9a801620ef ("PCI: endpoint: Add support to specify alignment for buffers allocated to BARs") Signed-off-by: Jerome Brunet --- drivers/pci/endpoint/pci-epf-core.c | 21 ++++++++++++++------- include/linux/pci-epf.h | 3 +++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c index 394395c7f8decfa2010469655a4bd58a002993fd..982db6c1fbe77653f6a74a31df5c4e997507d2d8 100644 --- a/drivers/pci/endpoint/pci-epf-core.c +++ b/drivers/pci/endpoint/pci-epf-core.c @@ -236,12 +236,13 @@ void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar, } dev = epc->dev.parent; - dma_free_coherent(dev, epf_bar[bar].size, addr, + dma_free_coherent(dev, epf_bar[bar].aligned_size, addr, epf_bar[bar].phys_addr); epf_bar[bar].phys_addr = 0; epf_bar[bar].addr = NULL; epf_bar[bar].size = 0; + epf_bar[bar].aligned_size = 0; epf_bar[bar].barno = 0; epf_bar[bar].flags = 0; } @@ -264,7 +265,7 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar, enum pci_epc_interface_type type) { u64 bar_fixed_size = epc_features->bar[bar].fixed_size; - size_t align = epc_features->align; + size_t aligned_size, align = epc_features->align; struct pci_epf_bar *epf_bar; dma_addr_t phys_addr; struct pci_epc *epc; @@ -285,12 +286,17 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar, return NULL; } size = bar_fixed_size; + } else { + /* BAR size must be power of two */ + size = roundup_pow_of_two(size); } - if (align) - size = ALIGN(size, align); - else - size = roundup_pow_of_two(size); + /* + * Allocate enough memory to accommodate the iATU alignment requirement. + * In most cases, this will be the same as .size but it might be different + * if, for example, the fixed size of a BAR is smaller than align. + */ + aligned_size = align ? ALIGN(size, align) : size; if (type == PRIMARY_INTERFACE) { epc = epf->epc; @@ -301,7 +307,7 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar, } dev = epc->dev.parent; - space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL); + space = dma_alloc_coherent(dev, aligned_size, &phys_addr, GFP_KERNEL); if (!space) { dev_err(dev, "failed to allocate mem space\n"); return NULL; @@ -310,6 +316,7 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar, epf_bar[bar].phys_addr = phys_addr; epf_bar[bar].addr = space; epf_bar[bar].size = size; + epf_bar[bar].aligned_size = aligned_size; epf_bar[bar].barno = bar; if (upper_32_bits(size) || epc_features->bar[bar].only_64bit) epf_bar[bar].flags |= PCI_BASE_ADDRESS_MEM_TYPE_64; diff --git a/include/linux/pci-epf.h b/include/linux/pci-epf.h index 879d19cebd4fc6d8df9d724e3a52fa7fbd61e535..23b0878c2665db1c21e6e83543c33149ab1e0009 100644 --- a/include/linux/pci-epf.h +++ b/include/linux/pci-epf.h @@ -114,6 +114,8 @@ struct pci_epf_driver { * @phys_addr: physical address that should be mapped to the BAR * @addr: virtual address corresponding to the @phys_addr * @size: the size of the address space present in BAR + * @aligned_size: the size actually allocated to accommodate the iATU alignment + * requirement. * @barno: BAR number * @flags: flags that are set for the BAR */ @@ -121,6 +123,7 @@ struct pci_epf_bar { dma_addr_t phys_addr; void *addr; size_t size; + size_t aligned_size; enum pci_barno barno; int flags; }; -- 2.47.2