Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp442073lqc; Fri, 8 Mar 2024 01:54:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVqJqVOuxiUc/DWFW1GBcsDjLb7rSBmXVqpgXOToYKMUrNf4/7pfe6OHeBvYFRX3Yj+krrrKvYnZML3ri9Ro4HKpTG2x7+ykmqFSy+hXQ== X-Google-Smtp-Source: AGHT+IGvS0XfTFsHAeLi/M2AxZxxXNUJOVCgbSoE36VioV2E2IBaiyBOzAd+vEVrLJ/zIX/OLTPA X-Received: by 2002:a05:6a00:acb:b0:6e6:57fe:5081 with SMTP id c11-20020a056a000acb00b006e657fe5081mr6238671pfl.13.1709891683247; Fri, 08 Mar 2024 01:54:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709891683; cv=pass; d=google.com; s=arc-20160816; b=G9/tpJol0sEEW/db5GwotND0pz7qpmMb7Zzc4ZOLujLENUTFT5BRMi0zxcF3e4BOLO GJZIJmdHkcKRgDFcl2KgZw50iYt5ZC8LWoHJv4v64He26IvA6bK+Y1ucxqG9H9QXh95z p6GEal6kt3x6FtbhygPI0p0S6jGzeRVmGej+9XCbawK68rpDwQ4NosfRzflmAxiOlbhJ VLvz0av7FcD5B1V5996nWyqUNNzwQehjXaUHixYBtelnfMZ3N4QVMLopbF4p2gVqFRVA 9GNdqx2EVy4DU7/fKjjZ9xJ/oNtOZD7Xz0mxQSgbzEg924LVxi3s/0DNsPkVYadxn9pl 4ubg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=VfQM7lXKYKpHQ/1XcBoyNxnnvwniQ822sDP2vHbtLjo=; fh=NbyUzACeGykFDohATR7iP2JKQqi6ry0gd+NCRkYmpt4=; b=TThRtH8oyQuTvpvV1uKxZC1g9MPbNFEwKBPbX9OlOdxGqDkYgguv9xpuR3rPnMaMzE c0mckfehp7exJTHUkgemO3+zF/AuScY0wog9+vGhySavqLPb/vBBbgQzeyi6UL3mJdx2 RRJTFRRHZkHp/9eU5+v9fQ8K6U2ku5P2q00YgSlWqvcAmUF9Pmru/rBa6+9rOYSgHYkC t9/tbkVr9/3Yem1UeFQ4zF1c7hd3r76mrcTp3Ackmv/N4LbTlUKEG/gWTHP5PzCwseLY FMECmPD/rbjyPQvLBctcv5H6WbyhCe/zjP9hi4zjyg2r1TJcI9o6MHC6Iuzwv3ecmgEc Vd4w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rxJpnUEs; arc=pass (i=1 spf=pass spfdomain=flex--howardyen.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-96780-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96780-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r62-20020a632b41000000b005dc365174desi15703447pgr.377.2024.03.08.01.54.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 01:54:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-96780-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rxJpnUEs; arc=pass (i=1 spf=pass spfdomain=flex--howardyen.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-96780-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96780-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E4145284328 for ; Fri, 8 Mar 2024 09:54:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ADCB653E02; Fri, 8 Mar 2024 09:54:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rxJpnUEs" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 DDA79535CA for ; Fri, 8 Mar 2024 09:54:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709891666; cv=none; b=RdNKRyuDVZcQ8386puruA5swET2gfC8h4CQdYks5J/y8VQYbyXd5M5Eee5/46LHqE46P2r8fgAOHlsOdbym9L/ROYj/js7895UXVaUHkNV3O/tdFs93qtFdXu40Asc5caK3NeCn8DX1fV0MYyyDqsEsmkyVlyiAeWmq6FRlX7oE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709891666; c=relaxed/simple; bh=VwxF1Jd7hcZl80hbU87Fdd9TvKw6/1OHQY0K1N2PgAw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=d/aem6uZbHIVdvSOapeG+WrSrLnOfpryP9uLycwdTMr+YqKKF+RpyrubHXiiKt7Kp3tt++wX+QutARjNvybHRJs0TSvDeGFXXjCpeE3gBeSCIyYzitqMvkamV79Wkjfunl75UTQH9MZPmEMgWFzYMjHfUUlEbOtLJjPLGzPWOGU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--howardyen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rxJpnUEs; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--howardyen.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60a08eb0956so2449887b3.3 for ; Fri, 08 Mar 2024 01:54:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709891663; x=1710496463; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VfQM7lXKYKpHQ/1XcBoyNxnnvwniQ822sDP2vHbtLjo=; b=rxJpnUEsqBIXsPKCSkgq3O/S7cTz0tN124gaHmTixYcFb0f0Fjiy9tr2xNVq6al4n9 VX5VGrQETABuDJ9CC8M6L3hacjUVvXO1P0IzohDlzKLiVVxq4NbU5oddZSicMyj0vDT0 9yuYvBSLRN7XixGXHLaKGyjiX1IgVWq7Nq0D79Q4oPE+Rp4Lo8igW9q7sWTVcJ/PFipM V8Sx2GdOAGEHtSZxcBZRXi9+kSz9N/8kVwB9xdaq082Z92cJKeT/45OekxRx2FBHweZh KhKlJTaABQRgow4AvJS/8G1d3jhXjIAEjttnUNSUZqzGr9o2hT6dwNJNftOHLkLgDqFx 7VcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709891663; x=1710496463; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VfQM7lXKYKpHQ/1XcBoyNxnnvwniQ822sDP2vHbtLjo=; b=Ijxx9TR+M2VAOvQFx8ivbmQGmfLx7oANtAi9exvIBJVUhdj01iFwXkqCKJ/IO94Z+Y k7t6Z4pC4/aTBix1yVwDWfrp3t9xx+irmuaGm90lXAB6Xhw1T+jU6iJsUsyu+p54ygGB poB+oXkonWgT15hqhftebOheQMZASKrfCP0T880pyqZRmPFjdiSfUFZ+ORPwhMZ0p8b+ nwZLt9uzRNzWYl3Bn8VpEvMBvhL0f7BCtwmsiNT9WmMywD1olHnzmJnDZV5OFLHOr8Rr DZX8ZU9QuMZduKNKug3NjK31u5vmT4PaNi0+LdHaP9/NcfPAe99XA5YMrOWADiXO0/Yh WJaw== X-Gm-Message-State: AOJu0Yz+WgQLxaULM+PfvO2Uu9Bo2SH+9vTzs7pNW+J0pKqx0oM3Gd+U Th5vmhr/DF66/wWQEEMRFOegiKuMLOU0wVteIFoOr1tnqzQJBxY1lVPsBU2xkKB5q5Q10aLE5LE rfLS6RLePivNVZg== X-Received: from howardyen2.c.googlers.com ([fda3:e722:ac3:cc00:3:22c1:c0a8:16f7]) (user=howardyen job=sendgmr) by 2002:a05:690c:c1c:b0:609:379d:d33d with SMTP id cl28-20020a05690c0c1c00b00609379dd33dmr4737189ywb.6.1709891663336; Fri, 08 Mar 2024 01:54:23 -0800 (PST) Date: Fri, 8 Mar 2024 09:53:19 +0000 In-Reply-To: <20240308095320.1961469-1-howardyen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240308095320.1961469-1-howardyen@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240308095320.1961469-2-howardyen@google.com> Subject: [PATCH v4 1/2] dma-coherent: add support for multi coherent rmems per dev From: Howard Yen To: gregkh@linuxfoundation.org, rafael@kernel.org, mathias.nyman@intel.com, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, andriy.shevchenko@linux.intel.com, petr.tesarik.ext@huawei.com, broonie@kernel.org, james@equiv.tech, james.clark@arm.com, masahiroy@kernel.org Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, iommu@lists.linux.dev, Howard Yen Content-Type: text/plain; charset="UTF-8" Add support for multiple coherent rmems per device. This patch replaces original dma_mem with dma_mems list in device structure to store multiple rmems. These multiple rmems can be assigned to the device one by one by of_reserved_mem_device_init_by_idx() with the memory-region declaration in device tree as below and store the rmem to the dma_mems list. device1@0 { ... memory-region = <&reserved_mem0>, <&reserved_mem1>; ... }; When driver tries to allocate memory from the rmems, looks for the first available rmem and allocates the memory from this rmem. Then if driver removed, of_reserved_mem_device_release() needs to be invoked to release all the rmems assigned to the device. Signed-off-by: Howard Yen --- drivers/base/core.c | 3 ++ include/linux/device.h | 5 +-- kernel/dma/coherent.c | 92 +++++++++++++++++++++++++++--------------- 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 14d46af40f9a..d9af38d7b870 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3108,6 +3108,9 @@ void device_initialize(struct device *dev) defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) dev->dma_coherent = dma_default_coherent; +#endif +#ifdef CONFIG_DMA_DECLARE_COHERENT + INIT_LIST_HEAD(&dev->dma_mems); #endif swiotlb_dev_init(dev); } diff --git a/include/linux/device.h b/include/linux/device.h index 97c4b046c09d..5fa15e5adbdc 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -648,7 +648,7 @@ struct device_physical_location { * @dma_parms: A low level driver may set these to teach IOMMU code about * segment limitations. * @dma_pools: Dma pools (if dma'ble device). - * @dma_mem: Internal for coherent mem override. + * @dma_mems: Internal for coherent mems. * @cma_area: Contiguous memory area for dma allocations * @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use. * @dma_io_tlb_pools: List of transient swiotlb memory pools. @@ -749,8 +749,7 @@ struct device { struct list_head dma_pools; /* dma pools (if dma'ble) */ #ifdef CONFIG_DMA_DECLARE_COHERENT - struct dma_coherent_mem *dma_mem; /* internal for coherent mem - override */ + struct list_head dma_mems; /* Internal for coherent mems */ #endif #ifdef CONFIG_DMA_CMA struct cma *cma_area; /* contiguous memory area for dma diff --git a/kernel/dma/coherent.c b/kernel/dma/coherent.c index ff5683a57f77..f6748a3a5eb1 100644 --- a/kernel/dma/coherent.c +++ b/kernel/dma/coherent.c @@ -11,22 +11,16 @@ #include struct dma_coherent_mem { - void *virt_base; - dma_addr_t device_base; - unsigned long pfn_base; - int size; - unsigned long *bitmap; - spinlock_t spinlock; - bool use_dev_dma_pfn_offset; + struct list_head node; + void *virt_base; + dma_addr_t device_base; + unsigned long pfn_base; + int size; + spinlock_t spinlock; + unsigned long *bitmap; + bool use_dev_dma_pfn_offset; }; -static inline struct dma_coherent_mem *dev_get_coherent_memory(struct device *dev) -{ - if (dev && dev->dma_mem) - return dev->dma_mem; - return NULL; -} - static inline dma_addr_t dma_get_device_base(struct device *dev, struct dma_coherent_mem * mem) { @@ -61,6 +55,7 @@ static struct dma_coherent_mem *dma_init_coherent_memory(phys_addr_t phys_addr, dma_mem->pfn_base = PFN_DOWN(phys_addr); dma_mem->size = pages; dma_mem->use_dev_dma_pfn_offset = use_dma_pfn_offset; + INIT_LIST_HEAD(&dma_mem->node); spin_lock_init(&dma_mem->spinlock); return dma_mem; @@ -90,10 +85,8 @@ static int dma_assign_coherent_memory(struct device *dev, if (!dev) return -ENODEV; - if (dev->dma_mem) - return -EBUSY; + list_add_tail(&mem->node, &dev->dma_mems); - dev->dma_mem = mem; return 0; } @@ -118,23 +111,28 @@ int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, dma_addr_t device_addr, size_t size) { struct dma_coherent_mem *mem; - int ret; + int retval; mem = dma_init_coherent_memory(phys_addr, device_addr, size, false); if (IS_ERR(mem)) return PTR_ERR(mem); - ret = dma_assign_coherent_memory(dev, mem); - if (ret) + retval = dma_assign_coherent_memory(dev, mem); + if (retval) _dma_release_coherent_memory(mem); - return ret; + return retval; } void dma_release_coherent_memory(struct device *dev) { - if (dev) { - _dma_release_coherent_memory(dev->dma_mem); - dev->dma_mem = NULL; + struct dma_coherent_mem *mem_tmp, *q; + + if (!dev) + return; + + list_for_each_entry_safe(mem_tmp, q, &dev->dma_mems, node) { + list_del_init(&mem_tmp->node); + _dma_release_coherent_memory(mem_tmp); } } @@ -187,12 +185,17 @@ static void *__dma_alloc_from_coherent(struct device *dev, int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size, dma_addr_t *dma_handle, void **ret) { - struct dma_coherent_mem *mem = dev_get_coherent_memory(dev); + struct dma_coherent_mem *mem_tmp; - if (!mem) + if (list_empty(&dev->dma_mems)) return 0; - *ret = __dma_alloc_from_coherent(dev, mem, size, dma_handle); + list_for_each_entry(mem_tmp, &dev->dma_mems, node) { + *ret = __dma_alloc_from_coherent(dev, mem_tmp, size, dma_handle); + if (*ret) + break; + } + return 1; } @@ -226,9 +229,16 @@ static int __dma_release_from_coherent(struct dma_coherent_mem *mem, */ int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr) { - struct dma_coherent_mem *mem = dev_get_coherent_memory(dev); + struct dma_coherent_mem *mem_tmp; + int retval = 0; + + list_for_each_entry(mem_tmp, &dev->dma_mems, node) { + retval = __dma_release_from_coherent(mem_tmp, order, vaddr); + if (retval == 1) + break; + } - return __dma_release_from_coherent(mem, order, vaddr); + return retval; } static int __dma_mmap_from_coherent(struct dma_coherent_mem *mem, @@ -271,9 +281,16 @@ static int __dma_mmap_from_coherent(struct dma_coherent_mem *mem, int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma, void *vaddr, size_t size, int *ret) { - struct dma_coherent_mem *mem = dev_get_coherent_memory(dev); + struct dma_coherent_mem *mem_tmp; + int retval = 0; - return __dma_mmap_from_coherent(mem, vma, vaddr, size, ret); + list_for_each_entry(mem_tmp, &dev->dma_mems, node) { + retval = __dma_mmap_from_coherent(mem_tmp, vma, vaddr, size, ret); + if (retval == 1) + break; + } + + return retval; } #ifdef CONFIG_DMA_GLOBAL_POOL @@ -351,8 +368,17 @@ static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) static void rmem_dma_device_release(struct reserved_mem *rmem, struct device *dev) { - if (dev) - dev->dma_mem = NULL; + struct dma_coherent_mem *mem_tmp, *q; + + if (!dev) + return; + + list_for_each_entry_safe(mem_tmp, q, &dev->dma_mems, node) { + if (mem_tmp == rmem->priv) { + list_del_init(&mem_tmp->node); + break; + } + } } static const struct reserved_mem_ops rmem_dma_ops = { -- 2.44.0.278.ge034bb2e1d-goog