Bogdan Purcareata | 203e2de | 2018-01-17 18:36:44 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Freescale Management Complex (MC) bus driver MSI support |
| 4 | * |
Stuart Yoder | 6466dac | 2016-10-26 11:20:33 -0500 | [diff] [blame] | 5 | * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 6 | * Author: German Rivera <German.Rivera@freescale.com> |
| 7 | * |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 8 | */ |
| 9 | |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 10 | #include <linux/of_device.h> |
| 11 | #include <linux/of_address.h> |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 12 | #include <linux/of_irq.h> |
| 13 | #include <linux/irq.h> |
| 14 | #include <linux/irqdomain.h> |
| 15 | #include <linux/msi.h> |
Bogdan Purcareata | 6bd067c | 2018-02-05 08:07:42 -0600 | [diff] [blame] | 16 | |
Bogdan Purcareata | 07e9ef1 | 2017-02-13 18:40:41 +0200 | [diff] [blame] | 17 | #include "fsl-mc-private.h" |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 18 | |
Laurentiu Tudor | 507deb2 | 2017-07-19 14:42:28 +0300 | [diff] [blame] | 19 | #ifdef GENERIC_MSI_DOMAIN_OPS |
Stuart Yoder | adf72b5 | 2016-06-22 16:40:51 -0500 | [diff] [blame] | 20 | /* |
| 21 | * Generate a unique ID identifying the interrupt (only used within the MSI |
| 22 | * irqdomain. Combine the icid with the interrupt index. |
| 23 | */ |
| 24 | static irq_hw_number_t fsl_mc_domain_calc_hwirq(struct fsl_mc_device *dev, |
| 25 | struct msi_desc *desc) |
| 26 | { |
| 27 | /* |
| 28 | * Make the base hwirq value for ICID*10000 so it is readable |
| 29 | * as a decimal value in /proc/interrupts. |
| 30 | */ |
| 31 | return (irq_hw_number_t)(desc->fsl_mc.msi_index + (dev->icid * 10000)); |
| 32 | } |
| 33 | |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 34 | static void fsl_mc_msi_set_desc(msi_alloc_info_t *arg, |
| 35 | struct msi_desc *desc) |
| 36 | { |
| 37 | arg->desc = desc; |
Stuart Yoder | adf72b5 | 2016-06-22 16:40:51 -0500 | [diff] [blame] | 38 | arg->hwirq = fsl_mc_domain_calc_hwirq(to_fsl_mc_device(desc->dev), |
| 39 | desc); |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 40 | } |
Laurentiu Tudor | 507deb2 | 2017-07-19 14:42:28 +0300 | [diff] [blame] | 41 | #else |
| 42 | #define fsl_mc_msi_set_desc NULL |
| 43 | #endif |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 44 | |
| 45 | static void fsl_mc_msi_update_dom_ops(struct msi_domain_info *info) |
| 46 | { |
| 47 | struct msi_domain_ops *ops = info->ops; |
| 48 | |
Laurentiu Tudor | a385dd7 | 2017-11-17 15:38:32 +0200 | [diff] [blame] | 49 | if (!ops) |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 50 | return; |
| 51 | |
| 52 | /* |
| 53 | * set_desc should not be set by the caller |
| 54 | */ |
Sandhya Bankar | 905a672 | 2016-09-18 05:07:21 +0530 | [diff] [blame] | 55 | if (!ops->set_desc) |
Matthias Brugger | 33ea58a | 2016-04-14 23:24:26 +0200 | [diff] [blame] | 56 | ops->set_desc = fsl_mc_msi_set_desc; |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void __fsl_mc_msi_write_msg(struct fsl_mc_device *mc_bus_dev, |
| 60 | struct fsl_mc_device_irq *mc_dev_irq) |
| 61 | { |
| 62 | int error; |
| 63 | struct fsl_mc_device *owner_mc_dev = mc_dev_irq->mc_dev; |
| 64 | struct msi_desc *msi_desc = mc_dev_irq->msi_desc; |
| 65 | struct dprc_irq_cfg irq_cfg; |
| 66 | |
| 67 | /* |
| 68 | * msi_desc->msg.address is 0x0 when this function is invoked in |
| 69 | * the free_irq() code path. In this case, for the MC, we don't |
| 70 | * really need to "unprogram" the MSI, so we just return. |
| 71 | */ |
| 72 | if (msi_desc->msg.address_lo == 0x0 && msi_desc->msg.address_hi == 0x0) |
| 73 | return; |
| 74 | |
Laurentiu Tudor | a385dd7 | 2017-11-17 15:38:32 +0200 | [diff] [blame] | 75 | if (!owner_mc_dev) |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 76 | return; |
| 77 | |
| 78 | irq_cfg.paddr = ((u64)msi_desc->msg.address_hi << 32) | |
| 79 | msi_desc->msg.address_lo; |
| 80 | irq_cfg.val = msi_desc->msg.data; |
Stuart Yoder | ac06199 | 2016-04-11 11:48:59 -0500 | [diff] [blame] | 81 | irq_cfg.irq_num = msi_desc->irq; |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 82 | |
| 83 | if (owner_mc_dev == mc_bus_dev) { |
| 84 | /* |
| 85 | * IRQ is for the mc_bus_dev's DPRC itself |
| 86 | */ |
| 87 | error = dprc_set_irq(mc_bus_dev->mc_io, |
| 88 | MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI, |
| 89 | mc_bus_dev->mc_handle, |
| 90 | mc_dev_irq->dev_irq_index, |
| 91 | &irq_cfg); |
| 92 | if (error < 0) { |
| 93 | dev_err(&owner_mc_dev->dev, |
| 94 | "dprc_set_irq() failed: %d\n", error); |
| 95 | } |
| 96 | } else { |
| 97 | /* |
| 98 | * IRQ is for for a child device of mc_bus_dev |
| 99 | */ |
| 100 | error = dprc_set_obj_irq(mc_bus_dev->mc_io, |
| 101 | MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI, |
| 102 | mc_bus_dev->mc_handle, |
| 103 | owner_mc_dev->obj_desc.type, |
| 104 | owner_mc_dev->obj_desc.id, |
| 105 | mc_dev_irq->dev_irq_index, |
| 106 | &irq_cfg); |
| 107 | if (error < 0) { |
| 108 | dev_err(&owner_mc_dev->dev, |
| 109 | "dprc_obj_set_irq() failed: %d\n", error); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * NOTE: This function is invoked with interrupts disabled |
| 116 | */ |
| 117 | static void fsl_mc_msi_write_msg(struct irq_data *irq_data, |
| 118 | struct msi_msg *msg) |
| 119 | { |
| 120 | struct msi_desc *msi_desc = irq_data_get_msi_desc(irq_data); |
| 121 | struct fsl_mc_device *mc_bus_dev = to_fsl_mc_device(msi_desc->dev); |
| 122 | struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev); |
| 123 | struct fsl_mc_device_irq *mc_dev_irq = |
| 124 | &mc_bus->irq_resources[msi_desc->fsl_mc.msi_index]; |
| 125 | |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 126 | msi_desc->msg = *msg; |
| 127 | |
| 128 | /* |
| 129 | * Program the MSI (paddr, value) pair in the device: |
| 130 | */ |
| 131 | __fsl_mc_msi_write_msg(mc_bus_dev, mc_dev_irq); |
| 132 | } |
| 133 | |
| 134 | static void fsl_mc_msi_update_chip_ops(struct msi_domain_info *info) |
| 135 | { |
| 136 | struct irq_chip *chip = info->chip; |
| 137 | |
Laurentiu Tudor | a385dd7 | 2017-11-17 15:38:32 +0200 | [diff] [blame] | 138 | if (!chip) |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 139 | return; |
| 140 | |
| 141 | /* |
| 142 | * irq_write_msi_msg should not be set by the caller |
| 143 | */ |
Sandhya Bankar | 905a672 | 2016-09-18 05:07:21 +0530 | [diff] [blame] | 144 | if (!chip->irq_write_msi_msg) |
Matthias Brugger | 33ea58a | 2016-04-14 23:24:26 +0200 | [diff] [blame] | 145 | chip->irq_write_msi_msg = fsl_mc_msi_write_msg; |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * fsl_mc_msi_create_irq_domain - Create a fsl-mc MSI interrupt domain |
| 150 | * @np: Optional device-tree node of the interrupt controller |
| 151 | * @info: MSI domain info |
| 152 | * @parent: Parent irq domain |
| 153 | * |
| 154 | * Updates the domain and chip ops and creates a fsl-mc MSI |
| 155 | * interrupt domain. |
| 156 | * |
| 157 | * Returns: |
| 158 | * A domain pointer or NULL in case of failure. |
| 159 | */ |
| 160 | struct irq_domain *fsl_mc_msi_create_irq_domain(struct fwnode_handle *fwnode, |
| 161 | struct msi_domain_info *info, |
| 162 | struct irq_domain *parent) |
| 163 | { |
| 164 | struct irq_domain *domain; |
| 165 | |
Marc Zyngier | 6988e0e | 2018-05-08 13:14:31 +0100 | [diff] [blame] | 166 | if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE))) |
| 167 | info->flags &= ~MSI_FLAG_LEVEL_CAPABLE; |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 168 | if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS) |
| 169 | fsl_mc_msi_update_dom_ops(info); |
| 170 | if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) |
| 171 | fsl_mc_msi_update_chip_ops(info); |
| 172 | |
| 173 | domain = msi_create_irq_domain(fwnode, info, parent); |
| 174 | if (domain) |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 175 | irq_domain_update_bus_token(domain, DOMAIN_BUS_FSL_MC_MSI); |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 176 | |
| 177 | return domain; |
| 178 | } |
| 179 | |
| 180 | int fsl_mc_find_msi_domain(struct device *mc_platform_dev, |
| 181 | struct irq_domain **mc_msi_domain) |
| 182 | { |
| 183 | struct irq_domain *msi_domain; |
| 184 | struct device_node *mc_of_node = mc_platform_dev->of_node; |
| 185 | |
| 186 | msi_domain = of_msi_get_domain(mc_platform_dev, mc_of_node, |
| 187 | DOMAIN_BUS_FSL_MC_MSI); |
| 188 | if (!msi_domain) { |
Rob Herring | 3c18c44 | 2017-07-18 16:43:33 -0500 | [diff] [blame] | 189 | pr_err("Unable to find fsl-mc MSI domain for %pOF\n", |
| 190 | mc_of_node); |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 191 | |
| 192 | return -ENOENT; |
| 193 | } |
| 194 | |
| 195 | *mc_msi_domain = msi_domain; |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static void fsl_mc_msi_free_descs(struct device *dev) |
| 200 | { |
| 201 | struct msi_desc *desc, *tmp; |
| 202 | |
| 203 | list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) { |
| 204 | list_del(&desc->list); |
| 205 | free_msi_entry(desc); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | static int fsl_mc_msi_alloc_descs(struct device *dev, unsigned int irq_count) |
| 210 | |
| 211 | { |
| 212 | unsigned int i; |
| 213 | int error; |
| 214 | struct msi_desc *msi_desc; |
| 215 | |
| 216 | for (i = 0; i < irq_count; i++) { |
Thomas Gleixner | 28f4b04 | 2016-09-14 16:18:47 +0200 | [diff] [blame] | 217 | msi_desc = alloc_msi_entry(dev, 1, NULL); |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 218 | if (!msi_desc) { |
| 219 | dev_err(dev, "Failed to allocate msi entry\n"); |
| 220 | error = -ENOMEM; |
| 221 | goto cleanup_msi_descs; |
| 222 | } |
| 223 | |
| 224 | msi_desc->fsl_mc.msi_index = i; |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 225 | INIT_LIST_HEAD(&msi_desc->list); |
| 226 | list_add_tail(&msi_desc->list, dev_to_msi_list(dev)); |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | |
| 231 | cleanup_msi_descs: |
| 232 | fsl_mc_msi_free_descs(dev); |
| 233 | return error; |
| 234 | } |
| 235 | |
| 236 | int fsl_mc_msi_domain_alloc_irqs(struct device *dev, |
| 237 | unsigned int irq_count) |
| 238 | { |
| 239 | struct irq_domain *msi_domain; |
| 240 | int error; |
| 241 | |
Laurentiu Tudor | a385dd7 | 2017-11-17 15:38:32 +0200 | [diff] [blame] | 242 | if (!list_empty(dev_to_msi_list(dev))) |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 243 | return -EINVAL; |
| 244 | |
| 245 | error = fsl_mc_msi_alloc_descs(dev, irq_count); |
| 246 | if (error < 0) |
| 247 | return error; |
| 248 | |
| 249 | msi_domain = dev_get_msi_domain(dev); |
Laurentiu Tudor | a385dd7 | 2017-11-17 15:38:32 +0200 | [diff] [blame] | 250 | if (!msi_domain) { |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 251 | error = -EINVAL; |
| 252 | goto cleanup_msi_descs; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * NOTE: Calling this function will trigger the invocation of the |
| 257 | * its_fsl_mc_msi_prepare() callback |
| 258 | */ |
| 259 | error = msi_domain_alloc_irqs(msi_domain, dev, irq_count); |
| 260 | |
| 261 | if (error) { |
| 262 | dev_err(dev, "Failed to allocate IRQs\n"); |
| 263 | goto cleanup_msi_descs; |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | |
| 268 | cleanup_msi_descs: |
| 269 | fsl_mc_msi_free_descs(dev); |
| 270 | return error; |
| 271 | } |
| 272 | |
| 273 | void fsl_mc_msi_domain_free_irqs(struct device *dev) |
| 274 | { |
| 275 | struct irq_domain *msi_domain; |
| 276 | |
| 277 | msi_domain = dev_get_msi_domain(dev); |
Laurentiu Tudor | a385dd7 | 2017-11-17 15:38:32 +0200 | [diff] [blame] | 278 | if (!msi_domain) |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 279 | return; |
| 280 | |
| 281 | msi_domain_free_irqs(msi_domain, dev); |
| 282 | |
Laurentiu Tudor | a385dd7 | 2017-11-17 15:38:32 +0200 | [diff] [blame] | 283 | if (list_empty(dev_to_msi_list(dev))) |
J. German Rivera | 679c2c7 | 2016-01-06 16:03:21 -0600 | [diff] [blame] | 284 | return; |
| 285 | |
| 286 | fsl_mc_msi_free_descs(dev); |
| 287 | } |