Home | History | Annotate | Line # | Download | only in ttm
ttm_agp_backend.c revision 1.6.14.1
      1 /*	$NetBSD: ttm_agp_backend.c,v 1.6.14.1 2019/06/10 22:08:33 christos 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.6.14.1 2019/06/10 22:08:33 christos 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 CONFIG_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 	KASSERTMSG((ttm->state == tt_unpopulated),
     82 	    "ttm_agp_tt_populate: ttm %p state is not tt_unpopulated: %d",
     83 	    ttm, (int)ttm->state);
     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 	KASSERTMSG((ttm->state == tt_unbound),
     92 	    "ttm_agp_tt_unpopulate: ttm %p state is not tt_unbound: %d",
     93 	    ttm, (int)ttm->state);
     94 	ttm_bus_dma_unpopulate(container_of(ttm, struct ttm_dma_tt, ttm));
     95 }
     96 
     97 static int
     98 ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
     99 {
    100 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    101 	    ttm_dma.ttm);
    102 	struct agp_softc *const sc = ttm_agp->agp;
    103 	struct drm_mm_node *const node = bo_mem->mm_node;
    104 	const unsigned long agp_pgno = node->start;
    105 	unsigned i, j;
    106 	int ret;
    107 
    108 	KASSERT(!ttm_agp->bound);
    109 	KASSERT(ttm_agp->ttm_dma.dma_address->dm_nsegs == ttm->num_pages);
    110 	for (i = 0; i < ttm->num_pages; i++) {
    111 		KASSERT(ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len ==
    112 		    AGP_PAGE_SIZE);
    113 		/* XXX errno NetBSD->Linux */
    114 		ret = -AGP_BIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT,
    115 		    ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len);
    116 		if (ret)
    117 			goto fail;
    118 	}
    119 	drm_agp_flush();
    120 	AGP_FLUSH_TLB(sc);
    121 
    122 	/* Success!  */
    123 	ttm_agp->agp_pgno = agp_pgno;
    124 	ttm_agp->bound = true;
    125 	return 0;
    126 
    127 fail:	KASSERT(ret);
    128 	for (j = 0; j < i; j++)
    129 		(void)AGP_UNBIND_PAGE(sc, (agp_pgno + j) << AGP_PAGE_SHIFT);
    130 	return ret;
    131 }
    132 
    133 static int
    134 ttm_agp_unbind(struct ttm_tt *ttm)
    135 {
    136 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    137 	    ttm_dma.ttm);
    138 	struct agp_softc *const sc = ttm_agp->agp;
    139 	const unsigned long agp_pgno = ttm_agp->agp_pgno;
    140 	unsigned i;
    141 
    142 	/* XXX Can this happen?  */
    143 	if (!ttm_agp->bound)
    144 		return 0;
    145 
    146 	for (i = 0; i < ttm->num_pages; i++)
    147 		(void)AGP_UNBIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT);
    148 
    149 	ttm_agp->agp_pgno = 0;
    150 	ttm_agp->bound = false;
    151 	return 0;
    152 }
    153 
    154 static void
    155 ttm_agp_destroy(struct ttm_tt *ttm)
    156 {
    157 	struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
    158 	    ttm_dma.ttm);
    159 
    160 	/* XXX Can this happen?  */
    161 	if (ttm_agp->bound)
    162 		ttm_agp_unbind(ttm);
    163 	ttm_tt_fini(ttm);
    164 	kmem_free(ttm_agp, sizeof(*ttm_agp));
    165 }
    166 
    167 static const struct ttm_backend_func ttm_agp_backend_func = {
    168 	.bind = &ttm_agp_bind,
    169 	.unbind = &ttm_agp_unbind,
    170 	.destroy = &ttm_agp_destroy,
    171 };
    172 
    173 #endif
    174