1 // SPDX-License-Identifier: GPL-2.0
3 * Support for Medifield PNW Camera Imaging ISP subsystem.
5 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
7 * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 * This file contains functions for buffer object structure management
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/gfp.h> /* for GFP_ATOMIC */
27 #include <linux/mm_types.h>
28 #include <linux/hugetlb.h>
29 #include <linux/highmem.h>
30 #include <linux/slab.h> /* for kmalloc */
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
35 #include <linux/errno.h>
37 #include <asm/current.h>
38 #include <linux/sched/signal.h>
39 #include <linux/file.h>
41 #include <asm/set_memory.h>
43 #include "atomisp_internal.h"
44 #include "hmm/hmm_common.h"
45 #include "hmm/hmm_pool.h"
46 #include "hmm/hmm_bo.h"
48 static unsigned int order_to_nr(unsigned int order)
53 static unsigned int nr_to_order_bottom(unsigned int nr)
58 static int __bo_init(struct hmm_bo_device *bdev, struct hmm_buffer_object *bo,
61 check_bodev_null_return(bdev, -EINVAL);
62 var_equal_return(hmm_bo_device_inited(bdev), 0, -EINVAL,
63 "hmm_bo_device not inited yet.\n");
64 /* prevent zero size buffer object */
66 dev_err(atomisp_dev, "0 size buffer is not allowed.\n");
70 memset(bo, 0, sizeof(*bo));
71 mutex_init(&bo->mutex);
73 /* init the bo->list HEAD as an element of entire_bo_list */
74 INIT_LIST_HEAD(&bo->list);
78 bo->status = HMM_BO_FREE;
79 bo->start = bdev->start;
81 bo->end = bo->start + pgnr_to_size(pgnr);
88 static struct hmm_buffer_object *__bo_search_and_remove_from_free_rbtree(
89 struct rb_node *node, unsigned int pgnr)
91 struct hmm_buffer_object *this, *ret_bo, *temp_bo;
93 this = rb_entry(node, struct hmm_buffer_object, node);
94 if (this->pgnr == pgnr ||
95 (this->pgnr > pgnr && !this->node.rb_left)) {
96 goto remove_bo_and_return;
98 if (this->pgnr < pgnr) {
99 if (!this->node.rb_right)
101 ret_bo = __bo_search_and_remove_from_free_rbtree(
102 this->node.rb_right, pgnr);
104 ret_bo = __bo_search_and_remove_from_free_rbtree(
105 this->node.rb_left, pgnr);
108 if (this->pgnr > pgnr)
109 goto remove_bo_and_return;
116 remove_bo_and_return:
117 /* NOTE: All nodes on free rbtree have a 'prev' that points to NULL.
118 * 1. check if 'this->next' is NULL:
119 * yes: erase 'this' node and rebalance rbtree, return 'this'.
122 rb_erase(&this->node, &this->bdev->free_rbtree);
125 /* NOTE: if 'this->next' is not NULL, always return 'this->next' bo.
126 * 2. check if 'this->next->next' is NULL:
127 * yes: change the related 'next/prev' pointer,
128 * return 'this->next' but the rbtree stays unchanged.
130 temp_bo = this->next;
131 this->next = temp_bo->next;
133 temp_bo->next->prev = this;
134 temp_bo->next = NULL;
135 temp_bo->prev = NULL;
139 static struct hmm_buffer_object *__bo_search_by_addr(struct rb_root *root,
142 struct rb_node *n = root->rb_node;
143 struct hmm_buffer_object *bo;
146 bo = rb_entry(n, struct hmm_buffer_object, node);
148 if (bo->start > start) {
152 } else if (bo->start < start) {
164 static struct hmm_buffer_object *__bo_search_by_addr_in_range(
165 struct rb_root *root, unsigned int start)
167 struct rb_node *n = root->rb_node;
168 struct hmm_buffer_object *bo;
171 bo = rb_entry(n, struct hmm_buffer_object, node);
173 if (bo->start > start) {
189 static void __bo_insert_to_free_rbtree(struct rb_root *root,
190 struct hmm_buffer_object *bo)
192 struct rb_node **new = &root->rb_node;
193 struct rb_node *parent = NULL;
194 struct hmm_buffer_object *this;
195 unsigned int pgnr = bo->pgnr;
199 this = container_of(*new, struct hmm_buffer_object, node);
201 if (pgnr < this->pgnr) {
202 new = &((*new)->rb_left);
203 } else if (pgnr > this->pgnr) {
204 new = &((*new)->rb_right);
207 bo->next = this->next;
209 this->next->prev = bo;
211 bo->status = (bo->status & ~HMM_BO_MASK) | HMM_BO_FREE;
216 bo->status = (bo->status & ~HMM_BO_MASK) | HMM_BO_FREE;
218 rb_link_node(&bo->node, parent, new);
219 rb_insert_color(&bo->node, root);
222 static void __bo_insert_to_alloc_rbtree(struct rb_root *root,
223 struct hmm_buffer_object *bo)
225 struct rb_node **new = &root->rb_node;
226 struct rb_node *parent = NULL;
227 struct hmm_buffer_object *this;
228 unsigned int start = bo->start;
232 this = container_of(*new, struct hmm_buffer_object, node);
234 if (start < this->start)
235 new = &((*new)->rb_left);
237 new = &((*new)->rb_right);
240 kref_init(&bo->kref);
241 bo->status = (bo->status & ~HMM_BO_MASK) | HMM_BO_ALLOCED;
243 rb_link_node(&bo->node, parent, new);
244 rb_insert_color(&bo->node, root);
247 static struct hmm_buffer_object *__bo_break_up(struct hmm_bo_device *bdev,
248 struct hmm_buffer_object *bo,
251 struct hmm_buffer_object *new_bo;
255 new_bo = kmem_cache_alloc(bdev->bo_cache, GFP_KERNEL);
257 dev_err(atomisp_dev, "%s: __bo_alloc failed!\n", __func__);
260 ret = __bo_init(bdev, new_bo, pgnr);
262 dev_err(atomisp_dev, "%s: __bo_init failed!\n", __func__);
263 kmem_cache_free(bdev->bo_cache, new_bo);
267 new_bo->start = bo->start;
268 new_bo->end = new_bo->start + pgnr_to_size(pgnr);
269 bo->start = new_bo->end;
270 bo->pgnr = bo->pgnr - pgnr;
272 spin_lock_irqsave(&bdev->list_lock, flags);
273 list_add_tail(&new_bo->list, &bo->list);
274 spin_unlock_irqrestore(&bdev->list_lock, flags);
279 static void __bo_take_off_handling(struct hmm_buffer_object *bo)
281 struct hmm_bo_device *bdev = bo->bdev;
282 /* There are 4 situations when we take off a known bo from free rbtree:
283 * 1. if bo->next && bo->prev == NULL, bo is a rbtree node
284 * and does not have a linked list after bo, to take off this bo,
285 * we just need erase bo directly and rebalance the free rbtree
287 if (!bo->prev && !bo->next) {
288 rb_erase(&bo->node, &bdev->free_rbtree);
289 /* 2. when bo->next != NULL && bo->prev == NULL, bo is a rbtree node,
290 * and has a linked list,to take off this bo we need erase bo
291 * first, then, insert bo->next into free rbtree and rebalance
294 } else if (!bo->prev && bo->next) {
295 bo->next->prev = NULL;
296 rb_erase(&bo->node, &bdev->free_rbtree);
297 __bo_insert_to_free_rbtree(&bdev->free_rbtree, bo->next);
299 /* 3. when bo->prev != NULL && bo->next == NULL, bo is not a rbtree
300 * node, bo is the last element of the linked list after rbtree
301 * node, to take off this bo, we just need set the "prev/next"
302 * pointers to NULL, the free rbtree stays unchaged
304 } else if (bo->prev && !bo->next) {
305 bo->prev->next = NULL;
307 /* 4. when bo->prev != NULL && bo->next != NULL ,bo is not a rbtree
308 * node, bo is in the middle of the linked list after rbtree node,
309 * to take off this bo, we just set take the "prev/next" pointers
310 * to NULL, the free rbtree stays unchaged
312 } else if (bo->prev && bo->next) {
313 bo->next->prev = bo->prev;
314 bo->prev->next = bo->next;
320 static struct hmm_buffer_object *__bo_merge(struct hmm_buffer_object *bo,
321 struct hmm_buffer_object *next_bo)
323 struct hmm_bo_device *bdev;
327 next_bo->start = bo->start;
328 next_bo->pgnr = next_bo->pgnr + bo->pgnr;
330 spin_lock_irqsave(&bdev->list_lock, flags);
332 spin_unlock_irqrestore(&bdev->list_lock, flags);
334 kmem_cache_free(bo->bdev->bo_cache, bo);
340 * hmm_bo_device functions.
342 int hmm_bo_device_init(struct hmm_bo_device *bdev,
343 struct isp_mmu_client *mmu_driver,
344 unsigned int vaddr_start,
347 struct hmm_buffer_object *bo;
351 check_bodev_null_return(bdev, -EINVAL);
353 ret = isp_mmu_init(&bdev->mmu, mmu_driver);
355 dev_err(atomisp_dev, "isp_mmu_init failed.\n");
359 bdev->start = vaddr_start;
360 bdev->pgnr = size_to_pgnr_ceil(size);
361 bdev->size = pgnr_to_size(bdev->pgnr);
363 spin_lock_init(&bdev->list_lock);
364 mutex_init(&bdev->rbtree_mutex);
366 bdev->flag = HMM_BO_DEVICE_INITED;
368 INIT_LIST_HEAD(&bdev->entire_bo_list);
369 bdev->allocated_rbtree = RB_ROOT;
370 bdev->free_rbtree = RB_ROOT;
372 bdev->bo_cache = kmem_cache_create("bo_cache",
373 sizeof(struct hmm_buffer_object), 0, 0, NULL);
374 if (!bdev->bo_cache) {
375 dev_err(atomisp_dev, "%s: create cache failed!\n", __func__);
376 isp_mmu_exit(&bdev->mmu);
380 bo = kmem_cache_alloc(bdev->bo_cache, GFP_KERNEL);
382 dev_err(atomisp_dev, "%s: __bo_alloc failed!\n", __func__);
383 isp_mmu_exit(&bdev->mmu);
387 ret = __bo_init(bdev, bo, bdev->pgnr);
389 dev_err(atomisp_dev, "%s: __bo_init failed!\n", __func__);
390 kmem_cache_free(bdev->bo_cache, bo);
391 isp_mmu_exit(&bdev->mmu);
395 spin_lock_irqsave(&bdev->list_lock, flags);
396 list_add_tail(&bo->list, &bdev->entire_bo_list);
397 spin_unlock_irqrestore(&bdev->list_lock, flags);
399 __bo_insert_to_free_rbtree(&bdev->free_rbtree, bo);
404 struct hmm_buffer_object *hmm_bo_alloc(struct hmm_bo_device *bdev,
407 struct hmm_buffer_object *bo, *new_bo;
408 struct rb_root *root = &bdev->free_rbtree;
410 check_bodev_null_return(bdev, NULL);
411 var_equal_return(hmm_bo_device_inited(bdev), 0, NULL,
412 "hmm_bo_device not inited yet.\n");
415 dev_err(atomisp_dev, "0 size buffer is not allowed.\n");
419 mutex_lock(&bdev->rbtree_mutex);
420 bo = __bo_search_and_remove_from_free_rbtree(root->rb_node, pgnr);
422 mutex_unlock(&bdev->rbtree_mutex);
423 dev_err(atomisp_dev, "%s: Out of Memory! hmm_bo_alloc failed",
428 if (bo->pgnr > pgnr) {
429 new_bo = __bo_break_up(bdev, bo, pgnr);
431 mutex_unlock(&bdev->rbtree_mutex);
432 dev_err(atomisp_dev, "%s: __bo_break_up failed!\n",
437 __bo_insert_to_alloc_rbtree(&bdev->allocated_rbtree, new_bo);
438 __bo_insert_to_free_rbtree(&bdev->free_rbtree, bo);
440 mutex_unlock(&bdev->rbtree_mutex);
444 __bo_insert_to_alloc_rbtree(&bdev->allocated_rbtree, bo);
446 mutex_unlock(&bdev->rbtree_mutex);
450 void hmm_bo_release(struct hmm_buffer_object *bo)
452 struct hmm_bo_device *bdev = bo->bdev;
453 struct hmm_buffer_object *next_bo, *prev_bo;
455 mutex_lock(&bdev->rbtree_mutex);
460 * how to destroy the bo when it is stilled MMAPED?
462 * ideally, this will not happened as hmm_bo_release
463 * will only be called when kref reaches 0, and in mmap
464 * operation the hmm_bo_ref will eventually be called.
465 * so, if this happened, something goes wrong.
467 if (bo->status & HMM_BO_MMAPED) {
468 mutex_unlock(&bdev->rbtree_mutex);
469 dev_dbg(atomisp_dev, "destroy bo which is MMAPED, do nothing\n");
473 if (bo->status & HMM_BO_BINDED) {
474 dev_warn(atomisp_dev, "the bo is still binded, unbind it first...\n");
478 if (bo->status & HMM_BO_PAGE_ALLOCED) {
479 dev_warn(atomisp_dev, "the pages is not freed, free pages first\n");
480 hmm_bo_free_pages(bo);
482 if (bo->status & HMM_BO_VMAPED || bo->status & HMM_BO_VMAPED_CACHED) {
483 dev_warn(atomisp_dev, "the vunmap is not done, do it...\n");
487 rb_erase(&bo->node, &bdev->allocated_rbtree);
489 prev_bo = list_entry(bo->list.prev, struct hmm_buffer_object, list);
490 next_bo = list_entry(bo->list.next, struct hmm_buffer_object, list);
492 if (bo->list.prev != &bdev->entire_bo_list &&
493 prev_bo->end == bo->start &&
494 (prev_bo->status & HMM_BO_MASK) == HMM_BO_FREE) {
495 __bo_take_off_handling(prev_bo);
496 bo = __bo_merge(prev_bo, bo);
499 if (bo->list.next != &bdev->entire_bo_list &&
500 next_bo->start == bo->end &&
501 (next_bo->status & HMM_BO_MASK) == HMM_BO_FREE) {
502 __bo_take_off_handling(next_bo);
503 bo = __bo_merge(bo, next_bo);
506 __bo_insert_to_free_rbtree(&bdev->free_rbtree, bo);
508 mutex_unlock(&bdev->rbtree_mutex);
512 void hmm_bo_device_exit(struct hmm_bo_device *bdev)
514 struct hmm_buffer_object *bo;
517 dev_dbg(atomisp_dev, "%s: entering!\n", __func__);
519 check_bodev_null_return_void(bdev);
522 * release all allocated bos even they a in use
523 * and all bos will be merged into a big bo
525 while (!RB_EMPTY_ROOT(&bdev->allocated_rbtree))
527 rbtree_node_to_hmm_bo(bdev->allocated_rbtree.rb_node));
529 dev_dbg(atomisp_dev, "%s: finished releasing all allocated bos!\n",
532 /* free all bos to release all ISP virtual memory */
533 while (!list_empty(&bdev->entire_bo_list)) {
534 bo = list_to_hmm_bo(bdev->entire_bo_list.next);
536 spin_lock_irqsave(&bdev->list_lock, flags);
538 spin_unlock_irqrestore(&bdev->list_lock, flags);
540 kmem_cache_free(bdev->bo_cache, bo);
543 dev_dbg(atomisp_dev, "%s: finished to free all bos!\n", __func__);
545 kmem_cache_destroy(bdev->bo_cache);
547 isp_mmu_exit(&bdev->mmu);
550 int hmm_bo_device_inited(struct hmm_bo_device *bdev)
552 check_bodev_null_return(bdev, -EINVAL);
554 return bdev->flag == HMM_BO_DEVICE_INITED;
557 int hmm_bo_allocated(struct hmm_buffer_object *bo)
559 check_bo_null_return(bo, 0);
561 return bo->status & HMM_BO_ALLOCED;
564 struct hmm_buffer_object *hmm_bo_device_search_start(
565 struct hmm_bo_device *bdev, ia_css_ptr vaddr)
567 struct hmm_buffer_object *bo;
569 check_bodev_null_return(bdev, NULL);
571 mutex_lock(&bdev->rbtree_mutex);
572 bo = __bo_search_by_addr(&bdev->allocated_rbtree, vaddr);
574 mutex_unlock(&bdev->rbtree_mutex);
575 dev_err(atomisp_dev, "%s can not find bo with addr: 0x%x\n",
579 mutex_unlock(&bdev->rbtree_mutex);
584 struct hmm_buffer_object *hmm_bo_device_search_in_range(
585 struct hmm_bo_device *bdev, unsigned int vaddr)
587 struct hmm_buffer_object *bo;
589 check_bodev_null_return(bdev, NULL);
591 mutex_lock(&bdev->rbtree_mutex);
592 bo = __bo_search_by_addr_in_range(&bdev->allocated_rbtree, vaddr);
594 mutex_unlock(&bdev->rbtree_mutex);
595 dev_err(atomisp_dev, "%s can not find bo contain addr: 0x%x\n",
599 mutex_unlock(&bdev->rbtree_mutex);
604 struct hmm_buffer_object *hmm_bo_device_search_vmap_start(
605 struct hmm_bo_device *bdev, const void *vaddr)
607 struct list_head *pos;
608 struct hmm_buffer_object *bo;
611 check_bodev_null_return(bdev, NULL);
613 spin_lock_irqsave(&bdev->list_lock, flags);
614 list_for_each(pos, &bdev->entire_bo_list) {
615 bo = list_to_hmm_bo(pos);
616 /* pass bo which has no vm_node allocated */
617 if ((bo->status & HMM_BO_MASK) == HMM_BO_FREE)
619 if (bo->vmap_addr == vaddr)
622 spin_unlock_irqrestore(&bdev->list_lock, flags);
625 spin_unlock_irqrestore(&bdev->list_lock, flags);
629 static void free_private_bo_pages(struct hmm_buffer_object *bo,
630 struct hmm_pool *dypool,
631 struct hmm_pool *repool,
636 for (i = 0; i < free_pgnr; i++) {
637 switch (bo->page_obj[i].type) {
638 case HMM_PAGE_TYPE_RESERVED:
640 && repool->pops->pool_free_pages) {
641 repool->pops->pool_free_pages(repool->pool_info,
643 hmm_mem_stat.res_cnt--;
647 * HMM_PAGE_TYPE_GENERAL indicates that pages are from system
648 * memory, so when free them, they should be put into dynamic
651 case HMM_PAGE_TYPE_DYNAMIC:
652 case HMM_PAGE_TYPE_GENERAL:
654 && dypool->pops->pool_inited
655 && dypool->pops->pool_inited(dypool->pool_info)) {
656 if (dypool->pops->pool_free_pages)
657 dypool->pops->pool_free_pages(
666 * if dynamic memory pool doesn't exist, need to free
667 * pages to system directly.
670 ret = set_pages_wb(bo->page_obj[i].page, 1);
673 "set page to WB err ...ret = %d\n",
676 W/A: set_pages_wb seldom return value = -EFAULT
677 indicate that address of page is not in valid
678 range(0xffff880000000000~0xffffc7ffffffffff)
679 then, _free_pages would panic; Do not know why page
680 address be valid,it maybe memory corruption by lowmemory
683 __free_pages(bo->page_obj[i].page, 0);
684 hmm_mem_stat.sys_size--;
693 /*Allocate pages which will be used only by ISP*/
694 static int alloc_private_pages(struct hmm_buffer_object *bo,
697 struct hmm_pool *dypool,
698 struct hmm_pool *repool)
701 unsigned int pgnr, order, blk_pgnr, alloc_pgnr;
703 gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN; /* REVISIT: need __GFP_FS too? */
705 int failure_number = 0;
706 bool reduce_order = false;
707 bool lack_mem = true;
710 gfp |= __GFP_HIGHMEM;
714 bo->page_obj = kmalloc_array(pgnr, sizeof(struct hmm_page_object),
716 if (unlikely(!bo->page_obj))
723 * get physical pages from dynamic pages pool.
725 if (dypool->pops && dypool->pops->pool_alloc_pages) {
726 alloc_pgnr = dypool->pops->pool_alloc_pages(dypool->pool_info,
729 hmm_mem_stat.dyc_size -= alloc_pgnr;
731 if (alloc_pgnr == pgnr)
739 * get physical pages from reserved pages pool for atomisp.
741 if (repool->pops && repool->pops->pool_alloc_pages) {
742 alloc_pgnr = repool->pops->pool_alloc_pages(repool->pool_info,
743 &bo->page_obj[i], pgnr,
745 hmm_mem_stat.res_cnt += alloc_pgnr;
746 if (alloc_pgnr == pgnr)
754 order = nr_to_order_bottom(pgnr);
756 * if be short of memory, we will set order to 0
760 order = HMM_MIN_ORDER;
761 else if (order > HMM_MAX_ORDER)
762 order = HMM_MAX_ORDER;
765 * When order > HMM_MIN_ORDER, for performance reasons we don't
766 * want alloc_pages() to sleep. In case it fails and fallbacks
767 * to HMM_MIN_ORDER or in case the requested order is originally
768 * the minimum value, we can allow alloc_pages() to sleep for
769 * robustness purpose.
771 * REVISIT: why __GFP_FS is necessary?
773 if (order == HMM_MIN_ORDER) {
775 gfp |= __GFP_RECLAIM | __GFP_FS;
778 pages = alloc_pages(gfp, order);
779 if (unlikely(!pages)) {
781 * in low memory case, if allocation page fails,
782 * we turn to try if order=0 allocation could
783 * succeed. if order=0 fails too, that means there is
786 if (order == HMM_MIN_ORDER) {
788 "%s: cannot allocate pages\n",
792 order = HMM_MIN_ORDER;
796 * if fail two times continuously, we think be short
799 if (failure_number == 2) {
805 blk_pgnr = order_to_nr(order);
809 * set memory to uncacheable -- UC_MINUS
811 ret = set_pages_uc(pages, blk_pgnr);
814 "set page uncacheablefailed.\n");
816 __free_pages(pages, order);
822 for (j = 0; j < blk_pgnr; j++) {
823 bo->page_obj[i].page = pages + j;
824 bo->page_obj[i++].type = HMM_PAGE_TYPE_GENERAL;
828 hmm_mem_stat.sys_size += blk_pgnr;
831 * if order is not reduced this time, clear
835 reduce_order = false;
844 free_private_bo_pages(bo, dypool, repool, alloc_pgnr);
851 static void free_private_pages(struct hmm_buffer_object *bo,
852 struct hmm_pool *dypool,
853 struct hmm_pool *repool)
855 free_private_bo_pages(bo, dypool, repool, bo->pgnr);
860 static void free_user_pages(struct hmm_buffer_object *bo)
864 hmm_mem_stat.usr_size -= bo->pgnr;
866 if (bo->mem_type == HMM_BO_MEM_TYPE_PFN) {
867 unpin_user_pages(bo->pages, bo->pgnr);
869 for (i = 0; i < bo->pgnr; i++)
870 put_page(bo->pages[i]);
877 * Convert user space virtual address into pages list
879 static int alloc_user_pages(struct hmm_buffer_object *bo,
880 const void __user *userptr, bool cached)
884 struct vm_area_struct *vma;
887 pages = kmalloc_array(bo->pgnr, sizeof(struct page *), GFP_KERNEL);
888 if (unlikely(!pages))
891 bo->page_obj = kmalloc_array(bo->pgnr, sizeof(struct hmm_page_object),
893 if (unlikely(!bo->page_obj)) {
898 mutex_unlock(&bo->mutex);
899 mmap_read_lock(current->mm);
900 vma = find_vma(current->mm, (unsigned long)userptr);
901 mmap_read_unlock(current->mm);
903 dev_err(atomisp_dev, "find_vma failed\n");
906 mutex_lock(&bo->mutex);
909 mutex_lock(&bo->mutex);
911 * Handle frame buffer allocated in other kerenl space driver
912 * and map to user space
915 userptr = untagged_addr(userptr);
919 if (vma->vm_flags & (VM_IO | VM_PFNMAP)) {
920 page_nr = pin_user_pages((unsigned long)userptr, bo->pgnr,
921 FOLL_LONGTERM | FOLL_WRITE,
923 bo->mem_type = HMM_BO_MEM_TYPE_PFN;
925 /*Handle frame buffer allocated in user space*/
926 mutex_unlock(&bo->mutex);
927 page_nr = get_user_pages_fast((unsigned long)userptr,
928 (int)(bo->pgnr), 1, pages);
929 mutex_lock(&bo->mutex);
930 bo->mem_type = HMM_BO_MEM_TYPE_USER;
933 dev_dbg(atomisp_dev, "%s: %d %s pages were allocated as 0x%08x\n",
936 bo->mem_type == HMM_BO_MEM_TYPE_USER ? "user" : "pfn", page_nr);
938 hmm_mem_stat.usr_size += bo->pgnr;
940 /* can be written by caller, not forced */
941 if (page_nr != bo->pgnr) {
943 "get_user_pages err: bo->pgnr = %d, pgnr actually pinned = %d.\n",
948 for (i = 0; i < bo->pgnr; i++) {
949 bo->page_obj[i].page = pages[i];
950 bo->page_obj[i].type = HMM_PAGE_TYPE_GENERAL;
963 * allocate/free physical pages for the bo.
965 * type indicate where are the pages from. currently we have 3 types
966 * of memory: HMM_BO_PRIVATE, HMM_BO_USER, HMM_BO_SHARE.
968 * from_highmem is only valid when type is HMM_BO_PRIVATE, it will
969 * try to alloc memory from highmem if from_highmem is set.
971 * userptr is only valid when type is HMM_BO_USER, it indicates
972 * the start address from user space task.
974 * from_highmem and userptr will both be ignored when type is
977 int hmm_bo_alloc_pages(struct hmm_buffer_object *bo,
978 enum hmm_bo_type type, int from_highmem,
979 const void __user *userptr, bool cached)
983 check_bo_null_return(bo, -EINVAL);
985 mutex_lock(&bo->mutex);
986 check_bo_status_no_goto(bo, HMM_BO_PAGE_ALLOCED, status_err);
990 * add HMM_BO_USER type
992 if (type == HMM_BO_PRIVATE) {
993 ret = alloc_private_pages(bo, from_highmem,
994 cached, &dynamic_pool, &reserved_pool);
995 } else if (type == HMM_BO_USER) {
996 ret = alloc_user_pages(bo, userptr, cached);
998 dev_err(atomisp_dev, "invalid buffer type.\n");
1006 bo->status |= HMM_BO_PAGE_ALLOCED;
1008 mutex_unlock(&bo->mutex);
1013 mutex_unlock(&bo->mutex);
1014 dev_err(atomisp_dev, "alloc pages err...\n");
1017 mutex_unlock(&bo->mutex);
1018 dev_err(atomisp_dev,
1019 "buffer object has already page allocated.\n");
1024 * free physical pages of the bo.
1026 void hmm_bo_free_pages(struct hmm_buffer_object *bo)
1028 check_bo_null_return_void(bo);
1030 mutex_lock(&bo->mutex);
1032 check_bo_status_yes_goto(bo, HMM_BO_PAGE_ALLOCED, status_err2);
1034 /* clear the flag anyway. */
1035 bo->status &= (~HMM_BO_PAGE_ALLOCED);
1037 if (bo->type == HMM_BO_PRIVATE)
1038 free_private_pages(bo, &dynamic_pool, &reserved_pool);
1039 else if (bo->type == HMM_BO_USER)
1040 free_user_pages(bo);
1042 dev_err(atomisp_dev, "invalid buffer type.\n");
1043 mutex_unlock(&bo->mutex);
1048 mutex_unlock(&bo->mutex);
1049 dev_err(atomisp_dev,
1050 "buffer object not page allocated yet.\n");
1053 int hmm_bo_page_allocated(struct hmm_buffer_object *bo)
1055 check_bo_null_return(bo, 0);
1057 return bo->status & HMM_BO_PAGE_ALLOCED;
1061 * get physical page info of the bo.
1063 int hmm_bo_get_page_info(struct hmm_buffer_object *bo,
1064 struct hmm_page_object **page_obj, int *pgnr)
1066 check_bo_null_return(bo, -EINVAL);
1068 mutex_lock(&bo->mutex);
1070 check_bo_status_yes_goto(bo, HMM_BO_PAGE_ALLOCED, status_err);
1072 *page_obj = bo->page_obj;
1075 mutex_unlock(&bo->mutex);
1080 dev_err(atomisp_dev,
1081 "buffer object not page allocated yet.\n");
1082 mutex_unlock(&bo->mutex);
1087 * bind the physical pages to a virtual address space.
1089 int hmm_bo_bind(struct hmm_buffer_object *bo)
1093 struct hmm_bo_device *bdev;
1096 check_bo_null_return(bo, -EINVAL);
1098 mutex_lock(&bo->mutex);
1100 check_bo_status_yes_goto(bo,
1101 HMM_BO_PAGE_ALLOCED | HMM_BO_ALLOCED,
1104 check_bo_status_no_goto(bo, HMM_BO_BINDED, status_err2);
1110 for (i = 0; i < bo->pgnr; i++) {
1112 isp_mmu_map(&bdev->mmu, virt,
1113 page_to_phys(bo->page_obj[i].page), 1);
1116 virt += (1 << PAGE_SHIFT);
1122 * theoretically, we donot need to flush TLB as we didnot change
1123 * any existed address mappings, but for Silicon Hive's MMU, its
1124 * really a bug here. I guess when fetching PTEs (page table entity)
1125 * to TLB, its MMU will fetch additional INVALID PTEs automatically
1126 * for performance issue. EX, we only set up 1 page address mapping,
1127 * meaning updating 1 PTE, but the MMU fetches 4 PTE at one time,
1128 * so the additional 3 PTEs are invalid.
1130 if (bo->start != 0x0)
1131 isp_mmu_flush_tlb_range(&bdev->mmu, bo->start,
1132 (bo->pgnr << PAGE_SHIFT));
1134 bo->status |= HMM_BO_BINDED;
1136 mutex_unlock(&bo->mutex);
1141 /* unbind the physical pages with related virtual address space */
1143 for ( ; i > 0; i--) {
1144 isp_mmu_unmap(&bdev->mmu, virt, 1);
1145 virt += pgnr_to_size(1);
1148 mutex_unlock(&bo->mutex);
1149 dev_err(atomisp_dev,
1150 "setup MMU address mapping failed.\n");
1154 mutex_unlock(&bo->mutex);
1155 dev_err(atomisp_dev, "buffer object already binded.\n");
1158 mutex_unlock(&bo->mutex);
1159 dev_err(atomisp_dev,
1160 "buffer object vm_node or page not allocated.\n");
1165 * unbind the physical pages with related virtual address space.
1167 void hmm_bo_unbind(struct hmm_buffer_object *bo)
1170 struct hmm_bo_device *bdev;
1173 check_bo_null_return_void(bo);
1175 mutex_lock(&bo->mutex);
1177 check_bo_status_yes_goto(bo,
1178 HMM_BO_PAGE_ALLOCED |
1180 HMM_BO_BINDED, status_err);
1186 for (i = 0; i < bo->pgnr; i++) {
1187 isp_mmu_unmap(&bdev->mmu, virt, 1);
1188 virt += pgnr_to_size(1);
1192 * flush TLB as the address mapping has been removed and
1193 * related TLBs should be invalidated.
1195 isp_mmu_flush_tlb_range(&bdev->mmu, bo->start,
1196 (bo->pgnr << PAGE_SHIFT));
1198 bo->status &= (~HMM_BO_BINDED);
1200 mutex_unlock(&bo->mutex);
1205 mutex_unlock(&bo->mutex);
1206 dev_err(atomisp_dev,
1207 "buffer vm or page not allocated or not binded yet.\n");
1210 int hmm_bo_binded(struct hmm_buffer_object *bo)
1214 check_bo_null_return(bo, 0);
1216 mutex_lock(&bo->mutex);
1218 ret = bo->status & HMM_BO_BINDED;
1220 mutex_unlock(&bo->mutex);
1225 void *hmm_bo_vmap(struct hmm_buffer_object *bo, bool cached)
1227 struct page **pages;
1230 check_bo_null_return(bo, NULL);
1232 mutex_lock(&bo->mutex);
1233 if (((bo->status & HMM_BO_VMAPED) && !cached) ||
1234 ((bo->status & HMM_BO_VMAPED_CACHED) && cached)) {
1235 mutex_unlock(&bo->mutex);
1236 return bo->vmap_addr;
1239 /* cached status need to be changed, so vunmap first */
1240 if (bo->status & HMM_BO_VMAPED || bo->status & HMM_BO_VMAPED_CACHED) {
1241 vunmap(bo->vmap_addr);
1242 bo->vmap_addr = NULL;
1243 bo->status &= ~(HMM_BO_VMAPED | HMM_BO_VMAPED_CACHED);
1246 pages = kmalloc_array(bo->pgnr, sizeof(*pages), GFP_KERNEL);
1247 if (unlikely(!pages)) {
1248 mutex_unlock(&bo->mutex);
1252 for (i = 0; i < bo->pgnr; i++)
1253 pages[i] = bo->page_obj[i].page;
1255 bo->vmap_addr = vmap(pages, bo->pgnr, VM_MAP,
1256 cached ? PAGE_KERNEL : PAGE_KERNEL_NOCACHE);
1257 if (unlikely(!bo->vmap_addr)) {
1259 mutex_unlock(&bo->mutex);
1260 dev_err(atomisp_dev, "vmap failed...\n");
1263 bo->status |= (cached ? HMM_BO_VMAPED_CACHED : HMM_BO_VMAPED);
1267 mutex_unlock(&bo->mutex);
1268 return bo->vmap_addr;
1271 void hmm_bo_flush_vmap(struct hmm_buffer_object *bo)
1273 check_bo_null_return_void(bo);
1275 mutex_lock(&bo->mutex);
1276 if (!(bo->status & HMM_BO_VMAPED_CACHED) || !bo->vmap_addr) {
1277 mutex_unlock(&bo->mutex);
1281 clflush_cache_range(bo->vmap_addr, bo->pgnr * PAGE_SIZE);
1282 mutex_unlock(&bo->mutex);
1285 void hmm_bo_vunmap(struct hmm_buffer_object *bo)
1287 check_bo_null_return_void(bo);
1289 mutex_lock(&bo->mutex);
1290 if (bo->status & HMM_BO_VMAPED || bo->status & HMM_BO_VMAPED_CACHED) {
1291 vunmap(bo->vmap_addr);
1292 bo->vmap_addr = NULL;
1293 bo->status &= ~(HMM_BO_VMAPED | HMM_BO_VMAPED_CACHED);
1296 mutex_unlock(&bo->mutex);
1300 void hmm_bo_ref(struct hmm_buffer_object *bo)
1302 check_bo_null_return_void(bo);
1304 kref_get(&bo->kref);
1307 static void kref_hmm_bo_release(struct kref *kref)
1312 hmm_bo_release(kref_to_hmm_bo(kref));
1315 void hmm_bo_unref(struct hmm_buffer_object *bo)
1317 check_bo_null_return_void(bo);
1319 kref_put(&bo->kref, kref_hmm_bo_release);
1322 static void hmm_bo_vm_open(struct vm_area_struct *vma)
1324 struct hmm_buffer_object *bo =
1325 (struct hmm_buffer_object *)vma->vm_private_data;
1327 check_bo_null_return_void(bo);
1331 mutex_lock(&bo->mutex);
1333 bo->status |= HMM_BO_MMAPED;
1337 mutex_unlock(&bo->mutex);
1340 static void hmm_bo_vm_close(struct vm_area_struct *vma)
1342 struct hmm_buffer_object *bo =
1343 (struct hmm_buffer_object *)vma->vm_private_data;
1345 check_bo_null_return_void(bo);
1349 mutex_lock(&bo->mutex);
1353 if (!bo->mmap_count) {
1354 bo->status &= (~HMM_BO_MMAPED);
1355 vma->vm_private_data = NULL;
1358 mutex_unlock(&bo->mutex);
1361 static const struct vm_operations_struct hmm_bo_vm_ops = {
1362 .open = hmm_bo_vm_open,
1363 .close = hmm_bo_vm_close,
1367 * mmap the bo to user space.
1369 int hmm_bo_mmap(struct vm_area_struct *vma, struct hmm_buffer_object *bo)
1371 unsigned int start, end;
1373 unsigned int pgnr, i;
1376 check_bo_null_return(bo, -EINVAL);
1378 check_bo_status_yes_goto(bo, HMM_BO_PAGE_ALLOCED, status_err);
1381 start = vma->vm_start;
1385 * check vma's virtual address space size and buffer object's size.
1388 if ((start + pgnr_to_size(pgnr)) != end) {
1389 dev_warn(atomisp_dev,
1390 "vma's address space size not equal to buffer object's size");
1394 virt = vma->vm_start;
1395 for (i = 0; i < pgnr; i++) {
1396 pfn = page_to_pfn(bo->page_obj[i].page);
1397 if (remap_pfn_range(vma, virt, pfn, PAGE_SIZE, PAGE_SHARED)) {
1398 dev_warn(atomisp_dev,
1399 "remap_pfn_range failed: virt = 0x%x, pfn = 0x%x, mapped_pgnr = %d\n",
1406 vma->vm_private_data = bo;
1408 vma->vm_ops = &hmm_bo_vm_ops;
1409 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1412 * call hmm_bo_vm_open explicitly.
1414 hmm_bo_vm_open(vma);
1419 dev_err(atomisp_dev, "buffer page not allocated yet.\n");