xref: /freebsd/sys/dev/virtio/virtio_endian.h (revision db494ceb)
19da9560cSBryan Venteicher /*-
29da9560cSBryan Venteicher  * SPDX-License-Identifier: BSD-2-Clause
39da9560cSBryan Venteicher  *
49da9560cSBryan Venteicher  * Copyright (c) 2017, Bryan Venteicher <bryanv@FreeBSD.org>
59da9560cSBryan Venteicher  * All rights reserved.
69da9560cSBryan Venteicher  *
79da9560cSBryan Venteicher  * Redistribution and use in source and binary forms, with or without
89da9560cSBryan Venteicher  * modification, are permitted provided that the following conditions
99da9560cSBryan Venteicher  * are met:
109da9560cSBryan Venteicher  * 1. Redistributions of source code must retain the above copyright
119da9560cSBryan Venteicher  *    notice unmodified, this list of conditions, and the following
129da9560cSBryan Venteicher  *    disclaimer.
139da9560cSBryan Venteicher  * 2. Redistributions in binary form must reproduce the above copyright
149da9560cSBryan Venteicher  *    notice, this list of conditions and the following disclaimer in the
159da9560cSBryan Venteicher  *    documentation and/or other materials provided with the distribution.
169da9560cSBryan Venteicher  *
179da9560cSBryan Venteicher  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189da9560cSBryan Venteicher  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199da9560cSBryan Venteicher  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209da9560cSBryan Venteicher  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219da9560cSBryan Venteicher  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229da9560cSBryan Venteicher  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239da9560cSBryan Venteicher  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249da9560cSBryan Venteicher  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259da9560cSBryan Venteicher  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269da9560cSBryan Venteicher  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279da9560cSBryan Venteicher  *
289da9560cSBryan Venteicher  * $FreeBSD$
299da9560cSBryan Venteicher  */
309da9560cSBryan Venteicher 
319da9560cSBryan Venteicher #ifndef _VIRTIO_ENDIAN_H_
329da9560cSBryan Venteicher #define _VIRTIO_ENDIAN_H_
339da9560cSBryan Venteicher 
349da9560cSBryan Venteicher #include <sys/endian.h>
3554ac6f72SKa Ho Ng #ifndef _KERNEL
3654ac6f72SKa Ho Ng #include <stdbool.h>
3754ac6f72SKa Ho Ng #endif /* _KERNEL */
389da9560cSBryan Venteicher 
399da9560cSBryan Venteicher /*
409da9560cSBryan Venteicher  * VirtIO V1 (modern) uses little endian, while legacy VirtIO uses the guest's
419da9560cSBryan Venteicher  * native endian. These functions convert to and from the Guest's (driver's)
429da9560cSBryan Venteicher  * and the Host's (device's) endianness when needed.
439da9560cSBryan Venteicher  */
449da9560cSBryan Venteicher 
459da9560cSBryan Venteicher static inline uint16_t
469da9560cSBryan Venteicher virtio_htog16(bool modern, uint16_t val)
479da9560cSBryan Venteicher {
48db494cebSMark Johnston 	if (modern)
499da9560cSBryan Venteicher 		return (le16toh(val));
509da9560cSBryan Venteicher 	else
519da9560cSBryan Venteicher 		return (val);
529da9560cSBryan Venteicher }
539da9560cSBryan Venteicher 
549da9560cSBryan Venteicher static inline uint16_t
559da9560cSBryan Venteicher virtio_gtoh16(bool modern, uint16_t val)
569da9560cSBryan Venteicher {
57db494cebSMark Johnston 	if (modern)
589da9560cSBryan Venteicher 		return (htole16(val));
599da9560cSBryan Venteicher 	else
609da9560cSBryan Venteicher 		return (val);
619da9560cSBryan Venteicher }
629da9560cSBryan Venteicher 
639da9560cSBryan Venteicher static inline uint32_t
649da9560cSBryan Venteicher virtio_htog32(bool modern, uint32_t val)
659da9560cSBryan Venteicher {
66db494cebSMark Johnston 	if (modern)
679da9560cSBryan Venteicher 		return (le32toh(val));
689da9560cSBryan Venteicher 	else
699da9560cSBryan Venteicher 		return (val);
709da9560cSBryan Venteicher }
719da9560cSBryan Venteicher 
729da9560cSBryan Venteicher static inline uint32_t
739da9560cSBryan Venteicher virtio_gtoh32(bool modern, uint32_t val)
749da9560cSBryan Venteicher {
75db494cebSMark Johnston 	if (modern)
769da9560cSBryan Venteicher 		return (htole32(val));
779da9560cSBryan Venteicher 	else
789da9560cSBryan Venteicher 		return (val);
799da9560cSBryan Venteicher }
809da9560cSBryan Venteicher 
819da9560cSBryan Venteicher static inline uint64_t
829da9560cSBryan Venteicher virtio_htog64(bool modern, uint64_t val)
839da9560cSBryan Venteicher {
84db494cebSMark Johnston 	if (modern)
859da9560cSBryan Venteicher 		return (le64toh(val));
869da9560cSBryan Venteicher 	else
879da9560cSBryan Venteicher 		return (val);
889da9560cSBryan Venteicher }
899da9560cSBryan Venteicher 
909da9560cSBryan Venteicher static inline uint64_t
919da9560cSBryan Venteicher virtio_gtoh64(bool modern, uint64_t val)
929da9560cSBryan Venteicher {
93db494cebSMark Johnston 	if (modern)
949da9560cSBryan Venteicher 		return (htole64(val));
959da9560cSBryan Venteicher 	else
969da9560cSBryan Venteicher 		return (val);
979da9560cSBryan Venteicher }
989da9560cSBryan Venteicher 
999da9560cSBryan Venteicher #endif /* _VIRTIO_ENDIAN_H_ */
100