| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2024-2025 ARM Limited, All Rights Reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
#define pr_fmt(fmt) "GICv5 ITS: " fmt |
| 7 |
|
| 8 |
#include <linux/acpi.h> |
| 9 |
#include <linux/acpi_iort.h> |
| 10 |
#include <linux/bitmap.h> |
| 11 |
#include <linux/iommu.h> |
| 12 |
#include <linux/init.h> |
| 13 |
#include <linux/kernel.h> |
| 14 |
#include <linux/msi.h> |
| 15 |
#include <linux/of.h> |
| 16 |
#include <linux/of_address.h> |
| 17 |
#include <linux/of_irq.h> |
| 18 |
#include <linux/slab.h> |
| 19 |
|
| 20 |
#include <linux/irqchip.h> |
| 21 |
#include <linux/irqchip/arm-gic-v5.h> |
| 22 |
#include <linux/irqchip/irq-msi-lib.h> |
| 23 |
|
| 24 |
#include "irq-gic-its-msi-parent.h" |
| 25 |
|
| 26 |
#define ITS_FLAGS_NON_COHERENT BIT(0) |
| 27 |
|
| 28 |
struct gicv5_its_chip_data { |
| 29 |
struct xarray its_devices; |
| 30 |
struct mutex dev_alloc_lock; |
| 31 |
struct fwnode_handle *fwnode; |
| 32 |
struct gicv5_its_devtab_cfg devtab_cfgr; |
| 33 |
void __iomem *its_base; |
| 34 |
u32 flags; |
| 35 |
unsigned int msi_domain_flags; |
| 36 |
}; |
| 37 |
|
| 38 |
struct gicv5_its_dev { |
| 39 |
struct gicv5_its_chip_data *its_node; |
| 40 |
struct gicv5_its_itt_cfg itt_cfg; |
| 41 |
unsigned long *event_map; |
| 42 |
u32 device_id; |
| 43 |
u32 num_events; |
| 44 |
phys_addr_t its_trans_phys_base; |
| 45 |
}; |
| 46 |
|
| 47 |
static u32 its_readl_relaxed(struct gicv5_its_chip_data *its_node, const u32 reg_offset) |
| 48 |
{ |
| 49 |
return readl_relaxed(its_node->its_base + reg_offset); |
| 50 |
} |
| 51 |
|
| 52 |
static void its_writel_relaxed(struct gicv5_its_chip_data *its_node, const u32 val, |
| 53 |
const u32 reg_offset) |
| 54 |
{ |
| 55 |
writel_relaxed(val, its_node->its_base + reg_offset); |
| 56 |
} |
| 57 |
|
| 58 |
static void its_writeq_relaxed(struct gicv5_its_chip_data *its_node, const u64 val, |
| 59 |
const u32 reg_offset) |
| 60 |
{ |
| 61 |
writeq_relaxed(val, its_node->its_base + reg_offset); |
| 62 |
} |
| 63 |
|
| 64 |
static void gicv5_its_dcache_clean(struct gicv5_its_chip_data *its, void *start, |
| 65 |
size_t sz) |
| 66 |
{ |
| 67 |
void *end = start + sz; |
| 68 |
|
| 69 |
if (its->flags & ITS_FLAGS_NON_COHERENT) |
| 70 |
dcache_clean_inval_poc((unsigned long)start, (unsigned long)end); |
| 71 |
else |
| 72 |
dsb(ishst); |
| 73 |
} |
| 74 |
|
| 75 |
static void its_write_table_entry(struct gicv5_its_chip_data *its, __le64 *entry, |
| 76 |
u64 val) |
| 77 |
{ |
| 78 |
WRITE_ONCE(*entry, cpu_to_le64(val)); |
| 79 |
gicv5_its_dcache_clean(its, entry, sizeof(*entry)); |
| 80 |
} |
| 81 |
|
| 82 |
#define devtab_cfgr_field(its, f) \ |
| 83 |
FIELD_GET(GICV5_ITS_DT_CFGR_##f, (its)->devtab_cfgr.cfgr) |
| 84 |
|
| 85 |
static int gicv5_its_cache_sync(struct gicv5_its_chip_data *its) |
| 86 |
{ |
| 87 |
return gicv5_wait_for_op_atomic(its->its_base, GICV5_ITS_STATUSR, |
| 88 |
GICV5_ITS_STATUSR_IDLE, NULL); |
| 89 |
} |
| 90 |
|
| 91 |
static void gicv5_its_syncr(struct gicv5_its_chip_data *its, |
| 92 |
struct gicv5_its_dev *its_dev) |
| 93 |
{ |
| 94 |
u64 syncr; |
| 95 |
|
| 96 |
syncr = FIELD_PREP(GICV5_ITS_SYNCR_SYNC, 1) | |
| 97 |
FIELD_PREP(GICV5_ITS_SYNCR_DEVICEID, its_dev->device_id); |
| 98 |
|
| 99 |
its_writeq_relaxed(its, syncr, GICV5_ITS_SYNCR); |
| 100 |
|
| 101 |
gicv5_wait_for_op(its->its_base, GICV5_ITS_SYNC_STATUSR, GICV5_ITS_SYNC_STATUSR_IDLE); |
| 102 |
} |
| 103 |
|
| 104 |
/* Number of bits required for each L2 {device/interrupt translation} table size */ |
| 105 |
#define ITS_L2SZ_64K_L2_BITS 13 |
| 106 |
#define ITS_L2SZ_16K_L2_BITS 11 |
| 107 |
#define ITS_L2SZ_4K_L2_BITS 9 |
| 108 |
|
| 109 |
static unsigned int gicv5_its_l2sz_to_l2_bits(unsigned int sz) |
| 110 |
{ |
| 111 |
switch (sz) { |
| 112 |
case GICV5_ITS_DT_ITT_CFGR_L2SZ_64k: |
| 113 |
return ITS_L2SZ_64K_L2_BITS; |
| 114 |
case GICV5_ITS_DT_ITT_CFGR_L2SZ_16k: |
| 115 |
return ITS_L2SZ_16K_L2_BITS; |
| 116 |
case GICV5_ITS_DT_ITT_CFGR_L2SZ_4k: |
| 117 |
default: |
| 118 |
return ITS_L2SZ_4K_L2_BITS; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static int gicv5_its_itt_cache_inv(struct gicv5_its_chip_data *its, u32 device_id, |
| 123 |
u16 event_id) |
| 124 |
{ |
| 125 |
u32 eventr, eidr; |
| 126 |
u64 didr; |
| 127 |
|
| 128 |
didr = FIELD_PREP(GICV5_ITS_DIDR_DEVICEID, device_id); |
| 129 |
eidr = FIELD_PREP(GICV5_ITS_EIDR_EVENTID, event_id); |
| 130 |
eventr = FIELD_PREP(GICV5_ITS_INV_EVENTR_I, 0x1); |
| 131 |
|
| 132 |
its_writeq_relaxed(its, didr, GICV5_ITS_DIDR); |
| 133 |
its_writel_relaxed(its, eidr, GICV5_ITS_EIDR); |
| 134 |
its_writel_relaxed(its, eventr, GICV5_ITS_INV_EVENTR); |
| 135 |
|
| 136 |
return gicv5_its_cache_sync(its); |
| 137 |
} |
| 138 |
|
| 139 |
static void gicv5_its_free_itt_linear(struct gicv5_its_dev *its_dev) |
| 140 |
{ |
| 141 |
kfree(its_dev->itt_cfg.linear.itt); |
| 142 |
} |
| 143 |
|
| 144 |
static void gicv5_its_free_itt_two_level(struct gicv5_its_dev *its_dev) |
| 145 |
{ |
| 146 |
unsigned int i, num_ents = its_dev->itt_cfg.l2.num_l1_ents; |
| 147 |
|
| 148 |
for (i = 0; i < num_ents; i++) |
| 149 |
kfree(its_dev->itt_cfg.l2.l2ptrs[i]); |
| 150 |
|
| 151 |
kfree(its_dev->itt_cfg.l2.l2ptrs); |
| 152 |
kfree(its_dev->itt_cfg.l2.l1itt); |
| 153 |
} |
| 154 |
|
| 155 |
static void gicv5_its_free_itt(struct gicv5_its_dev *its_dev) |
| 156 |
{ |
| 157 |
if (!its_dev->itt_cfg.l2itt) |
| 158 |
gicv5_its_free_itt_linear(its_dev); |
| 159 |
else |
| 160 |
gicv5_its_free_itt_two_level(its_dev); |
| 161 |
} |
| 162 |
|
| 163 |
static int gicv5_its_create_itt_linear(struct gicv5_its_chip_data *its, |
| 164 |
struct gicv5_its_dev *its_dev, |
| 165 |
unsigned int event_id_bits) |
| 166 |
{ |
| 167 |
unsigned int num_ents = BIT(event_id_bits); |
| 168 |
__le64 *itt; |
| 169 |
|
| 170 |
itt = kcalloc(num_ents, sizeof(*itt), GFP_KERNEL); |
| 171 |
if (!itt) |
| 172 |
return -ENOMEM; |
| 173 |
|
| 174 |
its_dev->itt_cfg.linear.itt = itt; |
| 175 |
its_dev->itt_cfg.linear.num_ents = num_ents; |
| 176 |
its_dev->itt_cfg.l2itt = false; |
| 177 |
its_dev->itt_cfg.event_id_bits = event_id_bits; |
| 178 |
|
| 179 |
gicv5_its_dcache_clean(its, itt, num_ents * sizeof(*itt)); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* Allocate a two-level ITT. All ITT entries are allocated in one go, unlike |
| 186 |
* with the device table. Span may be used to limit the second level table |
| 187 |
* size, where possible. |
| 188 |
*/ |
| 189 |
static int gicv5_its_create_itt_two_level(struct gicv5_its_chip_data *its, |
| 190 |
struct gicv5_its_dev *its_dev, |
| 191 |
unsigned int event_id_bits, |
| 192 |
unsigned int itt_l2sz, |
| 193 |
unsigned int num_events) |
| 194 |
{ |
| 195 |
unsigned int l1_bits, l2_bits, span, events_per_l2_table; |
| 196 |
unsigned int complete_tables, final_span, num_ents; |
| 197 |
__le64 *itt_l1, *itt_l2, **l2ptrs; |
| 198 |
int i, ret; |
| 199 |
u64 val; |
| 200 |
|
| 201 |
ret = gicv5_its_l2sz_to_l2_bits(itt_l2sz); |
| 202 |
if (ret >= event_id_bits) { |
| 203 |
pr_debug("Incorrect l2sz (0x%x) for %u EventID bits. Cannot allocate ITT\n", |
| 204 |
itt_l2sz, event_id_bits); |
| 205 |
return -EINVAL; |
| 206 |
} |
| 207 |
|
| 208 |
l2_bits = ret; |
| 209 |
|
| 210 |
l1_bits = event_id_bits - l2_bits; |
| 211 |
|
| 212 |
num_ents = BIT(l1_bits); |
| 213 |
|
| 214 |
itt_l1 = kcalloc(num_ents, sizeof(*itt_l1), GFP_KERNEL); |
| 215 |
if (!itt_l1) |
| 216 |
return -ENOMEM; |
| 217 |
|
| 218 |
l2ptrs = kcalloc(num_ents, sizeof(*l2ptrs), GFP_KERNEL); |
| 219 |
if (!l2ptrs) { |
| 220 |
kfree(itt_l1); |
| 221 |
return -ENOMEM; |
| 222 |
} |
| 223 |
|
| 224 |
its_dev->itt_cfg.l2.l2ptrs = l2ptrs; |
| 225 |
|
| 226 |
its_dev->itt_cfg.l2.l2sz = itt_l2sz; |
| 227 |
its_dev->itt_cfg.l2.l1itt = itt_l1; |
| 228 |
its_dev->itt_cfg.l2.num_l1_ents = num_ents; |
| 229 |
its_dev->itt_cfg.l2itt = true; |
| 230 |
its_dev->itt_cfg.event_id_bits = event_id_bits; |
| 231 |
|
| 232 |
/* |
| 233 |
* Need to determine how many entries there are per L2 - this is based |
| 234 |
* on the number of bits in the table. |
| 235 |
*/ |
| 236 |
events_per_l2_table = BIT(l2_bits); |
| 237 |
complete_tables = num_events / events_per_l2_table; |
| 238 |
final_span = order_base_2(num_events % events_per_l2_table); |
| 239 |
|
| 240 |
for (i = 0; i < num_ents; i++) { |
| 241 |
size_t l2sz; |
| 242 |
|
| 243 |
span = i == complete_tables ? final_span : l2_bits; |
| 244 |
|
| 245 |
itt_l2 = kcalloc(BIT(span), sizeof(*itt_l2), GFP_KERNEL); |
| 246 |
if (!itt_l2) { |
| 247 |
ret = -ENOMEM; |
| 248 |
goto out_free; |
| 249 |
} |
| 250 |
|
| 251 |
its_dev->itt_cfg.l2.l2ptrs[i] = itt_l2; |
| 252 |
|
| 253 |
l2sz = BIT(span) * sizeof(*itt_l2); |
| 254 |
|
| 255 |
gicv5_its_dcache_clean(its, itt_l2, l2sz); |
| 256 |
|
| 257 |
val = (virt_to_phys(itt_l2) & GICV5_ITTL1E_L2_ADDR_MASK) | |
| 258 |
FIELD_PREP(GICV5_ITTL1E_SPAN, span) | |
| 259 |
FIELD_PREP(GICV5_ITTL1E_VALID, 0x1); |
| 260 |
|
| 261 |
WRITE_ONCE(itt_l1[i], cpu_to_le64(val)); |
| 262 |
} |
| 263 |
|
| 264 |
gicv5_its_dcache_clean(its, itt_l1, num_ents * sizeof(*itt_l1)); |
| 265 |
|
| 266 |
return 0; |
| 267 |
|
| 268 |
out_free: |
| 269 |
for (i = i - 1; i >= 0; i--) |
| 270 |
kfree(its_dev->itt_cfg.l2.l2ptrs[i]); |
| 271 |
|
| 272 |
kfree(its_dev->itt_cfg.l2.l2ptrs); |
| 273 |
kfree(itt_l1); |
| 274 |
return ret; |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* Function to check whether the device table or ITT table support |
| 279 |
* a two-level table and if so depending on the number of id_bits |
| 280 |
* requested, determine whether a two-level table is required. |
| 281 |
* |
| 282 |
* Return the 2-level size value if a two level table is deemed |
| 283 |
* necessary. |
| 284 |
*/ |
| 285 |
static bool gicv5_its_l2sz_two_level(bool devtab, u32 its_idr1, u8 id_bits, u8 *sz) |
| 286 |
{ |
| 287 |
unsigned int l2_bits, l2_sz; |
| 288 |
|
| 289 |
if (devtab && !FIELD_GET(GICV5_ITS_IDR1_DT_LEVELS, its_idr1)) |
| 290 |
return false; |
| 291 |
|
| 292 |
if (!devtab && !FIELD_GET(GICV5_ITS_IDR1_ITT_LEVELS, its_idr1)) |
| 293 |
return false; |
| 294 |
|
| 295 |
/* |
| 296 |
* Pick an L2 size that matches the pagesize; if a match |
| 297 |
* is not found, go for the smallest supported l2 size granule. |
| 298 |
* |
| 299 |
* This ensures that we will always be able to allocate |
| 300 |
* contiguous memory at L2. |
| 301 |
*/ |
| 302 |
switch (PAGE_SIZE) { |
| 303 |
case SZ_64K: |
| 304 |
if (GICV5_ITS_IDR1_L2SZ_SUPPORT_64KB(its_idr1)) { |
| 305 |
l2_sz = GICV5_ITS_DT_ITT_CFGR_L2SZ_64k; |
| 306 |
break; |
| 307 |
} |
| 308 |
fallthrough; |
| 309 |
case SZ_4K: |
| 310 |
if (GICV5_ITS_IDR1_L2SZ_SUPPORT_4KB(its_idr1)) { |
| 311 |
l2_sz = GICV5_ITS_DT_ITT_CFGR_L2SZ_4k; |
| 312 |
break; |
| 313 |
} |
| 314 |
fallthrough; |
| 315 |
case SZ_16K: |
| 316 |
if (GICV5_ITS_IDR1_L2SZ_SUPPORT_16KB(its_idr1)) { |
| 317 |
l2_sz = GICV5_ITS_DT_ITT_CFGR_L2SZ_16k; |
| 318 |
break; |
| 319 |
} |
| 320 |
if (GICV5_ITS_IDR1_L2SZ_SUPPORT_4KB(its_idr1)) { |
| 321 |
l2_sz = GICV5_ITS_DT_ITT_CFGR_L2SZ_4k; |
| 322 |
break; |
| 323 |
} |
| 324 |
if (GICV5_ITS_IDR1_L2SZ_SUPPORT_64KB(its_idr1)) { |
| 325 |
l2_sz = GICV5_ITS_DT_ITT_CFGR_L2SZ_64k; |
| 326 |
break; |
| 327 |
} |
| 328 |
|
| 329 |
l2_sz = GICV5_ITS_DT_ITT_CFGR_L2SZ_4k; |
| 330 |
break; |
| 331 |
} |
| 332 |
|
| 333 |
l2_bits = gicv5_its_l2sz_to_l2_bits(l2_sz); |
| 334 |
|
| 335 |
if (l2_bits > id_bits) |
| 336 |
return false; |
| 337 |
|
| 338 |
*sz = l2_sz; |
| 339 |
|
| 340 |
return true; |
| 341 |
} |
| 342 |
|
| 343 |
static __le64 *gicv5_its_device_get_itte_ref(struct gicv5_its_dev *its_dev, |
| 344 |
u16 event_id) |
| 345 |
{ |
| 346 |
unsigned int l1_idx, l2_idx, l2_bits; |
| 347 |
__le64 *l2_itt; |
| 348 |
|
| 349 |
if (!its_dev->itt_cfg.l2itt) { |
| 350 |
__le64 *itt = its_dev->itt_cfg.linear.itt; |
| 351 |
|
| 352 |
return &itt[event_id]; |
| 353 |
} |
| 354 |
|
| 355 |
l2_bits = gicv5_its_l2sz_to_l2_bits(its_dev->itt_cfg.l2.l2sz); |
| 356 |
l1_idx = event_id >> l2_bits; |
| 357 |
l2_idx = event_id & GENMASK(l2_bits - 1, 0); |
| 358 |
l2_itt = its_dev->itt_cfg.l2.l2ptrs[l1_idx]; |
| 359 |
|
| 360 |
return &l2_itt[l2_idx]; |
| 361 |
} |
| 362 |
|
| 363 |
static int gicv5_its_device_cache_inv(struct gicv5_its_chip_data *its, |
| 364 |
struct gicv5_its_dev *its_dev) |
| 365 |
{ |
| 366 |
u32 devicer; |
| 367 |
u64 didr; |
| 368 |
|
| 369 |
didr = FIELD_PREP(GICV5_ITS_DIDR_DEVICEID, its_dev->device_id); |
| 370 |
devicer = FIELD_PREP(GICV5_ITS_INV_DEVICER_I, 0x1) | |
| 371 |
FIELD_PREP(GICV5_ITS_INV_DEVICER_EVENTID_BITS, |
| 372 |
its_dev->itt_cfg.event_id_bits) | |
| 373 |
FIELD_PREP(GICV5_ITS_INV_DEVICER_L1, 0x0); |
| 374 |
its_writeq_relaxed(its, didr, GICV5_ITS_DIDR); |
| 375 |
its_writel_relaxed(its, devicer, GICV5_ITS_INV_DEVICER); |
| 376 |
|
| 377 |
return gicv5_its_cache_sync(its); |
| 378 |
} |
| 379 |
|
| 380 |
/* |
| 381 |
* Allocate a level 2 device table entry, update L1 parent to reference it. |
| 382 |
* Only used for 2-level device tables, and it is called on demand. |
| 383 |
*/ |
| 384 |
static int gicv5_its_alloc_l2_devtab(struct gicv5_its_chip_data *its, |
| 385 |
unsigned int l1_index) |
| 386 |
{ |
| 387 |
__le64 *l2devtab, *l1devtab = its->devtab_cfgr.l2.l1devtab; |
| 388 |
u8 span, l2sz, l2_bits; |
| 389 |
u64 l1dte; |
| 390 |
|
| 391 |
if (FIELD_GET(GICV5_DTL1E_VALID, le64_to_cpu(l1devtab[l1_index]))) |
| 392 |
return 0; |
| 393 |
|
| 394 |
span = FIELD_GET(GICV5_DTL1E_SPAN, le64_to_cpu(l1devtab[l1_index])); |
| 395 |
l2sz = devtab_cfgr_field(its, L2SZ); |
| 396 |
|
| 397 |
l2_bits = gicv5_its_l2sz_to_l2_bits(l2sz); |
| 398 |
|
| 399 |
/* |
| 400 |
* Span allows us to create a smaller L2 device table. |
| 401 |
* If it is too large, use the number of allowed L2 bits. |
| 402 |
*/ |
| 403 |
if (span > l2_bits) |
| 404 |
span = l2_bits; |
| 405 |
|
| 406 |
l2devtab = kcalloc(BIT(span), sizeof(*l2devtab), GFP_KERNEL); |
| 407 |
if (!l2devtab) |
| 408 |
return -ENOMEM; |
| 409 |
|
| 410 |
its->devtab_cfgr.l2.l2ptrs[l1_index] = l2devtab; |
| 411 |
|
| 412 |
l1dte = FIELD_PREP(GICV5_DTL1E_SPAN, span) | |
| 413 |
(virt_to_phys(l2devtab) & GICV5_DTL1E_L2_ADDR_MASK) | |
| 414 |
FIELD_PREP(GICV5_DTL1E_VALID, 0x1); |
| 415 |
its_write_table_entry(its, &l1devtab[l1_index], l1dte); |
| 416 |
|
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
static __le64 *gicv5_its_devtab_get_dte_ref(struct gicv5_its_chip_data *its, |
| 421 |
u32 device_id, bool alloc) |
| 422 |
{ |
| 423 |
u8 str = devtab_cfgr_field(its, STRUCTURE); |
| 424 |
unsigned int l2sz, l2_bits, l1_idx, l2_idx; |
| 425 |
__le64 *l2devtab; |
| 426 |
int ret; |
| 427 |
|
| 428 |
if (str == GICV5_ITS_DT_ITT_CFGR_STRUCTURE_LINEAR) { |
| 429 |
l2devtab = its->devtab_cfgr.linear.devtab; |
| 430 |
return &l2devtab[device_id]; |
| 431 |
} |
| 432 |
|
| 433 |
l2sz = devtab_cfgr_field(its, L2SZ); |
| 434 |
l2_bits = gicv5_its_l2sz_to_l2_bits(l2sz); |
| 435 |
l1_idx = device_id >> l2_bits; |
| 436 |
l2_idx = device_id & GENMASK(l2_bits - 1, 0); |
| 437 |
|
| 438 |
if (alloc) { |
| 439 |
/* |
| 440 |
* Allocate a new L2 device table here before |
| 441 |
* continuing. We make the assumption that the span in |
| 442 |
* the L1 table has been set correctly, and blindly use |
| 443 |
* that value. |
| 444 |
*/ |
| 445 |
ret = gicv5_its_alloc_l2_devtab(its, l1_idx); |
| 446 |
if (ret) |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
|
| 450 |
l2devtab = its->devtab_cfgr.l2.l2ptrs[l1_idx]; |
| 451 |
return &l2devtab[l2_idx]; |
| 452 |
} |
| 453 |
|
| 454 |
/* |
| 455 |
* Register a new device in the device table. Allocate an ITT and |
| 456 |
* program the L2DTE entry according to the ITT structure that |
| 457 |
* was chosen. |
| 458 |
*/ |
| 459 |
static int gicv5_its_device_register(struct gicv5_its_chip_data *its, |
| 460 |
struct gicv5_its_dev *its_dev) |
| 461 |
{ |
| 462 |
u8 event_id_bits, device_id_bits, itt_struct, itt_l2sz; |
| 463 |
phys_addr_t itt_phys_base; |
| 464 |
bool two_level_itt; |
| 465 |
u32 idr1, idr2; |
| 466 |
__le64 *dte; |
| 467 |
u64 val; |
| 468 |
int ret; |
| 469 |
|
| 470 |
device_id_bits = devtab_cfgr_field(its, DEVICEID_BITS); |
| 471 |
|
| 472 |
if (its_dev->device_id >= BIT(device_id_bits)) { |
| 473 |
pr_err("Supplied DeviceID (%u) outside of Device Table range (%u)!", |
| 474 |
its_dev->device_id, (u32)GENMASK(device_id_bits - 1, 0)); |
| 475 |
return -EINVAL; |
| 476 |
} |
| 477 |
|
| 478 |
dte = gicv5_its_devtab_get_dte_ref(its, its_dev->device_id, true); |
| 479 |
if (!dte) |
| 480 |
return -ENOMEM; |
| 481 |
|
| 482 |
if (FIELD_GET(GICV5_DTL2E_VALID, le64_to_cpu(*dte))) |
| 483 |
return -EBUSY; |
| 484 |
|
| 485 |
/* |
| 486 |
* Determine how many bits we need, validate those against the max. |
| 487 |
* Based on these, determine if we should go for a 1- or 2-level ITT. |
| 488 |
*/ |
| 489 |
event_id_bits = order_base_2(its_dev->num_events); |
| 490 |
|
| 491 |
idr2 = its_readl_relaxed(its, GICV5_ITS_IDR2); |
| 492 |
|
| 493 |
if (event_id_bits > FIELD_GET(GICV5_ITS_IDR2_EVENTID_BITS, idr2)) { |
| 494 |
pr_err("Required EventID bits (%u) larger than supported bits (%u)!", |
| 495 |
event_id_bits, |
| 496 |
(u8)FIELD_GET(GICV5_ITS_IDR2_EVENTID_BITS, idr2)); |
| 497 |
return -EINVAL; |
| 498 |
} |
| 499 |
|
| 500 |
idr1 = its_readl_relaxed(its, GICV5_ITS_IDR1); |
| 501 |
|
| 502 |
/* |
| 503 |
* L2 ITT size is programmed into the L2DTE regardless of |
| 504 |
* whether a two-level or linear ITT is built, init it. |
| 505 |
*/ |
| 506 |
itt_l2sz = 0; |
| 507 |
|
| 508 |
two_level_itt = gicv5_its_l2sz_two_level(false, idr1, event_id_bits, |
| 509 |
&itt_l2sz); |
| 510 |
if (two_level_itt) |
| 511 |
ret = gicv5_its_create_itt_two_level(its, its_dev, event_id_bits, |
| 512 |
itt_l2sz, |
| 513 |
its_dev->num_events); |
| 514 |
else |
| 515 |
ret = gicv5_its_create_itt_linear(its, its_dev, event_id_bits); |
| 516 |
if (ret) |
| 517 |
return ret; |
| 518 |
|
| 519 |
itt_phys_base = two_level_itt ? virt_to_phys(its_dev->itt_cfg.l2.l1itt) : |
| 520 |
virt_to_phys(its_dev->itt_cfg.linear.itt); |
| 521 |
|
| 522 |
itt_struct = two_level_itt ? GICV5_ITS_DT_ITT_CFGR_STRUCTURE_TWO_LEVEL : |
| 523 |
GICV5_ITS_DT_ITT_CFGR_STRUCTURE_LINEAR; |
| 524 |
|
| 525 |
val = FIELD_PREP(GICV5_DTL2E_EVENT_ID_BITS, event_id_bits) | |
| 526 |
FIELD_PREP(GICV5_DTL2E_ITT_STRUCTURE, itt_struct) | |
| 527 |
(itt_phys_base & GICV5_DTL2E_ITT_ADDR_MASK) | |
| 528 |
FIELD_PREP(GICV5_DTL2E_ITT_L2SZ, itt_l2sz) | |
| 529 |
FIELD_PREP(GICV5_DTL2E_VALID, 0x1); |
| 530 |
|
| 531 |
its_write_table_entry(its, dte, val); |
| 532 |
|
| 533 |
ret = gicv5_its_device_cache_inv(its, its_dev); |
| 534 |
if (ret) { |
| 535 |
its_write_table_entry(its, dte, 0); |
| 536 |
gicv5_its_free_itt(its_dev); |
| 537 |
return ret; |
| 538 |
} |
| 539 |
|
| 540 |
return 0; |
| 541 |
} |
| 542 |
|
| 543 |
/* |
| 544 |
* Unregister a device in the device table. Lookup the device by ID, free the |
| 545 |
* corresponding ITT, mark the device as invalid in the device table. |
| 546 |
*/ |
| 547 |
static int gicv5_its_device_unregister(struct gicv5_its_chip_data *its, |
| 548 |
struct gicv5_its_dev *its_dev) |
| 549 |
{ |
| 550 |
__le64 *dte; |
| 551 |
|
| 552 |
dte = gicv5_its_devtab_get_dte_ref(its, its_dev->device_id, false); |
| 553 |
|
| 554 |
if (!FIELD_GET(GICV5_DTL2E_VALID, le64_to_cpu(*dte))) { |
| 555 |
pr_debug("Device table entry for DeviceID 0x%x is not valid. Nothing to clean up!", |
| 556 |
its_dev->device_id); |
| 557 |
return -EINVAL; |
| 558 |
} |
| 559 |
|
| 560 |
/* Zero everything - make it clear that this is an invalid entry */ |
| 561 |
its_write_table_entry(its, dte, 0); |
| 562 |
|
| 563 |
gicv5_its_free_itt(its_dev); |
| 564 |
|
| 565 |
return gicv5_its_device_cache_inv(its, its_dev); |
| 566 |
} |
| 567 |
|
| 568 |
/* |
| 569 |
* Allocate a 1-level device table. All entries are allocated, but marked |
| 570 |
* invalid. |
| 571 |
*/ |
| 572 |
static int gicv5_its_alloc_devtab_linear(struct gicv5_its_chip_data *its, |
| 573 |
u8 device_id_bits) |
| 574 |
{ |
| 575 |
__le64 *devtab; |
| 576 |
size_t sz; |
| 577 |
u64 baser; |
| 578 |
u32 cfgr; |
| 579 |
|
| 580 |
/* |
| 581 |
* We expect a GICv5 implementation requiring a large number of |
| 582 |
* deviceID bits to support a 2-level device table. If that's not |
| 583 |
* the case, cap the number of deviceIDs supported according to the |
| 584 |
* kmalloc limits so that the system can chug along with a linear |
| 585 |
* device table. |
| 586 |
*/ |
| 587 |
sz = BIT_ULL(device_id_bits) * sizeof(*devtab); |
| 588 |
if (sz > KMALLOC_MAX_SIZE) { |
| 589 |
u8 device_id_cap = ilog2(KMALLOC_MAX_SIZE/sizeof(*devtab)); |
| 590 |
|
| 591 |
pr_warn("Limiting device ID bits from %u to %u\n", |
| 592 |
device_id_bits, device_id_cap); |
| 593 |
device_id_bits = device_id_cap; |
| 594 |
} |
| 595 |
|
| 596 |
devtab = kcalloc(BIT(device_id_bits), sizeof(*devtab), GFP_KERNEL); |
| 597 |
if (!devtab) |
| 598 |
return -ENOMEM; |
| 599 |
|
| 600 |
gicv5_its_dcache_clean(its, devtab, sz); |
| 601 |
|
| 602 |
cfgr = FIELD_PREP(GICV5_ITS_DT_CFGR_STRUCTURE, |
| 603 |
GICV5_ITS_DT_ITT_CFGR_STRUCTURE_LINEAR) | |
| 604 |
FIELD_PREP(GICV5_ITS_DT_CFGR_L2SZ, 0) | |
| 605 |
FIELD_PREP(GICV5_ITS_DT_CFGR_DEVICEID_BITS, device_id_bits); |
| 606 |
its_writel_relaxed(its, cfgr, GICV5_ITS_DT_CFGR); |
| 607 |
|
| 608 |
baser = virt_to_phys(devtab) & GICV5_ITS_DT_BASER_ADDR_MASK; |
| 609 |
its_writeq_relaxed(its, baser, GICV5_ITS_DT_BASER); |
| 610 |
|
| 611 |
its->devtab_cfgr.cfgr = cfgr; |
| 612 |
its->devtab_cfgr.linear.devtab = devtab; |
| 613 |
|
| 614 |
return 0; |
| 615 |
} |
| 616 |
|
| 617 |
/* |
| 618 |
* Allocate a 2-level device table. L2 entries are not allocated, |
| 619 |
* they are allocated on-demand. |
| 620 |
*/ |
| 621 |
static int gicv5_its_alloc_devtab_two_level(struct gicv5_its_chip_data *its, |
| 622 |
u8 device_id_bits, |
| 623 |
u8 devtab_l2sz) |
| 624 |
{ |
| 625 |
unsigned int l1_bits, l2_bits, i; |
| 626 |
__le64 *l1devtab, **l2ptrs; |
| 627 |
size_t l1_sz; |
| 628 |
u64 baser; |
| 629 |
u32 cfgr; |
| 630 |
|
| 631 |
l2_bits = gicv5_its_l2sz_to_l2_bits(devtab_l2sz); |
| 632 |
|
| 633 |
l1_bits = device_id_bits - l2_bits; |
| 634 |
l1_sz = BIT(l1_bits) * sizeof(*l1devtab); |
| 635 |
/* |
| 636 |
* With 2-level device table support it is highly unlikely |
| 637 |
* that we are not able to allocate the required amount of |
| 638 |
* device table memory to cover deviceID space; cap the |
| 639 |
* deviceID space if we encounter such set-up. |
| 640 |
* If this ever becomes a problem we could revisit the policy |
| 641 |
* behind level 2 size selection to reduce level-1 deviceID bits. |
| 642 |
*/ |
| 643 |
if (l1_sz > KMALLOC_MAX_SIZE) { |
| 644 |
l1_bits = ilog2(KMALLOC_MAX_SIZE/sizeof(*l1devtab)); |
| 645 |
|
| 646 |
pr_warn("Limiting device ID bits from %u to %u\n", |
| 647 |
device_id_bits, l1_bits + l2_bits); |
| 648 |
device_id_bits = l1_bits + l2_bits; |
| 649 |
l1_sz = KMALLOC_MAX_SIZE; |
| 650 |
} |
| 651 |
|
| 652 |
l1devtab = kcalloc(BIT(l1_bits), sizeof(*l1devtab), GFP_KERNEL); |
| 653 |
if (!l1devtab) |
| 654 |
return -ENOMEM; |
| 655 |
|
| 656 |
l2ptrs = kcalloc(BIT(l1_bits), sizeof(*l2ptrs), GFP_KERNEL); |
| 657 |
if (!l2ptrs) { |
| 658 |
kfree(l1devtab); |
| 659 |
return -ENOMEM; |
| 660 |
} |
| 661 |
|
| 662 |
for (i = 0; i < BIT(l1_bits); i++) |
| 663 |
l1devtab[i] = cpu_to_le64(FIELD_PREP(GICV5_DTL1E_SPAN, l2_bits)); |
| 664 |
|
| 665 |
gicv5_its_dcache_clean(its, l1devtab, l1_sz); |
| 666 |
|
| 667 |
cfgr = FIELD_PREP(GICV5_ITS_DT_CFGR_STRUCTURE, |
| 668 |
GICV5_ITS_DT_ITT_CFGR_STRUCTURE_TWO_LEVEL) | |
| 669 |
FIELD_PREP(GICV5_ITS_DT_CFGR_L2SZ, devtab_l2sz) | |
| 670 |
FIELD_PREP(GICV5_ITS_DT_CFGR_DEVICEID_BITS, device_id_bits); |
| 671 |
its_writel_relaxed(its, cfgr, GICV5_ITS_DT_CFGR); |
| 672 |
|
| 673 |
baser = virt_to_phys(l1devtab) & GICV5_ITS_DT_BASER_ADDR_MASK; |
| 674 |
its_writeq_relaxed(its, baser, GICV5_ITS_DT_BASER); |
| 675 |
|
| 676 |
its->devtab_cfgr.cfgr = cfgr; |
| 677 |
its->devtab_cfgr.l2.l1devtab = l1devtab; |
| 678 |
its->devtab_cfgr.l2.l2ptrs = l2ptrs; |
| 679 |
|
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
/* |
| 684 |
* Initialise the device table as either 1- or 2-level depending on what is |
| 685 |
* supported by the hardware. |
| 686 |
*/ |
| 687 |
static int gicv5_its_init_devtab(struct gicv5_its_chip_data *its) |
| 688 |
{ |
| 689 |
u8 device_id_bits, devtab_l2sz; |
| 690 |
bool two_level_devtab; |
| 691 |
u32 idr1; |
| 692 |
|
| 693 |
idr1 = its_readl_relaxed(its, GICV5_ITS_IDR1); |
| 694 |
|
| 695 |
device_id_bits = FIELD_GET(GICV5_ITS_IDR1_DEVICEID_BITS, idr1); |
| 696 |
two_level_devtab = gicv5_its_l2sz_two_level(true, idr1, device_id_bits, |
| 697 |
&devtab_l2sz); |
| 698 |
if (two_level_devtab) |
| 699 |
return gicv5_its_alloc_devtab_two_level(its, device_id_bits, |
| 700 |
devtab_l2sz); |
| 701 |
else |
| 702 |
return gicv5_its_alloc_devtab_linear(its, device_id_bits); |
| 703 |
} |
| 704 |
|
| 705 |
static void gicv5_its_deinit_devtab(struct gicv5_its_chip_data *its) |
| 706 |
{ |
| 707 |
u8 str = devtab_cfgr_field(its, STRUCTURE); |
| 708 |
|
| 709 |
if (str == GICV5_ITS_DT_ITT_CFGR_STRUCTURE_LINEAR) { |
| 710 |
kfree(its->devtab_cfgr.linear.devtab); |
| 711 |
} else { |
| 712 |
kfree(its->devtab_cfgr.l2.l1devtab); |
| 713 |
kfree(its->devtab_cfgr.l2.l2ptrs); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
static void gicv5_its_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) |
| 718 |
{ |
| 719 |
struct gicv5_its_dev *its_dev = irq_data_get_irq_chip_data(d); |
| 720 |
u64 addr = its_dev->its_trans_phys_base; |
| 721 |
|
| 722 |
msg->data = FIELD_GET(GICV5_ITS_HWIRQ_EVENT_ID, d->hwirq); |
| 723 |
msi_msg_set_addr(irq_data_get_msi_desc(d), msg, addr); |
| 724 |
} |
| 725 |
|
| 726 |
static const struct irq_chip gicv5_its_irq_chip = { |
| 727 |
.name = "GICv5-ITS-MSI", |
| 728 |
.irq_mask = irq_chip_mask_parent, |
| 729 |
.irq_unmask = irq_chip_unmask_parent, |
| 730 |
.irq_eoi = irq_chip_eoi_parent, |
| 731 |
.irq_set_affinity = irq_chip_set_affinity_parent, |
| 732 |
.irq_get_irqchip_state = irq_chip_get_parent_state, |
| 733 |
.irq_set_irqchip_state = irq_chip_set_parent_state, |
| 734 |
.irq_compose_msi_msg = gicv5_its_compose_msi_msg, |
| 735 |
}; |
| 736 |
|
| 737 |
static struct gicv5_its_dev *gicv5_its_find_device(struct gicv5_its_chip_data *its, |
| 738 |
u32 device_id) |
| 739 |
{ |
| 740 |
struct gicv5_its_dev *dev = xa_load(&its->its_devices, device_id); |
| 741 |
|
| 742 |
return dev ? dev : ERR_PTR(-ENODEV); |
| 743 |
} |
| 744 |
|
| 745 |
static struct gicv5_its_dev *gicv5_its_alloc_device(struct gicv5_its_chip_data *its, int nvec, |
| 746 |
u32 dev_id) |
| 747 |
{ |
| 748 |
struct gicv5_its_dev *its_dev; |
| 749 |
void *entry; |
| 750 |
int ret; |
| 751 |
|
| 752 |
its_dev = gicv5_its_find_device(its, dev_id); |
| 753 |
if (!IS_ERR(its_dev)) { |
| 754 |
pr_err("A device with this DeviceID (0x%x) has already been registered.\n", |
| 755 |
dev_id); |
| 756 |
|
| 757 |
return ERR_PTR(-EBUSY); |
| 758 |
} |
| 759 |
|
| 760 |
its_dev = kzalloc_obj(*its_dev); |
| 761 |
if (!its_dev) |
| 762 |
return ERR_PTR(-ENOMEM); |
| 763 |
|
| 764 |
its_dev->device_id = dev_id; |
| 765 |
its_dev->num_events = nvec; |
| 766 |
|
| 767 |
ret = gicv5_its_device_register(its, its_dev); |
| 768 |
if (ret) { |
| 769 |
pr_err("Failed to register the device\n"); |
| 770 |
goto out_dev_free; |
| 771 |
} |
| 772 |
|
| 773 |
its_dev->its_node = its; |
| 774 |
|
| 775 |
its_dev->event_map = (unsigned long *)bitmap_zalloc(its_dev->num_events, GFP_KERNEL); |
| 776 |
if (!its_dev->event_map) { |
| 777 |
ret = -ENOMEM; |
| 778 |
goto out_unregister; |
| 779 |
} |
| 780 |
|
| 781 |
entry = xa_store(&its->its_devices, dev_id, its_dev, GFP_KERNEL); |
| 782 |
if (xa_is_err(entry)) { |
| 783 |
ret = xa_err(entry); |
| 784 |
goto out_bitmap_free; |
| 785 |
} |
| 786 |
|
| 787 |
return its_dev; |
| 788 |
|
| 789 |
out_bitmap_free: |
| 790 |
bitmap_free(its_dev->event_map); |
| 791 |
out_unregister: |
| 792 |
gicv5_its_device_unregister(its, its_dev); |
| 793 |
out_dev_free: |
| 794 |
kfree(its_dev); |
| 795 |
return ERR_PTR(ret); |
| 796 |
} |
| 797 |
|
| 798 |
static int gicv5_its_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 799 |
int nvec, msi_alloc_info_t *info) |
| 800 |
{ |
| 801 |
u32 dev_id = info->scratchpad[0].ul; |
| 802 |
struct msi_domain_info *msi_info; |
| 803 |
struct gicv5_its_chip_data *its; |
| 804 |
struct gicv5_its_dev *its_dev; |
| 805 |
|
| 806 |
msi_info = msi_get_domain_info(domain); |
| 807 |
its = msi_info->data; |
| 808 |
|
| 809 |
guard(mutex)(&its->dev_alloc_lock); |
| 810 |
|
| 811 |
its_dev = gicv5_its_alloc_device(its, nvec, dev_id); |
| 812 |
if (IS_ERR(its_dev)) |
| 813 |
return PTR_ERR(its_dev); |
| 814 |
|
| 815 |
its_dev->its_trans_phys_base = info->scratchpad[1].ul; |
| 816 |
info->scratchpad[0].ptr = its_dev; |
| 817 |
|
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
static void gicv5_its_msi_teardown(struct irq_domain *domain, msi_alloc_info_t *info) |
| 822 |
{ |
| 823 |
struct gicv5_its_dev *its_dev = info->scratchpad[0].ptr; |
| 824 |
struct msi_domain_info *msi_info; |
| 825 |
struct gicv5_its_chip_data *its; |
| 826 |
|
| 827 |
msi_info = msi_get_domain_info(domain); |
| 828 |
its = msi_info->data; |
| 829 |
|
| 830 |
guard(mutex)(&its->dev_alloc_lock); |
| 831 |
|
| 832 |
if (WARN_ON_ONCE(!bitmap_empty(its_dev->event_map, its_dev->num_events))) |
| 833 |
return; |
| 834 |
|
| 835 |
xa_erase(&its->its_devices, its_dev->device_id); |
| 836 |
bitmap_free(its_dev->event_map); |
| 837 |
gicv5_its_device_unregister(its, its_dev); |
| 838 |
kfree(its_dev); |
| 839 |
} |
| 840 |
|
| 841 |
static struct msi_domain_ops gicv5_its_msi_domain_ops = { |
| 842 |
.msi_prepare = gicv5_its_msi_prepare, |
| 843 |
.msi_teardown = gicv5_its_msi_teardown, |
| 844 |
}; |
| 845 |
|
| 846 |
static int gicv5_its_map_event(struct gicv5_its_dev *its_dev, u16 event_id, u32 lpi) |
| 847 |
{ |
| 848 |
struct gicv5_its_chip_data *its = its_dev->its_node; |
| 849 |
u64 itt_entry; |
| 850 |
__le64 *itte; |
| 851 |
|
| 852 |
itte = gicv5_its_device_get_itte_ref(its_dev, event_id); |
| 853 |
|
| 854 |
if (FIELD_GET(GICV5_ITTL2E_VALID, le64_to_cpu(*itte))) |
| 855 |
return -EEXIST; |
| 856 |
|
| 857 |
itt_entry = FIELD_PREP(GICV5_ITTL2E_LPI_ID, lpi) | |
| 858 |
FIELD_PREP(GICV5_ITTL2E_VALID, 0x1); |
| 859 |
|
| 860 |
its_write_table_entry(its, itte, itt_entry); |
| 861 |
|
| 862 |
gicv5_its_itt_cache_inv(its, its_dev->device_id, event_id); |
| 863 |
|
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
static void gicv5_its_unmap_event(struct gicv5_its_dev *its_dev, u16 event_id) |
| 868 |
{ |
| 869 |
struct gicv5_its_chip_data *its = its_dev->its_node; |
| 870 |
u64 itte_val; |
| 871 |
__le64 *itte; |
| 872 |
|
| 873 |
itte = gicv5_its_device_get_itte_ref(its_dev, event_id); |
| 874 |
|
| 875 |
itte_val = le64_to_cpu(*itte); |
| 876 |
itte_val &= ~GICV5_ITTL2E_VALID; |
| 877 |
|
| 878 |
its_write_table_entry(its, itte, itte_val); |
| 879 |
|
| 880 |
gicv5_its_itt_cache_inv(its, its_dev->device_id, event_id); |
| 881 |
} |
| 882 |
|
| 883 |
static int gicv5_its_alloc_eventid(struct gicv5_its_dev *its_dev, msi_alloc_info_t *info, |
| 884 |
unsigned int nr_irqs, u32 *eventid) |
| 885 |
{ |
| 886 |
int event_id_base; |
| 887 |
|
| 888 |
if (!(info->flags & MSI_ALLOC_FLAGS_FIXED_MSG_DATA)) { |
| 889 |
event_id_base = bitmap_find_free_region(its_dev->event_map, |
| 890 |
its_dev->num_events, |
| 891 |
get_count_order(nr_irqs)); |
| 892 |
if (event_id_base < 0) |
| 893 |
return event_id_base; |
| 894 |
} else { |
| 895 |
/* |
| 896 |
* We want to have a fixed EventID mapped for hardcoded |
| 897 |
* message data allocations. |
| 898 |
*/ |
| 899 |
if (WARN_ON_ONCE(nr_irqs != 1)) |
| 900 |
return -EINVAL; |
| 901 |
|
| 902 |
event_id_base = info->hwirq; |
| 903 |
|
| 904 |
if (event_id_base >= its_dev->num_events) { |
| 905 |
pr_err("EventID outside of ITT range; cannot allocate an ITT entry!\n"); |
| 906 |
|
| 907 |
return -EINVAL; |
| 908 |
} |
| 909 |
|
| 910 |
if (test_and_set_bit(event_id_base, its_dev->event_map)) { |
| 911 |
pr_warn("Can't reserve event_id bitmap\n"); |
| 912 |
return -EINVAL; |
| 913 |
|
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
*eventid = event_id_base; |
| 918 |
|
| 919 |
return 0; |
| 920 |
} |
| 921 |
|
| 922 |
static void gicv5_its_free_eventid(struct gicv5_its_dev *its_dev, u32 event_id_base, |
| 923 |
unsigned int nr_irqs) |
| 924 |
{ |
| 925 |
bitmap_release_region(its_dev->event_map, event_id_base, |
| 926 |
get_count_order(nr_irqs)); |
| 927 |
} |
| 928 |
|
| 929 |
static int gicv5_its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 930 |
unsigned int nr_irqs, void *arg) |
| 931 |
{ |
| 932 |
struct gicv5_its_dev *its_dev; |
| 933 |
u32 device_id, event_id_base; |
| 934 |
msi_alloc_info_t *info = arg; |
| 935 |
irq_hw_number_t hwirq; |
| 936 |
struct irq_data *irqd; |
| 937 |
int ret, i; |
| 938 |
|
| 939 |
its_dev = info->scratchpad[0].ptr; |
| 940 |
device_id = its_dev->device_id; |
| 941 |
|
| 942 |
ret = gicv5_its_alloc_eventid(its_dev, info, nr_irqs, &event_id_base); |
| 943 |
if (ret) |
| 944 |
return ret; |
| 945 |
|
| 946 |
ret = iommu_dma_prepare_msi(info->desc, its_dev->its_trans_phys_base); |
| 947 |
if (ret) |
| 948 |
goto out_eventid; |
| 949 |
|
| 950 |
ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, NULL); |
| 951 |
if (ret) |
| 952 |
goto out_eventid; |
| 953 |
|
| 954 |
for (i = 0; i < nr_irqs; i++) { |
| 955 |
/* |
| 956 |
* Store eventid and deviceid into the hwirq for later use. |
| 957 |
* |
| 958 |
* hwirq = event_id << 32 | device_id |
| 959 |
*/ |
| 960 |
hwirq = FIELD_PREP(GICV5_ITS_HWIRQ_DEVICE_ID, device_id) | |
| 961 |
FIELD_PREP(GICV5_ITS_HWIRQ_EVENT_ID, (u64)event_id_base + i); |
| 962 |
irq_domain_set_info(domain, virq + i, hwirq, |
| 963 |
&gicv5_its_irq_chip, its_dev, |
| 964 |
handle_fasteoi_irq, NULL, NULL); |
| 965 |
|
| 966 |
irqd = irq_get_irq_data(virq + i); |
| 967 |
irqd_set_single_target(irqd); |
| 968 |
irqd_set_affinity_on_activate(irqd); |
| 969 |
} |
| 970 |
|
| 971 |
return 0; |
| 972 |
|
| 973 |
out_eventid: |
| 974 |
gicv5_its_free_eventid(its_dev, event_id_base, nr_irqs); |
| 975 |
return ret; |
| 976 |
} |
| 977 |
|
| 978 |
static void gicv5_its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 979 |
unsigned int nr_irqs) |
| 980 |
{ |
| 981 |
struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 982 |
struct gicv5_its_chip_data *its; |
| 983 |
struct gicv5_its_dev *its_dev; |
| 984 |
u16 event_id_base; |
| 985 |
unsigned int i; |
| 986 |
|
| 987 |
its_dev = irq_data_get_irq_chip_data(d); |
| 988 |
its = its_dev->its_node; |
| 989 |
|
| 990 |
event_id_base = FIELD_GET(GICV5_ITS_HWIRQ_EVENT_ID, d->hwirq); |
| 991 |
|
| 992 |
bitmap_release_region(its_dev->event_map, event_id_base, |
| 993 |
get_count_order(nr_irqs)); |
| 994 |
|
| 995 |
for (i = 0; i < nr_irqs; i++) { |
| 996 |
d = irq_domain_get_irq_data(domain, virq + i); |
| 997 |
irq_domain_reset_irq_data(d); |
| 998 |
} |
| 999 |
|
| 1000 |
/* Hierarchically free irq data */ |
| 1001 |
irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 1002 |
|
| 1003 |
gicv5_its_syncr(its, its_dev); |
| 1004 |
gicv5_irs_syncr(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
static int gicv5_its_irq_domain_activate(struct irq_domain *domain, struct irq_data *d, |
| 1008 |
bool reserve) |
| 1009 |
{ |
| 1010 |
struct gicv5_its_dev *its_dev = irq_data_get_irq_chip_data(d); |
| 1011 |
u16 event_id; |
| 1012 |
u32 lpi; |
| 1013 |
|
| 1014 |
event_id = FIELD_GET(GICV5_ITS_HWIRQ_EVENT_ID, d->hwirq); |
| 1015 |
lpi = d->parent_data->hwirq; |
| 1016 |
|
| 1017 |
return gicv5_its_map_event(its_dev, event_id, lpi); |
| 1018 |
} |
| 1019 |
|
| 1020 |
static void gicv5_its_irq_domain_deactivate(struct irq_domain *domain, |
| 1021 |
struct irq_data *d) |
| 1022 |
{ |
| 1023 |
struct gicv5_its_dev *its_dev = irq_data_get_irq_chip_data(d); |
| 1024 |
u16 event_id; |
| 1025 |
|
| 1026 |
event_id = FIELD_GET(GICV5_ITS_HWIRQ_EVENT_ID, d->hwirq); |
| 1027 |
|
| 1028 |
gicv5_its_unmap_event(its_dev, event_id); |
| 1029 |
} |
| 1030 |
|
| 1031 |
static const struct irq_domain_ops gicv5_its_irq_domain_ops = { |
| 1032 |
.alloc = gicv5_its_irq_domain_alloc, |
| 1033 |
.free = gicv5_its_irq_domain_free, |
| 1034 |
.activate = gicv5_its_irq_domain_activate, |
| 1035 |
.deactivate = gicv5_its_irq_domain_deactivate, |
| 1036 |
.select = msi_lib_irq_domain_select, |
| 1037 |
}; |
| 1038 |
|
| 1039 |
static int gicv5_its_write_cr0(struct gicv5_its_chip_data *its, bool enable) |
| 1040 |
{ |
| 1041 |
u32 cr0 = FIELD_PREP(GICV5_ITS_CR0_ITSEN, enable); |
| 1042 |
|
| 1043 |
its_writel_relaxed(its, cr0, GICV5_ITS_CR0); |
| 1044 |
return gicv5_wait_for_op_atomic(its->its_base, GICV5_ITS_CR0, |
| 1045 |
GICV5_ITS_CR0_IDLE, NULL); |
| 1046 |
} |
| 1047 |
|
| 1048 |
static int gicv5_its_enable(struct gicv5_its_chip_data *its) |
| 1049 |
{ |
| 1050 |
return gicv5_its_write_cr0(its, true); |
| 1051 |
} |
| 1052 |
|
| 1053 |
static int gicv5_its_disable(struct gicv5_its_chip_data *its) |
| 1054 |
{ |
| 1055 |
return gicv5_its_write_cr0(its, false); |
| 1056 |
} |
| 1057 |
|
| 1058 |
static void gicv5_its_print_info(struct gicv5_its_chip_data *its_node) |
| 1059 |
{ |
| 1060 |
bool devtab_linear; |
| 1061 |
u8 device_id_bits; |
| 1062 |
u8 str; |
| 1063 |
|
| 1064 |
device_id_bits = devtab_cfgr_field(its_node, DEVICEID_BITS); |
| 1065 |
|
| 1066 |
str = devtab_cfgr_field(its_node, STRUCTURE); |
| 1067 |
devtab_linear = (str == GICV5_ITS_DT_ITT_CFGR_STRUCTURE_LINEAR); |
| 1068 |
|
| 1069 |
pr_info("ITS %s enabled using %s device table device_id_bits %u\n", |
| 1070 |
fwnode_get_name(its_node->fwnode), |
| 1071 |
devtab_linear ? "linear" : "2-level", |
| 1072 |
device_id_bits); |
| 1073 |
} |
| 1074 |
|
| 1075 |
static int gicv5_its_init_domain(struct gicv5_its_chip_data *its, struct irq_domain *parent) |
| 1076 |
{ |
| 1077 |
struct irq_domain_info dom_info = { |
| 1078 |
.fwnode = its->fwnode, |
| 1079 |
.ops = &gicv5_its_irq_domain_ops, |
| 1080 |
.domain_flags = its->msi_domain_flags, |
| 1081 |
.parent = parent, |
| 1082 |
}; |
| 1083 |
struct msi_domain_info *info; |
| 1084 |
|
| 1085 |
info = kzalloc_obj(*info); |
| 1086 |
if (!info) |
| 1087 |
return -ENOMEM; |
| 1088 |
|
| 1089 |
info->ops = &gicv5_its_msi_domain_ops; |
| 1090 |
info->data = its; |
| 1091 |
dom_info.host_data = info; |
| 1092 |
|
| 1093 |
if (!msi_create_parent_irq_domain(&dom_info, &gic_v5_its_msi_parent_ops)) { |
| 1094 |
kfree(info); |
| 1095 |
return -ENOMEM; |
| 1096 |
} |
| 1097 |
|
| 1098 |
return 0; |
| 1099 |
} |
| 1100 |
|
| 1101 |
static int __init gicv5_its_init_bases(void __iomem *its_base, struct fwnode_handle *handle, |
| 1102 |
struct irq_domain *parent_domain, bool noncoherent) |
| 1103 |
{ |
| 1104 |
struct device_node *np = to_of_node(handle); |
| 1105 |
struct gicv5_its_chip_data *its_node; |
| 1106 |
u32 cr0, cr1; |
| 1107 |
bool enabled; |
| 1108 |
int ret; |
| 1109 |
|
| 1110 |
its_node = kzalloc_obj(*its_node); |
| 1111 |
if (!its_node) |
| 1112 |
return -ENOMEM; |
| 1113 |
|
| 1114 |
mutex_init(&its_node->dev_alloc_lock); |
| 1115 |
xa_init(&its_node->its_devices); |
| 1116 |
its_node->fwnode = handle; |
| 1117 |
its_node->its_base = its_base; |
| 1118 |
its_node->msi_domain_flags = IRQ_DOMAIN_FLAG_ISOLATED_MSI | |
| 1119 |
IRQ_DOMAIN_FLAG_FWNODE_PARENT; |
| 1120 |
|
| 1121 |
cr0 = its_readl_relaxed(its_node, GICV5_ITS_CR0); |
| 1122 |
enabled = FIELD_GET(GICV5_ITS_CR0_ITSEN, cr0); |
| 1123 |
if (WARN(enabled, "ITS %s enabled, disabling it before proceeding\n", np->full_name)) { |
| 1124 |
ret = gicv5_its_disable(its_node); |
| 1125 |
if (ret) |
| 1126 |
goto out_free_node; |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (of_property_read_bool(np, "dma-noncoherent")) { |
| 1130 |
/* |
| 1131 |
* A non-coherent ITS implies that some cache levels cannot be |
| 1132 |
* used coherently by the cores and GIC. Our only option is to mark |
| 1133 |
* memory attributes for the GIC as non-cacheable; by default, |
| 1134 |
* non-cacheable memory attributes imply outer-shareable |
| 1135 |
* shareability, the value written into ITS_CR1_SH is ignored. |
| 1136 |
*/ |
| 1137 |
cr1 = FIELD_PREP(GICV5_ITS_CR1_ITT_RA, GICV5_NO_READ_ALLOC) | |
| 1138 |
FIELD_PREP(GICV5_ITS_CR1_DT_RA, GICV5_NO_READ_ALLOC) | |
| 1139 |
FIELD_PREP(GICV5_ITS_CR1_IC, GICV5_NON_CACHE) | |
| 1140 |
FIELD_PREP(GICV5_ITS_CR1_OC, GICV5_NON_CACHE); |
| 1141 |
its_node->flags |= ITS_FLAGS_NON_COHERENT; |
| 1142 |
} else { |
| 1143 |
cr1 = FIELD_PREP(GICV5_ITS_CR1_ITT_RA, GICV5_READ_ALLOC) | |
| 1144 |
FIELD_PREP(GICV5_ITS_CR1_DT_RA, GICV5_READ_ALLOC) | |
| 1145 |
FIELD_PREP(GICV5_ITS_CR1_IC, GICV5_WB_CACHE) | |
| 1146 |
FIELD_PREP(GICV5_ITS_CR1_OC, GICV5_WB_CACHE) | |
| 1147 |
FIELD_PREP(GICV5_ITS_CR1_SH, GICV5_INNER_SHARE); |
| 1148 |
} |
| 1149 |
|
| 1150 |
its_writel_relaxed(its_node, cr1, GICV5_ITS_CR1); |
| 1151 |
|
| 1152 |
ret = gicv5_its_init_devtab(its_node); |
| 1153 |
if (ret) |
| 1154 |
goto out_free_node; |
| 1155 |
|
| 1156 |
ret = gicv5_its_enable(its_node); |
| 1157 |
if (ret) |
| 1158 |
goto out_free_devtab; |
| 1159 |
|
| 1160 |
ret = gicv5_its_init_domain(its_node, parent_domain); |
| 1161 |
if (ret) |
| 1162 |
goto out_disable_its; |
| 1163 |
|
| 1164 |
gicv5_its_print_info(its_node); |
| 1165 |
|
| 1166 |
return 0; |
| 1167 |
|
| 1168 |
out_disable_its: |
| 1169 |
gicv5_its_disable(its_node); |
| 1170 |
out_free_devtab: |
| 1171 |
gicv5_its_deinit_devtab(its_node); |
| 1172 |
out_free_node: |
| 1173 |
kfree(its_node); |
| 1174 |
return ret; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static int __init gicv5_its_init(struct device_node *node) |
| 1178 |
{ |
| 1179 |
void __iomem *its_base; |
| 1180 |
int ret, idx; |
| 1181 |
|
| 1182 |
idx = of_property_match_string(node, "reg-names", "ns-config"); |
| 1183 |
if (idx < 0) { |
| 1184 |
pr_err("%pOF: ns-config reg-name not present\n", node); |
| 1185 |
return -ENODEV; |
| 1186 |
} |
| 1187 |
|
| 1188 |
its_base = of_io_request_and_map(node, idx, of_node_full_name(node)); |
| 1189 |
if (IS_ERR(its_base)) { |
| 1190 |
pr_err("%pOF: unable to map GICv5 ITS_CONFIG_FRAME\n", node); |
| 1191 |
return PTR_ERR(its_base); |
| 1192 |
} |
| 1193 |
|
| 1194 |
ret = gicv5_its_init_bases(its_base, of_fwnode_handle(node), |
| 1195 |
gicv5_global_data.lpi_domain, |
| 1196 |
of_property_read_bool(node, "dma-noncoherent")); |
| 1197 |
if (ret) |
| 1198 |
goto out_unmap; |
| 1199 |
|
| 1200 |
return 0; |
| 1201 |
|
| 1202 |
out_unmap: |
| 1203 |
iounmap(its_base); |
| 1204 |
return ret; |
| 1205 |
} |
| 1206 |
|
| 1207 |
void __init gicv5_its_of_probe(struct device_node *parent) |
| 1208 |
{ |
| 1209 |
struct device_node *np; |
| 1210 |
|
| 1211 |
for_each_available_child_of_node(parent, np) { |
| 1212 |
if (!of_device_is_compatible(np, "arm,gic-v5-its")) |
| 1213 |
continue; |
| 1214 |
|
| 1215 |
if (gicv5_its_init(np)) |
| 1216 |
pr_err("Failed to init ITS %s\n", np->full_name); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
#ifdef CONFIG_ACPI |
| 1221 |
|
| 1222 |
#define ACPI_GICV5_ITS_MEM_SIZE (SZ_64K) |
| 1223 |
|
| 1224 |
static struct acpi_madt_gicv5_translator *current_its_entry __initdata; |
| 1225 |
static struct fwnode_handle *current_its_fwnode __initdata; |
| 1226 |
|
| 1227 |
static int __init gic_acpi_parse_madt_its_translate(union acpi_subtable_headers *header, |
| 1228 |
const unsigned long end) |
| 1229 |
{ |
| 1230 |
struct acpi_madt_gicv5_translate_frame *its_frame; |
| 1231 |
struct fwnode_handle *msi_dom_handle; |
| 1232 |
struct resource res = {}; |
| 1233 |
int err; |
| 1234 |
|
| 1235 |
its_frame = (struct acpi_madt_gicv5_translate_frame *)header; |
| 1236 |
if (its_frame->linked_translator_id != current_its_entry->translator_id) |
| 1237 |
return 0; |
| 1238 |
|
| 1239 |
res.start = its_frame->base_address; |
| 1240 |
res.end = its_frame->base_address + ACPI_GICV5_ITS_MEM_SIZE - 1; |
| 1241 |
res.flags = IORESOURCE_MEM; |
| 1242 |
|
| 1243 |
msi_dom_handle = irq_domain_alloc_parented_fwnode(&res.start, current_its_fwnode); |
| 1244 |
if (!msi_dom_handle) { |
| 1245 |
pr_err("ITS@%pa: Unable to allocate GICv5 ITS translate domain token\n", |
| 1246 |
&res.start); |
| 1247 |
return -ENOMEM; |
| 1248 |
} |
| 1249 |
|
| 1250 |
err = iort_register_domain_token(its_frame->translate_frame_id, res.start, |
| 1251 |
msi_dom_handle); |
| 1252 |
if (err) { |
| 1253 |
pr_err("ITS@%pa: Unable to register GICv5 ITS domain token (ITS TRANSLATE FRAME ID %d) to IORT\n", |
| 1254 |
&res.start, its_frame->translate_frame_id); |
| 1255 |
irq_domain_free_fwnode(msi_dom_handle); |
| 1256 |
return err; |
| 1257 |
} |
| 1258 |
|
| 1259 |
return 0; |
| 1260 |
} |
| 1261 |
|
| 1262 |
static int __init gic_acpi_free_madt_its_translate(union acpi_subtable_headers *header, |
| 1263 |
const unsigned long end) |
| 1264 |
{ |
| 1265 |
struct acpi_madt_gicv5_translate_frame *its_frame; |
| 1266 |
struct fwnode_handle *msi_dom_handle; |
| 1267 |
|
| 1268 |
its_frame = (struct acpi_madt_gicv5_translate_frame *)header; |
| 1269 |
if (its_frame->linked_translator_id != current_its_entry->translator_id) |
| 1270 |
return 0; |
| 1271 |
|
| 1272 |
msi_dom_handle = iort_find_domain_token(its_frame->translate_frame_id); |
| 1273 |
if (!msi_dom_handle) |
| 1274 |
return 0; |
| 1275 |
|
| 1276 |
iort_deregister_domain_token(its_frame->translate_frame_id); |
| 1277 |
irq_domain_free_fwnode(msi_dom_handle); |
| 1278 |
|
| 1279 |
return 0; |
| 1280 |
} |
| 1281 |
|
| 1282 |
static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header, |
| 1283 |
const unsigned long end) |
| 1284 |
{ |
| 1285 |
struct acpi_madt_gicv5_translator *its_entry; |
| 1286 |
struct fwnode_handle *dom_handle; |
| 1287 |
struct resource res = {}; |
| 1288 |
void __iomem *its_base; |
| 1289 |
int err; |
| 1290 |
|
| 1291 |
its_entry = (struct acpi_madt_gicv5_translator *)header; |
| 1292 |
res.start = its_entry->base_address; |
| 1293 |
res.end = its_entry->base_address + ACPI_GICV5_ITS_MEM_SIZE - 1; |
| 1294 |
res.flags = IORESOURCE_MEM; |
| 1295 |
|
| 1296 |
if (!request_mem_region(res.start, resource_size(&res), "GICv5 ITS")) |
| 1297 |
return -EBUSY; |
| 1298 |
|
| 1299 |
dom_handle = irq_domain_alloc_fwnode(&res.start); |
| 1300 |
if (!dom_handle) { |
| 1301 |
pr_err("ITS@%pa: Unable to allocate GICv5 ITS domain token\n", |
| 1302 |
&res.start); |
| 1303 |
err = -ENOMEM; |
| 1304 |
goto out_rel_res; |
| 1305 |
} |
| 1306 |
|
| 1307 |
current_its_entry = its_entry; |
| 1308 |
current_its_fwnode = dom_handle; |
| 1309 |
|
| 1310 |
acpi_table_parse_madt(ACPI_MADT_TYPE_GICV5_ITS_TRANSLATE, |
| 1311 |
gic_acpi_parse_madt_its_translate, 0); |
| 1312 |
|
| 1313 |
its_base = ioremap(res.start, ACPI_GICV5_ITS_MEM_SIZE); |
| 1314 |
if (!its_base) { |
| 1315 |
err = -ENOMEM; |
| 1316 |
goto out_unregister; |
| 1317 |
} |
| 1318 |
|
| 1319 |
err = gicv5_its_init_bases(its_base, dom_handle, gicv5_global_data.lpi_domain, |
| 1320 |
its_entry->flags & ACPI_MADT_GICV5_ITS_NON_COHERENT); |
| 1321 |
if (err) |
| 1322 |
goto out_unmap; |
| 1323 |
|
| 1324 |
return 0; |
| 1325 |
|
| 1326 |
out_unmap: |
| 1327 |
iounmap(its_base); |
| 1328 |
out_unregister: |
| 1329 |
acpi_table_parse_madt(ACPI_MADT_TYPE_GICV5_ITS_TRANSLATE, |
| 1330 |
gic_acpi_free_madt_its_translate, 0); |
| 1331 |
irq_domain_free_fwnode(dom_handle); |
| 1332 |
out_rel_res: |
| 1333 |
release_mem_region(res.start, resource_size(&res)); |
| 1334 |
return err; |
| 1335 |
} |
| 1336 |
|
| 1337 |
void __init gicv5_its_acpi_probe(void) |
| 1338 |
{ |
| 1339 |
acpi_table_parse_madt(ACPI_MADT_TYPE_GICV5_ITS, gic_acpi_parse_madt_its, 0); |
| 1340 |
} |
| 1341 |
#else |
| 1342 |
void __init gicv5_its_acpi_probe(void) { } |
| 1343 |
#endif |