Thomas Gleixner | 3b20eb2 | 2019-05-29 16:57:35 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 2 | /****************************************************************************** |
| 3 | * platform-pci.c |
| 4 | * |
| 5 | * Xen platform PCI device driver |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 6 | * |
| 7 | * Authors: ssmith@xensource.com and stefano.stabellini@eu.citrix.com |
| 8 | * |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 9 | * Copyright (c) 2005, Intel Corporation. |
| 10 | * Copyright (c) 2007, XenSource Inc. |
| 11 | * Copyright (c) 2010, Citrix |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/io.h> |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 17 | #include <linux/init.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 18 | #include <linux/pci.h> |
| 19 | |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 20 | #include <xen/platform_pci.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 21 | #include <xen/grant_table.h> |
| 22 | #include <xen/xenbus.h> |
| 23 | #include <xen/events.h> |
| 24 | #include <xen/hvm.h> |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 25 | #include <xen/xen-ops.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 26 | |
| 27 | #define DRV_NAME "xen-platform-pci" |
| 28 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 29 | static unsigned long platform_mmio; |
| 30 | static unsigned long platform_mmio_alloc; |
| 31 | static unsigned long platform_mmiolen; |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 32 | static uint64_t callback_via; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 33 | |
Rashika Kheria | 598ddb8 | 2014-02-09 16:29:30 +0530 | [diff] [blame] | 34 | static unsigned long alloc_xen_mmio(unsigned long len) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 35 | { |
| 36 | unsigned long addr; |
| 37 | |
| 38 | addr = platform_mmio + platform_mmio_alloc; |
| 39 | platform_mmio_alloc += len; |
| 40 | BUG_ON(platform_mmio_alloc > platform_mmiolen); |
| 41 | |
| 42 | return addr; |
| 43 | } |
| 44 | |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 45 | static uint64_t get_callback_via(struct pci_dev *pdev) |
| 46 | { |
| 47 | u8 pin; |
| 48 | int irq; |
| 49 | |
| 50 | irq = pdev->irq; |
| 51 | if (irq < 16) |
| 52 | return irq; /* ISA IRQ */ |
| 53 | |
| 54 | pin = pdev->pin; |
| 55 | |
| 56 | /* We don't know the GSI. Specify the PCI INTx line instead. */ |
| 57 | return ((uint64_t)0x01 << HVM_CALLBACK_VIA_TYPE_SHIFT) | /* PCI INTx identifier */ |
| 58 | ((uint64_t)pci_domain_nr(pdev->bus) << 32) | |
| 59 | ((uint64_t)pdev->bus->number << 16) | |
| 60 | ((uint64_t)(pdev->devfn & 0xff) << 8) | |
| 61 | ((uint64_t)(pin - 1) & 3); |
| 62 | } |
| 63 | |
| 64 | static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id) |
| 65 | { |
| 66 | xen_hvm_evtchn_do_upcall(); |
| 67 | return IRQ_HANDLED; |
| 68 | } |
| 69 | |
| 70 | static int xen_allocate_irq(struct pci_dev *pdev) |
| 71 | { |
| 72 | return request_irq(pdev->irq, do_hvm_evtchn_intr, |
| 73 | IRQF_NOBALANCING | IRQF_TRIGGER_RISING, |
| 74 | "xen-platform-pci", pdev); |
| 75 | } |
| 76 | |
Bjorn Helgaas | 77b84bb | 2019-11-01 08:20:40 -0500 | [diff] [blame] | 77 | static int platform_pci_resume(struct device *dev) |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 78 | { |
| 79 | int err; |
Boris Ostrovsky | 84d582d | 2017-04-24 15:04:53 -0400 | [diff] [blame] | 80 | |
| 81 | if (xen_have_vector_callback) |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 82 | return 0; |
Boris Ostrovsky | 84d582d | 2017-04-24 15:04:53 -0400 | [diff] [blame] | 83 | |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 84 | err = xen_set_callback_via(callback_via); |
| 85 | if (err) { |
Bjorn Helgaas | 77b84bb | 2019-11-01 08:20:40 -0500 | [diff] [blame] | 86 | dev_err(dev, "platform_pci_resume failure!\n"); |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 87 | return err; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 92 | static int platform_pci_probe(struct pci_dev *pdev, |
| 93 | const struct pci_device_id *ent) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 94 | { |
| 95 | int i, ret; |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 96 | long ioaddr; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 97 | long mmio_addr, mmio_len; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 98 | unsigned int max_nr_gframes; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 99 | unsigned long grant_frames; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 100 | |
Olaf Hering | 38ad4f4 | 2012-07-10 15:31:39 +0200 | [diff] [blame] | 101 | if (!xen_domain()) |
| 102 | return -ENODEV; |
| 103 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 104 | i = pci_enable_device(pdev); |
| 105 | if (i) |
| 106 | return i; |
| 107 | |
| 108 | ioaddr = pci_resource_start(pdev, 0); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 109 | |
| 110 | mmio_addr = pci_resource_start(pdev, 1); |
| 111 | mmio_len = pci_resource_len(pdev, 1); |
| 112 | |
| 113 | if (mmio_addr == 0 || ioaddr == 0) { |
| 114 | dev_err(&pdev->dev, "no resources found\n"); |
| 115 | ret = -ENOENT; |
| 116 | goto pci_out; |
| 117 | } |
| 118 | |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 119 | ret = pci_request_region(pdev, 1, DRV_NAME); |
| 120 | if (ret < 0) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 121 | goto pci_out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 122 | |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 123 | ret = pci_request_region(pdev, 0, DRV_NAME); |
| 124 | if (ret < 0) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 125 | goto mem_out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 126 | |
| 127 | platform_mmio = mmio_addr; |
| 128 | platform_mmiolen = mmio_len; |
Boris Ostrovsky | 84d582d | 2017-04-24 15:04:53 -0400 | [diff] [blame] | 129 | if (!xen_have_vector_callback) { |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 130 | ret = xen_allocate_irq(pdev); |
| 131 | if (ret) { |
| 132 | dev_warn(&pdev->dev, "request_irq failed err=%d\n", ret); |
| 133 | goto out; |
| 134 | } |
David Woodhouse | 8f4fd86 | 2021-01-06 15:39:55 +0000 | [diff] [blame] | 135 | /* |
| 136 | * It doesn't strictly *have* to run on CPU0 but it sure |
| 137 | * as hell better process the event channel ports delivered |
| 138 | * to CPU0. |
| 139 | */ |
| 140 | irq_set_affinity(pdev->irq, cpumask_of(0)); |
| 141 | |
Stefano Stabellini | da72ff5 | 2017-01-13 10:07:23 -0800 | [diff] [blame] | 142 | callback_via = get_callback_via(pdev); |
| 143 | ret = xen_set_callback_via(callback_via); |
| 144 | if (ret) { |
| 145 | dev_warn(&pdev->dev, "Unable to set the evtchn callback " |
| 146 | "err=%d\n", ret); |
| 147 | goto out; |
| 148 | } |
| 149 | } |
| 150 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 151 | max_nr_gframes = gnttab_max_grant_frames(); |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 152 | grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes); |
Wei Yongjun | 89b9e08 | 2014-01-07 21:11:05 +0800 | [diff] [blame] | 153 | ret = gnttab_setup_auto_xlat_frames(grant_frames); |
| 154 | if (ret) |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 155 | goto out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 156 | ret = gnttab_init(); |
| 157 | if (ret) |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 158 | goto grant_out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 159 | return 0; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 160 | grant_out: |
| 161 | gnttab_free_auto_xlat_frames(); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 162 | out: |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 163 | pci_release_region(pdev, 0); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 164 | mem_out: |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 165 | pci_release_region(pdev, 1); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 166 | pci_out: |
| 167 | pci_disable_device(pdev); |
| 168 | return ret; |
| 169 | } |
| 170 | |
Arvind Yadav | fff219d | 2017-08-02 23:16:57 +0530 | [diff] [blame] | 171 | static const struct pci_device_id platform_pci_tbl[] = { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 172 | {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM, |
| 173 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
| 174 | {0,} |
| 175 | }; |
| 176 | |
Rikard Falkeborn | c17db64 | 2020-05-09 15:47:55 +0200 | [diff] [blame] | 177 | static const struct dev_pm_ops platform_pm_ops = { |
Bjorn Helgaas | 77b84bb | 2019-11-01 08:20:40 -0500 | [diff] [blame] | 178 | .resume_noirq = platform_pci_resume, |
| 179 | }; |
| 180 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 181 | static struct pci_driver platform_driver = { |
| 182 | .name = DRV_NAME, |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 183 | .probe = platform_pci_probe, |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 184 | .id_table = platform_pci_tbl, |
Bjorn Helgaas | 77b84bb | 2019-11-01 08:20:40 -0500 | [diff] [blame] | 185 | .driver = { |
| 186 | .pm = &platform_pm_ops, |
| 187 | }, |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 188 | }; |
| 189 | |
Geliang Tang | 1ea55e8 | 2016-11-14 20:52:26 +0800 | [diff] [blame] | 190 | builtin_pci_driver(platform_driver); |