summaryrefslogtreecommitdiff
path: root/drivers/pci/ecam.c
blob: 419ec5f58f826907f0674c718f6ab01a1123e65b (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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Arch agnostic direct PCI config space access via
 * ECAM (Enhanced Configuration Access Mechanism)
 *
 * Per-architecture code takes care of the mappings, region validation and
 * accesses themselves.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/mutex.h>
#include <linux/rculist.h>
#include <linux/ecam.h>

#include <asm/io.h>

#define PREFIX "PCI ECAM: "

static DEFINE_MUTEX(pci_ecam_lock);

LIST_HEAD(pci_ecam_list);

#ifdef CONFIG_GENERIC_PCI_ECAM
extern struct acpi_mcfg_fixup __start_acpi_mcfg_fixups[];
extern struct acpi_mcfg_fixup __end_acpi_mcfg_fixups[];

int pci_ecam_read(unsigned int seg, unsigned int bus,
			  unsigned int devfn, int reg, int len, u32 *value)
{
	struct pci_ecam_region *cfg;
	char __iomem *addr;

	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
err:		*value = -1;
		return -EINVAL;
	}

	rcu_read_lock();
	cfg = pci_ecam_lookup(seg, bus);
	if (!cfg || !cfg->virt) {
		rcu_read_unlock();
		goto err;
	}

	if (cfg->read)
		(*cfg->read)(cfg, bus, devfn, reg, len, value);
	else {
		addr = cfg->virt + (PCI_ECAM_BUS_OFFSET(bus) | (devfn << 12));
		*value = pci_mmio_read(len, addr + reg);
	}
	rcu_read_unlock();

	return 0;
}

int pci_ecam_write(unsigned int seg, unsigned int bus,
			   unsigned int devfn, int reg, int len, u32 value)
{
	struct pci_ecam_region *cfg;
	char __iomem *addr;

	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
		return -EINVAL;

	rcu_read_lock();
	cfg = pci_ecam_lookup(seg, bus);
	if (!cfg || !cfg->virt) {
		rcu_read_unlock();
		return -EINVAL;
	}

	if (cfg->write)
		(*cfg->write)(cfg, bus, devfn, reg, len, value);
	else {
		addr = cfg->virt + (PCI_ECAM_BUS_OFFSET(bus) | (devfn << 12));
		pci_mmio_write(len, addr + reg, value);
	}
	rcu_read_unlock();

	return 0;
}

static void __iomem *pci_ecam_ioremap(struct pci_ecam_region *cfg)
{
	void __iomem *addr;
	u64 start, size;
	int num_buses;

	start = cfg->address + PCI_ECAM_BUS_OFFSET(cfg->start_bus);
	num_buses = cfg->end_bus - cfg->start_bus + 1;
	size = PCI_ECAM_BUS_OFFSET(num_buses);
	addr = ioremap_nocache(start, size);
	if (addr)
		addr -= PCI_ECAM_BUS_OFFSET(cfg->start_bus);
	return addr;
}

int __init pci_ecam_arch_init(void)
{
	struct pci_ecam_region *cfg;

	list_for_each_entry(cfg, &pci_ecam_list, list)
		if (pci_ecam_arch_map(cfg)) {
			pci_ecam_arch_free();
			return 0;
		}

	return 1;
}

void __init pci_ecam_arch_free(void)
{
	struct pci_ecam_region *cfg;

	list_for_each_entry(cfg, &pci_ecam_list, list)
		pci_ecam_arch_unmap(cfg);
}

int pci_ecam_arch_map(struct pci_ecam_region *cfg)
{
	cfg->virt = pci_ecam_ioremap(cfg);
	if (!cfg->virt) {
		pr_err(PREFIX "can't map ECAM at %pR\n", &cfg->res);
		return -ENOMEM;
	}

	return 0;
}

void pci_ecam_arch_unmap(struct pci_ecam_region *cfg)
{
	if (cfg && cfg->virt) {
		iounmap(cfg->virt + PCI_ECAM_BUS_OFFSET(cfg->start_bus));
		cfg->virt = NULL;
	}
}
#endif

static u32
pci_ecam_generic_read(int len, void __iomem *addr)
{
	u32 data = 0;

	switch (len) {
	case 1:
		data = readb(addr);
		break;
	case 2:
		data = readw(addr);
		break;
	case 4:
		data = readl(addr);
		break;
	}

	return data;
}

static void
pci_ecam_generic_write(int len, void __iomem *addr, u32 value)
{
	switch (len) {
	case 1:
		writeb(value, addr);
		break;
	case 2:
		writew(value, addr);
		break;
	case 4:
		writel(value, addr);
		break;
	}
}

static struct pci_ecam_mmio_ops pci_ecam_mmio_default = {
	.read = pci_ecam_generic_read,
	.write = pci_ecam_generic_write,
};

static struct pci_ecam_mmio_ops *pci_ecam_mmio = &pci_ecam_mmio_default;

void
pci_ecam_register_mmio(struct pci_ecam_mmio_ops *ops)
{
	pci_ecam_mmio = ops;
}

