xhci.c 27 KB

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