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_get_first_free_bar() - helper to get first unreserved BAR
88 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
90 * Invoke to get the first unreserved BAR that can be used for endpoint
91 * function. For any incorrect value in reserved_bar return '0'.
93 unsigned int pci_epc_get_first_free_bar(const struct pci_epc_features
101 free_bar
= ffz(epc_features
->reserved_bar
);
107 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar
);
110 * pci_epc_get_features() - get the features supported by EPC
111 * @epc: the features supported by *this* EPC device will be returned
112 * @func_no: the features supported by the EPC device specific to the
113 * endpoint function with func_no will be returned
115 * Invoke to get the features provided by the EPC which may be
116 * specific to an endpoint function. Returns pci_epc_features on success
117 * and NULL for any failures.
119 const struct pci_epc_features
*pci_epc_get_features(struct pci_epc
*epc
,
122 const struct pci_epc_features
*epc_features
;
125 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
128 if (!epc
->ops
->get_features
)
131 spin_lock_irqsave(&epc
->lock
, flags
);
132 epc_features
= epc
->ops
->get_features(epc
, func_no
);
133 spin_unlock_irqrestore(&epc
->lock
, flags
);
137 EXPORT_SYMBOL_GPL(pci_epc_get_features
);
140 * pci_epc_stop() - stop the PCI link
141 * @epc: the link of the EPC device that has to be stopped
143 * Invoke to stop the PCI link
145 void pci_epc_stop(struct pci_epc
*epc
)
149 if (IS_ERR(epc
) || !epc
->ops
->stop
)
152 spin_lock_irqsave(&epc
->lock
, flags
);
154 spin_unlock_irqrestore(&epc
->lock
, flags
);
156 EXPORT_SYMBOL_GPL(pci_epc_stop
);
159 * pci_epc_start() - start the PCI link
160 * @epc: the link of *this* EPC device has to be started
162 * Invoke to start the PCI link
164 int pci_epc_start(struct pci_epc
*epc
)
172 if (!epc
->ops
->start
)
175 spin_lock_irqsave(&epc
->lock
, flags
);
176 ret
= epc
->ops
->start(epc
);
177 spin_unlock_irqrestore(&epc
->lock
, flags
);
181 EXPORT_SYMBOL_GPL(pci_epc_start
);
184 * pci_epc_raise_irq() - interrupt the host system
185 * @epc: the EPC device which has to interrupt the host
186 * @func_no: the endpoint function number in the EPC device
187 * @type: specify the type of interrupt; legacy, MSI or MSI-X
188 * @interrupt_num: the MSI or MSI-X interrupt number
190 * Invoke to raise an legacy, MSI or MSI-X interrupt
192 int pci_epc_raise_irq(struct pci_epc
*epc
, u8 func_no
,
193 enum pci_epc_irq_type type
, u16 interrupt_num
)
198 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
201 if (!epc
->ops
->raise_irq
)
204 spin_lock_irqsave(&epc
->lock
, flags
);
205 ret
= epc
->ops
->raise_irq(epc
, func_no
, type
, interrupt_num
);
206 spin_unlock_irqrestore(&epc
->lock
, flags
);
210 EXPORT_SYMBOL_GPL(pci_epc_raise_irq
);
213 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
214 * @epc: the EPC device to which MSI interrupts was requested
215 * @func_no: the endpoint function number in the EPC device
217 * Invoke to get the number of MSI interrupts allocated by the RC
219 int pci_epc_get_msi(struct pci_epc
*epc
, u8 func_no
)
224 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
227 if (!epc
->ops
->get_msi
)
230 spin_lock_irqsave(&epc
->lock
, flags
);
231 interrupt
= epc
->ops
->get_msi(epc
, func_no
);
232 spin_unlock_irqrestore(&epc
->lock
, flags
);
237 interrupt
= 1 << interrupt
;
241 EXPORT_SYMBOL_GPL(pci_epc_get_msi
);
244 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
245 * @epc: the EPC device on which MSI has to be configured
246 * @func_no: the endpoint function number in the EPC device
247 * @interrupts: number of MSI interrupts required by the EPF
249 * Invoke to set the required number of MSI interrupts.
251 int pci_epc_set_msi(struct pci_epc
*epc
, u8 func_no
, u8 interrupts
)
257 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
||
261 if (!epc
->ops
->set_msi
)
264 encode_int
= order_base_2(interrupts
);
266 spin_lock_irqsave(&epc
->lock
, flags
);
267 ret
= epc
->ops
->set_msi(epc
, func_no
, encode_int
);
268 spin_unlock_irqrestore(&epc
->lock
, flags
);
272 EXPORT_SYMBOL_GPL(pci_epc_set_msi
);
275 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
276 * @epc: the EPC device to which MSI-X interrupts was requested
277 * @func_no: the endpoint function number in the EPC device
279 * Invoke to get the number of MSI-X interrupts allocated by the RC
281 int pci_epc_get_msix(struct pci_epc
*epc
, u8 func_no
)
286 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
289 if (!epc
->ops
->get_msix
)
292 spin_lock_irqsave(&epc
->lock
, flags
);
293 interrupt
= epc
->ops
->get_msix(epc
, func_no
);
294 spin_unlock_irqrestore(&epc
->lock
, flags
);
299 return interrupt
+ 1;
301 EXPORT_SYMBOL_GPL(pci_epc_get_msix
);
304 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
305 * @epc: the EPC device on which MSI-X has to be configured
306 * @func_no: the endpoint function number in the EPC device
307 * @interrupts: number of MSI-X interrupts required by the EPF
309 * Invoke to set the required number of MSI-X interrupts.
311 int pci_epc_set_msix(struct pci_epc
*epc
, u8 func_no
, u16 interrupts
)
316 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
||
317 interrupts
< 1 || interrupts
> 2048)
320 if (!epc
->ops
->set_msix
)
323 spin_lock_irqsave(&epc
->lock
, flags
);
324 ret
= epc
->ops
->set_msix(epc
, func_no
, interrupts
- 1);
325 spin_unlock_irqrestore(&epc
->lock
, flags
);
329 EXPORT_SYMBOL_GPL(pci_epc_set_msix
);
332 * pci_epc_unmap_addr() - unmap CPU address from PCI address
333 * @epc: the EPC device on which address is allocated
334 * @func_no: the endpoint function number in the EPC device
335 * @phys_addr: physical address of the local system
337 * Invoke to unmap the CPU address from PCI address.
339 void pci_epc_unmap_addr(struct pci_epc
*epc
, u8 func_no
,
340 phys_addr_t phys_addr
)
344 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
347 if (!epc
->ops
->unmap_addr
)
350 spin_lock_irqsave(&epc
->lock
, flags
);
351 epc
->ops
->unmap_addr(epc
, func_no
, phys_addr
);
352 spin_unlock_irqrestore(&epc
->lock
, flags
);
354 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr
);
357 * pci_epc_map_addr() - map CPU address to PCI address
358 * @epc: the EPC device on which address is allocated
359 * @func_no: the endpoint function number in the EPC device
360 * @phys_addr: physical address of the local system
361 * @pci_addr: PCI address to which the physical address should be mapped
362 * @size: the size of the allocation
364 * Invoke to map CPU address with PCI address.
366 int pci_epc_map_addr(struct pci_epc
*epc
, u8 func_no
,
367 phys_addr_t phys_addr
, u64 pci_addr
, size_t size
)
372 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
375 if (!epc
->ops
->map_addr
)
378 spin_lock_irqsave(&epc
->lock
, flags
);
379 ret
= epc
->ops
->map_addr(epc
, func_no
, phys_addr
, pci_addr
, size
);
380 spin_unlock_irqrestore(&epc
->lock
, flags
);
384 EXPORT_SYMBOL_GPL(pci_epc_map_addr
);
387 * pci_epc_clear_bar() - reset the BAR
388 * @epc: the EPC device for which the BAR has to be cleared
389 * @func_no: the endpoint function number in the EPC device
390 * @epf_bar: the struct epf_bar that contains the BAR information
392 * Invoke to reset the BAR of the endpoint device.
394 void pci_epc_clear_bar(struct pci_epc
*epc
, u8 func_no
,
395 struct pci_epf_bar
*epf_bar
)
399 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
||
400 (epf_bar
->barno
== BAR_5
&&
401 epf_bar
->flags
& PCI_BASE_ADDRESS_MEM_TYPE_64
))
404 if (!epc
->ops
->clear_bar
)
407 spin_lock_irqsave(&epc
->lock
, flags
);
408 epc
->ops
->clear_bar(epc
, func_no
, epf_bar
);
409 spin_unlock_irqrestore(&epc
->lock
, flags
);
411 EXPORT_SYMBOL_GPL(pci_epc_clear_bar
);
414 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
415 * @epc: the EPC device on which BAR has to be configured
416 * @func_no: the endpoint function number in the EPC device
417 * @epf_bar: the struct epf_bar that contains the BAR information
419 * Invoke to configure the BAR of the endpoint device.
421 int pci_epc_set_bar(struct pci_epc
*epc
, u8 func_no
,
422 struct pci_epf_bar
*epf_bar
)
425 unsigned long irq_flags
;
426 int flags
= epf_bar
->flags
;
428 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
||
429 (epf_bar
->barno
== BAR_5
&&
430 flags
& PCI_BASE_ADDRESS_MEM_TYPE_64
) ||
431 (flags
& PCI_BASE_ADDRESS_SPACE_IO
&&
432 flags
& PCI_BASE_ADDRESS_IO_MASK
) ||
433 (upper_32_bits(epf_bar
->size
) &&
434 !(flags
& PCI_BASE_ADDRESS_MEM_TYPE_64
)))
437 if (!epc
->ops
->set_bar
)
440 spin_lock_irqsave(&epc
->lock
, irq_flags
);
441 ret
= epc
->ops
->set_bar(epc
, func_no
, epf_bar
);
442 spin_unlock_irqrestore(&epc
->lock
, irq_flags
);
446 EXPORT_SYMBOL_GPL(pci_epc_set_bar
);
449 * pci_epc_write_header() - write standard configuration header
450 * @epc: the EPC device to which the configuration header should be written
451 * @func_no: the endpoint function number in the EPC device
452 * @header: standard configuration header fields
454 * Invoke to write the configuration header to the endpoint controller. Every
455 * endpoint controller will have a dedicated location to which the standard
456 * configuration header would be written. The callback function should write
457 * the header fields to this dedicated location.
459 int pci_epc_write_header(struct pci_epc
*epc
, u8 func_no
,
460 struct pci_epf_header
*header
)
465 if (IS_ERR_OR_NULL(epc
) || func_no
>= epc
->max_functions
)
468 if (!epc
->ops
->write_header
)
471 spin_lock_irqsave(&epc
->lock
, flags
);
472 ret
= epc
->ops
->write_header(epc
, func_no
, header
);
473 spin_unlock_irqrestore(&epc
->lock
, flags
);
477 EXPORT_SYMBOL_GPL(pci_epc_write_header
);
480 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
481 * @epc: the EPC device to which the endpoint function should be added
482 * @epf: the endpoint function to be added
484 * A PCI endpoint device can have one or more functions. In the case of PCIe,
485 * the specification allows up to 8 PCIe endpoint functions. Invoke
486 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
488 int pci_epc_add_epf(struct pci_epc
*epc
, struct pci_epf
*epf
)
498 if (epf
->func_no
> epc
->max_functions
- 1)
503 spin_lock_irqsave(&epc
->lock
, flags
);
504 list_add_tail(&epf
->list
, &epc
->pci_epf
);
505 spin_unlock_irqrestore(&epc
->lock
, flags
);
509 EXPORT_SYMBOL_GPL(pci_epc_add_epf
);
512 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
513 * @epc: the EPC device from which the endpoint function should be removed
514 * @epf: the endpoint function to be removed
516 * Invoke to remove PCI endpoint function from the endpoint controller.
518 void pci_epc_remove_epf(struct pci_epc
*epc
, struct pci_epf
*epf
)
522 if (!epc
|| IS_ERR(epc
) || !epf
)
525 spin_lock_irqsave(&epc
->lock
, flags
);
526 list_del(&epf
->list
);
528 spin_unlock_irqrestore(&epc
->lock
, flags
);
530 EXPORT_SYMBOL_GPL(pci_epc_remove_epf
);
533 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
534 * connection with the Root Complex.
535 * @epc: the EPC device which has established link with the host
537 * Invoke to Notify the EPF device that the EPC device has established a
538 * connection with the Root Complex.
540 void pci_epc_linkup(struct pci_epc
*epc
)
545 if (!epc
|| IS_ERR(epc
))
548 spin_lock_irqsave(&epc
->lock
, flags
);
549 list_for_each_entry(epf
, &epc
->pci_epf
, list
)
551 spin_unlock_irqrestore(&epc
->lock
, flags
);
553 EXPORT_SYMBOL_GPL(pci_epc_linkup
);
556 * pci_epc_destroy() - destroy the EPC device
557 * @epc: the EPC device that has to be destroyed
559 * Invoke to destroy the PCI EPC device
561 void pci_epc_destroy(struct pci_epc
*epc
)
563 pci_ep_cfs_remove_epc_group(epc
->group
);
564 device_unregister(&epc
->dev
);
567 EXPORT_SYMBOL_GPL(pci_epc_destroy
);
570 * devm_pci_epc_destroy() - destroy the EPC device
571 * @dev: device that wants to destroy the EPC
572 * @epc: the EPC device that has to be destroyed
574 * Invoke to destroy the devres associated with this
575 * pci_epc and destroy the EPC device.
577 void devm_pci_epc_destroy(struct device
*dev
, struct pci_epc
*epc
)
581 r
= devres_destroy(dev
, devm_pci_epc_release
, devm_pci_epc_match
,
583 dev_WARN_ONCE(dev
, r
, "couldn't find PCI EPC resource\n");
585 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy
);
588 * __pci_epc_create() - create a new endpoint controller (EPC) device
589 * @dev: device that is creating the new EPC
590 * @ops: function pointers for performing EPC operations
591 * @owner: the owner of the module that creates the EPC device
593 * Invoke to create a new EPC device and add it to pci_epc class.
596 __pci_epc_create(struct device
*dev
, const struct pci_epc_ops
*ops
,
597 struct module
*owner
)
607 epc
= kzalloc(sizeof(*epc
), GFP_KERNEL
);
613 spin_lock_init(&epc
->lock
);
614 INIT_LIST_HEAD(&epc
->pci_epf
);
616 device_initialize(&epc
->dev
);
617 epc
->dev
.class = pci_epc_class
;
618 epc
->dev
.parent
= dev
;
621 ret
= dev_set_name(&epc
->dev
, "%s", dev_name(dev
));
625 ret
= device_add(&epc
->dev
);
629 epc
->group
= pci_ep_cfs_add_epc_group(dev_name(dev
));
634 put_device(&epc
->dev
);
640 EXPORT_SYMBOL_GPL(__pci_epc_create
);
643 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
644 * @dev: device that is creating the new EPC
645 * @ops: function pointers for performing EPC operations
646 * @owner: the owner of the module that creates the EPC device
648 * Invoke to create a new EPC device and add it to pci_epc class.
649 * While at that, it also associates the device with the pci_epc using devres.
650 * On driver detach, release function is invoked on the devres data,
651 * then, devres data is freed.
654 __devm_pci_epc_create(struct device
*dev
, const struct pci_epc_ops
*ops
,
655 struct module
*owner
)
657 struct pci_epc
**ptr
, *epc
;
659 ptr
= devres_alloc(devm_pci_epc_release
, sizeof(*ptr
), GFP_KERNEL
);
661 return ERR_PTR(-ENOMEM
);
663 epc
= __pci_epc_create(dev
, ops
, owner
);
666 devres_add(dev
, ptr
);
673 EXPORT_SYMBOL_GPL(__devm_pci_epc_create
);
675 static int __init
pci_epc_init(void)
677 pci_epc_class
= class_create(THIS_MODULE
, "pci_epc");
678 if (IS_ERR(pci_epc_class
)) {
679 pr_err("failed to create pci epc class --> %ld\n",
680 PTR_ERR(pci_epc_class
));
681 return PTR_ERR(pci_epc_class
);
686 module_init(pci_epc_init
);
688 static void __exit
pci_epc_exit(void)
690 class_destroy(pci_epc_class
);
692 module_exit(pci_epc_exit
);
694 MODULE_DESCRIPTION("PCI EPC Library");
695 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
696 MODULE_LICENSE("GPL v2");