blob: 6138bdef6e63d9f3da37716866f2a38544c2a0c1 [file] [log] [blame]
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001#include <sys/types.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
Arnaldo Carvalho de Melo531d2412016-03-23 15:16:55 -03006#include <linux/stringify.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +02007
8#define mfspr(rn) ({unsigned long rval; \
9 asm volatile("mfspr %0," __stringify(rn) \
10 : "=r" (rval)); rval; })
11
12#define SPRN_PVR 0x11F /* Processor Version Register */
13#define PVR_VER(pvr) (((pvr) >> 16) & 0xFFFF) /* Version field */
14#define PVR_REV(pvr) (((pvr) >> 0) & 0xFFFF) /* Revison field */
15
16int
17get_cpuid(char *buffer, size_t sz)
18{
19 unsigned long pvr;
20 int nb;
21
22 pvr = mfspr(SPRN_PVR);
23
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -030024 nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +020025
26 /* look for end marker to ensure the entire data fit */
27 if (strchr(buffer, '$')) {
28 buffer[nb-1] = '\0';
29 return 0;
30 }
31 return -1;
32}