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