| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved. |
| 3 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/acpi.h> |
| 19 | #include <linux/acpi_iort.h> |
| 20 | #include <linux/bitmap.h> |
| 21 | #include <linux/cpu.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/dma-iommu.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/irqdomain.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/list_sort.h> |
| 28 | #include <linux/log2.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/msi.h> |
| 31 | #include <linux/of.h> |
| 32 | #include <linux/of_address.h> |
| 33 | #include <linux/of_irq.h> |
| 34 | #include <linux/of_pci.h> |
| 35 | #include <linux/of_platform.h> |
| 36 | #include <linux/percpu.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/syscore_ops.h> |
| 39 | |
| 40 | #include <linux/irqchip.h> |
| 41 | #include <linux/irqchip/arm-gic-v3.h> |
| 42 | #include <linux/irqchip/arm-gic-v4.h> |
| 43 | |
| 44 | #include <asm/cputype.h> |
| 45 | #include <asm/exception.h> |
| 46 | |
| 47 | #include "irq-gic-common.h" |
| 48 | |
| 49 | #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0) |
| 50 | #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1) |
| 51 | #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2) |
| 52 | #define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3) |
| 53 | |
| 54 | #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0) |
| 55 | |
| 56 | static u32 lpi_id_bits; |
| 57 | |
| 58 | /* |
| 59 | * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to |
| 60 | * deal with (one configuration byte per interrupt). PENDBASE has to |
| 61 | * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI). |
| 62 | */ |
| 63 | #define LPI_NRBITS lpi_id_bits |
| 64 | #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) |
| 65 | #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) |
| 66 | |
| 67 | #define LPI_PROP_DEFAULT_PRIO 0xa0 |
| 68 | |
| 69 | /* |
| 70 | * Collection structure - just an ID, and a redistributor address to |
| 71 | * ping. We use one per CPU as a bag of interrupts assigned to this |
| 72 | * CPU. |
| 73 | */ |
| 74 | struct its_collection { |
| 75 | u64 target_address; |
| 76 | u16 col_id; |
| 77 | }; |
| 78 | |
| 79 | /* |
| 80 | * The ITS_BASER structure - contains memory information, cached |
| 81 | * value of BASER register configuration and ITS page size. |
| 82 | */ |
| 83 | struct its_baser { |
| 84 | void *base; |
| 85 | u64 val; |
| 86 | u32 order; |
| 87 | u32 psz; |
| 88 | }; |
| 89 | |
| 90 | struct its_device; |
| 91 | |
| 92 | /* |
| 93 | * The ITS structure - contains most of the infrastructure, with the |
| 94 | * top-level MSI domain, the command queue, the collections, and the |
| 95 | * list of devices writing to it. |
| 96 | * |
| 97 | * dev_alloc_lock has to be taken for device allocations, while the |
| 98 | * spinlock must be taken to parse data structures such as the device |
| 99 | * list. |
| 100 | */ |
| 101 | struct its_node { |
| 102 | raw_spinlock_t lock; |
| 103 | struct mutex dev_alloc_lock; |
| 104 | struct list_head entry; |
| 105 | void __iomem *base; |
| 106 | phys_addr_t phys_base; |
| 107 | struct its_cmd_block *cmd_base; |
| 108 | struct its_cmd_block *cmd_write; |
| 109 | struct its_baser tables[GITS_BASER_NR_REGS]; |
| 110 | struct its_collection *collections; |
| 111 | struct fwnode_handle *fwnode_handle; |
| 112 | u64 (*get_msi_base)(struct its_device *its_dev); |
| 113 | u64 cbaser_save; |
| 114 | u32 ctlr_save; |
| 115 | struct list_head its_device_list; |
| 116 | u64 flags; |
| 117 | unsigned long list_nr; |
| 118 | u32 ite_size; |
| 119 | u32 device_ids; |
| 120 | int numa_node; |
| 121 | unsigned int msi_domain_flags; |
| 122 | u32 pre_its_base; /* for Socionext Synquacer */ |
| 123 | bool is_v4; |
| 124 | int vlpi_redist_offset; |
| 125 | }; |
| 126 | |
| 127 | #define ITS_ITT_ALIGN SZ_256 |
| 128 | |
| 129 | /* The maximum number of VPEID bits supported by VLPI commands */ |
| 130 | #define ITS_MAX_VPEID_BITS (16) |
| 131 | #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS)) |
| 132 | |
| 133 | /* Convert page order to size in bytes */ |
| 134 | #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o)) |
| 135 | |
| 136 | struct event_lpi_map { |
| 137 | unsigned long *lpi_map; |
| 138 | u16 *col_map; |
| 139 | irq_hw_number_t lpi_base; |
| 140 | int nr_lpis; |
| 141 | struct mutex vlpi_lock; |
| 142 | struct its_vm *vm; |
| 143 | struct its_vlpi_map *vlpi_maps; |
| 144 | int nr_vlpis; |
| 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * The ITS view of a device - belongs to an ITS, owns an interrupt |
| 149 | * translation table, and a list of interrupts. If it some of its |
| 150 | * LPIs are injected into a guest (GICv4), the event_map.vm field |
| 151 | * indicates which one. |
| 152 | */ |
| 153 | struct its_device { |
| 154 | struct list_head entry; |
| 155 | struct its_node *its; |
| 156 | struct event_lpi_map event_map; |
| 157 | void *itt; |
| 158 | u32 nr_ites; |
| 159 | u32 device_id; |
| 160 | bool shared; |
| 161 | }; |
| 162 | |
| 163 | static struct { |
| 164 | raw_spinlock_t lock; |
| 165 | struct its_device *dev; |
| 166 | struct its_vpe **vpes; |
| 167 | int next_victim; |
| 168 | } vpe_proxy; |
| 169 | |
| 170 | static LIST_HEAD(its_nodes); |
| 171 | static DEFINE_RAW_SPINLOCK(its_lock); |
| 172 | static struct rdists *gic_rdists; |
| 173 | static struct irq_domain *its_parent; |
| 174 | |
| 175 | static unsigned long its_list_map; |
| 176 | static u16 vmovp_seq_num; |
| 177 | static DEFINE_RAW_SPINLOCK(vmovp_lock); |
| 178 | |
| 179 | static DEFINE_IDA(its_vpeid_ida); |
| 180 | |
| 181 | #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist)) |
| 182 | #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) |
| 183 | #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K) |
| 184 | |
| 185 | static u16 get_its_list(struct its_vm *vm) |
| 186 | { |
| 187 | struct its_node *its; |
| 188 | unsigned long its_list = 0; |
| 189 | |
| 190 | list_for_each_entry(its, &its_nodes, entry) { |
| 191 | if (!its->is_v4) |
| 192 | continue; |
| 193 | |
| 194 | if (vm->vlpi_count[its->list_nr]) |
| 195 | __set_bit(its->list_nr, &its_list); |
| 196 | } |
| 197 | |
| 198 | return (u16)its_list; |
| 199 | } |
| 200 | |
| 201 | static struct its_collection *dev_event_to_col(struct its_device *its_dev, |
| 202 | u32 event) |
| 203 | { |
| 204 | struct its_node *its = its_dev->its; |
| 205 | |
| 206 | return its->collections + its_dev->event_map.col_map[event]; |
| 207 | } |
| 208 | |
| 209 | static struct its_collection *valid_col(struct its_collection *col) |
| 210 | { |
| 211 | if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(0, 15))) |
| 212 | return NULL; |
| 213 | |
| 214 | return col; |
| 215 | } |
| 216 | |
| 217 | static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe) |
| 218 | { |
| 219 | if (valid_col(its->collections + vpe->col_idx)) |
| 220 | return vpe; |
| 221 | |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * ITS command descriptors - parameters to be encoded in a command |
| 227 | * block. |
| 228 | */ |
| 229 | struct its_cmd_desc { |
| 230 | union { |
| 231 | struct { |
| 232 | struct its_device *dev; |
| 233 | u32 event_id; |
| 234 | } its_inv_cmd; |
| 235 | |
| 236 | struct { |
| 237 | struct its_device *dev; |
| 238 | u32 event_id; |
| 239 | } its_clear_cmd; |
| 240 | |
| 241 | struct { |
| 242 | struct its_device *dev; |
| 243 | u32 event_id; |
| 244 | } its_int_cmd; |
| 245 | |
| 246 | struct { |
| 247 | struct its_device *dev; |
| 248 | int valid; |
| 249 | } its_mapd_cmd; |
| 250 | |
| 251 | struct { |
| 252 | struct its_collection *col; |
| 253 | int valid; |
| 254 | } its_mapc_cmd; |
| 255 | |
| 256 | struct { |
| 257 | struct its_device *dev; |
| 258 | u32 phys_id; |
| 259 | u32 event_id; |
| 260 | } its_mapti_cmd; |
| 261 | |
| 262 | struct { |
| 263 | struct its_device *dev; |
| 264 | struct its_collection *col; |
| 265 | u32 event_id; |
| 266 | } its_movi_cmd; |
| 267 | |
| 268 | struct { |
| 269 | struct its_device *dev; |
| 270 | u32 event_id; |
| 271 | } its_discard_cmd; |
| 272 | |
| 273 | struct { |
| 274 | struct its_collection *col; |
| 275 | } its_invall_cmd; |
| 276 | |
| 277 | struct { |
| 278 | struct its_vpe *vpe; |
| 279 | } its_vinvall_cmd; |
| 280 | |
| 281 | struct { |
| 282 | struct its_vpe *vpe; |
| 283 | struct its_collection *col; |
| 284 | bool valid; |
| 285 | } its_vmapp_cmd; |
| 286 | |
| 287 | struct { |
| 288 | struct its_vpe *vpe; |
| 289 | struct its_device *dev; |
| 290 | u32 virt_id; |
| 291 | u32 event_id; |
| 292 | bool db_enabled; |
| 293 | } its_vmapti_cmd; |
| 294 | |
| 295 | struct { |
| 296 | struct its_vpe *vpe; |
| 297 | struct its_device *dev; |
| 298 | u32 event_id; |
| 299 | bool db_enabled; |
| 300 | } its_vmovi_cmd; |
| 301 | |
| 302 | struct { |
| 303 | struct its_vpe *vpe; |
| 304 | struct its_collection *col; |
| 305 | u16 seq_num; |
| 306 | u16 its_list; |
| 307 | } its_vmovp_cmd; |
| 308 | }; |
| 309 | }; |
| 310 | |
| 311 | /* |
| 312 | * The ITS command block, which is what the ITS actually parses. |
| 313 | */ |
| 314 | struct its_cmd_block { |
| 315 | u64 raw_cmd[4]; |
| 316 | }; |
| 317 | |
| 318 | #define ITS_CMD_QUEUE_SZ SZ_64K |
| 319 | #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block)) |
| 320 | |
| 321 | typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *, |
| 322 | struct its_cmd_block *, |
| 323 | struct its_cmd_desc *); |
| 324 | |
| 325 | typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *, |
| 326 | struct its_cmd_block *, |
| 327 | struct its_cmd_desc *); |
| 328 | |
| 329 | static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l) |
| 330 | { |
| 331 | u64 mask = GENMASK_ULL(h, l); |
| 332 | *raw_cmd &= ~mask; |
| 333 | *raw_cmd |= (val << l) & mask; |
| 334 | } |
| 335 | |
| 336 | static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr) |
| 337 | { |
| 338 | its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0); |
| 339 | } |
| 340 | |
| 341 | static void its_encode_devid(struct its_cmd_block *cmd, u32 devid) |
| 342 | { |
| 343 | its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32); |
| 344 | } |
| 345 | |
| 346 | static void its_encode_event_id(struct its_cmd_block *cmd, u32 id) |
| 347 | { |
| 348 | its_mask_encode(&cmd->raw_cmd[1], id, 31, 0); |
| 349 | } |
| 350 | |
| 351 | static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id) |
| 352 | { |
| 353 | its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32); |
| 354 | } |
| 355 | |
| 356 | static void its_encode_size(struct its_cmd_block *cmd, u8 size) |
| 357 | { |
| 358 | its_mask_encode(&cmd->raw_cmd[1], size, 4, 0); |
| 359 | } |
| 360 | |
| 361 | static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr) |
| 362 | { |
| 363 | its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8); |
| 364 | } |
| 365 | |
| 366 | static void its_encode_valid(struct its_cmd_block *cmd, int valid) |
| 367 | { |
| 368 | its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63); |
| 369 | } |
| 370 | |
| 371 | static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr) |
| 372 | { |
| 373 | its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16); |
| 374 | } |
| 375 | |
| 376 | static void its_encode_collection(struct its_cmd_block *cmd, u16 col) |
| 377 | { |
| 378 | its_mask_encode(&cmd->raw_cmd[2], col, 15, 0); |
| 379 | } |
| 380 | |
| 381 | static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid) |
| 382 | { |
| 383 | its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32); |
| 384 | } |
| 385 | |
| 386 | static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id) |
| 387 | { |
| 388 | its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0); |
| 389 | } |
| 390 | |
| 391 | static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id) |
| 392 | { |
| 393 | its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32); |
| 394 | } |
| 395 | |
| 396 | static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid) |
| 397 | { |
| 398 | its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0); |
| 399 | } |
| 400 | |
| 401 | static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num) |
| 402 | { |
| 403 | its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32); |
| 404 | } |
| 405 | |
| 406 | static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list) |
| 407 | { |
| 408 | its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0); |
| 409 | } |
| 410 | |
| 411 | static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa) |
| 412 | { |
| 413 | its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16); |
| 414 | } |
| 415 | |
| 416 | static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size) |
| 417 | { |
| 418 | its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0); |
| 419 | } |
| 420 | |
| 421 | static inline void its_fixup_cmd(struct its_cmd_block *cmd) |
| 422 | { |
| 423 | /* Let's fixup BE commands */ |
| 424 | cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]); |
| 425 | cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]); |
| 426 | cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]); |
| 427 | cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]); |
| 428 | } |
| 429 | |
| 430 | static struct its_collection *its_build_mapd_cmd(struct its_node *its, |
| 431 | struct its_cmd_block *cmd, |
| 432 | struct its_cmd_desc *desc) |
| 433 | { |
| 434 | unsigned long itt_addr; |
| 435 | u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites); |
| 436 | |
| 437 | itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt); |
| 438 | itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN); |
| 439 | |
| 440 | its_encode_cmd(cmd, GITS_CMD_MAPD); |
| 441 | its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id); |
| 442 | its_encode_size(cmd, size - 1); |
| 443 | its_encode_itt(cmd, itt_addr); |
| 444 | its_encode_valid(cmd, desc->its_mapd_cmd.valid); |
| 445 | |
| 446 | its_fixup_cmd(cmd); |
| 447 | |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | static struct its_collection *its_build_mapc_cmd(struct its_node *its, |
| 452 | struct its_cmd_block *cmd, |
| 453 | struct its_cmd_desc *desc) |
| 454 | { |
| 455 | its_encode_cmd(cmd, GITS_CMD_MAPC); |
| 456 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 457 | its_encode_target(cmd, desc->its_mapc_cmd.col->target_address); |
| 458 | its_encode_valid(cmd, desc->its_mapc_cmd.valid); |
| 459 | |
| 460 | its_fixup_cmd(cmd); |
| 461 | |
| 462 | return desc->its_mapc_cmd.col; |
| 463 | } |
| 464 | |
| 465 | static struct its_collection *its_build_mapti_cmd(struct its_node *its, |
| 466 | struct its_cmd_block *cmd, |
| 467 | struct its_cmd_desc *desc) |
| 468 | { |
| 469 | struct its_collection *col; |
| 470 | |
| 471 | col = dev_event_to_col(desc->its_mapti_cmd.dev, |
| 472 | desc->its_mapti_cmd.event_id); |
| 473 | |
| 474 | its_encode_cmd(cmd, GITS_CMD_MAPTI); |
| 475 | its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id); |
| 476 | its_encode_event_id(cmd, desc->its_mapti_cmd.event_id); |
| 477 | its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id); |
| 478 | its_encode_collection(cmd, col->col_id); |
| 479 | |
| 480 | its_fixup_cmd(cmd); |
| 481 | |
| 482 | return valid_col(col); |
| 483 | } |
| 484 | |
| 485 | static struct its_collection *its_build_movi_cmd(struct its_node *its, |
| 486 | struct its_cmd_block *cmd, |
| 487 | struct its_cmd_desc *desc) |
| 488 | { |
| 489 | struct its_collection *col; |
| 490 | |
| 491 | col = dev_event_to_col(desc->its_movi_cmd.dev, |
| 492 | desc->its_movi_cmd.event_id); |
| 493 | |
| 494 | its_encode_cmd(cmd, GITS_CMD_MOVI); |
| 495 | its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id); |
| 496 | its_encode_event_id(cmd, desc->its_movi_cmd.event_id); |
| 497 | its_encode_collection(cmd, desc->its_movi_cmd.col->col_id); |
| 498 | |
| 499 | its_fixup_cmd(cmd); |
| 500 | |
| 501 | return valid_col(col); |
| 502 | } |
| 503 | |
| 504 | static struct its_collection *its_build_discard_cmd(struct its_node *its, |
| 505 | struct its_cmd_block *cmd, |
| 506 | struct its_cmd_desc *desc) |
| 507 | { |
| 508 | struct its_collection *col; |
| 509 | |
| 510 | col = dev_event_to_col(desc->its_discard_cmd.dev, |
| 511 | desc->its_discard_cmd.event_id); |
| 512 | |
| 513 | its_encode_cmd(cmd, GITS_CMD_DISCARD); |
| 514 | its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id); |
| 515 | its_encode_event_id(cmd, desc->its_discard_cmd.event_id); |
| 516 | |
| 517 | its_fixup_cmd(cmd); |
| 518 | |
| 519 | return valid_col(col); |
| 520 | } |
| 521 | |
| 522 | static struct its_collection *its_build_inv_cmd(struct its_node *its, |
| 523 | struct its_cmd_block *cmd, |
| 524 | struct its_cmd_desc *desc) |
| 525 | { |
| 526 | struct its_collection *col; |
| 527 | |
| 528 | col = dev_event_to_col(desc->its_inv_cmd.dev, |
| 529 | desc->its_inv_cmd.event_id); |
| 530 | |
| 531 | its_encode_cmd(cmd, GITS_CMD_INV); |
| 532 | its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); |
| 533 | its_encode_event_id(cmd, desc->its_inv_cmd.event_id); |
| 534 | |
| 535 | its_fixup_cmd(cmd); |
| 536 | |
| 537 | return valid_col(col); |
| 538 | } |
| 539 | |
| 540 | static struct its_collection *its_build_int_cmd(struct its_node *its, |
| 541 | struct its_cmd_block *cmd, |
| 542 | struct its_cmd_desc *desc) |
| 543 | { |
| 544 | struct its_collection *col; |
| 545 | |
| 546 | col = dev_event_to_col(desc->its_int_cmd.dev, |
| 547 | desc->its_int_cmd.event_id); |
| 548 | |
| 549 | its_encode_cmd(cmd, GITS_CMD_INT); |
| 550 | its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); |
| 551 | its_encode_event_id(cmd, desc->its_int_cmd.event_id); |
| 552 | |
| 553 | its_fixup_cmd(cmd); |
| 554 | |
| 555 | return valid_col(col); |
| 556 | } |
| 557 | |
| 558 | static struct its_collection *its_build_clear_cmd(struct its_node *its, |
| 559 | struct its_cmd_block *cmd, |
| 560 | struct its_cmd_desc *desc) |
| 561 | { |
| 562 | struct its_collection *col; |
| 563 | |
| 564 | col = dev_event_to_col(desc->its_clear_cmd.dev, |
| 565 | desc->its_clear_cmd.event_id); |
| 566 | |
| 567 | its_encode_cmd(cmd, GITS_CMD_CLEAR); |
| 568 | its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); |
| 569 | its_encode_event_id(cmd, desc->its_clear_cmd.event_id); |
| 570 | |
| 571 | its_fixup_cmd(cmd); |
| 572 | |
| 573 | return valid_col(col); |
| 574 | } |
| 575 | |
| 576 | static struct its_collection *its_build_invall_cmd(struct its_node *its, |
| 577 | struct its_cmd_block *cmd, |
| 578 | struct its_cmd_desc *desc) |
| 579 | { |
| 580 | its_encode_cmd(cmd, GITS_CMD_INVALL); |
| 581 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 582 | |
| 583 | its_fixup_cmd(cmd); |
| 584 | |
| 585 | return NULL; |
| 586 | } |
| 587 | |
| 588 | static struct its_vpe *its_build_vinvall_cmd(struct its_node *its, |
| 589 | struct its_cmd_block *cmd, |
| 590 | struct its_cmd_desc *desc) |
| 591 | { |
| 592 | its_encode_cmd(cmd, GITS_CMD_VINVALL); |
| 593 | its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id); |
| 594 | |
| 595 | its_fixup_cmd(cmd); |
| 596 | |
| 597 | return valid_vpe(its, desc->its_vinvall_cmd.vpe); |
| 598 | } |
| 599 | |
| 600 | static struct its_vpe *its_build_vmapp_cmd(struct its_node *its, |
| 601 | struct its_cmd_block *cmd, |
| 602 | struct its_cmd_desc *desc) |
| 603 | { |
| 604 | unsigned long vpt_addr; |
| 605 | u64 target; |
| 606 | |
| 607 | vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page)); |
| 608 | target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset; |
| 609 | |
| 610 | its_encode_cmd(cmd, GITS_CMD_VMAPP); |
| 611 | its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id); |
| 612 | its_encode_valid(cmd, desc->its_vmapp_cmd.valid); |
| 613 | its_encode_target(cmd, target); |
| 614 | its_encode_vpt_addr(cmd, vpt_addr); |
| 615 | its_encode_vpt_size(cmd, LPI_NRBITS - 1); |
| 616 | |
| 617 | its_fixup_cmd(cmd); |
| 618 | |
| 619 | return valid_vpe(its, desc->its_vmapp_cmd.vpe); |
| 620 | } |
| 621 | |
| 622 | static struct its_vpe *its_build_vmapti_cmd(struct its_node *its, |
| 623 | struct its_cmd_block *cmd, |
| 624 | struct its_cmd_desc *desc) |
| 625 | { |
| 626 | u32 db; |
| 627 | |
| 628 | if (desc->its_vmapti_cmd.db_enabled) |
| 629 | db = desc->its_vmapti_cmd.vpe->vpe_db_lpi; |
| 630 | else |
| 631 | db = 1023; |
| 632 | |
| 633 | its_encode_cmd(cmd, GITS_CMD_VMAPTI); |
| 634 | its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id); |
| 635 | its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id); |
| 636 | its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id); |
| 637 | its_encode_db_phys_id(cmd, db); |
| 638 | its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id); |
| 639 | |
| 640 | its_fixup_cmd(cmd); |
| 641 | |
| 642 | return valid_vpe(its, desc->its_vmapti_cmd.vpe); |
| 643 | } |
| 644 | |
| 645 | static struct its_vpe *its_build_vmovi_cmd(struct its_node *its, |
| 646 | struct its_cmd_block *cmd, |
| 647 | struct its_cmd_desc *desc) |
| 648 | { |
| 649 | u32 db; |
| 650 | |
| 651 | if (desc->its_vmovi_cmd.db_enabled) |
| 652 | db = desc->its_vmovi_cmd.vpe->vpe_db_lpi; |
| 653 | else |
| 654 | db = 1023; |
| 655 | |
| 656 | its_encode_cmd(cmd, GITS_CMD_VMOVI); |
| 657 | its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id); |
| 658 | its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id); |
| 659 | its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id); |
| 660 | its_encode_db_phys_id(cmd, db); |
| 661 | its_encode_db_valid(cmd, true); |
| 662 | |
| 663 | its_fixup_cmd(cmd); |
| 664 | |
| 665 | return valid_vpe(its, desc->its_vmovi_cmd.vpe); |
| 666 | } |
| 667 | |
| 668 | static struct its_vpe *its_build_vmovp_cmd(struct its_node *its, |
| 669 | struct its_cmd_block *cmd, |
| 670 | struct its_cmd_desc *desc) |
| 671 | { |
| 672 | u64 target; |
| 673 | |
| 674 | target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset; |
| 675 | its_encode_cmd(cmd, GITS_CMD_VMOVP); |
| 676 | its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num); |
| 677 | its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list); |
| 678 | its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id); |
| 679 | its_encode_target(cmd, target); |
| 680 | |
| 681 | its_fixup_cmd(cmd); |
| 682 | |
| 683 | return valid_vpe(its, desc->its_vmovp_cmd.vpe); |
| 684 | } |
| 685 | |
| 686 | static u64 its_cmd_ptr_to_offset(struct its_node *its, |
| 687 | struct its_cmd_block *ptr) |
| 688 | { |
| 689 | return (ptr - its->cmd_base) * sizeof(*ptr); |
| 690 | } |
| 691 | |
| 692 | static int its_queue_full(struct its_node *its) |
| 693 | { |
| 694 | int widx; |
| 695 | int ridx; |
| 696 | |
| 697 | widx = its->cmd_write - its->cmd_base; |
| 698 | ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block); |
| 699 | |
| 700 | /* This is incredibly unlikely to happen, unless the ITS locks up. */ |
| 701 | if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx) |
| 702 | return 1; |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static struct its_cmd_block *its_allocate_entry(struct its_node *its) |
| 708 | { |
| 709 | struct its_cmd_block *cmd; |
| 710 | u32 count = 1000000; /* 1s! */ |
| 711 | |
| 712 | while (its_queue_full(its)) { |
| 713 | count--; |
| 714 | if (!count) { |
| 715 | pr_err_ratelimited("ITS queue not draining\n"); |
| 716 | return NULL; |
| 717 | } |
| 718 | cpu_relax(); |
| 719 | udelay(1); |
| 720 | } |
| 721 | |
| 722 | cmd = its->cmd_write++; |
| 723 | |
| 724 | /* Handle queue wrapping */ |
| 725 | if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES)) |
| 726 | its->cmd_write = its->cmd_base; |
| 727 | |
| 728 | /* Clear command */ |
| 729 | cmd->raw_cmd[0] = 0; |
| 730 | cmd->raw_cmd[1] = 0; |
| 731 | cmd->raw_cmd[2] = 0; |
| 732 | cmd->raw_cmd[3] = 0; |
| 733 | |
| 734 | return cmd; |
| 735 | } |
| 736 | |
| 737 | static struct its_cmd_block *its_post_commands(struct its_node *its) |
| 738 | { |
| 739 | u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write); |
| 740 | |
| 741 | writel_relaxed(wr, its->base + GITS_CWRITER); |
| 742 | |
| 743 | return its->cmd_write; |
| 744 | } |
| 745 | |
| 746 | static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd) |
| 747 | { |
| 748 | /* |
| 749 | * Make sure the commands written to memory are observable by |
| 750 | * the ITS. |
| 751 | */ |
| 752 | if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING) |
| 753 | gic_flush_dcache_to_poc(cmd, sizeof(*cmd)); |
| 754 | else |
| 755 | dsb(ishst); |
| 756 | } |
| 757 | |
| 758 | static int its_wait_for_range_completion(struct its_node *its, |
| 759 | u64 prev_idx, |
| 760 | struct its_cmd_block *to) |
| 761 | { |
| 762 | u64 rd_idx, to_idx, linear_idx; |
| 763 | u32 count = 1000000; /* 1s! */ |
| 764 | |
| 765 | /* Linearize to_idx if the command set has wrapped around */ |
| 766 | to_idx = its_cmd_ptr_to_offset(its, to); |
| 767 | if (to_idx < prev_idx) |
| 768 | to_idx += ITS_CMD_QUEUE_SZ; |
| 769 | |
| 770 | linear_idx = prev_idx; |
| 771 | |
| 772 | while (1) { |
| 773 | s64 delta; |
| 774 | |
| 775 | rd_idx = readl_relaxed(its->base + GITS_CREADR); |
| 776 | |
| 777 | /* |
| 778 | * Compute the read pointer progress, taking the |
| 779 | * potential wrap-around into account. |
| 780 | */ |
| 781 | delta = rd_idx - prev_idx; |
| 782 | if (rd_idx < prev_idx) |
| 783 | delta += ITS_CMD_QUEUE_SZ; |
| 784 | |
| 785 | linear_idx += delta; |
| 786 | if (linear_idx >= to_idx) |
| 787 | break; |
| 788 | |
| 789 | count--; |
| 790 | if (!count) { |
| 791 | pr_err_ratelimited("ITS queue timeout (%llu %llu)\n", |
| 792 | to_idx, linear_idx); |
| 793 | return -1; |
| 794 | } |
| 795 | prev_idx = rd_idx; |
| 796 | cpu_relax(); |
| 797 | udelay(1); |
| 798 | } |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | /* Warning, macro hell follows */ |
| 804 | #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \ |
| 805 | void name(struct its_node *its, \ |
| 806 | buildtype builder, \ |
| 807 | struct its_cmd_desc *desc) \ |
| 808 | { \ |
| 809 | struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \ |
| 810 | synctype *sync_obj; \ |
| 811 | unsigned long flags; \ |
| 812 | u64 rd_idx; \ |
| 813 | \ |
| 814 | raw_spin_lock_irqsave(&its->lock, flags); \ |
| 815 | \ |
| 816 | cmd = its_allocate_entry(its); \ |
| 817 | if (!cmd) { /* We're soooooo screewed... */ \ |
| 818 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 819 | return; \ |
| 820 | } \ |
| 821 | sync_obj = builder(its, cmd, desc); \ |
| 822 | its_flush_cmd(its, cmd); \ |
| 823 | \ |
| 824 | if (sync_obj) { \ |
| 825 | sync_cmd = its_allocate_entry(its); \ |
| 826 | if (!sync_cmd) \ |
| 827 | goto post; \ |
| 828 | \ |
| 829 | buildfn(its, sync_cmd, sync_obj); \ |
| 830 | its_flush_cmd(its, sync_cmd); \ |
| 831 | } \ |
| 832 | \ |
| 833 | post: \ |
| 834 | rd_idx = readl_relaxed(its->base + GITS_CREADR); \ |
| 835 | next_cmd = its_post_commands(its); \ |
| 836 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 837 | \ |
| 838 | if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \ |
| 839 | pr_err_ratelimited("ITS cmd %ps failed\n", builder); \ |
| 840 | } |
| 841 | |
| 842 | static void its_build_sync_cmd(struct its_node *its, |
| 843 | struct its_cmd_block *sync_cmd, |
| 844 | struct its_collection *sync_col) |
| 845 | { |
| 846 | its_encode_cmd(sync_cmd, GITS_CMD_SYNC); |
| 847 | its_encode_target(sync_cmd, sync_col->target_address); |
| 848 | |
| 849 | its_fixup_cmd(sync_cmd); |
| 850 | } |
| 851 | |
| 852 | static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t, |
| 853 | struct its_collection, its_build_sync_cmd) |
| 854 | |
| 855 | static void its_build_vsync_cmd(struct its_node *its, |
| 856 | struct its_cmd_block *sync_cmd, |
| 857 | struct its_vpe *sync_vpe) |
| 858 | { |
| 859 | its_encode_cmd(sync_cmd, GITS_CMD_VSYNC); |
| 860 | its_encode_vpeid(sync_cmd, sync_vpe->vpe_id); |
| 861 | |
| 862 | its_fixup_cmd(sync_cmd); |
| 863 | } |
| 864 | |
| 865 | static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t, |
| 866 | struct its_vpe, its_build_vsync_cmd) |
| 867 | |
| 868 | static void its_send_int(struct its_device *dev, u32 event_id) |
| 869 | { |
| 870 | struct its_cmd_desc desc; |
| 871 | |
| 872 | desc.its_int_cmd.dev = dev; |
| 873 | desc.its_int_cmd.event_id = event_id; |
| 874 | |
| 875 | its_send_single_command(dev->its, its_build_int_cmd, &desc); |
| 876 | } |
| 877 | |
| 878 | static void its_send_clear(struct its_device *dev, u32 event_id) |
| 879 | { |
| 880 | struct its_cmd_desc desc; |
| 881 | |
| 882 | desc.its_clear_cmd.dev = dev; |
| 883 | desc.its_clear_cmd.event_id = event_id; |
| 884 | |
| 885 | its_send_single_command(dev->its, its_build_clear_cmd, &desc); |
| 886 | } |
| 887 | |
| 888 | static void its_send_inv(struct its_device *dev, u32 event_id) |
| 889 | { |
| 890 | struct its_cmd_desc desc; |
| 891 | |
| 892 | desc.its_inv_cmd.dev = dev; |
| 893 | desc.its_inv_cmd.event_id = event_id; |
| 894 | |
| 895 | its_send_single_command(dev->its, its_build_inv_cmd, &desc); |
| 896 | } |
| 897 | |
| 898 | static void its_send_mapd(struct its_device *dev, int valid) |
| 899 | { |
| 900 | struct its_cmd_desc desc; |
| 901 | |
| 902 | desc.its_mapd_cmd.dev = dev; |
| 903 | desc.its_mapd_cmd.valid = !!valid; |
| 904 | |
| 905 | its_send_single_command(dev->its, its_build_mapd_cmd, &desc); |
| 906 | } |
| 907 | |
| 908 | static void its_send_mapc(struct its_node *its, struct its_collection *col, |
| 909 | int valid) |
| 910 | { |
| 911 | struct its_cmd_desc desc; |
| 912 | |
| 913 | desc.its_mapc_cmd.col = col; |
| 914 | desc.its_mapc_cmd.valid = !!valid; |
| 915 | |
| 916 | its_send_single_command(its, its_build_mapc_cmd, &desc); |
| 917 | } |
| 918 | |
| 919 | static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id) |
| 920 | { |
| 921 | struct its_cmd_desc desc; |
| 922 | |
| 923 | desc.its_mapti_cmd.dev = dev; |
| 924 | desc.its_mapti_cmd.phys_id = irq_id; |
| 925 | desc.its_mapti_cmd.event_id = id; |
| 926 | |
| 927 | its_send_single_command(dev->its, its_build_mapti_cmd, &desc); |
| 928 | } |
| 929 | |
| 930 | static void its_send_movi(struct its_device *dev, |
| 931 | struct its_collection *col, u32 id) |
| 932 | { |
| 933 | struct its_cmd_desc desc; |
| 934 | |
| 935 | desc.its_movi_cmd.dev = dev; |
| 936 | desc.its_movi_cmd.col = col; |
| 937 | desc.its_movi_cmd.event_id = id; |
| 938 | |
| 939 | its_send_single_command(dev->its, its_build_movi_cmd, &desc); |
| 940 | } |
| 941 | |
| 942 | static void its_send_discard(struct its_device *dev, u32 id) |
| 943 | { |
| 944 | struct its_cmd_desc desc; |
| 945 | |
| 946 | desc.its_discard_cmd.dev = dev; |
| 947 | desc.its_discard_cmd.event_id = id; |
| 948 | |
| 949 | its_send_single_command(dev->its, its_build_discard_cmd, &desc); |
| 950 | } |
| 951 | |
| 952 | static void its_send_invall(struct its_node *its, struct its_collection *col) |
| 953 | { |
| 954 | struct its_cmd_desc desc; |
| 955 | |
| 956 | desc.its_invall_cmd.col = col; |
| 957 | |
| 958 | its_send_single_command(its, its_build_invall_cmd, &desc); |
| 959 | } |
| 960 | |
| 961 | static void its_send_vmapti(struct its_device *dev, u32 id) |
| 962 | { |
| 963 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 964 | struct its_cmd_desc desc; |
| 965 | |
| 966 | desc.its_vmapti_cmd.vpe = map->vpe; |
| 967 | desc.its_vmapti_cmd.dev = dev; |
| 968 | desc.its_vmapti_cmd.virt_id = map->vintid; |
| 969 | desc.its_vmapti_cmd.event_id = id; |
| 970 | desc.its_vmapti_cmd.db_enabled = map->db_enabled; |
| 971 | |
| 972 | its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc); |
| 973 | } |
| 974 | |
| 975 | static void its_send_vmovi(struct its_device *dev, u32 id) |
| 976 | { |
| 977 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 978 | struct its_cmd_desc desc; |
| 979 | |
| 980 | desc.its_vmovi_cmd.vpe = map->vpe; |
| 981 | desc.its_vmovi_cmd.dev = dev; |
| 982 | desc.its_vmovi_cmd.event_id = id; |
| 983 | desc.its_vmovi_cmd.db_enabled = map->db_enabled; |
| 984 | |
| 985 | its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc); |
| 986 | } |
| 987 | |
| 988 | static void its_send_vmapp(struct its_node *its, |
| 989 | struct its_vpe *vpe, bool valid) |
| 990 | { |
| 991 | struct its_cmd_desc desc; |
| 992 | |
| 993 | desc.its_vmapp_cmd.vpe = vpe; |
| 994 | desc.its_vmapp_cmd.valid = valid; |
| 995 | desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx]; |
| 996 | |
| 997 | its_send_single_vcommand(its, its_build_vmapp_cmd, &desc); |
| 998 | } |
| 999 | |
| 1000 | static void its_send_vmovp(struct its_vpe *vpe) |
| 1001 | { |
| 1002 | struct its_cmd_desc desc = {}; |
| 1003 | struct its_node *its; |
| 1004 | unsigned long flags; |
| 1005 | int col_id = vpe->col_idx; |
| 1006 | |
| 1007 | desc.its_vmovp_cmd.vpe = vpe; |
| 1008 | |
| 1009 | if (!its_list_map) { |
| 1010 | its = list_first_entry(&its_nodes, struct its_node, entry); |
| 1011 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 1012 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Yet another marvel of the architecture. If using the |
| 1018 | * its_list "feature", we need to make sure that all ITSs |
| 1019 | * receive all VMOVP commands in the same order. The only way |
| 1020 | * to guarantee this is to make vmovp a serialization point. |
| 1021 | * |
| 1022 | * Wall <-- Head. |
| 1023 | */ |
| 1024 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1025 | |
| 1026 | desc.its_vmovp_cmd.seq_num = vmovp_seq_num++; |
| 1027 | desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm); |
| 1028 | |
| 1029 | /* Emit VMOVPs */ |
| 1030 | list_for_each_entry(its, &its_nodes, entry) { |
| 1031 | if (!its->is_v4) |
| 1032 | continue; |
| 1033 | |
| 1034 | if (!vpe->its_vm->vlpi_count[its->list_nr]) |
| 1035 | continue; |
| 1036 | |
| 1037 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 1038 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 1039 | } |
| 1040 | |
| 1041 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1042 | } |
| 1043 | |
| 1044 | static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe) |
| 1045 | { |
| 1046 | struct its_cmd_desc desc; |
| 1047 | |
| 1048 | desc.its_vinvall_cmd.vpe = vpe; |
| 1049 | its_send_single_vcommand(its, its_build_vinvall_cmd, &desc); |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * irqchip functions - assumes MSI, mostly. |
| 1054 | */ |
| 1055 | |
| 1056 | static inline u32 its_get_event_id(struct irq_data *d) |
| 1057 | { |
| 1058 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1059 | return d->hwirq - its_dev->event_map.lpi_base; |
| 1060 | } |
| 1061 | |
| 1062 | static void lpi_write_config(struct irq_data *d, u8 clr, u8 set) |
| 1063 | { |
| 1064 | irq_hw_number_t hwirq; |
| 1065 | struct page *prop_page; |
| 1066 | u8 *cfg; |
| 1067 | |
| 1068 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 1069 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1070 | u32 event = its_get_event_id(d); |
| 1071 | struct its_vlpi_map *map; |
| 1072 | |
| 1073 | prop_page = its_dev->event_map.vm->vprop_page; |
| 1074 | map = &its_dev->event_map.vlpi_maps[event]; |
| 1075 | hwirq = map->vintid; |
| 1076 | |
| 1077 | /* Remember the updated property */ |
| 1078 | map->properties &= ~clr; |
| 1079 | map->properties |= set | LPI_PROP_GROUP1; |
| 1080 | } else { |
| 1081 | prop_page = gic_rdists->prop_page; |
| 1082 | hwirq = d->hwirq; |
| 1083 | } |
| 1084 | |
| 1085 | cfg = page_address(prop_page) + hwirq - 8192; |
| 1086 | *cfg &= ~clr; |
| 1087 | *cfg |= set | LPI_PROP_GROUP1; |
| 1088 | |
| 1089 | /* |
| 1090 | * Make the above write visible to the redistributors. |
| 1091 | * And yes, we're flushing exactly: One. Single. Byte. |
| 1092 | * Humpf... |
| 1093 | */ |
| 1094 | if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING) |
| 1095 | gic_flush_dcache_to_poc(cfg, sizeof(*cfg)); |
| 1096 | else |
| 1097 | dsb(ishst); |
| 1098 | } |
| 1099 | |
| 1100 | static void lpi_update_config(struct irq_data *d, u8 clr, u8 set) |
| 1101 | { |
| 1102 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1103 | |
| 1104 | lpi_write_config(d, clr, set); |
| 1105 | its_send_inv(its_dev, its_get_event_id(d)); |
| 1106 | } |
| 1107 | |
| 1108 | static void its_vlpi_set_doorbell(struct irq_data *d, bool enable) |
| 1109 | { |
| 1110 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1111 | u32 event = its_get_event_id(d); |
| 1112 | |
| 1113 | if (its_dev->event_map.vlpi_maps[event].db_enabled == enable) |
| 1114 | return; |
| 1115 | |
| 1116 | its_dev->event_map.vlpi_maps[event].db_enabled = enable; |
| 1117 | |
| 1118 | /* |
| 1119 | * More fun with the architecture: |
| 1120 | * |
| 1121 | * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI |
| 1122 | * value or to 1023, depending on the enable bit. But that |
| 1123 | * would be issueing a mapping for an /existing/ DevID+EventID |
| 1124 | * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI |
| 1125 | * to the /same/ vPE, using this opportunity to adjust the |
| 1126 | * doorbell. Mouahahahaha. We loves it, Precious. |
| 1127 | */ |
| 1128 | its_send_vmovi(its_dev, event); |
| 1129 | } |
| 1130 | |
| 1131 | static void its_mask_irq(struct irq_data *d) |
| 1132 | { |
| 1133 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1134 | its_vlpi_set_doorbell(d, false); |
| 1135 | |
| 1136 | lpi_update_config(d, LPI_PROP_ENABLED, 0); |
| 1137 | } |
| 1138 | |
| 1139 | static void its_unmask_irq(struct irq_data *d) |
| 1140 | { |
| 1141 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1142 | its_vlpi_set_doorbell(d, true); |
| 1143 | |
| 1144 | lpi_update_config(d, 0, LPI_PROP_ENABLED); |
| 1145 | } |
| 1146 | |
| 1147 | static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val, |
| 1148 | bool force) |
| 1149 | { |
| 1150 | unsigned int cpu; |
| 1151 | const struct cpumask *cpu_mask = cpu_online_mask; |
| 1152 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1153 | struct its_collection *target_col; |
| 1154 | u32 id = its_get_event_id(d); |
| 1155 | |
| 1156 | /* A forwarded interrupt should use irq_set_vcpu_affinity */ |
| 1157 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1158 | return -EINVAL; |
| 1159 | |
| 1160 | /* lpi cannot be routed to a redistributor that is on a foreign node */ |
| 1161 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 1162 | if (its_dev->its->numa_node >= 0) { |
| 1163 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
| 1164 | if (!cpumask_intersects(mask_val, cpu_mask)) |
| 1165 | return -EINVAL; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | cpu = cpumask_any_and(mask_val, cpu_mask); |
| 1170 | |
| 1171 | if (cpu >= nr_cpu_ids) |
| 1172 | return -EINVAL; |
| 1173 | |
| 1174 | /* don't set the affinity when the target cpu is same as current one */ |
| 1175 | if (cpu != its_dev->event_map.col_map[id]) { |
| 1176 | target_col = &its_dev->its->collections[cpu]; |
| 1177 | its_send_movi(its_dev, target_col, id); |
| 1178 | its_dev->event_map.col_map[id] = cpu; |
| 1179 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
| 1180 | } |
| 1181 | |
| 1182 | return IRQ_SET_MASK_OK_DONE; |
| 1183 | } |
| 1184 | |
| 1185 | static u64 its_irq_get_msi_base(struct its_device *its_dev) |
| 1186 | { |
| 1187 | struct its_node *its = its_dev->its; |
| 1188 | |
| 1189 | return its->phys_base + GITS_TRANSLATER; |
| 1190 | } |
| 1191 | |
| 1192 | static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) |
| 1193 | { |
| 1194 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1195 | struct its_node *its; |
| 1196 | u64 addr; |
| 1197 | |
| 1198 | its = its_dev->its; |
| 1199 | addr = its->get_msi_base(its_dev); |
| 1200 | |
| 1201 | msg->address_lo = lower_32_bits(addr); |
| 1202 | msg->address_hi = upper_32_bits(addr); |
| 1203 | msg->data = its_get_event_id(d); |
| 1204 | |
| 1205 | iommu_dma_map_msi_msg(d->irq, msg); |
| 1206 | } |
| 1207 | |
| 1208 | static int its_irq_set_irqchip_state(struct irq_data *d, |
| 1209 | enum irqchip_irq_state which, |
| 1210 | bool state) |
| 1211 | { |
| 1212 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1213 | u32 event = its_get_event_id(d); |
| 1214 | |
| 1215 | if (which != IRQCHIP_STATE_PENDING) |
| 1216 | return -EINVAL; |
| 1217 | |
| 1218 | if (state) |
| 1219 | its_send_int(its_dev, event); |
| 1220 | else |
| 1221 | its_send_clear(its_dev, event); |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | static void its_map_vm(struct its_node *its, struct its_vm *vm) |
| 1227 | { |
| 1228 | unsigned long flags; |
| 1229 | |
| 1230 | /* Not using the ITS list? Everything is always mapped. */ |
| 1231 | if (!its_list_map) |
| 1232 | return; |
| 1233 | |
| 1234 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1235 | |
| 1236 | /* |
| 1237 | * If the VM wasn't mapped yet, iterate over the vpes and get |
| 1238 | * them mapped now. |
| 1239 | */ |
| 1240 | vm->vlpi_count[its->list_nr]++; |
| 1241 | |
| 1242 | if (vm->vlpi_count[its->list_nr] == 1) { |
| 1243 | int i; |
| 1244 | |
| 1245 | for (i = 0; i < vm->nr_vpes; i++) { |
| 1246 | struct its_vpe *vpe = vm->vpes[i]; |
| 1247 | struct irq_data *d = irq_get_irq_data(vpe->irq); |
| 1248 | |
| 1249 | /* Map the VPE to the first possible CPU */ |
| 1250 | vpe->col_idx = cpumask_first(cpu_online_mask); |
| 1251 | its_send_vmapp(its, vpe, true); |
| 1252 | its_send_vinvall(its, vpe); |
| 1253 | irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1258 | } |
| 1259 | |
| 1260 | static void its_unmap_vm(struct its_node *its, struct its_vm *vm) |
| 1261 | { |
| 1262 | unsigned long flags; |
| 1263 | |
| 1264 | /* Not using the ITS list? Everything is always mapped. */ |
| 1265 | if (!its_list_map) |
| 1266 | return; |
| 1267 | |
| 1268 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1269 | |
| 1270 | if (!--vm->vlpi_count[its->list_nr]) { |
| 1271 | int i; |
| 1272 | |
| 1273 | for (i = 0; i < vm->nr_vpes; i++) |
| 1274 | its_send_vmapp(its, vm->vpes[i], false); |
| 1275 | } |
| 1276 | |
| 1277 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1278 | } |
| 1279 | |
| 1280 | static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) |
| 1281 | { |
| 1282 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1283 | u32 event = its_get_event_id(d); |
| 1284 | int ret = 0; |
| 1285 | |
| 1286 | if (!info->map) |
| 1287 | return -EINVAL; |
| 1288 | |
| 1289 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1290 | |
| 1291 | if (!its_dev->event_map.vm) { |
| 1292 | struct its_vlpi_map *maps; |
| 1293 | |
| 1294 | maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps), |
| 1295 | GFP_KERNEL); |
| 1296 | if (!maps) { |
| 1297 | ret = -ENOMEM; |
| 1298 | goto out; |
| 1299 | } |
| 1300 | |
| 1301 | its_dev->event_map.vm = info->map->vm; |
| 1302 | its_dev->event_map.vlpi_maps = maps; |
| 1303 | } else if (its_dev->event_map.vm != info->map->vm) { |
| 1304 | ret = -EINVAL; |
| 1305 | goto out; |
| 1306 | } |
| 1307 | |
| 1308 | /* Get our private copy of the mapping information */ |
| 1309 | its_dev->event_map.vlpi_maps[event] = *info->map; |
| 1310 | |
| 1311 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 1312 | /* Already mapped, move it around */ |
| 1313 | its_send_vmovi(its_dev, event); |
| 1314 | } else { |
| 1315 | /* Ensure all the VPEs are mapped on this ITS */ |
| 1316 | its_map_vm(its_dev->its, info->map->vm); |
| 1317 | |
| 1318 | /* |
| 1319 | * Flag the interrupt as forwarded so that we can |
| 1320 | * start poking the virtual property table. |
| 1321 | */ |
| 1322 | irqd_set_forwarded_to_vcpu(d); |
| 1323 | |
| 1324 | /* Write out the property to the prop table */ |
| 1325 | lpi_write_config(d, 0xff, info->map->properties); |
| 1326 | |
| 1327 | /* Drop the physical mapping */ |
| 1328 | its_send_discard(its_dev, event); |
| 1329 | |
| 1330 | /* and install the virtual one */ |
| 1331 | its_send_vmapti(its_dev, event); |
| 1332 | |
| 1333 | /* Increment the number of VLPIs */ |
| 1334 | its_dev->event_map.nr_vlpis++; |
| 1335 | } |
| 1336 | |
| 1337 | out: |
| 1338 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) |
| 1343 | { |
| 1344 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1345 | u32 event = its_get_event_id(d); |
| 1346 | int ret = 0; |
| 1347 | |
| 1348 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1349 | |
| 1350 | if (!its_dev->event_map.vm || |
| 1351 | !its_dev->event_map.vlpi_maps[event].vm) { |
| 1352 | ret = -EINVAL; |
| 1353 | goto out; |
| 1354 | } |
| 1355 | |
| 1356 | /* Copy our mapping information to the incoming request */ |
| 1357 | *info->map = its_dev->event_map.vlpi_maps[event]; |
| 1358 | |
| 1359 | out: |
| 1360 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1361 | return ret; |
| 1362 | } |
| 1363 | |
| 1364 | static int its_vlpi_unmap(struct irq_data *d) |
| 1365 | { |
| 1366 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1367 | u32 event = its_get_event_id(d); |
| 1368 | int ret = 0; |
| 1369 | |
| 1370 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1371 | |
| 1372 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { |
| 1373 | ret = -EINVAL; |
| 1374 | goto out; |
| 1375 | } |
| 1376 | |
| 1377 | /* Drop the virtual mapping */ |
| 1378 | its_send_discard(its_dev, event); |
| 1379 | |
| 1380 | /* and restore the physical one */ |
| 1381 | irqd_clr_forwarded_to_vcpu(d); |
| 1382 | its_send_mapti(its_dev, d->hwirq, event); |
| 1383 | lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO | |
| 1384 | LPI_PROP_ENABLED | |
| 1385 | LPI_PROP_GROUP1)); |
| 1386 | |
| 1387 | /* Potentially unmap the VM from this ITS */ |
| 1388 | its_unmap_vm(its_dev->its, its_dev->event_map.vm); |
| 1389 | |
| 1390 | /* |
| 1391 | * Drop the refcount and make the device available again if |
| 1392 | * this was the last VLPI. |
| 1393 | */ |
| 1394 | if (!--its_dev->event_map.nr_vlpis) { |
| 1395 | its_dev->event_map.vm = NULL; |
| 1396 | kfree(its_dev->event_map.vlpi_maps); |
| 1397 | } |
| 1398 | |
| 1399 | out: |
| 1400 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1401 | return ret; |
| 1402 | } |
| 1403 | |
| 1404 | static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) |
| 1405 | { |
| 1406 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1407 | |
| 1408 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) |
| 1409 | return -EINVAL; |
| 1410 | |
| 1411 | if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI) |
| 1412 | lpi_update_config(d, 0xff, info->config); |
| 1413 | else |
| 1414 | lpi_write_config(d, 0xff, info->config); |
| 1415 | its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED)); |
| 1416 | |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 1421 | { |
| 1422 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1423 | struct its_cmd_info *info = vcpu_info; |
| 1424 | |
| 1425 | /* Need a v4 ITS */ |
| 1426 | if (!its_dev->its->is_v4) |
| 1427 | return -EINVAL; |
| 1428 | |
| 1429 | /* Unmap request? */ |
| 1430 | if (!info) |
| 1431 | return its_vlpi_unmap(d); |
| 1432 | |
| 1433 | switch (info->cmd_type) { |
| 1434 | case MAP_VLPI: |
| 1435 | return its_vlpi_map(d, info); |
| 1436 | |
| 1437 | case GET_VLPI: |
| 1438 | return its_vlpi_get(d, info); |
| 1439 | |
| 1440 | case PROP_UPDATE_VLPI: |
| 1441 | case PROP_UPDATE_AND_INV_VLPI: |
| 1442 | return its_vlpi_prop_update(d, info); |
| 1443 | |
| 1444 | default: |
| 1445 | return -EINVAL; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | static struct irq_chip its_irq_chip = { |
| 1450 | .name = "ITS", |
| 1451 | .irq_mask = its_mask_irq, |
| 1452 | .irq_unmask = its_unmask_irq, |
| 1453 | .irq_eoi = irq_chip_eoi_parent, |
| 1454 | .irq_set_affinity = its_set_affinity, |
| 1455 | .irq_compose_msi_msg = its_irq_compose_msi_msg, |
| 1456 | .irq_set_irqchip_state = its_irq_set_irqchip_state, |
| 1457 | .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity, |
| 1458 | }; |
| 1459 | |
| 1460 | |
| 1461 | /* |
| 1462 | * How we allocate LPIs: |
| 1463 | * |
| 1464 | * lpi_range_list contains ranges of LPIs that are to available to |
| 1465 | * allocate from. To allocate LPIs, just pick the first range that |
| 1466 | * fits the required allocation, and reduce it by the required |
| 1467 | * amount. Once empty, remove the range from the list. |
| 1468 | * |
| 1469 | * To free a range of LPIs, add a free range to the list, sort it and |
| 1470 | * merge the result if the new range happens to be adjacent to an |
| 1471 | * already free block. |
| 1472 | * |
| 1473 | * The consequence of the above is that allocation is cost is low, but |
| 1474 | * freeing is expensive. We assumes that freeing rarely occurs. |
| 1475 | */ |
| 1476 | #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */ |
| 1477 | |
| 1478 | static DEFINE_MUTEX(lpi_range_lock); |
| 1479 | static LIST_HEAD(lpi_range_list); |
| 1480 | |
| 1481 | struct lpi_range { |
| 1482 | struct list_head entry; |
| 1483 | u32 base_id; |
| 1484 | u32 span; |
| 1485 | }; |
| 1486 | |
| 1487 | static struct lpi_range *mk_lpi_range(u32 base, u32 span) |
| 1488 | { |
| 1489 | struct lpi_range *range; |
| 1490 | |
| 1491 | range = kzalloc(sizeof(*range), GFP_KERNEL); |
| 1492 | if (range) { |
| 1493 | INIT_LIST_HEAD(&range->entry); |
| 1494 | range->base_id = base; |
| 1495 | range->span = span; |
| 1496 | } |
| 1497 | |
| 1498 | return range; |
| 1499 | } |
| 1500 | |
| 1501 | static int lpi_range_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1502 | { |
| 1503 | struct lpi_range *ra, *rb; |
| 1504 | |
| 1505 | ra = container_of(a, struct lpi_range, entry); |
| 1506 | rb = container_of(b, struct lpi_range, entry); |
| 1507 | |
| 1508 | return ra->base_id - rb->base_id; |
| 1509 | } |
| 1510 | |
| 1511 | static void merge_lpi_ranges(void) |
| 1512 | { |
| 1513 | struct lpi_range *range, *tmp; |
| 1514 | |
| 1515 | list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) { |
| 1516 | if (!list_is_last(&range->entry, &lpi_range_list) && |
| 1517 | (tmp->base_id == (range->base_id + range->span))) { |
| 1518 | tmp->base_id = range->base_id; |
| 1519 | tmp->span += range->span; |
| 1520 | list_del(&range->entry); |
| 1521 | kfree(range); |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | static int alloc_lpi_range(u32 nr_lpis, u32 *base) |
| 1527 | { |
| 1528 | struct lpi_range *range, *tmp; |
| 1529 | int err = -ENOSPC; |
| 1530 | |
| 1531 | mutex_lock(&lpi_range_lock); |
| 1532 | |
| 1533 | list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) { |
| 1534 | if (range->span >= nr_lpis) { |
| 1535 | *base = range->base_id; |
| 1536 | range->base_id += nr_lpis; |
| 1537 | range->span -= nr_lpis; |
| 1538 | |
| 1539 | if (range->span == 0) { |
| 1540 | list_del(&range->entry); |
| 1541 | kfree(range); |
| 1542 | } |
| 1543 | |
| 1544 | err = 0; |
| 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | mutex_unlock(&lpi_range_lock); |
| 1550 | |
| 1551 | pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis); |
| 1552 | return err; |
| 1553 | } |
| 1554 | |
| 1555 | static int free_lpi_range(u32 base, u32 nr_lpis) |
| 1556 | { |
| 1557 | struct lpi_range *new; |
| 1558 | int err = 0; |
| 1559 | |
| 1560 | mutex_lock(&lpi_range_lock); |
| 1561 | |
| 1562 | new = mk_lpi_range(base, nr_lpis); |
| 1563 | if (!new) { |
| 1564 | err = -ENOMEM; |
| 1565 | goto out; |
| 1566 | } |
| 1567 | |
| 1568 | list_add(&new->entry, &lpi_range_list); |
| 1569 | list_sort(NULL, &lpi_range_list, lpi_range_cmp); |
| 1570 | merge_lpi_ranges(); |
| 1571 | out: |
| 1572 | mutex_unlock(&lpi_range_lock); |
| 1573 | return err; |
| 1574 | } |
| 1575 | |
| 1576 | static int __init its_lpi_init(u32 id_bits) |
| 1577 | { |
| 1578 | u32 lpis = (1UL << id_bits) - 8192; |
| 1579 | u32 numlpis; |
| 1580 | int err; |
| 1581 | |
| 1582 | numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer); |
| 1583 | |
| 1584 | if (numlpis > 2 && !WARN_ON(numlpis > lpis)) { |
| 1585 | lpis = numlpis; |
| 1586 | pr_info("ITS: Using hypervisor restricted LPI range [%u]\n", |
| 1587 | lpis); |
| 1588 | } |
| 1589 | |
| 1590 | /* |
| 1591 | * Initializing the allocator is just the same as freeing the |
| 1592 | * full range of LPIs. |
| 1593 | */ |
| 1594 | err = free_lpi_range(8192, lpis); |
| 1595 | pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis); |
| 1596 | return err; |
| 1597 | } |
| 1598 | |
| 1599 | static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids) |
| 1600 | { |
| 1601 | unsigned long *bitmap = NULL; |
| 1602 | int err = 0; |
| 1603 | |
| 1604 | do { |
| 1605 | err = alloc_lpi_range(nr_irqs, base); |
| 1606 | if (!err) |
| 1607 | break; |
| 1608 | |
| 1609 | nr_irqs /= 2; |
| 1610 | } while (nr_irqs > 0); |
| 1611 | |
| 1612 | if (!nr_irqs) |
| 1613 | err = -ENOSPC; |
| 1614 | |
| 1615 | if (err) |
| 1616 | goto out; |
| 1617 | |
| 1618 | bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC); |
| 1619 | if (!bitmap) |
| 1620 | goto out; |
| 1621 | |
| 1622 | *nr_ids = nr_irqs; |
| 1623 | |
| 1624 | out: |
| 1625 | if (!bitmap) |
| 1626 | *base = *nr_ids = 0; |
| 1627 | |
| 1628 | return bitmap; |
| 1629 | } |
| 1630 | |
| 1631 | static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids) |
| 1632 | { |
| 1633 | WARN_ON(free_lpi_range(base, nr_ids)); |
| 1634 | kfree(bitmap); |
| 1635 | } |
| 1636 | |
| 1637 | static struct page *its_allocate_prop_table(gfp_t gfp_flags) |
| 1638 | { |
| 1639 | struct page *prop_page; |
| 1640 | |
| 1641 | prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ)); |
| 1642 | if (!prop_page) |
| 1643 | return NULL; |
| 1644 | |
| 1645 | /* Priority 0xa0, Group-1, disabled */ |
| 1646 | memset(page_address(prop_page), |
| 1647 | LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, |
| 1648 | LPI_PROPBASE_SZ); |
| 1649 | |
| 1650 | /* Make sure the GIC will observe the written configuration */ |
| 1651 | gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ); |
| 1652 | |
| 1653 | return prop_page; |
| 1654 | } |
| 1655 | |
| 1656 | static void its_free_prop_table(struct page *prop_page) |
| 1657 | { |
| 1658 | free_pages((unsigned long)page_address(prop_page), |
| 1659 | get_order(LPI_PROPBASE_SZ)); |
| 1660 | } |
| 1661 | |
| 1662 | static int __init its_alloc_lpi_tables(void) |
| 1663 | { |
| 1664 | phys_addr_t paddr; |
| 1665 | |
| 1666 | lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gic_rdists->gicd_typer), |
| 1667 | ITS_MAX_LPI_NRBITS); |
| 1668 | gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT); |
| 1669 | if (!gic_rdists->prop_page) { |
| 1670 | pr_err("Failed to allocate PROPBASE\n"); |
| 1671 | return -ENOMEM; |
| 1672 | } |
| 1673 | |
| 1674 | paddr = page_to_phys(gic_rdists->prop_page); |
| 1675 | pr_info("GIC: using LPI property table @%pa\n", &paddr); |
| 1676 | |
| 1677 | return its_lpi_init(lpi_id_bits); |
| 1678 | } |
| 1679 | |
| 1680 | static const char *its_base_type_string[] = { |
| 1681 | [GITS_BASER_TYPE_DEVICE] = "Devices", |
| 1682 | [GITS_BASER_TYPE_VCPU] = "Virtual CPUs", |
| 1683 | [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)", |
| 1684 | [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections", |
| 1685 | [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)", |
| 1686 | [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)", |
| 1687 | [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)", |
| 1688 | }; |
| 1689 | |
| 1690 | static u64 its_read_baser(struct its_node *its, struct its_baser *baser) |
| 1691 | { |
| 1692 | u32 idx = baser - its->tables; |
| 1693 | |
| 1694 | return gits_read_baser(its->base + GITS_BASER + (idx << 3)); |
| 1695 | } |
| 1696 | |
| 1697 | static void its_write_baser(struct its_node *its, struct its_baser *baser, |
| 1698 | u64 val) |
| 1699 | { |
| 1700 | u32 idx = baser - its->tables; |
| 1701 | |
| 1702 | gits_write_baser(val, its->base + GITS_BASER + (idx << 3)); |
| 1703 | baser->val = its_read_baser(its, baser); |
| 1704 | } |
| 1705 | |
| 1706 | static int its_setup_baser(struct its_node *its, struct its_baser *baser, |
| 1707 | u64 cache, u64 shr, u32 psz, u32 order, |
| 1708 | bool indirect) |
| 1709 | { |
| 1710 | u64 val = its_read_baser(its, baser); |
| 1711 | u64 esz = GITS_BASER_ENTRY_SIZE(val); |
| 1712 | u64 type = GITS_BASER_TYPE(val); |
| 1713 | u64 baser_phys, tmp; |
| 1714 | u32 alloc_pages; |
| 1715 | void *base; |
| 1716 | |
| 1717 | retry_alloc_baser: |
| 1718 | alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz); |
| 1719 | if (alloc_pages > GITS_BASER_PAGES_MAX) { |
| 1720 | pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n", |
| 1721 | &its->phys_base, its_base_type_string[type], |
| 1722 | alloc_pages, GITS_BASER_PAGES_MAX); |
| 1723 | alloc_pages = GITS_BASER_PAGES_MAX; |
| 1724 | order = get_order(GITS_BASER_PAGES_MAX * psz); |
| 1725 | } |
| 1726 | |
| 1727 | base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 1728 | if (!base) |
| 1729 | return -ENOMEM; |
| 1730 | |
| 1731 | baser_phys = virt_to_phys(base); |
| 1732 | |
| 1733 | /* Check if the physical address of the memory is above 48bits */ |
| 1734 | if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) { |
| 1735 | |
| 1736 | /* 52bit PA is supported only when PageSize=64K */ |
| 1737 | if (psz != SZ_64K) { |
| 1738 | pr_err("ITS: no 52bit PA support when psz=%d\n", psz); |
| 1739 | free_pages((unsigned long)base, order); |
| 1740 | return -ENXIO; |
| 1741 | } |
| 1742 | |
| 1743 | /* Convert 52bit PA to 48bit field */ |
| 1744 | baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys); |
| 1745 | } |
| 1746 | |
| 1747 | retry_baser: |
| 1748 | val = (baser_phys | |
| 1749 | (type << GITS_BASER_TYPE_SHIFT) | |
| 1750 | ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | |
| 1751 | ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) | |
| 1752 | cache | |
| 1753 | shr | |
| 1754 | GITS_BASER_VALID); |
| 1755 | |
| 1756 | val |= indirect ? GITS_BASER_INDIRECT : 0x0; |
| 1757 | |
| 1758 | switch (psz) { |
| 1759 | case SZ_4K: |
| 1760 | val |= GITS_BASER_PAGE_SIZE_4K; |
| 1761 | break; |
| 1762 | case SZ_16K: |
| 1763 | val |= GITS_BASER_PAGE_SIZE_16K; |
| 1764 | break; |
| 1765 | case SZ_64K: |
| 1766 | val |= GITS_BASER_PAGE_SIZE_64K; |
| 1767 | break; |
| 1768 | } |
| 1769 | |
| 1770 | its_write_baser(its, baser, val); |
| 1771 | tmp = baser->val; |
| 1772 | |
| 1773 | if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) { |
| 1774 | /* |
| 1775 | * Shareability didn't stick. Just use |
| 1776 | * whatever the read reported, which is likely |
| 1777 | * to be the only thing this redistributor |
| 1778 | * supports. If that's zero, make it |
| 1779 | * non-cacheable as well. |
| 1780 | */ |
| 1781 | shr = tmp & GITS_BASER_SHAREABILITY_MASK; |
| 1782 | if (!shr) { |
| 1783 | cache = GITS_BASER_nC; |
| 1784 | gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order)); |
| 1785 | } |
| 1786 | goto retry_baser; |
| 1787 | } |
| 1788 | |
| 1789 | if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) { |
| 1790 | /* |
| 1791 | * Page size didn't stick. Let's try a smaller |
| 1792 | * size and retry. If we reach 4K, then |
| 1793 | * something is horribly wrong... |
| 1794 | */ |
| 1795 | free_pages((unsigned long)base, order); |
| 1796 | baser->base = NULL; |
| 1797 | |
| 1798 | switch (psz) { |
| 1799 | case SZ_16K: |
| 1800 | psz = SZ_4K; |
| 1801 | goto retry_alloc_baser; |
| 1802 | case SZ_64K: |
| 1803 | psz = SZ_16K; |
| 1804 | goto retry_alloc_baser; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | if (val != tmp) { |
| 1809 | pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n", |
| 1810 | &its->phys_base, its_base_type_string[type], |
| 1811 | val, tmp); |
| 1812 | free_pages((unsigned long)base, order); |
| 1813 | return -ENXIO; |
| 1814 | } |
| 1815 | |
| 1816 | baser->order = order; |
| 1817 | baser->base = base; |
| 1818 | baser->psz = psz; |
| 1819 | tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz; |
| 1820 | |
| 1821 | pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n", |
| 1822 | &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp), |
| 1823 | its_base_type_string[type], |
| 1824 | (unsigned long)virt_to_phys(base), |
| 1825 | indirect ? "indirect" : "flat", (int)esz, |
| 1826 | psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT); |
| 1827 | |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | static bool its_parse_indirect_baser(struct its_node *its, |
| 1832 | struct its_baser *baser, |
| 1833 | u32 psz, u32 *order, u32 ids) |
| 1834 | { |
| 1835 | u64 tmp = its_read_baser(its, baser); |
| 1836 | u64 type = GITS_BASER_TYPE(tmp); |
| 1837 | u64 esz = GITS_BASER_ENTRY_SIZE(tmp); |
| 1838 | u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb; |
| 1839 | u32 new_order = *order; |
| 1840 | bool indirect = false; |
| 1841 | |
| 1842 | /* No need to enable Indirection if memory requirement < (psz*2)bytes */ |
| 1843 | if ((esz << ids) > (psz * 2)) { |
| 1844 | /* |
| 1845 | * Find out whether hw supports a single or two-level table by |
| 1846 | * table by reading bit at offset '62' after writing '1' to it. |
| 1847 | */ |
| 1848 | its_write_baser(its, baser, val | GITS_BASER_INDIRECT); |
| 1849 | indirect = !!(baser->val & GITS_BASER_INDIRECT); |
| 1850 | |
| 1851 | if (indirect) { |
| 1852 | /* |
| 1853 | * The size of the lvl2 table is equal to ITS page size |
| 1854 | * which is 'psz'. For computing lvl1 table size, |
| 1855 | * subtract ID bits that sparse lvl2 table from 'ids' |
| 1856 | * which is reported by ITS hardware times lvl1 table |
| 1857 | * entry size. |
| 1858 | */ |
| 1859 | ids -= ilog2(psz / (int)esz); |
| 1860 | esz = GITS_LVL1_ENTRY_SIZE; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* |
| 1865 | * Allocate as many entries as required to fit the |
| 1866 | * range of device IDs that the ITS can grok... The ID |
| 1867 | * space being incredibly sparse, this results in a |
| 1868 | * massive waste of memory if two-level device table |
| 1869 | * feature is not supported by hardware. |
| 1870 | */ |
| 1871 | new_order = max_t(u32, get_order(esz << ids), new_order); |
| 1872 | if (new_order >= MAX_ORDER) { |
| 1873 | new_order = MAX_ORDER - 1; |
| 1874 | ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz); |
| 1875 | pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n", |
| 1876 | &its->phys_base, its_base_type_string[type], |
| 1877 | its->device_ids, ids); |
| 1878 | } |
| 1879 | |
| 1880 | *order = new_order; |
| 1881 | |
| 1882 | return indirect; |
| 1883 | } |
| 1884 | |
| 1885 | static void its_free_tables(struct its_node *its) |
| 1886 | { |
| 1887 | int i; |
| 1888 | |
| 1889 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 1890 | if (its->tables[i].base) { |
| 1891 | free_pages((unsigned long)its->tables[i].base, |
| 1892 | its->tables[i].order); |
| 1893 | its->tables[i].base = NULL; |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | static int its_alloc_tables(struct its_node *its) |
| 1899 | { |
| 1900 | u64 shr = GITS_BASER_InnerShareable; |
| 1901 | u64 cache = GITS_BASER_RaWaWb; |
| 1902 | u32 psz = SZ_64K; |
| 1903 | int err, i; |
| 1904 | |
| 1905 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) |
| 1906 | /* erratum 24313: ignore memory access type */ |
| 1907 | cache = GITS_BASER_nCnB; |
| 1908 | |
| 1909 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 1910 | struct its_baser *baser = its->tables + i; |
| 1911 | u64 val = its_read_baser(its, baser); |
| 1912 | u64 type = GITS_BASER_TYPE(val); |
| 1913 | u32 order = get_order(psz); |
| 1914 | bool indirect = false; |
| 1915 | |
| 1916 | switch (type) { |
| 1917 | case GITS_BASER_TYPE_NONE: |
| 1918 | continue; |
| 1919 | |
| 1920 | case GITS_BASER_TYPE_DEVICE: |
| 1921 | indirect = its_parse_indirect_baser(its, baser, |
| 1922 | psz, &order, |
| 1923 | its->device_ids); |
| 1924 | break; |
| 1925 | |
| 1926 | case GITS_BASER_TYPE_VCPU: |
| 1927 | indirect = its_parse_indirect_baser(its, baser, |
| 1928 | psz, &order, |
| 1929 | ITS_MAX_VPEID_BITS); |
| 1930 | break; |
| 1931 | } |
| 1932 | |
| 1933 | err = its_setup_baser(its, baser, cache, shr, psz, order, indirect); |
| 1934 | if (err < 0) { |
| 1935 | its_free_tables(its); |
| 1936 | return err; |
| 1937 | } |
| 1938 | |
| 1939 | /* Update settings which will be used for next BASERn */ |
| 1940 | psz = baser->psz; |
| 1941 | cache = baser->val & GITS_BASER_CACHEABILITY_MASK; |
| 1942 | shr = baser->val & GITS_BASER_SHAREABILITY_MASK; |
| 1943 | } |
| 1944 | |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
| 1948 | static int its_alloc_collections(struct its_node *its) |
| 1949 | { |
| 1950 | int i; |
| 1951 | |
| 1952 | its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections), |
| 1953 | GFP_KERNEL); |
| 1954 | if (!its->collections) |
| 1955 | return -ENOMEM; |
| 1956 | |
| 1957 | for (i = 0; i < nr_cpu_ids; i++) |
| 1958 | its->collections[i].target_address = ~0ULL; |
| 1959 | |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | static struct page *its_allocate_pending_table(gfp_t gfp_flags) |
| 1964 | { |
| 1965 | struct page *pend_page; |
| 1966 | /* |
| 1967 | * The pending pages have to be at least 64kB aligned, |
| 1968 | * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below. |
| 1969 | */ |
| 1970 | pend_page = alloc_pages(gfp_flags | __GFP_ZERO, |
| 1971 | get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K))); |
| 1972 | if (!pend_page) |
| 1973 | return NULL; |
| 1974 | |
| 1975 | /* Make sure the GIC will observe the zero-ed page */ |
| 1976 | gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ); |
| 1977 | |
| 1978 | return pend_page; |
| 1979 | } |
| 1980 | |
| 1981 | static void its_free_pending_table(struct page *pt) |
| 1982 | { |
| 1983 | free_pages((unsigned long)page_address(pt), |
| 1984 | get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K))); |
| 1985 | } |
| 1986 | |
| 1987 | static u64 its_clear_vpend_valid(void __iomem *vlpi_base) |
| 1988 | { |
| 1989 | u32 count = 1000000; /* 1s! */ |
| 1990 | bool clean; |
| 1991 | u64 val; |
| 1992 | |
| 1993 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 1994 | val &= ~GICR_VPENDBASER_Valid; |
| 1995 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 1996 | |
| 1997 | do { |
| 1998 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 1999 | clean = !(val & GICR_VPENDBASER_Dirty); |
| 2000 | if (!clean) { |
| 2001 | count--; |
| 2002 | cpu_relax(); |
| 2003 | udelay(1); |
| 2004 | } |
| 2005 | } while (!clean && count); |
| 2006 | |
| 2007 | return val; |
| 2008 | } |
| 2009 | |
| 2010 | static void its_cpu_init_lpis(void) |
| 2011 | { |
| 2012 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 2013 | struct page *pend_page; |
| 2014 | u64 val, tmp; |
| 2015 | |
| 2016 | /* If we didn't allocate the pending table yet, do it now */ |
| 2017 | pend_page = gic_data_rdist()->pend_page; |
| 2018 | if (!pend_page) { |
| 2019 | phys_addr_t paddr; |
| 2020 | |
| 2021 | pend_page = its_allocate_pending_table(GFP_NOWAIT); |
| 2022 | if (!pend_page) { |
| 2023 | pr_err("Failed to allocate PENDBASE for CPU%d\n", |
| 2024 | smp_processor_id()); |
| 2025 | return; |
| 2026 | } |
| 2027 | |
| 2028 | paddr = page_to_phys(pend_page); |
| 2029 | pr_info("CPU%d: using LPI pending table @%pa\n", |
| 2030 | smp_processor_id(), &paddr); |
| 2031 | gic_data_rdist()->pend_page = pend_page; |
| 2032 | } |
| 2033 | |
| 2034 | /* set PROPBASE */ |
| 2035 | val = (page_to_phys(gic_rdists->prop_page) | |
| 2036 | GICR_PROPBASER_InnerShareable | |
| 2037 | GICR_PROPBASER_RaWaWb | |
| 2038 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 2039 | |
| 2040 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
| 2041 | tmp = gicr_read_propbaser(rbase + GICR_PROPBASER); |
| 2042 | |
| 2043 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
| 2044 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 2045 | /* |
| 2046 | * The HW reports non-shareable, we must |
| 2047 | * remove the cacheability attributes as |
| 2048 | * well. |
| 2049 | */ |
| 2050 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 2051 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 2052 | val |= GICR_PROPBASER_nC; |
| 2053 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
| 2054 | } |
| 2055 | pr_info_once("GIC: using cache flushing for LPI property table\n"); |
| 2056 | gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING; |
| 2057 | } |
| 2058 | |
| 2059 | /* set PENDBASE */ |
| 2060 | val = (page_to_phys(pend_page) | |
| 2061 | GICR_PENDBASER_InnerShareable | |
| 2062 | GICR_PENDBASER_RaWaWb); |
| 2063 | |
| 2064 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
| 2065 | tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER); |
| 2066 | |
| 2067 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 2068 | /* |
| 2069 | * The HW reports non-shareable, we must remove the |
| 2070 | * cacheability attributes as well. |
| 2071 | */ |
| 2072 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 2073 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 2074 | val |= GICR_PENDBASER_nC; |
| 2075 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
| 2076 | } |
| 2077 | |
| 2078 | /* Enable LPIs */ |
| 2079 | val = readl_relaxed(rbase + GICR_CTLR); |
| 2080 | val |= GICR_CTLR_ENABLE_LPIS; |
| 2081 | writel_relaxed(val, rbase + GICR_CTLR); |
| 2082 | |
| 2083 | if (gic_rdists->has_vlpis) { |
| 2084 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 2085 | |
| 2086 | /* |
| 2087 | * It's possible for CPU to receive VLPIs before it is |
| 2088 | * sheduled as a vPE, especially for the first CPU, and the |
| 2089 | * VLPI with INTID larger than 2^(IDbits+1) will be considered |
| 2090 | * as out of range and dropped by GIC. |
| 2091 | * So we initialize IDbits to known value to avoid VLPI drop. |
| 2092 | */ |
| 2093 | val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 2094 | pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n", |
| 2095 | smp_processor_id(), val); |
| 2096 | gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
| 2097 | |
| 2098 | /* |
| 2099 | * Also clear Valid bit of GICR_VPENDBASER, in case some |
| 2100 | * ancient programming gets left in and has possibility of |
| 2101 | * corrupting memory. |
| 2102 | */ |
| 2103 | val = its_clear_vpend_valid(vlpi_base); |
| 2104 | WARN_ON(val & GICR_VPENDBASER_Dirty); |
| 2105 | } |
| 2106 | |
| 2107 | /* Make sure the GIC has seen the above */ |
| 2108 | dsb(sy); |
| 2109 | } |
| 2110 | |
| 2111 | static void its_cpu_init_collection(struct its_node *its) |
| 2112 | { |
| 2113 | int cpu = smp_processor_id(); |
| 2114 | u64 target; |
| 2115 | |
| 2116 | /* avoid cross node collections and its mapping */ |
| 2117 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 2118 | struct device_node *cpu_node; |
| 2119 | |
| 2120 | cpu_node = of_get_cpu_node(cpu, NULL); |
| 2121 | if (its->numa_node != NUMA_NO_NODE && |
| 2122 | its->numa_node != of_node_to_nid(cpu_node)) |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * We now have to bind each collection to its target |
| 2128 | * redistributor. |
| 2129 | */ |
| 2130 | if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) { |
| 2131 | /* |
| 2132 | * This ITS wants the physical address of the |
| 2133 | * redistributor. |
| 2134 | */ |
| 2135 | target = gic_data_rdist()->phys_base; |
| 2136 | } else { |
| 2137 | /* This ITS wants a linear CPU number. */ |
| 2138 | target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); |
| 2139 | target = GICR_TYPER_CPU_NUMBER(target) << 16; |
| 2140 | } |
| 2141 | |
| 2142 | /* Perform collection mapping */ |
| 2143 | its->collections[cpu].target_address = target; |
| 2144 | its->collections[cpu].col_id = cpu; |
| 2145 | |
| 2146 | its_send_mapc(its, &its->collections[cpu], 1); |
| 2147 | its_send_invall(its, &its->collections[cpu]); |
| 2148 | } |
| 2149 | |
| 2150 | static void its_cpu_init_collections(void) |
| 2151 | { |
| 2152 | struct its_node *its; |
| 2153 | |
| 2154 | raw_spin_lock(&its_lock); |
| 2155 | |
| 2156 | list_for_each_entry(its, &its_nodes, entry) |
| 2157 | its_cpu_init_collection(its); |
| 2158 | |
| 2159 | raw_spin_unlock(&its_lock); |
| 2160 | } |
| 2161 | |
| 2162 | static struct its_device *its_find_device(struct its_node *its, u32 dev_id) |
| 2163 | { |
| 2164 | struct its_device *its_dev = NULL, *tmp; |
| 2165 | unsigned long flags; |
| 2166 | |
| 2167 | raw_spin_lock_irqsave(&its->lock, flags); |
| 2168 | |
| 2169 | list_for_each_entry(tmp, &its->its_device_list, entry) { |
| 2170 | if (tmp->device_id == dev_id) { |
| 2171 | its_dev = tmp; |
| 2172 | break; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | raw_spin_unlock_irqrestore(&its->lock, flags); |
| 2177 | |
| 2178 | return its_dev; |
| 2179 | } |
| 2180 | |
| 2181 | static struct its_baser *its_get_baser(struct its_node *its, u32 type) |
| 2182 | { |
| 2183 | int i; |
| 2184 | |
| 2185 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 2186 | if (GITS_BASER_TYPE(its->tables[i].val) == type) |
| 2187 | return &its->tables[i]; |
| 2188 | } |
| 2189 | |
| 2190 | return NULL; |
| 2191 | } |
| 2192 | |
| 2193 | static bool its_alloc_table_entry(struct its_baser *baser, u32 id) |
| 2194 | { |
| 2195 | struct page *page; |
| 2196 | u32 esz, idx; |
| 2197 | __le64 *table; |
| 2198 | |
| 2199 | /* Don't allow device id that exceeds single, flat table limit */ |
| 2200 | esz = GITS_BASER_ENTRY_SIZE(baser->val); |
| 2201 | if (!(baser->val & GITS_BASER_INDIRECT)) |
| 2202 | return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz)); |
| 2203 | |
| 2204 | /* Compute 1st level table index & check if that exceeds table limit */ |
| 2205 | idx = id >> ilog2(baser->psz / esz); |
| 2206 | if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE)) |
| 2207 | return false; |
| 2208 | |
| 2209 | table = baser->base; |
| 2210 | |
| 2211 | /* Allocate memory for 2nd level table */ |
| 2212 | if (!table[idx]) { |
| 2213 | page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz)); |
| 2214 | if (!page) |
| 2215 | return false; |
| 2216 | |
| 2217 | /* Flush Lvl2 table to PoC if hw doesn't support coherency */ |
| 2218 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
| 2219 | gic_flush_dcache_to_poc(page_address(page), baser->psz); |
| 2220 | |
| 2221 | table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); |
| 2222 | |
| 2223 | /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ |
| 2224 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
| 2225 | gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); |
| 2226 | |
| 2227 | /* Ensure updated table contents are visible to ITS hardware */ |
| 2228 | dsb(sy); |
| 2229 | } |
| 2230 | |
| 2231 | return true; |
| 2232 | } |
| 2233 | |
| 2234 | static bool its_alloc_device_table(struct its_node *its, u32 dev_id) |
| 2235 | { |
| 2236 | struct its_baser *baser; |
| 2237 | |
| 2238 | baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE); |
| 2239 | |
| 2240 | /* Don't allow device id that exceeds ITS hardware limit */ |
| 2241 | if (!baser) |
| 2242 | return (ilog2(dev_id) < its->device_ids); |
| 2243 | |
| 2244 | return its_alloc_table_entry(baser, dev_id); |
| 2245 | } |
| 2246 | |
| 2247 | static bool its_alloc_vpe_table(u32 vpe_id) |
| 2248 | { |
| 2249 | struct its_node *its; |
| 2250 | |
| 2251 | /* |
| 2252 | * Make sure the L2 tables are allocated on *all* v4 ITSs. We |
| 2253 | * could try and only do it on ITSs corresponding to devices |
| 2254 | * that have interrupts targeted at this VPE, but the |
| 2255 | * complexity becomes crazy (and you have tons of memory |
| 2256 | * anyway, right?). |
| 2257 | */ |
| 2258 | list_for_each_entry(its, &its_nodes, entry) { |
| 2259 | struct its_baser *baser; |
| 2260 | |
| 2261 | if (!its->is_v4) |
| 2262 | continue; |
| 2263 | |
| 2264 | baser = its_get_baser(its, GITS_BASER_TYPE_VCPU); |
| 2265 | if (!baser) |
| 2266 | return false; |
| 2267 | |
| 2268 | if (!its_alloc_table_entry(baser, vpe_id)) |
| 2269 | return false; |
| 2270 | } |
| 2271 | |
| 2272 | return true; |
| 2273 | } |
| 2274 | |
| 2275 | static struct its_device *its_create_device(struct its_node *its, u32 dev_id, |
| 2276 | int nvecs, bool alloc_lpis) |
| 2277 | { |
| 2278 | struct its_device *dev; |
| 2279 | unsigned long *lpi_map = NULL; |
| 2280 | unsigned long flags; |
| 2281 | u16 *col_map = NULL; |
| 2282 | void *itt; |
| 2283 | int lpi_base; |
| 2284 | int nr_lpis; |
| 2285 | int nr_ites; |
| 2286 | int sz; |
| 2287 | |
| 2288 | if (!its_alloc_device_table(its, dev_id)) |
| 2289 | return NULL; |
| 2290 | |
| 2291 | if (WARN_ON(!is_power_of_2(nvecs))) |
| 2292 | nvecs = roundup_pow_of_two(nvecs); |
| 2293 | |
| 2294 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 2295 | /* |
| 2296 | * Even if the device wants a single LPI, the ITT must be |
| 2297 | * sized as a power of two (and you need at least one bit...). |
| 2298 | */ |
| 2299 | nr_ites = max(2, nvecs); |
| 2300 | sz = nr_ites * its->ite_size; |
| 2301 | sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1; |
| 2302 | itt = kzalloc(sz, GFP_KERNEL); |
| 2303 | if (alloc_lpis) { |
| 2304 | lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis); |
| 2305 | if (lpi_map) |
| 2306 | col_map = kcalloc(nr_lpis, sizeof(*col_map), |
| 2307 | GFP_KERNEL); |
| 2308 | } else { |
| 2309 | col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL); |
| 2310 | nr_lpis = 0; |
| 2311 | lpi_base = 0; |
| 2312 | } |
| 2313 | |
| 2314 | if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) { |
| 2315 | kfree(dev); |
| 2316 | kfree(itt); |
| 2317 | kfree(lpi_map); |
| 2318 | kfree(col_map); |
| 2319 | return NULL; |
| 2320 | } |
| 2321 | |
| 2322 | gic_flush_dcache_to_poc(itt, sz); |
| 2323 | |
| 2324 | dev->its = its; |
| 2325 | dev->itt = itt; |
| 2326 | dev->nr_ites = nr_ites; |
| 2327 | dev->event_map.lpi_map = lpi_map; |
| 2328 | dev->event_map.col_map = col_map; |
| 2329 | dev->event_map.lpi_base = lpi_base; |
| 2330 | dev->event_map.nr_lpis = nr_lpis; |
| 2331 | mutex_init(&dev->event_map.vlpi_lock); |
| 2332 | dev->device_id = dev_id; |
| 2333 | INIT_LIST_HEAD(&dev->entry); |
| 2334 | |
| 2335 | raw_spin_lock_irqsave(&its->lock, flags); |
| 2336 | list_add(&dev->entry, &its->its_device_list); |
| 2337 | raw_spin_unlock_irqrestore(&its->lock, flags); |
| 2338 | |
| 2339 | /* Map device to its ITT */ |
| 2340 | its_send_mapd(dev, 1); |
| 2341 | |
| 2342 | return dev; |
| 2343 | } |
| 2344 | |
| 2345 | static void its_free_device(struct its_device *its_dev) |
| 2346 | { |
| 2347 | unsigned long flags; |
| 2348 | |
| 2349 | raw_spin_lock_irqsave(&its_dev->its->lock, flags); |
| 2350 | list_del(&its_dev->entry); |
| 2351 | raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); |
| 2352 | kfree(its_dev->itt); |
| 2353 | kfree(its_dev); |
| 2354 | } |
| 2355 | |
| 2356 | static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq) |
| 2357 | { |
| 2358 | int idx; |
| 2359 | |
| 2360 | idx = bitmap_find_free_region(dev->event_map.lpi_map, |
| 2361 | dev->event_map.nr_lpis, |
| 2362 | get_count_order(nvecs)); |
| 2363 | if (idx < 0) |
| 2364 | return -ENOSPC; |
| 2365 | |
| 2366 | *hwirq = dev->event_map.lpi_base + idx; |
| 2367 | set_bit(idx, dev->event_map.lpi_map); |
| 2368 | |
| 2369 | return 0; |
| 2370 | } |
| 2371 | |
| 2372 | static int its_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 2373 | int nvec, msi_alloc_info_t *info) |
| 2374 | { |
| 2375 | struct its_node *its; |
| 2376 | struct its_device *its_dev; |
| 2377 | struct msi_domain_info *msi_info; |
| 2378 | u32 dev_id; |
| 2379 | int err = 0; |
| 2380 | |
| 2381 | /* |
| 2382 | * We ignore "dev" entierely, and rely on the dev_id that has |
| 2383 | * been passed via the scratchpad. This limits this domain's |
| 2384 | * usefulness to upper layers that definitely know that they |
| 2385 | * are built on top of the ITS. |
| 2386 | */ |
| 2387 | dev_id = info->scratchpad[0].ul; |
| 2388 | |
| 2389 | msi_info = msi_get_domain_info(domain); |
| 2390 | its = msi_info->data; |
| 2391 | |
| 2392 | if (!gic_rdists->has_direct_lpi && |
| 2393 | vpe_proxy.dev && |
| 2394 | vpe_proxy.dev->its == its && |
| 2395 | dev_id == vpe_proxy.dev->device_id) { |
| 2396 | /* Bad luck. Get yourself a better implementation */ |
| 2397 | WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n", |
| 2398 | dev_id); |
| 2399 | return -EINVAL; |
| 2400 | } |
| 2401 | |
| 2402 | mutex_lock(&its->dev_alloc_lock); |
| 2403 | its_dev = its_find_device(its, dev_id); |
| 2404 | if (its_dev) { |
| 2405 | /* |
| 2406 | * We already have seen this ID, probably through |
| 2407 | * another alias (PCI bridge of some sort). No need to |
| 2408 | * create the device. |
| 2409 | */ |
| 2410 | its_dev->shared = true; |
| 2411 | pr_debug("Reusing ITT for devID %x\n", dev_id); |
| 2412 | goto out; |
| 2413 | } |
| 2414 | |
| 2415 | its_dev = its_create_device(its, dev_id, nvec, true); |
| 2416 | if (!its_dev) { |
| 2417 | err = -ENOMEM; |
| 2418 | goto out; |
| 2419 | } |
| 2420 | |
| 2421 | pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); |
| 2422 | out: |
| 2423 | mutex_unlock(&its->dev_alloc_lock); |
| 2424 | info->scratchpad[0].ptr = its_dev; |
| 2425 | return err; |
| 2426 | } |
| 2427 | |
| 2428 | static struct msi_domain_ops its_msi_domain_ops = { |
| 2429 | .msi_prepare = its_msi_prepare, |
| 2430 | }; |
| 2431 | |
| 2432 | static int its_irq_gic_domain_alloc(struct irq_domain *domain, |
| 2433 | unsigned int virq, |
| 2434 | irq_hw_number_t hwirq) |
| 2435 | { |
| 2436 | struct irq_fwspec fwspec; |
| 2437 | |
| 2438 | if (irq_domain_get_of_node(domain->parent)) { |
| 2439 | fwspec.fwnode = domain->parent->fwnode; |
| 2440 | fwspec.param_count = 3; |
| 2441 | fwspec.param[0] = GIC_IRQ_TYPE_LPI; |
| 2442 | fwspec.param[1] = hwirq; |
| 2443 | fwspec.param[2] = IRQ_TYPE_EDGE_RISING; |
| 2444 | } else if (is_fwnode_irqchip(domain->parent->fwnode)) { |
| 2445 | fwspec.fwnode = domain->parent->fwnode; |
| 2446 | fwspec.param_count = 2; |
| 2447 | fwspec.param[0] = hwirq; |
| 2448 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
| 2449 | } else { |
| 2450 | return -EINVAL; |
| 2451 | } |
| 2452 | |
| 2453 | return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
| 2454 | } |
| 2455 | |
| 2456 | static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 2457 | unsigned int nr_irqs, void *args) |
| 2458 | { |
| 2459 | msi_alloc_info_t *info = args; |
| 2460 | struct its_device *its_dev = info->scratchpad[0].ptr; |
| 2461 | irq_hw_number_t hwirq; |
| 2462 | int err; |
| 2463 | int i; |
| 2464 | |
| 2465 | err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq); |
| 2466 | if (err) |
| 2467 | return err; |
| 2468 | |
| 2469 | for (i = 0; i < nr_irqs; i++) { |
| 2470 | err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i); |
| 2471 | if (err) |
| 2472 | return err; |
| 2473 | |
| 2474 | irq_domain_set_hwirq_and_chip(domain, virq + i, |
| 2475 | hwirq + i, &its_irq_chip, its_dev); |
| 2476 | irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i))); |
| 2477 | pr_debug("ID:%d pID:%d vID:%d\n", |
| 2478 | (int)(hwirq + i - its_dev->event_map.lpi_base), |
| 2479 | (int)(hwirq + i), virq + i); |
| 2480 | } |
| 2481 | |
| 2482 | return 0; |
| 2483 | } |
| 2484 | |
| 2485 | static int its_irq_domain_activate(struct irq_domain *domain, |
| 2486 | struct irq_data *d, bool reserve) |
| 2487 | { |
| 2488 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2489 | u32 event = its_get_event_id(d); |
| 2490 | const struct cpumask *cpu_mask = cpu_online_mask; |
| 2491 | int cpu; |
| 2492 | |
| 2493 | /* get the cpu_mask of local node */ |
| 2494 | if (its_dev->its->numa_node >= 0) |
| 2495 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
| 2496 | |
| 2497 | /* Bind the LPI to the first possible CPU */ |
| 2498 | cpu = cpumask_first_and(cpu_mask, cpu_online_mask); |
| 2499 | if (cpu >= nr_cpu_ids) { |
| 2500 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) |
| 2501 | return -EINVAL; |
| 2502 | |
| 2503 | cpu = cpumask_first(cpu_online_mask); |
| 2504 | } |
| 2505 | |
| 2506 | its_dev->event_map.col_map[event] = cpu; |
| 2507 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
| 2508 | |
| 2509 | /* Map the GIC IRQ and event to the device */ |
| 2510 | its_send_mapti(its_dev, d->hwirq, event); |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
| 2514 | static void its_irq_domain_deactivate(struct irq_domain *domain, |
| 2515 | struct irq_data *d) |
| 2516 | { |
| 2517 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2518 | u32 event = its_get_event_id(d); |
| 2519 | |
| 2520 | /* Stop the delivery of interrupts */ |
| 2521 | its_send_discard(its_dev, event); |
| 2522 | } |
| 2523 | |
| 2524 | static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 2525 | unsigned int nr_irqs) |
| 2526 | { |
| 2527 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 2528 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2529 | struct its_node *its = its_dev->its; |
| 2530 | int i; |
| 2531 | |
| 2532 | bitmap_release_region(its_dev->event_map.lpi_map, |
| 2533 | its_get_event_id(irq_domain_get_irq_data(domain, virq)), |
| 2534 | get_count_order(nr_irqs)); |
| 2535 | |
| 2536 | for (i = 0; i < nr_irqs; i++) { |
| 2537 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 2538 | virq + i); |
| 2539 | /* Nuke the entry in the domain */ |
| 2540 | irq_domain_reset_irq_data(data); |
| 2541 | } |
| 2542 | |
| 2543 | mutex_lock(&its->dev_alloc_lock); |
| 2544 | |
| 2545 | /* |
| 2546 | * If all interrupts have been freed, start mopping the |
| 2547 | * floor. This is conditionned on the device not being shared. |
| 2548 | */ |
| 2549 | if (!its_dev->shared && |
| 2550 | bitmap_empty(its_dev->event_map.lpi_map, |
| 2551 | its_dev->event_map.nr_lpis)) { |
| 2552 | its_lpi_free(its_dev->event_map.lpi_map, |
| 2553 | its_dev->event_map.lpi_base, |
| 2554 | its_dev->event_map.nr_lpis); |
| 2555 | kfree(its_dev->event_map.col_map); |
| 2556 | |
| 2557 | /* Unmap device/itt */ |
| 2558 | its_send_mapd(its_dev, 0); |
| 2559 | its_free_device(its_dev); |
| 2560 | } |
| 2561 | |
| 2562 | mutex_unlock(&its->dev_alloc_lock); |
| 2563 | |
| 2564 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 2565 | } |
| 2566 | |
| 2567 | static const struct irq_domain_ops its_domain_ops = { |
| 2568 | .alloc = its_irq_domain_alloc, |
| 2569 | .free = its_irq_domain_free, |
| 2570 | .activate = its_irq_domain_activate, |
| 2571 | .deactivate = its_irq_domain_deactivate, |
| 2572 | }; |
| 2573 | |
| 2574 | /* |
| 2575 | * This is insane. |
| 2576 | * |
| 2577 | * If a GICv4 doesn't implement Direct LPIs (which is extremely |
| 2578 | * likely), the only way to perform an invalidate is to use a fake |
| 2579 | * device to issue an INV command, implying that the LPI has first |
| 2580 | * been mapped to some event on that device. Since this is not exactly |
| 2581 | * cheap, we try to keep that mapping around as long as possible, and |
| 2582 | * only issue an UNMAP if we're short on available slots. |
| 2583 | * |
| 2584 | * Broken by design(tm). |
| 2585 | */ |
| 2586 | static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe) |
| 2587 | { |
| 2588 | /* Already unmapped? */ |
| 2589 | if (vpe->vpe_proxy_event == -1) |
| 2590 | return; |
| 2591 | |
| 2592 | its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event); |
| 2593 | vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL; |
| 2594 | |
| 2595 | /* |
| 2596 | * We don't track empty slots at all, so let's move the |
| 2597 | * next_victim pointer if we can quickly reuse that slot |
| 2598 | * instead of nuking an existing entry. Not clear that this is |
| 2599 | * always a win though, and this might just generate a ripple |
| 2600 | * effect... Let's just hope VPEs don't migrate too often. |
| 2601 | */ |
| 2602 | if (vpe_proxy.vpes[vpe_proxy.next_victim]) |
| 2603 | vpe_proxy.next_victim = vpe->vpe_proxy_event; |
| 2604 | |
| 2605 | vpe->vpe_proxy_event = -1; |
| 2606 | } |
| 2607 | |
| 2608 | static void its_vpe_db_proxy_unmap(struct its_vpe *vpe) |
| 2609 | { |
| 2610 | if (!gic_rdists->has_direct_lpi) { |
| 2611 | unsigned long flags; |
| 2612 | |
| 2613 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 2614 | its_vpe_db_proxy_unmap_locked(vpe); |
| 2615 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe) |
| 2620 | { |
| 2621 | /* Already mapped? */ |
| 2622 | if (vpe->vpe_proxy_event != -1) |
| 2623 | return; |
| 2624 | |
| 2625 | /* This slot was already allocated. Kick the other VPE out. */ |
| 2626 | if (vpe_proxy.vpes[vpe_proxy.next_victim]) |
| 2627 | its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]); |
| 2628 | |
| 2629 | /* Map the new VPE instead */ |
| 2630 | vpe_proxy.vpes[vpe_proxy.next_victim] = vpe; |
| 2631 | vpe->vpe_proxy_event = vpe_proxy.next_victim; |
| 2632 | vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites; |
| 2633 | |
| 2634 | vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx; |
| 2635 | its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event); |
| 2636 | } |
| 2637 | |
| 2638 | static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to) |
| 2639 | { |
| 2640 | unsigned long flags; |
| 2641 | struct its_collection *target_col; |
| 2642 | |
| 2643 | if (gic_rdists->has_direct_lpi) { |
| 2644 | void __iomem *rdbase; |
| 2645 | |
| 2646 | rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base; |
| 2647 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); |
| 2648 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 2649 | cpu_relax(); |
| 2650 | |
| 2651 | return; |
| 2652 | } |
| 2653 | |
| 2654 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 2655 | |
| 2656 | its_vpe_db_proxy_map_locked(vpe); |
| 2657 | |
| 2658 | target_col = &vpe_proxy.dev->its->collections[to]; |
| 2659 | its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event); |
| 2660 | vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to; |
| 2661 | |
| 2662 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 2663 | } |
| 2664 | |
| 2665 | static int its_vpe_set_affinity(struct irq_data *d, |
| 2666 | const struct cpumask *mask_val, |
| 2667 | bool force) |
| 2668 | { |
| 2669 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2670 | int cpu = cpumask_first(mask_val); |
| 2671 | |
| 2672 | /* |
| 2673 | * Changing affinity is mega expensive, so let's be as lazy as |
| 2674 | * we can and only do it if we really have to. Also, if mapped |
| 2675 | * into the proxy device, we need to move the doorbell |
| 2676 | * interrupt to its new location. |
| 2677 | */ |
| 2678 | if (vpe->col_idx != cpu) { |
| 2679 | int from = vpe->col_idx; |
| 2680 | |
| 2681 | vpe->col_idx = cpu; |
| 2682 | its_send_vmovp(vpe); |
| 2683 | its_vpe_db_proxy_move(vpe, from, cpu); |
| 2684 | } |
| 2685 | |
| 2686 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
| 2687 | |
| 2688 | return IRQ_SET_MASK_OK_DONE; |
| 2689 | } |
| 2690 | |
| 2691 | static void its_vpe_schedule(struct its_vpe *vpe) |
| 2692 | { |
| 2693 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 2694 | u64 val; |
| 2695 | |
| 2696 | /* Schedule the VPE */ |
| 2697 | val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) & |
| 2698 | GENMASK_ULL(51, 12); |
| 2699 | val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 2700 | val |= GICR_VPROPBASER_RaWb; |
| 2701 | val |= GICR_VPROPBASER_InnerShareable; |
| 2702 | gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
| 2703 | |
| 2704 | val = virt_to_phys(page_address(vpe->vpt_page)) & |
| 2705 | GENMASK_ULL(51, 16); |
| 2706 | val |= GICR_VPENDBASER_RaWaWb; |
| 2707 | val |= GICR_VPENDBASER_NonShareable; |
| 2708 | /* |
| 2709 | * There is no good way of finding out if the pending table is |
| 2710 | * empty as we can race against the doorbell interrupt very |
| 2711 | * easily. So in the end, vpe->pending_last is only an |
| 2712 | * indication that the vcpu has something pending, not one |
| 2713 | * that the pending table is empty. A good implementation |
| 2714 | * would be able to read its coarse map pretty quickly anyway, |
| 2715 | * making this a tolerable issue. |
| 2716 | */ |
| 2717 | val |= GICR_VPENDBASER_PendingLast; |
| 2718 | val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0; |
| 2719 | val |= GICR_VPENDBASER_Valid; |
| 2720 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 2721 | } |
| 2722 | |
| 2723 | static void its_vpe_deschedule(struct its_vpe *vpe) |
| 2724 | { |
| 2725 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 2726 | u64 val; |
| 2727 | |
| 2728 | val = its_clear_vpend_valid(vlpi_base); |
| 2729 | |
| 2730 | if (unlikely(val & GICR_VPENDBASER_Dirty)) { |
| 2731 | pr_err_ratelimited("ITS virtual pending table not cleaning\n"); |
| 2732 | vpe->idai = false; |
| 2733 | vpe->pending_last = true; |
| 2734 | } else { |
| 2735 | vpe->idai = !!(val & GICR_VPENDBASER_IDAI); |
| 2736 | vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | static void its_vpe_invall(struct its_vpe *vpe) |
| 2741 | { |
| 2742 | struct its_node *its; |
| 2743 | |
| 2744 | list_for_each_entry(its, &its_nodes, entry) { |
| 2745 | if (!its->is_v4) |
| 2746 | continue; |
| 2747 | |
| 2748 | if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr]) |
| 2749 | continue; |
| 2750 | |
| 2751 | /* |
| 2752 | * Sending a VINVALL to a single ITS is enough, as all |
| 2753 | * we need is to reach the redistributors. |
| 2754 | */ |
| 2755 | its_send_vinvall(its, vpe); |
| 2756 | return; |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 2761 | { |
| 2762 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2763 | struct its_cmd_info *info = vcpu_info; |
| 2764 | |
| 2765 | switch (info->cmd_type) { |
| 2766 | case SCHEDULE_VPE: |
| 2767 | its_vpe_schedule(vpe); |
| 2768 | return 0; |
| 2769 | |
| 2770 | case DESCHEDULE_VPE: |
| 2771 | its_vpe_deschedule(vpe); |
| 2772 | return 0; |
| 2773 | |
| 2774 | case INVALL_VPE: |
| 2775 | its_vpe_invall(vpe); |
| 2776 | return 0; |
| 2777 | |
| 2778 | default: |
| 2779 | return -EINVAL; |
| 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | static void its_vpe_send_cmd(struct its_vpe *vpe, |
| 2784 | void (*cmd)(struct its_device *, u32)) |
| 2785 | { |
| 2786 | unsigned long flags; |
| 2787 | |
| 2788 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 2789 | |
| 2790 | its_vpe_db_proxy_map_locked(vpe); |
| 2791 | cmd(vpe_proxy.dev, vpe->vpe_proxy_event); |
| 2792 | |
| 2793 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 2794 | } |
| 2795 | |
| 2796 | static void its_vpe_send_inv(struct irq_data *d) |
| 2797 | { |
| 2798 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2799 | |
| 2800 | if (gic_rdists->has_direct_lpi) { |
| 2801 | void __iomem *rdbase; |
| 2802 | |
| 2803 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
| 2804 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR); |
| 2805 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 2806 | cpu_relax(); |
| 2807 | } else { |
| 2808 | its_vpe_send_cmd(vpe, its_send_inv); |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | static void its_vpe_mask_irq(struct irq_data *d) |
| 2813 | { |
| 2814 | /* |
| 2815 | * We need to unmask the LPI, which is described by the parent |
| 2816 | * irq_data. Instead of calling into the parent (which won't |
| 2817 | * exactly do the right thing, let's simply use the |
| 2818 | * parent_data pointer. Yes, I'm naughty. |
| 2819 | */ |
| 2820 | lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0); |
| 2821 | its_vpe_send_inv(d); |
| 2822 | } |
| 2823 | |
| 2824 | static void its_vpe_unmask_irq(struct irq_data *d) |
| 2825 | { |
| 2826 | /* Same hack as above... */ |
| 2827 | lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED); |
| 2828 | its_vpe_send_inv(d); |
| 2829 | } |
| 2830 | |
| 2831 | static int its_vpe_set_irqchip_state(struct irq_data *d, |
| 2832 | enum irqchip_irq_state which, |
| 2833 | bool state) |
| 2834 | { |
| 2835 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2836 | |
| 2837 | if (which != IRQCHIP_STATE_PENDING) |
| 2838 | return -EINVAL; |
| 2839 | |
| 2840 | if (gic_rdists->has_direct_lpi) { |
| 2841 | void __iomem *rdbase; |
| 2842 | |
| 2843 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
| 2844 | if (state) { |
| 2845 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR); |
| 2846 | } else { |
| 2847 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); |
| 2848 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 2849 | cpu_relax(); |
| 2850 | } |
| 2851 | } else { |
| 2852 | if (state) |
| 2853 | its_vpe_send_cmd(vpe, its_send_int); |
| 2854 | else |
| 2855 | its_vpe_send_cmd(vpe, its_send_clear); |
| 2856 | } |
| 2857 | |
| 2858 | return 0; |
| 2859 | } |
| 2860 | |
| 2861 | static struct irq_chip its_vpe_irq_chip = { |
| 2862 | .name = "GICv4-vpe", |
| 2863 | .irq_mask = its_vpe_mask_irq, |
| 2864 | .irq_unmask = its_vpe_unmask_irq, |
| 2865 | .irq_eoi = irq_chip_eoi_parent, |
| 2866 | .irq_set_affinity = its_vpe_set_affinity, |
| 2867 | .irq_set_irqchip_state = its_vpe_set_irqchip_state, |
| 2868 | .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity, |
| 2869 | }; |
| 2870 | |
| 2871 | static int its_vpe_id_alloc(void) |
| 2872 | { |
| 2873 | return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL); |
| 2874 | } |
| 2875 | |
| 2876 | static void its_vpe_id_free(u16 id) |
| 2877 | { |
| 2878 | ida_simple_remove(&its_vpeid_ida, id); |
| 2879 | } |
| 2880 | |
| 2881 | static int its_vpe_init(struct its_vpe *vpe) |
| 2882 | { |
| 2883 | struct page *vpt_page; |
| 2884 | int vpe_id; |
| 2885 | |
| 2886 | /* Allocate vpe_id */ |
| 2887 | vpe_id = its_vpe_id_alloc(); |
| 2888 | if (vpe_id < 0) |
| 2889 | return vpe_id; |
| 2890 | |
| 2891 | /* Allocate VPT */ |
| 2892 | vpt_page = its_allocate_pending_table(GFP_KERNEL); |
| 2893 | if (!vpt_page) { |
| 2894 | its_vpe_id_free(vpe_id); |
| 2895 | return -ENOMEM; |
| 2896 | } |
| 2897 | |
| 2898 | if (!its_alloc_vpe_table(vpe_id)) { |
| 2899 | its_vpe_id_free(vpe_id); |
| 2900 | its_free_pending_table(vpt_page); |
| 2901 | return -ENOMEM; |
| 2902 | } |
| 2903 | |
| 2904 | vpe->vpe_id = vpe_id; |
| 2905 | vpe->vpt_page = vpt_page; |
| 2906 | vpe->vpe_proxy_event = -1; |
| 2907 | |
| 2908 | return 0; |
| 2909 | } |
| 2910 | |
| 2911 | static void its_vpe_teardown(struct its_vpe *vpe) |
| 2912 | { |
| 2913 | its_vpe_db_proxy_unmap(vpe); |
| 2914 | its_vpe_id_free(vpe->vpe_id); |
| 2915 | its_free_pending_table(vpe->vpt_page); |
| 2916 | } |
| 2917 | |
| 2918 | static void its_vpe_irq_domain_free(struct irq_domain *domain, |
| 2919 | unsigned int virq, |
| 2920 | unsigned int nr_irqs) |
| 2921 | { |
| 2922 | struct its_vm *vm = domain->host_data; |
| 2923 | int i; |
| 2924 | |
| 2925 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 2926 | |
| 2927 | for (i = 0; i < nr_irqs; i++) { |
| 2928 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 2929 | virq + i); |
| 2930 | struct its_vpe *vpe = irq_data_get_irq_chip_data(data); |
| 2931 | |
| 2932 | BUG_ON(vm != vpe->its_vm); |
| 2933 | |
| 2934 | clear_bit(data->hwirq, vm->db_bitmap); |
| 2935 | its_vpe_teardown(vpe); |
| 2936 | irq_domain_reset_irq_data(data); |
| 2937 | } |
| 2938 | |
| 2939 | if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) { |
| 2940 | its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis); |
| 2941 | its_free_prop_table(vm->vprop_page); |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 2946 | unsigned int nr_irqs, void *args) |
| 2947 | { |
| 2948 | struct its_vm *vm = args; |
| 2949 | unsigned long *bitmap; |
| 2950 | struct page *vprop_page; |
| 2951 | int base, nr_ids, i, err = 0; |
| 2952 | |
| 2953 | BUG_ON(!vm); |
| 2954 | |
| 2955 | bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids); |
| 2956 | if (!bitmap) |
| 2957 | return -ENOMEM; |
| 2958 | |
| 2959 | if (nr_ids < nr_irqs) { |
| 2960 | its_lpi_free(bitmap, base, nr_ids); |
| 2961 | return -ENOMEM; |
| 2962 | } |
| 2963 | |
| 2964 | vprop_page = its_allocate_prop_table(GFP_KERNEL); |
| 2965 | if (!vprop_page) { |
| 2966 | its_lpi_free(bitmap, base, nr_ids); |
| 2967 | return -ENOMEM; |
| 2968 | } |
| 2969 | |
| 2970 | vm->db_bitmap = bitmap; |
| 2971 | vm->db_lpi_base = base; |
| 2972 | vm->nr_db_lpis = nr_ids; |
| 2973 | vm->vprop_page = vprop_page; |
| 2974 | |
| 2975 | for (i = 0; i < nr_irqs; i++) { |
| 2976 | vm->vpes[i]->vpe_db_lpi = base + i; |
| 2977 | err = its_vpe_init(vm->vpes[i]); |
| 2978 | if (err) |
| 2979 | break; |
| 2980 | err = its_irq_gic_domain_alloc(domain, virq + i, |
| 2981 | vm->vpes[i]->vpe_db_lpi); |
| 2982 | if (err) |
| 2983 | break; |
| 2984 | irq_domain_set_hwirq_and_chip(domain, virq + i, i, |
| 2985 | &its_vpe_irq_chip, vm->vpes[i]); |
| 2986 | set_bit(i, bitmap); |
| 2987 | } |
| 2988 | |
| 2989 | if (err) { |
| 2990 | if (i > 0) |
| 2991 | its_vpe_irq_domain_free(domain, virq, i - 1); |
| 2992 | |
| 2993 | its_lpi_free(bitmap, base, nr_ids); |
| 2994 | its_free_prop_table(vprop_page); |
| 2995 | } |
| 2996 | |
| 2997 | return err; |
| 2998 | } |
| 2999 | |
| 3000 | static int its_vpe_irq_domain_activate(struct irq_domain *domain, |
| 3001 | struct irq_data *d, bool reserve) |
| 3002 | { |
| 3003 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 3004 | struct its_node *its; |
| 3005 | |
| 3006 | /* If we use the list map, we issue VMAPP on demand... */ |
| 3007 | if (its_list_map) |
| 3008 | return 0; |
| 3009 | |
| 3010 | /* Map the VPE to the first possible CPU */ |
| 3011 | vpe->col_idx = cpumask_first(cpu_online_mask); |
| 3012 | |
| 3013 | list_for_each_entry(its, &its_nodes, entry) { |
| 3014 | if (!its->is_v4) |
| 3015 | continue; |
| 3016 | |
| 3017 | its_send_vmapp(its, vpe, true); |
| 3018 | its_send_vinvall(its, vpe); |
| 3019 | } |
| 3020 | |
| 3021 | irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); |
| 3022 | |
| 3023 | return 0; |
| 3024 | } |
| 3025 | |
| 3026 | static void its_vpe_irq_domain_deactivate(struct irq_domain *domain, |
| 3027 | struct irq_data *d) |
| 3028 | { |
| 3029 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 3030 | struct its_node *its; |
| 3031 | |
| 3032 | /* |
| 3033 | * If we use the list map, we unmap the VPE once no VLPIs are |
| 3034 | * associated with the VM. |
| 3035 | */ |
| 3036 | if (its_list_map) |
| 3037 | return; |
| 3038 | |
| 3039 | list_for_each_entry(its, &its_nodes, entry) { |
| 3040 | if (!its->is_v4) |
| 3041 | continue; |
| 3042 | |
| 3043 | its_send_vmapp(its, vpe, false); |
| 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | static const struct irq_domain_ops its_vpe_domain_ops = { |
| 3048 | .alloc = its_vpe_irq_domain_alloc, |
| 3049 | .free = its_vpe_irq_domain_free, |
| 3050 | .activate = its_vpe_irq_domain_activate, |
| 3051 | .deactivate = its_vpe_irq_domain_deactivate, |
| 3052 | }; |
| 3053 | |
| 3054 | static int its_force_quiescent(void __iomem *base) |
| 3055 | { |
| 3056 | u32 count = 1000000; /* 1s */ |
| 3057 | u32 val; |
| 3058 | |
| 3059 | val = readl_relaxed(base + GITS_CTLR); |
| 3060 | /* |
| 3061 | * GIC architecture specification requires the ITS to be both |
| 3062 | * disabled and quiescent for writes to GITS_BASER<n> or |
| 3063 | * GITS_CBASER to not have UNPREDICTABLE results. |
| 3064 | */ |
| 3065 | if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE)) |
| 3066 | return 0; |
| 3067 | |
| 3068 | /* Disable the generation of all interrupts to this ITS */ |
| 3069 | val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe); |
| 3070 | writel_relaxed(val, base + GITS_CTLR); |
| 3071 | |
| 3072 | /* Poll GITS_CTLR and wait until ITS becomes quiescent */ |
| 3073 | while (1) { |
| 3074 | val = readl_relaxed(base + GITS_CTLR); |
| 3075 | if (val & GITS_CTLR_QUIESCENT) |
| 3076 | return 0; |
| 3077 | |
| 3078 | count--; |
| 3079 | if (!count) |
| 3080 | return -EBUSY; |
| 3081 | |
| 3082 | cpu_relax(); |
| 3083 | udelay(1); |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | static bool __maybe_unused its_enable_quirk_cavium_22375(void *data) |
| 3088 | { |
| 3089 | struct its_node *its = data; |
| 3090 | |
| 3091 | /* erratum 22375: only alloc 8MB table size */ |
| 3092 | its->device_ids = 0x14; /* 20 bits, 8MB */ |
| 3093 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375; |
| 3094 | |
| 3095 | return true; |
| 3096 | } |
| 3097 | |
| 3098 | static bool __maybe_unused its_enable_quirk_cavium_23144(void *data) |
| 3099 | { |
| 3100 | struct its_node *its = data; |
| 3101 | |
| 3102 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144; |
| 3103 | |
| 3104 | return true; |
| 3105 | } |
| 3106 | |
| 3107 | static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data) |
| 3108 | { |
| 3109 | struct its_node *its = data; |
| 3110 | |
| 3111 | /* On QDF2400, the size of the ITE is 16Bytes */ |
| 3112 | its->ite_size = 16; |
| 3113 | |
| 3114 | return true; |
| 3115 | } |
| 3116 | |
| 3117 | static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev) |
| 3118 | { |
| 3119 | struct its_node *its = its_dev->its; |
| 3120 | |
| 3121 | /* |
| 3122 | * The Socionext Synquacer SoC has a so-called 'pre-ITS', |
| 3123 | * which maps 32-bit writes targeted at a separate window of |
| 3124 | * size '4 << device_id_bits' onto writes to GITS_TRANSLATER |
| 3125 | * with device ID taken from bits [device_id_bits + 1:2] of |
| 3126 | * the window offset. |
| 3127 | */ |
| 3128 | return its->pre_its_base + (its_dev->device_id << 2); |
| 3129 | } |
| 3130 | |
| 3131 | static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data) |
| 3132 | { |
| 3133 | struct its_node *its = data; |
| 3134 | u32 pre_its_window[2]; |
| 3135 | u32 ids; |
| 3136 | |
| 3137 | if (!fwnode_property_read_u32_array(its->fwnode_handle, |
| 3138 | "socionext,synquacer-pre-its", |
| 3139 | pre_its_window, |
| 3140 | ARRAY_SIZE(pre_its_window))) { |
| 3141 | |
| 3142 | its->pre_its_base = pre_its_window[0]; |
| 3143 | its->get_msi_base = its_irq_get_msi_base_pre_its; |
| 3144 | |
| 3145 | ids = ilog2(pre_its_window[1]) - 2; |
| 3146 | if (its->device_ids > ids) |
| 3147 | its->device_ids = ids; |
| 3148 | |
| 3149 | /* the pre-ITS breaks isolation, so disable MSI remapping */ |
| 3150 | its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP; |
| 3151 | return true; |
| 3152 | } |
| 3153 | return false; |
| 3154 | } |
| 3155 | |
| 3156 | static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data) |
| 3157 | { |
| 3158 | struct its_node *its = data; |
| 3159 | |
| 3160 | /* |
| 3161 | * Hip07 insists on using the wrong address for the VLPI |
| 3162 | * page. Trick it into doing the right thing... |
| 3163 | */ |
| 3164 | its->vlpi_redist_offset = SZ_128K; |
| 3165 | return true; |
| 3166 | } |
| 3167 | |
| 3168 | static const struct gic_quirk its_quirks[] = { |
| 3169 | #ifdef CONFIG_CAVIUM_ERRATUM_22375 |
| 3170 | { |
| 3171 | .desc = "ITS: Cavium errata 22375, 24313", |
| 3172 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 3173 | .mask = 0xffff0fff, |
| 3174 | .init = its_enable_quirk_cavium_22375, |
| 3175 | }, |
| 3176 | #endif |
| 3177 | #ifdef CONFIG_CAVIUM_ERRATUM_23144 |
| 3178 | { |
| 3179 | .desc = "ITS: Cavium erratum 23144", |
| 3180 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 3181 | .mask = 0xffff0fff, |
| 3182 | .init = its_enable_quirk_cavium_23144, |
| 3183 | }, |
| 3184 | #endif |
| 3185 | #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065 |
| 3186 | { |
| 3187 | .desc = "ITS: QDF2400 erratum 0065", |
| 3188 | .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */ |
| 3189 | .mask = 0xffffffff, |
| 3190 | .init = its_enable_quirk_qdf2400_e0065, |
| 3191 | }, |
| 3192 | #endif |
| 3193 | #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS |
| 3194 | { |
| 3195 | /* |
| 3196 | * The Socionext Synquacer SoC incorporates ARM's own GIC-500 |
| 3197 | * implementation, but with a 'pre-ITS' added that requires |
| 3198 | * special handling in software. |
| 3199 | */ |
| 3200 | .desc = "ITS: Socionext Synquacer pre-ITS", |
| 3201 | .iidr = 0x0001143b, |
| 3202 | .mask = 0xffffffff, |
| 3203 | .init = its_enable_quirk_socionext_synquacer, |
| 3204 | }, |
| 3205 | #endif |
| 3206 | #ifdef CONFIG_HISILICON_ERRATUM_161600802 |
| 3207 | { |
| 3208 | .desc = "ITS: Hip07 erratum 161600802", |
| 3209 | .iidr = 0x00000004, |
| 3210 | .mask = 0xffffffff, |
| 3211 | .init = its_enable_quirk_hip07_161600802, |
| 3212 | }, |
| 3213 | #endif |
| 3214 | { |
| 3215 | } |
| 3216 | }; |
| 3217 | |
| 3218 | static void its_enable_quirks(struct its_node *its) |
| 3219 | { |
| 3220 | u32 iidr = readl_relaxed(its->base + GITS_IIDR); |
| 3221 | |
| 3222 | gic_enable_quirks(iidr, its_quirks, its); |
| 3223 | } |
| 3224 | |
| 3225 | static int its_save_disable(void) |
| 3226 | { |
| 3227 | struct its_node *its; |
| 3228 | int err = 0; |
| 3229 | |
| 3230 | raw_spin_lock(&its_lock); |
| 3231 | list_for_each_entry(its, &its_nodes, entry) { |
| 3232 | void __iomem *base; |
| 3233 | |
| 3234 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 3235 | continue; |
| 3236 | |
| 3237 | base = its->base; |
| 3238 | its->ctlr_save = readl_relaxed(base + GITS_CTLR); |
| 3239 | err = its_force_quiescent(base); |
| 3240 | if (err) { |
| 3241 | pr_err("ITS@%pa: failed to quiesce: %d\n", |
| 3242 | &its->phys_base, err); |
| 3243 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
| 3244 | goto err; |
| 3245 | } |
| 3246 | |
| 3247 | its->cbaser_save = gits_read_cbaser(base + GITS_CBASER); |
| 3248 | } |
| 3249 | |
| 3250 | err: |
| 3251 | if (err) { |
| 3252 | list_for_each_entry_continue_reverse(its, &its_nodes, entry) { |
| 3253 | void __iomem *base; |
| 3254 | |
| 3255 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 3256 | continue; |
| 3257 | |
| 3258 | base = its->base; |
| 3259 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
| 3260 | } |
| 3261 | } |
| 3262 | raw_spin_unlock(&its_lock); |
| 3263 | |
| 3264 | return err; |
| 3265 | } |
| 3266 | |
| 3267 | static void its_restore_enable(void) |
| 3268 | { |
| 3269 | struct its_node *its; |
| 3270 | int ret; |
| 3271 | |
| 3272 | raw_spin_lock(&its_lock); |
| 3273 | list_for_each_entry(its, &its_nodes, entry) { |
| 3274 | void __iomem *base; |
| 3275 | int i; |
| 3276 | |
| 3277 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 3278 | continue; |
| 3279 | |
| 3280 | base = its->base; |
| 3281 | |
| 3282 | /* |
| 3283 | * Make sure that the ITS is disabled. If it fails to quiesce, |
| 3284 | * don't restore it since writing to CBASER or BASER<n> |
| 3285 | * registers is undefined according to the GIC v3 ITS |
| 3286 | * Specification. |
| 3287 | */ |
| 3288 | ret = its_force_quiescent(base); |
| 3289 | if (ret) { |
| 3290 | pr_err("ITS@%pa: failed to quiesce on resume: %d\n", |
| 3291 | &its->phys_base, ret); |
| 3292 | continue; |
| 3293 | } |
| 3294 | |
| 3295 | gits_write_cbaser(its->cbaser_save, base + GITS_CBASER); |
| 3296 | |
| 3297 | /* |
| 3298 | * Writing CBASER resets CREADR to 0, so make CWRITER and |
| 3299 | * cmd_write line up with it. |
| 3300 | */ |
| 3301 | its->cmd_write = its->cmd_base; |
| 3302 | gits_write_cwriter(0, base + GITS_CWRITER); |
| 3303 | |
| 3304 | /* Restore GITS_BASER from the value cache. */ |
| 3305 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 3306 | struct its_baser *baser = &its->tables[i]; |
| 3307 | |
| 3308 | if (!(baser->val & GITS_BASER_VALID)) |
| 3309 | continue; |
| 3310 | |
| 3311 | its_write_baser(its, baser, baser->val); |
| 3312 | } |
| 3313 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
| 3314 | |
| 3315 | /* |
| 3316 | * Reinit the collection if it's stored in the ITS. This is |
| 3317 | * indicated by the col_id being less than the HCC field. |
| 3318 | * CID < HCC as specified in the GIC v3 Documentation. |
| 3319 | */ |
| 3320 | if (its->collections[smp_processor_id()].col_id < |
| 3321 | GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER))) |
| 3322 | its_cpu_init_collection(its); |
| 3323 | } |
| 3324 | raw_spin_unlock(&its_lock); |
| 3325 | } |
| 3326 | |
| 3327 | static struct syscore_ops its_syscore_ops = { |
| 3328 | .suspend = its_save_disable, |
| 3329 | .resume = its_restore_enable, |
| 3330 | }; |
| 3331 | |
| 3332 | static int its_init_domain(struct fwnode_handle *handle, struct its_node *its) |
| 3333 | { |
| 3334 | struct irq_domain *inner_domain; |
| 3335 | struct msi_domain_info *info; |
| 3336 | |
| 3337 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 3338 | if (!info) |
| 3339 | return -ENOMEM; |
| 3340 | |
| 3341 | inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its); |
| 3342 | if (!inner_domain) { |
| 3343 | kfree(info); |
| 3344 | return -ENOMEM; |
| 3345 | } |
| 3346 | |
| 3347 | inner_domain->parent = its_parent; |
| 3348 | irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS); |
| 3349 | inner_domain->flags |= its->msi_domain_flags; |
| 3350 | info->ops = &its_msi_domain_ops; |
| 3351 | info->data = its; |
| 3352 | inner_domain->host_data = info; |
| 3353 | |
| 3354 | return 0; |
| 3355 | } |
| 3356 | |
| 3357 | static int its_init_vpe_domain(void) |
| 3358 | { |
| 3359 | struct its_node *its; |
| 3360 | u32 devid; |
| 3361 | int entries; |
| 3362 | |
| 3363 | if (gic_rdists->has_direct_lpi) { |
| 3364 | pr_info("ITS: Using DirectLPI for VPE invalidation\n"); |
| 3365 | return 0; |
| 3366 | } |
| 3367 | |
| 3368 | /* Any ITS will do, even if not v4 */ |
| 3369 | its = list_first_entry(&its_nodes, struct its_node, entry); |
| 3370 | |
| 3371 | entries = roundup_pow_of_two(nr_cpu_ids); |
| 3372 | vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes), |
| 3373 | GFP_KERNEL); |
| 3374 | if (!vpe_proxy.vpes) { |
| 3375 | pr_err("ITS: Can't allocate GICv4 proxy device array\n"); |
| 3376 | return -ENOMEM; |
| 3377 | } |
| 3378 | |
| 3379 | /* Use the last possible DevID */ |
| 3380 | devid = GENMASK(its->device_ids - 1, 0); |
| 3381 | vpe_proxy.dev = its_create_device(its, devid, entries, false); |
| 3382 | if (!vpe_proxy.dev) { |
| 3383 | kfree(vpe_proxy.vpes); |
| 3384 | pr_err("ITS: Can't allocate GICv4 proxy device\n"); |
| 3385 | return -ENOMEM; |
| 3386 | } |
| 3387 | |
| 3388 | BUG_ON(entries > vpe_proxy.dev->nr_ites); |
| 3389 | |
| 3390 | raw_spin_lock_init(&vpe_proxy.lock); |
| 3391 | vpe_proxy.next_victim = 0; |
| 3392 | pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n", |
| 3393 | devid, vpe_proxy.dev->nr_ites); |
| 3394 | |
| 3395 | return 0; |
| 3396 | } |
| 3397 | |
| 3398 | static int __init its_compute_its_list_map(struct resource *res, |
| 3399 | void __iomem *its_base) |
| 3400 | { |
| 3401 | int its_number; |
| 3402 | u32 ctlr; |
| 3403 | |
| 3404 | /* |
| 3405 | * This is assumed to be done early enough that we're |
| 3406 | * guaranteed to be single-threaded, hence no |
| 3407 | * locking. Should this change, we should address |
| 3408 | * this. |
| 3409 | */ |
| 3410 | its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX); |
| 3411 | if (its_number >= GICv4_ITS_LIST_MAX) { |
| 3412 | pr_err("ITS@%pa: No ITSList entry available!\n", |
| 3413 | &res->start); |
| 3414 | return -EINVAL; |
| 3415 | } |
| 3416 | |
| 3417 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 3418 | ctlr &= ~GITS_CTLR_ITS_NUMBER; |
| 3419 | ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT; |
| 3420 | writel_relaxed(ctlr, its_base + GITS_CTLR); |
| 3421 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 3422 | if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) { |
| 3423 | its_number = ctlr & GITS_CTLR_ITS_NUMBER; |
| 3424 | its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT; |
| 3425 | } |
| 3426 | |
| 3427 | if (test_and_set_bit(its_number, &its_list_map)) { |
| 3428 | pr_err("ITS@%pa: Duplicate ITSList entry %d\n", |
| 3429 | &res->start, its_number); |
| 3430 | return -EINVAL; |
| 3431 | } |
| 3432 | |
| 3433 | return its_number; |
| 3434 | } |
| 3435 | |
| 3436 | static int __init its_probe_one(struct resource *res, |
| 3437 | struct fwnode_handle *handle, int numa_node) |
| 3438 | { |
| 3439 | struct its_node *its; |
| 3440 | void __iomem *its_base; |
| 3441 | u32 val, ctlr; |
| 3442 | u64 baser, tmp, typer; |
| 3443 | int err; |
| 3444 | |
| 3445 | its_base = ioremap(res->start, resource_size(res)); |
| 3446 | if (!its_base) { |
| 3447 | pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start); |
| 3448 | return -ENOMEM; |
| 3449 | } |
| 3450 | |
| 3451 | val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 3452 | if (val != 0x30 && val != 0x40) { |
| 3453 | pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start); |
| 3454 | err = -ENODEV; |
| 3455 | goto out_unmap; |
| 3456 | } |
| 3457 | |
| 3458 | err = its_force_quiescent(its_base); |
| 3459 | if (err) { |
| 3460 | pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start); |
| 3461 | goto out_unmap; |
| 3462 | } |
| 3463 | |
| 3464 | pr_info("ITS %pR\n", res); |
| 3465 | |
| 3466 | its = kzalloc(sizeof(*its), GFP_KERNEL); |
| 3467 | if (!its) { |
| 3468 | err = -ENOMEM; |
| 3469 | goto out_unmap; |
| 3470 | } |
| 3471 | |
| 3472 | raw_spin_lock_init(&its->lock); |
| 3473 | mutex_init(&its->dev_alloc_lock); |
| 3474 | INIT_LIST_HEAD(&its->entry); |
| 3475 | INIT_LIST_HEAD(&its->its_device_list); |
| 3476 | typer = gic_read_typer(its_base + GITS_TYPER); |
| 3477 | its->base = its_base; |
| 3478 | its->phys_base = res->start; |
| 3479 | its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer); |
| 3480 | its->device_ids = GITS_TYPER_DEVBITS(typer); |
| 3481 | its->is_v4 = !!(typer & GITS_TYPER_VLPIS); |
| 3482 | if (its->is_v4) { |
| 3483 | if (!(typer & GITS_TYPER_VMOVP)) { |
| 3484 | err = its_compute_its_list_map(res, its_base); |
| 3485 | if (err < 0) |
| 3486 | goto out_free_its; |
| 3487 | |
| 3488 | its->list_nr = err; |
| 3489 | |
| 3490 | pr_info("ITS@%pa: Using ITS number %d\n", |
| 3491 | &res->start, err); |
| 3492 | } else { |
| 3493 | pr_info("ITS@%pa: Single VMOVP capable\n", &res->start); |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | its->numa_node = numa_node; |
| 3498 | |
| 3499 | its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 3500 | get_order(ITS_CMD_QUEUE_SZ)); |
| 3501 | if (!its->cmd_base) { |
| 3502 | err = -ENOMEM; |
| 3503 | goto out_free_its; |
| 3504 | } |
| 3505 | its->cmd_write = its->cmd_base; |
| 3506 | its->fwnode_handle = handle; |
| 3507 | its->get_msi_base = its_irq_get_msi_base; |
| 3508 | its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP; |
| 3509 | |
| 3510 | its_enable_quirks(its); |
| 3511 | |
| 3512 | err = its_alloc_tables(its); |
| 3513 | if (err) |
| 3514 | goto out_free_cmd; |
| 3515 | |
| 3516 | err = its_alloc_collections(its); |
| 3517 | if (err) |
| 3518 | goto out_free_tables; |
| 3519 | |
| 3520 | baser = (virt_to_phys(its->cmd_base) | |
| 3521 | GITS_CBASER_RaWaWb | |
| 3522 | GITS_CBASER_InnerShareable | |
| 3523 | (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | |
| 3524 | GITS_CBASER_VALID); |
| 3525 | |
| 3526 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
| 3527 | tmp = gits_read_cbaser(its->base + GITS_CBASER); |
| 3528 | |
| 3529 | if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { |
| 3530 | if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { |
| 3531 | /* |
| 3532 | * The HW reports non-shareable, we must |
| 3533 | * remove the cacheability attributes as |
| 3534 | * well. |
| 3535 | */ |
| 3536 | baser &= ~(GITS_CBASER_SHAREABILITY_MASK | |
| 3537 | GITS_CBASER_CACHEABILITY_MASK); |
| 3538 | baser |= GITS_CBASER_nC; |
| 3539 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
| 3540 | } |
| 3541 | pr_info("ITS: using cache flushing for cmd queue\n"); |
| 3542 | its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; |
| 3543 | } |
| 3544 | |
| 3545 | gits_write_cwriter(0, its->base + GITS_CWRITER); |
| 3546 | ctlr = readl_relaxed(its->base + GITS_CTLR); |
| 3547 | ctlr |= GITS_CTLR_ENABLE; |
| 3548 | if (its->is_v4) |
| 3549 | ctlr |= GITS_CTLR_ImDe; |
| 3550 | writel_relaxed(ctlr, its->base + GITS_CTLR); |
| 3551 | |
| 3552 | if (GITS_TYPER_HCC(typer)) |
| 3553 | its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE; |
| 3554 | |
| 3555 | err = its_init_domain(handle, its); |
| 3556 | if (err) |
| 3557 | goto out_free_tables; |
| 3558 | |
| 3559 | raw_spin_lock(&its_lock); |
| 3560 | list_add(&its->entry, &its_nodes); |
| 3561 | raw_spin_unlock(&its_lock); |
| 3562 | |
| 3563 | return 0; |
| 3564 | |
| 3565 | out_free_tables: |
| 3566 | its_free_tables(its); |
| 3567 | out_free_cmd: |
| 3568 | free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ)); |
| 3569 | out_free_its: |
| 3570 | kfree(its); |
| 3571 | out_unmap: |
| 3572 | iounmap(its_base); |
| 3573 | pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err); |
| 3574 | return err; |
| 3575 | } |
| 3576 | |
| 3577 | static bool gic_rdists_supports_plpis(void) |
| 3578 | { |
| 3579 | return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS); |
| 3580 | } |
| 3581 | |
| 3582 | static int redist_disable_lpis(void) |
| 3583 | { |
| 3584 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 3585 | u64 timeout = USEC_PER_SEC; |
| 3586 | u64 val; |
| 3587 | |
| 3588 | /* |
| 3589 | * If coming via a CPU hotplug event, we don't need to disable |
| 3590 | * LPIs before trying to re-enable them. They are already |
| 3591 | * configured and all is well in the world. Detect this case |
| 3592 | * by checking the allocation of the pending table for the |
| 3593 | * current CPU. |
| 3594 | */ |
| 3595 | if (gic_data_rdist()->pend_page) |
| 3596 | return 0; |
| 3597 | |
| 3598 | if (!gic_rdists_supports_plpis()) { |
| 3599 | pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); |
| 3600 | return -ENXIO; |
| 3601 | } |
| 3602 | |
| 3603 | val = readl_relaxed(rbase + GICR_CTLR); |
| 3604 | if (!(val & GICR_CTLR_ENABLE_LPIS)) |
| 3605 | return 0; |
| 3606 | |
| 3607 | pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n", |
| 3608 | smp_processor_id()); |
| 3609 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
| 3610 | |
| 3611 | /* Disable LPIs */ |
| 3612 | val &= ~GICR_CTLR_ENABLE_LPIS; |
| 3613 | writel_relaxed(val, rbase + GICR_CTLR); |
| 3614 | |
| 3615 | /* Make sure any change to GICR_CTLR is observable by the GIC */ |
| 3616 | dsb(sy); |
| 3617 | |
| 3618 | /* |
| 3619 | * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs |
| 3620 | * from 1 to 0 before programming GICR_PEND{PROP}BASER registers. |
| 3621 | * Error out if we time out waiting for RWP to clear. |
| 3622 | */ |
| 3623 | while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) { |
| 3624 | if (!timeout) { |
| 3625 | pr_err("CPU%d: Timeout while disabling LPIs\n", |
| 3626 | smp_processor_id()); |
| 3627 | return -ETIMEDOUT; |
| 3628 | } |
| 3629 | udelay(1); |
| 3630 | timeout--; |
| 3631 | } |
| 3632 | |
| 3633 | /* |
| 3634 | * After it has been written to 1, it is IMPLEMENTATION |
| 3635 | * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be |
| 3636 | * cleared to 0. Error out if clearing the bit failed. |
| 3637 | */ |
| 3638 | if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) { |
| 3639 | pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id()); |
| 3640 | return -EBUSY; |
| 3641 | } |
| 3642 | |
| 3643 | return 0; |
| 3644 | } |
| 3645 | |
| 3646 | int its_cpu_init(void) |
| 3647 | { |
| 3648 | if (!list_empty(&its_nodes)) { |
| 3649 | int ret; |
| 3650 | |
| 3651 | ret = redist_disable_lpis(); |
| 3652 | if (ret) |
| 3653 | return ret; |
| 3654 | |
| 3655 | its_cpu_init_lpis(); |
| 3656 | its_cpu_init_collections(); |
| 3657 | } |
| 3658 | |
| 3659 | return 0; |
| 3660 | } |
| 3661 | |
| 3662 | static const struct of_device_id its_device_id[] = { |
| 3663 | { .compatible = "arm,gic-v3-its", }, |
| 3664 | {}, |
| 3665 | }; |
| 3666 | |
| 3667 | static int __init its_of_probe(struct device_node *node) |
| 3668 | { |
| 3669 | struct device_node *np; |
| 3670 | struct resource res; |
| 3671 | |
| 3672 | for (np = of_find_matching_node(node, its_device_id); np; |
| 3673 | np = of_find_matching_node(np, its_device_id)) { |
| 3674 | if (!of_device_is_available(np)) |
| 3675 | continue; |
| 3676 | if (!of_property_read_bool(np, "msi-controller")) { |
| 3677 | pr_warn("%pOF: no msi-controller property, ITS ignored\n", |
| 3678 | np); |
| 3679 | continue; |
| 3680 | } |
| 3681 | |
| 3682 | if (of_address_to_resource(np, 0, &res)) { |
| 3683 | pr_warn("%pOF: no regs?\n", np); |
| 3684 | continue; |
| 3685 | } |
| 3686 | |
| 3687 | its_probe_one(&res, &np->fwnode, of_node_to_nid(np)); |
| 3688 | } |
| 3689 | return 0; |
| 3690 | } |
| 3691 | |
| 3692 | #ifdef CONFIG_ACPI |
| 3693 | |
| 3694 | #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) |
| 3695 | |
| 3696 | #ifdef CONFIG_ACPI_NUMA |
| 3697 | struct its_srat_map { |
| 3698 | /* numa node id */ |
| 3699 | u32 numa_node; |
| 3700 | /* GIC ITS ID */ |
| 3701 | u32 its_id; |
| 3702 | }; |
| 3703 | |
| 3704 | static struct its_srat_map *its_srat_maps __initdata; |
| 3705 | static int its_in_srat __initdata; |
| 3706 | |
| 3707 | static int __init acpi_get_its_numa_node(u32 its_id) |
| 3708 | { |
| 3709 | int i; |
| 3710 | |
| 3711 | for (i = 0; i < its_in_srat; i++) { |
| 3712 | if (its_id == its_srat_maps[i].its_id) |
| 3713 | return its_srat_maps[i].numa_node; |
| 3714 | } |
| 3715 | return NUMA_NO_NODE; |
| 3716 | } |
| 3717 | |
| 3718 | static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header, |
| 3719 | const unsigned long end) |
| 3720 | { |
| 3721 | return 0; |
| 3722 | } |
| 3723 | |
| 3724 | static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header, |
| 3725 | const unsigned long end) |
| 3726 | { |
| 3727 | int node; |
| 3728 | struct acpi_srat_gic_its_affinity *its_affinity; |
| 3729 | |
| 3730 | its_affinity = (struct acpi_srat_gic_its_affinity *)header; |
| 3731 | if (!its_affinity) |
| 3732 | return -EINVAL; |
| 3733 | |
| 3734 | if (its_affinity->header.length < sizeof(*its_affinity)) { |
| 3735 | pr_err("SRAT: Invalid header length %d in ITS affinity\n", |
| 3736 | its_affinity->header.length); |
| 3737 | return -EINVAL; |
| 3738 | } |
| 3739 | |
| 3740 | node = acpi_map_pxm_to_node(its_affinity->proximity_domain); |
| 3741 | |
| 3742 | if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) { |
| 3743 | pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node); |
| 3744 | return 0; |
| 3745 | } |
| 3746 | |
| 3747 | its_srat_maps[its_in_srat].numa_node = node; |
| 3748 | its_srat_maps[its_in_srat].its_id = its_affinity->its_id; |
| 3749 | its_in_srat++; |
| 3750 | pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n", |
| 3751 | its_affinity->proximity_domain, its_affinity->its_id, node); |
| 3752 | |
| 3753 | return 0; |
| 3754 | } |
| 3755 | |
| 3756 | static void __init acpi_table_parse_srat_its(void) |
| 3757 | { |
| 3758 | int count; |
| 3759 | |
| 3760 | count = acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 3761 | sizeof(struct acpi_table_srat), |
| 3762 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 3763 | gic_acpi_match_srat_its, 0); |
| 3764 | if (count <= 0) |
| 3765 | return; |
| 3766 | |
| 3767 | its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map), |
| 3768 | GFP_KERNEL); |
| 3769 | if (!its_srat_maps) { |
| 3770 | pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n"); |
| 3771 | return; |
| 3772 | } |
| 3773 | |
| 3774 | acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 3775 | sizeof(struct acpi_table_srat), |
| 3776 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 3777 | gic_acpi_parse_srat_its, 0); |
| 3778 | } |
| 3779 | |
| 3780 | /* free the its_srat_maps after ITS probing */ |
| 3781 | static void __init acpi_its_srat_maps_free(void) |
| 3782 | { |
| 3783 | kfree(its_srat_maps); |
| 3784 | } |
| 3785 | #else |
| 3786 | static void __init acpi_table_parse_srat_its(void) { } |
| 3787 | static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; } |
| 3788 | static void __init acpi_its_srat_maps_free(void) { } |
| 3789 | #endif |
| 3790 | |
| 3791 | static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header, |
| 3792 | const unsigned long end) |
| 3793 | { |
| 3794 | struct acpi_madt_generic_translator *its_entry; |
| 3795 | struct fwnode_handle *dom_handle; |
| 3796 | struct resource res; |
| 3797 | int err; |
| 3798 | |
| 3799 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 3800 | memset(&res, 0, sizeof(res)); |
| 3801 | res.start = its_entry->base_address; |
| 3802 | res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1; |
| 3803 | res.flags = IORESOURCE_MEM; |
| 3804 | |
| 3805 | dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address); |
| 3806 | if (!dom_handle) { |
| 3807 | pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n", |
| 3808 | &res.start); |
| 3809 | return -ENOMEM; |
| 3810 | } |
| 3811 | |
| 3812 | err = iort_register_domain_token(its_entry->translation_id, res.start, |
| 3813 | dom_handle); |
| 3814 | if (err) { |
| 3815 | pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n", |
| 3816 | &res.start, its_entry->translation_id); |
| 3817 | goto dom_err; |
| 3818 | } |
| 3819 | |
| 3820 | err = its_probe_one(&res, dom_handle, |
| 3821 | acpi_get_its_numa_node(its_entry->translation_id)); |
| 3822 | if (!err) |
| 3823 | return 0; |
| 3824 | |
| 3825 | iort_deregister_domain_token(its_entry->translation_id); |
| 3826 | dom_err: |
| 3827 | irq_domain_free_fwnode(dom_handle); |
| 3828 | return err; |
| 3829 | } |
| 3830 | |
| 3831 | static void __init its_acpi_probe(void) |
| 3832 | { |
| 3833 | acpi_table_parse_srat_its(); |
| 3834 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 3835 | gic_acpi_parse_madt_its, 0); |
| 3836 | acpi_its_srat_maps_free(); |
| 3837 | } |
| 3838 | #else |
| 3839 | static void __init its_acpi_probe(void) { } |
| 3840 | #endif |
| 3841 | |
| 3842 | int __init its_init(struct fwnode_handle *handle, struct rdists *rdists, |
| 3843 | struct irq_domain *parent_domain) |
| 3844 | { |
| 3845 | struct device_node *of_node; |
| 3846 | struct its_node *its; |
| 3847 | bool has_v4 = false; |
| 3848 | int err; |
| 3849 | |
| 3850 | its_parent = parent_domain; |
| 3851 | of_node = to_of_node(handle); |
| 3852 | if (of_node) |
| 3853 | its_of_probe(of_node); |
| 3854 | else |
| 3855 | its_acpi_probe(); |
| 3856 | |
| 3857 | if (list_empty(&its_nodes)) { |
| 3858 | pr_warn("ITS: No ITS available, not enabling LPIs\n"); |
| 3859 | return -ENXIO; |
| 3860 | } |
| 3861 | |
| 3862 | gic_rdists = rdists; |
| 3863 | err = its_alloc_lpi_tables(); |
| 3864 | if (err) |
| 3865 | return err; |
| 3866 | |
| 3867 | list_for_each_entry(its, &its_nodes, entry) |
| 3868 | has_v4 |= its->is_v4; |
| 3869 | |
| 3870 | if (has_v4 & rdists->has_vlpis) { |
| 3871 | if (its_init_vpe_domain() || |
| 3872 | its_init_v4(parent_domain, &its_vpe_domain_ops)) { |
| 3873 | rdists->has_vlpis = false; |
| 3874 | pr_err("ITS: Disabling GICv4 support\n"); |
| 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | register_syscore_ops(&its_syscore_ops); |
| 3879 | |
| 3880 | return 0; |
| 3881 | } |