xref: /illumos-gate/usr/src/uts/i86pc/sys/acpidev.h (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2009, Intel Corporation.
23  * All rights reserved.
24  */
25 
26 #ifndef	_SYS_ACPIDEV_H
27 #define	_SYS_ACPIDEV_H
28 #include <sys/types.h>
29 #include <sys/obpdefs.h>
30 #include <sys/sunddi.h>
31 #include <sys/acpi/acpi.h>
32 #include <sys/acpica.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /* Maximum recursion levels when enumerating objects in ACPI namespace. */
39 #define	ACPIDEV_MAX_ENUM_LEVELS		32
40 
41 /* Maximum length of device name for ACPI object. */
42 #define	ACPIDEV_MAX_NAMELEN		OBP_MAXDRVNAME
43 
44 /* Pseudo ACPI device HID for ACPI root object. */
45 #define	ACPIDEV_HID_ROOTNEX		"SOLA0001"
46 /* Pseudo ACPI device HID for ACPI virtual bus. */
47 #define	ACPIDEV_HID_VIRTNEX		"SOLA0002"
48 #define	ACPIDEV_HID_SCOPE		"SOLA0003"
49 #define	ACPIDEV_HID_PROCESSOR		"SOLA0004"
50 
51 /* ACPI device HIDs/CIDs defined by ACPI specification. */
52 #define	ACPIDEV_HID_CONTAINER1		"PNP0A05"
53 #define	ACPIDEV_HID_CONTAINER2		"PNP0A06"
54 #define	ACPIDEV_HID_MODULE		"ACPI0004"
55 #define	ACPIDEV_HID_CPU			"ACPI0007"
56 #define	ACPIDEV_HID_PCI_HOSTBRIDGE	"PNP0A03"
57 #define	ACPIDEV_HID_PCIE_HOSTBRIDGE	"PNP0A08"
58 #define	ACPIDEV_HID_MEMORY		"PNP0C80"
59 
60 /* Common ACPI object names. */
61 #define	ACPIDEV_OBJECT_NAME_SB		ACPI_NS_SYSTEM_BUS
62 #define	ACPIDEV_OBJECT_NAME_PR		"_PR_"
63 
64 /* Common ACPI method names. */
65 #define	ACPIDEV_METHOD_NAME_MAT		"_MAT"
66 
67 /* Device names for ACPI objects. */
68 #define	ACPIDEV_NODE_NAME_ROOT		"fw"
69 #define	ACPIDEV_NODE_NAME_CONTAINER	"container"
70 #define	ACPIDEV_NODE_NAME_MODULE_SBD	"sb"
71 #define	ACPIDEV_NODE_NAME_MODULE_CPU	"socket"
72 #define	ACPIDEV_NODE_NAME_CPU		"cpu"
73 #define	ACPIDEV_NODE_NAME_PROCESSOR	"cpus"
74 #define	ACPIDEV_NODE_NAME_MEMORY	"mem"
75 
76 /* Device types for ACPI objects. */
77 #define	ACPIDEV_TYPE_ROOTNEX		"acpirootnex"
78 #define	ACPIDEV_TYPE_VIRTNEX		"acpivirtnex"
79 #define	ACPIDEV_TYPE_SCOPE		"acpiscope"
80 #define	ACPIDEV_TYPE_DEVICE		"acpidevice"
81 #define	ACPIDEV_TYPE_CONTAINER		"acpicontainer"
82 #define	ACPIDEV_TYPE_CPU		"cpu"
83 #define	ACPIDEV_TYPE_MEMORY		"acpimemory"
84 
85 /* Device property names for ACPI objects. */
86 #define	ACPIDEV_PROP_NAME_UNIT_ADDR	"unit-address"
87 #define	ACPIDEV_PROP_NAME_ACPI_UID	"acpi-uid"
88 #define	ACPIDEV_PROP_NAME_PROCESSOR_ID	"acpi-processor-id"
89 #define	ACPIDEV_PROP_NAME_LOCALAPIC_ID	"apic-id"
90 
91 #define	ACPIDEV_PROP_NAME_UID_FORMAT	"acpidev-uid-format"
92 
93 /* ACPI device class Id. */
94 typedef enum acpidev_class_id {
95 	ACPIDEV_CLASS_ID_INVALID = 0,
96 	ACPIDEV_CLASS_ID_ROOTNEX = 1,
97 	ACPIDEV_CLASS_ID_SCOPE = 2,
98 	ACPIDEV_CLASS_ID_DEVICE = 3,
99 	ACPIDEV_CLASS_ID_CONTAINER = 4,
100 	ACPIDEV_CLASS_ID_CPU = 5,
101 	ACPIDEV_CLASS_ID_MEMORY = 6,
102 	ACPIDEV_CLASS_ID_MAX
103 } acpidev_class_id_t;
104 
105 /* Flags for acpidev_options boot options. */
106 #define	ACPIDEV_OUSER_NO_CPU		0x1
107 #define	ACPIDEV_OUSER_NO_MEM		0x2
108 #define	ACPIDEV_OUSER_NO_CONTAINER	0x4
109 
110 #ifdef	_KERNEL
111 
112 /* Forward declaration */
113 typedef	struct acpidev_data_impl	*acpidev_data_handle_t;
114 typedef struct acpidev_walk_info	acpidev_walk_info_t;
115 typedef struct acpidev_filter_rule	acpidev_filter_rule_t;
116 typedef struct acpidev_class		acpidev_class_t;
117 typedef struct acpidev_class_list	acpidev_class_list_t;
118 
119 /* Type of ACPI device enumerating operation. */
120 typedef enum acpidev_op_type {
121 	ACPIDEV_OP_BOOT_PROBE = 0,	/* First pass probing at boot time. */
122 	ACPIDEV_OP_BOOT_REPROBE,	/* Second pass probing at boot time. */
123 	ACPIDEV_OP_HOTPLUG_PROBE	/* Probing for hotplug at runtime. */
124 } acpidev_op_type_t;
125 
126 /*
127  * Structure to pass arguments when enumerating ACPI namespace.
128  */
129 struct acpidev_walk_info {
130 	/* Always valid for all callbacks. */
131 	acpidev_op_type_t		awi_op_type;
132 	int				awi_level;
133 	acpidev_walk_info_t		*awi_parent;
134 	acpidev_class_t			*awi_class_curr;
135 
136 	/* Valid for all callbacks except pre_probe and post_probe. */
137 	int				awi_flags;
138 	ACPI_HANDLE			awi_hdl;
139 	ACPI_DEVICE_INFO		*awi_info;
140 	char				*awi_name;
141 	acpidev_data_handle_t		awi_data;
142 
143 	/* Need to validate it before access. */
144 	dev_info_t			*awi_dip;
145 	acpidev_class_list_t		**awi_class_list;
146 
147 	/* Used by class to store data temporarily. */
148 	intptr_t			awi_scratchpad[4];
149 };
150 
151 /* Disable creating device nodes for ACPI objects. */
152 #define	ACPIDEV_WI_DISABLE_CREATE	0x1
153 /* Device node has already been created for an ACPI object. */
154 #define	ACPIDEV_WI_DEVICE_CREATED	0x2
155 /* Disable enumerating children of ACPI objects. */
156 #define	ACPIDEV_WI_DISABLE_SCAN		0x10
157 /* Children of ACPI objects have already been enumerated. */
158 #define	ACPIDEV_WI_CHILD_SCANNED	0x20
159 
160 /*
161  * Device filtering result code.
162  * Device filtering logic will be applied to determine how to handle ACPI
163  * objects according to the filtering result code when enumerating ACPI objects.
164  */
165 typedef enum acpidev_filter_result {
166 	ACPIDEV_FILTER_FAILED = -1,	/* operation failed */
167 	ACPIDEV_FILTER_CONTINUE = 0,	/* continue to evaluate filter rules */
168 	ACPIDEV_FILTER_DEFAULT,		/* create node and scan child */
169 	ACPIDEV_FILTER_SCAN,		/* scan child of current node only */
170 	ACPIDEV_FILTER_CREATE,		/* create device node only */
171 	ACPIDEV_FILTER_SKIP,		/* skip current node */
172 } acpidev_filter_result_t;
173 
174 typedef acpidev_filter_result_t (* acpidev_filter_func_t)(acpidev_walk_info_t *,
175     ACPI_HANDLE, acpidev_filter_rule_t *, char *, int);
176 
177 /*
178  * Device filter rule data structure.
179  * User provided callback will be called if adf_filter_func is not NULL,
180  * otherwise default filtering algorithm will be applied.
181  */
182 struct acpidev_filter_rule {
183 	acpidev_filter_func_t		adf_filter_func;
184 	intptr_t			adf_filter_arg;
185 	acpidev_filter_result_t		adf_retcode;
186 	acpidev_class_list_t		**adf_class_list;
187 	intptr_t			adf_minlvl;
188 	intptr_t			adf_maxlvl;
189 	char				*adf_pattern;
190 	char				*adf_replace;
191 };
192 
193 /* Callback function prototypes for ACPI device class driver. */
194 typedef ACPI_STATUS (* acpidev_pre_probe_t)(acpidev_walk_info_t *);
195 typedef ACPI_STATUS (* acpidev_post_probe_t)(acpidev_walk_info_t *);
196 typedef ACPI_STATUS (* acpidev_probe_t)(acpidev_walk_info_t *);
197 typedef acpidev_filter_result_t (* acpidev_filter_t)(acpidev_walk_info_t *,
198     char *, int);
199 typedef ACPI_STATUS (* acpidev_init_t)(acpidev_walk_info_t *);
200 typedef ACPI_STATUS (* acpidev_fini_t)(ACPI_HANDLE, dev_info_t *,
201     acpidev_class_t *);
202 
203 /* Device class driver interface. */
204 struct acpidev_class {
205 	volatile uint32_t		adc_refcnt;
206 	int				adc_version;
207 	acpidev_class_id_t		adc_class_id;
208 	/* Name of device class, used in log messages. */
209 	char				*adc_class_name;
210 	/* Used as "device_type" property. */
211 	char				*adc_dev_type;
212 	/* Private storage for device driver. */
213 	void				*adc_private;
214 	/* Callback to setup environment before probing child objects. */
215 	acpidev_pre_probe_t		adc_pre_probe;
216 	/* Callback to clean environment after probing child objects. */
217 	acpidev_post_probe_t		adc_post_probe;
218 	/* Callback to probe child objects. */
219 	acpidev_probe_t			adc_probe;
220 	/* Callback to figure out policy to handle objects. */
221 	acpidev_filter_t		adc_filter;
222 	/* Callback to set device class specific device properties. */
223 	acpidev_init_t			adc_init;
224 	/* Callback to clean up resources when destroying device nodes. */
225 	acpidev_fini_t			adc_fini;
226 };
227 
228 /* Versions of the ACPI device class driver data structure. */
229 #define	ACPIDEV_CLASS_REV1		1
230 #define	ACPIDEV_CLASS_REV		ACPIDEV_CLASS_REV1
231 
232 /*
233  * Class drivers.
234  */
235 extern acpidev_class_t			acpidev_class_scope;
236 extern acpidev_class_t			acpidev_class_device;
237 extern acpidev_class_t			acpidev_class_container;
238 extern acpidev_class_t			acpidev_class_cpu;
239 extern acpidev_class_t			acpidev_class_memory;
240 
241 /*
242  * Class driver lists.
243  */
244 extern acpidev_class_list_t		*acpidev_class_list_root;
245 extern acpidev_class_list_t		*acpidev_class_list_scope;
246 extern acpidev_class_list_t		*acpidev_class_list_device;
247 extern acpidev_class_list_t		*acpidev_class_list_cpu;
248 extern acpidev_class_list_t		*acpidev_class_list_memory;
249 
250 /*
251  * Register a device class driver onto a driver list. All class drivers on the
252  * same list will be called in order when processing an ACPI object.
253  * This interface can be used to support machine/platform specific object
254  * handling by registering special plug-in class drivers to override system
255  * default behaviors.
256  * listpp:	pointer to driver list header
257  * clsp:	device class driver to register
258  * tail:	insert at tail of list if true
259  * Return values:
260  *	AE_OK: success
261  *	AE_BAD_PARAMETER: invalid parameter
262  *	AE_BAD_DATA: driver version mismatch
263  *	AE_ALREADY_EXISTS: class driver already exists on the list
264  */
265 extern ACPI_STATUS acpidev_register_class(acpidev_class_list_t **listpp,
266     acpidev_class_t *clsp, boolean_t tail);
267 
268 /*
269  * Unregister a device class driver from a driver list.
270  * listpp: pointer to driver list header
271  * clsp: device class driver to unregister
272  * Return values:
273  *	AE_OK: success
274  *	AE_BAD_PARAMETER: invalid parameter
275  *	AE_NOT_FOUND: class driver doesn't exist in list
276  *	AE_ERROR: class driver is still in use.
277  */
278 extern ACPI_STATUS acpidev_unregister_class(acpidev_class_list_t **listpp,
279     acpidev_class_t *clsp);
280 
281 /*
282  * Recursively enumerate child objects of an ACPI object.
283  * It does following things in turn:
284  * 1) Call pre_probe callback for each registered handler
285  * 2) Enumerate child objects and call probe callbacks for each object
286  * 3) Call post_probe callback for each registered handler
287  * Return AE_OK on success and error code on failure.
288  */
289 extern ACPI_STATUS acpidev_probe_child(acpidev_walk_info_t *infop);
290 
291 /*
292  * Default handler to process ACPI objects.
293  * It creates a device node for an ACPI object and scans all child objects on
294  * demand.
295  * Return values:
296  * AE_OK: on success
297  * AE_NOT_EXIST: device doesn't exist according to _STA value.
298  * AE_ALREADY_EXISTS: object already handled by other handler.
299  * AE_ERROR: on other failure
300  */
301 extern ACPI_STATUS acpidev_process_object(acpidev_walk_info_t *infop,
302     int flags);
303 
304 /* Flags for acpidev_process_device() */
305 #define	ACPIDEV_PROCESS_FLAG_CREATE	0x1	/* Create device */
306 #define	ACPIDEV_PROCESS_FLAG_SCAN	0x2	/* Scan child objects */
307 #define	ACPIDEV_PROCESS_FLAG_CHECK	0x100	/* Check status */
308 #define	ACPIDEV_PROCESS_FLAG_NOBIND	0x200	/* Skip binding driver */
309 #define	ACPIDEV_PROCESS_FLAG_OFFLINE	0x400	/* Put device into offline. */
310 #define	ACPIDEV_PROCESS_FLAG_NOTAG	0x800	/* Skip tag dip with object. */
311 #define	ACPIDEV_PROCESS_FLAG_SYNCSTATUS	0x1000	/* Sync object status. */
312 
313 /*
314  * Filter ACPI objects according to filter rules, generate devname if needed.
315  * infop:	pointer to walker information structure
316  * hdl:		handle of ACPI object in question
317  * afrp:	pointer to filter rule array
318  * entries:	number of filter rules in array
319  * devname:	buffer to store generated device name
320  * len:		sizeof devname buffer
321  */
322 extern acpidev_filter_result_t acpidev_filter_device(acpidev_walk_info_t *infop,
323     ACPI_HANDLE hdl, acpidev_filter_rule_t *afrp, int entries,
324     char *devname, int len);
325 
326 /* Default object filtering algorithm. */
327 extern acpidev_filter_result_t acpidev_filter_default(
328     acpidev_walk_info_t *infop, ACPI_HANDLE hdl, acpidev_filter_rule_t *afrp,
329     char *devname, int len);
330 
331 /* Utility routines */
332 extern dev_info_t *acpidev_root_node(void);
333 extern char *acpidev_get_object_name(ACPI_HANDLE hdl);
334 extern void acpidev_free_object_name(char *objname);
335 
336 extern acpidev_walk_info_t *acpidev_alloc_walk_info(acpidev_op_type_t op_type,
337     int lvl, ACPI_HANDLE hdl, acpidev_class_list_t **listpp,
338     acpidev_walk_info_t *pinfop);
339 extern void acpidev_free_walk_info(acpidev_walk_info_t *infop);
340 extern dev_info_t *acpidev_walk_info_get_pdip(acpidev_walk_info_t *infop);
341 
342 /* Interfaces to access data associated with ACPI object. */
343 extern acpidev_data_handle_t acpidev_data_get_handle(ACPI_HANDLE hdl);
344 extern acpidev_data_handle_t acpidev_data_create_handle(ACPI_HANDLE hdl);
345 extern void acpidev_data_destroy_handle(ACPI_HANDLE hdl);
346 extern ACPI_HANDLE acpidev_data_get_object(acpidev_data_handle_t hdl);
347 extern dev_info_t *acpidev_data_get_devinfo(acpidev_data_handle_t hdl);
348 extern int acpidev_data_get_status(acpidev_data_handle_t hdl);
349 extern void acpidev_data_set_flag(acpidev_data_handle_t hdl, uint32_t flag);
350 extern void acpidev_data_clear_flag(acpidev_data_handle_t hdl, uint32_t flag);
351 extern uint32_t acpidev_data_get_flag(acpidev_data_handle_t hdl, uint32_t flag);
352 
353 /*
354  * Try to generate meaningful device unit address from uid.
355  * Return buf on success and NULL on failure.
356  */
357 extern char *acpidev_generate_unitaddr(char *uid, char **fmts, size_t nfmt,
358     char *buf, size_t len);
359 
360 /*
361  * Set device unit address property if _UID is available or unitaddr is valid.
362  * Return AE_OK on success and error code on failure.
363  * N.B.: it returns AE_OK if _UID is unavailable and unitaddr is NULL.
364  */
365 extern ACPI_STATUS acpidev_set_unitaddr(acpidev_walk_info_t *infop,
366     char **fmts, size_t nfmt, char *unitaddr);
367 
368 /*
369  * Generate the device 'compatible' property list for a device based on:
370  *	* Device HID if available
371  *	* Device CIDs if available
372  *	* property array passed in
373  * infop:	pointer to walk information structure
374  * compat:	pointer to property array
375  * acount:	entries in property array
376  * Return AE_OK on success and error code on failure.
377  */
378 extern ACPI_STATUS acpidev_set_compatible(acpidev_walk_info_t *infop,
379     char **compat, int acount);
380 
381 /*
382  * Query ACPI device status.
383  * N.B.: it returns with all status bits set if _STA is not available.
384  */
385 extern int acpidev_query_device_status(ACPI_HANDLE hdl);
386 
387 /*
388  * Check whether device exists.
389  * Return false if device doesn't exist.
390  */
391 extern boolean_t acpidev_check_device_present(int status);
392 
393 /*
394  * Check whether device is enabled.
395  * Return false if device doesn't exist or hasn't been enabled.
396  */
397 extern boolean_t acpidev_check_device_enabled(int status);
398 
399 /*
400  * Match device ids with ACPI object's _HID and _CIDs.
401  * infop: ACPI object information structure
402  * ids: array of ACPI HIDs and CIDs
403  * count: entries in array
404  * Return TRUE if one item matches or num is zero, else FALSE.
405  */
406 extern boolean_t acpidev_match_device_id(ACPI_DEVICE_INFO *infop,
407     char **ids, int count);
408 
409 /*
410  * Implement almost the same function as AcpiGetDevices() with the following
411  * changes/enhancements:
412  * 1) Support limiting recursive levels.
413  * 2) Support matching multiple ids instead of one.
414  * 3) Report device without ACPI_STA_DEVICE_PRESENT flag which will be ignored
415  *    by AcpiGetDevices().
416  */
417 extern ACPI_STATUS acpidev_get_device_by_id(ACPI_HANDLE hdl,
418     char **ids, int count, int maxdepth, boolean_t skip_non_exist,
419     ACPI_WALK_CALLBACK userfunc, void *userarg, void** retval);
420 
421 /* Callback for APIC entry walker. */
422 typedef ACPI_STATUS (* acpidev_apic_walker_t)(ACPI_SUBTABLE_HEADER *, void *);
423 
424 /*
425  * Walk ACPI APIC entries from the first source available in following order:
426  * 1) ACPI buffer passed in if bufp isn't NULL.
427  * 2) Buffer returned by evaluating method if it isn't NULL.
428  * 3) MADT table as last resort.
429  */
430 extern ACPI_STATUS acpidev_walk_apic(ACPI_BUFFER *bufp, ACPI_HANDLE hdl,
431     char *method, acpidev_apic_walker_t func, void *context);
432 
433 #endif	/* _KERNEL */
434 
435 #ifdef __cplusplus
436 }
437 #endif
438 
439 #endif	/* _SYS_ACPIDEV_H */
440