Home | History | Annotate | Line # | Download | only in ttm
ttm_agp_backend.c revision 1.5.4.1
      1  1.5.4.1    bouyer /*	$NetBSD: ttm_agp_backend.c,v 1.5.4.1 2017/04/21 16:54:00 bouyer 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 #include <sys/cdefs.h>
     33  1.5.4.1    bouyer __KERNEL_RCSID(0, "$NetBSD: ttm_agp_backend.c,v 1.5.4.1 2017/04/21 16:54:00 bouyer Exp $");
     34      1.1  riastrad 
     35      1.1  riastrad #include <sys/types.h>
     36      1.1  riastrad #include <sys/kmem.h>
     37      1.1  riastrad 
     38      1.1  riastrad #include <dev/pci/pcireg.h>
     39      1.1  riastrad #include <dev/pci/pcivar.h>
     40      1.1  riastrad #include <dev/pci/agpvar.h>
     41      1.1  riastrad 
     42      1.1  riastrad #include <ttm/ttm_bo_driver.h>
     43      1.1  riastrad #include <ttm/ttm_page_alloc.h>
     44      1.1  riastrad 
     45      1.5  jmcneill #if __OS_HAS_AGP
     46      1.5  jmcneill 
     47      1.1  riastrad struct ttm_agp {
     48      1.1  riastrad 	struct ttm_dma_tt ttm_dma;
     49      1.1  riastrad 	struct agp_softc *agp;
     50      1.1  riastrad 	unsigned long agp_pgno;
     51      1.1  riastrad 	bool bound;
     52      1.1  riastrad };
     53      1.1  riastrad 
     54      1.1  riastrad static const struct ttm_backend_func ttm_agp_backend_func;
     55      1.1  riastrad 
     56      1.1  riastrad struct ttm_tt *
     57      1.1  riastrad ttm_agp_tt_create(struct ttm_bo_device *bdev, struct agp_bridge_data *bridge,
     58      1.1  riastrad     unsigned long size, uint32_t page_flags, struct page *dummy_read_page)
     59      1.1  riastrad {
     60      1.1  riastrad 	struct ttm_agp *ttm_agp;
     61      1.1  riastrad 
     62      1.3  riastrad 	ttm_agp = kmem_zalloc(sizeof(*ttm_agp), KM_SLEEP);
     63      1.1  riastrad 	ttm_agp->agp = &bridge->abd_sc;
     64      1.1  riastrad 	ttm_agp->ttm_dma.ttm.func = &ttm_agp_backend_func;
     65      1.1  riastrad 
     66      1.2  riastrad 	if (ttm_dma_tt_init(&ttm_agp->ttm_dma, bdev, size, page_flags,
     67      1.1  riastrad 		dummy_read_page) != 0)
     68      1.1  riastrad 		goto fail;
     69      1.1  riastrad 
     70      1.1  riastrad 	/* Success!  */
     71      1.1  riastrad 	return &ttm_agp->ttm_dma.ttm;
     72      1.1  riastrad 
     73      1.1  riastrad fail:	kmem_free(ttm_agp, sizeof(*ttm_agp));
     74      1.1  riastrad 	return NULL;
     75      1.1  riastrad }
     76      1.1  riastrad 
     77      1.1  riastrad int
     78      1.1  riastrad ttm_agp_tt_populate(struct ttm_tt *ttm)
     79      1.1  riastrad {
     80      1.1  riastrad 
     81  1.5.4.1    bouyer 	KASSERTMSG((ttm->state == tt_unpopulated),
     82  1.5.4.1    bouyer 	    "ttm_agp_tt_populate: ttm %p state is not tt_unpopulated: %d",
     83  1.5.4.1    bouyer 	    ttm, (int)ttm->state);
     84      1.1  riastrad 	return ttm_bus_dma_populate(container_of(ttm, struct ttm_dma_tt, ttm));
     85      1.1  riastrad }
     86      1.1  riastrad 
     87      1.1  riastrad void
     88      1.1  riastrad ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
     89      1.1  riastrad {
     90      1.1  riastrad 
     91  1.5.4.1    bouyer 	KASSERTMSG((ttm->state == tt_unbound),
     92  1.5.4.1    bouyer 	    "ttm_agp_tt_unpopulate: ttm %p state is not tt_unbound: %d",
     93  1.5.4.1    bouyer 	    ttm, (int)ttm->state);
     94      1.1  riastrad 	ttm_bus_dma_unpopulate(container_of(ttm, struct ttm_dma_tt, ttm));
     95      1.1  riastrad }
     96      1.1  riastrad 
     97      1.1  riastrad static int
     98      1.1  riastrad ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
     99      1.1  riastrad {
    100      1.1  riastrad 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    101      1.1  riastrad 	    ttm_dma.ttm);
    102      1.1  riastrad 	struct agp_softc *const sc = ttm_agp->agp;
    103      1.1  riastrad 	struct drm_mm_node *const node = bo_mem->mm_node;
    104      1.1  riastrad 	const unsigned long agp_pgno = node->start;
    105      1.1  riastrad 	unsigned i, j;
    106      1.1  riastrad 	int ret;
    107      1.1  riastrad 
    108      1.1  riastrad 	KASSERT(!ttm_agp->bound);
    109      1.1  riastrad 	KASSERT(ttm_agp->ttm_dma.dma_address->dm_nsegs == ttm->num_pages);
    110      1.1  riastrad 	for (i = 0; i < ttm->num_pages; i++) {
    111      1.1  riastrad 		KASSERT(ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len ==
    112      1.1  riastrad 		    AGP_PAGE_SIZE);
    113      1.1  riastrad 		/* XXX errno NetBSD->Linux */
    114      1.1  riastrad 		ret = -AGP_BIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT,
    115      1.1  riastrad 		    ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len);
    116      1.1  riastrad 		if (ret)
    117      1.1  riastrad 			goto fail;
    118      1.1  riastrad 	}
    119      1.1  riastrad 	agp_flush_cache();
    120      1.1  riastrad 	AGP_FLUSH_TLB(sc);
    121      1.1  riastrad 
    122      1.1  riastrad 	/* Success!  */
    123      1.1  riastrad 	ttm_agp->agp_pgno = agp_pgno;
    124      1.1  riastrad 	ttm_agp->bound = true;
    125      1.1  riastrad 	return 0;
    126      1.1  riastrad 
    127      1.1  riastrad fail:	KASSERT(ret);
    128      1.1  riastrad 	for (j = 0; j < i; j++)
    129      1.1  riastrad 		(void)AGP_UNBIND_PAGE(sc, (agp_pgno + j) << AGP_PAGE_SHIFT);
    130      1.1  riastrad 	return ret;
    131      1.1  riastrad }
    132      1.1  riastrad 
    133      1.1  riastrad static int
    134      1.1  riastrad ttm_agp_unbind(struct ttm_tt *ttm)
    135      1.1  riastrad {
    136      1.1  riastrad 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    137      1.1  riastrad 	    ttm_dma.ttm);
    138      1.1  riastrad 	struct agp_softc *const sc = ttm_agp->agp;
    139      1.1  riastrad 	const unsigned long agp_pgno = ttm_agp->agp_pgno;
    140      1.1  riastrad 	unsigned i;
    141      1.1  riastrad 
    142      1.1  riastrad 	/* XXX Can this happen?  */
    143      1.1  riastrad 	if (!ttm_agp->bound)
    144      1.1  riastrad 		return 0;
    145      1.1  riastrad 
    146      1.1  riastrad 	for (i = 0; i < ttm->num_pages; i++)
    147      1.1  riastrad 		(void)AGP_UNBIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT);
    148      1.1  riastrad 
    149      1.1  riastrad 	ttm_agp->agp_pgno = 0;
    150      1.1  riastrad 	ttm_agp->bound = false;
    151      1.1  riastrad 	return 0;
    152      1.1  riastrad }
    153      1.1  riastrad 
    154      1.1  riastrad static void
    155      1.1  riastrad ttm_agp_destroy(struct ttm_tt *ttm)
    156      1.1  riastrad {
    157      1.1  riastrad 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    158      1.1  riastrad 	    ttm_dma.ttm);
    159      1.1  riastrad 
    160      1.1  riastrad 	/* XXX Can this happen?  */
    161      1.1  riastrad 	if (ttm_agp->bound)
    162      1.1  riastrad 		ttm_agp_unbind(ttm);
    163      1.1  riastrad 	ttm_tt_fini(ttm);
    164      1.4  riastrad 	kmem_free(ttm_agp, sizeof(*ttm_agp));
    165      1.1  riastrad }
    166      1.1  riastrad 
    167      1.1  riastrad static const struct ttm_backend_func ttm_agp_backend_func = {
    168      1.1  riastrad 	.bind = &ttm_agp_bind,
    169      1.1  riastrad 	.unbind = &ttm_agp_unbind,
    170      1.1  riastrad 	.destroy = &ttm_agp_destroy,
    171      1.1  riastrad };
    172      1.5  jmcneill 
    173      1.5  jmcneill #endif
    174