why 'Delay is needed after doing I/O'?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kingwah
Posts: 3
Joined: Tue Jul 30, 2019 11:53 pm

why 'Delay is needed after doing I/O'?

Post by kingwah »

When I read the Linux kernel source code(Linux 3.10.0), I noticed following codes in the file 'arch/x86/boot/pm.c':

Code: Select all

 22 static void realmode_switch_hook(void)
 23 {
 24         if (boot_params.hdr.realmode_swtch) {
 25                 asm volatile("lcallw *%0"
 26                              : : "m" (boot_params.hdr.realmode_swtch)
 27                              : "eax", "ebx", "ecx", "edx");
 28         } else {
 29                 asm volatile("cli");
 30                 outb(0x80, 0x70); /* Disable NMI */
 31                 io_delay();
 32         }
 33 }
The function call 'io_delay()' here is to let the CPU wait for some time (by 'outb' a byte to the diagnostics IO Port 0x80), and my question is:
Why does it need to wait for some delay after doing I/O?
User avatar
iansjack
Member
Member
Posts: 4705
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: why 'Delay is needed after doing I/O'?

Post by iansjack »

Most devices require a delay after writing to their command registers to give the device time to respond to the command. Alternatively, there may be a status register that can be polled to indicate when a command has been processed.

Ignoring ths is the sort of thing that can cause an OS to run on an emulator but not a real CPU.
kingwah
Posts: 3
Joined: Tue Jul 30, 2019 11:53 pm

Re: why 'Delay is needed after doing I/O'?

Post by kingwah »

iansjack wrote:Most devices require a delay after writing to their command registers to give the device time to respond to the command. Alternatively, there may be a status register that can be polled to indicate when a command has been processed.

Ignoring ths is the sort of thing that can cause an OS to run on an emulator but not a real CPU.
Thank you very much, :)
User avatar
iansjack
Member
Member
Posts: 4705
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: why 'Delay is needed after doing I/O'?

Post by iansjack »

I should have mentioned another possibility. Some devices can be configured to issue an interrupt to notify the cpu that an operation has completed. Typically this is used with network controllers or drive controllers, where the time to complete some operations may be relatively long. That way the whole system doesn't have to stop waiting for an i/o operation. And, of course, a keyboard controller is normally configured this way.
Post Reply