xref: /openbsd/sys/dev/acpi/acpivar.h (revision 09467b48)
1 /*	$OpenBSD: acpivar.h,v 1.109 2020/05/14 13:07:10 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/timeout.h>
27 #include <sys/rwlock.h>
28 
29 #include <machine/bus.h>
30 
31 #include <dev/pci/pcivar.h>
32 
33 #include "acpipwrres.h"
34 
35 /* #define ACPI_DEBUG */
36 #ifdef ACPI_DEBUG
37 extern int acpi_debug;
38 #define dprintf(x...)	  do { if (acpi_debug) printf(x); } while (0)
39 #define dnprintf(n,x...)  do { if (acpi_debug > (n)) printf(x); } while (0)
40 #else
41 #define dprintf(x...)
42 #define dnprintf(n,x...)
43 #endif
44 
45 extern int acpi_hasprocfvs;
46 extern int acpi_haspci;
47 
48 struct klist;
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 struct acpi_attach_args {
60 	char		*aaa_name;
61 	bus_space_tag_t	 aaa_iot;
62 	bus_space_tag_t	 aaa_memt;
63 	bus_dma_tag_t	 aaa_dmat;
64 	void		*aaa_table;
65 	struct aml_node *aaa_node;
66 	const char	*aaa_dev;
67 	const char	*aaa_cdev;
68 	uint64_t	 aaa_addr[4];
69 	uint64_t	 aaa_size[4];
70 	bus_space_tag_t	 aaa_bst[4];
71 	int		 aaa_naddr;
72 	uint32_t	 aaa_irq[4];
73 	uint32_t	 aaa_irq_flags[4];
74 	int		 aaa_nirq;
75 };
76 
77 struct acpi_mem_map {
78 	vaddr_t		 baseva;
79 	uint8_t		*va;
80 	size_t		 vsize;
81 	paddr_t		 pa;
82 };
83 
84 struct acpi_q {
85 	SIMPLEQ_ENTRY(acpi_q)	 q_next;
86 	int			 q_id;
87 	void			*q_table;
88 	uint8_t			 q_data[0];
89 };
90 
91 struct acpi_taskq {
92 	SIMPLEQ_ENTRY(acpi_taskq)	next;
93 	void 				(*handler)(void *, int);
94 	void				*arg0;
95 	int				arg1;
96 };
97 
98 struct acpi_wakeq {
99 	SIMPLEQ_ENTRY(acpi_wakeq)	 q_next;
100 	struct aml_node			*q_node;
101 	struct aml_value		*q_wakepkg;
102 	int				 q_gpe;
103 	int				 q_state;
104 };
105 
106 #if NACPIPWRRES > 0
107 struct acpi_pwrres {
108 	SIMPLEQ_ENTRY(acpi_pwrres)	 p_next;
109 	struct aml_node			*p_node;	/* device's node */
110 	int				 p_state;	/* current state */
111 
112 	int				 p_res_state;
113 	struct acpipwrres_softc		*p_res_sc;
114 };
115 
116 typedef SIMPLEQ_HEAD(, acpi_pwrres) acpi_pwrreshead_t;
117 #endif /* NACPIPWRRES > 0 */
118 
119 typedef SIMPLEQ_HEAD(, acpi_q) acpi_qhead_t;
120 typedef SIMPLEQ_HEAD(, acpi_wakeq) acpi_wakeqhead_t;
121 
122 #define ACPIREG_PM1A_STS	0x00
123 #define ACPIREG_PM1A_EN		0x01
124 #define ACPIREG_PM1A_CNT	0x02
125 #define ACPIREG_PM1B_STS	0x03
126 #define ACPIREG_PM1B_EN		0x04
127 #define ACPIREG_PM1B_CNT	0x05
128 #define ACPIREG_PM2_CNT		0x06
129 #define ACPIREG_PM_TMR		0x07
130 #define ACPIREG_GPE0_STS	0x08
131 #define ACPIREG_GPE0_EN		0x09
132 #define ACPIREG_GPE1_STS	0x0A
133 #define ACPIREG_GPE1_EN		0x0B
134 #define ACPIREG_SMICMD		0x0C
135 #define ACPIREG_MAXREG		0x0D
136 
137 /* Special registers */
138 #define ACPIREG_PM1_STS		0x0E
139 #define ACPIREG_PM1_EN		0x0F
140 #define ACPIREG_PM1_CNT		0x10
141 #define ACPIREG_GPE_STS		0x11
142 #define ACPIREG_GPE_EN		0x12
143 
144 /* System status (_SST) codes */
145 #define ACPI_SST_INDICATOR_OFF	0
146 #define ACPI_SST_WORKING	1
147 #define ACPI_SST_WAKING		2
148 #define ACPI_SST_SLEEPING	3
149 #define ACPI_SST_SLEEP_CONTEXT	4
150 
151 struct acpi_parsestate {
152 	uint8_t			*start;
153 	uint8_t			*end;
154 	uint8_t			*pos;
155 };
156 
157 struct acpi_reg_map {
158 	bus_space_handle_t	ioh;
159 	int			addr;
160 	int			size;
161 	int			access;
162 	const char		*name;
163 };
164 
165 struct acpi_thread {
166 	struct acpi_softc   *sc;
167 	volatile int	    running;
168 };
169 
170 struct acpi_mutex {
171 	struct rwlock		amt_lock;
172 #define ACPI_MTX_MAXNAME	5
173 	char			amt_name[ACPI_MTX_MAXNAME + 3]; /* only 4 used */
174 	int			amt_ref_count;
175 	int			amt_timeout;
176 	int			amt_synclevel;
177 };
178 
179 struct gpe_block {
180 	int  (*handler)(struct acpi_softc *, int, void *);
181 	void *arg;
182 	int   active;
183 	int   flags;
184 };
185 
186 struct acpi_devlist {
187 	struct aml_node			*dev_node;
188 	TAILQ_ENTRY(acpi_devlist)	dev_link;
189 };
190 
191 TAILQ_HEAD(acpi_devlist_head, acpi_devlist);
192 
193 struct acpi_ac {
194 	struct acpiac_softc	*aac_softc;
195 	SLIST_ENTRY(acpi_ac)	aac_link;
196 };
197 
198 SLIST_HEAD(acpi_ac_head, acpi_ac);
199 
200 struct acpi_bat {
201 	struct acpibat_softc	*aba_softc;
202 	SLIST_ENTRY(acpi_bat)	aba_link;
203 };
204 
205 SLIST_HEAD(acpi_bat_head, acpi_bat);
206 
207 struct acpi_sbs {
208 	struct acpisbs_softc	*asbs_softc;
209 	SLIST_ENTRY(acpi_sbs)	asbs_link;
210 };
211 
212 SLIST_HEAD(acpi_sbs_head, acpi_sbs);
213 
214 struct acpi_softc {
215 	struct device		sc_dev;
216 
217 	bus_space_tag_t		sc_iot;
218 	bus_space_tag_t		sc_memt;
219 	bus_dma_tag_t		sc_cc_dmat;
220 	bus_dma_tag_t		sc_ci_dmat;
221 
222 	/*
223 	 * First-level ACPI tables
224 	 */
225 	struct acpi_fadt	*sc_fadt;
226 	acpi_qhead_t		 sc_tables;
227 	acpi_wakeqhead_t	 sc_wakedevs;
228 #if NACPIPWRRES > 0
229 	acpi_pwrreshead_t	 sc_pwrresdevs;
230 #endif /* NACPIPWRRES > 0 */
231 	int			 sc_hw_reduced;
232 
233 	/*
234 	 * Second-level information from FADT
235 	 */
236 	struct acpi_facs	*sc_facs;	/* Shared with firmware! */
237 
238 	struct klist		*sc_note;
239 	struct acpi_reg_map	sc_pmregs[ACPIREG_MAXREG];
240 	bus_space_handle_t	sc_ioh_pm1a_evt;
241 
242 	void			*sc_interrupt;
243 
244 	struct rwlock		sc_lck;
245 
246 	struct {
247 		int slp_typa;
248 		int slp_typb;
249 	}			sc_sleeptype[6];
250 	int			sc_maxgpe;
251 	int			sc_lastgpe;
252 
253 	struct gpe_block	*gpe_table;
254 
255 	int			sc_threadwaiting;
256 	uint32_t		sc_gpe_sts;
257 	uint32_t		sc_gpe_en;
258 	struct acpi_thread	*sc_thread;
259 
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 	struct acpiec_softc	*sc_ec;		/* XXX assume single EC */
268 
269 	struct acpi_ac_head	sc_ac;
270 	struct acpi_bat_head	sc_bat;
271 	struct acpi_sbs_head	sc_sbs;
272 	int			sc_havesbs;
273 
274 	struct timeout		sc_dev_timeout;
275 
276 	int			sc_major;
277 	int			sc_minor;
278 
279 	int			sc_pse;		/* passive cooling enabled */
280 
281 	int			sc_flags;
282 };
283 
284 extern struct acpi_softc *acpi_softc;
285 
286 #define	SCFLAG_OREAD	0x0000001
287 #define	SCFLAG_OWRITE	0x0000002
288 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
289 
290 #define GPE_NONE	0x00
291 #define GPE_LEVEL	0x01
292 #define GPE_EDGE	0x02
293 #define GPE_DIRECT	0x04
294 
295 struct acpi_table {
296 	int	offset;
297 	size_t	size;
298 	void	*table;
299 };
300 
301 struct acpi_dev_rank {
302 	struct device	*dev;
303 	int		rank;
304 	TAILQ_ENTRY(acpi_dev_rank) link;
305 };
306 
307 #define	ACPI_IOC_GETFACS	_IOR('A', 0, struct acpi_facs)
308 #define	ACPI_IOC_GETTABLE	_IOWR('A', 1, struct acpi_table)
309 #define ACPI_IOC_SETSLEEPSTATE	_IOW('A', 2, int)
310 
311 #if defined(_KERNEL)
312 
313 struct   acpi_gas;
314 int	 acpi_map_address(struct acpi_softc *, struct acpi_gas *, bus_addr_t,
315 	     bus_size_t, bus_space_handle_t *, bus_space_tag_t *);
316 
317 int	 acpi_map(paddr_t, size_t, struct acpi_mem_map *);
318 void	 acpi_unmap(struct acpi_mem_map *);
319 
320 int	 acpi_bus_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
321 	     bus_space_handle_t *);
322 void	 acpi_bus_space_unmap(bus_space_tag_t, bus_space_handle_t, bus_size_t);
323 
324 struct	 bios_attach_args;
325 int	 acpi_probe(struct device *, struct cfdata *, struct bios_attach_args *);
326 u_int	 acpi_checksum(const void *, size_t);
327 void	 acpi_attach_common(struct acpi_softc *, paddr_t);
328 void	 acpi_attach_machdep(struct acpi_softc *);
329 int	 acpi_interrupt(void *);
330 void	 acpi_powerdown(void);
331 void	 acpi_reset(void);
332 
333 
334 #define ACPI_SLEEP_SUSPEND	0x01
335 #define ACPI_SLEEP_HIBERNATE	0x02
336 
337 int	 acpi_sleep_state(struct acpi_softc *, int);
338 void	 acpi_sleep_clocks(struct acpi_softc *, int);
339 int	 acpi_sleep_cpu(struct acpi_softc *, int);
340 void	 acpi_sleep_mp(void);
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 void	 acpi_resume_mp(void);
345 void	 acpi_sleep_walk(struct acpi_softc *, int);
346 
347 
348 #define ACPI_IOREAD 0
349 #define ACPI_IOWRITE 1
350 
351 void acpi_wakeup(void *);
352 
353 int acpi_gasio(struct acpi_softc *, int, int, uint64_t, int, int, void *);
354 
355 void	acpi_register_gpio(struct acpi_softc *, struct aml_node *);
356 void	acpi_register_gsb(struct acpi_softc *, struct aml_node *);
357 
358 int	acpi_set_gpehandler(struct acpi_softc *, int,
359 	    int (*)(struct acpi_softc *, int, void *), void *, int);
360 void	acpi_enable_gpe(struct acpi_softc *, uint32_t);
361 
362 int	acpiec_intr(struct acpiec_softc *);
363 void	acpiec_read(struct acpiec_softc *, uint8_t, int, uint8_t *);
364 void	acpiec_write(struct acpiec_softc *, uint8_t, int, uint8_t *);
365 void	acpiec_handle_events(struct acpiec_softc *);
366 
367 #if NACPIPWRRES > 0
368 int	acpipwrres_ref_incr(struct acpipwrres_softc *, struct aml_node *);
369 int	acpipwrres_ref_decr(struct acpipwrres_softc *, struct aml_node *);
370 #endif /* NACPIPWRRES > 0 */
371 
372 int	acpi_read_pmreg(struct acpi_softc *, int, int);
373 void	acpi_write_pmreg(struct acpi_softc *, int, int, int);
374 
375 void	acpi_poll(void *);
376 void	acpi_sleep(int, char *);
377 
378 int	acpi_matchcls(struct acpi_attach_args *, int, int, int);
379 int	acpi_matchhids(struct acpi_attach_args *, const char *[], const char *);
380 int	acpi_parsehid(struct aml_node *, void *, char *, char *, size_t);
381 int64_t	acpi_getsta(struct acpi_softc *sc, struct aml_node *);
382 
383 int	acpi_getprop(struct aml_node *, const char *, void *, int);
384 uint32_t acpi_getpropint(struct aml_node *, const char *, uint32_t);
385 
386 int	acpi_record_event(struct acpi_softc *, u_int);
387 
388 void	acpi_addtask(struct acpi_softc *, void (*)(void *, int), void *, int);
389 int	acpi_dotask(struct acpi_softc *);
390 
391 void	acpi_powerdown_task(void *, int);
392 void	acpi_sleep_task(void *, int);
393 
394 /* Section 5.2.10.1: global lock acquire/release functions */
395 #define	GL_BIT_PENDING	0x01
396 #define	GL_BIT_OWNED	0x02
397 int	acpi_acquire_glk(uint32_t *);
398 int	acpi_release_glk(uint32_t *);
399 
400 void	acpi_pciroots_attach(struct device *, void *, cfprint_t);
401 void	acpi_attach_deps(struct acpi_softc *, struct aml_node *);
402 
403 struct aml_node *acpi_find_pci(pci_chipset_tag_t, pcitag_t);
404 
405 void	*acpi_intr_establish(int, int, int, int (*)(void *), void *,
406 	    const char *);
407 
408 struct acpi_q *acpi_maptable(struct acpi_softc *sc, paddr_t,
409 	    const char *, const char *, const char *, int);
410 
411 #endif
412 
413 #endif /* !_ACPI_WAKECODE */
414 #endif	/* !_DEV_ACPI_ACPIVAR_H_ */
415