1 1.7 riastrad /* $NetBSD: drm_vma_manager.h,v 1.7 2021/12/19 11:57:27 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2014 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.1 riastrad #ifndef _DRM_DRM_VMA_MANAGER_H_ 33 1.1 riastrad #define _DRM_DRM_VMA_MANAGER_H_ 34 1.1 riastrad 35 1.1 riastrad #include <sys/types.h> 36 1.1 riastrad #include <sys/param.h> 37 1.1 riastrad #include <sys/rbtree.h> 38 1.1 riastrad #include <sys/rwlock.h> 39 1.1 riastrad #include <sys/vmem.h> 40 1.1 riastrad 41 1.5 riastrad #include <linux/mm.h> 42 1.5 riastrad 43 1.6 riastrad #if BITS_PER_LONG == 64 44 1.6 riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) 45 1.6 riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 256) 46 1.6 riastrad #else 47 1.6 riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) 48 1.6 riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) 49 1.6 riastrad #endif 50 1.6 riastrad 51 1.2 riastrad struct drm_file; 52 1.2 riastrad 53 1.1 riastrad struct drm_vma_offset_manager { 54 1.1 riastrad krwlock_t vom_lock; 55 1.1 riastrad struct rb_tree vom_nodes; 56 1.1 riastrad struct vmem *vom_vmem; 57 1.1 riastrad }; 58 1.1 riastrad 59 1.1 riastrad struct drm_vma_offset_node { 60 1.1 riastrad krwlock_t von_lock; 61 1.1 riastrad vmem_addr_t von_startpage; 62 1.1 riastrad vmem_size_t von_npages; 63 1.1 riastrad struct rb_tree von_files; 64 1.1 riastrad struct rb_node von_rb_node; 65 1.4 riastrad 66 1.4 riastrad /* Linux API names */ 67 1.4 riastrad bool readonly; 68 1.1 riastrad }; 69 1.1 riastrad 70 1.1 riastrad static inline unsigned long 71 1.3 riastrad drm_vma_node_start(const struct drm_vma_offset_node *node) 72 1.1 riastrad { 73 1.1 riastrad return node->von_startpage; 74 1.1 riastrad } 75 1.1 riastrad 76 1.1 riastrad static inline unsigned long 77 1.3 riastrad drm_vma_node_size(const struct drm_vma_offset_node *node) 78 1.1 riastrad { 79 1.1 riastrad return node->von_npages; 80 1.1 riastrad } 81 1.1 riastrad 82 1.1 riastrad static inline bool 83 1.3 riastrad drm_vma_node_has_offset(const struct drm_vma_offset_node *node) 84 1.1 riastrad { 85 1.1 riastrad return (node->von_npages != 0); 86 1.1 riastrad } 87 1.1 riastrad 88 1.1 riastrad static inline uint64_t 89 1.3 riastrad drm_vma_node_offset_addr(const struct drm_vma_offset_node *node) 90 1.1 riastrad { 91 1.1 riastrad return (uint64_t)node->von_startpage << PAGE_SHIFT; 92 1.1 riastrad } 93 1.1 riastrad 94 1.1 riastrad struct drm_vma_offset_file { 95 1.2 riastrad struct drm_file *vof_file; 96 1.1 riastrad struct rb_node vof_rb_node; 97 1.1 riastrad }; 98 1.1 riastrad 99 1.1 riastrad void drm_vma_offset_manager_init(struct drm_vma_offset_manager *, 100 1.1 riastrad unsigned long, unsigned long); 101 1.1 riastrad void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *); 102 1.1 riastrad 103 1.1 riastrad /* 104 1.1 riastrad * This is called drm_vma_node_reset upstream. Name is changed so you 105 1.1 riastrad * have to find calls and match them with drm_vma_node_destroy. 106 1.1 riastrad */ 107 1.1 riastrad void drm_vma_node_init(struct drm_vma_offset_node *); 108 1.1 riastrad 109 1.1 riastrad /* This does not exist upstream. */ 110 1.1 riastrad void drm_vma_node_destroy(struct drm_vma_offset_node *); 111 1.1 riastrad 112 1.1 riastrad int drm_vma_offset_add(struct drm_vma_offset_manager *, 113 1.1 riastrad struct drm_vma_offset_node *, unsigned long); 114 1.1 riastrad void drm_vma_offset_remove(struct drm_vma_offset_manager *, 115 1.1 riastrad struct drm_vma_offset_node *); 116 1.1 riastrad 117 1.1 riastrad void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *); 118 1.1 riastrad void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *); 119 1.1 riastrad struct drm_vma_offset_node * 120 1.1 riastrad drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *, 121 1.1 riastrad unsigned long, unsigned long); 122 1.1 riastrad struct drm_vma_offset_node * 123 1.1 riastrad drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *, 124 1.1 riastrad unsigned long, unsigned long); 125 1.7 riastrad struct drm_vma_offset_node * 126 1.7 riastrad drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *, 127 1.7 riastrad unsigned long, unsigned long); 128 1.1 riastrad 129 1.2 riastrad int drm_vma_node_allow(struct drm_vma_offset_node *, struct drm_file *); 130 1.2 riastrad void drm_vma_node_revoke(struct drm_vma_offset_node *, struct drm_file *); 131 1.2 riastrad bool drm_vma_node_is_allowed(struct drm_vma_offset_node *, 132 1.2 riastrad struct drm_file *); 133 1.1 riastrad int drm_vma_node_verify_access(struct drm_vma_offset_node *, 134 1.2 riastrad struct drm_file *); 135 1.1 riastrad 136 1.1 riastrad #endif /* _DRM_DRM_VMA_MANAGER_H_ */ 137