summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/pciecam.c
blob: da640f698aa4e522e1040b99faa500413f2b84b2 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* $OpenBSD: pciecam.c,v 1.3 2021/06/25 17:41:22 patrick Exp $ */
/*
 * Copyright (c) 2013,2017 Patrick Wildt <patrick@blueri.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/extent.h>
#include <sys/device.h>
#include <sys/evcount.h>
#include <sys/socket.h>
#include <sys/timeout.h>

#include <machine/intr.h>
#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/pci/pcivar.h>

#include <dev/ofw/fdt.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/ofw_misc.h>

/* Assembling ECAM Configuration Address */
#define PCIE_BUS_SHIFT			20
#define PCIE_SLOT_SHIFT			15
#define PCIE_FUNC_SHIFT			12
#define PCIE_BUS_MASK			0xff
#define PCIE_SLOT_MASK			0x1f
#define PCIE_FUNC_MASK			0x7
#define PCIE_REG_MASK			0xfff

#define PCIE_ADDR_OFFSET(bus, slot, func, reg)			\
	((((bus) & PCIE_BUS_MASK) << PCIE_BUS_SHIFT)	|	\
	(((slot) & PCIE_SLOT_MASK) << PCIE_SLOT_SHIFT)	|	\
	(((func) & PCIE_FUNC_MASK) << PCIE_FUNC_SHIFT)	|	\
	((reg) & PCIE_REG_MASK))

#define HREAD4(sc, reg)							\
	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val)						\
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define HSET4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))

struct pciecam_range {
	uint32_t			 flags;
	uint64_t			 pci_base;
	uint64_t			 phys_base;
	uint64_t			 size;
};

struct pciecam_softc {
	struct device			 sc_dev;
	int				 sc_node;
	bus_space_tag_t			 sc_iot;
	bus_space_handle_t		 sc_ioh;
	bus_dma_tag_t			 sc_dmat;

	int				 sc_dw_quirk;

	int				 sc_acells;
	int				 sc_scells;
	int				 sc_pacells;
	int				 sc_pscells;

	struct bus_space		 sc_bus;
	struct pciecam_range		*sc_pciranges;
	int				 sc_pcirangeslen;
	struct extent			*sc_ioex;
	struct extent			*sc_memex;
	char				 sc_ioex_name[32];
	char				 sc_memex_name[32];
	struct machine_pci_chipset	 sc_pc;
};

struct pciecam_intr_handle {
	struct machine_intr_handle	 pih_ih;
	bus_dma_tag_t			 pih_dmat;
	bus_dmamap_t			 pih_map;
};

int pciecam_match(struct device *, void *, void *);
void pciecam_attach(struct device *, struct device *, void *);
void pciecam_attach_hook(struct device *, struct device *, struct pcibus_attach_args *);
int pciecam_bus_maxdevs(void *, int);
pcitag_t pciecam_make_tag(void *, int, int, int);
void pciecam_decompose_tag(void *, pcitag_t, int *, int *, int *);
int pciecam_conf_size(void *, pcitag_t);
pcireg_t pciecam_conf_read(void *, pcitag_t, int);
void pciecam_conf_write(void *, pcitag_t, int, pcireg_t);
int pciecam_probe_device_hook(void *, struct pci_attach_args *);
int pciecam_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
const char *pciecam_intr_string(void *, pci_intr_handle_t);
void *pciecam_intr_establish(void *, pci_intr_handle_t, int,
    struct cpu_info *, int (*func)(void *), void *, char *);
void pciecam_intr_disestablish(void *, void *);
int pciecam_bs_map(bus_space_tag_t, bus_addr_t, bus_size_t, int, bus_space_handle_t *);
paddr_t pciecam_bs_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);

struct interrupt_controller pciecam_ic = {
	.ic_barrier = intr_barrier
};

struct cfattach pciecam_ca = {
	sizeof (struct pciecam_softc), pciecam_match, pciecam_attach
};

struct cfdriver pciecam_cd = {
	NULL, "pciecam", DV_DULL
};

int
pciecam_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return (OF_is_compatible(faa->fa_node, "pci-host-ecam-generic") ||
	    OF_is_compatible(faa->fa_node, "snps,dw-pcie-ecam"));
}

