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.

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