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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 <config/ioapi.h>
  21. #include <ipxe/uaccess.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. * Map bus address as an I/O address
  182. *
  183. * @v bus_addr Bus address
  184. * @v len Length of region
  185. * @ret io_addr I/O address
  186. */
  187. void * ioremap ( unsigned long bus_addr, size_t len );
  188. /**
  189. * Unmap I/O address
  190. *
  191. * @v io_addr I/O address
  192. */
  193. void iounmap ( volatile const void *io_addr );
  194. /**
  195. * Convert I/O address to bus address (for debug only)
  196. *
  197. * @v io_addr I/O address
  198. * @ret bus_addr Bus address
  199. */
  200. unsigned long io_to_bus ( volatile const void *io_addr );
  201. /**
  202. * Read byte from memory-mapped device
  203. *
  204. * @v io_addr I/O address
  205. * @ret data Value read
  206. */
  207. uint8_t readb ( volatile uint8_t *io_addr );
  208. #define readb( io_addr ) IOAPI_READ ( readb, uint8_t, io_addr, "MEM", 2 )
  209. /**
  210. * Read 16-bit word from memory-mapped device
  211. *
  212. * @v io_addr I/O address
  213. * @ret data Value read
  214. */
  215. uint16_t readw ( volatile uint16_t *io_addr );
  216. #define readw( io_addr ) IOAPI_READ ( readw, uint16_t, io_addr, "MEM", 4 )
  217. /**
  218. * Read 32-bit dword from memory-mapped device
  219. *
  220. * @v io_addr I/O address
  221. * @ret data Value read
  222. */
  223. uint32_t readl ( volatile uint32_t *io_addr );
  224. #define readl( io_addr ) IOAPI_READ ( readl, uint32_t, io_addr, "MEM", 8 )
  225. /**
  226. * Read 64-bit qword from memory-mapped device
  227. *
  228. * @v io_addr I/O address
  229. * @ret data Value read
  230. */
  231. uint64_t readq ( volatile uint64_t *io_addr );
  232. #define readq( io_addr ) IOAPI_READ ( readq, uint64_t, io_addr, "MEM", 16 )
  233. /**
  234. * Write byte to memory-mapped device
  235. *
  236. * @v data Value to write
  237. * @v io_addr I/O address
  238. */
  239. void writeb ( uint8_t data, volatile uint8_t *io_addr );
  240. #define writeb( data, io_addr ) \
  241. IOAPI_WRITE ( writeb, uint8_t, data, io_addr, "MEM", 2 )
  242. /**
  243. * Write 16-bit word to memory-mapped device
  244. *
  245. * @v data Value to write
  246. * @v io_addr I/O address
  247. */
  248. void writew ( uint16_t data, volatile uint16_t *io_addr );
  249. #define writew( data, io_addr ) \
  250. IOAPI_WRITE ( writew, uint16_t, data, io_addr, "MEM", 4 )
  251. /**
  252. * Write 32-bit dword to memory-mapped device
  253. *
  254. * @v data Value to write
  255. * @v io_addr I/O address
  256. */
  257. void writel ( uint32_t data, volatile uint32_t *io_addr );
  258. #define writel( data, io_addr ) \
  259. IOAPI_WRITE ( writel, uint32_t, data, io_addr, "MEM", 8 )
  260. /**
  261. * Write 64-bit qword to memory-mapped device
  262. *
  263. * @v data Value to write
  264. * @v io_addr I/O address
  265. */
  266. void writeq ( uint64_t data, volatile uint64_t *io_addr );
  267. #define writeq( data, io_addr ) \
  268. IOAPI_WRITE ( writeq, uint64_t, data, io_addr, "MEM", 16 )
  269. /**
  270. * Read byte from I/O-mapped device
  271. *
  272. * @v io_addr I/O address
  273. * @ret data Value read
  274. */
  275. uint8_t inb ( volatile uint8_t *io_addr );
  276. #define inb( io_addr ) IOAPI_READ ( inb, uint8_t, io_addr, "IO", 2 )
  277. /**
  278. * Read 16-bit word from I/O-mapped device
  279. *
  280. * @v io_addr I/O address
  281. * @ret data Value read
  282. */
  283. uint16_t inw ( volatile uint16_t *io_addr );
  284. #define inw( io_addr ) IOAPI_READ ( inw, uint16_t, io_addr, "IO", 4 )
  285. /**
  286. * Read 32-bit dword from I/O-mapped device
  287. *
  288. * @v io_addr I/O address
  289. * @ret data Value read
  290. */
  291. uint32_t inl ( volatile uint32_t *io_addr );
  292. #define inl( io_addr ) IOAPI_READ ( inl, uint32_t, io_addr, "IO", 8 )
  293. /**
  294. * Write byte to I/O-mapped device
  295. *
  296. * @v data Value to write
  297. * @v io_addr I/O address
  298. */
  299. void outb ( uint8_t data, volatile uint8_t *io_addr );
  300. #define outb( data, io_addr ) \
  301. IOAPI_WRITE ( outb, uint8_t, data, io_addr, "IO", 2 )
  302. /**
  303. * Write 16-bit word to I/O-mapped device
  304. *
  305. * @v data Value to write
  306. * @v io_addr I/O address
  307. */
  308. void outw ( uint16_t data, volatile uint16_t *io_addr );
  309. #define outw( data, io_addr ) \
  310. IOAPI_WRITE ( outw, uint16_t, data, io_addr, "IO", 4 )
  311. /**
  312. * Write 32-bit dword to I/O-mapped device
  313. *
  314. * @v data Value to write
  315. * @v io_addr I/O address
  316. */
  317. void outl ( uint32_t data, volatile uint32_t *io_addr );
  318. #define outl( data, io_addr ) \
  319. IOAPI_WRITE ( outl, uint32_t, data, io_addr, "IO", 8 )
  320. /**
  321. * Read bytes from I/O-mapped device
  322. *
  323. * @v io_addr I/O address
  324. * @v data Data buffer
  325. * @v count Number of bytes to read
  326. */
  327. void insb ( volatile uint8_t *io_addr, uint8_t *data, unsigned int count );
  328. #define insb( io_addr, data, count ) \
  329. IOAPI_READS ( insb, uint8_t, io_addr, data, count, "IO", 2 )
  330. /**
  331. * Read 16-bit words from I/O-mapped device
  332. *
  333. * @v io_addr I/O address
  334. * @v data Data buffer
  335. * @v count Number of words to read
  336. */
  337. void insw ( volatile uint16_t *io_addr, uint16_t *data, unsigned int count );
  338. #define insw( io_addr, data, count ) \
  339. IOAPI_READS ( insw, uint16_t, io_addr, data, count, "IO", 4 )
  340. /**
  341. * Read 32-bit words from I/O-mapped device
  342. *
  343. * @v io_addr I/O address
  344. * @v data Data buffer
  345. * @v count Number of words to read
  346. */
  347. void insl ( volatile uint32_t *io_addr, uint32_t *data, unsigned int count );
  348. #define insl( io_addr, data, count ) \
  349. IOAPI_READS ( insl, uint32_t, io_addr, data, count, "IO", 8 )
  350. /**
  351. * Write bytes to I/O-mapped device
  352. *
  353. * @v io_addr I/O address
  354. * @v data Data buffer
  355. * @v count Number of bytes to write
  356. */
  357. void outsb ( volatile uint8_t *io_addr, const uint8_t *data,
  358. unsigned int count );
  359. #define outsb( io_addr, data, count ) \
  360. IOAPI_WRITES ( outsb, uint8_t, io_addr, data, count, "IO", 2 )
  361. /**
  362. * Write 16-bit words to I/O-mapped device
  363. *
  364. * @v io_addr I/O address
  365. * @v data Data buffer
  366. * @v count Number of words to write
  367. */
  368. void outsw ( volatile uint16_t *io_addr, const uint16_t *data,
  369. unsigned int count );
  370. #define outsw( io_addr, data, count ) \
  371. IOAPI_WRITES ( outsw, uint16_t, io_addr, data, count, "IO", 4 )
  372. /**
  373. * Write 32-bit words to I/O-mapped device
  374. *
  375. * @v io_addr I/O address
  376. * @v data Data buffer
  377. * @v count Number of words to write
  378. */
  379. void outsl ( volatile uint32_t *io_addr, const uint32_t *data,
  380. unsigned int count );
  381. #define outsl( io_addr, data, count ) \
  382. IOAPI_WRITES ( outsl, uint32_t, io_addr, data, count, "IO", 8 )
  383. /**
  384. * Slow down I/O
  385. *
  386. */
  387. void iodelay ( void );
  388. /**
  389. * Read value from I/O-mapped device, slowly
  390. *
  391. * @v _func Function to use to read value
  392. * @v data Value to write
  393. * @v io_addr I/O address
  394. */
  395. #define INX_P( _func, _type, io_addr ) ( { \
  396. _type _data = _func ( (io_addr) ); \
  397. iodelay(); \
  398. _data; } )
  399. /**
  400. * Read byte from I/O-mapped device
  401. *
  402. * @v io_addr I/O address
  403. * @ret data Value read
  404. */
  405. #define inb_p( io_addr ) INX_P ( inb, uint8_t, io_addr )
  406. /**
  407. * Read 16-bit word from I/O-mapped device
  408. *
  409. * @v io_addr I/O address
  410. * @ret data Value read
  411. */
  412. #define inw_p( io_addr ) INX_P ( inw, uint16_t, io_addr )
  413. /**
  414. * Read 32-bit dword from I/O-mapped device
  415. *
  416. * @v io_addr I/O address
  417. * @ret data Value read
  418. */
  419. #define inl_p( io_addr ) INX_P ( inl, uint32_t, io_addr )
  420. /**
  421. * Write value to I/O-mapped device, slowly
  422. *
  423. * @v _func Function to use to write value
  424. * @v data Value to write
  425. * @v io_addr I/O address
  426. */
  427. #define OUTX_P( _func, data, io_addr ) do { \
  428. _func ( (data), (io_addr) ); \
  429. iodelay(); \
  430. } while ( 0 )
  431. /**
  432. * Write byte to I/O-mapped device, slowly
  433. *
  434. * @v data Value to write
  435. * @v io_addr I/O address
  436. */
  437. #define outb_p( data, io_addr ) OUTX_P ( outb, data, io_addr )
  438. /**
  439. * Write 16-bit word to I/O-mapped device, slowly
  440. *
  441. * @v data Value to write
  442. * @v io_addr I/O address
  443. */
  444. #define outw_p( data, io_addr ) OUTX_P ( outw, data, io_addr )
  445. /**
  446. * Write 32-bit dword to I/O-mapped device, slowly
  447. *
  448. * @v data Value to write
  449. * @v io_addr I/O address
  450. */
  451. #define outl_p( data, io_addr ) OUTX_P ( outl, data, io_addr )
  452. /**
  453. * Memory barrier
  454. *
  455. */
  456. void mb ( void );
  457. #define rmb() mb()
  458. #define wmb() mb()
  459. /** A usable memory region */
  460. struct memory_region {
  461. /** Physical start address */
  462. uint64_t start;
  463. /** Physical end address */
  464. uint64_t end;
  465. };
  466. /** Maximum number of memory regions we expect to encounter */
  467. #define MAX_MEMORY_REGIONS 8
  468. /** A memory map */
  469. struct memory_map {
  470. /** Memory regions */
  471. struct memory_region regions[MAX_MEMORY_REGIONS];
  472. /** Number of used regions */
  473. unsigned int count;
  474. };
  475. /**
  476. * Get memory map
  477. *
  478. * @v memmap Memory map to fill in
  479. */
  480. void get_memmap ( struct memory_map *memmap );
  481. #endif /* _IPXE_IO_H */