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 27KB

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