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