1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2024 Loongson Technology Corporation Limited
6 #include <asm/kvm_eiointc.h>
7 #include <asm/kvm_pch_pic.h>
8 #include <asm/kvm_vcpu.h>
9 #include <linux/count_zeros.h>
11 /* update the isr according to irq level and route irq to eiointc */
12 static void pch_pic_update_irq(struct loongarch_pch_pic
*s
, int irq
, int level
)
17 * set isr and route irq to eiointc and
18 * the route table is in htmsi_vector[]
21 if (mask
& s
->irr
& ~s
->mask
) {
23 irq
= s
->htmsi_vector
[irq
];
24 eiointc_set_irq(s
->kvm
->arch
.eiointc
, irq
, level
);
27 if (mask
& s
->isr
& ~s
->irr
) {
29 irq
= s
->htmsi_vector
[irq
];
30 eiointc_set_irq(s
->kvm
->arch
.eiointc
, irq
, level
);
35 /* update batch irqs, the irq_mask is a bitmap of irqs */
36 static void pch_pic_update_batch_irqs(struct loongarch_pch_pic
*s
, u64 irq_mask
, int level
)
40 /* find each irq by irqs bitmap and update each irq */
41 bits
= sizeof(irq_mask
) * 8;
42 irq
= find_first_bit((void *)&irq_mask
, bits
);
44 pch_pic_update_irq(s
, irq
, level
);
45 bitmap_clear((void *)&irq_mask
, irq
, 1);
46 irq
= find_first_bit((void *)&irq_mask
, bits
);
50 /* called when a irq is triggered in pch pic */
51 void pch_pic_set_irq(struct loongarch_pch_pic
*s
, int irq
, int level
)
57 s
->irr
|= mask
; /* set irr */
60 * In edge triggered mode, 0 does not mean to clear irq
61 * The irr register variable is cleared when cpu writes to the
62 * PCH_PIC_CLEAR_START address area
65 spin_unlock(&s
->lock
);
70 pch_pic_update_irq(s
, irq
, level
);
71 spin_unlock(&s
->lock
);
75 void pch_msi_set_irq(struct kvm
*kvm
, int irq
, int level
)
77 eiointc_set_irq(kvm
->arch
.eiointc
, irq
, level
);
81 * pch pic register is 64-bit, but it is accessed by 32-bit,
82 * so we use high to get whether low or high 32 bits we want
85 static u32
pch_pic_read_reg(u64
*s
, int high
)
89 /* read the high 32 bits when high is 1 */
90 return high
? (u32
)(val
>> 32) : (u32
)val
;
94 * pch pic register is 64-bit, but it is accessed by 32-bit,
95 * so we use high to get whether low or high 32 bits we want
98 static u32
pch_pic_write_reg(u64
*s
, int high
, u32 v
)
100 u64 val
= *s
, data
= v
;
104 * Clear val high 32 bits
105 * Write the high 32 bits when the high is 1
107 *s
= (val
<< 32 >> 32) | (data
<< 32);
111 * Clear val low 32 bits
112 * Write the low 32 bits when the high is 0
114 *s
= (val
>> 32 << 32) | v
;
119 static int loongarch_pch_pic_read(struct loongarch_pch_pic
*s
, gpa_t addr
, int len
, void *val
)
121 int offset
, index
, ret
= 0;
125 offset
= addr
- s
->pch_pic_base
;
129 case PCH_PIC_INT_ID_START
... PCH_PIC_INT_ID_END
:
131 int_id
|= (u64
)PCH_PIC_INT_ID_VER
<< 32;
133 int_id
|= (u64
)31 << (32 + 16);
135 int_id
|= PCH_PIC_INT_ID_VAL
;
136 *(u64
*)val
= int_id
;
138 case PCH_PIC_MASK_START
... PCH_PIC_MASK_END
:
139 offset
-= PCH_PIC_MASK_START
;
142 data
= pch_pic_read_reg(&s
->mask
, index
);
145 case PCH_PIC_HTMSI_EN_START
... PCH_PIC_HTMSI_EN_END
:
146 offset
-= PCH_PIC_HTMSI_EN_START
;
148 /* read htmsi enable reg */
149 data
= pch_pic_read_reg(&s
->htmsi_en
, index
);
152 case PCH_PIC_EDGE_START
... PCH_PIC_EDGE_END
:
153 offset
-= PCH_PIC_EDGE_START
;
155 /* read edge enable reg */
156 data
= pch_pic_read_reg(&s
->edge
, index
);
159 case PCH_PIC_AUTO_CTRL0_START
... PCH_PIC_AUTO_CTRL0_END
:
160 case PCH_PIC_AUTO_CTRL1_START
... PCH_PIC_AUTO_CTRL1_END
:
161 /* we only use default mode: fixed interrupt distribution mode */
164 case PCH_PIC_ROUTE_ENTRY_START
... PCH_PIC_ROUTE_ENTRY_END
:
165 /* only route to int0: eiointc */
168 case PCH_PIC_HTMSI_VEC_START
... PCH_PIC_HTMSI_VEC_END
:
169 offset
-= PCH_PIC_HTMSI_VEC_START
;
170 /* read htmsi vector */
171 data
= s
->htmsi_vector
[offset
];
174 case PCH_PIC_POLARITY_START
... PCH_PIC_POLARITY_END
:
175 /* we only use defalut value 0: high level triggered */
181 spin_unlock(&s
->lock
);
186 static int kvm_pch_pic_read(struct kvm_vcpu
*vcpu
,
187 struct kvm_io_device
*dev
,
188 gpa_t addr
, int len
, void *val
)
191 struct loongarch_pch_pic
*s
= vcpu
->kvm
->arch
.pch_pic
;
194 kvm_err("%s: pch pic irqchip not valid!\n", __func__
);
198 /* statistics of pch pic reading */
199 vcpu
->kvm
->stat
.pch_pic_read_exits
++;
200 ret
= loongarch_pch_pic_read(s
, addr
, len
, val
);
205 static int loongarch_pch_pic_write(struct loongarch_pch_pic
*s
, gpa_t addr
,
206 int len
, const void *val
)
209 u32 old
, data
, offset
, index
;
214 offset
= addr
- s
->pch_pic_base
;
218 case PCH_PIC_MASK_START
... PCH_PIC_MASK_END
:
219 offset
-= PCH_PIC_MASK_START
;
220 /* get whether high or low 32 bits we want to write */
222 old
= pch_pic_write_reg(&s
->mask
, index
, data
);
223 /* enable irq when mask value change to 0 */
224 irq
= (old
& ~data
) << (32 * index
);
225 pch_pic_update_batch_irqs(s
, irq
, 1);
226 /* disable irq when mask value change to 1 */
227 irq
= (~old
& data
) << (32 * index
);
228 pch_pic_update_batch_irqs(s
, irq
, 0);
230 case PCH_PIC_HTMSI_EN_START
... PCH_PIC_HTMSI_EN_END
:
231 offset
-= PCH_PIC_HTMSI_EN_START
;
233 pch_pic_write_reg(&s
->htmsi_en
, index
, data
);
235 case PCH_PIC_EDGE_START
... PCH_PIC_EDGE_END
:
236 offset
-= PCH_PIC_EDGE_START
;
238 /* 1: edge triggered, 0: level triggered */
239 pch_pic_write_reg(&s
->edge
, index
, data
);
241 case PCH_PIC_CLEAR_START
... PCH_PIC_CLEAR_END
:
242 offset
-= PCH_PIC_CLEAR_START
;
244 /* write 1 to clear edge irq */
245 old
= pch_pic_read_reg(&s
->irr
, index
);
247 * get the irq bitmap which is edge triggered and
248 * already set and to be cleared
250 irq
= old
& pch_pic_read_reg(&s
->edge
, index
) & data
;
251 /* write irr to the new state where irqs have been cleared */
252 pch_pic_write_reg(&s
->irr
, index
, old
& ~irq
);
253 /* update cleared irqs */
254 pch_pic_update_batch_irqs(s
, irq
, 0);
256 case PCH_PIC_AUTO_CTRL0_START
... PCH_PIC_AUTO_CTRL0_END
:
257 offset
-= PCH_PIC_AUTO_CTRL0_START
;
259 /* we only use default mode: fixed interrupt distribution mode */
260 pch_pic_write_reg(&s
->auto_ctrl0
, index
, 0);
262 case PCH_PIC_AUTO_CTRL1_START
... PCH_PIC_AUTO_CTRL1_END
:
263 offset
-= PCH_PIC_AUTO_CTRL1_START
;
265 /* we only use default mode: fixed interrupt distribution mode */
266 pch_pic_write_reg(&s
->auto_ctrl1
, index
, 0);
268 case PCH_PIC_ROUTE_ENTRY_START
... PCH_PIC_ROUTE_ENTRY_END
:
269 offset
-= PCH_PIC_ROUTE_ENTRY_START
;
270 /* only route to int0: eiointc */
271 s
->route_entry
[offset
] = 1;
273 case PCH_PIC_HTMSI_VEC_START
... PCH_PIC_HTMSI_VEC_END
:
274 /* route table to eiointc */
275 offset
-= PCH_PIC_HTMSI_VEC_START
;
276 s
->htmsi_vector
[offset
] = (u8
)data
;
278 case PCH_PIC_POLARITY_START
... PCH_PIC_POLARITY_END
:
279 offset
-= PCH_PIC_POLARITY_START
;
281 /* we only use defalut value 0: high level triggered */
282 pch_pic_write_reg(&s
->polarity
, index
, 0);
288 spin_unlock(&s
->lock
);
293 static int kvm_pch_pic_write(struct kvm_vcpu
*vcpu
,
294 struct kvm_io_device
*dev
,
295 gpa_t addr
, int len
, const void *val
)
298 struct loongarch_pch_pic
*s
= vcpu
->kvm
->arch
.pch_pic
;
301 kvm_err("%s: pch pic irqchip not valid!\n", __func__
);
305 /* statistics of pch pic writing */
306 vcpu
->kvm
->stat
.pch_pic_write_exits
++;
307 ret
= loongarch_pch_pic_write(s
, addr
, len
, val
);
312 static const struct kvm_io_device_ops kvm_pch_pic_ops
= {
313 .read
= kvm_pch_pic_read
,
314 .write
= kvm_pch_pic_write
,
317 static int kvm_pch_pic_init(struct kvm_device
*dev
, u64 addr
)
320 struct kvm
*kvm
= dev
->kvm
;
321 struct kvm_io_device
*device
;
322 struct loongarch_pch_pic
*s
= dev
->kvm
->arch
.pch_pic
;
324 s
->pch_pic_base
= addr
;
326 /* init device by pch pic writing and reading ops */
327 kvm_iodevice_init(device
, &kvm_pch_pic_ops
);
328 mutex_lock(&kvm
->slots_lock
);
329 /* register pch pic device */
330 ret
= kvm_io_bus_register_dev(kvm
, KVM_MMIO_BUS
, addr
, PCH_PIC_SIZE
, device
);
331 mutex_unlock(&kvm
->slots_lock
);
333 return (ret
< 0) ? -EFAULT
: 0;
336 /* used by user space to get or set pch pic registers */
337 static int kvm_pch_pic_regs_access(struct kvm_device
*dev
,
338 struct kvm_device_attr
*attr
,
341 int addr
, offset
, len
= 8, ret
= 0;
344 struct loongarch_pch_pic
*s
;
346 s
= dev
->kvm
->arch
.pch_pic
;
348 data
= (void __user
*)attr
->addr
;
350 /* get pointer to pch pic register by addr */
352 case PCH_PIC_MASK_START
:
355 case PCH_PIC_HTMSI_EN_START
:
358 case PCH_PIC_EDGE_START
:
361 case PCH_PIC_AUTO_CTRL0_START
:
364 case PCH_PIC_AUTO_CTRL1_START
:
367 case PCH_PIC_ROUTE_ENTRY_START
... PCH_PIC_ROUTE_ENTRY_END
:
368 offset
= addr
- PCH_PIC_ROUTE_ENTRY_START
;
369 p
= &s
->route_entry
[offset
];
372 case PCH_PIC_HTMSI_VEC_START
... PCH_PIC_HTMSI_VEC_END
:
373 offset
= addr
- PCH_PIC_HTMSI_VEC_START
;
374 p
= &s
->htmsi_vector
[offset
];
377 case PCH_PIC_INT_IRR_START
:
380 case PCH_PIC_INT_ISR_START
:
383 case PCH_PIC_POLARITY_START
:
391 /* write or read value according to is_write */
393 if (copy_from_user(p
, data
, len
))
396 if (copy_to_user(data
, p
, len
))
399 spin_unlock(&s
->lock
);
404 static int kvm_pch_pic_get_attr(struct kvm_device
*dev
,
405 struct kvm_device_attr
*attr
)
407 switch (attr
->group
) {
408 case KVM_DEV_LOONGARCH_PCH_PIC_GRP_REGS
:
409 return kvm_pch_pic_regs_access(dev
, attr
, false);
415 static int kvm_pch_pic_set_attr(struct kvm_device
*dev
,
416 struct kvm_device_attr
*attr
)
419 void __user
*uaddr
= (void __user
*)(long)attr
->addr
;
421 switch (attr
->group
) {
422 case KVM_DEV_LOONGARCH_PCH_PIC_GRP_CTRL
:
423 switch (attr
->attr
) {
424 case KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT
:
425 if (copy_from_user(&addr
, uaddr
, sizeof(addr
)))
428 if (!dev
->kvm
->arch
.pch_pic
) {
429 kvm_err("%s: please create pch_pic irqchip first!\n", __func__
);
433 return kvm_pch_pic_init(dev
, addr
);
435 kvm_err("%s: unknown group (%d) attr (%lld)\n", __func__
, attr
->group
,
439 case KVM_DEV_LOONGARCH_PCH_PIC_GRP_REGS
:
440 return kvm_pch_pic_regs_access(dev
, attr
, true);
446 static int kvm_setup_default_irq_routing(struct kvm
*kvm
)
449 u32 nr
= KVM_IRQCHIP_NUM_PINS
;
450 struct kvm_irq_routing_entry
*entries
;
452 entries
= kcalloc(nr
, sizeof(*entries
), GFP_KERNEL
);
456 for (i
= 0; i
< nr
; i
++) {
458 entries
[i
].type
= KVM_IRQ_ROUTING_IRQCHIP
;
459 entries
[i
].u
.irqchip
.irqchip
= 0;
460 entries
[i
].u
.irqchip
.pin
= i
;
462 ret
= kvm_set_irq_routing(kvm
, entries
, nr
, 0);
468 static int kvm_pch_pic_create(struct kvm_device
*dev
, u32 type
)
471 struct kvm
*kvm
= dev
->kvm
;
472 struct loongarch_pch_pic
*s
;
474 /* pch pic should not has been created */
475 if (kvm
->arch
.pch_pic
)
478 ret
= kvm_setup_default_irq_routing(kvm
);
482 s
= kzalloc(sizeof(struct loongarch_pch_pic
), GFP_KERNEL
);
486 spin_lock_init(&s
->lock
);
488 kvm
->arch
.pch_pic
= s
;
493 static void kvm_pch_pic_destroy(struct kvm_device
*dev
)
496 struct loongarch_pch_pic
*s
;
498 if (!dev
|| !dev
->kvm
|| !dev
->kvm
->arch
.pch_pic
)
502 s
= kvm
->arch
.pch_pic
;
503 /* unregister pch pic device and free it's memory */
504 kvm_io_bus_unregister_dev(kvm
, KVM_MMIO_BUS
, &s
->device
);
508 static struct kvm_device_ops kvm_pch_pic_dev_ops
= {
509 .name
= "kvm-loongarch-pch-pic",
510 .create
= kvm_pch_pic_create
,
511 .destroy
= kvm_pch_pic_destroy
,
512 .set_attr
= kvm_pch_pic_set_attr
,
513 .get_attr
= kvm_pch_pic_get_attr
,
516 int kvm_loongarch_register_pch_pic_device(void)
518 return kvm_register_device_ops(&kvm_pch_pic_dev_ops
, KVM_DEV_TYPE_LOONGARCH_PCHPIC
);