1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Cadence
3 // Cadence PCIe endpoint controller driver.
4 // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com>
5
6 #include <linux/delay.h>
7 #include <linux/kernel.h>
8 #include <linux/of.h>
9 #include <linux/pci-epc.h>
10 #include <linux/platform_device.h>
11 #include <linux/sizes.h>
12
13 #include "pcie-cadence.h"
14
15 #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */
16 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
17 #define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
18
cdns_pcie_get_fn_from_vfn(struct cdns_pcie * pcie,u8 fn,u8 vfn)19 static u8 cdns_pcie_get_fn_from_vfn(struct cdns_pcie *pcie, u8 fn, u8 vfn)
20 {
21 u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
22 u32 first_vf_offset, stride;
23
24 if (vfn == 0)
25 return fn;
26
27 first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_SRIOV_VF_OFFSET);
28 stride = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_SRIOV_VF_STRIDE);
29 fn = fn + first_vf_offset + ((vfn - 1) * stride);
30
31 return fn;
32 }
33
cdns_pcie_ep_write_header(struct pci_epc * epc,u8 fn,u8 vfn,struct pci_epf_header * hdr)34 static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn, u8 vfn,
35 struct pci_epf_header *hdr)
36 {
37 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
38 u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
39 struct cdns_pcie *pcie = &ep->pcie;
40 u32 reg;
41
42 if (vfn > 1) {
43 dev_err(&epc->dev, "Only Virtual Function #1 has deviceID\n");
44 return -EINVAL;
45 } else if (vfn == 1) {
46 reg = cap + PCI_SRIOV_VF_DID;
47 cdns_pcie_ep_fn_writew(pcie, fn, reg, hdr->deviceid);
48 return 0;
49 }
50
51 cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
52 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
53 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
54 cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
55 hdr->subclass_code | hdr->baseclass_code << 8);
56 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
57 hdr->cache_line_size);
58 cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
59 cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
60
61 /*
62 * Vendor ID can only be modified from function 0, all other functions
63 * use the same vendor ID as function 0.
64 */
65 if (fn == 0) {
66 /* Update the vendor IDs. */
67 u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
68 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
69
70 cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
71 }
72
73 return 0;
74 }
75
cdns_pcie_ep_set_bar(struct pci_epc * epc,u8 fn,u8 vfn,struct pci_epf_bar * epf_bar)76 static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn, u8 vfn,
77 struct pci_epf_bar *epf_bar)
78 {
79 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
80 struct cdns_pcie_epf *epf = &ep->epf[fn];
81 struct cdns_pcie *pcie = &ep->pcie;
82 dma_addr_t bar_phys = epf_bar->phys_addr;
83 enum pci_barno bar = epf_bar->barno;
84 int flags = epf_bar->flags;
85 u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
86 u64 sz;
87
88 /* BAR size is 2^(aperture + 7) */
89 sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE);
90 /*
91 * roundup_pow_of_two() returns an unsigned long, which is not suited
92 * for 64bit values.
93 */
94 sz = 1ULL << fls64(sz - 1);
95 aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
96
97 if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
98 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
99 } else {
100 bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
101 bool is_64bits = sz > SZ_2G;
102
103 if (is_64bits && (bar & 1))
104 return -EINVAL;
105
106 if (is_64bits && !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
107 epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
108
109 if (is_64bits && is_prefetch)
110 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
111 else if (is_prefetch)
112 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
113 else if (is_64bits)
114 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
115 else
116 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
117 }
118
119 addr0 = lower_32_bits(bar_phys);
120 addr1 = upper_32_bits(bar_phys);
121
122 if (vfn == 1)
123 reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG(bar, fn);
124 else
125 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
126 b = (bar < BAR_4) ? bar : bar - BAR_4;
127
128 if (vfn == 0 || vfn == 1) {
129 cfg = cdns_pcie_readl(pcie, reg);
130 cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
131 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
132 cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
133 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
134 cdns_pcie_writel(pcie, reg, cfg);
135 }
136
137 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
138 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
139 addr0);
140 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
141 addr1);
142
143 if (vfn > 0)
144 epf = &epf->epf[vfn - 1];
145 epf->epf_bar[bar] = epf_bar;
146
147 return 0;
148 }
149
cdns_pcie_ep_clear_bar(struct pci_epc * epc,u8 fn,u8 vfn,struct pci_epf_bar * epf_bar)150 static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn, u8 vfn,
151 struct pci_epf_bar *epf_bar)
152 {
153 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
154 struct cdns_pcie_epf *epf = &ep->epf[fn];
155 struct cdns_pcie *pcie = &ep->pcie;
156 enum pci_barno bar = epf_bar->barno;
157 u32 reg, cfg, b, ctrl;
158
159 if (vfn == 1)
160 reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG(bar, fn);
161 else
162 reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
163 b = (bar < BAR_4) ? bar : bar - BAR_4;
164
165 if (vfn == 0 || vfn == 1) {
166 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
167 cfg = cdns_pcie_readl(pcie, reg);
168 cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
169 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
170 cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
171 cdns_pcie_writel(pcie, reg, cfg);
172 }
173
174 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
175 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
176 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);
177
178 if (vfn > 0)
179 epf = &epf->epf[vfn - 1];
180 epf->epf_bar[bar] = NULL;
181 }
182
cdns_pcie_ep_map_addr(struct pci_epc * epc,u8 fn,u8 vfn,phys_addr_t addr,u64 pci_addr,size_t size)183 static int cdns_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, u8 vfn,
184 phys_addr_t addr, u64 pci_addr, size_t size)
185 {
186 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
187 struct cdns_pcie *pcie = &ep->pcie;
188 u32 r;
189
190 r = find_first_zero_bit(&ep->ob_region_map, BITS_PER_LONG);
191 if (r >= ep->max_regions - 1) {
192 dev_err(&epc->dev, "no free outbound region\n");
193 return -EINVAL;
194 }
195
196 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
197 cdns_pcie_set_outbound_region(pcie, 0, fn, r, false, addr, pci_addr, size);
198
199 set_bit(r, &ep->ob_region_map);
200 ep->ob_addr[r] = addr;
201
202 return 0;
203 }
204
cdns_pcie_ep_unmap_addr(struct pci_epc * epc,u8 fn,u8 vfn,phys_addr_t addr)205 static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, u8 fn, u8 vfn,
206 phys_addr_t addr)
207 {
208 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
209 struct cdns_pcie *pcie = &ep->pcie;
210 u32 r;
211
212 for (r = 0; r < ep->max_regions - 1; r++)
213 if (ep->ob_addr[r] == addr)
214 break;
215
216 if (r == ep->max_regions - 1)
217 return;
218
219 cdns_pcie_reset_outbound_region(pcie, r);
220
221 ep->ob_addr[r] = 0;
222 clear_bit(r, &ep->ob_region_map);
223 }
224
cdns_pcie_ep_set_msi(struct pci_epc * epc,u8 fn,u8 vfn,u8 mmc)225 static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 vfn, u8 mmc)
226 {
227 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
228 struct cdns_pcie *pcie = &ep->pcie;
229 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
230 u16 flags;
231
232 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
233
234 /*
235 * Set the Multiple Message Capable bitfield into the Message Control
236 * register.
237 */
238 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
239 flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
240 flags |= PCI_MSI_FLAGS_64BIT;
241 flags &= ~PCI_MSI_FLAGS_MASKBIT;
242 cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
243
244 return 0;
245 }
246
cdns_pcie_ep_get_msi(struct pci_epc * epc,u8 fn,u8 vfn)247 static int cdns_pcie_ep_get_msi(struct pci_epc *epc, u8 fn, u8 vfn)
248 {
249 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
250 struct cdns_pcie *pcie = &ep->pcie;
251 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
252 u16 flags, mme;
253
254 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
255
256 /* Validate that the MSI feature is actually enabled. */
257 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
258 if (!(flags & PCI_MSI_FLAGS_ENABLE))
259 return -EINVAL;
260
261 /*
262 * Get the Multiple Message Enable bitfield from the Message Control
263 * register.
264 */
265 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
266
267 return mme;
268 }
269
cdns_pcie_ep_get_msix(struct pci_epc * epc,u8 func_no,u8 vfunc_no)270 static int cdns_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
271 {
272 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
273 struct cdns_pcie *pcie = &ep->pcie;
274 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
275 u32 val, reg;
276
277 func_no = cdns_pcie_get_fn_from_vfn(pcie, func_no, vfunc_no);
278
279 reg = cap + PCI_MSIX_FLAGS;
280 val = cdns_pcie_ep_fn_readw(pcie, func_no, reg);
281 if (!(val & PCI_MSIX_FLAGS_ENABLE))
282 return -EINVAL;
283
284 val &= PCI_MSIX_FLAGS_QSIZE;
285
286 return val;
287 }
288
cdns_pcie_ep_set_msix(struct pci_epc * epc,u8 fn,u8 vfn,u16 interrupts,enum pci_barno bir,u32 offset)289 static int cdns_pcie_ep_set_msix(struct pci_epc *epc, u8 fn, u8 vfn,
290 u16 interrupts, enum pci_barno bir,
291 u32 offset)
292 {
293 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
294 struct cdns_pcie *pcie = &ep->pcie;
295 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
296 u32 val, reg;
297
298 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
299
300 reg = cap + PCI_MSIX_FLAGS;
301 val = cdns_pcie_ep_fn_readw(pcie, fn, reg);
302 val &= ~PCI_MSIX_FLAGS_QSIZE;
303 val |= interrupts;
304 cdns_pcie_ep_fn_writew(pcie, fn, reg, val);
305
306 /* Set MSIX BAR and offset */
307 reg = cap + PCI_MSIX_TABLE;
308 val = offset | bir;
309 cdns_pcie_ep_fn_writel(pcie, fn, reg, val);
310
311 /* Set PBA BAR and offset. BAR must match MSIX BAR */
312 reg = cap + PCI_MSIX_PBA;
313 val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
314 cdns_pcie_ep_fn_writel(pcie, fn, reg, val);
315
316 return 0;
317 }
318
cdns_pcie_ep_assert_intx(struct cdns_pcie_ep * ep,u8 fn,u8 intx,bool is_asserted)319 static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn, u8 intx,
320 bool is_asserted)
321 {
322 struct cdns_pcie *pcie = &ep->pcie;
323 unsigned long flags;
324 u32 offset;
325 u16 status;
326 u8 msg_code;
327
328 intx &= 3;
329
330 /* Set the outbound region if needed. */
331 if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY ||
332 ep->irq_pci_fn != fn)) {
333 /* First region was reserved for IRQ writes. */
334 cdns_pcie_set_outbound_region_for_normal_msg(pcie, 0, fn, 0,
335 ep->irq_phys_addr);
336 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
337 ep->irq_pci_fn = fn;
338 }
339
340 if (is_asserted) {
341 ep->irq_pending |= BIT(intx);
342 msg_code = MSG_CODE_ASSERT_INTA + intx;
343 } else {
344 ep->irq_pending &= ~BIT(intx);
345 msg_code = MSG_CODE_DEASSERT_INTA + intx;
346 }
347
348 spin_lock_irqsave(&ep->lock, flags);
349 status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
350 if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
351 status ^= PCI_STATUS_INTERRUPT;
352 cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
353 }
354 spin_unlock_irqrestore(&ep->lock, flags);
355
356 offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
357 CDNS_PCIE_NORMAL_MSG_CODE(msg_code);
358 writel(0, ep->irq_cpu_addr + offset);
359 }
360
cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep * ep,u8 fn,u8 vfn,u8 intx)361 static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
362 u8 intx)
363 {
364 u16 cmd;
365
366 cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
367 if (cmd & PCI_COMMAND_INTX_DISABLE)
368 return -EINVAL;
369
370 cdns_pcie_ep_assert_intx(ep, fn, intx, true);
371 /*
372 * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
373 */
374 mdelay(1);
375 cdns_pcie_ep_assert_intx(ep, fn, intx, false);
376 return 0;
377 }
378
cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep * ep,u8 fn,u8 vfn,u8 interrupt_num)379 static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
380 u8 interrupt_num)
381 {
382 struct cdns_pcie *pcie = &ep->pcie;
383 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
384 u16 flags, mme, data, data_mask;
385 u8 msi_count;
386 u64 pci_addr, pci_addr_mask = 0xff;
387
388 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
389
390 /* Check whether the MSI feature has been enabled by the PCI host. */
391 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
392 if (!(flags & PCI_MSI_FLAGS_ENABLE))
393 return -EINVAL;
394
395 /* Get the number of enabled MSIs */
396 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
397 msi_count = 1 << mme;
398 if (!interrupt_num || interrupt_num > msi_count)
399 return -EINVAL;
400
401 /* Compute the data value to be written. */
402 data_mask = msi_count - 1;
403 data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
404 data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
405
406 /* Get the PCI address where to write the data into. */
407 pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
408 pci_addr <<= 32;
409 pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
410 pci_addr &= GENMASK_ULL(63, 2);
411
412 /* Set the outbound region if needed. */
413 if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) ||
414 ep->irq_pci_fn != fn)) {
415 /* First region was reserved for IRQ writes. */
416 cdns_pcie_set_outbound_region(pcie, 0, fn, 0,
417 false,
418 ep->irq_phys_addr,
419 pci_addr & ~pci_addr_mask,
420 pci_addr_mask + 1);
421 ep->irq_pci_addr = (pci_addr & ~pci_addr_mask);
422 ep->irq_pci_fn = fn;
423 }
424 writel(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
425
426 return 0;
427 }
428
cdns_pcie_ep_map_msi_irq(struct pci_epc * epc,u8 fn,u8 vfn,phys_addr_t addr,u8 interrupt_num,u32 entry_size,u32 * msi_data,u32 * msi_addr_offset)429 static int cdns_pcie_ep_map_msi_irq(struct pci_epc *epc, u8 fn, u8 vfn,
430 phys_addr_t addr, u8 interrupt_num,
431 u32 entry_size, u32 *msi_data,
432 u32 *msi_addr_offset)
433 {
434 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
435 u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
436 struct cdns_pcie *pcie = &ep->pcie;
437 u64 pci_addr, pci_addr_mask = 0xff;
438 u16 flags, mme, data, data_mask;
439 u8 msi_count;
440 int ret;
441 int i;
442
443 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
444
445 /* Check whether the MSI feature has been enabled by the PCI host. */
446 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
447 if (!(flags & PCI_MSI_FLAGS_ENABLE))
448 return -EINVAL;
449
450 /* Get the number of enabled MSIs */
451 mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
452 msi_count = 1 << mme;
453 if (!interrupt_num || interrupt_num > msi_count)
454 return -EINVAL;
455
456 /* Compute the data value to be written. */
457 data_mask = msi_count - 1;
458 data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
459 data = data & ~data_mask;
460
461 /* Get the PCI address where to write the data into. */
462 pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
463 pci_addr <<= 32;
464 pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
465 pci_addr &= GENMASK_ULL(63, 2);
466
467 for (i = 0; i < interrupt_num; i++) {
468 ret = cdns_pcie_ep_map_addr(epc, fn, vfn, addr,
469 pci_addr & ~pci_addr_mask,
470 entry_size);
471 if (ret)
472 return ret;
473 addr = addr + entry_size;
474 }
475
476 *msi_data = data;
477 *msi_addr_offset = pci_addr & pci_addr_mask;
478
479 return 0;
480 }
481
cdns_pcie_ep_send_msix_irq(struct cdns_pcie_ep * ep,u8 fn,u8 vfn,u16 interrupt_num)482 static int cdns_pcie_ep_send_msix_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
483 u16 interrupt_num)
484 {
485 u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
486 u32 tbl_offset, msg_data, reg;
487 struct cdns_pcie *pcie = &ep->pcie;
488 struct pci_epf_msix_tbl *msix_tbl;
489 struct cdns_pcie_epf *epf;
490 u64 pci_addr_mask = 0xff;
491 u64 msg_addr;
492 u16 flags;
493 u8 bir;
494
495 epf = &ep->epf[fn];
496 if (vfn > 0)
497 epf = &epf->epf[vfn - 1];
498
499 fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
500
501 /* Check whether the MSI-X feature has been enabled by the PCI host. */
502 flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSIX_FLAGS);
503 if (!(flags & PCI_MSIX_FLAGS_ENABLE))
504 return -EINVAL;
505
506 reg = cap + PCI_MSIX_TABLE;
507 tbl_offset = cdns_pcie_ep_fn_readl(pcie, fn, reg);
508 bir = tbl_offset & PCI_MSIX_TABLE_BIR;
509 tbl_offset &= PCI_MSIX_TABLE_OFFSET;
510
511 msix_tbl = epf->epf_bar[bir]->addr + tbl_offset;
512 msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
513 msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
514
515 /* Set the outbound region if needed. */
516 if (ep->irq_pci_addr != (msg_addr & ~pci_addr_mask) ||
517 ep->irq_pci_fn != fn) {
518 /* First region was reserved for IRQ writes. */
519 cdns_pcie_set_outbound_region(pcie, 0, fn, 0,
520 false,
521 ep->irq_phys_addr,
522 msg_addr & ~pci_addr_mask,
523 pci_addr_mask + 1);
524 ep->irq_pci_addr = (msg_addr & ~pci_addr_mask);
525 ep->irq_pci_fn = fn;
526 }
527 writel(msg_data, ep->irq_cpu_addr + (msg_addr & pci_addr_mask));
528
529 return 0;
530 }
531
cdns_pcie_ep_raise_irq(struct pci_epc * epc,u8 fn,u8 vfn,enum pci_epc_irq_type type,u16 interrupt_num)532 static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn, u8 vfn,
533 enum pci_epc_irq_type type,
534 u16 interrupt_num)
535 {
536 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
537 struct cdns_pcie *pcie = &ep->pcie;
538 struct device *dev = pcie->dev;
539
540 switch (type) {
541 case PCI_EPC_IRQ_LEGACY:
542 if (vfn > 0) {
543 dev_err(dev, "Cannot raise legacy interrupts for VF\n");
544 return -EINVAL;
545 }
546 return cdns_pcie_ep_send_legacy_irq(ep, fn, vfn, 0);
547
548 case PCI_EPC_IRQ_MSI:
549 return cdns_pcie_ep_send_msi_irq(ep, fn, vfn, interrupt_num);
550
551 case PCI_EPC_IRQ_MSIX:
552 return cdns_pcie_ep_send_msix_irq(ep, fn, vfn, interrupt_num);
553
554 default:
555 break;
556 }
557
558 return -EINVAL;
559 }
560
cdns_pcie_ep_start(struct pci_epc * epc)561 static int cdns_pcie_ep_start(struct pci_epc *epc)
562 {
563 struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
564 struct cdns_pcie *pcie = &ep->pcie;
565 struct device *dev = pcie->dev;
566 int max_epfs = sizeof(epc->function_num_map) * 8;
567 int ret, value, epf;
568
569 /*
570 * BIT(0) is hardwired to 1, hence function 0 is always enabled
571 * and can't be disabled anyway.
572 */
573 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, epc->function_num_map);
574
575 if (ep->quirk_disable_flr) {
576 for (epf = 0; epf < max_epfs; epf++) {
577 if (!(epc->function_num_map & BIT(epf)))
578 continue;
579
580 value = cdns_pcie_ep_fn_readl(pcie, epf,
581 CDNS_PCIE_EP_FUNC_DEV_CAP_OFFSET +
582 PCI_EXP_DEVCAP);
583 value &= ~PCI_EXP_DEVCAP_FLR;
584 cdns_pcie_ep_fn_writel(pcie, epf,
585 CDNS_PCIE_EP_FUNC_DEV_CAP_OFFSET +
586 PCI_EXP_DEVCAP, value);
587 }
588 }
589
590 ret = cdns_pcie_start_link(pcie);
591 if (ret) {
592 dev_err(dev, "Failed to start link\n");
593 return ret;
594 }
595
596 return 0;
597 }
598
599 static const struct pci_epc_features cdns_pcie_epc_vf_features = {
600 .linkup_notifier = false,
601 .msi_capable = true,
602 .msix_capable = true,
603 .align = 65536,
604 };
605
606 static const struct pci_epc_features cdns_pcie_epc_features = {
607 .linkup_notifier = false,
608 .msi_capable = true,
609 .msix_capable = true,
610 .align = 256,
611 };
612
613 static const struct pci_epc_features*
cdns_pcie_ep_get_features(struct pci_epc * epc,u8 func_no,u8 vfunc_no)614 cdns_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
615 {
616 if (!vfunc_no)
617 return &cdns_pcie_epc_features;
618
619 return &cdns_pcie_epc_vf_features;
620 }
621
622 static const struct pci_epc_ops cdns_pcie_epc_ops = {
623 .write_header = cdns_pcie_ep_write_header,
624 .set_bar = cdns_pcie_ep_set_bar,
625 .clear_bar = cdns_pcie_ep_clear_bar,
626 .map_addr = cdns_pcie_ep_map_addr,
627 .unmap_addr = cdns_pcie_ep_unmap_addr,
628 .set_msi = cdns_pcie_ep_set_msi,
629 .get_msi = cdns_pcie_ep_get_msi,
630 .set_msix = cdns_pcie_ep_set_msix,
631 .get_msix = cdns_pcie_ep_get_msix,
632 .raise_irq = cdns_pcie_ep_raise_irq,
633 .map_msi_irq = cdns_pcie_ep_map_msi_irq,
634 .start = cdns_pcie_ep_start,
635 .get_features = cdns_pcie_ep_get_features,
636 };
637
638
cdns_pcie_ep_setup(struct cdns_pcie_ep * ep)639 int cdns_pcie_ep_setup(struct cdns_pcie_ep *ep)
640 {
641 struct device *dev = ep->pcie.dev;
642 struct platform_device *pdev = to_platform_device(dev);
643 struct device_node *np = dev->of_node;
644 struct cdns_pcie *pcie = &ep->pcie;
645 struct cdns_pcie_epf *epf;
646 struct resource *res;
647 struct pci_epc *epc;
648 int ret;
649 int i;
650
651 pcie->is_rc = false;
652
653 pcie->reg_base = devm_platform_ioremap_resource_byname(pdev, "reg");
654 if (IS_ERR(pcie->reg_base)) {
655 dev_err(dev, "missing \"reg\"\n");
656 return PTR_ERR(pcie->reg_base);
657 }
658
659 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
660 if (!res) {
661 dev_err(dev, "missing \"mem\"\n");
662 return -EINVAL;
663 }
664 pcie->mem_res = res;
665
666 ep->max_regions = CDNS_PCIE_MAX_OB;
667 of_property_read_u32(np, "cdns,max-outbound-regions", &ep->max_regions);
668
669 ep->ob_addr = devm_kcalloc(dev,
670 ep->max_regions, sizeof(*ep->ob_addr),
671 GFP_KERNEL);
672 if (!ep->ob_addr)
673 return -ENOMEM;
674
675 /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
676 cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
677
678 epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
679 if (IS_ERR(epc)) {
680 dev_err(dev, "failed to create epc device\n");
681 return PTR_ERR(epc);
682 }
683
684 epc_set_drvdata(epc, ep);
685
686 if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0)
687 epc->max_functions = 1;
688
689 ep->epf = devm_kcalloc(dev, epc->max_functions, sizeof(*ep->epf),
690 GFP_KERNEL);
691 if (!ep->epf)
692 return -ENOMEM;
693
694 epc->max_vfs = devm_kcalloc(dev, epc->max_functions,
695 sizeof(*epc->max_vfs), GFP_KERNEL);
696 if (!epc->max_vfs)
697 return -ENOMEM;
698
699 ret = of_property_read_u8_array(np, "max-virtual-functions",
700 epc->max_vfs, epc->max_functions);
701 if (ret == 0) {
702 for (i = 0; i < epc->max_functions; i++) {
703 epf = &ep->epf[i];
704 if (epc->max_vfs[i] == 0)
705 continue;
706 epf->epf = devm_kcalloc(dev, epc->max_vfs[i],
707 sizeof(*ep->epf), GFP_KERNEL);
708 if (!epf->epf)
709 return -ENOMEM;
710 }
711 }
712
713 ret = pci_epc_mem_init(epc, pcie->mem_res->start,
714 resource_size(pcie->mem_res), PAGE_SIZE);
715 if (ret < 0) {
716 dev_err(dev, "failed to initialize the memory space\n");
717 return ret;
718 }
719
720 ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
721 SZ_128K);
722 if (!ep->irq_cpu_addr) {
723 dev_err(dev, "failed to reserve memory space for MSI\n");
724 ret = -ENOMEM;
725 goto free_epc_mem;
726 }
727 ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
728 /* Reserve region 0 for IRQs */
729 set_bit(0, &ep->ob_region_map);
730
731 if (ep->quirk_detect_quiet_flag)
732 cdns_pcie_detect_quiet_min_delay_set(&ep->pcie);
733
734 spin_lock_init(&ep->lock);
735
736 return 0;
737
738 free_epc_mem:
739 pci_epc_mem_exit(epc);
740
741 return ret;
742 }
743