Home | History | Annotate | Line # | Download | only in i915drm
intel_gtt_subr.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: intel_gtt_subr.c,v 1.1 2021/12/19 11:45:01 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad /* Intel GTT stubs */
     33  1.1  riastrad 
     34  1.1  riastrad #include <sys/cdefs.h>
     35  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_gtt_subr.c,v 1.1 2021/12/19 11:45:01 riastradh Exp $");
     36  1.1  riastrad 
     37  1.1  riastrad #include <sys/types.h>
     38  1.1  riastrad #include <sys/bus.h>
     39  1.1  riastrad #include <sys/errno.h>
     40  1.1  riastrad #include <sys/systm.h>
     41  1.1  riastrad 
     42  1.1  riastrad #include <machine/vmparam.h>
     43  1.1  riastrad 
     44  1.1  riastrad #include <dev/pci/pcivar.h>		/* XXX agpvar.h needs...  */
     45  1.1  riastrad #include <dev/pci/agpvar.h>
     46  1.1  riastrad #include <dev/pci/agp_i810var.h>
     47  1.1  riastrad 
     48  1.1  riastrad #include <linux/scatterlist.h>
     49  1.1  riastrad 
     50  1.1  riastrad #include "drm/intel-gtt.h"
     51  1.1  riastrad 
     52  1.1  riastrad /* Access to this should be single-threaded.  */
     53  1.1  riastrad static struct {
     54  1.1  riastrad 	bus_dma_segment_t	scratch_seg;
     55  1.1  riastrad 	bus_dmamap_t		scratch_map;
     56  1.1  riastrad } intel_gtt;
     57  1.1  riastrad 
     58  1.1  riastrad struct resource intel_graphics_stolen_res;
     59  1.1  riastrad 
     60  1.1  riastrad void
     61  1.1  riastrad intel_gtt_get(uint64_t *va_size, bus_addr_t *aper_base, uint64_t *aper_size)
     62  1.1  riastrad {
     63  1.1  riastrad 	struct agp_softc *const sc = agp_i810_sc;
     64  1.1  riastrad 
     65  1.1  riastrad 	if (sc == NULL) {
     66  1.1  riastrad 		*va_size = 0;
     67  1.1  riastrad 		*aper_base = 0;
     68  1.1  riastrad 		*aper_size = 0;
     69  1.1  riastrad 		return;
     70  1.1  riastrad 	}
     71  1.1  riastrad 
     72  1.1  riastrad 	struct agp_i810_softc *const isc = sc->as_chipc;
     73  1.1  riastrad 	*va_size = ((size_t)(isc->gtt_size/sizeof(uint32_t)) << PAGE_SHIFT);
     74  1.1  riastrad 	*aper_base = sc->as_apaddr;
     75  1.1  riastrad 	*aper_size = sc->as_apsize;
     76  1.1  riastrad 
     77  1.1  riastrad #ifdef notyet
     78  1.1  riastrad 	intel_graphics_stolen_res.base = ...;
     79  1.1  riastrad 	intel_graphics_stolen_res.size = isc->stolen;
     80  1.1  riastrad #endif
     81  1.1  riastrad }
     82  1.1  riastrad 
     83  1.1  riastrad int
     84  1.1  riastrad intel_gmch_probe(struct pci_dev *bridge_pci __unused,
     85  1.1  riastrad     struct pci_dev *gpu __unused, struct agp_bridge_data *bridge_agp __unused)
     86  1.1  riastrad {
     87  1.1  riastrad 	struct agp_softc *const sc = agp_i810_sc;
     88  1.1  riastrad 	int nsegs;
     89  1.1  riastrad 	int error;
     90  1.1  riastrad 
     91  1.1  riastrad 	if (sc == NULL)
     92  1.1  riastrad 		return 0;
     93  1.1  riastrad 
     94  1.1  riastrad 	error = bus_dmamem_alloc(sc->as_dmat, PAGE_SIZE, PAGE_SIZE, 0,
     95  1.1  riastrad 	    &intel_gtt.scratch_seg, 1, &nsegs, BUS_DMA_WAITOK);
     96  1.1  riastrad 	if (error)
     97  1.1  riastrad 		goto fail0;
     98  1.1  riastrad 	KASSERT(nsegs == 1);
     99  1.1  riastrad 
    100  1.1  riastrad 	error = bus_dmamap_create(sc->as_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
    101  1.1  riastrad 	    BUS_DMA_WAITOK, &intel_gtt.scratch_map);
    102  1.1  riastrad 	if (error)
    103  1.1  riastrad 		goto fail1;
    104  1.1  riastrad 
    105  1.1  riastrad 	error = bus_dmamap_load_raw(sc->as_dmat, intel_gtt.scratch_map,
    106  1.1  riastrad 	    &intel_gtt.scratch_seg, 1, PAGE_SIZE, BUS_DMA_WAITOK);
    107  1.1  riastrad 	if (error)
    108  1.1  riastrad 		goto fail2;
    109  1.1  riastrad 
    110  1.1  riastrad 	/* Success!  */
    111  1.1  riastrad 	return 1;
    112  1.1  riastrad 
    113  1.1  riastrad fail3: __unused
    114  1.1  riastrad 	bus_dmamap_unload(sc->as_dmat, intel_gtt.scratch_map);
    115  1.1  riastrad fail2:	bus_dmamap_destroy(sc->as_dmat, intel_gtt.scratch_map);
    116  1.1  riastrad fail1:	bus_dmamem_free(sc->as_dmat, &intel_gtt.scratch_seg, 1);
    117  1.1  riastrad fail0:	KASSERT(error);
    118  1.1  riastrad 	return 0;
    119  1.1  riastrad }
    120  1.1  riastrad 
    121  1.1  riastrad void
    122  1.1  riastrad intel_gmch_remove(void)
    123  1.1  riastrad {
    124  1.1  riastrad 	struct agp_softc *const sc = agp_i810_sc;
    125  1.1  riastrad 
    126  1.1  riastrad 	bus_dmamap_unload(sc->as_dmat, intel_gtt.scratch_map);
    127  1.1  riastrad 	bus_dmamap_destroy(sc->as_dmat, intel_gtt.scratch_map);
    128  1.1  riastrad 	bus_dmamem_free(sc->as_dmat, &intel_gtt.scratch_seg, 1);
    129  1.1  riastrad }
    130  1.1  riastrad 
    131  1.1  riastrad bool
    132  1.1  riastrad intel_enable_gtt(void)
    133  1.1  riastrad {
    134  1.1  riastrad 	struct agp_softc *sc = agp_i810_sc;
    135  1.1  riastrad 	struct agp_i810_softc *isc;
    136  1.1  riastrad 
    137  1.1  riastrad 	if (sc == NULL)
    138  1.1  riastrad 		return false;
    139  1.1  riastrad 	isc = sc->as_chipc;
    140  1.1  riastrad 	agp_i810_reset(isc);
    141  1.1  riastrad 	return true;
    142  1.1  riastrad }
    143  1.1  riastrad 
    144  1.1  riastrad void
    145  1.1  riastrad intel_gtt_chipset_flush(void)
    146  1.1  riastrad {
    147  1.1  riastrad 
    148  1.1  riastrad 	KASSERT(agp_i810_sc != NULL);
    149  1.1  riastrad 	agp_i810_chipset_flush(agp_i810_sc->as_chipc);
    150  1.1  riastrad }
    151  1.1  riastrad 
    152  1.1  riastrad static int
    153  1.1  riastrad intel_gtt_flags(unsigned flags)
    154  1.1  riastrad {
    155  1.1  riastrad 	int gtt_flags = AGP_I810_GTT_VALID;
    156  1.1  riastrad 
    157  1.1  riastrad 	switch (flags) {
    158  1.1  riastrad 	case AGP_USER_MEMORY:
    159  1.1  riastrad 		break;
    160  1.1  riastrad 	case AGP_USER_CACHED_MEMORY:
    161  1.1  riastrad 		gtt_flags |= AGP_I810_GTT_CACHED;
    162  1.1  riastrad 		break;
    163  1.1  riastrad 	default:
    164  1.1  riastrad 		panic("invalid intel gtt flags: %x", flags);
    165  1.1  riastrad 	}
    166  1.1  riastrad 
    167  1.1  riastrad 	return gtt_flags;
    168  1.1  riastrad }
    169  1.1  riastrad 
    170  1.1  riastrad void
    171  1.1  riastrad intel_gtt_insert_page(bus_addr_t addr, unsigned va_page, unsigned flags)
    172  1.1  riastrad {
    173  1.1  riastrad 	struct agp_i810_softc *const isc = agp_i810_sc->as_chipc;
    174  1.1  riastrad 	off_t va = (off_t)va_page << PAGE_SHIFT;
    175  1.1  riastrad 	int gtt_flags = intel_gtt_flags(flags);
    176  1.1  riastrad 	int error;
    177  1.1  riastrad 
    178  1.1  riastrad 	error = agp_i810_write_gtt_entry(isc, va, addr, gtt_flags);
    179  1.1  riastrad 	if (error)
    180  1.1  riastrad 		device_printf(agp_i810_sc->as_dev,
    181  1.1  riastrad 		    "write gtt entry"
    182  1.1  riastrad 		    " %"PRIxMAX" -> %"PRIxMAX" (flags=%x) failed: %d\n",
    183  1.1  riastrad 		    (uintmax_t)va, (uintmax_t)addr, flags,
    184  1.1  riastrad 		    error);
    185  1.1  riastrad 	agp_i810_post_gtt_entry(isc, va);
    186  1.1  riastrad 	intel_gtt_chipset_flush();
    187  1.1  riastrad }
    188  1.1  riastrad 
    189  1.1  riastrad void
    190  1.1  riastrad intel_gtt_insert_sg_entries(struct sg_table *sg, unsigned va_page,
    191  1.1  riastrad     unsigned flags)
    192  1.1  riastrad {
    193  1.1  riastrad 	bus_dmamap_t dmamap = sg->sgl[0].sg_dmamap;
    194  1.1  riastrad 	struct agp_i810_softc *const isc = agp_i810_sc->as_chipc;
    195  1.1  riastrad 	off_t va = (off_t)va_page << PAGE_SHIFT;
    196  1.1  riastrad 	unsigned seg;
    197  1.1  riastrad 	int gtt_flags = intel_gtt_flags(flags);
    198  1.1  riastrad 	int error;
    199  1.1  riastrad 
    200  1.1  riastrad 	KASSERT(0 <= va);
    201  1.1  riastrad 	KASSERT((va >> PAGE_SHIFT) == va_page);
    202  1.1  riastrad 	KASSERT(0 < dmamap->dm_nsegs);
    203  1.1  riastrad 
    204  1.1  riastrad 	for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    205  1.1  riastrad 		const bus_addr_t addr = dmamap->dm_segs[seg].ds_addr;
    206  1.1  riastrad 		bus_size_t len;
    207  1.1  riastrad 
    208  1.1  riastrad 		for (len = dmamap->dm_segs[seg].ds_len;
    209  1.1  riastrad 		     len >= PAGE_SIZE;
    210  1.1  riastrad 		     len -= PAGE_SIZE, va += PAGE_SIZE) {
    211  1.1  riastrad 			error = agp_i810_write_gtt_entry(isc, va, addr,
    212  1.1  riastrad 			    gtt_flags);
    213  1.1  riastrad 			if (error)
    214  1.1  riastrad 				device_printf(agp_i810_sc->as_dev,
    215  1.1  riastrad 				    "write gtt entry"
    216  1.1  riastrad 				    " %"PRIxMAX" -> %"PRIxMAX" (flags=%x)"
    217  1.1  riastrad 				    " failed: %d\n",
    218  1.1  riastrad 				    (uintmax_t)va, (uintmax_t)addr, flags,
    219  1.1  riastrad 				    error);
    220  1.1  riastrad 		}
    221  1.1  riastrad 		KASSERTMSG(len == 0,
    222  1.1  riastrad 		    "segment length not divisible by PAGE_SIZE: %jx",
    223  1.1  riastrad 		    (uintmax_t)dmamap->dm_segs[seg].ds_len);
    224  1.1  riastrad 	}
    225  1.1  riastrad 	agp_i810_post_gtt_entry(isc, (va - PAGE_SIZE));
    226  1.1  riastrad 	intel_gtt_chipset_flush();
    227  1.1  riastrad }
    228  1.1  riastrad 
    229  1.1  riastrad void
    230  1.1  riastrad intel_gtt_clear_range(unsigned va_page, unsigned npages)
    231  1.1  riastrad {
    232  1.1  riastrad 	struct agp_i810_softc *const isc = agp_i810_sc->as_chipc;
    233  1.1  riastrad 	const bus_addr_t addr = intel_gtt.scratch_map->dm_segs[0].ds_addr;
    234  1.1  riastrad 	const int gtt_flags = AGP_I810_GTT_VALID;
    235  1.1  riastrad 	off_t va = (va_page << PAGE_SHIFT);
    236  1.1  riastrad 
    237  1.1  riastrad 	KASSERT(0 <= va);
    238  1.1  riastrad 	KASSERT((va >> PAGE_SHIFT) == va_page);
    239  1.1  riastrad 	KASSERT(0 < npages);
    240  1.1  riastrad 
    241  1.1  riastrad 	while (npages--) {
    242  1.1  riastrad 		agp_i810_write_gtt_entry(isc, va, addr, gtt_flags);
    243  1.1  riastrad 		va += PAGE_SIZE;
    244  1.1  riastrad 	}
    245  1.1  riastrad 	agp_i810_post_gtt_entry(isc, va - PAGE_SIZE);
    246  1.1  riastrad }
    247