x2apic_uv_x.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * SGI UV APIC functions (note: not an Intel compatible APIC)
  7. *
  8. * Copyright (C) 2007-2014 Silicon Graphics, Inc. All rights reserved.
  9. */
  10. #include <linux/cpumask.h>
  11. #include <linux/hardirq.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/threads.h>
  14. #include <linux/kernel.h>
  15. #include <linux/export.h>
  16. #include <linux/string.h>
  17. #include <linux/ctype.h>
  18. #include <linux/sched.h>
  19. #include <linux/timer.h>
  20. #include <linux/slab.h>
  21. #include <linux/cpu.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. #include <linux/pci.h>
  25. #include <linux/kdebug.h>
  26. #include <linux/delay.h>
  27. #include <linux/crash_dump.h>
  28. #include <linux/reboot.h>
  29. #include <asm/uv/uv_mmrs.h>
  30. #include <asm/uv/uv_hub.h>
  31. #include <asm/current.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/uv/bios.h>
  34. #include <asm/uv/uv.h>
  35. #include <asm/apic.h>
  36. #include <asm/ipi.h>
  37. #include <asm/smp.h>
  38. #include <asm/x86_init.h>
  39. #include <asm/nmi.h>
  40. DEFINE_PER_CPU(int, x2apic_extra_bits);
  41. #define PR_DEVEL(fmt, args...) pr_devel("%s: " fmt, __func__, args)
  42. static enum uv_system_type uv_system_type;
  43. static u64 gru_start_paddr, gru_end_paddr;
  44. static u64 gru_dist_base, gru_first_node_paddr = -1LL, gru_last_node_paddr;
  45. static u64 gru_dist_lmask, gru_dist_umask;
  46. static union uvh_apicid uvh_apicid;
  47. /* info derived from CPUID */
  48. static struct {
  49. unsigned int apicid_shift;
  50. unsigned int apicid_mask;
  51. unsigned int socketid_shift; /* aka pnode_shift for UV1/2/3 */
  52. unsigned int pnode_mask;
  53. unsigned int gpa_shift;
  54. } uv_cpuid;
  55. int uv_min_hub_revision_id;
  56. EXPORT_SYMBOL_GPL(uv_min_hub_revision_id);
  57. unsigned int uv_apicid_hibits;
  58. EXPORT_SYMBOL_GPL(uv_apicid_hibits);
  59. static struct apic apic_x2apic_uv_x;
  60. static struct uv_hub_info_s uv_hub_info_node0;
  61. /* Set this to use hardware error handler instead of kernel panic */
  62. static int disable_uv_undefined_panic = 1;
  63. unsigned long uv_undefined(char *str)
  64. {
  65. if (likely(!disable_uv_undefined_panic))
  66. panic("UV: error: undefined MMR: %s\n", str);
  67. else
  68. pr_crit("UV: error: undefined MMR: %s\n", str);
  69. return ~0ul; /* cause a machine fault */
  70. }
  71. EXPORT_SYMBOL(uv_undefined);
  72. static unsigned long __init uv_early_read_mmr(unsigned long addr)
  73. {
  74. unsigned long val, *mmr;
  75. mmr = early_ioremap(UV_LOCAL_MMR_BASE | addr, sizeof(*mmr));
  76. val = *mmr;
  77. early_iounmap(mmr, sizeof(*mmr));
  78. return val;
  79. }
  80. static inline bool is_GRU_range(u64 start, u64 end)
  81. {
  82. if (gru_dist_base) {
  83. u64 su = start & gru_dist_umask; /* upper (incl pnode) bits */
  84. u64 sl = start & gru_dist_lmask; /* base offset bits */
  85. u64 eu = end & gru_dist_umask;
  86. u64 el = end & gru_dist_lmask;
  87. /* Must reside completely within a single GRU range */
  88. return (sl == gru_dist_base && el == gru_dist_base &&
  89. su >= gru_first_node_paddr &&
  90. su <= gru_last_node_paddr &&
  91. eu == su);
  92. } else {
  93. return start >= gru_start_paddr && end <= gru_end_paddr;
  94. }
  95. }
  96. static bool uv_is_untracked_pat_range(u64 start, u64 end)
  97. {
  98. return is_ISA_range(start, end) || is_GRU_range(start, end);
  99. }
  100. static int __init early_get_pnodeid(void)
  101. {
  102. union uvh_node_id_u node_id;
  103. union uvh_rh_gam_config_mmr_u m_n_config;
  104. int pnode;
  105. /* Currently, all blades have same revision number */
  106. node_id.v = uv_early_read_mmr(UVH_NODE_ID);
  107. m_n_config.v = uv_early_read_mmr(UVH_RH_GAM_CONFIG_MMR);
  108. uv_min_hub_revision_id = node_id.s.revision;
  109. switch (node_id.s.part_number) {
  110. case UV2_HUB_PART_NUMBER:
  111. case UV2_HUB_PART_NUMBER_X:
  112. uv_min_hub_revision_id += UV2_HUB_REVISION_BASE - 1;
  113. break;
  114. case UV3_HUB_PART_NUMBER:
  115. case UV3_HUB_PART_NUMBER_X:
  116. uv_min_hub_revision_id += UV3_HUB_REVISION_BASE;
  117. break;
  118. case UV4_HUB_PART_NUMBER:
  119. uv_min_hub_revision_id += UV4_HUB_REVISION_BASE - 1;
  120. break;
  121. }
  122. uv_hub_info->hub_revision = uv_min_hub_revision_id;
  123. uv_cpuid.pnode_mask = (1 << m_n_config.s.n_skt) - 1;
  124. pnode = (node_id.s.node_id >> 1) & uv_cpuid.pnode_mask;
  125. uv_cpuid.gpa_shift = 46; /* default unless changed */
  126. pr_info("UV: rev:%d part#:%x nodeid:%04x n_skt:%d pnmsk:%x pn:%x\n",
  127. node_id.s.revision, node_id.s.part_number, node_id.s.node_id,
  128. m_n_config.s.n_skt, uv_cpuid.pnode_mask, pnode);
  129. return pnode;
  130. }
  131. /* [copied from arch/x86/kernel/cpu/topology.c:detect_extended_topology()] */
  132. #define SMT_LEVEL 0 /* leaf 0xb SMT level */
  133. #define INVALID_TYPE 0 /* leaf 0xb sub-leaf types */
  134. #define SMT_TYPE 1
  135. #define CORE_TYPE 2
  136. #define LEAFB_SUBTYPE(ecx) (((ecx) >> 8) & 0xff)
  137. #define BITS_SHIFT_NEXT_LEVEL(eax) ((eax) & 0x1f)
  138. static void set_x2apic_bits(void)
  139. {
  140. unsigned int eax, ebx, ecx, edx, sub_index;
  141. unsigned int sid_shift;
  142. cpuid(0, &eax, &ebx, &ecx, &edx);
  143. if (eax < 0xb) {
  144. pr_info("UV: CPU does not have CPUID.11\n");
  145. return;
  146. }
  147. cpuid_count(0xb, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
  148. if (ebx == 0 || (LEAFB_SUBTYPE(ecx) != SMT_TYPE)) {
  149. pr_info("UV: CPUID.11 not implemented\n");
  150. return;
  151. }
  152. sid_shift = BITS_SHIFT_NEXT_LEVEL(eax);
  153. sub_index = 1;
  154. do {
  155. cpuid_count(0xb, sub_index, &eax, &ebx, &ecx, &edx);
  156. if (LEAFB_SUBTYPE(ecx) == CORE_TYPE) {
  157. sid_shift = BITS_SHIFT_NEXT_LEVEL(eax);
  158. break;
  159. }
  160. sub_index++;
  161. } while (LEAFB_SUBTYPE(ecx) != INVALID_TYPE);
  162. uv_cpuid.apicid_shift = 0;
  163. uv_cpuid.apicid_mask = (~(-1 << sid_shift));
  164. uv_cpuid.socketid_shift = sid_shift;
  165. }
  166. static void __init early_get_apic_socketid_shift(void)
  167. {
  168. if (is_uv2_hub() || is_uv3_hub())
  169. uvh_apicid.v = uv_early_read_mmr(UVH_APICID);
  170. set_x2apic_bits();
  171. pr_info("UV: apicid_shift:%d apicid_mask:0x%x\n",
  172. uv_cpuid.apicid_shift, uv_cpuid.apicid_mask);
  173. pr_info("UV: socketid_shift:%d pnode_mask:0x%x\n",
  174. uv_cpuid.socketid_shift, uv_cpuid.pnode_mask);
  175. }
  176. /*
  177. * Add an extra bit as dictated by bios to the destination apicid of
  178. * interrupts potentially passing through the UV HUB. This prevents
  179. * a deadlock between interrupts and IO port operations.
  180. */
  181. static void __init uv_set_apicid_hibit(void)
  182. {
  183. union uv1h_lb_target_physical_apic_id_mask_u apicid_mask;
  184. if (is_uv1_hub()) {
  185. apicid_mask.v =
  186. uv_early_read_mmr(UV1H_LB_TARGET_PHYSICAL_APIC_ID_MASK);
  187. uv_apicid_hibits =
  188. apicid_mask.s1.bit_enables & UV_APICID_HIBIT_MASK;
  189. }
  190. }
  191. static int __init uv_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  192. {
  193. int pnodeid;
  194. int uv_apic;
  195. if (strncmp(oem_id, "SGI", 3) != 0)
  196. return 0;
  197. if (numa_off) {
  198. pr_err("UV: NUMA is off, disabling UV support\n");
  199. return 0;
  200. }
  201. /* Setup early hub type field in uv_hub_info for Node 0 */
  202. uv_cpu_info->p_uv_hub_info = &uv_hub_info_node0;
  203. /*
  204. * Determine UV arch type.
  205. * SGI: UV100/1000
  206. * SGI2: UV2000/3000
  207. * SGI3: UV300 (truncated to 4 chars because of different varieties)
  208. * SGI4: UV400 (truncated to 4 chars because of different varieties)
  209. */
  210. uv_hub_info->hub_revision =
  211. !strncmp(oem_id, "SGI4", 4) ? UV4_HUB_REVISION_BASE :
  212. !strncmp(oem_id, "SGI3", 4) ? UV3_HUB_REVISION_BASE :
  213. !strcmp(oem_id, "SGI2") ? UV2_HUB_REVISION_BASE :
  214. !strcmp(oem_id, "SGI") ? UV1_HUB_REVISION_BASE : 0;
  215. if (uv_hub_info->hub_revision == 0)
  216. goto badbios;
  217. pnodeid = early_get_pnodeid();
  218. early_get_apic_socketid_shift();
  219. x86_platform.is_untracked_pat_range = uv_is_untracked_pat_range;
  220. x86_platform.nmi_init = uv_nmi_init;
  221. if (!strcmp(oem_table_id, "UVX")) { /* most common */
  222. uv_system_type = UV_X2APIC;
  223. uv_apic = 0;
  224. } else if (!strcmp(oem_table_id, "UVH")) { /* only UV1 systems */
  225. uv_system_type = UV_NON_UNIQUE_APIC;
  226. __this_cpu_write(x2apic_extra_bits,
  227. pnodeid << uvh_apicid.s.pnode_shift);
  228. uv_set_apicid_hibit();
  229. uv_apic = 1;
  230. } else if (!strcmp(oem_table_id, "UVL")) { /* only used for */
  231. uv_system_type = UV_LEGACY_APIC; /* very small systems */
  232. uv_apic = 0;
  233. } else {
  234. goto badbios;
  235. }
  236. pr_info("UV: OEM IDs %s/%s, System/HUB Types %d/%d, uv_apic %d\n",
  237. oem_id, oem_table_id, uv_system_type,
  238. uv_min_hub_revision_id, uv_apic);
  239. return uv_apic;
  240. badbios:
  241. pr_err("UV: OEM_ID:%s OEM_TABLE_ID:%s\n", oem_id, oem_table_id);
  242. pr_err("Current BIOS not supported, update kernel and/or BIOS\n");
  243. BUG();
  244. }
  245. enum uv_system_type get_uv_system_type(void)
  246. {
  247. return uv_system_type;
  248. }
  249. int is_uv_system(void)
  250. {
  251. return uv_system_type != UV_NONE;
  252. }
  253. EXPORT_SYMBOL_GPL(is_uv_system);
  254. void **__uv_hub_info_list;
  255. EXPORT_SYMBOL_GPL(__uv_hub_info_list);
  256. DEFINE_PER_CPU(struct uv_cpu_info_s, __uv_cpu_info);
  257. EXPORT_PER_CPU_SYMBOL_GPL(__uv_cpu_info);
  258. short uv_possible_blades;
  259. EXPORT_SYMBOL_GPL(uv_possible_blades);
  260. unsigned long sn_rtc_cycles_per_second;
  261. EXPORT_SYMBOL(sn_rtc_cycles_per_second);
  262. /* the following values are used for the per node hub info struct */
  263. static __initdata unsigned short *_node_to_pnode;
  264. static __initdata unsigned short _min_socket, _max_socket;
  265. static __initdata unsigned short _min_pnode, _max_pnode, _gr_table_len;
  266. static __initdata struct uv_gam_range_entry *uv_gre_table;
  267. static __initdata struct uv_gam_parameters *uv_gp_table;
  268. static __initdata unsigned short *_socket_to_node;
  269. static __initdata unsigned short *_socket_to_pnode;
  270. static __initdata unsigned short *_pnode_to_socket;
  271. static __initdata struct uv_gam_range_s *_gr_table;
  272. #define SOCK_EMPTY ((unsigned short)~0)
  273. extern int uv_hub_info_version(void)
  274. {
  275. return UV_HUB_INFO_VERSION;
  276. }
  277. EXPORT_SYMBOL(uv_hub_info_version);
  278. /* Build GAM range lookup table */
  279. static __init void build_uv_gr_table(void)
  280. {
  281. struct uv_gam_range_entry *gre = uv_gre_table;
  282. struct uv_gam_range_s *grt;
  283. unsigned long last_limit = 0, ram_limit = 0;
  284. int bytes, i, sid, lsid = -1, indx = 0, lindx = -1;
  285. if (!gre)
  286. return;
  287. bytes = _gr_table_len * sizeof(struct uv_gam_range_s);
  288. grt = kzalloc(bytes, GFP_KERNEL);
  289. BUG_ON(!grt);
  290. _gr_table = grt;
  291. for (; gre->type != UV_GAM_RANGE_TYPE_UNUSED; gre++) {
  292. if (gre->type == UV_GAM_RANGE_TYPE_HOLE) {
  293. if (!ram_limit) { /* mark hole between ram/non-ram */
  294. ram_limit = last_limit;
  295. last_limit = gre->limit;
  296. lsid++;
  297. continue;
  298. }
  299. last_limit = gre->limit;
  300. pr_info("UV: extra hole in GAM RE table @%d\n",
  301. (int)(gre - uv_gre_table));
  302. continue;
  303. }
  304. if (_max_socket < gre->sockid) {
  305. pr_err("UV: GAM table sockid(%d) too large(>%d) @%d\n",
  306. gre->sockid, _max_socket,
  307. (int)(gre - uv_gre_table));
  308. continue;
  309. }
  310. sid = gre->sockid - _min_socket;
  311. if (lsid < sid) { /* new range */
  312. grt = &_gr_table[indx];
  313. grt->base = lindx;
  314. grt->nasid = gre->nasid;
  315. grt->limit = last_limit = gre->limit;
  316. lsid = sid;
  317. lindx = indx++;
  318. continue;
  319. }
  320. if (lsid == sid && !ram_limit) { /* update range */
  321. if (grt->limit == last_limit) { /* .. if contiguous */
  322. grt->limit = last_limit = gre->limit;
  323. continue;
  324. }
  325. }
  326. if (!ram_limit) { /* non-contiguous ram range */
  327. grt++;
  328. grt->base = lindx;
  329. grt->nasid = gre->nasid;
  330. grt->limit = last_limit = gre->limit;
  331. continue;
  332. }
  333. grt++; /* non-contiguous/non-ram */
  334. grt->base = grt - _gr_table; /* base is this entry */
  335. grt->nasid = gre->nasid;
  336. grt->limit = last_limit = gre->limit;
  337. lsid++;
  338. }
  339. /* shorten table if possible */
  340. grt++;
  341. i = grt - _gr_table;
  342. if (i < _gr_table_len) {
  343. void *ret;
  344. bytes = i * sizeof(struct uv_gam_range_s);
  345. ret = krealloc(_gr_table, bytes, GFP_KERNEL);
  346. if (ret) {
  347. _gr_table = ret;
  348. _gr_table_len = i;
  349. }
  350. }
  351. /* display resultant gam range table */
  352. for (i = 0, grt = _gr_table; i < _gr_table_len; i++, grt++) {
  353. int gb = grt->base;
  354. unsigned long start = gb < 0 ? 0 :
  355. (unsigned long)_gr_table[gb].limit << UV_GAM_RANGE_SHFT;
  356. unsigned long end =
  357. (unsigned long)grt->limit << UV_GAM_RANGE_SHFT;
  358. pr_info("UV: GAM Range %2d %04x 0x%013lx-0x%013lx (%d)\n",
  359. i, grt->nasid, start, end, gb);
  360. }
  361. }
  362. static int uv_wakeup_secondary(int phys_apicid, unsigned long start_rip)
  363. {
  364. unsigned long val;
  365. int pnode;
  366. pnode = uv_apicid_to_pnode(phys_apicid);
  367. phys_apicid |= uv_apicid_hibits;
  368. val = (1UL << UVH_IPI_INT_SEND_SHFT) |
  369. (phys_apicid << UVH_IPI_INT_APIC_ID_SHFT) |
  370. ((start_rip << UVH_IPI_INT_VECTOR_SHFT) >> 12) |
  371. APIC_DM_INIT;
  372. uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
  373. val = (1UL << UVH_IPI_INT_SEND_SHFT) |
  374. (phys_apicid << UVH_IPI_INT_APIC_ID_SHFT) |
  375. ((start_rip << UVH_IPI_INT_VECTOR_SHFT) >> 12) |
  376. APIC_DM_STARTUP;
  377. uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
  378. return 0;
  379. }
  380. static void uv_send_IPI_one(int cpu, int vector)
  381. {
  382. unsigned long apicid;
  383. int pnode;
  384. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  385. pnode = uv_apicid_to_pnode(apicid);
  386. uv_hub_send_ipi(pnode, apicid, vector);
  387. }
  388. static void uv_send_IPI_mask(const struct cpumask *mask, int vector)
  389. {
  390. unsigned int cpu;
  391. for_each_cpu(cpu, mask)
  392. uv_send_IPI_one(cpu, vector);
  393. }
  394. static void uv_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
  395. {
  396. unsigned int this_cpu = smp_processor_id();
  397. unsigned int cpu;
  398. for_each_cpu(cpu, mask) {
  399. if (cpu != this_cpu)
  400. uv_send_IPI_one(cpu, vector);
  401. }
  402. }
  403. static void uv_send_IPI_allbutself(int vector)
  404. {
  405. unsigned int this_cpu = smp_processor_id();
  406. unsigned int cpu;
  407. for_each_online_cpu(cpu) {
  408. if (cpu != this_cpu)
  409. uv_send_IPI_one(cpu, vector);
  410. }
  411. }
  412. static void uv_send_IPI_all(int vector)
  413. {
  414. uv_send_IPI_mask(cpu_online_mask, vector);
  415. }
  416. static int uv_apic_id_valid(int apicid)
  417. {
  418. return 1;
  419. }
  420. static int uv_apic_id_registered(void)
  421. {
  422. return 1;
  423. }
  424. static void uv_init_apic_ldr(void)
  425. {
  426. }
  427. static int
  428. uv_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  429. const struct cpumask *andmask,
  430. unsigned int *apicid)
  431. {
  432. int unsigned cpu;
  433. /*
  434. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  435. * May as well be the first.
  436. */
  437. for_each_cpu_and(cpu, cpumask, andmask) {
  438. if (cpumask_test_cpu(cpu, cpu_online_mask))
  439. break;
  440. }
  441. if (likely(cpu < nr_cpu_ids)) {
  442. *apicid = per_cpu(x86_cpu_to_apicid, cpu) | uv_apicid_hibits;
  443. return 0;
  444. }
  445. return -EINVAL;
  446. }
  447. static unsigned int x2apic_get_apic_id(unsigned long x)
  448. {
  449. unsigned int id;
  450. WARN_ON(preemptible() && num_online_cpus() > 1);
  451. id = x | __this_cpu_read(x2apic_extra_bits);
  452. return id;
  453. }
  454. static unsigned long set_apic_id(unsigned int id)
  455. {
  456. /* CHECKME: Do we need to mask out the xapic extra bits? */
  457. return id;
  458. }
  459. static unsigned int uv_read_apic_id(void)
  460. {
  461. return x2apic_get_apic_id(apic_read(APIC_ID));
  462. }
  463. static int uv_phys_pkg_id(int initial_apicid, int index_msb)
  464. {
  465. return uv_read_apic_id() >> index_msb;
  466. }
  467. static void uv_send_IPI_self(int vector)
  468. {
  469. apic_write(APIC_SELF_IPI, vector);
  470. }
  471. static int uv_probe(void)
  472. {
  473. return apic == &apic_x2apic_uv_x;
  474. }
  475. static struct apic apic_x2apic_uv_x __ro_after_init = {
  476. .name = "UV large system",
  477. .probe = uv_probe,
  478. .acpi_madt_oem_check = uv_acpi_madt_oem_check,
  479. .apic_id_valid = uv_apic_id_valid,
  480. .apic_id_registered = uv_apic_id_registered,
  481. .irq_delivery_mode = dest_Fixed,
  482. .irq_dest_mode = 0, /* physical */
  483. .target_cpus = online_target_cpus,
  484. .disable_esr = 0,
  485. .dest_logical = APIC_DEST_LOGICAL,
  486. .check_apicid_used = NULL,
  487. .vector_allocation_domain = default_vector_allocation_domain,
  488. .init_apic_ldr = uv_init_apic_ldr,
  489. .ioapic_phys_id_map = NULL,
  490. .setup_apic_routing = NULL,
  491. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  492. .apicid_to_cpu_present = NULL,
  493. .check_phys_apicid_present = default_check_phys_apicid_present,
  494. .phys_pkg_id = uv_phys_pkg_id,
  495. .get_apic_id = x2apic_get_apic_id,
  496. .set_apic_id = set_apic_id,
  497. .cpu_mask_to_apicid_and = uv_cpu_mask_to_apicid_and,
  498. .send_IPI = uv_send_IPI_one,
  499. .send_IPI_mask = uv_send_IPI_mask,
  500. .send_IPI_mask_allbutself = uv_send_IPI_mask_allbutself,
  501. .send_IPI_allbutself = uv_send_IPI_allbutself,
  502. .send_IPI_all = uv_send_IPI_all,
  503. .send_IPI_self = uv_send_IPI_self,
  504. .wakeup_secondary_cpu = uv_wakeup_secondary,
  505. .inquire_remote_apic = NULL,
  506. .read = native_apic_msr_read,
  507. .write = native_apic_msr_write,
  508. .eoi_write = native_apic_msr_eoi_write,
  509. .icr_read = native_x2apic_icr_read,
  510. .icr_write = native_x2apic_icr_write,
  511. .wait_icr_idle = native_x2apic_wait_icr_idle,
  512. .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
  513. };
  514. static void set_x2apic_extra_bits(int pnode)
  515. {
  516. __this_cpu_write(x2apic_extra_bits, pnode << uvh_apicid.s.pnode_shift);
  517. }
  518. #define UVH_RH_GAM_ALIAS210_REDIRECT_CONFIG_LENGTH 3
  519. #define DEST_SHIFT UVH_RH_GAM_ALIAS210_REDIRECT_CONFIG_0_MMR_DEST_BASE_SHFT
  520. static __init void get_lowmem_redirect(unsigned long *base, unsigned long *size)
  521. {
  522. union uvh_rh_gam_alias210_overlay_config_2_mmr_u alias;
  523. union uvh_rh_gam_alias210_redirect_config_2_mmr_u redirect;
  524. unsigned long m_redirect;
  525. unsigned long m_overlay;
  526. int i;
  527. for (i = 0; i < UVH_RH_GAM_ALIAS210_REDIRECT_CONFIG_LENGTH; i++) {
  528. switch (i) {
  529. case 0:
  530. m_redirect = UVH_RH_GAM_ALIAS210_REDIRECT_CONFIG_0_MMR;
  531. m_overlay = UVH_RH_GAM_ALIAS210_OVERLAY_CONFIG_0_MMR;
  532. break;
  533. case 1:
  534. m_redirect = UVH_RH_GAM_ALIAS210_REDIRECT_CONFIG_1_MMR;
  535. m_overlay = UVH_RH_GAM_ALIAS210_OVERLAY_CONFIG_1_MMR;
  536. break;
  537. case 2:
  538. m_redirect = UVH_RH_GAM_ALIAS210_REDIRECT_CONFIG_2_MMR;
  539. m_overlay = UVH_RH_GAM_ALIAS210_OVERLAY_CONFIG_2_MMR;
  540. break;
  541. }
  542. alias.v = uv_read_local_mmr(m_overlay);
  543. if (alias.s.enable && alias.s.base == 0) {
  544. *size = (1UL << alias.s.m_alias);
  545. redirect.v = uv_read_local_mmr(m_redirect);
  546. *base = (unsigned long)redirect.s.dest_base
  547. << DEST_SHIFT;
  548. return;
  549. }
  550. }
  551. *base = *size = 0;
  552. }
  553. enum map_type {map_wb, map_uc};
  554. static __init void map_high(char *id, unsigned long base, int pshift,
  555. int bshift, int max_pnode, enum map_type map_type)
  556. {
  557. unsigned long bytes, paddr;
  558. paddr = base << pshift;
  559. bytes = (1UL << bshift) * (max_pnode + 1);
  560. if (!paddr) {
  561. pr_info("UV: Map %s_HI base address NULL\n", id);
  562. return;
  563. }
  564. pr_debug("UV: Map %s_HI 0x%lx - 0x%lx\n", id, paddr, paddr + bytes);
  565. if (map_type == map_uc)
  566. init_extra_mapping_uc(paddr, bytes);
  567. else
  568. init_extra_mapping_wb(paddr, bytes);
  569. }
  570. static __init void map_gru_distributed(unsigned long c)
  571. {
  572. union uvh_rh_gam_gru_overlay_config_mmr_u gru;
  573. u64 paddr;
  574. unsigned long bytes;
  575. int nid;
  576. gru.v = c;
  577. /* only base bits 42:28 relevant in dist mode */
  578. gru_dist_base = gru.v & 0x000007fff0000000UL;
  579. if (!gru_dist_base) {
  580. pr_info("UV: Map GRU_DIST base address NULL\n");
  581. return;
  582. }
  583. bytes = 1UL << UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR_BASE_SHFT;
  584. gru_dist_lmask = ((1UL << uv_hub_info->m_val) - 1) & ~(bytes - 1);
  585. gru_dist_umask = ~((1UL << uv_hub_info->m_val) - 1);
  586. gru_dist_base &= gru_dist_lmask; /* Clear bits above M */
  587. for_each_online_node(nid) {
  588. paddr = ((u64)uv_node_to_pnode(nid) << uv_hub_info->m_val) |
  589. gru_dist_base;
  590. init_extra_mapping_wb(paddr, bytes);
  591. gru_first_node_paddr = min(paddr, gru_first_node_paddr);
  592. gru_last_node_paddr = max(paddr, gru_last_node_paddr);
  593. }
  594. /* Save upper (63:M) bits of address only for is_GRU_range */
  595. gru_first_node_paddr &= gru_dist_umask;
  596. gru_last_node_paddr &= gru_dist_umask;
  597. pr_debug("UV: Map GRU_DIST base 0x%016llx 0x%016llx - 0x%016llx\n",
  598. gru_dist_base, gru_first_node_paddr, gru_last_node_paddr);
  599. }
  600. static __init void map_gru_high(int max_pnode)
  601. {
  602. union uvh_rh_gam_gru_overlay_config_mmr_u gru;
  603. int shift = UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR_BASE_SHFT;
  604. unsigned long mask = UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR_BASE_MASK;
  605. unsigned long base;
  606. gru.v = uv_read_local_mmr(UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR);
  607. if (!gru.s.enable) {
  608. pr_info("UV: GRU disabled\n");
  609. return;
  610. }
  611. if (is_uv3_hub() && gru.s3.mode) {
  612. map_gru_distributed(gru.v);
  613. return;
  614. }
  615. base = (gru.v & mask) >> shift;
  616. map_high("GRU", base, shift, shift, max_pnode, map_wb);
  617. gru_start_paddr = ((u64)base << shift);
  618. gru_end_paddr = gru_start_paddr + (1UL << shift) * (max_pnode + 1);
  619. }
  620. static __init void map_mmr_high(int max_pnode)
  621. {
  622. union uvh_rh_gam_mmr_overlay_config_mmr_u mmr;
  623. int shift = UVH_RH_GAM_MMR_OVERLAY_CONFIG_MMR_BASE_SHFT;
  624. mmr.v = uv_read_local_mmr(UVH_RH_GAM_MMR_OVERLAY_CONFIG_MMR);
  625. if (mmr.s.enable)
  626. map_high("MMR", mmr.s.base, shift, shift, max_pnode, map_uc);
  627. else
  628. pr_info("UV: MMR disabled\n");
  629. }
  630. /*
  631. * This commonality works because both 0 & 1 versions of the MMIOH OVERLAY
  632. * and REDIRECT MMR regs are exactly the same on UV3.
  633. */
  634. struct mmioh_config {
  635. unsigned long overlay;
  636. unsigned long redirect;
  637. char *id;
  638. };
  639. static __initdata struct mmioh_config mmiohs[] = {
  640. {
  641. UV3H_RH_GAM_MMIOH_OVERLAY_CONFIG0_MMR,
  642. UV3H_RH_GAM_MMIOH_REDIRECT_CONFIG0_MMR,
  643. "MMIOH0"
  644. },
  645. {
  646. UV3H_RH_GAM_MMIOH_OVERLAY_CONFIG1_MMR,
  647. UV3H_RH_GAM_MMIOH_REDIRECT_CONFIG1_MMR,
  648. "MMIOH1"
  649. },
  650. };
  651. /* UV3 & UV4 have identical MMIOH overlay configs */
  652. static __init void map_mmioh_high_uv3(int index, int min_pnode, int max_pnode)
  653. {
  654. union uv3h_rh_gam_mmioh_overlay_config0_mmr_u overlay;
  655. unsigned long mmr;
  656. unsigned long base;
  657. int i, n, shift, m_io, max_io;
  658. int nasid, lnasid, fi, li;
  659. char *id;
  660. id = mmiohs[index].id;
  661. overlay.v = uv_read_local_mmr(mmiohs[index].overlay);
  662. pr_info("UV: %s overlay 0x%lx base:0x%x m_io:%d\n",
  663. id, overlay.v, overlay.s3.base, overlay.s3.m_io);
  664. if (!overlay.s3.enable) {
  665. pr_info("UV: %s disabled\n", id);
  666. return;
  667. }
  668. shift = UV3H_RH_GAM_MMIOH_OVERLAY_CONFIG0_MMR_BASE_SHFT;
  669. base = (unsigned long)overlay.s3.base;
  670. m_io = overlay.s3.m_io;
  671. mmr = mmiohs[index].redirect;
  672. n = UV3H_RH_GAM_MMIOH_REDIRECT_CONFIG0_MMR_DEPTH;
  673. min_pnode *= 2; /* convert to NASID */
  674. max_pnode *= 2;
  675. max_io = lnasid = fi = li = -1;
  676. for (i = 0; i < n; i++) {
  677. union uv3h_rh_gam_mmioh_redirect_config0_mmr_u redirect;
  678. redirect.v = uv_read_local_mmr(mmr + i * 8);
  679. nasid = redirect.s3.nasid;
  680. if (nasid < min_pnode || max_pnode < nasid)
  681. nasid = -1; /* invalid NASID */
  682. if (nasid == lnasid) {
  683. li = i;
  684. if (i != n-1) /* last entry check */
  685. continue;
  686. }
  687. /* check if we have a cached (or last) redirect to print */
  688. if (lnasid != -1 || (i == n-1 && nasid != -1)) {
  689. unsigned long addr1, addr2;
  690. int f, l;
  691. if (lnasid == -1) {
  692. f = l = i;
  693. lnasid = nasid;
  694. } else {
  695. f = fi;
  696. l = li;
  697. }
  698. addr1 = (base << shift) +
  699. f * (1ULL << m_io);
  700. addr2 = (base << shift) +
  701. (l + 1) * (1ULL << m_io);
  702. pr_info("UV: %s[%03d..%03d] NASID 0x%04x ADDR 0x%016lx - 0x%016lx\n",
  703. id, fi, li, lnasid, addr1, addr2);
  704. if (max_io < l)
  705. max_io = l;
  706. }
  707. fi = li = i;
  708. lnasid = nasid;
  709. }
  710. pr_info("UV: %s base:0x%lx shift:%d M_IO:%d MAX_IO:%d\n",
  711. id, base, shift, m_io, max_io);
  712. if (max_io >= 0)
  713. map_high(id, base, shift, m_io, max_io, map_uc);
  714. }
  715. static __init void map_mmioh_high(int min_pnode, int max_pnode)
  716. {
  717. union uvh_rh_gam_mmioh_overlay_config_mmr_u mmioh;
  718. unsigned long mmr, base;
  719. int shift, enable, m_io, n_io;
  720. if (is_uv3_hub() || is_uv4_hub()) {
  721. /* Map both MMIOH Regions */
  722. map_mmioh_high_uv3(0, min_pnode, max_pnode);
  723. map_mmioh_high_uv3(1, min_pnode, max_pnode);
  724. return;
  725. }
  726. if (is_uv1_hub()) {
  727. mmr = UV1H_RH_GAM_MMIOH_OVERLAY_CONFIG_MMR;
  728. shift = UV1H_RH_GAM_MMIOH_OVERLAY_CONFIG_MMR_BASE_SHFT;
  729. mmioh.v = uv_read_local_mmr(mmr);
  730. enable = !!mmioh.s1.enable;
  731. base = mmioh.s1.base;
  732. m_io = mmioh.s1.m_io;
  733. n_io = mmioh.s1.n_io;
  734. } else if (is_uv2_hub()) {
  735. mmr = UV2H_RH_GAM_MMIOH_OVERLAY_CONFIG_MMR;
  736. shift = UV2H_RH_GAM_MMIOH_OVERLAY_CONFIG_MMR_BASE_SHFT;
  737. mmioh.v = uv_read_local_mmr(mmr);
  738. enable = !!mmioh.s2.enable;
  739. base = mmioh.s2.base;
  740. m_io = mmioh.s2.m_io;
  741. n_io = mmioh.s2.n_io;
  742. } else
  743. return;
  744. if (enable) {
  745. max_pnode &= (1 << n_io) - 1;
  746. pr_info(
  747. "UV: base:0x%lx shift:%d N_IO:%d M_IO:%d max_pnode:0x%x\n",
  748. base, shift, m_io, n_io, max_pnode);
  749. map_high("MMIOH", base, shift, m_io, max_pnode, map_uc);
  750. } else {
  751. pr_info("UV: MMIOH disabled\n");
  752. }
  753. }
  754. static __init void map_low_mmrs(void)
  755. {
  756. init_extra_mapping_uc(UV_GLOBAL_MMR32_BASE, UV_GLOBAL_MMR32_SIZE);
  757. init_extra_mapping_uc(UV_LOCAL_MMR_BASE, UV_LOCAL_MMR_SIZE);
  758. }
  759. static __init void uv_rtc_init(void)
  760. {
  761. long status;
  762. u64 ticks_per_sec;
  763. status = uv_bios_freq_base(BIOS_FREQ_BASE_REALTIME_CLOCK,
  764. &ticks_per_sec);
  765. if (status != BIOS_STATUS_SUCCESS || ticks_per_sec < 100000) {
  766. printk(KERN_WARNING
  767. "unable to determine platform RTC clock frequency, "
  768. "guessing.\n");
  769. /* BIOS gives wrong value for clock freq. so guess */
  770. sn_rtc_cycles_per_second = 1000000000000UL / 30000UL;
  771. } else
  772. sn_rtc_cycles_per_second = ticks_per_sec;
  773. }
  774. /*
  775. * percpu heartbeat timer
  776. */
  777. static void uv_heartbeat(unsigned long ignored)
  778. {
  779. struct timer_list *timer = &uv_scir_info->timer;
  780. unsigned char bits = uv_scir_info->state;
  781. /* flip heartbeat bit */
  782. bits ^= SCIR_CPU_HEARTBEAT;
  783. /* is this cpu idle? */
  784. if (idle_cpu(raw_smp_processor_id()))
  785. bits &= ~SCIR_CPU_ACTIVITY;
  786. else
  787. bits |= SCIR_CPU_ACTIVITY;
  788. /* update system controller interface reg */
  789. uv_set_scir_bits(bits);
  790. /* enable next timer period */
  791. mod_timer(timer, jiffies + SCIR_CPU_HB_INTERVAL);
  792. }
  793. static int uv_heartbeat_enable(unsigned int cpu)
  794. {
  795. while (!uv_cpu_scir_info(cpu)->enabled) {
  796. struct timer_list *timer = &uv_cpu_scir_info(cpu)->timer;
  797. uv_set_cpu_scir_bits(cpu, SCIR_CPU_HEARTBEAT|SCIR_CPU_ACTIVITY);
  798. setup_pinned_timer(timer, uv_heartbeat, cpu);
  799. timer->expires = jiffies + SCIR_CPU_HB_INTERVAL;
  800. add_timer_on(timer, cpu);
  801. uv_cpu_scir_info(cpu)->enabled = 1;
  802. /* also ensure that boot cpu is enabled */
  803. cpu = 0;
  804. }
  805. return 0;
  806. }
  807. #ifdef CONFIG_HOTPLUG_CPU
  808. static int uv_heartbeat_disable(unsigned int cpu)
  809. {
  810. if (uv_cpu_scir_info(cpu)->enabled) {
  811. uv_cpu_scir_info(cpu)->enabled = 0;
  812. del_timer(&uv_cpu_scir_info(cpu)->timer);
  813. }
  814. uv_set_cpu_scir_bits(cpu, 0xff);
  815. return 0;
  816. }
  817. static __init void uv_scir_register_cpu_notifier(void)
  818. {
  819. cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/x2apic-uvx:online",
  820. uv_heartbeat_enable, uv_heartbeat_disable);
  821. }
  822. #else /* !CONFIG_HOTPLUG_CPU */
  823. static __init void uv_scir_register_cpu_notifier(void)
  824. {
  825. }
  826. static __init int uv_init_heartbeat(void)
  827. {
  828. int cpu;
  829. if (is_uv_system())
  830. for_each_online_cpu(cpu)
  831. uv_heartbeat_enable(cpu);
  832. return 0;
  833. }
  834. late_initcall(uv_init_heartbeat);
  835. #endif /* !CONFIG_HOTPLUG_CPU */
  836. /* Direct Legacy VGA I/O traffic to designated IOH */
  837. int uv_set_vga_state(struct pci_dev *pdev, bool decode,
  838. unsigned int command_bits, u32 flags)
  839. {
  840. int domain, bus, rc;
  841. PR_DEVEL("devfn %x decode %d cmd %x flags %d\n",
  842. pdev->devfn, decode, command_bits, flags);
  843. if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
  844. return 0;
  845. if ((command_bits & PCI_COMMAND_IO) == 0)
  846. return 0;
  847. domain = pci_domain_nr(pdev->bus);
  848. bus = pdev->bus->number;
  849. rc = uv_bios_set_legacy_vga_target(decode, domain, bus);
  850. PR_DEVEL("vga decode %d %x:%x, rc: %d\n", decode, domain, bus, rc);
  851. return rc;
  852. }
  853. /*
  854. * Called on each cpu to initialize the per_cpu UV data area.
  855. * FIXME: hotplug not supported yet
  856. */
  857. void uv_cpu_init(void)
  858. {
  859. /* CPU 0 initialization will be done via uv_system_init. */
  860. if (smp_processor_id() == 0)
  861. return;
  862. uv_hub_info->nr_online_cpus++;
  863. if (get_uv_system_type() == UV_NON_UNIQUE_APIC)
  864. set_x2apic_extra_bits(uv_hub_info->pnode);
  865. }
  866. struct mn {
  867. unsigned char m_val;
  868. unsigned char n_val;
  869. unsigned char m_shift;
  870. unsigned char n_lshift;
  871. };
  872. static void get_mn(struct mn *mnp)
  873. {
  874. union uvh_rh_gam_config_mmr_u m_n_config;
  875. union uv3h_gr0_gam_gr_config_u m_gr_config;
  876. m_n_config.v = uv_read_local_mmr(UVH_RH_GAM_CONFIG_MMR);
  877. mnp->n_val = m_n_config.s.n_skt;
  878. if (is_uv4_hub()) {
  879. mnp->m_val = 0;
  880. mnp->n_lshift = 0;
  881. } else if (is_uv3_hub()) {
  882. mnp->m_val = m_n_config.s3.m_skt;
  883. m_gr_config.v = uv_read_local_mmr(UV3H_GR0_GAM_GR_CONFIG);
  884. mnp->n_lshift = m_gr_config.s3.m_skt;
  885. } else if (is_uv2_hub()) {
  886. mnp->m_val = m_n_config.s2.m_skt;
  887. mnp->n_lshift = mnp->m_val == 40 ? 40 : 39;
  888. } else if (is_uv1_hub()) {
  889. mnp->m_val = m_n_config.s1.m_skt;
  890. mnp->n_lshift = mnp->m_val;
  891. }
  892. mnp->m_shift = mnp->m_val ? 64 - mnp->m_val : 0;
  893. }
  894. void __init uv_init_hub_info(struct uv_hub_info_s *hub_info)
  895. {
  896. struct mn mn = {0}; /* avoid unitialized warnings */
  897. union uvh_node_id_u node_id;
  898. get_mn(&mn);
  899. hub_info->m_val = mn.m_val;
  900. hub_info->n_val = mn.n_val;
  901. hub_info->m_shift = mn.m_shift;
  902. hub_info->n_lshift = mn.n_lshift ? mn.n_lshift : 0;
  903. hub_info->hub_revision = uv_hub_info->hub_revision;
  904. hub_info->pnode_mask = uv_cpuid.pnode_mask;
  905. hub_info->min_pnode = _min_pnode;
  906. hub_info->min_socket = _min_socket;
  907. hub_info->pnode_to_socket = _pnode_to_socket;
  908. hub_info->socket_to_node = _socket_to_node;
  909. hub_info->socket_to_pnode = _socket_to_pnode;
  910. hub_info->gr_table_len = _gr_table_len;
  911. hub_info->gr_table = _gr_table;
  912. hub_info->gpa_mask = mn.m_val ?
  913. (1UL << (mn.m_val + mn.n_val)) - 1 :
  914. (1UL << uv_cpuid.gpa_shift) - 1;
  915. node_id.v = uv_read_local_mmr(UVH_NODE_ID);
  916. hub_info->gnode_extra =
  917. (node_id.s.node_id & ~((1 << mn.n_val) - 1)) >> 1;
  918. hub_info->gnode_upper =
  919. ((unsigned long)hub_info->gnode_extra << mn.m_val);
  920. if (uv_gp_table) {
  921. hub_info->global_mmr_base = uv_gp_table->mmr_base;
  922. hub_info->global_mmr_shift = uv_gp_table->mmr_shift;
  923. hub_info->global_gru_base = uv_gp_table->gru_base;
  924. hub_info->global_gru_shift = uv_gp_table->gru_shift;
  925. hub_info->gpa_shift = uv_gp_table->gpa_shift;
  926. hub_info->gpa_mask = (1UL << hub_info->gpa_shift) - 1;
  927. } else {
  928. hub_info->global_mmr_base =
  929. uv_read_local_mmr(UVH_RH_GAM_MMR_OVERLAY_CONFIG_MMR) &
  930. ~UV_MMR_ENABLE;
  931. hub_info->global_mmr_shift = _UV_GLOBAL_MMR64_PNODE_SHIFT;
  932. }
  933. get_lowmem_redirect(
  934. &hub_info->lowmem_remap_base, &hub_info->lowmem_remap_top);
  935. hub_info->apic_pnode_shift = uv_cpuid.socketid_shift;
  936. /* show system specific info */
  937. pr_info("UV: N:%d M:%d m_shift:%d n_lshift:%d\n",
  938. hub_info->n_val, hub_info->m_val,
  939. hub_info->m_shift, hub_info->n_lshift);
  940. pr_info("UV: gpa_mask/shift:0x%lx/%d pnode_mask:0x%x apic_pns:%d\n",
  941. hub_info->gpa_mask, hub_info->gpa_shift,
  942. hub_info->pnode_mask, hub_info->apic_pnode_shift);
  943. pr_info("UV: mmr_base/shift:0x%lx/%ld gru_base/shift:0x%lx/%ld\n",
  944. hub_info->global_mmr_base, hub_info->global_mmr_shift,
  945. hub_info->global_gru_base, hub_info->global_gru_shift);
  946. pr_info("UV: gnode_upper:0x%lx gnode_extra:0x%x\n",
  947. hub_info->gnode_upper, hub_info->gnode_extra);
  948. }
  949. static void __init decode_gam_params(unsigned long ptr)
  950. {
  951. uv_gp_table = (struct uv_gam_parameters *)ptr;
  952. pr_info("UV: GAM Params...\n");
  953. pr_info("UV: mmr_base/shift:0x%llx/%d gru_base/shift:0x%llx/%d gpa_shift:%d\n",
  954. uv_gp_table->mmr_base, uv_gp_table->mmr_shift,
  955. uv_gp_table->gru_base, uv_gp_table->gru_shift,
  956. uv_gp_table->gpa_shift);
  957. }
  958. static void __init decode_gam_rng_tbl(unsigned long ptr)
  959. {
  960. struct uv_gam_range_entry *gre = (struct uv_gam_range_entry *)ptr;
  961. unsigned long lgre = 0;
  962. int index = 0;
  963. int sock_min = 999999, pnode_min = 99999;
  964. int sock_max = -1, pnode_max = -1;
  965. uv_gre_table = gre;
  966. for (; gre->type != UV_GAM_RANGE_TYPE_UNUSED; gre++) {
  967. if (!index) {
  968. pr_info("UV: GAM Range Table...\n");
  969. pr_info("UV: # %20s %14s %5s %4s %5s %3s %2s\n",
  970. "Range", "", "Size", "Type", "NASID",
  971. "SID", "PN");
  972. }
  973. pr_info(
  974. "UV: %2d: 0x%014lx-0x%014lx %5luG %3d %04x %02x %02x\n",
  975. index++,
  976. (unsigned long)lgre << UV_GAM_RANGE_SHFT,
  977. (unsigned long)gre->limit << UV_GAM_RANGE_SHFT,
  978. ((unsigned long)(gre->limit - lgre)) >>
  979. (30 - UV_GAM_RANGE_SHFT), /* 64M -> 1G */
  980. gre->type, gre->nasid, gre->sockid, gre->pnode);
  981. lgre = gre->limit;
  982. if (sock_min > gre->sockid)
  983. sock_min = gre->sockid;
  984. if (sock_max < gre->sockid)
  985. sock_max = gre->sockid;
  986. if (pnode_min > gre->pnode)
  987. pnode_min = gre->pnode;
  988. if (pnode_max < gre->pnode)
  989. pnode_max = gre->pnode;
  990. }
  991. _min_socket = sock_min;
  992. _max_socket = sock_max;
  993. _min_pnode = pnode_min;
  994. _max_pnode = pnode_max;
  995. _gr_table_len = index;
  996. pr_info(
  997. "UV: GRT: %d entries, sockets(min:%x,max:%x) pnodes(min:%x,max:%x)\n",
  998. index, _min_socket, _max_socket, _min_pnode, _max_pnode);
  999. }
  1000. static void __init decode_uv_systab(void)
  1001. {
  1002. struct uv_systab *st;
  1003. int i;
  1004. st = uv_systab;
  1005. if ((!st || st->revision < UV_SYSTAB_VERSION_UV4) && !is_uv4_hub())
  1006. return;
  1007. if (st->revision != UV_SYSTAB_VERSION_UV4_LATEST) {
  1008. pr_crit(
  1009. "UV: BIOS UVsystab version(%x) mismatch, expecting(%x)\n",
  1010. st->revision, UV_SYSTAB_VERSION_UV4_LATEST);
  1011. BUG();
  1012. }
  1013. for (i = 0; st->entry[i].type != UV_SYSTAB_TYPE_UNUSED; i++) {
  1014. unsigned long ptr = st->entry[i].offset;
  1015. if (!ptr)
  1016. continue;
  1017. ptr = ptr + (unsigned long)st;
  1018. switch (st->entry[i].type) {
  1019. case UV_SYSTAB_TYPE_GAM_PARAMS:
  1020. decode_gam_params(ptr);
  1021. break;
  1022. case UV_SYSTAB_TYPE_GAM_RNG_TBL:
  1023. decode_gam_rng_tbl(ptr);
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. /*
  1029. * Setup physical blade translations from UVH_NODE_PRESENT_TABLE
  1030. * .. NB: UVH_NODE_PRESENT_TABLE is going away,
  1031. * .. being replaced by GAM Range Table
  1032. */
  1033. static __init void boot_init_possible_blades(struct uv_hub_info_s *hub_info)
  1034. {
  1035. int i, uv_pb = 0;
  1036. pr_info("UV: NODE_PRESENT_DEPTH = %d\n", UVH_NODE_PRESENT_TABLE_DEPTH);
  1037. for (i = 0; i < UVH_NODE_PRESENT_TABLE_DEPTH; i++) {
  1038. unsigned long np;
  1039. np = uv_read_local_mmr(UVH_NODE_PRESENT_TABLE + i * 8);
  1040. if (np)
  1041. pr_info("UV: NODE_PRESENT(%d) = 0x%016lx\n", i, np);
  1042. uv_pb += hweight64(np);
  1043. }
  1044. if (uv_possible_blades != uv_pb)
  1045. uv_possible_blades = uv_pb;
  1046. }
  1047. static void __init build_socket_tables(void)
  1048. {
  1049. struct uv_gam_range_entry *gre = uv_gre_table;
  1050. int num, nump;
  1051. int cpu, i, lnid;
  1052. int minsock = _min_socket;
  1053. int maxsock = _max_socket;
  1054. int minpnode = _min_pnode;
  1055. int maxpnode = _max_pnode;
  1056. size_t bytes;
  1057. if (!gre) {
  1058. if (is_uv1_hub() || is_uv2_hub() || is_uv3_hub()) {
  1059. pr_info("UV: No UVsystab socket table, ignoring\n");
  1060. return; /* not required */
  1061. }
  1062. pr_crit(
  1063. "UV: Error: UVsystab address translations not available!\n");
  1064. BUG();
  1065. }
  1066. /* build socket id -> node id, pnode */
  1067. num = maxsock - minsock + 1;
  1068. bytes = num * sizeof(_socket_to_node[0]);
  1069. _socket_to_node = kmalloc(bytes, GFP_KERNEL);
  1070. _socket_to_pnode = kmalloc(bytes, GFP_KERNEL);
  1071. nump = maxpnode - minpnode + 1;
  1072. bytes = nump * sizeof(_pnode_to_socket[0]);
  1073. _pnode_to_socket = kmalloc(bytes, GFP_KERNEL);
  1074. BUG_ON(!_socket_to_node || !_socket_to_pnode || !_pnode_to_socket);
  1075. for (i = 0; i < num; i++)
  1076. _socket_to_node[i] = _socket_to_pnode[i] = SOCK_EMPTY;
  1077. for (i = 0; i < nump; i++)
  1078. _pnode_to_socket[i] = SOCK_EMPTY;
  1079. /* fill in pnode/node/addr conversion list values */
  1080. pr_info("UV: GAM Building socket/pnode conversion tables\n");
  1081. for (; gre->type != UV_GAM_RANGE_TYPE_UNUSED; gre++) {
  1082. if (gre->type == UV_GAM_RANGE_TYPE_HOLE)
  1083. continue;
  1084. i = gre->sockid - minsock;
  1085. if (_socket_to_pnode[i] != SOCK_EMPTY)
  1086. continue; /* duplicate */
  1087. _socket_to_pnode[i] = gre->pnode;
  1088. i = gre->pnode - minpnode;
  1089. _pnode_to_socket[i] = gre->sockid;
  1090. pr_info(
  1091. "UV: sid:%02x type:%d nasid:%04x pn:%02x pn2s:%2x\n",
  1092. gre->sockid, gre->type, gre->nasid,
  1093. _socket_to_pnode[gre->sockid - minsock],
  1094. _pnode_to_socket[gre->pnode - minpnode]);
  1095. }
  1096. /* Set socket -> node values */
  1097. lnid = -1;
  1098. for_each_present_cpu(cpu) {
  1099. int nid = cpu_to_node(cpu);
  1100. int apicid, sockid;
  1101. if (lnid == nid)
  1102. continue;
  1103. lnid = nid;
  1104. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  1105. sockid = apicid >> uv_cpuid.socketid_shift;
  1106. _socket_to_node[sockid - minsock] = nid;
  1107. pr_info("UV: sid:%02x: apicid:%04x node:%2d\n",
  1108. sockid, apicid, nid);
  1109. }
  1110. /* Setup physical blade to pnode translation from GAM Range Table */
  1111. bytes = num_possible_nodes() * sizeof(_node_to_pnode[0]);
  1112. _node_to_pnode = kmalloc(bytes, GFP_KERNEL);
  1113. BUG_ON(!_node_to_pnode);
  1114. for (lnid = 0; lnid < num_possible_nodes(); lnid++) {
  1115. unsigned short sockid;
  1116. for (sockid = minsock; sockid <= maxsock; sockid++) {
  1117. if (lnid == _socket_to_node[sockid - minsock]) {
  1118. _node_to_pnode[lnid] =
  1119. _socket_to_pnode[sockid - minsock];
  1120. break;
  1121. }
  1122. }
  1123. if (sockid > maxsock) {
  1124. pr_err("UV: socket for node %d not found!\n", lnid);
  1125. BUG();
  1126. }
  1127. }
  1128. /*
  1129. * If socket id == pnode or socket id == node for all nodes,
  1130. * system runs faster by removing corresponding conversion table.
  1131. */
  1132. pr_info("UV: Checking socket->node/pnode for identity maps\n");
  1133. if (minsock == 0) {
  1134. for (i = 0; i < num; i++)
  1135. if (_socket_to_node[i] == SOCK_EMPTY ||
  1136. i != _socket_to_node[i])
  1137. break;
  1138. if (i >= num) {
  1139. kfree(_socket_to_node);
  1140. _socket_to_node = NULL;
  1141. pr_info("UV: 1:1 socket_to_node table removed\n");
  1142. }
  1143. }
  1144. if (minsock == minpnode) {
  1145. for (i = 0; i < num; i++)
  1146. if (_socket_to_pnode[i] != SOCK_EMPTY &&
  1147. _socket_to_pnode[i] != i + minpnode)
  1148. break;
  1149. if (i >= num) {
  1150. kfree(_socket_to_pnode);
  1151. _socket_to_pnode = NULL;
  1152. pr_info("UV: 1:1 socket_to_pnode table removed\n");
  1153. }
  1154. }
  1155. }
  1156. void __init uv_system_init(void)
  1157. {
  1158. struct uv_hub_info_s hub_info = {0};
  1159. int bytes, cpu, nodeid;
  1160. unsigned short min_pnode = 9999, max_pnode = 0;
  1161. char *hub = is_uv4_hub() ? "UV400" :
  1162. is_uv3_hub() ? "UV300" :
  1163. is_uv2_hub() ? "UV2000/3000" :
  1164. is_uv1_hub() ? "UV100/1000" : NULL;
  1165. if (!hub) {
  1166. pr_err("UV: Unknown/unsupported UV hub\n");
  1167. return;
  1168. }
  1169. pr_info("UV: Found %s hub\n", hub);
  1170. map_low_mmrs();
  1171. uv_bios_init(); /* get uv_systab for decoding */
  1172. decode_uv_systab();
  1173. build_socket_tables();
  1174. build_uv_gr_table();
  1175. uv_init_hub_info(&hub_info);
  1176. uv_possible_blades = num_possible_nodes();
  1177. if (!_node_to_pnode)
  1178. boot_init_possible_blades(&hub_info);
  1179. /* uv_num_possible_blades() is really the hub count */
  1180. pr_info("UV: Found %d hubs, %d nodes, %d cpus\n",
  1181. uv_num_possible_blades(),
  1182. num_possible_nodes(),
  1183. num_possible_cpus());
  1184. uv_bios_get_sn_info(0, &uv_type, &sn_partition_id, &sn_coherency_id,
  1185. &sn_region_size, &system_serial_number);
  1186. hub_info.coherency_domain_number = sn_coherency_id;
  1187. uv_rtc_init();
  1188. bytes = sizeof(void *) * uv_num_possible_blades();
  1189. __uv_hub_info_list = kzalloc(bytes, GFP_KERNEL);
  1190. BUG_ON(!__uv_hub_info_list);
  1191. bytes = sizeof(struct uv_hub_info_s);
  1192. for_each_node(nodeid) {
  1193. struct uv_hub_info_s *new_hub;
  1194. if (__uv_hub_info_list[nodeid]) {
  1195. pr_err("UV: Node %d UV HUB already initialized!?\n",
  1196. nodeid);
  1197. BUG();
  1198. }
  1199. /* Allocate new per hub info list */
  1200. new_hub = (nodeid == 0) ?
  1201. &uv_hub_info_node0 :
  1202. kzalloc_node(bytes, GFP_KERNEL, nodeid);
  1203. BUG_ON(!new_hub);
  1204. __uv_hub_info_list[nodeid] = new_hub;
  1205. new_hub = uv_hub_info_list(nodeid);
  1206. BUG_ON(!new_hub);
  1207. *new_hub = hub_info;
  1208. /* Use information from GAM table if available */
  1209. if (_node_to_pnode)
  1210. new_hub->pnode = _node_to_pnode[nodeid];
  1211. else /* Fill in during cpu loop */
  1212. new_hub->pnode = 0xffff;
  1213. new_hub->numa_blade_id = uv_node_to_blade_id(nodeid);
  1214. new_hub->memory_nid = -1;
  1215. new_hub->nr_possible_cpus = 0;
  1216. new_hub->nr_online_cpus = 0;
  1217. }
  1218. /* Initialize per cpu info */
  1219. for_each_possible_cpu(cpu) {
  1220. int apicid = per_cpu(x86_cpu_to_apicid, cpu);
  1221. int numa_node_id;
  1222. unsigned short pnode;
  1223. nodeid = cpu_to_node(cpu);
  1224. numa_node_id = numa_cpu_node(cpu);
  1225. pnode = uv_apicid_to_pnode(apicid);
  1226. uv_cpu_info_per(cpu)->p_uv_hub_info = uv_hub_info_list(nodeid);
  1227. uv_cpu_info_per(cpu)->blade_cpu_id =
  1228. uv_cpu_hub_info(cpu)->nr_possible_cpus++;
  1229. if (uv_cpu_hub_info(cpu)->memory_nid == -1)
  1230. uv_cpu_hub_info(cpu)->memory_nid = cpu_to_node(cpu);
  1231. if (nodeid != numa_node_id && /* init memoryless node */
  1232. uv_hub_info_list(numa_node_id)->pnode == 0xffff)
  1233. uv_hub_info_list(numa_node_id)->pnode = pnode;
  1234. else if (uv_cpu_hub_info(cpu)->pnode == 0xffff)
  1235. uv_cpu_hub_info(cpu)->pnode = pnode;
  1236. uv_cpu_scir_info(cpu)->offset = uv_scir_offset(apicid);
  1237. }
  1238. for_each_node(nodeid) {
  1239. unsigned short pnode = uv_hub_info_list(nodeid)->pnode;
  1240. /* Add pnode info for pre-GAM list nodes without cpus */
  1241. if (pnode == 0xffff) {
  1242. unsigned long paddr;
  1243. paddr = node_start_pfn(nodeid) << PAGE_SHIFT;
  1244. pnode = uv_gpa_to_pnode(uv_soc_phys_ram_to_gpa(paddr));
  1245. uv_hub_info_list(nodeid)->pnode = pnode;
  1246. }
  1247. min_pnode = min(pnode, min_pnode);
  1248. max_pnode = max(pnode, max_pnode);
  1249. pr_info("UV: UVHUB node:%2d pn:%02x nrcpus:%d\n",
  1250. nodeid,
  1251. uv_hub_info_list(nodeid)->pnode,
  1252. uv_hub_info_list(nodeid)->nr_possible_cpus);
  1253. }
  1254. pr_info("UV: min_pnode:%02x max_pnode:%02x\n", min_pnode, max_pnode);
  1255. map_gru_high(max_pnode);
  1256. map_mmr_high(max_pnode);
  1257. map_mmioh_high(min_pnode, max_pnode);
  1258. uv_nmi_setup();
  1259. uv_cpu_init();
  1260. uv_scir_register_cpu_notifier();
  1261. proc_mkdir("sgi_uv", NULL);
  1262. /* register Legacy VGA I/O redirection handler */
  1263. pci_register_set_vga_state(uv_set_vga_state);
  1264. /*
  1265. * For a kdump kernel the reset must be BOOT_ACPI, not BOOT_EFI, as
  1266. * EFI is not enabled in the kdump kernel.
  1267. */
  1268. if (is_kdump_kernel())
  1269. reboot_type = BOOT_ACPI;
  1270. }
  1271. apic_driver(apic_x2apic_uv_x);