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

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