1 // SPDX-License-Identifier: GPL-2.0
3 * OS info memory interface
5 * Copyright IBM Corp. 2012
9 #define KMSG_COMPONENT "os_info"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/crash_dump.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <asm/checksum.h>
16 #include <asm/abs_lowcore.h>
17 #include <asm/os_info.h>
18 #include <asm/maccess.h>
19 #include <asm/asm-offsets.h>
22 * OS info structure has to be page aligned
24 static struct os_info os_info __page_aligned_data;
27 * Compute checksum over OS info structure
29 u32 os_info_csum(struct os_info *os_info)
31 int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
32 return (__force u32)csum_partial(&os_info->version_major, size, 0);
36 * Add crashkernel info to OS info and update checksum
38 void os_info_crashkernel_add(unsigned long base, unsigned long size)
40 os_info.crashkernel_addr = (u64)(unsigned long)base;
41 os_info.crashkernel_size = (u64)(unsigned long)size;
42 os_info.csum = os_info_csum(&os_info);
46 * Add OS info entry and update checksum
48 void os_info_entry_add(int nr, void *ptr, u64 size)
50 os_info.entry[nr].addr = __pa(ptr);
51 os_info.entry[nr].size = size;
52 os_info.entry[nr].csum = (__force u32)csum_partial(ptr, size, 0);
53 os_info.csum = os_info_csum(&os_info);
57 * Initialize OS info structure and set lowcore pointer
59 void __init os_info_init(void)
61 struct lowcore *abs_lc;
63 os_info.version_major = OS_INFO_VERSION_MAJOR;
64 os_info.version_minor = OS_INFO_VERSION_MINOR;
65 os_info.magic = OS_INFO_MAGIC;
66 os_info.csum = os_info_csum(&os_info);
67 abs_lc = get_abs_lowcore();
68 abs_lc->os_info = __pa(&os_info);
69 put_abs_lowcore(abs_lc);
72 #ifdef CONFIG_CRASH_DUMP
74 static struct os_info *os_info_old;
77 * Allocate and copy OS info entry from oldmem
79 static void os_info_old_alloc(int nr, int align)
81 unsigned long addr, size = 0;
82 char *buf, *buf_align, *msg;
85 addr = os_info_old->entry[nr].addr;
87 msg = "not available";
90 size = os_info_old->entry[nr].size;
91 buf = kmalloc(size + align - 1, GFP_KERNEL);
96 buf_align = PTR_ALIGN(buf, align);
97 if (copy_oldmem_kernel(buf_align, addr, size)) {
101 csum = (__force u32)csum_partial(buf_align, size, 0);
102 if (csum != os_info_old->entry[nr].csum) {
103 msg = "checksum failed";
106 os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
112 os_info_old->entry[nr].addr = 0;
114 pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
115 nr, msg, addr, size);
119 * Initialize os info and os info entries from oldmem
121 static void os_info_old_init(void)
123 static int os_info_init;
128 if (!oldmem_data.start)
130 if (copy_oldmem_kernel(&addr, __LC_OS_INFO, sizeof(addr)))
132 if (addr == 0 || addr % PAGE_SIZE)
134 os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
137 if (copy_oldmem_kernel(os_info_old, addr, sizeof(*os_info_old)))
139 if (os_info_old->magic != OS_INFO_MAGIC)
141 if (os_info_old->csum != os_info_csum(os_info_old))
143 if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
145 os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
146 os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
147 pr_info("crashkernel: addr=0x%lx size=%lu\n",
148 (unsigned long) os_info_old->crashkernel_addr,
149 (unsigned long) os_info_old->crashkernel_size);
160 * Return pointer to os infor entry and its size
162 void *os_info_old_entry(int nr, unsigned long *size)
168 if (!os_info_old->entry[nr].addr)
170 *size = (unsigned long) os_info_old->entry[nr].size;
171 return (void *)(unsigned long)os_info_old->entry[nr].addr;