18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved. 38c2ecf20Sopenharmony_ci * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two 68c2ecf20Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 78c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 88c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the 98c2ecf20Sopenharmony_ci * OpenIB.org BSD license below: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or 128c2ecf20Sopenharmony_ci * without modification, are permitted provided that the following 138c2ecf20Sopenharmony_ci * conditions are met: 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above 168c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 178c2ecf20Sopenharmony_ci * disclaimer. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above 208c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 218c2ecf20Sopenharmony_ci * disclaimer in the documentation and/or other materials 228c2ecf20Sopenharmony_ci * provided with the distribution. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 258c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 268c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 278c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 288c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 298c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 308c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 318c2ecf20Sopenharmony_ci * SOFTWARE. 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#include <linux/mm.h> 358c2ecf20Sopenharmony_ci#include <linux/sched/signal.h> 368c2ecf20Sopenharmony_ci#include <linux/device.h> 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#include "qib.h" 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic void __qib_release_user_pages(struct page **p, size_t num_pages, 418c2ecf20Sopenharmony_ci int dirty) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci unpin_user_pages_dirty_lock(p, num_pages, dirty); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/** 478c2ecf20Sopenharmony_ci * qib_map_page - a safety wrapper around pci_map_page() 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * A dma_addr of all 0's is interpreted by the chip as "disabled". 508c2ecf20Sopenharmony_ci * Unfortunately, it can also be a valid dma_addr returned on some 518c2ecf20Sopenharmony_ci * architectures. 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * The powerpc iommu assigns dma_addrs in ascending order, so we don't 548c2ecf20Sopenharmony_ci * have to bother with retries or mapping a dummy page to insure we 558c2ecf20Sopenharmony_ci * don't just get the same mapping again. 568c2ecf20Sopenharmony_ci * 578c2ecf20Sopenharmony_ci * I'm sure we won't be so lucky with other iommu's, so FIXME. 588c2ecf20Sopenharmony_ci */ 598c2ecf20Sopenharmony_ciint qib_map_page(struct pci_dev *hwdev, struct page *page, dma_addr_t *daddr) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci dma_addr_t phys; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci phys = pci_map_page(hwdev, page, 0, PAGE_SIZE, PCI_DMA_FROMDEVICE); 648c2ecf20Sopenharmony_ci if (pci_dma_mapping_error(hwdev, phys)) 658c2ecf20Sopenharmony_ci return -ENOMEM; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (!phys) { 688c2ecf20Sopenharmony_ci pci_unmap_page(hwdev, phys, PAGE_SIZE, PCI_DMA_FROMDEVICE); 698c2ecf20Sopenharmony_ci phys = pci_map_page(hwdev, page, 0, PAGE_SIZE, 708c2ecf20Sopenharmony_ci PCI_DMA_FROMDEVICE); 718c2ecf20Sopenharmony_ci if (pci_dma_mapping_error(hwdev, phys)) 728c2ecf20Sopenharmony_ci return -ENOMEM; 738c2ecf20Sopenharmony_ci /* 748c2ecf20Sopenharmony_ci * FIXME: If we get 0 again, we should keep this page, 758c2ecf20Sopenharmony_ci * map another, then free the 0 page. 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci *daddr = phys; 798c2ecf20Sopenharmony_ci return 0; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci/** 838c2ecf20Sopenharmony_ci * qib_get_user_pages - lock user pages into memory 848c2ecf20Sopenharmony_ci * @start_page: the start page 858c2ecf20Sopenharmony_ci * @num_pages: the number of pages 868c2ecf20Sopenharmony_ci * @p: the output page structures 878c2ecf20Sopenharmony_ci * 888c2ecf20Sopenharmony_ci * This function takes a given start page (page aligned user virtual 898c2ecf20Sopenharmony_ci * address) and pins it and the following specified number of pages. For 908c2ecf20Sopenharmony_ci * now, num_pages is always 1, but that will probably change at some point 918c2ecf20Sopenharmony_ci * (because caller is doing expected sends on a single virtually contiguous 928c2ecf20Sopenharmony_ci * buffer, so we can do all pages at once). 938c2ecf20Sopenharmony_ci */ 948c2ecf20Sopenharmony_ciint qib_get_user_pages(unsigned long start_page, size_t num_pages, 958c2ecf20Sopenharmony_ci struct page **p) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci unsigned long locked, lock_limit; 988c2ecf20Sopenharmony_ci size_t got; 998c2ecf20Sopenharmony_ci int ret; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1028c2ecf20Sopenharmony_ci locked = atomic64_add_return(num_pages, ¤t->mm->pinned_vm); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { 1058c2ecf20Sopenharmony_ci ret = -ENOMEM; 1068c2ecf20Sopenharmony_ci goto bail; 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci mmap_read_lock(current->mm); 1108c2ecf20Sopenharmony_ci for (got = 0; got < num_pages; got += ret) { 1118c2ecf20Sopenharmony_ci ret = pin_user_pages(start_page + got * PAGE_SIZE, 1128c2ecf20Sopenharmony_ci num_pages - got, 1138c2ecf20Sopenharmony_ci FOLL_LONGTERM | FOLL_WRITE | FOLL_FORCE, 1148c2ecf20Sopenharmony_ci p + got, NULL); 1158c2ecf20Sopenharmony_ci if (ret < 0) { 1168c2ecf20Sopenharmony_ci mmap_read_unlock(current->mm); 1178c2ecf20Sopenharmony_ci goto bail_release; 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci mmap_read_unlock(current->mm); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci return 0; 1238c2ecf20Sopenharmony_cibail_release: 1248c2ecf20Sopenharmony_ci __qib_release_user_pages(p, got, 0); 1258c2ecf20Sopenharmony_cibail: 1268c2ecf20Sopenharmony_ci atomic64_sub(num_pages, ¤t->mm->pinned_vm); 1278c2ecf20Sopenharmony_ci return ret; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_civoid qib_release_user_pages(struct page **p, size_t num_pages) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci __qib_release_user_pages(p, num_pages, 1); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci /* during close after signal, mm can be NULL */ 1358c2ecf20Sopenharmony_ci if (current->mm) 1368c2ecf20Sopenharmony_ci atomic64_sub(num_pages, ¤t->mm->pinned_vm); 1378c2ecf20Sopenharmony_ci} 138