xref: /openbsd-src/sys/dev/pci/drm/ttm/ttm_agp_backend.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ttm_agp_backend.c,v 1.3 2014/02/09 10:57:26 jsg Exp $	*/
2 /**************************************************************************
3  *
4  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30  *          Keith Packard.
31  */
32 
33 #define pr_fmt(fmt) "[TTM] " fmt
34 
35 #include <dev/pci/drm/ttm/ttm_module.h>
36 #include <dev/pci/drm/ttm/ttm_bo_driver.h>
37 #include <dev/pci/drm/ttm/ttm_page_alloc.h>
38 #ifdef TTM_HAS_AGP
39 #include <dev/pci/drm/ttm/ttm_placement.h>
40 
41 struct ttm_agp_backend {
42 	struct ttm_tt ttm;
43 	int bound;
44 	bus_addr_t addr;
45 	struct drm_agp_head *agp;
46 };
47 
48 static int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
49 {
50 	struct ttm_agp_backend	*agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
51 	struct drm_mm_node *node = bo_mem->mm_node;
52 	struct agp_softc *sc = agp_be->agp->agpdev;
53 	bus_addr_t addr;
54 //	int cached = (bo_mem->placement & TTM_PL_FLAG_CACHED);
55 	unsigned i;
56 
57 	addr = sc->sc_apaddr + (node->start << PAGE_SHIFT);
58 	for (i = 0; i < ttm->num_pages; i++) {
59 		struct vm_page *page = ttm->pages[i];
60 
61 		if (!page)
62 			page = ttm->dummy_read_page;
63 
64 		sc->sc_methods->bind_page(sc->sc_chipc, addr, VM_PAGE_TO_PHYS(page), 0);
65 		addr += PAGE_SIZE;
66 	}
67 	agp_flush_cache();
68 	sc->sc_methods->flush_tlb(sc->sc_chipc);
69 	agp_be->addr = sc->sc_apaddr + (node->start << PAGE_SHIFT);
70 	agp_be->bound = 1;
71 
72 	return 0;
73 }
74 
75 static int ttm_agp_unbind(struct ttm_tt *ttm)
76 {
77 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
78 	struct agp_softc *sc = agp_be->agp->agpdev;
79 	bus_addr_t addr;
80 	unsigned i;
81 
82 	if (agp_be->bound) {
83 		addr = agp_be->addr;
84 		for (i = 0; i < ttm->num_pages; i++) {
85 			sc->sc_methods->unbind_page(sc->sc_chipc, addr);
86 			addr += PAGE_SIZE;
87 		}
88 		agp_flush_cache();
89 		sc->sc_methods->flush_tlb(sc->sc_chipc);
90 		agp_be->bound = 0;
91 	}
92 	return 0;
93 }
94 
95 static void ttm_agp_destroy(struct ttm_tt *ttm)
96 {
97 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
98 
99 	if (agp_be->bound)
100 		ttm_agp_unbind(ttm);
101 	ttm_tt_fini(ttm);
102 	kfree(agp_be);
103 }
104 
105 static struct ttm_backend_func ttm_agp_func = {
106 	.bind = ttm_agp_bind,
107 	.unbind = ttm_agp_unbind,
108 	.destroy = ttm_agp_destroy,
109 };
110 
111 struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
112 				 struct drm_agp_head *agp,
113 				 unsigned long size, uint32_t page_flags,
114 				 struct vm_page *dummy_read_page)
115 {
116 	struct ttm_agp_backend *agp_be;
117 
118 	agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL);
119 	if (!agp_be)
120 		return NULL;
121 
122 	agp_be->bound = 0;
123 	agp_be->agp = agp;
124 	agp_be->ttm.func = &ttm_agp_func;
125 
126 	if (ttm_tt_init(&agp_be->ttm, bdev, size, page_flags, dummy_read_page)) {
127 		return NULL;
128 	}
129 
130 	return &agp_be->ttm;
131 }
132 EXPORT_SYMBOL(ttm_agp_tt_create);
133 
134 int ttm_agp_tt_populate(struct ttm_tt *ttm)
135 {
136 	if (ttm->state != tt_unpopulated)
137 		return 0;
138 
139 	return ttm_pool_populate(ttm);
140 }
141 EXPORT_SYMBOL(ttm_agp_tt_populate);
142 
143 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
144 {
145 	ttm_pool_unpopulate(ttm);
146 }
147 EXPORT_SYMBOL(ttm_agp_tt_unpopulate);
148 
149 #endif
150