1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Endpoint *Controller* (EPC) library
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
18 static struct class *pci_epc_class
;
20 static void devm_pci_epc_release(struct device
*dev
, void *res
)
22 struct pci_epc
*epc
= *(struct pci_epc
**)res
;
27 static int devm_pci_epc_match(struct device
*dev
, void *res
, void *match_data
)
29 struct pci_epc
**epc
= res
;
31 return *epc
== match_data
;
35 * pci_epc_put() - release the PCI endpoint controller
36 * @epc: epc returned by pci_epc_get()
38 * release the refcount the caller obtained by invoking pci_epc_get()
40 void pci_epc_put(struct pci_epc
*epc
)
42 if (!epc
|| IS_ERR(epc
))
45 module_put(epc
->ops
->owner
);
46 put_device(&epc
->dev
);
48 EXPORT_SYMBOL_GPL(pci_epc_put
);
51 * pci_epc_get() - get the PCI endpoint controller
52 * @epc_name: device name of the endpoint controller
54 * Invoke to get struct pci_epc * corresponding to the device name of the
57 struct pci_epc
*pci_epc_get(const char *epc_name
)
62 struct class_dev_iter iter
;
64 class_dev_iter_init(&iter
, pci_epc_class
, NULL
, NULL
);
65 while ((dev
= class_dev_iter_next(&iter
))) {
66 if (strcmp(epc_name
, dev_name(dev
)))
69 epc
= to_pci_epc(dev
);
70 if (!try_module_get(epc
->ops
->owner
)) {
75 class_dev_iter_exit(&iter
);
76 get_device(&epc
->dev
);
81 class_dev_iter_exit(&iter
);
84 EXPORT_SYMBOL_GPL(pci_epc_get
);
87 * pci_epc_stop() - stop the PCI link
88 * @epc: the link of the EPC device that has to be stopped
90 * Invoke to stop the PCI link
92 void pci_epc_stop(struct pci_epc
*epc
)
96 if (IS_ERR(epc
) || !epc
->ops
->stop
)
99 spin_lock_irqsave(&epc
->lock
, flags
);
101 spin_unlock_irqrestore(&epc
->lock
, flags
);
103 EXPORT_SYMBOL_GPL(pci_epc_stop
);
106 * pci_epc_start() - start the PCI link
107 * @epc: the link of *this* EPC device has to be started
109 * Invoke to start the PCI link
111 int pci_epc_start(struct pci_epc
*epc
)
119 if (!epc
->ops
->start
)
122 spin_lock_irqsave(&epc
->lock
, flags
);
123 ret
= epc
->ops
->start(epc
);
124 spin_unlock_irqrestore(&epc
->lock
, flags
);
128 EXPORT_SYMBOL_GPL(pci_epc_start
);
131 * pci_epc_raise_irq() - interrupt the host system
132 * @epc: the EPC device which has to interrupt the host
133 * @func_no: the endpoint function number in the EPC device
134 * @type: specify the type of interrupt; legacy or MSI
135 * @interrupt_num: the MSI interrupt number
137 * Invoke to raise an MSI or legacy interrupt
139 int pci_epc_raise_irq(struct pci_epc
*epc
, u8 func_no
,
140 enum pci_epc_irq_type type
, u8 interrupt_num
)
145 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
148 if (!epc
->ops
->raise_irq
)
151 spin_lock_irqsave(&epc
->lock
, flags
);
152 ret
= epc
->ops
->raise_irq(epc
, func_no
, type
, interrupt_num
);
153 spin_unlock_irqrestore(&epc
->lock
, flags
);
157 EXPORT_SYMBOL_GPL(pci_epc_raise_irq
);
160 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
161 * @epc: the EPC device to which MSI interrupts was requested
162 * @func_no: the endpoint function number in the EPC device
164 * Invoke to get the number of MSI interrupts allocated by the RC
166 int pci_epc_get_msi(struct pci_epc
*epc
, u8 func_no
)
171 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
174 if (!epc
->ops
->get_msi
)
177 spin_lock_irqsave(&epc
->lock
, flags
);
178 interrupt
= epc
->ops
->get_msi(epc
, func_no
);
179 spin_unlock_irqrestore(&epc
->lock
, flags
);
184 interrupt
= 1 << interrupt
;
188 EXPORT_SYMBOL_GPL(pci_epc_get_msi
);
191 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
192 * @epc: the EPC device on which MSI has to be configured
193 * @func_no: the endpoint function number in the EPC device
194 * @interrupts: number of MSI interrupts required by the EPF
196 * Invoke to set the required number of MSI interrupts.
198 int pci_epc_set_msi(struct pci_epc
*epc
, u8 func_no
, u8 interrupts
)
204 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
207 if (!epc
->ops
->set_msi
)
210 encode_int
= order_base_2(interrupts
);
212 spin_lock_irqsave(&epc
->lock
, flags
);
213 ret
= epc
->ops
->set_msi(epc
, func_no
, encode_int
);
214 spin_unlock_irqrestore(&epc
->lock
, flags
);
218 EXPORT_SYMBOL_GPL(pci_epc_set_msi
);
221 * pci_epc_unmap_addr() - unmap CPU address from PCI address
222 * @epc: the EPC device on which address is allocated
223 * @func_no: the endpoint function number in the EPC device
224 * @phys_addr: physical address of the local system
226 * Invoke to unmap the CPU address from PCI address.
228 void pci_epc_unmap_addr(struct pci_epc
*epc
, u8 func_no
,
229 phys_addr_t phys_addr
)
233 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
236 if (!epc
->ops
->unmap_addr
)
239 spin_lock_irqsave(&epc
->lock
, flags
);
240 epc
->ops
->unmap_addr(epc
, func_no
, phys_addr
);
241 spin_unlock_irqrestore(&epc
->lock
, flags
);
243 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr
);
246 * pci_epc_map_addr() - map CPU address to PCI address
247 * @epc: the EPC device on which address is allocated
248 * @func_no: the endpoint function number in the EPC device
249 * @phys_addr: physical address of the local system
250 * @pci_addr: PCI address to which the physical address should be mapped
251 * @size: the size of the allocation
253 * Invoke to map CPU address with PCI address.
255 int pci_epc_map_addr(struct pci_epc
*epc
, u8 func_no
,
256 phys_addr_t phys_addr
, u64 pci_addr
, size_t size
)
261 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
264 if (!epc
->ops
->map_addr
)
267 spin_lock_irqsave(&epc
->lock
, flags
);
268 ret
= epc
->ops
->map_addr(epc
, func_no
, phys_addr
, pci_addr
, size
);
269 spin_unlock_irqrestore(&epc
->lock
, flags
);
273 EXPORT_SYMBOL_GPL(pci_epc_map_addr
);
276 * pci_epc_clear_bar() - reset the BAR
277 * @epc: the EPC device for which the BAR has to be cleared
278 * @func_no: the endpoint function number in the EPC device
279 * @bar: the BAR number that has to be reset
281 * Invoke to reset the BAR of the endpoint device.
283 void pci_epc_clear_bar(struct pci_epc
*epc
, u8 func_no
, int bar
)
287 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
290 if (!epc
->ops
->clear_bar
)
293 spin_lock_irqsave(&epc
->lock
, flags
);
294 epc
->ops
->clear_bar(epc
, func_no
, bar
);
295 spin_unlock_irqrestore(&epc
->lock
, flags
);
297 EXPORT_SYMBOL_GPL(pci_epc_clear_bar
);
300 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
301 * @epc: the EPC device on which BAR has to be configured
302 * @func_no: the endpoint function number in the EPC device
303 * @bar: the BAR number that has to be configured
304 * @size: the size of the addr space
305 * @flags: specify memory allocation/io allocation/32bit address/64 bit address
307 * Invoke to configure the BAR of the endpoint device.
309 int pci_epc_set_bar(struct pci_epc
*epc
, u8 func_no
, enum pci_barno bar
,
310 dma_addr_t bar_phys
, size_t size
, int flags
)
313 unsigned long irq_flags
;
315 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
318 if (!epc
->ops
->set_bar
)
321 spin_lock_irqsave(&epc
->lock
, irq_flags
);
322 ret
= epc
->ops
->set_bar(epc
, func_no
, bar
, bar_phys
, size
, flags
);
323 spin_unlock_irqrestore(&epc
->lock
, irq_flags
);
327 EXPORT_SYMBOL_GPL(pci_epc_set_bar
);
330 * pci_epc_write_header() - write standard configuration header
331 * @epc: the EPC device to which the configuration header should be written
332 * @func_no: the endpoint function number in the EPC device
333 * @header: standard configuration header fields
335 * Invoke to write the configuration header to the endpoint controller. Every
336 * endpoint controller will have a dedicated location to which the standard
337 * configuration header would be written. The callback function should write
338 * the header fields to this dedicated location.
340 int pci_epc_write_header(struct pci_epc
*epc
, u8 func_no
,
341 struct pci_epf_header
*header
)
346 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
349 if (!epc
->ops
->write_header
)
352 spin_lock_irqsave(&epc
->lock
, flags
);
353 ret
= epc
->ops
->write_header(epc
, func_no
, header
);
354 spin_unlock_irqrestore(&epc
->lock
, flags
);
358 EXPORT_SYMBOL_GPL(pci_epc_write_header
);
361 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
362 * @epc: the EPC device to which the endpoint function should be added
363 * @epf: the endpoint function to be added
365 * A PCI endpoint device can have one or more functions. In the case of PCIe,
366 * the specification allows up to 8 PCIe endpoint functions. Invoke
367 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
369 int pci_epc_add_epf(struct pci_epc
*epc
, struct pci_epf
*epf
)
379 if (epf
->func_no
> epc
->max_functions
- 1)
384 spin_lock_irqsave(&epc
->lock
, flags
);
385 list_add_tail(&epf
->list
, &epc
->pci_epf
);
386 spin_unlock_irqrestore(&epc
->lock
, flags
);
390 EXPORT_SYMBOL_GPL(pci_epc_add_epf
);
393 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
394 * @epc: the EPC device from which the endpoint function should be removed
395 * @epf: the endpoint function to be removed
397 * Invoke to remove PCI endpoint function from the endpoint controller.
399 void pci_epc_remove_epf(struct pci_epc
*epc
, struct pci_epf
*epf
)
403 if (!epc
|| IS_ERR(epc
))
406 spin_lock_irqsave(&epc
->lock
, flags
);
407 list_del(&epf
->list
);
408 spin_unlock_irqrestore(&epc
->lock
, flags
);
410 EXPORT_SYMBOL_GPL(pci_epc_remove_epf
);
413 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
414 * connection with the Root Complex.
415 * @epc: the EPC device which has established link with the host
417 * Invoke to Notify the EPF device that the EPC device has established a
418 * connection with the Root Complex.
420 void pci_epc_linkup(struct pci_epc
*epc
)
425 if (!epc
|| IS_ERR(epc
))
428 spin_lock_irqsave(&epc
->lock
, flags
);
429 list_for_each_entry(epf
, &epc
->pci_epf
, list
)
431 spin_unlock_irqrestore(&epc
->lock
, flags
);
433 EXPORT_SYMBOL_GPL(pci_epc_linkup
);
436 * pci_epc_destroy() - destroy the EPC device
437 * @epc: the EPC device that has to be destroyed
439 * Invoke to destroy the PCI EPC device
441 void pci_epc_destroy(struct pci_epc
*epc
)
443 pci_ep_cfs_remove_epc_group(epc
->group
);
444 device_unregister(&epc
->dev
);
447 EXPORT_SYMBOL_GPL(pci_epc_destroy
);
450 * devm_pci_epc_destroy() - destroy the EPC device
451 * @dev: device that wants to destroy the EPC
452 * @epc: the EPC device that has to be destroyed
454 * Invoke to destroy the devres associated with this
455 * pci_epc and destroy the EPC device.
457 void devm_pci_epc_destroy(struct device
*dev
, struct pci_epc
*epc
)
461 r
= devres_destroy(dev
, devm_pci_epc_release
, devm_pci_epc_match
,
463 dev_WARN_ONCE(dev
, r
, "couldn't find PCI EPC resource\n");
465 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy
);
468 * __pci_epc_create() - create a new endpoint controller (EPC) device
469 * @dev: device that is creating the new EPC
470 * @ops: function pointers for performing EPC operations
471 * @owner: the owner of the module that creates the EPC device
473 * Invoke to create a new EPC device and add it to pci_epc class.
476 __pci_epc_create(struct device
*dev
, const struct pci_epc_ops
*ops
,
477 struct module
*owner
)
487 epc
= kzalloc(sizeof(*epc
), GFP_KERNEL
);
493 spin_lock_init(&epc
->lock
);
494 INIT_LIST_HEAD(&epc
->pci_epf
);
496 device_initialize(&epc
->dev
);
497 epc
->dev
.class = pci_epc_class
;
498 epc
->dev
.parent
= dev
;
501 ret
= dev_set_name(&epc
->dev
, "%s", dev_name(dev
));
505 ret
= device_add(&epc
->dev
);
509 epc
->group
= pci_ep_cfs_add_epc_group(dev_name(dev
));
514 put_device(&epc
->dev
);
520 EXPORT_SYMBOL_GPL(__pci_epc_create
);
523 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
524 * @dev: device that is creating the new EPC
525 * @ops: function pointers for performing EPC operations
526 * @owner: the owner of the module that creates the EPC device
528 * Invoke to create a new EPC device and add it to pci_epc class.
529 * While at that, it also associates the device with the pci_epc using devres.
530 * On driver detach, release function is invoked on the devres data,
531 * then, devres data is freed.
534 __devm_pci_epc_create(struct device
*dev
, const struct pci_epc_ops
*ops
,
535 struct module
*owner
)
537 struct pci_epc
**ptr
, *epc
;
539 ptr
= devres_alloc(devm_pci_epc_release
, sizeof(*ptr
), GFP_KERNEL
);
541 return ERR_PTR(-ENOMEM
);
543 epc
= __pci_epc_create(dev
, ops
, owner
);
546 devres_add(dev
, ptr
);
553 EXPORT_SYMBOL_GPL(__devm_pci_epc_create
);
555 static int __init
pci_epc_init(void)
557 pci_epc_class
= class_create(THIS_MODULE
, "pci_epc");
558 if (IS_ERR(pci_epc_class
)) {
559 pr_err("failed to create pci epc class --> %ld\n",
560 PTR_ERR(pci_epc_class
));
561 return PTR_ERR(pci_epc_class
);
566 module_init(pci_epc_init
);
568 static void __exit
pci_epc_exit(void)
570 class_destroy(pci_epc_class
);
572 module_exit(pci_epc_exit
);
574 MODULE_DESCRIPTION("PCI EPC Library");
575 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
576 MODULE_LICENSE("GPL v2");