mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-18 00:24:58 +08:00
a2d635decb
-----BEGIN PGP SIGNATURE----- iQIcBAABAgAGBQJc04M6AAoJEAx081l5xIa+SJgP/0uIgIOM53vPpydgmr+2IEHF jbDqrd+mipgNriRVHjDsWdUHCUNtyhB7YEBCMrj3mY0rRFI7FlQQf4lOwYGoHiKP 4JZg4kwC37997lFXl1uabGj3DmJLtxKL2/D15zCH/uLe+2EDzWznP6NVdFT3WK0P YKZQCWT19PWSsLoBRPutWxkmop4AYvkqE0a6vXUlJlFYZK3Bbytx6/179uWKfiX5 ZkKEEtx1XiDAvcp5gBb6PISurycrBY0e/bkPBnK3ES5vawMbTU5IrmWOrQ4D8yOd z9qOVZawZ6+b2XBDgBWjQ9bM7I5R7Il1q/LglYEaFI9+wHUnlUdDSm6ft5/5BiCZ fqgkh5Bj2iEsajbSsacoljMOpxpYPqj63mqc+7fAGXF34V+B+9U1bpt8kCbMKowf 7Abb7IuiCR6vLDapjP6VqTMvdQ4O466OEAN83ULGFTdmMqYYH4AxaIwc+xcAk/aP RNq7/RHhh4FRynRAj9fCkGlF3ArnM88gLINwWuEQq4SClWGcvdw7eaHpwWo77c4g iccCnTLqSIg5pDVu07AQzzBlW6KulWxh5o72x+Xx+EXWdYUDHQ1SlNs11bSNUBV1 5MkrzY2GuD+NFEjsXJEDIPOr40mQOyJCXnxq8nXPsz/hD9kHeJPvWn3J3eVKyb5B Z6/knNqM0BDn3SaYR/rD =YFiQ -----END PGP SIGNATURE----- Merge tag 'drm-next-2019-05-09' of git://anongit.freedesktop.org/drm/drm Pull drm updates from Dave Airlie: "This has two exciting community drivers for ARM Mali accelerators. Since ARM has never been open source friendly on the GPU side of the house, the community has had to create open source drivers for the Mali GPUs. Lima covers the older t4xx and panfrost the newer 6xx/7xx series. Well done to all involved and hopefully this will help ARM head in the right direction. There is also now the ability if you don't have any of the legacy drivers enabled (pre-KMS) to remove all the pre-KMS support code from the core drm, this saves 10% or so in codesize on my machine. i915 also enable Icelake/Elkhart Lake Gen11 GPUs by default, vboxvideo moves out of staging. There are also some rcar-du patches which crossover with media tree but all should be acked by Mauro. Summary: uapi changes: - Colorspace connector property - fourcc - new YUV formts - timeline sync objects initially merged - expose FB_DAMAGE_CLIPS to atomic userspace new drivers: - vboxvideo: moved out of staging - aspeed: ASPEED SoC BMC chip display support - lima: ARM Mali4xx GPU acceleration driver support - panfrost: ARM Mali6xx/7xx Midgard/Bitfrost acceleration driver support core: - component helper docs - unplugging fixes - devm device init - MIPI/DSI rate control - shmem backed gem objects - connector, display_info, edid_quirks cleanups - dma_buf fence chain support - 64-bit dma-fence seqno comparison fixes - move initial fb config code to core - gem fence array helpers for Lima - ability to remove legacy support code if no drivers requires it (removes 10% of drm.ko size) - lease fixes ttm: - unified DRM_FILE_PAGE_OFFSET handling - Account for kernel allocations in kernel zone only panel: - OSD070T1718-19TS panel support - panel-tpo-td028ttec1 backlight support - Ronbo RB070D30 MIPI/DSI - Feiyang FY07024DI26A30-D MIPI-DSI panel - Rocktech jh057n00900 MIPI-DSI panel i915: - Comet Lake (Gen9) PCI IDs - Updated Icelake PCI IDs - Elkhartlake (Gen11) support - DP MST property addtions - plane and watermark fixes - Icelake port sync and VEBOX disable fixes - struct_mutex usage reduction - Icelake gamma fix - GuC reset fixes - make mmap more asynchronous - sound display power well race fixes - DDI/MIPI-DSI clocks for Icelake - Icelake RPS frequency changing support - Icelake workarounds amdgpu: - Use HMM for userptr - vega20 experimental smu11 support - RAS support for vega20 - BACO support for vega12 + fixes for vega20 - reworked IH interrupt handling - amdkfd RAS support - Freesync improvements - initial timeline sync object support - DC Z ordering fixes - NV12 planes support - colorspace properties for planes= - eDP opts if eDP already initialized nouveau: - misc fixes etnaviv: - misc fixes msm: - GPU zap shader support expansion - robustness ABI addition exynos: - Logging cleanups tegra: - Shared reset fix - CPU cache maintenance fix cirrus: - driver rewritten using simple helpers meson: - G12A support vmwgfx: - Resource dirtying management improvements - Userspace logging improvements virtio: - PRIME fixes rockchip: - rk3066 hdmi support sun4i: - DSI burst mode support vc4: - load tracker to detect underflow v3d: - v3d v4.2 support malidp: - initial Mali D71 support in komeda driver tfp410: - omap related improvement omapdrm: - drm bridge/panel support - drop some omap specific panels rcar-du: - Display writeback support" * tag 'drm-next-2019-05-09' of git://anongit.freedesktop.org/drm/drm: (1507 commits) drm/msm/a6xx: No zap shader is not an error drm/cma-helper: Fix drm_gem_cma_free_object() drm: Fix timestamp docs for variable refresh properties. drm/komeda: Mark the local functions as static drm/komeda: Fixed warning: Function parameter or member not described drm/komeda: Expose bus_width to Komeda-CORE drm/komeda: Add sysfs attribute: core_id and config_id drm: add non-desktop quirk for Valve HMDs drm/panfrost: Show stored feature registers drm/panfrost: Don't scream about deferred probe drm/panfrost: Disable PM on probe failure drm/panfrost: Set DMA masks earlier drm/panfrost: Add sanity checks to submit IOCTL drm/etnaviv: initialize idle mask before querying the HW db drm: introduce a capability flag for syncobj timeline support drm: report consistent errors when checking syncobj capibility drm/nouveau/nouveau: forward error generated while resuming objects tree drm/nouveau/fb/ramgk104: fix spelling mistake "sucessfully" -> "successfully" drm/nouveau/i2c: Disable i2c bus access after ->fini() drm/nouveau: Remove duplicate ACPI_VIDEO_NOTIFY_PROBE definition ...
691 lines
16 KiB
C
691 lines
16 KiB
C
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
|
|
/**************************************************************************
|
|
*
|
|
* Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
|
|
* All Rights Reserved.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
* copy of this software and associated documentation files (the
|
|
* "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
* the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice (including the
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
* of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
**************************************************************************/
|
|
|
|
#define pr_fmt(fmt) "[TTM] " fmt
|
|
|
|
#include <drm/ttm/ttm_memory.h>
|
|
#include <drm/ttm/ttm_module.h>
|
|
#include <drm/ttm/ttm_page_alloc.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/wait.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/module.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/swap.h>
|
|
|
|
#define TTM_MEMORY_ALLOC_RETRIES 4
|
|
|
|
struct ttm_mem_global ttm_mem_glob;
|
|
EXPORT_SYMBOL(ttm_mem_glob);
|
|
|
|
struct ttm_mem_zone {
|
|
struct kobject kobj;
|
|
struct ttm_mem_global *glob;
|
|
const char *name;
|
|
uint64_t zone_mem;
|
|
uint64_t emer_mem;
|
|
uint64_t max_mem;
|
|
uint64_t swap_limit;
|
|
uint64_t used_mem;
|
|
};
|
|
|
|
static struct attribute ttm_mem_sys = {
|
|
.name = "zone_memory",
|
|
.mode = S_IRUGO
|
|
};
|
|
static struct attribute ttm_mem_emer = {
|
|
.name = "emergency_memory",
|
|
.mode = S_IRUGO | S_IWUSR
|
|
};
|
|
static struct attribute ttm_mem_max = {
|
|
.name = "available_memory",
|
|
.mode = S_IRUGO | S_IWUSR
|
|
};
|
|
static struct attribute ttm_mem_swap = {
|
|
.name = "swap_limit",
|
|
.mode = S_IRUGO | S_IWUSR
|
|
};
|
|
static struct attribute ttm_mem_used = {
|
|
.name = "used_memory",
|
|
.mode = S_IRUGO
|
|
};
|
|
|
|
static void ttm_mem_zone_kobj_release(struct kobject *kobj)
|
|
{
|
|
struct ttm_mem_zone *zone =
|
|
container_of(kobj, struct ttm_mem_zone, kobj);
|
|
|
|
pr_info("Zone %7s: Used memory at exit: %llu KiB\n",
|
|
zone->name, (unsigned long long)zone->used_mem >> 10);
|
|
kfree(zone);
|
|
}
|
|
|
|
static ssize_t ttm_mem_zone_show(struct kobject *kobj,
|
|
struct attribute *attr,
|
|
char *buffer)
|
|
{
|
|
struct ttm_mem_zone *zone =
|
|
container_of(kobj, struct ttm_mem_zone, kobj);
|
|
uint64_t val = 0;
|
|
|
|
spin_lock(&zone->glob->lock);
|
|
if (attr == &ttm_mem_sys)
|
|
val = zone->zone_mem;
|
|
else if (attr == &ttm_mem_emer)
|
|
val = zone->emer_mem;
|
|
else if (attr == &ttm_mem_max)
|
|
val = zone->max_mem;
|
|
else if (attr == &ttm_mem_swap)
|
|
val = zone->swap_limit;
|
|
else if (attr == &ttm_mem_used)
|
|
val = zone->used_mem;
|
|
spin_unlock(&zone->glob->lock);
|
|
|
|
return snprintf(buffer, PAGE_SIZE, "%llu\n",
|
|
(unsigned long long) val >> 10);
|
|
}
|
|
|
|
static void ttm_check_swapping(struct ttm_mem_global *glob);
|
|
|
|
static ssize_t ttm_mem_zone_store(struct kobject *kobj,
|
|
struct attribute *attr,
|
|
const char *buffer,
|
|
size_t size)
|
|
{
|
|
struct ttm_mem_zone *zone =
|
|
container_of(kobj, struct ttm_mem_zone, kobj);
|
|
int chars;
|
|
unsigned long val;
|
|
uint64_t val64;
|
|
|
|
chars = sscanf(buffer, "%lu", &val);
|
|
if (chars == 0)
|
|
return size;
|
|
|
|
val64 = val;
|
|
val64 <<= 10;
|
|
|
|
spin_lock(&zone->glob->lock);
|
|
if (val64 > zone->zone_mem)
|
|
val64 = zone->zone_mem;
|
|
if (attr == &ttm_mem_emer) {
|
|
zone->emer_mem = val64;
|
|
if (zone->max_mem > val64)
|
|
zone->max_mem = val64;
|
|
} else if (attr == &ttm_mem_max) {
|
|
zone->max_mem = val64;
|
|
if (zone->emer_mem < val64)
|
|
zone->emer_mem = val64;
|
|
} else if (attr == &ttm_mem_swap)
|
|
zone->swap_limit = val64;
|
|
spin_unlock(&zone->glob->lock);
|
|
|
|
ttm_check_swapping(zone->glob);
|
|
|
|
return size;
|
|
}
|
|
|
|
static struct attribute *ttm_mem_zone_attrs[] = {
|
|
&ttm_mem_sys,
|
|
&ttm_mem_emer,
|
|
&ttm_mem_max,
|
|
&ttm_mem_swap,
|
|
&ttm_mem_used,
|
|
NULL
|
|
};
|
|
|
|
static const struct sysfs_ops ttm_mem_zone_ops = {
|
|
.show = &ttm_mem_zone_show,
|
|
.store = &ttm_mem_zone_store
|
|
};
|
|
|
|
static struct kobj_type ttm_mem_zone_kobj_type = {
|
|
.release = &ttm_mem_zone_kobj_release,
|
|
.sysfs_ops = &ttm_mem_zone_ops,
|
|
.default_attrs = ttm_mem_zone_attrs,
|
|
};
|
|
|
|
static struct attribute ttm_mem_global_lower_mem_limit = {
|
|
.name = "lower_mem_limit",
|
|
.mode = S_IRUGO | S_IWUSR
|
|
};
|
|
|
|
static ssize_t ttm_mem_global_show(struct kobject *kobj,
|
|
struct attribute *attr,
|
|
char *buffer)
|
|
{
|
|
struct ttm_mem_global *glob =
|
|
container_of(kobj, struct ttm_mem_global, kobj);
|
|
uint64_t val = 0;
|
|
|
|
spin_lock(&glob->lock);
|
|
val = glob->lower_mem_limit;
|
|
spin_unlock(&glob->lock);
|
|
/* convert from number of pages to KB */
|
|
val <<= (PAGE_SHIFT - 10);
|
|
return snprintf(buffer, PAGE_SIZE, "%llu\n",
|
|
(unsigned long long) val);
|
|
}
|
|
|
|
static ssize_t ttm_mem_global_store(struct kobject *kobj,
|
|
struct attribute *attr,
|
|
const char *buffer,
|
|
size_t size)
|
|
{
|
|
int chars;
|
|
uint64_t val64;
|
|
unsigned long val;
|
|
struct ttm_mem_global *glob =
|
|
container_of(kobj, struct ttm_mem_global, kobj);
|
|
|
|
chars = sscanf(buffer, "%lu", &val);
|
|
if (chars == 0)
|
|
return size;
|
|
|
|
val64 = val;
|
|
/* convert from KB to number of pages */
|
|
val64 >>= (PAGE_SHIFT - 10);
|
|
|
|
spin_lock(&glob->lock);
|
|
glob->lower_mem_limit = val64;
|
|
spin_unlock(&glob->lock);
|
|
|
|
return size;
|
|
}
|
|
|
|
static struct attribute *ttm_mem_global_attrs[] = {
|
|
&ttm_mem_global_lower_mem_limit,
|
|
NULL
|
|
};
|
|
|
|
static const struct sysfs_ops ttm_mem_global_ops = {
|
|
.show = &ttm_mem_global_show,
|
|
.store = &ttm_mem_global_store,
|
|
};
|
|
|
|
static struct kobj_type ttm_mem_glob_kobj_type = {
|
|
.sysfs_ops = &ttm_mem_global_ops,
|
|
.default_attrs = ttm_mem_global_attrs,
|
|
};
|
|
|
|
static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
|
|
bool from_wq, uint64_t extra)
|
|
{
|
|
unsigned int i;
|
|
struct ttm_mem_zone *zone;
|
|
uint64_t target;
|
|
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
zone = glob->zones[i];
|
|
|
|
if (from_wq)
|
|
target = zone->swap_limit;
|
|
else if (capable(CAP_SYS_ADMIN))
|
|
target = zone->emer_mem;
|
|
else
|
|
target = zone->max_mem;
|
|
|
|
target = (extra > target) ? 0ULL : target;
|
|
|
|
if (zone->used_mem > target)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* At this point we only support a single shrink callback.
|
|
* Extend this if needed, perhaps using a linked list of callbacks.
|
|
* Note that this function is reentrant:
|
|
* many threads may try to swap out at any given time.
|
|
*/
|
|
|
|
static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
|
|
uint64_t extra, struct ttm_operation_ctx *ctx)
|
|
{
|
|
int ret;
|
|
|
|
spin_lock(&glob->lock);
|
|
|
|
while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
|
|
spin_unlock(&glob->lock);
|
|
ret = ttm_bo_swapout(glob->bo_glob, ctx);
|
|
spin_lock(&glob->lock);
|
|
if (unlikely(ret != 0))
|
|
break;
|
|
}
|
|
|
|
spin_unlock(&glob->lock);
|
|
}
|
|
|
|
static void ttm_shrink_work(struct work_struct *work)
|
|
{
|
|
struct ttm_operation_ctx ctx = {
|
|
.interruptible = false,
|
|
.no_wait_gpu = false
|
|
};
|
|
struct ttm_mem_global *glob =
|
|
container_of(work, struct ttm_mem_global, work);
|
|
|
|
ttm_shrink(glob, true, 0ULL, &ctx);
|
|
}
|
|
|
|
static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
|
|
const struct sysinfo *si)
|
|
{
|
|
struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
|
|
uint64_t mem;
|
|
int ret;
|
|
|
|
if (unlikely(!zone))
|
|
return -ENOMEM;
|
|
|
|
mem = si->totalram - si->totalhigh;
|
|
mem *= si->mem_unit;
|
|
|
|
zone->name = "kernel";
|
|
zone->zone_mem = mem;
|
|
zone->max_mem = mem >> 1;
|
|
zone->emer_mem = (mem >> 1) + (mem >> 2);
|
|
zone->swap_limit = zone->max_mem - (mem >> 3);
|
|
zone->used_mem = 0;
|
|
zone->glob = glob;
|
|
glob->zone_kernel = zone;
|
|
ret = kobject_init_and_add(
|
|
&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
|
|
if (unlikely(ret != 0)) {
|
|
kobject_put(&zone->kobj);
|
|
return ret;
|
|
}
|
|
glob->zones[glob->num_zones++] = zone;
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CONFIG_HIGHMEM
|
|
static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
|
|
const struct sysinfo *si)
|
|
{
|
|
struct ttm_mem_zone *zone;
|
|
uint64_t mem;
|
|
int ret;
|
|
|
|
if (si->totalhigh == 0)
|
|
return 0;
|
|
|
|
zone = kzalloc(sizeof(*zone), GFP_KERNEL);
|
|
if (unlikely(!zone))
|
|
return -ENOMEM;
|
|
|
|
mem = si->totalram;
|
|
mem *= si->mem_unit;
|
|
|
|
zone->name = "highmem";
|
|
zone->zone_mem = mem;
|
|
zone->max_mem = mem >> 1;
|
|
zone->emer_mem = (mem >> 1) + (mem >> 2);
|
|
zone->swap_limit = zone->max_mem - (mem >> 3);
|
|
zone->used_mem = 0;
|
|
zone->glob = glob;
|
|
glob->zone_highmem = zone;
|
|
ret = kobject_init_and_add(
|
|
&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s",
|
|
zone->name);
|
|
if (unlikely(ret != 0)) {
|
|
kobject_put(&zone->kobj);
|
|
return ret;
|
|
}
|
|
glob->zones[glob->num_zones++] = zone;
|
|
return 0;
|
|
}
|
|
#else
|
|
static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
|
|
const struct sysinfo *si)
|
|
{
|
|
struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
|
|
uint64_t mem;
|
|
int ret;
|
|
|
|
if (unlikely(!zone))
|
|
return -ENOMEM;
|
|
|
|
mem = si->totalram;
|
|
mem *= si->mem_unit;
|
|
|
|
/**
|
|
* No special dma32 zone needed.
|
|
*/
|
|
|
|
if (mem <= ((uint64_t) 1ULL << 32)) {
|
|
kfree(zone);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Limit max dma32 memory to 4GB for now
|
|
* until we can figure out how big this
|
|
* zone really is.
|
|
*/
|
|
|
|
mem = ((uint64_t) 1ULL << 32);
|
|
zone->name = "dma32";
|
|
zone->zone_mem = mem;
|
|
zone->max_mem = mem >> 1;
|
|
zone->emer_mem = (mem >> 1) + (mem >> 2);
|
|
zone->swap_limit = zone->max_mem - (mem >> 3);
|
|
zone->used_mem = 0;
|
|
zone->glob = glob;
|
|
glob->zone_dma32 = zone;
|
|
ret = kobject_init_and_add(
|
|
&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
|
|
if (unlikely(ret != 0)) {
|
|
kobject_put(&zone->kobj);
|
|
return ret;
|
|
}
|
|
glob->zones[glob->num_zones++] = zone;
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
int ttm_mem_global_init(struct ttm_mem_global *glob)
|
|
{
|
|
struct sysinfo si;
|
|
int ret;
|
|
int i;
|
|
struct ttm_mem_zone *zone;
|
|
|
|
spin_lock_init(&glob->lock);
|
|
glob->swap_queue = create_singlethread_workqueue("ttm_swap");
|
|
INIT_WORK(&glob->work, ttm_shrink_work);
|
|
ret = kobject_init_and_add(
|
|
&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
|
|
if (unlikely(ret != 0)) {
|
|
kobject_put(&glob->kobj);
|
|
return ret;
|
|
}
|
|
|
|
si_meminfo(&si);
|
|
|
|
/* set it as 0 by default to keep original behavior of OOM */
|
|
glob->lower_mem_limit = 0;
|
|
|
|
ret = ttm_mem_init_kernel_zone(glob, &si);
|
|
if (unlikely(ret != 0))
|
|
goto out_no_zone;
|
|
#ifdef CONFIG_HIGHMEM
|
|
ret = ttm_mem_init_highmem_zone(glob, &si);
|
|
if (unlikely(ret != 0))
|
|
goto out_no_zone;
|
|
#else
|
|
ret = ttm_mem_init_dma32_zone(glob, &si);
|
|
if (unlikely(ret != 0))
|
|
goto out_no_zone;
|
|
#endif
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
zone = glob->zones[i];
|
|
pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
|
|
zone->name, (unsigned long long)zone->max_mem >> 10);
|
|
}
|
|
ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
|
|
ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
|
|
return 0;
|
|
out_no_zone:
|
|
ttm_mem_global_release(glob);
|
|
return ret;
|
|
}
|
|
|
|
void ttm_mem_global_release(struct ttm_mem_global *glob)
|
|
{
|
|
struct ttm_mem_zone *zone;
|
|
unsigned int i;
|
|
|
|
/* let the page allocator first stop the shrink work. */
|
|
ttm_page_alloc_fini();
|
|
ttm_dma_page_alloc_fini();
|
|
|
|
flush_workqueue(glob->swap_queue);
|
|
destroy_workqueue(glob->swap_queue);
|
|
glob->swap_queue = NULL;
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
zone = glob->zones[i];
|
|
kobject_del(&zone->kobj);
|
|
kobject_put(&zone->kobj);
|
|
}
|
|
kobject_del(&glob->kobj);
|
|
kobject_put(&glob->kobj);
|
|
memset(glob, 0, sizeof(*glob));
|
|
}
|
|
|
|
static void ttm_check_swapping(struct ttm_mem_global *glob)
|
|
{
|
|
bool needs_swapping = false;
|
|
unsigned int i;
|
|
struct ttm_mem_zone *zone;
|
|
|
|
spin_lock(&glob->lock);
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
zone = glob->zones[i];
|
|
if (zone->used_mem > zone->swap_limit) {
|
|
needs_swapping = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
spin_unlock(&glob->lock);
|
|
|
|
if (unlikely(needs_swapping))
|
|
(void)queue_work(glob->swap_queue, &glob->work);
|
|
|
|
}
|
|
|
|
static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
|
|
struct ttm_mem_zone *single_zone,
|
|
uint64_t amount)
|
|
{
|
|
unsigned int i;
|
|
struct ttm_mem_zone *zone;
|
|
|
|
spin_lock(&glob->lock);
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
zone = glob->zones[i];
|
|
if (single_zone && zone != single_zone)
|
|
continue;
|
|
zone->used_mem -= amount;
|
|
}
|
|
spin_unlock(&glob->lock);
|
|
}
|
|
|
|
void ttm_mem_global_free(struct ttm_mem_global *glob,
|
|
uint64_t amount)
|
|
{
|
|
return ttm_mem_global_free_zone(glob, glob->zone_kernel, amount);
|
|
}
|
|
EXPORT_SYMBOL(ttm_mem_global_free);
|
|
|
|
/*
|
|
* check if the available mem is under lower memory limit
|
|
*
|
|
* a. if no swap disk at all or free swap space is under swap_mem_limit
|
|
* but available system mem is bigger than sys_mem_limit, allow TTM
|
|
* allocation;
|
|
*
|
|
* b. if the available system mem is less than sys_mem_limit but free
|
|
* swap disk is bigger than swap_mem_limit, allow TTM allocation.
|
|
*/
|
|
bool
|
|
ttm_check_under_lowerlimit(struct ttm_mem_global *glob,
|
|
uint64_t num_pages,
|
|
struct ttm_operation_ctx *ctx)
|
|
{
|
|
int64_t available;
|
|
|
|
if (ctx->flags & TTM_OPT_FLAG_FORCE_ALLOC)
|
|
return false;
|
|
|
|
available = get_nr_swap_pages() + si_mem_available();
|
|
available -= num_pages;
|
|
if (available < glob->lower_mem_limit)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
EXPORT_SYMBOL(ttm_check_under_lowerlimit);
|
|
|
|
static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
|
|
struct ttm_mem_zone *single_zone,
|
|
uint64_t amount, bool reserve)
|
|
{
|
|
uint64_t limit;
|
|
int ret = -ENOMEM;
|
|
unsigned int i;
|
|
struct ttm_mem_zone *zone;
|
|
|
|
spin_lock(&glob->lock);
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
zone = glob->zones[i];
|
|
if (single_zone && zone != single_zone)
|
|
continue;
|
|
|
|
limit = (capable(CAP_SYS_ADMIN)) ?
|
|
zone->emer_mem : zone->max_mem;
|
|
|
|
if (zone->used_mem > limit)
|
|
goto out_unlock;
|
|
}
|
|
|
|
if (reserve) {
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
zone = glob->zones[i];
|
|
if (single_zone && zone != single_zone)
|
|
continue;
|
|
zone->used_mem += amount;
|
|
}
|
|
}
|
|
|
|
ret = 0;
|
|
out_unlock:
|
|
spin_unlock(&glob->lock);
|
|
ttm_check_swapping(glob);
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
|
|
struct ttm_mem_zone *single_zone,
|
|
uint64_t memory,
|
|
struct ttm_operation_ctx *ctx)
|
|
{
|
|
int count = TTM_MEMORY_ALLOC_RETRIES;
|
|
|
|
while (unlikely(ttm_mem_global_reserve(glob,
|
|
single_zone,
|
|
memory, true)
|
|
!= 0)) {
|
|
if (ctx->no_wait_gpu)
|
|
return -ENOMEM;
|
|
if (unlikely(count-- == 0))
|
|
return -ENOMEM;
|
|
ttm_shrink(glob, false, memory + (memory >> 2) + 16, ctx);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
|
|
struct ttm_operation_ctx *ctx)
|
|
{
|
|
/**
|
|
* Normal allocations of kernel memory are registered in
|
|
* the kernel zone.
|
|
*/
|
|
|
|
return ttm_mem_global_alloc_zone(glob, glob->zone_kernel, memory, ctx);
|
|
}
|
|
EXPORT_SYMBOL(ttm_mem_global_alloc);
|
|
|
|
int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
|
|
struct page *page, uint64_t size,
|
|
struct ttm_operation_ctx *ctx)
|
|
{
|
|
struct ttm_mem_zone *zone = NULL;
|
|
|
|
/**
|
|
* Page allocations may be registed in a single zone
|
|
* only if highmem or !dma32.
|
|
*/
|
|
|
|
#ifdef CONFIG_HIGHMEM
|
|
if (PageHighMem(page) && glob->zone_highmem != NULL)
|
|
zone = glob->zone_highmem;
|
|
#else
|
|
if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
|
|
zone = glob->zone_kernel;
|
|
#endif
|
|
return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
|
|
}
|
|
|
|
void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
|
|
uint64_t size)
|
|
{
|
|
struct ttm_mem_zone *zone = NULL;
|
|
|
|
#ifdef CONFIG_HIGHMEM
|
|
if (PageHighMem(page) && glob->zone_highmem != NULL)
|
|
zone = glob->zone_highmem;
|
|
#else
|
|
if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
|
|
zone = glob->zone_kernel;
|
|
#endif
|
|
ttm_mem_global_free_zone(glob, zone, size);
|
|
}
|
|
|
|
size_t ttm_round_pot(size_t size)
|
|
{
|
|
if ((size & (size - 1)) == 0)
|
|
return size;
|
|
else if (size > PAGE_SIZE)
|
|
return PAGE_ALIGN(size);
|
|
else {
|
|
size_t tmp_size = 4;
|
|
|
|
while (tmp_size < size)
|
|
tmp_size <<= 1;
|
|
|
|
return tmp_size;
|
|
}
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(ttm_round_pot);
|
|
|
|
uint64_t ttm_get_kernel_zone_memory_size(struct ttm_mem_global *glob)
|
|
{
|
|
return glob->zone_kernel->max_mem;
|
|
}
|
|
EXPORT_SYMBOL(ttm_get_kernel_zone_memory_size);
|