physmem_info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define boot_fmt(fmt) "physmem: " fmt
  3. #include <linux/processor.h>
  4. #include <linux/errno.h>
  5. #include <linux/init.h>
  6. #include <asm/physmem_info.h>
  7. #include <asm/stacktrace.h>
  8. #include <asm/boot_data.h>
  9. #include <asm/sparsemem.h>
  10. #include <asm/sections.h>
  11. #include <asm/setup.h>
  12. #include <asm/sclp.h>
  13. #include <asm/asm.h>
  14. #include <asm/uv.h>
  15. #include "decompressor.h"
  16. #include "boot.h"
  17. struct physmem_info __bootdata(physmem_info);
  18. static unsigned int physmem_alloc_ranges;
  19. static unsigned long physmem_alloc_pos;
  20. /* up to 256 storage elements, 1020 subincrements each */
  21. #define ENTRIES_EXTENDED_MAX \
  22. (256 * (1020 / 2) * sizeof(struct physmem_range))
  23. static struct physmem_range *__get_physmem_range_ptr(u32 n)
  24. {
  25. if (n < MEM_INLINED_ENTRIES)
  26. return &physmem_info.online[n];
  27. if (unlikely(!physmem_info.online_extended)) {
  28. physmem_info.online_extended = (struct physmem_range *)physmem_alloc_range(
  29. RR_MEM_DETECT_EXT, ENTRIES_EXTENDED_MAX, sizeof(long), 0,
  30. physmem_alloc_pos, true);
  31. }
  32. return &physmem_info.online_extended[n - MEM_INLINED_ENTRIES];
  33. }
  34. /*
  35. * sequential calls to add_physmem_online_range with adjacent memory ranges
  36. * are merged together into single memory range.
  37. */
  38. void add_physmem_online_range(u64 start, u64 end)
  39. {
  40. struct physmem_range *range;
  41. if (physmem_info.range_count) {
  42. range = __get_physmem_range_ptr(physmem_info.range_count - 1);
  43. if (range->end == start) {
  44. range->end = end;
  45. return;
  46. }
  47. }
  48. range = __get_physmem_range_ptr(physmem_info.range_count);
  49. range->start = start;
  50. range->end = end;
  51. physmem_info.range_count++;
  52. }
  53. static int __diag260(unsigned long rx1, unsigned long rx2)
  54. {
  55. union register_pair rx;
  56. int cc, exception;
  57. unsigned long ry;
  58. rx.even = rx1;
  59. rx.odd = rx2;
  60. ry = 0x10; /* storage configuration */
  61. exception = 1;
  62. asm_inline volatile(
  63. " diag %[rx],%[ry],0x260\n"
  64. "0: lhi %[exc],0\n"
  65. "1:\n"
  66. CC_IPM(cc)
  67. EX_TABLE(0b, 1b)
  68. : CC_OUT(cc, cc), [exc] "+d" (exception), [ry] "+d" (ry)
  69. : [rx] "d" (rx.pair)
  70. : CC_CLOBBER_LIST("memory"));
  71. cc = exception ? -1 : CC_TRANSFORM(cc);
  72. return cc == 0 ? ry : -1;
  73. }
  74. static int diag260(void)
  75. {
  76. int rc, i;
  77. struct {
  78. unsigned long start;
  79. unsigned long end;
  80. } storage_extents[8] __aligned(16); /* VM supports up to 8 extends */
  81. memset(storage_extents, 0, sizeof(storage_extents));
  82. rc = __diag260((unsigned long)storage_extents, sizeof(storage_extents));
  83. if (rc == -1)
  84. return -1;
  85. for (i = 0; i < min_t(int, rc, ARRAY_SIZE(storage_extents)); i++)
  86. add_physmem_online_range(storage_extents[i].start, storage_extents[i].end + 1);
  87. return 0;
  88. }
  89. #define DIAG500_SC_STOR_LIMIT 4
  90. static int diag500_storage_limit(unsigned long *max_physmem_end)
  91. {
  92. unsigned long storage_limit;
  93. asm_inline volatile(
  94. " lghi %%r1,%[subcode]\n"
  95. " lghi %%r2,0\n"
  96. " diag %%r2,%%r4,0x500\n"
  97. "0: lgr %[slimit],%%r2\n"
  98. EX_TABLE(0b, 0b)
  99. : [slimit] "=d" (storage_limit)
  100. : [subcode] "i" (DIAG500_SC_STOR_LIMIT)
  101. : "memory", "1", "2");
  102. if (!storage_limit)
  103. return -EINVAL;
  104. /* Convert inclusive end to exclusive end */
  105. *max_physmem_end = storage_limit + 1;
  106. return 0;
  107. }
  108. static int tprot(unsigned long addr)
  109. {
  110. int cc, exception;
  111. exception = 1;
  112. asm_inline volatile(
  113. " tprot 0(%[addr]),0\n"
  114. "0: lhi %[exc],0\n"
  115. "1:\n"
  116. CC_IPM(cc)
  117. EX_TABLE(0b, 1b)
  118. : CC_OUT(cc, cc), [exc] "+d" (exception)
  119. : [addr] "a" (addr)
  120. : CC_CLOBBER_LIST("memory"));
  121. cc = exception ? -EFAULT : CC_TRANSFORM(cc);
  122. return cc;
  123. }
  124. static unsigned long search_mem_end(void)
  125. {
  126. unsigned long range = 1 << (MAX_PHYSMEM_BITS - 20); /* in 1MB blocks */
  127. unsigned long offset = 0;
  128. unsigned long pivot;
  129. while (range > 1) {
  130. range >>= 1;
  131. pivot = offset + range;
  132. if (!tprot(pivot << 20))
  133. offset = pivot;
  134. }
  135. return (offset + 1) << 20;
  136. }
  137. unsigned long detect_max_physmem_end(void)
  138. {
  139. unsigned long max_physmem_end = 0;
  140. if (!diag500_storage_limit(&max_physmem_end)) {
  141. physmem_info.info_source = MEM_DETECT_DIAG500_STOR_LIMIT;
  142. } else if (!sclp_early_get_memsize(&max_physmem_end)) {
  143. physmem_info.info_source = MEM_DETECT_SCLP_READ_INFO;
  144. } else {
  145. max_physmem_end = search_mem_end();
  146. physmem_info.info_source = MEM_DETECT_BIN_SEARCH;
  147. }
  148. boot_debug("Max physical memory: 0x%016lx (info source: %s)\n", max_physmem_end,
  149. get_physmem_info_source());
  150. return max_physmem_end;
  151. }
  152. void detect_physmem_online_ranges(unsigned long max_physmem_end)
  153. {
  154. unsigned long start, end;
  155. int i;
  156. if (!sclp_early_read_storage_info()) {
  157. physmem_info.info_source = MEM_DETECT_SCLP_STOR_INFO;
  158. } else if (physmem_info.info_source == MEM_DETECT_DIAG500_STOR_LIMIT) {
  159. unsigned long online_end;
  160. if (!sclp_early_get_memsize(&online_end)) {
  161. physmem_info.info_source = MEM_DETECT_SCLP_READ_INFO;
  162. add_physmem_online_range(0, online_end);
  163. }
  164. } else if (!diag260()) {
  165. physmem_info.info_source = MEM_DETECT_DIAG260;
  166. } else if (max_physmem_end) {
  167. add_physmem_online_range(0, max_physmem_end);
  168. }
  169. boot_debug("Online memory ranges (info source: %s):\n", get_physmem_info_source());
  170. for_each_physmem_online_range(i, &start, &end)
  171. boot_debug(" online [%d]: 0x%016lx-0x%016lx\n", i, start, end);
  172. }
  173. void physmem_set_usable_limit(unsigned long limit)
  174. {
  175. physmem_info.usable = limit;
  176. physmem_alloc_pos = limit;
  177. boot_debug("Usable memory limit: 0x%016lx\n", limit);
  178. }
  179. static void die_oom(unsigned long size, unsigned long align, unsigned long min, unsigned long max)
  180. {
  181. unsigned long start, end, total_mem = 0, total_reserved_mem = 0;
  182. struct reserved_range *range;
  183. enum reserved_range_type t;
  184. int i;
  185. boot_emerg("Linux version %s\n", kernel_version);
  186. if (!is_prot_virt_guest() && early_command_line[0])
  187. boot_emerg("Kernel command line: %s\n", early_command_line);
  188. boot_emerg("Out of memory allocating %lu bytes 0x%lx aligned in range %lx:%lx\n",
  189. size, align, min, max);
  190. boot_emerg("Reserved memory ranges:\n");
  191. for_each_physmem_reserved_range(t, range, &start, &end) {
  192. boot_emerg("%016lx %016lx %s\n", start, end, get_rr_type_name(t));
  193. total_reserved_mem += end - start;
  194. }
  195. boot_emerg("Usable online memory ranges (info source: %s [%d]):\n",
  196. get_physmem_info_source(), physmem_info.info_source);
  197. for_each_physmem_usable_range(i, &start, &end) {
  198. boot_emerg("%016lx %016lx\n", start, end);
  199. total_mem += end - start;
  200. }
  201. boot_emerg("Usable online memory total: %lu Reserved: %lu Free: %lu\n",
  202. total_mem, total_reserved_mem,
  203. total_mem > total_reserved_mem ? total_mem - total_reserved_mem : 0);
  204. boot_panic("Oom\n");
  205. }
  206. static void _physmem_reserve(enum reserved_range_type type, unsigned long addr, unsigned long size)
  207. {
  208. physmem_info.reserved[type].start = addr;
  209. physmem_info.reserved[type].end = addr + size;
  210. }
  211. void physmem_reserve(enum reserved_range_type type, unsigned long addr, unsigned long size)
  212. {
  213. _physmem_reserve(type, addr, size);
  214. boot_debug("%-14s 0x%016lx-0x%016lx %s\n", "Reserve:", addr, addr + size,
  215. get_rr_type_name(type));
  216. }
  217. void physmem_free(enum reserved_range_type type)
  218. {
  219. boot_debug("%-14s 0x%016lx-0x%016lx %s\n", "Free:", physmem_info.reserved[type].start,
  220. physmem_info.reserved[type].end, get_rr_type_name(type));
  221. physmem_info.reserved[type].start = 0;
  222. physmem_info.reserved[type].end = 0;
  223. }
  224. static bool __physmem_alloc_intersects(unsigned long addr, unsigned long size,
  225. unsigned long *intersection_start)
  226. {
  227. unsigned long res_addr, res_size;
  228. int t;
  229. for (t = 0; t < RR_MAX; t++) {
  230. if (!get_physmem_reserved(t, &res_addr, &res_size))
  231. continue;
  232. if (intersects(addr, size, res_addr, res_size)) {
  233. *intersection_start = res_addr;
  234. return true;
  235. }
  236. }
  237. return ipl_report_certs_intersects(addr, size, intersection_start);
  238. }
  239. static unsigned long __physmem_alloc_range(unsigned long size, unsigned long align,
  240. unsigned long min, unsigned long max,
  241. unsigned int from_ranges, unsigned int *ranges_left,
  242. bool die_on_oom)
  243. {
  244. unsigned int nranges = from_ranges ?: physmem_info.range_count;
  245. unsigned long range_start, range_end;
  246. unsigned long intersection_start;
  247. unsigned long addr, pos = max;
  248. align = max(align, 8UL);
  249. while (nranges) {
  250. __get_physmem_range(nranges - 1, &range_start, &range_end, false);
  251. pos = min(range_end, pos);
  252. if (round_up(min, align) + size > pos)
  253. break;
  254. addr = round_down(pos - size, align);
  255. if (range_start > addr) {
  256. nranges--;
  257. continue;
  258. }
  259. if (__physmem_alloc_intersects(addr, size, &intersection_start)) {
  260. pos = intersection_start;
  261. continue;
  262. }
  263. if (ranges_left)
  264. *ranges_left = nranges;
  265. return addr;
  266. }
  267. if (die_on_oom)
  268. die_oom(size, align, min, max);
  269. return 0;
  270. }
  271. unsigned long physmem_alloc_range(enum reserved_range_type type, unsigned long size,
  272. unsigned long align, unsigned long min, unsigned long max,
  273. bool die_on_oom)
  274. {
  275. unsigned long addr;
  276. max = min(max, physmem_alloc_pos);
  277. addr = __physmem_alloc_range(size, align, min, max, 0, NULL, die_on_oom);
  278. if (addr)
  279. _physmem_reserve(type, addr, size);
  280. boot_debug("%-14s 0x%016lx-0x%016lx %s\n", "Alloc range:", addr, addr + size,
  281. get_rr_type_name(type));
  282. return addr;
  283. }
  284. unsigned long physmem_alloc(enum reserved_range_type type, unsigned long size,
  285. unsigned long align, bool die_on_oom)
  286. {
  287. struct reserved_range *range = &physmem_info.reserved[type];
  288. struct reserved_range *new_range = NULL;
  289. unsigned int ranges_left;
  290. unsigned long addr;
  291. addr = __physmem_alloc_range(size, align, 0, physmem_alloc_pos, physmem_alloc_ranges,
  292. &ranges_left, die_on_oom);
  293. if (!addr)
  294. return 0;
  295. /* if not a consecutive allocation of the same type or first allocation */
  296. if (range->start != addr + size) {
  297. if (range->end) {
  298. addr = __physmem_alloc_range(sizeof(struct reserved_range), 0, 0,
  299. physmem_alloc_pos, physmem_alloc_ranges,
  300. &ranges_left, true);
  301. new_range = (struct reserved_range *)addr;
  302. addr = __physmem_alloc_range(size, align, 0, addr, ranges_left,
  303. &ranges_left, die_on_oom);
  304. if (!addr)
  305. return 0;
  306. *new_range = *range;
  307. range->chain = new_range;
  308. }
  309. range->end = addr + size;
  310. }
  311. if (type != RR_VMEM) {
  312. boot_debug("%-14s 0x%016lx-0x%016lx %-20s align 0x%lx split %d\n", "Alloc topdown:",
  313. addr, addr + size, get_rr_type_name(type), align, !!new_range);
  314. }
  315. range->start = addr;
  316. physmem_alloc_pos = addr;
  317. physmem_alloc_ranges = ranges_left;
  318. return addr;
  319. }
  320. unsigned long physmem_alloc_or_die(enum reserved_range_type type, unsigned long size,
  321. unsigned long align)
  322. {
  323. return physmem_alloc(type, size, align, true);
  324. }
  325. unsigned long get_physmem_alloc_pos(void)
  326. {
  327. return physmem_alloc_pos;
  328. }
  329. void dump_physmem_reserved(void)
  330. {
  331. struct reserved_range *range;
  332. enum reserved_range_type t;
  333. unsigned long start, end;
  334. boot_debug("Reserved memory ranges:\n");
  335. for_each_physmem_reserved_range(t, range, &start, &end) {
  336. if (end) {
  337. boot_debug("%-14s 0x%016lx-0x%016lx @%012lx chain %012lx\n",
  338. get_rr_type_name(t), start, end, (unsigned long)range,
  339. (unsigned long)range->chain);
  340. }
  341. }
  342. }