Linux Audio

Check our new training course

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