From patchwork Fri Apr 13 17:42:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leif Lindholm X-Patchwork-Id: 133405 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp964077ljb; Fri, 13 Apr 2018 10:42:18 -0700 (PDT) X-Google-Smtp-Source: AIpwx48VOx8ZJ0ihXZMnCv7t4TdbgUE+XfLrVb5GR+xREPfPNFrau/EmbNReXAAiDIQVDdAyukKB X-Received: by 10.101.66.200 with SMTP id l8mr795942pgp.335.1523641338114; Fri, 13 Apr 2018 10:42:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523641338; cv=none; d=google.com; s=arc-20160816; b=Idc4T6aM8/YC2qjH+TH6T9nhc52/+SD+Y71j/ynTKyuig6TRlaurUPpKgDYpXzy+Jn AWp44ewGtMhM32vVuw05R+g8gQNhpMPHQLmJqJEwTTf2qfApV25X8jSzfCL69jFsPb2G wQog8c2OpeQXVx3ZjSes4iuWtfR1uGJ2mSdYd03SfEBaVW9tDO83drKjdufNU1FK4kTY P/3+nDIjMryYiuayqaM5g4jnhX1FYmY3DQAnvDWj74AbwhiUs/M6t1eJc8wYMR5zummP Bdpnu6qyqZXeZmzdA0ftDX1Ts4Mu80AT5W3VFkb2deeAdO0z0xzfDZXUFGjYj8NXbGuV 3VVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=byOdVUV4u/hPOIO7L1ySKZ7gCBa89LVuZjCDON6/8P4=; b=QW1sAOkTkPSj+ZUL4kjiCfonZrlTjOIe4Usuqy1b7BACv6ukKVEofBEuVCVPcfyWFt hXiLEZUOTcyW6V3r9eTOlteKqSwT3NnnWO4EQBRhKsJM9PDoqPIgR5UhEbI+HvvQ6Twp I4MKT/S3+uQdjcGgcdt1wiqFZ10eTaGyRLJ9DdsTtzG+kdmSKuoeobvHsDVe/sAJ9out ZVvBmIW9qD8H/3LkgontSfPR7bwWY1I/xcPxHugV+T1swjL2LTBFfG3TrAn2rUN9eXqR TbtsLYYb3PkNigYuC/sawUE7mqekTjIuUqUKY/9mExru9yLgtHdujB1inIqKkM2bda+A cF1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=WPqVN7iE; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id u3-v6si1892298plj.338.2018.04.13.10.42.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Apr 2018 10:42:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=WPqVN7iE; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 43D5122729639; Fri, 13 Apr 2018 10:42:17 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c0c::22d; helo=mail-wr0-x22d.google.com; envelope-from=leif.lindholm@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wr0-x22d.google.com (mail-wr0-x22d.google.com [IPv6:2a00:1450:400c:c0c::22d]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 5911722729629 for ; Fri, 13 Apr 2018 10:42:16 -0700 (PDT) Received: by mail-wr0-x22d.google.com with SMTP id z73so10108272wrb.0 for ; Fri, 13 Apr 2018 10:42:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=dSbxKYNz5/rn9+qE/kI5b3VGVGrl3DA6iKTeWCb8YGM=; b=WPqVN7iE04azpW5mJynzm7G9WmehuPRrAG5g7FfAe3ANYw8IqtZrdEW7OWY9qQlshw 1l3JiK54WPTuz+nzUISQ3VFR1Aae0muRtnz8JE7H/7hwcR6h9MlKzxcVI/3ztieQMWeB bhk2dfhPqNI/6eRtzgU9IQ7IZzAphpLK6S2eU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=dSbxKYNz5/rn9+qE/kI5b3VGVGrl3DA6iKTeWCb8YGM=; b=B710kGhAA/2OPuyNAoLG3veQGWb7uOvKpAeXmjWhUY5mE5RSJkLnrtTTQyN3lPs6Uw xngsgqBXEpK78xS50Iw3RAhQq69zBH1aysqMaowhbDSP55us6KgP0aHAARmmcxouC4Ti UBfB/prQgaCNt2wbvC741msQkYPqwZb+C3ECd4M+tmRNXavtQwXGe1Z8gr4CwhwS967G NUSBRZt1vx3LSKgKRYNhjHKYOmjXzvCzBI0XdF/FV+GAm4jbaKDa8H39Cfk4ved7rmuJ lsMlLm2Xgxzj/z8zyjh1GcftJPDIbc/XMo09/d7SkFUu5CGwG1ih+p9r917AaKTpwoTT j4eA== X-Gm-Message-State: ALQs6tBJiYXxQ8ZP/wjFK2N6+GXaPZMOaq2t7y8z9oFpoOuJSv5ndjTL fkI76i8uBD2pE/Q12fs0W60RVe4Vpys= X-Received: by 10.223.158.201 with SMTP id b9mr4229253wrf.215.1523641333376; Fri, 13 Apr 2018 10:42:13 -0700 (PDT) Received: from vanye.hemma.eciton.net (cpc92316-cmbg19-2-0-cust118.5-4.cable.virginm.net. [82.12.0.119]) by smtp.gmail.com with ESMTPSA id 80sm2863450wmk.46.2018.04.13.10.42.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Apr 2018 10:42:12 -0700 (PDT) From: Leif Lindholm To: edk2-devel@lists.01.org Date: Fri, 13 Apr 2018 18:42:11 +0100 Message-Id: <20180413174211.858-1-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 Subject: [edk2] [PATCH] MdePkg: add big-endian MMIO BaseBeIoLib X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael D Kinney , Laszlo Ersek , Liming Gao MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" When performing MMIO to a destination of the opposite endianness to the executing processor, this library provides automatic byte order reversal on inputs and outputs. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Leif Lindholm --- Udit, many apologies for this dragging out - back-to-back conferences, holidays, and lots of catching up. This modified version introduces a single BeIoLib instance, backed by a source-file that could be used also for a hypothetical LeIoLib. There is no LeIoLib.h included though. While this is arguably overengineered, I do feel reasonably strongly that code should be named for what it does, not for how it is used, and doing it this way lets me follow that rule. I have not duplicated the .uni file together with the .inf, since this follows what is done in BaseIoLibIntrinsic. MdePkg/Include/Library/BeIoLib.h | 376 +++++++++++++++++++ MdePkg/Library/BaseIoLibSwap/BaseBeIoLib.inf | 48 +++ MdePkg/Library/BaseIoLibSwap/BaseIoLibSwap.uni | 23 ++ MdePkg/Library/BaseIoLibSwap/IoLibSwap.c | 477 +++++++++++++++++++++++++ MdePkg/MdePkg.dec | 3 + 5 files changed, 927 insertions(+) create mode 100644 MdePkg/Include/Library/BeIoLib.h create mode 100644 MdePkg/Library/BaseIoLibSwap/BaseBeIoLib.inf create mode 100644 MdePkg/Library/BaseIoLibSwap/BaseIoLibSwap.uni create mode 100644 MdePkg/Library/BaseIoLibSwap/IoLibSwap.c -- 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/MdePkg/Include/Library/BeIoLib.h b/MdePkg/Include/Library/BeIoLib.h new file mode 100644 index 0000000000..5b2dc1a8e1 --- /dev/null +++ b/MdePkg/Include/Library/BeIoLib.h @@ -0,0 +1,376 @@ +/** @file + Provide byte-swapping services to access MMIO registers. + +Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2017, AMD Incorporated. All rights reserved.
+Copyright (c) 2018, Linaro ltd. All rights reserved.
+ +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#ifndef __BE_IO_LIB_H__ +#define __BE_IO_LIB_H__ + +/** + Reads a 16-bit MMIO register of opposite endianness. + + Reads the 16-bit MMIO register specified by Address. + The 16-bit read value is returned in reversed byte order. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT16 +EFIAPI +BeMmioRead16 ( + IN UINTN Address + ); + +/** + Writes a 16-bit MMIO register of opposite endianness. + + Writes the 16-bit MMIO register specified by Address with the byte-reversed + version of the value specified by Value and returns the original Value. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + + @return Value. + +**/ +UINT16 +EFIAPI +BeMmioWrite16 ( + IN UINTN Address, + IN UINT16 Value + ); + +/** + Reads a 16-bit MMIO register of opposite endianness, performs a bitwise OR, + and writes the result back to the 16-bit MMIO register. + + Reads the 16-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise OR between the read result and the value specified + by OrData, byte-reverses the result, and writes the result to the 16-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param OrData The value to OR with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT16 +EFIAPI +BeMmioOr16 ( + IN UINTN Address, + IN UINT16 OrData + ); + +/** + Reads a 16-bit MMIO register of opposite endianness, performs a bitwise AND, + and writes the result back to the 16-bit MMIO register. + + Reads the 16-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, byte-reverses the result, and writes the result to the 16-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT16 +EFIAPI +BeMmioAnd16 ( + IN UINTN Address, + IN UINT16 AndData + ); + +/** + Reads a 16-bit MMIO register of opposite endianness, performs a bitwise AND + followed by a bitwise OR, and writes the result back to the 16-bit MMIO + register. + + Reads the 16-bit MMIO register specified by Address, byte reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, performs a bitwise OR between the result of the AND operation and + the value specified by OrData, byte-reverses the result, and writes the result + to the 16-bit MMIO register specified by Address. The pre-reversal value + written to the MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + @param OrData The value to OR with the result of the AND operation. + + @return The value written back to the MMIO register. + +**/ +UINT16 +EFIAPI +BeMmioAndThenOr16 ( + IN UINTN Address, + IN UINT16 AndData, + IN UINT16 OrData + ); + +/** + Reads a 32-bit MMIO register of opposite endianness. + + Reads the 32-bit MMIO register specified by Address. + The 32-bit read value is returned in reversed byte order. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT32 +EFIAPI +BeMmioRead32 ( + IN UINTN Address + ); + +/** + Writes a 32-bit MMIO register of opposite endianness. + + Writes the 32-bit MMIO register specified by Address with the byte-reversed + version of the value specified by Value and returns the original Value. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + + @return Value. + +**/ +UINT32 +EFIAPI +BeMmioWrite32 ( + IN UINTN Address, + IN UINT32 Value + ); + +/** + Reads a 32-bit MMIO register of opposite endianness, performs a bitwise OR, + and writes the result back to the 32-bit MMIO register. + + Reads the 32-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise OR between the read result and the value specified + by OrData, byte-reverses the result, and writes the result to the 32-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param OrData The value to OR with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT32 +EFIAPI +BeMmioOr32 ( + IN UINTN Address, + IN UINT32 OrData + ); + +/** + Reads a 32-bit MMIO register of opposite endianness, performs a bitwise AND, + and writes the result back to the 32-bit MMIO register. + + Reads the 32-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, byte-reverses the result, and writes the result to the 32-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT32 +EFIAPI +BeMmioAnd32 ( + IN UINTN Address, + IN UINT32 AndData + ); + +/** + Reads a 32-bit MMIO register of opposite endianness, performs a bitwise AND + followed by a bitwise OR, and writes the result back to the 32-bit MMIO + register. + + Reads the 32-bit MMIO register specified by Address, byte reverses the read + value, performs a bitwise AND between the read result and the value specified + by AndData, performs a bitwise OR between the result of the AND operation and + the value specified by OrData, byte-reverses the result, and writes the result + to the 32-bit MMIO register specified by Address. The pre-reversal value + written to the MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + @param OrData The value to OR with the result of the AND operation. + + @return The value written back to the MMIO register. + +**/ +UINT32 +EFIAPI +BeMmioAndThenOr32 ( + IN UINTN Address, + IN UINT32 AndData, + IN UINT32 OrData + ); + +/** + Reads a 64-bit MMIO register of opposite endianness. + + Reads the 64-bit MMIO register specified by Address. + The 64-bit read value is returned in reversed byte order. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT64 +EFIAPI +BeMmioRead64 ( + IN UINTN Address + ); + +/** + Writes a 64-bit MMIO register of opposite endianness. + + Writes the 64-bit MMIO register specified by Address with the byte-reversed + version of the value specified by Value and returns Value. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT64 +EFIAPI +BeMmioWrite64 ( + IN UINTN Address, + IN UINT64 Value + ); + +/** + Reads a 64-bit MMIO register of opposite endianness, performs a bitwise OR, + and writes the result back to the 64-bit MMIO register. + + Reads the 64-bit MMIO register specified by Address, byte reverses the read + result, performs a bitwise OR between the read result and the value specified + by OrData, byte-reverses the result, and writes the result to the 64-bit MMIO + register specified by Address. The pre-reversal value written to the + MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param OrData The value to OR with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT64 +EFIAPI +BeMmioOr64 ( + IN UINTN Address, + IN UINT64 OrData + ); + +/** + Reads a 64-bit MMIO register of opposite endianness, performs a bitwise AND, + and writes the result back to the 64-bit MMIO register. + + Reads the 64-bit MMIO register specified by Address, byte-reverses the read + value, performs a bitwise AND between the read result and the value specified + by AndData, byte-reverses the result, and writes the result to the 64-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT64 +EFIAPI +BeMmioAnd64 ( + IN UINTN Address, + IN UINT64 AndData + ); + +/** + Reads a 64-bit MMIO register of opposite endianness, performs a bitwise AND + followed by a bitwise OR, and writes the result back to the 64-bit MMIO + register. + + Reads the 64-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, performs a bitwise OR between the result of the AND operation and + the value specified by OrData, byte-reverses the result, and writes the result + to the 64-bit MMIO register specified by Address. The pre-reversal value + written to the MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + @param OrData The value to OR with the result of the AND operation. + + @return The value written back to the MMIO register. + +**/ +UINT64 +EFIAPI +BeMmioAndThenOr64 ( + IN UINTN Address, + IN UINT64 AndData, + IN UINT64 OrData + ); + +#endif diff --git a/MdePkg/Library/BaseIoLibSwap/BaseBeIoLib.inf b/MdePkg/Library/BaseIoLibSwap/BaseBeIoLib.inf new file mode 100644 index 0000000000..fbd68b9929 --- /dev/null +++ b/MdePkg/Library/BaseIoLibSwap/BaseBeIoLib.inf @@ -0,0 +1,48 @@ +## @file +# Byte swapping I/O Library. +# +# Byte swapping I/O Library for all architectures. Only MMIO supported. I/O +# accesses take place through the normal IoLib, but values read and written +# are byte-reversed to interact with peripherals of non-native endianness. +# +# Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
+# Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
+# Copyright (c) 2017, AMD Incorporated. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php. +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +## + +[Defines] + INF_VERSION = 0x0001001a + BASE_NAME = BaseBeIoLib + MODULE_UNI_FILE = BaseIoLibSwap.uni + FILE_GUID = 073c3fbd-ff0d-41b6-a209-1e42fd2a3bab + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = BeIoLib + + +# +# VALID_ARCHITECTURES = IA32 X64 EBC IPF ARM AARCH64 +# + +[Sources] + IoLibSwap.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + IoLib + +[BuildOptions] + GCC:*_*_*_CC_FLAGS = -D FUNCTION_PREFIX=Be + INTEL:*_*_*_CC_FLAGS = /D FUNCTION_PREFIX=Be + MSFT:*_*_*_CC_FLAGS = /D FUNCTION_PREFIX=Be diff --git a/MdePkg/Library/BaseIoLibSwap/BaseIoLibSwap.uni b/MdePkg/Library/BaseIoLibSwap/BaseIoLibSwap.uni new file mode 100644 index 0000000000..e35b4abef7 --- /dev/null +++ b/MdePkg/Library/BaseIoLibSwap/BaseIoLibSwap.uni @@ -0,0 +1,23 @@ +// /** @file +// Byte swapping I/O Library. +// +// Byte swapping I/O Library for all architectures. Only MMIO supported. I/O +// accesses take place through the normal IoLib, but values read and written +// are byte-reversed to interact with peripherals of non-native endianness. +// +// Copyright (c) 2018, Linaro ltd. All rights reserved.
+// +// This program and the accompanying materials +// are licensed and made available under the terms and conditions of the BSD License +// which accompanies this distribution. The full text of the license may be found at +// http://opensource.org/licenses/bsd-license.php. +// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Byte swapping I/O Library" + +#string STR_MODULE_DESCRIPTION #language en-US "Byte swapping I/O Library for all architectures. Only MMIO supported. I/O accesses take place through the normal IoLib, but values read and written are byte-reversed to interact with peripherals of non-native endianness." + diff --git a/MdePkg/Library/BaseIoLibSwap/IoLibSwap.c b/MdePkg/Library/BaseIoLibSwap/IoLibSwap.c new file mode 100644 index 0000000000..f4f49f72d2 --- /dev/null +++ b/MdePkg/Library/BaseIoLibSwap/IoLibSwap.c @@ -0,0 +1,477 @@ +/** @file + Provide byte-swapping services to access MMIO registers. + +Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2017, AMD Incorporated. All rights reserved.
+Copyright (c) 2018, Linaro ltd. All rights reserved.
+ +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include + +#define __CONCATENATE(a, b) a ## b +#define _CONCATENATE(a, b) __CONCATENATE(a, b) +#define ADD_PREFIX(name) _CONCATENATE (FUNCTION_PREFIX, name) + +/** + Reads a 16-bit MMIO register of opposite endianness. + + Reads the 16-bit MMIO register specified by Address. + The 16-bit read value is returned in reversed byte order. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT16 +EFIAPI +ADD_PREFIX (MmioRead16) ( + IN UINTN Address + ) +{ + return SwapBytes16 (MmioRead16 (Address)); +} + +/** + Writes a 16-bit MMIO register of opposite endianness. + + Writes the 16-bit MMIO register specified by Address with the byte-reversed + version of the value specified by Value and returns the original Value. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + + @return Value. + +**/ +UINT16 +EFIAPI +ADD_PREFIX (MmioWrite16) ( + IN UINTN Address, + IN UINT16 Value + ) +{ + (VOID) MmioWrite16 (Address, SwapBytes16 (Value)); + + return Value; +} + +/** + Reads a 16-bit MMIO register of opposite endianness, performs a bitwise OR, + and writes the result back to the 16-bit MMIO register. + + Reads the 16-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise OR between the read result and the value specified + by OrData, byte-reverses the result, and writes the result to the 16-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param OrData The value to OR with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT16 +EFIAPI +ADD_PREFIX (MmioOr16) ( + IN UINTN Address, + IN UINT16 OrData + ) +{ + UINT16 Value; + + Value = ADD_PREFIX (MmioRead16) (Address); + Value |= OrData; + + return ADD_PREFIX (MmioWrite16) (Address, Value); +} + +/** + Reads a 16-bit MMIO register of opposite endianness, performs a bitwise AND, + and writes the result back to the 16-bit MMIO register. + + Reads the 16-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, byte-reverses the result, and writes the result to the 16-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT16 +EFIAPI +ADD_PREFIX (MmioAnd16) ( + IN UINTN Address, + IN UINT16 AndData + ) +{ + UINT16 Value; + + Value = ADD_PREFIX (MmioRead16) (Address); + Value &= AndData; + + return ADD_PREFIX (MmioWrite16) (Address, Value); +} + +/** + Reads a 16-bit MMIO register of opposite endianness, performs a bitwise AND + followed by a bitwise OR, and writes the result back to the 16-bit MMIO + register. + + Reads the 16-bit MMIO register specified by Address, byte reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, performs a bitwise OR between the result of the AND operation and + the value specified by OrData, byte-reverses the result, and writes the result + to the 16-bit MMIO register specified by Address. The pre-reversal value + written to the MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + @param OrData The value to OR with the result of the AND operation. + + @return The value written back to the MMIO register. + +**/ +UINT16 +EFIAPI +ADD_PREFIX (MmioAndThenOr16) ( + IN UINTN Address, + IN UINT16 AndData, + IN UINT16 OrData + ) +{ + UINT16 Value; + + Value = ADD_PREFIX (MmioRead16) (Address); + Value &= AndData; + Value |= OrData; + + return ADD_PREFIX (MmioWrite16) (Address, Value); +} + +/** + Reads a 32-bit MMIO register of opposite endianness. + + Reads the 32-bit MMIO register specified by Address. + The 32-bit read value is returned in reversed byte order. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT32 +EFIAPI +ADD_PREFIX (MmioRead32) ( + IN UINTN Address + ) +{ + return SwapBytes32 (MmioRead32 (Address)); +} + +/** + Writes a 32-bit MMIO register of opposite endianness. + + Writes the 32-bit MMIO register specified by Address with the byte-reversed + version of the value specified by Value and returns the original Value. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + + @return Value. + +**/ +UINT32 +EFIAPI +ADD_PREFIX (MmioWrite32) ( + IN UINTN Address, + IN UINT32 Value + ) +{ + (VOID) MmioWrite32 (Address, SwapBytes32 (Value)); + + return Value; +} + +/** + Reads a 32-bit MMIO register of opposite endianness, performs a bitwise OR, + and writes the result back to the 32-bit MMIO register. + + Reads the 32-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise OR between the read result and the value specified + by OrData, byte-reverses the result, and writes the result to the 32-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param OrData The value to OR with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT32 +EFIAPI +ADD_PREFIX (MmioOr32) ( + IN UINTN Address, + IN UINT32 OrData + ) +{ + UINT32 Value; + + Value = ADD_PREFIX (MmioRead32) (Address); + Value |= OrData; + + return ADD_PREFIX (MmioWrite32) (Address, Value); +} + +/** + Reads a 32-bit MMIO register of opposite endianness, performs a bitwise AND, + and writes the result back to the 32-bit MMIO register. + + Reads the 32-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, byte-reverses the result, and writes the result to the 32-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT32 +EFIAPI +ADD_PREFIX (MmioAnd32) ( + IN UINTN Address, + IN UINT32 AndData + ) +{ + UINT32 Value; + + Value = ADD_PREFIX (MmioRead32) (Address); + Value &= AndData; + + return ADD_PREFIX (MmioWrite32) (Address, Value); +} + +/** + Reads a 32-bit MMIO register of opposite endianness, performs a bitwise AND + followed by a bitwise OR, and writes the result back to the 32-bit MMIO + register. + + Reads the 32-bit MMIO register specified by Address, byte reverses the read + value, performs a bitwise AND between the read result and the value specified + by AndData, performs a bitwise OR between the result of the AND operation and + the value specified by OrData, byte-reverses the result, and writes the result + to the 32-bit MMIO register specified by Address. The pre-reversal value + written to the MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + @param OrData The value to OR with the result of the AND operation. + + @return The value written back to the MMIO register. + +**/ +UINT32 +EFIAPI +ADD_PREFIX (MmioAndThenOr32) ( + IN UINTN Address, + IN UINT32 AndData, + IN UINT32 OrData + ) +{ + UINT32 Value; + + Value = ADD_PREFIX (MmioRead32) (Address); + Value &= AndData; + Value |= OrData; + + return ADD_PREFIX (MmioWrite32) (Address, Value); +} + +/** + Reads a 64-bit MMIO register of opposite endianness. + + Reads the 64-bit MMIO register specified by Address. + The 64-bit read value is returned in reversed byte order. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to read. + + @return The value read. + +**/ +UINT64 +EFIAPI +ADD_PREFIX (MmioRead64) ( + IN UINTN Address + ) +{ + return SwapBytes64 (MmioRead64 (Address)); +} + +/** + Writes a 64-bit MMIO register of opposite endianness. + + Writes the 64-bit MMIO register specified by Address with the byte-reversed + version of the value specified by Value and returns Value. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param Value The value to write to the MMIO register. + +**/ +UINT64 +EFIAPI +ADD_PREFIX (MmioWrite64) ( + IN UINTN Address, + IN UINT64 Value + ) +{ + (VOID) MmioWrite64 (Address, SwapBytes64 (Value)); + + return Value; +} + +/** + Reads a 64-bit MMIO register of opposite endianness, performs a bitwise OR, + and writes the result back to the 64-bit MMIO register. + + Reads the 64-bit MMIO register specified by Address, byte reverses the read + result, performs a bitwise OR between the read result and the value specified + by OrData, byte-reverses the result, and writes the result to the 64-bit MMIO + register specified by Address. The pre-reversal value written to the + MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param OrData The value to OR with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT64 +EFIAPI +ADD_PREFIX (MmioOr64) ( + IN UINTN Address, + IN UINT64 OrData + ) +{ + UINT64 Value; + + Value = ADD_PREFIX (MmioRead64) (Address); + Value |= OrData; + + return ADD_PREFIX (MmioWrite64) (Address, Value); +} + +/** + Reads a 64-bit MMIO register of opposite endianness, performs a bitwise AND, + and writes the result back to the 64-bit MMIO register. + + Reads the 64-bit MMIO register specified by Address, byte-reverses the read + value, performs a bitwise AND between the read result and the value specified + by AndData, byte-reverses the result, and writes the result to the 64-bit MMIO + register specified by Address. The pre-reversal value written to the MMIO + register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + + @return The value written back to the MMIO register. + +**/ +UINT64 +EFIAPI +ADD_PREFIX (MmioAnd64) ( + IN UINTN Address, + IN UINT64 AndData + ) +{ + UINT64 Value; + + Value = ADD_PREFIX (MmioRead64) (Address); + Value &= AndData; + + return ADD_PREFIX (MmioWrite64) (Address, Value); +} + +/** + Reads a 64-bit MMIO register of opposite endianness, performs a bitwise AND + followed by a bitwise OR, and writes the result back to the 64-bit MMIO + register. + + Reads the 64-bit MMIO register specified by Address, byte-reverses the read + result, performs a bitwise AND between the read result and the value specified + by AndData, performs a bitwise OR between the result of the AND operation and + the value specified by OrData, byte-reverses the result, and writes the result + to the 64-bit MMIO register specified by Address. The pre-reversal value + written to the MMIO register is returned. + This function must guarantee that all MMIO read and write + operations are serialized. + + @param Address The MMIO register to write. + @param AndData The value to AND with the read value from the MMIO register. + @param OrData The value to OR with the result of the AND operation. + + @return The value written back to the MMIO register. + +**/ +UINT64 +EFIAPI +ADD_PREFIX (MmioAndThenOr64) ( + IN UINTN Address, + IN UINT64 AndData, + IN UINT64 OrData + ) +{ + UINT64 Value; + + Value = ADD_PREFIX (MmioRead64) (Address); + Value &= AndData; + Value |= OrData; + + return ADD_PREFIX (MmioWrite64) (Address, Value); +} diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index 0e64f22f4a..ae7c8dfa11 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -160,6 +160,9 @@ [LibraryClasses] ## @libraryclass Provide services to access I/O Ports and MMIO registers. IoLib|Include/Library/IoLib.h + ## @libraryclass Provide big-endian services to access MMIO registers. + BeIoLib|Include/Library/BeIoLib.h + ## @libraryclass Provide services to create, get and update HSTI table in AIP protocol. HstiLib|Include/Library/HstiLib.h