Slight optimisation of the uhci-hcd init code
Subsystems (2)
PATCH 1/1 Slight optimisation of the uhci-hcd init code
Hello,
While trying to understand why starting usb on my laptop made the bus master activity full I came accross a strange code in uhci-hcd: a seven level nested "if". The same thing can be achieved with a simgle ffz(). The attached patch should give to the code a bit better looking, on my x86 it even saves 96 bytes, cool ;-)
hoping you like it,
Eric Piel
PS: still, I'm not sure it's normal to see ffffffff as "bus master activity" in /proc/acpi/processor/CPU0/power as soon as uhci-hcd is loaded. In particular, it prevents the processor to go to C3 state. Could you give me your pint of view?
usb/host/uhci-hcd.c renamed from usb/host/uhci-hcd.c.orig +6 -24
@@ -52,6 +52,7 @@#include <asm/io.h> #include <asm/irq.h> #include <asm/system.h> +#include <asm/bitops.h> #include "../core/hcd.h" #include "uhci-hcd.h"
@@ -2213,10 +2214,11 @@ {struct uhci_hcd *uhci =hcd_to_uhci (hcd);int retval = -EBUSY ; -int i, port; +int port;unsigned io_size;dma_addr_t dma_handle;struct usb_device *udev; +unsigned long i;#ifdef CONFIG_PROC_FS struct proc_dir_entry *ent;#endif
@@ -2321,7 +2323,7 @@for (i =0 ; i <UHCI_NUM_SKELQH ; i++) { uhci->skelqh[i] =uhci_alloc_qh (uhci, udev);if (!uhci->skelqh[i]) { -err ("unable to allocate QH %d" , i); +err ("unable to allocate QH %lu" , i); goto err_alloc_skelqh; } }
@@ -2360,28 +2362,8 @@* us a reasonable dynamic range for irq latencies. */ for (i =0 ; i <UHCI_NUMFRAMES ; i++) { -int irq =0 ; - -if (i &1 ) { - irq++; -if (i &2 ) { - irq++; -if (i &4 ) { - irq++; -if (i &8 ) { - irq++; -if (i &16 ) { - irq++; -if (i &32 ) { - irq++; -if (i &64 ) - irq++; - } - } - } - } - } - } +unsigned long irq; + irq =ffz (i & ((1 <<7 ) -1 ));/* Only place we don't use the frame list routines */ uhci->fl->frame[i] =cpu_to_le32 (uhci->skelqh[7 - irq]->dma_handle);
Alan Stern
maintainerThanks for fowarding this, Johannes.
Quoted 10 lines · On Mon, 9 Feb 2004 [email protected] wrote:
Hello,
While trying to understand why starting usb on my laptop made the bus master activity full I came accross a strange code in uhci-hcd: a seven level nested "if". The same thing can be achieved with a simgle
ffz(). The attached patch should give to the code a bit better looking, on my x86 it even saves 96 bytes, cool ;-)hoping you like it,
Eric Piel
It's a nice idea. I was planning to alter the logic behind that nested "if" anyway, and I'll keep your recommendation in mind when I do it.
Quoted 3 lines
PS: still, I'm not sure it's normal to see ffffffff as "bus master activity" in
/proc/acpi/processor/CPU0/poweras soon as uhci-hcd is loaded. In particular, it prevents the processor to go to C3 state. Could you give me your pint of view?
I'm not sure exactly what the ffffffff value means -- maximum utilization?
Anyway, a UHCI host controller is a bus master and it can generate a lot of activity depending on what USB devices are plugged in. If no USB devices are plugged, the controller will be suspended after 1 second. Of course then it shouldn't be accessing the PCI bus at all.
There is one exception to this. Some manufacturers try to disable unused USB ports on their motherboards by tying the overcurrent input permanently high. (This seems to be particularly common among laptops.) With some Intel controllers this strategy generates "device-connected" interrupts, so the driver doesn't try to suspend the controller. There's a patch to fix this behavior currently being tested.
Alan Stern
Quoting Alan Stern <[email protected]>:
Quoted 2 lines
It's a nice idea. I was planning to alter the logic behind that nested "if" anyway, and I'll keep your recommendation in mind when I do it.
Great
Quoted 8 lines
Quoted 1 line
PS: still, I'm not sure it's normal to see ffffffff as "bus master
activity" in
Quoted 1 line
/proc/acpi/processor/CPU0/poweras soon as uhci-hcd is loaded. Inparticular, it
Quoted 1 line
prevents the processor to go to C3 state. Could you give me your pint of
view?
I'm not sure exactly what the ffffffff value means -- maximum utilization?
Yes, more exactly it means that the last 64 times the idle task was called the bus master was detected as being used.
Quoted 11 lines
Anyway, a UHCI host controller is a bus master and it can generate a lot of activity depending on what USB devices are plugged in. If no USB devices are plugged, the controller will be suspended after 1 second. Of course then it shouldn't be accessing the PCI bus at all.
There is one exception to this. Some manufacturers try to disable unused USB ports on their motherboards by tying the overcurrent input permanently high. (This seems to be particularly common among laptops.) With some Intel controllers this strategy generates "device-connected" interrupts, so the driver doesn't try to suspend the controller. There's a patch to fix this behavior currently being tested.
Well, my laptop seems fine. When I unplug the mouse the bus master activity goes to 0000000. I've very little idea about how the bus master has to be used and even less about the uhci host controller. However, at a first glance I thought it was weird to see full bus master activity when all that is connected is a mouse not being used. Do you think it's normal?
Eric
Alan Stern
maintainerQuoted 3 lines · On Wed, 11 Feb 2004 [email protected] wrote:
Quoted 1 line
I'm not sure exactly what the ffffffff value means -- maximum utilization?
Yes, more exactly it means that the last 64 times the idle task was called the bus master was detected as being used.
By "detected as being used", do you mean that the device was actually carrying out bus transactions or only that it was enabled as a bus master? And if it's the first, do you mean that the bus activity was occurring just as the idle task happened to check or that some bus activity had occurred since the last time the idle task checked?
USB controllers are more or less permanently enabled, but they only create bus activity when they actually have some work to do. If the only USB device you have connected is a mouse then there should be a small amount of bus activity occuring once every millisecond, with a little more than that every 32 milliseconds (or whatever polling period your mouse uses -- look in /proc/bus/usb/devices), and even more when you actually move the mouse or press a button.
Quoted 5 lines
Well, my laptop seems fine. When I unplug the mouse the bus master activity goes to 0000000. I've very little idea about how the bus master has to be used and even less about the uhci host controller. However, at a first glance I thought it was weird to see full bus master activity when all that is connected is a mouse not being used. Do you think it's normal?
Since I don't really understand the value being reported, I can't say. You can judge for yourself from the explanation above.
Alan Stern
Alan Stern
maintainerEric, Johannes:
Thought you might like to see this... I need to make sure it works right, and then it's going in.
Alan Stern
===== uhci-hcd.c 1.80 vs edited =====
drivers/usb/host/uhci-hcd.c +30 -35
@@ -2336,16 +2336,17 @@ }/* -* 8 Interrupt queues; link int2 to int1, int4 to int2, etc +* 8 Interrupt queues; link all higher int queues to int1, * then link int1 to control and control to bulk */ - uhci->skel_int128_qh->link =cpu_to_le32 (uhci->skel_int64_qh->dma_handle) |UHCI_PTR_QH ; - uhci->skel_int64_qh->link =cpu_to_le32 (uhci->skel_int32_qh->dma_handle) |UHCI_PTR_QH ; - uhci->skel_int32_qh->link =cpu_to_le32 (uhci->skel_int16_qh->dma_handle) |UHCI_PTR_QH ; - uhci->skel_int16_qh->link =cpu_to_le32 (uhci->skel_int8_qh->dma_handle) |UHCI_PTR_QH ; - uhci->skel_int8_qh->link =cpu_to_le32 (uhci->skel_int4_qh->dma_handle) |UHCI_PTR_QH ; - uhci->skel_int4_qh->link =cpu_to_le32 (uhci->skel_int2_qh->dma_handle) |UHCI_PTR_QH ; - uhci->skel_int2_qh->link =cpu_to_le32 (uhci->skel_int1_qh->dma_handle) |UHCI_PTR_QH ; + uhci->skel_int128_qh->link = + uhci->skel_int64_qh->link = + uhci->skel_int32_qh->link = + uhci->skel_int16_qh->link = + uhci->skel_int8_qh->link = + uhci->skel_int4_qh->link = + uhci->skel_int2_qh->link = +cpu_to_le32 (uhci->skel_int1_qh->dma_handle) |UHCI_PTR_QH ; uhci->skel_int1_qh->link =cpu_to_le32 (uhci->skel_ls_control_qh->dma_handle) |UHCI_PTR_QH ; uhci->skel_ls_control_qh->link =cpu_to_le32 (uhci->skel_hs_control_qh->dma_handle) |UHCI_PTR_QH ;
@@ -2361,39 +2362,33 @@ uhci->skel_term_qh->element =cpu_to_le32 (uhci->term_td->dma_handle);/* -* Fill the frame list: make all entries point to -* the proper interrupt queue. +* Fill the frame list: make all entries point to the proper +* interrupt queue. * -* This is probably silly, but it's a simple way to -* scatter the interrupt queues in a way that gives -* us a reasonable dynamic range for irq latencies. +* The interrupt queues will be interleaved as evenly as possible. +* There's not much to be done about period-1 interrupts; they have +* to occur in every frame. But we can schedule period-2 interrupts +* in odd-numbered frames, period-4 interrupts in frames congruent +* to 2 (mod 4), and so on. This way each frame only has two +* interrupt QHs, which will help spread out bandwidth utilization. */ for (i =0 ; i <UHCI_NUMFRAMES ; i++) { -int irq =0 ; +int irq; -if (i &1 ) { - irq++; -if (i &2 ) { - irq++; -if (i &4 ) { - irq++; -if (i &8 ) { - irq++; -if (i &16 ) { - irq++; -if (i &32 ) { - irq++; -if (i &64 ) - irq++; - } - } - } - } - } - } +/* +* ffs (Find First bit Set) does exactly what we need: +* 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[6], +* 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc. +* ffs > 6 => not on any high-period queue, so use +* skel_int1_qh = skelqh[7]. +* Add UHCI_NUMFRAMES to insure at least one bit is set. +*/ + irq =6 - (int )__ffs (i +UHCI_NUMFRAMES ); +if (irq <0 ) + irq =7 ;/* Only place we don't use the frame list routines */ - uhci->fl->frame[i] =cpu_to_le32 (uhci->skelqh[7 - irq]->dma_handle); + uhci->fl->frame[i] =cpu_to_le32 (uhci->skelqh[irq]->dma_handle); }start_hc (uhci);