David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License, version 2, as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * You should have received a copy of the GNU General Public License |
| 12 | * along with this program; if not, write to the Free Software |
| 13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 | * |
| 15 | * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
| 16 | * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com> |
| 17 | */ |
| 18 | |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/kvm.h> |
| 22 | #include <linux/kvm_host.h> |
| 23 | #include <linux/highmem.h> |
| 24 | #include <linux/gfp.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/hugetlb.h> |
| 27 | #include <linux/list.h> |
| 28 | |
| 29 | #include <asm/tlbflush.h> |
| 30 | #include <asm/kvm_ppc.h> |
| 31 | #include <asm/kvm_book3s.h> |
| 32 | #include <asm/mmu-hash64.h> |
| 33 | #include <asm/hvcall.h> |
| 34 | #include <asm/synch.h> |
| 35 | #include <asm/ppc-opcode.h> |
| 36 | #include <asm/kvm_host.h> |
| 37 | #include <asm/udbg.h> |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 38 | #include <asm/iommu.h> |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 39 | |
| 40 | #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64)) |
| 41 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 42 | /* |
| 43 | * Finds a TCE table descriptor by LIOBN. |
| 44 | * |
| 45 | * WARNING: This will be called in real or virtual mode on HV KVM and virtual |
| 46 | * mode on PR KVM |
| 47 | */ |
| 48 | static struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm_vcpu *vcpu, |
| 49 | unsigned long liobn) |
| 50 | { |
| 51 | struct kvm *kvm = vcpu->kvm; |
| 52 | struct kvmppc_spapr_tce_table *stt; |
| 53 | |
| 54 | list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) |
| 55 | if (stt->liobn == liobn) |
| 56 | return stt; |
| 57 | |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Validates IO address. |
| 63 | * |
| 64 | * WARNING: This will be called in real-mode on HV KVM and virtual |
| 65 | * mode on PR KVM |
| 66 | */ |
| 67 | static long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt, |
| 68 | unsigned long ioba, unsigned long npages) |
| 69 | { |
| 70 | unsigned long mask = (1ULL << IOMMU_PAGE_SHIFT_4K) - 1; |
| 71 | unsigned long idx = ioba >> IOMMU_PAGE_SHIFT_4K; |
| 72 | unsigned long size = stt->window_size >> IOMMU_PAGE_SHIFT_4K; |
| 73 | |
| 74 | if ((ioba & mask) || (idx + npages > size) || (idx + npages < idx)) |
| 75 | return H_PARAMETER; |
| 76 | |
| 77 | return H_SUCCESS; |
| 78 | } |
| 79 | |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 80 | /* WARNING: This will be called in real-mode on HV KVM and virtual |
| 81 | * mode on PR KVM |
| 82 | */ |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 83 | long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
| 84 | unsigned long ioba, unsigned long tce) |
| 85 | { |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 86 | struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn); |
| 87 | long ret; |
| 88 | unsigned long idx; |
| 89 | struct page *page; |
| 90 | u64 *tbl; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 91 | |
| 92 | /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ |
| 93 | /* liobn, ioba, tce); */ |
| 94 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 95 | if (!stt) |
| 96 | return H_TOO_HARD; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 97 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 98 | ret = kvmppc_ioba_validate(stt, ioba, 1); |
| 99 | if (ret != H_SUCCESS) |
| 100 | return ret; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 101 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 102 | idx = ioba >> SPAPR_TCE_SHIFT; |
| 103 | page = stt->pages[idx / TCES_PER_PAGE]; |
| 104 | tbl = (u64 *)page_address(page); |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 105 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 106 | /* FIXME: Need to validate the TCE itself */ |
| 107 | /* udbg_printf("tce @ %p\n", &tbl[idx % TCES_PER_PAGE]); */ |
| 108 | tbl[idx % TCES_PER_PAGE] = tce; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 109 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 110 | return H_SUCCESS; |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 111 | } |
Paul Mackerras | 066212e | 2013-10-07 22:17:50 +0530 | [diff] [blame] | 112 | EXPORT_SYMBOL_GPL(kvmppc_h_put_tce); |
Laurent Dufour | 69e9fbb2 | 2014-02-21 16:31:10 +0100 | [diff] [blame] | 113 | |
| 114 | long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
| 115 | unsigned long ioba) |
| 116 | { |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 117 | struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn); |
| 118 | long ret; |
| 119 | unsigned long idx; |
| 120 | struct page *page; |
| 121 | u64 *tbl; |
Laurent Dufour | 69e9fbb2 | 2014-02-21 16:31:10 +0100 | [diff] [blame] | 122 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 123 | if (!stt) |
| 124 | return H_TOO_HARD; |
Laurent Dufour | 69e9fbb2 | 2014-02-21 16:31:10 +0100 | [diff] [blame] | 125 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 126 | ret = kvmppc_ioba_validate(stt, ioba, 1); |
| 127 | if (ret != H_SUCCESS) |
| 128 | return ret; |
Laurent Dufour | 69e9fbb2 | 2014-02-21 16:31:10 +0100 | [diff] [blame] | 129 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 130 | idx = ioba >> SPAPR_TCE_SHIFT; |
| 131 | page = stt->pages[idx / TCES_PER_PAGE]; |
| 132 | tbl = (u64 *)page_address(page); |
Laurent Dufour | 69e9fbb2 | 2014-02-21 16:31:10 +0100 | [diff] [blame] | 133 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 134 | vcpu->arch.gpr[4] = tbl[idx % TCES_PER_PAGE]; |
Laurent Dufour | 69e9fbb2 | 2014-02-21 16:31:10 +0100 | [diff] [blame] | 135 | |
Alexey Kardashevskiy | fcbb2ce | 2016-02-15 12:55:04 +1100 | [diff] [blame^] | 136 | return H_SUCCESS; |
Laurent Dufour | 69e9fbb2 | 2014-02-21 16:31:10 +0100 | [diff] [blame] | 137 | } |
| 138 | EXPORT_SYMBOL_GPL(kvmppc_h_get_tce); |