1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
5  * Copyright (c) 2022 Intel Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 
42 #include "dm.h"
43 #include "log.h"
44 #include "hsm_ioctl_defs.h"
45 #include "acpi.h"
46 
47 static int lapic_ids[ACRN_PLATFORM_LAPIC_IDS_MAX] = {0xff};
48 
49 /* pcpuid is the processor local APIC instance index in MADT */
lapicid_from_pcpuid(int pcpu_id)50 int lapicid_from_pcpuid(int pcpu_id)
51 {
52 	if (pcpu_id < 0 || pcpu_id >= ACRN_PLATFORM_LAPIC_IDS_MAX ||
53 		lapic_ids[pcpu_id] == 0xff) {
54 		return -1;
55 	}
56 	return (int)lapic_ids[pcpu_id];
57 }
58 
lapic_to_pcpu(int lapic)59 int lapic_to_pcpu(int lapic)
60 {
61 	int j;
62 
63 	for (j = 0; j < ACRN_PLATFORM_LAPIC_IDS_MAX; j++) {
64 		if (lapic == lapicid_from_pcpuid(j)) {
65 			return j;
66 		}
67 	}
68 	return ACRN_PLATFORM_LAPIC_IDS_MAX - 1;
69 }
70 
71 static int
local_parse_madt(struct acpi_table_madt * madt)72 local_parse_madt(struct acpi_table_madt *madt)
73 {
74 	uint16_t pcpu_num = 0U;
75 	int ret = 0;
76 	struct acpi_madt_local_apic *processor;
77 	struct acpi_table_madt *madt_ptr;
78 	void *first, *end, *iterator;
79 	struct acpi_subtable_header *entry;
80 
81 	madt_ptr = madt;
82 
83 	first = madt_ptr + 1;
84 	end = (void *)madt_ptr + madt_ptr->header.length;
85 
86 	for (iterator = first; (iterator) < (end); iterator += entry->length) {
87 		entry = (struct acpi_subtable_header *)iterator;
88 		if (entry->length < sizeof(struct acpi_subtable_header)) {
89 			break;
90 		}
91 
92 		if (entry->type == ACPI_MADT_TYPE_LOCAL_APIC) {
93 			processor = (struct acpi_madt_local_apic *)iterator;
94 			if ((processor->lapic_flags & ACPI_MADT_ENABLED) != 0U) {
95 				if (pcpu_num < ACRN_PLATFORM_LAPIC_IDS_MAX) {
96 					lapic_ids[pcpu_num] = processor->id;
97 				}
98 				pcpu_num++;
99 			}
100 		}
101 	}
102 
103 	if (pcpu_num == 0) {
104 		ret = -1;
105 	}
106 	return ret;
107 }
108 
109 /*
110  * There has an assumption. The Service VM owned pcpu starts from physical cpu 0,
111  * otherwise Service VM doesn't know the mapping relationship between its vcpu0
112  * and pcpu_id.
113  */
parse_madt(void)114 int parse_madt(void)
115 {
116 	int ret = 0U;
117 	ssize_t size;
118 	struct acpi_table_madt *madt;
119 	struct stat file_state;
120 
121 	int fd = open("/sys/firmware/acpi/tables/APIC", O_RDONLY);
122 	if (fd < 0) {
123 		pr_err("Failed to open the Service VM APIC file.\n");
124 		return -1;
125 	}
126 
127 	if (fstat(fd, &file_state) == -1) {
128 		pr_err("Failed to get APIC file state.\n");
129 		close(fd);
130 		return -1;
131 	}
132 
133 	madt = (struct acpi_table_madt *)malloc(file_state.st_size);
134 	if (madt == NULL) {
135 		pr_err("Failed to malloc %d bytes to store the MADT table.\n", file_state.st_size);
136 		close(fd);
137 		return -1;
138 	}
139 	size = read(fd, madt, file_state.st_size);
140 	if (size == file_state.st_size) {
141 		ret = local_parse_madt(madt);
142 	} else {
143 		pr_err("Failed to read Service VM MADT table.\n");
144 		ret = -1;
145 	}
146 
147 	free(madt);
148 	close(fd);
149 	return ret;
150 }
151