xref: /openbmc/linux/drivers/soc/qcom/socinfo.c (revision d6e2d652)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2009-2017, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2017-2019, Linaro Ltd.
5  */
6 
7 #include <linux/debugfs.h>
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/random.h>
12 #include <linux/slab.h>
13 #include <linux/soc/qcom/smem.h>
14 #include <linux/soc/qcom/socinfo.h>
15 #include <linux/string.h>
16 #include <linux/stringify.h>
17 #include <linux/sys_soc.h>
18 #include <linux/types.h>
19 
20 #include <asm/unaligned.h>
21 
22 #include <dt-bindings/arm/qcom,ids.h>
23 
24 /*
25  * SoC version type with major number in the upper 16 bits and minor
26  * number in the lower 16 bits.
27  */
28 #define SOCINFO_MAJOR(ver) (((ver) >> 16) & 0xffff)
29 #define SOCINFO_MINOR(ver) ((ver) & 0xffff)
30 #define SOCINFO_VERSION(maj, min)  ((((maj) & 0xffff) << 16)|((min) & 0xffff))
31 
32 /* Helper macros to create soc_id table */
33 #define qcom_board_id(id) QCOM_ID_ ## id, __stringify(id)
34 #define qcom_board_id_named(id, name) QCOM_ID_ ## id, (name)
35 
36 #ifdef CONFIG_DEBUG_FS
37 #define SMEM_IMAGE_VERSION_BLOCKS_COUNT        32
38 #define SMEM_IMAGE_VERSION_SIZE                4096
39 #define SMEM_IMAGE_VERSION_NAME_SIZE           75
40 #define SMEM_IMAGE_VERSION_VARIANT_SIZE        20
41 #define SMEM_IMAGE_VERSION_OEM_SIZE            32
42 
43 /*
44  * SMEM Image table indices
45  */
46 #define SMEM_IMAGE_TABLE_BOOT_INDEX     0
47 #define SMEM_IMAGE_TABLE_TZ_INDEX       1
48 #define SMEM_IMAGE_TABLE_RPM_INDEX      3
49 #define SMEM_IMAGE_TABLE_APPS_INDEX     10
50 #define SMEM_IMAGE_TABLE_MPSS_INDEX     11
51 #define SMEM_IMAGE_TABLE_ADSP_INDEX     12
52 #define SMEM_IMAGE_TABLE_CNSS_INDEX     13
53 #define SMEM_IMAGE_TABLE_VIDEO_INDEX    14
54 #define SMEM_IMAGE_VERSION_TABLE       469
55 
56 /*
57  * SMEM Image table names
58  */
59 static const char *const socinfo_image_names[] = {
60 	[SMEM_IMAGE_TABLE_ADSP_INDEX] = "adsp",
61 	[SMEM_IMAGE_TABLE_APPS_INDEX] = "apps",
62 	[SMEM_IMAGE_TABLE_BOOT_INDEX] = "boot",
63 	[SMEM_IMAGE_TABLE_CNSS_INDEX] = "cnss",
64 	[SMEM_IMAGE_TABLE_MPSS_INDEX] = "mpss",
65 	[SMEM_IMAGE_TABLE_RPM_INDEX] = "rpm",
66 	[SMEM_IMAGE_TABLE_TZ_INDEX] = "tz",
67 	[SMEM_IMAGE_TABLE_VIDEO_INDEX] = "video",
68 };
69 
70 static const char *const pmic_models[] = {
71 	[0]  = "Unknown PMIC model",
72 	[1]  = "PM8941",
73 	[2]  = "PM8841",
74 	[3]  = "PM8019",
75 	[4]  = "PM8226",
76 	[5]  = "PM8110",
77 	[6]  = "PMA8084",
78 	[7]  = "PMI8962",
79 	[8]  = "PMD9635",
80 	[9]  = "PM8994",
81 	[10] = "PMI8994",
82 	[11] = "PM8916",
83 	[12] = "PM8004",
84 	[13] = "PM8909/PM8058",
85 	[14] = "PM8028",
86 	[15] = "PM8901",
87 	[16] = "PM8950/PM8027",
88 	[17] = "PMI8950/ISL9519",
89 	[18] = "PMK8001/PM8921",
90 	[19] = "PMI8996/PM8018",
91 	[20] = "PM8998/PM8015",
92 	[21] = "PMI8998/PM8014",
93 	[22] = "PM8821",
94 	[23] = "PM8038",
95 	[24] = "PM8005/PM8922",
96 	[25] = "PM8917",
97 	[26] = "PM660L",
98 	[27] = "PM660",
99 	[30] = "PM8150",
100 	[31] = "PM8150L",
101 	[32] = "PM8150B",
102 	[33] = "PMK8002",
103 	[36] = "PM8009",
104 	[37] = "PMI632",
105 	[38] = "PM8150C",
106 	[40] = "PM6150",
107 	[41] = "SMB2351",
108 	[44] = "PM8008",
109 	[45] = "PM6125",
110 	[46] = "PM7250B",
111 	[47] = "PMK8350",
112 	[48] = "PM8350",
113 	[49] = "PM8350C",
114 	[50] = "PM8350B",
115 	[51] = "PMR735A",
116 	[52] = "PMR735B",
117 	[55] = "PM2250",
118 	[58] = "PM8450",
119 	[65] = "PM8010",
120 };
121 
122 struct socinfo_params {
123 	u32 raw_device_family;
124 	u32 hw_plat_subtype;
125 	u32 accessory_chip;
126 	u32 raw_device_num;
127 	u32 chip_family;
128 	u32 foundry_id;
129 	u32 plat_ver;
130 	u32 raw_ver;
131 	u32 hw_plat;
132 	u32 fmt;
133 	u32 nproduct_id;
134 	u32 num_clusters;
135 	u32 ncluster_array_offset;
136 	u32 num_subset_parts;
137 	u32 nsubset_parts_array_offset;
138 	u32 nmodem_supported;
139 	u32 feature_code;
140 	u32 pcode;
141 	u32 oem_variant;
142 	u32 num_func_clusters;
143 	u32 boot_cluster;
144 	u32 boot_core;
145 };
146 
147 struct smem_image_version {
148 	char name[SMEM_IMAGE_VERSION_NAME_SIZE];
149 	char variant[SMEM_IMAGE_VERSION_VARIANT_SIZE];
150 	char pad;
151 	char oem[SMEM_IMAGE_VERSION_OEM_SIZE];
152 };
153 #endif /* CONFIG_DEBUG_FS */
154 
155 struct qcom_socinfo {
156 	struct soc_device *soc_dev;
157 	struct soc_device_attribute attr;
158 #ifdef CONFIG_DEBUG_FS
159 	struct dentry *dbg_root;
160 	struct socinfo_params info;
161 #endif /* CONFIG_DEBUG_FS */
162 };
163 
164 struct soc_id {
165 	unsigned int id;
166 	const char *name;
167 };
168 
169 static const struct soc_id soc_id[] = {
170 	{ qcom_board_id(MSM8260) },
171 	{ qcom_board_id(MSM8660) },
172 	{ qcom_board_id(APQ8060) },
173 	{ qcom_board_id(MSM8960) },
174 	{ qcom_board_id(APQ8064) },
175 	{ qcom_board_id(MSM8930) },
176 	{ qcom_board_id(MSM8630) },
177 	{ qcom_board_id(MSM8230) },
178 	{ qcom_board_id(APQ8030) },
179 	{ qcom_board_id(MSM8627) },
180 	{ qcom_board_id(MSM8227) },
181 	{ qcom_board_id(MSM8660A) },
182 	{ qcom_board_id(MSM8260A) },
183 	{ qcom_board_id(APQ8060A) },
184 	{ qcom_board_id(MSM8974) },
185 	{ qcom_board_id(MSM8225) },
186 	{ qcom_board_id(MSM8625) },
187 	{ qcom_board_id(MPQ8064) },
188 	{ qcom_board_id(MSM8960AB) },
189 	{ qcom_board_id(APQ8060AB) },
190 	{ qcom_board_id(MSM8260AB) },
191 	{ qcom_board_id(MSM8660AB) },
192 	{ qcom_board_id(MSM8930AA) },
193 	{ qcom_board_id(MSM8630AA) },
194 	{ qcom_board_id(MSM8230AA) },
195 	{ qcom_board_id(MSM8626) },
196 	{ qcom_board_id(MSM8610) },
197 	{ qcom_board_id(APQ8064AB) },
198 	{ qcom_board_id(MSM8930AB) },
199 	{ qcom_board_id(MSM8630AB) },
200 	{ qcom_board_id(MSM8230AB) },
201 	{ qcom_board_id(APQ8030AB) },
202 	{ qcom_board_id(MSM8226) },
203 	{ qcom_board_id(MSM8526) },
204 	{ qcom_board_id(APQ8030AA) },
205 	{ qcom_board_id(MSM8110) },
206 	{ qcom_board_id(MSM8210) },
207 	{ qcom_board_id(MSM8810) },
208 	{ qcom_board_id(MSM8212) },
209 	{ qcom_board_id(MSM8612) },
210 	{ qcom_board_id(MSM8112) },
211 	{ qcom_board_id(MSM8125) },
212 	{ qcom_board_id(MSM8225Q) },
213 	{ qcom_board_id(MSM8625Q) },
214 	{ qcom_board_id(MSM8125Q) },
215 	{ qcom_board_id(APQ8064AA) },
216 	{ qcom_board_id(APQ8084) },
217 	{ qcom_board_id(MSM8130) },
218 	{ qcom_board_id(MSM8130AA) },
219 	{ qcom_board_id(MSM8130AB) },
220 	{ qcom_board_id(MSM8627AA) },
221 	{ qcom_board_id(MSM8227AA) },
222 	{ qcom_board_id(APQ8074) },
223 	{ qcom_board_id(MSM8274) },
224 	{ qcom_board_id(MSM8674) },
225 	{ qcom_board_id(MDM9635) },
226 	{ qcom_board_id_named(MSM8974PRO_AC, "MSM8974PRO-AC") },
227 	{ qcom_board_id(MSM8126) },
228 	{ qcom_board_id(APQ8026) },
229 	{ qcom_board_id(MSM8926) },
230 	{ qcom_board_id(IPQ8062) },
231 	{ qcom_board_id(IPQ8064) },
232 	{ qcom_board_id(IPQ8066) },
233 	{ qcom_board_id(IPQ8068) },
234 	{ qcom_board_id(MSM8326) },
235 	{ qcom_board_id(MSM8916) },
236 	{ qcom_board_id(MSM8994) },
237 	{ qcom_board_id_named(APQ8074PRO_AA, "APQ8074PRO-AA") },
238 	{ qcom_board_id_named(APQ8074PRO_AB, "APQ8074PRO-AB") },
239 	{ qcom_board_id_named(APQ8074PRO_AC, "APQ8074PRO-AC") },
240 	{ qcom_board_id_named(MSM8274PRO_AA, "MSM8274PRO-AA") },
241 	{ qcom_board_id_named(MSM8274PRO_AB, "MSM8274PRO-AB") },
242 	{ qcom_board_id_named(MSM8274PRO_AC, "MSM8274PRO-AC") },
243 	{ qcom_board_id_named(MSM8674PRO_AA, "MSM8674PRO-AA") },
244 	{ qcom_board_id_named(MSM8674PRO_AB, "MSM8674PRO-AB") },
245 	{ qcom_board_id_named(MSM8674PRO_AC, "MSM8674PRO-AC") },
246 	{ qcom_board_id_named(MSM8974PRO_AA, "MSM8974PRO-AA") },
247 	{ qcom_board_id_named(MSM8974PRO_AB, "MSM8974PRO-AB") },
248 	{ qcom_board_id(APQ8028) },
249 	{ qcom_board_id(MSM8128) },
250 	{ qcom_board_id(MSM8228) },
251 	{ qcom_board_id(MSM8528) },
252 	{ qcom_board_id(MSM8628) },
253 	{ qcom_board_id(MSM8928) },
254 	{ qcom_board_id(MSM8510) },
255 	{ qcom_board_id(MSM8512) },
256 	{ qcom_board_id(MSM8936) },
257 	{ qcom_board_id(MDM9640) },
258 	{ qcom_board_id(MSM8939) },
259 	{ qcom_board_id(APQ8036) },
260 	{ qcom_board_id(APQ8039) },
261 	{ qcom_board_id(MSM8236) },
262 	{ qcom_board_id(MSM8636) },
263 	{ qcom_board_id(MSM8909) },
264 	{ qcom_board_id(MSM8996) },
265 	{ qcom_board_id(APQ8016) },
266 	{ qcom_board_id(MSM8216) },
267 	{ qcom_board_id(MSM8116) },
268 	{ qcom_board_id(MSM8616) },
269 	{ qcom_board_id(MSM8992) },
270 	{ qcom_board_id(APQ8092) },
271 	{ qcom_board_id(APQ8094) },
272 	{ qcom_board_id(MSM8209) },
273 	{ qcom_board_id(MSM8208) },
274 	{ qcom_board_id(MDM9209) },
275 	{ qcom_board_id(MDM9309) },
276 	{ qcom_board_id(MDM9609) },
277 	{ qcom_board_id(MSM8239) },
278 	{ qcom_board_id(MSM8952) },
279 	{ qcom_board_id(APQ8009) },
280 	{ qcom_board_id(MSM8956) },
281 	{ qcom_board_id(MSM8929) },
282 	{ qcom_board_id(MSM8629) },
283 	{ qcom_board_id(MSM8229) },
284 	{ qcom_board_id(APQ8029) },
285 	{ qcom_board_id(APQ8056) },
286 	{ qcom_board_id(MSM8609) },
287 	{ qcom_board_id(APQ8076) },
288 	{ qcom_board_id(MSM8976) },
289 	{ qcom_board_id(IPQ8065) },
290 	{ qcom_board_id(IPQ8069) },
291 	{ qcom_board_id(MDM9650) },
292 	{ qcom_board_id(MDM9655) },
293 	{ qcom_board_id(MDM9250) },
294 	{ qcom_board_id(MDM9255) },
295 	{ qcom_board_id(MDM9350) },
296 	{ qcom_board_id(APQ8052) },
297 	{ qcom_board_id(MDM9607) },
298 	{ qcom_board_id(APQ8096) },
299 	{ qcom_board_id(MSM8998) },
300 	{ qcom_board_id(MSM8953) },
301 	{ qcom_board_id(MSM8937) },
302 	{ qcom_board_id(APQ8037) },
303 	{ qcom_board_id(MDM8207) },
304 	{ qcom_board_id(MDM9207) },
305 	{ qcom_board_id(MDM9307) },
306 	{ qcom_board_id(MDM9628) },
307 	{ qcom_board_id(MSM8909W) },
308 	{ qcom_board_id(APQ8009W) },
309 	{ qcom_board_id(MSM8996L) },
310 	{ qcom_board_id(MSM8917) },
311 	{ qcom_board_id(APQ8053) },
312 	{ qcom_board_id(MSM8996SG) },
313 	{ qcom_board_id(APQ8017) },
314 	{ qcom_board_id(MSM8217) },
315 	{ qcom_board_id(MSM8617) },
316 	{ qcom_board_id(MSM8996AU) },
317 	{ qcom_board_id(APQ8096AU) },
318 	{ qcom_board_id(APQ8096SG) },
319 	{ qcom_board_id(MSM8940) },
320 	{ qcom_board_id(SDX201) },
321 	{ qcom_board_id(SDM660) },
322 	{ qcom_board_id(SDM630) },
323 	{ qcom_board_id(APQ8098) },
324 	{ qcom_board_id(MSM8920) },
325 	{ qcom_board_id(SDM845) },
326 	{ qcom_board_id(MDM9206) },
327 	{ qcom_board_id(IPQ8074) },
328 	{ qcom_board_id(SDA660) },
329 	{ qcom_board_id(SDM658) },
330 	{ qcom_board_id(SDA658) },
331 	{ qcom_board_id(SDA630) },
332 	{ qcom_board_id(MSM8905) },
333 	{ qcom_board_id(SDX202) },
334 	{ qcom_board_id(SDM450) },
335 	{ qcom_board_id(SM8150) },
336 	{ qcom_board_id(SDA845) },
337 	{ qcom_board_id(IPQ8072) },
338 	{ qcom_board_id(IPQ8076) },
339 	{ qcom_board_id(IPQ8078) },
340 	{ qcom_board_id(SDM636) },
341 	{ qcom_board_id(SDA636) },
342 	{ qcom_board_id(SDM632) },
343 	{ qcom_board_id(SDA632) },
344 	{ qcom_board_id(SDA450) },
345 	{ qcom_board_id(SDM439) },
346 	{ qcom_board_id(SDM429) },
347 	{ qcom_board_id(SM8250) },
348 	{ qcom_board_id(SA8155) },
349 	{ qcom_board_id(SDA439) },
350 	{ qcom_board_id(SDA429) },
351 	{ qcom_board_id(SM7150) },
352 	{ qcom_board_id(IPQ8070) },
353 	{ qcom_board_id(IPQ8071) },
354 	{ qcom_board_id(QM215) },
355 	{ qcom_board_id(IPQ8072A) },
356 	{ qcom_board_id(IPQ8074A) },
357 	{ qcom_board_id(IPQ8076A) },
358 	{ qcom_board_id(IPQ8078A) },
359 	{ qcom_board_id(SM6125) },
360 	{ qcom_board_id(IPQ8070A) },
361 	{ qcom_board_id(IPQ8071A) },
362 	{ qcom_board_id(IPQ6018) },
363 	{ qcom_board_id(IPQ6028) },
364 	{ qcom_board_id(SDM429W) },
365 	{ qcom_board_id(SM4250) },
366 	{ qcom_board_id(IPQ6000) },
367 	{ qcom_board_id(IPQ6010) },
368 	{ qcom_board_id(SC7180) },
369 	{ qcom_board_id(SM6350) },
370 	{ qcom_board_id(QCM2150) },
371 	{ qcom_board_id(SDA429W) },
372 	{ qcom_board_id(SM8350) },
373 	{ qcom_board_id(QCM2290) },
374 	{ qcom_board_id(SM6115) },
375 	{ qcom_board_id(IPQ5010) },
376 	{ qcom_board_id(IPQ5018) },
377 	{ qcom_board_id(IPQ5028) },
378 	{ qcom_board_id(SC8280XP) },
379 	{ qcom_board_id(IPQ6005) },
380 	{ qcom_board_id(QRB5165) },
381 	{ qcom_board_id(SM8450) },
382 	{ qcom_board_id(SM7225) },
383 	{ qcom_board_id(SA8295P) },
384 	{ qcom_board_id(SA8540P) },
385 	{ qcom_board_id(QCM4290) },
386 	{ qcom_board_id(QCS4290) },
387 	{ qcom_board_id_named(SM8450_2, "SM8450") },
388 	{ qcom_board_id_named(SM8450_3, "SM8450") },
389 	{ qcom_board_id(SC7280) },
390 	{ qcom_board_id(SC7180P) },
391 	{ qcom_board_id(IPQ5000) },
392 	{ qcom_board_id(IPQ0509) },
393 	{ qcom_board_id(IPQ0518) },
394 	{ qcom_board_id(SM6375) },
395 	{ qcom_board_id(IPQ9514) },
396 	{ qcom_board_id(IPQ9550) },
397 	{ qcom_board_id(IPQ9554) },
398 	{ qcom_board_id(IPQ9570) },
399 	{ qcom_board_id(IPQ9574) },
400 	{ qcom_board_id(SM8550) },
401 	{ qcom_board_id(IPQ5016) },
402 	{ qcom_board_id(IPQ9510) },
403 	{ qcom_board_id(QRB4210) },
404 	{ qcom_board_id(QRB2210) },
405 	{ qcom_board_id(SA8775P) },
406 	{ qcom_board_id(QRU1000) },
407 	{ qcom_board_id(QDU1000) },
408 	{ qcom_board_id(QDU1010) },
409 	{ qcom_board_id(IPQ5019) },
410 	{ qcom_board_id(QRU1032) },
411 	{ qcom_board_id(QRU1052) },
412 	{ qcom_board_id(QRU1062) },
413 	{ qcom_board_id(IPQ5332) },
414 	{ qcom_board_id(IPQ5322) },
415 	{ qcom_board_id(IPQ5312) },
416 	{ qcom_board_id(IPQ5302) },
417 	{ qcom_board_id(IPQ5300) },
418 };
419 
420 static const char *socinfo_machine(struct device *dev, unsigned int id)
421 {
422 	int idx;
423 
424 	for (idx = 0; idx < ARRAY_SIZE(soc_id); idx++) {
425 		if (soc_id[idx].id == id)
426 			return soc_id[idx].name;
427 	}
428 
429 	return NULL;
430 }
431 
432 #ifdef CONFIG_DEBUG_FS
433 
434 #define QCOM_OPEN(name, _func)						\
435 static int qcom_open_##name(struct inode *inode, struct file *file)	\
436 {									\
437 	return single_open(file, _func, inode->i_private);		\
438 }									\
439 									\
440 static const struct file_operations qcom_ ##name## _ops = {		\
441 	.open = qcom_open_##name,					\
442 	.read = seq_read,						\
443 	.llseek = seq_lseek,						\
444 	.release = single_release,					\
445 }
446 
447 #define DEBUGFS_ADD(info, name)						\
448 	debugfs_create_file(__stringify(name), 0444,			\
449 			    qcom_socinfo->dbg_root,			\
450 			    info, &qcom_ ##name## _ops)
451 
452 
453 static int qcom_show_build_id(struct seq_file *seq, void *p)
454 {
455 	struct socinfo *socinfo = seq->private;
456 
457 	seq_printf(seq, "%s\n", socinfo->build_id);
458 
459 	return 0;
460 }
461 
462 static int qcom_show_pmic_model(struct seq_file *seq, void *p)
463 {
464 	struct socinfo *socinfo = seq->private;
465 	int model = SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_model));
466 
467 	if (model < 0)
468 		return -EINVAL;
469 
470 	if (model < ARRAY_SIZE(pmic_models) && pmic_models[model])
471 		seq_printf(seq, "%s\n", pmic_models[model]);
472 	else
473 		seq_printf(seq, "unknown (%d)\n", model);
474 
475 	return 0;
476 }
477 
478 static int qcom_show_pmic_model_array(struct seq_file *seq, void *p)
479 {
480 	struct socinfo *socinfo = seq->private;
481 	unsigned int num_pmics = le32_to_cpu(socinfo->num_pmics);
482 	unsigned int pmic_array_offset = le32_to_cpu(socinfo->pmic_array_offset);
483 	int i;
484 	void *ptr = socinfo;
485 
486 	ptr += pmic_array_offset;
487 
488 	/* No need for bounds checking, it happened at socinfo_debugfs_init */
489 	for (i = 0; i < num_pmics; i++) {
490 		unsigned int model = SOCINFO_MINOR(get_unaligned_le32(ptr + 2 * i * sizeof(u32)));
491 		unsigned int die_rev = get_unaligned_le32(ptr + (2 * i + 1) * sizeof(u32));
492 
493 		if (model < ARRAY_SIZE(pmic_models) && pmic_models[model])
494 			seq_printf(seq, "%s %u.%u\n", pmic_models[model],
495 				   SOCINFO_MAJOR(die_rev),
496 				   SOCINFO_MINOR(die_rev));
497 		else
498 			seq_printf(seq, "unknown (%d)\n", model);
499 	}
500 
501 	return 0;
502 }
503 
504 static int qcom_show_pmic_die_revision(struct seq_file *seq, void *p)
505 {
506 	struct socinfo *socinfo = seq->private;
507 
508 	seq_printf(seq, "%u.%u\n",
509 		   SOCINFO_MAJOR(le32_to_cpu(socinfo->pmic_die_rev)),
510 		   SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_die_rev)));
511 
512 	return 0;
513 }
514 
515 static int qcom_show_chip_id(struct seq_file *seq, void *p)
516 {
517 	struct socinfo *socinfo = seq->private;
518 
519 	seq_printf(seq, "%s\n", socinfo->chip_id);
520 
521 	return 0;
522 }
523 
524 QCOM_OPEN(build_id, qcom_show_build_id);
525 QCOM_OPEN(pmic_model, qcom_show_pmic_model);
526 QCOM_OPEN(pmic_model_array, qcom_show_pmic_model_array);
527 QCOM_OPEN(pmic_die_rev, qcom_show_pmic_die_revision);
528 QCOM_OPEN(chip_id, qcom_show_chip_id);
529 
530 #define DEFINE_IMAGE_OPS(type)					\
531 static int show_image_##type(struct seq_file *seq, void *p)		  \
532 {								  \
533 	struct smem_image_version *image_version = seq->private;  \
534 	if (image_version->type[0] != '\0')			  \
535 		seq_printf(seq, "%s\n", image_version->type);	  \
536 	return 0;						  \
537 }								  \
538 static int open_image_##type(struct inode *inode, struct file *file)	  \
539 {									  \
540 	return single_open(file, show_image_##type, inode->i_private); \
541 }									  \
542 									  \
543 static const struct file_operations qcom_image_##type##_ops = {	  \
544 	.open = open_image_##type,					  \
545 	.read = seq_read,						  \
546 	.llseek = seq_lseek,						  \
547 	.release = single_release,					  \
548 }
549 
550 DEFINE_IMAGE_OPS(name);
551 DEFINE_IMAGE_OPS(variant);
552 DEFINE_IMAGE_OPS(oem);
553 
554 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
555 				 struct socinfo *info, size_t info_size)
556 {
557 	struct smem_image_version *versions;
558 	struct dentry *dentry;
559 	size_t size;
560 	int i;
561 	unsigned int num_pmics;
562 	unsigned int pmic_array_offset;
563 
564 	qcom_socinfo->dbg_root = debugfs_create_dir("qcom_socinfo", NULL);
565 
566 	qcom_socinfo->info.fmt = __le32_to_cpu(info->fmt);
567 
568 	debugfs_create_x32("info_fmt", 0444, qcom_socinfo->dbg_root,
569 			   &qcom_socinfo->info.fmt);
570 
571 	switch (qcom_socinfo->info.fmt) {
572 	case SOCINFO_VERSION(0, 19):
573 		qcom_socinfo->info.num_func_clusters = __le32_to_cpu(info->num_func_clusters);
574 		qcom_socinfo->info.boot_cluster = __le32_to_cpu(info->boot_cluster);
575 		qcom_socinfo->info.boot_core = __le32_to_cpu(info->boot_core);
576 
577 		debugfs_create_u32("num_func_clusters", 0444, qcom_socinfo->dbg_root,
578 				   &qcom_socinfo->info.num_func_clusters);
579 		debugfs_create_u32("boot_cluster", 0444, qcom_socinfo->dbg_root,
580 				   &qcom_socinfo->info.boot_cluster);
581 		debugfs_create_u32("boot_core", 0444, qcom_socinfo->dbg_root,
582 				   &qcom_socinfo->info.boot_core);
583 		fallthrough;
584 	case SOCINFO_VERSION(0, 18):
585 	case SOCINFO_VERSION(0, 17):
586 		qcom_socinfo->info.oem_variant = __le32_to_cpu(info->oem_variant);
587 		debugfs_create_u32("oem_variant", 0444, qcom_socinfo->dbg_root,
588 				   &qcom_socinfo->info.oem_variant);
589 		fallthrough;
590 	case SOCINFO_VERSION(0, 16):
591 		qcom_socinfo->info.feature_code = __le32_to_cpu(info->feature_code);
592 		qcom_socinfo->info.pcode = __le32_to_cpu(info->pcode);
593 
594 		debugfs_create_u32("feature_code", 0444, qcom_socinfo->dbg_root,
595 				   &qcom_socinfo->info.feature_code);
596 		debugfs_create_u32("pcode", 0444, qcom_socinfo->dbg_root,
597 				   &qcom_socinfo->info.pcode);
598 		fallthrough;
599 	case SOCINFO_VERSION(0, 15):
600 		qcom_socinfo->info.nmodem_supported = __le32_to_cpu(info->nmodem_supported);
601 
602 		debugfs_create_u32("nmodem_supported", 0444, qcom_socinfo->dbg_root,
603 				   &qcom_socinfo->info.nmodem_supported);
604 		fallthrough;
605 	case SOCINFO_VERSION(0, 14):
606 		qcom_socinfo->info.num_clusters = __le32_to_cpu(info->num_clusters);
607 		qcom_socinfo->info.ncluster_array_offset = __le32_to_cpu(info->ncluster_array_offset);
608 		qcom_socinfo->info.num_subset_parts = __le32_to_cpu(info->num_subset_parts);
609 		qcom_socinfo->info.nsubset_parts_array_offset =
610 			__le32_to_cpu(info->nsubset_parts_array_offset);
611 
612 		debugfs_create_u32("num_clusters", 0444, qcom_socinfo->dbg_root,
613 				   &qcom_socinfo->info.num_clusters);
614 		debugfs_create_u32("ncluster_array_offset", 0444, qcom_socinfo->dbg_root,
615 				   &qcom_socinfo->info.ncluster_array_offset);
616 		debugfs_create_u32("num_subset_parts", 0444, qcom_socinfo->dbg_root,
617 				   &qcom_socinfo->info.num_subset_parts);
618 		debugfs_create_u32("nsubset_parts_array_offset", 0444, qcom_socinfo->dbg_root,
619 				   &qcom_socinfo->info.nsubset_parts_array_offset);
620 		fallthrough;
621 	case SOCINFO_VERSION(0, 13):
622 		qcom_socinfo->info.nproduct_id = __le32_to_cpu(info->nproduct_id);
623 
624 		debugfs_create_u32("nproduct_id", 0444, qcom_socinfo->dbg_root,
625 				   &qcom_socinfo->info.nproduct_id);
626 		DEBUGFS_ADD(info, chip_id);
627 		fallthrough;
628 	case SOCINFO_VERSION(0, 12):
629 		qcom_socinfo->info.chip_family =
630 			__le32_to_cpu(info->chip_family);
631 		qcom_socinfo->info.raw_device_family =
632 			__le32_to_cpu(info->raw_device_family);
633 		qcom_socinfo->info.raw_device_num =
634 			__le32_to_cpu(info->raw_device_num);
635 
636 		debugfs_create_x32("chip_family", 0444, qcom_socinfo->dbg_root,
637 				   &qcom_socinfo->info.chip_family);
638 		debugfs_create_x32("raw_device_family", 0444,
639 				   qcom_socinfo->dbg_root,
640 				   &qcom_socinfo->info.raw_device_family);
641 		debugfs_create_x32("raw_device_number", 0444,
642 				   qcom_socinfo->dbg_root,
643 				   &qcom_socinfo->info.raw_device_num);
644 		fallthrough;
645 	case SOCINFO_VERSION(0, 11):
646 		num_pmics = le32_to_cpu(info->num_pmics);
647 		pmic_array_offset = le32_to_cpu(info->pmic_array_offset);
648 		if (pmic_array_offset + 2 * num_pmics * sizeof(u32) <= info_size)
649 			DEBUGFS_ADD(info, pmic_model_array);
650 		fallthrough;
651 	case SOCINFO_VERSION(0, 10):
652 	case SOCINFO_VERSION(0, 9):
653 		qcom_socinfo->info.foundry_id = __le32_to_cpu(info->foundry_id);
654 
655 		debugfs_create_u32("foundry_id", 0444, qcom_socinfo->dbg_root,
656 				   &qcom_socinfo->info.foundry_id);
657 		fallthrough;
658 	case SOCINFO_VERSION(0, 8):
659 	case SOCINFO_VERSION(0, 7):
660 		DEBUGFS_ADD(info, pmic_model);
661 		DEBUGFS_ADD(info, pmic_die_rev);
662 		fallthrough;
663 	case SOCINFO_VERSION(0, 6):
664 		qcom_socinfo->info.hw_plat_subtype =
665 			__le32_to_cpu(info->hw_plat_subtype);
666 
667 		debugfs_create_u32("hardware_platform_subtype", 0444,
668 				   qcom_socinfo->dbg_root,
669 				   &qcom_socinfo->info.hw_plat_subtype);
670 		fallthrough;
671 	case SOCINFO_VERSION(0, 5):
672 		qcom_socinfo->info.accessory_chip =
673 			__le32_to_cpu(info->accessory_chip);
674 
675 		debugfs_create_u32("accessory_chip", 0444,
676 				   qcom_socinfo->dbg_root,
677 				   &qcom_socinfo->info.accessory_chip);
678 		fallthrough;
679 	case SOCINFO_VERSION(0, 4):
680 		qcom_socinfo->info.plat_ver = __le32_to_cpu(info->plat_ver);
681 
682 		debugfs_create_u32("platform_version", 0444,
683 				   qcom_socinfo->dbg_root,
684 				   &qcom_socinfo->info.plat_ver);
685 		fallthrough;
686 	case SOCINFO_VERSION(0, 3):
687 		qcom_socinfo->info.hw_plat = __le32_to_cpu(info->hw_plat);
688 
689 		debugfs_create_u32("hardware_platform", 0444,
690 				   qcom_socinfo->dbg_root,
691 				   &qcom_socinfo->info.hw_plat);
692 		fallthrough;
693 	case SOCINFO_VERSION(0, 2):
694 		qcom_socinfo->info.raw_ver  = __le32_to_cpu(info->raw_ver);
695 
696 		debugfs_create_u32("raw_version", 0444, qcom_socinfo->dbg_root,
697 				   &qcom_socinfo->info.raw_ver);
698 		fallthrough;
699 	case SOCINFO_VERSION(0, 1):
700 		DEBUGFS_ADD(info, build_id);
701 		break;
702 	}
703 
704 	versions = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_IMAGE_VERSION_TABLE,
705 				 &size);
706 
707 	for (i = 0; i < ARRAY_SIZE(socinfo_image_names); i++) {
708 		if (!socinfo_image_names[i])
709 			continue;
710 
711 		dentry = debugfs_create_dir(socinfo_image_names[i],
712 					    qcom_socinfo->dbg_root);
713 		debugfs_create_file("name", 0444, dentry, &versions[i],
714 				    &qcom_image_name_ops);
715 		debugfs_create_file("variant", 0444, dentry, &versions[i],
716 				    &qcom_image_variant_ops);
717 		debugfs_create_file("oem", 0444, dentry, &versions[i],
718 				    &qcom_image_oem_ops);
719 	}
720 }
721 
722 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo)
723 {
724 	debugfs_remove_recursive(qcom_socinfo->dbg_root);
725 }
726 #else
727 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
728 				 struct socinfo *info, size_t info_size)
729 {
730 }
731 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo) {  }
732 #endif /* CONFIG_DEBUG_FS */
733 
734 static int qcom_socinfo_probe(struct platform_device *pdev)
735 {
736 	struct qcom_socinfo *qs;
737 	struct socinfo *info;
738 	size_t item_size;
739 
740 	info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_HW_SW_BUILD_ID,
741 			      &item_size);
742 	if (IS_ERR(info)) {
743 		dev_err(&pdev->dev, "Couldn't find socinfo\n");
744 		return PTR_ERR(info);
745 	}
746 
747 	qs = devm_kzalloc(&pdev->dev, sizeof(*qs), GFP_KERNEL);
748 	if (!qs)
749 		return -ENOMEM;
750 
751 	qs->attr.family = "Snapdragon";
752 	qs->attr.machine = socinfo_machine(&pdev->dev,
753 					   le32_to_cpu(info->id));
754 	qs->attr.soc_id = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u",
755 					 le32_to_cpu(info->id));
756 	qs->attr.revision = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u.%u",
757 					   SOCINFO_MAJOR(le32_to_cpu(info->ver)),
758 					   SOCINFO_MINOR(le32_to_cpu(info->ver)));
759 	if (offsetof(struct socinfo, serial_num) <= item_size)
760 		qs->attr.serial_number = devm_kasprintf(&pdev->dev, GFP_KERNEL,
761 							"%u",
762 							le32_to_cpu(info->serial_num));
763 
764 	qs->soc_dev = soc_device_register(&qs->attr);
765 	if (IS_ERR(qs->soc_dev))
766 		return PTR_ERR(qs->soc_dev);
767 
768 	socinfo_debugfs_init(qs, info, item_size);
769 
770 	/* Feed the soc specific unique data into entropy pool */
771 	add_device_randomness(info, item_size);
772 
773 	platform_set_drvdata(pdev, qs);
774 
775 	return 0;
776 }
777 
778 static int qcom_socinfo_remove(struct platform_device *pdev)
779 {
780 	struct qcom_socinfo *qs = platform_get_drvdata(pdev);
781 
782 	soc_device_unregister(qs->soc_dev);
783 
784 	socinfo_debugfs_exit(qs);
785 
786 	return 0;
787 }
788 
789 static struct platform_driver qcom_socinfo_driver = {
790 	.probe = qcom_socinfo_probe,
791 	.remove = qcom_socinfo_remove,
792 	.driver  = {
793 		.name = "qcom-socinfo",
794 	},
795 };
796 
797 module_platform_driver(qcom_socinfo_driver);
798 
799 MODULE_DESCRIPTION("Qualcomm SoCinfo driver");
800 MODULE_LICENSE("GPL v2");
801 MODULE_ALIAS("platform:qcom-socinfo");
802