|
| 1 | +/*********************************************************************/ |
| 2 | +/* Copyright 2009, 2010 The University of Texas at Austin. */ |
| 3 | +/* All rights reserved. */ |
| 4 | +/* */ |
| 5 | +/* Redistribution and use in source and binary forms, with or */ |
| 6 | +/* without modification, are permitted provided that the following */ |
| 7 | +/* conditions are met: */ |
| 8 | +/* */ |
| 9 | +/* 1. Redistributions of source code must retain the above */ |
| 10 | +/* copyright notice, this list of conditions and the following */ |
| 11 | +/* disclaimer. */ |
| 12 | +/* */ |
| 13 | +/* 2. Redistributions in binary form must reproduce the above */ |
| 14 | +/* copyright notice, this list of conditions and the following */ |
| 15 | +/* disclaimer in the documentation and/or other materials */ |
| 16 | +/* provided with the distribution. */ |
| 17 | +/* */ |
| 18 | +/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */ |
| 19 | +/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */ |
| 20 | +/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
| 21 | +/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ |
| 22 | +/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */ |
| 23 | +/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */ |
| 24 | +/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ |
| 25 | +/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */ |
| 26 | +/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */ |
| 27 | +/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ |
| 28 | +/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ |
| 29 | +/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */ |
| 30 | +/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ |
| 31 | +/* POSSIBILITY OF SUCH DAMAGE. */ |
| 32 | +/* */ |
| 33 | +/* The views and conclusions contained in the software and */ |
| 34 | +/* documentation are those of the authors and should not be */ |
| 35 | +/* interpreted as representing official policies, either expressed */ |
| 36 | +/* or implied, of The University of Texas at Austin. */ |
| 37 | +/*********************************************************************/ |
| 38 | + |
| 39 | +#include "common.h" |
| 40 | +#include <asm/hwcap.h> |
| 41 | +#include <sys/auxv.h> |
| 42 | + |
| 43 | +extern gotoblas_t gotoblas_ARMV8; |
| 44 | +extern gotoblas_t gotoblas_CORTEXA57; |
| 45 | +extern gotoblas_t gotoblas_THUNDERX; |
| 46 | +extern gotoblas_t gotoblas_THUNDERX2T99; |
| 47 | + |
| 48 | +extern void openblas_warning(int verbose, const char * msg); |
| 49 | + |
| 50 | +#define NUM_CORETYPES 4 |
| 51 | + |
| 52 | +/* |
| 53 | + * In case asm/hwcap.h is outdated on the build system, make sure |
| 54 | + * that HWCAP_CPUID is defined |
| 55 | + */ |
| 56 | +#ifndef HWCAP_CPUID |
| 57 | +#define HWCAP_CPUID (1 << 11) |
| 58 | +#endif |
| 59 | + |
| 60 | +#define get_cpu_ftr(id, var) ({ \ |
| 61 | + asm("mrs %0, "#id : "=r" (var)); \ |
| 62 | + }) |
| 63 | + |
| 64 | +static char *corename[] = { |
| 65 | + "armv8", |
| 66 | + "cortexa57", |
| 67 | + "thunderx", |
| 68 | + "thunderx2t99", |
| 69 | + "unknown" |
| 70 | +}; |
| 71 | + |
| 72 | +char *gotoblas_corename(void) { |
| 73 | + if (gotoblas == &gotoblas_ARMV8) return corename[ 0]; |
| 74 | + if (gotoblas == &gotoblas_CORTEXA57) return corename[ 1]; |
| 75 | + if (gotoblas == &gotoblas_THUNDERX) return corename[ 2]; |
| 76 | + if (gotoblas == &gotoblas_THUNDERX2T99) return corename[ 3]; |
| 77 | + return corename[NUM_CORETYPES]; |
| 78 | +} |
| 79 | + |
| 80 | +static gotoblas_t *force_coretype(char *coretype) { |
| 81 | + int i ; |
| 82 | + int found = -1; |
| 83 | + char message[128]; |
| 84 | + |
| 85 | + for ( i=0 ; i < NUM_CORETYPES; i++) |
| 86 | + { |
| 87 | + if (!strncasecmp(coretype, corename[i], 20)) |
| 88 | + { |
| 89 | + found = i; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + switch (found) |
| 95 | + { |
| 96 | + case 0: return (&gotoblas_ARMV8); |
| 97 | + case 1: return (&gotoblas_CORTEXA57); |
| 98 | + case 2: return (&gotoblas_THUNDERX); |
| 99 | + case 3: return (&gotoblas_THUNDERX2T99); |
| 100 | + } |
| 101 | + snprintf(message, 128, "Core not found: %s\n", coretype); |
| 102 | + openblas_warning(1, message); |
| 103 | + return NULL; |
| 104 | +} |
| 105 | + |
| 106 | +static gotoblas_t *get_coretype(void) { |
| 107 | + int implementer, variant, part, arch, revision, midr_el1; |
| 108 | + |
| 109 | + if (!(getauxval(AT_HWCAP) & HWCAP_CPUID)) { |
| 110 | + char coremsg[128]; |
| 111 | + snprintf(coremsg, 128, "Kernel lacks cpuid feature support. Auto detection of core type failed !!!\n"); |
| 112 | + openblas_warning(1, coremsg); |
| 113 | + return NULL; |
| 114 | + } |
| 115 | + |
| 116 | + get_cpu_ftr(MIDR_EL1, midr_el1); |
| 117 | + /* |
| 118 | + * MIDR_EL1 |
| 119 | + * |
| 120 | + * 31 24 23 20 19 16 15 4 3 0 |
| 121 | + * ----------------------------------------------------------------- |
| 122 | + * | Implementer | Variant | Architecture | Part Number | Revision | |
| 123 | + * ----------------------------------------------------------------- |
| 124 | + */ |
| 125 | + implementer = (midr_el1 >> 24) & 0xFF; |
| 126 | + part = (midr_el1 >> 4) & 0xFFF; |
| 127 | + |
| 128 | + switch(implementer) |
| 129 | + { |
| 130 | + case 0x41: // ARM |
| 131 | + switch (part) |
| 132 | + { |
| 133 | + case 0xd07: // Cortex A57 |
| 134 | + case 0xd08: // Cortex A72 |
| 135 | + case 0xd03: // Cortex A53 |
| 136 | + return &gotoblas_CORTEXA57; |
| 137 | + } |
| 138 | + break; |
| 139 | + case 0x42: // Broadcom |
| 140 | + switch (part) |
| 141 | + { |
| 142 | + case 0x516: // Vulcan |
| 143 | + return &gotoblas_THUNDERX2T99; |
| 144 | + } |
| 145 | + break; |
| 146 | + case 0x43: // Cavium |
| 147 | + switch (part) |
| 148 | + { |
| 149 | + case 0x0a1: // ThunderX |
| 150 | + return &gotoblas_THUNDERX; |
| 151 | + case 0x0af: // ThunderX2 |
| 152 | + return &gotoblas_THUNDERX2T99; |
| 153 | + } |
| 154 | + break; |
| 155 | + } |
| 156 | + return NULL; |
| 157 | +} |
| 158 | + |
| 159 | +void gotoblas_dynamic_init(void) { |
| 160 | + |
| 161 | + char coremsg[128]; |
| 162 | + char coren[22]; |
| 163 | + char *p; |
| 164 | + |
| 165 | + if (gotoblas) return; |
| 166 | + |
| 167 | + p = getenv("OPENBLAS_CORETYPE"); |
| 168 | + if ( p ) |
| 169 | + { |
| 170 | + gotoblas = force_coretype(p); |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + gotoblas = get_coretype(); |
| 175 | + } |
| 176 | + |
| 177 | + if (gotoblas == NULL) |
| 178 | + { |
| 179 | + snprintf(coremsg, 128, "Falling back to generic ARMV8 core\n"); |
| 180 | + openblas_warning(1, coremsg); |
| 181 | + gotoblas = &gotoblas_ARMV8; |
| 182 | + } |
| 183 | + |
| 184 | + if (gotoblas && gotoblas->init) { |
| 185 | + strncpy(coren, gotoblas_corename(), 20); |
| 186 | + sprintf(coremsg, "Core: %s\n", coren); |
| 187 | + openblas_warning(2, coremsg); |
| 188 | + gotoblas -> init(); |
| 189 | + } else { |
| 190 | + openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n"); |
| 191 | + exit(1); |
| 192 | + } |
| 193 | + |
| 194 | +} |
| 195 | + |
| 196 | +void gotoblas_dynamic_quit(void) { |
| 197 | + gotoblas = NULL; |
| 198 | +} |
0 commit comments