blob: f8ccee132867c566dabf4bb727e9c17d78637894 [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>
Sukadev Bhattiprolu379649c2016-03-28 09:31:41 -03007#include "header.h"
8#include "util.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +02009
10#define mfspr(rn) ({unsigned long rval; \
11 asm volatile("mfspr %0," __stringify(rn) \
12 : "=r" (rval)); rval; })
13
14#define SPRN_PVR 0x11F /* Processor Version Register */
15#define PVR_VER(pvr) (((pvr) >> 16) & 0xFFFF) /* Version field */
16#define PVR_REV(pvr) (((pvr) >> 0) & 0xFFFF) /* Revison field */
17
18int
19get_cpuid(char *buffer, size_t sz)
20{
21 unsigned long pvr;
22 int nb;
23
24 pvr = mfspr(SPRN_PVR);
25
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -030026 nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +020027
28 /* look for end marker to ensure the entire data fit */
29 if (strchr(buffer, '$')) {
30 buffer[nb-1] = '\0';
31 return 0;
32 }
33 return -1;
34}