1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
4 #include <linux/module.h>
13 void (*desc_changed)(void *ctx);
15 u32 __iomem *peer_mws[];
19 * ntb_msi_init() - Initialize the MSI context
20 * @ntb: NTB device context
22 * This function must be called before any other ntb_msi function.
23 * It initializes the context for MSI operations and maps
24 * the peer memory windows.
26 * This function reserves the last N outbound memory windows (where N
27 * is the number of peers).
29 * Return: Zero on success, otherwise a negative error number.
31 int ntb_msi_init(struct ntb_dev *ntb,
32 void (*desc_changed)(void *ctx))
34 phys_addr_t mw_phys_addr;
35 resource_size_t mw_size;
42 peers = ntb_peer_port_count(ntb);
46 struct_size = sizeof(*ntb->msi) + sizeof(*ntb->msi->peer_mws) * peers;
48 ntb->msi = devm_kzalloc(&ntb->dev, struct_size, GFP_KERNEL);
52 ntb->msi->desc_changed = desc_changed;
54 for (i = 0; i < peers; i++) {
55 peer_widx = ntb_peer_mw_count(ntb) - 1 - i;
57 ret = ntb_peer_mw_get_addr(ntb, peer_widx, &mw_phys_addr,
62 ntb->msi->peer_mws[i] = devm_ioremap(&ntb->dev, mw_phys_addr,
64 if (!ntb->msi->peer_mws[i]) {
73 for (i = 0; i < peers; i++)
74 if (ntb->msi->peer_mws[i])
75 devm_iounmap(&ntb->dev, ntb->msi->peer_mws[i]);
77 devm_kfree(&ntb->dev, ntb->msi);
81 EXPORT_SYMBOL(ntb_msi_init);
84 * ntb_msi_setup_mws() - Initialize the MSI inbound memory windows
85 * @ntb: NTB device context
87 * This function sets up the required inbound memory windows. It should be
88 * called from a work function after a link up event.
90 * Over the entire network, this function will reserves the last N
91 * inbound memory windows for each peer (where N is the number of peers).
93 * ntb_msi_init() must be called before this function.
95 * Return: Zero on success, otherwise a negative error number.
97 int ntb_msi_setup_mws(struct ntb_dev *ntb)
99 struct msi_desc *desc;
102 resource_size_t addr_align, size_align, size_max;
103 resource_size_t mw_size = SZ_32K;
104 resource_size_t mw_min_size = mw_size;
111 desc = first_msi_entry(&ntb->pdev->dev);
112 addr = desc->msg.address_lo + ((uint64_t)desc->msg.address_hi << 32);
114 for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
115 peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
119 ret = ntb_mw_get_align(ntb, peer, peer_widx, &addr_align,
124 addr &= ~(addr_align - 1);
127 for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
128 peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
134 ret = ntb_mw_get_align(ntb, peer, peer_widx, NULL,
135 &size_align, &size_max);
139 mw_size = round_up(mw_size, size_align);
140 mw_size = max(mw_size, size_max);
141 if (mw_size < mw_min_size)
142 mw_min_size = mw_size;
144 ret = ntb_mw_set_trans(ntb, peer, peer_widx,
150 ntb->msi->base_addr = addr;
151 ntb->msi->end_addr = addr + mw_min_size;
156 for (i = 0; i < peer; i++) {
157 peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
161 ntb_mw_clear_trans(ntb, i, peer_widx);
166 EXPORT_SYMBOL(ntb_msi_setup_mws);
169 * ntb_msi_clear_mws() - Clear all inbound memory windows
170 * @ntb: NTB device context
172 * This function tears down the resources used by ntb_msi_setup_mws().
174 void ntb_msi_clear_mws(struct ntb_dev *ntb)
179 for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
180 peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
184 ntb_mw_clear_trans(ntb, peer, peer_widx);
187 EXPORT_SYMBOL(ntb_msi_clear_mws);
189 struct ntb_msi_devres {
191 struct msi_desc *entry;
192 struct ntb_msi_desc *msi_desc;
195 static int ntb_msi_set_desc(struct ntb_dev *ntb, struct msi_desc *entry,
196 struct ntb_msi_desc *msi_desc)
200 addr = entry->msg.address_lo +
201 ((uint64_t)entry->msg.address_hi << 32);
203 if (addr < ntb->msi->base_addr || addr >= ntb->msi->end_addr) {
204 dev_warn_once(&ntb->dev,
205 "IRQ %d: MSI Address not within the memory window (%llx, [%llx %llx])\n",
206 entry->irq, addr, ntb->msi->base_addr,
211 msi_desc->addr_offset = addr - ntb->msi->base_addr;
212 msi_desc->data = entry->msg.data;
217 static void ntb_msi_write_msg(struct msi_desc *entry, void *data)
219 struct ntb_msi_devres *dr = data;
221 WARN_ON(ntb_msi_set_desc(dr->ntb, entry, dr->msi_desc));
223 if (dr->ntb->msi->desc_changed)
224 dr->ntb->msi->desc_changed(dr->ntb->ctx);
227 static void ntbm_msi_callback_release(struct device *dev, void *res)
229 struct ntb_msi_devres *dr = res;
231 dr->entry->write_msi_msg = NULL;
232 dr->entry->write_msi_msg_data = NULL;
235 static int ntbm_msi_setup_callback(struct ntb_dev *ntb, struct msi_desc *entry,
236 struct ntb_msi_desc *msi_desc)
238 struct ntb_msi_devres *dr;
240 dr = devres_alloc(ntbm_msi_callback_release,
241 sizeof(struct ntb_msi_devres), GFP_KERNEL);
247 dr->msi_desc = msi_desc;
249 devres_add(&ntb->dev, dr);
251 dr->entry->write_msi_msg = ntb_msi_write_msg;
252 dr->entry->write_msi_msg_data = dr;
258 * ntbm_msi_request_threaded_irq() - allocate an MSI interrupt
259 * @ntb: NTB device context
260 * @handler: Function to be called when the IRQ occurs
261 * @thread_fn: Function to be called in a threaded interrupt context. NULL
262 * for clients which handle everything in @handler
263 * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
264 * @dev_id: A cookie passed back to the handler function
266 * This function assigns an interrupt handler to an unused
267 * MSI interrupt and returns the descriptor used to trigger
268 * it. The descriptor can then be sent to a peer to trigger
271 * The interrupt resource is managed with devres so it will
272 * be automatically freed when the NTB device is torn down.
274 * If an IRQ allocated with this function needs to be freed
275 * separately, ntbm_free_irq() must be used.
277 * Return: IRQ number assigned on success, otherwise a negative error number.
279 int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb, irq_handler_t handler,
280 irq_handler_t thread_fn,
281 const char *name, void *dev_id,
282 struct ntb_msi_desc *msi_desc)
284 struct msi_desc *entry;
285 struct irq_desc *desc;
291 for_each_pci_msi_entry(entry, ntb->pdev) {
292 desc = irq_to_desc(entry->irq);
296 ret = devm_request_threaded_irq(&ntb->dev, entry->irq, handler,
297 thread_fn, 0, name, dev_id);
301 if (ntb_msi_set_desc(ntb, entry, msi_desc)) {
302 devm_free_irq(&ntb->dev, entry->irq, dev_id);
306 ret = ntbm_msi_setup_callback(ntb, entry, msi_desc);
308 devm_free_irq(&ntb->dev, entry->irq, dev_id);
318 EXPORT_SYMBOL(ntbm_msi_request_threaded_irq);
320 static int ntbm_msi_callback_match(struct device *dev, void *res, void *data)
322 struct ntb_dev *ntb = dev_ntb(dev);
323 struct ntb_msi_devres *dr = res;
325 return dr->ntb == ntb && dr->entry == data;
329 * ntbm_msi_free_irq() - free an interrupt
330 * @ntb: NTB device context
331 * @irq: Interrupt line to free
332 * @dev_id: Device identity to free
334 * This function should be used to manually free IRQs allocated with
335 * ntbm_request_[threaded_]irq().
337 void ntbm_msi_free_irq(struct ntb_dev *ntb, unsigned int irq, void *dev_id)
339 struct msi_desc *entry = irq_get_msi_desc(irq);
341 entry->write_msi_msg = NULL;
342 entry->write_msi_msg_data = NULL;
344 WARN_ON(devres_destroy(&ntb->dev, ntbm_msi_callback_release,
345 ntbm_msi_callback_match, entry));
347 devm_free_irq(&ntb->dev, irq, dev_id);
349 EXPORT_SYMBOL(ntbm_msi_free_irq);
352 * ntb_msi_peer_trigger() - Trigger an interrupt handler on a peer
353 * @ntb: NTB device context
355 * @desc: MSI descriptor data which triggers the interrupt
357 * This function triggers an interrupt on a peer. It requires
358 * the descriptor structure to have been passed from that peer
359 * by some other means.
361 * Return: Zero on success, otherwise a negative error number.
363 int ntb_msi_peer_trigger(struct ntb_dev *ntb, int peer,
364 struct ntb_msi_desc *desc)
371 idx = desc->addr_offset / sizeof(*ntb->msi->peer_mws[peer]);
373 iowrite32(desc->data, &ntb->msi->peer_mws[peer][idx]);
377 EXPORT_SYMBOL(ntb_msi_peer_trigger);
380 * ntb_msi_peer_addr() - Get the DMA address to trigger a peer's MSI interrupt
381 * @ntb: NTB device context
383 * @desc: MSI descriptor data which triggers the interrupt
384 * @msi_addr: Physical address to trigger the interrupt
386 * This function allows using DMA engines to trigger an interrupt
387 * (for example, trigger an interrupt to process the data after
388 * sending it). To trigger the interrupt, write @desc.data to the address
389 * returned in @msi_addr
391 * Return: Zero on success, otherwise a negative error number.
393 int ntb_msi_peer_addr(struct ntb_dev *ntb, int peer,
394 struct ntb_msi_desc *desc,
395 phys_addr_t *msi_addr)
397 int peer_widx = ntb_peer_mw_count(ntb) - 1 - peer;
398 phys_addr_t mw_phys_addr;
401 ret = ntb_peer_mw_get_addr(ntb, peer_widx, &mw_phys_addr, NULL);
406 *msi_addr = mw_phys_addr + desc->addr_offset;
410 EXPORT_SYMBOL(ntb_msi_peer_addr);