0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/of.h>
0010 #include <linux/platform_device.h>
0011
0012 #include "pcie-designware.h"
0013 #include <linux/pci-epc.h>
0014 #include <linux/pci-epf.h>
0015
0016 #include "../../pci.h"
0017
0018 void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
0019 {
0020 struct pci_epc *epc = ep->epc;
0021
0022 pci_epc_linkup(epc);
0023 }
0024 EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
0025
0026 void dw_pcie_ep_init_notify(struct dw_pcie_ep *ep)
0027 {
0028 struct pci_epc *epc = ep->epc;
0029
0030 pci_epc_init_notify(epc);
0031 }
0032 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_notify);
0033
0034 struct dw_pcie_ep_func *
0035 dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
0036 {
0037 struct dw_pcie_ep_func *ep_func;
0038
0039 list_for_each_entry(ep_func, &ep->func_list, list) {
0040 if (ep_func->func_no == func_no)
0041 return ep_func;
0042 }
0043
0044 return NULL;
0045 }
0046
0047 static unsigned int dw_pcie_ep_func_select(struct dw_pcie_ep *ep, u8 func_no)
0048 {
0049 unsigned int func_offset = 0;
0050
0051 if (ep->ops->func_conf_select)
0052 func_offset = ep->ops->func_conf_select(ep, func_no);
0053
0054 return func_offset;
0055 }
0056
0057 static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
0058 enum pci_barno bar, int flags)
0059 {
0060 u32 reg;
0061 unsigned int func_offset = 0;
0062 struct dw_pcie_ep *ep = &pci->ep;
0063
0064 func_offset = dw_pcie_ep_func_select(ep, func_no);
0065
0066 reg = func_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
0067 dw_pcie_dbi_ro_wr_en(pci);
0068 dw_pcie_writel_dbi2(pci, reg, 0x0);
0069 dw_pcie_writel_dbi(pci, reg, 0x0);
0070 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
0071 dw_pcie_writel_dbi2(pci, reg + 4, 0x0);
0072 dw_pcie_writel_dbi(pci, reg + 4, 0x0);
0073 }
0074 dw_pcie_dbi_ro_wr_dis(pci);
0075 }
0076
0077 void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
0078 {
0079 u8 func_no, funcs;
0080
0081 funcs = pci->ep.epc->max_functions;
0082
0083 for (func_no = 0; func_no < funcs; func_no++)
0084 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
0085 }
0086 EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar);
0087
0088 static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
0089 u8 cap_ptr, u8 cap)
0090 {
0091 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0092 unsigned int func_offset = 0;
0093 u8 cap_id, next_cap_ptr;
0094 u16 reg;
0095
0096 if (!cap_ptr)
0097 return 0;
0098
0099 func_offset = dw_pcie_ep_func_select(ep, func_no);
0100
0101 reg = dw_pcie_readw_dbi(pci, func_offset + cap_ptr);
0102 cap_id = (reg & 0x00ff);
0103
0104 if (cap_id > PCI_CAP_ID_MAX)
0105 return 0;
0106
0107 if (cap_id == cap)
0108 return cap_ptr;
0109
0110 next_cap_ptr = (reg & 0xff00) >> 8;
0111 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
0112 }
0113
0114 static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
0115 {
0116 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0117 unsigned int func_offset = 0;
0118 u8 next_cap_ptr;
0119 u16 reg;
0120
0121 func_offset = dw_pcie_ep_func_select(ep, func_no);
0122
0123 reg = dw_pcie_readw_dbi(pci, func_offset + PCI_CAPABILITY_LIST);
0124 next_cap_ptr = (reg & 0x00ff);
0125
0126 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
0127 }
0128
0129 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0130 struct pci_epf_header *hdr)
0131 {
0132 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0133 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0134 unsigned int func_offset = 0;
0135
0136 func_offset = dw_pcie_ep_func_select(ep, func_no);
0137
0138 dw_pcie_dbi_ro_wr_en(pci);
0139 dw_pcie_writew_dbi(pci, func_offset + PCI_VENDOR_ID, hdr->vendorid);
0140 dw_pcie_writew_dbi(pci, func_offset + PCI_DEVICE_ID, hdr->deviceid);
0141 dw_pcie_writeb_dbi(pci, func_offset + PCI_REVISION_ID, hdr->revid);
0142 dw_pcie_writeb_dbi(pci, func_offset + PCI_CLASS_PROG, hdr->progif_code);
0143 dw_pcie_writew_dbi(pci, func_offset + PCI_CLASS_DEVICE,
0144 hdr->subclass_code | hdr->baseclass_code << 8);
0145 dw_pcie_writeb_dbi(pci, func_offset + PCI_CACHE_LINE_SIZE,
0146 hdr->cache_line_size);
0147 dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_VENDOR_ID,
0148 hdr->subsys_vendor_id);
0149 dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_ID, hdr->subsys_id);
0150 dw_pcie_writeb_dbi(pci, func_offset + PCI_INTERRUPT_PIN,
0151 hdr->interrupt_pin);
0152 dw_pcie_dbi_ro_wr_dis(pci);
0153
0154 return 0;
0155 }
0156
0157 static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no, int type,
0158 dma_addr_t cpu_addr, enum pci_barno bar)
0159 {
0160 int ret;
0161 u32 free_win;
0162 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0163
0164 if (!ep->bar_to_atu[bar])
0165 free_win = find_first_zero_bit(ep->ib_window_map, pci->num_ib_windows);
0166 else
0167 free_win = ep->bar_to_atu[bar];
0168
0169 if (free_win >= pci->num_ib_windows) {
0170 dev_err(pci->dev, "No free inbound window\n");
0171 return -EINVAL;
0172 }
0173
0174 ret = dw_pcie_prog_inbound_atu(pci, func_no, free_win, type,
0175 cpu_addr, bar);
0176 if (ret < 0) {
0177 dev_err(pci->dev, "Failed to program IB window\n");
0178 return ret;
0179 }
0180
0181 ep->bar_to_atu[bar] = free_win;
0182 set_bit(free_win, ep->ib_window_map);
0183
0184 return 0;
0185 }
0186
0187 static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
0188 phys_addr_t phys_addr,
0189 u64 pci_addr, size_t size)
0190 {
0191 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0192 u32 free_win;
0193 int ret;
0194
0195 free_win = find_first_zero_bit(ep->ob_window_map, pci->num_ob_windows);
0196 if (free_win >= pci->num_ob_windows) {
0197 dev_err(pci->dev, "No free outbound window\n");
0198 return -EINVAL;
0199 }
0200
0201 ret = dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
0202 phys_addr, pci_addr, size);
0203 if (ret)
0204 return ret;
0205
0206 set_bit(free_win, ep->ob_window_map);
0207 ep->outbound_addr[free_win] = phys_addr;
0208
0209 return 0;
0210 }
0211
0212 static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0213 struct pci_epf_bar *epf_bar)
0214 {
0215 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0216 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0217 enum pci_barno bar = epf_bar->barno;
0218 u32 atu_index = ep->bar_to_atu[bar];
0219
0220 __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
0221
0222 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, atu_index);
0223 clear_bit(atu_index, ep->ib_window_map);
0224 ep->epf_bar[bar] = NULL;
0225 ep->bar_to_atu[bar] = 0;
0226 }
0227
0228 static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0229 struct pci_epf_bar *epf_bar)
0230 {
0231 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0232 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0233 enum pci_barno bar = epf_bar->barno;
0234 size_t size = epf_bar->size;
0235 int flags = epf_bar->flags;
0236 unsigned int func_offset = 0;
0237 int ret, type;
0238 u32 reg;
0239
0240 func_offset = dw_pcie_ep_func_select(ep, func_no);
0241
0242 reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
0243
0244 if (!(flags & PCI_BASE_ADDRESS_SPACE))
0245 type = PCIE_ATU_TYPE_MEM;
0246 else
0247 type = PCIE_ATU_TYPE_IO;
0248
0249 ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar);
0250 if (ret)
0251 return ret;
0252
0253 if (ep->epf_bar[bar])
0254 return 0;
0255
0256 dw_pcie_dbi_ro_wr_en(pci);
0257
0258 dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
0259 dw_pcie_writel_dbi(pci, reg, flags);
0260
0261 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
0262 dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
0263 dw_pcie_writel_dbi(pci, reg + 4, 0);
0264 }
0265
0266 ep->epf_bar[bar] = epf_bar;
0267 dw_pcie_dbi_ro_wr_dis(pci);
0268
0269 return 0;
0270 }
0271
0272 static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
0273 u32 *atu_index)
0274 {
0275 u32 index;
0276 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0277
0278 for (index = 0; index < pci->num_ob_windows; index++) {
0279 if (ep->outbound_addr[index] != addr)
0280 continue;
0281 *atu_index = index;
0282 return 0;
0283 }
0284
0285 return -EINVAL;
0286 }
0287
0288 static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0289 phys_addr_t addr)
0290 {
0291 int ret;
0292 u32 atu_index;
0293 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0294 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0295
0296 ret = dw_pcie_find_index(ep, addr, &atu_index);
0297 if (ret < 0)
0298 return;
0299
0300 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, atu_index);
0301 clear_bit(atu_index, ep->ob_window_map);
0302 }
0303
0304 static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0305 phys_addr_t addr, u64 pci_addr, size_t size)
0306 {
0307 int ret;
0308 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0309 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0310
0311 ret = dw_pcie_ep_outbound_atu(ep, func_no, addr, pci_addr, size);
0312 if (ret) {
0313 dev_err(pci->dev, "Failed to enable address\n");
0314 return ret;
0315 }
0316
0317 return 0;
0318 }
0319
0320 static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
0321 {
0322 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0323 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0324 u32 val, reg;
0325 unsigned int func_offset = 0;
0326 struct dw_pcie_ep_func *ep_func;
0327
0328 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
0329 if (!ep_func || !ep_func->msi_cap)
0330 return -EINVAL;
0331
0332 func_offset = dw_pcie_ep_func_select(ep, func_no);
0333
0334 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
0335 val = dw_pcie_readw_dbi(pci, reg);
0336 if (!(val & PCI_MSI_FLAGS_ENABLE))
0337 return -EINVAL;
0338
0339 val = (val & PCI_MSI_FLAGS_QSIZE) >> 4;
0340
0341 return val;
0342 }
0343
0344 static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0345 u8 interrupts)
0346 {
0347 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0348 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0349 u32 val, reg;
0350 unsigned int func_offset = 0;
0351 struct dw_pcie_ep_func *ep_func;
0352
0353 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
0354 if (!ep_func || !ep_func->msi_cap)
0355 return -EINVAL;
0356
0357 func_offset = dw_pcie_ep_func_select(ep, func_no);
0358
0359 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
0360 val = dw_pcie_readw_dbi(pci, reg);
0361 val &= ~PCI_MSI_FLAGS_QMASK;
0362 val |= (interrupts << 1) & PCI_MSI_FLAGS_QMASK;
0363 dw_pcie_dbi_ro_wr_en(pci);
0364 dw_pcie_writew_dbi(pci, reg, val);
0365 dw_pcie_dbi_ro_wr_dis(pci);
0366
0367 return 0;
0368 }
0369
0370 static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
0371 {
0372 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0373 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0374 u32 val, reg;
0375 unsigned int func_offset = 0;
0376 struct dw_pcie_ep_func *ep_func;
0377
0378 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
0379 if (!ep_func || !ep_func->msix_cap)
0380 return -EINVAL;
0381
0382 func_offset = dw_pcie_ep_func_select(ep, func_no);
0383
0384 reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
0385 val = dw_pcie_readw_dbi(pci, reg);
0386 if (!(val & PCI_MSIX_FLAGS_ENABLE))
0387 return -EINVAL;
0388
0389 val &= PCI_MSIX_FLAGS_QSIZE;
0390
0391 return val;
0392 }
0393
0394 static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0395 u16 interrupts, enum pci_barno bir, u32 offset)
0396 {
0397 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0398 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0399 u32 val, reg;
0400 unsigned int func_offset = 0;
0401 struct dw_pcie_ep_func *ep_func;
0402
0403 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
0404 if (!ep_func || !ep_func->msix_cap)
0405 return -EINVAL;
0406
0407 dw_pcie_dbi_ro_wr_en(pci);
0408
0409 func_offset = dw_pcie_ep_func_select(ep, func_no);
0410
0411 reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
0412 val = dw_pcie_readw_dbi(pci, reg);
0413 val &= ~PCI_MSIX_FLAGS_QSIZE;
0414 val |= interrupts;
0415 dw_pcie_writew_dbi(pci, reg, val);
0416
0417 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
0418 val = offset | bir;
0419 dw_pcie_writel_dbi(pci, reg, val);
0420
0421 reg = ep_func->msix_cap + func_offset + PCI_MSIX_PBA;
0422 val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
0423 dw_pcie_writel_dbi(pci, reg, val);
0424
0425 dw_pcie_dbi_ro_wr_dis(pci);
0426
0427 return 0;
0428 }
0429
0430 static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0431 enum pci_epc_irq_type type, u16 interrupt_num)
0432 {
0433 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0434
0435 if (!ep->ops->raise_irq)
0436 return -EINVAL;
0437
0438 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
0439 }
0440
0441 static void dw_pcie_ep_stop(struct pci_epc *epc)
0442 {
0443 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0444 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0445
0446 dw_pcie_stop_link(pci);
0447 }
0448
0449 static int dw_pcie_ep_start(struct pci_epc *epc)
0450 {
0451 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0452 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0453
0454 return dw_pcie_start_link(pci);
0455 }
0456
0457 static const struct pci_epc_features*
0458 dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
0459 {
0460 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
0461
0462 if (!ep->ops->get_features)
0463 return NULL;
0464
0465 return ep->ops->get_features(ep);
0466 }
0467
0468 static const struct pci_epc_ops epc_ops = {
0469 .write_header = dw_pcie_ep_write_header,
0470 .set_bar = dw_pcie_ep_set_bar,
0471 .clear_bar = dw_pcie_ep_clear_bar,
0472 .map_addr = dw_pcie_ep_map_addr,
0473 .unmap_addr = dw_pcie_ep_unmap_addr,
0474 .set_msi = dw_pcie_ep_set_msi,
0475 .get_msi = dw_pcie_ep_get_msi,
0476 .set_msix = dw_pcie_ep_set_msix,
0477 .get_msix = dw_pcie_ep_get_msix,
0478 .raise_irq = dw_pcie_ep_raise_irq,
0479 .start = dw_pcie_ep_start,
0480 .stop = dw_pcie_ep_stop,
0481 .get_features = dw_pcie_ep_get_features,
0482 };
0483
0484 int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
0485 {
0486 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0487 struct device *dev = pci->dev;
0488
0489 dev_err(dev, "EP cannot trigger legacy IRQs\n");
0490
0491 return -EINVAL;
0492 }
0493 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_legacy_irq);
0494
0495 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
0496 u8 interrupt_num)
0497 {
0498 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0499 struct dw_pcie_ep_func *ep_func;
0500 struct pci_epc *epc = ep->epc;
0501 unsigned int aligned_offset;
0502 unsigned int func_offset = 0;
0503 u16 msg_ctrl, msg_data;
0504 u32 msg_addr_lower, msg_addr_upper, reg;
0505 u64 msg_addr;
0506 bool has_upper;
0507 int ret;
0508
0509 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
0510 if (!ep_func || !ep_func->msi_cap)
0511 return -EINVAL;
0512
0513 func_offset = dw_pcie_ep_func_select(ep, func_no);
0514
0515
0516 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
0517 msg_ctrl = dw_pcie_readw_dbi(pci, reg);
0518 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
0519 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
0520 msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
0521 if (has_upper) {
0522 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
0523 msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
0524 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
0525 msg_data = dw_pcie_readw_dbi(pci, reg);
0526 } else {
0527 msg_addr_upper = 0;
0528 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
0529 msg_data = dw_pcie_readw_dbi(pci, reg);
0530 }
0531 aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
0532 msg_addr = ((u64)msg_addr_upper) << 32 |
0533 (msg_addr_lower & ~aligned_offset);
0534 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
0535 epc->mem->window.page_size);
0536 if (ret)
0537 return ret;
0538
0539 writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
0540
0541 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
0542
0543 return 0;
0544 }
0545 EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq);
0546
0547 int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
0548 u16 interrupt_num)
0549 {
0550 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0551 struct dw_pcie_ep_func *ep_func;
0552 u32 msg_data;
0553
0554 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
0555 if (!ep_func || !ep_func->msix_cap)
0556 return -EINVAL;
0557
0558 msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
0559 (interrupt_num - 1);
0560
0561 dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
0562
0563 return 0;
0564 }
0565
0566 int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
0567 u16 interrupt_num)
0568 {
0569 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0570 struct dw_pcie_ep_func *ep_func;
0571 struct pci_epf_msix_tbl *msix_tbl;
0572 struct pci_epc *epc = ep->epc;
0573 unsigned int func_offset = 0;
0574 u32 reg, msg_data, vec_ctrl;
0575 unsigned int aligned_offset;
0576 u32 tbl_offset;
0577 u64 msg_addr;
0578 int ret;
0579 u8 bir;
0580
0581 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
0582 if (!ep_func || !ep_func->msix_cap)
0583 return -EINVAL;
0584
0585 func_offset = dw_pcie_ep_func_select(ep, func_no);
0586
0587 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
0588 tbl_offset = dw_pcie_readl_dbi(pci, reg);
0589 bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
0590 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
0591
0592 msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
0593 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
0594 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
0595 vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
0596
0597 if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
0598 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
0599 return -EPERM;
0600 }
0601
0602 aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
0603 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
0604 epc->mem->window.page_size);
0605 if (ret)
0606 return ret;
0607
0608 writel(msg_data, ep->msi_mem + aligned_offset);
0609
0610 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
0611
0612 return 0;
0613 }
0614
0615 void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
0616 {
0617 struct pci_epc *epc = ep->epc;
0618
0619 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
0620 epc->mem->window.page_size);
0621
0622 pci_epc_mem_exit(epc);
0623 }
0624
0625 static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
0626 {
0627 u32 header;
0628 int pos = PCI_CFG_SPACE_SIZE;
0629
0630 while (pos) {
0631 header = dw_pcie_readl_dbi(pci, pos);
0632 if (PCI_EXT_CAP_ID(header) == cap)
0633 return pos;
0634
0635 pos = PCI_EXT_CAP_NEXT(header);
0636 if (!pos)
0637 break;
0638 }
0639
0640 return 0;
0641 }
0642
0643 int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
0644 {
0645 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0646 unsigned int offset;
0647 unsigned int nbars;
0648 u8 hdr_type;
0649 u32 reg;
0650 int i;
0651
0652 hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
0653 PCI_HEADER_TYPE_MASK;
0654 if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
0655 dev_err(pci->dev,
0656 "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
0657 hdr_type);
0658 return -EIO;
0659 }
0660
0661 offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
0662
0663 dw_pcie_dbi_ro_wr_en(pci);
0664
0665 if (offset) {
0666 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
0667 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
0668 PCI_REBAR_CTRL_NBAR_SHIFT;
0669
0670 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
0671 dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, 0x0);
0672 }
0673
0674 dw_pcie_setup(pci);
0675 dw_pcie_dbi_ro_wr_dis(pci);
0676
0677 return 0;
0678 }
0679 EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
0680
0681 int dw_pcie_ep_init(struct dw_pcie_ep *ep)
0682 {
0683 int ret;
0684 void *addr;
0685 u8 func_no;
0686 struct resource *res;
0687 struct pci_epc *epc;
0688 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0689 struct device *dev = pci->dev;
0690 struct platform_device *pdev = to_platform_device(dev);
0691 struct device_node *np = dev->of_node;
0692 const struct pci_epc_features *epc_features;
0693 struct dw_pcie_ep_func *ep_func;
0694
0695 INIT_LIST_HEAD(&ep->func_list);
0696
0697 if (!pci->dbi_base) {
0698 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
0699 pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
0700 if (IS_ERR(pci->dbi_base))
0701 return PTR_ERR(pci->dbi_base);
0702 }
0703
0704 if (!pci->dbi_base2) {
0705 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi2");
0706 if (!res) {
0707 pci->dbi_base2 = pci->dbi_base + SZ_4K;
0708 } else {
0709 pci->dbi_base2 = devm_pci_remap_cfg_resource(dev, res);
0710 if (IS_ERR(pci->dbi_base2))
0711 return PTR_ERR(pci->dbi_base2);
0712 }
0713 }
0714
0715 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
0716 if (!res)
0717 return -EINVAL;
0718
0719 ep->phys_base = res->start;
0720 ep->addr_size = resource_size(res);
0721
0722 dw_pcie_version_detect(pci);
0723
0724 dw_pcie_iatu_detect(pci);
0725
0726 ep->ib_window_map = devm_bitmap_zalloc(dev, pci->num_ib_windows,
0727 GFP_KERNEL);
0728 if (!ep->ib_window_map)
0729 return -ENOMEM;
0730
0731 ep->ob_window_map = devm_bitmap_zalloc(dev, pci->num_ob_windows,
0732 GFP_KERNEL);
0733 if (!ep->ob_window_map)
0734 return -ENOMEM;
0735
0736 addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t),
0737 GFP_KERNEL);
0738 if (!addr)
0739 return -ENOMEM;
0740 ep->outbound_addr = addr;
0741
0742 if (pci->link_gen < 1)
0743 pci->link_gen = of_pci_get_max_link_speed(np);
0744
0745 epc = devm_pci_epc_create(dev, &epc_ops);
0746 if (IS_ERR(epc)) {
0747 dev_err(dev, "Failed to create epc device\n");
0748 return PTR_ERR(epc);
0749 }
0750
0751 ep->epc = epc;
0752 epc_set_drvdata(epc, ep);
0753
0754 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
0755 if (ret < 0)
0756 epc->max_functions = 1;
0757
0758 for (func_no = 0; func_no < epc->max_functions; func_no++) {
0759 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
0760 if (!ep_func)
0761 return -ENOMEM;
0762
0763 ep_func->func_no = func_no;
0764 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
0765 PCI_CAP_ID_MSI);
0766 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
0767 PCI_CAP_ID_MSIX);
0768
0769 list_add_tail(&ep_func->list, &ep->func_list);
0770 }
0771
0772 if (ep->ops->ep_init)
0773 ep->ops->ep_init(ep);
0774
0775 ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
0776 ep->page_size);
0777 if (ret < 0) {
0778 dev_err(dev, "Failed to initialize address space\n");
0779 return ret;
0780 }
0781
0782 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
0783 epc->mem->window.page_size);
0784 if (!ep->msi_mem) {
0785 ret = -ENOMEM;
0786 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
0787 goto err_exit_epc_mem;
0788 }
0789
0790 if (ep->ops->get_features) {
0791 epc_features = ep->ops->get_features(ep);
0792 if (epc_features->core_init_notifier)
0793 return 0;
0794 }
0795
0796 ret = dw_pcie_ep_init_complete(ep);
0797 if (ret)
0798 goto err_free_epc_mem;
0799
0800 return 0;
0801
0802 err_free_epc_mem:
0803 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
0804 epc->mem->window.page_size);
0805
0806 err_exit_epc_mem:
0807 pci_epc_mem_exit(epc);
0808
0809 return ret;
0810 }
0811 EXPORT_SYMBOL_GPL(dw_pcie_ep_init);