1 // SPDX-License-Identifier: GPL-2.0
2 /* pci_msi.c: Sparc64 MSI support common layer.
4 * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/interrupt.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/irq.h>
15 static irqreturn_t
sparc64_msiq_interrupt(int irq
, void *cookie
)
17 struct sparc64_msiq_cookie
*msiq_cookie
= cookie
;
18 struct pci_pbm_info
*pbm
= msiq_cookie
->pbm
;
19 unsigned long msiqid
= msiq_cookie
->msiqid
;
20 const struct sparc64_msiq_ops
*ops
;
21 unsigned long orig_head
, head
;
26 err
= ops
->get_head(pbm
, msiqid
, &head
);
27 if (unlikely(err
< 0))
34 err
= ops
->dequeue_msi(pbm
, msiqid
, &head
, &msi
);
35 if (likely(err
> 0)) {
38 irq
= pbm
->msi_irq_table
[msi
- pbm
->msi_first
];
39 generic_handle_irq(irq
);
42 if (unlikely(err
< 0))
48 if (likely(head
!= orig_head
)) {
49 err
= ops
->set_head(pbm
, msiqid
, head
);
50 if (unlikely(err
< 0))
56 printk(KERN_EMERG
"MSI: Get head on msiqid[%lu] gives error %d\n",
61 printk(KERN_EMERG
"MSI: Dequeue head[%lu] from msiqid[%lu] "
67 printk(KERN_EMERG
"MSI: Set head[%lu] on msiqid[%lu] "
76 static u32
pick_msiq(struct pci_pbm_info
*pbm
)
78 static DEFINE_SPINLOCK(rotor_lock
);
82 spin_lock_irqsave(&rotor_lock
, flags
);
84 rotor
= pbm
->msiq_rotor
;
85 ret
= pbm
->msiq_first
+ rotor
;
87 if (++rotor
>= pbm
->msiq_num
)
89 pbm
->msiq_rotor
= rotor
;
91 spin_unlock_irqrestore(&rotor_lock
, flags
);
97 static int alloc_msi(struct pci_pbm_info
*pbm
)
101 for (i
= 0; i
< pbm
->msi_num
; i
++) {
102 if (!test_and_set_bit(i
, pbm
->msi_bitmap
))
103 return i
+ pbm
->msi_first
;
109 static void free_msi(struct pci_pbm_info
*pbm
, int msi_num
)
111 msi_num
-= pbm
->msi_first
;
112 clear_bit(msi_num
, pbm
->msi_bitmap
);
115 static struct irq_chip msi_irq
= {
117 .irq_mask
= pci_msi_mask_irq
,
118 .irq_unmask
= pci_msi_unmask_irq
,
119 .irq_enable
= pci_msi_unmask_irq
,
120 .irq_disable
= pci_msi_mask_irq
,
121 /* XXX affinity XXX */
124 static int sparc64_setup_msi_irq(unsigned int *irq_p
,
125 struct pci_dev
*pdev
,
126 struct msi_desc
*entry
)
128 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
129 const struct sparc64_msiq_ops
*ops
= pbm
->msi_ops
;
134 *irq_p
= irq_alloc(0, 0);
139 irq_set_chip_and_handler_name(*irq_p
, &msi_irq
, handle_simple_irq
,
142 err
= alloc_msi(pbm
);
143 if (unlikely(err
< 0))
148 msiqid
= pick_msiq(pbm
);
150 err
= ops
->msi_setup(pbm
, msiqid
, msi
,
151 (entry
->pci
.msi_attrib
.is_64
? 1 : 0));
155 pbm
->msi_irq_table
[msi
- pbm
->msi_first
] = *irq_p
;
157 if (entry
->pci
.msi_attrib
.is_64
) {
158 msg
.address_hi
= pbm
->msi64_start
>> 32;
159 msg
.address_lo
= pbm
->msi64_start
& 0xffffffff;
162 msg
.address_lo
= pbm
->msi32_start
;
166 irq_set_msi_desc(*irq_p
, entry
);
167 pci_write_msi_msg(*irq_p
, &msg
);
175 irq_set_chip(*irq_p
, NULL
);
183 static void sparc64_teardown_msi_irq(unsigned int irq
,
184 struct pci_dev
*pdev
)
186 struct pci_pbm_info
*pbm
= pdev
->dev
.archdata
.host_controller
;
187 const struct sparc64_msiq_ops
*ops
= pbm
->msi_ops
;
188 unsigned int msi_num
;
191 for (i
= 0; i
< pbm
->msi_num
; i
++) {
192 if (pbm
->msi_irq_table
[i
] == irq
)
195 if (i
>= pbm
->msi_num
) {
196 pci_err(pdev
, "%s: teardown: No MSI for irq %u\n", pbm
->name
,
201 msi_num
= pbm
->msi_first
+ i
;
202 pbm
->msi_irq_table
[i
] = ~0U;
204 err
= ops
->msi_teardown(pbm
, msi_num
);
206 pci_err(pdev
, "%s: teardown: ops->teardown() on MSI %u, "
207 "irq %u, gives error %d\n", pbm
->name
, msi_num
, irq
,
212 free_msi(pbm
, msi_num
);
214 irq_set_chip(irq
, NULL
);
218 static int msi_bitmap_alloc(struct pci_pbm_info
*pbm
)
220 unsigned long size
, bits_per_ulong
;
222 bits_per_ulong
= sizeof(unsigned long) * 8;
223 size
= (pbm
->msi_num
+ (bits_per_ulong
- 1)) & ~(bits_per_ulong
- 1);
225 BUG_ON(size
% sizeof(unsigned long));
227 pbm
->msi_bitmap
= kzalloc(size
, GFP_KERNEL
);
228 if (!pbm
->msi_bitmap
)
234 static void msi_bitmap_free(struct pci_pbm_info
*pbm
)
236 kfree(pbm
->msi_bitmap
);
237 pbm
->msi_bitmap
= NULL
;
240 static int msi_table_alloc(struct pci_pbm_info
*pbm
)
244 size
= pbm
->msiq_num
* sizeof(struct sparc64_msiq_cookie
);
245 pbm
->msiq_irq_cookies
= kzalloc(size
, GFP_KERNEL
);
246 if (!pbm
->msiq_irq_cookies
)
249 for (i
= 0; i
< pbm
->msiq_num
; i
++) {
250 struct sparc64_msiq_cookie
*p
;
252 p
= &pbm
->msiq_irq_cookies
[i
];
254 p
->msiqid
= pbm
->msiq_first
+ i
;
257 size
= pbm
->msi_num
* sizeof(unsigned int);
258 pbm
->msi_irq_table
= kzalloc(size
, GFP_KERNEL
);
259 if (!pbm
->msi_irq_table
) {
260 kfree(pbm
->msiq_irq_cookies
);
261 pbm
->msiq_irq_cookies
= NULL
;
268 static void msi_table_free(struct pci_pbm_info
*pbm
)
270 kfree(pbm
->msiq_irq_cookies
);
271 pbm
->msiq_irq_cookies
= NULL
;
273 kfree(pbm
->msi_irq_table
);
274 pbm
->msi_irq_table
= NULL
;
277 static int bringup_one_msi_queue(struct pci_pbm_info
*pbm
,
278 const struct sparc64_msiq_ops
*ops
,
279 unsigned long msiqid
,
280 unsigned long devino
)
282 int irq
= ops
->msiq_build_irq(pbm
, msiqid
, devino
);
288 nid
= pbm
->numa_node
;
290 irq_set_affinity(irq
, cpumask_of_node(nid
));
292 err
= request_irq(irq
, sparc64_msiq_interrupt
, 0,
294 &pbm
->msiq_irq_cookies
[msiqid
- pbm
->msiq_first
]);
301 static int sparc64_bringup_msi_queues(struct pci_pbm_info
*pbm
,
302 const struct sparc64_msiq_ops
*ops
)
306 for (i
= 0; i
< pbm
->msiq_num
; i
++) {
307 unsigned long msiqid
= i
+ pbm
->msiq_first
;
308 unsigned long devino
= i
+ pbm
->msiq_first_devino
;
311 err
= bringup_one_msi_queue(pbm
, ops
, msiqid
, devino
);
319 void sparc64_pbm_msi_init(struct pci_pbm_info
*pbm
,
320 const struct sparc64_msiq_ops
*ops
)
325 val
= of_get_property(pbm
->op
->dev
.of_node
, "#msi-eqs", &len
);
326 if (!val
|| len
!= 4)
328 pbm
->msiq_num
= *val
;
330 const struct msiq_prop
{
335 const struct msi_range_prop
{
339 const struct addr_range_prop
{
348 val
= of_get_property(pbm
->op
->dev
.of_node
, "msi-eq-size", &len
);
349 if (!val
|| len
!= 4)
352 pbm
->msiq_ent_count
= *val
;
354 mqp
= of_get_property(pbm
->op
->dev
.of_node
,
355 "msi-eq-to-devino", &len
);
357 mqp
= of_get_property(pbm
->op
->dev
.of_node
,
358 "msi-eq-devino", &len
);
359 if (!mqp
|| len
!= sizeof(struct msiq_prop
))
362 pbm
->msiq_first
= mqp
->first_msiq
;
363 pbm
->msiq_first_devino
= mqp
->first_devino
;
365 val
= of_get_property(pbm
->op
->dev
.of_node
, "#msi", &len
);
366 if (!val
|| len
!= 4)
370 mrng
= of_get_property(pbm
->op
->dev
.of_node
, "msi-ranges", &len
);
371 if (!mrng
|| len
!= sizeof(struct msi_range_prop
))
373 pbm
->msi_first
= mrng
->first_msi
;
375 val
= of_get_property(pbm
->op
->dev
.of_node
, "msi-data-mask", &len
);
376 if (!val
|| len
!= 4)
378 pbm
->msi_data_mask
= *val
;
380 val
= of_get_property(pbm
->op
->dev
.of_node
, "msix-data-width", &len
);
381 if (!val
|| len
!= 4)
383 pbm
->msix_data_width
= *val
;
385 arng
= of_get_property(pbm
->op
->dev
.of_node
, "msi-address-ranges",
387 if (!arng
|| len
!= sizeof(struct addr_range_prop
))
389 pbm
->msi32_start
= ((u64
)arng
->msi32_high
<< 32) |
390 (u64
) arng
->msi32_low
;
391 pbm
->msi64_start
= ((u64
)arng
->msi64_high
<< 32) |
392 (u64
) arng
->msi64_low
;
393 pbm
->msi32_len
= arng
->msi32_len
;
394 pbm
->msi64_len
= arng
->msi64_len
;
396 if (msi_bitmap_alloc(pbm
))
399 if (msi_table_alloc(pbm
)) {
400 msi_bitmap_free(pbm
);
404 if (ops
->msiq_alloc(pbm
)) {
406 msi_bitmap_free(pbm
);
410 if (sparc64_bringup_msi_queues(pbm
, ops
)) {
413 msi_bitmap_free(pbm
);
417 printk(KERN_INFO
"%s: MSI Queue first[%u] num[%u] count[%u] "
420 pbm
->msiq_first
, pbm
->msiq_num
,
422 pbm
->msiq_first_devino
);
423 printk(KERN_INFO
"%s: MSI first[%u] num[%u] mask[0x%x] "
426 pbm
->msi_first
, pbm
->msi_num
, pbm
->msi_data_mask
,
427 pbm
->msix_data_width
);
428 printk(KERN_INFO
"%s: MSI addr32[0x%llx:0x%x] "
429 "addr64[0x%llx:0x%x]\n",
431 pbm
->msi32_start
, pbm
->msi32_len
,
432 pbm
->msi64_start
, pbm
->msi64_len
);
433 printk(KERN_INFO
"%s: MSI queues at RA [%016lx]\n",
435 __pa(pbm
->msi_queues
));
438 pbm
->setup_msi_irq
= sparc64_setup_msi_irq
;
439 pbm
->teardown_msi_irq
= sparc64_teardown_msi_irq
;
445 printk(KERN_INFO
"%s: No MSI support.\n", pbm
->name
);