1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (c) 2024, Google LLC. 4 * Pasha Tatashin <pasha.tatashin@soleen.com> 5 */ 6 7 #ifndef __IOMMU_PAGES_H 8 #define __IOMMU_PAGES_H 9 10 #include <linux/iommu.h> 11 12 /** 13 * struct ioptdesc - Memory descriptor for IOMMU page tables 14 * @iopt_freelist_elm: List element for a struct iommu_pages_list 15 * 16 * This struct overlays struct page for now. Do not modify without a good 17 * understanding of the issues. 18 */ 19 struct ioptdesc { 20 unsigned long __page_flags; 21 22 struct list_head iopt_freelist_elm; 23 unsigned long __page_mapping; 24 pgoff_t __index; 25 void *_private; 26 27 unsigned int __page_type; 28 atomic_t __page_refcount; 29 #ifdef CONFIG_MEMCG 30 unsigned long memcg_data; 31 #endif 32 }; 33 34 static inline struct ioptdesc *folio_ioptdesc(struct folio *folio) 35 { 36 return (struct ioptdesc *)folio; 37 } 38 39 static inline struct folio *ioptdesc_folio(struct ioptdesc *iopt) 40 { 41 return (struct folio *)iopt; 42 } 43 44 static inline struct ioptdesc *virt_to_ioptdesc(void *virt) 45 { 46 return folio_ioptdesc(virt_to_folio(virt)); 47 } 48 49 void *iommu_alloc_pages_node_sz(int nid, gfp_t gfp, size_t size); 50 void iommu_free_pages(void *virt); 51 void iommu_put_pages_list(struct iommu_pages_list *list); 52 53 /** 54 * iommu_pages_list_add - add the page to a iommu_pages_list 55 * @list: List to add the page to 56 * @virt: Address returned from iommu_alloc_pages_node_sz() 57 */ 58 static inline void iommu_pages_list_add(struct iommu_pages_list *list, 59 void *virt) 60 { 61 list_add_tail(&virt_to_ioptdesc(virt)->iopt_freelist_elm, &list->pages); 62 } 63 64 /** 65 * iommu_pages_list_splice - Put all the pages in list from into list to 66 * @from: Source list of pages 67 * @to: Destination list of pages 68 * 69 * from must be re-initialized after calling this function if it is to be 70 * used again. 71 */ 72 static inline void iommu_pages_list_splice(struct iommu_pages_list *from, 73 struct iommu_pages_list *to) 74 { 75 list_splice(&from->pages, &to->pages); 76 } 77 78 /** 79 * iommu_pages_list_empty - True if the list is empty 80 * @list: List to check 81 */ 82 static inline bool iommu_pages_list_empty(struct iommu_pages_list *list) 83 { 84 return list_empty(&list->pages); 85 } 86 87 /** 88 * iommu_alloc_pages_sz - Allocate a zeroed page of a given size from 89 * specific NUMA node 90 * @nid: memory NUMA node id 91 * @gfp: buddy allocator flags 92 * @size: Memory size to allocate, this is rounded up to a power of 2 93 * 94 * Returns the virtual address of the allocated page. 95 */ 96 static inline void *iommu_alloc_pages_sz(gfp_t gfp, size_t size) 97 { 98 return iommu_alloc_pages_node_sz(NUMA_NO_NODE, gfp, size); 99 } 100 101 #endif /* __IOMMU_PAGES_H */ 102