xhci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. spinlock_t xhci_controller_init_lock; // xhci控制器初始化锁(在usb_init中被初始化)
  10. static int xhci_ctrl_count = 0; // xhci控制器计数
  11. static struct xhci_host_controller_t xhci_hc[XHCI_MAX_HOST_CONTROLLERS] = {0};
  12. /*
  13. 注意!!!
  14. 尽管采用MMI/O的方式访问寄存器,但是对于指定大小的寄存器,
  15. 在发起读请求的时候,只能从寄存器的起始地址位置开始读取。
  16. 例子:不能在一个32bit的寄存器中的偏移量8的位置开始读取1个字节
  17. 这种情况下,我们必须从32bit的寄存器的0地址处开始读取32bit,然后通过移位的方式得到其中的字节。
  18. */
  19. #define xhci_read_cap_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase + offset))
  20. #define xhci_get_ptr_cap_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase + offset))
  21. #define xhci_write_cap_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase + offset) = (uint8_t)value)
  22. #define xhci_read_cap_reg32(id, offset) (*(uint32_t *)(xhci_hc[id].vbase + offset))
  23. #define xhci_get_ptr_cap_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase + offset))
  24. #define xhci_write_cap_reg32(id, offset, value) (*(uint32_t *)(xhci_hc[id].vbase + offset) = (uint32_t)value)
  25. #define xhci_read_cap_reg64(id, offset) (*(uint64_t *)(xhci_hc[id].vbase + offset))
  26. #define xhci_get_ptr_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase + offset))
  27. #define xhci_write_cap_reg64(id, offset, value) (*(uint64_t *)(xhci_hc[id].vbase + offset) = (uint64_t)value)
  28. #define xhci_read_op_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase_op + offset))
  29. #define xhci_get_ptr_op_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase_op + offset))
  30. #define xhci_write_op_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase_op + offset) = (uint8_t)value)
  31. #define xhci_read_op_reg32(id, offset) (*(uint32_t *)(xhci_hc[id].vbase_op + offset))
  32. #define xhci_get_ptr_op_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase_op + offset))
  33. #define xhci_write_op_reg32(id, offset, value) (*(uint32_t *)(xhci_hc[id].vbase_op + offset) = (uint32_t)value)
  34. #define xhci_read_op_reg64(id, offset) (*(uint64_t *)(xhci_hc[id].vbase_op + offset))
  35. #define xhci_get_ptr_op_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase_op + offset))
  36. #define xhci_write_op_reg64(id, offset, value) (*(uint64_t *)(xhci_hc[id].vbase_op + offset) = (uint64_t)value)
  37. #define xhci_is_aligned64(addr) ((addr & 0x3f) == 0) // 是否64bytes对齐
  38. /**
  39. * @brief 判断端口信息
  40. * @param cid 主机控制器id
  41. * @param pid 端口id
  42. */
  43. #define XHCI_PORT_IS_USB2(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB2)
  44. #define XHCI_PORT_IS_USB3(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB3)
  45. #define XHCI_PORT_IS_USB2_HSO(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HSO) == XHCI_PROTOCOL_HSO)
  46. #define XHCI_PORT_HAS_PAIR(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HAS_PAIR) == XHCI_PROTOCOL_HAS_PAIR)
  47. #define XHCI_PORT_IS_ACTIVE(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_ACTIVE) == XHCI_PROTOCOL_ACTIVE)
  48. /**
  49. * @brief 设置link TRB的命令(dword3)
  50. *
  51. */
  52. #define xhci_TRB_set_link_cmd(trb_vaddr) \
  53. do \
  54. { \
  55. struct xhci_TRB_normal_t *ptr = (struct xhci_TRB_normal_t *)trb_vaddr; \
  56. ptr->TRB_type = TRB_TYPE_LINK; \
  57. ptr->ioc = 0; \
  58. ptr->chain = 0; \
  59. ptr->ent = 0; \
  60. ptr->cycle = 1; \
  61. } while (0)
  62. #define FAIL_ON(value, to) \
  63. do \
  64. { \
  65. if (unlikely(value != 0)) \
  66. goto to; \
  67. } while (0)
  68. // Common TRB types
  69. enum
  70. {
  71. TRB_TYPE_NORMAL = 1,
  72. TRB_TYPE_SETUP_STAGE,
  73. TRB_TYPE_DATA_STAGE,
  74. TRB_TYPE_STATUS_STAGE,
  75. TRB_TYPE_ISOCH,
  76. TRB_TYPE_LINK,
  77. TRB_TYPE_EVENT_DATA,
  78. TRB_TYPE_NO_OP,
  79. TRB_TYPE_ENABLE_SLOT,
  80. TRB_TYPE_DISABLE_SLOT = 10,
  81. TRB_TYPE_ADDRESS_DEVICE = 11,
  82. TRB_TYPE_CONFIG_EP,
  83. TRB_TYPE_EVALUATE_CONTEXT,
  84. TRB_TYPE_RESET_EP,
  85. TRB_TYPE_STOP_EP = 15,
  86. TRB_TYPE_SET_TR_DEQUEUE,
  87. TRB_TYPE_RESET_DEVICE,
  88. TRB_TYPE_FORCE_EVENT,
  89. TRB_TYPE_DEG_BANDWIDTH,
  90. TRB_TYPE_SET_LAT_TOLERANCE = 20,
  91. TRB_TYPE_GET_PORT_BAND = 21,
  92. TRB_TYPE_FORCE_HEADER,
  93. TRB_TYPE_NO_OP_CMD, // 24 - 31 = reserved
  94. TRB_TYPE_TRANS_EVENT = 32,
  95. TRB_TYPE_COMMAND_COMPLETION,
  96. TRB_TYPE_PORT_STATUS_CHANGE,
  97. TRB_TYPE_BANDWIDTH_REQUEST,
  98. TRB_TYPE_DOORBELL_EVENT,
  99. TRB_TYPE_HOST_CONTROLLER_EVENT = 37,
  100. TRB_TYPE_DEVICE_NOTIFICATION,
  101. TRB_TYPE_MFINDEX_WRAP,
  102. // 40 - 47 = reserved
  103. // 48 - 63 = Vendor Defined
  104. };
  105. /**
  106. * @brief 在controller数组之中寻找可用插槽
  107. *
  108. * 注意:该函数只能被获得init锁的进程所调用
  109. * @return int 可用id(无空位时返回-1)
  110. */
  111. static int xhci_hc_find_available_id()
  112. {
  113. if (unlikely(xhci_ctrl_count >= XHCI_MAX_HOST_CONTROLLERS))
  114. return -1;
  115. for (int i = 0; i < XHCI_MAX_HOST_CONTROLLERS; ++i)
  116. {
  117. if (xhci_hc[i].pci_dev_hdr == NULL)
  118. return i;
  119. }
  120. return -1;
  121. }
  122. /**
  123. * @brief 停止xhci主机控制器
  124. *
  125. * @param id 主机控制器id
  126. * @return int
  127. */
  128. static int xhci_hc_stop(int id)
  129. {
  130. // 判断是否已经停止
  131. if (unlikely((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 1))
  132. return 0;
  133. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00000000);
  134. char timeout = 17;
  135. while ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
  136. {
  137. usleep(1000);
  138. if (--timeout == 0)
  139. return -ETIMEDOUT;
  140. }
  141. return 0;
  142. }
  143. /**
  144. * @brief reset xHCI主机控制器
  145. *
  146. * @param id 主机控制器id
  147. * @return int
  148. */
  149. static int xhci_hc_reset(int id)
  150. {
  151. int retval = 0;
  152. // 判断HCHalted是否置位
  153. if ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
  154. {
  155. // 未置位,需要先尝试停止usb主机控制器
  156. retval = xhci_hc_stop(id);
  157. if (unlikely(retval))
  158. return retval;
  159. }
  160. int timeout = 500; // wait 500ms
  161. // reset
  162. xhci_write_op_reg32(id, XHCI_OPS_USBCMD, (1 << 1));
  163. while (xhci_read_op_reg32(id, XHCI_OPS_USBCMD) & (1 << 1))
  164. {
  165. usleep(1000);
  166. if (--timeout == 0)
  167. return -ETIMEDOUT;
  168. }
  169. // kdebug("reset done!, timeout=%d", timeout);
  170. return retval;
  171. }
  172. /**
  173. * @brief 停止指定xhci控制器的legacy support
  174. *
  175. * @param id 控制器id
  176. * @return int
  177. */
  178. static int xhci_hc_stop_legacy(int id)
  179. {
  180. uint64_t current_offset = xhci_hc[id].ext_caps_off;
  181. do
  182. {
  183. // 判断当前entry是否为legacy support entry
  184. if (xhci_read_cap_reg8(id, current_offset) == XHCI_XECP_ID_LEGACY)
  185. {
  186. // 接管控制权
  187. xhci_write_cap_reg32(id, current_offset, xhci_read_cap_reg32(id, current_offset) | XHCI_XECP_LEGACY_OS_OWNED);
  188. // 等待响应完成
  189. int timeout = XHCI_XECP_LEGACY_TIMEOUT;
  190. while ((xhci_read_cap_reg32(id, current_offset) & XHCI_XECP_LEGACY_OWNING_MASK) != XHCI_XECP_LEGACY_OS_OWNED)
  191. {
  192. usleep(1000);
  193. if (--timeout == 0)
  194. {
  195. kerror("The BIOS doesn't stop legacy support.");
  196. return -ETIMEDOUT;
  197. }
  198. }
  199. // 处理完成
  200. return 0;
  201. }
  202. // 读取下一个entry的偏移增加量
  203. int next_off = ((xhci_read_cap_reg32(id, current_offset) & 0xff00) >> 8) << 2;
  204. // 将指针跳转到下一个entry
  205. current_offset = next_off ? (current_offset + next_off) : 0;
  206. } while (current_offset);
  207. // 当前controller不存在legacy支持,也问题不大,不影响
  208. return 0;
  209. }
  210. /**
  211. * @brief
  212. *
  213. * @return uint32_t
  214. */
  215. /**
  216. * @brief 在Ex capability list中寻找符合指定的协议号的寄存器offset、count、flag信息
  217. *
  218. * @param id 主机控制器id
  219. * @param list_off 列表项位置距离控制器虚拟基地址的偏移量
  220. * @param version 要寻找的端口版本号(2或3)
  221. * @param offset 返回的 Compatible Port Offset
  222. * @param count 返回的 Compatible Port Count
  223. * @param protocol_flag 返回的与协议相关的flag
  224. * @return uint32_t 下一个列表项的偏移量
  225. */
  226. 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)
  227. {
  228. if (count)
  229. *count = 0;
  230. do
  231. {
  232. uint32_t dw0 = xhci_read_cap_reg32(id, list_off);
  233. uint32_t next_list_off = (dw0 >> 8) & 0xff;
  234. next_list_off = next_list_off ? (list_off + (next_list_off << 2)) : 0;
  235. if ((dw0 & 0xff) == XHCI_XECP_ID_PROTOCOL && ((dw0 & 0xff000000) >> 24) == version)
  236. {
  237. uint32_t dw2 = xhci_read_cap_reg32(id, list_off + 8);
  238. if (offset != NULL)
  239. *offset = (uint32_t)(dw2 & 0xff);
  240. if (count != NULL)
  241. *count = (uint32_t)((dw2 & 0xff00) >> 8);
  242. if (protocol_flag != NULL)
  243. *protocol_flag = (uint16_t)((dw2 >> 16) & 0xffff);
  244. return next_list_off;
  245. }
  246. list_off = next_list_off;
  247. } while (list_off);
  248. return 0;
  249. }
  250. /**
  251. * @brief 配对xhci主机控制器的usb2、usb3端口
  252. *
  253. * @param id 主机控制器id
  254. * @return int 返回码
  255. */
  256. static int xhci_hc_pair_ports(int id)
  257. {
  258. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  259. memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCCPARAMS1_reg_t));
  260. // 从hcs1获取端口数量
  261. xhci_hc[id].port_num = hcs1.max_ports;
  262. // 找到所有的端口并标记其端口信息
  263. xhci_hc[id].port_num_u2 = 0;
  264. xhci_hc[id].port_num_u3 = 0;
  265. uint32_t next_off = xhci_hc[id].ext_caps_off;
  266. uint32_t offset, cnt;
  267. uint16_t protocol_flags;
  268. // 寻找所有的usb2端口
  269. while (next_off)
  270. {
  271. next_off = xhci_hc_get_protocol_offset(id, next_off, 2, &offset, &cnt, &protocol_flags);
  272. if (cnt)
  273. {
  274. for (int i = 0; i < cnt; ++i)
  275. {
  276. xhci_hc[id].ports[offset + i].offset = ++xhci_hc[id].port_num_u2;
  277. xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB2;
  278. // usb2 high speed only
  279. if (protocol_flags & 2)
  280. xhci_hc[id].ports[offset + i].flags |= XHCI_PROTOCOL_HSO;
  281. }
  282. }
  283. }
  284. // 寻找所有的usb3端口
  285. next_off = xhci_hc[id].ext_caps_off;
  286. while (next_off)
  287. {
  288. next_off = xhci_hc_get_protocol_offset(id, next_off, 3, &offset, &cnt, &protocol_flags);
  289. if (cnt)
  290. {
  291. for (int i = 0; i < cnt; ++i)
  292. {
  293. xhci_hc[id].ports[offset + i].offset = ++xhci_hc[id].port_num_u3;
  294. xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB3;
  295. }
  296. }
  297. }
  298. // 将对应的USB2端口和USB3端口进行配对
  299. for (int i = 1; i <= xhci_hc[id].port_num; ++i)
  300. {
  301. for (int j = i; j <= xhci_hc[id].port_num; ++j)
  302. {
  303. if (unlikely(i == j))
  304. continue;
  305. if ((xhci_hc[id].ports[i].offset == xhci_hc[id].ports[j].offset) &&
  306. ((xhci_hc[id].ports[i].flags & XHCI_PROTOCOL_INFO) != (xhci_hc[id].ports[j].flags & XHCI_PROTOCOL_INFO)))
  307. {
  308. xhci_hc[id].ports[i].paired_port_num = j;
  309. xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_HAS_PAIR;
  310. xhci_hc[id].ports[j].paired_port_num = i;
  311. xhci_hc[id].ports[j].flags |= XHCI_PROTOCOL_HAS_PAIR;
  312. }
  313. }
  314. }
  315. // 标记所有的usb3端口为激活状态
  316. for (int i = 1; i <= xhci_hc[id].port_num; ++i)
  317. {
  318. if (XHCI_PORT_IS_USB3(id, i) ||
  319. (XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i))))
  320. xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_ACTIVE;
  321. }
  322. 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);
  323. /*
  324. // 打印配对结果
  325. for (int i = 1; i <= xhci_hc[id].port_num; ++i)
  326. {
  327. if (XHCI_PORT_IS_USB3(id, i))
  328. {
  329. kdebug("USB3 port %d, offset=%d, pair with usb2 port %d, current port is %s", i, xhci_hc[id].ports[i].offset,
  330. xhci_hc[id].ports[i].paired_port_num, XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive");
  331. }
  332. else if (XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i))) // 单独的2.0接口
  333. {
  334. kdebug("Stand alone USB2 port %d, offset=%d, current port is %s", i, xhci_hc[id].ports[i].offset,
  335. XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive");
  336. }
  337. else if (XHCI_PORT_IS_USB2(id, i))
  338. {
  339. kdebug("USB2 port %d, offset=%d, current port is %s, has pair=%s", i, xhci_hc[id].ports[i].offset,
  340. XHCI_PORT_IS_ACTIVE(id, i) ? "active" : "inactive", XHCI_PORT_HAS_PAIR(id, i)?"true":"false");
  341. }
  342. }
  343. */
  344. return 0;
  345. }
  346. /**
  347. * @brief 创建ring,并将最后一个trb指向头一个trb
  348. *
  349. * @param trbs 要创建的trb数量
  350. * @return uint64_t
  351. */
  352. static uint64_t xhci_create_ring(int trbs)
  353. {
  354. int total_size = trbs * sizeof(struct xhci_TRB_t);
  355. const uint64_t vaddr = (uint64_t)kmalloc(total_size, 0);
  356. memset(vaddr, 0, total_size);
  357. // 设置最后一个trb为link trb
  358. xhci_TRB_set_link_cmd(vaddr + total_size - sizeof(sizeof(struct xhci_TRB_t)));
  359. return vaddr;
  360. }
  361. /**
  362. * @brief 初始化xhci控制器
  363. *
  364. * @param header 指定控制器的pci device头部
  365. */
  366. void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
  367. {
  368. if (xhci_ctrl_count >= XHCI_MAX_HOST_CONTROLLERS)
  369. {
  370. kerror("Initialize xhci controller failed: exceed the limit of max controllers.");
  371. return;
  372. }
  373. spin_lock(&xhci_controller_init_lock);
  374. 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);
  375. int cid = xhci_hc_find_available_id();
  376. if (cid < 0)
  377. {
  378. kerror("Initialize xhci controller failed: exceed the limit of max controllers.");
  379. goto failed_exceed_max;
  380. }
  381. memset(&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
  382. xhci_hc[cid].controller_id = cid;
  383. xhci_hc[cid].pci_dev_hdr = dev_hdr;
  384. 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
  385. // 为当前控制器映射寄存器地址空间
  386. xhci_hc[cid].vbase = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + XHCI_MAPPING_OFFSET + 65536 * xhci_hc[cid].controller_id;
  387. // kdebug("dev_hdr->BAR0 & (~0xf)=%#018lx", dev_hdr->BAR0 & (~0xf));
  388. mm_map_phys_addr(xhci_hc[cid].vbase, dev_hdr->BAR0 & (~0xf), 65536, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, true);
  389. // 读取xhci控制寄存器
  390. uint16_t iversion = *(uint16_t *)(xhci_hc[cid].vbase + XHCI_CAPS_HCIVERSION);
  391. struct xhci_caps_HCCPARAMS1_reg_t hcc1;
  392. struct xhci_caps_HCCPARAMS2_reg_t hcc2;
  393. struct xhci_caps_HCSPARAMS1_reg_t hcs1;
  394. struct xhci_caps_HCSPARAMS2_reg_t hcs2;
  395. memcpy(&hcc1, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCCPARAMS1), sizeof(struct xhci_caps_HCCPARAMS1_reg_t));
  396. memcpy(&hcc2, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCCPARAMS2), sizeof(struct xhci_caps_HCCPARAMS1_reg_t));
  397. memcpy(&hcs1, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCCPARAMS1_reg_t));
  398. memcpy(&hcs2, xhci_get_ptr_cap_reg32(cid, XHCI_CAPS_HCSPARAMS2), sizeof(struct xhci_caps_HCCPARAMS1_reg_t));
  399. // kdebug("hcc1.xECP=%#010lx", hcc1.xECP);
  400. // 计算operational registers的地址
  401. xhci_hc[cid].vbase_op = xhci_hc[cid].vbase + xhci_read_cap_reg8(cid, XHCI_CAPS_CAPLENGTH);
  402. xhci_hc[cid].db_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_DBOFF) & (~0x3); // bits [1:0] reserved
  403. xhci_hc[cid].rts_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_RTSOFF) & (~0x1f); // bits [4:0] reserved.
  404. xhci_hc[cid].ext_caps_off = (hcc1.xECP) * 4;
  405. xhci_hc[cid].context_size = (hcc1.csz) ? 64 : 32;
  406. if (iversion < 0x95)
  407. {
  408. kwarn("Unsupported/Unknowned xHCI controller version: %#06x. This may cause unexpected behavior.", iversion);
  409. }
  410. // if it is a Panther Point device, make sure sockets are xHCI controlled.
  411. if (((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0) & 0xffff) == 0x8086) &&
  412. ((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 2) & 0xffff) == 0x1E31) &&
  413. ((pci_read_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 8) & 0xff) == 4))
  414. {
  415. kdebug("Is a Panther Point device");
  416. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd8, 0xffffffff);
  417. pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd0, 0xffffffff);
  418. }
  419. // 重置xhci控制器
  420. FAIL_ON(xhci_hc_reset(cid), failed);
  421. // 关闭legacy支持
  422. FAIL_ON(xhci_hc_stop_legacy(cid), failed);
  423. // 端口配对
  424. FAIL_ON(xhci_hc_pair_ports(cid), failed);
  425. // 获取页面大小
  426. xhci_hc[cid].page_size = (xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE) & 0xffff) << 12;
  427. kdebug("pg size=%d", xhci_hc[cid].page_size);
  428. // 获取设备上下文空间
  429. xhci_hc[cid].dcbaap_vaddr = (uint64_t)kmalloc(2048, 0); // 分配2KB的设备上下文地址数组空间
  430. memset(xhci_hc[cid].dcbaap_vaddr, 0, 2048);
  431. kdebug("dcbaap_vaddr=%#018lx", xhci_hc[cid].dcbaap_vaddr);
  432. if (unlikely(!xhci_is_aligned64(xhci_hc[cid].dcbaap_vaddr))) // 地址不是按照64byte对齐
  433. {
  434. kerror("dcbaap isn't 64 byte aligned.");
  435. goto failed_free_dyn;
  436. }
  437. // 写入dcbaap
  438. xhci_write_cap_reg64(cid, XHCI_OPS_DCBAAP, virt_2_phys(xhci_hc[cid].dcbaap_vaddr));
  439. xhci_hc[cid].cmd_ring_vaddr = xhci_create_ring(XHCI_CMND_RING_TRBS);
  440. ++xhci_ctrl_count;
  441. spin_unlock(&xhci_controller_init_lock);
  442. return;
  443. failed_free_dyn:; // 释放动态申请的内存
  444. if (xhci_hc[cid].dcbaap_vaddr)
  445. kfree(xhci_hc[cid].dcbaap_vaddr);
  446. failed:;
  447. // 取消地址映射
  448. mm_unmap(xhci_hc[cid].vbase, 65536);
  449. // 清空数组
  450. memset((void *)&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
  451. failed_exceed_max:;
  452. kerror("Failed to initialize controller: bus=%d, dev=%d, func=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func);
  453. spin_unlock(&xhci_controller_init_lock);
  454. }