void
pciecam_attach(struct device *parent, struct device *self, void *aux)
{
	struct fdt_attach_args *faa = aux;
	struct pciecam_softc *sc = (struct pciecam_softc *) self;
	struct pcibus_attach_args pba;
	uint32_t *ranges;
	int i, j, nranges, rangeslen;

	sc->sc_node = faa->fa_node;
	sc->sc_iot = faa->fa_iot;
	sc->sc_dmat = faa->fa_dmat;

	if (OF_is_compatible(faa->fa_node, "snps,dw-pcie-ecam"))
		sc->sc_dw_quirk = 1;

	sc->sc_acells = OF_getpropint(sc->sc_node, "#address-cells",
	    faa->fa_acells);
	sc->sc_scells = OF_getpropint(sc->sc_node, "#size-cells",
	    faa->fa_scells);
	sc->sc_pacells = faa->fa_acells;
	sc->sc_pscells = faa->fa_scells;

	rangeslen = OF_getproplen(sc->sc_node, "ranges");
	if (rangeslen <= 0 || (rangeslen % sizeof(uint32_t)) ||
	     (rangeslen / sizeof(uint32_t)) % (sc->sc_acells +
	     sc->sc_pacells + sc->sc_scells))
		panic("pciecam_attach: invalid ranges property");

	ranges = malloc(rangeslen, M_TEMP, M_WAITOK);
	OF_getpropintarray(sc->sc_node, "ranges", ranges,
	    rangeslen);

	nranges = (rangeslen / sizeof(uint32_t)) /
	    (sc->sc_acells + sc->sc_pacells + sc->sc_scells);
	sc->sc_pciranges = mallocarray(nranges,
	    sizeof(struct pciecam_range), M_TEMP, M_WAITOK);
	sc->sc_pcirangeslen = nranges;

	for (i = 0, j = 0; i < nranges; i++) {
		sc->sc_pciranges[i].flags = ranges[j++];
		sc->sc_pciranges[i].pci_base = ranges[j++];
		if (sc->sc_acells - 1 == 2) {
			sc->sc_pciranges[i].pci_base <<= 32;
			sc->sc_pciranges[i].pci_base |= ranges[j++];
		}
		sc->sc_pciranges[i].phys_base = ranges[j++];
		if (sc->sc_pacells == 2) {
			sc->sc_pciranges[i].phys_base <<= 32;
			sc->sc_pciranges[i].phys_base |= ranges[j++];
		}
		sc->sc_pciranges[i].size = ranges[j++];
		if (sc->sc_scells == 2) {
			sc->sc_pciranges[i].size <<= 32;
			sc->sc_pciranges[i].size |= ranges[j++];
		}
	}

	free(ranges, M_TEMP, rangeslen);

	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
		panic("pciecam_attach: bus_space_map failed!");

	printf("\n");

	/*
	 * Map PCIe address space.
	 */
	snprintf(sc->sc_ioex_name, sizeof(sc->sc_ioex_name),
	    "%s pciio", sc->sc_dev.dv_xname);
	sc->sc_ioex = extent_create(sc->sc_ioex_name, 0, (u_long)-1L,
	    M_DEVBUF, NULL, 0, EX_NOWAIT | EX_FILLED);

	snprintf(sc->sc_memex_name, sizeof(sc->sc_memex_name),
	    "%s pcimem", sc->sc_dev.dv_xname);
	sc->sc_memex = extent_create(sc->sc_memex_name, 0, (u_long)-1L,
	    M_DEVBUF, NULL, 0, EX_NOWAIT | EX_FILLED);

	for (i = 0; i < nranges; i++) {
		if (sc->sc_pciranges[i].flags >> 24 == 0)
			continue;
		if (sc->sc_pciranges[i].flags >> 24 == 1)
			extent_free(sc->sc_ioex, sc->sc_pciranges[i].pci_base,
			    sc->sc_pciranges[i].size, EX_NOWAIT);
		else
			extent_free(sc->sc_memex, sc->sc_pciranges[i].pci_base,
			    sc->sc_pciranges[i].size, EX_NOWAIT);
	}

	memcpy(&sc->sc_bus, sc->sc_iot, sizeof(sc->sc_bus));
	sc->sc_bus.bus_private = sc;
	sc->sc_bus._space_map = pciecam_bs_map;
	sc->sc_bus._space_mmap = pciecam_bs_mmap;

	sc->sc_pc.pc_conf_v = sc;
	sc->sc_pc.pc_attach_hook = pciecam_attach_hook;
	sc->sc_pc.pc_bus_maxdevs = pciecam_bus_maxdevs;
	sc->sc_pc.pc_make_tag = pciecam_make_tag;
	sc->sc_pc.pc_decompose_tag = pciecam_decompose_tag;
	sc->sc_pc.pc_conf_size = pciecam_conf_size;
	sc->sc_pc.pc_conf_read = pciecam_conf_read;
	sc->sc_pc.pc_conf_write = pciecam_conf_write;
	sc->sc_pc.pc_probe_device_hook = pciecam_probe_device_hook;

	sc->sc_pc.pc_intr_v = sc;
	sc->sc_pc.pc_intr_map = pciecam_intr_map;
	sc->sc_pc.pc_intr_map_msi = _pci_intr_map_msi;
	sc->sc_pc.pc_intr_map_msix = _pci_intr_map_msix;
	sc->sc_pc.pc_intr_string = pciecam_intr_string;
	sc->sc_pc.pc_intr_establish = pciecam_intr_establish;
	sc->sc_pc.pc_intr_disestablish = pciecam_intr_disestablish;

	bzero(&pba, sizeof(pba));
	pba.pba_dmat = sc->sc_dmat;

	pba.pba_busname = "pci";
	pba.pba_iot = &sc->sc_bus;
	pba.pba_memt = &sc->sc_bus;
	pba.pba_ioex = sc->sc_ioex;
	pba.pba_memex = sc->sc_memex;
	pba.pba_pmemex = sc->sc_memex;
	pba.pba_pc = &sc->sc_pc;
	pba.pba_domain = pci_ndomains++;
	pba.pba_bus = 0;

	if (OF_getproplen(sc->sc_node, "msi-map") > 0 ||
	    OF_getproplen(sc->sc_node, "msi-parent") > 0)
		pba.pba_flags |= PCI_FLAGS_MSI_ENABLED;

	config_found(self, &pba, NULL);
}

