]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseIoLibIntrinsic/IoLibIpf.c
Add alignment checking for IoLib functions to conform to MdeLib spec.
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibIpf.c
CommitLineData
e1f414b6 1/** @file\r
2 Common I/O Library routines.\r
3\r
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e1f414b6 13**/\r
14\r
15//\r
16// Include common header file for this module.\r
17//\r
f734a10a 18#include "BaseIoLibIntrinsicInternal.h"\r
e1f414b6 19\r
20#define MAP_PORT_BASE_TO_MEM(_Port) \\r
21 ((((_Port) & 0xfffc) << 10) | ((_Port) & 0x0fff))\r
22\r
8fd567c6 23/**\r
24 Translates I/O port address to memory address.\r
25\r
26 This function translates I/O port address to memory address by adding the 64MB\r
27 aligned I/O Port space to the I/O address.\r
28 If I/O Port space base is not 64MB aligned, then ASSERT (). \r
29\r
30 @param Port The I/O port to read.\r
31\r
32 @return The memory address.\r
33\r
34**/\r
35UINTN\r
36InternalGetMemoryMapAddress (\r
37 IN UINTN Port\r
38 )\r
39{\r
40 UINTN Address;\r
41 UINTN IoBlockBaseAddress;\r
42\r
43 Address = MAP_PORT_BASE_TO_MEM (Port);\r
44 IoBlockBaseAddress = PcdGet64(PcdIoBlockBaseAddressForIpf);\r
45\r
46 //\r
47 // Make sure that the I/O Port space base is 64MB aligned.\r
48 // \r
49 ASSERT ((IoBlockBaseAddress & 0x3ffffff) == 0);\r
50 Address += IoBlockBaseAddress;\r
51\r
52 return Address;\r
53}\r
54\r
e1f414b6 55/**\r
56 Reads a 8-bit I/O port.\r
57\r
58 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.\r
59 This function must guarantee that all I/O read and write operations are\r
60 serialized.\r
61\r
62 @param Port The I/O port to read.\r
63\r
64 @return The value read.\r
65\r
66**/\r
67UINT8\r
68EFIAPI\r
69IoRead8 (\r
70 IN UINT64 Port\r
71 )\r
72{\r
8fd567c6 73 return MmioRead8 (InternalGetMemoryMapAddress (Port));\r
e1f414b6 74}\r
75\r
76/**\r
77 Reads a 16-bit I/O port.\r
78\r
79 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.\r
80 This function must guarantee that all I/O read and write operations are\r
81 serialized.\r
82\r
83 @param Port The I/O port to read.\r
84\r
85 @return The value read.\r
86\r
87**/\r
88UINT16\r
89EFIAPI\r
90IoRead16 (\r
91 IN UINT64 Port\r
92 )\r
93{\r
8fd567c6 94 return MmioRead16 (InternalGetMemoryMapAddress (Port));\r
e1f414b6 95}\r
96\r
97/**\r
98 Reads a 32-bit I/O port.\r
99\r
100 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.\r
101 This function must guarantee that all I/O read and write operations are\r
102 serialized.\r
103\r
104 @param Port The I/O port to read.\r
105\r
106 @return The value read.\r
107\r
108**/\r
109UINT32\r
110EFIAPI\r
111IoRead32 (\r
112 IN UINT64 Port\r
113 )\r
114{\r
8fd567c6 115 return MmioRead32 (InternalGetMemoryMapAddress (Port));\r
e1f414b6 116}\r
117\r
118/**\r
119 Reads a 64-bit I/O port.\r
120\r
121 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.\r
122 This function must guarantee that all I/O read and write operations are\r
123 serialized.\r
124\r
125 If 64-bit I/O port operations are not supported, then ASSERT().\r
126\r
127 @param Port The I/O port to read.\r
128\r
129 @return The value read.\r
130\r
131**/\r
132UINT64\r
133EFIAPI\r
134IoRead64 (\r
135 IN UINTN Port\r
136 )\r
137{\r
138 ASSERT (FALSE);\r
139 return 0;\r
140}\r
141\r
142/**\r
143 Writes a 8-bit I/O port.\r
144\r
145 Writes the 8-bit I/O port specified by Port with the value specified by Value\r
146 and returns Value. This function must guarantee that all I/O read and write\r
147 operations are serialized.\r
148\r
149 @param Port The I/O port to write.\r
150 @param Value The value to write to the I/O port.\r
151\r
152 @return The value written the I/O port.\r
153\r
154**/\r
155UINT8\r
156EFIAPI\r
157IoWrite8 (\r
158 IN UINT64 Port,\r
159 IN UINT8 Data\r
160 )\r
161{\r
8fd567c6 162 return MmioWrite8 (InternalGetMemoryMapAddress (Port), Data);\r
e1f414b6 163}\r
164\r
165/**\r
166 Writes a 16-bit I/O port.\r
167\r
168 Writes the 16-bit I/O port specified by Port with the value specified by Value\r
169 and returns Value. This function must guarantee that all I/O read and write\r
170 operations are serialized.\r
171\r
172 @param Port The I/O port to write.\r
173 @param Value The value to write to the I/O port.\r
174\r
175 @return The value written the I/O port.\r
176\r
177**/\r
178UINT16\r
179EFIAPI\r
180IoWrite16 (\r
181 IN UINT64 Port,\r
182 IN UINT16 Data\r
183 )\r
184{\r
8fd567c6 185 return MmioWrite16 (InternalGetMemoryMapAddress (Port), Data);\r
e1f414b6 186}\r
187\r
188/**\r
189 Writes a 32-bit I/O port.\r
190\r
191 Writes the 32-bit I/O port specified by Port with the value specified by Value\r
192 and returns Value. This function must guarantee that all I/O read and write\r
193 operations are serialized.\r
194\r
195 @param Port The I/O port to write.\r
196 @param Value The value to write to the I/O port.\r
197\r
198 @return The value written the I/O port.\r
199\r
200**/\r
201UINT32\r
202EFIAPI\r
203IoWrite32 (\r
204 IN UINT64 Port,\r
205 IN UINT32 Data\r
206 )\r
207{\r
8fd567c6 208 return MmioWrite32 (InternalGetMemoryMapAddress (Port), Data);\r
e1f414b6 209}\r
210\r
211/**\r
212 Writes a 64-bit I/O port.\r
213\r
214 Writes the 64-bit I/O port specified by Port with the value specified by Value\r
215 and returns Value. This function must guarantee that all I/O read and write\r
216 operations are serialized.\r
217\r
218 If 64-bit I/O port operations are not supported, then ASSERT().\r
219\r
220 @param Port The I/O port to write.\r
221 @param Value The value to write to the I/O port.\r
222\r
223 @return The value written the I/O port.\r
224\r
225**/\r
226UINT64\r
227EFIAPI\r
228IoWrite64 (\r
229 IN UINTN Port,\r
230 IN UINT64 Value\r
231 )\r
232{\r
233 ASSERT (FALSE);\r
234 return 0;\r
235}\r
236\r
237/**\r
238 Reads a 8-bit MMIO register.\r
239\r
240 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is\r
241 returned. This function must guarantee that all MMIO read and write\r
242 operations are serialized.\r
243\r
244 @param Address The MMIO register to read.\r
245\r
246 @return The value read.\r
247\r
248**/\r
249UINT8\r
250EFIAPI\r
251MmioRead8 (\r
252 IN UINT64 Address\r
253 )\r
254{\r
255 UINT8 Data;\r
256\r
257 Address |= BIT63;\r
258\r
259 MemoryFence ();\r
260 Data = *((volatile UINT8 *) Address);\r
261 MemoryFence ();\r
262\r
263 return Data;\r
264}\r
265\r
266/**\r
267 Reads a 16-bit MMIO register.\r
268\r
269 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is\r
270 returned. This function must guarantee that all MMIO read and write\r
271 operations are serialized.\r
272\r
273 @param Address The MMIO register to read.\r
274\r
275 @return The value read.\r
276\r
277**/\r
278UINT16\r
279EFIAPI\r
280MmioRead16 (\r
281 IN UINT64 Address\r
282 )\r
283{\r
284 UINT16 Data;\r
285\r
8fd567c6 286 //\r
287 // Make sure that Address is 16-bit aligned.\r
288 // \r
289 ASSERT ((Address & 1) == 0);\r
290\r
e1f414b6 291 Address |= BIT63;\r
292\r
293 MemoryFence ();\r
294 Data = *((volatile UINT16 *) Address);\r
295 MemoryFence ();\r
296\r
297 return Data;\r
298}\r
299\r
300/**\r
301 Reads a 32-bit MMIO register.\r
302\r
303 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is\r
304 returned. This function must guarantee that all MMIO read and write\r
305 operations are serialized.\r
306\r
307 @param Address The MMIO register to read.\r
308\r
309 @return The value read.\r
310\r
311**/\r
312UINT32\r
313EFIAPI\r
314MmioRead32 (\r
315 IN UINT64 Address\r
316 )\r
317{\r
318 UINT32 Data;\r
319\r
8fd567c6 320 //\r
321 // Make sure that Address is 32-bit aligned.\r
322 // \r
323 ASSERT ((Address & 3) == 0);\r
324\r
e1f414b6 325 Address |= BIT63;\r
326\r
327 MemoryFence ();\r
328 Data = *((volatile UINT32 *) Address);\r
329 MemoryFence ();\r
330\r
331 return Data;\r
332}\r
333\r
334/**\r
335 Reads a 64-bit MMIO register.\r
336\r
337 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is\r
338 returned. This function must guarantee that all MMIO read and write\r
339 operations are serialized.\r
340\r
341 @param Address The MMIO register to read.\r
342\r
343 @return The value read.\r
344\r
345**/\r
346UINT64\r
347EFIAPI\r
348MmioRead64 (\r
349 IN UINT64 Address\r
350 )\r
351{\r
352 UINT64 Data;\r
353\r
8fd567c6 354 //\r
355 // Make sure that Address is 64-bit aligned.\r
356 // \r
357 ASSERT ((Address & 7) == 0);\r
358\r
e1f414b6 359 Address |= BIT63;\r
360\r
361 MemoryFence ();\r
362 Data = *((volatile UINT64 *) Address);\r
363 MemoryFence ();\r
364\r
365 return Data;\r
366\r
367}\r
368\r
369/**\r
370 Writes a 8-bit MMIO register.\r
371\r
372 Writes the 8-bit MMIO register specified by Address with the value specified\r
373 by Value and returns Value. This function must guarantee that all MMIO read\r
374 and write operations are serialized.\r
375\r
376 @param Address The MMIO register to write.\r
377 @param Data The value to write to the MMIO register.\r
378\r
379 @return The value written the memory address.\r
380\r
381**/\r
382UINT8\r
383EFIAPI\r
384MmioWrite8 (\r
385 IN UINT64 Address,\r
386 IN UINT8 Data\r
387 )\r
388{\r
389 Address |= BIT63;\r
390\r
391 MemoryFence ();\r
392 *((volatile UINT8 *) Address) = Data;\r
393 MemoryFence ();\r
394\r
395 return Data;\r
396}\r
397\r
398/**\r
399 Writes a 16-bit MMIO register.\r
400\r
401 Writes the 16-bit MMIO register specified by Address with the value specified\r
402 by Value and returns Value. This function must guarantee that all MMIO read\r
403 and write operations are serialized.\r
404\r
405 @param Address The MMIO register to write.\r
406 @param Data The value to write to the MMIO register.\r
407\r
408 @return The value written the memory address.\r
409\r
410**/\r
411UINT16\r
412EFIAPI\r
413MmioWrite16 (\r
414 IN UINT64 Address,\r
415 IN UINT16 Data\r
416 )\r
417{\r
8fd567c6 418 //\r
419 // Make sure that Address is 16-bit aligned.\r
420 // \r
421 ASSERT ((Address & 1) == 0);\r
422\r
e1f414b6 423 Address |= BIT63;\r
424\r
425 MemoryFence ();\r
426 *((volatile UINT16 *) Address) = Data;\r
427 MemoryFence ();\r
428\r
429 return Data;\r
430}\r
431\r
432/**\r
433 Writes a 32-bit MMIO register.\r
434\r
435 Writes the 32-bit MMIO register specified by Address with the value specified\r
436 by Value and returns Value. This function must guarantee that all MMIO read\r
437 and write operations are serialized.\r
438\r
439 @param Address The MMIO register to write.\r
440 @param Data The value to write to the MMIO register.\r
441\r
442 @return The value written the memory address.\r
443\r
444**/\r
445UINT32\r
446EFIAPI\r
447MmioWrite32 (\r
448 IN UINT64 Address,\r
449 IN UINT32 Data\r
450 )\r
451{\r
8fd567c6 452 //\r
453 // Make sure that Address is 32-bit aligned.\r
454 // \r
455 ASSERT ((Address & 3) == 0);\r
456\r
e1f414b6 457 Address |= BIT63;\r
458\r
459 MemoryFence ();\r
460 *((volatile UINT32 *) Address) = Data;\r
461 MemoryFence ();\r
462\r
463 return Data;\r
464}\r
465\r
466/**\r
467 Writes a 64-bit MMIO register.\r
468\r
469 Writes the 64-bit MMIO register specified by Address with the value specified\r
470 by Value and returns Value. This function must guarantee that all MMIO read\r
471 and write operations are serialized.\r
472\r
473 @param Address The MMIO register to write.\r
474 @param Data The value to write to the MMIO register.\r
475\r
476 @return The value written the memory address.\r
477\r
478**/\r
479UINT64\r
480EFIAPI\r
481MmioWrite64 (\r
482 IN UINT64 Address,\r
483 IN UINT64 Data\r
484 )\r
485{\r
8fd567c6 486 //\r
487 // Make sure that Address is 64-bit aligned.\r
488 // \r
489 ASSERT ((Address & 7) == 0);\r
490\r
e1f414b6 491 Address |= BIT63;\r
492\r
493 MemoryFence ();\r
494 *((volatile UINT64 *) Address) = Data;\r
495 MemoryFence ();\r
496\r
497 return Data;\r
498}\r