فهرست منبع

Poll the PS/2 mouse from IRQ12

Input was sampled by the kworker every few milliseconds under the BKL,
so a busy renderer could delay the cursor and a CPU-bound command made
it visibly lag.  The AUX port already has an interrupt line; use it when
a hardware cursor is active, where the handler only has to write the
cursor registers and never touches the shadow buffer or damage list.

The keyboard IRQ must no longer consume AUX bytes: leave them for IRQ12
while the mouse is streaming, and discard them only when no one owns the
port.  The kworker still polls USB HID and falls back to PS/2 polling
when there is no hardware cursor.  It now runs every 2 ms at weight 4.

Verified on the X201: pointer and keyboard remain responsive with the
colors/browser open, and the QEMU OHCI cursor test still passes.
Johannes Zellner 2 هفته پیش
والد
کامیت
ce7ac628b7
5فایلهای تغییر یافته به همراه152 افزوده شده و 24 حذف شده
  1. 10 4
      io.c
  2. 4 2
      kernel.c
  3. 121 17
      mouse.c
  4. 3 1
      mouse.h
  5. 14 0
      timer.S

+ 10 - 4
io.c

@@ -4,6 +4,7 @@
 
 #include "io.h"
 #include "fbtext.h"
+#include "mouse.h"
 #include "process.h"
 #include "screen.h"
 #include "timer.h"
@@ -111,12 +112,17 @@ void kbd_irq(void)
 		st = inb(KB_STATUS);
 		if (!(st & 0x01))
 			break;
