summaryrefslogtreecommitdiffstats
path: root/drivers/pci/endpoint/pci-ep-msi.c
blob: 9c13f193537df6e58514eb0b8767a9c3d94b5255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// SPDX-License-Identifier: GPL-2.0
/*
 * PCI Endpoint *Controller* (EPC) MSI library
 *
 * Copyright (C) 2025 NXP
 * Author: Frank Li <Frank.Li@nxp.com>
 */

#include <linux/align.h>
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/export.h>
#include <linux/interrupt.h>
#include <linux/irqdomain.h>
#include <linux/module.h>
#include <linux/msi.h>
#include <linux/of_irq.h>
#include <linux/pci-epc.h>
#include <linux/pci-epf.h>
#include <linux/pci-ep-cfs.h>
#include <linux/pci-ep-msi.h>
#include <linux/slab.h>

static void pci_epf_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
{
	struct pci_epc *epc;
	struct pci_epf *epf;

	epc = pci_epc_get(dev_name(msi_desc_to_dev(desc)));
	if (IS_ERR(epc))
		return;

	epf = list_first_entry_or_null(&epc->pci_epf, struct pci_epf, list);

	if (epf && epf->db_msg && desc->msi_index < epf->num_db)
		memcpy(&epf->db_msg[desc->msi_index].msg, msg, sizeof(*msg));

	pci_epc_put(epc);
}

static int pci_epf_alloc_doorbell_embedded(struct pci_epf *epf, u16 num_db)
{
	const struct pci_epc_aux_resource *doorbell = NULL;
	struct pci_epf_doorbell_msg *msg;
	struct pci_epc *epc = epf->epc;
	size_t map_size = 0, off = 0;
	dma_addr_t iova_base = 0;
	phys_addr_t phys_base;
	int count, ret, i;
	u64 addr;

	count = pci_epc_get_aux_resources_count(epc, epf->func_no,
						epf->vfunc_no);
	if (count < 0)
		return count;
	if (!count)
		return -ENODEV;

	struct pci_epc_aux_resource *res __free(kfree) =
				kzalloc_objs(*res, count);
	if (!res)
		return -ENOMEM;

	ret = pci_epc_get_aux_resources(epc, epf->func_no, epf->vfunc_no,
					res, count);
	if (ret)
		return ret;

	/* TODO: Support multiple DOORBELL_MMIO resources per EPC. */
	for (i = 0; i < count; i++) {
		if (res[i].type != PCI_EPC_AUX_DOORBELL_MMIO)
			continue;

		doorbell = &res[i];
		break;
	}
	if (!doorbell)
		return -ENODEV;
	addr = doorbell->phys_addr;
	if (!IS_ALIGNED(addr, sizeof(u32)))
		return -EINVAL;

	/*
	 * Reuse the pre-exposed BAR window if available. Otherwise map the MMIO
	 * doorbell resource here. Any required IOMMU mapping is handled
	 * internally, matching the MSI doorbell semantics.
	 */
	if (doorbell->bar == NO_BAR) {
		phys_base = addr & PAGE_MASK;
		off = addr - phys_base;
		map_size = PAGE_ALIGN(off + sizeof(u32));

		iova_base = dma_map_resource(epc->dev.parent, phys_base,
					     map_size, DMA_FROM_DEVICE, 0);
		if (dma_mapping_error(epc->dev.parent, iova_base))
			return -EIO;

		addr = iova_base + off;
	}

	msg = kzalloc_objs(*msg, num_db);
	if (!msg) {
		ret = -ENOMEM;
		goto err_unmap;
	}

	/*
	 * Embedded doorbell backends (e.g. DesignWare eDMA interrupt emulation)
	 * typically provide a single IRQ and do not offer per-doorbell
	 * distinguishable address/data pairs. The EPC aux resource therefore
	 * exposes one DOORBELL_MMIO entry (u.db_mmio.irq).
	 *
	 * Still, pci_epf_alloc_doorbell() allows requesting multiple doorbells.
	 * For such backends we replicate the same address/data for each entry
	 * and mark the IRQ as shared (IRQF_SHARED). Consumers must treat them
	 * as equivalent "kick" doorbells.
	 */
	for (i = 0; i < num_db; i++)
		msg[i] = (struct pci_epf_doorbell_msg) {
			.msg.address_lo = (u32)addr,
			.msg.address_hi = (u32)(addr >> 32),
			.msg.data = doorbell->u.db_mmio.data,
			.virq = doorbell->u.db_mmio.irq,
			.irq_flags = IRQF_SHARED,
			.type = PCI_EPF_DOORBELL_EMBEDDED,
			.bar = doorbell->bar,
			.offset = (doorbell->bar == NO_BAR) ? 0 :
				  doorbell->bar_offset,
			.iova_base = iova_base,
			.iova_size = map_size,
		};

	epf->num_db = num_db;
	epf->db_msg = msg;
	return 0;

err_unmap:
	if (map_size)
		dma_unmap_resource(epc->dev.parent, iova_base, map_size,
				   DMA_FROM_DEVICE, 0);
	return ret;
}

