• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Allocator abstraction for the hypervisor.
4  * Copyright (C) 2023 Google LLC
5  * Author: Mostafa Saleh <smostafa@google.com>
6  */
7 
8 #include <nvhe/alloc.h>
9 #include <nvhe/alloc_mgt.h>
10 #include <nvhe/iommu.h>
11 
12 static struct hyp_mgt_allocator_ops *registered_allocators[] = {
13 	[HYP_ALLOC_MGT_HEAP_ID] = &hyp_alloc_ops,
14 	[HYP_ALLOC_MGT_IOMMU_ID] = &kvm_iommu_allocator_ops,
15 };
16 
17 #define MAX_ALLOC_ID		(ARRAY_SIZE(registered_allocators))
18 
hyp_alloc_mgt_refill(unsigned long id,struct kvm_hyp_memcache * host_mc)19 int hyp_alloc_mgt_refill(unsigned long id, struct kvm_hyp_memcache *host_mc)
20 {
21 	struct hyp_mgt_allocator_ops *ops;
22 
23 	if (id > MAX_ALLOC_ID)
24 		return -EINVAL;
25 
26 	id = array_index_nospec(id, MAX_ALLOC_ID);
27 
28 	ops = registered_allocators[id];
29 
30 	return ops->refill ? ops->refill(host_mc) : 0;
31 }
32 
hyp_alloc_mgt_reclaimable(void)33 int hyp_alloc_mgt_reclaimable(void)
34 {
35 	struct hyp_mgt_allocator_ops *ops;
36 	int reclaimable = 0;
37 	int i;
38 
39 	for (i = 0 ; i < MAX_ALLOC_ID ; ++i) {
40 		ops = registered_allocators[i];
41 		if (ops->reclaimable)
42 			reclaimable += ops->reclaimable();
43 	}
44 	return reclaimable;
45 }
46 
hyp_alloc_mgt_reclaim(struct kvm_hyp_memcache * host_mc,int target)47 void hyp_alloc_mgt_reclaim(struct kvm_hyp_memcache *host_mc, int target)
48 {
49 	struct hyp_mgt_allocator_ops *ops;
50 	int i;
51 
52 	for (i = 0 ; (i < MAX_ALLOC_ID) && (host_mc->nr_pages < target) ; ++i) {
53 		ops = registered_allocators[i];
54 		/* Not fair but OK for now. */
55 		if (ops->reclaim)
56 			ops->reclaim(host_mc, target);
57 	}
58 }
59