From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (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 E1B2C4534A9 for ; Wed, 2 Sep 2026 10:51:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.70 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788346290; cv=none; b=WwqA/xSjRBcAHrGemrTqWaI26sZ5DcS3LAQ2vHEZ41IjXmePa9oJDR30gtB+KjAVRrKGzkTWUCLWyPRwcWRNSkBqZjqPt8YIv2ti5ZymVIqy/NKZ8QYLYhfaX6RDIpPw0T38QeHaIk8OzdKFJEaC8AY74jhuDQ86ZECy9UT2hM4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788346290; c=relaxed/simple; bh=IR5a7dw8niUl1D2oqM5QJBMPZf1MLc6PkEbzReKAZA0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NfzlD/CbctjTfH9GEpAstJ5BweMtE1nRV0xYM9KrxFASPNmcSwtmabdf2S3k19e8P0nLvp55NKxpbDLvTqySCq5Jky78NkZQmLBmEzSAaKkmzG2Q04SFjBhx3vu/Ves6B7g2vQfEwiGRigzu6Pn7UIxtPGxeY0K4M4MfnCyCd08= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WXuXAD+N; arc=none smtp.client-ip=209.85.128.70 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--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WXuXAD+N" Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4994cf6cdb9so6533855e9.3 for ; Wed, 02 Sep 2026 03:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1788346279; x=1788951079; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=w1wC9u3twhPNjqquLWPyT5Y0R6x/ILvd7DMTdNgZ19s=; b=WXuXAD+NsAQlcuemeby70nHcrjsZoBBMWjHzvJF9i0K/iI3X6b+YvlfPk3Vah9xNlD f1wilQQd0JZ9zCAsQBBKbYBVc05hEDXHEUhQ48ccCmLqaN32LLsFVLx9NAOi7yZcOL35 UancxbOuVa0q4/lFgGIIuyYQ18rgveak81+CQP3SYHKyTp3C7cXjAQHoOydlU70n3i1T pGSXps3GZvdIfe/s4swNy/FnbnylWaVo5QypyZe/SasCdz/mQ9Z1phjNGK0IxoWTmFzI 1LbpKa+OjnFZ2dc/CXDW1wEJs6m9e6RykuOF3/xeZgxpHR3a0Ibo93wd9NNsfhGlyDkT GnLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788346279; x=1788951079; h=content-type: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:content-type; bh=w1wC9u3twhPNjqquLWPyT5Y0R6x/ILvd7DMTdNgZ19s=; b=YOL/7Agd3cpfndJ31owoTA82gFPxgNe7tHHIImpKE0Qal6UlDt+EbhaMYuubUj/BGJ W6RT91wsWAkwOc0W8a2CBzrV+84fVe+HEFZpFGrI8xA9HbXpzpTi6uqmL0CyJPS3rB0W BFlOuzwkveMCuj+hhShnqX8XSaOu0zy1gLpVj2L/tsvS8EHow4mdNkctCpUwspjLujxL ndsu1aY26IbY43IVEsBXqrmQO8rXXvwpHiX9CLALm/iqtEw4/X/rnHpHjhP/yqIaJ5e9 SlzPI6q7AvJmLOzBw1NBJNbcnboCzXBGGvf7/sVNHUplKXi3GsCYdL67EKaZd4N+KzXf 6H5w== X-Forwarded-Encrypted: i=1; AHgh+RoPY2Fa62hBc5tEmR1puAviuMWt0bpLLkuApZ2K2MCnV/Uf+8Ld3vC3M/AwpMUHtxpyScnytyWqU0hZ@vger.kernel.org X-Gm-Message-State: AFuF++nIzINJ8bjrfcq7d37aOQPo71AMEaYc8vFWVtvP/UuobmA2c0zp DcMVRBbrARUMVXHIAq3z5FVFLRT53rL+QUTEawq4IudSAOxLgA61dJM5uvgw5va367pRKJbaAHg Kxl88ux2vxyV1AxoqGEUPNQ== X-Received: from wmqy14.prod.google.com ([2002:a05:600c:364e:b0:49c:ca29:c0ed]) (user=vdonnefort job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5653:b0:49c:eb17:cf3b with SMTP id 5b1f17b1804b1-49ceb17cff4mr15337645e9.8.1788346278647; Wed, 02 Sep 2026 03:51:18 -0700 (PDT) Date: Wed, 2 Sep 2026 11:47:12 +0100 In-Reply-To: <20260902104712.2399797-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260902104712.2399797-1-vdonnefort@google.com> X-Mailer: git-send-email 2.55.0.970.g62bdec98f9-goog Message-ID: <20260902104712.2399797-11-vdonnefort@google.com> Subject: [PATCH v9 10/10] optee: Add support for arm,ffa-lend-pool From: Vincent Donnefort To: catalin.marinas@arm.com, will@kernel.org, rppt@kernel.org, akpm@linux-foundation.org, sudeep.holla@kernel.org, jenswi@kernel.org, robh@kernel.org Cc: mark.rutland@arm.com, sumit.garg@kernel.org, ardb@kernel.org, thierry.reding@kernel.org, david@kernel.org, danielmentz@google.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, op-tee@lists.trustedfirmware.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Vincent Donnefort Content-Type: text/plain; charset="UTF-8" Hook OP-TEE dynamically allocated protected memory pools to the "arm,ffa-lend-pool" driver. While the SMC transport platform device resolves the pool through its DT "memory-region" property, the FF-A transport lacks a device tree node and binds via ffa_lend_pool_attach(). Signed-off-by: Vincent Donnefort diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index 633715b98625..4d6555171918 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -979,6 +979,7 @@ static void optee_ffa_remove(struct ffa_device *ffa_dev) mutex_destroy(&optee->ffa.mutex); rhashtable_free_and_destroy(&optee->ffa.global_ids, rh_free_fn, NULL); + ffa_lend_pool_detach(&optee->teedev->dev); kfree(optee); } @@ -1042,13 +1043,21 @@ static int optee_ffa_protmem_pool_init(struct optee *optee, u32 sec_caps) int rc = 0; if (sec_caps & OPTEE_FFA_SEC_CAP_PROTMEM) { + rc = ffa_lend_pool_attach(&optee->teedev->dev); + if (rc && rc != -ENODEV) + return rc; + pool = optee_protmem_alloc_dyn_pool(optee, id); - if (IS_ERR(pool)) + if (IS_ERR(pool)) { + ffa_lend_pool_detach(&optee->teedev->dev); return PTR_ERR(pool); + } rc = tee_device_register_dma_heap(optee->teedev, id, pool); - if (rc) + if (rc) { pool->ops->destroy_pool(pool); + ffa_lend_pool_detach(&optee->teedev->dev); + } } return rc; diff --git a/drivers/tee/optee/protmem.c b/drivers/tee/optee/protmem.c index be3abf6e8aa6..9b64db9b4e64 100644 --- a/drivers/tee/optee/protmem.c +++ b/drivers/tee/optee/protmem.c @@ -42,14 +42,6 @@ static int init_dyn_protmem(struct optee_protmem_dyn_pool *rp) goto err_null_protmem; } - /* - * TODO unmap the memory range since the physical memory will - * become inaccesible after the lend_protmem() call. - * - * If the platform supports a hypervisor at EL2, it will unmap the - * intermediate physical memory for us and stop cache pre-fetch of - * the memory. - */ rc = rp->optee->ops->lend_protmem(rp->optee, rp->protmem, rp->mem_attrs, rp->mem_attr_count, rp->use_case); diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index b8a2bdac3208..5949aa717cad 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -1528,6 +1529,8 @@ static void optee_smc_remove(struct platform_device *pdev) if (optee->smc.memremaped_shm) memunmap(optee->smc.memremaped_shm); + of_reserved_mem_device_release(&optee->teedev->dev); + kfree(optee); } @@ -1712,16 +1715,22 @@ static int optee_protmem_pool_init(struct optee *optee) if (!protm && !dyn_protm) return 0; + of_reserved_mem_device_init_by_idx(&optee->teedev->dev, + dev_of_node(optee->teedev->dev.parent), 0); if (protm) pool = static_protmem_pool_init(optee); if (dyn_protm && IS_ERR(pool)) pool = optee_protmem_alloc_dyn_pool(optee, heap_id); - if (IS_ERR(pool)) + if (IS_ERR(pool)) { + of_reserved_mem_device_release(&optee->teedev->dev); return PTR_ERR(pool); + } rc = tee_device_register_dma_heap(optee->teedev, heap_id, pool); - if (rc) + if (rc) { pool->ops->destroy_pool(pool); + of_reserved_mem_device_release(&optee->teedev->dev); + } return rc; } @@ -1833,14 +1842,14 @@ static int optee_probe(struct platform_device *pdev) (sec_caps & OPTEE_SMC_SEC_CAP_RPMB_PROBE)) optee->in_kernel_rpmb_routing = true; - teedev = tee_device_alloc(&optee_clnt_desc, NULL, pool, optee); + teedev = tee_device_alloc(&optee_clnt_desc, &pdev->dev, pool, optee); if (IS_ERR(teedev)) { rc = PTR_ERR(teedev); goto err_free_optee; } optee->teedev = teedev; - teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee); + teedev = tee_device_alloc(&optee_supp_desc, &pdev->dev, pool, optee); if (IS_ERR(teedev)) { rc = PTR_ERR(teedev); goto err_unreg_teedev; diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 6742b3579c86..49a9b2993c83 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -3,6 +3,7 @@ * Copyright (c) 2015-2017, 2019-2021 Linaro Limited */ #include +#include #include #include #include @@ -43,6 +44,7 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) dma_mem = container_of(shm, struct tee_shm_dma_mem, shm); p = dma_mem; + ffa_lend_reclaimed(&teedev->dev, shm->paddr, shm->size); dma_free_pages(&teedev->dev, shm->size, dma_mem->page, dma_mem->dma_addr, DMA_BIDIRECTIONAL); #endif @@ -288,6 +290,7 @@ struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx, struct tee_shm_dma_mem *dma_mem; dma_addr_t dma_addr; struct page *page; + int ret; if (!tee_device_get(teedev)) return ERR_PTR(-EINVAL); @@ -297,9 +300,13 @@ struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx, if (!page) goto err_put_teedev; + ret = ffa_prepare_lend(&teedev->dev, page_to_phys(page), page_count * PAGE_SIZE); + if (ret && ret != -ENODEV) + goto err_free_pages; + dma_mem = kzalloc_obj(*dma_mem); if (!dma_mem) - goto err_free_pages; + goto err_map_pages; refcount_set(&dma_mem->shm.refcount, 1); dma_mem->shm.ctx = ctx; @@ -313,6 +320,8 @@ struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx, return &dma_mem->shm; +err_map_pages: + ffa_lend_reclaimed(&teedev->dev, page_to_phys(page), page_count * PAGE_SIZE); err_free_pages: dma_free_pages(&teedev->dev, page_count * PAGE_SIZE, page, dma_addr, DMA_BIDIRECTIONAL); -- 2.55.0.970.g62bdec98f9-goog