xref: /linux/drivers/gpu/drm/etnaviv/etnaviv_dump.c (revision 1396500d673bd027683a0609ff84dca7eb6ea2e7)
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>
7ea1f5729SLucas Stach #include "etnaviv_cmdbuf.h"
8a8c21a54SThe etnaviv authors #include "etnaviv_dump.h"
9a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
10a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
11a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
126d7a20c0SLucas Stach #include "etnaviv_sched.h"
13a8c21a54SThe etnaviv authors #include "state.xml.h"
14a8c21a54SThe etnaviv authors #include "state_hi.xml.h"
15a8c21a54SThe etnaviv authors 
166d7a20c0SLucas Stach static bool etnaviv_dump_core = true;
176d7a20c0SLucas Stach module_param_named(dump_core, etnaviv_dump_core, bool, 0600);
186d7a20c0SLucas Stach 
19a8c21a54SThe etnaviv authors struct core_dump_iterator {
20a8c21a54SThe etnaviv authors 	void *start;
21a8c21a54SThe etnaviv authors 	struct etnaviv_dump_object_header *hdr;
22a8c21a54SThe etnaviv authors 	void *data;
23a8c21a54SThe etnaviv authors };
24a8c21a54SThe etnaviv authors 
25a8c21a54SThe etnaviv authors static const unsigned short etnaviv_dump_registers[] = {
26a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_STATUS,
27a8c21a54SThe etnaviv authors 	VIVS_HI_CLOCK_CONTROL,
28a8c21a54SThe etnaviv authors 	VIVS_HI_IDLE_STATE,
29a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_CONFIG,
30a8c21a54SThe etnaviv authors 	VIVS_HI_INTR_ENBL,
31a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_IDENTITY,
32a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_FEATURE,
33a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_MODEL,
34a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_REV,
35a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_DATE,
36a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_TIME,
37a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_MINOR_FEATURE_0,
38a8c21a54SThe etnaviv authors 	VIVS_HI_CACHE_CONTROL,
39a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_CONTROL,
40a8c21a54SThe etnaviv authors 	VIVS_PM_POWER_CONTROLS,
41a8c21a54SThe etnaviv authors 	VIVS_PM_MODULE_CONTROLS,
42a8c21a54SThe etnaviv authors 	VIVS_PM_MODULE_STATUS,
43a8c21a54SThe etnaviv authors 	VIVS_PM_PULSE_EATER,
44a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_FE_PAGE_TABLE,
45a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_TX_PAGE_TABLE,
46a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_PE_PAGE_TABLE,
47a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_PEZ_PAGE_TABLE,
48a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_RA_PAGE_TABLE,
49a8c21a54SThe etnaviv authors 	VIVS_MC_DEBUG_MEMORY,
50a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_RA,
51a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_FE,
52a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_TX,
53a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_PEZ,
54a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_PE,
55a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_TIMING_CONTROL,
56a8c21a54SThe etnaviv authors 	VIVS_MC_BUS_CONFIG,
57a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_STATUS,
58a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_DEBUG_STATE,
59a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_ADDRESS,
60a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_LOW,
61a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_HIGH,
62a8c21a54SThe etnaviv authors 	VIVS_FE_AUTO_FLUSH,
63a8c21a54SThe etnaviv authors };
64a8c21a54SThe etnaviv authors 
65a8c21a54SThe etnaviv authors static void etnaviv_core_dump_header(struct core_dump_iterator *iter,
66a8c21a54SThe etnaviv authors 	u32 type, void *data_end)
67a8c21a54SThe etnaviv authors {
68a8c21a54SThe etnaviv authors 	struct etnaviv_dump_object_header *hdr = iter->hdr;
69a8c21a54SThe etnaviv authors 
70a8c21a54SThe etnaviv authors 	hdr->magic = cpu_to_le32(ETDUMP_MAGIC);
71a8c21a54SThe etnaviv authors 	hdr->type = cpu_to_le32(type);
72a8c21a54SThe etnaviv authors 	hdr->file_offset = cpu_to_le32(iter->data - iter->start);
73a8c21a54SThe etnaviv authors 	hdr->file_size = cpu_to_le32(data_end - iter->data);
74a8c21a54SThe etnaviv authors 
75a8c21a54SThe etnaviv authors 	iter->hdr++;
76a8c21a54SThe etnaviv authors 	iter->data += hdr->file_size;
77a8c21a54SThe etnaviv authors }
78a8c21a54SThe etnaviv authors 
79a8c21a54SThe etnaviv authors static void etnaviv_core_dump_registers(struct core_dump_iterator *iter,
80a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu)
81a8c21a54SThe etnaviv authors {
82a8c21a54SThe etnaviv authors 	struct etnaviv_dump_registers *reg = iter->data;
83a8c21a54SThe etnaviv authors 	unsigned int i;
84a8c21a54SThe etnaviv authors 
85a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(etnaviv_dump_registers); i++, reg++) {
86a8c21a54SThe etnaviv authors 		reg->reg = etnaviv_dump_registers[i];
87a8c21a54SThe etnaviv authors 		reg->value = gpu_read(gpu, etnaviv_dump_registers[i]);
88a8c21a54SThe etnaviv authors 	}
89a8c21a54SThe etnaviv authors 
90a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, ETDUMP_BUF_REG, reg);
91a8c21a54SThe etnaviv authors }
92a8c21a54SThe etnaviv authors 
93a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mmu(struct core_dump_iterator *iter,
94a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu, size_t mmu_size)
95a8c21a54SThe etnaviv authors {
96a8c21a54SThe etnaviv authors 	etnaviv_iommu_dump(gpu->mmu, iter->data);
97a8c21a54SThe etnaviv authors 
98a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, ETDUMP_BUF_MMU, iter->data + mmu_size);
99a8c21a54SThe etnaviv authors }
100a8c21a54SThe etnaviv authors 
101a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mem(struct core_dump_iterator *iter, u32 type,
102a8c21a54SThe etnaviv authors 	void *ptr, size_t size, u64 iova)
103a8c21a54SThe etnaviv authors {
104a8c21a54SThe etnaviv authors 	memcpy(iter->data, ptr, size);
105a8c21a54SThe etnaviv authors 
106a8c21a54SThe etnaviv authors 	iter->hdr->iova = cpu_to_le64(iova);
107a8c21a54SThe etnaviv authors 
108a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, type, iter->data + size);
109a8c21a54SThe etnaviv authors }
110a8c21a54SThe etnaviv authors 
111a8c21a54SThe etnaviv authors void etnaviv_core_dump(struct etnaviv_gpu *gpu)
112a8c21a54SThe etnaviv authors {
113a8c21a54SThe etnaviv authors 	struct core_dump_iterator iter;
114a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *vram;
115a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *obj;
1162f9225dbSLucas Stach 	struct etnaviv_gem_submit *submit;
1176d7a20c0SLucas Stach 	struct drm_sched_job *s_job;
118a8c21a54SThe etnaviv authors 	unsigned int n_obj, n_bomap_pages;
119a8c21a54SThe etnaviv authors 	size_t file_size, mmu_size;
120a8c21a54SThe etnaviv authors 	__le64 *bomap, *bomap_start;
1211db8c142SSharat Masetty 	unsigned long flags;
122a8c21a54SThe etnaviv authors 
1236d7a20c0SLucas Stach 	/* Only catch the first event, or when manually re-armed */
1246d7a20c0SLucas Stach 	if (!etnaviv_dump_core)
1256d7a20c0SLucas Stach 		return;
1266d7a20c0SLucas Stach 	etnaviv_dump_core = false;
1276d7a20c0SLucas Stach 
128*1396500dSLucas Stach 	mutex_lock(&gpu->mmu->lock);
129*1396500dSLucas Stach 
130a8c21a54SThe etnaviv authors 	mmu_size = etnaviv_iommu_dump_size(gpu->mmu);
131a8c21a54SThe etnaviv authors 
132a8c21a54SThe etnaviv authors 	/* We always dump registers, mmu, ring and end marker */
133a8c21a54SThe etnaviv authors 	n_obj = 4;
134a8c21a54SThe etnaviv authors 	n_bomap_pages = 0;
135a8c21a54SThe etnaviv authors 	file_size = ARRAY_SIZE(etnaviv_dump_registers) *
136a8c21a54SThe etnaviv authors 			sizeof(struct etnaviv_dump_registers) +
1372f9225dbSLucas Stach 		    mmu_size + gpu->buffer.size;
138a8c21a54SThe etnaviv authors 
139a8c21a54SThe etnaviv authors 	/* Add in the active command buffers */
140e7df065aSDave Airlie 	spin_lock_irqsave(&gpu->sched.job_list_lock, flags);
1416d7a20c0SLucas Stach 	list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) {
1426d7a20c0SLucas Stach 		submit = to_etnaviv_submit(s_job);
1432f9225dbSLucas Stach 		file_size += submit->cmdbuf.size;
144a8c21a54SThe etnaviv authors 		n_obj++;
145a8c21a54SThe etnaviv authors 	}
146e7df065aSDave Airlie 	spin_unlock_irqrestore(&gpu->sched.job_list_lock, flags);
147a8c21a54SThe etnaviv authors 
148a8c21a54SThe etnaviv authors 	/* Add in the active buffer objects */
149a8c21a54SThe etnaviv authors 	list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) {
150a8c21a54SThe etnaviv authors 		if (!vram->use)
151a8c21a54SThe etnaviv authors 			continue;
152a8c21a54SThe etnaviv authors 
153a8c21a54SThe etnaviv authors 		obj = vram->object;
154a8c21a54SThe etnaviv authors 		file_size += obj->base.size;
155a8c21a54SThe etnaviv authors 		n_bomap_pages += obj->base.size >> PAGE_SHIFT;
156a8c21a54SThe etnaviv authors 		n_obj++;
157a8c21a54SThe etnaviv authors 	}
158a8c21a54SThe etnaviv authors 
159a8c21a54SThe etnaviv authors 	/* If we have any buffer objects, add a bomap object */
160a8c21a54SThe etnaviv authors 	if (n_bomap_pages) {
161a8c21a54SThe etnaviv authors 		file_size += n_bomap_pages * sizeof(__le64);
162a8c21a54SThe etnaviv authors 		n_obj++;
163a8c21a54SThe etnaviv authors 	}
164a8c21a54SThe etnaviv authors 
165a8c21a54SThe etnaviv authors 	/* Add the size of the headers */
166a8c21a54SThe etnaviv authors 	file_size += sizeof(*iter.hdr) * n_obj;
167a8c21a54SThe etnaviv authors 
168a8c21a54SThe etnaviv authors 	/* Allocate the file in vmalloc memory, it's likely to be big */
16919809c2dSMichal Hocko 	iter.start = __vmalloc(file_size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
17019809c2dSMichal Hocko 			       PAGE_KERNEL);
171a8c21a54SThe etnaviv authors 	if (!iter.start) {
172*1396500dSLucas Stach 		mutex_unlock(&gpu->mmu->lock);
173a8c21a54SThe etnaviv authors 		dev_warn(gpu->dev, "failed to allocate devcoredump file\n");
174a8c21a54SThe etnaviv authors 		return;
175a8c21a54SThe etnaviv authors 	}
176a8c21a54SThe etnaviv authors 
177a8c21a54SThe etnaviv authors 	/* Point the data member after the headers */
178a8c21a54SThe etnaviv authors 	iter.hdr = iter.start;
179a8c21a54SThe etnaviv authors 	iter.data = &iter.hdr[n_obj];
180a8c21a54SThe etnaviv authors 
181a8c21a54SThe etnaviv authors 	memset(iter.hdr, 0, iter.data - iter.start);
182a8c21a54SThe etnaviv authors 
183a8c21a54SThe etnaviv authors 	etnaviv_core_dump_registers(&iter, gpu);
184a8c21a54SThe etnaviv authors 	etnaviv_core_dump_mmu(&iter, gpu, mmu_size);
1852f9225dbSLucas Stach 	etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr,
1862f9225dbSLucas Stach 			      gpu->buffer.size,
1872f9225dbSLucas Stach 			      etnaviv_cmdbuf_get_va(&gpu->buffer));
188a8c21a54SThe etnaviv authors 
189e7df065aSDave Airlie 	spin_lock_irqsave(&gpu->sched.job_list_lock, flags);
1906d7a20c0SLucas Stach 	list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) {
1916d7a20c0SLucas Stach 		submit = to_etnaviv_submit(s_job);
1922f9225dbSLucas Stach 		etnaviv_core_dump_mem(&iter, ETDUMP_BUF_CMD,
1932f9225dbSLucas Stach 				      submit->cmdbuf.vaddr, submit->cmdbuf.size,
1942f9225dbSLucas Stach 				      etnaviv_cmdbuf_get_va(&submit->cmdbuf));
1956d7a20c0SLucas Stach 	}
196e7df065aSDave Airlie 	spin_unlock_irqrestore(&gpu->sched.job_list_lock, flags);
197a8c21a54SThe etnaviv authors 
198a8c21a54SThe etnaviv authors 	/* Reserve space for the bomap */
199a8c21a54SThe etnaviv authors 	if (n_bomap_pages) {
200a8c21a54SThe etnaviv authors 		bomap_start = bomap = iter.data;
201a8c21a54SThe etnaviv authors 		memset(bomap, 0, sizeof(*bomap) * n_bomap_pages);
202a8c21a54SThe etnaviv authors 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BOMAP,
203a8c21a54SThe etnaviv authors 					 bomap + n_bomap_pages);
204a8c21a54SThe etnaviv authors 	} else {
205a8c21a54SThe etnaviv authors 		/* Silence warning */
206a8c21a54SThe etnaviv authors 		bomap_start = bomap = NULL;
207a8c21a54SThe etnaviv authors 	}
208a8c21a54SThe etnaviv authors 
209a8c21a54SThe etnaviv authors 	list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) {
210a8c21a54SThe etnaviv authors 		struct page **pages;
211a8c21a54SThe etnaviv authors 		void *vaddr;
212a8c21a54SThe etnaviv authors 
213a8c21a54SThe etnaviv authors 		if (vram->use == 0)
214a8c21a54SThe etnaviv authors 			continue;
215a8c21a54SThe etnaviv authors 
216a8c21a54SThe etnaviv authors 		obj = vram->object;
217a8c21a54SThe etnaviv authors 
218339073efSLucas Stach 		mutex_lock(&obj->lock);
219a8c21a54SThe etnaviv authors 		pages = etnaviv_gem_get_pages(obj);
220339073efSLucas Stach 		mutex_unlock(&obj->lock);
221f8261c37SDan Carpenter 		if (!IS_ERR(pages)) {
222a8c21a54SThe etnaviv authors 			int j;
223a8c21a54SThe etnaviv authors 
224a8c21a54SThe etnaviv authors 			iter.hdr->data[0] = bomap - bomap_start;
225a8c21a54SThe etnaviv authors 
226a8c21a54SThe etnaviv authors 			for (j = 0; j < obj->base.size >> PAGE_SHIFT; j++)
227a8c21a54SThe etnaviv authors 				*bomap++ = cpu_to_le64(page_to_phys(*pages++));
228a8c21a54SThe etnaviv authors 		}
229a8c21a54SThe etnaviv authors 
230a8c21a54SThe etnaviv authors 		iter.hdr->iova = cpu_to_le64(vram->iova);
231a8c21a54SThe etnaviv authors 
232ce3088fdSLucas Stach 		vaddr = etnaviv_gem_vmap(&obj->base);
2339f07bb0dSLucas Stach 		if (vaddr)
234a8c21a54SThe etnaviv authors 			memcpy(iter.data, vaddr, obj->base.size);
235a8c21a54SThe etnaviv authors 
236a8c21a54SThe etnaviv authors 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BO, iter.data +
237a8c21a54SThe etnaviv authors 					 obj->base.size);
238a8c21a54SThe etnaviv authors 	}
239a8c21a54SThe etnaviv authors 
240*1396500dSLucas Stach 	mutex_unlock(&gpu->mmu->lock);
241*1396500dSLucas Stach 
242a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(&iter, ETDUMP_BUF_END, iter.data);
243a8c21a54SThe etnaviv authors 
244a8c21a54SThe etnaviv authors 	dev_coredumpv(gpu->dev, iter.start, iter.data - iter.start, GFP_KERNEL);
245a8c21a54SThe etnaviv authors }
246