GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / staging / media / atomisp / pci / atomisp2 / hrt / hive_isp_css_mm_hrt.c
1 /*
2  * Support for Medifield PNW Camera Imaging ISP subsystem.
3  *
4  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
5  *
6  * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version
10  * 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  *
22  */
23
24 #include "atomisp_internal.h"
25
26 #include "hive_isp_css_mm_hrt.h"
27 #include "hmm/hmm.h"
28
29 #define __page_align(size)      (((size) + (PAGE_SIZE-1)) & (~(PAGE_SIZE-1)))
30
31 static void *my_userptr;
32 static unsigned my_num_pages;
33 static enum hrt_userptr_type my_usr_type;
34
35 void hrt_isp_css_mm_set_user_ptr(void *userptr,
36                                  unsigned int num_pages,
37                                  enum hrt_userptr_type type)
38 {
39         my_userptr = userptr;
40         my_num_pages = num_pages;
41         my_usr_type = type;
42 }
43
44 static ia_css_ptr __hrt_isp_css_mm_alloc(size_t bytes, void *userptr,
45                                     unsigned int num_pages,
46                                     enum hrt_userptr_type type,
47                                     bool cached)
48 {
49 #ifdef CONFIG_ION
50         if (type == HRT_USR_ION)
51                 return hmm_alloc(bytes, HMM_BO_ION, 0,
52                                          userptr, cached);
53
54 #endif
55         if (type == HRT_USR_PTR) {
56                 if (userptr == NULL)
57                         return hmm_alloc(bytes, HMM_BO_PRIVATE, 0,
58                                                  NULL, cached);
59                 else {
60                         if (num_pages < ((__page_align(bytes)) >> PAGE_SHIFT))
61                                 dev_err(atomisp_dev,
62                                          "user space memory size is less"
63                                          " than the expected size..\n");
64                         else if (num_pages > ((__page_align(bytes))
65                                               >> PAGE_SHIFT))
66                                 dev_err(atomisp_dev,
67                                          "user space memory size is"
68                                          " large than the expected size..\n");
69
70                         return hmm_alloc(bytes, HMM_BO_USER, 0,
71                                                  userptr, cached);
72                 }
73         } else {
74                 dev_err(atomisp_dev, "user ptr type is incorrect.\n");
75                 return 0;
76         }
77 }
78
79 ia_css_ptr hrt_isp_css_mm_alloc(size_t bytes)
80 {
81         return __hrt_isp_css_mm_alloc(bytes, my_userptr,
82                                       my_num_pages, my_usr_type, false);
83 }
84
85 ia_css_ptr hrt_isp_css_mm_alloc_user_ptr(size_t bytes, void *userptr,
86                                     unsigned int num_pages,
87                                     enum hrt_userptr_type type,
88                                     bool cached)
89 {
90         return __hrt_isp_css_mm_alloc(bytes, userptr, num_pages,
91                                       type, cached);
92 }
93
94 ia_css_ptr hrt_isp_css_mm_alloc_cached(size_t bytes)
95 {
96         if (my_userptr == NULL)
97                 return hmm_alloc(bytes, HMM_BO_PRIVATE, 0, NULL,
98                                                 HMM_CACHED);
99         else {
100                 if (my_num_pages < ((__page_align(bytes)) >> PAGE_SHIFT))
101                         dev_err(atomisp_dev,
102                                         "user space memory size is less"
103                                         " than the expected size..\n");
104                 else if (my_num_pages > ((__page_align(bytes)) >> PAGE_SHIFT))
105                         dev_err(atomisp_dev,
106                                         "user space memory size is"
107                                         " large than the expected size..\n");
108
109                 return hmm_alloc(bytes, HMM_BO_USER, 0,
110                                                 my_userptr, HMM_CACHED);
111         }
112 }
113
114 ia_css_ptr hrt_isp_css_mm_calloc(size_t bytes)
115 {
116         ia_css_ptr ptr = hrt_isp_css_mm_alloc(bytes);
117         if (ptr)
118                 hmm_set(ptr, 0, bytes);
119         return ptr;
120 }
121
122 ia_css_ptr hrt_isp_css_mm_calloc_cached(size_t bytes)
123 {
124         ia_css_ptr ptr = hrt_isp_css_mm_alloc_cached(bytes);
125         if (ptr)
126                 hmm_set(ptr, 0, bytes);
127         return ptr;
128 }
129