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