ion_cma_secure_heap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * drivers/gpu/ion/ion_secure_cma_heap.c
  3. *
  4. * Copyright (C) Linaro 2012
  5. * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
  6. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  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. */
  18. #include <linux/device.h>
  19. #include <linux/ion.h>
  20. #include <linux/slab.h>
  21. #include <linux/errno.h>
  22. #include <linux/err.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/msm_ion.h>
  25. #include <mach/iommu_domains.h>
  26. #include <asm/cacheflush.h>
  27. /* for ion_heap_ops structure */
  28. #include "ion_priv.h"
  29. #include "msm/ion_cp_common.h"
  30. #define ION_CMA_ALLOCATE_FAILED NULL
  31. struct ion_secure_cma_buffer_info {
  32. /*
  33. * This needs to come first for compatibility with the secure buffer API
  34. */
  35. struct ion_cp_buffer secure;
  36. dma_addr_t phys;
  37. struct sg_table *table;
  38. bool is_cached;
  39. };
  40. struct ion_cma_alloc_chunk {
  41. void *cpu_addr;
  42. struct list_head entry;
  43. dma_addr_t handle;
  44. unsigned long chunk_size;
  45. atomic_t cnt;
  46. };
  47. struct ion_cma_secure_heap {
  48. struct device *dev;
  49. /*
  50. * Protects against races between threads allocating memory/adding to
  51. * pool at the same time. (e.g. thread 1 adds to pool, thread 2
  52. * allocates thread 1's memory before thread 1 knows it needs to
  53. * allocate more.
  54. * Admittedly this is fairly coarse grained right now but the chance for
  55. * contention on this lock is unlikely right now. This can be changed if
  56. * this ever changes in the future
  57. */
  58. struct mutex alloc_lock;
  59. /*
  60. * protects the list of memory chunks in this pool
  61. */
  62. struct mutex chunk_lock;
  63. struct ion_heap heap;
  64. /*
  65. * Bitmap for allocation. This contains the aggregate of all chunks. */
  66. unsigned long *bitmap;
  67. /*
  68. * List of all allocated chunks
  69. *
  70. * This is where things get 'clever'. Individual allocations from
  71. * dma_alloc_coherent must be allocated and freed in one chunk.
  72. * We don't just want to limit the allocations to those confined
  73. * within a single chunk (if clients allocate n small chunks we would
  74. * never be able to use the combined size). The bitmap allocator is
  75. * used to find the contiguous region and the parts of the chunks are
  76. * marked off as used. The chunks won't be freed in the shrinker until
  77. * the usage is actually zero.
  78. */
  79. struct list_head chunks;
  80. int npages;
  81. ion_phys_addr_t base;
  82. struct work_struct work;
  83. unsigned long last_alloc;
  84. struct shrinker shrinker;
  85. atomic_t total_allocated;
  86. atomic_t total_pool_size;
  87. unsigned long heap_size;
  88. unsigned long default_prefetch_size;
  89. };
  90. static void ion_secure_pool_pages(struct work_struct *work);
  91. /*
  92. * Create scatter-list for the already allocated DMA buffer.
  93. * This function could be replace by dma_common_get_sgtable
  94. * as soon as it will avalaible.
  95. */
  96. int ion_secure_cma_get_sgtable(struct device *dev, struct sg_table *sgt,
  97. dma_addr_t handle, size_t size)
  98. {
  99. struct page *page = phys_to_page(handle);
  100. int ret;
  101. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  102. if (unlikely(ret))
  103. return ret;
  104. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  105. sg_dma_address(sgt->sgl) = handle;
  106. return 0;
  107. }
  108. static int ion_secure_cma_add_to_pool(
  109. struct ion_cma_secure_heap *sheap,
  110. unsigned long len)
  111. {
  112. void *cpu_addr;
  113. dma_addr_t handle;
  114. DEFINE_DMA_ATTRS(attrs);
  115. int ret = 0;
  116. struct ion_cma_alloc_chunk *chunk;
  117. mutex_lock(&sheap->chunk_lock);
  118. chunk = kzalloc(sizeof(*chunk), GFP_KERNEL);
  119. if (!chunk) {
  120. ret = -ENOMEM;
  121. goto out;
  122. }
  123. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
  124. dma_set_attr(DMA_ATTR_SKIP_ZEROING, &attrs);
  125. cpu_addr = dma_alloc_attrs(sheap->dev, len, &handle, GFP_KERNEL,
  126. &attrs);
  127. if (!cpu_addr) {
  128. ret = -ENOMEM;
  129. goto out_free;
  130. }
  131. chunk->cpu_addr = cpu_addr;
  132. chunk->handle = handle;
  133. chunk->chunk_size = len;
  134. atomic_set(&chunk->cnt, 0);
  135. list_add(&chunk->entry, &sheap->chunks);
  136. atomic_add(len, &sheap->total_pool_size);
  137. /* clear the bitmap to indicate this region can be allocated from */
  138. bitmap_clear(sheap->bitmap, (handle - sheap->base) >> PAGE_SHIFT,
  139. len >> PAGE_SHIFT);
  140. goto out;
  141. out_free:
  142. kfree(chunk);
  143. out:
  144. mutex_unlock(&sheap->chunk_lock);
  145. return ret;
  146. }
  147. static void ion_secure_pool_pages(struct work_struct *work)
  148. {
  149. struct ion_cma_secure_heap *sheap = container_of(work,
  150. struct ion_cma_secure_heap, work);
  151. ion_secure_cma_add_to_pool(sheap, sheap->last_alloc);
  152. }
  153. /*
  154. * @s1: start of the first region
  155. * @l1: length of the first region
  156. * @s2: start of the second region
  157. * @l2: length of the second region
  158. *
  159. * Returns the total number of bytes that intersect.
  160. *
  161. * s1 is the region we are trying to clear so s2 may be subsumed by s1 but the
  162. * maximum size to clear should only ever be l1
  163. *
  164. */
  165. static unsigned int intersect(unsigned long s1, unsigned long l1,
  166. unsigned long s2, unsigned long l2)
  167. {
  168. unsigned long base1 = s1;
  169. unsigned long end1 = s1 + l1;
  170. unsigned long base2 = s2;
  171. unsigned long end2 = s2 + l2;
  172. /* Case 0: The regions don't overlap at all */
  173. if (!(base1 < end2 && base2 < end1))
  174. return 0;
  175. /* Case 1: region 2 is subsumed by region 1 */
  176. if (base1 <= base2 && end2 <= end1)
  177. return l2;
  178. /* case 2: region 1 is subsumed by region 2 */
  179. if (base2 <= base1 && end1 <= end2)
  180. return l1;
  181. /* case 3: region1 overlaps region2 on the bottom */
  182. if (base2 < end1 && base2 > base1)
  183. return end1 - base2;
  184. /* case 4: region 2 overlaps region1 on the bottom */
  185. if (base1 < end2 && base1 > base2)
  186. return end2 - base1;
  187. pr_err("Bad math! Did not detect chunks correctly! %lx %lx %lx %lx\n",
  188. s1, l1, s2, l2);
  189. BUG();
  190. }
  191. int ion_secure_cma_prefetch(struct ion_heap *heap, void *data)
  192. {
  193. unsigned long len = (unsigned long)data;
  194. struct ion_cma_secure_heap *sheap =
  195. container_of(heap, struct ion_cma_secure_heap, heap);
  196. unsigned long diff;
  197. if ((int) heap->type != ION_HEAP_TYPE_SECURE_DMA)
  198. return -EINVAL;
  199. if (len == 0)
  200. len = sheap->default_prefetch_size;
  201. /*
  202. * Only prefetch as much space as there is left in the pool so
  203. * check against the current free size of the heap.
  204. * This is slightly racy if someone else is allocating at the same
  205. * time. CMA has a restricted size for the heap so worst case
  206. * the prefetch doesn't work because the allocation fails.
  207. */
  208. diff = sheap->heap_size - atomic_read(&sheap->total_pool_size);
  209. if (len > diff)
  210. len = diff;
  211. sheap->last_alloc = len;
  212. schedule_work(&sheap->work);
  213. return 0;
  214. }
  215. static void bad_math_dump(unsigned long len, int total_overlap,
  216. struct ion_cma_secure_heap *sheap,
  217. bool alloc, dma_addr_t paddr)
  218. {
  219. struct list_head *entry;
  220. pr_err("Bad math! expected total was %lx actual was %x\n",
  221. len, total_overlap);
  222. pr_err("attempted %s address was %pa len %lx\n",
  223. alloc ? "allocation" : "free", &paddr, len);
  224. pr_err("chunks:\n");
  225. list_for_each(entry, &sheap->chunks) {
  226. struct ion_cma_alloc_chunk *chunk =
  227. container_of(entry,
  228. struct ion_cma_alloc_chunk, entry);
  229. pr_info("--- pa %pa len %lx\n",
  230. &chunk->handle, chunk->chunk_size);
  231. }
  232. BUG();
  233. }
  234. static int ion_secure_cma_alloc_from_pool(
  235. struct ion_cma_secure_heap *sheap,
  236. dma_addr_t *phys,
  237. unsigned long len)
  238. {
  239. dma_addr_t paddr;
  240. unsigned long page_no;
  241. int ret = 0;
  242. int total_overlap = 0;
  243. struct list_head *entry;
  244. mutex_lock(&sheap->chunk_lock);
  245. page_no = bitmap_find_next_zero_area(sheap->bitmap,
  246. sheap->npages, 0, len >> PAGE_SHIFT, 0);
  247. if (page_no >= sheap->npages) {
  248. ret = -ENOMEM;
  249. goto out;
  250. }
  251. bitmap_set(sheap->bitmap, page_no, len >> PAGE_SHIFT);
  252. paddr = sheap->base + (page_no << PAGE_SHIFT);
  253. list_for_each(entry, &sheap->chunks) {
  254. struct ion_cma_alloc_chunk *chunk = container_of(entry,
  255. struct ion_cma_alloc_chunk, entry);
  256. int overlap = intersect(chunk->handle,
  257. chunk->chunk_size, paddr, len);
  258. atomic_add(overlap, &chunk->cnt);
  259. total_overlap += overlap;
  260. }
  261. if (total_overlap != len)
  262. bad_math_dump(len, total_overlap, sheap, 1, paddr);
  263. *phys = paddr;
  264. out:
  265. mutex_unlock(&sheap->chunk_lock);
  266. return ret;
  267. }
  268. static void ion_secure_cma_free_chunk(struct ion_cma_secure_heap *sheap,
  269. struct ion_cma_alloc_chunk *chunk)
  270. {
  271. /* This region is 'allocated' and not available to allocate from */
  272. bitmap_set(sheap->bitmap, (chunk->handle - sheap->base) >> PAGE_SHIFT,
  273. chunk->chunk_size >> PAGE_SHIFT);
  274. dma_free_coherent(sheap->dev, chunk->chunk_size, chunk->cpu_addr,
  275. chunk->handle);
  276. atomic_sub(chunk->chunk_size, &sheap->total_pool_size);
  277. list_del(&chunk->entry);
  278. kfree(chunk);
  279. }
  280. int ion_secure_cma_drain_pool(struct ion_heap *heap, void *unused)
  281. {
  282. struct ion_cma_secure_heap *sheap =
  283. container_of(heap, struct ion_cma_secure_heap, heap);
  284. struct list_head *entry, *_n;
  285. mutex_lock(&sheap->chunk_lock);
  286. list_for_each_safe(entry, _n, &sheap->chunks) {
  287. struct ion_cma_alloc_chunk *chunk = container_of(entry,
  288. struct ion_cma_alloc_chunk, entry);
  289. if (atomic_read(&chunk->cnt) == 0)
  290. ion_secure_cma_free_chunk(sheap, chunk);
  291. }
  292. mutex_unlock(&sheap->chunk_lock);
  293. return 0;
  294. }
  295. static int ion_secure_cma_shrinker(struct shrinker *shrinker,
  296. struct shrink_control *sc)
  297. {
  298. struct ion_cma_secure_heap *sheap = container_of(shrinker,
  299. struct ion_cma_secure_heap, shrinker);
  300. int nr_to_scan = sc->nr_to_scan;
  301. struct list_head *entry, *_n;
  302. if (nr_to_scan == 0)
  303. return atomic_read(&sheap->total_pool_size);
  304. /*
  305. * CMA pages can only be used for movable allocation so don't free if
  306. * the allocation isn't movable
  307. */
  308. if (!(sc->gfp_mask & __GFP_MOVABLE))
  309. return atomic_read(&sheap->total_pool_size);
  310. /*
  311. * Allocation path may recursively call the shrinker. Don't shrink if
  312. * that happens.
  313. */
  314. if (!mutex_trylock(&sheap->chunk_lock))
  315. return -1;
  316. list_for_each_safe(entry, _n, &sheap->chunks) {
  317. struct ion_cma_alloc_chunk *chunk = container_of(entry,
  318. struct ion_cma_alloc_chunk, entry);
  319. if (nr_to_scan < 0)
  320. break;
  321. if (atomic_read(&chunk->cnt) == 0) {
  322. nr_to_scan -= chunk->chunk_size;
  323. ion_secure_cma_free_chunk(sheap, chunk);
  324. }
  325. }
  326. mutex_unlock(&sheap->chunk_lock);
  327. return atomic_read(&sheap->total_pool_size);
  328. }
  329. static void ion_secure_cma_free_from_pool(struct ion_cma_secure_heap *sheap,
  330. dma_addr_t handle,
  331. unsigned long len)
  332. {
  333. struct list_head *entry, *_n;
  334. int total_overlap = 0;
  335. mutex_lock(&sheap->chunk_lock);
  336. bitmap_clear(sheap->bitmap, (handle - sheap->base) >> PAGE_SHIFT,
  337. len >> PAGE_SHIFT);
  338. list_for_each_safe(entry, _n, &sheap->chunks) {
  339. struct ion_cma_alloc_chunk *chunk = container_of(entry,
  340. struct ion_cma_alloc_chunk, entry);
  341. int overlap = intersect(chunk->handle,
  342. chunk->chunk_size, handle, len);
  343. /*
  344. * Don't actually free this from the pool list yet, let either
  345. * an explicit drain call or the shrinkers take care of the
  346. * pool.
  347. */
  348. atomic_sub_return(overlap, &chunk->cnt);
  349. BUG_ON(atomic_read(&chunk->cnt) < 0);
  350. total_overlap += overlap;
  351. }
  352. BUG_ON(atomic_read(&sheap->total_pool_size) < 0);
  353. if (total_overlap != len)
  354. bad_math_dump(len, total_overlap, sheap, 0, handle);
  355. mutex_unlock(&sheap->chunk_lock);
  356. }
  357. /* ION CMA heap operations functions */
  358. static struct ion_secure_cma_buffer_info *__ion_secure_cma_allocate(
  359. struct ion_heap *heap, struct ion_buffer *buffer,
  360. unsigned long len, unsigned long align,
  361. unsigned long flags)
  362. {
  363. struct ion_cma_secure_heap *sheap =
  364. container_of(heap, struct ion_cma_secure_heap, heap);
  365. struct ion_secure_cma_buffer_info *info;
  366. int ret;
  367. dev_dbg(sheap->dev, "Request buffer allocation len %ld\n", len);
  368. info = kzalloc(sizeof(struct ion_secure_cma_buffer_info), GFP_KERNEL);
  369. if (!info) {
  370. dev_err(sheap->dev, "Can't allocate buffer info\n");
  371. return ION_CMA_ALLOCATE_FAILED;
  372. }
  373. mutex_lock(&sheap->alloc_lock);
  374. ret = ion_secure_cma_alloc_from_pool(sheap, &info->phys, len);
  375. if (ret) {
  376. retry:
  377. ret = ion_secure_cma_add_to_pool(sheap, len);
  378. if (ret) {
  379. mutex_unlock(&sheap->alloc_lock);
  380. dev_err(sheap->dev, "Fail to allocate buffer\n");
  381. goto err;
  382. }
  383. ret = ion_secure_cma_alloc_from_pool(sheap, &info->phys, len);
  384. if (ret) {
  385. /*
  386. * Lost the race with the shrinker, try again
  387. */
  388. goto retry;
  389. }
  390. }
  391. mutex_unlock(&sheap->alloc_lock);
  392. atomic_add(len, &sheap->total_allocated);
  393. info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  394. if (!info->table) {
  395. dev_err(sheap->dev, "Fail to allocate sg table\n");
  396. goto err;
  397. }
  398. ion_secure_cma_get_sgtable(sheap->dev,
  399. info->table, info->phys, len);
  400. info->secure.buffer = info->phys;
  401. /* keep this for memory release */
  402. buffer->priv_virt = info;
  403. dev_dbg(sheap->dev, "Allocate buffer %p\n", buffer);
  404. return info;
  405. err:
  406. kfree(info);
  407. return ION_CMA_ALLOCATE_FAILED;
  408. }
  409. static int ion_secure_cma_allocate(struct ion_heap *heap,
  410. struct ion_buffer *buffer,
  411. unsigned long len, unsigned long align,
  412. unsigned long flags)
  413. {
  414. unsigned long secure_allocation = flags & ION_FLAG_SECURE;
  415. struct ion_secure_cma_buffer_info *buf = NULL;
  416. if (!secure_allocation) {
  417. pr_err("%s: non-secure allocation disallowed from heap %s %lx\n",
  418. __func__, heap->name, flags);
  419. return -ENOMEM;
  420. }
  421. if (ION_IS_CACHED(flags)) {
  422. pr_err("%s: cannot allocate cached memory from secure heap %s\n",
  423. __func__, heap->name);
  424. return -ENOMEM;
  425. }
  426. buf = __ion_secure_cma_allocate(heap, buffer, len, align, flags);
  427. if (buf) {
  428. int ret;
  429. buf->secure.want_delayed_unsecure = 0;
  430. atomic_set(&buf->secure.secure_cnt, 0);
  431. mutex_init(&buf->secure.lock);
  432. buf->secure.is_secure = 1;
  433. buf->secure.ignore_check = true;
  434. /*
  435. * make sure the size is set before trying to secure
  436. */
  437. buffer->size = len;
  438. ret = ion_cp_secure_buffer(buffer, ION_CP_V2, 0, 0);
  439. if (ret) {
  440. /*
  441. * Don't treat the secure buffer failing here as an
  442. * error for backwards compatibility reasons. If
  443. * the secure fails, the map will also fail so there
  444. * is no security risk.
  445. */
  446. pr_debug("%s: failed to secure buffer\n", __func__);
  447. }
  448. return 0;
  449. } else {
  450. return -ENOMEM;
  451. }
  452. }
  453. static void ion_secure_cma_free(struct ion_buffer *buffer)
  454. {
  455. struct ion_cma_secure_heap *sheap =
  456. container_of(buffer->heap, struct ion_cma_secure_heap, heap);
  457. struct ion_secure_cma_buffer_info *info = buffer->priv_virt;
  458. dev_dbg(sheap->dev, "Release buffer %p\n", buffer);
  459. ion_cp_unsecure_buffer(buffer, 1);
  460. atomic_sub(buffer->size, &sheap->total_allocated);
  461. BUG_ON(atomic_read(&sheap->total_allocated) < 0);
  462. /* release memory */
  463. ion_secure_cma_free_from_pool(sheap, info->phys, buffer->size);
  464. /* release sg table */
  465. sg_free_table(info->table);
  466. kfree(info->table);
  467. kfree(info);
  468. }
  469. static int ion_secure_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer,
  470. ion_phys_addr_t *addr, size_t *len)
  471. {
  472. struct ion_cma_secure_heap *sheap =
  473. container_of(heap, struct ion_cma_secure_heap, heap);
  474. struct ion_secure_cma_buffer_info *info = buffer->priv_virt;
  475. dev_dbg(sheap->dev, "Return buffer %p physical address 0x%pa\n", buffer,
  476. &info->phys);
  477. *addr = info->phys;
  478. *len = buffer->size;
  479. return 0;
  480. }
  481. struct sg_table *ion_secure_cma_heap_map_dma(struct ion_heap *heap,
  482. struct ion_buffer *buffer)
  483. {
  484. struct ion_secure_cma_buffer_info *info = buffer->priv_virt;
  485. return info->table;
  486. }
  487. void ion_secure_cma_heap_unmap_dma(struct ion_heap *heap,
  488. struct ion_buffer *buffer)
  489. {
  490. return;
  491. }
  492. static int ion_secure_cma_mmap(struct ion_heap *mapper,
  493. struct ion_buffer *buffer,
  494. struct vm_area_struct *vma)
  495. {
  496. pr_info("%s: mmaping from secure heap %s disallowed\n",
  497. __func__, mapper->name);
  498. return -EINVAL;
  499. }
  500. static void *ion_secure_cma_map_kernel(struct ion_heap *heap,
  501. struct ion_buffer *buffer)
  502. {
  503. pr_info("%s: kernel mapping from secure heap %s disallowed\n",
  504. __func__, heap->name);
  505. return NULL;
  506. }
  507. static void ion_secure_cma_unmap_kernel(struct ion_heap *heap,
  508. struct ion_buffer *buffer)
  509. {
  510. return;
  511. }
  512. static int ion_secure_cma_print_debug(struct ion_heap *heap, struct seq_file *s,
  513. const struct list_head *mem_map)
  514. {
  515. struct ion_cma_secure_heap *sheap =
  516. container_of(heap, struct ion_cma_secure_heap, heap);
  517. if (mem_map) {
  518. struct mem_map_data *data;
  519. seq_printf(s, "\nMemory Map\n");
  520. seq_printf(s, "%16.s %14.s %14.s %14.s\n",
  521. "client", "start address", "end address",
  522. "size (hex)");
  523. list_for_each_entry(data, mem_map, node) {
  524. const char *client_name = "(null)";
  525. if (data->client_name)
  526. client_name = data->client_name;
  527. seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n",
  528. client_name, &data->addr,
  529. &data->addr_end,
  530. data->size, data->size);
  531. }
  532. }
  533. seq_printf(s, "Total allocated: %x\n",
  534. atomic_read(&sheap->total_allocated));
  535. seq_printf(s, "Total pool size: %x\n",
  536. atomic_read(&sheap->total_pool_size));
  537. return 0;
  538. }
  539. static struct ion_heap_ops ion_secure_cma_ops = {
  540. .allocate = ion_secure_cma_allocate,
  541. .free = ion_secure_cma_free,
  542. .map_dma = ion_secure_cma_heap_map_dma,
  543. .unmap_dma = ion_secure_cma_heap_unmap_dma,
  544. .phys = ion_secure_cma_phys,
  545. .map_user = ion_secure_cma_mmap,
  546. .map_kernel = ion_secure_cma_map_kernel,
  547. .unmap_kernel = ion_secure_cma_unmap_kernel,
  548. .print_debug = ion_secure_cma_print_debug,
  549. .secure_buffer = ion_cp_secure_buffer,
  550. .unsecure_buffer = ion_cp_unsecure_buffer,
  551. };
  552. struct ion_heap *ion_secure_cma_heap_create(struct ion_platform_heap *data)
  553. {
  554. struct ion_cma_secure_heap *sheap;
  555. int map_size = BITS_TO_LONGS(data->size >> PAGE_SHIFT) * sizeof(long);
  556. sheap = kzalloc(sizeof(*sheap), GFP_KERNEL);
  557. if (!sheap)
  558. return ERR_PTR(-ENOMEM);
  559. sheap->dev = data->priv;
  560. mutex_init(&sheap->chunk_lock);
  561. mutex_init(&sheap->alloc_lock);
  562. sheap->heap.ops = &ion_secure_cma_ops;
  563. sheap->heap.type = ION_HEAP_TYPE_SECURE_DMA;
  564. sheap->npages = data->size >> PAGE_SHIFT;
  565. sheap->base = data->base;
  566. sheap->heap_size = data->size;
  567. sheap->bitmap = kmalloc(map_size, GFP_KERNEL);
  568. INIT_LIST_HEAD(&sheap->chunks);
  569. INIT_WORK(&sheap->work, ion_secure_pool_pages);
  570. sheap->shrinker.seeks = DEFAULT_SEEKS;
  571. sheap->shrinker.batch = 0;
  572. sheap->shrinker.shrink = ion_secure_cma_shrinker;
  573. sheap->default_prefetch_size = sheap->heap_size;
  574. register_shrinker(&sheap->shrinker);
  575. if (!sheap->bitmap) {
  576. kfree(sheap);
  577. return ERR_PTR(-ENOMEM);
  578. }
  579. if (data->extra_data) {
  580. struct ion_cma_pdata *extra = data->extra_data;
  581. sheap->default_prefetch_size = extra->default_prefetch_size;
  582. }
  583. /*
  584. * we initially mark everything in the allocator as being free so that
  585. * allocations can come in later
  586. */
  587. bitmap_fill(sheap->bitmap, sheap->npages);
  588. return &sheap->heap;
  589. }
  590. void ion_secure_cma_heap_destroy(struct ion_heap *heap)
  591. {
  592. struct ion_cma_secure_heap *sheap =
  593. container_of(heap, struct ion_cma_secure_heap, heap);
  594. kfree(sheap);
  595. }