You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

io.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #ifndef _IPXE_IO_H
  2. #define _IPXE_IO_H
  3. /** @file
  4. *
  5. * iPXE I/O API
  6. *
  7. * The I/O API provides methods for reading from and writing to
  8. * memory-mapped and I/O-mapped devices.
  9. *
  10. * The standard methods (readl()/writel() etc.) do not strictly check
  11. * the type of the address parameter; this is because traditional
  12. * usage does not necessarily provide the correct pointer type. For
  13. * example, code written for ISA devices at fixed I/O addresses (such
  14. * as the keyboard controller) tend to use plain integer constants for
  15. * the address parameter.
  16. */
  17. FILE_LICENCE ( GPL2_OR_LATER );
  18. #include <stdint.h>
  19. #include <ipxe/api.h>
  20. #include <config/ioapi.h>
  21. #include <ipxe/uaccess.h>
  22. /**
  23. * Calculate static inline I/O API function name
  24. *
  25. * @v _prefix Subsystem prefix
  26. * @v _api_func API function
  27. * @ret _subsys_func Subsystem API function
  28. */
  29. #define IOAPI_INLINE( _subsys, _api_func ) \
  30. SINGLE_API_INLINE ( IOAPI_PREFIX_ ## _subsys, _api_func )
  31. /**
  32. * Provide an I/O API implementation
  33. *
  34. * @v _prefix Subsystem prefix
  35. * @v _api_func API function
  36. * @v _func Implementing function
  37. */
  38. #define PROVIDE_IOAPI( _subsys, _api_func, _func ) \
  39. PROVIDE_SINGLE_API ( IOAPI_PREFIX_ ## _subsys, _api_func, _func )
  40. /**
  41. * Provide a static inline I/O API implementation
  42. *
  43. * @v _prefix Subsystem prefix
  44. * @v _api_func API function
  45. */
  46. #define PROVIDE_IOAPI_INLINE( _subsys, _api_func ) \
  47. PROVIDE_SINGLE_API_INLINE ( IOAPI_PREFIX_ ## _subsys, _api_func )
  48. /* Include all architecture-independent I/O API headers */
  49. #include <ipxe/efi/efi_io.h>
  50. /* Include all architecture-dependent I/O API headers */
  51. #include <bits/io.h>
  52. /**
  53. * Wrap an I/O read
  54. *
  55. * @v _func I/O API function
  56. * @v _type Data type
  57. * @v io_addr I/O address
  58. * @v _prefix Prefix for address in debug message
  59. * @v _ndigits Number of hex digits for this data type
  60. */
  61. #define IOAPI_READ( _func, _type, io_addr, _prefix, _ndigits ) ( { \
  62. volatile _type *_io_addr = \
  63. ( ( volatile _type * ) ( intptr_t ) (io_addr) ); \
  64. _type _data = _func ( _io_addr ); \
  65. DBGIO ( "[" _prefix " %08lx] => %0" #_ndigits "llx\n", \
  66. io_to_bus ( _io_addr ), ( unsigned long long ) _data ); \
  67. _data; } )
  68. /**
  69. * Wrap an I/O write
  70. *
  71. * @v _func I/O API function
  72. * @v _type Data type
  73. * @v data Value to write
  74. * @v io_addr I/O address
  75. * @v _prefix Prefix for address in debug message
  76. * @v _ndigits Number of hex digits for this data type
  77. */
  78. #define IOAPI_WRITE( _func, _type, data, io_addr, _prefix, _ndigits ) do { \
  79. volatile _type *_io_addr = \
  80. ( ( volatile _type * ) ( intptr_t ) (io_addr) ); \
  81. _type _data = (data); \
  82. DBGIO ( "[" _prefix " %08lx] <= %0" #_ndigits "llx\n", \
  83. io_to_bus ( _io_addr ), ( unsigned long long ) _data ); \
  84. _func ( _data, _io_addr ); \
  85. } while ( 0 )
  86. /**
  87. * Wrap an I/O string read
  88. *
  89. * @v _func I/O API function
  90. * @v _type Data type
  91. * @v io_addr I/O address
  92. * @v data Data buffer
  93. * @v count Number of elements to read
  94. * @v _prefix Prefix for address in debug message
  95. * @v _ndigits Number of hex digits for this data type
  96. */
  97. #define IOAPI_READS( _func, _type, io_addr, data, count, _prefix, _ndigits ) \
  98. do { \
  99. volatile _type *_io_addr = \
  100. ( ( volatile _type * ) ( intptr_t ) (io_addr) ); \
  101. void *_data_void = (data); /* Check data is a pointer */ \
  102. _type * _data = ( ( _type * ) _data_void ); \
  103. const _type * _dbg_data = _data; \
  104. unsigned int _count = (count); \
  105. unsigned int _dbg_count = _count; \
  106. _func ( _io_addr, _data, _count ); \
  107. DBGIO ( "[" _prefix " %08lx] =>", io_to_bus ( _io_addr ) ); \
  108. while ( _dbg_count-- ) { \
  109. DBGIO ( " %0" #_ndigits "llx", \
  110. ( ( unsigned long long ) *(_dbg_data++) ) ); \
  111. } \
  112. DBGIO ( "\n" ); \
  113. } while ( 0 )
  114. /**
  115. * Wrap an I/O string write
  116. *
  117. * @v _func I/O API function
  118. * @v _type Data type
  119. * @v io_addr I/O address
  120. * @v data Data buffer
  121. * @v count Number of elements to write
  122. * @v _prefix Prefix for address in debug message
  123. * @v _ndigits Number of hex digits for this data type
  124. */
  125. #define IOAPI_WRITES( _func, _type, io_addr, data, count, _prefix, _ndigits ) \
  126. do { \
  127. volatile _type *_io_addr = \
  128. ( ( volatile _type * ) ( intptr_t ) (io_addr) ); \
  129. const void *_data_void = (data); /* Check data is a pointer */ \
  130. const _type * _data = ( ( const _type * ) _data_void ); \
  131. const _type * _dbg_data = _data; \
  132. unsigned int _count = (count); \
  133. unsigned int _dbg_count = _count; \
  134. DBGIO ( "[" _prefix " %08lx] <=", io_to_bus ( _io_addr ) ); \
  135. while ( _dbg_count-- ) { \
  136. DBGIO ( " %0" #_ndigits "llx", \
  137. ( ( unsigned long long ) *(_dbg_data++) ) ); \
  138. } \
  139. DBGIO ( "\n" ); \
  140. _func ( _io_addr, _data, _count ); \
  141. } while ( 0 )
  142. /**
  143. * Convert physical address to a bus address
  144. *
  145. * @v phys_addr Physical address
  146. * @ret bus_addr Bus address
  147. */
  148. unsigned long phys_to_bus ( unsigned long phys_addr );
  149. /**
  150. * Convert bus address to a physical address
  151. *
  152. * @v bus_addr Bus address
  153. * @ret phys_addr Physical address
  154. */
  155. unsigned long bus_to_phys ( unsigned long bus_addr );
  156. /**
  157. * Convert virtual address to a bus address
  158. *
  159. * @v addr Virtual address
  160. * @ret bus_addr Bus address
  161. */
  162. static inline __always_inline unsigned long
  163. virt_to_bus ( volatile const void *addr ) {
  164. return phys_to_bus ( virt_to_phys ( addr ) );
  165. }
  166. /**
  167. * Convert bus address to a virtual address
  168. *
  169. * @v bus_addr Bus address
  170. * @ret addr Virtual address
  171. *
  172. * This operation is not available under all memory models.
  173. */
  174. static inline __always_inline void * bus_to_virt ( unsigned long bus_addr ) {
  175. return phys_to_virt ( bus_to_phys ( bus_addr ) );
  176. }
  177. /**
  178. * Map bus address as an I/O address
  179. *
  180. * @v bus_addr Bus address
  181. * @v len Length of region
  182. * @ret io_addr I/O address
  183. */
  184. void * ioremap ( unsigned long bus_addr, size_t len );
  185. /**
  186. * Unmap I/O address
  187. *
  188. * @v io_addr I/O address
  189. */
  190. void iounmap ( volatile const void *io_addr );
  191. /**
  192. * Convert I/O address to bus address (for debug only)
  193. *
  194. * @v io_addr I/O address
  195. * @ret bus_addr Bus address
  196. */
  197. unsigned long io_to_bus ( volatile const void *io_addr );
  198. /**
  199. * Read byte from memory-mapped device
  200. *
  201. * @v io_addr I/O address
  202. * @ret data Value read
  203. */
  204. uint8_t readb ( volatile uint8_t *io_addr );
  205. #define readb( io_addr ) IOAPI_READ ( readb, uint8_t, io_addr, "MEM", 2 )
  206. /**
  207. * Read 16-bit word from memory-mapped device
  208. *
  209. * @v io_addr I/O address
  210. * @ret data Value read
  211. */
  212. uint16_t readw ( volatile uint16_t *io_addr );
  213. #define readw( io_addr ) IOAPI_READ ( readw, uint16_t, io_addr, "MEM", 4 )
  214. /**
  215. * Read 32-bit dword from memory-mapped device
  216. *
  217. * @v io_addr I/O address
  218. * @ret data Value read
  219. */
  220. uint32_t readl ( volatile uint32_t *io_addr );
  221. #define readl( io_addr ) IOAPI_READ ( readl, uint32_t, io_addr, "MEM", 8 )
  222. /**
  223. * Read 64-bit qword from memory-mapped device
  224. *
  225. * @v io_addr I/O address
  226. * @ret data Value read
  227. */
  228. uint64_t readq ( volatile uint64_t *io_addr );
  229. #define readq( io_addr ) IOAPI_READ ( readq, uint64_t, io_addr, "MEM", 16 )
  230. /**
  231. * Write byte to memory-mapped device
  232. *
  233. * @v data Value to write
  234. * @v io_addr I/O address
  235. */
  236. void writeb ( uint8_t data, volatile uint8_t *io_addr );
  237. #define writeb( data, io_addr ) \
  238. IOAPI_WRITE ( writeb, uint8_t, data, io_addr, "MEM", 2 )
  239. /**
  240. * Write 16-bit word to memory-mapped device
  241. *
  242. * @v data Value to write
  243. * @v io_addr I/O address
  244. */
  245. void writew ( uint16_t data, volatile uint16_t *io_addr );
  246. #define writew( data, io_addr ) \
  247. IOAPI_WRITE ( writew, uint16_t, data, io_addr, "MEM", 4 )
  248. /**
  249. * Write 32-bit dword to memory-mapped device
  250. *
  251. * @v data Value to write
  252. * @v io_addr I/O address
  253. */
  254. void writel ( uint32_t data, volatile uint32_t *io_addr );
  255. #define writel( data, io_addr ) \
  256. IOAPI_WRITE ( writel, uint32_t, data, io_addr, "MEM", 8 )
  257. /**
  258. * Write 64-bit qword to memory-mapped device
  259. *
  260. * @v data Value to write
  261. * @v io_addr I/O address
  262. */
  263. void writeq ( uint64_t data, volatile uint64_t *io_addr );
  264. #define writeq( data, io_addr ) \
  265. IOAPI_WRITE ( writeq, uint64_t, data, io_addr, "MEM", 16 )
  266. /**
  267. * Read byte from I/O-mapped device
  268. *
  269. * @v io_addr I/O address
  270. * @ret data Value read
  271. */
  272. uint8_t inb ( volatile uint8_t *io_addr );
  273. #define inb( io_addr ) IOAPI_READ ( inb, uint8_t, io_addr, "IO", 2 )
  274. /**
  275. * Read 16-bit word from I/O-mapped device
  276. *
  277. * @v io_addr I/O address
  278. * @ret data Value read
  279. */
  280. uint16_t inw ( volatile uint16_t *io_addr );
  281. #define inw( io_addr ) IOAPI_READ ( inw, uint16_t, io_addr, "IO", 4 )
  282. /**
  283. * Read 32-bit dword from I/O-mapped device
  284. *
  285. * @v io_addr I/O address
  286. * @ret data Value read
  287. */
  288. uint32_t inl ( volatile uint32_t *io_addr );
  289. #define inl( io_addr ) IOAPI_READ ( inl, uint32_t, io_addr, "IO", 8 )
  290. /**
  291. * Write byte to I/O-mapped device
  292. *
  293. * @v data Value to write
  294. * @v io_addr I/O address
  295. */
  296. void outb ( uint8_t data, volatile uint8_t *io_addr );
  297. #define outb( data, io_addr ) \
  298. IOAPI_WRITE ( outb, uint8_t, data, io_addr, "IO", 2 )
  299. /**
  300. * Write 16-bit word to I/O-mapped device
  301. *
  302. * @v data Value to write
  303. * @v io_addr I/O address
  304. */
  305. void outw ( uint16_t data, volatile uint16_t *io_addr );
  306. #define outw( data, io_addr ) \
  307. IOAPI_WRITE ( outw, uint16_t, data, io_addr, "IO", 4 )
  308. /**
  309. * Write 32-bit dword to I/O-mapped device
  310. *
  311. * @v data Value to write
  312. * @v io_addr I/O address
  313. */
  314. void outl ( uint32_t data, volatile uint32_t *io_addr );
  315. #define outl( data, io_addr ) \
  316. IOAPI_WRITE ( outl, uint32_t, data, io_addr, "IO", 8 )
  317. /**
  318. * Read bytes from I/O-mapped device
  319. *
  320. * @v io_addr I/O address
  321. * @v data Data buffer
  322. * @v count Number of bytes to read
  323. */
  324. void insb ( volatile uint8_t *io_addr, uint8_t *data, unsigned int count );
  325. #define insb( io_addr, data, count ) \
  326. IOAPI_READS ( insb, uint8_t, io_addr, data, count, "IO", 2 )
  327. /**
  328. * Read 16-bit words from I/O-mapped device
  329. *
  330. * @v io_addr I/O address
  331. * @v data Data buffer
  332. * @v count Number of words to read
  333. */
  334. void insw ( volatile uint16_t *io_addr, uint16_t *data, unsigned int count );
  335. #define insw( io_addr, data, count ) \
  336. IOAPI_READS ( insw, uint16_t, io_addr, data, count, "IO", 4 )
  337. /**
  338. * Read 32-bit words from I/O-mapped device
  339. *
  340. * @v io_addr I/O address
  341. * @v data Data buffer
  342. * @v count Number of words to read
  343. */
  344. void insl ( volatile uint32_t *io_addr, uint32_t *data, unsigned int count );
  345. #define insl( io_addr, data, count ) \
  346. IOAPI_READS ( insl, uint32_t, io_addr, data, count, "IO", 8 )
  347. /**
  348. * Write bytes to I/O-mapped device
  349. *
  350. * @v io_addr I/O address
  351. * @v data Data buffer
  352. * @v count Number of bytes to write
  353. */
  354. void outsb ( volatile uint8_t *io_addr, const uint8_t *data,
  355. unsigned int count );
  356. #define outsb( io_addr, data, count ) \
  357. IOAPI_WRITES ( outsb, uint8_t, io_addr, data, count, "IO", 2 )
  358. /**
  359. * Write 16-bit words to I/O-mapped device
  360. *
  361. * @v io_addr I/O address
  362. * @v data Data buffer
  363. * @v count Number of words to write
  364. */
  365. void outsw ( volatile uint16_t *io_addr, const uint16_t *data,
  366. unsigned int count );
  367. #define outsw( io_addr, data, count ) \
  368. IOAPI_WRITES ( outsw, uint16_t, io_addr, data, count, "IO", 4 )
  369. /**
  370. * Write 32-bit words to I/O-mapped device
  371. *
  372. * @v io_addr I/O address
  373. * @v data Data buffer
  374. * @v count Number of words to write
  375. */
  376. void outsl ( volatile uint32_t *io_addr, const uint32_t *data,
  377. unsigned int count );
  378. #define outsl( io_addr, data, count ) \
  379. IOAPI_WRITES ( outsl, uint32_t, io_addr, data, count, "IO", 8 )
  380. /**
  381. * Slow down I/O
  382. *
  383. */
  384. void iodelay ( void );
  385. /**
  386. * Read value from I/O-mapped device, slowly
  387. *
  388. * @v _func Function to use to read value
  389. * @v data Value to write
  390. * @v io_addr I/O address
  391. */
  392. #define INX_P( _func, _type, io_addr ) ( { \
  393. _type _data = _func ( (io_addr) ); \
  394. iodelay(); \
  395. _data; } )
  396. /**
  397. * Read byte from I/O-mapped device
  398. *
  399. * @v io_addr I/O address
  400. * @ret data Value read
  401. */
  402. #define inb_p( io_addr ) INX_P ( inb, uint8_t, io_addr )
  403. /**
  404. * Read 16-bit word from I/O-mapped device
  405. *
  406. * @v io_addr I/O address
  407. * @ret data Value read
  408. */
  409. #define inw_p( io_addr ) INX_P ( inw, uint16_t, io_addr )
  410. /**
  411. * Read 32-bit dword from I/O-mapped device
  412. *
  413. * @v io_addr I/O address
  414. * @ret data Value read
  415. */
  416. #define inl_p( io_addr ) INX_P ( inl, uint32_t, io_addr )
  417. /**
  418. * Write value to I/O-mapped device, slowly
  419. *
  420. * @v _func Function to use to write value
  421. * @v data Value to write
  422. * @v io_addr I/O address
  423. */
  424. #define OUTX_P( _func, data, io_addr ) do { \
  425. _func ( (data), (io_addr) ); \
  426. iodelay(); \
  427. } while ( 0 )
  428. /**
  429. * Write byte to I/O-mapped device, slowly
  430. *
  431. * @v data Value to write
  432. * @v io_addr I/O address
  433. */
  434. #define outb_p( data, io_addr ) OUTX_P ( outb, data, io_addr )
  435. /**
  436. * Write 16-bit word to I/O-mapped device, slowly
  437. *
  438. * @v data Value to write
  439. * @v io_addr I/O address
  440. */
  441. #define outw_p( data, io_addr ) OUTX_P ( outw, data, io_addr )
  442. /**
  443. * Write 32-bit dword to I/O-mapped device, slowly
  444. *
  445. * @v data Value to write
  446. * @v io_addr I/O address
  447. */
  448. #define outl_p( data, io_addr ) OUTX_P ( outl, data, io_addr )
  449. /**
  450. * Memory barrier
  451. *
  452. */
  453. void mb ( void );
  454. #define rmb() mb()
  455. #define wmb() mb()
  456. /** A usable memory region */
  457. struct memory_region {
  458. /** Physical start address */
  459. uint64_t start;
  460. /** Physical end address */
  461. uint64_t end;
  462. };
  463. /** Maximum number of memory regions we expect to encounter */
  464. #define MAX_MEMORY_REGIONS 8
  465. /** A memory map */
  466. struct memory_map {
  467. /** Memory regions */
  468. struct memory_region regions[MAX_MEMORY_REGIONS];
  469. /** Number of used regions */
  470. unsigned int count;
  471. };
  472. /**
  473. * Get memory map
  474. *
  475. * @v memmap Memory map to fill in
  476. */
  477. void get_memmap ( struct memory_map *memmap );
  478. #endif /* _IPXE_IO_H */