u32
pci_mmio_read(int len, void __iomem *addr)
{
	if (!pci_ecam_mmio) {
		pr_err("PCI config space has no accessors !");
		return 0;
	}

	return pci_ecam_mmio->read(len, addr);
}

void
pci_mmio_write(int len, void __iomem *addr, u32 value)
{
	if (!pci_ecam_mmio) {
		pr_err("PCI config space has no accessors !");
		return;
	}

	pci_ecam_mmio->write(len, addr, value);
}

static void __init pci_ecam_remove(struct pci_ecam_region *cfg)
{
	if (cfg->res.parent)
		release_resource(&cfg->res);
	list_del(&cfg->list);
	kfree(cfg);
}

void __init pci_ecam_free_all(void)
{
	struct pci_ecam_region *cfg, *tmp;

	pci_ecam_arch_free();
	list_for_each_entry_safe(cfg, tmp, &pci_ecam_list, list)
		pci_ecam_remove(cfg);
}

void pci_ecam_list_add_sorted(struct pci_ecam_region *new)
{
	struct pci_ecam_region *cfg;

	/* keep list sorted by segment and starting bus number */
	list_for_each_entry_rcu(cfg, &pci_ecam_list, list) {
		if (cfg->segment > new->segment ||
		    (cfg->segment == new->segment &&
		     cfg->start_bus >= new->start_bus)) {
			list_add_tail_rcu(&new->list, &cfg->list);
			return;
		}
	}
	list_add_tail_rcu(&new->list, &pci_ecam_list);
}

struct pci_ecam_region *pci_ecam_alloc(int segment, int start,
					    int end, u64 addr)
{
	struct pci_ecam_region *new;
	struct resource *res;

	if (addr == 0)
		return NULL;

	new = kzalloc(sizeof(*new), GFP_KERNEL);
	if (!new)
		return NULL;

	new->address = addr;
	new->segment = segment;
	new->start_bus = start;
	new->end_bus = end;

	res = &new->res;
	res->start = addr + PCI_ECAM_BUS_OFFSET(start);
	res->end = addr + PCI_ECAM_BUS_OFFSET(end + 1) - 1;
	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
	snprintf(new->name, PCI_ECAM_RESOURCE_NAME_LEN,
		 "PCI ECAM %04x [bus %02x-%02x]", segment, start, end);
	res->name = new->name;

	return new;
}

struct pci_ecam_region *pci_ecam_add(int segment, int start,
					  int end, u64 addr)
{
	struct pci_ecam_region *new;

	new = pci_ecam_alloc(segment, start, end, addr);
	if (new) {
		mutex_lock(&pci_ecam_lock);
		pci_ecam_list_add_sorted(new);
		mutex_unlock(&pci_ecam_lock);

		pr_info(PREFIX
		       "ECAM for domain %04x [bus %02x-%02x] at %pR "
		       "(base %#lx)\n",
		       segment, start, end, &new->res, (unsigned long)addr);
	}

	return new;
}

struct pci_ecam_region *pci_ecam_lookup(int segment, int bus)
{
	struct pci_ecam_region *cfg;

	list_for_each_entry_rcu(cfg, &pci_ecam_list, list)
		if (cfg->segment == segment &&
		    cfg->start_bus <= bus && bus <= cfg->end_bus)
			return cfg;

	return NULL;
}

/* Delete ECAM information for host bridges */
int pci_ecam_delete(u16 seg, u8 start, u8 end)
{
	struct pci_ecam_region *cfg;

	mutex_lock(&pci_ecam_lock);
	list_for_each_entry_rcu(cfg, &pci_ecam_list, list)
		if (cfg->segment == seg && cfg->start_bus == start &&
		    cfg->end_bus == end) {
			list_del_rcu(&cfg->list);
			synchronize_rcu();
			pci_ecam_arch_unmap(cfg);
			if (cfg->res.parent)
				release_resource(&cfg->res);
			mutex_unlock(&pci_ecam_lock);
			kfree(cfg);
			return 0;
		}
	mutex_unlock(&pci_ecam_lock);

	return -ENOENT;
}

int pci_ecam_inject(struct pci_ecam_region *cfg)
{
	struct pci_ecam_region *cfg_conflict;
	int err = 0;

	mutex_lock(&pci_ecam_lock);
	cfg_conflict = pci_ecam_lookup(cfg->segment, cfg->start_bus);
	if (cfg_conflict) {
		if (cfg_conflict->end_bus < cfg->end_bus)
			pr_info(FW_INFO "ECAM for "
				"domain %04x [bus %02x-%02x] "
				"only partially covers this bridge\n",
				cfg_conflict->segment, cfg_conflict->start_bus,
				cfg_conflict->end_bus);
		err = -EEXIST;
		goto out;
	}

	if (pci_ecam_arch_map(cfg)) {
		pr_warn("fail to map ECAM %pR.\n", &cfg->res);
		err = -ENOMEM;
		goto out;
	} else {
		pci_ecam_list_add_sorted(cfg);
		pr_info("ECAM at %pR (base %#lx)\n",
			&cfg->res, (unsigned long)cfg->address);

	}
out:
	mutex_unlock(&pci_ecam_lock);
	return err;
}