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