1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /* How much nesting do we support? */
4 #define ACPIGEN_LENSTACK_SIZE 10
7 * If you need to change this, change acpigen_write_len_f and
11 #define ACPIGEN_MAXLEN 0xfffff
15 #include <acpi/acpigen.h>
17 #include <commonlib/helpers.h>
18 #include <console/console.h>
19 #include <device/device.h>
20 #include <device/soundwire.h>
23 static char *gencurrent
;
25 char *len_stack
[ACPIGEN_LENSTACK_SIZE
];
28 void acpigen_write_len_f(void)
30 ASSERT(ltop
< (ACPIGEN_LENSTACK_SIZE
- 1))
31 len_stack
[ltop
++] = gencurrent
;
37 void acpigen_pop_len(void)
41 char *p
= len_stack
[--ltop
];
43 ASSERT(len
<= ACPIGEN_MAXLEN
)
44 /* generate store length for 0xfffff max */
45 p
[0] = (0x80 | (len
& 0xf));
46 p
[1] = (len
>> 4 & 0xff);
47 p
[2] = (len
>> 12 & 0xff);
51 void acpigen_set_current(char *curr
)
56 char *acpigen_get_current(void)
61 void acpigen_emit_byte(unsigned char b
)
66 void acpigen_emit_ext_op(uint8_t op
)
68 acpigen_emit_byte(EXT_OP_PREFIX
);
69 acpigen_emit_byte(op
);
72 void acpigen_emit_word(unsigned int data
)
74 acpigen_emit_byte(data
& 0xff);
75 acpigen_emit_byte((data
>> 8) & 0xff);
78 void acpigen_emit_dword(unsigned int data
)
80 acpigen_emit_byte(data
& 0xff);
81 acpigen_emit_byte((data
>> 8) & 0xff);
82 acpigen_emit_byte((data
>> 16) & 0xff);
83 acpigen_emit_byte((data
>> 24) & 0xff);
86 char *acpigen_write_package(int nr_el
)
89 acpigen_emit_byte(PACKAGE_OP
);
90 acpigen_write_len_f();
91 p
= acpigen_get_current();
92 acpigen_emit_byte(nr_el
);
96 void acpigen_write_byte(unsigned int data
)
98 acpigen_emit_byte(BYTE_PREFIX
);
99 acpigen_emit_byte(data
& 0xff);
102 void acpigen_write_word(unsigned int data
)
104 acpigen_emit_byte(WORD_PREFIX
);
105 acpigen_emit_word(data
);
108 void acpigen_write_dword(unsigned int data
)
110 acpigen_emit_byte(DWORD_PREFIX
);
111 acpigen_emit_dword(data
);
114 void acpigen_write_qword(uint64_t data
)
116 acpigen_emit_byte(QWORD_PREFIX
);
117 acpigen_emit_dword(data
& 0xffffffff);
118 acpigen_emit_dword((data
>> 32) & 0xffffffff);
121 void acpigen_write_zero(void)
123 acpigen_emit_byte(ZERO_OP
);
126 void acpigen_write_one(void)
128 acpigen_emit_byte(ONE_OP
);
131 void acpigen_write_ones(void)
133 acpigen_emit_byte(ONES_OP
);
136 void acpigen_write_integer(uint64_t data
)
139 acpigen_write_zero();
142 else if (data
<= 0xff)
143 acpigen_write_byte((unsigned char)data
);
144 else if (data
<= 0xffff)
145 acpigen_write_word((unsigned int)data
);
146 else if (data
<= 0xffffffff)
147 acpigen_write_dword((unsigned int)data
);
149 acpigen_write_qword(data
);
152 void acpigen_write_name_byte(const char *name
, uint8_t val
)
154 acpigen_write_name(name
);
155 acpigen_write_byte(val
);
158 void acpigen_write_name_dword(const char *name
, uint32_t val
)
160 acpigen_write_name(name
);
161 acpigen_write_dword(val
);
164 void acpigen_write_name_qword(const char *name
, uint64_t val
)
166 acpigen_write_name(name
);
167 acpigen_write_qword(val
);
170 void acpigen_write_name_integer(const char *name
, uint64_t val
)
172 acpigen_write_name(name
);
173 acpigen_write_integer(val
);
176 void acpigen_write_name_string(const char *name
, const char *string
)
178 acpigen_write_name(name
);
179 acpigen_write_string(string
);
182 void acpigen_write_name_unicode(const char *name
, const char *string
)
184 const size_t len
= strlen(string
) + 1;
185 acpigen_write_name(name
);
186 acpigen_emit_byte(BUFFER_OP
);
187 acpigen_write_len_f();
188 acpigen_write_integer(len
);
189 for (size_t i
= 0; i
< len
; i
++) {
190 const char c
= string
[i
];
191 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
192 acpigen_emit_word(c
>= 0 ? c
: '?');
197 void acpigen_emit_stream(const char *data
, int size
)
200 for (i
= 0; i
< size
; i
++)
201 acpigen_emit_byte(data
[i
]);
204 void acpigen_emit_string(const char *string
)
206 acpigen_emit_stream(string
, string
? strlen(string
) : 0);
207 acpigen_emit_byte('\0'); /* NUL */
210 void acpigen_write_string(const char *string
)
212 acpigen_emit_byte(STRING_PREFIX
);
213 acpigen_emit_string(string
);
216 void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id
)
218 char hid
[9]; /* BOOTxxxx */
220 snprintf(hid
, sizeof(hid
), "%.4s%04X", COREBOOT_ACPI_ID
, id
);
221 acpigen_write_name_string("_HID", hid
);
225 * The naming conventions for ACPI namespace names are a bit tricky as
226 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
227 * and "By convention, when an ASL compiler pads a name shorter than 4
228 * characters, it is done so with trailing underscores ('_')".
230 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
233 static void acpigen_emit_simple_namestring(const char *name
)
237 for (i
= 0; i
< 4; i
++) {
238 if ((name
[i
] == '\0') || (name
[i
] == '.')) {
239 acpigen_emit_stream(ud
, 4 - i
);
242 acpigen_emit_byte(name
[i
]);
246 static void acpigen_emit_double_namestring(const char *name
, int dotpos
)
248 acpigen_emit_byte(DUAL_NAME_PREFIX
);
249 acpigen_emit_simple_namestring(name
);
250 acpigen_emit_simple_namestring(&name
[dotpos
+ 1]);
253 static void acpigen_emit_multi_namestring(const char *name
)
256 unsigned char *pathlen
;
257 acpigen_emit_byte(MULTI_NAME_PREFIX
);
258 acpigen_emit_byte(ZERO_OP
);
259 pathlen
= ((unsigned char *)acpigen_get_current()) - 1;
261 while (name
[0] != '\0') {
262 acpigen_emit_simple_namestring(name
);
263 /* find end or next entity */
264 while ((name
[0] != '.') && (name
[0] != '\0'))
266 /* forward to next */
275 void acpigen_emit_namestring(const char *namepath
)
280 /* Check for NULL pointer */
284 /* We can start with a '\'. */
285 if (namepath
[0] == '\\') {
286 acpigen_emit_byte('\\');
290 /* And there can be any number of '^' */
291 while (namepath
[0] == '^') {
292 acpigen_emit_byte('^');
296 /* If we have only \\ or only ^...^. Then we need to put a null
298 if (namepath
[0] == '\0') {
299 acpigen_emit_byte(ZERO_OP
);
304 while (namepath
[i
] != '\0') {
305 if (namepath
[i
] == '.') {
313 acpigen_emit_simple_namestring(namepath
);
314 else if (dotcount
== 1)
315 acpigen_emit_double_namestring(namepath
, dotpos
);
317 acpigen_emit_multi_namestring(namepath
);
320 void acpigen_write_name(const char *name
)
322 acpigen_emit_byte(NAME_OP
);
323 acpigen_emit_namestring(name
);
326 void acpigen_write_scope(const char *name
)
328 acpigen_emit_byte(SCOPE_OP
);
329 acpigen_write_len_f();
330 acpigen_emit_namestring(name
);
333 void acpigen_get_package_op_element(uint8_t package_op
, unsigned int element
, uint8_t dest_op
)
335 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
336 acpigen_write_store();
337 acpigen_emit_byte(DEREF_OP
);
338 acpigen_emit_byte(INDEX_OP
);
339 acpigen_emit_byte(package_op
);
340 acpigen_write_integer(element
);
341 acpigen_emit_byte(ZERO_OP
); /* Ignore Index() Destination */
342 acpigen_emit_byte(dest_op
);
345 void acpigen_set_package_op_element_int(uint8_t package_op
, unsigned int element
, uint64_t src
)
347 /* DeRefOf (<package>[<element>]) = <src> */
348 acpigen_write_store();
349 acpigen_write_integer(src
);
350 acpigen_emit_byte(DEREF_OP
);
351 acpigen_emit_byte(INDEX_OP
);
352 acpigen_emit_byte(package_op
);
353 acpigen_write_integer(element
);
354 acpigen_emit_byte(ZERO_OP
); /* Ignore Index() Destination */
357 void acpigen_get_package_element(const char *package
, unsigned int element
, uint8_t dest_op
)
359 /* <dest_op> = <package>[<element>] */
360 acpigen_write_store();
361 acpigen_emit_byte(INDEX_OP
);
362 acpigen_emit_namestring(package
);
363 acpigen_write_integer(element
);
364 acpigen_emit_byte(ZERO_OP
); /* Ignore Index() Destination */
365 acpigen_emit_byte(dest_op
);
368 void acpigen_set_package_element_int(const char *package
, unsigned int element
, uint64_t src
)
370 /* <package>[<element>] = <src> */
371 acpigen_write_store();
372 acpigen_write_integer(src
);
373 acpigen_emit_byte(INDEX_OP
);
374 acpigen_emit_namestring(package
);
375 acpigen_write_integer(element
);
376 acpigen_emit_byte(ZERO_OP
); /* Ignore Index() Destination */
379 void acpigen_set_package_element_namestr(const char *package
, unsigned int element
,
382 /* <package>[<element>] = <src> */
383 acpigen_write_store();
384 acpigen_emit_namestring(src
);
385 acpigen_emit_byte(INDEX_OP
);
386 acpigen_emit_namestring(package
);
387 acpigen_write_integer(element
);
388 acpigen_emit_byte(ZERO_OP
); /* Ignore Index() Destination */
391 void acpigen_write_processor_namestring(unsigned int cpu_index
)
394 snprintf(buffer
, sizeof(buffer
), CONFIG_ACPI_CPU_STRING
, cpu_index
);
395 acpigen_emit_namestring(buffer
);
398 /* Processor() operator is deprecated as of ACPI 6.0, use Device() instead. */
399 void acpigen_write_processor(u8 cpuindex
, u32 pblock_addr
, u8 pblock_len
)
402 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
405 acpigen_emit_ext_op(PROCESSOR_OP
);
406 acpigen_write_len_f();
407 acpigen_write_processor_namestring(cpuindex
);
408 acpigen_emit_byte(cpuindex
);
409 acpigen_emit_dword(pblock_addr
);
410 acpigen_emit_byte(pblock_len
);
413 void acpigen_write_processor_device(unsigned int cpu_index
)
415 acpigen_emit_ext_op(DEVICE_OP
);
416 acpigen_write_len_f();
417 acpigen_write_processor_namestring(cpu_index
);
418 acpigen_write_name_string("_HID", "ACPI0007");
419 acpigen_write_name_integer("_UID", cpu_index
);
422 void acpigen_write_processor_package(const char *const name
, const unsigned int first_core
,
423 const unsigned int core_count
)
427 acpigen_write_name(name
);
428 acpigen_write_package(core_count
);
430 for (i
= first_core
; i
< first_core
+ core_count
; ++i
)
431 acpigen_write_processor_namestring(i
);
436 /* Method to notify all CPU cores */
437 void acpigen_write_processor_cnot(const unsigned int number_of_cores
)
441 acpigen_write_method("\\_SB.CNOT", 1);
442 for (core_id
= 0; core_id
< number_of_cores
; core_id
++) {
443 acpigen_emit_byte(NOTIFY_OP
);
444 acpigen_write_processor_namestring(core_id
);
445 acpigen_emit_byte(ARG0_OP
);
451 * Generate ACPI AML code for OperationRegion
452 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
453 * where rname is region name, space is region space, offset is region offset &
454 * len is region length.
455 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
457 void acpigen_write_opregion(const struct opregion
*opreg
)
460 acpigen_emit_ext_op(OPREGION_OP
);
461 /* NameString 4 chars only */
462 acpigen_emit_simple_namestring(opreg
->name
);
464 acpigen_emit_byte(opreg
->regionspace
);
465 /* RegionOffset & RegionLen, it can be byte word or double word */
466 acpigen_write_integer(opreg
->regionoffset
);
467 acpigen_write_integer(opreg
->regionlen
);
471 * Generate ACPI AML code for Mutex
472 * Arg0: Pointer to name of mutex
473 * Arg1: Initial value of mutex
475 void acpigen_write_mutex(const char *name
, const uint8_t flags
)
478 acpigen_emit_ext_op(MUTEX_OP
);
479 /* NameString 4 chars only */
480 acpigen_emit_simple_namestring(name
);
481 acpigen_emit_byte(flags
);
484 void acpigen_write_acquire(const char *name
, const uint16_t val
)
487 acpigen_emit_ext_op(ACQUIRE_OP
);
488 /* NameString 4 chars only */
489 acpigen_emit_simple_namestring(name
);
490 acpigen_emit_word(val
);
493 void acpigen_write_release(const char *name
)
496 acpigen_emit_ext_op(RELEASE_OP
);
497 /* NameString 4 chars only */
498 acpigen_emit_simple_namestring(name
);
501 static void acpigen_write_field_length(uint32_t len
)
508 emit
[0] = len
& 0x3F;
513 emit
[i
] = len
& 0xFF;
518 /* Update bit 7:6 : Number of bytes followed by emit[0] */
519 emit
[0] |= (i
- 1) << 6;
521 for (j
= 0; j
< i
; j
++)
522 acpigen_emit_byte(emit
[j
]);
525 static void acpigen_write_field_offset(uint32_t offset
, uint32_t current_bit_pos
)
529 if (offset
< current_bit_pos
) {
530 printk(BIOS_WARNING
, "%s: Cannot move offset backward", __func__
);
534 diff_bits
= offset
- current_bit_pos
;
536 if (diff_bits
> 0xFFFFFFF) {
537 printk(BIOS_WARNING
, "%s: Offset very large to encode", __func__
);
541 acpigen_emit_byte(0);
542 acpigen_write_field_length(diff_bits
);
545 void acpigen_write_field_name(const char *name
, uint32_t size
)
547 acpigen_emit_simple_namestring(name
);
548 acpigen_write_field_length(size
);
551 static void acpigen_write_field_reserved(uint32_t size
)
553 acpigen_emit_byte(0);
554 acpigen_write_field_length(size
);
558 * Generate ACPI AML code for Field
560 * Arg1: Pointer to struct fieldlist.
561 * Arg2: no. of entries in Arg1
562 * Arg3: flags which indicate filed access type, lock rule & update rule.
563 * Example with fieldlist
564 * struct fieldlist l[] = {
565 * FIELDLIST_OFFSET(0x84),
566 * FIELDLIST_NAMESTR("PMCS", 2),
567 * FIELDLIST_RESERVED(6),
569 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
572 * Field (UART, AnyAcc, NoLock, Preserve)
579 void acpigen_write_field(const char *name
, const struct fieldlist
*l
, size_t count
,
583 uint32_t current_bit_pos
= 0;
586 acpigen_emit_ext_op(FIELD_OP
);
588 acpigen_write_len_f();
589 /* NameString 4 chars only */
590 acpigen_emit_simple_namestring(name
);
592 acpigen_emit_byte(flags
);
594 for (i
= 0; i
< count
; i
++) {
597 acpigen_write_field_name(l
[i
].name
, l
[i
].bits
);
598 current_bit_pos
+= l
[i
].bits
;
601 acpigen_write_field_reserved(l
[i
].bits
);
602 current_bit_pos
+= l
[i
].bits
;
605 acpigen_write_field_offset(l
[i
].bits
, current_bit_pos
);
606 current_bit_pos
= l
[i
].bits
;
609 printk(BIOS_ERR
, "%s: Invalid field type 0x%X\n", __func__
, l
[i
].type
);
617 * Generate ACPI AML code for IndexField
619 * Arg1: Pointer to struct fieldlist.
620 * Arg2: no. of entries in Arg1
621 * Arg3: flags which indicate filed access type, lock rule & update rule.
622 * Example with fieldlist
623 * struct fieldlist l[] = {
624 * FIELDLIST_OFFSET(0x84),
625 * FIELDLIST_NAMESTR("PMCS", 2),
627 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
631 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
637 void acpigen_write_indexfield(const char *idx
, const char *data
, struct fieldlist
*l
,
638 size_t count
, uint8_t flags
)
641 uint32_t current_bit_pos
= 0;
644 acpigen_emit_ext_op(INDEX_FIELD_OP
);
646 acpigen_write_len_f();
647 /* NameString 4 chars only */
648 acpigen_emit_simple_namestring(idx
);
649 /* NameString 4 chars only */
650 acpigen_emit_simple_namestring(data
);
652 acpigen_emit_byte(flags
);
654 for (i
= 0; i
< count
; i
++) {
657 acpigen_write_field_name(l
[i
].name
, l
[i
].bits
);
658 current_bit_pos
+= l
[i
].bits
;
661 acpigen_write_field_offset(l
[i
].bits
, current_bit_pos
);
662 current_bit_pos
= l
[i
].bits
;
665 printk(BIOS_ERR
, "%s: Invalid field type 0x%X\n", __func__
, l
[i
].type
);
672 void acpigen_write_empty_PCT(void)
675 Name (_PCT, Package (0x02)
682 0x0000000000000000, // Address
691 0x0000000000000000, // Address
696 static char stream
[] = {
697 /* 00000030 "0._PCT.," */
698 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
699 /* 00000038 "........" */
700 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
701 /* 00000040 "........" */
702 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
703 /* 00000048 "....y..." */
704 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
705 /* 00000050 "........" */
706 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
707 /* 00000058 "........" */
708 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
711 acpigen_emit_stream(stream
, ARRAY_SIZE(stream
));
714 void acpigen_write_empty_PTC(void)
717 Name (_PTC, Package (0x02)
724 0x0000000000000000, // Address
733 0x0000000000000000, // Address
739 .space_id
= ACPI_ADDRESS_SPACE_FIXED
,
742 .access_size
= ACPI_ACCESS_SIZE_UNDEFINED
,
747 acpigen_write_name("_PTC");
748 acpigen_write_package(2);
750 /* ControlRegister */
751 acpigen_write_register_resource(&addr
);
754 acpigen_write_register_resource(&addr
);
759 static void __acpigen_write_method(const char *name
, uint8_t flags
)
761 acpigen_emit_byte(METHOD_OP
);
762 acpigen_write_len_f();
763 acpigen_emit_namestring(name
);
764 acpigen_emit_byte(flags
);
767 /* Method (name, nargs, NotSerialized) */
768 void acpigen_write_method(const char *name
, int nargs
)
770 __acpigen_write_method(name
, (nargs
& 7));
773 /* Method (name, nargs, Serialized) */
774 void acpigen_write_method_serialized(const char *name
, int nargs
)
776 __acpigen_write_method(name
, (nargs
& 7) | (1 << 3));
779 void acpigen_write_device(const char *name
)
781 acpigen_emit_ext_op(DEVICE_OP
);
782 acpigen_write_len_f();
783 acpigen_emit_namestring(name
);
786 void acpigen_write_thermal_zone(const char *name
)
788 acpigen_emit_ext_op(THERMAL_ZONE_OP
);
789 acpigen_write_len_f();
790 acpigen_emit_namestring(name
);
793 void acpigen_write_STA(uint8_t status
)
796 * Method (_STA, 0, NotSerialized) { Return (status) }
798 acpigen_write_method("_STA", 0);
799 acpigen_emit_byte(RETURN_OP
);
800 acpigen_write_byte(status
);
804 void acpigen_write_STA_ext(const char *namestring
)
807 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
809 acpigen_write_method("_STA", 0);
810 acpigen_emit_byte(RETURN_OP
);
811 acpigen_emit_namestring(namestring
);
815 void acpigen_write_LPI_package(u64 level
, const struct acpi_lpi_state
*states
, u16 nentries
)
818 * Name (_LPI, Package (0x06) // _LPI: Low Power Idle States
821 * 0x0000000000000000,
831 * ResourceTemplate ()
833 * Register (FFixedHW,
835 * 0x02, // Bit Offset
836 * 0x0000000000000000, // Address
840 * ResourceTemplate ()
842 * Register (SystemMemory,
844 * 0x00, // Bit Offset
845 * 0x0000000000000000, // Address
849 * ResourceTemplate ()
851 * Register (SystemMemory,
853 * 0x00, // Bit Offset
854 * 0x0000000000000000, // Address
864 acpigen_write_name("_LPI");
865 acpigen_write_package(3 + nentries
);
866 acpigen_write_word(0); /* Revision */
867 acpigen_write_qword(level
);
868 acpigen_write_word(nentries
);
870 for (size_t i
= 0; i
< nentries
; i
++, states
++) {
871 acpigen_write_package(0xA);
872 acpigen_write_dword(states
->min_residency_us
);
873 acpigen_write_dword(states
->worst_case_wakeup_latency_us
);
874 acpigen_write_dword(states
->flags
);
875 acpigen_write_dword(states
->arch_context_lost_flags
);
876 acpigen_write_dword(states
->residency_counter_frequency_hz
);
877 acpigen_write_dword(states
->enabled_parent_state
);
878 acpigen_write_register_resource(&states
->entry_method
);
879 acpigen_write_register_resource(&states
->residency_counter_register
);
880 acpigen_write_register_resource(&states
->usage_counter_register
);
881 acpigen_write_string(states
->state_name
);
888 * Generates a func with max supported P-states.
890 void acpigen_write_PPC(u8 nr
)
893 Method (_PPC, 0, NotSerialized)
898 acpigen_write_method("_PPC", 0);
899 acpigen_emit_byte(RETURN_OP
);
901 acpigen_write_byte(nr
);
906 * Generates a func with max supported P-states saved
907 * in the variable PPCM.
909 void acpigen_write_PPC_NVS(void)
912 Method (_PPC, 0, NotSerialized)
917 acpigen_write_method("_PPC", 0);
918 acpigen_emit_byte(RETURN_OP
);
920 acpigen_emit_namestring("PPCM");
924 void acpigen_write_TPC(const char *gnvs_tpc_limit
)
927 // Sample _TPC method
928 Method (_TPC, 0, NotSerialized)
933 acpigen_write_method("_TPC", 0);
934 acpigen_emit_byte(RETURN_OP
);
935 acpigen_emit_namestring(gnvs_tpc_limit
);
939 void acpigen_write_PRW(u32 wake
, u32 level
)
942 * Name (_PRW, Package () { wake, level }
944 acpigen_write_name("_PRW");
945 acpigen_write_package(2);
946 acpigen_write_integer(wake
);
947 acpigen_write_integer(level
);
951 void acpigen_write_PSS_package(u32 coreFreq
, u32 power
, u32 transLat
, u32 busmLat
, u32 control
,
954 acpigen_write_package(6);
955 acpigen_write_dword(coreFreq
);
956 acpigen_write_dword(power
);
957 acpigen_write_dword(transLat
);
958 acpigen_write_dword(busmLat
);
959 acpigen_write_dword(control
);
960 acpigen_write_dword(status
);
963 printk(BIOS_DEBUG
, "PSS: %uMHz power %u control 0x%x status 0x%x\n", coreFreq
, power
,
967 void acpigen_write_pss_object(const struct acpi_sw_pstate
*pstate_values
, size_t nentries
)
971 acpigen_write_name("_PSS");
972 acpigen_write_package(nentries
);
973 for (pstate
= 0; pstate
< nentries
; pstate
++) {
974 acpigen_write_PSS_package(
975 pstate_values
->core_freq
, pstate_values
->power
,
976 pstate_values
->transition_latency
, pstate_values
->bus_master_latency
,
977 pstate_values
->control_value
, pstate_values
->status_value
);
984 void acpigen_write_PSD_package(u32 domain
, u32 numprocs
, PSD_coord coordtype
)
986 acpigen_write_name("_PSD");
987 acpigen_write_package(1);
988 acpigen_write_package(5);
989 acpigen_write_byte(5); // 5 values
990 acpigen_write_byte(0); // revision 0
991 acpigen_write_dword(domain
);
992 acpigen_write_dword(coordtype
);
993 acpigen_write_dword(numprocs
);
998 void acpigen_write_CST_package_entry(const acpi_cstate_t
*cstate
)
1000 acpigen_write_package(4);
1001 acpigen_write_register_resource(&cstate
->resource
);
1002 acpigen_write_byte(cstate
->ctype
);
1003 acpigen_write_word(cstate
->latency
);
1004 acpigen_write_dword(cstate
->power
);
1008 void acpigen_write_CST_package(const acpi_cstate_t
*cstate
, int nentries
)
1011 acpigen_write_name("_CST");
1012 acpigen_write_package(nentries
+1);
1013 acpigen_write_integer(nentries
);
1015 for (i
= 0; i
< nentries
; i
++)
1016 acpigen_write_CST_package_entry(cstate
+ i
);
1021 void acpigen_write_CSD_package(u32 domain
, u32 numprocs
, CSD_coord coordtype
,
1024 acpigen_write_name("_CSD");
1025 acpigen_write_package(1);
1026 acpigen_write_package(6);
1027 acpigen_write_integer(6); // 6 values
1028 acpigen_write_byte(0); // revision 0
1029 acpigen_write_dword(domain
);
1030 acpigen_write_dword(coordtype
);
1031 acpigen_write_dword(numprocs
);
1032 acpigen_write_dword(index
);
1037 void acpigen_write_TSS_package(int entries
, acpi_tstate_t
*tstate_list
)
1040 Sample _TSS package with 100% and 50% duty cycles
1041 Name (_TSS, Package (0x02)
1043 Package(){100, 1000, 0, 0x00, 0)
1044 Package(){50, 520, 0, 0x18, 0)
1048 acpi_tstate_t
*tstate
= tstate_list
;
1050 acpigen_write_name("_TSS");
1051 acpigen_write_package(entries
);
1053 for (i
= 0; i
< entries
; i
++) {
1054 acpigen_write_package(5);
1055 acpigen_write_dword(tstate
->percent
);
1056 acpigen_write_dword(tstate
->power
);
1057 acpigen_write_dword(tstate
->latency
);
1058 acpigen_write_dword(tstate
->control
);
1059 acpigen_write_dword(tstate
->status
);
1067 void acpigen_write_TSD_package(u32 domain
, u32 numprocs
, PSD_coord coordtype
)
1069 acpigen_write_name("_TSD");
1070 acpigen_write_package(1);
1071 acpigen_write_package(5);
1072 acpigen_write_byte(5); // 5 values
1073 acpigen_write_byte(0); // revision 0
1074 acpigen_write_dword(domain
);
1075 acpigen_write_dword(coordtype
);
1076 acpigen_write_dword(numprocs
);
1081 void acpigen_write_mem32fixed(int readwrite
, u32 base
, u32 size
)
1084 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
1086 * Bit7 : 1 => big item
1087 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1089 acpigen_emit_byte(0x86);
1090 /* Byte 1+2: length (0x0009) */
1091 acpigen_emit_byte(0x09);
1092 acpigen_emit_byte(0x00);
1093 /* bit1-7 are ignored */
1094 acpigen_emit_byte(readwrite
? 0x01 : 0x00);
1095 acpigen_emit_dword(base
);
1096 acpigen_emit_dword(size
);
1099 static void acpigen_write_register(const acpi_addr_t
*addr
)
1101 acpigen_emit_byte(0x82); /* Register Descriptor */
1102 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1103 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1104 acpigen_emit_byte(addr
->space_id
); /* Address Space ID */
1105 acpigen_emit_byte(addr
->bit_width
); /* Register Bit Width */
1106 acpigen_emit_byte(addr
->bit_offset
); /* Register Bit Offset */
1107 acpigen_emit_byte(addr
->access_size
); /* Register Access Size */
1108 acpigen_emit_dword(addr
->addrl
); /* Register Address Low */
1109 acpigen_emit_dword(addr
->addrh
); /* Register Address High */
1112 void acpigen_write_register_resource(const acpi_addr_t
*addr
)
1114 acpigen_write_resourcetemplate_header();
1115 acpigen_write_register(addr
);
1116 acpigen_write_resourcetemplate_footer();
1119 void acpigen_write_irq(u16 mask
)
1122 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
1124 * Bit7 : 0 => small item
1125 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1126 * Bit2-0: 010 (0x2) => 2 Bytes long
1128 acpigen_emit_byte(0x22);
1129 acpigen_emit_byte(mask
& 0xff);
1130 acpigen_emit_byte((mask
>> 8) & 0xff);
1133 void acpigen_write_io16(u16 min
, u16 max
, u8 align
, u8 len
, u8 decode16
)
1136 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
1138 * Bit7 : 0 => small item
1139 * Bit6-3: 1000 (0x8) => I/O port descriptor
1140 * Bit2-0: 111 (0x7) => 7 Bytes long
1142 acpigen_emit_byte(0x47);
1143 /* Does the device decode all 16 or just 10 bits? */
1144 /* bit1-7 are ignored */
1145 acpigen_emit_byte(decode16
? 0x01 : 0x00);
1146 /* minimum base address the device may be configured for */
1147 acpigen_emit_byte(min
& 0xff);
1148 acpigen_emit_byte((min
>> 8) & 0xff);
1149 /* maximum base address the device may be configured for */
1150 acpigen_emit_byte(max
& 0xff);
1151 acpigen_emit_byte((max
>> 8) & 0xff);
1152 /* alignment for min base */
1153 acpigen_emit_byte(align
& 0xff);
1154 acpigen_emit_byte(len
& 0xff);
1157 void acpigen_write_resourcetemplate_header(void)
1160 * A ResourceTemplate() is a Buffer() with a
1161 * (Byte|Word|DWord) containing the length, followed by one or more
1162 * resource items, terminated by the end tag.
1163 * (small item 0xf, len 1)
1165 acpigen_emit_byte(BUFFER_OP
);
1166 acpigen_write_len_f();
1167 acpigen_emit_byte(WORD_PREFIX
);
1168 len_stack
[ltop
++] = acpigen_get_current();
1169 /* Add 2 dummy bytes for the ACPI word (keep aligned with
1170 the calculation in acpigen_write_resourcetemplate() below). */
1171 acpigen_emit_byte(0x00);
1172 acpigen_emit_byte(0x00);
1175 void acpigen_write_resourcetemplate_footer(void)
1177 char *p
= len_stack
[--ltop
];
1180 * end tag (acpi 4.0 Section 6.4.2.8)
1182 * 0x00 is treated as a good checksum according to the spec
1183 * and is what iasl generates.
1185 acpigen_emit_byte(0x79);
1186 acpigen_emit_byte(0x00);
1188 /* Start counting past the 2-bytes length added in
1189 acpigen_write_resourcetemplate() above. */
1190 len
= acpigen_get_current() - (p
+ 2);
1192 /* patch len word */
1194 p
[1] = (len
>> 8) & 0xff;
1195 /* patch len field */
1199 static void acpigen_add_mainboard_rsvd_mem32(void *gp
, struct device
*dev
, struct resource
*res
)
1201 acpigen_write_mem32fixed(0, res
->base
, res
->size
);
1204 static void acpigen_add_mainboard_rsvd_io(void *gp
, struct device
*dev
, struct resource
*res
)
1206 resource_t base
= res
->base
;
1207 resource_t size
= res
->size
;
1209 resource_t sz
= size
> 255 ? 255 : size
;
1210 acpigen_write_io16(base
, base
, 0, sz
, 1);
1216 void acpigen_write_mainboard_resource_template(void)
1218 acpigen_write_resourcetemplate_header();
1220 /* Add reserved memory ranges. */
1221 search_global_resources(
1222 IORESOURCE_MEM
| IORESOURCE_RESERVE
,
1223 IORESOURCE_MEM
| IORESOURCE_RESERVE
,
1224 acpigen_add_mainboard_rsvd_mem32
, 0);
1226 /* Add reserved io ranges. */
1227 search_global_resources(
1228 IORESOURCE_IO
| IORESOURCE_RESERVE
,
1229 IORESOURCE_IO
| IORESOURCE_RESERVE
,
1230 acpigen_add_mainboard_rsvd_io
, 0);
1232 acpigen_write_resourcetemplate_footer();
1235 void acpigen_write_mainboard_resources(const char *scope
, const char *name
)
1237 acpigen_write_scope(scope
);
1238 acpigen_write_name(name
);
1239 acpigen_write_mainboard_resource_template();
1243 static int hex2bin(const char c
)
1245 if (c
>= 'A' && c
<= 'F')
1246 return c
- 'A' + 10;
1247 if (c
>= 'a' && c
<= 'f')
1248 return c
- 'a' + 10;
1252 void acpigen_emit_eisaid(const char *eisaid
)
1256 /* Clamping individual values would be better but
1257 there is a disagreement over what is a valid
1258 EISA id, so accept anything and don't clamp,
1259 parent code should create a valid EISAid.
1261 compact
|= (eisaid
[0] - 'A' + 1) << 26;
1262 compact
|= (eisaid
[1] - 'A' + 1) << 21;
1263 compact
|= (eisaid
[2] - 'A' + 1) << 16;
1264 compact
|= hex2bin(eisaid
[3]) << 12;
1265 compact
|= hex2bin(eisaid
[4]) << 8;
1266 compact
|= hex2bin(eisaid
[5]) << 4;
1267 compact
|= hex2bin(eisaid
[6]);
1269 acpigen_emit_byte(0xc);
1270 acpigen_emit_byte((compact
>> 24) & 0xff);
1271 acpigen_emit_byte((compact
>> 16) & 0xff);
1272 acpigen_emit_byte((compact
>> 8) & 0xff);
1273 acpigen_emit_byte(compact
& 0xff);
1279 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1280 * order for the bytes that make up a UUID Buffer object.
1281 * UUID byte order for input:
1282 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1283 * UUID byte order for output:
1284 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1287 void acpigen_write_uuid(const char *uuid
)
1289 uint8_t buf
[UUID_LEN
];
1290 size_t i
, order
[UUID_LEN
] = { 3, 2, 1, 0, 5, 4, 7, 6,
1291 8, 9, 10, 11, 12, 13, 14, 15 };
1293 /* Parse UUID string into bytes */
1294 if (hexstrtobin(uuid
, buf
, UUID_LEN
) < UUID_LEN
)
1298 acpigen_emit_byte(BUFFER_OP
);
1299 acpigen_write_len_f();
1301 /* Buffer length in bytes */
1302 acpigen_write_word(UUID_LEN
);
1304 /* Output UUID in expected order */
1305 for (i
= 0; i
< UUID_LEN
; i
++)
1306 acpigen_emit_byte(buf
[order
[i
]]);
1312 * Name (_PRx, Package(One) { name })
1314 * PowerResource (name, level, order)
1316 void acpigen_write_power_res(const char *name
, uint8_t level
, uint16_t order
,
1317 const char * const dev_states
[], size_t dev_states_count
)
1320 for (i
= 0; i
< dev_states_count
; i
++) {
1321 acpigen_write_name(dev_states
[i
]);
1322 acpigen_write_package(1);
1323 acpigen_emit_simple_namestring(name
);
1324 acpigen_pop_len(); /* Package */
1327 acpigen_emit_ext_op(POWER_RES_OP
);
1329 acpigen_write_len_f();
1331 acpigen_emit_simple_namestring(name
);
1332 acpigen_emit_byte(level
);
1333 acpigen_emit_word(order
);
1337 void acpigen_write_sleep(uint64_t sleep_ms
)
1339 acpigen_emit_ext_op(SLEEP_OP
);
1340 acpigen_write_integer(sleep_ms
);
1343 void acpigen_write_store(void)
1345 acpigen_emit_byte(STORE_OP
);
1348 /* Store (src, dst) */
1349 void acpigen_write_store_ops(uint8_t src
, uint8_t dst
)
1351 acpigen_write_store();
1352 acpigen_emit_byte(src
);
1353 acpigen_emit_byte(dst
);
1356 /* Store (src, "namestr") */
1357 void acpigen_write_store_op_to_namestr(uint8_t src
, const char *dst
)
1359 acpigen_write_store();
1360 acpigen_emit_byte(src
);
1361 acpigen_emit_namestring(dst
);
1364 /* Store (src, "namestr") */
1365 void acpigen_write_store_int_to_namestr(uint64_t src
, const char *dst
)
1367 acpigen_write_store();
1368 acpigen_write_integer(src
);
1369 acpigen_emit_namestring(dst
);
1372 /* Store ("namestr", dst) */
1373 void acpigen_write_store_namestr_to_op(const char *src
, uint8_t dst
)
1375 acpigen_write_store();
1376 acpigen_emit_namestring(src
);
1377 acpigen_emit_byte(dst
);
1380 /* Store (src, dst) */
1381 void acpigen_write_store_int_to_op(uint64_t src
, uint8_t dst
)
1383 acpigen_write_store();
1384 acpigen_write_integer(src
);
1385 acpigen_emit_byte(dst
);
1388 /* Store ("namestr", "namestr") */
1389 void acpigen_write_store_namestr_to_namestr(const char *src
, const char *dst
)
1391 acpigen_write_store();
1392 acpigen_emit_namestring(src
);
1393 acpigen_emit_namestring(dst
);
1396 /* Or (arg1, arg2, res) */
1397 void acpigen_write_or(uint8_t arg1
, uint8_t arg2
, uint8_t res
)
1399 acpigen_emit_byte(OR_OP
);
1400 acpigen_emit_byte(arg1
);
1401 acpigen_emit_byte(arg2
);
1402 acpigen_emit_byte(res
);
1405 /* Xor (arg1, arg2, res) */
1406 void acpigen_write_xor(uint8_t arg1
, uint8_t arg2
, uint8_t res
)
1408 acpigen_emit_byte(XOR_OP
);
1409 acpigen_emit_byte(arg1
);
1410 acpigen_emit_byte(arg2
);
1411 acpigen_emit_byte(res
);
1414 /* And (arg1, arg2, res) */
1415 void acpigen_write_and(uint8_t arg1
, uint8_t arg2
, uint8_t res
)
1417 acpigen_emit_byte(AND_OP
);
1418 acpigen_emit_byte(arg1
);
1419 acpigen_emit_byte(arg2
);
1420 acpigen_emit_byte(res
);
1423 /* Not (arg, res) */
1424 void acpigen_write_not(uint8_t arg
, uint8_t res
)
1426 acpigen_emit_byte(NOT_OP
);
1427 acpigen_emit_byte(arg
);
1428 acpigen_emit_byte(res
);
1431 /* Concatenate (str1, str2, res) */
1432 void acpigen_concatenate_string_string(const char *str1
, const char *str2
, uint8_t res
)
1434 acpigen_emit_byte(CONCATENATE_OP
);
1435 acpigen_write_string(str1
);
1436 acpigen_write_string(str2
);
1437 acpigen_emit_byte(res
);
1440 /* Concatenate (str, val, tmp_res) */
1441 void acpigen_concatenate_string_int(const char *str
, uint64_t val
, uint8_t res
)
1443 acpigen_emit_byte(CONCATENATE_OP
);
1444 acpigen_write_string(str
);
1445 acpigen_write_integer(val
);
1446 acpigen_emit_byte(res
);
1449 /* Concatenate (str, src_res, dest_res) */
1450 void acpigen_concatenate_string_op(const char *str
, uint8_t src_res
, uint8_t dest_res
)
1452 acpigen_emit_byte(CONCATENATE_OP
);
1453 acpigen_write_string(str
);
1454 acpigen_emit_byte(src_res
);
1455 acpigen_emit_byte(dest_res
);
1458 /* Store (str, DEBUG) */
1459 void acpigen_write_debug_string(const char *str
)
1461 acpigen_write_store();
1462 acpigen_write_string(str
);
1463 acpigen_emit_ext_op(DEBUG_OP
);
1466 /* Store (val, DEBUG) */
1467 void acpigen_write_debug_integer(uint64_t val
)
1469 acpigen_write_store();
1470 acpigen_write_integer(val
);
1471 acpigen_emit_ext_op(DEBUG_OP
);
1474 /* Store (op, DEBUG) */
1475 void acpigen_write_debug_op(uint8_t op
)
1477 acpigen_write_store();
1478 acpigen_emit_byte(op
);
1479 acpigen_emit_ext_op(DEBUG_OP
);
1482 /* Store (str, DEBUG) */
1483 void acpigen_write_debug_namestr(const char *str
)
1485 acpigen_write_store();
1486 acpigen_emit_namestring(str
);
1487 acpigen_emit_ext_op(DEBUG_OP
);
1490 /* Concatenate (str1, str2, tmp_res)
1491 Store(tmp_res, DEBUG) */
1492 void acpigen_write_debug_concatenate_string_string(const char *str1
, const char *str2
,
1495 acpigen_concatenate_string_string(str1
, str2
, tmp_res
);
1496 acpigen_write_debug_op(tmp_res
);
1499 /* Concatenate (str1, val, tmp_res)
1500 Store(tmp_res, DEBUG) */
1501 void acpigen_write_debug_concatenate_string_int(const char *str
, uint64_t val
,
1504 acpigen_concatenate_string_int(str
, val
, tmp_res
);
1505 acpigen_write_debug_op(tmp_res
);
1508 /* Concatenate (str1, res, tmp_res)
1509 Store(tmp_res, DEBUG) */
1510 void acpigen_write_debug_concatenate_string_op(const char *str
, uint8_t res
,
1513 acpigen_concatenate_string_op(str
, res
, tmp_res
);
1514 acpigen_write_debug_op(tmp_res
);
1517 void acpigen_write_if(void)
1519 acpigen_emit_byte(IF_OP
);
1520 acpigen_write_len_f();
1523 /* If (And (arg1, arg2)) */
1524 void acpigen_write_if_and(uint8_t arg1
, uint8_t arg2
)
1527 acpigen_emit_byte(AND_OP
);
1528 acpigen_emit_byte(arg1
);
1529 acpigen_emit_byte(arg2
);
1533 * Generates ACPI code for checking if operand1 and operand2 are equal.
1534 * Both operand1 and operand2 are ACPI ops.
1536 * If (Lequal (op,1 op2))
1538 void acpigen_write_if_lequal_op_op(uint8_t op1
, uint8_t op2
)
1541 acpigen_emit_byte(LEQUAL_OP
);
1542 acpigen_emit_byte(op1
);
1543 acpigen_emit_byte(op2
);
1547 * Generates ACPI code for checking if operand1 is greater than operand2.
1548 * Both operand1 and operand2 are ACPI ops.
1550 * If (Lgreater (op1 op2))
1552 void acpigen_write_if_lgreater_op_op(uint8_t op1
, uint8_t op2
)
1555 acpigen_emit_byte(LGREATER_OP
);
1556 acpigen_emit_byte(op1
);
1557 acpigen_emit_byte(op2
);
1561 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1562 * operand1 is ACPI op and operand2 is an integer.
1564 * If (Lequal (op, val))
1566 void acpigen_write_if_lequal_op_int(uint8_t op
, uint64_t val
)
1569 acpigen_emit_byte(LEQUAL_OP
);
1570 acpigen_emit_byte(op
);
1571 acpigen_write_integer(val
);
1575 * Generates ACPI code for checking if operand is greater than the value, where,
1576 * operand is ACPI op and val is an integer.
1578 * If (Lgreater (op, val))
1580 void acpigen_write_if_lgreater_op_int(uint8_t op
, uint64_t val
)
1583 acpigen_emit_byte(LGREATER_OP
);
1584 acpigen_emit_byte(op
);
1585 acpigen_write_integer(val
);
1589 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1590 * operand1 is namestring and operand2 is an integer.
1592 * If (Lequal ("namestr", val))
1594 void acpigen_write_if_lequal_namestr_int(const char *namestr
, uint64_t val
)
1597 acpigen_emit_byte(LEQUAL_OP
);
1598 acpigen_emit_namestring(namestr
);
1599 acpigen_write_integer(val
);
1603 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1604 * operand1 is namestring and operand2 is an integer.
1606 * If (Lgreater ("namestr", val))
1608 void acpigen_write_if_lgreater_namestr_int(const char *namestr
, uint64_t val
)
1611 acpigen_emit_byte(LGREATER_OP
);
1612 acpigen_emit_namestring(namestr
);
1613 acpigen_write_integer(val
);
1617 * Generates ACPI code to check at runtime if an object named `namestring`
1618 * exists, and leaves the If scope open to continue execute code when this
1619 * is true. NOTE: Requires matching acpigen_write_if_end().
1621 * If (CondRefOf (NAME))
1623 void acpigen_write_if_cond_ref_of(const char *namestring
)
1626 acpigen_emit_ext_op(COND_REFOF_OP
);
1627 acpigen_emit_namestring(namestring
);
1628 acpigen_emit_byte(ZERO_OP
); /* ignore COND_REFOF_OP destination */
1631 /* Closes previously opened if statement and generates ACPI code for else statement. */
1632 void acpigen_write_else(void)
1635 acpigen_emit_byte(ELSE_OP
);
1636 acpigen_write_len_f();
1639 void acpigen_write_shiftleft_op_int(uint8_t src_result
, uint64_t count
)
1641 acpigen_emit_byte(SHIFT_LEFT_OP
);
1642 acpigen_emit_byte(src_result
);
1643 acpigen_write_integer(count
);
1644 acpigen_emit_byte(ZERO_OP
);
1647 void acpigen_write_to_buffer(uint8_t src
, uint8_t dst
)
1649 acpigen_emit_byte(TO_BUFFER_OP
);
1650 acpigen_emit_byte(src
);
1651 acpigen_emit_byte(dst
);
1654 void acpigen_write_to_integer(uint8_t src
, uint8_t dst
)
1656 acpigen_emit_byte(TO_INTEGER_OP
);
1657 acpigen_emit_byte(src
);
1658 acpigen_emit_byte(dst
);
1661 void acpigen_write_to_integer_from_namestring(const char *source
, uint8_t dst_op
)
1663 acpigen_emit_byte(TO_INTEGER_OP
);
1664 acpigen_emit_namestring(source
);
1665 acpigen_emit_byte(dst_op
);
1668 void acpigen_write_byte_buffer(uint8_t *arr
, size_t size
)
1672 acpigen_emit_byte(BUFFER_OP
);
1673 acpigen_write_len_f();
1674 acpigen_write_integer(size
);
1676 for (i
= 0; i
< size
; i
++)
1677 acpigen_emit_byte(arr
[i
]);
1682 void acpigen_write_return_byte_buffer(uint8_t *arr
, size_t size
)
1684 acpigen_emit_byte(RETURN_OP
);
1685 acpigen_write_byte_buffer(arr
, size
);
1688 void acpigen_write_return_singleton_buffer(uint8_t arg
)
1690 acpigen_write_return_byte_buffer(&arg
, 1);
1693 void acpigen_write_return_op(uint8_t arg
)
1695 acpigen_emit_byte(RETURN_OP
);
1696 acpigen_emit_byte(arg
);
1699 void acpigen_write_return_byte(uint8_t arg
)
1701 acpigen_emit_byte(RETURN_OP
);
1702 acpigen_write_byte(arg
);
1705 void acpigen_write_return_integer(uint64_t arg
)
1707 acpigen_emit_byte(RETURN_OP
);
1708 acpigen_write_integer(arg
);
1711 void acpigen_write_return_namestr(const char *arg
)
1713 acpigen_emit_byte(RETURN_OP
);
1714 acpigen_emit_namestring(arg
);
1717 void acpigen_write_return_string(const char *arg
)
1719 acpigen_emit_byte(RETURN_OP
);
1720 acpigen_write_string(arg
);
1723 void acpigen_write_upc(enum acpi_upc_type type
)
1725 acpigen_write_name("_UPC");
1726 acpigen_write_package(4);
1728 acpigen_write_byte(type
== UPC_TYPE_UNUSED
? 0 : 0xff);
1730 acpigen_write_byte(type
);
1732 acpigen_write_zero();
1734 acpigen_write_zero();
1738 void acpigen_write_pld(const struct acpi_pld
*pld
)
1742 if (acpi_pld_to_buffer(pld
, buf
, ARRAY_SIZE(buf
)) < 0)
1745 acpigen_write_name("_PLD");
1746 acpigen_write_package(1);
1747 acpigen_write_byte_buffer(buf
, ARRAY_SIZE(buf
));
1751 void acpigen_write_dsm(const char *uuid
, void (**callbacks
)(void *), size_t count
, void *arg
)
1753 struct dsm_uuid id
= DSM_UUID(uuid
, callbacks
, count
, arg
);
1754 acpigen_write_dsm_uuid_arr(&id
, 1);
1758 * Create a supported functions bitmask
1759 * bit 0: other functions than 0 are supported
1760 * bits 1-x: function x supported
1762 static void acpigen_dsm_uuid_enum_functions(const struct dsm_uuid
*id
)
1764 const size_t bytes
= DIV_ROUND_UP(id
->count
, BITS_PER_BYTE
);
1765 uint8_t *buffer
= alloca(bytes
);
1769 memset(buffer
, 0, bytes
);
1771 for (size_t i
= 0; i
< bytes
; i
++) {
1772 for (size_t j
= 0; j
< BITS_PER_BYTE
; j
++) {
1773 if (cb_idx
>= id
->count
)
1776 if (id
->callbacks
[cb_idx
++]) {
1778 buffer
[i
] |= BIT(j
);
1784 buffer
[0] |= BIT(0);
1786 acpigen_write_return_byte_buffer(buffer
, bytes
);
1789 static void acpigen_write_dsm_uuid(struct dsm_uuid
*id
)
1793 /* If (LEqual (Local0, ToUUID(uuid))) */
1795 acpigen_emit_byte(LEQUAL_OP
);
1796 acpigen_emit_byte(LOCAL0_OP
);
1797 acpigen_write_uuid(id
->uuid
);
1799 /* ToInteger (Arg2, Local1) */
1800 acpigen_write_to_integer(ARG2_OP
, LOCAL1_OP
);
1802 /* If (LEqual(Local1, 0)) */
1804 acpigen_write_if_lequal_op_int(LOCAL1_OP
, 0);
1805 if (id
->callbacks
[0])
1806 id
->callbacks
[0](id
->arg
);
1808 acpigen_dsm_uuid_enum_functions(id
);
1809 acpigen_write_if_end();
1812 for (i
= 1; i
< id
->count
; i
++) {
1813 /* If (LEqual (Local1, i)) */
1814 acpigen_write_if_lequal_op_int(LOCAL1_OP
, i
);
1816 /* Callback to write if handler. */
1817 if (id
->callbacks
[i
])
1818 id
->callbacks
[i
](id
->arg
);
1820 acpigen_write_if_end(); /* If */
1823 /* Default case: Return (Buffer (One) { 0x0 }) */
1824 acpigen_write_return_singleton_buffer(0x0);
1826 acpigen_write_if_end(); /* If (LEqual (Local0, ToUUID(uuid))) */
1831 * Generate ACPI AML code for _DSM method.
1832 * This function takes as input array of uuid for the device, set of callbacks
1833 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1834 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1837 * Arguments passed into _DSM method:
1840 * Arg2 = Function index
1841 * Arg3 = Function specific arguments
1843 * AML code generated would look like:
1844 * Method (_DSM, 4, Serialized) {
1845 * ToBuffer (Arg0, Local0)
1846 * If (LEqual (Local0, ToUUID(uuid))) {
1847 * ToInteger (Arg2, Local1)
1848 * If (LEqual (Local1, 0)) {
1849 * <acpigen by callback[0]>
1852 * If (LEqual (Local1, n)) {
1853 * <acpigen by callback[n]>
1855 * Return (Buffer (One) { 0x0 })
1858 * If (LEqual (Local0, ToUUID(uuidn))) {
1861 * Return (Buffer (One) { 0x0 })
1864 void acpigen_write_dsm_uuid_arr(struct dsm_uuid
*ids
, size_t count
)
1868 /* Method (_DSM, 4, Serialized) */
1869 acpigen_write_method_serialized("_DSM", 0x4);
1871 /* ToBuffer (Arg0, Local0) */
1872 acpigen_write_to_buffer(ARG0_OP
, LOCAL0_OP
);
1874 for (i
= 0; i
< count
; i
++)
1875 acpigen_write_dsm_uuid(&ids
[i
]);
1877 /* Return (Buffer (One) { 0x0 }) */
1878 acpigen_write_return_singleton_buffer(0x0);
1880 acpigen_pop_len(); /* Method _DSM */
1883 void acpigen_write_CPPC_package(const struct cppc_config
*config
)
1887 switch (config
->version
) {
1889 max
= CPPC_MAX_FIELDS_VER_1
;
1892 max
= CPPC_MAX_FIELDS_VER_2
;
1895 max
= CPPC_MAX_FIELDS_VER_3
;
1898 printk(BIOS_ERR
, "CPPC version %u is not implemented\n", config
->version
);
1901 acpigen_write_name(CPPC_PACKAGE_NAME
);
1903 /* Adding 2 to account for length and version fields */
1904 acpigen_write_package(max
+ 2);
1905 acpigen_write_dword(max
+ 2);
1907 acpigen_write_byte(config
->version
);
1909 for (i
= 0; i
< max
; ++i
) {
1910 const cppc_entry_t
*entry
= &config
->entries
[i
];
1911 if (entry
->type
== CPPC_TYPE_DWORD
)
1912 acpigen_write_dword(entry
->dword
);
1914 acpigen_write_register_resource(&entry
->reg
);
1919 void acpigen_write_CPPC_method(void)
1922 snprintf(pscope
, sizeof(pscope
), CONFIG_ACPI_CPU_STRING
"." CPPC_PACKAGE_NAME
, 0);
1924 acpigen_write_method("_CPC", 0);
1925 acpigen_emit_byte(RETURN_OP
);
1926 acpigen_emit_namestring(pscope
);
1931 * Generate ACPI AML code for _ROM method.
1932 * This function takes as input ROM data and ROM length.
1934 * The ACPI spec isn't clear about what should happen at the end of the
1935 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1936 * bytes in the returned buffer with zeros.
1938 * Arguments passed into _DSM method:
1939 * Arg0 = Offset in Bytes
1940 * Arg1 = Bytes to return
1943 * acpigen_write_rom(0xdeadbeef, 0x10000)
1945 * AML code generated would look like:
1946 * Method (_ROM, 2, NotSerialized) {
1948 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1949 * Field (ROMS, AnyAcc, NoLock, Preserve)
1955 * Store (Arg0, Local0)
1956 * Store (Arg1, Local1)
1958 * If (LGreater (Local1, 0x1000))
1960 * Store (0x1000, Local1)
1963 * Store (Local1, Local3)
1965 * If (LGreater (Local0, 0x10000))
1967 * Return(Buffer(Local1){0})
1970 * If (LGreater (Local0, 0x0f000))
1972 * Subtract (0x10000, Local0, Local2)
1973 * If (LGreater (Local1, Local2))
1975 * Store (Local2, Local1)
1979 * Name (ROM1, Buffer (Local3) {0})
1981 * Multiply (Local0, 0x08, Local0)
1982 * Multiply (Local1, 0x08, Local1)
1984 * CreateField (RBF0, Local0, Local1, TMPB)
1985 * Store (TMPB, ROM1)
1990 void acpigen_write_rom(void *bios
, const size_t length
)
1995 /* Method (_ROM, 2, Serialized) */
1996 acpigen_write_method_serialized("_ROM", 2);
1998 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1999 struct opregion opreg
= OPREGION("ROMS", SYSTEMMEMORY
, (uintptr_t)bios
, length
);
2000 acpigen_write_opregion(&opreg
);
2002 struct fieldlist l
[] = {
2003 FIELDLIST_OFFSET(0),
2004 FIELDLIST_NAMESTR("RBF0", 8 * length
),
2007 /* Field (ROMS, AnyAcc, NoLock, Preserve)
2012 acpigen_write_field(opreg
.name
, l
, 2, FIELD_ANYACC
| FIELD_NOLOCK
| FIELD_PRESERVE
);
2014 /* Store (Arg0, Local0) */
2015 acpigen_write_store_ops(ARG0_OP
, LOCAL0_OP
);
2017 /* Store (Arg1, Local1) */
2018 acpigen_write_store_ops(ARG1_OP
, LOCAL1_OP
);
2020 /* ACPI SPEC requires to return at maximum 4KiB */
2021 /* If (LGreater (Local1, 0x1000)) */
2022 acpigen_write_if_lgreater_op_int(LOCAL1_OP
, 0x1000);
2024 /* Store (0x1000, Local1) */
2025 acpigen_write_store_int_to_op(0x1000, LOCAL1_OP
);
2030 /* Store (Local1, Local3) */
2031 acpigen_write_store_ops(LOCAL1_OP
, LOCAL3_OP
);
2033 /* If (LGreater (Local0, length)) */
2034 acpigen_write_if_lgreater_op_int(LOCAL0_OP
, length
);
2036 /* Return(Buffer(Local1){0}) */
2037 acpigen_emit_byte(RETURN_OP
);
2038 acpigen_emit_byte(BUFFER_OP
);
2039 acpigen_write_len_f();
2040 acpigen_emit_byte(LOCAL1_OP
);
2041 acpigen_emit_byte(0);
2047 /* If (LGreater (Local0, length - 4096)) */
2048 acpigen_write_if_lgreater_op_int(LOCAL0_OP
, length
- 4096);
2050 /* Subtract (length, Local0, Local2) */
2051 acpigen_emit_byte(SUBTRACT_OP
);
2052 acpigen_write_integer(length
);
2053 acpigen_emit_byte(LOCAL0_OP
);
2054 acpigen_emit_byte(LOCAL2_OP
);
2056 /* If (LGreater (Local1, Local2)) */
2057 acpigen_write_if_lgreater_op_op(LOCAL1_OP
, LOCAL2_OP
);
2059 /* Store (Local2, Local1) */
2060 acpigen_write_store_ops(LOCAL2_OP
, LOCAL1_OP
);
2068 /* Name (ROM1, Buffer (Local3) {0}) */
2069 acpigen_write_name("ROM1");
2070 acpigen_emit_byte(BUFFER_OP
);
2071 acpigen_write_len_f();
2072 acpigen_emit_byte(LOCAL3_OP
);
2073 acpigen_emit_byte(0);
2076 /* Multiply (Local1, 0x08, Local1) */
2077 acpigen_emit_byte(MULTIPLY_OP
);
2078 acpigen_emit_byte(LOCAL1_OP
);
2079 acpigen_write_integer(0x08);
2080 acpigen_emit_byte(LOCAL1_OP
);
2082 /* Multiply (Local0, 0x08, Local0) */
2083 acpigen_emit_byte(MULTIPLY_OP
);
2084 acpigen_emit_byte(LOCAL0_OP
);
2085 acpigen_write_integer(0x08);
2086 acpigen_emit_byte(LOCAL0_OP
);
2088 /* CreateField (RBF0, Local0, Local1, TMPB) */
2089 acpigen_emit_ext_op(CREATEFIELD_OP
);
2090 acpigen_emit_namestring("RBF0");
2091 acpigen_emit_byte(LOCAL0_OP
);
2092 acpigen_emit_byte(LOCAL1_OP
);
2093 acpigen_emit_namestring("TMPB");
2095 /* Store (TMPB, ROM1) */
2096 acpigen_write_store_namestr_to_namestr("TMPB", "ROM1");
2099 acpigen_emit_byte(RETURN_OP
);
2100 acpigen_emit_namestring("ROM1");
2107 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
2108 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
2109 * make callbacks into SoC acpigen code.
2111 * Returns 0 on success and -1 on error.
2113 int acpigen_enable_tx_gpio(const struct acpi_gpio
*gpio
)
2115 if (gpio
->active_low
)
2116 return acpigen_soc_clear_tx_gpio(gpio
->pins
[0]);
2118 return acpigen_soc_set_tx_gpio(gpio
->pins
[0]);
2121 int acpigen_disable_tx_gpio(const struct acpi_gpio
*gpio
)
2123 if (gpio
->active_low
)
2124 return acpigen_soc_set_tx_gpio(gpio
->pins
[0]);
2126 return acpigen_soc_clear_tx_gpio(gpio
->pins
[0]);
2129 void acpigen_get_rx_gpio(const struct acpi_gpio
*gpio
)
2131 acpigen_soc_read_rx_gpio(gpio
->pins
[0]);
2133 if (gpio
->active_low
)
2134 acpigen_write_xor(LOCAL0_OP
, 1, LOCAL0_OP
);
2137 void acpigen_get_tx_gpio(const struct acpi_gpio
*gpio
)
2139 acpigen_soc_get_tx_gpio(gpio
->pins
[0]);
2141 if (gpio
->active_low
)
2142 acpigen_write_xor(LOCAL0_OP
, 1, LOCAL0_OP
);
2145 /* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
2146 void acpigen_resource_word(u16 res_type
, u16 gen_flags
, u16 type_flags
, u16 gran
, u16 range_min
,
2147 u16 range_max
, u16 translation
, u16 length
)
2149 acpigen_emit_byte(0x88);
2150 /* Byte 1+2: length (0x000d) */
2151 acpigen_emit_byte(0x0d);
2152 acpigen_emit_byte(0x00);
2154 acpigen_emit_byte(res_type
); // 0 - mem, 1 - io, 2 - bus
2156 acpigen_emit_byte(gen_flags
);
2158 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2159 acpigen_emit_byte(type_flags
);
2160 /* granularity, min, max, translation, length */
2161 acpigen_emit_word(gran
);
2162 acpigen_emit_word(range_min
);
2163 acpigen_emit_word(range_max
);
2164 acpigen_emit_word(translation
);
2165 acpigen_emit_word(length
);
2168 /* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
2169 void acpigen_resource_dword(u16 res_type
, u16 gen_flags
, u16 type_flags
, u32 gran
,
2170 u32 range_min
, u32 range_max
, u32 translation
, u32 length
)
2172 acpigen_emit_byte(0x87);
2173 /* Byte 1+2: length (0023) */
2174 acpigen_emit_byte(23);
2175 acpigen_emit_byte(0x00);
2177 acpigen_emit_byte(res_type
); // 0 - mem, 1 - io, 2 - bus
2179 acpigen_emit_byte(gen_flags
);
2181 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2182 acpigen_emit_byte(type_flags
);
2183 /* granularity, min, max, translation, length */
2184 acpigen_emit_dword(gran
);
2185 acpigen_emit_dword(range_min
);
2186 acpigen_emit_dword(range_max
);
2187 acpigen_emit_dword(translation
);
2188 acpigen_emit_dword(length
);
2191 static void acpigen_emit_qword(u64 data
)
2193 acpigen_emit_dword(data
& 0xffffffff);
2194 acpigen_emit_dword((data
>> 32) & 0xffffffff);
2197 /* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
2198 void acpigen_resource_qword(u16 res_type
, u16 gen_flags
, u16 type_flags
, u64 gran
,
2199 u64 range_min
, u64 range_max
, u64 translation
, u64 length
)
2201 acpigen_emit_byte(0x8a);
2202 /* Byte 1+2: length (0x002b) */
2203 acpigen_emit_byte(0x2b);
2204 acpigen_emit_byte(0x00);
2206 acpigen_emit_byte(res_type
); // 0 - mem, 1 - io, 2 - bus
2208 acpigen_emit_byte(gen_flags
);
2210 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2211 acpigen_emit_byte(type_flags
);
2212 /* granularity, min, max, translation, length */
2213 acpigen_emit_qword(gran
);
2214 acpigen_emit_qword(range_min
);
2215 acpigen_emit_qword(range_max
);
2216 acpigen_emit_qword(translation
);
2217 acpigen_emit_qword(length
);
2220 void acpigen_write_ADR(uint64_t adr
)
2222 acpigen_write_name_qword("_ADR", adr
);
2226 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2227 * @address: SoundWire device address properties.
2229 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2231 * 63..52 - Reserved (0)
2232 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2233 * Used when a Controller has multiple master devices, each producing a
2234 * separate SoundWire Link. Set to 0 for single-link controllers.
2235 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2236 * 47..44 - SoundWire specification version that this device supports
2237 * 43..40 - Unique ID for multiple devices
2238 * 39..24 - MIPI standard manufacturer code
2239 * 23..08 - Vendor defined part ID
2240 * 07..00 - MIPI class encoding
2242 void acpigen_write_ADR_soundwire_device(const struct soundwire_address
*address
)
2244 acpigen_write_ADR((((uint64_t)address
->link_id
& 0xf) << 48) |
2245 (((uint64_t)address
->version
& 0xf) << 44) |
2246 (((uint64_t)address
->unique_id
& 0xf) << 40) |
2247 (((uint64_t)address
->manufacturer_id
& 0xffff) << 24) |
2248 (((uint64_t)address
->part_id
& 0xffff) << 8) |
2249 (((uint64_t)address
->class & 0xff)));
2252 void acpigen_notify(const char *namestr
, int value
)
2254 acpigen_emit_byte(NOTIFY_OP
);
2255 acpigen_emit_namestring(namestr
);
2256 acpigen_write_integer(value
);
2259 static void _create_field(uint8_t aml_op
, uint8_t srcop
, size_t byte_offset
, const char *name
)
2261 acpigen_emit_byte(aml_op
);
2262 acpigen_emit_byte(srcop
);
2263 acpigen_write_integer(byte_offset
);
2264 acpigen_emit_namestring(name
);
2267 void acpigen_write_create_byte_field(uint8_t op
, size_t byte_offset
, const char *name
)
2269 _create_field(CREATE_BYTE_OP
, op
, byte_offset
, name
);
2272 void acpigen_write_create_word_field(uint8_t op
, size_t byte_offset
, const char *name
)
2274 _create_field(CREATE_WORD_OP
, op
, byte_offset
, name
);
2277 void acpigen_write_create_dword_field(uint8_t op
, size_t byte_offset
, const char *name
)
2279 _create_field(CREATE_DWORD_OP
, op
, byte_offset
, name
);
2282 void acpigen_write_create_qword_field(uint8_t op
, size_t byte_offset
, const char *name
)
2284 _create_field(CREATE_QWORD_OP
, op
, byte_offset
, name
);
2287 void acpigen_write_pct_package(const acpi_addr_t
*perf_ctrl
, const acpi_addr_t
*perf_sts
)
2289 acpigen_write_name("_PCT");
2290 acpigen_write_package(0x02);
2291 acpigen_write_register_resource(perf_ctrl
);
2292 acpigen_write_register_resource(perf_sts
);
2297 void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate
*pstate_value
)
2299 acpigen_write_package(0x08);
2300 acpigen_write_dword(pstate_value
->core_freq
);
2301 acpigen_write_dword(pstate_value
->power
);
2302 acpigen_write_dword(pstate_value
->transition_latency
);
2303 acpigen_write_dword(pstate_value
->bus_master_latency
);
2305 acpigen_write_byte_buffer((uint8_t *)&pstate_value
->control_value
, sizeof(uint64_t));
2306 acpigen_write_byte_buffer((uint8_t *)&pstate_value
->status_value
, sizeof(uint64_t));
2307 acpigen_write_byte_buffer((uint8_t *)&pstate_value
->control_mask
, sizeof(uint64_t));
2308 acpigen_write_byte_buffer((uint8_t *)&pstate_value
->status_mask
, sizeof(uint64_t));
2313 void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate
*pstate_values
, size_t nentries
)
2317 acpigen_write_name("XPSS");
2318 acpigen_write_package(nentries
);
2319 for (pstate
= 0; pstate
< nentries
; pstate
++) {
2320 acpigen_write_xpss_package(pstate_values
);
2327 /* Delay up to wait_ms until provided namestr matches expected value. */
2328 void acpigen_write_delay_until_namestr_int(uint32_t wait_ms
, const char *name
, uint64_t value
)
2330 uint32_t wait_ms_segment
= 1;
2331 uint32_t segments
= wait_ms
;
2333 /* Sleep in 16ms segments if delay is more than 32ms. */
2335 wait_ms_segment
= 16;
2336 segments
= wait_ms
/ 16;
2339 acpigen_write_store_int_to_op(segments
, LOCAL7_OP
);
2340 acpigen_emit_byte(WHILE_OP
);
2341 acpigen_write_len_f();
2342 acpigen_emit_byte(LGREATER_OP
);
2343 acpigen_emit_byte(LOCAL7_OP
);
2344 acpigen_emit_byte(ZERO_OP
);
2346 /* If name is not provided then just delay in a loop. */
2348 acpigen_write_if_lequal_namestr_int(name
, value
);
2349 acpigen_emit_byte(BREAK_OP
);
2350 acpigen_pop_len(); /* If */
2353 acpigen_write_sleep(wait_ms_segment
);
2354 acpigen_emit_byte(DECREMENT_OP
);
2355 acpigen_emit_byte(LOCAL7_OP
);
2356 acpigen_pop_len(); /* While */