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 (drm_on_each_cpu(drm_ttm_ipi_handler, NULL, 1) != 0)
76 DRM_ERROR("Timed out waiting for drm cache flush.\n");
78 EXPORT_SYMBOL(drm_ttm_cache_flush);
81 * Allocates storage for pointers to the pages that back the ttm.
83 * Uses kmalloc if possible. Otherwise falls back to vmalloc.
85 static void drm_ttm_alloc_page_directory(struct drm_ttm *ttm)
87 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
90 if (drm_alloc_memctl(size))
93 if (size <= PAGE_SIZE)
94 ttm->pages = drm_calloc(1, size, DRM_MEM_TTM);
97 ttm->pages = vmalloc_user(size);
99 ttm->page_flags |= DRM_TTM_PAGEDIR_VMALLOC;
102 drm_free_memctl(size);
105 static void drm_ttm_free_page_directory(struct drm_ttm *ttm)
107 unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
109 if (ttm->page_flags & DRM_TTM_PAGEDIR_VMALLOC) {
111 ttm->page_flags &= ~DRM_TTM_PAGEDIR_VMALLOC;
113 drm_free(ttm->pages, size, DRM_MEM_TTM);
115 drm_free_memctl(size);
119 static struct page *drm_ttm_alloc_page(void)
123 if (drm_alloc_memctl(PAGE_SIZE))
126 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32);
128 drm_free_memctl(PAGE_SIZE);
131 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
132 SetPageReserved(page);
138 * Change caching policy for the linear kernel map
139 * for range of pages in a ttm.
142 static int drm_ttm_set_caching(struct drm_ttm *ttm, int noncached)
145 struct page **cur_page;
148 if ((ttm->page_flags & DRM_TTM_PAGE_UNCACHED) == noncached)
152 drm_ttm_cache_flush(ttm->pages, ttm->num_pages);
154 for (i = 0; i < ttm->num_pages; ++i) {
155 cur_page = ttm->pages + i;
157 if (!PageHighMem(*cur_page)) {
159 map_page_into_agp(*cur_page);
161 unmap_page_from_agp(*cur_page);
168 flush_agp_mappings();
170 DRM_FLAG_MASKED(ttm->page_flags, noncached, DRM_TTM_PAGE_UNCACHED);
176 static void drm_ttm_free_user_pages(struct drm_ttm *ttm)
183 BUG_ON(!(ttm->page_flags & DRM_TTM_PAGE_USER));
184 write = ((ttm->page_flags & DRM_TTM_PAGE_WRITE) != 0);
185 dirty = ((ttm->page_flags & DRM_TTM_PAGE_USER_DIRTY) != 0);
187 for (i = 0; i < ttm->num_pages; ++i) {
188 page = ttm->pages[i];
192 if (page == ttm->dummy_read_page) {
197 if (write && dirty && !PageReserved(page))
198 set_page_dirty_lock(page);
200 ttm->pages[i] = NULL;
205 static void drm_ttm_free_alloced_pages(struct drm_ttm *ttm)
208 struct drm_buffer_manager *bm = &ttm->dev->bm;
209 struct page **cur_page;
211 for (i = 0; i < ttm->num_pages; ++i) {
212 cur_page = ttm->pages + i;
214 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
215 ClearPageReserved(*cur_page);
217 if (page_count(*cur_page) != 1)
218 DRM_ERROR("Erroneous page count. Leaking pages.\n");
219 if (page_mapped(*cur_page))
220 DRM_ERROR("Erroneous map count. Leaking page mappings.\n");
221 __free_page(*cur_page);
222 drm_free_memctl(PAGE_SIZE);
229 * Free all resources associated with a ttm.
232 int drm_ttm_destroy(struct drm_ttm *ttm)
234 struct drm_ttm_backend *be;
241 be->func->destroy(be);
246 if (ttm->page_flags & DRM_TTM_PAGE_UNCACHED)
247 drm_ttm_set_caching(ttm, 0);
249 if (ttm->page_flags & DRM_TTM_PAGE_USER)
250 drm_ttm_free_user_pages(ttm);
252 drm_ttm_free_alloced_pages(ttm);
254 drm_ttm_free_page_directory(ttm);
257 drm_ctl_free(ttm, sizeof(*ttm), DRM_MEM_TTM);
261 struct page *drm_ttm_get_page(struct drm_ttm *ttm, int index)
264 struct drm_buffer_manager *bm = &ttm->dev->bm;
266 while(NULL == (p = ttm->pages[index])) {
267 p = drm_ttm_alloc_page();
272 ttm->pages[--ttm->first_himem_page] = p;
274 ttm->pages[++ttm->last_lomem_page] = p;
280 EXPORT_SYMBOL(drm_ttm_get_page);
285 * @ttm: the ttm to map pages to. This must always be
286 * a freshly created ttm.
288 * @tsk: a pointer to the address space from which to map
291 * @write: a boolean indicating that write access is desired
293 * start: the starting address
295 * Map a range of user addresses to a new ttm object. This
296 * provides access to user memory from the graphics device.
298 int drm_ttm_set_user(struct drm_ttm *ttm,
299 struct task_struct *tsk,
301 unsigned long num_pages)
303 struct mm_struct *mm = tsk->mm;
305 int write = (ttm->page_flags & DRM_TTM_PAGE_WRITE) != 0;
307 BUG_ON(num_pages != ttm->num_pages);
308 BUG_ON((ttm->page_flags & DRM_TTM_PAGE_USER) == 0);
310 down_read(&mm->mmap_sem);
311 ret = get_user_pages(tsk, mm, start, num_pages,
312 write, 0, ttm->pages, NULL);
313 up_read(&mm->mmap_sem);
315 if (ret != num_pages && write) {
316 drm_ttm_free_user_pages(ttm);
326 * @ttm: the object to allocate pages for
328 * Allocate pages for all unset page entries, then
329 * call the backend to create the hardware mappings
331 int drm_ttm_populate(struct drm_ttm *ttm)
335 struct drm_ttm_backend *be;
337 if (ttm->state != ttm_unpopulated)
342 for (i = 0; i < ttm->num_pages; ++i) {
343 page = drm_ttm_get_page(ttm, i);
348 be->func->populate(be, ttm->num_pages, ttm->pages, ttm->dummy_read_page);
349 ttm->state = ttm_unbound;
356 * @dev: the drm_device
358 * @size: The size (in bytes) of the desired object
360 * @page_flags: various DRM_TTM_PAGE_* flags. See drm_object.h.
362 * Allocate and initialize a ttm, leaving it unpopulated at this time
365 struct drm_ttm *drm_ttm_create(struct drm_device *dev, unsigned long size,
366 uint32_t page_flags, struct page *dummy_read_page)
368 struct drm_bo_driver *bo_driver = dev->driver->bo_driver;
374 ttm = drm_ctl_calloc(1, sizeof(*ttm), DRM_MEM_TTM);
379 atomic_set(&ttm->vma_count, 0);
382 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
383 ttm->first_himem_page = ttm->num_pages;
384 ttm->last_lomem_page = -1;
386 ttm->page_flags = page_flags;
388 ttm->dummy_read_page = dummy_read_page;
391 * Account also for AGP module memory usage.
394 drm_ttm_alloc_page_directory(ttm);
396 drm_ttm_destroy(ttm);
397 DRM_ERROR("Failed allocating page table\n");
400 ttm->be = bo_driver->create_ttm_backend_entry(dev);
402 drm_ttm_destroy(ttm);
403 DRM_ERROR("Failed creating ttm backend entry\n");
406 ttm->state = ttm_unpopulated;
413 * @ttm: the object to be unbound from the aperture.
415 * Transition a ttm from bound to evicted, where it
416 * isn't present in the aperture, but various caches may
419 void drm_ttm_evict(struct drm_ttm *ttm)
421 struct drm_ttm_backend *be = ttm->be;
424 if (ttm->state == ttm_bound) {
425 ret = be->func->unbind(be);
429 ttm->state = ttm_evicted;
433 * drm_ttm_fixup_caching:
435 * @ttm: the object to set unbound
437 * XXX this function is misnamed. Transition a ttm from evicted to
438 * unbound, flushing caches as appropriate.
440 void drm_ttm_fixup_caching(struct drm_ttm *ttm)
443 if (ttm->state == ttm_evicted) {
444 struct drm_ttm_backend *be = ttm->be;
445 if (be->func->needs_ub_cache_adjust(be))
446 drm_ttm_set_caching(ttm, 0);
447 ttm->state = ttm_unbound;
454 * @ttm: the object to unbind from the graphics device
456 * Unbind an object from the aperture. This removes the mappings
457 * from the graphics device and flushes caches if necessary.
459 void drm_ttm_unbind(struct drm_ttm *ttm)
461 if (ttm->state == ttm_bound)
464 drm_ttm_fixup_caching(ttm);
470 * @ttm: the ttm object to bind to the graphics device
472 * @bo_mem: the aperture memory region which will hold the object
474 * Bind a ttm object to the aperture. This ensures that the necessary
475 * pages are allocated, flushes CPU caches as needed and marks the
476 * ttm as DRM_TTM_PAGE_USER_DIRTY to indicate that it may have been
477 * modified by the GPU
479 int drm_ttm_bind(struct drm_ttm *ttm, struct drm_bo_mem_reg *bo_mem)
481 struct drm_bo_driver *bo_driver = ttm->dev->driver->bo_driver;
483 struct drm_ttm_backend *be;
487 if (ttm->state == ttm_bound)
492 ret = drm_ttm_populate(ttm);
496 if (ttm->state == ttm_unbound && !(bo_mem->flags & DRM_BO_FLAG_CACHED))
497 drm_ttm_set_caching(ttm, DRM_TTM_PAGE_UNCACHED);
498 else if ((bo_mem->flags & DRM_BO_FLAG_CACHED_MAPPED) &&
499 bo_driver->ttm_cache_flush)
500 bo_driver->ttm_cache_flush(ttm);
502 ret = be->func->bind(be, bo_mem);
504 ttm->state = ttm_evicted;
505 DRM_ERROR("Couldn't bind backend.\n");
509 ttm->state = ttm_bound;
510 if (ttm->page_flags & DRM_TTM_PAGE_USER)
511 ttm->page_flags |= DRM_TTM_PAGE_USER_DIRTY;
514 EXPORT_SYMBOL(drm_ttm_bind);