2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright (C) 2019 Advanced Micro Devices, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
15 * Copyright (c) 2019 Advanced Micro Devices, Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of AMD corporation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Contact Information :
42 * Rajesh Kumar <rajesh1.kumar@amd.com>
46 * The Non-Transparent Bridge (NTB) is a device that allows you to connect
47 * two or more systems using a PCI-e links, providing remote memory access.
49 * This module contains a driver for NTB hardware in AMD CPUs
51 * Much of the code in this module is shared with Linux. Any patches may
52 * be picked up and redistributed in Linux with a dual GPL/BSD license.
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
58 #include <sys/param.h>
59 #include <sys/kernel.h>
60 #include <sys/systm.h>
63 #include <sys/malloc.h>
64 #include <sys/module.h>
65 #include <sys/mutex.h>
68 #include <sys/sysctl.h>
73 #include <machine/bus.h>
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcivar.h>
78 #include "ntb_hw_amd.h"
79 #include "dev/ntb/ntb.h"
81 MALLOC_DEFINE(M_AMD_NTB, "amd_ntb_hw", "amd_ntb_hw driver memory allocations");
83 static const struct amd_ntb_hw_info amd_ntb_hw_info_list[] = {
85 { .vendor_id = NTB_HW_AMD_VENDOR_ID,
86 .device_id = NTB_HW_AMD_DEVICE_ID1,
91 .msix_vector_count = 24,
92 .quirks = QUIRK_MW0_32BIT,
93 .desc = "AMD Non-Transparent Bridge"},
95 { .vendor_id = NTB_HW_AMD_VENDOR_ID,
96 .device_id = NTB_HW_AMD_DEVICE_ID2,
101 .msix_vector_count = 24,
103 .desc = "AMD Non-Transparent Bridge"},
106 static const struct pci_device_table amd_ntb_devs[] = {
107 { PCI_DEV(NTB_HW_AMD_VENDOR_ID, NTB_HW_AMD_DEVICE_ID1),
108 .driver_data = (uintptr_t)&amd_ntb_hw_info_list[0],
109 PCI_DESCR("AMD Non-Transparent Bridge") },
110 { PCI_DEV(NTB_HW_AMD_VENDOR_ID, NTB_HW_AMD_DEVICE_ID2),
111 .driver_data = (uintptr_t)&amd_ntb_hw_info_list[1],
112 PCI_DESCR("AMD Non-Transparent Bridge") }
115 static unsigned g_amd_ntb_hw_debug_level;
116 SYSCTL_UINT(_hw_ntb, OID_AUTO, debug_level, CTLFLAG_RWTUN,
117 &g_amd_ntb_hw_debug_level, 0, "amd_ntb_hw log level -- higher is verbose");
119 #define amd_ntb_printf(lvl, ...) do { \
120 if (lvl <= g_amd_ntb_hw_debug_level) \
121 device_printf(ntb->device, __VA_ARGS__); \
125 static __inline uint64_t
126 bus_space_read_8(bus_space_tag_t tag, bus_space_handle_t handle,
130 return (bus_space_read_4(tag, handle, offset) |
131 ((uint64_t)bus_space_read_4(tag, handle, offset + 4)) << 32);
135 bus_space_write_8(bus_space_tag_t tag, bus_space_handle_t handle,
136 bus_size_t offset, uint64_t val)
139 bus_space_write_4(tag, handle, offset, val);
140 bus_space_write_4(tag, handle, offset + 4, val >> 32);
145 * AMD NTB INTERFACE ROUTINES
148 amd_ntb_port_number(device_t dev)
150 struct amd_ntb_softc *ntb = device_get_softc(dev);
152 amd_ntb_printf(1, "%s: conn_type %d\n", __func__, ntb->conn_type);
154 switch (ntb->conn_type) {
156 return (NTB_PORT_PRI_USD);
158 return (NTB_PORT_SEC_DSD);
167 amd_ntb_peer_port_count(device_t dev)
169 struct amd_ntb_softc *ntb = device_get_softc(dev);
171 amd_ntb_printf(1, "%s: peer cnt %d\n", __func__, NTB_DEF_PEER_CNT);
172 return (NTB_DEF_PEER_CNT);
176 amd_ntb_peer_port_number(device_t dev, int pidx)
178 struct amd_ntb_softc *ntb = device_get_softc(dev);
180 amd_ntb_printf(1, "%s: pidx %d conn type %d\n",
181 __func__, pidx, ntb->conn_type);
183 if (pidx != NTB_DEF_PEER_IDX)
186 switch (ntb->conn_type) {
188 return (NTB_PORT_SEC_DSD);
190 return (NTB_PORT_PRI_USD);
199 amd_ntb_peer_port_idx(device_t dev, int port)
201 struct amd_ntb_softc *ntb = device_get_softc(dev);
204 peer_port = amd_ntb_peer_port_number(dev, NTB_DEF_PEER_IDX);
206 amd_ntb_printf(1, "%s: port %d peer_port %d\n",
207 __func__, port, peer_port);
209 if (peer_port == -EINVAL || port != peer_port)
216 * AMD NTB INTERFACE - LINK ROUTINES
219 amd_link_is_up(struct amd_ntb_softc *ntb)
222 amd_ntb_printf(2, "%s: peer_sta 0x%x cntl_sta 0x%x\n",
223 __func__, ntb->peer_sta, ntb->cntl_sta);
226 return (NTB_LNK_STA_ACTIVE(ntb->cntl_sta));
231 static inline enum ntb_speed
232 amd_ntb_link_sta_speed(struct amd_ntb_softc *ntb)
235 if (!amd_link_is_up(ntb))
236 return (NTB_SPEED_NONE);
238 return (NTB_LNK_STA_SPEED(ntb->lnk_sta));
241 static inline enum ntb_width
242 amd_ntb_link_sta_width(struct amd_ntb_softc *ntb)
245 if (!amd_link_is_up(ntb))
246 return (NTB_WIDTH_NONE);
248 return (NTB_LNK_STA_WIDTH(ntb->lnk_sta));
252 amd_ntb_link_is_up(device_t dev, enum ntb_speed *speed, enum ntb_width *width)
254 struct amd_ntb_softc *ntb = device_get_softc(dev);
257 *speed = amd_ntb_link_sta_speed(ntb);
259 *width = amd_ntb_link_sta_width(ntb);
261 return (amd_link_is_up(ntb));
265 amd_ntb_link_enable(device_t dev, enum ntb_speed max_speed,
266 enum ntb_width max_width)
268 struct amd_ntb_softc *ntb = device_get_softc(dev);
271 amd_ntb_printf(1, "%s: int_mask 0x%x conn_type %d\n",
272 __func__, ntb->int_mask, ntb->conn_type);
274 amd_init_side_info(ntb);
276 /* Enable event interrupt */
277 ntb->int_mask &= ~AMD_EVENT_INTMASK;
278 amd_ntb_reg_write(4, AMD_INTMASK_OFFSET, ntb->int_mask);
280 if (ntb->conn_type == NTB_CONN_SEC)
283 amd_ntb_printf(0, "%s: Enabling Link.\n", __func__);
285 ntb_ctl = amd_ntb_reg_read(4, AMD_CNTL_OFFSET);
286 ntb_ctl |= (PMM_REG_CTL | SMM_REG_CTL);
287 amd_ntb_printf(1, "%s: ntb_ctl 0x%x\n", __func__, ntb_ctl);
288 amd_ntb_reg_write(4, AMD_CNTL_OFFSET, ntb_ctl);
294 amd_ntb_link_disable(device_t dev)
296 struct amd_ntb_softc *ntb = device_get_softc(dev);
299 amd_ntb_printf(1, "%s: int_mask 0x%x conn_type %d\n",
300 __func__, ntb->int_mask, ntb->conn_type);
302 amd_deinit_side_info(ntb);
304 /* Disable event interrupt */
305 ntb->int_mask |= AMD_EVENT_INTMASK;
306 amd_ntb_reg_write(4, AMD_INTMASK_OFFSET, ntb->int_mask);
308 if (ntb->conn_type == NTB_CONN_SEC)
311 amd_ntb_printf(0, "%s: Disabling Link.\n", __func__);
313 ntb_ctl = amd_ntb_reg_read(4, AMD_CNTL_OFFSET);
314 ntb_ctl &= ~(PMM_REG_CTL | SMM_REG_CTL);
315 amd_ntb_printf(1, "%s: ntb_ctl 0x%x\n", __func__, ntb_ctl);
316 amd_ntb_reg_write(4, AMD_CNTL_OFFSET, ntb_ctl);
322 * AMD NTB memory window routines
325 amd_ntb_mw_count(device_t dev)
327 struct amd_ntb_softc *ntb = device_get_softc(dev);
329 return (ntb->hw_info->mw_count);
333 amd_ntb_mw_get_range(device_t dev, unsigned mw_idx, vm_paddr_t *base,
334 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
337 struct amd_ntb_softc *ntb = device_get_softc(dev);
338 struct amd_ntb_pci_bar_info *bar_info;
340 if (mw_idx < 0 || mw_idx >= ntb->hw_info->mw_count)
343 bar_info = &ntb->bar_info[ntb->hw_info->bar_start_idx + mw_idx];
346 *base = bar_info->pbase;
349 *vbase = bar_info->vbase;
352 *align = bar_info->size;
355 *size = bar_info->size;
357 if (align_size != NULL)
360 if (plimit != NULL) {
362 * For Device ID 0x145B (which has 3 memory windows),
363 * memory window 0 use a 32-bit bar. The remaining
364 * cases all use 64-bit bar.
366 if ((mw_idx == 0) && (ntb->hw_info->quirks & QUIRK_MW0_32BIT))
367 *plimit = BUS_SPACE_MAXADDR_32BIT;
369 *plimit = BUS_SPACE_MAXADDR;
376 amd_ntb_mw_set_trans(device_t dev, unsigned mw_idx, bus_addr_t addr, size_t size)
378 struct amd_ntb_softc *ntb = device_get_softc(dev);
379 struct amd_ntb_pci_bar_info *bar_info;
381 if (mw_idx < 0 || mw_idx >= ntb->hw_info->mw_count)
384 bar_info = &ntb->bar_info[ntb->hw_info->bar_start_idx + mw_idx];
386 /* Make sure the range fits in the usable mw size. */
387 if (size > bar_info->size) {
388 amd_ntb_printf(0, "%s: size 0x%jx greater than mw_size 0x%jx\n",
389 __func__, (uintmax_t)size, (uintmax_t)bar_info->size);
393 amd_ntb_printf(1, "%s: mw %d mw_size 0x%jx size 0x%jx base %p\n",
394 __func__, mw_idx, (uintmax_t)bar_info->size,
395 (uintmax_t)size, (void *)bar_info->pci_bus_handle);
398 * AMD NTB XLAT and Limit registers needs to be written only after
401 * Set and verify setting the translation address register.
403 amd_ntb_peer_reg_write(8, bar_info->xlat_off, (uint64_t)addr);
404 amd_ntb_printf(0, "%s: mw %d xlat_off 0x%x cur_val 0x%jx addr %p\n",
405 __func__, mw_idx, bar_info->xlat_off,
406 amd_ntb_peer_reg_read(8, bar_info->xlat_off), (void *)addr);
409 * Set and verify setting the limit register.
411 * For Device ID 0x145B (which has 3 memory windows),
412 * memory window 0 use a 32-bit bar. The remaining
413 * cases all use 64-bit bar.
415 if ((mw_idx == 0) && (ntb->hw_info->quirks & QUIRK_MW0_32BIT)) {
416 amd_ntb_reg_write(4, bar_info->limit_off, (uint32_t)size);
417 amd_ntb_printf(1, "%s: limit_off 0x%x cur_val 0x%x limit 0x%x\n",
418 __func__, bar_info->limit_off,
419 amd_ntb_peer_reg_read(4, bar_info->limit_off),
422 amd_ntb_reg_write(8, bar_info->limit_off, (uint64_t)size);
423 amd_ntb_printf(1, "%s: limit_off 0x%x cur_val 0x%jx limit 0x%jx\n",
424 __func__, bar_info->limit_off,
425 amd_ntb_peer_reg_read(8, bar_info->limit_off),
433 amd_ntb_mw_clear_trans(device_t dev, unsigned mw_idx)
435 struct amd_ntb_softc *ntb = device_get_softc(dev);
437 amd_ntb_printf(1, "%s: mw_idx %d\n", __func__, mw_idx);
439 if (mw_idx < 0 || mw_idx >= ntb->hw_info->mw_count)
442 return (amd_ntb_mw_set_trans(dev, mw_idx, 0, 0));
446 amd_ntb_mw_set_wc(device_t dev, unsigned int mw_idx, vm_memattr_t mode)
448 struct amd_ntb_softc *ntb = device_get_softc(dev);
449 struct amd_ntb_pci_bar_info *bar_info;
452 if (mw_idx < 0 || mw_idx >= ntb->hw_info->mw_count)
455 bar_info = &ntb->bar_info[ntb->hw_info->bar_start_idx + mw_idx];
456 if (mode == bar_info->map_mode)
459 rc = pmap_change_attr((vm_offset_t)bar_info->vbase, bar_info->size, mode);
461 bar_info->map_mode = mode;
467 amd_ntb_mw_get_wc(device_t dev, unsigned mw_idx, vm_memattr_t *mode)
469 struct amd_ntb_softc *ntb = device_get_softc(dev);
470 struct amd_ntb_pci_bar_info *bar_info;
472 amd_ntb_printf(1, "%s: mw_idx %d\n", __func__, mw_idx);
474 if (mw_idx < 0 || mw_idx >= ntb->hw_info->mw_count)
477 bar_info = &ntb->bar_info[ntb->hw_info->bar_start_idx + mw_idx];
478 *mode = bar_info->map_mode;
484 * AMD NTB doorbell routines
487 amd_ntb_db_vector_count(device_t dev)
489 struct amd_ntb_softc *ntb = device_get_softc(dev);
491 amd_ntb_printf(1, "%s: db_count 0x%x\n", __func__,
492 ntb->hw_info->db_count);
494 return (ntb->hw_info->db_count);
498 amd_ntb_db_valid_mask(device_t dev)
500 struct amd_ntb_softc *ntb = device_get_softc(dev);
502 amd_ntb_printf(1, "%s: db_valid_mask 0x%x\n",
503 __func__, ntb->db_valid_mask);
505 return (ntb->db_valid_mask);
509 amd_ntb_db_vector_mask(device_t dev, uint32_t vector)
511 struct amd_ntb_softc *ntb = device_get_softc(dev);
513 amd_ntb_printf(1, "%s: vector %d db_count 0x%x db_valid_mask 0x%x\n",
514 __func__, vector, ntb->hw_info->db_count, ntb->db_valid_mask);
516 if (vector < 0 || vector >= ntb->hw_info->db_count)
519 return (ntb->db_valid_mask & (1 << vector));
523 amd_ntb_db_read(device_t dev)
525 struct amd_ntb_softc *ntb = device_get_softc(dev);
528 dbstat_off = (uint64_t)amd_ntb_reg_read(2, AMD_DBSTAT_OFFSET);
530 amd_ntb_printf(1, "%s: dbstat_off 0x%jx\n", __func__, dbstat_off);
536 amd_ntb_db_clear(device_t dev, uint64_t db_bits)
538 struct amd_ntb_softc *ntb = device_get_softc(dev);
540 amd_ntb_printf(1, "%s: db_bits 0x%jx\n", __func__, db_bits);
541 amd_ntb_reg_write(2, AMD_DBSTAT_OFFSET, (uint16_t)db_bits);
545 amd_ntb_db_set_mask(device_t dev, uint64_t db_bits)
547 struct amd_ntb_softc *ntb = device_get_softc(dev);
550 amd_ntb_printf(1, "%s: db_mask 0x%x db_bits 0x%jx\n",
551 __func__, ntb->db_mask, db_bits);
553 ntb->db_mask |= db_bits;
554 amd_ntb_reg_write(2, AMD_DBMASK_OFFSET, ntb->db_mask);
559 amd_ntb_db_clear_mask(device_t dev, uint64_t db_bits)
561 struct amd_ntb_softc *ntb = device_get_softc(dev);
564 amd_ntb_printf(1, "%s: db_mask 0x%x db_bits 0x%jx\n",
565 __func__, ntb->db_mask, db_bits);
567 ntb->db_mask &= ~db_bits;
568 amd_ntb_reg_write(2, AMD_DBMASK_OFFSET, ntb->db_mask);
573 amd_ntb_peer_db_set(device_t dev, uint64_t db_bits)
575 struct amd_ntb_softc *ntb = device_get_softc(dev);
577 amd_ntb_printf(1, "%s: db_bits 0x%jx\n", __func__, db_bits);
578 amd_ntb_reg_write(2, AMD_DBREQ_OFFSET, (uint16_t)db_bits);
582 * AMD NTB scratchpad routines
585 amd_ntb_spad_count(device_t dev)
587 struct amd_ntb_softc *ntb = device_get_softc(dev);
589 amd_ntb_printf(1, "%s: spad_count 0x%x\n", __func__,
592 return (ntb->spad_count);
596 amd_ntb_spad_read(device_t dev, unsigned int idx, uint32_t *val)
598 struct amd_ntb_softc *ntb = device_get_softc(dev);
601 amd_ntb_printf(2, "%s: idx %d\n", __func__, idx);
603 if (idx < 0 || idx >= ntb->spad_count)
606 offset = ntb->self_spad + (idx << 2);
607 *val = amd_ntb_reg_read(4, AMD_SPAD_OFFSET + offset);
608 amd_ntb_printf(2, "%s: offset 0x%x val 0x%x\n", __func__, offset, *val);
614 amd_ntb_spad_write(device_t dev, unsigned int idx, uint32_t val)
616 struct amd_ntb_softc *ntb = device_get_softc(dev);
619 amd_ntb_printf(2, "%s: idx %d\n", __func__, idx);
621 if (idx < 0 || idx >= ntb->spad_count)
624 offset = ntb->self_spad + (idx << 2);
625 amd_ntb_reg_write(4, AMD_SPAD_OFFSET + offset, val);
626 amd_ntb_printf(2, "%s: offset 0x%x val 0x%x\n", __func__, offset, val);
632 amd_ntb_spad_clear(struct amd_ntb_softc *ntb)
636 for (i = 0; i < ntb->spad_count; i++)
637 amd_ntb_spad_write(ntb->device, i, 0);
641 amd_ntb_peer_spad_read(device_t dev, unsigned int idx, uint32_t *val)
643 struct amd_ntb_softc *ntb = device_get_softc(dev);
646 amd_ntb_printf(2, "%s: idx %d\n", __func__, idx);
648 if (idx < 0 || idx >= ntb->spad_count)
651 offset = ntb->peer_spad + (idx << 2);
652 *val = amd_ntb_reg_read(4, AMD_SPAD_OFFSET + offset);
653 amd_ntb_printf(2, "%s: offset 0x%x val 0x%x\n", __func__, offset, *val);
659 amd_ntb_peer_spad_write(device_t dev, unsigned int idx, uint32_t val)
661 struct amd_ntb_softc *ntb = device_get_softc(dev);
664 amd_ntb_printf(2, "%s: idx %d\n", __func__, idx);
666 if (idx < 0 || idx >= ntb->spad_count)
669 offset = ntb->peer_spad + (idx << 2);
670 amd_ntb_reg_write(4, AMD_SPAD_OFFSET + offset, val);
671 amd_ntb_printf(2, "%s: offset 0x%x val 0x%x\n", __func__, offset, val);
681 amd_ntb_hw_info_handler(SYSCTL_HANDLER_ARGS)
683 struct amd_ntb_softc* ntb = arg1;
687 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
689 return (sb->s_error);
691 sbuf_printf(sb, "NTB AMD Hardware info:\n\n");
692 sbuf_printf(sb, "AMD NTB side: %s\n",
693 (ntb->conn_type == NTB_CONN_PRI)? "PRIMARY" : "SECONDARY");
694 sbuf_printf(sb, "AMD LNK STA: 0x%#06x\n", ntb->lnk_sta);
696 if (!amd_link_is_up(ntb))
697 sbuf_printf(sb, "AMD Link Status: Down\n");
699 sbuf_printf(sb, "AMD Link Status: Up\n");
700 sbuf_printf(sb, "AMD Link Speed: PCI-E Gen %u\n",
701 NTB_LNK_STA_SPEED(ntb->lnk_sta));
702 sbuf_printf(sb, "AMD Link Width: PCI-E Width %u\n",
703 NTB_LNK_STA_WIDTH(ntb->lnk_sta));
706 sbuf_printf(sb, "AMD Memory window count: %d\n",
707 ntb->hw_info->mw_count);
708 sbuf_printf(sb, "AMD Spad count: %d\n",
710 sbuf_printf(sb, "AMD Doorbell count: %d\n",
711 ntb->hw_info->db_count);
712 sbuf_printf(sb, "AMD MSI-X vec count: %d\n\n",
713 ntb->msix_vec_count);
714 sbuf_printf(sb, "AMD Doorbell valid mask: 0x%x\n",
716 sbuf_printf(sb, "AMD Doorbell Mask: 0x%x\n",
717 amd_ntb_reg_read(4, AMD_DBMASK_OFFSET));
718 sbuf_printf(sb, "AMD Doorbell: 0x%x\n",
719 amd_ntb_reg_read(4, AMD_DBSTAT_OFFSET));
720 sbuf_printf(sb, "AMD NTB Incoming XLAT: \n");
721 sbuf_printf(sb, "AMD XLAT1: 0x%jx\n",
722 amd_ntb_peer_reg_read(8, AMD_BAR1XLAT_OFFSET));
723 sbuf_printf(sb, "AMD XLAT23: 0x%jx\n",
724 amd_ntb_peer_reg_read(8, AMD_BAR23XLAT_OFFSET));
725 sbuf_printf(sb, "AMD XLAT45: 0x%jx\n",
726 amd_ntb_peer_reg_read(8, AMD_BAR45XLAT_OFFSET));
727 sbuf_printf(sb, "AMD LMT1: 0x%x\n",
728 amd_ntb_reg_read(4, AMD_BAR1LMT_OFFSET));
729 sbuf_printf(sb, "AMD LMT23: 0x%jx\n",
730 amd_ntb_reg_read(8, AMD_BAR23LMT_OFFSET));
731 sbuf_printf(sb, "AMD LMT45: 0x%jx\n",
732 amd_ntb_reg_read(8, AMD_BAR45LMT_OFFSET));
734 rc = sbuf_finish(sb);
740 amd_ntb_sysctl_init(struct amd_ntb_softc *ntb)
742 struct sysctl_oid_list *globals;
743 struct sysctl_ctx_list *ctx;
745 ctx = device_get_sysctl_ctx(ntb->device);
746 globals = SYSCTL_CHILDREN(device_get_sysctl_tree(ntb->device));
748 SYSCTL_ADD_PROC(ctx, globals, OID_AUTO, "info",
749 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ntb, 0,
750 amd_ntb_hw_info_handler, "A", "AMD NTB HW Information");
754 * Polls the HW link status register(s); returns true if something has changed.
757 amd_ntb_poll_link(struct amd_ntb_softc *ntb)
759 uint32_t fullreg, reg, stat;
761 fullreg = amd_ntb_peer_reg_read(4, AMD_SIDEINFO_OFFSET);
762 reg = fullreg & NTB_LIN_STA_ACTIVE_BIT;
764 if (reg == ntb->cntl_sta)
767 amd_ntb_printf(0, "%s: SIDEINFO reg_val = 0x%x cntl_sta 0x%x\n",
768 __func__, fullreg, ntb->cntl_sta);
772 stat = pci_read_config(ntb->device, AMD_LINK_STATUS_OFFSET, 4);
774 amd_ntb_printf(0, "%s: LINK_STATUS stat = 0x%x lnk_sta 0x%x.\n",
775 __func__, stat, ntb->lnk_sta);
783 amd_link_hb(void *arg)
785 struct amd_ntb_softc *ntb = arg;
787 if (amd_ntb_poll_link(ntb))
788 ntb_link_event(ntb->device);
790 if (!amd_link_is_up(ntb)) {
791 callout_reset(&ntb->hb_timer, AMD_LINK_HB_TIMEOUT,
794 callout_reset(&ntb->hb_timer, (AMD_LINK_HB_TIMEOUT * 10),
800 amd_ntb_interrupt(struct amd_ntb_softc *ntb, uint16_t vec)
802 if (vec < ntb->hw_info->db_count)
803 ntb_db_event(ntb->device, vec);
805 amd_ntb_printf(0, "Invalid vector %d\n", vec);
809 amd_ntb_vec_isr(void *arg)
811 struct amd_ntb_vec *nvec = arg;
813 amd_ntb_interrupt(nvec->ntb, nvec->num);
817 amd_ntb_irq_isr(void *arg)
819 /* If we couldn't set up MSI-X, we only have the one vector. */
820 amd_ntb_interrupt(arg, 0);
824 amd_init_side_info(struct amd_ntb_softc *ntb)
828 reg = amd_ntb_reg_read(4, AMD_SIDEINFO_OFFSET);
829 if (!(reg & AMD_SIDE_READY)) {
830 reg |= AMD_SIDE_READY;
831 amd_ntb_reg_write(4, AMD_SIDEINFO_OFFSET, reg);
833 reg = amd_ntb_reg_read(4, AMD_SIDEINFO_OFFSET);
837 amd_deinit_side_info(struct amd_ntb_softc *ntb)
841 reg = amd_ntb_reg_read(4, AMD_SIDEINFO_OFFSET);
842 if (reg & AMD_SIDE_READY) {
843 reg &= ~AMD_SIDE_READY;
844 amd_ntb_reg_write(4, AMD_SIDEINFO_OFFSET, reg);
845 amd_ntb_reg_read(4, AMD_SIDEINFO_OFFSET);
850 amd_ntb_setup_isr(struct amd_ntb_softc *ntb, uint16_t num_vectors, bool msi,
854 int flags = 0, rc = 0;
858 flags |= RF_SHAREABLE;
860 for (i = 0; i < num_vectors; i++) {
862 /* RID should be 0 for intx */
864 ntb->int_info[i].rid = i;
866 ntb->int_info[i].rid = i + 1;
868 ntb->int_info[i].res = bus_alloc_resource_any(ntb->device,
869 SYS_RES_IRQ, &ntb->int_info[i].rid, flags);
870 if (ntb->int_info[i].res == NULL) {
871 amd_ntb_printf(0, "bus_alloc_resource IRQ failed\n");
875 ntb->int_info[i].tag = NULL;
876 ntb->allocated_interrupts++;
879 rc = bus_setup_intr(ntb->device, ntb->int_info[i].res,
880 INTR_MPSAFE | INTR_TYPE_MISC, NULL, amd_ntb_irq_isr,
881 ntb, &ntb->int_info[i].tag);
883 rc = bus_setup_intr(ntb->device, ntb->int_info[i].res,
884 INTR_MPSAFE | INTR_TYPE_MISC, NULL, amd_ntb_vec_isr,
885 &ntb->msix_vec[i], &ntb->int_info[i].tag);
889 amd_ntb_printf(0, "bus_setup_intr %d failed\n", i);
898 amd_ntb_create_msix_vec(struct amd_ntb_softc *ntb, uint32_t max_vectors)
902 ntb->msix_vec = malloc(max_vectors * sizeof(*ntb->msix_vec), M_AMD_NTB,
905 for (i = 0; i < max_vectors; i++) {
906 ntb->msix_vec[i].num = i;
907 ntb->msix_vec[i].ntb = ntb;
914 amd_ntb_free_msix_vec(struct amd_ntb_softc *ntb)
916 if (ntb->msix_vec_count) {
917 pci_release_msi(ntb->device);
918 ntb->msix_vec_count = 0;
921 if (ntb->msix_vec != NULL) {
922 free(ntb->msix_vec, M_AMD_NTB);
923 ntb->msix_vec = NULL;
928 amd_ntb_init_isr(struct amd_ntb_softc *ntb)
930 uint32_t supported_vectors, num_vectors;
931 bool msi = false, intx = false;
934 ntb->db_mask = ntb->db_valid_mask;
936 rc = amd_ntb_create_msix_vec(ntb, ntb->hw_info->msix_vector_count);
938 amd_ntb_printf(0, "Error creating msix vectors: %d\n", rc);
943 * Check the number of MSI-X message supported by the device.
944 * Minimum necessary MSI-X message count should be equal to db_count.
946 supported_vectors = pci_msix_count(ntb->device);
947 num_vectors = MIN(supported_vectors, ntb->hw_info->db_count);
948 if (num_vectors < ntb->hw_info->db_count) {
949 amd_ntb_printf(0, "No minimum msix: supported %d db %d\n",
950 supported_vectors, ntb->hw_info->db_count);
952 goto err_msix_enable;
955 /* Allocate the necessary number of MSI-x messages */
956 rc = pci_alloc_msix(ntb->device, &num_vectors);
958 amd_ntb_printf(0, "Error allocating msix vectors: %d\n", rc);
960 goto err_msix_enable;
963 if (num_vectors < ntb->hw_info->db_count) {
964 amd_ntb_printf(0, "Allocated only %d MSI-X\n", num_vectors);
967 * Else set ntb->hw_info->db_count = ntb->msix_vec_count =
968 * num_vectors, msi=false and dont release msi.
975 free(ntb->msix_vec, M_AMD_NTB);
976 ntb->msix_vec = NULL;
977 pci_release_msi(ntb->device);
979 rc = pci_alloc_msi(ntb->device, &num_vectors);
981 amd_ntb_printf(0, "Error allocating msix vectors: %d\n", rc);
987 ntb->hw_info->db_count = ntb->msix_vec_count = num_vectors;
991 ntb->hw_info->db_count = 1;
992 ntb->msix_vec_count = 0;
995 amd_ntb_printf(0, "%s: db %d msix %d msi %d intx %d\n",
996 __func__, ntb->hw_info->db_count, ntb->msix_vec_count, (int)msi, (int)intx);
998 rc = amd_ntb_setup_isr(ntb, num_vectors, msi, intx);
1000 amd_ntb_printf(0, "Error setting up isr: %d\n", rc);
1001 amd_ntb_free_msix_vec(ntb);
1008 amd_ntb_deinit_isr(struct amd_ntb_softc *ntb)
1010 struct amd_ntb_int_info *current_int;
1013 /* Mask all doorbell interrupts */
1014 ntb->db_mask = ntb->db_valid_mask;
1015 amd_ntb_reg_write(4, AMD_DBMASK_OFFSET, ntb->db_mask);
1017 for (i = 0; i < ntb->allocated_interrupts; i++) {
1018 current_int = &ntb->int_info[i];
1019 if (current_int->tag != NULL)
1020 bus_teardown_intr(ntb->device, current_int->res,
1023 if (current_int->res != NULL)
1024 bus_release_resource(ntb->device, SYS_RES_IRQ,
1025 rman_get_rid(current_int->res), current_int->res);
1028 amd_ntb_free_msix_vec(ntb);
1031 static enum amd_ntb_conn_type
1032 amd_ntb_get_topo(struct amd_ntb_softc *ntb)
1036 info = amd_ntb_reg_read(4, AMD_SIDEINFO_OFFSET);
1038 if (info & AMD_SIDE_MASK)
1039 return (NTB_CONN_SEC);
1041 return (NTB_CONN_PRI);
1045 amd_ntb_init_dev(struct amd_ntb_softc *ntb)
1047 ntb->db_valid_mask = (1ull << ntb->hw_info->db_count) - 1;
1048 mtx_init(&ntb->db_mask_lock, "amd ntb db bits", NULL, MTX_SPIN);
1050 switch (ntb->conn_type) {
1053 ntb->spad_count >>= 1;
1055 if (ntb->conn_type == NTB_CONN_PRI) {
1057 ntb->peer_spad = 0x20;
1059 ntb->self_spad = 0x20;
1063 callout_init(&ntb->hb_timer, 1);
1064 callout_reset(&ntb->hb_timer, AMD_LINK_HB_TIMEOUT,
1070 amd_ntb_printf(0, "Unsupported AMD NTB topology %d\n",
1075 ntb->int_mask = AMD_EVENT_INTMASK;
1076 amd_ntb_reg_write(4, AMD_INTMASK_OFFSET, ntb->int_mask);
1082 amd_ntb_init(struct amd_ntb_softc *ntb)
1086 ntb->conn_type = amd_ntb_get_topo(ntb);
1087 amd_ntb_printf(0, "AMD NTB Side: %s\n",
1088 (ntb->conn_type == NTB_CONN_PRI)? "PRIMARY" : "SECONDARY");
1090 rc = amd_ntb_init_dev(ntb);
1094 rc = amd_ntb_init_isr(ntb);
1102 print_map_success(struct amd_ntb_softc *ntb, struct amd_ntb_pci_bar_info *bar,
1105 amd_ntb_printf(0, "Mapped BAR%d v:[%p-%p] p:[%p-%p] (0x%jx bytes) (%s)\n",
1106 PCI_RID2BAR(bar->pci_resource_id), bar->vbase,
1107 (char *)bar->vbase + bar->size - 1, (void *)bar->pbase,
1108 (void *)(bar->pbase + bar->size - 1), (uintmax_t)bar->size, kind);
1112 save_bar_parameters(struct amd_ntb_pci_bar_info *bar)
1114 bar->pci_bus_tag = rman_get_bustag(bar->pci_resource);
1115 bar->pci_bus_handle = rman_get_bushandle(bar->pci_resource);
1116 bar->pbase = rman_get_start(bar->pci_resource);
1117 bar->size = rman_get_size(bar->pci_resource);
1118 bar->vbase = rman_get_virtual(bar->pci_resource);
1119 bar->map_mode = VM_MEMATTR_UNCACHEABLE;
1123 map_bar(struct amd_ntb_softc *ntb, struct amd_ntb_pci_bar_info *bar)
1125 bar->pci_resource = bus_alloc_resource_any(ntb->device, SYS_RES_MEMORY,
1126 &bar->pci_resource_id, RF_ACTIVE);
1127 if (bar->pci_resource == NULL)
1130 save_bar_parameters(bar);
1131 print_map_success(ntb, bar, "mmr");
1137 amd_ntb_map_pci_bars(struct amd_ntb_softc *ntb)
1141 /* NTB Config/Control registers - BAR 0 */
1142 ntb->bar_info[NTB_CONFIG_BAR].pci_resource_id = PCIR_BAR(0);
1143 rc = map_bar(ntb, &ntb->bar_info[NTB_CONFIG_BAR]);
1147 /* Memory Window 0 BAR - BAR 1 */
1148 ntb->bar_info[NTB_BAR_1].pci_resource_id = PCIR_BAR(1);
1149 rc = map_bar(ntb, &ntb->bar_info[NTB_BAR_1]);
1152 ntb->bar_info[NTB_BAR_1].xlat_off = AMD_BAR1XLAT_OFFSET;
1153 ntb->bar_info[NTB_BAR_1].limit_off = AMD_BAR1LMT_OFFSET;
1155 /* Memory Window 1 BAR - BAR 2&3 */
1156 ntb->bar_info[NTB_BAR_2].pci_resource_id = PCIR_BAR(2);
1157 rc = map_bar(ntb, &ntb->bar_info[NTB_BAR_2]);
1160 ntb->bar_info[NTB_BAR_2].xlat_off = AMD_BAR23XLAT_OFFSET;
1161 ntb->bar_info[NTB_BAR_2].limit_off = AMD_BAR23LMT_OFFSET;
1163 /* Memory Window 2 BAR - BAR 4&5 */
1164 ntb->bar_info[NTB_BAR_3].pci_resource_id = PCIR_BAR(4);
1165 rc = map_bar(ntb, &ntb->bar_info[NTB_BAR_3]);
1168 ntb->bar_info[NTB_BAR_3].xlat_off = AMD_BAR45XLAT_OFFSET;
1169 ntb->bar_info[NTB_BAR_3].limit_off = AMD_BAR45LMT_OFFSET;
1173 amd_ntb_printf(0, "unable to allocate pci resource\n");
1179 amd_ntb_unmap_pci_bars(struct amd_ntb_softc *ntb)
1181 struct amd_ntb_pci_bar_info *bar_info;
1184 for (i = 0; i < NTB_MAX_BARS; i++) {
1185 bar_info = &ntb->bar_info[i];
1186 if (bar_info->pci_resource != NULL)
1187 bus_release_resource(ntb->device, SYS_RES_MEMORY,
1188 bar_info->pci_resource_id, bar_info->pci_resource);
1193 amd_ntb_probe(device_t device)
1195 struct amd_ntb_softc *ntb = device_get_softc(device);
1196 const struct pci_device_table *tbl;
1198 tbl = PCI_MATCH(device, amd_ntb_devs);
1202 ntb->hw_info = (struct amd_ntb_hw_info *)tbl->driver_data;
1203 ntb->spad_count = ntb->hw_info->spad_count;
1204 device_set_desc(device, tbl->descr);
1206 return (BUS_PROBE_GENERIC);
1210 amd_ntb_attach(device_t device)
1212 struct amd_ntb_softc *ntb = device_get_softc(device);
1215 ntb->device = device;
1217 /* Enable PCI bus mastering for "device" */
1218 pci_enable_busmaster(ntb->device);
1220 error = amd_ntb_map_pci_bars(ntb);
1224 error = amd_ntb_init(ntb);
1228 amd_init_side_info(ntb);
1230 amd_ntb_spad_clear(ntb);
1232 amd_ntb_sysctl_init(ntb);
1234 /* Attach children to this controller */
1235 error = ntb_register_device(device);
1239 amd_ntb_detach(device);
1245 amd_ntb_detach(device_t device)
1247 struct amd_ntb_softc *ntb = device_get_softc(device);
1249 ntb_unregister_device(device);
1250 amd_deinit_side_info(ntb);
1251 callout_drain(&ntb->hb_timer);
1252 amd_ntb_deinit_isr(ntb);
1253 mtx_destroy(&ntb->db_mask_lock);
1254 pci_disable_busmaster(ntb->device);
1255 amd_ntb_unmap_pci_bars(ntb);
1260 static device_method_t ntb_amd_methods[] = {
1261 /* Device interface */
1262 DEVMETHOD(device_probe, amd_ntb_probe),
1263 DEVMETHOD(device_attach, amd_ntb_attach),
1264 DEVMETHOD(device_detach, amd_ntb_detach),
1267 DEVMETHOD(bus_child_location_str, ntb_child_location_str),
1268 DEVMETHOD(bus_print_child, ntb_print_child),
1269 DEVMETHOD(bus_get_dma_tag, ntb_get_dma_tag),
1272 DEVMETHOD(ntb_port_number, amd_ntb_port_number),
1273 DEVMETHOD(ntb_peer_port_count, amd_ntb_peer_port_count),
1274 DEVMETHOD(ntb_peer_port_number, amd_ntb_peer_port_number),
1275 DEVMETHOD(ntb_peer_port_idx, amd_ntb_peer_port_idx),
1276 DEVMETHOD(ntb_link_is_up, amd_ntb_link_is_up),
1277 DEVMETHOD(ntb_link_enable, amd_ntb_link_enable),
1278 DEVMETHOD(ntb_link_disable, amd_ntb_link_disable),
1279 DEVMETHOD(ntb_mw_count, amd_ntb_mw_count),
1280 DEVMETHOD(ntb_mw_get_range, amd_ntb_mw_get_range),
1281 DEVMETHOD(ntb_mw_set_trans, amd_ntb_mw_set_trans),
1282 DEVMETHOD(ntb_mw_clear_trans, amd_ntb_mw_clear_trans),
1283 DEVMETHOD(ntb_mw_set_wc, amd_ntb_mw_set_wc),
1284 DEVMETHOD(ntb_mw_get_wc, amd_ntb_mw_get_wc),
1285 DEVMETHOD(ntb_db_valid_mask, amd_ntb_db_valid_mask),
1286 DEVMETHOD(ntb_db_vector_count, amd_ntb_db_vector_count),
1287 DEVMETHOD(ntb_db_vector_mask, amd_ntb_db_vector_mask),
1288 DEVMETHOD(ntb_db_read, amd_ntb_db_read),
1289 DEVMETHOD(ntb_db_clear, amd_ntb_db_clear),
1290 DEVMETHOD(ntb_db_set_mask, amd_ntb_db_set_mask),
1291 DEVMETHOD(ntb_db_clear_mask, amd_ntb_db_clear_mask),
1292 DEVMETHOD(ntb_peer_db_set, amd_ntb_peer_db_set),
1293 DEVMETHOD(ntb_spad_count, amd_ntb_spad_count),
1294 DEVMETHOD(ntb_spad_read, amd_ntb_spad_read),
1295 DEVMETHOD(ntb_spad_write, amd_ntb_spad_write),
1296 DEVMETHOD(ntb_peer_spad_read, amd_ntb_peer_spad_read),
1297 DEVMETHOD(ntb_peer_spad_write, amd_ntb_peer_spad_write),
1301 static DEFINE_CLASS_0(ntb_hw, ntb_amd_driver, ntb_amd_methods,
1302 sizeof(struct amd_ntb_softc));
1303 DRIVER_MODULE(ntb_hw_amd, pci, ntb_amd_driver, ntb_hw_devclass, NULL, NULL);
1304 MODULE_DEPEND(ntb_hw_amd, ntb, 1, 1, 1);
1305 MODULE_VERSION(ntb_hw_amd, 1);
1306 PCI_PNP_INFO(amd_ntb_devs);