1/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdarg.h>
32#include <pciaccess.h>
33#include <err.h>
34#include <unistd.h>
35
36#include "reg_dumper.h"
37#include "../i810_reg.h"
38
39#define INGTT(offset) (*(volatile uint32_t *)(gtt + (offset) / (KB(4) / 4)))
40
41int main(int argc, char **argv)
42{
43	I830Rec i830;
44	I830Ptr pI830 = &i830;
45	int start, aper_size;
46	unsigned char *gtt;
47
48	intel_i830rec_init(pI830);
49
50	if (!IS_I9XX(pI830)) {
51		printf("Unsupported chipset for gtt dumper\n");
52		exit(1);
53	}
54
55	if (IS_G4X(pI830) || IS_IGDNG(pI830))
56		gtt = (unsigned char *)(pI830->mmio + MB(2));
57	else if (IS_I965G(pI830))
58		gtt = (unsigned char *)(pI830->mmio + KB(512));
59	else {
60		/* 915/945 chips has GTT range in bar 3*/
61		int err = 0;
62		err = pci_device_map_range (pI830->PciInfo,
63				pI830->PciInfo->regions[3].base_addr,
64				pI830->PciInfo->regions[3].size,
65				PCI_DEV_MAP_FLAG_WRITABLE,
66				(void **)&gtt);
67		if (err != 0) {
68			fprintf(stderr, "mapping GTT bar failed\n");
69			exit(1);
70		}
71	}
72
73	aper_size = pI830->PciInfo->regions[2].size;
74
75	for (start = 0; start < aper_size; start += KB(4)) {
76		uint32_t start_pte = INGTT(start);
77		uint32_t end;
78		int constant_length = 0;
79		int linear_length = 0;
80
81		/* Check if it's a linear sequence */
82		for (end = start + KB(4); end < aper_size; end += KB(4)) {
83			uint32_t end_pte = INGTT(end);
84			if (end_pte == start_pte + (end - start))
85				linear_length++;
86			else
87				break;
88		}
89		if (linear_length > 0) {
90			printf("0x%08x - 0x%08x: linear from "
91			       "0x%08x to 0x%08x\n",
92			       start, end - KB(4),
93			       start_pte, start_pte + (end - start) - KB(4));
94			start = end - KB(4);
95			continue;
96		}
97
98		/* Check if it's a constant sequence */
99		for (end = start + KB(4); end < aper_size; end += KB(4)) {
100			uint32_t end_pte = INGTT(end);
101			if (end_pte == start_pte)
102				constant_length++;
103			else
104				break;
105		}
106		if (constant_length > 0) {
107			printf("0x%08x - 0x%08x: constant 0x%08x\n",
108			       start, end - KB(4),
109			       start_pte);
110			start = end - KB(4);
111			continue;
112		}
113
114		printf("0x%08x: 0x%08x\n", start, start_pte);
115	}
116
117	return 0;
118}
119