+		if (st & 0x20) {
+			/* AUX (mouse) byte: the IRQ12 handler or the mouse
+			 * poll owns it while streaming is enabled.  With no
+			 * owner, discard it so it cannot block the keyboard. */
+			if (mouse_streaming_enabled())
+				break;
+			(void)inb(KB_DATA);
+			continue;
+		}
 		{
 			unsigned char sc = inb(KB_DATA);
-			if (st & 0x20) {
-				/* AUX (mouse) byte: not ours, drop it. */
-				continue;
-			}
 			{
 				unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
 				if (next != kbd_tail) {

+ 4 - 2
kernel.c

@@ -61,6 +61,7 @@ static struct idtr_desc idtr;
 
 extern void pit_irq_stub(void);
 extern void kbd_irq_stub(void);
+extern void mouse_irq_stub(void);
 extern void fdc_irq_stub(void);
 extern void default_irq_stub(void);
 extern void resched_stub(void);
@@ -192,6 +193,7 @@ static void timer_init(void)
 	/* Hardware IRQ handlers. */
 	set_idt_gate(0x20, (unsigned int)pit_irq_stub);
 	set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
+	set_idt_gate(0x2c, (unsigned int)mouse_irq_stub);
 	set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
 
 	/* Local-APIC vectors: the AP timer and reschedule IPI, plus the
@@ -255,7 +257,7 @@ static void kworker_main(void)
 {
 	for (;;) {
 		system_housekeeping();
-		process_sleep_ms(5);
+		process_sleep_ms(2);
 	}
 }
 
@@ -518,7 +520,7 @@ void kernel_main(void)
 		serial_puts(SERIAL_COM1, "kworker: cannot create, using idle fallback\n");
 		kworker_pid = -1;
 	} else {
-		process_set_weight((unsigned int)kworker_pid, 1);
+		process_set_weight((unsigned int)kworker_pid, 4);
 		process_set_affinity((unsigned int)kworker_pid, 1);
 	}
 	boot_trace("kworker");

+ 121 - 17
mouse.c

@@ -29,13 +29,15 @@ static unsigned int mouse_id;
 static int mouse_cursor_flag;
 static unsigned int mouse_pkt_size;
 static unsigned int mouse_buttons;  /* bit0=left bit1=right bit2=middle */
-static int mouse_hw_cursor;         /* 1 = Cirrus, 2 = Radeon hardware cursor */
+static int mouse_hw_cursor;         /* 1 = Cirrus, 2 = Radeon, 3 = Intel */
+static int mouse_irq_mode;          /* PS/2 AUX IRQ12 owns packet input */
+static int mouse_aux_irq_ok;        /* controller config accepted AUX IRQ */
 
 static unsigned int fbw, fbh;
 static unsigned int cur_x, cur_y;
 static unsigned int saved_x, saved_y, saved_w, saved_h;
 static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
-static unsigned char pkt[3];
+static unsigned char pkt[4];
 static unsigned int pkt_idx;
 
 static inline unsigned char inb(unsigned short port)
@@ -63,6 +65,56 @@ static void enable_aux_port(void)
 	outb(PS2_CMD, 0xA8);
 }
 
+static inline unsigned int mouse_irq_save(void)
+{
+	unsigned int flags;
+
+	__asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
+	return flags;
+}
+
+static inline void mouse_irq_restore(unsigned int flags)
+{
+	__asm__ volatile ("pushl %0; popfl" :: "r"(flags));
+}
+
+/* Set the controller's AUX-interrupt bit.  The PIC line stays masked until
+ * mouse_init() has a hardware cursor and streaming is enabled. */
+static int ps2_aux_irq_config(void)
+{
+	unsigned int flags, spin = 100000;
+	unsigned char cfg;
+
+	flags = mouse_irq_save();
+	ps2_wait_input_empty();
+	outb(PS2_CMD, 0x20);
+	while (spin-- && !(inb(PS2_STATUS) & STATUS_OUT_FULL))
+		;
+	if (!(inb(PS2_STATUS) & STATUS_OUT_FULL)) {
+		mouse_irq_restore(flags);
+		return -1;
+	}
+	cfg = inb(PS2_DATA);
+	cfg |= 0x02;                    /* AUX (mouse) interrupt */
+	ps2_wait_input_empty();
+	outb(PS2_CMD, 0x60);
+	ps2_wait_input_empty();
+	outb(PS2_DATA, cfg);
+	mouse_irq_restore(flags);
+	return 0;
+}
+
+static void ps2_aux_irq_unmask(void)
+{
+	unsigned char mask;
+
+	/* Master IRQ2 is the cascade, slave IRQ12 is the AUX line. */
+	mask = inb(0x21);
+	outb(0x21, (unsigned char)(mask & ~0x04));
+	mask = inb(0xA1);
+	outb(0xA1, (unsigned char)(mask & ~0x10));
+}
+
 static void mouse_cmd_write(unsigned char val)
 {
 	ps2_wait_input_empty();
@@ -287,9 +339,12 @@ void mouse_init(void)
 	mouse_id = 0;
 	mouse_cursor_flag = 0;
 	mouse_pkt_size = 3;
+	mouse_irq_mode = 0;
+	mouse_aux_irq_ok = 0;
 	pkt_idx = 0;
 
 	enable_aux_port();
+	mouse_aux_irq_ok = (ps2_aux_irq_config() == 0);
 
 	if (mouse_send_cmd_ack(0xF5) != 0)
 		return;
@@ -310,6 +365,15 @@ void mouse_init(void)
 	if (mouse_send_cmd_ack(0xF4) != 0)
 		return;
 	mouse_streaming_flag = 1;
+
+	/* A hardware cursor makes the IRQ12 path safe: the handler only has
+	 * to write the cursor registers, not touch the shadow buffer or the
+	 * damage list.  Otherwise keep the kworker polling the controller. */
+	if (mouse_hw_cursor && mouse_aux_irq_ok) {
+		pkt_idx = 0;
+		mouse_irq_mode = 1;
+		ps2_aux_irq_unmask();
+	}
 }
 
 /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
@@ -329,6 +393,10 @@ static void mouse_move(int dx, int dy)
 		return;
 
 	if (mouse_hw_cursor) {
+		/* The PS/2 AUX IRQ can preempt a USB-driven update in the
+		 * kworker; keep the position read/modify/write atomic. */
+		unsigned int flags = mouse_irq_save();
+
 		cur_x = (unsigned int)nx;
 		cur_y = (unsigned int)ny;
 		if (mouse_hw_cursor == 3)
@@ -337,6 +405,7 @@ static void mouse_move(int dx, int dy)
 			radeon_cursor_move(cur_x, cur_y);
 		else
 			cirrus_cursor_move(cur_x, cur_y);
+		mouse_irq_restore(flags);
 	} else if (!mouse_auto_draw) {
 		/* A WM frame is between BEGIN and PRESENT: it erased the
 		 * software cursor and will redraw it at the new position. */
@@ -353,6 +422,53 @@ static void mouse_move(int dx, int dy)
 	}
 }
 
+/* Decode one complete PS/2 packet from pkt[] (3 or 4 bytes). */
+static void mouse_handle_packet(void)
+{
+	unsigned char b0 = pkt[0];
+	int dx = (int)(signed char)pkt[1];
+	int dy = (int)(signed char)pkt[2];
+
+	mouse_buttons = b0 & 0x07;
+	if (b0 & 0x10)
+		dx |= ~0xFF;
+	if (b0 & 0x20)
+		dy |= ~0xFF;
+	if ((b0 & 0xC0) != 0)
+		return;
+	mouse_move(dx, dy);
+}
+
+/* IRQ12: PS/2 AUX data.  With a hardware cursor the position update is just
+ * a few MMIO writes, so it is safe and fast in interrupt context.  This path
+ * is only enabled when mouse_irq_mode is set (hardware cursor present). */
+void mouse_irq(void)
+{
+	unsigned int guard = 0;
+
+	if (mouse_irq_mode) {
+		for (;;) {
+			unsigned char st;
+
+			if (guard++ >= 16)
+				break;
+			st = inb(PS2_STATUS);
+			if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
+			    (STATUS_OUT_FULL | STATUS_AUX_DATA))
+				break;
+			pkt[pkt_idx++] = inb(PS2_DATA);
+			if (pkt_idx < mouse_pkt_size)
+				continue;
+			pkt_idx = 0;
+			mouse_handle_packet();
+		}
+	}
+
+	/* Slave first, then the master cascade. */
+	outb(0xA0, 0x20);
+	outb(0x20, 0x20);
+}
+
 void mouse_poll_update(void)
 {
 	unsigned int guard = 0;
@@ -371,13 +487,13 @@ void mouse_poll_update(void)
 		mouse_move(usb_dx, usb_dy);
 	}
 
