Home | History | Annotate | Line # | Download | only in ttm
ttm_agp_backend.c revision 1.3.2.3
      1  1.3.2.2       tls /*	$NetBSD: ttm_agp_backend.c,v 1.3.2.3 2017/12/03 11:38:01 jdolecek Exp $	*/
      2  1.3.2.2       tls 
      3  1.3.2.2       tls /*-
      4  1.3.2.2       tls  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.3.2.2       tls  * All rights reserved.
      6  1.3.2.2       tls  *
      7  1.3.2.2       tls  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.2.2       tls  * by Taylor R. Campbell.
      9  1.3.2.2       tls  *
     10  1.3.2.2       tls  * Redistribution and use in source and binary forms, with or without
     11  1.3.2.2       tls  * modification, are permitted provided that the following conditions
     12  1.3.2.2       tls  * are met:
     13  1.3.2.2       tls  * 1. Redistributions of source code must retain the above copyright
     14  1.3.2.2       tls  *    notice, this list of conditions and the following disclaimer.
     15  1.3.2.2       tls  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.2.2       tls  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.2.2       tls  *    documentation and/or other materials provided with the distribution.
     18  1.3.2.2       tls  *
     19  1.3.2.2       tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.3.2.2       tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.3.2.2       tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.3.2.2       tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.3.2.2       tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.3.2.2       tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.3.2.2       tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.3.2.2       tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.3.2.2       tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.3.2.2       tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.3.2.2       tls  * POSSIBILITY OF SUCH DAMAGE.
     30  1.3.2.2       tls  */
     31  1.3.2.2       tls 
     32  1.3.2.2       tls #include <sys/cdefs.h>
     33  1.3.2.2       tls __KERNEL_RCSID(0, "$NetBSD: ttm_agp_backend.c,v 1.3.2.3 2017/12/03 11:38:01 jdolecek Exp $");
     34  1.3.2.2       tls 
     35  1.3.2.2       tls #include <sys/types.h>
     36  1.3.2.2       tls #include <sys/kmem.h>
     37  1.3.2.2       tls 
     38  1.3.2.2       tls #include <dev/pci/pcireg.h>
     39  1.3.2.2       tls #include <dev/pci/pcivar.h>
     40  1.3.2.2       tls #include <dev/pci/agpvar.h>
     41  1.3.2.2       tls 
     42  1.3.2.2       tls #include <ttm/ttm_bo_driver.h>
     43  1.3.2.2       tls #include <ttm/ttm_page_alloc.h>
     44  1.3.2.2       tls 
     45  1.3.2.3  jdolecek #if __OS_HAS_AGP
     46  1.3.2.3  jdolecek 
     47  1.3.2.2       tls struct ttm_agp {
     48  1.3.2.2       tls 	struct ttm_dma_tt ttm_dma;
     49  1.3.2.2       tls 	struct agp_softc *agp;
     50  1.3.2.2       tls 	unsigned long agp_pgno;
     51  1.3.2.2       tls 	bool bound;
     52  1.3.2.2       tls };
     53  1.3.2.2       tls 
     54  1.3.2.2       tls static const struct ttm_backend_func ttm_agp_backend_func;
     55  1.3.2.2       tls 
     56  1.3.2.2       tls struct ttm_tt *
     57  1.3.2.2       tls ttm_agp_tt_create(struct ttm_bo_device *bdev, struct agp_bridge_data *bridge,
     58  1.3.2.2       tls     unsigned long size, uint32_t page_flags, struct page *dummy_read_page)
     59  1.3.2.2       tls {
     60  1.3.2.2       tls 	struct ttm_agp *ttm_agp;
     61  1.3.2.2       tls 
     62  1.3.2.3  jdolecek 	ttm_agp = kmem_zalloc(sizeof(*ttm_agp), KM_SLEEP);
     63  1.3.2.2       tls 	ttm_agp->agp = &bridge->abd_sc;
     64  1.3.2.2       tls 	ttm_agp->ttm_dma.ttm.func = &ttm_agp_backend_func;
     65  1.3.2.2       tls 
     66  1.3.2.3  jdolecek 	if (ttm_dma_tt_init(&ttm_agp->ttm_dma, bdev, size, page_flags,
     67  1.3.2.2       tls 		dummy_read_page) != 0)
     68  1.3.2.2       tls 		goto fail;
     69  1.3.2.2       tls 
     70  1.3.2.2       tls 	/* Success!  */
     71  1.3.2.2       tls 	return &ttm_agp->ttm_dma.ttm;
     72  1.3.2.2       tls 
     73  1.3.2.2       tls fail:	kmem_free(ttm_agp, sizeof(*ttm_agp));
     74  1.3.2.2       tls 	return NULL;
     75  1.3.2.2       tls }
     76  1.3.2.2       tls 
     77  1.3.2.2       tls int
     78  1.3.2.2       tls ttm_agp_tt_populate(struct ttm_tt *ttm)
     79  1.3.2.2       tls {
     80  1.3.2.2       tls 
     81  1.3.2.3  jdolecek 	KASSERTMSG((ttm->state == tt_unpopulated),
     82  1.3.2.3  jdolecek 	    "ttm_agp_tt_populate: ttm %p state is not tt_unpopulated: %d",
     83  1.3.2.3  jdolecek 	    ttm, (int)ttm->state);
     84  1.3.2.2       tls 	return ttm_bus_dma_populate(container_of(ttm, struct ttm_dma_tt, ttm));
     85  1.3.2.2       tls }
     86  1.3.2.2       tls 
     87  1.3.2.2       tls void
     88  1.3.2.2       tls ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
     89  1.3.2.2       tls {
     90  1.3.2.2       tls 
     91  1.3.2.3  jdolecek 	KASSERTMSG((ttm->state == tt_unbound),
     92  1.3.2.3  jdolecek 	    "ttm_agp_tt_unpopulate: ttm %p state is not tt_unbound: %d",
     93  1.3.2.3  jdolecek 	    ttm, (int)ttm->state);
     94  1.3.2.2       tls 	ttm_bus_dma_unpopulate(container_of(ttm, struct ttm_dma_tt, ttm));
     95  1.3.2.2       tls }
     96  1.3.2.2       tls 
     97  1.3.2.2       tls static int
     98  1.3.2.2       tls ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
     99  1.3.2.2       tls {
    100  1.3.2.2       tls 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    101  1.3.2.2       tls 	    ttm_dma.ttm);
    102  1.3.2.2       tls 	struct agp_softc *const sc = ttm_agp->agp;
    103  1.3.2.2       tls 	struct drm_mm_node *const node = bo_mem->mm_node;
    104  1.3.2.2       tls 	const unsigned long agp_pgno = node->start;
    105  1.3.2.2       tls 	unsigned i, j;
    106  1.3.2.2       tls 	int ret;
    107  1.3.2.2       tls 
    108  1.3.2.2       tls 	KASSERT(!ttm_agp->bound);
    109  1.3.2.2       tls 	KASSERT(ttm_agp->ttm_dma.dma_address->dm_nsegs == ttm->num_pages);
    110  1.3.2.2       tls 	for (i = 0; i < ttm->num_pages; i++) {
    111  1.3.2.2       tls 		KASSERT(ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len ==
    112  1.3.2.2       tls 		    AGP_PAGE_SIZE);
    113  1.3.2.2       tls 		/* XXX errno NetBSD->Linux */
    114  1.3.2.2       tls 		ret = -AGP_BIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT,
    115  1.3.2.2       tls 		    ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len);
    116  1.3.2.2       tls 		if (ret)
    117  1.3.2.2       tls 			goto fail;
    118  1.3.2.2       tls 	}
    119  1.3.2.2       tls 	agp_flush_cache();
    120  1.3.2.2       tls 	AGP_FLUSH_TLB(sc);
    121  1.3.2.2       tls 
    122  1.3.2.2       tls 	/* Success!  */
    123  1.3.2.2       tls 	ttm_agp->agp_pgno = agp_pgno;
    124  1.3.2.2       tls 	ttm_agp->bound = true;
    125  1.3.2.2       tls 	return 0;
    126  1.3.2.2       tls 
    127  1.3.2.2       tls fail:	KASSERT(ret);
    128  1.3.2.2       tls 	for (j = 0; j < i; j++)
    129  1.3.2.2       tls 		(void)AGP_UNBIND_PAGE(sc, (agp_pgno + j) << AGP_PAGE_SHIFT);
    130  1.3.2.2       tls 	return ret;
    131  1.3.2.2       tls }
    132  1.3.2.2       tls 
    133  1.3.2.2       tls static int
    134  1.3.2.2       tls ttm_agp_unbind(struct ttm_tt *ttm)
    135  1.3.2.2       tls {
    136  1.3.2.2       tls 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    137  1.3.2.2       tls 	    ttm_dma.ttm);
    138  1.3.2.2       tls 	struct agp_softc *const sc = ttm_agp->agp;
    139  1.3.2.2       tls 	const unsigned long agp_pgno = ttm_agp->agp_pgno;
    140  1.3.2.2       tls 	unsigned i;
    141  1.3.2.2       tls 
    142  1.3.2.2       tls 	/* XXX Can this happen?  */
    143  1.3.2.2       tls 	if (!ttm_agp->bound)
    144  1.3.2.2       tls 		return 0;
    145  1.3.2.2       tls 
    146  1.3.2.2       tls 	for (i = 0; i < ttm->num_pages; i++)
    147  1.3.2.2       tls 		(void)AGP_UNBIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT);
    148  1.3.2.2       tls 
    149  1.3.2.2       tls 	ttm_agp->agp_pgno = 0;
    150  1.3.2.2       tls 	ttm_agp->bound = false;
    151  1.3.2.2       tls 	return 0;
    152  1.3.2.2       tls }
    153  1.3.2.2       tls 
    154  1.3.2.2       tls static void
    155  1.3.2.2       tls ttm_agp_destroy(struct ttm_tt *ttm)
    156  1.3.2.2       tls {
    157  1.3.2.2       tls 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    158  1.3.2.2       tls 	    ttm_dma.ttm);
    159  1.3.2.2       tls 
    160  1.3.2.2       tls 	/* XXX Can this happen?  */
    161  1.3.2.2       tls 	if (ttm_agp->bound)
    162  1.3.2.2       tls 		ttm_agp_unbind(ttm);
    163  1.3.2.2       tls 	ttm_tt_fini(ttm);
    164  1.3.2.3  jdolecek 	kmem_free(ttm_agp, sizeof(*ttm_agp));
    165  1.3.2.2       tls }
    166  1.3.2.2       tls 
    167  1.3.2.2       tls static const struct ttm_backend_func ttm_agp_backend_func = {
    168  1.3.2.2       tls 	.bind = &ttm_agp_bind,
    169  1.3.2.2       tls 	.unbind = &ttm_agp_unbind,
    170  1.3.2.2       tls 	.destroy = &ttm_agp_destroy,
    171  1.3.2.2       tls };
    172  1.3.2.3  jdolecek 
    173  1.3.2.3  jdolecek #endif
    174