1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright 2024 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
13 #include <dm/ofnode.h>
14 #include "upl_common.h"
17 * read_addr() - Read an address
19 * Reads an address in the correct format, either 32- or 64-bit
22 * @node: Node to read from
23 * @prop: Property name to read
24 * @addr: Place to put the address
25 * Return: 0 if OK, -ve on error
27 static int read_addr(const struct upl *upl, ofnode node, const char *prop,
32 if (upl->addr_cells == 1) {
35 ret = ofnode_read_u32(node, prop, &val);
41 ret = ofnode_read_u64(node, prop, &val);
50 * read_size() - Read a size
52 * Reads a size in the correct format, either 32- or 64-bit
55 * @node: Node to read from
56 * @prop: Property name to read
57 * @addr: Place to put the size
58 * Return: 0 if OK, -ve on error
60 static int read_size(const struct upl *upl, ofnode node, const char *prop,
65 if (upl->size_cells == 1) {
68 ret = ofnode_read_u32(node, prop, &val);
74 ret = ofnode_read_u64(node, prop, &val);
83 * ofnode_read_bitmask() - Read a bit mask from a string list
85 * @node: Node to read from
86 * @prop: Property name to read
87 * @names: Array of names for each bit
88 * @count: Number of array entries
89 * @value: Returns resulting bit-mask value on success
90 * Return: 0 if OK, -EINVAL if a bit number is not defined, -ENOSPC if the
91 * string is too long for the (internal) buffer, -EINVAL if no such property
93 static int ofnode_read_bitmask(ofnode node, const char *prop,
94 const char *const names[], uint count,
103 ret = ofnode_read_string_list(node, prop, &list);
105 return log_msg_ret("rea", ret);
108 for (strp = list; *strp; strp++) {
109 const char *str = *strp;
112 for (bit = 0; bit < count; bit++) {
113 if (!strcmp(str, names[bit])) {
121 log_warning("%s/%s: Invalid value '%s'\n",
122 ofnode_get_name(node), prop, str);
130 * ofnode_read_value() - Read a string value as an int using a lookup
132 * @node: Node to read from
133 * @prop: Property name to read
134 * @names: Array of names for each int value
135 * @count: Number of array entries
136 * @valuep: Returns int value read
137 * Return: 0 if OK, -EINVAL if a bit number is not defined, -ENOENT if the
138 * property does not exist
140 static int ofnode_read_value(ofnode node, const char *prop,
141 const char *const names[], uint count,
147 str = ofnode_read_string(node, prop);
149 return log_msg_ret("rd", -ENOENT);
151 for (i = 0; i < count; i++) {
152 if (!strcmp(names[i], str)) {
158 log_debug("Unnamed value '%s'\n", str);
159 return log_msg_ret("val", -EINVAL);
162 static int read_uint(ofnode node, const char *prop, uint *valp)
167 ret = ofnode_read_u32(node, prop, &val);
176 * decode_root_props() - Decode root properties from the tree
179 * @node: Node to decode
180 * Return 0 if OK, -ve on error
182 static int decode_root_props(struct upl *upl, ofnode node)
186 ret = read_uint(node, UPLP_ADDRESS_CELLS, &upl->addr_cells);
188 ret = read_uint(node, UPLP_SIZE_CELLS, &upl->size_cells);
190 return log_msg_ret("cel", ret);
196 * decode_root_props() - Decode UPL parameters from the tree
199 * @node: Node to decode
200 * Return 0 if OK, -ve on error
202 static int decode_upl_params(struct upl *upl, ofnode options)
207 node = ofnode_find_subnode(options, UPLN_UPL_PARAMS);
208 if (!ofnode_valid(node))
209 return log_msg_ret("par", -EINVAL);
210 log_debug("decoding '%s'\n", ofnode_get_name(node));
212 ret = read_addr(upl, node, UPLP_SMBIOS, &upl->smbios);
214 return log_msg_ret("smb", ret);
215 ret = read_addr(upl, node, UPLP_ACPI, &upl->acpi);
217 return log_msg_ret("acp", ret);
218 ret = ofnode_read_bitmask(node, UPLP_BOOTMODE, bootmode_names,
219 UPLBM_COUNT, &upl->bootmode);
221 return log_msg_ret("boo", ret);
222 ret = read_uint(node, UPLP_ADDR_WIDTH, &upl->addr_width);
224 return log_msg_ret("add", ret);
225 ret = read_uint(node, UPLP_ACPI_NVS_SIZE, &upl->acpi_nvs_size);
227 return log_msg_ret("nvs", ret);
233 * decode_upl_images() - Decode /options/upl-image nodes
235 * @node: /options node in which to look for the node
236 * Return 0 if OK, -ve on error
238 static int decode_upl_images(struct upl *upl, ofnode options)
243 images = ofnode_find_subnode(options, UPLN_UPL_IMAGE);
244 if (!ofnode_valid(images))
245 return log_msg_ret("img", -EINVAL);
246 log_debug("decoding '%s'\n", ofnode_get_name(images));
248 ret = read_addr(upl, images, UPLP_FIT, &upl->fit);
250 ret = read_uint(images, UPLP_CONF_OFFSET, &upl->conf_offset);
252 return log_msg_ret("cnf", ret);
254 ofnode_for_each_subnode(node, images) {
255 struct upl_image img;
257 ret = read_addr(upl, node, UPLP_LOAD, &img.load);
259 ret = read_size(upl, node, UPLP_SIZE, &img.size);
261 ret = read_uint(node, UPLP_OFFSET, &img.offset);
262 img.description = ofnode_read_string(node, UPLP_DESCRIPTION);
263 if (!img.description)
264 return log_msg_ret("sim", ret);
265 if (!alist_add(&upl->image, img))
266 return log_msg_ret("img", -ENOMEM);
273 * decode_addr_size() - Decide a set of addr/size pairs
275 * Each base/size value from the devicetree is written to the region list
278 * @buf: Bytes to decode
279 * @size: Number of bytes to decode
280 * @regions: List of regions to process (struct memregion)
281 * Returns: number of regions found, if OK, else -ve on error
283 static int decode_addr_size(const struct upl *upl, const char *buf, int size,
284 struct alist *regions)
286 const char *ptr, *end = buf + size;
289 alist_init_struct(regions, struct memregion);
291 for (i = 0; ptr < end; i++) {
292 struct memregion reg;
294 if (upl->addr_cells == 1)
295 reg.base = fdt32_to_cpu(*(u32 *)ptr);
297 reg.base = fdt64_to_cpu(*(u64 *)ptr);
298 ptr += upl->addr_cells * sizeof(u32);
300 if (upl->size_cells == 1)
301 reg.size = fdt32_to_cpu(*(u32 *)ptr);
303 reg.size = fdt64_to_cpu(*(u64 *)ptr);
304 ptr += upl->size_cells * sizeof(u32);
308 if (!alist_add(regions, reg))
309 return log_msg_ret("reg", -ENOMEM);
316 * node_matches_at() - Check if a node name matches "base@..."
318 * Return: true if the node name matches the base string followed by an @ sign;
321 static bool node_matches_at(ofnode node, const char *base)
323 const char *name = ofnode_get_name(node);
324 int len = strlen(base);
326 return !strncmp(base, name, len) && name[len] == '@';
330 * decode_upl_memory_node() - Decode a /memory node from the tree
333 * @node: Node to decode
334 * Return 0 if OK, -ve on error
336 static int decode_upl_memory_node(struct upl *upl, ofnode node)
342 buf = ofnode_read_prop(node, UPLP_REG, &size);
344 log_warning("Node '%s': Missing '%s' property\n",
345 ofnode_get_name(node), UPLP_REG);
346 return log_msg_ret("reg", -EINVAL);
348 len = decode_addr_size(upl, buf, size, &mem.region);
350 return log_msg_ret("buf", len);
351 mem.hotpluggable = ofnode_read_bool(node, UPLP_HOTPLUGGABLE);
352 if (!alist_add(&upl->mem, mem))
353 return log_msg_ret("mem", -ENOMEM);
359 * decode_upl_memmap() - Decode memory-map nodes from the tree
362 * @root: Parent node containing the /memory-map nodes
363 * Return 0 if OK, -ve on error
365 static int decode_upl_memmap(struct upl *upl, ofnode root)
369 ofnode_for_each_subnode(node, root) {
370 struct upl_memmap memmap;
374 memmap.name = ofnode_get_name(node);
377 buf = ofnode_read_prop(node, UPLP_REG, &size);
379 log_warning("Node '%s': Missing '%s' property\n",
380 ofnode_get_name(node), UPLP_REG);
384 len = decode_addr_size(upl, buf, size, &memmap.region);
386 return log_msg_ret("buf", len);
387 ret = ofnode_read_bitmask(node, UPLP_USAGE, usage_names,
388 UPLUS_COUNT, &memmap.usage);
389 if (ret && ret != -EINVAL) /* optional property */
390 return log_msg_ret("bit", ret);
392 if (!alist_add(&upl->memmap, memmap))
393 return log_msg_ret("mmp", -ENOMEM);
400 * decode_upl_memres() - Decode reserved-memory nodes from the tree
403 * @root: Parent node containing the reserved-memory nodes
404 * Return 0 if OK, -ve on error
406 static int decode_upl_memres(struct upl *upl, ofnode root)
410 ofnode_for_each_subnode(node, root) {
411 struct upl_memres memres;
415 log_debug("decoding '%s'\n", ofnode_get_name(node));
416 memres.name = ofnode_get_name(node);
418 buf = ofnode_read_prop(node, UPLP_REG, &size);
420 log_warning("Node '%s': Missing 'reg' property\n",
421 ofnode_get_name(node));
425 len = decode_addr_size(upl, buf, size, &memres.region);
427 return log_msg_ret("buf", len);
428 memres.no_map = ofnode_read_bool(node, UPLP_NO_MAP);
430 if (!alist_add(&upl->memres, memres))
431 return log_msg_ret("mre", -ENOMEM);
438 * decode_upl_serial() - Decode the serial node
441 * @root: Parent node contain node
442 * Return 0 if OK, -ve on error
444 static int decode_upl_serial(struct upl *upl, ofnode node)
446 struct upl_serial *ser = &upl->serial;
451 ser->compatible = ofnode_read_string(node, UPLP_COMPATIBLE);
452 if (!ser->compatible) {
453 log_warning("Node '%s': Missing compatible string\n",
454 ofnode_get_name(node));
455 return log_msg_ret("com", -EINVAL);
457 ret = read_uint(node, UPLP_CLOCK_FREQUENCY, &ser->clock_frequency);
459 ret = read_uint(node, UPLP_CURRENT_SPEED, &ser->current_speed);
461 return log_msg_ret("spe", ret);
463 buf = ofnode_read_prop(node, UPLP_REG, &size);
465 log_warning("Node '%s': Missing 'reg' property\n",
466 ofnode_get_name(node));
467 return log_msg_ret("reg", -EINVAL);
470 len = decode_addr_size(upl, buf, sizeof(buf), &ser->reg);
472 return log_msg_ret("buf", len);
475 ser->reg_io_shift = UPLD_REG_IO_SHIFT;
476 ser->reg_offset = UPLD_REG_OFFSET;
477 ser->reg_io_width = UPLD_REG_IO_WIDTH;
478 read_uint(node, UPLP_REG_IO_SHIFT, &ser->reg_io_shift);
479 read_uint(node, UPLP_REG_OFFSET, &ser->reg_offset);
480 read_uint(node, UPLP_REG_IO_WIDTH, &ser->reg_io_width);
481 read_addr(upl, node, UPLP_VIRTUAL_REG, &ser->virtual_reg);
482 ret = ofnode_read_value(node, UPLP_ACCESS_TYPE, access_types,
483 ARRAY_SIZE(access_types), &ser->access_type);
484 if (ret && ret != -ENOENT)
485 return log_msg_ret("ser", ret);
491 * decode_upl_graphics() - Decode graphics node
494 * @root: Node to decode
495 * Return 0 if OK, -ve on error
497 static int decode_upl_graphics(struct upl *upl, ofnode node)
499 struct upl_graphics *gra = &upl->graphics;
500 const char *buf, *compat;
504 compat = ofnode_read_string(node, UPLP_COMPATIBLE);
506 log_warning("Node '%s': Missing compatible string\n",
507 ofnode_get_name(node));
508 return log_msg_ret("com", -EINVAL);
510 if (strcmp(UPLC_GRAPHICS, compat)) {
511 log_warning("Node '%s': Ignoring compatible '%s'\n",
512 ofnode_get_name(node), compat);
516 buf = ofnode_read_prop(node, UPLP_REG, &size);
518 log_warning("Node '%s': Missing 'reg' property\n",
519 ofnode_get_name(node));
520 return log_msg_ret("reg", -EINVAL);
523 len = decode_addr_size(upl, buf, size, &gra->reg);
525 return log_msg_ret("buf", len);
527 ret = read_uint(node, UPLP_WIDTH, &gra->width);
529 ret = read_uint(node, UPLP_HEIGHT, &gra->height);
531 ret = read_uint(node, UPLP_STRIDE, &gra->stride);
533 ret = ofnode_read_value(node, UPLP_GRAPHICS_FORMAT,
535 ARRAY_SIZE(graphics_formats),
539 return log_msg_ret("pro", ret);
544 int upl_read_handoff(struct upl *upl, oftree tree)
549 if (!oftree_valid(tree))
550 return log_msg_ret("tre", -EINVAL);
552 root = oftree_root(tree);
555 ret = decode_root_props(upl, root);
557 return log_msg_ret("roo", ret);
559 ofnode_for_each_subnode(node, root) {
560 const char *name = ofnode_get_name(node);
562 log_debug("decoding '%s'\n", name);
563 if (!strcmp(UPLN_OPTIONS, name)) {
564 ret = decode_upl_params(upl, node);
566 return log_msg_ret("opt", ret);
568 ret = decode_upl_images(upl, node);
569 } else if (node_matches_at(node, UPLN_MEMORY)) {
570 ret = decode_upl_memory_node(upl, node);
571 } else if (!strcmp(UPLN_MEMORY_MAP, name)) {
572 ret = decode_upl_memmap(upl, node);
573 } else if (!strcmp(UPLN_MEMORY_RESERVED, name)) {
574 ret = decode_upl_memres(upl, node);
575 } else if (node_matches_at(node, UPLN_SERIAL)) {
576 ret = decode_upl_serial(upl, node);
577 } else if (node_matches_at(node, UPLN_GRAPHICS)) {
578 ret = decode_upl_graphics(upl, node);
580 log_debug("Unknown node '%s'\n", name);
584 return log_msg_ret("err", ret);