1f86ad0edSMatthew Brost // SPDX-License-Identifier: GPL-2.0-only OR MIT 2f86ad0edSMatthew Brost /* 3f86ad0edSMatthew Brost * Copyright © 2024-2025 Intel Corporation 4f86ad0edSMatthew Brost */ 5f86ad0edSMatthew Brost 6f86ad0edSMatthew Brost #include <linux/dma-mapping.h> 7f86ad0edSMatthew Brost #include <linux/migrate.h> 8f86ad0edSMatthew Brost #include <linux/pagemap.h> 9*2ef19be2SThomas Hellström #include <drm/drm_drv.h> 10f86ad0edSMatthew Brost #include <drm/drm_pagemap.h> 11f86ad0edSMatthew Brost 12f86ad0edSMatthew Brost /** 13f86ad0edSMatthew Brost * DOC: Overview 14f86ad0edSMatthew Brost * 15f86ad0edSMatthew Brost * The DRM pagemap layer is intended to augment the dev_pagemap functionality by 16f86ad0edSMatthew Brost * providing a way to populate a struct mm_struct virtual range with device 17f86ad0edSMatthew Brost * private pages and to provide helpers to abstract device memory allocations, 18f86ad0edSMatthew Brost * to migrate memory back and forth between device memory and system RAM and 19f86ad0edSMatthew Brost * to handle access (and in the future migration) between devices implementing 20f86ad0edSMatthew Brost * a fast interconnect that is not necessarily visible to the rest of the 21f86ad0edSMatthew Brost * system. 22f86ad0edSMatthew Brost * 23f86ad0edSMatthew Brost * Typically the DRM pagemap receives requests from one or more DRM GPU SVM 24*2ef19be2SThomas Hellström * instances to populate struct mm_struct virtual ranges with memory, and the 25*2ef19be2SThomas Hellström * migration is best effort only and may thus fail. The implementation should 26*2ef19be2SThomas Hellström * also handle device unbinding by blocking (return an -ENODEV) error for new 27*2ef19be2SThomas Hellström * population requests and after that migrate all device pages to system ram. 28f86ad0edSMatthew Brost */ 29f86ad0edSMatthew Brost 30f86ad0edSMatthew Brost /** 31f86ad0edSMatthew Brost * DOC: Migration 32f86ad0edSMatthew Brost * 33*2ef19be2SThomas Hellström * Migration granularity typically follows the GPU SVM range requests, but 34*2ef19be2SThomas Hellström * if there are clashes, due to races or due to the fact that multiple GPU 35*2ef19be2SThomas Hellström * SVM instances have different views of the ranges used, and because of that 36*2ef19be2SThomas Hellström * parts of a requested range is already present in the requested device memory, 37*2ef19be2SThomas Hellström * the implementation has a variety of options. It can fail and it can choose 38*2ef19be2SThomas Hellström * to populate only the part of the range that isn't already in device memory, 39*2ef19be2SThomas Hellström * and it can evict the range to system before trying to migrate. Ideally an 40*2ef19be2SThomas Hellström * implementation would just try to migrate the missing part of the range and 41*2ef19be2SThomas Hellström * allocate just enough memory to do so. 42f86ad0edSMatthew Brost * 43*2ef19be2SThomas Hellström * When migrating to system memory as a response to a cpu fault or a device 44*2ef19be2SThomas Hellström * memory eviction request, currently a full device memory allocation is 45*2ef19be2SThomas Hellström * migrated back to system. Moving forward this might need improvement for 46*2ef19be2SThomas Hellström * situations where a single page needs bouncing between system memory and 47*2ef19be2SThomas Hellström * device memory due to, for example, atomic operations. 48f86ad0edSMatthew Brost * 49f86ad0edSMatthew Brost * Key DRM pagemap components: 50f86ad0edSMatthew Brost * 51f86ad0edSMatthew Brost * - Device Memory Allocations: 52f86ad0edSMatthew Brost * Embedded structure containing enough information for the drm_pagemap to 53f86ad0edSMatthew Brost * migrate to / from device memory. 54f86ad0edSMatthew Brost * 55f86ad0edSMatthew Brost * - Device Memory Operations: 56f86ad0edSMatthew Brost * Define the interface for driver-specific device memory operations 57f86ad0edSMatthew Brost * release memory, populate pfns, and copy to / from device memory. 58f86ad0edSMatthew Brost */ 59f86ad0edSMatthew Brost 60f86ad0edSMatthew Brost /** 61f86ad0edSMatthew Brost * struct drm_pagemap_zdd - GPU SVM zone device data 62f86ad0edSMatthew Brost * 63f86ad0edSMatthew Brost * @refcount: Reference count for the zdd 64f86ad0edSMatthew Brost * @devmem_allocation: device memory allocation 65f86ad0edSMatthew Brost * @device_private_page_owner: Device private pages owner 66f86ad0edSMatthew Brost * 67f86ad0edSMatthew Brost * This structure serves as a generic wrapper installed in 68f86ad0edSMatthew Brost * page->zone_device_data. It provides infrastructure for looking up a device 69f86ad0edSMatthew Brost * memory allocation upon CPU page fault and asynchronously releasing device 70f86ad0edSMatthew Brost * memory once the CPU has no page references. Asynchronous release is useful 71f86ad0edSMatthew Brost * because CPU page references can be dropped in IRQ contexts, while releasing 72f86ad0edSMatthew Brost * device memory likely requires sleeping locks. 73f86ad0edSMatthew Brost */ 74f86ad0edSMatthew Brost struct drm_pagemap_zdd { 75f86ad0edSMatthew Brost struct kref refcount; 76f86ad0edSMatthew Brost struct drm_pagemap_devmem *devmem_allocation; 77f86ad0edSMatthew Brost void *device_private_page_owner; 78f86ad0edSMatthew Brost }; 79f86ad0edSMatthew Brost 80f86ad0edSMatthew Brost /** 81f86ad0edSMatthew Brost * drm_pagemap_zdd_alloc() - Allocate a zdd structure. 82f86ad0edSMatthew Brost * @device_private_page_owner: Device private pages owner 83f86ad0edSMatthew Brost * 84f86ad0edSMatthew Brost * This function allocates and initializes a new zdd structure. It sets up the 85f86ad0edSMatthew Brost * reference count and initializes the destroy work. 86f86ad0edSMatthew Brost * 87f86ad0edSMatthew Brost * Return: Pointer to the allocated zdd on success, ERR_PTR() on failure. 88f86ad0edSMatthew Brost */ 89f86ad0edSMatthew Brost static struct drm_pagemap_zdd * 90f86ad0edSMatthew Brost drm_pagemap_zdd_alloc(void *device_private_page_owner) 91f86ad0edSMatthew Brost { 92f86ad0edSMatthew Brost struct drm_pagemap_zdd *zdd; 93f86ad0edSMatthew Brost 94f86ad0edSMatthew Brost zdd = kmalloc(sizeof(*zdd), GFP_KERNEL); 95f86ad0edSMatthew Brost if (!zdd) 96f86ad0edSMatthew Brost return NULL; 97f86ad0edSMatthew Brost 98f86ad0edSMatthew Brost kref_init(&zdd->refcount); 99f86ad0edSMatthew Brost zdd->devmem_allocation = NULL; 100f86ad0edSMatthew Brost zdd->device_private_page_owner = device_private_page_owner; 101f86ad0edSMatthew Brost 102f86ad0edSMatthew Brost return zdd; 103f86ad0edSMatthew Brost } 104f86ad0edSMatthew Brost 105f86ad0edSMatthew Brost /** 106f86ad0edSMatthew Brost * drm_pagemap_zdd_get() - Get a reference to a zdd structure. 107f86ad0edSMatthew Brost * @zdd: Pointer to the zdd structure. 108f86ad0edSMatthew Brost * 109f86ad0edSMatthew Brost * This function increments the reference count of the provided zdd structure. 110f86ad0edSMatthew Brost * 111f86ad0edSMatthew Brost * Return: Pointer to the zdd structure. 112f86ad0edSMatthew Brost */ 113f86ad0edSMatthew Brost static struct drm_pagemap_zdd *drm_pagemap_zdd_get(struct drm_pagemap_zdd *zdd) 114f86ad0edSMatthew Brost { 115f86ad0edSMatthew Brost kref_get(&zdd->refcount); 116f86ad0edSMatthew Brost return zdd; 117f86ad0edSMatthew Brost } 118f86ad0edSMatthew Brost 119f86ad0edSMatthew Brost /** 120f86ad0edSMatthew Brost * drm_pagemap_zdd_destroy() - Destroy a zdd structure. 121f86ad0edSMatthew Brost * @ref: Pointer to the reference count structure. 122f86ad0edSMatthew Brost * 123f86ad0edSMatthew Brost * This function queues the destroy_work of the zdd for asynchronous destruction. 124f86ad0edSMatthew Brost */ 125f86ad0edSMatthew Brost static void drm_pagemap_zdd_destroy(struct kref *ref) 126f86ad0edSMatthew Brost { 127f86ad0edSMatthew Brost struct drm_pagemap_zdd *zdd = 128f86ad0edSMatthew Brost container_of(ref, struct drm_pagemap_zdd, refcount); 129f86ad0edSMatthew Brost struct drm_pagemap_devmem *devmem = zdd->devmem_allocation; 130f86ad0edSMatthew Brost 131f86ad0edSMatthew Brost if (devmem) { 132f86ad0edSMatthew Brost complete_all(&devmem->detached); 133f86ad0edSMatthew Brost if (devmem->ops->devmem_release) 134f86ad0edSMatthew Brost devmem->ops->devmem_release(devmem); 135f86ad0edSMatthew Brost } 136f86ad0edSMatthew Brost kfree(zdd); 137f86ad0edSMatthew Brost } 138f86ad0edSMatthew Brost 139f86ad0edSMatthew Brost /** 140f86ad0edSMatthew Brost * drm_pagemap_zdd_put() - Put a zdd reference. 141f86ad0edSMatthew Brost * @zdd: Pointer to the zdd structure. 142f86ad0edSMatthew Brost * 143f86ad0edSMatthew Brost * This function decrements the reference count of the provided zdd structure 144f86ad0edSMatthew Brost * and schedules its destruction if the count drops to zero. 145f86ad0edSMatthew Brost */ 146f86ad0edSMatthew Brost static void drm_pagemap_zdd_put(struct drm_pagemap_zdd *zdd) 147f86ad0edSMatthew Brost { 148f86ad0edSMatthew Brost kref_put(&zdd->refcount, drm_pagemap_zdd_destroy); 149f86ad0edSMatthew Brost } 150f86ad0edSMatthew Brost 151f86ad0edSMatthew Brost /** 152f86ad0edSMatthew Brost * drm_pagemap_migration_unlock_put_page() - Put a migration page 153f86ad0edSMatthew Brost * @page: Pointer to the page to put 154f86ad0edSMatthew Brost * 155f86ad0edSMatthew Brost * This function unlocks and puts a page. 156f86ad0edSMatthew Brost */ 157f86ad0edSMatthew Brost static void drm_pagemap_migration_unlock_put_page(struct page *page) 158f86ad0edSMatthew Brost { 159f86ad0edSMatthew Brost unlock_page(page); 160f86ad0edSMatthew Brost put_page(page); 161f86ad0edSMatthew Brost } 162f86ad0edSMatthew Brost 163f86ad0edSMatthew Brost /** 164f86ad0edSMatthew Brost * drm_pagemap_migration_unlock_put_pages() - Put migration pages 165f86ad0edSMatthew Brost * @npages: Number of pages 166f86ad0edSMatthew Brost * @migrate_pfn: Array of migrate page frame numbers 167f86ad0edSMatthew Brost * 168f86ad0edSMatthew Brost * This function unlocks and puts an array of pages. 169f86ad0edSMatthew Brost */ 170f86ad0edSMatthew Brost static void drm_pagemap_migration_unlock_put_pages(unsigned long npages, 171f86ad0edSMatthew Brost unsigned long *migrate_pfn) 172f86ad0edSMatthew Brost { 173f86ad0edSMatthew Brost unsigned long i; 174f86ad0edSMatthew Brost 175f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) { 176f86ad0edSMatthew Brost struct page *page; 177f86ad0edSMatthew Brost 178f86ad0edSMatthew Brost if (!migrate_pfn[i]) 179f86ad0edSMatthew Brost continue; 180f86ad0edSMatthew Brost 181f86ad0edSMatthew Brost page = migrate_pfn_to_page(migrate_pfn[i]); 182f86ad0edSMatthew Brost drm_pagemap_migration_unlock_put_page(page); 183f86ad0edSMatthew Brost migrate_pfn[i] = 0; 184f86ad0edSMatthew Brost } 185f86ad0edSMatthew Brost } 186f86ad0edSMatthew Brost 187f86ad0edSMatthew Brost /** 188f86ad0edSMatthew Brost * drm_pagemap_get_devmem_page() - Get a reference to a device memory page 189f86ad0edSMatthew Brost * @page: Pointer to the page 190f86ad0edSMatthew Brost * @zdd: Pointer to the GPU SVM zone device data 191f86ad0edSMatthew Brost * 192f86ad0edSMatthew Brost * This function associates the given page with the specified GPU SVM zone 193f86ad0edSMatthew Brost * device data and initializes it for zone device usage. 194f86ad0edSMatthew Brost */ 195f86ad0edSMatthew Brost static void drm_pagemap_get_devmem_page(struct page *page, 196f86ad0edSMatthew Brost struct drm_pagemap_zdd *zdd) 197f86ad0edSMatthew Brost { 198f86ad0edSMatthew Brost page->zone_device_data = drm_pagemap_zdd_get(zdd); 199f86ad0edSMatthew Brost zone_device_page_init(page); 200f86ad0edSMatthew Brost } 201f86ad0edSMatthew Brost 202f86ad0edSMatthew Brost /** 203f86ad0edSMatthew Brost * drm_pagemap_migrate_map_pages() - Map migration pages for GPU SVM migration 204f86ad0edSMatthew Brost * @dev: The device for which the pages are being mapped 205f86ad0edSMatthew Brost * @dma_addr: Array to store DMA addresses corresponding to mapped pages 206f86ad0edSMatthew Brost * @migrate_pfn: Array of migrate page frame numbers to map 207f86ad0edSMatthew Brost * @npages: Number of pages to map 208f86ad0edSMatthew Brost * @dir: Direction of data transfer (e.g., DMA_BIDIRECTIONAL) 209f86ad0edSMatthew Brost * 210f86ad0edSMatthew Brost * This function maps pages of memory for migration usage in GPU SVM. It 211f86ad0edSMatthew Brost * iterates over each page frame number provided in @migrate_pfn, maps the 212f86ad0edSMatthew Brost * corresponding page, and stores the DMA address in the provided @dma_addr 213f86ad0edSMatthew Brost * array. 214f86ad0edSMatthew Brost * 215f86ad0edSMatthew Brost * Returns: 0 on success, -EFAULT if an error occurs during mapping. 216f86ad0edSMatthew Brost */ 217f86ad0edSMatthew Brost static int drm_pagemap_migrate_map_pages(struct device *dev, 218f86ad0edSMatthew Brost dma_addr_t *dma_addr, 219f86ad0edSMatthew Brost unsigned long *migrate_pfn, 220f86ad0edSMatthew Brost unsigned long npages, 221f86ad0edSMatthew Brost enum dma_data_direction dir) 222f86ad0edSMatthew Brost { 223f86ad0edSMatthew Brost unsigned long i; 224f86ad0edSMatthew Brost 225f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) { 226f86ad0edSMatthew Brost struct page *page = migrate_pfn_to_page(migrate_pfn[i]); 227f86ad0edSMatthew Brost 228f86ad0edSMatthew Brost if (!page) 229f86ad0edSMatthew Brost continue; 230f86ad0edSMatthew Brost 231f86ad0edSMatthew Brost if (WARN_ON_ONCE(is_zone_device_page(page))) 232f86ad0edSMatthew Brost return -EFAULT; 233f86ad0edSMatthew Brost 234f86ad0edSMatthew Brost dma_addr[i] = dma_map_page(dev, page, 0, PAGE_SIZE, dir); 235f86ad0edSMatthew Brost if (dma_mapping_error(dev, dma_addr[i])) 236f86ad0edSMatthew Brost return -EFAULT; 237f86ad0edSMatthew Brost } 238f86ad0edSMatthew Brost 239f86ad0edSMatthew Brost return 0; 240f86ad0edSMatthew Brost } 241f86ad0edSMatthew Brost 242f86ad0edSMatthew Brost /** 243f86ad0edSMatthew Brost * drm_pagemap_migrate_unmap_pages() - Unmap pages previously mapped for GPU SVM migration 244f86ad0edSMatthew Brost * @dev: The device for which the pages were mapped 245f86ad0edSMatthew Brost * @dma_addr: Array of DMA addresses corresponding to mapped pages 246f86ad0edSMatthew Brost * @npages: Number of pages to unmap 247f86ad0edSMatthew Brost * @dir: Direction of data transfer (e.g., DMA_BIDIRECTIONAL) 248f86ad0edSMatthew Brost * 249f86ad0edSMatthew Brost * This function unmaps previously mapped pages of memory for GPU Shared Virtual 250f86ad0edSMatthew Brost * Memory (SVM). It iterates over each DMA address provided in @dma_addr, checks 251f86ad0edSMatthew Brost * if it's valid and not already unmapped, and unmaps the corresponding page. 252f86ad0edSMatthew Brost */ 253f86ad0edSMatthew Brost static void drm_pagemap_migrate_unmap_pages(struct device *dev, 254f86ad0edSMatthew Brost dma_addr_t *dma_addr, 255f86ad0edSMatthew Brost unsigned long npages, 256f86ad0edSMatthew Brost enum dma_data_direction dir) 257f86ad0edSMatthew Brost { 258f86ad0edSMatthew Brost unsigned long i; 259f86ad0edSMatthew Brost 260f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) { 261f86ad0edSMatthew Brost if (!dma_addr[i] || dma_mapping_error(dev, dma_addr[i])) 262f86ad0edSMatthew Brost continue; 263f86ad0edSMatthew Brost 264f86ad0edSMatthew Brost dma_unmap_page(dev, dma_addr[i], PAGE_SIZE, dir); 265f86ad0edSMatthew Brost } 266f86ad0edSMatthew Brost } 267f86ad0edSMatthew Brost 268f86ad0edSMatthew Brost static unsigned long 269f86ad0edSMatthew Brost npages_in_range(unsigned long start, unsigned long end) 270f86ad0edSMatthew Brost { 271f86ad0edSMatthew Brost return (end - start) >> PAGE_SHIFT; 272f86ad0edSMatthew Brost } 273f86ad0edSMatthew Brost 274f86ad0edSMatthew Brost /** 275f86ad0edSMatthew Brost * drm_pagemap_migrate_to_devmem() - Migrate a struct mm_struct range to device memory 276f86ad0edSMatthew Brost * @devmem_allocation: The device memory allocation to migrate to. 277f86ad0edSMatthew Brost * The caller should hold a reference to the device memory allocation, 278f86ad0edSMatthew Brost * and the reference is consumed by this function unless it returns with 279f86ad0edSMatthew Brost * an error. 280f86ad0edSMatthew Brost * @mm: Pointer to the struct mm_struct. 281f86ad0edSMatthew Brost * @start: Start of the virtual address range to migrate. 282f86ad0edSMatthew Brost * @end: End of the virtual address range to migrate. 283f86ad0edSMatthew Brost * @timeslice_ms: The time requested for the migrated pagemap pages to 284f86ad0edSMatthew Brost * be present in @mm before being allowed to be migrated back. 285f86ad0edSMatthew Brost * @pgmap_owner: Not used currently, since only system memory is considered. 286f86ad0edSMatthew Brost * 287f86ad0edSMatthew Brost * This function migrates the specified virtual address range to device memory. 288f86ad0edSMatthew Brost * It performs the necessary setup and invokes the driver-specific operations for 289f86ad0edSMatthew Brost * migration to device memory. Expected to be called while holding the mmap lock in 290f86ad0edSMatthew Brost * at least read mode. 291f86ad0edSMatthew Brost * 292f86ad0edSMatthew Brost * Note: The @timeslice_ms parameter can typically be used to force data to 293f86ad0edSMatthew Brost * remain in pagemap pages long enough for a GPU to perform a task and to prevent 294f86ad0edSMatthew Brost * a migration livelock. One alternative would be for the GPU driver to block 295f86ad0edSMatthew Brost * in a mmu_notifier for the specified amount of time, but adding the 296f86ad0edSMatthew Brost * functionality to the pagemap is likely nicer to the system as a whole. 297f86ad0edSMatthew Brost * 298f86ad0edSMatthew Brost * Return: %0 on success, negative error code on failure. 299f86ad0edSMatthew Brost */ 300f86ad0edSMatthew Brost int drm_pagemap_migrate_to_devmem(struct drm_pagemap_devmem *devmem_allocation, 301f86ad0edSMatthew Brost struct mm_struct *mm, 302f86ad0edSMatthew Brost unsigned long start, unsigned long end, 303f86ad0edSMatthew Brost unsigned long timeslice_ms, 304f86ad0edSMatthew Brost void *pgmap_owner) 305f86ad0edSMatthew Brost { 306f86ad0edSMatthew Brost const struct drm_pagemap_devmem_ops *ops = devmem_allocation->ops; 307f86ad0edSMatthew Brost struct migrate_vma migrate = { 308f86ad0edSMatthew Brost .start = start, 309f86ad0edSMatthew Brost .end = end, 310f86ad0edSMatthew Brost .pgmap_owner = pgmap_owner, 311f86ad0edSMatthew Brost .flags = MIGRATE_VMA_SELECT_SYSTEM, 312f86ad0edSMatthew Brost }; 313f86ad0edSMatthew Brost unsigned long i, npages = npages_in_range(start, end); 314f86ad0edSMatthew Brost struct vm_area_struct *vas; 315f86ad0edSMatthew Brost struct drm_pagemap_zdd *zdd = NULL; 316f86ad0edSMatthew Brost struct page **pages; 317f86ad0edSMatthew Brost dma_addr_t *dma_addr; 318f86ad0edSMatthew Brost void *buf; 319f86ad0edSMatthew Brost int err; 320f86ad0edSMatthew Brost 321f86ad0edSMatthew Brost mmap_assert_locked(mm); 322f86ad0edSMatthew Brost 323f86ad0edSMatthew Brost if (!ops->populate_devmem_pfn || !ops->copy_to_devmem || 324f86ad0edSMatthew Brost !ops->copy_to_ram) 325f86ad0edSMatthew Brost return -EOPNOTSUPP; 326f86ad0edSMatthew Brost 327f86ad0edSMatthew Brost vas = vma_lookup(mm, start); 328f86ad0edSMatthew Brost if (!vas) { 329f86ad0edSMatthew Brost err = -ENOENT; 330f86ad0edSMatthew Brost goto err_out; 331f86ad0edSMatthew Brost } 332f86ad0edSMatthew Brost 333f86ad0edSMatthew Brost if (end > vas->vm_end || start < vas->vm_start) { 334f86ad0edSMatthew Brost err = -EINVAL; 335f86ad0edSMatthew Brost goto err_out; 336f86ad0edSMatthew Brost } 337f86ad0edSMatthew Brost 338f86ad0edSMatthew Brost if (!vma_is_anonymous(vas)) { 339f86ad0edSMatthew Brost err = -EBUSY; 340f86ad0edSMatthew Brost goto err_out; 341f86ad0edSMatthew Brost } 342f86ad0edSMatthew Brost 343f86ad0edSMatthew Brost buf = kvcalloc(npages, 2 * sizeof(*migrate.src) + sizeof(*dma_addr) + 344f86ad0edSMatthew Brost sizeof(*pages), GFP_KERNEL); 345f86ad0edSMatthew Brost if (!buf) { 346f86ad0edSMatthew Brost err = -ENOMEM; 347f86ad0edSMatthew Brost goto err_out; 348f86ad0edSMatthew Brost } 349f86ad0edSMatthew Brost dma_addr = buf + (2 * sizeof(*migrate.src) * npages); 350f86ad0edSMatthew Brost pages = buf + (2 * sizeof(*migrate.src) + sizeof(*dma_addr)) * npages; 351f86ad0edSMatthew Brost 352f86ad0edSMatthew Brost zdd = drm_pagemap_zdd_alloc(pgmap_owner); 353f86ad0edSMatthew Brost if (!zdd) { 354f86ad0edSMatthew Brost err = -ENOMEM; 355f86ad0edSMatthew Brost goto err_free; 356f86ad0edSMatthew Brost } 357f86ad0edSMatthew Brost 358f86ad0edSMatthew Brost migrate.vma = vas; 359f86ad0edSMatthew Brost migrate.src = buf; 360f86ad0edSMatthew Brost migrate.dst = migrate.src + npages; 361f86ad0edSMatthew Brost 362f86ad0edSMatthew Brost err = migrate_vma_setup(&migrate); 363f86ad0edSMatthew Brost if (err) 364f86ad0edSMatthew Brost goto err_free; 365f86ad0edSMatthew Brost 366f86ad0edSMatthew Brost if (!migrate.cpages) { 367f86ad0edSMatthew Brost err = -EFAULT; 368f86ad0edSMatthew Brost goto err_free; 369f86ad0edSMatthew Brost } 370f86ad0edSMatthew Brost 371f86ad0edSMatthew Brost if (migrate.cpages != npages) { 372f86ad0edSMatthew Brost err = -EBUSY; 373f86ad0edSMatthew Brost goto err_finalize; 374f86ad0edSMatthew Brost } 375f86ad0edSMatthew Brost 376f86ad0edSMatthew Brost err = ops->populate_devmem_pfn(devmem_allocation, npages, migrate.dst); 377f86ad0edSMatthew Brost if (err) 378f86ad0edSMatthew Brost goto err_finalize; 379f86ad0edSMatthew Brost 380f86ad0edSMatthew Brost err = drm_pagemap_migrate_map_pages(devmem_allocation->dev, dma_addr, 381f86ad0edSMatthew Brost migrate.src, npages, DMA_TO_DEVICE); 382f86ad0edSMatthew Brost if (err) 383f86ad0edSMatthew Brost goto err_finalize; 384f86ad0edSMatthew Brost 385f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) { 386f86ad0edSMatthew Brost struct page *page = pfn_to_page(migrate.dst[i]); 387f86ad0edSMatthew Brost 388f86ad0edSMatthew Brost pages[i] = page; 389f86ad0edSMatthew Brost migrate.dst[i] = migrate_pfn(migrate.dst[i]); 390f86ad0edSMatthew Brost drm_pagemap_get_devmem_page(page, zdd); 391f86ad0edSMatthew Brost } 392f86ad0edSMatthew Brost 393f86ad0edSMatthew Brost err = ops->copy_to_devmem(pages, dma_addr, npages); 394f86ad0edSMatthew Brost if (err) 395f86ad0edSMatthew Brost goto err_finalize; 396f86ad0edSMatthew Brost 397f86ad0edSMatthew Brost /* Upon success bind devmem allocation to range and zdd */ 398f86ad0edSMatthew Brost devmem_allocation->timeslice_expiration = get_jiffies_64() + 399f86ad0edSMatthew Brost msecs_to_jiffies(timeslice_ms); 400f86ad0edSMatthew Brost zdd->devmem_allocation = devmem_allocation; /* Owns ref */ 401f86ad0edSMatthew Brost 402f86ad0edSMatthew Brost err_finalize: 403f86ad0edSMatthew Brost if (err) 404f86ad0edSMatthew Brost drm_pagemap_migration_unlock_put_pages(npages, migrate.dst); 405f86ad0edSMatthew Brost migrate_vma_pages(&migrate); 406f86ad0edSMatthew Brost migrate_vma_finalize(&migrate); 407f86ad0edSMatthew Brost drm_pagemap_migrate_unmap_pages(devmem_allocation->dev, dma_addr, npages, 408f86ad0edSMatthew Brost DMA_TO_DEVICE); 409f86ad0edSMatthew Brost err_free: 410f86ad0edSMatthew Brost if (zdd) 411f86ad0edSMatthew Brost drm_pagemap_zdd_put(zdd); 412f86ad0edSMatthew Brost kvfree(buf); 413f86ad0edSMatthew Brost err_out: 414f86ad0edSMatthew Brost return err; 415f86ad0edSMatthew Brost } 416f86ad0edSMatthew Brost EXPORT_SYMBOL_GPL(drm_pagemap_migrate_to_devmem); 417f86ad0edSMatthew Brost 418f86ad0edSMatthew Brost /** 419f86ad0edSMatthew Brost * drm_pagemap_migrate_populate_ram_pfn() - Populate RAM PFNs for a VM area 420f86ad0edSMatthew Brost * @vas: Pointer to the VM area structure, can be NULL 421f86ad0edSMatthew Brost * @fault_page: Fault page 422f86ad0edSMatthew Brost * @npages: Number of pages to populate 423f86ad0edSMatthew Brost * @mpages: Number of pages to migrate 424f86ad0edSMatthew Brost * @src_mpfn: Source array of migrate PFNs 425f86ad0edSMatthew Brost * @mpfn: Array of migrate PFNs to populate 426f86ad0edSMatthew Brost * @addr: Start address for PFN allocation 427f86ad0edSMatthew Brost * 428f86ad0edSMatthew Brost * This function populates the RAM migrate page frame numbers (PFNs) for the 429f86ad0edSMatthew Brost * specified VM area structure. It allocates and locks pages in the VM area for 430f86ad0edSMatthew Brost * RAM usage. If vas is non-NULL use alloc_page_vma for allocation, if NULL use 431f86ad0edSMatthew Brost * alloc_page for allocation. 432f86ad0edSMatthew Brost * 433f86ad0edSMatthew Brost * Return: 0 on success, negative error code on failure. 434f86ad0edSMatthew Brost */ 435f86ad0edSMatthew Brost static int drm_pagemap_migrate_populate_ram_pfn(struct vm_area_struct *vas, 436f86ad0edSMatthew Brost struct page *fault_page, 437f86ad0edSMatthew Brost unsigned long npages, 438f86ad0edSMatthew Brost unsigned long *mpages, 439f86ad0edSMatthew Brost unsigned long *src_mpfn, 440f86ad0edSMatthew Brost unsigned long *mpfn, 441f86ad0edSMatthew Brost unsigned long addr) 442f86ad0edSMatthew Brost { 443f86ad0edSMatthew Brost unsigned long i; 444f86ad0edSMatthew Brost 445f86ad0edSMatthew Brost for (i = 0; i < npages; ++i, addr += PAGE_SIZE) { 446f86ad0edSMatthew Brost struct page *page, *src_page; 447f86ad0edSMatthew Brost 448f86ad0edSMatthew Brost if (!(src_mpfn[i] & MIGRATE_PFN_MIGRATE)) 449f86ad0edSMatthew Brost continue; 450f86ad0edSMatthew Brost 451f86ad0edSMatthew Brost src_page = migrate_pfn_to_page(src_mpfn[i]); 452f86ad0edSMatthew Brost if (!src_page) 453f86ad0edSMatthew Brost continue; 454f86ad0edSMatthew Brost 455f86ad0edSMatthew Brost if (fault_page) { 456f86ad0edSMatthew Brost if (src_page->zone_device_data != 457f86ad0edSMatthew Brost fault_page->zone_device_data) 458f86ad0edSMatthew Brost continue; 459f86ad0edSMatthew Brost } 460f86ad0edSMatthew Brost 461f86ad0edSMatthew Brost if (vas) 462f86ad0edSMatthew Brost page = alloc_page_vma(GFP_HIGHUSER, vas, addr); 463f86ad0edSMatthew Brost else 464f86ad0edSMatthew Brost page = alloc_page(GFP_HIGHUSER); 465f86ad0edSMatthew Brost 466f86ad0edSMatthew Brost if (!page) 467f86ad0edSMatthew Brost goto free_pages; 468f86ad0edSMatthew Brost 469f86ad0edSMatthew Brost mpfn[i] = migrate_pfn(page_to_pfn(page)); 470f86ad0edSMatthew Brost } 471f86ad0edSMatthew Brost 472f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) { 473f86ad0edSMatthew Brost struct page *page = migrate_pfn_to_page(mpfn[i]); 474f86ad0edSMatthew Brost 475f86ad0edSMatthew Brost if (!page) 476f86ad0edSMatthew Brost continue; 477f86ad0edSMatthew Brost 478f86ad0edSMatthew Brost WARN_ON_ONCE(!trylock_page(page)); 479f86ad0edSMatthew Brost ++*mpages; 480f86ad0edSMatthew Brost } 481f86ad0edSMatthew Brost 482f86ad0edSMatthew Brost return 0; 483f86ad0edSMatthew Brost 484f86ad0edSMatthew Brost free_pages: 485f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) { 486f86ad0edSMatthew Brost struct page *page = migrate_pfn_to_page(mpfn[i]); 487f86ad0edSMatthew Brost 488f86ad0edSMatthew Brost if (!page) 489f86ad0edSMatthew Brost continue; 490f86ad0edSMatthew Brost 491f86ad0edSMatthew Brost put_page(page); 492f86ad0edSMatthew Brost mpfn[i] = 0; 493f86ad0edSMatthew Brost } 494f86ad0edSMatthew Brost return -ENOMEM; 495f86ad0edSMatthew Brost } 496f86ad0edSMatthew Brost 497f86ad0edSMatthew Brost /** 498f86ad0edSMatthew Brost * drm_pagemap_evict_to_ram() - Evict GPU SVM range to RAM 499f86ad0edSMatthew Brost * @devmem_allocation: Pointer to the device memory allocation 500f86ad0edSMatthew Brost * 501f86ad0edSMatthew Brost * Similar to __drm_pagemap_migrate_to_ram but does not require mmap lock and 502f86ad0edSMatthew Brost * migration done via migrate_device_* functions. 503f86ad0edSMatthew Brost * 504f86ad0edSMatthew Brost * Return: 0 on success, negative error code on failure. 505f86ad0edSMatthew Brost */ 506f86ad0edSMatthew Brost int drm_pagemap_evict_to_ram(struct drm_pagemap_devmem *devmem_allocation) 507f86ad0edSMatthew Brost { 508f86ad0edSMatthew Brost const struct drm_pagemap_devmem_ops *ops = devmem_allocation->ops; 509f86ad0edSMatthew Brost unsigned long npages, mpages = 0; 510f86ad0edSMatthew Brost struct page **pages; 511f86ad0edSMatthew Brost unsigned long *src, *dst; 512f86ad0edSMatthew Brost dma_addr_t *dma_addr; 513f86ad0edSMatthew Brost void *buf; 514f86ad0edSMatthew Brost int i, err = 0; 515f86ad0edSMatthew Brost unsigned int retry_count = 2; 516f86ad0edSMatthew Brost 517f86ad0edSMatthew Brost npages = devmem_allocation->size >> PAGE_SHIFT; 518f86ad0edSMatthew Brost 519f86ad0edSMatthew Brost retry: 520f86ad0edSMatthew Brost if (!mmget_not_zero(devmem_allocation->mm)) 521f86ad0edSMatthew Brost return -EFAULT; 522f86ad0edSMatthew Brost 523f86ad0edSMatthew Brost buf = kvcalloc(npages, 2 * sizeof(*src) + sizeof(*dma_addr) + 524f86ad0edSMatthew Brost sizeof(*pages), GFP_KERNEL); 525f86ad0edSMatthew Brost if (!buf) { 526f86ad0edSMatthew Brost err = -ENOMEM; 527f86ad0edSMatthew Brost goto err_out; 528f86ad0edSMatthew Brost } 529f86ad0edSMatthew Brost src = buf; 530f86ad0edSMatthew Brost dst = buf + (sizeof(*src) * npages); 531f86ad0edSMatthew Brost dma_addr = buf + (2 * sizeof(*src) * npages); 532f86ad0edSMatthew Brost pages = buf + (2 * sizeof(*src) + sizeof(*dma_addr)) * npages; 533f86ad0edSMatthew Brost 534f86ad0edSMatthew Brost err = ops->populate_devmem_pfn(devmem_allocation, npages, src); 535f86ad0edSMatthew Brost if (err) 536f86ad0edSMatthew Brost goto err_free; 537f86ad0edSMatthew Brost 538f86ad0edSMatthew Brost err = migrate_device_pfns(src, npages); 539f86ad0edSMatthew Brost if (err) 540f86ad0edSMatthew Brost goto err_free; 541f86ad0edSMatthew Brost 542f86ad0edSMatthew Brost err = drm_pagemap_migrate_populate_ram_pfn(NULL, NULL, npages, &mpages, 543f86ad0edSMatthew Brost src, dst, 0); 544f86ad0edSMatthew Brost if (err || !mpages) 545f86ad0edSMatthew Brost goto err_finalize; 546f86ad0edSMatthew Brost 547f86ad0edSMatthew Brost err = drm_pagemap_migrate_map_pages(devmem_allocation->dev, dma_addr, 548f86ad0edSMatthew Brost dst, npages, DMA_FROM_DEVICE); 549f86ad0edSMatthew Brost if (err) 550f86ad0edSMatthew Brost goto err_finalize; 551f86ad0edSMatthew Brost 552f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) 553f86ad0edSMatthew Brost pages[i] = migrate_pfn_to_page(src[i]); 554f86ad0edSMatthew Brost 555f86ad0edSMatthew Brost err = ops->copy_to_ram(pages, dma_addr, npages); 556f86ad0edSMatthew Brost if (err) 557f86ad0edSMatthew Brost goto err_finalize; 558f86ad0edSMatthew Brost 559f86ad0edSMatthew Brost err_finalize: 560f86ad0edSMatthew Brost if (err) 561f86ad0edSMatthew Brost drm_pagemap_migration_unlock_put_pages(npages, dst); 562f86ad0edSMatthew Brost migrate_device_pages(src, dst, npages); 563f86ad0edSMatthew Brost migrate_device_finalize(src, dst, npages); 564f86ad0edSMatthew Brost drm_pagemap_migrate_unmap_pages(devmem_allocation->dev, dma_addr, npages, 565f86ad0edSMatthew Brost DMA_FROM_DEVICE); 566f86ad0edSMatthew Brost err_free: 567f86ad0edSMatthew Brost kvfree(buf); 568f86ad0edSMatthew Brost err_out: 569f86ad0edSMatthew Brost mmput_async(devmem_allocation->mm); 570f86ad0edSMatthew Brost 571f86ad0edSMatthew Brost if (completion_done(&devmem_allocation->detached)) 572f86ad0edSMatthew Brost return 0; 573f86ad0edSMatthew Brost 574f86ad0edSMatthew Brost if (retry_count--) { 575f86ad0edSMatthew Brost cond_resched(); 576f86ad0edSMatthew Brost goto retry; 577f86ad0edSMatthew Brost } 578f86ad0edSMatthew Brost 579f86ad0edSMatthew Brost return err ?: -EBUSY; 580f86ad0edSMatthew Brost } 581f86ad0edSMatthew Brost EXPORT_SYMBOL_GPL(drm_pagemap_evict_to_ram); 582f86ad0edSMatthew Brost 583f86ad0edSMatthew Brost /** 584f86ad0edSMatthew Brost * __drm_pagemap_migrate_to_ram() - Migrate GPU SVM range to RAM (internal) 585f86ad0edSMatthew Brost * @vas: Pointer to the VM area structure 586f86ad0edSMatthew Brost * @device_private_page_owner: Device private pages owner 587f86ad0edSMatthew Brost * @page: Pointer to the page for fault handling (can be NULL) 588f86ad0edSMatthew Brost * @fault_addr: Fault address 589f86ad0edSMatthew Brost * @size: Size of migration 590f86ad0edSMatthew Brost * 591f86ad0edSMatthew Brost * This internal function performs the migration of the specified GPU SVM range 592f86ad0edSMatthew Brost * to RAM. It sets up the migration, populates + dma maps RAM PFNs, and 593f86ad0edSMatthew Brost * invokes the driver-specific operations for migration to RAM. 594f86ad0edSMatthew Brost * 595f86ad0edSMatthew Brost * Return: 0 on success, negative error code on failure. 596f86ad0edSMatthew Brost */ 597f86ad0edSMatthew Brost static int __drm_pagemap_migrate_to_ram(struct vm_area_struct *vas, 598f86ad0edSMatthew Brost void *device_private_page_owner, 599f86ad0edSMatthew Brost struct page *page, 600f86ad0edSMatthew Brost unsigned long fault_addr, 601f86ad0edSMatthew Brost unsigned long size) 602f86ad0edSMatthew Brost { 603f86ad0edSMatthew Brost struct migrate_vma migrate = { 604f86ad0edSMatthew Brost .vma = vas, 605f86ad0edSMatthew Brost .pgmap_owner = device_private_page_owner, 606f86ad0edSMatthew Brost .flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE | 607f86ad0edSMatthew Brost MIGRATE_VMA_SELECT_DEVICE_COHERENT, 608f86ad0edSMatthew Brost .fault_page = page, 609f86ad0edSMatthew Brost }; 610f86ad0edSMatthew Brost struct drm_pagemap_zdd *zdd; 611f86ad0edSMatthew Brost const struct drm_pagemap_devmem_ops *ops; 612f86ad0edSMatthew Brost struct device *dev = NULL; 613f86ad0edSMatthew Brost unsigned long npages, mpages = 0; 614f86ad0edSMatthew Brost struct page **pages; 615f86ad0edSMatthew Brost dma_addr_t *dma_addr; 616f86ad0edSMatthew Brost unsigned long start, end; 617f86ad0edSMatthew Brost void *buf; 618f86ad0edSMatthew Brost int i, err = 0; 619f86ad0edSMatthew Brost 620f86ad0edSMatthew Brost if (page) { 621f86ad0edSMatthew Brost zdd = page->zone_device_data; 622f86ad0edSMatthew Brost if (time_before64(get_jiffies_64(), 623f86ad0edSMatthew Brost zdd->devmem_allocation->timeslice_expiration)) 624f86ad0edSMatthew Brost return 0; 625f86ad0edSMatthew Brost } 626f86ad0edSMatthew Brost 627f86ad0edSMatthew Brost start = ALIGN_DOWN(fault_addr, size); 628f86ad0edSMatthew Brost end = ALIGN(fault_addr + 1, size); 629f86ad0edSMatthew Brost 630f86ad0edSMatthew Brost /* Corner where VMA area struct has been partially unmapped */ 631f86ad0edSMatthew Brost if (start < vas->vm_start) 632f86ad0edSMatthew Brost start = vas->vm_start; 633f86ad0edSMatthew Brost if (end > vas->vm_end) 634f86ad0edSMatthew Brost end = vas->vm_end; 635f86ad0edSMatthew Brost 636f86ad0edSMatthew Brost migrate.start = start; 637f86ad0edSMatthew Brost migrate.end = end; 638f86ad0edSMatthew Brost npages = npages_in_range(start, end); 639f86ad0edSMatthew Brost 640f86ad0edSMatthew Brost buf = kvcalloc(npages, 2 * sizeof(*migrate.src) + sizeof(*dma_addr) + 641f86ad0edSMatthew Brost sizeof(*pages), GFP_KERNEL); 642f86ad0edSMatthew Brost if (!buf) { 643f86ad0edSMatthew Brost err = -ENOMEM; 644f86ad0edSMatthew Brost goto err_out; 645f86ad0edSMatthew Brost } 646f86ad0edSMatthew Brost dma_addr = buf + (2 * sizeof(*migrate.src) * npages); 647f86ad0edSMatthew Brost pages = buf + (2 * sizeof(*migrate.src) + sizeof(*dma_addr)) * npages; 648f86ad0edSMatthew Brost 649f86ad0edSMatthew Brost migrate.vma = vas; 650f86ad0edSMatthew Brost migrate.src = buf; 651f86ad0edSMatthew Brost migrate.dst = migrate.src + npages; 652f86ad0edSMatthew Brost 653f86ad0edSMatthew Brost err = migrate_vma_setup(&migrate); 654f86ad0edSMatthew Brost if (err) 655f86ad0edSMatthew Brost goto err_free; 656f86ad0edSMatthew Brost 657f86ad0edSMatthew Brost /* Raced with another CPU fault, nothing to do */ 658f86ad0edSMatthew Brost if (!migrate.cpages) 659f86ad0edSMatthew Brost goto err_free; 660f86ad0edSMatthew Brost 661f86ad0edSMatthew Brost if (!page) { 662f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) { 663f86ad0edSMatthew Brost if (!(migrate.src[i] & MIGRATE_PFN_MIGRATE)) 664f86ad0edSMatthew Brost continue; 665f86ad0edSMatthew Brost 666f86ad0edSMatthew Brost page = migrate_pfn_to_page(migrate.src[i]); 667f86ad0edSMatthew Brost break; 668f86ad0edSMatthew Brost } 669f86ad0edSMatthew Brost 670f86ad0edSMatthew Brost if (!page) 671f86ad0edSMatthew Brost goto err_finalize; 672f86ad0edSMatthew Brost } 673f86ad0edSMatthew Brost zdd = page->zone_device_data; 674f86ad0edSMatthew Brost ops = zdd->devmem_allocation->ops; 675f86ad0edSMatthew Brost dev = zdd->devmem_allocation->dev; 676f86ad0edSMatthew Brost 677f86ad0edSMatthew Brost err = drm_pagemap_migrate_populate_ram_pfn(vas, page, npages, &mpages, 678f86ad0edSMatthew Brost migrate.src, migrate.dst, 679f86ad0edSMatthew Brost start); 680f86ad0edSMatthew Brost if (err) 681f86ad0edSMatthew Brost goto err_finalize; 682f86ad0edSMatthew Brost 683f86ad0edSMatthew Brost err = drm_pagemap_migrate_map_pages(dev, dma_addr, migrate.dst, npages, 684f86ad0edSMatthew Brost DMA_FROM_DEVICE); 685f86ad0edSMatthew Brost if (err) 686f86ad0edSMatthew Brost goto err_finalize; 687f86ad0edSMatthew Brost 688f86ad0edSMatthew Brost for (i = 0; i < npages; ++i) 689f86ad0edSMatthew Brost pages[i] = migrate_pfn_to_page(migrate.src[i]); 690f86ad0edSMatthew Brost 691f86ad0edSMatthew Brost err = ops->copy_to_ram(pages, dma_addr, npages); 692f86ad0edSMatthew Brost if (err) 693f86ad0edSMatthew Brost goto err_finalize; 694f86ad0edSMatthew Brost 695f86ad0edSMatthew Brost err_finalize: 696f86ad0edSMatthew Brost if (err) 697f86ad0edSMatthew Brost drm_pagemap_migration_unlock_put_pages(npages, migrate.dst); 698f86ad0edSMatthew Brost migrate_vma_pages(&migrate); 699f86ad0edSMatthew Brost migrate_vma_finalize(&migrate); 700f86ad0edSMatthew Brost if (dev) 701f86ad0edSMatthew Brost drm_pagemap_migrate_unmap_pages(dev, dma_addr, npages, 702f86ad0edSMatthew Brost DMA_FROM_DEVICE); 703f86ad0edSMatthew Brost err_free: 704f86ad0edSMatthew Brost kvfree(buf); 705f86ad0edSMatthew Brost err_out: 706f86ad0edSMatthew Brost 707f86ad0edSMatthew Brost return err; 708f86ad0edSMatthew Brost } 709f86ad0edSMatthew Brost 710f86ad0edSMatthew Brost /** 711f86ad0edSMatthew Brost * drm_pagemap_page_free() - Put GPU SVM zone device data associated with a page 712f86ad0edSMatthew Brost * @page: Pointer to the page 713f86ad0edSMatthew Brost * 714f86ad0edSMatthew Brost * This function is a callback used to put the GPU SVM zone device data 715f86ad0edSMatthew Brost * associated with a page when it is being released. 716f86ad0edSMatthew Brost */ 717f86ad0edSMatthew Brost static void drm_pagemap_page_free(struct page *page) 718f86ad0edSMatthew Brost { 719f86ad0edSMatthew Brost drm_pagemap_zdd_put(page->zone_device_data); 720f86ad0edSMatthew Brost } 721f86ad0edSMatthew Brost 722f86ad0edSMatthew Brost /** 723f86ad0edSMatthew Brost * drm_pagemap_migrate_to_ram() - Migrate a virtual range to RAM (page fault handler) 724f86ad0edSMatthew Brost * @vmf: Pointer to the fault information structure 725f86ad0edSMatthew Brost * 726f86ad0edSMatthew Brost * This function is a page fault handler used to migrate a virtual range 727f86ad0edSMatthew Brost * to ram. The device memory allocation in which the device page is found is 728f86ad0edSMatthew Brost * migrated in its entirety. 729f86ad0edSMatthew Brost * 730f86ad0edSMatthew Brost * Returns: 731f86ad0edSMatthew Brost * VM_FAULT_SIGBUS on failure, 0 on success. 732f86ad0edSMatthew Brost */ 733f86ad0edSMatthew Brost static vm_fault_t drm_pagemap_migrate_to_ram(struct vm_fault *vmf) 734f86ad0edSMatthew Brost { 735f86ad0edSMatthew Brost struct drm_pagemap_zdd *zdd = vmf->page->zone_device_data; 736f86ad0edSMatthew Brost int err; 737f86ad0edSMatthew Brost 738f86ad0edSMatthew Brost err = __drm_pagemap_migrate_to_ram(vmf->vma, 739f86ad0edSMatthew Brost zdd->device_private_page_owner, 740f86ad0edSMatthew Brost vmf->page, vmf->address, 741f86ad0edSMatthew Brost zdd->devmem_allocation->size); 742f86ad0edSMatthew Brost 743f86ad0edSMatthew Brost return err ? VM_FAULT_SIGBUS : 0; 744f86ad0edSMatthew Brost } 745f86ad0edSMatthew Brost 746f86ad0edSMatthew Brost static const struct dev_pagemap_ops drm_pagemap_pagemap_ops = { 747f86ad0edSMatthew Brost .page_free = drm_pagemap_page_free, 748f86ad0edSMatthew Brost .migrate_to_ram = drm_pagemap_migrate_to_ram, 749f86ad0edSMatthew Brost }; 750f86ad0edSMatthew Brost 751f86ad0edSMatthew Brost /** 752f86ad0edSMatthew Brost * drm_pagemap_pagemap_ops_get() - Retrieve GPU SVM device page map operations 753f86ad0edSMatthew Brost * 754f86ad0edSMatthew Brost * Returns: 755f86ad0edSMatthew Brost * Pointer to the GPU SVM device page map operations structure. 756f86ad0edSMatthew Brost */ 757f86ad0edSMatthew Brost const struct dev_pagemap_ops *drm_pagemap_pagemap_ops_get(void) 758f86ad0edSMatthew Brost { 759f86ad0edSMatthew Brost return &drm_pagemap_pagemap_ops; 760f86ad0edSMatthew Brost } 761f86ad0edSMatthew Brost EXPORT_SYMBOL_GPL(drm_pagemap_pagemap_ops_get); 762f86ad0edSMatthew Brost 763f86ad0edSMatthew Brost /** 764f86ad0edSMatthew Brost * drm_pagemap_devmem_init() - Initialize a drm_pagemap device memory allocation 765f86ad0edSMatthew Brost * 766f86ad0edSMatthew Brost * @devmem_allocation: The struct drm_pagemap_devmem to initialize. 767f86ad0edSMatthew Brost * @dev: Pointer to the device structure which device memory allocation belongs to 768f86ad0edSMatthew Brost * @mm: Pointer to the mm_struct for the address space 769f86ad0edSMatthew Brost * @ops: Pointer to the operations structure for GPU SVM device memory 770f86ad0edSMatthew Brost * @dpagemap: The struct drm_pagemap we're allocating from. 771f86ad0edSMatthew Brost * @size: Size of device memory allocation 772f86ad0edSMatthew Brost */ 773f86ad0edSMatthew Brost void drm_pagemap_devmem_init(struct drm_pagemap_devmem *devmem_allocation, 774f86ad0edSMatthew Brost struct device *dev, struct mm_struct *mm, 775f86ad0edSMatthew Brost const struct drm_pagemap_devmem_ops *ops, 776f86ad0edSMatthew Brost struct drm_pagemap *dpagemap, size_t size) 777f86ad0edSMatthew Brost { 778f86ad0edSMatthew Brost init_completion(&devmem_allocation->detached); 779f86ad0edSMatthew Brost devmem_allocation->dev = dev; 780f86ad0edSMatthew Brost devmem_allocation->mm = mm; 781f86ad0edSMatthew Brost devmem_allocation->ops = ops; 782f86ad0edSMatthew Brost devmem_allocation->dpagemap = dpagemap; 783f86ad0edSMatthew Brost devmem_allocation->size = size; 784f86ad0edSMatthew Brost } 785f86ad0edSMatthew Brost EXPORT_SYMBOL_GPL(drm_pagemap_devmem_init); 786f86ad0edSMatthew Brost 787f86ad0edSMatthew Brost /** 788f86ad0edSMatthew Brost * drm_pagemap_page_to_dpagemap() - Return a pointer the drm_pagemap of a page 789f86ad0edSMatthew Brost * @page: The struct page. 790f86ad0edSMatthew Brost * 791f86ad0edSMatthew Brost * Return: A pointer to the struct drm_pagemap of a device private page that 792f86ad0edSMatthew Brost * was populated from the struct drm_pagemap. If the page was *not* populated 793f86ad0edSMatthew Brost * from a struct drm_pagemap, the result is undefined and the function call 794f86ad0edSMatthew Brost * may result in dereferencing and invalid address. 795f86ad0edSMatthew Brost */ 796f86ad0edSMatthew Brost struct drm_pagemap *drm_pagemap_page_to_dpagemap(struct page *page) 797f86ad0edSMatthew Brost { 798f86ad0edSMatthew Brost struct drm_pagemap_zdd *zdd = page->zone_device_data; 799f86ad0edSMatthew Brost 800f86ad0edSMatthew Brost return zdd->devmem_allocation->dpagemap; 801f86ad0edSMatthew Brost } 802f86ad0edSMatthew Brost EXPORT_SYMBOL_GPL(drm_pagemap_page_to_dpagemap); 803*2ef19be2SThomas Hellström 804*2ef19be2SThomas Hellström /** 805*2ef19be2SThomas Hellström * drm_pagemap_populate_mm() - Populate a virtual range with device memory pages 806*2ef19be2SThomas Hellström * @dpagemap: Pointer to the drm_pagemap managing the device memory 807*2ef19be2SThomas Hellström * @start: Start of the virtual range to populate. 808*2ef19be2SThomas Hellström * @end: End of the virtual range to populate. 809*2ef19be2SThomas Hellström * @mm: Pointer to the virtual address space. 810*2ef19be2SThomas Hellström * @timeslice_ms: The time requested for the migrated pagemap pages to 811*2ef19be2SThomas Hellström * be present in @mm before being allowed to be migrated back. 812*2ef19be2SThomas Hellström * 813*2ef19be2SThomas Hellström * Attempt to populate a virtual range with device memory pages, 814*2ef19be2SThomas Hellström * clearing them or migrating data from the existing pages if necessary. 815*2ef19be2SThomas Hellström * The function is best effort only, and implementations may vary 816*2ef19be2SThomas Hellström * in how hard they try to satisfy the request. 817*2ef19be2SThomas Hellström * 818*2ef19be2SThomas Hellström * Return: %0 on success, negative error code on error. If the hardware 819*2ef19be2SThomas Hellström * device was removed / unbound the function will return %-ENODEV. 820*2ef19be2SThomas Hellström */ 821*2ef19be2SThomas Hellström int drm_pagemap_populate_mm(struct drm_pagemap *dpagemap, 822*2ef19be2SThomas Hellström unsigned long start, unsigned long end, 823*2ef19be2SThomas Hellström struct mm_struct *mm, 824*2ef19be2SThomas Hellström unsigned long timeslice_ms) 825*2ef19be2SThomas Hellström { 826*2ef19be2SThomas Hellström int err; 827*2ef19be2SThomas Hellström 828*2ef19be2SThomas Hellström if (!mmget_not_zero(mm)) 829*2ef19be2SThomas Hellström return -EFAULT; 830*2ef19be2SThomas Hellström mmap_read_lock(mm); 831*2ef19be2SThomas Hellström err = dpagemap->ops->populate_mm(dpagemap, start, end, mm, 832*2ef19be2SThomas Hellström timeslice_ms); 833*2ef19be2SThomas Hellström mmap_read_unlock(mm); 834*2ef19be2SThomas Hellström mmput(mm); 835*2ef19be2SThomas Hellström 836*2ef19be2SThomas Hellström return err; 837*2ef19be2SThomas Hellström } 838