Skip to contentSkip to conversation

ttm: merge ttm_backend & ttm_tt, introduce ttm dma allocator V6

PATCH In review [email protected] 3 participants 20 messages Nov 16, 2011 – Nov 17, 2011

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

Respin some of the patch with syntax/typo fix + patch 10 with proper memory accounting of page being free.

Cheers,
Jerome

#1 lore raw

PATCH 1/14 swiotlb: Expose swiotlb_nr_tlb function to modules

From: Konrad Rzeszutek Wilk <[email protected]>

As a mechanism to detect whether SWIOTLB is enabled or not. We also fix the spelling - it was swioltb instead of swiotlb.

CC FUJITA Tomonori <[email protected]>
[v1: Ripped out swiotlb_enabled]
---
 drivers/xen/swiotlb-xen.c |    2 +-
 include/linux/swiotlb.h   |    2 +-
 lib/swiotlb.c             |    5 +++--
 3 files changed, 5 insertions(+), 4 deletions(-)
drivers/xen/swiotlb-xen.c +1 -1
index 8e964b9..4864e5d 100644
@@ -153,7 +153,7 @@ void __init xen_swiotlb_init(int verbose)
 	char *m = NULL;
 	unsigned int repeat = 3;
 
-	nr_tbl = swioltb_nr_tbl();
+	nr_tbl = swiotlb_nr_tbl();
 	if (nr_tbl)
 		xen_io_tlb_nslabs = nr_tbl;
 	else {
include/linux/swiotlb.h +1 -1
index 445702c..e872526 100644
@@ -24,7 +24,7 @@ extern int swiotlb_force;
 
 extern void swiotlb_init(int verbose);
 extern void swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose);
-extern unsigned long swioltb_nr_tbl(void);
+extern unsigned long swiotlb_nr_tbl(void);
 
 /*
  * Enumeration for sync targets
lib/swiotlb.c +3 -2
index 99093b3..058935e 100644
@@ -110,11 +110,11 @@ setup_io_tlb_npages(char *str)
 __setup("swiotlb=", setup_io_tlb_npages);
 /* make io_tlb_overflow tunable too? */
 
-unsigned long swioltb_nr_tbl(void)
+unsigned long swiotlb_nr_tbl(void)
 {
 	return io_tlb_nslabs;
 }
-
+EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
 /* Note that this doesn't work with highmem page */
 static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
 				      volatile void *address)
@@ -321,6 +321,7 @@ void __init swiotlb_free(void)
 		free_bootmem_late(__pa(io_tlb_start),
 				  PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
 	}
+	io_tlb_nslabs = 0;
 }
 
 static int is_swiotlb_buffer(phys_addr_t paddr)
Signature · 1 line
1.7.7.1
#2 lore raw

PATCH 2/14 drm/ttm: remove userspace backed ttm object support

In reply to [email protected]

From: Jerome Glisse <[email protected]>

This was never use in none of the driver, properly using userspace page for bo would need more code (vma interaction mostly). Removing this dead code in preparation of ttm_tt & backend merge.

---
 drivers/gpu/drm/ttm/ttm_bo.c    |   22 --------
 drivers/gpu/drm/ttm/ttm_tt.c    |  105 +--------------------------------------
 include/drm/ttm/ttm_bo_api.h    |    5 --
 include/drm/ttm/ttm_bo_driver.h |   24 ---------
 4 files changed, 1 insertions(+), 155 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +0 -22
index 617b646..4bde335 100644
@@ -342,22 +342,6 @@ static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
 		if (unlikely(bo->ttm == NULL))
 			ret = -ENOMEM;
 		break;
-	case ttm_bo_type_user:
-		bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
-					page_flags | TTM_PAGE_FLAG_USER,
-					glob->dummy_read_page);
-		if (unlikely(bo->ttm == NULL)) {
-			ret = -ENOMEM;
-			break;
-		}
-
-		ret = ttm_tt_set_user(bo->ttm, current,
-				      bo->buffer_start, bo->num_pages);
-		if (unlikely(ret != 0)) {
-			ttm_tt_destroy(bo->ttm);
-			bo->ttm = NULL;
-		}
-		break;
 	default:
 		printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
 		ret = -EINVAL;
@@ -907,16 +891,12 @@ static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
 }
 
 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
