xref: /linux/drivers/gpu/drm/etnaviv/etnaviv_dump.c (revision a3ecd97aa6aa444cc6c1b4c5b45d8d25068f19f6)
1f6ffbd4fSLucas Stach // SPDX-License-Identifier: GPL-2.0
2a8c21a54SThe etnaviv authors /*
3f6ffbd4fSLucas Stach  * Copyright (C) 2015-2018 Etnaviv Project
4a8c21a54SThe etnaviv authors  */
5a8c21a54SThe etnaviv authors 
6a8c21a54SThe etnaviv authors #include <linux/devcoredump.h>
72e737e52SLucas Stach #include <linux/moduleparam.h>
82e737e52SLucas Stach 
9ea1f5729SLucas Stach #include "etnaviv_cmdbuf.h"
10a8c21a54SThe etnaviv authors #include "etnaviv_dump.h"
11a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
12a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
13a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
146d7a20c0SLucas Stach #include "etnaviv_sched.h"
15a8c21a54SThe etnaviv authors #include "state.xml.h"
16a8c21a54SThe etnaviv authors #include "state_hi.xml.h"
17a8c21a54SThe etnaviv authors 
186d7a20c0SLucas Stach static bool etnaviv_dump_core = true;
196d7a20c0SLucas Stach module_param_named(dump_core, etnaviv_dump_core, bool, 0600);
206d7a20c0SLucas Stach 
21a8c21a54SThe etnaviv authors struct core_dump_iterator {
22a8c21a54SThe etnaviv authors 	void *start;
23a8c21a54SThe etnaviv authors 	struct etnaviv_dump_object_header *hdr;
24a8c21a54SThe etnaviv authors 	void *data;
25a8c21a54SThe etnaviv authors };
26a8c21a54SThe etnaviv authors 
27a8c21a54SThe etnaviv authors static const unsigned short etnaviv_dump_registers[] = {
28a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_STATUS,
29a8c21a54SThe etnaviv authors 	VIVS_HI_CLOCK_CONTROL,
30a8c21a54SThe etnaviv authors 	VIVS_HI_IDLE_STATE,
31a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_CONFIG,
32a8c21a54SThe etnaviv authors 	VIVS_HI_INTR_ENBL,
33a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_IDENTITY,
34a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_FEATURE,
35a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_MODEL,
36a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_REV,
37a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_DATE,
38a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_TIME,
39a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_MINOR_FEATURE_0,
40a8c21a54SThe etnaviv authors 	VIVS_HI_CACHE_CONTROL,
41a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_CONTROL,
42a8c21a54SThe etnaviv authors 	VIVS_PM_POWER_CONTROLS,
43a8c21a54SThe etnaviv authors 	VIVS_PM_MODULE_CONTROLS,
44a8c21a54SThe etnaviv authors 	VIVS_PM_MODULE_STATUS,
45a8c21a54SThe etnaviv authors 	VIVS_PM_PULSE_EATER,
46a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_FE_PAGE_TABLE,
47a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_TX_PAGE_TABLE,
48a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_PE_PAGE_TABLE,
49a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_PEZ_PAGE_TABLE,
50a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_RA_PAGE_TABLE,
51a8c21a54SThe etnaviv authors 	VIVS_MC_DEBUG_MEMORY,
52a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_RA,
53a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_FE,
54a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_TX,
55a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_PEZ,
56a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_PE,
57a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_TIMING_CONTROL,
58a8c21a54SThe etnaviv authors 	VIVS_MC_BUS_CONFIG,
59a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_STATUS,
60a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_DEBUG_STATE,
61a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_ADDRESS,
62a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_LOW,
63a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_HIGH,
64a8c21a54SThe etnaviv authors 	VIVS_FE_AUTO_FLUSH,
65a8c21a54SThe etnaviv authors };
66a8c21a54SThe etnaviv authors 
67a8c21a54SThe etnaviv authors static void etnaviv_core_dump_header(struct core_dump_iterator *iter,
68a8c21a54SThe etnaviv authors 	u32 type, void *data_end)
69a8c21a54SThe etnaviv authors {
70a8c21a54SThe etnaviv authors 	struct etnaviv_dump_object_header *hdr = iter->hdr;
71a8c21a54SThe etnaviv authors 
72a8c21a54SThe etnaviv authors 	hdr->magic = cpu_to_le32(ETDUMP_MAGIC);
73a8c21a54SThe etnaviv authors 	hdr->type = cpu_to_le32(type);
74a8c21a54SThe etnaviv authors 	hdr->file_offset = cpu_to_le32(iter->data - iter->start);
75a8c21a54SThe etnaviv authors 	hdr->file_size = cpu_to_le32(data_end - iter->data);
76a8c21a54SThe etnaviv authors 
77a8c21a54SThe etnaviv authors 	iter->hdr++;
7803a27539SMarc Kleine-Budde 	iter->data += le32_to_cpu(hdr->file_size);
79a8c21a54SThe etnaviv authors }
80a8c21a54SThe etnaviv authors 
81a8c21a54SThe etnaviv authors static void etnaviv_core_dump_registers(struct core_dump_iterator *iter,
82a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu)
83a8c21a54SThe etnaviv authors {
84a8c21a54SThe etnaviv authors 	struct etnaviv_dump_registers *reg = iter->data;
85a8c21a54SThe etnaviv authors 	unsigned int i;
8661a6920bSDoug Brown 	u32 read_addr;
87a8c21a54SThe etnaviv authors 
88a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(etnaviv_dump_registers); i++, reg++) {
8961a6920bSDoug Brown 		read_addr = etnaviv_dump_registers[i];
9061a6920bSDoug Brown 		if (read_addr >= VIVS_PM_POWER_CONTROLS &&
9161a6920bSDoug Brown 		    read_addr <= VIVS_PM_PULSE_EATER)
9261a6920bSDoug Brown 			read_addr = gpu_fix_power_address(gpu, read_addr);
9303a27539SMarc Kleine-Budde 		reg->reg = cpu_to_le32(etnaviv_dump_registers[i]);
9461a6920bSDoug Brown 		reg->value = cpu_to_le32(gpu_read(gpu, read_addr));
95a8c21a54SThe etnaviv authors 	}
96a8c21a54SThe etnaviv authors 
97a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, ETDUMP_BUF_REG, reg);
98a8c21a54SThe etnaviv authors }
99a8c21a54SThe etnaviv authors 
100a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mmu(struct core_dump_iterator *iter,
10127b67278SLucas Stach 	struct etnaviv_iommu_context *mmu, size_t mmu_size)
102a8c21a54SThe etnaviv authors {
1033001eeb7SLucas Stach 	etnaviv_iommu_dump(mmu, iter->data);
104a8c21a54SThe etnaviv authors 
105a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, ETDUMP_BUF_MMU, iter->data + mmu_size);
106a8c21a54SThe etnaviv authors }
107a8c21a54SThe etnaviv authors 
108a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mem(struct core_dump_iterator *iter, u32 type,
109a8c21a54SThe etnaviv authors 	void *ptr, size_t size, u64 iova)
110a8c21a54SThe etnaviv authors {
111a8c21a54SThe etnaviv authors 	memcpy(iter->data, ptr, size);
112a8c21a54SThe etnaviv authors 
113a8c21a54SThe etnaviv authors 	iter->hdr->iova = cpu_to_le64(iova);
114a8c21a54SThe etnaviv authors 
115a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, type, iter->data + size);
116a8c21a54SThe etnaviv authors }
117a8c21a54SThe etnaviv authors 
1189a1fdae5SLucas Stach void etnaviv_core_dump(struct etnaviv_gem_submit *submit)
119a8c21a54SThe etnaviv authors {
1209a1fdae5SLucas Stach 	struct etnaviv_gpu *gpu = submit->gpu;
121a8c21a54SThe etnaviv authors 	struct core_dump_iterator iter;
122a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *obj;
123a8c21a54SThe etnaviv authors 	unsigned int n_obj, n_bomap_pages;
124a8c21a54SThe etnaviv authors 	size_t file_size, mmu_size;
125a8c21a54SThe etnaviv authors 	__le64 *bomap, *bomap_start;
1269a1fdae5SLucas Stach 	int i;
127a8c21a54SThe etnaviv authors 
1286d7a20c0SLucas Stach 	/* Only catch the first event, or when manually re-armed */
1296d7a20c0SLucas Stach 	if (!etnaviv_dump_core)
1306d7a20c0SLucas Stach 		return;
1316d7a20c0SLucas Stach 	etnaviv_dump_core = false;
1326d7a20c0SLucas Stach 
13320faf200SLucas Stach 	mutex_lock(&submit->mmu_context->lock);
1341396500dSLucas Stach 
13520faf200SLucas Stach 	mmu_size = etnaviv_iommu_dump_size(submit->mmu_context);
136a8c21a54SThe etnaviv authors 
1379a1fdae5SLucas Stach 	/* We always dump registers, mmu, ring, hanging cmdbuf and end marker */
1389a1fdae5SLucas Stach 	n_obj = 5;
139a8c21a54SThe etnaviv authors 	n_bomap_pages = 0;
140a8c21a54SThe etnaviv authors 	file_size = ARRAY_SIZE(etnaviv_dump_registers) *
141a8c21a54SThe etnaviv authors 			sizeof(struct etnaviv_dump_registers) +
1429a1fdae5SLucas Stach 		    mmu_size + gpu->buffer.size + submit->cmdbuf.size;
143a8c21a54SThe etnaviv authors 
144a8c21a54SThe etnaviv authors 	/* Add in the active buffer objects */
1459a1fdae5SLucas Stach 	for (i = 0; i < submit->nr_bos; i++) {
1469a1fdae5SLucas Stach 		obj = submit->bos[i].obj;
147a8c21a54SThe etnaviv authors 		file_size += obj->base.size;
148a8c21a54SThe etnaviv authors 		n_bomap_pages += obj->base.size >> PAGE_SHIFT;
149a8c21a54SThe etnaviv authors 		n_obj++;
150a8c21a54SThe etnaviv authors 	}
151a8c21a54SThe etnaviv authors 
152a8c21a54SThe etnaviv authors 	/* If we have any buffer objects, add a bomap object */
153a8c21a54SThe etnaviv authors 	if (n_bomap_pages) {
154a8c21a54SThe etnaviv authors 		file_size += n_bomap_pages * sizeof(__le64);
155a8c21a54SThe etnaviv authors 		n_obj++;
156a8c21a54SThe etnaviv authors 	}
157a8c21a54SThe etnaviv authors 
158a8c21a54SThe etnaviv authors 	/* Add the size of the headers */
159a8c21a54SThe etnaviv authors 	file_size += sizeof(*iter.hdr) * n_obj;
160a8c21a54SThe etnaviv authors 
161a8c21a54SThe etnaviv authors 	/* Allocate the file in vmalloc memory, it's likely to be big */
162*a3ecd97aSLucas Stach 	iter.start = __vmalloc(file_size, GFP_NOWAIT);
163a8c21a54SThe etnaviv authors 	if (!iter.start) {
16420faf200SLucas Stach 		mutex_unlock(&submit->mmu_context->lock);
165a8c21a54SThe etnaviv authors 		dev_warn(gpu->dev, "failed to allocate devcoredump file\n");
166a8c21a54SThe etnaviv authors 		return;
167a8c21a54SThe etnaviv authors 	}
168a8c21a54SThe etnaviv authors 
169a8c21a54SThe etnaviv authors 	/* Point the data member after the headers */
170a8c21a54SThe etnaviv authors 	iter.hdr = iter.start;
171a8c21a54SThe etnaviv authors 	iter.data = &iter.hdr[n_obj];
172a8c21a54SThe etnaviv authors 
173a8c21a54SThe etnaviv authors 	memset(iter.hdr, 0, iter.data - iter.start);
174a8c21a54SThe etnaviv authors 
175a8c21a54SThe etnaviv authors 	etnaviv_core_dump_registers(&iter, gpu);
17620faf200SLucas Stach 	etnaviv_core_dump_mmu(&iter, submit->mmu_context, mmu_size);
1772f9225dbSLucas Stach 	etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr,
1782f9225dbSLucas Stach 			      gpu->buffer.size,
179db82a043SLucas Stach 			      etnaviv_cmdbuf_get_va(&gpu->buffer,
18020faf200SLucas Stach 					&submit->mmu_context->cmdbuf_mapping));
181a8c21a54SThe etnaviv authors 
1822f9225dbSLucas Stach 	etnaviv_core_dump_mem(&iter, ETDUMP_BUF_CMD,
1832f9225dbSLucas Stach 			      submit->cmdbuf.vaddr, submit->cmdbuf.size,
184db82a043SLucas Stach 			      etnaviv_cmdbuf_get_va(&submit->cmdbuf,
18520faf200SLucas Stach 					&submit->mmu_context->cmdbuf_mapping));
186a8c21a54SThe etnaviv authors 
18720faf200SLucas Stach 	mutex_unlock(&submit->mmu_context->lock);
188ca8cb695SLucas Stach 
189a8c21a54SThe etnaviv authors 	/* Reserve space for the bomap */
190a8c21a54SThe etnaviv authors 	if (n_bomap_pages) {
191a8c21a54SThe etnaviv authors 		bomap_start = bomap = iter.data;
192a8c21a54SThe etnaviv authors 		memset(bomap, 0, sizeof(*bomap) * n_bomap_pages);
193a8c21a54SThe etnaviv authors 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BOMAP,
194a8c21a54SThe etnaviv authors 					 bomap + n_bomap_pages);
195a8c21a54SThe etnaviv authors 	} else {
196a8c21a54SThe etnaviv authors 		/* Silence warning */
197a8c21a54SThe etnaviv authors 		bomap_start = bomap = NULL;
198a8c21a54SThe etnaviv authors 	}
199a8c21a54SThe etnaviv authors 
2009a1fdae5SLucas Stach 	for (i = 0; i < submit->nr_bos; i++) {
2019a1fdae5SLucas Stach 		struct etnaviv_vram_mapping *vram;
202a8c21a54SThe etnaviv authors 		struct page **pages;
203a8c21a54SThe etnaviv authors 		void *vaddr;
204a8c21a54SThe etnaviv authors 
2059a1fdae5SLucas Stach 		obj = submit->bos[i].obj;
2069a1fdae5SLucas Stach 		vram = submit->bos[i].mapping;
207a8c21a54SThe etnaviv authors 
208339073efSLucas Stach 		mutex_lock(&obj->lock);
209a8c21a54SThe etnaviv authors 		pages = etnaviv_gem_get_pages(obj);
210339073efSLucas Stach 		mutex_unlock(&obj->lock);
211f8261c37SDan Carpenter 		if (!IS_ERR(pages)) {
212a8c21a54SThe etnaviv authors 			int j;
213a8c21a54SThe etnaviv authors 
21403a27539SMarc Kleine-Budde 			iter.hdr->data[0] = cpu_to_le32((bomap - bomap_start));
215a8c21a54SThe etnaviv authors 
216a8c21a54SThe etnaviv authors 			for (j = 0; j < obj->base.size >> PAGE_SHIFT; j++)
217a8c21a54SThe etnaviv authors 				*bomap++ = cpu_to_le64(page_to_phys(*pages++));
218a8c21a54SThe etnaviv authors 		}
219a8c21a54SThe etnaviv authors 
220a8c21a54SThe etnaviv authors 		iter.hdr->iova = cpu_to_le64(vram->iova);
221a8c21a54SThe etnaviv authors 
222ce3088fdSLucas Stach 		vaddr = etnaviv_gem_vmap(&obj->base);
2239f07bb0dSLucas Stach 		if (vaddr)
224a8c21a54SThe etnaviv authors 			memcpy(iter.data, vaddr, obj->base.size);
225a8c21a54SThe etnaviv authors 
226a8c21a54SThe etnaviv authors 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BO, iter.data +
227a8c21a54SThe etnaviv authors 					 obj->base.size);
228a8c21a54SThe etnaviv authors 	}
229a8c21a54SThe etnaviv authors 
230a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(&iter, ETDUMP_BUF_END, iter.data);
231a8c21a54SThe etnaviv authors 
232*a3ecd97aSLucas Stach 	dev_coredumpv(gpu->dev, iter.start, iter.data - iter.start, GFP_NOWAIT);
233a8c21a54SThe etnaviv authors }
234