drm_pci.c revision 1.1.2.9 1 1.1.2.1 riastrad /* $NetBSD: drm_pci.c,v 1.1.2.9 2013/09/08 15:46:22 riastradh Exp $ */
2 1.1.2.1 riastrad
3 1.1.2.1 riastrad /*-
4 1.1.2.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1.2.1 riastrad * All rights reserved.
6 1.1.2.1 riastrad *
7 1.1.2.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 riastrad * by Taylor R. Campbell.
9 1.1.2.1 riastrad *
10 1.1.2.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 riastrad * modification, are permitted provided that the following conditions
12 1.1.2.1 riastrad * are met:
13 1.1.2.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 riastrad *
19 1.1.2.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 riastrad */
31 1.1.2.1 riastrad
32 1.1.2.1 riastrad #include <sys/cdefs.h>
33 1.1.2.1 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_pci.c,v 1.1.2.9 2013/09/08 15:46:22 riastradh Exp $");
34 1.1.2.1 riastrad
35 1.1.2.1 riastrad #include <sys/types.h>
36 1.1.2.1 riastrad #include <sys/errno.h>
37 1.1.2.1 riastrad #include <sys/systm.h>
38 1.1.2.1 riastrad
39 1.1.2.1 riastrad #include <dev/pci/pcivar.h>
40 1.1.2.1 riastrad
41 1.1.2.1 riastrad #include <drm/drmP.h>
42 1.1.2.1 riastrad
43 1.1.2.1 riastrad static int drm_pci_get_irq(struct drm_device *);
44 1.1.2.1 riastrad static int drm_pci_irq_install(struct drm_device *,
45 1.1.2.1 riastrad irqreturn_t (*)(void *), int, const char *, void *,
46 1.1.2.1 riastrad struct drm_bus_irq_cookie **);
47 1.1.2.1 riastrad static void drm_pci_irq_uninstall(struct drm_device *,
48 1.1.2.1 riastrad struct drm_bus_irq_cookie *);
49 1.1.2.1 riastrad static const char *
50 1.1.2.1 riastrad drm_pci_get_name(struct drm_device *);
51 1.1.2.1 riastrad static int drm_pci_set_busid(struct drm_device *, struct drm_master *);
52 1.1.2.1 riastrad static int drm_pci_set_unique(struct drm_device *, struct drm_master *,
53 1.1.2.1 riastrad struct drm_unique *);
54 1.1.2.1 riastrad static int drm_pci_irq_by_busid(struct drm_device *,
55 1.1.2.1 riastrad struct drm_irq_busid *);
56 1.1.2.1 riastrad static int drm_pci_agp_init(struct drm_device *);
57 1.1.2.1 riastrad
58 1.1.2.1 riastrad const struct drm_bus drm_pci_bus = {
59 1.1.2.1 riastrad .bus_type = DRIVER_BUS_PCI,
60 1.1.2.1 riastrad .get_irq = drm_pci_get_irq,
61 1.1.2.1 riastrad .irq_install = drm_pci_irq_install,
62 1.1.2.1 riastrad .irq_uninstall = drm_pci_irq_uninstall,
63 1.1.2.1 riastrad .get_name = drm_pci_get_name,
64 1.1.2.1 riastrad .set_busid = drm_pci_set_busid,
65 1.1.2.1 riastrad .set_unique = drm_pci_set_unique,
66 1.1.2.1 riastrad .irq_by_busid = drm_pci_irq_by_busid,
67 1.1.2.1 riastrad .agp_init = drm_pci_agp_init,
68 1.1.2.1 riastrad };
69 1.1.2.1 riastrad
70 1.1.2.1 riastrad static const struct pci_attach_args *
71 1.1.2.1 riastrad drm_pci_attach_args(struct drm_device *dev)
72 1.1.2.1 riastrad {
73 1.1.2.1 riastrad return &dev->pdev->pd_pa;
74 1.1.2.1 riastrad }
75 1.1.2.1 riastrad
76 1.1.2.6 riastrad int
77 1.1.2.6 riastrad drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
78 1.1.2.6 riastrad {
79 1.1.2.6 riastrad
80 1.1.2.6 riastrad driver->bus = &drm_pci_bus;
81 1.1.2.6 riastrad return 0;
82 1.1.2.6 riastrad }
83 1.1.2.6 riastrad
84 1.1.2.6 riastrad void
85 1.1.2.6 riastrad drm_pci_exit(struct drm_driver *driver __unused,
86 1.1.2.6 riastrad struct pci_driver *pdriver __unused)
87 1.1.2.6 riastrad {
88 1.1.2.6 riastrad }
89 1.1.2.6 riastrad
90 1.1.2.7 riastrad /* XXX Should go elsewhere. */
91 1.1.2.7 riastrad #define PCI_NMAPREGS ((PCI_MAPREG_END - PCI_MAPREG_START) / 4)
92 1.1.2.7 riastrad
93 1.1.2.2 riastrad void
94 1.1.2.2 riastrad drm_pci_attach(device_t self, const struct pci_attach_args *pa,
95 1.1.2.2 riastrad struct pci_dev *pdev, struct drm_device *dev)
96 1.1.2.2 riastrad {
97 1.1.2.7 riastrad unsigned int unit;
98 1.1.2.2 riastrad
99 1.1.2.3 riastrad linux_pci_dev_init(pdev, self, pa, 0);
100 1.1.2.2 riastrad
101 1.1.2.2 riastrad dev->pdev = pdev;
102 1.1.2.2 riastrad dev->pci_vendor = pdev->vendor;
103 1.1.2.2 riastrad dev->pci_device = pdev->device;
104 1.1.2.2 riastrad
105 1.1.2.2 riastrad /* XXX Set the power state to D0? */
106 1.1.2.7 riastrad
107 1.1.2.7 riastrad dev->bst = pa->pa_memt;
108 1.1.2.7 riastrad dev->bus_dmat = pa->pa_dmat; /* XXX dmat64? */
109 1.1.2.9 riastrad dev->dmat = dev->bus_dmat;
110 1.1.2.7 riastrad dev->dmat_subregion_p = false;
111 1.1.2.7 riastrad
112 1.1.2.7 riastrad CTASSERT(PCI_NMAPREGS < (SIZE_MAX / sizeof(dev->bus_maps[0])));
113 1.1.2.7 riastrad dev->bus_nmaps = PCI_NMAPREGS;
114 1.1.2.7 riastrad dev->bus_maps = kmem_zalloc((PCI_NMAPREGS * sizeof(dev->bus_maps[0])),
115 1.1.2.7 riastrad KM_SLEEP);
116 1.1.2.7 riastrad for (unit = 0; unit < dev->bus_nmaps; unit++) {
117 1.1.2.7 riastrad struct drm_bus_map *const bm = &dev->bus_maps[unit];
118 1.1.2.8 riastrad const int reg = PCI_BAR(unit);
119 1.1.2.7 riastrad const pcireg_t type =
120 1.1.2.7 riastrad pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
121 1.1.2.7 riastrad
122 1.1.2.7 riastrad /* Reject non-memory mappings. */
123 1.1.2.7 riastrad if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
124 1.1.2.7 riastrad aprint_debug_dev(self, "map %u has non-memory type:"
125 1.1.2.7 riastrad " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
126 1.1.2.7 riastrad continue;
127 1.1.2.7 riastrad }
128 1.1.2.7 riastrad
129 1.1.2.7 riastrad /* Inquire about it. We'll map it in drm_ioremap. */
130 1.1.2.7 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
131 1.1.2.7 riastrad &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
132 1.1.2.7 riastrad aprint_debug_dev(self, "map %u failed\n", unit);
133 1.1.2.7 riastrad continue;
134 1.1.2.7 riastrad }
135 1.1.2.7 riastrad
136 1.1.2.7 riastrad aprint_debug_dev(self, "map %u at %"PRIxMAX" size %"PRIxMAX
137 1.1.2.7 riastrad " flags 0x%"PRIxMAX"\n",
138 1.1.2.7 riastrad unit, (uintmax_t)bm->bm_base, (uintmax_t)bm->bm_size,
139 1.1.2.7 riastrad (uintmax_t)bm->bm_flags);
140 1.1.2.7 riastrad
141 1.1.2.7 riastrad /* Assume since it is a memory mapping it can be linear. */
142 1.1.2.7 riastrad bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
143 1.1.2.7 riastrad }
144 1.1.2.7 riastrad
145 1.1.2.7 riastrad /* All `agp' maps are just initialized to zero. */
146 1.1.2.7 riastrad CTASSERT(PCI_NMAPREGS < (SIZE_MAX / sizeof(dev->agp_maps[0])));
147 1.1.2.7 riastrad dev->agp_nmaps = PCI_NMAPREGS;
148 1.1.2.7 riastrad dev->agp_maps = kmem_zalloc((PCI_NMAPREGS * sizeof(dev->agp_maps[0])),
149 1.1.2.7 riastrad KM_SLEEP);
150 1.1.2.2 riastrad }
151 1.1.2.2 riastrad
152 1.1.2.2 riastrad int
153 1.1.2.2 riastrad drm_pci_detach(struct drm_device *dev __unused, int flags __unused)
154 1.1.2.2 riastrad {
155 1.1.2.2 riastrad
156 1.1.2.7 riastrad kmem_free(dev->bus_maps, (PCI_NMAPREGS * sizeof(dev->bus_maps[0])));
157 1.1.2.7 riastrad kmem_free(dev->agp_maps, (PCI_NMAPREGS * sizeof(dev->agp_maps[0])));
158 1.1.2.7 riastrad
159 1.1.2.7 riastrad /*
160 1.1.2.7 riastrad * XXX This is the wrong place! Should be a routine in
161 1.1.2.7 riastrad * drm_memory.c.
162 1.1.2.7 riastrad */
163 1.1.2.7 riastrad if (dev->dmat_subregion_p)
164 1.1.2.7 riastrad bus_dmatag_destroy(dev->dmat);
165 1.1.2.7 riastrad
166 1.1.2.2 riastrad /* XXX Disestablish irqs or anything? */
167 1.1.2.2 riastrad return 0;
168 1.1.2.2 riastrad }
169 1.1.2.2 riastrad
170 1.1.2.1 riastrad static int
171 1.1.2.1 riastrad drm_pci_get_irq(struct drm_device *dev)
172 1.1.2.1 riastrad {
173 1.1.2.1 riastrad pci_intr_handle_t ih_pih;
174 1.1.2.1 riastrad int ih_int;
175 1.1.2.1 riastrad
176 1.1.2.1 riastrad /*
177 1.1.2.1 riastrad * This is a compile-time assertion that the types match. If
178 1.1.2.1 riastrad * this fails, we have to change a bunch of drm code that uses
179 1.1.2.1 riastrad * int for intr handles.
180 1.1.2.1 riastrad */
181 1.1.2.1 riastrad KASSERT(&ih_pih != &ih_int);
182 1.1.2.1 riastrad
183 1.1.2.1 riastrad if (pci_intr_map(drm_pci_attach_args(dev), &ih_pih))
184 1.1.2.1 riastrad return -1; /* XXX Hope -1 is an invalid intr handle. */
185 1.1.2.1 riastrad
186 1.1.2.1 riastrad ih_int = ih_pih;
187 1.1.2.1 riastrad return ih_int;
188 1.1.2.1 riastrad }
189 1.1.2.1 riastrad
190 1.1.2.1 riastrad static int
191 1.1.2.1 riastrad drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
192 1.1.2.1 riastrad int flags, const char *name, void *arg,
193 1.1.2.1 riastrad struct drm_bus_irq_cookie **cookiep)
194 1.1.2.1 riastrad {
195 1.1.2.1 riastrad const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
196 1.1.2.1 riastrad pci_intr_handle_t ih;
197 1.1.2.1 riastrad const char *intrstr;
198 1.1.2.1 riastrad void *ih_cookie;
199 1.1.2.1 riastrad
200 1.1.2.1 riastrad if (pci_intr_map(pa, &ih))
201 1.1.2.1 riastrad return -ENOENT;
202 1.1.2.1 riastrad
203 1.1.2.1 riastrad intrstr = pci_intr_string(pa->pa_pc, ih);
204 1.1.2.1 riastrad ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
205 1.1.2.1 riastrad if (ih_cookie == NULL) {
206 1.1.2.1 riastrad aprint_error_dev(dev->dev,
207 1.1.2.1 riastrad "couldn't establish interrupt at %s (%s)\n",
208 1.1.2.1 riastrad intrstr, name);
209 1.1.2.1 riastrad return -ENOENT;
210 1.1.2.1 riastrad }
211 1.1.2.1 riastrad
212 1.1.2.1 riastrad aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
213 1.1.2.1 riastrad intrstr, name);
214 1.1.2.1 riastrad *cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
215 1.1.2.1 riastrad return 0;
216 1.1.2.1 riastrad }
217 1.1.2.1 riastrad
218 1.1.2.1 riastrad static void
219 1.1.2.1 riastrad drm_pci_irq_uninstall(struct drm_device *dev,
220 1.1.2.1 riastrad struct drm_bus_irq_cookie *cookie)
221 1.1.2.1 riastrad {
222 1.1.2.1 riastrad const struct pci_attach_args *pa = drm_pci_attach_args(dev);
223 1.1.2.1 riastrad
224 1.1.2.1 riastrad pci_intr_disestablish(pa->pa_pc, (void *)cookie);
225 1.1.2.1 riastrad }
226 1.1.2.1 riastrad
227 1.1.2.1 riastrad static const char *
228 1.1.2.1 riastrad drm_pci_get_name(struct drm_device *dev)
229 1.1.2.1 riastrad {
230 1.1.2.1 riastrad return "pci"; /* XXX PCI bus names? */
231 1.1.2.1 riastrad }
232 1.1.2.1 riastrad
233 1.1.2.1 riastrad static int
234 1.1.2.1 riastrad drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
235 1.1.2.1 riastrad {
236 1.1.2.1 riastrad return -ENOSYS; /* XXX PCI bus ids? */
237 1.1.2.1 riastrad }
238 1.1.2.1 riastrad
239 1.1.2.1 riastrad static int
240 1.1.2.1 riastrad drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
241 1.1.2.1 riastrad struct drm_unique *unique)
242 1.1.2.1 riastrad {
243 1.1.2.1 riastrad return -ENOSYS; /* XXX */
244 1.1.2.1 riastrad }
245 1.1.2.1 riastrad
246 1.1.2.1 riastrad static int
247 1.1.2.1 riastrad drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
248 1.1.2.1 riastrad {
249 1.1.2.1 riastrad return -ENOSYS; /* XXX */
250 1.1.2.1 riastrad }
251 1.1.2.1 riastrad
252 1.1.2.1 riastrad static int
253 1.1.2.1 riastrad drm_pci_agp_init(struct drm_device *dev)
254 1.1.2.1 riastrad {
255 1.1.2.5 riastrad return 0; /* XXX */
256 1.1.2.1 riastrad }
257