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.
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) 2016 Advanced Micro Devices, Inc. All Rights Reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copy
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
27 * * Neither the name of AMD Corporation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * AMD PCIe NTB Linux driver
45 * Contact Information:
46 * Xiangliang Yu <Xiangliang.Yu@amd.com>
49 #include <linux/debugfs.h>
50 #include <linux/delay.h>
51 #include <linux/init.h>
52 #include <linux/interrupt.h>
53 #include <linux/module.h>
54 #include <linux/acpi.h>
55 #include <linux/pci.h>
56 #include <linux/random.h>
57 #include <linux/slab.h>
58 #include <linux/ntb.h>
60 #include "ntb_hw_amd.h"
62 #define NTB_NAME "ntb_hw_amd"
63 #define NTB_DESC "AMD(R) PCI-E Non-Transparent Bridge Driver"
66 MODULE_DESCRIPTION(NTB_DESC);
67 MODULE_VERSION(NTB_VER);
68 MODULE_LICENSE("Dual BSD/GPL");
69 MODULE_AUTHOR("AMD Inc.");
71 static const struct file_operations amd_ntb_debugfs_info;
72 static struct dentry *debugfs_dir;
74 static int ndev_mw_to_bar(struct amd_ntb_dev *ndev, int idx)
76 if (idx < 0 || idx > ndev->mw_count)
82 static int amd_ntb_mw_count(struct ntb_dev *ntb)
84 return ntb_ndev(ntb)->mw_count;
87 static int amd_ntb_mw_get_range(struct ntb_dev *ntb, int idx,
89 resource_size_t *size,
90 resource_size_t *align,
91 resource_size_t *align_size)
93 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
96 bar = ndev_mw_to_bar(ndev, idx);
101 *base = pci_resource_start(ndev->ntb.pdev, bar);
104 *size = pci_resource_len(ndev->ntb.pdev, bar);
115 static int amd_ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
116 dma_addr_t addr, resource_size_t size)
118 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
119 unsigned long xlat_reg, limit_reg = 0;
120 resource_size_t mw_size;
121 void __iomem *mmio, *peer_mmio;
122 u64 base_addr, limit, reg_val;
125 bar = ndev_mw_to_bar(ndev, idx);
129 mw_size = pci_resource_len(ndev->ntb.pdev, bar);
131 /* make sure the range fits in the usable mw size */
135 mmio = ndev->self_mmio;
136 peer_mmio = ndev->peer_mmio;
138 base_addr = pci_resource_start(ndev->ntb.pdev, bar);
141 xlat_reg = AMD_BAR23XLAT_OFFSET + ((bar - 2) << 3);
142 limit_reg = AMD_BAR23LMT_OFFSET + ((bar - 2) << 3);
144 /* Set the limit if supported */
145 limit = base_addr + size;
147 /* set and verify setting the translation address */
148 write64(addr, peer_mmio + xlat_reg);
149 reg_val = read64(peer_mmio + xlat_reg);
150 if (reg_val != addr) {
151 write64(0, peer_mmio + xlat_reg);
155 /* set and verify setting the limit */
156 write64(limit, mmio + limit_reg);
157 reg_val = read64(mmio + limit_reg);
158 if (reg_val != limit) {
159 write64(base_addr, mmio + limit_reg);
160 write64(0, peer_mmio + xlat_reg);
164 xlat_reg = AMD_BAR1XLAT_OFFSET;
165 limit_reg = AMD_BAR1LMT_OFFSET;
167 /* split bar addr range must all be 32 bit */
168 if (addr & (~0ull << 32))
170 if ((addr + size) & (~0ull << 32))
173 /* Set the limit if supported */
174 limit = base_addr + size;
176 /* set and verify setting the translation address */
177 write64(addr, peer_mmio + xlat_reg);
178 reg_val = read64(peer_mmio + xlat_reg);
179 if (reg_val != addr) {
180 write64(0, peer_mmio + xlat_reg);
184 /* set and verify setting the limit */
185 writel(limit, mmio + limit_reg);
186 reg_val = readl(mmio + limit_reg);
187 if (reg_val != limit) {
188 writel(base_addr, mmio + limit_reg);
189 writel(0, peer_mmio + xlat_reg);
197 static int amd_link_is_up(struct amd_ntb_dev *ndev)
200 return NTB_LNK_STA_ACTIVE(ndev->cntl_sta);
202 /* If peer_sta is reset or D0 event, the ISR has
203 * started a timer to check link status of hardware.
204 * So here just clear status bit. And if peer_sta is
205 * D3 or PME_TO, D0/reset event will be happened when
206 * system wakeup/poweron, so do nothing here.
208 if (ndev->peer_sta & AMD_PEER_RESET_EVENT)
209 ndev->peer_sta &= ~AMD_PEER_RESET_EVENT;
210 else if (ndev->peer_sta & AMD_PEER_D0_EVENT)
216 static int amd_ntb_link_is_up(struct ntb_dev *ntb,
217 enum ntb_speed *speed,
218 enum ntb_width *width)
220 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
223 if (amd_link_is_up(ndev)) {
225 *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
227 *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
229 dev_dbg(ndev_dev(ndev), "link is up.\n");
234 *speed = NTB_SPEED_NONE;
236 *width = NTB_WIDTH_NONE;
238 dev_dbg(ndev_dev(ndev), "link is down.\n");
244 static int amd_ntb_link_enable(struct ntb_dev *ntb,
245 enum ntb_speed max_speed,
246 enum ntb_width max_width)
248 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
249 void __iomem *mmio = ndev->self_mmio;
252 /* Enable event interrupt */
253 ndev->int_mask &= ~AMD_EVENT_INTMASK;
254 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
256 if (ndev->ntb.topo == NTB_TOPO_SEC)
258 dev_dbg(ndev_dev(ndev), "Enabling Link.\n");
260 ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
261 ntb_ctl |= (PMM_REG_CTL | SMM_REG_CTL);
262 writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
267 static int amd_ntb_link_disable(struct ntb_dev *ntb)
269 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
270 void __iomem *mmio = ndev->self_mmio;
273 /* Disable event interrupt */
274 ndev->int_mask |= AMD_EVENT_INTMASK;
275 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
277 if (ndev->ntb.topo == NTB_TOPO_SEC)
279 dev_dbg(ndev_dev(ndev), "Enabling Link.\n");
281 ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
282 ntb_ctl &= ~(PMM_REG_CTL | SMM_REG_CTL);
283 writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
288 static u64 amd_ntb_db_valid_mask(struct ntb_dev *ntb)
290 return ntb_ndev(ntb)->db_valid_mask;
293 static int amd_ntb_db_vector_count(struct ntb_dev *ntb)
295 return ntb_ndev(ntb)->db_count;
298 static u64 amd_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
300 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
302 if (db_vector < 0 || db_vector > ndev->db_count)
305 return ntb_ndev(ntb)->db_valid_mask & (1 << db_vector);
308 static u64 amd_ntb_db_read(struct ntb_dev *ntb)
310 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
311 void __iomem *mmio = ndev->self_mmio;
313 return (u64)readw(mmio + AMD_DBSTAT_OFFSET);
316 static int amd_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
318 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
319 void __iomem *mmio = ndev->self_mmio;
321 writew((u16)db_bits, mmio + AMD_DBSTAT_OFFSET);
326 static int amd_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
328 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
329 void __iomem *mmio = ndev->self_mmio;
332 if (db_bits & ~ndev->db_valid_mask)
335 spin_lock_irqsave(&ndev->db_mask_lock, flags);
336 ndev->db_mask |= db_bits;
337 writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
338 spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
343 static int amd_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
345 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
346 void __iomem *mmio = ndev->self_mmio;
349 if (db_bits & ~ndev->db_valid_mask)
352 spin_lock_irqsave(&ndev->db_mask_lock, flags);
353 ndev->db_mask &= ~db_bits;
354 writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
355 spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
360 static int amd_ntb_peer_db_addr(struct ntb_dev *ntb,
361 phys_addr_t *db_addr,
362 resource_size_t *db_size)
364 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
367 *db_addr = (phys_addr_t)(ndev->peer_mmio + AMD_DBREQ_OFFSET);
369 *db_size = sizeof(u32);
374 static int amd_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
376 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
377 void __iomem *mmio = ndev->self_mmio;
379 writew((u16)db_bits, mmio + AMD_DBREQ_OFFSET);
384 static int amd_ntb_spad_count(struct ntb_dev *ntb)
386 return ntb_ndev(ntb)->spad_count;
389 static u32 amd_ntb_spad_read(struct ntb_dev *ntb, int idx)
391 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
392 void __iomem *mmio = ndev->self_mmio;
395 if (idx < 0 || idx >= ndev->spad_count)
398 offset = ndev->self_spad + (idx << 2);
399 return readl(mmio + AMD_SPAD_OFFSET + offset);
402 static int amd_ntb_spad_write(struct ntb_dev *ntb,
405 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
406 void __iomem *mmio = ndev->self_mmio;
409 if (idx < 0 || idx >= ndev->spad_count)
412 offset = ndev->self_spad + (idx << 2);
413 writel(val, mmio + AMD_SPAD_OFFSET + offset);
418 static int amd_ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
419 phys_addr_t *spad_addr)
421 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
423 if (idx < 0 || idx >= ndev->spad_count)
427 *spad_addr = (phys_addr_t)(ndev->self_mmio + AMD_SPAD_OFFSET +
428 ndev->peer_spad + (idx << 2));
432 static u32 amd_ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
434 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
435 void __iomem *mmio = ndev->self_mmio;
438 if (idx < 0 || idx >= ndev->spad_count)
441 offset = ndev->peer_spad + (idx << 2);
442 return readl(mmio + AMD_SPAD_OFFSET + offset);
445 static int amd_ntb_peer_spad_write(struct ntb_dev *ntb,
448 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
449 void __iomem *mmio = ndev->self_mmio;
452 if (idx < 0 || idx >= ndev->spad_count)
455 offset = ndev->peer_spad + (idx << 2);
456 writel(val, mmio + AMD_SPAD_OFFSET + offset);
461 static const struct ntb_dev_ops amd_ntb_ops = {
462 .mw_count = amd_ntb_mw_count,
463 .mw_get_range = amd_ntb_mw_get_range,
464 .mw_set_trans = amd_ntb_mw_set_trans,
465 .link_is_up = amd_ntb_link_is_up,
466 .link_enable = amd_ntb_link_enable,
467 .link_disable = amd_ntb_link_disable,
468 .db_valid_mask = amd_ntb_db_valid_mask,
469 .db_vector_count = amd_ntb_db_vector_count,
470 .db_vector_mask = amd_ntb_db_vector_mask,
471 .db_read = amd_ntb_db_read,
472 .db_clear = amd_ntb_db_clear,
473 .db_set_mask = amd_ntb_db_set_mask,
474 .db_clear_mask = amd_ntb_db_clear_mask,
475 .peer_db_addr = amd_ntb_peer_db_addr,
476 .peer_db_set = amd_ntb_peer_db_set,
477 .spad_count = amd_ntb_spad_count,
478 .spad_read = amd_ntb_spad_read,
479 .spad_write = amd_ntb_spad_write,
480 .peer_spad_addr = amd_ntb_peer_spad_addr,
481 .peer_spad_read = amd_ntb_peer_spad_read,
482 .peer_spad_write = amd_ntb_peer_spad_write,
485 static void amd_ack_smu(struct amd_ntb_dev *ndev, u32 bit)
487 void __iomem *mmio = ndev->self_mmio;
490 reg = readl(mmio + AMD_SMUACK_OFFSET);
492 writel(reg, mmio + AMD_SMUACK_OFFSET);
494 ndev->peer_sta |= bit;
497 static void amd_handle_event(struct amd_ntb_dev *ndev, int vec)
499 void __iomem *mmio = ndev->self_mmio;
502 status = readl(mmio + AMD_INTSTAT_OFFSET);
503 if (!(status & AMD_EVENT_INTMASK))
506 dev_dbg(ndev_dev(ndev), "status = 0x%x and vec = %d\n", status, vec);
508 status &= AMD_EVENT_INTMASK;
510 case AMD_PEER_FLUSH_EVENT:
511 dev_info(ndev_dev(ndev), "Flush is done.\n");
513 case AMD_PEER_RESET_EVENT:
514 amd_ack_smu(ndev, AMD_PEER_RESET_EVENT);
516 /* link down first */
517 ntb_link_event(&ndev->ntb);
518 /* polling peer status */
519 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
522 case AMD_PEER_D3_EVENT:
523 case AMD_PEER_PMETO_EVENT:
524 amd_ack_smu(ndev, status);
527 ntb_link_event(&ndev->ntb);
530 case AMD_PEER_D0_EVENT:
531 mmio = ndev->peer_mmio;
532 status = readl(mmio + AMD_PMESTAT_OFFSET);
533 /* check if this is WAKEUP event */
535 dev_info(ndev_dev(ndev), "Wakeup is done.\n");
537 amd_ack_smu(ndev, AMD_PEER_D0_EVENT);
539 /* start a timer to poll link status */
540 schedule_delayed_work(&ndev->hb_timer,
541 AMD_LINK_HB_TIMEOUT);
544 dev_info(ndev_dev(ndev), "event status = 0x%x.\n", status);
549 static irqreturn_t ndev_interrupt(struct amd_ntb_dev *ndev, int vec)
551 dev_dbg(ndev_dev(ndev), "vec %d\n", vec);
553 if (vec > (AMD_DB_CNT - 1) || (ndev->msix_vec_count == 1))
554 amd_handle_event(ndev, vec);
556 if (vec < AMD_DB_CNT)
557 ntb_db_event(&ndev->ntb, vec);
562 static irqreturn_t ndev_vec_isr(int irq, void *dev)
564 struct amd_ntb_vec *nvec = dev;
566 return ndev_interrupt(nvec->ndev, nvec->num);
569 static irqreturn_t ndev_irq_isr(int irq, void *dev)
571 struct amd_ntb_dev *ndev = dev;
573 return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
576 static int ndev_init_isr(struct amd_ntb_dev *ndev,
577 int msix_min, int msix_max)
579 struct pci_dev *pdev;
580 int rc, i, msix_count, node;
582 pdev = ndev_pdev(ndev);
584 node = dev_to_node(&pdev->dev);
586 ndev->db_mask = ndev->db_valid_mask;
588 /* Try to set up msix irq */
589 ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
592 goto err_msix_vec_alloc;
594 ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
599 for (i = 0; i < msix_max; ++i)
600 ndev->msix[i].entry = i;
602 msix_count = pci_enable_msix_range(pdev, ndev->msix,
605 goto err_msix_enable;
607 /* NOTE: Disable MSIX if msix count is less than 16 because of
608 * hardware limitation.
610 if (msix_count < msix_min) {
611 pci_disable_msix(pdev);
612 goto err_msix_enable;
615 for (i = 0; i < msix_count; ++i) {
616 ndev->vec[i].ndev = ndev;
617 ndev->vec[i].num = i;
618 rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
619 "ndev_vec_isr", &ndev->vec[i]);
621 goto err_msix_request;
624 dev_dbg(ndev_dev(ndev), "Using msix interrupts\n");
625 ndev->db_count = msix_min;
626 ndev->msix_vec_count = msix_max;
631 free_irq(ndev->msix[i].vector, ndev);
632 pci_disable_msix(pdev);
641 /* Try to set up msi irq */
642 rc = pci_enable_msi(pdev);
646 rc = request_irq(pdev->irq, ndev_irq_isr, 0,
647 "ndev_irq_isr", ndev);
649 goto err_msi_request;
651 dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
653 ndev->msix_vec_count = 1;
657 pci_disable_msi(pdev);
660 /* Try to set up intx irq */
663 rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
664 "ndev_irq_isr", ndev);
666 goto err_intx_request;
668 dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
670 ndev->msix_vec_count = 1;
677 static void ndev_deinit_isr(struct amd_ntb_dev *ndev)
679 struct pci_dev *pdev;
680 void __iomem *mmio = ndev->self_mmio;
683 pdev = ndev_pdev(ndev);
685 /* Mask all doorbell interrupts */
686 ndev->db_mask = ndev->db_valid_mask;
687 writel(ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
690 i = ndev->msix_vec_count;
692 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
693 pci_disable_msix(pdev);
697 free_irq(pdev->irq, ndev);
698 if (pci_dev_msi_enabled(pdev))
699 pci_disable_msi(pdev);
705 static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
706 size_t count, loff_t *offp)
708 struct amd_ntb_dev *ndev;
713 union { u64 v64; u32 v32; u16 v16; } u;
715 ndev = filp->private_data;
716 mmio = ndev->self_mmio;
718 buf_size = min(count, 0x800ul);
720 buf = kmalloc(buf_size, GFP_KERNEL);
726 off += scnprintf(buf + off, buf_size - off,
727 "NTB Device Information:\n");
729 off += scnprintf(buf + off, buf_size - off,
730 "Connection Topology -\t%s\n",
731 ntb_topo_string(ndev->ntb.topo));
733 off += scnprintf(buf + off, buf_size - off,
734 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
736 if (!amd_link_is_up(ndev)) {
737 off += scnprintf(buf + off, buf_size - off,
738 "Link Status -\t\tDown\n");
740 off += scnprintf(buf + off, buf_size - off,
741 "Link Status -\t\tUp\n");
742 off += scnprintf(buf + off, buf_size - off,
743 "Link Speed -\t\tPCI-E Gen %u\n",
744 NTB_LNK_STA_SPEED(ndev->lnk_sta));
745 off += scnprintf(buf + off, buf_size - off,
746 "Link Width -\t\tx%u\n",
747 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
750 off += scnprintf(buf + off, buf_size - off,
751 "Memory Window Count -\t%u\n", ndev->mw_count);
752 off += scnprintf(buf + off, buf_size - off,
753 "Scratchpad Count -\t%u\n", ndev->spad_count);
754 off += scnprintf(buf + off, buf_size - off,
755 "Doorbell Count -\t%u\n", ndev->db_count);
756 off += scnprintf(buf + off, buf_size - off,
757 "MSIX Vector Count -\t%u\n", ndev->msix_vec_count);
759 off += scnprintf(buf + off, buf_size - off,
760 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
762 u.v32 = readl(ndev->self_mmio + AMD_DBMASK_OFFSET);
763 off += scnprintf(buf + off, buf_size - off,
764 "Doorbell Mask -\t\t\t%#06x\n", u.v32);
766 u.v32 = readl(mmio + AMD_DBSTAT_OFFSET);
767 off += scnprintf(buf + off, buf_size - off,
768 "Doorbell Bell -\t\t\t%#06x\n", u.v32);
770 off += scnprintf(buf + off, buf_size - off,
771 "\nNTB Incoming XLAT:\n");
773 u.v64 = read64(mmio + AMD_BAR1XLAT_OFFSET);
774 off += scnprintf(buf + off, buf_size - off,
775 "XLAT1 -\t\t%#018llx\n", u.v64);
777 u.v64 = read64(ndev->self_mmio + AMD_BAR23XLAT_OFFSET);
778 off += scnprintf(buf + off, buf_size - off,
779 "XLAT23 -\t\t%#018llx\n", u.v64);
781 u.v64 = read64(ndev->self_mmio + AMD_BAR45XLAT_OFFSET);
782 off += scnprintf(buf + off, buf_size - off,
783 "XLAT45 -\t\t%#018llx\n", u.v64);
785 u.v32 = readl(mmio + AMD_BAR1LMT_OFFSET);
786 off += scnprintf(buf + off, buf_size - off,
787 "LMT1 -\t\t\t%#06x\n", u.v32);
789 u.v64 = read64(ndev->self_mmio + AMD_BAR23LMT_OFFSET);
790 off += scnprintf(buf + off, buf_size - off,
791 "LMT23 -\t\t\t%#018llx\n", u.v64);
793 u.v64 = read64(ndev->self_mmio + AMD_BAR45LMT_OFFSET);
794 off += scnprintf(buf + off, buf_size - off,
795 "LMT45 -\t\t\t%#018llx\n", u.v64);
797 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
802 static void ndev_init_debugfs(struct amd_ntb_dev *ndev)
805 ndev->debugfs_dir = NULL;
806 ndev->debugfs_info = NULL;
809 debugfs_create_dir(ndev_name(ndev), debugfs_dir);
810 if (!ndev->debugfs_dir)
811 ndev->debugfs_info = NULL;
814 debugfs_create_file("info", S_IRUSR,
815 ndev->debugfs_dir, ndev,
816 &amd_ntb_debugfs_info);
820 static void ndev_deinit_debugfs(struct amd_ntb_dev *ndev)
822 debugfs_remove_recursive(ndev->debugfs_dir);
825 static inline void ndev_init_struct(struct amd_ntb_dev *ndev,
826 struct pci_dev *pdev)
828 ndev->ntb.pdev = pdev;
829 ndev->ntb.topo = NTB_TOPO_NONE;
830 ndev->ntb.ops = &amd_ntb_ops;
831 ndev->int_mask = AMD_EVENT_INTMASK;
832 spin_lock_init(&ndev->db_mask_lock);
835 static int amd_poll_link(struct amd_ntb_dev *ndev)
837 void __iomem *mmio = ndev->peer_mmio;
841 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
842 reg &= NTB_LIN_STA_ACTIVE_BIT;
844 dev_dbg(ndev_dev(ndev), "%s: reg_val = 0x%x.\n", __func__, reg);
846 if (reg == ndev->cntl_sta)
849 ndev->cntl_sta = reg;
851 rc = pci_read_config_dword(ndev->ntb.pdev,
852 AMD_LINK_STATUS_OFFSET, &stat);
855 ndev->lnk_sta = stat;
860 static void amd_link_hb(struct work_struct *work)
862 struct amd_ntb_dev *ndev = hb_ndev(work);
864 if (amd_poll_link(ndev))
865 ntb_link_event(&ndev->ntb);
867 if (!amd_link_is_up(ndev))
868 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
871 static int amd_init_isr(struct amd_ntb_dev *ndev)
873 return ndev_init_isr(ndev, AMD_DB_CNT, AMD_MSIX_VECTOR_CNT);
876 static void amd_init_side_info(struct amd_ntb_dev *ndev)
878 void __iomem *mmio = ndev->self_mmio;
881 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
882 if (!(reg & AMD_SIDE_READY)) {
883 reg |= AMD_SIDE_READY;
884 writel(reg, mmio + AMD_SIDEINFO_OFFSET);
888 static void amd_deinit_side_info(struct amd_ntb_dev *ndev)
890 void __iomem *mmio = ndev->self_mmio;
893 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
894 if (reg & AMD_SIDE_READY) {
895 reg &= ~AMD_SIDE_READY;
896 writel(reg, mmio + AMD_SIDEINFO_OFFSET);
897 readl(mmio + AMD_SIDEINFO_OFFSET);
901 static int amd_init_ntb(struct amd_ntb_dev *ndev)
903 void __iomem *mmio = ndev->self_mmio;
905 ndev->mw_count = AMD_MW_CNT;
906 ndev->spad_count = AMD_SPADS_CNT;
907 ndev->db_count = AMD_DB_CNT;
909 switch (ndev->ntb.topo) {
912 ndev->spad_count >>= 1;
913 if (ndev->ntb.topo == NTB_TOPO_PRI) {
915 ndev->peer_spad = 0x20;
917 ndev->self_spad = 0x20;
921 INIT_DELAYED_WORK(&ndev->hb_timer, amd_link_hb);
922 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
926 dev_err(ndev_dev(ndev), "AMD NTB does not support B2B mode.\n");
930 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
932 /* Mask event interrupts */
933 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
938 static enum ntb_topo amd_get_topo(struct amd_ntb_dev *ndev)
940 void __iomem *mmio = ndev->self_mmio;
943 info = readl(mmio + AMD_SIDEINFO_OFFSET);
944 if (info & AMD_SIDE_MASK)
950 static int amd_init_dev(struct amd_ntb_dev *ndev)
952 struct pci_dev *pdev;
955 pdev = ndev_pdev(ndev);
957 ndev->ntb.topo = amd_get_topo(ndev);
958 dev_dbg(ndev_dev(ndev), "AMD NTB topo is %s\n",
959 ntb_topo_string(ndev->ntb.topo));
961 rc = amd_init_ntb(ndev);
965 rc = amd_init_isr(ndev);
967 dev_err(ndev_dev(ndev), "fail to init isr.\n");
971 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
976 static void amd_deinit_dev(struct amd_ntb_dev *ndev)
978 cancel_delayed_work_sync(&ndev->hb_timer);
980 ndev_deinit_isr(ndev);
983 static int amd_ntb_init_pci(struct amd_ntb_dev *ndev,
984 struct pci_dev *pdev)
988 pci_set_drvdata(pdev, ndev);
990 rc = pci_enable_device(pdev);
994 rc = pci_request_regions(pdev, NTB_NAME);
996 goto err_pci_regions;
998 pci_set_master(pdev);
1000 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1002 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1005 dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
1008 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1010 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1013 dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
1016 ndev->self_mmio = pci_iomap(pdev, 0, 0);
1017 if (!ndev->self_mmio) {
1021 ndev->peer_mmio = ndev->self_mmio + AMD_PEER_OFFSET;
1026 pci_clear_master(pdev);
1028 pci_disable_device(pdev);
1030 pci_set_drvdata(pdev, NULL);
1034 static void amd_ntb_deinit_pci(struct amd_ntb_dev *ndev)
1036 struct pci_dev *pdev = ndev_pdev(ndev);
1038 pci_iounmap(pdev, ndev->self_mmio);
1040 pci_clear_master(pdev);
1041 pci_release_regions(pdev);
1042 pci_disable_device(pdev);
1043 pci_set_drvdata(pdev, NULL);
1046 static int amd_ntb_pci_probe(struct pci_dev *pdev,
1047 const struct pci_device_id *id)
1049 struct amd_ntb_dev *ndev;
1052 node = dev_to_node(&pdev->dev);
1054 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
1060 ndev_init_struct(ndev, pdev);
1062 rc = amd_ntb_init_pci(ndev, pdev);
1066 rc = amd_init_dev(ndev);
1070 /* write side info */
1071 amd_init_side_info(ndev);
1073 amd_poll_link(ndev);
1075 ndev_init_debugfs(ndev);
1077 rc = ntb_register_device(&ndev->ntb);
1081 dev_info(&pdev->dev, "NTB device registered.\n");
1086 ndev_deinit_debugfs(ndev);
1087 amd_deinit_dev(ndev);
1089 amd_ntb_deinit_pci(ndev);
1096 static void amd_ntb_pci_remove(struct pci_dev *pdev)
1098 struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
1100 ntb_unregister_device(&ndev->ntb);
1101 ndev_deinit_debugfs(ndev);
1102 amd_deinit_side_info(ndev);
1103 amd_deinit_dev(ndev);
1104 amd_ntb_deinit_pci(ndev);
1108 static const struct file_operations amd_ntb_debugfs_info = {
1109 .owner = THIS_MODULE,
1110 .open = simple_open,
1111 .read = ndev_debugfs_read,
1114 static const struct pci_device_id amd_ntb_pci_tbl[] = {
1115 {PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NTB)},
1118 MODULE_DEVICE_TABLE(pci, amd_ntb_pci_tbl);
1120 static struct pci_driver amd_ntb_pci_driver = {
1121 .name = KBUILD_MODNAME,
1122 .id_table = amd_ntb_pci_tbl,
1123 .probe = amd_ntb_pci_probe,
1124 .remove = amd_ntb_pci_remove,
1127 static int __init amd_ntb_pci_driver_init(void)
1129 pr_info("%s %s\n", NTB_DESC, NTB_VER);
1131 if (debugfs_initialized())
1132 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1134 return pci_register_driver(&amd_ntb_pci_driver);
1136 module_init(amd_ntb_pci_driver_init);
1138 static void __exit amd_ntb_pci_driver_exit(void)
1140 pci_unregister_driver(&amd_ntb_pci_driver);
1141 debugfs_remove_recursive(debugfs_dir);
1143 module_exit(amd_ntb_pci_driver_exit);