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