blob: 18f0ed8b1f93b8fab25824162f86bac9beb2d781 [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Stefano Stabellini183d03c2010-05-17 17:08:21 +01002/******************************************************************************
3 * platform-pci.c
4 *
5 * Xen platform PCI device driver
Paul Gortmakere01dc532016-02-21 19:06:08 -05006 *
7 * Authors: ssmith@xensource.com and stefano.stabellini@eu.citrix.com
8 *
Stefano Stabellini183d03c2010-05-17 17:08:21 +01009 * Copyright (c) 2005, Intel Corporation.
10 * Copyright (c) 2007, XenSource Inc.
11 * Copyright (c) 2010, Citrix
Stefano Stabellini183d03c2010-05-17 17:08:21 +010012 */
13
14
15#include <linux/interrupt.h>
16#include <linux/io.h>
Paul Gortmakere01dc532016-02-21 19:06:08 -050017#include <linux/init.h>
Stefano Stabellini183d03c2010-05-17 17:08:21 +010018#include <linux/pci.h>
19
Stefano Stabellinic1c54132010-05-14 12:44:30 +010020#include <xen/platform_pci.h>
Stefano Stabellini183d03c2010-05-17 17:08:21 +010021#include <xen/grant_table.h>
22#include <xen/xenbus.h>
23#include <xen/events.h>
24#include <xen/hvm.h>
Stefano Stabellini016b6f52010-05-14 12:45:07 +010025#include <xen/xen-ops.h>
Stefano Stabellini183d03c2010-05-17 17:08:21 +010026
27#define DRV_NAME "xen-platform-pci"
28
Stefano Stabellini183d03c2010-05-17 17:08:21 +010029static unsigned long platform_mmio;
30static unsigned long platform_mmio_alloc;
31static unsigned long platform_mmiolen;
Stefano Stabellinida72ff52017-01-13 10:07:23 -080032static uint64_t callback_via;
Stefano Stabellini183d03c2010-05-17 17:08:21 +010033
Rashika Kheria598ddb82014-02-09 16:29:30 +053034static unsigned long alloc_xen_mmio(unsigned long len)
Stefano Stabellini183d03c2010-05-17 17:08:21 +010035{
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 Stabellinida72ff52017-01-13 10:07:23 -080045static 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
64static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
65{
66 xen_hvm_evtchn_do_upcall();
67 return IRQ_HANDLED;
68}
69
70static 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 Helgaas77b84bb2019-11-01 08:20:40 -050077static int platform_pci_resume(struct device *dev)
Stefano Stabellinida72ff52017-01-13 10:07:23 -080078{
79 int err;
Boris Ostrovsky84d582d2017-04-24 15:04:53 -040080
81 if (xen_have_vector_callback)
Stefano Stabellinida72ff52017-01-13 10:07:23 -080082 return 0;
Boris Ostrovsky84d582d2017-04-24 15:04:53 -040083
Stefano Stabellinida72ff52017-01-13 10:07:23 -080084 err = xen_set_callback_via(callback_via);
85 if (err) {
Bjorn Helgaas77b84bb2019-11-01 08:20:40 -050086 dev_err(dev, "platform_pci_resume failure!\n");
Stefano Stabellinida72ff52017-01-13 10:07:23 -080087 return err;
88 }
89 return 0;
90}
91
Paul Gortmakere01dc532016-02-21 19:06:08 -050092static int platform_pci_probe(struct pci_dev *pdev,
93 const struct pci_device_id *ent)
Stefano Stabellini183d03c2010-05-17 17:08:21 +010094{
95 int i, ret;
Ian Campbell00f28e42011-01-11 11:50:28 +000096 long ioaddr;
Stefano Stabellini183d03c2010-05-17 17:08:21 +010097 long mmio_addr, mmio_len;
Stefano Stabellini183d03c2010-05-17 17:08:21 +010098 unsigned int max_nr_gframes;
Konrad Rzeszutek Wilkefaf30a2014-01-06 10:40:36 -050099 unsigned long grant_frames;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100100
Olaf Hering38ad4f42012-07-10 15:31:39 +0200101 if (!xen_domain())
102 return -ENODEV;
103
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100104 i = pci_enable_device(pdev);
105 if (i)
106 return i;
107
108 ioaddr = pci_resource_start(pdev, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100109
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 Campbell00f28e42011-01-11 11:50:28 +0000119 ret = pci_request_region(pdev, 1, DRV_NAME);
120 if (ret < 0)
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100121 goto pci_out;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100122
Ian Campbell00f28e42011-01-11 11:50:28 +0000123 ret = pci_request_region(pdev, 0, DRV_NAME);
124 if (ret < 0)
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100125 goto mem_out;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100126
127 platform_mmio = mmio_addr;
128 platform_mmiolen = mmio_len;
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400129 if (!xen_have_vector_callback) {
Stefano Stabellinida72ff52017-01-13 10:07:23 -0800130 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 Woodhouse8f4fd862021-01-06 15:39:55 +0000135 /*
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 Stabellinida72ff52017-01-13 10:07:23 -0800142 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 Stabellini183d03c2010-05-17 17:08:21 +0100151 max_nr_gframes = gnttab_max_grant_frames();
Konrad Rzeszutek Wilkefaf30a2014-01-06 10:40:36 -0500152 grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
Wei Yongjun89b9e082014-01-07 21:11:05 +0800153 ret = gnttab_setup_auto_xlat_frames(grant_frames);
154 if (ret)
Konrad Rzeszutek Wilkefaf30a2014-01-06 10:40:36 -0500155 goto out;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100156 ret = gnttab_init();
157 if (ret)
Konrad Rzeszutek Wilkefaf30a2014-01-06 10:40:36 -0500158 goto grant_out;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100159 return 0;
Konrad Rzeszutek Wilkefaf30a2014-01-06 10:40:36 -0500160grant_out:
161 gnttab_free_auto_xlat_frames();
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100162out:
Ian Campbell00f28e42011-01-11 11:50:28 +0000163 pci_release_region(pdev, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100164mem_out:
Ian Campbell00f28e42011-01-11 11:50:28 +0000165 pci_release_region(pdev, 1);
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100166pci_out:
167 pci_disable_device(pdev);
168 return ret;
169}
170
Arvind Yadavfff219d2017-08-02 23:16:57 +0530171static const struct pci_device_id platform_pci_tbl[] = {
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100172 {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
173 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
174 {0,}
175};
176
Rikard Falkebornc17db642020-05-09 15:47:55 +0200177static const struct dev_pm_ops platform_pm_ops = {
Bjorn Helgaas77b84bb2019-11-01 08:20:40 -0500178 .resume_noirq = platform_pci_resume,
179};
180
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100181static struct pci_driver platform_driver = {
182 .name = DRV_NAME,
Paul Gortmakere01dc532016-02-21 19:06:08 -0500183 .probe = platform_pci_probe,
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100184 .id_table = platform_pci_tbl,
Bjorn Helgaas77b84bb2019-11-01 08:20:40 -0500185 .driver = {
186 .pm = &platform_pm_ops,
187 },
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100188};
189
Geliang Tang1ea55e82016-11-14 20:52:26 +0800190builtin_pci_driver(platform_driver);