xref: /openbsd/sys/dev/acpi/acpivar.h (revision 097a140d)
1 /*	$OpenBSD: acpivar.h,v 1.114 2021/03/15 22:44:57 patrick 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[8];
73 	uint32_t	 aaa_irq_flags[8];
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_lastgpe;
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_tts;
260 	struct aml_node		*sc_pts;
261 	struct aml_node		*sc_bfs;
262 	struct aml_node		*sc_gts;
263 	struct aml_node		*sc_sst;
264 	struct aml_node		*sc_wak;
265 	int			sc_state;
266 	struct acpiec_softc	*sc_ec;		/* XXX assume single EC */
267 
268 	struct acpi_ac_head	sc_ac;
269 	struct acpi_bat_head	sc_bat;
270 	struct acpi_sbs_head	sc_sbs;
271 	int			sc_havesbs;
272 
273 	struct timeout		sc_dev_timeout;
274 
275 	int			sc_major;
276 	int			sc_minor;
277 
278 	int			sc_pse;		/* passive cooling enabled */
279 
280 	int			sc_flags;
281 
282 	int			sc_skip_processor;
283 };
284 
285 extern struct acpi_softc *acpi_softc;
286 
287 #define	SCFLAG_OREAD	0x0000001
288 #define	SCFLAG_OWRITE	0x0000002
289 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
290 
291 #define GPE_NONE	0x00
292 #define GPE_LEVEL	0x01
293 #define GPE_EDGE	0x02
294 #define GPE_DIRECT	0x04
295 
296 struct acpi_table {
297 	int	offset;
298 	size_t	size;
299 	void	*table;
300 };
301 
302 struct acpi_dev_rank {
303 	struct device	*dev;
304 	int		rank;
305 	TAILQ_ENTRY(acpi_dev_rank) link;
306 };
307 
308 #define	ACPI_IOC_GETFACS	_IOR('A', 0, struct acpi_facs)
309 #define	ACPI_IOC_GETTABLE	_IOWR('A', 1, struct acpi_table)
310 #define ACPI_IOC_SETSLEEPSTATE	_IOW('A', 2, int)
311 
312 #if defined(_KERNEL)
313 
314 struct   acpi_gas;
315 int	 acpi_map_address(struct acpi_softc *, struct acpi_gas *, bus_addr_t,
316 	     bus_size_t, bus_space_handle_t *, bus_space_tag_t *);
317 
318 int	 acpi_map(paddr_t, size_t, struct acpi_mem_map *);
319 void	 acpi_unmap(struct acpi_mem_map *);
320 
321 int	 acpi_bus_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
322 	     bus_space_handle_t *);
323 void	 acpi_bus_space_unmap(bus_space_tag_t, bus_space_handle_t, bus_size_t);
324 
325 struct	 bios_attach_args;
326 int	 acpi_probe(struct device *, struct cfdata *, struct bios_attach_args *);
327 u_int	 acpi_checksum(const void *, size_t);
328 void	 acpi_attach_common(struct acpi_softc *, paddr_t);
329 void	 acpi_attach_machdep(struct acpi_softc *);
330 int	 acpi_interrupt(void *);
331 void	 acpi_powerdown(void);
332 void	 acpi_reset(void);
333 
334 
335 #define ACPI_SLEEP_SUSPEND	0x01
336 #define ACPI_SLEEP_HIBERNATE	0x02
337 
338 int	 acpi_sleep_state(struct acpi_softc *, int);
339 void	 acpi_sleep_clocks(struct acpi_softc *, int);
340 int	 acpi_sleep_cpu(struct acpi_softc *, int);
341 void	 acpi_sleep_mp(void);
342 void	 acpi_sleep_pm(struct acpi_softc *, int);
343 void	 acpi_resume_pm(struct acpi_softc *, int);
344 void	 acpi_resume_cpu(struct acpi_softc *, int);
345 void	 acpi_resume_mp(void);
346 void	 acpi_sleep_walk(struct acpi_softc *, int);
347 
348 
349 #define ACPI_IOREAD 0
350 #define ACPI_IOWRITE 1
351 
352 void acpi_wakeup(void *);
353 
354 int acpi_gasio(struct acpi_softc *, int, int, uint64_t, int, int, void *);
355 
356 void	acpi_register_gpio(struct acpi_softc *, struct aml_node *);
357 void	acpi_register_gsb(struct acpi_softc *, struct aml_node *);
358 
359 int	acpi_set_gpehandler(struct acpi_softc *, int,
360 	    int (*)(struct acpi_softc *, int, void *), void *, int);
361 void	acpi_enable_gpe(struct acpi_softc *, uint32_t);
362 
363 int	acpiec_intr(struct acpiec_softc *);
364 void	acpiec_read(struct acpiec_softc *, uint8_t, int, uint8_t *);
365 void	acpiec_write(struct acpiec_softc *, uint8_t, int, uint8_t *);
366 void	acpiec_handle_events(struct acpiec_softc *);
367 
368 #if NACPIPWRRES > 0
369 int	acpipwrres_ref_incr(struct acpipwrres_softc *, struct aml_node *);
370 int	acpipwrres_ref_decr(struct acpipwrres_softc *, struct aml_node *);
371 #endif /* NACPIPWRRES > 0 */
372 
373 int	acpi_read_pmreg(struct acpi_softc *, int, int);
374 void	acpi_write_pmreg(struct acpi_softc *, int, int, int);
375 
376 void	acpi_poll(void *);
377 void	acpi_sleep(int, char *);
378 
379 int	acpi_matchcls(struct acpi_attach_args *, int, int, int);
380 int	acpi_matchhids(struct acpi_attach_args *, const char *[], const char *);
381 int	acpi_parsehid(struct aml_node *, void *, char *, char *, size_t);
382 int64_t	acpi_getsta(struct acpi_softc *sc, struct aml_node *);
383 
384 int	acpi_getprop(struct aml_node *, const char *, void *, int);
385 uint32_t acpi_getpropint(struct aml_node *, const char *, uint32_t);
386 
387 int	acpi_record_event(struct acpi_softc *, u_int);
388 
389 void	acpi_addtask(struct acpi_softc *, void (*)(void *, int), void *, int);
390 int	acpi_dotask(struct acpi_softc *);
391 
392 void	acpi_powerdown_task(void *, int);
393 void	acpi_sleep_task(void *, int);
394 
395 /* Section 5.2.10.1: global lock acquire/release functions */
396 #define	GL_BIT_PENDING	0x01
397 #define	GL_BIT_OWNED	0x02
398 int	acpi_acquire_glk(uint32_t *);
399 int	acpi_release_glk(uint32_t *);
400 
401 void	acpi_pciroots_attach(struct device *, void *, cfprint_t);
402 void	acpi_attach_deps(struct acpi_softc *, struct aml_node *);
403 
404 struct aml_node *acpi_find_pci(pci_chipset_tag_t, pcitag_t);
405 
406 void	*acpi_intr_establish(int, int, int, int (*)(void *), void *,
407 	    const char *);
408 void	acpi_intr_disestablish(void *);
409 
410 struct acpi_q *acpi_maptable(struct acpi_softc *sc, paddr_t,
411 	    const char *, const char *, const char *, int);
412 
413 bus_dma_tag_t acpi_iommu_device_map(struct aml_node *, bus_dma_tag_t);
414 
415 #endif
416 
417 #endif /* !_ACPI_WAKECODE */
418 #endif	/* !_DEV_ACPI_ACPIVAR_H_ */
419