phyp_dump.c 15 KB

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