Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / pci / endpoint / pci-epc-core.c
blobcadd3db0cbb08647025c08cdb2434dd7cd72c270
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Endpoint *Controller* (EPC) library
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 */
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;
24 pci_epc_destroy(epc);
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;
34 /**
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))
43 return;
45 module_put(epc->ops->owner);
46 put_device(&epc->dev);
48 EXPORT_SYMBOL_GPL(pci_epc_put);
50 /**
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
55 * endpoint controller
57 struct pci_epc *pci_epc_get(const char *epc_name)
59 int ret = -EINVAL;
60 struct pci_epc *epc;
61 struct device *dev;
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)))
67 continue;
69 epc = to_pci_epc(dev);
70 if (!try_module_get(epc->ops->owner)) {
71 ret = -EINVAL;
72 goto err;
75 class_dev_iter_exit(&iter);
76 get_device(&epc->dev);
77 return epc;
80 err:
81 class_dev_iter_exit(&iter);
82 return ERR_PTR(ret);
84 EXPORT_SYMBOL_GPL(pci_epc_get);
86 /**
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
94 *epc_features)
96 int free_bar;
98 if (!epc_features)
99 return 0;
101 free_bar = ffz(epc_features->reserved_bar);
102 if (free_bar > 5)
103 return 0;
105 return free_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,
120 u8 func_no)
122 const struct pci_epc_features *epc_features;
124 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
125 return NULL;
127 if (!epc->ops->get_features)
128 return NULL;
130 mutex_lock(&epc->lock);
131 epc_features = epc->ops->get_features(epc, func_no);
132 mutex_unlock(&epc->lock);
134 return epc_features;
136 EXPORT_SYMBOL_GPL(pci_epc_get_features);
139 * pci_epc_stop() - stop the PCI link
140 * @epc: the link of the EPC device that has to be stopped
142 * Invoke to stop the PCI link
144 void pci_epc_stop(struct pci_epc *epc)
146 if (IS_ERR(epc) || !epc->ops->stop)
147 return;
149 mutex_lock(&epc->lock);
150 epc->ops->stop(epc);
151 mutex_unlock(&epc->lock);
153 EXPORT_SYMBOL_GPL(pci_epc_stop);
156 * pci_epc_start() - start the PCI link
157 * @epc: the link of *this* EPC device has to be started
159 * Invoke to start the PCI link
161 int pci_epc_start(struct pci_epc *epc)
163 int ret;
165 if (IS_ERR(epc))
166 return -EINVAL;
168 if (!epc->ops->start)
169 return 0;
171 mutex_lock(&epc->lock);
172 ret = epc->ops->start(epc);
173 mutex_unlock(&epc->lock);
175 return ret;
177 EXPORT_SYMBOL_GPL(pci_epc_start);
180 * pci_epc_raise_irq() - interrupt the host system
181 * @epc: the EPC device which has to interrupt the host
182 * @func_no: the endpoint function number in the EPC device
183 * @type: specify the type of interrupt; legacy, MSI or MSI-X
184 * @interrupt_num: the MSI or MSI-X interrupt number
186 * Invoke to raise an legacy, MSI or MSI-X interrupt
188 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no,
189 enum pci_epc_irq_type type, u16 interrupt_num)
191 int ret;
193 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
194 return -EINVAL;
196 if (!epc->ops->raise_irq)
197 return 0;
199 mutex_lock(&epc->lock);
200 ret = epc->ops->raise_irq(epc, func_no, type, interrupt_num);
201 mutex_unlock(&epc->lock);
203 return ret;
205 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
208 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
209 * @epc: the EPC device to which MSI interrupts was requested
210 * @func_no: the endpoint function number in the EPC device
212 * Invoke to get the number of MSI interrupts allocated by the RC
214 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no)
216 int interrupt;
218 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
219 return 0;
221 if (!epc->ops->get_msi)
222 return 0;
224 mutex_lock(&epc->lock);
225 interrupt = epc->ops->get_msi(epc, func_no);
226 mutex_unlock(&epc->lock);
228 if (interrupt < 0)
229 return 0;
231 interrupt = 1 << interrupt;
233 return interrupt;
235 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
238 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
239 * @epc: the EPC device on which MSI has to be configured
240 * @func_no: the endpoint function number in the EPC device
241 * @interrupts: number of MSI interrupts required by the EPF
243 * Invoke to set the required number of MSI interrupts.
245 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
247 int ret;
248 u8 encode_int;
250 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
251 interrupts > 32)
252 return -EINVAL;
254 if (!epc->ops->set_msi)
255 return 0;
257 encode_int = order_base_2(interrupts);
259 mutex_lock(&epc->lock);
260 ret = epc->ops->set_msi(epc, func_no, encode_int);
261 mutex_unlock(&epc->lock);
263 return ret;
265 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
268 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
269 * @epc: the EPC device to which MSI-X interrupts was requested
270 * @func_no: the endpoint function number in the EPC device
272 * Invoke to get the number of MSI-X interrupts allocated by the RC
274 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no)
276 int interrupt;
278 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
279 return 0;
281 if (!epc->ops->get_msix)
282 return 0;
284 mutex_lock(&epc->lock);
285 interrupt = epc->ops->get_msix(epc, func_no);
286 mutex_unlock(&epc->lock);
288 if (interrupt < 0)
289 return 0;
291 return interrupt + 1;
293 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
296 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
297 * @epc: the EPC device on which MSI-X has to be configured
298 * @func_no: the endpoint function number in the EPC device
299 * @interrupts: number of MSI-X interrupts required by the EPF
300 * @bir: BAR where the MSI-X table resides
301 * @offset: Offset pointing to the start of MSI-X table
303 * Invoke to set the required number of MSI-X interrupts.
305 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,
306 enum pci_barno bir, u32 offset)
308 int ret;
310 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
311 interrupts < 1 || interrupts > 2048)
312 return -EINVAL;
314 if (!epc->ops->set_msix)
315 return 0;
317 mutex_lock(&epc->lock);
318 ret = epc->ops->set_msix(epc, func_no, interrupts - 1, bir, offset);
319 mutex_unlock(&epc->lock);
321 return ret;
323 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
326 * pci_epc_unmap_addr() - unmap CPU address from PCI address
327 * @epc: the EPC device on which address is allocated
328 * @func_no: the endpoint function number in the EPC device
329 * @phys_addr: physical address of the local system
331 * Invoke to unmap the CPU address from PCI address.
333 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
334 phys_addr_t phys_addr)
336 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
337 return;
339 if (!epc->ops->unmap_addr)
340 return;
342 mutex_lock(&epc->lock);
343 epc->ops->unmap_addr(epc, func_no, phys_addr);
344 mutex_unlock(&epc->lock);
346 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
349 * pci_epc_map_addr() - map CPU address to PCI address
350 * @epc: the EPC device on which address is allocated
351 * @func_no: the endpoint function number in the EPC device
352 * @phys_addr: physical address of the local system
353 * @pci_addr: PCI address to which the physical address should be mapped
354 * @size: the size of the allocation
356 * Invoke to map CPU address with PCI address.
358 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
359 phys_addr_t phys_addr, u64 pci_addr, size_t size)
361 int ret;
363 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
364 return -EINVAL;
366 if (!epc->ops->map_addr)
367 return 0;
369 mutex_lock(&epc->lock);
370 ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size);
371 mutex_unlock(&epc->lock);
373 return ret;
375 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
378 * pci_epc_clear_bar() - reset the BAR
379 * @epc: the EPC device for which the BAR has to be cleared
380 * @func_no: the endpoint function number in the EPC device
381 * @epf_bar: the struct epf_bar that contains the BAR information
383 * Invoke to reset the BAR of the endpoint device.
385 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
386 struct pci_epf_bar *epf_bar)
388 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
389 (epf_bar->barno == BAR_5 &&
390 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
391 return;
393 if (!epc->ops->clear_bar)
394 return;
396 mutex_lock(&epc->lock);
397 epc->ops->clear_bar(epc, func_no, epf_bar);
398 mutex_unlock(&epc->lock);
400 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
403 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
404 * @epc: the EPC device on which BAR has to be configured
405 * @func_no: the endpoint function number in the EPC device
406 * @epf_bar: the struct epf_bar that contains the BAR information
408 * Invoke to configure the BAR of the endpoint device.
410 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
411 struct pci_epf_bar *epf_bar)
413 int ret;
414 int flags = epf_bar->flags;
416 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
417 (epf_bar->barno == BAR_5 &&
418 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
419 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
420 flags & PCI_BASE_ADDRESS_IO_MASK) ||
421 (upper_32_bits(epf_bar->size) &&
422 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
423 return -EINVAL;
425 if (!epc->ops->set_bar)
426 return 0;
428 mutex_lock(&epc->lock);
429 ret = epc->ops->set_bar(epc, func_no, epf_bar);
430 mutex_unlock(&epc->lock);
432 return ret;
434 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
437 * pci_epc_write_header() - write standard configuration header
438 * @epc: the EPC device to which the configuration header should be written
439 * @func_no: the endpoint function number in the EPC device
440 * @header: standard configuration header fields
442 * Invoke to write the configuration header to the endpoint controller. Every
443 * endpoint controller will have a dedicated location to which the standard
444 * configuration header would be written. The callback function should write
445 * the header fields to this dedicated location.
447 int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
448 struct pci_epf_header *header)
450 int ret;
452 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
453 return -EINVAL;
455 if (!epc->ops->write_header)
456 return 0;
458 mutex_lock(&epc->lock);
459 ret = epc->ops->write_header(epc, func_no, header);
460 mutex_unlock(&epc->lock);
462 return ret;
464 EXPORT_SYMBOL_GPL(pci_epc_write_header);
467 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
468 * @epc: the EPC device to which the endpoint function should be added
469 * @epf: the endpoint function to be added
471 * A PCI endpoint device can have one or more functions. In the case of PCIe,
472 * the specification allows up to 8 PCIe endpoint functions. Invoke
473 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
475 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
477 u32 func_no;
478 int ret = 0;
480 if (epf->epc)
481 return -EBUSY;
483 if (IS_ERR(epc))
484 return -EINVAL;
486 mutex_lock(&epc->lock);
487 func_no = find_first_zero_bit(&epc->function_num_map,
488 BITS_PER_LONG);
489 if (func_no >= BITS_PER_LONG) {
490 ret = -EINVAL;
491 goto ret;
494 if (func_no > epc->max_functions - 1) {
495 dev_err(&epc->dev, "Exceeding max supported Function Number\n");
496 ret = -EINVAL;
497 goto ret;
500 set_bit(func_no, &epc->function_num_map);
501 epf->func_no = func_no;
502 epf->epc = epc;
504 list_add_tail(&epf->list, &epc->pci_epf);
506 ret:
507 mutex_unlock(&epc->lock);
509 return ret;
511 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
514 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
515 * @epc: the EPC device from which the endpoint function should be removed
516 * @epf: the endpoint function to be removed
518 * Invoke to remove PCI endpoint function from the endpoint controller.
520 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
522 if (!epc || IS_ERR(epc) || !epf)
523 return;
525 mutex_lock(&epc->lock);
526 clear_bit(epf->func_no, &epc->function_num_map);
527 list_del(&epf->list);
528 epf->epc = NULL;
529 mutex_unlock(&epc->lock);
531 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
534 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
535 * connection with the Root Complex.
536 * @epc: the EPC device which has established link with the host
538 * Invoke to Notify the EPF device that the EPC device has established a
539 * connection with the Root Complex.
541 void pci_epc_linkup(struct pci_epc *epc)
543 if (!epc || IS_ERR(epc))
544 return;
546 atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL);
548 EXPORT_SYMBOL_GPL(pci_epc_linkup);
551 * pci_epc_init_notify() - Notify the EPF device that EPC device's core
552 * initialization is completed.
553 * @epc: the EPC device whose core initialization is completeds
555 * Invoke to Notify the EPF device that the EPC device's initialization
556 * is completed.
558 void pci_epc_init_notify(struct pci_epc *epc)
560 if (!epc || IS_ERR(epc))
561 return;
563 atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL);
565 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
568 * pci_epc_destroy() - destroy the EPC device
569 * @epc: the EPC device that has to be destroyed
571 * Invoke to destroy the PCI EPC device
573 void pci_epc_destroy(struct pci_epc *epc)
575 pci_ep_cfs_remove_epc_group(epc->group);
576 device_unregister(&epc->dev);
577 kfree(epc);
579 EXPORT_SYMBOL_GPL(pci_epc_destroy);
582 * devm_pci_epc_destroy() - destroy the EPC device
583 * @dev: device that wants to destroy the EPC
584 * @epc: the EPC device that has to be destroyed
586 * Invoke to destroy the devres associated with this
587 * pci_epc and destroy the EPC device.
589 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
591 int r;
593 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
594 epc);
595 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
597 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
600 * __pci_epc_create() - create a new endpoint controller (EPC) device
601 * @dev: device that is creating the new EPC
602 * @ops: function pointers for performing EPC operations
603 * @owner: the owner of the module that creates the EPC device
605 * Invoke to create a new EPC device and add it to pci_epc class.
607 struct pci_epc *
608 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
609 struct module *owner)
611 int ret;
612 struct pci_epc *epc;
614 if (WARN_ON(!dev)) {
615 ret = -EINVAL;
616 goto err_ret;
619 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
620 if (!epc) {
621 ret = -ENOMEM;
622 goto err_ret;
625 mutex_init(&epc->lock);
626 INIT_LIST_HEAD(&epc->pci_epf);
627 ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier);
629 device_initialize(&epc->dev);
630 epc->dev.class = pci_epc_class;
631 epc->dev.parent = dev;
632 epc->ops = ops;
634 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
635 if (ret)
636 goto put_dev;
638 ret = device_add(&epc->dev);
639 if (ret)
640 goto put_dev;
642 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
644 return epc;
646 put_dev:
647 put_device(&epc->dev);
648 kfree(epc);
650 err_ret:
651 return ERR_PTR(ret);
653 EXPORT_SYMBOL_GPL(__pci_epc_create);
656 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
657 * @dev: device that is creating the new EPC
658 * @ops: function pointers for performing EPC operations
659 * @owner: the owner of the module that creates the EPC device
661 * Invoke to create a new EPC device and add it to pci_epc class.
662 * While at that, it also associates the device with the pci_epc using devres.
663 * On driver detach, release function is invoked on the devres data,
664 * then, devres data is freed.
666 struct pci_epc *
667 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
668 struct module *owner)
670 struct pci_epc **ptr, *epc;
672 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
673 if (!ptr)
674 return ERR_PTR(-ENOMEM);
676 epc = __pci_epc_create(dev, ops, owner);
677 if (!IS_ERR(epc)) {
678 *ptr = epc;
679 devres_add(dev, ptr);
680 } else {
681 devres_free(ptr);
684 return epc;
686 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
688 static int __init pci_epc_init(void)
690 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
691 if (IS_ERR(pci_epc_class)) {
692 pr_err("failed to create pci epc class --> %ld\n",
693 PTR_ERR(pci_epc_class));
694 return PTR_ERR(pci_epc_class);
697 return 0;
699 module_init(pci_epc_init);
701 static void __exit pci_epc_exit(void)
703 class_destroy(pci_epc_class);
705 module_exit(pci_epc_exit);
707 MODULE_DESCRIPTION("PCI EPC Library");
708 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
709 MODULE_LICENSE("GPL v2");