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