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/** @file util.c
29 *
30 * Utility functions for the various tools in the reg_dumper directory.
31 */
32
33#include <err.h>
34#include <pciaccess.h>
35
36#include "reg_dumper.h"
37
38/**
39 * Sets up the pI830 for use by common.h-style macros, particularly
40 * INREG/OUTERG.
41 */
42void intel_i830rec_init(I830Ptr pI830)
43{
44    struct pci_device *dev;
45    int err, mmio_bar;
46    void *mmio;
47
48    err = pci_system_init();
49    if (err != 0) {
50	fprintf(stderr, "Couldn't initialize PCI system: %s\n", strerror(err));
51	exit(1);
52    }
53
54    /* Grab the graphics card */
55    dev = pci_device_find_by_slot(0, 0, 2, 0);
56    if (dev == NULL)
57	errx(1, "Couldn't find graphics card");
58
59    err = pci_device_probe(dev);
60    if (err != 0) {
61	fprintf(stderr, "Couldn't probe graphics card: %s\n", strerror(err));
62	exit(1);
63    }
64
65    if (dev->vendor_id != 0x8086)
66	errx(1, "Graphics card is non-intel");
67
68    pI830->PciInfo = dev;
69
70    mmio_bar = IS_I9XX(pI830) ? 0 : 1;
71
72    err = pci_device_map_range (dev,
73				dev->regions[mmio_bar].base_addr,
74				dev->regions[mmio_bar].size,
75				PCI_DEV_MAP_FLAG_WRITABLE,
76				&mmio);
77
78    if (err != 0) {
79	fprintf(stderr, "Couldn't map MMIO region: %s\n", strerror(err));
80	exit(1);
81    }
82    pI830->mmio = mmio;
83}
84