pci.c 14 KB

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