drm_pci.c revision 1.6.2.4 1 /* $NetBSD: drm_pci.c,v 1.6.2.4 2019/04/16 08:10:22 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_pci.c,v 1.6.2.4 2019/04/16 08:10:22 martin Exp $");
34
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/systm.h>
38
39 #include <dev/pci/pcivar.h>
40
41 #include <drm/drmP.h>
42
43 static int drm_pci_get_irq(struct drm_device *);
44 static int drm_pci_irq_install(struct drm_device *,
45 irqreturn_t (*)(void *), int, const char *, void *,
46 struct drm_bus_irq_cookie **);
47 static void drm_pci_irq_uninstall(struct drm_device *,
48 struct drm_bus_irq_cookie *);
49 static const char *
50 drm_pci_get_name(struct drm_device *);
51 static int drm_pci_set_busid(struct drm_device *, struct drm_master *);
52 static int drm_pci_set_unique(struct drm_device *, struct drm_master *,
53 struct drm_unique *);
54 static int drm_pci_irq_by_busid(struct drm_device *,
55 struct drm_irq_busid *);
56
57 const struct drm_bus drm_pci_bus = {
58 .bus_type = DRIVER_BUS_PCI,
59 .get_irq = drm_pci_get_irq,
60 .irq_install = drm_pci_irq_install,
61 .irq_uninstall = drm_pci_irq_uninstall,
62 .get_name = drm_pci_get_name,
63 .set_busid = drm_pci_set_busid,
64 .set_unique = drm_pci_set_unique,
65 .irq_by_busid = drm_pci_irq_by_busid,
66 };
67
68 static const struct pci_attach_args *
69 drm_pci_attach_args(struct drm_device *dev)
70 {
71 return &dev->pdev->pd_pa;
72 }
73
74 int
75 drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
76 {
77
78 driver->bus = &drm_pci_bus;
79 return 0;
80 }
81
82 void
83 drm_pci_exit(struct drm_driver *driver __unused,
84 struct pci_driver *pdriver __unused)
85 {
86 }
87
88 int
89 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
90 struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
91 struct drm_device **devp)
92 {
93 struct drm_device *dev;
94 unsigned int unit;
95 int ret;
96
97 /* Ensure the drm agp hooks are installed. */
98 /* XXX errno NetBSD->Linux */
99 ret = -drmkms_pci_agp_guarantee_initialized();
100 if (ret)
101 goto fail0;
102
103 /* Initialize the Linux PCI device descriptor. */
104 linux_pci_dev_init(pdev, self, pa, 0);
105
106 /* Create a DRM device. */
107 dev = drm_dev_alloc(driver, self);
108 if (dev == NULL) {
109 ret = -ENOMEM;
110 goto fail0;
111 }
112
113 dev->pdev = pdev;
114 pdev->pd_drm_dev = dev; /* XXX Nouveau kludge. */
115
116 /* XXX Set the power state to D0? */
117
118 /* Set up the bus space and bus DMA tags. */
119 dev->bst = pa->pa_memt;
120 /* XXX Let the driver say something about 32-bit vs 64-bit DMA? */
121 dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
122 dev->dmat = dev->bus_dmat;
123 dev->dmat_subregion_p = false;
124
125 /* Find all the memory maps. */
126 CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
127 dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
128 sizeof(dev->bus_maps[0]), KM_SLEEP);
129 dev->bus_nmaps = PCI_NUM_RESOURCES;
130 for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
131 struct drm_bus_map *const bm = &dev->bus_maps[unit];
132 const int reg = PCI_BAR(unit);
133 const pcireg_t type =
134 pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
135
136 /* Reject non-memory mappings. */
137 if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
138 aprint_debug_dev(self, "map %u has non-memory type:"
139 " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
140 continue;
141 }
142
143 /*
144 * If it's a 64-bit mapping, don't interpret the second
145 * half of it as another BAR in the next iteration of
146 * the loop -- move on to the next unit.
147 */
148 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
149 unit++;
150
151 /* Inquire about it. We'll map it in drm_core_ioremap. */
152 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
153 &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
154 aprint_debug_dev(self, "map %u failed\n", unit);
155 continue;
156 }
157
158 /* Assume since it is a memory mapping it can be linear. */
159 bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
160 }
161
162 /* Set up AGP stuff if requested. */
163 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
164 if (drm_pci_device_is_agp(dev))
165 dev->agp = drm_agp_init(dev);
166 if (dev->agp)
167 dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
168 dev->agp->agp_info.aki_info.ai_aperture_size);
169 }
170
171 /* Register the DRM device and do driver-specific initialization. */
172 ret = drm_dev_register(dev, cookie);
173 if (ret)
174 goto fail1;
175
176 /* Success! */
177 *devp = dev;
178 return 0;
179
180 fail2: __unused
181 drm_dev_unregister(dev);
182 fail1: drm_pci_agp_destroy(dev);
183 dev->bus_nmaps = 0;
184 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
185 if (dev->dmat_subregion_p)
186 bus_dmatag_destroy(dev->dmat);
187 drm_dev_unref(dev);
188 fail0: return ret;
189 }
190
191 int
192 drm_pci_detach(struct drm_device *dev, int flags __unused)
193 {
194
195 /* Do driver-specific detachment and unregister the device. */
196 drm_dev_unregister(dev);
197
198 /* Tear down AGP stuff if necessary. */
199 drm_pci_agp_destroy(dev);
200
201 /* Free the record of available bus space mappings. */
202 dev->bus_nmaps = 0;
203 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
204
205 /* Tear down bus space and bus DMA tags. */
206 if (dev->dmat_subregion_p)
207 bus_dmatag_destroy(dev->dmat);
208
209 drm_dev_unref(dev);
210
211 return 0;
212 }
213
214 void
215 drm_pci_agp_destroy(struct drm_device *dev)
216 {
217
218 if (dev->agp) {
219 arch_phys_wc_del(dev->agp->agp_mtrr);
220 drm_agp_clear(dev);
221 kfree(dev->agp); /* XXX Should go in drm_agp_clear... */
222 dev->agp = NULL;
223 }
224 }
225
226 static int
227 drm_pci_get_irq(struct drm_device *dev)
228 {
229
230 /*
231 * Caller expects a nonzero int, and doesn't really use it for
232 * anything, so no need to pci_intr_map here.
233 */
234 return dev->pdev->pd_pa.pa_intrpin;
235 }
236
237 static int
238 drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
239 int flags, const char *name, void *arg,
240 struct drm_bus_irq_cookie **cookiep)
241 {
242 const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
243 pci_intr_handle_t ih;
244 const char *intrstr;
245 void *ih_cookie;
246 char intrbuf[PCI_INTRSTR_LEN];
247
248 if (pci_intr_map(pa, &ih))
249 return -ENOENT;
250
251 intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
252 ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
253 if (ih_cookie == NULL) {
254 aprint_error_dev(dev->dev,
255 "couldn't establish interrupt at %s (%s)\n",
256 intrstr, name);
257 return -ENOENT;
258 }
259
260 aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
261 intrstr, name);
262 *cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
263 return 0;
264 }
265
266 static void
267 drm_pci_irq_uninstall(struct drm_device *dev,
268 struct drm_bus_irq_cookie *cookie)
269 {
270 const struct pci_attach_args *pa = drm_pci_attach_args(dev);
271
272 pci_intr_disestablish(pa->pa_pc, (void *)cookie);
273 }
274
275 static const char *
276 drm_pci_get_name(struct drm_device *dev)
277 {
278 return "pci"; /* XXX PCI bus names? */
279 }
280
281 static int
282 drm_pci_format_unique(struct drm_device *dev, char *buf, size_t size)
283 {
284 const unsigned int domain = device_unit(device_parent(dev->dev));
285 const unsigned int bus = dev->pdev->pd_pa.pa_bus;
286 const unsigned int device = dev->pdev->pd_pa.pa_device;
287 const unsigned int function = dev->pdev->pd_pa.pa_function;
288
289 return snprintf(buf, size, "pci:%04x:%02x:%02x.%d",
290 domain, bus, device, function);
291 }
292
293 static int
294 drm_pci_format_devname(struct drm_device *dev, const char *unique,
295 char *buf, size_t size)
296 {
297
298 return snprintf(buf, size, "%s@%s",
299 device_xname(device_parent(dev->dev)),
300 unique);
301 }
302
303 static int
304 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
305 {
306 int n;
307 char *buf;
308
309 n = drm_pci_format_unique(dev, NULL, 0);
310 if (n < 0)
311 return -ENOSPC; /* XXX */
312 if (0xff < n)
313 n = 0xff;
314
315 buf = kzalloc(n + 1, GFP_KERNEL);
316 (void)drm_pci_format_unique(dev, buf, n + 1);
317
318 if (master->unique)
319 kfree(master->unique);
320 master->unique = buf;
321 master->unique_len = n;
322 master->unique_size = n + 1;
323
324 n = drm_pci_format_devname(dev, master->unique, NULL, 0);
325 if (n < 0)
326 return -ENOSPC; /* XXX back out? */
327 if (0xff < n)
328 n = 0xff;
329
330 buf = kzalloc(n + 1, GFP_KERNEL);
331 (void)drm_pci_format_devname(dev, master->unique, buf, n + 1);
332
333 if (dev->devname)
334 kfree(dev->devname);
335 dev->devname = buf;
336
337 return 0;
338 }
339
340 static int
341 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
342 struct drm_unique *unique __unused)
343 {
344
345 /*
346 * XXX This is silly. We're supposed to reject unique names
347 * that don't match the ones we would generate anyway. For
348 * expedience, we'll just generate the one we would and ignore
349 * whatever userland threw at us...
350 */
351 return drm_pci_set_busid(dev, master);
352 }
353
354 static int
355 drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
356 {
357 return -ENOSYS; /* XXX */
358 }
359