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/kernel.h>
10#include <linux/of.h>
11
12#include "pcie-designware.h"
13#include <linux/pci-epc.h>
14#include <linux/pci-epf.h>
15
16#include "../../pci.h"
17
18void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
19{
20	struct pci_epc *epc = ep->epc;
21
22	pci_epc_linkup(epc);
23}
24EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
25
26void dw_pcie_ep_init_notify(struct dw_pcie_ep *ep)
27{
28	struct pci_epc *epc = ep->epc;
29
30	pci_epc_init_notify(epc);
31}
32EXPORT_SYMBOL_GPL(dw_pcie_ep_init_notify);
33
34struct dw_pcie_ep_func *
35dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
36{
37	struct dw_pcie_ep_func *ep_func;
38
39	list_for_each_entry(ep_func, &ep->func_list, list) {
40		if (ep_func->func_no == func_no)
41			return ep_func;
42	}
43
44	return NULL;
45}
46
47static unsigned int dw_pcie_ep_func_select(struct dw_pcie_ep *ep, u8 func_no)
48{
49	unsigned int func_offset = 0;
50
51	if (ep->ops->func_conf_select)
52		func_offset = ep->ops->func_conf_select(ep, func_no);
53
54	return func_offset;
55}
56
57static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
58				   enum pci_barno bar, int flags)
59{
60	u32 reg;
61	unsigned int func_offset = 0;
62	struct dw_pcie_ep *ep = &pci->ep;
63
64	func_offset = dw_pcie_ep_func_select(ep, func_no);
65
66	reg = func_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
67	dw_pcie_dbi_ro_wr_en(pci);
68	dw_pcie_writel_dbi2(pci, reg, 0x0);
69	dw_pcie_writel_dbi(pci, reg, 0x0);
70	if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
71		dw_pcie_writel_dbi2(pci, reg + 4, 0x0);
72		dw_pcie_writel_dbi(pci, reg + 4, 0x0);
73	}
74	dw_pcie_dbi_ro_wr_dis(pci);
75}
76
77void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
78{
79	u8 func_no, funcs;
80
81	funcs = pci->ep.epc->max_functions;
82
83	for (func_no = 0; func_no < funcs; func_no++)
84		__dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
85}
86
87static 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
128static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no,
129				   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);
133	unsigned int func_offset = 0;
134
135	func_offset = dw_pcie_ep_func_select(ep, func_no);
136
137	dw_pcie_dbi_ro_wr_en(pci);
138	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,
143			   hdr->subclass_code | hdr->baseclass_code << 8);
144	dw_pcie_writeb_dbi(pci, func_offset + PCI_CACHE_LINE_SIZE,
145			   hdr->cache_line_size);
146	dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_VENDOR_ID,
147			   hdr->subsys_vendor_id);
148	dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_ID, hdr->subsys_id);
149	dw_pcie_writeb_dbi(pci, func_offset + PCI_INTERRUPT_PIN,
150			   hdr->interrupt_pin);
151	dw_pcie_dbi_ro_wr_dis(pci);
152
153	return 0;
154}
155
156static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no,
157				  enum pci_barno bar, dma_addr_t cpu_addr,
158				  enum dw_pcie_as_type as_type)
159{
160	int ret;
161	u32 free_win;
162	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
163
164	free_win = find_first_zero_bit(ep->ib_window_map, ep->num_ib_windows);
165	if (free_win >= ep->num_ib_windows) {
166		dev_err(pci->dev, "No free inbound window\n");
167		return -EINVAL;
168	}
169
170	ret = dw_pcie_prog_inbound_atu(pci, func_no, free_win, bar, cpu_addr,
171				       as_type);
172	if (ret < 0) {
173		dev_err(pci->dev, "Failed to program IB window\n");
174		return ret;
175	}
176
177	ep->bar_to_atu[bar] = free_win;
178	set_bit(free_win, ep->ib_window_map);
179
180	return 0;
181}
182
183static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
184				   phys_addr_t phys_addr,
185				   u64 pci_addr, size_t size)
186{
187	u32 free_win;
188	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
189
190	free_win = find_first_zero_bit(ep->ob_window_map, ep->num_ob_windows);
191	if (free_win >= ep->num_ob_windows) {
192		dev_err(pci->dev, "No free outbound window\n");
193		return -EINVAL;
194	}
195
196	dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
197				     phys_addr, pci_addr, size);
198
199	set_bit(free_win, ep->ob_window_map);
200	ep->outbound_addr[free_win] = phys_addr;
201
202	return 0;
203}
204
205static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no,
206				 struct pci_epf_bar *epf_bar)
207{
208	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
209	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
210	enum pci_barno bar = epf_bar->barno;
211	u32 atu_index = ep->bar_to_atu[bar];
212
213	__dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
214
215	dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
216	clear_bit(atu_index, ep->ib_window_map);
217	ep->epf_bar[bar] = NULL;
218}
219
220static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no,
221			      struct pci_epf_bar *epf_bar)
222{
223	int ret;
224	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
225	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
226	enum pci_barno bar = epf_bar->barno;
227	size_t size = epf_bar->size;
228	int flags = epf_bar->flags;
229	enum dw_pcie_as_type as_type;
230	u32 reg;
231	unsigned int func_offset = 0;
232
233	func_offset = dw_pcie_ep_func_select(ep, func_no);
234
235	reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
236
237	if (!(flags & PCI_BASE_ADDRESS_SPACE))
238		as_type = DW_PCIE_AS_MEM;
239	else
240		as_type = DW_PCIE_AS_IO;
241
242	ret = dw_pcie_ep_inbound_atu(ep, func_no, bar,
243				     epf_bar->phys_addr, as_type);
244	if (ret)
245		return ret;
246
247	dw_pcie_dbi_ro_wr_en(pci);
248
249	dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
250	dw_pcie_writel_dbi(pci, reg, flags);
251
252	if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
253		dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
254		dw_pcie_writel_dbi(pci, reg + 4, 0);
255	}
256
257	ep->epf_bar[bar] = epf_bar;
258	dw_pcie_dbi_ro_wr_dis(pci);
259
260	return 0;
261}
262
263static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
264			      u32 *atu_index)
265{
266	u32 index;
267
268	for (index = 0; index < ep->num_ob_windows; index++) {
269		if (ep->outbound_addr[index] != addr)
270			continue;
271		*atu_index = index;
272		return 0;
273	}
274
275	return -EINVAL;
276}
277
278static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no,
279				  phys_addr_t addr)
280{
281	int ret;
282	u32 atu_index;
283	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
284	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
285
286	ret = dw_pcie_find_index(ep, addr, &atu_index);
287	if (ret < 0)
288		return;
289
290	dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND);
291	clear_bit(atu_index, ep->ob_window_map);
292}
293
294static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no,
295			       phys_addr_t addr,
296			       u64 pci_addr, size_t size)
297{
298	int ret;
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_ep_outbound_atu(ep, func_no, addr, pci_addr, size);
303	if (ret) {
304		dev_err(pci->dev, "Failed to enable address\n");
305		return ret;
306	}
307
308	return 0;
309}
310
311static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no)
312{
313	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
314	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
315	u32 val, reg;
316	unsigned int func_offset = 0;
317	struct dw_pcie_ep_func *ep_func;
318
319	ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
320	if (!ep_func || !ep_func->msi_cap)
321		return -EINVAL;
322
323	func_offset = dw_pcie_ep_func_select(ep, func_no);
324
325	reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
326	val = dw_pcie_readw_dbi(pci, reg);
327	if (!(val & PCI_MSI_FLAGS_ENABLE))
328		return -EINVAL;
329
330	val = (val & PCI_MSI_FLAGS_QSIZE) >> 4;
331
332	return val;
333}
334
335static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
336{
337	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
338	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
339	u32 val, reg;
340	unsigned int func_offset = 0;
341	struct dw_pcie_ep_func *ep_func;
342
343	ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
344	if (!ep_func || !ep_func->msi_cap)
345		return -EINVAL;
346
347	func_offset = dw_pcie_ep_func_select(ep, func_no);
348
349	reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
350	val = dw_pcie_readw_dbi(pci, reg);
351	val &= ~PCI_MSI_FLAGS_QMASK;
352	val |= (interrupts << 1) & PCI_MSI_FLAGS_QMASK;
353	dw_pcie_dbi_ro_wr_en(pci);
354	dw_pcie_writew_dbi(pci, reg, val);
355	dw_pcie_dbi_ro_wr_dis(pci);
356
357	return 0;
358}
359
360static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no)
361{
362	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
363	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
364	u32 val, reg;
365	unsigned int func_offset = 0;
366	struct dw_pcie_ep_func *ep_func;
367
368	ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
369	if (!ep_func || !ep_func->msix_cap)
370		return -EINVAL;
371
372	func_offset = dw_pcie_ep_func_select(ep, func_no);
373
374	reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
375	val = dw_pcie_readw_dbi(pci, reg);
376	if (!(val & PCI_MSIX_FLAGS_ENABLE))
377		return -EINVAL;
378
379	val &= PCI_MSIX_FLAGS_QSIZE;
380
381	return val;
382}
383
384static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,
385			       enum pci_barno bir, u32 offset)
386{
387	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
388	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
389	u32 val, reg;
390	unsigned int func_offset = 0;
391	struct dw_pcie_ep_func *ep_func;
392
393	ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
394	if (!ep_func || !ep_func->msix_cap)
395		return -EINVAL;
396
397	dw_pcie_dbi_ro_wr_en(pci);
398
399	func_offset = dw_pcie_ep_func_select(ep, func_no);
400
401	reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
402	val = dw_pcie_readw_dbi(pci, reg);
403	val &= ~PCI_MSIX_FLAGS_QSIZE;
404	val |= interrupts;
405	dw_pcie_writew_dbi(pci, reg, val);
406
407	reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
408	val = offset | bir;
409	dw_pcie_writel_dbi(pci, reg, val);
410
411	reg = ep_func->msix_cap + func_offset + PCI_MSIX_PBA;
412	val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
413	dw_pcie_writel_dbi(pci, reg, val);
414
415	dw_pcie_dbi_ro_wr_dis(pci);
416
417	return 0;
418}
419
420static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no,
421				enum pci_epc_irq_type type, u16 interrupt_num)
422{
423	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
424
425	if (!ep->ops->raise_irq)
426		return -EINVAL;
427
428	return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
429}
430
431static void dw_pcie_ep_stop(struct pci_epc *epc)
432{
433	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
434	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
435
436	if (!pci->ops->stop_link)
437		return;
438
439	pci->ops->stop_link(pci);
440}
441
442static int dw_pcie_ep_start(struct pci_epc *epc)
443{
444	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
445	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
446
447	if (!pci->ops->start_link)
448		return -EINVAL;
449
450	return pci->ops->start_link(pci);
451}
452
453static const struct pci_epc_features*
454dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no)
455{
456	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
457
458	if (!ep->ops->get_features)
459		return NULL;
460
461	return ep->ops->get_features(ep);
462}
463
464static const struct pci_epc_ops epc_ops = {
465	.write_header		= dw_pcie_ep_write_header,
466	.set_bar		= dw_pcie_ep_set_bar,
467	.clear_bar		= dw_pcie_ep_clear_bar,
468	.map_addr		= dw_pcie_ep_map_addr,
469	.unmap_addr		= dw_pcie_ep_unmap_addr,
470	.set_msi		= dw_pcie_ep_set_msi,
471	.get_msi		= dw_pcie_ep_get_msi,
472	.set_msix		= dw_pcie_ep_set_msix,
473	.get_msix		= dw_pcie_ep_get_msix,
474	.raise_irq		= dw_pcie_ep_raise_irq,
475	.start			= dw_pcie_ep_start,
476	.stop			= dw_pcie_ep_stop,
477	.get_features		= dw_pcie_ep_get_features,
478};
479
480int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
481{
482	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
483	struct device *dev = pci->dev;
484
485	dev_err(dev, "EP cannot trigger legacy IRQs\n");
486
487	return -EINVAL;
488}
489
490int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
491			     u8 interrupt_num)
492{
493	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
494	struct dw_pcie_ep_func *ep_func;
495	struct pci_epc *epc = ep->epc;
496	unsigned int aligned_offset;
497	unsigned int func_offset = 0;
498	u16 msg_ctrl, msg_data;
499	u32 msg_addr_lower, msg_addr_upper, reg;
500	u64 msg_addr;
501	bool has_upper;
502	int ret;
503
504	ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
505	if (!ep_func || !ep_func->msi_cap)
506		return -EINVAL;
507
508	func_offset = dw_pcie_ep_func_select(ep, func_no);
509
510	/* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
511	reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
512	msg_ctrl = dw_pcie_readw_dbi(pci, reg);
513	has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
514	reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
515	msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
516	if (has_upper) {
517		reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
518		msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
519		reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
520		msg_data = dw_pcie_readw_dbi(pci, reg);
521	} else {
522		msg_addr_upper = 0;
523		reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
524		msg_data = dw_pcie_readw_dbi(pci, reg);
525	}
526	aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
527	msg_addr = ((u64)msg_addr_upper) << 32 |
528			(msg_addr_lower & ~aligned_offset);
529	ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys, msg_addr,
530				  epc->mem->window.page_size);
531	if (ret)
532		return ret;
533
534	writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
535
536	dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
537
538	return 0;
539}
540
541int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
542				       u16 interrupt_num)
543{
544	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
545	struct dw_pcie_ep_func *ep_func;
546	u32 msg_data;
547
548	ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
549	if (!ep_func || !ep_func->msix_cap)
550		return -EINVAL;
551
552	msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
553		   (interrupt_num - 1);
554
555	dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
556
557	return 0;
558}
559
560int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
561			      u16 interrupt_num)
562{
563	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
564	struct dw_pcie_ep_func *ep_func;
565	struct pci_epf_msix_tbl *msix_tbl;
566	struct pci_epc *epc = ep->epc;
567	unsigned int func_offset = 0;
568	u32 reg, msg_data, vec_ctrl;
569	unsigned int aligned_offset;
570	u32 tbl_offset;
571	u64 msg_addr;
572	int ret;
573	u8 bir;
574
575	ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
576	if (!ep_func || !ep_func->msix_cap)
577		return -EINVAL;
578
579	func_offset = dw_pcie_ep_func_select(ep, func_no);
580
581	reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
582	tbl_offset = dw_pcie_readl_dbi(pci, reg);
583	bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
584	tbl_offset &= PCI_MSIX_TABLE_OFFSET;
585
586	msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
587	msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
588	msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
589	vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
590
591	if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
592		dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
593		return -EPERM;
594	}
595
596	aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
597	msg_addr = ALIGN_DOWN(msg_addr, epc->mem->window.page_size);
598	ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys,  msg_addr,
599				  epc->mem->window.page_size);
600	if (ret)
601		return ret;
602
603	writel(msg_data, ep->msi_mem + aligned_offset);
604
605	dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
606
607	return 0;
608}
609
610void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
611{
612	struct pci_epc *epc = ep->epc;
613
614	pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
615			      epc->mem->window.page_size);
616
617	pci_epc_mem_exit(epc);
618}
619
620static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
621{
622	u32 header;
623	int pos = PCI_CFG_SPACE_SIZE;
624
625	while (pos) {
626		header = dw_pcie_readl_dbi(pci, pos);
627		if (PCI_EXT_CAP_ID(header) == cap)
628			return pos;
629
630		pos = PCI_EXT_CAP_NEXT(header);
631		if (!pos)
632			break;
633	}
634
635	return 0;
636}
637
638int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
639{
640	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
641	unsigned int offset;
642	unsigned int nbars;
643	u8 hdr_type;
644	u32 reg;
645	int i;
646
647	hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
648		   PCI_HEADER_TYPE_MASK;
649	if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
650		dev_err(pci->dev,
651			"PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
652			hdr_type);
653		return -EIO;
654	}
655
656	offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
657
658	dw_pcie_dbi_ro_wr_en(pci);
659
660	if (offset) {
661		reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
662		nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
663			PCI_REBAR_CTRL_NBAR_SHIFT;
664
665		for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
666			dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, 0x0);
667	}
668
669	dw_pcie_setup(pci);
670	dw_pcie_dbi_ro_wr_dis(pci);
671
672	return 0;
673}
674EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
675
676int dw_pcie_ep_init(struct dw_pcie_ep *ep)
677{
678	int ret;
679	void *addr;
680	u8 func_no;
681	struct pci_epc *epc;
682	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
683	struct device *dev = pci->dev;
684	struct device_node *np = dev->of_node;
685	const struct pci_epc_features *epc_features;
686	struct dw_pcie_ep_func *ep_func;
687
688	INIT_LIST_HEAD(&ep->func_list);
689
690	if (!pci->dbi_base || !pci->dbi_base2) {
691		dev_err(dev, "dbi_base/dbi_base2 is not populated\n");
692		return -EINVAL;
693	}
694
695	ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows);
696	if (ret < 0) {
697		dev_err(dev, "Unable to read *num-ib-windows* property\n");
698		return ret;
699	}
700	if (ep->num_ib_windows > MAX_IATU_IN) {
701		dev_err(dev, "Invalid *num-ib-windows*\n");
702		return -EINVAL;
703	}
704
705	ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows);
706	if (ret < 0) {
707		dev_err(dev, "Unable to read *num-ob-windows* property\n");
708		return ret;
709	}
710	if (ep->num_ob_windows > MAX_IATU_OUT) {
711		dev_err(dev, "Invalid *num-ob-windows*\n");
712		return -EINVAL;
713	}
714
715	ep->ib_window_map = devm_kcalloc(dev,
716					 BITS_TO_LONGS(ep->num_ib_windows),
717					 sizeof(long),
718					 GFP_KERNEL);
719	if (!ep->ib_window_map)
720		return -ENOMEM;
721
722	ep->ob_window_map = devm_kcalloc(dev,
723					 BITS_TO_LONGS(ep->num_ob_windows),
724					 sizeof(long),
725					 GFP_KERNEL);
726	if (!ep->ob_window_map)
727		return -ENOMEM;
728
729	addr = devm_kcalloc(dev, ep->num_ob_windows, sizeof(phys_addr_t),
730			    GFP_KERNEL);
731	if (!addr)
732		return -ENOMEM;
733	ep->outbound_addr = addr;
734
735	if (pci->link_gen < 1)
736		pci->link_gen = of_pci_get_max_link_speed(np);
737
738	epc = devm_pci_epc_create(dev, &epc_ops);
739	if (IS_ERR(epc)) {
740		dev_err(dev, "Failed to create epc device\n");
741		return PTR_ERR(epc);
742	}
743
744	ep->epc = epc;
745	epc_set_drvdata(epc, ep);
746
747	ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
748	if (ret < 0)
749		epc->max_functions = 1;
750
751	for (func_no = 0; func_no < epc->max_functions; func_no++) {
752		ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
753		if (!ep_func)
754			return -ENOMEM;
755
756		ep_func->func_no = func_no;
757		ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
758							      PCI_CAP_ID_MSI);
759		ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
760							       PCI_CAP_ID_MSIX);
761
762		list_add_tail(&ep_func->list, &ep->func_list);
763	}
764
765	if (ep->ops->ep_init)
766		ep->ops->ep_init(ep);
767
768	ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
769			       ep->page_size);
770	if (ret < 0) {
771		dev_err(dev, "Failed to initialize address space\n");
772		return ret;
773	}
774
775	ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
776					     epc->mem->window.page_size);
777	if (!ep->msi_mem) {
778		ret = -ENOMEM;
779		dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
780		goto err_exit_epc_mem;
781	}
782
783	if (ep->ops->get_features) {
784		epc_features = ep->ops->get_features(ep);
785		if (epc_features->core_init_notifier)
786			return 0;
787	}
788
789	ret = dw_pcie_ep_init_complete(ep);
790	if (ret)
791		goto err_free_epc_mem;
792
793	return 0;
794
795err_free_epc_mem:
796	pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
797			      epc->mem->window.page_size);
798
799err_exit_epc_mem:
800	pci_epc_mem_exit(epc);
801
802	return ret;
803}
804EXPORT_SYMBOL_GPL(dw_pcie_ep_init);
805