Home | History | Annotate | Line # | Download | only in pci
drm_pci.c revision 1.21
      1 /*	$NetBSD: drm_pci.c,v 1.21 2018/08/27 07:03:02 riastradh 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.21 2018/08/27 07:03:02 riastradh 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 struct drm_bus_irq_cookie {
     44 	pci_intr_handle_t *intr_handles;
     45 	void *ih_cookie;
     46 };
     47 
     48 static int	drm_pci_get_irq(struct drm_device *);
     49 static int	drm_pci_irq_install(struct drm_device *,
     50 		    irqreturn_t (*)(void *), int, const char *, void *,
     51 		    struct drm_bus_irq_cookie **);
     52 static void	drm_pci_irq_uninstall(struct drm_device *,
     53 		    struct drm_bus_irq_cookie *);
     54 static const char *
     55 		drm_pci_get_name(struct drm_device *);
     56 static int	drm_pci_set_unique(struct drm_device *, struct drm_master *,
     57 		    struct drm_unique *);
     58 static int	drm_pci_irq_by_busid(struct drm_device *,
     59 		    struct drm_irq_busid *);
     60 
     61 const struct drm_bus drm_pci_bus = {
     62 	.bus_type = DRIVER_BUS_PCI,
     63 	.get_irq = drm_pci_get_irq,
     64 	.irq_install = drm_pci_irq_install,
     65 	.irq_uninstall = drm_pci_irq_uninstall,
     66 	.get_name = drm_pci_get_name,
     67 	.set_busid = drm_pci_set_busid,
     68 	.set_unique = drm_pci_set_unique,
     69 	.irq_by_busid = drm_pci_irq_by_busid,
     70 };
     71 
     72 static const struct pci_attach_args *
     73 drm_pci_attach_args(struct drm_device *dev)
     74 {
     75 	return &dev->pdev->pd_pa;
     76 }
     77 
     78 int
     79 drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
     80 {
     81 
     82 	driver->bus = &drm_pci_bus;
     83 	return 0;
     84 }
     85 
     86 void
     87 drm_pci_exit(struct drm_driver *driver __unused,
     88     struct pci_driver *pdriver __unused)
     89 {
     90 }
     91 
     92 int
     93 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
     94     struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
     95     struct drm_device **devp)
     96 {
     97 	struct drm_device *dev;
     98 	unsigned int unit;
     99 	int ret;
    100 
    101 	/* Ensure the drm agp hooks are installed.  */
    102 	/* XXX errno NetBSD->Linux */
    103 	ret = -drmkms_pci_agp_guarantee_initialized();
    104 	if (ret)
    105 		goto fail0;
    106 
    107 	/* Initialize the Linux PCI device descriptor.  */
    108 	linux_pci_dev_init(pdev, self, pa, 0);
    109 
    110 	/* Create a DRM device.  */
    111 	dev = drm_dev_alloc(driver, self);
    112 	if (dev == NULL) {
    113 		ret = -ENOMEM;
    114 		goto fail0;
    115 	}
    116 
    117 	dev->pdev = pdev;
    118 	pdev->pd_drm_dev = dev;	/* XXX Nouveau kludge.  */
    119 
    120 	/* XXX Set the power state to D0?  */
    121 
    122 	/* Set up the bus space and bus DMA tags.  */
    123 	dev->bst = pa->pa_memt;
    124 	/* XXX Let the driver say something about 32-bit vs 64-bit DMA?  */
    125 	dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
    126 	dev->dmat = dev->bus_dmat;
    127 	dev->dmat_subregion_p = false;
    128 
    129 	/* Find all the memory maps.  */
    130 	CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
    131 	dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
    132 	    sizeof(dev->bus_maps[0]), KM_SLEEP);
    133 	dev->bus_nmaps = PCI_NUM_RESOURCES;
    134 	for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
    135 		struct drm_bus_map *const bm = &dev->bus_maps[unit];
    136 		const int reg = PCI_BAR(unit);
    137 		const pcireg_t type =
    138 		    pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
    139 
    140 		/* Reject non-memory mappings.  */
    141 		if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
    142 			aprint_debug_dev(self, "map %u has non-memory type:"
    143 			    " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
    144 			continue;
    145 		}
    146 
    147 		/* Inquire about it.  We'll map it in drm_legacy_ioremap.  */
    148 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
    149 			&bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
    150 			aprint_debug_dev(self, "map %u failed\n", unit);
    151 			continue;
    152 		}
    153 
    154 		/* Assume since it is a memory mapping it can be linear.  */
    155 		bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
    156 	}
    157 
    158 	/* Set up AGP stuff if requested.  */
    159 	if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
    160 		if (drm_pci_device_is_agp(dev))
    161 			dev->agp = drm_agp_init(dev);
    162 		if (dev->agp)
    163 			dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
    164 				dev->agp->agp_info.aki_info.ai_aperture_size);
    165 	}
    166 
    167 	/* Register the DRM device and do driver-specific initialization.  */
    168 	ret = drm_dev_register(dev, cookie);
    169 	if (ret)
    170 		goto fail1;
    171 
    172 	/* Success!  */
    173 	*devp = dev;
    174 	return 0;
    175 
    176 fail2: __unused
    177 	drm_dev_unregister(dev);
    178 fail1:	drm_pci_agp_destroy(dev);
    179 	dev->bus_nmaps = 0;
    180 	kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
    181 	if (dev->dmat_subregion_p)
    182 		bus_dmatag_destroy(dev->dmat);
    183 	drm_dev_unref(dev);
    184 fail0:	return ret;
    185 }
    186 
    187 int
    188 drm_pci_detach(struct drm_device *dev, int flags __unused)
    189 {
    190 
    191 	/* Do driver-specific detachment and unregister the device.  */
    192 	drm_dev_unregister(dev);
    193 
    194 	/* Tear down AGP stuff if necessary.  */
    195 	drm_pci_agp_destroy(dev);
    196 
    197 	/* Free the record of available bus space mappings.  */
    198 	dev->bus_nmaps = 0;
    199 	kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
    200 
    201 	/* Tear down bus space and bus DMA tags.  */
    202 	if (dev->dmat_subregion_p)
    203 		bus_dmatag_destroy(dev->dmat);
    204 
    205 	drm_dev_unref(dev);
    206 
    207 	return 0;
    208 }
    209 
    210 void
    211 drm_pci_agp_destroy(struct drm_device *dev)
    212 {
    213 
    214 	if (dev->agp) {
    215 		arch_phys_wc_del(dev->agp->agp_mtrr);
    216 		drm_agp_clear(dev);
    217 		kfree(dev->agp); /* XXX Should go in drm_agp_clear...  */
    218 		dev->agp = NULL;
    219 	}
    220 }
    221 
    222 static int
    223 drm_pci_get_irq(struct drm_device *dev)
    224 {
    225 
    226 	/*
    227 	 * Caller expects a nonzero int, and doesn't really use it for
    228 	 * anything, so no need to pci_intr_map here.
    229 	 */
    230 	return dev->pdev->pd_pa.pa_intrpin;
    231 }
    232 
    233 static int
    234 drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
    235     int flags, const char *name, void *arg, struct drm_bus_irq_cookie **cookiep)
    236 {
    237 	const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
    238 	const char *intrstr;
    239 	char intrbuf[PCI_INTRSTR_LEN];
    240 	struct drm_bus_irq_cookie *irq_cookie;
    241 
    242 	irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP);
    243 
    244 	if (dev->pdev->msi_enabled) {
    245 		if (dev->pdev->pd_intr_handles == NULL) {
    246 			if (pci_msi_alloc_exact(pa, &irq_cookie->intr_handles,
    247 			    1)) {
    248 				aprint_error_dev(dev->dev,
    249 				    "couldn't allocate MSI (%s)\n", name);
    250 				goto error;
    251 			}
    252 		} else {
    253 			irq_cookie->intr_handles = dev->pdev->pd_intr_handles;
    254 			dev->pdev->pd_intr_handles = NULL;
    255 		}
    256 	} else {
    257 		if (pci_intx_alloc(pa, &irq_cookie->intr_handles)) {
    258 			aprint_error_dev(dev->dev,
    259 			    "couldn't allocate INTx interrupt (%s)\n", name);
    260 			goto error;
    261 		}
    262 	}
    263 
    264 	intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0],
    265 	    intrbuf, sizeof(intrbuf));
    266 	irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc,
    267 	    irq_cookie->intr_handles[0], IPL_DRM, handler, arg, name);
    268 	if (irq_cookie->ih_cookie == NULL) {
    269 		aprint_error_dev(dev->dev,
    270 		    "couldn't establish interrupt at %s (%s)\n", intrstr, name);
    271 		pci_intr_release(pa->pa_pc, irq_cookie->intr_handles, 1);
    272 		goto error;
    273 	}
    274 
    275 	aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name);
    276 	*cookiep = irq_cookie;
    277 	return 0;
    278 
    279 error:
    280 	kmem_free(irq_cookie, sizeof(*irq_cookie));
    281 	return -ENOENT;
    282 }
    283 
    284 static void
    285 drm_pci_irq_uninstall(struct drm_device *dev, struct drm_bus_irq_cookie *cookie)
    286 {
    287 	const struct pci_attach_args *pa = drm_pci_attach_args(dev);
    288 
    289 	pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie);
    290 	pci_intr_release(pa->pa_pc, cookie->intr_handles, 1);
    291 	kmem_free(cookie, sizeof(*cookie));
    292 }
    293 
    294 static const char *
    295 drm_pci_get_name(struct drm_device *dev)
    296 {
    297 	return "pci";		/* XXX PCI bus names?  */
    298 }
    299 
    300 static int
    301 drm_pci_format_unique(struct drm_device *dev, char *buf, size_t size)
    302 {
    303 	const unsigned int domain = device_unit(device_parent(dev->dev));
    304 	const unsigned int bus = dev->pdev->pd_pa.pa_bus;
    305 	const unsigned int device = dev->pdev->pd_pa.pa_device;
    306 	const unsigned int function = dev->pdev->pd_pa.pa_function;
    307 
    308 	return snprintf(buf, size, "pci:%04x:%02x:%02x.%d",
    309 	    domain, bus, device, function);
    310 }
    311 
    312 static int
    313 drm_pci_format_devname(struct drm_device *dev, const char *unique,
    314     char *buf, size_t size)
    315 {
    316 
    317 	return snprintf(buf, size, "%s@%s",
    318 	    device_xname(device_parent(dev->dev)),
    319 	    unique);
    320 }
    321 
    322 int
    323 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
    324 {
    325 	int n;
    326 	char *buf;
    327 
    328 	n = drm_pci_format_unique(dev, NULL, 0);
    329 	if (n < 0)
    330 		return -ENOSPC;	/* XXX */
    331 	if (0xff < n)
    332 		n = 0xff;
    333 
    334 	buf = kzalloc(n + 1, GFP_KERNEL);
    335 	(void)drm_pci_format_unique(dev, buf, n + 1);
    336 
    337 	if (master->unique)
    338 		kfree(master->unique);
    339 	master->unique = buf;
    340 	master->unique_len = n;
    341 	master->unique_size = n + 1;
    342 
    343 	n = drm_pci_format_devname(dev, master->unique, NULL, 0);
    344 	if (n < 0)
    345 		return -ENOSPC;	/* XXX back out? */
    346 	if (0xff < n)
    347 		n = 0xff;
    348 
    349 	buf = kzalloc(n + 1, GFP_KERNEL);
    350 	(void)drm_pci_format_devname(dev, master->unique, buf, n + 1);
    351 
    352 	if (dev->devname)
    353 		kfree(dev->devname);
    354 	dev->devname = buf;
    355 
    356 	return 0;
    357 }
    358 
    359 static int
    360 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
    361     struct drm_unique *unique __unused)
    362 {
    363 
    364 	/*
    365 	 * XXX This is silly.  We're supposed to reject unique names
    366 	 * that don't match the ones we would generate anyway.  For
    367 	 * expedience, we'll just generate the one we would and ignore
    368 	 * whatever userland threw at us...
    369 	 */
    370 	return drm_pci_set_busid(dev, master);
    371 }
    372 
    373 static int
    374 drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
    375 {
    376 	return -ENOSYS;		/* XXX */
    377 }
    378