1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * IOMMU API for ARM architected SMMUv3 implementations. 4 * 5 * Copyright (C) 2015 ARM Limited 6 * 7 * Author: Will Deacon <will.deacon@arm.com> 8 * 9 * This driver is powered by bad coffee and bombay mix. 10 */ 11 12 #include <linux/acpi.h> 13 #include <linux/acpi_iort.h> 14 #include <linux/bitops.h> 15 #include <linux/crash_dump.h> 16 #include <linux/delay.h> 17 #include <linux/err.h> 18 #include <linux/interrupt.h> 19 #include <linux/io-pgtable.h> 20 #include <linux/iopoll.h> 21 #include <linux/module.h> 22 #include <linux/msi.h> 23 #include <linux/of.h> 24 #include <linux/of_address.h> 25 #include <linux/of_platform.h> 26 #include <linux/pci.h> 27 #include <linux/pci-ats.h> 28 #include <linux/platform_device.h> 29 #include <linux/string_choices.h> 30 #include <kunit/visibility.h> 31 #include <uapi/linux/iommufd.h> 32 33 #include "arm-smmu-v3.h" 34 #include "../../dma-iommu.h" 35 36 static bool disable_msipolling; 37 module_param(disable_msipolling, bool, 0444); 38 MODULE_PARM_DESC(disable_msipolling, 39 "Disable MSI-based polling for CMD_SYNC completion."); 40 41 static struct iommu_ops arm_smmu_ops; 42 static struct iommu_dirty_ops arm_smmu_dirty_ops; 43 44 enum arm_smmu_msi_index { 45 EVTQ_MSI_INDEX, 46 GERROR_MSI_INDEX, 47 PRIQ_MSI_INDEX, 48 ARM_SMMU_MAX_MSIS, 49 }; 50 51 #define NUM_ENTRY_QWORDS 8 52 static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64)); 53 static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64)); 54 55 static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = { 56 [EVTQ_MSI_INDEX] = { 57 ARM_SMMU_EVTQ_IRQ_CFG0, 58 ARM_SMMU_EVTQ_IRQ_CFG1, 59 ARM_SMMU_EVTQ_IRQ_CFG2, 60 }, 61 [GERROR_MSI_INDEX] = { 62 ARM_SMMU_GERROR_IRQ_CFG0, 63 ARM_SMMU_GERROR_IRQ_CFG1, 64 ARM_SMMU_GERROR_IRQ_CFG2, 65 }, 66 [PRIQ_MSI_INDEX] = { 67 ARM_SMMU_PRIQ_IRQ_CFG0, 68 ARM_SMMU_PRIQ_IRQ_CFG1, 69 ARM_SMMU_PRIQ_IRQ_CFG2, 70 }, 71 }; 72 73 struct arm_smmu_option_prop { 74 u32 opt; 75 const char *prop; 76 }; 77 78 DEFINE_XARRAY_ALLOC1(arm_smmu_asid_xa); 79 DEFINE_MUTEX(arm_smmu_asid_lock); 80 81 static struct arm_smmu_option_prop arm_smmu_options[] = { 82 { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" }, 83 { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"}, 84 { 0, NULL}, 85 }; 86 87 static const char * const event_str[] = { 88 [EVT_ID_BAD_STREAMID_CONFIG] = "C_BAD_STREAMID", 89 [EVT_ID_STE_FETCH_FAULT] = "F_STE_FETCH", 90 [EVT_ID_BAD_STE_CONFIG] = "C_BAD_STE", 91 [EVT_ID_STREAM_DISABLED_FAULT] = "F_STREAM_DISABLED", 92 [EVT_ID_BAD_SUBSTREAMID_CONFIG] = "C_BAD_SUBSTREAMID", 93 [EVT_ID_CD_FETCH_FAULT] = "F_CD_FETCH", 94 [EVT_ID_BAD_CD_CONFIG] = "C_BAD_CD", 95 [EVT_ID_TRANSLATION_FAULT] = "F_TRANSLATION", 96 [EVT_ID_ADDR_SIZE_FAULT] = "F_ADDR_SIZE", 97 [EVT_ID_ACCESS_FAULT] = "F_ACCESS", 98 [EVT_ID_PERMISSION_FAULT] = "F_PERMISSION", 99 [EVT_ID_VMS_FETCH_FAULT] = "F_VMS_FETCH", 100 }; 101 102 static const char * const event_class_str[] = { 103 [0] = "CD fetch", 104 [1] = "Stage 1 translation table fetch", 105 [2] = "Input address caused fault", 106 [3] = "Reserved", 107 }; 108 109 static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master); 110 111 static void parse_driver_options(struct arm_smmu_device *smmu) 112 { 113 int i = 0; 114 115 do { 116 if (of_property_read_bool(smmu->dev->of_node, 117 arm_smmu_options[i].prop)) { 118 smmu->options |= arm_smmu_options[i].opt; 119 dev_notice(smmu->dev, "option %s\n", 120 arm_smmu_options[i].prop); 121 } 122 } while (arm_smmu_options[++i].opt); 123 } 124 125 /* Low-level queue manipulation functions */ 126 static bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n) 127 { 128 u32 space, prod, cons; 129 130 prod = Q_IDX(q, q->prod); 131 cons = Q_IDX(q, q->cons); 132 133 if (Q_WRP(q, q->prod) == Q_WRP(q, q->cons)) 134 space = (1 << q->max_n_shift) - (prod - cons); 135 else 136 space = cons - prod; 137 138 return space >= n; 139 } 140 141 static bool queue_full(struct arm_smmu_ll_queue *q) 142 { 143 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) && 144 Q_WRP(q, q->prod) != Q_WRP(q, q->cons); 145 } 146 147 static bool queue_empty(struct arm_smmu_ll_queue *q) 148 { 149 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) && 150 Q_WRP(q, q->prod) == Q_WRP(q, q->cons); 151 } 152 153 static bool queue_consumed(struct arm_smmu_ll_queue *q, u32 prod) 154 { 155 return ((Q_WRP(q, q->cons) == Q_WRP(q, prod)) && 156 (Q_IDX(q, q->cons) > Q_IDX(q, prod))) || 157 ((Q_WRP(q, q->cons) != Q_WRP(q, prod)) && 158 (Q_IDX(q, q->cons) <= Q_IDX(q, prod))); 159 } 160 161 static void queue_sync_cons_out(struct arm_smmu_queue *q) 162 { 163 /* 164 * Ensure that all CPU accesses (reads and writes) to the queue 165 * are complete before we update the cons pointer. 166 */ 167 __iomb(); 168 writel_relaxed(q->llq.cons, q->cons_reg); 169 } 170 171 static void queue_inc_cons(struct arm_smmu_ll_queue *q) 172 { 173 u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1; 174 q->cons = Q_OVF(q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons); 175 } 176 177 static void queue_sync_cons_ovf(struct arm_smmu_queue *q) 178 { 179 struct arm_smmu_ll_queue *llq = &q->llq; 180 181 if (likely(Q_OVF(llq->prod) == Q_OVF(llq->cons))) 182 return; 183 184 llq->cons = Q_OVF(llq->prod) | Q_WRP(llq, llq->cons) | 185 Q_IDX(llq, llq->cons); 186 queue_sync_cons_out(q); 187 } 188 189 static int queue_sync_prod_in(struct arm_smmu_queue *q) 190 { 191 u32 prod; 192 int ret = 0; 193 194 /* 195 * We can't use the _relaxed() variant here, as we must prevent 196 * speculative reads of the queue before we have determined that 197 * prod has indeed moved. 198 */ 199 prod = readl(q->prod_reg); 200 201 if (Q_OVF(prod) != Q_OVF(q->llq.prod)) 202 ret = -EOVERFLOW; 203 204 q->llq.prod = prod; 205 return ret; 206 } 207 208 static u32 queue_inc_prod_n(struct arm_smmu_ll_queue *q, int n) 209 { 210 u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + n; 211 return Q_OVF(q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod); 212 } 213 214 static void queue_poll_init(struct arm_smmu_device *smmu, 215 struct arm_smmu_queue_poll *qp) 216 { 217 qp->delay = 1; 218 qp->spin_cnt = 0; 219 qp->wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV); 220 qp->timeout = ktime_add_us(ktime_get(), ARM_SMMU_POLL_TIMEOUT_US); 221 } 222 223 static int queue_poll(struct arm_smmu_queue_poll *qp) 224 { 225 if (ktime_compare(ktime_get(), qp->timeout) > 0) 226 return -ETIMEDOUT; 227 228 if (qp->wfe) { 229 wfe(); 230 } else if (++qp->spin_cnt < ARM_SMMU_POLL_SPIN_COUNT) { 231 cpu_relax(); 232 } else { 233 udelay(qp->delay); 234 qp->delay *= 2; 235 qp->spin_cnt = 0; 236 } 237 238 return 0; 239 } 240 241 static void queue_write(__le64 *dst, u64 *src, size_t n_dwords) 242 { 243 int i; 244 245 for (i = 0; i < n_dwords; ++i) 246 *dst++ = cpu_to_le64(*src++); 247 } 248 249 static void queue_read(u64 *dst, __le64 *src, size_t n_dwords) 250 { 251 int i; 252 253 for (i = 0; i < n_dwords; ++i) 254 *dst++ = le64_to_cpu(*src++); 255 } 256 257 static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent) 258 { 259 if (queue_empty(&q->llq)) 260 return -EAGAIN; 261 262 queue_read(ent, Q_ENT(q, q->llq.cons), q->ent_dwords); 263 queue_inc_cons(&q->llq); 264 queue_sync_cons_out(q); 265 return 0; 266 } 267 268 /* High-level queue accessors */ 269 static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) 270 { 271 memset(cmd, 0, 1 << CMDQ_ENT_SZ_SHIFT); 272 cmd[0] |= FIELD_PREP(CMDQ_0_OP, ent->opcode); 273 274 switch (ent->opcode) { 275 case CMDQ_OP_TLBI_EL2_ALL: 276 case CMDQ_OP_TLBI_NSNH_ALL: 277 break; 278 case CMDQ_OP_PREFETCH_CFG: 279 cmd[0] |= FIELD_PREP(CMDQ_PREFETCH_0_SID, ent->prefetch.sid); 280 break; 281 case CMDQ_OP_CFGI_CD: 282 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SSID, ent->cfgi.ssid); 283 fallthrough; 284 case CMDQ_OP_CFGI_STE: 285 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, ent->cfgi.sid); 286 cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_LEAF, ent->cfgi.leaf); 287 break; 288 case CMDQ_OP_CFGI_CD_ALL: 289 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, ent->cfgi.sid); 290 break; 291 case CMDQ_OP_CFGI_ALL: 292 /* Cover the entire SID range */ 293 cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_RANGE, 31); 294 break; 295 case CMDQ_OP_TLBI_NH_VA: 296 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid); 297 fallthrough; 298 case CMDQ_OP_TLBI_EL2_VA: 299 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_NUM, ent->tlbi.num); 300 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_SCALE, ent->tlbi.scale); 301 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid); 302 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf); 303 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TTL, ent->tlbi.ttl); 304 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TG, ent->tlbi.tg); 305 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_VA_MASK; 306 break; 307 case CMDQ_OP_TLBI_S2_IPA: 308 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_NUM, ent->tlbi.num); 309 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_SCALE, ent->tlbi.scale); 310 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid); 311 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf); 312 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TTL, ent->tlbi.ttl); 313 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TG, ent->tlbi.tg); 314 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_IPA_MASK; 315 break; 316 case CMDQ_OP_TLBI_NH_ASID: 317 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid); 318 fallthrough; 319 case CMDQ_OP_TLBI_NH_ALL: 320 case CMDQ_OP_TLBI_S12_VMALL: 321 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid); 322 break; 323 case CMDQ_OP_TLBI_EL2_ASID: 324 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid); 325 break; 326 case CMDQ_OP_ATC_INV: 327 cmd[0] |= FIELD_PREP(CMDQ_0_SSV, ent->substream_valid); 328 cmd[0] |= FIELD_PREP(CMDQ_ATC_0_GLOBAL, ent->atc.global); 329 cmd[0] |= FIELD_PREP(CMDQ_ATC_0_SSID, ent->atc.ssid); 330 cmd[0] |= FIELD_PREP(CMDQ_ATC_0_SID, ent->atc.sid); 331 cmd[1] |= FIELD_PREP(CMDQ_ATC_1_SIZE, ent->atc.size); 332 cmd[1] |= ent->atc.addr & CMDQ_ATC_1_ADDR_MASK; 333 break; 334 case CMDQ_OP_PRI_RESP: 335 cmd[0] |= FIELD_PREP(CMDQ_0_SSV, ent->substream_valid); 336 cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SSID, ent->pri.ssid); 337 cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SID, ent->pri.sid); 338 cmd[1] |= FIELD_PREP(CMDQ_PRI_1_GRPID, ent->pri.grpid); 339 switch (ent->pri.resp) { 340 case PRI_RESP_DENY: 341 case PRI_RESP_FAIL: 342 case PRI_RESP_SUCC: 343 break; 344 default: 345 return -EINVAL; 346 } 347 cmd[1] |= FIELD_PREP(CMDQ_PRI_1_RESP, ent->pri.resp); 348 break; 349 case CMDQ_OP_RESUME: 350 cmd[0] |= FIELD_PREP(CMDQ_RESUME_0_SID, ent->resume.sid); 351 cmd[0] |= FIELD_PREP(CMDQ_RESUME_0_RESP, ent->resume.resp); 352 cmd[1] |= FIELD_PREP(CMDQ_RESUME_1_STAG, ent->resume.stag); 353 break; 354 case CMDQ_OP_CMD_SYNC: 355 if (ent->sync.msiaddr) { 356 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_IRQ); 357 cmd[1] |= ent->sync.msiaddr & CMDQ_SYNC_1_MSIADDR_MASK; 358 } else { 359 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV); 360 } 361 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSH, ARM_SMMU_SH_ISH); 362 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSIATTR, ARM_SMMU_MEMATTR_OIWB); 363 break; 364 default: 365 return -ENOENT; 366 } 367 368 return 0; 369 } 370 371 static struct arm_smmu_cmdq *arm_smmu_get_cmdq(struct arm_smmu_device *smmu, 372 struct arm_smmu_cmdq_ent *ent) 373 { 374 struct arm_smmu_cmdq *cmdq = NULL; 375 376 if (smmu->impl_ops && smmu->impl_ops->get_secondary_cmdq) 377 cmdq = smmu->impl_ops->get_secondary_cmdq(smmu, ent); 378 379 return cmdq ?: &smmu->cmdq; 380 } 381 382 static bool arm_smmu_cmdq_needs_busy_polling(struct arm_smmu_device *smmu, 383 struct arm_smmu_cmdq *cmdq) 384 { 385 if (cmdq == &smmu->cmdq) 386 return false; 387 388 return smmu->options & ARM_SMMU_OPT_TEGRA241_CMDQV; 389 } 390 391 static void arm_smmu_cmdq_build_sync_cmd(u64 *cmd, struct arm_smmu_device *smmu, 392 struct arm_smmu_cmdq *cmdq, u32 prod) 393 { 394 struct arm_smmu_queue *q = &cmdq->q; 395 struct arm_smmu_cmdq_ent ent = { 396 .opcode = CMDQ_OP_CMD_SYNC, 397 }; 398 399 /* 400 * Beware that Hi16xx adds an extra 32 bits of goodness to its MSI 401 * payload, so the write will zero the entire command on that platform. 402 */ 403 if (smmu->options & ARM_SMMU_OPT_MSIPOLL) { 404 ent.sync.msiaddr = q->base_dma + Q_IDX(&q->llq, prod) * 405 q->ent_dwords * 8; 406 } 407 408 arm_smmu_cmdq_build_cmd(cmd, &ent); 409 if (arm_smmu_cmdq_needs_busy_polling(smmu, cmdq)) 410 u64p_replace_bits(cmd, CMDQ_SYNC_0_CS_NONE, CMDQ_SYNC_0_CS); 411 } 412 413 void __arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu, 414 struct arm_smmu_cmdq *cmdq) 415 { 416 static const char * const cerror_str[] = { 417 [CMDQ_ERR_CERROR_NONE_IDX] = "No error", 418 [CMDQ_ERR_CERROR_ILL_IDX] = "Illegal command", 419 [CMDQ_ERR_CERROR_ABT_IDX] = "Abort on command fetch", 420 [CMDQ_ERR_CERROR_ATC_INV_IDX] = "ATC invalidate timeout", 421 }; 422 struct arm_smmu_queue *q = &cmdq->q; 423 424 int i; 425 u64 cmd[CMDQ_ENT_DWORDS]; 426 u32 cons = readl_relaxed(q->cons_reg); 427 u32 idx = FIELD_GET(CMDQ_CONS_ERR, cons); 428 struct arm_smmu_cmdq_ent cmd_sync = { 429 .opcode = CMDQ_OP_CMD_SYNC, 430 }; 431 432 dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons, 433 idx < ARRAY_SIZE(cerror_str) ? cerror_str[idx] : "Unknown"); 434 435 switch (idx) { 436 case CMDQ_ERR_CERROR_ABT_IDX: 437 dev_err(smmu->dev, "retrying command fetch\n"); 438 return; 439 case CMDQ_ERR_CERROR_NONE_IDX: 440 return; 441 case CMDQ_ERR_CERROR_ATC_INV_IDX: 442 /* 443 * ATC Invalidation Completion timeout. CONS is still pointing 444 * at the CMD_SYNC. Attempt to complete other pending commands 445 * by repeating the CMD_SYNC, though we might well end up back 446 * here since the ATC invalidation may still be pending. 447 */ 448 return; 449 case CMDQ_ERR_CERROR_ILL_IDX: 450 default: 451 break; 452 } 453 454 /* 455 * We may have concurrent producers, so we need to be careful 456 * not to touch any of the shadow cmdq state. 457 */ 458 queue_read(cmd, Q_ENT(q, cons), q->ent_dwords); 459 dev_err(smmu->dev, "skipping command in error state:\n"); 460 for (i = 0; i < ARRAY_SIZE(cmd); ++i) 461 dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]); 462 463 /* Convert the erroneous command into a CMD_SYNC */ 464 arm_smmu_cmdq_build_cmd(cmd, &cmd_sync); 465 if (arm_smmu_cmdq_needs_busy_polling(smmu, cmdq)) 466 u64p_replace_bits(cmd, CMDQ_SYNC_0_CS_NONE, CMDQ_SYNC_0_CS); 467 468 queue_write(Q_ENT(q, cons), cmd, q->ent_dwords); 469 } 470 471 static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu) 472 { 473 __arm_smmu_cmdq_skip_err(smmu, &smmu->cmdq); 474 } 475 476 /* 477 * Command queue locking. 478 * This is a form of bastardised rwlock with the following major changes: 479 * 480 * - The only LOCK routines are exclusive_trylock() and shared_lock(). 481 * Neither have barrier semantics, and instead provide only a control 482 * dependency. 483 * 484 * - The UNLOCK routines are supplemented with shared_tryunlock(), which 485 * fails if the caller appears to be the last lock holder (yes, this is 486 * racy). All successful UNLOCK routines have RELEASE semantics. 487 */ 488 static void arm_smmu_cmdq_shared_lock(struct arm_smmu_cmdq *cmdq) 489 { 490 int val; 491 492 /* 493 * We can try to avoid the cmpxchg() loop by simply incrementing the 494 * lock counter. When held in exclusive state, the lock counter is set 495 * to INT_MIN so these increments won't hurt as the value will remain 496 * negative. 497 */ 498 if (atomic_fetch_inc_relaxed(&cmdq->lock) >= 0) 499 return; 500 501 do { 502 val = atomic_cond_read_relaxed(&cmdq->lock, VAL >= 0); 503 } while (atomic_cmpxchg_relaxed(&cmdq->lock, val, val + 1) != val); 504 } 505 506 static void arm_smmu_cmdq_shared_unlock(struct arm_smmu_cmdq *cmdq) 507 { 508 (void)atomic_dec_return_release(&cmdq->lock); 509 } 510 511 static bool arm_smmu_cmdq_shared_tryunlock(struct arm_smmu_cmdq *cmdq) 512 { 513 if (atomic_read(&cmdq->lock) == 1) 514 return false; 515 516 arm_smmu_cmdq_shared_unlock(cmdq); 517 return true; 518 } 519 520 #define arm_smmu_cmdq_exclusive_trylock_irqsave(cmdq, flags) \ 521 ({ \ 522 bool __ret; \ 523 local_irq_save(flags); \ 524 __ret = !atomic_cmpxchg_relaxed(&cmdq->lock, 0, INT_MIN); \ 525 if (!__ret) \ 526 local_irq_restore(flags); \ 527 __ret; \ 528 }) 529 530 #define arm_smmu_cmdq_exclusive_unlock_irqrestore(cmdq, flags) \ 531 ({ \ 532 atomic_set_release(&cmdq->lock, 0); \ 533 local_irq_restore(flags); \ 534 }) 535 536 537 /* 538 * Command queue insertion. 539 * This is made fiddly by our attempts to achieve some sort of scalability 540 * since there is one queue shared amongst all of the CPUs in the system. If 541 * you like mixed-size concurrency, dependency ordering and relaxed atomics, 542 * then you'll *love* this monstrosity. 543 * 544 * The basic idea is to split the queue up into ranges of commands that are 545 * owned by a given CPU; the owner may not have written all of the commands 546 * itself, but is responsible for advancing the hardware prod pointer when 547 * the time comes. The algorithm is roughly: 548 * 549 * 1. Allocate some space in the queue. At this point we also discover 550 * whether the head of the queue is currently owned by another CPU, 551 * or whether we are the owner. 552 * 553 * 2. Write our commands into our allocated slots in the queue. 554 * 555 * 3. Mark our slots as valid in arm_smmu_cmdq.valid_map. 556 * 557 * 4. If we are an owner: 558 * a. Wait for the previous owner to finish. 559 * b. Mark the queue head as unowned, which tells us the range 560 * that we are responsible for publishing. 561 * c. Wait for all commands in our owned range to become valid. 562 * d. Advance the hardware prod pointer. 563 * e. Tell the next owner we've finished. 564 * 565 * 5. If we are inserting a CMD_SYNC (we may or may not have been an 566 * owner), then we need to stick around until it has completed: 567 * a. If we have MSIs, the SMMU can write back into the CMD_SYNC 568 * to clear the first 4 bytes. 569 * b. Otherwise, we spin waiting for the hardware cons pointer to 570 * advance past our command. 571 * 572 * The devil is in the details, particularly the use of locking for handling 573 * SYNC completion and freeing up space in the queue before we think that it is 574 * full. 575 */ 576 static void __arm_smmu_cmdq_poll_set_valid_map(struct arm_smmu_cmdq *cmdq, 577 u32 sprod, u32 eprod, bool set) 578 { 579 u32 swidx, sbidx, ewidx, ebidx; 580 struct arm_smmu_ll_queue llq = { 581 .max_n_shift = cmdq->q.llq.max_n_shift, 582 .prod = sprod, 583 }; 584 585 ewidx = BIT_WORD(Q_IDX(&llq, eprod)); 586 ebidx = Q_IDX(&llq, eprod) % BITS_PER_LONG; 587 588 while (llq.prod != eprod) { 589 unsigned long mask; 590 atomic_long_t *ptr; 591 u32 limit = BITS_PER_LONG; 592 593 swidx = BIT_WORD(Q_IDX(&llq, llq.prod)); 594 sbidx = Q_IDX(&llq, llq.prod) % BITS_PER_LONG; 595 596 ptr = &cmdq->valid_map[swidx]; 597 598 if ((swidx == ewidx) && (sbidx < ebidx)) 599 limit = ebidx; 600 601 mask = GENMASK(limit - 1, sbidx); 602 603 /* 604 * The valid bit is the inverse of the wrap bit. This means 605 * that a zero-initialised queue is invalid and, after marking 606 * all entries as valid, they become invalid again when we 607 * wrap. 608 */ 609 if (set) { 610 atomic_long_xor(mask, ptr); 611 } else { /* Poll */ 612 unsigned long valid; 613 614 valid = (ULONG_MAX + !!Q_WRP(&llq, llq.prod)) & mask; 615 atomic_long_cond_read_relaxed(ptr, (VAL & mask) == valid); 616 } 617 618 llq.prod = queue_inc_prod_n(&llq, limit - sbidx); 619 } 620 } 621 622 /* Mark all entries in the range [sprod, eprod) as valid */ 623 static void arm_smmu_cmdq_set_valid_map(struct arm_smmu_cmdq *cmdq, 624 u32 sprod, u32 eprod) 625 { 626 __arm_smmu_cmdq_poll_set_valid_map(cmdq, sprod, eprod, true); 627 } 628 629 /* Wait for all entries in the range [sprod, eprod) to become valid */ 630 static void arm_smmu_cmdq_poll_valid_map(struct arm_smmu_cmdq *cmdq, 631 u32 sprod, u32 eprod) 632 { 633 __arm_smmu_cmdq_poll_set_valid_map(cmdq, sprod, eprod, false); 634 } 635 636 /* Wait for the command queue to become non-full */ 637 static int arm_smmu_cmdq_poll_until_not_full(struct arm_smmu_device *smmu, 638 struct arm_smmu_cmdq *cmdq, 639 struct arm_smmu_ll_queue *llq) 640 { 641 unsigned long flags; 642 struct arm_smmu_queue_poll qp; 643 int ret = 0; 644 645 /* 646 * Try to update our copy of cons by grabbing exclusive cmdq access. If 647 * that fails, spin until somebody else updates it for us. 648 */ 649 if (arm_smmu_cmdq_exclusive_trylock_irqsave(cmdq, flags)) { 650 WRITE_ONCE(cmdq->q.llq.cons, readl_relaxed(cmdq->q.cons_reg)); 651 arm_smmu_cmdq_exclusive_unlock_irqrestore(cmdq, flags); 652 llq->val = READ_ONCE(cmdq->q.llq.val); 653 return 0; 654 } 655 656 queue_poll_init(smmu, &qp); 657 do { 658 llq->val = READ_ONCE(cmdq->q.llq.val); 659 if (!queue_full(llq)) 660 break; 661 662 ret = queue_poll(&qp); 663 } while (!ret); 664 665 return ret; 666 } 667 668 /* 669 * Wait until the SMMU signals a CMD_SYNC completion MSI. 670 * Must be called with the cmdq lock held in some capacity. 671 */ 672 static int __arm_smmu_cmdq_poll_until_msi(struct arm_smmu_device *smmu, 673 struct arm_smmu_cmdq *cmdq, 674 struct arm_smmu_ll_queue *llq) 675 { 676 int ret = 0; 677 struct arm_smmu_queue_poll qp; 678 u32 *cmd = (u32 *)(Q_ENT(&cmdq->q, llq->prod)); 679 680 queue_poll_init(smmu, &qp); 681 682 /* 683 * The MSI won't generate an event, since it's being written back 684 * into the command queue. 685 */ 686 qp.wfe = false; 687 smp_cond_load_relaxed(cmd, !VAL || (ret = queue_poll(&qp))); 688 llq->cons = ret ? llq->prod : queue_inc_prod_n(llq, 1); 689 return ret; 690 } 691 692 /* 693 * Wait until the SMMU cons index passes llq->prod. 694 * Must be called with the cmdq lock held in some capacity. 695 */ 696 static int __arm_smmu_cmdq_poll_until_consumed(struct arm_smmu_device *smmu, 697 struct arm_smmu_cmdq *cmdq, 698 struct arm_smmu_ll_queue *llq) 699 { 700 struct arm_smmu_queue_poll qp; 701 u32 prod = llq->prod; 702 int ret = 0; 703 704 queue_poll_init(smmu, &qp); 705 llq->val = READ_ONCE(cmdq->q.llq.val); 706 do { 707 if (queue_consumed(llq, prod)) 708 break; 709 710 ret = queue_poll(&qp); 711 712 /* 713 * This needs to be a readl() so that our subsequent call 714 * to arm_smmu_cmdq_shared_tryunlock() can fail accurately. 715 * 716 * Specifically, we need to ensure that we observe all 717 * shared_lock()s by other CMD_SYNCs that share our owner, 718 * so that a failing call to tryunlock() means that we're 719 * the last one out and therefore we can safely advance 720 * cmdq->q.llq.cons. Roughly speaking: 721 * 722 * CPU 0 CPU1 CPU2 (us) 723 * 724 * if (sync) 725 * shared_lock(); 726 * 727 * dma_wmb(); 728 * set_valid_map(); 729 * 730 * if (owner) { 731 * poll_valid_map(); 732 * <control dependency> 733 * writel(prod_reg); 734 * 735 * readl(cons_reg); 736 * tryunlock(); 737 * 738 * Requires us to see CPU 0's shared_lock() acquisition. 739 */ 740 llq->cons = readl(cmdq->q.cons_reg); 741 } while (!ret); 742 743 return ret; 744 } 745 746 static int arm_smmu_cmdq_poll_until_sync(struct arm_smmu_device *smmu, 747 struct arm_smmu_cmdq *cmdq, 748 struct arm_smmu_ll_queue *llq) 749 { 750 if (smmu->options & ARM_SMMU_OPT_MSIPOLL && 751 !arm_smmu_cmdq_needs_busy_polling(smmu, cmdq)) 752 return __arm_smmu_cmdq_poll_until_msi(smmu, cmdq, llq); 753 754 return __arm_smmu_cmdq_poll_until_consumed(smmu, cmdq, llq); 755 } 756 757 static void arm_smmu_cmdq_write_entries(struct arm_smmu_cmdq *cmdq, u64 *cmds, 758 u32 prod, int n) 759 { 760 int i; 761 struct arm_smmu_ll_queue llq = { 762 .max_n_shift = cmdq->q.llq.max_n_shift, 763 .prod = prod, 764 }; 765 766 for (i = 0; i < n; ++i) { 767 u64 *cmd = &cmds[i * CMDQ_ENT_DWORDS]; 768 769 prod = queue_inc_prod_n(&llq, i); 770 queue_write(Q_ENT(&cmdq->q, prod), cmd, CMDQ_ENT_DWORDS); 771 } 772 } 773 774 /* 775 * This is the actual insertion function, and provides the following 776 * ordering guarantees to callers: 777 * 778 * - There is a dma_wmb() before publishing any commands to the queue. 779 * This can be relied upon to order prior writes to data structures 780 * in memory (such as a CD or an STE) before the command. 781 * 782 * - On completion of a CMD_SYNC, there is a control dependency. 783 * This can be relied upon to order subsequent writes to memory (e.g. 784 * freeing an IOVA) after completion of the CMD_SYNC. 785 * 786 * - Command insertion is totally ordered, so if two CPUs each race to 787 * insert their own list of commands then all of the commands from one 788 * CPU will appear before any of the commands from the other CPU. 789 */ 790 int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, 791 struct arm_smmu_cmdq *cmdq, u64 *cmds, int n, 792 bool sync) 793 { 794 u64 cmd_sync[CMDQ_ENT_DWORDS]; 795 u32 prod; 796 unsigned long flags; 797 bool owner; 798 struct arm_smmu_ll_queue llq, head; 799 int ret = 0; 800 801 llq.max_n_shift = cmdq->q.llq.max_n_shift; 802 803 /* 1. Allocate some space in the queue */ 804 local_irq_save(flags); 805 llq.val = READ_ONCE(cmdq->q.llq.val); 806 do { 807 u64 old; 808 809 while (!queue_has_space(&llq, n + sync)) { 810 local_irq_restore(flags); 811 if (arm_smmu_cmdq_poll_until_not_full(smmu, cmdq, &llq)) 812 dev_err_ratelimited(smmu->dev, "CMDQ timeout\n"); 813 local_irq_save(flags); 814 } 815 816 head.cons = llq.cons; 817 head.prod = queue_inc_prod_n(&llq, n + sync) | 818 CMDQ_PROD_OWNED_FLAG; 819 820 old = cmpxchg_relaxed(&cmdq->q.llq.val, llq.val, head.val); 821 if (old == llq.val) 822 break; 823 824 llq.val = old; 825 } while (1); 826 owner = !(llq.prod & CMDQ_PROD_OWNED_FLAG); 827 head.prod &= ~CMDQ_PROD_OWNED_FLAG; 828 llq.prod &= ~CMDQ_PROD_OWNED_FLAG; 829 830 /* 831 * 2. Write our commands into the queue 832 * Dependency ordering from the cmpxchg() loop above. 833 */ 834 arm_smmu_cmdq_write_entries(cmdq, cmds, llq.prod, n); 835 if (sync) { 836 prod = queue_inc_prod_n(&llq, n); 837 arm_smmu_cmdq_build_sync_cmd(cmd_sync, smmu, cmdq, prod); 838 queue_write(Q_ENT(&cmdq->q, prod), cmd_sync, CMDQ_ENT_DWORDS); 839 840 /* 841 * In order to determine completion of our CMD_SYNC, we must 842 * ensure that the queue can't wrap twice without us noticing. 843 * We achieve that by taking the cmdq lock as shared before 844 * marking our slot as valid. 845 */ 846 arm_smmu_cmdq_shared_lock(cmdq); 847 } 848 849 /* 3. Mark our slots as valid, ensuring commands are visible first */ 850 dma_wmb(); 851 arm_smmu_cmdq_set_valid_map(cmdq, llq.prod, head.prod); 852 853 /* 4. If we are the owner, take control of the SMMU hardware */ 854 if (owner) { 855 /* a. Wait for previous owner to finish */ 856 atomic_cond_read_relaxed(&cmdq->owner_prod, VAL == llq.prod); 857 858 /* b. Stop gathering work by clearing the owned flag */ 859 prod = atomic_fetch_andnot_relaxed(CMDQ_PROD_OWNED_FLAG, 860 &cmdq->q.llq.atomic.prod); 861 prod &= ~CMDQ_PROD_OWNED_FLAG; 862 863 /* 864 * c. Wait for any gathered work to be written to the queue. 865 * Note that we read our own entries so that we have the control 866 * dependency required by (d). 867 */ 868 arm_smmu_cmdq_poll_valid_map(cmdq, llq.prod, prod); 869 870 /* 871 * d. Advance the hardware prod pointer 872 * Control dependency ordering from the entries becoming valid. 873 */ 874 writel_relaxed(prod, cmdq->q.prod_reg); 875 876 /* 877 * e. Tell the next owner we're done 878 * Make sure we've updated the hardware first, so that we don't 879 * race to update prod and potentially move it backwards. 880 */ 881 atomic_set_release(&cmdq->owner_prod, prod); 882 } 883 884 /* 5. If we are inserting a CMD_SYNC, we must wait for it to complete */ 885 if (sync) { 886 llq.prod = queue_inc_prod_n(&llq, n); 887 ret = arm_smmu_cmdq_poll_until_sync(smmu, cmdq, &llq); 888 if (ret) { 889 dev_err_ratelimited(smmu->dev, 890 "CMD_SYNC timeout at 0x%08x [hwprod 0x%08x, hwcons 0x%08x]\n", 891 llq.prod, 892 readl_relaxed(cmdq->q.prod_reg), 893 readl_relaxed(cmdq->q.cons_reg)); 894 } 895 896 /* 897 * Try to unlock the cmdq lock. This will fail if we're the last 898 * reader, in which case we can safely update cmdq->q.llq.cons 899 */ 900 if (!arm_smmu_cmdq_shared_tryunlock(cmdq)) { 901 WRITE_ONCE(cmdq->q.llq.cons, llq.cons); 902 arm_smmu_cmdq_shared_unlock(cmdq); 903 } 904 } 905 906 local_irq_restore(flags); 907 return ret; 908 } 909 910 static int __arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu, 911 struct arm_smmu_cmdq_ent *ent, 912 bool sync) 913 { 914 u64 cmd[CMDQ_ENT_DWORDS]; 915 916 if (unlikely(arm_smmu_cmdq_build_cmd(cmd, ent))) { 917 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n", 918 ent->opcode); 919 return -EINVAL; 920 } 921 922 return arm_smmu_cmdq_issue_cmdlist( 923 smmu, arm_smmu_get_cmdq(smmu, ent), cmd, 1, sync); 924 } 925 926 static int arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu, 927 struct arm_smmu_cmdq_ent *ent) 928 { 929 return __arm_smmu_cmdq_issue_cmd(smmu, ent, false); 930 } 931 932 static int arm_smmu_cmdq_issue_cmd_with_sync(struct arm_smmu_device *smmu, 933 struct arm_smmu_cmdq_ent *ent) 934 { 935 return __arm_smmu_cmdq_issue_cmd(smmu, ent, true); 936 } 937 938 static void arm_smmu_cmdq_batch_init(struct arm_smmu_device *smmu, 939 struct arm_smmu_cmdq_batch *cmds, 940 struct arm_smmu_cmdq_ent *ent) 941 { 942 cmds->num = 0; 943 cmds->cmdq = arm_smmu_get_cmdq(smmu, ent); 944 } 945 946 static void arm_smmu_cmdq_batch_add(struct arm_smmu_device *smmu, 947 struct arm_smmu_cmdq_batch *cmds, 948 struct arm_smmu_cmdq_ent *cmd) 949 { 950 bool unsupported_cmd = !arm_smmu_cmdq_supports_cmd(cmds->cmdq, cmd); 951 bool force_sync = (cmds->num == CMDQ_BATCH_ENTRIES - 1) && 952 (smmu->options & ARM_SMMU_OPT_CMDQ_FORCE_SYNC); 953 int index; 954 955 if (force_sync || unsupported_cmd) { 956 arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmdq, cmds->cmds, 957 cmds->num, true); 958 arm_smmu_cmdq_batch_init(smmu, cmds, cmd); 959 } 960 961 if (cmds->num == CMDQ_BATCH_ENTRIES) { 962 arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmdq, cmds->cmds, 963 cmds->num, false); 964 arm_smmu_cmdq_batch_init(smmu, cmds, cmd); 965 } 966 967 index = cmds->num * CMDQ_ENT_DWORDS; 968 if (unlikely(arm_smmu_cmdq_build_cmd(&cmds->cmds[index], cmd))) { 969 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n", 970 cmd->opcode); 971 return; 972 } 973 974 cmds->num++; 975 } 976 977 static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, 978 struct arm_smmu_cmdq_batch *cmds) 979 { 980 return arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmdq, cmds->cmds, 981 cmds->num, true); 982 } 983 984 static void arm_smmu_page_response(struct device *dev, struct iopf_fault *unused, 985 struct iommu_page_response *resp) 986 { 987 struct arm_smmu_cmdq_ent cmd = {0}; 988 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 989 int sid = master->streams[0].id; 990 991 if (WARN_ON(!master->stall_enabled)) 992 return; 993 994 cmd.opcode = CMDQ_OP_RESUME; 995 cmd.resume.sid = sid; 996 cmd.resume.stag = resp->grpid; 997 switch (resp->code) { 998 case IOMMU_PAGE_RESP_INVALID: 999 case IOMMU_PAGE_RESP_FAILURE: 1000 cmd.resume.resp = CMDQ_RESUME_0_RESP_ABORT; 1001 break; 1002 case IOMMU_PAGE_RESP_SUCCESS: 1003 cmd.resume.resp = CMDQ_RESUME_0_RESP_RETRY; 1004 break; 1005 default: 1006 break; 1007 } 1008 1009 arm_smmu_cmdq_issue_cmd(master->smmu, &cmd); 1010 /* 1011 * Don't send a SYNC, it doesn't do anything for RESUME or PRI_RESP. 1012 * RESUME consumption guarantees that the stalled transaction will be 1013 * terminated... at some point in the future. PRI_RESP is fire and 1014 * forget. 1015 */ 1016 } 1017 1018 /* Context descriptor manipulation functions */ 1019 void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid) 1020 { 1021 struct arm_smmu_cmdq_ent cmd = { 1022 .opcode = smmu->features & ARM_SMMU_FEAT_E2H ? 1023 CMDQ_OP_TLBI_EL2_ASID : CMDQ_OP_TLBI_NH_ASID, 1024 .tlbi.asid = asid, 1025 }; 1026 1027 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); 1028 } 1029 1030 /* 1031 * Based on the value of ent report which bits of the STE the HW will access. It 1032 * would be nice if this was complete according to the spec, but minimally it 1033 * has to capture the bits this driver uses. 1034 */ 1035 VISIBLE_IF_KUNIT 1036 void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits) 1037 { 1038 unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent[0])); 1039 1040 used_bits[0] = cpu_to_le64(STRTAB_STE_0_V); 1041 if (!(ent[0] & cpu_to_le64(STRTAB_STE_0_V))) 1042 return; 1043 1044 used_bits[0] |= cpu_to_le64(STRTAB_STE_0_CFG); 1045 1046 /* S1 translates */ 1047 if (cfg & BIT(0)) { 1048 used_bits[0] |= cpu_to_le64(STRTAB_STE_0_S1FMT | 1049 STRTAB_STE_0_S1CTXPTR_MASK | 1050 STRTAB_STE_0_S1CDMAX); 1051 used_bits[1] |= 1052 cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR | 1053 STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH | 1054 STRTAB_STE_1_S1STALLD | STRTAB_STE_1_STRW | 1055 STRTAB_STE_1_EATS | STRTAB_STE_1_MEV); 1056 used_bits[2] |= cpu_to_le64(STRTAB_STE_2_S2VMID); 1057 1058 /* 1059 * See 13.5 Summary of attribute/permission configuration fields 1060 * for the SHCFG behavior. 1061 */ 1062 if (FIELD_GET(STRTAB_STE_1_S1DSS, le64_to_cpu(ent[1])) == 1063 STRTAB_STE_1_S1DSS_BYPASS) 1064 used_bits[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG); 1065 } 1066 1067 /* S2 translates */ 1068 if (cfg & BIT(1)) { 1069 used_bits[1] |= 1070 cpu_to_le64(STRTAB_STE_1_S2FWB | STRTAB_STE_1_EATS | 1071 STRTAB_STE_1_SHCFG | STRTAB_STE_1_MEV); 1072 used_bits[2] |= 1073 cpu_to_le64(STRTAB_STE_2_S2VMID | STRTAB_STE_2_VTCR | 1074 STRTAB_STE_2_S2AA64 | STRTAB_STE_2_S2ENDI | 1075 STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2S | 1076 STRTAB_STE_2_S2R); 1077 used_bits[3] |= cpu_to_le64(STRTAB_STE_3_S2TTB_MASK); 1078 } 1079 1080 if (cfg == STRTAB_STE_0_CFG_BYPASS) 1081 used_bits[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG); 1082 } 1083 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_get_ste_used); 1084 1085 /* 1086 * Figure out if we can do a hitless update of entry to become target. Returns a 1087 * bit mask where 1 indicates that qword needs to be set disruptively. 1088 * unused_update is an intermediate value of entry that has unused bits set to 1089 * their new values. 1090 */ 1091 static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, 1092 const __le64 *entry, const __le64 *target, 1093 __le64 *unused_update) 1094 { 1095 __le64 target_used[NUM_ENTRY_QWORDS] = {}; 1096 __le64 cur_used[NUM_ENTRY_QWORDS] = {}; 1097 u8 used_qword_diff = 0; 1098 unsigned int i; 1099 1100 writer->ops->get_used(entry, cur_used); 1101 writer->ops->get_used(target, target_used); 1102 1103 for (i = 0; i != NUM_ENTRY_QWORDS; i++) { 1104 /* 1105 * Check that masks are up to date, the make functions are not 1106 * allowed to set a bit to 1 if the used function doesn't say it 1107 * is used. 1108 */ 1109 WARN_ON_ONCE(target[i] & ~target_used[i]); 1110 1111 /* Bits can change because they are not currently being used */ 1112 unused_update[i] = (entry[i] & cur_used[i]) | 1113 (target[i] & ~cur_used[i]); 1114 /* 1115 * Each bit indicates that a used bit in a qword needs to be 1116 * changed after unused_update is applied. 1117 */ 1118 if ((unused_update[i] & target_used[i]) != target[i]) 1119 used_qword_diff |= 1 << i; 1120 } 1121 return used_qword_diff; 1122 } 1123 1124 static bool entry_set(struct arm_smmu_entry_writer *writer, __le64 *entry, 1125 const __le64 *target, unsigned int start, 1126 unsigned int len) 1127 { 1128 bool changed = false; 1129 unsigned int i; 1130 1131 for (i = start; len != 0; len--, i++) { 1132 if (entry[i] != target[i]) { 1133 WRITE_ONCE(entry[i], target[i]); 1134 changed = true; 1135 } 1136 } 1137 1138 if (changed) 1139 writer->ops->sync(writer); 1140 return changed; 1141 } 1142 1143 /* 1144 * Update the STE/CD to the target configuration. The transition from the 1145 * current entry to the target entry takes place over multiple steps that 1146 * attempts to make the transition hitless if possible. This function takes care 1147 * not to create a situation where the HW can perceive a corrupted entry. HW is 1148 * only required to have a 64 bit atomicity with stores from the CPU, while 1149 * entries are many 64 bit values big. 1150 * 1151 * The difference between the current value and the target value is analyzed to 1152 * determine which of three updates are required - disruptive, hitless or no 1153 * change. 1154 * 1155 * In the most general disruptive case we can make any update in three steps: 1156 * - Disrupting the entry (V=0) 1157 * - Fill now unused qwords, execpt qword 0 which contains V 1158 * - Make qword 0 have the final value and valid (V=1) with a single 64 1159 * bit store 1160 * 1161 * However this disrupts the HW while it is happening. There are several 1162 * interesting cases where a STE/CD can be updated without disturbing the HW 1163 * because only a small number of bits are changing (S1DSS, CONFIG, etc) or 1164 * because the used bits don't intersect. We can detect this by calculating how 1165 * many 64 bit values need update after adjusting the unused bits and skip the 1166 * V=0 process. This relies on the IGNORED behavior described in the 1167 * specification. 1168 */ 1169 VISIBLE_IF_KUNIT 1170 void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *entry, 1171 const __le64 *target) 1172 { 1173 __le64 unused_update[NUM_ENTRY_QWORDS]; 1174 u8 used_qword_diff; 1175 1176 used_qword_diff = 1177 arm_smmu_entry_qword_diff(writer, entry, target, unused_update); 1178 if (hweight8(used_qword_diff) == 1) { 1179 /* 1180 * Only one qword needs its used bits to be changed. This is a 1181 * hitless update, update all bits the current STE/CD is 1182 * ignoring to their new values, then update a single "critical 1183 * qword" to change the STE/CD and finally 0 out any bits that 1184 * are now unused in the target configuration. 1185 */ 1186 unsigned int critical_qword_index = ffs(used_qword_diff) - 1; 1187 1188 /* 1189 * Skip writing unused bits in the critical qword since we'll be 1190 * writing it in the next step anyways. This can save a sync 1191 * when the only change is in that qword. 1192 */ 1193 unused_update[critical_qword_index] = 1194 entry[critical_qword_index]; 1195 entry_set(writer, entry, unused_update, 0, NUM_ENTRY_QWORDS); 1196 entry_set(writer, entry, target, critical_qword_index, 1); 1197 entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS); 1198 } else if (used_qword_diff) { 1199 /* 1200 * At least two qwords need their inuse bits to be changed. This 1201 * requires a breaking update, zero the V bit, write all qwords 1202 * but 0, then set qword 0 1203 */ 1204 unused_update[0] = 0; 1205 entry_set(writer, entry, unused_update, 0, 1); 1206 entry_set(writer, entry, target, 1, NUM_ENTRY_QWORDS - 1); 1207 entry_set(writer, entry, target, 0, 1); 1208 } else { 1209 /* 1210 * No inuse bit changed. Sanity check that all unused bits are 0 1211 * in the entry. The target was already sanity checked by 1212 * compute_qword_diff(). 1213 */ 1214 WARN_ON_ONCE( 1215 entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS)); 1216 } 1217 } 1218 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_write_entry); 1219 1220 static void arm_smmu_sync_cd(struct arm_smmu_master *master, 1221 int ssid, bool leaf) 1222 { 1223 size_t i; 1224 struct arm_smmu_cmdq_batch cmds; 1225 struct arm_smmu_device *smmu = master->smmu; 1226 struct arm_smmu_cmdq_ent cmd = { 1227 .opcode = CMDQ_OP_CFGI_CD, 1228 .cfgi = { 1229 .ssid = ssid, 1230 .leaf = leaf, 1231 }, 1232 }; 1233 1234 arm_smmu_cmdq_batch_init(smmu, &cmds, &cmd); 1235 for (i = 0; i < master->num_streams; i++) { 1236 cmd.cfgi.sid = master->streams[i].id; 1237 arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); 1238 } 1239 1240 arm_smmu_cmdq_batch_submit(smmu, &cmds); 1241 } 1242 1243 static void arm_smmu_write_cd_l1_desc(struct arm_smmu_cdtab_l1 *dst, 1244 dma_addr_t l2ptr_dma) 1245 { 1246 u64 val = (l2ptr_dma & CTXDESC_L1_DESC_L2PTR_MASK) | CTXDESC_L1_DESC_V; 1247 1248 /* The HW has 64 bit atomicity with stores to the L2 CD table */ 1249 WRITE_ONCE(dst->l2ptr, cpu_to_le64(val)); 1250 } 1251 1252 static dma_addr_t arm_smmu_cd_l1_get_desc(const struct arm_smmu_cdtab_l1 *src) 1253 { 1254 return le64_to_cpu(src->l2ptr) & CTXDESC_L1_DESC_L2PTR_MASK; 1255 } 1256 1257 struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, 1258 u32 ssid) 1259 { 1260 struct arm_smmu_cdtab_l2 *l2; 1261 struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1262 1263 if (!arm_smmu_cdtab_allocated(cd_table)) 1264 return NULL; 1265 1266 if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) 1267 return &cd_table->linear.table[ssid]; 1268 1269 l2 = cd_table->l2.l2ptrs[arm_smmu_cdtab_l1_idx(ssid)]; 1270 if (!l2) 1271 return NULL; 1272 return &l2->cds[arm_smmu_cdtab_l2_idx(ssid)]; 1273 } 1274 1275 static struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, 1276 u32 ssid) 1277 { 1278 struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1279 struct arm_smmu_device *smmu = master->smmu; 1280 1281 might_sleep(); 1282 iommu_group_mutex_assert(master->dev); 1283 1284 if (!arm_smmu_cdtab_allocated(cd_table)) { 1285 if (arm_smmu_alloc_cd_tables(master)) 1286 return NULL; 1287 } 1288 1289 if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_64K_L2) { 1290 unsigned int idx = arm_smmu_cdtab_l1_idx(ssid); 1291 struct arm_smmu_cdtab_l2 **l2ptr = &cd_table->l2.l2ptrs[idx]; 1292 1293 if (!*l2ptr) { 1294 dma_addr_t l2ptr_dma; 1295 1296 *l2ptr = dma_alloc_coherent(smmu->dev, sizeof(**l2ptr), 1297 &l2ptr_dma, GFP_KERNEL); 1298 if (!*l2ptr) 1299 return NULL; 1300 1301 arm_smmu_write_cd_l1_desc(&cd_table->l2.l1tab[idx], 1302 l2ptr_dma); 1303 /* An invalid L1CD can be cached */ 1304 arm_smmu_sync_cd(master, ssid, false); 1305 } 1306 } 1307 return arm_smmu_get_cd_ptr(master, ssid); 1308 } 1309 1310 struct arm_smmu_cd_writer { 1311 struct arm_smmu_entry_writer writer; 1312 unsigned int ssid; 1313 }; 1314 1315 VISIBLE_IF_KUNIT 1316 void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits) 1317 { 1318 used_bits[0] = cpu_to_le64(CTXDESC_CD_0_V); 1319 if (!(ent[0] & cpu_to_le64(CTXDESC_CD_0_V))) 1320 return; 1321 memset(used_bits, 0xFF, sizeof(struct arm_smmu_cd)); 1322 1323 /* 1324 * If EPD0 is set by the make function it means 1325 * T0SZ/TG0/IR0/OR0/SH0/TTB0 are IGNORED 1326 */ 1327 if (ent[0] & cpu_to_le64(CTXDESC_CD_0_TCR_EPD0)) { 1328 used_bits[0] &= ~cpu_to_le64( 1329 CTXDESC_CD_0_TCR_T0SZ | CTXDESC_CD_0_TCR_TG0 | 1330 CTXDESC_CD_0_TCR_IRGN0 | CTXDESC_CD_0_TCR_ORGN0 | 1331 CTXDESC_CD_0_TCR_SH0); 1332 used_bits[1] &= ~cpu_to_le64(CTXDESC_CD_1_TTB0_MASK); 1333 } 1334 } 1335 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_get_cd_used); 1336 1337 static void arm_smmu_cd_writer_sync_entry(struct arm_smmu_entry_writer *writer) 1338 { 1339 struct arm_smmu_cd_writer *cd_writer = 1340 container_of(writer, struct arm_smmu_cd_writer, writer); 1341 1342 arm_smmu_sync_cd(writer->master, cd_writer->ssid, true); 1343 } 1344 1345 static const struct arm_smmu_entry_writer_ops arm_smmu_cd_writer_ops = { 1346 .sync = arm_smmu_cd_writer_sync_entry, 1347 .get_used = arm_smmu_get_cd_used, 1348 }; 1349 1350 void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, 1351 struct arm_smmu_cd *cdptr, 1352 const struct arm_smmu_cd *target) 1353 { 1354 bool target_valid = target->data[0] & cpu_to_le64(CTXDESC_CD_0_V); 1355 bool cur_valid = cdptr->data[0] & cpu_to_le64(CTXDESC_CD_0_V); 1356 struct arm_smmu_cd_writer cd_writer = { 1357 .writer = { 1358 .ops = &arm_smmu_cd_writer_ops, 1359 .master = master, 1360 }, 1361 .ssid = ssid, 1362 }; 1363 1364 if (ssid != IOMMU_NO_PASID && cur_valid != target_valid) { 1365 if (cur_valid) 1366 master->cd_table.used_ssids--; 1367 else 1368 master->cd_table.used_ssids++; 1369 } 1370 1371 arm_smmu_write_entry(&cd_writer.writer, cdptr->data, target->data); 1372 } 1373 1374 void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, 1375 struct arm_smmu_master *master, 1376 struct arm_smmu_domain *smmu_domain) 1377 { 1378 struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; 1379 const struct io_pgtable_cfg *pgtbl_cfg = 1380 &io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops)->cfg; 1381 typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = 1382 &pgtbl_cfg->arm_lpae_s1_cfg.tcr; 1383 1384 memset(target, 0, sizeof(*target)); 1385 1386 target->data[0] = cpu_to_le64( 1387 FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) | 1388 FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) | 1389 FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) | 1390 FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) | 1391 FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) | 1392 #ifdef __BIG_ENDIAN 1393 CTXDESC_CD_0_ENDI | 1394 #endif 1395 CTXDESC_CD_0_TCR_EPD1 | 1396 CTXDESC_CD_0_V | 1397 FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) | 1398 CTXDESC_CD_0_AA64 | 1399 (master->stall_enabled ? CTXDESC_CD_0_S : 0) | 1400 CTXDESC_CD_0_R | 1401 CTXDESC_CD_0_A | 1402 CTXDESC_CD_0_ASET | 1403 FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) 1404 ); 1405 1406 /* To enable dirty flag update, set both Access flag and dirty state update */ 1407 if (pgtbl_cfg->quirks & IO_PGTABLE_QUIRK_ARM_HD) 1408 target->data[0] |= cpu_to_le64(CTXDESC_CD_0_TCR_HA | 1409 CTXDESC_CD_0_TCR_HD); 1410 1411 target->data[1] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.ttbr & 1412 CTXDESC_CD_1_TTB0_MASK); 1413 target->data[3] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.mair); 1414 } 1415 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_s1_cd); 1416 1417 void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid) 1418 { 1419 struct arm_smmu_cd target = {}; 1420 struct arm_smmu_cd *cdptr; 1421 1422 if (!arm_smmu_cdtab_allocated(&master->cd_table)) 1423 return; 1424 cdptr = arm_smmu_get_cd_ptr(master, ssid); 1425 if (WARN_ON(!cdptr)) 1426 return; 1427 arm_smmu_write_cd_entry(master, ssid, cdptr, &target); 1428 } 1429 1430 static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) 1431 { 1432 int ret; 1433 size_t l1size; 1434 size_t max_contexts; 1435 struct arm_smmu_device *smmu = master->smmu; 1436 struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1437 1438 cd_table->s1cdmax = master->ssid_bits; 1439 max_contexts = 1 << cd_table->s1cdmax; 1440 1441 if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB) || 1442 max_contexts <= CTXDESC_L2_ENTRIES) { 1443 cd_table->s1fmt = STRTAB_STE_0_S1FMT_LINEAR; 1444 cd_table->linear.num_ents = max_contexts; 1445 1446 l1size = max_contexts * sizeof(struct arm_smmu_cd); 1447 cd_table->linear.table = dma_alloc_coherent(smmu->dev, l1size, 1448 &cd_table->cdtab_dma, 1449 GFP_KERNEL); 1450 if (!cd_table->linear.table) 1451 return -ENOMEM; 1452 } else { 1453 cd_table->s1fmt = STRTAB_STE_0_S1FMT_64K_L2; 1454 cd_table->l2.num_l1_ents = 1455 DIV_ROUND_UP(max_contexts, CTXDESC_L2_ENTRIES); 1456 1457 cd_table->l2.l2ptrs = kcalloc(cd_table->l2.num_l1_ents, 1458 sizeof(*cd_table->l2.l2ptrs), 1459 GFP_KERNEL); 1460 if (!cd_table->l2.l2ptrs) 1461 return -ENOMEM; 1462 1463 l1size = cd_table->l2.num_l1_ents * sizeof(struct arm_smmu_cdtab_l1); 1464 cd_table->l2.l1tab = dma_alloc_coherent(smmu->dev, l1size, 1465 &cd_table->cdtab_dma, 1466 GFP_KERNEL); 1467 if (!cd_table->l2.l2ptrs) { 1468 ret = -ENOMEM; 1469 goto err_free_l2ptrs; 1470 } 1471 } 1472 return 0; 1473 1474 err_free_l2ptrs: 1475 kfree(cd_table->l2.l2ptrs); 1476 cd_table->l2.l2ptrs = NULL; 1477 return ret; 1478 } 1479 1480 static void arm_smmu_free_cd_tables(struct arm_smmu_master *master) 1481 { 1482 int i; 1483 struct arm_smmu_device *smmu = master->smmu; 1484 struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1485 1486 if (cd_table->s1fmt != STRTAB_STE_0_S1FMT_LINEAR) { 1487 for (i = 0; i < cd_table->l2.num_l1_ents; i++) { 1488 if (!cd_table->l2.l2ptrs[i]) 1489 continue; 1490 1491 dma_free_coherent(smmu->dev, 1492 sizeof(*cd_table->l2.l2ptrs[i]), 1493 cd_table->l2.l2ptrs[i], 1494 arm_smmu_cd_l1_get_desc(&cd_table->l2.l1tab[i])); 1495 } 1496 kfree(cd_table->l2.l2ptrs); 1497 1498 dma_free_coherent(smmu->dev, 1499 cd_table->l2.num_l1_ents * 1500 sizeof(struct arm_smmu_cdtab_l1), 1501 cd_table->l2.l1tab, cd_table->cdtab_dma); 1502 } else { 1503 dma_free_coherent(smmu->dev, 1504 cd_table->linear.num_ents * 1505 sizeof(struct arm_smmu_cd), 1506 cd_table->linear.table, cd_table->cdtab_dma); 1507 } 1508 } 1509 1510 /* Stream table manipulation functions */ 1511 static void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst, 1512 dma_addr_t l2ptr_dma) 1513 { 1514 u64 val = 0; 1515 1516 val |= FIELD_PREP(STRTAB_L1_DESC_SPAN, STRTAB_SPLIT + 1); 1517 val |= l2ptr_dma & STRTAB_L1_DESC_L2PTR_MASK; 1518 1519 /* The HW has 64 bit atomicity with stores to the L2 STE table */ 1520 WRITE_ONCE(dst->l2ptr, cpu_to_le64(val)); 1521 } 1522 1523 struct arm_smmu_ste_writer { 1524 struct arm_smmu_entry_writer writer; 1525 u32 sid; 1526 }; 1527 1528 static void arm_smmu_ste_writer_sync_entry(struct arm_smmu_entry_writer *writer) 1529 { 1530 struct arm_smmu_ste_writer *ste_writer = 1531 container_of(writer, struct arm_smmu_ste_writer, writer); 1532 struct arm_smmu_cmdq_ent cmd = { 1533 .opcode = CMDQ_OP_CFGI_STE, 1534 .cfgi = { 1535 .sid = ste_writer->sid, 1536 .leaf = true, 1537 }, 1538 }; 1539 1540 arm_smmu_cmdq_issue_cmd_with_sync(writer->master->smmu, &cmd); 1541 } 1542 1543 static const struct arm_smmu_entry_writer_ops arm_smmu_ste_writer_ops = { 1544 .sync = arm_smmu_ste_writer_sync_entry, 1545 .get_used = arm_smmu_get_ste_used, 1546 }; 1547 1548 static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid, 1549 struct arm_smmu_ste *ste, 1550 const struct arm_smmu_ste *target) 1551 { 1552 struct arm_smmu_device *smmu = master->smmu; 1553 struct arm_smmu_ste_writer ste_writer = { 1554 .writer = { 1555 .ops = &arm_smmu_ste_writer_ops, 1556 .master = master, 1557 }, 1558 .sid = sid, 1559 }; 1560 1561 arm_smmu_write_entry(&ste_writer.writer, ste->data, target->data); 1562 1563 /* It's likely that we'll want to use the new STE soon */ 1564 if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH)) { 1565 struct arm_smmu_cmdq_ent 1566 prefetch_cmd = { .opcode = CMDQ_OP_PREFETCH_CFG, 1567 .prefetch = { 1568 .sid = sid, 1569 } }; 1570 1571 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd); 1572 } 1573 } 1574 1575 void arm_smmu_make_abort_ste(struct arm_smmu_ste *target) 1576 { 1577 memset(target, 0, sizeof(*target)); 1578 target->data[0] = cpu_to_le64( 1579 STRTAB_STE_0_V | 1580 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT)); 1581 } 1582 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_abort_ste); 1583 1584 VISIBLE_IF_KUNIT 1585 void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu, 1586 struct arm_smmu_ste *target) 1587 { 1588 memset(target, 0, sizeof(*target)); 1589 target->data[0] = cpu_to_le64( 1590 STRTAB_STE_0_V | 1591 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_BYPASS)); 1592 1593 if (smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR) 1594 target->data[1] = cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, 1595 STRTAB_STE_1_SHCFG_INCOMING)); 1596 } 1597 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_bypass_ste); 1598 1599 VISIBLE_IF_KUNIT 1600 void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, 1601 struct arm_smmu_master *master, bool ats_enabled, 1602 unsigned int s1dss) 1603 { 1604 struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1605 struct arm_smmu_device *smmu = master->smmu; 1606 1607 memset(target, 0, sizeof(*target)); 1608 target->data[0] = cpu_to_le64( 1609 STRTAB_STE_0_V | 1610 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S1_TRANS) | 1611 FIELD_PREP(STRTAB_STE_0_S1FMT, cd_table->s1fmt) | 1612 (cd_table->cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) | 1613 FIELD_PREP(STRTAB_STE_0_S1CDMAX, cd_table->s1cdmax)); 1614 1615 target->data[1] = cpu_to_le64( 1616 FIELD_PREP(STRTAB_STE_1_S1DSS, s1dss) | 1617 FIELD_PREP(STRTAB_STE_1_S1CIR, STRTAB_STE_1_S1C_CACHE_WBRA) | 1618 FIELD_PREP(STRTAB_STE_1_S1COR, STRTAB_STE_1_S1C_CACHE_WBRA) | 1619 FIELD_PREP(STRTAB_STE_1_S1CSH, ARM_SMMU_SH_ISH) | 1620 ((smmu->features & ARM_SMMU_FEAT_STALLS && 1621 !master->stall_enabled) ? 1622 STRTAB_STE_1_S1STALLD : 1623 0) | 1624 FIELD_PREP(STRTAB_STE_1_EATS, 1625 ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0)); 1626 1627 if ((smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR) && 1628 s1dss == STRTAB_STE_1_S1DSS_BYPASS) 1629 target->data[1] |= cpu_to_le64(FIELD_PREP( 1630 STRTAB_STE_1_SHCFG, STRTAB_STE_1_SHCFG_INCOMING)); 1631 1632 if (smmu->features & ARM_SMMU_FEAT_E2H) { 1633 /* 1634 * To support BTM the streamworld needs to match the 1635 * configuration of the CPU so that the ASID broadcasts are 1636 * properly matched. This means either S/NS-EL2-E2H (hypervisor) 1637 * or NS-EL1 (guest). Since an SVA domain can be installed in a 1638 * PASID this should always use a BTM compatible configuration 1639 * if the HW supports it. 1640 */ 1641 target->data[1] |= cpu_to_le64( 1642 FIELD_PREP(STRTAB_STE_1_STRW, STRTAB_STE_1_STRW_EL2)); 1643 } else { 1644 target->data[1] |= cpu_to_le64( 1645 FIELD_PREP(STRTAB_STE_1_STRW, STRTAB_STE_1_STRW_NSEL1)); 1646 1647 /* 1648 * VMID 0 is reserved for stage-2 bypass EL1 STEs, see 1649 * arm_smmu_domain_alloc_id() 1650 */ 1651 target->data[2] = 1652 cpu_to_le64(FIELD_PREP(STRTAB_STE_2_S2VMID, 0)); 1653 } 1654 } 1655 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_cdtable_ste); 1656 1657 void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, 1658 struct arm_smmu_master *master, 1659 struct arm_smmu_domain *smmu_domain, 1660 bool ats_enabled) 1661 { 1662 struct arm_smmu_s2_cfg *s2_cfg = &smmu_domain->s2_cfg; 1663 const struct io_pgtable_cfg *pgtbl_cfg = 1664 &io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops)->cfg; 1665 typeof(&pgtbl_cfg->arm_lpae_s2_cfg.vtcr) vtcr = 1666 &pgtbl_cfg->arm_lpae_s2_cfg.vtcr; 1667 u64 vtcr_val; 1668 struct arm_smmu_device *smmu = master->smmu; 1669 1670 memset(target, 0, sizeof(*target)); 1671 target->data[0] = cpu_to_le64( 1672 STRTAB_STE_0_V | 1673 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S2_TRANS)); 1674 1675 target->data[1] = cpu_to_le64( 1676 FIELD_PREP(STRTAB_STE_1_EATS, 1677 ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0)); 1678 1679 if (pgtbl_cfg->quirks & IO_PGTABLE_QUIRK_ARM_S2FWB) 1680 target->data[1] |= cpu_to_le64(STRTAB_STE_1_S2FWB); 1681 if (smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR) 1682 target->data[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, 1683 STRTAB_STE_1_SHCFG_INCOMING)); 1684 1685 vtcr_val = FIELD_PREP(STRTAB_STE_2_VTCR_S2T0SZ, vtcr->tsz) | 1686 FIELD_PREP(STRTAB_STE_2_VTCR_S2SL0, vtcr->sl) | 1687 FIELD_PREP(STRTAB_STE_2_VTCR_S2IR0, vtcr->irgn) | 1688 FIELD_PREP(STRTAB_STE_2_VTCR_S2OR0, vtcr->orgn) | 1689 FIELD_PREP(STRTAB_STE_2_VTCR_S2SH0, vtcr->sh) | 1690 FIELD_PREP(STRTAB_STE_2_VTCR_S2TG, vtcr->tg) | 1691 FIELD_PREP(STRTAB_STE_2_VTCR_S2PS, vtcr->ps); 1692 target->data[2] = cpu_to_le64( 1693 FIELD_PREP(STRTAB_STE_2_S2VMID, s2_cfg->vmid) | 1694 FIELD_PREP(STRTAB_STE_2_VTCR, vtcr_val) | 1695 STRTAB_STE_2_S2AA64 | 1696 #ifdef __BIG_ENDIAN 1697 STRTAB_STE_2_S2ENDI | 1698 #endif 1699 STRTAB_STE_2_S2PTW | 1700 (master->stall_enabled ? STRTAB_STE_2_S2S : 0) | 1701 STRTAB_STE_2_S2R); 1702 1703 target->data[3] = cpu_to_le64(pgtbl_cfg->arm_lpae_s2_cfg.vttbr & 1704 STRTAB_STE_3_S2TTB_MASK); 1705 } 1706 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_s2_domain_ste); 1707 1708 /* 1709 * This can safely directly manipulate the STE memory without a sync sequence 1710 * because the STE table has not been installed in the SMMU yet. 1711 */ 1712 static void arm_smmu_init_initial_stes(struct arm_smmu_ste *strtab, 1713 unsigned int nent) 1714 { 1715 unsigned int i; 1716 1717 for (i = 0; i < nent; ++i) { 1718 arm_smmu_make_abort_ste(strtab); 1719 strtab++; 1720 } 1721 } 1722 1723 static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) 1724 { 1725 dma_addr_t l2ptr_dma; 1726 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 1727 struct arm_smmu_strtab_l2 **l2table; 1728 1729 l2table = &cfg->l2.l2ptrs[arm_smmu_strtab_l1_idx(sid)]; 1730 if (*l2table) 1731 return 0; 1732 1733 *l2table = dmam_alloc_coherent(smmu->dev, sizeof(**l2table), 1734 &l2ptr_dma, GFP_KERNEL); 1735 if (!*l2table) { 1736 dev_err(smmu->dev, 1737 "failed to allocate l2 stream table for SID %u\n", 1738 sid); 1739 return -ENOMEM; 1740 } 1741 1742 arm_smmu_init_initial_stes((*l2table)->stes, 1743 ARRAY_SIZE((*l2table)->stes)); 1744 arm_smmu_write_strtab_l1_desc(&cfg->l2.l1tab[arm_smmu_strtab_l1_idx(sid)], 1745 l2ptr_dma); 1746 return 0; 1747 } 1748 1749 static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs) 1750 { 1751 struct arm_smmu_stream *stream_rhs = 1752 rb_entry(rhs, struct arm_smmu_stream, node); 1753 const u32 *sid_lhs = lhs; 1754 1755 if (*sid_lhs < stream_rhs->id) 1756 return -1; 1757 if (*sid_lhs > stream_rhs->id) 1758 return 1; 1759 return 0; 1760 } 1761 1762 static int arm_smmu_streams_cmp_node(struct rb_node *lhs, 1763 const struct rb_node *rhs) 1764 { 1765 return arm_smmu_streams_cmp_key( 1766 &rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs); 1767 } 1768 1769 static struct arm_smmu_master * 1770 arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid) 1771 { 1772 struct rb_node *node; 1773 1774 lockdep_assert_held(&smmu->streams_mutex); 1775 1776 node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key); 1777 if (!node) 1778 return NULL; 1779 return rb_entry(node, struct arm_smmu_stream, node)->master; 1780 } 1781 1782 /* IRQ and event handlers */ 1783 static void arm_smmu_decode_event(struct arm_smmu_device *smmu, u64 *raw, 1784 struct arm_smmu_event *event) 1785 { 1786 struct arm_smmu_master *master; 1787 1788 event->id = FIELD_GET(EVTQ_0_ID, raw[0]); 1789 event->sid = FIELD_GET(EVTQ_0_SID, raw[0]); 1790 event->ssv = FIELD_GET(EVTQ_0_SSV, raw[0]); 1791 event->ssid = event->ssv ? FIELD_GET(EVTQ_0_SSID, raw[0]) : IOMMU_NO_PASID; 1792 event->privileged = FIELD_GET(EVTQ_1_PnU, raw[1]); 1793 event->instruction = FIELD_GET(EVTQ_1_InD, raw[1]); 1794 event->s2 = FIELD_GET(EVTQ_1_S2, raw[1]); 1795 event->read = FIELD_GET(EVTQ_1_RnW, raw[1]); 1796 event->stag = FIELD_GET(EVTQ_1_STAG, raw[1]); 1797 event->stall = FIELD_GET(EVTQ_1_STALL, raw[1]); 1798 event->class = FIELD_GET(EVTQ_1_CLASS, raw[1]); 1799 event->iova = FIELD_GET(EVTQ_2_ADDR, raw[2]); 1800 event->ipa = raw[3] & EVTQ_3_IPA; 1801 event->fetch_addr = raw[3] & EVTQ_3_FETCH_ADDR; 1802 event->ttrnw = FIELD_GET(EVTQ_1_TT_READ, raw[1]); 1803 event->class_tt = false; 1804 event->dev = NULL; 1805 1806 if (event->id == EVT_ID_PERMISSION_FAULT) 1807 event->class_tt = (event->class == EVTQ_1_CLASS_TT); 1808 1809 mutex_lock(&smmu->streams_mutex); 1810 master = arm_smmu_find_master(smmu, event->sid); 1811 if (master) 1812 event->dev = get_device(master->dev); 1813 mutex_unlock(&smmu->streams_mutex); 1814 } 1815 1816 static int arm_smmu_handle_event(struct arm_smmu_device *smmu, u64 *evt, 1817 struct arm_smmu_event *event) 1818 { 1819 int ret = 0; 1820 u32 perm = 0; 1821 struct arm_smmu_master *master; 1822 struct iopf_fault fault_evt = { }; 1823 struct iommu_fault *flt = &fault_evt.fault; 1824 1825 switch (event->id) { 1826 case EVT_ID_BAD_STE_CONFIG: 1827 case EVT_ID_STREAM_DISABLED_FAULT: 1828 case EVT_ID_BAD_SUBSTREAMID_CONFIG: 1829 case EVT_ID_BAD_CD_CONFIG: 1830 case EVT_ID_TRANSLATION_FAULT: 1831 case EVT_ID_ADDR_SIZE_FAULT: 1832 case EVT_ID_ACCESS_FAULT: 1833 case EVT_ID_PERMISSION_FAULT: 1834 break; 1835 default: 1836 return -EOPNOTSUPP; 1837 } 1838 1839 if (event->stall) { 1840 if (event->read) 1841 perm |= IOMMU_FAULT_PERM_READ; 1842 else 1843 perm |= IOMMU_FAULT_PERM_WRITE; 1844 1845 if (event->instruction) 1846 perm |= IOMMU_FAULT_PERM_EXEC; 1847 1848 if (event->privileged) 1849 perm |= IOMMU_FAULT_PERM_PRIV; 1850 1851 flt->type = IOMMU_FAULT_PAGE_REQ; 1852 flt->prm = (struct iommu_fault_page_request){ 1853 .flags = IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE, 1854 .grpid = event->stag, 1855 .perm = perm, 1856 .addr = event->iova, 1857 }; 1858 1859 if (event->ssv) { 1860 flt->prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 1861 flt->prm.pasid = event->ssid; 1862 } 1863 } 1864 1865 mutex_lock(&smmu->streams_mutex); 1866 master = arm_smmu_find_master(smmu, event->sid); 1867 if (!master) { 1868 ret = -EINVAL; 1869 goto out_unlock; 1870 } 1871 1872 if (event->stall) 1873 ret = iommu_report_device_fault(master->dev, &fault_evt); 1874 else if (master->vmaster && !event->s2) 1875 ret = arm_vmaster_report_event(master->vmaster, evt); 1876 else 1877 ret = -EOPNOTSUPP; /* Unhandled events should be pinned */ 1878 out_unlock: 1879 mutex_unlock(&smmu->streams_mutex); 1880 return ret; 1881 } 1882 1883 static void arm_smmu_dump_raw_event(struct arm_smmu_device *smmu, u64 *raw, 1884 struct arm_smmu_event *event) 1885 { 1886 int i; 1887 1888 dev_err(smmu->dev, "event 0x%02x received:\n", event->id); 1889 1890 for (i = 0; i < EVTQ_ENT_DWORDS; ++i) 1891 dev_err(smmu->dev, "\t0x%016llx\n", raw[i]); 1892 } 1893 1894 #define ARM_SMMU_EVT_KNOWN(e) ((e)->id < ARRAY_SIZE(event_str) && event_str[(e)->id]) 1895 #define ARM_SMMU_LOG_EVT_STR(e) ARM_SMMU_EVT_KNOWN(e) ? event_str[(e)->id] : "UNKNOWN" 1896 #define ARM_SMMU_LOG_CLIENT(e) (e)->dev ? dev_name((e)->dev) : "(unassigned sid)" 1897 1898 static void arm_smmu_dump_event(struct arm_smmu_device *smmu, u64 *raw, 1899 struct arm_smmu_event *evt, 1900 struct ratelimit_state *rs) 1901 { 1902 if (!__ratelimit(rs)) 1903 return; 1904 1905 arm_smmu_dump_raw_event(smmu, raw, evt); 1906 1907 switch (evt->id) { 1908 case EVT_ID_TRANSLATION_FAULT: 1909 case EVT_ID_ADDR_SIZE_FAULT: 1910 case EVT_ID_ACCESS_FAULT: 1911 case EVT_ID_PERMISSION_FAULT: 1912 dev_err(smmu->dev, "event: %s client: %s sid: %#x ssid: %#x iova: %#llx ipa: %#llx", 1913 ARM_SMMU_LOG_EVT_STR(evt), ARM_SMMU_LOG_CLIENT(evt), 1914 evt->sid, evt->ssid, evt->iova, evt->ipa); 1915 1916 dev_err(smmu->dev, "%s %s %s %s \"%s\"%s%s stag: %#x", 1917 evt->privileged ? "priv" : "unpriv", 1918 evt->instruction ? "inst" : "data", 1919 str_read_write(evt->read), 1920 evt->s2 ? "s2" : "s1", event_class_str[evt->class], 1921 evt->class_tt ? (evt->ttrnw ? " ttd_read" : " ttd_write") : "", 1922 evt->stall ? " stall" : "", evt->stag); 1923 1924 break; 1925 1926 case EVT_ID_STE_FETCH_FAULT: 1927 case EVT_ID_CD_FETCH_FAULT: 1928 case EVT_ID_VMS_FETCH_FAULT: 1929 dev_err(smmu->dev, "event: %s client: %s sid: %#x ssid: %#x fetch_addr: %#llx", 1930 ARM_SMMU_LOG_EVT_STR(evt), ARM_SMMU_LOG_CLIENT(evt), 1931 evt->sid, evt->ssid, evt->fetch_addr); 1932 1933 break; 1934 1935 default: 1936 dev_err(smmu->dev, "event: %s client: %s sid: %#x ssid: %#x", 1937 ARM_SMMU_LOG_EVT_STR(evt), ARM_SMMU_LOG_CLIENT(evt), 1938 evt->sid, evt->ssid); 1939 } 1940 } 1941 1942 static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev) 1943 { 1944 u64 evt[EVTQ_ENT_DWORDS]; 1945 struct arm_smmu_event event = {0}; 1946 struct arm_smmu_device *smmu = dev; 1947 struct arm_smmu_queue *q = &smmu->evtq.q; 1948 struct arm_smmu_ll_queue *llq = &q->llq; 1949 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, 1950 DEFAULT_RATELIMIT_BURST); 1951 1952 do { 1953 while (!queue_remove_raw(q, evt)) { 1954 arm_smmu_decode_event(smmu, evt, &event); 1955 if (arm_smmu_handle_event(smmu, evt, &event)) 1956 arm_smmu_dump_event(smmu, evt, &event, &rs); 1957 1958 put_device(event.dev); 1959 cond_resched(); 1960 } 1961 1962 /* 1963 * Not much we can do on overflow, so scream and pretend we're 1964 * trying harder. 1965 */ 1966 if (queue_sync_prod_in(q) == -EOVERFLOW) 1967 dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n"); 1968 } while (!queue_empty(llq)); 1969 1970 /* Sync our overflow flag, as we believe we're up to speed */ 1971 queue_sync_cons_ovf(q); 1972 return IRQ_HANDLED; 1973 } 1974 1975 static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) 1976 { 1977 u32 sid, ssid; 1978 u16 grpid; 1979 bool ssv, last; 1980 1981 sid = FIELD_GET(PRIQ_0_SID, evt[0]); 1982 ssv = FIELD_GET(PRIQ_0_SSID_V, evt[0]); 1983 ssid = ssv ? FIELD_GET(PRIQ_0_SSID, evt[0]) : IOMMU_NO_PASID; 1984 last = FIELD_GET(PRIQ_0_PRG_LAST, evt[0]); 1985 grpid = FIELD_GET(PRIQ_1_PRG_IDX, evt[1]); 1986 1987 dev_info(smmu->dev, "unexpected PRI request received:\n"); 1988 dev_info(smmu->dev, 1989 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n", 1990 sid, ssid, grpid, last ? "L" : "", 1991 evt[0] & PRIQ_0_PERM_PRIV ? "" : "un", 1992 evt[0] & PRIQ_0_PERM_READ ? "R" : "", 1993 evt[0] & PRIQ_0_PERM_WRITE ? "W" : "", 1994 evt[0] & PRIQ_0_PERM_EXEC ? "X" : "", 1995 evt[1] & PRIQ_1_ADDR_MASK); 1996 1997 if (last) { 1998 struct arm_smmu_cmdq_ent cmd = { 1999 .opcode = CMDQ_OP_PRI_RESP, 2000 .substream_valid = ssv, 2001 .pri = { 2002 .sid = sid, 2003 .ssid = ssid, 2004 .grpid = grpid, 2005 .resp = PRI_RESP_DENY, 2006 }, 2007 }; 2008 2009 arm_smmu_cmdq_issue_cmd(smmu, &cmd); 2010 } 2011 } 2012 2013 static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) 2014 { 2015 struct arm_smmu_device *smmu = dev; 2016 struct arm_smmu_queue *q = &smmu->priq.q; 2017 struct arm_smmu_ll_queue *llq = &q->llq; 2018 u64 evt[PRIQ_ENT_DWORDS]; 2019 2020 do { 2021 while (!queue_remove_raw(q, evt)) 2022 arm_smmu_handle_ppr(smmu, evt); 2023 2024 if (queue_sync_prod_in(q) == -EOVERFLOW) 2025 dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n"); 2026 } while (!queue_empty(llq)); 2027 2028 /* Sync our overflow flag, as we believe we're up to speed */ 2029 queue_sync_cons_ovf(q); 2030 return IRQ_HANDLED; 2031 } 2032 2033 static int arm_smmu_device_disable(struct arm_smmu_device *smmu); 2034 2035 static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) 2036 { 2037 u32 gerror, gerrorn, active; 2038 struct arm_smmu_device *smmu = dev; 2039 2040 gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR); 2041 gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN); 2042 2043 active = gerror ^ gerrorn; 2044 if (!(active & GERROR_ERR_MASK)) 2045 return IRQ_NONE; /* No errors pending */ 2046 2047 dev_warn(smmu->dev, 2048 "unexpected global error reported (0x%08x), this could be serious\n", 2049 active); 2050 2051 if (active & GERROR_SFM_ERR) { 2052 dev_err(smmu->dev, "device has entered Service Failure Mode!\n"); 2053 arm_smmu_device_disable(smmu); 2054 } 2055 2056 if (active & GERROR_MSI_GERROR_ABT_ERR) 2057 dev_warn(smmu->dev, "GERROR MSI write aborted\n"); 2058 2059 if (active & GERROR_MSI_PRIQ_ABT_ERR) 2060 dev_warn(smmu->dev, "PRIQ MSI write aborted\n"); 2061 2062 if (active & GERROR_MSI_EVTQ_ABT_ERR) 2063 dev_warn(smmu->dev, "EVTQ MSI write aborted\n"); 2064 2065 if (active & GERROR_MSI_CMDQ_ABT_ERR) 2066 dev_warn(smmu->dev, "CMDQ MSI write aborted\n"); 2067 2068 if (active & GERROR_PRIQ_ABT_ERR) 2069 dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n"); 2070 2071 if (active & GERROR_EVTQ_ABT_ERR) 2072 dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n"); 2073 2074 if (active & GERROR_CMDQ_ERR) 2075 arm_smmu_cmdq_skip_err(smmu); 2076 2077 writel(gerror, smmu->base + ARM_SMMU_GERRORN); 2078 return IRQ_HANDLED; 2079 } 2080 2081 static irqreturn_t arm_smmu_combined_irq_thread(int irq, void *dev) 2082 { 2083 struct arm_smmu_device *smmu = dev; 2084 2085 arm_smmu_evtq_thread(irq, dev); 2086 if (smmu->features & ARM_SMMU_FEAT_PRI) 2087 arm_smmu_priq_thread(irq, dev); 2088 2089 return IRQ_HANDLED; 2090 } 2091 2092 static irqreturn_t arm_smmu_combined_irq_handler(int irq, void *dev) 2093 { 2094 arm_smmu_gerror_handler(irq, dev); 2095 return IRQ_WAKE_THREAD; 2096 } 2097 2098 static void 2099 arm_smmu_atc_inv_to_cmd(int ssid, unsigned long iova, size_t size, 2100 struct arm_smmu_cmdq_ent *cmd) 2101 { 2102 size_t log2_span; 2103 size_t span_mask; 2104 /* ATC invalidates are always on 4096-bytes pages */ 2105 size_t inval_grain_shift = 12; 2106 unsigned long page_start, page_end; 2107 2108 /* 2109 * ATS and PASID: 2110 * 2111 * If substream_valid is clear, the PCIe TLP is sent without a PASID 2112 * prefix. In that case all ATC entries within the address range are 2113 * invalidated, including those that were requested with a PASID! There 2114 * is no way to invalidate only entries without PASID. 2115 * 2116 * When using STRTAB_STE_1_S1DSS_SSID0 (reserving CD 0 for non-PASID 2117 * traffic), translation requests without PASID create ATC entries 2118 * without PASID, which must be invalidated with substream_valid clear. 2119 * This has the unpleasant side-effect of invalidating all PASID-tagged 2120 * ATC entries within the address range. 2121 */ 2122 *cmd = (struct arm_smmu_cmdq_ent) { 2123 .opcode = CMDQ_OP_ATC_INV, 2124 .substream_valid = (ssid != IOMMU_NO_PASID), 2125 .atc.ssid = ssid, 2126 }; 2127 2128 if (!size) { 2129 cmd->atc.size = ATC_INV_SIZE_ALL; 2130 return; 2131 } 2132 2133 page_start = iova >> inval_grain_shift; 2134 page_end = (iova + size - 1) >> inval_grain_shift; 2135 2136 /* 2137 * In an ATS Invalidate Request, the address must be aligned on the 2138 * range size, which must be a power of two number of page sizes. We 2139 * thus have to choose between grossly over-invalidating the region, or 2140 * splitting the invalidation into multiple commands. For simplicity 2141 * we'll go with the first solution, but should refine it in the future 2142 * if multiple commands are shown to be more efficient. 2143 * 2144 * Find the smallest power of two that covers the range. The most 2145 * significant differing bit between the start and end addresses, 2146 * fls(start ^ end), indicates the required span. For example: 2147 * 2148 * We want to invalidate pages [8; 11]. This is already the ideal range: 2149 * x = 0b1000 ^ 0b1011 = 0b11 2150 * span = 1 << fls(x) = 4 2151 * 2152 * To invalidate pages [7; 10], we need to invalidate [0; 15]: 2153 * x = 0b0111 ^ 0b1010 = 0b1101 2154 * span = 1 << fls(x) = 16 2155 */ 2156 log2_span = fls_long(page_start ^ page_end); 2157 span_mask = (1ULL << log2_span) - 1; 2158 2159 page_start &= ~span_mask; 2160 2161 cmd->atc.addr = page_start << inval_grain_shift; 2162 cmd->atc.size = log2_span; 2163 } 2164 2165 static int arm_smmu_atc_inv_master(struct arm_smmu_master *master, 2166 ioasid_t ssid) 2167 { 2168 int i; 2169 struct arm_smmu_cmdq_ent cmd; 2170 struct arm_smmu_cmdq_batch cmds; 2171 2172 arm_smmu_atc_inv_to_cmd(ssid, 0, 0, &cmd); 2173 2174 arm_smmu_cmdq_batch_init(master->smmu, &cmds, &cmd); 2175 for (i = 0; i < master->num_streams; i++) { 2176 cmd.atc.sid = master->streams[i].id; 2177 arm_smmu_cmdq_batch_add(master->smmu, &cmds, &cmd); 2178 } 2179 2180 return arm_smmu_cmdq_batch_submit(master->smmu, &cmds); 2181 } 2182 2183 int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, 2184 unsigned long iova, size_t size) 2185 { 2186 struct arm_smmu_master_domain *master_domain; 2187 int i; 2188 unsigned long flags; 2189 struct arm_smmu_cmdq_ent cmd = { 2190 .opcode = CMDQ_OP_ATC_INV, 2191 }; 2192 struct arm_smmu_cmdq_batch cmds; 2193 2194 if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_ATS)) 2195 return 0; 2196 2197 /* 2198 * Ensure that we've completed prior invalidation of the main TLBs 2199 * before we read 'nr_ats_masters' in case of a concurrent call to 2200 * arm_smmu_enable_ats(): 2201 * 2202 * // unmap() // arm_smmu_enable_ats() 2203 * TLBI+SYNC atomic_inc(&nr_ats_masters); 2204 * smp_mb(); [...] 2205 * atomic_read(&nr_ats_masters); pci_enable_ats() // writel() 2206 * 2207 * Ensures that we always see the incremented 'nr_ats_masters' count if 2208 * ATS was enabled at the PCI device before completion of the TLBI. 2209 */ 2210 smp_mb(); 2211 if (!atomic_read(&smmu_domain->nr_ats_masters)) 2212 return 0; 2213 2214 arm_smmu_cmdq_batch_init(smmu_domain->smmu, &cmds, &cmd); 2215 2216 spin_lock_irqsave(&smmu_domain->devices_lock, flags); 2217 list_for_each_entry(master_domain, &smmu_domain->devices, 2218 devices_elm) { 2219 struct arm_smmu_master *master = master_domain->master; 2220 2221 if (!master->ats_enabled) 2222 continue; 2223 2224 if (master_domain->nested_ats_flush) { 2225 /* 2226 * If a S2 used as a nesting parent is changed we have 2227 * no option but to completely flush the ATC. 2228 */ 2229 arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd); 2230 } else { 2231 arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, 2232 &cmd); 2233 } 2234 2235 for (i = 0; i < master->num_streams; i++) { 2236 cmd.atc.sid = master->streams[i].id; 2237 arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd); 2238 } 2239 } 2240 spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 2241 2242 return arm_smmu_cmdq_batch_submit(smmu_domain->smmu, &cmds); 2243 } 2244 2245 /* IO_PGTABLE API */ 2246 static void arm_smmu_tlb_inv_context(void *cookie) 2247 { 2248 struct arm_smmu_domain *smmu_domain = cookie; 2249 struct arm_smmu_device *smmu = smmu_domain->smmu; 2250 struct arm_smmu_cmdq_ent cmd; 2251 2252 /* 2253 * NOTE: when io-pgtable is in non-strict mode, we may get here with 2254 * PTEs previously cleared by unmaps on the current CPU not yet visible 2255 * to the SMMU. We are relying on the dma_wmb() implicit during cmd 2256 * insertion to guarantee those are observed before the TLBI. Do be 2257 * careful, 007. 2258 */ 2259 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 2260 arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); 2261 } else { 2262 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL; 2263 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; 2264 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); 2265 } 2266 arm_smmu_atc_inv_domain(smmu_domain, 0, 0); 2267 } 2268 2269 static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, 2270 unsigned long iova, size_t size, 2271 size_t granule, 2272 struct arm_smmu_domain *smmu_domain) 2273 { 2274 struct arm_smmu_device *smmu = smmu_domain->smmu; 2275 unsigned long end = iova + size, num_pages = 0, tg = 0; 2276 size_t inv_range = granule; 2277 struct arm_smmu_cmdq_batch cmds; 2278 2279 if (!size) 2280 return; 2281 2282 if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) { 2283 /* Get the leaf page size */ 2284 tg = __ffs(smmu_domain->domain.pgsize_bitmap); 2285 2286 num_pages = size >> tg; 2287 2288 /* Convert page size of 12,14,16 (log2) to 1,2,3 */ 2289 cmd->tlbi.tg = (tg - 10) / 2; 2290 2291 /* 2292 * Determine what level the granule is at. For non-leaf, both 2293 * io-pgtable and SVA pass a nominal last-level granule because 2294 * they don't know what level(s) actually apply, so ignore that 2295 * and leave TTL=0. However for various errata reasons we still 2296 * want to use a range command, so avoid the SVA corner case 2297 * where both scale and num could be 0 as well. 2298 */ 2299 if (cmd->tlbi.leaf) 2300 cmd->tlbi.ttl = 4 - ((ilog2(granule) - 3) / (tg - 3)); 2301 else if ((num_pages & CMDQ_TLBI_RANGE_NUM_MAX) == 1) 2302 num_pages++; 2303 } 2304 2305 arm_smmu_cmdq_batch_init(smmu, &cmds, cmd); 2306 2307 while (iova < end) { 2308 if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) { 2309 /* 2310 * On each iteration of the loop, the range is 5 bits 2311 * worth of the aligned size remaining. 2312 * The range in pages is: 2313 * 2314 * range = (num_pages & (0x1f << __ffs(num_pages))) 2315 */ 2316 unsigned long scale, num; 2317 2318 /* Determine the power of 2 multiple number of pages */ 2319 scale = __ffs(num_pages); 2320 cmd->tlbi.scale = scale; 2321 2322 /* Determine how many chunks of 2^scale size we have */ 2323 num = (num_pages >> scale) & CMDQ_TLBI_RANGE_NUM_MAX; 2324 cmd->tlbi.num = num - 1; 2325 2326 /* range is num * 2^scale * pgsize */ 2327 inv_range = num << (scale + tg); 2328 2329 /* Clear out the lower order bits for the next iteration */ 2330 num_pages -= num << scale; 2331 } 2332 2333 cmd->tlbi.addr = iova; 2334 arm_smmu_cmdq_batch_add(smmu, &cmds, cmd); 2335 iova += inv_range; 2336 } 2337 arm_smmu_cmdq_batch_submit(smmu, &cmds); 2338 } 2339 2340 static void arm_smmu_tlb_inv_range_domain(unsigned long iova, size_t size, 2341 size_t granule, bool leaf, 2342 struct arm_smmu_domain *smmu_domain) 2343 { 2344 struct arm_smmu_cmdq_ent cmd = { 2345 .tlbi = { 2346 .leaf = leaf, 2347 }, 2348 }; 2349 2350 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 2351 cmd.opcode = smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ? 2352 CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA; 2353 cmd.tlbi.asid = smmu_domain->cd.asid; 2354 } else { 2355 cmd.opcode = CMDQ_OP_TLBI_S2_IPA; 2356 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; 2357 } 2358 __arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain); 2359 2360 if (smmu_domain->nest_parent) { 2361 /* 2362 * When the S2 domain changes all the nested S1 ASIDs have to be 2363 * flushed too. 2364 */ 2365 cmd.opcode = CMDQ_OP_TLBI_NH_ALL; 2366 arm_smmu_cmdq_issue_cmd_with_sync(smmu_domain->smmu, &cmd); 2367 } 2368 2369 /* 2370 * Unfortunately, this can't be leaf-only since we may have 2371 * zapped an entire table. 2372 */ 2373 arm_smmu_atc_inv_domain(smmu_domain, iova, size); 2374 } 2375 2376 void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, 2377 size_t granule, bool leaf, 2378 struct arm_smmu_domain *smmu_domain) 2379 { 2380 struct arm_smmu_cmdq_ent cmd = { 2381 .opcode = smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ? 2382 CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA, 2383 .tlbi = { 2384 .asid = asid, 2385 .leaf = leaf, 2386 }, 2387 }; 2388 2389 __arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain); 2390 } 2391 2392 static void arm_smmu_tlb_inv_page_nosync(struct iommu_iotlb_gather *gather, 2393 unsigned long iova, size_t granule, 2394 void *cookie) 2395 { 2396 struct arm_smmu_domain *smmu_domain = cookie; 2397 struct iommu_domain *domain = &smmu_domain->domain; 2398 2399 iommu_iotlb_gather_add_page(domain, gather, iova, granule); 2400 } 2401 2402 static void arm_smmu_tlb_inv_walk(unsigned long iova, size_t size, 2403 size_t granule, void *cookie) 2404 { 2405 arm_smmu_tlb_inv_range_domain(iova, size, granule, false, cookie); 2406 } 2407 2408 static const struct iommu_flush_ops arm_smmu_flush_ops = { 2409 .tlb_flush_all = arm_smmu_tlb_inv_context, 2410 .tlb_flush_walk = arm_smmu_tlb_inv_walk, 2411 .tlb_add_page = arm_smmu_tlb_inv_page_nosync, 2412 }; 2413 2414 static bool arm_smmu_dbm_capable(struct arm_smmu_device *smmu) 2415 { 2416 u32 features = (ARM_SMMU_FEAT_HD | ARM_SMMU_FEAT_COHERENCY); 2417 2418 return (smmu->features & features) == features; 2419 } 2420 2421 /* IOMMU API */ 2422 static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) 2423 { 2424 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 2425 2426 switch (cap) { 2427 case IOMMU_CAP_CACHE_COHERENCY: 2428 /* Assume that a coherent TCU implies coherent TBUs */ 2429 return master->smmu->features & ARM_SMMU_FEAT_COHERENCY; 2430 case IOMMU_CAP_ENFORCE_CACHE_COHERENCY: 2431 return arm_smmu_master_canwbs(master); 2432 case IOMMU_CAP_NOEXEC: 2433 case IOMMU_CAP_DEFERRED_FLUSH: 2434 return true; 2435 case IOMMU_CAP_DIRTY_TRACKING: 2436 return arm_smmu_dbm_capable(master->smmu); 2437 default: 2438 return false; 2439 } 2440 } 2441 2442 static bool arm_smmu_enforce_cache_coherency(struct iommu_domain *domain) 2443 { 2444 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 2445 struct arm_smmu_master_domain *master_domain; 2446 unsigned long flags; 2447 bool ret = true; 2448 2449 spin_lock_irqsave(&smmu_domain->devices_lock, flags); 2450 list_for_each_entry(master_domain, &smmu_domain->devices, 2451 devices_elm) { 2452 if (!arm_smmu_master_canwbs(master_domain->master)) { 2453 ret = false; 2454 break; 2455 } 2456 } 2457 smmu_domain->enforce_cache_coherency = ret; 2458 spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 2459 return ret; 2460 } 2461 2462 struct arm_smmu_domain *arm_smmu_domain_alloc(void) 2463 { 2464 struct arm_smmu_domain *smmu_domain; 2465 2466 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL); 2467 if (!smmu_domain) 2468 return ERR_PTR(-ENOMEM); 2469 2470 INIT_LIST_HEAD(&smmu_domain->devices); 2471 spin_lock_init(&smmu_domain->devices_lock); 2472 2473 return smmu_domain; 2474 } 2475 2476 static void arm_smmu_domain_free_paging(struct iommu_domain *domain) 2477 { 2478 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 2479 struct arm_smmu_device *smmu = smmu_domain->smmu; 2480 2481 free_io_pgtable_ops(smmu_domain->pgtbl_ops); 2482 2483 /* Free the ASID or VMID */ 2484 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 2485 /* Prevent SVA from touching the CD while we're freeing it */ 2486 mutex_lock(&arm_smmu_asid_lock); 2487 xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); 2488 mutex_unlock(&arm_smmu_asid_lock); 2489 } else { 2490 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; 2491 if (cfg->vmid) 2492 ida_free(&smmu->vmid_map, cfg->vmid); 2493 } 2494 2495 kfree(smmu_domain); 2496 } 2497 2498 static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, 2499 struct arm_smmu_domain *smmu_domain) 2500 { 2501 int ret; 2502 u32 asid = 0; 2503 struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; 2504 2505 /* Prevent SVA from modifying the ASID until it is written to the CD */ 2506 mutex_lock(&arm_smmu_asid_lock); 2507 ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, 2508 XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); 2509 cd->asid = (u16)asid; 2510 mutex_unlock(&arm_smmu_asid_lock); 2511 return ret; 2512 } 2513 2514 static int arm_smmu_domain_finalise_s2(struct arm_smmu_device *smmu, 2515 struct arm_smmu_domain *smmu_domain) 2516 { 2517 int vmid; 2518 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; 2519 2520 /* Reserve VMID 0 for stage-2 bypass STEs */ 2521 vmid = ida_alloc_range(&smmu->vmid_map, 1, (1 << smmu->vmid_bits) - 1, 2522 GFP_KERNEL); 2523 if (vmid < 0) 2524 return vmid; 2525 2526 cfg->vmid = (u16)vmid; 2527 return 0; 2528 } 2529 2530 static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, 2531 struct arm_smmu_device *smmu, u32 flags) 2532 { 2533 int ret; 2534 enum io_pgtable_fmt fmt; 2535 struct io_pgtable_cfg pgtbl_cfg; 2536 struct io_pgtable_ops *pgtbl_ops; 2537 int (*finalise_stage_fn)(struct arm_smmu_device *smmu, 2538 struct arm_smmu_domain *smmu_domain); 2539 bool enable_dirty = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING; 2540 2541 pgtbl_cfg = (struct io_pgtable_cfg) { 2542 .pgsize_bitmap = smmu->pgsize_bitmap, 2543 .coherent_walk = smmu->features & ARM_SMMU_FEAT_COHERENCY, 2544 .tlb = &arm_smmu_flush_ops, 2545 .iommu_dev = smmu->dev, 2546 }; 2547 2548 switch (smmu_domain->stage) { 2549 case ARM_SMMU_DOMAIN_S1: { 2550 unsigned long ias = (smmu->features & 2551 ARM_SMMU_FEAT_VAX) ? 52 : 48; 2552 2553 pgtbl_cfg.ias = min_t(unsigned long, ias, VA_BITS); 2554 pgtbl_cfg.oas = smmu->ias; 2555 if (enable_dirty) 2556 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_ARM_HD; 2557 fmt = ARM_64_LPAE_S1; 2558 finalise_stage_fn = arm_smmu_domain_finalise_s1; 2559 break; 2560 } 2561 case ARM_SMMU_DOMAIN_S2: 2562 if (enable_dirty) 2563 return -EOPNOTSUPP; 2564 pgtbl_cfg.ias = smmu->ias; 2565 pgtbl_cfg.oas = smmu->oas; 2566 fmt = ARM_64_LPAE_S2; 2567 finalise_stage_fn = arm_smmu_domain_finalise_s2; 2568 if ((smmu->features & ARM_SMMU_FEAT_S2FWB) && 2569 (flags & IOMMU_HWPT_ALLOC_NEST_PARENT)) 2570 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_ARM_S2FWB; 2571 break; 2572 default: 2573 return -EINVAL; 2574 } 2575 2576 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain); 2577 if (!pgtbl_ops) 2578 return -ENOMEM; 2579 2580 smmu_domain->domain.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap; 2581 smmu_domain->domain.geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1; 2582 smmu_domain->domain.geometry.force_aperture = true; 2583 if (enable_dirty && smmu_domain->stage == ARM_SMMU_DOMAIN_S1) 2584 smmu_domain->domain.dirty_ops = &arm_smmu_dirty_ops; 2585 2586 ret = finalise_stage_fn(smmu, smmu_domain); 2587 if (ret < 0) { 2588 free_io_pgtable_ops(pgtbl_ops); 2589 return ret; 2590 } 2591 2592 smmu_domain->pgtbl_ops = pgtbl_ops; 2593 smmu_domain->smmu = smmu; 2594 return 0; 2595 } 2596 2597 static struct arm_smmu_ste * 2598 arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid) 2599 { 2600 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 2601 2602 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { 2603 /* Two-level walk */ 2604 return &cfg->l2.l2ptrs[arm_smmu_strtab_l1_idx(sid)] 2605 ->stes[arm_smmu_strtab_l2_idx(sid)]; 2606 } else { 2607 /* Simple linear lookup */ 2608 return &cfg->linear.table[sid]; 2609 } 2610 } 2611 2612 void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master, 2613 const struct arm_smmu_ste *target) 2614 { 2615 int i, j; 2616 struct arm_smmu_device *smmu = master->smmu; 2617 2618 master->cd_table.in_ste = 2619 FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(target->data[0])) == 2620 STRTAB_STE_0_CFG_S1_TRANS; 2621 master->ste_ats_enabled = 2622 FIELD_GET(STRTAB_STE_1_EATS, le64_to_cpu(target->data[1])) == 2623 STRTAB_STE_1_EATS_TRANS; 2624 2625 for (i = 0; i < master->num_streams; ++i) { 2626 u32 sid = master->streams[i].id; 2627 struct arm_smmu_ste *step = 2628 arm_smmu_get_step_for_sid(smmu, sid); 2629 2630 /* Bridged PCI devices may end up with duplicated IDs */ 2631 for (j = 0; j < i; j++) 2632 if (master->streams[j].id == sid) 2633 break; 2634 if (j < i) 2635 continue; 2636 2637 arm_smmu_write_ste(master, sid, step, target); 2638 } 2639 } 2640 2641 static bool arm_smmu_ats_supported(struct arm_smmu_master *master) 2642 { 2643 struct device *dev = master->dev; 2644 struct arm_smmu_device *smmu = master->smmu; 2645 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 2646 2647 if (!(smmu->features & ARM_SMMU_FEAT_ATS)) 2648 return false; 2649 2650 if (!(fwspec->flags & IOMMU_FWSPEC_PCI_RC_ATS)) 2651 return false; 2652 2653 return dev_is_pci(dev) && pci_ats_supported(to_pci_dev(dev)); 2654 } 2655 2656 static void arm_smmu_enable_ats(struct arm_smmu_master *master) 2657 { 2658 size_t stu; 2659 struct pci_dev *pdev; 2660 struct arm_smmu_device *smmu = master->smmu; 2661 2662 /* Smallest Translation Unit: log2 of the smallest supported granule */ 2663 stu = __ffs(smmu->pgsize_bitmap); 2664 pdev = to_pci_dev(master->dev); 2665 2666 /* 2667 * ATC invalidation of PASID 0 causes the entire ATC to be flushed. 2668 */ 2669 arm_smmu_atc_inv_master(master, IOMMU_NO_PASID); 2670 if (pci_enable_ats(pdev, stu)) 2671 dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu); 2672 } 2673 2674 static int arm_smmu_enable_pasid(struct arm_smmu_master *master) 2675 { 2676 int ret; 2677 int features; 2678 int num_pasids; 2679 struct pci_dev *pdev; 2680 2681 if (!dev_is_pci(master->dev)) 2682 return -ENODEV; 2683 2684 pdev = to_pci_dev(master->dev); 2685 2686 features = pci_pasid_features(pdev); 2687 if (features < 0) 2688 return features; 2689 2690 num_pasids = pci_max_pasids(pdev); 2691 if (num_pasids <= 0) 2692 return num_pasids; 2693 2694 ret = pci_enable_pasid(pdev, features); 2695 if (ret) { 2696 dev_err(&pdev->dev, "Failed to enable PASID\n"); 2697 return ret; 2698 } 2699 2700 master->ssid_bits = min_t(u8, ilog2(num_pasids), 2701 master->smmu->ssid_bits); 2702 return 0; 2703 } 2704 2705 static void arm_smmu_disable_pasid(struct arm_smmu_master *master) 2706 { 2707 struct pci_dev *pdev; 2708 2709 if (!dev_is_pci(master->dev)) 2710 return; 2711 2712 pdev = to_pci_dev(master->dev); 2713 2714 if (!pdev->pasid_enabled) 2715 return; 2716 2717 master->ssid_bits = 0; 2718 pci_disable_pasid(pdev); 2719 } 2720 2721 static struct arm_smmu_master_domain * 2722 arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, 2723 struct iommu_domain *domain, 2724 struct arm_smmu_master *master, 2725 ioasid_t ssid, bool nested_ats_flush) 2726 { 2727 struct arm_smmu_master_domain *master_domain; 2728 2729 lockdep_assert_held(&smmu_domain->devices_lock); 2730 2731 list_for_each_entry(master_domain, &smmu_domain->devices, 2732 devices_elm) { 2733 if (master_domain->master == master && 2734 master_domain->domain == domain && 2735 master_domain->ssid == ssid && 2736 master_domain->nested_ats_flush == nested_ats_flush) 2737 return master_domain; 2738 } 2739 return NULL; 2740 } 2741 2742 /* 2743 * If the domain uses the smmu_domain->devices list return the arm_smmu_domain 2744 * structure, otherwise NULL. These domains track attached devices so they can 2745 * issue invalidations. 2746 */ 2747 static struct arm_smmu_domain * 2748 to_smmu_domain_devices(struct iommu_domain *domain) 2749 { 2750 /* The domain can be NULL only when processing the first attach */ 2751 if (!domain) 2752 return NULL; 2753 if ((domain->type & __IOMMU_DOMAIN_PAGING) || 2754 domain->type == IOMMU_DOMAIN_SVA) 2755 return to_smmu_domain(domain); 2756 if (domain->type == IOMMU_DOMAIN_NESTED) 2757 return to_smmu_nested_domain(domain)->vsmmu->s2_parent; 2758 return NULL; 2759 } 2760 2761 static int arm_smmu_enable_iopf(struct arm_smmu_master *master, 2762 struct arm_smmu_master_domain *master_domain) 2763 { 2764 int ret; 2765 2766 iommu_group_mutex_assert(master->dev); 2767 2768 if (!IS_ENABLED(CONFIG_ARM_SMMU_V3_SVA)) 2769 return -EOPNOTSUPP; 2770 2771 /* 2772 * Drivers for devices supporting PRI or stall require iopf others have 2773 * device-specific fault handlers and don't need IOPF, so this is not a 2774 * failure. 2775 */ 2776 if (!master->stall_enabled) 2777 return 0; 2778 2779 /* We're not keeping track of SIDs in fault events */ 2780 if (master->num_streams != 1) 2781 return -EOPNOTSUPP; 2782 2783 if (master->iopf_refcount) { 2784 master->iopf_refcount++; 2785 master_domain->using_iopf = true; 2786 return 0; 2787 } 2788 2789 ret = iopf_queue_add_device(master->smmu->evtq.iopf, master->dev); 2790 if (ret) 2791 return ret; 2792 master->iopf_refcount = 1; 2793 master_domain->using_iopf = true; 2794 return 0; 2795 } 2796 2797 static void arm_smmu_disable_iopf(struct arm_smmu_master *master, 2798 struct arm_smmu_master_domain *master_domain) 2799 { 2800 iommu_group_mutex_assert(master->dev); 2801 2802 if (!IS_ENABLED(CONFIG_ARM_SMMU_V3_SVA)) 2803 return; 2804 2805 if (!master_domain || !master_domain->using_iopf) 2806 return; 2807 2808 master->iopf_refcount--; 2809 if (master->iopf_refcount == 0) 2810 iopf_queue_remove_device(master->smmu->evtq.iopf, master->dev); 2811 } 2812 2813 static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, 2814 struct iommu_domain *domain, 2815 ioasid_t ssid) 2816 { 2817 struct arm_smmu_domain *smmu_domain = to_smmu_domain_devices(domain); 2818 struct arm_smmu_master_domain *master_domain; 2819 bool nested_ats_flush = false; 2820 unsigned long flags; 2821 2822 if (!smmu_domain) 2823 return; 2824 2825 if (domain->type == IOMMU_DOMAIN_NESTED) 2826 nested_ats_flush = to_smmu_nested_domain(domain)->enable_ats; 2827 2828 spin_lock_irqsave(&smmu_domain->devices_lock, flags); 2829 master_domain = arm_smmu_find_master_domain(smmu_domain, domain, master, 2830 ssid, nested_ats_flush); 2831 if (master_domain) { 2832 list_del(&master_domain->devices_elm); 2833 if (master->ats_enabled) 2834 atomic_dec(&smmu_domain->nr_ats_masters); 2835 } 2836 spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 2837 2838 arm_smmu_disable_iopf(master, master_domain); 2839 kfree(master_domain); 2840 } 2841 2842 /* 2843 * Start the sequence to attach a domain to a master. The sequence contains three 2844 * steps: 2845 * arm_smmu_attach_prepare() 2846 * arm_smmu_install_ste_for_dev() 2847 * arm_smmu_attach_commit() 2848 * 2849 * If prepare succeeds then the sequence must be completed. The STE installed 2850 * must set the STE.EATS field according to state.ats_enabled. 2851 * 2852 * If the device supports ATS then this determines if EATS should be enabled 2853 * in the STE, and starts sequencing EATS disable if required. 2854 * 2855 * The change of the EATS in the STE and the PCI ATS config space is managed by 2856 * this sequence to be in the right order so that if PCI ATS is enabled then 2857 * STE.ETAS is enabled. 2858 * 2859 * new_domain can be a non-paging domain. In this case ATS will not be enabled, 2860 * and invalidations won't be tracked. 2861 */ 2862 int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, 2863 struct iommu_domain *new_domain) 2864 { 2865 struct arm_smmu_master *master = state->master; 2866 struct arm_smmu_master_domain *master_domain; 2867 struct arm_smmu_domain *smmu_domain = 2868 to_smmu_domain_devices(new_domain); 2869 unsigned long flags; 2870 int ret; 2871 2872 /* 2873 * arm_smmu_share_asid() must not see two domains pointing to the same 2874 * arm_smmu_master_domain contents otherwise it could randomly write one 2875 * or the other to the CD. 2876 */ 2877 lockdep_assert_held(&arm_smmu_asid_lock); 2878 2879 if (smmu_domain || state->cd_needs_ats) { 2880 /* 2881 * The SMMU does not support enabling ATS with bypass/abort. 2882 * When the STE is in bypass (STE.Config[2:0] == 0b100), ATS 2883 * Translation Requests and Translated transactions are denied 2884 * as though ATS is disabled for the stream (STE.EATS == 0b00), 2885 * causing F_BAD_ATS_TREQ and F_TRANSL_FORBIDDEN events 2886 * (IHI0070Ea 5.2 Stream Table Entry). 2887 * 2888 * However, if we have installed a CD table and are using S1DSS 2889 * then ATS will work in S1DSS bypass. See "13.6.4 Full ATS 2890 * skipping stage 1". 2891 * 2892 * Disable ATS if we are going to create a normal 0b100 bypass 2893 * STE. 2894 */ 2895 state->ats_enabled = !state->disable_ats && 2896 arm_smmu_ats_supported(master); 2897 } 2898 2899 if (smmu_domain) { 2900 if (new_domain->type == IOMMU_DOMAIN_NESTED) { 2901 ret = arm_smmu_attach_prepare_vmaster( 2902 state, to_smmu_nested_domain(new_domain)); 2903 if (ret) 2904 return ret; 2905 } 2906 2907 master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); 2908 if (!master_domain) { 2909 kfree(state->vmaster); 2910 return -ENOMEM; 2911 } 2912 master_domain->domain = new_domain; 2913 master_domain->master = master; 2914 master_domain->ssid = state->ssid; 2915 if (new_domain->type == IOMMU_DOMAIN_NESTED) 2916 master_domain->nested_ats_flush = 2917 to_smmu_nested_domain(new_domain)->enable_ats; 2918 2919 if (new_domain->iopf_handler) { 2920 ret = arm_smmu_enable_iopf(master, master_domain); 2921 if (ret) 2922 goto err_free_master_domain; 2923 } 2924 2925 /* 2926 * During prepare we want the current smmu_domain and new 2927 * smmu_domain to be in the devices list before we change any 2928 * HW. This ensures that both domains will send ATS 2929 * invalidations to the master until we are done. 2930 * 2931 * It is tempting to make this list only track masters that are 2932 * using ATS, but arm_smmu_share_asid() also uses this to change 2933 * the ASID of a domain, unrelated to ATS. 2934 * 2935 * Notice if we are re-attaching the same domain then the list 2936 * will have two identical entries and commit will remove only 2937 * one of them. 2938 */ 2939 spin_lock_irqsave(&smmu_domain->devices_lock, flags); 2940 if (smmu_domain->enforce_cache_coherency && 2941 !arm_smmu_master_canwbs(master)) { 2942 spin_unlock_irqrestore(&smmu_domain->devices_lock, 2943 flags); 2944 kfree(state->vmaster); 2945 ret = -EINVAL; 2946 goto err_iopf; 2947 } 2948 2949 if (state->ats_enabled) 2950 atomic_inc(&smmu_domain->nr_ats_masters); 2951 list_add(&master_domain->devices_elm, &smmu_domain->devices); 2952 spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 2953 } 2954 2955 if (!state->ats_enabled && master->ats_enabled) { 2956 pci_disable_ats(to_pci_dev(master->dev)); 2957 /* 2958 * This is probably overkill, but the config write for disabling 2959 * ATS should complete before the STE is configured to generate 2960 * UR to avoid AER noise. 2961 */ 2962 wmb(); 2963 } 2964 return 0; 2965 2966 err_iopf: 2967 arm_smmu_disable_iopf(master, master_domain); 2968 err_free_master_domain: 2969 kfree(master_domain); 2970 return ret; 2971 } 2972 2973 /* 2974 * Commit is done after the STE/CD are configured with the EATS setting. It 2975 * completes synchronizing the PCI device's ATC and finishes manipulating the 2976 * smmu_domain->devices list. 2977 */ 2978 void arm_smmu_attach_commit(struct arm_smmu_attach_state *state) 2979 { 2980 struct arm_smmu_master *master = state->master; 2981 2982 lockdep_assert_held(&arm_smmu_asid_lock); 2983 2984 arm_smmu_attach_commit_vmaster(state); 2985 2986 if (state->ats_enabled && !master->ats_enabled) { 2987 arm_smmu_enable_ats(master); 2988 } else if (state->ats_enabled && master->ats_enabled) { 2989 /* 2990 * The translation has changed, flush the ATC. At this point the 2991 * SMMU is translating for the new domain and both the old&new 2992 * domain will issue invalidations. 2993 */ 2994 arm_smmu_atc_inv_master(master, state->ssid); 2995 } else if (!state->ats_enabled && master->ats_enabled) { 2996 /* ATS is being switched off, invalidate the entire ATC */ 2997 arm_smmu_atc_inv_master(master, IOMMU_NO_PASID); 2998 } 2999 master->ats_enabled = state->ats_enabled; 3000 3001 arm_smmu_remove_master_domain(master, state->old_domain, state->ssid); 3002 } 3003 3004 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) 3005 { 3006 int ret = 0; 3007 struct arm_smmu_ste target; 3008 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 3009 struct arm_smmu_device *smmu; 3010 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 3011 struct arm_smmu_attach_state state = { 3012 .old_domain = iommu_get_domain_for_dev(dev), 3013 .ssid = IOMMU_NO_PASID, 3014 }; 3015 struct arm_smmu_master *master; 3016 struct arm_smmu_cd *cdptr; 3017 3018 if (!fwspec) 3019 return -ENOENT; 3020 3021 state.master = master = dev_iommu_priv_get(dev); 3022 smmu = master->smmu; 3023 3024 if (smmu_domain->smmu != smmu) 3025 return -EINVAL; 3026 3027 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 3028 cdptr = arm_smmu_alloc_cd_ptr(master, IOMMU_NO_PASID); 3029 if (!cdptr) 3030 return -ENOMEM; 3031 } else if (arm_smmu_ssids_in_use(&master->cd_table)) 3032 return -EBUSY; 3033 3034 /* 3035 * Prevent arm_smmu_share_asid() from trying to change the ASID 3036 * of either the old or new domain while we are working on it. 3037 * This allows the STE and the smmu_domain->devices list to 3038 * be inconsistent during this routine. 3039 */ 3040 mutex_lock(&arm_smmu_asid_lock); 3041 3042 ret = arm_smmu_attach_prepare(&state, domain); 3043 if (ret) { 3044 mutex_unlock(&arm_smmu_asid_lock); 3045 return ret; 3046 } 3047 3048 switch (smmu_domain->stage) { 3049 case ARM_SMMU_DOMAIN_S1: { 3050 struct arm_smmu_cd target_cd; 3051 3052 arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); 3053 arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, 3054 &target_cd); 3055 arm_smmu_make_cdtable_ste(&target, master, state.ats_enabled, 3056 STRTAB_STE_1_S1DSS_SSID0); 3057 arm_smmu_install_ste_for_dev(master, &target); 3058 break; 3059 } 3060 case ARM_SMMU_DOMAIN_S2: 3061 arm_smmu_make_s2_domain_ste(&target, master, smmu_domain, 3062 state.ats_enabled); 3063 arm_smmu_install_ste_for_dev(master, &target); 3064 arm_smmu_clear_cd(master, IOMMU_NO_PASID); 3065 break; 3066 } 3067 3068 arm_smmu_attach_commit(&state); 3069 mutex_unlock(&arm_smmu_asid_lock); 3070 return 0; 3071 } 3072 3073 static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain, 3074 struct device *dev, ioasid_t id, 3075 struct iommu_domain *old) 3076 { 3077 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 3078 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 3079 struct arm_smmu_device *smmu = master->smmu; 3080 struct arm_smmu_cd target_cd; 3081 3082 if (smmu_domain->smmu != smmu) 3083 return -EINVAL; 3084 3085 if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) 3086 return -EINVAL; 3087 3088 /* 3089 * We can read cd.asid outside the lock because arm_smmu_set_pasid() 3090 * will fix it 3091 */ 3092 arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); 3093 return arm_smmu_set_pasid(master, to_smmu_domain(domain), id, 3094 &target_cd, old); 3095 } 3096 3097 static void arm_smmu_update_ste(struct arm_smmu_master *master, 3098 struct iommu_domain *sid_domain, 3099 bool ats_enabled) 3100 { 3101 unsigned int s1dss = STRTAB_STE_1_S1DSS_TERMINATE; 3102 struct arm_smmu_ste ste; 3103 3104 if (master->cd_table.in_ste && master->ste_ats_enabled == ats_enabled) 3105 return; 3106 3107 if (sid_domain->type == IOMMU_DOMAIN_IDENTITY) 3108 s1dss = STRTAB_STE_1_S1DSS_BYPASS; 3109 else 3110 WARN_ON(sid_domain->type != IOMMU_DOMAIN_BLOCKED); 3111 3112 /* 3113 * Change the STE into a cdtable one with SID IDENTITY/BLOCKED behavior 3114 * using s1dss if necessary. If the cd_table is already installed then 3115 * the S1DSS is correct and this will just update the EATS. Otherwise it 3116 * installs the entire thing. This will be hitless. 3117 */ 3118 arm_smmu_make_cdtable_ste(&ste, master, ats_enabled, s1dss); 3119 arm_smmu_install_ste_for_dev(master, &ste); 3120 } 3121 3122 int arm_smmu_set_pasid(struct arm_smmu_master *master, 3123 struct arm_smmu_domain *smmu_domain, ioasid_t pasid, 3124 struct arm_smmu_cd *cd, struct iommu_domain *old) 3125 { 3126 struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev); 3127 struct arm_smmu_attach_state state = { 3128 .master = master, 3129 .ssid = pasid, 3130 .old_domain = old, 3131 }; 3132 struct arm_smmu_cd *cdptr; 3133 int ret; 3134 3135 /* The core code validates pasid */ 3136 3137 if (smmu_domain->smmu != master->smmu) 3138 return -EINVAL; 3139 3140 if (!master->cd_table.in_ste && 3141 sid_domain->type != IOMMU_DOMAIN_IDENTITY && 3142 sid_domain->type != IOMMU_DOMAIN_BLOCKED) 3143 return -EINVAL; 3144 3145 cdptr = arm_smmu_alloc_cd_ptr(master, pasid); 3146 if (!cdptr) 3147 return -ENOMEM; 3148 3149 mutex_lock(&arm_smmu_asid_lock); 3150 ret = arm_smmu_attach_prepare(&state, &smmu_domain->domain); 3151 if (ret) 3152 goto out_unlock; 3153 3154 /* 3155 * We don't want to obtain to the asid_lock too early, so fix up the 3156 * caller set ASID under the lock in case it changed. 3157 */ 3158 cd->data[0] &= ~cpu_to_le64(CTXDESC_CD_0_ASID); 3159 cd->data[0] |= cpu_to_le64( 3160 FIELD_PREP(CTXDESC_CD_0_ASID, smmu_domain->cd.asid)); 3161 3162 arm_smmu_write_cd_entry(master, pasid, cdptr, cd); 3163 arm_smmu_update_ste(master, sid_domain, state.ats_enabled); 3164 3165 arm_smmu_attach_commit(&state); 3166 3167 out_unlock: 3168 mutex_unlock(&arm_smmu_asid_lock); 3169 return ret; 3170 } 3171 3172 static int arm_smmu_blocking_set_dev_pasid(struct iommu_domain *new_domain, 3173 struct device *dev, ioasid_t pasid, 3174 struct iommu_domain *old_domain) 3175 { 3176 struct arm_smmu_domain *smmu_domain = to_smmu_domain(old_domain); 3177 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 3178 3179 mutex_lock(&arm_smmu_asid_lock); 3180 arm_smmu_clear_cd(master, pasid); 3181 if (master->ats_enabled) 3182 arm_smmu_atc_inv_master(master, pasid); 3183 arm_smmu_remove_master_domain(master, &smmu_domain->domain, pasid); 3184 mutex_unlock(&arm_smmu_asid_lock); 3185 3186 /* 3187 * When the last user of the CD table goes away downgrade the STE back 3188 * to a non-cd_table one. 3189 */ 3190 if (!arm_smmu_ssids_in_use(&master->cd_table)) { 3191 struct iommu_domain *sid_domain = 3192 iommu_get_domain_for_dev(master->dev); 3193 3194 if (sid_domain->type == IOMMU_DOMAIN_IDENTITY || 3195 sid_domain->type == IOMMU_DOMAIN_BLOCKED) 3196 sid_domain->ops->attach_dev(sid_domain, dev); 3197 } 3198 return 0; 3199 } 3200 3201 static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, 3202 struct device *dev, 3203 struct arm_smmu_ste *ste, 3204 unsigned int s1dss) 3205 { 3206 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 3207 struct arm_smmu_attach_state state = { 3208 .master = master, 3209 .old_domain = iommu_get_domain_for_dev(dev), 3210 .ssid = IOMMU_NO_PASID, 3211 }; 3212 3213 /* 3214 * Do not allow any ASID to be changed while are working on the STE, 3215 * otherwise we could miss invalidations. 3216 */ 3217 mutex_lock(&arm_smmu_asid_lock); 3218 3219 /* 3220 * If the CD table is not in use we can use the provided STE, otherwise 3221 * we use a cdtable STE with the provided S1DSS. 3222 */ 3223 if (arm_smmu_ssids_in_use(&master->cd_table)) { 3224 /* 3225 * If a CD table has to be present then we need to run with ATS 3226 * on because we have to assume a PASID is using ATS. For 3227 * IDENTITY this will setup things so that S1DSS=bypass which 3228 * follows the explanation in "13.6.4 Full ATS skipping stage 1" 3229 * and allows for ATS on the RID to work. 3230 */ 3231 state.cd_needs_ats = true; 3232 arm_smmu_attach_prepare(&state, domain); 3233 arm_smmu_make_cdtable_ste(ste, master, state.ats_enabled, s1dss); 3234 } else { 3235 arm_smmu_attach_prepare(&state, domain); 3236 } 3237 arm_smmu_install_ste_for_dev(master, ste); 3238 arm_smmu_attach_commit(&state); 3239 mutex_unlock(&arm_smmu_asid_lock); 3240 3241 /* 3242 * This has to be done after removing the master from the 3243 * arm_smmu_domain->devices to avoid races updating the same context 3244 * descriptor from arm_smmu_share_asid(). 3245 */ 3246 arm_smmu_clear_cd(master, IOMMU_NO_PASID); 3247 } 3248 3249 static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, 3250 struct device *dev) 3251 { 3252 struct arm_smmu_ste ste; 3253 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 3254 3255 arm_smmu_master_clear_vmaster(master); 3256 arm_smmu_make_bypass_ste(master->smmu, &ste); 3257 arm_smmu_attach_dev_ste(domain, dev, &ste, STRTAB_STE_1_S1DSS_BYPASS); 3258 return 0; 3259 } 3260 3261 static const struct iommu_domain_ops arm_smmu_identity_ops = { 3262 .attach_dev = arm_smmu_attach_dev_identity, 3263 }; 3264 3265 static struct iommu_domain arm_smmu_identity_domain = { 3266 .type = IOMMU_DOMAIN_IDENTITY, 3267 .ops = &arm_smmu_identity_ops, 3268 }; 3269 3270 static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, 3271 struct device *dev) 3272 { 3273 struct arm_smmu_ste ste; 3274 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 3275 3276 arm_smmu_master_clear_vmaster(master); 3277 arm_smmu_make_abort_ste(&ste); 3278 arm_smmu_attach_dev_ste(domain, dev, &ste, 3279 STRTAB_STE_1_S1DSS_TERMINATE); 3280 return 0; 3281 } 3282 3283 static const struct iommu_domain_ops arm_smmu_blocked_ops = { 3284 .attach_dev = arm_smmu_attach_dev_blocked, 3285 .set_dev_pasid = arm_smmu_blocking_set_dev_pasid, 3286 }; 3287 3288 static struct iommu_domain arm_smmu_blocked_domain = { 3289 .type = IOMMU_DOMAIN_BLOCKED, 3290 .ops = &arm_smmu_blocked_ops, 3291 }; 3292 3293 static struct iommu_domain * 3294 arm_smmu_domain_alloc_paging_flags(struct device *dev, u32 flags, 3295 const struct iommu_user_data *user_data) 3296 { 3297 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 3298 struct arm_smmu_device *smmu = master->smmu; 3299 const u32 PAGING_FLAGS = IOMMU_HWPT_ALLOC_DIRTY_TRACKING | 3300 IOMMU_HWPT_ALLOC_PASID | 3301 IOMMU_HWPT_ALLOC_NEST_PARENT; 3302 struct arm_smmu_domain *smmu_domain; 3303 int ret; 3304 3305 if (flags & ~PAGING_FLAGS) 3306 return ERR_PTR(-EOPNOTSUPP); 3307 if (user_data) 3308 return ERR_PTR(-EOPNOTSUPP); 3309 3310 smmu_domain = arm_smmu_domain_alloc(); 3311 if (IS_ERR(smmu_domain)) 3312 return ERR_CAST(smmu_domain); 3313 3314 switch (flags) { 3315 case 0: 3316 /* Prefer S1 if available */ 3317 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) 3318 smmu_domain->stage = ARM_SMMU_DOMAIN_S1; 3319 else 3320 smmu_domain->stage = ARM_SMMU_DOMAIN_S2; 3321 break; 3322 case IOMMU_HWPT_ALLOC_NEST_PARENT: 3323 if (!(smmu->features & ARM_SMMU_FEAT_NESTING)) { 3324 ret = -EOPNOTSUPP; 3325 goto err_free; 3326 } 3327 smmu_domain->stage = ARM_SMMU_DOMAIN_S2; 3328 smmu_domain->nest_parent = true; 3329 break; 3330 case IOMMU_HWPT_ALLOC_DIRTY_TRACKING: 3331 case IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_PASID: 3332 case IOMMU_HWPT_ALLOC_PASID: 3333 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1)) { 3334 ret = -EOPNOTSUPP; 3335 goto err_free; 3336 } 3337 smmu_domain->stage = ARM_SMMU_DOMAIN_S1; 3338 break; 3339 default: 3340 ret = -EOPNOTSUPP; 3341 goto err_free; 3342 } 3343 3344 smmu_domain->domain.type = IOMMU_DOMAIN_UNMANAGED; 3345 smmu_domain->domain.ops = arm_smmu_ops.default_domain_ops; 3346 ret = arm_smmu_domain_finalise(smmu_domain, smmu, flags); 3347 if (ret) 3348 goto err_free; 3349 return &smmu_domain->domain; 3350 3351 err_free: 3352 kfree(smmu_domain); 3353 return ERR_PTR(ret); 3354 } 3355 3356 static int arm_smmu_map_pages(struct iommu_domain *domain, unsigned long iova, 3357 phys_addr_t paddr, size_t pgsize, size_t pgcount, 3358 int prot, gfp_t gfp, size_t *mapped) 3359 { 3360 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops; 3361 3362 if (!ops) 3363 return -ENODEV; 3364 3365 return ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp, mapped); 3366 } 3367 3368 static size_t arm_smmu_unmap_pages(struct iommu_domain *domain, unsigned long iova, 3369 size_t pgsize, size_t pgcount, 3370 struct iommu_iotlb_gather *gather) 3371 { 3372 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 3373 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops; 3374 3375 if (!ops) 3376 return 0; 3377 3378 return ops->unmap_pages(ops, iova, pgsize, pgcount, gather); 3379 } 3380 3381 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain) 3382 { 3383 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 3384 3385 if (smmu_domain->smmu) 3386 arm_smmu_tlb_inv_context(smmu_domain); 3387 } 3388 3389 static void arm_smmu_iotlb_sync(struct iommu_domain *domain, 3390 struct iommu_iotlb_gather *gather) 3391 { 3392 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 3393 3394 if (!gather->pgsize) 3395 return; 3396 3397 arm_smmu_tlb_inv_range_domain(gather->start, 3398 gather->end - gather->start + 1, 3399 gather->pgsize, true, smmu_domain); 3400 } 3401 3402 static phys_addr_t 3403 arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) 3404 { 3405 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops; 3406 3407 if (!ops) 3408 return 0; 3409 3410 return ops->iova_to_phys(ops, iova); 3411 } 3412 3413 static struct platform_driver arm_smmu_driver; 3414 3415 static 3416 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode) 3417 { 3418 struct device *dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode); 3419 3420 put_device(dev); 3421 return dev ? dev_get_drvdata(dev) : NULL; 3422 } 3423 3424 static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) 3425 { 3426 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) 3427 return arm_smmu_strtab_l1_idx(sid) < smmu->strtab_cfg.l2.num_l1_ents; 3428 return sid < smmu->strtab_cfg.linear.num_ents; 3429 } 3430 3431 static int arm_smmu_init_sid_strtab(struct arm_smmu_device *smmu, u32 sid) 3432 { 3433 /* Check the SIDs are in range of the SMMU and our stream table */ 3434 if (!arm_smmu_sid_in_range(smmu, sid)) 3435 return -ERANGE; 3436 3437 /* Ensure l2 strtab is initialised */ 3438 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) 3439 return arm_smmu_init_l2_strtab(smmu, sid); 3440 3441 return 0; 3442 } 3443 3444 static int arm_smmu_insert_master(struct arm_smmu_device *smmu, 3445 struct arm_smmu_master *master) 3446 { 3447 int i; 3448 int ret = 0; 3449 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); 3450 3451 master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams), 3452 GFP_KERNEL); 3453 if (!master->streams) 3454 return -ENOMEM; 3455 master->num_streams = fwspec->num_ids; 3456 3457 mutex_lock(&smmu->streams_mutex); 3458 for (i = 0; i < fwspec->num_ids; i++) { 3459 struct arm_smmu_stream *new_stream = &master->streams[i]; 3460 struct rb_node *existing; 3461 u32 sid = fwspec->ids[i]; 3462 3463 new_stream->id = sid; 3464 new_stream->master = master; 3465 3466 ret = arm_smmu_init_sid_strtab(smmu, sid); 3467 if (ret) 3468 break; 3469 3470 /* Insert into SID tree */ 3471 existing = rb_find_add(&new_stream->node, &smmu->streams, 3472 arm_smmu_streams_cmp_node); 3473 if (existing) { 3474 struct arm_smmu_master *existing_master = 3475 rb_entry(existing, struct arm_smmu_stream, node) 3476 ->master; 3477 3478 /* Bridged PCI devices may end up with duplicated IDs */ 3479 if (existing_master == master) 3480 continue; 3481 3482 dev_warn(master->dev, 3483 "Aliasing StreamID 0x%x (from %s) unsupported, expect DMA to be broken\n", 3484 sid, dev_name(existing_master->dev)); 3485 ret = -ENODEV; 3486 break; 3487 } 3488 } 3489 3490 if (ret) { 3491 for (i--; i >= 0; i--) 3492 rb_erase(&master->streams[i].node, &smmu->streams); 3493 kfree(master->streams); 3494 } 3495 mutex_unlock(&smmu->streams_mutex); 3496 3497 return ret; 3498 } 3499 3500 static void arm_smmu_remove_master(struct arm_smmu_master *master) 3501 { 3502 int i; 3503 struct arm_smmu_device *smmu = master->smmu; 3504 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); 3505 3506 if (!smmu || !master->streams) 3507 return; 3508 3509 mutex_lock(&smmu->streams_mutex); 3510 for (i = 0; i < fwspec->num_ids; i++) 3511 rb_erase(&master->streams[i].node, &smmu->streams); 3512 mutex_unlock(&smmu->streams_mutex); 3513 3514 kfree(master->streams); 3515 } 3516 3517 static struct iommu_device *arm_smmu_probe_device(struct device *dev) 3518 { 3519 int ret; 3520 struct arm_smmu_device *smmu; 3521 struct arm_smmu_master *master; 3522 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 3523 3524 if (WARN_ON_ONCE(dev_iommu_priv_get(dev))) 3525 return ERR_PTR(-EBUSY); 3526 3527 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); 3528 if (!smmu) 3529 return ERR_PTR(-ENODEV); 3530 3531 master = kzalloc(sizeof(*master), GFP_KERNEL); 3532 if (!master) 3533 return ERR_PTR(-ENOMEM); 3534 3535 master->dev = dev; 3536 master->smmu = smmu; 3537 dev_iommu_priv_set(dev, master); 3538 3539 ret = arm_smmu_insert_master(smmu, master); 3540 if (ret) 3541 goto err_free_master; 3542 3543 device_property_read_u32(dev, "pasid-num-bits", &master->ssid_bits); 3544 master->ssid_bits = min(smmu->ssid_bits, master->ssid_bits); 3545 3546 /* 3547 * Note that PASID must be enabled before, and disabled after ATS: 3548 * PCI Express Base 4.0r1.0 - 10.5.1.3 ATS Control Register 3549 * 3550 * Behavior is undefined if this bit is Set and the value of the PASID 3551 * Enable, Execute Requested Enable, or Privileged Mode Requested bits 3552 * are changed. 3553 */ 3554 arm_smmu_enable_pasid(master); 3555 3556 if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB)) 3557 master->ssid_bits = min_t(u8, master->ssid_bits, 3558 CTXDESC_LINEAR_CDMAX); 3559 3560 if ((smmu->features & ARM_SMMU_FEAT_STALLS && 3561 device_property_read_bool(dev, "dma-can-stall")) || 3562 smmu->features & ARM_SMMU_FEAT_STALL_FORCE) 3563 master->stall_enabled = true; 3564 3565 if (dev_is_pci(dev)) { 3566 unsigned int stu = __ffs(smmu->pgsize_bitmap); 3567 3568 pci_prepare_ats(to_pci_dev(dev), stu); 3569 } 3570 3571 return &smmu->iommu; 3572 3573 err_free_master: 3574 kfree(master); 3575 return ERR_PTR(ret); 3576 } 3577 3578 static void arm_smmu_release_device(struct device *dev) 3579 { 3580 struct arm_smmu_master *master = dev_iommu_priv_get(dev); 3581 3582 WARN_ON(master->iopf_refcount); 3583 3584 /* Put the STE back to what arm_smmu_init_strtab() sets */ 3585 if (dev->iommu->require_direct) 3586 arm_smmu_attach_dev_identity(&arm_smmu_identity_domain, dev); 3587 else 3588 arm_smmu_attach_dev_blocked(&arm_smmu_blocked_domain, dev); 3589 3590 arm_smmu_disable_pasid(master); 3591 arm_smmu_remove_master(master); 3592 if (arm_smmu_cdtab_allocated(&master->cd_table)) 3593 arm_smmu_free_cd_tables(master); 3594 kfree(master); 3595 } 3596 3597 static int arm_smmu_read_and_clear_dirty(struct iommu_domain *domain, 3598 unsigned long iova, size_t size, 3599 unsigned long flags, 3600 struct iommu_dirty_bitmap *dirty) 3601 { 3602 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 3603 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops; 3604 3605 return ops->read_and_clear_dirty(ops, iova, size, flags, dirty); 3606 } 3607 3608 static int arm_smmu_set_dirty_tracking(struct iommu_domain *domain, 3609 bool enabled) 3610 { 3611 /* 3612 * Always enabled and the dirty bitmap is cleared prior to 3613 * set_dirty_tracking(). 3614 */ 3615 return 0; 3616 } 3617 3618 static struct iommu_group *arm_smmu_device_group(struct device *dev) 3619 { 3620 struct iommu_group *group; 3621 3622 /* 3623 * We don't support devices sharing stream IDs other than PCI RID 3624 * aliases, since the necessary ID-to-device lookup becomes rather 3625 * impractical given a potential sparse 32-bit stream ID space. 3626 */ 3627 if (dev_is_pci(dev)) 3628 group = pci_device_group(dev); 3629 else 3630 group = generic_device_group(dev); 3631 3632 return group; 3633 } 3634 3635 static int arm_smmu_of_xlate(struct device *dev, 3636 const struct of_phandle_args *args) 3637 { 3638 return iommu_fwspec_add_ids(dev, args->args, 1); 3639 } 3640 3641 static void arm_smmu_get_resv_regions(struct device *dev, 3642 struct list_head *head) 3643 { 3644 struct iommu_resv_region *region; 3645 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 3646 3647 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH, 3648 prot, IOMMU_RESV_SW_MSI, GFP_KERNEL); 3649 if (!region) 3650 return; 3651 3652 list_add_tail(®ion->list, head); 3653 3654 iommu_dma_get_resv_regions(dev, head); 3655 } 3656 3657 /* 3658 * HiSilicon PCIe tune and trace device can be used to trace TLP headers on the 3659 * PCIe link and save the data to memory by DMA. The hardware is restricted to 3660 * use identity mapping only. 3661 */ 3662 #define IS_HISI_PTT_DEVICE(pdev) ((pdev)->vendor == PCI_VENDOR_ID_HUAWEI && \ 3663 (pdev)->device == 0xa12e) 3664 3665 static int arm_smmu_def_domain_type(struct device *dev) 3666 { 3667 if (dev_is_pci(dev)) { 3668 struct pci_dev *pdev = to_pci_dev(dev); 3669 3670 if (IS_HISI_PTT_DEVICE(pdev)) 3671 return IOMMU_DOMAIN_IDENTITY; 3672 } 3673 3674 return 0; 3675 } 3676 3677 static struct iommu_ops arm_smmu_ops = { 3678 .identity_domain = &arm_smmu_identity_domain, 3679 .blocked_domain = &arm_smmu_blocked_domain, 3680 .capable = arm_smmu_capable, 3681 .hw_info = arm_smmu_hw_info, 3682 .domain_alloc_sva = arm_smmu_sva_domain_alloc, 3683 .domain_alloc_paging_flags = arm_smmu_domain_alloc_paging_flags, 3684 .probe_device = arm_smmu_probe_device, 3685 .release_device = arm_smmu_release_device, 3686 .device_group = arm_smmu_device_group, 3687 .of_xlate = arm_smmu_of_xlate, 3688 .get_resv_regions = arm_smmu_get_resv_regions, 3689 .page_response = arm_smmu_page_response, 3690 .def_domain_type = arm_smmu_def_domain_type, 3691 .viommu_alloc = arm_vsmmu_alloc, 3692 .user_pasid_table = 1, 3693 .pgsize_bitmap = -1UL, /* Restricted during device attach */ 3694 .owner = THIS_MODULE, 3695 .default_domain_ops = &(const struct iommu_domain_ops) { 3696 .attach_dev = arm_smmu_attach_dev, 3697 .enforce_cache_coherency = arm_smmu_enforce_cache_coherency, 3698 .set_dev_pasid = arm_smmu_s1_set_dev_pasid, 3699 .map_pages = arm_smmu_map_pages, 3700 .unmap_pages = arm_smmu_unmap_pages, 3701 .flush_iotlb_all = arm_smmu_flush_iotlb_all, 3702 .iotlb_sync = arm_smmu_iotlb_sync, 3703 .iova_to_phys = arm_smmu_iova_to_phys, 3704 .free = arm_smmu_domain_free_paging, 3705 } 3706 }; 3707 3708 static struct iommu_dirty_ops arm_smmu_dirty_ops = { 3709 .read_and_clear_dirty = arm_smmu_read_and_clear_dirty, 3710 .set_dirty_tracking = arm_smmu_set_dirty_tracking, 3711 }; 3712 3713 /* Probing and initialisation functions */ 3714 int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, 3715 struct arm_smmu_queue *q, void __iomem *page, 3716 unsigned long prod_off, unsigned long cons_off, 3717 size_t dwords, const char *name) 3718 { 3719 size_t qsz; 3720 3721 do { 3722 qsz = ((1 << q->llq.max_n_shift) * dwords) << 3; 3723 q->base = dmam_alloc_coherent(smmu->dev, qsz, &q->base_dma, 3724 GFP_KERNEL); 3725 if (q->base || qsz < PAGE_SIZE) 3726 break; 3727 3728 q->llq.max_n_shift--; 3729 } while (1); 3730 3731 if (!q->base) { 3732 dev_err(smmu->dev, 3733 "failed to allocate queue (0x%zx bytes) for %s\n", 3734 qsz, name); 3735 return -ENOMEM; 3736 } 3737 3738 if (!WARN_ON(q->base_dma & (qsz - 1))) { 3739 dev_info(smmu->dev, "allocated %u entries for %s\n", 3740 1 << q->llq.max_n_shift, name); 3741 } 3742 3743 q->prod_reg = page + prod_off; 3744 q->cons_reg = page + cons_off; 3745 q->ent_dwords = dwords; 3746 3747 q->q_base = Q_BASE_RWA; 3748 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK; 3749 q->q_base |= FIELD_PREP(Q_BASE_LOG2SIZE, q->llq.max_n_shift); 3750 3751 q->llq.prod = q->llq.cons = 0; 3752 return 0; 3753 } 3754 3755 int arm_smmu_cmdq_init(struct arm_smmu_device *smmu, 3756 struct arm_smmu_cmdq *cmdq) 3757 { 3758 unsigned int nents = 1 << cmdq->q.llq.max_n_shift; 3759 3760 atomic_set(&cmdq->owner_prod, 0); 3761 atomic_set(&cmdq->lock, 0); 3762 3763 cmdq->valid_map = (atomic_long_t *)devm_bitmap_zalloc(smmu->dev, nents, 3764 GFP_KERNEL); 3765 if (!cmdq->valid_map) 3766 return -ENOMEM; 3767 3768 return 0; 3769 } 3770 3771 static int arm_smmu_init_queues(struct arm_smmu_device *smmu) 3772 { 3773 int ret; 3774 3775 /* cmdq */ 3776 ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, smmu->base, 3777 ARM_SMMU_CMDQ_PROD, ARM_SMMU_CMDQ_CONS, 3778 CMDQ_ENT_DWORDS, "cmdq"); 3779 if (ret) 3780 return ret; 3781 3782 ret = arm_smmu_cmdq_init(smmu, &smmu->cmdq); 3783 if (ret) 3784 return ret; 3785 3786 /* evtq */ 3787 ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, smmu->page1, 3788 ARM_SMMU_EVTQ_PROD, ARM_SMMU_EVTQ_CONS, 3789 EVTQ_ENT_DWORDS, "evtq"); 3790 if (ret) 3791 return ret; 3792 3793 if ((smmu->features & ARM_SMMU_FEAT_SVA) && 3794 (smmu->features & ARM_SMMU_FEAT_STALLS)) { 3795 smmu->evtq.iopf = iopf_queue_alloc(dev_name(smmu->dev)); 3796 if (!smmu->evtq.iopf) 3797 return -ENOMEM; 3798 } 3799 3800 /* priq */ 3801 if (!(smmu->features & ARM_SMMU_FEAT_PRI)) 3802 return 0; 3803 3804 return arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1, 3805 ARM_SMMU_PRIQ_PROD, ARM_SMMU_PRIQ_CONS, 3806 PRIQ_ENT_DWORDS, "priq"); 3807 } 3808 3809 static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu) 3810 { 3811 u32 l1size; 3812 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 3813 unsigned int last_sid_idx = 3814 arm_smmu_strtab_l1_idx((1ULL << smmu->sid_bits) - 1); 3815 3816 /* Calculate the L1 size, capped to the SIDSIZE. */ 3817 cfg->l2.num_l1_ents = min(last_sid_idx + 1, STRTAB_MAX_L1_ENTRIES); 3818 if (cfg->l2.num_l1_ents <= last_sid_idx) 3819 dev_warn(smmu->dev, 3820 "2-level strtab only covers %u/%u bits of SID\n", 3821 ilog2(cfg->l2.num_l1_ents * STRTAB_NUM_L2_STES), 3822 smmu->sid_bits); 3823 3824 l1size = cfg->l2.num_l1_ents * sizeof(struct arm_smmu_strtab_l1); 3825 cfg->l2.l1tab = dmam_alloc_coherent(smmu->dev, l1size, &cfg->l2.l1_dma, 3826 GFP_KERNEL); 3827 if (!cfg->l2.l1tab) { 3828 dev_err(smmu->dev, 3829 "failed to allocate l1 stream table (%u bytes)\n", 3830 l1size); 3831 return -ENOMEM; 3832 } 3833 3834 cfg->l2.l2ptrs = devm_kcalloc(smmu->dev, cfg->l2.num_l1_ents, 3835 sizeof(*cfg->l2.l2ptrs), GFP_KERNEL); 3836 if (!cfg->l2.l2ptrs) 3837 return -ENOMEM; 3838 3839 return 0; 3840 } 3841 3842 static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) 3843 { 3844 u32 size; 3845 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 3846 3847 size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste); 3848 cfg->linear.table = dmam_alloc_coherent(smmu->dev, size, 3849 &cfg->linear.ste_dma, 3850 GFP_KERNEL); 3851 if (!cfg->linear.table) { 3852 dev_err(smmu->dev, 3853 "failed to allocate linear stream table (%u bytes)\n", 3854 size); 3855 return -ENOMEM; 3856 } 3857 cfg->linear.num_ents = 1 << smmu->sid_bits; 3858 3859 arm_smmu_init_initial_stes(cfg->linear.table, cfg->linear.num_ents); 3860 return 0; 3861 } 3862 3863 static int arm_smmu_init_strtab(struct arm_smmu_device *smmu) 3864 { 3865 int ret; 3866 3867 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) 3868 ret = arm_smmu_init_strtab_2lvl(smmu); 3869 else 3870 ret = arm_smmu_init_strtab_linear(smmu); 3871 if (ret) 3872 return ret; 3873 3874 ida_init(&smmu->vmid_map); 3875 3876 return 0; 3877 } 3878 3879 static int arm_smmu_init_structures(struct arm_smmu_device *smmu) 3880 { 3881 int ret; 3882 3883 mutex_init(&smmu->streams_mutex); 3884 smmu->streams = RB_ROOT; 3885 3886 ret = arm_smmu_init_queues(smmu); 3887 if (ret) 3888 return ret; 3889 3890 ret = arm_smmu_init_strtab(smmu); 3891 if (ret) 3892 return ret; 3893 3894 if (smmu->impl_ops && smmu->impl_ops->init_structures) 3895 return smmu->impl_ops->init_structures(smmu); 3896 3897 return 0; 3898 } 3899 3900 static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val, 3901 unsigned int reg_off, unsigned int ack_off) 3902 { 3903 u32 reg; 3904 3905 writel_relaxed(val, smmu->base + reg_off); 3906 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val, 3907 1, ARM_SMMU_POLL_TIMEOUT_US); 3908 } 3909 3910 /* GBPA is "special" */ 3911 static int arm_smmu_update_gbpa(struct arm_smmu_device *smmu, u32 set, u32 clr) 3912 { 3913 int ret; 3914 u32 reg, __iomem *gbpa = smmu->base + ARM_SMMU_GBPA; 3915 3916 ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE), 3917 1, ARM_SMMU_POLL_TIMEOUT_US); 3918 if (ret) 3919 return ret; 3920 3921 reg &= ~clr; 3922 reg |= set; 3923 writel_relaxed(reg | GBPA_UPDATE, gbpa); 3924 ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE), 3925 1, ARM_SMMU_POLL_TIMEOUT_US); 3926 3927 if (ret) 3928 dev_err(smmu->dev, "GBPA not responding to update\n"); 3929 return ret; 3930 } 3931 3932 static void arm_smmu_free_msis(void *data) 3933 { 3934 struct device *dev = data; 3935 3936 platform_device_msi_free_irqs_all(dev); 3937 } 3938 3939 static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) 3940 { 3941 phys_addr_t doorbell; 3942 struct device *dev = msi_desc_to_dev(desc); 3943 struct arm_smmu_device *smmu = dev_get_drvdata(dev); 3944 phys_addr_t *cfg = arm_smmu_msi_cfg[desc->msi_index]; 3945 3946 doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo; 3947 doorbell &= MSI_CFG0_ADDR_MASK; 3948 3949 writeq_relaxed(doorbell, smmu->base + cfg[0]); 3950 writel_relaxed(msg->data, smmu->base + cfg[1]); 3951 writel_relaxed(ARM_SMMU_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]); 3952 } 3953 3954 static void arm_smmu_setup_msis(struct arm_smmu_device *smmu) 3955 { 3956 int ret, nvec = ARM_SMMU_MAX_MSIS; 3957 struct device *dev = smmu->dev; 3958 3959 /* Clear the MSI address regs */ 3960 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0); 3961 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0); 3962 3963 if (smmu->features & ARM_SMMU_FEAT_PRI) 3964 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0); 3965 else 3966 nvec--; 3967 3968 if (!(smmu->features & ARM_SMMU_FEAT_MSI)) 3969 return; 3970 3971 if (!dev->msi.domain) { 3972 dev_info(smmu->dev, "msi_domain absent - falling back to wired irqs\n"); 3973 return; 3974 } 3975 3976 /* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */ 3977 ret = platform_device_msi_init_and_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg); 3978 if (ret) { 3979 dev_warn(dev, "failed to allocate MSIs - falling back to wired irqs\n"); 3980 return; 3981 } 3982 3983 smmu->evtq.q.irq = msi_get_virq(dev, EVTQ_MSI_INDEX); 3984 smmu->gerr_irq = msi_get_virq(dev, GERROR_MSI_INDEX); 3985 smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX); 3986 3987 /* Add callback to free MSIs on teardown */ 3988 devm_add_action_or_reset(dev, arm_smmu_free_msis, dev); 3989 } 3990 3991 static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) 3992 { 3993 int irq, ret; 3994 3995 arm_smmu_setup_msis(smmu); 3996 3997 /* Request interrupt lines */ 3998 irq = smmu->evtq.q.irq; 3999 if (irq) { 4000 ret = devm_request_threaded_irq(smmu->dev, irq, NULL, 4001 arm_smmu_evtq_thread, 4002 IRQF_ONESHOT, 4003 "arm-smmu-v3-evtq", smmu); 4004 if (ret < 0) 4005 dev_warn(smmu->dev, "failed to enable evtq irq\n"); 4006 } else { 4007 dev_warn(smmu->dev, "no evtq irq - events will not be reported!\n"); 4008 } 4009 4010 irq = smmu->gerr_irq; 4011 if (irq) { 4012 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler, 4013 0, "arm-smmu-v3-gerror", smmu); 4014 if (ret < 0) 4015 dev_warn(smmu->dev, "failed to enable gerror irq\n"); 4016 } else { 4017 dev_warn(smmu->dev, "no gerr irq - errors will not be reported!\n"); 4018 } 4019 4020 if (smmu->features & ARM_SMMU_FEAT_PRI) { 4021 irq = smmu->priq.q.irq; 4022 if (irq) { 4023 ret = devm_request_threaded_irq(smmu->dev, irq, NULL, 4024 arm_smmu_priq_thread, 4025 IRQF_ONESHOT, 4026 "arm-smmu-v3-priq", 4027 smmu); 4028 if (ret < 0) 4029 dev_warn(smmu->dev, 4030 "failed to enable priq irq\n"); 4031 } else { 4032 dev_warn(smmu->dev, "no priq irq - PRI will be broken\n"); 4033 } 4034 } 4035 } 4036 4037 static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu) 4038 { 4039 int ret, irq; 4040 u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN; 4041 4042 /* Disable IRQs first */ 4043 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL, 4044 ARM_SMMU_IRQ_CTRLACK); 4045 if (ret) { 4046 dev_err(smmu->dev, "failed to disable irqs\n"); 4047 return ret; 4048 } 4049 4050 irq = smmu->combined_irq; 4051 if (irq) { 4052 /* 4053 * Cavium ThunderX2 implementation doesn't support unique irq 4054 * lines. Use a single irq line for all the SMMUv3 interrupts. 4055 */ 4056 ret = devm_request_threaded_irq(smmu->dev, irq, 4057 arm_smmu_combined_irq_handler, 4058 arm_smmu_combined_irq_thread, 4059 IRQF_ONESHOT, 4060 "arm-smmu-v3-combined-irq", smmu); 4061 if (ret < 0) 4062 dev_warn(smmu->dev, "failed to enable combined irq\n"); 4063 } else 4064 arm_smmu_setup_unique_irqs(smmu); 4065 4066 if (smmu->features & ARM_SMMU_FEAT_PRI) 4067 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN; 4068 4069 /* Enable interrupt generation on the SMMU */ 4070 ret = arm_smmu_write_reg_sync(smmu, irqen_flags, 4071 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK); 4072 if (ret) 4073 dev_warn(smmu->dev, "failed to enable irqs\n"); 4074 4075 return 0; 4076 } 4077 4078 static int arm_smmu_device_disable(struct arm_smmu_device *smmu) 4079 { 4080 int ret; 4081 4082 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK); 4083 if (ret) 4084 dev_err(smmu->dev, "failed to clear cr0\n"); 4085 4086 return ret; 4087 } 4088 4089 static void arm_smmu_write_strtab(struct arm_smmu_device *smmu) 4090 { 4091 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 4092 dma_addr_t dma; 4093 u32 reg; 4094 4095 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { 4096 reg = FIELD_PREP(STRTAB_BASE_CFG_FMT, 4097 STRTAB_BASE_CFG_FMT_2LVL) | 4098 FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, 4099 ilog2(cfg->l2.num_l1_ents) + STRTAB_SPLIT) | 4100 FIELD_PREP(STRTAB_BASE_CFG_SPLIT, STRTAB_SPLIT); 4101 dma = cfg->l2.l1_dma; 4102 } else { 4103 reg = FIELD_PREP(STRTAB_BASE_CFG_FMT, 4104 STRTAB_BASE_CFG_FMT_LINEAR) | 4105 FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, smmu->sid_bits); 4106 dma = cfg->linear.ste_dma; 4107 } 4108 writeq_relaxed((dma & STRTAB_BASE_ADDR_MASK) | STRTAB_BASE_RA, 4109 smmu->base + ARM_SMMU_STRTAB_BASE); 4110 writel_relaxed(reg, smmu->base + ARM_SMMU_STRTAB_BASE_CFG); 4111 } 4112 4113 static int arm_smmu_device_reset(struct arm_smmu_device *smmu) 4114 { 4115 int ret; 4116 u32 reg, enables; 4117 struct arm_smmu_cmdq_ent cmd; 4118 4119 /* Clear CR0 and sync (disables SMMU and queue processing) */ 4120 reg = readl_relaxed(smmu->base + ARM_SMMU_CR0); 4121 if (reg & CR0_SMMUEN) { 4122 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n"); 4123 arm_smmu_update_gbpa(smmu, GBPA_ABORT, 0); 4124 } 4125 4126 ret = arm_smmu_device_disable(smmu); 4127 if (ret) 4128 return ret; 4129 4130 /* CR1 (table and queue memory attributes) */ 4131 reg = FIELD_PREP(CR1_TABLE_SH, ARM_SMMU_SH_ISH) | 4132 FIELD_PREP(CR1_TABLE_OC, CR1_CACHE_WB) | 4133 FIELD_PREP(CR1_TABLE_IC, CR1_CACHE_WB) | 4134 FIELD_PREP(CR1_QUEUE_SH, ARM_SMMU_SH_ISH) | 4135 FIELD_PREP(CR1_QUEUE_OC, CR1_CACHE_WB) | 4136 FIELD_PREP(CR1_QUEUE_IC, CR1_CACHE_WB); 4137 writel_relaxed(reg, smmu->base + ARM_SMMU_CR1); 4138 4139 /* CR2 (random crap) */ 4140 reg = CR2_PTM | CR2_RECINVSID; 4141 4142 if (smmu->features & ARM_SMMU_FEAT_E2H) 4143 reg |= CR2_E2H; 4144 4145 writel_relaxed(reg, smmu->base + ARM_SMMU_CR2); 4146 4147 /* Stream table */ 4148 arm_smmu_write_strtab(smmu); 4149 4150 /* Command queue */ 4151 writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE); 4152 writel_relaxed(smmu->cmdq.q.llq.prod, smmu->base + ARM_SMMU_CMDQ_PROD); 4153 writel_relaxed(smmu->cmdq.q.llq.cons, smmu->base + ARM_SMMU_CMDQ_CONS); 4154 4155 enables = CR0_CMDQEN; 4156 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0, 4157 ARM_SMMU_CR0ACK); 4158 if (ret) { 4159 dev_err(smmu->dev, "failed to enable command queue\n"); 4160 return ret; 4161 } 4162 4163 /* Invalidate any cached configuration */ 4164 cmd.opcode = CMDQ_OP_CFGI_ALL; 4165 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); 4166 4167 /* Invalidate any stale TLB entries */ 4168 if (smmu->features & ARM_SMMU_FEAT_HYP) { 4169 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL; 4170 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); 4171 } 4172 4173 cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL; 4174 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); 4175 4176 /* Event queue */ 4177 writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE); 4178 writel_relaxed(smmu->evtq.q.llq.prod, smmu->page1 + ARM_SMMU_EVTQ_PROD); 4179 writel_relaxed(smmu->evtq.q.llq.cons, smmu->page1 + ARM_SMMU_EVTQ_CONS); 4180 4181 enables |= CR0_EVTQEN; 4182 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0, 4183 ARM_SMMU_CR0ACK); 4184 if (ret) { 4185 dev_err(smmu->dev, "failed to enable event queue\n"); 4186 return ret; 4187 } 4188 4189 /* PRI queue */ 4190 if (smmu->features & ARM_SMMU_FEAT_PRI) { 4191 writeq_relaxed(smmu->priq.q.q_base, 4192 smmu->base + ARM_SMMU_PRIQ_BASE); 4193 writel_relaxed(smmu->priq.q.llq.prod, 4194 smmu->page1 + ARM_SMMU_PRIQ_PROD); 4195 writel_relaxed(smmu->priq.q.llq.cons, 4196 smmu->page1 + ARM_SMMU_PRIQ_CONS); 4197 4198 enables |= CR0_PRIQEN; 4199 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0, 4200 ARM_SMMU_CR0ACK); 4201 if (ret) { 4202 dev_err(smmu->dev, "failed to enable PRI queue\n"); 4203 return ret; 4204 } 4205 } 4206 4207 if (smmu->features & ARM_SMMU_FEAT_ATS) { 4208 enables |= CR0_ATSCHK; 4209 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0, 4210 ARM_SMMU_CR0ACK); 4211 if (ret) { 4212 dev_err(smmu->dev, "failed to enable ATS check\n"); 4213 return ret; 4214 } 4215 } 4216 4217 ret = arm_smmu_setup_irqs(smmu); 4218 if (ret) { 4219 dev_err(smmu->dev, "failed to setup irqs\n"); 4220 return ret; 4221 } 4222 4223 if (is_kdump_kernel()) 4224 enables &= ~(CR0_EVTQEN | CR0_PRIQEN); 4225 4226 /* Enable the SMMU interface */ 4227 enables |= CR0_SMMUEN; 4228 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0, 4229 ARM_SMMU_CR0ACK); 4230 if (ret) { 4231 dev_err(smmu->dev, "failed to enable SMMU interface\n"); 4232 return ret; 4233 } 4234 4235 if (smmu->impl_ops && smmu->impl_ops->device_reset) { 4236 ret = smmu->impl_ops->device_reset(smmu); 4237 if (ret) { 4238 dev_err(smmu->dev, "failed to reset impl\n"); 4239 return ret; 4240 } 4241 } 4242 4243 return 0; 4244 } 4245 4246 #define IIDR_IMPLEMENTER_ARM 0x43b 4247 #define IIDR_PRODUCTID_ARM_MMU_600 0x483 4248 #define IIDR_PRODUCTID_ARM_MMU_700 0x487 4249 4250 static void arm_smmu_device_iidr_probe(struct arm_smmu_device *smmu) 4251 { 4252 u32 reg; 4253 unsigned int implementer, productid, variant, revision; 4254 4255 reg = readl_relaxed(smmu->base + ARM_SMMU_IIDR); 4256 implementer = FIELD_GET(IIDR_IMPLEMENTER, reg); 4257 productid = FIELD_GET(IIDR_PRODUCTID, reg); 4258 variant = FIELD_GET(IIDR_VARIANT, reg); 4259 revision = FIELD_GET(IIDR_REVISION, reg); 4260 4261 switch (implementer) { 4262 case IIDR_IMPLEMENTER_ARM: 4263 switch (productid) { 4264 case IIDR_PRODUCTID_ARM_MMU_600: 4265 /* Arm erratum 1076982 */ 4266 if (variant == 0 && revision <= 2) 4267 smmu->features &= ~ARM_SMMU_FEAT_SEV; 4268 /* Arm erratum 1209401 */ 4269 if (variant < 2) 4270 smmu->features &= ~ARM_SMMU_FEAT_NESTING; 4271 break; 4272 case IIDR_PRODUCTID_ARM_MMU_700: 4273 /* Arm erratum 2812531 */ 4274 smmu->features &= ~ARM_SMMU_FEAT_BTM; 4275 smmu->options |= ARM_SMMU_OPT_CMDQ_FORCE_SYNC; 4276 /* Arm errata 2268618, 2812531 */ 4277 smmu->features &= ~ARM_SMMU_FEAT_NESTING; 4278 break; 4279 } 4280 break; 4281 } 4282 } 4283 4284 static void arm_smmu_get_httu(struct arm_smmu_device *smmu, u32 reg) 4285 { 4286 u32 fw_features = smmu->features & (ARM_SMMU_FEAT_HA | ARM_SMMU_FEAT_HD); 4287 u32 hw_features = 0; 4288 4289 switch (FIELD_GET(IDR0_HTTU, reg)) { 4290 case IDR0_HTTU_ACCESS_DIRTY: 4291 hw_features |= ARM_SMMU_FEAT_HD; 4292 fallthrough; 4293 case IDR0_HTTU_ACCESS: 4294 hw_features |= ARM_SMMU_FEAT_HA; 4295 } 4296 4297 if (smmu->dev->of_node) 4298 smmu->features |= hw_features; 4299 else if (hw_features != fw_features) 4300 /* ACPI IORT sets the HTTU bits */ 4301 dev_warn(smmu->dev, 4302 "IDR0.HTTU features(0x%x) overridden by FW configuration (0x%x)\n", 4303 hw_features, fw_features); 4304 } 4305 4306 static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) 4307 { 4308 u32 reg; 4309 bool coherent = smmu->features & ARM_SMMU_FEAT_COHERENCY; 4310 4311 /* IDR0 */ 4312 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0); 4313 4314 /* 2-level structures */ 4315 if (FIELD_GET(IDR0_ST_LVL, reg) == IDR0_ST_LVL_2LVL) 4316 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB; 4317 4318 if (reg & IDR0_CD2L) 4319 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB; 4320 4321 /* 4322 * Translation table endianness. 4323 * We currently require the same endianness as the CPU, but this 4324 * could be changed later by adding a new IO_PGTABLE_QUIRK. 4325 */ 4326 switch (FIELD_GET(IDR0_TTENDIAN, reg)) { 4327 case IDR0_TTENDIAN_MIXED: 4328 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE; 4329 break; 4330 #ifdef __BIG_ENDIAN 4331 case IDR0_TTENDIAN_BE: 4332 smmu->features |= ARM_SMMU_FEAT_TT_BE; 4333 break; 4334 #else 4335 case IDR0_TTENDIAN_LE: 4336 smmu->features |= ARM_SMMU_FEAT_TT_LE; 4337 break; 4338 #endif 4339 default: 4340 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n"); 4341 return -ENXIO; 4342 } 4343 4344 /* Boolean feature flags */ 4345 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI) 4346 smmu->features |= ARM_SMMU_FEAT_PRI; 4347 4348 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS) 4349 smmu->features |= ARM_SMMU_FEAT_ATS; 4350 4351 if (reg & IDR0_SEV) 4352 smmu->features |= ARM_SMMU_FEAT_SEV; 4353 4354 if (reg & IDR0_MSI) { 4355 smmu->features |= ARM_SMMU_FEAT_MSI; 4356 if (coherent && !disable_msipolling) 4357 smmu->options |= ARM_SMMU_OPT_MSIPOLL; 4358 } 4359 4360 if (reg & IDR0_HYP) { 4361 smmu->features |= ARM_SMMU_FEAT_HYP; 4362 if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN)) 4363 smmu->features |= ARM_SMMU_FEAT_E2H; 4364 } 4365 4366 arm_smmu_get_httu(smmu, reg); 4367 4368 /* 4369 * The coherency feature as set by FW is used in preference to the ID 4370 * register, but warn on mismatch. 4371 */ 4372 if (!!(reg & IDR0_COHACC) != coherent) 4373 dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n", 4374 str_true_false(coherent)); 4375 4376 switch (FIELD_GET(IDR0_STALL_MODEL, reg)) { 4377 case IDR0_STALL_MODEL_FORCE: 4378 smmu->features |= ARM_SMMU_FEAT_STALL_FORCE; 4379 fallthrough; 4380 case IDR0_STALL_MODEL_STALL: 4381 smmu->features |= ARM_SMMU_FEAT_STALLS; 4382 } 4383 4384 if (reg & IDR0_S1P) 4385 smmu->features |= ARM_SMMU_FEAT_TRANS_S1; 4386 4387 if (reg & IDR0_S2P) 4388 smmu->features |= ARM_SMMU_FEAT_TRANS_S2; 4389 4390 if (!(reg & (IDR0_S1P | IDR0_S2P))) { 4391 dev_err(smmu->dev, "no translation support!\n"); 4392 return -ENXIO; 4393 } 4394 4395 /* We only support the AArch64 table format at present */ 4396 switch (FIELD_GET(IDR0_TTF, reg)) { 4397 case IDR0_TTF_AARCH32_64: 4398 smmu->ias = 40; 4399 fallthrough; 4400 case IDR0_TTF_AARCH64: 4401 break; 4402 default: 4403 dev_err(smmu->dev, "AArch64 table format not supported!\n"); 4404 return -ENXIO; 4405 } 4406 4407 /* ASID/VMID sizes */ 4408 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8; 4409 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8; 4410 4411 /* IDR1 */ 4412 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1); 4413 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) { 4414 dev_err(smmu->dev, "embedded implementation not supported\n"); 4415 return -ENXIO; 4416 } 4417 4418 if (reg & IDR1_ATTR_TYPES_OVR) 4419 smmu->features |= ARM_SMMU_FEAT_ATTR_TYPES_OVR; 4420 4421 /* Queue sizes, capped to ensure natural alignment */ 4422 smmu->cmdq.q.llq.max_n_shift = min_t(u32, CMDQ_MAX_SZ_SHIFT, 4423 FIELD_GET(IDR1_CMDQS, reg)); 4424 if (smmu->cmdq.q.llq.max_n_shift <= ilog2(CMDQ_BATCH_ENTRIES)) { 4425 /* 4426 * We don't support splitting up batches, so one batch of 4427 * commands plus an extra sync needs to fit inside the command 4428 * queue. There's also no way we can handle the weird alignment 4429 * restrictions on the base pointer for a unit-length queue. 4430 */ 4431 dev_err(smmu->dev, "command queue size <= %d entries not supported\n", 4432 CMDQ_BATCH_ENTRIES); 4433 return -ENXIO; 4434 } 4435 4436 smmu->evtq.q.llq.max_n_shift = min_t(u32, EVTQ_MAX_SZ_SHIFT, 4437 FIELD_GET(IDR1_EVTQS, reg)); 4438 smmu->priq.q.llq.max_n_shift = min_t(u32, PRIQ_MAX_SZ_SHIFT, 4439 FIELD_GET(IDR1_PRIQS, reg)); 4440 4441 /* SID/SSID sizes */ 4442 smmu->ssid_bits = FIELD_GET(IDR1_SSIDSIZE, reg); 4443 smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg); 4444 smmu->iommu.max_pasids = 1UL << smmu->ssid_bits; 4445 4446 /* 4447 * If the SMMU supports fewer bits than would fill a single L2 stream 4448 * table, use a linear table instead. 4449 */ 4450 if (smmu->sid_bits <= STRTAB_SPLIT) 4451 smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB; 4452 4453 /* IDR3 */ 4454 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR3); 4455 if (FIELD_GET(IDR3_RIL, reg)) 4456 smmu->features |= ARM_SMMU_FEAT_RANGE_INV; 4457 if (FIELD_GET(IDR3_FWB, reg)) 4458 smmu->features |= ARM_SMMU_FEAT_S2FWB; 4459 4460 /* IDR5 */ 4461 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5); 4462 4463 /* Maximum number of outstanding stalls */ 4464 smmu->evtq.max_stalls = FIELD_GET(IDR5_STALL_MAX, reg); 4465 4466 /* Page sizes */ 4467 if (reg & IDR5_GRAN64K) 4468 smmu->pgsize_bitmap |= SZ_64K | SZ_512M; 4469 if (reg & IDR5_GRAN16K) 4470 smmu->pgsize_bitmap |= SZ_16K | SZ_32M; 4471 if (reg & IDR5_GRAN4K) 4472 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G; 4473 4474 /* Input address size */ 4475 if (FIELD_GET(IDR5_VAX, reg) == IDR5_VAX_52_BIT) 4476 smmu->features |= ARM_SMMU_FEAT_VAX; 4477 4478 /* Output address size */ 4479 switch (FIELD_GET(IDR5_OAS, reg)) { 4480 case IDR5_OAS_32_BIT: 4481 smmu->oas = 32; 4482 break; 4483 case IDR5_OAS_36_BIT: 4484 smmu->oas = 36; 4485 break; 4486 case IDR5_OAS_40_BIT: 4487 smmu->oas = 40; 4488 break; 4489 case IDR5_OAS_42_BIT: 4490 smmu->oas = 42; 4491 break; 4492 case IDR5_OAS_44_BIT: 4493 smmu->oas = 44; 4494 break; 4495 case IDR5_OAS_52_BIT: 4496 smmu->oas = 52; 4497 smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */ 4498 break; 4499 default: 4500 dev_info(smmu->dev, 4501 "unknown output address size. Truncating to 48-bit\n"); 4502 fallthrough; 4503 case IDR5_OAS_48_BIT: 4504 smmu->oas = 48; 4505 } 4506 4507 if (arm_smmu_ops.pgsize_bitmap == -1UL) 4508 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap; 4509 else 4510 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap; 4511 4512 /* Set the DMA mask for our table walker */ 4513 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas))) 4514 dev_warn(smmu->dev, 4515 "failed to set DMA mask for table walker\n"); 4516 4517 smmu->ias = max(smmu->ias, smmu->oas); 4518 4519 if ((smmu->features & ARM_SMMU_FEAT_TRANS_S1) && 4520 (smmu->features & ARM_SMMU_FEAT_TRANS_S2)) 4521 smmu->features |= ARM_SMMU_FEAT_NESTING; 4522 4523 arm_smmu_device_iidr_probe(smmu); 4524 4525 if (arm_smmu_sva_supported(smmu)) 4526 smmu->features |= ARM_SMMU_FEAT_SVA; 4527 4528 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n", 4529 smmu->ias, smmu->oas, smmu->features); 4530 return 0; 4531 } 4532 4533 #ifdef CONFIG_ACPI 4534 #ifdef CONFIG_TEGRA241_CMDQV 4535 static void acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node *node, 4536 struct arm_smmu_device *smmu) 4537 { 4538 const char *uid = kasprintf(GFP_KERNEL, "%u", node->identifier); 4539 struct acpi_device *adev; 4540 4541 /* Look for an NVDA200C node whose _UID matches the SMMU node ID */ 4542 adev = acpi_dev_get_first_match_dev("NVDA200C", uid, -1); 4543 if (adev) { 4544 /* Tegra241 CMDQV driver is responsible for put_device() */ 4545 smmu->impl_dev = &adev->dev; 4546 smmu->options |= ARM_SMMU_OPT_TEGRA241_CMDQV; 4547 dev_info(smmu->dev, "found companion CMDQV device: %s\n", 4548 dev_name(smmu->impl_dev)); 4549 } 4550 kfree(uid); 4551 } 4552 #else 4553 static void acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node *node, 4554 struct arm_smmu_device *smmu) 4555 { 4556 } 4557 #endif 4558 4559 static int acpi_smmu_iort_probe_model(struct acpi_iort_node *node, 4560 struct arm_smmu_device *smmu) 4561 { 4562 struct acpi_iort_smmu_v3 *iort_smmu = 4563 (struct acpi_iort_smmu_v3 *)node->node_data; 4564 4565 switch (iort_smmu->model) { 4566 case ACPI_IORT_SMMU_V3_CAVIUM_CN99XX: 4567 smmu->options |= ARM_SMMU_OPT_PAGE0_REGS_ONLY; 4568 break; 4569 case ACPI_IORT_SMMU_V3_HISILICON_HI161X: 4570 smmu->options |= ARM_SMMU_OPT_SKIP_PREFETCH; 4571 break; 4572 case ACPI_IORT_SMMU_V3_GENERIC: 4573 /* 4574 * Tegra241 implementation stores its SMMU options and impl_dev 4575 * in DSDT. Thus, go through the ACPI tables unconditionally. 4576 */ 4577 acpi_smmu_dsdt_probe_tegra241_cmdqv(node, smmu); 4578 break; 4579 } 4580 4581 dev_notice(smmu->dev, "option mask 0x%x\n", smmu->options); 4582 return 0; 4583 } 4584 4585 static int arm_smmu_device_acpi_probe(struct platform_device *pdev, 4586 struct arm_smmu_device *smmu) 4587 { 4588 struct acpi_iort_smmu_v3 *iort_smmu; 4589 struct device *dev = smmu->dev; 4590 struct acpi_iort_node *node; 4591 4592 node = *(struct acpi_iort_node **)dev_get_platdata(dev); 4593 4594 /* Retrieve SMMUv3 specific data */ 4595 iort_smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 4596 4597 if (iort_smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) 4598 smmu->features |= ARM_SMMU_FEAT_COHERENCY; 4599 4600 switch (FIELD_GET(ACPI_IORT_SMMU_V3_HTTU_OVERRIDE, iort_smmu->flags)) { 4601 case IDR0_HTTU_ACCESS_DIRTY: 4602 smmu->features |= ARM_SMMU_FEAT_HD; 4603 fallthrough; 4604 case IDR0_HTTU_ACCESS: 4605 smmu->features |= ARM_SMMU_FEAT_HA; 4606 } 4607 4608 return acpi_smmu_iort_probe_model(node, smmu); 4609 } 4610 #else 4611 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev, 4612 struct arm_smmu_device *smmu) 4613 { 4614 return -ENODEV; 4615 } 4616 #endif 4617 4618 static int arm_smmu_device_dt_probe(struct platform_device *pdev, 4619 struct arm_smmu_device *smmu) 4620 { 4621 struct device *dev = &pdev->dev; 4622 u32 cells; 4623 int ret = -EINVAL; 4624 4625 if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells)) 4626 dev_err(dev, "missing #iommu-cells property\n"); 4627 else if (cells != 1) 4628 dev_err(dev, "invalid #iommu-cells value (%d)\n", cells); 4629 else 4630 ret = 0; 4631 4632 parse_driver_options(smmu); 4633 4634 if (of_dma_is_coherent(dev->of_node)) 4635 smmu->features |= ARM_SMMU_FEAT_COHERENCY; 4636 4637 return ret; 4638 } 4639 4640 static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) 4641 { 4642 if (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY) 4643 return SZ_64K; 4644 else 4645 return SZ_128K; 4646 } 4647 4648 static void __iomem *arm_smmu_ioremap(struct device *dev, resource_size_t start, 4649 resource_size_t size) 4650 { 4651 struct resource res = DEFINE_RES_MEM(start, size); 4652 4653 return devm_ioremap_resource(dev, &res); 4654 } 4655 4656 static void arm_smmu_rmr_install_bypass_ste(struct arm_smmu_device *smmu) 4657 { 4658 struct list_head rmr_list; 4659 struct iommu_resv_region *e; 4660 4661 INIT_LIST_HEAD(&rmr_list); 4662 iort_get_rmr_sids(dev_fwnode(smmu->dev), &rmr_list); 4663 4664 list_for_each_entry(e, &rmr_list, list) { 4665 struct iommu_iort_rmr_data *rmr; 4666 int ret, i; 4667 4668 rmr = container_of(e, struct iommu_iort_rmr_data, rr); 4669 for (i = 0; i < rmr->num_sids; i++) { 4670 ret = arm_smmu_init_sid_strtab(smmu, rmr->sids[i]); 4671 if (ret) { 4672 dev_err(smmu->dev, "RMR SID(0x%x) bypass failed\n", 4673 rmr->sids[i]); 4674 continue; 4675 } 4676 4677 /* 4678 * STE table is not programmed to HW, see 4679 * arm_smmu_initial_bypass_stes() 4680 */ 4681 arm_smmu_make_bypass_ste(smmu, 4682 arm_smmu_get_step_for_sid(smmu, rmr->sids[i])); 4683 } 4684 } 4685 4686 iort_put_rmr_sids(dev_fwnode(smmu->dev), &rmr_list); 4687 } 4688 4689 static void arm_smmu_impl_remove(void *data) 4690 { 4691 struct arm_smmu_device *smmu = data; 4692 4693 if (smmu->impl_ops && smmu->impl_ops->device_remove) 4694 smmu->impl_ops->device_remove(smmu); 4695 } 4696 4697 /* 4698 * Probe all the compiled in implementations. Each one checks to see if it 4699 * matches this HW and if so returns a devm_krealloc'd arm_smmu_device which 4700 * replaces the callers. Otherwise the original is returned or ERR_PTR. 4701 */ 4702 static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu) 4703 { 4704 struct arm_smmu_device *new_smmu = ERR_PTR(-ENODEV); 4705 int ret; 4706 4707 if (smmu->impl_dev && (smmu->options & ARM_SMMU_OPT_TEGRA241_CMDQV)) 4708 new_smmu = tegra241_cmdqv_probe(smmu); 4709 4710 if (new_smmu == ERR_PTR(-ENODEV)) 4711 return smmu; 4712 if (IS_ERR(new_smmu)) 4713 return new_smmu; 4714 4715 ret = devm_add_action_or_reset(new_smmu->dev, arm_smmu_impl_remove, 4716 new_smmu); 4717 if (ret) 4718 return ERR_PTR(ret); 4719 return new_smmu; 4720 } 4721 4722 static int arm_smmu_device_probe(struct platform_device *pdev) 4723 { 4724 int irq, ret; 4725 struct resource *res; 4726 resource_size_t ioaddr; 4727 struct arm_smmu_device *smmu; 4728 struct device *dev = &pdev->dev; 4729 4730 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL); 4731 if (!smmu) 4732 return -ENOMEM; 4733 smmu->dev = dev; 4734 4735 if (dev->of_node) { 4736 ret = arm_smmu_device_dt_probe(pdev, smmu); 4737 } else { 4738 ret = arm_smmu_device_acpi_probe(pdev, smmu); 4739 } 4740 if (ret) 4741 return ret; 4742 4743 smmu = arm_smmu_impl_probe(smmu); 4744 if (IS_ERR(smmu)) 4745 return PTR_ERR(smmu); 4746 4747 /* Base address */ 4748 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 4749 if (!res) 4750 return -EINVAL; 4751 if (resource_size(res) < arm_smmu_resource_size(smmu)) { 4752 dev_err(dev, "MMIO region too small (%pr)\n", res); 4753 return -EINVAL; 4754 } 4755 ioaddr = res->start; 4756 4757 /* 4758 * Don't map the IMPLEMENTATION DEFINED regions, since they may contain 4759 * the PMCG registers which are reserved by the PMU driver. 4760 */ 4761 smmu->base = arm_smmu_ioremap(dev, ioaddr, ARM_SMMU_REG_SZ); 4762 if (IS_ERR(smmu->base)) 4763 return PTR_ERR(smmu->base); 4764 4765 if (arm_smmu_resource_size(smmu) > SZ_64K) { 4766 smmu->page1 = arm_smmu_ioremap(dev, ioaddr + SZ_64K, 4767 ARM_SMMU_REG_SZ); 4768 if (IS_ERR(smmu->page1)) 4769 return PTR_ERR(smmu->page1); 4770 } else { 4771 smmu->page1 = smmu->base; 4772 } 4773 4774 /* Interrupt lines */ 4775 4776 irq = platform_get_irq_byname_optional(pdev, "combined"); 4777 if (irq > 0) 4778 smmu->combined_irq = irq; 4779 else { 4780 irq = platform_get_irq_byname_optional(pdev, "eventq"); 4781 if (irq > 0) 4782 smmu->evtq.q.irq = irq; 4783 4784 irq = platform_get_irq_byname_optional(pdev, "priq"); 4785 if (irq > 0) 4786 smmu->priq.q.irq = irq; 4787 4788 irq = platform_get_irq_byname_optional(pdev, "gerror"); 4789 if (irq > 0) 4790 smmu->gerr_irq = irq; 4791 } 4792 /* Probe the h/w */ 4793 ret = arm_smmu_device_hw_probe(smmu); 4794 if (ret) 4795 return ret; 4796 4797 /* Initialise in-memory data structures */ 4798 ret = arm_smmu_init_structures(smmu); 4799 if (ret) 4800 goto err_free_iopf; 4801 4802 /* Record our private device structure */ 4803 platform_set_drvdata(pdev, smmu); 4804 4805 /* Check for RMRs and install bypass STEs if any */ 4806 arm_smmu_rmr_install_bypass_ste(smmu); 4807 4808 /* Reset the device */ 4809 ret = arm_smmu_device_reset(smmu); 4810 if (ret) 4811 goto err_disable; 4812 4813 /* And we're up. Go go go! */ 4814 ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, 4815 "smmu3.%pa", &ioaddr); 4816 if (ret) 4817 goto err_disable; 4818 4819 ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); 4820 if (ret) { 4821 dev_err(dev, "Failed to register iommu\n"); 4822 goto err_free_sysfs; 4823 } 4824 4825 return 0; 4826 4827 err_free_sysfs: 4828 iommu_device_sysfs_remove(&smmu->iommu); 4829 err_disable: 4830 arm_smmu_device_disable(smmu); 4831 err_free_iopf: 4832 iopf_queue_free(smmu->evtq.iopf); 4833 return ret; 4834 } 4835 4836 static void arm_smmu_device_remove(struct platform_device *pdev) 4837 { 4838 struct arm_smmu_device *smmu = platform_get_drvdata(pdev); 4839 4840 iommu_device_unregister(&smmu->iommu); 4841 iommu_device_sysfs_remove(&smmu->iommu); 4842 arm_smmu_device_disable(smmu); 4843 iopf_queue_free(smmu->evtq.iopf); 4844 ida_destroy(&smmu->vmid_map); 4845 } 4846 4847 static void arm_smmu_device_shutdown(struct platform_device *pdev) 4848 { 4849 struct arm_smmu_device *smmu = platform_get_drvdata(pdev); 4850 4851 arm_smmu_device_disable(smmu); 4852 } 4853 4854 static const struct of_device_id arm_smmu_of_match[] = { 4855 { .compatible = "arm,smmu-v3", }, 4856 { }, 4857 }; 4858 MODULE_DEVICE_TABLE(of, arm_smmu_of_match); 4859 4860 static void arm_smmu_driver_unregister(struct platform_driver *drv) 4861 { 4862 arm_smmu_sva_notifier_synchronize(); 4863 platform_driver_unregister(drv); 4864 } 4865 4866 static struct platform_driver arm_smmu_driver = { 4867 .driver = { 4868 .name = "arm-smmu-v3", 4869 .of_match_table = arm_smmu_of_match, 4870 .suppress_bind_attrs = true, 4871 }, 4872 .probe = arm_smmu_device_probe, 4873 .remove = arm_smmu_device_remove, 4874 .shutdown = arm_smmu_device_shutdown, 4875 }; 4876 module_driver(arm_smmu_driver, platform_driver_register, 4877 arm_smmu_driver_unregister); 4878 4879 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations"); 4880 MODULE_AUTHOR("Will Deacon <will@kernel.org>"); 4881 MODULE_ALIAS("platform:arm-smmu-v3"); 4882 MODULE_LICENSE("GPL v2"); 4883