xref: /openbmc/linux/arch/mips/include/asm/machine.h (revision 33def849)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2eed0eabdSPaul Burton /*
3eed0eabdSPaul Burton  * Copyright (C) 2016 Imagination Technologies
4fb615d61SPaul Burton  * Author: Paul Burton <paul.burton@mips.com>
5eed0eabdSPaul Burton  */
6eed0eabdSPaul Burton 
7eed0eabdSPaul Burton #ifndef __MIPS_ASM_MACHINE_H__
8eed0eabdSPaul Burton #define __MIPS_ASM_MACHINE_H__
9eed0eabdSPaul Burton 
10eed0eabdSPaul Burton #include <linux/libfdt.h>
11eed0eabdSPaul Burton #include <linux/of.h>
12eed0eabdSPaul Burton 
13eed0eabdSPaul Burton struct mips_machine {
14eed0eabdSPaul Burton 	const struct of_device_id *matches;
15eed0eabdSPaul Burton 	const void *fdt;
16eed0eabdSPaul Burton 	bool (*detect)(void);
17eed0eabdSPaul Burton 	const void *(*fixup_fdt)(const void *fdt, const void *match_data);
18eed0eabdSPaul Burton 	unsigned int (*measure_hpt_freq)(void);
19eed0eabdSPaul Burton };
20eed0eabdSPaul Burton 
21eed0eabdSPaul Burton extern long __mips_machines_start;
22eed0eabdSPaul Burton extern long __mips_machines_end;
23eed0eabdSPaul Burton 
24eed0eabdSPaul Burton #define MIPS_MACHINE(name)						\
25eed0eabdSPaul Burton 	static const struct mips_machine __mips_mach_##name		\
2633def849SJoe Perches 		__used __section(".mips.machines.init")
27eed0eabdSPaul Burton 
28eed0eabdSPaul Burton #define for_each_mips_machine(mach)					\
29eed0eabdSPaul Burton 	for ((mach) = (struct mips_machine *)&__mips_machines_start;	\
30eed0eabdSPaul Burton 	     (mach) < (struct mips_machine *)&__mips_machines_end;	\
31eed0eabdSPaul Burton 	     (mach)++)
32eed0eabdSPaul Burton 
33eed0eabdSPaul Burton /**
34eed0eabdSPaul Burton  * mips_machine_is_compatible() - check if a machine is compatible with an FDT
35eed0eabdSPaul Burton  * @mach: the machine struct to check
36eed0eabdSPaul Burton  * @fdt: the FDT to check for compatibility with
37eed0eabdSPaul Burton  *
38eed0eabdSPaul Burton  * Check whether the given machine @mach is compatible with the given flattened
39eed0eabdSPaul Burton  * device tree @fdt, based upon the compatibility property of the root node.
40eed0eabdSPaul Burton  *
41eed0eabdSPaul Burton  * Return: the device id matched if any, else NULL
42eed0eabdSPaul Burton  */
43eed0eabdSPaul Burton static inline const struct of_device_id *
mips_machine_is_compatible(const struct mips_machine * mach,const void * fdt)44eed0eabdSPaul Burton mips_machine_is_compatible(const struct mips_machine *mach, const void *fdt)
45eed0eabdSPaul Burton {
46eed0eabdSPaul Burton 	const struct of_device_id *match;
47eed0eabdSPaul Burton 
48eed0eabdSPaul Burton 	if (!mach->matches)
49eed0eabdSPaul Burton 		return NULL;
50eed0eabdSPaul Burton 
519a9ab307SJames Hogan 	for (match = mach->matches; match->compatible[0]; match++) {
52eed0eabdSPaul Burton 		if (fdt_node_check_compatible(fdt, 0, match->compatible) == 0)
53eed0eabdSPaul Burton 			return match;
54eed0eabdSPaul Burton 	}
55eed0eabdSPaul Burton 
56eed0eabdSPaul Burton 	return NULL;
57eed0eabdSPaul Burton }
58eed0eabdSPaul Burton 
59e889dfcaSPaul Burton /**
60e889dfcaSPaul Burton  * struct mips_fdt_fixup - Describe a fixup to apply to an FDT
61e889dfcaSPaul Burton  * @apply: applies the fixup to @fdt, returns zero on success else -errno
62e889dfcaSPaul Burton  * @description: a short description of the fixup
63e889dfcaSPaul Burton  *
64e889dfcaSPaul Burton  * Describes a fixup applied to an FDT blob by the @apply function. The
65e889dfcaSPaul Burton  * @description field provides a short description of the fixup intended for
66e889dfcaSPaul Burton  * use in error messages if the @apply function returns non-zero.
67e889dfcaSPaul Burton  */
68e889dfcaSPaul Burton struct mips_fdt_fixup {
69e889dfcaSPaul Burton 	int (*apply)(void *fdt);
70e889dfcaSPaul Burton 	const char *description;
71e889dfcaSPaul Burton };
72e889dfcaSPaul Burton 
73e889dfcaSPaul Burton /**
74e889dfcaSPaul Burton  * apply_mips_fdt_fixups() - apply fixups to an FDT blob
75e889dfcaSPaul Burton  * @fdt_out: buffer in which to place the fixed-up FDT
76e889dfcaSPaul Burton  * @fdt_out_size: the size of the @fdt_out buffer
77e889dfcaSPaul Burton  * @fdt_in: the FDT blob
78e889dfcaSPaul Burton  * @fixups: pointer to an array of fixups to be applied
79e889dfcaSPaul Burton  *
80e889dfcaSPaul Burton  * Loop through the array of fixups pointed to by @fixups, calling the apply
81e889dfcaSPaul Burton  * function on each until either one returns an error or we reach the end of
82e889dfcaSPaul Burton  * the list as indicated by an entry with a NULL apply field.
83e889dfcaSPaul Burton  *
84e889dfcaSPaul Burton  * Return: zero on success, else -errno
85e889dfcaSPaul Burton  */
86e889dfcaSPaul Burton extern int __init apply_mips_fdt_fixups(void *fdt_out, size_t fdt_out_size,
87e889dfcaSPaul Burton 					const void *fdt_in,
88e889dfcaSPaul Burton 					const struct mips_fdt_fixup *fixups);
89e889dfcaSPaul Burton 
90eed0eabdSPaul Burton #endif /* __MIPS_ASM_MACHINE_H__ */
91