1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 #include <linux/dma-mapping.h>
4 #include <linux/interrupt.h>
5 #include <linux/iopoll.h>
6 #include <linux/of.h>
7 #include <linux/of_address.h>
8 #include <linux/of_platform.h>
9 #include <linux/pci.h>
10 #include <linux/platform_device.h>
11
12 #include "arm-smmu-v3.h"
13 #include "../../dma-iommu.h"
14
15 struct arm_smmu_option_prop {
16 u32 opt;
17 const char *prop;
18 };
19
20 static struct arm_smmu_option_prop arm_smmu_options[] = {
21 { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
22 { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"},
23 { 0, NULL},
24 };
25
26 static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {
27 [EVTQ_MSI_INDEX] = {
28 ARM_SMMU_EVTQ_IRQ_CFG0,
29 ARM_SMMU_EVTQ_IRQ_CFG1,
30 ARM_SMMU_EVTQ_IRQ_CFG2,
31 },
32 [GERROR_MSI_INDEX] = {
33 ARM_SMMU_GERROR_IRQ_CFG0,
34 ARM_SMMU_GERROR_IRQ_CFG1,
35 ARM_SMMU_GERROR_IRQ_CFG2,
36 },
37 [PRIQ_MSI_INDEX] = {
38 ARM_SMMU_PRIQ_IRQ_CFG0,
39 ARM_SMMU_PRIQ_IRQ_CFG1,
40 ARM_SMMU_PRIQ_IRQ_CFG2,
41 },
42 };
43
parse_driver_options(struct arm_smmu_device * smmu)44 static void parse_driver_options(struct arm_smmu_device *smmu)
45 {
46 int i = 0;
47
48 do {
49 if (of_property_read_bool(smmu->dev->of_node,
50 arm_smmu_options[i].prop)) {
51 smmu->options |= arm_smmu_options[i].opt;
52 dev_notice(smmu->dev, "option %s\n",
53 arm_smmu_options[i].prop);
54 }
55 } while (arm_smmu_options[++i].opt);
56 }
57
58 #ifdef CONFIG_ACPI
59 #ifdef CONFIG_TEGRA241_CMDQV
acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node * node,struct arm_smmu_device * smmu)60 static void acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node *node,
61 struct arm_smmu_device *smmu)
62 {
63 const char *uid = kasprintf(GFP_KERNEL, "%u", node->identifier);
64 struct acpi_device *adev;
65
66 /* Look for an NVDA200C node whose _UID matches the SMMU node ID */
67 adev = acpi_dev_get_first_match_dev("NVDA200C", uid, -1);
68 if (adev) {
69 /* Tegra241 CMDQV driver is responsible for put_device() */
70 smmu->impl_dev = &adev->dev;
71 smmu->options |= ARM_SMMU_OPT_TEGRA241_CMDQV;
72 dev_info(smmu->dev, "found companion CMDQV device: %s\n",
73 dev_name(smmu->impl_dev));
74 }
75 kfree(uid);
76 }
77 #else
acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node * node,struct arm_smmu_device * smmu)78 static void acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node *node,
79 struct arm_smmu_device *smmu)
80 {
81 }
82 #endif
83
acpi_smmu_iort_probe_model(struct acpi_iort_node * node,struct arm_smmu_device * smmu)84 static int acpi_smmu_iort_probe_model(struct acpi_iort_node *node,
85 struct arm_smmu_device *smmu)
86 {
87 struct acpi_iort_smmu_v3 *iort_smmu =
88 (struct acpi_iort_smmu_v3 *)node->node_data;
89
90 switch (iort_smmu->model) {
91 case ACPI_IORT_SMMU_V3_CAVIUM_CN99XX:
92 smmu->options |= ARM_SMMU_OPT_PAGE0_REGS_ONLY;
93 break;
94 case ACPI_IORT_SMMU_V3_HISILICON_HI161X:
95 smmu->options |= ARM_SMMU_OPT_SKIP_PREFETCH;
96 break;
97 case ACPI_IORT_SMMU_V3_GENERIC:
98 /*
99 * Tegra241 implementation stores its SMMU options and impl_dev
100 * in DSDT. Thus, go through the ACPI tables unconditionally.
101 */
102 acpi_smmu_dsdt_probe_tegra241_cmdqv(node, smmu);
103 break;
104 }
105
106 dev_notice(smmu->dev, "option mask 0x%x\n", smmu->options);
107 return 0;
108 }
109
arm_smmu_device_acpi_probe(struct platform_device * pdev,struct arm_smmu_device * smmu)110 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
111 struct arm_smmu_device *smmu)
112 {
113 struct acpi_iort_smmu_v3 *iort_smmu;
114 struct device *dev = smmu->dev;
115 struct acpi_iort_node *node;
116
117 node = *(struct acpi_iort_node **)dev_get_platdata(dev);
118
119 /* Retrieve SMMUv3 specific data */
120 iort_smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
121
122 if (iort_smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE)
123 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
124
125 switch (FIELD_GET(ACPI_IORT_SMMU_V3_HTTU_OVERRIDE, iort_smmu->flags)) {
126 case IDR0_HTTU_ACCESS_DIRTY:
127 smmu->features |= ARM_SMMU_FEAT_HD;
128 fallthrough;
129 case IDR0_HTTU_ACCESS:
130 smmu->features |= ARM_SMMU_FEAT_HA;
131 }
132
133 return acpi_smmu_iort_probe_model(node, smmu);
134 }
135 #else
arm_smmu_device_acpi_probe(struct platform_device * pdev,struct arm_smmu_device * smmu)136 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
137 struct arm_smmu_device *smmu)
138 {
139 return -ENODEV;
140 }
141 #endif
142
arm_smmu_device_dt_probe(struct platform_device * pdev,struct arm_smmu_device * smmu)143 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
144 struct arm_smmu_device *smmu)
145 {
146 struct device *dev = &pdev->dev;
147 u32 cells;
148 int ret = -EINVAL;
149
150 if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells))
151 dev_err(dev, "missing #iommu-cells property\n");
152 else if (cells != 1)
153 dev_err(dev, "invalid #iommu-cells value (%d)\n", cells);
154 else
155 ret = 0;
156
157 parse_driver_options(smmu);
158
159 if (of_dma_is_coherent(dev->of_node))
160 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
161
162 return ret;
163 }
164
arm_smmu_fw_probe(struct platform_device * pdev,struct arm_smmu_device * smmu)165 int arm_smmu_fw_probe(struct platform_device *pdev,
166 struct arm_smmu_device *smmu)
167 {
168 if (smmu->dev->of_node)
169 return arm_smmu_device_dt_probe(pdev, smmu);
170 else
171 return arm_smmu_device_acpi_probe(pdev, smmu);
172 }
173
174 #define IIDR_IMPLEMENTER_ARM 0x43b
175 #define IIDR_PRODUCTID_ARM_MMU_600 0x483
176 #define IIDR_PRODUCTID_ARM_MMU_700 0x487
177
arm_smmu_device_iidr_probe(struct arm_smmu_device * smmu)178 static void arm_smmu_device_iidr_probe(struct arm_smmu_device *smmu)
179 {
180 u32 reg;
181 unsigned int implementer, productid, variant, revision;
182
183 reg = readl_relaxed(smmu->base + ARM_SMMU_IIDR);
184 implementer = FIELD_GET(IIDR_IMPLEMENTER, reg);
185 productid = FIELD_GET(IIDR_PRODUCTID, reg);
186 variant = FIELD_GET(IIDR_VARIANT, reg);
187 revision = FIELD_GET(IIDR_REVISION, reg);
188
189 switch (implementer) {
190 case IIDR_IMPLEMENTER_ARM:
191 switch (productid) {
192 case IIDR_PRODUCTID_ARM_MMU_600:
193 /* Arm erratum 1076982 */
194 if (variant == 0 && revision <= 2)
195 smmu->features &= ~ARM_SMMU_FEAT_SEV;
196 /* Arm erratum 1209401 */
197 if (variant < 2)
198 smmu->features &= ~ARM_SMMU_FEAT_NESTING;
199 break;
200 case IIDR_PRODUCTID_ARM_MMU_700:
201 /* Arm erratum 2812531 */
202 smmu->features &= ~ARM_SMMU_FEAT_BTM;
203 smmu->options |= ARM_SMMU_OPT_CMDQ_FORCE_SYNC;
204 /* Arm errata 2268618, 2812531 */
205 smmu->features &= ~ARM_SMMU_FEAT_NESTING;
206 break;
207 }
208 break;
209 }
210 }
211
arm_smmu_get_httu(struct arm_smmu_device * smmu,u32 reg)212 static void arm_smmu_get_httu(struct arm_smmu_device *smmu, u32 reg)
213 {
214 u32 fw_features = smmu->features & (ARM_SMMU_FEAT_HA | ARM_SMMU_FEAT_HD);
215 u32 hw_features = 0;
216
217 switch (FIELD_GET(IDR0_HTTU, reg)) {
218 case IDR0_HTTU_ACCESS_DIRTY:
219 hw_features |= ARM_SMMU_FEAT_HD;
220 fallthrough;
221 case IDR0_HTTU_ACCESS:
222 hw_features |= ARM_SMMU_FEAT_HA;
223 }
224
225 if (smmu->dev->of_node)
226 smmu->features |= hw_features;
227 else if (hw_features != fw_features)
228 /* ACPI IORT sets the HTTU bits */
229 dev_warn(smmu->dev,
230 "IDR0.HTTU features(0x%x) overridden by FW configuration (0x%x)\n",
231 hw_features, fw_features);
232 }
233
arm_smmu_device_hw_probe(struct arm_smmu_device * smmu)234 int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
235 {
236 u32 reg;
237 bool coherent = smmu->features & ARM_SMMU_FEAT_COHERENCY;
238
239 /* IDR0 */
240 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
241
242 /* 2-level structures */
243 if (FIELD_GET(IDR0_ST_LVL, reg) == IDR0_ST_LVL_2LVL)
244 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
245
246 if (reg & IDR0_CD2L)
247 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
248
249 /*
250 * Translation table endianness.
251 * We currently require the same endianness as the CPU, but this
252 * could be changed later by adding a new IO_PGTABLE_QUIRK.
253 */
254 switch (FIELD_GET(IDR0_TTENDIAN, reg)) {
255 case IDR0_TTENDIAN_MIXED:
256 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
257 break;
258 #ifdef __BIG_ENDIAN
259 case IDR0_TTENDIAN_BE:
260 smmu->features |= ARM_SMMU_FEAT_TT_BE;
261 break;
262 #else
263 case IDR0_TTENDIAN_LE:
264 smmu->features |= ARM_SMMU_FEAT_TT_LE;
265 break;
266 #endif
267 default:
268 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
269 return -ENXIO;
270 }
271
272 /* Boolean feature flags */
273 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
274 smmu->features |= ARM_SMMU_FEAT_PRI;
275
276 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
277 smmu->features |= ARM_SMMU_FEAT_ATS;
278
279 if (reg & IDR0_SEV)
280 smmu->features |= ARM_SMMU_FEAT_SEV;
281
282 if (reg & IDR0_MSI) {
283 smmu->features |= ARM_SMMU_FEAT_MSI;
284 if (coherent)
285 smmu->options |= ARM_SMMU_OPT_MSIPOLL;
286 }
287
288 if (reg & IDR0_HYP) {
289 smmu->features |= ARM_SMMU_FEAT_HYP;
290 if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
291 smmu->features |= ARM_SMMU_FEAT_E2H;
292 }
293
294 arm_smmu_get_httu(smmu, reg);
295
296 /*
297 * The coherency feature as set by FW is used in preference to the ID
298 * register, but warn on mismatch.
299 */
300 if (!!(reg & IDR0_COHACC) != coherent)
301 dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n",
302 coherent ? "true" : "false");
303
304 switch (FIELD_GET(IDR0_STALL_MODEL, reg)) {
305 case IDR0_STALL_MODEL_FORCE:
306 smmu->features |= ARM_SMMU_FEAT_STALL_FORCE;
307 fallthrough;
308 case IDR0_STALL_MODEL_STALL:
309 smmu->features |= ARM_SMMU_FEAT_STALLS;
310 }
311
312 if (reg & IDR0_S1P)
313 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
314
315 if (reg & IDR0_S2P)
316 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
317
318 if (!(reg & (IDR0_S1P | IDR0_S2P))) {
319 dev_err(smmu->dev, "no translation support!\n");
320 return -ENXIO;
321 }
322
323 /* We only support the AArch64 table format at present */
324 switch (FIELD_GET(IDR0_TTF, reg)) {
325 case IDR0_TTF_AARCH32_64:
326 smmu->ias = 40;
327 fallthrough;
328 case IDR0_TTF_AARCH64:
329 break;
330 default:
331 dev_err(smmu->dev, "AArch64 table format not supported!\n");
332 return -ENXIO;
333 }
334
335 /* ASID/VMID sizes */
336 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
337 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
338
339 /* IDR1 */
340 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
341 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
342 dev_err(smmu->dev, "embedded implementation not supported\n");
343 return -ENXIO;
344 }
345
346 if (reg & IDR1_ATTR_TYPES_OVR)
347 smmu->features |= ARM_SMMU_FEAT_ATTR_TYPES_OVR;
348
349 /* Queue sizes, capped to ensure natural alignment */
350 smmu->cmdq.q.llq.max_n_shift = min_t(u32, CMDQ_MAX_SZ_SHIFT,
351 FIELD_GET(IDR1_CMDQS, reg));
352 if (smmu->cmdq.q.llq.max_n_shift <= ilog2(CMDQ_BATCH_ENTRIES)) {
353 /*
354 * We don't support splitting up batches, so one batch of
355 * commands plus an extra sync needs to fit inside the command
356 * queue. There's also no way we can handle the weird alignment
357 * restrictions on the base pointer for a unit-length queue.
358 */
359 dev_err(smmu->dev, "command queue size <= %d entries not supported\n",
360 CMDQ_BATCH_ENTRIES);
361 return -ENXIO;
362 }
363
364 smmu->evtq.q.llq.max_n_shift = min_t(u32, EVTQ_MAX_SZ_SHIFT,
365 FIELD_GET(IDR1_EVTQS, reg));
366 smmu->priq.q.llq.max_n_shift = min_t(u32, PRIQ_MAX_SZ_SHIFT,
367 FIELD_GET(IDR1_PRIQS, reg));
368
369 /* SID/SSID sizes */
370 smmu->ssid_bits = FIELD_GET(IDR1_SSIDSIZE, reg);
371 smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg);
372 smmu->iommu.max_pasids = 1UL << smmu->ssid_bits;
373
374 /*
375 * If the SMMU supports fewer bits than would fill a single L2 stream
376 * table, use a linear table instead.
377 */
378 if (smmu->sid_bits <= STRTAB_SPLIT)
379 smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB;
380
381 /* IDR3 */
382 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR3);
383 if (FIELD_GET(IDR3_RIL, reg))
384 smmu->features |= ARM_SMMU_FEAT_RANGE_INV;
385
386 /* IDR5 */
387 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
388
389 /* Maximum number of outstanding stalls */
390 smmu->evtq.max_stalls = FIELD_GET(IDR5_STALL_MAX, reg);
391
392 /* Page sizes */
393 if (reg & IDR5_GRAN64K)
394 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
395 if (reg & IDR5_GRAN16K)
396 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
397 if (reg & IDR5_GRAN4K)
398 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
399
400 /* Input address size */
401 if (FIELD_GET(IDR5_VAX, reg) == IDR5_VAX_52_BIT)
402 smmu->features |= ARM_SMMU_FEAT_VAX;
403
404 /* Output address size */
405 switch (FIELD_GET(IDR5_OAS, reg)) {
406 case IDR5_OAS_32_BIT:
407 smmu->oas = 32;
408 break;
409 case IDR5_OAS_36_BIT:
410 smmu->oas = 36;
411 break;
412 case IDR5_OAS_40_BIT:
413 smmu->oas = 40;
414 break;
415 case IDR5_OAS_42_BIT:
416 smmu->oas = 42;
417 break;
418 case IDR5_OAS_44_BIT:
419 smmu->oas = 44;
420 break;
421 case IDR5_OAS_52_BIT:
422 smmu->oas = 52;
423 smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
424 break;
425 default:
426 dev_info(smmu->dev,
427 "unknown output address size. Truncating to 48-bit\n");
428 fallthrough;
429 case IDR5_OAS_48_BIT:
430 smmu->oas = 48;
431 }
432
433 /* Set the DMA mask for our table walker */
434 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
435 dev_warn(smmu->dev,
436 "failed to set DMA mask for table walker\n");
437
438 smmu->ias = max(smmu->ias, smmu->oas);
439
440 if ((smmu->features & ARM_SMMU_FEAT_TRANS_S1) &&
441 (smmu->features & ARM_SMMU_FEAT_TRANS_S2))
442 smmu->features |= ARM_SMMU_FEAT_NESTING;
443
444 arm_smmu_device_iidr_probe(smmu);
445
446 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
447 smmu->ias, smmu->oas, smmu->features);
448 return 0;
449 }
450
arm_smmu_write_reg_sync(struct arm_smmu_device * smmu,u32 val,unsigned int reg_off,unsigned int ack_off)451 int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
452 unsigned int reg_off, unsigned int ack_off)
453 {
454 u32 reg;
455
456 writel_relaxed(val, smmu->base + reg_off);
457 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
458 1, ARM_SMMU_POLL_TIMEOUT_US);
459 }
460
461 /* GBPA is "special" */
arm_smmu_update_gbpa(struct arm_smmu_device * smmu,u32 set,u32 clr)462 int arm_smmu_update_gbpa(struct arm_smmu_device *smmu, u32 set, u32 clr)
463 {
464 int ret;
465 u32 reg, __iomem *gbpa = smmu->base + ARM_SMMU_GBPA;
466
467 ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
468 1, ARM_SMMU_POLL_TIMEOUT_US);
469 if (ret)
470 return ret;
471
472 reg &= ~clr;
473 reg |= set;
474 writel_relaxed(reg | GBPA_UPDATE, gbpa);
475 ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
476 1, ARM_SMMU_POLL_TIMEOUT_US);
477
478 if (ret)
479 dev_err(smmu->dev, "GBPA not responding to update\n");
480 return ret;
481 }
482
arm_smmu_device_disable(struct arm_smmu_device * smmu)483 int arm_smmu_device_disable(struct arm_smmu_device *smmu)
484 {
485 int ret;
486
487 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
488 if (ret)
489 dev_err(smmu->dev, "failed to clear cr0\n");
490
491 return ret;
492 }
493
arm_smmu_device_group(struct device * dev)494 struct iommu_group *arm_smmu_device_group(struct device *dev)
495 {
496 struct iommu_group *group;
497
498 /*
499 * We don't support devices sharing stream IDs other than PCI RID
500 pick f473c7c1b189 fixup drv splt * aliases, since the necessary ID-to-device lookup becomes rather
501 * impractical given a potential sparse 32-bit stream ID space.
502 */
503 if (dev_is_pci(dev))
504 group = pci_device_group(dev);
505 else
506 group = generic_device_group(dev);
507
508 return group;
509 }
510
arm_smmu_of_xlate(struct device * dev,const struct of_phandle_args * args)511 int arm_smmu_of_xlate(struct device *dev, const struct of_phandle_args *args)
512 {
513 return iommu_fwspec_add_ids(dev, args->args, 1);
514 }
515
516
arm_smmu_get_resv_regions(struct device * dev,struct list_head * head)517 void arm_smmu_get_resv_regions(struct device *dev,
518 struct list_head *head)
519 {
520 struct iommu_resv_region *region;
521 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
522
523 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
524 prot, IOMMU_RESV_SW_MSI, GFP_KERNEL);
525 if (!region)
526 return;
527
528 list_add_tail(®ion->list, head);
529
530 iommu_dma_get_resv_regions(dev, head);
531 }
532
arm_smmu_init_one_queue(struct arm_smmu_device * smmu,struct arm_smmu_queue * q,void __iomem * page,unsigned long prod_off,unsigned long cons_off,size_t dwords,const char * name)533 int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
534 struct arm_smmu_queue *q, void __iomem *page,
535 unsigned long prod_off, unsigned long cons_off,
536 size_t dwords, const char *name)
537 {
538 size_t qsz;
539
540 do {
541 qsz = ((1 << q->llq.max_n_shift) * dwords) << 3;
542 q->base = dmam_alloc_coherent(smmu->dev, PAGE_ALIGN(qsz), &q->base_dma,
543 GFP_KERNEL);
544 if (q->base || qsz < PAGE_SIZE)
545 break;
546
547 q->llq.max_n_shift--;
548 } while (1);
549
550 if (!q->base) {
551 dev_err(smmu->dev,
552 "failed to allocate queue (0x%zx bytes) for %s\n",
553 qsz, name);
554 return -ENOMEM;
555 }
556
557 if (!WARN_ON(q->base_dma & (qsz - 1))) {
558 dev_info(smmu->dev, "allocated %u entries for %s\n",
559 1 << q->llq.max_n_shift, name);
560 }
561
562 q->prod_reg = page + prod_off;
563 q->cons_reg = page + cons_off;
564 q->ent_dwords = dwords;
565
566 q->q_base = Q_BASE_RWA;
567 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK;
568 q->q_base |= FIELD_PREP(Q_BASE_LOG2SIZE, q->llq.max_n_shift);
569
570 q->llq.prod = q->llq.cons = 0;
571 return 0;
572 }
573
574 /* Stream table initialization functions */
arm_smmu_init_strtab_2lvl(struct arm_smmu_device * smmu)575 static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
576 {
577 u32 l1size;
578 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
579 unsigned int last_sid_idx =
580 arm_smmu_strtab_l1_idx((1ULL << smmu->sid_bits) - 1);
581
582 /* Calculate the L1 size, capped to the SIDSIZE. */
583 cfg->l2.num_l1_ents = min(last_sid_idx + 1, STRTAB_MAX_L1_ENTRIES);
584 if (cfg->l2.num_l1_ents <= last_sid_idx)
585 dev_warn(smmu->dev,
586 "2-level strtab only covers %u/%u bits of SID\n",
587 ilog2(cfg->l2.num_l1_ents * STRTAB_NUM_L2_STES),
588 smmu->sid_bits);
589
590 l1size = cfg->l2.num_l1_ents * sizeof(struct arm_smmu_strtab_l1);
591 cfg->l2.l1tab = dmam_alloc_coherent(smmu->dev, PAGE_ALIGN(l1size), &cfg->l2.l1_dma,
592 GFP_KERNEL);
593 if (!cfg->l2.l1tab) {
594 dev_err(smmu->dev,
595 "failed to allocate l1 stream table (%u bytes)\n",
596 l1size);
597 return -ENOMEM;
598 }
599
600 cfg->l2.l2ptrs = devm_kcalloc(smmu->dev, cfg->l2.num_l1_ents,
601 sizeof(*cfg->l2.l2ptrs), GFP_KERNEL);
602 if (!cfg->l2.l2ptrs)
603 return -ENOMEM;
604
605 return 0;
606 }
607
arm_smmu_init_strtab_linear(struct arm_smmu_device * smmu)608 static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
609 {
610 u32 size;
611 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
612
613 size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste);
614 cfg->linear.table = dmam_alloc_coherent(smmu->dev, PAGE_ALIGN(size),
615 &cfg->linear.ste_dma,
616 GFP_KERNEL);
617 if (!cfg->linear.table) {
618 dev_err(smmu->dev,
619 "failed to allocate linear stream table (%u bytes)\n",
620 size);
621 return -ENOMEM;
622 }
623 cfg->linear.num_ents = 1 << smmu->sid_bits;
624
625 return 0;
626 }
627
arm_smmu_init_strtab(struct arm_smmu_device * smmu)628 int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
629 {
630 int ret;
631
632 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
633 ret = arm_smmu_init_strtab_2lvl(smmu);
634 else
635 ret = arm_smmu_init_strtab_linear(smmu);
636 if (ret)
637 return ret;
638
639 ida_init(&smmu->vmid_map);
640
641 return 0;
642 }
643
arm_smmu_write_strtab(struct arm_smmu_device * smmu)644 void arm_smmu_write_strtab(struct arm_smmu_device *smmu)
645 {
646 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
647 dma_addr_t dma;
648 u32 reg;
649
650 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
651 reg = FIELD_PREP(STRTAB_BASE_CFG_FMT,
652 STRTAB_BASE_CFG_FMT_2LVL) |
653 FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE,
654 ilog2(cfg->l2.num_l1_ents) + STRTAB_SPLIT) |
655 FIELD_PREP(STRTAB_BASE_CFG_SPLIT, STRTAB_SPLIT);
656 dma = cfg->l2.l1_dma;
657 } else {
658 reg = FIELD_PREP(STRTAB_BASE_CFG_FMT,
659 STRTAB_BASE_CFG_FMT_LINEAR) |
660 FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, smmu->sid_bits);
661 dma = cfg->linear.ste_dma;
662 }
663 writeq_relaxed((dma & STRTAB_BASE_ADDR_MASK) | STRTAB_BASE_RA,
664 smmu->base + ARM_SMMU_STRTAB_BASE);
665 writel_relaxed(reg, smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
666 }
667
arm_smmu_free_msis(void * data)668 static void arm_smmu_free_msis(void *data)
669 {
670 struct device *dev = data;
671
672 platform_device_msi_free_irqs_all(dev);
673 }
674
arm_smmu_write_msi_msg(struct msi_desc * desc,struct msi_msg * msg)675 static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
676 {
677 phys_addr_t doorbell;
678 struct device *dev = msi_desc_to_dev(desc);
679 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
680 phys_addr_t *cfg = arm_smmu_msi_cfg[desc->msi_index];
681
682 doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
683 doorbell &= MSI_CFG0_ADDR_MASK;
684
685 writeq_relaxed(doorbell, smmu->base + cfg[0]);
686 writel_relaxed(msg->data, smmu->base + cfg[1]);
687 writel_relaxed(ARM_SMMU_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]);
688 }
689
arm_smmu_setup_msis(struct arm_smmu_device * smmu)690 static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
691 {
692 int ret, nvec = ARM_SMMU_MAX_MSIS;
693 struct device *dev = smmu->dev;
694
695 /* Clear the MSI address regs */
696 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
697 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
698
699 if (smmu->features & ARM_SMMU_FEAT_PRI)
700 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
701 else
702 nvec--;
703
704 if (!(smmu->features & ARM_SMMU_FEAT_MSI))
705 return;
706
707 if (!dev->msi.domain) {
708 dev_info(smmu->dev, "msi_domain absent - falling back to wired irqs\n");
709 return;
710 }
711
712 /* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */
713 ret = platform_device_msi_init_and_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg);
714 if (ret) {
715 dev_warn(dev, "failed to allocate MSIs - falling back to wired irqs\n");
716 return;
717 }
718
719 smmu->evtq.q.irq = msi_get_virq(dev, EVTQ_MSI_INDEX);
720 smmu->gerr_irq = msi_get_virq(dev, GERROR_MSI_INDEX);
721 smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX);
722
723 /* Add callback to free MSIs on teardown */
724 devm_add_action_or_reset(dev, arm_smmu_free_msis, dev);
725 }
726
arm_smmu_setup_unique_irqs(struct arm_smmu_device * smmu,irqreturn_t evtqirq (int irq,void * dev),irqreturn_t gerrorirq (int irq,void * dev),irqreturn_t priirq (int irq,void * dev))727 void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu,
728 irqreturn_t evtqirq(int irq, void *dev),
729 irqreturn_t gerrorirq(int irq, void *dev),
730 irqreturn_t priirq(int irq, void *dev))
731 {
732 int irq, ret;
733
734 arm_smmu_setup_msis(smmu);
735
736 /* Request interrupt lines */
737 irq = smmu->evtq.q.irq;
738 if (irq) {
739 ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
740 evtqirq,
741 IRQF_ONESHOT,
742 "arm-smmu-v3-evtq", smmu);
743 if (ret < 0)
744 dev_warn(smmu->dev, "failed to enable evtq irq\n");
745 } else {
746 dev_warn(smmu->dev, "no evtq irq - events will not be reported!\n");
747 }
748
749 irq = smmu->gerr_irq;
750 if (irq) {
751 ret = devm_request_irq(smmu->dev, irq, gerrorirq,
752 0, "arm-smmu-v3-gerror", smmu);
753 if (ret < 0)
754 dev_warn(smmu->dev, "failed to enable gerror irq\n");
755 } else {
756 dev_warn(smmu->dev, "no gerr irq - errors will not be reported!\n");
757 }
758
759 if (smmu->features & ARM_SMMU_FEAT_PRI) {
760 irq = smmu->priq.q.irq;
761 if (irq) {
762 ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
763 priirq,
764 IRQF_ONESHOT,
765 "arm-smmu-v3-priq",
766 smmu);
767 if (ret < 0)
768 dev_warn(smmu->dev,
769 "failed to enable priq irq\n");
770 } else {
771 dev_warn(smmu->dev, "no priq irq - PRI will be broken\n");
772 }
773 }
774 }
775
arm_smmu_probe_irq(struct platform_device * pdev,struct arm_smmu_device * smmu)776 void arm_smmu_probe_irq(struct platform_device *pdev,
777 struct arm_smmu_device *smmu)
778 {
779 int irq;
780
781 irq = platform_get_irq_byname_optional(pdev, "combined");
782 if (irq > 0)
783 smmu->combined_irq = irq;
784 else {
785 irq = platform_get_irq_byname_optional(pdev, "eventq");
786 if (irq > 0)
787 smmu->evtq.q.irq = irq;
788
789 irq = platform_get_irq_byname_optional(pdev, "priq");
790 if (irq > 0)
791 smmu->priq.q.irq = irq;
792
793 irq = platform_get_irq_byname_optional(pdev, "gerror");
794 if (irq > 0)
795 smmu->gerr_irq = irq;
796 }
797 }
798
arm_smmu_register_iommu(struct arm_smmu_device * smmu,struct iommu_ops * ops,phys_addr_t ioaddr)799 int arm_smmu_register_iommu(struct arm_smmu_device *smmu,
800 struct iommu_ops *ops, phys_addr_t ioaddr)
801 {
802 int ret;
803 struct device *dev = smmu->dev;
804
805 ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL,
806 "smmu3.%pa", &ioaddr);
807 if (ret)
808 return ret;
809
810 ret = iommu_device_register(&smmu->iommu, ops, dev);
811 if (ret) {
812 dev_err(dev, "Failed to register iommu\n");
813 iommu_device_sysfs_remove(&smmu->iommu);
814 return ret;
815 }
816
817 return 0;
818 }
819
arm_smmu_unregister_iommu(struct arm_smmu_device * smmu)820 void arm_smmu_unregister_iommu(struct arm_smmu_device *smmu)
821 {
822 iommu_device_unregister(&smmu->iommu);
823 iommu_device_sysfs_remove(&smmu->iommu);
824 }
825