+	if (mouse_irq_mode)
+		return;                 /* IRQ12 owns the PS/2 packets */
 	if (!mouse_streaming_flag)
 		return;
 
 	for (;;) {
 		unsigned char st;
-		unsigned char b0;
-		int dx, dy;
 
 		/* Bound the drain: a stuck/emulated controller must not
 		 * wedge the WM loop; leftovers are read next iteration. */
@@ -393,19 +509,7 @@ void mouse_poll_update(void)
 		if (pkt_idx < mouse_pkt_size)
 			continue;
 		pkt_idx = 0;
-
-		b0 = pkt[0];
-		mouse_buttons = b0 & 0x07;
-		dx = (int)(signed char)pkt[1];
-		dy = (int)(signed char)pkt[2];
-		if (b0 & 0x10)
-			dx |= ~0xFF;
-		if (b0 & 0x20)
-			dy |= ~0xFF;
-		if ((b0 & 0xC0) != 0)
-			continue;
-
-		mouse_move(dx, dy);
+		mouse_handle_packet();
 	}
 }
 

+ 3 - 1
mouse.h

@@ -3,6 +3,8 @@
 
 void mouse_init(void);
 void mouse_poll_update(void);
+/* IRQ12 handler: PS/2 AUX data, used when a hardware cursor is active. */
+void mouse_irq(void);
 
 int mouse_is_present(void);
 /* 1 when a PS/2 mouse was detected; the protocol accessors below are
@@ -11,7 +13,7 @@ int mouse_ps2_present(void);
 int mouse_streaming_enabled(void);
 unsigned int mouse_device_id(void);
 int mouse_cursor_visible(void);
-/* Active cursor backend: 0 = software, 1 = Cirrus, 2 = Radeon. */
+/* Active cursor backend: 0 = software, 1 = Cirrus, 2 = Radeon, 3 = Intel. */
 int mouse_cursor_backend(void);
 /* Human-readable form of the above ("software", "nvidia hw", ...). */
 const char *mouse_cursor_backend_name(void);

+ 14 - 0
timer.S

@@ -21,6 +21,20 @@ kbd_irq_stub:
 	popa
 	iret
 
+.globl mouse_irq_stub
+mouse_irq_stub:
+	pusha
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  $0x28, %ax
+	movw  %ax, %gs
+	call  mouse_irq
+	IRQ_RETURN_DS 36
+	popa
+	iret
+
 .globl fdc_irq_stub
 fdc_irq_stub:
 	pusha