1*f6ffbd4fSLucas Stach // SPDX-License-Identifier: GPL-2.0 2a8c21a54SThe etnaviv authors /* 3*f6ffbd4fSLucas 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; 121a8c21a54SThe etnaviv authors 1226d7a20c0SLucas Stach /* Only catch the first event, or when manually re-armed */ 1236d7a20c0SLucas Stach if (!etnaviv_dump_core) 1246d7a20c0SLucas Stach return; 1256d7a20c0SLucas Stach etnaviv_dump_core = false; 1266d7a20c0SLucas Stach 127a8c21a54SThe etnaviv authors mmu_size = etnaviv_iommu_dump_size(gpu->mmu); 128a8c21a54SThe etnaviv authors 129a8c21a54SThe etnaviv authors /* We always dump registers, mmu, ring and end marker */ 130a8c21a54SThe etnaviv authors n_obj = 4; 131a8c21a54SThe etnaviv authors n_bomap_pages = 0; 132a8c21a54SThe etnaviv authors file_size = ARRAY_SIZE(etnaviv_dump_registers) * 133a8c21a54SThe etnaviv authors sizeof(struct etnaviv_dump_registers) + 1342f9225dbSLucas Stach mmu_size + gpu->buffer.size; 135a8c21a54SThe etnaviv authors 136a8c21a54SThe etnaviv authors /* Add in the active command buffers */ 1376d7a20c0SLucas Stach spin_lock(&gpu->sched.job_list_lock); 1386d7a20c0SLucas Stach list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) { 1396d7a20c0SLucas Stach submit = to_etnaviv_submit(s_job); 1402f9225dbSLucas Stach file_size += submit->cmdbuf.size; 141a8c21a54SThe etnaviv authors n_obj++; 142a8c21a54SThe etnaviv authors } 1436d7a20c0SLucas Stach spin_unlock(&gpu->sched.job_list_lock); 144a8c21a54SThe etnaviv authors 145a8c21a54SThe etnaviv authors /* Add in the active buffer objects */ 146a8c21a54SThe etnaviv authors list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) { 147a8c21a54SThe etnaviv authors if (!vram->use) 148a8c21a54SThe etnaviv authors continue; 149a8c21a54SThe etnaviv authors 150a8c21a54SThe etnaviv authors obj = vram->object; 151a8c21a54SThe etnaviv authors file_size += obj->base.size; 152a8c21a54SThe etnaviv authors n_bomap_pages += obj->base.size >> PAGE_SHIFT; 153a8c21a54SThe etnaviv authors n_obj++; 154a8c21a54SThe etnaviv authors } 155a8c21a54SThe etnaviv authors 156a8c21a54SThe etnaviv authors /* If we have any buffer objects, add a bomap object */ 157a8c21a54SThe etnaviv authors if (n_bomap_pages) { 158a8c21a54SThe etnaviv authors file_size += n_bomap_pages * sizeof(__le64); 159a8c21a54SThe etnaviv authors n_obj++; 160a8c21a54SThe etnaviv authors } 161a8c21a54SThe etnaviv authors 162a8c21a54SThe etnaviv authors /* Add the size of the headers */ 163a8c21a54SThe etnaviv authors file_size += sizeof(*iter.hdr) * n_obj; 164a8c21a54SThe etnaviv authors 165a8c21a54SThe etnaviv authors /* Allocate the file in vmalloc memory, it's likely to be big */ 16619809c2dSMichal Hocko iter.start = __vmalloc(file_size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY, 16719809c2dSMichal Hocko PAGE_KERNEL); 168a8c21a54SThe etnaviv authors if (!iter.start) { 169a8c21a54SThe etnaviv authors dev_warn(gpu->dev, "failed to allocate devcoredump file\n"); 170a8c21a54SThe etnaviv authors return; 171a8c21a54SThe etnaviv authors } 172a8c21a54SThe etnaviv authors 173a8c21a54SThe etnaviv authors /* Point the data member after the headers */ 174a8c21a54SThe etnaviv authors iter.hdr = iter.start; 175a8c21a54SThe etnaviv authors iter.data = &iter.hdr[n_obj]; 176a8c21a54SThe etnaviv authors 177a8c21a54SThe etnaviv authors memset(iter.hdr, 0, iter.data - iter.start); 178a8c21a54SThe etnaviv authors 179a8c21a54SThe etnaviv authors etnaviv_core_dump_registers(&iter, gpu); 180a8c21a54SThe etnaviv authors etnaviv_core_dump_mmu(&iter, gpu, mmu_size); 1812f9225dbSLucas Stach etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr, 1822f9225dbSLucas Stach gpu->buffer.size, 1832f9225dbSLucas Stach etnaviv_cmdbuf_get_va(&gpu->buffer)); 184a8c21a54SThe etnaviv authors 1856d7a20c0SLucas Stach spin_lock(&gpu->sched.job_list_lock); 1866d7a20c0SLucas Stach list_for_each_entry(s_job, &gpu->sched.ring_mirror_list, node) { 1876d7a20c0SLucas Stach submit = to_etnaviv_submit(s_job); 1882f9225dbSLucas Stach etnaviv_core_dump_mem(&iter, ETDUMP_BUF_CMD, 1892f9225dbSLucas Stach submit->cmdbuf.vaddr, submit->cmdbuf.size, 1902f9225dbSLucas Stach etnaviv_cmdbuf_get_va(&submit->cmdbuf)); 1916d7a20c0SLucas Stach } 1926d7a20c0SLucas Stach spin_unlock(&gpu->sched.job_list_lock); 193a8c21a54SThe etnaviv authors 194a8c21a54SThe etnaviv authors /* Reserve space for the bomap */ 195a8c21a54SThe etnaviv authors if (n_bomap_pages) { 196a8c21a54SThe etnaviv authors bomap_start = bomap = iter.data; 197a8c21a54SThe etnaviv authors memset(bomap, 0, sizeof(*bomap) * n_bomap_pages); 198a8c21a54SThe etnaviv authors etnaviv_core_dump_header(&iter, ETDUMP_BUF_BOMAP, 199a8c21a54SThe etnaviv authors bomap + n_bomap_pages); 200a8c21a54SThe etnaviv authors } else { 201a8c21a54SThe etnaviv authors /* Silence warning */ 202a8c21a54SThe etnaviv authors bomap_start = bomap = NULL; 203a8c21a54SThe etnaviv authors } 204a8c21a54SThe etnaviv authors 205a8c21a54SThe etnaviv authors list_for_each_entry(vram, &gpu->mmu->mappings, mmu_node) { 206a8c21a54SThe etnaviv authors struct page **pages; 207a8c21a54SThe etnaviv authors void *vaddr; 208a8c21a54SThe etnaviv authors 209a8c21a54SThe etnaviv authors if (vram->use == 0) 210a8c21a54SThe etnaviv authors continue; 211a8c21a54SThe etnaviv authors 212a8c21a54SThe etnaviv authors obj = vram->object; 213a8c21a54SThe etnaviv authors 214339073efSLucas Stach mutex_lock(&obj->lock); 215a8c21a54SThe etnaviv authors pages = etnaviv_gem_get_pages(obj); 216339073efSLucas Stach mutex_unlock(&obj->lock); 217a8c21a54SThe etnaviv authors if (pages) { 218a8c21a54SThe etnaviv authors int j; 219a8c21a54SThe etnaviv authors 220a8c21a54SThe etnaviv authors iter.hdr->data[0] = bomap - bomap_start; 221a8c21a54SThe etnaviv authors 222a8c21a54SThe etnaviv authors for (j = 0; j < obj->base.size >> PAGE_SHIFT; j++) 223a8c21a54SThe etnaviv authors *bomap++ = cpu_to_le64(page_to_phys(*pages++)); 224a8c21a54SThe etnaviv authors } 225a8c21a54SThe etnaviv authors 226a8c21a54SThe etnaviv authors iter.hdr->iova = cpu_to_le64(vram->iova); 227a8c21a54SThe etnaviv authors 228ce3088fdSLucas Stach vaddr = etnaviv_gem_vmap(&obj->base); 2299f07bb0dSLucas Stach if (vaddr) 230a8c21a54SThe etnaviv authors memcpy(iter.data, vaddr, obj->base.size); 231a8c21a54SThe etnaviv authors 232a8c21a54SThe etnaviv authors etnaviv_core_dump_header(&iter, ETDUMP_BUF_BO, iter.data + 233a8c21a54SThe etnaviv authors obj->base.size); 234a8c21a54SThe etnaviv authors } 235a8c21a54SThe etnaviv authors 236a8c21a54SThe etnaviv authors etnaviv_core_dump_header(&iter, ETDUMP_BUF_END, iter.data); 237a8c21a54SThe etnaviv authors 238a8c21a54SThe etnaviv authors dev_coredumpv(gpu->dev, iter.start, iter.data - iter.start, GFP_KERNEL); 239a8c21a54SThe etnaviv authors } 240