xref: /OK3568_Linux_fs/kernel/include/drm/ttm/ttm_memory.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /**************************************************************************
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
7*4882a593Smuzhiyun  * copy of this software and associated documentation files (the
8*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun  * without limitation the rights to use, copy, modify, merge, publish,
10*4882a593Smuzhiyun  * distribute, sub license, and/or sell copies of the Software, and to
11*4882a593Smuzhiyun  * permit persons to whom the Software is furnished to do so, subject to
12*4882a593Smuzhiyun  * the following conditions:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial portions
16*4882a593Smuzhiyun  * of the Software.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*4882a593Smuzhiyun  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*4882a593Smuzhiyun  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*4882a593Smuzhiyun  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  **************************************************************************/
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #ifndef TTM_MEMORY_H
29*4882a593Smuzhiyun #define TTM_MEMORY_H
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <linux/workqueue.h>
32*4882a593Smuzhiyun #include <linux/spinlock.h>
33*4882a593Smuzhiyun #include <linux/bug.h>
34*4882a593Smuzhiyun #include <linux/wait.h>
35*4882a593Smuzhiyun #include <linux/errno.h>
36*4882a593Smuzhiyun #include <linux/kobject.h>
37*4882a593Smuzhiyun #include <linux/mm.h>
38*4882a593Smuzhiyun #include "ttm_bo_api.h"
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun  * struct ttm_mem_global - Global memory accounting structure.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * @shrink: A single callback to shrink TTM memory usage. Extend this
44*4882a593Smuzhiyun  * to a linked list to be able to handle multiple callbacks when needed.
45*4882a593Smuzhiyun  * @swap_queue: A workqueue to handle shrinking in low memory situations. We
46*4882a593Smuzhiyun  * need a separate workqueue since it will spend a lot of time waiting
47*4882a593Smuzhiyun  * for the GPU, and this will otherwise block other workqueue tasks(?)
48*4882a593Smuzhiyun  * At this point we use only a single-threaded workqueue.
49*4882a593Smuzhiyun  * @work: The workqueue callback for the shrink queue.
50*4882a593Smuzhiyun  * @lock: Lock to protect the @shrink - and the memory accounting members,
51*4882a593Smuzhiyun  * that is, essentially the whole structure with some exceptions.
52*4882a593Smuzhiyun  * @lower_mem_limit: include lower limit of swap space and lower limit of
53*4882a593Smuzhiyun  * system memory.
54*4882a593Smuzhiyun  * @zones: Array of pointers to accounting zones.
55*4882a593Smuzhiyun  * @num_zones: Number of populated entries in the @zones array.
56*4882a593Smuzhiyun  * @zone_kernel: Pointer to the kernel zone.
57*4882a593Smuzhiyun  * @zone_highmem: Pointer to the highmem zone if there is one.
58*4882a593Smuzhiyun  * @zone_dma32: Pointer to the dma32 zone if there is one.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Note that this structure is not per device. It should be global for all
61*4882a593Smuzhiyun  * graphics devices.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #define TTM_MEM_MAX_ZONES 2
65*4882a593Smuzhiyun struct ttm_mem_zone;
66*4882a593Smuzhiyun extern struct ttm_mem_global {
67*4882a593Smuzhiyun 	struct kobject kobj;
68*4882a593Smuzhiyun 	struct workqueue_struct *swap_queue;
69*4882a593Smuzhiyun 	struct work_struct work;
70*4882a593Smuzhiyun 	spinlock_t lock;
71*4882a593Smuzhiyun 	uint64_t lower_mem_limit;
72*4882a593Smuzhiyun 	struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
73*4882a593Smuzhiyun 	unsigned int num_zones;
74*4882a593Smuzhiyun 	struct ttm_mem_zone *zone_kernel;
75*4882a593Smuzhiyun #ifdef CONFIG_HIGHMEM
76*4882a593Smuzhiyun 	struct ttm_mem_zone *zone_highmem;
77*4882a593Smuzhiyun #else
78*4882a593Smuzhiyun 	struct ttm_mem_zone *zone_dma32;
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun } ttm_mem_glob;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun int ttm_mem_global_init(struct ttm_mem_global *glob);
83*4882a593Smuzhiyun void ttm_mem_global_release(struct ttm_mem_global *glob);
84*4882a593Smuzhiyun int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
85*4882a593Smuzhiyun 			 struct ttm_operation_ctx *ctx);
86*4882a593Smuzhiyun void ttm_mem_global_free(struct ttm_mem_global *glob, uint64_t amount);
87*4882a593Smuzhiyun int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
88*4882a593Smuzhiyun 			      struct page *page, uint64_t size,
89*4882a593Smuzhiyun 			      struct ttm_operation_ctx *ctx);
90*4882a593Smuzhiyun void ttm_mem_global_free_page(struct ttm_mem_global *glob,
91*4882a593Smuzhiyun 			      struct page *page, uint64_t size);
92*4882a593Smuzhiyun size_t ttm_round_pot(size_t size);
93*4882a593Smuzhiyun bool ttm_check_under_lowerlimit(struct ttm_mem_global *glob, uint64_t num_pages,
94*4882a593Smuzhiyun 				struct ttm_operation_ctx *ctx);
95*4882a593Smuzhiyun #endif
96