]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/pseries/rtas_pci.c
ZFS: MFV 2.0-rc1-ga00c61
[FreeBSD/FreeBSD.git] / sys / powerpc / pseries / rtas_pci.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/rman.h>
38
39 #include <dev/ofw/openfirm.h>
40 #include <dev/ofw/ofw_pci.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/ofw/ofwpci.h>
44
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47
48 #include <machine/bus.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/md_var.h>
51 #include <machine/pio.h>
52 #include <machine/resource.h>
53 #include <machine/rtas.h>
54
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57
58 #include <powerpc/pseries/plpar_iommu.h>
59
60 #include "pcib_if.h"
61 #include "iommu_if.h"
62
63 /*
64  * Device interface.
65  */
66 static int              rtaspci_probe(device_t);
67 static int              rtaspci_attach(device_t);
68
69 /*
70  * pcib interface.
71  */
72 static u_int32_t        rtaspci_read_config(device_t, u_int, u_int, u_int,
73                             u_int, int);
74 static void             rtaspci_write_config(device_t, u_int, u_int, u_int,
75                             u_int, u_int32_t, int);
76
77 /*
78  * Driver methods.
79  */
80 static device_method_t  rtaspci_methods[] = {
81         /* Device interface */
82         DEVMETHOD(device_probe,         rtaspci_probe),
83         DEVMETHOD(device_attach,        rtaspci_attach),
84
85         /* pcib interface */
86         DEVMETHOD(pcib_read_config,     rtaspci_read_config),
87         DEVMETHOD(pcib_write_config,    rtaspci_write_config),
88
89         DEVMETHOD_END
90 };
91
92 struct rtaspci_softc {
93         struct ofw_pci_softc    pci_sc;
94
95         struct ofw_pci_register sc_pcir;
96
97         cell_t                  read_pci_config, write_pci_config;
98         cell_t                  ex_read_pci_config, ex_write_pci_config;
99         int                     sc_extended_config;
100 };
101
102 static devclass_t       rtaspci_devclass;
103 DEFINE_CLASS_1(pcib, rtaspci_driver, rtaspci_methods,
104     sizeof(struct rtaspci_softc), ofw_pci_driver);
105 DRIVER_MODULE(rtaspci, ofwbus, rtaspci_driver, rtaspci_devclass, 0, 0);
106
107 static int
108 rtaspci_probe(device_t dev)
109 {
110         const char      *type;
111
112         if (!rtas_exists())
113                 return (ENXIO);
114
115         type = ofw_bus_get_type(dev);
116
117         if (OF_getproplen(ofw_bus_get_node(dev), "used-by-rtas") < 0)
118                 return (ENXIO);
119         if (type == NULL || strcmp(type, "pci") != 0)
120                 return (ENXIO);
121
122         device_set_desc(dev, "RTAS Host-PCI bridge");
123         return (BUS_PROBE_GENERIC);
124 }
125
126 static int
127 rtaspci_attach(device_t dev)
128 {
129         struct          rtaspci_softc *sc;
130
131         sc = device_get_softc(dev);
132
133         if (OF_getencprop(ofw_bus_get_node(dev), "reg", (pcell_t *)&sc->sc_pcir,
134             sizeof(sc->sc_pcir)) == -1)
135                 return (ENXIO);
136
137         sc->read_pci_config = rtas_token_lookup("read-pci-config");
138         sc->write_pci_config = rtas_token_lookup("write-pci-config");
139         sc->ex_read_pci_config = rtas_token_lookup("ibm,read-pci-config");
140         sc->ex_write_pci_config = rtas_token_lookup("ibm,write-pci-config");
141
142         sc->sc_extended_config = 0;
143         OF_getencprop(ofw_bus_get_node(dev), "ibm,pci-config-space-type",
144             &sc->sc_extended_config, sizeof(sc->sc_extended_config));
145
146         return (ofw_pci_attach(dev));
147 }
148
149 static uint32_t
150 rtaspci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
151     int width)
152 {
153         struct rtaspci_softc *sc;
154         uint32_t retval = 0xffffffff;
155         uint32_t config_addr;
156         int error, pcierror;
157
158         sc = device_get_softc(dev);
159         
160         config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) |
161             ((func & 0x7) << 8) | (reg & 0xff);
162         if (sc->sc_extended_config)
163                 config_addr |= (reg & 0xf00) << 16;
164                 
165         if (sc->ex_read_pci_config != -1)
166                 error = rtas_call_method(sc->ex_read_pci_config, 4, 2,
167                     config_addr, sc->sc_pcir.phys_hi,
168                     sc->sc_pcir.phys_mid, width, &pcierror, &retval);
169         else
170                 error = rtas_call_method(sc->read_pci_config, 2, 2,
171                     config_addr, width, &pcierror, &retval);
172
173         /* Sign-extend output */
174         switch (width) {
175         case 1:
176                 retval = (int32_t)(int8_t)(retval);
177                 break;
178         case 2:
179                 retval = (int32_t)(int16_t)(retval);
180                 break;
181         }
182         
183         if (error < 0 || pcierror != 0)
184                 retval = 0xffffffff;
185
186         return (retval);
187 }
188
189 static void
190 rtaspci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
191     u_int reg, uint32_t val, int width)
192 {
193         struct rtaspci_softc *sc;
194         uint32_t config_addr;
195         int pcierror;
196
197         sc = device_get_softc(dev);
198         
199         config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) |
200             ((func & 0x7) << 8) | (reg & 0xff);
201         if (sc->sc_extended_config)
202                 config_addr |= (reg & 0xf00) << 16;
203                 
204         if (sc->ex_write_pci_config != -1)
205                 rtas_call_method(sc->ex_write_pci_config, 5, 1, config_addr,
206                     sc->sc_pcir.phys_hi, sc->sc_pcir.phys_mid,
207                     width, val, &pcierror);
208         else
209                 rtas_call_method(sc->write_pci_config, 3, 1, config_addr,
210                     width, val, &pcierror);
211 }
212