blob: e58e2de8936bc5a8d4593920c0356dc8f6137f22 [file] [log] [blame]
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +05301/**
2 * PCI Endpoint *Controller* (EPC) header file
3 *
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
10 */
11
12#ifndef __LINUX_PCI_EPC_H
13#define __LINUX_PCI_EPC_H
14
15#include <linux/pci-epf.h>
16
17struct pci_epc;
18
19enum pci_epc_irq_type {
20 PCI_EPC_IRQ_UNKNOWN,
21 PCI_EPC_IRQ_LEGACY,
22 PCI_EPC_IRQ_MSI,
23};
24
25/**
26 * struct pci_epc_ops - set of function pointers for performing EPC operations
27 * @write_header: ops to populate configuration space header
28 * @set_bar: ops to configure the BAR
29 * @clear_bar: ops to reset the BAR
30 * @map_addr: ops to map CPU address to PCI address
31 * @unmap_addr: ops to unmap CPU address and PCI address
32 * @set_msi: ops to set the requested number of MSI interrupts in the MSI
33 * capability register
34 * @get_msi: ops to get the number of MSI interrupts allocated by the RC from
35 * the MSI capability register
36 * @raise_irq: ops to raise a legacy or MSI interrupt
37 * @start: ops to start the PCI link
38 * @stop: ops to stop the PCI link
39 * @owner: the module owner containing the ops
40 */
41struct pci_epc_ops {
Cyrille Pitchen44947382018-01-30 21:56:56 +010042 int (*write_header)(struct pci_epc *epc, u8 func_no,
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053043 struct pci_epf_header *hdr);
Cyrille Pitchen44947382018-01-30 21:56:56 +010044 int (*set_bar)(struct pci_epc *epc, u8 func_no,
45 enum pci_barno bar,
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053046 dma_addr_t bar_phys, size_t size, int flags);
Cyrille Pitchen44947382018-01-30 21:56:56 +010047 void (*clear_bar)(struct pci_epc *epc, u8 func_no,
48 enum pci_barno bar);
49 int (*map_addr)(struct pci_epc *epc, u8 func_no,
50 phys_addr_t addr, u64 pci_addr, size_t size);
51 void (*unmap_addr)(struct pci_epc *epc, u8 func_no,
52 phys_addr_t addr);
53 int (*set_msi)(struct pci_epc *epc, u8 func_no, u8 interrupts);
54 int (*get_msi)(struct pci_epc *epc, u8 func_no);
55 int (*raise_irq)(struct pci_epc *epc, u8 func_no,
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053056 enum pci_epc_irq_type type, u8 interrupt_num);
57 int (*start)(struct pci_epc *epc);
58 void (*stop)(struct pci_epc *epc);
59 struct module *owner;
60};
61
62/**
63 * struct pci_epc_mem - address space of the endpoint controller
64 * @phys_base: physical base address of the PCI address space
65 * @size: the size of the PCI address space
66 * @bitmap: bitmap to manage the PCI address space
67 * @pages: number of bits representing the address region
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +053068 * @page_size: size of each page
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053069 */
70struct pci_epc_mem {
71 phys_addr_t phys_base;
72 size_t size;
73 unsigned long *bitmap;
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +053074 size_t page_size;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053075 int pages;
76};
77
78/**
79 * struct pci_epc - represents the PCI EPC device
80 * @dev: PCI EPC device
81 * @pci_epf: list of endpoint functions present in this EPC device
82 * @ops: function pointers for performing endpoint operations
83 * @mem: address space of the endpoint controller
84 * @max_functions: max number of functions that can be configured in this EPC
Kishon Vijay Abraham I3a401a22017-03-27 15:15:01 +053085 * @group: configfs group representing the PCI EPC device
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053086 * @lock: spinlock to protect pci_epc ops
87 */
88struct pci_epc {
89 struct device dev;
90 struct list_head pci_epf;
91 const struct pci_epc_ops *ops;
92 struct pci_epc_mem *mem;
93 u8 max_functions;
Kishon Vijay Abraham I3a401a22017-03-27 15:15:01 +053094 struct config_group *group;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053095 /* spinlock to protect against concurrent access of EP controller */
96 spinlock_t lock;
97};
98
99#define to_pci_epc(device) container_of((device), struct pci_epc, dev)
100
101#define pci_epc_create(dev, ops) \
102 __pci_epc_create((dev), (ops), THIS_MODULE)
103#define devm_pci_epc_create(dev, ops) \
104 __devm_pci_epc_create((dev), (ops), THIS_MODULE)
105
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +0530106#define pci_epc_mem_init(epc, phys_addr, size) \
107 __pci_epc_mem_init((epc), (phys_addr), (size), PAGE_SIZE)
108
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530109static inline void epc_set_drvdata(struct pci_epc *epc, void *data)
110{
111 dev_set_drvdata(&epc->dev, data);
112}
113
114static inline void *epc_get_drvdata(struct pci_epc *epc)
115{
116 return dev_get_drvdata(&epc->dev);
117}
118
119struct pci_epc *
120__devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
121 struct module *owner);
122struct pci_epc *
123__pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
124 struct module *owner);
125void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc);
126void pci_epc_destroy(struct pci_epc *epc);
127int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf);
128void pci_epc_linkup(struct pci_epc *epc);
129void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf);
Cyrille Pitchen44947382018-01-30 21:56:56 +0100130int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
131 struct pci_epf_header *hdr);
132int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
133 enum pci_barno bar,
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530134 dma_addr_t bar_phys, size_t size, int flags);
Cyrille Pitchen44947382018-01-30 21:56:56 +0100135void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, int bar);
136int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
137 phys_addr_t phys_addr,
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530138 u64 pci_addr, size_t size);
Cyrille Pitchen44947382018-01-30 21:56:56 +0100139void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
140 phys_addr_t phys_addr);
141int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts);
142int pci_epc_get_msi(struct pci_epc *epc, u8 func_no);
143int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no,
144 enum pci_epc_irq_type type, u8 interrupt_num);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530145int pci_epc_start(struct pci_epc *epc);
146void pci_epc_stop(struct pci_epc *epc);
147struct pci_epc *pci_epc_get(const char *epc_name);
148void pci_epc_put(struct pci_epc *epc);
149
Kishon Vijay Abraham I52c92852017-08-18 20:27:56 +0530150int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_addr, size_t size,
151 size_t page_size);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530152void pci_epc_mem_exit(struct pci_epc *epc);
153void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
154 phys_addr_t *phys_addr, size_t size);
155void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
156 void __iomem *virt_addr, size_t size);
157#endif /* __LINUX_PCI_EPC_H */