xref: /openbsd/sys/dev/acpi/acpivar.h (revision 8a0aee6c)
1 /*	$OpenBSD: acpivar.h,v 1.127 2024/05/13 19:56:37 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_parsestate {
157 	uint8_t			*start;
158 	uint8_t			*end;
159 	uint8_t			*pos;
160 };
161 
162 struct acpi_reg_map {
163 	bus_space_handle_t	ioh;
164 	int			addr;
165 	int			size;
166 	int			access;
167 	const char		*name;
168 };
169 
170 struct acpi_thread {
171 	struct acpi_softc   *sc;
172 	volatile int	    running;
173 };
174 
175 struct acpi_mutex {
176 	struct rwlock		amt_lock;
177 #define ACPI_MTX_MAXNAME	5
178 	char			amt_name[ACPI_MTX_MAXNAME + 3]; /* only 4 used */
179 	int			amt_ref_count;
180 	int			amt_timeout;
181 	int			amt_synclevel;
182 };
183 
184 struct gpe_block {
185 	int  (*handler)(struct acpi_softc *, int, void *);
186 	void *arg;
187 	int   active;
188 	int   flags;
189 };
190 
191 struct acpi_devlist {
192 	struct aml_node			*dev_node;
193 	TAILQ_ENTRY(acpi_devlist)	dev_link;
194 };
195 
196 TAILQ_HEAD(acpi_devlist_head, acpi_devlist);
197 
198 struct acpi_ac {
199 	struct acpiac_softc	*aac_softc;
200 	SLIST_ENTRY(acpi_ac)	aac_link;
201 };
202 
203 SLIST_HEAD(acpi_ac_head, acpi_ac);
204 
205 struct acpi_bat {
206 	struct acpibat_softc	*aba_softc;
207 	SLIST_ENTRY(acpi_bat)	aba_link;
208 };
209 
210 SLIST_HEAD(acpi_bat_head, acpi_bat);
211 
212 struct acpi_sbs {
213 	struct acpisbs_softc	*asbs_softc;
214 	SLIST_ENTRY(acpi_sbs)	asbs_link;
215 };
216 
217 SLIST_HEAD(acpi_sbs_head, acpi_sbs);
218 
219 struct acpi_softc {
220 	struct device		sc_dev;
221 
222 	bus_space_tag_t		sc_iot;
223 	bus_space_tag_t		sc_memt;
224 	bus_dma_tag_t		sc_cc_dmat;
225 	bus_dma_tag_t		sc_ci_dmat;
226 
227 	/*
228 	 * First-level ACPI tables
229 	 */
230 	struct acpi_fadt	*sc_fadt;
231 	acpi_qhead_t		 sc_tables;
232 	acpi_wakeqhead_t	 sc_wakedevs;
233 #if NACPIPWRRES > 0
234 	acpi_pwrreshead_t	 sc_pwrresdevs;
235 #endif /* NACPIPWRRES > 0 */
236 	int			 sc_hw_reduced;
237 
238 	/*
239 	 * Second-level information from FADT
240 	 */
241 	struct acpi_facs	*sc_facs;	/* Shared with firmware! */
242 
243 	struct klist		sc_note;
244 	struct acpi_reg_map	sc_pmregs[ACPIREG_MAXREG];
245 	bus_space_handle_t	sc_ioh_pm1a_evt;
246 
247 	void			*sc_interrupt;
248 
249 	struct rwlock		sc_lck;
250 
251 	struct {
252 		int slp_typa;
253 		int slp_typb;
254 	}			sc_sleeptype[6];
255 	int			sc_lastgpe;
256 
257 	struct gpe_block	*gpe_table;
258 
259 	int			sc_threadwaiting;
260 	uint32_t		sc_gpe_sts;
261 	uint32_t		sc_gpe_en;
262 	struct acpi_thread	*sc_thread;
263 
264 	struct aml_node		*sc_root;
265 	struct aml_node		*sc_tts;
266 	struct aml_node		*sc_pts;
267 	struct aml_node		*sc_bfs;
268 	struct aml_node		*sc_gts;
269 	struct aml_node		*sc_sst;
270 	struct aml_node		*sc_wak;
271 	int			sc_state;
272 	time_t			sc_resume_time;
273 	struct acpiec_softc	*sc_ec;		/* XXX assume single EC */
274 
275 	struct acpi_ac_head	sc_ac;
276 	struct acpi_bat_head	sc_bat;
277 	struct acpi_sbs_head	sc_sbs;
278 	int			sc_havesbs;
279 
280 	struct timeout		sc_dev_timeout;
281 
282 	int			sc_major;
283 	int			sc_minor;
284 
285 	int			sc_pse;		/* passive cooling enabled */
286 
287 	int			sc_flags;
288 
289 	int			sc_skip_processor;
290 };
291 
292 extern struct acpi_softc *acpi_softc;
293 
294 #define	SCFLAG_OREAD	0x0000001
295 #define	SCFLAG_OWRITE	0x0000002
296 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
297 
298 #define GPE_NONE	0x00
299 #define GPE_LEVEL	0x01
300 #define GPE_EDGE	0x02
301 
302 struct acpi_table {
303 	int	offset;
304 	size_t	size;
305 	void	*table;
306 };
307 
308 struct acpi_dev_rank {
309 	struct device	*dev;
310 	int		rank;
311 	TAILQ_ENTRY(acpi_dev_rank) link;
312 };
313 
314 #define	ACPI_IOC_GETFACS	_IOR('A', 0, struct acpi_facs)
315 #define	ACPI_IOC_GETTABLE	_IOWR('A', 1, struct acpi_table)
316 #define ACPI_IOC_SETSLEEPSTATE	_IOW('A', 2, int)
317 
318 #if defined(_KERNEL)
319 
320 struct   acpi_gas;
321 int	 acpi_map_address(struct acpi_softc *, struct acpi_gas *, bus_addr_t,
322 	     bus_size_t, bus_space_handle_t *, bus_space_tag_t *);
323 
324 int	 acpi_map(paddr_t, size_t, struct acpi_mem_map *);
325 void	 acpi_unmap(struct acpi_mem_map *);
326 
327 int	 acpi_bus_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
328 	     bus_space_handle_t *);
329 void	 acpi_bus_space_unmap(bus_space_tag_t, bus_space_handle_t, bus_size_t);
330 
331 struct	 bios_attach_args;
332 int	 acpi_probe(struct device *, struct cfdata *, struct bios_attach_args *);
333 u_int	 acpi_checksum(const void *, size_t);
334 void	 acpi_attach_common(struct acpi_softc *, paddr_t);
335 void	 acpi_attach_machdep(struct acpi_softc *);
336 int	 acpi_interrupt(void *);
337 void	 acpi_powerdown(void);
338 void	 acpi_reset(void);
339 
340 int	 acpi_sleep_cpu(struct acpi_softc *, int);
341 void	 acpi_sleep_pm(struct acpi_softc *, int);
342 void	 acpi_resume_pm(struct acpi_softc *, int);
343 void	 acpi_resume_cpu(struct acpi_softc *, int);
344 int	 acpi_resuming(struct acpi_softc *);
345 
346 #define ACPI_IOREAD 0
347 #define ACPI_IOWRITE 1
348 
349 void acpi_wakeup(void *);
350 
351 int acpi_gasio(struct acpi_softc *, int, int, uint64_t, int, int, void *);
352 
353 void	acpi_register_gpio(struct acpi_softc *, struct aml_node *);
354 void	acpi_register_gsb(struct acpi_softc *, struct aml_node *);
355 
356 int	acpi_set_gpehandler(struct acpi_softc *, int,
357 	    int (*)(struct acpi_softc *, int, void *), void *, int);
358 
359 void	acpiec_read(struct acpiec_softc *, uint8_t, int, uint8_t *);
360 void	acpiec_write(struct acpiec_softc *, uint8_t, int, uint8_t *);
361 
362 #if NACPIPWRRES > 0
363 int	acpipwrres_ref_incr(struct acpipwrres_softc *, struct aml_node *);
364 int	acpipwrres_ref_decr(struct acpipwrres_softc *, struct aml_node *);
365 #endif /* NACPIPWRRES > 0 */
366 
367 int	acpi_read_pmreg(struct acpi_softc *, int, int);
368 void	acpi_write_pmreg(struct acpi_softc *, int, int, int);
369 
370 void	acpi_poll(void *);
371 void	acpi_sleep(int, char *);
372 
373 int	acpi_matchcls(struct acpi_attach_args *, int, int, int);
374 int	acpi_matchhids(struct acpi_attach_args *, const char *[], const char *);
375 int	acpi_parsehid(struct aml_node *, void *, char *, char *, size_t);
376 void	acpi_parse_crs(struct acpi_softc *, struct acpi_attach_args *);
377 int64_t	acpi_getsta(struct acpi_softc *sc, struct aml_node *);
378 
379 int	acpi_getprop(struct aml_node *, const char *, void *, int);
380 uint64_t acpi_getpropint(struct aml_node *, const char *, uint64_t);
381 
382 void	acpi_indicator(struct acpi_softc *, int);
383 void	acpi_disable_allgpes(struct acpi_softc *);
384 void	acpi_enable_wakegpes(struct acpi_softc *, int);
385 
386 int	acpi_batcount(struct acpi_softc *);
387 struct apm_power_info;
388 int	acpi_apminfo(struct apm_power_info *);
389 
390 int	acpi_record_event(struct acpi_softc *, u_int);
391 
392 void	acpi_addtask(struct acpi_softc *, void (*)(void *, int), void *, int);
393 int	acpi_dotask(struct acpi_softc *);
394 
395 void	acpi_powerdown_task(void *, int);
396 void	acpi_sleep_task(void *, int);
397 
398 /* Section 5.2.10.1: global lock acquire/release functions */
399 #define	GL_BIT_PENDING	0x01
400 #define	GL_BIT_OWNED	0x02
401 int	acpi_acquire_glk(uint32_t *);
402 int	acpi_release_glk(uint32_t *);
403 
404 void	acpi_pciroots_attach(struct device *, void *, cfprint_t);
405 void	acpi_attach_deps(struct acpi_softc *, struct aml_node *);
406 
407 struct aml_node *acpi_find_pci(pci_chipset_tag_t, pcitag_t);
408 
409 void	*acpi_intr_establish(int, int, int, int (*)(void *), void *,
410 	    const char *);
411 void	acpi_intr_disestablish(void *);
412 
413 struct acpi_q *acpi_maptable(struct acpi_softc *sc, paddr_t,
414 	    const char *, const char *, const char *, int);
415 
416 bus_dma_tag_t acpi_iommu_device_map(struct aml_node *, bus_dma_tag_t);
417 
418 int	acpi_toggle_wakedev(struct acpi_softc *, struct aml_node *, int);
419 
420 #endif
421 
422 #endif /* !_ACPI_WAKECODE */
423 #endif	/* !_DEV_ACPI_ACPIVAR_H_ */
424