staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / pci / endpoint / pci-epc-core.c
blob094dcc3203b8d96e6f85cabb6f167a9004936521
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_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)
94 unsigned long flags;
96 if (IS_ERR(epc) || !epc->ops->stop)
97 return;
99 spin_lock_irqsave(&epc->lock, flags);
100 epc->ops->stop(epc);
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)
113 int ret;
114 unsigned long flags;
116 if (IS_ERR(epc))
117 return -EINVAL;
119 if (!epc->ops->start)
120 return 0;
122 spin_lock_irqsave(&epc->lock, flags);
123 ret = epc->ops->start(epc);
124 spin_unlock_irqrestore(&epc->lock, flags);
126 return ret;
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, MSI or MSI-X
135 * @interrupt_num: the MSI or MSI-X interrupt number
137 * Invoke to raise an legacy, MSI or MSI-X interrupt
139 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no,
140 enum pci_epc_irq_type type, u16 interrupt_num)
142 int ret;
143 unsigned long flags;
145 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
146 return -EINVAL;
148 if (!epc->ops->raise_irq)
149 return 0;
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);
155 return ret;
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)
168 int interrupt;
169 unsigned long flags;
171 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
172 return 0;
174 if (!epc->ops->get_msi)
175 return 0;
177 spin_lock_irqsave(&epc->lock, flags);
178 interrupt = epc->ops->get_msi(epc, func_no);
179 spin_unlock_irqrestore(&epc->lock, flags);
181 if (interrupt < 0)
182 return 0;
184 interrupt = 1 << interrupt;
186 return 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)
200 int ret;
201 u8 encode_int;
202 unsigned long flags;
204 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
205 interrupts > 32)
206 return -EINVAL;
208 if (!epc->ops->set_msi)
209 return 0;
211 encode_int = order_base_2(interrupts);
213 spin_lock_irqsave(&epc->lock, flags);
214 ret = epc->ops->set_msi(epc, func_no, encode_int);
215 spin_unlock_irqrestore(&epc->lock, flags);
217 return ret;
219 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
222 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
223 * @epc: the EPC device to which MSI-X interrupts was requested
224 * @func_no: the endpoint function number in the EPC device
226 * Invoke to get the number of MSI-X interrupts allocated by the RC
228 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no)
230 int interrupt;
231 unsigned long flags;
233 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
234 return 0;
236 if (!epc->ops->get_msix)
237 return 0;
239 spin_lock_irqsave(&epc->lock, flags);
240 interrupt = epc->ops->get_msix(epc, func_no);
241 spin_unlock_irqrestore(&epc->lock, flags);
243 if (interrupt < 0)
244 return 0;
246 return interrupt + 1;
248 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
251 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
252 * @epc: the EPC device on which MSI-X has to be configured
253 * @func_no: the endpoint function number in the EPC device
254 * @interrupts: number of MSI-X interrupts required by the EPF
256 * Invoke to set the required number of MSI-X interrupts.
258 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts)
260 int ret;
261 unsigned long flags;
263 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
264 interrupts < 1 || interrupts > 2048)
265 return -EINVAL;
267 if (!epc->ops->set_msix)
268 return 0;
270 spin_lock_irqsave(&epc->lock, flags);
271 ret = epc->ops->set_msix(epc, func_no, interrupts - 1);
272 spin_unlock_irqrestore(&epc->lock, flags);
274 return ret;
276 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
279 * pci_epc_unmap_addr() - unmap CPU address from PCI address
280 * @epc: the EPC device on which address is allocated
281 * @func_no: the endpoint function number in the EPC device
282 * @phys_addr: physical address of the local system
284 * Invoke to unmap the CPU address from PCI address.
286 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
287 phys_addr_t phys_addr)
289 unsigned long flags;
291 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
292 return;
294 if (!epc->ops->unmap_addr)
295 return;
297 spin_lock_irqsave(&epc->lock, flags);
298 epc->ops->unmap_addr(epc, func_no, phys_addr);
299 spin_unlock_irqrestore(&epc->lock, flags);
301 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
304 * pci_epc_map_addr() - map CPU address to PCI address
305 * @epc: the EPC device on which address is allocated
306 * @func_no: the endpoint function number in the EPC device
307 * @phys_addr: physical address of the local system
308 * @pci_addr: PCI address to which the physical address should be mapped
309 * @size: the size of the allocation
311 * Invoke to map CPU address with PCI address.
313 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
314 phys_addr_t phys_addr, u64 pci_addr, size_t size)
316 int ret;
317 unsigned long flags;
319 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
320 return -EINVAL;
322 if (!epc->ops->map_addr)
323 return 0;
325 spin_lock_irqsave(&epc->lock, flags);
326 ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size);
327 spin_unlock_irqrestore(&epc->lock, flags);
329 return ret;
331 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
334 * pci_epc_clear_bar() - reset the BAR
335 * @epc: the EPC device for which the BAR has to be cleared
336 * @func_no: the endpoint function number in the EPC device
337 * @epf_bar: the struct epf_bar that contains the BAR information
339 * Invoke to reset the BAR of the endpoint device.
341 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
342 struct pci_epf_bar *epf_bar)
344 unsigned long flags;
346 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
347 (epf_bar->barno == BAR_5 &&
348 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
349 return;
351 if (!epc->ops->clear_bar)
352 return;
354 spin_lock_irqsave(&epc->lock, flags);
355 epc->ops->clear_bar(epc, func_no, epf_bar);
356 spin_unlock_irqrestore(&epc->lock, flags);
358 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
361 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
362 * @epc: the EPC device on which BAR has to be configured
363 * @func_no: the endpoint function number in the EPC device
364 * @epf_bar: the struct epf_bar that contains the BAR information
366 * Invoke to configure the BAR of the endpoint device.
368 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
369 struct pci_epf_bar *epf_bar)
371 int ret;
372 unsigned long irq_flags;
373 int flags = epf_bar->flags;
375 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
376 (epf_bar->barno == BAR_5 &&
377 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
378 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
379 flags & PCI_BASE_ADDRESS_IO_MASK) ||
380 (upper_32_bits(epf_bar->size) &&
381 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
382 return -EINVAL;
384 if (!epc->ops->set_bar)
385 return 0;
387 spin_lock_irqsave(&epc->lock, irq_flags);
388 ret = epc->ops->set_bar(epc, func_no, epf_bar);
389 spin_unlock_irqrestore(&epc->lock, irq_flags);
391 return ret;
393 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
396 * pci_epc_write_header() - write standard configuration header
397 * @epc: the EPC device to which the configuration header should be written
398 * @func_no: the endpoint function number in the EPC device
399 * @header: standard configuration header fields
401 * Invoke to write the configuration header to the endpoint controller. Every
402 * endpoint controller will have a dedicated location to which the standard
403 * configuration header would be written. The callback function should write
404 * the header fields to this dedicated location.
406 int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
407 struct pci_epf_header *header)
409 int ret;
410 unsigned long flags;
412 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
413 return -EINVAL;
415 if (!epc->ops->write_header)
416 return 0;
418 spin_lock_irqsave(&epc->lock, flags);
419 ret = epc->ops->write_header(epc, func_no, header);
420 spin_unlock_irqrestore(&epc->lock, flags);
422 return ret;
424 EXPORT_SYMBOL_GPL(pci_epc_write_header);
427 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
428 * @epc: the EPC device to which the endpoint function should be added
429 * @epf: the endpoint function to be added
431 * A PCI endpoint device can have one or more functions. In the case of PCIe,
432 * the specification allows up to 8 PCIe endpoint functions. Invoke
433 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
435 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
437 unsigned long flags;
439 if (epf->epc)
440 return -EBUSY;
442 if (IS_ERR(epc))
443 return -EINVAL;
445 if (epf->func_no > epc->max_functions - 1)
446 return -EINVAL;
448 epf->epc = epc;
450 spin_lock_irqsave(&epc->lock, flags);
451 list_add_tail(&epf->list, &epc->pci_epf);
452 spin_unlock_irqrestore(&epc->lock, flags);
454 return 0;
456 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
459 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
460 * @epc: the EPC device from which the endpoint function should be removed
461 * @epf: the endpoint function to be removed
463 * Invoke to remove PCI endpoint function from the endpoint controller.
465 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
467 unsigned long flags;
469 if (!epc || IS_ERR(epc))
470 return;
472 spin_lock_irqsave(&epc->lock, flags);
473 list_del(&epf->list);
474 spin_unlock_irqrestore(&epc->lock, flags);
476 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
479 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
480 * connection with the Root Complex.
481 * @epc: the EPC device which has established link with the host
483 * Invoke to Notify the EPF device that the EPC device has established a
484 * connection with the Root Complex.
486 void pci_epc_linkup(struct pci_epc *epc)
488 unsigned long flags;
489 struct pci_epf *epf;
491 if (!epc || IS_ERR(epc))
492 return;
494 spin_lock_irqsave(&epc->lock, flags);
495 list_for_each_entry(epf, &epc->pci_epf, list)
496 pci_epf_linkup(epf);
497 spin_unlock_irqrestore(&epc->lock, flags);
499 EXPORT_SYMBOL_GPL(pci_epc_linkup);
502 * pci_epc_destroy() - destroy the EPC device
503 * @epc: the EPC device that has to be destroyed
505 * Invoke to destroy the PCI EPC device
507 void pci_epc_destroy(struct pci_epc *epc)
509 pci_ep_cfs_remove_epc_group(epc->group);
510 device_unregister(&epc->dev);
511 kfree(epc);
513 EXPORT_SYMBOL_GPL(pci_epc_destroy);
516 * devm_pci_epc_destroy() - destroy the EPC device
517 * @dev: device that wants to destroy the EPC
518 * @epc: the EPC device that has to be destroyed
520 * Invoke to destroy the devres associated with this
521 * pci_epc and destroy the EPC device.
523 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
525 int r;
527 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
528 epc);
529 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
531 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
534 * __pci_epc_create() - create a new endpoint controller (EPC) device
535 * @dev: device that is creating the new EPC
536 * @ops: function pointers for performing EPC operations
537 * @owner: the owner of the module that creates the EPC device
539 * Invoke to create a new EPC device and add it to pci_epc class.
541 struct pci_epc *
542 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
543 struct module *owner)
545 int ret;
546 struct pci_epc *epc;
548 if (WARN_ON(!dev)) {
549 ret = -EINVAL;
550 goto err_ret;
553 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
554 if (!epc) {
555 ret = -ENOMEM;
556 goto err_ret;
559 spin_lock_init(&epc->lock);
560 INIT_LIST_HEAD(&epc->pci_epf);
562 device_initialize(&epc->dev);
563 epc->dev.class = pci_epc_class;
564 epc->dev.parent = dev;
565 epc->ops = ops;
567 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
568 if (ret)
569 goto put_dev;
571 ret = device_add(&epc->dev);
572 if (ret)
573 goto put_dev;
575 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
577 return epc;
579 put_dev:
580 put_device(&epc->dev);
581 kfree(epc);
583 err_ret:
584 return ERR_PTR(ret);
586 EXPORT_SYMBOL_GPL(__pci_epc_create);
589 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
590 * @dev: device that is creating the new EPC
591 * @ops: function pointers for performing EPC operations
592 * @owner: the owner of the module that creates the EPC device
594 * Invoke to create a new EPC device and add it to pci_epc class.
595 * While at that, it also associates the device with the pci_epc using devres.
596 * On driver detach, release function is invoked on the devres data,
597 * then, devres data is freed.
599 struct pci_epc *
600 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
601 struct module *owner)
603 struct pci_epc **ptr, *epc;
605 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
606 if (!ptr)
607 return ERR_PTR(-ENOMEM);
609 epc = __pci_epc_create(dev, ops, owner);
610 if (!IS_ERR(epc)) {
611 *ptr = epc;
612 devres_add(dev, ptr);
613 } else {
614 devres_free(ptr);
617 return epc;
619 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
621 static int __init pci_epc_init(void)
623 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
624 if (IS_ERR(pci_epc_class)) {
625 pr_err("failed to create pci epc class --> %ld\n",
626 PTR_ERR(pci_epc_class));
627 return PTR_ERR(pci_epc_class);
630 return 0;
632 module_init(pci_epc_init);
634 static void __exit pci_epc_exit(void)
636 class_destroy(pci_epc_class);
638 module_exit(pci_epc_exit);
640 MODULE_DESCRIPTION("PCI EPC Library");
641 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
642 MODULE_LICENSE("GPL v2");