blob: f2e5feba552678411145973529423509593dc37e [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Krzysztof Kozlowski9b41d192020-07-29 22:12:19 +02002/*
Bjorn Helgaas96291d52017-09-01 16:35:50 -05003 * Synopsys DesignWare PCIe Endpoint controller driver
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +05304 *
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <[email protected]>
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +05307 */
8
Dan Carpenter6ea2f3b2024-01-26 11:40:37 +03009#include <linux/align.h>
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053010#include <linux/of.h>
Rob Herringa0fd3612020-11-05 15:11:46 -060011#include <linux/platform_device.h>
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053012
13#include "pcie-designware.h"
14#include <linux/pci-epc.h>
15#include <linux/pci-epf.h>
16
17void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
18{
19 struct pci_epc *epc = ep->epc;
20
21 pci_epc_linkup(epc);
22}
Vidya Sagarc57247f2020-03-03 23:40:52 +053023EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053024
Vidya Sagarac37dde2020-02-17 17:40:35 +053025void dw_pcie_ep_init_notify(struct dw_pcie_ep *ep)
26{
27 struct pci_epc *epc = ep->epc;
28
29 pci_epc_init_notify(epc);
30}
Vidya Sagarc57247f2020-03-03 23:40:52 +053031EXPORT_SYMBOL_GPL(dw_pcie_ep_init_notify);
Vidya Sagarac37dde2020-02-17 17:40:35 +053032
Xiaowei Bao47a06262020-09-18 16:00:16 +080033struct dw_pcie_ep_func *
34dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
35{
36 struct dw_pcie_ep_func *ep_func;
37
38 list_for_each_entry(ep_func, &ep->func_list, list) {
39 if (ep_func->func_no == func_no)
40 return ep_func;
41 }
42
43 return NULL;
44}
45
Xiaowei Bao24ede432020-09-18 16:00:13 +080046static unsigned int dw_pcie_ep_func_select(struct dw_pcie_ep *ep, u8 func_no)
47{
48 unsigned int func_offset = 0;
49
50 if (ep->ops->func_conf_select)
51 func_offset = ep->ops->func_conf_select(ep, func_no);
52
53 return func_offset;
54}
55
56static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
57 enum pci_barno bar, int flags)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053058{
59 u32 reg;
Xiaowei Bao24ede432020-09-18 16:00:13 +080060 unsigned int func_offset = 0;
61 struct dw_pcie_ep *ep = &pci->ep;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053062
Xiaowei Bao24ede432020-09-18 16:00:13 +080063 func_offset = dw_pcie_ep_func_select(ep, func_no);
64
65 reg = func_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
Niklas Cassel1cab8262017-12-20 00:29:24 +010066 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053067 dw_pcie_writel_dbi2(pci, reg, 0x0);
68 dw_pcie_writel_dbi(pci, reg, 0x0);
Niklas Cassel96a3be42018-03-28 13:50:16 +020069 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
70 dw_pcie_writel_dbi2(pci, reg + 4, 0x0);
71 dw_pcie_writel_dbi(pci, reg + 4, 0x0);
72 }
Niklas Cassel1cab8262017-12-20 00:29:24 +010073 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053074}
75
Niklas Cassel77d08db2018-03-28 13:50:14 +020076void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
77{
Xiaowei Bao24ede432020-09-18 16:00:13 +080078 u8 func_no, funcs;
79
80 funcs = pci->ep.epc->max_functions;
81
82 for (func_no = 0; func_no < funcs; func_no++)
83 __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
Niklas Cassel77d08db2018-03-28 13:50:14 +020084}
Manivannan Sadhasivamf55fee52021-09-20 12:29:45 +053085EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar);
Niklas Cassel77d08db2018-03-28 13:50:14 +020086
Xiaowei Bao47a06262020-09-18 16:00:16 +080087static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
88 u8 cap_ptr, u8 cap)
89{
90 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
91 unsigned int func_offset = 0;
92 u8 cap_id, next_cap_ptr;
93 u16 reg;
94
95 if (!cap_ptr)
96 return 0;
97
98 func_offset = dw_pcie_ep_func_select(ep, func_no);
99
100 reg = dw_pcie_readw_dbi(pci, func_offset + cap_ptr);
101 cap_id = (reg & 0x00ff);
102
103 if (cap_id > PCI_CAP_ID_MAX)
104 return 0;
105
106 if (cap_id == cap)
107 return cap_ptr;
108
109 next_cap_ptr = (reg & 0xff00) >> 8;
110 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
111}
112
113static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
114{
115 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
116 unsigned int func_offset = 0;
117 u8 next_cap_ptr;
118 u16 reg;
119
120 func_offset = dw_pcie_ep_func_select(ep, func_no);
121
122 reg = dw_pcie_readw_dbi(pci, func_offset + PCI_CAPABILITY_LIST);
123 next_cap_ptr = (reg & 0x00ff);
124
125 return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
126}
127
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530128static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530129 struct pci_epf_header *hdr)
130{
131 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
132 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Xiaowei Bao24ede432020-09-18 16:00:13 +0800133 unsigned int func_offset = 0;
134
135 func_offset = dw_pcie_ep_func_select(ep, func_no);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530136
Niklas Cassel1cab8262017-12-20 00:29:24 +0100137 dw_pcie_dbi_ro_wr_en(pci);
Xiaowei Bao24ede432020-09-18 16:00:13 +0800138 dw_pcie_writew_dbi(pci, func_offset + PCI_VENDOR_ID, hdr->vendorid);
139 dw_pcie_writew_dbi(pci, func_offset + PCI_DEVICE_ID, hdr->deviceid);
140 dw_pcie_writeb_dbi(pci, func_offset + PCI_REVISION_ID, hdr->revid);
141 dw_pcie_writeb_dbi(pci, func_offset + PCI_CLASS_PROG, hdr->progif_code);
142 dw_pcie_writew_dbi(pci, func_offset + PCI_CLASS_DEVICE,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530143 hdr->subclass_code | hdr->baseclass_code << 8);
Xiaowei Bao24ede432020-09-18 16:00:13 +0800144 dw_pcie_writeb_dbi(pci, func_offset + PCI_CACHE_LINE_SIZE,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530145 hdr->cache_line_size);
Xiaowei Bao24ede432020-09-18 16:00:13 +0800146 dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_VENDOR_ID,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530147 hdr->subsys_vendor_id);
Xiaowei Bao24ede432020-09-18 16:00:13 +0800148 dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_ID, hdr->subsys_id);
149 dw_pcie_writeb_dbi(pci, func_offset + PCI_INTERRUPT_PIN,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530150 hdr->interrupt_pin);
Niklas Cassel1cab8262017-12-20 00:29:24 +0100151 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530152
153 return 0;
154}
155
Serge Semin4859db92022-06-24 17:39:41 +0300156static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no, int type,
157 dma_addr_t cpu_addr, enum pci_barno bar)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530158{
159 int ret;
160 u32 free_win;
161 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
162
Frank Li4284c882022-02-22 10:23:52 -0600163 if (!ep->bar_to_atu[bar])
164 free_win = find_first_zero_bit(ep->ib_window_map, pci->num_ib_windows);
165 else
Frank Li92446492024-04-12 12:08:41 -0400166 free_win = ep->bar_to_atu[bar] - 1;
Frank Li4284c882022-02-22 10:23:52 -0600167
Rob Herring9ca17af2020-11-05 15:11:58 -0600168 if (free_win >= pci->num_ib_windows) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100169 dev_err(pci->dev, "No free inbound window\n");
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530170 return -EINVAL;
171 }
172
Serge Semin8522e172022-11-13 22:12:56 +0300173 ret = dw_pcie_prog_ep_inbound_atu(pci, func_no, free_win, type,
174 cpu_addr, bar);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530175 if (ret < 0) {
176 dev_err(pci->dev, "Failed to program IB window\n");
177 return ret;
178 }
179
Frank Li92446492024-04-12 12:08:41 -0400180 /*
181 * Always increment free_win before assignment, since value 0 is used to identify
182 * unallocated mapping.
183 */
184 ep->bar_to_atu[bar] = free_win + 1;
Niklas Casselad4a5be2017-12-14 14:01:44 +0100185 set_bit(free_win, ep->ib_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530186
187 return 0;
188}
189
Xiaowei Bao24ede432020-09-18 16:00:13 +0800190static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
191 phys_addr_t phys_addr,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530192 u64 pci_addr, size_t size)
193{
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530194 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Serge Semince06bf52022-06-24 17:39:46 +0300195 u32 free_win;
196 int ret;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530197
Rob Herring9ca17af2020-11-05 15:11:58 -0600198 free_win = find_first_zero_bit(ep->ob_window_map, pci->num_ob_windows);
199 if (free_win >= pci->num_ob_windows) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100200 dev_err(pci->dev, "No free outbound window\n");
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530201 return -EINVAL;
202 }
203
Serge Semince06bf52022-06-24 17:39:46 +0300204 ret = dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
205 phys_addr, pci_addr, size);
206 if (ret)
207 return ret;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530208
Niklas Casselad4a5be2017-12-14 14:01:44 +0100209 set_bit(free_win, ep->ob_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530210 ep->outbound_addr[free_win] = phys_addr;
211
212 return 0;
213}
214
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530215static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
Niklas Cassel77d08db2018-03-28 13:50:14 +0200216 struct pci_epf_bar *epf_bar)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530217{
218 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
219 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Niklas Cassel77d08db2018-03-28 13:50:14 +0200220 enum pci_barno bar = epf_bar->barno;
Frank Li92446492024-04-12 12:08:41 -0400221 u32 atu_index = ep->bar_to_atu[bar] - 1;
222
223 if (!ep->bar_to_atu[bar])
224 return;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530225
Xiaowei Bao24ede432020-09-18 16:00:13 +0800226 __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530227
Serge Semin38fe2722022-06-24 17:39:42 +0300228 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, atu_index);
Niklas Casselad4a5be2017-12-14 14:01:44 +0100229 clear_bit(atu_index, ep->ib_window_map);
Kishon Vijay Abraham I6f5e1932020-02-25 13:47:02 +0530230 ep->epf_bar[bar] = NULL;
Frank Li4284c882022-02-22 10:23:52 -0600231 ep->bar_to_atu[bar] = 0;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530232}
233
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530234static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
Niklas Casselbc4a4892018-03-28 13:50:07 +0200235 struct pci_epf_bar *epf_bar)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530236{
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530237 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
238 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Niklas Casselbc4a4892018-03-28 13:50:07 +0200239 enum pci_barno bar = epf_bar->barno;
240 size_t size = epf_bar->size;
241 int flags = epf_bar->flags;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800242 unsigned int func_offset = 0;
Serge Semin4859db92022-06-24 17:39:41 +0300243 int ret, type;
244 u32 reg;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800245
246 func_offset = dw_pcie_ep_func_select(ep, func_no);
247
248 reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530249
250 if (!(flags & PCI_BASE_ADDRESS_SPACE))
Serge Semin4859db92022-06-24 17:39:41 +0300251 type = PCIE_ATU_TYPE_MEM;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530252 else
Serge Semin4859db92022-06-24 17:39:41 +0300253 type = PCIE_ATU_TYPE_IO;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530254
Serge Semin4859db92022-06-24 17:39:41 +0300255 ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530256 if (ret)
257 return ret;
258
Frank Li4284c882022-02-22 10:23:52 -0600259 if (ep->epf_bar[bar])
260 return 0;
261
Niklas Cassel1cab8262017-12-20 00:29:24 +0100262 dw_pcie_dbi_ro_wr_en(pci);
Niklas Casseld28810b2018-03-28 13:50:11 +0200263
264 dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530265 dw_pcie_writel_dbi(pci, reg, flags);
Niklas Casseld28810b2018-03-28 13:50:11 +0200266
267 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
268 dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
269 dw_pcie_writel_dbi(pci, reg + 4, 0);
270 }
271
Kishon Vijay Abraham I6f5e1932020-02-25 13:47:02 +0530272 ep->epf_bar[bar] = epf_bar;
Niklas Cassel1cab8262017-12-20 00:29:24 +0100273 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530274
275 return 0;
276}
277
278static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
279 u32 *atu_index)
280{
281 u32 index;
Rob Herring9ca17af2020-11-05 15:11:58 -0600282 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530283
Rob Herring9ca17af2020-11-05 15:11:58 -0600284 for (index = 0; index < pci->num_ob_windows; index++) {
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530285 if (ep->outbound_addr[index] != addr)
286 continue;
287 *atu_index = index;
288 return 0;
289 }
290
291 return -EINVAL;
292}
293
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530294static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
Cyrille Pitchen44947382018-01-30 21:56:56 +0100295 phys_addr_t addr)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530296{
297 int ret;
298 u32 atu_index;
299 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
300 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
301
302 ret = dw_pcie_find_index(ep, addr, &atu_index);
303 if (ret < 0)
304 return;
305
Serge Semin38fe2722022-06-24 17:39:42 +0300306 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, atu_index);
Niklas Casselad4a5be2017-12-14 14:01:44 +0100307 clear_bit(atu_index, ep->ob_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530308}
309
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530310static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
311 phys_addr_t addr, u64 pci_addr, size_t size)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530312{
313 int ret;
314 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
315 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
316
Xiaowei Bao24ede432020-09-18 16:00:13 +0800317 ret = dw_pcie_ep_outbound_atu(ep, func_no, addr, pci_addr, size);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530318 if (ret) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100319 dev_err(pci->dev, "Failed to enable address\n");
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530320 return ret;
321 }
322
323 return 0;
324}
325
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530326static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530327{
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530328 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
329 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200330 u32 val, reg;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800331 unsigned int func_offset = 0;
Xiaowei Bao47a06262020-09-18 16:00:16 +0800332 struct dw_pcie_ep_func *ep_func;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530333
Xiaowei Bao47a06262020-09-18 16:00:16 +0800334 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
335 if (!ep_func || !ep_func->msi_cap)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530336 return -EINVAL;
337
Xiaowei Bao24ede432020-09-18 16:00:13 +0800338 func_offset = dw_pcie_ep_func_select(ep, func_no);
339
Xiaowei Bao47a06262020-09-18 16:00:16 +0800340 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200341 val = dw_pcie_readw_dbi(pci, reg);
342 if (!(val & PCI_MSI_FLAGS_ENABLE))
343 return -EINVAL;
344
345 val = (val & PCI_MSI_FLAGS_QSIZE) >> 4;
346
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530347 return val;
348}
349
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530350static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
351 u8 interrupts)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530352{
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530353 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
354 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200355 u32 val, reg;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800356 unsigned int func_offset = 0;
Xiaowei Bao47a06262020-09-18 16:00:16 +0800357 struct dw_pcie_ep_func *ep_func;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530358
Xiaowei Bao47a06262020-09-18 16:00:16 +0800359 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
360 if (!ep_func || !ep_func->msi_cap)
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200361 return -EINVAL;
362
Xiaowei Bao24ede432020-09-18 16:00:13 +0800363 func_offset = dw_pcie_ep_func_select(ep, func_no);
364
Xiaowei Bao47a06262020-09-18 16:00:16 +0800365 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200366 val = dw_pcie_readw_dbi(pci, reg);
367 val &= ~PCI_MSI_FLAGS_QMASK;
368 val |= (interrupts << 1) & PCI_MSI_FLAGS_QMASK;
Niklas Cassel1cab8262017-12-20 00:29:24 +0100369 dw_pcie_dbi_ro_wr_en(pci);
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200370 dw_pcie_writew_dbi(pci, reg, val);
Niklas Cassel1cab8262017-12-20 00:29:24 +0100371 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530372
373 return 0;
374}
375
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530376static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200377{
378 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
379 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
380 u32 val, reg;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800381 unsigned int func_offset = 0;
Xiaowei Bao47a06262020-09-18 16:00:16 +0800382 struct dw_pcie_ep_func *ep_func;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200383
Xiaowei Bao47a06262020-09-18 16:00:16 +0800384 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
385 if (!ep_func || !ep_func->msix_cap)
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200386 return -EINVAL;
387
Xiaowei Bao24ede432020-09-18 16:00:13 +0800388 func_offset = dw_pcie_ep_func_select(ep, func_no);
389
Xiaowei Bao47a06262020-09-18 16:00:16 +0800390 reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200391 val = dw_pcie_readw_dbi(pci, reg);
392 if (!(val & PCI_MSIX_FLAGS_ENABLE))
393 return -EINVAL;
394
395 val &= PCI_MSIX_FLAGS_QSIZE;
396
397 return val;
398}
399
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530400static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
401 u16 interrupts, enum pci_barno bir, u32 offset)
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200402{
403 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
404 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
405 u32 val, reg;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800406 unsigned int func_offset = 0;
Xiaowei Bao47a06262020-09-18 16:00:16 +0800407 struct dw_pcie_ep_func *ep_func;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200408
Xiaowei Bao47a06262020-09-18 16:00:16 +0800409 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
410 if (!ep_func || !ep_func->msix_cap)
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200411 return -EINVAL;
412
Kishon Vijay Abraham I83153d92020-02-25 13:47:01 +0530413 dw_pcie_dbi_ro_wr_en(pci);
414
Xiaowei Bao24ede432020-09-18 16:00:13 +0800415 func_offset = dw_pcie_ep_func_select(ep, func_no);
416
Xiaowei Bao47a06262020-09-18 16:00:16 +0800417 reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200418 val = dw_pcie_readw_dbi(pci, reg);
419 val &= ~PCI_MSIX_FLAGS_QSIZE;
420 val |= interrupts;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200421 dw_pcie_writew_dbi(pci, reg, val);
Kishon Vijay Abraham I83153d92020-02-25 13:47:01 +0530422
Xiaowei Bao47a06262020-09-18 16:00:16 +0800423 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
Kishon Vijay Abraham I83153d92020-02-25 13:47:01 +0530424 val = offset | bir;
425 dw_pcie_writel_dbi(pci, reg, val);
426
Xiaowei Bao47a06262020-09-18 16:00:16 +0800427 reg = ep_func->msix_cap + func_offset + PCI_MSIX_PBA;
Kishon Vijay Abraham I83153d92020-02-25 13:47:01 +0530428 val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
429 dw_pcie_writel_dbi(pci, reg, val);
430
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200431 dw_pcie_dbi_ro_wr_dis(pci);
432
433 return 0;
434}
435
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530436static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
Gustavo Pimenteld3c70a92018-07-19 10:32:13 +0200437 enum pci_epc_irq_type type, u16 interrupt_num)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530438{
439 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
440
441 if (!ep->ops->raise_irq)
442 return -EINVAL;
443
Bjorn Helgaas16093362018-02-01 11:36:07 -0600444 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530445}
446
447static void dw_pcie_ep_stop(struct pci_epc *epc)
448{
449 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
450 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
451
Serge Semina37beef2022-06-24 17:34:23 +0300452 dw_pcie_stop_link(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530453}
454
455static int dw_pcie_ep_start(struct pci_epc *epc)
456{
457 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
458 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
459
Serge Semina37beef2022-06-24 17:34:23 +0300460 return dw_pcie_start_link(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530461}
462
Kishon Vijay Abraham Ifee35cb2019-01-14 16:45:00 +0530463static const struct pci_epc_features*
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530464dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
Kishon Vijay Abraham Ifee35cb2019-01-14 16:45:00 +0530465{
466 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
467
468 if (!ep->ops->get_features)
469 return NULL;
470
471 return ep->ops->get_features(ep);
472}
473
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530474static const struct pci_epc_ops epc_ops = {
475 .write_header = dw_pcie_ep_write_header,
476 .set_bar = dw_pcie_ep_set_bar,
477 .clear_bar = dw_pcie_ep_clear_bar,
478 .map_addr = dw_pcie_ep_map_addr,
479 .unmap_addr = dw_pcie_ep_unmap_addr,
480 .set_msi = dw_pcie_ep_set_msi,
481 .get_msi = dw_pcie_ep_get_msi,
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200482 .set_msix = dw_pcie_ep_set_msix,
483 .get_msix = dw_pcie_ep_get_msix,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530484 .raise_irq = dw_pcie_ep_raise_irq,
485 .start = dw_pcie_ep_start,
486 .stop = dw_pcie_ep_stop,
Kishon Vijay Abraham Ifee35cb2019-01-14 16:45:00 +0530487 .get_features = dw_pcie_ep_get_features,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530488};
489
Gustavo Pimentelcb22d402018-07-19 10:32:16 +0200490int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
491{
492 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
493 struct device *dev = pci->dev;
494
495 dev_err(dev, "EP cannot trigger legacy IRQs\n");
496
497 return -EINVAL;
498}
Manivannan Sadhasivamf55fee52021-09-20 12:29:45 +0530499EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_legacy_irq);
Gustavo Pimentelcb22d402018-07-19 10:32:16 +0200500
Bjorn Helgaas16093362018-02-01 11:36:07 -0600501int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100502 u8 interrupt_num)
503{
504 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Xiaowei Bao47a06262020-09-18 16:00:16 +0800505 struct dw_pcie_ep_func *ep_func;
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100506 struct pci_epc *epc = ep->epc;
Kishon Vijay Abraham I6b733032019-03-25 15:09:45 +0530507 unsigned int aligned_offset;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800508 unsigned int func_offset = 0;
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100509 u16 msg_ctrl, msg_data;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200510 u32 msg_addr_lower, msg_addr_upper, reg;
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100511 u64 msg_addr;
512 bool has_upper;
513 int ret;
514
Xiaowei Bao47a06262020-09-18 16:00:16 +0800515 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
516 if (!ep_func || !ep_func->msi_cap)
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200517 return -EINVAL;
518
Xiaowei Bao24ede432020-09-18 16:00:13 +0800519 func_offset = dw_pcie_ep_func_select(ep, func_no);
520
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100521 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
Xiaowei Bao47a06262020-09-18 16:00:16 +0800522 reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200523 msg_ctrl = dw_pcie_readw_dbi(pci, reg);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100524 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
Xiaowei Bao47a06262020-09-18 16:00:16 +0800525 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200526 msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100527 if (has_upper) {
Xiaowei Bao47a06262020-09-18 16:00:16 +0800528 reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200529 msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
Xiaowei Bao47a06262020-09-18 16:00:16 +0800530 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200531 msg_data = dw_pcie_readw_dbi(pci, reg);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100532 } else {
533 msg_addr_upper = 0;
Xiaowei Bao47a06262020-09-18 16:00:16 +0800534 reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
Gustavo Pimentel3920a5d2018-07-19 10:32:15 +0200535 msg_data = dw_pcie_readw_dbi(pci, reg);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100536 }
Lad Prabhakard45e3c12020-05-07 13:33:16 +0100537 aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
Kishon Vijay Abraham I6b733032019-03-25 15:09:45 +0530538 msg_addr = ((u64)msg_addr_upper) << 32 |
539 (msg_addr_lower & ~aligned_offset);
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530540 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
Lad Prabhakard45e3c12020-05-07 13:33:16 +0100541 epc->mem->window.page_size);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100542 if (ret)
543 return ret;
544
Kishon Vijay Abraham I6b733032019-03-25 15:09:45 +0530545 writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100546
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530547 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100548
549 return 0;
550}
Manivannan Sadhasivamf55fee52021-09-20 12:29:45 +0530551EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100552
Xiaowei Bao2f7f7002020-09-18 16:00:14 +0800553int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
554 u16 interrupt_num)
555{
556 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
557 struct dw_pcie_ep_func *ep_func;
558 u32 msg_data;
559
560 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
561 if (!ep_func || !ep_func->msix_cap)
562 return -EINVAL;
563
564 msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
565 (interrupt_num - 1);
566
567 dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
568
569 return 0;
570}
571
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200572int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
Xiaowei Bao24ede432020-09-18 16:00:13 +0800573 u16 interrupt_num)
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200574{
575 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Xiaowei Bao47a06262020-09-18 16:00:16 +0800576 struct dw_pcie_ep_func *ep_func;
Kishon Vijay Abraham I6f5e1932020-02-25 13:47:02 +0530577 struct pci_epf_msix_tbl *msix_tbl;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200578 struct pci_epc *epc = ep->epc;
Xiaowei Bao24ede432020-09-18 16:00:13 +0800579 unsigned int func_offset = 0;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200580 u32 reg, msg_data, vec_ctrl;
Kishon Vijay Abraham I6f5e1932020-02-25 13:47:02 +0530581 unsigned int aligned_offset;
582 u32 tbl_offset;
583 u64 msg_addr;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200584 int ret;
Kishon Vijay Abraham I6f5e1932020-02-25 13:47:02 +0530585 u8 bir;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200586
Xiaowei Bao47a06262020-09-18 16:00:16 +0800587 ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
588 if (!ep_func || !ep_func->msix_cap)
589 return -EINVAL;
590
Xiaowei Bao24ede432020-09-18 16:00:13 +0800591 func_offset = dw_pcie_ep_func_select(ep, func_no);
592
Xiaowei Bao47a06262020-09-18 16:00:16 +0800593 reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200594 tbl_offset = dw_pcie_readl_dbi(pci, reg);
595 bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
596 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200597
Jiri Slabybf711622020-04-20 08:52:27 +0200598 msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
Kishon Vijay Abraham I6f5e1932020-02-25 13:47:02 +0530599 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
600 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
601 vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200602
Gustavo Pimentel0380cf82018-12-07 18:24:37 +0100603 if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
604 dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200605 return -EPERM;
Gustavo Pimentel0380cf82018-12-07 18:24:37 +0100606 }
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200607
Lad Prabhakard45e3c12020-05-07 13:33:16 +0100608 aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
Dan Carpenter6ea2f3b2024-01-26 11:40:37 +0300609 msg_addr = ALIGN_DOWN(msg_addr, epc->mem->window.page_size);
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530610 ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
Lad Prabhakard45e3c12020-05-07 13:33:16 +0100611 epc->mem->window.page_size);
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200612 if (ret)
613 return ret;
614
Kishon Vijay Abraham I6f5e1932020-02-25 13:47:02 +0530615 writel(msg_data, ep->msi_mem + aligned_offset);
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200616
Kishon Vijay Abraham I53fd3cb2021-08-19 18:03:39 +0530617 dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200618
619 return 0;
620}
621
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530622void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
623{
Serge Semin939fbcd2023-01-13 20:14:09 +0300624 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530625 struct pci_epc *epc = ep->epc;
626
Serge Semin939fbcd2023-01-13 20:14:09 +0300627 dw_pcie_edma_remove(pci);
628
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100629 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
Lad Prabhakard45e3c12020-05-07 13:33:16 +0100630 epc->mem->window.page_size);
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100631
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530632 pci_epc_mem_exit(epc);
633}
634
Kishon Vijay Abraham Ifc9a7702019-03-25 15:09:44 +0530635static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
636{
637 u32 header;
638 int pos = PCI_CFG_SPACE_SIZE;
639
640 while (pos) {
641 header = dw_pcie_readl_dbi(pci, pos);
642 if (PCI_EXT_CAP_ID(header) == cap)
643 return pos;
644
645 pos = PCI_EXT_CAP_NEXT(header);
646 if (!pos)
647 break;
648 }
649
650 return 0;
651}
652
Vidya Sagare966f732020-02-17 17:40:33 +0530653int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
654{
655 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Vidya Sagar442ae912022-09-19 20:03:40 +0530656 unsigned int offset, ptm_cap_base;
Vidya Sagare966f732020-02-17 17:40:33 +0530657 unsigned int nbars;
658 u8 hdr_type;
659 u32 reg;
660 int i;
661
Hou Zhiqiang16270a92020-08-18 17:27:46 +0800662 hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
663 PCI_HEADER_TYPE_MASK;
Vidya Sagare966f732020-02-17 17:40:33 +0530664 if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
665 dev_err(pci->dev,
666 "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
667 hdr_type);
668 return -EIO;
669 }
670
Vidya Sagare966f732020-02-17 17:40:33 +0530671 offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
Vidya Sagar442ae912022-09-19 20:03:40 +0530672 ptm_cap_base = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM);
Rob Herring39bc5002020-08-20 21:54:14 -0600673
674 dw_pcie_dbi_ro_wr_en(pci);
675
Vidya Sagare966f732020-02-17 17:40:33 +0530676 if (offset) {
677 reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
678 nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
679 PCI_REBAR_CTRL_NBAR_SHIFT;
680
Niklas Casselcebb4ba2024-03-07 12:15:20 +0100681 /*
682 * PCIe r6.0, sec 7.8.6.2 require us to support at least one
683 * size in the range from 1 MB to 512 GB. Advertise support
684 * for 1 MB BAR size only.
685 */
Vidya Sagare966f732020-02-17 17:40:33 +0530686 for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
Niklas Casselcebb4ba2024-03-07 12:15:20 +0100687 dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, BIT(4));
Vidya Sagare966f732020-02-17 17:40:33 +0530688 }
689
Vidya Sagar442ae912022-09-19 20:03:40 +0530690 /*
691 * PTM responder capability can be disabled only after disabling
692 * PTM root capability.
693 */
694 if (ptm_cap_base) {
695 dw_pcie_dbi_ro_wr_en(pci);
696 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP);
697 reg &= ~PCI_PTM_CAP_ROOT;
698 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg);
699
700 reg = dw_pcie_readl_dbi(pci, ptm_cap_base + PCI_PTM_CAP);
701 reg &= ~(PCI_PTM_CAP_RES | PCI_PTM_GRANULARITY_MASK);
702 dw_pcie_writel_dbi(pci, ptm_cap_base + PCI_PTM_CAP, reg);
703 dw_pcie_dbi_ro_wr_dis(pci);
704 }
705
Vidya Sagare966f732020-02-17 17:40:33 +0530706 dw_pcie_setup(pci);
Rob Herring39bc5002020-08-20 21:54:14 -0600707 dw_pcie_dbi_ro_wr_dis(pci);
Vidya Sagare966f732020-02-17 17:40:33 +0530708
709 return 0;
710}
Vidya Sagarc57247f2020-03-03 23:40:52 +0530711EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
Vidya Sagare966f732020-02-17 17:40:33 +0530712
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530713int dw_pcie_ep_init(struct dw_pcie_ep *ep)
714{
715 int ret;
716 void *addr;
Xiaowei Bao47a06262020-09-18 16:00:16 +0800717 u8 func_no;
Rob Herringa0fd3612020-11-05 15:11:46 -0600718 struct resource *res;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530719 struct pci_epc *epc;
720 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
721 struct device *dev = pci->dev;
Rob Herringa0fd3612020-11-05 15:11:46 -0600722 struct platform_device *pdev = to_platform_device(dev);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530723 struct device_node *np = dev->of_node;
Vidya Sagare966f732020-02-17 17:40:33 +0530724 const struct pci_epc_features *epc_features;
Xiaowei Bao47a06262020-09-18 16:00:16 +0800725 struct dw_pcie_ep_func *ep_func;
726
727 INIT_LIST_HEAD(&ep->func_list);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530728
Serge Seminef8c5882022-11-13 22:12:58 +0300729 ret = dw_pcie_get_resources(pci);
730 if (ret)
731 return ret;
Rob Herringa0fd3612020-11-05 15:11:46 -0600732
733 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
734 if (!res)
735 return -EINVAL;
736
737 ep->phys_base = res->start;
738 ep->addr_size = resource_size(res);
739
Serge Semin13e9d392022-06-24 17:39:36 +0300740 dw_pcie_version_detect(pci);
741
Serge Semine3dc79a2022-06-24 17:39:34 +0300742 dw_pcie_iatu_detect(pci);
743
Christophe JAILLET6be6f852022-07-09 16:10:52 +0200744 ep->ib_window_map = devm_bitmap_zalloc(dev, pci->num_ib_windows,
745 GFP_KERNEL);
Niklas Casselad4a5be2017-12-14 14:01:44 +0100746 if (!ep->ib_window_map)
747 return -ENOMEM;
748
Christophe JAILLET6be6f852022-07-09 16:10:52 +0200749 ep->ob_window_map = devm_bitmap_zalloc(dev, pci->num_ob_windows,
750 GFP_KERNEL);
Niklas Casselad4a5be2017-12-14 14:01:44 +0100751 if (!ep->ob_window_map)
752 return -ENOMEM;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530753
Rob Herring9ca17af2020-11-05 15:11:58 -0600754 addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t),
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530755 GFP_KERNEL);
756 if (!addr)
757 return -ENOMEM;
758 ep->outbound_addr = addr;
759
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530760 epc = devm_pci_epc_create(dev, &epc_ops);
761 if (IS_ERR(epc)) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100762 dev_err(dev, "Failed to create epc device\n");
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530763 return PTR_ERR(epc);
764 }
765
Gustavo Pimentel4e965ed2018-07-19 10:32:11 +0200766 ep->epc = epc;
767 epc_set_drvdata(epc, ep);
768
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530769 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
770 if (ret < 0)
771 epc->max_functions = 1;
772
Xiaowei Bao47a06262020-09-18 16:00:16 +0800773 for (func_no = 0; func_no < epc->max_functions; func_no++) {
774 ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
775 if (!ep_func)
776 return -ENOMEM;
Xiaowei Bao6bfc9c32020-09-18 16:00:15 +0800777
Xiaowei Bao47a06262020-09-18 16:00:16 +0800778 ep_func->func_no = func_no;
779 ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
780 PCI_CAP_ID_MSI);
781 ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
782 PCI_CAP_ID_MSIX);
783
784 list_add_tail(&ep_func->list, &ep->func_list);
785 }
Xiaowei Bao6bfc9c32020-09-18 16:00:15 +0800786
Xiaowei Bao24ede432020-09-18 16:00:13 +0800787 if (ep->ops->ep_init)
788 ep->ops->ep_init(ep);
789
Lad Prabhakard45e3c12020-05-07 13:33:16 +0100790 ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
791 ep->page_size);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530792 if (ret < 0) {
793 dev_err(dev, "Failed to initialize address space\n");
794 return ret;
795 }
796
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100797 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
Lad Prabhakard45e3c12020-05-07 13:33:16 +0100798 epc->mem->window.page_size);
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100799 if (!ep->msi_mem) {
Serge Semin8161e962022-06-24 17:34:15 +0300800 ret = -ENOMEM;
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200801 dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
Serge Semin8161e962022-06-24 17:34:15 +0300802 goto err_exit_epc_mem;
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100803 }
Gustavo Pimentelbeb46412018-07-19 10:32:14 +0200804
Serge Semin939fbcd2023-01-13 20:14:09 +0300805 ret = dw_pcie_edma_detect(pci);
806 if (ret)
807 goto err_free_epc_mem;
808
Vidya Sagare966f732020-02-17 17:40:33 +0530809 if (ep->ops->get_features) {
810 epc_features = ep->ops->get_features(ep);
811 if (epc_features->core_init_notifier)
812 return 0;
Kishon Vijay Abraham Ifc9a7702019-03-25 15:09:44 +0530813 }
814
Serge Semin8161e962022-06-24 17:34:15 +0300815 ret = dw_pcie_ep_init_complete(ep);
816 if (ret)
Serge Semin939fbcd2023-01-13 20:14:09 +0300817 goto err_remove_edma;
Serge Semin8161e962022-06-24 17:34:15 +0300818
819 return 0;
820
Serge Semin939fbcd2023-01-13 20:14:09 +0300821err_remove_edma:
822 dw_pcie_edma_remove(pci);
823
Serge Semin8161e962022-06-24 17:34:15 +0300824err_free_epc_mem:
825 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
826 epc->mem->window.page_size);
827
828err_exit_epc_mem:
829 pci_epc_mem_exit(epc);
830
831 return ret;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530832}
Vidya Sagarc57247f2020-03-03 23:40:52 +0530833EXPORT_SYMBOL_GPL(dw_pcie_ep_init);