summaryrefslogtreecommitdiff
path: root/ppgtt.c
blob: 388494091b191c3c4f4de8e7a1930593acaee18e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright © 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>

#include "main.h"

/* i915_gem_gtt.h */

/* GEN8 legacy style address is defined as a 3 level page table:
 * 31:30 | 29:21 | 20:12 |  11:0
 * PDPE  |  PDE  |  PTE  | offset
 * The difference as compared to normal x86 3 level page table is the PDPEs are
 * programmed via register.
 */
#define FOUR_PAGE_SIZE (4096 * 4)
#define PDE_COUNT_PER_PAGE 512
#define PTE_COUNT_PER_PDE 512
#define PDE_COUNT      (4*PDE_COUNT_PER_PAGE)
#define PDE_MAPPABLE_SIZE (512*4096)

static uint64_t pd_page[PDE_COUNT];
static uint64_t pt_page[PTE_COUNT_PER_PDE];

int print_ppgtt_mapping(uint32_t pd_phyaddr)
{
    uint32_t pde, pte;
    uint64_t *pd;
    
    /* first map page directory */
    pd_phyaddr &= 0xfffff000;

    lseek(devmem_fd, pd_phyaddr, SEEK_SET);
    if (read(devmem_fd, pd_page, FOUR_PAGE_SIZE) != FOUR_PAGE_SIZE) {
        perror("Read page directory content from /dev/mem failed\n");
        exit(-1);
    }
    
    pd = &pd_page[0];
    for (pde = 0; pde<PDE_COUNT; pde++) {
        uint32_t pde_value, pde_addr;
        uint32_t addr_start = pde * PDE_MAPPABLE_SIZE;
        uint32_t addr_end=(pde+1)* PDE_MAPPABLE_SIZE;
        uint64_t *pt;

        if ((pd[pde] & 1) == 0) /* not valid */
            continue;

        pde_value = (uint32_t) pd[pde];
        pde_addr = pde_value & 0xfffff000;
        printf("pde[%04d]=0x%08x (ppgtt offset range:0x%08x~0x%08x(%04dM~%04dM)\n",
               pde, pde_value, addr_start, addr_end, addr_start>>20, addr_end>>20);

        lseek(devmem_fd, pde_addr, SEEK_SET);
        if (read(devmem_fd, &pt_page[0], 4096) != 4096) {
            perror("Read page table content from /dev/mem failed\n");
            exit(-1);
        }
        pt = &pt_page[0];
        for (pte=0; pte<PTE_COUNT_PER_PDE; pte++) {
            uint32_t pte_value, pte_addr;
            uint32_t addr;
            
            if ((pt[pte] & 1) == 0)
                continue;
            
            if (pte > 0) {
                if (pt[pte] == pt[pte-1])
                    continue;
            }
            pte_value = (uint32_t) pt[pte];
            pte_addr = pte_value & 0xfffff000;
            addr = addr_start +  pte * 4096;
            printf("\t pte[%03d] = 0x%08x(page no.=%06d,  ppgtt addr 0x%x(%04dM+%04dK))\n",
                   pte, pte_value, pte_addr>>12, addr, addr>>20, (addr>>10)&0x3ff);
        }
    }
    
    return 0;
}

int get_ppgtt_page(uint32_t pd_phyaddr, uint32_t addr, unsigned char one_page[])
{
    uint32_t pdp_idx = (addr >> 30) & 0x3;
    uint32_t pd_idx = (addr >> 21) & 0x1ff;
    uint32_t pt_idx = (addr >> 12) & 0x1ff;
    uint32_t pd_index, pde_value, pde_addr, pte_value, pte_addr;

    /* first map page directory */
    pd_phyaddr &= 0xfffff000;

    /* get PDE entry */
    lseek(devmem_fd, pd_phyaddr, SEEK_SET);
    if (read(devmem_fd, &pd_page[0], FOUR_PAGE_SIZE) != FOUR_PAGE_SIZE) {
        perror("Read page directory content from /dev/mem failed\n");
        exit(-1);
    }

    /* get PTE entry */
    pd_index = pdp_idx * PDE_COUNT_PER_PAGE + pd_idx;
    pde_value = (uint32_t)(pd_page[pd_index] & 0xfffff000);
    pde_addr = pde_value & 0xfffff000;
    lseek(devmem_fd, pde_addr, SEEK_SET);
    if (read(devmem_fd, &pt_page[0], 4096) != 4096) {
        perror("Read page table content from /dev/mem failed\n");
        exit(-1);
    }
    pte_value  = (uint32_t)pt_page[pt_idx];
    /*
    printf("Batch buffer in PPGTT:(PD index %d(page %d, offset %d), entry 0x%08x)/(PT index %d, entry 0x%08x)\n",
           pd_index, pdp_idx, pd_idx, pde_value, pt_idx, pte_value);
    */
    pte_addr = pte_value & 0xfffff000;
    lseek(devmem_fd, pte_addr, SEEK_SET);
    if (read(devmem_fd, &one_page[0], 4096) != 4096) { /* read the content */
        perror("Read page table content from /dev/mem failed\n");
        exit(-1);
    }
    
    return 0;
}


int process_dumpppgtt(char *cmdhdr,unsigned int stat_addr)
{
    uint32_t ppgtt_addr;

    if (is_gen7) {
        printf("PPGTT is not supported on GEN7\n");
        return 0;
    }
    CHECK_STRICT_DEVMEM;
    
    ppgtt_addr = get_ppgtt(stat_addr);
    
    printf("Page directory address 0x%08x\n", ppgtt_addr);
    print_ppgtt_mapping(ppgtt_addr);

    return 0;
}