From patchwork Wed Feb 21 16:02:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Pawe=C5=82_Anikiel?= X-Patchwork-Id: 204286 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1131311dyc; Wed, 21 Feb 2024 08:04:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWjuHhqpBdD50vhEstHSEHvlHrkO3MUK5xCUCLPaOuGv5diZR2PiYO9oPoJ6dwm7InhpQZgqEcGlSyRQVKjPe5QygJkEA== X-Google-Smtp-Source: AGHT+IEl3v1MyruombhfQh8EbQZdQlE9P6NXqJUKQv6wlAOJfpDZMk9yu3gf8p9ANI5vkkE18AK/ X-Received: by 2002:a6b:e30d:0:b0:7c7:6a40:5492 with SMTP id u13-20020a6be30d000000b007c76a405492mr4878991ioc.4.1708531495657; Wed, 21 Feb 2024 08:04:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708531495; cv=pass; d=google.com; s=arc-20160816; b=bBr4LJg/tgPTDNNIM8AyLKIkmFy+Hph8e9Z1wRdNieRZkAaSlc/tpuyL/57j1iyXJT Na8QxKXOaBJdwrKGr7H4M3zNv60tem18iYtBZ4WfenPDLDryqZsxk5gGsR9QA6M9iunD i05/dIdX9kaSAFLTl3lSNcWwFYgrqhHp3pjPisUG+CJ6emZDUhHNXFrtsAP1CaplODB/ VsshoboDmuD94aWLufxddPc9ZbdkOZhDyrNQykCyEOjKjJlDbEYWFezQrqjVL22nTWxX nPnkLo3SbvLhOYd/Le77BzuI/Z+4jeI8a9aKMXjvh6Y79k7TurwnikekaPPfNZxx9y9k ey7g== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75066-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 4-20020a6b1504000000b007c484d0304bsi3204195iov.83.2024.02.21.08.04.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 08:04:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75066-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75066-ouuuleilei=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 2EC632873F6 for ; Wed, 21 Feb 2024 16:04:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D6DE83CCD; Wed, 21 Feb 2024 16:02:46 +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_Anikiel?= " 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?= =?utf-8?q?=C5=82_Anikiel?= " X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791525121782610319 X-GMAIL-MSGID: 1791525121782610319 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ł 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/Kconfig 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. diff --git a/drivers/gpu/drm/display/drm_dp_mst_topology.c b/drivers/gpu/drm/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 @@ #include #include +#include #include #include #include @@ -195,73 +196,6 @@ drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len) } /* sideband msg handling */ -static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles) -{ - u8 bitmask = 0x80; - u8 bitshift = 7; - u8 array_index = 0; - int number_of_bits = num_nibbles * 4; - u8 remainder = 0; - - while (number_of_bits != 0) { - number_of_bits--; - remainder <<= 1; - remainder |= (data[array_index] & bitmask) >> bitshift; - bitmask >>= 1; - bitshift--; - if (bitmask == 0) { - bitmask = 0x80; - bitshift = 7; - array_index++; - } - if ((remainder & 0x10) == 0x10) - remainder ^= 0x13; - } - - number_of_bits = 4; - while (number_of_bits != 0) { - number_of_bits--; - remainder <<= 1; - if ((remainder & 0x10) != 0) - remainder ^= 0x13; - } - - return remainder; -} - -static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes) -{ - u8 bitmask = 0x80; - u8 bitshift = 7; - u8 array_index = 0; - int number_of_bits = number_of_bytes * 8; - u16 remainder = 0; - - while (number_of_bits != 0) { - number_of_bits--; - remainder <<= 1; - remainder |= (data[array_index] & bitmask) >> bitshift; - bitmask >>= 1; - bitshift--; - if (bitmask == 0) { - bitmask = 0x80; - bitshift = 7; - array_index++; - } - if ((remainder & 0x100) == 0x100) - remainder ^= 0xd5; - } - - number_of_bits = 8; - while (number_of_bits != 0) { - number_of_bits--; - remainder <<= 1; - if ((remainder & 0x100) != 0) - remainder ^= 0xd5; - } - - return remainder & 0xff; -} static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr) { u8 size = 3; @@ -284,7 +218,7 @@ static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr, (hdr->msg_len & 0x3f); buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4); - crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1); + crc4 = crc_dp_msg_header(buf, (idx * 2) - 1); buf[idx - 1] |= (crc4 & 0xf); *len = idx; @@ -305,7 +239,7 @@ static bool drm_dp_decode_sideband_msg_hdr(const struct drm_dp_mst_topology_mgr len += ((buf[0] & 0xf0) >> 4) / 2; if (len > buflen) return false; - crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1); + crc4 = crc_dp_msg_header(buf, (len * 2) - 1); if ((crc4 & 0xf) != (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 len) { u8 crc4; - crc4 = drm_dp_msg_data_crc4(msg, len); + crc4 = crc_dp_msg_data(msg, len); msg[len] = crc4; } @@ -782,7 +716,7 @@ static bool drm_dp_sideband_append_payload(struct drm_dp_sideband_msg_rx *msg, if (msg->curchunk_idx >= msg->curchunk_len) { /* do CRC */ - crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1); + crc4 = crc_dp_msg_data(msg->chunk, msg->curchunk_len - 1); if (crc4 != 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. +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 outside + 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) += crc7.o obj-$(CONFIG_LIBCRC32C) += libcrc32c.o obj-$(CONFIG_CRC8) += crc8.o obj-$(CONFIG_CRC64_ROCKSOFT) += crc64-rocksoft.o +obj-$(CONFIG_CRC_DP) += crc-dp.o obj-$(CONFIG_XXHASH) += xxhash.o obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o 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 = 0x80; + u8 bitshift = 7; + u8 array_index = 0; + int number_of_bits = num_nibbles * 4; + u8 remainder = 0; + + while (number_of_bits != 0) { + number_of_bits--; + remainder <<= 1; + remainder |= (data[array_index] & bitmask) >> bitshift; + bitmask >>= 1; + bitshift--; + if (bitmask == 0) { + bitmask = 0x80; + bitshift = 7; + array_index++; + } + if ((remainder & 0x10) == 0x10) + remainder ^= 0x13; + } + + number_of_bits = 4; + while (number_of_bits != 0) { + number_of_bits--; + remainder <<= 1; + if ((remainder & 0x10) != 0) + remainder ^= 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 = 0x80; + u8 bitshift = 7; + u8 array_index = 0; + int number_of_bits = number_of_bytes * 8; + u16 remainder = 0; + + while (number_of_bits != 0) { + number_of_bits--; + remainder <<= 1; + remainder |= (data[array_index] & bitmask) >> bitshift; + bitmask >>= 1; + bitshift--; + if (bitmask == 0) { + bitmask = 0x80; + bitshift = 7; + array_index++; + } + if ((remainder & 0x100) == 0x100) + remainder ^= 0xd5; + } + + number_of_bits = 8; + while (number_of_bits != 0) { + number_of_bits--; + remainder <<= 1; + if ((remainder & 0x100) != 0) + remainder ^= 0xd5; + } + + return remainder & 0xff; +}