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.

xhci.h 25KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 transfer request block */
  517. union xhci_trb {
  518. /** Template */
  519. struct xhci_trb_template template;
  520. /** Common fields */
  521. struct xhci_trb_common common;
  522. /** Normal TRB */
  523. struct xhci_trb_normal normal;
  524. /** Setup stage TRB */
  525. struct xhci_trb_setup setup;
  526. /** Data stage TRB */
  527. struct xhci_trb_data data;
  528. /** Status stage TRB */
  529. struct xhci_trb_status status;
  530. /** Link TRB */
  531. struct xhci_trb_link link;
  532. /** Enable slot TRB */
  533. struct xhci_trb_enable_slot enable;
  534. /** Disable slot TRB */
  535. struct xhci_trb_disable_slot disable;
  536. /** Input context TRB */
  537. struct xhci_trb_context context;
  538. /** Reset endpoint TRB */
  539. struct xhci_trb_reset_endpoint reset;
  540. /** Stop endpoint TRB */
  541. struct xhci_trb_stop_endpoint stop;
  542. /** Set transfer ring dequeue pointer TRB */
  543. struct xhci_trb_set_tr_dequeue_pointer dequeue;
  544. /** Transfer event */
  545. struct xhci_trb_transfer transfer;
  546. /** Command completion event */
  547. struct xhci_trb_complete complete;
  548. /** Port status changed event */
  549. struct xhci_trb_port_status port;
  550. } __attribute__ (( packed ));
  551. /** An input control context */
  552. struct xhci_control_context {
  553. /** Drop context flags */
  554. uint32_t drop;
  555. /** Add context flags */
  556. uint32_t add;
  557. /** Reserved */
  558. uint32_t reserved_a[5];
  559. /** Configuration value */
  560. uint8_t config;
  561. /** Interface number */
  562. uint8_t intf;
  563. /** Alternate setting */
  564. uint8_t alt;
  565. /** Reserved */
  566. uint8_t reserved_b;
  567. } __attribute__ (( packed ));
  568. /** A slot context */
  569. struct xhci_slot_context {
  570. /** Device info */
  571. uint32_t info;
  572. /** Maximum exit latency */
  573. uint16_t latency;
  574. /** Root hub port number */
  575. uint8_t port;
  576. /** Number of downstream ports */
  577. uint8_t ports;
  578. /** TT hub slot ID */
  579. uint8_t tt_id;
  580. /** TT port number */
  581. uint8_t tt_port;
  582. /** Interrupter target */
  583. uint16_t intr;
  584. /** USB address */
  585. uint8_t address;
  586. /** Reserved */
  587. uint16_t reserved_a;
  588. /** Slot state */
  589. uint8_t state;
  590. /** Reserved */
  591. uint32_t reserved_b[4];
  592. } __attribute__ (( packed ));
  593. /** Construct slot context device info */
  594. #define XHCI_SLOT_INFO( entries, hub, speed, route ) \
  595. ( ( (entries) << 27 ) | ( (hub) << 26 ) | ( (speed) << 20 ) | (route) )
  596. /** An endpoint context */
  597. struct xhci_endpoint_context {
  598. /** Endpoint state */
  599. uint8_t state;
  600. /** Stream configuration */
  601. uint8_t stream;
  602. /** Polling interval */
  603. uint8_t interval;
  604. /** Max ESIT payload high */
  605. uint8_t esit_high;
  606. /** Endpoint type */
  607. uint8_t type;
  608. /** Maximum burst size */
  609. uint8_t burst;
  610. /** Maximum packet size */
  611. uint16_t mtu;
  612. /** Transfer ring dequeue pointer */
  613. uint64_t dequeue;
  614. /** Average TRB length */
  615. uint16_t trb_len;
  616. /** Max ESIT payload low */
  617. uint16_t esit_low;
  618. /** Reserved */
  619. uint32_t reserved[3];
  620. } __attribute__ (( packed ));
  621. /** Endpoint states */
  622. enum xhci_endpoint_state {
  623. /** Endpoint is disabled */
  624. XHCI_ENDPOINT_DISABLED = 0,
  625. /** Endpoint is running */
  626. XHCI_ENDPOINT_RUNNING = 1,
  627. /** Endpoint is halted due to a USB Halt condition */
  628. XHCI_ENDPOINT_HALTED = 2,
  629. /** Endpoint is stopped */
  630. XHCI_ENDPOINT_STOPPED = 3,
  631. /** Endpoint is halted due to a TRB error */
  632. XHCI_ENDPOINT_ERROR = 4,
  633. };
  634. /** Endpoint state mask */
  635. #define XHCI_ENDPOINT_STATE_MASK 0x07
  636. /** Endpoint type */
  637. #define XHCI_EP_TYPE(type) ( (type) << 3 )
  638. /** Control endpoint type */
  639. #define XHCI_EP_TYPE_CONTROL XHCI_EP_TYPE ( 4 )
  640. /** Input endpoint type */
  641. #define XHCI_EP_TYPE_IN XHCI_EP_TYPE ( 4 )
  642. /** Endpoint dequeue cycle state */
  643. #define XHCI_EP_DCS 0x00000001UL
  644. /** Control endpoint average TRB length */
  645. #define XHCI_EP0_TRB_LEN 8
  646. /** An event ring segment */
  647. struct xhci_event_ring_segment {
  648. /** Base address */
  649. uint64_t base;
  650. /** Number of TRBs */
  651. uint32_t count;
  652. /** Reserved */
  653. uint32_t reserved;
  654. } __attribute__ (( packed ));
  655. /** A transfer request block command/transfer ring */
  656. struct xhci_trb_ring {
  657. /** Producer counter */
  658. unsigned int prod;
  659. /** Consumer counter */
  660. unsigned int cons;
  661. /** Ring size (log2) */
  662. unsigned int shift;
  663. /** Ring counter mask */
  664. unsigned int mask;
  665. /** I/O buffers */
  666. struct io_buffer **iobuf;
  667. /** Transfer request blocks */
  668. union xhci_trb *trb;
  669. /** Length of transfer request blocks */
  670. size_t len;
  671. /** Link TRB (if applicable) */
  672. struct xhci_trb_link *link;
  673. /** Doorbell register */
  674. void *db;
  675. /** Doorbell register value */
  676. uint32_t dbval;
  677. };
  678. /** An event ring */
  679. struct xhci_event_ring {
  680. /** Consumer counter */
  681. unsigned int cons;
  682. /** Event ring segment table */
  683. struct xhci_event_ring_segment *segment;
  684. /** Transfer request blocks */
  685. union xhci_trb *trb;
  686. };
  687. /**
  688. * Calculate doorbell register value
  689. *
  690. * @v target Doorbell target
  691. * @v stream Doorbell stream ID
  692. * @ret dbval Doorbell register value
  693. */
  694. #define XHCI_DBVAL( target, stream ) ( (target) | ( (stream) << 16 ) )
  695. /**
  696. * Calculate space used in TRB ring
  697. *
  698. * @v ring TRB ring
  699. * @ret fill Number of entries used
  700. */
  701. static inline __attribute__ (( always_inline )) unsigned int
  702. xhci_ring_fill ( struct xhci_trb_ring *ring ) {
  703. return ( ring->prod - ring->cons );
  704. }
  705. /**
  706. * Calculate space remaining in TRB ring
  707. *
  708. * @v ring TRB ring
  709. * @ret remaining Number of entries remaining
  710. *
  711. * xHCI does not allow us to completely fill a ring; there must be at
  712. * least one free entry (excluding the Link TRB).
  713. */
  714. static inline __attribute__ (( always_inline )) unsigned int
  715. xhci_ring_remaining ( struct xhci_trb_ring *ring ) {
  716. unsigned int fill = xhci_ring_fill ( ring );
  717. /* We choose to utilise rings with ( 2^n + 1 ) entries, with
  718. * the final entry being a Link TRB. The maximum fill level
  719. * is therefore
  720. *
  721. * ( ( 2^n + 1 ) - 1 (Link TRB) - 1 (one slot always empty)
  722. * == ( 2^n - 1 )
  723. *
  724. * which is therefore equal to the ring mask.
  725. */
  726. assert ( fill <= ring->mask );
  727. return ( ring->mask - fill );
  728. }
  729. /**
  730. * Calculate physical address of most recently consumed TRB
  731. *
  732. * @v ring TRB ring
  733. * @ret trb TRB physical address
  734. */
  735. static inline __attribute__ (( always_inline )) physaddr_t
  736. xhci_ring_consumed ( struct xhci_trb_ring *ring ) {
  737. unsigned int index = ( ( ring->cons - 1 ) & ring->mask );
  738. return virt_to_phys ( &ring->trb[index] );
  739. }
  740. /** Slot context index */
  741. #define XHCI_CTX_SLOT 0
  742. /** Calculate context index from USB endpoint address */
  743. #define XHCI_CTX(address) \
  744. ( (address) ? ( ( ( (address) & 0x0f ) << 1 ) | \
  745. ( ( (address) & 0x80 ) >> 7 ) ) : 1 )
  746. /** Endpoint zero context index */
  747. #define XHCI_CTX_EP0 XHCI_CTX ( 0x00 )
  748. /** End of contexts */
  749. #define XHCI_CTX_END 32
  750. /** Device context index */
  751. #define XHCI_DCI(ctx) ( (ctx) + 0 )
  752. /** Input context index */
  753. #define XHCI_ICI(ctx) ( (ctx) + 1 )
  754. /** Number of TRBs (excluding Link TRB) in the command ring
  755. *
  756. * This is a policy decision.
  757. */
  758. #define XHCI_CMD_TRBS_LOG2 2
  759. /** Number of TRBs in the event ring
  760. *
  761. * This is a policy decision.
  762. */
  763. #define XHCI_EVENT_TRBS_LOG2 6
  764. /** Number of TRBs in a transfer ring
  765. *
  766. * This is a policy decision.
  767. */
  768. #define XHCI_TRANSFER_TRBS_LOG2 6
  769. /** Maximum time to wait for BIOS to release ownership
  770. *
  771. * This is a policy decision.
  772. */
  773. #define XHCI_USBLEGSUP_MAX_WAIT_MS 100
  774. /** Maximum time to wait for host controller to stop
  775. *
  776. * This is a policy decision.
  777. */
  778. #define XHCI_STOP_MAX_WAIT_MS 100
  779. /** Maximum time to wait for reset to complete
  780. *
  781. * This is a policy decision.
  782. */
  783. #define XHCI_RESET_MAX_WAIT_MS 500
  784. /** Maximum time to wait for a command to complete
  785. *
  786. * This is a policy decision.
  787. */
  788. #define XHCI_COMMAND_MAX_WAIT_MS 500
  789. /** Maximum time to wait for a port reset to complete
  790. *
  791. * This is a policy decision.
  792. */
  793. #define XHCI_PORT_RESET_MAX_WAIT_MS 500
  794. /** An xHCI device */
  795. struct xhci_device {
  796. /** Registers */
  797. void *regs;
  798. /** Capability registers */
  799. void *cap;
  800. /** Operational registers */
  801. void *op;
  802. /** Runtime registers */
  803. void *run;
  804. /** Doorbell registers */
  805. void *db;
  806. /** Number of device slots */
  807. unsigned int slots;
  808. /** Number of interrupters */
  809. unsigned int intrs;
  810. /** Number of ports */
  811. unsigned int ports;
  812. /** Number of page-sized scratchpad buffers */
  813. unsigned int scratchpads;
  814. /** 64-bit addressing capability */
  815. int addr64;
  816. /** Context size shift */
  817. unsigned int csz_shift;
  818. /** xHCI extended capabilities offset */
  819. unsigned int xecp;
  820. /** Page size */
  821. size_t pagesize;
  822. /** USB legacy support capability (if present and enabled) */
  823. unsigned int legacy;
  824. /** Device context base address array */
  825. uint64_t *dcbaa;
  826. /** Scratchpad buffer area */
  827. userptr_t scratchpad;
  828. /** Scratchpad buffer array */
  829. uint64_t *scratchpad_array;
  830. /** Command ring */
  831. struct xhci_trb_ring command;
  832. /** Event ring */
  833. struct xhci_event_ring event;
  834. /** Current command completion buffer (if any) */
  835. union xhci_trb *completion;
  836. /** Device slots, indexed by slot ID */
  837. struct xhci_slot **slot;
  838. /** USB bus */
  839. struct usb_bus *bus;
  840. };
  841. /** An xHCI device slot */
  842. struct xhci_slot {
  843. /** xHCI device */
  844. struct xhci_device *xhci;
  845. /** USB device */
  846. struct usb_device *usb;
  847. /** Slot ID */
  848. unsigned int id;
  849. /** Slot context */
  850. struct xhci_slot_context *context;
  851. /** Route string */
  852. unsigned int route;
  853. /** Root hub port number */
  854. unsigned int port;
  855. /** Protocol speed ID */
  856. unsigned int psiv;
  857. /** Endpoints, indexed by context ID */
  858. struct xhci_endpoint *endpoint[XHCI_CTX_END];
  859. };
  860. /** An xHCI endpoint */
  861. struct xhci_endpoint {
  862. /** xHCI device */
  863. struct xhci_device *xhci;
  864. /** xHCI slot */
  865. struct xhci_slot *slot;
  866. /** USB endpoint */
  867. struct usb_endpoint *ep;
  868. /** Context index */
  869. unsigned int ctx;
  870. /** Endpoint type */
  871. unsigned int type;
  872. /** Endpoint context */
  873. struct xhci_endpoint_context *context;
  874. /** Transfer ring */
  875. struct xhci_trb_ring ring;
  876. };
  877. #endif /* _IPXE_XHCI_H */