xhci.c 33 KB

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