]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/cavium/thunder_pcie.c
Merge ^/head r288100 through r288125.
[FreeBSD/FreeBSD.git] / sys / arm64 / cavium / thunder_pcie.c
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under
6  * the sponsorship of the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /* PCIe root complex driver for Cavium Thunder SOC */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/rman.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/endian.h>
43 #include <sys/cpuset.h>
44 #include <dev/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcib_private.h>
50 #include <machine/cpu.h>
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53
54 #include "thunder_pcie_common.h"
55
56 #include "pcib_if.h"
57
58 /* Assembling ECAM Configuration Address */
59 #define PCIE_BUS_SHIFT          20
60 #define PCIE_SLOT_SHIFT         15
61 #define PCIE_FUNC_SHIFT         12
62 #define PCIE_BUS_MASK           0xFF
63 #define PCIE_SLOT_MASK          0x1F
64 #define PCIE_FUNC_MASK          0x07
65 #define PCIE_REG_MASK           0xFFF
66
67 #define PCIE_ADDR_OFFSET(bus, slot, func, reg)                  \
68     ((((bus) & PCIE_BUS_MASK) << PCIE_BUS_SHIFT)        |       \
69     (((slot) & PCIE_SLOT_MASK) << PCIE_SLOT_SHIFT)      |       \
70     (((func) & PCIE_FUNC_MASK) << PCIE_FUNC_SHIFT)      |       \
71     ((reg) & PCIE_REG_MASK))
72
73 #define THUNDER_ECAM0_CFG_BASE  0x848000000000UL
74 #define THUNDER_ECAM1_CFG_BASE  0x849000000000UL
75 #define THUNDER_ECAM2_CFG_BASE  0x84a000000000UL
76 #define THUNDER_ECAM3_CFG_BASE  0x84b000000000UL
77 #define THUNDER_ECAM4_CFG_BASE  0x948000000000UL
78 #define THUNDER_ECAM5_CFG_BASE  0x949000000000UL
79 #define THUNDER_ECAM6_CFG_BASE  0x94a000000000UL
80 #define THUNDER_ECAM7_CFG_BASE  0x94b000000000UL
81
82 #define OFW_CELL_TO_UINT64(cell)        \
83     (((uint64_t)(*(cell)) << 32) | (uint64_t)(*((cell) + 1)))
84
85 #define SPACE_CODE_SHIFT        24
86 #define SPACE_CODE_MASK         0x3
87 #define SPACE_CODE_IO_SPACE     0x1
88 #define PROPS_CELL_SIZE         1
89 #define PCI_ADDR_CELL_SIZE      2
90
91 struct thunder_pcie_softc {
92         struct pcie_range       ranges[MAX_RANGES_TUPLES];
93         struct rman             mem_rman;
94         struct resource         *res;
95         int                     ecam;
96         device_t                dev;
97 };
98
99 /* Forward prototypes */
100 static struct resource *thunder_pcie_alloc_resource(device_t,
101     device_t, int, int *, u_long, u_long, u_long, u_int);
102 static int thunder_pcie_attach(device_t);
103 static int thunder_pcie_identify_pcib(device_t);
104 static int thunder_pcie_maxslots(device_t);
105 static int parse_pci_mem_ranges(struct thunder_pcie_softc *);
106 static int thunder_pcie_probe(device_t);
107 static uint32_t thunder_pcie_read_config(device_t, u_int, u_int, u_int, u_int,
108     int);
109 static int thunder_pcie_read_ivar(device_t, device_t, int, uintptr_t *);
110 static int thunder_pcie_release_resource(device_t, device_t, int, int,
111     struct resource *);
112 static void thunder_pcie_write_config(device_t, u_int, u_int,
113     u_int, u_int, uint32_t, int);
114 static int thunder_pcie_write_ivar(device_t, device_t, int, uintptr_t);
115
116 static int
117 thunder_pcie_probe(device_t dev)
118 {
119
120         if (!ofw_bus_status_okay(dev))
121                 return (ENXIO);
122
123         if (ofw_bus_is_compatible(dev, "cavium,thunder-pcie")) {
124                 device_set_desc(dev, "Cavium Integrated PCI/PCI-E Controller");
125                 return (BUS_PROBE_DEFAULT);
126         }
127
128         return (ENXIO);
129 }
130
131 static int
132 thunder_pcie_attach(device_t dev)
133 {
134         int rid;
135         struct thunder_pcie_softc *sc;
136         int error;
137         int tuple;
138         uint64_t base, size;
139
140         sc = device_get_softc(dev);
141         sc->dev = dev;
142
143         /* Identify pcib domain */
144         if (thunder_pcie_identify_pcib(dev))
145                 return (ENXIO);
146
147         rid = 0;
148         sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
149         if (sc->res == NULL) {
150                 device_printf(dev, "could not map memory.\n");
151                 return (ENXIO);
152         }
153
154         sc->mem_rman.rm_type = RMAN_ARRAY;
155         sc->mem_rman.rm_descr = "PCIe Memory";
156
157         /* Retrieve 'ranges' property from FDT */
158         if (bootverbose)
159                 device_printf(dev, "parsing FDT for ECAM%d:\n",
160                     sc->ecam);
161         if (parse_pci_mem_ranges(sc))
162                 return (ENXIO);
163
164         /* Initialize rman and allocate memory regions */
165         error = rman_init(&sc->mem_rman);
166         if (error) {
167                 device_printf(dev, "rman_init() failed. error = %d\n", error);
168                 return (error);
169         }
170
171         for (tuple = 0; tuple < MAX_RANGES_TUPLES; tuple++) {
172                 base = sc->ranges[tuple].phys_base;
173                 size = sc->ranges[tuple].size;
174                 if ((base == 0) || (size == 0))
175                         continue; /* empty range element */
176
177                 error = rman_manage_region(&sc->mem_rman, base, base + size - 1);
178                 if (error) {
179                         device_printf(dev, "rman_manage_region() failed. error = %d\n", error);
180                         rman_fini(&sc->mem_rman);
181                         return (error);
182                 }
183         }
184         device_add_child(dev, "pci", -1);
185
186         return (bus_generic_attach(dev));
187 }
188
189 static int
190 parse_pci_mem_ranges(struct thunder_pcie_softc *sc)
191 {
192         phandle_t node;
193         pcell_t pci_addr_cells, parent_addr_cells, size_cells;
194         pcell_t attributes;
195         pcell_t *ranges_buf, *cell_ptr;
196         int cells_count, tuples_count;
197         int tuple;
198         int rv;
199
200         node = ofw_bus_get_node(sc->dev);
201
202         /* Find address cells if present */
203         if (OF_getencprop(node, "#address-cells", &pci_addr_cells,
204             sizeof(pci_addr_cells)) < sizeof(pci_addr_cells))
205                 pci_addr_cells = 2;
206
207         /* Find size cells if present */
208         if (OF_getencprop(node, "#size-cells", &size_cells,
209             sizeof(size_cells)) < sizeof(size_cells))
210                 size_cells = 1;
211
212         /* Find parent address cells if present */
213         if (OF_getencprop(OF_parent(node), "#address-cells",
214             &parent_addr_cells, sizeof(parent_addr_cells)) < sizeof(parent_addr_cells))
215                 parent_addr_cells = 2;
216
217         /* Check if FDT format matches driver requirements */
218         if ((parent_addr_cells != 2) || (pci_addr_cells != 3) ||
219             (size_cells != 2)) {
220                 device_printf(sc->dev,
221                     "Unexpected number of address or size cells in FDT "
222                     " %d:%d:%d\n",
223                     parent_addr_cells, pci_addr_cells, size_cells);
224                 return (ENXIO);
225         }
226
227         cells_count = OF_getencprop_alloc(node, "ranges",
228             sizeof(pcell_t), (void **)&ranges_buf);
229         if (cells_count == -1) {
230                 device_printf(sc->dev, "Error parsing FDT 'ranges' property\n");
231                 return (ENXIO);
232         }
233
234         tuples_count = cells_count /
235             (pci_addr_cells + parent_addr_cells + size_cells);
236         if ((tuples_count > MAX_RANGES_TUPLES) ||
237             (tuples_count < MIN_RANGES_TUPLES)) {
238                 device_printf(sc->dev,
239                     "Unexpected number of 'ranges' tuples in FDT\n");
240                 rv = ENXIO;
241                 goto out;
242         }
243
244         cell_ptr = ranges_buf;
245
246         for (tuple = 0; tuple < tuples_count; tuple++) {
247                 /*
248                  * TUPLE FORMAT:
249                  *  attributes  - 32-bit attributes field
250                  *  PCI address - bus address combined of two cells in
251                  *                a following format:
252                  *                <ADDR MSB> <ADDR LSB>
253                  *  PA address  - physical address combined of two cells in
254                  *                a following format:
255                  *                <ADDR MSB> <ADDR LSB>
256                  *  size        - range size combined of two cells in
257                  *                a following format:
258                  *                <ADDR MSB> <ADDR LSB>
259                  */
260                 attributes = *cell_ptr;
261                 attributes = (attributes >> SPACE_CODE_SHIFT) & SPACE_CODE_MASK;
262                 if (attributes == SPACE_CODE_IO_SPACE) {
263                         /* Internal PCIe does not support IO space, ignore. */
264                         sc->ranges[tuple].phys_base = 0;
265                         sc->ranges[tuple].size = 0;
266                         cell_ptr +=
267                             (pci_addr_cells + parent_addr_cells + size_cells);
268                         continue;
269                 }
270                 cell_ptr += PROPS_CELL_SIZE;
271                 sc->ranges[tuple].pci_base = OFW_CELL_TO_UINT64(cell_ptr);
272                 cell_ptr += PCI_ADDR_CELL_SIZE;
273                 sc->ranges[tuple].phys_base = OFW_CELL_TO_UINT64(cell_ptr);
274                 cell_ptr += parent_addr_cells;
275                 sc->ranges[tuple].size = OFW_CELL_TO_UINT64(cell_ptr);
276                 cell_ptr += size_cells;
277
278                 if (bootverbose) {
279                         device_printf(sc->dev,
280                             "\tPCI addr: 0x%jx, CPU addr: 0x%jx, Size: 0x%jx\n",
281                             sc->ranges[tuple].pci_base,
282                             sc->ranges[tuple].phys_base,
283                             sc->ranges[tuple].size);
284                 }
285
286         }
287         for (; tuple < MAX_RANGES_TUPLES; tuple++) {
288                 /* zero-fill remaining tuples to mark empty elements in array */
289                 sc->ranges[tuple].phys_base = 0;
290                 sc->ranges[tuple].size = 0;
291         }
292
293         rv = 0;
294 out:
295         free(ranges_buf, M_OFWPROP);
296         return (rv);
297 }
298
299 static uint32_t
300 thunder_pcie_read_config(device_t dev, u_int bus, u_int slot,
301     u_int func, u_int reg, int bytes)
302 {
303         uint64_t offset;
304         uint32_t data;
305         struct thunder_pcie_softc *sc;
306         bus_space_tag_t t;
307         bus_space_handle_t h;
308
309         if ((bus > PCI_BUSMAX) || (slot > PCI_SLOTMAX) ||
310             (func > PCI_FUNCMAX) || (reg > PCIE_REGMAX))
311                 return (~0U);
312
313         sc = device_get_softc(dev);
314
315         offset = PCIE_ADDR_OFFSET(bus, slot, func, reg);
316         t = rman_get_bustag(sc->res);
317         h = rman_get_bushandle(sc->res);
318
319         switch (bytes) {
320         case 1:
321                 data = bus_space_read_1(t, h, offset);
322                 break;
323         case 2:
324                 data = le16toh(bus_space_read_2(t, h, offset));
325                 break;
326         case 4:
327                 data = le32toh(bus_space_read_4(t, h, offset));
328                 break;
329         default:
330                 return (~0U);
331         }
332
333         return (data);
334 }
335
336 static void
337 thunder_pcie_write_config(device_t dev, u_int bus, u_int slot,
338     u_int func, u_int reg, uint32_t val, int bytes)
339 {
340         uint64_t offset;
341         struct thunder_pcie_softc *sc;
342         bus_space_tag_t t;
343         bus_space_handle_t h;
344
345         if ((bus > PCI_BUSMAX) || (slot > PCI_SLOTMAX) ||
346             (func > PCI_FUNCMAX) || (reg > PCIE_REGMAX))
347                 return ;
348
349         sc = device_get_softc(dev);
350
351         offset = PCIE_ADDR_OFFSET(bus, slot, func, reg);
352         t = rman_get_bustag(sc->res);
353         h = rman_get_bushandle(sc->res);
354
355         switch (bytes) {
356         case 1:
357                 bus_space_write_1(t, h, offset, val);
358                 break;
359         case 2:
360                 bus_space_write_2(t, h, offset, htole16(val));
361                 break;
362         case 4:
363                 bus_space_write_4(t, h, offset, htole32(val));
364                 break;
365         default:
366                 return;
367         }
368
369 }
370
371 static int
372 thunder_pcie_maxslots(device_t dev)
373 {
374
375         /* max slots per bus acc. to standard */
376         return (PCI_SLOTMAX);
377 }
378
379 static int
380 thunder_pcie_read_ivar(device_t dev, device_t child, int index,
381     uintptr_t *result)
382 {
383         struct thunder_pcie_softc *sc;
384
385         sc = device_get_softc(dev);
386
387         if (index == PCIB_IVAR_BUS) {
388                 /* this pcib is always on bus 0 */
389                 *result = 0;
390                 return (0);
391         }
392         if (index == PCIB_IVAR_DOMAIN) {
393                 *result = sc->ecam;
394                 return (0);
395         }
396
397         return (ENOENT);
398 }
399
400 static int
401 thunder_pcie_write_ivar(device_t dev, device_t child, int index,
402     uintptr_t value)
403 {
404
405         return (ENOENT);
406 }
407
408 static int
409 thunder_pcie_release_resource(device_t dev, device_t child, int type, int rid,
410     struct resource *res)
411 {
412
413         if (type != SYS_RES_MEMORY)
414                 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
415                     type, rid, res));
416
417         return (rman_release_resource(res));
418 }
419
420 static struct resource *
421 thunder_pcie_alloc_resource(device_t dev, device_t child, int type, int *rid,
422     u_long start, u_long end, u_long count, u_int flags)
423 {
424         struct thunder_pcie_softc *sc = device_get_softc(dev);
425         struct rman *rm = NULL;
426         struct resource *res;
427
428         switch (type) {
429         case SYS_RES_IOPORT:
430                 goto fail;
431                 break;
432         case SYS_RES_MEMORY:
433                 rm = &sc->mem_rman;
434                 break;
435         default:
436                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
437                     type, rid, start, end, count, flags));
438         };
439
440         if ((start == 0UL) && (end == ~0UL)) {
441                 device_printf(dev,
442                     "Cannot allocate resource with unspecified range\n");
443                 goto fail;
444         }
445
446         /* Convert input BUS address to required PHYS */
447         if (range_addr_is_pci(sc->ranges, start, count) == 0)
448                 goto fail;
449         start = range_addr_pci_to_phys(sc->ranges, start);
450         end = start + count - 1;
451
452         if (bootverbose) {
453                 device_printf(dev,
454                     "rman_reserve_resource: start=%#lx, end=%#lx, count=%#lx\n",
455                     start, end, count);
456         }
457
458         res = rman_reserve_resource(rm, start, end, count, flags, child);
459         if (res == NULL)
460                 goto fail;
461
462         rman_set_rid(res, *rid);
463
464         if ((flags & RF_ACTIVE) != 0)
465                 if (bus_activate_resource(child, type, *rid, res)) {
466                         rman_release_resource(res);
467                         goto fail;
468                 }
469
470         return (res);
471
472 fail:
473         if (bootverbose) {
474                 device_printf(dev, "%s FAIL: type=%d, rid=%d, "
475                     "start=%016lx, end=%016lx, count=%016lx, flags=%x\n",
476                     __func__, type, *rid, start, end, count, flags);
477         }
478
479         return (NULL);
480 }
481
482 static int
483 thunder_pcie_identify_pcib(device_t dev)
484 {
485         struct thunder_pcie_softc *sc;
486         u_long start;
487
488         sc = device_get_softc(dev);
489         start = bus_get_resource_start(dev, SYS_RES_MEMORY, 0);
490
491         switch(start) {
492         case THUNDER_ECAM0_CFG_BASE:
493                 sc->ecam = 0;
494                 break;
495         case THUNDER_ECAM1_CFG_BASE:
496                 sc->ecam = 1;
497                 break;
498         case THUNDER_ECAM2_CFG_BASE:
499                 sc->ecam = 2;
500                 break;
501         case THUNDER_ECAM3_CFG_BASE:
502                 sc->ecam = 3;
503                 break;
504         case THUNDER_ECAM4_CFG_BASE:
505                 sc->ecam = 4;
506                 break;
507         case THUNDER_ECAM5_CFG_BASE:
508                 sc->ecam = 5;
509                 break;
510         case THUNDER_ECAM6_CFG_BASE:
511                 sc->ecam = 6;
512                 break;
513         case THUNDER_ECAM7_CFG_BASE:
514                 sc->ecam = 7;
515                 break;
516         default:
517                 device_printf(dev,
518                     "error: incorrect resource address=%#lx.\n", start);
519                 return (ENXIO);
520         }
521         return (0);
522 }
523
524 static device_method_t thunder_pcie_methods[] = {
525         DEVMETHOD(device_probe,                 thunder_pcie_probe),
526         DEVMETHOD(device_attach,                thunder_pcie_attach),
527         DEVMETHOD(pcib_maxslots,                thunder_pcie_maxslots),
528         DEVMETHOD(pcib_read_config,             thunder_pcie_read_config),
529         DEVMETHOD(pcib_write_config,            thunder_pcie_write_config),
530         DEVMETHOD(bus_read_ivar,                thunder_pcie_read_ivar),
531         DEVMETHOD(bus_write_ivar,               thunder_pcie_write_ivar),
532         DEVMETHOD(bus_alloc_resource,           thunder_pcie_alloc_resource),
533         DEVMETHOD(bus_release_resource,         thunder_pcie_release_resource),
534         DEVMETHOD(bus_activate_resource,        bus_generic_activate_resource),
535         DEVMETHOD(bus_deactivate_resource,      bus_generic_deactivate_resource),
536         DEVMETHOD(bus_setup_intr,               bus_generic_setup_intr),
537         DEVMETHOD(bus_teardown_intr,            bus_generic_teardown_intr),
538         DEVMETHOD(pcib_map_msi,                 thunder_common_map_msi),
539         DEVMETHOD(pcib_alloc_msix,              thunder_common_alloc_msix),
540         DEVMETHOD(pcib_release_msix,            thunder_common_release_msix),
541         DEVMETHOD(pcib_alloc_msi,               thunder_common_alloc_msi),
542         DEVMETHOD(pcib_release_msi,             thunder_common_release_msi),
543
544         DEVMETHOD_END
545 };
546
547 static driver_t thunder_pcie_driver = {
548         "pcib",
549         thunder_pcie_methods,
550         sizeof(struct thunder_pcie_softc),
551 };
552
553 static devclass_t thunder_pcie_devclass;
554
555 DRIVER_MODULE(thunder_pcib, simplebus, thunder_pcie_driver,
556 thunder_pcie_devclass, 0, 0);
557 DRIVER_MODULE(thunder_pcib, ofwbus, thunder_pcie_driver,
558 thunder_pcie_devclass, 0, 0);