a3889c205f677532188f556be3af9c8f79dc0de2
[pintos-anon] / src / threads / io.h
1 /* This file is derived from source code used in MIT's 6.828
2    course.  The original copyright notice is reproduced in full
3    below. */
4
5 /*
6  * Copyright (C) 1997 Massachusetts Institute of Technology 
7  *
8  * This software is being provided by the copyright holders under the
9  * following license. By obtaining, using and/or copying this software,
10  * you agree that you have read, understood, and will comply with the
11  * following terms and conditions:
12  *
13  * Permission to use, copy, modify, distribute, and sell this software
14  * and its documentation for any purpose and without fee or royalty is
15  * hereby granted, provided that the full text of this NOTICE appears on
16  * ALL copies of the software and documentation or portions thereof,
17  * including modifications, that you make.
18  *
19  * THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
20  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
21  * BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
22  * WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
23  * THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
24  * THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
25  * HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
26  * DOCUMENTATION.
27  *
28  * The name and trademarks of copyright holders may NOT be used in
29  * advertising or publicity pertaining to the software without specific,
30  * written prior permission. Title to copyright in this software and any
31  * associated documentation will at all times remain with copyright
32  * holders. See the file AUTHORS which should have accompanied this software
33  * for a list of all copyright holders.
34  *
35  * This file may be derived from previously copyrighted software. This
36  * copyright applies only to those changes made by the copyright
37  * holders listed in the AUTHORS file. The rest of this file is covered by
38  * the copyright notices, if any, listed below.
39  */
40
41 #ifndef THREADS_IO_H
42 #define THREADS_IO_H
43
44 #include <stddef.h>
45 #include <stdint.h>
46
47 /* Reads and returns a byte from PORT. */
48 static inline uint8_t
49 inb (uint16_t port)
50 {
51   uint8_t data;
52   asm volatile ("inb %w1,%0" : "=a" (data) : "d" (port));
53   return data;
54 }
55
56 /* Reads CNT bytes from PORT, one after another, and stores them
57    into the buffer starting at ADDR. */
58 static inline void
59 insb (uint16_t port, void *addr, size_t cnt)
60 {
61   asm volatile ("cld; repne; insb"
62                 : "=D" (addr), "=c" (cnt)
63                 : "d" (port), "0" (addr), "1" (cnt)
64                 : "memory", "cc");
65 }
66
67 /* Reads and returns 16 bits from PORT. */
68 static inline uint16_t
69 inw (uint16_t port)
70 {
71   uint16_t data;
72   asm volatile ("inw %w1,%0" : "=a" (data) : "d" (port));
73   return data;
74 }
75
76 /* Reads CNT 16-bit (halfword) units from PORT, one after
77    another, and stores them into the buffer starting at ADDR. */
78 static inline void
79 insw (uint16_t port, void *addr, size_t cnt)
80 {
81   asm volatile ("cld; repne; insw"
82                 : "=D" (addr), "=c" (cnt)
83                 : "d" (port), "0" (addr), "1" (cnt)
84                 : "memory", "cc");
85 }
86
87 /* Reads and returns 32 bits from PORT. */
88 static inline uint32_t
89 inl (uint16_t port)
90 {
91   uint32_t data;
92   asm volatile ("inl %w1,%0" : "=a" (data) : "d" (port));
93   return data;
94 }
95
96 /* Reads CNT 32-bit (word) units from PORT, one after another,
97    and stores them into the buffer starting at ADDR. */
98 static inline void
99 insl (uint16_t port, void *addr, size_t cnt)
100 {
101   asm volatile ("cld; repne; insl"
102                 : "=D" (addr), "=c" (cnt)
103                 : "d" (port), "0" (addr), "1" (cnt)
104                 : "memory", "cc");
105 }
106
107 /* Writes byte DATA to PORT. */
108 static inline void
109 outb (uint16_t port, uint8_t data)
110 {
111   asm volatile ("outb %0,%w1" : : "a" (data), "d" (port));
112 }
113
114 /* Writes to PORT each byte of data in the CNT-byte buffer
115    starting at ADDR. */
116 static inline void
117 outsb (uint16_t port, const void *addr, size_t cnt)
118 {
119   asm volatile ("cld; repne; outsb"
120                 : "=S" (addr), "=c" (cnt)
121                 : "d" (port), "0" (addr), "1" (cnt)
122                 : "cc");
123 }
124
125 /* Writes the 16-bit DATA to PORT. */
126 static inline void
127 outw (uint16_t port, uint16_t data)
128 {
129   asm volatile ("outw %0,%w1" : : "a" (data), "d" (port));
130 }
131
132 /* Writes to PORT each 16-bit unit (halfword) of data in the
133    CNT-halfword buffer starting at ADDR. */
134 static inline void
135 outsw (uint16_t port, const void *addr, size_t cnt)
136 {
137   asm volatile ("cld; repne; outsw"
138                 : "=S" (addr), "=c" (cnt)
139                 : "d" (port), "0" (addr), "1" (cnt)
140                 : "cc");
141 }
142
143 /* Writes the 32-bit DATA to PORT. */
144 static inline void
145 outl (uint16_t port, uint32_t data)
146 {
147   asm volatile ("outl %0,%w1" : : "a" (data), "d" (port));
148 }
149
150 /* Writes to PORT each 32-bit unit (word) of data in the CNT-word
151    buffer starting at ADDR. */
152 static inline void
153 outsl (uint16_t port, const void *addr, size_t cnt)
154 {
155   asm volatile ("cld; repne; outsl"
156                 : "=S" (addr), "=c" (cnt)
157                 : "d" (port), "0" (addr), "1" (cnt)
158                 : "cc");
159 }
160
161 #endif /* threads/io.h */