payloads/libpayload/curses/menu/eti.h: Remove unneeded whitespaces
[coreboot.git] / src / acpi / acpigen.c
bloba4a74f65d7fdb29d99e54a8b63222633ea859b4a
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /* How much nesting do we support? */
4 #define ACPIGEN_LENSTACK_SIZE 10
6 /*
7 * If you need to change this, change acpigen_write_len_f and
8 * acpigen_pop_len
9 */
11 #define ACPIGEN_MAXLEN 0xfffff
13 #include <lib.h>
14 #include <string.h>
15 #include <acpi/acpigen.h>
16 #include <assert.h>
17 #include <console/console.h>
18 #include <device/device.h>
19 #include <device/pci_def.h>
20 #include <device/pci_type.h>
21 #include <device/soundwire.h>
23 static char *gencurrent;
25 char *len_stack[ACPIGEN_LENSTACK_SIZE];
26 int ltop = 0;
28 void acpigen_write_len_f(void)
30 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
31 len_stack[ltop++] = gencurrent;
32 acpigen_emit_byte(0);
33 acpigen_emit_byte(0);
34 acpigen_emit_byte(0);
37 void acpigen_pop_len(void)
39 int len;
40 ASSERT(ltop > 0)
41 char *p = len_stack[--ltop];
42 len = gencurrent - p;
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)
53 gencurrent = curr;
56 char *acpigen_get_current(void)
58 return gencurrent;
61 void acpigen_emit_byte(unsigned char b)
63 (*gencurrent++) = 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)
88 char *p;
89 acpigen_emit_byte(PACKAGE_OP);
90 acpigen_write_len_f();
91 p = acpigen_get_current();
92 acpigen_emit_byte(nr_el);
93 return p;
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)
138 if (data == 0)
139 acpigen_write_zero();
140 else if (data == 1)
141 acpigen_write_one();
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);
148 else
149 acpigen_write_qword(data);
152 void acpigen_write_name_zero(const char *name)
154 acpigen_write_name(name);
155 acpigen_write_one();
158 void acpigen_write_name_one(const char *name)
160 acpigen_write_name(name);
161 acpigen_write_zero();
164 void acpigen_write_name_byte(const char *name, uint8_t val)
166 acpigen_write_name(name);
167 acpigen_write_byte(val);
170 void acpigen_write_name_dword(const char *name, uint32_t val)
172 acpigen_write_name(name);
173 acpigen_write_dword(val);
176 void acpigen_write_name_qword(const char *name, uint64_t val)
178 acpigen_write_name(name);
179 acpigen_write_qword(val);
182 void acpigen_write_name_integer(const char *name, uint64_t val)
184 acpigen_write_name(name);
185 acpigen_write_integer(val);
188 void acpigen_write_name_string(const char *name, const char *string)
190 acpigen_write_name(name);
191 acpigen_write_string(string);
194 void acpigen_write_name_unicode(const char *name, const char *string)
196 const size_t len = strlen(string) + 1;
197 acpigen_write_name(name);
198 acpigen_emit_byte(BUFFER_OP);
199 acpigen_write_len_f();
200 acpigen_write_integer(len);
201 for (size_t i = 0; i < len; i++) {
202 const char c = string[i];
203 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
204 acpigen_emit_word(c >= 0 ? c : '?');
206 acpigen_pop_len();
209 void acpigen_emit_stream(const char *data, int size)
211 int i;
212 for (i = 0; i < size; i++)
213 acpigen_emit_byte(data[i]);
216 void acpigen_emit_string(const char *string)
218 acpigen_emit_stream(string, string ? strlen(string) : 0);
219 acpigen_emit_byte('\0'); /* NUL */
222 void acpigen_write_string(const char *string)
224 acpigen_emit_byte(STRING_PREFIX);
225 acpigen_emit_string(string);
228 void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
230 char hid[9]; /* BOOTxxxx */
232 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
233 acpigen_write_name_string("_HID", hid);
237 * The naming conventions for ACPI namespace names are a bit tricky as
238 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
239 * and "By convention, when an ASL compiler pads a name shorter than 4
240 * characters, it is done so with trailing underscores ('_')".
242 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
245 static void acpigen_emit_simple_namestring(const char *name)
247 int i;
248 char ud[] = "____";
249 for (i = 0; i < 4; i++) {
250 if ((name[i] == '\0') || (name[i] == '.')) {
251 acpigen_emit_stream(ud, 4 - i);
252 break;
254 acpigen_emit_byte(name[i]);
258 static void acpigen_emit_double_namestring(const char *name, int dotpos)
260 acpigen_emit_byte(DUAL_NAME_PREFIX);
261 acpigen_emit_simple_namestring(name);
262 acpigen_emit_simple_namestring(&name[dotpos + 1]);
265 static void acpigen_emit_multi_namestring(const char *name)
267 int count = 0;
268 unsigned char *pathlen;
269 acpigen_emit_byte(MULTI_NAME_PREFIX);
270 acpigen_emit_byte(ZERO_OP);
271 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
273 while (name[0] != '\0') {
274 acpigen_emit_simple_namestring(name);
275 /* find end or next entity */
276 while ((name[0] != '.') && (name[0] != '\0'))
277 name++;
278 /* forward to next */
279 if (name[0] == '.')
280 name++;
281 count++;
284 pathlen[0] = count;
288 void acpigen_emit_namestring(const char *namepath)
290 int dotcount = 0, i;
291 int dotpos = 0;
293 /* We can start with a '\'. */
294 if (namepath[0] == '\\') {
295 acpigen_emit_byte('\\');
296 namepath++;
299 /* And there can be any number of '^' */
300 while (namepath[0] == '^') {
301 acpigen_emit_byte('^');
302 namepath++;
305 /* If we have only \\ or only ^...^. Then we need to put a null
306 name (0x00). */
307 if (namepath[0] == '\0') {
308 acpigen_emit_byte(ZERO_OP);
309 return;
312 i = 0;
313 while (namepath[i] != '\0') {
314 if (namepath[i] == '.') {
315 dotcount++;
316 dotpos = i;
318 i++;
321 if (dotcount == 0)
322 acpigen_emit_simple_namestring(namepath);
323 else if (dotcount == 1)
324 acpigen_emit_double_namestring(namepath, dotpos);
325 else
326 acpigen_emit_multi_namestring(namepath);
329 void acpigen_write_name(const char *name)
331 acpigen_emit_byte(NAME_OP);
332 acpigen_emit_namestring(name);
335 void acpigen_write_scope(const char *name)
337 acpigen_emit_byte(SCOPE_OP);
338 acpigen_write_len_f();
339 acpigen_emit_namestring(name);
342 void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
344 /* <dest_op> = DeRefOf (<package_op>[<element]) */
345 acpigen_write_store();
346 acpigen_emit_byte(DEREF_OP);
347 acpigen_emit_byte(INDEX_OP);
348 acpigen_emit_byte(package_op);
349 acpigen_write_integer(element);
350 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
351 acpigen_emit_byte(dest_op);
354 void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
357 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
360 char pscope[16];
361 acpigen_emit_ext_op(PROCESSOR_OP);
362 acpigen_write_len_f();
364 snprintf(pscope, sizeof(pscope),
365 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
366 acpigen_emit_namestring(pscope);
367 acpigen_emit_byte(cpuindex);
368 acpigen_emit_dword(pblock_addr);
369 acpigen_emit_byte(pblock_len);
372 void acpigen_write_processor_package(const char *const name,
373 const unsigned int first_core,
374 const unsigned int core_count)
376 unsigned int i;
377 char pscope[16];
379 acpigen_write_name(name);
380 acpigen_write_package(core_count);
381 for (i = first_core; i < first_core + core_count; ++i) {
382 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
383 acpigen_emit_namestring(pscope);
385 acpigen_pop_len();
388 /* Method to notify all CPU cores */
389 void acpigen_write_processor_cnot(const unsigned int number_of_cores)
391 int core_id;
393 acpigen_write_method("\\_SB.CNOT", 1);
394 for (core_id = 0; core_id < number_of_cores; core_id++) {
395 char buffer[DEVICE_PATH_MAX];
396 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
397 core_id);
398 acpigen_emit_byte(NOTIFY_OP);
399 acpigen_emit_namestring(buffer);
400 acpigen_emit_byte(ARG0_OP);
402 acpigen_pop_len();
406 * Generate ACPI AML code for OperationRegion
407 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
408 * where rname is region name, space is region space, offset is region offset &
409 * len is region length.
410 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
412 void acpigen_write_opregion(struct opregion *opreg)
414 /* OpregionOp */
415 acpigen_emit_ext_op(OPREGION_OP);
416 /* NameString 4 chars only */
417 acpigen_emit_simple_namestring(opreg->name);
418 /* RegionSpace */
419 acpigen_emit_byte(opreg->regionspace);
420 /* RegionOffset & RegionLen, it can be byte word or double word */
421 acpigen_write_integer(opreg->regionoffset);
422 acpigen_write_integer(opreg->regionlen);
426 * Generate ACPI AML code for Mutex
427 * Arg0: Pointer to name of mutex
428 * Arg1: Initial value of mutex
430 void acpigen_write_mutex(const char *name, const uint8_t flags)
432 /* MutexOp */
433 acpigen_emit_ext_op(MUTEX_OP);
434 /* NameString 4 chars only */
435 acpigen_emit_simple_namestring(name);
436 acpigen_emit_byte(flags);
439 void acpigen_write_acquire(const char *name, const uint16_t val)
441 /* AcquireOp */
442 acpigen_emit_ext_op(ACQUIRE_OP);
443 /* NameString 4 chars only */
444 acpigen_emit_simple_namestring(name);
445 acpigen_emit_word(val);
448 void acpigen_write_release(const char *name)
450 /* ReleaseOp */
451 acpigen_emit_ext_op(RELEASE_OP);
452 /* NameString 4 chars only */
453 acpigen_emit_simple_namestring(name);
456 static void acpigen_write_field_length(uint32_t len)
458 uint8_t i, j;
459 uint8_t emit[4];
461 i = 1;
462 if (len < 0x40) {
463 emit[0] = len & 0x3F;
464 } else {
465 emit[0] = len & 0xF;
466 len >>= 4;
467 while (len) {
468 emit[i] = len & 0xFF;
469 i++;
470 len >>= 8;
473 /* Update bit 7:6 : Number of bytes followed by emit[0] */
474 emit[0] |= (i - 1) << 6;
476 for (j = 0; j < i; j++)
477 acpigen_emit_byte(emit[j]);
480 static void acpigen_write_field_offset(uint32_t offset,
481 uint32_t current_bit_pos)
483 uint32_t diff_bits;
485 if (offset < current_bit_pos) {
486 printk(BIOS_WARNING, "%s: Cannot move offset backward",
487 __func__);
488 return;
491 diff_bits = offset - current_bit_pos;
492 /* Upper limit */
493 if (diff_bits > 0xFFFFFFF) {
494 printk(BIOS_WARNING, "%s: Offset very large to encode",
495 __func__);
496 return;
499 acpigen_emit_byte(0);
500 acpigen_write_field_length(diff_bits);
503 static void acpigen_write_field_name(const char *name, uint32_t size)
505 acpigen_emit_simple_namestring(name);
506 acpigen_write_field_length(size);
510 * Generate ACPI AML code for Field
511 * Arg0: region name
512 * Arg1: Pointer to struct fieldlist.
513 * Arg2: no. of entries in Arg1
514 * Arg3: flags which indicate filed access type, lock rule & update rule.
515 * Example with fieldlist
516 * struct fieldlist l[] = {
517 * FIELDLIST_OFFSET(0x84),
518 * FIELDLIST_NAMESTR("PMCS", 2),
519 * };
520 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
521 * FIELD_PRESERVE);
522 * Output:
523 * Field (UART, AnyAcc, NoLock, Preserve)
525 * Offset (0x84),
526 * PMCS, 2
529 void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
530 uint8_t flags)
532 uint16_t i;
533 uint32_t current_bit_pos = 0;
535 /* FieldOp */
536 acpigen_emit_ext_op(FIELD_OP);
537 /* Package Length */
538 acpigen_write_len_f();
539 /* NameString 4 chars only */
540 acpigen_emit_simple_namestring(name);
541 /* Field Flag */
542 acpigen_emit_byte(flags);
544 for (i = 0; i < count; i++) {
545 switch (l[i].type) {
546 case NAME_STRING:
547 acpigen_write_field_name(l[i].name, l[i].bits);
548 current_bit_pos += l[i].bits;
549 break;
550 case OFFSET:
551 acpigen_write_field_offset(l[i].bits, current_bit_pos);
552 current_bit_pos = l[i].bits;
553 break;
554 default:
555 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
556 , __func__, l[i].type);
557 break;
560 acpigen_pop_len();
564 * Generate ACPI AML code for IndexField
565 * Arg0: region name
566 * Arg1: Pointer to struct fieldlist.
567 * Arg2: no. of entries in Arg1
568 * Arg3: flags which indicate filed access type, lock rule & update rule.
569 * Example with fieldlist
570 * struct fieldlist l[] = {
571 * FIELDLIST_OFFSET(0x84),
572 * FIELDLIST_NAMESTR("PMCS", 2),
573 * };
574 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
575 * FIELD_NOLOCK |
576 * FIELD_PRESERVE);
577 * Output:
578 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
580 * Offset (0x84),
581 * PMCS, 2
584 void acpigen_write_indexfield(const char *idx, const char *data,
585 struct fieldlist *l, size_t count, uint8_t flags)
587 uint16_t i;
588 uint32_t current_bit_pos = 0;
590 /* FieldOp */
591 acpigen_emit_ext_op(INDEX_FIELD_OP);
592 /* Package Length */
593 acpigen_write_len_f();
594 /* NameString 4 chars only */
595 acpigen_emit_simple_namestring(idx);
596 /* NameString 4 chars only */
597 acpigen_emit_simple_namestring(data);
598 /* Field Flag */
599 acpigen_emit_byte(flags);
601 for (i = 0; i < count; i++) {
602 switch (l[i].type) {
603 case NAME_STRING:
604 acpigen_write_field_name(l[i].name, l[i].bits);
605 current_bit_pos += l[i].bits;
606 break;
607 case OFFSET:
608 acpigen_write_field_offset(l[i].bits, current_bit_pos);
609 current_bit_pos = l[i].bits;
610 break;
611 default:
612 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
613 , __func__, l[i].type);
614 break;
617 acpigen_pop_len();
620 void acpigen_write_empty_PCT(void)
623 Name (_PCT, Package (0x02)
625 ResourceTemplate ()
627 Register (FFixedHW,
628 0x00, // Bit Width
629 0x00, // Bit Offset
630 0x0000000000000000, // Address
634 ResourceTemplate ()
636 Register (FFixedHW,
637 0x00, // Bit Width
638 0x00, // Bit Offset
639 0x0000000000000000, // Address
644 static char stream[] = {
645 /* 00000030 "0._PCT.," */
646 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
647 /* 00000038 "........" */
648 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
649 /* 00000040 "........" */
650 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
651 /* 00000048 "....y..." */
652 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
653 /* 00000050 "........" */
654 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
655 /* 00000058 "........" */
656 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
657 0x00, 0x79, 0x00
659 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
662 void acpigen_write_empty_PTC(void)
665 Name (_PTC, Package (0x02)
667 ResourceTemplate ()
669 Register (FFixedHW,
670 0x00, // Bit Width
671 0x00, // Bit Offset
672 0x0000000000000000, // Address
676 ResourceTemplate ()
678 Register (FFixedHW,
679 0x00, // Bit Width
680 0x00, // Bit Offset
681 0x0000000000000000, // Address
686 acpi_addr_t addr = {
687 .space_id = ACPI_ADDRESS_SPACE_FIXED,
688 .bit_width = 0,
689 .bit_offset = 0,
690 .access_size = 0,
691 .addrl = 0,
692 .addrh = 0,
695 acpigen_write_name("_PTC");
696 acpigen_write_package(2);
698 /* ControlRegister */
699 acpigen_write_register_resource(&addr);
701 /* StatusRegister */
702 acpigen_write_register_resource(&addr);
704 acpigen_pop_len();
707 static void __acpigen_write_method(const char *name, uint8_t flags)
709 acpigen_emit_byte(METHOD_OP);
710 acpigen_write_len_f();
711 acpigen_emit_namestring(name);
712 acpigen_emit_byte(flags);
715 /* Method (name, nargs, NotSerialized) */
716 void acpigen_write_method(const char *name, int nargs)
718 __acpigen_write_method(name, (nargs & 7));
721 /* Method (name, nargs, Serialized) */
722 void acpigen_write_method_serialized(const char *name, int nargs)
724 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
727 void acpigen_write_device(const char *name)
729 acpigen_emit_ext_op(DEVICE_OP);
730 acpigen_write_len_f();
731 acpigen_emit_namestring(name);
734 void acpigen_write_STA(uint8_t status)
737 * Method (_STA, 0, NotSerialized) { Return (status) }
739 acpigen_write_method("_STA", 0);
740 acpigen_emit_byte(RETURN_OP);
741 acpigen_write_byte(status);
742 acpigen_pop_len();
745 void acpigen_write_STA_ext(const char *namestring)
748 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
750 acpigen_write_method("_STA", 0);
751 acpigen_emit_byte(RETURN_OP);
752 acpigen_emit_namestring(namestring);
753 acpigen_pop_len();
757 * Generates a func with max supported P-states.
759 void acpigen_write_PPC(u8 nr)
762 Method (_PPC, 0, NotSerialized)
764 Return (nr)
767 acpigen_write_method("_PPC", 0);
768 acpigen_emit_byte(RETURN_OP);
769 /* arg */
770 acpigen_write_byte(nr);
771 acpigen_pop_len();
775 * Generates a func with max supported P-states saved
776 * in the variable PPCM.
778 void acpigen_write_PPC_NVS(void)
781 Method (_PPC, 0, NotSerialized)
783 Return (PPCM)
786 acpigen_write_method("_PPC", 0);
787 acpigen_emit_byte(RETURN_OP);
788 /* arg */
789 acpigen_emit_namestring("PPCM");
790 acpigen_pop_len();
793 void acpigen_write_TPC(const char *gnvs_tpc_limit)
796 // Sample _TPC method
797 Method (_TPC, 0, NotSerialized)
799 Return (\TLVL)
802 acpigen_write_method("_TPC", 0);
803 acpigen_emit_byte(RETURN_OP);
804 acpigen_emit_namestring(gnvs_tpc_limit);
805 acpigen_pop_len();
808 void acpigen_write_PRW(u32 wake, u32 level)
811 * Name (_PRW, Package () { wake, level }
813 acpigen_write_name("_PRW");
814 acpigen_write_package(2);
815 acpigen_write_integer(wake);
816 acpigen_write_integer(level);
817 acpigen_pop_len();
820 void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
821 u32 busmLat, u32 control, u32 status)
823 acpigen_write_package(6);
824 acpigen_write_dword(coreFreq);
825 acpigen_write_dword(power);
826 acpigen_write_dword(transLat);
827 acpigen_write_dword(busmLat);
828 acpigen_write_dword(control);
829 acpigen_write_dword(status);
830 acpigen_pop_len();
832 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
833 coreFreq, power, control, status);
836 void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
838 acpigen_write_name("_PSD");
839 acpigen_write_package(1);
840 acpigen_write_package(5);
841 acpigen_write_byte(5); // 5 values
842 acpigen_write_byte(0); // revision 0
843 acpigen_write_dword(domain);
844 acpigen_write_dword(coordtype);
845 acpigen_write_dword(numprocs);
846 acpigen_pop_len();
847 acpigen_pop_len();
850 void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
852 acpigen_write_package(4);
853 acpigen_write_register_resource(&cstate->resource);
854 acpigen_write_dword(cstate->ctype);
855 acpigen_write_dword(cstate->latency);
856 acpigen_write_dword(cstate->power);
857 acpigen_pop_len();
860 void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
862 int i;
863 acpigen_write_name("_CST");
864 acpigen_write_package(nentries+1);
865 acpigen_write_dword(nentries);
867 for (i = 0; i < nentries; i++)
868 acpigen_write_CST_package_entry(cstate + i);
870 acpigen_pop_len();
873 void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
874 u32 index)
876 acpigen_write_name("_CSD");
877 acpigen_write_package(1);
878 acpigen_write_package(6);
879 acpigen_write_byte(6); // 6 values
880 acpigen_write_byte(0); // revision 0
881 acpigen_write_dword(domain);
882 acpigen_write_dword(coordtype);
883 acpigen_write_dword(numprocs);
884 acpigen_write_dword(index);
885 acpigen_pop_len();
886 acpigen_pop_len();
889 void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
892 Sample _TSS package with 100% and 50% duty cycles
893 Name (_TSS, Package (0x02)
895 Package(){100, 1000, 0, 0x00, 0)
896 Package(){50, 520, 0, 0x18, 0)
899 int i;
900 acpi_tstate_t *tstate = tstate_list;
902 acpigen_write_name("_TSS");
903 acpigen_write_package(entries);
905 for (i = 0; i < entries; i++) {
906 acpigen_write_package(5);
907 acpigen_write_dword(tstate->percent);
908 acpigen_write_dword(tstate->power);
909 acpigen_write_dword(tstate->latency);
910 acpigen_write_dword(tstate->control);
911 acpigen_write_dword(tstate->status);
912 acpigen_pop_len();
913 tstate++;
916 acpigen_pop_len();
919 void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
921 acpigen_write_name("_TSD");
922 acpigen_write_package(1);
923 acpigen_write_package(5);
924 acpigen_write_byte(5); // 5 values
925 acpigen_write_byte(0); // revision 0
926 acpigen_write_dword(domain);
927 acpigen_write_dword(coordtype);
928 acpigen_write_dword(numprocs);
929 acpigen_pop_len();
930 acpigen_pop_len();
935 void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
938 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
939 * Byte 0:
940 * Bit7 : 1 => big item
941 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
943 acpigen_emit_byte(0x86);
944 /* Byte 1+2: length (0x0009) */
945 acpigen_emit_byte(0x09);
946 acpigen_emit_byte(0x00);
947 /* bit1-7 are ignored */
948 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
949 acpigen_emit_dword(base);
950 acpigen_emit_dword(size);
953 static void acpigen_write_register(const acpi_addr_t *addr)
955 acpigen_emit_byte(0x82); /* Register Descriptor */
956 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
957 acpigen_emit_byte(0x00); /* Register Length 15:8 */
958 acpigen_emit_byte(addr->space_id); /* Address Space ID */
959 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
960 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
961 acpigen_emit_byte(addr->access_size); /* Register Access Size */
962 acpigen_emit_dword(addr->addrl); /* Register Address Low */
963 acpigen_emit_dword(addr->addrh); /* Register Address High */
966 void acpigen_write_register_resource(const acpi_addr_t *addr)
968 acpigen_write_resourcetemplate_header();
969 acpigen_write_register(addr);
970 acpigen_write_resourcetemplate_footer();
973 void acpigen_write_irq(u16 mask)
976 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
977 * Byte 0:
978 * Bit7 : 0 => small item
979 * Bit6-3: 0100 (0x4) => IRQ port descriptor
980 * Bit2-0: 010 (0x2) => 2 Bytes long
982 acpigen_emit_byte(0x22);
983 acpigen_emit_byte(mask & 0xff);
984 acpigen_emit_byte((mask >> 8) & 0xff);
987 void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
990 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
991 * Byte 0:
992 * Bit7 : 0 => small item
993 * Bit6-3: 1000 (0x8) => I/O port descriptor
994 * Bit2-0: 111 (0x7) => 7 Bytes long
996 acpigen_emit_byte(0x47);
997 /* Does the device decode all 16 or just 10 bits? */
998 /* bit1-7 are ignored */
999 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1000 /* minimum base address the device may be configured for */
1001 acpigen_emit_byte(min & 0xff);
1002 acpigen_emit_byte((min >> 8) & 0xff);
1003 /* maximum base address the device may be configured for */
1004 acpigen_emit_byte(max & 0xff);
1005 acpigen_emit_byte((max >> 8) & 0xff);
1006 /* alignment for min base */
1007 acpigen_emit_byte(align & 0xff);
1008 acpigen_emit_byte(len & 0xff);
1011 void acpigen_write_resourcetemplate_header(void)
1014 * A ResourceTemplate() is a Buffer() with a
1015 * (Byte|Word|DWord) containing the length, followed by one or more
1016 * resource items, terminated by the end tag.
1017 * (small item 0xf, len 1)
1019 acpigen_emit_byte(BUFFER_OP);
1020 acpigen_write_len_f();
1021 acpigen_emit_byte(WORD_PREFIX);
1022 len_stack[ltop++] = acpigen_get_current();
1023 /* Add 2 dummy bytes for the ACPI word (keep aligned with
1024 the calculation in acpigen_write_resourcetemplate() below). */
1025 acpigen_emit_byte(0x00);
1026 acpigen_emit_byte(0x00);
1029 void acpigen_write_resourcetemplate_footer(void)
1031 char *p = len_stack[--ltop];
1032 int len;
1034 * end tag (acpi 4.0 Section 6.4.2.8)
1035 * 0x79 <checksum>
1036 * 0x00 is treated as a good checksum according to the spec
1037 * and is what iasl generates.
1039 acpigen_emit_byte(0x79);
1040 acpigen_emit_byte(0x00);
1042 /* Start counting past the 2-bytes length added in
1043 acpigen_write_resourcetemplate() above. */
1044 len = acpigen_get_current() - (p + 2);
1046 /* patch len word */
1047 p[0] = len & 0xff;
1048 p[1] = (len >> 8) & 0xff;
1049 /* patch len field */
1050 acpigen_pop_len();
1053 static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1054 struct resource *res)
1056 acpigen_write_mem32fixed(0, res->base, res->size);
1059 static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1060 struct resource *res)
1062 resource_t base = res->base;
1063 resource_t size = res->size;
1064 while (size > 0) {
1065 resource_t sz = size > 255 ? 255 : size;
1066 acpigen_write_io16(base, base, 0, sz, 1);
1067 size -= sz;
1068 base += sz;
1072 void acpigen_write_mainboard_resource_template(void)
1074 acpigen_write_resourcetemplate_header();
1076 /* Add reserved memory ranges. */
1077 search_global_resources(
1078 IORESOURCE_MEM | IORESOURCE_RESERVE,
1079 IORESOURCE_MEM | IORESOURCE_RESERVE,
1080 acpigen_add_mainboard_rsvd_mem32, 0);
1082 /* Add reserved io ranges. */
1083 search_global_resources(
1084 IORESOURCE_IO | IORESOURCE_RESERVE,
1085 IORESOURCE_IO | IORESOURCE_RESERVE,
1086 acpigen_add_mainboard_rsvd_io, 0);
1088 acpigen_write_resourcetemplate_footer();
1091 void acpigen_write_mainboard_resources(const char *scope, const char *name)
1093 acpigen_write_scope(scope);
1094 acpigen_write_name(name);
1095 acpigen_write_mainboard_resource_template();
1096 acpigen_pop_len();
1099 static int hex2bin(const char c)
1101 if (c >= 'A' && c <= 'F')
1102 return c - 'A' + 10;
1103 if (c >= 'a' && c <= 'f')
1104 return c - 'a' + 10;
1105 return c - '0';
1108 void acpigen_emit_eisaid(const char *eisaid)
1110 u32 compact = 0;
1112 /* Clamping individual values would be better but
1113 there is a disagreement over what is a valid
1114 EISA id, so accept anything and don't clamp,
1115 parent code should create a valid EISAid.
1117 compact |= (eisaid[0] - 'A' + 1) << 26;
1118 compact |= (eisaid[1] - 'A' + 1) << 21;
1119 compact |= (eisaid[2] - 'A' + 1) << 16;
1120 compact |= hex2bin(eisaid[3]) << 12;
1121 compact |= hex2bin(eisaid[4]) << 8;
1122 compact |= hex2bin(eisaid[5]) << 4;
1123 compact |= hex2bin(eisaid[6]);
1125 acpigen_emit_byte(0xc);
1126 acpigen_emit_byte((compact >> 24) & 0xff);
1127 acpigen_emit_byte((compact >> 16) & 0xff);
1128 acpigen_emit_byte((compact >> 8) & 0xff);
1129 acpigen_emit_byte(compact & 0xff);
1133 * ToUUID(uuid)
1135 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1136 * order for the bytes that make up a UUID Buffer object.
1137 * UUID byte order for input:
1138 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1139 * UUID byte order for output:
1140 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1142 #define UUID_LEN 16
1143 void acpigen_write_uuid(const char *uuid)
1145 uint8_t buf[UUID_LEN];
1146 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1147 8, 9, 10, 11, 12, 13, 14, 15 };
1149 /* Parse UUID string into bytes */
1150 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1151 return;
1153 /* BufferOp */
1154 acpigen_emit_byte(BUFFER_OP);
1155 acpigen_write_len_f();
1157 /* Buffer length in bytes */
1158 acpigen_write_word(UUID_LEN);
1160 /* Output UUID in expected order */
1161 for (i = 0; i < UUID_LEN; i++)
1162 acpigen_emit_byte(buf[order[i]]);
1164 acpigen_pop_len();
1168 * Name (_PRx, Package(One) { name })
1169 * ...
1170 * PowerResource (name, level, order)
1172 void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1173 const char * const dev_states[], size_t dev_states_count)
1175 size_t i;
1176 for (i = 0; i < dev_states_count; i++) {
1177 acpigen_write_name(dev_states[i]);
1178 acpigen_write_package(1);
1179 acpigen_emit_simple_namestring(name);
1180 acpigen_pop_len(); /* Package */
1183 acpigen_emit_ext_op(POWER_RES_OP);
1185 acpigen_write_len_f();
1187 acpigen_emit_simple_namestring(name);
1188 acpigen_emit_byte(level);
1189 acpigen_emit_word(order);
1192 /* Sleep (ms) */
1193 void acpigen_write_sleep(uint64_t sleep_ms)
1195 acpigen_emit_ext_op(SLEEP_OP);
1196 acpigen_write_integer(sleep_ms);
1199 void acpigen_write_store(void)
1201 acpigen_emit_byte(STORE_OP);
1204 /* Store (src, dst) */
1205 void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1207 acpigen_write_store();
1208 acpigen_emit_byte(src);
1209 acpigen_emit_byte(dst);
1212 /* Store (src, "namestr") */
1213 void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1215 acpigen_write_store();
1216 acpigen_emit_byte(src);
1217 acpigen_emit_namestring(dst);
1220 /* Or (arg1, arg2, res) */
1221 void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1223 acpigen_emit_byte(OR_OP);
1224 acpigen_emit_byte(arg1);
1225 acpigen_emit_byte(arg2);
1226 acpigen_emit_byte(res);
1229 /* Xor (arg1, arg2, res) */
1230 void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1232 acpigen_emit_byte(XOR_OP);
1233 acpigen_emit_byte(arg1);
1234 acpigen_emit_byte(arg2);
1235 acpigen_emit_byte(res);
1238 /* And (arg1, arg2, res) */
1239 void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1241 acpigen_emit_byte(AND_OP);
1242 acpigen_emit_byte(arg1);
1243 acpigen_emit_byte(arg2);
1244 acpigen_emit_byte(res);
1247 /* Not (arg, res) */
1248 void acpigen_write_not(uint8_t arg, uint8_t res)
1250 acpigen_emit_byte(NOT_OP);
1251 acpigen_emit_byte(arg);
1252 acpigen_emit_byte(res);
1255 /* Store (str, DEBUG) */
1256 void acpigen_write_debug_string(const char *str)
1258 acpigen_write_store();
1259 acpigen_write_string(str);
1260 acpigen_emit_ext_op(DEBUG_OP);
1263 /* Store (val, DEBUG) */
1264 void acpigen_write_debug_integer(uint64_t val)
1266 acpigen_write_store();
1267 acpigen_write_integer(val);
1268 acpigen_emit_ext_op(DEBUG_OP);
1271 /* Store (op, DEBUG) */
1272 void acpigen_write_debug_op(uint8_t op)
1274 acpigen_write_store();
1275 acpigen_emit_byte(op);
1276 acpigen_emit_ext_op(DEBUG_OP);
1279 void acpigen_write_if(void)
1281 acpigen_emit_byte(IF_OP);
1282 acpigen_write_len_f();
1285 /* If (And (arg1, arg2)) */
1286 void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1288 acpigen_write_if();
1289 acpigen_emit_byte(AND_OP);
1290 acpigen_emit_byte(arg1);
1291 acpigen_emit_byte(arg2);
1295 * Generates ACPI code for checking if operand1 and operand2 are equal.
1296 * Both operand1 and operand2 are ACPI ops.
1298 * If (Lequal (op,1 op2))
1300 void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1302 acpigen_write_if();
1303 acpigen_emit_byte(LEQUAL_OP);
1304 acpigen_emit_byte(op1);
1305 acpigen_emit_byte(op2);
1309 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1310 * operand1 is ACPI op and operand2 is an integer.
1312 * If (Lequal (op, val))
1314 void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
1316 acpigen_write_if();
1317 acpigen_emit_byte(LEQUAL_OP);
1318 acpigen_emit_byte(op);
1319 acpigen_write_integer(val);
1323 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1324 * operand1 is namestring and operand2 is an integer.
1326 * If (Lequal ("namestr", val))
1328 void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1330 acpigen_write_if();
1331 acpigen_emit_byte(LEQUAL_OP);
1332 acpigen_emit_namestring(namestr);
1333 acpigen_write_integer(val);
1336 void acpigen_write_else(void)
1338 acpigen_emit_byte(ELSE_OP);
1339 acpigen_write_len_f();
1342 void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1344 acpigen_emit_byte(TO_BUFFER_OP);
1345 acpigen_emit_byte(src);
1346 acpigen_emit_byte(dst);
1349 void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1351 acpigen_emit_byte(TO_INTEGER_OP);
1352 acpigen_emit_byte(src);
1353 acpigen_emit_byte(dst);
1356 void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
1358 size_t i;
1360 acpigen_emit_byte(BUFFER_OP);
1361 acpigen_write_len_f();
1362 acpigen_write_integer(size);
1364 for (i = 0; i < size; i++)
1365 acpigen_emit_byte(arr[i]);
1367 acpigen_pop_len();
1370 void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
1372 acpigen_emit_byte(RETURN_OP);
1373 acpigen_write_byte_buffer(arr, size);
1376 void acpigen_write_return_singleton_buffer(uint8_t arg)
1378 acpigen_write_return_byte_buffer(&arg, 1);
1381 void acpigen_write_return_op(uint8_t arg)
1383 acpigen_emit_byte(RETURN_OP);
1384 acpigen_emit_byte(arg);
1387 void acpigen_write_return_byte(uint8_t arg)
1389 acpigen_emit_byte(RETURN_OP);
1390 acpigen_write_byte(arg);
1393 void acpigen_write_return_integer(uint64_t arg)
1395 acpigen_emit_byte(RETURN_OP);
1396 acpigen_write_integer(arg);
1399 void acpigen_write_return_string(const char *arg)
1401 acpigen_emit_byte(RETURN_OP);
1402 acpigen_write_string(arg);
1405 void acpigen_write_upc(enum acpi_upc_type type)
1407 acpigen_write_name("_UPC");
1408 acpigen_write_package(4);
1409 /* Connectable */
1410 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1411 /* Type */
1412 acpigen_write_byte(type);
1413 /* Reserved0 */
1414 acpigen_write_zero();
1415 /* Reserved1 */
1416 acpigen_write_zero();
1417 acpigen_pop_len();
1420 void acpigen_write_pld(const struct acpi_pld *pld)
1422 uint8_t buf[20];
1424 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1425 return;
1427 acpigen_write_name("_PLD");
1428 acpigen_write_package(1);
1429 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
1430 acpigen_pop_len();
1433 void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1434 size_t count, void *arg)
1436 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1437 acpigen_write_dsm_uuid_arr(&id, 1);
1440 static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1442 size_t i;
1444 /* If (LEqual (Local0, ToUUID(uuid))) */
1445 acpigen_write_if();
1446 acpigen_emit_byte(LEQUAL_OP);
1447 acpigen_emit_byte(LOCAL0_OP);
1448 acpigen_write_uuid(id->uuid);
1450 /* ToInteger (Arg2, Local1) */
1451 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1453 for (i = 0; i < id->count; i++) {
1454 /* If (LEqual (Local1, i)) */
1455 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1457 /* Callback to write if handler. */
1458 if (id->callbacks[i])
1459 id->callbacks[i](id->arg);
1461 acpigen_pop_len(); /* If */
1464 /* Default case: Return (Buffer (One) { 0x0 }) */
1465 acpigen_write_return_singleton_buffer(0x0);
1467 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1472 * Generate ACPI AML code for _DSM method.
1473 * This function takes as input array of uuid for the device, set of callbacks
1474 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1475 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1476 * callbacks.
1478 * Arguments passed into _DSM method:
1479 * Arg0 = UUID
1480 * Arg1 = Revision
1481 * Arg2 = Function index
1482 * Arg3 = Function specific arguments
1484 * AML code generated would look like:
1485 * Method (_DSM, 4, Serialized) {
1486 * ToBuffer (Arg0, Local0)
1487 * If (LEqual (Local0, ToUUID(uuid))) {
1488 * ToInteger (Arg2, Local1)
1489 * If (LEqual (Local1, 0)) {
1490 * <acpigen by callback[0]>
1492 * ...
1493 * If (LEqual (Local1, n)) {
1494 * <acpigen by callback[n]>
1496 * Return (Buffer (One) { 0x0 })
1498 * ...
1499 * If (LEqual (Local0, ToUUID(uuidn))) {
1500 * ...
1502 * Return (Buffer (One) { 0x0 })
1505 void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
1507 size_t i;
1509 /* Method (_DSM, 4, Serialized) */
1510 acpigen_write_method_serialized("_DSM", 0x4);
1512 /* ToBuffer (Arg0, Local0) */
1513 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1515 for (i = 0; i < count; i++)
1516 acpigen_write_dsm_uuid(&ids[i]);
1518 /* Return (Buffer (One) { 0x0 }) */
1519 acpigen_write_return_singleton_buffer(0x0);
1521 acpigen_pop_len(); /* Method _DSM */
1524 #define CPPC_PACKAGE_NAME "\\GCPC"
1526 void acpigen_write_CPPC_package(const struct cppc_config *config)
1528 u32 i;
1529 u32 max;
1530 switch (config->version) {
1531 case 1:
1532 max = CPPC_MAX_FIELDS_VER_1;
1533 break;
1534 case 2:
1535 max = CPPC_MAX_FIELDS_VER_2;
1536 break;
1537 case 3:
1538 max = CPPC_MAX_FIELDS_VER_3;
1539 break;
1540 default:
1541 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1542 config->version);
1543 return;
1545 acpigen_write_name(CPPC_PACKAGE_NAME);
1547 /* Adding 2 to account for length and version fields */
1548 acpigen_write_package(max + 2);
1549 acpigen_write_dword(max + 2);
1551 acpigen_write_byte(config->version);
1553 for (i = 0; i < max; ++i) {
1554 const acpi_addr_t *reg = &(config->regs[i]);
1555 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
1556 reg->bit_width == 32 && reg->access_size == 0) {
1557 acpigen_write_dword(reg->addrl);
1558 } else {
1559 acpigen_write_register_resource(reg);
1562 acpigen_pop_len();
1565 void acpigen_write_CPPC_method(void)
1567 acpigen_write_method("_CPC", 0);
1568 acpigen_emit_byte(RETURN_OP);
1569 acpigen_emit_namestring(CPPC_PACKAGE_NAME);
1570 acpigen_pop_len();
1574 * Generate ACPI AML code for _ROM method.
1575 * This function takes as input ROM data and ROM length.
1577 * The ACPI spec isn't clear about what should happen at the end of the
1578 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1579 * bytes in the returned buffer with zeros.
1581 * Arguments passed into _DSM method:
1582 * Arg0 = Offset in Bytes
1583 * Arg1 = Bytes to return
1585 * Example:
1586 * acpigen_write_rom(0xdeadbeef, 0x10000)
1588 * AML code generated would look like:
1589 * Method (_ROM, 2, NotSerialized) {
1591 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1592 * Field (ROMS, AnyAcc, NoLock, Preserve)
1594 * Offset (0),
1595 * RBF0, 0x80000
1598 * Store (Arg0, Local0)
1599 * Store (Arg1, Local1)
1601 * If (LGreater (Local1, 0x1000))
1603 * Store (0x1000, Local1)
1606 * Store (Local1, Local3)
1608 * If (LGreater (Local0, 0x10000))
1610 * Return(Buffer(Local1){0})
1613 * If (LGreater (Local0, 0x0f000))
1615 * Subtract (0x10000, Local0, Local2)
1616 * If (LGreater (Local1, Local2))
1618 * Store (Local2, Local1)
1622 * Name (ROM1, Buffer (Local3) {0})
1624 * Multiply (Local0, 0x08, Local0)
1625 * Multiply (Local1, 0x08, Local1)
1627 * CreateField (RBF0, Local0, Local1, TMPB)
1628 * Store (TMPB, ROM1)
1629 * Return (ROM1)
1633 void acpigen_write_rom(void *bios, const size_t length)
1635 ASSERT(bios)
1636 ASSERT(length)
1638 /* Method (_ROM, 2, Serialized) */
1639 acpigen_write_method_serialized("_ROM", 2);
1641 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1642 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1643 (uintptr_t)bios, length);
1644 acpigen_write_opregion(&opreg);
1646 struct fieldlist l[] = {
1647 FIELDLIST_OFFSET(0),
1648 FIELDLIST_NAMESTR("RBF0", 8 * length),
1651 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1653 * Offset (0),
1654 * RBF0, 0x80000
1655 * } */
1656 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1657 FIELD_NOLOCK | FIELD_PRESERVE);
1659 /* Store (Arg0, Local0) */
1660 acpigen_write_store();
1661 acpigen_emit_byte(ARG0_OP);
1662 acpigen_emit_byte(LOCAL0_OP);
1664 /* Store (Arg1, Local1) */
1665 acpigen_write_store();
1666 acpigen_emit_byte(ARG1_OP);
1667 acpigen_emit_byte(LOCAL1_OP);
1669 /* ACPI SPEC requires to return at maximum 4KiB */
1670 /* If (LGreater (Local1, 0x1000)) */
1671 acpigen_write_if();
1672 acpigen_emit_byte(LGREATER_OP);
1673 acpigen_emit_byte(LOCAL1_OP);
1674 acpigen_write_integer(0x1000);
1676 /* Store (0x1000, Local1) */
1677 acpigen_write_store();
1678 acpigen_write_integer(0x1000);
1679 acpigen_emit_byte(LOCAL1_OP);
1681 /* Pop if */
1682 acpigen_pop_len();
1684 /* Store (Local1, Local3) */
1685 acpigen_write_store();
1686 acpigen_emit_byte(LOCAL1_OP);
1687 acpigen_emit_byte(LOCAL3_OP);
1689 /* If (LGreater (Local0, length)) */
1690 acpigen_write_if();
1691 acpigen_emit_byte(LGREATER_OP);
1692 acpigen_emit_byte(LOCAL0_OP);
1693 acpigen_write_integer(length);
1695 /* Return(Buffer(Local1){0}) */
1696 acpigen_emit_byte(RETURN_OP);
1697 acpigen_emit_byte(BUFFER_OP);
1698 acpigen_write_len_f();
1699 acpigen_emit_byte(LOCAL1_OP);
1700 acpigen_emit_byte(0);
1701 acpigen_pop_len();
1703 /* Pop if */
1704 acpigen_pop_len();
1706 /* If (LGreater (Local0, length - 4096)) */
1707 acpigen_write_if();
1708 acpigen_emit_byte(LGREATER_OP);
1709 acpigen_emit_byte(LOCAL0_OP);
1710 acpigen_write_integer(length - 4096);
1712 /* Subtract (length, Local0, Local2) */
1713 acpigen_emit_byte(SUBTRACT_OP);
1714 acpigen_write_integer(length);
1715 acpigen_emit_byte(LOCAL0_OP);
1716 acpigen_emit_byte(LOCAL2_OP);
1718 /* If (LGreater (Local1, Local2)) */
1719 acpigen_write_if();
1720 acpigen_emit_byte(LGREATER_OP);
1721 acpigen_emit_byte(LOCAL1_OP);
1722 acpigen_emit_byte(LOCAL2_OP);
1724 /* Store (Local2, Local1) */
1725 acpigen_write_store();
1726 acpigen_emit_byte(LOCAL2_OP);
1727 acpigen_emit_byte(LOCAL1_OP);
1729 /* Pop if */
1730 acpigen_pop_len();
1732 /* Pop if */
1733 acpigen_pop_len();
1735 /* Name (ROM1, Buffer (Local3) {0}) */
1736 acpigen_write_name("ROM1");
1737 acpigen_emit_byte(BUFFER_OP);
1738 acpigen_write_len_f();
1739 acpigen_emit_byte(LOCAL3_OP);
1740 acpigen_emit_byte(0);
1741 acpigen_pop_len();
1743 /* Multiply (Local1, 0x08, Local1) */
1744 acpigen_emit_byte(MULTIPLY_OP);
1745 acpigen_emit_byte(LOCAL1_OP);
1746 acpigen_write_integer(0x08);
1747 acpigen_emit_byte(LOCAL1_OP);
1749 /* Multiply (Local0, 0x08, Local0) */
1750 acpigen_emit_byte(MULTIPLY_OP);
1751 acpigen_emit_byte(LOCAL0_OP);
1752 acpigen_write_integer(0x08);
1753 acpigen_emit_byte(LOCAL0_OP);
1755 /* CreateField (RBF0, Local0, Local1, TMPB) */
1756 acpigen_emit_ext_op(CREATEFIELD_OP);
1757 acpigen_emit_namestring("RBF0");
1758 acpigen_emit_byte(LOCAL0_OP);
1759 acpigen_emit_byte(LOCAL1_OP);
1760 acpigen_emit_namestring("TMPB");
1762 /* Store (TMPB, ROM1) */
1763 acpigen_write_store();
1764 acpigen_emit_namestring("TMPB");
1765 acpigen_emit_namestring("ROM1");
1767 /* Return (ROM1) */
1768 acpigen_emit_byte(RETURN_OP);
1769 acpigen_emit_namestring("ROM1");
1771 /* Pop method */
1772 acpigen_pop_len();
1776 /* Soc-implemented functions -- weak definitions. */
1777 int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
1779 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1780 acpigen_write_debug_string("read_rx_gpio not available");
1781 return -1;
1784 int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
1786 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1787 acpigen_write_debug_string("get_tx_gpio not available");
1788 return -1;
1791 int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
1793 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1794 acpigen_write_debug_string("set_tx_gpio not available");
1795 return -1;
1798 int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
1800 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1801 acpigen_write_debug_string("clear_tx_gpio not available");
1802 return -1;
1806 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1807 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1808 * make callbacks into SoC acpigen code.
1810 * Returns 0 on success and -1 on error.
1812 int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1814 if (gpio->active_low)
1815 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1816 else
1817 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1820 int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1822 if (gpio->active_low)
1823 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1824 else
1825 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1828 void acpigen_get_rx_gpio(struct acpi_gpio *gpio)
1830 acpigen_soc_read_rx_gpio(gpio->pins[0]);
1832 if (gpio->active_low)
1833 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1836 void acpigen_get_tx_gpio(struct acpi_gpio *gpio)
1838 acpigen_soc_get_tx_gpio(gpio->pins[0]);
1840 if (gpio->active_low)
1841 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1844 /* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
1845 void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
1846 u16 range_min, u16 range_max, u16 translation, u16 length)
1848 acpigen_emit_byte(0x88);
1849 /* Byte 1+2: length (0x000d) */
1850 acpigen_emit_byte(0x0d);
1851 acpigen_emit_byte(0x00);
1852 /* resource type */
1853 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1854 /* general flags */
1855 acpigen_emit_byte(gen_flags);
1856 /* type flags */
1857 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1858 acpigen_emit_byte(type_flags);
1859 /* granularity, min, max, translation, length */
1860 acpigen_emit_word(gran);
1861 acpigen_emit_word(range_min);
1862 acpigen_emit_word(range_max);
1863 acpigen_emit_word(translation);
1864 acpigen_emit_word(length);
1867 /* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
1868 void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
1869 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
1871 acpigen_emit_byte(0x87);
1872 /* Byte 1+2: length (0023) */
1873 acpigen_emit_byte(23);
1874 acpigen_emit_byte(0x00);
1875 /* resource type */
1876 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1877 /* general flags */
1878 acpigen_emit_byte(gen_flags);
1879 /* type flags */
1880 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1881 acpigen_emit_byte(type_flags);
1882 /* granularity, min, max, translation, length */
1883 acpigen_emit_dword(gran);
1884 acpigen_emit_dword(range_min);
1885 acpigen_emit_dword(range_max);
1886 acpigen_emit_dword(translation);
1887 acpigen_emit_dword(length);
1890 static void acpigen_emit_qword(u64 data)
1892 acpigen_emit_dword(data & 0xffffffff);
1893 acpigen_emit_dword((data >> 32) & 0xffffffff);
1896 /* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
1897 void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
1898 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
1900 acpigen_emit_byte(0x8a);
1901 /* Byte 1+2: length (0x002b) */
1902 acpigen_emit_byte(0x2b);
1903 acpigen_emit_byte(0x00);
1904 /* resource type */
1905 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1906 /* general flags */
1907 acpigen_emit_byte(gen_flags);
1908 /* type flags */
1909 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1910 acpigen_emit_byte(type_flags);
1911 /* granularity, min, max, translation, length */
1912 acpigen_emit_qword(gran);
1913 acpigen_emit_qword(range_min);
1914 acpigen_emit_qword(range_max);
1915 acpigen_emit_qword(translation);
1916 acpigen_emit_qword(length);
1919 void acpigen_write_ADR(uint64_t adr)
1921 acpigen_write_name_qword("_ADR", adr);
1924 void acpigen_write_ADR_pci_devfn(pci_devfn_t devfn)
1927 * _ADR for PCI Bus is encoded as follows:
1928 * [63:32] - unused
1929 * [31:16] - device #
1930 * [15:0] - function #
1932 acpigen_write_ADR(PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn));
1935 void acpigen_write_ADR_pci_device(const struct device *dev)
1937 assert(dev->path.type == DEVICE_PATH_PCI);
1938 acpigen_write_ADR_pci_devfn(dev->path.pci.devfn);
1942 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
1943 * @address: SoundWire device address properties.
1945 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
1947 * 63..52 - Reserved (0)
1948 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
1949 * Used when a Controller has multiple master devices, each producing a
1950 * separate SoundWire Link. Set to 0 for single-link controllers.
1951 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
1952 * 47..44 - SoundWire specification version that this device supports
1953 * 43..40 - Unique ID for multiple devices
1954 * 39..24 - MIPI standard manufacturer code
1955 * 23..08 - Vendor defined part ID
1956 * 07..00 - MIPI class encoding
1958 void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
1960 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
1961 (((uint64_t)address->version & 0xf) << 44) |
1962 (((uint64_t)address->unique_id & 0xf) << 40) |
1963 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
1964 (((uint64_t)address->part_id & 0xffff) << 8) |
1965 (((uint64_t)address->class & 0xff)));
1968 void acpigen_notify(const char *namestr, int value)
1970 acpigen_emit_byte(NOTIFY_OP);
1971 acpigen_emit_namestring(namestr);
1972 acpigen_write_integer(value);