linux_pci.c revision 1.25 1 1.25 mrg /* $NetBSD: linux_pci.c,v 1.25 2022/10/17 03:05:32 mrg Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.7 jmcneill #ifdef _KERNEL_OPT
33 1.17 riastrad #include "acpica.h"
34 1.7 jmcneill #include "opt_pci.h"
35 1.7 jmcneill #endif
36 1.7 jmcneill
37 1.1 riastrad #include <sys/cdefs.h>
38 1.25 mrg __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.25 2022/10/17 03:05:32 mrg Exp $");
39 1.16 riastrad
40 1.16 riastrad #if NACPICA > 0
41 1.16 riastrad #include <dev/acpi/acpivar.h>
42 1.16 riastrad #include <dev/acpi/acpi_pci.h>
43 1.16 riastrad #endif
44 1.1 riastrad
45 1.1 riastrad #include <linux/pci.h>
46 1.1 riastrad
47 1.5 riastrad #include <drm/drm_agp_netbsd.h>
48 1.5 riastrad
49 1.1 riastrad device_t
50 1.1 riastrad pci_dev_dev(struct pci_dev *pdev)
51 1.1 riastrad {
52 1.1 riastrad
53 1.1 riastrad return pdev->pd_dev;
54 1.1 riastrad }
55 1.1 riastrad
56 1.12 riastrad void
57 1.12 riastrad pci_set_drvdata(struct pci_dev *pdev, void *drvdata)
58 1.12 riastrad {
59 1.12 riastrad pdev->pd_drvdata = drvdata;
60 1.12 riastrad }
61 1.12 riastrad
62 1.12 riastrad void *
63 1.1 riastrad pci_get_drvdata(struct pci_dev *pdev)
64 1.1 riastrad {
65 1.12 riastrad return pdev->pd_drvdata;
66 1.1 riastrad }
67 1.1 riastrad
68 1.20 riastrad const char *
69 1.20 riastrad pci_name(struct pci_dev *pdev)
70 1.20 riastrad {
71 1.20 riastrad
72 1.20 riastrad /* XXX not sure this has the right format */
73 1.20 riastrad return device_xname(pci_dev_dev(pdev));
74 1.20 riastrad }
75 1.20 riastrad
76 1.25 mrg /*
77 1.25 mrg * Setup enough of a parent that we can access config space.
78 1.25 mrg * This is gross and grovels pci(4) and ppb(4) internals.
79 1.25 mrg */
80 1.25 mrg static struct pci_dev *
81 1.25 mrg alloc_fake_parent_device(device_t parent, const struct pci_attach_args *pa)
82 1.25 mrg {
83 1.25 mrg
84 1.25 mrg if (parent == NULL || !device_is_a(parent, "pci"))
85 1.25 mrg return NULL;
86 1.25 mrg
87 1.25 mrg device_t pparent = device_parent(parent);
88 1.25 mrg if (pparent == NULL || !device_is_a(pparent, "ppb"))
89 1.25 mrg return NULL;
90 1.25 mrg
91 1.25 mrg struct pci_softc *pcisc = device_private(parent);
92 1.25 mrg struct ppb_softc *ppbsc = device_private(pparent);
93 1.25 mrg
94 1.25 mrg struct pci_dev *parentdev = kmem_zalloc(sizeof(*parentdev), KM_SLEEP);
95 1.25 mrg
96 1.25 mrg /* Copy this device's pci_attach_args{} as a base-line. */
97 1.25 mrg struct pci_attach_args *npa = &parentdev->pd_pa;
98 1.25 mrg *npa = *pa;
99 1.25 mrg
100 1.25 mrg /* Now update with stuff found in parent. */
101 1.25 mrg npa->pa_iot = pcisc->sc_iot;
102 1.25 mrg npa->pa_memt = pcisc->sc_memt;
103 1.25 mrg npa->pa_dmat = pcisc->sc_dmat;
104 1.25 mrg npa->pa_dmat64 = pcisc->sc_dmat64;
105 1.25 mrg npa->pa_pc = pcisc->sc_pc;
106 1.25 mrg npa->pa_flags = 0; /* XXX? */
107 1.25 mrg
108 1.25 mrg /* Copy the parent tag, and read some info about it. */
109 1.25 mrg npa->pa_tag = ppbsc->sc_tag;
110 1.25 mrg pcireg_t id = pci_conf_read(npa->pa_pc, npa->pa_tag, PCI_ID_REG);
111 1.25 mrg pcireg_t subid = pci_conf_read(npa->pa_pc, npa->pa_tag,
112 1.25 mrg PCI_SUBSYS_ID_REG);
113 1.25 mrg pcireg_t class = pci_conf_read(npa->pa_pc, npa->pa_tag, PCI_CLASS_REG);
114 1.25 mrg
115 1.25 mrg /*
116 1.25 mrg * Fill in as much of pci_attach_args and pci_dev as reasonably possible.
117 1.25 mrg * Most of this is not used currently.
118 1.25 mrg */
119 1.25 mrg int bus, device, function;
120 1.25 mrg pci_decompose_tag(npa->pa_pc, npa->pa_tag, &bus, &device, &function);
121 1.25 mrg npa->pa_device = device;
122 1.25 mrg npa->pa_function = function;
123 1.25 mrg npa->pa_bus = bus;
124 1.25 mrg npa->pa_id = id;
125 1.25 mrg npa->pa_class = class;
126 1.25 mrg npa->pa_intrswiz = pcisc->sc_intrswiz;
127 1.25 mrg npa->pa_intrtag = pcisc->sc_intrtag;
128 1.25 mrg npa->pa_intrpin = PCI_INTERRUPT_PIN_NONE;
129 1.25 mrg
130 1.25 mrg parentdev->pd_dev = parent;
131 1.25 mrg
132 1.25 mrg parentdev->bus = NULL;
133 1.25 mrg parentdev->devfn = device << 3 | function;
134 1.25 mrg parentdev->vendor = PCI_VENDOR(id);
135 1.25 mrg parentdev->device = PCI_PRODUCT(id);
136 1.25 mrg parentdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subid);
137 1.25 mrg parentdev->subsystem_device = PCI_SUBSYS_ID(subid);
138 1.25 mrg parentdev->revision = PCI_REVISION(class);
139 1.25 mrg parentdev->class = __SHIFTOUT(class, 0xffffff00UL); /* ? */
140 1.25 mrg
141 1.25 mrg return parentdev;
142 1.25 mrg }
143 1.25 mrg
144 1.1 riastrad void
145 1.1 riastrad linux_pci_dev_init(struct pci_dev *pdev, device_t dev, device_t parent,
146 1.1 riastrad const struct pci_attach_args *pa, int kludges)
147 1.1 riastrad {
148 1.1 riastrad const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag,
149 1.1 riastrad PCI_SUBSYS_ID_REG);
150 1.1 riastrad unsigned i;
151 1.1 riastrad
152 1.3 riastrad memset(pdev, 0, sizeof(*pdev)); /* paranoia */
153 1.3 riastrad
154 1.1 riastrad pdev->pd_pa = *pa;
155 1.1 riastrad pdev->pd_kludges = kludges;
156 1.1 riastrad pdev->pd_rom_vaddr = NULL;
157 1.1 riastrad pdev->pd_dev = dev;
158 1.1 riastrad #if (NACPICA > 0)
159 1.7 jmcneill const int seg = pci_get_segment(pa->pa_pc);
160 1.7 jmcneill pdev->pd_ad = acpi_pcidev_find(seg, pa->pa_bus,
161 1.1 riastrad pa->pa_device, pa->pa_function);
162 1.1 riastrad #else
163 1.1 riastrad pdev->pd_ad = NULL;
164 1.1 riastrad #endif
165 1.1 riastrad pdev->pd_saved_state = NULL;
166 1.1 riastrad pdev->pd_intr_handles = NULL;
167 1.12 riastrad pdev->pd_drvdata = NULL;
168 1.1 riastrad pdev->bus = kmem_zalloc(sizeof(*pdev->bus), KM_NOSLEEP);
169 1.1 riastrad pdev->bus->pb_pc = pa->pa_pc;
170 1.1 riastrad pdev->bus->pb_dev = parent;
171 1.1 riastrad pdev->bus->number = pa->pa_bus;
172 1.25 mrg pdev->bus->self = alloc_fake_parent_device(parent, pa);
173 1.1 riastrad pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function);
174 1.1 riastrad pdev->vendor = PCI_VENDOR(pa->pa_id);
175 1.1 riastrad pdev->device = PCI_PRODUCT(pa->pa_id);
176 1.1 riastrad pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id);
177 1.1 riastrad pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id);
178 1.1 riastrad pdev->revision = PCI_REVISION(pa->pa_class);
179 1.1 riastrad pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */
180 1.1 riastrad
181 1.1 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
182 1.1 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) {
183 1.1 riastrad const int reg = PCI_BAR(i);
184 1.1 riastrad
185 1.1 riastrad pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc,
186 1.1 riastrad pa->pa_tag, reg);
187 1.1 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
188 1.1 riastrad pdev->pd_resources[i].type,
189 1.1 riastrad &pdev->pd_resources[i].addr,
190 1.1 riastrad &pdev->pd_resources[i].size,
191 1.1 riastrad &pdev->pd_resources[i].flags)) {
192 1.1 riastrad pdev->pd_resources[i].addr = 0;
193 1.1 riastrad pdev->pd_resources[i].size = 0;
194 1.1 riastrad pdev->pd_resources[i].flags = 0;
195 1.1 riastrad }
196 1.1 riastrad pdev->pd_resources[i].kva = NULL;
197 1.2 riastrad pdev->pd_resources[i].mapped = false;
198 1.1 riastrad }
199 1.1 riastrad }
200 1.1 riastrad
201 1.1 riastrad int
202 1.1 riastrad pci_find_capability(struct pci_dev *pdev, int cap)
203 1.1 riastrad {
204 1.1 riastrad
205 1.1 riastrad return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap,
206 1.1 riastrad NULL, NULL);
207 1.1 riastrad }
208 1.1 riastrad
209 1.1 riastrad int
210 1.1 riastrad pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
211 1.1 riastrad {
212 1.1 riastrad
213 1.1 riastrad KASSERT(!ISSET(reg, 3));
214 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg);
215 1.1 riastrad return 0;
216 1.1 riastrad }
217 1.1 riastrad
218 1.1 riastrad int
219 1.1 riastrad pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
220 1.1 riastrad {
221 1.1 riastrad
222 1.1 riastrad KASSERT(!ISSET(reg, 1));
223 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
224 1.1 riastrad (reg &~ 2)) >> (8 * (reg & 2));
225 1.1 riastrad return 0;
226 1.1 riastrad }
227 1.1 riastrad
228 1.1 riastrad int
229 1.1 riastrad pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep)
230 1.1 riastrad {
231 1.1 riastrad
232 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
233 1.1 riastrad (reg &~ 3)) >> (8 * (reg & 3));
234 1.1 riastrad return 0;
235 1.1 riastrad }
236 1.1 riastrad
237 1.1 riastrad int
238 1.1 riastrad pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value)
239 1.1 riastrad {
240 1.1 riastrad
241 1.1 riastrad KASSERT(!ISSET(reg, 3));
242 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value);
243 1.1 riastrad return 0;
244 1.1 riastrad }
245 1.1 riastrad
246 1.1 riastrad int
247 1.1 riastrad pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
248 1.1 riastrad uint32_t *valuep)
249 1.1 riastrad {
250 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
251 1.1 riastrad PCI_FUNC(devfn));
252 1.1 riastrad
253 1.1 riastrad KASSERT(!ISSET(reg, 1));
254 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3));
255 1.1 riastrad return 0;
256 1.1 riastrad }
257 1.1 riastrad
258 1.1 riastrad int
259 1.1 riastrad pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg,
260 1.1 riastrad uint16_t *valuep)
261 1.1 riastrad {
262 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
263 1.1 riastrad PCI_FUNC(devfn));
264 1.1 riastrad
265 1.1 riastrad KASSERT(!ISSET(reg, 1));
266 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2));
267 1.1 riastrad return 0;
268 1.1 riastrad }
269 1.1 riastrad
270 1.1 riastrad int
271 1.1 riastrad pci_bus_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
272 1.1 riastrad uint8_t *valuep)
273 1.1 riastrad {
274 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
275 1.1 riastrad PCI_FUNC(devfn));
276 1.1 riastrad
277 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3));
278 1.1 riastrad return 0;
279 1.1 riastrad }
280 1.1 riastrad
281 1.1 riastrad int
282 1.1 riastrad pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
283 1.1 riastrad uint32_t value)
284 1.1 riastrad {
285 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
286 1.1 riastrad PCI_FUNC(devfn));
287 1.1 riastrad
288 1.1 riastrad KASSERT(!ISSET(reg, 3));
289 1.1 riastrad pci_conf_write(bus->pb_pc, tag, reg, value);
290 1.1 riastrad return 0;
291 1.1 riastrad }
292 1.1 riastrad
293 1.1 riastrad static void
294 1.1 riastrad pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes,
295 1.1 riastrad uint32_t value)
296 1.1 riastrad {
297 1.1 riastrad const uint32_t mask = ~((~0UL) << (8 * bytes));
298 1.1 riastrad const int reg32 = (reg &~ 3);
299 1.1 riastrad const unsigned int shift = (8 * (reg & 3));
300 1.1 riastrad uint32_t value32;
301 1.1 riastrad
302 1.1 riastrad KASSERT(bytes <= 4);
303 1.1 riastrad KASSERT(!ISSET(value, ~mask));
304 1.1 riastrad value32 = pci_conf_read(pc, tag, reg32);
305 1.1 riastrad value32 &=~ (mask << shift);
306 1.1 riastrad value32 |= (value << shift);
307 1.1 riastrad pci_conf_write(pc, tag, reg32, value32);
308 1.1 riastrad }
309 1.1 riastrad
310 1.1 riastrad int
311 1.1 riastrad pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value)
312 1.1 riastrad {
313 1.1 riastrad
314 1.1 riastrad KASSERT(!ISSET(reg, 1));
315 1.1 riastrad pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value);
316 1.1 riastrad return 0;
317 1.1 riastrad }
318 1.1 riastrad
319 1.1 riastrad int
320 1.1 riastrad pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value)
321 1.1 riastrad {
322 1.1 riastrad
323 1.1 riastrad pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value);
324 1.1 riastrad return 0;
325 1.1 riastrad }
326 1.1 riastrad
327 1.1 riastrad int
328 1.1 riastrad pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg,
329 1.1 riastrad uint16_t value)
330 1.1 riastrad {
331 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
332 1.1 riastrad PCI_FUNC(devfn));
333 1.1 riastrad
334 1.1 riastrad KASSERT(!ISSET(reg, 1));
335 1.1 riastrad pci_rmw_config(bus->pb_pc, tag, reg, 2, value);
336 1.1 riastrad return 0;
337 1.1 riastrad }
338 1.1 riastrad
339 1.1 riastrad int
340 1.1 riastrad pci_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
341 1.1 riastrad uint8_t value)
342 1.1 riastrad {
343 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
344 1.1 riastrad PCI_FUNC(devfn));
345 1.1 riastrad
346 1.1 riastrad pci_rmw_config(bus->pb_pc, tag, reg, 1, value);
347 1.1 riastrad return 0;
348 1.1 riastrad }
349 1.1 riastrad
350 1.1 riastrad int
351 1.1 riastrad pci_enable_msi(struct pci_dev *pdev)
352 1.1 riastrad {
353 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
354 1.1 riastrad
355 1.1 riastrad if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1))
356 1.1 riastrad return -EINVAL;
357 1.1 riastrad
358 1.1 riastrad pdev->msi_enabled = 1;
359 1.1 riastrad return 0;
360 1.1 riastrad }
361 1.1 riastrad
362 1.1 riastrad void
363 1.1 riastrad pci_disable_msi(struct pci_dev *pdev __unused)
364 1.1 riastrad {
365 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
366 1.1 riastrad
367 1.1 riastrad if (pdev->pd_intr_handles != NULL) {
368 1.1 riastrad pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1);
369 1.1 riastrad pdev->pd_intr_handles = NULL;
370 1.1 riastrad }
371 1.1 riastrad pdev->msi_enabled = 0;
372 1.1 riastrad }
373 1.1 riastrad
374 1.1 riastrad void
375 1.1 riastrad pci_set_master(struct pci_dev *pdev)
376 1.1 riastrad {
377 1.1 riastrad pcireg_t csr;
378 1.1 riastrad
379 1.1 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
380 1.1 riastrad PCI_COMMAND_STATUS_REG);
381 1.1 riastrad csr |= PCI_COMMAND_MASTER_ENABLE;
382 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
383 1.1 riastrad PCI_COMMAND_STATUS_REG, csr);
384 1.1 riastrad }
385 1.1 riastrad
386 1.1 riastrad void
387 1.1 riastrad pci_clear_master(struct pci_dev *pdev)
388 1.1 riastrad {
389 1.1 riastrad pcireg_t csr;
390 1.1 riastrad
391 1.1 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
392 1.1 riastrad PCI_COMMAND_STATUS_REG);
393 1.1 riastrad csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE;
394 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
395 1.1 riastrad PCI_COMMAND_STATUS_REG, csr);
396 1.1 riastrad }
397 1.1 riastrad
398 1.25 mrg int
399 1.25 mrg pcie_capability_read_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
400 1.25 mrg {
401 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
402 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
403 1.25 mrg int off;
404 1.25 mrg
405 1.25 mrg *valuep = 0;
406 1.25 mrg
407 1.25 mrg /* Must have capabilities. */
408 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
409 1.25 mrg return 1;
410 1.25 mrg
411 1.25 mrg *valuep = pci_conf_read(pc, tag, off + reg);
412 1.25 mrg
413 1.25 mrg return 0;
414 1.25 mrg }
415 1.25 mrg
416 1.25 mrg int
417 1.25 mrg pcie_capability_read_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
418 1.25 mrg {
419 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
420 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
421 1.25 mrg int off;
422 1.25 mrg
423 1.25 mrg *valuep = 0;
424 1.25 mrg
425 1.25 mrg /* Must have capabilities. */
426 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
427 1.25 mrg return 1;
428 1.25 mrg
429 1.25 mrg *valuep = pci_conf_read(pc, tag, off + (reg &~ 2)) >> (8 * (reg & 2));
430 1.25 mrg
431 1.25 mrg return 0;
432 1.25 mrg }
433 1.25 mrg
434 1.25 mrg int
435 1.25 mrg pcie_capability_write_dword(struct pci_dev *pdev, int reg, uint32_t value)
436 1.25 mrg {
437 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
438 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
439 1.25 mrg int off;
440 1.25 mrg
441 1.25 mrg /* Must have capabilities. */
442 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
443 1.25 mrg return 1;
444 1.25 mrg
445 1.25 mrg pci_conf_write(pc, tag, off + reg, value);
446 1.25 mrg
447 1.25 mrg return 0;
448 1.25 mrg }
449 1.25 mrg
450 1.25 mrg int
451 1.25 mrg pcie_capability_write_word(struct pci_dev *pdev, int reg, uint16_t value)
452 1.25 mrg {
453 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
454 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
455 1.25 mrg int off;
456 1.25 mrg
457 1.25 mrg /* Must have capabilities. */
458 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
459 1.25 mrg return 1;
460 1.25 mrg
461 1.25 mrg pci_rmw_config(pc, tag, off + reg, 2, value);
462 1.25 mrg
463 1.25 mrg return 0;
464 1.25 mrg }
465 1.25 mrg
466 1.25 mrg /* From PCIe 5.0 7.5.3.4 "Device Control Register" */
467 1.25 mrg static const unsigned readrqmax[] = {
468 1.25 mrg 128,
469 1.25 mrg 256,
470 1.25 mrg 512,
471 1.25 mrg 1024,
472 1.25 mrg 2048,
473 1.25 mrg 4096,
474 1.25 mrg };
475 1.25 mrg
476 1.25 mrg int
477 1.25 mrg pcie_get_readrq(struct pci_dev *pdev)
478 1.25 mrg {
479 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
480 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
481 1.25 mrg unsigned val;
482 1.25 mrg int off;
483 1.25 mrg
484 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
485 1.25 mrg return -EINVAL; /* XXX NetBSD->Linux */
486 1.25 mrg
487 1.25 mrg val = __SHIFTOUT(pci_conf_read(pc, tag, off + PCIE_DCSR),
488 1.25 mrg PCIE_DCSR_MAX_READ_REQ);
489 1.25 mrg
490 1.25 mrg if (val >= __arraycount(readrqmax))
491 1.25 mrg val = 0;
492 1.25 mrg return readrqmax[val];
493 1.25 mrg }
494 1.25 mrg
495 1.25 mrg int
496 1.25 mrg pcie_set_readrq(struct pci_dev *pdev, int val)
497 1.25 mrg {
498 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
499 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
500 1.25 mrg pcireg_t reg, newval = 0;
501 1.25 mrg unsigned i;
502 1.25 mrg int off;
503 1.25 mrg
504 1.25 mrg for (i = 0; i < __arraycount(readrqmax); i++) {
505 1.25 mrg if (readrqmax[i] == val) {
506 1.25 mrg newval = i;
507 1.25 mrg break;
508 1.25 mrg }
509 1.25 mrg }
510 1.25 mrg
511 1.25 mrg if (i == __arraycount(readrqmax))
512 1.25 mrg return -EINVAL;
513 1.25 mrg
514 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
515 1.25 mrg return -EINVAL; /* XXX NetBSD->Linux */
516 1.25 mrg
517 1.25 mrg reg = pci_conf_read(pc, tag, off + PCIE_DCSR);
518 1.25 mrg reg &= ~PCIE_DCSR_MAX_READ_REQ | (newval << 12);
519 1.25 mrg pci_conf_write(pc, tag, off + PCIE_DCSR, reg);
520 1.25 mrg
521 1.25 mrg return 0;
522 1.25 mrg }
523 1.25 mrg
524 1.1 riastrad bus_addr_t
525 1.1 riastrad pcibios_align_resource(void *p, const struct resource *resource,
526 1.1 riastrad bus_addr_t addr, bus_size_t size)
527 1.1 riastrad {
528 1.1 riastrad panic("pcibios_align_resource has accessed unaligned neurons!");
529 1.1 riastrad }
530 1.1 riastrad
531 1.1 riastrad int
532 1.1 riastrad pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource,
533 1.1 riastrad bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused,
534 1.1 riastrad bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t,
535 1.1 riastrad bus_size_t) __unused,
536 1.1 riastrad struct pci_dev *pdev)
537 1.1 riastrad {
538 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
539 1.1 riastrad bus_space_tag_t bst;
540 1.1 riastrad int error;
541 1.1 riastrad
542 1.1 riastrad switch (resource->flags) {
543 1.1 riastrad case IORESOURCE_MEM:
544 1.1 riastrad bst = pa->pa_memt;
545 1.1 riastrad break;
546 1.1 riastrad
547 1.1 riastrad case IORESOURCE_IO:
548 1.1 riastrad bst = pa->pa_iot;
549 1.1 riastrad break;
550 1.1 riastrad
551 1.1 riastrad default:
552 1.1 riastrad panic("I don't know what kind of resource you want!");
553 1.1 riastrad }
554 1.1 riastrad
555 1.1 riastrad resource->r_bst = bst;
556 1.1 riastrad error = bus_space_alloc(bst, start, __type_max(bus_addr_t),
557 1.1 riastrad size, align, 0, 0, &resource->start, &resource->r_bsh);
558 1.1 riastrad if (error)
559 1.1 riastrad return error;
560 1.1 riastrad
561 1.13 riastrad resource->end = start + (size - 1);
562 1.1 riastrad return 0;
563 1.1 riastrad }
564 1.1 riastrad
565 1.1 riastrad /*
566 1.1 riastrad * XXX Mega-kludgerific! pci_get_bus_and_slot and pci_get_class are
567 1.1 riastrad * defined only for their single purposes in i915drm, in
568 1.1 riastrad * i915_get_bridge_dev and intel_detect_pch. We can't define them more
569 1.1 riastrad * generally without adapting pci_find_device (and pci_enumerate_bus
570 1.1 riastrad * internally) to pass a cookie through.
571 1.1 riastrad */
572 1.1 riastrad
573 1.1 riastrad static int
574 1.1 riastrad pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa)
575 1.1 riastrad {
576 1.1 riastrad
577 1.11 riastrad /* XXX domain */
578 1.1 riastrad if (pa->pa_bus != 0)
579 1.1 riastrad return 0;
580 1.1 riastrad if (pa->pa_device != 0)
581 1.1 riastrad return 0;
582 1.1 riastrad if (pa->pa_function != 0)
583 1.1 riastrad return 0;
584 1.1 riastrad
585 1.1 riastrad return 1;
586 1.1 riastrad }
587 1.1 riastrad
588 1.1 riastrad struct pci_dev *
589 1.11 riastrad pci_get_domain_bus_and_slot(int domain, int bus, int slot)
590 1.1 riastrad {
591 1.1 riastrad struct pci_attach_args pa;
592 1.1 riastrad
593 1.11 riastrad KASSERT(domain == 0);
594 1.1 riastrad KASSERT(bus == 0);
595 1.1 riastrad KASSERT(slot == PCI_DEVFN(0, 0));
596 1.1 riastrad
597 1.1 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0))
598 1.1 riastrad return NULL;
599 1.1 riastrad
600 1.1 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
601 1.1 riastrad linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
602 1.1 riastrad
603 1.1 riastrad return pdev;
604 1.1 riastrad }
605 1.1 riastrad
606 1.1 riastrad static int
607 1.1 riastrad pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa)
608 1.1 riastrad {
609 1.1 riastrad
610 1.1 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
611 1.1 riastrad return 0;
612 1.1 riastrad if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
613 1.1 riastrad return 0;
614 1.1 riastrad
615 1.1 riastrad return 1;
616 1.1 riastrad }
617 1.1 riastrad
618 1.1 riastrad void
619 1.1 riastrad pci_dev_put(struct pci_dev *pdev)
620 1.1 riastrad {
621 1.1 riastrad
622 1.1 riastrad if (pdev == NULL)
623 1.1 riastrad return;
624 1.1 riastrad
625 1.1 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE));
626 1.1 riastrad kmem_free(pdev->bus, sizeof(*pdev->bus));
627 1.1 riastrad kmem_free(pdev, sizeof(*pdev));
628 1.1 riastrad }
629 1.1 riastrad
630 1.1 riastrad struct pci_dev * /* XXX i915 kludge */
631 1.1 riastrad pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from)
632 1.1 riastrad {
633 1.1 riastrad struct pci_attach_args pa;
634 1.1 riastrad
635 1.1 riastrad KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8));
636 1.1 riastrad
637 1.1 riastrad if (from != NULL) {
638 1.1 riastrad pci_dev_put(from);
639 1.1 riastrad return NULL;
640 1.1 riastrad }
641 1.1 riastrad
642 1.1 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge))
643 1.1 riastrad return NULL;
644 1.1 riastrad
645 1.1 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
646 1.1 riastrad linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
647 1.1 riastrad
648 1.1 riastrad return pdev;
649 1.1 riastrad }
650 1.1 riastrad
651 1.19 riastrad int
652 1.19 riastrad pci_dev_present(const struct pci_device_id *ids)
653 1.19 riastrad {
654 1.19 riastrad
655 1.19 riastrad /* XXX implement me -- pci_find_device doesn't pass a cookie */
656 1.19 riastrad return 0;
657 1.19 riastrad }
658 1.19 riastrad
659 1.1 riastrad void
660 1.1 riastrad pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused)
661 1.1 riastrad {
662 1.1 riastrad
663 1.1 riastrad /* XXX Disable the ROM address decoder. */
664 1.1 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
665 1.1 riastrad KASSERT(vaddr == pdev->pd_rom_vaddr);
666 1.1 riastrad bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size);
667 1.1 riastrad pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM;
668 1.1 riastrad pdev->pd_rom_vaddr = NULL;
669 1.1 riastrad }
670 1.1 riastrad
671 1.1 riastrad /* XXX Whattakludge! Should move this in sys/arch/. */
672 1.1 riastrad static int
673 1.1 riastrad pci_map_rom_md(struct pci_dev *pdev)
674 1.1 riastrad {
675 1.1 riastrad #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
676 1.1 riastrad const bus_addr_t rom_base = 0xc0000;
677 1.1 riastrad const bus_size_t rom_size = 0x20000;
678 1.1 riastrad bus_space_handle_t rom_bsh;
679 1.1 riastrad int error;
680 1.1 riastrad
681 1.1 riastrad if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY)
682 1.1 riastrad return ENXIO;
683 1.1 riastrad if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
684 1.1 riastrad return ENXIO;
685 1.1 riastrad /* XXX Check whether this is the primary VGA card? */
686 1.1 riastrad error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size,
687 1.1 riastrad (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh);
688 1.1 riastrad if (error)
689 1.1 riastrad return ENXIO;
690 1.1 riastrad
691 1.1 riastrad pdev->pd_rom_bst = pdev->pd_pa.pa_memt;
692 1.1 riastrad pdev->pd_rom_bsh = rom_bsh;
693 1.1 riastrad pdev->pd_rom_size = rom_size;
694 1.1 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
695 1.1 riastrad
696 1.1 riastrad return 0;
697 1.1 riastrad #else
698 1.1 riastrad return ENXIO;
699 1.1 riastrad #endif
700 1.1 riastrad }
701 1.1 riastrad
702 1.1 riastrad void __pci_rom_iomem *
703 1.1 riastrad pci_map_rom(struct pci_dev *pdev, size_t *sizep)
704 1.1 riastrad {
705 1.1 riastrad
706 1.1 riastrad KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
707 1.1 riastrad
708 1.1 riastrad if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM,
709 1.1 riastrad (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR),
710 1.1 riastrad &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size)
711 1.1 riastrad != 0)
712 1.1 riastrad goto fail_mi;
713 1.1 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
714 1.1 riastrad
715 1.1 riastrad /* XXX This type is obviously wrong in general... */
716 1.1 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
717 1.1 riastrad pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
718 1.1 riastrad &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
719 1.1 riastrad pci_unmap_rom(pdev, NULL);
720 1.1 riastrad goto fail_mi;
721 1.1 riastrad }
722 1.1 riastrad goto success;
723 1.1 riastrad
724 1.1 riastrad fail_mi:
725 1.1 riastrad if (pci_map_rom_md(pdev) != 0)
726 1.1 riastrad goto fail_md;
727 1.1 riastrad
728 1.1 riastrad /* XXX This type is obviously wrong in general... */
729 1.1 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
730 1.1 riastrad pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
731 1.1 riastrad &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
732 1.1 riastrad pci_unmap_rom(pdev, NULL);
733 1.1 riastrad goto fail_md;
734 1.1 riastrad }
735 1.1 riastrad
736 1.1 riastrad success:
737 1.1 riastrad KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX);
738 1.1 riastrad *sizep = pdev->pd_rom_found_size;
739 1.1 riastrad pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst,
740 1.1 riastrad pdev->pd_rom_found_bsh);
741 1.1 riastrad return pdev->pd_rom_vaddr;
742 1.1 riastrad
743 1.1 riastrad fail_md:
744 1.1 riastrad return NULL;
745 1.1 riastrad }
746 1.1 riastrad
747 1.1 riastrad void __pci_rom_iomem *
748 1.1 riastrad pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep)
749 1.1 riastrad {
750 1.1 riastrad
751 1.1 riastrad *sizep = 0;
752 1.1 riastrad return NULL;
753 1.1 riastrad }
754 1.1 riastrad
755 1.1 riastrad int
756 1.1 riastrad pci_enable_rom(struct pci_dev *pdev)
757 1.1 riastrad {
758 1.1 riastrad const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
759 1.1 riastrad const pcitag_t tag = pdev->pd_pa.pa_tag;
760 1.1 riastrad pcireg_t addr;
761 1.1 riastrad int s;
762 1.1 riastrad
763 1.1 riastrad /* XXX Don't do anything if the ROM isn't there. */
764 1.1 riastrad
765 1.1 riastrad s = splhigh();
766 1.1 riastrad addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
767 1.1 riastrad addr |= PCI_MAPREG_ROM_ENABLE;
768 1.1 riastrad pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
769 1.1 riastrad splx(s);
770 1.1 riastrad
771 1.1 riastrad return 0;
772 1.1 riastrad }
773 1.1 riastrad
774 1.1 riastrad void
775 1.1 riastrad pci_disable_rom(struct pci_dev *pdev)
776 1.1 riastrad {
777 1.1 riastrad const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
778 1.1 riastrad const pcitag_t tag = pdev->pd_pa.pa_tag;
779 1.1 riastrad pcireg_t addr;
780 1.1 riastrad int s;
781 1.1 riastrad
782 1.1 riastrad s = splhigh();
783 1.1 riastrad addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
784 1.1 riastrad addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE;
785 1.1 riastrad pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
786 1.1 riastrad splx(s);
787 1.1 riastrad }
788 1.1 riastrad
789 1.1 riastrad bus_addr_t
790 1.1 riastrad pci_resource_start(struct pci_dev *pdev, unsigned i)
791 1.1 riastrad {
792 1.1 riastrad
793 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES);
794 1.1 riastrad return pdev->pd_resources[i].addr;
795 1.1 riastrad }
796 1.1 riastrad
797 1.1 riastrad bus_size_t
798 1.1 riastrad pci_resource_len(struct pci_dev *pdev, unsigned i)
799 1.1 riastrad {
800 1.1 riastrad
801 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES);
802 1.1 riastrad return pdev->pd_resources[i].size;
803 1.1 riastrad }
804 1.1 riastrad
805 1.1 riastrad bus_addr_t
806 1.1 riastrad pci_resource_end(struct pci_dev *pdev, unsigned i)
807 1.1 riastrad {
808 1.1 riastrad
809 1.1 riastrad return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1);
810 1.1 riastrad }
811 1.1 riastrad
812 1.1 riastrad int
813 1.1 riastrad pci_resource_flags(struct pci_dev *pdev, unsigned i)
814 1.1 riastrad {
815 1.1 riastrad
816 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES);
817 1.1 riastrad return pdev->pd_resources[i].flags;
818 1.1 riastrad }
819 1.1 riastrad
820 1.1 riastrad void __pci_iomem *
821 1.1 riastrad pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size)
822 1.1 riastrad {
823 1.1 riastrad int error;
824 1.1 riastrad
825 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES);
826 1.1 riastrad KASSERT(pdev->pd_resources[i].kva == NULL);
827 1.1 riastrad
828 1.1 riastrad if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM)
829 1.1 riastrad return NULL;
830 1.1 riastrad if (pdev->pd_resources[i].size < size)
831 1.1 riastrad return NULL;
832 1.1 riastrad error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr,
833 1.1 riastrad size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags,
834 1.1 riastrad &pdev->pd_resources[i].bsh);
835 1.6 riastrad if (error)
836 1.5 riastrad return NULL;
837 1.1 riastrad pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt;
838 1.1 riastrad pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst,
839 1.1 riastrad pdev->pd_resources[i].bsh);
840 1.1 riastrad pdev->pd_resources[i].mapped = true;
841 1.1 riastrad
842 1.1 riastrad return pdev->pd_resources[i].kva;
843 1.1 riastrad }
844 1.1 riastrad
845 1.1 riastrad void
846 1.1 riastrad pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva)
847 1.1 riastrad {
848 1.1 riastrad unsigned i;
849 1.1 riastrad
850 1.1 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
851 1.1 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) {
852 1.1 riastrad if (pdev->pd_resources[i].kva == kva)
853 1.1 riastrad break;
854 1.1 riastrad }
855 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES);
856 1.1 riastrad
857 1.1 riastrad pdev->pd_resources[i].kva = NULL;
858 1.1 riastrad bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh,
859 1.1 riastrad pdev->pd_resources[i].size);
860 1.1 riastrad }
861 1.1 riastrad
862 1.1 riastrad void
863 1.1 riastrad pci_save_state(struct pci_dev *pdev)
864 1.1 riastrad {
865 1.1 riastrad
866 1.1 riastrad KASSERT(pdev->pd_saved_state == NULL);
867 1.1 riastrad pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state),
868 1.1 riastrad KM_SLEEP);
869 1.1 riastrad pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
870 1.1 riastrad pdev->pd_saved_state);
871 1.1 riastrad }
872 1.1 riastrad
873 1.1 riastrad void
874 1.1 riastrad pci_restore_state(struct pci_dev *pdev)
875 1.1 riastrad {
876 1.1 riastrad
877 1.1 riastrad KASSERT(pdev->pd_saved_state != NULL);
878 1.1 riastrad pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
879 1.1 riastrad pdev->pd_saved_state);
880 1.1 riastrad kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state));
881 1.1 riastrad pdev->pd_saved_state = NULL;
882 1.1 riastrad }
883 1.1 riastrad
884 1.1 riastrad bool
885 1.1 riastrad pci_is_pcie(struct pci_dev *pdev)
886 1.1 riastrad {
887 1.1 riastrad
888 1.1 riastrad return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0);
889 1.1 riastrad }
890 1.1 riastrad
891 1.1 riastrad bool
892 1.1 riastrad pci_dma_supported(struct pci_dev *pdev, uintmax_t mask)
893 1.1 riastrad {
894 1.1 riastrad
895 1.1 riastrad /* XXX Cop-out. */
896 1.1 riastrad if (mask > DMA_BIT_MASK(32))
897 1.1 riastrad return pci_dma64_available(&pdev->pd_pa);
898 1.1 riastrad else
899 1.1 riastrad return true;
900 1.1 riastrad }
901 1.1 riastrad
902 1.1 riastrad bool
903 1.14 riastrad pci_is_thunderbolt_attached(struct pci_dev *pdev)
904 1.14 riastrad {
905 1.14 riastrad
906 1.14 riastrad /* XXX Cop-out. */
907 1.14 riastrad return false;
908 1.14 riastrad }
909 1.14 riastrad
910 1.14 riastrad bool
911 1.1 riastrad pci_is_root_bus(struct pci_bus *bus)
912 1.1 riastrad {
913 1.1 riastrad
914 1.24 mrg return bus->number == 0;
915 1.1 riastrad }
916 1.1 riastrad
917 1.1 riastrad int
918 1.1 riastrad pci_domain_nr(struct pci_bus *bus)
919 1.1 riastrad {
920 1.1 riastrad
921 1.23 riastrad return pci_get_segment(bus->pb_pc);
922 1.1 riastrad }
923 1.1 riastrad
924 1.1 riastrad /*
925 1.1 riastrad * We explicitly rename pci_enable/disable_device so that you have to
926 1.1 riastrad * review each use of them, since NetBSD's PCI API does _not_ respect
927 1.1 riastrad * our local enablecnt here, but there are different parts of NetBSD
928 1.1 riastrad * that automatically enable/disable like PMF, so you have to decide
929 1.1 riastrad * for each one whether to call it or not.
930 1.1 riastrad */
931 1.1 riastrad
932 1.1 riastrad int
933 1.1 riastrad linux_pci_enable_device(struct pci_dev *pdev)
934 1.1 riastrad {
935 1.1 riastrad const struct pci_attach_args *pa = &pdev->pd_pa;
936 1.1 riastrad pcireg_t csr;
937 1.1 riastrad int s;
938 1.1 riastrad
939 1.1 riastrad if (pdev->pd_enablecnt++)
940 1.1 riastrad return 0;
941 1.1 riastrad
942 1.1 riastrad s = splhigh();
943 1.1 riastrad csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
944 1.4 riastrad /* If someone else (firmware) already enabled it, credit them. */
945 1.4 riastrad if (csr & (PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE))
946 1.4 riastrad pdev->pd_enablecnt++;
947 1.1 riastrad csr |= PCI_COMMAND_IO_ENABLE;
948 1.1 riastrad csr |= PCI_COMMAND_MEM_ENABLE;
949 1.1 riastrad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
950 1.1 riastrad splx(s);
951 1.1 riastrad
952 1.1 riastrad return 0;
953 1.1 riastrad }
954 1.1 riastrad
955 1.1 riastrad void
956 1.1 riastrad linux_pci_disable_device(struct pci_dev *pdev)
957 1.1 riastrad {
958 1.1 riastrad const struct pci_attach_args *pa = &pdev->pd_pa;
959 1.1 riastrad pcireg_t csr;
960 1.1 riastrad int s;
961 1.1 riastrad
962 1.1 riastrad if (--pdev->pd_enablecnt)
963 1.1 riastrad return;
964 1.1 riastrad
965 1.1 riastrad s = splhigh();
966 1.1 riastrad csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
967 1.1 riastrad csr &= ~PCI_COMMAND_IO_ENABLE;
968 1.1 riastrad csr &= ~PCI_COMMAND_MEM_ENABLE;
969 1.1 riastrad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
970 1.1 riastrad splx(s);
971 1.1 riastrad }
972 1.1 riastrad
973 1.1 riastrad void
974 1.1 riastrad linux_pci_dev_destroy(struct pci_dev *pdev)
975 1.1 riastrad {
976 1.1 riastrad unsigned i;
977 1.1 riastrad
978 1.25 mrg if (pdev->bus->self != NULL) {
979 1.25 mrg kmem_free(pdev->bus->self, sizeof(*pdev->bus->self));
980 1.25 mrg }
981 1.1 riastrad if (pdev->bus != NULL) {
982 1.1 riastrad kmem_free(pdev->bus, sizeof(*pdev->bus));
983 1.1 riastrad pdev->bus = NULL;
984 1.1 riastrad }
985 1.1 riastrad if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) {
986 1.1 riastrad pci_unmap_rom(pdev, pdev->pd_rom_vaddr);
987 1.1 riastrad pdev->pd_rom_vaddr = 0;
988 1.1 riastrad }
989 1.1 riastrad for (i = 0; i < __arraycount(pdev->pd_resources); i++) {
990 1.1 riastrad if (!pdev->pd_resources[i].mapped)
991 1.1 riastrad continue;
992 1.1 riastrad bus_space_unmap(pdev->pd_resources[i].bst,
993 1.1 riastrad pdev->pd_resources[i].bsh, pdev->pd_resources[i].size);
994 1.1 riastrad }
995 1.1 riastrad
996 1.1 riastrad /* There is no way these should be still in use. */
997 1.1 riastrad KASSERT(pdev->pd_saved_state == NULL);
998 1.1 riastrad KASSERT(pdev->pd_intr_handles == NULL);
999 1.1 riastrad }
1000 1.24 mrg
1001 1.24 mrg enum pci_bus_speed
1002 1.24 mrg pcie_get_speed_cap(struct pci_dev *dev)
1003 1.24 mrg {
1004 1.24 mrg pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
1005 1.24 mrg pcitag_t tag = dev->pd_pa.pa_tag;
1006 1.24 mrg pcireg_t lcap, lcap2, xcap;
1007 1.24 mrg int off;
1008 1.24 mrg
1009 1.24 mrg /* Must have capabilities. */
1010 1.24 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
1011 1.24 mrg return PCI_SPEED_UNKNOWN;
1012 1.24 mrg
1013 1.24 mrg /* Only PCIe 3.x has LCAP2. */
1014 1.24 mrg xcap = pci_conf_read(pc, tag, off + PCIE_XCAP);
1015 1.24 mrg if (__SHIFTOUT(xcap, PCIE_XCAP_VER_MASK) >= 2) {
1016 1.24 mrg lcap2 = pci_conf_read(pc, tag, off + PCIE_LCAP2);
1017 1.24 mrg if (lcap2) {
1018 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS64) != 0) {
1019 1.24 mrg return PCIE_SPEED_64_0GT;
1020 1.24 mrg }
1021 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS32) != 0) {
1022 1.24 mrg return PCIE_SPEED_32_0GT;
1023 1.24 mrg }
1024 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS16) != 0) {
1025 1.24 mrg return PCIE_SPEED_16_0GT;
1026 1.24 mrg }
1027 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS8) != 0) {
1028 1.24 mrg return PCIE_SPEED_8_0GT;
1029 1.24 mrg }
1030 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS5) != 0) {
1031 1.24 mrg return PCIE_SPEED_5_0GT;
1032 1.24 mrg }
1033 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS2) != 0) {
1034 1.24 mrg return PCIE_SPEED_2_5GT;
1035 1.24 mrg }
1036 1.24 mrg }
1037 1.24 mrg }
1038 1.24 mrg
1039 1.24 mrg lcap = pci_conf_read(pc, tag, off + PCIE_LCAP);
1040 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_64) {
1041 1.24 mrg return PCIE_SPEED_64_0GT;
1042 1.24 mrg }
1043 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_32) {
1044 1.24 mrg return PCIE_SPEED_32_0GT;
1045 1.24 mrg }
1046 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_16) {
1047 1.24 mrg return PCIE_SPEED_16_0GT;
1048 1.24 mrg }
1049 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_8) {
1050 1.24 mrg return PCIE_SPEED_8_0GT;
1051 1.24 mrg }
1052 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_5) {
1053 1.24 mrg return PCIE_SPEED_5_0GT;
1054 1.24 mrg }
1055 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_2) {
1056 1.24 mrg return PCIE_SPEED_2_5GT;
1057 1.24 mrg }
1058 1.24 mrg
1059 1.24 mrg return PCI_SPEED_UNKNOWN;
1060 1.24 mrg }
1061 1.24 mrg
1062 1.24 mrg /*
1063 1.24 mrg * This should walk the tree, it only checks this device currently.
1064 1.24 mrg * It also does not write to limiting_dev (the only caller in drm2
1065 1.24 mrg * currently does not use it.)
1066 1.24 mrg */
1067 1.24 mrg unsigned
1068 1.24 mrg pcie_bandwidth_available(struct pci_dev *dev,
1069 1.24 mrg struct pci_dev **limiting_dev,
1070 1.24 mrg enum pci_bus_speed *speed,
1071 1.24 mrg enum pcie_link_width *width)
1072 1.24 mrg {
1073 1.24 mrg pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
1074 1.24 mrg pcitag_t tag = dev->pd_pa.pa_tag;
1075 1.24 mrg pcireg_t lcsr;
1076 1.24 mrg unsigned per_line_speed, num_lanes;
1077 1.24 mrg int off;
1078 1.24 mrg
1079 1.24 mrg /* Must have capabilities. */
1080 1.24 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
1081 1.24 mrg return 0;
1082 1.24 mrg
1083 1.24 mrg if (speed)
1084 1.24 mrg *speed = PCI_SPEED_UNKNOWN;
1085 1.24 mrg if (width)
1086 1.24 mrg *width = 0;
1087 1.24 mrg
1088 1.24 mrg lcsr = pci_conf_read(pc, tag, off + PCIE_LCSR);
1089 1.24 mrg
1090 1.24 mrg switch (lcsr & PCIE_LCSR_NLW) {
1091 1.24 mrg case PCIE_LCSR_NLW_X1:
1092 1.24 mrg case PCIE_LCSR_NLW_X2:
1093 1.24 mrg case PCIE_LCSR_NLW_X4:
1094 1.24 mrg case PCIE_LCSR_NLW_X8:
1095 1.24 mrg case PCIE_LCSR_NLW_X12:
1096 1.24 mrg case PCIE_LCSR_NLW_X16:
1097 1.24 mrg case PCIE_LCSR_NLW_X32:
1098 1.24 mrg num_lanes = __SHIFTOUT(lcsr, PCIE_LCSR_NLW);
1099 1.24 mrg if (width)
1100 1.24 mrg *width = num_lanes;
1101 1.24 mrg break;
1102 1.24 mrg default:
1103 1.24 mrg num_lanes = 0;
1104 1.24 mrg break;
1105 1.24 mrg }
1106 1.24 mrg
1107 1.24 mrg switch (__SHIFTOUT(lcsr, PCIE_LCSR_LINKSPEED)) {
1108 1.24 mrg case PCIE_LCSR_LINKSPEED_2:
1109 1.24 mrg *speed = PCIE_SPEED_2_5GT;
1110 1.24 mrg per_line_speed = 2500 * 8 / 10;
1111 1.24 mrg break;
1112 1.24 mrg case PCIE_LCSR_LINKSPEED_5:
1113 1.24 mrg *speed = PCIE_SPEED_5_0GT;
1114 1.24 mrg per_line_speed = 5000 * 8 / 10;
1115 1.24 mrg break;
1116 1.24 mrg case PCIE_LCSR_LINKSPEED_8:
1117 1.24 mrg *speed = PCIE_SPEED_8_0GT;
1118 1.24 mrg per_line_speed = 8000 * 128 / 130;
1119 1.24 mrg break;
1120 1.24 mrg case PCIE_LCSR_LINKSPEED_16:
1121 1.24 mrg *speed = PCIE_SPEED_16_0GT;
1122 1.24 mrg per_line_speed = 16000 * 128 / 130;
1123 1.24 mrg break;
1124 1.24 mrg case PCIE_LCSR_LINKSPEED_32:
1125 1.24 mrg *speed = PCIE_SPEED_32_0GT;
1126 1.24 mrg per_line_speed = 32000 * 128 / 130;
1127 1.24 mrg break;
1128 1.24 mrg case PCIE_LCSR_LINKSPEED_64:
1129 1.24 mrg *speed = PCIE_SPEED_64_0GT;
1130 1.24 mrg per_line_speed = 64000 * 128 / 130;
1131 1.24 mrg break;
1132 1.24 mrg default:
1133 1.24 mrg per_line_speed = 0;
1134 1.24 mrg }
1135 1.24 mrg
1136 1.24 mrg return num_lanes * per_line_speed;
1137 1.24 mrg }
1138