xref: /dflybsd-src/sys/dev/drm/include/linux/mm.h (revision 789731325bde747251c28a37e0a00ed4efb88c46)
133c8403bSFrançois Tigeot /*-
233c8403bSFrançois Tigeot  * Copyright (c) 2010 Isilon Systems, Inc.
333c8403bSFrançois Tigeot  * Copyright (c) 2010 iX Systems, Inc.
433c8403bSFrançois Tigeot  * Copyright (c) 2010 Panasas, Inc.
533c8403bSFrançois Tigeot  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
687d00696SMatthew Dillon  * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
79d1b0c59SFrançois Tigeot  * Copyright (c) 2015-2020 François Tigeot <ftigeot@wolfpond.org>
833c8403bSFrançois Tigeot  * All rights reserved.
933c8403bSFrançois Tigeot  *
1033c8403bSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
1133c8403bSFrançois Tigeot  * modification, are permitted provided that the following conditions
1233c8403bSFrançois Tigeot  * are met:
1333c8403bSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
1433c8403bSFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
1533c8403bSFrançois Tigeot  *    disclaimer.
1633c8403bSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
1733c8403bSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
1833c8403bSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
1933c8403bSFrançois Tigeot  *
2033c8403bSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2133c8403bSFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2233c8403bSFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2333c8403bSFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2433c8403bSFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2533c8403bSFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2633c8403bSFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2733c8403bSFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2833c8403bSFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2933c8403bSFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3033c8403bSFrançois Tigeot  */
3133c8403bSFrançois Tigeot #ifndef	_LINUX_MM_H_
3233c8403bSFrançois Tigeot #define	_LINUX_MM_H_
3333c8403bSFrançois Tigeot 
3433c8403bSFrançois Tigeot #include <linux/errno.h>
35d6aa1cc5SFrançois Tigeot 
36f0bba3d1SFrançois Tigeot #include <linux/mmdebug.h>
379db5a7ceSFrançois Tigeot #include <linux/gfp.h>
3819c468b4SFrançois Tigeot #include <linux/bug.h>
39d6aa1cc5SFrançois Tigeot #include <linux/list.h>
40f0bba3d1SFrançois Tigeot #include <linux/mmzone.h>
41d6aa1cc5SFrançois Tigeot #include <linux/rbtree.h>
42d6aa1cc5SFrançois Tigeot #include <linux/atomic.h>
43d6aa1cc5SFrançois Tigeot #include <linux/mm_types.h>
44d6aa1cc5SFrançois Tigeot #include <linux/err.h>
4580fbca37SFrançois Tigeot #include <linux/shrinker.h>
4633c8403bSFrançois Tigeot 
478b1a6a38SFrançois Tigeot #include <asm/page.h>
482cd2ed12SFrançois Tigeot #include <asm/pgtable.h>
49c6002f72SFrançois Tigeot #include <asm/processor.h>
508b1a6a38SFrançois Tigeot 
51f0bba3d1SFrançois Tigeot static inline struct page *
nth_page(struct page * page,int n)52f0bba3d1SFrançois Tigeot nth_page(struct page *page, int n)
53f4374057SFrançois Tigeot {
54f4374057SFrançois Tigeot 	return page + n;
55f4374057SFrançois Tigeot }
56f4374057SFrançois Tigeot 
5733c8403bSFrançois Tigeot #define PAGE_ALIGN(addr) round_page(addr)
5833c8403bSFrançois Tigeot 
595f38e86dSFrançois Tigeot #define VM_FAULT_RETRY		0x0400
605f38e86dSFrançois Tigeot 
615f38e86dSFrançois Tigeot #define FAULT_FLAG_ALLOW_RETRY		0x04
625f38e86dSFrançois Tigeot #define FAULT_FLAG_RETRY_NOWAIT		0x08
635f38e86dSFrançois Tigeot 
64a34b4168SMatthew Dillon struct vm_fault {
653f2dd94aSFrançois Tigeot 	struct vm_area_struct *vma;
665f38e86dSFrançois Tigeot 	unsigned int flags;
675f38e86dSFrançois Tigeot 	void __user *virtual_address;
68a34b4168SMatthew Dillon };
69a34b4168SMatthew Dillon 
70a34b4168SMatthew Dillon #define VM_FAULT_NOPAGE		0x0001
71a34b4168SMatthew Dillon #define VM_FAULT_SIGBUS		0x0002
72a34b4168SMatthew Dillon #define VM_FAULT_OOM		0x0004
73a34b4168SMatthew Dillon 
74a34b4168SMatthew Dillon #define VM_DONTDUMP	0x0001
75a34b4168SMatthew Dillon #define VM_DONTEXPAND	0x0002
76a34b4168SMatthew Dillon #define VM_IO		0x0004
77a34b4168SMatthew Dillon #define VM_MIXEDMAP	0x0008
78a34b4168SMatthew Dillon 
79a34b4168SMatthew Dillon struct vm_operations_struct {
80a34b4168SMatthew Dillon 	int (*fault)(struct vm_area_struct *vma, struct vm_fault *vmf);
81a34b4168SMatthew Dillon 	void (*open)(struct vm_area_struct *vma);
82a34b4168SMatthew Dillon 	void (*close)(struct vm_area_struct *vma);
833f2dd94aSFrançois Tigeot 	int (*access)(struct vm_area_struct *vma, unsigned long addr,
843f2dd94aSFrançois Tigeot 		      void *buf, int len, int write);
8533c8403bSFrançois Tigeot };
8633c8403bSFrançois Tigeot 
8733c8403bSFrançois Tigeot /*
8833c8403bSFrançois Tigeot  * Compute log2 of the power of two rounded up count of pages
8933c8403bSFrançois Tigeot  * needed for size bytes.
9033c8403bSFrançois Tigeot  */
9133c8403bSFrançois Tigeot static inline int
get_order(unsigned long size)9233c8403bSFrançois Tigeot get_order(unsigned long size)
9333c8403bSFrançois Tigeot {
9433c8403bSFrançois Tigeot 	int order;
9533c8403bSFrançois Tigeot 
9633c8403bSFrançois Tigeot 	size = (size - 1) >> PAGE_SHIFT;
9733c8403bSFrançois Tigeot 	order = 0;
9833c8403bSFrançois Tigeot 	while (size) {
9933c8403bSFrançois Tigeot 		order++;
10033c8403bSFrançois Tigeot 		size >>= 1;
10133c8403bSFrançois Tigeot 	}
10233c8403bSFrançois Tigeot 	return (order);
10333c8403bSFrançois Tigeot }
10433c8403bSFrançois Tigeot 
10533c8403bSFrançois Tigeot /*
10633c8403bSFrançois Tigeot  * This only works via mmap ops.
10733c8403bSFrançois Tigeot  */
10833c8403bSFrançois Tigeot static inline int
io_remap_pfn_range(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn,unsigned long size,vm_memattr_t prot)10933c8403bSFrançois Tigeot io_remap_pfn_range(struct vm_area_struct *vma,
11033c8403bSFrançois Tigeot     unsigned long addr, unsigned long pfn, unsigned long size,
11133c8403bSFrançois Tigeot     vm_memattr_t prot)
11233c8403bSFrançois Tigeot {
11333c8403bSFrançois Tigeot 	vma->vm_page_prot = prot;
11433c8403bSFrançois Tigeot 	vma->vm_pfn = pfn;
11533c8403bSFrançois Tigeot 
11633c8403bSFrançois Tigeot 	return (0);
11733c8403bSFrançois Tigeot }
11833c8403bSFrançois Tigeot 
119db74a0c1SFrançois Tigeot static inline unsigned long
vma_pages(struct vm_area_struct * vma)120db74a0c1SFrançois Tigeot vma_pages(struct vm_area_struct *vma)
121db74a0c1SFrançois Tigeot {
122db74a0c1SFrançois Tigeot 	unsigned long size;
123db74a0c1SFrançois Tigeot 
124db74a0c1SFrançois Tigeot 	size = vma->vm_end - vma->vm_start;
125db74a0c1SFrançois Tigeot 
126db74a0c1SFrançois Tigeot 	return size >> PAGE_SHIFT;
127db74a0c1SFrançois Tigeot }
128db74a0c1SFrançois Tigeot 
129e2a4a6b1SFrançois Tigeot #define offset_in_page(off)	((unsigned long)(off) & PAGE_MASK)
130d0f32dccSFrançois Tigeot 
131612428b2SFrançois Tigeot static inline void
set_page_dirty(struct page * page)132f0bba3d1SFrançois Tigeot set_page_dirty(struct page *page)
133612428b2SFrançois Tigeot {
134f0bba3d1SFrançois Tigeot 	vm_page_dirty((struct vm_page *)page);
135612428b2SFrançois Tigeot }
136612428b2SFrançois Tigeot 
137326e34b8SFrançois Tigeot static inline void
get_page(struct vm_page * page)138326e34b8SFrançois Tigeot get_page(struct vm_page *page)
139326e34b8SFrançois Tigeot {
140326e34b8SFrançois Tigeot 	vm_page_hold(page);
141326e34b8SFrançois Tigeot }
142326e34b8SFrançois Tigeot 
14386e5f7fcSFrançois Tigeot extern vm_paddr_t Realmem;
14486e5f7fcSFrançois Tigeot 
get_num_physpages(void)14586e5f7fcSFrançois Tigeot static inline unsigned long get_num_physpages(void)
14686e5f7fcSFrançois Tigeot {
14786e5f7fcSFrançois Tigeot 	return Realmem / PAGE_SIZE;
14886e5f7fcSFrançois Tigeot }
14986e5f7fcSFrançois Tigeot 
150646a8671SFrançois Tigeot int is_vmalloc_addr(const void *x);
151646a8671SFrançois Tigeot 
1523306aed3SFrançois Tigeot static inline void
unmap_mapping_range(struct address_space * mapping,loff_t const holebegin,loff_t const holelen,int even_cows)1533306aed3SFrançois Tigeot unmap_mapping_range(struct address_space *mapping,
1543306aed3SFrançois Tigeot 	loff_t const holebegin, loff_t const holelen, int even_cows)
1553306aed3SFrançois Tigeot {
1563306aed3SFrançois Tigeot }
1573306aed3SFrançois Tigeot 
15843e748b9SFrançois Tigeot #define VM_SHARED	0x00000008
15943e748b9SFrançois Tigeot 
16043e748b9SFrançois Tigeot #define VM_PFNMAP	0x00000400
16143e748b9SFrançois Tigeot 
162f56a1a92SFrançois Tigeot static inline struct page *
vmalloc_to_page(const void * addr)163f56a1a92SFrançois Tigeot vmalloc_to_page(const void *addr)
164f56a1a92SFrançois Tigeot {
165f56a1a92SFrançois Tigeot 	vm_paddr_t paddr;
166f56a1a92SFrançois Tigeot 
167f56a1a92SFrançois Tigeot 	paddr = pmap_kextract((vm_offset_t)addr);
168f56a1a92SFrançois Tigeot 	return (struct page *)(PHYS_TO_VM_PAGE(paddr));
169f56a1a92SFrançois Tigeot }
170f56a1a92SFrançois Tigeot 
1712e7b1889SFrançois Tigeot static inline void
put_page(struct page * page)1722e7b1889SFrançois Tigeot put_page(struct page *page)
1732e7b1889SFrançois Tigeot {
1742e7b1889SFrançois Tigeot 	vm_page_busy_wait((struct vm_page *)page, FALSE, "i915gem");
1752e7b1889SFrançois Tigeot 	vm_page_unwire((struct vm_page *)page, 1);
1762e7b1889SFrançois Tigeot 	vm_page_wakeup((struct vm_page *)page);
1772e7b1889SFrançois Tigeot }
1782e7b1889SFrançois Tigeot 
179e2a4a6b1SFrançois Tigeot static inline void *
page_address(const struct page * page)180e2a4a6b1SFrançois Tigeot page_address(const struct page *page)
181e2a4a6b1SFrançois Tigeot {
182e2a4a6b1SFrançois Tigeot 	return (void *)VM_PAGE_TO_PHYS((const struct vm_page *)page);
183e2a4a6b1SFrançois Tigeot }
184e2a4a6b1SFrançois Tigeot 
1852cecdd68SFrançois Tigeot void * kvmalloc_array(size_t n, size_t size, gfp_t flags);
1862cecdd68SFrançois Tigeot 
1872cecdd68SFrançois Tigeot #define kvfree(addr)	kfree(addr)
1882cecdd68SFrançois Tigeot 
1893f2dd94aSFrançois Tigeot #define FOLL_WRITE	0x01
1903f2dd94aSFrançois Tigeot 
191*78973132SSergey Zigachev struct sysinfo {
192*78973132SSergey Zigachev 	uint64_t totalram;
193*78973132SSergey Zigachev 	uint64_t totalhigh;
194*78973132SSergey Zigachev 	uint32_t mem_unit;
195*78973132SSergey Zigachev };
196*78973132SSergey Zigachev 
197*78973132SSergey Zigachev void si_meminfo(struct sysinfo *si);
198*78973132SSergey Zigachev 
199*78973132SSergey Zigachev long get_user_pages(unsigned long start, unsigned long nr_pages,
200*78973132SSergey Zigachev 		    unsigned int gup_flags, struct page **pages,
201*78973132SSergey Zigachev 		    struct vm_area_struct **vmas);
202*78973132SSergey Zigachev 
203*78973132SSergey Zigachev void release_pages(struct page **pages, unsigned long nr_pages);
204*78973132SSergey Zigachev 
20533c8403bSFrançois Tigeot #endif	/* _LINUX_MM_H_ */
206