linux_pci.c revision 1.27 1 1.27 mrg /* $NetBSD: linux_pci.c,v 1.27 2023/09/30 10:46:46 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.27 mrg __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.27 2023/09/30 10:46:46 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.27 mrg /*
173 1.27 mrg * NetBSD doesn't have an easy "am I PCIe" or "give me PCIe speed
174 1.27 mrg * from capability" function, but we already emulate the Linux
175 1.27 mrg * versions that do.
176 1.27 mrg */
177 1.27 mrg if (pci_is_pcie(pdev)) {
178 1.27 mrg pdev->bus->max_bus_speed = pcie_get_speed_cap(pdev);
179 1.27 mrg } else {
180 1.27 mrg /* XXX: Do AGP/PCI-X, etc.? */
181 1.27 mrg pdev->bus->max_bus_speed = PCI_SPEED_UNKNOWN;
182 1.27 mrg }
183 1.25 mrg pdev->bus->self = alloc_fake_parent_device(parent, pa);
184 1.1 riastrad pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function);
185 1.1 riastrad pdev->vendor = PCI_VENDOR(pa->pa_id);
186 1.1 riastrad pdev->device = PCI_PRODUCT(pa->pa_id);
187 1.1 riastrad pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id);
188 1.1 riastrad pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id);
189 1.1 riastrad pdev->revision = PCI_REVISION(pa->pa_class);
190 1.1 riastrad pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */
191 1.1 riastrad
192 1.1 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
193 1.1 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) {
194 1.1 riastrad const int reg = PCI_BAR(i);
195 1.1 riastrad
196 1.1 riastrad pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc,
197 1.1 riastrad pa->pa_tag, reg);
198 1.1 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
199 1.1 riastrad pdev->pd_resources[i].type,
200 1.1 riastrad &pdev->pd_resources[i].addr,
201 1.1 riastrad &pdev->pd_resources[i].size,
202 1.1 riastrad &pdev->pd_resources[i].flags)) {
203 1.1 riastrad pdev->pd_resources[i].addr = 0;
204 1.1 riastrad pdev->pd_resources[i].size = 0;
205 1.1 riastrad pdev->pd_resources[i].flags = 0;
206 1.1 riastrad }
207 1.1 riastrad pdev->pd_resources[i].kva = NULL;
208 1.2 riastrad pdev->pd_resources[i].mapped = false;
209 1.1 riastrad }
210 1.1 riastrad }
211 1.1 riastrad
212 1.1 riastrad int
213 1.1 riastrad pci_find_capability(struct pci_dev *pdev, int cap)
214 1.1 riastrad {
215 1.1 riastrad
216 1.1 riastrad return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap,
217 1.1 riastrad NULL, NULL);
218 1.1 riastrad }
219 1.1 riastrad
220 1.1 riastrad int
221 1.1 riastrad pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
222 1.1 riastrad {
223 1.1 riastrad
224 1.1 riastrad KASSERT(!ISSET(reg, 3));
225 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg);
226 1.1 riastrad return 0;
227 1.1 riastrad }
228 1.1 riastrad
229 1.1 riastrad int
230 1.1 riastrad pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
231 1.1 riastrad {
232 1.1 riastrad
233 1.1 riastrad KASSERT(!ISSET(reg, 1));
234 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
235 1.1 riastrad (reg &~ 2)) >> (8 * (reg & 2));
236 1.1 riastrad return 0;
237 1.1 riastrad }
238 1.1 riastrad
239 1.1 riastrad int
240 1.1 riastrad pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep)
241 1.1 riastrad {
242 1.1 riastrad
243 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
244 1.1 riastrad (reg &~ 3)) >> (8 * (reg & 3));
245 1.1 riastrad return 0;
246 1.1 riastrad }
247 1.1 riastrad
248 1.1 riastrad int
249 1.1 riastrad pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value)
250 1.1 riastrad {
251 1.1 riastrad
252 1.1 riastrad KASSERT(!ISSET(reg, 3));
253 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value);
254 1.1 riastrad return 0;
255 1.1 riastrad }
256 1.1 riastrad
257 1.1 riastrad int
258 1.1 riastrad pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
259 1.1 riastrad uint32_t *valuep)
260 1.1 riastrad {
261 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
262 1.1 riastrad PCI_FUNC(devfn));
263 1.1 riastrad
264 1.1 riastrad KASSERT(!ISSET(reg, 1));
265 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3));
266 1.1 riastrad return 0;
267 1.1 riastrad }
268 1.1 riastrad
269 1.1 riastrad int
270 1.1 riastrad pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg,
271 1.1 riastrad uint16_t *valuep)
272 1.1 riastrad {
273 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
274 1.1 riastrad PCI_FUNC(devfn));
275 1.1 riastrad
276 1.1 riastrad KASSERT(!ISSET(reg, 1));
277 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2));
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_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
283 1.1 riastrad uint8_t *valuep)
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 *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3));
289 1.1 riastrad return 0;
290 1.1 riastrad }
291 1.1 riastrad
292 1.1 riastrad int
293 1.1 riastrad pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
294 1.1 riastrad uint32_t value)
295 1.1 riastrad {
296 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
297 1.1 riastrad PCI_FUNC(devfn));
298 1.1 riastrad
299 1.1 riastrad KASSERT(!ISSET(reg, 3));
300 1.1 riastrad pci_conf_write(bus->pb_pc, tag, reg, value);
301 1.1 riastrad return 0;
302 1.1 riastrad }
303 1.1 riastrad
304 1.1 riastrad static void
305 1.1 riastrad pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes,
306 1.1 riastrad uint32_t value)
307 1.1 riastrad {
308 1.1 riastrad const uint32_t mask = ~((~0UL) << (8 * bytes));
309 1.1 riastrad const int reg32 = (reg &~ 3);
310 1.1 riastrad const unsigned int shift = (8 * (reg & 3));
311 1.1 riastrad uint32_t value32;
312 1.1 riastrad
313 1.1 riastrad KASSERT(bytes <= 4);
314 1.1 riastrad KASSERT(!ISSET(value, ~mask));
315 1.1 riastrad value32 = pci_conf_read(pc, tag, reg32);
316 1.1 riastrad value32 &=~ (mask << shift);
317 1.1 riastrad value32 |= (value << shift);
318 1.1 riastrad pci_conf_write(pc, tag, reg32, value32);
319 1.1 riastrad }
320 1.1 riastrad
321 1.1 riastrad int
322 1.1 riastrad pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value)
323 1.1 riastrad {
324 1.1 riastrad
325 1.1 riastrad KASSERT(!ISSET(reg, 1));
326 1.1 riastrad pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value);
327 1.1 riastrad return 0;
328 1.1 riastrad }
329 1.1 riastrad
330 1.1 riastrad int
331 1.1 riastrad pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value)
332 1.1 riastrad {
333 1.1 riastrad
334 1.1 riastrad pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value);
335 1.1 riastrad return 0;
336 1.1 riastrad }
337 1.1 riastrad
338 1.1 riastrad int
339 1.1 riastrad pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg,
340 1.1 riastrad uint16_t value)
341 1.1 riastrad {
342 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
343 1.1 riastrad PCI_FUNC(devfn));
344 1.1 riastrad
345 1.1 riastrad KASSERT(!ISSET(reg, 1));
346 1.1 riastrad pci_rmw_config(bus->pb_pc, tag, reg, 2, 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_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
352 1.1 riastrad uint8_t value)
353 1.1 riastrad {
354 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
355 1.1 riastrad PCI_FUNC(devfn));
356 1.1 riastrad
357 1.1 riastrad pci_rmw_config(bus->pb_pc, tag, reg, 1, value);
358 1.1 riastrad return 0;
359 1.1 riastrad }
360 1.1 riastrad
361 1.1 riastrad int
362 1.1 riastrad pci_enable_msi(struct pci_dev *pdev)
363 1.1 riastrad {
364 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
365 1.1 riastrad
366 1.1 riastrad if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1))
367 1.1 riastrad return -EINVAL;
368 1.1 riastrad
369 1.1 riastrad pdev->msi_enabled = 1;
370 1.1 riastrad return 0;
371 1.1 riastrad }
372 1.1 riastrad
373 1.1 riastrad void
374 1.1 riastrad pci_disable_msi(struct pci_dev *pdev __unused)
375 1.1 riastrad {
376 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
377 1.1 riastrad
378 1.1 riastrad if (pdev->pd_intr_handles != NULL) {
379 1.1 riastrad pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1);
380 1.1 riastrad pdev->pd_intr_handles = NULL;
381 1.1 riastrad }
382 1.1 riastrad pdev->msi_enabled = 0;
383 1.1 riastrad }
384 1.1 riastrad
385 1.1 riastrad void
386 1.1 riastrad pci_set_master(struct pci_dev *pdev)
387 1.1 riastrad {
388 1.1 riastrad pcireg_t csr;
389 1.1 riastrad
390 1.1 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
391 1.1 riastrad PCI_COMMAND_STATUS_REG);
392 1.1 riastrad csr |= PCI_COMMAND_MASTER_ENABLE;
393 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
394 1.1 riastrad PCI_COMMAND_STATUS_REG, csr);
395 1.1 riastrad }
396 1.1 riastrad
397 1.1 riastrad void
398 1.1 riastrad pci_clear_master(struct pci_dev *pdev)
399 1.1 riastrad {
400 1.1 riastrad pcireg_t csr;
401 1.1 riastrad
402 1.1 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
403 1.1 riastrad PCI_COMMAND_STATUS_REG);
404 1.1 riastrad csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE;
405 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
406 1.1 riastrad PCI_COMMAND_STATUS_REG, csr);
407 1.1 riastrad }
408 1.1 riastrad
409 1.25 mrg int
410 1.25 mrg pcie_capability_read_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
411 1.25 mrg {
412 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
413 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
414 1.25 mrg int off;
415 1.25 mrg
416 1.25 mrg *valuep = 0;
417 1.25 mrg
418 1.25 mrg /* Must have capabilities. */
419 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
420 1.25 mrg return 1;
421 1.25 mrg
422 1.25 mrg *valuep = pci_conf_read(pc, tag, off + reg);
423 1.25 mrg
424 1.25 mrg return 0;
425 1.25 mrg }
426 1.25 mrg
427 1.25 mrg int
428 1.25 mrg pcie_capability_read_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
429 1.25 mrg {
430 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
431 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
432 1.25 mrg int off;
433 1.25 mrg
434 1.25 mrg *valuep = 0;
435 1.25 mrg
436 1.25 mrg /* Must have capabilities. */
437 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
438 1.25 mrg return 1;
439 1.25 mrg
440 1.25 mrg *valuep = pci_conf_read(pc, tag, off + (reg &~ 2)) >> (8 * (reg & 2));
441 1.25 mrg
442 1.25 mrg return 0;
443 1.25 mrg }
444 1.25 mrg
445 1.25 mrg int
446 1.25 mrg pcie_capability_write_dword(struct pci_dev *pdev, int reg, uint32_t value)
447 1.25 mrg {
448 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
449 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
450 1.25 mrg int off;
451 1.25 mrg
452 1.25 mrg /* Must have capabilities. */
453 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
454 1.25 mrg return 1;
455 1.25 mrg
456 1.25 mrg pci_conf_write(pc, tag, off + reg, value);
457 1.25 mrg
458 1.25 mrg return 0;
459 1.25 mrg }
460 1.25 mrg
461 1.25 mrg int
462 1.25 mrg pcie_capability_write_word(struct pci_dev *pdev, int reg, uint16_t value)
463 1.25 mrg {
464 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
465 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
466 1.25 mrg int off;
467 1.25 mrg
468 1.25 mrg /* Must have capabilities. */
469 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
470 1.25 mrg return 1;
471 1.25 mrg
472 1.25 mrg pci_rmw_config(pc, tag, off + reg, 2, value);
473 1.25 mrg
474 1.25 mrg return 0;
475 1.25 mrg }
476 1.25 mrg
477 1.25 mrg /* From PCIe 5.0 7.5.3.4 "Device Control Register" */
478 1.25 mrg static const unsigned readrqmax[] = {
479 1.25 mrg 128,
480 1.25 mrg 256,
481 1.25 mrg 512,
482 1.25 mrg 1024,
483 1.25 mrg 2048,
484 1.25 mrg 4096,
485 1.25 mrg };
486 1.25 mrg
487 1.25 mrg int
488 1.25 mrg pcie_get_readrq(struct pci_dev *pdev)
489 1.25 mrg {
490 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
491 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
492 1.25 mrg unsigned val;
493 1.25 mrg int off;
494 1.25 mrg
495 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
496 1.25 mrg return -EINVAL; /* XXX NetBSD->Linux */
497 1.25 mrg
498 1.25 mrg val = __SHIFTOUT(pci_conf_read(pc, tag, off + PCIE_DCSR),
499 1.25 mrg PCIE_DCSR_MAX_READ_REQ);
500 1.25 mrg
501 1.25 mrg if (val >= __arraycount(readrqmax))
502 1.25 mrg val = 0;
503 1.25 mrg return readrqmax[val];
504 1.25 mrg }
505 1.25 mrg
506 1.25 mrg int
507 1.25 mrg pcie_set_readrq(struct pci_dev *pdev, int val)
508 1.25 mrg {
509 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
510 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag;
511 1.25 mrg pcireg_t reg, newval = 0;
512 1.25 mrg unsigned i;
513 1.25 mrg int off;
514 1.25 mrg
515 1.25 mrg for (i = 0; i < __arraycount(readrqmax); i++) {
516 1.25 mrg if (readrqmax[i] == val) {
517 1.25 mrg newval = i;
518 1.25 mrg break;
519 1.25 mrg }
520 1.25 mrg }
521 1.25 mrg
522 1.25 mrg if (i == __arraycount(readrqmax))
523 1.25 mrg return -EINVAL;
524 1.25 mrg
525 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
526 1.25 mrg return -EINVAL; /* XXX NetBSD->Linux */
527 1.25 mrg
528 1.25 mrg reg = pci_conf_read(pc, tag, off + PCIE_DCSR);
529 1.25 mrg reg &= ~PCIE_DCSR_MAX_READ_REQ | (newval << 12);
530 1.25 mrg pci_conf_write(pc, tag, off + PCIE_DCSR, reg);
531 1.25 mrg
532 1.25 mrg return 0;
533 1.25 mrg }
534 1.25 mrg
535 1.1 riastrad bus_addr_t
536 1.1 riastrad pcibios_align_resource(void *p, const struct resource *resource,
537 1.1 riastrad bus_addr_t addr, bus_size_t size)
538 1.1 riastrad {
539 1.1 riastrad panic("pcibios_align_resource has accessed unaligned neurons!");
540 1.1 riastrad }
541 1.1 riastrad
542 1.1 riastrad int
543 1.1 riastrad pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource,
544 1.1 riastrad bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused,
545 1.1 riastrad bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t,
546 1.1 riastrad bus_size_t) __unused,
547 1.1 riastrad struct pci_dev *pdev)
548 1.1 riastrad {
549 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
550 1.1 riastrad bus_space_tag_t bst;
551 1.1 riastrad int error;
552 1.1 riastrad
553 1.1 riastrad switch (resource->flags) {
554 1.1 riastrad case IORESOURCE_MEM:
555 1.1 riastrad bst = pa->pa_memt;
556 1.1 riastrad break;
557 1.1 riastrad
558 1.1 riastrad case IORESOURCE_IO:
559 1.1 riastrad bst = pa->pa_iot;
560 1.1 riastrad break;
561 1.1 riastrad
562 1.1 riastrad default:
563 1.1 riastrad panic("I don't know what kind of resource you want!");
564 1.1 riastrad }
565 1.1 riastrad
566 1.1 riastrad resource->r_bst = bst;
567 1.1 riastrad error = bus_space_alloc(bst, start, __type_max(bus_addr_t),
568 1.1 riastrad size, align, 0, 0, &resource->start, &resource->r_bsh);
569 1.1 riastrad if (error)
570 1.1 riastrad return error;
571 1.1 riastrad
572 1.13 riastrad resource->end = start + (size - 1);
573 1.1 riastrad return 0;
574 1.1 riastrad }
575 1.1 riastrad
576 1.1 riastrad /*
577 1.1 riastrad * XXX Mega-kludgerific! pci_get_bus_and_slot and pci_get_class are
578 1.1 riastrad * defined only for their single purposes in i915drm, in
579 1.1 riastrad * i915_get_bridge_dev and intel_detect_pch. We can't define them more
580 1.1 riastrad * generally without adapting pci_find_device (and pci_enumerate_bus
581 1.1 riastrad * internally) to pass a cookie through.
582 1.1 riastrad */
583 1.1 riastrad
584 1.1 riastrad static int
585 1.1 riastrad pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa)
586 1.1 riastrad {
587 1.1 riastrad
588 1.11 riastrad /* XXX domain */
589 1.1 riastrad if (pa->pa_bus != 0)
590 1.1 riastrad return 0;
591 1.1 riastrad if (pa->pa_device != 0)
592 1.1 riastrad return 0;
593 1.1 riastrad if (pa->pa_function != 0)
594 1.1 riastrad return 0;
595 1.1 riastrad
596 1.1 riastrad return 1;
597 1.1 riastrad }
598 1.1 riastrad
599 1.1 riastrad struct pci_dev *
600 1.11 riastrad pci_get_domain_bus_and_slot(int domain, int bus, int slot)
601 1.1 riastrad {
602 1.1 riastrad struct pci_attach_args pa;
603 1.1 riastrad
604 1.11 riastrad KASSERT(domain == 0);
605 1.1 riastrad KASSERT(bus == 0);
606 1.1 riastrad KASSERT(slot == PCI_DEVFN(0, 0));
607 1.1 riastrad
608 1.1 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0))
609 1.1 riastrad return NULL;
610 1.1 riastrad
611 1.1 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
612 1.1 riastrad linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
613 1.1 riastrad
614 1.1 riastrad return pdev;
615 1.1 riastrad }
616 1.1 riastrad
617 1.1 riastrad static int
618 1.1 riastrad pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa)
619 1.1 riastrad {
620 1.1 riastrad
621 1.1 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
622 1.1 riastrad return 0;
623 1.1 riastrad if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
624 1.1 riastrad return 0;
625 1.1 riastrad
626 1.1 riastrad return 1;
627 1.1 riastrad }
628 1.1 riastrad
629 1.1 riastrad void
630 1.1 riastrad pci_dev_put(struct pci_dev *pdev)
631 1.1 riastrad {
632 1.1 riastrad
633 1.1 riastrad if (pdev == NULL)
634 1.1 riastrad return;
635 1.1 riastrad
636 1.1 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE));
637 1.1 riastrad kmem_free(pdev->bus, sizeof(*pdev->bus));
638 1.1 riastrad kmem_free(pdev, sizeof(*pdev));
639 1.1 riastrad }
640 1.1 riastrad
641 1.1 riastrad struct pci_dev * /* XXX i915 kludge */
642 1.1 riastrad pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from)
643 1.1 riastrad {
644 1.1 riastrad struct pci_attach_args pa;
645 1.1 riastrad
646 1.1 riastrad KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8));
647 1.1 riastrad
648 1.1 riastrad if (from != NULL) {
649 1.1 riastrad pci_dev_put(from);
650 1.1 riastrad return NULL;
651 1.1 riastrad }
652 1.1 riastrad
653 1.1 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge))
654 1.1 riastrad return NULL;
655 1.1 riastrad
656 1.1 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
657 1.1 riastrad linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
658 1.1 riastrad
659 1.1 riastrad return pdev;
660 1.1 riastrad }
661 1.1 riastrad
662 1.19 riastrad int
663 1.19 riastrad pci_dev_present(const struct pci_device_id *ids)
664 1.19 riastrad {
665 1.19 riastrad
666 1.19 riastrad /* XXX implement me -- pci_find_device doesn't pass a cookie */
667 1.19 riastrad return 0;
668 1.19 riastrad }
669 1.19 riastrad
670 1.1 riastrad void
671 1.1 riastrad pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused)
672 1.1 riastrad {
673 1.1 riastrad
674 1.1 riastrad /* XXX Disable the ROM address decoder. */
675 1.1 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
676 1.1 riastrad KASSERT(vaddr == pdev->pd_rom_vaddr);
677 1.1 riastrad bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size);
678 1.1 riastrad pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM;
679 1.1 riastrad pdev->pd_rom_vaddr = NULL;
680 1.1 riastrad }
681 1.1 riastrad
682 1.1 riastrad /* XXX Whattakludge! Should move this in sys/arch/. */
683 1.1 riastrad static int
684 1.1 riastrad pci_map_rom_md(struct pci_dev *pdev)
685 1.1 riastrad {
686 1.1 riastrad #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
687 1.1 riastrad const bus_addr_t rom_base = 0xc0000;
688 1.1 riastrad const bus_size_t rom_size = 0x20000;
689 1.1 riastrad bus_space_handle_t rom_bsh;
690 1.1 riastrad int error;
691 1.1 riastrad
692 1.1 riastrad if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY)
693 1.1 riastrad return ENXIO;
694 1.1 riastrad if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
695 1.1 riastrad return ENXIO;
696 1.1 riastrad /* XXX Check whether this is the primary VGA card? */
697 1.1 riastrad error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size,
698 1.1 riastrad (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh);
699 1.1 riastrad if (error)
700 1.1 riastrad return ENXIO;
701 1.1 riastrad
702 1.1 riastrad pdev->pd_rom_bst = pdev->pd_pa.pa_memt;
703 1.1 riastrad pdev->pd_rom_bsh = rom_bsh;
704 1.1 riastrad pdev->pd_rom_size = rom_size;
705 1.1 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
706 1.1 riastrad
707 1.1 riastrad return 0;
708 1.1 riastrad #else
709 1.1 riastrad return ENXIO;
710 1.1 riastrad #endif
711 1.1 riastrad }
712 1.1 riastrad
713 1.1 riastrad void __pci_rom_iomem *
714 1.1 riastrad pci_map_rom(struct pci_dev *pdev, size_t *sizep)
715 1.1 riastrad {
716 1.1 riastrad
717 1.1 riastrad KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
718 1.1 riastrad
719 1.1 riastrad if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM,
720 1.1 riastrad (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR),
721 1.1 riastrad &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size)
722 1.1 riastrad != 0)
723 1.1 riastrad goto fail_mi;
724 1.1 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
725 1.1 riastrad
726 1.1 riastrad /* XXX This type is obviously wrong in general... */
727 1.1 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
728 1.1 riastrad pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
729 1.1 riastrad &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
730 1.1 riastrad pci_unmap_rom(pdev, NULL);
731 1.1 riastrad goto fail_mi;
732 1.1 riastrad }
733 1.1 riastrad goto success;
734 1.1 riastrad
735 1.1 riastrad fail_mi:
736 1.1 riastrad if (pci_map_rom_md(pdev) != 0)
737 1.1 riastrad goto fail_md;
738 1.1 riastrad
739 1.1 riastrad /* XXX This type is obviously wrong in general... */
740 1.1 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
741 1.1 riastrad pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
742 1.1 riastrad &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
743 1.1 riastrad pci_unmap_rom(pdev, NULL);
744 1.1 riastrad goto fail_md;
745 1.1 riastrad }
746 1.1 riastrad
747 1.1 riastrad success:
748 1.1 riastrad KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX);
749 1.1 riastrad *sizep = pdev->pd_rom_found_size;
750 1.1 riastrad pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst,
751 1.1 riastrad pdev->pd_rom_found_bsh);
752 1.1 riastrad return pdev->pd_rom_vaddr;
753 1.1 riastrad
754 1.1 riastrad fail_md:
755 1.1 riastrad return NULL;
756 1.1 riastrad }
757 1.1 riastrad
758 1.1 riastrad void __pci_rom_iomem *
759 1.1 riastrad pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep)
760 1.1 riastrad {
761 1.1 riastrad
762 1.1 riastrad *sizep = 0;
763 1.1 riastrad return NULL;
764 1.1 riastrad }
765 1.1 riastrad
766 1.1 riastrad int
767 1.1 riastrad pci_enable_rom(struct pci_dev *pdev)
768 1.1 riastrad {
769 1.1 riastrad const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
770 1.1 riastrad const pcitag_t tag = pdev->pd_pa.pa_tag;
771 1.1 riastrad pcireg_t addr;
772 1.1 riastrad int s;
773 1.1 riastrad
774 1.1 riastrad /* XXX Don't do anything if the ROM isn't there. */
775 1.1 riastrad
776 1.1 riastrad s = splhigh();
777 1.1 riastrad addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
778 1.1 riastrad addr |= PCI_MAPREG_ROM_ENABLE;
779 1.1 riastrad pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
780 1.1 riastrad splx(s);
781 1.1 riastrad
782 1.1 riastrad return 0;
783 1.1 riastrad }
784 1.1 riastrad
785 1.1 riastrad void
786 1.1 riastrad pci_disable_rom(struct pci_dev *pdev)
787 1.1 riastrad {
788 1.1 riastrad const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
789 1.1 riastrad const pcitag_t tag = pdev->pd_pa.pa_tag;
790 1.1 riastrad pcireg_t addr;
791 1.1 riastrad int s;
792 1.1 riastrad
793 1.1 riastrad s = splhigh();
794 1.1 riastrad addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
795 1.1 riastrad addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE;
796 1.1 riastrad pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
797 1.1 riastrad splx(s);
798 1.1 riastrad }
799 1.1 riastrad
800 1.1 riastrad bus_addr_t
801 1.1 riastrad pci_resource_start(struct pci_dev *pdev, unsigned i)
802 1.1 riastrad {
803 1.1 riastrad
804 1.26 mrg if (i >= PCI_NUM_RESOURCES)
805 1.26 mrg panic("resource %d >= max %d", i, PCI_NUM_RESOURCES);
806 1.1 riastrad return pdev->pd_resources[i].addr;
807 1.1 riastrad }
808 1.1 riastrad
809 1.1 riastrad bus_size_t
810 1.1 riastrad pci_resource_len(struct pci_dev *pdev, unsigned i)
811 1.1 riastrad {
812 1.1 riastrad
813 1.26 mrg if (i >= PCI_NUM_RESOURCES)
814 1.26 mrg panic("resource %d >= max %d", i, PCI_NUM_RESOURCES);
815 1.1 riastrad return pdev->pd_resources[i].size;
816 1.1 riastrad }
817 1.1 riastrad
818 1.1 riastrad bus_addr_t
819 1.1 riastrad pci_resource_end(struct pci_dev *pdev, unsigned i)
820 1.1 riastrad {
821 1.1 riastrad
822 1.1 riastrad return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1);
823 1.1 riastrad }
824 1.1 riastrad
825 1.1 riastrad int
826 1.1 riastrad pci_resource_flags(struct pci_dev *pdev, unsigned i)
827 1.1 riastrad {
828 1.1 riastrad
829 1.26 mrg if (i >= PCI_NUM_RESOURCES)
830 1.26 mrg panic("resource %d >= max %d", i, PCI_NUM_RESOURCES);
831 1.1 riastrad return pdev->pd_resources[i].flags;
832 1.1 riastrad }
833 1.1 riastrad
834 1.1 riastrad void __pci_iomem *
835 1.1 riastrad pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size)
836 1.1 riastrad {
837 1.1 riastrad int error;
838 1.1 riastrad
839 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES);
840 1.1 riastrad KASSERT(pdev->pd_resources[i].kva == NULL);
841 1.1 riastrad
842 1.1 riastrad if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM)
843 1.1 riastrad return NULL;
844 1.1 riastrad if (pdev->pd_resources[i].size < size)
845 1.1 riastrad return NULL;
846 1.1 riastrad error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr,
847 1.1 riastrad size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags,
848 1.1 riastrad &pdev->pd_resources[i].bsh);
849 1.6 riastrad if (error)
850 1.5 riastrad return NULL;
851 1.1 riastrad pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt;
852 1.1 riastrad pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst,
853 1.1 riastrad pdev->pd_resources[i].bsh);
854 1.1 riastrad pdev->pd_resources[i].mapped = true;
855 1.1 riastrad
856 1.1 riastrad return pdev->pd_resources[i].kva;
857 1.1 riastrad }
858 1.1 riastrad
859 1.1 riastrad void
860 1.1 riastrad pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva)
861 1.1 riastrad {
862 1.1 riastrad unsigned i;
863 1.1 riastrad
864 1.1 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
865 1.1 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) {
866 1.1 riastrad if (pdev->pd_resources[i].kva == kva)
867 1.1 riastrad break;
868 1.1 riastrad }
869 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES);
870 1.1 riastrad
871 1.1 riastrad pdev->pd_resources[i].kva = NULL;
872 1.1 riastrad bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh,
873 1.1 riastrad pdev->pd_resources[i].size);
874 1.1 riastrad }
875 1.1 riastrad
876 1.1 riastrad void
877 1.1 riastrad pci_save_state(struct pci_dev *pdev)
878 1.1 riastrad {
879 1.1 riastrad
880 1.1 riastrad KASSERT(pdev->pd_saved_state == NULL);
881 1.1 riastrad pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state),
882 1.1 riastrad KM_SLEEP);
883 1.1 riastrad pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
884 1.1 riastrad pdev->pd_saved_state);
885 1.1 riastrad }
886 1.1 riastrad
887 1.1 riastrad void
888 1.1 riastrad pci_restore_state(struct pci_dev *pdev)
889 1.1 riastrad {
890 1.1 riastrad
891 1.1 riastrad KASSERT(pdev->pd_saved_state != NULL);
892 1.1 riastrad pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
893 1.1 riastrad pdev->pd_saved_state);
894 1.1 riastrad kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state));
895 1.1 riastrad pdev->pd_saved_state = NULL;
896 1.1 riastrad }
897 1.1 riastrad
898 1.1 riastrad bool
899 1.1 riastrad pci_is_pcie(struct pci_dev *pdev)
900 1.1 riastrad {
901 1.1 riastrad
902 1.1 riastrad return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0);
903 1.1 riastrad }
904 1.1 riastrad
905 1.1 riastrad bool
906 1.1 riastrad pci_dma_supported(struct pci_dev *pdev, uintmax_t mask)
907 1.1 riastrad {
908 1.1 riastrad
909 1.1 riastrad /* XXX Cop-out. */
910 1.1 riastrad if (mask > DMA_BIT_MASK(32))
911 1.1 riastrad return pci_dma64_available(&pdev->pd_pa);
912 1.1 riastrad else
913 1.1 riastrad return true;
914 1.1 riastrad }
915 1.1 riastrad
916 1.1 riastrad bool
917 1.14 riastrad pci_is_thunderbolt_attached(struct pci_dev *pdev)
918 1.14 riastrad {
919 1.14 riastrad
920 1.14 riastrad /* XXX Cop-out. */
921 1.14 riastrad return false;
922 1.14 riastrad }
923 1.14 riastrad
924 1.14 riastrad bool
925 1.1 riastrad pci_is_root_bus(struct pci_bus *bus)
926 1.1 riastrad {
927 1.1 riastrad
928 1.24 mrg return bus->number == 0;
929 1.1 riastrad }
930 1.1 riastrad
931 1.1 riastrad int
932 1.1 riastrad pci_domain_nr(struct pci_bus *bus)
933 1.1 riastrad {
934 1.1 riastrad
935 1.23 riastrad return pci_get_segment(bus->pb_pc);
936 1.1 riastrad }
937 1.1 riastrad
938 1.1 riastrad /*
939 1.1 riastrad * We explicitly rename pci_enable/disable_device so that you have to
940 1.1 riastrad * review each use of them, since NetBSD's PCI API does _not_ respect
941 1.1 riastrad * our local enablecnt here, but there are different parts of NetBSD
942 1.1 riastrad * that automatically enable/disable like PMF, so you have to decide
943 1.1 riastrad * for each one whether to call it or not.
944 1.1 riastrad */
945 1.1 riastrad
946 1.1 riastrad int
947 1.1 riastrad linux_pci_enable_device(struct pci_dev *pdev)
948 1.1 riastrad {
949 1.1 riastrad const struct pci_attach_args *pa = &pdev->pd_pa;
950 1.1 riastrad pcireg_t csr;
951 1.1 riastrad int s;
952 1.1 riastrad
953 1.1 riastrad if (pdev->pd_enablecnt++)
954 1.1 riastrad return 0;
955 1.1 riastrad
956 1.1 riastrad s = splhigh();
957 1.1 riastrad csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
958 1.4 riastrad /* If someone else (firmware) already enabled it, credit them. */
959 1.4 riastrad if (csr & (PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE))
960 1.4 riastrad pdev->pd_enablecnt++;
961 1.1 riastrad csr |= PCI_COMMAND_IO_ENABLE;
962 1.1 riastrad csr |= PCI_COMMAND_MEM_ENABLE;
963 1.1 riastrad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
964 1.1 riastrad splx(s);
965 1.1 riastrad
966 1.1 riastrad return 0;
967 1.1 riastrad }
968 1.1 riastrad
969 1.1 riastrad void
970 1.1 riastrad linux_pci_disable_device(struct pci_dev *pdev)
971 1.1 riastrad {
972 1.1 riastrad const struct pci_attach_args *pa = &pdev->pd_pa;
973 1.1 riastrad pcireg_t csr;
974 1.1 riastrad int s;
975 1.1 riastrad
976 1.1 riastrad if (--pdev->pd_enablecnt)
977 1.1 riastrad return;
978 1.1 riastrad
979 1.1 riastrad s = splhigh();
980 1.1 riastrad csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
981 1.1 riastrad csr &= ~PCI_COMMAND_IO_ENABLE;
982 1.1 riastrad csr &= ~PCI_COMMAND_MEM_ENABLE;
983 1.1 riastrad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
984 1.1 riastrad splx(s);
985 1.1 riastrad }
986 1.1 riastrad
987 1.1 riastrad void
988 1.1 riastrad linux_pci_dev_destroy(struct pci_dev *pdev)
989 1.1 riastrad {
990 1.1 riastrad unsigned i;
991 1.1 riastrad
992 1.25 mrg if (pdev->bus->self != NULL) {
993 1.25 mrg kmem_free(pdev->bus->self, sizeof(*pdev->bus->self));
994 1.25 mrg }
995 1.1 riastrad if (pdev->bus != NULL) {
996 1.1 riastrad kmem_free(pdev->bus, sizeof(*pdev->bus));
997 1.1 riastrad pdev->bus = NULL;
998 1.1 riastrad }
999 1.1 riastrad if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) {
1000 1.1 riastrad pci_unmap_rom(pdev, pdev->pd_rom_vaddr);
1001 1.1 riastrad pdev->pd_rom_vaddr = 0;
1002 1.1 riastrad }
1003 1.1 riastrad for (i = 0; i < __arraycount(pdev->pd_resources); i++) {
1004 1.1 riastrad if (!pdev->pd_resources[i].mapped)
1005 1.1 riastrad continue;
1006 1.1 riastrad bus_space_unmap(pdev->pd_resources[i].bst,
1007 1.1 riastrad pdev->pd_resources[i].bsh, pdev->pd_resources[i].size);
1008 1.1 riastrad }
1009 1.1 riastrad
1010 1.1 riastrad /* There is no way these should be still in use. */
1011 1.1 riastrad KASSERT(pdev->pd_saved_state == NULL);
1012 1.1 riastrad KASSERT(pdev->pd_intr_handles == NULL);
1013 1.1 riastrad }
1014 1.24 mrg
1015 1.24 mrg enum pci_bus_speed
1016 1.24 mrg pcie_get_speed_cap(struct pci_dev *dev)
1017 1.24 mrg {
1018 1.24 mrg pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
1019 1.24 mrg pcitag_t tag = dev->pd_pa.pa_tag;
1020 1.24 mrg pcireg_t lcap, lcap2, xcap;
1021 1.24 mrg int off;
1022 1.24 mrg
1023 1.24 mrg /* Must have capabilities. */
1024 1.24 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
1025 1.24 mrg return PCI_SPEED_UNKNOWN;
1026 1.24 mrg
1027 1.24 mrg /* Only PCIe 3.x has LCAP2. */
1028 1.24 mrg xcap = pci_conf_read(pc, tag, off + PCIE_XCAP);
1029 1.24 mrg if (__SHIFTOUT(xcap, PCIE_XCAP_VER_MASK) >= 2) {
1030 1.24 mrg lcap2 = pci_conf_read(pc, tag, off + PCIE_LCAP2);
1031 1.24 mrg if (lcap2) {
1032 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS64) != 0) {
1033 1.24 mrg return PCIE_SPEED_64_0GT;
1034 1.24 mrg }
1035 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS32) != 0) {
1036 1.24 mrg return PCIE_SPEED_32_0GT;
1037 1.24 mrg }
1038 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS16) != 0) {
1039 1.24 mrg return PCIE_SPEED_16_0GT;
1040 1.24 mrg }
1041 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS8) != 0) {
1042 1.24 mrg return PCIE_SPEED_8_0GT;
1043 1.24 mrg }
1044 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS5) != 0) {
1045 1.24 mrg return PCIE_SPEED_5_0GT;
1046 1.24 mrg }
1047 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS2) != 0) {
1048 1.24 mrg return PCIE_SPEED_2_5GT;
1049 1.24 mrg }
1050 1.24 mrg }
1051 1.24 mrg }
1052 1.24 mrg
1053 1.24 mrg lcap = pci_conf_read(pc, tag, off + PCIE_LCAP);
1054 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_64) {
1055 1.24 mrg return PCIE_SPEED_64_0GT;
1056 1.24 mrg }
1057 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_32) {
1058 1.24 mrg return PCIE_SPEED_32_0GT;
1059 1.24 mrg }
1060 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_16) {
1061 1.24 mrg return PCIE_SPEED_16_0GT;
1062 1.24 mrg }
1063 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_8) {
1064 1.24 mrg return PCIE_SPEED_8_0GT;
1065 1.24 mrg }
1066 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_5) {
1067 1.24 mrg return PCIE_SPEED_5_0GT;
1068 1.24 mrg }
1069 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_2) {
1070 1.24 mrg return PCIE_SPEED_2_5GT;
1071 1.24 mrg }
1072 1.24 mrg
1073 1.24 mrg return PCI_SPEED_UNKNOWN;
1074 1.24 mrg }
1075 1.24 mrg
1076 1.24 mrg /*
1077 1.24 mrg * This should walk the tree, it only checks this device currently.
1078 1.24 mrg * It also does not write to limiting_dev (the only caller in drm2
1079 1.24 mrg * currently does not use it.)
1080 1.24 mrg */
1081 1.24 mrg unsigned
1082 1.24 mrg pcie_bandwidth_available(struct pci_dev *dev,
1083 1.24 mrg struct pci_dev **limiting_dev,
1084 1.24 mrg enum pci_bus_speed *speed,
1085 1.24 mrg enum pcie_link_width *width)
1086 1.24 mrg {
1087 1.24 mrg pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
1088 1.24 mrg pcitag_t tag = dev->pd_pa.pa_tag;
1089 1.24 mrg pcireg_t lcsr;
1090 1.24 mrg unsigned per_line_speed, num_lanes;
1091 1.24 mrg int off;
1092 1.24 mrg
1093 1.24 mrg /* Must have capabilities. */
1094 1.24 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
1095 1.24 mrg return 0;
1096 1.24 mrg
1097 1.24 mrg if (speed)
1098 1.24 mrg *speed = PCI_SPEED_UNKNOWN;
1099 1.24 mrg if (width)
1100 1.24 mrg *width = 0;
1101 1.24 mrg
1102 1.24 mrg lcsr = pci_conf_read(pc, tag, off + PCIE_LCSR);
1103 1.24 mrg
1104 1.24 mrg switch (lcsr & PCIE_LCSR_NLW) {
1105 1.24 mrg case PCIE_LCSR_NLW_X1:
1106 1.24 mrg case PCIE_LCSR_NLW_X2:
1107 1.24 mrg case PCIE_LCSR_NLW_X4:
1108 1.24 mrg case PCIE_LCSR_NLW_X8:
1109 1.24 mrg case PCIE_LCSR_NLW_X12:
1110 1.24 mrg case PCIE_LCSR_NLW_X16:
1111 1.24 mrg case PCIE_LCSR_NLW_X32:
1112 1.24 mrg num_lanes = __SHIFTOUT(lcsr, PCIE_LCSR_NLW);
1113 1.24 mrg if (width)
1114 1.24 mrg *width = num_lanes;
1115 1.24 mrg break;
1116 1.24 mrg default:
1117 1.24 mrg num_lanes = 0;
1118 1.24 mrg break;
1119 1.24 mrg }
1120 1.24 mrg
1121 1.24 mrg switch (__SHIFTOUT(lcsr, PCIE_LCSR_LINKSPEED)) {
1122 1.24 mrg case PCIE_LCSR_LINKSPEED_2:
1123 1.24 mrg *speed = PCIE_SPEED_2_5GT;
1124 1.24 mrg per_line_speed = 2500 * 8 / 10;
1125 1.24 mrg break;
1126 1.24 mrg case PCIE_LCSR_LINKSPEED_5:
1127 1.24 mrg *speed = PCIE_SPEED_5_0GT;
1128 1.24 mrg per_line_speed = 5000 * 8 / 10;
1129 1.24 mrg break;
1130 1.24 mrg case PCIE_LCSR_LINKSPEED_8:
1131 1.24 mrg *speed = PCIE_SPEED_8_0GT;
1132 1.24 mrg per_line_speed = 8000 * 128 / 130;
1133 1.24 mrg break;
1134 1.24 mrg case PCIE_LCSR_LINKSPEED_16:
1135 1.24 mrg *speed = PCIE_SPEED_16_0GT;
1136 1.24 mrg per_line_speed = 16000 * 128 / 130;
1137 1.24 mrg break;
1138 1.24 mrg case PCIE_LCSR_LINKSPEED_32:
1139 1.24 mrg *speed = PCIE_SPEED_32_0GT;
1140 1.24 mrg per_line_speed = 32000 * 128 / 130;
1141 1.24 mrg break;
1142 1.24 mrg case PCIE_LCSR_LINKSPEED_64:
1143 1.24 mrg *speed = PCIE_SPEED_64_0GT;
1144 1.24 mrg per_line_speed = 64000 * 128 / 130;
1145 1.24 mrg break;
1146 1.24 mrg default:
1147 1.24 mrg per_line_speed = 0;
1148 1.24 mrg }
1149 1.24 mrg
1150 1.24 mrg return num_lanes * per_line_speed;
1151 1.24 mrg }
1152