probe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-10-24 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #define DBG_TAG "pci.probe"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. #include <drivers/pci.h>
  15. #include <drivers/core/bus.h>
  16. #include "procfs.h"
  17. rt_inline void spin_lock(struct rt_spinlock *spinlock)
  18. {
  19. rt_hw_spin_lock(&spinlock->lock);
  20. }
  21. rt_inline void spin_unlock(struct rt_spinlock *spinlock)
  22. {
  23. rt_hw_spin_unlock(&spinlock->lock);
  24. }
  25. struct rt_pci_host_bridge *rt_pci_host_bridge_alloc(rt_size_t priv_size)
  26. {
  27. struct rt_pci_host_bridge *bridge = rt_calloc(1, sizeof(*bridge) + priv_size);
  28. return bridge;
  29. }
  30. rt_err_t rt_pci_host_bridge_free(struct rt_pci_host_bridge *bridge)
  31. {
  32. if (!bridge)
  33. {
  34. return -RT_EINVAL;
  35. }
  36. if (bridge->bus_regions)
  37. {
  38. rt_free(bridge->bus_regions);
  39. }
  40. if (bridge->dma_regions)
  41. {
  42. rt_free(bridge->dma_regions);
  43. }
  44. rt_free(bridge);
  45. return RT_EOK;
  46. }
  47. rt_err_t rt_pci_host_bridge_init(struct rt_pci_host_bridge *host_bridge)
  48. {
  49. rt_err_t err = RT_EOK;
  50. if (host_bridge->parent.ofw_node)
  51. {
  52. err = rt_pci_ofw_host_bridge_init(host_bridge->parent.ofw_node, host_bridge);
  53. }
  54. return err;
  55. }
  56. struct rt_pci_device *rt_pci_alloc_device(struct rt_pci_bus *bus)
  57. {
  58. struct rt_pci_device *pdev = rt_calloc(1, sizeof(*pdev));
  59. if (!pdev)
  60. {
  61. return RT_NULL;
  62. }
  63. rt_list_init(&pdev->list);
  64. pdev->bus = bus;
  65. if (bus)
  66. {
  67. spin_lock(&bus->lock);
  68. rt_list_insert_before(&bus->devices_nodes, &pdev->list);
  69. spin_unlock(&bus->lock);
  70. }
  71. pdev->subsystem_vendor = PCI_ANY_ID;
  72. pdev->subsystem_device = PCI_ANY_ID;
  73. pdev->irq = -1;
  74. for (int i = 0; i < RT_ARRAY_SIZE(pdev->resource); ++i)
  75. {
  76. pdev->resource[i].flags = PCI_BUS_REGION_F_NONE;
  77. }
  78. #ifdef RT_PCI_MSI
  79. rt_list_init(&pdev->msi_desc_nodes);
  80. rt_spin_lock_init(&pdev->msi_lock);
  81. #endif
  82. return pdev;
  83. }
  84. struct rt_pci_device *rt_pci_scan_single_device(struct rt_pci_bus *bus, rt_uint32_t devfn)
  85. {
  86. rt_err_t err;
  87. struct rt_pci_device *pdev = RT_NULL;
  88. rt_uint16_t vendor = PCI_ANY_ID, device = PCI_ANY_ID;
  89. if (!bus)
  90. {
  91. goto _end;
  92. }
  93. err = rt_pci_bus_read_config_u16(bus, devfn, PCIR_VENDOR, &vendor);
  94. rt_pci_bus_read_config_u16(bus, devfn, PCIR_DEVICE, &device);
  95. if (vendor == (typeof(vendor))PCI_ANY_ID ||
  96. vendor == (typeof(vendor))0x0000 || err)
  97. {
  98. goto _end;
  99. }
  100. if (!(pdev = rt_pci_alloc_device(bus)))
  101. {
  102. goto _end;
  103. }
  104. pdev->devfn = devfn;
  105. pdev->vendor = vendor;
  106. pdev->device = device;
  107. rt_dm_dev_set_name(&pdev->parent, "%04x:%02x:%02x.%u",
  108. rt_pci_domain(pdev), pdev->bus->number,
  109. RT_PCI_SLOT(pdev->devfn), RT_PCI_FUNC(pdev->devfn));
  110. if (rt_pci_setup_device(pdev))
  111. {
  112. rt_free(pdev);
  113. pdev = RT_NULL;
  114. goto _end;
  115. }
  116. pci_procfs_attach(pdev);
  117. rt_pci_device_register(pdev);
  118. _end:
  119. return pdev;
  120. }
  121. static rt_bool_t pci_intx_mask_broken(struct rt_pci_device *pdev)
  122. {
  123. rt_bool_t res = RT_FALSE;
  124. rt_uint16_t orig, toggle, new;
  125. rt_pci_read_config_u16(pdev, PCIR_COMMAND, &orig);
  126. toggle = orig ^ PCIM_CMD_INTxDIS;
  127. rt_pci_write_config_u16(pdev, PCIR_COMMAND, toggle);
  128. rt_pci_read_config_u16(pdev, PCIR_COMMAND, &new);
  129. rt_pci_write_config_u16(pdev, PCIR_COMMAND, orig);
  130. if (new != toggle)
  131. {
  132. res = RT_TRUE;
  133. }
  134. return res;
  135. }
  136. static void pci_read_irq(struct rt_pci_device *pdev)
  137. {
  138. rt_uint8_t irq = 0;
  139. rt_pci_read_config_u8(pdev, PCIR_INTPIN, &irq);
  140. pdev->pin = irq;
  141. if (irq)
  142. {
  143. rt_pci_read_config_u8(pdev, PCIR_INTLINE, &irq);
  144. }
  145. pdev->irq = irq;
  146. }
  147. static void pcie_set_port_type(struct rt_pci_device *pdev)
  148. {
  149. int pos;
  150. if (!(pos = rt_pci_find_capability(pdev, PCIY_EXPRESS)))
  151. {
  152. return;
  153. }
  154. pdev->pcie_cap = pos;
  155. }
  156. static void pci_configure_ari(struct rt_pci_device *pdev)
  157. {
  158. rt_uint32_t cap, ctl2_ari;
  159. struct rt_pci_device *bridge;
  160. if (!rt_pci_is_pcie(pdev) || pdev->devfn)
  161. {
  162. return;
  163. }
  164. bridge = pdev->bus->self;
  165. if (rt_pci_is_root_bus(pdev->bus) || !bridge)
  166. {
  167. return;
  168. }
  169. rt_pci_read_config_u32(bridge, bridge->pcie_cap + PCIER_DEVICE_CAP2, &cap);
  170. if (!(cap & PCIEM_CAP2_ARI))
  171. {
  172. return;
  173. }
  174. rt_pci_read_config_u32(bridge, bridge->pcie_cap + PCIER_DEVICE_CTL2, &ctl2_ari);
  175. if (rt_pci_find_ext_capability(pdev, PCIZ_ARI))
  176. {
  177. ctl2_ari |= PCIEM_CTL2_ARI;
  178. bridge->ari_enabled = RT_TRUE;
  179. }
  180. else
  181. {
  182. ctl2_ari &= ~PCIEM_CTL2_ARI;
  183. bridge->ari_enabled = RT_FALSE;
  184. }
  185. rt_pci_write_config_u32(bridge, bridge->pcie_cap + PCIER_DEVICE_CTL2, ctl2_ari);
  186. }
  187. static rt_uint16_t pci_cfg_space_size_ext(struct rt_pci_device *pdev)
  188. {
  189. rt_uint32_t status;
  190. if (rt_pci_read_config_u32(pdev, PCI_REGMAX + 1, &status))
  191. {
  192. return PCI_REGMAX + 1;
  193. }
  194. return PCIE_REGMAX + 1;
  195. }
  196. static rt_uint16_t pci_cfg_space_size(struct rt_pci_device *pdev)
  197. {
  198. int pos;
  199. rt_uint32_t status;
  200. rt_uint16_t class = pdev->class >> 8;
  201. if (class == PCIS_BRIDGE_HOST)
  202. {
  203. return pci_cfg_space_size_ext(pdev);
  204. }
  205. if (rt_pci_is_pcie(pdev))
  206. {
  207. return pci_cfg_space_size_ext(pdev);
  208. }
  209. pos = rt_pci_find_capability(pdev, PCIY_PCIX);
  210. if (!pos)
  211. {
  212. return PCI_REGMAX + 1;
  213. }
  214. rt_pci_read_config_u32(pdev, pos + PCIXR_STATUS, &status);
  215. if (status & (PCIXM_STATUS_266CAP | PCIXM_STATUS_533CAP))
  216. {
  217. return pci_cfg_space_size_ext(pdev);
  218. }
  219. return PCI_REGMAX + 1;
  220. }
  221. static void pci_init_capabilities(struct rt_pci_device *pdev)
  222. {
  223. rt_pci_pme_init(pdev);
  224. #ifdef RT_PCI_MSI
  225. rt_pci_msi_init(pdev); /* Disable MSI */
  226. rt_pci_msix_init(pdev); /* Disable MSI-X */
  227. #endif
  228. pcie_set_port_type(pdev);
  229. pdev->cfg_size = pci_cfg_space_size(pdev);
  230. pci_configure_ari(pdev);
  231. pdev->no_msi = RT_FALSE;
  232. pdev->msi_enabled = RT_FALSE;
  233. pdev->msix_enabled = RT_FALSE;
  234. }
  235. rt_err_t rt_pci_setup_device(struct rt_pci_device *pdev)
  236. {
  237. rt_uint8_t pos;
  238. rt_uint32_t class = 0;
  239. struct rt_pci_host_bridge *host_bridge;
  240. if (!pdev)
  241. {
  242. return -RT_EINVAL;
  243. }
  244. if (!(host_bridge = rt_pci_find_host_bridge(pdev->bus)))
  245. {
  246. return -RT_EINVAL;
  247. }
  248. rt_pci_ofw_device_init(pdev);
  249. rt_pci_read_config_u32(pdev, PCIR_REVID, &class);
  250. pdev->revision = class & 0xff;
  251. pdev->class = class >> 8; /* Upper 3 bytes */
  252. rt_pci_read_config_u8(pdev, PCIR_HDRTYPE, &pdev->hdr_type);
  253. /* Clear errors left from system firmware */
  254. rt_pci_write_config_u16(pdev, PCIR_STATUS, 0xffff);
  255. if (pdev->hdr_type & 0x80)
  256. {
  257. pdev->multi_function = RT_TRUE;
  258. }
  259. pdev->hdr_type &= PCIM_HDRTYPE;
  260. if (pci_intx_mask_broken(pdev))
  261. {
  262. pdev->broken_intx_masking = RT_TRUE;
  263. }
  264. rt_dm_dev_set_name(&pdev->parent, "%04x:%02x:%02x.%u", rt_pci_domain(pdev),
  265. pdev->bus->number, RT_PCI_SLOT(pdev->devfn), RT_PCI_FUNC(pdev->devfn));
  266. switch (pdev->hdr_type)
  267. {
  268. case PCIM_HDRTYPE_NORMAL:
  269. if (class == PCIS_BRIDGE_PCI)
  270. {
  271. goto error;
  272. }
  273. pci_read_irq(pdev);
  274. rt_pci_device_alloc_resource(host_bridge, pdev);
  275. rt_pci_read_config_u16(pdev, PCIR_SUBVEND_0, &pdev->subsystem_vendor);
  276. rt_pci_read_config_u16(pdev, PCIR_SUBDEV_0, &pdev->subsystem_device);
  277. break;
  278. case PCIM_HDRTYPE_BRIDGE:
  279. pci_read_irq(pdev);
  280. rt_pci_device_alloc_resource(host_bridge, pdev);
  281. pos = rt_pci_find_capability(pdev, PCIY_SUBVENDOR);
  282. if (pos)
  283. {
  284. rt_pci_read_config_u16(pdev, PCIR_SUBVENDCAP, &pdev->subsystem_vendor);
  285. rt_pci_read_config_u16(pdev, PCIR_SUBDEVCAP, &pdev->subsystem_device);
  286. }
  287. break;
  288. case PCIM_HDRTYPE_CARDBUS:
  289. if (class != PCIS_BRIDGE_CARDBUS)
  290. {
  291. goto error;
  292. }
  293. pci_read_irq(pdev);
  294. rt_pci_device_alloc_resource(host_bridge, pdev);
  295. rt_pci_read_config_u16(pdev, PCIR_SUBVEND_2, &pdev->subsystem_vendor);
  296. rt_pci_read_config_u16(pdev, PCIR_SUBDEV_2, &pdev->subsystem_device);
  297. break;
  298. default:
  299. LOG_E("Ignoring device unknown header type %02x", pdev->hdr_type);
  300. return -RT_EIO;
  301. error:
  302. LOG_E("Ignoring class %08x (doesn't match header type %02x)", pdev->class, pdev->hdr_type);
  303. pdev->class = PCIC_NOT_DEFINED << 8;
  304. }
  305. pci_init_capabilities(pdev);
  306. if (rt_pci_is_pcie(pdev))
  307. {
  308. rt_pci_read_config_u16(pdev, pdev->pcie_cap + PCIER_FLAGS, &pdev->exp_flags);
  309. }
  310. return RT_EOK;
  311. }
  312. static struct rt_pci_bus *pci_alloc_bus(struct rt_pci_bus *parent);
  313. static rt_err_t pci_child_bus_init(struct rt_pci_bus *bus, rt_uint32_t bus_no,
  314. struct rt_pci_host_bridge *host_bridge, struct rt_pci_device *pdev)
  315. {
  316. rt_err_t err;
  317. struct rt_pci_bus *parent_bus = bus->parent;
  318. bus->sysdata = parent_bus->sysdata;
  319. bus->self = pdev;
  320. bus->ops = host_bridge->child_ops ? : parent_bus->ops;
  321. bus->number = bus_no;
  322. rt_sprintf(bus->name, "%04x:%02x", host_bridge->domain, bus_no);
  323. rt_pci_ofw_bus_init(bus);
  324. if (bus->ops->add)
  325. {
  326. if ((err = bus->ops->add(bus)))
  327. {
  328. rt_pci_ofw_bus_free(bus);
  329. LOG_E("PCI-Bus<%s> add bus failed with err = %s",
  330. bus->name, rt_strerror(err));
  331. return err;
  332. }
  333. }
  334. return RT_EOK;
  335. }
  336. static rt_bool_t pci_ea_fixed_busnrs(struct rt_pci_device *pdev,
  337. rt_uint8_t *sec, rt_uint8_t *sub)
  338. {
  339. int pos, offset;
  340. rt_uint32_t dw;
  341. rt_uint8_t ea_sec, ea_sub;
  342. pos = rt_pci_find_capability(pdev, PCIY_EA);
  343. if (!pos)
  344. {
  345. return RT_FALSE;
  346. }
  347. offset = pos + PCIR_EA_FIRST_ENT;
  348. rt_pci_read_config_u32(pdev, offset, &dw);
  349. ea_sec = PCIM_EA_SEC_NR(dw);
  350. ea_sub = PCIM_EA_SUB_NR(dw);
  351. if (ea_sec == 0 || ea_sub < ea_sec)
  352. {
  353. return RT_FALSE;
  354. }
  355. *sec = ea_sec;
  356. *sub = ea_sub;
  357. return RT_TRUE;
  358. }
  359. static void pcie_fixup_link(struct rt_pci_device *pdev)
  360. {
  361. int pos = pdev->pcie_cap;
  362. rt_uint16_t exp_lnkctl, exp_lnkctl2, exp_lnksta;
  363. rt_uint16_t exp_type = pdev->exp_flags & PCIEM_FLAGS_TYPE;
  364. if ((pdev->exp_flags & PCIEM_FLAGS_VERSION) < 2)
  365. {
  366. return;
  367. }
  368. if (exp_type != PCIEM_TYPE_ROOT_PORT &&
  369. exp_type != PCIEM_TYPE_DOWNSTREAM_PORT &&
  370. exp_type != PCIEM_TYPE_PCIE_BRIDGE)
  371. {
  372. return;
  373. }
  374. rt_pci_read_config_u16(pdev, pos + PCIER_LINK_CTL, &exp_lnkctl);
  375. rt_pci_read_config_u16(pdev, pos + PCIER_LINK_CTL2, &exp_lnkctl2);
  376. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL2,
  377. (exp_lnkctl2 & ~PCIEM_LNKCTL2_TLS) | PCIEM_LNKCTL2_TLS_2_5GT);
  378. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL,
  379. exp_lnkctl | PCIEM_LINK_CTL_RETRAIN_LINK);
  380. for (int i = 0; i < 20; ++i)
  381. {
  382. rt_pci_read_config_u16(pdev, pos + PCIER_LINK_STA, &exp_lnksta);
  383. if (!!(exp_lnksta & PCIEM_LINK_STA_DL_ACTIVE))
  384. {
  385. goto _status_sync;
  386. }
  387. rt_thread_mdelay(10);
  388. }
  389. /* Fail, restore */
  390. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL2, exp_lnkctl2);
  391. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL,
  392. exp_lnkctl | PCIEM_LINK_CTL_RETRAIN_LINK);
  393. _status_sync:
  394. /* Wait a while for success or failure */
  395. rt_thread_mdelay(100);
  396. }
  397. static rt_uint32_t pci_scan_bridge_extend(struct rt_pci_bus *bus, struct rt_pci_device *pdev,
  398. rt_uint32_t bus_no_start, rt_uint32_t buses, rt_bool_t reconfigured)
  399. {
  400. rt_bool_t fixed_buses;
  401. rt_uint8_t fixed_sub, fixed_sec;
  402. rt_uint8_t primary, secondary, subordinate;
  403. rt_uint32_t value, bus_no = bus_no_start;
  404. struct rt_pci_bus *next_bus;
  405. struct rt_pci_host_bridge *host_bridge;
  406. /* We not supported init CardBus, it always used in the PC servers. */
  407. if (pdev->hdr_type == PCIM_HDRTYPE_CARDBUS)
  408. {
  409. LOG_E("CardBus is not supported in system");
  410. goto _end;
  411. }
  412. rt_pci_read_config_u32(pdev, PCIR_PRIBUS_1, &value);
  413. primary = value & 0xff;
  414. secondary = (value >> 8) & 0xff;
  415. subordinate = (value >> 16) & 0xff;
  416. if (primary == bus->number && bus->number > secondary && secondary > subordinate)
  417. {
  418. if (!reconfigured)
  419. {
  420. goto _end;
  421. }
  422. LOG_I("Bridge configuration: primary(%02x) secondary(%02x) subordinate(%02x)",
  423. primary, secondary, subordinate);
  424. }
  425. if (pdev->pcie_cap)
  426. {
  427. pcie_fixup_link(pdev);
  428. }
  429. ++bus_no;
  430. /* Count of subordinate */
  431. buses -= !!buses;
  432. host_bridge = rt_pci_find_host_bridge(bus);
  433. RT_ASSERT(host_bridge != RT_NULL);
  434. /* Clear errors */
  435. rt_pci_write_config_u16(pdev, PCIR_STATUS, RT_UINT16_MAX);
  436. fixed_buses = pci_ea_fixed_busnrs(pdev, &fixed_sec, &fixed_sub);
  437. if (!(next_bus = pci_alloc_bus(bus)))
  438. {
  439. goto _end;
  440. }
  441. /* Clear bus info */
  442. rt_pci_write_config_u32(pdev, PCIR_PRIBUS_1, value & ~0xffffff);
  443. if (!(next_bus = pci_alloc_bus(bus)))
  444. {
  445. LOG_E("Alloc bus(%02x) fail", bus_no);
  446. goto _end;
  447. }
  448. if (pci_child_bus_init(next_bus, bus_no, host_bridge, pdev))
  449. {
  450. goto _end;
  451. }
  452. /* Fill primary, secondary */
  453. value = (buses & 0xff000000) | (bus->number << 0) | (next_bus->number << 8);
  454. rt_pci_write_config_u32(pdev, PCIR_PRIBUS_1, value);
  455. bus_no = rt_pci_scan_child_buses(next_bus, buses);
  456. /* Fill subordinate */
  457. value |= next_bus->number + rt_list_len(&next_bus->children_nodes);
  458. rt_pci_write_config_u32(pdev, PCIR_PRIBUS_1, value);
  459. if (fixed_buses)
  460. {
  461. bus_no = fixed_sub;
  462. }
  463. rt_pci_write_config_u8(pdev, PCIR_SUBBUS_1, bus_no);
  464. _end:
  465. return bus_no;
  466. }
  467. rt_uint32_t rt_pci_scan_bridge(struct rt_pci_bus *bus, struct rt_pci_device *pdev,
  468. rt_uint32_t bus_no_start, rt_bool_t reconfigured)
  469. {
  470. if (!bus || !pdev)
  471. {
  472. return RT_UINT32_MAX;
  473. }
  474. return pci_scan_bridge_extend(bus, pdev, bus_no_start, 0, reconfigured);
  475. }
  476. rt_inline rt_bool_t only_one_child(struct rt_pci_bus *bus)
  477. {
  478. struct rt_pci_device *pdev;
  479. if (rt_pci_is_root_bus(bus))
  480. {
  481. return RT_FALSE;
  482. }
  483. pdev = bus->self;
  484. if (rt_pci_is_pcie(pdev))
  485. {
  486. rt_uint16_t exp_type = pdev->exp_flags & PCIEM_FLAGS_TYPE;
  487. if (exp_type == PCIEM_TYPE_ROOT_PORT ||
  488. exp_type == PCIEM_TYPE_DOWNSTREAM_PORT ||
  489. exp_type == PCIEM_TYPE_PCIE_BRIDGE)
  490. {
  491. return RT_TRUE;
  492. }
  493. }
  494. return RT_FALSE;
  495. }
  496. static int next_fn(struct rt_pci_bus *bus, struct rt_pci_device *pdev, int fn)
  497. {
  498. if (!rt_pci_is_root_bus(bus) && bus->self->ari_enabled)
  499. {
  500. int pos, next_fn;
  501. rt_uint16_t cap = 0;
  502. if (!pdev)
  503. {
  504. return -RT_EINVAL;
  505. }
  506. pos = rt_pci_find_ext_capability(pdev, PCIZ_ARI);
  507. if (!pos)
  508. {
  509. return -RT_EINVAL;
  510. }
  511. rt_pci_read_config_u16(pdev, pos + PCIR_ARI_CAP, &cap);
  512. next_fn = PCIM_ARI_CAP_NFN(cap);
  513. if (next_fn <= fn)
  514. {
  515. return -RT_EINVAL;
  516. }
  517. return next_fn;
  518. }
  519. if (fn >= RT_PCI_FUNCTION_MAX - 1)
  520. {
  521. return -RT_EINVAL;
  522. }
  523. if (pdev && !pdev->multi_function)
  524. {
  525. return -RT_EINVAL;
  526. }
  527. return fn + 1;
  528. }
  529. rt_size_t rt_pci_scan_slot(struct rt_pci_bus *bus, rt_uint32_t devfn)
  530. {
  531. rt_size_t nr = 0;
  532. struct rt_pci_device *pdev = RT_NULL;
  533. if (!bus)
  534. {
  535. return nr;
  536. }
  537. if (devfn > 0 && only_one_child(bus))
  538. {
  539. return nr;
  540. }
  541. for (int func = 0; func >= 0; func = next_fn(bus, pdev, func))
  542. {
  543. pdev = rt_pci_scan_single_device(bus, devfn + func);
  544. if (pdev)
  545. {
  546. ++nr;
  547. if (func > 0)
  548. {
  549. pdev->multi_function = RT_TRUE;
  550. }
  551. }
  552. else if (func == 0)
  553. {
  554. break;
  555. }
  556. }
  557. return nr;
  558. }
  559. rt_uint32_t rt_pci_scan_child_buses(struct rt_pci_bus *bus, rt_size_t buses)
  560. {
  561. rt_uint32_t bus_no;
  562. struct rt_pci_device *pdev = RT_NULL;
  563. if (!bus)
  564. {
  565. bus_no = RT_UINT32_MAX;
  566. goto _end;
  567. }
  568. bus_no = bus->number;
  569. for (rt_uint32_t devfn = 0;
  570. devfn < RT_PCI_DEVFN(RT_PCI_DEVICE_MAX - 1, RT_PCI_FUNCTION_MAX - 1);
  571. devfn += RT_PCI_FUNCTION_MAX)
  572. {
  573. rt_pci_scan_slot(bus, devfn);
  574. }
  575. rt_pci_foreach_bridge(pdev, bus)
  576. {
  577. int offset;
  578. bus_no = pci_scan_bridge_extend(bus, pdev, bus_no, buses, RT_TRUE);
  579. offset = bus_no - bus->number;
  580. if (buses > offset)
  581. {
  582. buses -= offset;
  583. }
  584. else
  585. {
  586. break;
  587. }
  588. }
  589. _end:
  590. return bus_no;
  591. }
  592. rt_uint32_t rt_pci_scan_child_bus(struct rt_pci_bus *bus)
  593. {
  594. return rt_pci_scan_child_buses(bus, 0);
  595. }
  596. static struct rt_pci_bus *pci_alloc_bus(struct rt_pci_bus *parent)
  597. {
  598. struct rt_pci_bus *bus = rt_calloc(1, sizeof(*bus));
  599. if (!bus)
  600. {
  601. return RT_NULL;
  602. }
  603. bus->parent = parent;
  604. rt_list_init(&bus->list);
  605. rt_list_init(&bus->children_nodes);
  606. rt_list_init(&bus->devices_nodes);
  607. rt_spin_lock_init(&bus->lock);
  608. return bus;
  609. }
  610. rt_err_t rt_pci_host_bridge_register(struct rt_pci_host_bridge *host_bridge)
  611. {
  612. struct rt_pci_bus *bus = pci_alloc_bus(RT_NULL);
  613. if (!bus)
  614. {
  615. return -RT_ENOMEM;
  616. }
  617. host_bridge->root_bus = bus;
  618. bus->sysdata = host_bridge->sysdata;
  619. bus->host_bridge = host_bridge;
  620. bus->ops = host_bridge->ops;
  621. bus->number = host_bridge->bus_range[0];
  622. rt_sprintf(bus->name, "%04x:%02x", host_bridge->domain, bus->number);
  623. if (bus->ops->add)
  624. {
  625. rt_err_t err = bus->ops->add(bus);
  626. if (err)
  627. {
  628. LOG_E("PCI-Bus<%s> add bus failed with err = %s", bus->name, rt_strerror(err));
  629. }
  630. }
  631. return RT_EOK;
  632. }
  633. rt_err_t rt_pci_scan_root_bus_bridge(struct rt_pci_host_bridge *host_bridge)
  634. {
  635. rt_err_t err;
  636. if ((err = rt_pci_host_bridge_register(host_bridge)))
  637. {
  638. return err;
  639. }
  640. rt_pci_scan_child_bus(host_bridge->root_bus);
  641. return err;
  642. }
  643. rt_err_t rt_pci_host_bridge_probe(struct rt_pci_host_bridge *host_bridge)
  644. {
  645. rt_err_t err;
  646. err = rt_pci_scan_root_bus_bridge(host_bridge);
  647. return err;
  648. }
  649. static rt_bool_t pci_remove_bus_device(struct rt_pci_device *pdev, void *data)
  650. {
  651. /* Bus will free if this is the last device */
  652. rt_bus_remove_device(&pdev->parent);
  653. /* To find all devices, always return false */
  654. return RT_FALSE;
  655. }
  656. rt_err_t rt_pci_host_bridge_remove(struct rt_pci_host_bridge *host_bridge)
  657. {
  658. rt_err_t err = RT_EOK;
  659. if (host_bridge && host_bridge->root_bus)
  660. {
  661. rt_pci_enum_device(host_bridge->root_bus, pci_remove_bus_device, RT_NULL);
  662. host_bridge->root_bus = RT_NULL;
  663. }
  664. else
  665. {
  666. err = -RT_EINVAL;
  667. }
  668. return err;
  669. }
  670. rt_err_t rt_pci_bus_remove(struct rt_pci_bus *bus)
  671. {
  672. rt_err_t err = RT_EOK;
  673. if (bus)
  674. {
  675. spin_lock(&bus->lock);
  676. if (rt_list_isempty(&bus->children_nodes) &&
  677. rt_list_isempty(&bus->devices_nodes))
  678. {
  679. rt_list_remove(&bus->list);
  680. spin_unlock(&bus->lock);
  681. if (bus->ops->remove)
  682. {
  683. bus->ops->remove(bus);
  684. }
  685. rt_pci_ofw_bus_free(bus);
  686. rt_free(bus);
  687. }
  688. else
  689. {
  690. spin_unlock(&bus->lock);
  691. err = -RT_EBUSY;
  692. }
  693. }
  694. else
  695. {
  696. err = -RT_EINVAL;
  697. }
  698. return err;
  699. }
  700. rt_err_t rt_pci_device_remove(struct rt_pci_device *pdev)
  701. {
  702. rt_err_t err = RT_EOK;
  703. if (pdev)
  704. {
  705. struct rt_pci_bus *bus = pdev->bus;
  706. pci_procfs_detach(pdev);
  707. spin_lock(&bus->lock);
  708. while (pdev->parent.ref_count > 1)
  709. {
  710. spin_unlock(&bus->lock);
  711. rt_thread_yield();
  712. spin_lock(&bus->lock);
  713. }
  714. rt_list_remove(&pdev->list);
  715. spin_unlock(&bus->lock);
  716. rt_free(pdev);
  717. }
  718. else
  719. {
  720. err = -RT_EINVAL;
  721. }
  722. return err;
  723. }