pcie-designware-host.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Synopsys DesignWare PCIe host controller driver
  4. *
  5. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  6. * https://www.samsung.com
  7. *
  8. * Author: Jingoo Han <jg1.han@samsung.com>
  9. */
  10. #include <linux/align.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/irqchip/chained_irq.h>
  13. #include <linux/irqchip/irq-msi-lib.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/msi.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_pci.h>
  18. #include <linux/pci_regs.h>
  19. #include <linux/platform_device.h>
  20. #include "../../pci.h"
  21. #include "pcie-designware.h"
  22. static struct pci_ops dw_pcie_ops;
  23. static struct pci_ops dw_pcie_ecam_ops;
  24. static struct pci_ops dw_child_pcie_ops;
  25. #ifdef CONFIG_SMP
  26. static void dw_irq_noop(struct irq_data *d) { }
  27. #endif
  28. static bool dw_pcie_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
  29. struct irq_domain *real_parent, struct msi_domain_info *info)
  30. {
  31. if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
  32. return false;
  33. #ifdef CONFIG_SMP
  34. info->chip->irq_ack = dw_irq_noop;
  35. info->chip->irq_pre_redirect = irq_chip_pre_redirect_parent;
  36. #else
  37. info->chip->irq_ack = irq_chip_ack_parent;
  38. #endif
  39. return true;
  40. }
  41. #define DW_PCIE_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  42. MSI_FLAG_USE_DEF_CHIP_OPS | \
  43. MSI_FLAG_PCI_MSI_MASK_PARENT)
  44. #define DW_PCIE_MSI_FLAGS_SUPPORTED (MSI_FLAG_MULTI_PCI_MSI | \
  45. MSI_FLAG_PCI_MSIX | \
  46. MSI_GENERIC_FLAGS_MASK)
  47. #define IS_256MB_ALIGNED(x) IS_ALIGNED(x, SZ_256M)
  48. static const struct msi_parent_ops dw_pcie_msi_parent_ops = {
  49. .required_flags = DW_PCIE_MSI_FLAGS_REQUIRED,
  50. .supported_flags = DW_PCIE_MSI_FLAGS_SUPPORTED,
  51. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  52. .prefix = "DW-",
  53. .init_dev_msi_info = dw_pcie_init_dev_msi_info,
  54. };
  55. /* MSI int handler */
  56. void dw_handle_msi_irq(struct dw_pcie_rp *pp)
  57. {
  58. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  59. unsigned int i, num_ctrls;
  60. num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
  61. for (i = 0; i < num_ctrls; i++) {
  62. unsigned int reg_off = i * MSI_REG_CTRL_BLOCK_SIZE;
  63. unsigned int irq_off = i * MAX_MSI_IRQS_PER_CTRL;
  64. unsigned long status, pos;
  65. status = dw_pcie_readl_dbi(pci, PCIE_MSI_INTR0_STATUS + reg_off);
  66. if (!status)
  67. continue;
  68. for_each_set_bit(pos, &status, MAX_MSI_IRQS_PER_CTRL)
  69. generic_handle_demux_domain_irq(pp->irq_domain, irq_off + pos);
  70. }
  71. }
  72. /* Chained MSI interrupt service routine */
  73. static void dw_chained_msi_isr(struct irq_desc *desc)
  74. {
  75. struct irq_chip *chip = irq_desc_get_chip(desc);
  76. struct dw_pcie_rp *pp;
  77. chained_irq_enter(chip, desc);
  78. pp = irq_desc_get_handler_data(desc);
  79. dw_handle_msi_irq(pp);
  80. chained_irq_exit(chip, desc);
  81. }
  82. static void dw_pci_setup_msi_msg(struct irq_data *d, struct msi_msg *msg)
  83. {
  84. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
  85. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  86. u64 msi_target = (u64)pp->msi_data;
  87. msg->address_lo = lower_32_bits(msi_target);
  88. msg->address_hi = upper_32_bits(msi_target);
  89. msg->data = d->hwirq;
  90. dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
  91. (int)d->hwirq, msg->address_hi, msg->address_lo);
  92. }
  93. static void dw_pci_bottom_mask(struct irq_data *d)
  94. {
  95. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
  96. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  97. unsigned int res, bit, ctrl;
  98. guard(raw_spinlock)(&pp->lock);
  99. ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
  100. res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
  101. bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
  102. pp->irq_mask[ctrl] |= BIT(bit);
  103. dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
  104. }
  105. static void dw_pci_bottom_unmask(struct irq_data *d)
  106. {
  107. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
  108. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  109. unsigned int res, bit, ctrl;
  110. guard(raw_spinlock)(&pp->lock);
  111. ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
  112. res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
  113. bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
  114. pp->irq_mask[ctrl] &= ~BIT(bit);
  115. dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
  116. }
  117. static void dw_pci_bottom_ack(struct irq_data *d)
  118. {
  119. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
  120. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  121. unsigned int res, bit, ctrl;
  122. ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
  123. res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
  124. bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
  125. dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_STATUS + res, BIT(bit));
  126. }
  127. static struct irq_chip dw_pci_msi_bottom_irq_chip = {
  128. .name = "DWPCI-MSI",
  129. .irq_compose_msi_msg = dw_pci_setup_msi_msg,
  130. .irq_mask = dw_pci_bottom_mask,
  131. .irq_unmask = dw_pci_bottom_unmask,
  132. #ifdef CONFIG_SMP
  133. .irq_ack = dw_irq_noop,
  134. .irq_pre_redirect = dw_pci_bottom_ack,
  135. .irq_set_affinity = irq_chip_redirect_set_affinity,
  136. #else
  137. .irq_ack = dw_pci_bottom_ack,
  138. #endif
  139. };
  140. static int dw_pcie_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  141. unsigned int nr_irqs, void *args)
  142. {
  143. struct dw_pcie_rp *pp = domain->host_data;
  144. int bit;
  145. scoped_guard (raw_spinlock_irq, &pp->lock) {
  146. bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors,
  147. order_base_2(nr_irqs));
  148. }
  149. if (bit < 0)
  150. return -ENOSPC;
  151. for (unsigned int i = 0; i < nr_irqs; i++) {
  152. irq_domain_set_info(domain, virq + i, bit + i, pp->msi_irq_chip,
  153. pp, handle_edge_irq, NULL, NULL);
  154. }
  155. return 0;
  156. }
  157. static void dw_pcie_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  158. unsigned int nr_irqs)
  159. {
  160. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  161. struct dw_pcie_rp *pp = domain->host_data;
  162. guard(raw_spinlock_irq)(&pp->lock);
  163. bitmap_release_region(pp->msi_irq_in_use, d->hwirq, order_base_2(nr_irqs));
  164. }
  165. static const struct irq_domain_ops dw_pcie_msi_domain_ops = {
  166. .alloc = dw_pcie_irq_domain_alloc,
  167. .free = dw_pcie_irq_domain_free,
  168. };
  169. int dw_pcie_allocate_domains(struct dw_pcie_rp *pp)
  170. {
  171. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  172. struct irq_domain_info info = {
  173. .fwnode = dev_fwnode(pci->dev),
  174. .ops = &dw_pcie_msi_domain_ops,
  175. .size = pp->num_vectors,
  176. .host_data = pp,
  177. };
  178. pp->irq_domain = msi_create_parent_irq_domain(&info, &dw_pcie_msi_parent_ops);
  179. if (!pp->irq_domain) {
  180. dev_err(pci->dev, "Failed to create IRQ domain\n");
  181. return -ENOMEM;
  182. }
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(dw_pcie_allocate_domains);
  186. void dw_pcie_free_msi(struct dw_pcie_rp *pp)
  187. {
  188. u32 ctrl;
  189. for (ctrl = 0; ctrl < MAX_MSI_CTRLS; ctrl++) {
  190. if (pp->msi_irq[ctrl] > 0)
  191. irq_set_chained_handler_and_data(pp->msi_irq[ctrl], NULL, NULL);
  192. }
  193. irq_domain_remove(pp->irq_domain);
  194. }
  195. EXPORT_SYMBOL_GPL(dw_pcie_free_msi);
  196. void dw_pcie_msi_init(struct dw_pcie_rp *pp)
  197. {
  198. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  199. u64 msi_target = (u64)pp->msi_data;
  200. u32 ctrl, num_ctrls;
  201. if (!pci_msi_enabled() || !pp->use_imsi_rx)
  202. return;
  203. num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
  204. /* Initialize IRQ Status array */
  205. for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
  206. dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK +
  207. (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
  208. pp->irq_mask[ctrl]);
  209. dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_ENABLE +
  210. (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
  211. ~0);
  212. }
  213. /* Program the msi_data */
  214. dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_LO, lower_32_bits(msi_target));
  215. dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_HI, upper_32_bits(msi_target));
  216. }
  217. EXPORT_SYMBOL_GPL(dw_pcie_msi_init);
  218. static int dw_pcie_parse_split_msi_irq(struct dw_pcie_rp *pp)
  219. {
  220. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  221. struct device *dev = pci->dev;
  222. struct platform_device *pdev = to_platform_device(dev);
  223. u32 ctrl, max_vectors;
  224. int irq;
  225. /* Parse any "msiX" IRQs described in the devicetree */
  226. for (ctrl = 0; ctrl < MAX_MSI_CTRLS; ctrl++) {
  227. char msi_name[] = "msiX";
  228. msi_name[3] = '0' + ctrl;
  229. irq = platform_get_irq_byname_optional(pdev, msi_name);
  230. if (irq == -ENXIO)
  231. break;
  232. if (irq < 0)
  233. return dev_err_probe(dev, irq,
  234. "Failed to parse MSI IRQ '%s'\n",
  235. msi_name);
  236. pp->msi_irq[ctrl] = irq;
  237. }
  238. /* If no "msiX" IRQs, caller should fallback to "msi" IRQ */
  239. if (ctrl == 0)
  240. return -ENXIO;
  241. max_vectors = ctrl * MAX_MSI_IRQS_PER_CTRL;
  242. if (pp->num_vectors > max_vectors) {
  243. dev_warn(dev, "Exceeding number of MSI vectors, limiting to %u\n",
  244. max_vectors);
  245. pp->num_vectors = max_vectors;
  246. }
  247. if (!pp->num_vectors)
  248. pp->num_vectors = max_vectors;
  249. return 0;
  250. }
  251. int dw_pcie_msi_host_init(struct dw_pcie_rp *pp)
  252. {
  253. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  254. struct device *dev = pci->dev;
  255. struct platform_device *pdev = to_platform_device(dev);
  256. u64 *msi_vaddr = NULL;
  257. int ret;
  258. u32 ctrl, num_ctrls;
  259. for (ctrl = 0; ctrl < MAX_MSI_CTRLS; ctrl++)
  260. pp->irq_mask[ctrl] = ~0;
  261. if (!pp->msi_irq[0]) {
  262. ret = dw_pcie_parse_split_msi_irq(pp);
  263. if (ret < 0 && ret != -ENXIO)
  264. return ret;
  265. }
  266. if (!pp->num_vectors)
  267. pp->num_vectors = MSI_DEF_NUM_VECTORS;
  268. num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
  269. if (!pp->msi_irq[0]) {
  270. pp->msi_irq[0] = platform_get_irq_byname_optional(pdev, "msi");
  271. if (pp->msi_irq[0] < 0) {
  272. pp->msi_irq[0] = platform_get_irq(pdev, 0);
  273. if (pp->msi_irq[0] < 0)
  274. return pp->msi_irq[0];
  275. }
  276. }
  277. dev_dbg(dev, "Using %d MSI vectors\n", pp->num_vectors);
  278. pp->msi_irq_chip = &dw_pci_msi_bottom_irq_chip;
  279. ret = dw_pcie_allocate_domains(pp);
  280. if (ret)
  281. return ret;
  282. for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
  283. if (pp->msi_irq[ctrl] > 0)
  284. irq_set_chained_handler_and_data(pp->msi_irq[ctrl],
  285. dw_chained_msi_isr, pp);
  286. }
  287. /*
  288. * Even though the iMSI-RX Module supports 64-bit addresses some
  289. * peripheral PCIe devices may lack 64-bit message support. In
  290. * order not to miss MSI TLPs from those devices the MSI target
  291. * address has to be within the lowest 4GB.
  292. *
  293. * Per DWC databook r6.21a, section 3.10.2.3, the incoming MWr TLP
  294. * targeting the MSI_CTRL_ADDR is terminated by the iMSI-RX and never
  295. * appears on the AXI bus. So MSI_CTRL_ADDR address doesn't need to be
  296. * mapped and can be any memory that doesn't get allocated for the BAR
  297. * memory. Since most of the platforms provide 32-bit address for
  298. * 'config' region, try cfg0_base as the first option for the MSI target
  299. * address if it's a 32-bit address. Otherwise, try 32-bit and 64-bit
  300. * coherent memory allocation one by one.
  301. */
  302. if (!(pp->cfg0_base & GENMASK_ULL(63, 32))) {
  303. pp->msi_data = pp->cfg0_base;
  304. return 0;
  305. }
  306. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  307. if (!ret)
  308. msi_vaddr = dmam_alloc_coherent(dev, sizeof(u64), &pp->msi_data,
  309. GFP_KERNEL);
  310. if (!msi_vaddr) {
  311. dev_warn(dev, "Failed to allocate 32-bit MSI address\n");
  312. dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
  313. msi_vaddr = dmam_alloc_coherent(dev, sizeof(u64), &pp->msi_data,
  314. GFP_KERNEL);
  315. if (!msi_vaddr) {
  316. dev_err(dev, "Failed to allocate MSI address\n");
  317. dw_pcie_free_msi(pp);
  318. return -ENOMEM;
  319. }
  320. }
  321. return 0;
  322. }
  323. EXPORT_SYMBOL_GPL(dw_pcie_msi_host_init);
  324. static void dw_pcie_host_request_msg_tlp_res(struct dw_pcie_rp *pp)
  325. {
  326. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  327. struct resource_entry *win;
  328. struct resource *res;
  329. win = resource_list_first_type(&pp->bridge->windows, IORESOURCE_MEM);
  330. if (win) {
  331. res = devm_kzalloc(pci->dev, sizeof(*res), GFP_KERNEL);
  332. if (!res)
  333. return;
  334. /*
  335. * Allocate MSG TLP region of size 'region_align' at the end of
  336. * the host bridge window.
  337. */
  338. res->start = win->res->end - pci->region_align + 1;
  339. res->end = win->res->end;
  340. res->name = "msg";
  341. res->flags = win->res->flags | IORESOURCE_BUSY;
  342. if (!devm_request_resource(pci->dev, win->res, res))
  343. pp->msg_res = res;
  344. }
  345. }
  346. static int dw_pcie_config_ecam_iatu(struct dw_pcie_rp *pp)
  347. {
  348. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  349. struct dw_pcie_ob_atu_cfg atu = {0};
  350. resource_size_t bus_range_max;
  351. struct resource_entry *bus;
  352. int ret;
  353. bus = resource_list_first_type(&pp->bridge->windows, IORESOURCE_BUS);
  354. /*
  355. * Root bus under the host bridge doesn't require any iATU configuration
  356. * as DBI region will be used to access root bus config space.
  357. * Immediate bus under Root Bus, needs type 0 iATU configuration and
  358. * remaining buses need type 1 iATU configuration.
  359. */
  360. atu.index = 0;
  361. atu.type = PCIE_ATU_TYPE_CFG0;
  362. atu.parent_bus_addr = pp->cfg0_base + SZ_1M;
  363. /* 1MiB is to cover 1 (bus) * 32 (devices) * 8 (functions) */
  364. atu.size = SZ_1M;
  365. atu.ctrl2 = PCIE_ATU_CFG_SHIFT_MODE_ENABLE;
  366. ret = dw_pcie_prog_outbound_atu(pci, &atu);
  367. if (ret)
  368. return ret;
  369. bus_range_max = resource_size(bus->res);
  370. if (bus_range_max < 2)
  371. return 0;
  372. /* Configure remaining buses in type 1 iATU configuration */
  373. atu.index = 1;
  374. atu.type = PCIE_ATU_TYPE_CFG1;
  375. atu.parent_bus_addr = pp->cfg0_base + SZ_2M;
  376. atu.size = (SZ_1M * bus_range_max) - SZ_2M;
  377. atu.ctrl2 = PCIE_ATU_CFG_SHIFT_MODE_ENABLE;
  378. return dw_pcie_prog_outbound_atu(pci, &atu);
  379. }
  380. static int dw_pcie_create_ecam_window(struct dw_pcie_rp *pp, struct resource *res)
  381. {
  382. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  383. struct device *dev = pci->dev;
  384. struct resource_entry *bus;
  385. bus = resource_list_first_type(&pp->bridge->windows, IORESOURCE_BUS);
  386. if (!bus)
  387. return -ENODEV;
  388. pp->cfg = pci_ecam_create(dev, res, bus->res, &pci_generic_ecam_ops);
  389. if (IS_ERR(pp->cfg))
  390. return PTR_ERR(pp->cfg);
  391. return 0;
  392. }
  393. static bool dw_pcie_ecam_enabled(struct dw_pcie_rp *pp, struct resource *config_res)
  394. {
  395. struct resource *bus_range;
  396. u64 nr_buses;
  397. /* Vendor glue drivers may implement their own ECAM mechanism */
  398. if (pp->native_ecam)
  399. return false;
  400. /*
  401. * PCIe spec r6.0, sec 7.2.2 mandates the base address used for ECAM to
  402. * be aligned on a 2^(n+20) byte boundary, where n is the number of bits
  403. * used for representing 'bus' in BDF. Since the DWC cores always use 8
  404. * bits for representing 'bus', the base address has to be aligned to
  405. * 2^28 byte boundary, which is 256 MiB.
  406. */
  407. if (!IS_256MB_ALIGNED(config_res->start))
  408. return false;
  409. bus_range = resource_list_first_type(&pp->bridge->windows, IORESOURCE_BUS)->res;
  410. if (!bus_range)
  411. return false;
  412. nr_buses = resource_size(config_res) >> PCIE_ECAM_BUS_SHIFT;
  413. return nr_buses >= resource_size(bus_range);
  414. }
  415. static int dw_pcie_host_get_resources(struct dw_pcie_rp *pp)
  416. {
  417. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  418. struct device *dev = pci->dev;
  419. struct platform_device *pdev = to_platform_device(dev);
  420. struct resource_entry *win;
  421. struct resource *res;
  422. int ret;
  423. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
  424. if (!res) {
  425. dev_err(dev, "Missing \"config\" reg space\n");
  426. return -ENODEV;
  427. }
  428. pp->cfg0_size = resource_size(res);
  429. pp->cfg0_base = res->start;
  430. pp->ecam_enabled = dw_pcie_ecam_enabled(pp, res);
  431. if (pp->ecam_enabled) {
  432. ret = dw_pcie_create_ecam_window(pp, res);
  433. if (ret)
  434. return ret;
  435. pp->bridge->ops = &dw_pcie_ecam_ops;
  436. pp->bridge->sysdata = pp->cfg;
  437. pp->cfg->priv = pp;
  438. } else {
  439. pp->va_cfg0_base = devm_pci_remap_cfg_resource(dev, res);
  440. if (IS_ERR(pp->va_cfg0_base))
  441. return PTR_ERR(pp->va_cfg0_base);
  442. /* Set default bus ops */
  443. pp->bridge->ops = &dw_pcie_ops;
  444. pp->bridge->child_ops = &dw_child_pcie_ops;
  445. pp->bridge->sysdata = pp;
  446. }
  447. ret = dw_pcie_get_resources(pci);
  448. if (ret) {
  449. if (pp->cfg)
  450. pci_ecam_free(pp->cfg);
  451. return ret;
  452. }
  453. /* Get the I/O range from DT */
  454. win = resource_list_first_type(&pp->bridge->windows, IORESOURCE_IO);
  455. if (win) {
  456. pp->io_size = resource_size(win->res);
  457. pp->io_bus_addr = win->res->start - win->offset;
  458. pp->io_base = pci_pio_to_address(win->res->start);
  459. }
  460. /*
  461. * visconti_pcie_cpu_addr_fixup() uses pp->io_base, so we have to
  462. * call dw_pcie_parent_bus_offset() after setting pp->io_base.
  463. */
  464. pci->parent_bus_offset = dw_pcie_parent_bus_offset(pci, "config",
  465. pp->cfg0_base);
  466. return 0;
  467. }
  468. int dw_pcie_host_init(struct dw_pcie_rp *pp)
  469. {
  470. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  471. struct device *dev = pci->dev;
  472. struct device_node *np = dev->of_node;
  473. struct pci_host_bridge *bridge;
  474. int ret;
  475. raw_spin_lock_init(&pp->lock);
  476. bridge = devm_pci_alloc_host_bridge(dev, 0);
  477. if (!bridge)
  478. return -ENOMEM;
  479. pp->bridge = bridge;
  480. ret = dw_pcie_host_get_resources(pp);
  481. if (ret)
  482. return ret;
  483. if (pp->ops->init) {
  484. ret = pp->ops->init(pp);
  485. if (ret)
  486. goto err_free_ecam;
  487. }
  488. if (pci_msi_enabled()) {
  489. pp->use_imsi_rx = !(pp->ops->msi_init ||
  490. of_property_present(np, "msi-parent") ||
  491. of_property_present(np, "msi-map"));
  492. /*
  493. * For the use_imsi_rx case the default assignment is handled
  494. * in the dw_pcie_msi_host_init().
  495. */
  496. if (!pp->use_imsi_rx && !pp->num_vectors) {
  497. pp->num_vectors = MSI_DEF_NUM_VECTORS;
  498. } else if (pp->num_vectors > MAX_MSI_IRQS) {
  499. dev_err(dev, "Invalid number of vectors\n");
  500. ret = -EINVAL;
  501. goto err_deinit_host;
  502. }
  503. if (pp->ops->msi_init) {
  504. ret = pp->ops->msi_init(pp);
  505. if (ret < 0)
  506. goto err_deinit_host;
  507. } else if (pp->use_imsi_rx) {
  508. ret = dw_pcie_msi_host_init(pp);
  509. if (ret < 0)
  510. goto err_deinit_host;
  511. }
  512. }
  513. dw_pcie_version_detect(pci);
  514. dw_pcie_iatu_detect(pci);
  515. if (pci->num_lanes < 1)
  516. pci->num_lanes = dw_pcie_link_get_max_link_width(pci);
  517. ret = of_pci_get_equalization_presets(dev, &pp->presets, pci->num_lanes);
  518. if (ret)
  519. goto err_free_msi;
  520. /*
  521. * Allocate the resource for MSG TLP before programming the iATU
  522. * outbound window in dw_pcie_setup_rc(). Since the allocation depends
  523. * on the value of 'region_align', this has to be done after
  524. * dw_pcie_iatu_detect().
  525. *
  526. * Glue drivers need to set 'use_atu_msg' before dw_pcie_host_init() to
  527. * make use of the generic MSG TLP implementation.
  528. */
  529. if (pp->use_atu_msg)
  530. dw_pcie_host_request_msg_tlp_res(pp);
  531. ret = dw_pcie_edma_detect(pci);
  532. if (ret)
  533. goto err_free_msi;
  534. ret = dw_pcie_setup_rc(pp);
  535. if (ret)
  536. goto err_remove_edma;
  537. if (!dw_pcie_link_up(pci)) {
  538. ret = dw_pcie_start_link(pci);
  539. if (ret)
  540. goto err_remove_edma;
  541. }
  542. /*
  543. * Only fail on timeout error. Other errors indicate the device may
  544. * become available later, so continue without failing.
  545. */
  546. ret = dw_pcie_wait_for_link(pci);
  547. if (ret == -ETIMEDOUT)
  548. goto err_stop_link;
  549. ret = pci_host_probe(bridge);
  550. if (ret)
  551. goto err_stop_link;
  552. if (pp->ops->post_init)
  553. pp->ops->post_init(pp);
  554. dwc_pcie_debugfs_init(pci, DW_PCIE_RC_TYPE);
  555. return 0;
  556. err_stop_link:
  557. dw_pcie_stop_link(pci);
  558. err_remove_edma:
  559. dw_pcie_edma_remove(pci);
  560. err_free_msi:
  561. if (pp->use_imsi_rx)
  562. dw_pcie_free_msi(pp);
  563. err_deinit_host:
  564. if (pp->ops->deinit)
  565. pp->ops->deinit(pp);
  566. err_free_ecam:
  567. if (pp->cfg)
  568. pci_ecam_free(pp->cfg);
  569. return ret;
  570. }
  571. EXPORT_SYMBOL_GPL(dw_pcie_host_init);
  572. void dw_pcie_host_deinit(struct dw_pcie_rp *pp)
  573. {
  574. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  575. dwc_pcie_debugfs_deinit(pci);
  576. pci_stop_root_bus(pp->bridge->bus);
  577. pci_remove_root_bus(pp->bridge->bus);
  578. dw_pcie_stop_link(pci);
  579. dw_pcie_edma_remove(pci);
  580. if (pp->use_imsi_rx)
  581. dw_pcie_free_msi(pp);
  582. if (pp->ops->deinit)
  583. pp->ops->deinit(pp);
  584. if (pp->cfg)
  585. pci_ecam_free(pp->cfg);
  586. }
  587. EXPORT_SYMBOL_GPL(dw_pcie_host_deinit);
  588. static void __iomem *dw_pcie_other_conf_map_bus(struct pci_bus *bus,
  589. unsigned int devfn, int where)
  590. {
  591. struct dw_pcie_rp *pp = bus->sysdata;
  592. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  593. struct dw_pcie_ob_atu_cfg atu = { 0 };
  594. int type, ret;
  595. u32 busdev;
  596. /*
  597. * Checking whether the link is up here is a last line of defense
  598. * against platforms that forward errors on the system bus as
  599. * SError upon PCI configuration transactions issued when the link
  600. * is down. This check is racy by definition and does not stop
  601. * the system from triggering an SError if the link goes down
  602. * after this check is performed.
  603. */
  604. if (!dw_pcie_link_up(pci))
  605. return NULL;
  606. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  607. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  608. if (pci_is_root_bus(bus->parent))
  609. type = PCIE_ATU_TYPE_CFG0;
  610. else
  611. type = PCIE_ATU_TYPE_CFG1;
  612. atu.type = type;
  613. atu.parent_bus_addr = pp->cfg0_base - pci->parent_bus_offset;
  614. atu.pci_addr = busdev;
  615. atu.size = pp->cfg0_size;
  616. ret = dw_pcie_prog_outbound_atu(pci, &atu);
  617. if (ret)
  618. return NULL;
  619. return pp->va_cfg0_base + where;
  620. }
  621. static int dw_pcie_rd_other_conf(struct pci_bus *bus, unsigned int devfn,
  622. int where, int size, u32 *val)
  623. {
  624. struct dw_pcie_rp *pp = bus->sysdata;
  625. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  626. struct dw_pcie_ob_atu_cfg atu = { 0 };
  627. int ret;
  628. ret = pci_generic_config_read(bus, devfn, where, size, val);
  629. if (ret != PCIBIOS_SUCCESSFUL)
  630. return ret;
  631. if (pp->cfg0_io_shared) {
  632. atu.type = PCIE_ATU_TYPE_IO;
  633. atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
  634. atu.pci_addr = pp->io_bus_addr;
  635. atu.size = pp->io_size;
  636. ret = dw_pcie_prog_outbound_atu(pci, &atu);
  637. if (ret)
  638. return PCIBIOS_SET_FAILED;
  639. }
  640. return PCIBIOS_SUCCESSFUL;
  641. }
  642. static int dw_pcie_wr_other_conf(struct pci_bus *bus, unsigned int devfn,
  643. int where, int size, u32 val)
  644. {
  645. struct dw_pcie_rp *pp = bus->sysdata;
  646. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  647. struct dw_pcie_ob_atu_cfg atu = { 0 };
  648. int ret;
  649. ret = pci_generic_config_write(bus, devfn, where, size, val);
  650. if (ret != PCIBIOS_SUCCESSFUL)
  651. return ret;
  652. if (pp->cfg0_io_shared) {
  653. atu.type = PCIE_ATU_TYPE_IO;
  654. atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
  655. atu.pci_addr = pp->io_bus_addr;
  656. atu.size = pp->io_size;
  657. ret = dw_pcie_prog_outbound_atu(pci, &atu);
  658. if (ret)
  659. return PCIBIOS_SET_FAILED;
  660. }
  661. return PCIBIOS_SUCCESSFUL;
  662. }
  663. static struct pci_ops dw_child_pcie_ops = {
  664. .map_bus = dw_pcie_other_conf_map_bus,
  665. .read = dw_pcie_rd_other_conf,
  666. .write = dw_pcie_wr_other_conf,
  667. };
  668. void __iomem *dw_pcie_own_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
  669. {
  670. struct dw_pcie_rp *pp = bus->sysdata;
  671. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  672. if (PCI_SLOT(devfn) > 0)
  673. return NULL;
  674. return pci->dbi_base + where;
  675. }
  676. EXPORT_SYMBOL_GPL(dw_pcie_own_conf_map_bus);
  677. static void __iomem *dw_pcie_ecam_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
  678. {
  679. struct pci_config_window *cfg = bus->sysdata;
  680. struct dw_pcie_rp *pp = cfg->priv;
  681. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  682. unsigned int busn = bus->number;
  683. if (busn > 0)
  684. return pci_ecam_map_bus(bus, devfn, where);
  685. if (PCI_SLOT(devfn) > 0)
  686. return NULL;
  687. return pci->dbi_base + where;
  688. }
  689. static struct pci_ops dw_pcie_ops = {
  690. .map_bus = dw_pcie_own_conf_map_bus,
  691. .read = pci_generic_config_read,
  692. .write = pci_generic_config_write,
  693. };
  694. static struct pci_ops dw_pcie_ecam_ops = {
  695. .map_bus = dw_pcie_ecam_conf_map_bus,
  696. .read = pci_generic_config_read,
  697. .write = pci_generic_config_write,
  698. };
  699. static int dw_pcie_iatu_setup(struct dw_pcie_rp *pp)
  700. {
  701. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  702. struct dw_pcie_ob_atu_cfg atu = { 0 };
  703. struct resource_entry *entry;
  704. int ob_iatu_index;
  705. int ib_iatu_index;
  706. int i, ret;
  707. if (!pci->num_ob_windows) {
  708. dev_err(pci->dev, "No outbound iATU found\n");
  709. return -EINVAL;
  710. }
  711. /*
  712. * Ensure all out/inbound windows are disabled before proceeding with
  713. * the MEM/IO (dma-)ranges setups.
  714. */
  715. for (i = 0; i < pci->num_ob_windows; i++)
  716. dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, i);
  717. for (i = 0; i < pci->num_ib_windows; i++)
  718. dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, i);
  719. /*
  720. * NOTE: For outbound address translation, outbound iATU at index 0 is
  721. * reserved for CFG IOs (dw_pcie_other_conf_map_bus()), thus start at
  722. * index 1.
  723. *
  724. * If using ECAM, outbound iATU at index 0 and index 1 is reserved for
  725. * CFG IOs.
  726. */
  727. if (pp->ecam_enabled) {
  728. ob_iatu_index = 2;
  729. ret = dw_pcie_config_ecam_iatu(pp);
  730. if (ret) {
  731. dev_err(pci->dev, "Failed to configure iATU in ECAM mode\n");
  732. return ret;
  733. }
  734. } else {
  735. ob_iatu_index = 1;
  736. }
  737. resource_list_for_each_entry(entry, &pp->bridge->windows) {
  738. resource_size_t res_size;
  739. if (resource_type(entry->res) != IORESOURCE_MEM)
  740. continue;
  741. atu.type = PCIE_ATU_TYPE_MEM;
  742. atu.parent_bus_addr = entry->res->start - pci->parent_bus_offset;
  743. atu.pci_addr = entry->res->start - entry->offset;
  744. /* Adjust iATU size if MSG TLP region was allocated before */
  745. if (pp->msg_res && pp->msg_res->parent == entry->res)
  746. res_size = resource_size(entry->res) -
  747. resource_size(pp->msg_res);
  748. else
  749. res_size = resource_size(entry->res);
  750. while (res_size > 0) {
  751. /*
  752. * Return failure if we run out of windows in the
  753. * middle. Otherwise, we would end up only partially
  754. * mapping a single resource.
  755. */
  756. if (ob_iatu_index >= pci->num_ob_windows) {
  757. dev_err(pci->dev, "Cannot add outbound window for region: %pr\n",
  758. entry->res);
  759. return -ENOMEM;
  760. }
  761. atu.index = ob_iatu_index;
  762. atu.size = MIN(pci->region_limit + 1, res_size);
  763. ret = dw_pcie_prog_outbound_atu(pci, &atu);
  764. if (ret) {
  765. dev_err(pci->dev, "Failed to set MEM range %pr\n",
  766. entry->res);
  767. return ret;
  768. }
  769. ob_iatu_index++;
  770. atu.parent_bus_addr += atu.size;
  771. atu.pci_addr += atu.size;
  772. res_size -= atu.size;
  773. }
  774. }
  775. if (pp->io_size) {
  776. if (ob_iatu_index < pci->num_ob_windows) {
  777. atu.index = ob_iatu_index;
  778. atu.type = PCIE_ATU_TYPE_IO;
  779. atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
  780. atu.pci_addr = pp->io_bus_addr;
  781. atu.size = pp->io_size;
  782. ret = dw_pcie_prog_outbound_atu(pci, &atu);
  783. if (ret) {
  784. dev_err(pci->dev, "Failed to set IO range %pr\n",
  785. entry->res);
  786. return ret;
  787. }
  788. ob_iatu_index++;
  789. } else {
  790. /*
  791. * If there are not enough outbound windows to give I/O
  792. * space its own iATU, the outbound iATU at index 0 will
  793. * be shared between I/O space and CFG IOs, by
  794. * temporarily reconfiguring the iATU to CFG space, in
  795. * order to do a CFG IO, and then immediately restoring
  796. * it to I/O space. This is only implemented when using
  797. * dw_pcie_other_conf_map_bus(), which is not the case
  798. * when using ECAM.
  799. */
  800. if (pp->ecam_enabled) {
  801. dev_err(pci->dev, "Cannot add outbound window for I/O\n");
  802. return -ENOMEM;
  803. }
  804. pp->cfg0_io_shared = true;
  805. }
  806. }
  807. if (pp->use_atu_msg) {
  808. if (ob_iatu_index >= pci->num_ob_windows) {
  809. dev_err(pci->dev, "Cannot add outbound window for MSG TLP\n");
  810. return -ENOMEM;
  811. }
  812. pp->msg_atu_index = ob_iatu_index++;
  813. }
  814. ib_iatu_index = 0;
  815. resource_list_for_each_entry(entry, &pp->bridge->dma_ranges) {
  816. resource_size_t res_start, res_size, window_size;
  817. if (resource_type(entry->res) != IORESOURCE_MEM)
  818. continue;
  819. res_size = resource_size(entry->res);
  820. res_start = entry->res->start;
  821. while (res_size > 0) {
  822. /*
  823. * Return failure if we run out of windows in the
  824. * middle. Otherwise, we would end up only partially
  825. * mapping a single resource.
  826. */
  827. if (ib_iatu_index >= pci->num_ib_windows) {
  828. dev_err(pci->dev, "Cannot add inbound window for region: %pr\n",
  829. entry->res);
  830. return -ENOMEM;
  831. }
  832. window_size = MIN(pci->region_limit + 1, res_size);
  833. ret = dw_pcie_prog_inbound_atu(pci, ib_iatu_index,
  834. PCIE_ATU_TYPE_MEM, res_start,
  835. res_start - entry->offset, window_size);
  836. if (ret) {
  837. dev_err(pci->dev, "Failed to set DMA range %pr\n",
  838. entry->res);
  839. return ret;
  840. }
  841. ib_iatu_index++;
  842. res_start += window_size;
  843. res_size -= window_size;
  844. }
  845. }
  846. return 0;
  847. }
  848. static void dw_pcie_program_presets(struct dw_pcie_rp *pp, enum pci_bus_speed speed)
  849. {
  850. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  851. u8 lane_eq_offset, lane_reg_size, cap_id;
  852. u8 *presets;
  853. u32 cap;
  854. int i;
  855. if (speed == PCIE_SPEED_8_0GT) {
  856. presets = (u8 *)pp->presets.eq_presets_8gts;
  857. lane_eq_offset = PCI_SECPCI_LE_CTRL;
  858. cap_id = PCI_EXT_CAP_ID_SECPCI;
  859. /* For data rate of 8 GT/S each lane equalization control is 16bits wide*/
  860. lane_reg_size = 0x2;
  861. } else if (speed == PCIE_SPEED_16_0GT) {
  862. presets = pp->presets.eq_presets_Ngts[EQ_PRESET_TYPE_16GTS - 1];
  863. lane_eq_offset = PCI_PL_16GT_LE_CTRL;
  864. cap_id = PCI_EXT_CAP_ID_PL_16GT;
  865. lane_reg_size = 0x1;
  866. } else if (speed == PCIE_SPEED_32_0GT) {
  867. presets = pp->presets.eq_presets_Ngts[EQ_PRESET_TYPE_32GTS - 1];
  868. lane_eq_offset = PCI_PL_32GT_LE_CTRL;
  869. cap_id = PCI_EXT_CAP_ID_PL_32GT;
  870. lane_reg_size = 0x1;
  871. } else if (speed == PCIE_SPEED_64_0GT) {
  872. presets = pp->presets.eq_presets_Ngts[EQ_PRESET_TYPE_64GTS - 1];
  873. lane_eq_offset = PCI_PL_64GT_LE_CTRL;
  874. cap_id = PCI_EXT_CAP_ID_PL_64GT;
  875. lane_reg_size = 0x1;
  876. } else {
  877. return;
  878. }
  879. if (presets[0] == PCI_EQ_RESV)
  880. return;
  881. cap = dw_pcie_find_ext_capability(pci, cap_id);
  882. if (!cap)
  883. return;
  884. /*
  885. * Write preset values to the registers byte-by-byte for the given
  886. * number of lanes and register size.
  887. */
  888. for (i = 0; i < pci->num_lanes * lane_reg_size; i++)
  889. dw_pcie_writeb_dbi(pci, cap + lane_eq_offset + i, presets[i]);
  890. }
  891. static void dw_pcie_config_presets(struct dw_pcie_rp *pp)
  892. {
  893. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  894. enum pci_bus_speed speed = pcie_link_speed[pci->max_link_speed];
  895. /*
  896. * Lane equalization settings need to be applied for all data rates the
  897. * controller supports and for all supported lanes.
  898. */
  899. if (speed >= PCIE_SPEED_8_0GT)
  900. dw_pcie_program_presets(pp, PCIE_SPEED_8_0GT);
  901. if (speed >= PCIE_SPEED_16_0GT)
  902. dw_pcie_program_presets(pp, PCIE_SPEED_16_0GT);
  903. if (speed >= PCIE_SPEED_32_0GT)
  904. dw_pcie_program_presets(pp, PCIE_SPEED_32_0GT);
  905. if (speed >= PCIE_SPEED_64_0GT)
  906. dw_pcie_program_presets(pp, PCIE_SPEED_64_0GT);
  907. }
  908. int dw_pcie_setup_rc(struct dw_pcie_rp *pp)
  909. {
  910. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  911. u32 val;
  912. int ret;
  913. /*
  914. * Enable DBI read-only registers for writing/updating configuration.
  915. * Write permission gets disabled towards the end of this function.
  916. */
  917. dw_pcie_dbi_ro_wr_en(pci);
  918. dw_pcie_setup(pci);
  919. dw_pcie_msi_init(pp);
  920. /* Setup RC BARs */
  921. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
  922. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
  923. /* Setup interrupt pins */
  924. val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
  925. val &= 0xffff00ff;
  926. val |= 0x00000100;
  927. dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
  928. /* Setup bus numbers */
  929. val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
  930. val &= 0xff000000;
  931. val |= 0x00ff0100;
  932. dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
  933. /* Setup command register */
  934. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  935. val &= 0xffff0000;
  936. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  937. PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
  938. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  939. dw_pcie_hide_unsupported_l1ss(pci);
  940. dw_pcie_config_presets(pp);
  941. /*
  942. * If the platform provides its own child bus config accesses, it means
  943. * the platform uses its own address translation component rather than
  944. * ATU, so we should not program the ATU here.
  945. */
  946. if (pp->bridge->child_ops == &dw_child_pcie_ops || pp->ecam_enabled) {
  947. ret = dw_pcie_iatu_setup(pp);
  948. if (ret)
  949. return ret;
  950. }
  951. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
  952. /* Program correct class for RC */
  953. dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
  954. val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
  955. val |= PORT_LOGIC_SPEED_CHANGE;
  956. dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
  957. dw_pcie_dbi_ro_wr_dis(pci);
  958. /*
  959. * The iMSI-RX module does not support receiving MSI or MSI-X generated
  960. * by the Root Port. If iMSI-RX is used as the MSI controller, remove
  961. * the MSI and MSI-X capabilities of the Root Port to allow the drivers
  962. * to fall back to INTx instead.
  963. */
  964. if (pp->use_imsi_rx) {
  965. dw_pcie_remove_capability(pci, PCI_CAP_ID_MSI);
  966. dw_pcie_remove_capability(pci, PCI_CAP_ID_MSIX);
  967. }
  968. return 0;
  969. }
  970. EXPORT_SYMBOL_GPL(dw_pcie_setup_rc);
  971. static int dw_pcie_pme_turn_off(struct dw_pcie *pci)
  972. {
  973. struct dw_pcie_ob_atu_cfg atu = { 0 };
  974. void __iomem *mem;
  975. int ret;
  976. if (pci->num_ob_windows <= pci->pp.msg_atu_index)
  977. return -ENOSPC;
  978. if (!pci->pp.msg_res)
  979. return -ENOSPC;
  980. atu.code = PCIE_MSG_CODE_PME_TURN_OFF;
  981. atu.routing = PCIE_MSG_TYPE_R_BC;
  982. atu.type = PCIE_ATU_TYPE_MSG;
  983. atu.size = resource_size(pci->pp.msg_res);
  984. atu.index = pci->pp.msg_atu_index;
  985. atu.parent_bus_addr = pci->pp.msg_res->start - pci->parent_bus_offset;
  986. ret = dw_pcie_prog_outbound_atu(pci, &atu);
  987. if (ret)
  988. return ret;
  989. mem = ioremap(pci->pp.msg_res->start, pci->region_align);
  990. if (!mem)
  991. return -ENOMEM;
  992. /* A dummy write is converted to a Msg TLP */
  993. writel(0, mem);
  994. iounmap(mem);
  995. return 0;
  996. }
  997. int dw_pcie_suspend_noirq(struct dw_pcie *pci)
  998. {
  999. u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
  1000. int ret = 0;
  1001. u32 val;
  1002. if (!dw_pcie_link_up(pci))
  1003. goto stop_link;
  1004. /*
  1005. * If L1SS is supported, then do not put the link into L2 as some
  1006. * devices such as NVMe expect low resume latency.
  1007. */
  1008. if (dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKCTL) & PCI_EXP_LNKCTL_ASPM_L1)
  1009. return 0;
  1010. if (pci->pp.ops->pme_turn_off) {
  1011. pci->pp.ops->pme_turn_off(&pci->pp);
  1012. } else {
  1013. ret = dw_pcie_pme_turn_off(pci);
  1014. if (ret)
  1015. return ret;
  1016. }
  1017. /*
  1018. * Some SoCs do not support reading the LTSSM register after
  1019. * PME_Turn_Off broadcast. For those SoCs, skip waiting for L2/L3 Ready
  1020. * state and wait 10ms as recommended in PCIe spec r6.0, sec 5.3.3.2.1.
  1021. */
  1022. if (pci->pp.skip_l23_ready) {
  1023. mdelay(PCIE_PME_TO_L2_TIMEOUT_US/1000);
  1024. goto stop_link;
  1025. }
  1026. ret = read_poll_timeout(dw_pcie_get_ltssm, val,
  1027. val == DW_PCIE_LTSSM_L2_IDLE ||
  1028. val <= DW_PCIE_LTSSM_DETECT_WAIT,
  1029. PCIE_PME_TO_L2_TIMEOUT_US/10,
  1030. PCIE_PME_TO_L2_TIMEOUT_US, false, pci);
  1031. if (ret) {
  1032. /* Only log message when LTSSM isn't in DETECT or POLL */
  1033. dev_err(pci->dev, "Timeout waiting for L2 entry! LTSSM: 0x%x\n", val);
  1034. return ret;
  1035. }
  1036. /*
  1037. * Per PCIe r6.0, sec 5.3.3.2.1, software should wait at least
  1038. * 100ns after L2/L3 Ready before turning off refclock and
  1039. * main power. This is harmless when no endpoint is connected.
  1040. */
  1041. udelay(1);
  1042. stop_link:
  1043. dw_pcie_stop_link(pci);
  1044. if (pci->pp.ops->deinit)
  1045. pci->pp.ops->deinit(&pci->pp);
  1046. pci->suspended = true;
  1047. return ret;
  1048. }
  1049. EXPORT_SYMBOL_GPL(dw_pcie_suspend_noirq);
  1050. int dw_pcie_resume_noirq(struct dw_pcie *pci)
  1051. {
  1052. int ret;
  1053. if (!pci->suspended)
  1054. return 0;
  1055. pci->suspended = false;
  1056. if (pci->pp.ops->init) {
  1057. ret = pci->pp.ops->init(&pci->pp);
  1058. if (ret) {
  1059. dev_err(pci->dev, "Host init failed: %d\n", ret);
  1060. return ret;
  1061. }
  1062. }
  1063. dw_pcie_setup_rc(&pci->pp);
  1064. ret = dw_pcie_start_link(pci);
  1065. if (ret)
  1066. return ret;
  1067. ret = dw_pcie_wait_for_link(pci);
  1068. if (ret)
  1069. return ret;
  1070. if (pci->pp.ops->post_init)
  1071. pci->pp.ops->post_init(&pci->pp);
  1072. return ret;
  1073. }
  1074. EXPORT_SYMBOL_GPL(dw_pcie_resume_noirq);