-				 bool disallow_fixed,
 				 uint32_t mem_type,
 				 uint32_t proposed_placement,
 				 uint32_t *masked_placement)
 {
 	uint32_t cur_flags = ttm_bo_type_flags(mem_type);
 
-	if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
-		return false;
-
 	if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
 		return false;
 
@@ -961,7 +941,6 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 		man = &bdev->man[mem_type];
 
 		type_ok = ttm_bo_mt_compatible(man,
-						bo->type == ttm_bo_type_user,
 						mem_type,
 						placement->placement[i],
 						&cur_flags);
@@ -1009,7 +988,6 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 		if (!man->has_type)
 			continue;
 		if (!ttm_bo_mt_compatible(man,
-						bo->type == ttm_bo_type_user,
 						mem_type,
 						placement->busy_placement[i],
 						&cur_flags))
drivers/gpu/drm/ttm/ttm_tt.c +1 -104

141 lines not on this page · raw message

include/drm/ttm/ttm_bo_api.h +0 -5
index 42e3469..8d95a42 100644
@@ -122,17 +122,12 @@ struct ttm_mem_reg {
  * be mmapped by user space. Each of these bos occupy a slot in the
  * device address space, that can be used for normal vm operations.
  *
- * @ttm_bo_type_user: These are user-space memory areas that are made
- * available to the GPU by mapping the buffer pages into the GPU aperture
- * space. These buffers cannot be mmaped from the device address space.
- *
  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
  * but they cannot be accessed from user-space. For kernel-only use.
  */
 
 enum ttm_bo_type {
 	ttm_bo_type_device,
-	ttm_bo_type_user,
 	ttm_bo_type_kernel
 };
 
include/drm/ttm/ttm_bo_driver.h +0 -24
index 94eb143..37527d6 100644
@@ -118,8 +118,6 @@ struct ttm_backend {
 	struct ttm_backend_func *func;
 };
 
-#define TTM_PAGE_FLAG_USER            (1 << 1)
-#define TTM_PAGE_FLAG_USER_DIRTY      (1 << 2)
 #define TTM_PAGE_FLAG_WRITE           (1 << 3)
 #define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
 #define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
@@ -146,8 +144,6 @@ enum ttm_caching_state {
  * @num_pages: Number of pages in the page array.
  * @bdev: Pointer to the current struct ttm_bo_device.
  * @be: Pointer to the ttm backend.
- * @tsk: The task for user ttm.
- * @start: virtual address for user ttm.
  * @swap_storage: Pointer to shmem struct file for swap storage.
  * @caching_state: The current caching state of the pages.
  * @state: The current binding state of the pages.
@@ -167,8 +163,6 @@ struct ttm_tt {
 	unsigned long num_pages;
 	struct ttm_bo_global *glob;
 	struct ttm_backend *be;
-	struct task_struct *tsk;
-	unsigned long start;
 	struct file *swap_storage;
 	enum ttm_caching_state caching_state;
 	enum {
@@ -618,24 +612,6 @@ extern struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev,
 				    struct page *dummy_read_page);
 
 /**
- * ttm_tt_set_user:
- *
- * @ttm: The struct ttm_tt to populate.
- * @tsk: A struct task_struct for which @start is a valid user-space address.
- * @start: A valid user-space address.
- * @num_pages: Size in pages of the user memory area.
- *
- * Populate a struct ttm_tt with a user-space memory area after first pinning
- * the pages backing it.
- * Returns:
- * !0: Error.
- */
-
-extern int ttm_tt_set_user(struct ttm_tt *ttm,
-			   struct task_struct *tsk,
-			   unsigned long start, unsigned long num_pages);
-
-/**
  * ttm_ttm_bind:
  *
  * @ttm: The struct ttm_tt containing backing pages.
Signature · 1 line
1.7.7.1
#3 lore raw

PATCH 3/14 drm/ttm: remove split btw highmen and lowmem page

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Split btw highmem and lowmem page was rendered useless by the pool code. Remove it. Note further cleanup would change the ttm page allocation helper to actualy take an array instead of relying on list this could drasticly reduce the number of function call in the common case of allocation whole buffer.

---
 drivers/gpu/drm/ttm/ttm_tt.c    |   11 ++---------
 include/drm/ttm/ttm_bo_driver.h |    7 -------
 2 files changed, 2 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_tt.c +2 -9
index c68b0e7..f0c5ffd 100644
@@ -70,7 +70,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
 	int ret;
 
-	while (NULL == (p = ttm->pages[index])) {
+	if (NULL == (p = ttm->pages[index])) {
 
 		INIT_LIST_HEAD(&h);
 
@@ -86,10 +86,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 		if (unlikely(ret != 0))
 			goto out_err;
 
-		if (PageHighMem(p))
-			ttm->pages[--ttm->first_himem_page] = p;
-		else
-			ttm->pages[++ttm->last_lomem_page] = p;
+		ttm->pages[index] = p;
 	}
 	return p;
 out_err:
@@ -271,8 +268,6 @@ static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
 	ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
 		      ttm->dma_address);
 	ttm->state = tt_unpopulated;
-	ttm->first_himem_page = ttm->num_pages;
-	ttm->last_lomem_page = -1;
 }
 
 void ttm_tt_destroy(struct ttm_tt *ttm)
@@ -316,8 +311,6 @@ struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
 
 	ttm->glob = bdev->glob;
 	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
-	ttm->first_himem_page = ttm->num_pages;
-	ttm->last_lomem_page = -1;
 	ttm->caching_state = tt_cached;
 	ttm->page_flags = page_flags;
 
include/drm/ttm/ttm_bo_driver.h +0 -7
index 37527d6..9da182b 100644
@@ -136,11 +136,6 @@ enum ttm_caching_state {
  * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
  * pointer.
  * @pages: Array of pages backing the data.
- * @first_himem_page: Himem pages are put last in the page array, which
- * enables us to run caching attribute changes on only the first part
- * of the page array containing lomem pages. This is the index of the
- * first himem page.
- * @last_lomem_page: Index of the last lomem page in the page array.
  * @num_pages: Number of pages in the page array.
  * @bdev: Pointer to the current struct ttm_bo_device.
  * @be: Pointer to the ttm backend.
@@ -157,8 +152,6 @@ enum ttm_caching_state {
 struct ttm_tt {
 	struct page *dummy_read_page;
 	struct page **pages;
-	long first_himem_page;
-	long last_lomem_page;
 	uint32_t page_flags;
 	unsigned long num_pages;
 	struct ttm_bo_global *glob;
Signature · 1 line
1.7.7.1
#4 lore raw

PATCH 4/14 drm/ttm: remove unused backend flags field

In reply to [email protected]

From: Jerome Glisse <[email protected]>

This field is not use by any of the driver just drop it.

---
 drivers/gpu/drm/radeon/radeon_ttm.c |    1 -
 include/drm/ttm/ttm_bo_driver.h     |    2 --
 2 files changed, 0 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +0 -1
index 0b5468b..97c76ae 100644
@@ -787,7 +787,6 @@ struct ttm_backend *radeon_ttm_backend_create(struct radeon_device *rdev)
 		return NULL;
 	}
 	gtt->backend.bdev = &rdev->mman.bdev;
-	gtt->backend.flags = 0;
 	gtt->backend.func = &radeon_backend_func;
 	gtt->rdev = rdev;
 	gtt->pages = NULL;
include/drm/ttm/ttm_bo_driver.h +0 -2
index 9da182b..6d17140 100644
@@ -106,7 +106,6 @@ struct ttm_backend_func {
  * struct ttm_backend
  *
  * @bdev: Pointer to a struct ttm_bo_device.
- * @flags: For driver use.
  * @func: Pointer to a struct ttm_backend_func that describes
  * the backend methods.
  *
@@ -114,7 +113,6 @@ struct ttm_backend_func {
 
 struct ttm_backend {
 	struct ttm_bo_device *bdev;
-	uint32_t flags;
 	struct ttm_backend_func *func;
 };
 
Signature · 1 line
1.7.7.1
#5 lore raw

PATCH 5/14 drm/ttm: use ttm put pages function to properly restore cache attribute

In reply to [email protected]

From: Jerome Glisse <[email protected]>

On failure we need to make sure the page we free has wb cache attribute. Do this pas call the proper ttm page helper function.

---
 drivers/gpu/drm/ttm/ttm_tt.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
drivers/gpu/drm/ttm/ttm_tt.c +4 -1
index f0c5ffd..90527a2 100644
@@ -90,7 +90,10 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 	}
 	return p;
 out_err:
-	put_page(p);
+	INIT_LIST_HEAD(&h);
+	list_add(&p->lru, &h);
+	ttm_put_pages(&h, 1, ttm->page_flags,
+		      ttm->caching_state, &ttm->dma_address[index]);
 	return NULL;
 }
 
Signature · 1 line
1.7.7.1
#6 lore raw

PATCH 6/14 drm/ttm: test for dma_address array allocation failure

In reply to [email protected]

From: Jerome Glisse <[email protected]>

---
 drivers/gpu/drm/ttm/ttm_tt.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/gpu/drm/ttm/ttm_tt.c +1 -1
index 90527a2..54bbbad 100644
@@ -320,7 +320,7 @@ struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
 	ttm->dummy_read_page = dummy_read_page;
 
 	ttm_tt_alloc_page_directory(ttm);
-	if (!ttm->pages) {
+	if (!ttm->pages || !ttm->dma_address) {
 		ttm_tt_destroy(ttm);
 		printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
 		return NULL;
Signature · 1 line
1.7.7.1
#7 lore raw

PATCH 7/14 drm/ttm: page allocation use page array instead of list

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Use the ttm_tt pages array for pages allocations, move the list unwinding into the page allocation functions.

---
 drivers/gpu/drm/ttm/ttm_page_alloc.c |   85 +++++++++++++++++++++-------------
 drivers/gpu/drm/ttm/ttm_tt.c         |   36 +++------------
 include/drm/ttm/ttm_page_alloc.h     |    8 ++--
 3 files changed, 63 insertions(+), 66 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc.c +52 -33
index 727e93d..0f3e6d2 100644
@@ -619,8 +619,10 @@ static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
  * @return count of pages still required to fulfill the request.
  */
 static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
-		struct list_head *pages, int ttm_flags,
-		enum ttm_caching_state cstate, unsigned count)
+					struct list_head *pages,
+					int ttm_flags,
+					enum ttm_caching_state cstate,
+					unsigned count)
 {
 	unsigned long irq_flags;
 	struct list_head *p;
@@ -664,13 +666,15 @@ out:
  * On success pages list will hold count number of correctly
  * cached pages.
  */
-int ttm_get_pages(struct list_head *pages, int flags,
-		  enum ttm_caching_state cstate, unsigned count,
+int ttm_get_pages(struct page **pages, int flags,
+		  enum ttm_caching_state cstate, unsigned npages,
 		  dma_addr_t *dma_address)
 {
 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
+	struct list_head plist;
 	struct page *p = NULL;
 	gfp_t gfp_flags = GFP_USER;
+	unsigned count;
 	int r;
 
 	/* set zero flag for page allocation if required */
@@ -684,7 +688,7 @@ int ttm_get_pages(struct list_head *pages, int flags,
 		else
 			gfp_flags |= GFP_HIGHUSER;
 
-		for (r = 0; r < count; ++r) {
+		for (r = 0; r < npages; ++r) {
 			p = alloc_page(gfp_flags);
 			if (!p) {
 
@@ -693,85 +697,100 @@ int ttm_get_pages(struct list_head *pages, int flags,
 				return -ENOMEM;
 			}
 
-			list_add(&p->lru, pages);
+			pages[r] = p;
 		}
 		return 0;
 	}
 
-
 	/* combine zero flag to pool flags */
 	gfp_flags |= pool->gfp_flags;
 
 	/* First we take pages from the pool */
-	count = ttm_page_pool_get_pages(pool, pages, flags, cstate, count);
+	INIT_LIST_HEAD(&plist);
+	npages = ttm_page_pool_get_pages(pool, &plist, flags, cstate, npages);
+	count = 0;
+	list_for_each_entry(p, &plist, lru) {
+		pages[count++] = p;
+	}
 
 	/* clear the pages coming from the pool if requested */
 	if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
-		list_for_each_entry(p, pages, lru) {
+		list_for_each_entry(p, &plist, lru) {
 			clear_page(page_address(p));
 		}
 	}
 
 	/* If pool didn't have enough pages allocate new one. */
-	if (count > 0) {
+	if (npages > 0) {
 		/* ttm_alloc_new_pages doesn't reference pool so we can run
 		 * multiple requests in parallel.
 		 **/
-		r = ttm_alloc_new_pages(pages, gfp_flags, flags, cstate, count);
+		INIT_LIST_HEAD(&plist);
+		r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate, npages);
+		list_for_each_entry(p, &plist, lru) {
+			pages[count++] = p;
+		}
 		if (r) {
 			/* If there is any pages in the list put them back to
 			 * the pool. */
 			printk(KERN_ERR TTM_PFX
 			       "Failed to allocate extra pages "
 			       "for large request.");
-			ttm_put_pages(pages, 0, flags, cstate, NULL);
+			ttm_put_pages(pages, count, flags, cstate, NULL);
 			return r;
 		}
 	}
 
-
 	return 0;
 }
 
 /* Put all pages in pages list to correct pool to wait for reuse */
-void ttm_put_pages(struct list_head *pages, unsigned page_count, int flags,
+void ttm_put_pages(struct page **pages, unsigned npages, int flags,
 		   enum ttm_caching_state cstate, dma_addr_t *dma_address)
 {
 	unsigned long irq_flags;
 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
-	struct page *p, *tmp;
+	unsigned i;
 
 	if (pool == NULL) {
 		/* No pool for this memory type so free the pages */
-
-		list_for_each_entry_safe(p, tmp, pages, lru) {
-			__free_page(p);
+		for (i = 0; i < npages; i++) {
+			if (pages[i]) {
+				if (page_count(pages[i]) != 1)
+					printk(KERN_ERR TTM_PFX
+					       "Erroneous page count. "
+					       "Leaking pages.\n");
+				__free_page(pages[i]);
+				pages[i] = NULL;
+			}
 		}
-		/* Make the pages list empty */
-		INIT_LIST_HEAD(pages);
 		return;
 	}
-	if (page_count == 0) {
-		list_for_each_entry_safe(p, tmp, pages, lru) {
-			++page_count;
-		}
-	}
 
 	spin_lock_irqsave(&pool->lock, irq_flags);
-	list_splice_init(pages, &pool->list);
-	pool->npages += page_count;
+	for (i = 0; i < npages; i++) {
+		if (pages[i]) {
+			if (page_count(pages[i]) != 1)
+				printk(KERN_ERR TTM_PFX
+				       "Erroneous page count. "
+				       "Leaking pages.\n");
+			list_add_tail(&pages[i]->lru, &pool->list);
+			pages[i] = NULL;
+			pool->npages++;
+		}
+	}
 	/* Check that we don't go over the pool limit */
-	page_count = 0;
+	npages = 0;
 	if (pool->npages > _manager->options.max_size) {
-		page_count = pool->npages - _manager->options.max_size;
+		npages = pool->npages - _manager->options.max_size;
 		/* free at least NUM_PAGES_TO_ALLOC number of pages
 		 * to reduce calls to set_memory_wb */
-		if (page_count < NUM_PAGES_TO_ALLOC)
-			page_count = NUM_PAGES_TO_ALLOC;
+		if (npages < NUM_PAGES_TO_ALLOC)
+			npages = NUM_PAGES_TO_ALLOC;
 	}
 	spin_unlock_irqrestore(&pool->lock, irq_flags);
-	if (page_count)
-		ttm_page_pool_free(pool, page_count);
+	if (npages)
+		ttm_page_pool_free(pool, npages);
 }
 
 static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
drivers/gpu/drm/ttm/ttm_tt.c +7 -29
index 54bbbad..6e079de 100644
@@ -66,22 +66,16 @@ static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
 static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 {
 	struct page *p;
-	struct list_head h;
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
 	int ret;
 
 	if (NULL == (p = ttm->pages[index])) {
 
-		INIT_LIST_HEAD(&h);
-
-		ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
+		ret = ttm_get_pages(&p, ttm->page_flags, ttm->caching_state, 1,
 				    &ttm->dma_address[index]);
-
 		if (ret != 0)
 			return NULL;
 
-		p = list_first_entry(&h, struct page, lru);
-
 		ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
 		if (unlikely(ret != 0))
 			goto out_err;
@@ -90,9 +84,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 	}
 	return p;
 out_err:
-	INIT_LIST_HEAD(&h);
-	list_add(&p->lru, &h);
-	ttm_put_pages(&h, 1, ttm->page_flags,
+	ttm_put_pages(&p, 1, ttm->page_flags,
 		      ttm->caching_state, &ttm->dma_address[index]);
 	return NULL;
 }
@@ -243,33 +235,19 @@ EXPORT_SYMBOL(ttm_tt_set_placement_caching);
 
 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
 {
-	int i;
-	unsigned count = 0;
-	struct list_head h;
-	struct page *cur_page;
 	struct ttm_backend *be = ttm->be;
-
-	INIT_LIST_HEAD(&h);
+	unsigned i;
 
 	if (be)
 		be->func->clear(be);
 	for (i = 0; i < ttm->num_pages; ++i) {
-
-		cur_page = ttm->pages[i];
-		ttm->pages[i] = NULL;
-		if (cur_page) {
-			if (page_count(cur_page) != 1)
-				printk(KERN_ERR TTM_PFX
-				       "Erroneous page count. "
-				       "Leaking pages.\n");
+		if (ttm->pages[i]) {
 			ttm_mem_global_free_page(ttm->glob->mem_glob,
-						 cur_page);
-			list_add(&cur_page->lru, &h);
-			count++;
+						 ttm->pages[i]);
+			ttm_put_pages(&ttm->pages[i], 1, ttm->page_flags,
+				      ttm->caching_state, &ttm->dma_address[i]);
 		}
 	}
-	ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
-		      ttm->dma_address);
 	ttm->state = tt_unpopulated;
 }
 
include/drm/ttm/ttm_page_alloc.h +4 -4
index 129de12..fe61c8d 100644
@@ -38,10 +38,10 @@
  * @count: number of pages to allocate.
  * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
  */
-int ttm_get_pages(struct list_head *pages,
+int ttm_get_pages(struct page **pages,
 		  int flags,
 		  enum ttm_caching_state cstate,
-		  unsigned count,
+		  unsigned npages,
 		  dma_addr_t *dma_address);
 /**
  * Put linked list of pages to pool.
@@ -53,8 +53,8 @@ int ttm_get_pages(struct list_head *pages,
  * @cstate: ttm caching state.
  * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
  */
-void ttm_put_pages(struct list_head *pages,
-		   unsigned page_count,
+void ttm_put_pages(struct page **pages,
+		   unsigned npages,
 		   int flags,
 		   enum ttm_caching_state cstate,
 		   dma_addr_t *dma_address);
Signature · 1 line
1.7.7.1
Reviewed-by on v1 7/14
#8 lore raw

Re: [PATCH 07/14] drm/ttm: page allocation use page array instead of list

Quoted 2 lines · On 11/16/2011 05:57 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

Quoted 290 lines

Use the ttm_tt pages array for pages allocations, move the list unwinding into the page allocation functions.

---
  drivers/gpu/drm/ttm/ttm_page_alloc.c |   85 +++++++++++++++++++++-------------
  drivers/gpu/drm/ttm/ttm_tt.c         |   36 +++------------
  include/drm/ttm/ttm_page_alloc.h     |    8 ++--
  3 files changed, 63 insertions(+), 66 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc.c +52 -33
index 727e93d..0f3e6d2 100644
@@ -619,8 +619,10 @@ static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
   * @return count of pages still required to fulfill the request.
   */
  static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
-		struct list_head *pages, int ttm_flags,
-		enum ttm_caching_state cstate, unsigned count)
+					struct list_head *pages,
+					int ttm_flags,
+					enum ttm_caching_state cstate,
+					unsigned count)
  {
  	unsigned long irq_flags;
  	struct list_head *p;
@@ -664,13 +666,15 @@ out:
   * On success pages list will hold count number of correctly
   * cached pages.
   */
-int ttm_get_pages(struct list_head *pages, int flags,
-		  enum ttm_caching_state cstate, unsigned count,
+int ttm_get_pages(struct page **pages, int flags,
+		  enum ttm_caching_state cstate, unsigned npages,
  		  dma_addr_t *dma_address)
  {
  	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
+	struct list_head plist;
  	struct page *p = NULL;
  	gfp_t gfp_flags = GFP_USER;
+	unsigned count;
  	int r;
 
  	/* set zero flag for page allocation if required */
@@ -684,7 +688,7 @@ int ttm_get_pages(struct list_head *pages, int flags,
  		else
  			gfp_flags |= GFP_HIGHUSER;
 
-		for (r = 0; r<  count; ++r) {
+		for (r = 0; r<  npages; ++r) {
  			p = alloc_page(gfp_flags);
  			if (!p) {
 
@@ -693,85 +697,100 @@ int ttm_get_pages(struct list_head *pages, int flags,
  				return -ENOMEM;
  			}
 
-			list_add(&p->lru, pages);
+			pages[r] = p;
  		}
  		return 0;
  	}
 
-
  	/* combine zero flag to pool flags */
  	gfp_flags |= pool->gfp_flags;
 
  	/* First we take pages from the pool */
-	count = ttm_page_pool_get_pages(pool, pages, flags, cstate, count);
+	INIT_LIST_HEAD(&plist);
+	npages = ttm_page_pool_get_pages(pool,&plist, flags, cstate, npages);
+	count = 0;
+	list_for_each_entry(p,&plist, lru) {
+		pages[count++] = p;
+	}
 
  	/* clear the pages coming from the pool if requested */
  	if (flags&  TTM_PAGE_FLAG_ZERO_ALLOC) {
-		list_for_each_entry(p, pages, lru) {
+		list_for_each_entry(p,&plist, lru) {
  			clear_page(page_address(p));
  		}
  	}
 
  	/* If pool didn't have enough pages allocate new one. */
-	if (count>  0) {
+	if (npages>  0) {
  		/* ttm_alloc_new_pages doesn't reference pool so we can run
  		 * multiple requests in parallel.
  		 **/
-		r = ttm_alloc_new_pages(pages, gfp_flags, flags, cstate, count);
+		INIT_LIST_HEAD(&plist);
+		r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate, npages);
+		list_for_each_entry(p,&plist, lru) {
+			pages[count++] = p;
+		}
  		if (r) {
  			/* If there is any pages in the list put them back to
  			 * the pool. */
  			printk(KERN_ERR TTM_PFX
  			       "Failed to allocate extra pages "
  			       "for large request.");
-			ttm_put_pages(pages, 0, flags, cstate, NULL);
+			ttm_put_pages(pages, count, flags, cstate, NULL);
  			return r;
  		}
  	}
 
-
  	return 0;
  }
 
  /* Put all pages in pages list to correct pool to wait for reuse */
-void ttm_put_pages(struct list_head *pages, unsigned page_count, int flags,
+void ttm_put_pages(struct page **pages, unsigned npages, int flags,
  		   enum ttm_caching_state cstate, dma_addr_t *dma_address)
  {
  	unsigned long irq_flags;
  	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
-	struct page *p, *tmp;
+	unsigned i;
 
  	if (pool == NULL) {
  		/* No pool for this memory type so free the pages */
-
-		list_for_each_entry_safe(p, tmp, pages, lru) {
-			__free_page(p);
+		for (i = 0; i<  npages; i++) {
+			if (pages[i]) {
+				if (page_count(pages[i]) != 1)
+					printk(KERN_ERR TTM_PFX
+					       "Erroneous page count. "
+					       "Leaking pages.\n");
+				__free_page(pages[i]);
+				pages[i] = NULL;
+			}
  		}
-		/* Make the pages list empty */
-		INIT_LIST_HEAD(pages);
  		return;
  	}
-	if (page_count == 0) {
-		list_for_each_entry_safe(p, tmp, pages, lru) {
-			++page_count;
-		}
-	}
 
  	spin_lock_irqsave(&pool->lock, irq_flags);
-	list_splice_init(pages,&pool->list);
-	pool->npages += page_count;
+	for (i = 0; i<  npages; i++) {
+		if (pages[i]) {
+			if (page_count(pages[i]) != 1)
+				printk(KERN_ERR TTM_PFX
+				       "Erroneous page count. "
+				       "Leaking pages.\n");
+			list_add_tail(&pages[i]->lru,&pool->list);
+			pages[i] = NULL;
+			pool->npages++;
+		}
+	}
  	/* Check that we don't go over the pool limit */
-	page_count = 0;
+	npages = 0;
  	if (pool->npages>  _manager->options.max_size) {
-		page_count = pool->npages - _manager->options.max_size;
+		npages = pool->npages - _manager->options.max_size;
  		/* free at least NUM_PAGES_TO_ALLOC number of pages
  		 * to reduce calls to set_memory_wb */
-		if (page_count<  NUM_PAGES_TO_ALLOC)
-			page_count = NUM_PAGES_TO_ALLOC;
+		if (npages<  NUM_PAGES_TO_ALLOC)
+			npages = NUM_PAGES_TO_ALLOC;
  	}
  	spin_unlock_irqrestore(&pool->lock, irq_flags);
-	if (page_count)
-		ttm_page_pool_free(pool, page_count);
+	if (npages)
+		ttm_page_pool_free(pool, npages);
  }
 
  static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
drivers/gpu/drm/ttm/ttm_tt.c +7 -29
index 54bbbad..6e079de 100644
@@ -66,22 +66,16 @@ static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
  static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
  {
  	struct page *p;
-	struct list_head h;
  	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  	int ret;
 
  	if (NULL == (p = ttm->pages[index])) {
 
-		INIT_LIST_HEAD(&h);
-
-		ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
+		ret = ttm_get_pages(&p, ttm->page_flags, ttm->caching_state, 1,
  				&ttm->dma_address[index]);
-
  		if (ret != 0)
  			return NULL;
 
-		p = list_first_entry(&h, struct page, lru);
-
  		ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
  		if (unlikely(ret != 0))
  			goto out_err;
@@ -90,9 +84,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
  	}
  	return p;
  out_err:
-	INIT_LIST_HEAD(&h);
-	list_add(&p->lru,&h);
-	ttm_put_pages(&h, 1, ttm->page_flags,
+	ttm_put_pages(&p, 1, ttm->page_flags,
  		      ttm->caching_state,&ttm->dma_address[index]);
  	return NULL;
  }
@@ -243,33 +235,19 @@ EXPORT_SYMBOL(ttm_tt_set_placement_caching);
 
  static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
  {
-	int i;
-	unsigned count = 0;
-	struct list_head h;
-	struct page *cur_page;
  	struct ttm_backend *be = ttm->be;
-
-	INIT_LIST_HEAD(&h);
+	unsigned i;
 
  	if (be)
  		be->func->clear(be);
  	for (i = 0; i<  ttm->num_pages; ++i) {
-
-		cur_page = ttm->pages[i];
-		ttm->pages[i] = NULL;
-		if (cur_page) {
-			if (page_count(cur_page) != 1)
-				printk(KERN_ERR TTM_PFX
-				       "Erroneous page count. "
-				       "Leaking pages.\n");
+		if (ttm->pages[i]) {
  			ttm_mem_global_free_page(ttm->glob->mem_glob,
-						 cur_page);
-			list_add(&cur_page->lru,&h);
-			count++;
+						 ttm->pages[i]);
+			ttm_put_pages(&ttm->pages[i], 1, ttm->page_flags,
+				      ttm->caching_state,&ttm->dma_address[i]);
  		}
  	}
-	ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
-		      ttm->dma_address);
  	ttm->state = tt_unpopulated;
  }
 
include/drm/ttm/ttm_page_alloc.h +4 -4
index 129de12..fe61c8d 100644
@@ -38,10 +38,10 @@
   * @count: number of pages to allocate.
   * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
   */
-int ttm_get_pages(struct list_head *pages,
+int ttm_get_pages(struct page **pages,
  		  int flags,
  		  enum ttm_caching_state cstate,
-		  unsigned count,
+		  unsigned npages,
  		  dma_addr_t *dma_address);
  /**
   * Put linked list of pages to pool.
@@ -53,8 +53,8 @@ int ttm_get_pages(struct list_head *pages,
   * @cstate: ttm caching state.
   * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
   */
-void ttm_put_pages(struct list_head *pages,
-		   unsigned page_count,
+void ttm_put_pages(struct page **pages,
+		   unsigned npages,
  		   int flags,
  		   enum ttm_caching_state cstate,
  		   dma_addr_t *dma_address);
Reviewed-by on v1 7/14
#9 lore raw

Re: [PATCH 07/14] drm/ttm: page allocation use page array instead of list

In reply to [email protected]
Quoted 6 lines · On Wed, Nov 16, 2011 at 11:57:31AM -0500, [email protected] wrote:

From: Jerome Glisse <[email protected]>

Use the ttm_tt pages array for pages allocations, move the list unwinding into the page allocation functions.

Quoted 287 lines
---
 drivers/gpu/drm/ttm/ttm_page_alloc.c |   85 +++++++++++++++++++++-------------
 drivers/gpu/drm/ttm/ttm_tt.c         |   36 +++------------
 include/drm/ttm/ttm_page_alloc.h     |    8 ++--
 3 files changed, 63 insertions(+), 66 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc.c +52 -33
index 727e93d..0f3e6d2 100644
@@ -619,8 +619,10 @@ static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
  * @return count of pages still required to fulfill the request.
  */
 static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
-		struct list_head *pages, int ttm_flags,
-		enum ttm_caching_state cstate, unsigned count)
+					struct list_head *pages,
+					int ttm_flags,
+					enum ttm_caching_state cstate,
+					unsigned count)
 {
 	unsigned long irq_flags;
 	struct list_head *p;
@@ -664,13 +666,15 @@ out:
  * On success pages list will hold count number of correctly
  * cached pages.
  */
-int ttm_get_pages(struct list_head *pages, int flags,
-		  enum ttm_caching_state cstate, unsigned count,
+int ttm_get_pages(struct page **pages, int flags,
+		  enum ttm_caching_state cstate, unsigned npages,
 		  dma_addr_t *dma_address)
 {
 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
+	struct list_head plist;
 	struct page *p = NULL;
 	gfp_t gfp_flags = GFP_USER;
+	unsigned count;
 	int r;
 
 	/* set zero flag for page allocation if required */
@@ -684,7 +688,7 @@ int ttm_get_pages(struct list_head *pages, int flags,
 		else
 			gfp_flags |= GFP_HIGHUSER;
 
-		for (r = 0; r < count; ++r) {
+		for (r = 0; r < npages; ++r) {
 			p = alloc_page(gfp_flags);
 			if (!p) {
 
@@ -693,85 +697,100 @@ int ttm_get_pages(struct list_head *pages, int flags,
 				return -ENOMEM;
 			}
 
-			list_add(&p->lru, pages);
+			pages[r] = p;
 		}
 		return 0;
 	}
 
-
 	/* combine zero flag to pool flags */
 	gfp_flags |= pool->gfp_flags;
 
 	/* First we take pages from the pool */
-	count = ttm_page_pool_get_pages(pool, pages, flags, cstate, count);
+	INIT_LIST_HEAD(&plist);
+	npages = ttm_page_pool_get_pages(pool, &plist, flags, cstate, npages);
+	count = 0;
+	list_for_each_entry(p, &plist, lru) {
+		pages[count++] = p;
+	}
 
 	/* clear the pages coming from the pool if requested */
 	if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
-		list_for_each_entry(p, pages, lru) {
+		list_for_each_entry(p, &plist, lru) {
 			clear_page(page_address(p));
 		}
 	}
 
 	/* If pool didn't have enough pages allocate new one. */
-	if (count > 0) {
+	if (npages > 0) {
 		/* ttm_alloc_new_pages doesn't reference pool so we can run
 		 * multiple requests in parallel.
 		 **/
-		r = ttm_alloc_new_pages(pages, gfp_flags, flags, cstate, count);
+		INIT_LIST_HEAD(&plist);
+		r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate, npages);
+		list_for_each_entry(p, &plist, lru) {
+			pages[count++] = p;
+		}
 		if (r) {
 			/* If there is any pages in the list put them back to
 			 * the pool. */
 			printk(KERN_ERR TTM_PFX
 			       "Failed to allocate extra pages "
 			       "for large request.");
-			ttm_put_pages(pages, 0, flags, cstate, NULL);
+			ttm_put_pages(pages, count, flags, cstate, NULL);
 			return r;
 		}
 	}
 
-
 	return 0;
 }
 
 /* Put all pages in pages list to correct pool to wait for reuse */
-void ttm_put_pages(struct list_head *pages, unsigned page_count, int flags,
+void ttm_put_pages(struct page **pages, unsigned npages, int flags,
 		   enum ttm_caching_state cstate, dma_addr_t *dma_address)
 {
 	unsigned long irq_flags;
 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
-	struct page *p, *tmp;
+	unsigned i;
 
 	if (pool == NULL) {
 		/* No pool for this memory type so free the pages */
-
-		list_for_each_entry_safe(p, tmp, pages, lru) {
-			__free_page(p);
+		for (i = 0; i < npages; i++) {
+			if (pages[i]) {
+				if (page_count(pages[i]) != 1)
+					printk(KERN_ERR TTM_PFX
+					       "Erroneous page count. "
+					       "Leaking pages.\n");
+				__free_page(pages[i]);
+				pages[i] = NULL;
+			}
 		}
-		/* Make the pages list empty */
-		INIT_LIST_HEAD(pages);
 		return;
 	}
-	if (page_count == 0) {
-		list_for_each_entry_safe(p, tmp, pages, lru) {
-			++page_count;
-		}
-	}
 
 	spin_lock_irqsave(&pool->lock, irq_flags);
-	list_splice_init(pages, &pool->list);
-	pool->npages += page_count;
+	for (i = 0; i < npages; i++) {
+		if (pages[i]) {
+			if (page_count(pages[i]) != 1)
+				printk(KERN_ERR TTM_PFX
+				       "Erroneous page count. "
+				       "Leaking pages.\n");
+			list_add_tail(&pages[i]->lru, &pool->list);
+			pages[i] = NULL;
+			pool->npages++;
+		}
+	}
 	/* Check that we don't go over the pool limit */
-	page_count = 0;
+	npages = 0;
 	if (pool->npages > _manager->options.max_size) {
-		page_count = pool->npages - _manager->options.max_size;
+		npages = pool->npages - _manager->options.max_size;
 		/* free at least NUM_PAGES_TO_ALLOC number of pages
 		 * to reduce calls to set_memory_wb */
-		if (page_count < NUM_PAGES_TO_ALLOC)
-			page_count = NUM_PAGES_TO_ALLOC;
+		if (npages < NUM_PAGES_TO_ALLOC)
+			npages = NUM_PAGES_TO_ALLOC;
 	}
 	spin_unlock_irqrestore(&pool->lock, irq_flags);
-	if (page_count)
-		ttm_page_pool_free(pool, page_count);
+	if (npages)
+		ttm_page_pool_free(pool, npages);
 }
 
 static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
drivers/gpu/drm/ttm/ttm_tt.c +7 -29
index 54bbbad..6e079de 100644
@@ -66,22 +66,16 @@ static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
 static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 {
 	struct page *p;
-	struct list_head h;
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
 	int ret;
 
 	if (NULL == (p = ttm->pages[index])) {
 
-		INIT_LIST_HEAD(&h);
-
-		ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
+		ret = ttm_get_pages(&p, ttm->page_flags, ttm->caching_state, 1,
 				    &ttm->dma_address[index]);
-
 		if (ret != 0)
 			return NULL;
 
-		p = list_first_entry(&h, struct page, lru);
-
 		ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
 		if (unlikely(ret != 0))
 			goto out_err;
@@ -90,9 +84,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 	}
 	return p;
 out_err:
-	INIT_LIST_HEAD(&h);
-	list_add(&p->lru, &h);
-	ttm_put_pages(&h, 1, ttm->page_flags,
+	ttm_put_pages(&p, 1, ttm->page_flags,
 		      ttm->caching_state, &ttm->dma_address[index]);
 	return NULL;
 }
@@ -243,33 +235,19 @@ EXPORT_SYMBOL(ttm_tt_set_placement_caching);
 
 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
 {
-	int i;
-	unsigned count = 0;
-	struct list_head h;
-	struct page *cur_page;
 	struct ttm_backend *be = ttm->be;
-
-	INIT_LIST_HEAD(&h);
+	unsigned i;
 
 	if (be)
 		be->func->clear(be);
 	for (i = 0; i < ttm->num_pages; ++i) {
-
-		cur_page = ttm->pages[i];
-		ttm->pages[i] = NULL;
-		if (cur_page) {
-			if (page_count(cur_page) != 1)
-				printk(KERN_ERR TTM_PFX
-				       "Erroneous page count. "
-				       "Leaking pages.\n");
+		if (ttm->pages[i]) {
 			ttm_mem_global_free_page(ttm->glob->mem_glob,
-						 cur_page);
-			list_add(&cur_page->lru, &h);
-			count++;
+						 ttm->pages[i]);
+			ttm_put_pages(&ttm->pages[i], 1, ttm->page_flags,
+				      ttm->caching_state, &ttm->dma_address[i]);
 		}
 	}
-	ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
-		      ttm->dma_address);
 	ttm->state = tt_unpopulated;
 }
 
include/drm/ttm/ttm_page_alloc.h +4 -4
index 129de12..fe61c8d 100644
@@ -38,10 +38,10 @@
  * @count: number of pages to allocate.
  * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
  */
-int ttm_get_pages(struct list_head *pages,
+int ttm_get_pages(struct page **pages,
 		  int flags,
 		  enum ttm_caching_state cstate,
-		  unsigned count,
+		  unsigned npages,
 		  dma_addr_t *dma_address);
 /**
  * Put linked list of pages to pool.
@@ -53,8 +53,8 @@ int ttm_get_pages(struct list_head *pages,
  * @cstate: ttm caching state.
  * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
  */
-void ttm_put_pages(struct list_head *pages,
-		   unsigned page_count,
+void ttm_put_pages(struct page **pages,
+		   unsigned npages,
 		   int flags,
 		   enum ttm_caching_state cstate,
 		   dma_addr_t *dma_address);
Signature · 1 line
1.7.7.1
#10 lore raw

PATCH 8/14 drm/ttm: merge ttm_backend and ttm_tt V5

In reply to [email protected]

From: Jerome Glisse <[email protected]>

ttm_backend will only exist with a ttm_tt, and ttm_tt will only be of interest when bound to a backend. Merge them to avoid code and data duplication.

V2 Rebase on top of memory accounting overhaul
V3 Rebase on top of more memory accounting changes
V4 Rebase on top of no memory account changes (where/when is my
delorean when i need it ?)
V5 make sure ttm is unbound before destroying, change commit
message on suggestion from Tormod Volden

---
 drivers/gpu/drm/nouveau/nouveau_bo.c    |   14 ++-
 drivers/gpu/drm/nouveau/nouveau_drv.h   |    5 +-
 drivers/gpu/drm/nouveau/nouveau_sgdma.c |  188 ++++++++++++---------------
 drivers/gpu/drm/radeon/radeon_ttm.c     |  219 ++++++++++++-------------------
 drivers/gpu/drm/ttm/ttm_agp_backend.c   |   88 +++++--------
 drivers/gpu/drm/ttm/ttm_bo.c            |    9 +-
 drivers/gpu/drm/ttm/ttm_tt.c            |   59 ++-------
 drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c  |   66 +++-------
 include/drm/ttm/ttm_bo_driver.h         |  104 ++++++---------
 9 files changed, 294 insertions(+), 458 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +9 -5
index 7cc37e6..3f116ba 100644
@@ -343,8 +343,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
 		*mem = val;
 }
 
-static struct ttm_backend *
-nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
+static struct ttm_tt *
+nouveau_ttm_tt_create(struct ttm_bo_device *bdev,
+		      unsigned long size, uint32_t page_flags,
+		      struct page *dummy_read_page)
 {
 	struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
 	struct drm_device *dev = dev_priv->dev;
@@ -352,11 +354,13 @@ nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
 	switch (dev_priv->gart_info.type) {
 #if __OS_HAS_AGP
 	case NOUVEAU_GART_AGP:
-		return ttm_agp_backend_init(bdev, dev->agp->bridge);
+		return ttm_agp_tt_create(bdev, dev->agp->bridge,
+					 size, page_flags, dummy_read_page);
 #endif
 	case NOUVEAU_GART_PDMA:
 	case NOUVEAU_GART_HW:
-		return nouveau_sgdma_init_ttm(dev);
+		return nouveau_sgdma_create_ttm(bdev, size, page_flags,
+						dummy_read_page);
 	default:
 		NV_ERROR(dev, "Unknown GART type %d\n",
 			 dev_priv->gart_info.type);
@@ -1045,7 +1049,7 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 }
 
 struct ttm_bo_driver nouveau_bo_driver = {
-	.create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
+	.ttm_tt_create = &nouveau_ttm_tt_create,
 	.invalidate_caches = nouveau_bo_invalidate_caches,
 	.init_mem_type = nouveau_bo_init_mem_type,
 	.evict_flags = nouveau_bo_evict_flags,
drivers/gpu/drm/nouveau/nouveau_drv.h +4 -1
index 29837da..0c53e39 100644
@@ -1000,7 +1000,10 @@ extern int nouveau_sgdma_init(struct drm_device *);
 extern void nouveau_sgdma_takedown(struct drm_device *);
 extern uint32_t nouveau_sgdma_get_physical(struct drm_device *,
 					   uint32_t offset);
-extern struct ttm_backend *nouveau_sgdma_init_ttm(struct drm_device *);
+extern struct ttm_tt *nouveau_sgdma_create_ttm(struct ttm_bo_device *bdev,
+					       unsigned long size,
+					       uint32_t page_flags,
+					       struct page *dummy_read_page);
 
 /* nouveau_debugfs.c */
 #if defined(CONFIG_DRM_NOUVEAU_DEBUG)
drivers/gpu/drm/nouveau/nouveau_sgdma.c +85 -103

376 lines not on this page · raw message

drivers/gpu/drm/radeon/radeon_ttm.c +83 -136

238 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_agp_backend.c +34 -54

139 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_bo.c +3 -6
index 4bde335..cb73527 100644
@@ -337,8 +337,8 @@ static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
 		if (zero_alloc)
 			page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
 	case ttm_bo_type_kernel:
-		bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
-					page_flags, glob->dummy_read_page);
+		bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
+						      page_flags, glob->dummy_read_page);
 		if (unlikely(bo->ttm == NULL))
 			ret = -ENOMEM;
 		break;
@@ -1437,10 +1437,7 @@ int ttm_bo_global_init(struct drm_global_reference *ref)
 		goto out_no_shrink;
 	}
 
-	glob->ttm_bo_extra_size =
-		ttm_round_pot(sizeof(struct ttm_tt)) +
-		ttm_round_pot(sizeof(struct ttm_backend));
-
+	glob->ttm_bo_extra_size = ttm_round_pot(sizeof(struct ttm_tt));
 	glob->ttm_bo_size = glob->ttm_bo_extra_size +
 		ttm_round_pot(sizeof(struct ttm_buffer_object));
 
drivers/gpu/drm/ttm/ttm_tt.c +14 -45

140 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c +21 -45

113 lines not on this page · raw message

include/drm/ttm/ttm_bo_driver.h +41 -63

186 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#11 lore raw

PATCH 9/14 drm/ttm: introduce callback for ttm_tt populate & unpopulate V4

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Move the page allocation and freeing to driver callback and provide ttm code helper function for those.

Most intrusive change, is the fact that we now only fully populate an object this simplify some of code designed around the page fault design.

V2 Rebase on top of memory accounting overhaul
V3 New rebase on top of more memory accouting changes V4 Rebase on top of no memory account changes (where/when is my
delorean when i need it ?)

---
 drivers/gpu/drm/nouveau/nouveau_bo.c   |    3 +
 drivers/gpu/drm/radeon/radeon_ttm.c    |    2 +
 drivers/gpu/drm/ttm/ttm_bo_util.c      |   31 ++++++-----
 drivers/gpu/drm/ttm/ttm_bo_vm.c        |    9 +++-
 drivers/gpu/drm/ttm/ttm_page_alloc.c   |   57 ++++++++++++++++++++
 drivers/gpu/drm/ttm/ttm_tt.c           |   91 ++------------------------------
 drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c |    3 +
 include/drm/ttm/ttm_bo_driver.h        |   41 ++++++++------
 include/drm/ttm/ttm_page_alloc.h       |   18 ++++++
 9 files changed, 135 insertions(+), 120 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +3 -0
index 3f116ba..3271001 100644
@@ -28,6 +28,7 @@
  */
 
 #include "drmP.h"
+#include "ttm/ttm_page_alloc.h"
 
 #include "nouveau_drm.h"
 #include "nouveau_drv.h"
@@ -1050,6 +1051,8 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 
 struct ttm_bo_driver nouveau_bo_driver = {
 	.ttm_tt_create = &nouveau_ttm_tt_create,
+	.ttm_tt_populate = &ttm_pool_populate,
+	.ttm_tt_unpopulate = &ttm_pool_unpopulate,
 	.invalidate_caches = nouveau_bo_invalidate_caches,
 	.init_mem_type = nouveau_bo_init_mem_type,
 	.evict_flags = nouveau_bo_evict_flags,
drivers/gpu/drm/radeon/radeon_ttm.c +2 -0
index af4d5f2..b1768cb 100644
@@ -581,6 +581,8 @@ struct ttm_tt *radeon_ttm_tt_create(struct ttm_bo_device *bdev,
 
 static struct ttm_bo_driver radeon_bo_driver = {
 	.ttm_tt_create = &radeon_ttm_tt_create,
+	.ttm_tt_populate = &ttm_pool_populate,
+	.ttm_tt_unpopulate = &ttm_pool_unpopulate,
 	.invalidate_caches = &radeon_invalidate_caches,
 	.init_mem_type = &radeon_init_mem_type,
 	.evict_flags = &radeon_evict_flags,
drivers/gpu/drm/ttm/ttm_bo_util.c +17 -14
index 082fcae..60f204d 100644
@@ -244,7 +244,7 @@ static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
 				unsigned long page,
 				pgprot_t prot)
 {
-	struct page *d = ttm_tt_get_page(ttm, page);
+	struct page *d = ttm->pages[page];
 	void *dst;
 
 	if (!d)
@@ -281,7 +281,7 @@ static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
 				unsigned long page,
 				pgprot_t prot)
 {
-	struct page *s = ttm_tt_get_page(ttm, page);
+	struct page *s = ttm->pages[page];
 	void *src;
 
 	if (!s)
@@ -342,6 +342,12 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
 	if (old_iomap == NULL && ttm == NULL)
 		goto out2;
 
+	if (ttm->state == tt_unpopulated) {
+		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
+		if (ret)
+			goto out1;
+	}
+
 	add = 0;
 	dir = 1;
 
@@ -502,10 +508,16 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 {
 	struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
 	struct ttm_tt *ttm = bo->ttm;
-	struct page *d;
-	int i;
+	int ret;
 
 	BUG_ON(!ttm);
+
+	if (ttm->state == tt_unpopulated) {
+		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
+		if (ret)
+			return ret;
+	}
+
 	if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
 		/*
 		 * We're mapping a single page, and the desired
@@ -513,18 +525,9 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 		 */
 
 		map->bo_kmap_type = ttm_bo_map_kmap;
-		map->page = ttm_tt_get_page(ttm, start_page);
+		map->page = ttm->pages[start_page];
 		map->virtual = kmap(map->page);
 	} else {
-	    /*
-	     * Populate the part we're mapping;
-	     */
-		for (i = start_page; i < start_page + num_pages; ++i) {
-			d = ttm_tt_get_page(ttm, i);
-			if (!d)
-				return -ENOMEM;
-		}
-
 		/*
 		 * We need to use vmap to get the desired page protection
 		 * or to make the buffer object look contiguous.
drivers/gpu/drm/ttm/ttm_bo_vm.c +7 -2
index 221b924..5441284 100644
@@ -174,18 +174,23 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 		vma->vm_page_prot = (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
 		    vm_get_page_prot(vma->vm_flags) :
 		    ttm_io_prot(bo->mem.placement, vma->vm_page_prot);
+
+		/* Allocate all page at once, most common usage */
+		if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
+			retval = VM_FAULT_OOM;
+			goto out_io_unlock;
+		}
 	}
 
 	/*
 	 * Speculatively prefault a number of pages. Only error on
 	 * first page.
 	 */
-
 	for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
 		if (bo->mem.bus.is_iomem)
 			pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
 		else {
-			page = ttm_tt_get_page(ttm, page_offset);
+			page = ttm->pages[page_offset];
 			if (unlikely(!page && i == 0)) {
 				retval = VM_FAULT_OOM;
 				goto out_io_unlock;
drivers/gpu/drm/ttm/ttm_page_alloc.c +57 -0

63 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_tt.c +5 -86

145 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c +3 -0
index cc72435..3986d74 100644
@@ -28,6 +28,7 @@
 #include "vmwgfx_drv.h"
 #include "ttm/ttm_bo_driver.h"
 #include "ttm/ttm_placement.h"
+#include "ttm/ttm_page_alloc.h"
 
 static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM |
 	TTM_PL_FLAG_CACHED;
@@ -334,6 +335,8 @@ static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
 
 struct ttm_bo_driver vmw_bo_driver = {
 	.ttm_tt_create = &vmw_ttm_tt_create,
+	.ttm_tt_populate = &ttm_pool_populate,
+	.ttm_tt_unpopulate = &ttm_pool_unpopulate,
 	.invalidate_caches = vmw_invalidate_caches,
 	.init_mem_type = vmw_init_mem_type,
 	.evict_flags = vmw_evict_flags,
include/drm/ttm/ttm_bo_driver.h +23 -18

63 lines not on this page · raw message

include/drm/ttm/ttm_page_alloc.h +18 -0
index fe61c8d..18deeee 100644
@@ -68,6 +68,24 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages);
 void ttm_page_alloc_fini(void);
 
 /**
+ * ttm_pool_populate:
+ *
+ * @ttm: The struct ttm_tt to contain the backing pages.
+ *
+ * Add backing pages to all of @ttm
+ */
+extern int ttm_pool_populate(struct ttm_tt *ttm);
+
+/**
+ * ttm_pool_unpopulate:
+ *
+ * @ttm: The struct ttm_tt which to free backing pages.
+ *
+ * Free all pages of @ttm
+ */
+extern void ttm_pool_unpopulate(struct ttm_tt *ttm);
+
+/**
  * Output the state of pools to debugfs file
  */
 extern int ttm_page_alloc_debugfs(struct seq_file *m, void *data);
Signature · 1 line
1.7.7.1
#12 lore raw

PATCH 10/14 drm/ttm: provide dma aware ttm page pool code V9

In reply to [email protected]

From: Konrad Rzeszutek Wilk <[email protected]>

In TTM world the pages for the graphic drivers are kept in three different
pools: write combined, uncached, and cached (write-back). When the pages are used by the graphic driver the graphic adapter via its built in MMU (or AGP) programs these pages in. The programming requires the virtual address (from the graphic adapter perspective) and the physical address (either System RAM or the memory on the card) which is obtained using the pci_map_* calls (which does the virtual to physical - or bus address translation). During the graphic application's "life" those pages can be shuffled around, swapped out to disk, moved from the VRAM to System RAM or vice-versa. This all works with the existing TTM pool code
- except when we want to use the software IOTLB (SWIOTLB) code to "map" the physical addresses to the graphic adapter MMU. We end up programming the bounce buffer's physical address instead of the TTM pool memory's and get a non-worky driver. There are two solutions:
1) using the DMA API to allocate pages that are screened by the DMA API, or
2) using the pci_sync_* calls to copy the pages from the bounce-buffer and back.

This patch fixes the issue by allocating pages using the DMA API. The second is a viable option - but it has performance drawbacks and potential correctness issues - think of the write cache page being bounced (SWIOTLB->TTM), the WC is set on the TTM page and the copy from SWIOTLB not making it to the TTM page until the page has been recycled in the pool (and used by another application).

The bounce buffer does not get activated often - only in cases where we have a 32-bit capable card and we want to use a page that is allocated above the 4GB limit. The bounce buffer offers the solution of copying the contents of that 4GB page to an location below 4GB and then back when the operation has been completed (or vice-versa). This is done by using the 'pci_sync_*' calls.
Note: If you look carefully enough in the existing TTM page pool code you will notice the GFP_DMA32 flag is used - which should guarantee that the provided page is under 4GB. It certainly is the case, except this gets ignored in two cases:
- If user specifies 'swiotlb=force' which bounces _every_ page.
- If user is using a Xen's PV Linux guest (which uses the SWIOTLB and the underlaying PFN's aren't necessarily under 4GB).

To not have this extra copying done the other option is to allocate the pages using the DMA API so that there is not need to map the page and perform the expensive 'pci_sync_*' calls.

This DMA API capable TTM pool requires for this the 'struct device' to properly call the DMA API. It also has to track the virtual and bus address of the page being handed out in case it ends up being swapped out or de-allocated - to make sure it is de-allocated using the proper's 'struct device'.

Implementation wise the code keeps two lists: one that is attached to the 'struct device' (via the dev->dma_pools list) and a global one to be used when the 'struct device' is unavailable (think shrinker code). The global list can iterate over all of the 'struct device' and its associated dma_pool. The list in dev->dma_pools can only iterate the device's dma_pool.

                                                            /[struct device_pool]\
        /---------------------------------------------------| dev                |
       /                                            +-------| dma_pool           |
 /-----+------\                                    /        \--------------------/
 |struct device|     /-->[struct dma_pool for WC]</         /[struct device_pool]\
 | dma_pools   +----+                                     /-| dev                |
 |  ...        |    \--->[struct dma_pool for uncached]<-/--| dma_pool           |
 \-----+------/                                         /   \--------------------/
        \----------------------------------------------/

[Two pools associated with the device (WC and UC), and the parallel list containing the 'struct dev' and 'struct dma_pool' entries]

The maximum amount of dma pools a device can have is six: write-combined, uncached, and cached; then there are the DMA32 variants which are:
write-combined dma32, uncached dma32, and cached dma32.

Currently this code only gets activated when any variant of the SWIOTLB IOMMU code is running (Intel without VT-d, AMD without GART, IBM Calgary and Xen PV with PCI devices).

[v1: Using swiotlb_nr_tbl instead of swiotlb_enabled]

[v2: Major overhaul - added 'inuse_list' to seperate used from inuse and reorder the order of lists to get better performance.]

[v3: Added comments/and some logic based on review, Added Jerome tag]
[v4: rebase on top of ttm_tt & ttm_backend merge]
[v5: rebase on top of ttm memory accounting overhaul]
[v6: New rebase on top of more memory accouting changes]
[v7: well rebase on top of no memory accounting changes]
[v8: make sure pages list is initialized empty]
[v9: calll ttm_mem_global_free_page in unpopulate for accurate accountg]
---
 drivers/gpu/drm/ttm/Makefile             |    4 +
 drivers/gpu/drm/ttm/ttm_memory.c         |    2 +
 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 1134 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/ttm/ttm_tt.c             |    1 +
 include/drm/ttm/ttm_bo_driver.h          |    2 +
 include/drm/ttm/ttm_page_alloc.h         |   36 +
 6 files changed, 1179 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c
drivers/gpu/drm/ttm/Makefile +4 -0
index f3cf6f0..b2b33dd 100644
@@ -7,4 +7,8 @@ ttm-y := ttm_agp_backend.o ttm_memory.o ttm_tt.o ttm_bo.o \
 	ttm_object.o ttm_lock.o ttm_execbuf_util.o ttm_page_alloc.o \
 	ttm_bo_manager.o
 
+ifeq ($(CONFIG_SWIOTLB),y)
+ttm-y += ttm_page_alloc_dma.o
+endif
+
 obj-$(CONFIG_DRM_TTM) += ttm.o
drivers/gpu/drm/ttm/ttm_memory.c +2 -0
index e70ddd8..9eba8e9 100644
@@ -395,6 +395,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
 		       zone->name, (unsigned long long) zone->max_mem >> 10);
 	}
 	ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
+	ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
 	return 0;
 out_no_zone:
 	ttm_mem_global_release(glob);
@@ -409,6 +410,7 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
 
 	/* let the page allocator first stop the shrink work. */
 	ttm_page_alloc_fini();
+	ttm_dma_page_alloc_fini();
 
 	flush_workqueue(glob->swap_queue);
 	destroy_workqueue(glob->swap_queue);
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c new file +1134 -0

1,134 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_tt.c +1 -0
index 77f0e6f..1625739 100644
@@ -196,6 +196,7 @@ int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
 	ttm->dummy_read_page = dummy_read_page;
 	ttm->state = tt_unpopulated;
 
+	INIT_LIST_HEAD(&ttm->alloc_list);
 	ttm_tt_alloc_page_directory(ttm);
 	if (!ttm->pages || !ttm->dma_address) {
 		ttm_tt_destroy(ttm);
include/drm/ttm/ttm_bo_driver.h +2 -0
index ae06e42..beef9ab 100644
@@ -104,6 +104,7 @@ enum ttm_caching_state {
  * @caching_state: The current caching state of the pages.
  * @state: The current binding state of the pages.
  * @dma_address: The DMA (bus) addresses of the pages (if TTM_PAGE_FLAG_DMA32)
+ * @alloc_list: used by some page allocation backend
  *
  * This is a structure holding the pages, caching- and aperture binding
  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
@@ -127,6 +128,7 @@ struct ttm_tt {
 		tt_unpopulated,
 	} state;
 	dma_addr_t *dma_address;
+	struct list_head alloc_list;
 };
 
 #define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
include/drm/ttm/ttm_page_alloc.h +36 -0
index 18deeee..1e1337e 100644
@@ -89,4 +89,40 @@ extern void ttm_pool_unpopulate(struct ttm_tt *ttm);
  * Output the state of pools to debugfs file
  */
 extern int ttm_page_alloc_debugfs(struct seq_file *m, void *data);
+
+
+#ifdef CONFIG_SWIOTLB
+/**
+ * Initialize pool allocator.
+ */
+int ttm_dma_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages);
+
+/**
+ * Free pool allocator.
+ */
+void ttm_dma_page_alloc_fini(void);
+
+/**
+ * Output the state of pools to debugfs file
+ */
+extern int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data);
+
+int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev);
+extern void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev);
+
+#else
+static inline int ttm_dma_page_alloc_init(struct ttm_mem_global *glob,
+					  unsigned max_pages)
+{
+	return -ENODEV;
+}
+
+static inline void ttm_dma_page_alloc_fini(void) { return; }
+
+static inline int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data)
+{
+	return 0;
+}
+#endif
+
 #endif
Signature · 6 lines
1.7.7.1

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#13 lore raw

PATCH 11/14 drm/radeon/kms: enable the ttm dma pool if swiotlb is on V4

In reply to [email protected]

From: Konrad Rzeszutek Wilk <[email protected]>

With the exception that we do not handle the AGP case. We only deal with PCIe cards such as ATI ES1000 or HD3200 that have been detected to only do DMA up to 32-bits.

V2 force dma32 if we fail to set bigger dma mask
V3 Rebase on top of no memory account changes (where/when is my
delorean when i need it ?)
V4 add debugfs entry is swiotlb is active not only if we are
on dma 32bits only gpu

---
 drivers/gpu/drm/radeon/radeon.h        |    1 -
 drivers/gpu/drm/radeon/radeon_device.c |    6 ++
 drivers/gpu/drm/radeon/radeon_gart.c   |   29 +-----------
 drivers/gpu/drm/radeon/radeon_ttm.c    |   83 +++++++++++++++++++++++++++++--
 4 files changed, 84 insertions(+), 35 deletions(-)
drivers/gpu/drm/radeon/radeon.h +0 -1
index fc5a1d6..de38e70 100644
@@ -320,7 +320,6 @@ struct radeon_gart {
 	unsigned			table_size;
 	struct page			**pages;
 	dma_addr_t			*pages_addr;
-	bool				*ttm_alloced;
 	bool				ready;
 };
 
drivers/gpu/drm/radeon/radeon_device.c +6 -0
index c4d00a1..fb347a8 100644
@@ -765,8 +765,14 @@ int radeon_device_init(struct radeon_device *rdev,
 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
 	if (r) {
 		rdev->need_dma32 = true;
+		dma_bits = 32;
 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
 	}
+	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
+	if (r) {
+		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
+		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
+	}
 
 	/* Registers mapping */
 	/* TODO: block userspace mapping of io register */
drivers/gpu/drm/radeon/radeon_gart.c +1 -28
index ba7ab79..a4d9816 100644
@@ -157,9 +157,6 @@ void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
 	p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
 	for (i = 0; i < pages; i++, p++) {
 		if (rdev->gart.pages[p]) {
-			if (!rdev->gart.ttm_alloced[p])
-				pci_unmap_page(rdev->pdev, rdev->gart.pages_addr[p],
-						PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
 			rdev->gart.pages[p] = NULL;
 			rdev->gart.pages_addr[p] = rdev->dummy_page.addr;
 			page_base = rdev->gart.pages_addr[p];
@@ -191,23 +188,7 @@ int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
 	p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
 
 	for (i = 0; i < pages; i++, p++) {
-		/* we reverted the patch using dma_addr in TTM for now but this
-		 * code stops building on alpha so just comment it out for now */
-		if (0) { /*dma_addr[i] != DMA_ERROR_CODE) */
-			rdev->gart.ttm_alloced[p] = true;
-			rdev->gart.pages_addr[p] = dma_addr[i];
-		} else {
-			/* we need to support large memory configurations */
-			/* assume that unbind have already been call on the range */
-			rdev->gart.pages_addr[p] = pci_map_page(rdev->pdev, pagelist[i],
-							0, PAGE_SIZE,
-							PCI_DMA_BIDIRECTIONAL);
-			if (pci_dma_mapping_error(rdev->pdev, rdev->gart.pages_addr[p])) {
-				/* FIXME: failed to map page (return -ENOMEM?) */
-				radeon_gart_unbind(rdev, offset, pages);
-				return -ENOMEM;
-			}
-		}
+		rdev->gart.pages_addr[p] = dma_addr[i];
 		rdev->gart.pages[p] = pagelist[i];
 		if (rdev->gart.ptr) {
 			page_base = rdev->gart.pages_addr[p];
@@ -274,12 +255,6 @@ int radeon_gart_init(struct radeon_device *rdev)
 		radeon_gart_fini(rdev);
 		return -ENOMEM;
 	}
-	rdev->gart.ttm_alloced = kzalloc(sizeof(bool) *
-					 rdev->gart.num_cpu_pages, GFP_KERNEL);
-	if (rdev->gart.ttm_alloced == NULL) {
-		radeon_gart_fini(rdev);
-		return -ENOMEM;
-	}
 	/* set GART entry to point to the dummy page by default */
 	for (i = 0; i < rdev->gart.num_cpu_pages; i++) {
 		rdev->gart.pages_addr[i] = rdev->dummy_page.addr;
@@ -296,10 +271,8 @@ void radeon_gart_fini(struct radeon_device *rdev)
 	rdev->gart.ready = false;
 	kfree(rdev->gart.pages);
 	kfree(rdev->gart.pages_addr);
-	kfree(rdev->gart.ttm_alloced);
 	rdev->gart.pages = NULL;
 	rdev->gart.pages_addr = NULL;
-	rdev->gart.ttm_alloced = NULL;
 
 	radeon_dummy_page_fini(rdev);
 }
drivers/gpu/drm/radeon/radeon_ttm.c +77 -6

104 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#14 lore raw

PATCH 12/14 drm/nouveau: enable the ttm dma pool when swiotlb is active V3

In reply to [email protected]

From: Konrad Rzeszutek Wilk <[email protected]>

If the card is capable of more than 32-bit, then use the default TTM page pool code which allocates from anywhere in the memory.

Note: If the 'ttm.no_dma' parameter is set, the override is ignored and the default TTM pool is used.

V2 use pci_set_consistent_dma_mask
V3 Rebase on top of no memory account changes (where/when is my
delorean when i need it ?)

---
 drivers/gpu/drm/nouveau/nouveau_bo.c      |   73 ++++++++++++++++++++++++++++-
 drivers/gpu/drm/nouveau/nouveau_debugfs.c |    1 +
 drivers/gpu/drm/nouveau/nouveau_mem.c     |    6 ++
 drivers/gpu/drm/nouveau/nouveau_sgdma.c   |   60 +-----------------------
 4 files changed, 79 insertions(+), 61 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +71 -2
index 3271001..e603909 100644
@@ -1049,10 +1049,79 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 	nouveau_fence_unref(&old_fence);
 }
 
+static int
+nouveau_ttm_tt_populate(struct ttm_tt *ttm)
+{
+	struct drm_nouveau_private *dev_priv;
+	struct drm_device *dev;
+	unsigned i;
+	int r;
+
+	if (ttm->state != tt_unpopulated)
+		return 0;
+
+	dev_priv = nouveau_bdev(ttm->bdev);
+	dev = dev_priv->dev;
+
+#ifdef CONFIG_SWIOTLB
+	if (swiotlb_nr_tbl()) {
+		return ttm_dma_populate(ttm, dev->dev);
+	}
+#endif
+
+	r = ttm_pool_populate(ttm);
+	if (r) {
+		return r;
+	}
+
+	for (i = 0; i < ttm->num_pages; i++) {
+		ttm->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
+						   0, PAGE_SIZE,
+						   PCI_DMA_BIDIRECTIONAL);
+		if (pci_dma_mapping_error(dev->pdev, ttm->dma_address[i])) {
+			while (--i) {
+				pci_unmap_page(dev->pdev, ttm->dma_address[i],
+					       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
+				ttm->dma_address[i] = 0;
+			}
+			ttm_pool_unpopulate(ttm);
+			return -EFAULT;
+		}
+	}
+	return 0;
+}
+
+static void
+nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
+{
+	struct drm_nouveau_private *dev_priv;
+	struct drm_device *dev;
+	unsigned i;
+
+	dev_priv = nouveau_bdev(ttm->bdev);
+	dev = dev_priv->dev;
+
+#ifdef CONFIG_SWIOTLB
+	if (swiotlb_nr_tbl()) {
+		ttm_dma_unpopulate(ttm, dev->dev);
+		return;
+	}
+#endif
+
+	for (i = 0; i < ttm->num_pages; i++) {
+		if (ttm->dma_address[i]) {
+			pci_unmap_page(dev->pdev, ttm->dma_address[i],
+				       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
+		}
+	}
+
+	ttm_pool_unpopulate(ttm);
+}
+
 struct ttm_bo_driver nouveau_bo_driver = {
 	.ttm_tt_create = &nouveau_ttm_tt_create,
-	.ttm_tt_populate = &ttm_pool_populate,
-	.ttm_tt_unpopulate = &ttm_pool_unpopulate,
+	.ttm_tt_populate = &nouveau_ttm_tt_populate,
+	.ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
 	.invalidate_caches = nouveau_bo_invalidate_caches,
 	.init_mem_type = nouveau_bo_init_mem_type,
 	.evict_flags = nouveau_bo_evict_flags,
drivers/gpu/drm/nouveau/nouveau_debugfs.c +1 -0
index 8e15923..f52c2db 100644
@@ -178,6 +178,7 @@ static struct drm_info_list nouveau_debugfs_list[] = {
 	{ "memory", nouveau_debugfs_memory_info, 0, NULL },
 	{ "vbios.rom", nouveau_debugfs_vbios_image, 0, NULL },
 	{ "ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL },
+	{ "ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL },
 };
 #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)
 
drivers/gpu/drm/nouveau/nouveau_mem.c +6 -0
index 36bec48..37fcaa2 100644
@@ -407,6 +407,12 @@ nouveau_mem_vram_init(struct drm_device *dev)
 	ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
 	if (ret)
 		return ret;
+	ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
+	if (ret) {
+		/* Reset to default value. */
+		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
+	}
+
 
 	ret = nouveau_ttm_global_init(dev_priv);
 	if (ret)
drivers/gpu/drm/nouveau/nouveau_sgdma.c +1 -59

122 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#15 lore raw

PATCH 13/14 drm/ttm: isolate dma data from ttm_tt V4

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Move dma data to a superset ttm_dma_tt structure which herit from ttm_tt. This allow driver that don't use dma functionalities to not have to waste memory for it.

V2 Rebase on top of no memory account changes (where/when is my
delorean when i need it ?)
V3 Make sure page list is initialized empty
V4 typo/syntax fixes

---
 drivers/gpu/drm/nouveau/nouveau_bo.c     |   18 +++--
 drivers/gpu/drm/nouveau/nouveau_sgdma.c  |   22 ++++--
 drivers/gpu/drm/radeon/radeon_ttm.c      |   43 ++++++------
 drivers/gpu/drm/ttm/ttm_page_alloc.c     |  114 +++++++++++++++---------------
 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c |   35 +++++----
 drivers/gpu/drm/ttm/ttm_tt.c             |   60 +++++++++++++---
 drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c   |    2 +
 include/drm/ttm/ttm_bo_driver.h          |   32 ++++++++-
 include/drm/ttm/ttm_page_alloc.h         |   33 +--------
 9 files changed, 203 insertions(+), 156 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +10 -8
index e603909..4347776 100644
@@ -1052,6 +1052,7 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 static int
 nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 {
+	struct ttm_dma_tt *ttm_dma = (void *)ttm;
 	struct drm_nouveau_private *dev_priv;
 	struct drm_device *dev;
 	unsigned i;
@@ -1065,7 +1066,7 @@ nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 
 #ifdef CONFIG_SWIOTLB
 	if (swiotlb_nr_tbl()) {
-		return ttm_dma_populate(ttm, dev->dev);
+		return ttm_dma_populate((void *)ttm, dev->dev);
 	}
 #endif
 
@@ -1075,14 +1076,14 @@ nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 	}
 
 	for (i = 0; i < ttm->num_pages; i++) {
-		ttm->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
+		ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
 						   0, PAGE_SIZE,
 						   PCI_DMA_BIDIRECTIONAL);
-		if (pci_dma_mapping_error(dev->pdev, ttm->dma_address[i])) {
+		if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
 			while (--i) {
-				pci_unmap_page(dev->pdev, ttm->dma_address[i],
+				pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
 					       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
-				ttm->dma_address[i] = 0;
+				ttm_dma->dma_address[i] = 0;
 			}
 			ttm_pool_unpopulate(ttm);
 			return -EFAULT;
@@ -1094,6 +1095,7 @@ nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 static void
 nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
 {
+	struct ttm_dma_tt *ttm_dma = (void *)ttm;
 	struct drm_nouveau_private *dev_priv;
 	struct drm_device *dev;
 	unsigned i;
@@ -1103,14 +1105,14 @@ nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
 
 #ifdef CONFIG_SWIOTLB
 	if (swiotlb_nr_tbl()) {
-		ttm_dma_unpopulate(ttm, dev->dev);
+		ttm_dma_unpopulate((void *)ttm, dev->dev);
 		return;
 	}
 #endif
 
 	for (i = 0; i < ttm->num_pages; i++) {
-		if (ttm->dma_address[i]) {
-			pci_unmap_page(dev->pdev, ttm->dma_address[i],
+		if (ttm_dma->dma_address[i]) {
+			pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
 				       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
 		}
 	}
drivers/gpu/drm/nouveau/nouveau_sgdma.c +14 -8
index ee1eb7c..47f245e 100644
@@ -8,7 +8,10 @@
 #define NV_CTXDMA_PAGE_MASK  (NV_CTXDMA_PAGE_SIZE - 1)
 
 struct nouveau_sgdma_be {
-	struct ttm_tt ttm;
+	/* this has to be the first field so populate/unpopulated in
+	 * nouve_bo.c works properly, otherwise have to move them here
+	 */
+	struct ttm_dma_tt ttm;
 	struct drm_device *dev;
 	u64 offset;
 };
@@ -20,6 +23,7 @@ nouveau_sgdma_destroy(struct ttm_tt *ttm)
 
 	if (ttm) {
 		NV_DEBUG(nvbe->dev, "\n");
+		ttm_dma_tt_fini(&nvbe->ttm);
 		kfree(nvbe);
 	}
 }
@@ -38,7 +42,7 @@ nv04_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 	nvbe->offset = mem->start << PAGE_SHIFT;
 	pte = (nvbe->offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
 	for (i = 0; i < ttm->num_pages; i++) {
-		dma_addr_t dma_offset = ttm->dma_address[i];
+		dma_addr_t dma_offset = nvbe->ttm.dma_address[i];
 		uint32_t offset_l = lower_32_bits(dma_offset);
 
 		for (j = 0; j < PAGE_SIZE / NV_CTXDMA_PAGE_SIZE; j++, pte++) {
@@ -97,7 +101,7 @@ nv41_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
 	struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
 	struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
-	dma_addr_t *list = ttm->dma_address;
+	dma_addr_t *list = nvbe->ttm.dma_address;
 	u32 pte = mem->start << 2;
 	u32 cnt = ttm->num_pages;
 
@@ -206,7 +210,7 @@ nv44_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
 	struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
 	struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
-	dma_addr_t *list = ttm->dma_address;
+	dma_addr_t *list = nvbe->ttm.dma_address;
 	u32 pte = mem->start << 2, tmp[4];
 	u32 cnt = ttm->num_pages;
 	int i;
@@ -282,10 +286,11 @@ static struct ttm_backend_func nv44_sgdma_backend = {
 static int
 nv50_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 {
+	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
 	struct nouveau_mem *node = mem->mm_node;
 
 	/* noop: bound in move_notify() */
-	node->pages = ttm->dma_address;
+	node->pages = nvbe->ttm.dma_address;
 	return 0;
 }
 
@@ -316,12 +321,13 @@ nouveau_sgdma_create_ttm(struct ttm_bo_device *bdev,
 		return NULL;
 
 	nvbe->dev = dev;
-	nvbe->ttm.func = dev_priv->gart_info.func;
+	nvbe->ttm.ttm.func = dev_priv->gart_info.func;
 
-	if (ttm_tt_init(&nvbe->ttm, bdev, size, page_flags, dummy_read_page)) {
+	if (ttm_dma_tt_init(&nvbe->ttm, bdev, size, page_flags, dummy_read_page)) {
+		kfree(nvbe);
 		return NULL;
 	}
-	return &nvbe->ttm;
+	return &nvbe->ttm.ttm;
 }
 
 int
drivers/gpu/drm/radeon/radeon_ttm.c +22 -21

122 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_page_alloc.c +56 -58

148 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +19 -16
index 7a47793..6678abc 100644
@@ -789,7 +789,7 @@ out:
 
 /*
  * @return count of pages still required to fulfill the request.
-*/
+ */
 static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
 					 unsigned long *irq_flags)
 {
@@ -838,10 +838,11 @@ static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
  * allocates one page at a time.
  */
 static int ttm_dma_pool_get_pages(struct dma_pool *pool,
-				  struct ttm_tt *ttm,
+				  struct ttm_dma_tt *ttm_dma,
 				  unsigned index)
 {
 	struct dma_page *d_page;
+	struct ttm_tt *ttm = &ttm_dma->ttm;
 	unsigned long irq_flags;
 	int count, r = -ENOMEM;
 
@@ -850,8 +851,8 @@ static int ttm_dma_pool_get_pages(struct dma_pool *pool,
 	if (count) {
 		d_page = list_first_entry(&pool->free_list, struct dma_page, page_list);
 		ttm->pages[index] = d_page->p;
-		ttm->dma_address[index] = d_page->dma;
-		list_move_tail(&d_page->page_list, &ttm->alloc_list);
+		ttm_dma->dma_address[index] = d_page->dma;
+		list_move_tail(&d_page->page_list, &ttm_dma->pages_list);
 		r = 0;
 		pool->npages_in_use += 1;
 		pool->npages_free -= 1;
@@ -864,8 +865,9 @@ static int ttm_dma_pool_get_pages(struct dma_pool *pool,
  * On success pages list will hold count number of correctly
  * cached pages. On failure will hold the negative return value (-ENOMEM, etc).
  */
-int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev)
+int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev)
 {
+	struct ttm_tt *ttm = &ttm_dma->ttm;
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
 	struct dma_pool *pool;
 	enum pool_type type;
@@ -892,18 +894,18 @@ int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev)
 		}
 	}
 
-	INIT_LIST_HEAD(&ttm->alloc_list);
+	INIT_LIST_HEAD(&ttm_dma->pages_list);
 	for (i = 0; i < ttm->num_pages; ++i) {
-		ret = ttm_dma_pool_get_pages(pool, ttm, i);
+		ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
 		if (ret != 0) {
-			ttm_dma_unpopulate(ttm, dev);
+			ttm_dma_unpopulate(ttm_dma, dev);
 			return -ENOMEM;
 		}
 
 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
 						false, false);
 		if (unlikely(ret != 0)) {
-			ttm_dma_unpopulate(ttm, dev);
+			ttm_dma_unpopulate(ttm_dma, dev);
 			return -ENOMEM;
 		}
 	}
@@ -911,7 +913,7 @@ int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev)
 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
 		ret = ttm_tt_swapin(ttm);
 		if (unlikely(ret != 0)) {
-			ttm_dma_unpopulate(ttm, dev);
+			ttm_dma_unpopulate(ttm_dma, dev);
 			return ret;
 		}
 	}
@@ -937,8 +939,9 @@ static int ttm_dma_pool_get_num_unused_pages(void)
 }
 
 /* Put all pages in pages list to correct pool to wait for reuse */
-void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
+void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev)
 {
+	struct ttm_tt *ttm = &ttm_dma->ttm;
 	struct dma_pool *pool;
 	struct dma_page *d_page, *next;
 	enum pool_type type;
@@ -956,7 +959,7 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 		     ttm_to_type(ttm->page_flags, tt_cached)) == pool);
 
 	/* make sure pages array match list and count number of pages */
-	list_for_each_entry(d_page, &ttm->alloc_list, page_list) {
+	list_for_each_entry(d_page, &ttm_dma->pages_list, page_list) {
 		ttm->pages[count] = d_page->p;
 		count++;
 	}
@@ -967,7 +970,7 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 		pool->nfrees += count;
 	} else {
 		pool->npages_free += count;
-		list_splice(&ttm->alloc_list, &pool->free_list);
+		list_splice(&ttm_dma->pages_list, &pool->free_list);
 		if (pool->npages_free > _manager->options.max_size) {
 			count = pool->npages_free - _manager->options.max_size;
 		}
@@ -975,7 +978,7 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 	spin_unlock_irqrestore(&pool->lock, irq_flags);
 
 	if (is_cached) {
-		list_for_each_entry_safe(d_page, next, &ttm->alloc_list, page_list) {
+		list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list, page_list) {
 			ttm_mem_global_free_page(ttm->glob->mem_glob,
 						 d_page->p);
 			ttm_dma_page_put(pool, d_page);
@@ -987,10 +990,10 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 		}
 	}
 
-	INIT_LIST_HEAD(&ttm->alloc_list);
+	INIT_LIST_HEAD(&ttm_dma->pages_list);
 	for (i = 0; i < ttm->num_pages; i++) {
 		ttm->pages[i] = NULL;
-		ttm->dma_address[i] = 0;
+		ttm_dma->dma_address[i] = 0;
 	}
 
 	/* shrink pool if necessary */
drivers/gpu/drm/ttm/ttm_tt.c +49 -11

88 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c +2 -0
index 3986d74..1e2c0fb 100644
@@ -168,6 +168,7 @@ static void vmw_ttm_destroy(struct ttm_tt *ttm)
 {
 	struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
 
+	ttm_tt_fini(ttm);
 	kfree(vmw_be);
 }
 
@@ -191,6 +192,7 @@ struct ttm_tt *vmw_ttm_tt_create(struct ttm_bo_device *bdev,
 	vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
 
 	if (ttm_tt_init(&vmw_be->ttm, bdev, size, page_flags, dummy_read_page)) {
+		kfree(vmw_be);
 		return NULL;
 	}
 
include/drm/ttm/ttm_bo_driver.h +29 -3
index beef9ab..b2a0848 100644
@@ -103,8 +103,6 @@ enum ttm_caching_state {
  * @swap_storage: Pointer to shmem struct file for swap storage.
  * @caching_state: The current caching state of the pages.
  * @state: The current binding state of the pages.
- * @dma_address: The DMA (bus) addresses of the pages (if TTM_PAGE_FLAG_DMA32)
- * @alloc_list: used by some page allocation backend
  *
  * This is a structure holding the pages, caching- and aperture binding
  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
@@ -127,8 +125,23 @@ struct ttm_tt {
 		tt_unbound,
 		tt_unpopulated,
 	} state;
+};
+
+/**
+ * struct ttm_dma_tt
+ *
+ * @ttm: Base ttm_tt struct.
+ * @dma_address: The DMA (bus) addresses of the pages
+ * @pages_list: used by some page allocation backend
+ *
+ * This is a structure holding the pages, caching- and aperture binding
+ * status for a buffer object that isn't backed by fixed (VRAM / AGP)
+ * memory.
+ */
+struct ttm_dma_tt {
+	struct ttm_tt ttm;
 	dma_addr_t *dma_address;
-	struct list_head alloc_list;
+	struct list_head pages_list;
 };
 
 #define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
@@ -595,6 +608,19 @@ ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
 extern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
 			unsigned long size, uint32_t page_flags,
 			struct page *dummy_read_page);
+extern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
+			   unsigned long size, uint32_t page_flags,
+			   struct page *dummy_read_page);
+
+/**
+ * ttm_tt_fini
+ *
+ * @ttm: the ttm_tt structure.
+ *
+ * Free memory of ttm_tt structure
+ */
+extern void ttm_tt_fini(struct ttm_tt *ttm);
+extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
 
 /**
  * ttm_ttm_bind:
include/drm/ttm/ttm_page_alloc.h +2 -31
index 1e1337e..5fe2740 100644
@@ -30,35 +30,6 @@
 #include "ttm_memory.h"
 
 /**
- * Get count number of pages from pool to pages list.
- *
- * @pages: head of empty linked list where pages are filled.
- * @flags: ttm flags for page allocation.
- * @cstate: ttm caching state for the page.
- * @count: number of pages to allocate.
- * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
- */
-int ttm_get_pages(struct page **pages,
-		  int flags,
-		  enum ttm_caching_state cstate,
-		  unsigned npages,
-		  dma_addr_t *dma_address);
-/**
- * Put linked list of pages to pool.
- *
- * @pages: list of pages to free.
- * @page_count: number of pages in the list. Zero can be passed for unknown
- * count.
- * @flags: ttm flags for page allocation.
- * @cstate: ttm caching state.
- * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
- */
-void ttm_put_pages(struct page **pages,
-		   unsigned npages,
-		   int flags,
-		   enum ttm_caching_state cstate,
-		   dma_addr_t *dma_address);
-/**
  * Initialize pool allocator.
  */
 int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages);
@@ -107,8 +78,8 @@ void ttm_dma_page_alloc_fini(void);
  */
 extern int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data);
 
-int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev);
-extern void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev);
+extern int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev);
+extern void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev);
 
 #else
 static inline int ttm_dma_page_alloc_init(struct ttm_mem_global *glob,
Signature · 1 line
1.7.7.1
Reviewed-by on v1 13/14
#16 lore raw

Re: [PATCH 13/14] drm/ttm: isolate dma data from ttm_tt V4

Quoted 6 lines · On Wed, Nov 16, 2011 at 11:57:37AM -0500, [email protected] wrote:

From: Jerome Glisse <[email protected]>

Move dma data to a superset ttm_dma_tt structure which herit from ttm_tt. This allow driver that don't use dma functionalities to not have to waste memory for it.

Quoted 7 lines

V2 Rebase on top of no memory account changes (where/when is my
delorean when i need it ?)
V3 Make sure page list is initialized empty
V4 typo/syntax fixes

Quoted 807 lines
---
 drivers/gpu/drm/nouveau/nouveau_bo.c     |   18 +++--
 drivers/gpu/drm/nouveau/nouveau_sgdma.c  |   22 ++++--
 drivers/gpu/drm/radeon/radeon_ttm.c      |   43 ++++++------
 drivers/gpu/drm/ttm/ttm_page_alloc.c     |  114 +++++++++++++++---------------
 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c |   35 +++++----
 drivers/gpu/drm/ttm/ttm_tt.c             |   60 +++++++++++++---
 drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c   |    2 +
 include/drm/ttm/ttm_bo_driver.h          |   32 ++++++++-
 include/drm/ttm/ttm_page_alloc.h         |   33 +--------
 9 files changed, 203 insertions(+), 156 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +10 -8
index e603909..4347776 100644
@@ -1052,6 +1052,7 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 static int
 nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 {
+	struct ttm_dma_tt *ttm_dma = (void *)ttm;
 	struct drm_nouveau_private *dev_priv;
 	struct drm_device *dev;
 	unsigned i;
@@ -1065,7 +1066,7 @@ nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 
 #ifdef CONFIG_SWIOTLB
 	if (swiotlb_nr_tbl()) {
-		return ttm_dma_populate(ttm, dev->dev);
+		return ttm_dma_populate((void *)ttm, dev->dev);
 	}
 #endif
 
@@ -1075,14 +1076,14 @@ nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 	}
 
 	for (i = 0; i < ttm->num_pages; i++) {
-		ttm->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
+		ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
 						   0, PAGE_SIZE,
 						   PCI_DMA_BIDIRECTIONAL);
-		if (pci_dma_mapping_error(dev->pdev, ttm->dma_address[i])) {
+		if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
 			while (--i) {
-				pci_unmap_page(dev->pdev, ttm->dma_address[i],
+				pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
 					       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
-				ttm->dma_address[i] = 0;
+				ttm_dma->dma_address[i] = 0;
 			}
 			ttm_pool_unpopulate(ttm);
 			return -EFAULT;
@@ -1094,6 +1095,7 @@ nouveau_ttm_tt_populate(struct ttm_tt *ttm)
 static void
 nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
 {
+	struct ttm_dma_tt *ttm_dma = (void *)ttm;
 	struct drm_nouveau_private *dev_priv;
 	struct drm_device *dev;
 	unsigned i;
@@ -1103,14 +1105,14 @@ nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
 
 #ifdef CONFIG_SWIOTLB
 	if (swiotlb_nr_tbl()) {
-		ttm_dma_unpopulate(ttm, dev->dev);
+		ttm_dma_unpopulate((void *)ttm, dev->dev);
 		return;
 	}
 #endif
 
 	for (i = 0; i < ttm->num_pages; i++) {
-		if (ttm->dma_address[i]) {
-			pci_unmap_page(dev->pdev, ttm->dma_address[i],
+		if (ttm_dma->dma_address[i]) {
+			pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
 				       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
 		}
 	}
drivers/gpu/drm/nouveau/nouveau_sgdma.c +14 -8
index ee1eb7c..47f245e 100644
@@ -8,7 +8,10 @@
 #define NV_CTXDMA_PAGE_MASK  (NV_CTXDMA_PAGE_SIZE - 1)
 
 struct nouveau_sgdma_be {
-	struct ttm_tt ttm;
+	/* this has to be the first field so populate/unpopulated in
+	 * nouve_bo.c works properly, otherwise have to move them here
+	 */
+	struct ttm_dma_tt ttm;
 	struct drm_device *dev;
 	u64 offset;
 };
@@ -20,6 +23,7 @@ nouveau_sgdma_destroy(struct ttm_tt *ttm)
 
 	if (ttm) {
 		NV_DEBUG(nvbe->dev, "\n");
+		ttm_dma_tt_fini(&nvbe->ttm);
 		kfree(nvbe);
 	}
 }
@@ -38,7 +42,7 @@ nv04_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 	nvbe->offset = mem->start << PAGE_SHIFT;
 	pte = (nvbe->offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
 	for (i = 0; i < ttm->num_pages; i++) {
-		dma_addr_t dma_offset = ttm->dma_address[i];
+		dma_addr_t dma_offset = nvbe->ttm.dma_address[i];
 		uint32_t offset_l = lower_32_bits(dma_offset);
 
 		for (j = 0; j < PAGE_SIZE / NV_CTXDMA_PAGE_SIZE; j++, pte++) {
@@ -97,7 +101,7 @@ nv41_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
 	struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
 	struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
-	dma_addr_t *list = ttm->dma_address;
+	dma_addr_t *list = nvbe->ttm.dma_address;
 	u32 pte = mem->start << 2;
 	u32 cnt = ttm->num_pages;
 
@@ -206,7 +210,7 @@ nv44_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
 	struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
 	struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
-	dma_addr_t *list = ttm->dma_address;
+	dma_addr_t *list = nvbe->ttm.dma_address;
 	u32 pte = mem->start << 2, tmp[4];
 	u32 cnt = ttm->num_pages;
 	int i;
@@ -282,10 +286,11 @@ static struct ttm_backend_func nv44_sgdma_backend = {
 static int
 nv50_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
 {
+	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
 	struct nouveau_mem *node = mem->mm_node;
 
 	/* noop: bound in move_notify() */
-	node->pages = ttm->dma_address;
+	node->pages = nvbe->ttm.dma_address;
 	return 0;
 }
 
@@ -316,12 +321,13 @@ nouveau_sgdma_create_ttm(struct ttm_bo_device *bdev,
 		return NULL;
 
 	nvbe->dev = dev;
-	nvbe->ttm.func = dev_priv->gart_info.func;
+	nvbe->ttm.ttm.func = dev_priv->gart_info.func;
 
-	if (ttm_tt_init(&nvbe->ttm, bdev, size, page_flags, dummy_read_page)) {
+	if (ttm_dma_tt_init(&nvbe->ttm, bdev, size, page_flags, dummy_read_page)) {
+		kfree(nvbe);
 		return NULL;
 	}
-	return &nvbe->ttm;
+	return &nvbe->ttm.ttm;
 }
 
 int
drivers/gpu/drm/radeon/radeon_ttm.c +22 -21
index f499b2c..e111a38 100644
@@ -501,7 +501,7 @@ static bool radeon_sync_obj_signaled(void *sync_obj, void *sync_arg)
  * TTM backend functions.
  */
 struct radeon_ttm_tt {
-	struct ttm_tt			ttm;
+	struct ttm_dma_tt		ttm;
 	struct radeon_device		*rdev;
 	u64				offset;
 };
@@ -509,17 +509,16 @@ struct radeon_ttm_tt {
 static int radeon_ttm_backend_bind(struct ttm_tt *ttm,
 				   struct ttm_mem_reg *bo_mem)
 {
-	struct radeon_ttm_tt *gtt;
+	struct radeon_ttm_tt *gtt = (void*)ttm;
 	int r;
 
-	gtt = container_of(ttm, struct radeon_ttm_tt, ttm);
 	gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
 	if (!ttm->num_pages) {
 		WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
 		     ttm->num_pages, bo_mem, ttm);
 	}
 	r = radeon_gart_bind(gtt->rdev, gtt->offset,
-			     ttm->num_pages, ttm->pages, ttm->dma_address);
+			     ttm->num_pages, ttm->pages, gtt->ttm.dma_address);
 	if (r) {
 		DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
 			  ttm->num_pages, (unsigned)gtt->offset);
@@ -530,18 +529,17 @@ static int radeon_ttm_backend_bind(struct ttm_tt *ttm,
 
 static int radeon_ttm_backend_unbind(struct ttm_tt *ttm)
 {
-	struct radeon_ttm_tt *gtt;
+	struct radeon_ttm_tt *gtt = (void *)ttm;
 
-	gtt = container_of(ttm, struct radeon_ttm_tt, ttm);
 	radeon_gart_unbind(gtt->rdev, gtt->offset, ttm->num_pages);
 	return 0;
 }
 
 static void radeon_ttm_backend_destroy(struct ttm_tt *ttm)
 {
-	struct radeon_ttm_tt *gtt;
+	struct radeon_ttm_tt *gtt = (void *)ttm;
 
-	gtt = container_of(ttm, struct radeon_ttm_tt, ttm);
+	ttm_dma_tt_fini(&gtt->ttm);
 	kfree(gtt);
 }
 
@@ -570,17 +568,19 @@ struct ttm_tt *radeon_ttm_tt_create(struct ttm_bo_device *bdev,
 	if (gtt == NULL) {
 		return NULL;
 	}
-	gtt->ttm.func = &radeon_backend_func;
+	gtt->ttm.ttm.func = &radeon_backend_func;
 	gtt->rdev = rdev;
-	if (ttm_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
+	if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
+		kfree(gtt);
 		return NULL;
 	}
-	return &gtt->ttm;
+	return &gtt->ttm.ttm;
 }
 
 static int radeon_ttm_tt_populate(struct ttm_tt *ttm)
 {
 	struct radeon_device *rdev;
+	struct radeon_ttm_tt *gtt = (void *)ttm;
 	unsigned i;
 	int r;
 
@@ -591,7 +591,7 @@ static int radeon_ttm_tt_populate(struct ttm_tt *ttm)
 
 #ifdef CONFIG_SWIOTLB
 	if (swiotlb_nr_tbl()) {
-		return ttm_dma_populate(ttm, rdev->dev);
+		return ttm_dma_populate(&gtt->ttm, rdev->dev);
 	}
 #endif
 
@@ -601,14 +601,14 @@ static int radeon_ttm_tt_populate(struct ttm_tt *ttm)
 	}
 
 	for (i = 0; i < ttm->num_pages; i++) {
-		ttm->dma_address[i] = pci_map_page(rdev->pdev, ttm->pages[i],
-						   0, PAGE_SIZE,
-						   PCI_DMA_BIDIRECTIONAL);
-		if (pci_dma_mapping_error(rdev->pdev, ttm->dma_address[i])) {
+		gtt->ttm.dma_address[i] = pci_map_page(rdev->pdev, ttm->pages[i],
+						       0, PAGE_SIZE,
+						       PCI_DMA_BIDIRECTIONAL);
+		if (pci_dma_mapping_error(rdev->pdev, gtt->ttm.dma_address[i])) {
 			while (--i) {
-				pci_unmap_page(rdev->pdev, ttm->dma_address[i],
+				pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i],
 					       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
-				ttm->dma_address[i] = 0;
+				gtt->ttm.dma_address[i] = 0;
 			}
 			ttm_pool_unpopulate(ttm);
 			return -EFAULT;
@@ -620,20 +620,21 @@ static int radeon_ttm_tt_populate(struct ttm_tt *ttm)
 static void radeon_ttm_tt_unpopulate(struct ttm_tt *ttm)
 {
 	struct radeon_device *rdev;
+	struct radeon_ttm_tt *gtt = (void *)ttm;
 	unsigned i;
 
 	rdev = radeon_get_rdev(ttm->bdev);
 
 #ifdef CONFIG_SWIOTLB
 	if (swiotlb_nr_tbl()) {
-		ttm_dma_unpopulate(ttm, rdev->dev);
+		ttm_dma_unpopulate(&gtt->ttm, rdev->dev);
 		return;
 	}
 #endif
 
 	for (i = 0; i < ttm->num_pages; i++) {
-		if (ttm->dma_address[i]) {
-			pci_unmap_page(rdev->pdev, ttm->dma_address[i],
+		if (gtt->ttm.dma_address[i]) {
+			pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i],
 				       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
 		}
 	}
drivers/gpu/drm/ttm/ttm_page_alloc.c +56 -58
index 8d6267e..499debd 100644
@@ -662,13 +662,61 @@ out:
 	return count;
 }
 
+/* Put all pages in pages list to correct pool to wait for reuse */
+static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
+			  enum ttm_caching_state cstate)
+{
+	unsigned long irq_flags;
+	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
+	unsigned i;
+
+	if (pool == NULL) {
+		/* No pool for this memory type so free the pages */
+		for (i = 0; i < npages; i++) {
+			if (pages[i]) {
+				if (page_count(pages[i]) != 1)
+					printk(KERN_ERR TTM_PFX
+					       "Erroneous page count. "
+					       "Leaking pages.\n");
+				__free_page(pages[i]);
+				pages[i] = NULL;
+			}
+		}
+		return;
+	}
+
+	spin_lock_irqsave(&pool->lock, irq_flags);
+	for (i = 0; i < npages; i++) {
+		if (pages[i]) {
+			if (page_count(pages[i]) != 1)
+				printk(KERN_ERR TTM_PFX
+				       "Erroneous page count. "
+				       "Leaking pages.\n");
+			list_add_tail(&pages[i]->lru, &pool->list);
+			pages[i] = NULL;
+			pool->npages++;
+		}
+	}
+	/* Check that we don't go over the pool limit */
+	npages = 0;
+	if (pool->npages > _manager->options.max_size) {
+		npages = pool->npages - _manager->options.max_size;
+		/* free at least NUM_PAGES_TO_ALLOC number of pages
+		 * to reduce calls to set_memory_wb */
+		if (npages < NUM_PAGES_TO_ALLOC)
+			npages = NUM_PAGES_TO_ALLOC;
+	}
+	spin_unlock_irqrestore(&pool->lock, irq_flags);
+	if (npages)
+		ttm_page_pool_free(pool, npages);
+}
+
 /*
  * On success pages list will hold count number of correctly
  * cached pages.
  */
-int ttm_get_pages(struct page **pages, int flags,
-		  enum ttm_caching_state cstate, unsigned npages,
-		  dma_addr_t *dma_address)
+static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
+			 enum ttm_caching_state cstate)
 {
 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
 	struct list_head plist;
@@ -736,7 +784,7 @@ int ttm_get_pages(struct page **pages, int flags,
 			printk(KERN_ERR TTM_PFX
 			       "Failed to allocate extra pages "
 			       "for large request.");
-			ttm_put_pages(pages, count, flags, cstate, NULL);
+			ttm_put_pages(pages, count, flags, cstate);
 			return r;
 		}
 	}
@@ -744,55 +792,6 @@ int ttm_get_pages(struct page **pages, int flags,
 	return 0;
 }
 
-/* Put all pages in pages list to correct pool to wait for reuse */
-void ttm_put_pages(struct page **pages, unsigned npages, int flags,
-		   enum ttm_caching_state cstate, dma_addr_t *dma_address)
-{
-	unsigned long irq_flags;
-	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
-	unsigned i;
-
-	if (pool == NULL) {
-		/* No pool for this memory type so free the pages */
-		for (i = 0; i < npages; i++) {
-			if (pages[i]) {
-				if (page_count(pages[i]) != 1)
-					printk(KERN_ERR TTM_PFX
-					       "Erroneous page count. "
-					       "Leaking pages.\n");
-				__free_page(pages[i]);
-				pages[i] = NULL;
-			}
-		}
-		return;
-	}
-
-	spin_lock_irqsave(&pool->lock, irq_flags);
-	for (i = 0; i < npages; i++) {
-		if (pages[i]) {
-			if (page_count(pages[i]) != 1)
-				printk(KERN_ERR TTM_PFX
-				       "Erroneous page count. "
-				       "Leaking pages.\n");
-			list_add_tail(&pages[i]->lru, &pool->list);
-			pages[i] = NULL;
-			pool->npages++;
-		}
-	}
-	/* Check that we don't go over the pool limit */
-	npages = 0;
-	if (pool->npages > _manager->options.max_size) {
-		npages = pool->npages - _manager->options.max_size;
-		/* free at least NUM_PAGES_TO_ALLOC number of pages
-		 * to reduce calls to set_memory_wb */
-		if (npages < NUM_PAGES_TO_ALLOC)
-			npages = NUM_PAGES_TO_ALLOC;
-	}
-	spin_unlock_irqrestore(&pool->lock, irq_flags);
-	if (npages)
-		ttm_page_pool_free(pool, npages);
-}
-
 static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
 		char *name)
 {
@@ -865,9 +864,9 @@ int ttm_pool_populate(struct ttm_tt *ttm)
 		return 0;
 
 	for (i = 0; i < ttm->num_pages; ++i) {
-		ret = ttm_get_pages(&ttm->pages[i], ttm->page_flags,
-				    ttm->caching_state, 1,
-				    &ttm->dma_address[i]);
+		ret = ttm_get_pages(&ttm->pages[i], 1,
+				    ttm->page_flags,
+				    ttm->caching_state);
 		if (ret != 0) {
 			ttm_pool_unpopulate(ttm);
 			return -ENOMEM;
@@ -904,8 +903,7 @@ void ttm_pool_unpopulate(struct ttm_tt *ttm)
 						 ttm->pages[i]);
 			ttm_put_pages(&ttm->pages[i], 1,
 				      ttm->page_flags,
-				      ttm->caching_state,
-				      ttm->dma_address);
+				      ttm->caching_state);
 		}
 	}
 	ttm->state = tt_unpopulated;
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +19 -16
index 7a47793..6678abc 100644
@@ -789,7 +789,7 @@ out:
 
 /*
  * @return count of pages still required to fulfill the request.
-*/
+ */
 static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
 					 unsigned long *irq_flags)
 {
@@ -838,10 +838,11 @@ static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
  * allocates one page at a time.
  */
 static int ttm_dma_pool_get_pages(struct dma_pool *pool,
-				  struct ttm_tt *ttm,
+				  struct ttm_dma_tt *ttm_dma,
 				  unsigned index)
 {
 	struct dma_page *d_page;
+	struct ttm_tt *ttm = &ttm_dma->ttm;
 	unsigned long irq_flags;
 	int count, r = -ENOMEM;
 
@@ -850,8 +851,8 @@ static int ttm_dma_pool_get_pages(struct dma_pool *pool,
 	if (count) {
 		d_page = list_first_entry(&pool->free_list, struct dma_page, page_list);
 		ttm->pages[index] = d_page->p;
-		ttm->dma_address[index] = d_page->dma;
-		list_move_tail(&d_page->page_list, &ttm->alloc_list);
+		ttm_dma->dma_address[index] = d_page->dma;
+		list_move_tail(&d_page->page_list, &ttm_dma->pages_list);
 		r = 0;
 		pool->npages_in_use += 1;
 		pool->npages_free -= 1;
@@ -864,8 +865,9 @@ static int ttm_dma_pool_get_pages(struct dma_pool *pool,
  * On success pages list will hold count number of correctly
  * cached pages. On failure will hold the negative return value (-ENOMEM, etc).
  */
-int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev)
+int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev)
 {
+	struct ttm_tt *ttm = &ttm_dma->ttm;
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
 	struct dma_pool *pool;
 	enum pool_type type;
@@ -892,18 +894,18 @@ int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev)
 		}
 	}
 
-	INIT_LIST_HEAD(&ttm->alloc_list);
+	INIT_LIST_HEAD(&ttm_dma->pages_list);
 	for (i = 0; i < ttm->num_pages; ++i) {
-		ret = ttm_dma_pool_get_pages(pool, ttm, i);
+		ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
 		if (ret != 0) {
-			ttm_dma_unpopulate(ttm, dev);
+			ttm_dma_unpopulate(ttm_dma, dev);
 			return -ENOMEM;
 		}
 
 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
 						false, false);
 		if (unlikely(ret != 0)) {
-			ttm_dma_unpopulate(ttm, dev);
+			ttm_dma_unpopulate(ttm_dma, dev);
 			return -ENOMEM;
 		}
 	}
@@ -911,7 +913,7 @@ int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev)
 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
 		ret = ttm_tt_swapin(ttm);
 		if (unlikely(ret != 0)) {
-			ttm_dma_unpopulate(ttm, dev);
+			ttm_dma_unpopulate(ttm_dma, dev);
 			return ret;
 		}
 	}
@@ -937,8 +939,9 @@ static int ttm_dma_pool_get_num_unused_pages(void)
 }
 
 /* Put all pages in pages list to correct pool to wait for reuse */
-void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
+void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev)
 {
+	struct ttm_tt *ttm = &ttm_dma->ttm;
 	struct dma_pool *pool;
 	struct dma_page *d_page, *next;
 	enum pool_type type;
@@ -956,7 +959,7 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 		     ttm_to_type(ttm->page_flags, tt_cached)) == pool);
 
 	/* make sure pages array match list and count number of pages */
-	list_for_each_entry(d_page, &ttm->alloc_list, page_list) {
+	list_for_each_entry(d_page, &ttm_dma->pages_list, page_list) {
 		ttm->pages[count] = d_page->p;
 		count++;
 	}
@@ -967,7 +970,7 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 		pool->nfrees += count;
 	} else {
 		pool->npages_free += count;
-		list_splice(&ttm->alloc_list, &pool->free_list);
+		list_splice(&ttm_dma->pages_list, &pool->free_list);
 		if (pool->npages_free > _manager->options.max_size) {
 			count = pool->npages_free - _manager->options.max_size;
 		}
@@ -975,7 +978,7 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 	spin_unlock_irqrestore(&pool->lock, irq_flags);
 
 	if (is_cached) {
-		list_for_each_entry_safe(d_page, next, &ttm->alloc_list, page_list) {
+		list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list, page_list) {
 			ttm_mem_global_free_page(ttm->glob->mem_glob,
 						 d_page->p);
 			ttm_dma_page_put(pool, d_page);
@@ -987,10 +990,10 @@ void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev)
 		}
 	}
 
-	INIT_LIST_HEAD(&ttm->alloc_list);
+	INIT_LIST_HEAD(&ttm_dma->pages_list);
 	for (i = 0; i < ttm->num_pages; i++) {
 		ttm->pages[i] = NULL;
-		ttm->dma_address[i] = 0;
+		ttm_dma->dma_address[i] = 0;
 	}
 
 	/* shrink pool if necessary */
drivers/gpu/drm/ttm/ttm_tt.c +49 -11
index 1625739..58e1fa1 100644
@@ -48,17 +48,14 @@
  */
 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
 {
-	ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(*ttm->pages));
-	ttm->dma_address = drm_calloc_large(ttm->num_pages,
-					    sizeof(*ttm->dma_address));
+	ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
 }
 
-static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
+static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
 {
-	drm_free_large(ttm->pages);
-	ttm->pages = NULL;
-	drm_free_large(ttm->dma_address);
-	ttm->dma_address = NULL;
+	ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
+	ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
+					    sizeof(*ttm->dma_address));
 }
 
 #ifdef CONFIG_X86
@@ -173,7 +170,6 @@ void ttm_tt_destroy(struct ttm_tt *ttm)
 
 	if (likely(ttm->pages != NULL)) {
 		ttm->bdev->driver->ttm_tt_unpopulate(ttm);
-		ttm_tt_free_page_directory(ttm);
 	}
 
 	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
@@ -196,9 +192,8 @@ int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
 	ttm->dummy_read_page = dummy_read_page;
 	ttm->state = tt_unpopulated;
 
-	INIT_LIST_HEAD(&ttm->alloc_list);
 	ttm_tt_alloc_page_directory(ttm);
-	if (!ttm->pages || !ttm->dma_address) {
+	if (!ttm->pages) {
 		ttm_tt_destroy(ttm);
 		printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
 		return -ENOMEM;
@@ -207,6 +202,49 @@ int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
 }
 EXPORT_SYMBOL(ttm_tt_init);
 
+void ttm_tt_fini(struct ttm_tt *ttm)
+{
+	drm_free_large(ttm->pages);
+	ttm->pages = NULL;
+}
+EXPORT_SYMBOL(ttm_tt_fini);
+
+int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
+		unsigned long size, uint32_t page_flags,
+		struct page *dummy_read_page)
+{
+	struct ttm_tt *ttm = &ttm_dma->ttm;
+
+	ttm->bdev = bdev;
+	ttm->glob = bdev->glob;
+	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	ttm->caching_state = tt_cached;
+	ttm->page_flags = page_flags;
+	ttm->dummy_read_page = dummy_read_page;
+	ttm->state = tt_unpopulated;
+
+	INIT_LIST_HEAD(&ttm_dma->pages_list);
+	ttm_dma_tt_alloc_page_directory(ttm_dma);
+	if (!ttm->pages || !ttm_dma->dma_address) {
+		ttm_tt_destroy(ttm);
+		printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
+		return -ENOMEM;
+	}
+	return 0;
+}
+EXPORT_SYMBOL(ttm_dma_tt_init);
+
+void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
+{
+	struct ttm_tt *ttm = &ttm_dma->ttm;
+
+	drm_free_large(ttm->pages);
+	ttm->pages = NULL;
+	drm_free_large(ttm_dma->dma_address);
+	ttm_dma->dma_address = NULL;
+}
+EXPORT_SYMBOL(ttm_dma_tt_fini);
+
 void ttm_tt_unbind(struct ttm_tt *ttm)
 {
 	int ret;
drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c +2 -0
index 3986d74..1e2c0fb 100644
@@ -168,6 +168,7 @@ static void vmw_ttm_destroy(struct ttm_tt *ttm)
 {
 	struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
 
+	ttm_tt_fini(ttm);
 	kfree(vmw_be);
 }
 
@@ -191,6 +192,7 @@ struct ttm_tt *vmw_ttm_tt_create(struct ttm_bo_device *bdev,
 	vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
 
 	if (ttm_tt_init(&vmw_be->ttm, bdev, size, page_flags, dummy_read_page)) {
+		kfree(vmw_be);
 		return NULL;
 	}
 
include/drm/ttm/ttm_bo_driver.h +29 -3
index beef9ab..b2a0848 100644
@@ -103,8 +103,6 @@ enum ttm_caching_state {
  * @swap_storage: Pointer to shmem struct file for swap storage.
  * @caching_state: The current caching state of the pages.
  * @state: The current binding state of the pages.
- * @dma_address: The DMA (bus) addresses of the pages (if TTM_PAGE_FLAG_DMA32)
- * @alloc_list: used by some page allocation backend
  *
  * This is a structure holding the pages, caching- and aperture binding
  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
@@ -127,8 +125,23 @@ struct ttm_tt {
 		tt_unbound,
 		tt_unpopulated,
 	} state;
+};
+
+/**
+ * struct ttm_dma_tt
+ *
+ * @ttm: Base ttm_tt struct.
+ * @dma_address: The DMA (bus) addresses of the pages
+ * @pages_list: used by some page allocation backend
+ *
+ * This is a structure holding the pages, caching- and aperture binding
+ * status for a buffer object that isn't backed by fixed (VRAM / AGP)
+ * memory.
+ */
+struct ttm_dma_tt {
+	struct ttm_tt ttm;
 	dma_addr_t *dma_address;
-	struct list_head alloc_list;
+	struct list_head pages_list;
 };
 
 #define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
@@ -595,6 +608,19 @@ ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
 extern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
 			unsigned long size, uint32_t page_flags,
 			struct page *dummy_read_page);
+extern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
+			   unsigned long size, uint32_t page_flags,
+			   struct page *dummy_read_page);
+
+/**
+ * ttm_tt_fini
+ *
+ * @ttm: the ttm_tt structure.
+ *
+ * Free memory of ttm_tt structure
+ */
+extern void ttm_tt_fini(struct ttm_tt *ttm);
+extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
 
 /**
  * ttm_ttm_bind:
include/drm/ttm/ttm_page_alloc.h +2 -31
index 1e1337e..5fe2740 100644
@@ -30,35 +30,6 @@
 #include "ttm_memory.h"
 
 /**
- * Get count number of pages from pool to pages list.
- *
- * @pages: head of empty linked list where pages are filled.
- * @flags: ttm flags for page allocation.
- * @cstate: ttm caching state for the page.
- * @count: number of pages to allocate.
- * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
- */
-int ttm_get_pages(struct page **pages,
-		  int flags,
-		  enum ttm_caching_state cstate,
-		  unsigned npages,
-		  dma_addr_t *dma_address);
-/**
- * Put linked list of pages to pool.
- *
- * @pages: list of pages to free.
- * @page_count: number of pages in the list. Zero can be passed for unknown
- * count.
- * @flags: ttm flags for page allocation.
- * @cstate: ttm caching state.
- * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
- */
-void ttm_put_pages(struct page **pages,
-		   unsigned npages,
-		   int flags,
-		   enum ttm_caching_state cstate,
-		   dma_addr_t *dma_address);
-/**
  * Initialize pool allocator.
  */
 int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages);
@@ -107,8 +78,8 @@ void ttm_dma_page_alloc_fini(void);
  */
 extern int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data);
 
-int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev);
-extern void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev);
+extern int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev);
+extern void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev);
 
 #else
 static inline int ttm_dma_page_alloc_init(struct ttm_mem_global *glob,
Signature · 1 line
1.7.7.1
#17 lore raw

PATCH 14/14 drm/ttm: simplify memory accounting for ttm user

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Provide helper function to compute the kernel memory size needed for each buffer object. Move all the accounting inside ttm, simplifying driver and avoiding code duplication accross them.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c     |    6 +++-
 drivers/gpu/drm/radeon/radeon_object.c   |    8 +++-
 drivers/gpu/drm/ttm/ttm_bo.c             |   52 +++++++++++++++++++++++-------
 drivers/gpu/drm/vmwgfx/vmwgfx_resource.c |   35 +-------------------
 include/drm/ttm/ttm_bo_api.h             |   19 ++++++++++-
 include/drm/ttm/ttm_bo_driver.h          |    5 ---
 6 files changed, 70 insertions(+), 55 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +5 -1
index 4347776..857bca4 100644
@@ -93,6 +93,7 @@ nouveau_bo_new(struct drm_device *dev, int size, int align,
 {
 	struct drm_nouveau_private *dev_priv = dev->dev_private;
 	struct nouveau_bo *nvbo;
+	size_t acc_size;
 	int ret;
 
 	nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
@@ -115,9 +116,12 @@ nouveau_bo_new(struct drm_device *dev, int size, int align,
 	nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
 	nouveau_bo_placement_set(nvbo, flags, 0);
 
+	acc_size = ttm_bo_dma_acc_size(&dev_priv->ttm.bdev, size,
+				       sizeof(struct nouveau_bo));
+
 	ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
 			  ttm_bo_type_device, &nvbo->placement,
-			  align >> PAGE_SHIFT, 0, false, NULL, size,
+			  align >> PAGE_SHIFT, 0, false, NULL, acc_size,
 			  nouveau_bo_del_ttm);
 	if (ret) {
 		/* ttm will call nouveau_bo_del_ttm if it fails.. */
drivers/gpu/drm/radeon/radeon_object.c +6 -2
index 1c85152..695b480 100644
@@ -95,6 +95,7 @@ int radeon_bo_create(struct radeon_device *rdev,
 	enum ttm_bo_type type;
 	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
 	unsigned long max_size = 0;
+	size_t acc_size;
 	int r;
 
 	size = ALIGN(size, PAGE_SIZE);
@@ -117,6 +118,9 @@ int radeon_bo_create(struct radeon_device *rdev,
 		return -ENOMEM;
 	}
 
+	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
+				       sizeof(struct radeon_bo));
+
 retry:
 	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
 	if (bo == NULL)
@@ -134,8 +138,8 @@ retry:
 	/* Kernel allocation are uninterruptible */
 	mutex_lock(&rdev->vram_mutex);
 	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
-			&bo->placement, page_align, 0, !kernel, NULL, size,
-			&radeon_ttm_bo_destroy);
+			&bo->placement, page_align, 0, !kernel, NULL,
+			acc_size, &radeon_ttm_bo_destroy);
 	mutex_unlock(&rdev->vram_mutex);
 	if (unlikely(r != 0)) {
 		if (r != -ERESTARTSYS) {
drivers/gpu/drm/ttm/ttm_bo.c +40 -12

95 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +1 -34
index 86c5e4c..2eb84a5 100644
@@ -1517,29 +1517,10 @@ out_bad_surface:
 /**
  * Buffer management.
  */
-
-static size_t vmw_dmabuf_acc_size(struct ttm_bo_global *glob,
-				  unsigned long num_pages)
-{
-	static size_t bo_user_size = ~0;
-
-	size_t page_array_size =
-	    (num_pages * sizeof(void *) + PAGE_SIZE - 1) & PAGE_MASK;
-
-	if (unlikely(bo_user_size == ~0)) {
-		bo_user_size = glob->ttm_bo_extra_size +
-		    ttm_round_pot(sizeof(struct vmw_dma_buffer));
-	}
-
-	return bo_user_size + page_array_size;
-}
-
 void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
 {
 	struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
-	struct ttm_bo_global *glob = bo->glob;
 
-	ttm_mem_global_free(glob->mem_glob, bo->acc_size);
 	kfree(vmw_bo);
 }
 
@@ -1550,24 +1531,12 @@ int vmw_dmabuf_init(struct vmw_private *dev_priv,
 		    void (*bo_free) (struct ttm_buffer_object *bo))
 {
 	struct ttm_bo_device *bdev = &dev_priv->bdev;
-	struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
 	size_t acc_size;
 	int ret;
 
 	BUG_ON(!bo_free);
 
-	acc_size =
-	    vmw_dmabuf_acc_size(bdev->glob,
-				(size + PAGE_SIZE - 1) >> PAGE_SHIFT);
-
-	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
-	if (unlikely(ret != 0)) {
-		/* we must free the bo here as
-		 * ttm_buffer_object_init does so as well */
-		bo_free(&vmw_bo->base);
-		return ret;
-	}
-
+	acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
 	memset(vmw_bo, 0, sizeof(*vmw_bo));
 
 	INIT_LIST_HEAD(&vmw_bo->validate_list);
@@ -1582,9 +1551,7 @@ int vmw_dmabuf_init(struct vmw_private *dev_priv,
 static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
 {
 	struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
-	struct ttm_bo_global *glob = bo->glob;
 
-	ttm_mem_global_free(glob->mem_glob, bo->acc_size);
 	kfree(vmw_user_bo);
 }
 
include/drm/ttm/ttm_bo_api.h +18 -1
index 8d95a42..974c8f8 100644
@@ -429,9 +429,9 @@ extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
  * -EBUSY if the buffer is busy and no_wait is true.
  * -ERESTARTSYS if interrupted by a signal.
  */
-
 extern int
 ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
+
 /**
  * ttm_bo_synccpu_write_release:
  *
@@ -442,6 +442,22 @@ ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
 extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
 
 /**
+ * ttm_bo_acc_size
+ *
+ * @bdev: Pointer to a ttm_bo_device struct.
+ * @bo_size: size of the buffer object in byte.
+ * @struct_size: size of the structure holding buffer object datas
+ *
+ * Returns size to account for a buffer object
+ */
+size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
+		       unsigned long bo_size,
+		       unsigned struct_size);
+size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
+			   unsigned long bo_size,
+			   unsigned struct_size);
+
+/**
  * ttm_bo_init
  *
  * @bdev: Pointer to a ttm_bo_device struct.
@@ -488,6 +504,7 @@ extern int ttm_bo_init(struct ttm_bo_device *bdev,
 			struct file *persistent_swap_storage,
 			size_t acc_size,
 			void (*destroy) (struct ttm_buffer_object *));
+
 /**
  * ttm_bo_synccpu_object_init
  *
include/drm/ttm/ttm_bo_driver.h +0 -5
index b2a0848..2be8891 100644
@@ -469,9 +469,6 @@ struct ttm_bo_global_ref {
  * @dummy_read_page: Pointer to a dummy page used for mapping requests
  * of unpopulated pages.
  * @shrink: A shrink callback object used for buffer object swap.
- * @ttm_bo_extra_size: Extra size (sizeof(struct ttm_buffer_object) excluded)
- * used by a buffer object. This is excluding page arrays and backing pages.
- * @ttm_bo_size: This is @ttm_bo_extra_size + sizeof(struct ttm_buffer_object).
  * @device_list_mutex: Mutex protecting the device list.
  * This mutex is held while traversing the device list for pm options.
  * @lru_lock: Spinlock protecting the bo subsystem lru lists.
@@ -489,8 +486,6 @@ struct ttm_bo_global {
 	struct ttm_mem_global *mem_glob;
 	struct page *dummy_read_page;
 	struct ttm_mem_shrink shrink;
-	size_t ttm_bo_extra_size;
-	size_t ttm_bo_size;
 	struct mutex device_list_mutex;
 	spinlock_t lru_lock;
 
Signature · 1 line
1.7.7.1
Reviewed-by on v1 14/14
#18 lore raw

Re: [PATCH 14/14] drm/ttm: simplify memory accounting for ttm user

Quoted 8 lines · On 11/16/2011 05:57 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

Provide helper function to compute the kernel memory size needed for each buffer object. Move all the accounting inside ttm, simplifying driver and avoiding code duplication accross them.

Nice.
One thing I had in mind once (although it's not consistent with the current TTM code) would be that kernel bos should not be accounted, since no other kernel objects are, really...

Accounting should only take place as the result of user-space requesting the creation of a persistent object.

However, if that sounds like a good idea, that could be implemented as a later fix based on the buffer object type.

Quoted 313 lines
---
  drivers/gpu/drm/nouveau/nouveau_bo.c     |    6 +++-
  drivers/gpu/drm/radeon/radeon_object.c   |    8 +++-
  drivers/gpu/drm/ttm/ttm_bo.c             |   52 +++++++++++++++++++++++-------
  drivers/gpu/drm/vmwgfx/vmwgfx_resource.c |   35 +-------------------
  include/drm/ttm/ttm_bo_api.h             |   19 ++++++++++-
  include/drm/ttm/ttm_bo_driver.h          |    5 ---
  6 files changed, 70 insertions(+), 55 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +5 -1
index 4347776..857bca4 100644
@@ -93,6 +93,7 @@ nouveau_bo_new(struct drm_device *dev, int size, int align,
  {
  	struct drm_nouveau_private *dev_priv = dev->dev_private;
  	struct nouveau_bo *nvbo;
+	size_t acc_size;
  	int ret;
 
  	nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
@@ -115,9 +116,12 @@ nouveau_bo_new(struct drm_device *dev, int size, int align,
  	nvbo->bo.mem.num_pages = size>>  PAGE_SHIFT;
  	nouveau_bo_placement_set(nvbo, flags, 0);
 
+	acc_size = ttm_bo_dma_acc_size(&dev_priv->ttm.bdev, size,
+				       sizeof(struct nouveau_bo));
+
  	ret = ttm_bo_init(&dev_priv->ttm.bdev,&nvbo->bo, size,
  			  ttm_bo_type_device,&nvbo->placement,
-			  align>>  PAGE_SHIFT, 0, false, NULL, size,
+			  align>>  PAGE_SHIFT, 0, false, NULL, acc_size,
  			  nouveau_bo_del_ttm);
  	if (ret) {
  		/* ttm will call nouveau_bo_del_ttm if it fails.. */
drivers/gpu/drm/radeon/radeon_object.c +6 -2
index 1c85152..695b480 100644
@@ -95,6 +95,7 @@ int radeon_bo_create(struct radeon_device *rdev,
  	enum ttm_bo_type type;
  	unsigned long page_align = roundup(byte_align, PAGE_SIZE)>>  PAGE_SHIFT;
  	unsigned long max_size = 0;
+	size_t acc_size;
  	int r;
 
  	size = ALIGN(size, PAGE_SIZE);
@@ -117,6 +118,9 @@ int radeon_bo_create(struct radeon_device *rdev,
  		return -ENOMEM;
  	}
 
+	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
+				       sizeof(struct radeon_bo));
+
  retry:
  	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
  	if (bo == NULL)
@@ -134,8 +138,8 @@ retry:
  	/* Kernel allocation are uninterruptible */
  	mutex_lock(&rdev->vram_mutex);
  	r = ttm_bo_init(&rdev->mman.bdev,&bo->tbo, size, type,
-			&bo->placement, page_align, 0, !kernel, NULL, size,
-			&radeon_ttm_bo_destroy);
+			&bo->placement, page_align, 0, !kernel, NULL,
+			acc_size,&radeon_ttm_bo_destroy);
  	mutex_unlock(&rdev->vram_mutex);
  	if (unlikely(r != 0)) {
  		if (r != -ERESTARTSYS) {
drivers/gpu/drm/ttm/ttm_bo.c +40 -12
index cb73527..de7ad99 100644
@@ -137,6 +137,7 @@ static void ttm_bo_release_list(struct kref *list_kref)
  	struct ttm_buffer_object *bo =
  	    container_of(list_kref, struct ttm_buffer_object, list_kref);
  	struct ttm_bo_device *bdev = bo->bdev;
+	size_t acc_size = bo->acc_size;
 
  	BUG_ON(atomic_read(&bo->list_kref.refcount));
  	BUG_ON(atomic_read(&bo->kref.refcount));
@@ -152,9 +153,9 @@ static void ttm_bo_release_list(struct kref *list_kref)
  	if (bo->destroy)
  		bo->destroy(bo);
  	else {
-		ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
  		kfree(bo);
  	}
+	ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  }
 
  int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
@@ -1157,6 +1158,17 @@ int ttm_bo_init(struct ttm_bo_device *bdev,
  {
  	int ret = 0;
  	unsigned long num_pages;
+	struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
+
+	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
+	if (ret) {
+		printk(KERN_ERR TTM_PFX "Out of kernel memory.\n");
+		if (destroy)
+			(*destroy)(bo);
+		else
+			kfree(bo);
+		return -ENOMEM;
+	}
 
  	size += buffer_start&  ~PAGE_MASK;
  	num_pages = (size + PAGE_SIZE - 1)>>  PAGE_SHIFT;
@@ -1227,14 +1239,34 @@ out_err:
  }
  EXPORT_SYMBOL(ttm_bo_init);
 
-static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
-				 unsigned long num_pages)
+size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
+		       unsigned long bo_size,
+		       unsigned struct_size)
  {
-	size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1)&
-	    PAGE_MASK;
+	unsigned npages = (PAGE_ALIGN(bo_size))>>  PAGE_SHIFT;
+	size_t size = 0;
 
-	return glob->ttm_bo_size + 2 * page_array_size;
+	size += ttm_round_pot(struct_size);
+	size += PAGE_ALIGN(npages * sizeof(void *));
+	size += ttm_round_pot(sizeof(struct ttm_tt));
+	return size;
  }
+EXPORT_SYMBOL(ttm_bo_acc_size);
+
+size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
+			   unsigned long bo_size,
+			   unsigned struct_size)
+{
+	unsigned npages = (PAGE_ALIGN(bo_size))>>  PAGE_SHIFT;
+	size_t size = 0;
+
+	size += ttm_round_pot(struct_size);
+	size += PAGE_ALIGN(npages * sizeof(void *));
+	size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
+	size += ttm_round_pot(sizeof(struct ttm_dma_tt));
+	return size;
+}
+EXPORT_SYMBOL(ttm_bo_dma_acc_size);
 
  int ttm_bo_create(struct ttm_bo_device *bdev,
  			unsigned long size,
@@ -1248,10 +1280,10 @@ int ttm_bo_create(struct ttm_bo_device *bdev,
  {
  	struct ttm_buffer_object *bo;
  	struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
+	size_t acc_size;
  	int ret;
 
-	size_t acc_size =
-	    ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1)>>  PAGE_SHIFT);
+	acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  	if (unlikely(ret != 0))
  		return ret;
@@ -1437,10 +1469,6 @@ int ttm_bo_global_init(struct drm_global_reference *ref)
  		goto out_no_shrink;
  	}
 
-	glob->ttm_bo_extra_size = ttm_round_pot(sizeof(struct ttm_tt));
-	glob->ttm_bo_size = glob->ttm_bo_extra_size +
-		ttm_round_pot(sizeof(struct ttm_buffer_object));
-
  	atomic_set(&glob->bo_count, 0);
 
  	ret = kobject_init_and_add(
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +1 -34
index 86c5e4c..2eb84a5 100644
@@ -1517,29 +1517,10 @@ out_bad_surface:
  /**
   * Buffer management.
   */
-
-static size_t vmw_dmabuf_acc_size(struct ttm_bo_global *glob,
-				  unsigned long num_pages)
-{
-	static size_t bo_user_size = ~0;
-
-	size_t page_array_size =
-	    (num_pages * sizeof(void *) + PAGE_SIZE - 1)&  PAGE_MASK;
-
-	if (unlikely(bo_user_size == ~0)) {
-		bo_user_size = glob->ttm_bo_extra_size +
-		    ttm_round_pot(sizeof(struct vmw_dma_buffer));
-	}
-
-	return bo_user_size + page_array_size;
-}
-
  void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  {
  	struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
-	struct ttm_bo_global *glob = bo->glob;
 
-	ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  	kfree(vmw_bo);
  }
 
@@ -1550,24 +1531,12 @@ int vmw_dmabuf_init(struct vmw_private *dev_priv,
  		    void (*bo_free) (struct ttm_buffer_object *bo))
  {
  	struct ttm_bo_device *bdev =&dev_priv->bdev;
-	struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  	size_t acc_size;
  	int ret;
 
  	BUG_ON(!bo_free);
 
-	acc_size =
-	    vmw_dmabuf_acc_size(bdev->glob,
-				(size + PAGE_SIZE - 1)>>  PAGE_SHIFT);
-
-	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
-	if (unlikely(ret != 0)) {
-		/* we must free the bo here as
-		 * ttm_buffer_object_init does so as well */
-		bo_free(&vmw_bo->base);
-		return ret;
-	}
-
+	acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
  	memset(vmw_bo, 0, sizeof(*vmw_bo));
 
  	INIT_LIST_HEAD(&vmw_bo->validate_list);
@@ -1582,9 +1551,7 @@ int vmw_dmabuf_init(struct vmw_private *dev_priv,
  static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  {
  	struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
-	struct ttm_bo_global *glob = bo->glob;
 
-	ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  	kfree(vmw_user_bo);
  }
 
include/drm/ttm/ttm_bo_api.h +18 -1
index 8d95a42..974c8f8 100644
@@ -429,9 +429,9 @@ extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
   * -EBUSY if the buffer is busy and no_wait is true.
   * -ERESTARTSYS if interrupted by a signal.
   */
-
  extern int
  ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
+
  /**
   * ttm_bo_synccpu_write_release:
   *
@@ -442,6 +442,22 @@ ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
  extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
 
  /**
+ * ttm_bo_acc_size
+ *
+ * @bdev: Pointer to a ttm_bo_device struct.
+ * @bo_size: size of the buffer object in byte.
+ * @struct_size: size of the structure holding buffer object datas
+ *
+ * Returns size to account for a buffer object
+ */
+size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
+		       unsigned long bo_size,
+		       unsigned struct_size);
+size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
+			   unsigned long bo_size,
+			   unsigned struct_size);
+
+/**
   * ttm_bo_init
   *
   * @bdev: Pointer to a ttm_bo_device struct.
@@ -488,6 +504,7 @@ extern int ttm_bo_init(struct ttm_bo_device *bdev,
  			struct file *persistent_swap_storage,
  			size_t acc_size,
  			void (*destroy) (struct ttm_buffer_object *));
+
  /**
   * ttm_bo_synccpu_object_init
   *
include/drm/ttm/ttm_bo_driver.h +0 -5
index b2a0848..2be8891 100644
@@ -469,9 +469,6 @@ struct ttm_bo_global_ref {
   * @dummy_read_page: Pointer to a dummy page used for mapping requests
   * of unpopulated pages.
   * @shrink: A shrink callback object used for buffer object swap.
- * @ttm_bo_extra_size: Extra size (sizeof(struct ttm_buffer_object) excluded)
- * used by a buffer object. This is excluding page arrays and backing pages.
- * @ttm_bo_size: This is @ttm_bo_extra_size + sizeof(struct ttm_buffer_object).
   * @device_list_mutex: Mutex protecting the device list.
   * This mutex is held while traversing the device list for pm options.
   * @lru_lock: Spinlock protecting the bo subsystem lru lists.
@@ -489,8 +486,6 @@ struct ttm_bo_global {
  	struct ttm_mem_global *mem_glob;
  	struct page *dummy_read_page;
  	struct ttm_mem_shrink shrink;
-	size_t ttm_bo_extra_size;
-	size_t ttm_bo_size;
  	struct mutex device_list_mutex;
  	spinlock_t lru_lock;
 
#19 lore raw
In reply to [email protected]
Quoted 2 lines · On Wed, Nov 16, 2011 at 11:57:24AM -0500, [email protected] wrote:

Respin some of the patch with syntax/typo fix + patch 10 with proper memory accounting of page being free.

Ran it yesterday all day and as well today using the TTM DMA (so SWIOTLB is on) and had no trouble. This is with a Radeon HD 4870 on a AMD Phenom 6-core thingy.

Next will, when I am back, will run it with some more hardware - some ATI low-end, NVidia low and high-end and try as well on a G5 which has a radeon card in it.

Oh, didn't even try to revert the patch you mentioned - I just used your latest posting code and ran with that.