1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Synopsys DesignWare PCIe Endpoint controller driver 4 * 5 * Copyright (C) 2017 Texas Instruments 6 * Author: Kishon Vijay Abraham I <kishon@ti.com> 7 */ 8 9 #include <linux/align.h> 10 #include <linux/bitfield.h> 11 #include <linux/of.h> 12 #include <linux/overflow.h> 13 #include <linux/platform_device.h> 14 15 #include "pcie-designware.h" 16 #include <linux/pci-epc.h> 17 #include <linux/pci-epf.h> 18 19 /** 20 * dw_pcie_ep_get_func_from_ep - Get the struct dw_pcie_ep_func corresponding to 21 * the endpoint function 22 * @ep: DWC EP device 23 * @func_no: Function number of the endpoint device 24 * 25 * Return: struct dw_pcie_ep_func if success, NULL otherwise. 26 */ 27 struct dw_pcie_ep_func * 28 dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no) 29 { 30 struct dw_pcie_ep_func *ep_func; 31 32 list_for_each_entry(ep_func, &ep->func_list, list) { 33 if (ep_func->func_no == func_no) 34 return ep_func; 35 } 36 37 return NULL; 38 } 39 40 static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no, 41 enum pci_barno bar, int flags) 42 { 43 struct dw_pcie_ep *ep = &pci->ep; 44 u32 reg; 45 46 reg = PCI_BASE_ADDRESS_0 + (4 * bar); 47 dw_pcie_dbi_ro_wr_en(pci); 48 dw_pcie_ep_writel_dbi2(ep, func_no, reg, 0x0); 49 dw_pcie_ep_writel_dbi(ep, func_no, reg, 0x0); 50 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) { 51 dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, 0x0); 52 dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0x0); 53 } 54 dw_pcie_dbi_ro_wr_dis(pci); 55 } 56 57 /** 58 * dw_pcie_ep_reset_bar - Reset endpoint BAR 59 * @pci: DWC PCI device 60 * @bar: BAR number of the endpoint 61 */ 62 void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar) 63 { 64 u8 func_no, funcs; 65 66 funcs = pci->ep.epc->max_functions; 67 68 for (func_no = 0; func_no < funcs; func_no++) 69 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0); 70 } 71 EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar); 72 73 static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap) 74 { 75 return PCI_FIND_NEXT_CAP(dw_pcie_ep_read_cfg, PCI_CAPABILITY_LIST, 76 cap, NULL, ep, func_no); 77 } 78 79 static u16 dw_pcie_ep_find_ext_capability(struct dw_pcie_ep *ep, 80 u8 func_no, u8 cap) 81 { 82 return PCI_FIND_NEXT_EXT_CAP(dw_pcie_ep_read_cfg, 0, 83 cap, NULL, ep, func_no); 84 } 85 86 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 87 struct pci_epf_header *hdr) 88 { 89 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 90 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 91 92 dw_pcie_dbi_ro_wr_en(pci); 93 dw_pcie_ep_writew_dbi(ep, func_no, PCI_VENDOR_ID, hdr->vendorid); 94 dw_pcie_ep_writew_dbi(ep, func_no, PCI_DEVICE_ID, hdr->deviceid); 95 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_REVISION_ID, hdr->revid); 96 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_CLASS_PROG, hdr->progif_code); 97 dw_pcie_ep_writew_dbi(ep, func_no, PCI_CLASS_DEVICE, 98 hdr->subclass_code | hdr->baseclass_code << 8); 99 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_CACHE_LINE_SIZE, 100 hdr->cache_line_size); 101 dw_pcie_ep_writew_dbi(ep, func_no, PCI_SUBSYSTEM_VENDOR_ID, 102 hdr->subsys_vendor_id); 103 dw_pcie_ep_writew_dbi(ep, func_no, PCI_SUBSYSTEM_ID, hdr->subsys_id); 104 dw_pcie_ep_writeb_dbi(ep, func_no, PCI_INTERRUPT_PIN, 105 hdr->interrupt_pin); 106 dw_pcie_dbi_ro_wr_dis(pci); 107 108 return 0; 109 } 110 111 /* BAR Match Mode inbound iATU mapping */ 112 static int dw_pcie_ep_ib_atu_bar(struct dw_pcie_ep *ep, u8 func_no, int type, 113 dma_addr_t parent_bus_addr, enum pci_barno bar, 114 size_t size) 115 { 116 int ret; 117 u32 free_win; 118 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 119 struct dw_pcie_ep_func *ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 120 121 if (!ep_func) 122 return -EINVAL; 123 124 if (!ep_func->bar_to_atu[bar]) 125 free_win = find_first_zero_bit(ep->ib_window_map, pci->num_ib_windows); 126 else 127 free_win = ep_func->bar_to_atu[bar] - 1; 128 129 if (free_win >= pci->num_ib_windows) { 130 dev_err(pci->dev, "No free inbound window\n"); 131 return -EINVAL; 132 } 133 134 ret = dw_pcie_prog_ep_inbound_atu(pci, func_no, free_win, type, 135 parent_bus_addr, bar, size); 136 if (ret < 0) { 137 dev_err(pci->dev, "Failed to program IB window\n"); 138 return ret; 139 } 140 141 /* 142 * Always increment free_win before assignment, since value 0 is used to identify 143 * unallocated mapping. 144 */ 145 ep_func->bar_to_atu[bar] = free_win + 1; 146 set_bit(free_win, ep->ib_window_map); 147 148 return 0; 149 } 150 151 static void dw_pcie_ep_clear_ib_maps(struct dw_pcie_ep *ep, u8 func_no, enum pci_barno bar) 152 { 153 struct dw_pcie_ep_func *ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 154 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 155 struct device *dev = pci->dev; 156 unsigned int i, num; 157 u32 atu_index; 158 u32 *indexes; 159 160 if (!ep_func) 161 return; 162 163 /* Tear down the BAR Match Mode mapping, if any. */ 164 if (ep_func->bar_to_atu[bar]) { 165 atu_index = ep_func->bar_to_atu[bar] - 1; 166 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, atu_index); 167 clear_bit(atu_index, ep->ib_window_map); 168 ep_func->bar_to_atu[bar] = 0; 169 return; 170 } 171 172 /* Tear down all Address Match Mode mappings, if any. */ 173 indexes = ep_func->ib_atu_indexes[bar]; 174 num = ep_func->num_ib_atu_indexes[bar]; 175 ep_func->ib_atu_indexes[bar] = NULL; 176 ep_func->num_ib_atu_indexes[bar] = 0; 177 if (!indexes) 178 return; 179 for (i = 0; i < num; i++) { 180 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, indexes[i]); 181 clear_bit(indexes[i], ep->ib_window_map); 182 } 183 devm_kfree(dev, indexes); 184 } 185 186 static u64 dw_pcie_ep_read_bar_assigned(struct dw_pcie_ep *ep, u8 func_no, 187 enum pci_barno bar, int flags) 188 { 189 u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar); 190 u32 lo, hi; 191 u64 addr; 192 193 lo = dw_pcie_ep_readl_dbi(ep, func_no, reg); 194 195 if (flags & PCI_BASE_ADDRESS_SPACE) 196 return lo & PCI_BASE_ADDRESS_IO_MASK; 197 198 addr = lo & PCI_BASE_ADDRESS_MEM_MASK; 199 if (!(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)) 200 return addr; 201 202 hi = dw_pcie_ep_readl_dbi(ep, func_no, reg + 4); 203 return addr | ((u64)hi << 32); 204 } 205 206 static int dw_pcie_ep_validate_submap(struct dw_pcie_ep *ep, 207 const struct pci_epf_bar_submap *submap, 208 unsigned int num_submap, size_t bar_size) 209 { 210 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 211 u32 align = pci->region_align; 212 size_t off = 0; 213 unsigned int i; 214 size_t size; 215 216 if (!align || !IS_ALIGNED(bar_size, align)) 217 return -EINVAL; 218 219 /* 220 * The submap array order defines the BAR layout (submap[0] starts 221 * at offset 0 and each entry immediately follows the previous 222 * one). Here, validate that it forms a strict, gapless 223 * decomposition of the BAR: 224 * - each entry has a non-zero size 225 * - sizes, implicit offsets and phys_addr are aligned to 226 * pci->region_align 227 * - each entry lies within the BAR range 228 * - the entries exactly cover the whole BAR 229 * 230 * Note: dw_pcie_prog_inbound_atu() also checks alignment for the 231 * PCI address and the target phys_addr, but validating up-front 232 * avoids partially programming iATU windows in vain. 233 */ 234 for (i = 0; i < num_submap; i++) { 235 size = submap[i].size; 236 237 if (!size) 238 return -EINVAL; 239 240 if (!IS_ALIGNED(size, align) || !IS_ALIGNED(off, align)) 241 return -EINVAL; 242 243 if (!IS_ALIGNED(submap[i].phys_addr, align)) 244 return -EINVAL; 245 246 if (off > bar_size || size > bar_size - off) 247 return -EINVAL; 248 249 off += size; 250 } 251 if (off != bar_size) 252 return -EINVAL; 253 254 return 0; 255 } 256 257 /* Address Match Mode inbound iATU mapping */ 258 static int dw_pcie_ep_ib_atu_addr(struct dw_pcie_ep *ep, u8 func_no, int type, 259 const struct pci_epf_bar *epf_bar) 260 { 261 struct dw_pcie_ep_func *ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 262 const struct pci_epf_bar_submap *submap = epf_bar->submap; 263 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 264 enum pci_barno bar = epf_bar->barno; 265 struct device *dev = pci->dev; 266 u64 pci_addr, parent_bus_addr; 267 u64 size, base, off = 0; 268 int free_win, ret; 269 unsigned int i; 270 u32 *indexes; 271 272 if (!ep_func || !epf_bar->num_submap || !submap || !epf_bar->size) 273 return -EINVAL; 274 275 ret = dw_pcie_ep_validate_submap(ep, submap, epf_bar->num_submap, 276 epf_bar->size); 277 if (ret) 278 return ret; 279 280 base = dw_pcie_ep_read_bar_assigned(ep, func_no, bar, epf_bar->flags); 281 if (!base) { 282 dev_err(dev, 283 "BAR%u not assigned, cannot set up sub-range mappings\n", 284 bar); 285 return -EINVAL; 286 } 287 288 indexes = devm_kcalloc(dev, epf_bar->num_submap, sizeof(*indexes), 289 GFP_KERNEL); 290 if (!indexes) 291 return -ENOMEM; 292 293 ep_func->ib_atu_indexes[bar] = indexes; 294 ep_func->num_ib_atu_indexes[bar] = 0; 295 296 for (i = 0; i < epf_bar->num_submap; i++) { 297 size = submap[i].size; 298 parent_bus_addr = submap[i].phys_addr; 299 300 if (off > (~0ULL) - base) { 301 ret = -EINVAL; 302 goto err; 303 } 304 305 pci_addr = base + off; 306 off += size; 307 308 free_win = find_first_zero_bit(ep->ib_window_map, 309 pci->num_ib_windows); 310 if (free_win >= pci->num_ib_windows) { 311 ret = -ENOSPC; 312 goto err; 313 } 314 315 ret = dw_pcie_prog_inbound_atu(pci, free_win, type, 316 parent_bus_addr, pci_addr, size); 317 if (ret) 318 goto err; 319 320 set_bit(free_win, ep->ib_window_map); 321 indexes[i] = free_win; 322 ep_func->num_ib_atu_indexes[bar] = i + 1; 323 } 324 return 0; 325 err: 326 dw_pcie_ep_clear_ib_maps(ep, func_no, bar); 327 return ret; 328 } 329 330 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, 331 struct dw_pcie_ob_atu_cfg *atu) 332 { 333 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 334 u32 free_win; 335 int ret; 336 337 free_win = find_first_zero_bit(ep->ob_window_map, pci->num_ob_windows); 338 if (free_win >= pci->num_ob_windows) { 339 dev_err(pci->dev, "No free outbound window\n"); 340 return -EINVAL; 341 } 342 343 atu->index = free_win; 344 ret = dw_pcie_prog_outbound_atu(pci, atu); 345 if (ret) 346 return ret; 347 348 set_bit(free_win, ep->ob_window_map); 349 ep->outbound_addr[free_win] = atu->parent_bus_addr; 350 351 return 0; 352 } 353 354 static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 355 struct pci_epf_bar *epf_bar) 356 { 357 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 358 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 359 enum pci_barno bar = epf_bar->barno; 360 struct dw_pcie_ep_func *ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 361 362 if (!ep_func || !ep_func->epf_bar[bar]) 363 return; 364 365 __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags); 366 367 dw_pcie_ep_clear_ib_maps(ep, func_no, bar); 368 369 ep_func->epf_bar[bar] = NULL; 370 } 371 372 static unsigned int dw_pcie_ep_get_rebar_offset(struct dw_pcie_ep *ep, u8 func_no, 373 enum pci_barno bar) 374 { 375 u32 reg, bar_index; 376 unsigned int offset, nbars; 377 int i; 378 379 offset = dw_pcie_ep_find_ext_capability(ep, func_no, PCI_EXT_CAP_ID_REBAR); 380 if (!offset) 381 return offset; 382 383 reg = dw_pcie_ep_readl_dbi(ep, func_no, offset + PCI_REBAR_CTRL); 384 nbars = FIELD_GET(PCI_REBAR_CTRL_NBAR_MASK, reg); 385 386 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL) { 387 reg = dw_pcie_ep_readl_dbi(ep, func_no, offset + PCI_REBAR_CTRL); 388 bar_index = FIELD_GET(PCI_REBAR_CTRL_BAR_IDX, reg); 389 if (bar_index == bar) 390 return offset; 391 } 392 393 return 0; 394 } 395 396 static int dw_pcie_ep_set_bar_resizable(struct dw_pcie_ep *ep, u8 func_no, 397 struct pci_epf_bar *epf_bar) 398 { 399 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 400 enum pci_barno bar = epf_bar->barno; 401 size_t size = epf_bar->size; 402 int flags = epf_bar->flags; 403 u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar); 404 unsigned int rebar_offset; 405 u32 rebar_cap, rebar_ctrl; 406 int ret; 407 408 rebar_offset = dw_pcie_ep_get_rebar_offset(ep, func_no, bar); 409 if (!rebar_offset) 410 return -EINVAL; 411 412 ret = pci_epc_bar_size_to_rebar_cap(size, &rebar_cap); 413 if (ret) 414 return ret; 415 416 dw_pcie_dbi_ro_wr_en(pci); 417 418 /* 419 * A BAR mask should not be written for a resizable BAR. The BAR mask 420 * is automatically derived by the controller every time the "selected 421 * size" bits are updated, see "Figure 3-26 Resizable BAR Example for 422 * 32-bit Memory BAR0" in DWC EP databook 5.96a. We simply need to write 423 * BIT(0) to set the BAR enable bit. 424 */ 425 dw_pcie_ep_writel_dbi2(ep, func_no, reg, BIT(0)); 426 dw_pcie_ep_writel_dbi(ep, func_no, reg, flags); 427 428 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) { 429 dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, 0); 430 dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0); 431 } 432 433 /* 434 * Bits 31:0 in PCI_REBAR_CAP define "supported sizes" bits for sizes 435 * 1 MB to 128 TB. Bits 31:16 in PCI_REBAR_CTRL define "supported sizes" 436 * bits for sizes 256 TB to 8 EB. Disallow sizes 256 TB to 8 EB. 437 */ 438 rebar_ctrl = dw_pcie_ep_readl_dbi(ep, func_no, rebar_offset + PCI_REBAR_CTRL); 439 rebar_ctrl &= ~GENMASK(31, 16); 440 dw_pcie_ep_writel_dbi(ep, func_no, rebar_offset + PCI_REBAR_CTRL, rebar_ctrl); 441 442 /* 443 * The "selected size" (bits 13:8) in PCI_REBAR_CTRL are automatically 444 * updated when writing PCI_REBAR_CAP, see "Figure 3-26 Resizable BAR 445 * Example for 32-bit Memory BAR0" in DWC EP databook 5.96a. 446 */ 447 dw_pcie_ep_writel_dbi(ep, func_no, rebar_offset + PCI_REBAR_CAP, rebar_cap); 448 449 dw_pcie_dbi_ro_wr_dis(pci); 450 451 return 0; 452 } 453 454 static int dw_pcie_ep_set_bar_programmable(struct dw_pcie_ep *ep, u8 func_no, 455 struct pci_epf_bar *epf_bar) 456 { 457 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 458 enum pci_barno bar = epf_bar->barno; 459 size_t size = epf_bar->size; 460 int flags = epf_bar->flags; 461 u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar); 462 463 dw_pcie_dbi_ro_wr_en(pci); 464 465 dw_pcie_ep_writel_dbi2(ep, func_no, reg, lower_32_bits(size - 1)); 466 dw_pcie_ep_writel_dbi(ep, func_no, reg, flags); 467 468 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) { 469 dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, upper_32_bits(size - 1)); 470 dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0); 471 } 472 473 dw_pcie_dbi_ro_wr_dis(pci); 474 475 return 0; 476 } 477 478 static enum pci_epc_bar_type dw_pcie_ep_get_bar_type(struct dw_pcie_ep *ep, 479 enum pci_barno bar) 480 { 481 const struct pci_epc_features *epc_features; 482 483 if (!ep->ops->get_features) 484 return BAR_PROGRAMMABLE; 485 486 epc_features = ep->ops->get_features(ep); 487 488 return epc_features->bar[bar].type; 489 } 490 491 static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 492 struct pci_epf_bar *epf_bar) 493 { 494 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 495 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 496 struct dw_pcie_ep_func *ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 497 enum pci_barno bar = epf_bar->barno; 498 size_t size = epf_bar->size; 499 enum pci_epc_bar_type bar_type; 500 int flags = epf_bar->flags; 501 int ret, type; 502 503 if (!ep_func) 504 return -EINVAL; 505 506 /* 507 * DWC does not allow BAR pairs to overlap, e.g. you cannot combine BARs 508 * 1 and 2 to form a 64-bit BAR. 509 */ 510 if ((flags & PCI_BASE_ADDRESS_MEM_TYPE_64) && (bar & 1)) 511 return -EINVAL; 512 513 /* 514 * Certain EPF drivers dynamically change the physical address of a BAR 515 * (i.e. they call set_bar() twice, without ever calling clear_bar(), as 516 * calling clear_bar() would clear the BAR's PCI address assigned by the 517 * host). 518 */ 519 if (ep_func->epf_bar[bar]) { 520 /* 521 * We can only dynamically change a BAR if the new BAR size and 522 * BAR flags do not differ from the existing configuration. 523 * 524 * Note: this safety check only works when the caller uses 525 * a new struct pci_epf_bar in the second set_bar() call. 526 * If the same instance is updated in place and passed in, 527 * we cannot reliably detect invalid barno/size/flags 528 * changes here. 529 */ 530 if (ep_func->epf_bar[bar]->barno != bar || 531 ep_func->epf_bar[bar]->size != size || 532 ep_func->epf_bar[bar]->flags != flags) 533 return -EINVAL; 534 535 /* 536 * When dynamically changing a BAR, tear down any existing 537 * mappings before re-programming. This is redundant when 538 * both the old and new mappings are BAR Match Mode, but 539 * required to handle in-place updates and match-mode 540 * changes reliably. 541 */ 542 dw_pcie_ep_clear_ib_maps(ep, func_no, bar); 543 544 /* 545 * When dynamically changing a BAR, skip writing the BAR reg, as 546 * that would clear the BAR's PCI address assigned by the host. 547 */ 548 goto config_atu; 549 } else { 550 /* 551 * Subrange mapping is an update-only operation. The BAR 552 * must have been configured once without submaps so that 553 * subsequent set_bar() calls can update inbound mappings 554 * without touching the BAR register (and clobbering the 555 * host-assigned address). 556 */ 557 if (epf_bar->num_submap) 558 return -EINVAL; 559 } 560 561 bar_type = dw_pcie_ep_get_bar_type(ep, bar); 562 switch (bar_type) { 563 case BAR_FIXED: 564 /* 565 * There is no need to write a BAR mask for a fixed BAR (except 566 * to write 1 to the LSB of the BAR mask register, to enable the 567 * BAR). Write the BAR mask regardless. (The fixed bits in the 568 * BAR mask register will be read-only anyway.) 569 */ 570 fallthrough; 571 case BAR_PROGRAMMABLE: 572 ret = dw_pcie_ep_set_bar_programmable(ep, func_no, epf_bar); 573 break; 574 case BAR_RESIZABLE: 575 ret = dw_pcie_ep_set_bar_resizable(ep, func_no, epf_bar); 576 break; 577 default: 578 ret = -EINVAL; 579 dev_err(pci->dev, "Invalid BAR type\n"); 580 break; 581 } 582 583 if (ret) 584 return ret; 585 586 config_atu: 587 if (!(flags & PCI_BASE_ADDRESS_SPACE)) 588 type = PCIE_TLP_TYPE_MEM_RDWR; 589 else 590 type = PCIE_TLP_TYPE_IO_RDWR; 591 592 if (epf_bar->num_submap) 593 ret = dw_pcie_ep_ib_atu_addr(ep, func_no, type, epf_bar); 594 else 595 ret = dw_pcie_ep_ib_atu_bar(ep, func_no, type, 596 epf_bar->phys_addr, bar, size); 597 598 if (ret) 599 return ret; 600 601 ep_func->epf_bar[bar] = epf_bar; 602 603 return 0; 604 } 605 606 static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr, 607 u32 *atu_index) 608 { 609 u32 index; 610 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 611 612 for_each_set_bit(index, ep->ob_window_map, pci->num_ob_windows) { 613 if (ep->outbound_addr[index] != addr) 614 continue; 615 *atu_index = index; 616 return 0; 617 } 618 619 return -EINVAL; 620 } 621 622 static u64 dw_pcie_ep_align_addr(struct pci_epc *epc, u64 pci_addr, 623 size_t *pci_size, size_t *offset) 624 { 625 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 626 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 627 u64 mask = pci->region_align - 1; 628 size_t ofst = pci_addr & mask; 629 630 *pci_size = ALIGN(ofst + *pci_size, epc->mem->window.page_size); 631 *offset = ofst; 632 633 return pci_addr & ~mask; 634 } 635 636 static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 637 phys_addr_t addr) 638 { 639 int ret; 640 u32 atu_index; 641 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 642 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 643 644 ret = dw_pcie_find_index(ep, addr - pci->parent_bus_offset, 645 &atu_index); 646 if (ret < 0) 647 return; 648 649 ep->outbound_addr[atu_index] = 0; 650 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, atu_index); 651 clear_bit(atu_index, ep->ob_window_map); 652 } 653 654 static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 655 phys_addr_t addr, u64 pci_addr, size_t size) 656 { 657 int ret; 658 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 659 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 660 struct dw_pcie_ob_atu_cfg atu = { 0 }; 661 662 atu.func_no = func_no; 663 atu.type = PCIE_TLP_TYPE_MEM_RDWR; 664 atu.parent_bus_addr = addr - pci->parent_bus_offset; 665 atu.pci_addr = pci_addr; 666 atu.size = size; 667 ret = dw_pcie_ep_outbound_atu(ep, &atu); 668 if (ret) { 669 dev_err(pci->dev, "Failed to enable address\n"); 670 return ret; 671 } 672 673 return 0; 674 } 675 676 static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no) 677 { 678 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 679 struct dw_pcie_ep_func *ep_func; 680 u32 val, reg; 681 682 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 683 if (!ep_func || !ep_func->msi_cap) 684 return -EINVAL; 685 686 reg = ep_func->msi_cap + PCI_MSI_FLAGS; 687 val = dw_pcie_ep_readw_dbi(ep, func_no, reg); 688 if (!(val & PCI_MSI_FLAGS_ENABLE)) 689 return -EINVAL; 690 691 val = FIELD_GET(PCI_MSI_FLAGS_QSIZE, val); 692 693 return 1 << val; 694 } 695 696 static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 697 u8 nr_irqs) 698 { 699 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 700 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 701 struct dw_pcie_ep_func *ep_func; 702 u8 mmc = order_base_2(nr_irqs); 703 u32 val, reg; 704 705 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 706 if (!ep_func || !ep_func->msi_cap) 707 return -EINVAL; 708 709 reg = ep_func->msi_cap + PCI_MSI_FLAGS; 710 val = dw_pcie_ep_readw_dbi(ep, func_no, reg); 711 FIELD_MODIFY(PCI_MSI_FLAGS_QMASK, &val, mmc); 712 dw_pcie_dbi_ro_wr_en(pci); 713 dw_pcie_ep_writew_dbi(ep, func_no, reg, val); 714 dw_pcie_dbi_ro_wr_dis(pci); 715 716 return 0; 717 } 718 719 static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no) 720 { 721 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 722 struct dw_pcie_ep_func *ep_func; 723 u32 val, reg; 724 725 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 726 if (!ep_func || !ep_func->msix_cap) 727 return -EINVAL; 728 729 reg = ep_func->msix_cap + PCI_MSIX_FLAGS; 730 val = dw_pcie_ep_readw_dbi(ep, func_no, reg); 731 if (!(val & PCI_MSIX_FLAGS_ENABLE)) 732 return -EINVAL; 733 734 val &= PCI_MSIX_FLAGS_QSIZE; 735 736 return val + 1; 737 } 738 739 static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 740 u16 nr_irqs, enum pci_barno bir, u32 offset) 741 { 742 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 743 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 744 struct dw_pcie_ep_func *ep_func; 745 u32 val, reg; 746 747 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 748 if (!ep_func || !ep_func->msix_cap) 749 return -EINVAL; 750 751 dw_pcie_dbi_ro_wr_en(pci); 752 753 reg = ep_func->msix_cap + PCI_MSIX_FLAGS; 754 val = dw_pcie_ep_readw_dbi(ep, func_no, reg); 755 val &= ~PCI_MSIX_FLAGS_QSIZE; 756 val |= nr_irqs - 1; /* encoded as N-1 */ 757 dw_pcie_ep_writew_dbi(ep, func_no, reg, val); 758 759 reg = ep_func->msix_cap + PCI_MSIX_TABLE; 760 val = offset | bir; 761 dw_pcie_ep_writel_dbi(ep, func_no, reg, val); 762 763 reg = ep_func->msix_cap + PCI_MSIX_PBA; 764 val = (offset + (nr_irqs * PCI_MSIX_ENTRY_SIZE)) | bir; 765 dw_pcie_ep_writel_dbi(ep, func_no, reg, val); 766 767 dw_pcie_dbi_ro_wr_dis(pci); 768 769 return 0; 770 } 771 772 static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 773 unsigned int type, u16 interrupt_num) 774 { 775 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 776 777 if (!ep->ops->raise_irq) 778 return -EINVAL; 779 780 return ep->ops->raise_irq(ep, func_no, type, interrupt_num); 781 } 782 783 static void dw_pcie_ep_stop(struct pci_epc *epc) 784 { 785 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 786 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 787 788 /* 789 * Tear down the dedicated outbound window used for MSI 790 * generation. This avoids leaking an iATU window across 791 * endpoint stop/start cycles. 792 */ 793 if (ep->msi_iatu_mapped) { 794 dw_pcie_ep_unmap_addr(epc, 0, 0, ep->msi_mem_phys); 795 ep->msi_iatu_mapped = false; 796 } 797 798 dw_pcie_stop_link(pci); 799 } 800 801 static int dw_pcie_ep_start(struct pci_epc *epc) 802 { 803 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 804 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 805 806 return dw_pcie_start_link(pci); 807 } 808 809 static const struct pci_epc_features* 810 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no) 811 { 812 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 813 814 if (!ep->ops->get_features) 815 return NULL; 816 817 return ep->ops->get_features(ep); 818 } 819 820 static const struct pci_epc_bar_rsvd_region * 821 dw_pcie_ep_find_bar_rsvd_region(struct dw_pcie_ep *ep, 822 enum pci_epc_bar_rsvd_region_type type, 823 enum pci_barno *bar, 824 resource_size_t *bar_offset) 825 { 826 const struct pci_epc_features *features; 827 const struct pci_epc_bar_desc *bar_desc; 828 const struct pci_epc_bar_rsvd_region *r; 829 int i, j; 830 831 if (!ep->ops->get_features) 832 return NULL; 833 834 features = ep->ops->get_features(ep); 835 if (!features) 836 return NULL; 837 838 for (i = BAR_0; i <= BAR_5; i++) { 839 bar_desc = &features->bar[i]; 840 841 if (!bar_desc->nr_rsvd_regions || !bar_desc->rsvd_regions) 842 continue; 843 844 for (j = 0; j < bar_desc->nr_rsvd_regions; j++) { 845 r = &bar_desc->rsvd_regions[j]; 846 847 if (r->type != type) 848 continue; 849 850 if (bar) 851 *bar = i; 852 if (bar_offset) 853 *bar_offset = r->offset; 854 return r; 855 } 856 } 857 858 return NULL; 859 } 860 861 static int 862 dw_pcie_ep_get_aux_resources_count(struct pci_epc *epc, u8 func_no, 863 u8 vfunc_no) 864 { 865 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 866 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 867 struct dw_edma_chip *edma = &pci->edma; 868 869 if (!pci->edma_reg_size) 870 return 0; 871 872 if (edma->db_offset == ~0) 873 return 0; 874 875 return 1; 876 } 877 878 static int 879 dw_pcie_ep_get_aux_resources(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 880 struct pci_epc_aux_resource *resources, 881 int num_resources) 882 { 883 struct dw_pcie_ep *ep = epc_get_drvdata(epc); 884 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 885 const struct pci_epc_bar_rsvd_region *rsvd; 886 struct dw_edma_chip *edma = &pci->edma; 887 enum pci_barno dma_ctrl_bar = NO_BAR; 888 resource_size_t db_offset = edma->db_offset; 889 resource_size_t dma_ctrl_bar_offset = 0; 890 resource_size_t dma_reg_size; 891 int count; 892 893 count = dw_pcie_ep_get_aux_resources_count(epc, func_no, vfunc_no); 894 if (count < 0) 895 return count; 896 897 if (num_resources < count) 898 return -ENOSPC; 899 900 if (!count) 901 return 0; 902 903 dma_reg_size = pci->edma_reg_size; 904 905 rsvd = dw_pcie_ep_find_bar_rsvd_region(ep, 906 PCI_EPC_BAR_RSVD_DMA_CTRL_MMIO, 907 &dma_ctrl_bar, 908 &dma_ctrl_bar_offset); 909 if (rsvd && rsvd->size < dma_reg_size) 910 dma_reg_size = rsvd->size; 911 912 /* 913 * For interrupt-emulation doorbells, report a standalone resource 914 * instead of bundling it into the DMA controller MMIO resource. 915 */ 916 if (range_end_overflows_t(resource_size_t, db_offset, 917 sizeof(u32), dma_reg_size)) 918 return -EINVAL; 919 920 resources[0] = (struct pci_epc_aux_resource) { 921 .type = PCI_EPC_AUX_DOORBELL_MMIO, 922 .phys_addr = pci->edma_reg_phys + db_offset, 923 .size = sizeof(u32), 924 .bar = dma_ctrl_bar, 925 .bar_offset = dma_ctrl_bar != NO_BAR ? 926 dma_ctrl_bar_offset + db_offset : 0, 927 .u.db_mmio = { 928 .irq = edma->db_irq, 929 .data = 0, /* write 0 to assert */ 930 }, 931 }; 932 933 return 0; 934 } 935 936 static const struct pci_epc_ops epc_ops = { 937 .write_header = dw_pcie_ep_write_header, 938 .set_bar = dw_pcie_ep_set_bar, 939 .clear_bar = dw_pcie_ep_clear_bar, 940 .align_addr = dw_pcie_ep_align_addr, 941 .map_addr = dw_pcie_ep_map_addr, 942 .unmap_addr = dw_pcie_ep_unmap_addr, 943 .set_msi = dw_pcie_ep_set_msi, 944 .get_msi = dw_pcie_ep_get_msi, 945 .set_msix = dw_pcie_ep_set_msix, 946 .get_msix = dw_pcie_ep_get_msix, 947 .raise_irq = dw_pcie_ep_raise_irq, 948 .start = dw_pcie_ep_start, 949 .stop = dw_pcie_ep_stop, 950 .get_features = dw_pcie_ep_get_features, 951 .get_aux_resources_count = dw_pcie_ep_get_aux_resources_count, 952 .get_aux_resources = dw_pcie_ep_get_aux_resources, 953 }; 954 955 /** 956 * dw_pcie_ep_raise_intx_irq - Raise INTx IRQ to the host 957 * @ep: DWC EP device 958 * @func_no: Function number of the endpoint 959 * 960 * Return: 0 if success, errno otherwise. 961 */ 962 int dw_pcie_ep_raise_intx_irq(struct dw_pcie_ep *ep, u8 func_no) 963 { 964 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 965 struct device *dev = pci->dev; 966 967 dev_err(dev, "EP cannot raise INTX IRQs\n"); 968 969 return -EINVAL; 970 } 971 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_intx_irq); 972 973 /** 974 * dw_pcie_ep_raise_msi_irq - Raise MSI IRQ to the host 975 * @ep: DWC EP device 976 * @func_no: Function number of the endpoint 977 * @interrupt_num: Interrupt number to be raised 978 * 979 * Return: 0 if success, errno otherwise. 980 */ 981 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no, 982 u8 interrupt_num) 983 { 984 u32 msg_addr_lower, msg_addr_upper, reg; 985 struct dw_pcie_ep_func *ep_func; 986 struct pci_epc *epc = ep->epc; 987 size_t map_size = sizeof(u32); 988 size_t offset; 989 u16 msg_ctrl, msg_data; 990 bool has_upper; 991 u64 msg_addr; 992 int ret; 993 994 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 995 if (!ep_func || !ep_func->msi_cap) 996 return -EINVAL; 997 998 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */ 999 reg = ep_func->msi_cap + PCI_MSI_FLAGS; 1000 msg_ctrl = dw_pcie_ep_readw_dbi(ep, func_no, reg); 1001 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT); 1002 reg = ep_func->msi_cap + PCI_MSI_ADDRESS_LO; 1003 msg_addr_lower = dw_pcie_ep_readl_dbi(ep, func_no, reg); 1004 if (has_upper) { 1005 reg = ep_func->msi_cap + PCI_MSI_ADDRESS_HI; 1006 msg_addr_upper = dw_pcie_ep_readl_dbi(ep, func_no, reg); 1007 reg = ep_func->msi_cap + PCI_MSI_DATA_64; 1008 msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg); 1009 } else { 1010 msg_addr_upper = 0; 1011 reg = ep_func->msi_cap + PCI_MSI_DATA_32; 1012 msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg); 1013 } 1014 msg_addr = ((u64)msg_addr_upper) << 32 | msg_addr_lower; 1015 1016 msg_addr = dw_pcie_ep_align_addr(epc, msg_addr, &map_size, &offset); 1017 1018 /* 1019 * Program the outbound iATU once and keep it enabled. 1020 * 1021 * The spec warns that updating iATU registers while there are 1022 * operations in flight on the AXI bridge interface is not 1023 * supported, so we avoid reprogramming the region on every MSI, 1024 * specifically unmapping immediately after writel(). 1025 */ 1026 if (ep->msi_iatu_mapped && (ep->msi_msg_addr != msg_addr || 1027 ep->msi_map_size != map_size)) { 1028 /* 1029 * The host changed the MSI target address or the required 1030 * mapping size changed. Reprogramming the iATU when there are 1031 * operations in flight is unsafe on this controller. However, 1032 * there is no unified way to check if we have operations in 1033 * flight, thus we don't know if we should WARN() or not. 1034 */ 1035 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys); 1036 ep->msi_iatu_mapped = false; 1037 } 1038 1039 if (!ep->msi_iatu_mapped) { 1040 ret = dw_pcie_ep_map_addr(epc, func_no, 0, 1041 ep->msi_mem_phys, msg_addr, 1042 map_size); 1043 if (ret) 1044 return ret; 1045 1046 ep->msi_iatu_mapped = true; 1047 ep->msi_msg_addr = msg_addr; 1048 ep->msi_map_size = map_size; 1049 } 1050 1051 writel(msg_data | (interrupt_num - 1), ep->msi_mem + offset); 1052 1053 return 0; 1054 } 1055 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq); 1056 1057 /** 1058 * dw_pcie_ep_raise_msix_irq_doorbell - Raise MSI-X to the host using Doorbell 1059 * method 1060 * @ep: DWC EP device 1061 * @func_no: Function number of the endpoint device 1062 * @interrupt_num: Interrupt number to be raised 1063 * 1064 * Return: 0 if success, errno otherwise. 1065 */ 1066 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no, 1067 u16 interrupt_num) 1068 { 1069 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1070 struct dw_pcie_ep_func *ep_func; 1071 u32 msg_data; 1072 1073 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 1074 if (!ep_func || !ep_func->msix_cap) 1075 return -EINVAL; 1076 1077 msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) | 1078 (interrupt_num - 1); 1079 1080 dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data); 1081 1082 return 0; 1083 } 1084 1085 /** 1086 * dw_pcie_ep_raise_msix_irq - Raise MSI-X to the host 1087 * @ep: DWC EP device 1088 * @func_no: Function number of the endpoint device 1089 * @interrupt_num: Interrupt number to be raised 1090 * 1091 * Return: 0 if success, errno otherwise. 1092 */ 1093 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no, 1094 u16 interrupt_num) 1095 { 1096 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1097 struct pci_epf_msix_tbl *msix_tbl; 1098 struct dw_pcie_ep_func *ep_func; 1099 struct pci_epc *epc = ep->epc; 1100 size_t map_size = sizeof(u32); 1101 size_t offset; 1102 u32 reg, msg_data, vec_ctrl; 1103 u32 tbl_offset; 1104 u64 msg_addr; 1105 int ret; 1106 u8 bir; 1107 1108 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 1109 if (!ep_func || !ep_func->msix_cap) 1110 return -EINVAL; 1111 1112 reg = ep_func->msix_cap + PCI_MSIX_TABLE; 1113 tbl_offset = dw_pcie_ep_readl_dbi(ep, func_no, reg); 1114 bir = FIELD_GET(PCI_MSIX_TABLE_BIR, tbl_offset); 1115 tbl_offset &= PCI_MSIX_TABLE_OFFSET; 1116 1117 msix_tbl = ep_func->epf_bar[bir]->addr + tbl_offset; 1118 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr; 1119 msg_data = msix_tbl[(interrupt_num - 1)].msg_data; 1120 vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl; 1121 1122 if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) { 1123 dev_dbg(pci->dev, "MSI-X entry ctrl set\n"); 1124 return -EPERM; 1125 } 1126 1127 msg_addr = dw_pcie_ep_align_addr(epc, msg_addr, &map_size, &offset); 1128 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr, 1129 map_size); 1130 if (ret) 1131 return ret; 1132 1133 writel(msg_data, ep->msi_mem + offset); 1134 1135 /* flush posted write before unmap */ 1136 readl(ep->msi_mem + offset); 1137 1138 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys); 1139 1140 return 0; 1141 } 1142 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msix_irq); 1143 1144 /** 1145 * dw_pcie_ep_cleanup - Cleanup DWC EP resources after fundamental reset 1146 * @ep: DWC EP device 1147 * 1148 * Cleans up the DWC EP specific resources like eDMA etc... after fundamental 1149 * reset like PERST#. Note that this API is only applicable for drivers 1150 * supporting PERST# or any other methods of fundamental reset. 1151 */ 1152 void dw_pcie_ep_cleanup(struct dw_pcie_ep *ep) 1153 { 1154 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1155 1156 dwc_pcie_debugfs_deinit(pci); 1157 dw_pcie_edma_remove(pci); 1158 } 1159 EXPORT_SYMBOL_GPL(dw_pcie_ep_cleanup); 1160 1161 /** 1162 * dw_pcie_ep_deinit - Deinitialize the endpoint device 1163 * @ep: DWC EP device 1164 * 1165 * Deinitialize the endpoint device. EPC device is not destroyed since that will 1166 * be taken care by Devres. 1167 */ 1168 void dw_pcie_ep_deinit(struct dw_pcie_ep *ep) 1169 { 1170 struct pci_epc *epc = ep->epc; 1171 1172 dw_pcie_ep_cleanup(ep); 1173 1174 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem, 1175 epc->mem->window.page_size); 1176 1177 pci_epc_mem_exit(epc); 1178 } 1179 EXPORT_SYMBOL_GPL(dw_pcie_ep_deinit); 1180 1181 static void dw_pcie_ep_init_rebar_registers(struct dw_pcie_ep *ep, u8 func_no) 1182 { 1183 struct dw_pcie_ep_func *ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 1184 unsigned int offset, nbars; 1185 enum pci_barno bar; 1186 u32 reg, i, val; 1187 1188 if (!ep_func) 1189 return; 1190 1191 offset = dw_pcie_ep_find_ext_capability(ep, func_no, PCI_EXT_CAP_ID_REBAR); 1192 1193 if (offset) { 1194 reg = dw_pcie_ep_readl_dbi(ep, func_no, offset + PCI_REBAR_CTRL); 1195 nbars = FIELD_GET(PCI_REBAR_CTRL_NBAR_MASK, reg); 1196 1197 /* 1198 * PCIe r6.0, sec 7.8.6.2 require us to support at least one 1199 * size in the range from 1 MB to 512 GB. Advertise support 1200 * for 1 MB BAR size only. 1201 * 1202 * For a BAR that has been configured via dw_pcie_ep_set_bar(), 1203 * advertise support for only that size instead. 1204 */ 1205 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL) { 1206 /* 1207 * While the RESBAR_CAP_REG_* fields are sticky, the 1208 * RESBAR_CTRL_REG_BAR_SIZE field is non-sticky (it is 1209 * sticky in certain versions of DWC PCIe, but not all). 1210 * 1211 * RESBAR_CTRL_REG_BAR_SIZE is updated automatically by 1212 * the controller when RESBAR_CAP_REG is written, which 1213 * is why RESBAR_CAP_REG is written here. 1214 */ 1215 val = dw_pcie_ep_readl_dbi(ep, func_no, offset + PCI_REBAR_CTRL); 1216 bar = FIELD_GET(PCI_REBAR_CTRL_BAR_IDX, val); 1217 if (ep_func->epf_bar[bar]) 1218 pci_epc_bar_size_to_rebar_cap(ep_func->epf_bar[bar]->size, &val); 1219 else 1220 val = BIT(4); 1221 1222 dw_pcie_ep_writel_dbi(ep, func_no, offset + PCI_REBAR_CAP, val); 1223 } 1224 } 1225 } 1226 1227 static void dw_pcie_ep_init_non_sticky_registers(struct dw_pcie *pci) 1228 { 1229 struct dw_pcie_ep *ep = &pci->ep; 1230 u8 funcs = ep->epc->max_functions; 1231 u32 func0_lnkcap, lnkcap; 1232 u8 func_no, offset; 1233 1234 dw_pcie_dbi_ro_wr_en(pci); 1235 1236 for (func_no = 0; func_no < funcs; func_no++) 1237 dw_pcie_ep_init_rebar_registers(ep, func_no); 1238 1239 dw_pcie_setup(pci); 1240 1241 /* 1242 * PCIe r7.0, section 7.5.3.6 states that for multi-function 1243 * endpoints, max link width and speed fields must report same 1244 * values for all functions. However, dw_pcie_setup() programs 1245 * these fields only for function 0. Hence, mirror these fields 1246 * to all other functions as well. 1247 */ 1248 if (funcs > 1) { 1249 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 1250 func0_lnkcap = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP); 1251 func0_lnkcap = FIELD_GET(PCI_EXP_LNKCAP_MLW | 1252 PCI_EXP_LNKCAP_SLS, func0_lnkcap); 1253 1254 for (func_no = 1; func_no < funcs; func_no++) { 1255 offset = dw_pcie_ep_find_capability(ep, func_no, 1256 PCI_CAP_ID_EXP); 1257 lnkcap = dw_pcie_ep_readl_dbi(ep, func_no, 1258 offset + PCI_EXP_LNKCAP); 1259 FIELD_MODIFY(PCI_EXP_LNKCAP_MLW | PCI_EXP_LNKCAP_SLS, 1260 &lnkcap, func0_lnkcap); 1261 dw_pcie_ep_writel_dbi(ep, func_no, 1262 offset + PCI_EXP_LNKCAP, lnkcap); 1263 } 1264 } 1265 1266 dw_pcie_dbi_ro_wr_dis(pci); 1267 } 1268 1269 static void dw_pcie_ep_disable_bars(struct dw_pcie_ep *ep) 1270 { 1271 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1272 enum pci_epc_bar_type bar_type; 1273 enum pci_barno bar; 1274 1275 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { 1276 bar_type = dw_pcie_ep_get_bar_type(ep, bar); 1277 1278 /* 1279 * Reserved BARs should not get disabled by default. All other 1280 * BAR types are disabled by default. 1281 * 1282 * This is in line with the current EPC core design, where all 1283 * BARs are disabled by default, and then the EPF driver enables 1284 * the BARs it wishes to use. 1285 */ 1286 if (bar_type != BAR_RESERVED) 1287 dw_pcie_ep_reset_bar(pci, bar); 1288 } 1289 } 1290 1291 /** 1292 * dw_pcie_ep_init_registers - Initialize DWC EP specific registers 1293 * @ep: DWC EP device 1294 * 1295 * Initialize the registers (CSRs) specific to DWC EP. This API should be called 1296 * only when the endpoint receives an active refclk (either from host or 1297 * generated locally). 1298 */ 1299 int dw_pcie_ep_init_registers(struct dw_pcie_ep *ep) 1300 { 1301 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1302 struct dw_pcie_ep_func *ep_func; 1303 struct device *dev = pci->dev; 1304 struct pci_epc *epc = ep->epc; 1305 u32 ptm_cap_base, reg; 1306 u8 hdr_type; 1307 u8 func_no; 1308 void *addr; 1309 int ret; 1310 1311 hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) & 1312 PCI_HEADER_TYPE_MASK; 1313 if (hdr_type != PCI_HEADER_TYPE_NORMAL) { 1314 dev_err(pci->dev, 1315 "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n", 1316 hdr_type); 1317 return -EIO; 1318 } 1319 1320 dw_pcie_version_detect(pci); 1321 1322 dw_pcie_iatu_detect(pci); 1323 1324 ret = dw_pcie_edma_detect(pci); 1325 if (ret) 1326 return ret; 1327 1328 ret = -ENOMEM; 1329 if (!ep->ib_window_map) { 1330 ep->ib_window_map = devm_bitmap_zalloc(dev, pci->num_ib_windows, 1331 GFP_KERNEL); 1332 if (!ep->ib_window_map) 1333 goto err_remove_edma; 1334 } 1335 1336 if (!ep->ob_window_map) { 1337 ep->ob_window_map = devm_bitmap_zalloc(dev, pci->num_ob_windows, 1338 GFP_KERNEL); 1339 if (!ep->ob_window_map) 1340 goto err_remove_edma; 1341 } 1342 1343 if (!ep->outbound_addr) { 1344 addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t), 1345 GFP_KERNEL); 1346 if (!addr) 1347 goto err_remove_edma; 1348 ep->outbound_addr = addr; 1349 } 1350 1351 for (func_no = 0; func_no < epc->max_functions; func_no++) { 1352 1353 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no); 1354 if (ep_func) 1355 continue; 1356 1357 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL); 1358 if (!ep_func) 1359 goto err_remove_edma; 1360 1361 ep_func->func_no = func_no; 1362 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no, 1363 PCI_CAP_ID_MSI); 1364 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no, 1365 PCI_CAP_ID_MSIX); 1366 1367 list_add_tail(&ep_func->list, &ep->func_list); 1368 } 1369 1370 if (ep->ops->init) 1371 ep->ops->init(ep); 1372 1373 dw_pcie_ep_disable_bars(ep); 1374 1375 /* 1376 * PCIe r6.0, section 7.9.15 states that for endpoints that support 1377 * PTM, this capability structure is required in exactly one 1378 * function, which controls the PTM behavior of all PTM capable 1379 * functions. This indicates the PTM capability structure 1380 * represents controller-level registers rather than per-function 1381 * registers. 1382 * 1383 * Therefore, PTM capability registers are configured using the 1384 * standard DBI accessors, instead of func_no indexed per-function 1385 * accessors. 1386 */ 1387 ptm_cap_base = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM); 1388 1389 /* 1390 * PTM responder capability can be disabled only after disabling 1391 * PTM root capability. 1392 */ 1393 if (ptm_cap_base) { 1394 dw_pcie_dbi_ro_wr_en(pci); 1395 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP); 1396 reg &= ~PCI_PTM_CAP_ROOT; 1397 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg); 1398 1399 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP); 1400 reg &= ~(PCI_PTM_CAP_RES | PCI_PTM_GRANULARITY_MASK); 1401 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg); 1402 dw_pcie_dbi_ro_wr_dis(pci); 1403 } 1404 1405 dw_pcie_ep_init_non_sticky_registers(pci); 1406 1407 dwc_pcie_debugfs_init(pci, DW_PCIE_EP_TYPE); 1408 1409 return 0; 1410 1411 err_remove_edma: 1412 dw_pcie_edma_remove(pci); 1413 1414 return ret; 1415 } 1416 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_registers); 1417 1418 /** 1419 * dw_pcie_ep_linkup - Notify EPF drivers about Link Up event 1420 * @ep: DWC EP device 1421 */ 1422 void dw_pcie_ep_linkup(struct dw_pcie_ep *ep) 1423 { 1424 struct pci_epc *epc = ep->epc; 1425 1426 pci_epc_linkup(epc); 1427 } 1428 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup); 1429 1430 /** 1431 * dw_pcie_ep_linkdown - Notify EPF drivers about Link Down event 1432 * @ep: DWC EP device 1433 * 1434 * Non-sticky registers are also initialized before sending the notification to 1435 * the EPF drivers. This is needed since the registers need to be initialized 1436 * before the link comes back again. 1437 */ 1438 void dw_pcie_ep_linkdown(struct dw_pcie_ep *ep) 1439 { 1440 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1441 struct pci_epc *epc = ep->epc; 1442 1443 /* 1444 * Initialize the non-sticky DWC registers as they would've reset post 1445 * Link Down. This is specifically needed for drivers not supporting 1446 * PERST# as they have no way to reinitialize the registers before the 1447 * link comes back again. 1448 */ 1449 dw_pcie_ep_init_non_sticky_registers(pci); 1450 1451 pci_epc_linkdown(epc); 1452 } 1453 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkdown); 1454 1455 static int dw_pcie_ep_get_resources(struct dw_pcie_ep *ep) 1456 { 1457 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1458 struct device *dev = pci->dev; 1459 struct platform_device *pdev = to_platform_device(dev); 1460 struct device_node *np = dev->of_node; 1461 struct pci_epc *epc = ep->epc; 1462 struct resource *res; 1463 int ret; 1464 1465 ret = dw_pcie_get_resources(pci); 1466 if (ret) 1467 return ret; 1468 1469 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space"); 1470 if (!res) 1471 return -EINVAL; 1472 1473 ep->phys_base = res->start; 1474 ep->addr_size = resource_size(res); 1475 1476 /* 1477 * artpec6_pcie_cpu_addr_fixup() uses ep->phys_base, so call 1478 * dw_pcie_parent_bus_offset() after setting ep->phys_base. 1479 */ 1480 pci->parent_bus_offset = dw_pcie_parent_bus_offset(pci, "addr_space", 1481 ep->phys_base); 1482 1483 ret = of_property_read_u8(np, "max-functions", &epc->max_functions); 1484 if (ret < 0) 1485 epc->max_functions = 1; 1486 1487 return 0; 1488 } 1489 1490 /** 1491 * dw_pcie_ep_init - Initialize the endpoint device 1492 * @ep: DWC EP device 1493 * 1494 * Initialize the endpoint device. Allocate resources and create the EPC 1495 * device with the endpoint framework. 1496 * 1497 * Return: 0 if success, errno otherwise. 1498 */ 1499 int dw_pcie_ep_init(struct dw_pcie_ep *ep) 1500 { 1501 int ret; 1502 struct pci_epc *epc; 1503 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1504 struct device *dev = pci->dev; 1505 1506 INIT_LIST_HEAD(&ep->func_list); 1507 ep->msi_iatu_mapped = false; 1508 ep->msi_msg_addr = 0; 1509 ep->msi_map_size = 0; 1510 1511 epc = devm_pci_epc_create(dev, &epc_ops); 1512 if (IS_ERR(epc)) { 1513 dev_err(dev, "Failed to create epc device\n"); 1514 return PTR_ERR(epc); 1515 } 1516 1517 ep->epc = epc; 1518 epc_set_drvdata(epc, ep); 1519 1520 ret = dw_pcie_ep_get_resources(ep); 1521 if (ret) 1522 return ret; 1523 1524 if (ep->ops->pre_init) 1525 ep->ops->pre_init(ep); 1526 1527 ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size, 1528 ep->page_size); 1529 if (ret < 0) { 1530 dev_err(dev, "Failed to initialize address space\n"); 1531 return ret; 1532 } 1533 1534 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys, 1535 epc->mem->window.page_size); 1536 if (!ep->msi_mem) { 1537 ret = -ENOMEM; 1538 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n"); 1539 goto err_exit_epc_mem; 1540 } 1541 1542 return 0; 1543 1544 err_exit_epc_mem: 1545 pci_epc_mem_exit(epc); 1546 1547 return ret; 1548 } 1549 EXPORT_SYMBOL_GPL(dw_pcie_ep_init); 1550