1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2024 NXP
4  */
5 
6 #include <errno.h>
7 #include <fdtdec.h>
8 #include <malloc.h>
9 #include <asm/arch/sys_proto.h>
10 
disable_thermal_cpu_nodes(void * blob,u32 num_disabled_cores,u32 max_cores)11 static void disable_thermal_cpu_nodes(void *blob, u32 num_disabled_cores, u32 max_cores)
12 {
13 	static const char * const thermal_path[] = {
14 		"/thermal-zones/cpu-thermal/cooling-maps/map0"
15 	};
16 
17 	int nodeoff, cnt, i, ret, j;
18 	u32 num_le32 = max_cores * 3;
19 	u32 *cooling_dev = (u32 *)malloc(num_le32 * sizeof(__le32));
20 
21 	if (!cooling_dev) {
22 		printf("failed to alloc cooling dev\n");
23 		return;
24 	}
25 
26 	for (i = 0; i < ARRAY_SIZE(thermal_path); i++) {
27 		nodeoff = fdt_path_offset(blob, thermal_path[i]);
28 		if (nodeoff < 0)
29 			continue; /* Not found, skip it */
30 
31 		cnt = fdtdec_get_int_array_count(blob, nodeoff, "cooling-device",
32 						 cooling_dev, num_le32);
33 		if (cnt < 0)
34 			continue;
35 
36 		if (cnt != num_le32)
37 			printf("Warning: %s, cooling-device count %d\n", thermal_path[i], cnt);
38 
39 		for (j = 0; j < cnt; j++)
40 			cooling_dev[j] = cpu_to_fdt32(cooling_dev[j]);
41 
42 		ret = fdt_setprop(blob, nodeoff, "cooling-device", cooling_dev,
43 				  sizeof(__le32) * (num_le32 - num_disabled_cores * 3));
44 		if (ret < 0) {
45 			printf("Warning: %s, cooling-device setprop failed %d\n",
46 			       thermal_path[i], ret);
47 			continue;
48 		}
49 
50 		printf("Update node %s, cooling-device prop\n", thermal_path[i]);
51 	}
52 
53 	free(cooling_dev);
54 }
55 
disable_cpu_nodes(void * blob,const char * const * nodes_path,u32 num_disabled_cores,u32 max_cores)56 int disable_cpu_nodes(void *blob, const char * const *nodes_path, u32 num_disabled_cores,
57 		      u32 max_cores)
58 {
59 	u32 i = 0;
60 	int rc;
61 	int nodeoff;
62 
63 	if (max_cores == 0 || (num_disabled_cores > (max_cores - 1)))
64 		return -EINVAL;
65 
66 	i = max_cores - num_disabled_cores;
67 
68 	for (; i < max_cores; i++) {
69 		nodeoff = fdt_path_offset(blob, nodes_path[i]);
70 		if (nodeoff < 0)
71 			continue; /* Not found, skip it */
72 
73 		debug("Found %s node\n", nodes_path[i]);
74 
75 		rc = fdt_del_node(blob, nodeoff);
76 		if (rc < 0) {
77 			printf("Unable to delete node %s, err=%s\n",
78 			       nodes_path[i], fdt_strerror(rc));
79 		} else {
80 			printf("Delete node %s\n", nodes_path[i]);
81 		}
82 	}
83 
84 	disable_thermal_cpu_nodes(blob, num_disabled_cores, max_cores);
85 
86 	return 0;
87 }
88 
fixup_thermal_trips(void * blob,const char * name)89 int fixup_thermal_trips(void *blob, const char *name)
90 {
91 	int minc, maxc;
92 	int node, trip;
93 
94 	node = fdt_path_offset(blob, "/thermal-zones");
95 	if (node < 0)
96 		return node;
97 
98 	node = fdt_subnode_offset(blob, node, name);
99 	if (node < 0)
100 		return node;
101 
102 	node = fdt_subnode_offset(blob, node, "trips");
103 	if (node < 0)
104 		return node;
105 
106 	get_cpu_temp_grade(&minc, &maxc);
107 
108 	fdt_for_each_subnode(trip, blob, node) {
109 		const char *type;
110 		int temp, ret;
111 
112 		type = fdt_getprop(blob, trip, "type", NULL);
113 		if (!type)
114 			continue;
115 
116 		temp = 0;
117 		if (!strcmp(type, "critical"))
118 			temp = 1000 * maxc;
119 		else if (!strcmp(type, "passive"))
120 			temp = 1000 * (maxc - 10);
121 		if (temp) {
122 			ret = fdt_setprop_u32(blob, trip, "temperature", temp);
123 			if (ret)
124 				return ret;
125 		}
126 	}
127 
128 	return 0;
129 }
130