pci.c 14 KB

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