Merge drm/drm-next into drm-misc-next
[linux-2.6-microblaze.git] / arch / s390 / boot / mem_detect.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/errno.h>
3 #include <linux/init.h>
4 #include <asm/sclp.h>
5 #include <asm/sections.h>
6 #include <asm/mem_detect.h>
7 #include <asm/sparsemem.h>
8 #include "compressed/decompressor.h"
9 #include "boot.h"
10
11 struct mem_detect_info __bootdata(mem_detect);
12
13 /* up to 256 storage elements, 1020 subincrements each */
14 #define ENTRIES_EXTENDED_MAX                                                   \
15         (256 * (1020 / 2) * sizeof(struct mem_detect_block))
16
17 /*
18  * To avoid corrupting old kernel memory during dump, find lowest memory
19  * chunk possible either right after the kernel end (decompressed kernel) or
20  * after initrd (if it is present and there is no hole between the kernel end
21  * and initrd)
22  */
23 static void *mem_detect_alloc_extended(void)
24 {
25         unsigned long offset = ALIGN(mem_safe_offset(), sizeof(u64));
26
27         if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && INITRD_START && INITRD_SIZE &&
28             INITRD_START < offset + ENTRIES_EXTENDED_MAX)
29                 offset = ALIGN(INITRD_START + INITRD_SIZE, sizeof(u64));
30
31         return (void *)offset;
32 }
33
34 static struct mem_detect_block *__get_mem_detect_block_ptr(u32 n)
35 {
36         if (n < MEM_INLINED_ENTRIES)
37                 return &mem_detect.entries[n];
38         if (unlikely(!mem_detect.entries_extended))
39                 mem_detect.entries_extended = mem_detect_alloc_extended();
40         return &mem_detect.entries_extended[n - MEM_INLINED_ENTRIES];
41 }
42
43 /*
44  * sequential calls to add_mem_detect_block with adjacent memory areas
45  * are merged together into single memory block.
46  */
47 void add_mem_detect_block(u64 start, u64 end)
48 {
49         struct mem_detect_block *block;
50
51         if (mem_detect.count) {
52                 block = __get_mem_detect_block_ptr(mem_detect.count - 1);
53                 if (block->end == start) {
54                         block->end = end;
55                         return;
56                 }
57         }
58
59         block = __get_mem_detect_block_ptr(mem_detect.count);
60         block->start = start;
61         block->end = end;
62         mem_detect.count++;
63 }
64
65 static int __diag260(unsigned long rx1, unsigned long rx2)
66 {
67         register unsigned long _rx1 asm("2") = rx1;
68         register unsigned long _rx2 asm("3") = rx2;
69         register unsigned long _ry asm("4") = 0x10; /* storage configuration */
70         int rc = -1;                                /* fail */
71         unsigned long reg1, reg2;
72         psw_t old = S390_lowcore.program_new_psw;
73
74         asm volatile(
75                 "       epsw    %0,%1\n"
76                 "       st      %0,%[psw_pgm]\n"
77                 "       st      %1,%[psw_pgm]+4\n"
78                 "       larl    %0,1f\n"
79                 "       stg     %0,%[psw_pgm]+8\n"
80                 "       diag    %[rx],%[ry],0x260\n"
81                 "       ipm     %[rc]\n"
82                 "       srl     %[rc],28\n"
83                 "1:\n"
84                 : "=&d" (reg1), "=&a" (reg2),
85                   [psw_pgm] "=Q" (S390_lowcore.program_new_psw),
86                   [rc] "+&d" (rc), [ry] "+d" (_ry)
87                 : [rx] "d" (_rx1), "d" (_rx2)
88                 : "cc", "memory");
89         S390_lowcore.program_new_psw = old;
90         return rc == 0 ? _ry : -1;
91 }
92
93 static int diag260(void)
94 {
95         int rc, i;
96
97         struct {
98                 unsigned long start;
99                 unsigned long end;
100         } storage_extents[8] __aligned(16); /* VM supports up to 8 extends */
101
102         memset(storage_extents, 0, sizeof(storage_extents));
103         rc = __diag260((unsigned long)storage_extents, sizeof(storage_extents));
104         if (rc == -1)
105                 return -1;
106
107         for (i = 0; i < min_t(int, rc, ARRAY_SIZE(storage_extents)); i++)
108                 add_mem_detect_block(storage_extents[i].start, storage_extents[i].end + 1);
109         return 0;
110 }
111
112 static int tprot(unsigned long addr)
113 {
114         unsigned long pgm_addr;
115         int rc = -EFAULT;
116         psw_t old = S390_lowcore.program_new_psw;
117
118         S390_lowcore.program_new_psw.mask = __extract_psw();
119         asm volatile(
120                 "       larl    %[pgm_addr],1f\n"
121                 "       stg     %[pgm_addr],%[psw_pgm_addr]\n"
122                 "       tprot   0(%[addr]),0\n"
123                 "       ipm     %[rc]\n"
124                 "       srl     %[rc],28\n"
125                 "1:\n"
126                 : [pgm_addr] "=&d"(pgm_addr),
127                   [psw_pgm_addr] "=Q"(S390_lowcore.program_new_psw.addr),
128                   [rc] "+&d"(rc)
129                 : [addr] "a"(addr)
130                 : "cc", "memory");
131         S390_lowcore.program_new_psw = old;
132         return rc;
133 }
134
135 static void search_mem_end(void)
136 {
137         unsigned long range = 1 << (MAX_PHYSMEM_BITS - 20); /* in 1MB blocks */
138         unsigned long offset = 0;
139         unsigned long pivot;
140
141         while (range > 1) {
142                 range >>= 1;
143                 pivot = offset + range;
144                 if (!tprot(pivot << 20))
145                         offset = pivot;
146         }
147
148         add_mem_detect_block(0, (offset + 1) << 20);
149 }
150
151 unsigned long detect_memory(void)
152 {
153         unsigned long max_physmem_end;
154
155         sclp_early_get_memsize(&max_physmem_end);
156
157         if (!sclp_early_read_storage_info()) {
158                 mem_detect.info_source = MEM_DETECT_SCLP_STOR_INFO;
159                 return max_physmem_end;
160         }
161
162         if (!diag260()) {
163                 mem_detect.info_source = MEM_DETECT_DIAG260;
164                 return max_physmem_end;
165         }
166
167         if (max_physmem_end) {
168                 add_mem_detect_block(0, max_physmem_end);
169                 mem_detect.info_source = MEM_DETECT_SCLP_READ_INFO;
170                 return max_physmem_end;
171         }
172
173         search_mem_end();
174         mem_detect.info_source = MEM_DETECT_BIN_SEARCH;
175         return get_mem_detect_end();
176 }