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