OsdHardware.c revision 1.6 1 1.6 gsutre /* $NetBSD: OsdHardware.c,v 1.6 2010/07/10 21:31:00 gsutre Exp $ */
2 1.1 kochi
3 1.1 kochi /*
4 1.1 kochi * Copyright 2001 Wasabi Systems, Inc.
5 1.1 kochi * All rights reserved.
6 1.1 kochi *
7 1.1 kochi * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 kochi *
9 1.1 kochi * Redistribution and use in source and binary forms, with or without
10 1.1 kochi * modification, are permitted provided that the following conditions
11 1.1 kochi * are met:
12 1.1 kochi * 1. Redistributions of source code must retain the above copyright
13 1.1 kochi * notice, this list of conditions and the following disclaimer.
14 1.1 kochi * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 kochi * notice, this list of conditions and the following disclaimer in the
16 1.1 kochi * documentation and/or other materials provided with the distribution.
17 1.1 kochi * 3. All advertising materials mentioning features or use of this software
18 1.1 kochi * must display the following acknowledgement:
19 1.1 kochi * This product includes software developed for the NetBSD Project by
20 1.1 kochi * Wasabi Systems, Inc.
21 1.1 kochi * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 kochi * or promote products derived from this software without specific prior
23 1.1 kochi * written permission.
24 1.1 kochi *
25 1.1 kochi * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 kochi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 kochi * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 kochi * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 kochi * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 kochi * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 kochi * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 kochi * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 kochi * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 kochi * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 kochi * POSSIBILITY OF SUCH DAMAGE.
36 1.1 kochi */
37 1.1 kochi
38 1.1 kochi /*
39 1.1 kochi * OS Services Layer
40 1.1 kochi *
41 1.1 kochi * 6.7: Address Space Access: Port Input/Output
42 1.1 kochi * 6.8: Address Space Access: Memory and Memory Mapped I/O
43 1.1 kochi * 6.9: Address Space Access: PCI Configuration Space
44 1.1 kochi */
45 1.1 kochi
46 1.1 kochi #include <sys/cdefs.h>
47 1.6 gsutre __KERNEL_RCSID(0, "$NetBSD: OsdHardware.c,v 1.6 2010/07/10 21:31:00 gsutre Exp $");
48 1.1 kochi
49 1.1 kochi #include <sys/param.h>
50 1.1 kochi #include <sys/device.h>
51 1.1 kochi
52 1.1 kochi #include <dev/acpi/acpica.h>
53 1.1 kochi #include <dev/acpi/acpivar.h>
54 1.6 gsutre #include <dev/acpi/acpi_pci.h>
55 1.1 kochi
56 1.1 kochi #include <machine/acpi_machdep.h>
57 1.1 kochi
58 1.1 kochi /*
59 1.1 kochi * ACPICA doesn't provide much in the way of letting us know which
60 1.1 kochi * hardware resources it wants to use. We therefore have to resort
61 1.1 kochi * to calling machinde-dependent code to do the access for us.
62 1.1 kochi */
63 1.1 kochi
64 1.1 kochi /*
65 1.1 kochi * AcpiOsReadPort:
66 1.1 kochi *
67 1.1 kochi * Read a value from an input port.
68 1.1 kochi */
69 1.1 kochi ACPI_STATUS
70 1.1 kochi AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
71 1.1 kochi {
72 1.1 kochi
73 1.1 kochi switch (Width) {
74 1.1 kochi case 8:
75 1.1 kochi *Value = acpi_md_OsIn8(Address);
76 1.1 kochi break;
77 1.1 kochi
78 1.1 kochi case 16:
79 1.1 kochi *Value = acpi_md_OsIn16(Address);
80 1.1 kochi break;
81 1.1 kochi
82 1.1 kochi case 32:
83 1.1 kochi *Value = acpi_md_OsIn32(Address);
84 1.1 kochi break;
85 1.1 kochi
86 1.1 kochi default:
87 1.1 kochi return AE_BAD_PARAMETER;
88 1.1 kochi }
89 1.1 kochi
90 1.1 kochi return AE_OK;
91 1.1 kochi }
92 1.1 kochi
93 1.1 kochi /*
94 1.1 kochi * AcpiOsWritePort:
95 1.1 kochi *
96 1.1 kochi * Write a value to an output port.
97 1.1 kochi */
98 1.1 kochi ACPI_STATUS
99 1.1 kochi AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
100 1.1 kochi {
101 1.1 kochi
102 1.1 kochi switch (Width) {
103 1.1 kochi case 8:
104 1.1 kochi acpi_md_OsOut8(Address, Value);
105 1.1 kochi break;
106 1.1 kochi
107 1.1 kochi case 16:
108 1.1 kochi acpi_md_OsOut16(Address, Value);
109 1.1 kochi break;
110 1.1 kochi
111 1.1 kochi case 32:
112 1.1 kochi acpi_md_OsOut32(Address, Value);
113 1.1 kochi break;
114 1.1 kochi
115 1.1 kochi default:
116 1.1 kochi return AE_BAD_PARAMETER;
117 1.1 kochi }
118 1.1 kochi
119 1.1 kochi return AE_OK;
120 1.1 kochi }
121 1.1 kochi
122 1.1 kochi /*
123 1.1 kochi * AcpiOsReadMemory:
124 1.1 kochi *
125 1.1 kochi * Read a value from a memory location.
126 1.1 kochi */
127 1.1 kochi ACPI_STATUS
128 1.1 kochi AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, UINT32 Width)
129 1.1 kochi {
130 1.1 kochi void *LogicalAddress;
131 1.5 drochner ACPI_STATUS rv = AE_OK;
132 1.1 kochi
133 1.3 jmcneill LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
134 1.3 jmcneill if (LogicalAddress == NULL)
135 1.3 jmcneill return AE_NOT_EXIST;
136 1.1 kochi
137 1.1 kochi switch (Width) {
138 1.1 kochi case 8:
139 1.1 kochi *Value = *(volatile uint8_t *) LogicalAddress;
140 1.1 kochi break;
141 1.1 kochi
142 1.1 kochi case 16:
143 1.1 kochi *Value = *(volatile uint16_t *) LogicalAddress;
144 1.1 kochi break;
145 1.1 kochi
146 1.1 kochi case 32:
147 1.1 kochi *Value = *(volatile uint32_t *) LogicalAddress;
148 1.1 kochi break;
149 1.1 kochi
150 1.1 kochi default:
151 1.1 kochi rv = AE_BAD_PARAMETER;
152 1.1 kochi }
153 1.1 kochi
154 1.1 kochi AcpiOsUnmapMemory(LogicalAddress, Width / 8);
155 1.1 kochi
156 1.1 kochi return rv;
157 1.1 kochi }
158 1.1 kochi
159 1.1 kochi /*
160 1.1 kochi * AcpiOsWriteMemory:
161 1.1 kochi *
162 1.1 kochi * Write a value to a memory location.
163 1.1 kochi */
164 1.1 kochi ACPI_STATUS
165 1.1 kochi AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT32 Value, UINT32 Width)
166 1.1 kochi {
167 1.1 kochi void *LogicalAddress;
168 1.5 drochner ACPI_STATUS rv = AE_OK;
169 1.1 kochi
170 1.3 jmcneill LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
171 1.3 jmcneill if (LogicalAddress == NULL)
172 1.3 jmcneill return AE_NOT_FOUND;
173 1.1 kochi
174 1.1 kochi switch (Width) {
175 1.1 kochi case 8:
176 1.1 kochi *(volatile uint8_t *) LogicalAddress = Value;
177 1.1 kochi break;
178 1.1 kochi
179 1.1 kochi case 16:
180 1.1 kochi *(volatile uint16_t *) LogicalAddress = Value;
181 1.1 kochi break;
182 1.1 kochi
183 1.1 kochi case 32:
184 1.1 kochi *(volatile uint32_t *) LogicalAddress = Value;
185 1.1 kochi break;
186 1.1 kochi
187 1.1 kochi default:
188 1.1 kochi rv = AE_BAD_PARAMETER;
189 1.1 kochi }
190 1.1 kochi
191 1.1 kochi AcpiOsUnmapMemory(LogicalAddress, Width / 8);
192 1.1 kochi
193 1.1 kochi return rv;
194 1.1 kochi }
195 1.1 kochi
196 1.1 kochi /*
197 1.1 kochi * AcpiOsReadPciConfiguration:
198 1.1 kochi *
199 1.1 kochi * Read a value from a PCI configuration register.
200 1.1 kochi */
201 1.1 kochi ACPI_STATUS
202 1.1 kochi AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value,
203 1.1 kochi UINT32 Width)
204 1.1 kochi {
205 1.1 kochi pcitag_t tag;
206 1.1 kochi pcireg_t tmp;
207 1.1 kochi
208 1.1 kochi /* XXX Need to deal with "segment" ("hose" in Alpha terminology). */
209 1.1 kochi
210 1.4 jmcneill if (PciId->Bus >= 256 || PciId->Device >= 32 || PciId->Function >= 8)
211 1.4 jmcneill return AE_BAD_PARAMETER;
212 1.4 jmcneill
213 1.1 kochi tag = pci_make_tag(acpi_softc->sc_pc, PciId->Bus, PciId->Device,
214 1.1 kochi PciId->Function);
215 1.1 kochi tmp = pci_conf_read(acpi_softc->sc_pc, tag, Register & ~3);
216 1.1 kochi
217 1.1 kochi switch (Width) {
218 1.1 kochi case 8:
219 1.1 kochi *(uint8_t *) Value = (tmp >> ((Register & 3) * 8)) & 0xff;
220 1.1 kochi break;
221 1.1 kochi
222 1.1 kochi case 16:
223 1.1 kochi *(uint16_t *) Value = (tmp >> ((Register & 3) * 8)) & 0xffff;
224 1.1 kochi break;
225 1.1 kochi
226 1.1 kochi case 32:
227 1.1 kochi *(uint32_t *) Value = tmp;
228 1.1 kochi break;
229 1.1 kochi
230 1.1 kochi default:
231 1.1 kochi return AE_BAD_PARAMETER;
232 1.1 kochi }
233 1.1 kochi
234 1.1 kochi return AE_OK;
235 1.1 kochi }
236 1.1 kochi
237 1.1 kochi /*
238 1.1 kochi * AcpiOsWritePciConfiguration:
239 1.1 kochi *
240 1.1 kochi * Write a value to a PCI configuration register.
241 1.1 kochi */
242 1.1 kochi ACPI_STATUS
243 1.1 kochi AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register,
244 1.1 kochi ACPI_INTEGER Value, UINT32 Width)
245 1.1 kochi {
246 1.1 kochi pcitag_t tag;
247 1.1 kochi pcireg_t tmp;
248 1.1 kochi
249 1.1 kochi /* XXX Need to deal with "segment" ("hose" in Alpha terminology). */
250 1.1 kochi
251 1.1 kochi tag = pci_make_tag(acpi_softc->sc_pc, PciId->Bus, PciId->Device,
252 1.1 kochi PciId->Function);
253 1.1 kochi
254 1.1 kochi switch (Width) {
255 1.1 kochi case 8:
256 1.1 kochi tmp = pci_conf_read(acpi_softc->sc_pc, tag, Register & ~3);
257 1.1 kochi tmp &= ~(0xff << ((Register & 3) * 8));
258 1.1 kochi tmp |= (Value << ((Register & 3) * 8));
259 1.1 kochi break;
260 1.1 kochi
261 1.1 kochi case 16:
262 1.1 kochi tmp = pci_conf_read(acpi_softc->sc_pc, tag, Register & ~3);
263 1.1 kochi tmp &= ~(0xffff << ((Register & 3) * 8));
264 1.1 kochi tmp |= (Value << ((Register & 3) * 8));
265 1.1 kochi break;
266 1.1 kochi
267 1.1 kochi case 32:
268 1.1 kochi tmp = Value;
269 1.1 kochi break;
270 1.1 kochi
271 1.1 kochi default:
272 1.1 kochi return AE_BAD_PARAMETER;
273 1.1 kochi }
274 1.1 kochi
275 1.1 kochi pci_conf_write(acpi_softc->sc_pc, tag, Register & ~3, tmp);
276 1.1 kochi
277 1.1 kochi return AE_OK;
278 1.1 kochi }
279 1.1 kochi
280 1.6 gsutre /*
281 1.6 gsutre * acpi_os_derive_pciid_rec:
282 1.6 gsutre *
283 1.6 gsutre * Helper function for AcpiOsDerivePciId. The parameters are:
284 1.6 gsutre * - chandle: a handle to the node whose PCI id shall be derived.
285 1.6 gsutre * - rhandle: a handle the PCI root bridge upstream of chandle.
286 1.6 gsutre * - pciid: where the derived PCI id is returned.
287 1.6 gsutre *
288 1.6 gsutre * This function assumes that rhandle is a proper ancestor of chandle,
289 1.6 gsutre * and that pciid has already been filled by ACPICA:
290 1.6 gsutre * - segment# and bus# obtained from _SEG and _BBN on rhandle,
291 1.6 gsutre * - device# and function# obtained from _ADR on the ACPI device node
292 1.6 gsutre * whose scope chandle is in).
293 1.6 gsutre */
294 1.6 gsutre static ACPI_STATUS
295 1.6 gsutre acpi_os_derive_pciid_rec(ACPI_HANDLE chandle, ACPI_HANDLE rhandle, ACPI_PCI_ID *pciid)
296 1.1 kochi {
297 1.6 gsutre ACPI_HANDLE phandle;
298 1.6 gsutre ACPI_INTEGER address;
299 1.6 gsutre ACPI_OBJECT_TYPE objtype;
300 1.1 kochi ACPI_STATUS rv;
301 1.6 gsutre uint16_t valb;
302 1.1 kochi
303 1.6 gsutre KASSERT(chandle != rhandle);
304 1.1 kochi
305 1.6 gsutre /*
306 1.6 gsutre * Get parent device node. This is should not fail since chandle has
307 1.6 gsutre * at least one ancestor that is a device node: rhandle.
308 1.6 gsutre */
309 1.6 gsutre phandle = chandle;
310 1.6 gsutre do {
311 1.6 gsutre rv = AcpiGetParent(phandle, &phandle);
312 1.6 gsutre if (ACPI_FAILURE(rv))
313 1.6 gsutre return rv;
314 1.6 gsutre rv = AcpiGetType(phandle, &objtype);
315 1.6 gsutre if (ACPI_FAILURE(rv))
316 1.6 gsutre return rv;
317 1.6 gsutre }
318 1.6 gsutre while (objtype != ACPI_TYPE_DEVICE);
319 1.1 kochi
320 1.1 kochi /*
321 1.6 gsutre * If the parent is rhandle then we have nothing to do since ACPICA
322 1.6 gsutre * has pre-filled the PCI id to the best it could.
323 1.1 kochi */
324 1.6 gsutre if (phandle == rhandle)
325 1.6 gsutre return AE_OK;
326 1.1 kochi
327 1.6 gsutre /* Recursive call to get PCI id of the parent */
328 1.6 gsutre rv = acpi_os_derive_pciid_rec(phandle, rhandle, pciid);
329 1.6 gsutre if (ACPI_FAILURE(rv))
330 1.6 gsutre return rv;
331 1.1 kochi
332 1.6 gsutre /*
333 1.6 gsutre * If this is not an ACPI device, return the PCI id of its parent.
334 1.6 gsutre */
335 1.6 gsutre rv = AcpiGetType(chandle, &objtype);
336 1.6 gsutre if (ACPI_FAILURE(rv))
337 1.6 gsutre return rv;
338 1.6 gsutre if (objtype != ACPI_TYPE_DEVICE)
339 1.6 gsutre return AE_OK;
340 1.1 kochi
341 1.6 gsutre /*
342 1.6 gsutre * This is an ACPI device node. Its parent device node is not a PCI
343 1.6 gsutre * root bridge. Check that it is a PCI-to-PCI bridge and get its
344 1.6 gsutre * secondary bus#.
345 1.6 gsutre */
346 1.6 gsutre rv = acpi_pcidev_ppb_downbus(pciid->Segment, pciid->Bus, pciid->Device,
347 1.6 gsutre pciid->Function, &valb);
348 1.6 gsutre if (ACPI_FAILURE(rv))
349 1.6 gsutre return rv;
350 1.1 kochi
351 1.6 gsutre /* Get address (contains dev# and fun# for PCI devices). */
352 1.6 gsutre rv = acpi_eval_integer(chandle, METHOD_NAME__ADR, &address);
353 1.6 gsutre if (ACPI_FAILURE(rv))
354 1.6 gsutre return rv;
355 1.1 kochi
356 1.6 gsutre pciid->Bus = valb;
357 1.6 gsutre pciid->Device = ACPI_HIWORD(ACPI_LODWORD(address));
358 1.6 gsutre pciid->Function = ACPI_LOWORD(ACPI_LODWORD(address));
359 1.6 gsutre return AE_OK;
360 1.1 kochi }
361 1.1 kochi
362 1.1 kochi /*
363 1.1 kochi * AcpiOsDerivePciId:
364 1.1 kochi *
365 1.6 gsutre * Derive correct PCI bus# by traversing bridges.
366 1.6 gsutre *
367 1.6 gsutre * In ACPICA release 20100331 (as well as older versions), the interface
368 1.6 gsutre * of this function is not correctly documented in the ACPICA programmer
369 1.6 gsutre * reference. The correct interface parameters to this function are:
370 1.6 gsutre * - rhandle: a handle the PCI root bridge upstream of handle.
371 1.6 gsutre * - chandle: a handle to the PCI_Config operation region.
372 1.6 gsutre * - PciId: where the derived PCI id is returned.
373 1.1 kochi */
374 1.1 kochi void
375 1.1 kochi AcpiOsDerivePciId(
376 1.1 kochi ACPI_HANDLE rhandle,
377 1.1 kochi ACPI_HANDLE chandle,
378 1.1 kochi ACPI_PCI_ID **PciId)
379 1.1 kochi {
380 1.6 gsutre ACPI_PCI_ID pciid;
381 1.6 gsutre ACPI_STATUS rv;
382 1.6 gsutre
383 1.6 gsutre if (chandle == rhandle)
384 1.6 gsutre return;
385 1.6 gsutre
386 1.6 gsutre pciid = **PciId;
387 1.6 gsutre rv = acpi_os_derive_pciid_rec(chandle, rhandle, &pciid);
388 1.6 gsutre if (ACPI_FAILURE(rv))
389 1.6 gsutre return;
390 1.6 gsutre
391 1.6 gsutre (*PciId)->Bus = pciid.Bus;
392 1.1 kochi }
393