x2apic_uv_x.c 43 KB

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