1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel IXP4xx Queue Manager driver for Linux
5 * Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>
8 #include <linux/ioport.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/soc/ixp4xx/qmgr.h>
16 static struct qmgr_regs __iomem
*qmgr_regs
;
17 static int qmgr_irq_1
;
18 static int qmgr_irq_2
;
19 static spinlock_t qmgr_lock
;
20 static u32 used_sram_bitmap
[4]; /* 128 16-dword pages */
21 static void (*irq_handlers
[QUEUES
])(void *pdev
);
22 static void *irq_pdevs
[QUEUES
];
25 char qmgr_queue_descs
[QUEUES
][32];
28 void qmgr_put_entry(unsigned int queue
, u32 val
)
31 BUG_ON(!qmgr_queue_descs
[queue
]); /* not yet requested */
33 printk(KERN_DEBUG
"Queue %s(%i) put %X\n",
34 qmgr_queue_descs
[queue
], queue
, val
);
36 __raw_writel(val
, &qmgr_regs
->acc
[queue
][0]);
39 u32
qmgr_get_entry(unsigned int queue
)
42 val
= __raw_readl(&qmgr_regs
->acc
[queue
][0]);
44 BUG_ON(!qmgr_queue_descs
[queue
]); /* not yet requested */
46 printk(KERN_DEBUG
"Queue %s(%i) get %X\n",
47 qmgr_queue_descs
[queue
], queue
, val
);
52 static int __qmgr_get_stat1(unsigned int queue
)
54 return (__raw_readl(&qmgr_regs
->stat1
[queue
>> 3])
55 >> ((queue
& 7) << 2)) & 0xF;
58 static int __qmgr_get_stat2(unsigned int queue
)
60 BUG_ON(queue
>= HALF_QUEUES
);
61 return (__raw_readl(&qmgr_regs
->stat2
[queue
>> 4])
62 >> ((queue
& 0xF) << 1)) & 0x3;
66 * qmgr_stat_empty() - checks if a hardware queue is empty
67 * @queue: queue number
69 * Returns non-zero value if the queue is empty.
71 int qmgr_stat_empty(unsigned int queue
)
73 BUG_ON(queue
>= HALF_QUEUES
);
74 return __qmgr_get_stat1(queue
) & QUEUE_STAT1_EMPTY
;
78 * qmgr_stat_below_low_watermark() - checks if a queue is below low watermark
79 * @queue: queue number
81 * Returns non-zero value if the queue is below low watermark.
83 int qmgr_stat_below_low_watermark(unsigned int queue
)
85 if (queue
>= HALF_QUEUES
)
86 return (__raw_readl(&qmgr_regs
->statne_h
) >>
87 (queue
- HALF_QUEUES
)) & 0x01;
88 return __qmgr_get_stat1(queue
) & QUEUE_STAT1_NEARLY_EMPTY
;
92 * qmgr_stat_full() - checks if a hardware queue is full
93 * @queue: queue number
95 * Returns non-zero value if the queue is full.
97 int qmgr_stat_full(unsigned int queue
)
99 if (queue
>= HALF_QUEUES
)
100 return (__raw_readl(&qmgr_regs
->statf_h
) >>
101 (queue
- HALF_QUEUES
)) & 0x01;
102 return __qmgr_get_stat1(queue
) & QUEUE_STAT1_FULL
;
106 * qmgr_stat_overflow() - checks if a hardware queue experienced overflow
107 * @queue: queue number
109 * Returns non-zero value if the queue experienced overflow.
111 int qmgr_stat_overflow(unsigned int queue
)
113 return __qmgr_get_stat2(queue
) & QUEUE_STAT2_OVERFLOW
;
116 void qmgr_set_irq(unsigned int queue
, int src
,
117 void (*handler
)(void *pdev
), void *pdev
)
121 spin_lock_irqsave(&qmgr_lock
, flags
);
122 if (queue
< HALF_QUEUES
) {
125 BUG_ON(src
> QUEUE_IRQ_SRC_NOT_FULL
);
126 reg
= &qmgr_regs
->irqsrc
[queue
>> 3]; /* 8 queues per u32 */
127 bit
= (queue
% 8) * 4; /* 3 bits + 1 reserved bit per queue */
128 __raw_writel((__raw_readl(reg
) & ~(7 << bit
)) | (src
<< bit
),
131 /* IRQ source for queues 32-63 is fixed */
132 BUG_ON(src
!= QUEUE_IRQ_SRC_NOT_NEARLY_EMPTY
);
134 irq_handlers
[queue
] = handler
;
135 irq_pdevs
[queue
] = pdev
;
136 spin_unlock_irqrestore(&qmgr_lock
, flags
);
140 static irqreturn_t
qmgr_irq1_a0(int irq
, void *pdev
)
143 u32 en_bitmap
, src
, stat
;
145 /* ACK - it may clear any bits so don't rely on it */
146 __raw_writel(0xFFFFFFFF, &qmgr_regs
->irqstat
[0]);
148 en_bitmap
= qmgr_regs
->irqen
[0];
150 i
= __fls(en_bitmap
); /* number of the last "low" queue */
151 en_bitmap
&= ~BIT(i
);
152 src
= qmgr_regs
->irqsrc
[i
>> 3];
153 stat
= qmgr_regs
->stat1
[i
>> 3];
154 if (src
& 4) /* the IRQ condition is inverted */
156 if (stat
& BIT(src
& 3)) {
157 irq_handlers
[i
](irq_pdevs
[i
]);
165 static irqreturn_t
qmgr_irq2_a0(int irq
, void *pdev
)
170 /* ACK - it may clear any bits so don't rely on it */
171 __raw_writel(0xFFFFFFFF, &qmgr_regs
->irqstat
[1]);
173 req_bitmap
= qmgr_regs
->irqen
[1] & qmgr_regs
->statne_h
;
175 i
= __fls(req_bitmap
); /* number of the last "high" queue */
176 req_bitmap
&= ~BIT(i
);
177 irq_handlers
[HALF_QUEUES
+ i
](irq_pdevs
[HALF_QUEUES
+ i
]);
184 static irqreturn_t
qmgr_irq(int irq
, void *pdev
)
186 int i
, half
= (irq
== qmgr_irq_1
? 0 : 1);
187 u32 req_bitmap
= __raw_readl(&qmgr_regs
->irqstat
[half
]);
191 __raw_writel(req_bitmap
, &qmgr_regs
->irqstat
[half
]); /* ACK */
194 i
= __fls(req_bitmap
); /* number of the last queue */
195 req_bitmap
&= ~BIT(i
);
196 i
+= half
* HALF_QUEUES
;
197 irq_handlers
[i
](irq_pdevs
[i
]);
203 void qmgr_enable_irq(unsigned int queue
)
206 int half
= queue
/ 32;
207 u32 mask
= 1 << (queue
& (HALF_QUEUES
- 1));
209 spin_lock_irqsave(&qmgr_lock
, flags
);
210 __raw_writel(__raw_readl(&qmgr_regs
->irqen
[half
]) | mask
,
211 &qmgr_regs
->irqen
[half
]);
212 spin_unlock_irqrestore(&qmgr_lock
, flags
);
215 void qmgr_disable_irq(unsigned int queue
)
218 int half
= queue
/ 32;
219 u32 mask
= 1 << (queue
& (HALF_QUEUES
- 1));
221 spin_lock_irqsave(&qmgr_lock
, flags
);
222 __raw_writel(__raw_readl(&qmgr_regs
->irqen
[half
]) & ~mask
,
223 &qmgr_regs
->irqen
[half
]);
224 __raw_writel(mask
, &qmgr_regs
->irqstat
[half
]); /* clear */
225 spin_unlock_irqrestore(&qmgr_lock
, flags
);
228 static inline void shift_mask(u32
*mask
)
230 mask
[3] = mask
[3] << 1 | mask
[2] >> 31;
231 mask
[2] = mask
[2] << 1 | mask
[1] >> 31;
232 mask
[1] = mask
[1] << 1 | mask
[0] >> 31;
237 int qmgr_request_queue(unsigned int queue
, unsigned int len
/* dwords */,
238 unsigned int nearly_empty_watermark
,
239 unsigned int nearly_full_watermark
,
240 const char *desc_format
, const char* name
)
242 int __qmgr_request_queue(unsigned int queue
, unsigned int len
/* dwords */,
243 unsigned int nearly_empty_watermark
,
244 unsigned int nearly_full_watermark
)
247 u32 cfg
, addr
= 0, mask
[4]; /* in 16-dwords */
250 BUG_ON(queue
>= QUEUES
);
252 if ((nearly_empty_watermark
| nearly_full_watermark
) & ~7)
276 cfg
|= nearly_empty_watermark
<< 26;
277 cfg
|= nearly_full_watermark
<< 29;
278 len
/= 16; /* in 16-dwords: 1, 2, 4 or 8 */
279 mask
[1] = mask
[2] = mask
[3] = 0;
281 if (!try_module_get(THIS_MODULE
))
284 spin_lock_irq(&qmgr_lock
);
285 if (__raw_readl(&qmgr_regs
->sram
[queue
])) {
291 if (!(used_sram_bitmap
[0] & mask
[0]) &&
292 !(used_sram_bitmap
[1] & mask
[1]) &&
293 !(used_sram_bitmap
[2] & mask
[2]) &&
294 !(used_sram_bitmap
[3] & mask
[3]))
295 break; /* found free space */
299 if (addr
+ len
> ARRAY_SIZE(qmgr_regs
->sram
)) {
300 printk(KERN_ERR
"qmgr: no free SRAM space for"
301 " queue %i\n", queue
);
307 used_sram_bitmap
[0] |= mask
[0];
308 used_sram_bitmap
[1] |= mask
[1];
309 used_sram_bitmap
[2] |= mask
[2];
310 used_sram_bitmap
[3] |= mask
[3];
311 __raw_writel(cfg
| (addr
<< 14), &qmgr_regs
->sram
[queue
]);
313 snprintf(qmgr_queue_descs
[queue
], sizeof(qmgr_queue_descs
[0]),
315 printk(KERN_DEBUG
"qmgr: requested queue %s(%i) addr = 0x%02X\n",
316 qmgr_queue_descs
[queue
], queue
, addr
);
318 spin_unlock_irq(&qmgr_lock
);
322 spin_unlock_irq(&qmgr_lock
);
323 module_put(THIS_MODULE
);
327 void qmgr_release_queue(unsigned int queue
)
329 u32 cfg
, addr
, mask
[4];
331 BUG_ON(queue
>= QUEUES
); /* not in valid range */
333 spin_lock_irq(&qmgr_lock
);
334 cfg
= __raw_readl(&qmgr_regs
->sram
[queue
]);
335 addr
= (cfg
>> 14) & 0xFF;
337 BUG_ON(!addr
); /* not requested */
339 switch ((cfg
>> 24) & 3) {
340 case 0: mask
[0] = 0x1; break;
341 case 1: mask
[0] = 0x3; break;
342 case 2: mask
[0] = 0xF; break;
343 case 3: mask
[0] = 0xFF; break;
346 mask
[1] = mask
[2] = mask
[3] = 0;
352 printk(KERN_DEBUG
"qmgr: releasing queue %s(%i)\n",
353 qmgr_queue_descs
[queue
], queue
);
354 qmgr_queue_descs
[queue
][0] = '\x0';
357 while ((addr
= qmgr_get_entry(queue
)))
358 printk(KERN_ERR
"qmgr: released queue %i not empty: 0x%08X\n",
361 __raw_writel(0, &qmgr_regs
->sram
[queue
]);
363 used_sram_bitmap
[0] &= ~mask
[0];
364 used_sram_bitmap
[1] &= ~mask
[1];
365 used_sram_bitmap
[2] &= ~mask
[2];
366 used_sram_bitmap
[3] &= ~mask
[3];
367 irq_handlers
[queue
] = NULL
; /* catch IRQ bugs */
368 spin_unlock_irq(&qmgr_lock
);
370 module_put(THIS_MODULE
);
373 static int ixp4xx_qmgr_probe(struct platform_device
*pdev
)
376 irq_handler_t handler1
, handler2
;
377 struct device
*dev
= &pdev
->dev
;
378 struct resource
*res
;
381 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
384 qmgr_regs
= devm_ioremap_resource(dev
, res
);
385 if (IS_ERR(qmgr_regs
))
386 return PTR_ERR(qmgr_regs
);
388 irq1
= platform_get_irq(pdev
, 0);
390 return irq1
? irq1
: -EINVAL
;
392 irq2
= platform_get_irq(pdev
, 1);
394 return irq2
? irq2
: -EINVAL
;
397 /* reset qmgr registers */
398 for (i
= 0; i
< 4; i
++) {
399 __raw_writel(0x33333333, &qmgr_regs
->stat1
[i
]);
400 __raw_writel(0, &qmgr_regs
->irqsrc
[i
]);
402 for (i
= 0; i
< 2; i
++) {
403 __raw_writel(0, &qmgr_regs
->stat2
[i
]);
404 __raw_writel(0xFFFFFFFF, &qmgr_regs
->irqstat
[i
]); /* clear */
405 __raw_writel(0, &qmgr_regs
->irqen
[i
]);
408 __raw_writel(0xFFFFFFFF, &qmgr_regs
->statne_h
);
409 __raw_writel(0, &qmgr_regs
->statf_h
);
411 for (i
= 0; i
< QUEUES
; i
++)
412 __raw_writel(0, &qmgr_regs
->sram
[i
]);
414 if (cpu_is_ixp42x_rev_a0()) {
415 handler1
= qmgr_irq1_a0
;
416 handler2
= qmgr_irq2_a0
;
418 handler1
= handler2
= qmgr_irq
;
420 err
= devm_request_irq(dev
, irq1
, handler1
, 0, "IXP4xx Queue Manager",
423 dev_err(dev
, "failed to request IRQ%i (%i)\n",
428 err
= devm_request_irq(dev
, irq2
, handler2
, 0, "IXP4xx Queue Manager",
431 dev_err(dev
, "failed to request IRQ%i (%i)\n",
436 used_sram_bitmap
[0] = 0xF; /* 4 first pages reserved for config */
437 spin_lock_init(&qmgr_lock
);
439 dev_info(dev
, "IXP4xx Queue Manager initialized.\n");
443 static int ixp4xx_qmgr_remove(struct platform_device
*pdev
)
445 synchronize_irq(qmgr_irq_1
);
446 synchronize_irq(qmgr_irq_2
);
450 static const struct of_device_id ixp4xx_qmgr_of_match
[] = {
452 .compatible
= "intel,ixp4xx-ahb-queue-manager",
457 static struct platform_driver ixp4xx_qmgr_driver
= {
459 .name
= "ixp4xx-qmgr",
460 .of_match_table
= of_match_ptr(ixp4xx_qmgr_of_match
),
462 .probe
= ixp4xx_qmgr_probe
,
463 .remove
= ixp4xx_qmgr_remove
,
465 module_platform_driver(ixp4xx_qmgr_driver
);
467 MODULE_LICENSE("GPL v2");
468 MODULE_AUTHOR("Krzysztof Halasa");
470 EXPORT_SYMBOL(qmgr_put_entry
);
471 EXPORT_SYMBOL(qmgr_get_entry
);
472 EXPORT_SYMBOL(qmgr_stat_empty
);
473 EXPORT_SYMBOL(qmgr_stat_below_low_watermark
);
474 EXPORT_SYMBOL(qmgr_stat_full
);
475 EXPORT_SYMBOL(qmgr_stat_overflow
);
476 EXPORT_SYMBOL(qmgr_set_irq
);
477 EXPORT_SYMBOL(qmgr_enable_irq
);
478 EXPORT_SYMBOL(qmgr_disable_irq
);
480 EXPORT_SYMBOL(qmgr_queue_descs
);
481 EXPORT_SYMBOL(qmgr_request_queue
);
483 EXPORT_SYMBOL(__qmgr_request_queue
);
485 EXPORT_SYMBOL(qmgr_release_queue
);