Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/ntb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/msi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) struct ntb_msi {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 	u64 base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 	u64 end_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	void (*desc_changed)(void *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	u32 __iomem *peer_mws[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * ntb_msi_init() - Initialize the MSI context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * @ntb:	NTB device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * This function must be called before any other ntb_msi function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * It initializes the context for MSI operations and maps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * the peer memory windows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * This function reserves the last N outbound memory windows (where N
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * is the number of peers).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * Return: Zero on success, otherwise a negative error number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) int ntb_msi_init(struct ntb_dev *ntb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		 void (*desc_changed)(void *ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	phys_addr_t mw_phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	resource_size_t mw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	size_t struct_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int peer_widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	peers = ntb_peer_port_count(ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (peers <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct_size = sizeof(*ntb->msi) + sizeof(*ntb->msi->peer_mws) * peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	ntb->msi = devm_kzalloc(&ntb->dev, struct_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (!ntb->msi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	ntb->msi->desc_changed = desc_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	for (i = 0; i < peers; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		peer_widx = ntb_peer_mw_count(ntb) - 1 - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		ret = ntb_peer_mw_get_addr(ntb, peer_widx, &mw_phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 					   &mw_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			goto unroll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		ntb->msi->peer_mws[i] = devm_ioremap(&ntb->dev, mw_phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 						     mw_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		if (!ntb->msi->peer_mws[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			goto unroll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) unroll:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	for (i = 0; i < peers; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		if (ntb->msi->peer_mws[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			devm_iounmap(&ntb->dev, ntb->msi->peer_mws[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	devm_kfree(&ntb->dev, ntb->msi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	ntb->msi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) EXPORT_SYMBOL(ntb_msi_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * ntb_msi_setup_mws() - Initialize the MSI inbound memory windows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * @ntb:	NTB device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * This function sets up the required inbound memory windows. It should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * called from a work function after a link up event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * Over the entire network, this function will reserves the last N
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * inbound memory windows for each peer (where N is the number of peers).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * ntb_msi_init() must be called before this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * Return: Zero on success, otherwise a negative error number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) int ntb_msi_setup_mws(struct ntb_dev *ntb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct msi_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	u64 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int peer, peer_widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	resource_size_t addr_align, size_align, size_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	resource_size_t mw_size = SZ_32K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	resource_size_t mw_min_size = mw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (!ntb->msi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	desc = first_msi_entry(&ntb->pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	addr = desc->msg.address_lo + ((uint64_t)desc->msg.address_hi << 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		if (peer_widx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			return peer_widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		ret = ntb_mw_get_align(ntb, peer, peer_widx, &addr_align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				       NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		addr &= ~(addr_align - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (peer_widx < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			ret = peer_widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		ret = ntb_mw_get_align(ntb, peer, peer_widx, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 				       &size_align, &size_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		mw_size = round_up(mw_size, size_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		mw_size = max(mw_size, size_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (mw_size < mw_min_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			mw_min_size = mw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		ret = ntb_mw_set_trans(ntb, peer, peer_widx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				       addr, mw_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	ntb->msi->base_addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	ntb->msi->end_addr = addr + mw_min_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	for (i = 0; i < peer; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		if (peer_widx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		ntb_mw_clear_trans(ntb, i, peer_widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) EXPORT_SYMBOL(ntb_msi_setup_mws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * ntb_msi_clear_mws() - Clear all inbound memory windows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * @ntb:	NTB device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * This function tears down the resources used by ntb_msi_setup_mws().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) void ntb_msi_clear_mws(struct ntb_dev *ntb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int peer_widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (peer_widx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		ntb_mw_clear_trans(ntb, peer, peer_widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) EXPORT_SYMBOL(ntb_msi_clear_mws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct ntb_msi_devres {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct ntb_dev *ntb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct msi_desc *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	struct ntb_msi_desc *msi_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int ntb_msi_set_desc(struct ntb_dev *ntb, struct msi_desc *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			    struct ntb_msi_desc *msi_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	u64 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	addr = entry->msg.address_lo +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		((uint64_t)entry->msg.address_hi << 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (addr < ntb->msi->base_addr || addr >= ntb->msi->end_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		dev_warn_once(&ntb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			      "IRQ %d: MSI Address not within the memory window (%llx, [%llx %llx])\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			      entry->irq, addr, ntb->msi->base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			      ntb->msi->end_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	msi_desc->addr_offset = addr - ntb->msi->base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	msi_desc->data = entry->msg.data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static void ntb_msi_write_msg(struct msi_desc *entry, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct ntb_msi_devres *dr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	WARN_ON(ntb_msi_set_desc(dr->ntb, entry, dr->msi_desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (dr->ntb->msi->desc_changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		dr->ntb->msi->desc_changed(dr->ntb->ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void ntbm_msi_callback_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct ntb_msi_devres *dr = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	dr->entry->write_msi_msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	dr->entry->write_msi_msg_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int ntbm_msi_setup_callback(struct ntb_dev *ntb, struct msi_desc *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				   struct ntb_msi_desc *msi_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct ntb_msi_devres *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	dr = devres_alloc(ntbm_msi_callback_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			  sizeof(struct ntb_msi_devres), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	dr->ntb = ntb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	dr->entry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	dr->msi_desc = msi_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	devres_add(&ntb->dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	dr->entry->write_msi_msg = ntb_msi_write_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	dr->entry->write_msi_msg_data = dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * ntbm_msi_request_threaded_irq() - allocate an MSI interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  * @ntb:	NTB device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * @handler:	Function to be called when the IRQ occurs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * @thread_fn:  Function to be called in a threaded interrupt context. NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  *              for clients which handle everything in @handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  * @devname:    An ascii name for the claiming device, dev_name(dev) if NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  * @dev_id:     A cookie passed back to the handler function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * This function assigns an interrupt handler to an unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  * MSI interrupt and returns the descriptor used to trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  * it. The descriptor can then be sent to a peer to trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  * the interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * The interrupt resource is managed with devres so it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * be automatically freed when the NTB device is torn down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * If an IRQ allocated with this function needs to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * separately, ntbm_free_irq() must be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  * Return: IRQ number assigned on success, otherwise a negative error number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb, irq_handler_t handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				  irq_handler_t thread_fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				  const char *name, void *dev_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				  struct ntb_msi_desc *msi_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct msi_desc *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (!ntb->msi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	for_each_pci_msi_entry(entry, ntb->pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		desc = irq_to_desc(entry->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (desc->action)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		ret = devm_request_threaded_irq(&ntb->dev, entry->irq, handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 						thread_fn, 0, name, dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		if (ntb_msi_set_desc(ntb, entry, msi_desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			devm_free_irq(&ntb->dev, entry->irq, dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		ret = ntbm_msi_setup_callback(ntb, entry, msi_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			devm_free_irq(&ntb->dev, entry->irq, dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return entry->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) EXPORT_SYMBOL(ntbm_msi_request_threaded_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int ntbm_msi_callback_match(struct device *dev, void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct ntb_dev *ntb = dev_ntb(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct ntb_msi_devres *dr = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return dr->ntb == ntb && dr->entry == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  * ntbm_msi_free_irq() - free an interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  * @ntb:	NTB device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  * @irq:	Interrupt line to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  * @dev_id:	Device identity to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  * This function should be used to manually free IRQs allocated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  * ntbm_request_[threaded_]irq().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) void ntbm_msi_free_irq(struct ntb_dev *ntb, unsigned int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct msi_desc *entry = irq_get_msi_desc(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	entry->write_msi_msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	entry->write_msi_msg_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	WARN_ON(devres_destroy(&ntb->dev, ntbm_msi_callback_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			       ntbm_msi_callback_match, entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	devm_free_irq(&ntb->dev, irq, dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) EXPORT_SYMBOL(ntbm_msi_free_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * ntb_msi_peer_trigger() - Trigger an interrupt handler on a peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  * @ntb:	NTB device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  * @peer:	Peer index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  * @desc:	MSI descriptor data which triggers the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * This function triggers an interrupt on a peer. It requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  * the descriptor structure to have been passed from that peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  * by some other means.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * Return: Zero on success, otherwise a negative error number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int ntb_msi_peer_trigger(struct ntb_dev *ntb, int peer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			 struct ntb_msi_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (!ntb->msi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	idx = desc->addr_offset / sizeof(*ntb->msi->peer_mws[peer]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	iowrite32(desc->data, &ntb->msi->peer_mws[peer][idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) EXPORT_SYMBOL(ntb_msi_peer_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * ntb_msi_peer_addr() - Get the DMA address to trigger a peer's MSI interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * @ntb:	NTB device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * @peer:	Peer index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  * @desc:	MSI descriptor data which triggers the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  * @msi_addr:   Physical address to trigger the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * This function allows using DMA engines to trigger an interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  * (for example, trigger an interrupt to process the data after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * sending it). To trigger the interrupt, write @desc.data to the address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  * returned in @msi_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  * Return: Zero on success, otherwise a negative error number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) int ntb_msi_peer_addr(struct ntb_dev *ntb, int peer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		      struct ntb_msi_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		      phys_addr_t *msi_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	int peer_widx = ntb_peer_mw_count(ntb) - 1 - peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	phys_addr_t mw_phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	ret = ntb_peer_mw_get_addr(ntb, peer_widx, &mw_phys_addr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (msi_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		*msi_addr = mw_phys_addr + desc->addr_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) EXPORT_SYMBOL(ntb_msi_peer_addr);