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