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 	mmu_size = etnaviv_iommu_dump_size(gpu->mmu);
129 
130 	/* We always dump registers, mmu, ring and end marker */
131 	n_obj = 4;
132 	n_bomap_pages = 0;
133 	file_size = ARRAY_SIZE(etnaviv_dump_registers) *
134 			sizeof(struct etnaviv_dump_registers) +
135 		    mmu_size + gpu->buffer.size;
136 
137 	/* Add in the active command buffers */
138 	spin_lock_irqsave(&gpu->sched.job_list_lock, flags);
139 	list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) {
140 		submit = to_etnaviv_submit(s_job);
141 		file_size += submit->cmdbuf.size;
142 		n_obj++;
143 	}
144 	spin_unlock_irqrestore(&gpu->sched.job_list_lock, flags);
145 
146 	/* Add in the active buffer objects */
147 	list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) {
148 		if (!vram->use)
149 			continue;
150 
151 		obj = vram->object;
152 		file_size += obj->base.size;
153 		n_bomap_pages += obj->base.size >> PAGE_SHIFT;
154 		n_obj++;
155 	}
156 
157 	/* If we have any buffer objects, add a bomap object */
158 	if (n_bomap_pages) {
159 		file_size += n_bomap_pages * sizeof(__le64);
160 		n_obj++;
161 	}
162 
163 	/* Add the size of the headers */
164 	file_size += sizeof(*iter.hdr) * n_obj;
165 
166 	/* Allocate the file in vmalloc memory, it's likely to be big */
167 	iter.start = __vmalloc(file_size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
168 			       PAGE_KERNEL);
169 	if (!iter.start) {
170 		dev_warn(gpu->dev, "failed to allocate devcoredump file\n");
171 		return;
172 	}
173 
174 	/* Point the data member after the headers */
175 	iter.hdr = iter.start;
176 	iter.data = &iter.hdr[n_obj];
177 
178 	memset(iter.hdr, 0, iter.data - iter.start);
179 
180 	etnaviv_core_dump_registers(&iter, gpu);
181 	etnaviv_core_dump_mmu(&iter, gpu, mmu_size);
182 	etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr,
183 			      gpu->buffer.size,
184 			      etnaviv_cmdbuf_get_va(&gpu->buffer));
185 
186 	spin_lock_irqsave(&gpu->sched.job_list_lock, flags);
187 	list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) {
188 		submit = to_etnaviv_submit(s_job);
189 		etnaviv_core_dump_mem(&iter, ETDUMP_BUF_CMD,
190 				      submit->cmdbuf.vaddr, submit->cmdbuf.size,
191 				      etnaviv_cmdbuf_get_va(&submit->cmdbuf));
192 	}
193 	spin_unlock_irqrestore(&gpu->sched.job_list_lock, flags);
194 
195 	/* Reserve space for the bomap */
196 	if (n_bomap_pages) {
197 		bomap_start = bomap = iter.data;
198 		memset(bomap, 0, sizeof(*bomap) * n_bomap_pages);
199 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BOMAP,
200 					 bomap + n_bomap_pages);
201 	} else {
202 		/* Silence warning */
203 		bomap_start = bomap = NULL;
204 	}
205 
206 	list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) {
207 		struct page **pages;
208 		void *vaddr;
209 
210 		if (vram->use == 0)
211 			continue;
212 
213 		obj = vram->object;
214 
215 		mutex_lock(&obj->lock);
216 		pages = etnaviv_gem_get_pages(obj);
217 		mutex_unlock(&obj->lock);
218 		if (pages) {
219 			int j;
220 
221 			iter.hdr->data[0] = bomap - bomap_start;
222 
223 			for (j = 0; j < obj->base.size >> PAGE_SHIFT; j++)
224 				*bomap++ = cpu_to_le64(page_to_phys(*pages++));
225 		}
226 
227 		iter.hdr->iova = cpu_to_le64(vram->iova);
228 
229 		vaddr = etnaviv_gem_vmap(&obj->base);
230 		if (vaddr)
231 			memcpy(iter.data, vaddr, obj->base.size);
232 
233 		etnaviv_core_dump_header(&iter, ETDUMP_BUF_BO, iter.data +
234 					 obj->base.size);
235 	}
236 
237 	etnaviv_core_dump_header(&iter, ETDUMP_BUF_END, iter.data);
238 
239 	dev_coredumpv(gpu->dev, iter.start, iter.data - iter.start, GFP_KERNEL);
240 }
241