Serenity Operating System
at hosted 183 lines 5.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/Assertions.h> 30#include <AK/LogStream.h> 31#include <AK/String.h> 32#include <AK/Types.h> 33 34#if defined(KERNEL) 35# include <Kernel/Arch/i386/CPU.h> 36#endif 37 38namespace IO { 39 40inline u8 in8(u16 port) 41{ 42 u8 value; 43 asm volatile("inb %1, %0" 44 : "=a"(value) 45 : "Nd"(port)); 46 return value; 47} 48 49inline u16 in16(u16 port) 50{ 51 u16 value; 52 asm volatile("inw %1, %0" 53 : "=a"(value) 54 : "Nd"(port)); 55 return value; 56} 57 58inline u32 in32(u16 port) 59{ 60 u32 value; 61 asm volatile("inl %1, %0" 62 : "=a"(value) 63 : "Nd"(port)); 64 return value; 65} 66 67inline void repeated_in16(u16 port, u8* buffer, int buffer_size) 68{ 69 asm volatile("rep insw" 70 : "+D"(buffer), "+c"(buffer_size) 71 : "d"(port) 72 : "memory"); 73} 74 75inline void out8(u16 port, u8 value) 76{ 77 asm volatile("outb %0, %1" ::"a"(value), "Nd"(port)); 78} 79 80inline void out16(u16 port, u16 value) 81{ 82 asm volatile("outw %0, %1" ::"a"(value), "Nd"(port)); 83} 84 85inline void out32(u16 port, u32 value) 86{ 87 asm volatile("outl %0, %1" ::"a"(value), "Nd"(port)); 88} 89 90inline void repeated_out16(u16 port, const u8* data, int data_size) 91{ 92 asm volatile("rep outsw" 93 : "+S"(data), "+c"(data_size) 94 : "d"(port)); 95} 96 97inline void delay() 98{ 99 // ~3 microsecs 100 for (auto i = 0; i < 32; i++) { 101 IO::in8(0x80); 102 } 103} 104 105} 106 107class IOAddress { 108public: 109 IOAddress() {} 110 explicit IOAddress(u16 address) 111 : m_address(address) 112 { 113 } 114 115 IOAddress offset(u16 o) const { return IOAddress(m_address + o); } 116 u16 get() const { return m_address; } 117 void set(u16 address) { m_address = address; } 118 void mask(u16 m) { m_address &= m; } 119 120 template<typename T> 121 [[gnu::always_inline]] inline T in() 122 { 123 if constexpr (sizeof(T) == 4) 124 return IO::in32(get()); 125 if constexpr (sizeof(T) == 2) 126 return IO::in16(get()); 127 if constexpr (sizeof(T) == 1) 128 return IO::in8(get()); 129 ASSERT_NOT_REACHED(); 130 } 131 132 template<typename T> 133 [[gnu::always_inline]] inline void out(T value) 134 { 135 if constexpr (sizeof(T) == 4) { 136 IO::out32(get(), value); 137 return; 138 } 139 if constexpr (sizeof(T) == 2) { 140 IO::out16(get(), value); 141 return; 142 } 143 if constexpr (sizeof(T) == 1) { 144 IO::out8(get(), value); 145 return; 146 } 147 ASSERT_NOT_REACHED(); 148 } 149 150 inline void out(u32 value, u8 bit_width) 151 { 152 if (bit_width == 32) { 153 IO::out32(get(), value); 154 return; 155 } 156 if (bit_width == 16) { 157 IO::out16(get(), value); 158 return; 159 } 160 if (bit_width == 8) { 161 IO::out8(get(), value); 162 return; 163 } 164 ASSERT_NOT_REACHED(); 165 } 166 167 bool is_null() const { return m_address == 0; } 168 169 bool operator==(const IOAddress& other) const { return m_address == other.m_address; } 170 bool operator!=(const IOAddress& other) const { return m_address != other.m_address; } 171 bool operator>(const IOAddress& other) const { return m_address > other.m_address; } 172 bool operator>=(const IOAddress& other) const { return m_address >= other.m_address; } 173 bool operator<(const IOAddress& other) const { return m_address < other.m_address; } 174 bool operator<=(const IOAddress& other) const { return m_address <= other.m_address; } 175 176private: 177 u16 m_address { 0 }; 178}; 179 180inline const LogStream& operator<<(const LogStream& stream, IOAddress value) 181{ 182 return stream << "IO " << String::format("%x", value.get()); 183}