| @@ -1,807 +1,803 @@ | | | @@ -1,807 +1,803 @@ |
1 | /* $NetBSD: linux_pci.c,v 1.20 2021/12/19 12:00:16 riastradh Exp $ */ | | 1 | /* $NetBSD: linux_pci.c,v 1.21 2022/02/27 14:19:20 riastradh Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Taylor R. Campbell. | | 8 | * by Taylor R. Campbell. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #ifdef _KERNEL_OPT | | 32 | #ifdef _KERNEL_OPT |
33 | #include "acpica.h" | | 33 | #include "acpica.h" |
34 | #include "opt_pci.h" | | 34 | #include "opt_pci.h" |
35 | #endif | | 35 | #endif |
36 | | | 36 | |
37 | #include <sys/cdefs.h> | | 37 | #include <sys/cdefs.h> |
38 | __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.20 2021/12/19 12:00:16 riastradh Exp $"); | | 38 | __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.21 2022/02/27 14:19:20 riastradh Exp $"); |
39 | | | 39 | |
40 | #if NACPICA > 0 | | 40 | #if NACPICA > 0 |
41 | #include <dev/acpi/acpivar.h> | | 41 | #include <dev/acpi/acpivar.h> |
42 | #include <dev/acpi/acpi_pci.h> | | 42 | #include <dev/acpi/acpi_pci.h> |
43 | #endif | | 43 | #endif |
44 | | | 44 | |
45 | #include <linux/pci.h> | | 45 | #include <linux/pci.h> |
46 | | | 46 | |
47 | #include <drm/drm_agp_netbsd.h> | | 47 | #include <drm/drm_agp_netbsd.h> |
48 | | | 48 | |
49 | device_t | | 49 | device_t |
50 | pci_dev_dev(struct pci_dev *pdev) | | 50 | pci_dev_dev(struct pci_dev *pdev) |
51 | { | | 51 | { |
52 | | | 52 | |
53 | return pdev->pd_dev; | | 53 | return pdev->pd_dev; |
54 | } | | 54 | } |
55 | | | 55 | |
56 | void | | 56 | void |
57 | pci_set_drvdata(struct pci_dev *pdev, void *drvdata) | | 57 | pci_set_drvdata(struct pci_dev *pdev, void *drvdata) |
58 | { | | 58 | { |
59 | pdev->pd_drvdata = drvdata; | | 59 | pdev->pd_drvdata = drvdata; |
60 | } | | 60 | } |
61 | | | 61 | |
62 | void * | | 62 | void * |
63 | pci_get_drvdata(struct pci_dev *pdev) | | 63 | pci_get_drvdata(struct pci_dev *pdev) |
64 | { | | 64 | { |
65 | return pdev->pd_drvdata; | | 65 | return pdev->pd_drvdata; |
66 | } | | 66 | } |
67 | | | 67 | |
68 | const char * | | 68 | const char * |
69 | pci_name(struct pci_dev *pdev) | | 69 | pci_name(struct pci_dev *pdev) |
70 | { | | 70 | { |
71 | | | 71 | |
72 | /* XXX not sure this has the right format */ | | 72 | /* XXX not sure this has the right format */ |
73 | return device_xname(pci_dev_dev(pdev)); | | 73 | return device_xname(pci_dev_dev(pdev)); |
74 | } | | 74 | } |
75 | | | 75 | |
76 | void | | 76 | void |
77 | linux_pci_dev_init(struct pci_dev *pdev, device_t dev, device_t parent, | | 77 | linux_pci_dev_init(struct pci_dev *pdev, device_t dev, device_t parent, |
78 | const struct pci_attach_args *pa, int kludges) | | 78 | const struct pci_attach_args *pa, int kludges) |
79 | { | | 79 | { |
80 | const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag, | | 80 | const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag, |
81 | PCI_SUBSYS_ID_REG); | | 81 | PCI_SUBSYS_ID_REG); |
82 | unsigned i; | | 82 | unsigned i; |
83 | | | 83 | |
84 | memset(pdev, 0, sizeof(*pdev)); /* paranoia */ | | 84 | memset(pdev, 0, sizeof(*pdev)); /* paranoia */ |
85 | | | 85 | |
86 | pdev->pd_pa = *pa; | | 86 | pdev->pd_pa = *pa; |
87 | pdev->pd_kludges = kludges; | | 87 | pdev->pd_kludges = kludges; |
88 | pdev->pd_rom_vaddr = NULL; | | 88 | pdev->pd_rom_vaddr = NULL; |
89 | pdev->pd_dev = dev; | | 89 | pdev->pd_dev = dev; |
90 | #if (NACPICA > 0) | | 90 | #if (NACPICA > 0) |
91 | #ifdef __HAVE_PCI_GET_SEGMENT | | | |
92 | const int seg = pci_get_segment(pa->pa_pc); | | 91 | const int seg = pci_get_segment(pa->pa_pc); |
93 | #else | | | |
94 | const int seg = 0; | | | |
95 | #endif | | | |
96 | pdev->pd_ad = acpi_pcidev_find(seg, pa->pa_bus, | | 92 | pdev->pd_ad = acpi_pcidev_find(seg, pa->pa_bus, |
97 | pa->pa_device, pa->pa_function); | | 93 | pa->pa_device, pa->pa_function); |
98 | #else | | 94 | #else |
99 | pdev->pd_ad = NULL; | | 95 | pdev->pd_ad = NULL; |
100 | #endif | | 96 | #endif |
101 | pdev->pd_saved_state = NULL; | | 97 | pdev->pd_saved_state = NULL; |
102 | pdev->pd_intr_handles = NULL; | | 98 | pdev->pd_intr_handles = NULL; |
103 | pdev->pd_drvdata = NULL; | | 99 | pdev->pd_drvdata = NULL; |
104 | pdev->bus = kmem_zalloc(sizeof(*pdev->bus), KM_NOSLEEP); | | 100 | pdev->bus = kmem_zalloc(sizeof(*pdev->bus), KM_NOSLEEP); |
105 | pdev->bus->pb_pc = pa->pa_pc; | | 101 | pdev->bus->pb_pc = pa->pa_pc; |
106 | pdev->bus->pb_dev = parent; | | 102 | pdev->bus->pb_dev = parent; |
107 | pdev->bus->number = pa->pa_bus; | | 103 | pdev->bus->number = pa->pa_bus; |
108 | pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function); | | 104 | pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function); |
109 | pdev->vendor = PCI_VENDOR(pa->pa_id); | | 105 | pdev->vendor = PCI_VENDOR(pa->pa_id); |
110 | pdev->device = PCI_PRODUCT(pa->pa_id); | | 106 | pdev->device = PCI_PRODUCT(pa->pa_id); |
111 | pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id); | | 107 | pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id); |
112 | pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id); | | 108 | pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id); |
113 | pdev->revision = PCI_REVISION(pa->pa_class); | | 109 | pdev->revision = PCI_REVISION(pa->pa_class); |
114 | pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */ | | 110 | pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */ |
115 | | | 111 | |
116 | CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); | | 112 | CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); |
117 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | | 113 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
118 | const int reg = PCI_BAR(i); | | 114 | const int reg = PCI_BAR(i); |
119 | | | 115 | |
120 | pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc, | | 116 | pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc, |
121 | pa->pa_tag, reg); | | 117 | pa->pa_tag, reg); |
122 | if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, | | 118 | if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, |
123 | pdev->pd_resources[i].type, | | 119 | pdev->pd_resources[i].type, |
124 | &pdev->pd_resources[i].addr, | | 120 | &pdev->pd_resources[i].addr, |
125 | &pdev->pd_resources[i].size, | | 121 | &pdev->pd_resources[i].size, |
126 | &pdev->pd_resources[i].flags)) { | | 122 | &pdev->pd_resources[i].flags)) { |
127 | pdev->pd_resources[i].addr = 0; | | 123 | pdev->pd_resources[i].addr = 0; |
128 | pdev->pd_resources[i].size = 0; | | 124 | pdev->pd_resources[i].size = 0; |
129 | pdev->pd_resources[i].flags = 0; | | 125 | pdev->pd_resources[i].flags = 0; |
130 | } | | 126 | } |
131 | pdev->pd_resources[i].kva = NULL; | | 127 | pdev->pd_resources[i].kva = NULL; |
132 | pdev->pd_resources[i].mapped = false; | | 128 | pdev->pd_resources[i].mapped = false; |
133 | } | | 129 | } |
134 | } | | 130 | } |
135 | | | 131 | |
136 | int | | 132 | int |
137 | pci_find_capability(struct pci_dev *pdev, int cap) | | 133 | pci_find_capability(struct pci_dev *pdev, int cap) |
138 | { | | 134 | { |
139 | | | 135 | |
140 | return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap, | | 136 | return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap, |
141 | NULL, NULL); | | 137 | NULL, NULL); |
142 | } | | 138 | } |
143 | | | 139 | |
144 | int | | 140 | int |
145 | pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep) | | 141 | pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep) |
146 | { | | 142 | { |
147 | | | 143 | |
148 | KASSERT(!ISSET(reg, 3)); | | 144 | KASSERT(!ISSET(reg, 3)); |
149 | *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg); | | 145 | *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg); |
150 | return 0; | | 146 | return 0; |
151 | } | | 147 | } |
152 | | | 148 | |
153 | int | | 149 | int |
154 | pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep) | | 150 | pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep) |
155 | { | | 151 | { |
156 | | | 152 | |
157 | KASSERT(!ISSET(reg, 1)); | | 153 | KASSERT(!ISSET(reg, 1)); |
158 | *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 154 | *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
159 | (reg &~ 2)) >> (8 * (reg & 2)); | | 155 | (reg &~ 2)) >> (8 * (reg & 2)); |
160 | return 0; | | 156 | return 0; |
161 | } | | 157 | } |
162 | | | 158 | |
163 | int | | 159 | int |
164 | pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep) | | 160 | pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep) |
165 | { | | 161 | { |
166 | | | 162 | |
167 | *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 163 | *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
168 | (reg &~ 3)) >> (8 * (reg & 3)); | | 164 | (reg &~ 3)) >> (8 * (reg & 3)); |
169 | return 0; | | 165 | return 0; |
170 | } | | 166 | } |
171 | | | 167 | |
172 | int | | 168 | int |
173 | pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value) | | 169 | pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value) |
174 | { | | 170 | { |
175 | | | 171 | |
176 | KASSERT(!ISSET(reg, 3)); | | 172 | KASSERT(!ISSET(reg, 3)); |
177 | pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value); | | 173 | pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value); |
178 | return 0; | | 174 | return 0; |
179 | } | | 175 | } |
180 | | | 176 | |
181 | int | | 177 | int |
182 | pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg, | | 178 | pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg, |
183 | uint32_t *valuep) | | 179 | uint32_t *valuep) |
184 | { | | 180 | { |
185 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), | | 181 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), |
186 | PCI_FUNC(devfn)); | | 182 | PCI_FUNC(devfn)); |
187 | | | 183 | |
188 | KASSERT(!ISSET(reg, 1)); | | 184 | KASSERT(!ISSET(reg, 1)); |
189 | *valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3)); | | 185 | *valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3)); |
190 | return 0; | | 186 | return 0; |
191 | } | | 187 | } |
192 | | | 188 | |
193 | int | | 189 | int |
194 | pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg, | | 190 | pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg, |
195 | uint16_t *valuep) | | 191 | uint16_t *valuep) |
196 | { | | 192 | { |
197 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), | | 193 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), |
198 | PCI_FUNC(devfn)); | | 194 | PCI_FUNC(devfn)); |
199 | | | 195 | |
200 | KASSERT(!ISSET(reg, 1)); | | 196 | KASSERT(!ISSET(reg, 1)); |
201 | *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2)); | | 197 | *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2)); |
202 | return 0; | | 198 | return 0; |
203 | } | | 199 | } |
204 | | | 200 | |
205 | int | | 201 | int |
206 | pci_bus_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg, | | 202 | pci_bus_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg, |
207 | uint8_t *valuep) | | 203 | uint8_t *valuep) |
208 | { | | 204 | { |
209 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), | | 205 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), |
210 | PCI_FUNC(devfn)); | | 206 | PCI_FUNC(devfn)); |
211 | | | 207 | |
212 | *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3)); | | 208 | *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3)); |
213 | return 0; | | 209 | return 0; |
214 | } | | 210 | } |
215 | | | 211 | |
216 | int | | 212 | int |
217 | pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg, | | 213 | pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg, |
218 | uint32_t value) | | 214 | uint32_t value) |
219 | { | | 215 | { |
220 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), | | 216 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), |
221 | PCI_FUNC(devfn)); | | 217 | PCI_FUNC(devfn)); |
222 | | | 218 | |
223 | KASSERT(!ISSET(reg, 3)); | | 219 | KASSERT(!ISSET(reg, 3)); |
224 | pci_conf_write(bus->pb_pc, tag, reg, value); | | 220 | pci_conf_write(bus->pb_pc, tag, reg, value); |
225 | return 0; | | 221 | return 0; |
226 | } | | 222 | } |
227 | | | 223 | |
228 | static void | | 224 | static void |
229 | pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes, | | 225 | pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes, |
230 | uint32_t value) | | 226 | uint32_t value) |
231 | { | | 227 | { |
232 | const uint32_t mask = ~((~0UL) << (8 * bytes)); | | 228 | const uint32_t mask = ~((~0UL) << (8 * bytes)); |
233 | const int reg32 = (reg &~ 3); | | 229 | const int reg32 = (reg &~ 3); |
234 | const unsigned int shift = (8 * (reg & 3)); | | 230 | const unsigned int shift = (8 * (reg & 3)); |
235 | uint32_t value32; | | 231 | uint32_t value32; |
236 | | | 232 | |
237 | KASSERT(bytes <= 4); | | 233 | KASSERT(bytes <= 4); |
238 | KASSERT(!ISSET(value, ~mask)); | | 234 | KASSERT(!ISSET(value, ~mask)); |
239 | value32 = pci_conf_read(pc, tag, reg32); | | 235 | value32 = pci_conf_read(pc, tag, reg32); |
240 | value32 &=~ (mask << shift); | | 236 | value32 &=~ (mask << shift); |
241 | value32 |= (value << shift); | | 237 | value32 |= (value << shift); |
242 | pci_conf_write(pc, tag, reg32, value32); | | 238 | pci_conf_write(pc, tag, reg32, value32); |
243 | } | | 239 | } |
244 | | | 240 | |
245 | int | | 241 | int |
246 | pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value) | | 242 | pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value) |
247 | { | | 243 | { |
248 | | | 244 | |
249 | KASSERT(!ISSET(reg, 1)); | | 245 | KASSERT(!ISSET(reg, 1)); |
250 | pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value); | | 246 | pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value); |
251 | return 0; | | 247 | return 0; |
252 | } | | 248 | } |
253 | | | 249 | |
254 | int | | 250 | int |
255 | pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value) | | 251 | pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value) |
256 | { | | 252 | { |
257 | | | 253 | |
258 | pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value); | | 254 | pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value); |
259 | return 0; | | 255 | return 0; |
260 | } | | 256 | } |
261 | | | 257 | |
262 | int | | 258 | int |
263 | pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg, | | 259 | pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg, |
264 | uint16_t value) | | 260 | uint16_t value) |
265 | { | | 261 | { |
266 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), | | 262 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), |
267 | PCI_FUNC(devfn)); | | 263 | PCI_FUNC(devfn)); |
268 | | | 264 | |
269 | KASSERT(!ISSET(reg, 1)); | | 265 | KASSERT(!ISSET(reg, 1)); |
270 | pci_rmw_config(bus->pb_pc, tag, reg, 2, value); | | 266 | pci_rmw_config(bus->pb_pc, tag, reg, 2, value); |
271 | return 0; | | 267 | return 0; |
272 | } | | 268 | } |
273 | | | 269 | |
274 | int | | 270 | int |
275 | pci_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg, | | 271 | pci_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg, |
276 | uint8_t value) | | 272 | uint8_t value) |
277 | { | | 273 | { |
278 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), | | 274 | pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), |
279 | PCI_FUNC(devfn)); | | 275 | PCI_FUNC(devfn)); |
280 | | | 276 | |
281 | pci_rmw_config(bus->pb_pc, tag, reg, 1, value); | | 277 | pci_rmw_config(bus->pb_pc, tag, reg, 1, value); |
282 | return 0; | | 278 | return 0; |
283 | } | | 279 | } |
284 | | | 280 | |
285 | int | | 281 | int |
286 | pci_enable_msi(struct pci_dev *pdev) | | 282 | pci_enable_msi(struct pci_dev *pdev) |
287 | { | | 283 | { |
288 | const struct pci_attach_args *const pa = &pdev->pd_pa; | | 284 | const struct pci_attach_args *const pa = &pdev->pd_pa; |
289 | | | 285 | |
290 | if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1)) | | 286 | if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1)) |
291 | return -EINVAL; | | 287 | return -EINVAL; |
292 | | | 288 | |
293 | pdev->msi_enabled = 1; | | 289 | pdev->msi_enabled = 1; |
294 | return 0; | | 290 | return 0; |
295 | } | | 291 | } |
296 | | | 292 | |
297 | void | | 293 | void |
298 | pci_disable_msi(struct pci_dev *pdev __unused) | | 294 | pci_disable_msi(struct pci_dev *pdev __unused) |
299 | { | | 295 | { |
300 | const struct pci_attach_args *const pa = &pdev->pd_pa; | | 296 | const struct pci_attach_args *const pa = &pdev->pd_pa; |
301 | | | 297 | |
302 | if (pdev->pd_intr_handles != NULL) { | | 298 | if (pdev->pd_intr_handles != NULL) { |
303 | pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1); | | 299 | pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1); |
304 | pdev->pd_intr_handles = NULL; | | 300 | pdev->pd_intr_handles = NULL; |
305 | } | | 301 | } |
306 | pdev->msi_enabled = 0; | | 302 | pdev->msi_enabled = 0; |
307 | } | | 303 | } |
308 | | | 304 | |
309 | void | | 305 | void |
310 | pci_set_master(struct pci_dev *pdev) | | 306 | pci_set_master(struct pci_dev *pdev) |
311 | { | | 307 | { |
312 | pcireg_t csr; | | 308 | pcireg_t csr; |
313 | | | 309 | |
314 | csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 310 | csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
315 | PCI_COMMAND_STATUS_REG); | | 311 | PCI_COMMAND_STATUS_REG); |
316 | csr |= PCI_COMMAND_MASTER_ENABLE; | | 312 | csr |= PCI_COMMAND_MASTER_ENABLE; |
317 | pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 313 | pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
318 | PCI_COMMAND_STATUS_REG, csr); | | 314 | PCI_COMMAND_STATUS_REG, csr); |
319 | } | | 315 | } |
320 | | | 316 | |
321 | void | | 317 | void |
322 | pci_clear_master(struct pci_dev *pdev) | | 318 | pci_clear_master(struct pci_dev *pdev) |
323 | { | | 319 | { |
324 | pcireg_t csr; | | 320 | pcireg_t csr; |
325 | | | 321 | |
326 | csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 322 | csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
327 | PCI_COMMAND_STATUS_REG); | | 323 | PCI_COMMAND_STATUS_REG); |
328 | csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE; | | 324 | csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE; |
329 | pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 325 | pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
330 | PCI_COMMAND_STATUS_REG, csr); | | 326 | PCI_COMMAND_STATUS_REG, csr); |
331 | } | | 327 | } |
332 | | | 328 | |
333 | bus_addr_t | | 329 | bus_addr_t |
334 | pcibios_align_resource(void *p, const struct resource *resource, | | 330 | pcibios_align_resource(void *p, const struct resource *resource, |
335 | bus_addr_t addr, bus_size_t size) | | 331 | bus_addr_t addr, bus_size_t size) |
336 | { | | 332 | { |
337 | panic("pcibios_align_resource has accessed unaligned neurons!"); | | 333 | panic("pcibios_align_resource has accessed unaligned neurons!"); |
338 | } | | 334 | } |
339 | | | 335 | |
340 | int | | 336 | int |
341 | pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource, | | 337 | pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource, |
342 | bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused, | | 338 | bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused, |
343 | bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t, | | 339 | bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t, |
344 | bus_size_t) __unused, | | 340 | bus_size_t) __unused, |
345 | struct pci_dev *pdev) | | 341 | struct pci_dev *pdev) |
346 | { | | 342 | { |
347 | const struct pci_attach_args *const pa = &pdev->pd_pa; | | 343 | const struct pci_attach_args *const pa = &pdev->pd_pa; |
348 | bus_space_tag_t bst; | | 344 | bus_space_tag_t bst; |
349 | int error; | | 345 | int error; |
350 | | | 346 | |
351 | switch (resource->flags) { | | 347 | switch (resource->flags) { |
352 | case IORESOURCE_MEM: | | 348 | case IORESOURCE_MEM: |
353 | bst = pa->pa_memt; | | 349 | bst = pa->pa_memt; |
354 | break; | | 350 | break; |
355 | | | 351 | |
356 | case IORESOURCE_IO: | | 352 | case IORESOURCE_IO: |
357 | bst = pa->pa_iot; | | 353 | bst = pa->pa_iot; |
358 | break; | | 354 | break; |
359 | | | 355 | |
360 | default: | | 356 | default: |
361 | panic("I don't know what kind of resource you want!"); | | 357 | panic("I don't know what kind of resource you want!"); |
362 | } | | 358 | } |
363 | | | 359 | |
364 | resource->r_bst = bst; | | 360 | resource->r_bst = bst; |
365 | error = bus_space_alloc(bst, start, __type_max(bus_addr_t), | | 361 | error = bus_space_alloc(bst, start, __type_max(bus_addr_t), |
366 | size, align, 0, 0, &resource->start, &resource->r_bsh); | | 362 | size, align, 0, 0, &resource->start, &resource->r_bsh); |
367 | if (error) | | 363 | if (error) |
368 | return error; | | 364 | return error; |
369 | | | 365 | |
370 | resource->end = start + (size - 1); | | 366 | resource->end = start + (size - 1); |
371 | return 0; | | 367 | return 0; |
372 | } | | 368 | } |
373 | | | 369 | |
374 | /* | | 370 | /* |
375 | * XXX Mega-kludgerific! pci_get_bus_and_slot and pci_get_class are | | 371 | * XXX Mega-kludgerific! pci_get_bus_and_slot and pci_get_class are |
376 | * defined only for their single purposes in i915drm, in | | 372 | * defined only for their single purposes in i915drm, in |
377 | * i915_get_bridge_dev and intel_detect_pch. We can't define them more | | 373 | * i915_get_bridge_dev and intel_detect_pch. We can't define them more |
378 | * generally without adapting pci_find_device (and pci_enumerate_bus | | 374 | * generally without adapting pci_find_device (and pci_enumerate_bus |
379 | * internally) to pass a cookie through. | | 375 | * internally) to pass a cookie through. |
380 | */ | | 376 | */ |
381 | | | 377 | |
382 | static int | | 378 | static int |
383 | pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa) | | 379 | pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa) |
384 | { | | 380 | { |
385 | | | 381 | |
386 | /* XXX domain */ | | 382 | /* XXX domain */ |
387 | if (pa->pa_bus != 0) | | 383 | if (pa->pa_bus != 0) |
388 | return 0; | | 384 | return 0; |
389 | if (pa->pa_device != 0) | | 385 | if (pa->pa_device != 0) |
390 | return 0; | | 386 | return 0; |
391 | if (pa->pa_function != 0) | | 387 | if (pa->pa_function != 0) |
392 | return 0; | | 388 | return 0; |
393 | | | 389 | |
394 | return 1; | | 390 | return 1; |
395 | } | | 391 | } |
396 | | | 392 | |
397 | struct pci_dev * | | 393 | struct pci_dev * |
398 | pci_get_domain_bus_and_slot(int domain, int bus, int slot) | | 394 | pci_get_domain_bus_and_slot(int domain, int bus, int slot) |
399 | { | | 395 | { |
400 | struct pci_attach_args pa; | | 396 | struct pci_attach_args pa; |
401 | | | 397 | |
402 | KASSERT(domain == 0); | | 398 | KASSERT(domain == 0); |
403 | KASSERT(bus == 0); | | 399 | KASSERT(bus == 0); |
404 | KASSERT(slot == PCI_DEVFN(0, 0)); | | 400 | KASSERT(slot == PCI_DEVFN(0, 0)); |
405 | | | 401 | |
406 | if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0)) | | 402 | if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0)) |
407 | return NULL; | | 403 | return NULL; |
408 | | | 404 | |
409 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); | | 405 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); |
410 | linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); | | 406 | linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); |
411 | | | 407 | |
412 | return pdev; | | 408 | return pdev; |
413 | } | | 409 | } |
414 | | | 410 | |
415 | static int | | 411 | static int |
416 | pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa) | | 412 | pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa) |
417 | { | | 413 | { |
418 | | | 414 | |
419 | if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE) | | 415 | if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE) |
420 | return 0; | | 416 | return 0; |
421 | if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA) | | 417 | if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA) |
422 | return 0; | | 418 | return 0; |
423 | | | 419 | |
424 | return 1; | | 420 | return 1; |
425 | } | | 421 | } |
426 | | | 422 | |
427 | void | | 423 | void |
428 | pci_dev_put(struct pci_dev *pdev) | | 424 | pci_dev_put(struct pci_dev *pdev) |
429 | { | | 425 | { |
430 | | | 426 | |
431 | if (pdev == NULL) | | 427 | if (pdev == NULL) |
432 | return; | | 428 | return; |
433 | | | 429 | |
434 | KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE)); | | 430 | KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE)); |
435 | kmem_free(pdev->bus, sizeof(*pdev->bus)); | | 431 | kmem_free(pdev->bus, sizeof(*pdev->bus)); |
436 | kmem_free(pdev, sizeof(*pdev)); | | 432 | kmem_free(pdev, sizeof(*pdev)); |
437 | } | | 433 | } |
438 | | | 434 | |
439 | struct pci_dev * /* XXX i915 kludge */ | | 435 | struct pci_dev * /* XXX i915 kludge */ |
440 | pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from) | | 436 | pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from) |
441 | { | | 437 | { |
442 | struct pci_attach_args pa; | | 438 | struct pci_attach_args pa; |
443 | | | 439 | |
444 | KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8)); | | 440 | KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8)); |
445 | | | 441 | |
446 | if (from != NULL) { | | 442 | if (from != NULL) { |
447 | pci_dev_put(from); | | 443 | pci_dev_put(from); |
448 | return NULL; | | 444 | return NULL; |
449 | } | | 445 | } |
450 | | | 446 | |
451 | if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge)) | | 447 | if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge)) |
452 | return NULL; | | 448 | return NULL; |
453 | | | 449 | |
454 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); | | 450 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); |
455 | linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); | | 451 | linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); |
456 | | | 452 | |
457 | return pdev; | | 453 | return pdev; |
458 | } | | 454 | } |
459 | | | 455 | |
460 | int | | 456 | int |
461 | pci_dev_present(const struct pci_device_id *ids) | | 457 | pci_dev_present(const struct pci_device_id *ids) |
462 | { | | 458 | { |
463 | | | 459 | |
464 | /* XXX implement me -- pci_find_device doesn't pass a cookie */ | | 460 | /* XXX implement me -- pci_find_device doesn't pass a cookie */ |
465 | return 0; | | 461 | return 0; |
466 | } | | 462 | } |
467 | | | 463 | |
468 | void | | 464 | void |
469 | pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused) | | 465 | pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused) |
470 | { | | 466 | { |
471 | | | 467 | |
472 | /* XXX Disable the ROM address decoder. */ | | 468 | /* XXX Disable the ROM address decoder. */ |
473 | KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); | | 469 | KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); |
474 | KASSERT(vaddr == pdev->pd_rom_vaddr); | | 470 | KASSERT(vaddr == pdev->pd_rom_vaddr); |
475 | bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size); | | 471 | bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size); |
476 | pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM; | | 472 | pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM; |
477 | pdev->pd_rom_vaddr = NULL; | | 473 | pdev->pd_rom_vaddr = NULL; |
478 | } | | 474 | } |
479 | | | 475 | |
480 | /* XXX Whattakludge! Should move this in sys/arch/. */ | | 476 | /* XXX Whattakludge! Should move this in sys/arch/. */ |
481 | static int | | 477 | static int |
482 | pci_map_rom_md(struct pci_dev *pdev) | | 478 | pci_map_rom_md(struct pci_dev *pdev) |
483 | { | | 479 | { |
484 | #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__) | | 480 | #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__) |
485 | const bus_addr_t rom_base = 0xc0000; | | 481 | const bus_addr_t rom_base = 0xc0000; |
486 | const bus_size_t rom_size = 0x20000; | | 482 | const bus_size_t rom_size = 0x20000; |
487 | bus_space_handle_t rom_bsh; | | 483 | bus_space_handle_t rom_bsh; |
488 | int error; | | 484 | int error; |
489 | | | 485 | |
490 | if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY) | | 486 | if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY) |
491 | return ENXIO; | | 487 | return ENXIO; |
492 | if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA) | | 488 | if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA) |
493 | return ENXIO; | | 489 | return ENXIO; |
494 | /* XXX Check whether this is the primary VGA card? */ | | 490 | /* XXX Check whether this is the primary VGA card? */ |
495 | error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size, | | 491 | error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size, |
496 | (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh); | | 492 | (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh); |
497 | if (error) | | 493 | if (error) |
498 | return ENXIO; | | 494 | return ENXIO; |
499 | | | 495 | |
500 | pdev->pd_rom_bst = pdev->pd_pa.pa_memt; | | 496 | pdev->pd_rom_bst = pdev->pd_pa.pa_memt; |
501 | pdev->pd_rom_bsh = rom_bsh; | | 497 | pdev->pd_rom_bsh = rom_bsh; |
502 | pdev->pd_rom_size = rom_size; | | 498 | pdev->pd_rom_size = rom_size; |
503 | pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; | | 499 | pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; |
504 | | | 500 | |
505 | return 0; | | 501 | return 0; |
506 | #else | | 502 | #else |
507 | return ENXIO; | | 503 | return ENXIO; |
508 | #endif | | 504 | #endif |
509 | } | | 505 | } |
510 | | | 506 | |
511 | void __pci_rom_iomem * | | 507 | void __pci_rom_iomem * |
512 | pci_map_rom(struct pci_dev *pdev, size_t *sizep) | | 508 | pci_map_rom(struct pci_dev *pdev, size_t *sizep) |
513 | { | | 509 | { |
514 | | | 510 | |
515 | KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); | | 511 | KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); |
516 | | | 512 | |
517 | if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM, | | 513 | if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM, |
518 | (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR), | | 514 | (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR), |
519 | &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size) | | 515 | &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size) |
520 | != 0) | | 516 | != 0) |
521 | goto fail_mi; | | 517 | goto fail_mi; |
522 | pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; | | 518 | pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; |
523 | | | 519 | |
524 | /* XXX This type is obviously wrong in general... */ | | 520 | /* XXX This type is obviously wrong in general... */ |
525 | if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, | | 521 | if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, |
526 | pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, | | 522 | pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, |
527 | &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { | | 523 | &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { |
528 | pci_unmap_rom(pdev, NULL); | | 524 | pci_unmap_rom(pdev, NULL); |
529 | goto fail_mi; | | 525 | goto fail_mi; |
530 | } | | 526 | } |
531 | goto success; | | 527 | goto success; |
532 | | | 528 | |
533 | fail_mi: | | 529 | fail_mi: |
534 | if (pci_map_rom_md(pdev) != 0) | | 530 | if (pci_map_rom_md(pdev) != 0) |
535 | goto fail_md; | | 531 | goto fail_md; |
536 | | | 532 | |
537 | /* XXX This type is obviously wrong in general... */ | | 533 | /* XXX This type is obviously wrong in general... */ |
538 | if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, | | 534 | if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, |
539 | pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, | | 535 | pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, |
540 | &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { | | 536 | &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { |
541 | pci_unmap_rom(pdev, NULL); | | 537 | pci_unmap_rom(pdev, NULL); |
542 | goto fail_md; | | 538 | goto fail_md; |
543 | } | | 539 | } |
544 | | | 540 | |
545 | success: | | 541 | success: |
546 | KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX); | | 542 | KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX); |
547 | *sizep = pdev->pd_rom_found_size; | | 543 | *sizep = pdev->pd_rom_found_size; |
548 | pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst, | | 544 | pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst, |
549 | pdev->pd_rom_found_bsh); | | 545 | pdev->pd_rom_found_bsh); |
550 | return pdev->pd_rom_vaddr; | | 546 | return pdev->pd_rom_vaddr; |
551 | | | 547 | |
552 | fail_md: | | 548 | fail_md: |
553 | return NULL; | | 549 | return NULL; |
554 | } | | 550 | } |
555 | | | 551 | |
556 | void __pci_rom_iomem * | | 552 | void __pci_rom_iomem * |
557 | pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep) | | 553 | pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep) |
558 | { | | 554 | { |
559 | | | 555 | |
560 | *sizep = 0; | | 556 | *sizep = 0; |
561 | return NULL; | | 557 | return NULL; |
562 | } | | 558 | } |
563 | | | 559 | |
564 | int | | 560 | int |
565 | pci_enable_rom(struct pci_dev *pdev) | | 561 | pci_enable_rom(struct pci_dev *pdev) |
566 | { | | 562 | { |
567 | const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; | | 563 | const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; |
568 | const pcitag_t tag = pdev->pd_pa.pa_tag; | | 564 | const pcitag_t tag = pdev->pd_pa.pa_tag; |
569 | pcireg_t addr; | | 565 | pcireg_t addr; |
570 | int s; | | 566 | int s; |
571 | | | 567 | |
572 | /* XXX Don't do anything if the ROM isn't there. */ | | 568 | /* XXX Don't do anything if the ROM isn't there. */ |
573 | | | 569 | |
574 | s = splhigh(); | | 570 | s = splhigh(); |
575 | addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); | | 571 | addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); |
576 | addr |= PCI_MAPREG_ROM_ENABLE; | | 572 | addr |= PCI_MAPREG_ROM_ENABLE; |
577 | pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); | | 573 | pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); |
578 | splx(s); | | 574 | splx(s); |
579 | | | 575 | |
580 | return 0; | | 576 | return 0; |
581 | } | | 577 | } |
582 | | | 578 | |
583 | void | | 579 | void |
584 | pci_disable_rom(struct pci_dev *pdev) | | 580 | pci_disable_rom(struct pci_dev *pdev) |
585 | { | | 581 | { |
586 | const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; | | 582 | const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; |
587 | const pcitag_t tag = pdev->pd_pa.pa_tag; | | 583 | const pcitag_t tag = pdev->pd_pa.pa_tag; |
588 | pcireg_t addr; | | 584 | pcireg_t addr; |
589 | int s; | | 585 | int s; |
590 | | | 586 | |
591 | s = splhigh(); | | 587 | s = splhigh(); |
592 | addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); | | 588 | addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); |
593 | addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE; | | 589 | addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE; |
594 | pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); | | 590 | pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); |
595 | splx(s); | | 591 | splx(s); |
596 | } | | 592 | } |
597 | | | 593 | |
598 | bus_addr_t | | 594 | bus_addr_t |
599 | pci_resource_start(struct pci_dev *pdev, unsigned i) | | 595 | pci_resource_start(struct pci_dev *pdev, unsigned i) |
600 | { | | 596 | { |
601 | | | 597 | |
602 | KASSERT(i < PCI_NUM_RESOURCES); | | 598 | KASSERT(i < PCI_NUM_RESOURCES); |
603 | return pdev->pd_resources[i].addr; | | 599 | return pdev->pd_resources[i].addr; |
604 | } | | 600 | } |
605 | | | 601 | |
606 | bus_size_t | | 602 | bus_size_t |
607 | pci_resource_len(struct pci_dev *pdev, unsigned i) | | 603 | pci_resource_len(struct pci_dev *pdev, unsigned i) |
608 | { | | 604 | { |
609 | | | 605 | |
610 | KASSERT(i < PCI_NUM_RESOURCES); | | 606 | KASSERT(i < PCI_NUM_RESOURCES); |
611 | return pdev->pd_resources[i].size; | | 607 | return pdev->pd_resources[i].size; |
612 | } | | 608 | } |
613 | | | 609 | |
614 | bus_addr_t | | 610 | bus_addr_t |
615 | pci_resource_end(struct pci_dev *pdev, unsigned i) | | 611 | pci_resource_end(struct pci_dev *pdev, unsigned i) |
616 | { | | 612 | { |
617 | | | 613 | |
618 | return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1); | | 614 | return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1); |
619 | } | | 615 | } |
620 | | | 616 | |
621 | int | | 617 | int |
622 | pci_resource_flags(struct pci_dev *pdev, unsigned i) | | 618 | pci_resource_flags(struct pci_dev *pdev, unsigned i) |
623 | { | | 619 | { |
624 | | | 620 | |
625 | KASSERT(i < PCI_NUM_RESOURCES); | | 621 | KASSERT(i < PCI_NUM_RESOURCES); |
626 | return pdev->pd_resources[i].flags; | | 622 | return pdev->pd_resources[i].flags; |
627 | } | | 623 | } |
628 | | | 624 | |
629 | void __pci_iomem * | | 625 | void __pci_iomem * |
630 | pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size) | | 626 | pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size) |
631 | { | | 627 | { |
632 | int error; | | 628 | int error; |
633 | | | 629 | |
634 | KASSERT(i < PCI_NUM_RESOURCES); | | 630 | KASSERT(i < PCI_NUM_RESOURCES); |
635 | KASSERT(pdev->pd_resources[i].kva == NULL); | | 631 | KASSERT(pdev->pd_resources[i].kva == NULL); |
636 | | | 632 | |
637 | if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM) | | 633 | if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM) |
638 | return NULL; | | 634 | return NULL; |
639 | if (pdev->pd_resources[i].size < size) | | 635 | if (pdev->pd_resources[i].size < size) |
640 | return NULL; | | 636 | return NULL; |
641 | error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr, | | 637 | error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr, |
642 | size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags, | | 638 | size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags, |
643 | &pdev->pd_resources[i].bsh); | | 639 | &pdev->pd_resources[i].bsh); |
644 | if (error) | | 640 | if (error) |
645 | return NULL; | | 641 | return NULL; |
646 | /* XXX Synchronize with drm_agp_borrow_hook in drm_agpsupport.c. */ | | 642 | /* XXX Synchronize with drm_agp_borrow_hook in drm_agpsupport.c. */ |
647 | pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt; | | 643 | pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt; |
648 | pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst, | | 644 | pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst, |
649 | pdev->pd_resources[i].bsh); | | 645 | pdev->pd_resources[i].bsh); |
650 | pdev->pd_resources[i].mapped = true; | | 646 | pdev->pd_resources[i].mapped = true; |
651 | | | 647 | |
652 | return pdev->pd_resources[i].kva; | | 648 | return pdev->pd_resources[i].kva; |
653 | } | | 649 | } |
654 | | | 650 | |
655 | void | | 651 | void |
656 | pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva) | | 652 | pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva) |
657 | { | | 653 | { |
658 | unsigned i; | | 654 | unsigned i; |
659 | | | 655 | |
660 | CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); | | 656 | CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); |
661 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | | 657 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
662 | if (pdev->pd_resources[i].kva == kva) | | 658 | if (pdev->pd_resources[i].kva == kva) |
663 | break; | | 659 | break; |
664 | } | | 660 | } |
665 | KASSERT(i < PCI_NUM_RESOURCES); | | 661 | KASSERT(i < PCI_NUM_RESOURCES); |
666 | | | 662 | |
667 | pdev->pd_resources[i].kva = NULL; | | 663 | pdev->pd_resources[i].kva = NULL; |
668 | bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh, | | 664 | bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh, |
669 | pdev->pd_resources[i].size); | | 665 | pdev->pd_resources[i].size); |
670 | } | | 666 | } |
671 | | | 667 | |
672 | void | | 668 | void |
673 | pci_save_state(struct pci_dev *pdev) | | 669 | pci_save_state(struct pci_dev *pdev) |
674 | { | | 670 | { |
675 | | | 671 | |
676 | KASSERT(pdev->pd_saved_state == NULL); | | 672 | KASSERT(pdev->pd_saved_state == NULL); |
677 | pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state), | | 673 | pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state), |
678 | KM_SLEEP); | | 674 | KM_SLEEP); |
679 | pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 675 | pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
680 | pdev->pd_saved_state); | | 676 | pdev->pd_saved_state); |
681 | } | | 677 | } |
682 | | | 678 | |
683 | void | | 679 | void |
684 | pci_restore_state(struct pci_dev *pdev) | | 680 | pci_restore_state(struct pci_dev *pdev) |
685 | { | | 681 | { |
686 | | | 682 | |
687 | KASSERT(pdev->pd_saved_state != NULL); | | 683 | KASSERT(pdev->pd_saved_state != NULL); |
688 | pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, | | 684 | pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, |
689 | pdev->pd_saved_state); | | 685 | pdev->pd_saved_state); |
690 | kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state)); | | 686 | kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state)); |
691 | pdev->pd_saved_state = NULL; | | 687 | pdev->pd_saved_state = NULL; |
692 | } | | 688 | } |
693 | | | 689 | |
694 | bool | | 690 | bool |
695 | pci_is_pcie(struct pci_dev *pdev) | | 691 | pci_is_pcie(struct pci_dev *pdev) |
696 | { | | 692 | { |
697 | | | 693 | |
698 | return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0); | | 694 | return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0); |
699 | } | | 695 | } |
700 | | | 696 | |
701 | bool | | 697 | bool |
702 | pci_dma_supported(struct pci_dev *pdev, uintmax_t mask) | | 698 | pci_dma_supported(struct pci_dev *pdev, uintmax_t mask) |
703 | { | | 699 | { |
704 | | | 700 | |
705 | /* XXX Cop-out. */ | | 701 | /* XXX Cop-out. */ |
706 | if (mask > DMA_BIT_MASK(32)) | | 702 | if (mask > DMA_BIT_MASK(32)) |
707 | return pci_dma64_available(&pdev->pd_pa); | | 703 | return pci_dma64_available(&pdev->pd_pa); |
708 | else | | 704 | else |
709 | return true; | | 705 | return true; |
710 | } | | 706 | } |
711 | | | 707 | |
712 | bool | | 708 | bool |
713 | pci_is_thunderbolt_attached(struct pci_dev *pdev) | | 709 | pci_is_thunderbolt_attached(struct pci_dev *pdev) |
714 | { | | 710 | { |
715 | | | 711 | |
716 | /* XXX Cop-out. */ | | 712 | /* XXX Cop-out. */ |
717 | return false; | | 713 | return false; |
718 | } | | 714 | } |
719 | | | 715 | |
720 | bool | | 716 | bool |
721 | pci_is_root_bus(struct pci_bus *bus) | | 717 | pci_is_root_bus(struct pci_bus *bus) |
722 | { | | 718 | { |
723 | | | 719 | |
724 | /* XXX Cop-out. */ | | 720 | /* XXX Cop-out. */ |
725 | return false; | | 721 | return false; |
726 | } | | 722 | } |
727 | | | 723 | |
728 | int | | 724 | int |
729 | pci_domain_nr(struct pci_bus *bus) | | 725 | pci_domain_nr(struct pci_bus *bus) |
730 | { | | 726 | { |
731 | | | 727 | |
732 | return device_unit(bus->pb_dev); | | 728 | return device_unit(bus->pb_dev); |
733 | } | | 729 | } |
734 | | | 730 | |
735 | /* | | 731 | /* |
736 | * We explicitly rename pci_enable/disable_device so that you have to | | 732 | * We explicitly rename pci_enable/disable_device so that you have to |
737 | * review each use of them, since NetBSD's PCI API does _not_ respect | | 733 | * review each use of them, since NetBSD's PCI API does _not_ respect |
738 | * our local enablecnt here, but there are different parts of NetBSD | | 734 | * our local enablecnt here, but there are different parts of NetBSD |
739 | * that automatically enable/disable like PMF, so you have to decide | | 735 | * that automatically enable/disable like PMF, so you have to decide |
740 | * for each one whether to call it or not. | | 736 | * for each one whether to call it or not. |
741 | */ | | 737 | */ |
742 | | | 738 | |
743 | int | | 739 | int |
744 | linux_pci_enable_device(struct pci_dev *pdev) | | 740 | linux_pci_enable_device(struct pci_dev *pdev) |
745 | { | | 741 | { |
746 | const struct pci_attach_args *pa = &pdev->pd_pa; | | 742 | const struct pci_attach_args *pa = &pdev->pd_pa; |
747 | pcireg_t csr; | | 743 | pcireg_t csr; |
748 | int s; | | 744 | int s; |
749 | | | 745 | |
750 | if (pdev->pd_enablecnt++) | | 746 | if (pdev->pd_enablecnt++) |
751 | return 0; | | 747 | return 0; |
752 | | | 748 | |
753 | s = splhigh(); | | 749 | s = splhigh(); |
754 | csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); | | 750 | csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); |
755 | /* If someone else (firmware) already enabled it, credit them. */ | | 751 | /* If someone else (firmware) already enabled it, credit them. */ |
756 | if (csr & (PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE)) | | 752 | if (csr & (PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE)) |
757 | pdev->pd_enablecnt++; | | 753 | pdev->pd_enablecnt++; |
758 | csr |= PCI_COMMAND_IO_ENABLE; | | 754 | csr |= PCI_COMMAND_IO_ENABLE; |
759 | csr |= PCI_COMMAND_MEM_ENABLE; | | 755 | csr |= PCI_COMMAND_MEM_ENABLE; |
760 | pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); | | 756 | pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); |
761 | splx(s); | | 757 | splx(s); |
762 | | | 758 | |
763 | return 0; | | 759 | return 0; |
764 | } | | 760 | } |
765 | | | 761 | |
766 | void | | 762 | void |
767 | linux_pci_disable_device(struct pci_dev *pdev) | | 763 | linux_pci_disable_device(struct pci_dev *pdev) |
768 | { | | 764 | { |
769 | const struct pci_attach_args *pa = &pdev->pd_pa; | | 765 | const struct pci_attach_args *pa = &pdev->pd_pa; |
770 | pcireg_t csr; | | 766 | pcireg_t csr; |
771 | int s; | | 767 | int s; |
772 | | | 768 | |
773 | if (--pdev->pd_enablecnt) | | 769 | if (--pdev->pd_enablecnt) |
774 | return; | | 770 | return; |
775 | | | 771 | |
776 | s = splhigh(); | | 772 | s = splhigh(); |
777 | csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); | | 773 | csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); |
778 | csr &= ~PCI_COMMAND_IO_ENABLE; | | 774 | csr &= ~PCI_COMMAND_IO_ENABLE; |
779 | csr &= ~PCI_COMMAND_MEM_ENABLE; | | 775 | csr &= ~PCI_COMMAND_MEM_ENABLE; |
780 | pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); | | 776 | pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); |
781 | splx(s); | | 777 | splx(s); |
782 | } | | 778 | } |
783 | | | 779 | |
784 | void | | 780 | void |
785 | linux_pci_dev_destroy(struct pci_dev *pdev) | | 781 | linux_pci_dev_destroy(struct pci_dev *pdev) |
786 | { | | 782 | { |
787 | unsigned i; | | 783 | unsigned i; |
788 | | | 784 | |
789 | if (pdev->bus != NULL) { | | 785 | if (pdev->bus != NULL) { |
790 | kmem_free(pdev->bus, sizeof(*pdev->bus)); | | 786 | kmem_free(pdev->bus, sizeof(*pdev->bus)); |
791 | pdev->bus = NULL; | | 787 | pdev->bus = NULL; |
792 | } | | 788 | } |
793 | if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) { | | 789 | if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) { |
794 | pci_unmap_rom(pdev, pdev->pd_rom_vaddr); | | 790 | pci_unmap_rom(pdev, pdev->pd_rom_vaddr); |
795 | pdev->pd_rom_vaddr = 0; | | 791 | pdev->pd_rom_vaddr = 0; |
796 | } | | 792 | } |
797 | for (i = 0; i < __arraycount(pdev->pd_resources); i++) { | | 793 | for (i = 0; i < __arraycount(pdev->pd_resources); i++) { |
798 | if (!pdev->pd_resources[i].mapped) | | 794 | if (!pdev->pd_resources[i].mapped) |
799 | continue; | | 795 | continue; |
800 | bus_space_unmap(pdev->pd_resources[i].bst, | | 796 | bus_space_unmap(pdev->pd_resources[i].bst, |
801 | pdev->pd_resources[i].bsh, pdev->pd_resources[i].size); | | 797 | pdev->pd_resources[i].bsh, pdev->pd_resources[i].size); |
802 | } | | 798 | } |
803 | | | 799 | |
804 | /* There is no way these should be still in use. */ | | 800 | /* There is no way these should be still in use. */ |
805 | KASSERT(pdev->pd_saved_state == NULL); | | 801 | KASSERT(pdev->pd_saved_state == NULL); |
806 | KASSERT(pdev->pd_intr_handles == NULL); | | 802 | KASSERT(pdev->pd_intr_handles == NULL); |
807 | } | | 803 | } |