| 1 | /* |
| 2 | * Copyright 2010-2017 Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * Disclaimer: The codes contained in these modules may be specific to |
| 14 | * the Intel Software Development Platform codenamed Knights Ferry, |
| 15 | * and the Intel product codenamed Knights Corner, and are not backward |
| 16 | * compatible with other Intel products. Additionally, Intel will NOT |
| 17 | * support the codes or instruction set in future products. |
| 18 | * |
| 19 | * Intel offers no warranty of any kind regarding the code. This code is |
| 20 | * licensed on an "AS IS" basis and Intel is not obligated to provide |
| 21 | * any support, assistance, installation, training, or other services |
| 22 | * of any kind. Intel is also not obligated to provide any updates, |
| 23 | * enhancements or extensions. Intel specifically disclaims any warranty |
| 24 | * of merchantability, non-infringement, fitness for any particular |
| 25 | * purpose, and any other warranty. |
| 26 | * |
| 27 | * Further, Intel disclaims all liability of any kind, including but |
| 28 | * not limited to liability for infringement of any proprietary rights, |
| 29 | * relating to the use of the code, even if Intel is notified of the |
| 30 | * possibility of such liability. Except as expressly stated in an Intel |
| 31 | * license agreement provided with this code and agreed upon with Intel, |
| 32 | * no license, express or implied, by estoppel or otherwise, to any |
| 33 | * intellectual property rights is granted herein. |
| 34 | */ |
| 35 | |
| 36 | #include "micint.h" |
| 37 | |
| 38 | int mic_psmi_open(struct file *filp) |
| 39 | { |
| 40 | bd_info_t *bd_info = mic_data.dd_bi[0]; |
| 41 | if (!bd_info->bi_ctx.bi_psmi.enabled) |
| 42 | return -EINVAL; |
| 43 | ((filp)->private_data) = &bd_info->bi_ctx; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | extern int usagemode_param; |
| 48 | |
| 49 | ssize_t mic_psmi_read(struct file * filp, char __user *buf, |
| 50 | size_t count, loff_t *pos) |
| 51 | { |
| 52 | ssize_t total_bytes = 0; |
| 53 | unsigned int pg_no, pg_off, bytes; |
| 54 | mic_ctx_t *mic_ctx = ((filp)->private_data); |
| 55 | struct mic_psmi_ctx *psmi_ctx = &mic_ctx->bi_psmi; |
| 56 | loff_t mem_size; |
| 57 | |
| 58 | if (!psmi_ctx->enabled) |
| 59 | return -EINVAL; |
| 60 | if (FAMILY_ABR == mic_ctx->bi_family && |
| 61 | USAGE_MODE_NORMAL != usagemode_param) |
| 62 | mem_size = MIC_APERTURE_SIZE; |
| 63 | else |
| 64 | mem_size = psmi_ctx->dma_mem_size; |
| 65 | if (*pos >= mem_size || count <= 0) |
| 66 | return 0; |
| 67 | if (*pos + count > mem_size) |
| 68 | count = mem_size - *pos; |
| 69 | /* read aperture memory */ |
| 70 | if (USAGE_MODE_NORMAL != usagemode_param) { |
| 71 | if (copy_to_user(buf, |
| 72 | mic_ctx->aper.va + *pos, count)) |
| 73 | return -EFAULT; |
| 74 | goto read_exit; |
| 75 | } |
| 76 | /* read host memory allocated for psmi handler */ |
| 77 | pg_no = *pos / MIC_PSMI_PAGE_SIZE; |
| 78 | pg_off = *pos % MIC_PSMI_PAGE_SIZE; |
| 79 | while (total_bytes < count) { |
| 80 | pci_dma_sync_single_for_cpu(mic_ctx->bi_pdev, |
| 81 | psmi_ctx->dma_tbl[pg_no + 1].pa, |
| 82 | MIC_PSMI_PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 83 | bytes = MIC_PSMI_PAGE_SIZE - pg_off; |
| 84 | if (total_bytes + bytes > count) |
| 85 | bytes = count - total_bytes; |
| 86 | if (copy_to_user(buf, |
| 87 | (void *)psmi_ctx->va_tbl[pg_no].pa + pg_off, bytes)) |
| 88 | return -EFAULT; |
| 89 | total_bytes += bytes; |
| 90 | buf += bytes; |
| 91 | pg_no++; |
| 92 | /* Only the first page needs an offset */ |
| 93 | pg_off = 0; |
| 94 | } |
| 95 | read_exit: |
| 96 | *pos += count; |
| 97 | return count; |
| 98 | } |
| 99 | |
| 100 | static ssize_t show_mem_size(struct device *dev, |
| 101 | struct device_attribute *attr, char *buf) |
| 102 | { |
| 103 | mic_ctx_t *mic_ctx = dev_get_drvdata(dev); |
| 104 | struct mic_psmi_ctx *psmi_ctx = &mic_ctx->bi_psmi; |
| 105 | |
| 106 | return snprintf(buf, PAGE_SIZE, "%ld\n", |
| 107 | (unsigned long)psmi_ctx->dma_mem_size); |
| 108 | } |
| 109 | static DEVICE_ATTR(mem_size, S_IRUGO, show_mem_size, NULL); |
| 110 | |
| 111 | static struct attribute *psmi_attributes[] = { |
| 112 | &dev_attr_mem_size.attr, |
| 113 | NULL |
| 114 | }; |
| 115 | |
| 116 | struct attribute_group psmi_attr_group = { |
| 117 | .attrs = psmi_attributes |
| 118 | }; |
| 119 | |
| 120 | #if (defined(RHEL_RELEASE_CODE) && \ |
| 121 | (LINUX_VERSION_CODE == KERNEL_VERSION(2,6,32))) || \ |
| 122 | LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34) |
| 123 | static ssize_t mic_psmi_read_ptes(struct file *filp, struct kobject *kobj, |
| 124 | struct bin_attribute *attr, char *buf, loff_t pos, size_t size) |
| 125 | #else |
| 126 | static ssize_t mic_psmi_read_ptes(struct kobject *kobj, |
| 127 | struct bin_attribute *attr, char *buf, loff_t pos, size_t size) |
| 128 | #endif |
| 129 | { |
| 130 | struct device *dev = container_of(kobj, struct device, kobj); |
| 131 | struct mic_psmi_ctx *psmi_ctx = |
| 132 | &((mic_ctx_t *)dev_get_drvdata(dev))->bi_psmi; |
| 133 | |
| 134 | if (pos >= psmi_ctx->dma_tbl_size || size <= 0) |
| 135 | return 0; |
| 136 | if (pos + size > psmi_ctx->dma_tbl_size) |
| 137 | size = psmi_ctx->dma_tbl_size - pos; |
| 138 | memcpy(buf, psmi_ctx->dma_tbl, size); |
| 139 | return size; |
| 140 | } |
| 141 | |
| 142 | struct bin_attribute mic_psmi_ptes_attr = { |
| 143 | .attr = { |
| 144 | .name = "psmi_ptes", |
| 145 | .mode = S_IRUSR |
| 146 | }, |
| 147 | .read = mic_psmi_read_ptes |
| 148 | }; |
| 149 | |
| 150 | extern bool mic_psmi_enable; |
| 151 | module_param_named(psmi, mic_psmi_enable, bool, S_IRUSR); |
| 152 | MODULE_PARM_DESC(psmi, "Enable/disable mic psmi"); |