Lines Matching +full:msi +full:- +full:map

1 // SPDX-License-Identifier: GPL-2.0
13 #include <linux/pci-epc.h>
14 #include <linux/pci-epf.h>
15 #include <linux/pci-ep-cfs.h>
29 * pci_epc_put() - release the PCI endpoint controller
39 module_put(epc->ops->owner); in pci_epc_put()
40 put_device(&epc->dev); in pci_epc_put()
45 * pci_epc_get() - get the PCI endpoint controller
53 int ret = -EINVAL; in pci_epc_get()
62 if (try_module_get(epc->ops->owner)) in pci_epc_get()
72 * pci_epc_get_first_free_bar() - helper to get first unreserved BAR
86 * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
101 /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */ in pci_epc_get_next_free_bar()
102 if (bar > 0 && epc_features->bar[bar - 1].only_64bit) in pci_epc_get_next_free_bar()
107 if (epc_features->bar[i].type != BAR_RESERVED) in pci_epc_get_next_free_bar()
118 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) in pci_epc_function_is_valid()
121 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) in pci_epc_function_is_valid()
128 * pci_epc_get_features() - get the features supported by EPC
147 if (!epc->ops->get_features) in pci_epc_get_features()
150 mutex_lock(&epc->lock); in pci_epc_get_features()
151 epc_features = epc->ops->get_features(epc, func_no, vfunc_no); in pci_epc_get_features()
152 mutex_unlock(&epc->lock); in pci_epc_get_features()
159 * pci_epc_stop() - stop the PCI link
166 if (IS_ERR(epc) || !epc->ops->stop) in pci_epc_stop()
169 mutex_lock(&epc->lock); in pci_epc_stop()
170 epc->ops->stop(epc); in pci_epc_stop()
171 mutex_unlock(&epc->lock); in pci_epc_stop()
176 * pci_epc_start() - start the PCI link
186 return -EINVAL; in pci_epc_start()
188 if (!epc->ops->start) in pci_epc_start()
191 mutex_lock(&epc->lock); in pci_epc_start()
192 ret = epc->ops->start(epc); in pci_epc_start()
193 mutex_unlock(&epc->lock); in pci_epc_start()
200 * pci_epc_raise_irq() - interrupt the host system
204 * @type: specify the type of interrupt; INTX, MSI or MSI-X
205 * @interrupt_num: the MSI or MSI-X interrupt number with range (1-N)
207 * Invoke to raise an INTX, MSI or MSI-X interrupt
215 return -EINVAL; in pci_epc_raise_irq()
217 if (!epc->ops->raise_irq) in pci_epc_raise_irq()
220 mutex_lock(&epc->lock); in pci_epc_raise_irq()
221 ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num); in pci_epc_raise_irq()
222 mutex_unlock(&epc->lock); in pci_epc_raise_irq()
229 * pci_epc_map_msi_irq() - Map physical address to MSI address and return
230 * MSI data
231 * @epc: the EPC device which has the MSI capability
235 * @interrupt_num: the MSI interrupt number with range (1-N)
237 * @msi_data: the data that should be written in order to raise MSI interrupt
239 * @msi_addr_offset: Offset of MSI address from the aligned outbound address
240 * to which the MSI address is mapped
242 * Invoke to map physical address to MSI address and return MSI data. The
256 return -EINVAL; in pci_epc_map_msi_irq()
258 if (!epc->ops->map_msi_irq) in pci_epc_map_msi_irq()
259 return -EINVAL; in pci_epc_map_msi_irq()
261 mutex_lock(&epc->lock); in pci_epc_map_msi_irq()
262 ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr, in pci_epc_map_msi_irq()
265 mutex_unlock(&epc->lock); in pci_epc_map_msi_irq()
272 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
273 * @epc: the EPC device to which MSI interrupts was requested
277 * Invoke to get the number of MSI interrupts allocated by the RC
286 if (!epc->ops->get_msi) in pci_epc_get_msi()
289 mutex_lock(&epc->lock); in pci_epc_get_msi()
290 interrupt = epc->ops->get_msi(epc, func_no, vfunc_no); in pci_epc_get_msi()
291 mutex_unlock(&epc->lock); in pci_epc_get_msi()
303 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
304 * @epc: the EPC device on which MSI has to be configured
307 * @interrupts: number of MSI interrupts required by the EPF
309 * Invoke to set the required number of MSI interrupts.
317 return -EINVAL; in pci_epc_set_msi()
320 return -EINVAL; in pci_epc_set_msi()
322 if (!epc->ops->set_msi) in pci_epc_set_msi()
327 mutex_lock(&epc->lock); in pci_epc_set_msi()
328 ret = epc->ops->set_msi(epc, func_no, vfunc_no, encode_int); in pci_epc_set_msi()
329 mutex_unlock(&epc->lock); in pci_epc_set_msi()
336 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
337 * @epc: the EPC device to which MSI-X interrupts was requested
341 * Invoke to get the number of MSI-X interrupts allocated by the RC
350 if (!epc->ops->get_msix) in pci_epc_get_msix()
353 mutex_lock(&epc->lock); in pci_epc_get_msix()
354 interrupt = epc->ops->get_msix(epc, func_no, vfunc_no); in pci_epc_get_msix()
355 mutex_unlock(&epc->lock); in pci_epc_get_msix()
365 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
366 * @epc: the EPC device on which MSI-X has to be configured
369 * @interrupts: number of MSI-X interrupts required by the EPF
370 * @bir: BAR where the MSI-X table resides
371 * @offset: Offset pointing to the start of MSI-X table
373 * Invoke to set the required number of MSI-X interrupts.
381 return -EINVAL; in pci_epc_set_msix()
384 return -EINVAL; in pci_epc_set_msix()
386 if (!epc->ops->set_msix) in pci_epc_set_msix()
389 mutex_lock(&epc->lock); in pci_epc_set_msix()
390 ret = epc->ops->set_msix(epc, func_no, vfunc_no, interrupts - 1, bir, in pci_epc_set_msix()
392 mutex_unlock(&epc->lock); in pci_epc_set_msix()
399 * pci_epc_unmap_addr() - unmap CPU address from PCI address
413 if (!epc->ops->unmap_addr) in pci_epc_unmap_addr()
416 mutex_lock(&epc->lock); in pci_epc_unmap_addr()
417 epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr); in pci_epc_unmap_addr()
418 mutex_unlock(&epc->lock); in pci_epc_unmap_addr()
423 * pci_epc_map_addr() - map CPU address to PCI address
431 * Invoke to map CPU address with PCI address.
439 return -EINVAL; in pci_epc_map_addr()
441 if (!epc->ops->map_addr) in pci_epc_map_addr()
444 mutex_lock(&epc->lock); in pci_epc_map_addr()
445 ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr, in pci_epc_map_addr()
447 mutex_unlock(&epc->lock); in pci_epc_map_addr()
454 * pci_epc_mem_map() - allocate and map a PCI address to a CPU address
459 * @pci_size: the number of bytes to map starting from @pci_addr
460 * @map: where to return the mapping information
462 * Allocate a controller memory address region and map it to a RC PCI address
469 * indicated by @map->pci_size. This size may be less than the requested
471 * @map->virt_addr (@map->phys_addr indicates the physical address).
473 * respectively indicated by @map->map_size and @map->virt_base (and
474 * @map->phys_base for the physical address of @map->virt_base).
479 u64 pci_addr, size_t pci_size, struct pci_epc_map *map) in pci_epc_mem_map() argument
486 return -EINVAL; in pci_epc_mem_map()
488 if (!pci_size || !map) in pci_epc_mem_map()
489 return -EINVAL; in pci_epc_mem_map()
492 * Align the PCI address to map. If the controller defines the in pci_epc_mem_map()
493 * .align_addr() operation, use it to determine the PCI address to map in pci_epc_mem_map()
497 memset(map, 0, sizeof(*map)); in pci_epc_mem_map()
498 map->pci_addr = pci_addr; in pci_epc_mem_map()
499 if (epc->ops->align_addr) in pci_epc_mem_map()
500 map->map_pci_addr = in pci_epc_mem_map()
501 epc->ops->align_addr(epc, pci_addr, in pci_epc_mem_map()
504 map->map_pci_addr = pci_addr; in pci_epc_mem_map()
505 map->map_size = map_size; in pci_epc_mem_map()
506 if (map->map_pci_addr + map->map_size < pci_addr + pci_size) in pci_epc_mem_map()
507 map->pci_size = map->map_pci_addr + map->map_size - pci_addr; in pci_epc_mem_map()
509 map->pci_size = pci_size; in pci_epc_mem_map()
511 map->virt_base = pci_epc_mem_alloc_addr(epc, &map->phys_base, in pci_epc_mem_map()
512 map->map_size); in pci_epc_mem_map()
513 if (!map->virt_base) in pci_epc_mem_map()
514 return -ENOMEM; in pci_epc_mem_map()
516 map->phys_addr = map->phys_base + map_offset; in pci_epc_mem_map()
517 map->virt_addr = map->virt_base + map_offset; in pci_epc_mem_map()
519 ret = pci_epc_map_addr(epc, func_no, vfunc_no, map->phys_base, in pci_epc_mem_map()
520 map->map_pci_addr, map->map_size); in pci_epc_mem_map()
522 pci_epc_mem_free_addr(epc, map->phys_base, map->virt_base, in pci_epc_mem_map()
523 map->map_size); in pci_epc_mem_map()
532 * pci_epc_mem_unmap() - unmap and free a CPU address region
536 * @map: the mapping information
542 struct pci_epc_map *map) in pci_epc_mem_unmap() argument
547 if (!map || !map->virt_base) in pci_epc_mem_unmap()
550 pci_epc_unmap_addr(epc, func_no, vfunc_no, map->phys_base); in pci_epc_mem_unmap()
551 pci_epc_mem_free_addr(epc, map->phys_base, map->virt_base, in pci_epc_mem_unmap()
552 map->map_size); in pci_epc_mem_unmap()
557 * pci_epc_clear_bar() - reset the BAR
571 if (epf_bar->barno == BAR_5 && in pci_epc_clear_bar()
572 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64) in pci_epc_clear_bar()
575 if (!epc->ops->clear_bar) in pci_epc_clear_bar()
578 mutex_lock(&epc->lock); in pci_epc_clear_bar()
579 epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar); in pci_epc_clear_bar()
580 mutex_unlock(&epc->lock); in pci_epc_clear_bar()
585 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
597 enum pci_barno bar = epf_bar->barno; in pci_epc_set_bar()
598 int flags = epf_bar->flags; in pci_epc_set_bar()
603 return -EINVAL; in pci_epc_set_bar()
605 if (epc_features->bar[bar].type == BAR_RESIZABLE && in pci_epc_set_bar()
606 (epf_bar->size < SZ_1M || (u64)epf_bar->size > (SZ_128G * 1024))) in pci_epc_set_bar()
607 return -EINVAL; in pci_epc_set_bar()
609 if (epc_features->bar[bar].type == BAR_FIXED && in pci_epc_set_bar()
610 (epc_features->bar[bar].fixed_size != epf_bar->size)) in pci_epc_set_bar()
611 return -EINVAL; in pci_epc_set_bar()
613 if (!is_power_of_2(epf_bar->size)) in pci_epc_set_bar()
614 return -EINVAL; in pci_epc_set_bar()
616 if ((epf_bar->barno == BAR_5 && flags & PCI_BASE_ADDRESS_MEM_TYPE_64) || in pci_epc_set_bar()
619 (upper_32_bits(epf_bar->size) && in pci_epc_set_bar()
621 return -EINVAL; in pci_epc_set_bar()
623 if (!epc->ops->set_bar) in pci_epc_set_bar()
626 mutex_lock(&epc->lock); in pci_epc_set_bar()
627 ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar); in pci_epc_set_bar()
628 mutex_unlock(&epc->lock); in pci_epc_set_bar()
635 * pci_epc_bar_size_to_rebar_cap() - convert a size to the representation used
650 return -EINVAL; in pci_epc_bar_size_to_rebar_cap()
652 *cap = ilog2(size) - ilog2(SZ_1M); in pci_epc_bar_size_to_rebar_cap()
662 * pci_epc_write_header() - write standard configuration header
679 return -EINVAL; in pci_epc_write_header()
683 return -EINVAL; in pci_epc_write_header()
685 if (!epc->ops->write_header) in pci_epc_write_header()
688 mutex_lock(&epc->lock); in pci_epc_write_header()
689 ret = epc->ops->write_header(epc, func_no, vfunc_no, header); in pci_epc_write_header()
690 mutex_unlock(&epc->lock); in pci_epc_write_header()
697 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
714 if (IS_ERR_OR_NULL(epc) || epf->is_vf) in pci_epc_add_epf()
715 return -EINVAL; in pci_epc_add_epf()
717 if (type == PRIMARY_INTERFACE && epf->epc) in pci_epc_add_epf()
718 return -EBUSY; in pci_epc_add_epf()
720 if (type == SECONDARY_INTERFACE && epf->sec_epc) in pci_epc_add_epf()
721 return -EBUSY; in pci_epc_add_epf()
723 mutex_lock(&epc->list_lock); in pci_epc_add_epf()
724 func_no = find_first_zero_bit(&epc->function_num_map, in pci_epc_add_epf()
727 ret = -EINVAL; in pci_epc_add_epf()
731 if (func_no > epc->max_functions - 1) { in pci_epc_add_epf()
732 dev_err(&epc->dev, "Exceeding max supported Function Number\n"); in pci_epc_add_epf()
733 ret = -EINVAL; in pci_epc_add_epf()
737 set_bit(func_no, &epc->function_num_map); in pci_epc_add_epf()
739 epf->func_no = func_no; in pci_epc_add_epf()
740 epf->epc = epc; in pci_epc_add_epf()
741 list = &epf->list; in pci_epc_add_epf()
743 epf->sec_epc_func_no = func_no; in pci_epc_add_epf()
744 epf->sec_epc = epc; in pci_epc_add_epf()
745 list = &epf->sec_epc_list; in pci_epc_add_epf()
748 list_add_tail(list, &epc->pci_epf); in pci_epc_add_epf()
750 mutex_unlock(&epc->list_lock); in pci_epc_add_epf()
757 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
774 mutex_lock(&epc->list_lock); in pci_epc_remove_epf()
776 func_no = epf->func_no; in pci_epc_remove_epf()
777 list = &epf->list; in pci_epc_remove_epf()
778 epf->epc = NULL; in pci_epc_remove_epf()
780 func_no = epf->sec_epc_func_no; in pci_epc_remove_epf()
781 list = &epf->sec_epc_list; in pci_epc_remove_epf()
782 epf->sec_epc = NULL; in pci_epc_remove_epf()
784 clear_bit(func_no, &epc->function_num_map); in pci_epc_remove_epf()
786 mutex_unlock(&epc->list_lock); in pci_epc_remove_epf()
791 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
805 mutex_lock(&epc->list_lock); in pci_epc_linkup()
806 list_for_each_entry(epf, &epc->pci_epf, list) { in pci_epc_linkup()
807 mutex_lock(&epf->lock); in pci_epc_linkup()
808 if (epf->event_ops && epf->event_ops->link_up) in pci_epc_linkup()
809 epf->event_ops->link_up(epf); in pci_epc_linkup()
810 mutex_unlock(&epf->lock); in pci_epc_linkup()
812 mutex_unlock(&epc->list_lock); in pci_epc_linkup()
817 * pci_epc_linkdown() - Notify the EPF device that EPC device has dropped the
831 mutex_lock(&epc->list_lock); in pci_epc_linkdown()
832 list_for_each_entry(epf, &epc->pci_epf, list) { in pci_epc_linkdown()
833 mutex_lock(&epf->lock); in pci_epc_linkdown()
834 if (epf->event_ops && epf->event_ops->link_down) in pci_epc_linkdown()
835 epf->event_ops->link_down(epf); in pci_epc_linkdown()
836 mutex_unlock(&epf->lock); in pci_epc_linkdown()
838 mutex_unlock(&epc->list_lock); in pci_epc_linkdown()
843 * pci_epc_init_notify() - Notify the EPF device that EPC device initialization
857 mutex_lock(&epc->list_lock); in pci_epc_init_notify()
858 list_for_each_entry(epf, &epc->pci_epf, list) { in pci_epc_init_notify()
859 mutex_lock(&epf->lock); in pci_epc_init_notify()
860 if (epf->event_ops && epf->event_ops->epc_init) in pci_epc_init_notify()
861 epf->event_ops->epc_init(epf); in pci_epc_init_notify()
862 mutex_unlock(&epf->lock); in pci_epc_init_notify()
864 epc->init_complete = true; in pci_epc_init_notify()
865 mutex_unlock(&epc->list_lock); in pci_epc_init_notify()
870 * pci_epc_notify_pending_init() - Notify the pending EPC device initialization
881 if (epc->init_complete) { in pci_epc_notify_pending_init()
882 mutex_lock(&epf->lock); in pci_epc_notify_pending_init()
883 if (epf->event_ops && epf->event_ops->epc_init) in pci_epc_notify_pending_init()
884 epf->event_ops->epc_init(epf); in pci_epc_notify_pending_init()
885 mutex_unlock(&epf->lock); in pci_epc_notify_pending_init()
891 * pci_epc_deinit_notify() - Notify the EPF device about EPC deinitialization
903 mutex_lock(&epc->list_lock); in pci_epc_deinit_notify()
904 list_for_each_entry(epf, &epc->pci_epf, list) { in pci_epc_deinit_notify()
905 mutex_lock(&epf->lock); in pci_epc_deinit_notify()
906 if (epf->event_ops && epf->event_ops->epc_deinit) in pci_epc_deinit_notify()
907 epf->event_ops->epc_deinit(epf); in pci_epc_deinit_notify()
908 mutex_unlock(&epf->lock); in pci_epc_deinit_notify()
910 epc->init_complete = false; in pci_epc_deinit_notify()
911 mutex_unlock(&epc->list_lock); in pci_epc_deinit_notify()
916 * pci_epc_bus_master_enable_notify() - Notify the EPF device that the EPC
931 mutex_lock(&epc->list_lock); in pci_epc_bus_master_enable_notify()
932 list_for_each_entry(epf, &epc->pci_epf, list) { in pci_epc_bus_master_enable_notify()
933 mutex_lock(&epf->lock); in pci_epc_bus_master_enable_notify()
934 if (epf->event_ops && epf->event_ops->bus_master_enable) in pci_epc_bus_master_enable_notify()
935 epf->event_ops->bus_master_enable(epf); in pci_epc_bus_master_enable_notify()
936 mutex_unlock(&epf->lock); in pci_epc_bus_master_enable_notify()
938 mutex_unlock(&epc->list_lock); in pci_epc_bus_master_enable_notify()
943 * pci_epc_destroy() - destroy the EPC device
950 pci_ep_cfs_remove_epc_group(epc->group); in pci_epc_destroy()
952 pci_bus_release_domain_nr(epc->dev.parent, epc->domain_nr); in pci_epc_destroy()
954 device_unregister(&epc->dev); in pci_epc_destroy()
964 * __pci_epc_create() - create a new endpoint controller (EPC) device
979 ret = -EINVAL; in __pci_epc_create()
985 ret = -ENOMEM; in __pci_epc_create()
989 mutex_init(&epc->lock); in __pci_epc_create()
990 mutex_init(&epc->list_lock); in __pci_epc_create()
991 INIT_LIST_HEAD(&epc->pci_epf); in __pci_epc_create()
993 device_initialize(&epc->dev); in __pci_epc_create()
994 epc->dev.class = &pci_epc_class; in __pci_epc_create()
995 epc->dev.parent = dev; in __pci_epc_create()
996 epc->dev.release = pci_epc_release; in __pci_epc_create()
997 epc->ops = ops; in __pci_epc_create()
1000 epc->domain_nr = pci_bus_find_domain_nr(NULL, dev); in __pci_epc_create()
1009 ret = dev_set_name(&epc->dev, "%s", dev_name(dev)); in __pci_epc_create()
1013 ret = device_add(&epc->dev); in __pci_epc_create()
1017 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev)); in __pci_epc_create()
1022 put_device(&epc->dev); in __pci_epc_create()
1030 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
1048 return ERR_PTR(-ENOMEM); in __devm_pci_epc_create()