From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 703DD2741C8 for ; Tue, 29 Apr 2025 06:06:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906820; cv=none; b=tXR9zhwZuYRZ2wHMIH8uN2pUWmbM6W4HxsArIAVbXdg5wdtOx7WmDwFzu7A+JM8JmYDcQwNnhhE0ryK5Z90XsBx9C6nhV/HhtRSbzaxA1bVl5KY/H35Sy6FUUe5+vkRjGc3j/EsVH4SaCKwctQjQrAIwPD1I7Sz6rD9pN3TEg8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906820; c=relaxed/simple; bh=jFBkkTZSAMc8YsW5eR17yaZHeNm/0DGHN2MFjL1ufWc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=otJ6JeE7pewTRWzbrUZrLVBliCjwVo9nsnu3plzN9B8d/GkjTYvbOQ1AIxiVWVW3fYMBEFfTuwVNL5IVrxwNVEU5PvpDDcure0Pd9xR3NEjNJ5sTeRq1ScoQePcosPS2BsI0eGF3c4eQY8h9sBA5B/VXal3yalk/1E8vWDu7BM4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=CVy0cjHP; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="CVy0cjHP" Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNqAxq011881 for ; Tue, 29 Apr 2025 06:06:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= tluKXCMaC5kMOu8ljoIfLsf72OuKjTNKaAhw+onm69U=; b=CVy0cjHPv3Zr2qaf Y+ZYOFhfBxNo77Y4SZCjSzR70EWrwd5eMxTNdDvBq72VomkAOI9/s8nTVCqYl+vr GKO7tHkNDe08BEqDIFHqg+Jk2INqyc5v1jkuO6ozFw4NrdCC7hv0uuKFINMD6Sk+ BV9//jH7cenb49NC7bQ21xqtanpg3zLjq705iiHC9GKOMA4XehXsy0pUW9cLreEu LNBiJe9dy0iS0gttCSqRcONQ363t6ZrHJYYB8vpo1XTbX/u/VJg+A/KSttpARnnX HiNEe8LAWySAkg+ygDmBRA3uldMTSyvvlP17vJ9RbmV3pwc4Rlf/gbB1lbS1EpaW J5RZCA== Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468pg9bf7k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:06:58 +0000 (GMT) Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-736bf7eb149so3778436b3a.0 for ; Mon, 28 Apr 2025 23:06:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906817; x=1746511617; 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=tluKXCMaC5kMOu8ljoIfLsf72OuKjTNKaAhw+onm69U=; b=Ydrr0zE2eRlAbvEWMGuBNVEObIC91z57OkJKmfW4fuxhlDMUjcJ/OQ9CloAG0/USam A97ogK/Ewbi6AD0EL5VTtrh+Nbhd2WYMazCh03TQrktchEzmFmO4TLlqm3Ncep3bACS1 yCnrvHR7m98zhu5G2Uxx8/IE+nU6ZgugsOtx7IM1032jQ4/+rQY9cYHaY+2UmpjsTwo9 O1TxniPTWkhmdKEhdJQ1d8ULvUeeYktPQdm0/yYYxo2djIQjQgRo7e6WZ2D1WW6oDrlH mZ6h9lSA2tTFjCCcPvdwbovjmVs+ioHZRaptFXxoP5dBtDZtDB8Cb/TKJ2DUXttl9cGc Uirw== X-Forwarded-Encrypted: i=1; AJvYcCWZtkUnVxFkBRsMTakRC0g1BcYdi+fSIzbxWGz3AQAXsa0hDiaD9oQMdfd/dhjYwmgiapYzK4SkJeRhX+U=@vger.kernel.org X-Gm-Message-State: AOJu0YyOwwUPaDMP2xgXebujazGikQWM6ycLkO06rHG86ilFgESKCDo8 XtU2XmuZMdjpTOPdQXmlLLzQUJRU2uaUQKjL8ADXArZqiWXZ6DSFvghZ3IYgD78mlU9U1jKkupc Xy6Z75ZcB+dxYIBsNJvhga9TeTshEzVHoibytM5NbTOYAhBXbhStkRF91PeCMKQ== X-Gm-Gg: ASbGncvimqM670jzmp1Yx+00fXvHGTv7xjSKoAjYl1/1Gi6TdChE9r5ZUulnjemf+ET wwDND5ccAG5GmtZ9rduNicnU7p3hpTFQ+Pm+8X+fFHpQQbIC7IW0AvpfepSzrvUONi+rMEHTdLD NUgiuYJ/mk8iI47BFyUHoNwxvD5wGOjtUH3FZHSpiCvM8eqC1PWQ+Y2t23w6pO6tU9RQ8tFh6yf kRiItFzmqEGyYv3M1wPf91o2b8YGS1S84JklJeqS7VEBp0EN9hve8tCVtOyAkCh4JrZ5o3jdT12 HOOHTS0k362SL35gm9qK8UjluO493YE97djS0nKZ5a6ZMnABE02P9jPtu03vqgVvOuVgwgo= X-Received: by 2002:a05:6a00:2883:b0:736:8c0f:774f with SMTP id d2e1a72fcca58-74029565221mr2187775b3a.22.1745906816672; Mon, 28 Apr 2025 23:06:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEg07otcOVfNPJ8y+U5whY82XEhwy5M5k9eRX8C8wg/VMa0dKjj+wgL5TymAJ0tRCcizjOWNQ== X-Received: by 2002:a05:6a00:2883:b0:736:8c0f:774f with SMTP id d2e1a72fcca58-74029565221mr2187751b3a.22.1745906816303; Mon, 28 Apr 2025 23:06:56 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:06:55 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:22 -0700 Subject: [PATCH v4 01/11] tee: allow a driver to allocate a tee_device without a pool 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-1-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi , Sumit Garg X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=ZpvtK87G c=1 sm=1 tr=0 ts=68106c82 cx=c_pps a=rEQLjTOiSrHUhVqRoksmgQ==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=1atmPuGhRQHGwWkYLZIA:9 a=QEXdDO2ut3YA:10 a=2VI0MkxyNR6bbpdq8BZq:22 X-Proofpoint-ORIG-GUID: rha1X7SDV2SbHNOBmfMm7q6BOIKtyWI- X-Proofpoint-GUID: rha1X7SDV2SbHNOBmfMm7q6BOIKtyWI- X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX2m4eYha2zUl9 IMbbo53tnoCLwMK6C0fsYtBmfcAhTxZM15rGYftUtvVLEUN6ukbgsnkrwuWpVanYFFR8a6EqimQ XzXhJnV5sjzgbtQ+6WMCQHDNgoUdipgTYf417quPkn+UVYiIdirLpvGPd94dBO/sS9Bh/jnOtqb ufdcxGqxVv6KDnVOSfsUpmAmf3IQcueTOckl4ZGfTSlUenF8qsK/Rtn/cumURImZnkDtsczTOm4 fpmBlADlWoopeRuIDBrnF9okMJSdi7aEzuYdvhEOMAy8WK2aC1ix6Jg+HTyGwnCluC4XnweZw6N r31Z8xDYcIPUMLgMqwNUcWv88+RW1Fz7040KMZjNg340vvIzEuS0IEqrpHebvSgm70J/pujwXLX 04XK685HxF7T/hvgKqMKvqHa/6cVQMkdCQds2mmFc3Rd3reIFCUzjDLdHsBQbpXzc9+W9gwD X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 impostorscore=0 mlxscore=0 bulkscore=0 mlxlogscore=999 malwarescore=0 adultscore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 suspectscore=0 phishscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 A TEE driver doesn't always need to provide a pool if it doesn't support memory sharing ioctls and can allocate memory for TEE messages in another way. Although this is mentioned in the documentation for tee_device_alloc(), it is not handled correctly. Reviewed-by: Sumit Garg Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/tee/tee_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index d113679b1e2d..24edce4cdbaa 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -888,7 +888,7 @@ struct tee_device *tee_device_alloc(const struct tee_de= sc *teedesc, =20 if (!teedesc || !teedesc->name || !teedesc->ops || !teedesc->ops->get_version || !teedesc->ops->open || - !teedesc->ops->release || !pool) + !teedesc->ops->release) return ERR_PTR(-EINVAL); =20 teedev =3D kzalloc(sizeof(*teedev), GFP_KERNEL); --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24B03274662 for ; Tue, 29 Apr 2025 06:07:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906822; cv=none; b=tHTQYpt+RtuEq3cEQ+bxLY5l1qTccaatCXas3uJ07zS+dZ11ixMSN+7krnr+RuhkjC5EcrF/gc677PcWItuX6eWp2lZQkndNQbjiyjUPmnZh0gCeNCRS8JtJirsUPxMsY8K+Cb5ePAF8ppwwuGKpWrwK36+xMkjGnwXp9aKkmDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906822; c=relaxed/simple; bh=L1aYNBugHemRKih8UZAQrKIGP4q+CkmcvzSHZrTkdJw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nxoogVMTbsANNemj2ZGPiLGNbr7aQ7ZepD1dQFyppiVQ1AFo5UUvaJy5rrwvIu00BFtSr4TCJRIi2pG5QQIl+rIQOEq8SPHoiIOZUceF6NNg7Y6l3cLx3pNuKAudr7ilmNsy7/U63332enX+MxyiT6+MZqBg7Tb30/fdt54kXok= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=dQAwOahF; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="dQAwOahF" Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNq1jA023841 for ; Tue, 29 Apr 2025 06:07:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= dnQ9ZvYmV3x3P6q8kxc8hFtmLDbG/Ebqq3P0pryF11M=; b=dQAwOahFBkP6mJIx cPHWb7aG6pA+zzilINQNCxYb7bSH46qPC+02Fj7txiHB2cdi+6aEdEY0rA9Qc91L ROtrefsQ70nm/R8tijnTYBSq5hPSr1Qn3SO62dOpGQAQMELcEeUApSj/hiibh1Xw xJ7ehz7puGgpostzR6SbSsUIgEFfmccBJamwVlphv4x2qdV0SxGqLAm5AswR6TPz ljKWO0kMXXeRId717WS4wbiCKYiDW/G6qK5yb5s6lrPA7IWofDZUkmCc0NwgAXd5 ohTEpV0mqdVzML7Rw57/7Antq/qUh3rqLhqQR+6Z5TOYaMnsCwYVzbfO5thqGh/l n1b7UA== Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468muqkm26-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:00 +0000 (GMT) Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-736bf7eb149so3778454b3a.0 for ; Mon, 28 Apr 2025 23:06:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906819; x=1746511619; 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=dnQ9ZvYmV3x3P6q8kxc8hFtmLDbG/Ebqq3P0pryF11M=; b=TJ2ZlAR4Lv84K+4IK/NdfzrMDz+nXHYAcQdJ3bAu4TpdbSQao6Wm+woDDQRA1G35Oe IfYj4P0XhOiZi4NYsFLjXASVykloB+WZIT/gY/2gLKi+MJO53kxww4J81EVVz7VnbsAU Id9f0Cr9Ki4zqIxw5oMGQOntLDHIQhwedJPpLu/jg3vdJeqA3ib8aGdqlNPvydv9pYO/ 1HflE6ePND32G2put7CGwRMCG9Ywy6HTaqHXm/NOiqwsCMZY8YqzUYN6vg/Y/FQCJ00N 4qc4fUFQ3AvAkEufojPFMqVJka1JwrU3i/MxFgx4gOPTGvVA8jXUB+vi6A189IkkazyI CS1A== X-Forwarded-Encrypted: i=1; AJvYcCX2IA/f/zjmL7gnM0a6rYQ8lcuyoZSXrvFQlWktgtJXMavkvB7WNT0ZXeXFIXd5tqAEsiuFOmMCrp0iRWI=@vger.kernel.org X-Gm-Message-State: AOJu0YwU5ytNqZIjHBKqSQnrbKqChsL1tqYVqn4xpsN1CGHH4+9eGYD1 JbTOkCXaP0NB9pRemC2+gHOx01ivMAsB2HuH28uWvHvf/S5QZ1rb+dXKaNEdT7nATm0Z68zssTD ItKCrp6HYdqHMG3knKBzGkbjTT0ulVjSwNOw8tb9qVvFxRcM5aBMe1zkEbDT5Gw== X-Gm-Gg: ASbGncs6bTJlB54XBxwvg3jrNSrUpa/ym7iGErGMaNTPyvw6A0dSkOEBmQ1DIxL3Vgx QrVEIi6PwCq833cJ9ptEMH8l16YZhuQPKt0qDKmNUJ1u6i8UKalQ75vAfaYp0xF5Y8P2YAwpdj8 Arbu/z+RuVp6KH8l2gkQ9cWV9p2qtExAcfEN6lI179sRjpKUcbFSDHb/ksxSDGVdJSWQMnN6DZ3 GJfYVqRubbb0X9ls7WS/RAwX/cvnFv6fu6Z2QHGdARHqOKPxwLixawJZXW5Wees0IEdz6iggVqs ce6Pqmvfe5Spsq8Zf8bLnJeaMRnBR66f+Of3cwkx0GU2i47QVnuF/Oy0pn9AlFynxkbuJJs= X-Received: by 2002:a05:6a21:3e05:b0:1ee:e96a:d9ed with SMTP id adf61e73a8af0-2095b1f5f89mr2175857637.7.1745906818203; Mon, 28 Apr 2025 23:06:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEHulkgSXpMufhY1kolwNeYf5vPD6BLjF1B7cv5zN7UHcdV02UCghJAy548hFwhdTnMqNVbqg== X-Received: by 2002:a05:6a21:3e05:b0:1ee:e96a:d9ed with SMTP id adf61e73a8af0-2095b1f5f89mr2175831637.7.1745906817824; Mon, 28 Apr 2025 23:06:57 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.06.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:06:57 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:23 -0700 Subject: [PATCH v4 02/11] tee: add close_context to TEE driver operation 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-2-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX3TdwsekqMnn3 vJLBWlQhR5vhi9Z7Udd6YRKIhP7bbIL3OOiyD8Zq6TcU8yUZT0f8jOikFZv7TSUWxJWNUrJR4OG fK3tZp//8NbWMqo/zpWdnQGlTItmWSeclr3lJF5g7wjMChwH5iUT26hzSnH4E4kYBoBsGLZoUd0 7LV8yTBxA/6t4tfk36+kzU/Rg9SxtVUHFEmAiTGzst+MupKs3CZaHhQANJTmmHnNKA4OikRMv6B zcP8ksnkeE+x32sKVamdsK1uw9YySXshDedPS/sUU7j2i9fZwPP2x3eNP9hc7YFZ6orhVUEa870 breooPe+e6rqKgBbxzlAF71qKa3PaG4gMSQPfPw1+jnQqb9pHwTx1jcRoYVEcW0awKw0humdjMt 0cUQlXJQLqMLL24mzvIoIjcfrbrmAp9H61BU4ktwiyd/SuEaJCixAVgVSv5l0Btq7QrePXZt X-Proofpoint-GUID: 8Lr33URvr5Z0uwzknhe3vF8CM7rvGBVA X-Proofpoint-ORIG-GUID: 8Lr33URvr5Z0uwzknhe3vF8CM7rvGBVA X-Authority-Analysis: v=2.4 cv=M/5NKzws c=1 sm=1 tr=0 ts=68106c84 cx=c_pps a=m5Vt/hrsBiPMCU0y4gIsQw==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=0l_YNDiMcOfTHwIteTYA:9 a=QEXdDO2ut3YA:10 a=IoOABgeZipijB_acs4fv:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 suspectscore=0 mlxscore=0 spamscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 bulkscore=0 phishscore=0 clxscore=1015 lowpriorityscore=0 priorityscore=1501 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 The tee_context can be used to manage TEE user resources, including those allocated by the driver for the TEE on behalf of the user. The release() callback is invoked only when all resources, such as tee_shm, are released and there are no references to the tee_context. When a user closes the device file, the driver should notify the TEE to release any resources it may hold and drop the context references. To achieve this, a close_context() callback is introduced to initiate resource release in the TEE driver when the device file is closed. Relocate teedev_ctx_get, teedev_ctx_put, tee_device_get, and tee_device_get functions to tee_core.h to make them accessible outside the TEE subsystem. Signed-off-by: Amirreza Zarrabi Reviewed-by: Sumit Garg Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/tee/tee_core.c | 7 +++++++ drivers/tee/tee_private.h | 6 ------ include/linux/tee_core.h | 50 +++++++++++++++++++++++++++++++++++++++++++= ++-- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 24edce4cdbaa..721522fe5c63 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -79,6 +79,7 @@ void teedev_ctx_get(struct tee_context *ctx) =20 kref_get(&ctx->refcount); } +EXPORT_SYMBOL_GPL(teedev_ctx_get); =20 static void teedev_ctx_release(struct kref *ref) { @@ -96,11 +97,15 @@ void teedev_ctx_put(struct tee_context *ctx) =20 kref_put(&ctx->refcount, teedev_ctx_release); } +EXPORT_SYMBOL_GPL(teedev_ctx_put); =20 void teedev_close_context(struct tee_context *ctx) { struct tee_device *teedev =3D ctx->teedev; =20 + if (teedev->desc->ops->close_context) + teedev->desc->ops->close_context(ctx); + teedev_ctx_put(ctx); tee_device_put(teedev); } @@ -1037,6 +1042,7 @@ void tee_device_put(struct tee_device *teedev) } mutex_unlock(&teedev->mutex); } +EXPORT_SYMBOL_GPL(tee_device_put); =20 bool tee_device_get(struct tee_device *teedev) { @@ -1049,6 +1055,7 @@ bool tee_device_get(struct tee_device *teedev) mutex_unlock(&teedev->mutex); return true; } +EXPORT_SYMBOL_GPL(tee_device_get); =20 /** * tee_device_unregister() - Removes a TEE device diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 9bc50605227c..d3f40a03de36 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -14,12 +14,6 @@ =20 int tee_shm_get_fd(struct tee_shm *shm); =20 -bool tee_device_get(struct tee_device *teedev); -void tee_device_put(struct tee_device *teedev); - -void teedev_ctx_get(struct tee_context *ctx); -void teedev_ctx_put(struct tee_context *ctx); - struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t siz= e); struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, unsigned long addr, size_t length); diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index a38494d6b5f4..284ca6b3e03a 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -65,8 +65,9 @@ struct tee_device { /** * struct tee_driver_ops - driver operations vtable * @get_version: returns version of driver - * @open: called when the device file is opened - * @release: release this open file + * @open: called for a context when the device file is opened + * @close_context: called when the device file is closed + * @release: called to release the context * @open_session: open a new session * @close_session: close a session * @system_session: declare session as a system session @@ -76,11 +77,17 @@ struct tee_device { * @supp_send: called for supplicant to send a response * @shm_register: register shared memory buffer in TEE * @shm_unregister: unregister shared memory buffer in TEE + * + * The context given to @open might last longer than the device file if it= is + * tied to other resources in the TEE driver. @close_context is called whe= n the + * client closes the device file, even if there are existing references to= the + * context. The TEE driver can use @close_context to start cleaning up. */ struct tee_driver_ops { void (*get_version)(struct tee_device *teedev, struct tee_ioctl_version_data *vers); int (*open)(struct tee_context *ctx); + void (*close_context)(struct tee_context *ctx); void (*release)(struct tee_context *ctx); int (*open_session)(struct tee_context *ctx, struct tee_ioctl_open_session_arg *arg, @@ -154,6 +161,24 @@ int tee_device_register(struct tee_device *teedev); */ void tee_device_unregister(struct tee_device *teedev); =20 +/** + * tee_device_get() - Increment the user count for a tee_device + * @teedev: Pointer to the tee_device + * + * If tee_device_unregister() has been called and the final user of @teedev + * has already released the device, this function will fail to prevent new= users + * from accessing the device during the unregistration process. + * + * Returns: true if @teedev remains valid, otherwise false + */ +bool tee_device_get(struct tee_device *teedev); + +/** + * tee_device_put() - Decrease the user count for a tee_device + * @teedev: pointer to the tee_device + */ +void tee_device_put(struct tee_device *teedev); + /** * tee_device_set_dev_groups() - Set device attribute groups * @teedev: Device to register @@ -315,4 +340,25 @@ struct tee_context *teedev_open(struct tee_device *tee= dev); */ void teedev_close_context(struct tee_context *ctx); =20 +/** + * teedev_ctx_get() - Increment the reference count of a context + * @ctx: Pointer to the context + * + * This function increases the refcount of the context, which is tied to + * resources shared by the same tee_device. During the unregistration proc= ess, + * the context may remain valid even after tee_device_unregister() has ret= urned. + * + * Users should ensure that the context's refcount is properly decreased b= efore + * calling tee_device_put(), typically within the context's release() func= tion. + * Alternatively, users can call tee_device_get() and teedev_ctx_get() tog= ether + * and release them simultaneously (see shm_alloc_helper()). + */ +void teedev_ctx_get(struct tee_context *ctx); + +/** + * teedev_ctx_put() - Decrease reference count on a context + * @ctx: pointer to the context + */ +void teedev_ctx_put(struct tee_context *ctx); + #endif /*__TEE_CORE_H*/ --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FFFE274671 for ; Tue, 29 Apr 2025 06:07:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906823; cv=none; b=BlpjoiTSzZcMPuNP6QbNMsDnTGkLSuG2JyfcMfOKX5hp33WNHJuQSShpWF5ow7pBNxKdmf0uV5XLndktttmCt28lgFQPA2Qv+N71eCQ5l3m9cmIZaTamz8gN7tbOf5xxov4tAZi9wm3kWX7uzcuqZps7wr5/FxTBKECszhATFZ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906823; c=relaxed/simple; bh=lm0UWskH7d1rtZJ/T+nzuR9G55DCPo2TN73ybskHlbQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Sh3gpyUs7M7D3a4C8XbCw3XtW+NOAksIq3Q3VmIGpTVgargsM7FuWffDF2iKUw9dazIZeL0jITTslq2Uvh58vUECYUOBREsVFe/iheEYQbocanQUlDnfwo3ToeVAXSParfFdoPv7VppPrWadjUr11UqjtEcPmL0rDYgDgWQYLWs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=jJJolMfa; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="jJJolMfa" Received: from pps.filterd (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNqPEl020497 for ; Tue, 29 Apr 2025 06:07:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= sxprD47J1ns4+472DQayzeIlGvHftXTVadqiV/NL7Ig=; b=jJJolMfaE1pMIhbU peLzegYcWzF6XcdzV73llv3xnqmOWgsgWKvCQC8HsXPd+/adfvmW6/mAXoWrJCEX zTYgvXCihU3pEEZ3B9ZAXNlH3ZghHpbGJzbxJUJ4h2Pg6no7oHgqny/YPScxxakZ KRktGzZcsfLzgsEkafpC2n8x+uJCMreOwLowD8XDYA3wdBIGQv8VwTUr6JGIBwE4 /G0eK3NwkII+O9uAU6/E+Ii55d1c9P8Ilhcgq3SzzrzxP0duGvmEaZCTcYFYFtAO /FhJ66gjHfRf3+AKLuj4RLAeRRybtg2jS3v0Z1y8DNDE3ra2BKBu6KEs8Lim+O++ s41UgQ== Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468n6jjqe9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:00 +0000 (GMT) Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-7377139d8b1so3861042b3a.0 for ; Mon, 28 Apr 2025 23:07:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906820; x=1746511620; 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=sxprD47J1ns4+472DQayzeIlGvHftXTVadqiV/NL7Ig=; b=KRh3Bha41mCzmMz5qY/5r9LW2tjWdiWYONlgYIks9UZv41uRGvW4LU0yrz/qFxrOKo bw3koPm6K5XVqfWb3PwrN19sa18UvKAylNaPpwlp3B3WZSsgceNbrNnQei3eD4C5M1qk qA+YoFYAVJQ56sUmq0A0kVmqOcV4cVJ59+TrHdbEiedxIr3MgJtmayJCl4NywAw3nnIw FOpTlKnIdFusNxQ1z8QHylEcgoNAkI9dKlqf4Vzj+8oH8yxtoNuBCFjsMwcn2/RHUea+ AB/ggkePUcbB+WKS6Su9qZ2EN0oAwmbOCaB6B46/t6W/iJ4Jdbi0G418yG3OLOT63CLl 5o5Q== X-Forwarded-Encrypted: i=1; AJvYcCUjtYURNTWYICImHEHloUiNWjiBvuXL6ldJSRzP2lGSSxB0OmPOjHxZlvLXLQA/hI0zXmEFytSxNwJZuck=@vger.kernel.org X-Gm-Message-State: AOJu0YzC2QUG57etR67dr3b8PnGezt0yBp5kOLFJggdwehYhbzqQ8ppo 4YeUUCZkI9CZYovb8bIzXMuhfqFRRr0QwQpZhtS/KZYqNfsB/UXqNTshPPxw9rIpZLN54CL+Mgm VWUwL28pGWjcsWr4yUa4tkclIYjS0PXcSmWE25VXD7+/aW6xvHQGaokfVYXdsXw== X-Gm-Gg: ASbGncuzHbQQl0PJyAUFTEig65QY/iSz0dBYQ1axCkuFj6v9qCt5E2d6MdkcSp8U5Rf H7+oMLjSnIv/tdPApro1f0cQ1UYNwKORrZs7nalYnGasPa2KEXM/XvINJT2lwUheB7+pk96uvjS 1NqAAoKRuVOQwxxMWdT/QzbCUwrHhbnBcnCvAInBUtYat3il5kylLemSoAG5E6E1CFXrg9c5eWv fdL4FJfqjN+/BXuiq1SfoNszpoECs2uER/pS4UPdPeI05YtNI+BGAuCbNclOZ2rGSHiZOkPlrZi QWbwZuFUybhfQ1rjUhsmSVPDC0YAftxwZqmuzUZDjQDtqFtGlXNIFCyDESgTg6tKh+gqT7g= X-Received: by 2002:a05:6a00:21c6:b0:736:6151:c6ca with SMTP id d2e1a72fcca58-7402710658amr3514729b3a.4.1745906819991; Mon, 28 Apr 2025 23:06:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEBuFacde4OVGB/zgIWhIZTviSUq1luP80t2jNeLYY1HUnZqwFAuegWqGa+YDw1+m78dpky5g== X-Received: by 2002:a05:6a00:21c6:b0:736:6151:c6ca with SMTP id d2e1a72fcca58-7402710658amr3514682b3a.4.1745906819523; Mon, 28 Apr 2025 23:06:59 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.06.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:06:58 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:24 -0700 Subject: [PATCH v4 03/11] tee: add TEE_IOCTL_PARAM_ATTR_TYPE_UBUF 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-3-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NSBTYWx0ZWRfX01WUZKIqL5Cv +77ybksz8HTa0myY5hQoJ0W6S+pYagZfoyhNING/dqZ5fYfGL9Ch56h0TzgioZ4tCUh05Voamyv PpMU3nRb3wrLyuy2Zvk7rdjZcPZvYkaVMoG+8NoLY3IIs9KoB+bBRdCebss1u5n7n0xds1rC8bW l1ogn6J2VtOH7SFTrtS6JK1upFLuYYbPo1prf5ADkP6/PPHIReedH/AOHDeRFoFskfZ4t/4zXCI /VIUDMjVqI+TBY+B/u46uQRN0k2RIMOAl86HIbb7zMPfgmi97GZEQ2qyp5UNU8XQzSrgyn7rLro q6JkCWdDwRYsHpB9/LMWPGJCOgl4A/RcQFmP92iJkOsOuYGWgIMErWKd4LtXnYNnFL/xFmUove9 kdHfwUafa7ClVjUxgvCzX/YFz64HBf2IweSb5Uchx9F83NNnVNSsWSYtMjDvi+uRMN9Gfpek X-Proofpoint-GUID: zAbQrSMrTTOxoGSOJ3qM34S8J_NeGch7 X-Proofpoint-ORIG-GUID: zAbQrSMrTTOxoGSOJ3qM34S8J_NeGch7 X-Authority-Analysis: v=2.4 cv=C8fpyRP+ c=1 sm=1 tr=0 ts=68106c84 cx=c_pps a=m5Vt/hrsBiPMCU0y4gIsQw==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=nExbU_x0mLWtth5-Sj8A:9 a=QEXdDO2ut3YA:10 a=IoOABgeZipijB_acs4fv:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_02,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 phishscore=0 mlxlogscore=999 priorityscore=1501 clxscore=1015 spamscore=0 adultscore=0 malwarescore=0 lowpriorityscore=0 suspectscore=0 bulkscore=0 mlxscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290045 For drivers that can transfer data to the TEE without using shared memory from client, it is necessary to receive the user address directly, bypassing any processing by the TEE subsystem. Introduce TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT/OUTPUT/INOUT to represent userspace buffers. Signed-off-by: Amirreza Zarrabi Reviewed-by: Sumit Garg Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/tee/tee_core.c | 33 +++++++++++++++++++++++++++++++++ include/linux/tee_drv.h | 6 ++++++ include/uapi/linux/tee.h | 22 ++++++++++++++++------ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 721522fe5c63..7bf265c4ac65 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -386,6 +386,17 @@ static int params_from_user(struct tee_context *ctx, s= truct tee_param *params, params[n].u.value.b =3D ip.b; params[n].u.value.c =3D ip.c; break; + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INOUT: + params[n].u.ubuf.uaddr =3D u64_to_user_ptr(ip.a); + params[n].u.ubuf.size =3D ip.b; + + if (!access_ok(params[n].u.ubuf.uaddr, + params[n].u.ubuf.size)) + return -EFAULT; + + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: @@ -454,6 +465,11 @@ static int params_to_user(struct tee_ioctl_param __use= r *uparams, put_user(p->u.value.c, &up->c)) return -EFAULT; break; + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INOUT: + if (put_user((u64)p->u.ubuf.size, &up->b)) + return -EFAULT; + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: if (put_user((u64)p->u.memref.size, &up->b)) @@ -654,6 +670,13 @@ static int params_to_supp(struct tee_context *ctx, ip.b =3D p->u.value.b; ip.c =3D p->u.value.c; break; + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INOUT: + ip.a =3D (u64)p->u.ubuf.uaddr; + ip.b =3D p->u.ubuf.size; + ip.c =3D 0; + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: @@ -756,6 +779,16 @@ static int params_from_supp(struct tee_param *params, = size_t num_params, p->u.value.b =3D ip.b; p->u.value.c =3D ip.c; break; + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INOUT: + p->u.ubuf.uaddr =3D u64_to_user_ptr(ip.a); + p->u.ubuf.size =3D ip.b; + + if (!access_ok(params[n].u.ubuf.uaddr, + params[n].u.ubuf.size)) + return -EFAULT; + + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: /* diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index a54c203000ed..78bbf12f02f0 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -82,6 +82,11 @@ struct tee_param_memref { struct tee_shm *shm; }; =20 +struct tee_param_ubuf { + void * __user uaddr; + size_t size; +}; + struct tee_param_value { u64 a; u64 b; @@ -92,6 +97,7 @@ struct tee_param { u64 attr; union { struct tee_param_memref memref; + struct tee_param_ubuf ubuf; struct tee_param_value value; } u; }; diff --git a/include/uapi/linux/tee.h b/include/uapi/linux/tee.h index d0430bee8292..3e9b1ec5dfde 100644 --- a/include/uapi/linux/tee.h +++ b/include/uapi/linux/tee.h @@ -151,6 +151,13 @@ struct tee_ioctl_buf_data { #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT 6 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT 7 /* input and output */ =20 +/* + * These defines userspace buffer parameters. + */ +#define TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT 8 +#define TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT 9 +#define TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INOUT 10 /* input and output */ + /* * Mask for the type part of the attribute, leaves room for more types */ @@ -186,14 +193,17 @@ struct tee_ioctl_buf_data { /** * struct tee_ioctl_param - parameter * @attr: attributes - * @a: if a memref, offset into the shared memory object, else a value par= ameter - * @b: if a memref, size of the buffer, else a value parameter + * @a: if a memref, offset into the shared memory object, + * else if a ubuf, address of the user buffer, + * else a value parameter + * @b: if a memref or ubuf, size of the buffer, else a value parameter * @c: if a memref, shared memory identifier, else a value parameter * - * @attr & TEE_PARAM_ATTR_TYPE_MASK indicates if memref or value is used in - * the union. TEE_PARAM_ATTR_TYPE_VALUE_* indicates value and - * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref. TEE_PARAM_ATTR_TYPE_NONE - * indicates that none of the members are used. + * @attr & TEE_PARAM_ATTR_TYPE_MASK indicates if memref, ubuf, or value is + * used in the union. TEE_PARAM_ATTR_TYPE_VALUE_* indicates value, + * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref, and TEE_PARAM_ATTR_TYPE_= UBUF_* + * indicates ubuf. TEE_PARAM_ATTR_TYPE_NONE indicates that none of the mem= bers + * are used. * * Shared memory is allocated with TEE_IOC_SHM_ALLOC which returns an * identifier representing the shared memory object. A memref can reference --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 065502741C8 for ; Tue, 29 Apr 2025 06:07:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906826; cv=none; b=YXxYavuXoloabrSnw5LHpCXBV8pF8W28CLJhbimcy5TqjsknCAv2d6ot3q0kj3G0kQT4+fpWM7y+XGJjUVVIsGzRaSEO5rerwtQAYRK6BNJ7O18PvawQ8eoOJmHxaUo4eSLq2gNjqwCvX10beN3R8qDYHFZBBD4E7DvPnpBo410= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906826; c=relaxed/simple; bh=hec8UTOVXl69wb9lp8BTknO+2nJjRvi97d7lNb2Uw90=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aCuwdJnXZP2ZvpnbMj8fjpJBFWCOBxXUNR4u0prGO19iwdcwTCNzep5/GfGbBa1AN9C9IxOOpN1V7OMFWdlRRWQKuP+9wLBSTwwnhxori8uuqdlubIlyztV/uleN6hBZFAND7otmUZOTrC7sOCNq7XlporHSY3212m9jq73SBc4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=b2Qi97rK; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="b2Qi97rK" Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNqNZ6012634 for ; Tue, 29 Apr 2025 06:07:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= nqaSzHCbIu6hmV519m/rD6Dou/IY7N09Q8GeFAdiYcA=; b=b2Qi97rKDRqJH7rz ra47+8sjYSo1ttkS/YayqpjFxPIGR4olVTykP5PzGGmnAP+hgdFs3xEiNljzdUbj VDIh7VBGlCyz6l/v6fZ5LTkodgVIYmQGpdpnVyAx1McgwUqaw8hjrs2Vy0PwaORp On4YlWpaW6m/PTDGRNrDaCNyJ987T+cHE8yv0vApI24ZVheaqpvs0shc7z95QZAX 3vm+0V/HgO0I8HuZstj/BsXgivXCwW7NHq849NBthG5Njtzh3gvJw7rba8TdEnPp V9exiXy3VnkiarhqiiJodupru637AsCjp6pBA+OQoeIXvPXkaThwj1jfGELAeU8K HEMuUw== Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468q323crf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:03 +0000 (GMT) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-736c1ea954fso2853707b3a.3 for ; Mon, 28 Apr 2025 23:07:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906822; x=1746511622; 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=nqaSzHCbIu6hmV519m/rD6Dou/IY7N09Q8GeFAdiYcA=; b=ni22dGWOnlDGAlD61JDcUZki93xqUu7kHWj/IAlAHIyuP/MaHyBiya/XzrXalqXAt4 tF8kpYeKIZqXLmvkTMwKisuLqU4p5bzuFneCmuQ+fhbheKIK29/K+2+spMpF0wob5iUc q0oBtncfJxsYZRKV9Xus1efj1WKkmCXrQ4w6OB1lvwxWBBi/it4fkUQsKPwg0ONgL8tk dUTTOL3TBbiQ5yAheWvMl0jIaMYzRQNBw1j3P0cpxcf+rD4EPwZx0W8RCoOz4m0BmJLT w0EAwfH8pt7Q0KZIdn3ezpWYMKAMI+P7/KoltEgrDqrII6eHZL/Pmh1qkbT0hnH5jxGT zgEQ== X-Forwarded-Encrypted: i=1; AJvYcCVrBpeQPAE9nfV8c5jmAE0sPoXpWGYiIVik4GVjUcMqtDZ+rh5YJmtyNsAyzFChj2xnE1ajv4DDe9mUwlE=@vger.kernel.org X-Gm-Message-State: AOJu0YzZLTra4hB87RGCrlHIagc/CI+3ZKzW+cONrT4Ggr7pV1sSrsnz JxGoik5e5/JsSAbpif5ERkcofmzZIdBuR0j9GuvgKlFr+pmf9fCAYN4znQw6VAocvW8urJuv/TO JSloKBXyj2CUhHJBDcC3rchq6Y69i0b7cP2VY93uhdmAe7NuV/E8p0ADKZ3VtDg== X-Gm-Gg: ASbGncvqy1xnTyE+mB9mARjDvCVMeYm3WKw5JEBVnYHCQZC4hGypiEYNqzXRxd0NHL/ rqIPu4QmKG+GtJkdkybiSVBZZxldSq/rNmBS/FxuckdsrXfMJD6m31vvvmF0LmSonRGtN9m/D3G XznCAOKXuyIww01/qQtFTqeVVTy1tq0VS+MJUfPGeb4WV99fKAs1PBJbvOJbR7WcvxlIh4IrywF kUNXIoX+URHncAG57k4kXrXSEZHFImvdj1jxbkE37v2PhraBUOEEZ1xw4BaVcRs4YfQzI4l1/h9 MU9ljJTGSIdpmf/Ahid+TToGuMgbuu4k7riPKYPuWgTcPMvvdMEZXrMN/ZE+l0H9otG5K2Y= X-Received: by 2002:a05:6a00:39a8:b0:736:5725:59b9 with SMTP id d2e1a72fcca58-74028f9268emr2549222b3a.2.1745906821634; Mon, 28 Apr 2025 23:07:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHFEv6V5Tpr84FeHF/ZfJGFbkoO4ix0Gt3nO+nBQUToA9zLuAbRhNmsOjQ3n+amprRdKJ+VFg== X-Received: by 2002:a05:6a00:39a8:b0:736:5725:59b9 with SMTP id d2e1a72fcca58-74028f9268emr2549177b3a.2.1745906821225; Mon, 28 Apr 2025 23:07:01 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.06.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:00 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:25 -0700 Subject: [PATCH v4 04/11] tee: add TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-4-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi , Sumit Garg X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=M7xNKzws c=1 sm=1 tr=0 ts=68106c87 cx=c_pps a=WW5sKcV1LcKqjgzy2JUPuA==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=fw-SQdjKjSnpQ1BqlTsA:9 a=QEXdDO2ut3YA:10 a=OpyuDcXvxspvyRM73sMx:22 X-Proofpoint-GUID: LO41jeDYItGzdoI2PRB3H4cjYnxi9Nlm X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX4kTpiNyF3ENN SD+8K6oeQq4qlhBV7c5ZNT7XZ9Z2wgW75GfcpKx6DmJkaA9tDvpx2oMZafZTBzSUpBzKQAkpXQZ t0YfhSsR44zGUvPpMHbqAApj7L6dYchk1IpCV4JzAum6hII/AM8RlIp2zkgbbsdOvx5CS6RFN1P Mm7ENeD8UyDF5I9IEY7sjXhoX9qo8RpQvtuHT5l+xi9Ixs78cR5454b6FHQKczjuJCdCm087b5A vWDy8HlLlJPT5WbcxPzrngGw3V6RebiTJWxl5Mlb0xhwIV5wIW/oeFxocdn0unB5VyUV+PHzPjI A8n+6f3UW2PXcGgTEzDEBC2hWYSHwKe68TpX/+6xbpKihUkIoj33BNBdpuEnHZMzdK9xL2JyrPN Uy0Pso+SlNOpXhJOU1XIOavS7r/mQxfuqpF/WyYsZy6t2xEVm0fvb3d6qeoHEudZU6xwsHE0 X-Proofpoint-ORIG-GUID: LO41jeDYItGzdoI2PRB3H4cjYnxi9Nlm X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 priorityscore=1501 clxscore=1015 mlxlogscore=999 spamscore=0 phishscore=0 bulkscore=0 adultscore=0 lowpriorityscore=0 suspectscore=0 malwarescore=0 mlxscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 The TEE subsystem allows session-based access to trusted services, requiring a session to be established to receive a service. This is not suitable for an environment that represents services as objects. An object supports various operations that a client can invoke, potentially generating a result or a new object that can be invoked independently of the original object. Add TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT/OUTPUT/INOUT to represent an object. Objects may reside in either TEE or userspace. To invoke an object in TEE, introduce a new ioctl. Use the existing SUPPL_RECV and SUPPL_SEND to invoke an object in userspace. Reviewed-by: Sumit Garg Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/tee/tee_core.c | 85 ++++++++++++++++++++++++++++++++++++++++++++= ++++ include/linux/tee_core.h | 4 +++ include/linux/tee_drv.h | 6 ++++ include/uapi/linux/tee.h | 41 +++++++++++++++++++---- 4 files changed, 130 insertions(+), 6 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 7bf265c4ac65..fde8fab58398 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -379,6 +379,7 @@ static int params_from_user(struct tee_context *ctx, st= ruct tee_param *params, switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) { case TEE_IOCTL_PARAM_ATTR_TYPE_NONE: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT: break; case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: @@ -397,6 +398,11 @@ static int params_from_user(struct tee_context *ctx, s= truct tee_param *params, return -EFAULT; =20 break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INOUT: + params[n].u.objref.id =3D ip.a; + params[n].u.objref.flags =3D ip.b; + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: @@ -470,6 +476,12 @@ static int params_to_user(struct tee_ioctl_param __use= r *uparams, if (put_user((u64)p->u.ubuf.size, &up->b)) return -EFAULT; break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INOUT: + if (put_user(p->u.objref.id, &up->a) || + put_user(p->u.objref.flags, &up->b)) + return -EFAULT; + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: if (put_user((u64)p->u.memref.size, &up->b)) @@ -622,6 +634,66 @@ static int tee_ioctl_invoke(struct tee_context *ctx, return rc; } =20 +static int tee_ioctl_object_invoke(struct tee_context *ctx, + struct tee_ioctl_buf_data __user *ubuf) +{ + int rc; + size_t n; + struct tee_ioctl_buf_data buf; + struct tee_ioctl_object_invoke_arg __user *uarg; + struct tee_ioctl_object_invoke_arg arg; + struct tee_ioctl_param __user *uparams =3D NULL; + struct tee_param *params =3D NULL; + + if (!ctx->teedev->desc->ops->object_invoke_func) + return -EINVAL; + + if (copy_from_user(&buf, ubuf, sizeof(buf))) + return -EFAULT; + + if (buf.buf_len > TEE_MAX_ARG_SIZE || + buf.buf_len < sizeof(struct tee_ioctl_object_invoke_arg)) + return -EINVAL; + + uarg =3D u64_to_user_ptr(buf.buf_ptr); + if (copy_from_user(&arg, uarg, sizeof(arg))) + return -EFAULT; + + if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) !=3D buf.buf_len) + return -EINVAL; + + if (arg.num_params) { + params =3D kcalloc(arg.num_params, sizeof(struct tee_param), + GFP_KERNEL); + if (!params) + return -ENOMEM; + uparams =3D uarg->params; + rc =3D params_from_user(ctx, params, arg.num_params, uparams); + if (rc) + goto out; + } + + rc =3D ctx->teedev->desc->ops->object_invoke_func(ctx, &arg, params); + if (rc) + goto out; + + if (put_user(arg.ret, &uarg->ret)) { + rc =3D -EFAULT; + goto out; + } + rc =3D params_to_user(uparams, arg.num_params, params); +out: + if (params) { + /* Decrease ref count for all valid shared memory pointers */ + for (n =3D 0; n < arg.num_params; n++) + if (tee_param_is_memref(params + n) && + params[n].u.memref.shm) + tee_shm_put(params[n].u.memref.shm); + kfree(params); + } + return rc; +} + static int tee_ioctl_cancel(struct tee_context *ctx, struct tee_ioctl_cancel_arg __user *uarg) { @@ -677,6 +749,12 @@ static int params_to_supp(struct tee_context *ctx, ip.b =3D p->u.ubuf.size; ip.c =3D 0; break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INOUT: + ip.a =3D p->u.objref.id; + ip.b =3D p->u.objref.flags; + ip.c =3D 0; + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: @@ -789,6 +867,11 @@ static int params_from_supp(struct tee_param *params, = size_t num_params, return -EFAULT; =20 break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INOUT: + p->u.objref.id =3D ip.a; + p->u.objref.flags =3D ip.b; + break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: /* @@ -869,6 +952,8 @@ static long tee_ioctl(struct file *filp, unsigned int c= md, unsigned long arg) return tee_ioctl_open_session(ctx, uarg); case TEE_IOC_INVOKE: return tee_ioctl_invoke(ctx, uarg); + case TEE_IOC_OBJECT_INVOKE: + return tee_ioctl_object_invoke(ctx, uarg); case TEE_IOC_CANCEL: return tee_ioctl_cancel(ctx, uarg); case TEE_IOC_CLOSE_SESSION: diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index 284ca6b3e03a..eb891de81e12 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -72,6 +72,7 @@ struct tee_device { * @close_session: close a session * @system_session: declare session as a system session * @invoke_func: invoke a trusted function + * @object_invoke_func: invoke a TEE object * @cancel_req: request cancel of an ongoing invoke or open * @supp_recv: called for supplicant to get a command * @supp_send: called for supplicant to send a response @@ -97,6 +98,9 @@ struct tee_driver_ops { int (*invoke_func)(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, struct tee_param *param); + int (*object_invoke_func)(struct tee_context *ctx, + struct tee_ioctl_object_invoke_arg *arg, + struct tee_param *param); int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session); int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params, struct tee_param *param); diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 78bbf12f02f0..3b34fdcc855f 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -87,6 +87,11 @@ struct tee_param_ubuf { size_t size; }; =20 +struct tee_param_objref { + u64 id; + u64 flags; +}; + struct tee_param_value { u64 a; u64 b; @@ -97,6 +102,7 @@ struct tee_param { u64 attr; union { struct tee_param_memref memref; + struct tee_param_objref objref; struct tee_param_ubuf ubuf; struct tee_param_value value; } u; diff --git a/include/uapi/linux/tee.h b/include/uapi/linux/tee.h index 3e9b1ec5dfde..441d97add53f 100644 --- a/include/uapi/linux/tee.h +++ b/include/uapi/linux/tee.h @@ -48,8 +48,10 @@ #define TEE_GEN_CAP_PRIVILEGED (1 << 1)/* Privileged device (for supplican= t) */ #define TEE_GEN_CAP_REG_MEM (1 << 2)/* Supports registering shared memory = */ #define TEE_GEN_CAP_MEMREF_NULL (1 << 3)/* NULL MemRef support */ +#define TEE_GEN_CAP_OBJREF (1 << 4)/* Supports generic object reference */ =20 -#define TEE_MEMREF_NULL (__u64)(-1) /* NULL MemRef Buffer */ +#define TEE_MEMREF_NULL ((__u64)(-1)) /* NULL MemRef Buffer */ +#define TEE_OBJREF_NULL ((__u64)(-1)) /* NULL ObjRef Object */ =20 /* * TEE Implementation ID @@ -158,6 +160,13 @@ struct tee_ioctl_buf_data { #define TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT 9 #define TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INOUT 10 /* input and output */ =20 +/* + * These defines object reference parameters. + */ +#define TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT 11 +#define TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT 12 +#define TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INOUT 13 + /* * Mask for the type part of the attribute, leaves room for more types */ @@ -195,15 +204,16 @@ struct tee_ioctl_buf_data { * @attr: attributes * @a: if a memref, offset into the shared memory object, * else if a ubuf, address of the user buffer, - * else a value parameter - * @b: if a memref or ubuf, size of the buffer, else a value parameter + * else if an objref, object identifier, else a value parameter + * @b: if a memref or ubuf, size of the buffer, + * else if objref, flags for the object, else a value parameter * @c: if a memref, shared memory identifier, else a value parameter * * @attr & TEE_PARAM_ATTR_TYPE_MASK indicates if memref, ubuf, or value is * used in the union. TEE_PARAM_ATTR_TYPE_VALUE_* indicates value, - * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref, and TEE_PARAM_ATTR_TYPE_= UBUF_* - * indicates ubuf. TEE_PARAM_ATTR_TYPE_NONE indicates that none of the mem= bers - * are used. + * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref, TEE_PARAM_ATTR_TYPE_UBUF= _* + * indicates ubuf, and TEE_PARAM_ATTR_TYPE_OBJREF_* indicates objref. + * TEE_PARAM_ATTR_TYPE_NONE indicates that none of the members are used. * * Shared memory is allocated with TEE_IOC_SHM_ALLOC which returns an * identifier representing the shared memory object. A memref can reference @@ -411,4 +421,23 @@ struct tee_ioctl_shm_register_data { * munmap(): unmaps previously shared memory */ =20 +/** + * struct tee_ioctl_invoke_func_arg - Invokes an object in a Trusted Appli= cation + * @id: [in] Object id + * @op: [in] Object operation, specific to the object + * @ret: [out] return value + * @num_params [in] number of parameters following this struct + */ +struct tee_ioctl_object_invoke_arg { + __u64 id; + __u32 op; + __u32 ret; + __u32 num_params; + /* num_params tells the actual number of element in params */ + struct tee_ioctl_param params[]; +}; + +#define TEE_IOC_OBJECT_INVOKE _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 10, \ + struct tee_ioctl_buf_data) + #endif /*__TEE_H*/ --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5BBE2750FC for ; Tue, 29 Apr 2025 06:07:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906827; cv=none; b=ot37X07usuw4iWYq8t4gbLDgQDvSBwuVlqE6IA01vL1mUma6wpEVLUdtHNgdpjeE4dTXwdSvrCf7AiFG8+lfOwp5EwNlIQiPkneKFo3TCi/WTA0Kinvbsf8O0XI6lCr6PIBTTciBGusDR8x6c9LNTR2533paKEldNDro3D7DsQU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906827; c=relaxed/simple; bh=JCfwSuLDRnQSsxUrzp2UNscuJYVjvct1rfPgTZbmKh0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=thSJKhot2T3ptwJWZ0DMluhzpa9B06VJp+mnYvGwSO+mJRh33BGiinmAylUTINtKQ1B8/Icqt6kIOlFQYvwzdfM5mTBLdLtrf2fNe0ZXJrQw65E4z/zcoU4gzml+btsDuvGzJ+TBqAxcbTMmkf4zOgRRfoGzIQ2GGkq0VP4EX68= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=bVw15nav; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="bVw15nav" Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNq1cv015281 for ; Tue, 29 Apr 2025 06:07:05 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= dXaMJm9zqA3fJKLXrRirVlQ9wrTmTCRFVGcozylZPRI=; b=bVw15navvi1MF1Fo WnGtcMdDyblTEh0b4Ugpl6uE+KSLFjXm/9efa4YkH2zIVjtcp3ZSFvGV4Uc5H41m 7B00OyX1SBqvOHWHwxGOnbFB0FjRRTS6mJDf+TRyUZdmipQnLI4CPi5emH8X2YdP a83KHQBE5GuwhaQxu61X4aRnaavktiJBt9Bi1SPWFpMX/HtH4Xi2PNYqPwEMpGAX pzzRzC6E21iFTcNjP2o517Agw+W4ecyAlssFEd6sv56cTk9sWA4HVEU37WdOMXs2 P0mpWPZDxVwa6Kqm0UAIW/laAioJ9jf+mW4c6u6gUnvK2XduIkoMKFRguDMzz1z1 NLBNXg== Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468qjwucnv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:04 +0000 (GMT) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-736b5f9279cso4943195b3a.2 for ; Mon, 28 Apr 2025 23:07:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906823; x=1746511623; 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=dXaMJm9zqA3fJKLXrRirVlQ9wrTmTCRFVGcozylZPRI=; b=jLqMSaFsrmk+4TsY6sdwkbb+8Z8/TMTRErnc+H9EoWMdSxex9CZACnYc5PZyFm7vLT GjZglnYzUwJm7t0LYIhZDBtBNzRo+RZbD7NO291Rne1thhSthy2f9gsGjLJQNJSS1ff3 7d05iRDXo8p9Wd+wctSCYyTd4Vcn+TcW38rTzderwiclAFRmQPD4NeXzJvFMz6vmYwnp PZWKepQiEqhPtCDFpiH0vc3rY7nekBx2gL+pqny5XxlY6fc9YsXhHk4x5hoTKdwQQTSS scABwxO1UtXvGHguKlY8RkX282CPtQnG6coucM0CTeKtPaHudmF3cEPeexg6x+hHQP4V 7zBw== X-Forwarded-Encrypted: i=1; AJvYcCXCN6G75T99SHIWOhNVtTTX7ZLC4GViXnv3USxVtQi5rHqyOjmcPmlAu0aBP7nDC/TT2vfHMCUODgtd3dw=@vger.kernel.org X-Gm-Message-State: AOJu0YwuwzGvuG9G+IBv0T+1qiz8YZnzCbYHsMO4z4vweMM3HluG4EpA iMsd4L7giqA/DItZb0zzkQyvhqYMLNUopQD7ypasJSveHlOk9wggkTNuTBxawqVOAyTpPCNPDrO 2vj0aypxkrJ+BPUD+ptlDypffaPm1jjQKfmcQPss1KQrKpLZUyxCq7pPDWlaqCw== X-Gm-Gg: ASbGncsUlnTtkUs/iMk0dwJyGo/YoPlIoHlIaIQf6hhsW58qGjnN4NOtVCbZldkIy25 XpjJPyevgwhA7uJqtVAuo84XsVwFIyg9VtHwDg70PTEkFeBJ7emm7SdW2aWChfmIzKBOkcVyZfy AFhznO/WB+/9Pbq7vd3mmYyTvWH4OHCxbt+IPYnrSplPaT/nB3Uyf64TPGrL8MKvnJ3ADz7qkRE 2Sp8f5+ZKaTe76W5b01mXqBIk6h959tMt0eAhH8h8BBEEIcL5TNzusgMDo0GGkCznwPpmBIul63 mdd2oou2YNy1pTWviwhnCIOkTXyOi8kgDreaW8GwMrWRWr/LMfaBPhS0iPEYwF+567Hma9Q= X-Received: by 2002:a05:6a00:4b09:b0:73e:2359:4115 with SMTP id d2e1a72fcca58-73ff7397c0fmr15889197b3a.23.1745906823309; Mon, 28 Apr 2025 23:07:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWs/sk/IuEyl57PWfZD9bbdAgc9G0v7sfhSv2D8g1GgEL+YywAjS4cO2vAxjNDzvM3fsz1Pg== X-Received: by 2002:a05:6a00:4b09:b0:73e:2359:4115 with SMTP id d2e1a72fcca58-73ff7397c0fmr15889153b3a.23.1745906822832; Mon, 28 Apr 2025 23:07:02 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.07.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:02 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:26 -0700 Subject: [PATCH v4 05/11] firmware: qcom: scm: add support for object invocation 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-5-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Proofpoint-ORIG-GUID: gA4vIkTAFHQMtojetTE7XUwydNeBoD2U X-Proofpoint-GUID: gA4vIkTAFHQMtojetTE7XUwydNeBoD2U X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX/sXgRqeD+OHT 6Gwci0fnS22KeLxm+mZwn7tkPvBWZa4k1k5TW3NWUQeVx0NggxD78ZJBMVXAZuijB14DJobbgCc bzKtwcCJk5LHduoJhwHwXHchy+Sg3Fkfy9XDqpTv3XCePWYLSGJpqeKOGluZorfpWLlrx6GmtCt IyF0TRQ58dClnV0yCgYgqALB4CVlR/z7QsjmUxgWAWJleZ0QKUZBdeJnzhCOco4+JwgWhjlksVj m5Zo3NfBgbadzEMORnzcaUa0oTo/JSNl4osrwSSQPLIHjYnQckM59ekN3YChJukrLnxbXW2AZ86 zaodFpYOn8temj0fCdjVVR9QGhbC40iGHW9X0Ge4Ss9Uwk9uPFo7OaxjUqGf0AH3zCEnaoChH2T i1r/tmae8BSavJclsMogXqvzQTMLxYHN6/nqQ8nWZFN8ztmAwzAvJFk6mStLsXLYJcwZ387H X-Authority-Analysis: v=2.4 cv=c/urQQ9l c=1 sm=1 tr=0 ts=68106c88 cx=c_pps a=WW5sKcV1LcKqjgzy2JUPuA==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=dBYAKgxw8cyf2mnx-aAA:9 a=QEXdDO2ut3YA:10 a=OpyuDcXvxspvyRM73sMx:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 mlxlogscore=999 phishscore=0 lowpriorityscore=0 impostorscore=0 mlxscore=0 malwarescore=0 clxscore=1015 spamscore=0 adultscore=0 priorityscore=1501 suspectscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 Qualcomm TEE (QTEE) hosts Trusted Applications (TAs) and services in the secure world, accessed via objects. A QTEE client can invoke these objects to request services. Similarly, QTEE can request services from the nonsecure world using objects exported to the secure world. Add low-level primitives to facilitate the invocation of objects hosted in QTEE, as well as those hosted in the nonsecure world. If support for object invocation is available, the qcom_scm allocates a dedicated child platform device. The driver for this device communicates with QTEE using low-level primitives. Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/firmware/qcom/qcom_scm.c | 128 +++++++++++++++++++++++++++++= ++++ drivers/firmware/qcom/qcom_scm.h | 7 ++ include/linux/firmware/qcom/qcom_scm.h | 27 +++++++ 3 files changed, 162 insertions(+) diff --git a/drivers/firmware/qcom/qcom_scm.c b/drivers/firmware/qcom/qcom_= scm.c index fc4d67e4c4a6..bff1b0d3306e 100644 --- a/drivers/firmware/qcom/qcom_scm.c +++ b/drivers/firmware/qcom/qcom_scm.c @@ -2084,6 +2084,124 @@ static int qcom_scm_qseecom_init(struct qcom_scm *s= cm) =20 #endif /* CONFIG_QCOM_QSEECOM */ =20 +#ifdef CONFIG_QCOMTEE + +/** + * qcom_scm_qtee_invoke_smc() - Invoke a QTEE object. + * @inbuf: start address of memory area used for inbound buffer. + * @inbuf_size: size of the memory area used for inbound buffer. + * @outbuf: start address of memory area used for outbound buffer. + * @outbuf_size: size of the memory area used for outbound buffer. + * @result: result of QTEE object invocation. + * @response_type: response type returned by QTEE. + * + * @response_type determines how the contents of @inbuf and @outbuf + * should be processed. + * + * Return: On success, return 0 or <0 on failure. + */ +int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf_size, + phys_addr_t outbuf, size_t outbuf_size, + u64 *result, u64 *response_type) +{ + struct qcom_scm_desc desc =3D { + .svc =3D QCOM_SCM_SVC_SMCINVOKE, + .cmd =3D QCOM_SCM_SMCINVOKE_INVOKE, + .owner =3D ARM_SMCCC_OWNER_TRUSTED_OS, + .args[0] =3D inbuf, + .args[1] =3D inbuf_size, + .args[2] =3D outbuf, + .args[3] =3D outbuf_size, + .arginfo =3D QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL, + QCOM_SCM_RW, QCOM_SCM_VAL), + }; + struct qcom_scm_res res; + int ret; + + ret =3D qcom_scm_call(__scm->dev, &desc, &res); + if (ret) + return ret; + + *response_type =3D res.result[0]; + *result =3D res.result[1]; + + return 0; +} +EXPORT_SYMBOL(qcom_scm_qtee_invoke_smc); + +/** + * qcom_scm_qtee_callback_response() - Submit response for callback reques= t. + * @buf: start address of memory area used for outbound buffer. + * @buf_size: size of the memory area used for outbound buffer. + * @result: Result of QTEE object invocation. + * @response_type: Response type returned by QTEE. + * + * @response_type determines how the contents of @buf should be processed. + * + * Return: On success, return 0 or <0 on failure. + */ +int qcom_scm_qtee_callback_response(phys_addr_t buf, size_t buf_size, + u64 *result, u64 *response_type) +{ + struct qcom_scm_desc desc =3D { + .svc =3D QCOM_SCM_SVC_SMCINVOKE, + .cmd =3D QCOM_SCM_SMCINVOKE_CB_RSP, + .owner =3D ARM_SMCCC_OWNER_TRUSTED_OS, + .args[0] =3D buf, + .args[1] =3D buf_size, + .arginfo =3D QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL), + }; + struct qcom_scm_res res; + int ret; + + ret =3D qcom_scm_call(__scm->dev, &desc, &res); + if (ret) + return ret; + + *response_type =3D res.result[0]; + *result =3D res.result[1]; + + return 0; +} +EXPORT_SYMBOL(qcom_scm_qtee_callback_response); + +static void qcom_scm_qtee_free(void *data) +{ + struct platform_device *qtee_dev =3D data; + + platform_device_unregister(qtee_dev); +} + +static int qcom_scm_qtee_init(struct qcom_scm *scm) +{ + struct platform_device *qtee_dev; + int ret; + + /* Setup QTEE interface device. */ + qtee_dev =3D platform_device_alloc("qcomtee", -1); + if (!qtee_dev) + return -ENOMEM; + + qtee_dev->dev.parent =3D scm->dev; + + ret =3D platform_device_add(qtee_dev); + if (ret) { + platform_device_put(qtee_dev); + return ret; + } + + return devm_add_action_or_reset(scm->dev, qcom_scm_qtee_free, qtee_dev); +} + +#else + +static int qcom_scm_qtee_init(struct qcom_scm *scm) +{ + return 0; +} + +#endif /* CONFIG_QCOMTEE */ + /** * qcom_scm_is_available() - Checks if SCM is available */ @@ -2319,6 +2437,16 @@ static int qcom_scm_probe(struct platform_device *pd= ev) ret =3D qcom_scm_qseecom_init(scm); WARN(ret < 0, "failed to initialize qseecom: %d\n", ret); =20 + /* + * Initialize the QTEE object interface. + * + * This only represents the availability for QTEE object invocation + * and callback support. On failure, ignore the result. Any subsystem + * depending on it may fail if it tries to access this interface. + */ + ret =3D qcom_scm_qtee_init(scm); + WARN(ret < 0, "failed to initialize qcomtee: %d\n", ret); + return 0; =20 err: diff --git a/drivers/firmware/qcom/qcom_scm.h b/drivers/firmware/qcom/qcom_= scm.h index 097369d38b84..a25202e99f7c 100644 --- a/drivers/firmware/qcom/qcom_scm.h +++ b/drivers/firmware/qcom/qcom_scm.h @@ -152,6 +152,13 @@ struct qcom_tzmem_pool *qcom_scm_get_tzmem_pool(void); #define QCOM_SCM_SVC_GPU 0x28 #define QCOM_SCM_SVC_GPU_INIT_REGS 0x01 =20 +/* ARM_SMCCC_OWNER_TRUSTED_OS calls */ + +#define QCOM_SCM_SVC_SMCINVOKE 0x06 +#define QCOM_SCM_SMCINVOKE_INVOKE_LEGACY 0x00 +#define QCOM_SCM_SMCINVOKE_CB_RSP 0x01 +#define QCOM_SCM_SMCINVOKE_INVOKE 0x02 + /* common error codes */ #define QCOM_SCM_V2_EBUSY -12 #define QCOM_SCM_ENOMEM -5 diff --git a/include/linux/firmware/qcom/qcom_scm.h b/include/linux/firmwar= e/qcom/qcom_scm.h index 983e1591bbba..bf5e64f6deba 100644 --- a/include/linux/firmware/qcom/qcom_scm.h +++ b/include/linux/firmware/qcom/qcom_scm.h @@ -176,4 +176,31 @@ static inline int qcom_scm_qseecom_app_send(u32 app_id, =20 #endif /* CONFIG_QCOM_QSEECOM */ =20 +#ifdef CONFIG_QCOMTEE + +int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf_size, + phys_addr_t outbuf, size_t outbuf_size, + u64 *result, u64 *response_type); +int qcom_scm_qtee_callback_response(phys_addr_t buf, size_t buf_size, + u64 *result, u64 *response_type); + +#else /* CONFIG_QCOMTEE */ + +static inline int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf= _size, + phys_addr_t outbuf, + size_t outbuf_size, u64 *result, + u64 *response_type) +{ + return -EINVAL; +} + +static inline int qcom_scm_qtee_callback_response(phys_addr_t buf, + size_t buf_size, u64 *result, + u64 *response_type) +{ + return -EINVAL; +} + +#endif /* CONFIG_QCOMTEE */ + #endif --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B490F275849 for ; Tue, 29 Apr 2025 06:07:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906829; cv=none; b=eiBHHmvUPoHyEHwDG8QdySMh+j07oqO1QbwZJX+VIAN0jbLr0UIhrtFOYOt2Hy61cFirp4Oz1Jy5hDa2OvqdHjdnuIn187uw0G9bafk3MwsMn2fjH0dKehvavaIzOzhriQ9APRM7iLJLIGWQUEdp0MoptKe44Deok+DN57JymFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906829; c=relaxed/simple; bh=W5kC8N4id0jgjxPJzXuRXDTRcStSEatZUDiitY0n/q4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pLKLVvbJzh9u1GAfv9bLbV+PWEwVkk/7vyeIGtgadfVjPMzsoOoZrC+pzBgiJ6PSE7odRSsm0M3NWHwCa3aBBwRiiaLtk7kWIRyfbw09YxC7qYUdUSD03QGYuPypuhFYYfS34du2ezPamfidM5hnl7LWzAZFUu0bjI0aMzPzDLk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=JEX7SZIH; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="JEX7SZIH" Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNq4VN006066 for ; Tue, 29 Apr 2025 06:07:06 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= lg7Nx8pcQfWvwE/UDeFmGOhXwmDLJxS5vqnqaGQtRx4=; b=JEX7SZIHI969GEMr JsOaxoBPsOlCLrnFs9nGvRYTDD/3Ov7nOylb7CX3dcX84YCpEBgCyWFf6eTtpLyy D5dkvH++tyM3lRlqaCAo6qgsoaB/XoSH6PR/P0bOuDOTQgCz/YvZIOTSXIdjbCye KytCGXY89XS/icWX1DvXuNEeBW/0PtE5z/uV19NCbECqllaELKWkGkTcAIBx9ena c8+98nusZlRV9SnEV+Na/fQ83EkjAxrwYSDVHrSDS2C/I7doaH+2OvBABtnLsi5E bYagRXcYW40cgeOW3nprEM27gk1JykKr8XU5xhzVnbui+gyu7sRpb3HaDet/LBop gOmTyw== Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468ptmkdug-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:06 +0000 (GMT) Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-6c8f99fef10so5974640a12.3 for ; Mon, 28 Apr 2025 23:07:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906825; x=1746511625; 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=lg7Nx8pcQfWvwE/UDeFmGOhXwmDLJxS5vqnqaGQtRx4=; b=cS5sxQrQUPB+4sRt1MFaL85CSssMRGIyVGX8zWzLOod25ZmlCYhcXtYkla20ihhoeE qoG9xIows8TN3RH4GfAUWNVEqE7u6Tkpc136iAp8fBJSrIhnd4ub1JvcOfgtUQ4K40GL EtdIJqhHLolQ47631fThlaXvrC4r3niWPEmM/pNMvWRL6pKKVjEiVl88zWqARmbY51lp K/2SfXPVY3o0aiRC35BmbKD/OdO7Ojwm+rVq87yzV6YOfCnecorh5t7gC9w45Xn4wfo0 Lc773r1zlhxWATkTpj1MGftpyEG8T+YairnuRoCH/4O0K0ZYMcfPz/Q9YZ1EPJoKmqiu jYBQ== X-Forwarded-Encrypted: i=1; AJvYcCVgiLkhJqyxkk+RTnGRnXNU/mjoNYmHNlJbrv+4KWZ42u/3MGnEOxPFGyLpzDpzGMkf4b5KfdYxat3sj8U=@vger.kernel.org X-Gm-Message-State: AOJu0YyfRAUTgLwdCmFN7M8mYi4tzNe9u5gWNaHHN0pHLxCPHtHjIBtn 2/pbC9QVx5XXkVVXg9IQnOoN3bwp6ZfctoxYDgQwT8YyHHPOlnnsEAwOT7RoRbeCWh7BSk3WEmW M80TKXlD56MBCzIFkuIH1UP/l5WW4MstjKHWOX7Wv25xsWtHrS2ernjAgwwBaeA== X-Gm-Gg: ASbGncs0/LCTsla4iHV+oLBLfH2rPg+IZbNLuA5MtwgKGbQmUaaZ9wOeKHx83+ervCK vjnVm4A4neUa6TMeenYTBkf6G3PBFPRiEQB/8TTsQAC7K8QmrXolGKg2H8rhV4wUoHYQHZMUUGZ PxdPuSPPnZbSpxTkcVhE76njJnhXnum3v0M5LT3DwtoU/QY7J2Cm67y5jJ/21nSgUUCBsJXhHQ9 FUylXBm98VOzaldpa3SLvvPVkod5PP2WOF5MNRUtHKenbY7CFXfOzxXUP3j3NH4nofpH+ftUpk7 T66SJMxBjzT+yj6R/t524cEYzWr8GFlAOkcvReNLdLQP7OimxcuHHIJOHdD5Vgx3vipQzDg= X-Received: by 2002:a05:6a20:6f02:b0:1f5:9175:2596 with SMTP id adf61e73a8af0-2046a4660f7mr17453251637.13.1745906824830; Mon, 28 Apr 2025 23:07:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEGglvLAHgrOV7iGI0hyH1ZbjgDlYIBAPRGE9nqhg3e33A4GqQ2E/mk1KOyhJNYbdwEI3f1mQ== X-Received: by 2002:a05:6a20:6f02:b0:1f5:9175:2596 with SMTP id adf61e73a8af0-2046a4660f7mr17453225637.13.1745906824454; Mon, 28 Apr 2025 23:07:04 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.07.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:03 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:27 -0700 Subject: [PATCH v4 06/11] firmware: qcom: scm: remove unused arguments to the shm_brige 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-6-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Proofpoint-ORIG-GUID: u8bzdMvOB7q7GobGd1mS1llIq0EQm1BH X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX6OQN21JlH8Hd qclUjU3d7Je2yvoHwk5K9f6YBA9zcKMFBySudC+kxSCiwbpjAoqq2X+TjSG/ildd46R7ejQYgQz LPI9x+O1KIn/3Y9ocuI8/ObZHnDksM0wJfOPhL+HxtNXPoQp3DKgPSdl9ELIvDeo9J8z9yGsP7l YUi7b5ws1pdJiTlwbtDhVLZ6VWzJWVB5b69ip1jOK3itMwpF/EkCvuWGQ0hVtMI+68vaP0M1Ow+ o0jNSuMtVBAMlfaJtriunJP+aYMcfjmrGZclSaHR/f4jX4bZJzgjebdMKl6RFNMPhW+4xuTparP PrEjwlwbjXXZOEUQe9kriuxr1KZI5ZTE1yJaZzi7hqkJ/qkTX6IkgKwMS4JkNHpQxlNUYtsLuyI luuUrxA4oP5CNBeNCLPwMkx2VtbNZNyyT1tHFIpaYFOOSOZyWVPiW4JKFLgzubC7wY/j+BRA X-Proofpoint-GUID: u8bzdMvOB7q7GobGd1mS1llIq0EQm1BH X-Authority-Analysis: v=2.4 cv=DKWP4zNb c=1 sm=1 tr=0 ts=68106c8a cx=c_pps a=Qgeoaf8Lrialg5Z894R3/Q==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=ViJ-rRY3MSY-B2NivjAA:9 a=QEXdDO2ut3YA:10 a=x9snwWr2DeNwDh03kgHS:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 clxscore=1015 priorityscore=1501 impostorscore=0 mlxscore=0 bulkscore=0 malwarescore=0 phishscore=0 spamscore=0 suspectscore=0 lowpriorityscore=0 adultscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 shm_bridge create/delete functions always use the scm device. There is no need to pass it as an argument. Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/firmware/qcom/qcom_scm.c | 4 ++-- drivers/firmware/qcom/qcom_tzmem.c | 8 ++++---- include/linux/firmware/qcom/qcom_scm.h | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/firmware/qcom/qcom_scm.c b/drivers/firmware/qcom/qcom_= scm.c index bff1b0d3306e..9fd5f900d327 100644 --- a/drivers/firmware/qcom/qcom_scm.c +++ b/drivers/firmware/qcom/qcom_scm.c @@ -1631,7 +1631,7 @@ int qcom_scm_shm_bridge_enable(void) } EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_enable); =20 -int qcom_scm_shm_bridge_create(struct device *dev, u64 pfn_and_ns_perm_fla= gs, +int qcom_scm_shm_bridge_create(u64 pfn_and_ns_perm_flags, u64 ipfn_and_s_perm_flags, u64 size_and_flags, u64 ns_vmids, u64 *handle) { @@ -1659,7 +1659,7 @@ int qcom_scm_shm_bridge_create(struct device *dev, u6= 4 pfn_and_ns_perm_flags, } EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_create); =20 -int qcom_scm_shm_bridge_delete(struct device *dev, u64 handle) +int qcom_scm_shm_bridge_delete(u64 handle) { struct qcom_scm_desc desc =3D { .svc =3D QCOM_SCM_SVC_MP, diff --git a/drivers/firmware/qcom/qcom_tzmem.c b/drivers/firmware/qcom/qco= m_tzmem.c index 92b365178235..548dbd346b1b 100644 --- a/drivers/firmware/qcom/qcom_tzmem.c +++ b/drivers/firmware/qcom/qcom_tzmem.c @@ -123,9 +123,9 @@ static int qcom_tzmem_init_area(struct qcom_tzmem_area = *area) if (!handle) return -ENOMEM; =20 - ret =3D qcom_scm_shm_bridge_create(qcom_tzmem_dev, pfn_and_ns_perm, - ipfn_and_s_perm, size_and_flags, - QCOM_SCM_VMID_HLOS, handle); + ret =3D qcom_scm_shm_bridge_create(pfn_and_ns_perm, ipfn_and_s_perm, + size_and_flags, QCOM_SCM_VMID_HLOS, + handle); if (ret) return ret; =20 @@ -141,7 +141,7 @@ static void qcom_tzmem_cleanup_area(struct qcom_tzmem_a= rea *area) if (!qcom_tzmem_using_shm_bridge) return; =20 - qcom_scm_shm_bridge_delete(qcom_tzmem_dev, *handle); + qcom_scm_shm_bridge_delete(*handle); kfree(handle); } =20 diff --git a/include/linux/firmware/qcom/qcom_scm.h b/include/linux/firmwar= e/qcom/qcom_scm.h index bf5e64f6deba..33fde08dce70 100644 --- a/include/linux/firmware/qcom/qcom_scm.h +++ b/include/linux/firmware/qcom/qcom_scm.h @@ -149,10 +149,10 @@ bool qcom_scm_lmh_dcvsh_available(void); int qcom_scm_gpu_init_regs(u32 gpu_req); =20 int qcom_scm_shm_bridge_enable(void); -int qcom_scm_shm_bridge_create(struct device *dev, u64 pfn_and_ns_perm_fla= gs, +int qcom_scm_shm_bridge_create(u64 pfn_and_ns_perm_flags, u64 ipfn_and_s_perm_flags, u64 size_and_flags, u64 ns_vmids, u64 *handle); -int qcom_scm_shm_bridge_delete(struct device *dev, u64 handle); +int qcom_scm_shm_bridge_delete(u64 handle); =20 #ifdef CONFIG_QCOM_QSEECOM =20 --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AD39275870 for ; Tue, 29 Apr 2025 06:07:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906830; cv=none; b=X4SHtpYCVMIA5GGMlCtnHsCMm756dF/6NrKuxQQNMS1qbu01Z3SFxdO1XKtQZujbFUm3WkPKwf0vnO+OMYBiNTEWxRWz+mesQ+TBIJgEDWPGcI1ovdxI1YJA+etJi1Lts8Q/oopZYvY5bch8Aqmwkfyoyd23Jpxk56xgqxtLUDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906830; c=relaxed/simple; bh=NZ9UoQrDtsa/bdxaCCDtRbtSyRUdRiMoDWerA6WxR6c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J9ywtGmj9H7LpoQJlpAzBgx/TL5xImI2xhBCShNajWl6ATpa/haWGTa3yUAld7N2edKz359hVhXD1TBaI95HO4le1pnt5K1bZpsbefG3DIvXGXDWSPOmdHV74stFbTwNv9g+qlHuhVL+a6832fnMtgqFBihxSO7ny6ds8L251Bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=ZtGsl3FO; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="ZtGsl3FO" Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNq3ru011884 for ; Tue, 29 Apr 2025 06:07:08 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= IWH0nupKGoXHcFbzQxDgDKgTTIrB45evqE6KF6xol4Q=; b=ZtGsl3FOipkDgsFh sqDrYwvaDlCxRraUKyTkAY2kyqTLRhOpjEueW1qs4N1ci4Uq0fQk2V19q8Fd7+g/ saIziV9O/f4wPl4M1WqF+s9jdoP8qyqhrEQr1+G3iuRixS7YIzhVaR3FLgVAMjBl i+YFR9AFQes6TpagJlMJwMdzCzbXNRqhb9pz5Dnr1MHZYOyOYoWYmCIzC79JnVSH YC00uGLspjsZpfWy3AT07Fkb4WWYbT91+puv48Rj1ryPRmOZ4O1CC4JH2Bmh7x+D N8pwe11pumMtETD09Y3nDZj+puX41l6+exAEGcf1+dOhqR0M2ZdPVNWDQTGrj8l2 d5PeyQ== Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468q323cs2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:07 +0000 (GMT) Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-7395095a505so4253623b3a.1 for ; Mon, 28 Apr 2025 23:07:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906826; x=1746511626; 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=IWH0nupKGoXHcFbzQxDgDKgTTIrB45evqE6KF6xol4Q=; b=ivp3LGQ+kunnKDn7iYrIAlBSmvaBKy8iWvKfgx6+ckkvatMkm9E/pzDo9jkfz8ZqvV V9z6hPwY3EQCrL7wkQou0CspIAWbjQHlGmH+8w3JEqWMsEabHC6SJ/z6n9rz9EihFb70 EeFo+WTGXu+1sp/OozwpHARXLM5/y6ze4p0e4V4hgwxzEmUAyRqc6LVx8yFjt4BOeEQK 2Pp0mixRVMwySMcTH5luk0c4ZzzpBO457VrvEJz7sAJZwZQb7bM2ebwcMepZbrTkHxZc Bk8uaMflWuC1e6TsUE2eVV4kBY7P+wRPMGaz9vuhQV0p5kjl0990afJXh1cwSnlP6occ U/PA== X-Forwarded-Encrypted: i=1; AJvYcCXUrUwdPdeTjnEbG5Vn1jM3b18AqMfXtkWbVqcfHKF+q/AC3Vh90VKk1pxSQBPSFuWswRiX5QEQOYMGESM=@vger.kernel.org X-Gm-Message-State: AOJu0YwneCe071tZ+bdQ5ebH2SxvKH9soSJWUFliCjzfTxGQxbjc5FEf P8xbvThDqgCIIN7gxvJ177udQ9CFSGyEyguULk1hHyi2jTyj0V8id1Rfs6Up3JYt+GAUD8ur/uw H//fEp8b1CThD2f0jQUje0u6ZSwhsgoaA7MJCi5OEOJpilh2qX7Zp+kdMIf3oig== X-Gm-Gg: ASbGnct4qyiWiWJsJXykuUOkSxGJOqNv50tDhPvkEoh1FY0Twpssxmm3SsRxyiRSuEm /Dh0GMR9EUZj9UBgi6xKThj5Nyxkqf/qhWFmqAikZK9GlC+e5P4jA4jBtQuvDkNnPE78j08wszC HGBfphYezQyzfgcjdNvANB6juFJu7P45F6CECWYehgm9DMNUXIprPk2ZhF0RT4oRsKKyvCd3ZZU LBOK+jmvUaqCJuC7AbjoEpTV84R5jr7vr9VxkN8ctp3r9YpX5GIE8A4QSBSPRjCPHuqwv+nHjty XBbz8rgvBE+p5uitNPCJJNgw/wiUBfFVvt3B8X+O1HN4Lo5WTiLt4ZmdKxeQ9sBWYN60qCE= X-Received: by 2002:a05:6a00:ac2:b0:73c:b86:b47f with SMTP id d2e1a72fcca58-73ff7246ffamr16789998b3a.4.1745906826418; Mon, 28 Apr 2025 23:07:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE/Iw5QWLjVAX0CA1a1Lr5eZnBvSqfuSz+6cNc/A6mOFVZZHAZTnGn+CTOmtVDh40vcM4lMOQ== X-Received: by 2002:a05:6a00:ac2:b0:73c:b86:b47f with SMTP id d2e1a72fcca58-73ff7246ffamr16789968b3a.4.1745906826019; Mon, 28 Apr 2025 23:07:06 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.07.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:05 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:28 -0700 Subject: [PATCH v4 07/11] firmware: qcom: tzmem: export shm_bridge create/delete 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-7-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=M7xNKzws c=1 sm=1 tr=0 ts=68106c8b cx=c_pps a=m5Vt/hrsBiPMCU0y4gIsQw==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=F9FMNDSCD7THEPBdllMA:9 a=QEXdDO2ut3YA:10 a=IoOABgeZipijB_acs4fv:22 X-Proofpoint-GUID: -VQPZ78_04dkEht37Q8rZJ8k9LZcQaDs X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfXw/WkuTFuwCp9 Hn6meu7AUJXRBfhkaqqrwlej6Vmtng7Frg77qIdmrmtTiEOUy9POl2+iv6VknzE6omr8AiDywbk B660H67+DApkyGRzde5NJ2HpbSYfhIG1bLWngmqknMzFRIUko1qgFwAZ7E6xxjAmrACz4ZRIKI0 EG0UoQVZkty+eyUPLDzQuc/I3qg/JJZqjRBX4DLG5r0IaWrMGA44A6gqTvLLeTEvfeKExhqlX09 Ms2hc0j6Xyc4X72czWrdM9nlVZuuR2Krks1MKzvGBiWSa7vFDCLLna0CqVrEDZaLU1QwDRcNBtZ my324+hIeHMOD12gx8T6XqXx5wwaXSTlOyMHqpDSuARVNoGWAUR0F48rezYsaM/3YlZkm4HEz7H /qlPmM4n/+emKBm7wjxohIOBJfgylqI6E/KV3lTpbGT0NBM7h8WIAyr/o8IdHRdKNKAo+ALw X-Proofpoint-ORIG-GUID: -VQPZ78_04dkEht37Q8rZJ8k9LZcQaDs X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 priorityscore=1501 clxscore=1015 mlxlogscore=999 spamscore=0 phishscore=0 bulkscore=0 adultscore=0 lowpriorityscore=0 suspectscore=0 malwarescore=0 mlxscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 Anyone with access to contiguous physical memory should be able to share memory with QTEE using shm_bridge. Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/firmware/qcom/qcom_tzmem.c | 57 +++++++++++++++++++++++++---= ---- include/linux/firmware/qcom/qcom_tzmem.h | 15 +++++++++ 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/drivers/firmware/qcom/qcom_tzmem.c b/drivers/firmware/qcom/qco= m_tzmem.c index 548dbd346b1b..a02913665801 100644 --- a/drivers/firmware/qcom/qcom_tzmem.c +++ b/drivers/firmware/qcom/qcom_tzmem.c @@ -107,25 +107,61 @@ static int qcom_tzmem_init(void) return 0; } =20 -static int qcom_tzmem_init_area(struct qcom_tzmem_area *area) +/** + * qcom_tzmem_shm_bridge_create() - Create a SHM bridge. + * @paddr: Physical address of the memory to share. + * @size: Size of the memory to share. + * @handle: Handle to the SHM bridge. + * + * On platforms that support SHM bridge, this function creates a SHM bridge + * for the given memory region with QTEE. The handle returned by this func= tion + * must be passed to qcom_tzmem_shm_bridge_delete() to free the SHM bridge. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, size_t size, u64 *hand= le) { u64 pfn_and_ns_perm, ipfn_and_s_perm, size_and_flags; - int ret; =20 if (!qcom_tzmem_using_shm_bridge) return 0; =20 - pfn_and_ns_perm =3D (u64)area->paddr | QCOM_SCM_PERM_RW; - ipfn_and_s_perm =3D (u64)area->paddr | QCOM_SCM_PERM_RW; - size_and_flags =3D area->size | (1 << QCOM_SHM_BRIDGE_NUM_VM_SHIFT); + pfn_and_ns_perm =3D paddr | QCOM_SCM_PERM_RW; + ipfn_and_s_perm =3D paddr | QCOM_SCM_PERM_RW; + size_and_flags =3D size | (1 << QCOM_SHM_BRIDGE_NUM_VM_SHIFT); + if (qcom_scm_shm_bridge_create(pfn_and_ns_perm, ipfn_and_s_perm, + size_and_flags, QCOM_SCM_VMID_HLOS, + handle)) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_create); + +/** + * qcom_tzmem_shm_bridge_delete() - Delete a SHM bridge. + * @handle: Handle to the SHM bridge. + * + * On platforms that support SHM bridge, this function deletes the SHM bri= dge + * for the given memory region. The handle must be the same as the one + * returned by qcom_tzmem_shm_bridge_create(). + */ +void qcom_tzmem_shm_bridge_delete(u64 handle) +{ + if (qcom_tzmem_using_shm_bridge) + qcom_scm_shm_bridge_delete(handle); +} +EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_delete); + +static int qcom_tzmem_init_area(struct qcom_tzmem_area *area) +{ + int ret; =20 u64 *handle __free(kfree) =3D kzalloc(sizeof(*handle), GFP_KERNEL); if (!handle) return -ENOMEM; =20 - ret =3D qcom_scm_shm_bridge_create(pfn_and_ns_perm, ipfn_and_s_perm, - size_and_flags, QCOM_SCM_VMID_HLOS, - handle); + ret =3D qcom_tzmem_shm_bridge_create(area->paddr, area->size, handle); if (ret) return ret; =20 @@ -138,10 +174,7 @@ static void qcom_tzmem_cleanup_area(struct qcom_tzmem_= area *area) { u64 *handle =3D area->priv; =20 - if (!qcom_tzmem_using_shm_bridge) - return; - - qcom_scm_shm_bridge_delete(*handle); + qcom_tzmem_shm_bridge_delete(*handle); kfree(handle); } =20 diff --git a/include/linux/firmware/qcom/qcom_tzmem.h b/include/linux/firmw= are/qcom/qcom_tzmem.h index b83b63a0c049..48ac0e5454c7 100644 --- a/include/linux/firmware/qcom/qcom_tzmem.h +++ b/include/linux/firmware/qcom/qcom_tzmem.h @@ -53,4 +53,19 @@ DEFINE_FREE(qcom_tzmem, void *, if (_T) qcom_tzmem_free(= _T)) =20 phys_addr_t qcom_tzmem_to_phys(void *ptr); =20 +#if IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE) +int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, size_t size, u64 *hand= le); +void qcom_tzmem_shm_bridge_delete(u64 handle); +#else +static inline int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, + size_t size, u64 *handle) +{ + return 0; +} + +static inline void qcom_tzmem_shm_bridge_delete(u64 handle) +{ +} +#endif + #endif /* __QCOM_TZMEM */ --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5300927814B for ; Tue, 29 Apr 2025 06:07:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906842; cv=none; b=M3SJn4ol4cIx8w9Wnsv/8js6wWIF0y2fTX5+BgvJzLydjqGjtFeGeVne/DlrLMmSJ3UbiFS2Y8uiiksOjFVnGYzvOoRfiSF6R9vNsHQnEYkRfuk1VZ03CCI1zj0yGxEOUhuZUlJpvqaprZ+emH2/ZioE/56xTER7ESZW2bFifD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906842; c=relaxed/simple; bh=DwtMeWEWAHYRpne5S9ztJzxCW/jWH3ZEhATO/ACBKuk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cp/Ikxj+VWAbp4Js/FtcfhlLLm1zg99G1esWRkwsMwnwbCgI1/pmMTx1nEOU+qnUQfx08RWsiv5sp5u0YMDzc1Hss5GaThzPvwxHsX/J/GKmAlt5ADIgEMQlzpU+8H9vNvXkqYO+Z7fPvJ3RdFMKk2gdJKOD+OGbkW6SzRe1B0w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=H30xAzSl; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="H30xAzSl" Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNq1iS024625 for ; Tue, 29 Apr 2025 06:07:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= nqcB0spyCGJ38lL3hWHLGKJyxyJ//gLUcqL8/6WuO+k=; b=H30xAzSlJjIBm13x o33Zqn8l3pyvKEH2uyUhbkerok/+Ly4S7rmnnkBqVH8+G/AX0N088+TTMb8XcwVU Qzf6M5SJAiAo0JgntN6Vmkz7Z1bCXZFyIES2bdakKlD7w8e8WhKrto3tHaIHXoEP Rdf1RE5JJGrbiqUsicY5iFd19N87A3UeWOjIFIHJIAoQxa8WA2sYxR1wgSM0EPSP 6NzKnlvaRy85jQMUCn+rTzbEFd5n/DHVfbbqazAm5B+n7lkv+fOZ4rNMqwN404kf 62SYKMwVh8+Fc8knAnR8F1swCHItaXfoTHKRQCtr5AkeKRJh/c2gDZx1s3CYq/qR aAELRQ== Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468muqkm3m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:14 +0000 (GMT) Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-7375e2642b4so3954874b3a.2 for ; Mon, 28 Apr 2025 23:07:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906833; x=1746511633; 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=nqcB0spyCGJ38lL3hWHLGKJyxyJ//gLUcqL8/6WuO+k=; b=bTRWCnOvkhXpxfflSclCwDeOqTl6hzQ0er/5g3JQyImObhYfhKj5+0Kqyv+oltyg55 KbOTCGl8U6uHyFrHEli+zPtfcuJxN9tZgOqiUFYnGaLal0K1SiX7NY7V9e1tUTOYG6kK NVV57ow+qtHzhpBBot4bsyYvivVIjc5q4Qv+k1obxopfCvtjf387k5CduzlJKXqjDsU3 Lm/oJ5mHkcb5WVNOnSJroYyNC7R8MTpUyVWigS6HL4OODoz68H9lyui6O9gZSf3JBHLT vyGJvXymFitFpOSSGM4nRPDHTznLINV36+KM+lrYAPBoMDDACxOcRFZayJkGWKJey71b 9bEg== X-Forwarded-Encrypted: i=1; AJvYcCXuPR+okKMEWo59TvB0l0XcZvmmgG6rmJWSg4DkwdvPld3UiwQYrl34up/OJcfxdkNJFJErit5q+wlQMhg=@vger.kernel.org X-Gm-Message-State: AOJu0YxC0PwaeH3jm/1+yNRS2LHDLSVtDWxcLASEbbNP7p6gheOIRU2p Grl3smOARTv0li+BC/erw7qJxTqAVLLKb/0riL3yHM0KtxbMK8mYYuqlPIjJOgebBNBqb9OfSCt WTS1ZzEv8ehmpIT5xPoG0VEG75vgo6U7u6GDW9g7SBDONIzT6+36P0wa12R7ZsA== X-Gm-Gg: ASbGncv0BKHPUfbbpOej/UHQlSRUXuXCVyOiGV6END9tJvuw1jKfuzhFohniKGI+LVA 7HJS56wcaNBH6CWIKEPvdPr5EwHG3ypXOdBSigYsXinlJM8E8sYmMlhrFK6P+QxNaHF+8FaWBJU iejbY9NgsxpDF9uElP3BhRU04ENnJQ0PjVj8DaEVougqzqE0HH1tRq85NvTllUT+wWAyMSnnLvl 2BzGBcU9GRBwD+f/m57DaQw/0HZUNROSbgyZ0bnA5Z4VB+kyblg7xxF+koFM3+nDSP1sU2gTNCQ kanNLeZPlbFmA1aZBVG6R1M/2vhNti6Tv+KKjRPObRsAtPkRhJSH9hvmWldt70imdhURRYE= X-Received: by 2002:a05:6a00:3a14:b0:736:b101:aed3 with SMTP id d2e1a72fcca58-74028f93bffmr2242678b3a.1.1745906830071; Mon, 28 Apr 2025 23:07:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGub95nwufqBtUDsV/4pza6ELrI1JAdl/aDpUlUd91AHlpM04J62aRiSJrckS7IVvDQzu+qow== X-Received: by 2002:a05:6a00:3a14:b0:736:b101:aed3 with SMTP id d2e1a72fcca58-74028f93bffmr2242562b3a.1.1745906827812; Mon, 28 Apr 2025 23:07:07 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:07 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:29 -0700 Subject: [PATCH v4 08/11] tee: add Qualcomm TEE driver 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-8-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX8es8Jd9FBNQa rl/XORIfdz9nDn/iYcrvkOC7wjjytfPem8fDB9HCJsZz0f7EewBXlyLcKcTfLWyl1a/0RM4hEX8 Psk18rqt0AY6/GTGyYp/RcAD+yU8j4FrY42fW+63BvfcICZXeos9ZqfCEV+V5+d+L0eSWK4HKDS wMVtDrRvJnqj0LdpMK1mACsQ5ZHkygzsP5We6P6MJj4bxOjSws289icT3x1EY+XhwbkzWL61ULS FpPpV3v0Pn70uYukeVfNT/ErhHWBRRaxz30Puhb+iT1Mpo7eQHgg5zuhBGxw8fXxgV3a8KK5SBM PCfa0gAWbnM9eHhEq2ZjCCLfOW7yu5qJnqKNB+RiDS9ikf6dZiSnHpDIxa8ctkGdtKTj5Cp7/uQ F8T/866l3e/dlgyJclPLUqS3+v/elS6TMweLHtBnjd0V7t/49sew+XCqAW/S6zJuLHH0NIUe X-Proofpoint-GUID: 62r4oWFtIa7enzGFk7NL9WP4d84PTgOe X-Proofpoint-ORIG-GUID: 62r4oWFtIa7enzGFk7NL9WP4d84PTgOe X-Authority-Analysis: v=2.4 cv=M/5NKzws c=1 sm=1 tr=0 ts=68106c92 cx=c_pps a=rEQLjTOiSrHUhVqRoksmgQ==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=VwQbUJbxAAAA:8 a=KKAkSRfTAAAA:8 a=izA1eYfQfB1yJISmjYAA:9 a=2_KD2L-nfmI4ngYE:21 a=QEXdDO2ut3YA:10 a=2VI0MkxyNR6bbpdq8BZq:22 a=cvBusfyB2V15izCimMoJ:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 suspectscore=0 mlxscore=0 spamscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 bulkscore=0 phishscore=0 clxscore=1015 lowpriorityscore=0 priorityscore=1501 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 Introduce qcomtee_object, which represents an object in both QTEE and the kernel. QTEE clients can invoke an instance of qcomtee_object to access QTEE services. If this invocation produces a new object in QTEE, an instance of qcomtee_object will be returned. Similarly, QTEE can request services from the kernel by issuing a callback request, which invokes an instance of qcomtee_object in the kernel. Any subsystem that exposes a service to QTEE should allocate and initialize an instance of qcomtee_object with a dispatcher callback that is called when the object is invoked. Implement initial support for exporting qcomtee_object to userspace and QTEE, enabling the invocation of objects hosted in QTEE and userspace through the TEE subsystem. Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- MAINTAINERS | 7 + drivers/tee/Kconfig | 1 + drivers/tee/Makefile | 1 + drivers/tee/qcomtee/Kconfig | 10 + drivers/tee/qcomtee/Makefile | 9 + drivers/tee/qcomtee/async.c | 160 +++++++ drivers/tee/qcomtee/call.c | 764 +++++++++++++++++++++++++++++= ++ drivers/tee/qcomtee/core.c | 806 +++++++++++++++++++++++++++++= ++++ drivers/tee/qcomtee/qcom_scm.c | 38 ++ drivers/tee/qcomtee/qcomtee_msg.h | 239 ++++++++++ drivers/tee/qcomtee/qcomtee_private.h | 222 +++++++++ drivers/tee/qcomtee/release.c | 48 ++ drivers/tee/qcomtee/shm.c | 149 ++++++ drivers/tee/qcomtee/user_obj.c | 713 +++++++++++++++++++++++++++++ include/linux/firmware/qcom/qcom_tee.h | 302 ++++++++++++ include/uapi/linux/tee.h | 1 + 16 files changed, 3470 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 906881b6c5cb..88a9ad34bcf6 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20257,6 +20257,13 @@ F: Documentation/networking/device_drivers/cellula= r/qualcomm/rmnet.rst F: drivers/net/ethernet/qualcomm/rmnet/ F: include/linux/if_rmnet.h =20 +QUALCOMM TEE (QCOMTEE) DRIVER +M: Amirreza Zarrabi +L: linux-arm-msm@vger.kernel.org +S: Maintained +F: drivers/tee/qcomtee/ +F: include/linux/firmware/qcom/qcom_tee.h + QUALCOMM TRUST ZONE MEMORY ALLOCATOR M: Bartosz Golaszewski L: linux-arm-msm@vger.kernel.org diff --git a/drivers/tee/Kconfig b/drivers/tee/Kconfig index 61b507c18780..3a995d7f0d74 100644 --- a/drivers/tee/Kconfig +++ b/drivers/tee/Kconfig @@ -16,5 +16,6 @@ if TEE source "drivers/tee/optee/Kconfig" source "drivers/tee/amdtee/Kconfig" source "drivers/tee/tstee/Kconfig" +source "drivers/tee/qcomtee/Kconfig" =20 endif diff --git a/drivers/tee/Makefile b/drivers/tee/Makefile index 5488cba30bd2..74e987f8f7ea 100644 --- a/drivers/tee/Makefile +++ b/drivers/tee/Makefile @@ -6,3 +6,4 @@ tee-objs +=3D tee_shm_pool.o obj-$(CONFIG_OPTEE) +=3D optee/ obj-$(CONFIG_AMDTEE) +=3D amdtee/ obj-$(CONFIG_ARM_TSTEE) +=3D tstee/ +obj-$(CONFIG_QCOMTEE) +=3D qcomtee/ diff --git a/drivers/tee/qcomtee/Kconfig b/drivers/tee/qcomtee/Kconfig new file mode 100644 index 000000000000..d180a6d07d33 --- /dev/null +++ b/drivers/tee/qcomtee/Kconfig @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-only +# Qualcomm Trusted Execution Environment Configuration +config QCOMTEE + tristate "Qualcomm TEE Support" + select QCOM_SCM + help + This option enables the Qualcomm Trusted Execution Environment (QTEE) + driver. It provides an API to access services offered by QTEE and any + loaded Trusted Applications (TAs), as well as exporting kernel + services to QTEE. diff --git a/drivers/tee/qcomtee/Makefile b/drivers/tee/qcomtee/Makefile new file mode 100644 index 000000000000..1b14b943e5f5 --- /dev/null +++ b/drivers/tee/qcomtee/Makefile @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_QCOMTEE) +=3D qcomtee.o +qcomtee-objs +=3D async.o +qcomtee-objs +=3D call.o +qcomtee-objs +=3D core.o +qcomtee-objs +=3D qcom_scm.o +qcomtee-objs +=3D release.o +qcomtee-objs +=3D shm.o +qcomtee-objs +=3D user_obj.o diff --git a/drivers/tee/qcomtee/async.c b/drivers/tee/qcomtee/async.c new file mode 100644 index 000000000000..4c880c3441a2 --- /dev/null +++ b/drivers/tee/qcomtee/async.c @@ -0,0 +1,160 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#include "qcomtee_private.h" +#include "qcomtee_msg.h" + +#define QCOMTEE_ASYNC_VERSION_1_0 0x00010000U /* Maj: 0x0001, Min: 0x0000.= */ +#define QCOMTEE_ASYNC_VERSION_1_1 0x00010001U /* Maj: 0x0001, Min: 0x0001.= */ +#define QCOMTEE_ASYNC_VERSION_1_2 0x00010002U /* Maj: 0x0001, Min: 0x0002.= */ +#define QCOMTEE_ASYNC_VERSION QCOMTEE_ASYNC_VERSION_1_2 /* Current Version= . */ + +#define QCOMTEE_ASYNC_VERSION_MAJOR(n) upper_16_bits(n) +#define QCOMTEE_ASYNC_VERSION_MINOR(n) lower_16_bits(n) + +/** + * struct qcomtee_async_msg_hdr - Asynchronous message header format. + * @version: current async protocol version of the remote endpoint. + * @op: async operation. + * + * @version specifies the endpoint's (QTEE or driver) supported async prot= ocol. + * For example, if QTEE sets @version to %QCOMTEE_ASYNC_VERSION_1_1, QTEE + * handles operations supported in %QCOMTEE_ASYNC_VERSION_1_1 or + * %QCOMTEE_ASYNC_VERSION_1_0. @op determines the message format. + */ +struct qcomtee_async_msg_hdr { + u32 version; + u32 op; +}; + +/* Size of an empty async message. */ +#define QCOMTEE_ASYNC_MSG_ZERO sizeof(struct qcomtee_async_msg_hdr) + +/** + * struct qcomtee_async_release_msg - Release asynchronous message. + * @hdr: message header as &struct qcomtee_async_msg_hdr. + * @counts: number of objects in @object_ids. + * @object_ids: array of object IDs that should be released. + * + * Available in Maj =3D 0x0001, Min >=3D 0x0000. + */ +struct qcomtee_async_release_msg { + struct qcomtee_async_msg_hdr hdr; + u32 counts; + u32 object_ids[] __counted_by(counts); +}; + +/** + * qcomtee_get_async_buffer() - Get the start of the asynchronous message. + * @oic: context used for the current invocation. + * @async_buffer: return buffer to extract from or fill in async messages. + * + * If @oic is used for direct object invocation, the whole outbound buffer + * is available for the async message. If @oic is used for a callback requ= est, + * the tail of the outbound buffer (after the callback request message) is + * available for the async message. + * + * The start of the async buffer is aligned, see qcomtee_msg_offset_align(= ). + */ +static void qcomtee_get_async_buffer(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_buffer *async_buffer) +{ + struct qcomtee_msg_callback *msg; + unsigned int offset; + int i; + + if (!(oic->flags & QCOMTEE_OIC_FLAG_BUSY)) { + /* The outbound buffer is empty. Using the whole buffer. */ + offset =3D 0; + } else { + msg =3D (struct qcomtee_msg_callback *)oic->out_msg.addr; + + /* Start offset in a message for buffer arguments. */ + offset =3D qcomtee_msg_buffer_args(struct qcomtee_msg_callback, + qcomtee_msg_args(msg)); + + /* Add size of IB arguments. */ + qcomtee_msg_for_each_input_buffer(i, msg) + offset +=3D qcomtee_msg_offset_align(msg->args[i].b.size); + + /* Add size of OB arguments. */ + qcomtee_msg_for_each_output_buffer(i, msg) + offset +=3D qcomtee_msg_offset_align(msg->args[i].b.size); + } + + async_buffer->addr =3D oic->out_msg.addr + offset; + async_buffer->size =3D oic->out_msg.size - offset; +} + +/** + * async_release() - Process QTEE async release requests. + * @oic: context used for the current invocation. + * @msg: async message for object release. + * @size: size of the async buffer available. + * + * Return: Size of the outbound buffer used when processing @msg. + */ +static size_t async_release(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_async_msg_hdr *async_msg, + size_t size) +{ + struct qcomtee_async_release_msg *msg; + struct qcomtee_object *object; + int i; + + msg =3D (struct qcomtee_async_release_msg *)async_msg; + + for (i =3D 0; i < msg->counts; i++) { + object =3D qcomtee_idx_erase(msg->object_ids[i]); + qcomtee_object_put(object); + } + + return struct_size(msg, object_ids, msg->counts); +} + +/** + * qcomtee_fetch_async_reqs() - Fetch and process asynchronous messages. + * @oic: context used for the current invocation. + * + * Calls handlers to process the requested operations in the async message. + * Currently, only supports async release requests. + */ +void qcomtee_fetch_async_reqs(struct qcomtee_object_invoke_ctx *oic) +{ + struct qcomtee_async_msg_hdr *async_msg; + struct qcomtee_buffer async_buffer; + size_t consumed, used =3D 0; + + qcomtee_get_async_buffer(oic, &async_buffer); + + while (async_buffer.size - used > QCOMTEE_ASYNC_MSG_ZERO) { + async_msg =3D (struct qcomtee_async_msg_hdr *)(async_buffer.addr + + used); + + if (QCOMTEE_ASYNC_VERSION_MAJOR(async_msg->version) !=3D + QCOMTEE_ASYNC_VERSION_MAJOR(QCOMTEE_ASYNC_VERSION)) + goto out; + + switch (async_msg->op) { + case QCOMTEE_MSG_OBJECT_OP_RELEASE: + consumed =3D async_release(oic, async_msg, + async_buffer.size - used); + break; + default: + goto out; + } + + /* Supported operation but unable to parse the message. */ + if (!consumed) + goto out; + + /* Next async message. */ + used +=3D qcomtee_msg_offset_align(consumed); + } + +out: + /* Reset the async buffer so async requests do not loop to QTEE. */ + memzero_explicit(async_buffer.addr, async_buffer.size); +} diff --git a/drivers/tee/qcomtee/call.c b/drivers/tee/qcomtee/call.c new file mode 100644 index 000000000000..9e288723ec21 --- /dev/null +++ b/drivers/tee/qcomtee/call.c @@ -0,0 +1,764 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include + +#include "qcomtee_private.h" + +static int find_qtee_object(struct qcomtee_object **object, unsigned long = id, + struct qcomtee_context_data *ctxdata) +{ + int err =3D 0; + + guard(rcu)(); + /* Object release is RCU protected. */ + *object =3D idr_find(&ctxdata->qtee_objects_idr, id); + if (!qcomtee_object_get(*object)) + err =3D -EINVAL; + + return err; +} + +static void del_qtee_object(unsigned long id, + struct qcomtee_context_data *ctxdata) +{ + struct qcomtee_object *object; + + scoped_guard(mutex, &ctxdata->qtee_lock) + object =3D idr_remove(&ctxdata->qtee_objects_idr, id); + + qcomtee_object_put(object); +} + +/** + * qcomtee_context_add_qtee_object() - Add a QTEE object to the context. + * @param: TEE parameter representing @object. + * @object: QTEE object. + * @ctx: context to add the object. + * + * It assumes @object is %QCOMTEE_OBJECT_TYPE_TEE and the caller has alrea= dy + * issued qcomtee_object_get() for @object. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_context_add_qtee_object(struct tee_param *param, + struct qcomtee_object *object, + struct tee_context *ctx) +{ + int ret; + struct qcomtee_context_data *ctxdata =3D ctx->data; + + scoped_guard(mutex, &ctxdata->qtee_lock) + ret =3D idr_alloc(&ctxdata->qtee_objects_idr, object, 0, 0, + GFP_KERNEL); + + if (ret < 0) + return ret; + + param->u.objref.id =3D ret; + /* QTEE Object: QCOMTEE_OBJREF_FLAG_TEE set. */ + param->u.objref.flags =3D QCOMTEE_OBJREF_FLAG_TEE; + + return 0; +} + +/* Retrieve the QTEE object added with qcomtee_context_add_qtee_object(). = */ +int qcomtee_context_find_qtee_object(struct qcomtee_object **object, + struct tee_param *param, + struct tee_context *ctx) +{ + struct qcomtee_context_data *ctxdata =3D ctx->data; + + /* 'qtee_objects_idr' stores QTEE objects only. */ + if (!(param->u.objref.flags & QCOMTEE_OBJREF_FLAG_TEE)) + return -EINVAL; + + return find_qtee_object(object, param->u.objref.id, ctxdata); +} + +/** + * qcomtee_context_del_qtee_object() - Delete a QTEE object from the conte= xt. + * @param: TEE parameter representing @object. + * @ctx: context for deleting the object. + * + * The @param has been initialized by qcomtee_context_add_qtee_object(). + */ +void qcomtee_context_del_qtee_object(struct tee_param *param, + struct tee_context *ctx) +{ + struct qcomtee_context_data *ctxdata =3D ctx->data; + + /* 'qtee_objects_idr' stores QTEE objects only. */ + if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_TEE) + del_qtee_object(param->u.objref.id, ctxdata); +} + +/** + * qcomtee_objref_to_arg() - Convert OBJREF parameter to QTEE argument. + * @arg: QTEE argument. + * @param: TEE parameter. + * @ctx: context in which the conversion should happen. + * + * It assumes @param is an OBJREF. + * It does not set @arg.type; the caller should initialize it to a correct + * &enum qcomtee_arg_type value. It gets the object's refcount in @arg; + * the caller should manage to put it afterward. + * + * For user objects (i.e. (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_USE= R)), + * qcomtee_user_param_to_object() calls qcomtee_object_get() to keep a + * temporary copy for the driver as the release of objects are asynchronous + * and they may go away even before returning from the invocation. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_objref_to_arg(struct qcomtee_arg *arg, struct tee_param *param, + struct tee_context *ctx) +{ + int err; + + /* param is a NULL object: */ + if (param->u.objref.id =3D=3D TEE_OBJREF_NULL) { + arg->o =3D NULL_QCOMTEE_OBJECT; + + return 0; + } + + /* param is a callback object: */ + if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_USER) { + err =3D qcomtee_user_param_to_object(&arg->o, param, ctx); + /* param is a QTEE object: */ + } else if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_TEE) { + err =3D qcomtee_context_find_qtee_object(&arg->o, param, ctx); + } else { + err =3D -EINVAL; + } + + if (err) + arg->o =3D NULL_QCOMTEE_OBJECT; + + return err; +} + +/** + * qcomtee_objref_from_arg() - Convert QTEE argument to OBJREF param. + * @param: TEE parameter. + * @arg: QTEE argument. + * @ctx: context in which the conversion should happen. + * + * It assumes @arg is of %QCOMTEE_ARG_TYPE_IO or %QCOMTEE_ARG_TYPE_OO. + * It does not set @param.attr; the caller should initialize it to a + * correct type. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_objref_from_arg(struct tee_param *param, struct qcomtee_arg *a= rg, + struct tee_context *ctx) +{ + struct qcomtee_object *object; + int err; + + /* param should be of OBJREF. */ + if (param->attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT && + param->attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT && + param->attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INOUT) + return -EINVAL; + + object =3D arg->o; + + switch (typeof_qcomtee_object(object)) { + case QCOMTEE_OBJECT_TYPE_NULL: + param->u.objref.id =3D TEE_OBJREF_NULL; + err =3D 0; + + break; + case QCOMTEE_OBJECT_TYPE_CB: + if (is_qcomtee_user_object(object)) + err =3D qcomtee_user_param_from_object(param, object, + ctx); + else + err =3D -EINVAL; + + break; + case QCOMTEE_OBJECT_TYPE_TEE: + err =3D qcomtee_context_add_qtee_object(param, object, ctx); + + break; + case QCOMTEE_OBJECT_TYPE_ROOT: + default: + return -EINVAL; + } + + return err; +} + +/** + * qcomtee_params_to_args() - Convert TEE parameters to QTEE arguments. + * @u: QTEE arguments. + * @params: TEE parameters. + * @num_params: number of elements in the parameter array. + * @ctx: context in which the conversion should happen. + * + * It assumes @u has at least @num_params + 1 entries and has been initial= ized + * with %QCOMTEE_ARG_TYPE_INV as &struct qcomtee_arg.type. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_params_to_args(struct qcomtee_arg *u, + struct tee_param *params, int num_params, + struct tee_context *ctx) +{ + int i; + + for (i =3D 0; i < num_params; i++) { + switch (params[i].attr) { + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT: + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT: + u[i].flags =3D QCOMTEE_ARG_FLAGS_UADDR; + u[i].b.uaddr =3D params[i].u.ubuf.uaddr; + u[i].b.size =3D params[i].u.ubuf.size; + + if (params[i].attr =3D=3D + TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT) + u[i].type =3D QCOMTEE_ARG_TYPE_IB; + else /* TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT */ + u[i].type =3D QCOMTEE_ARG_TYPE_OB; + + break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT: + u[i].type =3D QCOMTEE_ARG_TYPE_IO; + if (qcomtee_objref_to_arg(&u[i], ¶ms[i], ctx)) + goto out_failed; + + break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT: + u[i].type =3D QCOMTEE_ARG_TYPE_OO; + u[i].o =3D NULL_QCOMTEE_OBJECT; + break; + default: + goto out_failed; + } + } + + return 0; + +out_failed: + /* Undo qcomtee_objref_to_arg(). */ + for (i--; i >=3D 0; i--) { + if (u[i].type !=3D QCOMTEE_ARG_TYPE_IO) + continue; + + qcomtee_user_object_set_notify(u[i].o, false); + if (typeof_qcomtee_object(u[i].o) =3D=3D QCOMTEE_OBJECT_TYPE_CB) + qcomtee_object_put(u[i].o); + + qcomtee_object_put(u[i].o); + } + + return -EINVAL; +} + +/** + * qcomtee_params_from_args() - Convert QTEE arguments to TEE parameters. + * @params: TEE parameters. + * @u: QTEE arguments. + * @num_params: number of elements in the parameter array. + * @ctx: context in which the conversion should happen. + * + * @u should have already been initialized by qcomtee_params_to_args(). + * This also represents the end of a QTEE invocation that started with + * qcomtee_params_to_args() by releasing %QCOMTEE_ARG_TYPE_IO objects. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_params_from_args(struct tee_param *params, + struct qcomtee_arg *u, int num_params, + struct tee_context *ctx) +{ + int i, np; + + qcomtee_arg_for_each(np, u) { + switch (u[np].type) { + case QCOMTEE_ARG_TYPE_OB: + /* TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT */ + params[np].u.ubuf.size =3D u[np].b.size; + + break; + case QCOMTEE_ARG_TYPE_IO: + /* IEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT */ + qcomtee_object_put(u[np].o); + + break; + case QCOMTEE_ARG_TYPE_OO: + /* TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT */ + if (qcomtee_objref_from_arg(¶ms[np], &u[np], ctx)) + goto out_failed; + + break; + case QCOMTEE_ARG_TYPE_IB: + default: + break; + } + } + + return 0; + +out_failed: + /* Undo qcomtee_objref_from_arg(). */ + for (i =3D 0; i < np; i++) { + if (params[i].attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT) + qcomtee_context_del_qtee_object(¶ms[i], ctx); + } + + /* Release any IO and OO objects not processed. */ + for (; u[i].type; i++) { + if (u[i].type =3D=3D QCOMTEE_ARG_TYPE_OO || + u[i].type =3D=3D QCOMTEE_ARG_TYPE_IO) + qcomtee_object_put(u[i].o); + } + + return -EINVAL; +} + +/* TEE Device Ops. */ + +static int qcomtee_params_check(struct tee_param *params, int num_params) +{ + int io =3D 0, oo =3D 0, ib =3D 0, ob =3D 0; + int i; + + /* QTEE accepts 64 arguments. */ + if (num_params > QCOMTEE_ARGS_MAX) + return -EINVAL; + + /* Supported parameter types. */ + for (i =3D 0; i < num_params; i++) { + switch (params[i].attr) { + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT: + ib++; + break; + case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT: + ob++; + break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT: + io++; + break; + case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT: + oo++; + break; + default: + return -EINVAL; + } + } + + /* QTEE accepts 16 arguments of each supported types. */ + if (io > QCOMTEE_ARGS_PER_TYPE || oo > QCOMTEE_ARGS_PER_TYPE || + ib > QCOMTEE_ARGS_PER_TYPE || ob > QCOMTEE_ARGS_PER_TYPE) + return -EINVAL; + + return 0; +} + +/* Check if an operation on ROOT_QCOMTEE_OBJECT from userspace is permitte= d. */ +static int qcomtee_root_object_check(u32 op, struct tee_param *params, + int num_params) +{ + /* Some privileged operations recognized by QTEE. */ + if (op =3D=3D QCOMTEE_ROOT_OP_NOTIFY_DOMAIN_CHANGE || + op =3D=3D QCOMTEE_ROOT_OP_ADCI_ACCEPT || + op =3D=3D QCOMTEE_ROOT_OP_ADCI_SHUTDOWN) + return -EINVAL; + + /* + * QCOMTEE_ROOT_OP_REGISTER_WITH_CREDENTIALS is to register with QTEE + * by passing a credential object as input OBJREF. TEE_OBJREF_NULL as a + * credential object represents a privileged client for QTEE and + * is used by the kernel only. + */ + if (op =3D=3D QCOMTEE_ROOT_OP_REGISTER_WITH_CREDENTIALS && + num_params =3D=3D 2) { + if (params[0].attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT && + params[1].attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT) { + if (params[0].u.objref.id =3D=3D TEE_OBJREF_NULL) + return -EINVAL; + } + } + + return 0; +} + +/** + * qcomtee_object_invoke() - Invoke a QTEE object. + * @ctx: TEE context. + * @arg: ioctl arguments. + * @params: parameters for the object. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_object_invoke(struct tee_context *ctx, + struct tee_ioctl_object_invoke_arg *arg, + struct tee_param *params) +{ + struct qcomtee_object_invoke_ctx *oic __free(kfree) =3D NULL; + struct qcomtee_context_data *ctxdata =3D ctx->data; + struct qcomtee_arg *u __free(kfree) =3D NULL; + struct qcomtee_object *object; + int i, ret, result; + + if (qcomtee_params_check(params, arg->num_params)) + return -EINVAL; + + /* First, handle reserved operations: */ + if (arg->op =3D=3D QCOMTEE_MSG_OBJECT_OP_RELEASE) { + del_qtee_object(arg->id, ctxdata); + + return 0; + } + + /* Otherwise, invoke a QTEE object: */ + oic =3D qcomtee_object_invoke_ctx_alloc(ctx); + if (!oic) + return -ENOMEM; + + /* +1 for ending QCOMTEE_ARG_TYPE_INV. */ + u =3D kcalloc(arg->num_params + 1, sizeof(*u), GFP_KERNEL); + if (!u) + return -ENOMEM; + + /* Get an object to invoke. */ + if (arg->id =3D=3D TEE_OBJREF_NULL) { + /* Use ROOT if TEE_OBJREF_NULL is invoked. */ + if (qcomtee_root_object_check(arg->op, params, arg->num_params)) + return -EINVAL; + + object =3D ROOT_QCOMTEE_OBJECT; + } else if (find_qtee_object(&object, arg->id, ctxdata)) { + return -EINVAL; + } + + ret =3D qcomtee_params_to_args(u, params, arg->num_params, ctx); + if (ret) + goto out; + + ret =3D qcomtee_object_do_invoke(oic, object, arg->op, u, &result); + if (ret) { + qcomtee_arg_for_each_input_object(i, u) { + qcomtee_user_object_set_notify(u[i].o, false); + qcomtee_object_put(u[i].o); + } + + goto out; + } + + if (!result) { + /* Assume service is UNAVAIL if unable to process the result. */ + if (qcomtee_params_from_args(params, u, arg->num_params, ctx)) + result =3D QCOMTEE_MSG_ERROR_UNAVAIL; + } else { + /* + * qcomtee_params_to_args() gets a copy of IO for the driver to + * make sure they do not get released while in the middle of + * invocation. On success (!result), qcomtee_params_from_args() + * puts them. + */ + qcomtee_arg_for_each_input_object(i, u) + qcomtee_object_put(u[i].o); + } + + arg->ret =3D result; +out: + qcomtee_object_put(object); + + return ret; +} + +/** + * qcomtee_supp_recv() - Wait for a request for the supplicant. + * @ctx: TEE context. + * @op: requested operation on the object. + * @num_params: number of elements in the parameter array. + * @params: parameters for @op. + * + * The first parameter is a meta %TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT. + * On input, it provides a user buffer. This buffer is used for parameters= of + * type %TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT in qcomtee_cb_params_from_ar= gs(). + * On output, the object ID and request ID are stored in the meta paramete= r. + * + * @num_params is updated to the number of parameters that actually exist + * in @params on return. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_supp_recv(struct tee_context *ctx, u32 *op, u32 *num_pa= rams, + struct tee_param *params) +{ + struct qcomtee_user_object_request_data data; + void __user *uaddr; + size_t ubuf_size; + int i, ret; + + if (!*num_params) + return -EINVAL; + + /* We expect the first parameter to be an INOUT + meta parameter. */ + if (params->attr !=3D + (TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT | TEE_IOCTL_PARAM_ATTR_META)) + return -EINVAL; + + /* Other parameters are none. */ + for (i =3D 1; i < *num_params; i++) + if (params[i].attr) + return -EINVAL; + + if (!IS_ALIGNED(params->u.value.a, 8)) + return -EINVAL; + + /* User buffer and size from meta parameter. */ + uaddr =3D u64_to_user_ptr(params->u.value.a); + ubuf_size =3D params->u.value.b; + /* Process TEE parameters. +/-1 to ignore the meta parameter. */ + ret =3D qcomtee_user_object_select(ctx, params + 1, *num_params - 1, + uaddr, ubuf_size, &data); + if (ret) + return ret; + + params->u.value.a =3D data.object_id; + params->u.value.b =3D data.id; + params->u.value.c =3D 0; + *op =3D data.op; + *num_params =3D data.np + 1; + + return 0; +} + +/** + * qcomtee_supp_send() - Submit a response for a request. + * @ctx: TEE context. + * @errno: return value for the request. + * @num_params: number of elements in the parameter array. + * @params: returned parameters. + * + * The first parameter is a meta %TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT. + * It specifies the request ID this response belongs to. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_supp_send(struct tee_context *ctx, u32 errno, u32 num_p= arams, + struct tee_param *params) +{ + int req_id; + + if (!num_params) + return -EINVAL; + + /* We expect the first parameter to be an OUTPUT + meta parameter. */ + if (params->attr !=3D (TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT | + TEE_IOCTL_PARAM_ATTR_META)) + return -EINVAL; + + req_id =3D params->u.value.a; + /* Process TEE parameters. +/-1 to ignore the meta parameter. */ + return qcomtee_user_object_submit(ctx, params + 1, num_params - 1, + req_id, errno); +} + +static int qcomtee_open(struct tee_context *ctx) +{ + struct qcomtee_context_data *ctxdata __free(kfree) =3D NULL; + + ctxdata =3D kzalloc(sizeof(*ctxdata), GFP_KERNEL); + if (!ctxdata) + return -ENOMEM; + + /* + * In the QTEE driver, the same context is used to refcount resources + * shared by QTEE. For example, teedev_ctx_get() is called for any + * instance of callback objects (see qcomtee_user_param_to_object()). + * + * Maintain a copy of teedev for QTEE as it serves as a direct user of + * this context. The teedev will be released in the context's release(). + * + * tee_device_unregister() will remain blocked until all contexts + * are released. This includes contexts owned by the user, which are + * closed by teedev_close_context(), as well as those owned by QTEE + * closed by teedev_ctx_put() in object's release(). + */ + if (!tee_device_get(ctx->teedev)) + return -EINVAL; + + idr_init(&ctxdata->qtee_objects_idr); + mutex_init(&ctxdata->qtee_lock); + idr_init(&ctxdata->reqs_idr); + INIT_LIST_HEAD(&ctxdata->reqs_list); + mutex_init(&ctxdata->reqs_lock); + init_completion(&ctxdata->req_c); + + ctx->data =3D no_free_ptr(ctxdata); + + return 0; +} + +/* This is called when the user closes the device. */ +static void qcomtee_close_context(struct tee_context *ctx) +{ + struct qcomtee_context_data *ctxdata =3D ctx->data; + struct qcomtee_object *object; + int id; + + /* Process QUEUED or PROCESSING requests. */ + qcomtee_requests_destroy(ctxdata); + /* Release QTEE objects. */ + idr_for_each_entry(&ctxdata->qtee_objects_idr, object, id) + qcomtee_object_put(object); +} + +/* This is called when the final reference to the context goes away. */ +static void qcomtee_release(struct tee_context *ctx) +{ + struct qcomtee_context_data *ctxdata =3D ctx->data; + + idr_destroy(&ctxdata->qtee_objects_idr); + idr_destroy(&ctxdata->reqs_idr); + kfree(ctxdata); + + /* There is nothing shared in this context with QTEE. */ + tee_device_put(ctx->teedev); +} + +static void qcomtee_get_version(struct tee_device *teedev, + struct tee_ioctl_version_data *vers) +{ + struct tee_ioctl_version_data v =3D { + .impl_id =3D TEE_IMPL_ID_QTEE, + .gen_caps =3D TEE_GEN_CAP_OBJREF, + }; + + *vers =3D v; +} + +static const struct tee_driver_ops qcomtee_ops =3D { + .get_version =3D qcomtee_get_version, + .open =3D qcomtee_open, + .close_context =3D qcomtee_close_context, + .release =3D qcomtee_release, + .object_invoke_func =3D qcomtee_object_invoke, + .supp_recv =3D qcomtee_supp_recv, + .supp_send =3D qcomtee_supp_send, +}; + +static const struct tee_desc qcomtee_desc =3D { + .name =3D "qcomtee", + .ops =3D &qcomtee_ops, + .owner =3D THIS_MODULE, +}; + +static int qcomtee_probe(struct platform_device *pdev) +{ + struct workqueue_struct *async_wq; + struct tee_device *teedev; + struct tee_shm_pool *pool; + struct tee_context *ctx; + struct qcomtee *qcomtee; + int err; + + qcomtee =3D kzalloc(sizeof(*qcomtee), GFP_KERNEL); + if (!qcomtee) + return -ENOMEM; + + pool =3D qcomtee_shm_pool_alloc(); + if (IS_ERR(pool)) { + err =3D PTR_ERR(pool); + + goto err_free_qcomtee; + } + + teedev =3D tee_device_alloc(&qcomtee_desc, NULL, pool, qcomtee); + if (IS_ERR(teedev)) { + err =3D PTR_ERR(teedev); + + goto err_pool_destroy; + } + + qcomtee->teedev =3D teedev; + qcomtee->pool =3D pool; + err =3D tee_device_register(qcomtee->teedev); + if (err) + goto err_unreg_teedev; + + platform_set_drvdata(pdev, qcomtee); + /* Start async wq. */ + async_wq =3D alloc_ordered_workqueue("qcomtee_wq", 0); + if (!async_wq) + goto err_unreg_teedev; + + qcomtee->wq =3D async_wq; + /* Driver context used for async operations of teedev. */ + ctx =3D teedev_open(qcomtee->teedev); + if (IS_ERR(ctx)) { + err =3D PTR_ERR(ctx); + + goto err_dest_wq; + } + + qcomtee->ctx =3D ctx; + + return 0; + +err_dest_wq: + destroy_workqueue(qcomtee->wq); +err_unreg_teedev: + tee_device_unregister(qcomtee->teedev); +err_pool_destroy: + tee_shm_pool_free(pool); +err_free_qcomtee: + kfree(qcomtee); + + return err; +} + +/** + * qcomtee_remove() - Device Removal Routine. + * @pdev: platform device information struct. + * + * It is called by the platform subsystem to alert the driver that it shou= ld + * release the device. + * + * QTEE does not provide an API to inform it about a callback object going= away. + * However, when releasing QTEE objects, any callback object sent to QTEE + * previously would be released by QTEE as part of the object release. + */ +static void qcomtee_remove(struct platform_device *pdev) +{ + struct qcomtee *qcomtee =3D platform_get_drvdata(pdev); + + teedev_close_context(qcomtee->ctx); + /* Wait for RELEASE operations to be processed for QTEE objects. */ + tee_device_unregister(qcomtee->teedev); + destroy_workqueue(qcomtee->wq); + tee_shm_pool_free(qcomtee->pool); + kfree(qcomtee); +} + +static const struct platform_device_id qcomtee_ids[] =3D { { "qcomtee", 0 = }, {} }; +MODULE_DEVICE_TABLE(platform, qcomtee_ids); + +static struct platform_driver qcomtee_platform_driver =3D { + .probe =3D qcomtee_probe, + .remove =3D qcomtee_remove, + .driver =3D { + .name =3D "qcomtee", + }, + .id_table =3D qcomtee_ids, +}; + +module_platform_driver(qcomtee_platform_driver); + +MODULE_AUTHOR("Qualcomm"); +MODULE_DESCRIPTION("QTEE driver"); +MODULE_VERSION("1.0"); +MODULE_LICENSE("GPL"); diff --git a/drivers/tee/qcomtee/core.c b/drivers/tee/qcomtee/core.c new file mode 100644 index 000000000000..ad3b28f32529 --- /dev/null +++ b/drivers/tee/qcomtee/core.c @@ -0,0 +1,806 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include + +#include "qcomtee_msg.h" +#include "qcomtee_private.h" + +/* This is the QTEE root object. */ +struct qcomtee_object qcomtee_object_root =3D { + .name =3D "root", + .object_type =3D QCOMTEE_OBJECT_TYPE_ROOT, + .info.qtee_id =3D QCOMTEE_MSG_OBJECT_ROOT, +}; +EXPORT_SYMBOL_GPL(qcomtee_object_root); + +/* + * ''Local Object Table''. + * Objects from the kernel exported to QTEE are assigned an ID and stored + * in xa_qcom_local_objects (also known as the kernel object table). + * QTEE uses this ID to reference the objects using qcomtee_local_object_g= et(). + */ +static DEFINE_XARRAY_ALLOC(xa_qcom_local_objects); + +/* Next argument of type @type after index @i. */ +int qcomtee_next_arg_type(struct qcomtee_arg *u, int i, + enum qcomtee_arg_type type) +{ + while (u[i].type !=3D QCOMTEE_ARG_TYPE_INV && u[i].type !=3D type) + i++; + return i; +} + +/* + * QTEE expects IDs with the QCOMTEE_MSG_OBJECT_NS_BIT set for objects + * of the QCOMTEE_OBJECT_TYPE_CB type. + */ +#define QCOMTEE_OBJECT_ID_START (QCOMTEE_MSG_OBJECT_NS_BIT + 1) +#define QCOMTEE_OBJECT_ID_END (U32_MAX) + +#define QCOMTEE_OBJECT_SET(p, type, ...) \ + __QCOMTEE_OBJECT_SET(p, type, ##__VA_ARGS__, 0UL) +#define __QCOMTEE_OBJECT_SET(p, type, optr, ...) \ + do { \ + (p)->object_type =3D (type); \ + (p)->info.qtee_id =3D (unsigned long)(optr); \ + } while (0) + +static struct qcomtee_object * +qcomtee_qtee_object_alloc(struct qcomtee_object_invoke_ctx *oic, + unsigned int object_id) +{ + struct qcomtee *qcomtee =3D tee_get_drvdata(oic->ctx->teedev); + struct qcomtee_object *object; + + object =3D kzalloc(sizeof(*object), GFP_KERNEL); + if (!object) + return NULL_QCOMTEE_OBJECT; + + /* If failed, "no-name". */ + object->name =3D kasprintf(GFP_KERNEL, "qcomtee-%u", object_id); + QCOMTEE_OBJECT_SET(object, QCOMTEE_OBJECT_TYPE_TEE, object_id); + kref_init(&object->refcount); + /* A QTEE object requires a context for async operations. */ + object->info.qcomtee_async_ctx =3D qcomtee->ctx; + teedev_ctx_get(object->info.qcomtee_async_ctx); + + return object; +} + +void qcomtee_qtee_object_free(struct qcomtee_object *object) +{ + /* See qcomtee_qtee_object_alloc(). */ + teedev_ctx_put(object->info.qcomtee_async_ctx); + + kfree(object->name); + kfree(object); +} + +static void qcomtee_object_release(struct kref *refcount) +{ + struct qcomtee_object *object; + const char *name; + + object =3D container_of(refcount, struct qcomtee_object, refcount); + + /* + * qcomtee_object_get() is called in a RCU read lock. synchronize_rcu() + * to avoid releasing the object while it is being accessed in + * qcomtee_object_get(). + */ + synchronize_rcu(); + + switch (typeof_qcomtee_object(object)) { + case QCOMTEE_OBJECT_TYPE_TEE: + qcomtee_release_tee_object(object); + + break; + case QCOMTEE_OBJECT_TYPE_CB: + name =3D object->name; + + if (object->ops->release) + object->ops->release(object); + + kfree_const(name); + + break; + case QCOMTEE_OBJECT_TYPE_ROOT: + case QCOMTEE_OBJECT_TYPE_NULL: + default: + break; + } +} + +/** + * qcomtee_object_get() - Increase the object's reference count. + * @object: object to increase the reference count. + * + * Context: The caller should hold RCU read lock. + */ +int qcomtee_object_get(struct qcomtee_object *object) +{ + if (object !=3D NULL_QCOMTEE_OBJECT && object !=3D ROOT_QCOMTEE_OBJECT) + return kref_get_unless_zero(&object->refcount); + + return 0; +} +EXPORT_SYMBOL_GPL(qcomtee_object_get); + +/** + * qcomtee_object_put() - Decrease the object's reference count. + * @object: object to decrease the reference count. + */ +void qcomtee_object_put(struct qcomtee_object *object) +{ + if (object !=3D NULL_QCOMTEE_OBJECT && object !=3D ROOT_QCOMTEE_OBJECT) + kref_put(&object->refcount, qcomtee_object_release); +} +EXPORT_SYMBOL_GPL(qcomtee_object_put); + +static int qcomtee_idx_alloc(u32 *idx, struct qcomtee_object *object) +{ + static u32 xa_last_id =3D QCOMTEE_OBJECT_ID_START; + + /* Every ID allocated here has QCOMTEE_MSG_OBJECT_NS_BIT set. */ + return xa_alloc_cyclic(&xa_qcom_local_objects, idx, object, + XA_LIMIT(QCOMTEE_OBJECT_ID_START, + QCOMTEE_OBJECT_ID_END), + &xa_last_id, GFP_KERNEL); +} + +struct qcomtee_object *qcomtee_idx_erase(u32 idx) +{ + if (idx < QCOMTEE_OBJECT_ID_START || idx > QCOMTEE_OBJECT_ID_END) + return NULL_QCOMTEE_OBJECT; + + return xa_erase(&xa_qcom_local_objects, idx); +} + +/** + * qcomtee_object_id_get() - Get an ID for an object to send to QTEE. + * @object: object to assign an ID. + * @object_id: object ID. + * + * This is called on the path to QTEE to construct the message; see + * qcomtee_prepare_msg() and qcomtee_update_msg(). + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_object_id_get(struct qcomtee_object *object, + unsigned int *object_id) +{ + u32 idx; + + switch (typeof_qcomtee_object(object)) { + case QCOMTEE_OBJECT_TYPE_CB: + if (qcomtee_idx_alloc(&idx, object) < 0) + return -ENOSPC; + + *object_id =3D idx; + + break; + case QCOMTEE_OBJECT_TYPE_ROOT: + case QCOMTEE_OBJECT_TYPE_TEE: + *object_id =3D object->info.qtee_id; + + break; + case QCOMTEE_OBJECT_TYPE_NULL: + *object_id =3D QCOMTEE_MSG_OBJECT_NULL; + + break; + } + + return 0; +} + +/* Release object ID assigned in qcomtee_object_id_get. */ +static void qcomtee_object_id_put(unsigned int object_id) +{ + qcomtee_idx_erase(object_id); +} + +/** + * qcomtee_local_object_get() - Get the object referenced by the ID. + * @object_id: object ID. + * + * It is called on the path from QTEE. + * It is called on behalf of QTEE to obtain an instance of an object + * for a given ID. It increases the object's reference count on success. + * + * Return: On error, returns %NULL_QCOMTEE_OBJECT. + * On success, returns the object. + */ +static struct qcomtee_object *qcomtee_local_object_get(unsigned int object= _id) +{ + struct qcomtee_object *object; + + guard(rcu)(); + object =3D xa_load(&xa_qcom_local_objects, object_id); + /* It already checks for %NULL_QCOMTEE_OBJECT. */ + qcomtee_object_get(object); + + return object; +} + +/** + * qcomtee_object_user_init() - Initialize an object for the user. + * @object: object to initialize. + * @ot: type of object as &enum qcomtee_object_type. + * @ops: instance of callbacks. + * @fmt: name assigned to the object. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_object_user_init(struct qcomtee_object *object, + enum qcomtee_object_type ot, + struct qcomtee_object_operations *ops, + const char *fmt, ...) +{ + va_list ap; + int ret; + + kref_init(&object->refcount); + QCOMTEE_OBJECT_SET(object, QCOMTEE_OBJECT_TYPE_NULL); + + va_start(ap, fmt); + switch (ot) { + case QCOMTEE_OBJECT_TYPE_NULL: + ret =3D 0; + + break; + case QCOMTEE_OBJECT_TYPE_CB: + object->ops =3D ops; + if (!object->ops->dispatch) + return -EINVAL; + + /* If failed, "no-name". */ + object->name =3D kvasprintf_const(GFP_KERNEL, fmt, ap); + QCOMTEE_OBJECT_SET(object, QCOMTEE_OBJECT_TYPE_CB); + + ret =3D 0; + break; + case QCOMTEE_OBJECT_TYPE_ROOT: + case QCOMTEE_OBJECT_TYPE_TEE: + default: + ret =3D -EINVAL; + } + va_end(ap); + + return ret; +} +EXPORT_SYMBOL_GPL(qcomtee_object_user_init); + +/** + * qcomtee_object_type() - Returns the type of object represented by an ID. + * @object_id: object ID for the object. + * + * This is similar to typeof_qcomtee_object(), but instead of receiving an + * object as an argument, it receives an object ID. It is used internally + * on the return path from QTEE. + * + * Return: Returns the type of object referenced by @object_id. + */ +static enum qcomtee_object_type qcomtee_object_type(unsigned int object_id) +{ + if (object_id =3D=3D QCOMTEE_MSG_OBJECT_NULL) + return QCOMTEE_OBJECT_TYPE_NULL; + + if (object_id & QCOMTEE_MSG_OBJECT_NS_BIT) + return QCOMTEE_OBJECT_TYPE_CB; + + return QCOMTEE_OBJECT_TYPE_TEE; +} + +/** + * qcomtee_object_qtee_init() - Initialize an object for QTEE. + * @oic: context to use for the invocation. + * @object: object returned. + * @object_id: object ID received from QTEE. + * + * Return: On failure, returns < 0 and sets @object to %NULL_QCOMTEE_OBJEC= T. + * On success, returns 0 + */ +static int qcomtee_object_qtee_init(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_object **object, + unsigned int object_id) +{ + int ret =3D 0; + + switch (qcomtee_object_type(object_id)) { + case QCOMTEE_OBJECT_TYPE_NULL: + *object =3D NULL_QCOMTEE_OBJECT; + + break; + case QCOMTEE_OBJECT_TYPE_CB: + *object =3D qcomtee_local_object_get(object_id); + if (*object =3D=3D NULL_QCOMTEE_OBJECT) + ret =3D -EINVAL; + + break; + + default: /* QCOMTEE_OBJECT_TYPE_TEE */ + *object =3D qcomtee_qtee_object_alloc(oic, object_id); + if (*object =3D=3D NULL_QCOMTEE_OBJECT) + ret =3D -ENOMEM; + + break; + } + + return ret; +} + +/* + * ''Marshaling API'' + * qcomtee_prepare_msg - Prepare the inbound buffer for sending to QTEE + * qcomtee_update_args - Parse the QTEE response in the inbound buffer + * qcomtee_prepare_args - Parse the QTEE request from the outbound buffer + * qcomtee_update_msg - Update the outbound buffer with the response for= QTEE + */ + +static int qcomtee_prepare_msg(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *u) +{ + struct qcomtee_msg_object_invoke *msg; + unsigned int object_id; + int i, ib, ob, io, oo; + size_t off; + + /* Use the input message buffer in 'oic'. */ + msg =3D oic->in_msg.addr; + + /* Start offset in a message for buffer arguments. */ + off =3D qcomtee_msg_buffer_args(struct qcomtee_msg_object_invoke, + qcomtee_args_len(u)); + + /* Get the ID of the object being invoked. */ + if (qcomtee_object_id_get(object, &object_id)) + return -ENOSPC; + + ib =3D 0; + qcomtee_arg_for_each_input_buffer(i, u) { + void *ptr; + + /* Overflow already checked in qcomtee_msg_buffers_alloc(). */ + msg->args[ib].b.offset =3D off; + msg->args[ib].b.size =3D u[i].b.size; + + ptr =3D qcomtee_msg_offset_to_ptr(msg, off); + /* Userspace client or kernel client!? */ + if (!(u[i].flags & QCOMTEE_ARG_FLAGS_UADDR)) + memcpy(ptr, u[i].b.addr, u[i].b.size); + else if (copy_from_user(ptr, u[i].b.uaddr, u[i].b.size)) + return -EINVAL; + + off +=3D qcomtee_msg_offset_align(u[i].b.size); + ib++; + } + + ob =3D ib; + qcomtee_arg_for_each_output_buffer(i, u) { + /* Overflow already checked in qcomtee_msg_buffers_alloc(). */ + msg->args[ob].b.offset =3D off; + msg->args[ob].b.size =3D u[i].b.size; + + off +=3D qcomtee_msg_offset_align(u[i].b.size); + ob++; + } + + io =3D ob; + qcomtee_arg_for_each_input_object(i, u) { + if (qcomtee_object_id_get(u[i].o, &msg->args[io].o)) { + /* Put whatever we got. */ + qcomtee_object_id_put(object_id); + for (io--; io >=3D ob; io--) + qcomtee_object_id_put(msg->args[io].o); + + return -ENOSPC; + } + + io++; + } + + oo =3D io; + qcomtee_arg_for_each_output_object(i, u) + oo++; + + /* Set object, operation, and argument counts. */ + qcomtee_msg_init(msg, object_id, op, ib, ob, io, oo); + + return 0; +} + +/** + * qcomtee_update_args() - Parse the QTEE response in the inbound buffer. + * @u: array of arguments for the invocation. + * @oic: context to use for the invocation. + * + * @u must be the same as the one used in qcomtee_prepare_msg() when + * initializing the inbound buffer. + * + * On failure, it continues processing the QTEE message. The caller should + * do the necessary cleanup, including calling qcomtee_object_put() + * on the output objects. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_update_args(struct qcomtee_arg *u, + struct qcomtee_object_invoke_ctx *oic) +{ + struct qcomtee_msg_object_invoke *msg; + int i, ib, ob, io, oo; + int ret =3D 0; + + /* Use the input message buffer in 'oic'. */ + msg =3D oic->in_msg.addr; + + ib =3D 0; + qcomtee_arg_for_each_input_buffer(i, u) + ib++; + + ob =3D ib; + qcomtee_arg_for_each_output_buffer(i, u) { + void *ptr; + + /* QTEE can override the size to a smaller value. */ + u[i].b.size =3D msg->args[ob].b.size; + + ptr =3D qcomtee_msg_offset_to_ptr(msg, msg->args[ob].b.offset); + /* Userspace client or kernel client!? */ + if (!(u[i].flags & QCOMTEE_ARG_FLAGS_UADDR)) + memcpy(u[i].b.addr, ptr, u[i].b.size); + else if (copy_to_user(u[i].b.uaddr, ptr, u[i].b.size)) + ret =3D -EINVAL; + + ob++; + } + + io =3D ob; + qcomtee_arg_for_each_input_object(i, u) + io++; + + oo =3D io; + qcomtee_arg_for_each_output_object(i, u) { + if (qcomtee_object_qtee_init(oic, &u[i].o, msg->args[oo].o)) + ret =3D -EINVAL; + + oo++; + } + + return ret; +} + +/** + * qcomtee_prepare_args() - Parse the QTEE request from the outbound buffe= r. + * @oic: context to use for the invocation. + * + * It initializes &qcomtee_object_invoke_ctx->u based on the QTEE request = in + * the outbound buffer. It sets %QCOMTEE_ARG_TYPE_INV at the end of the ar= ray. + * + * On failure, it continues processing the QTEE message. The caller should + * do the necessary cleanup, including calling qcomtee_object_put() + * on the input objects. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_prepare_args(struct qcomtee_object_invoke_ctx *oic) +{ + struct qcomtee_msg_callback *msg; + union qcomtee_msg_arg *arg; + int i, ret =3D 0; + + /* Use the output message buffer in 'oic'. */ + msg =3D oic->out_msg.addr; + + qcomtee_msg_for_each_input_buffer(i, msg) { + arg =3D &msg->args[i]; + oic->u[i].b.addr =3D + qcomtee_msg_offset_to_ptr(msg, arg->b.offset); + oic->u[i].b.size =3D arg->b.size; + oic->u[i].type =3D QCOMTEE_ARG_TYPE_IB; + } + + qcomtee_msg_for_each_output_buffer(i, msg) { + arg =3D &msg->args[i]; + oic->u[i].b.addr =3D + qcomtee_msg_offset_to_ptr(msg, arg->b.offset); + oic->u[i].b.size =3D arg->b.size; + oic->u[i].type =3D QCOMTEE_ARG_TYPE_OB; + } + + qcomtee_msg_for_each_input_object(i, msg) { + if (qcomtee_object_qtee_init(oic, &oic->u[i].o, msg->args[i].o)) + ret =3D -EINVAL; + + oic->u[i].type =3D QCOMTEE_ARG_TYPE_IO; + } + + qcomtee_msg_for_each_output_object(i, msg) + oic->u[i].type =3D QCOMTEE_ARG_TYPE_OO; + + /* End of Arguments. */ + oic->u[i].type =3D QCOMTEE_ARG_TYPE_INV; + + return ret; +} + +static int qcomtee_update_msg(struct qcomtee_object_invoke_ctx *oic) +{ + struct qcomtee_msg_callback *msg; + int i, ib, ob, io, oo; + + /* Use the output message buffer in 'oic'. */ + msg =3D oic->out_msg.addr; + + ib =3D 0; + qcomtee_arg_for_each_input_buffer(i, oic->u) + ib++; + + ob =3D ib; + qcomtee_arg_for_each_output_buffer(i, oic->u) { + /* Only reduce size; never increase it. */ + if (msg->args[ob].b.size < oic->u[i].b.size) + return -EINVAL; + + msg->args[ob].b.size =3D oic->u[i].b.size; + ob++; + } + + io =3D ob; + qcomtee_arg_for_each_input_object(i, oic->u) + io++; + + oo =3D io; + qcomtee_arg_for_each_output_object(i, oic->u) { + if (qcomtee_object_id_get(oic->u[i].o, &msg->args[oo].o)) { + /* Put whatever we got. */ + for (oo--; oo >=3D io; oo--) + qcomtee_object_id_put(msg->args[oo].o); + + return -ENOSPC; + } + + oo++; + } + + return 0; +} + +/* Invoke a callback object. */ +static void qcomtee_cb_object_invoke(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_msg_callback *msg) +{ + int i, errno; + u32 op; + + /* Get the object being invoked. */ + unsigned int object_id =3D msg->cxt; + struct qcomtee_object *object; + + /* QTEE cannot invoke a NULL object or objects it hosts. */ + if (qcomtee_object_type(object_id) =3D=3D QCOMTEE_OBJECT_TYPE_NULL || + qcomtee_object_type(object_id) =3D=3D QCOMTEE_OBJECT_TYPE_TEE) { + errno =3D -EINVAL; + goto out; + } + + object =3D qcomtee_local_object_get(object_id); + if (object =3D=3D NULL_QCOMTEE_OBJECT) { + errno =3D -EINVAL; + goto out; + } + + oic->object =3D object; + + /* Filter bits used by transport. */ + op =3D msg->op & QCOMTEE_MSG_OBJECT_OP_MASK; + + switch (op) { + case QCOMTEE_MSG_OBJECT_OP_RELEASE: + qcomtee_object_id_put(object_id); + qcomtee_object_put(object); + errno =3D 0; + + break; + case QCOMTEE_MSG_OBJECT_OP_RETAIN: + qcomtee_object_get(object); + errno =3D 0; + + break; + default: + errno =3D qcomtee_prepare_args(oic); + if (errno) { + /* Release any object that arrived as input. */ + qcomtee_arg_for_each_input_buffer(i, oic->u) + qcomtee_object_put(oic->u[i].o); + + break; + } + + errno =3D object->ops->dispatch(oic, object, op, oic->u); + if (!errno) { + /* On success, notify at the appropriate time. */ + oic->flags |=3D QCOMTEE_OIC_FLAG_NOTIFY; + } + } + +out: + + oic->errno =3D errno; +} + +/** + * qcomtee_qtee_objects_put() - Put the callback objects in the argument a= rray. + * @u: array of arguments. + * + * When qcomtee_object_do_invoke_internal() is successfully invoked, + * QTEE takes ownership of the callback objects. If the invocation fails, + * qcomtee_object_do_invoke_internal() calls qcomtee_qtee_objects_put() + * to mimic the release of callback objects by QTEE. + */ +static void qcomtee_qtee_objects_put(struct qcomtee_arg *u) +{ + int i; + + qcomtee_arg_for_each_input_object(i, u) { + if (typeof_qcomtee_object(u[i].o) =3D=3D QCOMTEE_OBJECT_TYPE_CB) + qcomtee_object_put(u[i].o); + } +} + +/** + * qcomtee_object_do_invoke_internal() - Submit an invocation for an objec= t. + * @oic: context to use for the current invocation. + * @object: object being invoked. + * @op: requested operation on the object. + * @u: array of arguments for the current invocation. + * @result: result returned from QTEE. + * + * The caller is responsible for keeping track of the refcount for each + * object, including @object. On return, the caller loses ownership of all + * input objects of type %QCOMTEE_OBJECT_TYPE_CB. + * + * Return: On success, returns 0. On error, returns -EAGAIN if invocation + * failed and the user may retry the invocation, -ENODEV on fatal failure. + */ +int qcomtee_object_do_invoke_internal(struct qcomtee_object_invoke_ctx *oi= c, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *u, int *result) +{ + struct qcomtee_msg_callback *cb_msg; + struct qcomtee_object *qto; + int i, ret, errno; + u64 res_type; + + /* Allocate inbound and outbound buffers. */ + if (qcomtee_msg_buffers_alloc(oic, u)) { + qcomtee_qtee_objects_put(u); + + return -EAGAIN; + } + + if (qcomtee_prepare_msg(oic, object, op, u)) { + qcomtee_qtee_objects_put(u); + + ret =3D -EAGAIN; + goto out; + } + + /* Use input message buffer in 'oic'. */ + cb_msg =3D oic->out_msg.addr; + + while (1) { + if (oic->flags & QCOMTEE_OIC_FLAG_BUSY) { + errno =3D oic->errno; + if (!errno) + errno =3D qcomtee_update_msg(oic); + qcomtee_msg_set_result(cb_msg, errno); + } + + /* Invoke the remote object. */ + ret =3D qcomtee_object_invoke_ctx_invoke(oic, result, &res_type); + + if (oic->flags & QCOMTEE_OIC_FLAG_BUSY) { + qto =3D oic->object; + if (qto) { + if (oic->flags & QCOMTEE_OIC_FLAG_NOTIFY) { + /* Don't care about the exact errno. */ + if (qto->ops->notify) + qto->ops->notify(oic, qto, + errno || ret); + } + + /* Get is in qcomtee_cb_object_invoke(). */ + qcomtee_object_put(qto); + } + + oic->object =3D NULL_QCOMTEE_OBJECT; + oic->flags &=3D ~(QCOMTEE_OIC_FLAG_BUSY | + QCOMTEE_OIC_FLAG_NOTIFY); + } + + if (ret) { + if (!(oic->flags & QCOMTEE_OIC_FLAG_SHARED)) { + qcomtee_qtee_objects_put(u); + + ret =3D -EAGAIN; + } else { + /* + * On error, there is no clean way to exit. + * For some reason, we cannot communicate with + * QTEE, so we cannot notify QTEE about the + * failure and do further cleanup. + */ + ret =3D -ENODEV; + } + + goto out; + + } else { + /* + * QTEE obtained ownership of QCOMTEE_OBJECT_TYPE_CB + * input objects in 'u'. On further failure, QTEE is + * responsible for releasing them. + */ + oic->flags |=3D QCOMTEE_OIC_FLAG_SHARED; + } + + /* Is it a callback request? */ + if (res_type !=3D QCOMTEE_RESULT_INBOUND_REQ_NEEDED) { + if (!*result) { + ret =3D qcomtee_update_args(u, oic); + if (ret) { + /* Put output objects. Retry. */ + qcomtee_arg_for_each_output_object(i, u) + qcomtee_object_put(u[i].o); + + ret =3D -EAGAIN; + } + } + + break; + + } else { + oic->flags |=3D QCOMTEE_OIC_FLAG_BUSY; + + qcomtee_fetch_async_reqs(oic); + qcomtee_cb_object_invoke(oic, cb_msg); + } + } + + qcomtee_fetch_async_reqs(oic); +out: + qcomtee_msg_buffers_free(oic); + + return ret; +} + +int qcomtee_object_do_invoke(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *u, int *result) +{ + /* User can not set bits used by transport. */ + if (op & ~QCOMTEE_MSG_OBJECT_OP_MASK) + return -EINVAL; + + /* User can only invoke QTEE hosted objects. */ + if (typeof_qcomtee_object(object) !=3D QCOMTEE_OBJECT_TYPE_TEE && + typeof_qcomtee_object(object) !=3D QCOMTEE_OBJECT_TYPE_ROOT) + return -EINVAL; + + /* User cannot directly issue these operations to QTEE. */ + if (op =3D=3D QCOMTEE_MSG_OBJECT_OP_RELEASE || + op =3D=3D QCOMTEE_MSG_OBJECT_OP_RETAIN) + return -EINVAL; + + return qcomtee_object_do_invoke_internal(oic, object, op, u, result); +} +EXPORT_SYMBOL_GPL(qcomtee_object_do_invoke); diff --git a/drivers/tee/qcomtee/qcom_scm.c b/drivers/tee/qcomtee/qcom_scm.c new file mode 100644 index 000000000000..38a3d080f86d --- /dev/null +++ b/drivers/tee/qcomtee/qcom_scm.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#include + +#include "qcomtee_private.h" + +int qcomtee_object_invoke_ctx_invoke(struct qcomtee_object_invoke_ctx *oic, + int *result, u64 *res_type) +{ + phys_addr_t out_msg_paddr; + phys_addr_t in_msg_paddr; + int ret; + u64 res; + + tee_shm_get_pa(oic->out_shm, 0, &out_msg_paddr); + tee_shm_get_pa(oic->in_shm, 0, &in_msg_paddr); + if (!(oic->flags & QCOMTEE_OIC_FLAG_BUSY)) { + /* Direct QTEE object invocation. */ + ret =3D qcom_scm_qtee_invoke_smc(in_msg_paddr, oic->in_msg.size, + out_msg_paddr, oic->out_msg.size, + &res, res_type); + } else { + /* Submit callback response. */ + ret =3D qcom_scm_qtee_callback_response(out_msg_paddr, + oic->out_msg.size, + &res, res_type); + } + + if (ret) + pr_err("QTEE returned with %d.\n", ret); + else + *result =3D (int)res; + + return ret; +} diff --git a/drivers/tee/qcomtee/qcomtee_msg.h b/drivers/tee/qcomtee/qcomte= e_msg.h new file mode 100644 index 000000000000..33a6b426153c --- /dev/null +++ b/drivers/tee/qcomtee/qcomtee_msg.h @@ -0,0 +1,239 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#ifndef QCOMTEE_MSG_H +#define QCOMTEE_MSG_H + +#include + +/** + * DOC: ''Qualcomm TEE'' (QTEE) Transport Message + * + * There are two buffers shared with QTEE: inbound and outbound buffers. + * The inbound buffer is used for direct object invocation, and the outbou= nd + * buffer is used to make a request from QTEE to the kernel; i.e., a callb= ack + * request. + * + * The unused tail of the outbound buffer is also used for sending and + * receiving asynchronous messages. An asynchronous message is independent= of + * the current object invocation (i.e., contents of the inbound buffer) or + * callback request (i.e., the head of the outbound buffer); see + * qcomtee_get_async_buffer(). It is used by endpoints (QTEE or kernel) as= an + * optimization to reduce the number of context switches between the secur= e and + * non-secure worlds. + * + * For instance, QTEE never sends an explicit callback request to release = an + * object in the kernel. Instead, it sends asynchronous release messages i= n the + * outbound buffer when QTEE returns from the previous direct object invoc= ation, + * or appends asynchronous release messages after the current callback req= uest. + * + * QTEE supports two types of arguments in a message: buffer and object + * arguments. Depending on the direction of data flow, they could be input + * buffer (IO) to QTEE, output buffer (OB) from QTEE, input object (IO) to= QTEE, + * or output object (OO) from QTEE. Object arguments hold object IDs. Buff= er + * arguments hold (offset, size) pairs into the inbound or outbound buffer= s. + * + * QTEE holds an object table for objects it hosts and exposes to the kern= el. + * An object ID is an index to the object table in QTEE. + * + * For the direct object invocation message format in the inbound buffer, = see + * &struct qcomtee_msg_object_invoke. For the callback request message for= mat + * in the outbound buffer, see &struct qcomtee_msg_callback. For the messa= ge + * format for asynchronous messages in the outbound buffer, see + * &struct qcomtee_async_msg_hdr. + */ + +/** + * define QCOMTEE_MSG_OBJECT_NS_BIT - Non-secure bit + * + * Object ID is a globally unique 32-bit number. IDs referencing objects + * in the kernel should have %QCOMTEE_MSG_OBJECT_NS_BIT set. + */ +#define QCOMTEE_MSG_OBJECT_NS_BIT BIT(31) + +/* Static object IDs recognized by QTEE. */ +#define QCOMTEE_MSG_OBJECT_NULL (0U) +#define QCOMTEE_MSG_OBJECT_ROOT (1U) + +/* Definitions from QTEE as part of the transport protocol. */ + +/* qcomtee_msg_arg is an argument as recognized by QTEE. */ +union qcomtee_msg_arg { + struct { + u32 offset; + u32 size; + } b; + u32 o; +}; + +/* BI and BO payloads in QTEE messages should be at 64-bit boundaries. */ +#define qcomtee_msg_offset_align(o) ALIGN((o), sizeof(u64)) + +/* Operations for objects are 32-bit. Transport uses the upper 16 bits. */ +#define QCOMTEE_MSG_OBJECT_OP_MASK GENMASK(15, 0) + +/* Reserved Operation IDs sent to QTEE: */ +/* QCOMTEE_MSG_OBJECT_OP_RELEASE - Reduces the refcount and releases the o= bject. + * QCOMTEE_MSG_OBJECT_OP_RETAIN - Increases the refcount. + * + * These operation IDs are valid for all objects. + */ + +#define QCOMTEE_MSG_OBJECT_OP_RELEASE (QCOMTEE_MSG_OBJECT_OP_MASK - 0) +#define QCOMTEE_MSG_OBJECT_OP_RETAIN (QCOMTEE_MSG_OBJECT_OP_MASK - 1) + +/* Subset of operations supported by QTEE root object. */ + +#define QCOMTEE_ROOT_OP_REGISTER_WITH_CREDENTIALS 5 +#define QCOMTEE_ROOT_OP_NOTIFY_DOMAIN_CHANGE 4 +#define QCOMTEE_ROOT_OP_ADCI_ACCEPT 8 +#define QCOMTEE_ROOT_OP_ADCI_SHUTDOWN 9 + +/* Response types as returned from qcomtee_object_invoke_ctx_invoke(). */ + +/* The message contains a callback request. */ +#define QCOMTEE_RESULT_INBOUND_REQ_NEEDED 3 + +/** + * struct qcomtee_msg_object_invoke - Direct object invocation message. + * @ctx: object ID hosted in QTEE. + * @op: operation for the object. + * @counts: number of different types of arguments in @args. + * @args: array of arguments. + * + * @counts consists of 4 * 4-bit fields. Bits 0 - 3 represent the number of + * input buffers, bits 4 - 7 represent the number of output buffers, + * bits 8 - 11 represent the number of input objects, and bits 12 - 15 + * represent the number of output objects. The remaining bits should be ze= ro. + * + * The maximum number of arguments of each type is defined by + * %QCOMTEE_ARGS_PER_TYPE. + */ +struct qcomtee_msg_object_invoke { + u32 cxt; + u32 op; + u32 counts; + union qcomtee_msg_arg args[]; +}; + +/** + * struct qcomtee_msg_callback - Callback request message. + * @result: result of operation @op on the object referenced by @cxt. + * @cxt: object ID hosted in the kernel. + * @op: operation for the object. + * @counts: number of different types of arguments in @args. + * @args: array of arguments. + * + * For details of @counts, see &qcomtee_msg_object_invoke.counts. + */ +struct qcomtee_msg_callback { + u32 result; + u32 cxt; + u32 op; + u32 counts; + union qcomtee_msg_arg args[]; +}; + +/* Offset in the message for the beginning of the buffer argument's conten= ts. */ +#define qcomtee_msg_buffer_args(t, n) \ + qcomtee_msg_offset_align(struct_size_t(t, args, n)) +/* Pointer to the beginning of a buffer argument's content at an offset. */ +#define qcomtee_msg_offset_to_ptr(m, off) ((void *)&((char *)(m))[(off)]) + +/* Some helpers to manage msg.counts. */ + +#define QCOMTEE_MSG_NUM_IB(x) ((x) & 0xfU) +#define QCOMTEE_MSG_NUM_OB(x) (((x) >> 4) & 0xfU) +#define QCOMTEE_MSG_NUM_IO(x) (((x) >> 8) & 0xfU) +#define QCOMTEE_MSG_NUM_OO(x) (((x) >> 12) & 0xfU) + +#define QCOMTEE_MSG_IDX_IB(x) (0U) +#define QCOMTEE_MSG_IDX_OB(x) (QCOMTEE_MSG_IDX_IB(x) + QCOMTEE_MSG_NUM_IB(= x)) +#define QCOMTEE_MSG_IDX_IO(x) (QCOMTEE_MSG_IDX_OB(x) + QCOMTEE_MSG_NUM_OB(= x)) +#define QCOMTEE_MSG_IDX_OO(x) (QCOMTEE_MSG_IDX_IO(x) + QCOMTEE_MSG_NUM_IO(= x)) + +#define qcomtee_msg_for_each(i, c, type) \ + for (i =3D QCOMTEE_MSG_IDX_##type(c); \ + i < (QCOMTEE_MSG_IDX_##type(c) + QCOMTEE_MSG_NUM_##type(c)); i++) + +#define qcomtee_msg_for_each_input_buffer(i, m) \ + qcomtee_msg_for_each(i, (m)->counts, IB) +#define qcomtee_msg_for_each_output_buffer(i, m) \ + qcomtee_msg_for_each(i, (m)->counts, OB) +#define qcomtee_msg_for_each_input_object(i, m) \ + qcomtee_msg_for_each(i, (m)->counts, IO) +#define qcomtee_msg_for_each_output_object(i, m) \ + qcomtee_msg_for_each(i, (m)->counts, OO) + +/* Sum of arguments in a message. */ +#define qcomtee_msg_args(m) \ + (QCOMTEE_MSG_IDX_OO((m)->counts) + QCOMTEE_MSG_NUM_OO((m)->counts)) + +static inline void qcomtee_msg_init(struct qcomtee_msg_object_invoke *msg, + u32 cxt, u32 op, int in_buffer, + int out_buffer, int in_object, + int out_object) +{ + msg->counts |=3D (in_buffer & 0xfU); + msg->counts |=3D ((out_buffer - in_buffer) & 0xfU) << 4; + msg->counts |=3D ((in_object - out_buffer) & 0xfU) << 8; + msg->counts |=3D ((out_object - in_object) & 0xfU) << 12; + msg->cxt =3D cxt; + msg->op =3D op; +} + +/* Generic error codes. */ +#define QCOMTEE_MSG_OK 0 /* non-specific success code. */ +#define QCOMTEE_MSG_ERROR 1 /* non-specific error. */ +#define QCOMTEE_MSG_ERROR_INVALID 2 /* unsupported/unrecognized request= . */ +#define QCOMTEE_MSG_ERROR_SIZE_IN 3 /* supplied buffer/string too large= . */ +#define QCOMTEE_MSG_ERROR_SIZE_OUT 4 /* supplied output buffer too smal= l. */ +#define QCOMTEE_MSG_ERROR_USERBASE 10 /* start of user-defined error ran= ge. */ + +/* Transport layer error codes. */ +#define QCOMTEE_MSG_ERROR_DEFUNCT -90 /* object no longer exists. */ +#define QCOMTEE_MSG_ERROR_ABORT -91 /* calling thread must exit. */ +#define QCOMTEE_MSG_ERROR_BADOBJ -92 /* invalid object context. */ +#define QCOMTEE_MSG_ERROR_NOSLOTS -93 /* caller's object table full. */ +#define QCOMTEE_MSG_ERROR_MAXARGS -94 /* too many args. */ +#define QCOMTEE_MSG_ERROR_MAXDATA -95 /* buffers too large. */ +#define QCOMTEE_MSG_ERROR_UNAVAIL -96 /* the request could not be process= ed. */ +#define QCOMTEE_MSG_ERROR_KMEM -97 /* kernel out of memory. */ +#define QCOMTEE_MSG_ERROR_REMOTE -98 /* local method sent to remote objec= t. */ +#define QCOMTEE_MSG_ERROR_BUSY -99 /* Object is busy. */ +#define QCOMTEE_MSG_ERROR_TIMEOUT -103 /* Call Back Object invocation time= d out. */ + +static inline void qcomtee_msg_set_result(struct qcomtee_msg_callback *cb_= msg, + int err) +{ + if (!err) { + cb_msg->result =3D QCOMTEE_MSG_OK; + } else if (err < 0) { + /* If err < 0, then it is a transport error. */ + switch (err) { + case -ENOMEM: + cb_msg->result =3D QCOMTEE_MSG_ERROR_KMEM; + break; + case -ENODEV: + cb_msg->result =3D QCOMTEE_MSG_ERROR_DEFUNCT; + break; + case -ENOSPC: + case -EBUSY: + cb_msg->result =3D QCOMTEE_MSG_ERROR_BUSY; + break; + case -EBADF: + case -EINVAL: + cb_msg->result =3D QCOMTEE_MSG_ERROR_UNAVAIL; + break; + default: + cb_msg->result =3D QCOMTEE_MSG_ERROR; + } + } else { + /* If err > 0, then it is user defined error, pass it as is. */ + cb_msg->result =3D err; + } +} + +#endif /* QCOMTEE_MSG_H */ diff --git a/drivers/tee/qcomtee/qcomtee_private.h b/drivers/tee/qcomtee/qc= omtee_private.h new file mode 100644 index 000000000000..a6f2c0591580 --- /dev/null +++ b/drivers/tee/qcomtee/qcomtee_private.h @@ -0,0 +1,222 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#ifndef QCOMTEE_PRIVATE_H +#define QCOMTEE_PRIVATE_H + +#include +#include +#include + +#include "qcomtee_msg.h" + +/* Flags relating to object reference. */ +#define QCOMTEE_OBJREF_FLAG_TEE BIT(0) +#define QCOMTEE_OBJREF_FLAG_USER BIT(1) + +/** + * struct qcomtee - Main service struct. + * @teedev: client device. + * @pool: shared memory pool. + * @ctx: driver private context. + * @wq: workqueue for QTEE async operations. + */ +struct qcomtee { + struct tee_device *teedev; + struct tee_shm_pool *pool; + struct tee_context *ctx; + struct workqueue_struct *wq; +}; + +struct qcomtee_object *qcomtee_idx_erase(u32 idx); +void qcomtee_qtee_object_free(struct qcomtee_object *object); +void qcomtee_fetch_async_reqs(struct qcomtee_object_invoke_ctx *oic); +void qcomtee_release_tee_object(struct qcomtee_object *object); + +struct tee_shm_pool *qcomtee_shm_pool_alloc(void); +void qcomtee_msg_buffers_free(struct qcomtee_object_invoke_ctx *oic); +int qcomtee_msg_buffers_alloc(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_arg *u); + +int qcomtee_object_invoke_ctx_invoke(struct qcomtee_object_invoke_ctx *oic, + int *result, u64 *res_type); + +/** + * qcomtee_object_do_invoke_internal() - Submit an invocation for an objec= t. + * @oic: context to use for the current invocation. + * @object: object being invoked. + * @op: requested operation on the object. + * @u: array of arguments for the current invocation. + * @result: result returned from QTEE. + * + * The caller is responsible for keeping track of the refcount for each + * object, including @object. On return, the caller loses ownership of all + * input objects of type %QCOMTEE_OBJECT_TYPE_CB. + * + * Return: On success, returns 0. On error, returns -EAGAIN if invocation + * failed and the user may retry the invocation, -ENODEV on fatal failure. + */ +int qcomtee_object_do_invoke_internal(struct qcomtee_object_invoke_ctx *oi= c, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *u, int *result); + +/** + * struct qcomtee_context_data - Clients' or supplicants' context. + * @qtee_objects_idr: QTEE objects in this context. + * @qtee_lock: mutex for @qtee_objects_idr. + * @reqs_idr: requests currently being processed in this context. + * @reqs_list: FIFO for requests. + * @reqs_lock: mutex for @reqs_idr, @reqs_list and request states. + * @req_c: completion used when the supplicant is waiting for requests. + * @released: state of this context. + */ +struct qcomtee_context_data { + struct idr qtee_objects_idr; + /* Synchronize access to @qtee_objects_idr. */ + struct mutex qtee_lock; + + struct idr reqs_idr; + struct list_head reqs_list; + /* Synchronize access to @reqs_idr, @reqs_list and updating requests stat= es. */ + struct mutex reqs_lock; + + struct completion req_c; + + bool released; +}; + +/** + * qcomtee_context_add_qtee_object() - Add a QTEE object to the context. + * @param: TEE parameter representing @object. + * @object: QTEE object. + * @ctx: context to add the object. + * + * It assumes @object is %QCOMTEE_OBJECT_TYPE_TEE and the caller has alrea= dy + * issued qcomtee_object_get() for @object. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_context_add_qtee_object(struct tee_param *param, + struct qcomtee_object *object, + struct tee_context *ctx); + +/* Retrieve the QTEE object added with qcomtee_context_add_qtee_object(). = */ +int qcomtee_context_find_qtee_object(struct qcomtee_object **object, + struct tee_param *param, + struct tee_context *ctx); + +/** + * qcomtee_context_del_qtee_object() - Delete a QTEE object from the conte= xt. + * @param: TEE parameter representing @object. + * @ctx: context for deleting the object. + * + * The @param has been initialized by qcomtee_context_add_qtee_object(). + */ +void qcomtee_context_del_qtee_object(struct tee_param *param, + struct tee_context *ctx); + +/** + * qcomtee_objref_to_arg() - Convert OBJREF parameter to QTEE argument. + * @arg: QTEE argument. + * @param: TEE parameter. + * @ctx: context in which the conversion should happen. + * + * It assumes @param is an OBJREF. + * It does not set @arg.type; the caller should initialize it to a correct + * &enum qcomtee_arg_type value. It gets the object's refcount in @arg; + * the caller should manage to put it afterward. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_objref_to_arg(struct qcomtee_arg *arg, struct tee_param *param, + struct tee_context *ctx); + +/** + * qcomtee_objref_from_arg() - Convert QTEE argument to OBJREF param. + * @param: TEE parameter. + * @arg: QTEE argument. + * @ctx: context in which the conversion should happen. + * + * It assumes @arg is of %QCOMTEE_ARG_TYPE_IO or %QCOMTEE_ARG_TYPE_OO. + * It does not set @param.attr; the caller should initialize it to a + * correct type. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_objref_from_arg(struct tee_param *param, struct qcomtee_arg *a= rg, + struct tee_context *ctx); + +/* OBJECTS: */ + +/* (1) User Object API. */ + +/* Is it a user object? */ +int is_qcomtee_user_object(struct qcomtee_object *object); + +/* Set the user object's 'notify on release' flag. */ +void qcomtee_user_object_set_notify(struct qcomtee_object *object, bool no= tify); + +/* This is called when there are no more users for the ctxdata. */ +void qcomtee_requests_destroy(struct qcomtee_context_data *ctxdata); + +/** + * qcomtee_user_param_to_object() - OBJREF parameter to &struct qcomtee_ob= ject. + * @object: object returned. + * @param: TEE parameter. + * @ctx: context in which the conversion should happen. + * + * @param is an OBJREF with %QCOMTEE_OBJREF_FLAG_USER flags. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_user_param_to_object(struct qcomtee_object **object, + struct tee_param *param, + struct tee_context *ctx); + +/* Reverse what qcomtee_user_param_to_object() does. */ +int qcomtee_user_param_from_object(struct tee_param *param, + struct qcomtee_object *object, + struct tee_context *ctx); + +struct qcomtee_user_object_request_data { + int id; /* ID assigned to the request. */ + u64 object_id; /* Object ID being invoked by QTEE. */ + u32 op; /* Requested operation on object. */ + int np; /* Number of parameters in the request.*/ +}; + +/** + * qcomtee_user_object_select() - Select a request for a user object. + * @ctx: context to look for a user object. + * @params: parameters for @op. + * @num_params: number of elements in the parameter array. + * @uaddr: user buffer for output UBUF parameters. + * @size: size of user buffer @uaddr. + * @data: information for the selected request. + * + * @params is filled along with @data for the selected request. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_user_object_select(struct tee_context *ctx, + struct tee_param *params, int num_params, + void __user *uaddr, size_t size, + struct qcomtee_user_object_request_data *data); + +/** + * qcomtee_user_object_submit() - Submit a response for a user object. + * @ctx: context to look for a user object. + * @params: returned parameters. + * @num_params: number of elements in the parameter array. + * @req_id: request ID for the response. + * @errno: result of user object invocation. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_user_object_submit(struct tee_context *ctx, + struct tee_param *params, int num_params, + int req_id, int errno); + +#endif /* QCOMTEE_PRIVATE_H */ diff --git a/drivers/tee/qcomtee/release.c b/drivers/tee/qcomtee/release.c new file mode 100644 index 000000000000..7d149ef3d26f --- /dev/null +++ b/drivers/tee/qcomtee/release.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#include "qcomtee_private.h" + +static void qcomtee_destroy_user_object(struct work_struct *work) +{ + struct qcomtee_object *object; + struct qcomtee *qcomtee; + int ret, result; + + static struct qcomtee_object_invoke_ctx oic; + /* RELEASE does not require any argument. */ + static struct qcomtee_arg args[] =3D { { .type =3D QCOMTEE_ARG_TYPE_INV }= }; + + object =3D container_of(work, struct qcomtee_object, work); + qcomtee =3D tee_get_drvdata(object->info.qcomtee_async_ctx->teedev); + /* Get the TEE context used for asynchronous operations. */ + oic.ctx =3D object->info.qcomtee_async_ctx; + + ret =3D qcomtee_object_do_invoke_internal(&oic, object, + QCOMTEE_MSG_OBJECT_OP_RELEASE, + args, &result); + + /* Is it safe to retry the release? */ + if (ret =3D=3D -EAGAIN) { + queue_work(qcomtee->wq, &object->work); + } else { + if (ret || result) + pr_err("%s: %s release failed, ret =3D %d (%x).\n", + __func__, qcomtee_object_name(object), ret, + result); + + qcomtee_qtee_object_free(object); + } +} + +/* qcomtee_release_tee_object puts object in release work queue. */ +void qcomtee_release_tee_object(struct qcomtee_object *object) +{ + struct qcomtee *qcomtee =3D + tee_get_drvdata(object->info.qcomtee_async_ctx->teedev); + + INIT_WORK(&object->work, qcomtee_destroy_user_object); + queue_work(qcomtee->wq, &object->work); +} diff --git a/drivers/tee/qcomtee/shm.c b/drivers/tee/qcomtee/shm.c new file mode 100644 index 000000000000..998aabe96434 --- /dev/null +++ b/drivers/tee/qcomtee/shm.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include + +#include "qcomtee_msg.h" +#include "qcomtee_private.h" + +/** + * define MAX_OUTBOUND_BUFFER_SIZE - Maximum size of outbound buffers. + * + * The size of outbound buffer depends on QTEE callback requests. + * If an invocation requires any size larger than %MAX_OUTBOUND_BUFFER_SIZ= E, + * the user should probably use some other form of shared memory with QTEE. + */ +#define MAX_OUTBOUND_BUFFER_SIZE SZ_4K + +/* Same as %MAX_OUTBOUND_BUFFER_SIZE but for inbound buffer. */ +#define MAX_INBOUND_BUFFER_SIZE SZ_4M + +/** + * qcomtee_msg_buffers_alloc() - Allocate inbound and outbound buffers. + * @oic: context to use for the current invocation. + * @u: array of arguments for the current invocation. + * + * It calculates the size of inbound and outbound buffers based on the + * arguments in @u. It allocates the buffers from the teedev pool. + * + * Return: On success, returns 0. On error, returns < 0. + */ +int qcomtee_msg_buffers_alloc(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_arg *u) +{ + struct tee_context *ctx =3D oic->ctx; + struct tee_shm *shm; + size_t size; + int i; + + /* Start offset in a message for buffer arguments. */ + size =3D qcomtee_msg_buffer_args(struct qcomtee_msg_object_invoke, + qcomtee_args_len(u)); + if (size > MAX_INBOUND_BUFFER_SIZE) + return -EINVAL; + + /* Add size of IB arguments. */ + qcomtee_arg_for_each_input_buffer(i, u) { + size =3D size_add(size, qcomtee_msg_offset_align(u[i].b.size)); + if (size > MAX_INBOUND_BUFFER_SIZE) + return -EINVAL; + } + + /* Add size of OB arguments. */ + qcomtee_arg_for_each_output_buffer(i, u) { + size =3D size_add(size, qcomtee_msg_offset_align(u[i].b.size)); + if (size > MAX_INBOUND_BUFFER_SIZE) + return -EINVAL; + } + + shm =3D tee_shm_alloc_priv_buf(ctx, size); + if (IS_ERR(shm)) + return PTR_ERR(shm); + + /* Allocate inbound buffer. */ + oic->in_shm =3D shm; + shm =3D tee_shm_alloc_priv_buf(ctx, MAX_OUTBOUND_BUFFER_SIZE); + if (IS_ERR(shm)) { + tee_shm_free(oic->in_shm); + + return PTR_ERR(shm); + } + /* Allocate outbound buffer. */ + oic->out_shm =3D shm; + + oic->in_msg.addr =3D tee_shm_get_va(oic->in_shm, 0); + oic->in_msg.size =3D tee_shm_get_size(oic->in_shm); + oic->out_msg.addr =3D tee_shm_get_va(oic->out_shm, 0); + oic->out_msg.size =3D tee_shm_get_size(oic->out_shm); + /* QTEE assume unused buffers are zeroed. */ + memzero_explicit(oic->in_msg.addr, oic->in_msg.size); + memzero_explicit(oic->out_msg.addr, oic->out_msg.size); + + return 0; +} + +void qcomtee_msg_buffers_free(struct qcomtee_object_invoke_ctx *oic) +{ + tee_shm_free(oic->in_shm); + tee_shm_free(oic->out_shm); +} + +/* Dynamic shared memory pool based on tee_dyn_shm_alloc_helper(). */ + +static int qcomtee_shm_register(struct tee_context *ctx, struct tee_shm *s= hm, + struct page **pages, size_t num_pages, + unsigned long start) +{ + return qcom_tzmem_shm_bridge_create(shm->paddr, shm->size, + &shm->sec_world_id); +} + +static int qcomtee_shm_unregister(struct tee_context *ctx, struct tee_shm = *shm) +{ + qcom_tzmem_shm_bridge_delete(shm->sec_world_id); + + return 0; +} + +static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm, + size_t size, size_t align) +{ + if (!(shm->flags & TEE_SHM_PRIV)) + return -ENOMEM; + + return tee_dyn_shm_alloc_helper(shm, size, align, qcomtee_shm_register); +} + +static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) +{ + tee_dyn_shm_free_helper(shm, qcomtee_shm_unregister); +} + +static void pool_op_destroy_pool(struct tee_shm_pool *pool) +{ + kfree(pool); +} + +static const struct tee_shm_pool_ops pool_ops =3D { + .alloc =3D pool_op_alloc, + .free =3D pool_op_free, + .destroy_pool =3D pool_op_destroy_pool, +}; + +struct tee_shm_pool *qcomtee_shm_pool_alloc(void) +{ + struct tee_shm_pool *pool; + + pool =3D kzalloc(sizeof(*pool), GFP_KERNEL); + if (!pool) + return ERR_PTR(-ENOMEM); + + pool->ops =3D &pool_ops; + + return pool; +} diff --git a/drivers/tee/qcomtee/user_obj.c b/drivers/tee/qcomtee/user_obj.c new file mode 100644 index 000000000000..d53eb9cfa3ae --- /dev/null +++ b/drivers/tee/qcomtee/user_obj.c @@ -0,0 +1,713 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include "qcomtee_private.h" + +/** + * DOC: User Objects aka Supplicants + * + * Any userspace process with access to the TEE device file can behave as a + * supplicant by creating a user object. Any TEE parameter of type OBJREF = with + * %QCOMTEE_OBJREF_FLAG_USER flag set is considered a user object. + * + * A supplicant uses qcomtee_user_object_select() (i.e. TEE_IOC_SUPPL_RECV= ) to + * receive a QTEE user object request and qcomtee_user_object_submit() + * (i.e. TEE_IOC_SUPPL_SEND) to submit a response. QTEE expects to receive= the + * response, including OB and OO in a specific order in the message; param= eters + * submitted with qcomtee_user_object_submit() should maintain this order. + */ + +/** + * struct qcomtee_user_object - User object. + * @object: &struct qcomtee_object representing this user object. + * @ctx: context for which the user object is defined. + * @object_id: object ID in @ctx. + * @nor: notify on release. + * + * Any object managed in userspace is represented by this struct. + * If @nor is set, a notification message is sent back to userspace + * upon release. + */ +struct qcomtee_user_object { + struct qcomtee_object object; + struct tee_context *ctx; + u64 object_id; + bool nor; +}; + +#define to_qcomtee_user_object(o) \ + container_of((o), struct qcomtee_user_object, object) + +static struct qcomtee_object_operations qcomtee_user_object_ops; + +/* Is it a user object? */ +int is_qcomtee_user_object(struct qcomtee_object *object) +{ + return object !=3D NULL_QCOMTEE_OBJECT && + typeof_qcomtee_object(object) =3D=3D QCOMTEE_OBJECT_TYPE_CB && + object->ops =3D=3D &qcomtee_user_object_ops; +} + +/* Set the user object's 'notify on release' flag. */ +void qcomtee_user_object_set_notify(struct qcomtee_object *object, bool no= tify) +{ + if (is_qcomtee_user_object(object)) + to_qcomtee_user_object(object)->nor =3D notify; +} + +/* Supplicant Requests: */ + +/** + * enum qcomtee_req_state - Current state of request. + * @QCOMTEE_REQ_QUEUED: Request is waiting for supplicant. + * @QCOMTEE_REQ_PROCESSING: Request has been picked by the supplicant. + * @QCOMTEE_REQ_PROCESSED: Response has been submitted for the request. + */ +enum qcomtee_req_state { + QCOMTEE_REQ_QUEUED =3D 1, + QCOMTEE_REQ_PROCESSING, + QCOMTEE_REQ_PROCESSED, +}; + +/* User requests sent to supplicants. */ +struct qcomtee_ureq { + enum qcomtee_req_state state; + + /* User Request: */ + int req_id; + u64 object_id; + u32 op; + struct qcomtee_arg *args; + int errno; + + struct list_head node; + struct completion c; /* Completion for whoever wait. */ +}; + +/* + * Placeholder for a PROCESSING request in qcomtee_context.reqs_idr. + * + * If the thread that calls qcomtee_object_invoke() dies and the supplicant + * is processing the request, we replace the entry in qcomtee_context.reqs= _idr + * with empty_ureq. This ensures that (1) the req_id remains busy and is n= ot + * reused, and (2) the supplicant fails to submit the response and performs + * the necessary rollback. + */ +static struct qcomtee_ureq empty_ureq =3D { .state =3D QCOMTEE_REQ_PROCESS= ING }; + +/* Enqueue a user request for a context and assign a request ID. */ +static int ureq_enqueue(struct qcomtee_context_data *ctxdata, + struct qcomtee_ureq *ureq) +{ + int ret; + + guard(mutex)(&ctxdata->reqs_lock); + /* Supplicant is dying. */ + if (ctxdata->released) + return -ENODEV; + + /* Allocate an ID and queue the request. */ + ret =3D idr_alloc(&ctxdata->reqs_idr, ureq, 0, 0, GFP_KERNEL); + if (ret < 0) + return ret; + + ureq->req_id =3D ret; + ureq->state =3D QCOMTEE_REQ_QUEUED; + list_add_tail(&ureq->node, &ctxdata->reqs_list); + + return 0; +} + +/** + * ureq_dequeue() - Dequeue a user request from a context. + * @ctxdata: context data for a context to dequeue the request. + * @req_id: ID of the request to be dequeued. + * + * It dequeues a user request and releases its request ID. + * + * Context: The caller should hold &qcomtee_context_data->reqs_lock. + * Return: Returns the user request associated with this ID; otherwise, NU= LL. + */ +static struct qcomtee_ureq *ureq_dequeue(struct qcomtee_context_data *ctxd= ata, + int req_id) +{ + struct qcomtee_ureq *ureq; + + ureq =3D idr_remove(&ctxdata->reqs_idr, req_id); + if (ureq =3D=3D &empty_ureq || !ureq) + return NULL; + + list_del(&ureq->node); + + return ureq; +} + +/** + * ureq_replace() - Replace a user request. + * @ctxdata: context data for a context to replace the request. + * @ureq: request to be replaced. + * @ureq_new: request to replace it with. + * + * Context: The caller should hold &qcomtee_context_data->reqs_lock. + */ +static void ureq_replace(struct qcomtee_context_data *ctxdata, + struct qcomtee_ureq *ureq, + struct qcomtee_ureq *ureq_new) +{ + idr_replace(&ctxdata->reqs_idr, ureq_new, ureq->req_id); + /* ureq does not have a valid ID anymore; remove it from the queue. */ + list_del(&ureq->node); +} + +/** + * ureq_select() - Select the next request in a context. + * @ctxdata: context data for a context to pop a request. + * @ubuf_size: size of the available buffer for UBUF parameters. + * @num_params: number of entries for the TEE parameter array. + * + * It checks if @num_params is large enough to fit the next request argume= nts. + * It checks if @ubuf_size is large enough to fit IB buffer arguments. + * + * Context: The caller should hold &qcomtee_context_data->reqs_lock. + * Return: On success, returns a request; + * on failure, returns NULL and ERR_PTR. + */ +static struct qcomtee_ureq *ureq_select(struct qcomtee_context_data *ctxda= ta, + size_t ubuf_size, int num_params) +{ + struct qcomtee_ureq *req, *ureq =3D NULL; + struct qcomtee_arg *u; + int i; + + /* Find the a queued request. */ + list_for_each_entry(req, &ctxdata->reqs_list, node) { + if (req->state =3D=3D QCOMTEE_REQ_QUEUED) { + ureq =3D req; + break; + } + } + + if (!ureq) + return NULL; + + u =3D ureq->args; + /* (1) Is there enough TEE parameters? */ + if (num_params < qcomtee_args_len(u)) + return ERR_PTR(-EINVAL); + /* (2) Is there enough space to pass input buffers? */ + qcomtee_arg_for_each_input_buffer(i, u) { + ubuf_size =3D size_sub(ubuf_size, u[i].b.size); + if (ubuf_size =3D=3D SIZE_MAX) + return ERR_PTR(-EINVAL); + + ubuf_size =3D round_down(ubuf_size, 8); + } + + return ureq; +} + +/* This is called when there are no more users for the ctxdata. */ +void qcomtee_requests_destroy(struct qcomtee_context_data *ctxdata) +{ + struct qcomtee_ureq *req, *ureq; + + guard(mutex)(&ctxdata->reqs_lock); + /* So ureq_enqueue() refuses new requests from QTEE. */ + ctxdata->released =3D true; + + list_for_each_entry_safe(ureq, req, &ctxdata->reqs_list, node) { + ureq_dequeue(ctxdata, ureq->req_id); + + /* + * Terminate requests. + * Depending on who enqueues the ureq, there may be someone + * waiting for the response or not: + */ + if (ureq->op !=3D QCOMTEE_MSG_OBJECT_OP_RELEASE) { + /* (1) enqueued by qcomtee_user_object_dispatch(). */ + ureq->state =3D QCOMTEE_REQ_PROCESSED; + ureq->errno =3D -ENODEV; + + complete(&ureq->c); + } else { + /* (2) enqueued by qcomtee_user_object_release(). */ + kfree(ureq); + } + } +} + +/* User Object API. */ + +/* User object dispatcher. */ +static int qcomtee_user_object_dispatch(struct qcomtee_object_invoke_ctx *= oic, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *args) +{ + struct qcomtee_user_object *uo =3D to_qcomtee_user_object(object); + struct qcomtee_context_data *ctxdata =3D uo->ctx->data; + struct qcomtee_ureq *ureq __free(kfree) =3D NULL; + int errno; + + ureq =3D kzalloc(sizeof(*ureq), GFP_KERNEL); + if (!ureq) + return -ENOMEM; + + init_completion(&ureq->c); + ureq->object_id =3D uo->object_id; + ureq->op =3D op; + ureq->args =3D args; + + /* Queue the request. */ + if (ureq_enqueue(ctxdata, ureq)) + return -ENODEV; + + /* Wakeup supplicant to process it. */ + complete(&ctxdata->req_c); + + /* + * Wait for the supplicant to process the request. + * + * The supplicant is expected to process the request in a timely manner. + * We wait as KILLABLE in case the supplicant and invoke thread are + * both running from the same user process; otherwise, the process + * will be stuck on a fatal signal. + */ + if (!wait_for_completion_state(&ureq->c, + TASK_KILLABLE | TASK_FREEZABLE)) { + errno =3D ureq->errno; + /* On success, notify() frees the request. */ + if (!errno) + oic->data =3D no_free_ptr(ureq); + } else { + enum qcomtee_req_state prev_state; + + errno =3D -ENODEV; + + scoped_guard(mutex, &ctxdata->reqs_lock) { + prev_state =3D ureq->state; + + /* Replace with empty_ureq to keep req_id reserved. */ + if (prev_state =3D=3D QCOMTEE_REQ_PROCESSING) + ureq_replace(ctxdata, ureq, &empty_ureq); + + /* Remove as supplicant has never seen this request. */ + else if (prev_state =3D=3D QCOMTEE_REQ_QUEUED) + ureq_dequeue(ctxdata, ureq->req_id); + } + + /* Supplicant did some work, we should not discard it. */ + if (prev_state =3D=3D QCOMTEE_REQ_PROCESSED) { + errno =3D ureq->errno; + /* On success, notify() frees the request. */ + if (!errno) + oic->data =3D no_free_ptr(ureq); + } + } + + return errno; +} + +/* This is called after submitting the dispatcher response. */ +static void qcomtee_user_object_notify(struct qcomtee_object_invoke_ctx *o= ic, + struct qcomtee_object *unused_object, + int err) +{ + struct qcomtee_ureq *ureq =3D oic->data; + struct qcomtee_arg *u =3D ureq->args; + int i; + + qcomtee_arg_for_each_output_object(i, u) { + /* + * If err, there was a transport issue, and QTEE did not + * receive the response for the dispatcher. Release the callback + * object created for QTEE, in addition to the copies of + * objects kept for the drivers. + */ + if (err && + (typeof_qcomtee_object(u[i].o) =3D=3D QCOMTEE_OBJECT_TYPE_CB)) + qcomtee_object_put(u[i].o); + qcomtee_object_put(u[i].o); + } + + kfree(ureq); +} + +static void qcomtee_user_object_release(struct qcomtee_object *object) +{ + struct qcomtee_user_object *uo =3D to_qcomtee_user_object(object); + struct qcomtee_context_data *ctxdata =3D uo->ctx->data; + struct qcomtee_ureq *ureq; + + /* RELEASE does not require any argument. */ + static struct qcomtee_arg args[] =3D { { .type =3D QCOMTEE_ARG_TYPE_INV }= }; + + if (!uo->nor) + goto out_no_notify; + + ureq =3D kzalloc(sizeof(*ureq), GFP_KERNEL); + if (!ureq) + goto out_no_notify; + + /* QUEUE a release request: */ + ureq->object_id =3D uo->object_id; + ureq->op =3D QCOMTEE_MSG_OBJECT_OP_RELEASE; + ureq->args =3D args; + if (ureq_enqueue(ctxdata, ureq)) { + kfree(ureq); + /* Ignore the notification if it cannot be queued. */ + goto out_no_notify; + } + + complete(&ctxdata->req_c); + +out_no_notify: + teedev_ctx_put(uo->ctx); + kfree(uo); +} + +static struct qcomtee_object_operations qcomtee_user_object_ops =3D { + .release =3D qcomtee_user_object_release, + .notify =3D qcomtee_user_object_notify, + .dispatch =3D qcomtee_user_object_dispatch, +}; + +/** + * qcomtee_user_param_to_object() - OBJREF parameter to &struct qcomtee_ob= ject. + * @object: object returned. + * @param: TEE parameter. + * @ctx: context in which the conversion should happen. + * + * @param is an OBJREF with %QCOMTEE_OBJREF_FLAG_USER flags. + * It calls qcomtee_object_get() to keep a copy of @object for the driver + * as well as a copy for QTEE. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_user_param_to_object(struct qcomtee_object **object, + struct tee_param *param, + struct tee_context *ctx) +{ + struct qcomtee_user_object *user_object __free(kfree) =3D NULL; + int err; + + user_object =3D kzalloc(sizeof(*user_object), GFP_KERNEL); + if (!user_object) + return -ENOMEM; + + user_object->ctx =3D ctx; + user_object->object_id =3D param->u.objref.id; + /* By default, always notify userspace upon release. */ + user_object->nor =3D true; + err =3D qcomtee_object_user_init(&user_object->object, + QCOMTEE_OBJECT_TYPE_CB, + &qcomtee_user_object_ops, "uo-%lu", + param->u.objref.id); + if (err) + return err; + + teedev_ctx_get(ctx); + + *object =3D &no_free_ptr(user_object)->object; + qcomtee_object_get(*object); + + return 0; +} + +/* Reverse what qcomtee_user_param_to_object() does. */ +int qcomtee_user_param_from_object(struct tee_param *param, + struct qcomtee_object *object, + struct tee_context *ctx) +{ + struct qcomtee_user_object *uo; + + uo =3D to_qcomtee_user_object(object); + /* Ensure the object is in the same context as the caller. */ + if (uo->ctx !=3D ctx) + return -EINVAL; + + param->u.objref.id =3D uo->object_id; + param->u.objref.flags =3D QCOMTEE_OBJREF_FLAG_USER; + + /* User objects are valid in userspace; do not keep a copy. */ + qcomtee_object_put(object); + + return 0; +} + +/** + * qcomtee_cb_params_from_args() - Convert QTEE arguments to TEE parameter= s. + * @params: TEE parameters. + * @u: QTEE arguments. + * @num_params: number of elements in the parameter array. + * @ubuf_addr: user buffer for arguments of type %QCOMTEE_ARG_TYPE_IB. + * @ubuf_size: size of the user buffer. + * @ctx: context in which the conversion should happen. + * + * It expects @params to have enough entries for @u. Entries in @params ar= e of + * %TEE_IOCTL_PARAM_ATTR_TYPE_NONE. + * + * Return: On success, returns the number of input parameters; + * on failure, returns < 0. + */ +static int qcomtee_cb_params_from_args(struct tee_param *params, + struct qcomtee_arg *u, int num_params, + void __user *ubuf_addr, size_t ubuf_size, + struct tee_context *ctx) +{ + int i, np; + void __user *uaddr; + + qcomtee_arg_for_each(i, u) { + switch (u[i].type) { + case QCOMTEE_ARG_TYPE_IB: + params[i].attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT; + + /* Underflow already checked in ureq_select(). */ + ubuf_size =3D round_down(ubuf_size - u[i].b.size, 8); + uaddr =3D (void *__user)(ubuf_addr + ubuf_size); + + params[i].u.ubuf.uaddr =3D uaddr; + params[i].u.ubuf.size =3D u[i].b.size; + if (copy_to_user(params[i].u.ubuf.uaddr, u[i].b.addr, + u[i].b.size)) + goto out_failed; + + break; + case QCOMTEE_ARG_TYPE_OB: + params[i].attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT; + /* Let the user knows the maximum size we expect. */ + params[i].u.ubuf.size =3D u[i].b.size; + + break; + case QCOMTEE_ARG_TYPE_IO: + params[i].attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT; + if (qcomtee_objref_from_arg(¶ms[i], &u[i], ctx)) + goto out_failed; + + break; + case QCOMTEE_ARG_TYPE_OO: + params[i].attr =3D + TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT; + + break; + default: /* Never get here! */ + goto out_failed; + } + } + + return i; + +out_failed: + /* Undo qcomtee_objref_from_arg(). */ + for (np =3D i; np >=3D 0; np--) { + if (params[np].attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT) + qcomtee_context_del_qtee_object(¶ms[np], ctx); + } + + /* Release any IO objects not processed. */ + for (; u[i].type; i++) { + if (u[i].type =3D=3D QCOMTEE_ARG_TYPE_IO) + qcomtee_object_put(u[i].o); + } + + return -EINVAL; +} + +/** + * qcomtee_cb_params_to_args() - Convert TEE parameters to QTEE arguments. + * @u: QTEE arguments. + * @params: TEE parameters. + * @num_params: number of elements in the parameter array. + * @ctx: context in which the conversion should happen. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +static int qcomtee_cb_params_to_args(struct qcomtee_arg *u, + struct tee_param *params, int num_params, + struct tee_context *ctx) +{ + int i; + + qcomtee_arg_for_each(i, u) { + switch (u[i].type) { + case QCOMTEE_ARG_TYPE_IB: + if (params[i].attr !=3D + TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT) + goto out_failed; + + break; + case QCOMTEE_ARG_TYPE_OB: + if (params[i].attr !=3D + TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT) + goto out_failed; + + /* Client can not send more data than requested. */ + if (params[i].u.ubuf.size > u[i].b.size) + goto out_failed; + + if (copy_from_user(u[i].b.addr, params[i].u.ubuf.uaddr, + params[i].u.ubuf.size)) + goto out_failed; + + u[i].b.size =3D params[i].u.ubuf.size; + + break; + case QCOMTEE_ARG_TYPE_IO: + if (params[i].attr !=3D + TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT) + goto out_failed; + + break; + case QCOMTEE_ARG_TYPE_OO: + if (params[i].attr !=3D + TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT) + goto out_failed; + + if (qcomtee_objref_to_arg(&u[i], ¶ms[i], ctx)) + goto out_failed; + + break; + default: /* Never get here! */ + goto out_failed; + } + } + + return 0; + +out_failed: + /* Undo qcomtee_objref_to_arg(). */ + for (i--; i >=3D 0; i--) { + if (u[i].type !=3D QCOMTEE_ARG_TYPE_OO) + continue; + + qcomtee_user_object_set_notify(u[i].o, false); + if (typeof_qcomtee_object(u[i].o) =3D=3D QCOMTEE_OBJECT_TYPE_CB) + qcomtee_object_put(u[i].o); + + qcomtee_object_put(u[i].o); + } + + return -EINVAL; +} + +/** + * qcomtee_user_object_select() - Select a request for a user object. + * @ctx: context to look for a user object. + * @params: parameters for @op. + * @num_params: number of elements in the parameter array. + * @uaddr: user buffer for output UBUF parameters. + * @size: size of user buffer @uaddr. + * @data: information for the selected request. + * + * @params is filled along with @data for the selected request. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_user_object_select(struct tee_context *ctx, + struct tee_param *params, int num_params, + void __user *uaddr, size_t size, + struct qcomtee_user_object_request_data *data) +{ + struct qcomtee_context_data *ctxdata =3D ctx->data; + struct qcomtee_ureq *ureq; + int ret; + + while (1) { + scoped_guard(mutex, &ctxdata->reqs_lock) { + ureq =3D ureq_select(ctxdata, size, num_params); + if (!ureq) + goto wait_for_request; + + if (IS_ERR(ureq)) + return PTR_ERR(ureq); + + /* Processing the request 'QUEUED -> PROCESSING'. */ + ureq->state =3D QCOMTEE_REQ_PROCESSING; + + /* ''Prepare user request:'' */ + data->id =3D ureq->req_id; + data->object_id =3D ureq->object_id; + data->op =3D ureq->op; + ret =3D qcomtee_cb_params_from_args(params, ureq->args, + num_params, uaddr, + size, ctx); + if (ret >=3D 0) + goto done_request; + + /* Something is wrong with the request. */ + + ureq_dequeue(ctxdata, data->id); + /* Send error to QTEE. */ + ureq->state =3D QCOMTEE_REQ_PROCESSED; + ureq->errno =3D ret; + + complete(&ureq->c); + } + + continue; +wait_for_request: + /* Wait for a new QUEUED request. */ + if (wait_for_completion_interruptible(&ctxdata->req_c)) + return -ERESTARTSYS; + } + +done_request: + /* No one is waiting for the response. */ + if (data->op =3D=3D QCOMTEE_MSG_OBJECT_OP_RELEASE) { + scoped_guard(mutex, &ctxdata->reqs_lock) + ureq_dequeue(ctxdata, data->id); + kfree(ureq); + } + + data->np =3D ret; + + return 0; +} + +/** + * qcomtee_user_object_submit() - Submit a response for a user object. + * @ctx: context to look for a user object. + * @params: returned parameters. + * @num_params: number of elements in the parameter array. + * @req_id: request ID for the response. + * @errno: result of user object invocation. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_user_object_submit(struct tee_context *ctx, + struct tee_param *params, int num_params, + int req_id, int errno) +{ + struct qcomtee_context_data *ctxdata =3D ctx->data; + struct qcomtee_ureq *ureq; + + guard(mutex)(&ctxdata->reqs_lock); + + ureq =3D ureq_dequeue(ctxdata, req_id); + if (!ureq) + return -EINVAL; + + ureq->state =3D QCOMTEE_REQ_PROCESSED; + + if (!errno) + ureq->errno =3D qcomtee_cb_params_to_args(ureq->args, params, + num_params, ctx); + else + ureq->errno =3D errno; + + /* Return errno if qcomtee_cb_params_to_args() failed; otherwise 0. */ + if (!errno && ureq->errno) + errno =3D ureq->errno; + else + errno =3D 0; + + /* Send result to QTEE. */ + complete(&ureq->c); + + return errno; +} diff --git a/include/linux/firmware/qcom/qcom_tee.h b/include/linux/firmwar= e/qcom/qcom_tee.h new file mode 100644 index 000000000000..3f7c4ac2bdd6 --- /dev/null +++ b/include/linux/firmware/qcom/qcom_tee.h @@ -0,0 +1,302 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#ifndef __QCOM_TEE_H +#define __QCOM_TEE_H + +#include +#include +#include +#include + +struct qcomtee_object; + +/** + * DOC: Overview + * + * qcomtee_object provides object refcounting, ID allocation for objects h= osted + * in the kernel, and necessary message marshaling for Qualcomm TEE (QTEE). + * + * To invoke an object in QTEE, the user calls qcomtee_object_do_invoke() + * while passing an instance of &struct qcomtee_object and the requested + * operation + arguments. + * + * After boot, QTEE provides a static object %ROOT_QCOMTEE_OBJECT (type of + * %QCOMTEE_OBJECT_TYPE_ROOT). The root object is invoked to pass the user= 's + * credentials and obtain other instances of &struct qcomtee_object (type = of + * %QCOMTEE_OBJECT_TYPE_TEE) that represent services and TAs in QTEE; + * see &enum qcomtee_object_type. + * + * The objects received from QTEE are refcounted. So the owner of these ob= jects + * can issue qcomtee_object_get() to increase the refcount and pass objects + * to other clients, or issue qcomtee_object_put() to decrease the refcount + * and release the resources in QTEE. + * + * The kernel can host services accessible to QTEE. A driver should embed + * an instance of &struct qcomtee_object in the struct it wants to export = to + * QTEE (this is called a callback object). It issues qcomtee_object_user_= init() + * to set the dispatch() operation for the callback object and set its type + * to %QCOMTEE_OBJECT_TYPE_CB. + * + * core.c holds an object table for callback objects. An object ID is assi= gned + * to each callback object, which is an index to the object table. QTEE us= es + * these IDs to reference or invoke callback objects. + * + * If QTEE invokes a callback object in the kernel, the dispatch() operati= on is + * called in the context of the thread that originally called + * qcomtee_object_do_invoke(). + */ + +/** + * enum qcomtee_object_type - Object types. + * @QCOMTEE_OBJECT_TYPE_TEE: object hosted on QTEE. + * @QCOMTEE_OBJECT_TYPE_CB: object hosted on kernel. + * @QCOMTEE_OBJECT_TYPE_ROOT: 'primordial' object. + * @QCOMTEE_OBJECT_TYPE_NULL: NULL object. + * + * The primordial object is used for bootstrapping the IPC connection betw= een + * the kernel and QTEE. It is invoked by the kernel when it wants to get a + * 'client env'. + */ +enum qcomtee_object_type { + QCOMTEE_OBJECT_TYPE_TEE, + QCOMTEE_OBJECT_TYPE_CB, + QCOMTEE_OBJECT_TYPE_ROOT, + QCOMTEE_OBJECT_TYPE_NULL, +}; + +/** + * enum qcomtee_arg_type - Type of QTEE argument. + * @QCOMTEE_ARG_TYPE_INV: invalid type. + * @QCOMTEE_ARG_TYPE_OB: output buffer (OB). + * @QCOMTEE_ARG_TYPE_OO: output object (OO). + * @QCOMTEE_ARG_TYPE_IB: input buffer (IB). + * @QCOMTEE_ARG_TYPE_IO: input object (IO). + * + * Use the invalid type to specify the end of the argument array. + */ +enum qcomtee_arg_type { + QCOMTEE_ARG_TYPE_INV =3D 0, + QCOMTEE_ARG_TYPE_OB, + QCOMTEE_ARG_TYPE_OO, + QCOMTEE_ARG_TYPE_IB, + QCOMTEE_ARG_TYPE_IO, + QCOMTEE_ARG_TYPE_NR, +}; + +/** + * define QCOMTEE_ARGS_PER_TYPE - Maximum arguments of a specific type. + * + * The QTEE transport protocol limits the maximum number of arguments of + * a specific type (i.e., IB, OB, IO, and OO). + */ +#define QCOMTEE_ARGS_PER_TYPE 16 + +/* Maximum arguments that can fit in a QTEE message, ignoring the type. */ +#define QCOMTEE_ARGS_MAX (QCOMTEE_ARGS_PER_TYPE * (QCOMTEE_ARG_TYPE_NR - 1= )) + +struct qcomtee_buffer { + union { + void *addr; + void __user *uaddr; + }; + size_t size; +}; + +/** + * struct qcomtee_arg - Argument for QTEE object invocation. + * @type: type of argument as &enum qcomtee_arg_type. + * @flags: extra flags. + * @b: address and size if the type of argument is a buffer. + * @o: object instance if the type of argument is an object. + * + * &qcomtee_arg.flags only accepts %QCOMTEE_ARG_FLAGS_UADDR for now, which + * states that &qcomtee_arg.b contains a userspace address in uaddr. + */ +struct qcomtee_arg { + enum qcomtee_arg_type type; +/* 'b.uaddr' holds a __user address. */ +#define QCOMTEE_ARG_FLAGS_UADDR BIT(0) + unsigned int flags; + union { + struct qcomtee_buffer b; + struct qcomtee_object *o; + }; +}; + +static inline int qcomtee_args_len(struct qcomtee_arg *args) +{ + int i =3D 0; + + while (args[i].type !=3D QCOMTEE_ARG_TYPE_INV) + i++; + return i; +} + +/* Context is busy (callback is in progress). */ +#define QCOMTEE_OIC_FLAG_BUSY BIT(1) +/* Context needs to notify the current object. */ +#define QCOMTEE_OIC_FLAG_NOTIFY BIT(2) +/* Context has shared state with QTEE. */ +#define QCOMTEE_OIC_FLAG_SHARED BIT(3) + +struct qcomtee_object_invoke_ctx { + /* TEE context for this invocation. */ + struct tee_context *ctx; + unsigned long flags; + int errno; + + /* Current object invoked in this callback context. */ + struct qcomtee_object *object; + + /* Dispatcher argument array (+1 for ending QCOMTEE_ARG_TYPE_INV). */ + struct qcomtee_arg u[QCOMTEE_ARGS_MAX + 1]; + + /* Inbound and Outbound buffers shared with QTEE. */ + struct qcomtee_buffer in_msg; /* Inbound buffer. */ + struct qcomtee_buffer out_msg; /* Outbound buffer. */ + struct tee_shm *in_shm; /* TEE shm allocated for inbound buffer. */ + struct tee_shm *out_shm; /* TEE shm allocated for outbound buffer. */ + + /* Extra data attached to this context. */ + void *data; +}; + +static inline struct qcomtee_object_invoke_ctx * +qcomtee_object_invoke_ctx_alloc(struct tee_context *ctx) +{ + struct qcomtee_object_invoke_ctx *oic; + + oic =3D kzalloc(sizeof(*oic), GFP_KERNEL); + if (oic) + oic->ctx =3D ctx; + return oic; +} + +/** + * qcomtee_object_do_invoke() - Submit an invocation for an object. + * @oic: context to use for the current invocation. + * @object: object being invoked. + * @op: requested operation on the object. + * @u: array of arguments for the current invocation. + * @result: result returned from QTEE. + * + * The caller is responsible for keeping track of the refcount for each ob= ject, + * including @object. On return, the caller loses ownership of all input + * objects of type %QCOMTEE_OBJECT_TYPE_CB. + * + * @object can be of %QCOMTEE_OBJECT_TYPE_ROOT or %QCOMTEE_OBJECT_TYPE_TEE. + * + * Return: On success, returns 0. On error, returns -EAGAIN if invocation + * failed and the user may retry the invocation, -ENODEV on fatal failure. + */ +int qcomtee_object_do_invoke(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *u, int *result); + +/** + * struct qcomtee_object_operations - Callback object operations. + * @release: release the object if QTEE is not using it. + * @dispatch: dispatch the operation requested by QTEE. + * @notify: report the status of any pending response submitted by @dispat= ch. + */ +struct qcomtee_object_operations { + void (*release)(struct qcomtee_object *object); + int (*dispatch)(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *args); + void (*notify)(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_object *object, int err); +}; + +/** + * struct qcomtee_object - QTEE or kernel object. + * @name: object name. + * @refcount: reference counter. + * @object_type: object type as &enum qcomtee_object_type. + * @info: extra information for the object. + * @ops: callback operations for objects of type %QCOMTEE_OBJECT_TYPE_CB. + * @work: work for async operations on the object. + * + * @work is used for releasing objects of %QCOMTEE_OBJECT_TYPE_TEE type. + */ +struct qcomtee_object { + const char *name; + struct kref refcount; + + enum qcomtee_object_type object_type; + struct object_info { + unsigned long qtee_id; + /* TEE context for QTEE object async requests. */ + struct tee_context *qcomtee_async_ctx; + } info; + + struct qcomtee_object_operations *ops; + struct work_struct work; +}; + +/* Static instances of qcomtee_object objects. */ +#define NULL_QCOMTEE_OBJECT ((struct qcomtee_object *)(0)) +extern struct qcomtee_object qcomtee_object_root; +#define ROOT_QCOMTEE_OBJECT (&qcomtee_object_root) + +static inline enum qcomtee_object_type +typeof_qcomtee_object(struct qcomtee_object *object) +{ + if (object =3D=3D NULL_QCOMTEE_OBJECT) + return QCOMTEE_OBJECT_TYPE_NULL; + return object->object_type; +} + +static inline const char *qcomtee_object_name(struct qcomtee_object *objec= t) +{ + if (object =3D=3D NULL_QCOMTEE_OBJECT) + return "null"; + + if (!object->name) + return "no-name"; + return object->name; +} + +/** + * qcomtee_object_user_init() - Initialize an object for the user. + * @object: object to initialize. + * @ot: type of object as &enum qcomtee_object_type. + * @ops: instance of callbacks. + * @fmt: name assigned to the object. + * + * Return: On success, returns 0; on failure, returns < 0. + */ +int qcomtee_object_user_init(struct qcomtee_object *object, + enum qcomtee_object_type ot, + struct qcomtee_object_operations *ops, + const char *fmt, ...); + +/* Object release is RCU protected. */ +int qcomtee_object_get(struct qcomtee_object *object); +void qcomtee_object_put(struct qcomtee_object *object); + +#define qcomtee_arg_for_each(i, args) \ + for (i =3D 0; args[i].type !=3D QCOMTEE_ARG_TYPE_INV; i++) + +/* Next argument of type @type after index @i. */ +int qcomtee_next_arg_type(struct qcomtee_arg *u, int i, + enum qcomtee_arg_type type); + +/* Iterate over argument of given type. */ +#define qcomtee_arg_for_each_type(i, args, at) \ + for (i =3D qcomtee_next_arg_type(args, 0, at); \ + args[i].type !=3D QCOMTEE_ARG_TYPE_INV; \ + i =3D qcomtee_next_arg_type(args, i + 1, at)) + +#define qcomtee_arg_for_each_input_buffer(i, args) \ + qcomtee_arg_for_each_type(i, args, QCOMTEE_ARG_TYPE_IB) +#define qcomtee_arg_for_each_output_buffer(i, args) \ + qcomtee_arg_for_each_type(i, args, QCOMTEE_ARG_TYPE_OB) +#define qcomtee_arg_for_each_input_object(i, args) \ + qcomtee_arg_for_each_type(i, args, QCOMTEE_ARG_TYPE_IO) +#define qcomtee_arg_for_each_output_object(i, args) \ + qcomtee_arg_for_each_type(i, args, QCOMTEE_ARG_TYPE_OO) + +#endif /* __QCOM_TEE_H */ diff --git a/include/uapi/linux/tee.h b/include/uapi/linux/tee.h index 441d97add53f..fbd00551d8ba 100644 --- a/include/uapi/linux/tee.h +++ b/include/uapi/linux/tee.h @@ -59,6 +59,7 @@ #define TEE_IMPL_ID_OPTEE 1 #define TEE_IMPL_ID_AMDTEE 2 #define TEE_IMPL_ID_TSTEE 3 +#define TEE_IMPL_ID_QTEE 4 =20 /* * OP-TEE specific capabilities --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AA802777F9 for ; Tue, 29 Apr 2025 06:07:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906834; cv=none; b=YrXMn2qZKyDnN6Yb0fvvapDt/HrkdcH92XVR5JH5Qmf/rc1C/eial93FUvsU8O12Kh20zkbnppJwEw3CGZBhUQ7xR5U0g1laIFtMLh9MKKZNaL43y5Jc2cXtkSRrct9icpHMpG/arTFheu4vWcB1c+SaUNmlhYix8tM1uzyO3nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906834; c=relaxed/simple; bh=gRSWjiLARrzmQRjZB5msPGkCXYVjyOfXLJXcn1Bq8mI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GAzcENxMZRvVgBHgROGQPEHfOZA5cqR3GU6Za4msj1l+6NzR1bI70iAMM78CdwM5ND9GPPMslAbYjdLy4u/OsG/PoH7d4BXchPmCjubCTH+TK4MuahGHUBKTFfQzcXgHmNJiNMVEDIkqGmGjLmUyUQIX0vTlYJZXOLzWeC7QDIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=NxkJKZPG; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="NxkJKZPG" Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNq955025304 for ; Tue, 29 Apr 2025 06:07:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= Do1ywL2aPWTcX1TQtpVzcVjGBq9aILCb5vCCA7xohNA=; b=NxkJKZPGJwcm8SdG DOtY9IFdfS1/xRc257+yHF6FsqpLUsbX1FJnWEgpV7Ob4bN11jSB8EP6LYP8otrl rwb8w7UZLnxSKJ3f74F6Mh8OVltlEN+jOBWU1KNxwDBh+DEppYmrzbektOdhm9v8 rfYauelfA66RsM3j+ultj0PrdvHSdWts4Y6Ba/1FZCYu6bV+tADspUS8UNC0SnnL 7K79LlIMIpNTW50yKWh8agm9EA8FzIJjUZGykwZKn9ByteuNJ0zJtXpHdTrAto27 NJAHbXPXhkccBUaS4DlGISWc/qqWM1NnjLqn4XTwK3sEqt4ECURxLBQHQNBaNfwr Et3P/w== Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468muqkm3d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:11 +0000 (GMT) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-736cb72efd5so4208961b3a.3 for ; Mon, 28 Apr 2025 23:07:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906830; x=1746511630; 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=Do1ywL2aPWTcX1TQtpVzcVjGBq9aILCb5vCCA7xohNA=; b=AoVxp5j8Q81YrpKzS4xu+F2N2/3z8Z0M2WrsTx3oreNTTJZ214nlzZYSv0qKGCLsWM 2F3oKpa47TundTt4KKhP4cuAUqmQ2LrluiwhrWmJwZsLBdtK+y94sQZOBVkj5HuR8AKY +6FPQCCGAQHJ9ylI/UH28EJljp5jTNfnW6dBUoZIbuK1WhQ25d+WPNeQQhpHvB2+DaKl Nu4xvx2EIbZWSVGSU13mjIf8IoOJgHKblnCXaI/tH4nf6eu65kwQKycgyoNqEL3v6Xap aw0wpBUMHNgyxFETSNDW21tU/t+SFaEwq5hHj9A5Ce5HIkbWqSLgz2jyz98u2+Z16LWD qBYA== X-Forwarded-Encrypted: i=1; AJvYcCVF03f40+TeCSiMymJnu432XFFfqttUY1FAyrGHMksCeUCPwUDy55RU0UADIflBJiCHjB46RCnQaopThK4=@vger.kernel.org X-Gm-Message-State: AOJu0YxR7cjzSNuNscDoqBYdq7uJI9uZf2ADaCkuGiKjoRCrOVag04z7 Z/ZXudchP9M/+4g1I0XjizH/nCnW/YXib7MjzOVfDjbwbAPr3yrKgTlkocpoxGyNbUsruYk5cT3 DENKLbQcEp8G1BXVsdRrz0pIu0vs5Alj6+uC7imi0nLk6DtzLlaql/qf+nhGyPA== X-Gm-Gg: ASbGnct+7Oi+YxJS83g64jsgUyei+fAf+4+G8GyWdlTFaU5BChNZCVNUXJ8b3Mh1QuG UjtqTdTBeHVZjaI1Kk9lxWoXwmdGqyMB+Zo0+1iz+MawlG8nX3/BcqqHGnOSASSkgRDq5GavccW lTB15USwoPmw1alwq0l5d+XvhY8K9SdEL6dhZ0A+FadgIgHZdXyRusGsXC47oH8/hF+KKi4BKfa x44SEugKiXq9K03/W5W2N4+ZEj8CV2eFyW951DgGznhpZwhorn/aRMMRaO405bzHtuS3iHX3bGJ KjEs1CqBZT4tNPWJMOjibWSZBDiBBnsPpVW9BTu5nv8IApGgIhR4KkaMU3pmM83bnlZtVxA= X-Received: by 2002:a05:6a00:1942:b0:736:ab49:d56 with SMTP id d2e1a72fcca58-7402710d0afmr3239288b3a.1.1745906829923; Mon, 28 Apr 2025 23:07:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEJCLBOCrwppDN55T67L8JyaCUrnYNo1UO1BS0g+YB8+xMmPNXJ21ihJHILegBGscF2pG70MQ== X-Received: by 2002:a05:6a00:1942:b0:736:ab49:d56 with SMTP id d2e1a72fcca58-7402710d0afmr3239250b3a.1.1745906829502; Mon, 28 Apr 2025 23:07:09 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.07.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:08 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:30 -0700 Subject: [PATCH v4 09/11] qcomtee: add primordial object 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-9-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX6eTVTkcIwHuw sczDqLT0eE5ztSkHkhz7uau4I/uizHVVF1o5Cr7tR+9bdYXRklwrx59AESFRnQdRX0Dd4hnSO/E +3kNFZglTTmApx/vtQOmgI+8hWqwP0b2lDfdagiwTdCUnMjOVHCm16wFoOBomjUv7wVC3f9D41U BKc1wSq585fXzm3/ewODRVa9ODMXshZLg/6buGZRIHNJzUBpUvLWFKKbmDN65eE89cuy7iGjy/u hvaa6oQ8aGdL+HplLA+9DrxfvvP42XebT97Tigc4HJbvZTSThFWQB+OV/SNvzgzU03LHGFt8Aci qg97iZZqbn2NGHc6BaoLN0mI46VFO6dtq5CwmFm4VeQcF2xxN/XPTI/E1q/KkoAEKnDhYl/eQ7p 5hHTHk9Ll1ojd8FJK2Tf1TWfZAR3EU8+vCPVt3C2fhjYYdA3cknYEd9w8dftK74qBESPtqVZ X-Proofpoint-GUID: U-8B653Qd4L4ELMr3DvaTRC5sIYtdyOd X-Proofpoint-ORIG-GUID: U-8B653Qd4L4ELMr3DvaTRC5sIYtdyOd X-Authority-Analysis: v=2.4 cv=M/5NKzws c=1 sm=1 tr=0 ts=68106c8f cx=c_pps a=WW5sKcV1LcKqjgzy2JUPuA==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=6dJEewf6gjddJ4Qna_sA:9 a=QEXdDO2ut3YA:10 a=OpyuDcXvxspvyRM73sMx:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 suspectscore=0 mlxscore=0 spamscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 bulkscore=0 phishscore=0 clxscore=1015 lowpriorityscore=0 priorityscore=1501 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 After booting, the kernel provides a static object known as the primordial object. This object is utilized by QTEE for native kernel services such as yield or privileged operations. Signed-off-by: Amirreza Zarrabi Acked-by: Sumit Garg Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/tee/qcomtee/Makefile | 1 + drivers/tee/qcomtee/core.c | 19 +++++++--- drivers/tee/qcomtee/primordial_obj.c | 65 +++++++++++++++++++++++++++++++= ++++ drivers/tee/qcomtee/qcomtee_private.h | 3 ++ 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/drivers/tee/qcomtee/Makefile b/drivers/tee/qcomtee/Makefile index 1b14b943e5f5..a3a2d00e83f0 100644 --- a/drivers/tee/qcomtee/Makefile +++ b/drivers/tee/qcomtee/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_QCOMTEE) +=3D qcomtee.o qcomtee-objs +=3D async.o qcomtee-objs +=3D call.o qcomtee-objs +=3D core.o +qcomtee-objs +=3D primordial_obj.o qcomtee-objs +=3D qcom_scm.o qcomtee-objs +=3D release.o qcomtee-objs +=3D shm.o diff --git a/drivers/tee/qcomtee/core.c b/drivers/tee/qcomtee/core.c index ad3b28f32529..a1ac492fd21a 100644 --- a/drivers/tee/qcomtee/core.c +++ b/drivers/tee/qcomtee/core.c @@ -39,10 +39,12 @@ int qcomtee_next_arg_type(struct qcomtee_arg *u, int i, } =20 /* - * QTEE expects IDs with the QCOMTEE_MSG_OBJECT_NS_BIT set for objects - * of the QCOMTEE_OBJECT_TYPE_CB type. + * QTEE expects IDs with QCOMTEE_MSG_OBJECT_NS_BIT set for objects of + * QCOMTEE_OBJECT_TYPE_CB type. The first ID with QCOMTEE_MSG_OBJECT_NS_BIT + * set is reserved for the primordial object. */ -#define QCOMTEE_OBJECT_ID_START (QCOMTEE_MSG_OBJECT_NS_BIT + 1) +#define QCOMTEE_OBJECT_PRIMORDIAL (QCOMTEE_MSG_OBJECT_NS_BIT) +#define QCOMTEE_OBJECT_ID_START (QCOMTEE_OBJECT_PRIMORDIAL + 1) #define QCOMTEE_OBJECT_ID_END (U32_MAX) =20 #define QCOMTEE_OBJECT_SET(p, type, ...) \ @@ -127,7 +129,9 @@ static void qcomtee_object_release(struct kref *refcoun= t) */ int qcomtee_object_get(struct qcomtee_object *object) { - if (object !=3D NULL_QCOMTEE_OBJECT && object !=3D ROOT_QCOMTEE_OBJECT) + if (object !=3D &qcomtee_primordial_object && + object !=3D NULL_QCOMTEE_OBJECT && + object !=3D ROOT_QCOMTEE_OBJECT) return kref_get_unless_zero(&object->refcount); =20 return 0; @@ -140,7 +144,9 @@ EXPORT_SYMBOL_GPL(qcomtee_object_get); */ void qcomtee_object_put(struct qcomtee_object *object) { - if (object !=3D NULL_QCOMTEE_OBJECT && object !=3D ROOT_QCOMTEE_OBJECT) + if (object !=3D &qcomtee_primordial_object && + object !=3D NULL_QCOMTEE_OBJECT && + object !=3D ROOT_QCOMTEE_OBJECT) kref_put(&object->refcount, qcomtee_object_release); } EXPORT_SYMBOL_GPL(qcomtee_object_put); @@ -222,6 +228,9 @@ static struct qcomtee_object *qcomtee_local_object_get(= unsigned int object_id) { struct qcomtee_object *object; =20 + if (object_id =3D=3D QCOMTEE_OBJECT_PRIMORDIAL) + return &qcomtee_primordial_object; + guard(rcu)(); object =3D xa_load(&xa_qcom_local_objects, object_id); /* It already checks for %NULL_QCOMTEE_OBJECT. */ diff --git a/drivers/tee/qcomtee/primordial_obj.c b/drivers/tee/qcomtee/pri= mordial_obj.c new file mode 100644 index 000000000000..a30967d89c91 --- /dev/null +++ b/drivers/tee/qcomtee/primordial_obj.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include "qcomtee_private.h" + +/** + * DOC: Primordial Object + * + * After boot, the kernel provides a static object of type + * %QCOMTEE_OBJECT_TYPE_CB called the primordial object. This object is us= ed + * for native kernel services or privileged operations. + * + * We support: + * - %QCOMTEE_OBJECT_OP_YIELD to yield by the thread running in QTEE. + * - %QCOMTEE_OBJECT_OP_SLEEP to wait for a period of time. + */ + +#define QCOMTEE_OBJECT_OP_YIELD 1 +#define QCOMTEE_OBJECT_OP_SLEEP 2 + +static int +qcomtee_primordial_obj_dispatch(struct qcomtee_object_invoke_ctx *oic, + struct qcomtee_object *primordial_object_unused, + u32 op, struct qcomtee_arg *args) +{ + int err =3D 0; + + switch (op) { + case QCOMTEE_OBJECT_OP_YIELD: + cond_resched(); + /* No output object. */ + oic->data =3D NULL; + break; + case QCOMTEE_OBJECT_OP_SLEEP: + /* Check message format matched QCOMTEE_OBJECT_OP_SLEEP op. */ + if (qcomtee_args_len(args) !=3D 1 || + args[0].type !=3D QCOMTEE_ARG_TYPE_IB || + args[0].b.size < sizeof(u32)) + return -EINVAL; + + msleep(*(u32 *)(args[0].b.addr)); + /* No output object. */ + oic->data =3D NULL; + break; + default: + err =3D -EINVAL; + } + + return err; +} + +static struct qcomtee_object_operations qcomtee_primordial_obj_ops =3D { + .dispatch =3D qcomtee_primordial_obj_dispatch, +}; + +struct qcomtee_object qcomtee_primordial_object =3D { + .name =3D "primordial", + .object_type =3D QCOMTEE_OBJECT_TYPE_CB, + .ops =3D &qcomtee_primordial_obj_ops +}; diff --git a/drivers/tee/qcomtee/qcomtee_private.h b/drivers/tee/qcomtee/qc= omtee_private.h index a6f2c0591580..e59cfb3b79dd 100644 --- a/drivers/tee/qcomtee/qcomtee_private.h +++ b/drivers/tee/qcomtee/qcomtee_private.h @@ -219,4 +219,7 @@ int qcomtee_user_object_submit(struct tee_context *ctx, struct tee_param *params, int num_params, int req_id, int errno); =20 +/* (2) Primordial Object. */ +extern struct qcomtee_object qcomtee_primordial_object; + #endif /* QCOMTEE_PRIVATE_H */ --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9113327815E for ; Tue, 29 Apr 2025 06:07:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906836; cv=none; b=GBZAVVDFt2FQY4OXoYryZmM7mSTQbuhV3Es50vPFvjs/YQb/XygcNNRU3tYI4j7yuFBw6j+QjALYfKSYdZv70/ntzIfb+4K2KV2puTmclokxPx1WYfhBwUUWHFiOXxXFG0T1/Gr8DzQxhpmka2hr4XpZUr20i7gIBvVUbjqvfBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906836; c=relaxed/simple; bh=k2r+SNauP2EWJ/MDLhPAs6ZWGpa3c4NElVUBQgqpDpU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kMxllGRvz9nPxH9eIwnzUhifrHfQTZpWT7h6CR80Zqx7r0yDuHp6lp/NeipO6siT1vnc2j3HICSJGOhqj4T2ALtLyXbh33xTo3RpzJHCy6jtGjROT6JoiVMkW5fwow2OBCKvh9lbTLAfjSbK7GE8+/qs7XReFTG9n/cAzIZl6Eg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=U0DCjH97; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="U0DCjH97" Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNqJ79006831 for ; Tue, 29 Apr 2025 06:07:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= ulX4slyoW8l26MV+FfYX8z46+bp7nCd5Z7IPISLuWZo=; b=U0DCjH97/7Ayc5/7 mnTrh/pGx+1Kb6AGFgS2lTNVONAzlHMWhtWsEYGo5Hnv1EWYO3w/hq0vSYgBQCq8 kTmZekicJW15ye5QbXlmLQKnjyOGKZc5mf1J3yARcmMODYh5xkY2EMww+RLk/9EO H+LvIVbHGDQQexcDFg60O8LciefzPT7QJSdMfsbr21XsoSDo3oLM9RdPcTvycTv8 zfZ5d6MopCvrBKxkjRmQdxmNYODvWUCTzoEyv2vs30e8bFwF1HMfhNbdl371dUtQ C8i3ndEJKU3AVr/hzOQhQNFGJIBvnQEx7o+c8TCsU0xNAhAEP6D5BEGuuZCuo7Ff 24krTQ== Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468ptmkdv3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:13 +0000 (GMT) Received: by mail-pg1-f200.google.com with SMTP id 41be03b00d2f7-b115383fcecso3225819a12.1 for ; Mon, 28 Apr 2025 23:07:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906832; x=1746511632; 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=ulX4slyoW8l26MV+FfYX8z46+bp7nCd5Z7IPISLuWZo=; b=TJ5opTKtZgenRMgr7xkadMM+0RqGO+un+gU5AnkDy8+BmAACxpQk5YYu6cUu63kN1z LAkOzdQZS/J7bmIiE1acOWGsEvZFHRreLibXkgjYE5JegKd5+zGe8YC19R2uKgZ5rmlR QVcyg0u3SqmMwmd66bnJpCUlk85A96lxkBdWtavjB7YemfXYr+vz0IG9N32TFAkI9zex jewLpNtcukia+24RsVSZw9DFkrESLtkm7vImnSAi04gzKT2dCZL9dCaEYlKj0fpJv8QL LcXJBtCI2JraXJt+Ttgj9hyVANJxUkSZI2WW/kb0xeLRJtjCPFK9Hl5olyJj2lACUBwE 5xrA== X-Forwarded-Encrypted: i=1; AJvYcCWC+7tJoWEq7mjA2Lc18xOdEEtzRFyYNl7g9Md0teZDGHumoFptgzUd5iyUPSd/23CX4CK17crEdxqOYSs=@vger.kernel.org X-Gm-Message-State: AOJu0YypFhX9JYDUoQaKmpkOQk5/Y1NQGXQRsxS2HaP2tgAsaEQs8zJ+ gKEVnn6sorkWC4iRYFWmX2ggEtRGigxsws12ro1RpXcZ2uVylMS2vEZNU2EmE4J9s0rGniUA9nT i1/tZLZT2D1PV8AvxNSP1ChHBSBtDUoxT01LYTMiBxTX2e/GR/QqyNAan2eymWQ== X-Gm-Gg: ASbGncurN7ifiWR80lNOQpbLAm2i/iyUN2dYF2wit8JehcclE0ICHowPA4g0gXxXMI5 tiIX5DNYq4FAiyOUNTBze9uMzOTYUrXEePj3+qLhWnUgPZeggrCDH3QjkLk8RLPTD5Rnj1kJHGy cBABxs2nahMeiQbp3iPy5OUXfxZR7BV0I7rsbBhtv/aoCxMAIZSWvTNbLNmXML9WqzP6zhY463O N+McLA8lFZHImTj/ehwE45uYiH6m6Y2RnXLhnioKInoAtKbmwXbGF9yMpudcWucLLLc7PzgWRyG u7+scy8UzFQ9kE1wjbtInPnN0QEzm+MNpOnrWc34uvMSH0uV4dagzqL+N056YHN/n1L47nI= X-Received: by 2002:a05:6a20:7fa3:b0:1f3:48d5:7303 with SMTP id adf61e73a8af0-20957906e0fmr2796436637.31.1745906831688; Mon, 28 Apr 2025 23:07:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH49rm6ShEyn8KhI375KtcgrKVIgcQWcIUb2J8ILsXPtuMAREwDvVWev9Eb1wEBuO//QN7Pqg== X-Received: by 2002:a05:6a20:7fa3:b0:1f3:48d5:7303 with SMTP id adf61e73a8af0-20957906e0fmr2796392637.31.1745906831205; Mon, 28 Apr 2025 23:07:11 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.07.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:10 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:31 -0700 Subject: [PATCH v4 10/11] qcomtee: enable TEE_IOC_SHM_ALLOC ioctl 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-10-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Proofpoint-ORIG-GUID: bwTnGWCKt9MUKvgFB82Wjlr5drmvSkhJ X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX+CTG6keAASuT gosBn1tRQLBF3ZBPMx8y1bhQB8j2jSctbSq8PpneCQvHEyarFHKQ1EbSosK/0fWJjLJ5+LadqrW zL664D877LywT+cZd869sBPC7U0A8+vV8RAPPI+nrU/pXcJHRjyVlLBHioQZn1UBFwS3VdG1GG6 Xz/5OXT3k4di0/5a3ZPxNqBY+LZcy6PSi+2GVm/HqpAdZtd/VU8uZZJxMKjlIynitUCTFQEPPY5 PqVXut8yK+YTCUaMle8hh+zkBPm6I44LrmafkVFUt8+VYtcU9nttS21NVgexf3kzZ0ctOXA1n5+ kgltfXGRV3uEcEaGMOtyblzrALBGf47jiK8ud7eBm2wVJBpTydstgHs633RvVQ09zi1N6jECHqd IaXVpxrFStTaCmyZ+yzhfRj7aL94zb+LaZWWdYeG5pc1J3G6Ij5eZsuYGDLjcbvUwAy+rIZz X-Proofpoint-GUID: bwTnGWCKt9MUKvgFB82Wjlr5drmvSkhJ X-Authority-Analysis: v=2.4 cv=DKWP4zNb c=1 sm=1 tr=0 ts=68106c91 cx=c_pps a=oF/VQ+ItUULfLr/lQ2/icg==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=OR1utrCcES5rRZ7Z870A:9 a=pAO9heLGEKS4qVLa:21 a=QEXdDO2ut3YA:10 a=3WC7DwWrALyhR5TkjVHa:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 clxscore=1015 priorityscore=1501 impostorscore=0 mlxscore=0 bulkscore=0 malwarescore=0 phishscore=0 spamscore=0 suspectscore=0 lowpriorityscore=0 adultscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 Enable userspace to allocate shared memory with QTEE. Since QTEE handles shared memory as object, a wrapper is implemented to represent tee_shm as an object. The shared memory identifier, obtained through TEE_IOC_SHM_ALLOC, is transferred to the driver using TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT/OUTPUT. Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- drivers/tee/qcomtee/Makefile | 1 + drivers/tee/qcomtee/call.c | 10 +- drivers/tee/qcomtee/mem_obj.c | 172 ++++++++++++++++++++++++++++++= ++++ drivers/tee/qcomtee/primordial_obj.c | 50 ++++++++++ drivers/tee/qcomtee/qcomtee_private.h | 39 ++++++++ drivers/tee/qcomtee/shm.c | 3 - 6 files changed, 270 insertions(+), 5 deletions(-) diff --git a/drivers/tee/qcomtee/Makefile b/drivers/tee/qcomtee/Makefile index a3a2d00e83f0..6cb9fd38dc67 100644 --- a/drivers/tee/qcomtee/Makefile +++ b/drivers/tee/qcomtee/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_QCOMTEE) +=3D qcomtee.o qcomtee-objs +=3D async.o qcomtee-objs +=3D call.o qcomtee-objs +=3D core.o +qcomtee-objs +=3D mem_obj.o qcomtee-objs +=3D primordial_obj.o qcomtee-objs +=3D qcom_scm.o qcomtee-objs +=3D release.o diff --git a/drivers/tee/qcomtee/call.c b/drivers/tee/qcomtee/call.c index 9e288723ec21..239ffc24476a 100644 --- a/drivers/tee/qcomtee/call.c +++ b/drivers/tee/qcomtee/call.c @@ -110,8 +110,8 @@ void qcomtee_context_del_qtee_object(struct tee_param *= param, * &enum qcomtee_arg_type value. It gets the object's refcount in @arg; * the caller should manage to put it afterward. * - * For user objects (i.e. (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_USE= R)), - * qcomtee_user_param_to_object() calls qcomtee_object_get() to keep a + * For non-QTEE objects (i.e. !(param->u.objref.flags & + * QCOMTEE_OBJREF_FLAG_TEE)), qcomtee_object_get() is called to keep a * temporary copy for the driver as the release of objects are asynchronous * and they may go away even before returning from the invocation. * @@ -135,6 +135,9 @@ int qcomtee_objref_to_arg(struct qcomtee_arg *arg, stru= ct tee_param *param, /* param is a QTEE object: */ } else if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_TEE) { err =3D qcomtee_context_find_qtee_object(&arg->o, param, ctx); + /* param is a memory object: */ + } else if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_MEM) { + err =3D qcomtee_memobj_param_to_object(&arg->o, param, ctx); } else { err =3D -EINVAL; } @@ -181,6 +184,9 @@ int qcomtee_objref_from_arg(struct tee_param *param, st= ruct qcomtee_arg *arg, if (is_qcomtee_user_object(object)) err =3D qcomtee_user_param_from_object(param, object, ctx); + else if (is_qcomtee_memobj_object(object)) + err =3D qcomtee_memobj_param_from_object(param, object, + ctx); else err =3D -EINVAL; =20 diff --git a/drivers/tee/qcomtee/mem_obj.c b/drivers/tee/qcomtee/mem_obj.c new file mode 100644 index 000000000000..e17eb3b3dc2e --- /dev/null +++ b/drivers/tee/qcomtee/mem_obj.c @@ -0,0 +1,172 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include + +#include "qcomtee_private.h" + +/** + * DOC: Memory and Mapping Objects + * + * QTEE uses memory objects for memory sharing with Linux. + * A memory object can be a standard dma_buf or a contiguous memory range, + * e.g., tee_shm. A memory object should support one operation: map. When + * invoked by QTEE, a mapping object is generated. A mapping object suppor= ts + * one operation: unmap. + * + * (1) To map a memory object, QTEE invokes the primordial object with + * %QCOMTEE_OBJECT_OP_MAP_REGION operation; see + * qcomtee_primordial_obj_dispatch(). + * (2) To unmap a memory object, QTEE releases the mapping object which + * calls qcomtee_mem_object_release(). + * + * The map operation is implemented in the primordial object as a privileg= ed + * operation instead of qcomtee_mem_object_dispatch(). Otherwise, on + * platforms without shm_bridge, a user can trick QTEE into writing to the + * kernel memory by passing a user object as a memory object and returning= a + * random physical address as the result of the mapping request. + */ + +struct qcomtee_mem_object { + struct qcomtee_object object; + struct tee_shm *shm; + /* QTEE requires these felids to be page aligned. */ + phys_addr_t paddr; /* Physical address of range. */ + size_t size; /* Size of the range. */ +}; + +#define to_qcomtee_mem_object(o) \ + container_of((o), struct qcomtee_mem_object, object) + +static struct qcomtee_object_operations qcomtee_mem_object_ops; + +/* Is it a memory object using tee_shm? */ +int is_qcomtee_memobj_object(struct qcomtee_object *object) +{ + return object !=3D NULL_QCOMTEE_OBJECT && + typeof_qcomtee_object(object) =3D=3D QCOMTEE_OBJECT_TYPE_CB && + object->ops =3D=3D &qcomtee_mem_object_ops; +} + +static int qcomtee_mem_object_dispatch(struct qcomtee_object_invoke_ctx *o= ic, + struct qcomtee_object *object, u32 op, + struct qcomtee_arg *args) +{ + return -EINVAL; +} + +static void qcomtee_mem_object_release(struct qcomtee_object *object) +{ + struct qcomtee_mem_object *mem_object =3D to_qcomtee_mem_object(object); + + /* Matching get is in qcomtee_memobj_param_to_object(). */ + tee_shm_put(mem_object->shm); + kfree(mem_object); +} + +static struct qcomtee_object_operations qcomtee_mem_object_ops =3D { + .release =3D qcomtee_mem_object_release, + .dispatch =3D qcomtee_mem_object_dispatch, +}; + +/** + * qcomtee_memobj_param_to_object() - OBJREF parameter to &struct qcomtee_= object. + * @object: object returned. + * @param: TEE parameter. + * @ctx: context in which the conversion should happen. + * + * @param is an OBJREF with %QCOMTEE_OBJREF_FLAG_MEM flags. + * It calls qcomtee_object_get() to keep a copy of @object for the driver + * as well as a copy for QTEE. + * + * Return: On success return 0 or <0 on failure. + */ +int qcomtee_memobj_param_to_object(struct qcomtee_object **object, + struct tee_param *param, + struct tee_context *ctx) +{ + struct qcomtee_mem_object *mem_object __free(kfree) =3D NULL; + struct tee_shm *shm; + int err; + + mem_object =3D kzalloc(sizeof(*mem_object), GFP_KERNEL); + if (!mem_object) + return -ENOMEM; + + shm =3D tee_shm_get_from_id(ctx, param->u.objref.id); + if (!shm) + return -EINVAL; + + /* mem-object wrapping the memref. */ + err =3D qcomtee_object_user_init(&mem_object->object, + QCOMTEE_OBJECT_TYPE_CB, + &qcomtee_mem_object_ops, "tee-shm-%d", + shm->id); + if (err) { + tee_shm_put(shm); + + return err; + } + + mem_object->paddr =3D shm->paddr; + mem_object->size =3D shm->size; + mem_object->shm =3D shm; + + *object =3D &no_free_ptr(mem_object)->object; + qcomtee_object_get(*object); + + return 0; +} + +/* Reverse what qcomtee_memobj_param_to_object() does. */ +int qcomtee_memobj_param_from_object(struct tee_param *param, + struct qcomtee_object *object, + struct tee_context *ctx) +{ + struct qcomtee_mem_object *mem_object; + + mem_object =3D to_qcomtee_mem_object(object); + /* Sure if the memobj is in a same context it is originated from. */ + if (mem_object->shm->ctx !=3D ctx) + return -EINVAL; + + param->u.objref.id =3D mem_object->shm->id; + param->u.objref.flags =3D QCOMTEE_OBJREF_FLAG_MEM; + + /* Passing shm->id to userspace; drop the reference. */ + qcomtee_object_put(object); + + return 0; +} + +/** + * qcomtee_mem_object_map() - Map a memory object. + * @object: memory object. + * @map_object: created mapping object. + * @mem_paddr: physical address of the memory. + * @mem_size: size of the memory. + * @perms: QTEE access permissions. + * + * Return: On success return 0 or <0 on failure. + */ +int qcomtee_mem_object_map(struct qcomtee_object *object, + struct qcomtee_object **map_object, u64 *mem_paddr, + u64 *mem_size, u32 *perms) +{ + struct qcomtee_mem_object *mem_object =3D to_qcomtee_mem_object(object); + + /* Reuses the memory object as a mapping object by re-sharing it. */ + qcomtee_object_get(&mem_object->object); + + *map_object =3D &mem_object->object; + *mem_paddr =3D mem_object->paddr; + *mem_size =3D mem_object->size; + *perms =3D QCOM_SCM_PERM_RW; + + return 0; +} diff --git a/drivers/tee/qcomtee/primordial_obj.c b/drivers/tee/qcomtee/pri= mordial_obj.c index a30967d89c91..598557f09746 100644 --- a/drivers/tee/qcomtee/primordial_obj.c +++ b/drivers/tee/qcomtee/primordial_obj.c @@ -16,18 +16,31 @@ * for native kernel services or privileged operations. * * We support: + * - %QCOMTEE_OBJECT_OP_MAP_REGION to map a memory object and return mapp= ing + * object and mapping information (see qcomtee_mem_object_map()). * - %QCOMTEE_OBJECT_OP_YIELD to yield by the thread running in QTEE. * - %QCOMTEE_OBJECT_OP_SLEEP to wait for a period of time. */ =20 +#define QCOMTEE_OBJECT_OP_MAP_REGION 0 #define QCOMTEE_OBJECT_OP_YIELD 1 #define QCOMTEE_OBJECT_OP_SLEEP 2 =20 +/* Mapping information format as expected by QTEE. */ +struct qcomtee_mapping_info { + u64 paddr; + u64 len; + u32 perms; +} __packed; + static int qcomtee_primordial_obj_dispatch(struct qcomtee_object_invoke_ctx *oic, struct qcomtee_object *primordial_object_unused, u32 op, struct qcomtee_arg *args) { + struct qcomtee_mapping_info *map_info; + struct qcomtee_object *mem_object; + struct qcomtee_object *map_object; int err =3D 0; =20 switch (op) { @@ -35,6 +48,7 @@ qcomtee_primordial_obj_dispatch(struct qcomtee_object_inv= oke_ctx *oic, cond_resched(); /* No output object. */ oic->data =3D NULL; + break; case QCOMTEE_OBJECT_OP_SLEEP: /* Check message format matched QCOMTEE_OBJECT_OP_SLEEP op. */ @@ -46,6 +60,29 @@ qcomtee_primordial_obj_dispatch(struct qcomtee_object_in= voke_ctx *oic, msleep(*(u32 *)(args[0].b.addr)); /* No output object. */ oic->data =3D NULL; + + break; + case QCOMTEE_OBJECT_OP_MAP_REGION: + if (qcomtee_args_len(args) !=3D 3 || + args[0].type !=3D QCOMTEE_ARG_TYPE_OB || + args[1].type !=3D QCOMTEE_ARG_TYPE_IO || + args[2].type !=3D QCOMTEE_ARG_TYPE_OO || + args[0].b.size < sizeof(struct qcomtee_mapping_info)) + return -EINVAL; + + map_info =3D args[0].b.addr; + mem_object =3D args[1].o; + + qcomtee_mem_object_map(mem_object, &map_object, + &map_info->paddr, &map_info->len, + &map_info->perms); + + args[2].o =3D map_object; + /* One output object; pass it for cleanup to notify. */ + oic->data =3D map_object; + + qcomtee_object_put(mem_object); + break; default: err =3D -EINVAL; @@ -54,8 +91,21 @@ qcomtee_primordial_obj_dispatch(struct qcomtee_object_in= voke_ctx *oic, return err; } =20 +/* Called after submitting the callback response. */ +static void qcomtee_primordial_obj_notify(struct qcomtee_object_invoke_ctx= *oic, + struct qcomtee_object *unused, + int err) +{ + struct qcomtee_object *object =3D oic->data; + + /* If err, QTEE did not obtain mapping object. Drop it. */ + if (object && err) + qcomtee_object_put(object); +} + static struct qcomtee_object_operations qcomtee_primordial_obj_ops =3D { .dispatch =3D qcomtee_primordial_obj_dispatch, + .notify =3D qcomtee_primordial_obj_notify, }; =20 struct qcomtee_object qcomtee_primordial_object =3D { diff --git a/drivers/tee/qcomtee/qcomtee_private.h b/drivers/tee/qcomtee/qc= omtee_private.h index e59cfb3b79dd..a70052803399 100644 --- a/drivers/tee/qcomtee/qcomtee_private.h +++ b/drivers/tee/qcomtee/qcomtee_private.h @@ -15,6 +15,7 @@ /* Flags relating to object reference. */ #define QCOMTEE_OBJREF_FLAG_TEE BIT(0) #define QCOMTEE_OBJREF_FLAG_USER BIT(1) +#define QCOMTEE_OBJREF_FLAG_MEM BIT(2) =20 /** * struct qcomtee - Main service struct. @@ -222,4 +223,42 @@ int qcomtee_user_object_submit(struct tee_context *ctx, /* (2) Primordial Object. */ extern struct qcomtee_object qcomtee_primordial_object; =20 +/* (3) Memory Object API. */ + +/* Is it a memory object using tee_shm? */ +int is_qcomtee_memobj_object(struct qcomtee_object *object); + +/** + * qcomtee_memobj_param_to_object() - OBJREF parameter to &struct qcomtee_= object. + * @object: object returned. + * @param: TEE parameter. + * @ctx: context in which the conversion should happen. + * + * @param is an OBJREF with %QCOMTEE_OBJREF_FLAG_MEM flags. + * + * Return: On success return 0 or <0 on failure. + */ +int qcomtee_memobj_param_to_object(struct qcomtee_object **object, + struct tee_param *param, + struct tee_context *ctx); + +/* Reverse what qcomtee_memobj_param_to_object() does. */ +int qcomtee_memobj_param_from_object(struct tee_param *param, + struct qcomtee_object *object, + struct tee_context *ctx); + +/** + * qcomtee_mem_object_map() - Map a memory object. + * @object: memory object. + * @map_object: created mapping object. + * @mem_paddr: physical address of the memory. + * @mem_size: size of the memory. + * @perms: QTEE access permissions. + * + * Return: On success return 0 or <0 on failure. + */ +int qcomtee_mem_object_map(struct qcomtee_object *object, + struct qcomtee_object **map_object, u64 *mem_paddr, + u64 *mem_size, u32 *perms); + #endif /* QCOMTEE_PRIVATE_H */ diff --git a/drivers/tee/qcomtee/shm.c b/drivers/tee/qcomtee/shm.c index 998aabe96434..9bdf9e48bfeb 100644 --- a/drivers/tee/qcomtee/shm.c +++ b/drivers/tee/qcomtee/shm.c @@ -113,9 +113,6 @@ static int qcomtee_shm_unregister(struct tee_context *c= tx, struct tee_shm *shm) static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm, size_t size, size_t align) { - if (!(shm->flags & TEE_SHM_PRIV)) - return -ENOMEM; - return tee_dyn_shm_alloc_helper(shm, size, align, qcomtee_shm_register); } =20 --=20 2.34.1 From nobody Tue Dec 16 12:33:51 2025 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35823278E5A for ; Tue, 29 Apr 2025 06:07:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906838; cv=none; b=H/Iv5R/Cb6eWJmYe3xtg6+qPsrKPFkxtPEMSzSJ7OzkaZS+qA0Ie2Hb0PhVlZo3jPQ5+5VXvjVBSXonB94ipvVHTi4vz8i05JKWl+AzKXTMb2G1mCcQ3TxmTMkbM8vql0t9EhCZmZxtPPnuYxlOzU8uyZlcxio6+GRiGPZVGV5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745906838; c=relaxed/simple; bh=4k2KSDPURPddObcXyDVi8RbblAgUWBza0O2dAjpnuac=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SIkq+8MzQEAqHoGF9jWzfoKGVhha80nPTIGVS6HnaiJRlZjqtsCXtVvA7v7IVjxhyaTffmQBtn4fGrDTKmyzkIMxtSyne6OdbAZCmr8zOvSYqkOLBV8AQVXN6SyhcmxbJPUxlcmNMcPApy2bOZXcw3c87TrI7a2uKtI2OqvX+Hw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=FxBOyeiA; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="FxBOyeiA" Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 53SNqCu7000768 for ; Tue, 29 Apr 2025 06:07:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= pw0rd5bbftIaO3W4VpK12WfhwQcOHW8+t1g2YsM9EwI=; b=FxBOyeiA9/nLs5A8 yxpP4lKiNAIbsQKPD1FvfO68PIZINmxdZgmscriQC7jzsVcTK8HAdzniE6y2ARiK IusueYOXYq1AdhWakDgyIx+MpYtaX6KiVIEz8zDKHUcjoe57uuVLU1C2WxblVhWK wpi0uYRBo3xyHkoNi0XuhlfRpSdVyywSrQstxmY4TINdiQLhNznryq85/gtyQ/rM WDrJ/FX45CrvfpkjSwc/jrgoa37ZdlmoqrEEuDMkaNKrf8Z/QLBegB5eFy9NnAtK YSl022n77RVgOr5MoERH7t3StTnz9u45ziap4uRsiBQPf/4lOUGaoaDcJ3VTHS32 Ket7pg== Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 468qv9kd6n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Apr 2025 06:07:14 +0000 (GMT) Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-b115383fcecso3225829a12.1 for ; Mon, 28 Apr 2025 23:07:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745906833; x=1746511633; 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=pw0rd5bbftIaO3W4VpK12WfhwQcOHW8+t1g2YsM9EwI=; b=i/8764MpjRszjUkzrScqJbTBaQ2sLSp4psKq0+07smxgitDRVpVIxxCzdGvTGHwkup nZTnZI0l4XDDT5xKqqwDXww9tIWg59B6N//c5WYFYBZyOoZw4DmbTeqnKyq9CdroRbaR CCkLTIIlfb5YtRYgtnj12A4LK4uR+PtiRPSHur1Vei0ApzORtV2aQiCyz5ha8QrECUSD zTBR8VMFAvo1pCgO4u0evmswqdBhxz3n/OTmg8wH6NHh0TZJ0PUoemZxxmheiMYhqz4a VkVjEe5Agz1ajGTfxHgoTil5S12ADqsZ0s+FUz9nDh8fmi2l/irVSPWtKwqa6GmkYPC3 sA5A== X-Forwarded-Encrypted: i=1; AJvYcCXGa6c00+cc/Sa2LwUSUnU8oTQrG5B7ydqCdg5YNrbMG8rNAH/vUMUNaZjR0lgV60fnauW0CffKs1xkdak=@vger.kernel.org X-Gm-Message-State: AOJu0YxSA5GAsAWCT2y0WLU4nz6YQfOO9+S5PzVXBLZBAQy21DCKfWGM ba8OOMQY4FwQm3tHZdFVUAMAbKwlb286LZOs8cdbpPLTQC+5cPTk0wl3Yglf6xxofwDN9APV60E PY6NHzgW2miMtd4xU9mSel6JUaJbjKUWv1sqUGJnSESS2ZTJUfZpVsF/gzSnGWQ== X-Gm-Gg: ASbGncvQT8Zm4tWtLmMtRFf27J128AHI8gMcQ48zQ04qkuJivKSXGOI2WGex27aJTj+ /niSLfSQUXSTfQBAIpIqEf/ag4IDOkEvVzZIjvZfeILk+juHoC90Yuxe1kyyi3G1JzXZNNZQTU8 dpv7RziTjVjID57/AXg8TDYOSFa6trvgjJr+GFPo6rhKtru97M3CueNGtAdyYQkWI1YchPnzsav Tyoh5DYvKvngqgRWOerbBK943iIFrYVGWDsCBNFYFbu8LstA8r/g7xF0UsdgBCI4KooZ/lKwd+1 C4ste8InrJYiDAXtvIshWfJNtwtqPswVdjPsiJNUIII2DRAQZ370Xo8ksvnC+03xzHFeUlc= X-Received: by 2002:a05:6a20:43a1:b0:1f3:40a9:2c36 with SMTP id adf61e73a8af0-209561ed550mr2627424637.10.1745906833296; Mon, 28 Apr 2025 23:07:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFHdv2z3Dz3XdkSIvWGqI4vBe4abfk4+xgF4r2Y6iDadu24W/3/ymgSO5Ode7BHoDl3Xv8RUQ== X-Received: by 2002:a05:6a20:43a1:b0:1f3:40a9:2c36 with SMTP id adf61e73a8af0-209561ed550mr2627395637.10.1745906832886; Mon, 28 Apr 2025 23:07:12 -0700 (PDT) Received: from hu-azarrabi-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25acd9cbsm9353642b3a.174.2025.04.28.23.07.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 23:07:12 -0700 (PDT) From: Amirreza Zarrabi Date: Mon, 28 Apr 2025 23:06:32 -0700 Subject: [PATCH v4 11/11] Documentation: tee: Add Qualcomm TEE driver 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: quoted-printable Message-Id: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-11-6a143640a6cb@oss.qualcomm.com> References: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> In-Reply-To: <20250428-qcom-tee-using-tee-ss-without-mem-obj-v4-0-6a143640a6cb@oss.qualcomm.com> To: Jens Wiklander , Sumit Garg , Bjorn Andersson , Konrad Dybcio , Bartosz Golaszewski , Apurupa Pattapu , Kees Cook , "Gustavo A. R. Silva" , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, Amirreza Zarrabi X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=AO34vM+d c=1 sm=1 tr=0 ts=68106c92 cx=c_pps a=rz3CxIlbcmazkYymdCej/Q==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=IkcTkHD0fZMA:10 a=XR8D0OoHHMoA:10 a=EUspDBNiAAAA:8 a=7CQSdrXTAAAA:8 a=VwQbUJbxAAAA:8 a=4VYrYejmMApVX9KxlZkA:9 a=QEXdDO2ut3YA:10 a=M0EVDjxxv-UA:10 a=bFCP_H2QrGi7Okbo017w:22 a=a-qgeE7W1pNrGK8U0ZQC:22 X-Proofpoint-GUID: yxeMfVaAiee41GYfDg_xLWKzP_Uhn7eV X-Proofpoint-ORIG-GUID: yxeMfVaAiee41GYfDg_xLWKzP_Uhn7eV X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNDI5MDA0NCBTYWx0ZWRfX9qskzXSc1Oq+ wRfKiUWvEaWBIsHfh5uH3miIn3Yjycm4j8Dd4ulvX40pHu43D0UPzp/C3YFu7KPmEocGnADF+wO +8b+B4ZZ5iv4YsbRMY6hR8HeWUT1OQOBZtz5ioimwpMDNNuL7jHTKiJZchM8+WSQteRM42y0FXS 5Fw6hdn8EbmNvTQPl9AXcMrCdJ5fP+mQSK7wB+I/O0shsggUD3iKMX1XUvzqUFzDO+4RKVmnrGF mOzrwKeRkbsg4DhPwtvtLJ5ocDyye6Z8Xa34n9vl9J4LJZiuQguDdORXwB/MTocpXyeLFo0zHOt zaZUqTpwAVYmKwUrImFN+/ldd2iMFOLWa1uL0WYtV5loYlUvM9leZnLvxZLLLsRbC0NYG1eUlYU UqzbTa7OQOlv8nJLOTC5m1gJhcbzQAub0CqcpmC6ZUALxOZUjJS7SsaB6QlpGEKgYi652Zl7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-04-29_01,2025-04-24_02,2025-02-21_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 adultscore=0 clxscore=1015 malwarescore=0 spamscore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 phishscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2504070000 definitions=main-2504290044 Add documentation for the Qualcomm TEE driver. Signed-off-by: Amirreza Zarrabi Tested-by: Neil Armstrong # on SM8550-QRD Tested-by: Neil Armstrong # on SM8650-HDK Tested-by: Neil Armstrong # on SM8650-QRD --- Documentation/tee/index.rst | 1 + Documentation/tee/qtee.rst | 150 ++++++++++++++++++++++++++++++++++++++++= ++++ MAINTAINERS | 1 + 3 files changed, 152 insertions(+) diff --git a/Documentation/tee/index.rst b/Documentation/tee/index.rst index 4be6e69d7837..62afb7ee9b52 100644 --- a/Documentation/tee/index.rst +++ b/Documentation/tee/index.rst @@ -11,6 +11,7 @@ TEE Subsystem op-tee amd-tee ts-tee + qtee =20 .. only:: subproject and html =20 diff --git a/Documentation/tee/qtee.rst b/Documentation/tee/qtee.rst new file mode 100644 index 000000000000..8ae4da17c3a7 --- /dev/null +++ b/Documentation/tee/qtee.rst @@ -0,0 +1,150 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +QTEE (Qualcomm Trusted Execution Environment) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The QTEE driver handles communication with Qualcomm TEE [1]. + +The lowest level of communication with QTEE builds on the ARM SMC Calling +Convention (SMCCC) [2], which is the foundation for QTEE's Secure Channel +Manager (SCM) [3] used internally by the driver [4]. + +In a QTEE-based system, services are represented as objects with a series = of +operations that can be called to produce results, including other objects. + +When an object is hosted within QTEE, executing its operations is referred +to as direct invocation. QTEE can invoke objects hosted in the kernel or +userspace using a method known as callback requests. + +The SCM provides two functions for direct invocation and callback request: + +- QCOM_SCM_SMCINVOKE_INVOKE for direct invocation. It can return either + a result or a callback request. +- QCOM_SCM_SMCINVOKE_CB_RSP submits a response for a previous callback req= uest. + +The QTEE Transport Message [5] is stacked on top of the SCM driver functio= ns. + +A message consists of two buffers shared with QTEE: inbound and outbound +buffers. The inbound buffer is used for direct invocation, and the outbound +buffer is used to make callback requests. This picture shows the contents = of +a QTEE transport message:: + + +---------------------+ + | v + +-----------------+-------+-------+------+--------------------------+ + | qcomtee_msg_ |object | buffer | | + | object_invoke | id | offset, size | | = (inbound buffer) + +-----------------+-------+--------------+--------------------------+ + <---- header -----><---- arguments ------><- in/out buffer payload -> + + +-----------+ + | v + +-----------------+-------+-------+------+----------------------+ + | qcomtee_msg_ |object | buffer | | + | callback | id | offset, size | | (out= bound buffer) + +-----------------+-------+--------------+----------------------+ + +Each buffer is started with a header and array of arguments. + +QTEE Transport Message supports four types of arguments: + +- Input Object (IO) is an object parameter to the current invocation + or callback request. +- Output Object (OO) is an object parameter from the current invocation + or callback request. +- Input Buffer (IB) is (offset, size) pair to the inbound or outbound regi= on + to store parameter to the current invocation or callback request. +- Output Buffer (OB) is (offset, size) pair to the inbound or outbound reg= ion + to store parameter from the current invocation or callback request. + +The QTEE driver provides the qcomtee_object, which represents an object wi= thin +both QTEE and the kernel. To access any service in QTEE, a client needs to +invoke an instance of this object. Any structure intended to represent a s= ervice +for export to QTEE should include an instance of qcomtee_object:: + + struct driver_service { + struct qcomtee_object object; + ... + }; + + #define to_driver_service_object(o) container_of((o), struct driver_servi= ce, object) + + static int driver_service_dispatch(struct qcomtee_object *object, u32 op, + struct qcomtee_arg *args) + { + struct driver_service *so =3D to_driver_service_object(object); + + switch(op) { + case OBJECT_OP1: + ... + break; + default: + return -EINVAL; + } + } + + static void driver_service_object_release(struct si_object *object) + { + struct driver_service *so =3D to_driver_service_object(object); + kfree(so); + } + + struct si_object_operations driver_service_ops =3D { + .release =3D driver_service_object_release; + .dispatch =3D driver_service_dispatch; + }; + + void service_init(void) + { + struct driver_service *so =3D kzalloc(sizeof(*so), GFP_KERNEL); + + /* Initialize so->object as a callback object. */ + qcomtee_object_user_init(&so->object, QCOMTEE_OBJECT_TYPE_CB_OBJECT, + &driver_service_ops, "driver_service_object"); + + /* Invoke a QTEE object and pass/register 'so->object' with QTEE. */ + ... + } + module_init(service_init); + +The QTEE driver utilizes qcomtee_object to encapsulate userspace objects. = When +a callback request is made, it translates into calling the dispatch operat= ion. +For userspace objects, this is converted into requests accessible to callb= ack +servers and available through generic TEE API IOCTLs. + +Picture of the relationship between the different components in the QTEE +architecture:: + + User space Kernel Secure world + ~~~~~~~~~~ ~~~~~~ ~~~~~~~~~~~~ + +--------+ +----------+ +-------------= -+ + | Client | |callback | | Trusted = | + +--------+ |server | | Application = | + /\ +----------+ +-------------= -+ + || +----------+ /\ /\ + || |callback | || || + || |server | || \/ + || +----------+ || +-------------= -+ + \/ /\ || | TEE Internal= | + +-------+ || || | API = | + | TEE | || || +--------+--------+ +-------------= -+ + | Client| || || | TEE | QTEE | | QTEE = | + | API | \/ \/ | subsys | driver | | Trusted OS = | + +-------+----------------+----+-------+----+-------------+-------------= -+ + | Generic TEE API | | QTEE MSG = | + | IOCTL (TEE_IOC_*) | | SMCCC (QCOM_SCM_SMCINVOKE_*) = | + +-----------------------------+ +--------------------------------= -+ + +References +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +[1] https://docs.qualcomm.com/bundle/publicresource/topics/80-70015-11/qua= lcomm-trusted-execution-environment.html + +[2] http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html + +[3] drivers/firmware/qcom/qcom_scm.c + +[4] drivers/tee/qcomtee/qcom_scm.c + +[5] drivers/tee/qcomtee/qcomtee_msg.h diff --git a/MAINTAINERS b/MAINTAINERS index 88a9ad34bcf6..79259219d292 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20261,6 +20261,7 @@ QUALCOMM TEE (QCOMTEE) DRIVER M: Amirreza Zarrabi L: linux-arm-msm@vger.kernel.org S: Maintained +F: Documentation/tee/qtee.rst F: drivers/tee/qcomtee/ F: include/linux/firmware/qcom/qcom_tee.h =20 --=20 2.34.1