152a65ff5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2f3cf8bb0SJiang Liu /*
3f3cf8bb0SJiang Liu * Copyright (C) 2014 Intel Corp.
4f3cf8bb0SJiang Liu * Author: Jiang Liu <jiang.liu@linux.intel.com>
5f3cf8bb0SJiang Liu *
6f3cf8bb0SJiang Liu * This file is licensed under GPLv2.
7f3cf8bb0SJiang Liu *
8a359f757SIngo Molnar * This file contains common code to support Message Signaled Interrupts for
9f3cf8bb0SJiang Liu * PCI compatible and non PCI compatible devices.
10f3cf8bb0SJiang Liu */
11aeeb5965SJiang Liu #include <linux/types.h>
12aeeb5965SJiang Liu #include <linux/device.h>
13f3cf8bb0SJiang Liu #include <linux/irq.h>
14f3cf8bb0SJiang Liu #include <linux/irqdomain.h>
15f3cf8bb0SJiang Liu #include <linux/msi.h>
164e201566SMarc Zyngier #include <linux/slab.h>
173ba1f050SThomas Gleixner #include <linux/sysfs.h>
182f170814SBarry Song #include <linux/pci.h>
19d9109698SJiang Liu
2007557ccbSThomas Gleixner #include "internals.h"
2107557ccbSThomas Gleixner
22377712c5SThomas Gleixner /**
23377712c5SThomas Gleixner * struct msi_ctrl - MSI internal management control structure
24377712c5SThomas Gleixner * @domid: ID of the domain on which management operations should be done
25377712c5SThomas Gleixner * @first: First (hardware) slot index to operate on
26377712c5SThomas Gleixner * @last: Last (hardware) slot index to operate on
27f2480e7dSThomas Gleixner * @nirqs: The number of Linux interrupts to allocate. Can be larger
28f2480e7dSThomas Gleixner * than the range due to PCI/multi-MSI.
29377712c5SThomas Gleixner */
30377712c5SThomas Gleixner struct msi_ctrl {
31377712c5SThomas Gleixner unsigned int domid;
32377712c5SThomas Gleixner unsigned int first;
33377712c5SThomas Gleixner unsigned int last;
34f2480e7dSThomas Gleixner unsigned int nirqs;
35377712c5SThomas Gleixner };
36377712c5SThomas Gleixner
3794ff94cfSThomas Gleixner /* Invalid Xarray index which is outside of any searchable range */
3894ff94cfSThomas Gleixner #define MSI_XA_MAX_INDEX (ULONG_MAX - 1)
3994ff94cfSThomas Gleixner /* The maximum domain size */
4094ff94cfSThomas Gleixner #define MSI_XA_DOMAIN_SIZE (MSI_MAX_INDEX + 1)
4194ff94cfSThomas Gleixner
42f2480e7dSThomas Gleixner static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl);
4336db3d90SThomas Gleixner static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid);
44bf5e758fSThomas Gleixner static inline int msi_sysfs_create_group(struct device *dev);
45cc9a246dSThomas Gleixner
4694ff94cfSThomas Gleixner
4728f4b041SThomas Gleixner /**
48cc9a246dSThomas Gleixner * msi_alloc_desc - Allocate an initialized msi_desc
4928f4b041SThomas Gleixner * @dev: Pointer to the device for which this is allocated
5028f4b041SThomas Gleixner * @nvec: The number of vectors used in this entry
5128f4b041SThomas Gleixner * @affinity: Optional pointer to an affinity mask array size of @nvec
5228f4b041SThomas Gleixner *
533b35e7e6SRandy Dunlap * If @affinity is not %NULL then an affinity array[@nvec] is allocated
54bec04037SDou Liyang * and the affinity masks and flags from @affinity are copied.
553b35e7e6SRandy Dunlap *
563b35e7e6SRandy Dunlap * Return: pointer to allocated &msi_desc on success or %NULL on failure
5728f4b041SThomas Gleixner */
msi_alloc_desc(struct device * dev,int nvec,const struct irq_affinity_desc * affinity)58cc9a246dSThomas Gleixner static struct msi_desc *msi_alloc_desc(struct device *dev, int nvec,
59bec04037SDou Liyang const struct irq_affinity_desc *affinity)
60aa48b6f7SJiang Liu {
61cc9a246dSThomas Gleixner struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
6228f4b041SThomas Gleixner
63aa48b6f7SJiang Liu if (!desc)
64aa48b6f7SJiang Liu return NULL;
65aa48b6f7SJiang Liu
66aa48b6f7SJiang Liu desc->dev = dev;
6728f4b041SThomas Gleixner desc->nvec_used = nvec;
6828f4b041SThomas Gleixner if (affinity) {
69cc9a246dSThomas Gleixner desc->affinity = kmemdup(affinity, nvec * sizeof(*desc->affinity), GFP_KERNEL);
7028f4b041SThomas Gleixner if (!desc->affinity) {
7128f4b041SThomas Gleixner kfree(desc);
7228f4b041SThomas Gleixner return NULL;
7328f4b041SThomas Gleixner }
7428f4b041SThomas Gleixner }
75aa48b6f7SJiang Liu return desc;
76aa48b6f7SJiang Liu }
77aa48b6f7SJiang Liu
msi_free_desc(struct msi_desc * desc)78cc9a246dSThomas Gleixner static void msi_free_desc(struct msi_desc *desc)
79aa48b6f7SJiang Liu {
80cc9a246dSThomas Gleixner kfree(desc->affinity);
81cc9a246dSThomas Gleixner kfree(desc);
82aa48b6f7SJiang Liu }
83aa48b6f7SJiang Liu
msi_insert_desc(struct device * dev,struct msi_desc * desc,unsigned int domid,unsigned int index)8436db3d90SThomas Gleixner static int msi_insert_desc(struct device *dev, struct msi_desc *desc,
85fc8ab388SThomas Gleixner unsigned int domid, unsigned int index)
86cd6cf065SThomas Gleixner {
8736db3d90SThomas Gleixner struct msi_device_data *md = dev->msi.data;
88fc8ab388SThomas Gleixner struct xarray *xa = &md->__domains[domid].store;
8936db3d90SThomas Gleixner unsigned int hwsize;
90cd6cf065SThomas Gleixner int ret;
91cd6cf065SThomas Gleixner
9236db3d90SThomas Gleixner hwsize = msi_domain_get_hwsize(dev, domid);
933d393b21SThomas Gleixner
943d393b21SThomas Gleixner if (index == MSI_ANY_INDEX) {
953d393b21SThomas Gleixner struct xa_limit limit = { .min = 0, .max = hwsize - 1 };
963d393b21SThomas Gleixner unsigned int index;
973d393b21SThomas Gleixner
983d393b21SThomas Gleixner /* Let the xarray allocate a free index within the limit */
993d393b21SThomas Gleixner ret = xa_alloc(xa, &index, desc, limit, GFP_KERNEL);
1003d393b21SThomas Gleixner if (ret)
1013d393b21SThomas Gleixner goto fail;
1023d393b21SThomas Gleixner
1033d393b21SThomas Gleixner desc->msi_index = index;
1043d393b21SThomas Gleixner return 0;
1053d393b21SThomas Gleixner } else {
10636db3d90SThomas Gleixner if (index >= hwsize) {
10736db3d90SThomas Gleixner ret = -ERANGE;
10836db3d90SThomas Gleixner goto fail;
10936db3d90SThomas Gleixner }
11036db3d90SThomas Gleixner
111cd6cf065SThomas Gleixner desc->msi_index = index;
112f1139f90SThomas Gleixner ret = xa_insert(xa, index, desc, GFP_KERNEL);
113cd6cf065SThomas Gleixner if (ret)
11436db3d90SThomas Gleixner goto fail;
11536db3d90SThomas Gleixner return 0;
1163d393b21SThomas Gleixner }
11736db3d90SThomas Gleixner fail:
118cd6cf065SThomas Gleixner msi_free_desc(desc);
119cd6cf065SThomas Gleixner return ret;
120cd6cf065SThomas Gleixner }
121cd6cf065SThomas Gleixner
12260290525SThomas Gleixner /**
123fc8ab388SThomas Gleixner * msi_domain_insert_msi_desc - Allocate and initialize a MSI descriptor and
1241c893963SThomas Gleixner * insert it at @init_desc->msi_index
1251c893963SThomas Gleixner *
12660290525SThomas Gleixner * @dev: Pointer to the device for which the descriptor is allocated
127fc8ab388SThomas Gleixner * @domid: The id of the interrupt domain to which the desriptor is added
12860290525SThomas Gleixner * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor
12960290525SThomas Gleixner *
13060290525SThomas Gleixner * Return: 0 on success or an appropriate failure code.
13160290525SThomas Gleixner */
msi_domain_insert_msi_desc(struct device * dev,unsigned int domid,struct msi_desc * init_desc)132fc8ab388SThomas Gleixner int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
133fc8ab388SThomas Gleixner struct msi_desc *init_desc)
13460290525SThomas Gleixner {
13560290525SThomas Gleixner struct msi_desc *desc;
13660290525SThomas Gleixner
13760290525SThomas Gleixner lockdep_assert_held(&dev->msi.data->mutex);
13860290525SThomas Gleixner
139cc9a246dSThomas Gleixner desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity);
14060290525SThomas Gleixner if (!desc)
14160290525SThomas Gleixner return -ENOMEM;
14260290525SThomas Gleixner
143cd6cf065SThomas Gleixner /* Copy type specific data to the new descriptor. */
14460290525SThomas Gleixner desc->pci = init_desc->pci;
145fc8ab388SThomas Gleixner
14636db3d90SThomas Gleixner return msi_insert_desc(dev, desc, domid, init_desc->msi_index);
14760290525SThomas Gleixner }
14860290525SThomas Gleixner
msi_desc_match(struct msi_desc * desc,enum msi_desc_filter filter)149cd6cf065SThomas Gleixner static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter)
150cd6cf065SThomas Gleixner {
151cd6cf065SThomas Gleixner switch (filter) {
152cd6cf065SThomas Gleixner case MSI_DESC_ALL:
153cd6cf065SThomas Gleixner return true;
154cd6cf065SThomas Gleixner case MSI_DESC_NOTASSOCIATED:
155cd6cf065SThomas Gleixner return !desc->irq;
156cd6cf065SThomas Gleixner case MSI_DESC_ASSOCIATED:
157cd6cf065SThomas Gleixner return !!desc->irq;
158cd6cf065SThomas Gleixner }
159cd6cf065SThomas Gleixner WARN_ON_ONCE(1);
160cd6cf065SThomas Gleixner return false;
16160290525SThomas Gleixner }
16260290525SThomas Gleixner
msi_ctrl_valid(struct device * dev,struct msi_ctrl * ctrl)163377712c5SThomas Gleixner static bool msi_ctrl_valid(struct device *dev, struct msi_ctrl *ctrl)
164645474e2SThomas Gleixner {
16536db3d90SThomas Gleixner unsigned int hwsize;
16636db3d90SThomas Gleixner
167377712c5SThomas Gleixner if (WARN_ON_ONCE(ctrl->domid >= MSI_MAX_DEVICE_IRQDOMAINS ||
168db3568fdSMarc Zyngier (dev->msi.domain &&
169db3568fdSMarc Zyngier !dev->msi.data->__domains[ctrl->domid].domain)))
17036db3d90SThomas Gleixner return false;
17136db3d90SThomas Gleixner
17236db3d90SThomas Gleixner hwsize = msi_domain_get_hwsize(dev, ctrl->domid);
17336db3d90SThomas Gleixner if (WARN_ON_ONCE(ctrl->first > ctrl->last ||
17436db3d90SThomas Gleixner ctrl->first >= hwsize ||
17536db3d90SThomas Gleixner ctrl->last >= hwsize))
176377712c5SThomas Gleixner return false;
177377712c5SThomas Gleixner return true;
178377712c5SThomas Gleixner }
179377712c5SThomas Gleixner
msi_domain_free_descs(struct device * dev,struct msi_ctrl * ctrl)180377712c5SThomas Gleixner static void msi_domain_free_descs(struct device *dev, struct msi_ctrl *ctrl)
181377712c5SThomas Gleixner {
182645474e2SThomas Gleixner struct msi_desc *desc;
183377712c5SThomas Gleixner struct xarray *xa;
184cd6cf065SThomas Gleixner unsigned long idx;
185645474e2SThomas Gleixner
186645474e2SThomas Gleixner lockdep_assert_held(&dev->msi.data->mutex);
187645474e2SThomas Gleixner
188377712c5SThomas Gleixner if (!msi_ctrl_valid(dev, ctrl))
189377712c5SThomas Gleixner return;
190377712c5SThomas Gleixner
191377712c5SThomas Gleixner xa = &dev->msi.data->__domains[ctrl->domid].store;
192377712c5SThomas Gleixner xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
193cd6cf065SThomas Gleixner xa_erase(xa, idx);
1942f2940d1SThomas Gleixner
1952f2940d1SThomas Gleixner /* Leak the descriptor when it is still referenced */
1962f2940d1SThomas Gleixner if (WARN_ON_ONCE(msi_desc_match(desc, MSI_DESC_ASSOCIATED)))
1972f2940d1SThomas Gleixner continue;
198cc9a246dSThomas Gleixner msi_free_desc(desc);
199645474e2SThomas Gleixner }
200645474e2SThomas Gleixner }
201645474e2SThomas Gleixner
202377712c5SThomas Gleixner /**
203377712c5SThomas Gleixner * msi_domain_free_msi_descs_range - Free a range of MSI descriptors of a device in an irqdomain
204377712c5SThomas Gleixner * @dev: Device for which to free the descriptors
205377712c5SThomas Gleixner * @domid: Id of the domain to operate on
206377712c5SThomas Gleixner * @first: Index to start freeing from (inclusive)
207377712c5SThomas Gleixner * @last: Last index to be freed (inclusive)
208377712c5SThomas Gleixner */
msi_domain_free_msi_descs_range(struct device * dev,unsigned int domid,unsigned int first,unsigned int last)209377712c5SThomas Gleixner void msi_domain_free_msi_descs_range(struct device *dev, unsigned int domid,
210377712c5SThomas Gleixner unsigned int first, unsigned int last)
211377712c5SThomas Gleixner {
212377712c5SThomas Gleixner struct msi_ctrl ctrl = {
213377712c5SThomas Gleixner .domid = domid,
214377712c5SThomas Gleixner .first = first,
215377712c5SThomas Gleixner .last = last,
216377712c5SThomas Gleixner };
217377712c5SThomas Gleixner
218377712c5SThomas Gleixner msi_domain_free_descs(dev, &ctrl);
219377712c5SThomas Gleixner }
220377712c5SThomas Gleixner
22140742716SThomas Gleixner /**
22240742716SThomas Gleixner * msi_domain_add_simple_msi_descs - Allocate and initialize MSI descriptors
22340742716SThomas Gleixner * @dev: Pointer to the device for which the descriptors are allocated
22440742716SThomas Gleixner * @ctrl: Allocation control struct
22540742716SThomas Gleixner *
22640742716SThomas Gleixner * Return: 0 on success or an appropriate failure code.
22740742716SThomas Gleixner */
msi_domain_add_simple_msi_descs(struct device * dev,struct msi_ctrl * ctrl)22840742716SThomas Gleixner static int msi_domain_add_simple_msi_descs(struct device *dev, struct msi_ctrl *ctrl)
22940742716SThomas Gleixner {
23040742716SThomas Gleixner struct msi_desc *desc;
23140742716SThomas Gleixner unsigned int idx;
23240742716SThomas Gleixner int ret;
23340742716SThomas Gleixner
23440742716SThomas Gleixner lockdep_assert_held(&dev->msi.data->mutex);
23540742716SThomas Gleixner
23640742716SThomas Gleixner if (!msi_ctrl_valid(dev, ctrl))
23740742716SThomas Gleixner return -EINVAL;
23840742716SThomas Gleixner
23940742716SThomas Gleixner for (idx = ctrl->first; idx <= ctrl->last; idx++) {
24040742716SThomas Gleixner desc = msi_alloc_desc(dev, 1, NULL);
24140742716SThomas Gleixner if (!desc)
24240742716SThomas Gleixner goto fail_mem;
24336db3d90SThomas Gleixner ret = msi_insert_desc(dev, desc, ctrl->domid, idx);
24440742716SThomas Gleixner if (ret)
24540742716SThomas Gleixner goto fail;
24640742716SThomas Gleixner }
24740742716SThomas Gleixner return 0;
24840742716SThomas Gleixner
24940742716SThomas Gleixner fail_mem:
25040742716SThomas Gleixner ret = -ENOMEM;
25140742716SThomas Gleixner fail:
25240742716SThomas Gleixner msi_domain_free_descs(dev, ctrl);
25340742716SThomas Gleixner return ret;
25440742716SThomas Gleixner }
25540742716SThomas Gleixner
__get_cached_msi_msg(struct msi_desc * entry,struct msi_msg * msg)25638b6a1cfSJiang Liu void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
25738b6a1cfSJiang Liu {
25838b6a1cfSJiang Liu *msg = entry->msg;
25938b6a1cfSJiang Liu }
26038b6a1cfSJiang Liu
get_cached_msi_msg(unsigned int irq,struct msi_msg * msg)26138b6a1cfSJiang Liu void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
26238b6a1cfSJiang Liu {
26338b6a1cfSJiang Liu struct msi_desc *entry = irq_get_msi_desc(irq);
26438b6a1cfSJiang Liu
26538b6a1cfSJiang Liu __get_cached_msi_msg(entry, msg);
26638b6a1cfSJiang Liu }
26738b6a1cfSJiang Liu EXPORT_SYMBOL_GPL(get_cached_msi_msg);
26838b6a1cfSJiang Liu
msi_device_data_release(struct device * dev,void * res)269013bd8e5SThomas Gleixner static void msi_device_data_release(struct device *dev, void *res)
270013bd8e5SThomas Gleixner {
271125282cdSThomas Gleixner struct msi_device_data *md = res;
272f1139f90SThomas Gleixner int i;
273125282cdSThomas Gleixner
274f1139f90SThomas Gleixner for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++) {
27527a6dea3SThomas Gleixner msi_remove_device_irq_domain(dev, i);
276f1139f90SThomas Gleixner WARN_ON_ONCE(!xa_empty(&md->__domains[i].store));
277f1139f90SThomas Gleixner xa_destroy(&md->__domains[i].store);
278f1139f90SThomas Gleixner }
279013bd8e5SThomas Gleixner dev->msi.data = NULL;
280013bd8e5SThomas Gleixner }
281013bd8e5SThomas Gleixner
282013bd8e5SThomas Gleixner /**
283013bd8e5SThomas Gleixner * msi_setup_device_data - Setup MSI device data
284013bd8e5SThomas Gleixner * @dev: Device for which MSI device data should be set up
285013bd8e5SThomas Gleixner *
286013bd8e5SThomas Gleixner * Return: 0 on success, appropriate error code otherwise
287013bd8e5SThomas Gleixner *
288013bd8e5SThomas Gleixner * This can be called more than once for @dev. If the MSI device data is
289013bd8e5SThomas Gleixner * already allocated the call succeeds. The allocated memory is
290013bd8e5SThomas Gleixner * automatically released when the device is destroyed.
291013bd8e5SThomas Gleixner */
msi_setup_device_data(struct device * dev)292013bd8e5SThomas Gleixner int msi_setup_device_data(struct device *dev)
293013bd8e5SThomas Gleixner {
294013bd8e5SThomas Gleixner struct msi_device_data *md;
295f1139f90SThomas Gleixner int ret, i;
296013bd8e5SThomas Gleixner
297013bd8e5SThomas Gleixner if (dev->msi.data)
298013bd8e5SThomas Gleixner return 0;
299013bd8e5SThomas Gleixner
300013bd8e5SThomas Gleixner md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL);
301013bd8e5SThomas Gleixner if (!md)
302013bd8e5SThomas Gleixner return -ENOMEM;
303013bd8e5SThomas Gleixner
304bf5e758fSThomas Gleixner ret = msi_sysfs_create_group(dev);
305bf5e758fSThomas Gleixner if (ret) {
306bf5e758fSThomas Gleixner devres_free(md);
307bf5e758fSThomas Gleixner return ret;
308bf5e758fSThomas Gleixner }
309bf5e758fSThomas Gleixner
310f1139f90SThomas Gleixner for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++)
3113d393b21SThomas Gleixner xa_init_flags(&md->__domains[i].store, XA_FLAGS_ALLOC);
312f1139f90SThomas Gleixner
31364258eaaSThomas Gleixner /*
31464258eaaSThomas Gleixner * If @dev::msi::domain is set and is a global MSI domain, copy the
31564258eaaSThomas Gleixner * pointer into the domain array so all code can operate on domain
31664258eaaSThomas Gleixner * ids. The NULL pointer check is required to keep the legacy
31764258eaaSThomas Gleixner * architecture specific PCI/MSI support working.
31864258eaaSThomas Gleixner */
31964258eaaSThomas Gleixner if (dev->msi.domain && !irq_domain_is_msi_parent(dev->msi.domain))
32064258eaaSThomas Gleixner md->__domains[MSI_DEFAULT_DOMAIN].domain = dev->msi.domain;
32164258eaaSThomas Gleixner
322b5f687f9SThomas Gleixner mutex_init(&md->mutex);
323013bd8e5SThomas Gleixner dev->msi.data = md;
324013bd8e5SThomas Gleixner devres_add(dev, md);
325013bd8e5SThomas Gleixner return 0;
326013bd8e5SThomas Gleixner }
327013bd8e5SThomas Gleixner
328cf15f43aSThomas Gleixner /**
329b5f687f9SThomas Gleixner * msi_lock_descs - Lock the MSI descriptor storage of a device
330b5f687f9SThomas Gleixner * @dev: Device to operate on
331b5f687f9SThomas Gleixner */
msi_lock_descs(struct device * dev)332b5f687f9SThomas Gleixner void msi_lock_descs(struct device *dev)
333b5f687f9SThomas Gleixner {
334b5f687f9SThomas Gleixner mutex_lock(&dev->msi.data->mutex);
335b5f687f9SThomas Gleixner }
336b5f687f9SThomas Gleixner EXPORT_SYMBOL_GPL(msi_lock_descs);
337b5f687f9SThomas Gleixner
338b5f687f9SThomas Gleixner /**
339b5f687f9SThomas Gleixner * msi_unlock_descs - Unlock the MSI descriptor storage of a device
340b5f687f9SThomas Gleixner * @dev: Device to operate on
341b5f687f9SThomas Gleixner */
msi_unlock_descs(struct device * dev)342b5f687f9SThomas Gleixner void msi_unlock_descs(struct device *dev)
343b5f687f9SThomas Gleixner {
344f1139f90SThomas Gleixner /* Invalidate the index which was cached by the iterator */
34594ff94cfSThomas Gleixner dev->msi.data->__iter_idx = MSI_XA_MAX_INDEX;
346b5f687f9SThomas Gleixner mutex_unlock(&dev->msi.data->mutex);
347b5f687f9SThomas Gleixner }
348b5f687f9SThomas Gleixner EXPORT_SYMBOL_GPL(msi_unlock_descs);
349b5f687f9SThomas Gleixner
msi_find_desc(struct msi_device_data * md,unsigned int domid,enum msi_desc_filter filter)35094ff94cfSThomas Gleixner static struct msi_desc *msi_find_desc(struct msi_device_data *md, unsigned int domid,
35194ff94cfSThomas Gleixner enum msi_desc_filter filter)
3521046f71dSThomas Gleixner {
35394ff94cfSThomas Gleixner struct xarray *xa = &md->__domains[domid].store;
3541046f71dSThomas Gleixner struct msi_desc *desc;
3551046f71dSThomas Gleixner
356f1139f90SThomas Gleixner xa_for_each_start(xa, md->__iter_idx, desc, md->__iter_idx) {
3571046f71dSThomas Gleixner if (msi_desc_match(desc, filter))
3581046f71dSThomas Gleixner return desc;
3591046f71dSThomas Gleixner }
36094ff94cfSThomas Gleixner md->__iter_idx = MSI_XA_MAX_INDEX;
3611046f71dSThomas Gleixner return NULL;
3621046f71dSThomas Gleixner }
3631046f71dSThomas Gleixner
3641046f71dSThomas Gleixner /**
36594ff94cfSThomas Gleixner * msi_domain_first_desc - Get the first MSI descriptor of an irqdomain associated to a device
3661046f71dSThomas Gleixner * @dev: Device to operate on
36794ff94cfSThomas Gleixner * @domid: The id of the interrupt domain which should be walked.
3681046f71dSThomas Gleixner * @filter: Descriptor state filter
3691046f71dSThomas Gleixner *
3701046f71dSThomas Gleixner * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
3711046f71dSThomas Gleixner * must be invoked before the call.
3721046f71dSThomas Gleixner *
3731046f71dSThomas Gleixner * Return: Pointer to the first MSI descriptor matching the search
3741046f71dSThomas Gleixner * criteria, NULL if none found.
3751046f71dSThomas Gleixner */
msi_domain_first_desc(struct device * dev,unsigned int domid,enum msi_desc_filter filter)37694ff94cfSThomas Gleixner struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
37794ff94cfSThomas Gleixner enum msi_desc_filter filter)
3781046f71dSThomas Gleixner {
379cd6cf065SThomas Gleixner struct msi_device_data *md = dev->msi.data;
3801046f71dSThomas Gleixner
38194ff94cfSThomas Gleixner if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
3821046f71dSThomas Gleixner return NULL;
3831046f71dSThomas Gleixner
384cd6cf065SThomas Gleixner lockdep_assert_held(&md->mutex);
3851046f71dSThomas Gleixner
386cd6cf065SThomas Gleixner md->__iter_idx = 0;
38794ff94cfSThomas Gleixner return msi_find_desc(md, domid, filter);
3881046f71dSThomas Gleixner }
38994ff94cfSThomas Gleixner EXPORT_SYMBOL_GPL(msi_domain_first_desc);
3901046f71dSThomas Gleixner
3911046f71dSThomas Gleixner /**
3921046f71dSThomas Gleixner * msi_next_desc - Get the next MSI descriptor of a device
3931046f71dSThomas Gleixner * @dev: Device to operate on
39494ff94cfSThomas Gleixner * @domid: The id of the interrupt domain which should be walked.
395fdd53404SThomas Gleixner * @filter: Descriptor state filter
3961046f71dSThomas Gleixner *
3971046f71dSThomas Gleixner * The first invocation of msi_next_desc() has to be preceeded by a
398cd6cf065SThomas Gleixner * successful invocation of __msi_first_desc(). Consecutive invocations are
3991046f71dSThomas Gleixner * only valid if the previous one was successful. All these operations have
4001046f71dSThomas Gleixner * to be done within the same MSI mutex held region.
4011046f71dSThomas Gleixner *
4021046f71dSThomas Gleixner * Return: Pointer to the next MSI descriptor matching the search
4031046f71dSThomas Gleixner * criteria, NULL if none found.
4041046f71dSThomas Gleixner */
msi_next_desc(struct device * dev,unsigned int domid,enum msi_desc_filter filter)40594ff94cfSThomas Gleixner struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
40694ff94cfSThomas Gleixner enum msi_desc_filter filter)
4071046f71dSThomas Gleixner {
408cd6cf065SThomas Gleixner struct msi_device_data *md = dev->msi.data;
4091046f71dSThomas Gleixner
41094ff94cfSThomas Gleixner if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
4111046f71dSThomas Gleixner return NULL;
4121046f71dSThomas Gleixner
413cd6cf065SThomas Gleixner lockdep_assert_held(&md->mutex);
4141046f71dSThomas Gleixner
415cd6cf065SThomas Gleixner if (md->__iter_idx >= (unsigned long)MSI_MAX_INDEX)
4161046f71dSThomas Gleixner return NULL;
4171046f71dSThomas Gleixner
418cd6cf065SThomas Gleixner md->__iter_idx++;
41994ff94cfSThomas Gleixner return msi_find_desc(md, domid, filter);
4201046f71dSThomas Gleixner }
4211046f71dSThomas Gleixner EXPORT_SYMBOL_GPL(msi_next_desc);
4221046f71dSThomas Gleixner
423b5f687f9SThomas Gleixner /**
42498043704SAhmed S. Darwish * msi_domain_get_virq - Lookup the Linux interrupt number for a MSI index on a interrupt domain
425cf15f43aSThomas Gleixner * @dev: Device to operate on
42698043704SAhmed S. Darwish * @domid: Domain ID of the interrupt domain associated to the device
427cf15f43aSThomas Gleixner * @index: MSI interrupt index to look for (0-based)
428cf15f43aSThomas Gleixner *
429cf15f43aSThomas Gleixner * Return: The Linux interrupt number on success (> 0), 0 if not found
430cf15f43aSThomas Gleixner */
msi_domain_get_virq(struct device * dev,unsigned int domid,unsigned int index)43198043704SAhmed S. Darwish unsigned int msi_domain_get_virq(struct device *dev, unsigned int domid, unsigned int index)
432cf15f43aSThomas Gleixner {
433cf15f43aSThomas Gleixner struct msi_desc *desc;
434495c66acSThomas Gleixner unsigned int ret = 0;
43598043704SAhmed S. Darwish bool pcimsi = false;
436f1139f90SThomas Gleixner struct xarray *xa;
437cf15f43aSThomas Gleixner
438cf15f43aSThomas Gleixner if (!dev->msi.data)
439cf15f43aSThomas Gleixner return 0;
440cf15f43aSThomas Gleixner
44198043704SAhmed S. Darwish if (WARN_ON_ONCE(index > MSI_MAX_INDEX || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
44298043704SAhmed S. Darwish return 0;
44398043704SAhmed S. Darwish
44498043704SAhmed S. Darwish /* This check is only valid for the PCI default MSI domain */
44598043704SAhmed S. Darwish if (dev_is_pci(dev) && domid == MSI_DEFAULT_DOMAIN)
44698043704SAhmed S. Darwish pcimsi = to_pci_dev(dev)->msi_enabled;
447cf15f43aSThomas Gleixner
448495c66acSThomas Gleixner msi_lock_descs(dev);
44998043704SAhmed S. Darwish xa = &dev->msi.data->__domains[domid].store;
450f1139f90SThomas Gleixner desc = xa_load(xa, pcimsi ? 0 : index);
451cd6cf065SThomas Gleixner if (desc && desc->irq) {
452cf15f43aSThomas Gleixner /*
453cd6cf065SThomas Gleixner * PCI-MSI has only one descriptor for multiple interrupts.
454cf15f43aSThomas Gleixner * PCI-MSIX and platform MSI use a descriptor per
455cf15f43aSThomas Gleixner * interrupt.
456cf15f43aSThomas Gleixner */
457cd6cf065SThomas Gleixner if (pcimsi) {
458cd6cf065SThomas Gleixner if (index < desc->nvec_used)
459cd6cf065SThomas Gleixner ret = desc->irq + index;
460cd6cf065SThomas Gleixner } else {
461495c66acSThomas Gleixner ret = desc->irq;
462cf15f43aSThomas Gleixner }
463495c66acSThomas Gleixner }
46498043704SAhmed S. Darwish
465495c66acSThomas Gleixner msi_unlock_descs(dev);
466495c66acSThomas Gleixner return ret;
467cf15f43aSThomas Gleixner }
46898043704SAhmed S. Darwish EXPORT_SYMBOL_GPL(msi_domain_get_virq);
469cf15f43aSThomas Gleixner
4701197528aSThomas Gleixner #ifdef CONFIG_SYSFS
471bf5e758fSThomas Gleixner static struct attribute *msi_dev_attrs[] = {
472bf5e758fSThomas Gleixner NULL
473bf5e758fSThomas Gleixner };
474bf5e758fSThomas Gleixner
475bf5e758fSThomas Gleixner static const struct attribute_group msi_irqs_group = {
476bf5e758fSThomas Gleixner .name = "msi_irqs",
477bf5e758fSThomas Gleixner .attrs = msi_dev_attrs,
478bf5e758fSThomas Gleixner };
479bf5e758fSThomas Gleixner
msi_sysfs_create_group(struct device * dev)480bf5e758fSThomas Gleixner static inline int msi_sysfs_create_group(struct device *dev)
481bf5e758fSThomas Gleixner {
482bf5e758fSThomas Gleixner return devm_device_add_group(dev, &msi_irqs_group);
483bf5e758fSThomas Gleixner }
484bf5e758fSThomas Gleixner
msi_mode_show(struct device * dev,struct device_attribute * attr,char * buf)4852f170814SBarry Song static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
4862f170814SBarry Song char *buf)
4872f170814SBarry Song {
4886ef7f771SThomas Gleixner /* MSI vs. MSIX is per device not per interrupt */
4896ef7f771SThomas Gleixner bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false;
4902f170814SBarry Song
4912f170814SBarry Song return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi");
4922f170814SBarry Song }
4932f170814SBarry Song
msi_sysfs_remove_desc(struct device * dev,struct msi_desc * desc)494bf5e758fSThomas Gleixner static void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc)
4952f170814SBarry Song {
496bf5e758fSThomas Gleixner struct device_attribute *attrs = desc->sysfs_attrs;
4972f170814SBarry Song int i;
4982f170814SBarry Song
499bf5e758fSThomas Gleixner if (!attrs)
500bf5e758fSThomas Gleixner return;
5012f170814SBarry Song
502bf5e758fSThomas Gleixner desc->sysfs_attrs = NULL;
503bf5e758fSThomas Gleixner for (i = 0; i < desc->nvec_used; i++) {
504bf5e758fSThomas Gleixner if (attrs[i].show)
505bf5e758fSThomas Gleixner sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
506bf5e758fSThomas Gleixner kfree(attrs[i].attr.name);
5072f170814SBarry Song }
508bf5e758fSThomas Gleixner kfree(attrs);
5092f170814SBarry Song }
5102f170814SBarry Song
msi_sysfs_populate_desc(struct device * dev,struct msi_desc * desc)511bf5e758fSThomas Gleixner static int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc)
512bf5e758fSThomas Gleixner {
513bf5e758fSThomas Gleixner struct device_attribute *attrs;
514bf5e758fSThomas Gleixner int ret, i;
5152f170814SBarry Song
516bf5e758fSThomas Gleixner attrs = kcalloc(desc->nvec_used, sizeof(*attrs), GFP_KERNEL);
517bf5e758fSThomas Gleixner if (!attrs)
518bf5e758fSThomas Gleixner return -ENOMEM;
5192f170814SBarry Song
520bf5e758fSThomas Gleixner desc->sysfs_attrs = attrs;
521bf5e758fSThomas Gleixner for (i = 0; i < desc->nvec_used; i++) {
522bf5e758fSThomas Gleixner sysfs_attr_init(&attrs[i].attr);
523bf5e758fSThomas Gleixner attrs[i].attr.name = kasprintf(GFP_KERNEL, "%d", desc->irq + i);
524bf5e758fSThomas Gleixner if (!attrs[i].attr.name) {
525bf5e758fSThomas Gleixner ret = -ENOMEM;
526bf5e758fSThomas Gleixner goto fail;
5272f170814SBarry Song }
5282f170814SBarry Song
529bf5e758fSThomas Gleixner attrs[i].attr.mode = 0444;
530bf5e758fSThomas Gleixner attrs[i].show = msi_mode_show;
531bf5e758fSThomas Gleixner
532bf5e758fSThomas Gleixner ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
533bf5e758fSThomas Gleixner if (ret) {
534bf5e758fSThomas Gleixner attrs[i].show = NULL;
535bf5e758fSThomas Gleixner goto fail;
536bf5e758fSThomas Gleixner }
537bf5e758fSThomas Gleixner }
538bf5e758fSThomas Gleixner return 0;
539bf5e758fSThomas Gleixner
540bf5e758fSThomas Gleixner fail:
541bf5e758fSThomas Gleixner msi_sysfs_remove_desc(dev, desc);
542bf5e758fSThomas Gleixner return ret;
543bf5e758fSThomas Gleixner }
544bf5e758fSThomas Gleixner
545335b4223SMaximilian Heyne #if defined(CONFIG_PCI_MSI_ARCH_FALLBACKS) || defined(CONFIG_PCI_XEN)
5462f170814SBarry Song /**
547bf6e054eSThomas Gleixner * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device
548bf6e054eSThomas Gleixner * @dev: The device (PCI, platform etc) which will get sysfs entries
549bf6e054eSThomas Gleixner */
msi_device_populate_sysfs(struct device * dev)550bf6e054eSThomas Gleixner int msi_device_populate_sysfs(struct device *dev)
551bf6e054eSThomas Gleixner {
552bf5e758fSThomas Gleixner struct msi_desc *desc;
553bf5e758fSThomas Gleixner int ret;
554bf6e054eSThomas Gleixner
555bf5e758fSThomas Gleixner msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) {
556bf5e758fSThomas Gleixner if (desc->sysfs_attrs)
557bf5e758fSThomas Gleixner continue;
558bf5e758fSThomas Gleixner ret = msi_sysfs_populate_desc(dev, desc);
559bf5e758fSThomas Gleixner if (ret)
560bf5e758fSThomas Gleixner return ret;
561bf5e758fSThomas Gleixner }
562bf6e054eSThomas Gleixner return 0;
563bf6e054eSThomas Gleixner }
564bf6e054eSThomas Gleixner
565bf6e054eSThomas Gleixner /**
56624cff375SThomas Gleixner * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device
56724cff375SThomas Gleixner * @dev: The device (PCI, platform etc) for which to remove
56824cff375SThomas Gleixner * sysfs entries
5692f170814SBarry Song */
msi_device_destroy_sysfs(struct device * dev)57024cff375SThomas Gleixner void msi_device_destroy_sysfs(struct device *dev)
5712f170814SBarry Song {
572bf5e758fSThomas Gleixner struct msi_desc *desc;
5732f170814SBarry Song
574bf5e758fSThomas Gleixner msi_for_each_desc(desc, dev, MSI_DESC_ALL)
575bf5e758fSThomas Gleixner msi_sysfs_remove_desc(dev, desc);
5762f170814SBarry Song }
577335b4223SMaximilian Heyne #endif /* CONFIG_PCI_MSI_ARCH_FALLBACK || CONFIG_PCI_XEN */
578bf5e758fSThomas Gleixner #else /* CONFIG_SYSFS */
msi_sysfs_create_group(struct device * dev)579bf5e758fSThomas Gleixner static inline int msi_sysfs_create_group(struct device *dev) { return 0; }
msi_sysfs_populate_desc(struct device * dev,struct msi_desc * desc)580bf5e758fSThomas Gleixner static inline int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) { return 0; }
msi_sysfs_remove_desc(struct device * dev,struct msi_desc * desc)581bf5e758fSThomas Gleixner static inline void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) { }
582bf5e758fSThomas Gleixner #endif /* !CONFIG_SYSFS */
5832f170814SBarry Song
msi_get_device_domain(struct device * dev,unsigned int domid)5844cd5f440SThomas Gleixner static struct irq_domain *msi_get_device_domain(struct device *dev, unsigned int domid)
5854cd5f440SThomas Gleixner {
5864cd5f440SThomas Gleixner struct irq_domain *domain;
5874cd5f440SThomas Gleixner
5884cd5f440SThomas Gleixner lockdep_assert_held(&dev->msi.data->mutex);
5894cd5f440SThomas Gleixner
5904cd5f440SThomas Gleixner if (WARN_ON_ONCE(domid >= MSI_MAX_DEVICE_IRQDOMAINS))
5914cd5f440SThomas Gleixner return NULL;
5924cd5f440SThomas Gleixner
5934cd5f440SThomas Gleixner domain = dev->msi.data->__domains[domid].domain;
5944cd5f440SThomas Gleixner if (!domain)
5954cd5f440SThomas Gleixner return NULL;
5964cd5f440SThomas Gleixner
5974cd5f440SThomas Gleixner if (WARN_ON_ONCE(irq_domain_is_msi_parent(domain)))
5984cd5f440SThomas Gleixner return NULL;
5994cd5f440SThomas Gleixner
6004cd5f440SThomas Gleixner return domain;
6014cd5f440SThomas Gleixner }
602762687ceSThomas Gleixner
msi_domain_get_hwsize(struct device * dev,unsigned int domid)60336db3d90SThomas Gleixner static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid)
60436db3d90SThomas Gleixner {
60536db3d90SThomas Gleixner struct msi_domain_info *info;
60636db3d90SThomas Gleixner struct irq_domain *domain;
60736db3d90SThomas Gleixner
60836db3d90SThomas Gleixner domain = msi_get_device_domain(dev, domid);
60936db3d90SThomas Gleixner if (domain) {
61036db3d90SThomas Gleixner info = domain->host_data;
61136db3d90SThomas Gleixner return info->hwsize;
61236db3d90SThomas Gleixner }
613e982ad82SThomas Gleixner /* No domain, default to MSI_XA_DOMAIN_SIZE */
614e982ad82SThomas Gleixner return MSI_XA_DOMAIN_SIZE;
61536db3d90SThomas Gleixner }
61636db3d90SThomas Gleixner
irq_chip_write_msi_msg(struct irq_data * data,struct msi_msg * msg)61774faaf7aSThomas Gleixner static inline void irq_chip_write_msi_msg(struct irq_data *data,
61874faaf7aSThomas Gleixner struct msi_msg *msg)
61974faaf7aSThomas Gleixner {
62074faaf7aSThomas Gleixner data->chip->irq_write_msi_msg(data, msg);
62174faaf7aSThomas Gleixner }
62274faaf7aSThomas Gleixner
msi_check_level(struct irq_domain * domain,struct msi_msg * msg)6230be8153cSMarc Zyngier static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
6240be8153cSMarc Zyngier {
6250be8153cSMarc Zyngier struct msi_domain_info *info = domain->host_data;
6260be8153cSMarc Zyngier
6270be8153cSMarc Zyngier /*
6280be8153cSMarc Zyngier * If the MSI provider has messed with the second message and
6290be8153cSMarc Zyngier * not advertized that it is level-capable, signal the breakage.
6300be8153cSMarc Zyngier */
6310be8153cSMarc Zyngier WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
6320be8153cSMarc Zyngier (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
6330be8153cSMarc Zyngier (msg[1].address_lo || msg[1].address_hi || msg[1].data));
6340be8153cSMarc Zyngier }
6350be8153cSMarc Zyngier
636f3cf8bb0SJiang Liu /**
637f3cf8bb0SJiang Liu * msi_domain_set_affinity - Generic affinity setter function for MSI domains
638f3cf8bb0SJiang Liu * @irq_data: The irq data associated to the interrupt
639f3cf8bb0SJiang Liu * @mask: The affinity mask to set
640f3cf8bb0SJiang Liu * @force: Flag to enforce setting (disable online checks)
641f3cf8bb0SJiang Liu *
642f3cf8bb0SJiang Liu * Intended to be used by MSI interrupt controllers which are
643f3cf8bb0SJiang Liu * implemented with hierarchical domains.
6443b35e7e6SRandy Dunlap *
6453b35e7e6SRandy Dunlap * Return: IRQ_SET_MASK_* result code
646f3cf8bb0SJiang Liu */
msi_domain_set_affinity(struct irq_data * irq_data,const struct cpumask * mask,bool force)647f3cf8bb0SJiang Liu int msi_domain_set_affinity(struct irq_data *irq_data,
648f3cf8bb0SJiang Liu const struct cpumask *mask, bool force)
649f3cf8bb0SJiang Liu {
650f3cf8bb0SJiang Liu struct irq_data *parent = irq_data->parent_data;
6510be8153cSMarc Zyngier struct msi_msg msg[2] = { [1] = { }, };
652f3cf8bb0SJiang Liu int ret;
653f3cf8bb0SJiang Liu
654f3cf8bb0SJiang Liu ret = parent->chip->irq_set_affinity(parent, mask, force);
655f3cf8bb0SJiang Liu if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
6560be8153cSMarc Zyngier BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
6570be8153cSMarc Zyngier msi_check_level(irq_data->domain, msg);
6580be8153cSMarc Zyngier irq_chip_write_msi_msg(irq_data, msg);
659f3cf8bb0SJiang Liu }
660f3cf8bb0SJiang Liu
661f3cf8bb0SJiang Liu return ret;
662f3cf8bb0SJiang Liu }
663f3cf8bb0SJiang Liu
msi_domain_activate(struct irq_domain * domain,struct irq_data * irq_data,bool early)66472491643SThomas Gleixner static int msi_domain_activate(struct irq_domain *domain,
66572491643SThomas Gleixner struct irq_data *irq_data, bool early)
666f3cf8bb0SJiang Liu {
6670be8153cSMarc Zyngier struct msi_msg msg[2] = { [1] = { }, };
668f3cf8bb0SJiang Liu
6690be8153cSMarc Zyngier BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
6700be8153cSMarc Zyngier msi_check_level(irq_data->domain, msg);
6710be8153cSMarc Zyngier irq_chip_write_msi_msg(irq_data, msg);
67272491643SThomas Gleixner return 0;
673f3cf8bb0SJiang Liu }
674f3cf8bb0SJiang Liu
msi_domain_deactivate(struct irq_domain * domain,struct irq_data * irq_data)675f3cf8bb0SJiang Liu static void msi_domain_deactivate(struct irq_domain *domain,
676f3cf8bb0SJiang Liu struct irq_data *irq_data)
677f3cf8bb0SJiang Liu {
6780be8153cSMarc Zyngier struct msi_msg msg[2];
679f3cf8bb0SJiang Liu
6800be8153cSMarc Zyngier memset(msg, 0, sizeof(msg));
6810be8153cSMarc Zyngier irq_chip_write_msi_msg(irq_data, msg);
682f3cf8bb0SJiang Liu }
683f3cf8bb0SJiang Liu
msi_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)684f3cf8bb0SJiang Liu static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
685f3cf8bb0SJiang Liu unsigned int nr_irqs, void *arg)
686f3cf8bb0SJiang Liu {
687f3cf8bb0SJiang Liu struct msi_domain_info *info = domain->host_data;
688f3cf8bb0SJiang Liu struct msi_domain_ops *ops = info->ops;
689f3cf8bb0SJiang Liu irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
690f3cf8bb0SJiang Liu int i, ret;
691f3cf8bb0SJiang Liu
692f3cf8bb0SJiang Liu if (irq_find_mapping(domain, hwirq) > 0)
693f3cf8bb0SJiang Liu return -EEXIST;
694f3cf8bb0SJiang Liu
695bf6f869fSLiu Jiang if (domain->parent) {
696f3cf8bb0SJiang Liu ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
697f3cf8bb0SJiang Liu if (ret < 0)
698f3cf8bb0SJiang Liu return ret;
699bf6f869fSLiu Jiang }
700f3cf8bb0SJiang Liu
701f3cf8bb0SJiang Liu for (i = 0; i < nr_irqs; i++) {
702f3cf8bb0SJiang Liu ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
703f3cf8bb0SJiang Liu if (ret < 0) {
704f3cf8bb0SJiang Liu if (ops->msi_free) {
705f3cf8bb0SJiang Liu for (i--; i > 0; i--)
706f3cf8bb0SJiang Liu ops->msi_free(domain, info, virq + i);
707f3cf8bb0SJiang Liu }
708f3cf8bb0SJiang Liu irq_domain_free_irqs_top(domain, virq, nr_irqs);
709f3cf8bb0SJiang Liu return ret;
710f3cf8bb0SJiang Liu }
711f3cf8bb0SJiang Liu }
712f3cf8bb0SJiang Liu
713f3cf8bb0SJiang Liu return 0;
714f3cf8bb0SJiang Liu }
715f3cf8bb0SJiang Liu
msi_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)716f3cf8bb0SJiang Liu static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
717f3cf8bb0SJiang Liu unsigned int nr_irqs)
718f3cf8bb0SJiang Liu {
719f3cf8bb0SJiang Liu struct msi_domain_info *info = domain->host_data;
720f3cf8bb0SJiang Liu int i;
721f3cf8bb0SJiang Liu
722f3cf8bb0SJiang Liu if (info->ops->msi_free) {
723f3cf8bb0SJiang Liu for (i = 0; i < nr_irqs; i++)
724f3cf8bb0SJiang Liu info->ops->msi_free(domain, info, virq + i);
725f3cf8bb0SJiang Liu }
726f3cf8bb0SJiang Liu irq_domain_free_irqs_top(domain, virq, nr_irqs);
727f3cf8bb0SJiang Liu }
728f3cf8bb0SJiang Liu
72901364028SKrzysztof Kozlowski static const struct irq_domain_ops msi_domain_ops = {
730f3cf8bb0SJiang Liu .alloc = msi_domain_alloc,
731f3cf8bb0SJiang Liu .free = msi_domain_free,
732f3cf8bb0SJiang Liu .activate = msi_domain_activate,
733f3cf8bb0SJiang Liu .deactivate = msi_domain_deactivate,
734f3cf8bb0SJiang Liu };
735f3cf8bb0SJiang Liu
msi_domain_ops_get_hwirq(struct msi_domain_info * info,msi_alloc_info_t * arg)736aeeb5965SJiang Liu static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
737aeeb5965SJiang Liu msi_alloc_info_t *arg)
738aeeb5965SJiang Liu {
739aeeb5965SJiang Liu return arg->hwirq;
740aeeb5965SJiang Liu }
741aeeb5965SJiang Liu
msi_domain_ops_prepare(struct irq_domain * domain,struct device * dev,int nvec,msi_alloc_info_t * arg)742aeeb5965SJiang Liu static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
743aeeb5965SJiang Liu int nvec, msi_alloc_info_t *arg)
744aeeb5965SJiang Liu {
745aeeb5965SJiang Liu memset(arg, 0, sizeof(*arg));
746aeeb5965SJiang Liu return 0;
747aeeb5965SJiang Liu }
748aeeb5965SJiang Liu
msi_domain_ops_set_desc(msi_alloc_info_t * arg,struct msi_desc * desc)749aeeb5965SJiang Liu static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
750aeeb5965SJiang Liu struct msi_desc *desc)
751aeeb5965SJiang Liu {
752aeeb5965SJiang Liu arg->desc = desc;
753aeeb5965SJiang Liu }
754aeeb5965SJiang Liu
msi_domain_ops_init(struct irq_domain * domain,struct msi_domain_info * info,unsigned int virq,irq_hw_number_t hwirq,msi_alloc_info_t * arg)755aeeb5965SJiang Liu static int msi_domain_ops_init(struct irq_domain *domain,
756aeeb5965SJiang Liu struct msi_domain_info *info,
757aeeb5965SJiang Liu unsigned int virq, irq_hw_number_t hwirq,
758aeeb5965SJiang Liu msi_alloc_info_t *arg)
759aeeb5965SJiang Liu {
760aeeb5965SJiang Liu irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
761aeeb5965SJiang Liu info->chip_data);
762aeeb5965SJiang Liu if (info->handler && info->handler_name) {
763aeeb5965SJiang Liu __irq_set_handler(virq, info->handler, 0, info->handler_name);
764aeeb5965SJiang Liu if (info->handler_data)
765aeeb5965SJiang Liu irq_set_handler_data(virq, info->handler_data);
766aeeb5965SJiang Liu }
767aeeb5965SJiang Liu return 0;
768aeeb5965SJiang Liu }
769aeeb5965SJiang Liu
770aeeb5965SJiang Liu static struct msi_domain_ops msi_domain_ops_default = {
771aeeb5965SJiang Liu .get_hwirq = msi_domain_ops_get_hwirq,
772aeeb5965SJiang Liu .msi_init = msi_domain_ops_init,
773aeeb5965SJiang Liu .msi_prepare = msi_domain_ops_prepare,
774aeeb5965SJiang Liu .set_desc = msi_domain_ops_set_desc,
775aeeb5965SJiang Liu };
776aeeb5965SJiang Liu
msi_domain_update_dom_ops(struct msi_domain_info * info)777aeeb5965SJiang Liu static void msi_domain_update_dom_ops(struct msi_domain_info *info)
778aeeb5965SJiang Liu {
779aeeb5965SJiang Liu struct msi_domain_ops *ops = info->ops;
780aeeb5965SJiang Liu
781aeeb5965SJiang Liu if (ops == NULL) {
782aeeb5965SJiang Liu info->ops = &msi_domain_ops_default;
783aeeb5965SJiang Liu return;
784aeeb5965SJiang Liu }
785aeeb5965SJiang Liu
78643e9e705SThomas Gleixner if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
78743e9e705SThomas Gleixner return;
78843e9e705SThomas Gleixner
789aeeb5965SJiang Liu if (ops->get_hwirq == NULL)
790aeeb5965SJiang Liu ops->get_hwirq = msi_domain_ops_default.get_hwirq;
791aeeb5965SJiang Liu if (ops->msi_init == NULL)
792aeeb5965SJiang Liu ops->msi_init = msi_domain_ops_default.msi_init;
793aeeb5965SJiang Liu if (ops->msi_prepare == NULL)
794aeeb5965SJiang Liu ops->msi_prepare = msi_domain_ops_default.msi_prepare;
795aeeb5965SJiang Liu if (ops->set_desc == NULL)
796aeeb5965SJiang Liu ops->set_desc = msi_domain_ops_default.set_desc;
797aeeb5965SJiang Liu }
798aeeb5965SJiang Liu
msi_domain_update_chip_ops(struct msi_domain_info * info)799aeeb5965SJiang Liu static void msi_domain_update_chip_ops(struct msi_domain_info *info)
800aeeb5965SJiang Liu {
801aeeb5965SJiang Liu struct irq_chip *chip = info->chip;
802aeeb5965SJiang Liu
8030701c53eSMarc Zyngier BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
804aeeb5965SJiang Liu if (!chip->irq_set_affinity)
805aeeb5965SJiang Liu chip->irq_set_affinity = msi_domain_set_affinity;
806aeeb5965SJiang Liu }
807aeeb5965SJiang Liu
__msi_create_irq_domain(struct fwnode_handle * fwnode,struct msi_domain_info * info,unsigned int flags,struct irq_domain * parent)808a80c0aceSThomas Gleixner static struct irq_domain *__msi_create_irq_domain(struct fwnode_handle *fwnode,
809f3cf8bb0SJiang Liu struct msi_domain_info *info,
810a80c0aceSThomas Gleixner unsigned int flags,
811f3cf8bb0SJiang Liu struct irq_domain *parent)
812f3cf8bb0SJiang Liu {
813a97b852bSMarc Zyngier struct irq_domain *domain;
814a97b852bSMarc Zyngier
81561bf992fSThomas Gleixner if (info->hwsize > MSI_XA_DOMAIN_SIZE)
81661bf992fSThomas Gleixner return NULL;
81761bf992fSThomas Gleixner
81861bf992fSThomas Gleixner /*
81961bf992fSThomas Gleixner * Hardware size 0 is valid for backwards compatibility and for
82061bf992fSThomas Gleixner * domains which are not backed by a hardware table. Grant the
82161bf992fSThomas Gleixner * maximum index space.
82261bf992fSThomas Gleixner */
82361bf992fSThomas Gleixner if (!info->hwsize)
82461bf992fSThomas Gleixner info->hwsize = MSI_XA_DOMAIN_SIZE;
82561bf992fSThomas Gleixner
826aeeb5965SJiang Liu msi_domain_update_dom_ops(info);
827aeeb5965SJiang Liu if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
828aeeb5965SJiang Liu msi_domain_update_chip_ops(info);
829f3cf8bb0SJiang Liu
830a80c0aceSThomas Gleixner domain = irq_domain_create_hierarchy(parent, flags | IRQ_DOMAIN_FLAG_MSI, 0,
83188156f00SEric Auger fwnode, &msi_domain_ops, info);
8320165308aSThomas Gleixner
833ea9a78c3SJohan Hovold if (domain)
83422db089aSAhmed S. Darwish irq_domain_update_bus_token(domain, info->bus_token);
835a97b852bSMarc Zyngier
836a97b852bSMarc Zyngier return domain;
837f3cf8bb0SJiang Liu }
838f3cf8bb0SJiang Liu
839b78780d9SThomas Gleixner /**
840a80c0aceSThomas Gleixner * msi_create_irq_domain - Create an MSI interrupt domain
841a80c0aceSThomas Gleixner * @fwnode: Optional fwnode of the interrupt controller
842a80c0aceSThomas Gleixner * @info: MSI domain info
843a80c0aceSThomas Gleixner * @parent: Parent irq domain
844a80c0aceSThomas Gleixner *
845a80c0aceSThomas Gleixner * Return: pointer to the created &struct irq_domain or %NULL on failure
846a80c0aceSThomas Gleixner */
msi_create_irq_domain(struct fwnode_handle * fwnode,struct msi_domain_info * info,struct irq_domain * parent)847a80c0aceSThomas Gleixner struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
848a80c0aceSThomas Gleixner struct msi_domain_info *info,
849a80c0aceSThomas Gleixner struct irq_domain *parent)
850a80c0aceSThomas Gleixner {
851a80c0aceSThomas Gleixner return __msi_create_irq_domain(fwnode, info, 0, parent);
852a80c0aceSThomas Gleixner }
853a80c0aceSThomas Gleixner
854a80c0aceSThomas Gleixner /**
855b78780d9SThomas Gleixner * msi_parent_init_dev_msi_info - Delegate initialization of device MSI info down
856b78780d9SThomas Gleixner * in the domain hierarchy
857b78780d9SThomas Gleixner * @dev: The device for which the domain should be created
858b78780d9SThomas Gleixner * @domain: The domain in the hierarchy this op is being called on
859b78780d9SThomas Gleixner * @msi_parent_domain: The IRQ_DOMAIN_FLAG_MSI_PARENT domain for the child to
860b78780d9SThomas Gleixner * be created
861b78780d9SThomas Gleixner * @msi_child_info: The MSI domain info of the IRQ_DOMAIN_FLAG_MSI_DEVICE
862b78780d9SThomas Gleixner * domain to be created
863b78780d9SThomas Gleixner *
864b78780d9SThomas Gleixner * Return: true on success, false otherwise
865b78780d9SThomas Gleixner *
866b78780d9SThomas Gleixner * This is the most complex problem of per device MSI domains and the
867b78780d9SThomas Gleixner * underlying interrupt domain hierarchy:
868b78780d9SThomas Gleixner *
869b78780d9SThomas Gleixner * The device domain to be initialized requests the broadest feature set
870b78780d9SThomas Gleixner * possible and the underlying domain hierarchy puts restrictions on it.
871b78780d9SThomas Gleixner *
872b78780d9SThomas Gleixner * That's trivial for a simple parent->child relationship, but it gets
873b78780d9SThomas Gleixner * interesting with an intermediate domain: root->parent->child. The
874b78780d9SThomas Gleixner * intermediate 'parent' can expand the capabilities which the 'root'
875b78780d9SThomas Gleixner * domain is providing. So that creates a classic hen and egg problem:
876b78780d9SThomas Gleixner * Which entity is doing the restrictions/expansions?
877b78780d9SThomas Gleixner *
878b78780d9SThomas Gleixner * One solution is to let the root domain handle the initialization that's
879b78780d9SThomas Gleixner * why there is the @domain and the @msi_parent_domain pointer.
880b78780d9SThomas Gleixner */
msi_parent_init_dev_msi_info(struct device * dev,struct irq_domain * domain,struct irq_domain * msi_parent_domain,struct msi_domain_info * msi_child_info)881b78780d9SThomas Gleixner bool msi_parent_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
882b78780d9SThomas Gleixner struct irq_domain *msi_parent_domain,
883b78780d9SThomas Gleixner struct msi_domain_info *msi_child_info)
884b78780d9SThomas Gleixner {
885b78780d9SThomas Gleixner struct irq_domain *parent = domain->parent;
886b78780d9SThomas Gleixner
887b78780d9SThomas Gleixner if (WARN_ON_ONCE(!parent || !parent->msi_parent_ops ||
888b78780d9SThomas Gleixner !parent->msi_parent_ops->init_dev_msi_info))
889b78780d9SThomas Gleixner return false;
890b78780d9SThomas Gleixner
891b78780d9SThomas Gleixner return parent->msi_parent_ops->init_dev_msi_info(dev, parent, msi_parent_domain,
892b78780d9SThomas Gleixner msi_child_info);
893b78780d9SThomas Gleixner }
894b78780d9SThomas Gleixner
89527a6dea3SThomas Gleixner /**
89627a6dea3SThomas Gleixner * msi_create_device_irq_domain - Create a device MSI interrupt domain
89727a6dea3SThomas Gleixner * @dev: Pointer to the device
89827a6dea3SThomas Gleixner * @domid: Domain id
89927a6dea3SThomas Gleixner * @template: MSI domain info bundle used as template
90027a6dea3SThomas Gleixner * @hwsize: Maximum number of MSI table entries (0 if unknown or unlimited)
90127a6dea3SThomas Gleixner * @domain_data: Optional pointer to domain specific data which is set in
90227a6dea3SThomas Gleixner * msi_domain_info::data
90327a6dea3SThomas Gleixner * @chip_data: Optional pointer to chip specific data which is set in
90427a6dea3SThomas Gleixner * msi_domain_info::chip_data
90527a6dea3SThomas Gleixner *
90627a6dea3SThomas Gleixner * Return: True on success, false otherwise
90727a6dea3SThomas Gleixner *
90827a6dea3SThomas Gleixner * There is no firmware node required for this interface because the per
90927a6dea3SThomas Gleixner * device domains are software constructs which are actually closer to the
91027a6dea3SThomas Gleixner * hardware reality than any firmware can describe them.
91127a6dea3SThomas Gleixner *
91227a6dea3SThomas Gleixner * The domain name and the irq chip name for a MSI device domain are
91327a6dea3SThomas Gleixner * composed by: "$(PREFIX)$(CHIPNAME)-$(DEVNAME)"
91427a6dea3SThomas Gleixner *
91527a6dea3SThomas Gleixner * $PREFIX: Optional prefix provided by the underlying MSI parent domain
91627a6dea3SThomas Gleixner * via msi_parent_ops::prefix. If that pointer is NULL the prefix
91727a6dea3SThomas Gleixner * is empty.
91827a6dea3SThomas Gleixner * $CHIPNAME: The name of the irq_chip in @template
91927a6dea3SThomas Gleixner * $DEVNAME: The name of the device
92027a6dea3SThomas Gleixner *
92127a6dea3SThomas Gleixner * This results in understandable chip names and hardware interrupt numbers
92227a6dea3SThomas Gleixner * in e.g. /proc/interrupts
92327a6dea3SThomas Gleixner *
92427a6dea3SThomas Gleixner * PCI-MSI-0000:00:1c.0 0-edge Parent domain has no prefix
92527a6dea3SThomas Gleixner * IR-PCI-MSI-0000:00:1c.4 0-edge Same with interrupt remapping prefix 'IR-'
92627a6dea3SThomas Gleixner *
92727a6dea3SThomas Gleixner * IR-PCI-MSIX-0000:3d:00.0 0-edge Hardware interrupt numbers reflect
92827a6dea3SThomas Gleixner * IR-PCI-MSIX-0000:3d:00.0 1-edge the real MSI-X index on that device
92927a6dea3SThomas Gleixner * IR-PCI-MSIX-0000:3d:00.0 2-edge
93027a6dea3SThomas Gleixner *
93127a6dea3SThomas Gleixner * On IMS domains the hardware interrupt number is either a table entry
93227a6dea3SThomas Gleixner * index or a purely software managed index but it is guaranteed to be
93327a6dea3SThomas Gleixner * unique.
93427a6dea3SThomas Gleixner *
93527a6dea3SThomas Gleixner * The domain pointer is stored in @dev::msi::data::__irqdomains[]. All
93627a6dea3SThomas Gleixner * subsequent operations on the domain depend on the domain id.
93727a6dea3SThomas Gleixner *
93827a6dea3SThomas Gleixner * The domain is automatically freed when the device is removed via devres
93927a6dea3SThomas Gleixner * in the context of @dev::msi::data freeing, but it can also be
94027a6dea3SThomas Gleixner * independently removed via @msi_remove_device_irq_domain().
94127a6dea3SThomas Gleixner */
msi_create_device_irq_domain(struct device * dev,unsigned int domid,const struct msi_domain_template * template,unsigned int hwsize,void * domain_data,void * chip_data)94227a6dea3SThomas Gleixner bool msi_create_device_irq_domain(struct device *dev, unsigned int domid,
94327a6dea3SThomas Gleixner const struct msi_domain_template *template,
94427a6dea3SThomas Gleixner unsigned int hwsize, void *domain_data,
94527a6dea3SThomas Gleixner void *chip_data)
94627a6dea3SThomas Gleixner {
94727a6dea3SThomas Gleixner struct irq_domain *domain, *parent = dev->msi.domain;
94827a6dea3SThomas Gleixner const struct msi_parent_ops *pops;
94927a6dea3SThomas Gleixner struct msi_domain_template *bundle;
95027a6dea3SThomas Gleixner struct fwnode_handle *fwnode;
95127a6dea3SThomas Gleixner
95227a6dea3SThomas Gleixner if (!irq_domain_is_msi_parent(parent))
95327a6dea3SThomas Gleixner return false;
95427a6dea3SThomas Gleixner
95527a6dea3SThomas Gleixner if (domid >= MSI_MAX_DEVICE_IRQDOMAINS)
95627a6dea3SThomas Gleixner return false;
95727a6dea3SThomas Gleixner
95827a6dea3SThomas Gleixner bundle = kmemdup(template, sizeof(*bundle), GFP_KERNEL);
95927a6dea3SThomas Gleixner if (!bundle)
96027a6dea3SThomas Gleixner return false;
96127a6dea3SThomas Gleixner
96227a6dea3SThomas Gleixner bundle->info.hwsize = hwsize;
96327a6dea3SThomas Gleixner bundle->info.chip = &bundle->chip;
96427a6dea3SThomas Gleixner bundle->info.ops = &bundle->ops;
96527a6dea3SThomas Gleixner bundle->info.data = domain_data;
96627a6dea3SThomas Gleixner bundle->info.chip_data = chip_data;
96727a6dea3SThomas Gleixner
96827a6dea3SThomas Gleixner pops = parent->msi_parent_ops;
96927a6dea3SThomas Gleixner snprintf(bundle->name, sizeof(bundle->name), "%s%s-%s",
97027a6dea3SThomas Gleixner pops->prefix ? : "", bundle->chip.name, dev_name(dev));
97127a6dea3SThomas Gleixner bundle->chip.name = bundle->name;
97227a6dea3SThomas Gleixner
97327a6dea3SThomas Gleixner fwnode = irq_domain_alloc_named_fwnode(bundle->name);
97427a6dea3SThomas Gleixner if (!fwnode)
97527a6dea3SThomas Gleixner goto free_bundle;
97627a6dea3SThomas Gleixner
97727a6dea3SThomas Gleixner if (msi_setup_device_data(dev))
97827a6dea3SThomas Gleixner goto free_fwnode;
97927a6dea3SThomas Gleixner
98027a6dea3SThomas Gleixner msi_lock_descs(dev);
98127a6dea3SThomas Gleixner
98227a6dea3SThomas Gleixner if (WARN_ON_ONCE(msi_get_device_domain(dev, domid)))
98327a6dea3SThomas Gleixner goto fail;
98427a6dea3SThomas Gleixner
98527a6dea3SThomas Gleixner if (!pops->init_dev_msi_info(dev, parent, parent, &bundle->info))
98627a6dea3SThomas Gleixner goto fail;
98727a6dea3SThomas Gleixner
98827a6dea3SThomas Gleixner domain = __msi_create_irq_domain(fwnode, &bundle->info, IRQ_DOMAIN_FLAG_MSI_DEVICE, parent);
98927a6dea3SThomas Gleixner if (!domain)
99027a6dea3SThomas Gleixner goto fail;
99127a6dea3SThomas Gleixner
99227a6dea3SThomas Gleixner domain->dev = dev;
99327a6dea3SThomas Gleixner dev->msi.data->__domains[domid].domain = domain;
99427a6dea3SThomas Gleixner msi_unlock_descs(dev);
99527a6dea3SThomas Gleixner return true;
99627a6dea3SThomas Gleixner
99727a6dea3SThomas Gleixner fail:
99827a6dea3SThomas Gleixner msi_unlock_descs(dev);
99927a6dea3SThomas Gleixner free_fwnode:
1000ac8f29aeSJason Gunthorpe irq_domain_free_fwnode(fwnode);
100127a6dea3SThomas Gleixner free_bundle:
100227a6dea3SThomas Gleixner kfree(bundle);
100327a6dea3SThomas Gleixner return false;
100427a6dea3SThomas Gleixner }
100527a6dea3SThomas Gleixner
100627a6dea3SThomas Gleixner /**
100727a6dea3SThomas Gleixner * msi_remove_device_irq_domain - Free a device MSI interrupt domain
100827a6dea3SThomas Gleixner * @dev: Pointer to the device
100927a6dea3SThomas Gleixner * @domid: Domain id
101027a6dea3SThomas Gleixner */
msi_remove_device_irq_domain(struct device * dev,unsigned int domid)101127a6dea3SThomas Gleixner void msi_remove_device_irq_domain(struct device *dev, unsigned int domid)
101227a6dea3SThomas Gleixner {
1013ac8f29aeSJason Gunthorpe struct fwnode_handle *fwnode = NULL;
101427a6dea3SThomas Gleixner struct msi_domain_info *info;
101527a6dea3SThomas Gleixner struct irq_domain *domain;
101627a6dea3SThomas Gleixner
101727a6dea3SThomas Gleixner msi_lock_descs(dev);
101827a6dea3SThomas Gleixner
101927a6dea3SThomas Gleixner domain = msi_get_device_domain(dev, domid);
102027a6dea3SThomas Gleixner
102127a6dea3SThomas Gleixner if (!domain || !irq_domain_is_msi_device(domain))
102227a6dea3SThomas Gleixner goto unlock;
102327a6dea3SThomas Gleixner
102427a6dea3SThomas Gleixner dev->msi.data->__domains[domid].domain = NULL;
102527a6dea3SThomas Gleixner info = domain->host_data;
1026ac8f29aeSJason Gunthorpe if (irq_domain_is_msi_device(domain))
1027ac8f29aeSJason Gunthorpe fwnode = domain->fwnode;
102827a6dea3SThomas Gleixner irq_domain_remove(domain);
1029ac8f29aeSJason Gunthorpe irq_domain_free_fwnode(fwnode);
103027a6dea3SThomas Gleixner kfree(container_of(info, struct msi_domain_template, info));
103127a6dea3SThomas Gleixner
103227a6dea3SThomas Gleixner unlock:
103327a6dea3SThomas Gleixner msi_unlock_descs(dev);
103427a6dea3SThomas Gleixner }
103527a6dea3SThomas Gleixner
103626e91b75SThomas Gleixner /**
103726e91b75SThomas Gleixner * msi_match_device_irq_domain - Match a device irq domain against a bus token
103826e91b75SThomas Gleixner * @dev: Pointer to the device
103926e91b75SThomas Gleixner * @domid: Domain id
104026e91b75SThomas Gleixner * @bus_token: Bus token to match against the domain bus token
104126e91b75SThomas Gleixner *
104226e91b75SThomas Gleixner * Return: True if device domain exists and bus tokens match.
104326e91b75SThomas Gleixner */
msi_match_device_irq_domain(struct device * dev,unsigned int domid,enum irq_domain_bus_token bus_token)104426e91b75SThomas Gleixner bool msi_match_device_irq_domain(struct device *dev, unsigned int domid,
104526e91b75SThomas Gleixner enum irq_domain_bus_token bus_token)
104626e91b75SThomas Gleixner {
104726e91b75SThomas Gleixner struct msi_domain_info *info;
104826e91b75SThomas Gleixner struct irq_domain *domain;
104926e91b75SThomas Gleixner bool ret = false;
105026e91b75SThomas Gleixner
105126e91b75SThomas Gleixner msi_lock_descs(dev);
105226e91b75SThomas Gleixner domain = msi_get_device_domain(dev, domid);
105326e91b75SThomas Gleixner if (domain && irq_domain_is_msi_device(domain)) {
105426e91b75SThomas Gleixner info = domain->host_data;
105526e91b75SThomas Gleixner ret = info->bus_token == bus_token;
105626e91b75SThomas Gleixner }
105726e91b75SThomas Gleixner msi_unlock_descs(dev);
105826e91b75SThomas Gleixner return ret;
105926e91b75SThomas Gleixner }
106026e91b75SThomas Gleixner
msi_domain_prepare_irqs(struct irq_domain * domain,struct device * dev,int nvec,msi_alloc_info_t * arg)1061b2eba39bSMarc Zyngier int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
1062b2eba39bSMarc Zyngier int nvec, msi_alloc_info_t *arg)
1063b2eba39bSMarc Zyngier {
1064b2eba39bSMarc Zyngier struct msi_domain_info *info = domain->host_data;
1065b2eba39bSMarc Zyngier struct msi_domain_ops *ops = info->ops;
1066b2eba39bSMarc Zyngier
10672569f62cSThomas Gleixner return ops->msi_prepare(domain, dev, nvec, arg);
1068b2eba39bSMarc Zyngier }
1069b2eba39bSMarc Zyngier
msi_domain_populate_irqs(struct irq_domain * domain,struct device * dev,int virq_base,int nvec,msi_alloc_info_t * arg)10702145ac93SMarc Zyngier int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
1071a80713feSThomas Gleixner int virq_base, int nvec, msi_alloc_info_t *arg)
10722145ac93SMarc Zyngier {
10732145ac93SMarc Zyngier struct msi_domain_info *info = domain->host_data;
10742145ac93SMarc Zyngier struct msi_domain_ops *ops = info->ops;
107540742716SThomas Gleixner struct msi_ctrl ctrl = {
107640742716SThomas Gleixner .domid = MSI_DEFAULT_DOMAIN,
107740742716SThomas Gleixner .first = virq_base,
107840742716SThomas Gleixner .last = virq_base + nvec - 1,
107940742716SThomas Gleixner };
10802145ac93SMarc Zyngier struct msi_desc *desc;
1081f1139f90SThomas Gleixner struct xarray *xa;
1082a80713feSThomas Gleixner int ret, virq;
10832145ac93SMarc Zyngier
1084a80713feSThomas Gleixner msi_lock_descs(dev);
10850af2795fSMarc Zyngier
10860af2795fSMarc Zyngier if (!msi_ctrl_valid(dev, &ctrl)) {
10870af2795fSMarc Zyngier ret = -EINVAL;
10880af2795fSMarc Zyngier goto unlock;
10890af2795fSMarc Zyngier }
10900af2795fSMarc Zyngier
109140742716SThomas Gleixner ret = msi_domain_add_simple_msi_descs(dev, &ctrl);
1092cd6cf065SThomas Gleixner if (ret)
1093cd6cf065SThomas Gleixner goto unlock;
10942145ac93SMarc Zyngier
109540742716SThomas Gleixner xa = &dev->msi.data->__domains[ctrl.domid].store;
1096f1139f90SThomas Gleixner
1097cd6cf065SThomas Gleixner for (virq = virq_base; virq < virq_base + nvec; virq++) {
1098f1139f90SThomas Gleixner desc = xa_load(xa, virq);
1099a80713feSThomas Gleixner desc->irq = virq;
11002145ac93SMarc Zyngier
11012145ac93SMarc Zyngier ops->set_desc(arg, desc);
1102a80713feSThomas Gleixner ret = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
11032145ac93SMarc Zyngier if (ret)
1104a80713feSThomas Gleixner goto fail;
11052145ac93SMarc Zyngier
1106a80713feSThomas Gleixner irq_set_msi_desc(virq, desc);
11072145ac93SMarc Zyngier }
1108a80713feSThomas Gleixner msi_unlock_descs(dev);
1109a80713feSThomas Gleixner return 0;
11102145ac93SMarc Zyngier
1111a80713feSThomas Gleixner fail:
11120fb7fb71SThomas Gleixner for (--virq; virq >= virq_base; virq--) {
11130fb7fb71SThomas Gleixner msi_domain_depopulate_descs(dev, virq, 1);
1114a80713feSThomas Gleixner irq_domain_free_irqs_common(domain, virq, 1);
11150fb7fb71SThomas Gleixner }
111640742716SThomas Gleixner msi_domain_free_descs(dev, &ctrl);
1117cd6cf065SThomas Gleixner unlock:
1118a80713feSThomas Gleixner msi_unlock_descs(dev);
11192145ac93SMarc Zyngier return ret;
11202145ac93SMarc Zyngier }
11212145ac93SMarc Zyngier
msi_domain_depopulate_descs(struct device * dev,int virq_base,int nvec)11220fb7fb71SThomas Gleixner void msi_domain_depopulate_descs(struct device *dev, int virq_base, int nvec)
11230fb7fb71SThomas Gleixner {
11240fb7fb71SThomas Gleixner struct msi_ctrl ctrl = {
11250fb7fb71SThomas Gleixner .domid = MSI_DEFAULT_DOMAIN,
11260fb7fb71SThomas Gleixner .first = virq_base,
11270fb7fb71SThomas Gleixner .last = virq_base + nvec - 1,
11280fb7fb71SThomas Gleixner };
11290fb7fb71SThomas Gleixner struct msi_desc *desc;
11300fb7fb71SThomas Gleixner struct xarray *xa;
11310fb7fb71SThomas Gleixner unsigned long idx;
11320fb7fb71SThomas Gleixner
11330fb7fb71SThomas Gleixner if (!msi_ctrl_valid(dev, &ctrl))
11340fb7fb71SThomas Gleixner return;
11350fb7fb71SThomas Gleixner
11360fb7fb71SThomas Gleixner xa = &dev->msi.data->__domains[ctrl.domid].store;
11370fb7fb71SThomas Gleixner xa_for_each_range(xa, idx, desc, ctrl.first, ctrl.last)
11380fb7fb71SThomas Gleixner desc->irq = 0;
11390fb7fb71SThomas Gleixner }
11400fb7fb71SThomas Gleixner
1141bc976233SThomas Gleixner /*
1142bc976233SThomas Gleixner * Carefully check whether the device can use reservation mode. If
1143bc976233SThomas Gleixner * reservation mode is enabled then the early activation will assign a
1144bc976233SThomas Gleixner * dummy vector to the device. If the PCI/MSI device does not support
1145bc976233SThomas Gleixner * masking of the entry then this can result in spurious interrupts when
1146bc976233SThomas Gleixner * the device driver is not absolutely careful. But even then a malfunction
1147bc976233SThomas Gleixner * of the hardware could result in a spurious interrupt on the dummy vector
1148bc976233SThomas Gleixner * and render the device unusable. If the entry can be masked then the core
1149bc976233SThomas Gleixner * logic will prevent the spurious interrupt and reservation mode can be
1150bc976233SThomas Gleixner * used. For now reservation mode is restricted to PCI/MSI.
1151bc976233SThomas Gleixner */
msi_check_reservation_mode(struct irq_domain * domain,struct msi_domain_info * info,struct device * dev)1152bc976233SThomas Gleixner static bool msi_check_reservation_mode(struct irq_domain *domain,
1153bc976233SThomas Gleixner struct msi_domain_info *info,
1154bc976233SThomas Gleixner struct device *dev)
1155da5dd9e8SThomas Gleixner {
1156bc976233SThomas Gleixner struct msi_desc *desc;
1157bc976233SThomas Gleixner
1158c6c9e283SThomas Gleixner switch(domain->bus_token) {
1159c6c9e283SThomas Gleixner case DOMAIN_BUS_PCI_MSI:
1160bd141a3dSThomas Gleixner case DOMAIN_BUS_PCI_DEVICE_MSI:
1161bd141a3dSThomas Gleixner case DOMAIN_BUS_PCI_DEVICE_MSIX:
1162c6c9e283SThomas Gleixner case DOMAIN_BUS_VMD_MSI:
1163c6c9e283SThomas Gleixner break;
1164c6c9e283SThomas Gleixner default:
1165bc976233SThomas Gleixner return false;
1166c6c9e283SThomas Gleixner }
1167bc976233SThomas Gleixner
1168da5dd9e8SThomas Gleixner if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
1169da5dd9e8SThomas Gleixner return false;
1170bc976233SThomas Gleixner
1171bc976233SThomas Gleixner if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
1172bc976233SThomas Gleixner return false;
1173bc976233SThomas Gleixner
1174bc976233SThomas Gleixner /*
1175bc976233SThomas Gleixner * Checking the first MSI descriptor is sufficient. MSIX supports
11769c8e9c96SThomas Gleixner * masking and MSI does so when the can_mask attribute is set.
1177bc976233SThomas Gleixner */
1178495c66acSThomas Gleixner desc = msi_first_desc(dev, MSI_DESC_ALL);
1179e58f2259SThomas Gleixner return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask;
1180da5dd9e8SThomas Gleixner }
1181da5dd9e8SThomas Gleixner
msi_handle_pci_fail(struct irq_domain * domain,struct msi_desc * desc,int allocated)118289033762SThomas Gleixner static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc,
118389033762SThomas Gleixner int allocated)
118489033762SThomas Gleixner {
118589033762SThomas Gleixner switch(domain->bus_token) {
118689033762SThomas Gleixner case DOMAIN_BUS_PCI_MSI:
1187bd141a3dSThomas Gleixner case DOMAIN_BUS_PCI_DEVICE_MSI:
1188bd141a3dSThomas Gleixner case DOMAIN_BUS_PCI_DEVICE_MSIX:
118989033762SThomas Gleixner case DOMAIN_BUS_VMD_MSI:
119089033762SThomas Gleixner if (IS_ENABLED(CONFIG_PCI_MSI))
119189033762SThomas Gleixner break;
119289033762SThomas Gleixner fallthrough;
119389033762SThomas Gleixner default:
119489033762SThomas Gleixner return -ENOSPC;
119589033762SThomas Gleixner }
119689033762SThomas Gleixner
119789033762SThomas Gleixner /* Let a failed PCI multi MSI allocation retry */
119889033762SThomas Gleixner if (desc->nvec_used > 1)
119989033762SThomas Gleixner return 1;
120089033762SThomas Gleixner
120189033762SThomas Gleixner /* If there was a successful allocation let the caller know */
120289033762SThomas Gleixner return allocated ? allocated : -ENOSPC;
120389033762SThomas Gleixner }
120489033762SThomas Gleixner
1205ef8dd015SThomas Gleixner #define VIRQ_CAN_RESERVE 0x01
1206ef8dd015SThomas Gleixner #define VIRQ_ACTIVATE 0x02
1207ef8dd015SThomas Gleixner
msi_init_virq(struct irq_domain * domain,int virq,unsigned int vflags)1208ef8dd015SThomas Gleixner static int msi_init_virq(struct irq_domain *domain, int virq, unsigned int vflags)
1209ef8dd015SThomas Gleixner {
1210ef8dd015SThomas Gleixner struct irq_data *irqd = irq_domain_get_irq_data(domain, virq);
1211ef8dd015SThomas Gleixner int ret;
1212ef8dd015SThomas Gleixner
1213ef8dd015SThomas Gleixner if (!(vflags & VIRQ_CAN_RESERVE)) {
1214ef8dd015SThomas Gleixner irqd_clr_can_reserve(irqd);
1215d802057cSMarc Zyngier
1216d802057cSMarc Zyngier /*
1217d802057cSMarc Zyngier * If the interrupt is managed but no CPU is available to
1218d802057cSMarc Zyngier * service it, shut it down until better times. Note that
1219d802057cSMarc Zyngier * we only do this on the !RESERVE path as x86 (the only
1220d802057cSMarc Zyngier * architecture using this flag) deals with this in a
1221d802057cSMarc Zyngier * different way by using a catch-all vector.
1222d802057cSMarc Zyngier */
1223d802057cSMarc Zyngier if ((vflags & VIRQ_ACTIVATE) &&
1224d802057cSMarc Zyngier irqd_affinity_is_managed(irqd) &&
1225d802057cSMarc Zyngier !cpumask_intersects(irq_data_get_affinity_mask(irqd),
1226d802057cSMarc Zyngier cpu_online_mask)) {
1227d802057cSMarc Zyngier irqd_set_managed_shutdown(irqd);
1228d802057cSMarc Zyngier return 0;
1229d802057cSMarc Zyngier }
1230ef8dd015SThomas Gleixner }
1231ef8dd015SThomas Gleixner
1232ef8dd015SThomas Gleixner if (!(vflags & VIRQ_ACTIVATE))
1233ef8dd015SThomas Gleixner return 0;
1234ef8dd015SThomas Gleixner
1235ef8dd015SThomas Gleixner ret = irq_domain_activate_irq(irqd, vflags & VIRQ_CAN_RESERVE);
1236ef8dd015SThomas Gleixner if (ret)
1237ef8dd015SThomas Gleixner return ret;
1238ef8dd015SThomas Gleixner /*
1239ef8dd015SThomas Gleixner * If the interrupt uses reservation mode, clear the activated bit
1240ef8dd015SThomas Gleixner * so request_irq() will assign the final vector.
1241ef8dd015SThomas Gleixner */
1242ef8dd015SThomas Gleixner if (vflags & VIRQ_CAN_RESERVE)
1243ef8dd015SThomas Gleixner irqd_clr_activated(irqd);
1244ef8dd015SThomas Gleixner return 0;
1245ef8dd015SThomas Gleixner }
1246ef8dd015SThomas Gleixner
__msi_domain_alloc_irqs(struct device * dev,struct irq_domain * domain,struct msi_ctrl * ctrl)1247f2480e7dSThomas Gleixner static int __msi_domain_alloc_irqs(struct device *dev, struct irq_domain *domain,
1248f2480e7dSThomas Gleixner struct msi_ctrl *ctrl)
1249d9109698SJiang Liu {
1250f2480e7dSThomas Gleixner struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store;
1251d9109698SJiang Liu struct msi_domain_info *info = domain->host_data;
1252d9109698SJiang Liu struct msi_domain_ops *ops = info->ops;
1253f2480e7dSThomas Gleixner unsigned int vflags = 0, allocated = 0;
125406fde695SZenghui Yu msi_alloc_info_t arg = { };
1255ef8dd015SThomas Gleixner struct msi_desc *desc;
1256f2480e7dSThomas Gleixner unsigned long idx;
1257b6140914SThomas Gleixner int i, ret, virq;
1258d9109698SJiang Liu
1259f2480e7dSThomas Gleixner ret = msi_domain_prepare_irqs(domain, dev, ctrl->nirqs, &arg);
1260d9109698SJiang Liu if (ret)
1261d9109698SJiang Liu return ret;
1262d9109698SJiang Liu
1263ef8dd015SThomas Gleixner /*
1264ef8dd015SThomas Gleixner * This flag is set by the PCI layer as we need to activate
1265ef8dd015SThomas Gleixner * the MSI entries before the PCI layer enables MSI in the
1266ef8dd015SThomas Gleixner * card. Otherwise the card latches a random msi message.
1267ef8dd015SThomas Gleixner */
1268ef8dd015SThomas Gleixner if (info->flags & MSI_FLAG_ACTIVATE_EARLY)
1269ef8dd015SThomas Gleixner vflags |= VIRQ_ACTIVATE;
1270ef8dd015SThomas Gleixner
1271ef8dd015SThomas Gleixner /*
1272ef8dd015SThomas Gleixner * Interrupt can use a reserved vector and will not occupy
1273ef8dd015SThomas Gleixner * a real device vector until the interrupt is requested.
1274ef8dd015SThomas Gleixner */
1275*119f7373SKoichiro Den if (msi_check_reservation_mode(domain, info, dev))
1276ef8dd015SThomas Gleixner vflags |= VIRQ_CAN_RESERVE;
1277ef8dd015SThomas Gleixner
1278f2480e7dSThomas Gleixner xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
1279f2480e7dSThomas Gleixner if (!msi_desc_match(desc, MSI_DESC_NOTASSOCIATED))
1280f2480e7dSThomas Gleixner continue;
1281f2480e7dSThomas Gleixner
1282f2480e7dSThomas Gleixner /* This should return -ECONFUSED... */
1283f2480e7dSThomas Gleixner if (WARN_ON_ONCE(allocated >= ctrl->nirqs))
1284f2480e7dSThomas Gleixner return -EINVAL;
1285f2480e7dSThomas Gleixner
12868f986fd7SThomas Gleixner if (ops->prepare_desc)
12878f986fd7SThomas Gleixner ops->prepare_desc(domain, &arg, desc);
12888f986fd7SThomas Gleixner
1289d9109698SJiang Liu ops->set_desc(&arg, desc);
1290d9109698SJiang Liu
1291b6140914SThomas Gleixner virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
129206ee6d57SThomas Gleixner dev_to_node(dev), &arg, false,
12930972fa57SThomas Gleixner desc->affinity);
12940f62d941SThomas Gleixner if (virq < 0)
12950f62d941SThomas Gleixner return msi_handle_pci_fail(domain, desc, allocated);
1296d9109698SJiang Liu
129707557ccbSThomas Gleixner for (i = 0; i < desc->nvec_used; i++) {
1298d9109698SJiang Liu irq_set_msi_desc_off(virq, i, desc);
129907557ccbSThomas Gleixner irq_debugfs_copy_devname(virq + i, dev);
1300ef8dd015SThomas Gleixner ret = msi_init_virq(domain, virq + i, vflags);
1301bb9b428aSThomas Gleixner if (ret)
13020f62d941SThomas Gleixner return ret;
130374a5257aSThomas Gleixner }
1304bf5e758fSThomas Gleixner if (info->flags & MSI_FLAG_DEV_SYSFS) {
1305bf5e758fSThomas Gleixner ret = msi_sysfs_populate_desc(dev, desc);
1306bf5e758fSThomas Gleixner if (ret)
1307bf5e758fSThomas Gleixner return ret;
1308bf5e758fSThomas Gleixner }
1309ef8dd015SThomas Gleixner allocated++;
1310d9109698SJiang Liu }
1311d9109698SJiang Liu return 0;
13120f62d941SThomas Gleixner }
13130f62d941SThomas Gleixner
msi_domain_alloc_simple_msi_descs(struct device * dev,struct msi_domain_info * info,struct msi_ctrl * ctrl)131440742716SThomas Gleixner static int msi_domain_alloc_simple_msi_descs(struct device *dev,
131540742716SThomas Gleixner struct msi_domain_info *info,
1316f2480e7dSThomas Gleixner struct msi_ctrl *ctrl)
1317645474e2SThomas Gleixner {
1318645474e2SThomas Gleixner if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS))
1319645474e2SThomas Gleixner return 0;
1320645474e2SThomas Gleixner
1321f2480e7dSThomas Gleixner return msi_domain_add_simple_msi_descs(dev, ctrl);
1322f2480e7dSThomas Gleixner }
1323f2480e7dSThomas Gleixner
__msi_domain_alloc_locked(struct device * dev,struct msi_ctrl * ctrl)1324f2480e7dSThomas Gleixner static int __msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl)
1325f2480e7dSThomas Gleixner {
1326f2480e7dSThomas Gleixner struct msi_domain_info *info;
1327f2480e7dSThomas Gleixner struct msi_domain_ops *ops;
1328f2480e7dSThomas Gleixner struct irq_domain *domain;
1329f2480e7dSThomas Gleixner int ret;
1330f2480e7dSThomas Gleixner
1331f2480e7dSThomas Gleixner if (!msi_ctrl_valid(dev, ctrl))
1332f2480e7dSThomas Gleixner return -EINVAL;
1333f2480e7dSThomas Gleixner
1334f2480e7dSThomas Gleixner domain = msi_get_device_domain(dev, ctrl->domid);
1335f2480e7dSThomas Gleixner if (!domain)
1336f2480e7dSThomas Gleixner return -ENODEV;
1337f2480e7dSThomas Gleixner
1338f2480e7dSThomas Gleixner info = domain->host_data;
1339f2480e7dSThomas Gleixner
1340f2480e7dSThomas Gleixner ret = msi_domain_alloc_simple_msi_descs(dev, info, ctrl);
1341f2480e7dSThomas Gleixner if (ret)
1342f2480e7dSThomas Gleixner return ret;
1343f2480e7dSThomas Gleixner
1344f2480e7dSThomas Gleixner ops = info->ops;
1345f2480e7dSThomas Gleixner if (ops->domain_alloc_irqs)
1346f2480e7dSThomas Gleixner return ops->domain_alloc_irqs(domain, dev, ctrl->nirqs);
1347f2480e7dSThomas Gleixner
1348f2480e7dSThomas Gleixner return __msi_domain_alloc_irqs(dev, domain, ctrl);
1349f2480e7dSThomas Gleixner }
1350f2480e7dSThomas Gleixner
msi_domain_alloc_locked(struct device * dev,struct msi_ctrl * ctrl)1351f2480e7dSThomas Gleixner static int msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl)
1352f2480e7dSThomas Gleixner {
1353f2480e7dSThomas Gleixner int ret = __msi_domain_alloc_locked(dev, ctrl);
1354f2480e7dSThomas Gleixner
1355f2480e7dSThomas Gleixner if (ret)
1356f2480e7dSThomas Gleixner msi_domain_free_locked(dev, ctrl);
1357f2480e7dSThomas Gleixner return ret;
1358f2480e7dSThomas Gleixner }
1359f2480e7dSThomas Gleixner
1360f2480e7dSThomas Gleixner /**
1361f2480e7dSThomas Gleixner * msi_domain_alloc_irqs_range_locked - Allocate interrupts from a MSI interrupt domain
1362f2480e7dSThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
1363f2480e7dSThomas Gleixner * are allocated
1364f2480e7dSThomas Gleixner * @domid: Id of the interrupt domain to operate on
1365f2480e7dSThomas Gleixner * @first: First index to allocate (inclusive)
1366f2480e7dSThomas Gleixner * @last: Last index to allocate (inclusive)
1367f2480e7dSThomas Gleixner *
1368f2480e7dSThomas Gleixner * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
1369f2480e7dSThomas Gleixner * pair. Use this for MSI irqdomains which implement their own descriptor
1370f2480e7dSThomas Gleixner * allocation/free.
1371f2480e7dSThomas Gleixner *
1372f2480e7dSThomas Gleixner * Return: %0 on success or an error code.
1373f2480e7dSThomas Gleixner */
msi_domain_alloc_irqs_range_locked(struct device * dev,unsigned int domid,unsigned int first,unsigned int last)1374f2480e7dSThomas Gleixner int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
1375f2480e7dSThomas Gleixner unsigned int first, unsigned int last)
1376f2480e7dSThomas Gleixner {
1377f2480e7dSThomas Gleixner struct msi_ctrl ctrl = {
1378f2480e7dSThomas Gleixner .domid = domid,
1379f2480e7dSThomas Gleixner .first = first,
1380f2480e7dSThomas Gleixner .last = last,
1381f2480e7dSThomas Gleixner .nirqs = last + 1 - first,
1382f2480e7dSThomas Gleixner };
1383f2480e7dSThomas Gleixner
1384f2480e7dSThomas Gleixner return msi_domain_alloc_locked(dev, &ctrl);
1385f2480e7dSThomas Gleixner }
1386f2480e7dSThomas Gleixner
1387f2480e7dSThomas Gleixner /**
1388f2480e7dSThomas Gleixner * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain
1389f2480e7dSThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
1390f2480e7dSThomas Gleixner * are allocated
1391f2480e7dSThomas Gleixner * @domid: Id of the interrupt domain to operate on
1392f2480e7dSThomas Gleixner * @first: First index to allocate (inclusive)
1393f2480e7dSThomas Gleixner * @last: Last index to allocate (inclusive)
1394f2480e7dSThomas Gleixner *
1395f2480e7dSThomas Gleixner * Return: %0 on success or an error code.
1396f2480e7dSThomas Gleixner */
msi_domain_alloc_irqs_range(struct device * dev,unsigned int domid,unsigned int first,unsigned int last)1397f2480e7dSThomas Gleixner int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
1398f2480e7dSThomas Gleixner unsigned int first, unsigned int last)
1399f2480e7dSThomas Gleixner {
1400f2480e7dSThomas Gleixner int ret;
1401f2480e7dSThomas Gleixner
1402f2480e7dSThomas Gleixner msi_lock_descs(dev);
1403f2480e7dSThomas Gleixner ret = msi_domain_alloc_irqs_range_locked(dev, domid, first, last);
1404f2480e7dSThomas Gleixner msi_unlock_descs(dev);
1405f2480e7dSThomas Gleixner return ret;
1406f2480e7dSThomas Gleixner }
1407f2480e7dSThomas Gleixner
1408f2480e7dSThomas Gleixner /**
1409f2480e7dSThomas Gleixner * msi_domain_alloc_irqs_all_locked - Allocate all interrupts from a MSI interrupt domain
1410f2480e7dSThomas Gleixner *
1411f2480e7dSThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
1412f2480e7dSThomas Gleixner * are allocated
1413f2480e7dSThomas Gleixner * @domid: Id of the interrupt domain to operate on
1414f2480e7dSThomas Gleixner * @nirqs: The number of interrupts to allocate
1415f2480e7dSThomas Gleixner *
1416f2480e7dSThomas Gleixner * This function scans all MSI descriptors of the MSI domain and allocates interrupts
1417f2480e7dSThomas Gleixner * for all unassigned ones. That function is to be used for MSI domain usage where
1418f2480e7dSThomas Gleixner * the descriptor allocation is handled at the call site, e.g. PCI/MSI[X].
1419f2480e7dSThomas Gleixner *
1420f2480e7dSThomas Gleixner * Return: %0 on success or an error code.
1421f2480e7dSThomas Gleixner */
msi_domain_alloc_irqs_all_locked(struct device * dev,unsigned int domid,int nirqs)1422f2480e7dSThomas Gleixner int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs)
1423f2480e7dSThomas Gleixner {
1424f2480e7dSThomas Gleixner struct msi_ctrl ctrl = {
1425f2480e7dSThomas Gleixner .domid = domid,
1426f2480e7dSThomas Gleixner .first = 0,
142736db3d90SThomas Gleixner .last = msi_domain_get_hwsize(dev, domid) - 1,
1428f2480e7dSThomas Gleixner .nirqs = nirqs,
1429f2480e7dSThomas Gleixner };
1430f2480e7dSThomas Gleixner
1431f2480e7dSThomas Gleixner return msi_domain_alloc_locked(dev, &ctrl);
1432645474e2SThomas Gleixner }
1433645474e2SThomas Gleixner
14343d393b21SThomas Gleixner /**
14353d393b21SThomas Gleixner * msi_domain_alloc_irq_at - Allocate an interrupt from a MSI interrupt domain at
14363d393b21SThomas Gleixner * a given index - or at the next free index
14373d393b21SThomas Gleixner *
14383d393b21SThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
14393d393b21SThomas Gleixner * are allocated
14403d393b21SThomas Gleixner * @domid: Id of the interrupt domain to operate on
14413d393b21SThomas Gleixner * @index: Index for allocation. If @index == %MSI_ANY_INDEX the allocation
14423d393b21SThomas Gleixner * uses the next free index.
14433d393b21SThomas Gleixner * @affdesc: Optional pointer to an interrupt affinity descriptor structure
14443d393b21SThomas Gleixner * @icookie: Optional pointer to a domain specific per instance cookie. If
14453d393b21SThomas Gleixner * non-NULL the content of the cookie is stored in msi_desc::data.
14463d393b21SThomas Gleixner * Must be NULL for MSI-X allocations
14473d393b21SThomas Gleixner *
14483d393b21SThomas Gleixner * This requires a MSI interrupt domain which lets the core code manage the
14493d393b21SThomas Gleixner * MSI descriptors.
14503d393b21SThomas Gleixner *
14513d393b21SThomas Gleixner * Return: struct msi_map
14523d393b21SThomas Gleixner *
14533d393b21SThomas Gleixner * On success msi_map::index contains the allocated index number and
14543d393b21SThomas Gleixner * msi_map::virq the corresponding Linux interrupt number
14553d393b21SThomas Gleixner *
14563d393b21SThomas Gleixner * On failure msi_map::index contains the error code and msi_map::virq
14573d393b21SThomas Gleixner * is %0.
14583d393b21SThomas Gleixner */
msi_domain_alloc_irq_at(struct device * dev,unsigned int domid,unsigned int index,const struct irq_affinity_desc * affdesc,union msi_instance_cookie * icookie)14593d393b21SThomas Gleixner struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, unsigned int index,
14603d393b21SThomas Gleixner const struct irq_affinity_desc *affdesc,
14613d393b21SThomas Gleixner union msi_instance_cookie *icookie)
14623d393b21SThomas Gleixner {
14633d393b21SThomas Gleixner struct msi_ctrl ctrl = { .domid = domid, .nirqs = 1, };
14643d393b21SThomas Gleixner struct irq_domain *domain;
14653d393b21SThomas Gleixner struct msi_map map = { };
14663d393b21SThomas Gleixner struct msi_desc *desc;
14673d393b21SThomas Gleixner int ret;
14683d393b21SThomas Gleixner
14693d393b21SThomas Gleixner msi_lock_descs(dev);
14703d393b21SThomas Gleixner domain = msi_get_device_domain(dev, domid);
14713d393b21SThomas Gleixner if (!domain) {
14723d393b21SThomas Gleixner map.index = -ENODEV;
14733d393b21SThomas Gleixner goto unlock;
14743d393b21SThomas Gleixner }
14753d393b21SThomas Gleixner
14763d393b21SThomas Gleixner desc = msi_alloc_desc(dev, 1, affdesc);
14773d393b21SThomas Gleixner if (!desc) {
14783d393b21SThomas Gleixner map.index = -ENOMEM;
14793d393b21SThomas Gleixner goto unlock;
14803d393b21SThomas Gleixner }
14813d393b21SThomas Gleixner
14823d393b21SThomas Gleixner if (icookie)
14833d393b21SThomas Gleixner desc->data.icookie = *icookie;
14843d393b21SThomas Gleixner
14853d393b21SThomas Gleixner ret = msi_insert_desc(dev, desc, domid, index);
14863d393b21SThomas Gleixner if (ret) {
14873d393b21SThomas Gleixner map.index = ret;
14883d393b21SThomas Gleixner goto unlock;
14893d393b21SThomas Gleixner }
14903d393b21SThomas Gleixner
14913d393b21SThomas Gleixner ctrl.first = ctrl.last = desc->msi_index;
14923d393b21SThomas Gleixner
14933d393b21SThomas Gleixner ret = __msi_domain_alloc_irqs(dev, domain, &ctrl);
14943d393b21SThomas Gleixner if (ret) {
14953d393b21SThomas Gleixner map.index = ret;
14963d393b21SThomas Gleixner msi_domain_free_locked(dev, &ctrl);
14973d393b21SThomas Gleixner } else {
14983d393b21SThomas Gleixner map.index = desc->msi_index;
14993d393b21SThomas Gleixner map.virq = desc->irq;
15003d393b21SThomas Gleixner }
15013d393b21SThomas Gleixner unlock:
15023d393b21SThomas Gleixner msi_unlock_descs(dev);
15033d393b21SThomas Gleixner return map;
15043d393b21SThomas Gleixner }
15053d393b21SThomas Gleixner
__msi_domain_free_irqs(struct device * dev,struct irq_domain * domain,struct msi_ctrl * ctrl)15064cd5f440SThomas Gleixner static void __msi_domain_free_irqs(struct device *dev, struct irq_domain *domain,
15074cd5f440SThomas Gleixner struct msi_ctrl *ctrl)
1508d9109698SJiang Liu {
15094cd5f440SThomas Gleixner struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store;
1510bf5e758fSThomas Gleixner struct msi_domain_info *info = domain->host_data;
1511ef8dd015SThomas Gleixner struct irq_data *irqd;
1512d9109698SJiang Liu struct msi_desc *desc;
15134cd5f440SThomas Gleixner unsigned long idx;
1514dbbc9357SBixuan Cui int i;
1515dbbc9357SBixuan Cui
15164cd5f440SThomas Gleixner xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
1517ef8dd015SThomas Gleixner /* Only handle MSI entries which have an interrupt associated */
15184cd5f440SThomas Gleixner if (!msi_desc_match(desc, MSI_DESC_ASSOCIATED))
15194cd5f440SThomas Gleixner continue;
15204cd5f440SThomas Gleixner
1521ef8dd015SThomas Gleixner /* Make sure all interrupts are deactivated */
1522ef8dd015SThomas Gleixner for (i = 0; i < desc->nvec_used; i++) {
1523ef8dd015SThomas Gleixner irqd = irq_domain_get_irq_data(domain, desc->irq + i);
1524ef8dd015SThomas Gleixner if (irqd && irqd_is_activated(irqd))
1525ef8dd015SThomas Gleixner irq_domain_deactivate_irq(irqd);
1526dbbc9357SBixuan Cui }
1527d9109698SJiang Liu
1528d9109698SJiang Liu irq_domain_free_irqs(desc->irq, desc->nvec_used);
1529bf5e758fSThomas Gleixner if (info->flags & MSI_FLAG_DEV_SYSFS)
1530bf5e758fSThomas Gleixner msi_sysfs_remove_desc(dev, desc);
1531d9109698SJiang Liu desc->irq = 0;
1532d9109698SJiang Liu }
1533d9109698SJiang Liu }
1534d9109698SJiang Liu
msi_domain_free_locked(struct device * dev,struct msi_ctrl * ctrl)15354cd5f440SThomas Gleixner static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl)
1536645474e2SThomas Gleixner {
15374cd5f440SThomas Gleixner struct msi_domain_info *info;
15384cd5f440SThomas Gleixner struct msi_domain_ops *ops;
15394cd5f440SThomas Gleixner struct irq_domain *domain;
15404cd5f440SThomas Gleixner
15414cd5f440SThomas Gleixner if (!msi_ctrl_valid(dev, ctrl))
15424cd5f440SThomas Gleixner return;
15434cd5f440SThomas Gleixner
15444cd5f440SThomas Gleixner domain = msi_get_device_domain(dev, ctrl->domid);
15454cd5f440SThomas Gleixner if (!domain)
15464cd5f440SThomas Gleixner return;
15474cd5f440SThomas Gleixner
15484cd5f440SThomas Gleixner info = domain->host_data;
15494cd5f440SThomas Gleixner ops = info->ops;
15504cd5f440SThomas Gleixner
15514cd5f440SThomas Gleixner if (ops->domain_free_irqs)
15524cd5f440SThomas Gleixner ops->domain_free_irqs(domain, dev);
15534cd5f440SThomas Gleixner else
15544cd5f440SThomas Gleixner __msi_domain_free_irqs(dev, domain, ctrl);
15554cd5f440SThomas Gleixner
15564cd5f440SThomas Gleixner if (ops->msi_post_free)
15574cd5f440SThomas Gleixner ops->msi_post_free(domain, dev);
15584cd5f440SThomas Gleixner
1559645474e2SThomas Gleixner if (info->flags & MSI_FLAG_FREE_MSI_DESCS)
15604cd5f440SThomas Gleixner msi_domain_free_descs(dev, ctrl);
15614cd5f440SThomas Gleixner }
15624cd5f440SThomas Gleixner
15634cd5f440SThomas Gleixner /**
15644cd5f440SThomas Gleixner * msi_domain_free_irqs_range_locked - Free a range of interrupts from a MSI interrupt domain
15654cd5f440SThomas Gleixner * associated to @dev with msi_lock held
15664cd5f440SThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
15674cd5f440SThomas Gleixner * are freed
15684cd5f440SThomas Gleixner * @domid: Id of the interrupt domain to operate on
15694cd5f440SThomas Gleixner * @first: First index to free (inclusive)
15704cd5f440SThomas Gleixner * @last: Last index to free (inclusive)
15714cd5f440SThomas Gleixner */
msi_domain_free_irqs_range_locked(struct device * dev,unsigned int domid,unsigned int first,unsigned int last)15724cd5f440SThomas Gleixner void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
15734cd5f440SThomas Gleixner unsigned int first, unsigned int last)
15744cd5f440SThomas Gleixner {
15754cd5f440SThomas Gleixner struct msi_ctrl ctrl = {
15764cd5f440SThomas Gleixner .domid = domid,
15774cd5f440SThomas Gleixner .first = first,
15784cd5f440SThomas Gleixner .last = last,
15794cd5f440SThomas Gleixner };
15804cd5f440SThomas Gleixner msi_domain_free_locked(dev, &ctrl);
15814cd5f440SThomas Gleixner }
15824cd5f440SThomas Gleixner
15834cd5f440SThomas Gleixner /**
15844cd5f440SThomas Gleixner * msi_domain_free_irqs_range - Free a range of interrupts from a MSI interrupt domain
15854cd5f440SThomas Gleixner * associated to @dev
15864cd5f440SThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
15874cd5f440SThomas Gleixner * are freed
15884cd5f440SThomas Gleixner * @domid: Id of the interrupt domain to operate on
15894cd5f440SThomas Gleixner * @first: First index to free (inclusive)
15904cd5f440SThomas Gleixner * @last: Last index to free (inclusive)
15914cd5f440SThomas Gleixner */
msi_domain_free_irqs_range(struct device * dev,unsigned int domid,unsigned int first,unsigned int last)15924cd5f440SThomas Gleixner void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
15934cd5f440SThomas Gleixner unsigned int first, unsigned int last)
15944cd5f440SThomas Gleixner {
15954cd5f440SThomas Gleixner msi_lock_descs(dev);
15964cd5f440SThomas Gleixner msi_domain_free_irqs_range_locked(dev, domid, first, last);
15974cd5f440SThomas Gleixner msi_unlock_descs(dev);
15984cd5f440SThomas Gleixner }
15994cd5f440SThomas Gleixner
16004cd5f440SThomas Gleixner /**
16014cd5f440SThomas Gleixner * msi_domain_free_irqs_all_locked - Free all interrupts from a MSI interrupt domain
16024cd5f440SThomas Gleixner * associated to a device
16034cd5f440SThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
16044cd5f440SThomas Gleixner * are freed
16054cd5f440SThomas Gleixner * @domid: The id of the domain to operate on
16064cd5f440SThomas Gleixner *
16074cd5f440SThomas Gleixner * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
16084cd5f440SThomas Gleixner * pair. Use this for MSI irqdomains which implement their own vector
16094cd5f440SThomas Gleixner * allocation.
16104cd5f440SThomas Gleixner */
msi_domain_free_irqs_all_locked(struct device * dev,unsigned int domid)16114cd5f440SThomas Gleixner void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid)
16124cd5f440SThomas Gleixner {
161336db3d90SThomas Gleixner msi_domain_free_irqs_range_locked(dev, domid, 0,
161436db3d90SThomas Gleixner msi_domain_get_hwsize(dev, domid) - 1);
16154cd5f440SThomas Gleixner }
16164cd5f440SThomas Gleixner
16174cd5f440SThomas Gleixner /**
16184cd5f440SThomas Gleixner * msi_domain_free_irqs_all - Free all interrupts from a MSI interrupt domain
16194cd5f440SThomas Gleixner * associated to a device
16204cd5f440SThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts
16214cd5f440SThomas Gleixner * are freed
16224cd5f440SThomas Gleixner * @domid: The id of the domain to operate on
16234cd5f440SThomas Gleixner */
msi_domain_free_irqs_all(struct device * dev,unsigned int domid)16244cd5f440SThomas Gleixner void msi_domain_free_irqs_all(struct device *dev, unsigned int domid)
16254cd5f440SThomas Gleixner {
16264cd5f440SThomas Gleixner msi_lock_descs(dev);
16274cd5f440SThomas Gleixner msi_domain_free_irqs_all_locked(dev, domid);
16284cd5f440SThomas Gleixner msi_unlock_descs(dev);
1629645474e2SThomas Gleixner }
1630645474e2SThomas Gleixner
1631d9109698SJiang Liu /**
1632f3cf8bb0SJiang Liu * msi_get_domain_info - Get the MSI interrupt domain info for @domain
1633f3cf8bb0SJiang Liu * @domain: The interrupt domain to retrieve data from
1634f3cf8bb0SJiang Liu *
16353b35e7e6SRandy Dunlap * Return: the pointer to the msi_domain_info stored in @domain->host_data.
1636f3cf8bb0SJiang Liu */
msi_get_domain_info(struct irq_domain * domain)1637f3cf8bb0SJiang Liu struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
1638f3cf8bb0SJiang Liu {
1639f3cf8bb0SJiang Liu return (struct msi_domain_info *)domain->host_data;
1640f3cf8bb0SJiang Liu }
164117cde5e6SJason Gunthorpe
164217cde5e6SJason Gunthorpe /**
164317cde5e6SJason Gunthorpe * msi_device_has_isolated_msi - True if the device has isolated MSI
164417cde5e6SJason Gunthorpe * @dev: The device to check
164517cde5e6SJason Gunthorpe *
164617cde5e6SJason Gunthorpe * Isolated MSI means that HW modeled by an irq_domain on the path from the
164717cde5e6SJason Gunthorpe * initiating device to the CPU will validate that the MSI message specifies an
164817cde5e6SJason Gunthorpe * interrupt number that the device is authorized to trigger. This must block
164917cde5e6SJason Gunthorpe * devices from triggering interrupts they are not authorized to trigger.
165017cde5e6SJason Gunthorpe * Currently authorization means the MSI vector is one assigned to the device.
165117cde5e6SJason Gunthorpe *
165217cde5e6SJason Gunthorpe * This is interesting for securing VFIO use cases where a rouge MSI (eg created
165317cde5e6SJason Gunthorpe * by abusing a normal PCI MemWr DMA) must not allow the VFIO userspace to
165417cde5e6SJason Gunthorpe * impact outside its security domain, eg userspace triggering interrupts on
165517cde5e6SJason Gunthorpe * kernel drivers, a VM triggering interrupts on the hypervisor, or a VM
165617cde5e6SJason Gunthorpe * triggering interrupts on another VM.
165717cde5e6SJason Gunthorpe */
msi_device_has_isolated_msi(struct device * dev)165817cde5e6SJason Gunthorpe bool msi_device_has_isolated_msi(struct device *dev)
165917cde5e6SJason Gunthorpe {
166017cde5e6SJason Gunthorpe struct irq_domain *domain = dev_get_msi_domain(dev);
166117cde5e6SJason Gunthorpe
166217cde5e6SJason Gunthorpe for (; domain; domain = domain->parent)
1663dcb83f6eSJason Gunthorpe if (domain->flags & IRQ_DOMAIN_FLAG_ISOLATED_MSI)
166417cde5e6SJason Gunthorpe return true;
1665bf210f79SJason Gunthorpe return arch_is_isolated_msi();
166617cde5e6SJason Gunthorpe }
166717cde5e6SJason Gunthorpe EXPORT_SYMBOL_GPL(msi_device_has_isolated_msi);
1668