1f6ffbd4fSLucas Stach // SPDX-License-Identifier: GPL-2.0 2a8c21a54SThe etnaviv authors /* 3f6ffbd4fSLucas Stach * Copyright (C) 2015-2018 Etnaviv Project 4a8c21a54SThe etnaviv authors */ 5a8c21a54SThe etnaviv authors 6a8c21a54SThe etnaviv authors #include <linux/devcoredump.h> 72e737e52SLucas Stach #include <linux/moduleparam.h> 82e737e52SLucas Stach 9ea1f5729SLucas Stach #include "etnaviv_cmdbuf.h" 10a8c21a54SThe etnaviv authors #include "etnaviv_dump.h" 11a8c21a54SThe etnaviv authors #include "etnaviv_gem.h" 12a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h" 13a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h" 146d7a20c0SLucas Stach #include "etnaviv_sched.h" 15a8c21a54SThe etnaviv authors #include "state.xml.h" 16a8c21a54SThe etnaviv authors #include "state_hi.xml.h" 17a8c21a54SThe etnaviv authors 186d7a20c0SLucas Stach static bool etnaviv_dump_core = true; 196d7a20c0SLucas Stach module_param_named(dump_core, etnaviv_dump_core, bool, 0600); 206d7a20c0SLucas Stach 21a8c21a54SThe etnaviv authors struct core_dump_iterator { 22a8c21a54SThe etnaviv authors void *start; 23a8c21a54SThe etnaviv authors struct etnaviv_dump_object_header *hdr; 24a8c21a54SThe etnaviv authors void *data; 25a8c21a54SThe etnaviv authors }; 26a8c21a54SThe etnaviv authors 27a8c21a54SThe etnaviv authors static const unsigned short etnaviv_dump_registers[] = { 28a8c21a54SThe etnaviv authors VIVS_HI_AXI_STATUS, 29a8c21a54SThe etnaviv authors VIVS_HI_CLOCK_CONTROL, 30a8c21a54SThe etnaviv authors VIVS_HI_IDLE_STATE, 31a8c21a54SThe etnaviv authors VIVS_HI_AXI_CONFIG, 32a8c21a54SThe etnaviv authors VIVS_HI_INTR_ENBL, 33a8c21a54SThe etnaviv authors VIVS_HI_CHIP_IDENTITY, 34a8c21a54SThe etnaviv authors VIVS_HI_CHIP_FEATURE, 35a8c21a54SThe etnaviv authors VIVS_HI_CHIP_MODEL, 36a8c21a54SThe etnaviv authors VIVS_HI_CHIP_REV, 37a8c21a54SThe etnaviv authors VIVS_HI_CHIP_DATE, 38a8c21a54SThe etnaviv authors VIVS_HI_CHIP_TIME, 39a8c21a54SThe etnaviv authors VIVS_HI_CHIP_MINOR_FEATURE_0, 40a8c21a54SThe etnaviv authors VIVS_HI_CACHE_CONTROL, 41a8c21a54SThe etnaviv authors VIVS_HI_AXI_CONTROL, 42a8c21a54SThe etnaviv authors VIVS_PM_POWER_CONTROLS, 43a8c21a54SThe etnaviv authors VIVS_PM_MODULE_CONTROLS, 44a8c21a54SThe etnaviv authors VIVS_PM_MODULE_STATUS, 45a8c21a54SThe etnaviv authors VIVS_PM_PULSE_EATER, 46a8c21a54SThe etnaviv authors VIVS_MC_MMU_FE_PAGE_TABLE, 47a8c21a54SThe etnaviv authors VIVS_MC_MMU_TX_PAGE_TABLE, 48a8c21a54SThe etnaviv authors VIVS_MC_MMU_PE_PAGE_TABLE, 49a8c21a54SThe etnaviv authors VIVS_MC_MMU_PEZ_PAGE_TABLE, 50a8c21a54SThe etnaviv authors VIVS_MC_MMU_RA_PAGE_TABLE, 51a8c21a54SThe etnaviv authors VIVS_MC_DEBUG_MEMORY, 52a8c21a54SThe etnaviv authors VIVS_MC_MEMORY_BASE_ADDR_RA, 53a8c21a54SThe etnaviv authors VIVS_MC_MEMORY_BASE_ADDR_FE, 54a8c21a54SThe etnaviv authors VIVS_MC_MEMORY_BASE_ADDR_TX, 55a8c21a54SThe etnaviv authors VIVS_MC_MEMORY_BASE_ADDR_PEZ, 56a8c21a54SThe etnaviv authors VIVS_MC_MEMORY_BASE_ADDR_PE, 57a8c21a54SThe etnaviv authors VIVS_MC_MEMORY_TIMING_CONTROL, 58a8c21a54SThe etnaviv authors VIVS_MC_BUS_CONFIG, 59a8c21a54SThe etnaviv authors VIVS_FE_DMA_STATUS, 60a8c21a54SThe etnaviv authors VIVS_FE_DMA_DEBUG_STATE, 61a8c21a54SThe etnaviv authors VIVS_FE_DMA_ADDRESS, 62a8c21a54SThe etnaviv authors VIVS_FE_DMA_LOW, 63a8c21a54SThe etnaviv authors VIVS_FE_DMA_HIGH, 64a8c21a54SThe etnaviv authors VIVS_FE_AUTO_FLUSH, 65a8c21a54SThe etnaviv authors }; 66a8c21a54SThe etnaviv authors 67a8c21a54SThe etnaviv authors static void etnaviv_core_dump_header(struct core_dump_iterator *iter, 68a8c21a54SThe etnaviv authors u32 type, void *data_end) 69a8c21a54SThe etnaviv authors { 70a8c21a54SThe etnaviv authors struct etnaviv_dump_object_header *hdr = iter->hdr; 71a8c21a54SThe etnaviv authors 72a8c21a54SThe etnaviv authors hdr->magic = cpu_to_le32(ETDUMP_MAGIC); 73a8c21a54SThe etnaviv authors hdr->type = cpu_to_le32(type); 74a8c21a54SThe etnaviv authors hdr->file_offset = cpu_to_le32(iter->data - iter->start); 75a8c21a54SThe etnaviv authors hdr->file_size = cpu_to_le32(data_end - iter->data); 76a8c21a54SThe etnaviv authors 77a8c21a54SThe etnaviv authors iter->hdr++; 78a8c21a54SThe etnaviv authors iter->data += hdr->file_size; 79a8c21a54SThe etnaviv authors } 80a8c21a54SThe etnaviv authors 81a8c21a54SThe etnaviv authors static void etnaviv_core_dump_registers(struct core_dump_iterator *iter, 82a8c21a54SThe etnaviv authors struct etnaviv_gpu *gpu) 83a8c21a54SThe etnaviv authors { 84a8c21a54SThe etnaviv authors struct etnaviv_dump_registers *reg = iter->data; 85a8c21a54SThe etnaviv authors unsigned int i; 86a8c21a54SThe etnaviv authors 87a8c21a54SThe etnaviv authors for (i = 0; i < ARRAY_SIZE(etnaviv_dump_registers); i++, reg++) { 88a8c21a54SThe etnaviv authors reg->reg = etnaviv_dump_registers[i]; 89a8c21a54SThe etnaviv authors reg->value = gpu_read(gpu, etnaviv_dump_registers[i]); 90a8c21a54SThe etnaviv authors } 91a8c21a54SThe etnaviv authors 92a8c21a54SThe etnaviv authors etnaviv_core_dump_header(iter, ETDUMP_BUF_REG, reg); 93a8c21a54SThe etnaviv authors } 94a8c21a54SThe etnaviv authors 95a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mmu(struct core_dump_iterator *iter, 96*3001eeb7SLucas Stach struct etnaviv_iommu *mmu, size_t mmu_size) 97a8c21a54SThe etnaviv authors { 98*3001eeb7SLucas Stach etnaviv_iommu_dump(mmu, iter->data); 99a8c21a54SThe etnaviv authors 100a8c21a54SThe etnaviv authors etnaviv_core_dump_header(iter, ETDUMP_BUF_MMU, iter->data + mmu_size); 101a8c21a54SThe etnaviv authors } 102a8c21a54SThe etnaviv authors 103a8c21a54SThe etnaviv authors static void etnaviv_core_dump_mem(struct core_dump_iterator *iter, u32 type, 104a8c21a54SThe etnaviv authors void *ptr, size_t size, u64 iova) 105a8c21a54SThe etnaviv authors { 106a8c21a54SThe etnaviv authors memcpy(iter->data, ptr, size); 107a8c21a54SThe etnaviv authors 108a8c21a54SThe etnaviv authors iter->hdr->iova = cpu_to_le64(iova); 109a8c21a54SThe etnaviv authors 110a8c21a54SThe etnaviv authors etnaviv_core_dump_header(iter, type, iter->data + size); 111a8c21a54SThe etnaviv authors } 112a8c21a54SThe etnaviv authors 1139a1fdae5SLucas Stach void etnaviv_core_dump(struct etnaviv_gem_submit *submit) 114a8c21a54SThe etnaviv authors { 1159a1fdae5SLucas Stach struct etnaviv_gpu *gpu = submit->gpu; 116a8c21a54SThe etnaviv authors struct core_dump_iterator iter; 117a8c21a54SThe etnaviv authors struct etnaviv_gem_object *obj; 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; 1219a1fdae5SLucas Stach int i; 122a8c21a54SThe etnaviv authors 1236d7a20c0SLucas Stach /* Only catch the first event, or when manually re-armed */ 1246d7a20c0SLucas Stach if (!etnaviv_dump_core) 1256d7a20c0SLucas Stach return; 1266d7a20c0SLucas Stach etnaviv_dump_core = false; 1276d7a20c0SLucas Stach 1281396500dSLucas Stach mutex_lock(&gpu->mmu->lock); 1291396500dSLucas Stach 130a8c21a54SThe etnaviv authors mmu_size = etnaviv_iommu_dump_size(gpu->mmu); 131a8c21a54SThe etnaviv authors 1329a1fdae5SLucas Stach /* We always dump registers, mmu, ring, hanging cmdbuf and end marker */ 1339a1fdae5SLucas Stach n_obj = 5; 134a8c21a54SThe etnaviv authors n_bomap_pages = 0; 135a8c21a54SThe etnaviv authors file_size = ARRAY_SIZE(etnaviv_dump_registers) * 136a8c21a54SThe etnaviv authors sizeof(struct etnaviv_dump_registers) + 1379a1fdae5SLucas Stach mmu_size + gpu->buffer.size + submit->cmdbuf.size; 138a8c21a54SThe etnaviv authors 139a8c21a54SThe etnaviv authors /* Add in the active buffer objects */ 1409a1fdae5SLucas Stach for (i = 0; i < submit->nr_bos; i++) { 1419a1fdae5SLucas Stach obj = submit->bos[i].obj; 142a8c21a54SThe etnaviv authors file_size += obj->base.size; 143a8c21a54SThe etnaviv authors n_bomap_pages += obj->base.size >> PAGE_SHIFT; 144a8c21a54SThe etnaviv authors n_obj++; 145a8c21a54SThe etnaviv authors } 146a8c21a54SThe etnaviv authors 147a8c21a54SThe etnaviv authors /* If we have any buffer objects, add a bomap object */ 148a8c21a54SThe etnaviv authors if (n_bomap_pages) { 149a8c21a54SThe etnaviv authors file_size += n_bomap_pages * sizeof(__le64); 150a8c21a54SThe etnaviv authors n_obj++; 151a8c21a54SThe etnaviv authors } 152a8c21a54SThe etnaviv authors 153a8c21a54SThe etnaviv authors /* Add the size of the headers */ 154a8c21a54SThe etnaviv authors file_size += sizeof(*iter.hdr) * n_obj; 155a8c21a54SThe etnaviv authors 156a8c21a54SThe etnaviv authors /* Allocate the file in vmalloc memory, it's likely to be big */ 15719809c2dSMichal Hocko iter.start = __vmalloc(file_size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY, 15819809c2dSMichal Hocko PAGE_KERNEL); 159a8c21a54SThe etnaviv authors if (!iter.start) { 1601396500dSLucas Stach mutex_unlock(&gpu->mmu->lock); 161a8c21a54SThe etnaviv authors dev_warn(gpu->dev, "failed to allocate devcoredump file\n"); 162a8c21a54SThe etnaviv authors return; 163a8c21a54SThe etnaviv authors } 164a8c21a54SThe etnaviv authors 165a8c21a54SThe etnaviv authors /* Point the data member after the headers */ 166a8c21a54SThe etnaviv authors iter.hdr = iter.start; 167a8c21a54SThe etnaviv authors iter.data = &iter.hdr[n_obj]; 168a8c21a54SThe etnaviv authors 169a8c21a54SThe etnaviv authors memset(iter.hdr, 0, iter.data - iter.start); 170a8c21a54SThe etnaviv authors 171a8c21a54SThe etnaviv authors etnaviv_core_dump_registers(&iter, gpu); 172*3001eeb7SLucas Stach etnaviv_core_dump_mmu(&iter, gpu->mmu, mmu_size); 1732f9225dbSLucas Stach etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr, 1742f9225dbSLucas Stach gpu->buffer.size, 1752f9225dbSLucas Stach etnaviv_cmdbuf_get_va(&gpu->buffer)); 176a8c21a54SThe etnaviv authors 1772f9225dbSLucas Stach etnaviv_core_dump_mem(&iter, ETDUMP_BUF_CMD, 1782f9225dbSLucas Stach submit->cmdbuf.vaddr, submit->cmdbuf.size, 1792f9225dbSLucas Stach etnaviv_cmdbuf_get_va(&submit->cmdbuf)); 180a8c21a54SThe etnaviv authors 181a8c21a54SThe etnaviv authors /* Reserve space for the bomap */ 182a8c21a54SThe etnaviv authors if (n_bomap_pages) { 183a8c21a54SThe etnaviv authors bomap_start = bomap = iter.data; 184a8c21a54SThe etnaviv authors memset(bomap, 0, sizeof(*bomap) * n_bomap_pages); 185a8c21a54SThe etnaviv authors etnaviv_core_dump_header(&iter, ETDUMP_BUF_BOMAP, 186a8c21a54SThe etnaviv authors bomap + n_bomap_pages); 187a8c21a54SThe etnaviv authors } else { 188a8c21a54SThe etnaviv authors /* Silence warning */ 189a8c21a54SThe etnaviv authors bomap_start = bomap = NULL; 190a8c21a54SThe etnaviv authors } 191a8c21a54SThe etnaviv authors 1929a1fdae5SLucas Stach for (i = 0; i < submit->nr_bos; i++) { 1939a1fdae5SLucas Stach struct etnaviv_vram_mapping *vram; 194a8c21a54SThe etnaviv authors struct page **pages; 195a8c21a54SThe etnaviv authors void *vaddr; 196a8c21a54SThe etnaviv authors 1979a1fdae5SLucas Stach obj = submit->bos[i].obj; 1989a1fdae5SLucas Stach vram = submit->bos[i].mapping; 199a8c21a54SThe etnaviv authors 200339073efSLucas Stach mutex_lock(&obj->lock); 201a8c21a54SThe etnaviv authors pages = etnaviv_gem_get_pages(obj); 202339073efSLucas Stach mutex_unlock(&obj->lock); 203f8261c37SDan Carpenter if (!IS_ERR(pages)) { 204a8c21a54SThe etnaviv authors int j; 205a8c21a54SThe etnaviv authors 206a8c21a54SThe etnaviv authors iter.hdr->data[0] = bomap - bomap_start; 207a8c21a54SThe etnaviv authors 208a8c21a54SThe etnaviv authors for (j = 0; j < obj->base.size >> PAGE_SHIFT; j++) 209a8c21a54SThe etnaviv authors *bomap++ = cpu_to_le64(page_to_phys(*pages++)); 210a8c21a54SThe etnaviv authors } 211a8c21a54SThe etnaviv authors 212a8c21a54SThe etnaviv authors iter.hdr->iova = cpu_to_le64(vram->iova); 213a8c21a54SThe etnaviv authors 214ce3088fdSLucas Stach vaddr = etnaviv_gem_vmap(&obj->base); 2159f07bb0dSLucas Stach if (vaddr) 216a8c21a54SThe etnaviv authors memcpy(iter.data, vaddr, obj->base.size); 217a8c21a54SThe etnaviv authors 218a8c21a54SThe etnaviv authors etnaviv_core_dump_header(&iter, ETDUMP_BUF_BO, iter.data + 219a8c21a54SThe etnaviv authors obj->base.size); 220a8c21a54SThe etnaviv authors } 221a8c21a54SThe etnaviv authors 2221396500dSLucas Stach mutex_unlock(&gpu->mmu->lock); 2231396500dSLucas Stach 224a8c21a54SThe etnaviv authors etnaviv_core_dump_header(&iter, ETDUMP_BUF_END, iter.data); 225a8c21a54SThe etnaviv authors 226a8c21a54SThe etnaviv authors dev_coredumpv(gpu->dev, iter.start, iter.data - iter.start, GFP_KERNEL); 227a8c21a54SThe etnaviv authors } 228