@@ -41,53 +41,89 @@ const pfmlib_attr_desc_t arm_mods[]={
4141};
4242
4343pfm_arm_config_t pfm_arm_cfg = {
44- .implementer = -1 ,
45- .architecture = -1 ,
46- .part = -1 ,
44+ .init_cpuinfo_done = 0 ,
4745};
4846
49- #ifdef CONFIG_PFMLIB_OS_LINUX
47+ #define MAX_ARM_CPUIDS 8
48+
49+ static arm_cpuid_t arm_cpuids [MAX_ARM_CPUIDS ];
50+ static int num_arm_cpuids ;
51+
52+ static int pfmlib_find_arm_cpuid (arm_cpuid_t * attr , arm_cpuid_t * match_attr )
53+ {
54+ int i ;
55+
56+ if (attr == NULL )
57+ return PFM_ERR_NOTFOUND ;
58+
59+ for (i = 0 ; i < num_arm_cpuids ; i ++ ) {
60+ #if 0
5061/*
51- * helper function to retrieve one value from /proc/cpuinfo
52- * for internal libpfm use only
53- * attr: the attribute (line) to look for
54- * ret_buf: a buffer to store the value of the attribute (as a string)
55- * maxlen : number of bytes of capacity in ret_buf
56- *
57- * ret_buf is null terminated.
58- *
59- * Return:
60- * 0 : attribute found, ret_buf populated
61- * -1: attribute not found
62+ * disabled due to issues with expected arch vs. reported
63+ * arch by the Linux kernel cpuinfo
6264 */
65+ if (arm_cpuids [i ].arch != attr -> arch )
66+ continue ;
67+ #endif
68+ if (arm_cpuids [i ].impl != attr -> impl )
69+ continue ;
70+ if (arm_cpuids [i ].part != attr -> part )
71+ continue ;
72+ if (match_attr )
73+ * match_attr = arm_cpuids [i ];
74+ return PFM_SUCCESS ;
75+ }
76+ return PFM_ERR_NOTSUPP ;
77+ }
6378
79+ #ifdef CONFIG_PFMLIB_OS_LINUX
80+ /*
81+ * Function populates the arm_cpuidsp[] table with each unique
82+ * core identifications found on the host. In the case of hybrids
83+ * that number is greater than 1
84+ */
6485static int
65- pfmlib_getcpuinfo_attr ( const char * attr , char * ret_buf , size_t maxlen )
86+ pfmlib_init_cpuids ( void )
6687{
88+ arm_cpuid_t attr = {0 , };
6789 FILE * fp = NULL ;
6890 int ret = -1 ;
69- size_t attr_len , buf_len = 0 ;
91+ size_t buf_len = 0 ;
7092 char * p , * value = NULL ;
7193 char * buffer = NULL ;
94+ int nattrs = 0 ;
7295
73- if (attr == NULL || ret_buf == NULL || maxlen < 1 )
74- return -1 ;
75-
76- attr_len = strlen (attr );
96+ if (pfm_arm_cfg .init_cpuinfo_done == 1 )
97+ return PFM_SUCCESS ;
7798
78- fp = fopen ("/proc/cpuinfo" , "r" );
79- if (fp == NULL )
80- return -1 ;
99+ fp = fopen (pfm_cfg .proc_cpuinfo , "r" );
100+ if (fp == NULL ) {
101+ DPRINT ("pfmlib_init_cpuids: cannot open %s\n" , pfm_cfg .proc_cpuinfo );
102+ return PFM_ERR_NOTFOUND ;
103+ }
81104
82105 while (pfmlib_getl (& buffer , & buf_len , fp ) != -1 ){
106+ if (nattrs == ARM_NUM_ATTR_FIELDS ) {
107+ if (pfmlib_find_arm_cpuid (& attr , NULL ) != PFM_SUCCESS ) {
108+ /* must add */
109+ if (num_arm_cpuids == MAX_ARM_CPUIDS ) {
110+ DPRINT ("pfmlib_init_cpuids: too many cpuids num_arm_cpuids=%d\n" , num_arm_cpuids );
111+ ret = PFM_ERR_TOOMANY ;
112+ goto error ;
113+ }
114+ arm_cpuids [num_arm_cpuids ++ ] = attr ;
115+ __pfm_vbprintf ("Detected ARM CPU impl=0x%x arch=%d part=0x%x\n" , attr .impl , attr .arch , attr .part );
116+ }
117+ nattrs = 0 ;
118+ }
83119
84120 /* skip blank lines */
85- if (* buffer == '\n' )
121+ if (* buffer == '\n' || * buffer == '\r' )
86122 continue ;
87123
88124 p = strchr (buffer , ':' );
89125 if (p == NULL )
90- goto error ;
126+ continue ;
91127
92128 /*
93129 * p+2: +1 = space, +2= firt character
@@ -98,20 +134,38 @@ pfmlib_getcpuinfo_attr(const char *attr, char *ret_buf, size_t maxlen)
98134
99135 value [strlen (value )- 1 ] = '\0' ;
100136
101- if (!strncmp (attr , buffer , attr_len ))
102- break ;
137+ if (!strncmp ("CPU implementer" , buffer , 15 )) {
138+ attr .impl = strtoul (value , NULL , 0 );
139+ nattrs ++ ;
140+ continue ;
141+ }
142+ if (!strncmp ("CPU architecture" , buffer , 16 )) {
143+ attr .arch = strtoul (value , NULL , 0 );
144+ nattrs ++ ;
145+ continue ;
146+ }
147+ if (!strncmp ("CPU part" , buffer , 8 )) {
148+ attr .part = strtoul (value , NULL , 0 );
149+ nattrs ++ ;
150+ continue ;
151+ }
103152 }
104- strncpy (ret_buf , value , maxlen - 1 );
105- ret_buf [maxlen - 1 ] = '\0' ;
106- ret = 0 ;
153+ ret = PFM_SUCCESS ;
154+ DPRINT ("num_arm_cpuids=%d\n" , num_arm_cpuids );
107155error :
156+ for (nattrs = 0 ; nattrs < num_arm_cpuids ; nattrs ++ ) {
157+ DPRINT ("cpuids[%d] = impl=0x%x arch=%d part=0x%x\n" , nattrs , arm_cpuids [nattrs ].impl , arm_cpuids [nattrs ].arch , arm_cpuids [nattrs ].part );
158+ }
159+ pfm_arm_cfg .init_cpuinfo_done = 1 ;
160+
108161 free (buffer );
109162 fclose (fp );
163+
110164 return ret ;
111165}
112166#else
113167static int
114- pfmlib_getcpuinfo_attr ( const char * attr , char * ret_buf , size_t maxlen )
168+ pfmlib_init_cpuids ( void )
115169{
116170 return -1 ;
117171}
@@ -151,34 +205,15 @@ pfm_arm_display_reg(void *this, pfmlib_event_desc_t *e, pfm_arm_reg_t reg)
151205}
152206
153207int
154- pfm_arm_detect (void * this )
208+ pfm_arm_detect (arm_cpuid_t * attr , arm_cpuid_t * match_attr )
155209{
156-
157210 int ret ;
158- char buffer [128 ];
159211
160- if (pfm_arm_cfg .implementer == -1 ) {
161- ret = pfmlib_getcpuinfo_attr ("CPU implementer" , buffer , sizeof (buffer ));
162- if (ret == -1 )
163- return PFM_ERR_NOTSUPP ;
164- pfm_arm_cfg .implementer = strtol (buffer , NULL , 16 );
165- }
166-
167- if (pfm_arm_cfg .part == -1 ) {
168- ret = pfmlib_getcpuinfo_attr ("CPU part" , buffer , sizeof (buffer ));
169- if (ret == -1 )
170- return PFM_ERR_NOTSUPP ;
171- pfm_arm_cfg .part = strtol (buffer , NULL , 16 );
172- }
212+ ret = pfmlib_init_cpuids ();
213+ if (ret != PFM_SUCCESS )
214+ return PFM_ERR_NOTSUPP ;
173215
174- if (pfm_arm_cfg .architecture == -1 ) {
175- ret = pfmlib_getcpuinfo_attr ("CPU architecture" , buffer , sizeof (buffer ));
176- if (ret == -1 )
177- return PFM_ERR_NOTSUPP ;
178- pfm_arm_cfg .architecture = strtol (buffer , NULL , 16 );
179- }
180-
181- return PFM_SUCCESS ;
216+ return pfmlib_find_arm_cpuid (attr , match_attr );
182217}
183218
184219int
0 commit comments