xref: /linux/drivers/pci/controller/dwc/pcie-designware-host.c (revision 63af040b0b4067d75e59df9fa093f3f5dbc5899e)
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 (pp->ops->init) {
591 		ret = pp->ops->init(pp);
592 		if (ret)
593 			goto err_free_ecam;
594 	}
595 
596 	if (pci_msi_enabled()) {
597 		pp->use_imsi_rx = !(pp->ops->msi_init ||
598 				     of_property_present(np, "msi-parent") ||
599 				     of_property_present(np, "msi-map"));
600 
601 		/*
602 		 * For the use_imsi_rx case the default assignment is handled
603 		 * in the dw_pcie_msi_host_init().
604 		 */
605 		if (!pp->use_imsi_rx && !pp->num_vectors) {
606 			pp->num_vectors = MSI_DEF_NUM_VECTORS;
607 		} else if (pp->num_vectors > MAX_MSI_IRQS) {
608 			dev_err(dev, "Invalid number of vectors\n");
609 			ret = -EINVAL;
610 			goto err_deinit_host;
611 		}
612 
613 		if (pp->ops->msi_init) {
614 			ret = pp->ops->msi_init(pp);
615 			if (ret < 0)
616 				goto err_deinit_host;
617 		} else if (pp->use_imsi_rx) {
618 			ret = dw_pcie_msi_host_init(pp);
619 			if (ret < 0)
620 				goto err_deinit_host;
621 		}
622 	}
623 
624 	dw_pcie_version_detect(pci);
625 
626 	dw_pcie_iatu_detect(pci);
627 
628 	if (pci->num_lanes < 1)
629 		pci->num_lanes = dw_pcie_link_get_max_link_width(pci);
630 
631 	ret = of_pci_get_equalization_presets(dev, &pp->presets, pci->num_lanes);
632 	if (ret)
633 		goto err_free_msi;
634 
635 	/*
636 	 * Allocate the resource for MSG TLP before programming the iATU
637 	 * outbound window in dw_pcie_setup_rc(). Since the allocation depends
638 	 * on the value of 'region_align', this has to be done after
639 	 * dw_pcie_iatu_detect().
640 	 *
641 	 * Glue drivers need to set 'use_atu_msg' before dw_pcie_host_init() to
642 	 * make use of the generic MSG TLP implementation.
643 	 */
644 	if (pp->use_atu_msg)
645 		dw_pcie_host_request_msg_tlp_res(pp);
646 
647 	ret = dw_pcie_edma_detect(pci);
648 	if (ret)
649 		goto err_free_msi;
650 
651 	ret = dw_pcie_setup_rc(pp);
652 	if (ret)
653 		goto err_remove_edma;
654 
655 	if (!dw_pcie_link_up(pci)) {
656 		ret = dw_pcie_start_link(pci);
657 		if (ret)
658 			goto err_remove_edma;
659 	}
660 
661 	/*
662 	 * Only fail on timeout error. Other errors indicate the device may
663 	 * become available later, so continue without failing.
664 	 */
665 	ret = dw_pcie_wait_for_link(pci);
666 	if (ret == -ETIMEDOUT)
667 		goto err_stop_link;
668 
669 	ret = pci_host_probe(bridge);
670 	if (ret)
671 		goto err_stop_link;
672 
673 	if (pp->ops->post_init)
674 		pp->ops->post_init(pp);
675 
676 	dwc_pcie_debugfs_init(pci, DW_PCIE_RC_TYPE);
677 
678 	return 0;
679 
680 err_stop_link:
681 	dw_pcie_stop_link(pci);
682 
683 err_remove_edma:
684 	dw_pcie_edma_remove(pci);
685 
686 err_free_msi:
687 	if (pp->use_imsi_rx)
688 		dw_pcie_free_msi(pp);
689 
690 err_deinit_host:
691 	if (pp->ops->deinit)
692 		pp->ops->deinit(pp);
693 
694 err_free_ecam:
695 	if (pp->cfg)
696 		pci_ecam_free(pp->cfg);
697 
698 	return ret;
699 }
700 EXPORT_SYMBOL_GPL(dw_pcie_host_init);
701 
702 void dw_pcie_host_deinit(struct dw_pcie_rp *pp)
703 {
704 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
705 
706 	dwc_pcie_debugfs_deinit(pci);
707 
708 	pci_lock_rescan_remove();
709 	pci_stop_root_bus(pp->bridge->bus);
710 	pci_remove_root_bus(pp->bridge->bus);
711 	pci_unlock_rescan_remove();
712 
713 	dw_pcie_stop_link(pci);
714 
715 	dw_pcie_edma_remove(pci);
716 
717 	if (pp->use_imsi_rx)
718 		dw_pcie_free_msi(pp);
719 
720 	if (pp->ops->deinit)
721 		pp->ops->deinit(pp);
722 
723 	if (pp->cfg)
724 		pci_ecam_free(pp->cfg);
725 }
726 EXPORT_SYMBOL_GPL(dw_pcie_host_deinit);
727 
728 static void __iomem *dw_pcie_other_conf_map_bus(struct pci_bus *bus,
729 						unsigned int devfn, int where)
730 {
731 	struct dw_pcie_rp *pp = bus->sysdata;
732 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
733 	struct dw_pcie_ob_atu_cfg atu = { 0 };
734 	int type, ret;
735 	u32 busdev;
736 
737 	/*
738 	 * Checking whether the link is up here is a last line of defense
739 	 * against platforms that forward errors on the system bus as
740 	 * SError upon PCI configuration transactions issued when the link
741 	 * is down. This check is racy by definition and does not stop
742 	 * the system from triggering an SError if the link goes down
743 	 * after this check is performed.
744 	 */
745 	if (!dw_pcie_link_up(pci))
746 		return NULL;
747 
748 	busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
749 		 PCIE_ATU_FUNC(PCI_FUNC(devfn));
750 
751 	if (pci_is_root_bus(bus->parent))
752 		type = PCIE_TLP_TYPE_CFG0_RDWR;
753 	else
754 		type = PCIE_TLP_TYPE_CFG1_RDWR;
755 
756 	atu.type = type;
757 	atu.parent_bus_addr = pp->cfg0_base - pci->parent_bus_offset;
758 	atu.pci_addr = busdev;
759 	atu.size = pp->cfg0_size;
760 
761 	ret = dw_pcie_prog_outbound_atu(pci, &atu);
762 	if (ret)
763 		return NULL;
764 
765 	return pp->va_cfg0_base + where;
766 }
767 
768 static int dw_pcie_rd_other_conf(struct pci_bus *bus, unsigned int devfn,
769 				 int where, int size, u32 *val)
770 {
771 	struct dw_pcie_rp *pp = bus->sysdata;
772 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
773 	struct dw_pcie_ob_atu_cfg atu = { 0 };
774 	int ret;
775 
776 	ret = pci_generic_config_read(bus, devfn, where, size, val);
777 	if (ret != PCIBIOS_SUCCESSFUL)
778 		return ret;
779 
780 	if (pp->cfg0_io_shared) {
781 		atu.type = PCIE_TLP_TYPE_IO_RDWR;
782 		atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
783 		atu.pci_addr = pp->io_bus_addr;
784 		atu.size = pp->io_size;
785 
786 		ret = dw_pcie_prog_outbound_atu(pci, &atu);
787 		if (ret)
788 			return PCIBIOS_SET_FAILED;
789 	}
790 
791 	return PCIBIOS_SUCCESSFUL;
792 }
793 
794 static int dw_pcie_wr_other_conf(struct pci_bus *bus, unsigned int devfn,
795 				 int where, int size, u32 val)
796 {
797 	struct dw_pcie_rp *pp = bus->sysdata;
798 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
799 	struct dw_pcie_ob_atu_cfg atu = { 0 };
800 	int ret;
801 
802 	ret = pci_generic_config_write(bus, devfn, where, size, val);
803 	if (ret != PCIBIOS_SUCCESSFUL)
804 		return ret;
805 
806 	if (pp->cfg0_io_shared) {
807 		atu.type = PCIE_TLP_TYPE_IO_RDWR;
808 		atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
809 		atu.pci_addr = pp->io_bus_addr;
810 		atu.size = pp->io_size;
811 
812 		ret = dw_pcie_prog_outbound_atu(pci, &atu);
813 		if (ret)
814 			return PCIBIOS_SET_FAILED;
815 	}
816 
817 	return PCIBIOS_SUCCESSFUL;
818 }
819 
820 static struct pci_ops dw_child_pcie_ops = {
821 	.map_bus = dw_pcie_other_conf_map_bus,
822 	.read = dw_pcie_rd_other_conf,
823 	.write = dw_pcie_wr_other_conf,
824 };
825 
826 void __iomem *dw_pcie_own_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
827 {
828 	struct dw_pcie_rp *pp = bus->sysdata;
829 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
830 
831 	if (PCI_SLOT(devfn) > 0)
832 		return NULL;
833 
834 	return pci->dbi_base + where;
835 }
836 EXPORT_SYMBOL_GPL(dw_pcie_own_conf_map_bus);
837 
838 static void __iomem *dw_pcie_ecam_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
839 {
840 	struct pci_config_window *cfg = bus->sysdata;
841 	struct dw_pcie_rp *pp = cfg->priv;
842 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
843 	unsigned int busn = bus->number;
844 
845 	if (busn > 0)
846 		return pci_ecam_map_bus(bus, devfn, where);
847 
848 	if (PCI_SLOT(devfn) > 0)
849 		return NULL;
850 
851 	return pci->dbi_base + where;
852 }
853 
854 static struct pci_ops dw_pcie_ops = {
855 	.map_bus = dw_pcie_own_conf_map_bus,
856 	.read = pci_generic_config_read,
857 	.write = pci_generic_config_write,
858 };
859 
860 static struct pci_ops dw_pcie_ecam_ops = {
861 	.map_bus = dw_pcie_ecam_conf_map_bus,
862 	.read = pci_generic_config_read,
863 	.write = pci_generic_config_write,
864 };
865 
866 static int dw_pcie_iatu_setup(struct dw_pcie_rp *pp)
867 {
868 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
869 	struct dw_pcie_ob_atu_cfg atu = { 0 };
870 	struct resource_entry *entry;
871 	int ob_iatu_index;
872 	int ib_iatu_index;
873 	int i, ret;
874 
875 	if (!pci->num_ob_windows) {
876 		dev_err(pci->dev, "No outbound iATU found\n");
877 		return -EINVAL;
878 	}
879 
880 	/*
881 	 * Ensure all out/inbound windows are disabled before proceeding with
882 	 * the MEM/IO (dma-)ranges setups.
883 	 */
884 	for (i = 0; i < pci->num_ob_windows; i++)
885 		dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, i);
886 
887 	for (i = 0; i < pci->num_ib_windows; i++)
888 		dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, i);
889 
890 	/*
891 	 * NOTE: For outbound address translation, outbound iATU at index 0 is
892 	 * reserved for CFG IOs (dw_pcie_other_conf_map_bus()), thus start at
893 	 * index 1.
894 	 *
895 	 * If using ECAM, outbound iATU at index 0 and index 1 is reserved for
896 	 * CFG IOs.
897 	 */
898 	if (pp->ecam_enabled) {
899 		ob_iatu_index = 2;
900 		ret = dw_pcie_config_ecam_iatu(pp);
901 		if (ret) {
902 			dev_err(pci->dev, "Failed to configure iATU in ECAM mode\n");
903 			return ret;
904 		}
905 	} else {
906 		ob_iatu_index = 1;
907 	}
908 
909 	resource_list_for_each_entry(entry, &pp->bridge->windows) {
910 		resource_size_t res_size;
911 
912 		if (resource_type(entry->res) != IORESOURCE_MEM)
913 			continue;
914 
915 		atu.type = PCIE_TLP_TYPE_MEM_RDWR;
916 		atu.parent_bus_addr = entry->res->start - pci->parent_bus_offset;
917 		atu.pci_addr = entry->res->start - entry->offset;
918 
919 		/* Adjust iATU size if MSG TLP region was allocated before */
920 		if (pp->msg_res && pp->msg_res->parent == entry->res)
921 			res_size = resource_size(entry->res) -
922 					resource_size(pp->msg_res);
923 		else
924 			res_size = resource_size(entry->res);
925 
926 		while (res_size > 0) {
927 			/*
928 			 * Return failure if we run out of windows in the
929 			 * middle. Otherwise, we would end up only partially
930 			 * mapping a single resource.
931 			 */
932 			if (ob_iatu_index >= pci->num_ob_windows) {
933 				dev_err(pci->dev, "Cannot add outbound window for region: %pr\n",
934 					entry->res);
935 				return -ENOMEM;
936 			}
937 
938 			atu.index = ob_iatu_index;
939 			atu.size = MIN(pci->region_limit + 1, res_size);
940 
941 			ret = dw_pcie_prog_outbound_atu(pci, &atu);
942 			if (ret) {
943 				dev_err(pci->dev, "Failed to set MEM range %pr\n",
944 					entry->res);
945 				return ret;
946 			}
947 
948 			ob_iatu_index++;
949 			atu.parent_bus_addr += atu.size;
950 			atu.pci_addr += atu.size;
951 			res_size -= atu.size;
952 		}
953 	}
954 
955 	if (pp->io_size) {
956 		if (ob_iatu_index < pci->num_ob_windows) {
957 			atu.index = ob_iatu_index;
958 			atu.type = PCIE_TLP_TYPE_IO_RDWR;
959 			atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
960 			atu.pci_addr = pp->io_bus_addr;
961 			atu.size = pp->io_size;
962 
963 			ret = dw_pcie_prog_outbound_atu(pci, &atu);
964 			if (ret) {
965 				dev_err(pci->dev, "Failed to set IO range %pr\n",
966 					entry->res);
967 				return ret;
968 			}
969 			ob_iatu_index++;
970 		} else {
971 			/*
972 			 * If there are not enough outbound windows to give I/O
973 			 * space its own iATU, the outbound iATU at index 0 will
974 			 * be shared between I/O space and CFG IOs, by
975 			 * temporarily reconfiguring the iATU to CFG space, in
976 			 * order to do a CFG IO, and then immediately restoring
977 			 * it to I/O space. This is only implemented when using
978 			 * dw_pcie_other_conf_map_bus(), which is not the case
979 			 * when using ECAM.
980 			 */
981 			if (pp->ecam_enabled) {
982 				dev_err(pci->dev, "Cannot add outbound window for I/O\n");
983 				return -ENOMEM;
984 			}
985 			pp->cfg0_io_shared = true;
986 		}
987 	}
988 
989 	if (pp->use_atu_msg) {
990 		if (ob_iatu_index >= pci->num_ob_windows) {
991 			dev_err(pci->dev, "Cannot add outbound window for MSG TLP\n");
992 			return -ENOMEM;
993 		}
994 		pp->msg_atu_index = ob_iatu_index++;
995 	}
996 
997 	ib_iatu_index = 0;
998 	resource_list_for_each_entry(entry, &pp->bridge->dma_ranges) {
999 		resource_size_t res_start, res_size, window_size;
1000 
1001 		if (resource_type(entry->res) != IORESOURCE_MEM)
1002 			continue;
1003 
1004 		res_size = resource_size(entry->res);
1005 		res_start = entry->res->start;
1006 		while (res_size > 0) {
1007 			/*
1008 			 * Return failure if we run out of windows in the
1009 			 * middle. Otherwise, we would end up only partially
1010 			 * mapping a single resource.
1011 			 */
1012 			if (ib_iatu_index >= pci->num_ib_windows) {
1013 				dev_err(pci->dev, "Cannot add inbound window for region: %pr\n",
1014 					entry->res);
1015 				return -ENOMEM;
1016 			}
1017 
1018 			window_size = MIN(pci->region_limit + 1, res_size);
1019 			ret = dw_pcie_prog_inbound_atu(pci, ib_iatu_index,
1020 						       PCIE_TLP_TYPE_MEM_RDWR, res_start,
1021 						       res_start - entry->offset, window_size);
1022 			if (ret) {
1023 				dev_err(pci->dev, "Failed to set DMA range %pr\n",
1024 					entry->res);
1025 				return ret;
1026 			}
1027 
1028 			ib_iatu_index++;
1029 			res_start += window_size;
1030 			res_size -= window_size;
1031 		}
1032 	}
1033 
1034 	return 0;
1035 }
1036 
1037 static void dw_pcie_program_presets(struct dw_pcie_rp *pp, enum pci_bus_speed speed)
1038 {
1039 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1040 	u8 lane_eq_offset, lane_reg_size, cap_id;
1041 	u8 *presets;
1042 	u32 cap;
1043 	int i;
1044 
1045 	if (speed == PCIE_SPEED_8_0GT) {
1046 		presets = (u8 *)pp->presets.eq_presets_8gts;
1047 		lane_eq_offset =  PCI_SECPCI_LE_CTRL;
1048 		cap_id = PCI_EXT_CAP_ID_SECPCI;
1049 		/* For data rate of 8 GT/S each lane equalization control is 16bits wide*/
1050 		lane_reg_size = 0x2;
1051 	} else if (speed == PCIE_SPEED_16_0GT) {
1052 		presets = pp->presets.eq_presets_Ngts[EQ_PRESET_TYPE_16GTS - 1];
1053 		lane_eq_offset = PCI_PL_16GT_LE_CTRL;
1054 		cap_id = PCI_EXT_CAP_ID_PL_16GT;
1055 		lane_reg_size = 0x1;
1056 	} else if (speed == PCIE_SPEED_32_0GT) {
1057 		presets =  pp->presets.eq_presets_Ngts[EQ_PRESET_TYPE_32GTS - 1];
1058 		lane_eq_offset = PCI_PL_32GT_LE_CTRL;
1059 		cap_id = PCI_EXT_CAP_ID_PL_32GT;
1060 		lane_reg_size = 0x1;
1061 	} else if (speed == PCIE_SPEED_64_0GT) {
1062 		presets =  pp->presets.eq_presets_Ngts[EQ_PRESET_TYPE_64GTS - 1];
1063 		lane_eq_offset = PCI_PL_64GT_LE_CTRL;
1064 		cap_id = PCI_EXT_CAP_ID_PL_64GT;
1065 		lane_reg_size = 0x1;
1066 	} else {
1067 		return;
1068 	}
1069 
1070 	if (presets[0] == PCI_EQ_RESV)
1071 		return;
1072 
1073 	cap = dw_pcie_find_ext_capability(pci, cap_id);
1074 	if (!cap)
1075 		return;
1076 
1077 	/*
1078 	 * Write preset values to the registers byte-by-byte for the given
1079 	 * number of lanes and register size.
1080 	 */
1081 	for (i = 0; i < pci->num_lanes * lane_reg_size; i++)
1082 		dw_pcie_writeb_dbi(pci, cap + lane_eq_offset + i, presets[i]);
1083 }
1084 
1085 static void dw_pcie_config_presets(struct dw_pcie_rp *pp)
1086 {
1087 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1088 	enum pci_bus_speed speed = pcie_get_link_speed(pci->max_link_speed);
1089 
1090 	/*
1091 	 * Lane equalization settings need to be applied for all data rates the
1092 	 * controller supports and for all supported lanes.
1093 	 */
1094 
1095 	if (speed >= PCIE_SPEED_8_0GT)
1096 		dw_pcie_program_presets(pp, PCIE_SPEED_8_0GT);
1097 
1098 	if (speed >= PCIE_SPEED_16_0GT)
1099 		dw_pcie_program_presets(pp, PCIE_SPEED_16_0GT);
1100 
1101 	if (speed >= PCIE_SPEED_32_0GT)
1102 		dw_pcie_program_presets(pp, PCIE_SPEED_32_0GT);
1103 
1104 	if (speed >= PCIE_SPEED_64_0GT)
1105 		dw_pcie_program_presets(pp, PCIE_SPEED_64_0GT);
1106 }
1107 
1108 int dw_pcie_setup_rc(struct dw_pcie_rp *pp)
1109 {
1110 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1111 	u32 val;
1112 	int ret;
1113 
1114 	/*
1115 	 * Enable DBI read-only registers for writing/updating configuration.
1116 	 * Write permission gets disabled towards the end of this function.
1117 	 */
1118 	dw_pcie_dbi_ro_wr_en(pci);
1119 
1120 	dw_pcie_setup(pci);
1121 
1122 	dw_pcie_msi_init(pp);
1123 
1124 	/* Setup RC BARs */
1125 	dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
1126 	dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
1127 
1128 	/* Setup interrupt pins */
1129 	val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
1130 	val &= 0xffff00ff;
1131 	val |= 0x00000100;
1132 	dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
1133 
1134 	/* Setup bus numbers */
1135 	val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
1136 	val &= 0xff000000;
1137 	val |= 0x00ff0100;
1138 	dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
1139 
1140 	/* Setup command register */
1141 	val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
1142 	val &= 0xffff0000;
1143 	val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
1144 		PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
1145 	dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
1146 
1147 	dw_pcie_hide_unsupported_l1ss(pci);
1148 
1149 	dw_pcie_config_presets(pp);
1150 	/*
1151 	 * If the platform provides its own child bus config accesses, it means
1152 	 * the platform uses its own address translation component rather than
1153 	 * ATU, so we should not program the ATU here.
1154 	 */
1155 	if (pp->bridge->child_ops == &dw_child_pcie_ops || pp->ecam_enabled) {
1156 		ret = dw_pcie_iatu_setup(pp);
1157 		if (ret)
1158 			return ret;
1159 	}
1160 
1161 	dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
1162 
1163 	/* Program correct class for RC */
1164 	dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
1165 
1166 	val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
1167 	val |= PORT_LOGIC_SPEED_CHANGE;
1168 	dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
1169 
1170 	dw_pcie_dbi_ro_wr_dis(pci);
1171 
1172 	/*
1173 	 * The iMSI-RX module does not support receiving MSI or MSI-X generated
1174 	 * by the Root Port. If iMSI-RX is used as the MSI controller, remove
1175 	 * the MSI and MSI-X capabilities of the Root Port to allow the drivers
1176 	 * to fall back to INTx instead.
1177 	 */
1178 	if (pp->use_imsi_rx && !pp->keep_rp_msi_en) {
1179 		dw_pcie_remove_capability(pci, PCI_CAP_ID_MSI);
1180 		dw_pcie_remove_capability(pci, PCI_CAP_ID_MSIX);
1181 	}
1182 
1183 	return 0;
1184 }
1185 EXPORT_SYMBOL_GPL(dw_pcie_setup_rc);
1186 
1187 static int dw_pcie_pme_turn_off(struct dw_pcie *pci)
1188 {
1189 	struct dw_pcie_ob_atu_cfg atu = { 0 };
1190 	void __iomem *mem;
1191 	int ret;
1192 
1193 	if (pci->num_ob_windows <= pci->pp.msg_atu_index)
1194 		return -ENOSPC;
1195 
1196 	if (!pci->pp.msg_res)
1197 		return -ENOSPC;
1198 
1199 	atu.code = PCIE_MSG_CODE_PME_TURN_OFF;
1200 	atu.routing = PCIE_MSG_TYPE_R_BC;
1201 	atu.type = PCIE_TLP_TYPE_MSG;
1202 	atu.size = resource_size(pci->pp.msg_res);
1203 	atu.index = pci->pp.msg_atu_index;
1204 
1205 	atu.parent_bus_addr = pci->pp.msg_res->start - pci->parent_bus_offset;
1206 
1207 	ret = dw_pcie_prog_outbound_atu(pci, &atu);
1208 	if (ret)
1209 		return ret;
1210 
1211 	mem = ioremap(pci->pp.msg_res->start, pci->region_align);
1212 	if (!mem)
1213 		return -ENOMEM;
1214 
1215 	/* A dummy write is converted to a Msg TLP */
1216 	writel(0, mem);
1217 
1218 	iounmap(mem);
1219 
1220 	return 0;
1221 }
1222 
1223 int dw_pcie_suspend_noirq(struct dw_pcie *pci)
1224 {
1225 	bool pme_capable = false;
1226 	int ret = 0;
1227 	u32 val;
1228 
1229 	if (!dw_pcie_link_up(pci))
1230 		goto stop_link;
1231 
1232 	if (!pci_host_common_d3cold_possible(pci->pp.bridge, &pme_capable))
1233 		return 0;
1234 
1235 	if (pci->pp.ops->pme_turn_off) {
1236 		pci->pp.ops->pme_turn_off(&pci->pp);
1237 	} else {
1238 		ret = dw_pcie_pme_turn_off(pci);
1239 		if (ret)
1240 			return ret;
1241 	}
1242 
1243 	/*
1244 	 * Some SoCs do not support reading the LTSSM register after
1245 	 * PME_Turn_Off broadcast. For those SoCs, skip waiting for L2/L3 Ready
1246 	 * state and wait 10ms as recommended in PCIe spec r6.0, sec 5.3.3.2.1.
1247 	 */
1248 	if (pci->pp.skip_l23_ready) {
1249 		mdelay(PCIE_PME_TO_L2_TIMEOUT_US/1000);
1250 		goto stop_link;
1251 	}
1252 
1253 	ret = read_poll_timeout(dw_pcie_get_ltssm, val,
1254 				val == DW_PCIE_LTSSM_L2_IDLE ||
1255 				val <= DW_PCIE_LTSSM_DETECT_WAIT,
1256 				PCIE_PME_TO_L2_TIMEOUT_US/10,
1257 				PCIE_PME_TO_L2_TIMEOUT_US, false, pci);
1258 	if (ret) {
1259 		/*
1260 		 * Failure is non-fatal since spec r7.0, sec 5.3.3.2.1,
1261 		 * recommends proceeding with L2/L3 sequence even if one or more
1262 		 * devices do not respond with PME_TO_Ack after 10ms timeout.
1263 		 */
1264 		dev_warn(pci->dev, "Timeout waiting for L2 entry! LTSSM: 0x%x\n", val);
1265 		ret = 0;
1266 	}
1267 
1268 	/*
1269 	 * Per PCIe r6.0, sec 5.3.3.2.1, software should wait at least
1270 	 * 100ns after L2/L3 Ready before turning off refclock and
1271 	 * main power. This is harmless when no endpoint is connected.
1272 	 */
1273 	udelay(1);
1274 
1275 stop_link:
1276 	/*
1277 	 * TODO: "pme_capable" means some downstream device is wakeup-
1278 	 * enabled and is capable of generating PME from D3cold, which
1279 	 * requires auxiliary power.  Instead of always skipping power off
1280 	 * if PME is supported from D3cold, query the pwrctrl core and skip
1281 	 * power off only if device supports PME from D3cold and Vaux is
1282 	 * not supported.
1283 	 */
1284 	pci->pp.skip_pwrctrl_off = pme_capable;
1285 	dw_pcie_stop_link(pci);
1286 	if (pci->pp.ops->deinit)
1287 		pci->pp.ops->deinit(&pci->pp);
1288 
1289 	pci->suspended = true;
1290 
1291 	return ret;
1292 }
1293 EXPORT_SYMBOL_GPL(dw_pcie_suspend_noirq);
1294 
1295 int dw_pcie_resume_noirq(struct dw_pcie *pci)
1296 {
1297 	int ret;
1298 
1299 	if (!pci->suspended)
1300 		return 0;
1301 
1302 	if (pci->pp.ops->init) {
1303 		ret = pci->pp.ops->init(&pci->pp);
1304 		if (ret) {
1305 			dev_err(pci->dev, "Host init failed: %d\n", ret);
1306 			return ret;
1307 		}
1308 	}
1309 
1310 	dw_pcie_setup_rc(&pci->pp);
1311 
1312 	ret = dw_pcie_start_link(pci);
1313 	if (ret)
1314 		goto err_deinit;
1315 
1316 	ret = dw_pcie_wait_for_link(pci);
1317 	if (ret == -ETIMEDOUT)
1318 		goto err_stop_link;
1319 
1320 	if (pci->pp.ops->post_init)
1321 		pci->pp.ops->post_init(&pci->pp);
1322 
1323 	pci->suspended = false;
1324 
1325 	return 0;
1326 
1327 err_stop_link:
1328 	dw_pcie_stop_link(pci);
1329 
1330 err_deinit:
1331 	if (pci->pp.ops->deinit)
1332 		pci->pp.ops->deinit(&pci->pp);
1333 
1334 	return ret;
1335 }
1336 EXPORT_SYMBOL_GPL(dw_pcie_resume_noirq);
1337