phyp_dump.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 ssize_t show_phyp_dump_active(struct kobject *kobj,
  161. struct kobj_attribute *attr, char *buf)
  162. {
  163. /* create filesystem entry so kdump is phyp-dump aware */
  164. return sprintf(buf, "%lx\n", phyp_dump_info->phyp_dump_at_boot);
  165. }
  166. static struct kobj_attribute pdl = __ATTR(phyp_dump_active, 0600,
  167. show_phyp_dump_active,
  168. NULL);
  169. static void register_dump_area(struct phyp_dump_header *ph, unsigned long addr)
  170. {
  171. int rc;
  172. /* Add addr value if not initialized before */
  173. if (ph->cpu_data.destination_address == 0) {
  174. ph->cpu_data.destination_address += addr;
  175. ph->hpte_data.destination_address += addr;
  176. ph->kernel_data.destination_address += addr;
  177. }
  178. /* ToDo Invalidate kdump and free memory range. */
  179. do {
  180. rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
  181. 1, ph, sizeof(struct phyp_dump_header));
  182. } while (rtas_busy_delay(rc));
  183. if (rc) {
  184. printk(KERN_ERR "phyp-dump: unexpected error (%d) on "
  185. "register\n", rc);
  186. print_dump_header(ph);
  187. return;
  188. }
  189. rc = sysfs_create_file(kernel_kobj, &pdl.attr);
  190. if (rc)
  191. printk(KERN_ERR "phyp-dump: unable to create sysfs"
  192. " file (%d)\n", rc);
  193. }
  194. static
  195. void invalidate_last_dump(struct phyp_dump_header *ph, unsigned long addr)
  196. {
  197. int rc;
  198. /* Add addr value if not initialized before */
  199. if (ph->cpu_data.destination_address == 0) {
  200. ph->cpu_data.destination_address += addr;
  201. ph->hpte_data.destination_address += addr;
  202. ph->kernel_data.destination_address += addr;
  203. }
  204. do {
  205. rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
  206. 2, ph, sizeof(struct phyp_dump_header));
  207. } while (rtas_busy_delay(rc));
  208. if (rc) {
  209. printk(KERN_ERR "phyp-dump: unexpected error (%d) "
  210. "on invalidate\n", rc);
  211. print_dump_header(ph);
  212. }
  213. }
  214. /* ------------------------------------------------- */
  215. /**
  216. * release_memory_range -- release memory previously lmb_reserved
  217. * @start_pfn: starting physical frame number
  218. * @nr_pages: number of pages to free.
  219. *
  220. * This routine will release memory that had been previously
  221. * lmb_reserved in early boot. The released memory becomes
  222. * available for genreal use.
  223. */
  224. static void release_memory_range(unsigned long start_pfn,
  225. unsigned long nr_pages)
  226. {
  227. struct page *rpage;
  228. unsigned long end_pfn;
  229. long i;
  230. end_pfn = start_pfn + nr_pages;
  231. for (i = start_pfn; i <= end_pfn; i++) {
  232. rpage = pfn_to_page(i);
  233. if (PageReserved(rpage)) {
  234. ClearPageReserved(rpage);
  235. init_page_count(rpage);
  236. __free_page(rpage);
  237. totalram_pages++;
  238. }
  239. }
  240. }
  241. /**
  242. * track_freed_range -- Counts the range being freed.
  243. * Once the counter goes to zero, it re-registers dump for
  244. * future use.
  245. */
  246. static void
  247. track_freed_range(unsigned long addr, unsigned long length)
  248. {
  249. static unsigned long scratch_area_size, reserved_area_size;
  250. if (addr < phyp_dump_info->init_reserve_start)
  251. return;
  252. if ((addr >= phyp_dump_info->init_reserve_start) &&
  253. (addr <= phyp_dump_info->init_reserve_start +
  254. phyp_dump_info->init_reserve_size))
  255. reserved_area_size += length;
  256. if ((addr >= phyp_dump_info->reserved_scratch_addr) &&
  257. (addr <= phyp_dump_info->reserved_scratch_addr +
  258. phyp_dump_info->reserved_scratch_size))
  259. scratch_area_size += length;
  260. if ((reserved_area_size == phyp_dump_info->init_reserve_size) &&
  261. (scratch_area_size == phyp_dump_info->reserved_scratch_size)) {
  262. invalidate_last_dump(&phdr,
  263. phyp_dump_info->reserved_scratch_addr);
  264. register_dump_area(&phdr,
  265. phyp_dump_info->reserved_scratch_addr);
  266. }
  267. }
  268. /* ------------------------------------------------- */
  269. /**
  270. * sysfs_release_region -- sysfs interface to release memory range.
  271. *
  272. * Usage:
  273. * "echo <start addr> <length> > /sys/kernel/release_region"
  274. *
  275. * Example:
  276. * "echo 0x40000000 0x10000000 > /sys/kernel/release_region"
  277. *
  278. * will release 256MB starting at 1GB.
  279. */
  280. static ssize_t store_release_region(struct kobject *kobj,
  281. struct kobj_attribute *attr,
  282. const char *buf, size_t count)
  283. {
  284. unsigned long start_addr, length, end_addr;
  285. unsigned long start_pfn, nr_pages;
  286. ssize_t ret;
  287. ret = sscanf(buf, "%lx %lx", &start_addr, &length);
  288. if (ret != 2)
  289. return -EINVAL;
  290. track_freed_range(start_addr, length);
  291. /* Range-check - don't free any reserved memory that
  292. * wasn't reserved for phyp-dump */
  293. if (start_addr < phyp_dump_info->init_reserve_start)
  294. start_addr = phyp_dump_info->init_reserve_start;
  295. end_addr = phyp_dump_info->init_reserve_start +
  296. phyp_dump_info->init_reserve_size;
  297. if (start_addr+length > end_addr)
  298. length = end_addr - start_addr;
  299. /* Release the region of memory assed in by user */
  300. start_pfn = PFN_DOWN(start_addr);
  301. nr_pages = PFN_DOWN(length);
  302. release_memory_range(start_pfn, nr_pages);
  303. return count;
  304. }
  305. static ssize_t show_release_region(struct kobject *kobj,
  306. struct kobj_attribute *attr, char *buf)
  307. {
  308. u64 second_addr_range;
  309. /* total reserved size - start of scratch area */
  310. second_addr_range = phyp_dump_info->init_reserve_size -
  311. phyp_dump_info->reserved_scratch_size;
  312. return sprintf(buf, "CPU:0x%lx-0x%lx: HPTE:0x%lx-0x%lx:"
  313. " DUMP:0x%lx-0x%lx, 0x%lx-0x%lx:\n",
  314. phdr.cpu_data.destination_address,
  315. phdr.cpu_data.length_copied,
  316. phdr.hpte_data.destination_address,
  317. phdr.hpte_data.length_copied,
  318. phdr.kernel_data.destination_address,
  319. phdr.kernel_data.length_copied,
  320. phyp_dump_info->init_reserve_start,
  321. second_addr_range);
  322. }
  323. static struct kobj_attribute rr = __ATTR(release_region, 0600,
  324. show_release_region,
  325. store_release_region);
  326. static int __init phyp_dump_setup(void)
  327. {
  328. struct device_node *rtas;
  329. const struct phyp_dump_header *dump_header = NULL;
  330. unsigned long dump_area_start;
  331. unsigned long dump_area_length;
  332. int header_len = 0;
  333. int rc;
  334. /* If no memory was reserved in early boot, there is nothing to do */
  335. if (phyp_dump_info->init_reserve_size == 0)
  336. return 0;
  337. /* Return if phyp dump not supported */
  338. if (!phyp_dump_info->phyp_dump_configured)
  339. return -ENOSYS;
  340. /* Is there dump data waiting for us? If there isn't,
  341. * then register a new dump area, and release all of
  342. * the rest of the reserved ram.
  343. *
  344. * The /rtas/ibm,kernel-dump rtas node is present only
  345. * if there is dump data waiting for us.
  346. */
  347. rtas = of_find_node_by_path("/rtas");
  348. if (rtas) {
  349. dump_header = of_get_property(rtas, "ibm,kernel-dump",
  350. &header_len);
  351. of_node_put(rtas);
  352. }
  353. print_dump_header(dump_header);
  354. dump_area_length = init_dump_header(&phdr);
  355. /* align down */
  356. dump_area_start = phyp_dump_info->init_reserve_start & PAGE_MASK;
  357. if (dump_header == NULL) {
  358. register_dump_area(&phdr, dump_area_start);
  359. return 0;
  360. }
  361. /* re-register the dump area, if old dump was invalid */
  362. if ((dump_header) && (dump_header->status & DUMP_ERROR_FLAG)) {
  363. invalidate_last_dump(&phdr, dump_area_start);
  364. register_dump_area(&phdr, dump_area_start);
  365. return 0;
  366. }
  367. if (dump_header) {
  368. phyp_dump_info->reserved_scratch_addr =
  369. dump_header->cpu_data.destination_address;
  370. phyp_dump_info->reserved_scratch_size =
  371. dump_header->cpu_data.source_length +
  372. dump_header->hpte_data.source_length +
  373. dump_header->kernel_data.source_length;
  374. }
  375. /* Should we create a dump_subsys, analogous to s390/ipl.c ? */
  376. rc = sysfs_create_file(kernel_kobj, &rr.attr);
  377. if (rc)
  378. printk(KERN_ERR "phyp-dump: unable to create sysfs file (%d)\n",
  379. rc);
  380. /* ToDo: re-register the dump area, for next time. */
  381. return 0;
  382. }
  383. machine_subsys_initcall(pseries, phyp_dump_setup);
  384. int __init early_init_dt_scan_phyp_dump(unsigned long node,
  385. const char *uname, int depth, void *data)
  386. {
  387. const unsigned int *sizes;
  388. phyp_dump_info->phyp_dump_configured = 0;
  389. phyp_dump_info->phyp_dump_is_active = 0;
  390. if (depth != 1 || strcmp(uname, "rtas") != 0)
  391. return 0;
  392. if (of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL))
  393. phyp_dump_info->phyp_dump_configured++;
  394. if (of_get_flat_dt_prop(node, "ibm,dump-kernel", NULL))
  395. phyp_dump_info->phyp_dump_is_active++;
  396. sizes = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
  397. NULL);
  398. if (!sizes)
  399. return 0;
  400. if (sizes[0] == 1)
  401. phyp_dump_info->cpu_state_size = *((unsigned long *)&sizes[1]);
  402. if (sizes[3] == 2)
  403. phyp_dump_info->hpte_region_size =
  404. *((unsigned long *)&sizes[4]);
  405. return 1;
  406. }
  407. /* Look for phyp_dump= cmdline option */
  408. static int __init early_phyp_dump_enabled(char *p)
  409. {
  410. phyp_dump_info->phyp_dump_at_boot = 1;
  411. if (!p)
  412. return 0;
  413. if (strncmp(p, "1", 1) == 0)
  414. phyp_dump_info->phyp_dump_at_boot = 1;
  415. else if (strncmp(p, "0", 1) == 0)
  416. phyp_dump_info->phyp_dump_at_boot = 0;
  417. return 0;
  418. }
  419. early_param("phyp_dump", early_phyp_dump_enabled);
  420. /* Look for phyp_dump_reserve_size= cmdline option */
  421. static int __init early_phyp_dump_reserve_size(char *p)
  422. {
  423. if (p)
  424. phyp_dump_info->reserve_bootvar = memparse(p, &p);
  425. return 0;
  426. }
  427. early_param("phyp_dump_reserve_size", early_phyp_dump_reserve_size);