1 /*	$NetBSD: acpivar.h,v 1.74 2016/06/21 11:33:33 nonaka Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef _SYS_DEV_ACPI_ACPIVAR_H
39 #define _SYS_DEV_ACPI_ACPIVAR_H
40 
41 /*
42  * This file defines the ACPI interface provided to the rest of the
43  * kernel, as well as the autoconfiguration structures for ACPI
44  * support.
45  */
46 
47 #include <sys/bus.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/isa/isavar.h>
50 
51 #include <dev/acpi/acpica.h>
52 #include <dev/acpi/acpi_util.h>
53 
54 #include <dev/sysmon/sysmonvar.h>
55 
56 /*
57  * This structure is used to attach the ACPI "bus".
58  */
59 struct acpibus_attach_args {
60 	bus_space_tag_t		 aa_iot;	/* PCI I/O space tag */
61 	bus_space_tag_t		 aa_memt;	/* PCI MEM space tag */
62 	pci_chipset_tag_t	 aa_pc;		/* PCI chipset */
63 	int			 aa_pciflags;	/* PCI bus flags */
64 	isa_chipset_tag_t	 aa_ic;		/* ISA chipset */
65 	bus_dma_tag_t		 aa_dmat;	/* PCI DMA tag */
66 	bus_dma_tag_t		 aa_dmat64;	/* PCI 64bit DMA tag */
67 };
68 
69 /*
70  * PCI information for ACPI device nodes that correspond to PCI devices.
71  *
72  * Remarks:
73  *
74  *	ap_bus		<= 255
75  *	ap_device	<= 31
76  *	ap_function	<= 7		or	ap_function == 0xFFFF
77  *	ap_downbus	<= 255
78  *
79  * Validity of some fields depends on the value of ap_flags:
80  *
81  *	ap_segment				always valid
82  *	ap_bus, ap_device, ap_function		valid for PCI devices
83  *	ap_downbus				valid for PCI bridges
84  *
85  * The device and function numbers are encoded in the value returned by
86  * _ADR.  A function number of 0xFFFF is used to refer to all the
87  * functions on a PCI device (ACPI 4.0a, p. 200).
88  */
89 struct acpi_pci_info {
90 	uint16_t		 ap_flags;	/* Flags (cf. below) */
91 	uint16_t		 ap_segment;	/* PCI segment group */
92 	uint16_t		 ap_bus;	/* PCI bus */
93 	uint16_t		 ap_device;	/* PCI device */
94 	uint16_t		 ap_function;	/* PCI function */
95 	uint16_t		 ap_downbus;	/* PCI bridge downstream bus */
96 };
97 
98 /*
99  * Flags for PCI information.
100  */
101 #define ACPI_PCI_INFO_DEVICE	__BIT(0)	/* PCI device */
102 #define ACPI_PCI_INFO_BRIDGE	__BIT(1)	/* PCI bridge */
103 
104 /*
105  * An ACPI device node.
106  *
107  * Remarks:
108  *
109  *	ad_device	NULL if no device has attached to the node
110  *	ad_root		never NULL
111  *	ad_parent	only NULL if root of the tree ("\")
112  *	ad_pciinfo	NULL if not a PCI device
113  *	ad_wakedev	NULL if no wakeup capabilities
114  *	ad_notify	NULL if there is no notify handler
115  *	ad_devinfo	never NULL
116  *	ad_handle	never NULL
117  *
118  * Each ACPI device node is associated with its handle. The function
119  * acpi_match_node() can be used to get the node structure from a handle.
120  */
121 struct acpi_devnode {
122 	device_t		 ad_device;	/* Device */
123 	device_t		 ad_root;	/* Backpointer to acpi_softc */
124 	struct acpi_devnode	*ad_parent;	/* Backpointer to parent */
125 	struct acpi_pci_info	*ad_pciinfo;	/* PCI info */
126 	struct acpi_wakedev	*ad_wakedev;	/* Device wake */
127 	ACPI_NOTIFY_HANDLER	 ad_notify;	/* Device notify */
128 	ACPI_DEVICE_INFO	*ad_devinfo;	/* Device info */
129 	ACPI_HANDLE		 ad_handle;	/* Device handle */
130 	char			 ad_name[5];	/* Device name */
131 	uint32_t		 ad_flags;	/* Device flags */
132 	uint32_t		 ad_type;	/* Device type */
133 	int			 ad_state;	/* Device power state */
134 
135 	SIMPLEQ_ENTRY(acpi_devnode)	ad_list;
136 	SIMPLEQ_ENTRY(acpi_devnode)	ad_child_list;
137 	SIMPLEQ_HEAD(, acpi_devnode)	ad_child_head;
138 };
139 
140 /*
141  * ACPI driver capabilities (ad_flags).
142  */
143 #define ACPI_DEVICE_POWER	__BIT(0)	/* Support for D-states  */
144 #define ACPI_DEVICE_WAKEUP	__BIT(1)	/* Support for wake-up */
145 #define ACPI_DEVICE_EJECT	__BIT(2)	/* Support for "ejection" */
146 #define ACPI_DEVICE_DOCK	__BIT(3)	/* Support for docking */
147 
148 /*
149  * Software state of the ACPI subsystem.
150  */
151 struct acpi_softc {
152 	device_t		 sc_dev;	/* base device info */
153 	device_t		 sc_apmbus;	/* APM pseudo-bus */
154 	device_t		 sc_hpet;	/* hpet(4) pseudo-bus */
155 	device_t		 sc_wdrt;	/* acpiwdrt(4) pseudo-bus */
156 
157 	struct acpi_devnode	*sc_root;	/* root of the device tree */
158 
159 	bus_space_tag_t		 sc_iot;	/* PCI I/O space tag */
160 	bus_space_tag_t		 sc_memt;	/* PCI MEM space tag */
161 	pci_chipset_tag_t	 sc_pc;		/* PCI chipset tag */
162 	int			 sc_pciflags;	/* PCI bus flags */
163 	int			 sc_pci_bus;	/* internal PCI fixup */
164 	isa_chipset_tag_t	 sc_ic;		/* ISA chipset tag */
165 	bus_dma_tag_t		 sc_dmat;	/* PCI DMA tag */
166 	bus_dma_tag_t		 sc_dmat64;	/* PCI 64bit DMA tag */
167 
168 	void			*sc_sdhook;	/* shutdown hook */
169 
170 	int			 sc_quirks;
171 	int			 sc_sleepstate;
172 	int			 sc_sleepstates;
173 
174 	struct sysmon_pswitch	 sc_smpsw_power;
175 	struct sysmon_pswitch	 sc_smpsw_sleep;
176 
177 	SIMPLEQ_HEAD(, acpi_devnode)	ad_head;
178 };
179 
180 /*
181  * acpi_attach_args:
182  *
183  *	Used to attach a device instance to the acpi "bus".
184  */
185 struct acpi_attach_args {
186 	struct acpi_devnode *aa_node;	/* ACPI device node */
187 	bus_space_tag_t aa_iot;		/* PCI I/O space tag */
188 	bus_space_tag_t aa_memt;	/* PCI MEM space tag */
189 	pci_chipset_tag_t aa_pc;	/* PCI chipset tag */
190 	int aa_pciflags;		/* PCI bus flags */
191 	isa_chipset_tag_t aa_ic;	/* ISA chipset */
192 	bus_dma_tag_t aa_dmat;		/* PCI DMA tag */
193 	bus_dma_tag_t aa_dmat64;	/* PCI 64bit DMA tag */
194 };
195 
196 /*
197  * ACPI resources:
198  *
199  *	acpi_io		I/O ports
200  *	acpi_iorange	I/O port range
201  *	acpi_mem	memory region
202  *	acpi_memrange	memory range
203  *	acpi_irq	Interrupt Request
204  *	acpi_drq	DMA request
205  */
206 struct acpi_io {
207 	SIMPLEQ_ENTRY(acpi_io) ar_list;
208 	int		ar_index;
209 	uint32_t	ar_base;
210 	uint32_t	ar_length;
211 };
212 
213 struct acpi_iorange {
214 	SIMPLEQ_ENTRY(acpi_iorange) ar_list;
215 	int		ar_index;
216 	uint32_t	ar_low;
217 	uint32_t	ar_high;
218 	uint32_t	ar_length;
219 	uint32_t	ar_align;
220 };
221 
222 struct acpi_mem {
223 	SIMPLEQ_ENTRY(acpi_mem) ar_list;
224 	int		ar_index;
225 	uint32_t	ar_base;
226 	uint32_t	ar_length;
227 };
228 
229 struct acpi_memrange {
230 	SIMPLEQ_ENTRY(acpi_memrange) ar_list;
231 	int		ar_index;
232 	uint32_t	ar_low;
233 	uint32_t	ar_high;
234 	uint32_t	ar_length;
235 	uint32_t	ar_align;
236 };
237 
238 struct acpi_irq {
239 	SIMPLEQ_ENTRY(acpi_irq) ar_list;
240 	int		ar_index;
241 	uint32_t	ar_irq;
242 	uint32_t	ar_type;
243 };
244 
245 struct acpi_drq {
246 	SIMPLEQ_ENTRY(acpi_drq) ar_list;
247 	int		ar_index;
248 	uint32_t	ar_drq;
249 };
250 
251 struct acpi_resources {
252 	SIMPLEQ_HEAD(, acpi_io) ar_io;
253 	int ar_nio;
254 
255 	SIMPLEQ_HEAD(, acpi_iorange) ar_iorange;
256 	int ar_niorange;
257 
258 	SIMPLEQ_HEAD(, acpi_mem) ar_mem;
259 	int ar_nmem;
260 
261 	SIMPLEQ_HEAD(, acpi_memrange) ar_memrange;
262 	int ar_nmemrange;
263 
264 	SIMPLEQ_HEAD(, acpi_irq) ar_irq;
265 	int ar_nirq;
266 
267 	SIMPLEQ_HEAD(, acpi_drq) ar_drq;
268 	int ar_ndrq;
269 };
270 
271 /*
272  * acpi_resource_parse_ops:
273  *
274  *	The client of ACPI resources specifies these operations
275  *	when the resources are parsed.
276  */
277 struct acpi_resource_parse_ops {
278 	void	(*init)(device_t, void *, void **);
279 	void	(*fini)(device_t, void *);
280 
281 	void	(*ioport)(device_t, void *, uint32_t, uint32_t);
282 	void	(*iorange)(device_t, void *, uint32_t, uint32_t,
283 		    uint32_t, uint32_t);
284 
285 	void	(*memory)(device_t, void *, uint32_t, uint32_t);
286 	void	(*memrange)(device_t, void *, uint32_t, uint32_t,
287 		    uint32_t, uint32_t);
288 
289 	void	(*irq)(device_t, void *, uint32_t, uint32_t);
290 	void	(*drq)(device_t, void *, uint32_t);
291 
292 	void	(*start_dep)(device_t, void *, int);
293 	void	(*end_dep)(device_t, void *);
294 };
295 
296 extern struct acpi_softc *acpi_softc;
297 extern int acpi_active;
298 
299 extern const struct acpi_resource_parse_ops acpi_resource_parse_ops_default;
300 extern const struct acpi_resource_parse_ops acpi_resource_parse_ops_quiet;
301 
302 int		acpi_probe(void);
303 void		acpi_disable(void);
304 int		acpi_check(device_t, const char *);
305 
306 int		acpi_reset(void);
307 
308 ACPI_PHYSICAL_ADDRESS	acpi_OsGetRootPointer(void);
309 
310 bool		acpi_register_notify(struct acpi_devnode *,
311 				     ACPI_NOTIFY_HANDLER);
312 void		acpi_deregister_notify(struct acpi_devnode *);
313 
314 ACPI_STATUS	acpi_resource_parse(device_t, ACPI_HANDLE, const char *,
315 		    void *, const struct acpi_resource_parse_ops *);
316 void		acpi_resource_print(device_t, struct acpi_resources *);
317 void		acpi_resource_cleanup(struct acpi_resources *);
318 
319 void *		acpi_pci_link_devbyhandle(ACPI_HANDLE);
320 void		acpi_pci_link_add_reference(void *, int, int, int, int);
321 int		acpi_pci_link_route_interrupt(void *, int, int *, int *, int *);
322 char *		acpi_pci_link_name(void *);
323 ACPI_HANDLE	acpi_pci_link_handle(void *);
324 void		acpi_pci_link_state(void);
325 void		acpi_pci_link_resume(void);
326 
327 struct acpi_io		*acpi_res_io(struct acpi_resources *, int);
328 struct acpi_iorange	*acpi_res_iorange(struct acpi_resources *, int);
329 struct acpi_mem		*acpi_res_mem(struct acpi_resources *, int);
330 struct acpi_memrange	*acpi_res_memrange(struct acpi_resources *, int);
331 struct acpi_irq		*acpi_res_irq(struct acpi_resources *, int);
332 struct acpi_drq		*acpi_res_drq(struct acpi_resources *, int);
333 
334 /*
335  * Sleep state transition.
336  */
337 void			acpi_enter_sleep_state(int);
338 
339 /*
340  * MADT.
341  */
342 #define ACPI_PLATFORM_INT_PMI	1
343 #define ACPI_PLATFORM_INT_INIT	2
344 #define ACPI_PLATFORM_INT_CERR	3
345 
346 ACPI_STATUS		acpi_madt_map(void);
347 void			acpi_madt_unmap(void);
348 void			acpi_madt_walk(ACPI_STATUS (*)(ACPI_SUBTABLE_HEADER *,
349 				       void *), void *);
350 
351 /*
352  * Quirk handling.
353  */
354 struct acpi_quirk {
355 	const char	*aq_tabletype;		/* Type of table */
356 	const char	*aq_oemid;		/* "OemId" field */
357 	int		 aq_oemrev;		/* "OemRev" field */
358 	int		 aq_cmpop;		/* "OemRev" comparison */
359 	const char	*aq_tabid;		/* "TableId */
360 	int		 aq_quirks;		/* The actual quirk */
361 };
362 
363 #define ACPI_QUIRK_BROKEN	0x00000001	/* totally broken */
364 #define ACPI_QUIRK_BADPCI	0x00000002	/* bad PCI hierarchy */
365 #define ACPI_QUIRK_BADBBN	0x00000004	/* _BBN broken */
366 #define ACPI_QUIRK_IRQ0		0x00000008	/* bad 0->2 irq override */
367 #define ACPI_QUIRK_OLDBIOS	0x00000010	/* BIOS date blacklisted */
368 
369 int	acpi_find_quirks(void);
370 int	acpi_quirks_osi_add(const char *);
371 int	acpi_quirks_osi_del(const char *);
372 
373 #ifdef ACPI_DEBUG
374 void	acpi_debug_init(void);
375 #endif
376 
377 /*
378  * Misc routines with vectors updated by acpiverbose module.
379  */
380 extern void	(*acpi_print_verbose)(struct acpi_softc *);
381 extern void	(*acpi_print_dev)(const char *);
382 
383 void		acpi_load_verbose(void);
384 extern int	acpi_verbose_loaded;
385 
386 #endif	/* !_SYS_DEV_ACPI_ACPIVAR_H */
387