void
pciecam_attach_hook(struct device *parent, struct device *self,
    struct pcibus_attach_args *pba)
{
}

int
pciecam_bus_maxdevs(void *v, int bus)
{
	struct pciecam_softc *sc = (struct pciecam_softc *)v;

	if (bus == 0 && sc->sc_dw_quirk)
		return 1;
	return 32;
}

#define BUS_SHIFT 24
#define DEVICE_SHIFT 19
#define FNC_SHIFT 16

pcitag_t
pciecam_make_tag(void *sc, int bus, int dev, int fnc)
{
	return (bus << BUS_SHIFT) | (dev << DEVICE_SHIFT) | (fnc << FNC_SHIFT);
}

void
pciecam_decompose_tag(void *sc, pcitag_t tag, int *busp, int *devp, int *fncp)
{
	if (busp != NULL)
		*busp = (tag >> BUS_SHIFT) & 0xff;
	if (devp != NULL)
		*devp = (tag >> DEVICE_SHIFT) & 0x1f;
	if (fncp != NULL)
		*fncp = (tag >> FNC_SHIFT) & 0x7;
}

int
pciecam_conf_size(void *sc, pcitag_t tag)
{
	return PCIE_CONFIG_SPACE_SIZE;
}

pcireg_t
pciecam_conf_read(void *v, pcitag_t tag, int reg)
{
	struct pciecam_softc *sc = (struct pciecam_softc *)v;
	int bus, dev, fn;

	pciecam_decompose_tag(sc, tag, &bus, &dev, &fn);

	return HREAD4(sc, PCIE_ADDR_OFFSET(bus, dev, fn, reg & ~0x3));
}

void
pciecam_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
{
	struct pciecam_softc *sc = (struct pciecam_softc *)v;
	int bus, dev, fn;

	pciecam_decompose_tag(sc, tag, &bus, &dev, &fn);

	HWRITE4(sc, PCIE_ADDR_OFFSET(bus, dev, fn, reg & ~0x3), data);
}

int
pciecam_probe_device_hook(void *v, struct pci_attach_args *pa)
{
	struct pciecam_softc *sc = (struct pciecam_softc *)v;
	uint16_t rid;
	int i;

	rid = pci_requester_id(pa->pa_pc, pa->pa_tag);
	pa->pa_dmat = iommu_device_map_pci(sc->sc_node, rid, pa->pa_dmat);

	for (i = 0; i < sc->sc_pcirangeslen; i++) {
		if (sc->sc_pciranges[i].flags >> 24 == 0)
			continue;
		iommu_reserve_region_pci(sc->sc_node, rid,
		    sc->sc_pciranges[i].pci_base, sc->sc_pciranges[i].size);
	}

	return 0;
}

int
pciecam_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
	ihp->ih_pc = pa->pa_pc;
	ihp->ih_tag = pa->pa_intrtag;
	ihp->ih_intrpin = pa->pa_intrpin;
	ihp->ih_type = PCI_INTX;

	return 0;
}

const char *
pciecam_intr_string(void *sc, pci_intr_handle_t ih)
{
	switch (ih.ih_type) {
	case PCI_MSI:
		return "msi";
	case PCI_MSIX:
		return "msix";
	}

	return "irq";
}

void *
pciecam_intr_establish(void *self, pci_intr_handle_t ih, int level,
    struct cpu_info *ci, int (*func)(void *), void *arg, char *name)
{
	struct pciecam_softc *sc = (struct pciecam_softc *)self;
	struct pciecam_intr_handle *pih;
	bus_dma_segment_t seg;
	void *cookie;

