phyp_dump.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Hypervisor-assisted dump
  3. *
  4. * Linas Vepstas, Manish Ahuja 2008
  5. * Copyright 2008 IBM Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kobject.h>
  15. #include <linux/mm.h>
  16. #include <linux/of.h>
  17. #include <linux/pfn.h>
  18. #include <linux/swap.h>
  19. #include <linux/sysfs.h>
  20. #include <asm/page.h>
  21. #include <asm/phyp_dump.h>
  22. #include <asm/machdep.h>
  23. #include <asm/prom.h>
  24. #include <asm/rtas.h>
  25. /* Variables, used to communicate data between early boot and late boot */
  26. static struct phyp_dump phyp_dump_vars;
  27. struct phyp_dump *phyp_dump_info = &phyp_dump_vars;
  28. static int ibm_configure_kernel_dump;
  29. /* ------------------------------------------------- */
  30. /* RTAS interfaces to declare the dump regions */
  31. struct dump_section {
  32. u32 dump_flags;
  33. u16 source_type;
  34. u16 error_flags;
  35. u64 source_address;
  36. u64 source_length;
  37. u64 length_copied;
  38. u64 destination_address;
  39. };
  40. struct phyp_dump_header {
  41. u32 version;
  42. u16 num_of_sections;
  43. u16 status;
  44. u32 first_offset_section;
  45. u32 dump_disk_section;
  46. u64 block_num_dd;
  47. u64 num_of_blocks_dd;
  48. u32 offset_dd;
  49. u32 maxtime_to_auto;
  50. /* No dump disk path string used */
  51. struct dump_section cpu_data;
  52. struct dump_section hpte_data;
  53. struct dump_section kernel_data;
  54. };
  55. /* The dump header *must be* in low memory, so .bss it */
  56. static struct phyp_dump_header phdr;
  57. #define NUM_DUMP_SECTIONS 3
  58. #define DUMP_HEADER_VERSION 0x1
  59. #define DUMP_REQUEST_FLAG 0x1
  60. #define DUMP_SOURCE_CPU 0x0001
  61. #define DUMP_SOURCE_HPTE 0x0002
  62. #define DUMP_SOURCE_RMO 0x0011
  63. #define DUMP_ERROR_FLAG 0x2000
  64. #define DUMP_TRIGGERED 0x4000
  65. #define DUMP_PERFORMED 0x8000
  66. /**
  67. * init_dump_header() - initialize the header declaring a dump
  68. * Returns: length of dump save area.
  69. *
  70. * When the hypervisor saves crashed state, it needs to put
  71. * it somewhere. The dump header tells the hypervisor where
  72. * the data can be saved.
  73. */
  74. static unsigned long init_dump_header(struct phyp_dump_header *ph)
  75. {
  76. unsigned long addr_offset = 0;
  77. /* Set up the dump header */
  78. ph->version = DUMP_HEADER_VERSION;
  79. ph->num_of_sections = NUM_DUMP_SECTIONS;
  80. ph->status = 0;
  81. ph->first_offset_section =
  82. (u32)offsetof(struct phyp_dump_header, cpu_data);
  83. ph->dump_disk_section = 0;
  84. ph->block_num_dd = 0;
  85. ph->num_of_blocks_dd = 0;
  86. ph->offset_dd = 0;
  87. ph->maxtime_to_auto = 0; /* disabled */
  88. /* The first two sections are mandatory */
  89. ph->cpu_data.dump_flags = DUMP_REQUEST_FLAG;
  90. ph->cpu_data.source_type = DUMP_SOURCE_CPU;
  91. ph->cpu_data.source_address = 0;
  92. ph->cpu_data.source_length = phyp_dump_info->cpu_state_size;
  93. ph->cpu_data.destination_address = addr_offset;
  94. addr_offset += phyp_dump_info->cpu_state_size;
  95. ph->hpte_data.dump_flags = DUMP_REQUEST_FLAG;
  96. ph->hpte_data.source_type = DUMP_SOURCE_HPTE;
  97. ph->hpte_data.source_address = 0;
  98. ph->hpte_data.source_length = phyp_dump_info->hpte_region_size;
  99. ph->hpte_data.destination_address = addr_offset;
  100. addr_offset += phyp_dump_info->hpte_region_size;
  101. /* This section describes the low kernel region */
  102. ph->kernel_data.dump_flags = DUMP_REQUEST_FLAG;
  103. ph->kernel_data.source_type = DUMP_SOURCE_RMO;
  104. ph->kernel_data.source_address = PHYP_DUMP_RMR_START;
  105. ph->kernel_data.source_length = PHYP_DUMP_RMR_END;
  106. ph->kernel_data.destination_address = addr_offset;
  107. addr_offset += ph->kernel_data.source_length;
  108. return addr_offset;
  109. }
  110. static void print_dump_header(const struct phyp_dump_header *ph)
  111. {
  112. #ifdef DEBUG
  113. printk(KERN_INFO "dump header:\n");
  114. /* setup some ph->sections required */
  115. printk(KERN_INFO "version = %d\n", ph->version);
  116. printk(KERN_INFO "Sections = %d\n", ph->num_of_sections);
  117. printk(KERN_INFO "Status = 0x%x\n", ph->status);
  118. /* No ph->disk, so all should be set to 0 */
  119. printk(KERN_INFO "Offset to first section 0x%x\n",
  120. ph->first_offset_section);
  121. printk(KERN_INFO "dump disk sections should be zero\n");
  122. printk(KERN_INFO "dump disk section = %d\n", ph->dump_disk_section);
  123. printk(KERN_INFO "block num = %ld\n", ph->block_num_dd);
  124. printk(KERN_INFO "number of blocks = %ld\n", ph->num_of_blocks_dd);
  125. printk(KERN_INFO "dump disk offset = %d\n", ph->offset_dd);
  126. printk(KERN_INFO "Max auto time= %d\n", ph->maxtime_to_auto);
  127. /*set cpu state and hpte states as well scratch pad area */
  128. printk(KERN_INFO " CPU AREA \n");
  129. printk(KERN_INFO "cpu dump_flags =%d\n", ph->cpu_data.dump_flags);
  130. printk(KERN_INFO "cpu source_type =%d\n", ph->cpu_data.source_type);
  131. printk(KERN_INFO "cpu error_flags =%d\n", ph->cpu_data.error_flags);
  132. printk(KERN_INFO "cpu source_address =%lx\n",
  133. ph->cpu_data.source_address);
  134. printk(KERN_INFO "cpu source_length =%lx\n",
  135. ph->cpu_data.source_length);
  136. printk(KERN_INFO "cpu length_copied =%lx\n",
  137. ph->cpu_data.length_copied);
  138. printk(KERN_INFO " HPTE AREA \n");
  139. printk(KERN_INFO "HPTE dump_flags =%d\n", ph->hpte_data.dump_flags);
  140. printk(KERN_INFO "HPTE source_type =%d\n", ph->hpte_data.source_type);
  141. printk(KERN_INFO "HPTE error_flags =%d\n", ph->hpte_data.error_flags);
  142. printk(KERN_INFO "HPTE source_address =%lx\n",
  143. ph->hpte_data.source_address);
  144. printk(KERN_INFO "HPTE source_length =%lx\n",
  145. ph->hpte_data.source_length);
  146. printk(KERN_INFO "HPTE length_copied =%lx\n",
  147. ph->hpte_data.length_copied);
  148. printk(KERN_INFO " SRSD AREA \n");
  149. printk(KERN_INFO "SRSD dump_flags =%d\n", ph->kernel_data.dump_flags);
  150. printk(KERN_INFO "SRSD source_type =%d\n", ph->kernel_data.source_type);
  151. printk(KERN_INFO "SRSD error_flags =%d\n", ph->kernel_data.error_flags);
  152. printk(KERN_INFO "SRSD source_address =%lx\n",
  153. ph->kernel_data.source_address);
  154. printk(KERN_INFO "SRSD source_length =%lx\n",
  155. ph->kernel_data.source_length);
  156. printk(KERN_INFO "SRSD length_copied =%lx\n",
  157. ph->kernel_data.length_copied);
  158. #endif
  159. }
  160. static void register_dump_area(struct phyp_dump_header *ph, unsigned long addr)
  161. {
  162. int rc;
  163. /* Add addr value if not initialized before */
  164. if (ph->cpu_data.destination_address == 0) {
  165. ph->cpu_data.destination_address += addr;
  166. ph->hpte_data.destination_address += addr;
  167. ph->kernel_data.destination_address += addr;
  168. }
  169. /* ToDo Invalidate kdump and free memory range. */
  170. do {
  171. rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
  172. 1, ph, sizeof(struct phyp_dump_header));
  173. } while (rtas_busy_delay(rc));
  174. if (rc) {
  175. printk(KERN_ERR "phyp-dump: unexpected error (%d) on "
  176. "register\n", rc);
  177. print_dump_header(ph);
  178. }
  179. }
  180. static
  181. void invalidate_last_dump(struct phyp_dump_header *ph, unsigned long addr)
  182. {
  183. int rc;
  184. /* Add addr value if not initialized before */
  185. if (ph->cpu_data.destination_address == 0) {
  186. ph->cpu_data.destination_address += addr;
  187. ph->hpte_data.destination_address += addr;
  188. ph->kernel_data.destination_address += addr;
  189. }
  190. do {
  191. rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
  192. 2, ph, sizeof(struct phyp_dump_header));
  193. } while (rtas_busy_delay(rc));
  194. if (rc) {
  195. printk(KERN_ERR "phyp-dump: unexpected error (%d) "
  196. "on invalidate\n", rc);
  197. print_dump_header(ph);
  198. }
  199. }
  200. /* ------------------------------------------------- */
  201. /**
  202. * release_memory_range -- release memory previously lmb_reserved
  203. * @start_pfn: starting physical frame number
  204. * @nr_pages: number of pages to free.
  205. *
  206. * This routine will release memory that had been previously
  207. * lmb_reserved in early boot. The released memory becomes
  208. * available for genreal use.
  209. */
  210. static void release_memory_range(unsigned long start_pfn,
  211. unsigned long nr_pages)
  212. {
  213. struct page *rpage;
  214. unsigned long end_pfn;
  215. long i;
  216. end_pfn = start_pfn + nr_pages;
  217. for (i = start_pfn; i <= end_pfn; i++) {
  218. rpage = pfn_to_page(i);
  219. if (PageReserved(rpage)) {
  220. ClearPageReserved(rpage);
  221. init_page_count(rpage);
  222. __free_page(rpage);
  223. totalram_pages++;
  224. }
  225. }
  226. }
  227. /* ------------------------------------------------- */
  228. /**
  229. * sysfs_release_region -- sysfs interface to release memory range.
  230. *
  231. * Usage:
  232. * "echo <start addr> <length> > /sys/kernel/release_region"
  233. *
  234. * Example:
  235. * "echo 0x40000000 0x10000000 > /sys/kernel/release_region"
  236. *
  237. * will release 256MB starting at 1GB.
  238. */
  239. static ssize_t store_release_region(struct kobject *kobj,
  240. struct kobj_attribute *attr,
  241. const char *buf, size_t count)
  242. {
  243. unsigned long start_addr, length, end_addr;
  244. unsigned long start_pfn, nr_pages;
  245. ssize_t ret;
  246. ret = sscanf(buf, "%lx %lx", &start_addr, &length);
  247. if (ret != 2)
  248. return -EINVAL;
  249. /* Range-check - don't free any reserved memory that
  250. * wasn't reserved for phyp-dump */
  251. if (start_addr < phyp_dump_info->init_reserve_start)
  252. start_addr = phyp_dump_info->init_reserve_start;
  253. end_addr = phyp_dump_info->init_reserve_start +
  254. phyp_dump_info->init_reserve_size;
  255. if (start_addr+length > end_addr)
  256. length = end_addr - start_addr;
  257. /* Release the region of memory assed in by user */
  258. start_pfn = PFN_DOWN(start_addr);
  259. nr_pages = PFN_DOWN(length);
  260. release_memory_range(start_pfn, nr_pages);
  261. return count;
  262. }
  263. static ssize_t show_release_region(struct kobject *kobj,
  264. struct kobj_attribute *attr, char *buf)
  265. {
  266. u64 second_addr_range;
  267. /* total reserved size - start of scratch area */
  268. second_addr_range = phyp_dump_info->init_reserve_size -
  269. phyp_dump_info->reserved_scratch_size;
  270. return sprintf(buf, "CPU:0x%lx-0x%lx: HPTE:0x%lx-0x%lx:"
  271. " DUMP:0x%lx-0x%lx, 0x%lx-0x%lx:\n",
  272. phdr.cpu_data.destination_address,
  273. phdr.cpu_data.length_copied,
  274. phdr.hpte_data.destination_address,
  275. phdr.hpte_data.length_copied,
  276. phdr.kernel_data.destination_address,
  277. phdr.kernel_data.length_copied,
  278. phyp_dump_info->init_reserve_start,
  279. second_addr_range);
  280. }
  281. static struct kobj_attribute rr = __ATTR(release_region, 0600,
  282. show_release_region,
  283. store_release_region);
  284. static int __init phyp_dump_setup(void)
  285. {
  286. struct device_node *rtas;
  287. const struct phyp_dump_header *dump_header = NULL;
  288. unsigned long dump_area_start;
  289. unsigned long dump_area_length;
  290. int header_len = 0;
  291. int rc;
  292. /* If no memory was reserved in early boot, there is nothing to do */
  293. if (phyp_dump_info->init_reserve_size == 0)
  294. return 0;
  295. /* Return if phyp dump not supported */
  296. if (!phyp_dump_info->phyp_dump_configured)
  297. return -ENOSYS;
  298. /* Is there dump data waiting for us? If there isn't,
  299. * then register a new dump area, and release all of
  300. * the rest of the reserved ram.
  301. *
  302. * The /rtas/ibm,kernel-dump rtas node is present only
  303. * if there is dump data waiting for us.
  304. */
  305. rtas = of_find_node_by_path("/rtas");
  306. if (rtas) {
  307. dump_header = of_get_property(rtas, "ibm,kernel-dump",
  308. &header_len);
  309. of_node_put(rtas);
  310. }
  311. print_dump_header(dump_header);
  312. dump_area_length = init_dump_header(&phdr);
  313. /* align down */
  314. dump_area_start = phyp_dump_info->init_reserve_start & PAGE_MASK;
  315. if (dump_header == NULL) {
  316. register_dump_area(&phdr, dump_area_start);
  317. return 0;
  318. }
  319. /* re-register the dump area, if old dump was invalid */
  320. if ((dump_header) && (dump_header->status & DUMP_ERROR_FLAG)) {
  321. invalidate_last_dump(&phdr, dump_area_start);
  322. register_dump_area(&phdr, dump_area_start);
  323. return 0;
  324. }
  325. if (dump_header) {
  326. phyp_dump_info->reserved_scratch_addr =
  327. dump_header->cpu_data.destination_address;
  328. phyp_dump_info->reserved_scratch_size =
  329. dump_header->cpu_data.source_length +
  330. dump_header->hpte_data.source_length +
  331. dump_header->kernel_data.source_length;
  332. }
  333. /* Should we create a dump_subsys, analogous to s390/ipl.c ? */
  334. rc = sysfs_create_file(kernel_kobj, &rr.attr);
  335. if (rc)
  336. printk(KERN_ERR "phyp-dump: unable to create sysfs file (%d)\n",
  337. rc);
  338. /* ToDo: re-register the dump area, for next time. */
  339. return 0;
  340. }
  341. machine_subsys_initcall(pseries, phyp_dump_setup);
  342. int __init early_init_dt_scan_phyp_dump(unsigned long node,
  343. const char *uname, int depth, void *data)
  344. {
  345. const unsigned int *sizes;
  346. phyp_dump_info->phyp_dump_configured = 0;
  347. phyp_dump_info->phyp_dump_is_active = 0;
  348. if (depth != 1 || strcmp(uname, "rtas") != 0)
  349. return 0;
  350. if (of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL))
  351. phyp_dump_info->phyp_dump_configured++;
  352. if (of_get_flat_dt_prop(node, "ibm,dump-kernel", NULL))
  353. phyp_dump_info->phyp_dump_is_active++;
  354. sizes = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
  355. NULL);
  356. if (!sizes)
  357. return 0;
  358. if (sizes[0] == 1)
  359. phyp_dump_info->cpu_state_size = *((unsigned long *)&sizes[1]);
  360. if (sizes[3] == 2)
  361. phyp_dump_info->hpte_region_size =
  362. *((unsigned long *)&sizes[4]);
  363. return 1;
  364. }