blob: 573730d490a7d2ba214d1122df689829ec457a0b [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001/* SPDX-License-Identifier: GPL-2.0 */
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +05302/**
3 * PCI Endpoint *Controller* (EPC) header file
4 *
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +05307 */
8
9#ifndef __LINUX_PCI_EPC_H
10#define __LINUX_PCI_EPC_H
11
12#include <linux/pci-epf.h>
13
14struct pci_epc;
15
16enum pci_epc_irq_type {
17 PCI_EPC_IRQ_UNKNOWN,
18 PCI_EPC_IRQ_LEGACY,
19 PCI_EPC_IRQ_MSI,
20};
21
22/**
23 * struct pci_epc_ops - set of function pointers for performing EPC operations
24 * @write_header: ops to populate configuration space header
25 * @set_bar: ops to configure the BAR
26 * @clear_bar: ops to reset the BAR
27 * @map_addr: ops to map CPU address to PCI address
28 * @unmap_addr: ops to unmap CPU address and PCI address
29 * @set_msi: ops to set the requested number of MSI interrupts in the MSI
30 * capability register
31 * @get_msi: ops to get the number of MSI interrupts allocated by the RC from
32 * the MSI capability register
33 * @raise_irq: ops to raise a legacy or MSI interrupt
34 * @start: ops to start the PCI link
35 * @stop: ops to stop the PCI link
36 * @owner: the module owner containing the ops
37 */
38struct pci_epc_ops {
39 int (*write_header)(struct pci_epc *pci_epc,
40 struct pci_epf_header *hdr);
41 int (*set_bar)(struct pci_epc *epc, enum pci_barno bar,
42 dma_addr_t bar_phys, size_t size, int flags);
43 void (*clear_bar)(struct pci_epc *epc, enum pci_barno bar);
44 int (*map_addr)(struct pci_epc *epc, phys_addr_t addr,
45 u64 pci_addr, size_t size);
46 void (*unmap_addr)(struct pci_epc *epc, phys_addr_t addr);
47 int (*set_msi)(struct pci_epc *epc, u8 interrupts);
48 int (*get_msi)(struct pci_epc *epc);
49 int (*raise_irq)(struct pci_epc *pci_epc,
50 enum pci_epc_irq_type type, u8 interrupt_num);
51 int (*start)(struct pci_epc *epc);
52 void (*stop)(struct pci_epc *epc);
53 struct module *owner;
54};
55
56/**
57 * struct pci_epc_mem - address space of the endpoint controller
58 * @phys_base: physical base address of the PCI address space
59 * @size: the size of the PCI address space
60 * @bitmap: bitmap to manage the PCI address space
61 * @pages: number of bits representing the address region
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +053062 * @page_size: size of each page
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053063 */
64struct pci_epc_mem {
65 phys_addr_t phys_base;
66 size_t size;
67 unsigned long *bitmap;
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +053068 size_t page_size;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053069 int pages;
70};
71
72/**
73 * struct pci_epc - represents the PCI EPC device
74 * @dev: PCI EPC device
75 * @pci_epf: list of endpoint functions present in this EPC device
76 * @ops: function pointers for performing endpoint operations
77 * @mem: address space of the endpoint controller
78 * @max_functions: max number of functions that can be configured in this EPC
Kishon Vijay Abraham I3a401a22017-03-27 15:15:01 +053079 * @group: configfs group representing the PCI EPC device
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053080 * @lock: spinlock to protect pci_epc ops
81 */
82struct pci_epc {
83 struct device dev;
84 struct list_head pci_epf;
85 const struct pci_epc_ops *ops;
86 struct pci_epc_mem *mem;
87 u8 max_functions;
Kishon Vijay Abraham I3a401a22017-03-27 15:15:01 +053088 struct config_group *group;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053089 /* spinlock to protect against concurrent access of EP controller */
90 spinlock_t lock;
91};
92
93#define to_pci_epc(device) container_of((device), struct pci_epc, dev)
94
95#define pci_epc_create(dev, ops) \
96 __pci_epc_create((dev), (ops), THIS_MODULE)
97#define devm_pci_epc_create(dev, ops) \
98 __devm_pci_epc_create((dev), (ops), THIS_MODULE)
99
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +0530100#define pci_epc_mem_init(epc, phys_addr, size) \
101 __pci_epc_mem_init((epc), (phys_addr), (size), PAGE_SIZE)
102
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530103static inline void epc_set_drvdata(struct pci_epc *epc, void *data)
104{
105 dev_set_drvdata(&epc->dev, data);
106}
107
108static inline void *epc_get_drvdata(struct pci_epc *epc)
109{
110 return dev_get_drvdata(&epc->dev);
111}
112
113struct pci_epc *
114__devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
115 struct module *owner);
116struct pci_epc *
117__pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
118 struct module *owner);
119void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc);
120void pci_epc_destroy(struct pci_epc *epc);
121int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf);
122void pci_epc_linkup(struct pci_epc *epc);
123void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf);
124int pci_epc_write_header(struct pci_epc *epc, struct pci_epf_header *hdr);
125int pci_epc_set_bar(struct pci_epc *epc, enum pci_barno bar,
126 dma_addr_t bar_phys, size_t size, int flags);
127void pci_epc_clear_bar(struct pci_epc *epc, int bar);
128int pci_epc_map_addr(struct pci_epc *epc, phys_addr_t phys_addr,
129 u64 pci_addr, size_t size);
130void pci_epc_unmap_addr(struct pci_epc *epc, phys_addr_t phys_addr);
131int pci_epc_set_msi(struct pci_epc *epc, u8 interrupts);
132int pci_epc_get_msi(struct pci_epc *epc);
133int pci_epc_raise_irq(struct pci_epc *epc, enum pci_epc_irq_type type,
134 u8 interrupt_num);
135int pci_epc_start(struct pci_epc *epc);
136void pci_epc_stop(struct pci_epc *epc);
137struct pci_epc *pci_epc_get(const char *epc_name);
138void pci_epc_put(struct pci_epc *epc);
139
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +0530140int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_addr, size_t size,
141 size_t page_size);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530142void pci_epc_mem_exit(struct pci_epc *epc);
143void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
144 phys_addr_t *phys_addr, size_t size);
145void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
146 void __iomem *virt_addr, size_t size);
147#endif /* __LINUX_PCI_EPC_H */