	KASSERT(ih.ih_type != PCI_NONE);

	if (ih.ih_type != PCI_INTX) {
		uint64_t addr, data;

		/* Assume hardware passes Requester ID as sideband data. */
		data = pci_requester_id(ih.ih_pc, ih.ih_tag);
		cookie = fdt_intr_establish_msi_cpu(sc->sc_node, &addr,
		    &data, level, ci, func, arg, (void *)name);
		if (cookie == NULL)
			return NULL;

		pih = malloc(sizeof(*pih), M_DEVBUF, M_WAITOK);
		pih->pih_ih.ih_ic = &pciecam_ic;
		pih->pih_ih.ih_ih = cookie;
		pih->pih_dmat = ih.ih_dmat;

		if (bus_dmamap_create(pih->pih_dmat, sizeof(uint32_t), 1,
		    sizeof(uint32_t), 0, BUS_DMA_WAITOK, &pih->pih_map)) {
			free(pih, M_DEVBUF, sizeof(*pih));
			fdt_intr_disestablish(cookie);
			return NULL;
		}

		memset(&seg, 0, sizeof(seg));
		seg.ds_addr = addr;
		seg.ds_len = sizeof(uint32_t);

		if (bus_dmamap_load_raw(pih->pih_dmat, pih->pih_map,
		    &seg, 1, sizeof(uint32_t), BUS_DMA_WAITOK)) {
			bus_dmamap_destroy(pih->pih_dmat, pih->pih_map);
			free(pih, M_DEVBUF, sizeof(*pih));
			fdt_intr_disestablish(cookie);
			return NULL;
		}

		addr = pih->pih_map->dm_segs[0].ds_addr;
		if (ih.ih_type == PCI_MSIX) {
			pci_msix_enable(ih.ih_pc, ih.ih_tag,
			    &sc->sc_bus, ih.ih_intrpin, addr, data);
		} else
			pci_msi_enable(ih.ih_pc, ih.ih_tag, addr, data);
	} else {
		int bus, dev, fn;
		uint32_t reg[4];

		pciecam_decompose_tag(sc, ih.ih_tag, &bus, &dev, &fn);

		reg[0] = bus << 16 | dev << 11 | fn << 8;
		reg[1] = reg[2] = 0;
		reg[3] = ih.ih_intrpin;

		cookie = fdt_intr_establish_imap_cpu(sc->sc_node, reg,
		    sizeof(reg), level, ci, func, arg, name);
		if (cookie == NULL)
			return NULL;

		pih = malloc(sizeof(*pih), M_DEVBUF, M_WAITOK);
		pih->pih_ih.ih_ic = &pciecam_ic;
		pih->pih_ih.ih_ih = cookie;
		pih->pih_dmat = NULL;
	}

	return pih;
}

void
pciecam_intr_disestablish(void *sc, void *cookie)
{
	struct pciecam_intr_handle *pih = cookie;

	fdt_intr_disestablish(pih->pih_ih.ih_ih);
	if (pih->pih_dmat) {
		bus_dmamap_unload(pih->pih_dmat, pih->pih_map);
		bus_dmamap_destroy(pih->pih_dmat, pih->pih_map);
	}
	free(pih, M_DEVBUF, sizeof(*pih));
}

/*
 * Translate memory address if needed.
 */
int
pciecam_bs_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size,
    int flag, bus_space_handle_t *bshp)
{
	struct pciecam_softc *sc = t->bus_private;
	uint64_t physbase, pcibase, psize;
	int i;

	for (i = 0; i < sc->sc_pcirangeslen; i++) {
		physbase = sc->sc_pciranges[i].phys_base;
		pcibase = sc->sc_pciranges[i].pci_base;
		psize = sc->sc_pciranges[i].size;

		if (bpa >= pcibase && bpa + size <= pcibase + psize)
			return bus_space_map(sc->sc_iot,
			    bpa - pcibase + physbase, size, flag, bshp);
	}

	return ENXIO;
}

paddr_t
pciecam_bs_mmap(bus_space_tag_t t, bus_addr_t bpa, off_t off,
    int prot, int flags)
{
	struct pciecam_softc *sc = t->bus_private;
	uint64_t physbase, pcibase, psize;
	int i;

	for (i = 0; i < sc->sc_pcirangeslen; i++) {
		physbase = sc->sc_pciranges[i].phys_base;
		pcibase = sc->sc_pciranges[i].pci_base;
		psize = sc->sc_pciranges[i].size;

		if (bpa >= pcibase && bpa < pcibase + psize)
			return bus_space_mmap(sc->sc_iot,
			    bpa - pcibase + physbase, off, prot, flags);
	}

	return -1;
}