static int pci_epf_alloc_doorbell_msi(struct pci_epf *epf, u16 num_db)
{
	struct pci_epf_doorbell_msg *msg;
	struct device *dev = &epf->dev;
	struct pci_epc *epc = epf->epc;
	struct irq_domain *domain;
	int ret, i;

	domain = of_msi_map_get_device_domain(epc->dev.parent, 0,
					      DOMAIN_BUS_PLATFORM_MSI);
	if (!domain) {
		dev_err(dev, "Can't find MSI domain for EPC\n");
		return -ENODEV;
	}

	if (!irq_domain_is_msi_parent(domain))
		return -ENODEV;

	if (!irq_domain_is_msi_immutable(domain)) {
		dev_err(dev, "Mutable MSI controller not supported\n");
		return -ENODEV;
	}

	dev_set_msi_domain(epc->dev.parent, domain);

	msg = kzalloc_objs(struct pci_epf_doorbell_msg, num_db);
	if (!msg)
		return -ENOMEM;

	for (i = 0; i < num_db; i++)
		msg[i] = (struct pci_epf_doorbell_msg) {
			.type = PCI_EPF_DOORBELL_MSI,
			.bar = NO_BAR,
		};

	epf->num_db = num_db;
	epf->db_msg = msg;

	ret = platform_device_msi_init_and_alloc_irqs(epc->dev.parent, num_db,
						      pci_epf_write_msi_msg);
	if (ret) {
		dev_err(dev, "Failed to allocate MSI\n");
		kfree(msg);
		epf->db_msg = NULL;
		epf->num_db = 0;
		return ret;
	}

	for (i = 0; i < num_db; i++)
		epf->db_msg[i].virq = msi_get_virq(epc->dev.parent, i);

	return 0;
}

int pci_epf_alloc_doorbell(struct pci_epf *epf, u16 num_db)
{
	struct pci_epc *epc = epf->epc;
	struct device *dev = &epf->dev;
	int ret;

	/* TODO: Multi-EPF support */
	if (list_first_entry_or_null(&epc->pci_epf, struct pci_epf, list) != epf) {
		dev_err(dev, "Doorbell doesn't support multiple EPF\n");
		return -EINVAL;
	}

	if (epf->db_msg)
		return -EBUSY;

	ret = pci_epf_alloc_doorbell_msi(epf, num_db);
	if (!ret)
		return 0;

	/*
	 * Fall back to embedded doorbell only when platform MSI is unavailable
	 * for this EPC.
	 */
	if (ret != -ENODEV)
		return ret;

	ret = pci_epf_alloc_doorbell_embedded(epf, num_db);
	if (ret) {
		dev_err(dev, "Failed to allocate doorbell: %d\n", ret);
		return ret;
	}

	dev_info(dev, "Using embedded (DMA) doorbell fallback\n");
	return 0;
}
EXPORT_SYMBOL_GPL(pci_epf_alloc_doorbell);

void pci_epf_free_doorbell(struct pci_epf *epf)
{
	struct pci_epf_doorbell_msg *msg0;
	struct pci_epc *epc = epf->epc;

	if (!epf->db_msg)
		return;

	msg0 = &epf->db_msg[0];
	if (msg0->type == PCI_EPF_DOORBELL_MSI)
		platform_device_msi_free_irqs_all(epf->epc->dev.parent);
	else if (msg0->type == PCI_EPF_DOORBELL_EMBEDDED && msg0->iova_size)
		dma_unmap_resource(epc->dev.parent, msg0->iova_base,
				   msg0->iova_size, DMA_FROM_DEVICE, 0);

	kfree(epf->db_msg);
	epf->db_msg = NULL;
	epf->num_db = 0;
}
EXPORT_SYMBOL_GPL(pci_epf_free_doorbell);