pci-uclass.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. struct pci_controller *ctlr_hose;
  247. pplat = dev_get_parent_platdata(dev);
  248. unsigned int max_bus;
  249. pci_dev_t bdf;
  250. bdf = PCI_ADD_BUS(bus->seq, pplat->devfn);
  251. debug("%s: device %s\n", __func__, dev->name);
  252. /* The root controller has the region information */
  253. ctlr_hose = hose->ctlr->uclass_priv;
  254. max_bus = pciauto_config_device(ctlr_hose, bdf);
  255. sub_bus = max(sub_bus, max_bus);
  256. }
  257. debug("%s: done\n", __func__);
  258. return sub_bus;
  259. }
  260. int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
  261. {
  262. struct udevice *parent, *bus;
  263. int sub_bus;
  264. int ret;
  265. debug("%s\n", __func__);
  266. parent = hose->bus;
  267. /* Find the bus within the parent */
  268. ret = pci_bus_find_devfn(parent, bdf, &bus);
  269. if (ret) {
  270. debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
  271. bdf, parent->name, ret);
  272. return ret;
  273. }
  274. sub_bus = pci_get_bus_max() + 1;
  275. debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
  276. pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
  277. ret = device_probe(bus);
  278. if (ret) {
  279. debug("%s: Cannot probe bus bus %s: %d\n", __func__, bus->name,
  280. ret);
  281. return ret;
  282. }
  283. if (sub_bus != bus->seq) {
  284. printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
  285. __func__, bus->name, bus->seq, sub_bus);
  286. return -EPIPE;
  287. }
  288. sub_bus = pci_get_bus_max();
  289. pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
  290. return sub_bus;
  291. }
  292. int pci_bind_bus_devices(struct udevice *bus)
  293. {
  294. ulong vendor, device;
  295. ulong header_type;
  296. pci_dev_t devfn, end;
  297. bool found_multi;
  298. int ret;
  299. found_multi = false;
  300. end = PCI_DEVFN(PCI_MAX_PCI_DEVICES - 1, PCI_MAX_PCI_FUNCTIONS - 1);
  301. for (devfn = PCI_DEVFN(0, 0); devfn < end; devfn += PCI_DEVFN(0, 1)) {
  302. struct pci_child_platdata *pplat;
  303. struct udevice *dev;
  304. ulong class;
  305. if (PCI_FUNC(devfn) && !found_multi)
  306. continue;
  307. /* Check only the first access, we don't expect problems */
  308. ret = pci_bus_read_config(bus, devfn, PCI_HEADER_TYPE,
  309. &header_type, PCI_SIZE_8);
  310. if (ret)
  311. goto error;
  312. pci_bus_read_config(bus, devfn, PCI_VENDOR_ID, &vendor,
  313. PCI_SIZE_16);
  314. if (vendor == 0xffff || vendor == 0x0000)
  315. continue;
  316. if (!PCI_FUNC(devfn))
  317. found_multi = header_type & 0x80;
  318. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  319. bus->seq, bus->name, PCI_DEV(devfn), PCI_FUNC(devfn));
  320. pci_bus_read_config(bus, devfn, PCI_DEVICE_ID, &device,
  321. PCI_SIZE_16);
  322. pci_bus_read_config(bus, devfn, PCI_CLASS_DEVICE, &class,
  323. PCI_SIZE_16);
  324. /* Find this device in the device tree */
  325. ret = pci_bus_find_devfn(bus, devfn, &dev);
  326. /* If nothing in the device tree, bind a generic device */
  327. if (ret == -ENODEV) {
  328. char name[30], *str;
  329. const char *drv;
  330. sprintf(name, "pci_%x:%x.%x", bus->seq,
  331. PCI_DEV(devfn), PCI_FUNC(devfn));
  332. str = strdup(name);
  333. if (!str)
  334. return -ENOMEM;
  335. drv = class == PCI_CLASS_BRIDGE_PCI ?
  336. "pci_bridge_drv" : "pci_generic_drv";
  337. ret = device_bind_driver(bus, drv, str, &dev);
  338. }
  339. if (ret)
  340. return ret;
  341. /* Update the platform data */
  342. pplat = dev_get_parent_platdata(dev);
  343. pplat->devfn = devfn;
  344. pplat->vendor = vendor;
  345. pplat->device = device;
  346. pplat->class = class;
  347. }
  348. return 0;
  349. error:
  350. printf("Cannot read bus configuration: %d\n", ret);
  351. return ret;
  352. }
  353. static int pci_uclass_post_bind(struct udevice *bus)
  354. {
  355. /*
  356. * Scan the device tree for devices. This does not probe the PCI bus,
  357. * as this is not permitted while binding. It just finds devices
  358. * mentioned in the device tree.
  359. *
  360. * Before relocation, only bind devices marked for pre-relocation
  361. * use.
  362. */
  363. return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
  364. gd->flags & GD_FLG_RELOC ? false : true);
  365. }
  366. static int decode_regions(struct pci_controller *hose, const void *blob,
  367. int parent_node, int node)
  368. {
  369. int pci_addr_cells, addr_cells, size_cells;
  370. int cells_per_record;
  371. phys_addr_t addr;
  372. const u32 *prop;
  373. int len;
  374. int i;
  375. prop = fdt_getprop(blob, node, "ranges", &len);
  376. if (!prop)
  377. return -EINVAL;
  378. pci_addr_cells = fdt_address_cells(blob, node);
  379. addr_cells = fdt_address_cells(blob, parent_node);
  380. size_cells = fdt_size_cells(blob, node);
  381. /* PCI addresses are always 3-cells */
  382. len /= sizeof(u32);
  383. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  384. hose->region_count = 0;
  385. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  386. cells_per_record);
  387. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  388. u64 pci_addr, addr, size;
  389. int space_code;
  390. u32 flags;
  391. int type;
  392. if (len < cells_per_record)
  393. break;
  394. flags = fdt32_to_cpu(prop[0]);
  395. space_code = (flags >> 24) & 3;
  396. pci_addr = fdtdec_get_number(prop + 1, 2);
  397. prop += pci_addr_cells;
  398. addr = fdtdec_get_number(prop, addr_cells);
  399. prop += addr_cells;
  400. size = fdtdec_get_number(prop, size_cells);
  401. prop += size_cells;
  402. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  403. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  404. hose->region_count, pci_addr, addr, size, space_code);
  405. if (space_code & 2) {
  406. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  407. PCI_REGION_MEM;
  408. } else if (space_code & 1) {
  409. type = PCI_REGION_IO;
  410. } else {
  411. continue;
  412. }
  413. debug(" - type=%d\n", type);
  414. pci_set_region(hose->regions + hose->region_count++, pci_addr,
  415. addr, size, type);
  416. }
  417. /* Add a region for our local memory */
  418. addr = gd->ram_size;
  419. if (gd->pci_ram_top && gd->pci_ram_top < addr)
  420. addr = gd->pci_ram_top;
  421. pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
  422. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  423. return 0;
  424. }
  425. static int pci_uclass_pre_probe(struct udevice *bus)
  426. {
  427. struct pci_controller *hose;
  428. int ret;
  429. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  430. bus->parent->name);
  431. hose = bus->uclass_priv;
  432. /* For bridges, use the top-level PCI controller */
  433. if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
  434. hose->ctlr = bus;
  435. ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
  436. bus->of_offset);
  437. if (ret) {
  438. debug("%s: Cannot decode regions\n", __func__);
  439. return ret;
  440. }
  441. } else {
  442. struct pci_controller *parent_hose;
  443. parent_hose = dev_get_uclass_priv(bus->parent);
  444. hose->ctlr = parent_hose->bus;
  445. }
  446. hose->bus = bus;
  447. hose->first_busno = bus->seq;
  448. hose->last_busno = bus->seq;
  449. return 0;
  450. }
  451. static int pci_uclass_post_probe(struct udevice *bus)
  452. {
  453. int ret;
  454. /* Don't scan buses before relocation */
  455. if (!(gd->flags & GD_FLG_RELOC))
  456. return 0;
  457. debug("%s: probing bus %d\n", __func__, bus->seq);
  458. ret = pci_bind_bus_devices(bus);
  459. if (ret)
  460. return ret;
  461. #ifdef CONFIG_PCI_PNP
  462. ret = pci_auto_config_devices(bus);
  463. #endif
  464. return ret < 0 ? ret : 0;
  465. }
  466. static int pci_uclass_child_post_bind(struct udevice *dev)
  467. {
  468. struct pci_child_platdata *pplat;
  469. struct fdt_pci_addr addr;
  470. int ret;
  471. if (dev->of_offset == -1)
  472. return 0;
  473. /*
  474. * We could read vendor, device, class if available. But for now we
  475. * just check the address.
  476. */
  477. pplat = dev_get_parent_platdata(dev);
  478. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  479. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  480. if (ret) {
  481. if (ret != -ENOENT)
  482. return -EINVAL;
  483. } else {
  484. /* extract the bdf from fdt_pci_addr */
  485. pplat->devfn = addr.phys_hi & 0xffff00;
  486. }
  487. return 0;
  488. }
  489. int pci_bridge_read_config(struct udevice *bus, pci_dev_t devfn, uint offset,
  490. ulong *valuep, 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_read_config(hose->ctlr, bdf, offset, valuep, size);
  495. }
  496. int pci_bridge_write_config(struct udevice *bus, pci_dev_t devfn, uint offset,
  497. ulong value, enum pci_size_t size)
  498. {
  499. struct pci_controller *hose = bus->uclass_priv;
  500. pci_dev_t bdf = PCI_ADD_BUS(bus->seq, devfn);
  501. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  502. }
  503. UCLASS_DRIVER(pci) = {
  504. .id = UCLASS_PCI,
  505. .name = "pci",
  506. .flags = DM_UC_FLAG_SEQ_ALIAS,
  507. .post_bind = pci_uclass_post_bind,
  508. .pre_probe = pci_uclass_pre_probe,
  509. .post_probe = pci_uclass_post_probe,
  510. .child_post_bind = pci_uclass_child_post_bind,
  511. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  512. .per_child_platdata_auto_alloc_size =
  513. sizeof(struct pci_child_platdata),
  514. };
  515. static const struct dm_pci_ops pci_bridge_ops = {
  516. .read_config = pci_bridge_read_config,
  517. .write_config = pci_bridge_write_config,
  518. };
  519. static const struct udevice_id pci_bridge_ids[] = {
  520. { .compatible = "pci-bridge" },
  521. { }
  522. };
  523. U_BOOT_DRIVER(pci_bridge_drv) = {
  524. .name = "pci_bridge_drv",
  525. .id = UCLASS_PCI,
  526. .of_match = pci_bridge_ids,
  527. .ops = &pci_bridge_ops,
  528. };
  529. UCLASS_DRIVER(pci_generic) = {
  530. .id = UCLASS_PCI_GENERIC,
  531. .name = "pci_generic",
  532. };
  533. static const struct udevice_id pci_generic_ids[] = {
  534. { .compatible = "pci-generic" },
  535. { }
  536. };
  537. U_BOOT_DRIVER(pci_generic_drv) = {
  538. .name = "pci_generic_drv",
  539. .id = UCLASS_PCI_GENERIC,
  540. .of_match = pci_generic_ids,
  541. };