Back to home page

OSCL-LXR

 
 

    


0001 #ifndef __DRM_VMA_MANAGER_H__
0002 #define __DRM_VMA_MANAGER_H__
0003 
0004 /*
0005  * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
0006  *
0007  * Permission is hereby granted, free of charge, to any person obtaining a
0008  * copy of this software and associated documentation files (the "Software"),
0009  * to deal in the Software without restriction, including without limitation
0010  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0011  * and/or sell copies of the Software, and to permit persons to whom the
0012  * Software is furnished to do so, subject to the following conditions:
0013  *
0014  * The above copyright notice and this permission notice shall be included in
0015  * all copies or substantial portions of the Software.
0016  *
0017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0020  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0021  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0022  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 
0026 #include <drm/drm_mm.h>
0027 #include <linux/mm.h>
0028 #include <linux/rbtree.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/types.h>
0031 
0032 /* We make up offsets for buffer objects so we can recognize them at
0033  * mmap time. pgoff in mmap is an unsigned long, so we need to make sure
0034  * that the faked up offset will fit
0035  */
0036 #if BITS_PER_LONG == 64
0037 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
0038 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 256)
0039 #else
0040 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
0041 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
0042 #endif
0043 
0044 struct drm_file;
0045 
0046 struct drm_vma_offset_file {
0047     struct rb_node vm_rb;
0048     struct drm_file *vm_tag;
0049     unsigned long vm_count;
0050 };
0051 
0052 struct drm_vma_offset_node {
0053     rwlock_t vm_lock;
0054     struct drm_mm_node vm_node;
0055     struct rb_root vm_files;
0056     void *driver_private;
0057 };
0058 
0059 struct drm_vma_offset_manager {
0060     rwlock_t vm_lock;
0061     struct drm_mm vm_addr_space_mm;
0062 };
0063 
0064 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
0065                  unsigned long page_offset, unsigned long size);
0066 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
0067 
0068 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
0069                                unsigned long start,
0070                                unsigned long pages);
0071 int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
0072                struct drm_vma_offset_node *node, unsigned long pages);
0073 void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
0074                struct drm_vma_offset_node *node);
0075 
0076 int drm_vma_node_allow(struct drm_vma_offset_node *node, struct drm_file *tag);
0077 void drm_vma_node_revoke(struct drm_vma_offset_node *node,
0078              struct drm_file *tag);
0079 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
0080                  struct drm_file *tag);
0081 
0082 /**
0083  * drm_vma_offset_exact_lookup_locked() - Look up node by exact address
0084  * @mgr: Manager object
0085  * @start: Start address (page-based, not byte-based)
0086  * @pages: Size of object (page-based)
0087  *
0088  * Same as drm_vma_offset_lookup_locked() but does not allow any offset into the node.
0089  * It only returns the exact object with the given start address.
0090  *
0091  * RETURNS:
0092  * Node at exact start address @start.
0093  */
0094 static inline struct drm_vma_offset_node *
0095 drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *mgr,
0096                    unsigned long start,
0097                    unsigned long pages)
0098 {
0099     struct drm_vma_offset_node *node;
0100 
0101     node = drm_vma_offset_lookup_locked(mgr, start, pages);
0102     return (node && node->vm_node.start == start) ? node : NULL;
0103 }
0104 
0105 /**
0106  * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
0107  * @mgr: Manager object
0108  *
0109  * Lock VMA manager for extended lookups. Only locked VMA function calls
0110  * are allowed while holding this lock. All other contexts are blocked from VMA
0111  * until the lock is released via drm_vma_offset_unlock_lookup().
0112  *
0113  * Use this if you need to take a reference to the objects returned by
0114  * drm_vma_offset_lookup_locked() before releasing this lock again.
0115  *
0116  * This lock must not be used for anything else than extended lookups. You must
0117  * not call any other VMA helpers while holding this lock.
0118  *
0119  * Note: You're in atomic-context while holding this lock!
0120  */
0121 static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
0122 {
0123     read_lock(&mgr->vm_lock);
0124 }
0125 
0126 /**
0127  * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
0128  * @mgr: Manager object
0129  *
0130  * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
0131  */
0132 static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
0133 {
0134     read_unlock(&mgr->vm_lock);
0135 }
0136 
0137 /**
0138  * drm_vma_node_reset() - Initialize or reset node object
0139  * @node: Node to initialize or reset
0140  *
0141  * Reset a node to its initial state. This must be called before using it with
0142  * any VMA offset manager.
0143  *
0144  * This must not be called on an already allocated node, or you will leak
0145  * memory.
0146  */
0147 static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
0148 {
0149     memset(node, 0, sizeof(*node));
0150     node->vm_files = RB_ROOT;
0151     rwlock_init(&node->vm_lock);
0152 }
0153 
0154 /**
0155  * drm_vma_node_start() - Return start address for page-based addressing
0156  * @node: Node to inspect
0157  *
0158  * Return the start address of the given node. This can be used as offset into
0159  * the linear VM space that is provided by the VMA offset manager. Note that
0160  * this can only be used for page-based addressing. If you need a proper offset
0161  * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
0162  * drm_vma_node_offset_addr() helper instead.
0163  *
0164  * RETURNS:
0165  * Start address of @node for page-based addressing. 0 if the node does not
0166  * have an offset allocated.
0167  */
0168 static inline unsigned long drm_vma_node_start(const struct drm_vma_offset_node *node)
0169 {
0170     return node->vm_node.start;
0171 }
0172 
0173 /**
0174  * drm_vma_node_size() - Return size (page-based)
0175  * @node: Node to inspect
0176  *
0177  * Return the size as number of pages for the given node. This is the same size
0178  * that was passed to drm_vma_offset_add(). If no offset is allocated for the
0179  * node, this is 0.
0180  *
0181  * RETURNS:
0182  * Size of @node as number of pages. 0 if the node does not have an offset
0183  * allocated.
0184  */
0185 static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
0186 {
0187     return node->vm_node.size;
0188 }
0189 
0190 /**
0191  * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
0192  * @node: Linked offset node
0193  *
0194  * Same as drm_vma_node_start() but returns the address as a valid offset that
0195  * can be used for user-space mappings during mmap().
0196  * This must not be called on unlinked nodes.
0197  *
0198  * RETURNS:
0199  * Offset of @node for byte-based addressing. 0 if the node does not have an
0200  * object allocated.
0201  */
0202 static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
0203 {
0204     return ((__u64)node->vm_node.start) << PAGE_SHIFT;
0205 }
0206 
0207 /**
0208  * drm_vma_node_unmap() - Unmap offset node
0209  * @node: Offset node
0210  * @file_mapping: Address space to unmap @node from
0211  *
0212  * Unmap all userspace mappings for a given offset node. The mappings must be
0213  * associated with the @file_mapping address-space. If no offset exists
0214  * nothing is done.
0215  *
0216  * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
0217  * is not called on this node concurrently.
0218  */
0219 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
0220                       struct address_space *file_mapping)
0221 {
0222     if (drm_mm_node_allocated(&node->vm_node))
0223         unmap_mapping_range(file_mapping,
0224                     drm_vma_node_offset_addr(node),
0225                     drm_vma_node_size(node) << PAGE_SHIFT, 1);
0226 }
0227 
0228 /**
0229  * drm_vma_node_verify_access() - Access verification helper for TTM
0230  * @node: Offset node
0231  * @tag: Tag of file to check
0232  *
0233  * This checks whether @tag is granted access to @node. It is the same as
0234  * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
0235  * verify_access() callbacks.
0236  *
0237  * RETURNS:
0238  * 0 if access is granted, -EACCES otherwise.
0239  */
0240 static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
0241                          struct drm_file *tag)
0242 {
0243     return drm_vma_node_is_allowed(node, tag) ? 0 : -EACCES;
0244 }
0245 
0246 #endif /* __DRM_VMA_MANAGER_H__ */