Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp2451330rdb; Wed, 21 Feb 2024 08:04:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVc9ddnfjykqhaOEVnKwYl/V1OKc4ED9Rd0oDzu95+lb82FnTnpDn28USB+JeDkuRcmDwlNGSKxwnbL6WHZiQaU6nGCfpNwuuTBQrO0qA== X-Google-Smtp-Source: AGHT+IGFjAQd3GQFfi+gO+B1zSGtNtceEan921B94cjrnbReR7sX/5QjSEJqi4KeAZmleCL0B3z7 X-Received: by 2002:a6b:e30d:0:b0:7c7:6a40:5492 with SMTP id u13-20020a6be30d000000b007c76a405492mr4876331ioc.4.1708531465058; Wed, 21 Feb 2024 08:04:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708531465; cv=pass; d=google.com; s=arc-20160816; b=QmcqMLadPOF6OiElRZN1l4VZDgLBt8DjexbgQfy2QnfVxVklts8BhrvYSvSdz3L+qj 5++I2f8Uagzmgu7wM94JtszodQY0Kt5SEekQ9koqhj1S38y8wdLOiNWaCVVNeelzuP2m Qrz33jCk6WmJU9ugRrpDHkSNGs4lB+EMjXOmfErD4PP0EdUyi7oDomB4odhbokBLwBYU 8mbRfbw4a1v9e3trAhkl1n/D1u/1Yjhsraj9OkuhgghQlwCqGk7KWRA4SKpRtLbOGqyw 9iSq22c7rYr7z2jASAQXgD2+CHLo4qzoQRLMMRtUmRCJ/nvGFK9knE8l2WBuMLOkldNp ohTg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :in-reply-to:date:dkim-signature; bh=4+3Sm96T7vJUTpcS+4lNZ9LA/sEbsUcxo41ULm96MSg=; fh=QlUEkUWqCbKBFBNcxyRbDHTf2hxrqXmYJqjeBVIF4OY=; b=Vw2c8wEaMV3Z8ojvrHytNoj4uG8k+gLxuYGO2/ursZdaqfw55Ld8rPcF8XGXfHgezN 1UUeWdAoXNGiJErVarurLKvtd5zxj2osX/7Gvz6rr7WEpWrDrMnYTGRA7pWu0h2T14CA 2IiYqLMKQYchPqzLDz0/E51i60YAZQLvfYylc7PG5ulpCsTgkMx6tGDpFDyUqLQeaIfx 6BhKbdH4P05t5fMMNhLzOXUwuePA97czETxO1rS27gTskkUu7Sx62jhlAnUVoB2FexF4 4mWCbOeDtQ8odeHgxRsG+jIXdRAKs7rfQ5PY0WOa+ZNcWS2XYZaJNe4yBsUnlpLJQmgM HHDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CBfyA11N; arc=pass (i=1 spf=pass spfdomain=flex--panikiel.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-75066-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75066-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i21-20020a0566022c9500b007c72afddebesi2710591iow.6.2024.02.21.08.04.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:04:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75066-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CBfyA11N; arc=pass (i=1 spf=pass spfdomain=flex--panikiel.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-75066-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75066-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2EE4628739E for ; Wed, 21 Feb 2024 16:04:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E163839E5; Wed, 21 Feb 2024 16:02:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CBfyA11N" Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 190C382D61 for ; Wed, 21 Feb 2024 16:02:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708531362; cv=none; b=FNwGXCLZZWRD/5BXD/QAtZwLWtC+CUIUe7BAYPrrO8ehc9bnDRGRAeUfDvvEKB5hR4aire1VGMNg1IVKqETQQ8FJlPfRfQGlaS+joPQzpwmBtEbK6diiMfCYf1xNJ7KIcM2HUXCy1Mwg/5mkwx0AQE9sB9bd0eRqU63c8Pah8Ao= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708531362; c=relaxed/simple; bh=kX7afhI/UBfau7HZtOSciregQcoGbwDl2lFHyJNlIT0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y0CWIVawMaFZ6iIy2M74jwENu67xZ3gu/KmGQxGDrnc+YUbhED5jKHOh31T2D3BtWxG+DFlLEmBJy4N6N2UFujYc+MpvELixy3lM6rpoUcYa2yv/p98btMD5dNNoL62bBK3yUKOTaydAvRT1BKtcAGuQEnNblJYi40ykjlShp1k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--panikiel.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CBfyA11N; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--panikiel.bounces.google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4120faa1dacso3968965e9.2 for ; Wed, 21 Feb 2024 08:02:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708531358; x=1709136158; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=4+3Sm96T7vJUTpcS+4lNZ9LA/sEbsUcxo41ULm96MSg=; b=CBfyA11NM98Beh7cTJnwF+fOYuQTSfJ1mQFZ0kYInKQq7wWTr1lYn1QcAUoC2L8J9Z TcoVpTu3HvJ6weFyR+1bN95YnuNmgcmffF15cb9QIl/g3g6l4cqMzepdknXQqwHkFhbf crtPRu1nCbsenBCgTwChQDcu4xq0vnpk3CnglF0bsUC2uUQcz1/4uh7PZJRAyixUTrCL cIIe/0h+9Ki6MoyD0OdxUK+00luJBmMd0KhRal/8wJf3a6z86TStKxK+5WPpjmBAyR8g CWUAIiZUiuCiIqDVpw7a9/neSIVuQkbHXxzCw8WzUNjGBonkhf7UnWpVACOVOgbh+4Sj WUrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708531358; x=1709136158; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=4+3Sm96T7vJUTpcS+4lNZ9LA/sEbsUcxo41ULm96MSg=; b=Ug21LjN7pKeJcaNjRnqwNtXBrQWN5OVhD/TpJSSjZ+zoWq6U2FmjZzj6Jk7X9329Dx xH+/N5AQh6Xg14TeycKmkRQUji5Wz/4jbU1B3UIqn9BhsiTGs/1w5JKVOqtiSLytsxim Trdm/oBw3MZmYx+g0sFUUvy39JA9fa2CsYmdeBka8zaE6VTQVQC/BL3Ntie3LRv4O/vX Sbbg1zy3JWVvV5WR70PC1qOXgnc7cPwS1Sj5d1ejZ7KFvFz3sdNWJSwlfqz8DyCC/y+e FdH6feB0UINVMT4JCkVrx1JjNfYctKPG/+KTXM6Uu6FKalI5/k3k5R6QtOZG52f04rVm ziiA== X-Forwarded-Encrypted: i=1; AJvYcCX8Rhs56MTA037ka1JYoSMgNuSi+goaK9G3wblCwpRLSrnb1nfRpVC+4uVNkLcxlrDh0SbtLLxl4ctxMkbpNMcNsw8N77qy9B+PzRTr X-Gm-Message-State: AOJu0YwK5VWC0fshKSWq4CkV0WbHsWtInh6nh6sYgQZbE4kwN5/qc5hd bqjQGLRnBOId0QZWEMRKW/W/xP/8bgMERXh/38hq1WF71cSLeNt4K4ZFIqEbcvVoWSERW1P4LEN gSK1+btKTBg== X-Received: from szatan.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2d83]) (user=panikiel job=sendgmr) by 2002:a05:600c:a:b0:411:ea5e:6142 with SMTP id g10-20020a05600c000a00b00411ea5e6142mr128794wmc.2.1708531358714; Wed, 21 Feb 2024 08:02:38 -0800 (PST) Date: Wed, 21 Feb 2024 16:02:10 +0000 In-Reply-To: <20240221160215.484151-1-panikiel@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221160215.484151-1-panikiel@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240221160215.484151-5-panikiel@google.com> Subject: [PATCH v2 4/9] lib: Move DisplayPort CRC functions to common lib From: "=?UTF-8?q?Pawe=C5=82=20Anikiel?=" To: airlied@gmail.com, akpm@linux-foundation.org, conor+dt@kernel.org, daniel@ffwll.ch, dinguyen@kernel.org, hverkuil-cisco@xs4all.nl, krzysztof.kozlowski+dt@linaro.org, maarten.lankhorst@linux.intel.com, mchehab@kernel.org, mripard@kernel.org, robh+dt@kernel.org, tzimmermann@suse.de Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, chromeos-krk-upstreaming@google.com, ribalda@chromium.org, "=?UTF-8?q?Pawe=C5=82=20Anikiel?=" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable The CRC functions found in drivers/gpu/drm/display/drm_dp_mst_topology.c may be useful for other non-DRM code that deals with DisplayPort, e.g. v4l2 drivers for DP receivers. Move these functions to /lib. Signed-off-by: Pawe=C5=82 Anikiel --- drivers/gpu/drm/display/Kconfig | 1 + drivers/gpu/drm/display/drm_dp_mst_topology.c | 76 ++---------------- include/linux/crc-dp.h | 10 +++ lib/Kconfig | 8 ++ lib/Makefile | 1 + lib/crc-dp.c | 78 +++++++++++++++++++ 6 files changed, 103 insertions(+), 71 deletions(-) create mode 100644 include/linux/crc-dp.h create mode 100644 lib/crc-dp.c diff --git a/drivers/gpu/drm/display/Kconfig b/drivers/gpu/drm/display/Kcon= fig index 09712b88a5b8..c615f50152f2 100644 --- a/drivers/gpu/drm/display/Kconfig +++ b/drivers/gpu/drm/display/Kconfig @@ -14,6 +14,7 @@ config DRM_DISPLAY_HELPER config DRM_DISPLAY_DP_HELPER bool depends on DRM_DISPLAY_HELPER + select CRC_DP help DRM display helpers for DisplayPort. =20 diff --git a/drivers/gpu/drm/display/drm_dp_mst_topology.c b/drivers/gpu/dr= m/display/drm_dp_mst_topology.c index f7c6b60629c2..ada1f90fa808 100644 --- a/drivers/gpu/drm/display/drm_dp_mst_topology.c +++ b/drivers/gpu/drm/display/drm_dp_mst_topology.c @@ -22,6 +22,7 @@ =20 #include #include +#include #include #include #include @@ -195,73 +196,6 @@ drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *o= ut, size_t len) } =20 /* sideband msg handling */ -static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles) -{ - u8 bitmask =3D 0x80; - u8 bitshift =3D 7; - u8 array_index =3D 0; - int number_of_bits =3D num_nibbles * 4; - u8 remainder =3D 0; - - while (number_of_bits !=3D 0) { - number_of_bits--; - remainder <<=3D 1; - remainder |=3D (data[array_index] & bitmask) >> bitshift; - bitmask >>=3D 1; - bitshift--; - if (bitmask =3D=3D 0) { - bitmask =3D 0x80; - bitshift =3D 7; - array_index++; - } - if ((remainder & 0x10) =3D=3D 0x10) - remainder ^=3D 0x13; - } - - number_of_bits =3D 4; - while (number_of_bits !=3D 0) { - number_of_bits--; - remainder <<=3D 1; - if ((remainder & 0x10) !=3D 0) - remainder ^=3D 0x13; - } - - return remainder; -} - -static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes) -{ - u8 bitmask =3D 0x80; - u8 bitshift =3D 7; - u8 array_index =3D 0; - int number_of_bits =3D number_of_bytes * 8; - u16 remainder =3D 0; - - while (number_of_bits !=3D 0) { - number_of_bits--; - remainder <<=3D 1; - remainder |=3D (data[array_index] & bitmask) >> bitshift; - bitmask >>=3D 1; - bitshift--; - if (bitmask =3D=3D 0) { - bitmask =3D 0x80; - bitshift =3D 7; - array_index++; - } - if ((remainder & 0x100) =3D=3D 0x100) - remainder ^=3D 0xd5; - } - - number_of_bits =3D 8; - while (number_of_bits !=3D 0) { - number_of_bits--; - remainder <<=3D 1; - if ((remainder & 0x100) !=3D 0) - remainder ^=3D 0xd5; - } - - return remainder & 0xff; -} static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *h= dr) { u8 size =3D 3; @@ -284,7 +218,7 @@ static void drm_dp_encode_sideband_msg_hdr(struct drm_d= p_sideband_msg_hdr *hdr, (hdr->msg_len & 0x3f); buf[idx++] =3D (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4); =20 - crc4 =3D drm_dp_msg_header_crc4(buf, (idx * 2) - 1); + crc4 =3D crc_dp_msg_header(buf, (idx * 2) - 1); buf[idx - 1] |=3D (crc4 & 0xf); =20 *len =3D idx; @@ -305,7 +239,7 @@ static bool drm_dp_decode_sideband_msg_hdr(const struct= drm_dp_mst_topology_mgr len +=3D ((buf[0] & 0xf0) >> 4) / 2; if (len > buflen) return false; - crc4 =3D drm_dp_msg_header_crc4(buf, (len * 2) - 1); + crc4 =3D crc_dp_msg_header(buf, (len * 2) - 1); =20 if ((crc4 & 0xf) !=3D (buf[len - 1] & 0xf)) { drm_dbg_kms(mgr->dev, "crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]); @@ -725,7 +659,7 @@ static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 l= en) { u8 crc4; =20 - crc4 =3D drm_dp_msg_data_crc4(msg, len); + crc4 =3D crc_dp_msg_data(msg, len); msg[len] =3D crc4; } =20 @@ -782,7 +716,7 @@ static bool drm_dp_sideband_append_payload(struct drm_d= p_sideband_msg_rx *msg, =20 if (msg->curchunk_idx >=3D msg->curchunk_len) { /* do CRC */ - crc4 =3D drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1); + crc4 =3D crc_dp_msg_data(msg->chunk, msg->curchunk_len - 1); if (crc4 !=3D msg->chunk[msg->curchunk_len - 1]) print_hex_dump(KERN_DEBUG, "wrong crc", DUMP_PREFIX_NONE, 16, 1, diff --git a/include/linux/crc-dp.h b/include/linux/crc-dp.h new file mode 100644 index 000000000000..b63435c82b96 --- /dev/null +++ b/include/linux/crc-dp.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_CRC_DP_H +#define _LINUX_CRC_DP_H + +#include + +u8 crc_dp_msg_header(const uint8_t *data, size_t num_nibbles); +u8 crc_dp_msg_data(const uint8_t *data, u8 number_of_bytes); + +#endif /* _LINUX_CRC_DP_H */ diff --git a/lib/Kconfig b/lib/Kconfig index 5ddda7c2ed9b..28f9f6cfec9f 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -171,6 +171,14 @@ config CRC_ITU_T the kernel tree does. Such modules that use library CRC ITU-T V.41 functions require M here. =20 +config CRC_DP + tristate "CRC DisplayPort MST functions" + help + This option is provided for the case where no in-kernel-tree + modules require CRC DisplayPort MST functions, but a module built outsi= de + the kernel tree does. Such modules that use library CRC DisplayPort MST + functions require M here. + config CRC32 tristate "CRC32/CRC32c functions" default y diff --git a/lib/Makefile b/lib/Makefile index 6b09731d8e61..e4d7ffa260b3 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -188,6 +188,7 @@ obj-$(CONFIG_CRC7) +=3D crc7.o obj-$(CONFIG_LIBCRC32C) +=3D libcrc32c.o obj-$(CONFIG_CRC8) +=3D crc8.o obj-$(CONFIG_CRC64_ROCKSOFT) +=3D crc64-rocksoft.o +obj-$(CONFIG_CRC_DP) +=3D crc-dp.o obj-$(CONFIG_XXHASH) +=3D xxhash.o obj-$(CONFIG_GENERIC_ALLOCATOR) +=3D genalloc.o =20 diff --git a/lib/crc-dp.c b/lib/crc-dp.c new file mode 100644 index 000000000000..95b58bc436d4 --- /dev/null +++ b/lib/crc-dp.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +/* + * Sideband MSG Header CRC + * Defined in DisplayPort 1.2 spec, section 2.11.3.1.9 + */ +u8 crc_dp_msg_header(const uint8_t *data, size_t num_nibbles) +{ + u8 bitmask =3D 0x80; + u8 bitshift =3D 7; + u8 array_index =3D 0; + int number_of_bits =3D num_nibbles * 4; + u8 remainder =3D 0; + + while (number_of_bits !=3D 0) { + number_of_bits--; + remainder <<=3D 1; + remainder |=3D (data[array_index] & bitmask) >> bitshift; + bitmask >>=3D 1; + bitshift--; + if (bitmask =3D=3D 0) { + bitmask =3D 0x80; + bitshift =3D 7; + array_index++; + } + if ((remainder & 0x10) =3D=3D 0x10) + remainder ^=3D 0x13; + } + + number_of_bits =3D 4; + while (number_of_bits !=3D 0) { + number_of_bits--; + remainder <<=3D 1; + if ((remainder & 0x10) !=3D 0) + remainder ^=3D 0x13; + } + + return remainder; +} + +/* + * Sideband MSG Data CRC + * Defined in DisplayPort 1.2 spec, section 2.11.3.2.2 + */ +u8 crc_dp_msg_data(const uint8_t *data, u8 number_of_bytes) +{ + u8 bitmask =3D 0x80; + u8 bitshift =3D 7; + u8 array_index =3D 0; + int number_of_bits =3D number_of_bytes * 8; + u16 remainder =3D 0; + + while (number_of_bits !=3D 0) { + number_of_bits--; + remainder <<=3D 1; + remainder |=3D (data[array_index] & bitmask) >> bitshift; + bitmask >>=3D 1; + bitshift--; + if (bitmask =3D=3D 0) { + bitmask =3D 0x80; + bitshift =3D 7; + array_index++; + } + if ((remainder & 0x100) =3D=3D 0x100) + remainder ^=3D 0xd5; + } + + number_of_bits =3D 8; + while (number_of_bits !=3D 0) { + number_of_bits--; + remainder <<=3D 1; + if ((remainder & 0x100) !=3D 0) + remainder ^=3D 0xd5; + } + + return remainder & 0xff; +} --=20 2.44.0.rc0.258.g7320e95886-goog