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