pci_usrreq.c revision 1.2.2.2 1 1.2.2.2 nathanw /* $NetBSD: pci_usrreq.c,v 1.2.2.2 2001/09/21 22:35:59 nathanw Exp $ */
2 1.2.2.2 nathanw
3 1.2.2.2 nathanw /*
4 1.2.2.2 nathanw * Copyright 2001 Wasabi Systems, Inc.
5 1.2.2.2 nathanw * All rights reserved.
6 1.2.2.2 nathanw *
7 1.2.2.2 nathanw * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.2.2.2 nathanw *
9 1.2.2.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.2.2.2 nathanw * modification, are permitted provided that the following conditions
11 1.2.2.2 nathanw * are met:
12 1.2.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.2.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.2.2.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.2.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.2.2.2 nathanw * must display the following acknowledgement:
19 1.2.2.2 nathanw * This product includes software developed for the NetBSD Project by
20 1.2.2.2 nathanw * Wasabi Systems, Inc.
21 1.2.2.2 nathanw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.2.2.2 nathanw * or promote products derived from this software without specific prior
23 1.2.2.2 nathanw * written permission.
24 1.2.2.2 nathanw *
25 1.2.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.2.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.2.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
36 1.2.2.2 nathanw */
37 1.2.2.2 nathanw
38 1.2.2.2 nathanw /*
39 1.2.2.2 nathanw * User -> kernel interface for PCI bus access.
40 1.2.2.2 nathanw */
41 1.2.2.2 nathanw
42 1.2.2.2 nathanw #include <sys/param.h>
43 1.2.2.2 nathanw #include <sys/conf.h>
44 1.2.2.2 nathanw #include <sys/device.h>
45 1.2.2.2 nathanw #include <sys/ioctl.h>
46 1.2.2.2 nathanw #include <sys/proc.h>
47 1.2.2.2 nathanw #include <sys/systm.h>
48 1.2.2.2 nathanw #include <sys/errno.h>
49 1.2.2.2 nathanw #include <sys/fcntl.h>
50 1.2.2.2 nathanw
51 1.2.2.2 nathanw #include <dev/pci/pcireg.h>
52 1.2.2.2 nathanw #include <dev/pci/pcivar.h>
53 1.2.2.2 nathanw #include <dev/pci/pciio.h>
54 1.2.2.2 nathanw
55 1.2.2.2 nathanw cdev_decl(pci);
56 1.2.2.2 nathanw
57 1.2.2.2 nathanw int
58 1.2.2.2 nathanw pciopen(dev_t dev, int flags, int mode, struct proc *p)
59 1.2.2.2 nathanw {
60 1.2.2.2 nathanw struct pci_softc *sc;
61 1.2.2.2 nathanw int unit;
62 1.2.2.2 nathanw
63 1.2.2.2 nathanw unit = minor(dev);
64 1.2.2.2 nathanw sc = device_lookup(&pci_cd, unit);
65 1.2.2.2 nathanw if (sc == NULL)
66 1.2.2.2 nathanw return (ENXIO);
67 1.2.2.2 nathanw
68 1.2.2.2 nathanw return (0);
69 1.2.2.2 nathanw }
70 1.2.2.2 nathanw
71 1.2.2.2 nathanw int
72 1.2.2.2 nathanw pciclose(dev_t dev, int flags, int mode, struct proc *p)
73 1.2.2.2 nathanw {
74 1.2.2.2 nathanw
75 1.2.2.2 nathanw return (0);
76 1.2.2.2 nathanw }
77 1.2.2.2 nathanw
78 1.2.2.2 nathanw int
79 1.2.2.2 nathanw pciioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
80 1.2.2.2 nathanw {
81 1.2.2.2 nathanw struct pci_softc *sc = device_lookup(&pci_cd, minor(dev));
82 1.2.2.2 nathanw struct pciio_bdf_cfgreg *bdfr = (void *) data;
83 1.2.2.2 nathanw struct pciio_businfo *binfo = (void *) data;
84 1.2.2.2 nathanw pcitag_t tag;
85 1.2.2.2 nathanw
86 1.2.2.2 nathanw switch (cmd) {
87 1.2.2.2 nathanw case PCI_IOC_BDF_CFGREAD:
88 1.2.2.2 nathanw case PCI_IOC_BDF_CFGWRITE:
89 1.2.2.2 nathanw if (bdfr->bus > 255 || bdfr->device >= sc->sc_maxndevs ||
90 1.2.2.2 nathanw bdfr->function > 7)
91 1.2.2.2 nathanw return (EINVAL);
92 1.2.2.2 nathanw tag = pci_make_tag(sc->sc_pc, bdfr->bus, bdfr->device,
93 1.2.2.2 nathanw bdfr->function);
94 1.2.2.2 nathanw if (cmd == PCI_IOC_BDF_CFGREAD)
95 1.2.2.2 nathanw bdfr->cfgreg.val = pci_conf_read(sc->sc_pc, tag,
96 1.2.2.2 nathanw bdfr->cfgreg.reg);
97 1.2.2.2 nathanw else {
98 1.2.2.2 nathanw if ((flag & FWRITE) == 0)
99 1.2.2.2 nathanw return (EBADF);
100 1.2.2.2 nathanw pci_conf_write(sc->sc_pc, tag, bdfr->cfgreg.reg,
101 1.2.2.2 nathanw bdfr->cfgreg.val);
102 1.2.2.2 nathanw }
103 1.2.2.2 nathanw break;
104 1.2.2.2 nathanw
105 1.2.2.2 nathanw case PCI_IOC_BUSINFO:
106 1.2.2.2 nathanw binfo->busno = sc->sc_bus;
107 1.2.2.2 nathanw binfo->maxdevs = sc->sc_maxndevs;
108 1.2.2.2 nathanw break;
109 1.2.2.2 nathanw
110 1.2.2.2 nathanw default:
111 1.2.2.2 nathanw return (ENOTTY);
112 1.2.2.2 nathanw }
113 1.2.2.2 nathanw
114 1.2.2.2 nathanw return (0);
115 1.2.2.2 nathanw }
116 1.2.2.2 nathanw
117 1.2.2.2 nathanw paddr_t
118 1.2.2.2 nathanw pcimmap(dev_t dev, off_t offset, int prot)
119 1.2.2.2 nathanw {
120 1.2.2.2 nathanw #if 0
121 1.2.2.2 nathanw struct pci_softc *sc = device_lookup(&pci_cd, minor(dev));
122 1.2.2.2 nathanw
123 1.2.2.2 nathanw /*
124 1.2.2.2 nathanw * Since we allow mapping of the entire bus, we
125 1.2.2.2 nathanw * take the offset to be the address on the bus,
126 1.2.2.2 nathanw * and pass 0 as the offset into that range.
127 1.2.2.2 nathanw *
128 1.2.2.2 nathanw * XXX Need a way to deal with linear/prefetchable/etc.
129 1.2.2.2 nathanw */
130 1.2.2.2 nathanw return (bus_space_mmap(sc->sc_memt, offset, 0, prot, 0));
131 1.2.2.2 nathanw #else
132 1.2.2.2 nathanw /* XXX Consider this further. */
133 1.2.2.2 nathanw return (-1);
134 1.2.2.2 nathanw #endif
135 1.2.2.2 nathanw }
136 1.2.2.2 nathanw
137 1.2.2.2 nathanw /*
138 1.2.2.2 nathanw * pci_devioctl:
139 1.2.2.2 nathanw *
140 1.2.2.2 nathanw * PCI ioctls that can be performed on devices directly.
141 1.2.2.2 nathanw */
142 1.2.2.2 nathanw int
143 1.2.2.2 nathanw pci_devioctl(pci_chipset_tag_t pc, pcitag_t tag, u_long cmd, caddr_t data,
144 1.2.2.2 nathanw int flag, struct proc *p)
145 1.2.2.2 nathanw {
146 1.2.2.2 nathanw struct pciio_cfgreg *r = (void *) data;
147 1.2.2.2 nathanw
148 1.2.2.2 nathanw switch (cmd) {
149 1.2.2.2 nathanw case PCI_IOC_CFGREAD:
150 1.2.2.2 nathanw case PCI_IOC_CFGWRITE:
151 1.2.2.2 nathanw if (cmd == PCI_IOC_CFGREAD)
152 1.2.2.2 nathanw r->val = pci_conf_read(pc, tag, r->reg);
153 1.2.2.2 nathanw else {
154 1.2.2.2 nathanw if ((flag & FWRITE) == 0)
155 1.2.2.2 nathanw return (EBADF);
156 1.2.2.2 nathanw pci_conf_write(pc, tag, r->reg, r->val);
157 1.2.2.2 nathanw }
158 1.2.2.2 nathanw break;
159 1.2.2.2 nathanw
160 1.2.2.2 nathanw default:
161 1.2.2.2 nathanw return (ENOTTY);
162 1.2.2.2 nathanw }
163 1.2.2.2 nathanw
164 1.2.2.2 nathanw return (0);
165 1.2.2.2 nathanw }
166