]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/powerpc/platforms/powernv/npu-dma.c
bc06088d46bd184331884357740731e80c250418
[linux.git] / arch / powerpc / platforms / powernv / npu-dma.c
1 /*
2  * This file implements the DMA operations for NVLink devices. The NPU
3  * devices all point to the same iommu table as the parent PCI device.
4  *
5  * Copyright Alistair Popple, IBM Corporation 2015.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU General Public
9  * License as published by the Free Software Foundation.
10  */
11
12 #include <linux/mmu_notifier.h>
13 #include <linux/mmu_context.h>
14 #include <linux/of.h>
15 #include <linux/pci.h>
16 #include <linux/memblock.h>
17 #include <linux/sizes.h>
18
19 #include <asm/debugfs.h>
20 #include <asm/powernv.h>
21 #include <asm/opal.h>
22
23 #include "pci.h"
24
25 /*
26  * spinlock to protect initialisation of an npu_context for a particular
27  * mm_struct.
28  */
29 static DEFINE_SPINLOCK(npu_context_lock);
30
31 static struct pci_dev *get_pci_dev(struct device_node *dn)
32 {
33         struct pci_dn *pdn = PCI_DN(dn);
34
35         return pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus),
36                                            pdn->busno, pdn->devfn);
37 }
38
39 /* Given a NPU device get the associated PCI device. */
40 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
41 {
42         struct device_node *dn;
43         struct pci_dev *gpdev;
44
45         if (WARN_ON(!npdev))
46                 return NULL;
47
48         if (WARN_ON(!npdev->dev.of_node))
49                 return NULL;
50
51         /* Get assoicated PCI device */
52         dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
53         if (!dn)
54                 return NULL;
55
56         gpdev = get_pci_dev(dn);
57         of_node_put(dn);
58
59         return gpdev;
60 }
61 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
62
63 /* Given the real PCI device get a linked NPU device. */
64 struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
65 {
66         struct device_node *dn;
67         struct pci_dev *npdev;
68
69         if (WARN_ON(!gpdev))
70                 return NULL;
71
72         /* Not all PCI devices have device-tree nodes */
73         if (!gpdev->dev.of_node)
74                 return NULL;
75
76         /* Get assoicated PCI device */
77         dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
78         if (!dn)
79                 return NULL;
80
81         npdev = get_pci_dev(dn);
82         of_node_put(dn);
83
84         return npdev;
85 }
86 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
87
88 /*
89  * Returns the PE assoicated with the PCI device of the given
90  * NPU. Returns the linked pci device if pci_dev != NULL.
91  */
92 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
93                                                   struct pci_dev **gpdev)
94 {
95         struct pnv_phb *phb;
96         struct pci_controller *hose;
97         struct pci_dev *pdev;
98         struct pnv_ioda_pe *pe;
99         struct pci_dn *pdn;
100
101         pdev = pnv_pci_get_gpu_dev(npe->pdev);
102         if (!pdev)
103                 return NULL;
104
105         pdn = pci_get_pdn(pdev);
106         if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
107                 return NULL;
108
109         hose = pci_bus_to_host(pdev->bus);
110         phb = hose->private_data;
111         pe = &phb->ioda.pe_array[pdn->pe_number];
112
113         if (gpdev)
114                 *gpdev = pdev;
115
116         return pe;
117 }
118
119 static long pnv_npu_unset_window(struct iommu_table_group *table_group,
120                 int num);
121
122 static long pnv_npu_set_window(struct iommu_table_group *table_group, int num,
123                 struct iommu_table *tbl)
124 {
125         struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
126                         table_group);
127         struct pnv_phb *phb = npe->phb;
128         int64_t rc;
129         const unsigned long size = tbl->it_indirect_levels ?
130                 tbl->it_level_size : tbl->it_size;
131         const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
132         const __u64 win_size = tbl->it_size << tbl->it_page_shift;
133         int num2 = (num == 0) ? 1 : 0;
134
135         /* NPU has just one TVE so if there is another table, remove it first */
136         if (npe->table_group.tables[num2])
137                 pnv_npu_unset_window(&npe->table_group, num2);
138
139         pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
140                         start_addr, start_addr + win_size - 1,
141                         IOMMU_PAGE_SIZE(tbl));
142
143         rc = opal_pci_map_pe_dma_window(phb->opal_id,
144                         npe->pe_number,
145                         npe->pe_number,
146                         tbl->it_indirect_levels + 1,
147                         __pa(tbl->it_base),
148                         size << 3,
149                         IOMMU_PAGE_SIZE(tbl));
150         if (rc) {
151                 pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
152                 return rc;
153         }
154         pnv_pci_ioda2_tce_invalidate_entire(phb, false);
155
156         /* Add the table to the list so its TCE cache will get invalidated */
157         pnv_pci_link_table_and_group(phb->hose->node, num,
158                         tbl, &npe->table_group);
159
160         return 0;
161 }
162
163 static long pnv_npu_unset_window(struct iommu_table_group *table_group, int num)
164 {
165         struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
166                         table_group);
167         struct pnv_phb *phb = npe->phb;
168         int64_t rc;
169
170         if (!npe->table_group.tables[num])
171                 return 0;
172
173         pe_info(npe, "Removing DMA window\n");
174
175         rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
176                         npe->pe_number,
177                         0/* levels */, 0/* table address */,
178                         0/* table size */, 0/* page size */);
179         if (rc) {
180                 pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
181                 return rc;
182         }
183         pnv_pci_ioda2_tce_invalidate_entire(phb, false);
184
185         pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
186                         &npe->table_group);
187
188         return 0;
189 }
190
191 /*
192  * Enables 32 bit DMA on NPU.
193  */
194 static void pnv_npu_dma_set_32(struct pnv_ioda_pe *npe)
195 {
196         struct pci_dev *gpdev;
197         struct pnv_ioda_pe *gpe;
198         int64_t rc;
199
200         /*
201          * Find the assoicated PCI devices and get the dma window
202          * information from there.
203          */
204         if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
205                 return;
206
207         gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
208         if (!gpe)
209                 return;
210
211         rc = pnv_npu_set_window(&npe->table_group, 0,
212                         gpe->table_group.tables[0]);
213
214         /*
215          * NVLink devices use the same TCE table configuration as
216          * their parent device so drivers shouldn't be doing DMA
217          * operations directly on these devices.
218          */
219         set_dma_ops(&npe->pdev->dev, NULL);
220 }
221
222 /*
223  * Enables bypass mode on the NPU. The NPU only supports one
224  * window per link, so bypass needs to be explicitly enabled or
225  * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
226  * active at the same time.
227  */
228 static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe)
229 {
230         struct pnv_phb *phb = npe->phb;
231         int64_t rc = 0;
232         phys_addr_t top = memblock_end_of_DRAM();
233
234         if (phb->type != PNV_PHB_NPU_NVLINK || !npe->pdev)
235                 return -EINVAL;
236
237         rc = pnv_npu_unset_window(&npe->table_group, 0);
238         if (rc != OPAL_SUCCESS)
239                 return rc;
240
241         /* Enable the bypass window */
242
243         top = roundup_pow_of_two(top);
244         dev_info(&npe->pdev->dev, "Enabling bypass for PE %x\n",
245                         npe->pe_number);
246         rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
247                         npe->pe_number, npe->pe_number,
248                         0 /* bypass base */, top);
249
250         if (rc == OPAL_SUCCESS)
251                 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
252
253         return rc;
254 }
255
256 void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass)
257 {
258         int i;
259         struct pnv_phb *phb;
260         struct pci_dn *pdn;
261         struct pnv_ioda_pe *npe;
262         struct pci_dev *npdev;
263
264         for (i = 0; ; ++i) {
265                 npdev = pnv_pci_get_npu_dev(gpdev, i);
266
267                 if (!npdev)
268                         break;
269
270                 pdn = pci_get_pdn(npdev);
271                 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
272                         return;
273
274                 phb = pci_bus_to_host(npdev->bus)->private_data;
275
276                 /* We only do bypass if it's enabled on the linked device */
277                 npe = &phb->ioda.pe_array[pdn->pe_number];
278
279                 if (bypass) {
280                         dev_info(&npdev->dev,
281                                         "Using 64-bit DMA iommu bypass\n");
282                         pnv_npu_dma_set_bypass(npe);
283                 } else {
284                         dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
285                         pnv_npu_dma_set_32(npe);
286                 }
287         }
288 }
289
290 #ifdef CONFIG_IOMMU_API
291 /* Switch ownership from platform code to external user (e.g. VFIO) */
292 static void pnv_npu_take_ownership(struct iommu_table_group *table_group)
293 {
294         struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
295                         table_group);
296         struct pnv_phb *phb = npe->phb;
297         int64_t rc;
298         struct pci_dev *gpdev = NULL;
299
300         /*
301          * Note: NPU has just a single TVE in the hardware which means that
302          * while used by the kernel, it can have either 32bit window or
303          * DMA bypass but never both. So we deconfigure 32bit window only
304          * if it was enabled at the moment of ownership change.
305          */
306         if (npe->table_group.tables[0]) {
307                 pnv_npu_unset_window(&npe->table_group, 0);
308                 return;
309         }
310
311         /* Disable bypass */
312         rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
313                         npe->pe_number, npe->pe_number,
314                         0 /* bypass base */, 0);
315         if (rc) {
316                 pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
317                 return;
318         }
319         pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
320
321         get_gpu_pci_dev_and_pe(npe, &gpdev);
322         if (gpdev)
323                 pnv_npu2_unmap_lpar_dev(gpdev);
324 }
325
326 static void pnv_npu_release_ownership(struct iommu_table_group *table_group)
327 {
328         struct pnv_ioda_pe *npe = container_of(table_group, struct pnv_ioda_pe,
329                         table_group);
330         struct pci_dev *gpdev = NULL;
331
332         get_gpu_pci_dev_and_pe(npe, &gpdev);
333         if (gpdev)
334                 pnv_npu2_map_lpar_dev(gpdev, 0, MSR_DR | MSR_PR | MSR_HV);
335 }
336
337 static struct iommu_table_group_ops pnv_pci_npu_ops = {
338         .set_window = pnv_npu_set_window,
339         .unset_window = pnv_npu_unset_window,
340         .take_ownership = pnv_npu_take_ownership,
341         .release_ownership = pnv_npu_release_ownership,
342 };
343 #endif /* !CONFIG_IOMMU_API */
344
345 /*
346  * NPU2 ATS
347  */
348 /* Maximum possible number of ATSD MMIO registers per NPU */
349 #define NV_NMMU_ATSD_REGS 8
350 #define NV_NPU_MAX_PE_NUM       16
351
352 /*
353  * A compound NPU IOMMU group which might consist of 1 GPU + 2xNPUs (POWER8) or
354  * up to 3 x (GPU + 2xNPUs) (POWER9).
355  */
356 struct npu_comp {
357         struct iommu_table_group table_group;
358         int pe_num;
359         struct pnv_ioda_pe *pe[NV_NPU_MAX_PE_NUM];
360 };
361
362 /* An NPU descriptor, valid for POWER9 only */
363 struct npu {
364         int index;
365         __be64 *mmio_atsd_regs[NV_NMMU_ATSD_REGS];
366         unsigned int mmio_atsd_count;
367
368         /* Bitmask for MMIO register usage */
369         unsigned long mmio_atsd_usage;
370
371         /* Do we need to explicitly flush the nest mmu? */
372         bool nmmu_flush;
373
374         struct npu_comp npucomp;
375 };
376
377 #ifdef CONFIG_IOMMU_API
378 static long pnv_npu_peers_create_table_userspace(
379                 struct iommu_table_group *table_group,
380                 int num, __u32 page_shift, __u64 window_size, __u32 levels,
381                 struct iommu_table **ptbl)
382 {
383         struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
384                         table_group);
385
386         if (!npucomp->pe_num || !npucomp->pe[0] ||
387                         !npucomp->pe[0]->table_group.ops ||
388                         !npucomp->pe[0]->table_group.ops->create_table)
389                 return -EFAULT;
390
391         return npucomp->pe[0]->table_group.ops->create_table(
392                         &npucomp->pe[0]->table_group, num, page_shift,
393                         window_size, levels, ptbl);
394 }
395
396 static long pnv_npu_peers_set_window(struct iommu_table_group *table_group,
397                 int num, struct iommu_table *tbl)
398 {
399         int i, j;
400         long ret = 0;
401         struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
402                         table_group);
403
404         for (i = 0; i < npucomp->pe_num; ++i) {
405                 struct pnv_ioda_pe *pe = npucomp->pe[i];
406
407                 if (!pe->table_group.ops->set_window)
408                         continue;
409
410                 ret = pe->table_group.ops->set_window(&pe->table_group,
411                                 num, tbl);
412                 if (ret)
413                         break;
414         }
415
416         if (ret) {
417                 for (j = 0; j < i; ++j) {
418                         struct pnv_ioda_pe *pe = npucomp->pe[j];
419
420                         if (!pe->table_group.ops->unset_window)
421                                 continue;
422
423                         ret = pe->table_group.ops->unset_window(
424                                         &pe->table_group, num);
425                         if (ret)
426                                 break;
427                 }
428         } else {
429                 table_group->tables[num] = iommu_tce_table_get(tbl);
430         }
431
432         return ret;
433 }
434
435 static long pnv_npu_peers_unset_window(struct iommu_table_group *table_group,
436                 int num)
437 {
438         int i, j;
439         long ret = 0;
440         struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
441                         table_group);
442
443         for (i = 0; i < npucomp->pe_num; ++i) {
444                 struct pnv_ioda_pe *pe = npucomp->pe[i];
445
446                 WARN_ON(npucomp->table_group.tables[num] !=
447                                 table_group->tables[num]);
448                 if (!npucomp->table_group.tables[num])
449                         continue;
450
451                 if (!pe->table_group.ops->unset_window)
452                         continue;
453
454                 ret = pe->table_group.ops->unset_window(&pe->table_group, num);
455                 if (ret)
456                         break;
457         }
458
459         if (ret) {
460                 for (j = 0; j < i; ++j) {
461                         struct pnv_ioda_pe *pe = npucomp->pe[j];
462
463                         if (!npucomp->table_group.tables[num])
464                                 continue;
465
466                         if (!pe->table_group.ops->set_window)
467                                 continue;
468
469                         ret = pe->table_group.ops->set_window(&pe->table_group,
470                                         num, table_group->tables[num]);
471                         if (ret)
472                                 break;
473                 }
474         } else if (table_group->tables[num]) {
475                 iommu_tce_table_put(table_group->tables[num]);
476                 table_group->tables[num] = NULL;
477         }
478
479         return ret;
480 }
481
482 static void pnv_npu_peers_take_ownership(struct iommu_table_group *table_group)
483 {
484         int i;
485         struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
486                         table_group);
487
488         for (i = 0; i < npucomp->pe_num; ++i) {
489                 struct pnv_ioda_pe *pe = npucomp->pe[i];
490
491                 if (!pe->table_group.ops->take_ownership)
492                         continue;
493                 pe->table_group.ops->take_ownership(&pe->table_group);
494         }
495 }
496
497 static void pnv_npu_peers_release_ownership(
498                 struct iommu_table_group *table_group)
499 {
500         int i;
501         struct npu_comp *npucomp = container_of(table_group, struct npu_comp,
502                         table_group);
503
504         for (i = 0; i < npucomp->pe_num; ++i) {
505                 struct pnv_ioda_pe *pe = npucomp->pe[i];
506
507                 if (!pe->table_group.ops->release_ownership)
508                         continue;
509                 pe->table_group.ops->release_ownership(&pe->table_group);
510         }
511 }
512
513 static struct iommu_table_group_ops pnv_npu_peers_ops = {
514         .get_table_size = pnv_pci_ioda2_get_table_size,
515         .create_table = pnv_npu_peers_create_table_userspace,
516         .set_window = pnv_npu_peers_set_window,
517         .unset_window = pnv_npu_peers_unset_window,
518         .take_ownership = pnv_npu_peers_take_ownership,
519         .release_ownership = pnv_npu_peers_release_ownership,
520 };
521
522 static void pnv_comp_attach_table_group(struct npu_comp *npucomp,
523                 struct pnv_ioda_pe *pe)
524 {
525         if (WARN_ON(npucomp->pe_num == NV_NPU_MAX_PE_NUM))
526                 return;
527
528         npucomp->pe[npucomp->pe_num] = pe;
529         ++npucomp->pe_num;
530 }
531
532 struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
533 {
534         struct iommu_table_group *table_group;
535         struct npu_comp *npucomp;
536         struct pci_dev *gpdev = NULL;
537         struct pci_controller *hose;
538         struct pci_dev *npdev = NULL;
539
540         list_for_each_entry(gpdev, &pe->pbus->devices, bus_list) {
541                 npdev = pnv_pci_get_npu_dev(gpdev, 0);
542                 if (npdev)
543                         break;
544         }
545
546         if (!npdev)
547                 /* It is not an NPU attached device, skip */
548                 return NULL;
549
550         hose = pci_bus_to_host(npdev->bus);
551
552         if (hose->npu) {
553                 table_group = &hose->npu->npucomp.table_group;
554
555                 if (!table_group->group) {
556                         table_group->ops = &pnv_npu_peers_ops;
557                         iommu_register_group(table_group,
558                                         hose->global_number,
559                                         pe->pe_number);
560                 }
561         } else {
562                 /* Create a group for 1 GPU and attached NPUs for POWER8 */
563                 pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
564                 table_group = &pe->npucomp->table_group;
565                 table_group->ops = &pnv_npu_peers_ops;
566                 iommu_register_group(table_group, hose->global_number,
567                                 pe->pe_number);
568         }
569
570         /* Steal capabilities from a GPU PE */
571         table_group->max_dynamic_windows_supported =
572                 pe->table_group.max_dynamic_windows_supported;
573         table_group->tce32_start = pe->table_group.tce32_start;
574         table_group->tce32_size = pe->table_group.tce32_size;
575         table_group->max_levels = pe->table_group.max_levels;
576         if (!table_group->pgsizes)
577                 table_group->pgsizes = pe->table_group.pgsizes;
578
579         npucomp = container_of(table_group, struct npu_comp, table_group);
580         pnv_comp_attach_table_group(npucomp, pe);
581
582         return table_group;
583 }
584
585 struct iommu_table_group *pnv_npu_compound_attach(struct pnv_ioda_pe *pe)
586 {
587         struct iommu_table_group *table_group;
588         struct npu_comp *npucomp;
589         struct pci_dev *gpdev = NULL;
590         struct pci_dev *npdev;
591         struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(pe, &gpdev);
592
593         WARN_ON(!(pe->flags & PNV_IODA_PE_DEV));
594         if (!gpe)
595                 return NULL;
596
597         /*
598          * IODA2 bridges get this set up from pci_controller_ops::setup_bridge
599          * but NPU bridges do not have this hook defined so we do it here.
600          * We do not setup other table group parameters as they won't be used
601          * anyway - NVLink bridges are subordinate PEs.
602          */
603         pe->table_group.ops = &pnv_pci_npu_ops;
604
605         table_group = iommu_group_get_iommudata(
606                         iommu_group_get(&gpdev->dev));
607
608         /*
609          * On P9 NPU PHB and PCI PHB support different page sizes,
610          * keep only matching. We expect here that NVLink bridge PE pgsizes is
611          * initialized by the caller.
612          */
613         table_group->pgsizes &= pe->table_group.pgsizes;
614         npucomp = container_of(table_group, struct npu_comp, table_group);
615         pnv_comp_attach_table_group(npucomp, pe);
616
617         list_for_each_entry(npdev, &pe->phb->hose->bus->devices, bus_list) {
618                 struct pci_dev *gpdevtmp = pnv_pci_get_gpu_dev(npdev);
619
620                 if (gpdevtmp != gpdev)
621                         continue;
622
623                 iommu_add_device(table_group, &npdev->dev);
624         }
625
626         return table_group;
627 }
628 #endif /* CONFIG_IOMMU_API */
629
630 /* Maximum number of nvlinks per npu */
631 #define NV_MAX_LINKS 6
632
633 /* Maximum index of npu2 hosts in the system. Always < NV_MAX_NPUS */
634 static int max_npu2_index;
635
636 struct npu_context {
637         struct mm_struct *mm;
638         struct pci_dev *npdev[NV_MAX_NPUS][NV_MAX_LINKS];
639         struct mmu_notifier mn;
640         struct kref kref;
641         bool nmmu_flush;
642
643         /* Callback to stop translation requests on a given GPU */
644         void (*release_cb)(struct npu_context *context, void *priv);
645
646         /*
647          * Private pointer passed to the above callback for usage by
648          * device drivers.
649          */
650         void *priv;
651 };
652
653 struct mmio_atsd_reg {
654         struct npu *npu;
655         int reg;
656 };
657
658 /*
659  * Find a free MMIO ATSD register and mark it in use. Return -ENOSPC
660  * if none are available.
661  */
662 static int get_mmio_atsd_reg(struct npu *npu)
663 {
664         int i;
665
666         for (i = 0; i < npu->mmio_atsd_count; i++) {
667                 if (!test_bit(i, &npu->mmio_atsd_usage))
668                         if (!test_and_set_bit_lock(i, &npu->mmio_atsd_usage))
669                                 return i;
670         }
671
672         return -ENOSPC;
673 }
674
675 static void put_mmio_atsd_reg(struct npu *npu, int reg)
676 {
677         clear_bit_unlock(reg, &npu->mmio_atsd_usage);
678 }
679
680 /* MMIO ATSD register offsets */
681 #define XTS_ATSD_LAUNCH 0
682 #define XTS_ATSD_AVA    1
683 #define XTS_ATSD_STAT   2
684
685 static unsigned long get_atsd_launch_val(unsigned long pid, unsigned long psize)
686 {
687         unsigned long launch = 0;
688
689         if (psize == MMU_PAGE_COUNT) {
690                 /* IS set to invalidate entire matching PID */
691                 launch |= PPC_BIT(12);
692         } else {
693                 /* AP set to invalidate region of psize */
694                 launch |= (u64)mmu_get_ap(psize) << PPC_BITLSHIFT(17);
695         }
696
697         /* PRS set to process-scoped */
698         launch |= PPC_BIT(13);
699
700         /* PID */
701         launch |= pid << PPC_BITLSHIFT(38);
702
703         /* Leave "No flush" (bit 39) 0 so every ATSD performs a flush */
704
705         return launch;
706 }
707
708 static void mmio_atsd_regs_write(struct mmio_atsd_reg
709                         mmio_atsd_reg[NV_MAX_NPUS], unsigned long offset,
710                         unsigned long val)
711 {
712         struct npu *npu;
713         int i, reg;
714
715         for (i = 0; i <= max_npu2_index; i++) {
716                 reg = mmio_atsd_reg[i].reg;
717                 if (reg < 0)
718                         continue;
719
720                 npu = mmio_atsd_reg[i].npu;
721                 __raw_writeq_be(val, npu->mmio_atsd_regs[reg] + offset);
722         }
723 }
724
725 static void mmio_invalidate_pid(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
726                                 unsigned long pid)
727 {
728         unsigned long launch = get_atsd_launch_val(pid, MMU_PAGE_COUNT);
729
730         /* Invalidating the entire process doesn't use a va */
731         mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
732 }
733
734 static void mmio_invalidate_range(struct mmio_atsd_reg
735                         mmio_atsd_reg[NV_MAX_NPUS], unsigned long pid,
736                         unsigned long start, unsigned long psize)
737 {
738         unsigned long launch = get_atsd_launch_val(pid, psize);
739
740         /* Write all VAs first */
741         mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_AVA, start);
742
743         /* Issue one barrier for all address writes */
744         eieio();
745
746         /* Launch */
747         mmio_atsd_regs_write(mmio_atsd_reg, XTS_ATSD_LAUNCH, launch);
748 }
749
750 #define mn_to_npu_context(x) container_of(x, struct npu_context, mn)
751
752 static void mmio_invalidate_wait(
753         struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
754 {
755         struct npu *npu;
756         int i, reg;
757
758         /* Wait for all invalidations to complete */
759         for (i = 0; i <= max_npu2_index; i++) {
760                 if (mmio_atsd_reg[i].reg < 0)
761                         continue;
762
763                 /* Wait for completion */
764                 npu = mmio_atsd_reg[i].npu;
765                 reg = mmio_atsd_reg[i].reg;
766                 while (__raw_readq(npu->mmio_atsd_regs[reg] + XTS_ATSD_STAT))
767                         cpu_relax();
768         }
769 }
770
771 /*
772  * Acquires all the address translation shootdown (ATSD) registers required to
773  * launch an ATSD on all links this npu_context is active on.
774  */
775 static void acquire_atsd_reg(struct npu_context *npu_context,
776                         struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
777 {
778         int i, j;
779         struct npu *npu;
780         struct pci_dev *npdev;
781
782         for (i = 0; i <= max_npu2_index; i++) {
783                 mmio_atsd_reg[i].reg = -1;
784                 for (j = 0; j < NV_MAX_LINKS; j++) {
785                         /*
786                          * There are no ordering requirements with respect to
787                          * the setup of struct npu_context, but to ensure
788                          * consistent behaviour we need to ensure npdev[][] is
789                          * only read once.
790                          */
791                         npdev = READ_ONCE(npu_context->npdev[i][j]);
792                         if (!npdev)
793                                 continue;
794
795                         npu = pci_bus_to_host(npdev->bus)->npu;
796                         if (!npu)
797                                 continue;
798
799                         mmio_atsd_reg[i].npu = npu;
800                         mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
801                         while (mmio_atsd_reg[i].reg < 0) {
802                                 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
803                                 cpu_relax();
804                         }
805                         break;
806                 }
807         }
808 }
809
810 /*
811  * Release previously acquired ATSD registers. To avoid deadlocks the registers
812  * must be released in the same order they were acquired above in
813  * acquire_atsd_reg.
814  */
815 static void release_atsd_reg(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
816 {
817         int i;
818
819         for (i = 0; i <= max_npu2_index; i++) {
820                 /*
821                  * We can't rely on npu_context->npdev[][] being the same here
822                  * as when acquire_atsd_reg() was called, hence we use the
823                  * values stored in mmio_atsd_reg during the acquire phase
824                  * rather than re-reading npdev[][].
825                  */
826                 if (mmio_atsd_reg[i].reg < 0)
827                         continue;
828
829                 put_mmio_atsd_reg(mmio_atsd_reg[i].npu, mmio_atsd_reg[i].reg);
830         }
831 }
832
833 /*
834  * Invalidate a virtual address range
835  */
836 static void mmio_invalidate(struct npu_context *npu_context,
837                         unsigned long start, unsigned long size)
838 {
839         struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS];
840         unsigned long pid = npu_context->mm->context.id;
841         unsigned long atsd_start = 0;
842         unsigned long end = start + size - 1;
843         int atsd_psize = MMU_PAGE_COUNT;
844
845         /*
846          * Convert the input range into one of the supported sizes. If the range
847          * doesn't fit, use the next larger supported size. Invalidation latency
848          * is high, so over-invalidation is preferred to issuing multiple
849          * invalidates.
850          *
851          * A 4K page size isn't supported by NPU/GPU ATS, so that case is
852          * ignored.
853          */
854         if (size == SZ_64K) {
855                 atsd_start = start;
856                 atsd_psize = MMU_PAGE_64K;
857         } else if (ALIGN_DOWN(start, SZ_2M) == ALIGN_DOWN(end, SZ_2M)) {
858                 atsd_start = ALIGN_DOWN(start, SZ_2M);
859                 atsd_psize = MMU_PAGE_2M;
860         } else if (ALIGN_DOWN(start, SZ_1G) == ALIGN_DOWN(end, SZ_1G)) {
861                 atsd_start = ALIGN_DOWN(start, SZ_1G);
862                 atsd_psize = MMU_PAGE_1G;
863         }
864
865         if (npu_context->nmmu_flush)
866                 /*
867                  * Unfortunately the nest mmu does not support flushing specific
868                  * addresses so we have to flush the whole mm once before
869                  * shooting down the GPU translation.
870                  */
871                 flush_all_mm(npu_context->mm);
872
873         /*
874          * Loop over all the NPUs this process is active on and launch
875          * an invalidate.
876          */
877         acquire_atsd_reg(npu_context, mmio_atsd_reg);
878
879         if (atsd_psize == MMU_PAGE_COUNT)
880                 mmio_invalidate_pid(mmio_atsd_reg, pid);
881         else
882                 mmio_invalidate_range(mmio_atsd_reg, pid, atsd_start,
883                                         atsd_psize);
884
885         mmio_invalidate_wait(mmio_atsd_reg);
886
887         /*
888          * The GPU requires two flush ATSDs to ensure all entries have been
889          * flushed. We use PID 0 as it will never be used for a process on the
890          * GPU.
891          */
892         mmio_invalidate_pid(mmio_atsd_reg, 0);
893         mmio_invalidate_wait(mmio_atsd_reg);
894         mmio_invalidate_pid(mmio_atsd_reg, 0);
895         mmio_invalidate_wait(mmio_atsd_reg);
896
897         release_atsd_reg(mmio_atsd_reg);
898 }
899
900 static void pnv_npu2_mn_release(struct mmu_notifier *mn,
901                                 struct mm_struct *mm)
902 {
903         struct npu_context *npu_context = mn_to_npu_context(mn);
904
905         /* Call into device driver to stop requests to the NMMU */
906         if (npu_context->release_cb)
907                 npu_context->release_cb(npu_context, npu_context->priv);
908
909         /*
910          * There should be no more translation requests for this PID, but we
911          * need to ensure any entries for it are removed from the TLB.
912          */
913         mmio_invalidate(npu_context, 0, ~0UL);
914 }
915
916 static void pnv_npu2_mn_change_pte(struct mmu_notifier *mn,
917                                 struct mm_struct *mm,
918                                 unsigned long address,
919                                 pte_t pte)
920 {
921         struct npu_context *npu_context = mn_to_npu_context(mn);
922         mmio_invalidate(npu_context, address, PAGE_SIZE);
923 }
924
925 static void pnv_npu2_mn_invalidate_range(struct mmu_notifier *mn,
926                                         struct mm_struct *mm,
927                                         unsigned long start, unsigned long end)
928 {
929         struct npu_context *npu_context = mn_to_npu_context(mn);
930         mmio_invalidate(npu_context, start, end - start);
931 }
932
933 static const struct mmu_notifier_ops nv_nmmu_notifier_ops = {
934         .release = pnv_npu2_mn_release,
935         .change_pte = pnv_npu2_mn_change_pte,
936         .invalidate_range = pnv_npu2_mn_invalidate_range,
937 };
938
939 /*
940  * Call into OPAL to setup the nmmu context for the current task in
941  * the NPU. This must be called to setup the context tables before the
942  * GPU issues ATRs. pdev should be a pointed to PCIe GPU device.
943  *
944  * A release callback should be registered to allow a device driver to
945  * be notified that it should not launch any new translation requests
946  * as the final TLB invalidate is about to occur.
947  *
948  * Returns an error if there no contexts are currently available or a
949  * npu_context which should be passed to pnv_npu2_handle_fault().
950  *
951  * mmap_sem must be held in write mode and must not be called from interrupt
952  * context.
953  */
954 struct npu_context *pnv_npu2_init_context(struct pci_dev *gpdev,
955                         unsigned long flags,
956                         void (*cb)(struct npu_context *, void *),
957                         void *priv)
958 {
959         int rc;
960         u32 nvlink_index;
961         struct device_node *nvlink_dn;
962         struct mm_struct *mm = current->mm;
963         struct npu *npu;
964         struct npu_context *npu_context;
965         struct pci_controller *hose;
966
967         /*
968          * At present we don't support GPUs connected to multiple NPUs and I'm
969          * not sure the hardware does either.
970          */
971         struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
972
973         if (!npdev)
974                 /* No nvlink associated with this GPU device */
975                 return ERR_PTR(-ENODEV);
976
977         /* We only support DR/PR/HV in pnv_npu2_map_lpar_dev() */
978         if (flags & ~(MSR_DR | MSR_PR | MSR_HV))
979                 return ERR_PTR(-EINVAL);
980
981         nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
982         if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
983                                                         &nvlink_index)))
984                 return ERR_PTR(-ENODEV);
985
986         if (!mm || mm->context.id == 0) {
987                 /*
988                  * Kernel thread contexts are not supported and context id 0 is
989                  * reserved on the GPU.
990                  */
991                 return ERR_PTR(-EINVAL);
992         }
993
994         hose = pci_bus_to_host(npdev->bus);
995         npu = hose->npu;
996         if (!npu)
997                 return ERR_PTR(-ENODEV);
998
999         /*
1000          * We store the npu pci device so we can more easily get at the
1001          * associated npus.
1002          */
1003         spin_lock(&npu_context_lock);
1004         npu_context = mm->context.npu_context;
1005         if (npu_context) {
1006                 if (npu_context->release_cb != cb ||
1007                         npu_context->priv != priv) {
1008                         spin_unlock(&npu_context_lock);
1009                         return ERR_PTR(-EINVAL);
1010                 }
1011
1012                 WARN_ON(!kref_get_unless_zero(&npu_context->kref));
1013         }
1014         spin_unlock(&npu_context_lock);
1015
1016         if (!npu_context) {
1017                 /*
1018                  * We can set up these fields without holding the
1019                  * npu_context_lock as the npu_context hasn't been returned to
1020                  * the caller meaning it can't be destroyed. Parallel allocation
1021                  * is protected against by mmap_sem.
1022                  */
1023                 rc = -ENOMEM;
1024                 npu_context = kzalloc(sizeof(struct npu_context), GFP_KERNEL);
1025                 if (npu_context) {
1026                         kref_init(&npu_context->kref);
1027                         npu_context->mm = mm;
1028                         npu_context->mn.ops = &nv_nmmu_notifier_ops;
1029                         rc = __mmu_notifier_register(&npu_context->mn, mm);
1030                 }
1031
1032                 if (rc) {
1033                         kfree(npu_context);
1034                         return ERR_PTR(rc);
1035                 }
1036
1037                 mm->context.npu_context = npu_context;
1038         }
1039
1040         npu_context->release_cb = cb;
1041         npu_context->priv = priv;
1042
1043         /*
1044          * npdev is a pci_dev pointer setup by the PCI code. We assign it to
1045          * npdev[][] to indicate to the mmu notifiers that an invalidation
1046          * should also be sent over this nvlink. The notifiers don't use any
1047          * other fields in npu_context, so we just need to ensure that when they
1048          * deference npu_context->npdev[][] it is either a valid pointer or
1049          * NULL.
1050          */
1051         WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], npdev);
1052
1053         if (!npu->nmmu_flush) {
1054                 /*
1055                  * If we're not explicitly flushing ourselves we need to mark
1056                  * the thread for global flushes
1057                  */
1058                 npu_context->nmmu_flush = false;
1059                 mm_context_add_copro(mm);
1060         } else
1061                 npu_context->nmmu_flush = true;
1062
1063         return npu_context;
1064 }
1065 EXPORT_SYMBOL(pnv_npu2_init_context);
1066
1067 static void pnv_npu2_release_context(struct kref *kref)
1068 {
1069         struct npu_context *npu_context =
1070                 container_of(kref, struct npu_context, kref);
1071
1072         if (!npu_context->nmmu_flush)
1073                 mm_context_remove_copro(npu_context->mm);
1074
1075         npu_context->mm->context.npu_context = NULL;
1076 }
1077
1078 /*
1079  * Destroy a context on the given GPU. May free the npu_context if it is no
1080  * longer active on any GPUs. Must not be called from interrupt context.
1081  */
1082 void pnv_npu2_destroy_context(struct npu_context *npu_context,
1083                         struct pci_dev *gpdev)
1084 {
1085         int removed;
1086         struct npu *npu;
1087         struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1088         struct device_node *nvlink_dn;
1089         u32 nvlink_index;
1090         struct pci_controller *hose;
1091
1092         if (WARN_ON(!npdev))
1093                 return;
1094
1095         hose = pci_bus_to_host(npdev->bus);
1096         npu = hose->npu;
1097         if (!npu)
1098                 return;
1099         nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
1100         if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
1101                                                         &nvlink_index)))
1102                 return;
1103         WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], NULL);
1104         spin_lock(&npu_context_lock);
1105         removed = kref_put(&npu_context->kref, pnv_npu2_release_context);
1106         spin_unlock(&npu_context_lock);
1107
1108         /*
1109          * We need to do this outside of pnv_npu2_release_context so that it is
1110          * outside the spinlock as mmu_notifier_destroy uses SRCU.
1111          */
1112         if (removed) {
1113                 mmu_notifier_unregister(&npu_context->mn,
1114                                         npu_context->mm);
1115
1116                 kfree(npu_context);
1117         }
1118
1119 }
1120 EXPORT_SYMBOL(pnv_npu2_destroy_context);
1121
1122 /*
1123  * Assumes mmap_sem is held for the contexts associated mm.
1124  */
1125 int pnv_npu2_handle_fault(struct npu_context *context, uintptr_t *ea,
1126                         unsigned long *flags, unsigned long *status, int count)
1127 {
1128         u64 rc = 0, result = 0;
1129         int i, is_write;
1130         struct page *page[1];
1131         const char __user *u;
1132         char c;
1133
1134         /* mmap_sem should be held so the struct_mm must be present */
1135         struct mm_struct *mm = context->mm;
1136
1137         WARN_ON(!rwsem_is_locked(&mm->mmap_sem));
1138
1139         for (i = 0; i < count; i++) {
1140                 is_write = flags[i] & NPU2_WRITE;
1141                 rc = get_user_pages_remote(NULL, mm, ea[i], 1,
1142                                         is_write ? FOLL_WRITE : 0,
1143                                         page, NULL, NULL);
1144
1145                 if (rc != 1) {
1146                         status[i] = rc;
1147                         result = -EFAULT;
1148                         continue;
1149                 }
1150
1151                 /* Make sure partition scoped tree gets a pte */
1152                 u = page_address(page[0]);
1153                 if (__get_user(c, u))
1154                         result = -EFAULT;
1155
1156                 status[i] = 0;
1157                 put_page(page[0]);
1158         }
1159
1160         return result;
1161 }
1162 EXPORT_SYMBOL(pnv_npu2_handle_fault);
1163
1164 int pnv_npu2_init(struct pci_controller *hose)
1165 {
1166         unsigned int i;
1167         u64 mmio_atsd;
1168         static int npu_index;
1169         struct npu *npu;
1170         int ret;
1171
1172         npu = kzalloc(sizeof(*npu), GFP_KERNEL);
1173         if (!npu)
1174                 return -ENOMEM;
1175
1176         npu->nmmu_flush = of_property_read_bool(hose->dn, "ibm,nmmu-flush");
1177
1178         for (i = 0; i < ARRAY_SIZE(npu->mmio_atsd_regs) &&
1179                         !of_property_read_u64_index(hose->dn, "ibm,mmio-atsd",
1180                                 i, &mmio_atsd); i++)
1181                 npu->mmio_atsd_regs[i] = ioremap(mmio_atsd, 32);
1182
1183         pr_info("NPU%d: Found %d MMIO ATSD registers", hose->global_number, i);
1184         npu->mmio_atsd_count = i;
1185         npu->mmio_atsd_usage = 0;
1186         npu_index++;
1187         if (WARN_ON(npu_index >= NV_MAX_NPUS)) {
1188                 ret = -ENOSPC;
1189                 goto fail_exit;
1190         }
1191         max_npu2_index = npu_index;
1192         npu->index = npu_index;
1193         hose->npu = npu;
1194
1195         return 0;
1196
1197 fail_exit:
1198         for (i = 0; i < npu->mmio_atsd_count; ++i)
1199                 iounmap(npu->mmio_atsd_regs[i]);
1200
1201         kfree(npu);
1202
1203         return ret;
1204 }
1205
1206 int pnv_npu2_map_lpar_dev(struct pci_dev *gpdev, unsigned int lparid,
1207                 unsigned long msr)
1208 {
1209         int ret;
1210         struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1211         struct pci_controller *hose;
1212         struct pnv_phb *nphb;
1213
1214         if (!npdev)
1215                 return -ENODEV;
1216
1217         hose = pci_bus_to_host(npdev->bus);
1218         nphb = hose->private_data;
1219
1220         dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=%u\n",
1221                         nphb->opal_id, lparid);
1222         /*
1223          * Currently we only support radix and non-zero LPCR only makes sense
1224          * for hash tables so skiboot expects the LPCR parameter to be a zero.
1225          */
1226         ret = opal_npu_map_lpar(nphb->opal_id,
1227                         PCI_DEVID(gpdev->bus->number, gpdev->devfn), lparid,
1228                         0 /* LPCR bits */);
1229         if (ret) {
1230                 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
1231                 return ret;
1232         }
1233
1234         dev_dbg(&gpdev->dev, "init context opalid=%llu msr=%lx\n",
1235                         nphb->opal_id, msr);
1236         ret = opal_npu_init_context(nphb->opal_id, 0/*__unused*/, msr,
1237                         PCI_DEVID(gpdev->bus->number, gpdev->devfn));
1238         if (ret < 0)
1239                 dev_err(&gpdev->dev, "Failed to init context: %d\n", ret);
1240         else
1241                 ret = 0;
1242
1243         return 0;
1244 }
1245 EXPORT_SYMBOL_GPL(pnv_npu2_map_lpar_dev);
1246
1247 void pnv_npu2_map_lpar(struct pnv_ioda_pe *gpe, unsigned long msr)
1248 {
1249         struct pci_dev *gpdev;
1250
1251         list_for_each_entry(gpdev, &gpe->pbus->devices, bus_list)
1252                 pnv_npu2_map_lpar_dev(gpdev, 0, msr);
1253 }
1254
1255 int pnv_npu2_unmap_lpar_dev(struct pci_dev *gpdev)
1256 {
1257         int ret;
1258         struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
1259         struct pci_controller *hose;
1260         struct pnv_phb *nphb;
1261
1262         if (!npdev)
1263                 return -ENODEV;
1264
1265         hose = pci_bus_to_host(npdev->bus);
1266         nphb = hose->private_data;
1267
1268         dev_dbg(&gpdev->dev, "destroy context opalid=%llu\n",
1269                         nphb->opal_id);
1270         ret = opal_npu_destroy_context(nphb->opal_id, 0/*__unused*/,
1271                         PCI_DEVID(gpdev->bus->number, gpdev->devfn));
1272         if (ret < 0) {
1273                 dev_err(&gpdev->dev, "Failed to destroy context: %d\n", ret);
1274                 return ret;
1275         }
1276
1277         /* Set LPID to 0 anyway, just to be safe */
1278         dev_dbg(&gpdev->dev, "Map LPAR opalid=%llu lparid=0\n", nphb->opal_id);
1279         ret = opal_npu_map_lpar(nphb->opal_id,
1280                         PCI_DEVID(gpdev->bus->number, gpdev->devfn), 0 /*LPID*/,
1281                         0 /* LPCR bits */);
1282         if (ret)
1283                 dev_err(&gpdev->dev, "Error %d mapping device to LPAR\n", ret);
1284
1285         return ret;
1286 }
1287 EXPORT_SYMBOL_GPL(pnv_npu2_unmap_lpar_dev);