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