probe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. class = pdev->class >> 8;
  267. switch (pdev->hdr_type)
  268. {
  269. case PCIM_HDRTYPE_NORMAL:
  270. if (class == PCIS_BRIDGE_PCI)
  271. {
  272. goto error;
  273. }
  274. pci_read_irq(pdev);
  275. rt_pci_device_alloc_resource(host_bridge, pdev);
  276. rt_pci_read_config_u16(pdev, PCIR_SUBVEND_0, &pdev->subsystem_vendor);
  277. rt_pci_read_config_u16(pdev, PCIR_SUBDEV_0, &pdev->subsystem_device);
  278. break;
  279. case PCIM_HDRTYPE_BRIDGE:
  280. pci_read_irq(pdev);
  281. rt_pci_device_alloc_resource(host_bridge, pdev);
  282. pos = rt_pci_find_capability(pdev, PCIY_SUBVENDOR);
  283. if (pos)
  284. {
  285. rt_pci_read_config_u16(pdev, PCIR_SUBVENDCAP, &pdev->subsystem_vendor);
  286. rt_pci_read_config_u16(pdev, PCIR_SUBDEVCAP, &pdev->subsystem_device);
  287. }
  288. break;
  289. case PCIM_HDRTYPE_CARDBUS:
  290. if (class != PCIS_BRIDGE_CARDBUS)
  291. {
  292. goto error;
  293. }
  294. pci_read_irq(pdev);
  295. rt_pci_device_alloc_resource(host_bridge, pdev);
  296. rt_pci_read_config_u16(pdev, PCIR_SUBVEND_2, &pdev->subsystem_vendor);
  297. rt_pci_read_config_u16(pdev, PCIR_SUBDEV_2, &pdev->subsystem_device);
  298. break;
  299. default:
  300. LOG_E("Ignoring device unknown header type %02x", pdev->hdr_type);
  301. return -RT_EIO;
  302. error:
  303. LOG_E("Ignoring class %08x (doesn't match header type %02x)", pdev->class, pdev->hdr_type);
  304. pdev->class = PCIC_NOT_DEFINED << 8;
  305. }
  306. pci_init_capabilities(pdev);
  307. if (rt_pci_is_pcie(pdev))
  308. {
  309. rt_pci_read_config_u16(pdev, pdev->pcie_cap + PCIER_FLAGS, &pdev->exp_flags);
  310. }
  311. return RT_EOK;
  312. }
  313. static struct rt_pci_bus *pci_alloc_bus(struct rt_pci_bus *parent);
  314. static rt_err_t pci_child_bus_init(struct rt_pci_bus *bus, rt_uint32_t bus_no,
  315. struct rt_pci_host_bridge *host_bridge, struct rt_pci_device *pdev)
  316. {
  317. rt_err_t err;
  318. struct rt_pci_bus *parent_bus = bus->parent;
  319. bus->sysdata = parent_bus->sysdata;
  320. bus->self = pdev;
  321. bus->ops = host_bridge->child_ops ? : parent_bus->ops;
  322. bus->number = bus_no;
  323. rt_sprintf(bus->name, "%04x:%02x", host_bridge->domain, bus_no);
  324. rt_pci_ofw_bus_init(bus);
  325. if (bus->ops->add)
  326. {
  327. if ((err = bus->ops->add(bus)))
  328. {
  329. rt_pci_ofw_bus_free(bus);
  330. LOG_E("PCI-Bus<%s> add bus failed with err = %s",
  331. bus->name, rt_strerror(err));
  332. return err;
  333. }
  334. }
  335. return RT_EOK;
  336. }
  337. static rt_bool_t pci_ea_fixed_busnrs(struct rt_pci_device *pdev,
  338. rt_uint8_t *sec, rt_uint8_t *sub)
  339. {
  340. int pos, offset;
  341. rt_uint32_t dw;
  342. rt_uint8_t ea_sec, ea_sub;
  343. pos = rt_pci_find_capability(pdev, PCIY_EA);
  344. if (!pos)
  345. {
  346. return RT_FALSE;
  347. }
  348. offset = pos + PCIR_EA_FIRST_ENT;
  349. rt_pci_read_config_u32(pdev, offset, &dw);
  350. ea_sec = PCIM_EA_SEC_NR(dw);
  351. ea_sub = PCIM_EA_SUB_NR(dw);
  352. if (ea_sec == 0 || ea_sub < ea_sec)
  353. {
  354. return RT_FALSE;
  355. }
  356. *sec = ea_sec;
  357. *sub = ea_sub;
  358. return RT_TRUE;
  359. }
  360. static void pcie_fixup_link(struct rt_pci_device *pdev)
  361. {
  362. int pos = pdev->pcie_cap;
  363. rt_uint16_t exp_lnkctl, exp_lnkctl2, exp_lnksta;
  364. rt_uint16_t exp_type = pdev->exp_flags & PCIEM_FLAGS_TYPE;
  365. if ((pdev->exp_flags & PCIEM_FLAGS_VERSION) < 2)
  366. {
  367. return;
  368. }
  369. if (exp_type != PCIEM_TYPE_ROOT_PORT &&
  370. exp_type != PCIEM_TYPE_DOWNSTREAM_PORT &&
  371. exp_type != PCIEM_TYPE_PCIE_BRIDGE)
  372. {
  373. return;
  374. }
  375. rt_pci_read_config_u16(pdev, pos + PCIER_LINK_CTL, &exp_lnkctl);
  376. rt_pci_read_config_u16(pdev, pos + PCIER_LINK_CTL2, &exp_lnkctl2);
  377. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL2,
  378. (exp_lnkctl2 & ~PCIEM_LNKCTL2_TLS) | PCIEM_LNKCTL2_TLS_2_5GT);
  379. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL,
  380. exp_lnkctl | PCIEM_LINK_CTL_RETRAIN_LINK);
  381. for (int i = 0; i < 20; ++i)
  382. {
  383. rt_pci_read_config_u16(pdev, pos + PCIER_LINK_STA, &exp_lnksta);
  384. if (!!(exp_lnksta & PCIEM_LINK_STA_DL_ACTIVE))
  385. {
  386. goto _status_sync;
  387. }
  388. rt_thread_mdelay(10);
  389. }
  390. /* Fail, restore */
  391. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL2, exp_lnkctl2);
  392. rt_pci_write_config_u16(pdev, pos + PCIER_LINK_CTL,
  393. exp_lnkctl | PCIEM_LINK_CTL_RETRAIN_LINK);
  394. _status_sync:
  395. /* Wait a while for success or failure */
  396. rt_thread_mdelay(100);
  397. }
  398. static rt_uint32_t pci_scan_bridge_extend(struct rt_pci_bus *bus, struct rt_pci_device *pdev,
  399. rt_uint32_t bus_no_start, rt_uint32_t buses, rt_bool_t reconfigured)
  400. {
  401. rt_bool_t fixed_buses;
  402. rt_uint8_t fixed_sub, fixed_sec;
  403. rt_uint8_t primary, secondary, subordinate;
  404. rt_uint32_t value, bus_no = bus_no_start;
  405. struct rt_pci_bus *next_bus;
  406. struct rt_pci_host_bridge *host_bridge;
  407. /* We not supported init CardBus, it always used in the PC servers. */
  408. if (pdev->hdr_type == PCIM_HDRTYPE_CARDBUS)
  409. {
  410. LOG_E("CardBus is not supported in system");
  411. goto _end;
  412. }
  413. rt_pci_read_config_u32(pdev, PCIR_PRIBUS_1, &value);
  414. primary = value & 0xff;
  415. secondary = (value >> 8) & 0xff;
  416. subordinate = (value >> 16) & 0xff;
  417. if (primary == bus->number && bus->number > secondary && secondary > subordinate)
  418. {
  419. if (!reconfigured)
  420. {
  421. goto _end;
  422. }
  423. LOG_I("Bridge configuration: primary(%02x) secondary(%02x) subordinate(%02x)",
  424. primary, secondary, subordinate);
  425. }
  426. if (pdev->pcie_cap)
  427. {
  428. pcie_fixup_link(pdev);
  429. }
  430. ++bus_no;
  431. /* Count of subordinate */
  432. buses -= !!buses;
  433. host_bridge = rt_pci_find_host_bridge(bus);
  434. RT_ASSERT(host_bridge != RT_NULL);
  435. /* Clear errors */
  436. rt_pci_write_config_u16(pdev, PCIR_STATUS, RT_UINT16_MAX);
  437. fixed_buses = pci_ea_fixed_busnrs(pdev, &fixed_sec, &fixed_sub);
  438. if (!(next_bus = pci_alloc_bus(bus)))
  439. {
  440. goto _end;
  441. }
  442. /* Clear bus info */
  443. rt_pci_write_config_u32(pdev, PCIR_PRIBUS_1, value & ~0xffffff);
  444. if (!(next_bus = pci_alloc_bus(bus)))
  445. {
  446. LOG_E("Alloc bus(%02x) fail", bus_no);
  447. goto _end;
  448. }
  449. if (pci_child_bus_init(next_bus, bus_no, host_bridge, pdev))
  450. {
  451. goto _end;
  452. }
  453. /* Fill primary, secondary */
  454. value = (buses & 0xff000000) | (bus->number << 0) | (next_bus->number << 8);
  455. rt_pci_write_config_u32(pdev, PCIR_PRIBUS_1, value);
  456. bus_no = rt_pci_scan_child_buses(next_bus, buses);
  457. /* Fill subordinate */
  458. value |= next_bus->number + rt_list_len(&next_bus->children_nodes);
  459. rt_pci_write_config_u32(pdev, PCIR_PRIBUS_1, value);
  460. if (fixed_buses)
  461. {
  462. bus_no = fixed_sub;
  463. }
  464. rt_pci_write_config_u8(pdev, PCIR_SUBBUS_1, bus_no);
  465. _end:
  466. return bus_no;
  467. }
  468. rt_uint32_t rt_pci_scan_bridge(struct rt_pci_bus *bus, struct rt_pci_device *pdev,
  469. rt_uint32_t bus_no_start, rt_bool_t reconfigured)
  470. {
  471. if (!bus || !pdev)
  472. {
  473. return RT_UINT32_MAX;
  474. }
  475. return pci_scan_bridge_extend(bus, pdev, bus_no_start, 0, reconfigured);
  476. }
  477. rt_inline rt_bool_t only_one_child(struct rt_pci_bus *bus)
  478. {
  479. struct rt_pci_device *pdev;
  480. if (rt_pci_is_root_bus(bus))
  481. {
  482. return RT_FALSE;
  483. }
  484. pdev = bus->self;
  485. if (rt_pci_is_pcie(pdev))
  486. {
  487. rt_uint16_t exp_type = pdev->exp_flags & PCIEM_FLAGS_TYPE;
  488. if (exp_type == PCIEM_TYPE_ROOT_PORT ||
  489. exp_type == PCIEM_TYPE_DOWNSTREAM_PORT ||
  490. exp_type == PCIEM_TYPE_PCIE_BRIDGE)
  491. {
  492. return RT_TRUE;
  493. }
  494. }
  495. return RT_FALSE;
  496. }
  497. static int next_fn(struct rt_pci_bus *bus, struct rt_pci_device *pdev, int fn)
  498. {
  499. if (!rt_pci_is_root_bus(bus) && bus->self->ari_enabled)
  500. {
  501. int pos, next_fn;
  502. rt_uint16_t cap = 0;
  503. if (!pdev)
  504. {
  505. return -RT_EINVAL;
  506. }
  507. pos = rt_pci_find_ext_capability(pdev, PCIZ_ARI);
  508. if (!pos)
  509. {
  510. return -RT_EINVAL;
  511. }
  512. rt_pci_read_config_u16(pdev, pos + PCIR_ARI_CAP, &cap);
  513. next_fn = PCIM_ARI_CAP_NFN(cap);
  514. if (next_fn <= fn)
  515. {
  516. return -RT_EINVAL;
  517. }
  518. return next_fn;
  519. }
  520. if (fn >= RT_PCI_FUNCTION_MAX - 1)
  521. {
  522. return -RT_EINVAL;
  523. }
  524. if (pdev && !pdev->multi_function)
  525. {
  526. return -RT_EINVAL;
  527. }
  528. return fn + 1;
  529. }
  530. rt_size_t rt_pci_scan_slot(struct rt_pci_bus *bus, rt_uint32_t devfn)
  531. {
  532. rt_size_t nr = 0;
  533. struct rt_pci_device *pdev = RT_NULL;
  534. if (!bus)
  535. {
  536. return nr;
  537. }
  538. if (devfn > 0 && only_one_child(bus))
  539. {
  540. return nr;
  541. }
  542. for (int func = 0; func >= 0; func = next_fn(bus, pdev, func))
  543. {
  544. pdev = rt_pci_scan_single_device(bus, devfn + func);
  545. if (pdev)
  546. {
  547. ++nr;
  548. if (func > 0)
  549. {
  550. pdev->multi_function = RT_TRUE;
  551. }
  552. }
  553. else if (func == 0)
  554. {
  555. break;
  556. }
  557. }
  558. return nr;
  559. }
  560. rt_uint32_t rt_pci_scan_child_buses(struct rt_pci_bus *bus, rt_size_t buses)
  561. {
  562. rt_uint32_t bus_no;
  563. struct rt_pci_device *pdev = RT_NULL;
  564. if (!bus)
  565. {
  566. bus_no = RT_UINT32_MAX;
  567. goto _end;
  568. }
  569. bus_no = bus->number;
  570. for (rt_uint32_t devfn = 0;
  571. devfn < RT_PCI_DEVFN(RT_PCI_DEVICE_MAX - 1, RT_PCI_FUNCTION_MAX - 1);
  572. devfn += RT_PCI_FUNCTION_MAX)
  573. {
  574. rt_pci_scan_slot(bus, devfn);
  575. }
  576. rt_pci_foreach_bridge(pdev, bus)
  577. {
  578. int offset;
  579. bus_no = pci_scan_bridge_extend(bus, pdev, bus_no, buses, RT_TRUE);
  580. offset = bus_no - bus->number;
  581. if (buses > offset)
  582. {
  583. buses -= offset;
  584. }
  585. else
  586. {
  587. break;
  588. }
  589. }
  590. _end:
  591. return bus_no;
  592. }
  593. rt_uint32_t rt_pci_scan_child_bus(struct rt_pci_bus *bus)
  594. {
  595. return rt_pci_scan_child_buses(bus, 0);
  596. }
  597. static struct rt_pci_bus *pci_alloc_bus(struct rt_pci_bus *parent)
  598. {
  599. struct rt_pci_bus *bus = rt_calloc(1, sizeof(*bus));
  600. if (!bus)
  601. {
  602. return RT_NULL;
  603. }
  604. bus->parent = parent;
  605. rt_list_init(&bus->list);
  606. rt_list_init(&bus->children_nodes);
  607. rt_list_init(&bus->devices_nodes);
  608. rt_spin_lock_init(&bus->lock);
  609. return bus;
  610. }
  611. rt_err_t rt_pci_host_bridge_register(struct rt_pci_host_bridge *host_bridge)
  612. {
  613. struct rt_pci_bus *bus = pci_alloc_bus(RT_NULL);
  614. if (!bus)
  615. {
  616. return -RT_ENOMEM;
  617. }
  618. host_bridge->root_bus = bus;
  619. bus->sysdata = host_bridge->sysdata;
  620. bus->host_bridge = host_bridge;
  621. bus->ops = host_bridge->ops;
  622. bus->number = host_bridge->bus_range[0];
  623. rt_sprintf(bus->name, "%04x:%02x", host_bridge->domain, bus->number);
  624. if (bus->ops->add)
  625. {
  626. rt_err_t err = bus->ops->add(bus);
  627. if (err)
  628. {
  629. LOG_E("PCI-Bus<%s> add bus failed with err = %s", bus->name, rt_strerror(err));
  630. }
  631. }
  632. return RT_EOK;
  633. }
  634. rt_err_t rt_pci_scan_root_bus_bridge(struct rt_pci_host_bridge *host_bridge)
  635. {
  636. rt_err_t err;
  637. if ((err = rt_pci_host_bridge_register(host_bridge)))
  638. {
  639. return err;
  640. }
  641. rt_pci_scan_child_bus(host_bridge->root_bus);
  642. return err;
  643. }
  644. rt_err_t rt_pci_host_bridge_probe(struct rt_pci_host_bridge *host_bridge)
  645. {
  646. rt_err_t err;
  647. err = rt_pci_scan_root_bus_bridge(host_bridge);
  648. return err;
  649. }
  650. static rt_bool_t pci_remove_bus_device(struct rt_pci_device *pdev, void *data)
  651. {
  652. /* Bus will free if this is the last device */
  653. rt_bus_remove_device(&pdev->parent);
  654. /* To find all devices, always return false */
  655. return RT_FALSE;
  656. }
  657. rt_err_t rt_pci_host_bridge_remove(struct rt_pci_host_bridge *host_bridge)
  658. {
  659. rt_err_t err = RT_EOK;
  660. if (host_bridge && host_bridge->root_bus)
  661. {
  662. rt_pci_enum_device(host_bridge->root_bus, pci_remove_bus_device, RT_NULL);
  663. host_bridge->root_bus = RT_NULL;
  664. }
  665. else
  666. {
  667. err = -RT_EINVAL;
  668. }
  669. return err;
  670. }
  671. rt_err_t rt_pci_bus_remove(struct rt_pci_bus *bus)
  672. {
  673. rt_err_t err = RT_EOK;
  674. if (bus)
  675. {
  676. spin_lock(&bus->lock);
  677. if (rt_list_isempty(&bus->children_nodes) &&
  678. rt_list_isempty(&bus->devices_nodes))
  679. {
  680. rt_list_remove(&bus->list);
  681. spin_unlock(&bus->lock);
  682. if (bus->ops->remove)
  683. {
  684. bus->ops->remove(bus);
  685. }
  686. rt_pci_ofw_bus_free(bus);
  687. rt_free(bus);
  688. }
  689. else
  690. {
  691. spin_unlock(&bus->lock);
  692. err = -RT_EBUSY;
  693. }
  694. }
  695. else
  696. {
  697. err = -RT_EINVAL;
  698. }
  699. return err;
  700. }
  701. rt_err_t rt_pci_device_remove(struct rt_pci_device *pdev)
  702. {
  703. rt_err_t err = RT_EOK;
  704. if (pdev)
  705. {
  706. struct rt_pci_bus *bus = pdev->bus;
  707. pci_procfs_detach(pdev);
  708. spin_lock(&bus->lock);
  709. while (pdev->parent.ref_count > 1)
  710. {
  711. spin_unlock(&bus->lock);
  712. rt_thread_yield();
  713. spin_lock(&bus->lock);
  714. }
  715. rt_list_remove(&pdev->list);
  716. spin_unlock(&bus->lock);
  717. rt_free(pdev);
  718. }
  719. else
  720. {
  721. err = -RT_EINVAL;
  722. }
  723. return err;
  724. }