xref: /openbsd-src/sys/dev/pci/drm/ttm/ttm_agp_backend.c (revision 5a38ef86d0b61900239c7913d24a05e7b88a58f0)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
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 <drm/ttm/ttm_module.h>
36 #include <drm/ttm/ttm_bo_driver.h>
37 #include <drm/ttm/ttm_page_alloc.h>
38 #include <drm/ttm/ttm_placement.h>
39 #include <linux/agp_backend.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/io.h>
43 #include <asm/agp.h>
44 
45 #include <drm/drm_agpsupport.h>
46 
47 struct ttm_agp_backend {
48 	struct ttm_tt ttm;
49 	int bound;
50 	bus_addr_t addr;
51 	struct drm_agp_head *agp;
52 };
53 
54 int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem)
55 {
56 	struct ttm_agp_backend	*agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
57 	struct vm_page *dummy_read_page = ttm_bo_glob.dummy_read_page;
58 	struct drm_mm_node *node = bo_mem->mm_node;
59 	struct agp_softc *sc = agp_be->agp->agpdev;
60 	bus_addr_t addr;
61 //	int cached = (bo_mem->placement & TTM_PL_FLAG_CACHED);
62 	unsigned i;
63 
64 #ifdef notyet
65 	if (agp_be->mem)
66 		return 0;
67 #endif
68 
69 	addr = sc->sc_apaddr + (node->start << PAGE_SHIFT);
70 	for (i = 0; i < ttm->num_pages; i++) {
71 		struct vm_page *page = ttm->pages[i];
72 
73 		if (!page)
74 			page = dummy_read_page;
75 
76 		sc->sc_methods->bind_page(sc->sc_chipc, addr, VM_PAGE_TO_PHYS(page), 0);
77 		addr += PAGE_SIZE;
78 	}
79 	agp_flush_cache();
80 	sc->sc_methods->flush_tlb(sc->sc_chipc);
81 	agp_be->addr = sc->sc_apaddr + (node->start << PAGE_SHIFT);
82 	agp_be->bound = 1;
83 
84 	return 0;
85 }
86 EXPORT_SYMBOL(ttm_agp_bind);
87 
88 void ttm_agp_unbind(struct ttm_tt *ttm)
89 {
90 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
91 	struct agp_softc *sc = agp_be->agp->agpdev;
92 	bus_addr_t addr;
93 	unsigned i;
94 
95 	if (agp_be->bound) {
96 		addr = agp_be->addr;
97 		for (i = 0; i < ttm->num_pages; i++) {
98 			sc->sc_methods->unbind_page(sc->sc_chipc, addr);
99 			addr += PAGE_SIZE;
100 		}
101 		agp_flush_cache();
102 		sc->sc_methods->flush_tlb(sc->sc_chipc);
103 		agp_be->bound = 0;
104 	}
105 }
106 EXPORT_SYMBOL(ttm_agp_unbind);
107 
108 bool ttm_agp_is_bound(struct ttm_tt *ttm)
109 {
110 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
111 
112 	if (!ttm)
113 		return false;
114 
115 	return (agp_be->bound == 1);
116 }
117 EXPORT_SYMBOL(ttm_agp_is_bound);
118 
119 void ttm_agp_destroy(struct ttm_tt *ttm)
120 {
121 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
122 
123 	if (agp_be->bound)
124 		ttm_agp_unbind(ttm);
125 	ttm_tt_fini(ttm);
126 	kfree(agp_be);
127 }
128 EXPORT_SYMBOL(ttm_agp_destroy);
129 
130 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
131 				 struct drm_agp_head *agp,
132 				 uint32_t page_flags)
133 {
134 	struct ttm_agp_backend *agp_be;
135 
136 	agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL);
137 	if (!agp_be)
138 		return NULL;
139 
140 	agp_be->bound = 0;
141 	agp_be->agp = agp;
142 
143 	if (ttm_tt_init(&agp_be->ttm, bo, page_flags)) {
144 		return NULL;
145 	}
146 
147 	return &agp_be->ttm;
148 }
149 EXPORT_SYMBOL(ttm_agp_tt_create);
150