pci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include "pci.h"
  2. #include <common/kprint.h>
  3. #include <mm/slab.h>
  4. #include <debug/bug.h>
  5. static uint count_device_list = 0;
  6. static void pci_checkBus(uint8_t bus);
  7. /**
  8. * @brief 将设备信息结构体加到链表里面
  9. *
  10. */
  11. #define ADD_DEVICE_STRUCT_TO_LIST(ret) \
  12. do \
  13. { \
  14. if (count_device_list > 0) \
  15. { \
  16. ++count_device_list; \
  17. list_add(pci_device_structure_list, &(ret->header.list)); \
  18. } \
  19. else \
  20. { \
  21. ++count_device_list; \
  22. list_init(&(ret->header.list)); \
  23. pci_device_structure_list = &(ret->header.list); \
  24. } \
  25. } while (0)
  26. /**
  27. * @brief 生成架构相关的msi的message address
  28. *
  29. */
  30. #define pci_get_arch_msi_message_address(processor) ((uint64_t)(0xfee00000UL | (processor << 12)))
  31. /**
  32. * @brief 生成架构相关的message data
  33. *
  34. */
  35. #define pci_get_arch_msi_message_data(vector, processor, edge_trigger, assert) ((uint32_t)((vector & 0xff) | (edge_trigger == 1 ? 0 : (1 << 15)) | (assert == 0 ? 0 : (1 << 14))))
  36. /**
  37. * @brief 从pci配置空间读取信息
  38. *
  39. * @param bus 总线号
  40. * @param slot 设备号
  41. * @param func 功能号
  42. * @param offset 字节偏移量
  43. * @return uint 寄存器值
  44. */
  45. uint32_t pci_read_config(uchar bus, uchar slot, uchar func, uchar offset)
  46. {
  47. uint lbus = (uint)bus;
  48. uint lslot = (uint)slot;
  49. uint lfunc = ((uint)func) & 7;
  50. // 构造pci配置空间地址
  51. uint address = (uint)((lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | ((uint)0x80000000));
  52. io_out32(PORT_PCI_CONFIG_ADDRESS, address);
  53. // 读取返回的数据
  54. uint32_t ret = (uint)(io_in32(PORT_PCI_CONFIG_DATA));
  55. return ret;
  56. }
  57. /**
  58. * @brief 向pci配置空间写入信息
  59. *
  60. * @param bus 总线号
  61. * @param slot 设备号
  62. * @param func 功能号
  63. * @param offset 字节偏移量
  64. * @return uint 返回码
  65. */
  66. uint pci_write_config(uchar bus, uchar slot, uchar func, uchar offset, uint32_t data)
  67. {
  68. uint lbus = (uint)bus;
  69. uint lslot = (uint)slot;
  70. uint lfunc = ((uint)func) & 7;
  71. // 构造pci配置空间地址
  72. uint address = (uint)((lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | ((uint)0x80000000));
  73. io_out32(PORT_PCI_CONFIG_ADDRESS, address);
  74. // 写入数据
  75. io_out32(PORT_PCI_CONFIG_DATA, data);
  76. return 0;
  77. }
  78. /**
  79. * @brief 读取type为0x0的pci设备的header
  80. * 本函数只应被 pci_read_header()调用
  81. * @param header 返回的header
  82. * @param bus 总线号
  83. * @param slot 插槽号
  84. * @param func 功能号
  85. */
  86. static void pci_read_general_device_header(struct pci_device_structure_general_device_t *header, uchar bus, uchar slot, uchar func)
  87. {
  88. uint32_t tmp32;
  89. header->BAR0 = pci_read_config(bus, slot, func, 0x10);
  90. header->BAR1 = pci_read_config(bus, slot, func, 0x14);
  91. header->BAR2 = pci_read_config(bus, slot, func, 0x18);
  92. header->BAR3 = pci_read_config(bus, slot, func, 0x1c);
  93. header->BAR4 = pci_read_config(bus, slot, func, 0x20);
  94. header->BAR5 = pci_read_config(bus, slot, func, 0x24);
  95. header->Cardbus_CIS_Pointer = pci_read_config(bus, slot, func, 0x28);
  96. tmp32 = pci_read_config(bus, slot, func, 0x2c);
  97. header->Subsystem_Vendor_ID = tmp32 & 0xffff;
  98. header->Subsystem_ID = (tmp32 >> 16) & 0xffff;
  99. header->Expansion_ROM_base_address = pci_read_config(bus, slot, func, 0x30);
  100. tmp32 = pci_read_config(bus, slot, func, 0x34);
  101. header->Capabilities_Pointer = tmp32 & 0xff;
  102. header->reserved0 = (tmp32 >> 8) & 0xff;
  103. header->reserved1 = (tmp32 >> 16) & 0xffff;
  104. header->reserved2 = pci_read_config(bus, slot, func, 0x38);
  105. tmp32 = pci_read_config(bus, slot, func, 0x3c);
  106. header->Interrupt_Line = tmp32 & 0xff;
  107. header->Interrupt_PIN = (tmp32 >> 8) & 0xff;
  108. header->Min_Grant = (tmp32 >> 16) & 0xff;
  109. header->Max_Latency = (tmp32 >> 24) & 0xff;
  110. }
  111. /**
  112. * @brief 读取type为0x1的pci_to_pci_bridge的header
  113. * 本函数只应被 pci_read_header()调用
  114. * @param header 返回的header
  115. * @param bus 总线号
  116. * @param slot 插槽号
  117. * @param func 功能号
  118. */
  119. static void pci_read_pci_to_pci_bridge_header(struct pci_device_structure_pci_to_pci_bridge_t *header, uchar bus, uchar slot, uchar func)
  120. {
  121. uint32_t tmp32;
  122. header->BAR0 = pci_read_config(bus, slot, func, 0x10);
  123. header->BAR1 = pci_read_config(bus, slot, func, 0x14);
  124. tmp32 = pci_read_config(bus, slot, func, 0x18);
  125. header->Primary_Bus_Number = tmp32 & 0xff;
  126. header->Secondary_Bus_Number = (tmp32 >> 8) & 0xff;
  127. header->Subordinate_Bus_Number = (tmp32 >> 16) & 0xff;
  128. header->Secondary_Latency_Timer = (tmp32 >> 24) & 0xff;
  129. tmp32 = pci_read_config(bus, slot, func, 0x1c);
  130. header->io_base = tmp32 & 0xff;
  131. header->io_limit = (tmp32 >> 8) & 0xff;
  132. header->Secondary_Status = (tmp32 >> 16) & 0xffff;
  133. tmp32 = pci_read_config(bus, slot, func, 0x20);
  134. header->Memory_Base = tmp32 & 0xffff;
  135. header->Memory_Limit = (tmp32 >> 16) & 0xffff;
  136. tmp32 = pci_read_config(bus, slot, func, 0x24);
  137. header->Prefetchable_Memory_Base = tmp32 & 0xffff;
  138. header->Prefetchable_Memory_Limit = (tmp32 >> 16) & 0xffff;
  139. header->Prefetchable_Base_Upper_32_Bits = pci_read_config(bus, slot, func, 0x28);
  140. header->Prefetchable_Limit_Upper_32_Bits = pci_read_config(bus, slot, func, 0x2c);
  141. tmp32 = pci_read_config(bus, slot, func, 0x30);
  142. header->io_Base_Upper_16_Bits = tmp32 & 0xffff;
  143. header->io_Limit_Upper_16_Bits = (tmp32 >> 16) & 0xffff;
  144. tmp32 = pci_read_config(bus, slot, func, 0x34);
  145. header->Capability_Pointer = tmp32 & 0xff;
  146. header->reserved0 = (tmp32 >> 8) & 0xff;
  147. header->reserved1 = (tmp32 >> 16) & 0xffff;
  148. header->Expansion_ROM_base_address = pci_read_config(bus, slot, func, 0x38);
  149. tmp32 = pci_read_config(bus, slot, func, 0x3c);
  150. header->Interrupt_Line = tmp32 & 0xff;
  151. header->Interrupt_PIN = (tmp32 >> 8) & 0xff;
  152. header->Bridge_Control = (tmp32 >> 16) & 0xffff;
  153. }
  154. /**
  155. * @brief 读取type为0x2的pci_to_cardbus_bridge的header
  156. * 本函数只应被 pci_read_header()调用
  157. * @param header 返回的header
  158. * @param bus 总线号
  159. * @param slot 插槽号
  160. * @param func 功能号
  161. */
  162. static void pci_read_pci_to_cardbus_bridge_header(struct pci_device_structure_pci_to_cardbus_bridge_t *header, uchar bus, uchar slot, uchar func)
  163. {
  164. uint32_t tmp32;
  165. header->CardBus_Socket_ExCa_base_address = pci_read_config(bus, slot, func, 0x10);
  166. tmp32 = pci_read_config(bus, slot, func, 0x14);
  167. header->Offset_of_capabilities_list = tmp32 & 0xff;
  168. header->Reserved = (tmp32 >> 8) & 0xff;
  169. header->Secondary_status = (tmp32 >> 16) & 0xff;
  170. tmp32 = pci_read_config(bus, slot, func, 0x18);
  171. header->PCI_bus_number = tmp32 & 0xff;
  172. header->CardBus_bus_number = (tmp32 >> 8) & 0xff;
  173. header->Subordinate_bus_number = (tmp32 >> 16) & 0xff;
  174. header->CardBus_latency_timer = (tmp32 >> 24) & 0xff;
  175. header->Memory_Base_Address0 = pci_read_config(bus, slot, func, 0x1c);
  176. header->Memory_Limit0 = pci_read_config(bus, slot, func, 0x20);
  177. header->Memory_Base_Address1 = pci_read_config(bus, slot, func, 0x24);
  178. header->Memory_Limit1 = pci_read_config(bus, slot, func, 0x28);
  179. header->IO_Base_Address0 = pci_read_config(bus, slot, func, 0x2c);
  180. header->IO_Limit0 = pci_read_config(bus, slot, func, 0x30);
  181. header->IO_Base_Address1 = pci_read_config(bus, slot, func, 0x34);
  182. header->IO_Limit1 = pci_read_config(bus, slot, func, 0x38);
  183. tmp32 = pci_read_config(bus, slot, func, 0x3c);
  184. header->Interrupt_Line = tmp32 & 0xff;
  185. header->Interrupt_PIN = (tmp32 >> 8) & 0xff;
  186. header->Bridge_Control = (tmp32 >> 16) & 0xffff;
  187. tmp32 = pci_read_config(bus, slot, func, 0x40);
  188. header->Subsystem_Device_ID = tmp32 & 0xffff;
  189. header->Subsystem_Vendor_ID = (tmp32 >> 16) & 0xffff;
  190. header->PC_Card_legacy_mode_base_address_16_bit = pci_read_config(bus, slot, func, 0x44);
  191. }
  192. /**
  193. * @brief 读取pci设备标头
  194. *
  195. * @param type 标头类型
  196. * @param bus 总线号
  197. * @param slot 插槽号
  198. * @param func 功能号
  199. * @param add_to_list 添加到链表
  200. * @return 返回的header
  201. */
  202. void *pci_read_header(int *type, uchar bus, uchar slot, uchar func, bool add_to_list)
  203. {
  204. struct pci_device_structure_header_t *common_header = (struct pci_device_structure_header_t *)kmalloc(127, 0);
  205. common_header->bus = bus;
  206. common_header->device = slot;
  207. common_header->func = func;
  208. uint32_t tmp32;
  209. // 先读取公共header
  210. tmp32 = pci_read_config(bus, slot, func, 0x0);
  211. common_header->Vendor_ID = tmp32 & 0xffff;
  212. common_header->Device_ID = (tmp32 >> 16) & 0xffff;
  213. tmp32 = pci_read_config(bus, slot, func, 0x4);
  214. common_header->Command = tmp32 & 0xffff;
  215. common_header->Status = (tmp32 >> 16) & 0xffff;
  216. tmp32 = pci_read_config(bus, slot, func, 0x8);
  217. common_header->RevisionID = tmp32 & 0xff;
  218. common_header->ProgIF = (tmp32 >> 8) & 0xff;
  219. common_header->SubClass = (tmp32 >> 16) & 0xff;
  220. common_header->Class_code = (tmp32 >> 24) & 0xff;
  221. tmp32 = pci_read_config(bus, slot, func, 0xc);
  222. common_header->CacheLineSize = tmp32 & 0xff;
  223. common_header->LatencyTimer = (tmp32 >> 8) & 0xff;
  224. common_header->HeaderType = (tmp32 >> 16) & 0xff;
  225. common_header->BIST = (tmp32 >> 24) & 0xff;
  226. void *ret;
  227. if (common_header->Vendor_ID == 0xffff)
  228. {
  229. *type = E_DEVICE_INVALID;
  230. kfree(common_header);
  231. return NULL;
  232. }
  233. // 根据公共头部,判断该结构所属的类型
  234. switch (common_header->HeaderType)
  235. {
  236. case 0x0: // general device
  237. ret = common_header;
  238. pci_read_general_device_header((struct pci_device_structure_general_device_t *)ret, bus, slot, func);
  239. if (add_to_list)
  240. ADD_DEVICE_STRUCT_TO_LIST(((struct pci_device_structure_general_device_t *)ret));
  241. *type = 0x0;
  242. return ret;
  243. break;
  244. case 0x1:
  245. ret = common_header;
  246. pci_read_pci_to_pci_bridge_header((struct pci_device_structure_pci_to_pci_bridge_t *)ret, bus, slot, func);
  247. if (add_to_list)
  248. ADD_DEVICE_STRUCT_TO_LIST(((struct pci_device_structure_pci_to_pci_bridge_t *)ret));
  249. *type = 0x1;
  250. return ret;
  251. break;
  252. case 0x2:
  253. ret = common_header;
  254. pci_read_pci_to_cardbus_bridge_header((struct pci_device_structure_pci_to_cardbus_bridge_t *)ret, bus, slot, func);
  255. if (add_to_list)
  256. ADD_DEVICE_STRUCT_TO_LIST(((struct pci_device_structure_pci_to_cardbus_bridge_t *)ret));
  257. *type = 0x2;
  258. return ret;
  259. break;
  260. default: // 错误的头类型 这里不应该被执行
  261. // kerror("PCI->pci_read_header(): Invalid header type.");
  262. *type = E_WRONG_HEADER_TYPE;
  263. // kerror("vendor id=%#010lx", common_header->Vendor_ID);
  264. // kerror("header type = %d", common_header->HeaderType);
  265. kfree(common_header);
  266. return NULL;
  267. break;
  268. }
  269. }
  270. static void pci_checkFunction(uint8_t bus, uint8_t device, uint8_t function)
  271. {
  272. int header_type;
  273. struct pci_device_structure_header_t *header = pci_read_header(&header_type, bus, device, function, true);
  274. if (header_type == E_WRONG_HEADER_TYPE)
  275. {
  276. // kerror("pci_checkFunction(): wrong header type!");
  277. // 此处内存已经在read header函数里面释放,不用重复释放
  278. return;
  279. }
  280. // header = ((struct pci_device_structure_general_device_t *)raw_header)->header;
  281. if ((header->Class_code == 0x6) && (header->SubClass == 0x4))
  282. {
  283. uint8_t SecondaryBus = ((struct pci_device_structure_pci_to_pci_bridge_t *)header)->Secondary_Bus_Number;
  284. pci_checkBus(SecondaryBus);
  285. }
  286. }
  287. static int pci_checkDevice(uint8_t bus, uint8_t device)
  288. {
  289. int header_type;
  290. struct pci_device_structure_header_t *header = pci_read_header(&header_type, bus, device, 0, false);
  291. if (header_type == E_WRONG_HEADER_TYPE)
  292. {
  293. // 此处内存已经在read header函数里面释放,不用重复释放
  294. return E_WRONG_HEADER_TYPE;
  295. }
  296. if (header_type == E_DEVICE_INVALID)
  297. {
  298. // kerror("DEVICE INVALID");
  299. return E_DEVICE_INVALID;
  300. }
  301. uint16_t vendorID = header->Vendor_ID;
  302. if (vendorID == 0xffff) // 设备不存在
  303. {
  304. kfree(header);
  305. return E_DEVICE_INVALID;
  306. }
  307. pci_checkFunction(bus, device, 0);
  308. header_type = header->HeaderType;
  309. if ((header_type & 0x80) != 0)
  310. {
  311. kdebug("Multi func device");
  312. // 这是一个多function的设备,因此查询剩余的function
  313. for (uint8_t func = 1; func < 8; ++func)
  314. {
  315. struct pci_device_structure_header_t *tmp_header;
  316. tmp_header = (struct pci_device_structure_header_t *)pci_read_header(&header_type, bus, device, func, false);
  317. if (tmp_header->Vendor_ID != 0xffff)
  318. pci_checkFunction(bus, device, func);
  319. // 释放内存
  320. kfree(tmp_header);
  321. }
  322. }
  323. kfree(header);
  324. return 0;
  325. }
  326. static void pci_checkBus(uint8_t bus)
  327. {
  328. for (uint8_t device = 0; device < 32; ++device)
  329. {
  330. pci_checkDevice(bus, device);
  331. }
  332. }
  333. /**
  334. * @brief 扫描所有pci总线上的所有设备
  335. *
  336. */
  337. void pci_checkAllBuses()
  338. {
  339. kinfo("Checking all devices in PCI bus...");
  340. int header_type;
  341. struct pci_device_structure_header_t *header = pci_read_header(&header_type, 0, 0, 0, false);
  342. if (header_type == E_WRONG_HEADER_TYPE)
  343. {
  344. kBUG("pci_checkAllBuses(): wrong header type!");
  345. // 此处内存已经在read header函数里面释放,不用重复释放
  346. return;
  347. }
  348. header_type = header->HeaderType;
  349. if ((header_type & 0x80) == 0) // Single pci host controller
  350. {
  351. pci_checkBus(0);
  352. }
  353. else
  354. {
  355. // Multiple PCI host controller
  356. // 那么总线0,设备0,功能1则是总线1的pci主机控制器,以此类推
  357. struct pci_device_structure_header_t *tmp_header;
  358. for (uint8_t func = 0; func < 8; ++func)
  359. {
  360. tmp_header = (struct pci_device_structure_header_t *)pci_read_header(&header_type, 0, 0, func, false);
  361. if (WARN_ON(header->Vendor_ID != 0xffff)) // @todo 这里的判断条件可能有点问题
  362. {
  363. kfree(tmp_header);
  364. break;
  365. }
  366. pci_checkBus(func);
  367. kfree(tmp_header);
  368. }
  369. }
  370. kfree(header);
  371. }
  372. void pci_init()
  373. {
  374. kinfo("Initializing PCI bus...");
  375. pci_checkAllBuses();
  376. kinfo("Total pci device and function num = %d", count_device_list);
  377. struct pci_device_structure_header_t *ptr = container_of(pci_device_structure_list, struct pci_device_structure_header_t, list);
  378. for (int i = 0; i < count_device_list; ++i)
  379. {
  380. if (ptr->HeaderType == 0x0)
  381. {
  382. if (ptr->Status & 0x10)
  383. {
  384. kinfo("[ pci device %d ] class code = %d\tsubclass=%d\tstatus=%#010lx\tcap_pointer=%#010lx\tbar5=%#010lx", i, ptr->Class_code, ptr->SubClass, ptr->Status, ((struct pci_device_structure_general_device_t *)ptr)->Capabilities_Pointer, ((struct pci_device_structure_general_device_t *)ptr)->BAR5);
  385. uint32_t tmp = pci_read_config(ptr->bus, ptr->device, ptr->func, ((struct pci_device_structure_general_device_t *)ptr)->Capabilities_Pointer);
  386. }
  387. else
  388. {
  389. kinfo("[ pci device %d ] class code = %d\tsubclass=%d\tstatus=%#010lx\t", i, ptr->Class_code, ptr->SubClass, ptr->Status);
  390. }
  391. }
  392. else if (ptr->HeaderType == 0x1)
  393. {
  394. if (ptr->Status & 0x10)
  395. {
  396. kinfo("[ pci device %d ] class code = %d\tsubclass=%d\tstatus=%#010lx\tcap_pointer=%#010lx", i, ptr->Class_code, ptr->SubClass, ptr->Status, ((struct pci_device_structure_pci_to_pci_bridge_t *)ptr)->Capability_Pointer);
  397. }
  398. else
  399. {
  400. kinfo("[ pci device %d ] class code = %d\tsubclass=%d\tstatus=%#010lx\t", i, ptr->Class_code, ptr->SubClass, ptr->Status);
  401. }
  402. }
  403. else if (ptr->HeaderType == 0x2)
  404. {
  405. kinfo("[ pci device %d ] class code = %d\tsubclass=%d\tstatus=%#010lx\t", i, ptr->Class_code, ptr->SubClass, ptr->Status);
  406. }
  407. ptr = container_of(list_next(&(ptr->list)), struct pci_device_structure_header_t, list);
  408. }
  409. kinfo("PCI bus initialized.")
  410. }
  411. /**
  412. * @brief 启用 Message Signaled Interrupts
  413. *
  414. * @param header 设备header
  415. * @param vector 中断向量号
  416. * @param processor 要投递到的处理器
  417. * @param edge_trigger 是否边缘触发
  418. * @param assert 是否高电平触发
  419. *
  420. * @return 返回码
  421. */
  422. int pci_enable_msi(void *header, uint8_t vector, uint32_t processor, uint8_t edge_trigger, uint8_t assert)
  423. {
  424. struct pci_device_structure_header_t *ptr = (struct pci_device_structure_header_t *)header;
  425. uint32_t cap_ptr;
  426. uint32_t tmp;
  427. uint16_t message_control;
  428. uint64_t message_addr;
  429. switch (ptr->HeaderType)
  430. {
  431. case 0x00: // general device
  432. if (!(ptr->Status & 0x10))
  433. return E_NOT_SUPPORT_MSI;
  434. cap_ptr = ((struct pci_device_structure_general_device_t *)ptr)->Capabilities_Pointer;
  435. tmp = pci_read_config(ptr->bus, ptr->device, ptr->func, cap_ptr); // 读取cap+0x0处的值
  436. message_control = (tmp >> 16) & 0xffff;
  437. if (tmp & 0xff != 0x5)
  438. return E_NOT_SUPPORT_MSI;
  439. // 写入message address
  440. message_addr = pci_get_arch_msi_message_address(processor); // 获取message address
  441. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0x4, (uint32_t)(message_addr & 0xffffffff));
  442. if (message_control & (1 << 7)) // 64位
  443. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0x8, (uint32_t)((message_addr >> 32) & 0xffffffff));
  444. // 写入message data
  445. tmp = pci_get_arch_msi_message_data(vector, processor, edge_trigger, assert);
  446. if (message_control & (1 << 7)) // 64位
  447. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0xc, tmp);
  448. else
  449. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0x8, tmp);
  450. // 使能msi
  451. tmp = pci_read_config(ptr->bus, ptr->device, ptr->func, cap_ptr); // 读取cap+0x0处的值
  452. tmp |= (1 << 16);
  453. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr, tmp);
  454. break;
  455. case 0x01: // pci to pci bridge
  456. if (!(ptr->Status & 0x10))
  457. return E_NOT_SUPPORT_MSI;
  458. cap_ptr = ((struct pci_device_structure_pci_to_pci_bridge_t *)ptr)->Capability_Pointer;
  459. tmp = pci_read_config(ptr->bus, ptr->device, ptr->func, cap_ptr); // 读取cap+0x0处的值
  460. message_control = (tmp >> 16) & 0xffff;
  461. if (tmp & 0xff != 0x5)
  462. return E_NOT_SUPPORT_MSI;
  463. // 写入message address
  464. message_addr = pci_get_arch_msi_message_address(processor); // 获取message address
  465. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0x4, (uint32_t)(message_addr & 0xffffffff));
  466. if (message_control & (1 << 7)) // 64位
  467. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0x8, (uint32_t)((message_addr >> 32) & 0xffffffff));
  468. // 写入message data
  469. tmp = pci_get_arch_msi_message_data(vector, processor, edge_trigger, assert);
  470. if (message_control & (1 << 7)) // 64位
  471. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0xc, tmp);
  472. else
  473. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr + 0x8, tmp);
  474. // 使能msi
  475. tmp = pci_read_config(ptr->bus, ptr->device, ptr->func, cap_ptr); // 读取cap+0x0处的值
  476. tmp |= (1 << 16);
  477. pci_write_config(ptr->bus, ptr->device, ptr->func, cap_ptr, tmp);
  478. break;
  479. case 0x02: // pci to card bus bridge
  480. return E_NOT_SUPPORT_MSI;
  481. break;
  482. default: // 不应该到达这里
  483. return E_WRONG_HEADER_TYPE;
  484. break;
  485. }
  486. return 0;
  487. }
  488. /**
  489. * @brief 获取 device structure
  490. *
  491. * @param class_code
  492. * @param sub_class
  493. * @param res 返回的结果数组
  494. */
  495. void pci_get_device_structure(uint8_t class_code, uint8_t sub_class, struct pci_device_structure_header_t *res[], uint32_t *count_res)
  496. {
  497. struct pci_device_structure_header_t *ptr = container_of(pci_device_structure_list, struct pci_device_structure_header_t, list);
  498. *count_res = 0;
  499. for (int i = 0; i < count_device_list; ++i)
  500. {
  501. if ((ptr->Class_code == class_code) && (ptr->SubClass == sub_class))
  502. {
  503. kdebug("[%d] class_code=%d, sub_class=%d, progIF=%d, bar5=%#010lx", i, ptr->Class_code, ptr->SubClass, ptr->ProgIF,((struct pci_device_structure_general_device_t *)ptr)->BAR5);
  504. res[*count_res] = ptr;
  505. ++(*count_res);
  506. }
  507. ptr = container_of(list_next(&(ptr->list)), struct pci_device_structure_header_t, list);
  508. }
  509. }