pci-uclass.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Copyright (c) 2014 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <inttypes.h>
  12. #include <pci.h>
  13. #include <dm/lists.h>
  14. #include <dm/root.h>
  15. #include <dm/device-internal.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. struct pci_controller *pci_bus_to_hose(int busnum)
  18. {
  19. struct udevice *bus;
  20. int ret;
  21. ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
  22. if (ret) {
  23. debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
  24. return NULL;
  25. }
  26. return dev_get_uclass_priv(bus);
  27. }
  28. /**
  29. * pci_get_bus_max() - returns the bus number of the last active bus
  30. *
  31. * @return last bus number, or -1 if no active buses
  32. */
  33. static int pci_get_bus_max(void)
  34. {
  35. struct udevice *bus;
  36. struct uclass *uc;
  37. int ret = -1;
  38. ret = uclass_get(UCLASS_PCI, &uc);
  39. uclass_foreach_dev(bus, uc) {
  40. if (bus->seq > ret)
  41. ret = bus->seq;
  42. }
  43. debug("%s: ret=%d\n", __func__, ret);
  44. return ret;
  45. }
  46. int pci_last_busno(void)
  47. {
  48. struct pci_controller *hose;
  49. struct udevice *bus;
  50. struct uclass *uc;
  51. int ret;
  52. debug("pci_last_busno\n");
  53. ret = uclass_get(UCLASS_PCI, &uc);
  54. if (ret || list_empty(&uc->dev_head))
  55. return -1;
  56. /* Probe the last bus */
  57. bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
  58. debug("bus = %p, %s\n", bus, bus->name);
  59. assert(bus);
  60. ret = device_probe(bus);
  61. if (ret)
  62. return ret;
  63. /* If that bus has bridges, we may have new buses now. Get the last */
  64. bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
  65. hose = dev_get_uclass_priv(bus);
  66. debug("bus = %s, hose = %p\n", bus->name, hose);
  67. return hose->last_busno;
  68. }
  69. int pci_get_ff(enum pci_size_t size)
  70. {
  71. switch (size) {
  72. case PCI_SIZE_8:
  73. return 0xff;
  74. case PCI_SIZE_16:
  75. return 0xffff;
  76. default:
  77. return 0xffffffff;
  78. }
  79. }
  80. int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
  81. struct udevice **devp)
  82. {
  83. struct udevice *dev;
  84. for (device_find_first_child(bus, &dev);
  85. dev;
  86. device_find_next_child(&dev)) {
  87. struct pci_child_platdata *pplat;
  88. pplat = dev_get_parent_platdata(dev);
  89. if (pplat && pplat->devfn == find_devfn) {
  90. *devp = dev;
  91. return 0;
  92. }
  93. }
  94. return -ENODEV;
  95. }
  96. int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
  97. {
  98. struct udevice *bus;
  99. int ret;
  100. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  101. if (ret)
  102. return ret;
  103. return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
  104. }
  105. static int pci_device_matches_ids(struct udevice *dev,
  106. struct pci_device_id *ids)
  107. {
  108. struct pci_child_platdata *pplat;
  109. int i;
  110. pplat = dev_get_parent_platdata(dev);
  111. if (!pplat)
  112. return -EINVAL;
  113. for (i = 0; ids[i].vendor != 0; i++) {
  114. if (pplat->vendor == ids[i].vendor &&
  115. pplat->device == ids[i].device)
  116. return i;
  117. }
  118. return -EINVAL;
  119. }
  120. int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
  121. int *indexp, struct udevice **devp)
  122. {
  123. struct udevice *dev;
  124. /* Scan all devices on this bus */
  125. for (device_find_first_child(bus, &dev);
  126. dev;
  127. device_find_next_child(&dev)) {
  128. if (pci_device_matches_ids(dev, ids) >= 0) {
  129. if ((*indexp)-- <= 0) {
  130. *devp = dev;
  131. return 0;
  132. }
  133. }
  134. }
  135. return -ENODEV;
  136. }
  137. int pci_find_device_id(struct pci_device_id *ids, int index,
  138. struct udevice **devp)
  139. {
  140. struct udevice *bus;
  141. /* Scan all known buses */
  142. for (uclass_first_device(UCLASS_PCI, &bus);
  143. bus;
  144. uclass_next_device(&bus)) {
  145. if (!pci_bus_find_devices(bus, ids, &index, devp))
  146. return 0;
  147. }
  148. *devp = NULL;
  149. return -ENODEV;
  150. }
  151. int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
  152. unsigned long value, enum pci_size_t size)
  153. {
  154. struct dm_pci_ops *ops;
  155. ops = pci_get_ops(bus);
  156. if (!ops->write_config)
  157. return -ENOSYS;
  158. return ops->write_config(bus, bdf, offset, value, size);
  159. }
  160. int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
  161. enum pci_size_t size)
  162. {
  163. struct udevice *bus;
  164. int ret;
  165. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  166. if (ret)
  167. return ret;
  168. return pci_bus_write_config(bus, PCI_MASK_BUS(bdf), offset, value,
  169. size);
  170. }
  171. int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
  172. {
  173. return pci_write_config(bdf, offset, value, PCI_SIZE_32);
  174. }
  175. int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
  176. {
  177. return pci_write_config(bdf, offset, value, PCI_SIZE_16);
  178. }
  179. int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
  180. {
  181. return pci_write_config(bdf, offset, value, PCI_SIZE_8);
  182. }
  183. int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
  184. unsigned long *valuep, enum pci_size_t size)
  185. {
  186. struct dm_pci_ops *ops;
  187. ops = pci_get_ops(bus);
  188. if (!ops->read_config)
  189. return -ENOSYS;
  190. return ops->read_config(bus, bdf, offset, valuep, size);
  191. }
  192. int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
  193. enum pci_size_t size)
  194. {
  195. struct udevice *bus;
  196. int ret;
  197. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  198. if (ret)
  199. return ret;
  200. return pci_bus_read_config(bus, PCI_MASK_BUS(bdf), offset, valuep,
  201. size);
  202. }
  203. int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
  204. {
  205. unsigned long value;
  206. int ret;
  207. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
  208. if (ret)
  209. return ret;
  210. *valuep = value;
  211. return 0;
  212. }
  213. int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
  214. {
  215. unsigned long value;
  216. int ret;
  217. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
  218. if (ret)
  219. return ret;
  220. *valuep = value;
  221. return 0;
  222. }
  223. int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
  224. {
  225. unsigned long value;
  226. int ret;
  227. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
  228. if (ret)
  229. return ret;
  230. *valuep = value;
  231. return 0;
  232. }
  233. int pci_auto_config_devices(struct udevice *bus)
  234. {
  235. struct pci_controller *hose = bus->uclass_priv;
  236. unsigned int sub_bus;
  237. struct udevice *dev;
  238. int ret;
  239. sub_bus = bus->seq;
  240. debug("%s: start\n", __func__);
  241. pciauto_config_init(hose);
  242. for (ret = device_find_first_child(bus, &dev);
  243. !ret && dev;
  244. ret = device_find_next_child(&dev)) {
  245. struct pci_child_platdata *pplat;
  246. pplat = dev_get_parent_platdata(dev);
  247. unsigned int max_bus;
  248. pci_dev_t bdf;
  249. bdf = PCI_ADD_BUS(bus->seq, pplat->devfn);
  250. debug("%s: device %s\n", __func__, dev->name);
  251. max_bus = pciauto_config_device(hose, bdf);
  252. sub_bus = max(sub_bus, max_bus);
  253. }
  254. debug("%s: done\n", __func__);
  255. return sub_bus;
  256. }
  257. int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
  258. {
  259. struct udevice *parent, *bus;
  260. int sub_bus;
  261. int ret;
  262. debug("%s\n", __func__);
  263. parent = hose->bus;
  264. /* Find the bus within the parent */
  265. ret = pci_bus_find_devfn(parent, bdf, &bus);
  266. if (ret) {
  267. debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
  268. bdf, parent->name, ret);
  269. return ret;
  270. }
  271. sub_bus = pci_get_bus_max() + 1;
  272. debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
  273. pciauto_prescan_setup_bridge(hose, bdf, bus->seq);
  274. ret = device_probe(bus);
  275. if (ret) {
  276. debug("%s: Cannot probe bus bus %s: %d\n", __func__, bus->name,
  277. ret);
  278. return ret;
  279. }
  280. if (sub_bus != bus->seq) {
  281. printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
  282. __func__, bus->name, bus->seq, sub_bus);
  283. return -EPIPE;
  284. }
  285. sub_bus = pci_get_bus_max();
  286. pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
  287. return sub_bus;
  288. }
  289. int pci_bind_bus_devices(struct udevice *bus)
  290. {
  291. ulong vendor, device;
  292. ulong header_type;
  293. pci_dev_t devfn, end;
  294. bool found_multi;
  295. int ret;
  296. found_multi = false;
  297. end = PCI_DEVFN(PCI_MAX_PCI_DEVICES - 1, PCI_MAX_PCI_FUNCTIONS - 1);
  298. for (devfn = PCI_DEVFN(0, 0); devfn < end; devfn += PCI_DEVFN(0, 1)) {
  299. struct pci_child_platdata *pplat;
  300. struct udevice *dev;
  301. ulong class;
  302. if (PCI_FUNC(devfn) && !found_multi)
  303. continue;
  304. /* Check only the first access, we don't expect problems */
  305. ret = pci_bus_read_config(bus, devfn, PCI_HEADER_TYPE,
  306. &header_type, PCI_SIZE_8);
  307. if (ret)
  308. goto error;
  309. pci_bus_read_config(bus, devfn, PCI_VENDOR_ID, &vendor,
  310. PCI_SIZE_16);
  311. if (vendor == 0xffff || vendor == 0x0000)
  312. continue;
  313. if (!PCI_FUNC(devfn))
  314. found_multi = header_type & 0x80;
  315. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  316. bus->seq, bus->name, PCI_DEV(devfn), PCI_FUNC(devfn));
  317. pci_bus_read_config(bus, devfn, PCI_DEVICE_ID, &device,
  318. PCI_SIZE_16);
  319. pci_bus_read_config(bus, devfn, PCI_CLASS_DEVICE, &class,
  320. PCI_SIZE_16);
  321. /* Find this device in the device tree */
  322. ret = pci_bus_find_devfn(bus, devfn, &dev);
  323. /* If nothing in the device tree, bind a generic device */
  324. if (ret == -ENODEV) {
  325. char name[30], *str;
  326. const char *drv;
  327. sprintf(name, "pci_%x:%x.%x", bus->seq,
  328. PCI_DEV(devfn), PCI_FUNC(devfn));
  329. str = strdup(name);
  330. if (!str)
  331. return -ENOMEM;
  332. drv = class == PCI_CLASS_BRIDGE_PCI ?
  333. "pci_bridge_drv" : "pci_generic_drv";
  334. ret = device_bind_driver(bus, drv, str, &dev);
  335. }
  336. if (ret)
  337. return ret;
  338. /* Update the platform data */
  339. pplat = dev_get_parent_platdata(dev);
  340. pplat->devfn = devfn;
  341. pplat->vendor = vendor;
  342. pplat->device = device;
  343. pplat->class = class;
  344. }
  345. return 0;
  346. error:
  347. printf("Cannot read bus configuration: %d\n", ret);
  348. return ret;
  349. }
  350. static int pci_uclass_post_bind(struct udevice *bus)
  351. {
  352. /*
  353. * Scan the device tree for devices. This does not probe the PCI bus,
  354. * as this is not permitted while binding. It just finds devices
  355. * mentioned in the device tree.
  356. *
  357. * Before relocation, only bind devices marked for pre-relocation
  358. * use.
  359. */
  360. return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
  361. gd->flags & GD_FLG_RELOC ? false : true);
  362. }
  363. static int decode_regions(struct pci_controller *hose, const void *blob,
  364. int parent_node, int node)
  365. {
  366. int pci_addr_cells, addr_cells, size_cells;
  367. int cells_per_record;
  368. const u32 *prop;
  369. int len;
  370. int i;
  371. prop = fdt_getprop(blob, node, "ranges", &len);
  372. if (!prop)
  373. return -EINVAL;
  374. pci_addr_cells = fdt_address_cells(blob, node);
  375. addr_cells = fdt_address_cells(blob, parent_node);
  376. size_cells = fdt_size_cells(blob, node);
  377. /* PCI addresses are always 3-cells */
  378. len /= sizeof(u32);
  379. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  380. hose->region_count = 0;
  381. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  382. cells_per_record);
  383. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  384. u64 pci_addr, addr, size;
  385. int space_code;
  386. u32 flags;
  387. int type;
  388. if (len < cells_per_record)
  389. break;
  390. flags = fdt32_to_cpu(prop[0]);
  391. space_code = (flags >> 24) & 3;
  392. pci_addr = fdtdec_get_number(prop + 1, 2);
  393. prop += pci_addr_cells;
  394. addr = fdtdec_get_number(prop, addr_cells);
  395. prop += addr_cells;
  396. size = fdtdec_get_number(prop, size_cells);
  397. prop += size_cells;
  398. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  399. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  400. hose->region_count, pci_addr, addr, size, space_code);
  401. if (space_code & 2) {
  402. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  403. PCI_REGION_MEM;
  404. } else if (space_code & 1) {
  405. type = PCI_REGION_IO;
  406. } else {
  407. continue;
  408. }
  409. debug(" - type=%d\n", type);
  410. pci_set_region(hose->regions + hose->region_count++, pci_addr,
  411. addr, size, type);
  412. }
  413. /* Add a region for our local memory */
  414. pci_set_region(hose->regions + hose->region_count++, 0, 0,
  415. gd->ram_size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  416. return 0;
  417. }
  418. static int pci_uclass_pre_probe(struct udevice *bus)
  419. {
  420. struct pci_controller *hose;
  421. int ret;
  422. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  423. bus->parent->name);
  424. hose = bus->uclass_priv;
  425. /* For bridges, use the top-level PCI controller */
  426. if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
  427. hose->ctlr = bus;
  428. ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
  429. bus->of_offset);
  430. if (ret) {
  431. debug("%s: Cannot decode regions\n", __func__);
  432. return ret;
  433. }
  434. } else {
  435. struct pci_controller *parent_hose;
  436. parent_hose = dev_get_uclass_priv(bus->parent);
  437. hose->ctlr = parent_hose->bus;
  438. }
  439. hose->bus = bus;
  440. hose->first_busno = bus->seq;
  441. hose->last_busno = bus->seq;
  442. return 0;
  443. }
  444. static int pci_uclass_post_probe(struct udevice *bus)
  445. {
  446. int ret;
  447. /* Don't scan buses before relocation */
  448. if (!(gd->flags & GD_FLG_RELOC))
  449. return 0;
  450. debug("%s: probing bus %d\n", __func__, bus->seq);
  451. ret = pci_bind_bus_devices(bus);
  452. if (ret)
  453. return ret;
  454. #ifdef CONFIG_PCI_PNP
  455. ret = pci_auto_config_devices(bus);
  456. #endif
  457. return ret < 0 ? ret : 0;
  458. }
  459. static int pci_uclass_child_post_bind(struct udevice *dev)
  460. {
  461. struct pci_child_platdata *pplat;
  462. struct fdt_pci_addr addr;
  463. int ret;
  464. if (dev->of_offset == -1)
  465. return 0;
  466. /*
  467. * We could read vendor, device, class if available. But for now we
  468. * just check the address.
  469. */
  470. pplat = dev_get_parent_platdata(dev);
  471. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  472. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  473. if (ret) {
  474. if (ret != -ENOENT)
  475. return -EINVAL;
  476. } else {
  477. /* extract the bdf from fdt_pci_addr */
  478. pplat->devfn = addr.phys_hi & 0xffff00;
  479. }
  480. return 0;
  481. }
  482. int pci_bridge_read_config(struct udevice *bus, pci_dev_t devfn, uint offset,
  483. ulong *valuep, enum pci_size_t size)
  484. {
  485. struct pci_controller *hose = bus->uclass_priv;
  486. pci_dev_t bdf = PCI_ADD_BUS(bus->seq, devfn);
  487. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  488. }
  489. int pci_bridge_write_config(struct udevice *bus, pci_dev_t devfn, uint offset,
  490. ulong value, enum pci_size_t size)
  491. {
  492. struct pci_controller *hose = bus->uclass_priv;
  493. pci_dev_t bdf = PCI_ADD_BUS(bus->seq, devfn);
  494. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  495. }
  496. UCLASS_DRIVER(pci) = {
  497. .id = UCLASS_PCI,
  498. .name = "pci",
  499. .flags = DM_UC_FLAG_SEQ_ALIAS,
  500. .post_bind = pci_uclass_post_bind,
  501. .pre_probe = pci_uclass_pre_probe,
  502. .post_probe = pci_uclass_post_probe,
  503. .child_post_bind = pci_uclass_child_post_bind,
  504. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  505. .per_child_platdata_auto_alloc_size =
  506. sizeof(struct pci_child_platdata),
  507. };
  508. static const struct dm_pci_ops pci_bridge_ops = {
  509. .read_config = pci_bridge_read_config,
  510. .write_config = pci_bridge_write_config,
  511. };
  512. static const struct udevice_id pci_bridge_ids[] = {
  513. { .compatible = "pci-bridge" },
  514. { }
  515. };
  516. U_BOOT_DRIVER(pci_bridge_drv) = {
  517. .name = "pci_bridge_drv",
  518. .id = UCLASS_PCI,
  519. .of_match = pci_bridge_ids,
  520. .ops = &pci_bridge_ops,
  521. };
  522. UCLASS_DRIVER(pci_generic) = {
  523. .id = UCLASS_PCI_GENERIC,
  524. .name = "pci_generic",
  525. };
  526. static const struct udevice_id pci_generic_ids[] = {
  527. { .compatible = "pci-generic" },
  528. { }
  529. };
  530. U_BOOT_DRIVER(pci_generic_drv) = {
  531. .name = "pci_generic_drv",
  532. .id = UCLASS_PCI_GENERIC,
  533. .of_match = pci_generic_ids,
  534. };