Home | History | Annotate | Line # | Download | only in pci
drm_pci.c revision 1.18.4.2
      1  1.18.4.2    martin /*	$NetBSD: drm_pci.c,v 1.18.4.2 2020/04/08 14:08:28 martin Exp $	*/
      2       1.2  riastrad 
      3       1.2  riastrad /*-
      4       1.2  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5       1.2  riastrad  * All rights reserved.
      6       1.2  riastrad  *
      7       1.2  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8       1.2  riastrad  * by Taylor R. Campbell.
      9       1.2  riastrad  *
     10       1.2  riastrad  * Redistribution and use in source and binary forms, with or without
     11       1.2  riastrad  * modification, are permitted provided that the following conditions
     12       1.2  riastrad  * are met:
     13       1.2  riastrad  * 1. Redistributions of source code must retain the above copyright
     14       1.2  riastrad  *    notice, this list of conditions and the following disclaimer.
     15       1.2  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17       1.2  riastrad  *    documentation and/or other materials provided with the distribution.
     18       1.2  riastrad  *
     19       1.2  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.2  riastrad  */
     31       1.2  riastrad 
     32       1.2  riastrad #include <sys/cdefs.h>
     33  1.18.4.2    martin __KERNEL_RCSID(0, "$NetBSD: drm_pci.c,v 1.18.4.2 2020/04/08 14:08:28 martin Exp $");
     34       1.2  riastrad 
     35       1.2  riastrad #include <sys/types.h>
     36       1.2  riastrad #include <sys/errno.h>
     37       1.2  riastrad #include <sys/systm.h>
     38       1.2  riastrad 
     39       1.2  riastrad #include <dev/pci/pcivar.h>
     40       1.2  riastrad 
     41       1.2  riastrad #include <drm/drmP.h>
     42  1.18.4.1  christos #include <drm/drm_legacy.h>
     43       1.2  riastrad 
     44      1.16    nonaka struct drm_bus_irq_cookie {
     45      1.16    nonaka 	pci_intr_handle_t *intr_handles;
     46      1.16    nonaka 	void *ih_cookie;
     47      1.16    nonaka };
     48      1.16    nonaka 
     49       1.2  riastrad static const struct pci_attach_args *
     50       1.2  riastrad drm_pci_attach_args(struct drm_device *dev)
     51       1.2  riastrad {
     52       1.2  riastrad 	return &dev->pdev->pd_pa;
     53       1.2  riastrad }
     54       1.2  riastrad 
     55       1.2  riastrad int
     56       1.2  riastrad drm_pci_attach(device_t self, const struct pci_attach_args *pa,
     57       1.4  riastrad     struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
     58       1.4  riastrad     struct drm_device **devp)
     59       1.2  riastrad {
     60       1.4  riastrad 	struct drm_device *dev;
     61       1.2  riastrad 	unsigned int unit;
     62       1.4  riastrad 	int ret;
     63       1.2  riastrad 
     64  1.18.4.1  christos 	/* Ensure the drm agp hooks are initialized.  */
     65      1.10  riastrad 	/* XXX errno NetBSD->Linux */
     66  1.18.4.1  christos 	ret = -drm_guarantee_initialized();
     67      1.10  riastrad 	if (ret)
     68      1.10  riastrad 		goto fail0;
     69      1.10  riastrad 
     70       1.4  riastrad 	/* Create a DRM device.  */
     71       1.4  riastrad 	dev = drm_dev_alloc(driver, self);
     72       1.4  riastrad 	if (dev == NULL) {
     73       1.4  riastrad 		ret = -ENOMEM;
     74       1.4  riastrad 		goto fail0;
     75       1.4  riastrad 	}
     76       1.4  riastrad 
     77       1.2  riastrad 	dev->pdev = pdev;
     78      1.11  riastrad 	pdev->pd_drm_dev = dev;	/* XXX Nouveau kludge.  */
     79       1.2  riastrad 
     80       1.2  riastrad 	/* XXX Set the power state to D0?  */
     81       1.2  riastrad 
     82       1.4  riastrad 	/* Set up the bus space and bus DMA tags.  */
     83       1.2  riastrad 	dev->bst = pa->pa_memt;
     84       1.2  riastrad 	dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
     85  1.18.4.1  christos 	dev->bus_dmat32 = pa->pa_dmat;
     86       1.2  riastrad 	dev->dmat = dev->bus_dmat;
     87       1.2  riastrad 	dev->dmat_subregion_p = false;
     88  1.18.4.1  christos 	dev->dmat_subregion_min = 0;
     89  1.18.4.1  christos 	dev->dmat_subregion_max = __type_max(bus_addr_t);
     90       1.2  riastrad 
     91       1.4  riastrad 	/* Find all the memory maps.  */
     92       1.4  riastrad 	CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
     93       1.4  riastrad 	dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
     94       1.4  riastrad 	    sizeof(dev->bus_maps[0]), KM_SLEEP);
     95       1.4  riastrad 	dev->bus_nmaps = PCI_NUM_RESOURCES;
     96       1.4  riastrad 	for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
     97       1.2  riastrad 		struct drm_bus_map *const bm = &dev->bus_maps[unit];
     98       1.2  riastrad 		const int reg = PCI_BAR(unit);
     99       1.2  riastrad 		const pcireg_t type =
    100       1.2  riastrad 		    pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
    101       1.2  riastrad 
    102       1.2  riastrad 		/* Reject non-memory mappings.  */
    103       1.2  riastrad 		if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
    104       1.2  riastrad 			aprint_debug_dev(self, "map %u has non-memory type:"
    105       1.2  riastrad 			    " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
    106       1.2  riastrad 			continue;
    107       1.2  riastrad 		}
    108       1.2  riastrad 
    109  1.18.4.1  christos 		/*
    110  1.18.4.1  christos 		 * If it's a 64-bit mapping, don't interpret the second
    111  1.18.4.1  christos 		 * half of it as another BAR in the next iteration of
    112  1.18.4.1  christos 		 * the loop -- move on to the next unit.
    113  1.18.4.1  christos 		 */
    114  1.18.4.1  christos 		if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
    115  1.18.4.1  christos 			unit++;
    116  1.18.4.1  christos 
    117  1.18.4.1  christos 		/* Inquire about it.  We'll map it in drm_legacy_ioremap.  */
    118       1.2  riastrad 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
    119       1.2  riastrad 			&bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
    120       1.2  riastrad 			aprint_debug_dev(self, "map %u failed\n", unit);
    121       1.2  riastrad 			continue;
    122       1.2  riastrad 		}
    123       1.2  riastrad 
    124       1.2  riastrad 		/* Assume since it is a memory mapping it can be linear.  */
    125       1.2  riastrad 		bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
    126       1.2  riastrad 	}
    127       1.2  riastrad 
    128       1.4  riastrad 	/* Set up AGP stuff if requested.  */
    129       1.4  riastrad 	if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
    130       1.4  riastrad 		if (drm_pci_device_is_agp(dev))
    131       1.4  riastrad 			dev->agp = drm_agp_init(dev);
    132       1.4  riastrad 		if (dev->agp)
    133       1.4  riastrad 			dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
    134       1.4  riastrad 				dev->agp->agp_info.aki_info.ai_aperture_size);
    135       1.4  riastrad 	}
    136       1.4  riastrad 
    137       1.4  riastrad 	/* Register the DRM device and do driver-specific initialization.  */
    138       1.4  riastrad 	ret = drm_dev_register(dev, cookie);
    139       1.4  riastrad 	if (ret)
    140       1.4  riastrad 		goto fail1;
    141       1.4  riastrad 
    142       1.4  riastrad 	/* Success!  */
    143       1.4  riastrad 	*devp = dev;
    144       1.4  riastrad 	return 0;
    145       1.4  riastrad 
    146       1.4  riastrad fail2: __unused
    147       1.4  riastrad 	drm_dev_unregister(dev);
    148       1.4  riastrad fail1:	drm_pci_agp_destroy(dev);
    149       1.4  riastrad 	dev->bus_nmaps = 0;
    150       1.4  riastrad 	kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
    151  1.18.4.1  christos 	if (dev->dmat_subregion_p) {
    152       1.5  riastrad 		bus_dmatag_destroy(dev->dmat);
    153  1.18.4.1  christos 	}
    154       1.4  riastrad 	drm_dev_unref(dev);
    155       1.4  riastrad fail0:	return ret;
    156       1.2  riastrad }
    157       1.2  riastrad 
    158       1.2  riastrad int
    159       1.4  riastrad drm_pci_detach(struct drm_device *dev, int flags __unused)
    160       1.2  riastrad {
    161       1.2  riastrad 
    162       1.4  riastrad 	/* Do driver-specific detachment and unregister the device.  */
    163       1.4  riastrad 	drm_dev_unregister(dev);
    164       1.4  riastrad 
    165       1.4  riastrad 	/* Tear down AGP stuff if necessary.  */
    166       1.8  riastrad 	drm_pci_agp_destroy(dev);
    167       1.4  riastrad 
    168       1.4  riastrad 	/* Free the record of available bus space mappings.  */
    169       1.4  riastrad 	dev->bus_nmaps = 0;
    170       1.4  riastrad 	kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
    171       1.2  riastrad 
    172       1.4  riastrad 	/* Tear down bus space and bus DMA tags.  */
    173  1.18.4.1  christos 	if (dev->dmat_subregion_p) {
    174       1.2  riastrad 		bus_dmatag_destroy(dev->dmat);
    175  1.18.4.1  christos 	}
    176       1.2  riastrad 
    177       1.4  riastrad 	drm_dev_unref(dev);
    178       1.4  riastrad 
    179       1.2  riastrad 	return 0;
    180       1.2  riastrad }
    181       1.2  riastrad 
    182       1.4  riastrad void
    183       1.4  riastrad drm_pci_agp_destroy(struct drm_device *dev)
    184       1.4  riastrad {
    185       1.4  riastrad 
    186       1.4  riastrad 	if (dev->agp) {
    187       1.4  riastrad 		arch_phys_wc_del(dev->agp->agp_mtrr);
    188  1.18.4.1  christos 		drm_agp_fini(dev);
    189  1.18.4.1  christos 		KASSERT(dev->agp == NULL);
    190       1.4  riastrad 	}
    191       1.4  riastrad }
    192       1.4  riastrad 
    193  1.18.4.1  christos int
    194  1.18.4.1  christos drm_pci_request_irq(struct drm_device *dev, int flags)
    195       1.2  riastrad {
    196  1.18.4.1  christos 	const char *const name = device_xname(dev->dev);
    197  1.18.4.1  christos 	int (*const handler)(void *) = dev->driver->irq_handler;
    198       1.2  riastrad 	const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
    199       1.2  riastrad 	const char *intrstr;
    200       1.3  christos 	char intrbuf[PCI_INTRSTR_LEN];
    201      1.16    nonaka 	struct drm_bus_irq_cookie *irq_cookie;
    202       1.2  riastrad 
    203      1.16    nonaka 	irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP);
    204      1.16    nonaka 
    205      1.16    nonaka 	if (dev->pdev->msi_enabled) {
    206  1.18.4.1  christos 		if (dev->pdev->pd_intr_handles == NULL) {
    207      1.18    nonaka 			if (pci_msi_alloc_exact(pa, &irq_cookie->intr_handles,
    208      1.18    nonaka 			    1)) {
    209      1.18    nonaka 				aprint_error_dev(dev->dev,
    210      1.18    nonaka 				    "couldn't allocate MSI (%s)\n", name);
    211      1.18    nonaka 				goto error;
    212      1.18    nonaka 			}
    213      1.18    nonaka 		} else {
    214  1.18.4.1  christos 			irq_cookie->intr_handles = dev->pdev->pd_intr_handles;
    215  1.18.4.1  christos 			dev->pdev->pd_intr_handles = NULL;
    216      1.18    nonaka 		}
    217      1.16    nonaka 	} else {
    218      1.18    nonaka 		if (pci_intx_alloc(pa, &irq_cookie->intr_handles)) {
    219      1.18    nonaka 			aprint_error_dev(dev->dev,
    220      1.18    nonaka 			    "couldn't allocate INTx interrupt (%s)\n", name);
    221      1.18    nonaka 			goto error;
    222      1.18    nonaka 		}
    223      1.16    nonaka 	}
    224       1.2  riastrad 
    225      1.16    nonaka 	intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0],
    226      1.16    nonaka 	    intrbuf, sizeof(intrbuf));
    227      1.16    nonaka 	irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc,
    228  1.18.4.1  christos 	    irq_cookie->intr_handles[0], IPL_DRM, handler, dev, name);
    229      1.16    nonaka 	if (irq_cookie->ih_cookie == NULL) {
    230       1.2  riastrad 		aprint_error_dev(dev->dev,
    231      1.16    nonaka 		    "couldn't establish interrupt at %s (%s)\n", intrstr, name);
    232      1.18    nonaka 		pci_intr_release(pa->pa_pc, irq_cookie->intr_handles, 1);
    233      1.18    nonaka 		goto error;
    234       1.2  riastrad 	}
    235       1.2  riastrad 
    236      1.16    nonaka 	aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name);
    237  1.18.4.1  christos 	dev->irq_cookie = irq_cookie;
    238       1.2  riastrad 	return 0;
    239      1.18    nonaka 
    240      1.18    nonaka error:
    241      1.18    nonaka 	kmem_free(irq_cookie, sizeof(*irq_cookie));
    242      1.18    nonaka 	return -ENOENT;
    243       1.2  riastrad }
    244       1.2  riastrad 
    245  1.18.4.1  christos void
    246  1.18.4.1  christos drm_pci_free_irq(struct drm_device *dev)
    247       1.2  riastrad {
    248  1.18.4.1  christos 	struct drm_bus_irq_cookie *const cookie = dev->irq_cookie;
    249       1.2  riastrad 	const struct pci_attach_args *pa = drm_pci_attach_args(dev);
    250       1.2  riastrad 
    251      1.16    nonaka 	pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie);
    252      1.16    nonaka 	pci_intr_release(pa->pa_pc, cookie->intr_handles, 1);
    253      1.16    nonaka 	kmem_free(cookie, sizeof(*cookie));
    254  1.18.4.1  christos 	dev->irq_cookie = NULL;
    255       1.2  riastrad }
    256       1.2  riastrad 
    257  1.18.4.1  christos int
    258       1.2  riastrad drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
    259       1.2  riastrad {
    260  1.18.4.1  christos 	const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
    261       1.2  riastrad 
    262  1.18.4.1  christos 	master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
    263  1.18.4.1  christos 	    device_unit(device_parent(dev->dev)),
    264  1.18.4.1  christos 	    pa->pa_bus, pa->pa_device, pa->pa_function);
    265  1.18.4.1  christos 	if (master->unique == NULL)
    266  1.18.4.1  christos 		return -ENOMEM;
    267  1.18.4.1  christos 	master->unique_len = strlen(master->unique);
    268       1.2  riastrad 
    269       1.2  riastrad 	return 0;
    270       1.2  riastrad }
    271       1.2  riastrad 
    272  1.18.4.1  christos int
    273       1.2  riastrad drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
    274  1.18.4.1  christos     struct drm_unique *unique)
    275       1.2  riastrad {
    276  1.18.4.1  christos 	char kbuf[64], ubuf[64];
    277  1.18.4.1  christos 	int ret;
    278       1.2  riastrad 
    279  1.18.4.1  christos 	/* Reject excessively long unique strings.  */
    280  1.18.4.1  christos 	if (unique->unique_len > sizeof(ubuf) - 1)
    281  1.18.4.1  christos 		return -EINVAL;
    282       1.2  riastrad 
    283  1.18.4.1  christos 	/* Copy in the alleged unique string, NUL-terminated.  */
    284  1.18.4.1  christos 	ret = -copyin(unique->unique, ubuf, unique->unique_len);
    285  1.18.4.1  christos 	if (ret)
    286  1.18.4.1  christos 		return ret;
    287  1.18.4.1  christos 	ubuf[unique->unique_len] = '\0';
    288  1.18.4.1  christos 
    289  1.18.4.1  christos 	/* Make sure it matches what we expect.  */
    290  1.18.4.1  christos 	snprintf(kbuf, sizeof kbuf, "PCI:%d:%ld:%ld", dev->pdev->bus->number,
    291  1.18.4.1  christos 	    (long)PCI_SLOT(dev->pdev->devfn),
    292  1.18.4.1  christos 	    (long)PCI_FUNC(dev->pdev->devfn));
    293  1.18.4.1  christos 	if (strncmp(kbuf, ubuf, sizeof(kbuf)) != 0)
    294  1.18.4.1  christos 		return -EINVAL;
    295  1.18.4.1  christos 
    296  1.18.4.1  christos 	/* Remember it.  */
    297  1.18.4.1  christos 	master->unique = kstrdup(ubuf, GFP_KERNEL);
    298  1.18.4.1  christos 	master->unique_len = strlen(master->unique);
    299  1.18.4.1  christos 
    300  1.18.4.1  christos 	/* Success!  */
    301  1.18.4.1  christos 	return 0;
    302       1.2  riastrad }
    303  1.18.4.2    martin 
    304  1.18.4.2    martin int
    305  1.18.4.2    martin drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask)
    306  1.18.4.2    martin {
    307  1.18.4.2    martin 
    308  1.18.4.2    martin 	return -ENOSYS;
    309  1.18.4.2    martin }
    310