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