xref: /openbsd/sys/dev/acpi/acpivar.h (revision 81e1d337)
1 /*	$OpenBSD: acpivar.h,v 1.134 2024/08/08 07:02:38 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2005 Thorsten Lockert <tholo@sigmasoft.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _DEV_ACPI_ACPIVAR_H_
19 #define _DEV_ACPI_ACPIVAR_H_
20 
21 #define ACPI_TRAMPOLINE		(19 * NBPG)
22 #define ACPI_TRAMP_DATA		(20 * NBPG)
23 
24 #ifndef _ACPI_WAKECODE
25 
26 #include <sys/event.h>
27 #include <sys/timeout.h>
28 #include <sys/rwlock.h>
29 
30 #include <machine/bus.h>
31 
32 #include <dev/pci/pcivar.h>
33 
34 #include "acpipwrres.h"
35 
36 /* #define ACPI_DEBUG */
37 #ifdef ACPI_DEBUG
38 extern int acpi_debug;
39 #define dprintf(x...)	  do { if (acpi_debug) printf(x); } while (0)
40 #define dnprintf(n,x...)  do { if (acpi_debug > (n)) printf(x); } while (0)
41 #else
42 #define dprintf(x...)
43 #define dnprintf(n,x...)
44 #endif
45 
46 extern int acpi_hasprocfvs;
47 extern int acpi_haspci;
48 
49 struct acpiec_softc;
50 struct acpipwrres_softc;
51 
52 struct acpivideo_softc {
53 	struct device sc_dev;
54 
55 	struct acpi_softc *sc_acpi;
56 	struct aml_node	*sc_devnode;
57 };
58 
59 #define ACPIDEVCF_ADDR		0
60 #define acpidevcf_addr		cf_loc[ACPIDEVCF_ADDR]
61 #define ACPIDEVCF_ADDR_UNK	-1
62 
63 struct acpi_attach_args {
64 	char		*aaa_name;
65 	bus_space_tag_t	 aaa_iot;
66 	bus_space_tag_t	 aaa_memt;
67 	bus_dma_tag_t	 aaa_dmat;
68 	void		*aaa_table;
69 	struct aml_node *aaa_node;
70 	const char	*aaa_dev;
71 	const char	*aaa_cdev;
72 	uint64_t	 aaa_addr[8];
73 	uint64_t	 aaa_size[8];
74 	bus_space_tag_t	 aaa_bst[8];
75 	int		 aaa_naddr;
76 	uint32_t	 aaa_irq[8];
77 	uint32_t	 aaa_irq_flags[8];
78 	int		 aaa_nirq;
79 };
80 
81 struct acpi_mem_map {
82 	vaddr_t		 baseva;
83 	uint8_t		*va;
84 	size_t		 vsize;
85 	paddr_t		 pa;
86 };
87 
88 struct acpi_q {
89 	SIMPLEQ_ENTRY(acpi_q)	 q_next;
90 	int			 q_id;
91 	void			*q_table;
92 	uint8_t			 q_data[0];
93 };
94 
95 struct acpi_taskq {
96 	SIMPLEQ_ENTRY(acpi_taskq)	next;
97 	void 				(*handler)(void *, int);
98 	void				*arg0;
99 	int				arg1;
100 };
101 
102 struct acpi_wakeq {
103 	SIMPLEQ_ENTRY(acpi_wakeq)	 q_next;
104 	struct aml_node			*q_node;
105 	struct aml_value		*q_wakepkg;
106 	int				 q_gpe;
107 	int				 q_state;
108 	int				 q_enabled;
109 };
110 
111 #if NACPIPWRRES > 0
112 struct acpi_pwrres {
113 	SIMPLEQ_ENTRY(acpi_pwrres)	 p_next;
114 	struct aml_node			*p_node;	/* device's node */
115 	int				 p_state;	/* current state */
116 
117 	int				 p_res_state;
118 	struct acpipwrres_softc		*p_res_sc;
119 };
120 
121 typedef SIMPLEQ_HEAD(, acpi_pwrres) acpi_pwrreshead_t;
122 #endif /* NACPIPWRRES > 0 */
123 
124 typedef SIMPLEQ_HEAD(, acpi_q) acpi_qhead_t;
125 typedef SIMPLEQ_HEAD(, acpi_wakeq) acpi_wakeqhead_t;
126 
127 #define ACPIREG_PM1A_STS	0x00
128 #define ACPIREG_PM1A_EN		0x01
129 #define ACPIREG_PM1A_CNT	0x02
130 #define ACPIREG_PM1B_STS	0x03
131 #define ACPIREG_PM1B_EN		0x04
132 #define ACPIREG_PM1B_CNT	0x05
133 #define ACPIREG_PM2_CNT		0x06
134 #define ACPIREG_PM_TMR		0x07
135 #define ACPIREG_GPE0_STS	0x08
136 #define ACPIREG_GPE0_EN		0x09
137 #define ACPIREG_GPE1_STS	0x0A
138 #define ACPIREG_GPE1_EN		0x0B
139 #define ACPIREG_SMICMD		0x0C
140 #define ACPIREG_MAXREG		0x0D
141 
142 /* Special registers */
143 #define ACPIREG_PM1_STS		0x0E
144 #define ACPIREG_PM1_EN		0x0F
145 #define ACPIREG_PM1_CNT		0x10
146 #define ACPIREG_GPE_STS		0x11
147 #define ACPIREG_GPE_EN		0x12
148 
149 /* System status (_SST) codes */
150 #define ACPI_SST_INDICATOR_OFF	0
151 #define ACPI_SST_WORKING	1
152 #define ACPI_SST_WAKING		2
153 #define ACPI_SST_SLEEPING	3
154 #define ACPI_SST_SLEEP_CONTEXT	4
155 
156 struct acpi_reg_map {
157 	bus_space_handle_t	ioh;
158 	int			addr;
159 	int			size;
160 	int			access;
161 	const char		*name;
162 };
163 
164 struct acpi_thread {
165 	struct acpi_softc   *sc;
166 	volatile int	    running;
167 };
168 
169 struct acpi_mutex {
170 	struct rwlock		amt_lock;
171 #define ACPI_MTX_MAXNAME	5
172 	char			amt_name[ACPI_MTX_MAXNAME + 3]; /* only 4 used */
173 	int			amt_ref_count;
174 	int			amt_timeout;
175 	int			amt_synclevel;
176 };
177 
178 struct gpe_block {
179 	int  (*handler)(struct acpi_softc *, int, void *);
180 	void *arg;
181 	int   active;
182 	int   flags;
183 };
184 
185 struct acpi_devlist {
186 	struct aml_node			*dev_node;
187 	TAILQ_ENTRY(acpi_devlist)	dev_link;
188 };
189 
190 TAILQ_HEAD(acpi_devlist_head, acpi_devlist);
191 
192 struct acpi_ac {
193 	struct acpiac_softc	*aac_softc;
194 	SLIST_ENTRY(acpi_ac)	aac_link;
195 };
196 
197 SLIST_HEAD(acpi_ac_head, acpi_ac);
198 
199 struct acpi_bat {
200 	struct acpibat_softc	*aba_softc;
201 	SLIST_ENTRY(acpi_bat)	aba_link;
202 };
203 
204 SLIST_HEAD(acpi_bat_head, acpi_bat);
205 
206 struct acpi_sbs {
207 	struct acpisbs_softc	*asbs_softc;
208 	SLIST_ENTRY(acpi_sbs)	asbs_link;
209 };
210 
211 SLIST_HEAD(acpi_sbs_head, acpi_sbs);
212 
213 struct acpi_softc {
214 	struct device		sc_dev;
215 
216 	bus_space_tag_t		sc_iot;
217 	bus_space_tag_t		sc_memt;
218 	bus_dma_tag_t		sc_cc_dmat;
219 	bus_dma_tag_t		sc_ci_dmat;
220 
221 	/*
222 	 * First-level ACPI tables
223 	 */
224 	struct acpi_fadt	*sc_fadt;
225 	acpi_qhead_t		 sc_tables;
226 	acpi_wakeqhead_t	 sc_wakedevs;
227 #if NACPIPWRRES > 0
228 	acpi_pwrreshead_t	 sc_pwrresdevs;
229 #endif /* NACPIPWRRES > 0 */
230 	int			 sc_hw_reduced;
231 
232 	/*
233 	 * Second-level information from FADT
234 	 */
235 	struct acpi_facs	*sc_facs;	/* Shared with firmware! */
236 
237 	struct klist		sc_note;
238 	struct acpi_reg_map	sc_pmregs[ACPIREG_MAXREG];
239 	bus_space_handle_t	sc_ioh_pm1a_evt;
240 
241 	void			*sc_interrupt;
242 
243 	struct rwlock		sc_lck;
244 
245 	struct {
246 		int slp_typa;
247 		int slp_typb;
248 	}			sc_sleeptype[6];
249 	int			sc_lastgpe;
250 	int			sc_wakegpe;
251 
252 	struct gpe_block	*gpe_table;
253 
254 	int			sc_threadwaiting;
255 	uint32_t		sc_gpe_sts;
256 	uint32_t		sc_gpe_en;
257 	struct acpi_thread	*sc_thread;
258 
259 	struct aml_node		*sc_root;
260 	struct aml_node		*sc_tts;
261 	struct aml_node		*sc_pts;
262 	struct aml_node		*sc_bfs;
263 	struct aml_node		*sc_gts;
264 	struct aml_node		*sc_sst;
265 	struct aml_node		*sc_wak;
266 	int			sc_state;
267 	int			sc_wakeup;
268 	int			sc_wakeups;
269 	time_t			sc_resume_time;
270 	struct acpiec_softc	*sc_ec;		/* XXX assume single EC */
271 
272 	struct acpi_ac_head	sc_ac;
273 	struct acpi_bat_head	sc_bat;
274 	struct acpi_sbs_head	sc_sbs;
275 	int			sc_havesbs;
276 
277 	struct timeout		sc_dev_timeout;
278 
279 	int			sc_major;
280 	int			sc_minor;
281 
282 	int			sc_pse;		/* passive cooling enabled */
283 
284 	int			sc_flags;
285 
286 	int			sc_skip_processor;
287 
288 	void			(*sc_pmc_suspend)(void *);
289 	void			(*sc_pmc_resume)(void *);
290 	void			*sc_pmc_cookie;
291 };
292 
293 extern struct acpi_softc *acpi_softc;
294 
295 #define	SCFLAG_OREAD	0x0000001
296 #define	SCFLAG_OWRITE	0x0000002
297 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
298 
299 #define GPE_NONE	0x00
300 #define GPE_LEVEL	0x01
301 #define GPE_EDGE	0x02
302 
303 #if defined(_KERNEL)
304 
305 struct   acpi_gas;
306 int	 acpi_map_address(struct acpi_softc *, struct acpi_gas *, bus_addr_t,
307 	     bus_size_t, bus_space_handle_t *, bus_space_tag_t *);
308 
309 int	 acpi_map(paddr_t, size_t, struct acpi_mem_map *);
310 void	 acpi_unmap(struct acpi_mem_map *);
311 
312 int	 acpi_bus_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
313 	     bus_space_handle_t *);
314 void	 acpi_bus_space_unmap(bus_space_tag_t, bus_space_handle_t, bus_size_t);
315 
316 struct	 bios_attach_args;
317 int	 acpi_probe(struct device *, struct cfdata *, struct bios_attach_args *);
318 u_int	 acpi_checksum(const void *, size_t);
319 void	 acpi_attach_common(struct acpi_softc *, paddr_t);
320 void	 acpi_attach_machdep(struct acpi_softc *);
321 int	 acpi_interrupt(void *);
322 void	 acpi_powerdown(void);
323 void	 acpi_reset(void);
324 
325 int	 acpi_sleep_cpu(struct acpi_softc *, int);
326 void	 acpi_sleep_pm(struct acpi_softc *, int);
327 void	 acpi_resume_pm(struct acpi_softc *, int);
328 void	 acpi_resume_cpu(struct acpi_softc *, int);
329 int	 acpi_resuming(struct acpi_softc *);
330 
331 #define ACPI_IOREAD 0
332 #define ACPI_IOWRITE 1
333 
334 void acpi_wakeup(void *);
335 
336 int acpi_gasio(struct acpi_softc *, int, int, uint64_t, int, int, void *);
337 
338 void	acpi_register_gpio(struct acpi_softc *, struct aml_node *);
339 void	acpi_register_gsb(struct acpi_softc *, struct aml_node *);
340 
341 int	acpi_set_gpehandler(struct acpi_softc *, int,
342 	    int (*)(struct acpi_softc *, int, void *), void *, int);
343 
344 void	acpiec_read(struct acpiec_softc *, uint8_t, int, uint8_t *);
345 void	acpiec_write(struct acpiec_softc *, uint8_t, int, uint8_t *);
346 int	acpiec_gpehandler(struct acpi_softc *, int, void *);
347 
348 #if NACPIPWRRES > 0
349 int	acpipwrres_ref_incr(struct acpipwrres_softc *, struct aml_node *);
350 int	acpipwrres_ref_decr(struct acpipwrres_softc *, struct aml_node *);
351 #endif /* NACPIPWRRES > 0 */
352 
353 int	acpi_read_pmreg(struct acpi_softc *, int, int);
354 void	acpi_write_pmreg(struct acpi_softc *, int, int, int);
355 
356 void	acpi_poll(void *);
357 void	acpi_sleep(int, char *);
358 
359 int	acpi_matchcls(struct acpi_attach_args *, int, int, int);
360 int	acpi_matchhids(struct acpi_attach_args *, const char *[], const char *);
361 int	acpi_parsehid(struct aml_node *, void *, char *, char *, size_t);
362 void	acpi_parse_crs(struct acpi_softc *, struct acpi_attach_args *);
363 int64_t	acpi_getsta(struct acpi_softc *sc, struct aml_node *);
364 
365 int	acpi_getprop(struct aml_node *, const char *, void *, int);
366 uint64_t acpi_getpropint(struct aml_node *, const char *, uint64_t);
367 
368 void	acpi_indicator(struct acpi_softc *, int);
369 void	acpi_disable_allgpes(struct acpi_softc *);
370 void	acpi_enable_wakegpes(struct acpi_softc *, int);
371 
372 int	acpi_batcount(struct acpi_softc *);
373 struct apm_power_info;
374 int	acpi_apminfo(struct apm_power_info *);
375 
376 int	acpi_record_event(struct acpi_softc *, u_int);
377 
378 void	acpi_addtask(struct acpi_softc *, void (*)(void *, int), void *, int);
379 int	acpi_dotask(struct acpi_softc *);
380 
381 void	acpi_powerdown_task(void *, int);
382 void	acpi_sleep_task(void *, int);
383 
384 /* Section 5.2.10.1: global lock acquire/release functions */
385 #define	GL_BIT_PENDING	0x01
386 #define	GL_BIT_OWNED	0x02
387 int	acpi_acquire_glk(uint32_t *);
388 int	acpi_release_glk(uint32_t *);
389 
390 void	acpi_pciroots_attach(struct device *, void *, cfprint_t);
391 void	acpi_attach_deps(struct acpi_softc *, struct aml_node *);
392 
393 struct aml_node *acpi_find_pci(pci_chipset_tag_t, pcitag_t);
394 
395 void	*acpi_intr_establish(int, int, int, int (*)(void *), void *,
396 	    const char *);
397 void	acpi_intr_disestablish(void *);
398 
399 struct acpi_q *acpi_maptable(struct acpi_softc *sc, paddr_t,
400 	    const char *, const char *, const char *, int);
401 
402 bus_dma_tag_t acpi_iommu_device_map(struct aml_node *, bus_dma_tag_t);
403 
404 int	acpi_toggle_wakedev(struct acpi_softc *, struct aml_node *, int);
405 
406 #endif
407 
408 #endif /* !_ACPI_WAKECODE */
409 #endif	/* !_DEV_ACPI_ACPIVAR_H_ */
410