xref: /linux/drivers/gpu/drm/etnaviv/etnaviv_dump.c (revision 6d7a20c0776036115c6e22bc673d645d524c4b8a)
1a8c21a54SThe etnaviv authors /*
2a8c21a54SThe etnaviv authors  * Copyright (C) 2015 Etnaviv Project
3a8c21a54SThe etnaviv authors  *
4a8c21a54SThe etnaviv authors  * This program is free software; you can redistribute it and/or modify it
5a8c21a54SThe etnaviv authors  * under the terms of the GNU General Public License version 2 as published by
6a8c21a54SThe etnaviv authors  * the Free Software Foundation.
7a8c21a54SThe etnaviv authors  *
8a8c21a54SThe etnaviv authors  * This program is distributed in the hope that it will be useful, but WITHOUT
9a8c21a54SThe etnaviv authors  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10a8c21a54SThe etnaviv authors  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11a8c21a54SThe etnaviv authors  * more details.
12a8c21a54SThe etnaviv authors  *
13a8c21a54SThe etnaviv authors  * You should have received a copy of the GNU General Public License along with
14a8c21a54SThe etnaviv authors  * this program.  If not, see <http://www.gnu.org/licenses/>.
15a8c21a54SThe etnaviv authors  */
16a8c21a54SThe etnaviv authors 
17a8c21a54SThe etnaviv authors #include <linux/devcoredump.h>
18ea1f5729SLucas Stach #include "etnaviv_cmdbuf.h"
19a8c21a54SThe etnaviv authors #include "etnaviv_dump.h"
20a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
21a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
22a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
23*6d7a20c0SLucas Stach #include "etnaviv_sched.h"
24a8c21a54SThe etnaviv authors #include "state.xml.h"
25a8c21a54SThe etnaviv authors #include "state_hi.xml.h"
26a8c21a54SThe etnaviv authors 
27*6d7a20c0SLucas Stach static bool etnaviv_dump_core = true;
28*6d7a20c0SLucas Stach module_param_named(dump_core, etnaviv_dump_core, bool, 0600);
29*6d7a20c0SLucas Stach 
30a8c21a54SThe etnaviv authors struct core_dump_iterator {
31a8c21a54SThe etnaviv authors 	void *start;
32a8c21a54SThe etnaviv authors 	struct etnaviv_dump_object_header *hdr;
33a8c21a54SThe etnaviv authors 	void *data;
34a8c21a54SThe etnaviv authors };
35a8c21a54SThe etnaviv authors 
36a8c21a54SThe etnaviv authors static const unsigned short etnaviv_dump_registers[] = {
37a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_STATUS,
38a8c21a54SThe etnaviv authors 	VIVS_HI_CLOCK_CONTROL,
39a8c21a54SThe etnaviv authors 	VIVS_HI_IDLE_STATE,
40a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_CONFIG,
41a8c21a54SThe etnaviv authors 	VIVS_HI_INTR_ENBL,
42a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_IDENTITY,
43a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_FEATURE,
44a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_MODEL,
45a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_REV,
46a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_DATE,
47a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_TIME,
48a8c21a54SThe etnaviv authors 	VIVS_HI_CHIP_MINOR_FEATURE_0,
49a8c21a54SThe etnaviv authors 	VIVS_HI_CACHE_CONTROL,
50a8c21a54SThe etnaviv authors 	VIVS_HI_AXI_CONTROL,
51a8c21a54SThe etnaviv authors 	VIVS_PM_POWER_CONTROLS,
52a8c21a54SThe etnaviv authors 	VIVS_PM_MODULE_CONTROLS,
53a8c21a54SThe etnaviv authors 	VIVS_PM_MODULE_STATUS,
54a8c21a54SThe etnaviv authors 	VIVS_PM_PULSE_EATER,
55a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_FE_PAGE_TABLE,
56a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_TX_PAGE_TABLE,
57a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_PE_PAGE_TABLE,
58a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_PEZ_PAGE_TABLE,
59a8c21a54SThe etnaviv authors 	VIVS_MC_MMU_RA_PAGE_TABLE,
60a8c21a54SThe etnaviv authors 	VIVS_MC_DEBUG_MEMORY,
61a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_RA,
62a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_FE,
63a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_TX,
64a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_PEZ,
65a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_BASE_ADDR_PE,
66a8c21a54SThe etnaviv authors 	VIVS_MC_MEMORY_TIMING_CONTROL,
67a8c21a54SThe etnaviv authors 	VIVS_MC_BUS_CONFIG,
68a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_STATUS,
69a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_DEBUG_STATE,
70a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_ADDRESS,
71a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_LOW,
72a8c21a54SThe etnaviv authors 	VIVS_FE_DMA_HIGH,
73a8c21a54SThe etnaviv authors 	VIVS_FE_AUTO_FLUSH,
74a8c21a54SThe etnaviv authors };
75a8c21a54SThe etnaviv authors 
76a8c21a54SThe etnaviv authors static void etnaviv_core_dump_header(struct core_dump_iterator *iter,
77a8c21a54SThe etnaviv authors 	u32 type, void *data_end)
78a8c21a54SThe etnaviv authors {
79a8c21a54SThe etnaviv authors 	struct etnaviv_dump_object_header *hdr = iter->hdr;
80a8c21a54SThe etnaviv authors 
81a8c21a54SThe etnaviv authors 	hdr->magic = cpu_to_le32(ETDUMP_MAGIC);
82a8c21a54SThe etnaviv authors 	hdr->type = cpu_to_le32(type);
83a8c21a54SThe etnaviv authors 	hdr->file_offset = cpu_to_le32(iter->data - iter->start);
84a8c21a54SThe etnaviv authors 	hdr->file_size = cpu_to_le32(data_end - iter->data);
85a8c21a54SThe etnaviv authors 
86a8c21a54SThe etnaviv authors 	iter->hdr++;
87a8c21a54SThe etnaviv authors 	iter->data += hdr->file_size;
88a8c21a54SThe etnaviv authors }
89a8c21a54SThe etnaviv authors 
90a8c21a54SThe etnaviv authors static void etnaviv_core_dump_registers(struct core_dump_iterator *iter,
91a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu)
92a8c21a54SThe etnaviv authors {
93a8c21a54SThe etnaviv authors 	struct etnaviv_dump_registers *reg = iter->data;
94a8c21a54SThe etnaviv authors 	unsigned int i;
95a8c21a54SThe etnaviv authors 
96a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(etnaviv_dump_registers); i++, reg++) {
97a8c21a54SThe etnaviv authors 		reg->reg = etnaviv_dump_registers[i];
98a8c21a54SThe etnaviv authors 		reg->value = gpu_read(gpu, etnaviv_dump_registers[i]);
99a8c21a54SThe etnaviv authors 	}
100a8c21a54SThe etnaviv authors 
101a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, ETDUMP_BUF_REG, reg);
102a8c21a54SThe etnaviv authors }
103a8c21a54SThe etnaviv authors 
104a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mmu(struct core_dump_iterator *iter,
105a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu, size_t mmu_size)
106a8c21a54SThe etnaviv authors {
107a8c21a54SThe etnaviv authors 	etnaviv_iommu_dump(gpu->mmu, iter->data);
108a8c21a54SThe etnaviv authors 
109a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, ETDUMP_BUF_MMU, iter->data + mmu_size);
110a8c21a54SThe etnaviv authors }
111a8c21a54SThe etnaviv authors 
112a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mem(struct core_dump_iterator *iter, u32 type,
113a8c21a54SThe etnaviv authors 	void *ptr, size_t size, u64 iova)
114a8c21a54SThe etnaviv authors {
115a8c21a54SThe etnaviv authors 	memcpy(iter->data, ptr, size);
116a8c21a54SThe etnaviv authors 
117a8c21a54SThe etnaviv authors 	iter->hdr->iova = cpu_to_le64(iova);
118a8c21a54SThe etnaviv authors 
119a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(iter, type, iter->data + size);
120a8c21a54SThe etnaviv authors }
121a8c21a54SThe etnaviv authors 
122a8c21a54SThe etnaviv authors void etnaviv_core_dump(struct etnaviv_gpu *gpu)
123a8c21a54SThe etnaviv authors {
124a8c21a54SThe etnaviv authors 	struct core_dump_iterator iter;
125a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *vram;
126a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *obj;
1272f9225dbSLucas Stach 	struct etnaviv_gem_submit *submit;
128*6d7a20c0SLucas Stach 	struct drm_sched_job *s_job;
129a8c21a54SThe etnaviv authors 	unsigned int n_obj, n_bomap_pages;
130a8c21a54SThe etnaviv authors 	size_t file_size, mmu_size;
131a8c21a54SThe etnaviv authors 	__le64 *bomap, *bomap_start;
132a8c21a54SThe etnaviv authors 
133*6d7a20c0SLucas Stach 	/* Only catch the first event, or when manually re-armed */
134*6d7a20c0SLucas Stach 	if (!etnaviv_dump_core)
135*6d7a20c0SLucas Stach 		return;
136*6d7a20c0SLucas Stach 	etnaviv_dump_core = false;
137*6d7a20c0SLucas Stach 
138a8c21a54SThe etnaviv authors 	mmu_size = etnaviv_iommu_dump_size(gpu->mmu);
139a8c21a54SThe etnaviv authors 
140a8c21a54SThe etnaviv authors 	/* We always dump registers, mmu, ring and end marker */
141a8c21a54SThe etnaviv authors 	n_obj = 4;
142a8c21a54SThe etnaviv authors 	n_bomap_pages = 0;
143a8c21a54SThe etnaviv authors 	file_size = ARRAY_SIZE(etnaviv_dump_registers) *
144a8c21a54SThe etnaviv authors 			sizeof(struct etnaviv_dump_registers) +
1452f9225dbSLucas Stach 		    mmu_size + gpu->buffer.size;
146a8c21a54SThe etnaviv authors 
147a8c21a54SThe etnaviv authors 	/* Add in the active command buffers */
148*6d7a20c0SLucas Stach 	spin_lock(&gpu->sched.job_list_lock);
149*6d7a20c0SLucas Stach 	list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) {
150*6d7a20c0SLucas Stach 		submit = to_etnaviv_submit(s_job);
1512f9225dbSLucas Stach 		file_size += submit->cmdbuf.size;
152a8c21a54SThe etnaviv authors 		n_obj++;
153a8c21a54SThe etnaviv authors 	}
154*6d7a20c0SLucas Stach 	spin_unlock(&gpu->sched.job_list_lock);
155a8c21a54SThe etnaviv authors 
156a8c21a54SThe etnaviv authors 	/* Add in the active buffer objects */
157a8c21a54SThe etnaviv authors 	list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) {
158a8c21a54SThe etnaviv authors 		if (!vram->use)
159a8c21a54SThe etnaviv authors 			continue;
160a8c21a54SThe etnaviv authors 
161a8c21a54SThe etnaviv authors 		obj = vram->object;
162a8c21a54SThe etnaviv authors 		file_size += obj->base.size;
163a8c21a54SThe etnaviv authors 		n_bomap_pages += obj->base.size >> PAGE_SHIFT;
164a8c21a54SThe etnaviv authors 		n_obj++;
165a8c21a54SThe etnaviv authors 	}
166a8c21a54SThe etnaviv authors 
167a8c21a54SThe etnaviv authors 	/* If we have any buffer objects, add a bomap object */
168a8c21a54SThe etnaviv authors 	if (n_bomap_pages) {
169a8c21a54SThe etnaviv authors 		file_size += n_bomap_pages * sizeof(__le64);
170a8c21a54SThe etnaviv authors 		n_obj++;
171a8c21a54SThe etnaviv authors 	}
172a8c21a54SThe etnaviv authors 
173a8c21a54SThe etnaviv authors 	/* Add the size of the headers */
174a8c21a54SThe etnaviv authors 	file_size += sizeof(*iter.hdr) * n_obj;
175a8c21a54SThe etnaviv authors 
176a8c21a54SThe etnaviv authors 	/* Allocate the file in vmalloc memory, it's likely to be big */
17719809c2dSMichal Hocko 	iter.start = __vmalloc(file_size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
17819809c2dSMichal Hocko 			       PAGE_KERNEL);
179a8c21a54SThe etnaviv authors 	if (!iter.start) {
180a8c21a54SThe etnaviv authors 		dev_warn(gpu->dev, "failed to allocate devcoredump file\n");
181a8c21a54SThe etnaviv authors 		return;
182a8c21a54SThe etnaviv authors 	}
183a8c21a54SThe etnaviv authors 
184a8c21a54SThe etnaviv authors 	/* Point the data member after the headers */
185a8c21a54SThe etnaviv authors 	iter.hdr = iter.start;
186a8c21a54SThe etnaviv authors 	iter.data = &iter.hdr[n_obj];
187a8c21a54SThe etnaviv authors 
188a8c21a54SThe etnaviv authors 	memset(iter.hdr, 0, iter.data - iter.start);
189a8c21a54SThe etnaviv authors 
190a8c21a54SThe etnaviv authors 	etnaviv_core_dump_registers(&iter, gpu);
191a8c21a54SThe etnaviv authors 	etnaviv_core_dump_mmu(&iter, gpu, mmu_size);
1922f9225dbSLucas Stach 	etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr,
1932f9225dbSLucas Stach 			      gpu->buffer.size,
1942f9225dbSLucas Stach 			      etnaviv_cmdbuf_get_va(&gpu->buffer));
195a8c21a54SThe etnaviv authors 
196*6d7a20c0SLucas Stach 	spin_lock(&gpu->sched.job_list_lock);
197*6d7a20c0SLucas Stach 	list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) {
198*6d7a20c0SLucas Stach 		submit = to_etnaviv_submit(s_job);
1992f9225dbSLucas Stach 		etnaviv_core_dump_mem(&iter, ETDUMP_BUF_CMD,
2002f9225dbSLucas Stach 				      submit->cmdbuf.vaddr, submit->cmdbuf.size,
2012f9225dbSLucas Stach 				      etnaviv_cmdbuf_get_va(&submit->cmdbuf));
202*6d7a20c0SLucas Stach 	}
203*6d7a20c0SLucas Stach 	spin_unlock(&gpu->sched.job_list_lock);
204a8c21a54SThe etnaviv authors 
205a8c21a54SThe etnaviv authors 	/* Reserve space for the bomap */
206a8c21a54SThe etnaviv authors 	if (n_bomap_pages) {
207a8c21a54SThe etnaviv authors 		bomap_start = bomap = iter.data;
208a8c21a54SThe etnaviv authors 		memset(bomap, 0, sizeof(*bomap) * n_bomap_pages);
209a8c21a54SThe etnaviv authors 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BOMAP,
210a8c21a54SThe etnaviv authors 					 bomap + n_bomap_pages);
211a8c21a54SThe etnaviv authors 	} else {
212a8c21a54SThe etnaviv authors 		/* Silence warning */
213a8c21a54SThe etnaviv authors 		bomap_start = bomap = NULL;
214a8c21a54SThe etnaviv authors 	}
215a8c21a54SThe etnaviv authors 
216a8c21a54SThe etnaviv authors 	list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) {
217a8c21a54SThe etnaviv authors 		struct page **pages;
218a8c21a54SThe etnaviv authors 		void *vaddr;
219a8c21a54SThe etnaviv authors 
220a8c21a54SThe etnaviv authors 		if (vram->use == 0)
221a8c21a54SThe etnaviv authors 			continue;
222a8c21a54SThe etnaviv authors 
223a8c21a54SThe etnaviv authors 		obj = vram->object;
224a8c21a54SThe etnaviv authors 
225339073efSLucas Stach 		mutex_lock(&obj->lock);
226a8c21a54SThe etnaviv authors 		pages = etnaviv_gem_get_pages(obj);
227339073efSLucas Stach 		mutex_unlock(&obj->lock);
228a8c21a54SThe etnaviv authors 		if (pages) {
229a8c21a54SThe etnaviv authors 			int j;
230a8c21a54SThe etnaviv authors 
231a8c21a54SThe etnaviv authors 			iter.hdr->data[0] = bomap - bomap_start;
232a8c21a54SThe etnaviv authors 
233a8c21a54SThe etnaviv authors 			for (j = 0; j < obj->base.size >> PAGE_SHIFT; j++)
234a8c21a54SThe etnaviv authors 				*bomap++ = cpu_to_le64(page_to_phys(*pages++));
235a8c21a54SThe etnaviv authors 		}
236a8c21a54SThe etnaviv authors 
237a8c21a54SThe etnaviv authors 		iter.hdr->iova = cpu_to_le64(vram->iova);
238a8c21a54SThe etnaviv authors 
239ce3088fdSLucas Stach 		vaddr = etnaviv_gem_vmap(&obj->base);
2409f07bb0dSLucas Stach 		if (vaddr)
241a8c21a54SThe etnaviv authors 			memcpy(iter.data, vaddr, obj->base.size);
242a8c21a54SThe etnaviv authors 
243a8c21a54SThe etnaviv authors 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BO, iter.data +
244a8c21a54SThe etnaviv authors 					 obj->base.size);
245a8c21a54SThe etnaviv authors 	}
246a8c21a54SThe etnaviv authors 
247a8c21a54SThe etnaviv authors 	etnaviv_core_dump_header(&iter, ETDUMP_BUF_END, iter.data);
248a8c21a54SThe etnaviv authors 
249a8c21a54SThe etnaviv authors 	dev_coredumpv(gpu->dev, iter.start, iter.data - iter.start, GFP_KERNEL);
250a8c21a54SThe etnaviv authors }
251