1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
4  * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee>
5  */
6 
7 #define LOG_CATEGORY UCLASS_QFW
8 
9 #include <common.h>
10 #include <bootdev.h>
11 #include <bootflow.h>
12 #include <bootmeth.h>
13 #include <command.h>
14 #include <errno.h>
15 #include <log.h>
16 #include <malloc.h>
17 #include <qfw.h>
18 #include <dm.h>
19 #include <misc.h>
20 #include <tables_csum.h>
21 
22 #if defined(CONFIG_GENERATE_ACPI_TABLE) && !defined(CONFIG_SANDBOX)
23 /*
24  * This function allocates memory for ACPI tables
25  *
26  * @entry : BIOS linker command entry which tells where to allocate memory
27  *          (either high memory or low memory)
28  * @addr  : The address that should be used for low memory allcation. If the
29  *          memory allocation request is 'ZONE_HIGH' then this parameter will
30  *          be ignored.
31  * @return: 0 on success, or negative value on failure
32  */
bios_linker_allocate(struct udevice * dev,struct bios_linker_entry * entry,ulong * addr)33 static int bios_linker_allocate(struct udevice *dev,
34 				struct bios_linker_entry *entry, ulong *addr)
35 {
36 	uint32_t size, align;
37 	struct fw_file *file;
38 	unsigned long aligned_addr;
39 
40 	align = le32_to_cpu(entry->alloc.align);
41 	/* align must be power of 2 */
42 	if (align & (align - 1)) {
43 		printf("error: wrong alignment %u\n", align);
44 		return -EINVAL;
45 	}
46 
47 	file = qfw_find_file(dev, entry->alloc.file);
48 	if (!file) {
49 		printf("error: can't find file %s\n", entry->alloc.file);
50 		return -ENOENT;
51 	}
52 
53 	size = be32_to_cpu(file->cfg.size);
54 
55 	/*
56 	 * ZONE_HIGH means we need to allocate from high memory, since
57 	 * malloc space is already at the end of RAM, so we directly use it.
58 	 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
59 	 * in which is low memory
60 	 */
61 	if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
62 		aligned_addr = (unsigned long)memalign(align, size);
63 		if (!aligned_addr) {
64 			printf("error: allocating resource\n");
65 			return -ENOMEM;
66 		}
67 	} else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
68 		aligned_addr = ALIGN(*addr, align);
69 	} else {
70 		printf("error: invalid allocation zone\n");
71 		return -EINVAL;
72 	}
73 
74 	debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
75 	      file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
76 
77 	qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size,
78 		       (void *)aligned_addr);
79 	file->addr = aligned_addr;
80 
81 	/* adjust address for low memory allocation */
82 	if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
83 		*addr = (aligned_addr + size);
84 
85 	return 0;
86 }
87 
88 /*
89  * This function patches ACPI tables previously loaded
90  * by bios_linker_allocate()
91  *
92  * @entry : BIOS linker command entry which tells how to patch
93  *          ACPI tables
94  * @return: 0 on success, or negative value on failure
95  */
bios_linker_add_pointer(struct udevice * dev,struct bios_linker_entry * entry)96 static int bios_linker_add_pointer(struct udevice *dev,
97 				   struct bios_linker_entry *entry)
98 {
99 	struct fw_file *dest, *src;
100 	uint32_t offset = le32_to_cpu(entry->pointer.offset);
101 	uint64_t pointer = 0;
102 
103 	dest = qfw_find_file(dev, entry->pointer.dest_file);
104 	if (!dest || !dest->addr)
105 		return -ENOENT;
106 	src = qfw_find_file(dev, entry->pointer.src_file);
107 	if (!src || !src->addr)
108 		return -ENOENT;
109 
110 	debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
111 	      dest->addr, src->addr, offset, entry->pointer.size, pointer);
112 
113 	memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
114 	pointer	= le64_to_cpu(pointer);
115 	pointer += (unsigned long)src->addr;
116 	pointer	= cpu_to_le64(pointer);
117 	memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
118 
119 	return 0;
120 }
121 
122 /*
123  * This function updates checksum fields of ACPI tables previously loaded
124  * by bios_linker_allocate()
125  *
126  * @entry : BIOS linker command entry which tells where to update ACPI table
127  *          checksums
128  * @return: 0 on success, or negative value on failure
129  */
bios_linker_add_checksum(struct udevice * dev,struct bios_linker_entry * entry)130 static int bios_linker_add_checksum(struct udevice *dev,
131 				    struct bios_linker_entry *entry)
132 {
133 	struct fw_file *file;
134 	uint8_t *data, cksum = 0;
135 	uint8_t *cksum_start;
136 
137 	file = qfw_find_file(dev, entry->cksum.file);
138 	if (!file || !file->addr)
139 		return -ENOENT;
140 
141 	data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
142 	cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
143 	cksum = table_compute_checksum(cksum_start,
144 				       le32_to_cpu(entry->cksum.length));
145 	*data = cksum;
146 
147 	return 0;
148 }
149 
150 /* This function loads and patches ACPI tables provided by QEMU */
write_acpi_tables(ulong addr)151 ulong write_acpi_tables(ulong addr)
152 {
153 	int i, ret;
154 	struct fw_file *file;
155 	struct bios_linker_entry *table_loader;
156 	struct bios_linker_entry *entry;
157 	uint32_t size;
158 	struct udevice *dev;
159 
160 	ret = qfw_get_dev(&dev);
161 	if (ret) {
162 		printf("error: no qfw\n");
163 		return addr;
164 	}
165 
166 	/* make sure fw_list is loaded */
167 	ret = qfw_read_firmware_list(dev);
168 	if (ret) {
169 		printf("error: can't read firmware file list\n");
170 		return addr;
171 	}
172 
173 	file = qfw_find_file(dev, "etc/table-loader");
174 	if (!file) {
175 		printf("error: can't find etc/table-loader\n");
176 		return addr;
177 	}
178 
179 	size = be32_to_cpu(file->cfg.size);
180 	if ((size % sizeof(*entry)) != 0) {
181 		printf("error: table-loader maybe corrupted\n");
182 		return addr;
183 	}
184 
185 	table_loader = malloc(size);
186 	if (!table_loader) {
187 		printf("error: no memory for table-loader\n");
188 		return addr;
189 	}
190 
191 	qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader);
192 
193 	for (i = 0; i < (size / sizeof(*entry)); i++) {
194 		entry = table_loader + i;
195 		switch (le32_to_cpu(entry->command)) {
196 		case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
197 			ret = bios_linker_allocate(dev, entry, &addr);
198 			if (ret)
199 				goto out;
200 			break;
201 		case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
202 			ret = bios_linker_add_pointer(dev, entry);
203 			if (ret)
204 				goto out;
205 			break;
206 		case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
207 			ret = bios_linker_add_checksum(dev, entry);
208 			if (ret)
209 				goto out;
210 			break;
211 		default:
212 			break;
213 		}
214 	}
215 
216 out:
217 	if (ret) {
218 		struct fw_cfg_file_iter iter;
219 		for (file = qfw_file_iter_init(dev, &iter);
220 		     !qfw_file_iter_end(&iter);
221 		     file = qfw_file_iter_next(&iter)) {
222 			if (file->addr) {
223 				free((void *)file->addr);
224 				file->addr = 0;
225 			}
226 		}
227 	}
228 
229 	free(table_loader);
230 	return addr;
231 }
232 
acpi_get_rsdp_addr(void)233 ulong acpi_get_rsdp_addr(void)
234 {
235 	int ret;
236 	struct fw_file *file;
237 	struct udevice *dev;
238 
239 	ret = qfw_get_dev(&dev);
240 	if (ret) {
241 		printf("error: no qfw\n");
242 		return 0;
243 	}
244 
245 	file = qfw_find_file(dev, "etc/acpi/rsdp");
246 	return file->addr;
247 }
248 #endif
249 
qfw_read_entry_io(struct qfw_dev * qdev,u16 entry,u32 size,void * address)250 static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
251 			      void *address)
252 {
253 	struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
254 
255 	debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
256 	      address);
257 
258 	ops->read_entry_io(qdev->dev, entry, size, address);
259 }
260 
qfw_read_entry_dma(struct qfw_dev * qdev,u16 entry,u32 size,void * address)261 static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
262 			       void *address)
263 {
264 	struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
265 
266 	struct qfw_dma dma = {
267 		.length = cpu_to_be32(size),
268 		.address = cpu_to_be64((uintptr_t)address),
269 		.control = cpu_to_be32(FW_CFG_DMA_READ),
270 	};
271 
272 	/*
273 	 * writing FW_CFG_INVALID will cause read operation to resume at last
274 	 * offset, otherwise read will start at offset 0
275 	 */
276 	if (entry != FW_CFG_INVALID)
277 		dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
278 
279 	debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
280 	      entry, size, address, be32_to_cpu(dma.control));
281 
282 	barrier();
283 
284 	ops->read_entry_dma(qdev->dev, &dma);
285 }
286 
qfw_read_entry(struct udevice * dev,u16 entry,u32 size,void * address)287 void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
288 {
289 	struct qfw_dev *qdev = dev_get_uclass_priv(dev);
290 
291 	if (qdev->dma_present)
292 		qfw_read_entry_dma(qdev, entry, size, address);
293 	else
294 		qfw_read_entry_io(qdev, entry, size, address);
295 }
296 
qfw_register(struct udevice * dev)297 int qfw_register(struct udevice *dev)
298 {
299 	struct qfw_dev *qdev = dev_get_uclass_priv(dev);
300 	u32 qemu, dma_enabled;
301 
302 	qdev->dev = dev;
303 	INIT_LIST_HEAD(&qdev->fw_list);
304 
305 	qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
306 	if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
307 		return -ENODEV;
308 
309 	qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
310 	if (dma_enabled & FW_CFG_DMA_ENABLED)
311 		qdev->dma_present = true;
312 
313 	return 0;
314 }
315 
qfw_post_bind(struct udevice * dev)316 static int qfw_post_bind(struct udevice *dev)
317 {
318 	int ret;
319 
320 	ret = bootdev_setup_for_dev(dev, "qfw_bootdev");
321 	if (ret)
322 		return log_msg_ret("dev", ret);
323 
324 	return 0;
325 }
326 
qfw_get_bootflow(struct udevice * dev,struct bootflow_iter * iter,struct bootflow * bflow)327 static int qfw_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
328 			    struct bootflow *bflow)
329 {
330 	const struct udevice *media = dev_get_parent(dev);
331 	int ret;
332 
333 	if (!CONFIG_IS_ENABLED(BOOTSTD))
334 		return -ENOSYS;
335 
336 	log_debug("media=%s\n", media->name);
337 	ret = bootmeth_check(bflow->method, iter);
338 	if (ret)
339 		return log_msg_ret("check", ret);
340 
341 	log_debug("iter->part=%d\n", iter->part);
342 
343 	/* We only support the whole device, not partitions */
344 	if (iter->part)
345 		return log_msg_ret("max", -ESHUTDOWN);
346 
347 	log_debug("reading bootflow with method: %s\n", bflow->method->name);
348 	ret = bootmeth_read_bootflow(bflow->method, bflow);
349 	if (ret)
350 		return log_msg_ret("method", ret);
351 
352 	return 0;
353 }
354 
qfw_bootdev_bind(struct udevice * dev)355 static int qfw_bootdev_bind(struct udevice *dev)
356 {
357 	struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
358 
359 	ucp->prio = BOOTDEVP_4_SCAN_FAST;
360 
361 	return 0;
362 }
363 
qfw_bootdev_hunt(struct bootdev_hunter * info,bool show)364 static int qfw_bootdev_hunt(struct bootdev_hunter *info, bool show)
365 {
366 	int ret;
367 
368 	ret = uclass_probe_all(UCLASS_QFW);
369 	if (ret && ret != -ENOENT)
370 		return log_msg_ret("vir", ret);
371 
372 	return 0;
373 }
374 
375 UCLASS_DRIVER(qfw) = {
376 	.id		= UCLASS_QFW,
377 	.name		= "qfw",
378 	.post_bind	= qfw_post_bind,
379 	.per_device_auto	= sizeof(struct qfw_dev),
380 };
381 
382 struct bootdev_ops qfw_bootdev_ops = {
383 	.get_bootflow	= qfw_get_bootflow,
384 };
385 
386 static const struct udevice_id qfw_bootdev_ids[] = {
387 	{ .compatible = "u-boot,bootdev-qfw" },
388 	{ }
389 };
390 
391 U_BOOT_DRIVER(qfw_bootdev) = {
392 	.name		= "qfw_bootdev",
393 	.id		= UCLASS_BOOTDEV,
394 	.ops		= &qfw_bootdev_ops,
395 	.bind		= qfw_bootdev_bind,
396 	.of_match	= qfw_bootdev_ids,
397 };
398 
399 BOOTDEV_HUNTER(qfw_bootdev_hunter) = {
400 	.prio		= BOOTDEVP_4_SCAN_FAST,
401 	.uclass		= UCLASS_QFW,
402 	.hunt		= qfw_bootdev_hunt,
403 	.drv		= DM_DRIVER_REF(qfw_bootdev),
404 };
405