phyp_dump.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. /**
  64. * init_dump_header() - initialize the header declaring a dump
  65. * Returns: length of dump save area.
  66. *
  67. * When the hypervisor saves crashed state, it needs to put
  68. * it somewhere. The dump header tells the hypervisor where
  69. * the data can be saved.
  70. */
  71. static unsigned long init_dump_header(struct phyp_dump_header *ph)
  72. {
  73. unsigned long addr_offset = 0;
  74. /* Set up the dump header */
  75. ph->version = DUMP_HEADER_VERSION;
  76. ph->num_of_sections = NUM_DUMP_SECTIONS;
  77. ph->status = 0;
  78. ph->first_offset_section =
  79. (u32)offsetof(struct phyp_dump_header, cpu_data);
  80. ph->dump_disk_section = 0;
  81. ph->block_num_dd = 0;
  82. ph->num_of_blocks_dd = 0;
  83. ph->offset_dd = 0;
  84. ph->maxtime_to_auto = 0; /* disabled */
  85. /* The first two sections are mandatory */
  86. ph->cpu_data.dump_flags = DUMP_REQUEST_FLAG;
  87. ph->cpu_data.source_type = DUMP_SOURCE_CPU;
  88. ph->cpu_data.source_address = 0;
  89. ph->cpu_data.source_length = phyp_dump_info->cpu_state_size;
  90. ph->cpu_data.destination_address = addr_offset;
  91. addr_offset += phyp_dump_info->cpu_state_size;
  92. ph->hpte_data.dump_flags = DUMP_REQUEST_FLAG;
  93. ph->hpte_data.source_type = DUMP_SOURCE_HPTE;
  94. ph->hpte_data.source_address = 0;
  95. ph->hpte_data.source_length = phyp_dump_info->hpte_region_size;
  96. ph->hpte_data.destination_address = addr_offset;
  97. addr_offset += phyp_dump_info->hpte_region_size;
  98. /* This section describes the low kernel region */
  99. ph->kernel_data.dump_flags = DUMP_REQUEST_FLAG;
  100. ph->kernel_data.source_type = DUMP_SOURCE_RMO;
  101. ph->kernel_data.source_address = PHYP_DUMP_RMR_START;
  102. ph->kernel_data.source_length = PHYP_DUMP_RMR_END;
  103. ph->kernel_data.destination_address = addr_offset;
  104. addr_offset += ph->kernel_data.source_length;
  105. return addr_offset;
  106. }
  107. static void print_dump_header(const struct phyp_dump_header *ph)
  108. {
  109. #ifdef DEBUG
  110. printk(KERN_INFO "dump header:\n");
  111. /* setup some ph->sections required */
  112. printk(KERN_INFO "version = %d\n", ph->version);
  113. printk(KERN_INFO "Sections = %d\n", ph->num_of_sections);
  114. printk(KERN_INFO "Status = 0x%x\n", ph->status);
  115. /* No ph->disk, so all should be set to 0 */
  116. printk(KERN_INFO "Offset to first section 0x%x\n",
  117. ph->first_offset_section);
  118. printk(KERN_INFO "dump disk sections should be zero\n");
  119. printk(KERN_INFO "dump disk section = %d\n", ph->dump_disk_section);
  120. printk(KERN_INFO "block num = %ld\n", ph->block_num_dd);
  121. printk(KERN_INFO "number of blocks = %ld\n", ph->num_of_blocks_dd);
  122. printk(KERN_INFO "dump disk offset = %d\n", ph->offset_dd);
  123. printk(KERN_INFO "Max auto time= %d\n", ph->maxtime_to_auto);
  124. /*set cpu state and hpte states as well scratch pad area */
  125. printk(KERN_INFO " CPU AREA \n");
  126. printk(KERN_INFO "cpu dump_flags =%d\n", ph->cpu_data.dump_flags);
  127. printk(KERN_INFO "cpu source_type =%d\n", ph->cpu_data.source_type);
  128. printk(KERN_INFO "cpu error_flags =%d\n", ph->cpu_data.error_flags);
  129. printk(KERN_INFO "cpu source_address =%lx\n",
  130. ph->cpu_data.source_address);
  131. printk(KERN_INFO "cpu source_length =%lx\n",
  132. ph->cpu_data.source_length);
  133. printk(KERN_INFO "cpu length_copied =%lx\n",
  134. ph->cpu_data.length_copied);
  135. printk(KERN_INFO " HPTE AREA \n");
  136. printk(KERN_INFO "HPTE dump_flags =%d\n", ph->hpte_data.dump_flags);
  137. printk(KERN_INFO "HPTE source_type =%d\n", ph->hpte_data.source_type);
  138. printk(KERN_INFO "HPTE error_flags =%d\n", ph->hpte_data.error_flags);
  139. printk(KERN_INFO "HPTE source_address =%lx\n",
  140. ph->hpte_data.source_address);
  141. printk(KERN_INFO "HPTE source_length =%lx\n",
  142. ph->hpte_data.source_length);
  143. printk(KERN_INFO "HPTE length_copied =%lx\n",
  144. ph->hpte_data.length_copied);
  145. printk(KERN_INFO " SRSD AREA \n");
  146. printk(KERN_INFO "SRSD dump_flags =%d\n", ph->kernel_data.dump_flags);
  147. printk(KERN_INFO "SRSD source_type =%d\n", ph->kernel_data.source_type);
  148. printk(KERN_INFO "SRSD error_flags =%d\n", ph->kernel_data.error_flags);
  149. printk(KERN_INFO "SRSD source_address =%lx\n",
  150. ph->kernel_data.source_address);
  151. printk(KERN_INFO "SRSD source_length =%lx\n",
  152. ph->kernel_data.source_length);
  153. printk(KERN_INFO "SRSD length_copied =%lx\n",
  154. ph->kernel_data.length_copied);
  155. #endif
  156. }
  157. static void register_dump_area(struct phyp_dump_header *ph, unsigned long addr)
  158. {
  159. int rc;
  160. ph->cpu_data.destination_address += addr;
  161. ph->hpte_data.destination_address += addr;
  162. ph->kernel_data.destination_address += addr;
  163. do {
  164. rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
  165. 1, ph, sizeof(struct phyp_dump_header));
  166. } while (rtas_busy_delay(rc));
  167. if (rc) {
  168. printk(KERN_ERR "phyp-dump: unexpected error (%d) on "
  169. "register\n", rc);
  170. print_dump_header(ph);
  171. }
  172. }
  173. /* ------------------------------------------------- */
  174. /**
  175. * release_memory_range -- release memory previously lmb_reserved
  176. * @start_pfn: starting physical frame number
  177. * @nr_pages: number of pages to free.
  178. *
  179. * This routine will release memory that had been previously
  180. * lmb_reserved in early boot. The released memory becomes
  181. * available for genreal use.
  182. */
  183. static void
  184. release_memory_range(unsigned long start_pfn, unsigned long nr_pages)
  185. {
  186. struct page *rpage;
  187. unsigned long end_pfn;
  188. long i;
  189. end_pfn = start_pfn + nr_pages;
  190. for (i = start_pfn; i <= end_pfn; i++) {
  191. rpage = pfn_to_page(i);
  192. if (PageReserved(rpage)) {
  193. ClearPageReserved(rpage);
  194. init_page_count(rpage);
  195. __free_page(rpage);
  196. totalram_pages++;
  197. }
  198. }
  199. }
  200. /* ------------------------------------------------- */
  201. /**
  202. * sysfs_release_region -- sysfs interface to release memory range.
  203. *
  204. * Usage:
  205. * "echo <start addr> <length> > /sys/kernel/release_region"
  206. *
  207. * Example:
  208. * "echo 0x40000000 0x10000000 > /sys/kernel/release_region"
  209. *
  210. * will release 256MB starting at 1GB.
  211. */
  212. static ssize_t store_release_region(struct kobject *kobj,
  213. struct kobj_attribute *attr,
  214. const char *buf, size_t count)
  215. {
  216. unsigned long start_addr, length, end_addr;
  217. unsigned long start_pfn, nr_pages;
  218. ssize_t ret;
  219. ret = sscanf(buf, "%lx %lx", &start_addr, &length);
  220. if (ret != 2)
  221. return -EINVAL;
  222. /* Range-check - don't free any reserved memory that
  223. * wasn't reserved for phyp-dump */
  224. if (start_addr < phyp_dump_info->init_reserve_start)
  225. start_addr = phyp_dump_info->init_reserve_start;
  226. end_addr = phyp_dump_info->init_reserve_start +
  227. phyp_dump_info->init_reserve_size;
  228. if (start_addr+length > end_addr)
  229. length = end_addr - start_addr;
  230. /* Release the region of memory assed in by user */
  231. start_pfn = PFN_DOWN(start_addr);
  232. nr_pages = PFN_DOWN(length);
  233. release_memory_range(start_pfn, nr_pages);
  234. return count;
  235. }
  236. static struct kobj_attribute rr = __ATTR(release_region, 0600,
  237. NULL, store_release_region);
  238. static int __init phyp_dump_setup(void)
  239. {
  240. struct device_node *rtas;
  241. const struct phyp_dump_header *dump_header = NULL;
  242. unsigned long dump_area_start;
  243. unsigned long dump_area_length;
  244. int header_len = 0;
  245. int rc;
  246. /* If no memory was reserved in early boot, there is nothing to do */
  247. if (phyp_dump_info->init_reserve_size == 0)
  248. return 0;
  249. /* Return if phyp dump not supported */
  250. if (!phyp_dump_info->phyp_dump_configured)
  251. return -ENOSYS;
  252. /* Is there dump data waiting for us? If there isn't,
  253. * then register a new dump area, and release all of
  254. * the rest of the reserved ram.
  255. *
  256. * The /rtas/ibm,kernel-dump rtas node is present only
  257. * if there is dump data waiting for us.
  258. */
  259. rtas = of_find_node_by_path("/rtas");
  260. if (rtas) {
  261. dump_header = of_get_property(rtas, "ibm,kernel-dump",
  262. &header_len);
  263. of_node_put(rtas);
  264. }
  265. print_dump_header(dump_header);
  266. dump_area_length = init_dump_header(&phdr);
  267. /* align down */
  268. dump_area_start = phyp_dump_info->init_reserve_start & PAGE_MASK;
  269. if (dump_header == NULL) {
  270. register_dump_area(&phdr, dump_area_start);
  271. return 0;
  272. }
  273. /* Should we create a dump_subsys, analogous to s390/ipl.c ? */
  274. rc = sysfs_create_file(kernel_kobj, &rr.attr);
  275. if (rc)
  276. printk(KERN_ERR "phyp-dump: unable to create sysfs file (%d)\n",
  277. rc);
  278. /* ToDo: re-register the dump area, for next time. */
  279. return 0;
  280. }
  281. machine_subsys_initcall(pseries, phyp_dump_setup);
  282. int __init early_init_dt_scan_phyp_dump(unsigned long node,
  283. const char *uname, int depth, void *data)
  284. {
  285. const unsigned int *sizes;
  286. phyp_dump_info->phyp_dump_configured = 0;
  287. phyp_dump_info->phyp_dump_is_active = 0;
  288. if (depth != 1 || strcmp(uname, "rtas") != 0)
  289. return 0;
  290. if (of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL))
  291. phyp_dump_info->phyp_dump_configured++;
  292. if (of_get_flat_dt_prop(node, "ibm,dump-kernel", NULL))
  293. phyp_dump_info->phyp_dump_is_active++;
  294. sizes = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
  295. NULL);
  296. if (!sizes)
  297. return 0;
  298. if (sizes[0] == 1)
  299. phyp_dump_info->cpu_state_size = *((unsigned long *)&sizes[1]);
  300. if (sizes[3] == 2)
  301. phyp_dump_info->hpte_region_size =
  302. *((unsigned long *)&sizes[4]);
  303. return 1;
  304. }