2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
24 * Contact Information:
29 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * * Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * * Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in
40 * the documentation and/or other materials provided with the
42 * * Neither the name of Intel Corporation nor the names of its
43 * contributors may be used to endorse or promote products derived
44 * from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
49 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
50 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 #include <linux/module.h>
61 #include <linux/miscdevice.h>
63 #include <linux/device.h>
64 #include <linux/errno.h>
65 #include <linux/vmalloc.h>
70 #include <xen/xen-ops.h>
71 #include <xen/interface/memory.h>
73 #include <rte_config.h>
74 #include <exec-env/rte_dom0_common.h>
76 #include "dom0_mm_dev.h"
78 MODULE_LICENSE("Dual BSD/GPL");
79 MODULE_AUTHOR("Intel Corporation");
80 MODULE_DESCRIPTION("Kernel Module for supporting DPDK running on Xen Dom0");
82 static struct dom0_mm_dev dom0_dev;
83 static struct kobject *dom0_kobj = NULL;
85 static struct memblock_info *rsv_mm_info;
87 /* Default configuration for reserved memory size(2048 MB). */
88 static uint32_t rsv_memsize = 2048;
90 static int dom0_open(struct inode *inode, struct file *file);
91 static int dom0_release(struct inode *inode, struct file *file);
92 static int dom0_ioctl(struct file *file, unsigned int ioctl_num,
93 unsigned long ioctl_param);
94 static int dom0_mmap(struct file *file, struct vm_area_struct *vma);
95 static int dom0_memory_free(uint32_t size);
96 static int dom0_memory_release(struct dom0_mm_data *mm_data);
98 static const struct file_operations data_fops = {
101 .release = dom0_release,
103 .unlocked_ioctl = (void *)dom0_ioctl,
107 show_memsize_rsvd(struct device *dev, struct device_attribute *attr, char *buf)
109 return snprintf(buf, 10, "%u\n", dom0_dev.used_memsize);
113 show_memsize(struct device *dev, struct device_attribute *attr, char *buf)
115 return snprintf(buf, 10, "%u\n", dom0_dev.config_memsize);
119 store_memsize(struct device *dev, struct device_attribute *attr,
120 const char *buf, size_t count)
123 unsigned long mem_size;
125 if (0 != strict_strtoul(buf, 0, &mem_size))
128 mutex_lock(&dom0_dev.data_lock);
132 } else if (mem_size > (rsv_memsize - dom0_dev.used_memsize)) {
133 XEN_ERR("configure memory size fail\n");
137 dom0_dev.config_memsize = mem_size;
140 mutex_unlock(&dom0_dev.data_lock);
141 return err ? err : count;
144 static DEVICE_ATTR(memsize, S_IRUGO | S_IWUSR, show_memsize, store_memsize);
145 static DEVICE_ATTR(memsize_rsvd, S_IRUGO, show_memsize_rsvd, NULL);
147 static struct attribute *dev_attrs[] = {
148 &dev_attr_memsize.attr,
149 &dev_attr_memsize_rsvd.attr,
153 /* the memory size unit is MB */
154 static const struct attribute_group dev_attr_grp = {
155 .name = "memsize-mB",
161 sort_viraddr(struct memblock_info *mb, int cnt)
165 uint64_t tmp_viraddr;
167 /*sort virtual address and pfn */
168 for(i = 0; i < cnt; i ++) {
169 for(j = cnt - 1; j > i; j--) {
170 if(mb[j].pfn < mb[j - 1].pfn) {
171 tmp_pfn = mb[j - 1].pfn;
172 mb[j - 1].pfn = mb[j].pfn;
175 tmp_viraddr = mb[j - 1].vir_addr;
176 mb[j - 1].vir_addr = mb[j].vir_addr;
177 mb[j].vir_addr = tmp_viraddr;
184 dom0_find_memdata(const char * mem_name)
188 for(i = 0; i< NUM_MEM_CTX; i++) {
189 if(dom0_dev.mm_data[i] == NULL)
191 if (!strncmp(dom0_dev.mm_data[i]->name, mem_name,
192 sizeof(char) * DOM0_NAME_MAX)) {
202 dom0_find_mempos(void)
207 for(i = 0; i< NUM_MEM_CTX; i++) {
208 if(dom0_dev.mm_data[i] == NULL){
218 dom0_memory_release(struct dom0_mm_data *mm_data)
221 uint32_t num_block, block_id;
223 /* each memory block is 2M */
224 num_block = mm_data->mem_size / SIZE_PER_BLOCK;
228 /* reset global memory data */
229 idx = dom0_find_memdata(mm_data->name);
231 dom0_dev.used_memsize -= mm_data->mem_size;
232 dom0_dev.mm_data[idx] = NULL;
233 dom0_dev.num_mem_ctx--;
236 /* reset these memory blocks status as free */
237 for (idx = 0; idx < num_block; idx++) {
238 block_id = mm_data->block_num[idx];
239 rsv_mm_info[block_id].used = 0;
242 memset(mm_data, 0, sizeof(struct dom0_mm_data));
248 dom0_memory_free(uint32_t rsv_size)
250 uint64_t vstart, vaddr;
251 uint32_t i, num_block, size;
253 if (!xen_pv_domain())
256 /* each memory block is 2M */
257 num_block = rsv_size / SIZE_PER_BLOCK;
261 /* free all memory blocks of size of 4M and destroy contiguous region */
262 for (i = 0; i < dom0_dev.num_bigblock * 2; i += 2) {
263 vstart = rsv_mm_info[i].vir_addr;
265 if (rsv_mm_info[i].exchange_flag)
266 xen_destroy_contiguous_region(vstart,
267 DOM0_CONTIG_NUM_ORDER);
268 if (rsv_mm_info[i + 1].exchange_flag)
269 xen_destroy_contiguous_region(vstart +
271 DOM0_CONTIG_NUM_ORDER);
272 size = DOM0_MEMBLOCK_SIZE * 2;
275 ClearPageReserved(virt_to_page(vaddr));
279 free_pages(vstart, MAX_NUM_ORDER);
283 /* free all memory blocks size of 2M and destroy contiguous region */
284 for (; i < num_block; i++) {
285 vstart = rsv_mm_info[i].vir_addr;
287 if (rsv_mm_info[i].exchange_flag)
288 xen_destroy_contiguous_region(vstart,
289 DOM0_CONTIG_NUM_ORDER);
291 size = DOM0_MEMBLOCK_SIZE;
294 ClearPageReserved(virt_to_page(vaddr));
298 free_pages(vstart, DOM0_CONTIG_NUM_ORDER);
302 memset(rsv_mm_info, 0, sizeof(struct memblock_info) * num_block);
310 find_free_memory(uint32_t count, struct dom0_mm_data *mm_data)
315 while ((i < count) && (j < rsv_memsize / SIZE_PER_BLOCK)) {
316 if (rsv_mm_info[j].used == 0) {
317 mm_data->block_info[i].pfn = rsv_mm_info[j].pfn;
318 mm_data->block_info[i].vir_addr =
319 rsv_mm_info[j].vir_addr;
320 mm_data->block_info[i].mfn = rsv_mm_info[j].mfn;
321 mm_data->block_info[i].exchange_flag =
322 rsv_mm_info[j].exchange_flag;
323 mm_data->block_num[i] = j;
324 rsv_mm_info[j].used = 1;
332 * Find all memory segments in which physical addresses are contiguous.
335 find_memseg(int count, struct dom0_mm_data * mm_data)
339 uint64_t zone_len, pfn, num_block;
342 if (mm_data->block_info[i].exchange_flag == 0) {
347 pfn = mm_data->block_info[i].pfn;
348 mm_data->seg_info[idx].pfn = pfn;
349 mm_data->seg_info[idx].mfn[k] = mm_data->block_info[i].mfn;
351 for (j = i + 1; j < count; j++) {
353 /* ignore exchange fail memory block */
354 if (mm_data->block_info[j].exchange_flag == 0)
357 if (mm_data->block_info[j].pfn !=
358 (mm_data->block_info[j - 1].pfn +
359 DOM0_MEMBLOCK_SIZE / PAGE_SIZE))
362 mm_data->seg_info[idx].mfn[k] = mm_data->block_info[j].mfn;
366 zone_len = num_block * DOM0_MEMBLOCK_SIZE;
367 mm_data->seg_info[idx].size = zone_len;
369 XEN_PRINT("memseg id=%d, size=0x%llx\n", idx, zone_len);
372 if (idx == DOM0_NUM_MEMSEG)
375 mm_data->num_memseg = idx;
379 dom0_memory_reserve(uint32_t rsv_size)
381 uint64_t pfn, vstart, vaddr;
382 uint32_t i, num_block, size, allocated_size = 0;
384 /* 2M as memory block */
385 num_block = rsv_size / SIZE_PER_BLOCK;
387 rsv_mm_info = vmalloc(sizeof(struct memblock_info) * num_block);
389 XEN_ERR("Unable to allocate device memory information\n");
392 memset(rsv_mm_info, 0, sizeof(struct memblock_info) * num_block);
394 /* try alloc size of 4M once */
395 for (i = 0; i < num_block; i += 2) {
396 vstart = (unsigned long)
397 __get_free_pages(GFP_ATOMIC, MAX_NUM_ORDER);
401 dom0_dev.num_bigblock = i / 2 + 1;
402 allocated_size = SIZE_PER_BLOCK * (i + 2);
405 size = DOM0_MEMBLOCK_SIZE * 2;
409 SetPageReserved(virt_to_page(vaddr));
414 pfn = virt_to_pfn(vstart);
415 rsv_mm_info[i].pfn = pfn;
416 rsv_mm_info[i].vir_addr = vstart;
417 rsv_mm_info[i + 1].pfn =
418 pfn + DOM0_MEMBLOCK_SIZE / PAGE_SIZE;
419 rsv_mm_info[i + 1].vir_addr =
420 vstart + DOM0_MEMBLOCK_SIZE;
423 /*if it failed to alloc 4M, and continue to alloc 2M once */
424 for (; i < num_block; i++) {
425 vstart = (unsigned long)
426 __get_free_pages(GFP_ATOMIC, DOM0_CONTIG_NUM_ORDER);
428 XEN_ERR("allocate memory fail.\n");
429 dom0_memory_free(allocated_size);
433 allocated_size += DOM0_MEMBLOCK_SIZE;
435 size = DOM0_MEMBLOCK_SIZE;
438 SetPageReserved(virt_to_page(vaddr));
442 pfn = virt_to_pfn(vstart);
443 rsv_mm_info[i].pfn = pfn;
444 rsv_mm_info[i].vir_addr = vstart;
447 sort_viraddr(rsv_mm_info, num_block);
449 for (i = 0; i< num_block; i++) {
452 * This API is used to exchage MFN for getting a block of
453 * contiguous physical addresses, its maximum size is 2M.
455 if (xen_create_contiguous_region(rsv_mm_info[i].vir_addr,
456 DOM0_CONTIG_NUM_ORDER, 0) == 0) {
457 rsv_mm_info[i].exchange_flag = 1;
459 pfn_to_mfn(rsv_mm_info[i].pfn);
460 rsv_mm_info[i].used = 0;
462 XEN_ERR("exchange memeory fail\n");
463 rsv_mm_info[i].exchange_flag = 0;
464 dom0_dev.fail_times++;
465 if (dom0_dev.fail_times > MAX_EXCHANGE_FAIL_TIME) {
466 dom0_memory_free(rsv_size);
476 dom0_prepare_memsegs(struct memory_info *meminfo, struct dom0_mm_data *mm_data)
481 /* check if there is a free name buffer */
482 memcpy(mm_data->name, meminfo->name, DOM0_NAME_MAX);
483 mm_data->name[DOM0_NAME_MAX - 1] = '\0';
484 idx = dom0_find_mempos();
488 num_block = meminfo->size / SIZE_PER_BLOCK;
489 /* find free memory and new memory segments*/
490 find_free_memory(num_block, mm_data);
491 find_memseg(num_block, mm_data);
493 /* update private memory data */
495 mm_data->mem_size = meminfo->size;
497 /* update global memory data */
498 dom0_dev.mm_data[idx] = mm_data;
499 dom0_dev.num_mem_ctx++;
500 dom0_dev.used_memsize += mm_data->mem_size;
506 dom0_check_memory (struct memory_info *meminfo)
511 /* round memory size to the next even number. */
512 if (meminfo->size % 2)
515 mem_size = meminfo->size;
516 if (dom0_dev.num_mem_ctx > NUM_MEM_CTX) {
517 XEN_ERR("Memory data space is full in Dom0 driver\n");
520 idx = dom0_find_memdata(meminfo->name);
522 XEN_ERR("Memory data name %s has already exsited in Dom0 driver.\n",
526 if ((dom0_dev.used_memsize + mem_size) > rsv_memsize) {
527 XEN_ERR("Total size can't be larger than reserved size.\n");
540 if (rsv_memsize > DOM0_CONFIG_MEMSIZE) {
541 XEN_ERR("The reserved memory size cannot be greater than %d\n",
542 DOM0_CONFIG_MEMSIZE);
546 /* Setup the misc device */
547 dom0_dev.miscdev.minor = MISC_DYNAMIC_MINOR;
548 dom0_dev.miscdev.name = "dom0_mm";
549 dom0_dev.miscdev.fops = &data_fops;
551 /* register misc char device */
552 if (misc_register(&dom0_dev.miscdev) != 0) {
553 XEN_ERR("Misc device registration failed\n");
557 mutex_init(&dom0_dev.data_lock);
558 dom0_kobj = kobject_create_and_add("dom0-mm", mm_kobj);
561 XEN_ERR("dom0-mm object creation failed\n");
562 misc_deregister(&dom0_dev.miscdev);
566 if (sysfs_create_group(dom0_kobj, &dev_attr_grp)) {
567 kobject_put(dom0_kobj);
568 misc_deregister(&dom0_dev.miscdev);
572 if (dom0_memory_reserve(rsv_memsize) < 0) {
573 sysfs_remove_group(dom0_kobj, &dev_attr_grp);
574 kobject_put(dom0_kobj);
575 misc_deregister(&dom0_dev.miscdev);
579 XEN_PRINT("####### DPDK Xen Dom0 module loaded #######\n");
587 if (rsv_mm_info != NULL)
588 dom0_memory_free(rsv_memsize);
590 sysfs_remove_group(dom0_kobj, &dev_attr_grp);
591 kobject_put(dom0_kobj);
592 misc_deregister(&dom0_dev.miscdev);
594 XEN_PRINT("####### DPDK Xen Dom0 module unloaded #######\n");
598 dom0_open(struct inode *inode, struct file *file)
600 file->private_data = NULL;
602 XEN_PRINT(KERN_INFO "/dev/dom0_mm opened\n");
607 dom0_release(struct inode *inode, struct file *file)
610 struct dom0_mm_data *mm_data = file->private_data;
615 mutex_lock(&dom0_dev.data_lock);
616 if (--mm_data->refcnt == 0)
617 ret = dom0_memory_release(mm_data);
618 mutex_unlock(&dom0_dev.data_lock);
620 file->private_data = NULL;
621 XEN_PRINT(KERN_INFO "/dev/dom0_mm closed\n");
626 dom0_mmap(struct file *file, struct vm_area_struct *vm)
629 uint32_t idx = vm->vm_pgoff;
630 uint64_t pfn, size = vm->vm_end - vm->vm_start;
631 struct dom0_mm_data *mm_data = file->private_data;
636 mutex_lock(&dom0_dev.data_lock);
637 if (idx >= mm_data->num_memseg) {
638 mutex_unlock(&dom0_dev.data_lock);
642 if (size > mm_data->seg_info[idx].size){
643 mutex_unlock(&dom0_dev.data_lock);
647 XEN_PRINT("mmap memseg idx =%d,size = 0x%llx\n", idx, size);
649 pfn = mm_data->seg_info[idx].pfn;
650 mutex_unlock(&dom0_dev.data_lock);
652 status = remap_pfn_range(vm, vm->vm_start, pfn, size, PAGE_SHARED);
657 dom0_ioctl(struct file *file,
658 unsigned int ioctl_num,
659 unsigned long ioctl_param)
662 char name[DOM0_NAME_MAX] = {0};
663 struct memory_info meminfo;
664 struct dom0_mm_data *mm_data = file->private_data;
666 XEN_PRINT("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
669 * Switch according to the ioctl called
671 switch _IOC_NR(ioctl_num) {
672 case _IOC_NR(RTE_DOM0_IOCTL_PREPARE_MEMSEG):
673 ret = copy_from_user(&meminfo, (void *)ioctl_param,
674 sizeof(struct memory_info));
678 if (mm_data != NULL) {
679 XEN_ERR("Cannot create memory segment for the same"
680 " file descriptor\n");
684 /* Allocate private data */
685 mm_data = vmalloc(sizeof(struct dom0_mm_data));
687 XEN_ERR("Unable to allocate device private data\n");
690 memset(mm_data, 0, sizeof(struct dom0_mm_data));
692 mutex_lock(&dom0_dev.data_lock);
693 /* check if we can allocate memory*/
694 if (dom0_check_memory(&meminfo) < 0) {
695 mutex_unlock(&dom0_dev.data_lock);
700 /* allocate memory and created memory segments*/
701 if (dom0_prepare_memsegs(&meminfo, mm_data) < 0) {
702 XEN_ERR("create memory segment fail.\n");
703 mutex_unlock(&dom0_dev.data_lock);
707 file->private_data = mm_data;
708 mutex_unlock(&dom0_dev.data_lock);
711 /* support multiple process in term of memory mapping*/
712 case _IOC_NR(RTE_DOM0_IOCTL_ATTACH_TO_MEMSEG):
713 ret = copy_from_user(name, (void *)ioctl_param,
714 sizeof(char) * DOM0_NAME_MAX);
718 mutex_lock(&dom0_dev.data_lock);
719 idx = dom0_find_memdata(name);
721 mutex_unlock(&dom0_dev.data_lock);
725 mm_data = dom0_dev.mm_data[idx];
727 file->private_data = mm_data;
728 mutex_unlock(&dom0_dev.data_lock);
731 case _IOC_NR(RTE_DOM0_IOCTL_GET_NUM_MEMSEG):
732 ret = copy_to_user((void *)ioctl_param, &mm_data->num_memseg,
738 case _IOC_NR(RTE_DOM0_IOCTL_GET_MEMSEG_INFO):
739 ret = copy_to_user((void *)ioctl_param,
740 &mm_data->seg_info[0],
741 sizeof(struct memseg_info) *
742 mm_data->num_memseg);
747 XEN_PRINT("IOCTL default \n");
754 module_init(dom0_init);
755 module_exit(dom0_exit);
757 module_param(rsv_memsize, uint, S_IRUGO | S_IWUSR);
758 MODULE_PARM_DESC(rsv_memsize, "Xen-dom0 reserved memory size(MB).\n");