pci.c 17 KB

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