xhci.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. #include "xhci.h"
  2. #include <common/kprint.h>
  3. #include <debug/bug.h>
  4. #include <common/spinlock.h>
  5. #include <mm/mm.h>
  6. #include <mm/slab.h>
  7. #include <debug/traceback/traceback.h>
  8. #include <common/time.h>
  9. #include <exception/irq.h>
  10. #include <driver/interrupt/apic/apic.h>
  11. #pragma GCC push_options
  12. #pragma GCC optimize("O0")
  13. spinlock_t xhci_controller_init_lock = {0}; // xhci控制器初始化锁(在usb_init中被初始化)
  14. static int xhci_ctrl_count = 0; // xhci控制器计数
  15. static struct xhci_host_controller_t xhci_hc[XHCI_MAX_HOST_CONTROLLERS] = {0};
  16. void xhci_hc_irq_enable(uint64_t irq_num);
  17. void xhci_hc_irq_disable(uint64_t irq_num);
  18. uint64_t xhci_hc_irq_install(uint64_t irq_num, void *arg);
  19. void xhci_hc_irq_uninstall(uint64_t irq_num);
  20. static int xhci_hc_find_available_id();
  21. static int xhci_hc_stop(int id);
  22. static int xhci_hc_reset(int id);
  23. static int xhci_hc_stop_legacy(int id);
  24. static int xhci_hc_start_sched(int id);
  25. static int xhci_hc_stop_sched(int id);
  26. static uint32_t xhci_hc_get_protocol_offset(int id, uint32_t list_off, const int version, uint32_t *offset, uint32_t *count, uint16_t *protocol_flag);
  27. static int xhci_hc_pair_ports(int id);
  28. static uint64_t xhci_create_ring(int trbs);
  29. static uint64_t xhci_create_event_ring(int trbs, uint64_t *ret_ring_addr);
  30. void xhci_hc_irq_handler(uint64_t irq_num, uint64_t cid, struct pt_regs *regs);
  31. static int xhci_hc_init_intr(int id);
  32. static int xhci_hc_start_ports(int id);
  33. hardware_intr_controller xhci_hc_intr_controller =
  34. {
  35. .enable = xhci_hc_irq_enable,
  36. .disable = xhci_hc_irq_disable,
  37. .install = xhci_hc_irq_install,
  38. .uninstall = xhci_hc_irq_uninstall,
  39. .ack = apic_local_apic_edge_ack,
  40. };
  41. /*
  42. 注意!!!
  43. 尽管采用MMI/O的方式访问寄存器,但是对于指定大小的寄存器,
  44. 在发起读请求的时候,只能从寄存器的起始地址位置开始读取。
  45. 例子:不能在一个32bit的寄存器中的偏移量8的位置开始读取1个字节
  46. 这种情况下,我们必须从32bit的寄存器的0地址处开始读取32bit,然后通过移位的方式得到其中的字节。
  47. */
  48. #define xhci_read_cap_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase + offset))
  49. #define xhci_get_ptr_cap_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase + offset))
  50. #define xhci_write_cap_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase + offset) = (uint8_t)value)
  51. #define xhci_read_cap_reg32(id, offset) (*(uint32_t *)(xhci_hc[id].vbase + offset))
  52. #define xhci_get_ptr_cap_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase + offset))
  53. #define xhci_write_cap_reg32(id, offset, value) (*(uint32_t *)(xhci_hc[id].vbase + offset) = (uint32_t)value)
  54. #define xhci_read_cap_reg64(id, offset) (*(uint64_t *)(xhci_hc[id].vbase + offset))
  55. #define xhci_get_ptr_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase + offset))
  56. #define xhci_write_cap_reg64(id, offset, value) (*(uint64_t *)(xhci_hc[id].vbase + offset) = (uint64_t)value)
  57. #define xhci_read_op_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase_op + offset))
  58. #define xhci_get_ptr_op_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase_op + offset))
  59. #define xhci_write_op_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase_op + offset) = (uint8_t)value)
  60. #define xhci_read_op_reg32(id, offset) (*(uint32_t *)(xhci_hc[id].vbase_op + offset))
  61. #define xhci_get_ptr_op_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase_op + offset))
  62. #define xhci_write_op_reg32(id, offset, value) (*(uint32_t *)(xhci_hc[id].vbase_op + offset) = (uint32_t)value)
  63. #define xhci_read_op_reg64(id, offset) (*(uint64_t *)(xhci_hc[id].vbase_op + offset))
  64. #define xhci_get_ptr_op_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase_op + offset))
  65. #define xhci_write_op_reg64(id, offset, value) (*(uint64_t *)(xhci_hc[id].vbase_op + offset) = (uint64_t)value)
  66. /**
  67. * @brief 计算中断寄存器组虚拟地址
  68. * @param id 主机控制器id
  69. * @param num xhci中断寄存器组号
  70. */
  71. #define xhci_calc_intr_vaddr(id, num) (xhci_hc[id].vbase + xhci_hc[id].rts_offset + XHCI_RT_IR0 + num * XHCI_IR_SIZE)
  72. /**
  73. * @brief 读取/写入中断寄存器
  74. * @param id 主机控制器id
  75. * @param num xhci中断寄存器组号
  76. * @param intr_offset 寄存器在当前寄存器组中的偏移量
  77. */
  78. #define xhci_read_intr_reg32(id, num, intr_offset) (*(uint32_t *)(xhci_calc_intr_vaddr(id, num) + intr_offset))
  79. #define xhci_write_intr_reg32(id, num, intr_offset, value) (*(uint32_t *)(xhci_calc_intr_vaddr(id, num) + intr_offset) = value)
  80. #define xhci_read_intr_reg64(id, num, intr_offset) (*(uint64_t *)(xhci_calc_intr_vaddr(id, num) + intr_offset))
  81. #define xhci_write_intr_reg64(id, num, intr_offset, value) (*(uint64_t *)(xhci_calc_intr_vaddr(id, num) + intr_offset) = value)
  82. #define xhci_is_aligned64(addr) ((addr & 0x3f) == 0) // 是否64bytes对齐
  83. /**
  84. * @brief 判断端口信息
  85. * @param cid 主机控制器id
  86. * @param pid 端口id
  87. */
  88. #define XHCI_PORT_IS_USB2(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB2)
  89. #define XHCI_PORT_IS_USB3(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB3)
  90. #define XHCI_PORT_IS_USB2_HSO(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HSO) == XHCI_PROTOCOL_HSO)
  91. #define XHCI_PORT_HAS_PAIR(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HAS_PAIR) == XHCI_PROTOCOL_HAS_PAIR)
  92. #define XHCI_PORT_IS_ACTIVE(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_ACTIVE) == XHCI_PROTOCOL_ACTIVE)
  93. /**
  94. * @brief 设置link TRB的命令(dword3)
  95. *
  96. */
  97. #define xhci_TRB_set_link_cmd(trb_vaddr) \
  98. do \
  99. { \
  100. struct xhci_TRB_normal_t *ptr = (struct xhci_TRB_normal_t *)(trb_vaddr); \
  101. ptr->TRB_type = TRB_TYPE_LINK; \
  102. ptr->ioc = 0; \
  103. ptr->chain = 0; \
  104. ptr->ent = 0; \
  105. ptr->cycle = 1; \
  106. } while (0)
  107. // Common TRB types
  108. enum
  109. {
  110. TRB_TYPE_NORMAL = 1,
  111. TRB_TYPE_SETUP_STAGE,
  112. TRB_TYPE_DATA_STAGE,
  113. TRB_TYPE_STATUS_STAGE,
  114. TRB_TYPE_ISOCH,
  115. TRB_TYPE_LINK,
  116. TRB_TYPE_EVENT_DATA,
  117. TRB_TYPE_NO_OP,
  118. TRB_TYPE_ENABLE_SLOT,
  119. TRB_TYPE_DISABLE_SLOT = 10,
  120. TRB_TYPE_ADDRESS_DEVICE = 11,
  121. TRB_TYPE_CONFIG_EP,
  122. TRB_TYPE_EVALUATE_CONTEXT,
  123. TRB_TYPE_RESET_EP,
  124. TRB_TYPE_STOP_EP = 15,
  125. TRB_TYPE_SET_TR_DEQUEUE,
  126. TRB_TYPE_RESET_DEVICE,
  127. TRB_TYPE_FORCE_EVENT,
  128. TRB_TYPE_DEG_BANDWIDTH,
  129. TRB_TYPE_SET_LAT_TOLERANCE = 20,
  130. TRB_TYPE_GET_PORT_BAND = 21,
  131. TRB_TYPE_FORCE_HEADER,
  132. TRB_TYPE_NO_OP_CMD, // 24 - 31 = reserved
  133. TRB_TYPE_TRANS_EVENT = 32,
  134. TRB_TYPE_COMMAND_COMPLETION,
  135. TRB_TYPE_PORT_STATUS_CHANGE,
  136. TRB_TYPE_BANDWIDTH_REQUEST,
  137. TRB_TYPE_DOORBELL_EVENT,
  138. TRB_TYPE_HOST_CONTROLLER_EVENT = 37,
  139. TRB_TYPE_DEVICE_NOTIFICATION,
  140. TRB_TYPE_MFINDEX_WRAP,
  141. // 40 - 47 = reserved
  142. // 48 - 63 = Vendor Defined
  143. };
  144. /**
  145. * @brief 在controller数组之中寻找可用插槽
  146. *
  147. * 注意:该函数只能被获得init锁的进程所调用
  148. * @return int 可用id(无空位时返回-1)
  149. */
  150. static int xhci_hc_find_available_id()
  151. {
  152. if (unlikely(xhci_ctrl_count >= XHCI_MAX_HOST_CONTROLLERS))
  153. return -1;
  154. for (int i = 0; i < XHCI_MAX_HOST_CONTROLLERS; ++i)
  155. {
  156. if (xhci_hc[i].pci_dev_hdr == NULL)
  157. return i;
  158. }
  159. return -1;
  160. }
  161. /**
  162. * @brief 停止xhci主机控制器
  163. *
  164. * @param id 主机控制器id
  165. * @return int
  166. */
  167. static int xhci_hc_stop(int id)
  168. {
  169. // 判断是否已经停止
  170. if (unlikely((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 1))
  171. return 0;
  172. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00000000);
  173. char timeout = 17;
  174. while ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
  175. {
  176. usleep(1000);
  177. if (--timeout == 0)
  178. return -ETIMEDOUT;
  179. }
  180. return 0;
  181. }
  182. /**
  183. * @brief reset xHCI主机控制器
  184. *
  185. * @param id 主机控制器id
  186. * @return int
  187. */
  188. static int xhci_hc_reset(int id)
  189. {
  190. int retval = 0;
  191. kdebug("usbsts=%#010lx", xhci_read_op_reg32(id, XHCI_OPS_USBSTS));
  192. // 判断HCHalted是否置位
  193. if ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
  194. {
  195. kdebug("stopping usb hc...");
  196. // 未置位,需要先尝试停止usb主机控制器
  197. retval = xhci_hc_stop(id);
  198. if (unlikely(retval))
  199. return retval;
  200. }
  201. int timeout = 500; // wait 500ms
  202. // reset
  203. uint32_t cmd = xhci_read_op_reg32(id, XHCI_OPS_USBCMD);
  204. kdebug("cmd=%#010lx", cmd);
  205. cmd |= (1 << 1);
  206. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, cmd);
  207. kdebug("after rst, sts=%#010lx", xhci_read_op_reg32(id, XHCI_OPS_USBSTS));
  208. while (xhci_read_op_reg32(id, XHCI_OPS_USBCMD) & (1 << 1))
  209. {
  210. usleep(1000);
  211. if (--timeout == 0)
  212. return -ETIMEDOUT;
  213. }
  214. // kdebug("reset done!, timeout=%d", timeout);
  215. return retval;
  216. }
  217. /**
  218. * @brief 停止指定xhci控制器的legacy support
  219. *
  220. * @param id 控制器id
  221. * @return int
  222. */
  223. static int xhci_hc_stop_legacy(int id)
  224. {
  225. uint64_t current_offset = xhci_hc[id].ext_caps_off;
  226. do
  227. {
  228. // 判断当前entry是否为legacy support entry
  229. if (xhci_read_cap_reg8(id, current_offset) == XHCI_XECP_ID_LEGACY)
  230. {
  231. // 接管控制权
  232. xhci_write_cap_reg32(id, current_offset, xhci_read_cap_reg32(id, current_offset) | XHCI_XECP_LEGACY_OS_OWNED);
  233. // 等待响应完成
  234. int timeout = XHCI_XECP_LEGACY_TIMEOUT;
  235. while ((xhci_read_cap_reg32(id, current_offset) & XHCI_XECP_LEGACY_OWNING_MASK) != XHCI_XECP_LEGACY_OS_OWNED)
  236. {
  237. usleep(1000);
  238. if (--timeout == 0)
  239. {
  240. kerror("The BIOS doesn't stop legacy support.");
  241. return -ETIMEDOUT;
  242. }
  243. }
  244. // 处理完成
  245. return 0;
  246. }
  247. // 读取下一个entry的偏移增加量
  248. int next_off = ((xhci_read_cap_reg32(id, current_offset) & 0xff00) >> 8) << 2;
  249. // 将指针跳转到下一个entry
  250. current_offset = next_off ? (current_offset + next_off) : 0;
  251. } while (current_offset);
  252. // 当前controller不存在legacy支持,也问题不大,不影响
  253. return 0;
  254. }
  255. /**
  256. * @brief 启用指定xhci控制器的调度
  257. *
  258. * @param id 控制器id
  259. * @return int
  260. */
  261. static int xhci_hc_start_sched(int id)
  262. {
  263. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, (1 << 0) | (1 >> 2) | (1 << 3));
  264. usleep(100 * 1000);
  265. }
  266. /**
  267. * @brief 停止指定xhci控制器的调度
  268. *
  269. * @param id 控制器id
  270. * @return int
  271. */
  272. static int xhci_hc_stop_sched(int id)
  273. {
  274. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00);
  275. }
  276. /**
  277. * @brief
  278. *
  279. * @return uint32_t
  280. */
  281. /**
  282. * @brief 在Ex capability list中寻找符合指定的协议号的寄存器offset、count、flag信息
  283. *
  284. * @param id 主机控制器id
  285. * @param list_off 列表项位置距离控制器虚拟基地址的偏移量
  286. * @param version 要寻找的端口版本号(2或3)
  287. * @param offset 返回的 Compatible Port Offset
  288. * @param count 返回的 Compatible Port Count
  289. * @param protocol_flag 返回的与协议相关的flag
  290. * @return uint32_t 下一个列表项的偏移量
  291. */
  292. static uint32_t xhci_hc_get_protocol_offset(int id, uint32_t list_off, const int version, uint32_t *offset, uint32_t *count, uint16_t *protocol_flag)
  293. {
  294. if (count)
  295. *count = 0;
  296. do
  297. {
  298. uint32_t dw0 = xhci_read_cap_reg32(id, list_off);
  299. uint32_t next_list_off = (dw0 >> 8) & 0xff;
  300. next_list_off = next_list_off ? (list_off + (next_list_off << 2)) : 0;
  301. if ((dw0 & 0xff) == XHCI_XECP_ID_PROTOCOL && ((dw0 & 0xff000000) >> 24) == version)
  302. {
  303. uint32_t dw2 = xhci_read_cap_reg32(id, list_off + 8);
  304. if (offset != NULL)
  305. *offset = (uint32_t)(dw2 & 0xff) - 1; // 使其转换为zero based
  306. if (count != NULL)
  307. *count = (uint32_t)((dw2 & 0xff00) >> 8);
  308. if (protocol_flag != NULL && version == 2)
  309. *protocol_flag = (uint16_t)((dw2 >> 16) & 0x0fff);
  310. return next_list_off;
  311. }
  312. list_off = next_list_off;
  313. } while (list_off);
  314. return 0;
  315. }
  316. /**
  317. * @brief 配对xhci主机控制器的usb2、usb3端口
  318. *
  319. * @param id 主机控制器id
  320. * @return int 返回码
  321. */
  322. static int xhci_hc_pair_ports(int id)
  323. {
  324. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  325. memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
  326. // 从hcs1获取端口数量
  327. xhci_hc[id].port_num = hcs1.max_ports;
  328. // 找到所有的端口并标记其端口信息
  329. xhci_hc[id].port_num_u2 = 0;
  330. xhci_hc[id].port_num_u3 = 0;
  331. uint32_t next_off = xhci_hc[id].ext_caps_off;
  332. uint32_t offset, cnt;
  333. uint16_t protocol_flags = 0;
  334. // 寻找所有的usb2端口
  335. while (next_off)
  336. {
  337. next_off = xhci_hc_get_protocol_offset(id, next_off, 2, &offset, &cnt, &protocol_flags);
  338. if (cnt)
  339. {
  340. for (int i = 0; i < cnt; ++i)
  341. {
  342. xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u2++;
  343. xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB2;
  344. // usb2 high speed only
  345. if (protocol_flags & 2)
  346. xhci_hc[id].ports[offset + i].flags |= XHCI_PROTOCOL_HSO;
  347. }
  348. }
  349. }
  350. // 寻找所有的usb3端口
  351. next_off = xhci_hc[id].ext_caps_off;
  352. while (next_off)
  353. {
  354. next_off = xhci_hc_get_protocol_offset(id, next_off, 3, &offset, &cnt, &protocol_flags);
  355. if (cnt)
  356. {
  357. for (int i = 0; i < cnt; ++i)
  358. {
  359. xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u3++;
  360. xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB3;
  361. }
  362. }
  363. }
  364. // 将对应的USB2端口和USB3端口进行配对
  365. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  366. {
  367. for (int j = 0; j < xhci_hc[id].port_num; ++j)
  368. {
  369. if (unlikely(i == j))
  370. continue;
  371. if ((xhci_hc[id].ports[i].offset == xhci_hc[id].ports[j].offset) &&
  372. ((xhci_hc[id].ports[i].flags & XHCI_PROTOCOL_INFO) != (xhci_hc[id].ports[j].flags & XHCI_PROTOCOL_INFO)))
  373. {
  374. xhci_hc[id].ports[i].paired_port_num = j;
  375. xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_HAS_PAIR;
  376. xhci_hc[id].ports[j].paired_port_num = i;
  377. xhci_hc[id].ports[j].flags |= XHCI_PROTOCOL_HAS_PAIR;
  378. }
  379. }
  380. }
  381. // 标记所有的usb3、单独的usb2端口为激活状态
  382. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  383. {
  384. if (XHCI_PORT_IS_USB3(id, i) ||
  385. (XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i))))
  386. xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_ACTIVE;
  387. }
  388. kinfo("Found %d ports on root hub, usb2 ports:%d, usb3 ports:%d", xhci_hc[id].port_num, xhci_hc[id].port_num_u2, xhci_hc[id].port_num_u3);
  389. /*
  390. // 打印配对结果
  391. for (int i = 1; i <= xhci_hc[id].port_num; ++i)
  392. {
  393. if (XHCI_PORT_IS_USB3(id, i))
  394. {
  395. kdebug("USB3 port %d, offset=%d, pair with usb2 port %d, current port is %s", i, xhci_hc[id].ports[i].offset,
  396. xhci_hc[id].ports[i].paired_port_num, XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive");
  397. }
  398. else if (XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i))) // 单独的2.0接口
  399. {
  400. kdebug("Stand alone USB2 port %d, offset=%d, current port is %s", i, xhci_hc[id].ports[i].offset,
  401. XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive");
  402. }
  403. else if (XHCI_PORT_IS_USB2(id, i))
  404. {
  405. kdebug("USB2 port %d, offset=%d, current port is %s, has pair=%s", i, xhci_hc[id].ports[i].offset,
  406. XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive", XHCI_PORT_HAS_PAIR(id, i) ? "true" : "false");
  407. }
  408. }
  409. */
  410. return 0;
  411. }
  412. /**
  413. * @brief 创建ring,并将最后一个trb指向头一个trb
  414. *
  415. * @param trbs 要创建的trb数量
  416. * @return uint64_t trb数组的起始虚拟地址
  417. */
  418. static uint64_t xhci_create_ring(int trbs)
  419. {
  420. int total_size = trbs * sizeof(struct xhci_TRB_t);
  421. const uint64_t vaddr = (uint64_t)kmalloc(total_size, 0);
  422. memset((void *)vaddr, 0, total_size);
  423. // 设置最后一个trb为link trb
  424. xhci_TRB_set_link_cmd(vaddr + total_size - sizeof(struct xhci_TRB_t));
  425. return vaddr;
  426. }
  427. /**
  428. * @brief 创建新的event ring table和对应的ring segment
  429. *
  430. * @param trbs 包含的trb的数量
  431. * @param ret_ring_addr 返回的第一个event ring segment的基地址(虚拟)
  432. * @return uint64_t trb table的虚拟地址
  433. */
  434. static uint64_t xhci_create_event_ring(int trbs, uint64_t *ret_ring_addr)
  435. {
  436. const uint64_t table_vaddr = (const uint64_t)kmalloc(64, 0); // table支持8个segment
  437. if (unlikely(table_vaddr == NULL))
  438. return -ENOMEM;
  439. memset((void *)table_vaddr, 0, 64);
  440. // 暂时只创建1个segment
  441. const uint64_t seg_vaddr = (const uint64_t)kmalloc(trbs * sizeof(struct xhci_TRB_t), 0);
  442. if (unlikely(seg_vaddr == NULL))
  443. return -ENOMEM;
  444. memset((void *)seg_vaddr, 0, trbs * sizeof(struct xhci_TRB_t));
  445. // 将segment地址和大小写入table
  446. *(uint64_t *)(table_vaddr) = virt_2_phys(seg_vaddr);
  447. *(uint64_t *)(table_vaddr + 8) = trbs;
  448. *ret_ring_addr = seg_vaddr;
  449. return table_vaddr;
  450. }
  451. void xhci_hc_irq_enable(uint64_t irq_num)
  452. {
  453. int cid = xhci_find_hcid_by_irq_num(irq_num);
  454. if (WARN_ON(cid == -1))
  455. return;
  456. kdebug("start msi");
  457. pci_start_msi(xhci_hc[cid].pci_dev_hdr);
  458. kdebug("start sched");
  459. xhci_hc_start_sched(cid);
  460. kdebug("start ports");
  461. xhci_hc_start_ports(cid);
  462. kdebug("enabled");
  463. }
  464. void xhci_hc_irq_disable(uint64_t irq_num)
  465. {
  466. int cid = xhci_find_hcid_by_irq_num(irq_num);
  467. if (WARN_ON(cid == -1))
  468. return;
  469. xhci_hc_stop_sched(cid);
  470. pci_disable_msi(xhci_hc[cid].pci_dev_hdr);
  471. }
  472. uint64_t xhci_hc_irq_install(uint64_t irq_num, void *arg)
  473. {
  474. int cid = xhci_find_hcid_by_irq_num(irq_num);
  475. if (WARN_ON(cid == -1))
  476. return -EINVAL;
  477. struct xhci_hc_irq_install_info_t *info = (struct xhci_hc_irq_install_info_t *)arg;
  478. struct msi_desc_t msi_desc;
  479. memset(&msi_desc, 0, sizeof(struct msi_desc_t));
  480. msi_desc.pci_dev = (struct pci_device_structure_header_t *)xhci_hc[cid].pci_dev_hdr;
  481. msi_desc.assert = info->assert;
  482. msi_desc.edge_trigger = info->edge_trigger;
  483. msi_desc.processor = info->processor;
  484. msi_desc.pci.msi_attribute.is_64 = 1;
  485. // todo: QEMU是使用msix的,因此要先在pci中实现msix
  486. int retval = pci_enable_msi(&msi_desc);
  487. kdebug("pci retval = %d", retval);
  488. kdebug("xhci irq %d installed.", irq_num);
  489. return 0;
  490. }
  491. void xhci_hc_irq_uninstall(uint64_t irq_num)
  492. {
  493. // todo
  494. int cid = xhci_find_hcid_by_irq_num(irq_num);
  495. if (WARN_ON(cid == -1))
  496. return;
  497. xhci_hc_stop(cid);
  498. }
  499. /**
  500. * @brief xhci主机控制器的中断处理函数
  501. *
  502. * @param irq_num 中断向量号
  503. * @param cid 控制器号
  504. * @param regs 寄存器值
  505. */
  506. void xhci_hc_irq_handler(uint64_t irq_num, uint64_t cid, struct pt_regs *regs)
  507. {
  508. // todo: handle irq
  509. kdebug("USB irq received.");
  510. }
  511. /**
  512. * @brief 重置端口
  513. *
  514. * @param id 控制器id
  515. * @param port 端口id
  516. * @return int
  517. */
  518. static int xhci_reset_port(const int id, const int port)
  519. {
  520. int retval = 0;
  521. // 相对于op寄存器基地址的偏移量
  522. uint64_t port_status_offset = XHCI_OPS_PRS + port * 16;
  523. // kdebug("to reset %d, offset=%#018lx", port, port_status_offset);
  524. // 检查端口电源状态
  525. if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0)
  526. {
  527. kdebug("port is power off, starting...");
  528. xhci_write_cap_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9));
  529. usleep(2000);
  530. // 检测端口是否被启用, 若未启用,则报错
  531. if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0)
  532. {
  533. kdebug("cannot power on %d", port);
  534. return -EAGAIN;
  535. }
  536. }
  537. // kdebug("port:%d, power check ok", port);
  538. // 确保端口的status被清0
  539. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS);
  540. // 重置当前端口
  541. if (XHCI_PORT_IS_USB3(id, port))
  542. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | (1 << 31));
  543. else
  544. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | (1 << 4));
  545. retval = -ETIMEDOUT;
  546. // 等待portsc的port reset change位被置位,说明reset完成
  547. int timeout = 200;
  548. while (timeout)
  549. {
  550. uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC);
  551. if (XHCI_PORT_IS_USB3(id, port) && (val & (1 << 31)) == 0)
  552. break;
  553. else if (XHCI_PORT_IS_USB2(id, port) && (val & (1 << 4)) == 0)
  554. break;
  555. else if (val & (1 << 21))
  556. break;
  557. --timeout;
  558. usleep(500);
  559. }
  560. // kdebug("timeout= %d", timeout);
  561. if (timeout > 0)
  562. {
  563. // 等待恢复
  564. usleep(USB_TIME_RST_REC * 1000);
  565. uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC);
  566. // 如果reset之后,enable bit仍然是1,那么说明reset成功
  567. if (val & (1 << 1))
  568. {
  569. // 清除status change bit
  570. xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS);
  571. }
  572. retval = 0;
  573. }
  574. // 如果usb2端口成功reset,则处理该端口的active状态
  575. if (retval == 0 && XHCI_PORT_IS_USB2(id, port))
  576. {
  577. xhci_hc[id].ports[port].flags |= XHCI_PROTOCOL_ACTIVE;
  578. if (XHCI_PORT_HAS_PAIR(id, port)) // 如果有对应的usb3端口,则将usb3端口设置为未激活
  579. xhci_hc[id].ports[xhci_hc[id].ports[port].paired_port_num].flags &= ~(XHCI_PROTOCOL_ACTIVE);
  580. }
  581. // 如果usb3端口reset失败,则启用与之配对的usb2端口
  582. if (retval != 0 && XHCI_PORT_IS_USB3(id, port))
  583. {
  584. xhci_hc[id].ports[port].flags &= ~XHCI_PROTOCOL_ACTIVE;
  585. xhci_hc[id].ports[xhci_hc[id].ports[port].paired_port_num].flags |= XHCI_PROTOCOL_ACTIVE;
  586. }
  587. return retval;
  588. }
  589. /**
  590. * @brief 启用xhci控制器的端口
  591. *
  592. * @param id 控制器id
  593. * @return int
  594. */
  595. static int xhci_hc_start_ports(int id)
  596. {
  597. int cnt = 0;
  598. // 注意,这两个循环应该不能合并到一起,因为可能存在usb2端口offset在前,usb3端口在后的情况,那样的话就会出错
  599. // 循环启动所有的usb3端口
  600. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  601. {
  602. if (XHCI_PORT_IS_USB3(id, i) && XHCI_PORT_IS_ACTIVE(id, i))
  603. {
  604. // reset该端口
  605. if (likely(xhci_reset_port(id, i) == 0)) // 如果端口reset成功,就获取它的描述符
  606. // 否则,reset函数会把它给设置为未激活,并且标志配对的usb2端口是激活的
  607. {
  608. // xhci_hc_get_descriptor(id, i);
  609. ++cnt;
  610. }
  611. }
  612. }
  613. kdebug("active usb3 ports:%d", cnt);
  614. // 循环启动所有的usb2端口
  615. for (int i = 0; i < xhci_hc[id].port_num; ++i)
  616. {
  617. if (XHCI_PORT_IS_USB2(id, i) && XHCI_PORT_IS_ACTIVE(id, i))
  618. {
  619. // reset该端口
  620. if (likely(xhci_reset_port(id, i) == 0)) // 如果端口reset成功,就获取它的描述符
  621. // 否则,reset函数会把它给设置为未激活,并且标志配对的usb2端口是激活的
  622. {
  623. // xhci_hc_get_descriptor(id, i);
  624. ++cnt;
  625. }
  626. }
  627. }
  628. kinfo("xHCI controller %d: Started %d ports.", id, cnt);
  629. }
  630. /**
  631. * @brief 初始化xhci主机控制器的中断控制
  632. *
  633. * @param id 主机控制器id
  634. * @return int 返回码
  635. */
  636. static int xhci_hc_init_intr(int id)
  637. {
  638. uint64_t retval = 0;
  639. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  640. struct xhci_caps_HCSPARAMS2_reg_t hcs2;
  641. memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
  642. memcpy(&hcs2, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS2), sizeof(struct xhci_caps_HCSPARAMS2_reg_t));
  643. uint32_t max_segs = (1 << (uint32_t)(hcs2.ERST_Max));
  644. uint32_t max_interrupters = hcs1.max_intrs;
  645. // 创建 event ring
  646. retval = xhci_create_event_ring(4096, &xhci_hc[id].event_ring_vaddr);
  647. if (unlikely((int64_t)(retval) == -ENOMEM))
  648. return -ENOMEM;
  649. xhci_hc[id].event_ring_table_vaddr = retval;
  650. retval = 0;
  651. xhci_hc[id].current_event_ring_cycle = 1;
  652. // 写入第0个中断寄存器组
  653. xhci_write_intr_reg32(id, 0, XHCI_IR_MAN, 0x3); // 使能中断并清除pending位(这个pending位是写入1就清0的)
  654. xhci_write_intr_reg32(id, 0, XHCI_IR_MOD, 0); // 关闭中断管制
  655. xhci_write_intr_reg32(id, 0, XHCI_IR_TABLE_SIZE, 1); // 当前只有1个segment
  656. xhci_write_intr_reg64(id, 0, XHCI_IR_DEQUEUE, virt_2_phys(xhci_hc[id].event_ring_vaddr) | (1 << 3)); // 写入dequeue寄存器,并清除busy位(写1就会清除)
  657. xhci_write_intr_reg64(id, 0, XHCI_IR_TABLE_ADDR, virt_2_phys(xhci_hc[id].event_ring_table_vaddr)); // 写入table地址
  658. // 清除状态位
  659. xhci_write_op_reg32(id, XHCI_OPS_USBSTS, (1 << 10) | (1 << 4) | (1 << 3) | (1 << 2));
  660. // 开启usb中断
  661. // 注册中断处理程序
  662. struct xhci_hc_irq_install_info_t install_info;
  663. install_info.assert = 1;
  664. install_info.edge_trigger = 1;
  665. install_info.processor = 0; // 投递到bsp
  666. char *buf = (char *)kmalloc(16, 0);
  667. memset(buf, 0, 16);
  668. sprintk(buf, "xHCI HC%d", id);
  669. irq_register(xhci_controller_irq_num[id], &install_info, &xhci_hc_irq_handler, id, &xhci_hc_intr_controller, buf);
  670. kfree(buf);
  671. kdebug("xhci host controller %d: interrupt registered. irq num=%d", id, xhci_controller_irq_num[id]);
  672. return 0;
  673. }
  674. /**
  675. * @brief 初始化xhci控制器
  676. *
  677. * @param header 指定控制器的pci device头部
  678. */
  679. void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
  680. {
  681. if (xhci_ctrl_count >= XHCI_MAX_HOST_CONTROLLERS)
  682. {
  683. kerror("Initialize xhci controller failed: exceed the limit of max controllers.");
  684. return;
  685. }
  686. spin_lock(&xhci_controller_init_lock);
  687. kinfo("Initializing xhci host controller: bus=%#02x, device=%#02x, func=%#02x, VendorID=%#04x, irq_line=%d, irq_pin=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, dev_hdr->header.Vendor_ID, dev_hdr->Interrupt_Line, dev_hdr->Interrupt_PIN);
  688. int cid = xhci_hc_find_available_id();
  689. if (cid < 0)
  690. {
  691. kerror("Initialize xhci controller failed: exceed the limit of max controllers.");
  692. goto failed_exceed_max;
  693. }
  694. memset(&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
  695. xhci_hc[cid].controller_id = cid;
  696. xhci_hc[cid].pci_dev_hdr = dev_hdr;
  697. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x4, 0x0006); // mem I/O access enable and bus master enable
  698. // 为当前控制器映射寄存器地址空间
  699. xhci_hc[cid].vbase = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + XHCI_MAPPING_OFFSET + 65536 * xhci_hc[cid].controller_id;
  700. // kdebug("dev_hdr->BAR0 & (~0xf)=%#018lx", dev_hdr->BAR0 & (~0xf));
  701. mm_map_phys_addr(xhci_hc[cid].vbase, dev_hdr->BAR0 & (~0xf), 65536, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, true);
  702. // 读取xhci控制寄存器
  703. uint16_t iversion = *(uint16_t *)(xhci_hc[cid].vbase + XHCI_CAPS_HCIVERSION);
  704. struct xhci_caps_HCCPARAMS1_reg_t hcc1;
  705. struct xhci_caps_HCCPARAMS2_reg_t hcc2;
  706. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  707. struct xhci_caps_HCSPARAMS2_reg_t hcs2;
  708. memcpy(&hcc1, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCCPARAMS1), sizeof(struct xhci_caps_HCCPARAMS1_reg_t));
  709. memcpy(&hcc2, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCCPARAMS2), sizeof(struct xhci_caps_HCCPARAMS2_reg_t));
  710. memcpy(&hcs1, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
  711. memcpy(&hcs2, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCSPARAMS2), sizeof(struct xhci_caps_HCSPARAMS2_reg_t));
  712. // kdebug("hcc1.xECP=%#010lx", hcc1.xECP);
  713. // 计算operational registers的地址
  714. xhci_hc[cid].vbase_op = xhci_hc[cid].vbase + xhci_read_cap_reg8(cid, XHCI_CAPS_CAPLENGTH);
  715. xhci_hc[cid].db_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_DBOFF) & (~0x3); // bits [1:0] reserved
  716. xhci_hc[cid].rts_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_RTSOFF) & (~0x1f); // bits [4:0] reserved.
  717. xhci_hc[cid].ext_caps_off = 1UL * (hcc1.xECP) * 4;
  718. xhci_hc[cid].context_size = (hcc1.csz) ? 64 : 32;
  719. if (iversion < 0x95)
  720. {
  721. kwarn("Unsupported/Unknowned xHCI controller version: %#06x. This may cause unexpected behavior.", iversion);
  722. }
  723. // if it is a Panther Point device, make sure sockets are xHCI controlled.
  724. if (((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0) & 0xffff) == 0x8086) &&
  725. ((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 2) & 0xffff) == 0x1E31) &&
  726. ((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 8) & 0xff) == 4))
  727. {
  728. kdebug("Is a Panther Point device");
  729. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd8, 0xffffffff);
  730. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd0, 0xffffffff);
  731. }
  732. // 关闭legacy支持
  733. FAIL_ON_TO(xhci_hc_stop_legacy(cid), failed);
  734. // 重置xhci控制器
  735. FAIL_ON_TO(xhci_hc_reset(cid), failed);
  736. // 端口配对
  737. FAIL_ON_TO(xhci_hc_pair_ports(cid), failed);
  738. // ========== 设置USB host controller =========
  739. // 获取页面大小
  740. kdebug("ops pgsize=%#010lx", xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE));
  741. xhci_hc[cid].page_size = (xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE) & 0xffff) << 12;
  742. kdebug("page size=%d", xhci_hc[cid].page_size);
  743. // 获取设备上下文空间
  744. xhci_hc[cid].dcbaap_vaddr = (uint64_t)kmalloc(2048, 0); // 分配2KB的设备上下文地址数组空间
  745. memset((void *)xhci_hc[cid].dcbaap_vaddr, 0, 2048);
  746. kdebug("dcbaap_vaddr=%#018lx", xhci_hc[cid].dcbaap_vaddr);
  747. if (unlikely(!xhci_is_aligned64(xhci_hc[cid].dcbaap_vaddr))) // 地址不是按照64byte对齐
  748. {
  749. kerror("dcbaap isn't 64 byte aligned.");
  750. goto failed_free_dyn;
  751. }
  752. // 写入dcbaap
  753. xhci_write_op_reg64(cid, XHCI_OPS_DCBAAP, virt_2_phys(xhci_hc[cid].dcbaap_vaddr));
  754. // 创建command ring
  755. xhci_hc[cid].cmd_ring_vaddr = xhci_create_ring(XHCI_CMND_RING_TRBS);
  756. if (unlikely(!xhci_is_aligned64(xhci_hc[cid].cmd_ring_vaddr))) // 地址不是按照64byte对齐
  757. {
  758. kerror("cmd ring isn't 64 byte aligned.");
  759. goto failed_free_dyn;
  760. }
  761. // 设置初始cycle bit为1
  762. xhci_hc[cid].cmd_trb_cycle = XHCI_TRB_CYCLE_ON;
  763. // 写入command ring控制寄存器
  764. xhci_write_op_reg64(cid, XHCI_OPS_CRCR, virt_2_phys(xhci_hc[cid].cmd_ring_vaddr) | xhci_hc[cid].cmd_trb_cycle);
  765. // 写入配置寄存器
  766. uint32_t max_slots = hcs1.max_slots;
  767. kdebug("max slots = %d", max_slots);
  768. xhci_write_op_reg32(cid, XHCI_OPS_CONFIG, max_slots);
  769. // 写入设备通知控制寄存器
  770. xhci_write_op_reg32(cid, XHCI_OPS_DNCTRL, (1 << 1)); // 目前只有N1被支持
  771. FAIL_ON_TO(xhci_hc_init_intr(cid), failed_free_dyn);
  772. ++xhci_ctrl_count;
  773. spin_unlock(&xhci_controller_init_lock);
  774. return;
  775. failed_free_dyn:; // 释放动态申请的内存
  776. if (xhci_hc[cid].dcbaap_vaddr)
  777. kfree((void *)xhci_hc[cid].dcbaap_vaddr);
  778. if (xhci_hc[cid].cmd_ring_vaddr)
  779. kfree((void *)xhci_hc[cid].cmd_ring_vaddr);
  780. if (xhci_hc[cid].event_ring_table_vaddr)
  781. kfree((void *)xhci_hc[cid].event_ring_table_vaddr);
  782. if (xhci_hc[cid].event_ring_vaddr)
  783. kfree((void *)xhci_hc[cid].event_ring_vaddr);
  784. failed:;
  785. // 取消地址映射
  786. mm_unmap(xhci_hc[cid].vbase, 65536);
  787. // 清空数组
  788. memset((void *)&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
  789. failed_exceed_max:;
  790. kerror("Failed to initialize controller: bus=%d, dev=%d, func=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func);
  791. spin_unlock(&xhci_controller_init_lock);
  792. }
  793. #pragma GCC optimize("O0")