Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2026 Advanced Micro Devices, Inc.
3 : : */
4 : :
5 : : #include <stdint.h>
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <string.h>
9 : : #include <errno.h>
10 : : #include <fcntl.h>
11 : : #include <unistd.h>
12 : : #include <limits.h>
13 : :
14 : : #include <rte_common.h>
15 : : #include <rte_io.h>
16 : : #include <rte_eal.h>
17 : : #include <rte_pci.h>
18 : : #include <bus_pci_driver.h>
19 : : #include <rte_rawdev.h>
20 : : #include <rte_rawdev_pmd.h>
21 : : #include <rte_malloc.h>
22 : : #include <rte_memzone.h>
23 : :
24 : : #include "ntb.h"
25 : : #include "ntb_hw_amd.h"
26 : :
27 : : static enum amd_ntb_bar amd_ntb_bar[] = {
28 : : AMD_NTB_BAR23,
29 : : AMD_NTB_BAR45,
30 : : };
31 : :
32 : : static int
33 : 0 : amd_ntb_dev_init(const struct rte_rawdev *dev)
34 : : {
35 : 0 : struct ntb_hw *ntb = dev->dev_private;
36 : : uint32_t sideinfo;
37 : : int i, bar;
38 : :
39 [ # # ]: 0 : if (ntb == NULL) {
40 : 0 : NTB_LOG(ERR, "Invalid device.");
41 : 0 : return -EINVAL;
42 : : }
43 : :
44 : 0 : ntb->hw_addr = (char *)ntb->pci_dev->mem_resource[0].addr;
45 : :
46 : : /* Bit 0 of the link status register carries the topology. */
47 : : sideinfo = rte_read32((char *)ntb->hw_addr + AMD_SIDEINFO_OFFSET);
48 [ # # ]: 0 : if (sideinfo & AMD_SIDE_MASK)
49 : 0 : ntb->topo = NTB_TOPO_SEC;
50 : : else
51 : 0 : ntb->topo = NTB_TOPO_PRI;
52 : :
53 [ # # ]: 0 : NTB_LOG(INFO, "Device topology: %s (sideinfo 0x%" PRIx32 ")",
54 : : ntb->topo == NTB_TOPO_SEC ? "secondary" : "primary", sideinfo);
55 : :
56 : 0 : ntb->mw_cnt = AMD_MW_COUNT;
57 : 0 : ntb->db_cnt = AMD_DB_COUNT;
58 : : /* The 16-register scratchpad bank is split into two halves, so only
59 : : * AMD_SPAD_PER_SIDE registers are usable per side.
60 : : */
61 : 0 : ntb->spad_cnt = AMD_SPAD_PER_SIDE;
62 : :
63 : 0 : ntb->mw_size = rte_zmalloc("ntb_mw_size", ntb->mw_cnt * sizeof(uint64_t), 0);
64 [ # # ]: 0 : if (ntb->mw_size == NULL) {
65 : 0 : NTB_LOG(ERR, "Cannot allocate memory for mw size.");
66 : 0 : return -ENOMEM;
67 : : }
68 [ # # ]: 0 : for (i = 0; i < ntb->mw_cnt; i++) {
69 : 0 : bar = amd_ntb_bar[i];
70 : 0 : ntb->mw_size[i] = ntb->pci_dev->mem_resource[bar].len;
71 : 0 : NTB_LOG(INFO, "mw[%d] bar%d size 0x%" PRIx64, i, bar, ntb->mw_size[i]);
72 : : }
73 : :
74 : : /* Reserve the last 2 scratchpad registers for application use. */
75 [ # # ]: 0 : for (i = 0; i < NTB_SPAD_USER_MAX_NUM; i++)
76 : 0 : ntb->spad_user_list[i] = ntb->spad_cnt;
77 : 0 : ntb->spad_user_list[0] = ntb->spad_cnt - 2;
78 : 0 : ntb->spad_user_list[1] = ntb->spad_cnt - 1;
79 : :
80 : 0 : return 0;
81 : : }
82 : :
83 : : static void *
84 : 0 : amd_ntb_get_peer_mw_addr(const struct rte_rawdev *dev, int mw_idx)
85 : : {
86 : 0 : struct ntb_hw *ntb = dev->dev_private;
87 : :
88 [ # # # # ]: 0 : if (mw_idx < 0 || mw_idx >= ntb->mw_cnt) {
89 : 0 : NTB_LOG(ERR, "Invalid memory window index (0 - %u).", ntb->mw_cnt - 1);
90 : 0 : return NULL;
91 : : }
92 : :
93 : 0 : return ntb->pci_dev->mem_resource[amd_ntb_bar[mw_idx]].addr;
94 : : }
95 : :
96 : : static int
97 : 0 : amd_ntb_mw_set_trans(const struct rte_rawdev *dev, int mw_idx, uint64_t addr, uint64_t size)
98 : : {
99 : 0 : struct ntb_hw *ntb = dev->dev_private;
100 : 0 : void *peer_mmio = AMD_PEER_MMIO(ntb);
101 : : uint32_t xlat_off, limit_off;
102 : : uint64_t mw_size, reg_val;
103 : : uint8_t bar;
104 : :
105 [ # # # # ]: 0 : if (mw_idx < 0 || mw_idx >= ntb->mw_cnt) {
106 : 0 : NTB_LOG(ERR, "Invalid memory window index (0 - %u).", ntb->mw_cnt - 1);
107 : 0 : return -EINVAL;
108 : : }
109 : :
110 : 0 : bar = amd_ntb_bar[mw_idx];
111 : 0 : mw_size = ntb->pci_dev->mem_resource[bar].len;
112 [ # # ]: 0 : if (size > mw_size) {
113 : 0 : NTB_LOG(ERR, "Set translation size 0x%" PRIx64 " exceeds mw "
114 : : "size 0x%" PRIx64, size, mw_size);
115 : 0 : return -EINVAL;
116 : : }
117 : :
118 : : /* Outbound translation forms the target as (base | offset), so a
119 : : * misaligned base aliases writes (see mem_align op). Reject it early.
120 : : */
121 [ # # ]: 0 : if (addr & (rte_align64pow2(size) - 1)) {
122 : 0 : NTB_LOG(ERR, "mw%d translation base 0x%" PRIx64 " is not "
123 : : "aligned to a power of two >= size 0x%" PRIx64
124 : : "; window writes would alias.", mw_idx, addr, size);
125 : 0 : return -EINVAL;
126 : : }
127 : :
128 : : /* Program the peer's outbound translation window (S* registers) so
129 : : * that peer writes into its BAR land in our local memory at 'addr'.
130 : : */
131 [ # # ]: 0 : if (mw_idx == 0) {
132 : : xlat_off = AMD_BAR23_XLAT_OFFSET;
133 : : limit_off = AMD_BAR23_LIMIT_OFFSET;
134 : : } else {
135 : : xlat_off = AMD_BAR45_XLAT_OFFSET;
136 : : limit_off = AMD_BAR45_LIMIT_OFFSET;
137 : : }
138 : :
139 : 0 : rte_write64(addr, (char *)peer_mmio + xlat_off);
140 : : reg_val = rte_read64((char *)peer_mmio + xlat_off);
141 [ # # ]: 0 : if (reg_val != addr) {
142 : 0 : NTB_LOG(ERR, "Failed to set mw%d translation.", mw_idx);
143 : : rte_write64(0, (char *)peer_mmio + xlat_off);
144 : 0 : return -EIO;
145 : : }
146 : :
147 : 0 : rte_write64(size, (char *)peer_mmio + limit_off);
148 : : reg_val = rte_read64((char *)peer_mmio + limit_off);
149 [ # # ]: 0 : if (reg_val != size) {
150 : 0 : NTB_LOG(ERR, "Failed to set mw%d limit.", mw_idx);
151 : : rte_write64(0, (char *)peer_mmio + xlat_off);
152 : : rte_write64(0, (char *)peer_mmio + limit_off);
153 : 0 : return -EIO;
154 : : }
155 : :
156 : : return 0;
157 : : }
158 : :
159 : : static void *
160 : 0 : amd_ntb_ioremap(const struct rte_rawdev *dev, uint64_t addr)
161 : : {
162 : 0 : struct ntb_hw *ntb = dev->dev_private;
163 : : void *mapped = NULL;
164 : : void *base;
165 : : int i;
166 : :
167 [ # # ]: 0 : for (i = 0; i < ntb->peer_used_mws; i++) {
168 [ # # ]: 0 : if (addr >= ntb->peer_mw_base[i] &&
169 [ # # ]: 0 : addr <= ntb->peer_mw_base[i] + ntb->mw_size[i]) {
170 : 0 : base = amd_ntb_get_peer_mw_addr(dev, i);
171 : 0 : mapped = (void *)(size_t)(addr - ntb->peer_mw_base[i] + (size_t)base);
172 : 0 : break;
173 : : }
174 : : }
175 : :
176 : 0 : return mapped;
177 : : }
178 : :
179 : : /* Read link speed/width from the device's PCIe capability link status. */
180 : : static int
181 : 0 : amd_ntb_read_pcie_link_status(struct ntb_hw *ntb, uint16_t *link_status)
182 : : {
183 : 0 : struct rte_pci_device *pci_dev = ntb->pci_dev;
184 : : uint8_t pos, cap_id, next;
185 : : uint16_t status;
186 : : int ret;
187 : :
188 : 0 : ret = rte_pci_read_config(pci_dev, &status, sizeof(status), RTE_PCI_STATUS);
189 [ # # ]: 0 : if (ret != sizeof(status))
190 : : return -EIO;
191 [ # # ]: 0 : if (!(status & RTE_PCI_STATUS_CAP_LIST))
192 : : return -ENOTSUP;
193 : :
194 : 0 : ret = rte_pci_read_config(pci_dev, &pos, sizeof(pos), RTE_PCI_CAPABILITY_LIST);
195 [ # # ]: 0 : if (ret != sizeof(pos))
196 : : return -EIO;
197 : :
198 [ # # ]: 0 : while (pos) {
199 : 0 : ret = rte_pci_read_config(pci_dev, &cap_id, sizeof(cap_id), pos);
200 [ # # ]: 0 : if (ret != sizeof(cap_id))
201 : : return -EIO;
202 : 0 : ret = rte_pci_read_config(pci_dev, &next, sizeof(next), pos + 1);
203 [ # # ]: 0 : if (ret != sizeof(next))
204 : : return -EIO;
205 [ # # ]: 0 : if (cap_id == RTE_PCI_CAP_ID_EXP) {
206 : 0 : ret = rte_pci_read_config(pci_dev, link_status, sizeof(*link_status),
207 : 0 : pos + RTE_PCI_EXP_LNKSTA);
208 [ # # ]: 0 : if (ret != sizeof(*link_status))
209 : : return -EIO;
210 : 0 : return 0;
211 : : }
212 : 0 : pos = next;
213 : : }
214 : :
215 : : return -ENOTSUP;
216 : : }
217 : :
218 : : /* Read the PCIe link status (LNKSTA) from an arbitrary device's config space
219 : : * exposed via sysfs. Used to query bridges that are not bound to this driver
220 : : * (e.g. the upstream switch above a secondary-side NTB), which rte_pci_*
221 : : * cannot access directly.
222 : : */
223 : : static int
224 : 0 : amd_ntb_read_lnksta_sysfs(const char *config_path, uint16_t *link_status)
225 : : {
226 : : uint8_t pos, cap_id, next;
227 : : uint16_t status;
228 : : int fd, ret = -EIO;
229 : :
230 : : fd = open(config_path, O_RDONLY);
231 [ # # ]: 0 : if (fd < 0)
232 : 0 : return -errno;
233 : :
234 [ # # ]: 0 : if (pread(fd, &status, sizeof(status), RTE_PCI_STATUS) != sizeof(status))
235 : 0 : goto out;
236 [ # # ]: 0 : if (!(status & RTE_PCI_STATUS_CAP_LIST))
237 : 0 : goto out;
238 : :
239 [ # # ]: 0 : if (pread(fd, &pos, sizeof(pos), RTE_PCI_CAPABILITY_LIST) != sizeof(pos))
240 : 0 : goto out;
241 : :
242 [ # # ]: 0 : while (pos) {
243 [ # # ]: 0 : if (pread(fd, &cap_id, sizeof(cap_id), pos) != sizeof(cap_id))
244 : 0 : goto out;
245 [ # # ]: 0 : if (pread(fd, &next, sizeof(next), pos + 1) != sizeof(next))
246 : 0 : goto out;
247 [ # # ]: 0 : if (cap_id == RTE_PCI_CAP_ID_EXP) {
248 [ # # ]: 0 : if (pread(fd, link_status, sizeof(*link_status),
249 [ # # ]: 0 : pos + RTE_PCI_EXP_LNKSTA) == sizeof(*link_status))
250 : : ret = 0;
251 : 0 : goto out;
252 : : }
253 : 0 : pos = next;
254 : : }
255 : 0 : out:
256 : 0 : close(fd);
257 : 0 : return ret;
258 : : }
259 : :
260 : : /* On the secondary side the NTB device's own PCIe link status does not reflect
261 : : * the inter-host link. Mirror the Linux amd_ntb behaviour by walking up two
262 : : * bridge levels (device -> downstream switch -> upstream switch) and reading
263 : : * the upstream switch port's link status instead.
264 : : */
265 : : static int
266 : 0 : amd_ntb_read_upstream_link_status(struct ntb_hw *ntb, uint16_t *link_status)
267 : : {
268 : : char path[PATH_MAX];
269 : : char real[PATH_MAX];
270 : : char *p;
271 : : int i;
272 : :
273 : 0 : snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%x",
274 : 0 : ntb->pci_dev->addr.domain, ntb->pci_dev->addr.bus,
275 : 0 : ntb->pci_dev->addr.devid, ntb->pci_dev->addr.function);
276 : :
277 [ # # ]: 0 : if (realpath(path, real) == NULL)
278 : 0 : return -errno;
279 : :
280 : : /* Strip two trailing path components to reach the upstream switch. */
281 [ # # ]: 0 : for (i = 0; i < 2; i++) {
282 : 0 : p = strrchr(real, '/');
283 [ # # # # ]: 0 : if (p == NULL || p == real)
284 : : return -ENOENT;
285 : 0 : *p = '\0';
286 : : }
287 : :
288 : : snprintf(path, sizeof(path), "%s/config", real);
289 : 0 : return amd_ntb_read_lnksta_sysfs(path, link_status);
290 : : }
291 : :
292 : : static int
293 : 0 : amd_ntb_get_link_status(const struct rte_rawdev *dev)
294 : : {
295 : 0 : struct ntb_hw *ntb = dev->dev_private;
296 : 0 : uint16_t link_status = 0;
297 : : uint32_t sideinfo;
298 : : int ret;
299 : :
300 : : /* The link is usable once the peer has set its SIDE_READY bit. */
301 : 0 : sideinfo = rte_read32((char *)AMD_PEER_MMIO(ntb) + AMD_SIDEINFO_OFFSET);
302 : 0 : ntb->link_status = !!(sideinfo & AMD_SIDE_READY);
303 : :
304 [ # # ]: 0 : if (!ntb->link_status) {
305 : 0 : ntb->link_speed = NTB_SPEED_NONE;
306 : 0 : ntb->link_width = NTB_WIDTH_NONE;
307 : 0 : return 0;
308 : : }
309 : :
310 : : /* The primary reads its own PCIe link status; the secondary must read
311 : : * the upstream switch port above it. If the upstream read fails, fall
312 : : * back to the local device so speed/width is still best-effort.
313 : : */
314 [ # # ]: 0 : if (ntb->topo == NTB_TOPO_SEC) {
315 : 0 : ret = amd_ntb_read_upstream_link_status(ntb, &link_status);
316 [ # # ]: 0 : if (ret != 0)
317 : 0 : ret = amd_ntb_read_pcie_link_status(ntb, &link_status);
318 : : } else {
319 : 0 : ret = amd_ntb_read_pcie_link_status(ntb, &link_status);
320 : : }
321 : :
322 [ # # ]: 0 : if (ret == 0) {
323 : 0 : ntb->link_speed = AMD_LNK_STA_SPEED(link_status);
324 : 0 : ntb->link_width = AMD_LNK_STA_WIDTH(link_status);
325 : : } else {
326 : 0 : NTB_LOG(WARNING, "Failed to read PCIe link status (%d).", ret);
327 : 0 : ntb->link_speed = NTB_SPEED_NONE;
328 : 0 : ntb->link_width = NTB_WIDTH_NONE;
329 : : }
330 : :
331 : : return 0;
332 : : }
333 : :
334 : : static int
335 : 0 : amd_ntb_set_link(const struct rte_rawdev *dev, bool up)
336 : : {
337 : 0 : struct ntb_hw *ntb = dev->dev_private;
338 : 0 : void *mmio = ntb->hw_addr;
339 : : uint32_t reg;
340 : :
341 : : reg = rte_read32((char *)mmio + AMD_SIDEINFO_OFFSET);
342 [ # # ]: 0 : if (up) {
343 [ # # ]: 0 : if (!(reg & AMD_SIDE_READY)) {
344 : 0 : reg |= AMD_SIDE_READY;
345 : : rte_write32(reg, (char *)mmio + AMD_SIDEINFO_OFFSET);
346 : : }
347 : : reg = rte_read32((char *)mmio + AMD_CNTL_OFFSET);
348 : 0 : reg |= (AMD_PMM_REG_CTL | AMD_SMM_REG_CTL);
349 : : rte_write32(reg, (char *)mmio + AMD_CNTL_OFFSET);
350 : : } else {
351 [ # # ]: 0 : if (reg & AMD_SIDE_READY) {
352 : 0 : reg &= ~AMD_SIDE_READY;
353 : : rte_write32(reg, (char *)mmio + AMD_SIDEINFO_OFFSET);
354 : : }
355 : : reg = rte_read32((char *)mmio + AMD_CNTL_OFFSET);
356 : 0 : reg &= ~(AMD_PMM_REG_CTL | AMD_SMM_REG_CTL);
357 : : rte_write32(reg, (char *)mmio + AMD_CNTL_OFFSET);
358 : : }
359 : :
360 : 0 : return 0;
361 : : }
362 : :
363 : : static uint32_t
364 : 0 : amd_ntb_spad_read(const struct rte_rawdev *dev, int spad, bool peer)
365 : : {
366 : 0 : struct ntb_hw *ntb = dev->dev_private;
367 : : uint32_t offset;
368 : :
369 [ # # # # ]: 0 : if (spad < 0 || spad >= ntb->spad_cnt) {
370 : 0 : NTB_LOG(ERR, "Invalid scratchpad index.");
371 : 0 : return 0;
372 : : }
373 : :
374 [ # # ]: 0 : offset = AMD_SPAD_BASE(ntb, peer);
375 : :
376 : 0 : return rte_read32((char *)ntb->hw_addr + AMD_SPAD_OFFSET + offset + (spad << 2));
377 : : }
378 : :
379 : : static int
380 : 0 : amd_ntb_spad_write(const struct rte_rawdev *dev, int spad, bool peer, uint32_t spad_v)
381 : : {
382 : 0 : struct ntb_hw *ntb = dev->dev_private;
383 : : uint32_t offset;
384 : :
385 [ # # # # ]: 0 : if (spad < 0 || spad >= ntb->spad_cnt) {
386 : 0 : NTB_LOG(ERR, "Invalid scratchpad index.");
387 : 0 : return -EINVAL;
388 : : }
389 : :
390 [ # # ]: 0 : offset = AMD_SPAD_BASE(ntb, peer);
391 : :
392 : 0 : rte_write32(spad_v, (char *)ntb->hw_addr + AMD_SPAD_OFFSET + offset + (spad << 2));
393 : :
394 : 0 : return 0;
395 : : }
396 : :
397 : : static uint64_t
398 : 0 : amd_ntb_db_read(const struct rte_rawdev *dev)
399 : : {
400 : 0 : struct ntb_hw *ntb = dev->dev_private;
401 : :
402 : 0 : return (uint64_t)rte_read16((char *)ntb->hw_addr + AMD_DBSTAT_OFFSET);
403 : : }
404 : :
405 : : static int
406 : 0 : amd_ntb_db_clear(const struct rte_rawdev *dev, uint64_t db_bits)
407 : : {
408 : 0 : struct ntb_hw *ntb = dev->dev_private;
409 : :
410 : 0 : rte_write16((uint16_t)db_bits, (char *)ntb->hw_addr + AMD_DBSTAT_OFFSET);
411 : :
412 : 0 : return 0;
413 : : }
414 : :
415 : : static int
416 : 0 : amd_ntb_db_set_mask(const struct rte_rawdev *dev, uint64_t db_mask)
417 : : {
418 : 0 : struct ntb_hw *ntb = dev->dev_private;
419 : :
420 [ # # ]: 0 : if (db_mask & ~ntb->db_valid_mask)
421 : : return -EINVAL;
422 : :
423 : 0 : ntb->db_mask |= db_mask;
424 : 0 : rte_write16((uint16_t)ntb->db_mask, (char *)ntb->hw_addr + AMD_DBMASK_OFFSET);
425 : :
426 : 0 : return 0;
427 : : }
428 : :
429 : : static int
430 : 0 : amd_ntb_peer_db_set(const struct rte_rawdev *dev, uint8_t db_idx)
431 : : {
432 : 0 : struct ntb_hw *ntb = dev->dev_private;
433 : :
434 [ # # ]: 0 : if (((uint64_t)1 << db_idx) & ~ntb->db_valid_mask) {
435 : 0 : NTB_LOG(ERR, "Invalid doorbell.");
436 : 0 : return -EINVAL;
437 : : }
438 : :
439 : 0 : rte_write16((uint16_t)1 << db_idx, (char *)ntb->hw_addr + AMD_DBREQ_OFFSET);
440 : :
441 : 0 : return 0;
442 : : }
443 : :
444 : : static int
445 : 0 : amd_ntb_vector_bind(const struct rte_rawdev *dev __rte_unused, uint8_t intr __rte_unused,
446 : : uint8_t msix __rte_unused)
447 : : {
448 : : /* Each doorbell/event maps to its MSI-X vector by default. */
449 : 0 : return 0;
450 : : }
451 : :
452 : : /* Handshake: advertises the local configuration to the peer using the
453 : : * packed 8-register scratchpad layout, programs the peer's outbound
454 : : * translation windows and rings doorbell 0 to signal readiness.
455 : : */
456 : : static int
457 : 0 : amd_ntb_dev_handshake(const struct rte_rawdev *dev)
458 : : {
459 : 0 : struct ntb_hw *ntb = dev->dev_private;
460 : : uint32_t info;
461 : : uint64_t base;
462 : : int i, ret;
463 : :
464 : 0 : info = (ntb->mw_cnt & 0xff) | ((uint32_t)(ntb->queue_pairs & 0xff) << 8) |
465 : 0 : ((uint32_t)(ntb->used_mw_num & 0xff) << 16);
466 : 0 : ret = amd_ntb_spad_write(dev, AMD_SPAD_CNT_INFO, 1, info);
467 [ # # ]: 0 : if (ret < 0)
468 : : return ret;
469 : :
470 : 0 : ret = amd_ntb_spad_write(dev, AMD_SPAD_QUEUE_SZ, 1, ntb->queue_size);
471 [ # # ]: 0 : if (ret < 0)
472 : : return ret;
473 : :
474 [ # # ]: 0 : for (i = 0; i < ntb->used_mw_num; i++) {
475 : : /* Advertise the memzone virtual base (used by ioremap on the
476 : : * peer) and program the translation with the IOVA.
477 : : */
478 : 0 : base = (uint64_t)(size_t)ntb->mz[i]->addr;
479 : 0 : ret = amd_ntb_spad_write(dev, AMD_SPAD_MW0_BA_L + 2 * i, 1, (uint32_t)base);
480 [ # # ]: 0 : if (ret < 0)
481 : 0 : return ret;
482 : 0 : ret = amd_ntb_spad_write(dev, AMD_SPAD_MW0_BA_H + 2 * i, 1,
483 : 0 : (uint32_t)(base >> 32));
484 [ # # ]: 0 : if (ret < 0)
485 : 0 : return ret;
486 : : }
487 : :
488 [ # # ]: 0 : for (i = 0; i < ntb->used_mw_num; i++) {
489 : 0 : ret = amd_ntb_mw_set_trans(dev, i, ntb->mz[i]->iova, ntb->mz[i]->len);
490 [ # # ]: 0 : if (ret < 0)
491 : 0 : return ret;
492 : : }
493 : :
494 : : /* Ring doorbell 0 to tell the peer the device is ready. */
495 : 0 : return amd_ntb_peer_db_set(dev, 0);
496 : : }
497 : :
498 : : /* Peer-config read at device start. Validates the peer's queue
499 : : * configuration and records the peer memory-window base addresses.
500 : : */
501 : : static int
502 : 0 : amd_ntb_read_peer_config(const struct rte_rawdev *dev)
503 : : {
504 : 0 : struct ntb_hw *ntb = dev->dev_private;
505 : : uint32_t info, peer_qps, peer_qsz, lo, hi;
506 : : int i;
507 : :
508 : 0 : info = amd_ntb_spad_read(dev, AMD_SPAD_CNT_INFO, 0);
509 : 0 : peer_qps = (info >> 8) & 0xff;
510 [ # # ]: 0 : if (peer_qps != ntb->queue_pairs) {
511 : 0 : NTB_LOG(ERR, "Inconsistent number of queues! (local: %u peer: %u)",
512 : : ntb->queue_pairs, peer_qps);
513 : 0 : return -EINVAL;
514 : : }
515 : :
516 : 0 : peer_qsz = amd_ntb_spad_read(dev, AMD_SPAD_QUEUE_SZ, 0);
517 [ # # ]: 0 : if (peer_qsz != ntb->queue_size) {
518 : 0 : NTB_LOG(ERR, "Inconsistent queue size! (local: %u peer: %u)",
519 : : ntb->queue_size, peer_qsz);
520 : 0 : return -EINVAL;
521 : : }
522 : :
523 : 0 : ntb->peer_used_mws = (info >> 16) & 0xff;
524 [ # # ]: 0 : for (i = 0; i < ntb->peer_used_mws; i++) {
525 : 0 : lo = amd_ntb_spad_read(dev, AMD_SPAD_MW0_BA_L + 2 * i, 0);
526 : 0 : hi = amd_ntb_spad_read(dev, AMD_SPAD_MW0_BA_H + 2 * i, 0);
527 : 0 : ntb->peer_mw_base[i] = ((uint64_t)hi << 32) | lo;
528 : : }
529 : :
530 : : return 0;
531 : : }
532 : :
533 : : /* Check that the peer advertises the same number of memory windows. */
534 : : static int
535 : 0 : amd_ntb_peer_mw_validate(const struct rte_rawdev *dev)
536 : : {
537 : 0 : struct ntb_hw *ntb = dev->dev_private;
538 : : uint32_t info, peer_mw_cnt;
539 : :
540 : 0 : info = amd_ntb_spad_read(dev, AMD_SPAD_CNT_INFO, 0);
541 : 0 : peer_mw_cnt = info & 0xff;
542 [ # # ]: 0 : if (peer_mw_cnt != ntb->mw_cnt) {
543 : 0 : NTB_LOG(ERR, "Peer mw cnt %u != local mw cnt %u.", peer_mw_cnt, ntb->mw_cnt);
544 : 0 : return -EINVAL;
545 : : }
546 : :
547 : : return 0;
548 : : }
549 : :
550 : : /* Tear down the local scratchpad and translations so the peer can no
551 : : * longer reach local memory.
552 : : */
553 : : static void
554 : 0 : amd_ntb_link_cleanup(const struct rte_rawdev *dev)
555 : : {
556 : 0 : struct ntb_hw *ntb = dev->dev_private;
557 : : int i;
558 : :
559 [ # # ]: 0 : for (i = 0; i < ntb->spad_cnt; i++)
560 : 0 : amd_ntb_spad_write(dev, i, 0, 0);
561 : :
562 [ # # ]: 0 : for (i = 0; i < ntb->used_mw_num; i++)
563 : 0 : amd_ntb_mw_set_trans(dev, i, 0, 0);
564 : 0 : }
565 : :
566 : : /* Handle link/power-management events and acknowledge the SMU. Invoked from
567 : : * the common interrupt handler when no inter-device doorbell is pending.
568 : : */
569 : : static void
570 : 0 : amd_ntb_handle_hw_event(const struct rte_rawdev *dev)
571 : : {
572 : 0 : struct ntb_hw *ntb = dev->dev_private;
573 : : uint32_t event, ack;
574 : :
575 : 0 : event = rte_read32((char *)ntb->hw_addr + AMD_INTSTAT_OFFSET);
576 : 0 : event &= AMD_EVENT_INTMASK;
577 [ # # ]: 0 : if (event == 0)
578 : : return;
579 : :
580 [ # # # # ]: 0 : switch (event) {
581 : 0 : case AMD_PEER_FLUSH_EVENT:
582 : 0 : NTB_LOG(INFO, "Peer flush event.");
583 : 0 : break;
584 : 0 : case AMD_PEER_D0_EVENT:
585 : 0 : ack = rte_read32((char *)ntb->hw_addr + AMD_PMESTAT_OFFSET);
586 [ # # ]: 0 : if (ack & 0x1)
587 : 0 : NTB_LOG(INFO, "D0 wakeup completed for NTB.");
588 : : /* fall through to ack the SMU */
589 : : /* Falls through. */
590 : : case AMD_PEER_RESET_EVENT:
591 : : case AMD_LINK_DOWN_EVENT:
592 : : case AMD_PEER_D3_EVENT:
593 : : case AMD_PEER_PMETO_EVENT:
594 : : case AMD_LINK_UP_EVENT:
595 : 0 : ack = rte_read32((char *)ntb->hw_addr + AMD_SMUACK_OFFSET);
596 : 0 : ack |= event;
597 : 0 : rte_write32(ack, (char *)ntb->hw_addr + AMD_SMUACK_OFFSET);
598 : : break;
599 : 0 : default:
600 : 0 : NTB_LOG(ERR, "Unknown interrupt event 0x%" PRIx32, event);
601 : 0 : break;
602 : : }
603 : : }
604 : :
605 : : static uint64_t
606 : 0 : amd_ntb_get_mem_align(const struct rte_rawdev *dev __rte_unused, uint32_t mw_id __rte_unused,
607 : : uint64_t mw_len)
608 : : {
609 : : /* Base must be a power of two >= the window length, and >= 4K. */
610 : 0 : return RTE_MAX((uint64_t)RTE_PGSIZE_4K, rte_align64pow2(mw_len));
611 : : }
612 : :
613 : : const struct ntb_dev_ops amd_ntb_ops = {
614 : : .ntb_dev_init = amd_ntb_dev_init,
615 : : .get_peer_mw_addr = amd_ntb_get_peer_mw_addr,
616 : : .ioremap = amd_ntb_ioremap,
617 : : .get_link_status = amd_ntb_get_link_status,
618 : : .set_link = amd_ntb_set_link,
619 : : .spad_read = amd_ntb_spad_read,
620 : : .spad_write = amd_ntb_spad_write,
621 : : .db_read = amd_ntb_db_read,
622 : : .db_clear = amd_ntb_db_clear,
623 : : .db_set_mask = amd_ntb_db_set_mask,
624 : : .peer_db_set = amd_ntb_peer_db_set,
625 : : .vector_bind = amd_ntb_vector_bind,
626 : : .dev_handshake = amd_ntb_dev_handshake,
627 : : .peer_mw_validate = amd_ntb_peer_mw_validate,
628 : : .read_peer_config = amd_ntb_read_peer_config,
629 : : .link_cleanup = amd_ntb_link_cleanup,
630 : : .mem_align = amd_ntb_get_mem_align,
631 : : .handle_hw_event = amd_ntb_handle_hw_event,
632 : : };
|