1 /**************************************************************************
3 * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
33 static void drm_ttm_ipi_handler(void *null)
38 static void drm_ttm_cache_flush(void)
40 if (on_each_cpu(drm_ttm_ipi_handler, NULL, 1, 1) != 0)
41 DRM_ERROR("Timed out waiting for drm cache flush.\n");
45 * Use kmalloc if possible. Otherwise fall back to vmalloc.
48 static void ttm_alloc_pages(drm_ttm_t * ttm)
50 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
53 if (drm_alloc_memctl(size))
56 if (size <= PAGE_SIZE) {
57 ttm->pages = drm_calloc(1, size, DRM_MEM_TTM);
60 ttm->pages = vmalloc_user(size);
62 ttm->page_flags |= DRM_TTM_PAGE_VMALLOC;
65 drm_free_memctl(size);
69 static void ttm_free_pages(drm_ttm_t * ttm)
71 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
73 if (ttm->page_flags & DRM_TTM_PAGE_VMALLOC) {
75 ttm->page_flags &= ~DRM_TTM_PAGE_VMALLOC;
77 drm_free(ttm->pages, size, DRM_MEM_TTM);
79 drm_free_memctl(size);
83 static struct page *drm_ttm_alloc_page(void)
87 if (drm_alloc_memctl(PAGE_SIZE)) {
90 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32);
92 drm_free_memctl(PAGE_SIZE);
95 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
98 SetPageReserved(page);
104 * Change caching policy for the linear kernel map
105 * for range of pages in a ttm.
108 static int drm_set_caching(drm_ttm_t * ttm, int noncached)
111 struct page **cur_page;
114 if ((ttm->page_flags & DRM_TTM_PAGE_UNCACHED) == noncached)
118 drm_ttm_cache_flush();
120 for (i = 0; i < ttm->num_pages; ++i) {
121 cur_page = ttm->pages + i;
123 if (!PageHighMem(*cur_page)) {
125 map_page_into_agp(*cur_page);
127 unmap_page_from_agp(*cur_page);
134 flush_agp_mappings();
136 DRM_FLAG_MASKED(ttm->page_flags, noncached, DRM_TTM_PAGE_UNCACHED);
142 * Free all resources associated with a ttm.
145 int drm_destroy_ttm(drm_ttm_t * ttm)
149 struct page **cur_page;
150 drm_ttm_backend_t *be;
157 be->func->destroy(be);
162 drm_buffer_manager_t *bm = &ttm->dev->bm;
163 if (ttm->page_flags & DRM_TTM_PAGE_UNCACHED)
164 drm_set_caching(ttm, 0);
166 for (i = 0; i < ttm->num_pages; ++i) {
167 cur_page = ttm->pages + i;
169 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
170 unlock_page(*cur_page);
172 ClearPageReserved(*cur_page);
174 if (page_count(*cur_page) != 1) {
175 DRM_ERROR("Erroneous page count. "
178 if (page_mapped(*cur_page)) {
179 DRM_ERROR("Erroneous map count. "
180 "Leaking page mappings.\n");
182 __free_page(*cur_page);
183 drm_free_memctl(PAGE_SIZE);
190 drm_ctl_free(ttm, sizeof(*ttm), DRM_MEM_TTM);
194 struct page *drm_ttm_get_page(drm_ttm_t * ttm, int index)
197 drm_buffer_manager_t *bm = &ttm->dev->bm;
199 p = ttm->pages[index];
201 p = drm_ttm_alloc_page();
204 ttm->pages[index] = p;
210 static int drm_ttm_populate(drm_ttm_t * ttm)
214 drm_ttm_backend_t *be;
216 if (ttm->state != ttm_unpopulated)
220 for (i = 0; i < ttm->num_pages; ++i) {
221 page = drm_ttm_get_page(ttm, i);
225 be->func->populate(be, ttm->num_pages, ttm->pages);
226 ttm->state = ttm_unbound;
234 drm_ttm_t *drm_ttm_init(struct drm_device * dev, unsigned long size)
236 drm_bo_driver_t *bo_driver = dev->driver->bo_driver;
242 ttm = drm_ctl_calloc(1, sizeof(*ttm), DRM_MEM_TTM);
247 atomic_set(&ttm->vma_count, 0);
250 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
255 * Account also for AGP module memory usage.
258 ttm_alloc_pages(ttm);
260 drm_destroy_ttm(ttm);
261 DRM_ERROR("Failed allocating page table\n");
264 ttm->be = bo_driver->create_ttm_backend_entry(dev);
266 drm_destroy_ttm(ttm);
267 DRM_ERROR("Failed creating ttm backend entry\n");
270 ttm->state = ttm_unpopulated;
275 * Unbind a ttm region from the aperture.
278 void drm_ttm_evict(drm_ttm_t * ttm)
280 drm_ttm_backend_t *be = ttm->be;
283 if (ttm->state == ttm_bound) {
284 ret = be->func->unbind(be);
288 ttm->state = ttm_evicted;
291 void drm_ttm_fixup_caching(drm_ttm_t * ttm)
294 if (ttm->state == ttm_evicted) {
295 drm_ttm_backend_t *be = ttm->be;
296 if (be->func->needs_ub_cache_adjust(be)) {
297 drm_set_caching(ttm, 0);
299 ttm->state = ttm_unbound;
303 void drm_ttm_unbind(drm_ttm_t * ttm)
305 if (ttm->state == ttm_bound)
308 drm_ttm_fixup_caching(ttm);
311 int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset)
315 drm_ttm_backend_t *be;
319 if (ttm->state == ttm_bound)
324 ret = drm_ttm_populate(ttm);
328 if (ttm->state == ttm_unbound && !cached) {
329 drm_set_caching(ttm, DRM_TTM_PAGE_UNCACHED);
332 if ((ret = be->func->bind(be, aper_offset, cached))) {
333 ttm->state = ttm_evicted;
334 DRM_ERROR("Couldn't bind backend.\n");
338 ttm->aper_offset = aper_offset;
339 ttm->state = ttm_bound;
344 EXPORT_SYMBOL(drm_bind_ttm);