summaryrefslogtreecommitdiff
path: root/src/radeon_pci.c
blob: bdad3a8ab1d711d7f91c9f0418de1db4f79daf21 (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
/*
 * Copyright © 2009 Jerome Glisse <glisse@freedesktop.org>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "xf86drm.h"
#include "radeon_priv.h"

int radeon_probe(struct radeon *radeon)
{
	struct pci_slot_match match;
	struct pci_device_iterator *iter;
	struct pci_device *device;
	int i = 0;
	int r = 0;

	radeon->bios = (uint8_t*)malloc(256*1024);
	if (radeon->bios == NULL) {
		fprintf(stderr, "Failed to allocate bios copy\n");
		return -1;
	}
	if (pci_system_init()) {
		fprintf(stderr, "Failed to initialize libpciaccess\n");
		return -1;
	}
	match.domain = PCI_MATCH_ANY;
	match.bus = PCI_MATCH_ANY;
	match.dev = PCI_MATCH_ANY;
	match.func = 0;
	match.match_data = 0;
	iter = pci_slot_match_iterator_create(&match);
	while ((device = pci_device_next(iter))) {
		pci_device_probe(device);
		/* ATI vendor id 0x1002 and we are looking for a video
		 * card class 0x03 with subclass 0x00 (VGA)
		 */
		if (device->vendor_id == 0x1002) {
			radeon->family = radeon_family_from_device(device->device_id);
			radeon->device = device->device_id;
			if (radeon->family != CHIP_UNKNOWN) {
				for (i = 0; i < 6; i++) {
					if (device->regions[i].size == 64 * 1024) {
						radeon->mmio_base = device->regions[i].base_addr;
						radeon->mmio_size = device->regions[i].size;
					}
				}
				radeon->dev = device;
				break;
			}
		}
	}
	pci_iterator_destroy(iter);
	if (!radeon->dev) {
		fprintf(stderr, "Cannot find radeon device\n");
		return -1;
	}
	if (pci_device_map_range(radeon->dev, radeon->mmio_base, radeon->mmio_size,
				PCI_DEV_MAP_FLAG_WRITABLE,
				(void**)&radeon->mmio)) {
		fprintf(stderr, "Mapping mmio failed (are you root ?)\n");
		return -1;
	}
	r = pci_device_read_rom(radeon->dev, radeon->bios);
	if (r) {
		fprintf(stderr, "Cannot read rom %d\n", r);
		return -1;
	}
	return 0;
}

void radeon_close(struct radeon *radeon)
{
	if (pci_device_unmap_range(radeon->dev, radeon->mmio, radeon->mmio_size)) {
		fprintf(stderr, "Unmapping mmio failed\n");
	}
}

u32 radeon_mmio_rd32(struct radeon *radeon, u32 offset)
{
	u32 value;

	if (radeon->mmio == NULL) {
		fprintf(stderr, "(%s:%d) internal error\n", __func__, __LINE__);
		exit(1);
	}
	value = *(u32 * volatile)(radeon->mmio + offset);
	return value;
}

void radeon_mmio_wr32(struct radeon *radeon, u32 offset, u32 value)
{
	if (radeon->mmio == NULL) {
		fprintf(stderr, "(%s:%d) internal error\n", __func__, __LINE__);
		exit(1);
	}
	*(u32 * volatile)(radeon->mmio + offset) = value;
}

void radeon_mmio_wr8(struct radeon *radeon, u32 offset, u8 value)
{
	if (radeon->mmio == NULL) {
		fprintf(stderr, "(%s:%d) internal error\n", __func__, __LINE__);
		exit(1);
	}
	*(u8 * volatile)(radeon->mmio + offset) = value;
}

struct radeon *radeon_new_from_pci(void)
{
	struct radeon *radeon;
	int r;

	radeon = calloc(1, sizeof(*radeon));
	if (radeon == NULL)
		return NULL;
	radeon->refcount = 1;
	if (radeon_probe(radeon)) {
		return radeon_decref(radeon);
	}
	return radeon;
}