xref: /openbmc/linux/drivers/xen/platform-pci.c (revision 84e85359)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3  * platform-pci.c
4  *
5  * Xen platform PCI device driver
6  *
7  * Authors: ssmith@xensource.com and stefano.stabellini@eu.citrix.com
8  *
9  * Copyright (c) 2005, Intel Corporation.
10  * Copyright (c) 2007, XenSource Inc.
11  * Copyright (c) 2010, Citrix
12  */
13 
14 
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 
20 #include <xen/platform_pci.h>
21 #include <xen/grant_table.h>
22 #include <xen/xenbus.h>
23 #include <xen/events.h>
24 #include <xen/hvm.h>
25 #include <xen/xen-ops.h>
26 
27 #define DRV_NAME    "xen-platform-pci"
28 
29 static unsigned long platform_mmio;
30 static unsigned long platform_mmio_alloc;
31 static unsigned long platform_mmiolen;
32 static uint64_t callback_via;
33 
34 static unsigned long alloc_xen_mmio(unsigned long len)
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 
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)HVM_PARAM_CALLBACK_TYPE_PCI_INTX <<
58 			  HVM_CALLBACK_VIA_TYPE_SHIFT) |
59 		((uint64_t)pci_domain_nr(pdev->bus) << 32) |
60 		((uint64_t)pdev->bus->number << 16) |
61 		((uint64_t)(pdev->devfn & 0xff) << 8) |
62 		((uint64_t)(pin - 1) & 3);
63 }
64 
65 static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
66 {
67 	xen_hvm_evtchn_do_upcall();
68 	return IRQ_HANDLED;
69 }
70 
71 static int xen_allocate_irq(struct pci_dev *pdev)
72 {
73 	return request_irq(pdev->irq, do_hvm_evtchn_intr,
74 			IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
75 			"xen-platform-pci", pdev);
76 }
77 
78 static int platform_pci_resume(struct device *dev)
79 {
80 	int err;
81 
82 	if (xen_have_vector_callback)
83 		return 0;
84 
85 	err = xen_set_callback_via(callback_via);
86 	if (err) {
87 		dev_err(dev, "platform_pci_resume failure!\n");
88 		return err;
89 	}
90 	return 0;
91 }
92 
93 static int platform_pci_probe(struct pci_dev *pdev,
94 			      const struct pci_device_id *ent)
95 {
96 	int i, ret;
97 	long ioaddr;
98 	long mmio_addr, mmio_len;
99 	unsigned int max_nr_gframes;
100 	unsigned long grant_frames;
101 
102 	if (!xen_domain())
103 		return -ENODEV;
104 
105 	i = pci_enable_device(pdev);
106 	if (i)
107 		return i;
108 
109 	ioaddr = pci_resource_start(pdev, 0);
110 
111 	mmio_addr = pci_resource_start(pdev, 1);
112 	mmio_len = pci_resource_len(pdev, 1);
113 
114 	if (mmio_addr == 0 || ioaddr == 0) {
115 		dev_err(&pdev->dev, "no resources found\n");
116 		ret = -ENOENT;
117 		goto pci_out;
118 	}
119 
120 	ret = pci_request_region(pdev, 1, DRV_NAME);
121 	if (ret < 0)
122 		goto pci_out;
123 
124 	ret = pci_request_region(pdev, 0, DRV_NAME);
125 	if (ret < 0)
126 		goto mem_out;
127 
128 	platform_mmio = mmio_addr;
129 	platform_mmiolen = mmio_len;
130 	if (!xen_have_vector_callback) {
131 		ret = xen_allocate_irq(pdev);
132 		if (ret) {
133 			dev_warn(&pdev->dev, "request_irq failed err=%d\n", ret);
134 			goto out;
135 		}
136 		/*
137 		 * It doesn't strictly *have* to run on CPU0 but it sure
138 		 * as hell better process the event channel ports delivered
139 		 * to CPU0.
140 		 */
141 		irq_set_affinity(pdev->irq, cpumask_of(0));
142 
143 		callback_via = get_callback_via(pdev);
144 		ret = xen_set_callback_via(callback_via);
145 		if (ret) {
146 			dev_warn(&pdev->dev, "Unable to set the evtchn callback "
147 					 "err=%d\n", ret);
148 			goto irq_out;
149 		}
150 	}
151 
152 	max_nr_gframes = gnttab_max_grant_frames();
153 	grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
154 	ret = gnttab_setup_auto_xlat_frames(grant_frames);
155 	if (ret)
156 		goto irq_out;
157 	ret = gnttab_init();
158 	if (ret)
159 		goto grant_out;
160 	return 0;
161 grant_out:
162 	gnttab_free_auto_xlat_frames();
163 irq_out:
164 	if (!xen_have_vector_callback)
165 		free_irq(pdev->irq, pdev);
166 out:
167 	pci_release_region(pdev, 0);
168 mem_out:
169 	pci_release_region(pdev, 1);
170 pci_out:
171 	pci_disable_device(pdev);
172 	return ret;
173 }
174 
175 static const struct pci_device_id platform_pci_tbl[] = {
176 	{PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
177 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
178 	{0,}
179 };
180 
181 static const struct dev_pm_ops platform_pm_ops = {
182 	.resume_noirq =   platform_pci_resume,
183 };
184 
185 static struct pci_driver platform_driver = {
186 	.name =           DRV_NAME,
187 	.probe =          platform_pci_probe,
188 	.id_table =       platform_pci_tbl,
189 	.driver = {
190 		.pm =     &platform_pm_ops,
191 	},
192 };
193 
194 builtin_pci_driver(platform_driver);
195