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 #if defined( CONFIG_X86 ) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24))
34 static void drm_clflush_page(struct page *page)
36 uint8_t *page_virtual;
39 if (unlikely(page == NULL))
42 page_virtual = kmap_atomic(page, KM_USER0);
44 for (i=0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
45 clflush(page_virtual + i);
47 kunmap_atomic(page_virtual, KM_USER0);
50 static void drm_ttm_cache_flush_clflush(struct page *pages[], unsigned long num_pages)
55 for (i=0; i < num_pages; ++i)
56 drm_clflush_page(*pages++);
61 static void drm_ttm_ipi_handler(void *null)
66 void drm_ttm_cache_flush(struct page *pages[], unsigned long num_pages)
69 #if defined( CONFIG_X86 ) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24))
70 if (cpu_has_clflush) {
71 drm_ttm_cache_flush_clflush(pages, num_pages);
75 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
76 if (on_each_cpu(drm_ttm_ipi_handler, NULL, 1))
78 if (on_each_cpu(drm_ttm_ipi_handler, NULL, 1, 1) != 0)
80 DRM_ERROR("Timed out waiting for drm cache flush.\n");
82 EXPORT_SYMBOL(drm_ttm_cache_flush);
85 * Allocates storage for pointers to the pages that back the ttm.
87 * Uses kmalloc if possible. Otherwise falls back to vmalloc.
89 static void drm_ttm_alloc_page_directory(struct drm_ttm *ttm)
91 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
94 if (drm_alloc_memctl(size))
97 if (size <= PAGE_SIZE)
98 ttm->pages = drm_calloc(1, size, DRM_MEM_TTM);
101 ttm->pages = vmalloc_user(size);
103 ttm->page_flags |= DRM_TTM_PAGEDIR_VMALLOC;
106 drm_free_memctl(size);
109 static void drm_ttm_free_page_directory(struct drm_ttm *ttm)
111 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
113 if (ttm->page_flags & DRM_TTM_PAGEDIR_VMALLOC) {
115 ttm->page_flags &= ~DRM_TTM_PAGEDIR_VMALLOC;
117 drm_free(ttm->pages, size, DRM_MEM_TTM);
119 drm_free_memctl(size);
123 static struct page *drm_ttm_alloc_page(void)
127 if (drm_alloc_memctl(PAGE_SIZE))
130 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32);
132 drm_free_memctl(PAGE_SIZE);
139 * Change caching policy for the linear kernel map
140 * for range of pages in a ttm.
143 static int drm_ttm_set_caching(struct drm_ttm *ttm, int noncached)
146 struct page **cur_page;
149 if ((ttm->page_flags & DRM_TTM_PAGE_UNCACHED) == noncached)
153 drm_ttm_cache_flush(ttm->pages, ttm->num_pages);
155 for (i = 0; i < ttm->num_pages; ++i) {
156 cur_page = ttm->pages + i;
158 if (!PageHighMem(*cur_page)) {
160 map_page_into_agp(*cur_page);
162 unmap_page_from_agp(*cur_page);
169 flush_agp_mappings();
171 DRM_FLAG_MASKED(ttm->page_flags, noncached, DRM_TTM_PAGE_UNCACHED);
177 static void drm_ttm_free_user_pages(struct drm_ttm *ttm)
184 BUG_ON(!(ttm->page_flags & DRM_TTM_PAGE_USER));
185 write = ((ttm->page_flags & DRM_TTM_PAGE_WRITE) != 0);
186 dirty = ((ttm->page_flags & DRM_TTM_PAGE_USER_DIRTY) != 0);
188 for (i = 0; i < ttm->num_pages; ++i) {
189 page = ttm->pages[i];
193 if (page == ttm->dummy_read_page) {
198 if (write && dirty && !PageReserved(page))
199 set_page_dirty_lock(page);
201 ttm->pages[i] = NULL;
206 static void drm_ttm_free_alloced_pages(struct drm_ttm *ttm)
209 struct drm_buffer_manager *bm = &ttm->dev->bm;
210 struct page **cur_page;
212 for (i = 0; i < ttm->num_pages; ++i) {
213 cur_page = ttm->pages + i;
215 if (page_count(*cur_page) != 1)
216 DRM_ERROR("Erroneous page count. Leaking pages.\n");
217 if (page_mapped(*cur_page))
218 DRM_ERROR("Erroneous map count. Leaking page mappings.\n");
219 __free_page(*cur_page);
220 drm_free_memctl(PAGE_SIZE);
227 * Free all resources associated with a ttm.
230 int drm_ttm_destroy(struct drm_ttm *ttm)
232 struct drm_ttm_backend *be;
239 be->func->destroy(be);
244 if (ttm->page_flags & DRM_TTM_PAGE_UNCACHED)
245 drm_ttm_set_caching(ttm, 0);
247 if (ttm->page_flags & DRM_TTM_PAGE_USER)
248 drm_ttm_free_user_pages(ttm);
250 drm_ttm_free_alloced_pages(ttm);
252 drm_ttm_free_page_directory(ttm);
255 drm_ctl_free(ttm, sizeof(*ttm), DRM_MEM_TTM);
259 struct page *drm_ttm_get_page(struct drm_ttm *ttm, int index)
262 struct drm_buffer_manager *bm = &ttm->dev->bm;
264 while(NULL == (p = ttm->pages[index])) {
265 p = drm_ttm_alloc_page();
270 ttm->pages[--ttm->first_himem_page] = p;
272 ttm->pages[++ttm->last_lomem_page] = p;
278 EXPORT_SYMBOL(drm_ttm_get_page);
283 * @ttm: the ttm to map pages to. This must always be
284 * a freshly created ttm.
286 * @tsk: a pointer to the address space from which to map
289 * @write: a boolean indicating that write access is desired
291 * start: the starting address
293 * Map a range of user addresses to a new ttm object. This
294 * provides access to user memory from the graphics device.
296 int drm_ttm_set_user(struct drm_ttm *ttm,
297 struct task_struct *tsk,
299 unsigned long num_pages)
301 struct mm_struct *mm = tsk->mm;
303 int write = (ttm->page_flags & DRM_TTM_PAGE_WRITE) != 0;
305 BUG_ON(num_pages != ttm->num_pages);
306 BUG_ON((ttm->page_flags & DRM_TTM_PAGE_USER) == 0);
308 down_read(&mm->mmap_sem);
309 ret = get_user_pages(tsk, mm, start, num_pages,
310 write, 0, ttm->pages, NULL);
311 up_read(&mm->mmap_sem);
313 if (ret != num_pages && write) {
314 drm_ttm_free_user_pages(ttm);
324 * @ttm: the object to allocate pages for
326 * Allocate pages for all unset page entries, then
327 * call the backend to create the hardware mappings
329 int drm_ttm_populate(struct drm_ttm *ttm)
333 struct drm_ttm_backend *be;
335 if (ttm->state != ttm_unpopulated)
340 for (i = 0; i < ttm->num_pages; ++i) {
341 page = drm_ttm_get_page(ttm, i);
346 be->func->populate(be, ttm->num_pages, ttm->pages, ttm->dummy_read_page);
347 ttm->state = ttm_unbound;
354 * @dev: the drm_device
356 * @size: The size (in bytes) of the desired object
358 * @page_flags: various DRM_TTM_PAGE_* flags. See drm_object.h.
360 * Allocate and initialize a ttm, leaving it unpopulated at this time
363 struct drm_ttm *drm_ttm_create(struct drm_device *dev, unsigned long size,
364 uint32_t page_flags, struct page *dummy_read_page)
366 struct drm_bo_driver *bo_driver = dev->driver->bo_driver;
372 ttm = drm_ctl_calloc(1, sizeof(*ttm), DRM_MEM_TTM);
377 atomic_set(&ttm->vma_count, 0);
380 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
381 ttm->first_himem_page = ttm->num_pages;
382 ttm->last_lomem_page = -1;
384 ttm->page_flags = page_flags;
386 ttm->dummy_read_page = dummy_read_page;
389 * Account also for AGP module memory usage.
392 drm_ttm_alloc_page_directory(ttm);
394 drm_ttm_destroy(ttm);
395 DRM_ERROR("Failed allocating page table\n");
398 ttm->be = bo_driver->create_ttm_backend_entry(dev);
400 drm_ttm_destroy(ttm);
401 DRM_ERROR("Failed creating ttm backend entry\n");
404 ttm->state = ttm_unpopulated;
411 * @ttm: the object to be unbound from the aperture.
413 * Transition a ttm from bound to evicted, where it
414 * isn't present in the aperture, but various caches may
417 void drm_ttm_evict(struct drm_ttm *ttm)
419 struct drm_ttm_backend *be = ttm->be;
422 if (ttm->state == ttm_bound) {
423 ret = be->func->unbind(be);
427 ttm->state = ttm_evicted;
431 * drm_ttm_fixup_caching:
433 * @ttm: the object to set unbound
435 * XXX this function is misnamed. Transition a ttm from evicted to
436 * unbound, flushing caches as appropriate.
438 void drm_ttm_fixup_caching(struct drm_ttm *ttm)
441 if (ttm->state == ttm_evicted) {
442 struct drm_ttm_backend *be = ttm->be;
443 if (be->func->needs_ub_cache_adjust(be))
444 drm_ttm_set_caching(ttm, 0);
445 ttm->state = ttm_unbound;
452 * @ttm: the object to unbind from the graphics device
454 * Unbind an object from the aperture. This removes the mappings
455 * from the graphics device and flushes caches if necessary.
457 void drm_ttm_unbind(struct drm_ttm *ttm)
459 if (ttm->state == ttm_bound)
462 drm_ttm_fixup_caching(ttm);
468 * @ttm: the ttm object to bind to the graphics device
470 * @bo_mem: the aperture memory region which will hold the object
472 * Bind a ttm object to the aperture. This ensures that the necessary
473 * pages are allocated, flushes CPU caches as needed and marks the
474 * ttm as DRM_TTM_PAGE_USER_DIRTY to indicate that it may have been
475 * modified by the GPU
477 int drm_ttm_bind(struct drm_ttm *ttm, struct drm_bo_mem_reg *bo_mem)
479 struct drm_bo_driver *bo_driver = ttm->dev->driver->bo_driver;
481 struct drm_ttm_backend *be;
485 if (ttm->state == ttm_bound)
490 ret = drm_ttm_populate(ttm);
494 if (ttm->state == ttm_unbound && !(bo_mem->flags & DRM_BO_FLAG_CACHED))
495 drm_ttm_set_caching(ttm, DRM_TTM_PAGE_UNCACHED);
496 else if ((bo_mem->flags & DRM_BO_FLAG_CACHED_MAPPED) &&
497 bo_driver->ttm_cache_flush)
498 bo_driver->ttm_cache_flush(ttm);
500 ret = be->func->bind(be, bo_mem);
502 ttm->state = ttm_evicted;
503 DRM_ERROR("Couldn't bind backend.\n");
507 ttm->state = ttm_bound;
508 if (ttm->page_flags & DRM_TTM_PAGE_USER)
509 ttm->page_flags |= DRM_TTM_PAGE_USER_DIRTY;
512 EXPORT_SYMBOL(drm_ttm_bind);