1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Intel IXP4xx Queue Manager driver for Linux 4 * 5 * Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl> 6 */ 7 8 #include <linux/ioport.h> 9 #include <linux/interrupt.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/soc/ixp4xx/qmgr.h> 15 #include <mach/hardware.h> 16 #include <mach/cpu.h> 17 18 static struct qmgr_regs __iomem *qmgr_regs; 19 static int qmgr_irq_1; 20 static int qmgr_irq_2; 21 static spinlock_t qmgr_lock; 22 static u32 used_sram_bitmap[4]; /* 128 16-dword pages */ 23 static void (*irq_handlers[QUEUES])(void *pdev); 24 static void *irq_pdevs[QUEUES]; 25 26 #if DEBUG_QMGR 27 char qmgr_queue_descs[QUEUES][32]; 28 #endif 29 30 void qmgr_put_entry(unsigned int queue, u32 val) 31 { 32 #if DEBUG_QMGR 33 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */ 34 35 printk(KERN_DEBUG "Queue %s(%i) put %X\n", 36 qmgr_queue_descs[queue], queue, val); 37 #endif 38 __raw_writel(val, &qmgr_regs->acc[queue][0]); 39 } 40 41 u32 qmgr_get_entry(unsigned int queue) 42 { 43 u32 val; 44 val = __raw_readl(&qmgr_regs->acc[queue][0]); 45 #if DEBUG_QMGR 46 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */ 47 48 printk(KERN_DEBUG "Queue %s(%i) get %X\n", 49 qmgr_queue_descs[queue], queue, val); 50 #endif 51 return val; 52 } 53 54 static int __qmgr_get_stat1(unsigned int queue) 55 { 56 return (__raw_readl(&qmgr_regs->stat1[queue >> 3]) 57 >> ((queue & 7) << 2)) & 0xF; 58 } 59 60 static int __qmgr_get_stat2(unsigned int queue) 61 { 62 BUG_ON(queue >= HALF_QUEUES); 63 return (__raw_readl(&qmgr_regs->stat2[queue >> 4]) 64 >> ((queue & 0xF) << 1)) & 0x3; 65 } 66 67 /** 68 * qmgr_stat_empty() - checks if a hardware queue is empty 69 * @queue: queue number 70 * 71 * Returns non-zero value if the queue is empty. 72 */ 73 int qmgr_stat_empty(unsigned int queue) 74 { 75 BUG_ON(queue >= HALF_QUEUES); 76 return __qmgr_get_stat1(queue) & QUEUE_STAT1_EMPTY; 77 } 78 79 /** 80 * qmgr_stat_below_low_watermark() - checks if a queue is below low watermark 81 * @queue: queue number 82 * 83 * Returns non-zero value if the queue is below low watermark. 84 */ 85 int qmgr_stat_below_low_watermark(unsigned int queue) 86 { 87 if (queue >= HALF_QUEUES) 88 return (__raw_readl(&qmgr_regs->statne_h) >> 89 (queue - HALF_QUEUES)) & 0x01; 90 return __qmgr_get_stat1(queue) & QUEUE_STAT1_NEARLY_EMPTY; 91 } 92 93 /** 94 * qmgr_stat_full() - checks if a hardware queue is full 95 * @queue: queue number 96 * 97 * Returns non-zero value if the queue is full. 98 */ 99 int qmgr_stat_full(unsigned int queue) 100 { 101 if (queue >= HALF_QUEUES) 102 return (__raw_readl(&qmgr_regs->statf_h) >> 103 (queue - HALF_QUEUES)) & 0x01; 104 return __qmgr_get_stat1(queue) & QUEUE_STAT1_FULL; 105 } 106 107 /** 108 * qmgr_stat_overflow() - checks if a hardware queue experienced overflow 109 * @queue: queue number 110 * 111 * Returns non-zero value if the queue experienced overflow. 112 */ 113 int qmgr_stat_overflow(unsigned int queue) 114 { 115 return __qmgr_get_stat2(queue) & QUEUE_STAT2_OVERFLOW; 116 } 117 118 void qmgr_set_irq(unsigned int queue, int src, 119 void (*handler)(void *pdev), void *pdev) 120 { 121 unsigned long flags; 122 123 spin_lock_irqsave(&qmgr_lock, flags); 124 if (queue < HALF_QUEUES) { 125 u32 __iomem *reg; 126 int bit; 127 BUG_ON(src > QUEUE_IRQ_SRC_NOT_FULL); 128 reg = &qmgr_regs->irqsrc[queue >> 3]; /* 8 queues per u32 */ 129 bit = (queue % 8) * 4; /* 3 bits + 1 reserved bit per queue */ 130 __raw_writel((__raw_readl(reg) & ~(7 << bit)) | (src << bit), 131 reg); 132 } else 133 /* IRQ source for queues 32-63 is fixed */ 134 BUG_ON(src != QUEUE_IRQ_SRC_NOT_NEARLY_EMPTY); 135 136 irq_handlers[queue] = handler; 137 irq_pdevs[queue] = pdev; 138 spin_unlock_irqrestore(&qmgr_lock, flags); 139 } 140 141 142 static irqreturn_t qmgr_irq1_a0(int irq, void *pdev) 143 { 144 int i, ret = 0; 145 u32 en_bitmap, src, stat; 146 147 /* ACK - it may clear any bits so don't rely on it */ 148 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[0]); 149 150 en_bitmap = qmgr_regs->irqen[0]; 151 while (en_bitmap) { 152 i = __fls(en_bitmap); /* number of the last "low" queue */ 153 en_bitmap &= ~BIT(i); 154 src = qmgr_regs->irqsrc[i >> 3]; 155 stat = qmgr_regs->stat1[i >> 3]; 156 if (src & 4) /* the IRQ condition is inverted */ 157 stat = ~stat; 158 if (stat & BIT(src & 3)) { 159 irq_handlers[i](irq_pdevs[i]); 160 ret = IRQ_HANDLED; 161 } 162 } 163 return ret; 164 } 165 166 167 static irqreturn_t qmgr_irq2_a0(int irq, void *pdev) 168 { 169 int i, ret = 0; 170 u32 req_bitmap; 171 172 /* ACK - it may clear any bits so don't rely on it */ 173 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[1]); 174 175 req_bitmap = qmgr_regs->irqen[1] & qmgr_regs->statne_h; 176 while (req_bitmap) { 177 i = __fls(req_bitmap); /* number of the last "high" queue */ 178 req_bitmap &= ~BIT(i); 179 irq_handlers[HALF_QUEUES + i](irq_pdevs[HALF_QUEUES + i]); 180 ret = IRQ_HANDLED; 181 } 182 return ret; 183 } 184 185 186 static irqreturn_t qmgr_irq(int irq, void *pdev) 187 { 188 int i, half = (irq == qmgr_irq_1 ? 0 : 1); 189 u32 req_bitmap = __raw_readl(&qmgr_regs->irqstat[half]); 190 191 if (!req_bitmap) 192 return 0; 193 __raw_writel(req_bitmap, &qmgr_regs->irqstat[half]); /* ACK */ 194 195 while (req_bitmap) { 196 i = __fls(req_bitmap); /* number of the last queue */ 197 req_bitmap &= ~BIT(i); 198 i += half * HALF_QUEUES; 199 irq_handlers[i](irq_pdevs[i]); 200 } 201 return IRQ_HANDLED; 202 } 203 204 205 void qmgr_enable_irq(unsigned int queue) 206 { 207 unsigned long flags; 208 int half = queue / 32; 209 u32 mask = 1 << (queue & (HALF_QUEUES - 1)); 210 211 spin_lock_irqsave(&qmgr_lock, flags); 212 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) | mask, 213 &qmgr_regs->irqen[half]); 214 spin_unlock_irqrestore(&qmgr_lock, flags); 215 } 216 217 void qmgr_disable_irq(unsigned int queue) 218 { 219 unsigned long flags; 220 int half = queue / 32; 221 u32 mask = 1 << (queue & (HALF_QUEUES - 1)); 222 223 spin_lock_irqsave(&qmgr_lock, flags); 224 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) & ~mask, 225 &qmgr_regs->irqen[half]); 226 __raw_writel(mask, &qmgr_regs->irqstat[half]); /* clear */ 227 spin_unlock_irqrestore(&qmgr_lock, flags); 228 } 229 230 static inline void shift_mask(u32 *mask) 231 { 232 mask[3] = mask[3] << 1 | mask[2] >> 31; 233 mask[2] = mask[2] << 1 | mask[1] >> 31; 234 mask[1] = mask[1] << 1 | mask[0] >> 31; 235 mask[0] <<= 1; 236 } 237 238 #if DEBUG_QMGR 239 int qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */, 240 unsigned int nearly_empty_watermark, 241 unsigned int nearly_full_watermark, 242 const char *desc_format, const char* name) 243 #else 244 int __qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */, 245 unsigned int nearly_empty_watermark, 246 unsigned int nearly_full_watermark) 247 #endif 248 { 249 u32 cfg, addr = 0, mask[4]; /* in 16-dwords */ 250 int err; 251 252 BUG_ON(queue >= QUEUES); 253 254 if ((nearly_empty_watermark | nearly_full_watermark) & ~7) 255 return -EINVAL; 256 257 switch (len) { 258 case 16: 259 cfg = 0 << 24; 260 mask[0] = 0x1; 261 break; 262 case 32: 263 cfg = 1 << 24; 264 mask[0] = 0x3; 265 break; 266 case 64: 267 cfg = 2 << 24; 268 mask[0] = 0xF; 269 break; 270 case 128: 271 cfg = 3 << 24; 272 mask[0] = 0xFF; 273 break; 274 default: 275 return -EINVAL; 276 } 277 278 cfg |= nearly_empty_watermark << 26; 279 cfg |= nearly_full_watermark << 29; 280 len /= 16; /* in 16-dwords: 1, 2, 4 or 8 */ 281 mask[1] = mask[2] = mask[3] = 0; 282 283 if (!try_module_get(THIS_MODULE)) 284 return -ENODEV; 285 286 spin_lock_irq(&qmgr_lock); 287 if (__raw_readl(&qmgr_regs->sram[queue])) { 288 err = -EBUSY; 289 goto err; 290 } 291 292 while (1) { 293 if (!(used_sram_bitmap[0] & mask[0]) && 294 !(used_sram_bitmap[1] & mask[1]) && 295 !(used_sram_bitmap[2] & mask[2]) && 296 !(used_sram_bitmap[3] & mask[3])) 297 break; /* found free space */ 298 299 addr++; 300 shift_mask(mask); 301 if (addr + len > ARRAY_SIZE(qmgr_regs->sram)) { 302 printk(KERN_ERR "qmgr: no free SRAM space for" 303 " queue %i\n", queue); 304 err = -ENOMEM; 305 goto err; 306 } 307 } 308 309 used_sram_bitmap[0] |= mask[0]; 310 used_sram_bitmap[1] |= mask[1]; 311 used_sram_bitmap[2] |= mask[2]; 312 used_sram_bitmap[3] |= mask[3]; 313 __raw_writel(cfg | (addr << 14), &qmgr_regs->sram[queue]); 314 #if DEBUG_QMGR 315 snprintf(qmgr_queue_descs[queue], sizeof(qmgr_queue_descs[0]), 316 desc_format, name); 317 printk(KERN_DEBUG "qmgr: requested queue %s(%i) addr = 0x%02X\n", 318 qmgr_queue_descs[queue], queue, addr); 319 #endif 320 spin_unlock_irq(&qmgr_lock); 321 return 0; 322 323 err: 324 spin_unlock_irq(&qmgr_lock); 325 module_put(THIS_MODULE); 326 return err; 327 } 328 329 void qmgr_release_queue(unsigned int queue) 330 { 331 u32 cfg, addr, mask[4]; 332 333 BUG_ON(queue >= QUEUES); /* not in valid range */ 334 335 spin_lock_irq(&qmgr_lock); 336 cfg = __raw_readl(&qmgr_regs->sram[queue]); 337 addr = (cfg >> 14) & 0xFF; 338 339 BUG_ON(!addr); /* not requested */ 340 341 switch ((cfg >> 24) & 3) { 342 case 0: mask[0] = 0x1; break; 343 case 1: mask[0] = 0x3; break; 344 case 2: mask[0] = 0xF; break; 345 case 3: mask[0] = 0xFF; break; 346 } 347 348 mask[1] = mask[2] = mask[3] = 0; 349 350 while (addr--) 351 shift_mask(mask); 352 353 #if DEBUG_QMGR 354 printk(KERN_DEBUG "qmgr: releasing queue %s(%i)\n", 355 qmgr_queue_descs[queue], queue); 356 qmgr_queue_descs[queue][0] = '\x0'; 357 #endif 358 359 while ((addr = qmgr_get_entry(queue))) 360 printk(KERN_ERR "qmgr: released queue %i not empty: 0x%08X\n", 361 queue, addr); 362 363 __raw_writel(0, &qmgr_regs->sram[queue]); 364 365 used_sram_bitmap[0] &= ~mask[0]; 366 used_sram_bitmap[1] &= ~mask[1]; 367 used_sram_bitmap[2] &= ~mask[2]; 368 used_sram_bitmap[3] &= ~mask[3]; 369 irq_handlers[queue] = NULL; /* catch IRQ bugs */ 370 spin_unlock_irq(&qmgr_lock); 371 372 module_put(THIS_MODULE); 373 } 374 375 static int ixp4xx_qmgr_probe(struct platform_device *pdev) 376 { 377 int i, err; 378 irq_handler_t handler1, handler2; 379 struct device *dev = &pdev->dev; 380 struct resource *res; 381 int irq1, irq2; 382 383 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 384 if (!res) 385 return -ENODEV; 386 qmgr_regs = devm_ioremap_resource(dev, res); 387 if (IS_ERR(qmgr_regs)) 388 return PTR_ERR(qmgr_regs); 389 390 irq1 = platform_get_irq(pdev, 0); 391 if (irq1 <= 0) 392 return irq1 ? irq1 : -EINVAL; 393 qmgr_irq_1 = irq1; 394 irq2 = platform_get_irq(pdev, 1); 395 if (irq2 <= 0) 396 return irq2 ? irq2 : -EINVAL; 397 qmgr_irq_2 = irq2; 398 399 /* reset qmgr registers */ 400 for (i = 0; i < 4; i++) { 401 __raw_writel(0x33333333, &qmgr_regs->stat1[i]); 402 __raw_writel(0, &qmgr_regs->irqsrc[i]); 403 } 404 for (i = 0; i < 2; i++) { 405 __raw_writel(0, &qmgr_regs->stat2[i]); 406 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[i]); /* clear */ 407 __raw_writel(0, &qmgr_regs->irqen[i]); 408 } 409 410 __raw_writel(0xFFFFFFFF, &qmgr_regs->statne_h); 411 __raw_writel(0, &qmgr_regs->statf_h); 412 413 for (i = 0; i < QUEUES; i++) 414 __raw_writel(0, &qmgr_regs->sram[i]); 415 416 if (cpu_is_ixp42x_rev_a0()) { 417 handler1 = qmgr_irq1_a0; 418 handler2 = qmgr_irq2_a0; 419 } else 420 handler1 = handler2 = qmgr_irq; 421 422 err = devm_request_irq(dev, irq1, handler1, 0, "IXP4xx Queue Manager", 423 NULL); 424 if (err) { 425 dev_err(dev, "failed to request IRQ%i (%i)\n", 426 irq1, err); 427 return err; 428 } 429 430 err = devm_request_irq(dev, irq2, handler2, 0, "IXP4xx Queue Manager", 431 NULL); 432 if (err) { 433 dev_err(dev, "failed to request IRQ%i (%i)\n", 434 irq2, err); 435 return err; 436 } 437 438 used_sram_bitmap[0] = 0xF; /* 4 first pages reserved for config */ 439 spin_lock_init(&qmgr_lock); 440 441 dev_info(dev, "IXP4xx Queue Manager initialized.\n"); 442 return 0; 443 } 444 445 static int ixp4xx_qmgr_remove(struct platform_device *pdev) 446 { 447 synchronize_irq(qmgr_irq_1); 448 synchronize_irq(qmgr_irq_2); 449 return 0; 450 } 451 452 static const struct of_device_id ixp4xx_qmgr_of_match[] = { 453 { 454 .compatible = "intel,ixp4xx-ahb-queue-manager", 455 }, 456 {}, 457 }; 458 459 static struct platform_driver ixp4xx_qmgr_driver = { 460 .driver = { 461 .name = "ixp4xx-qmgr", 462 .of_match_table = of_match_ptr(ixp4xx_qmgr_of_match), 463 }, 464 .probe = ixp4xx_qmgr_probe, 465 .remove = ixp4xx_qmgr_remove, 466 }; 467 module_platform_driver(ixp4xx_qmgr_driver); 468 469 MODULE_LICENSE("GPL v2"); 470 MODULE_AUTHOR("Krzysztof Halasa"); 471 472 EXPORT_SYMBOL(qmgr_put_entry); 473 EXPORT_SYMBOL(qmgr_get_entry); 474 EXPORT_SYMBOL(qmgr_stat_empty); 475 EXPORT_SYMBOL(qmgr_stat_below_low_watermark); 476 EXPORT_SYMBOL(qmgr_stat_full); 477 EXPORT_SYMBOL(qmgr_stat_overflow); 478 EXPORT_SYMBOL(qmgr_set_irq); 479 EXPORT_SYMBOL(qmgr_enable_irq); 480 EXPORT_SYMBOL(qmgr_disable_irq); 481 #if DEBUG_QMGR 482 EXPORT_SYMBOL(qmgr_queue_descs); 483 EXPORT_SYMBOL(qmgr_request_queue); 484 #else 485 EXPORT_SYMBOL(__qmgr_request_queue); 486 #endif 487 EXPORT_SYMBOL(qmgr_release_queue); 488