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