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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. #ifndef _IPXE_XHCI_H
  2. #define _IPXE_XHCI_H
  3. /** @file
  4. *
  5. * USB eXtensible Host Controller Interface (xHCI) driver
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <assert.h>
  10. #include <ipxe/pci.h>
  11. #include <ipxe/uaccess.h>
  12. #include <ipxe/usb.h>
  13. /** Minimum alignment required for data structures
  14. *
  15. * With the exception of the scratchpad buffer pages (which are
  16. * page-aligned), data structures used by xHCI generally require from
  17. * 16 to 64 byte alignment and must not cross an (xHCI) page boundary.
  18. * We simplify this requirement by aligning each structure on its own
  19. * size, with a minimum of a 64 byte alignment.
  20. */
  21. #define XHCI_MIN_ALIGN 64
  22. /** xHCI PCI BAR */
  23. #define XHCI_BAR PCI_BASE_ADDRESS_0
  24. /** Capability register length */
  25. #define XHCI_CAP_CAPLENGTH 0x00
  26. /** Host controller interface version number */
  27. #define XHCI_CAP_HCIVERSION 0x02
  28. /** Structural parameters 1 */
  29. #define XHCI_CAP_HCSPARAMS1 0x04
  30. /** Number of device slots */
  31. #define XHCI_HCSPARAMS1_SLOTS(params) ( ( (params) >> 0 ) & 0xff )
  32. /** Number of interrupters */
  33. #define XHCI_HCSPARAMS1_INTRS(params) ( ( (params) >> 8 ) & 0x3ff )
  34. /** Number of ports */
  35. #define XHCI_HCSPARAMS1_PORTS(params) ( ( (params) >> 24 ) & 0xff )
  36. /** Structural parameters 2 */
  37. #define XHCI_CAP_HCSPARAMS2 0x08
  38. /** Number of page-sized scratchpad buffers */
  39. #define XHCI_HCSPARAMS2_SCRATCHPADS(params) \
  40. ( ( ( (params) >> 16 ) & 0x3e0 ) | ( ( (params) >> 27 ) & 0x1f ) )
  41. /** Capability parameters */
  42. #define XHCI_CAP_HCCPARAMS1 0x10
  43. /** 64-bit addressing capability */
  44. #define XHCI_HCCPARAMS1_ADDR64(params) ( ( (params) >> 0 ) & 0x1 )
  45. /** Context size shift */
  46. #define XHCI_HCCPARAMS1_CSZ_SHIFT(params) ( 5 + ( ( (params) >> 2 ) & 0x1 ) )
  47. /** xHCI extended capabilities pointer */
  48. #define XHCI_HCCPARAMS1_XECP(params) ( ( ( (params) >> 16 ) & 0xffff ) << 2 )
  49. /** Doorbell offset */
  50. #define XHCI_CAP_DBOFF 0x14
  51. /** Runtime register space offset */
  52. #define XHCI_CAP_RTSOFF 0x18
  53. /** xHCI extended capability ID */
  54. #define XHCI_XECP_ID(xecp) ( ( (xecp) >> 0 ) & 0xff )
  55. /** Next xHCI extended capability pointer */
  56. #define XHCI_XECP_NEXT(xecp) ( ( ( (xecp) >> 8 ) & 0xff ) << 2 )
  57. /** USB legacy support extended capability */
  58. #define XHCI_XECP_ID_LEGACY 1
  59. /** USB legacy support BIOS owned semaphore */
  60. #define XHCI_USBLEGSUP_BIOS 0x02
  61. /** USB legacy support BIOS ownership flag */
  62. #define XHCI_USBLEGSUP_BIOS_OWNED 0x01
  63. /** USB legacy support OS owned semaphore */
  64. #define XHCI_USBLEGSUP_OS 0x03
  65. /** USB legacy support OS ownership flag */
  66. #define XHCI_USBLEGSUP_OS_OWNED 0x01
  67. /** USB legacy support control/status */
  68. #define XHCI_USBLEGSUP_CTLSTS 0x04
  69. /** Supported protocol extended capability */
  70. #define XHCI_XECP_ID_SUPPORTED 2
  71. /** Supported protocol revision */
  72. #define XHCI_SUPPORTED_REVISION 0x00
  73. /** Supported protocol minor revision */
  74. #define XHCI_SUPPORTED_REVISION_VER(revision) ( ( (revision) >> 16 ) & 0xffff )
  75. /** Supported protocol name */
  76. #define XHCI_SUPPORTED_NAME 0x04
  77. /** Supported protocol ports */
  78. #define XHCI_SUPPORTED_PORTS 0x08
  79. /** Supported protocol port offset */
  80. #define XHCI_SUPPORTED_PORTS_OFFSET(ports) ( ( (ports) >> 0 ) & 0xff )
  81. /** Supported protocol port count */
  82. #define XHCI_SUPPORTED_PORTS_COUNT(ports) ( ( (ports) >> 8 ) & 0xff )
  83. /** Supported protocol PSI count */
  84. #define XHCI_SUPPORTED_PORTS_PSIC(ports) ( ( (ports) >> 28 ) & 0x0f )
  85. /** Supported protocol slot */
  86. #define XHCI_SUPPORTED_SLOT 0x0c
  87. /** Supported protocol slot type */
  88. #define XHCI_SUPPORTED_SLOT_TYPE(slot) ( ( (slot) >> 0 ) & 0x1f )
  89. /** Supported protocol PSI */
  90. #define XHCI_SUPPORTED_PSI(index) ( 0x10 + ( (index) * 4 ) )
  91. /** Supported protocol PSI value */
  92. #define XHCI_SUPPORTED_PSI_VALUE(psi) ( ( (psi) >> 0 ) & 0x0f )
  93. /** Supported protocol PSI mantissa */
  94. #define XHCI_SUPPORTED_PSI_MANTISSA(psi) ( ( (psi) >> 16 ) & 0xffff )
  95. /** Supported protocol PSI exponent */
  96. #define XHCI_SUPPORTED_PSI_EXPONENT(psi) ( ( (psi) >> 4 ) & 0x03 )
  97. /** Default PSI values */
  98. enum xhci_default_psi_value {
  99. /** Full speed (12Mbps) */
  100. XHCI_SPEED_FULL = 1,
  101. /** Low speed (1.5Mbps) */
  102. XHCI_SPEED_LOW = 2,
  103. /** High speed (480Mbps) */
  104. XHCI_SPEED_HIGH = 3,
  105. /** Super speed */
  106. XHCI_SPEED_SUPER = 4,
  107. };
  108. /** USB command register */
  109. #define XHCI_OP_USBCMD 0x00
  110. /** Run/stop */
  111. #define XHCI_USBCMD_RUN 0x00000001UL
  112. /** Host controller reset */
  113. #define XHCI_USBCMD_HCRST 0x00000002UL
  114. /** USB status register */
  115. #define XHCI_OP_USBSTS 0x04
  116. /** Host controller halted */
  117. #define XHCI_USBSTS_HCH 0x00000001UL
  118. /** Page size register */
  119. #define XHCI_OP_PAGESIZE 0x08
  120. /** Page size */
  121. #define XHCI_PAGESIZE(pagesize) ( (pagesize) << 12 )
  122. /** Device notifcation control register */
  123. #define XHCI_OP_DNCTRL 0x14
  124. /** Command ring control register */
  125. #define XHCI_OP_CRCR 0x18
  126. /** Command ring cycle state */
  127. #define XHCI_CRCR_RCS 0x00000001UL
  128. /** Command ring running */
  129. #define XHCI_CRCR_CRR 0x00000008UL
  130. /** Device context base address array pointer */
  131. #define XHCI_OP_DCBAAP 0x30
  132. /** Configure register */
  133. #define XHCI_OP_CONFIG 0x38
  134. /** Maximum device slots enabled */
  135. #define XHCI_CONFIG_MAX_SLOTS_EN(slots) ( (slots) << 0 )
  136. /** Maximum device slots enabled mask */
  137. #define XHCI_CONFIG_MAX_SLOTS_EN_MASK \
  138. XHCI_CONFIG_MAX_SLOTS_EN ( 0xff )
  139. /** Port status and control register */
  140. #define XHCI_OP_PORTSC(port) ( 0x400 - 0x10 + ( (port) << 4 ) )
  141. /** Current connect status */
  142. #define XHCI_PORTSC_CCS 0x00000001UL
  143. /** Port enabled */
  144. #define XHCI_PORTSC_PED 0x00000002UL
  145. /** Port reset */
  146. #define XHCI_PORTSC_PR 0x00000010UL
  147. /** Port link state */
  148. #define XHCI_PORTSC_PLS(pls) ( (pls) << 5 )
  149. /** Disabled port link state */
  150. #define XHCI_PORTSC_PLS_DISABLED XHCI_PORTSC_PLS ( 4 )
  151. /** RxDetect port link state */
  152. #define XHCI_PORTSC_PLS_RXDETECT XHCI_PORTSC_PLS ( 5 )
  153. /** Port link state mask */
  154. #define XHCI_PORTSC_PLS_MASK XHCI_PORTSC_PLS ( 0xf )
  155. /** Port power */
  156. #define XHCI_PORTSC_PP 0x00000200UL
  157. /** Time to delay after enabling power to a port */
  158. #define XHCI_PORT_POWER_DELAY_MS 20
  159. /** Port speed ID value */
  160. #define XHCI_PORTSC_PSIV(portsc) ( ( (portsc) >> 10 ) & 0xf )
  161. /** Port indicator control */
  162. #define XHCI_PORTSC_PIC(indicators) ( (indicators) << 14 )
  163. /** Port indicator control mask */
  164. #define XHCI_PORTSC_PIC_MASK XHCI_PORTSC_PIC ( 3 )
  165. /** Port link state write strobe */
  166. #define XHCI_PORTSC_LWS 0x00010000UL
  167. /** Connect status change */
  168. #define XHCI_PORTSC_CSC 0x00020000UL
  169. /** Port enabled/disabled change */
  170. #define XHCI_PORTSC_PEC 0x00040000UL
  171. /** Warm port reset change */
  172. #define XHCI_PORTSC_WRC 0x00080000UL
  173. /** Over-current change */
  174. #define XHCI_PORTSC_OCC 0x00100000UL
  175. /** Port reset change */
  176. #define XHCI_PORTSC_PRC 0x00200000UL
  177. /** Port link state change */
  178. #define XHCI_PORTSC_PLC 0x00400000UL
  179. /** Port config error change */
  180. #define XHCI_PORTSC_CEC 0x00800000UL
  181. /** Port status change mask */
  182. #define XHCI_PORTSC_CHANGE \
  183. ( XHCI_PORTSC_CSC | XHCI_PORTSC_PEC | XHCI_PORTSC_WRC | \
  184. XHCI_PORTSC_OCC | XHCI_PORTSC_PRC | XHCI_PORTSC_PLC | \
  185. XHCI_PORTSC_CEC )
  186. /** Port status and control bits which should be preserved
  187. *
  188. * The port status and control register is a horrendous mix of
  189. * differing semantics. Some bits are written to only when a separate
  190. * write strobe bit is set. Some bits should be preserved when
  191. * modifying other bits. Some bits will be cleared if written back as
  192. * a one. Most excitingly, the "port enabled" bit has the semantics
  193. * that 1=enabled, 0=disabled, yet writing a 1 will disable the port.
  194. */
  195. #define XHCI_PORTSC_PRESERVE ( XHCI_PORTSC_PP | XHCI_PORTSC_PIC_MASK )
  196. /** Port power management status and control register */
  197. #define XHCI_OP_PORTPMSC(port) ( 0x404 - 0x10 + ( (port) << 4 ) )
  198. /** Port link info register */
  199. #define XHCI_OP_PORTLI(port) ( 0x408 - 0x10 + ( (port) << 4 ) )
  200. /** Port hardware link power management control register */
  201. #define XHCI_OP_PORTHLPMC(port) ( 0x40c - 0x10 + ( (port) << 4 ) )
  202. /** Event ring segment table size register */
  203. #define XHCI_RUN_ERSTSZ(intr) ( 0x28 + ( (intr) << 5 ) )
  204. /** Event ring segment table base address register */
  205. #define XHCI_RUN_ERSTBA(intr) ( 0x30 + ( (intr) << 5 ) )
  206. /** Event ring dequeue pointer register */
  207. #define XHCI_RUN_ERDP(intr) ( 0x38 + ( (intr) << 5 ) )
  208. /** A transfer request block template */
  209. struct xhci_trb_template {
  210. /** Parameter */
  211. uint64_t parameter;
  212. /** Status */
  213. uint32_t status;
  214. /** Control */
  215. uint32_t control;
  216. };
  217. /** A transfer request block */
  218. struct xhci_trb_common {
  219. /** Reserved */
  220. uint64_t reserved_a;
  221. /** Reserved */
  222. uint32_t reserved_b;
  223. /** Flags */
  224. uint8_t flags;
  225. /** Type */
  226. uint8_t type;
  227. /** Reserved */
  228. uint16_t reserved_c;
  229. } __attribute__ (( packed ));
  230. /** Transfer request block cycle bit flag */
  231. #define XHCI_TRB_C 0x01
  232. /** Transfer request block toggle cycle bit flag */
  233. #define XHCI_TRB_TC 0x02
  234. /** Transfer request block chain flag */
  235. #define XHCI_TRB_CH 0x10
  236. /** Transfer request block interrupt on completion flag */
  237. #define XHCI_TRB_IOC 0x20
  238. /** Transfer request block immediate data flag */
  239. #define XHCI_TRB_IDT 0x40
  240. /** Transfer request block type */
  241. #define XHCI_TRB_TYPE(type) ( (type) << 2 )
  242. /** Transfer request block type mask */
  243. #define XHCI_TRB_TYPE_MASK XHCI_TRB_TYPE ( 0x3f )
  244. /** A normal transfer request block */
  245. struct xhci_trb_normal {
  246. /** Data buffer */
  247. uint64_t data;
  248. /** Length */
  249. uint32_t len;
  250. /** Flags */
  251. uint8_t flags;
  252. /** Type */
  253. uint8_t type;
  254. /** Reserved */
  255. uint16_t reserved;
  256. } __attribute__ (( packed ));
  257. /** A normal transfer request block */
  258. #define XHCI_TRB_NORMAL XHCI_TRB_TYPE ( 1 )
  259. /** Construct TD size field */
  260. #define XHCI_TD_SIZE(remaining) \
  261. ( ( ( (remaining) <= 0xf ) ? remaining : 0xf ) << 17 )
  262. /** A setup stage transfer request block */
  263. struct xhci_trb_setup {
  264. /** Setup packet */
  265. struct usb_setup_packet packet;
  266. /** Length */
  267. uint32_t len;
  268. /** Flags */
  269. uint8_t flags;
  270. /** Type */
  271. uint8_t type;
  272. /** Transfer direction */
  273. uint8_t direction;
  274. /** Reserved */
  275. uint8_t reserved;
  276. } __attribute__ (( packed ));
  277. /** A setup stage transfer request block */
  278. #define XHCI_TRB_SETUP XHCI_TRB_TYPE ( 2 )
  279. /** Setup stage input data direction */
  280. #define XHCI_SETUP_IN 3
  281. /** Setup stage output data direction */
  282. #define XHCI_SETUP_OUT 2
  283. /** A data stage transfer request block */
  284. struct xhci_trb_data {
  285. /** Data buffer */
  286. uint64_t data;
  287. /** Length */
  288. uint32_t len;
  289. /** Flags */
  290. uint8_t flags;
  291. /** Type */
  292. uint8_t type;
  293. /** Transfer direction */
  294. uint8_t direction;
  295. /** Reserved */
  296. uint8_t reserved;
  297. } __attribute__ (( packed ));
  298. /** A data stage transfer request block */
  299. #define XHCI_TRB_DATA XHCI_TRB_TYPE ( 3 )
  300. /** Input data direction */
  301. #define XHCI_DATA_IN 0x01
  302. /** Output data direction */
  303. #define XHCI_DATA_OUT 0x00
  304. /** A status stage transfer request block */
  305. struct xhci_trb_status {
  306. /** Reserved */
  307. uint64_t reserved_a;
  308. /** Reserved */
  309. uint32_t reserved_b;
  310. /** Flags */
  311. uint8_t flags;
  312. /** Type */
  313. uint8_t type;
  314. /** Direction */
  315. uint8_t direction;
  316. /** Reserved */
  317. uint8_t reserved_c;
  318. } __attribute__ (( packed ));
  319. /** A status stage transfer request block */
  320. #define XHCI_TRB_STATUS XHCI_TRB_TYPE ( 4 )
  321. /** Input status direction */
  322. #define XHCI_STATUS_IN 0x01
  323. /** Output status direction */
  324. #define XHCI_STATUS_OUT 0x00
  325. /** A link transfer request block */
  326. struct xhci_trb_link {
  327. /** Next ring segment */
  328. uint64_t next;
  329. /** Reserved */
  330. uint32_t reserved_a;
  331. /** Flags */
  332. uint8_t flags;
  333. /** Type */
  334. uint8_t type;
  335. /** Reserved */
  336. uint16_t reserved_c;
  337. } __attribute__ (( packed ));
  338. /** A link transfer request block */
  339. #define XHCI_TRB_LINK XHCI_TRB_TYPE ( 6 )
  340. /** A no-op transfer request block */
  341. #define XHCI_TRB_NOP XHCI_TRB_TYPE ( 8 )
  342. /** An enable slot transfer request block */
  343. struct xhci_trb_enable_slot {
  344. /** Reserved */
  345. uint64_t reserved_a;
  346. /** Reserved */
  347. uint32_t reserved_b;
  348. /** Flags */
  349. uint8_t flags;
  350. /** Type */
  351. uint8_t type;
  352. /** Slot type */
  353. uint8_t slot;
  354. /** Reserved */
  355. uint8_t reserved_c;
  356. } __attribute__ (( packed ));
  357. /** An enable slot transfer request block */
  358. #define XHCI_TRB_ENABLE_SLOT XHCI_TRB_TYPE ( 9 )
  359. /** A disable slot transfer request block */
  360. struct xhci_trb_disable_slot {
  361. /** Reserved */
  362. uint64_t reserved_a;
  363. /** Reserved */
  364. uint32_t reserved_b;
  365. /** Flags */
  366. uint8_t flags;
  367. /** Type */
  368. uint8_t type;
  369. /** Reserved */
  370. uint8_t reserved_c;
  371. /** Slot ID */
  372. uint8_t slot;
  373. } __attribute__ (( packed ));
  374. /** A disable slot transfer request block */
  375. #define XHCI_TRB_DISABLE_SLOT XHCI_TRB_TYPE ( 10 )
  376. /** A context transfer request block */
  377. struct xhci_trb_context {
  378. /** Input context */
  379. uint64_t input;
  380. /** Reserved */
  381. uint32_t reserved_a;
  382. /** Flags */
  383. uint8_t flags;
  384. /** Type */
  385. uint8_t type;
  386. /** Reserved */
  387. uint8_t reserved_b;
  388. /** Slot ID */
  389. uint8_t slot;
  390. } __attribute__ (( packed ));
  391. /** An address device transfer request block */
  392. #define XHCI_TRB_ADDRESS_DEVICE XHCI_TRB_TYPE ( 11 )
  393. /** A configure endpoint transfer request block */
  394. #define XHCI_TRB_CONFIGURE_ENDPOINT XHCI_TRB_TYPE ( 12 )
  395. /** An evaluate context transfer request block */
  396. #define XHCI_TRB_EVALUATE_CONTEXT XHCI_TRB_TYPE ( 13 )
  397. /** A reset endpoint transfer request block */
  398. struct xhci_trb_reset_endpoint {
  399. /** Reserved */
  400. uint64_t reserved_a;
  401. /** Reserved */
  402. uint32_t reserved_b;
  403. /** Flags */
  404. uint8_t flags;
  405. /** Type */
  406. uint8_t type;
  407. /** Endpoint ID */
  408. uint8_t endpoint;
  409. /** Slot ID */
  410. uint8_t slot;
  411. } __attribute__ (( packed ));
  412. /** A reset endpoint transfer request block */
  413. #define XHCI_TRB_RESET_ENDPOINT XHCI_TRB_TYPE ( 14 )
  414. /** A stop endpoint transfer request block */
  415. struct xhci_trb_stop_endpoint {
  416. /** Reserved */
  417. uint64_t reserved_a;
  418. /** Reserved */
  419. uint32_t reserved_b;
  420. /** Flags */
  421. uint8_t flags;
  422. /** Type */
  423. uint8_t type;
  424. /** Endpoint ID */
  425. uint8_t endpoint;
  426. /** Slot ID */
  427. uint8_t slot;
  428. } __attribute__ (( packed ));
  429. /** A stop endpoint transfer request block */
  430. #define XHCI_TRB_STOP_ENDPOINT XHCI_TRB_TYPE ( 15 )
  431. /** A set transfer ring dequeue pointer transfer request block */
  432. struct xhci_trb_set_tr_dequeue_pointer {
  433. /** Dequeue pointer */
  434. uint64_t dequeue;
  435. /** Reserved */
  436. uint32_t reserved;
  437. /** Flags */
  438. uint8_t flags;
  439. /** Type */
  440. uint8_t type;
  441. /** Endpoint ID */
  442. uint8_t endpoint;
  443. /** Slot ID */
  444. uint8_t slot;
  445. } __attribute__ (( packed ));
  446. /** A set transfer ring dequeue pointer transfer request block */
  447. #define XHCI_TRB_SET_TR_DEQUEUE_POINTER XHCI_TRB_TYPE ( 16 )
  448. /** A no-op command transfer request block */
  449. #define XHCI_TRB_NOP_CMD XHCI_TRB_TYPE ( 23 )
  450. /** A transfer event transfer request block */
  451. struct xhci_trb_transfer {
  452. /** Transfer TRB pointer */
  453. uint64_t transfer;
  454. /** Residual transfer length */
  455. uint16_t residual;
  456. /** Reserved */
  457. uint8_t reserved;
  458. /** Completion code */
  459. uint8_t code;
  460. /** Flags */
  461. uint8_t flags;
  462. /** Type */
  463. uint8_t type;
  464. /** Endpoint ID */
  465. uint8_t endpoint;
  466. /** Slot ID */
  467. uint8_t slot;
  468. } __attribute__ (( packed ));
  469. /** A transfer event transfer request block */
  470. #define XHCI_TRB_TRANSFER XHCI_TRB_TYPE ( 32 )
  471. /** A command completion event transfer request block */
  472. struct xhci_trb_complete {
  473. /** Command TRB pointer */
  474. uint64_t command;
  475. /** Parameter */
  476. uint8_t parameter[3];
  477. /** Completion code */
  478. uint8_t code;
  479. /** Flags */
  480. uint8_t flags;
  481. /** Type */
  482. uint8_t type;
  483. /** Virtual function ID */
  484. uint8_t vf;
  485. /** Slot ID */
  486. uint8_t slot;
  487. } __attribute__ (( packed ));
  488. /** A command completion event transfer request block */
  489. #define XHCI_TRB_COMPLETE XHCI_TRB_TYPE ( 33 )
  490. /** xHCI completion codes */
  491. enum xhci_completion_code {
  492. /** Success */
  493. XHCI_CMPLT_SUCCESS = 1,
  494. /** Short packet */
  495. XHCI_CMPLT_SHORT = 13,
  496. };
  497. /** A port status change transfer request block */
  498. struct xhci_trb_port_status {
  499. /** Reserved */
  500. uint8_t reserved_a[3];
  501. /** Port ID */
  502. uint8_t port;
  503. /** Reserved */
  504. uint8_t reserved_b[7];
  505. /** Completion code */
  506. uint8_t code;
  507. /** Flags */
  508. uint8_t flags;
  509. /** Type */
  510. uint8_t type;
  511. /** Reserved */
  512. uint16_t reserved_c;
  513. } __attribute__ (( packed ));
  514. /** A port status change transfer request block */
  515. #define XHCI_TRB_PORT_STATUS XHCI_TRB_TYPE ( 34 )
  516. /** A port status change transfer request block */
  517. struct xhci_trb_host_controller {
  518. /** Reserved */
  519. uint64_t reserved_a;
  520. /** Reserved */
  521. uint8_t reserved_b[3];
  522. /** Completion code */
  523. uint8_t code;
  524. /** Flags */
  525. uint8_t flags;
  526. /** Type */
  527. uint8_t type;
  528. /** Reserved */
  529. uint16_t reserved_c;
  530. } __attribute__ (( packed ));
  531. /** A port status change transfer request block */
  532. #define XHCI_TRB_HOST_CONTROLLER XHCI_TRB_TYPE ( 37 )
  533. /** A transfer request block */
  534. union xhci_trb {
  535. /** Template */
  536. struct xhci_trb_template template;
  537. /** Common fields */
  538. struct xhci_trb_common common;
  539. /** Normal TRB */
  540. struct xhci_trb_normal normal;
  541. /** Setup stage TRB */
  542. struct xhci_trb_setup setup;
  543. /** Data stage TRB */
  544. struct xhci_trb_data data;
  545. /** Status stage TRB */
  546. struct xhci_trb_status status;
  547. /** Link TRB */
  548. struct xhci_trb_link link;
  549. /** Enable slot TRB */
  550. struct xhci_trb_enable_slot enable;
  551. /** Disable slot TRB */
  552. struct xhci_trb_disable_slot disable;
  553. /** Input context TRB */
  554. struct xhci_trb_context context;
  555. /** Reset endpoint TRB */
  556. struct xhci_trb_reset_endpoint reset;
  557. /** Stop endpoint TRB */
  558. struct xhci_trb_stop_endpoint stop;
  559. /** Set transfer ring dequeue pointer TRB */
  560. struct xhci_trb_set_tr_dequeue_pointer dequeue;
  561. /** Transfer event */
  562. struct xhci_trb_transfer transfer;
  563. /** Command completion event */
  564. struct xhci_trb_complete complete;
  565. /** Port status changed event */
  566. struct xhci_trb_port_status port;
  567. /** Host controller event */
  568. struct xhci_trb_host_controller host;
  569. } __attribute__ (( packed ));
  570. /** An input control context */
  571. struct xhci_control_context {
  572. /** Drop context flags */
  573. uint32_t drop;
  574. /** Add context flags */
  575. uint32_t add;
  576. /** Reserved */
  577. uint32_t reserved_a[5];
  578. /** Configuration value */
  579. uint8_t config;
  580. /** Interface number */
  581. uint8_t intf;
  582. /** Alternate setting */
  583. uint8_t alt;
  584. /** Reserved */
  585. uint8_t reserved_b;
  586. } __attribute__ (( packed ));
  587. /** A slot context */
  588. struct xhci_slot_context {
  589. /** Device info */
  590. uint32_t info;
  591. /** Maximum exit latency */
  592. uint16_t latency;
  593. /** Root hub port number */
  594. uint8_t port;
  595. /** Number of downstream ports */
  596. uint8_t ports;
  597. /** TT hub slot ID */
  598. uint8_t tt_id;
  599. /** TT port number */
  600. uint8_t tt_port;
  601. /** Interrupter target */
  602. uint16_t intr;
  603. /** USB address */
  604. uint8_t address;
  605. /** Reserved */
  606. uint16_t reserved_a;
  607. /** Slot state */
  608. uint8_t state;
  609. /** Reserved */
  610. uint32_t reserved_b[4];
  611. } __attribute__ (( packed ));
  612. /** Construct slot context device info */
  613. #define XHCI_SLOT_INFO( entries, hub, speed, route ) \
  614. ( ( (entries) << 27 ) | ( (hub) << 26 ) | ( (speed) << 20 ) | (route) )
  615. /** An endpoint context */
  616. struct xhci_endpoint_context {
  617. /** Endpoint state */
  618. uint8_t state;
  619. /** Stream configuration */
  620. uint8_t stream;
  621. /** Polling interval */
  622. uint8_t interval;
  623. /** Max ESIT payload high */
  624. uint8_t esit_high;
  625. /** Endpoint type */
  626. uint8_t type;
  627. /** Maximum burst size */
  628. uint8_t burst;
  629. /** Maximum packet size */
  630. uint16_t mtu;
  631. /** Transfer ring dequeue pointer */
  632. uint64_t dequeue;
  633. /** Average TRB length */
  634. uint16_t trb_len;
  635. /** Max ESIT payload low */
  636. uint16_t esit_low;
  637. /** Reserved */
  638. uint32_t reserved[3];
  639. } __attribute__ (( packed ));
  640. /** Endpoint states */
  641. enum xhci_endpoint_state {
  642. /** Endpoint is disabled */
  643. XHCI_ENDPOINT_DISABLED = 0,
  644. /** Endpoint is running */
  645. XHCI_ENDPOINT_RUNNING = 1,
  646. /** Endpoint is halted due to a USB Halt condition */
  647. XHCI_ENDPOINT_HALTED = 2,
  648. /** Endpoint is stopped */
  649. XHCI_ENDPOINT_STOPPED = 3,
  650. /** Endpoint is halted due to a TRB error */
  651. XHCI_ENDPOINT_ERROR = 4,
  652. };
  653. /** Endpoint state mask */
  654. #define XHCI_ENDPOINT_STATE_MASK 0x07
  655. /** Endpoint type */
  656. #define XHCI_EP_TYPE(type) ( (type) << 3 )
  657. /** Control endpoint type */
  658. #define XHCI_EP_TYPE_CONTROL XHCI_EP_TYPE ( 4 )
  659. /** Input endpoint type */
  660. #define XHCI_EP_TYPE_IN XHCI_EP_TYPE ( 4 )
  661. /** Endpoint dequeue cycle state */
  662. #define XHCI_EP_DCS 0x00000001UL
  663. /** Control endpoint average TRB length */
  664. #define XHCI_EP0_TRB_LEN 8
  665. /** An event ring segment */
  666. struct xhci_event_ring_segment {
  667. /** Base address */
  668. uint64_t base;
  669. /** Number of TRBs */
  670. uint32_t count;
  671. /** Reserved */
  672. uint32_t reserved;
  673. } __attribute__ (( packed ));
  674. /** A transfer request block command/transfer ring */
  675. struct xhci_trb_ring {
  676. /** Producer counter */
  677. unsigned int prod;
  678. /** Consumer counter */
  679. unsigned int cons;
  680. /** Ring size (log2) */
  681. unsigned int shift;
  682. /** Ring counter mask */
  683. unsigned int mask;
  684. /** I/O buffers */
  685. struct io_buffer **iobuf;
  686. /** Transfer request blocks */
  687. union xhci_trb *trb;
  688. /** Length of transfer request blocks */
  689. size_t len;
  690. /** Link TRB (if applicable) */
  691. struct xhci_trb_link *link;
  692. /** Doorbell register */
  693. void *db;
  694. /** Doorbell register value */
  695. uint32_t dbval;
  696. };
  697. /** An event ring */
  698. struct xhci_event_ring {
  699. /** Consumer counter */
  700. unsigned int cons;
  701. /** Event ring segment table */
  702. struct xhci_event_ring_segment *segment;
  703. /** Transfer request blocks */
  704. union xhci_trb *trb;
  705. };
  706. /**
  707. * Calculate doorbell register value
  708. *
  709. * @v target Doorbell target
  710. * @v stream Doorbell stream ID
  711. * @ret dbval Doorbell register value
  712. */
  713. #define XHCI_DBVAL( target, stream ) ( (target) | ( (stream) << 16 ) )
  714. /**
  715. * Calculate space used in TRB ring
  716. *
  717. * @v ring TRB ring
  718. * @ret fill Number of entries used
  719. */
  720. static inline __attribute__ (( always_inline )) unsigned int
  721. xhci_ring_fill ( struct xhci_trb_ring *ring ) {
  722. return ( ring->prod - ring->cons );
  723. }
  724. /**
  725. * Calculate space remaining in TRB ring
  726. *
  727. * @v ring TRB ring
  728. * @ret remaining Number of entries remaining
  729. *
  730. * xHCI does not allow us to completely fill a ring; there must be at
  731. * least one free entry (excluding the Link TRB).
  732. */
  733. static inline __attribute__ (( always_inline )) unsigned int
  734. xhci_ring_remaining ( struct xhci_trb_ring *ring ) {
  735. unsigned int fill = xhci_ring_fill ( ring );
  736. /* We choose to utilise rings with ( 2^n + 1 ) entries, with
  737. * the final entry being a Link TRB. The maximum fill level
  738. * is therefore
  739. *
  740. * ( ( 2^n + 1 ) - 1 (Link TRB) - 1 (one slot always empty)
  741. * == ( 2^n - 1 )
  742. *
  743. * which is therefore equal to the ring mask.
  744. */
  745. assert ( fill <= ring->mask );
  746. return ( ring->mask - fill );
  747. }
  748. /**
  749. * Calculate physical address of most recently consumed TRB
  750. *
  751. * @v ring TRB ring
  752. * @ret trb TRB physical address
  753. */
  754. static inline __attribute__ (( always_inline )) physaddr_t
  755. xhci_ring_consumed ( struct xhci_trb_ring *ring ) {
  756. unsigned int index = ( ( ring->cons - 1 ) & ring->mask );
  757. return virt_to_phys ( &ring->trb[index] );
  758. }
  759. /** Slot context index */
  760. #define XHCI_CTX_SLOT 0
  761. /** Calculate context index from USB endpoint address */
  762. #define XHCI_CTX(address) \
  763. ( (address) ? ( ( ( (address) & 0x0f ) << 1 ) | \
  764. ( ( (address) & 0x80 ) >> 7 ) ) : 1 )
  765. /** Endpoint zero context index */
  766. #define XHCI_CTX_EP0 XHCI_CTX ( 0x00 )
  767. /** End of contexts */
  768. #define XHCI_CTX_END 32
  769. /** Device context index */
  770. #define XHCI_DCI(ctx) ( (ctx) + 0 )
  771. /** Input context index */
  772. #define XHCI_ICI(ctx) ( (ctx) + 1 )
  773. /** Number of TRBs (excluding Link TRB) in the command ring
  774. *
  775. * This is a policy decision.
  776. */
  777. #define XHCI_CMD_TRBS_LOG2 2
  778. /** Number of TRBs in the event ring
  779. *
  780. * This is a policy decision.
  781. */
  782. #define XHCI_EVENT_TRBS_LOG2 6
  783. /** Number of TRBs in a transfer ring
  784. *
  785. * This is a policy decision.
  786. */
  787. #define XHCI_TRANSFER_TRBS_LOG2 6
  788. /** Maximum time to wait for BIOS to release ownership
  789. *
  790. * This is a policy decision.
  791. */
  792. #define XHCI_USBLEGSUP_MAX_WAIT_MS 100
  793. /** Maximum time to wait for host controller to stop
  794. *
  795. * This is a policy decision.
  796. */
  797. #define XHCI_STOP_MAX_WAIT_MS 100
  798. /** Maximum time to wait for reset to complete
  799. *
  800. * This is a policy decision.
  801. */
  802. #define XHCI_RESET_MAX_WAIT_MS 500
  803. /** Maximum time to wait for a command to complete
  804. *
  805. * This is a policy decision.
  806. */
  807. #define XHCI_COMMAND_MAX_WAIT_MS 500
  808. /** Maximum time to wait for a port reset to complete
  809. *
  810. * This is a policy decision.
  811. */
  812. #define XHCI_PORT_RESET_MAX_WAIT_MS 500
  813. /** An xHCI device */
  814. struct xhci_device {
  815. /** Registers */
  816. void *regs;
  817. /** Capability registers */
  818. void *cap;
  819. /** Operational registers */
  820. void *op;
  821. /** Runtime registers */
  822. void *run;
  823. /** Doorbell registers */
  824. void *db;
  825. /** Number of device slots */
  826. unsigned int slots;
  827. /** Number of interrupters */
  828. unsigned int intrs;
  829. /** Number of ports */
  830. unsigned int ports;
  831. /** Number of page-sized scratchpad buffers */
  832. unsigned int scratchpads;
  833. /** 64-bit addressing capability */
  834. int addr64;
  835. /** Context size shift */
  836. unsigned int csz_shift;
  837. /** xHCI extended capabilities offset */
  838. unsigned int xecp;
  839. /** Page size */
  840. size_t pagesize;
  841. /** USB legacy support capability (if present and enabled) */
  842. unsigned int legacy;
  843. /** Device context base address array */
  844. uint64_t *dcbaa;
  845. /** Scratchpad buffer area */
  846. userptr_t scratchpad;
  847. /** Scratchpad buffer array */
  848. uint64_t *scratchpad_array;
  849. /** Command ring */
  850. struct xhci_trb_ring command;
  851. /** Event ring */
  852. struct xhci_event_ring event;
  853. /** Current command completion buffer (if any) */
  854. union xhci_trb *completion;
  855. /** Device slots, indexed by slot ID */
  856. struct xhci_slot **slot;
  857. /** USB bus */
  858. struct usb_bus *bus;
  859. };
  860. /** An xHCI device slot */
  861. struct xhci_slot {
  862. /** xHCI device */
  863. struct xhci_device *xhci;
  864. /** USB device */
  865. struct usb_device *usb;
  866. /** Slot ID */
  867. unsigned int id;
  868. /** Slot context */
  869. struct xhci_slot_context *context;
  870. /** Route string */
  871. unsigned int route;
  872. /** Root hub port number */
  873. unsigned int port;
  874. /** Protocol speed ID */
  875. unsigned int psiv;
  876. /** Endpoints, indexed by context ID */
  877. struct xhci_endpoint *endpoint[XHCI_CTX_END];
  878. };
  879. /** An xHCI endpoint */
  880. struct xhci_endpoint {
  881. /** xHCI device */
  882. struct xhci_device *xhci;
  883. /** xHCI slot */
  884. struct xhci_slot *slot;
  885. /** USB endpoint */
  886. struct usb_endpoint *ep;
  887. /** Context index */
  888. unsigned int ctx;
  889. /** Endpoint type */
  890. unsigned int type;
  891. /** Endpoint context */
  892. struct xhci_endpoint_context *context;
  893. /** Transfer ring */
  894. struct xhci_trb_ring ring;
  895. };
  896. #endif /* _IPXE_XHCI_H */