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) 2016 Advanced Micro Devices, Inc. All Rights Reserved.
8 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
16 * Copyright (C) 2016 Advanced Micro Devices, Inc. All Rights Reserved.
17 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
29 * * Neither the name of AMD Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * AMD PCIe NTB Linux driver
47 * Contact Information:
51 #include <linux/debugfs.h>
52 #include <linux/delay.h>
53 #include <linux/init.h>
54 #include <linux/interrupt.h>
55 #include <linux/module.h>
56 #include <linux/acpi.h>
57 #include <linux/pci.h>
58 #include <linux/random.h>
59 #include <linux/slab.h>
60 #include <linux/ntb.h>
62 #include "ntb_hw_amd.h"
64 #define NTB_NAME "ntb_hw_amd"
65 #define NTB_DESC "AMD(R) PCI-E Non-Transparent Bridge Driver"
68 MODULE_DESCRIPTION(NTB_DESC);
69 MODULE_VERSION(NTB_VER);
70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_AUTHOR("AMD Inc.");
73 static const struct file_operations amd_ntb_debugfs_info;
74 static struct dentry *debugfs_dir;
76 static int ndev_mw_to_bar(struct amd_ntb_dev *ndev, int idx)
78 if (idx < 0 || idx > ndev->mw_count)
81 return ndev->dev_data->mw_idx << idx;
84 static int amd_ntb_mw_count(struct ntb_dev *ntb, int pidx)
86 if (pidx != NTB_DEF_PEER_IDX)
89 return ntb_ndev(ntb)->mw_count;
92 static int amd_ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int idx,
93 resource_size_t *addr_align,
94 resource_size_t *size_align,
95 resource_size_t *size_max)
97 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
100 if (pidx != NTB_DEF_PEER_IDX)
103 bar = ndev_mw_to_bar(ndev, idx);
114 *size_max = pci_resource_len(ndev->ntb.pdev, bar);
119 static int amd_ntb_mw_set_trans(struct ntb_dev *ntb, int pidx, int idx,
120 dma_addr_t addr, resource_size_t size)
122 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
123 unsigned long xlat_reg, limit_reg = 0;
124 resource_size_t mw_size;
125 void __iomem *mmio, *peer_mmio;
126 u64 base_addr, limit, reg_val;
129 if (pidx != NTB_DEF_PEER_IDX)
132 bar = ndev_mw_to_bar(ndev, idx);
136 mw_size = pci_resource_len(ntb->pdev, bar);
138 /* make sure the range fits in the usable mw size */
142 mmio = ndev->self_mmio;
143 peer_mmio = ndev->peer_mmio;
145 base_addr = pci_resource_start(ntb->pdev, bar);
148 xlat_reg = AMD_BAR23XLAT_OFFSET + ((bar - 2) << 2);
149 limit_reg = AMD_BAR23LMT_OFFSET + ((bar - 2) << 2);
151 /* Set the limit if supported */
154 /* set and verify setting the translation address */
155 write64(addr, peer_mmio + xlat_reg);
156 reg_val = read64(peer_mmio + xlat_reg);
157 if (reg_val != addr) {
158 write64(0, peer_mmio + xlat_reg);
162 /* set and verify setting the limit */
163 write64(limit, peer_mmio + limit_reg);
164 reg_val = read64(peer_mmio + limit_reg);
165 if (reg_val != limit) {
166 write64(base_addr, mmio + limit_reg);
167 write64(0, peer_mmio + xlat_reg);
171 xlat_reg = AMD_BAR1XLAT_OFFSET;
172 limit_reg = AMD_BAR1LMT_OFFSET;
174 /* Set the limit if supported */
177 /* set and verify setting the translation address */
178 write64(addr, peer_mmio + xlat_reg);
179 reg_val = read64(peer_mmio + xlat_reg);
180 if (reg_val != addr) {
181 write64(0, peer_mmio + xlat_reg);
185 /* set and verify setting the limit */
186 writel(limit, peer_mmio + limit_reg);
187 reg_val = readl(peer_mmio + limit_reg);
188 if (reg_val != limit) {
189 writel(base_addr, mmio + limit_reg);
190 writel(0, peer_mmio + xlat_reg);
198 static int amd_ntb_get_link_status(struct amd_ntb_dev *ndev)
200 struct pci_dev *pdev = NULL;
201 struct pci_dev *pci_swds = NULL;
202 struct pci_dev *pci_swus = NULL;
206 if (ndev->ntb.topo == NTB_TOPO_SEC) {
207 /* Locate the pointer to Downstream Switch for this device */
208 pci_swds = pci_upstream_bridge(ndev->ntb.pdev);
211 * Locate the pointer to Upstream Switch for
212 * the Downstream Switch.
214 pci_swus = pci_upstream_bridge(pci_swds);
216 rc = pcie_capability_read_dword(pci_swus,
227 } else if (ndev->ntb.topo == NTB_TOPO_PRI) {
229 * For NTB primary, we simply read the Link Status and control
230 * register of the NTB device itself.
232 pdev = ndev->ntb.pdev;
233 rc = pcie_capability_read_dword(pdev, PCI_EXP_LNKCTL, &stat);
237 /* Catch all for everything else */
241 ndev->lnk_sta = stat;
246 static int amd_link_is_up(struct amd_ntb_dev *ndev)
251 * We consider the link to be up under two conditions:
253 * - When a link-up event is received. This is indicated by
254 * AMD_LINK_UP_EVENT set in peer_sta.
255 * - When driver on both sides of the link have been loaded.
256 * This is indicated by bit 1 being set in the peer
259 * This function should return 1 when the latter of the above
260 * two conditions is true.
262 * Now consider the sequence of events - Link-Up event occurs,
263 * then the peer side driver loads. In this case, we would have
264 * received LINK_UP event and bit 1 of peer SIDEINFO is also
265 * set. What happens now if the link goes down? Bit 1 of
266 * peer SIDEINFO remains set, but LINK_DOWN bit is set in
267 * peer_sta. So we should return 0 from this function. Not only
268 * that, we clear bit 1 of peer SIDEINFO to 0, since the peer
269 * side driver did not even get a chance to clear it before
270 * the link went down. This can be the case of surprise link
273 * LINK_UP event will always occur before the peer side driver
274 * gets loaded the very first time. So there can be a case when
275 * the LINK_UP event has occurred, but the peer side driver hasn't
276 * yet loaded. We return 0 in that case.
278 * There is also a special case when the primary side driver is
279 * unloaded and then loaded again. Since there is no change in
280 * the status of NTB secondary in this case, there is no Link-Up
281 * or Link-Down notification received. We recognize this condition
282 * with peer_sta being set to 0.
284 * If bit 1 of peer SIDEINFO register is not set, then we
285 * simply return 0 irrespective of the link up or down status
288 ret = amd_poll_link(ndev);
291 * We need to check the below only for NTB primary. For NTB
292 * secondary, simply checking the result of PSIDE_INFO
293 * register will suffice.
295 if (ndev->ntb.topo == NTB_TOPO_PRI) {
296 if ((ndev->peer_sta & AMD_LINK_UP_EVENT) ||
297 (ndev->peer_sta == 0))
299 else if (ndev->peer_sta & AMD_LINK_DOWN_EVENT) {
300 /* Clear peer sideinfo register */
301 amd_clear_side_info_reg(ndev, true);
305 } else { /* NTB_TOPO_SEC */
313 static u64 amd_ntb_link_is_up(struct ntb_dev *ntb,
314 enum ntb_speed *speed,
315 enum ntb_width *width)
317 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
320 if (amd_link_is_up(ndev)) {
322 *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
324 *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
326 dev_dbg(&ntb->pdev->dev, "link is up.\n");
331 *speed = NTB_SPEED_NONE;
333 *width = NTB_WIDTH_NONE;
335 dev_dbg(&ntb->pdev->dev, "link is down.\n");
341 static int amd_ntb_link_enable(struct ntb_dev *ntb,
342 enum ntb_speed max_speed,
343 enum ntb_width max_width)
345 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
346 void __iomem *mmio = ndev->self_mmio;
348 /* Enable event interrupt */
349 ndev->int_mask &= ~AMD_EVENT_INTMASK;
350 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
352 if (ndev->ntb.topo == NTB_TOPO_SEC)
354 dev_dbg(&ntb->pdev->dev, "Enabling Link.\n");
359 static int amd_ntb_link_disable(struct ntb_dev *ntb)
361 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
362 void __iomem *mmio = ndev->self_mmio;
364 /* Disable event interrupt */
365 ndev->int_mask |= AMD_EVENT_INTMASK;
366 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
368 if (ndev->ntb.topo == NTB_TOPO_SEC)
370 dev_dbg(&ntb->pdev->dev, "Enabling Link.\n");
375 static int amd_ntb_peer_mw_count(struct ntb_dev *ntb)
377 /* The same as for inbound MWs */
378 return ntb_ndev(ntb)->mw_count;
381 static int amd_ntb_peer_mw_get_addr(struct ntb_dev *ntb, int idx,
382 phys_addr_t *base, resource_size_t *size)
384 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
387 bar = ndev_mw_to_bar(ndev, idx);
392 *base = pci_resource_start(ndev->ntb.pdev, bar);
395 *size = pci_resource_len(ndev->ntb.pdev, bar);
400 static u64 amd_ntb_db_valid_mask(struct ntb_dev *ntb)
402 return ntb_ndev(ntb)->db_valid_mask;
405 static int amd_ntb_db_vector_count(struct ntb_dev *ntb)
407 return ntb_ndev(ntb)->db_count;
410 static u64 amd_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
412 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
414 if (db_vector < 0 || db_vector > ndev->db_count)
417 return ntb_ndev(ntb)->db_valid_mask & (1ULL << db_vector);
420 static u64 amd_ntb_db_read(struct ntb_dev *ntb)
422 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
423 void __iomem *mmio = ndev->self_mmio;
425 return (u64)readw(mmio + AMD_DBSTAT_OFFSET);
428 static int amd_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
430 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
431 void __iomem *mmio = ndev->self_mmio;
433 writew((u16)db_bits, mmio + AMD_DBSTAT_OFFSET);
438 static int amd_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
440 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
441 void __iomem *mmio = ndev->self_mmio;
444 if (db_bits & ~ndev->db_valid_mask)
447 spin_lock_irqsave(&ndev->db_mask_lock, flags);
448 ndev->db_mask |= db_bits;
449 writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
450 spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
455 static int amd_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
457 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
458 void __iomem *mmio = ndev->self_mmio;
461 if (db_bits & ~ndev->db_valid_mask)
464 spin_lock_irqsave(&ndev->db_mask_lock, flags);
465 ndev->db_mask &= ~db_bits;
466 writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
467 spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
472 static int amd_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
474 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
475 void __iomem *mmio = ndev->self_mmio;
477 writew((u16)db_bits, mmio + AMD_DBREQ_OFFSET);
482 static int amd_ntb_spad_count(struct ntb_dev *ntb)
484 return ntb_ndev(ntb)->spad_count;
487 static u32 amd_ntb_spad_read(struct ntb_dev *ntb, int idx)
489 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
490 void __iomem *mmio = ndev->self_mmio;
493 if (idx < 0 || idx >= ndev->spad_count)
496 offset = ndev->self_spad + (idx << 2);
497 return readl(mmio + AMD_SPAD_OFFSET + offset);
500 static int amd_ntb_spad_write(struct ntb_dev *ntb,
503 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
504 void __iomem *mmio = ndev->self_mmio;
507 if (idx < 0 || idx >= ndev->spad_count)
510 offset = ndev->self_spad + (idx << 2);
511 writel(val, mmio + AMD_SPAD_OFFSET + offset);
516 static u32 amd_ntb_peer_spad_read(struct ntb_dev *ntb, int pidx, int sidx)
518 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
519 void __iomem *mmio = ndev->self_mmio;
522 if (sidx < 0 || sidx >= ndev->spad_count)
525 offset = ndev->peer_spad + (sidx << 2);
526 return readl(mmio + AMD_SPAD_OFFSET + offset);
529 static int amd_ntb_peer_spad_write(struct ntb_dev *ntb, int pidx,
532 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
533 void __iomem *mmio = ndev->self_mmio;
536 if (sidx < 0 || sidx >= ndev->spad_count)
539 offset = ndev->peer_spad + (sidx << 2);
540 writel(val, mmio + AMD_SPAD_OFFSET + offset);
545 static const struct ntb_dev_ops amd_ntb_ops = {
546 .mw_count = amd_ntb_mw_count,
547 .mw_get_align = amd_ntb_mw_get_align,
548 .mw_set_trans = amd_ntb_mw_set_trans,
549 .peer_mw_count = amd_ntb_peer_mw_count,
550 .peer_mw_get_addr = amd_ntb_peer_mw_get_addr,
551 .link_is_up = amd_ntb_link_is_up,
552 .link_enable = amd_ntb_link_enable,
553 .link_disable = amd_ntb_link_disable,
554 .db_valid_mask = amd_ntb_db_valid_mask,
555 .db_vector_count = amd_ntb_db_vector_count,
556 .db_vector_mask = amd_ntb_db_vector_mask,
557 .db_read = amd_ntb_db_read,
558 .db_clear = amd_ntb_db_clear,
559 .db_set_mask = amd_ntb_db_set_mask,
560 .db_clear_mask = amd_ntb_db_clear_mask,
561 .peer_db_set = amd_ntb_peer_db_set,
562 .spad_count = amd_ntb_spad_count,
563 .spad_read = amd_ntb_spad_read,
564 .spad_write = amd_ntb_spad_write,
565 .peer_spad_read = amd_ntb_peer_spad_read,
566 .peer_spad_write = amd_ntb_peer_spad_write,
569 static void amd_ack_smu(struct amd_ntb_dev *ndev, u32 bit)
571 void __iomem *mmio = ndev->self_mmio;
574 reg = readl(mmio + AMD_SMUACK_OFFSET);
576 writel(reg, mmio + AMD_SMUACK_OFFSET);
579 static void amd_handle_event(struct amd_ntb_dev *ndev, int vec)
581 void __iomem *mmio = ndev->self_mmio;
582 struct device *dev = &ndev->ntb.pdev->dev;
585 status = readl(mmio + AMD_INTSTAT_OFFSET);
586 if (!(status & AMD_EVENT_INTMASK))
589 dev_dbg(dev, "status = 0x%x and vec = %d\n", status, vec);
591 status &= AMD_EVENT_INTMASK;
593 case AMD_PEER_FLUSH_EVENT:
594 ndev->peer_sta |= AMD_PEER_FLUSH_EVENT;
595 dev_info(dev, "Flush is done.\n");
597 case AMD_PEER_RESET_EVENT:
598 case AMD_LINK_DOWN_EVENT:
599 ndev->peer_sta |= status;
600 if (status == AMD_LINK_DOWN_EVENT)
601 ndev->peer_sta &= ~AMD_LINK_UP_EVENT;
603 amd_ack_smu(ndev, status);
605 /* link down first */
606 ntb_link_event(&ndev->ntb);
607 /* polling peer status */
608 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
611 case AMD_PEER_D3_EVENT:
612 case AMD_PEER_PMETO_EVENT:
613 case AMD_LINK_UP_EVENT:
614 ndev->peer_sta |= status;
615 if (status == AMD_LINK_UP_EVENT)
616 ndev->peer_sta &= ~AMD_LINK_DOWN_EVENT;
617 else if (status == AMD_PEER_D3_EVENT)
618 ndev->peer_sta &= ~AMD_PEER_D0_EVENT;
620 amd_ack_smu(ndev, status);
623 ntb_link_event(&ndev->ntb);
626 case AMD_PEER_D0_EVENT:
627 mmio = ndev->peer_mmio;
628 status = readl(mmio + AMD_PMESTAT_OFFSET);
629 /* check if this is WAKEUP event */
631 dev_info(dev, "Wakeup is done.\n");
633 ndev->peer_sta |= AMD_PEER_D0_EVENT;
634 ndev->peer_sta &= ~AMD_PEER_D3_EVENT;
635 amd_ack_smu(ndev, AMD_PEER_D0_EVENT);
637 /* start a timer to poll link status */
638 schedule_delayed_work(&ndev->hb_timer,
639 AMD_LINK_HB_TIMEOUT);
642 dev_info(dev, "event status = 0x%x.\n", status);
646 /* Clear the interrupt status */
647 writel(status, mmio + AMD_INTSTAT_OFFSET);
650 static void amd_handle_db_event(struct amd_ntb_dev *ndev, int vec)
652 struct device *dev = &ndev->ntb.pdev->dev;
655 status = amd_ntb_db_read(&ndev->ntb);
657 dev_dbg(dev, "status = 0x%llx and vec = %d\n", status, vec);
660 * Since we had reserved highest order bit of DB for signaling peer of
661 * a special event, this is the only status bit we should be concerned
664 if (status & BIT(ndev->db_last_bit)) {
665 ntb_db_clear(&ndev->ntb, BIT(ndev->db_last_bit));
666 /* send link down event notification */
667 ntb_link_event(&ndev->ntb);
670 * If we are here, that means the peer has signalled a special
671 * event which notifies that the peer driver has been
672 * un-loaded for some reason. Since there is a chance that the
673 * peer will load its driver again sometime, we schedule link
676 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
680 static irqreturn_t ndev_interrupt(struct amd_ntb_dev *ndev, int vec)
682 dev_dbg(&ndev->ntb.pdev->dev, "vec %d\n", vec);
684 if (vec > (AMD_DB_CNT - 1) || (ndev->msix_vec_count == 1))
685 amd_handle_event(ndev, vec);
687 if (vec < AMD_DB_CNT) {
688 amd_handle_db_event(ndev, vec);
689 ntb_db_event(&ndev->ntb, vec);
695 static irqreturn_t ndev_vec_isr(int irq, void *dev)
697 struct amd_ntb_vec *nvec = dev;
699 return ndev_interrupt(nvec->ndev, nvec->num);
702 static irqreturn_t ndev_irq_isr(int irq, void *dev)
704 struct amd_ntb_dev *ndev = dev;
706 return ndev_interrupt(ndev, irq - ndev->ntb.pdev->irq);
709 static int ndev_init_isr(struct amd_ntb_dev *ndev,
710 int msix_min, int msix_max)
712 struct pci_dev *pdev;
713 int rc, i, msix_count, node;
715 pdev = ndev->ntb.pdev;
717 node = dev_to_node(&pdev->dev);
719 ndev->db_mask = ndev->db_valid_mask;
721 /* Try to set up msix irq */
722 ndev->vec = kcalloc_node(msix_max, sizeof(*ndev->vec),
725 goto err_msix_vec_alloc;
727 ndev->msix = kcalloc_node(msix_max, sizeof(*ndev->msix),
732 for (i = 0; i < msix_max; ++i)
733 ndev->msix[i].entry = i;
735 msix_count = pci_enable_msix_range(pdev, ndev->msix,
738 goto err_msix_enable;
740 /* NOTE: Disable MSIX if msix count is less than 16 because of
741 * hardware limitation.
743 if (msix_count < msix_min) {
744 pci_disable_msix(pdev);
745 goto err_msix_enable;
748 for (i = 0; i < msix_count; ++i) {
749 ndev->vec[i].ndev = ndev;
750 ndev->vec[i].num = i;
751 rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
752 "ndev_vec_isr", &ndev->vec[i]);
754 goto err_msix_request;
757 dev_dbg(&pdev->dev, "Using msix interrupts\n");
758 ndev->db_count = msix_min;
759 ndev->msix_vec_count = msix_max;
764 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
765 pci_disable_msix(pdev);
774 /* Try to set up msi irq */
775 rc = pci_enable_msi(pdev);
779 rc = request_irq(pdev->irq, ndev_irq_isr, 0,
780 "ndev_irq_isr", ndev);
782 goto err_msi_request;
784 dev_dbg(&pdev->dev, "Using msi interrupts\n");
786 ndev->msix_vec_count = 1;
790 pci_disable_msi(pdev);
793 /* Try to set up intx irq */
796 rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
797 "ndev_irq_isr", ndev);
799 goto err_intx_request;
801 dev_dbg(&pdev->dev, "Using intx interrupts\n");
803 ndev->msix_vec_count = 1;
810 static void ndev_deinit_isr(struct amd_ntb_dev *ndev)
812 struct pci_dev *pdev;
813 void __iomem *mmio = ndev->self_mmio;
816 pdev = ndev->ntb.pdev;
818 /* Mask all doorbell interrupts */
819 ndev->db_mask = ndev->db_valid_mask;
820 writel(ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
823 i = ndev->msix_vec_count;
825 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
826 pci_disable_msix(pdev);
830 free_irq(pdev->irq, ndev);
831 if (pci_dev_msi_enabled(pdev))
832 pci_disable_msi(pdev);
838 static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
839 size_t count, loff_t *offp)
841 struct amd_ntb_dev *ndev;
846 union { u64 v64; u32 v32; u16 v16; } u;
848 ndev = filp->private_data;
849 mmio = ndev->self_mmio;
851 buf_size = min(count, 0x800ul);
853 buf = kmalloc(buf_size, GFP_KERNEL);
859 off += scnprintf(buf + off, buf_size - off,
860 "NTB Device Information:\n");
862 off += scnprintf(buf + off, buf_size - off,
863 "Connection Topology -\t%s\n",
864 ntb_topo_string(ndev->ntb.topo));
866 off += scnprintf(buf + off, buf_size - off,
867 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
869 if (!amd_link_is_up(ndev)) {
870 off += scnprintf(buf + off, buf_size - off,
871 "Link Status -\t\tDown\n");
873 off += scnprintf(buf + off, buf_size - off,
874 "Link Status -\t\tUp\n");
875 off += scnprintf(buf + off, buf_size - off,
876 "Link Speed -\t\tPCI-E Gen %u\n",
877 NTB_LNK_STA_SPEED(ndev->lnk_sta));
878 off += scnprintf(buf + off, buf_size - off,
879 "Link Width -\t\tx%u\n",
880 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
883 off += scnprintf(buf + off, buf_size - off,
884 "Memory Window Count -\t%u\n", ndev->mw_count);
885 off += scnprintf(buf + off, buf_size - off,
886 "Scratchpad Count -\t%u\n", ndev->spad_count);
887 off += scnprintf(buf + off, buf_size - off,
888 "Doorbell Count -\t%u\n", ndev->db_count);
889 off += scnprintf(buf + off, buf_size - off,
890 "MSIX Vector Count -\t%u\n", ndev->msix_vec_count);
892 off += scnprintf(buf + off, buf_size - off,
893 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
895 u.v32 = readl(ndev->self_mmio + AMD_DBMASK_OFFSET);
896 off += scnprintf(buf + off, buf_size - off,
897 "Doorbell Mask -\t\t\t%#06x\n", u.v32);
899 u.v32 = readl(mmio + AMD_DBSTAT_OFFSET);
900 off += scnprintf(buf + off, buf_size - off,
901 "Doorbell Bell -\t\t\t%#06x\n", u.v32);
903 off += scnprintf(buf + off, buf_size - off,
904 "\nNTB Incoming XLAT:\n");
906 u.v64 = read64(mmio + AMD_BAR1XLAT_OFFSET);
907 off += scnprintf(buf + off, buf_size - off,
908 "XLAT1 -\t\t%#018llx\n", u.v64);
910 u.v64 = read64(ndev->self_mmio + AMD_BAR23XLAT_OFFSET);
911 off += scnprintf(buf + off, buf_size - off,
912 "XLAT23 -\t\t%#018llx\n", u.v64);
914 u.v64 = read64(ndev->self_mmio + AMD_BAR45XLAT_OFFSET);
915 off += scnprintf(buf + off, buf_size - off,
916 "XLAT45 -\t\t%#018llx\n", u.v64);
918 u.v32 = readl(mmio + AMD_BAR1LMT_OFFSET);
919 off += scnprintf(buf + off, buf_size - off,
920 "LMT1 -\t\t\t%#06x\n", u.v32);
922 u.v64 = read64(ndev->self_mmio + AMD_BAR23LMT_OFFSET);
923 off += scnprintf(buf + off, buf_size - off,
924 "LMT23 -\t\t\t%#018llx\n", u.v64);
926 u.v64 = read64(ndev->self_mmio + AMD_BAR45LMT_OFFSET);
927 off += scnprintf(buf + off, buf_size - off,
928 "LMT45 -\t\t\t%#018llx\n", u.v64);
930 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
935 static void ndev_init_debugfs(struct amd_ntb_dev *ndev)
938 ndev->debugfs_dir = NULL;
939 ndev->debugfs_info = NULL;
942 debugfs_create_dir(pci_name(ndev->ntb.pdev),
944 if (!ndev->debugfs_dir)
945 ndev->debugfs_info = NULL;
948 debugfs_create_file("info", S_IRUSR,
949 ndev->debugfs_dir, ndev,
950 &amd_ntb_debugfs_info);
954 static void ndev_deinit_debugfs(struct amd_ntb_dev *ndev)
956 debugfs_remove_recursive(ndev->debugfs_dir);
959 static inline void ndev_init_struct(struct amd_ntb_dev *ndev,
960 struct pci_dev *pdev)
962 ndev->ntb.pdev = pdev;
963 ndev->ntb.topo = NTB_TOPO_NONE;
964 ndev->ntb.ops = &amd_ntb_ops;
965 ndev->int_mask = AMD_EVENT_INTMASK;
966 spin_lock_init(&ndev->db_mask_lock);
969 static int amd_poll_link(struct amd_ntb_dev *ndev)
971 void __iomem *mmio = ndev->peer_mmio;
974 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
975 reg &= AMD_SIDE_READY;
977 dev_dbg(&ndev->ntb.pdev->dev, "%s: reg_val = 0x%x.\n", __func__, reg);
979 ndev->cntl_sta = reg;
981 amd_ntb_get_link_status(ndev);
983 return ndev->cntl_sta;
986 static void amd_link_hb(struct work_struct *work)
988 struct amd_ntb_dev *ndev = hb_ndev(work);
990 if (amd_poll_link(ndev))
991 ntb_link_event(&ndev->ntb);
993 if (!amd_link_is_up(ndev))
994 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
997 static int amd_init_isr(struct amd_ntb_dev *ndev)
999 return ndev_init_isr(ndev, AMD_DB_CNT, AMD_MSIX_VECTOR_CNT);
1002 static void amd_set_side_info_reg(struct amd_ntb_dev *ndev, bool peer)
1004 void __iomem *mmio = NULL;
1008 mmio = ndev->peer_mmio;
1010 mmio = ndev->self_mmio;
1012 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
1013 if (!(reg & AMD_SIDE_READY)) {
1014 reg |= AMD_SIDE_READY;
1015 writel(reg, mmio + AMD_SIDEINFO_OFFSET);
1019 static void amd_clear_side_info_reg(struct amd_ntb_dev *ndev, bool peer)
1021 void __iomem *mmio = NULL;
1025 mmio = ndev->peer_mmio;
1027 mmio = ndev->self_mmio;
1029 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
1030 if (reg & AMD_SIDE_READY) {
1031 reg &= ~AMD_SIDE_READY;
1032 writel(reg, mmio + AMD_SIDEINFO_OFFSET);
1033 readl(mmio + AMD_SIDEINFO_OFFSET);
1037 static void amd_init_side_info(struct amd_ntb_dev *ndev)
1039 void __iomem *mmio = ndev->self_mmio;
1042 amd_set_side_info_reg(ndev, false);
1044 ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
1045 ntb_ctl |= (PMM_REG_CTL | SMM_REG_CTL);
1046 writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
1049 static void amd_deinit_side_info(struct amd_ntb_dev *ndev)
1051 void __iomem *mmio = ndev->self_mmio;
1054 amd_clear_side_info_reg(ndev, false);
1056 ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
1057 ntb_ctl &= ~(PMM_REG_CTL | SMM_REG_CTL);
1058 writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
1061 static int amd_init_ntb(struct amd_ntb_dev *ndev)
1063 void __iomem *mmio = ndev->self_mmio;
1065 ndev->mw_count = ndev->dev_data->mw_count;
1066 ndev->spad_count = AMD_SPADS_CNT;
1067 ndev->db_count = AMD_DB_CNT;
1069 switch (ndev->ntb.topo) {
1072 ndev->spad_count >>= 1;
1073 if (ndev->ntb.topo == NTB_TOPO_PRI) {
1074 ndev->self_spad = 0;
1075 ndev->peer_spad = 0x20;
1077 ndev->self_spad = 0x20;
1078 ndev->peer_spad = 0;
1081 INIT_DELAYED_WORK(&ndev->hb_timer, amd_link_hb);
1082 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
1086 dev_err(&ndev->ntb.pdev->dev,
1087 "AMD NTB does not support B2B mode.\n");
1091 /* Mask event interrupts */
1092 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
1097 static enum ntb_topo amd_get_topo(struct amd_ntb_dev *ndev)
1099 void __iomem *mmio = ndev->self_mmio;
1102 info = readl(mmio + AMD_SIDEINFO_OFFSET);
1103 if (info & AMD_SIDE_MASK)
1104 return NTB_TOPO_SEC;
1106 return NTB_TOPO_PRI;
1109 static int amd_init_dev(struct amd_ntb_dev *ndev)
1111 void __iomem *mmio = ndev->self_mmio;
1112 struct pci_dev *pdev;
1115 pdev = ndev->ntb.pdev;
1117 ndev->ntb.topo = amd_get_topo(ndev);
1118 dev_dbg(&pdev->dev, "AMD NTB topo is %s\n",
1119 ntb_topo_string(ndev->ntb.topo));
1121 rc = amd_init_ntb(ndev);
1125 rc = amd_init_isr(ndev);
1127 dev_err(&pdev->dev, "fail to init isr.\n");
1131 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
1133 * We reserve the highest order bit of the DB register which will
1134 * be used to notify peer when the driver on this side is being
1138 find_last_bit((unsigned long *)&ndev->db_valid_mask,
1139 hweight64(ndev->db_valid_mask));
1140 writew((u16)~BIT(ndev->db_last_bit), mmio + AMD_DBMASK_OFFSET);
1142 * Since now there is one less bit to account for, the DB count
1143 * and DB mask should be adjusted accordingly.
1145 ndev->db_count -= 1;
1146 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
1148 /* Enable Link-Up and Link-Down event interrupts */
1149 ndev->int_mask &= ~(AMD_LINK_UP_EVENT | AMD_LINK_DOWN_EVENT);
1150 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
1155 static void amd_deinit_dev(struct amd_ntb_dev *ndev)
1157 cancel_delayed_work_sync(&ndev->hb_timer);
1159 ndev_deinit_isr(ndev);
1162 static int amd_ntb_init_pci(struct amd_ntb_dev *ndev,
1163 struct pci_dev *pdev)
1167 pci_set_drvdata(pdev, ndev);
1169 rc = pci_enable_device(pdev);
1171 goto err_pci_enable;
1173 rc = pci_request_regions(pdev, NTB_NAME);
1175 goto err_pci_regions;
1177 pci_set_master(pdev);
1179 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1181 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1184 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
1187 ndev->self_mmio = pci_iomap(pdev, 0, 0);
1188 if (!ndev->self_mmio) {
1192 ndev->peer_mmio = ndev->self_mmio + AMD_PEER_OFFSET;
1197 pci_clear_master(pdev);
1198 pci_release_regions(pdev);
1200 pci_disable_device(pdev);
1202 pci_set_drvdata(pdev, NULL);
1206 static void amd_ntb_deinit_pci(struct amd_ntb_dev *ndev)
1208 struct pci_dev *pdev = ndev->ntb.pdev;
1210 pci_iounmap(pdev, ndev->self_mmio);
1212 pci_clear_master(pdev);
1213 pci_release_regions(pdev);
1214 pci_disable_device(pdev);
1215 pci_set_drvdata(pdev, NULL);
1218 static int amd_ntb_pci_probe(struct pci_dev *pdev,
1219 const struct pci_device_id *id)
1221 struct amd_ntb_dev *ndev;
1224 node = dev_to_node(&pdev->dev);
1226 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
1232 ndev->dev_data = (struct ntb_dev_data *)id->driver_data;
1234 ndev_init_struct(ndev, pdev);
1236 rc = amd_ntb_init_pci(ndev, pdev);
1240 rc = amd_init_dev(ndev);
1244 /* write side info */
1245 amd_init_side_info(ndev);
1247 amd_poll_link(ndev);
1249 ndev_init_debugfs(ndev);
1251 rc = ntb_register_device(&ndev->ntb);
1255 dev_info(&pdev->dev, "NTB device registered.\n");
1260 ndev_deinit_debugfs(ndev);
1261 amd_deinit_dev(ndev);
1263 amd_ntb_deinit_pci(ndev);
1270 static void amd_ntb_pci_remove(struct pci_dev *pdev)
1272 struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
1275 * Clear the READY bit in SIDEINFO register before sending DB event
1276 * to the peer. This will make sure that when the peer handles the
1277 * DB event, it correctly reads this bit as being 0.
1279 amd_deinit_side_info(ndev);
1280 ntb_peer_db_set(&ndev->ntb, BIT_ULL(ndev->db_last_bit));
1281 ntb_unregister_device(&ndev->ntb);
1282 ndev_deinit_debugfs(ndev);
1283 amd_deinit_dev(ndev);
1284 amd_ntb_deinit_pci(ndev);
1288 static void amd_ntb_pci_shutdown(struct pci_dev *pdev)
1290 struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
1292 /* Send link down notification */
1293 ntb_link_event(&ndev->ntb);
1295 amd_deinit_side_info(ndev);
1296 ntb_peer_db_set(&ndev->ntb, BIT_ULL(ndev->db_last_bit));
1297 ntb_unregister_device(&ndev->ntb);
1298 ndev_deinit_debugfs(ndev);
1299 amd_deinit_dev(ndev);
1300 amd_ntb_deinit_pci(ndev);
1304 static const struct file_operations amd_ntb_debugfs_info = {
1305 .owner = THIS_MODULE,
1306 .open = simple_open,
1307 .read = ndev_debugfs_read,
1310 static const struct ntb_dev_data dev_data[] = {
1311 { /* for device 145b */
1315 { /* for device 148b */
1321 static const struct pci_device_id amd_ntb_pci_tbl[] = {
1322 { PCI_VDEVICE(AMD, 0x145b), (kernel_ulong_t)&dev_data[0] },
1323 { PCI_VDEVICE(AMD, 0x148b), (kernel_ulong_t)&dev_data[1] },
1324 { PCI_VDEVICE(HYGON, 0x145b), (kernel_ulong_t)&dev_data[0] },
1327 MODULE_DEVICE_TABLE(pci, amd_ntb_pci_tbl);
1329 static struct pci_driver amd_ntb_pci_driver = {
1330 .name = KBUILD_MODNAME,
1331 .id_table = amd_ntb_pci_tbl,
1332 .probe = amd_ntb_pci_probe,
1333 .remove = amd_ntb_pci_remove,
1334 .shutdown = amd_ntb_pci_shutdown,
1337 static int __init amd_ntb_pci_driver_init(void)
1339 pr_info("%s %s\n", NTB_DESC, NTB_VER);
1341 if (debugfs_initialized())
1342 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1344 return pci_register_driver(&amd_ntb_pci_driver);
1346 module_init(amd_ntb_pci_driver_init);
1348 static void __exit amd_ntb_pci_driver_exit(void)
1350 pci_unregister_driver(&amd_ntb_pci_driver);
1351 debugfs_remove_recursive(debugfs_dir);
1353 module_exit(amd_ntb_pci_driver_exit);