summaryrefslogtreecommitdiffstats
path: root/src/drivers/bus/ecam.c
blob: 1d57bd2a0a9e2926d4a45e27b1cf0ad7649b4a08 (plain) (blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2022 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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 Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <errno.h>
#include <ipxe/uaccess.h>
#include <ipxe/ecam.h>

/** @file
 *
 * PCI Enhanced Configuration Access Mechanism (ECAM)
 *
 */

/** Cached mapped ECAM allocation */
static struct ecam_mapping ecam;

/**
 * Find lowest ECAM allocation not below a given PCI bus:dev.fn address
 *
 * @v busdevfn		PCI bus:dev.fn address
 * @v range		PCI device address range to fill in
 * @v alloc		ECAM allocation to fill in, or NULL
 * @ret rc		Return status code
 */
static int ecam_find ( uint32_t busdevfn, struct pci_range *range,
		       struct ecam_allocation *alloc ) {
	struct ecam_allocation tmp;
	unsigned int best = 0;
	unsigned int offset;
	unsigned int count;
	unsigned int index;
	userptr_t mcfg;
	uint32_t length;
	uint32_t start;

	/* Return empty range on error */
	range->count = 0;

	/* Locate MCFG table */
	mcfg = acpi_table ( ECAM_SIGNATURE, 0 );
	if ( ! mcfg ) {
		DBGC ( &ecam, "ECAM found no MCFG table\n" );
		return -ENOTSUP;
	}

	/* Get length of table */
	copy_from_user ( &length, mcfg,
			 offsetof ( struct ecam_table, acpi.length ),
			 sizeof ( length ) );

	/* Iterate over allocations */
	for ( offset = offsetof ( struct ecam_table, alloc ) ;
	      ( offset + sizeof ( tmp ) ) <= le32_to_cpu ( length ) ;
	      offset += sizeof ( tmp ) ) {

		/* Read allocation */
		copy_from_user ( &tmp, mcfg, offset, sizeof ( tmp ) );
		DBGC2 ( &ecam, "ECAM %04x:[%02x-%02x] has base %08llx\n",
			le16_to_cpu ( tmp.segment ), tmp.start, tmp.end,
			( ( unsigned long long ) le64_to_cpu ( tmp.base ) ) );
		start = PCI_BUSDEVFN ( le16_to_cpu ( tmp.segment ),
				       tmp.start, 0, 0 );
		count = PCI_BUSDEVFN ( 0, ( tmp.end - tmp.start + 1 ), 0, 0 );

		/* Check for a matching or new closest allocation */
		index = ( busdevfn - start );
		if ( ( index < count ) || ( index > best ) ) {
			if ( alloc )
				memcpy ( alloc, &tmp, sizeof ( *alloc ) );
			range->start = start;
			range->count = count;
			best = index;
		}

		/* Stop if this range contains the target bus:dev.fn address */
		if ( index < count )
			return 0;
	}

	return ( best ? 0 : -ENOENT );
}

/**
 * Find next PCI bus:dev.fn address range in system
 *
 * @v busdevfn		Starting PCI bus:dev.fn address
 * @v range		PCI bus:dev.fn address range to fill in
 */
static void ecam_discover ( uint32_t busdevfn, struct pci_range *range ) {

	/* Find new range, if any */
	ecam_find ( busdevfn, range, NULL );
}

/**
 * Access configuration space for PCI device
 *
 * @v pci		PCI device
 * @ret rc		Return status code
 */
static int ecam_access ( struct pci_device *pci ) {
	uint64_t base;
	size_t len;
	int rc;

	/* Reuse mapping if possible */
	if ( ( pci->busdevfn - ecam.range.start ) < ecam.range.count )
		return 0;

	/* Clear any existing mapping */
	if ( ecam.regs ) {
		iounmap ( ecam.regs );
		ecam.regs = NULL;
	}

	/* Find allocation for this PCI device */
	if ( ( rc = ecam_find ( pci->busdevfn, &ecam.range,
				&ecam.alloc ) ) != 0 ) {
		DBGC ( &ecam, "ECAM found no allocation for " PCI_FMT ": %s\n",
		       PCI_ARGS ( pci ), strerror ( rc ) );
		goto err_find;
	}
	if ( ecam.range.start > pci->busdevfn ) {
		DBGC ( &ecam, "ECAM found no allocation for " PCI_FMT "\n",
		       PCI_ARGS ( pci ) );
		goto err_find;
	}

	/* Map configuration space for this allocation */
	base = le64_to_cpu ( ecam.alloc.base );
	len = ( ecam.range.count * ECAM_SIZE );
	ecam.regs = ioremap ( base, len );
	if ( ! ecam.regs ) {
		DBGC ( &ecam, "ECAM %04x:[%02x-%02x] could not map "
		       "[%08llx,%08llx)\n", le16_to_cpu ( ecam.alloc.segment ),
		       ecam.alloc.start, ecam.alloc.end, base, ( base + len ) );
		rc = -ENODEV;
		goto err_ioremap;
	}

	/* Populate cached mapping */
	DBGC ( &ecam, "ECAM %04x:[%02x-%02x] mapped [%08llx,%08llx) -> %p\n",
	       le16_to_cpu ( ecam.alloc.segment ), ecam.alloc.start,
	       ecam.alloc.end, base, ( base + len ), ecam.regs );
	return 0;

	iounmap ( ecam.regs );
 err_ioremap:
 err_find:
	ecam.range.count = 0;
	return rc;
}

/**
 * Read from PCI configuration space
 *
 * @v pci	PCI device
 * @v location	Offset and length within PCI configuration space
 * @v value	Value read
 * @ret rc	Return status code
 */
int ecam_read ( struct pci_device *pci, unsigned int location, void *value ) {
	unsigned int where = ECAM_WHERE ( location );
	unsigned int len = ECAM_LEN ( location );
	unsigned int index;
	void *addr;
	int rc;

	/* Return all-ones on error */
	memset ( value, 0xff, len );

	/* Access configuration space */
	if ( ( rc = ecam_access ( pci ) ) != 0 )
		return rc;

	/* Read from address */
	index = ( pci->busdevfn - ecam.range.start );
	addr = ( ecam.regs + ( index * ECAM_SIZE ) + where );
	switch ( len ) {
	case 4:
		*( ( uint32_t *) value ) = readl ( addr );
		break;
	case 2:
		*( ( uint16_t *) value ) = readw ( addr );
		break;
	case 1:
		*( ( uint8_t *) value ) = readb ( addr );
		break;
	default:
		assert ( 0 );
	}

	return 0;
}

/**
 * Write to PCI configuration space
 *
 * @v pci	PCI device
 * @v location	Offset and length within PCI configuration space
 * @v value	Value to write
 * @ret rc	Return status code
 */
int ecam_write ( struct pci_device *pci, unsigned int location,
		 unsigned long value ) {
	unsigned int where = ECAM_WHERE ( location );
	unsigned int len = ECAM_LEN ( location );
	unsigned int index;
	void *addr;
	int rc;

	/* Access configuration space */
	if ( ( rc = ecam_access ( pci ) ) != 0 )
		return rc;

	/* Read from address */
	index = ( pci->busdevfn - ecam.range.start );
	addr = ( ecam.regs + ( index * ECAM_SIZE ) + where );
	switch ( len ) {
	case 4:
		writel ( value, addr );
		break;
	case 2:
		writew ( value, addr );
		break;
	case 1:
		writeb ( value, addr );
		break;
	default:
		assert ( 0 );
	}

	return 0;
}

PROVIDE_PCIAPI ( ecam, pci_discover, ecam_discover );
PROVIDE_PCIAPI_INLINE ( ecam, pci_read_config_byte );
PROVIDE_PCIAPI_INLINE ( ecam, pci_read_config_word );
PROVIDE_PCIAPI_INLINE ( ecam, pci_read_config_dword );
PROVIDE_PCIAPI_INLINE ( ecam, pci_write_config_byte );
PROVIDE_PCIAPI_INLINE ( ecam, pci_write_config_word );
PROVIDE_PCIAPI_INLINE ( ecam, pci_write_config_dword );
PROVIDE_PCIAPI_INLINE ( ecam, pci_ioremap );

struct pci_api ecam_api = PCIAPI_RUNTIME ( ecam );