~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/powerpc/kvm/book3s_64_vio.c

Version: ~ [ linux-5.16-rc3 ] ~ [ linux-5.15.5 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.82 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.162 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.218 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.256 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.291 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.293 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
 18  */
 19 
 20 #include <linux/types.h>
 21 #include <linux/string.h>
 22 #include <linux/kvm.h>
 23 #include <linux/kvm_host.h>
 24 #include <linux/highmem.h>
 25 #include <linux/gfp.h>
 26 #include <linux/slab.h>
 27 #include <linux/sched/signal.h>
 28 #include <linux/hugetlb.h>
 29 #include <linux/list.h>
 30 #include <linux/anon_inodes.h>
 31 #include <linux/iommu.h>
 32 #include <linux/file.h>
 33 
 34 #include <asm/tlbflush.h>
 35 #include <asm/kvm_ppc.h>
 36 #include <asm/kvm_book3s.h>
 37 #include <asm/book3s/64/mmu-hash.h>
 38 #include <asm/hvcall.h>
 39 #include <asm/synch.h>
 40 #include <asm/ppc-opcode.h>
 41 #include <asm/kvm_host.h>
 42 #include <asm/udbg.h>
 43 #include <asm/iommu.h>
 44 #include <asm/tce.h>
 45 #include <asm/mmu_context.h>
 46 
 47 static unsigned long kvmppc_tce_pages(unsigned long iommu_pages)
 48 {
 49         return ALIGN(iommu_pages * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
 50 }
 51 
 52 static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
 53 {
 54         unsigned long stt_bytes = sizeof(struct kvmppc_spapr_tce_table) +
 55                         (tce_pages * sizeof(struct page *));
 56 
 57         return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
 58 }
 59 
 60 static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
 61 {
 62         long ret = 0;
 63 
 64         if (!current || !current->mm)
 65                 return ret; /* process exited */
 66 
 67         down_write(&current->mm->mmap_sem);
 68 
 69         if (inc) {
 70                 unsigned long locked, lock_limit;
 71 
 72                 locked = current->mm->locked_vm + stt_pages;
 73                 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
 74                 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
 75                         ret = -ENOMEM;
 76                 else
 77                         current->mm->locked_vm += stt_pages;
 78         } else {
 79                 if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
 80                         stt_pages = current->mm->locked_vm;
 81 
 82                 current->mm->locked_vm -= stt_pages;
 83         }
 84 
 85         pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid,
 86                         inc ? '+' : '-',
 87                         stt_pages << PAGE_SHIFT,
 88                         current->mm->locked_vm << PAGE_SHIFT,
 89                         rlimit(RLIMIT_MEMLOCK),
 90                         ret ? " - exceeded" : "");
 91 
 92         up_write(&current->mm->mmap_sem);
 93 
 94         return ret;
 95 }
 96 
 97 static void kvm_spapr_tce_iommu_table_free(struct rcu_head *head)
 98 {
 99         struct kvmppc_spapr_tce_iommu_table *stit = container_of(head,
100                         struct kvmppc_spapr_tce_iommu_table, rcu);
101 
102         iommu_tce_table_put(stit->tbl);
103 
104         kfree(stit);
105 }
106 
107 static void kvm_spapr_tce_liobn_put(struct kref *kref)
108 {
109         struct kvmppc_spapr_tce_iommu_table *stit = container_of(kref,
110                         struct kvmppc_spapr_tce_iommu_table, kref);
111 
112         list_del_rcu(&stit->next);
113 
114         call_rcu(&stit->rcu, kvm_spapr_tce_iommu_table_free);
115 }
116 
117 extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
118                 struct iommu_group *grp)
119 {
120         int i;
121         struct kvmppc_spapr_tce_table *stt;
122         struct kvmppc_spapr_tce_iommu_table *stit, *tmp;
123         struct iommu_table_group *table_group = NULL;
124 
125         list_for_each_entry_rcu(stt, &kvm->arch.spapr_tce_tables, list) {
126 
127                 table_group = iommu_group_get_iommudata(grp);
128                 if (WARN_ON(!table_group))
129                         continue;
130 
131                 list_for_each_entry_safe(stit, tmp, &stt->iommu_tables, next) {
132                         for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
133                                 if (table_group->tables[i] != stit->tbl)
134                                         continue;
135 
136                                 kref_put(&stit->kref, kvm_spapr_tce_liobn_put);
137                                 return;
138                         }
139                 }
140         }
141 }
142 
143 extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
144                 struct iommu_group *grp)
145 {
146         struct kvmppc_spapr_tce_table *stt = NULL;
147         bool found = false;
148         struct iommu_table *tbl = NULL;
149         struct iommu_table_group *table_group;
150         long i;
151         struct kvmppc_spapr_tce_iommu_table *stit;
152         struct fd f;
153 
154         f = fdget(tablefd);
155         if (!f.file)
156                 return -EBADF;
157 
158         list_for_each_entry_rcu(stt, &kvm->arch.spapr_tce_tables, list) {
159                 if (stt == f.file->private_data) {
160                         found = true;
161                         break;
162                 }
163         }
164 
165         fdput(f);
166 
167         if (!found)
168                 return -EINVAL;
169 
170         table_group = iommu_group_get_iommudata(grp);
171         if (WARN_ON(!table_group))
172                 return -EFAULT;
173 
174         for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
175                 struct iommu_table *tbltmp = table_group->tables[i];
176 
177                 if (!tbltmp)
178                         continue;
179                 /* Make sure hardware table parameters are compatible */
180                 if ((tbltmp->it_page_shift <= stt->page_shift) &&
181                                 (tbltmp->it_offset << tbltmp->it_page_shift ==
182                                  stt->offset << stt->page_shift) &&
183                                 (tbltmp->it_size << tbltmp->it_page_shift >=
184                                  stt->size << stt->page_shift)) {
185                         /*
186                          * Reference the table to avoid races with
187                          * add/remove DMA windows.
188                          */
189                         tbl = iommu_tce_table_get(tbltmp);
190                         break;
191                 }
192         }
193         if (!tbl)
194                 return -EINVAL;
195 
196         list_for_each_entry_rcu(stit, &stt->iommu_tables, next) {
197                 if (tbl != stit->tbl)
198                         continue;
199 
200                 if (!kref_get_unless_zero(&stit->kref)) {
201                         /* stit is being destroyed */
202                         iommu_tce_table_put(tbl);
203                         return -ENOTTY;
204                 }
205                 /*
206                  * The table is already known to this KVM, we just increased
207                  * its KVM reference counter and can return.
208                  */
209                 return 0;
210         }
211 
212         stit = kzalloc(sizeof(*stit), GFP_KERNEL);
213         if (!stit) {
214                 iommu_tce_table_put(tbl);
215                 return -ENOMEM;
216         }
217 
218         stit->tbl = tbl;
219         kref_init(&stit->kref);
220 
221         list_add_rcu(&stit->next, &stt->iommu_tables);
222 
223         return 0;
224 }
225 
226 static void release_spapr_tce_table(struct rcu_head *head)
227 {
228         struct kvmppc_spapr_tce_table *stt = container_of(head,
229                         struct kvmppc_spapr_tce_table, rcu);
230         unsigned long i, npages = kvmppc_tce_pages(stt->size);
231 
232         for (i = 0; i < npages; i++)
233                 __free_page(stt->pages[i]);
234 
235         kfree(stt);
236 }
237 
238 static vm_fault_t kvm_spapr_tce_fault(struct vm_fault *vmf)
239 {
240         struct kvmppc_spapr_tce_table *stt = vmf->vma->vm_file->private_data;
241         struct page *page;
242 
243         if (vmf->pgoff >= kvmppc_tce_pages(stt->size))
244                 return VM_FAULT_SIGBUS;
245 
246         page = stt->pages[vmf->pgoff];
247         get_page(page);
248         vmf->page = page;
249         return 0;
250 }
251 
252 static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
253         .fault = kvm_spapr_tce_fault,
254 };
255 
256 static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
257 {
258         vma->vm_ops = &kvm_spapr_tce_vm_ops;
259         return 0;
260 }
261 
262 static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
263 {
264         struct kvmppc_spapr_tce_table *stt = filp->private_data;
265         struct kvmppc_spapr_tce_iommu_table *stit, *tmp;
266         struct kvm *kvm = stt->kvm;
267 
268         mutex_lock(&kvm->lock);
269         list_del_rcu(&stt->list);
270         mutex_unlock(&kvm->lock);
271 
272         list_for_each_entry_safe(stit, tmp, &stt->iommu_tables, next) {
273                 WARN_ON(!kref_read(&stit->kref));
274                 while (1) {
275                         if (kref_put(&stit->kref, kvm_spapr_tce_liobn_put))
276                                 break;
277                 }
278         }
279 
280         kvm_put_kvm(stt->kvm);
281 
282         kvmppc_account_memlimit(
283                 kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
284         call_rcu(&stt->rcu, release_spapr_tce_table);
285 
286         return 0;
287 }
288 
289 static const struct file_operations kvm_spapr_tce_fops = {
290         .mmap           = kvm_spapr_tce_mmap,
291         .release        = kvm_spapr_tce_release,
292 };
293 
294 long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
295                                    struct kvm_create_spapr_tce_64 *args)
296 {
297         struct kvmppc_spapr_tce_table *stt = NULL;
298         struct kvmppc_spapr_tce_table *siter;
299         unsigned long npages, size = args->size;
300         int ret = -ENOMEM;
301         int i;
302 
303         if (!args->size || args->page_shift < 12 || args->page_shift > 34 ||
304                 (args->offset + args->size > (ULLONG_MAX >> args->page_shift)))
305                 return -EINVAL;
306 
307         npages = kvmppc_tce_pages(size);
308         ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
309         if (ret)
310                 return ret;
311 
312         ret = -ENOMEM;
313         stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
314                       GFP_KERNEL);
315         if (!stt)
316                 goto fail_acct;
317 
318         stt->liobn = args->liobn;
319         stt->page_shift = args->page_shift;
320         stt->offset = args->offset;
321         stt->size = size;
322         stt->kvm = kvm;
323         INIT_LIST_HEAD_RCU(&stt->iommu_tables);
324 
325         for (i = 0; i < npages; i++) {
326                 stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
327                 if (!stt->pages[i])
328                         goto fail;
329         }
330 
331         mutex_lock(&kvm->lock);
332 
333         /* Check this LIOBN hasn't been previously allocated */
334         ret = 0;
335         list_for_each_entry(siter, &kvm->arch.spapr_tce_tables, list) {
336                 if (siter->liobn == args->liobn) {
337                         ret = -EBUSY;
338                         break;
339                 }
340         }
341 
342         if (!ret)
343                 ret = anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
344                                        stt, O_RDWR | O_CLOEXEC);
345 
346         if (ret >= 0) {
347                 list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables);
348                 kvm_get_kvm(kvm);
349         }
350 
351         mutex_unlock(&kvm->lock);
352 
353         if (ret >= 0)
354                 return ret;
355 
356  fail:
357         for (i = 0; i < npages; i++)
358                 if (stt->pages[i])
359                         __free_page(stt->pages[i]);
360 
361         kfree(stt);
362  fail_acct:
363         kvmppc_account_memlimit(kvmppc_stt_pages(npages), false);
364         return ret;
365 }
366 
367 static void kvmppc_clear_tce(struct iommu_table *tbl, unsigned long entry)
368 {
369         unsigned long hpa = 0;
370         enum dma_data_direction dir = DMA_NONE;
371 
372         iommu_tce_xchg(tbl, entry, &hpa, &dir);
373 }
374 
375 static long kvmppc_tce_iommu_mapped_dec(struct kvm *kvm,
376                 struct iommu_table *tbl, unsigned long entry)
377 {
378         struct mm_iommu_table_group_mem_t *mem = NULL;
379         const unsigned long pgsize = 1ULL << tbl->it_page_shift;
380         unsigned long *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
381 
382         if (!pua)
383                 /* it_userspace allocation might be delayed */
384                 return H_TOO_HARD;
385 
386         mem = mm_iommu_lookup(kvm->mm, *pua, pgsize);
387         if (!mem)
388                 return H_TOO_HARD;
389 
390         mm_iommu_mapped_dec(mem);
391 
392         *pua = 0;
393 
394         return H_SUCCESS;
395 }
396 
397 static long kvmppc_tce_iommu_do_unmap(struct kvm *kvm,
398                 struct iommu_table *tbl, unsigned long entry)
399 {
400         enum dma_data_direction dir = DMA_NONE;
401         unsigned long hpa = 0;
402         long ret;
403 
404         if (WARN_ON_ONCE(iommu_tce_xchg(tbl, entry, &hpa, &dir)))
405                 return H_HARDWARE;
406 
407         if (dir == DMA_NONE)
408                 return H_SUCCESS;
409 
410         ret = kvmppc_tce_iommu_mapped_dec(kvm, tbl, entry);
411         if (ret != H_SUCCESS)
412                 iommu_tce_xchg(tbl, entry, &hpa, &dir);
413 
414         return ret;
415 }
416 
417 static long kvmppc_tce_iommu_unmap(struct kvm *kvm,
418                 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
419                 unsigned long entry)
420 {
421         unsigned long i, ret = H_SUCCESS;
422         unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
423         unsigned long io_entry = entry * subpages;
424 
425         for (i = 0; i < subpages; ++i) {
426                 ret = kvmppc_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
427                 if (ret != H_SUCCESS)
428                         break;
429         }
430 
431         return ret;
432 }
433 
434 long kvmppc_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
435                 unsigned long entry, unsigned long ua,
436                 enum dma_data_direction dir)
437 {
438         long ret;
439         unsigned long hpa, *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
440         struct mm_iommu_table_group_mem_t *mem;
441 
442         if (!pua)
443                 /* it_userspace allocation might be delayed */
444                 return H_TOO_HARD;
445 
446         mem = mm_iommu_lookup(kvm->mm, ua, 1ULL << tbl->it_page_shift);
447         if (!mem)
448                 /* This only handles v2 IOMMU type, v1 is handled via ioctl() */
449                 return H_TOO_HARD;
450 
451         if (WARN_ON_ONCE(mm_iommu_ua_to_hpa(mem, ua, tbl->it_page_shift, &hpa)))
452                 return H_HARDWARE;
453 
454         if (mm_iommu_mapped_inc(mem))
455                 return H_CLOSED;
456 
457         ret = iommu_tce_xchg(tbl, entry, &hpa, &dir);
458         if (WARN_ON_ONCE(ret)) {
459                 mm_iommu_mapped_dec(mem);
460                 return H_HARDWARE;
461         }
462 
463         if (dir != DMA_NONE)
464                 kvmppc_tce_iommu_mapped_dec(kvm, tbl, entry);
465 
466         *pua = ua;
467 
468         return 0;
469 }
470 
471 static long kvmppc_tce_iommu_map(struct kvm *kvm,
472                 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
473                 unsigned long entry, unsigned long ua,
474                 enum dma_data_direction dir)
475 {
476         unsigned long i, pgoff, ret = H_SUCCESS;
477         unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
478         unsigned long io_entry = entry * subpages;
479 
480         for (i = 0, pgoff = 0; i < subpages;
481                         ++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {
482 
483                 ret = kvmppc_tce_iommu_do_map(kvm, tbl,
484                                 io_entry + i, ua + pgoff, dir);
485                 if (ret != H_SUCCESS)
486                         break;
487         }
488 
489         return ret;
490 }
491 
492 long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
493                       unsigned long ioba, unsigned long tce)
494 {
495         struct kvmppc_spapr_tce_table *stt;
496         long ret, idx;
497         struct kvmppc_spapr_tce_iommu_table *stit;
498         unsigned long entry, ua = 0;
499         enum dma_data_direction dir;
500 
501         /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
502         /*          liobn, ioba, tce); */
503 
504         stt = kvmppc_find_table(vcpu->kvm, liobn);
505         if (!stt)
506                 return H_TOO_HARD;
507 
508         ret = kvmppc_ioba_validate(stt, ioba, 1);
509         if (ret != H_SUCCESS)
510                 return ret;
511 
512         ret = kvmppc_tce_validate(stt, tce);
513         if (ret != H_SUCCESS)
514                 return ret;
515 
516         dir = iommu_tce_direction(tce);
517 
518         idx = srcu_read_lock(&vcpu->kvm->srcu);
519 
520         if ((dir != DMA_NONE) && kvmppc_gpa_to_ua(vcpu->kvm,
521                         tce & ~(TCE_PCI_READ | TCE_PCI_WRITE), &ua, NULL)) {
522                 ret = H_PARAMETER;
523                 goto unlock_exit;
524         }
525 
526         entry = ioba >> stt->page_shift;
527 
528         list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
529                 if (dir == DMA_NONE)
530                         ret = kvmppc_tce_iommu_unmap(vcpu->kvm, stt,
531                                         stit->tbl, entry);
532                 else
533                         ret = kvmppc_tce_iommu_map(vcpu->kvm, stt, stit->tbl,
534                                         entry, ua, dir);
535 
536                 if (ret == H_SUCCESS)
537                         continue;
538 
539                 if (ret == H_TOO_HARD)
540                         goto unlock_exit;
541 
542                 WARN_ON_ONCE(1);
543                 kvmppc_clear_tce(stit->tbl, entry);
544         }
545 
546         kvmppc_tce_put(stt, entry, tce);
547 
548 unlock_exit:
549         srcu_read_unlock(&vcpu->kvm->srcu, idx);
550 
551         return ret;
552 }
553 EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
554 
555 long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
556                 unsigned long liobn, unsigned long ioba,
557                 unsigned long tce_list, unsigned long npages)
558 {
559         struct kvmppc_spapr_tce_table *stt;
560         long i, ret = H_SUCCESS, idx;
561         unsigned long entry, ua = 0;
562         u64 __user *tces;
563         u64 tce;
564         struct kvmppc_spapr_tce_iommu_table *stit;
565 
566         stt = kvmppc_find_table(vcpu->kvm, liobn);
567         if (!stt)
568                 return H_TOO_HARD;
569 
570         entry = ioba >> stt->page_shift;
571         /*
572          * SPAPR spec says that the maximum size of the list is 512 TCEs
573          * so the whole table fits in 4K page
574          */
575         if (npages > 512)
576                 return H_PARAMETER;
577 
578         if (tce_list & (SZ_4K - 1))
579                 return H_PARAMETER;
580 
581         ret = kvmppc_ioba_validate(stt, ioba, npages);
582         if (ret != H_SUCCESS)
583                 return ret;
584 
585         idx = srcu_read_lock(&vcpu->kvm->srcu);
586         if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL)) {
587                 ret = H_TOO_HARD;
588                 goto unlock_exit;
589         }
590         tces = (u64 __user *) ua;
591 
592         for (i = 0; i < npages; ++i) {
593                 if (get_user(tce, tces + i)) {
594                         ret = H_TOO_HARD;
595                         goto unlock_exit;
596                 }
597                 tce = be64_to_cpu(tce);
598 
599                 ret = kvmppc_tce_validate(stt, tce);
600                 if (ret != H_SUCCESS)
601                         goto unlock_exit;
602 
603                 if (kvmppc_gpa_to_ua(vcpu->kvm,
604                                 tce & ~(TCE_PCI_READ | TCE_PCI_WRITE),
605                                 &ua, NULL))
606                         return H_PARAMETER;
607 
608                 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
609                         ret = kvmppc_tce_iommu_map(vcpu->kvm, stt,
610                                         stit->tbl, entry + i, ua,
611                                         iommu_tce_direction(tce));
612 
613                         if (ret == H_SUCCESS)
614                                 continue;
615 
616                         if (ret == H_TOO_HARD)
617                                 goto unlock_exit;
618 
619                         WARN_ON_ONCE(1);
620                         kvmppc_clear_tce(stit->tbl, entry);
621                 }
622 
623                 kvmppc_tce_put(stt, entry + i, tce);
624         }
625 
626 unlock_exit:
627         srcu_read_unlock(&vcpu->kvm->srcu, idx);
628 
629         return ret;
630 }
631 EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect);
632 
633 long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
634                 unsigned long liobn, unsigned long ioba,
635                 unsigned long tce_value, unsigned long npages)
636 {
637         struct kvmppc_spapr_tce_table *stt;
638         long i, ret;
639         struct kvmppc_spapr_tce_iommu_table *stit;
640 
641         stt = kvmppc_find_table(vcpu->kvm, liobn);
642         if (!stt)
643                 return H_TOO_HARD;
644 
645         ret = kvmppc_ioba_validate(stt, ioba, npages);
646         if (ret != H_SUCCESS)
647                 return ret;
648 
649         /* Check permission bits only to allow userspace poison TCE for debug */
650         if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
651                 return H_PARAMETER;
652 
653         list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
654                 unsigned long entry = ioba >> stt->page_shift;
655 
656                 for (i = 0; i < npages; ++i) {
657                         ret = kvmppc_tce_iommu_unmap(vcpu->kvm, stt,
658                                         stit->tbl, entry + i);
659 
660                         if (ret == H_SUCCESS)
661                                 continue;
662 
663                         if (ret == H_TOO_HARD)
664                                 return ret;
665 
666                         WARN_ON_ONCE(1);
667                         kvmppc_clear_tce(stit->tbl, entry);
668                 }
669         }
670 
671         for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
672                 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
673 
674         return H_SUCCESS;
675 }
676 EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce);
677 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp