drm_pci.c revision 1.1.2.8 1 1.1.2.1 riastrad /* $NetBSD: drm_pci.c,v 1.1.2.8 2013/07/24 04:05:34 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.8 2013/07/24 04:05:34 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.7 riastrad dev->dmat_subregion_p = false;
110 1.1.2.7 riastrad
111 1.1.2.7 riastrad CTASSERT(PCI_NMAPREGS < (SIZE_MAX / sizeof(dev->bus_maps[0])));
112 1.1.2.7 riastrad dev->bus_nmaps = PCI_NMAPREGS;
113 1.1.2.7 riastrad dev->bus_maps = kmem_zalloc((PCI_NMAPREGS * sizeof(dev->bus_maps[0])),
114 1.1.2.7 riastrad KM_SLEEP);
115 1.1.2.7 riastrad for (unit = 0; unit < dev->bus_nmaps; unit++) {
116 1.1.2.7 riastrad struct drm_bus_map *const bm = &dev->bus_maps[unit];
117 1.1.2.8 riastrad const int reg = PCI_BAR(unit);
118 1.1.2.7 riastrad const pcireg_t type =
119 1.1.2.7 riastrad pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
120 1.1.2.7 riastrad
121 1.1.2.7 riastrad /* Reject non-memory mappings. */
122 1.1.2.7 riastrad if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
123 1.1.2.7 riastrad aprint_debug_dev(self, "map %u has non-memory type:"
124 1.1.2.7 riastrad " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
125 1.1.2.7 riastrad continue;
126 1.1.2.7 riastrad }
127 1.1.2.7 riastrad
128 1.1.2.7 riastrad /* Inquire about it. We'll map it in drm_ioremap. */
129 1.1.2.7 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
130 1.1.2.7 riastrad &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
131 1.1.2.7 riastrad aprint_debug_dev(self, "map %u failed\n", unit);
132 1.1.2.7 riastrad continue;
133 1.1.2.7 riastrad }
134 1.1.2.7 riastrad
135 1.1.2.7 riastrad aprint_debug_dev(self, "map %u at %"PRIxMAX" size %"PRIxMAX
136 1.1.2.7 riastrad " flags 0x%"PRIxMAX"\n",
137 1.1.2.7 riastrad unit, (uintmax_t)bm->bm_base, (uintmax_t)bm->bm_size,
138 1.1.2.7 riastrad (uintmax_t)bm->bm_flags);
139 1.1.2.7 riastrad
140 1.1.2.7 riastrad /* Assume since it is a memory mapping it can be linear. */
141 1.1.2.7 riastrad bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
142 1.1.2.7 riastrad }
143 1.1.2.7 riastrad
144 1.1.2.7 riastrad /* All `agp' maps are just initialized to zero. */
145 1.1.2.7 riastrad CTASSERT(PCI_NMAPREGS < (SIZE_MAX / sizeof(dev->agp_maps[0])));
146 1.1.2.7 riastrad dev->agp_nmaps = PCI_NMAPREGS;
147 1.1.2.7 riastrad dev->agp_maps = kmem_zalloc((PCI_NMAPREGS * sizeof(dev->agp_maps[0])),
148 1.1.2.7 riastrad KM_SLEEP);
149 1.1.2.2 riastrad }
150 1.1.2.2 riastrad
151 1.1.2.2 riastrad int
152 1.1.2.2 riastrad drm_pci_detach(struct drm_device *dev __unused, int flags __unused)
153 1.1.2.2 riastrad {
154 1.1.2.2 riastrad
155 1.1.2.7 riastrad kmem_free(dev->bus_maps, (PCI_NMAPREGS * sizeof(dev->bus_maps[0])));
156 1.1.2.7 riastrad kmem_free(dev->agp_maps, (PCI_NMAPREGS * sizeof(dev->agp_maps[0])));
157 1.1.2.7 riastrad
158 1.1.2.7 riastrad /*
159 1.1.2.7 riastrad * XXX This is the wrong place! Should be a routine in
160 1.1.2.7 riastrad * drm_memory.c.
161 1.1.2.7 riastrad */
162 1.1.2.7 riastrad if (dev->dmat_subregion_p)
163 1.1.2.7 riastrad bus_dmatag_destroy(dev->dmat);
164 1.1.2.7 riastrad
165 1.1.2.2 riastrad /* XXX Disestablish irqs or anything? */
166 1.1.2.2 riastrad return 0;
167 1.1.2.2 riastrad }
168 1.1.2.2 riastrad
169 1.1.2.1 riastrad static int
170 1.1.2.1 riastrad drm_pci_get_irq(struct drm_device *dev)
171 1.1.2.1 riastrad {
172 1.1.2.1 riastrad pci_intr_handle_t ih_pih;
173 1.1.2.1 riastrad int ih_int;
174 1.1.2.1 riastrad
175 1.1.2.1 riastrad /*
176 1.1.2.1 riastrad * This is a compile-time assertion that the types match. If
177 1.1.2.1 riastrad * this fails, we have to change a bunch of drm code that uses
178 1.1.2.1 riastrad * int for intr handles.
179 1.1.2.1 riastrad */
180 1.1.2.1 riastrad KASSERT(&ih_pih != &ih_int);
181 1.1.2.1 riastrad
182 1.1.2.1 riastrad if (pci_intr_map(drm_pci_attach_args(dev), &ih_pih))
183 1.1.2.1 riastrad return -1; /* XXX Hope -1 is an invalid intr handle. */
184 1.1.2.1 riastrad
185 1.1.2.1 riastrad ih_int = ih_pih;
186 1.1.2.1 riastrad return ih_int;
187 1.1.2.1 riastrad }
188 1.1.2.1 riastrad
189 1.1.2.1 riastrad static int
190 1.1.2.1 riastrad drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
191 1.1.2.1 riastrad int flags, const char *name, void *arg,
192 1.1.2.1 riastrad struct drm_bus_irq_cookie **cookiep)
193 1.1.2.1 riastrad {
194 1.1.2.1 riastrad const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
195 1.1.2.1 riastrad pci_intr_handle_t ih;
196 1.1.2.1 riastrad const char *intrstr;
197 1.1.2.1 riastrad void *ih_cookie;
198 1.1.2.1 riastrad
199 1.1.2.1 riastrad if (pci_intr_map(pa, &ih))
200 1.1.2.1 riastrad return -ENOENT;
201 1.1.2.1 riastrad
202 1.1.2.1 riastrad intrstr = pci_intr_string(pa->pa_pc, ih);
203 1.1.2.1 riastrad ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
204 1.1.2.1 riastrad if (ih_cookie == NULL) {
205 1.1.2.1 riastrad aprint_error_dev(dev->dev,
206 1.1.2.1 riastrad "couldn't establish interrupt at %s (%s)\n",
207 1.1.2.1 riastrad intrstr, name);
208 1.1.2.1 riastrad return -ENOENT;
209 1.1.2.1 riastrad }
210 1.1.2.1 riastrad
211 1.1.2.1 riastrad aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
212 1.1.2.1 riastrad intrstr, name);
213 1.1.2.1 riastrad *cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
214 1.1.2.1 riastrad return 0;
215 1.1.2.1 riastrad }
216 1.1.2.1 riastrad
217 1.1.2.1 riastrad static void
218 1.1.2.1 riastrad drm_pci_irq_uninstall(struct drm_device *dev,
219 1.1.2.1 riastrad struct drm_bus_irq_cookie *cookie)
220 1.1.2.1 riastrad {
221 1.1.2.1 riastrad const struct pci_attach_args *pa = drm_pci_attach_args(dev);
222 1.1.2.1 riastrad
223 1.1.2.1 riastrad pci_intr_disestablish(pa->pa_pc, (void *)cookie);
224 1.1.2.1 riastrad }
225 1.1.2.1 riastrad
226 1.1.2.1 riastrad static const char *
227 1.1.2.1 riastrad drm_pci_get_name(struct drm_device *dev)
228 1.1.2.1 riastrad {
229 1.1.2.1 riastrad return "pci"; /* XXX PCI bus names? */
230 1.1.2.1 riastrad }
231 1.1.2.1 riastrad
232 1.1.2.1 riastrad static int
233 1.1.2.1 riastrad drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
234 1.1.2.1 riastrad {
235 1.1.2.1 riastrad return -ENOSYS; /* XXX PCI bus ids? */
236 1.1.2.1 riastrad }
237 1.1.2.1 riastrad
238 1.1.2.1 riastrad static int
239 1.1.2.1 riastrad drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
240 1.1.2.1 riastrad struct drm_unique *unique)
241 1.1.2.1 riastrad {
242 1.1.2.1 riastrad return -ENOSYS; /* XXX */
243 1.1.2.1 riastrad }
244 1.1.2.1 riastrad
245 1.1.2.1 riastrad static int
246 1.1.2.1 riastrad drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
247 1.1.2.1 riastrad {
248 1.1.2.1 riastrad return -ENOSYS; /* XXX */
249 1.1.2.1 riastrad }
250 1.1.2.1 riastrad
251 1.1.2.1 riastrad static int
252 1.1.2.1 riastrad drm_pci_agp_init(struct drm_device *dev)
253 1.1.2.1 riastrad {
254 1.1.2.5 riastrad return 0; /* XXX */
255 1.1.2.1 riastrad }
256