Path: blob/main/sys/arm64/cavium/thunder_pcie_common.c
39478 views
/*-1* Copyright (c) 2015 The FreeBSD Foundation2*3* This software was developed by Semihalf under4* the sponsorship of the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/* Common PCIe functions for Cavium Thunder SOC */2930#include <sys/cdefs.h>31#include "opt_platform.h"3233#include <sys/param.h>34#include <sys/systm.h>35#include <sys/kernel.h>36#include <sys/malloc.h>37#include <sys/bus.h>38#include <sys/rman.h>3940#include <machine/bus.h>41#include <machine/cpu.h>42#include <machine/intr.h>4344#ifdef FDT45#include <dev/ofw/openfirm.h>46#include <dev/ofw/ofw_bus.h>47#include <dev/ofw/ofw_bus_subr.h>48#include <dev/ofw/ofw_pci.h>49#endif5051#include <sys/pciio.h>52#include <dev/pci/pcireg.h>53#include <dev/pci/pcivar.h>54#include <dev/pci/pci_private.h>55#include <dev/pci/pcib_private.h>56#include <dev/pci/pci_host_generic.h>57#ifdef FDT58#include <dev/pci/pci_host_generic_fdt.h>59#endif6061#include "thunder_pcie_common.h"6263MALLOC_DEFINE(M_THUNDER_PCIE, "Thunder PCIe driver", "Thunder PCIe driver memory");6465#define THUNDER_CFG_BASE_TO_ECAM(x) ((((x) >> 36UL) & 0x3) | (((x) >> 42UL) & 0x4))6667uint32_t68range_addr_is_pci(struct pcie_range *ranges, uint64_t addr, uint64_t size)69{70struct pcie_range *r;71int tuple;7273for (tuple = 0; tuple < MAX_RANGES_TUPLES; tuple++) {74r = &ranges[tuple];75if (addr >= r->pci_base &&76addr < (r->pci_base + r->size) &&77size < r->size) {78/* Address is within PCI range */79return (1);80}81}8283/* Address is outside PCI range */84return (0);85}8687uint32_t88range_addr_is_phys(struct pcie_range *ranges, uint64_t addr, uint64_t size)89{90struct pcie_range *r;91int tuple;9293for (tuple = 0; tuple < MAX_RANGES_TUPLES; tuple++) {94r = &ranges[tuple];95if (addr >= r->phys_base &&96addr < (r->phys_base + r->size) &&97size < r->size) {98/* Address is within Physical range */99return (1);100}101}102103/* Address is outside Physical range */104return (0);105}106107uint64_t108range_addr_phys_to_pci(struct pcie_range *ranges, uint64_t phys_addr)109{110struct pcie_range *r;111uint64_t offset;112int tuple;113114/* Find physical address corresponding to given bus address */115for (tuple = 0; tuple < MAX_RANGES_TUPLES; tuple++) {116r = &ranges[tuple];117if (phys_addr >= r->phys_base &&118phys_addr < (r->phys_base + r->size)) {119/* Given phys addr is in this range.120* Translate phys addr to bus addr.121*/122offset = phys_addr - r->phys_base;123return (r->pci_base + offset);124}125}126return (0);127}128129uint64_t130range_addr_pci_to_phys(struct pcie_range *ranges, uint64_t pci_addr)131{132struct pcie_range *r;133uint64_t offset;134int tuple;135136/* Find physical address corresponding to given bus address */137for (tuple = 0; tuple < MAX_RANGES_TUPLES; tuple++) {138r = &ranges[tuple];139if (pci_addr >= r->pci_base &&140pci_addr < (r->pci_base + r->size)) {141/* Given pci addr is in this range.142* Translate bus addr to phys addr.143*/144offset = pci_addr - r->pci_base;145return (r->phys_base + offset);146}147}148return (0);149}150151int152thunder_pcie_identify_ecam(device_t dev, int *ecam)153{154rman_res_t start;155156/* Check if we're running on Cavium ThunderX */157if (!CPU_MATCH(CPU_IMPL_MASK | CPU_PART_MASK,158CPU_IMPL_CAVIUM, CPU_PART_THUNDERX, 0, 0))159return (EINVAL);160161start = bus_get_resource_start(dev, SYS_RES_MEMORY, 0);162*ecam = THUNDER_CFG_BASE_TO_ECAM(start);163164device_printf(dev, "ThunderX quirk, setting ECAM to %d\n", *ecam);165166return (0);167}168169#ifdef THUNDERX_PASS_1_1_ERRATA170struct resource *171thunder_pcie_alloc_resource(device_t dev, device_t child, int type, int *rid,172rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)173{174pci_addr_t map, testval;175176/*177* If Enhanced Allocation is not used, we can't allocate any random178* range. All internal devices have hardcoded place where they can179* be located within PCI address space. Fortunately, we can read180* this value from BAR.181*/182if (((type == SYS_RES_IOPORT) || (type == SYS_RES_MEMORY)) &&183RMAN_IS_DEFAULT_RANGE(start, end)) {184/* Read BAR manually to get resource address and size */185pci_read_bar(child, *rid, &map, &testval, NULL);186187/* Mask the information bits */188if (PCI_BAR_MEM(map))189map &= PCIM_BAR_MEM_BASE;190else191map &= PCIM_BAR_IO_BASE;192193if (PCI_BAR_MEM(testval))194testval &= PCIM_BAR_MEM_BASE;195else196testval &= PCIM_BAR_IO_BASE;197198start = map;199end = start + count - 1;200}201202return (pci_host_generic_core_alloc_resource(dev, child, type, rid,203start, end, count, flags));204}205#endif206207208