Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / ntb / hw / amd / ntb_hw_amd.c
blob6ccba0d862df704877db1589fff036d40c10cf36
1 /*
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.
5 * GPL LICENSE SUMMARY
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.
13 * BSD LICENSE
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
19 * are met:
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
26 * distribution.
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"
64 #define NTB_VER "1.0"
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)
77 return -EINVAL;
79 return 1 << idx;
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,
88 phys_addr_t *base,
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);
94 int bar;
96 bar = ndev_mw_to_bar(ndev, idx);
97 if (bar < 0)
98 return bar;
100 if (base)
101 *base = pci_resource_start(ndev->ntb.pdev, bar);
103 if (size)
104 *size = pci_resource_len(ndev->ntb.pdev, bar);
106 if (align)
107 *align = SZ_4K;
109 if (align_size)
110 *align_size = 1;
112 return 0;
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;
123 int bar;
125 bar = ndev_mw_to_bar(ndev, idx);
126 if (bar < 0)
127 return bar;
129 mw_size = pci_resource_len(ndev->ntb.pdev, bar);
131 /* make sure the range fits in the usable mw size */
132 if (size > mw_size)
133 return -EINVAL;
135 mmio = ndev->self_mmio;
136 peer_mmio = ndev->peer_mmio;
138 base_addr = pci_resource_start(ndev->ntb.pdev, bar);
140 if (bar != 1) {
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);
152 return -EIO;
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);
161 return -EIO;
163 } else {
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))
169 return -EINVAL;
170 if ((addr + size) & (~0ull << 32))
171 return -EINVAL;
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);
181 return -EIO;
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);
190 return -EIO;
194 return 0;
197 static int amd_link_is_up(struct amd_ntb_dev *ndev)
199 if (!ndev->peer_sta)
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)
211 ndev->peer_sta = 0;
213 return 0;
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);
221 int ret = 0;
223 if (amd_link_is_up(ndev)) {
224 if (speed)
225 *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
226 if (width)
227 *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
229 dev_dbg(ndev_dev(ndev), "link is up.\n");
231 ret = 1;
232 } else {
233 if (speed)
234 *speed = NTB_SPEED_NONE;
235 if (width)
236 *width = NTB_WIDTH_NONE;
238 dev_dbg(ndev_dev(ndev), "link is down.\n");
241 return ret;
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;
250 u32 ntb_ctl;
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)
257 return -EINVAL;
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);
264 return 0;
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;
271 u32 ntb_ctl;
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)
278 return -EINVAL;
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);
285 return 0;
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)
303 return 0;
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);
323 return 0;
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;
330 unsigned long flags;
332 if (db_bits & ~ndev->db_valid_mask)
333 return -EINVAL;
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);
340 return 0;
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;
347 unsigned long flags;
349 if (db_bits & ~ndev->db_valid_mask)
350 return -EINVAL;
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);
357 return 0;
360 static int amd_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
362 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
363 void __iomem *mmio = ndev->self_mmio;
365 writew((u16)db_bits, mmio + AMD_DBREQ_OFFSET);
367 return 0;
370 static int amd_ntb_spad_count(struct ntb_dev *ntb)
372 return ntb_ndev(ntb)->spad_count;
375 static u32 amd_ntb_spad_read(struct ntb_dev *ntb, int idx)
377 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
378 void __iomem *mmio = ndev->self_mmio;
379 u32 offset;
381 if (idx < 0 || idx >= ndev->spad_count)
382 return 0;
384 offset = ndev->self_spad + (idx << 2);
385 return readl(mmio + AMD_SPAD_OFFSET + offset);
388 static int amd_ntb_spad_write(struct ntb_dev *ntb,
389 int idx, u32 val)
391 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
392 void __iomem *mmio = ndev->self_mmio;
393 u32 offset;
395 if (idx < 0 || idx >= ndev->spad_count)
396 return -EINVAL;
398 offset = ndev->self_spad + (idx << 2);
399 writel(val, mmio + AMD_SPAD_OFFSET + offset);
401 return 0;
404 static u32 amd_ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
406 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
407 void __iomem *mmio = ndev->self_mmio;
408 u32 offset;
410 if (idx < 0 || idx >= ndev->spad_count)
411 return -EINVAL;
413 offset = ndev->peer_spad + (idx << 2);
414 return readl(mmio + AMD_SPAD_OFFSET + offset);
417 static int amd_ntb_peer_spad_write(struct ntb_dev *ntb,
418 int idx, u32 val)
420 struct amd_ntb_dev *ndev = ntb_ndev(ntb);
421 void __iomem *mmio = ndev->self_mmio;
422 u32 offset;
424 if (idx < 0 || idx >= ndev->spad_count)
425 return -EINVAL;
427 offset = ndev->peer_spad + (idx << 2);
428 writel(val, mmio + AMD_SPAD_OFFSET + offset);
430 return 0;
433 static const struct ntb_dev_ops amd_ntb_ops = {
434 .mw_count = amd_ntb_mw_count,
435 .mw_get_range = amd_ntb_mw_get_range,
436 .mw_set_trans = amd_ntb_mw_set_trans,
437 .link_is_up = amd_ntb_link_is_up,
438 .link_enable = amd_ntb_link_enable,
439 .link_disable = amd_ntb_link_disable,
440 .db_valid_mask = amd_ntb_db_valid_mask,
441 .db_vector_count = amd_ntb_db_vector_count,
442 .db_vector_mask = amd_ntb_db_vector_mask,
443 .db_read = amd_ntb_db_read,
444 .db_clear = amd_ntb_db_clear,
445 .db_set_mask = amd_ntb_db_set_mask,
446 .db_clear_mask = amd_ntb_db_clear_mask,
447 .peer_db_set = amd_ntb_peer_db_set,
448 .spad_count = amd_ntb_spad_count,
449 .spad_read = amd_ntb_spad_read,
450 .spad_write = amd_ntb_spad_write,
451 .peer_spad_read = amd_ntb_peer_spad_read,
452 .peer_spad_write = amd_ntb_peer_spad_write,
455 static void amd_ack_smu(struct amd_ntb_dev *ndev, u32 bit)
457 void __iomem *mmio = ndev->self_mmio;
458 int reg;
460 reg = readl(mmio + AMD_SMUACK_OFFSET);
461 reg |= bit;
462 writel(reg, mmio + AMD_SMUACK_OFFSET);
464 ndev->peer_sta |= bit;
467 static void amd_handle_event(struct amd_ntb_dev *ndev, int vec)
469 void __iomem *mmio = ndev->self_mmio;
470 u32 status;
472 status = readl(mmio + AMD_INTSTAT_OFFSET);
473 if (!(status & AMD_EVENT_INTMASK))
474 return;
476 dev_dbg(ndev_dev(ndev), "status = 0x%x and vec = %d\n", status, vec);
478 status &= AMD_EVENT_INTMASK;
479 switch (status) {
480 case AMD_PEER_FLUSH_EVENT:
481 dev_info(ndev_dev(ndev), "Flush is done.\n");
482 break;
483 case AMD_PEER_RESET_EVENT:
484 amd_ack_smu(ndev, AMD_PEER_RESET_EVENT);
486 /* link down first */
487 ntb_link_event(&ndev->ntb);
488 /* polling peer status */
489 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
491 break;
492 case AMD_PEER_D3_EVENT:
493 case AMD_PEER_PMETO_EVENT:
494 amd_ack_smu(ndev, status);
496 /* link down */
497 ntb_link_event(&ndev->ntb);
499 break;
500 case AMD_PEER_D0_EVENT:
501 mmio = ndev->peer_mmio;
502 status = readl(mmio + AMD_PMESTAT_OFFSET);
503 /* check if this is WAKEUP event */
504 if (status & 0x1)
505 dev_info(ndev_dev(ndev), "Wakeup is done.\n");
507 amd_ack_smu(ndev, AMD_PEER_D0_EVENT);
509 /* start a timer to poll link status */
510 schedule_delayed_work(&ndev->hb_timer,
511 AMD_LINK_HB_TIMEOUT);
512 break;
513 default:
514 dev_info(ndev_dev(ndev), "event status = 0x%x.\n", status);
515 break;
519 static irqreturn_t ndev_interrupt(struct amd_ntb_dev *ndev, int vec)
521 dev_dbg(ndev_dev(ndev), "vec %d\n", vec);
523 if (vec > (AMD_DB_CNT - 1) || (ndev->msix_vec_count == 1))
524 amd_handle_event(ndev, vec);
526 if (vec < AMD_DB_CNT)
527 ntb_db_event(&ndev->ntb, vec);
529 return IRQ_HANDLED;
532 static irqreturn_t ndev_vec_isr(int irq, void *dev)
534 struct amd_ntb_vec *nvec = dev;
536 return ndev_interrupt(nvec->ndev, nvec->num);
539 static irqreturn_t ndev_irq_isr(int irq, void *dev)
541 struct amd_ntb_dev *ndev = dev;
543 return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
546 static int ndev_init_isr(struct amd_ntb_dev *ndev,
547 int msix_min, int msix_max)
549 struct pci_dev *pdev;
550 int rc, i, msix_count, node;
552 pdev = ndev_pdev(ndev);
554 node = dev_to_node(&pdev->dev);
556 ndev->db_mask = ndev->db_valid_mask;
558 /* Try to set up msix irq */
559 ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
560 GFP_KERNEL, node);
561 if (!ndev->vec)
562 goto err_msix_vec_alloc;
564 ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
565 GFP_KERNEL, node);
566 if (!ndev->msix)
567 goto err_msix_alloc;
569 for (i = 0; i < msix_max; ++i)
570 ndev->msix[i].entry = i;
572 msix_count = pci_enable_msix_range(pdev, ndev->msix,
573 msix_min, msix_max);
574 if (msix_count < 0)
575 goto err_msix_enable;
577 /* NOTE: Disable MSIX if msix count is less than 16 because of
578 * hardware limitation.
580 if (msix_count < msix_min) {
581 pci_disable_msix(pdev);
582 goto err_msix_enable;
585 for (i = 0; i < msix_count; ++i) {
586 ndev->vec[i].ndev = ndev;
587 ndev->vec[i].num = i;
588 rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
589 "ndev_vec_isr", &ndev->vec[i]);
590 if (rc)
591 goto err_msix_request;
594 dev_dbg(ndev_dev(ndev), "Using msix interrupts\n");
595 ndev->db_count = msix_min;
596 ndev->msix_vec_count = msix_max;
597 return 0;
599 err_msix_request:
600 while (i-- > 0)
601 free_irq(ndev->msix[i].vector, ndev);
602 pci_disable_msix(pdev);
603 err_msix_enable:
604 kfree(ndev->msix);
605 err_msix_alloc:
606 kfree(ndev->vec);
607 err_msix_vec_alloc:
608 ndev->msix = NULL;
609 ndev->vec = NULL;
611 /* Try to set up msi irq */
612 rc = pci_enable_msi(pdev);
613 if (rc)
614 goto err_msi_enable;
616 rc = request_irq(pdev->irq, ndev_irq_isr, 0,
617 "ndev_irq_isr", ndev);
618 if (rc)
619 goto err_msi_request;
621 dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
622 ndev->db_count = 1;
623 ndev->msix_vec_count = 1;
624 return 0;
626 err_msi_request:
627 pci_disable_msi(pdev);
628 err_msi_enable:
630 /* Try to set up intx irq */
631 pci_intx(pdev, 1);
633 rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
634 "ndev_irq_isr", ndev);
635 if (rc)
636 goto err_intx_request;
638 dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
639 ndev->db_count = 1;
640 ndev->msix_vec_count = 1;
641 return 0;
643 err_intx_request:
644 return rc;
647 static void ndev_deinit_isr(struct amd_ntb_dev *ndev)
649 struct pci_dev *pdev;
650 void __iomem *mmio = ndev->self_mmio;
651 int i;
653 pdev = ndev_pdev(ndev);
655 /* Mask all doorbell interrupts */
656 ndev->db_mask = ndev->db_valid_mask;
657 writel(ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
659 if (ndev->msix) {
660 i = ndev->msix_vec_count;
661 while (i--)
662 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
663 pci_disable_msix(pdev);
664 kfree(ndev->msix);
665 kfree(ndev->vec);
666 } else {
667 free_irq(pdev->irq, ndev);
668 if (pci_dev_msi_enabled(pdev))
669 pci_disable_msi(pdev);
670 else
671 pci_intx(pdev, 0);
675 static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
676 size_t count, loff_t *offp)
678 struct amd_ntb_dev *ndev;
679 void __iomem *mmio;
680 char *buf;
681 size_t buf_size;
682 ssize_t ret, off;
683 union { u64 v64; u32 v32; u16 v16; } u;
685 ndev = filp->private_data;
686 mmio = ndev->self_mmio;
688 buf_size = min(count, 0x800ul);
690 buf = kmalloc(buf_size, GFP_KERNEL);
691 if (!buf)
692 return -ENOMEM;
694 off = 0;
696 off += scnprintf(buf + off, buf_size - off,
697 "NTB Device Information:\n");
699 off += scnprintf(buf + off, buf_size - off,
700 "Connection Topology -\t%s\n",
701 ntb_topo_string(ndev->ntb.topo));
703 off += scnprintf(buf + off, buf_size - off,
704 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
706 if (!amd_link_is_up(ndev)) {
707 off += scnprintf(buf + off, buf_size - off,
708 "Link Status -\t\tDown\n");
709 } else {
710 off += scnprintf(buf + off, buf_size - off,
711 "Link Status -\t\tUp\n");
712 off += scnprintf(buf + off, buf_size - off,
713 "Link Speed -\t\tPCI-E Gen %u\n",
714 NTB_LNK_STA_SPEED(ndev->lnk_sta));
715 off += scnprintf(buf + off, buf_size - off,
716 "Link Width -\t\tx%u\n",
717 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
720 off += scnprintf(buf + off, buf_size - off,
721 "Memory Window Count -\t%u\n", ndev->mw_count);
722 off += scnprintf(buf + off, buf_size - off,
723 "Scratchpad Count -\t%u\n", ndev->spad_count);
724 off += scnprintf(buf + off, buf_size - off,
725 "Doorbell Count -\t%u\n", ndev->db_count);
726 off += scnprintf(buf + off, buf_size - off,
727 "MSIX Vector Count -\t%u\n", ndev->msix_vec_count);
729 off += scnprintf(buf + off, buf_size - off,
730 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
732 u.v32 = readl(ndev->self_mmio + AMD_DBMASK_OFFSET);
733 off += scnprintf(buf + off, buf_size - off,
734 "Doorbell Mask -\t\t\t%#06x\n", u.v32);
736 u.v32 = readl(mmio + AMD_DBSTAT_OFFSET);
737 off += scnprintf(buf + off, buf_size - off,
738 "Doorbell Bell -\t\t\t%#06x\n", u.v32);
740 off += scnprintf(buf + off, buf_size - off,
741 "\nNTB Incoming XLAT:\n");
743 u.v64 = read64(mmio + AMD_BAR1XLAT_OFFSET);
744 off += scnprintf(buf + off, buf_size - off,
745 "XLAT1 -\t\t%#018llx\n", u.v64);
747 u.v64 = read64(ndev->self_mmio + AMD_BAR23XLAT_OFFSET);
748 off += scnprintf(buf + off, buf_size - off,
749 "XLAT23 -\t\t%#018llx\n", u.v64);
751 u.v64 = read64(ndev->self_mmio + AMD_BAR45XLAT_OFFSET);
752 off += scnprintf(buf + off, buf_size - off,
753 "XLAT45 -\t\t%#018llx\n", u.v64);
755 u.v32 = readl(mmio + AMD_BAR1LMT_OFFSET);
756 off += scnprintf(buf + off, buf_size - off,
757 "LMT1 -\t\t\t%#06x\n", u.v32);
759 u.v64 = read64(ndev->self_mmio + AMD_BAR23LMT_OFFSET);
760 off += scnprintf(buf + off, buf_size - off,
761 "LMT23 -\t\t\t%#018llx\n", u.v64);
763 u.v64 = read64(ndev->self_mmio + AMD_BAR45LMT_OFFSET);
764 off += scnprintf(buf + off, buf_size - off,
765 "LMT45 -\t\t\t%#018llx\n", u.v64);
767 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
768 kfree(buf);
769 return ret;
772 static void ndev_init_debugfs(struct amd_ntb_dev *ndev)
774 if (!debugfs_dir) {
775 ndev->debugfs_dir = NULL;
776 ndev->debugfs_info = NULL;
777 } else {
778 ndev->debugfs_dir =
779 debugfs_create_dir(ndev_name(ndev), debugfs_dir);
780 if (!ndev->debugfs_dir)
781 ndev->debugfs_info = NULL;
782 else
783 ndev->debugfs_info =
784 debugfs_create_file("info", S_IRUSR,
785 ndev->debugfs_dir, ndev,
786 &amd_ntb_debugfs_info);
790 static void ndev_deinit_debugfs(struct amd_ntb_dev *ndev)
792 debugfs_remove_recursive(ndev->debugfs_dir);
795 static inline void ndev_init_struct(struct amd_ntb_dev *ndev,
796 struct pci_dev *pdev)
798 ndev->ntb.pdev = pdev;
799 ndev->ntb.topo = NTB_TOPO_NONE;
800 ndev->ntb.ops = &amd_ntb_ops;
801 ndev->int_mask = AMD_EVENT_INTMASK;
802 spin_lock_init(&ndev->db_mask_lock);
805 static int amd_poll_link(struct amd_ntb_dev *ndev)
807 void __iomem *mmio = ndev->peer_mmio;
808 u32 reg, stat;
809 int rc;
811 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
812 reg &= NTB_LIN_STA_ACTIVE_BIT;
814 dev_dbg(ndev_dev(ndev), "%s: reg_val = 0x%x.\n", __func__, reg);
816 if (reg == ndev->cntl_sta)
817 return 0;
819 ndev->cntl_sta = reg;
821 rc = pci_read_config_dword(ndev->ntb.pdev,
822 AMD_LINK_STATUS_OFFSET, &stat);
823 if (rc)
824 return 0;
825 ndev->lnk_sta = stat;
827 return 1;
830 static void amd_link_hb(struct work_struct *work)
832 struct amd_ntb_dev *ndev = hb_ndev(work);
834 if (amd_poll_link(ndev))
835 ntb_link_event(&ndev->ntb);
837 if (!amd_link_is_up(ndev))
838 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
841 static int amd_init_isr(struct amd_ntb_dev *ndev)
843 return ndev_init_isr(ndev, AMD_DB_CNT, AMD_MSIX_VECTOR_CNT);
846 static void amd_init_side_info(struct amd_ntb_dev *ndev)
848 void __iomem *mmio = ndev->self_mmio;
849 unsigned int reg;
851 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
852 if (!(reg & AMD_SIDE_READY)) {
853 reg |= AMD_SIDE_READY;
854 writel(reg, mmio + AMD_SIDEINFO_OFFSET);
858 static void amd_deinit_side_info(struct amd_ntb_dev *ndev)
860 void __iomem *mmio = ndev->self_mmio;
861 unsigned int reg;
863 reg = readl(mmio + AMD_SIDEINFO_OFFSET);
864 if (reg & AMD_SIDE_READY) {
865 reg &= ~AMD_SIDE_READY;
866 writel(reg, mmio + AMD_SIDEINFO_OFFSET);
867 readl(mmio + AMD_SIDEINFO_OFFSET);
871 static int amd_init_ntb(struct amd_ntb_dev *ndev)
873 void __iomem *mmio = ndev->self_mmio;
875 ndev->mw_count = AMD_MW_CNT;
876 ndev->spad_count = AMD_SPADS_CNT;
877 ndev->db_count = AMD_DB_CNT;
879 switch (ndev->ntb.topo) {
880 case NTB_TOPO_PRI:
881 case NTB_TOPO_SEC:
882 ndev->spad_count >>= 1;
883 if (ndev->ntb.topo == NTB_TOPO_PRI) {
884 ndev->self_spad = 0;
885 ndev->peer_spad = 0x20;
886 } else {
887 ndev->self_spad = 0x20;
888 ndev->peer_spad = 0;
891 INIT_DELAYED_WORK(&ndev->hb_timer, amd_link_hb);
892 schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
894 break;
895 default:
896 dev_err(ndev_dev(ndev), "AMD NTB does not support B2B mode.\n");
897 return -EINVAL;
900 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
902 /* Mask event interrupts */
903 writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
905 return 0;
908 static enum ntb_topo amd_get_topo(struct amd_ntb_dev *ndev)
910 void __iomem *mmio = ndev->self_mmio;
911 u32 info;
913 info = readl(mmio + AMD_SIDEINFO_OFFSET);
914 if (info & AMD_SIDE_MASK)
915 return NTB_TOPO_SEC;
916 else
917 return NTB_TOPO_PRI;
920 static int amd_init_dev(struct amd_ntb_dev *ndev)
922 struct pci_dev *pdev;
923 int rc = 0;
925 pdev = ndev_pdev(ndev);
927 ndev->ntb.topo = amd_get_topo(ndev);
928 dev_dbg(ndev_dev(ndev), "AMD NTB topo is %s\n",
929 ntb_topo_string(ndev->ntb.topo));
931 rc = amd_init_ntb(ndev);
932 if (rc)
933 return rc;
935 rc = amd_init_isr(ndev);
936 if (rc) {
937 dev_err(ndev_dev(ndev), "fail to init isr.\n");
938 return rc;
941 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
943 return 0;
946 static void amd_deinit_dev(struct amd_ntb_dev *ndev)
948 cancel_delayed_work_sync(&ndev->hb_timer);
950 ndev_deinit_isr(ndev);
953 static int amd_ntb_init_pci(struct amd_ntb_dev *ndev,
954 struct pci_dev *pdev)
956 int rc;
958 pci_set_drvdata(pdev, ndev);
960 rc = pci_enable_device(pdev);
961 if (rc)
962 goto err_pci_enable;
964 rc = pci_request_regions(pdev, NTB_NAME);
965 if (rc)
966 goto err_pci_regions;
968 pci_set_master(pdev);
970 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
971 if (rc) {
972 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
973 if (rc)
974 goto err_dma_mask;
975 dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
978 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
979 if (rc) {
980 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
981 if (rc)
982 goto err_dma_mask;
983 dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
986 ndev->self_mmio = pci_iomap(pdev, 0, 0);
987 if (!ndev->self_mmio) {
988 rc = -EIO;
989 goto err_dma_mask;
991 ndev->peer_mmio = ndev->self_mmio + AMD_PEER_OFFSET;
993 return 0;
995 err_dma_mask:
996 pci_clear_master(pdev);
997 err_pci_regions:
998 pci_disable_device(pdev);
999 err_pci_enable:
1000 pci_set_drvdata(pdev, NULL);
1001 return rc;
1004 static void amd_ntb_deinit_pci(struct amd_ntb_dev *ndev)
1006 struct pci_dev *pdev = ndev_pdev(ndev);
1008 pci_iounmap(pdev, ndev->self_mmio);
1010 pci_clear_master(pdev);
1011 pci_release_regions(pdev);
1012 pci_disable_device(pdev);
1013 pci_set_drvdata(pdev, NULL);
1016 static int amd_ntb_pci_probe(struct pci_dev *pdev,
1017 const struct pci_device_id *id)
1019 struct amd_ntb_dev *ndev;
1020 int rc, node;
1022 node = dev_to_node(&pdev->dev);
1024 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
1025 if (!ndev) {
1026 rc = -ENOMEM;
1027 goto err_ndev;
1030 ndev_init_struct(ndev, pdev);
1032 rc = amd_ntb_init_pci(ndev, pdev);
1033 if (rc)
1034 goto err_init_pci;
1036 rc = amd_init_dev(ndev);
1037 if (rc)
1038 goto err_init_dev;
1040 /* write side info */
1041 amd_init_side_info(ndev);
1043 amd_poll_link(ndev);
1045 ndev_init_debugfs(ndev);
1047 rc = ntb_register_device(&ndev->ntb);
1048 if (rc)
1049 goto err_register;
1051 dev_info(&pdev->dev, "NTB device registered.\n");
1053 return 0;
1055 err_register:
1056 ndev_deinit_debugfs(ndev);
1057 amd_deinit_dev(ndev);
1058 err_init_dev:
1059 amd_ntb_deinit_pci(ndev);
1060 err_init_pci:
1061 kfree(ndev);
1062 err_ndev:
1063 return rc;
1066 static void amd_ntb_pci_remove(struct pci_dev *pdev)
1068 struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
1070 ntb_unregister_device(&ndev->ntb);
1071 ndev_deinit_debugfs(ndev);
1072 amd_deinit_side_info(ndev);
1073 amd_deinit_dev(ndev);
1074 amd_ntb_deinit_pci(ndev);
1075 kfree(ndev);
1078 static const struct file_operations amd_ntb_debugfs_info = {
1079 .owner = THIS_MODULE,
1080 .open = simple_open,
1081 .read = ndev_debugfs_read,
1084 static const struct pci_device_id amd_ntb_pci_tbl[] = {
1085 {PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NTB)},
1088 MODULE_DEVICE_TABLE(pci, amd_ntb_pci_tbl);
1090 static struct pci_driver amd_ntb_pci_driver = {
1091 .name = KBUILD_MODNAME,
1092 .id_table = amd_ntb_pci_tbl,
1093 .probe = amd_ntb_pci_probe,
1094 .remove = amd_ntb_pci_remove,
1097 static int __init amd_ntb_pci_driver_init(void)
1099 pr_info("%s %s\n", NTB_DESC, NTB_VER);
1101 if (debugfs_initialized())
1102 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1104 return pci_register_driver(&amd_ntb_pci_driver);
1106 module_init(amd_ntb_pci_driver_init);
1108 static void __exit amd_ntb_pci_driver_exit(void)
1110 pci_unregister_driver(&amd_ntb_pci_driver);
1111 debugfs_remove_recursive(debugfs_dir);
1113 module_exit(amd_ntb_pci_driver_exit);