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